1 #include <pc80/mc146818rtc.h>
3 #if CONFIG_USE_OPTION_TABLE
4 #include "option_table.h"
7 #ifndef CONFIG_MAX_REBOOT_CNT
8 #error "CONFIG_MAX_REBOOT_CNT not defined"
10 #if CONFIG_MAX_REBOOT_CNT > 15
11 #error "CONFIG_MAX_REBOOT_CNT too high"
14 static int cmos_error(void)
17 /* See if the cmos error condition has been flagged */
18 reg_d = cmos_read(RTC_REG_D);
19 return (reg_d & RTC_VRT) == 0;
22 static int cmos_chksum_valid(void)
24 #if CONFIG_USE_OPTION_TABLE
26 unsigned long sum, old_sum;
28 /* Comput the cmos checksum */
29 for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) {
30 sum += cmos_read(addr);
32 sum = (sum & 0xffff) ^ 0xffff;
34 /* Read the stored checksum */
35 old_sum = cmos_read(LB_CKS_LOC) << 8;
36 old_sum |= cmos_read(LB_CKS_LOC+1);
38 return sum == old_sum;
45 static inline int last_boot_normal(void)
48 byte = cmos_read(RTC_BOOT_BYTE);
49 return (byte & (1 << 1));
52 static inline int do_normal_boot(void)
56 if (cmos_error() || !cmos_chksum_valid()) {
57 /* There are no impossible values, no checksums so just
58 * trust whatever value we have in the the cmos,
59 * but clear the fallback bit.
61 byte = cmos_read(RTC_BOOT_BYTE);
63 byte |= CONFIG_MAX_REBOOT_CNT << 4;
64 cmos_write(byte, RTC_BOOT_BYTE);
67 /* The RTC_BOOT_BYTE is now o.k. see where to go. */
68 byte = cmos_read(RTC_BOOT_BYTE);
70 /* Are we in normal mode? */
72 byte &= 0x0f; /* yes, clear the boot count */
75 /* Properly set the last boot flag */
77 if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
81 /* Are we already at the max count? */
82 if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
83 byte += 1 << 4; /* No, add 1 to the count */
86 byte &= 0xfc; /* Yes, put in fallback mode */
89 /* Save the boot byte */
90 cmos_write(byte, RTC_BOOT_BYTE);
92 return (byte & (1<<1));
95 unsigned read_option(unsigned start, unsigned size, unsigned def)
97 #if CONFIG_USE_OPTION_TABLE
99 byte = cmos_read(start/8);
100 return (byte >> (start & 7U)) & ((1U << size) - 1U);