Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 1 | #include <pc80/mc146818rtc.h> |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 2 | #include <fallback.h> |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 3 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 4 | #ifndef CONFIG_MAX_REBOOT_CNT |
| 5 | #error "CONFIG_MAX_REBOOT_CNT not defined" |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 6 | #endif |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 7 | #if CONFIG_MAX_REBOOT_CNT > 15 |
| 8 | #error "CONFIG_MAX_REBOOT_CNT too high" |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 9 | #endif |
| 10 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 11 | static unsigned char cmos_read(unsigned char addr) |
| 12 | { |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 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); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | static void cmos_write(unsigned char val, unsigned char addr) |
| 23 | { |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 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); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 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 */ |
Stefan Reinauer | b5828d7 | 2010-03-29 17:14:28 +0000 | [diff] [blame^] | 47 | for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) { |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 48 | sum += cmos_read(addr); |
| 49 | } |
| 50 | sum = (sum & 0xffff) ^ 0xffff; |
| 51 | |
| 52 | /* Read the stored checksum */ |
Stefan Reinauer | b5828d7 | 2010-03-29 17:14:28 +0000 | [diff] [blame^] | 53 | old_sum = cmos_read(LB_CKS_LOC) << 8; |
| 54 | old_sum |= cmos_read(LB_CKS_LOC+1); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 55 | |
| 56 | return sum == old_sum; |
| 57 | } |
| 58 | |
| 59 | |
Stefan Reinauer | 53b0ea4 | 2010-03-22 11:50:52 +0000 | [diff] [blame] | 60 | static inline int last_boot_normal(void) |
Eric Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame] | 61 | { |
| 62 | unsigned char byte; |
| 63 | byte = cmos_read(RTC_BOOT_BYTE); |
| 64 | return (byte & (1 << 1)); |
| 65 | } |
| 66 | |
Stefan Reinauer | 53b0ea4 | 2010-03-22 11:50:52 +0000 | [diff] [blame] | 67 | static inline int do_normal_boot(void) |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 68 | { |
| 69 | unsigned char byte; |
| 70 | |
| 71 | if (cmos_error() || !cmos_chksum_valid()) { |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 72 | /* There are no impossible values, no checksums so just |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 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; |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 78 | byte |= CONFIG_MAX_REBOOT_CNT << 4; |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 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 | |
Eric Biederman | 2c018fb | 2003-07-21 20:13:45 +0000 | [diff] [blame] | 90 | /* Properly set the last boot flag */ |
| 91 | byte &= 0xfc; |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 92 | if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) { |
Eric Biederman | 2c018fb | 2003-07-21 20:13:45 +0000 | [diff] [blame] | 93 | byte |= (1<<1); |
| 94 | } |
| 95 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 96 | /* Are we already at the max count? */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 97 | if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) { |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 98 | byte += 1 << 4; /* No, add 1 to the count */ |
| 99 | } |
| 100 | else { |
| 101 | byte &= 0xfc; /* Yes, put in fallback mode */ |
| 102 | } |
| 103 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 104 | /* Save the boot byte */ |
| 105 | cmos_write(byte, RTC_BOOT_BYTE); |
| 106 | |
Eric Biederman | 2c018fb | 2003-07-21 20:13:45 +0000 | [diff] [blame] | 107 | return (byte & (1<<1)); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 108 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 109 | |
Stefan Reinauer | 53b0ea4 | 2010-03-22 11:50:52 +0000 | [diff] [blame] | 110 | static inline unsigned read_option(unsigned start, unsigned size, unsigned def) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 111 | { |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 112 | #if CONFIG_USE_OPTION_TABLE == 1 |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 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 | } |