Disable CMOS recovery code for ROMCC boards as the CBFS code used for
[coreboot.git] / src / pc80 / mc146818rtc_early.c
1 #include <pc80/mc146818rtc.h>
2 #include <fallback.h>
3 #if CONFIG_USE_OPTION_TABLE
4 #include "option_table.h"
5 #endif
6
7 #ifndef CONFIG_MAX_REBOOT_CNT
8 #error "CONFIG_MAX_REBOOT_CNT not defined"
9 #endif
10 #if  CONFIG_MAX_REBOOT_CNT > 15
11 #error "CONFIG_MAX_REBOOT_CNT too high"
12 #endif
13
14 #if CONFIG_USE_CMOS_RECOVERY
15 #include <cbfs.h>
16 #include <console/loglevel.h>
17
18 int do_printk(int msg_level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
19 #define printk_warning(fmt, arg...) do_printk(BIOS_WARNING ,fmt, ##arg)
20 #define printk_debug(fmt, arg...) do_printk(BIOS_DEBUG ,fmt, ##arg)
21 #endif
22
23 static int cmos_error(void)
24 {
25         unsigned char reg_d;
26         /* See if the cmos error condition has been flagged */
27         reg_d = cmos_read(RTC_REG_D);
28         return (reg_d & RTC_VRT) == 0;
29 }
30
31 static int cmos_chksum_valid(void)
32 {
33 #if CONFIG_USE_OPTION_TABLE
34         unsigned char addr;
35         unsigned long sum, old_sum;
36         sum = 0;
37         /* Comput the cmos checksum */
38         for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) {
39                 sum += cmos_read(addr);
40         }
41         sum = (sum & 0xffff) ^ 0xffff;
42
43         /* Read the stored checksum */
44         old_sum = cmos_read(LB_CKS_LOC) << 8;
45         old_sum |=  cmos_read(LB_CKS_LOC+1);
46
47         return sum == old_sum;
48 #else
49         return 0;
50 #endif
51 }
52
53
54 static inline int last_boot_normal(void)
55 {
56         unsigned char byte;
57         byte = cmos_read(RTC_BOOT_BYTE);
58         return (byte & (1 << 1));
59 }
60
61 static inline int do_normal_boot(void)
62 {
63         unsigned char byte;
64         int i;
65
66         if (cmos_error() || !cmos_chksum_valid()) {
67 #if CONFIG_USE_CMOS_RECOVERY
68                 char *cmos_default = cbfs_find_file("cmos.default", 0xaa);
69                 if (cmos_default) {
70                         printk_warning("WARNING - CMOS CORRUPTED. RESTORING DEFAULTS.\n");
71                         /* First 14 bytes are reserved for
72                            RTC and ignored by nvramtool, too.
73                            Only 128 bytes: 128+ requires cmos configuration and
74                            contains only suspend-to-ram data, which isn't part
75                            of the recovery procedure. */
76                         for (i = 14; i < 128; i++) {
77                                 cmos_write(cmos_default[i], i);
78                         }
79                         /* Now reboot to run with default cmos. */
80                         outb(0x06, 0xcf9);
81                         for (;;) asm("hlt"); /* Wait for reset! */
82                 }
83 #endif
84
85                 /* There are no impossible values, no checksums so just
86                  * trust whatever value we have in the the cmos,
87                  * but clear the fallback bit.
88                  */
89                 byte = cmos_read(RTC_BOOT_BYTE);
90                 byte &= 0x0c;
91                 byte |= CONFIG_MAX_REBOOT_CNT << 4;
92                 cmos_write(byte, RTC_BOOT_BYTE);
93         }
94
95         /* The RTC_BOOT_BYTE is now o.k. see where to go. */
96         byte = cmos_read(RTC_BOOT_BYTE);
97
98         /* Are we in normal mode? */
99         if (byte & 1) {
100                 byte &= 0x0f; /* yes, clear the boot count */
101         }
102
103         /* Properly set the last boot flag */
104         byte &= 0xfc;
105         if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
106                 byte |= (1<<1);
107         }
108
109         /* Are we already at the max count? */
110         if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
111                 byte += 1 << 4; /* No, add 1 to the count */
112         }
113         else {
114                 byte &= 0xfc;   /* Yes, put in fallback mode */
115         }
116
117         /* Save the boot byte */
118         cmos_write(byte, RTC_BOOT_BYTE);
119
120         return (byte & (1<<1));
121 }
122
123 unsigned read_option(unsigned start, unsigned size, unsigned def)
124 {
125 #if CONFIG_USE_OPTION_TABLE
126         unsigned byte;
127         byte = cmos_read(start/8);
128         return (byte >> (start & 7U)) & ((1U << size) - 1U);
129 #else
130         return def;
131 #endif
132 }