2003-06-11 23:55:00 +02:00
|
|
|
#include <pc80/mc146818rtc.h>
|
2010-02-22 07:09:43 +01:00
|
|
|
#include <fallback.h>
|
2010-09-25 12:40:47 +02:00
|
|
|
#if CONFIG_USE_OPTION_TABLE
|
|
|
|
#include "option_table.h"
|
|
|
|
#endif
|
2003-06-11 23:55:00 +02:00
|
|
|
|
2009-06-30 17:17:49 +02:00
|
|
|
#ifndef CONFIG_MAX_REBOOT_CNT
|
|
|
|
#error "CONFIG_MAX_REBOOT_CNT not defined"
|
2003-06-17 10:42:17 +02:00
|
|
|
#endif
|
2009-06-30 17:17:49 +02:00
|
|
|
#if CONFIG_MAX_REBOOT_CNT > 15
|
|
|
|
#error "CONFIG_MAX_REBOOT_CNT too high"
|
2003-06-17 10:42:17 +02:00
|
|
|
#endif
|
|
|
|
|
2003-06-11 23:55:00 +02:00
|
|
|
static int cmos_error(void)
|
|
|
|
{
|
|
|
|
unsigned char reg_d;
|
|
|
|
/* See if the cmos error condition has been flagged */
|
|
|
|
reg_d = cmos_read(RTC_REG_D);
|
|
|
|
return (reg_d & RTC_VRT) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmos_chksum_valid(void)
|
|
|
|
{
|
2010-07-06 23:05:04 +02:00
|
|
|
#if CONFIG_USE_OPTION_TABLE
|
2003-06-11 23:55:00 +02:00
|
|
|
unsigned char addr;
|
|
|
|
unsigned long sum, old_sum;
|
|
|
|
sum = 0;
|
|
|
|
/* Comput the cmos checksum */
|
2010-03-29 19:14:28 +02:00
|
|
|
for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) {
|
2003-06-11 23:55:00 +02:00
|
|
|
sum += cmos_read(addr);
|
|
|
|
}
|
|
|
|
sum = (sum & 0xffff) ^ 0xffff;
|
|
|
|
|
|
|
|
/* Read the stored checksum */
|
2010-03-29 19:14:28 +02:00
|
|
|
old_sum = cmos_read(LB_CKS_LOC) << 8;
|
|
|
|
old_sum |= cmos_read(LB_CKS_LOC+1);
|
2003-06-11 23:55:00 +02:00
|
|
|
|
|
|
|
return sum == old_sum;
|
2010-03-30 01:01:35 +02:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2003-06-11 23:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-22 12:50:52 +01:00
|
|
|
static inline int last_boot_normal(void)
|
2003-07-19 06:28:22 +02:00
|
|
|
{
|
|
|
|
unsigned char byte;
|
|
|
|
byte = cmos_read(RTC_BOOT_BYTE);
|
|
|
|
return (byte & (1 << 1));
|
|
|
|
}
|
|
|
|
|
2010-03-22 12:50:52 +01:00
|
|
|
static inline int do_normal_boot(void)
|
2003-06-11 23:55:00 +02:00
|
|
|
{
|
|
|
|
unsigned char byte;
|
|
|
|
|
|
|
|
if (cmos_error() || !cmos_chksum_valid()) {
|
2009-04-22 18:23:47 +02:00
|
|
|
/* There are no impossible values, no checksums so just
|
2003-06-11 23:55:00 +02:00
|
|
|
* trust whatever value we have in the the cmos,
|
|
|
|
* but clear the fallback bit.
|
|
|
|
*/
|
|
|
|
byte = cmos_read(RTC_BOOT_BYTE);
|
|
|
|
byte &= 0x0c;
|
2009-06-30 17:17:49 +02:00
|
|
|
byte |= CONFIG_MAX_REBOOT_CNT << 4;
|
2003-06-11 23:55:00 +02:00
|
|
|
cmos_write(byte, RTC_BOOT_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The RTC_BOOT_BYTE is now o.k. see where to go. */
|
|
|
|
byte = cmos_read(RTC_BOOT_BYTE);
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2003-06-11 23:55:00 +02:00
|
|
|
/* Are we in normal mode? */
|
|
|
|
if (byte & 1) {
|
|
|
|
byte &= 0x0f; /* yes, clear the boot count */
|
|
|
|
}
|
|
|
|
|
2003-07-21 22:13:45 +02:00
|
|
|
/* Properly set the last boot flag */
|
|
|
|
byte &= 0xfc;
|
2009-06-30 17:17:49 +02:00
|
|
|
if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
|
2003-07-21 22:13:45 +02:00
|
|
|
byte |= (1<<1);
|
|
|
|
}
|
|
|
|
|
2003-06-11 23:55:00 +02:00
|
|
|
/* Are we already at the max count? */
|
2009-06-30 17:17:49 +02:00
|
|
|
if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
|
2003-06-11 23:55:00 +02:00
|
|
|
byte += 1 << 4; /* No, add 1 to the count */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
byte &= 0xfc; /* Yes, put in fallback mode */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the boot byte */
|
|
|
|
cmos_write(byte, RTC_BOOT_BYTE);
|
|
|
|
|
2003-07-21 22:13:45 +02:00
|
|
|
return (byte & (1<<1));
|
2003-06-11 23:55:00 +02:00
|
|
|
}
|
2004-03-11 16:01:31 +01:00
|
|
|
|
2011-05-10 23:53:13 +02:00
|
|
|
unsigned read_option_lowlevel(unsigned start, unsigned size, unsigned def)
|
2004-03-11 16:01:31 +01:00
|
|
|
{
|
2010-07-06 23:05:04 +02:00
|
|
|
#if CONFIG_USE_OPTION_TABLE
|
2004-03-11 16:01:31 +01:00
|
|
|
unsigned byte;
|
|
|
|
byte = cmos_read(start/8);
|
|
|
|
return (byte >> (start & 7U)) & ((1U << size) - 1U);
|
|
|
|
#else
|
|
|
|
return def;
|
|
|
|
#endif
|
|
|
|
}
|