blob: 6db9ec8e1a538412499e0c3a3dd4f997f934daa6 [file] [log] [blame]
Eric Biederman05f26fc2003-06-11 21:55:00 +00001#include <pc80/mc146818rtc.h>
2#include <part/fallback_boot.h>
3
Stefan Reinauer08670622009-06-30 15:17:49 +00004#ifndef CONFIG_MAX_REBOOT_CNT
5#error "CONFIG_MAX_REBOOT_CNT not defined"
Eric Biederman8d9c1232003-06-17 08:42:17 +00006#endif
Stefan Reinauer08670622009-06-30 15:17:49 +00007#if CONFIG_MAX_REBOOT_CNT > 15
8#error "CONFIG_MAX_REBOOT_CNT too high"
Eric Biederman8d9c1232003-06-17 08:42:17 +00009#endif
10
Eric Biederman05f26fc2003-06-11 21:55:00 +000011static unsigned char cmos_read(unsigned char addr)
12{
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000013 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);
Eric Biederman05f26fc2003-06-11 21:55:00 +000020}
21
22static void cmos_write(unsigned char val, unsigned char addr)
23{
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000024 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);
Eric Biederman05f26fc2003-06-11 21:55:00 +000031}
32
33static 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
41static int cmos_chksum_valid(void)
42{
43 unsigned char addr;
44 unsigned long sum, old_sum;
45 sum = 0;
46 /* Comput the cmos checksum */
Stefan Reinauer08670622009-06-30 15:17:49 +000047 for(addr = CONFIG_LB_CKS_RANGE_START; addr <= CONFIG_LB_CKS_RANGE_END; addr++) {
Eric Biederman05f26fc2003-06-11 21:55:00 +000048 sum += cmos_read(addr);
49 }
50 sum = (sum & 0xffff) ^ 0xffff;
51
52 /* Read the stored checksum */
Stefan Reinauer08670622009-06-30 15:17:49 +000053 old_sum = cmos_read(CONFIG_LB_CKS_LOC) << 8;
54 old_sum |= cmos_read(CONFIG_LB_CKS_LOC+1);
Eric Biederman05f26fc2003-06-11 21:55:00 +000055
56 return sum == old_sum;
57}
58
59
Eric Biederman9b4336c2003-07-19 04:28:22 +000060static int last_boot_normal(void)
61{
62 unsigned char byte;
63 byte = cmos_read(RTC_BOOT_BYTE);
64 return (byte & (1 << 1));
65}
66
Eric Biederman05f26fc2003-06-11 21:55:00 +000067static int do_normal_boot(void)
68{
69 unsigned char byte;
70
71 if (cmos_error() || !cmos_chksum_valid()) {
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000072 /* There are no impossible values, no checksums so just
Eric Biederman05f26fc2003-06-11 21:55:00 +000073 * 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;
Stefan Reinauer08670622009-06-30 15:17:49 +000078 byte |= CONFIG_MAX_REBOOT_CNT << 4;
Eric Biederman05f26fc2003-06-11 21:55:00 +000079 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
Eric Biederman2c018fb2003-07-21 20:13:45 +000090 /* Properly set the last boot flag */
91 byte &= 0xfc;
Stefan Reinauer08670622009-06-30 15:17:49 +000092 if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
Eric Biederman2c018fb2003-07-21 20:13:45 +000093 byte |= (1<<1);
94 }
95
Eric Biederman05f26fc2003-06-11 21:55:00 +000096 /* Are we already at the max count? */
Stefan Reinauer08670622009-06-30 15:17:49 +000097 if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) {
Eric Biederman05f26fc2003-06-11 21:55:00 +000098 byte += 1 << 4; /* No, add 1 to the count */
99 }
100 else {
101 byte &= 0xfc; /* Yes, put in fallback mode */
102 }
103
Eric Biederman05f26fc2003-06-11 21:55:00 +0000104 /* Save the boot byte */
105 cmos_write(byte, RTC_BOOT_BYTE);
106
Eric Biederman2c018fb2003-07-21 20:13:45 +0000107 return (byte & (1<<1));
Eric Biederman05f26fc2003-06-11 21:55:00 +0000108}
Eric Biederman5cd81732004-03-11 15:01:31 +0000109
110static unsigned read_option(unsigned start, unsigned size, unsigned def)
111{
Stefan Reinauer08670622009-06-30 15:17:49 +0000112#if CONFIG_USE_OPTION_TABLE == 1
Eric Biederman5cd81732004-03-11 15:01:31 +0000113 unsigned byte;
114 byte = cmos_read(start/8);
115 return (byte >> (start & 7U)) & ((1U << size) - 1U);
116#else
117 return def;
118#endif
119}