1 #include <pc80/mc146818rtc.h>
3 #if CONFIG_HAVE_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 unsigned char cmos_read(unsigned char addr)
21 outb(addr, RTC_BASE_PORT + offs + 0);
22 return inb(RTC_BASE_PORT + offs + 1);
25 static void cmos_write(unsigned char val, unsigned char addr)
32 outb(addr, RTC_BASE_PORT + offs + 0);
33 outb(val, RTC_BASE_PORT + offs + 1);
36 static int cmos_error(void)
39 /* See if the cmos error condition has been flagged */
40 reg_d = cmos_read(RTC_REG_D);
41 return (reg_d & RTC_VRT) == 0;
44 static int cmos_chksum_valid(void)
46 #if CONFIG_HAVE_OPTION_TABLE == 1
48 unsigned long sum, old_sum;
50 /* Comput the cmos checksum */
51 for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) {
52 sum += cmos_read(addr);
54 sum = (sum & 0xffff) ^ 0xffff;
56 /* Read the stored checksum */
57 old_sum = cmos_read(LB_CKS_LOC) << 8;
58 old_sum |= cmos_read(LB_CKS_LOC+1);
60 return sum == old_sum;
67 static inline int last_boot_normal(void)
70 byte = cmos_read(RTC_BOOT_BYTE);
71 return (byte & (1 << 1));
74 static inline int do_normal_boot(void)
78 if (cmos_error() || !cmos_chksum_valid()) {
79 /* There are no impossible values, no checksums so just
80 * trust whatever value we have in the the cmos,
81 * but clear the fallback bit.
83 byte = cmos_read(RTC_BOOT_BYTE);
85 byte |= CONFIG_MAX_REBOOT_CNT << 4;
86 cmos_write(byte, RTC_BOOT_BYTE);
89 /* The RTC_BOOT_BYTE is now o.k. see where to go. */
90 byte = cmos_read(RTC_BOOT_BYTE);
92 /* Are we in normal mode? */
94 byte &= 0x0f; /* yes, clear the boot count */
97 /* Properly set the last boot flag */
99 if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
103 /* Are we already at the max count? */
104 if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
105 byte += 1 << 4; /* No, add 1 to the count */
108 byte &= 0xfc; /* Yes, put in fallback mode */
111 /* Save the boot byte */
112 cmos_write(byte, RTC_BOOT_BYTE);
114 return (byte & (1<<1));
117 static inline unsigned read_option(unsigned start, unsigned size, unsigned def)
119 #if CONFIG_USE_OPTION_TABLE == 1
121 byte = cmos_read(start/8);
122 return (byte >> (start & 7U)) & ((1U << size) - 1U);