Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / pc80 / mc146818rtc_early.c
1 #include <pc80/mc146818rtc.h>
2 #include <fallback.h>
3 #if CONFIG_HAVE_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 static unsigned char cmos_read(unsigned char addr)
15 {
16         int offs = 0;
17         if (addr >= 128) {
18                 offs = 2;
19                 addr -= 128;
20         }
21         outb(addr, RTC_BASE_PORT + offs + 0);
22         return inb(RTC_BASE_PORT + offs + 1);
23 }
24
25 static void cmos_write(unsigned char val, unsigned char addr)
26 {
27         int offs = 0;
28         if (addr >= 128) {
29                 offs = 2;
30                 addr -= 128;
31         }
32         outb(addr, RTC_BASE_PORT + offs + 0);
33         outb(val, RTC_BASE_PORT + offs + 1);
34 }
35
36 static int cmos_error(void)
37 {
38         unsigned char reg_d;
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;
42 }
43
44 static int cmos_chksum_valid(void)
45 {
46 #if CONFIG_HAVE_OPTION_TABLE == 1
47         unsigned char addr;
48         unsigned long sum, old_sum;
49         sum = 0;
50         /* Comput the cmos checksum */
51         for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) {
52                 sum += cmos_read(addr);
53         }
54         sum = (sum & 0xffff) ^ 0xffff;
55
56         /* Read the stored checksum */
57         old_sum = cmos_read(LB_CKS_LOC) << 8;
58         old_sum |=  cmos_read(LB_CKS_LOC+1);
59
60         return sum == old_sum;
61 #else
62         return 0;
63 #endif
64 }
65
66
67 static inline int last_boot_normal(void)
68 {
69         unsigned char byte;
70         byte = cmos_read(RTC_BOOT_BYTE);
71         return (byte & (1 << 1));
72 }
73
74 static inline int do_normal_boot(void)
75 {
76         unsigned char byte;
77
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.
82                  */
83                 byte = cmos_read(RTC_BOOT_BYTE);
84                 byte &= 0x0c;
85                 byte |= CONFIG_MAX_REBOOT_CNT << 4;
86                 cmos_write(byte, RTC_BOOT_BYTE);
87         }
88
89         /* The RTC_BOOT_BYTE is now o.k. see where to go. */
90         byte = cmos_read(RTC_BOOT_BYTE);
91
92         /* Are we in normal mode? */
93         if (byte & 1) {
94                 byte &= 0x0f; /* yes, clear the boot count */
95         }
96
97         /* Properly set the last boot flag */
98         byte &= 0xfc;
99         if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
100                 byte |= (1<<1);
101         }
102
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 */
106         }
107         else {
108                 byte &= 0xfc;   /* Yes, put in fallback mode */
109         }
110
111         /* Save the boot byte */
112         cmos_write(byte, RTC_BOOT_BYTE);
113
114         return (byte & (1<<1));
115 }
116
117 static inline unsigned read_option(unsigned start, unsigned size, unsigned def)
118 {
119 #if CONFIG_USE_OPTION_TABLE == 1
120         unsigned byte;
121         byte = cmos_read(start/8);
122         return (byte >> (start & 7U)) & ((1U << size) - 1U);
123 #else
124         return def;
125 #endif
126 }