This is a general cleanup patch
[coreboot.git] / src / pc80 / mc146818rtc_early.c
1 #include <pc80/mc146818rtc.h>
2 #include <fallback.h>
3
4 #ifndef CONFIG_MAX_REBOOT_CNT
5 #error "CONFIG_MAX_REBOOT_CNT not defined"
6 #endif
7 #if  CONFIG_MAX_REBOOT_CNT > 15
8 #error "CONFIG_MAX_REBOOT_CNT too high"
9 #endif
10
11 static unsigned char cmos_read(unsigned char addr)
12 {
13         int offs = 0;
14         if (addr >= 128) {
15                 offs = 2;
16                 addr -= 128;
17         }
18         outb(addr, RTC_BASE_PORT + offs + 0);
19         return inb(RTC_BASE_PORT + offs + 1);
20 }
21
22 static void cmos_write(unsigned char val, unsigned char addr)
23 {
24         int offs = 0;
25         if (addr >= 128) {
26                 offs = 2;
27                 addr -= 128;
28         }
29         outb(addr, RTC_BASE_PORT + offs + 0);
30         outb(val, RTC_BASE_PORT + offs + 1);
31 }
32
33 static int cmos_error(void)
34 {
35         unsigned char reg_d;
36         /* See if the cmos error condition has been flagged */
37         reg_d = cmos_read(RTC_REG_D);
38         return (reg_d & RTC_VRT) == 0;
39 }
40
41 static int cmos_chksum_valid(void)
42 {
43         unsigned char addr;
44         unsigned long sum, old_sum;
45         sum = 0;
46         /* Comput the cmos checksum */
47         for(addr = CONFIG_LB_CKS_RANGE_START; addr <= CONFIG_LB_CKS_RANGE_END; addr++) {
48                 sum += cmos_read(addr);
49         }
50         sum = (sum & 0xffff) ^ 0xffff;
51
52         /* Read the stored checksum */
53         old_sum = cmos_read(CONFIG_LB_CKS_LOC) << 8;
54         old_sum |=  cmos_read(CONFIG_LB_CKS_LOC+1);
55
56         return sum == old_sum;
57 }
58
59
60 static int last_boot_normal(void)
61 {
62         unsigned char byte;
63         byte = cmos_read(RTC_BOOT_BYTE);
64         return (byte & (1 << 1));
65 }
66
67 static int do_normal_boot(void)
68 {
69         unsigned char byte;
70
71         if (cmos_error() || !cmos_chksum_valid()) {
72                 /* There are no impossible values, no checksums so just
73                  * trust whatever value we have in the the cmos,
74                  * but clear the fallback bit.
75                  */
76                 byte = cmos_read(RTC_BOOT_BYTE);
77                 byte &= 0x0c;
78                 byte |= CONFIG_MAX_REBOOT_CNT << 4;
79                 cmos_write(byte, RTC_BOOT_BYTE);
80         }
81
82         /* The RTC_BOOT_BYTE is now o.k. see where to go. */
83         byte = cmos_read(RTC_BOOT_BYTE);
84         
85         /* Are we in normal mode? */
86         if (byte & 1) {
87                 byte &= 0x0f; /* yes, clear the boot count */
88         }
89
90         /* Properly set the last boot flag */
91         byte &= 0xfc;
92         if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
93                 byte |= (1<<1);
94         }
95
96         /* Are we already at the max count? */
97         if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
98                 byte += 1 << 4; /* No, add 1 to the count */
99         }
100         else {
101                 byte &= 0xfc;   /* Yes, put in fallback mode */
102         }
103
104         /* Save the boot byte */
105         cmos_write(byte, RTC_BOOT_BYTE);
106
107         return (byte & (1<<1));
108 }
109
110 static unsigned read_option(unsigned start, unsigned size, unsigned def)
111 {
112 #if CONFIG_USE_OPTION_TABLE == 1
113         unsigned byte;
114         byte = cmos_read(start/8);
115         return (byte >> (start & 7U)) & ((1U << size) - 1U);
116 #else
117         return def;
118 #endif
119 }