Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 2 | #include <pc80/mc146818rtc.h> |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 3 | #include <fallback.h> |
Stefan Reinauer | 10ec0fe | 2010-09-25 10:40:47 +0000 | [diff] [blame] | 4 | #if CONFIG_USE_OPTION_TABLE |
| 5 | #include "option_table.h" |
| 6 | #endif |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 7 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 8 | #ifndef CONFIG_MAX_REBOOT_CNT |
| 9 | #error "CONFIG_MAX_REBOOT_CNT not defined" |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 10 | #endif |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 11 | #if CONFIG_MAX_REBOOT_CNT > 15 |
| 12 | #error "CONFIG_MAX_REBOOT_CNT too high" |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 13 | #endif |
| 14 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 15 | static int cmos_error(void) |
| 16 | { |
| 17 | unsigned char reg_d; |
| 18 | /* See if the cmos error condition has been flagged */ |
| 19 | reg_d = cmos_read(RTC_REG_D); |
| 20 | return (reg_d & RTC_VRT) == 0; |
| 21 | } |
| 22 | |
| 23 | static int cmos_chksum_valid(void) |
| 24 | { |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 25 | #if CONFIG_USE_OPTION_TABLE |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 26 | unsigned char addr; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 27 | u16 sum, old_sum; |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 28 | sum = 0; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 29 | /* Compute the cmos checksum */ |
Stefan Reinauer | b5828d7 | 2010-03-29 17:14:28 +0000 | [diff] [blame] | 30 | for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) { |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 31 | sum += cmos_read(addr); |
| 32 | } |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 33 | |
| 34 | /* Read the stored checksum */ |
Stefan Reinauer | b5828d7 | 2010-03-29 17:14:28 +0000 | [diff] [blame] | 35 | old_sum = cmos_read(LB_CKS_LOC) << 8; |
| 36 | old_sum |= cmos_read(LB_CKS_LOC+1); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 37 | |
| 38 | return sum == old_sum; |
Stefan Reinauer | 8e726b7 | 2010-03-29 23:01:35 +0000 | [diff] [blame] | 39 | #else |
| 40 | return 0; |
| 41 | #endif |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
Edward O'Callaghan | ec79d7a | 2014-06-26 18:14:04 +1000 | [diff] [blame^] | 45 | static inline __attribute__((unused)) int last_boot_normal(void) |
Eric Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame] | 46 | { |
| 47 | unsigned char byte; |
| 48 | byte = cmos_read(RTC_BOOT_BYTE); |
| 49 | return (byte & (1 << 1)); |
| 50 | } |
| 51 | |
Edward O'Callaghan | ec79d7a | 2014-06-26 18:14:04 +1000 | [diff] [blame^] | 52 | static inline __attribute__((unused)) int do_normal_boot(void) |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 53 | { |
| 54 | unsigned char byte; |
| 55 | |
| 56 | if (cmos_error() || !cmos_chksum_valid()) { |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 57 | /* There are no impossible values, no checksums so just |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 58 | * trust whatever value we have in the the cmos, |
| 59 | * but clear the fallback bit. |
| 60 | */ |
| 61 | byte = cmos_read(RTC_BOOT_BYTE); |
| 62 | byte &= 0x0c; |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 63 | byte |= CONFIG_MAX_REBOOT_CNT << 4; |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 64 | cmos_write(byte, RTC_BOOT_BYTE); |
| 65 | } |
| 66 | |
| 67 | /* The RTC_BOOT_BYTE is now o.k. see where to go. */ |
| 68 | byte = cmos_read(RTC_BOOT_BYTE); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 69 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 70 | /* Are we in normal mode? */ |
| 71 | if (byte & 1) { |
| 72 | byte &= 0x0f; /* yes, clear the boot count */ |
| 73 | } |
| 74 | |
Eric Biederman | 2c018fb | 2003-07-21 20:13:45 +0000 | [diff] [blame] | 75 | /* Properly set the last boot flag */ |
| 76 | byte &= 0xfc; |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 77 | if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) { |
Eric Biederman | 2c018fb | 2003-07-21 20:13:45 +0000 | [diff] [blame] | 78 | byte |= (1<<1); |
| 79 | } |
| 80 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 81 | /* Are we already at the max count? */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 82 | if ((byte >> 4) < CONFIG_MAX_REBOOT_CNT) { |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 83 | byte += 1 << 4; /* No, add 1 to the count */ |
| 84 | } |
| 85 | else { |
| 86 | byte &= 0xfc; /* Yes, put in fallback mode */ |
| 87 | } |
| 88 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 89 | /* Save the boot byte */ |
| 90 | cmos_write(byte, RTC_BOOT_BYTE); |
| 91 | |
Eric Biederman | 2c018fb | 2003-07-21 20:13:45 +0000 | [diff] [blame] | 92 | return (byte & (1<<1)); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 93 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 94 | |
Patrick Georgi | b251753 | 2011-05-10 21:53:13 +0000 | [diff] [blame] | 95 | unsigned read_option_lowlevel(unsigned start, unsigned size, unsigned def) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 96 | { |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 97 | #if CONFIG_USE_OPTION_TABLE |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 98 | unsigned byte; |
| 99 | byte = cmos_read(start/8); |
| 100 | return (byte >> (start & 7U)) & ((1U << size) - 1U); |
| 101 | #else |
| 102 | return def; |
| 103 | #endif |
| 104 | } |