Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 2 | #include <build.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 3 | #include <console/console.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 4 | #include <pc80/mc146818rtc.h> |
Stefan Reinauer | ca374d4 | 2008-01-18 16:16:45 +0000 | [diff] [blame] | 5 | #include <boot/coreboot_tables.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 6 | #include <string.h> |
Stefan Reinauer | 10ec0fe | 2010-09-25 10:40:47 +0000 | [diff] [blame] | 7 | #if CONFIG_USE_OPTION_TABLE |
| 8 | #include "option_table.h" |
Patrick Georgi | 2447937 | 2011-01-18 13:56:36 +0000 | [diff] [blame] | 9 | #include <cbfs.h> |
Stefan Reinauer | 10ec0fe | 2010-09-25 10:40:47 +0000 | [diff] [blame] | 10 | #endif |
Duncan Laurie | 4b1610d | 2012-09-05 10:52:44 -0700 | [diff] [blame] | 11 | #include <arch/acpi.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 12 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 13 | |
| 14 | static void cmos_update_date(u8 has_century) |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 15 | { |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 16 | /* Now setup a default date equals to the build date */ |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 17 | cmos_write(0, RTC_CLK_SECOND); |
| 18 | cmos_write(0, RTC_CLK_MINUTE); |
| 19 | cmos_write(1, RTC_CLK_HOUR); |
Zheng Bao | 0e6d0ed | 2012-11-09 19:55:04 +0800 | [diff] [blame] | 20 | cmos_write(COREBOOT_BUILD_WEEKDAY_BCD + 1, RTC_CLK_DAYOFWEEK); |
| 21 | cmos_write(COREBOOT_BUILD_DAY_BCD, RTC_CLK_DAYOFMONTH); |
Dave Frodin | 832452a | 2013-02-18 08:34:49 -0700 | [diff] [blame] | 22 | cmos_write(COREBOOT_BUILD_MONTH_BCD, RTC_CLK_MONTH); |
Zheng Bao | 0e6d0ed | 2012-11-09 19:55:04 +0800 | [diff] [blame] | 23 | cmos_write(COREBOOT_BUILD_YEAR_BCD, RTC_CLK_YEAR); |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 24 | if (has_century) cmos_write(0x20, RTC_CLK_ALTCENTURY); |
| 25 | } |
| 26 | |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 27 | #if CONFIG_USE_OPTION_TABLE |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 28 | static int cmos_checksum_valid(int range_start, int range_end, int cks_loc) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 29 | { |
| 30 | int i; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 31 | u16 sum, old_sum; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 32 | sum = 0; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 33 | for (i = range_start; i <= range_end; i++) |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 34 | sum += cmos_read(i); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 35 | old_sum = ((cmos_read(cks_loc) << 8) | cmos_read(cks_loc + 1)) & |
| 36 | 0x0ffff; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 37 | return sum == old_sum; |
| 38 | } |
| 39 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 40 | static void cmos_set_checksum(int range_start, int range_end, int cks_loc) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 41 | { |
| 42 | int i; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 43 | u16 sum; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 44 | sum = 0; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 45 | for (i = range_start; i <= range_end; i++) |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 46 | sum += cmos_read(i); |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 47 | cmos_write(((sum >> 8) & 0x0ff), cks_loc); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 48 | cmos_write(((sum >> 0) & 0x0ff), cks_loc + 1); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 49 | } |
Stefan Reinauer | 2549d52 | 2010-03-17 03:44:45 +0000 | [diff] [blame] | 50 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 51 | |
Stefan Reinauer | 2fbbea0 | 2010-01-16 13:27:39 +0000 | [diff] [blame] | 52 | #if CONFIG_ARCH_X86 |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 53 | #define RTC_CONTROL_DEFAULT (RTC_24H) |
| 54 | #define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ) |
Stefan Reinauer | 2fbbea0 | 2010-01-16 13:27:39 +0000 | [diff] [blame] | 55 | #else |
| 56 | #if CONFIG_ARCH_ALPHA |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 57 | #define RTC_CONTROL_DEFAULT (RTC_SQWE | RTC_24H) |
| 58 | #define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ) |
| 59 | #endif |
Stefan Reinauer | 2fbbea0 | 2010-01-16 13:27:39 +0000 | [diff] [blame] | 60 | #endif |
Li-Ta Lo | 84a8028 | 2005-01-07 02:42:53 +0000 | [diff] [blame] | 61 | |
Duncan Laurie | 4b1610d | 2012-09-05 10:52:44 -0700 | [diff] [blame] | 62 | #ifndef __SMM__ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 63 | void cmos_init(int invalid) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 64 | { |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 65 | int cmos_invalid = 0; |
| 66 | int checksum_invalid = 0; |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 67 | #if CONFIG_USE_OPTION_TABLE |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 68 | unsigned char x; |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 69 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 70 | |
Martin Roth | 7312c54 | 2014-05-12 21:52:54 -0600 | [diff] [blame] | 71 | #ifndef __PRE_RAM__ |
Stefan Reinauer | 86ce7f9 | 2013-05-28 12:37:08 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Avoid clearing pending interrupts and resetting the RTC control |
| 74 | * register in the resume path because the Linux kernel relies on |
| 75 | * this to know if it should restart the RTC timer queue if the wake |
| 76 | * was due to the RTC alarm. |
| 77 | */ |
Kyösti Mälkki | 9d9eb1e | 2014-06-20 05:38:07 +0300 | [diff] [blame] | 78 | if (acpi_is_wakeup_s3()) |
Stefan Reinauer | 86ce7f9 | 2013-05-28 12:37:08 -0700 | [diff] [blame] | 79 | return; |
Martin Roth | 7312c54 | 2014-05-12 21:52:54 -0600 | [diff] [blame] | 80 | #endif /* __PRE_RAM__ */ |
Stefan Reinauer | 86ce7f9 | 2013-05-28 12:37:08 -0700 | [diff] [blame] | 81 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 82 | printk(BIOS_DEBUG, "RTC Init\n"); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 83 | |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 84 | #if CONFIG_USE_OPTION_TABLE |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 85 | /* See if there has been a CMOS power problem. */ |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 86 | x = cmos_read(RTC_VALID); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 87 | cmos_invalid = !(x & RTC_VRT); |
| 88 | |
| 89 | /* See if there is a CMOS checksum error */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 90 | checksum_invalid = !cmos_checksum_valid(PC_CKS_RANGE_START, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 91 | PC_CKS_RANGE_END,PC_CKS_LOC); |
| 92 | |
Stefan Reinauer | 1babddb | 2011-10-14 15:22:52 -0700 | [diff] [blame] | 93 | #define CLEAR_CMOS 0 |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 94 | #else |
| 95 | #define CLEAR_CMOS 1 |
| 96 | #endif |
| 97 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 98 | if (invalid || cmos_invalid || checksum_invalid) { |
Stefan Reinauer | 1babddb | 2011-10-14 15:22:52 -0700 | [diff] [blame] | 99 | #if CLEAR_CMOS |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 100 | int i; |
| 101 | |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 102 | cmos_write(0, 0x01); |
| 103 | cmos_write(0, 0x03); |
| 104 | cmos_write(0, 0x05); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 105 | for (i = 10; i < 128; i++) |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 106 | cmos_write(0, i); |
Stefan Reinauer | feadfb7 | 2012-11-06 18:39:41 -0800 | [diff] [blame] | 107 | #endif |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 108 | if (cmos_invalid) |
| 109 | cmos_update_date(RTC_HAS_NO_ALTCENTURY); |
Stefan Reinauer | feadfb7 | 2012-11-06 18:39:41 -0800 | [diff] [blame] | 110 | |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 111 | printk(BIOS_WARNING, "RTC:%s%s%s%s\n", |
| 112 | invalid?" Clear requested":"", |
| 113 | cmos_invalid?" Power Problem":"", |
| 114 | checksum_invalid?" Checksum invalid":"", |
| 115 | CLEAR_CMOS?" zeroing cmos":""); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 116 | } |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 117 | |
| 118 | /* Setup the real time clock */ |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 119 | cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 120 | /* Setup the frequency it operates at */ |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 121 | cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT); |
Vincent Palatin | fc1b9ee | 2012-08-07 16:05:14 -0700 | [diff] [blame] | 122 | /* Ensure all reserved bits are 0 in register D */ |
| 123 | cmos_write(RTC_VRT, RTC_VALID); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 124 | |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 125 | #if CONFIG_USE_OPTION_TABLE |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 126 | /* See if there is a LB CMOS checksum error */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 127 | checksum_invalid = !cmos_checksum_valid(LB_CKS_RANGE_START, |
Stefan Reinauer | b5828d7 | 2010-03-29 17:14:28 +0000 | [diff] [blame] | 128 | LB_CKS_RANGE_END,LB_CKS_LOC); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 129 | if (checksum_invalid) |
Stefan Reinauer | 1babddb | 2011-10-14 15:22:52 -0700 | [diff] [blame] | 130 | printk(BIOS_DEBUG, "RTC: coreboot checksum invalid\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 131 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 132 | /* Make certain we have a valid checksum */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 133 | cmos_set_checksum(PC_CKS_RANGE_START, PC_CKS_RANGE_END, PC_CKS_LOC); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 134 | #endif |
| 135 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 136 | /* Clear any pending interrupts */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 137 | cmos_read(RTC_INTR_FLAGS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 138 | } |
Duncan Laurie | 4b1610d | 2012-09-05 10:52:44 -0700 | [diff] [blame] | 139 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 140 | |
| 141 | |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 142 | #if CONFIG_USE_OPTION_TABLE |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 143 | /* |
| 144 | * This routine returns the value of the requested bits. |
| 145 | * input bit = bit count from the beginning of the cmos image |
| 146 | * length = number of bits to include in the value |
| 147 | * ret = a character pointer to where the value is to be returned |
| 148 | * returns CB_SUCCESS = successful, cb_err code if an error occurred |
| 149 | */ |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 150 | static enum cb_err get_cmos_value(unsigned long bit, unsigned long length, |
| 151 | void *vret) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 152 | { |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 153 | unsigned char *ret; |
| 154 | unsigned long byte,byte_bit; |
| 155 | unsigned long i; |
| 156 | unsigned char uchar; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 157 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 158 | /* |
| 159 | * The table is checked when it is built to ensure all |
| 160 | * values are valid. |
| 161 | */ |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 162 | ret = vret; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 163 | byte = bit / 8; /* find the byte where the data starts */ |
| 164 | byte_bit = bit % 8; /* find the bit in the byte where the data starts */ |
| 165 | if (length < 9) { /* one byte or less */ |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 166 | uchar = cmos_read(byte); /* load the byte */ |
| 167 | uchar >>= byte_bit; /* shift the bits to byte align */ |
| 168 | /* clear unspecified bits */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 169 | ret[0] = uchar & ((1 << length) - 1); |
| 170 | } else { /* more that one byte so transfer the whole bytes */ |
| 171 | for (i = 0; length; i++, length -= 8, byte++) { |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 172 | /* load the byte */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 173 | ret[i] = cmos_read(byte); |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 176 | return CB_SUCCESS; |
Luc Verhaegen | 9ceae90 | 2009-06-03 10:47:19 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 179 | enum cb_err get_option(void *dest, const char *name) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 180 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 181 | struct cmos_option_table *ct; |
| 182 | struct cmos_entries *ce; |
| 183 | size_t namelen; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 184 | int found = 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 185 | |
| 186 | /* Figure out how long name is */ |
| 187 | namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 188 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 189 | /* find the requested entry record */ |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 190 | ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin", |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 191 | CBFS_COMPONENT_CMOS_LAYOUT, NULL); |
Patrick Georgi | cef3b89 | 2011-01-18 14:28:45 +0000 | [diff] [blame] | 192 | if (!ct) { |
Stefan Reinauer | 1babddb | 2011-10-14 15:22:52 -0700 | [diff] [blame] | 193 | printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. " |
| 194 | "Options are disabled\n"); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 195 | return CB_CMOS_LAYOUT_NOT_FOUND; |
Patrick Georgi | cef3b89 | 2011-01-18 14:28:45 +0000 | [diff] [blame] | 196 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 197 | ce = (struct cmos_entries*)((unsigned char *)ct + ct->header_length); |
| 198 | for(; ce->tag == LB_TAG_OPTION; |
| 199 | ce = (struct cmos_entries*)((unsigned char *)ce + ce->size)) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 200 | if (memcmp(ce->name, name, namelen) == 0) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 201 | found = 1; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 205 | if (!found) { |
Stefan Reinauer | 8e726b7 | 2010-03-29 23:01:35 +0000 | [diff] [blame] | 206 | printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 207 | return CB_CMOS_OPTION_NOT_FOUND; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 208 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 209 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 210 | if (get_cmos_value(ce->bit, ce->length, dest) != CB_SUCCESS) |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 211 | return CB_CMOS_ACCESS_ERROR; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 212 | if (!cmos_checksum_valid(LB_CKS_RANGE_START, LB_CKS_RANGE_END, LB_CKS_LOC)) |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 213 | return CB_CMOS_CHECKSUM_INVALID; |
| 214 | return CB_SUCCESS; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 215 | } |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 216 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 217 | static enum cb_err set_cmos_value(unsigned long bit, unsigned long length, |
| 218 | void *vret) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 219 | { |
| 220 | unsigned char *ret; |
| 221 | unsigned long byte,byte_bit; |
| 222 | unsigned long i; |
| 223 | unsigned char uchar, mask; |
| 224 | unsigned int chksum_update_needed = 0; |
| 225 | |
| 226 | ret = vret; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 227 | byte = bit / 8; /* find the byte where the data starts */ |
| 228 | byte_bit = bit % 8; /* find the bit where the data starts */ |
| 229 | if (length <= 8) { /* one byte or less */ |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 230 | mask = (1 << length) - 1; |
| 231 | mask <<= byte_bit; |
| 232 | |
| 233 | uchar = cmos_read(byte); |
| 234 | uchar &= ~mask; |
| 235 | uchar |= (ret[0] << byte_bit); |
| 236 | cmos_write(uchar, byte); |
| 237 | if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END) |
| 238 | chksum_update_needed = 1; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 239 | } else { /* more that one byte so transfer the whole bytes */ |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 240 | if (byte_bit || length % 8) |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 241 | return CB_ERR_ARG; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 242 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 243 | for (i = 0; length; i++, length -= 8, byte++) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 244 | cmos_write(ret[i], byte); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 245 | if (byte >= LB_CKS_RANGE_START && |
| 246 | byte <= LB_CKS_RANGE_END) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 247 | chksum_update_needed = 1; |
| 248 | } |
| 249 | |
| 250 | if (chksum_update_needed) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 251 | cmos_set_checksum(LB_CKS_RANGE_START, LB_CKS_RANGE_END, |
| 252 | LB_CKS_LOC); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 253 | } |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 254 | return CB_SUCCESS; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 258 | enum cb_err set_option(const char *name, void *value) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 259 | { |
| 260 | struct cmos_option_table *ct; |
| 261 | struct cmos_entries *ce; |
| 262 | unsigned long length; |
| 263 | size_t namelen; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 264 | int found = 0; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 265 | |
| 266 | /* Figure out how long name is */ |
| 267 | namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); |
| 268 | |
| 269 | /* find the requested entry record */ |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 270 | ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin", |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 271 | CBFS_COMPONENT_CMOS_LAYOUT, NULL); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 272 | if (!ct) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 273 | printk(BIOS_ERR, "cmos_layout.bin could not be found. " |
| 274 | "Options are disabled\n"); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 275 | return CB_CMOS_LAYOUT_NOT_FOUND; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 276 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 277 | ce = (struct cmos_entries*)((unsigned char *)ct + ct->header_length); |
| 278 | for(; ce->tag == LB_TAG_OPTION; |
| 279 | ce = (struct cmos_entries*)((unsigned char *)ce + ce->size)) { |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 280 | if (memcmp(ce->name, name, namelen) == 0) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 281 | found = 1; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 282 | break; |
| 283 | } |
| 284 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 285 | if (!found) { |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 286 | printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 287 | return CB_CMOS_OPTION_NOT_FOUND; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | length = ce->length; |
| 291 | if (ce->config == 's') { |
| 292 | length = MAX(strlen((const char *)value) * 8, ce->length - 8); |
| 293 | /* make sure the string is null terminated */ |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 294 | if (set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0}) |
| 295 | != CB_SUCCESS) |
| 296 | return (CB_CMOS_ACCESS_ERROR); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 299 | if (set_cmos_value(ce->bit, length, value) != CB_SUCCESS) |
| 300 | return (CB_CMOS_ACCESS_ERROR); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 301 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 302 | return CB_SUCCESS; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 303 | } |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 304 | #endif /* CONFIG_USE_OPTION_TABLE */ |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * If the CMOS is cleared, the rtc_reg has the invalid date. That |
| 308 | * hurts some OSes. Even if we don't set USE_OPTION_TABLE, we need |
| 309 | * to make sure the date is valid. |
| 310 | */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 311 | void cmos_check_update_date(u8 has_century) |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 312 | { |
| 313 | u8 year, century; |
| 314 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 315 | /* Note: Need to check if the hardware supports RTC_CLK_ALTCENTURY. */ |
| 316 | century = has_century ? cmos_read(RTC_CLK_ALTCENTURY) : 0; |
| 317 | year = cmos_read(RTC_CLK_YEAR); |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 318 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame^] | 319 | /* |
| 320 | * TODO: If century is 0xFF, 100% that the cmos is cleared. |
| 321 | * Other than that, so far rtc_year is the only entry to check |
| 322 | * if the date is valid. |
| 323 | */ |
| 324 | if (century > 0x99 || year > 0x99) /* Invalid date */ |
| 325 | cmos_update_date(has_century); |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 326 | } |