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