Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <arch/acpi.h> |
| 21 | #include <bcd.h> |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Kyösti Mälkki | c36af7b | 2014-11-18 12:41:16 +0200 | [diff] [blame] | 23 | #include <version.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 24 | #include <console/console.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 25 | #include <pc80/mc146818rtc.h> |
Stefan Reinauer | ca374d4 | 2008-01-18 16:16:45 +0000 | [diff] [blame] | 26 | #include <boot/coreboot_tables.h> |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 27 | #include <rtc.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | #include <string.h> |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 29 | #include <cbfs.h> |
| 30 | |
| 31 | /* There's no way around this include guard. option_table.h is autogenerated */ |
Stefan Reinauer | 10ec0fe | 2010-09-25 10:40:47 +0000 | [diff] [blame] | 32 | #if CONFIG_USE_OPTION_TABLE |
| 33 | #include "option_table.h" |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 34 | #else |
| 35 | #define LB_CKS_RANGE_START 0 |
| 36 | #define LB_CKS_RANGE_END 0 |
| 37 | #define LB_CKS_LOC 0 |
Stefan Reinauer | 10ec0fe | 2010-09-25 10:40:47 +0000 | [diff] [blame] | 38 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 39 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 40 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 41 | static void cmos_reset_date(void) |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 42 | { |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 43 | /* Now setup a default date equals to the build date */ |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 44 | struct rtc_time time = { |
| 45 | .sec = 0, |
| 46 | .min = 0, |
| 47 | .hour = 1, |
| 48 | .mday = bcd2bin(coreboot_build_date.day), |
| 49 | .mon = bcd2bin(coreboot_build_date.month), |
| 50 | .year = (bcd2bin(coreboot_build_date.century) * 100) + |
| 51 | bcd2bin(coreboot_build_date.year), |
| 52 | .wday = bcd2bin(coreboot_build_date.weekday) |
| 53 | }; |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 54 | rtc_set(&time); |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 55 | } |
| 56 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 57 | 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] | 58 | { |
Timothy Pearson | f20c6e8 | 2015-02-14 16:15:31 -0600 | [diff] [blame] | 59 | if (IS_ENABLED(CONFIG_STATIC_OPTION_TABLE)) |
| 60 | return 1; |
| 61 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 62 | int i; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 63 | u16 sum, old_sum; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 64 | sum = 0; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 65 | for (i = range_start; i <= range_end; i++) |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 66 | sum += cmos_read(i); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 67 | old_sum = ((cmos_read(cks_loc) << 8) | cmos_read(cks_loc + 1)) & |
| 68 | 0x0ffff; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 69 | return sum == old_sum; |
| 70 | } |
| 71 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 72 | 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] | 73 | { |
| 74 | int i; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 75 | u16 sum; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 76 | sum = 0; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 77 | for (i = range_start; i <= range_end; i++) |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 78 | sum += cmos_read(i); |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 79 | cmos_write(((sum >> 8) & 0x0ff), cks_loc); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 80 | cmos_write(((sum >> 0) & 0x0ff), cks_loc + 1); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #define RTC_CONTROL_DEFAULT (RTC_24H) |
| 84 | #define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ) |
Li-Ta Lo | 84a8028 | 2005-01-07 02:42:53 +0000 | [diff] [blame] | 85 | |
Duncan Laurie | 4b1610d | 2012-09-05 10:52:44 -0700 | [diff] [blame] | 86 | #ifndef __SMM__ |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 87 | void cmos_init(bool invalid) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 88 | { |
Werner Zeh | a8b03da | 2015-02-09 08:17:40 +0100 | [diff] [blame^] | 89 | bool cmos_invalid = invalid; |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 90 | bool checksum_invalid = false; |
| 91 | bool clear_cmos; |
| 92 | size_t i; |
| 93 | uint8_t x; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 94 | |
Martin Roth | 7312c54 | 2014-05-12 21:52:54 -0600 | [diff] [blame] | 95 | #ifndef __PRE_RAM__ |
Stefan Reinauer | 86ce7f9 | 2013-05-28 12:37:08 -0700 | [diff] [blame] | 96 | /* |
| 97 | * Avoid clearing pending interrupts and resetting the RTC control |
| 98 | * register in the resume path because the Linux kernel relies on |
| 99 | * this to know if it should restart the RTC timer queue if the wake |
| 100 | * was due to the RTC alarm. |
| 101 | */ |
Kyösti Mälkki | 9d9eb1e | 2014-06-20 05:38:07 +0300 | [diff] [blame] | 102 | if (acpi_is_wakeup_s3()) |
Stefan Reinauer | 86ce7f9 | 2013-05-28 12:37:08 -0700 | [diff] [blame] | 103 | return; |
Martin Roth | 7312c54 | 2014-05-12 21:52:54 -0600 | [diff] [blame] | 104 | #endif /* __PRE_RAM__ */ |
Stefan Reinauer | 86ce7f9 | 2013-05-28 12:37:08 -0700 | [diff] [blame] | 105 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 106 | printk(BIOS_DEBUG, "RTC Init\n"); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 107 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 108 | if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) { |
| 109 | /* See if there has been a CMOS power problem. */ |
| 110 | x = cmos_read(RTC_VALID); |
| 111 | cmos_invalid = !(x & RTC_VRT); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 112 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 113 | /* See if there is a CMOS checksum error */ |
| 114 | checksum_invalid = !cmos_checksum_valid(PC_CKS_RANGE_START, |
| 115 | PC_CKS_RANGE_END, PC_CKS_LOC); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 116 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 117 | clear_cmos = false; |
| 118 | } else { |
| 119 | clear_cmos = true; |
| 120 | } |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 121 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 122 | if (invalid || cmos_invalid || checksum_invalid) { |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 123 | if (clear_cmos) { |
| 124 | cmos_write(0, 0x01); |
| 125 | cmos_write(0, 0x03); |
| 126 | cmos_write(0, 0x05); |
| 127 | for (i = 10; i < 128; i++) |
| 128 | cmos_write(0, i); |
| 129 | } |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 130 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 131 | if (cmos_invalid) |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 132 | cmos_reset_date(); |
Stefan Reinauer | feadfb7 | 2012-11-06 18:39:41 -0800 | [diff] [blame] | 133 | |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 134 | printk(BIOS_WARNING, "RTC:%s%s%s%s\n", |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 135 | invalid ? " Clear requested":"", |
| 136 | cmos_invalid ? " Power Problem":"", |
| 137 | checksum_invalid ? " Checksum invalid":"", |
| 138 | clear_cmos ? " zeroing cmos":""); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 139 | } |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 140 | |
| 141 | /* Setup the real time clock */ |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 142 | cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 143 | /* Setup the frequency it operates at */ |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 144 | cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT); |
Vincent Palatin | fc1b9ee | 2012-08-07 16:05:14 -0700 | [diff] [blame] | 145 | /* Ensure all reserved bits are 0 in register D */ |
| 146 | cmos_write(RTC_VRT, RTC_VALID); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 147 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 148 | if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) { |
| 149 | /* See if there is a LB CMOS checksum error */ |
| 150 | checksum_invalid = !cmos_checksum_valid(LB_CKS_RANGE_START, |
| 151 | LB_CKS_RANGE_END,LB_CKS_LOC); |
| 152 | if (checksum_invalid) |
| 153 | printk(BIOS_DEBUG, "RTC: coreboot checksum invalid\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 154 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 155 | /* Make certain we have a valid checksum */ |
| 156 | cmos_set_checksum(PC_CKS_RANGE_START, PC_CKS_RANGE_END, PC_CKS_LOC); |
| 157 | } |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 158 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 159 | /* Clear any pending interrupts */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 160 | cmos_read(RTC_INTR_FLAGS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 161 | } |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 162 | #endif /* __SMM__ */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 163 | |
| 164 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 165 | /* |
| 166 | * This routine returns the value of the requested bits. |
| 167 | * input bit = bit count from the beginning of the cmos image |
| 168 | * length = number of bits to include in the value |
| 169 | * ret = a character pointer to where the value is to be returned |
| 170 | * returns CB_SUCCESS = successful, cb_err code if an error occurred |
| 171 | */ |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 172 | static enum cb_err get_cmos_value(unsigned long bit, unsigned long length, |
| 173 | void *vret) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 174 | { |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 175 | unsigned char *ret; |
| 176 | unsigned long byte,byte_bit; |
| 177 | unsigned long i; |
| 178 | unsigned char uchar; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 179 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 180 | /* |
| 181 | * The table is checked when it is built to ensure all |
| 182 | * values are valid. |
| 183 | */ |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 184 | ret = vret; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 185 | byte = bit / 8; /* find the byte where the data starts */ |
| 186 | byte_bit = bit % 8; /* find the bit in the byte where the data starts */ |
| 187 | if (length < 9) { /* one byte or less */ |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 188 | uchar = cmos_read(byte); /* load the byte */ |
| 189 | uchar >>= byte_bit; /* shift the bits to byte align */ |
| 190 | /* clear unspecified bits */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 191 | ret[0] = uchar & ((1 << length) - 1); |
| 192 | } else { /* more that one byte so transfer the whole bytes */ |
| 193 | for (i = 0; length; i++, length -= 8, byte++) { |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 194 | /* load the byte */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 195 | ret[i] = cmos_read(byte); |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 198 | return CB_SUCCESS; |
Luc Verhaegen | 9ceae90 | 2009-06-03 10:47:19 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 201 | enum cb_err get_option(void *dest, const char *name) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 202 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 203 | struct cmos_option_table *ct; |
| 204 | struct cmos_entries *ce; |
| 205 | size_t namelen; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 206 | int found = 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 207 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 208 | if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE)) |
| 209 | return CB_CMOS_OTABLE_DISABLED; |
| 210 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 211 | /* Figure out how long name is */ |
| 212 | namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 213 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 214 | /* find the requested entry record */ |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 215 | ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin", |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 216 | CBFS_COMPONENT_CMOS_LAYOUT, NULL); |
Patrick Georgi | cef3b89 | 2011-01-18 14:28:45 +0000 | [diff] [blame] | 217 | if (!ct) { |
Stefan Reinauer | 1babddb | 2011-10-14 15:22:52 -0700 | [diff] [blame] | 218 | printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. " |
| 219 | "Options are disabled\n"); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 220 | return CB_CMOS_LAYOUT_NOT_FOUND; |
Patrick Georgi | cef3b89 | 2011-01-18 14:28:45 +0000 | [diff] [blame] | 221 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 222 | ce = (struct cmos_entries*)((unsigned char *)ct + ct->header_length); |
| 223 | for(; ce->tag == LB_TAG_OPTION; |
| 224 | ce = (struct cmos_entries*)((unsigned char *)ce + ce->size)) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 225 | if (memcmp(ce->name, name, namelen) == 0) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 226 | found = 1; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 230 | if (!found) { |
Stefan Reinauer | 8e726b7 | 2010-03-29 23:01:35 +0000 | [diff] [blame] | 231 | printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 232 | return CB_CMOS_OPTION_NOT_FOUND; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 233 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 234 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 235 | if (get_cmos_value(ce->bit, ce->length, dest) != CB_SUCCESS) |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 236 | return CB_CMOS_ACCESS_ERROR; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 237 | 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] | 238 | return CB_CMOS_CHECKSUM_INVALID; |
| 239 | return CB_SUCCESS; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 240 | } |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 241 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 242 | static enum cb_err set_cmos_value(unsigned long bit, unsigned long length, |
| 243 | void *vret) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 244 | { |
| 245 | unsigned char *ret; |
| 246 | unsigned long byte,byte_bit; |
| 247 | unsigned long i; |
| 248 | unsigned char uchar, mask; |
| 249 | unsigned int chksum_update_needed = 0; |
| 250 | |
| 251 | ret = vret; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 252 | byte = bit / 8; /* find the byte where the data starts */ |
| 253 | byte_bit = bit % 8; /* find the bit where the data starts */ |
| 254 | if (length <= 8) { /* one byte or less */ |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 255 | mask = (1 << length) - 1; |
| 256 | mask <<= byte_bit; |
| 257 | |
| 258 | uchar = cmos_read(byte); |
| 259 | uchar &= ~mask; |
| 260 | uchar |= (ret[0] << byte_bit); |
| 261 | cmos_write(uchar, byte); |
| 262 | if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END) |
| 263 | chksum_update_needed = 1; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 264 | } else { /* more that one byte so transfer the whole bytes */ |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 265 | if (byte_bit || length % 8) |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 266 | return CB_ERR_ARG; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 267 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 268 | for (i = 0; length; i++, length -= 8, byte++) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 269 | cmos_write(ret[i], byte); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 270 | if (byte >= LB_CKS_RANGE_START && |
| 271 | byte <= LB_CKS_RANGE_END) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 272 | chksum_update_needed = 1; |
| 273 | } |
| 274 | |
| 275 | if (chksum_update_needed) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 276 | cmos_set_checksum(LB_CKS_RANGE_START, LB_CKS_RANGE_END, |
| 277 | LB_CKS_LOC); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 278 | } |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 279 | return CB_SUCCESS; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 283 | enum cb_err set_option(const char *name, void *value) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 284 | { |
| 285 | struct cmos_option_table *ct; |
| 286 | struct cmos_entries *ce; |
| 287 | unsigned long length; |
| 288 | size_t namelen; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 289 | int found = 0; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 290 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 291 | if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE)) |
| 292 | return CB_CMOS_OTABLE_DISABLED; |
| 293 | |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 294 | /* Figure out how long name is */ |
| 295 | namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); |
| 296 | |
| 297 | /* find the requested entry record */ |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 298 | ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin", |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 299 | CBFS_COMPONENT_CMOS_LAYOUT, NULL); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 300 | if (!ct) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 301 | printk(BIOS_ERR, "cmos_layout.bin could not be found. " |
| 302 | "Options are disabled\n"); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 303 | return CB_CMOS_LAYOUT_NOT_FOUND; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 304 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 305 | ce = (struct cmos_entries*)((unsigned char *)ct + ct->header_length); |
| 306 | for(; ce->tag == LB_TAG_OPTION; |
| 307 | ce = (struct cmos_entries*)((unsigned char *)ce + ce->size)) { |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 308 | if (memcmp(ce->name, name, namelen) == 0) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 309 | found = 1; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 313 | if (!found) { |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 314 | printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 315 | return CB_CMOS_OPTION_NOT_FOUND; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | length = ce->length; |
| 319 | if (ce->config == 's') { |
| 320 | length = MAX(strlen((const char *)value) * 8, ce->length - 8); |
| 321 | /* make sure the string is null terminated */ |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 322 | if (set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0}) |
| 323 | != CB_SUCCESS) |
| 324 | return (CB_CMOS_ACCESS_ERROR); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 327 | if (set_cmos_value(ce->bit, length, value) != CB_SUCCESS) |
| 328 | return (CB_CMOS_ACCESS_ERROR); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 329 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 330 | return CB_SUCCESS; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 331 | } |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | * If the CMOS is cleared, the rtc_reg has the invalid date. That |
| 335 | * hurts some OSes. Even if we don't set USE_OPTION_TABLE, we need |
| 336 | * to make sure the date is valid. |
| 337 | */ |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 338 | void cmos_check_update_date() |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 339 | { |
| 340 | u8 year, century; |
| 341 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 342 | /* Assume hardware always supports RTC_CLK_ALTCENTURY. */ |
| 343 | century = cmos_read(RTC_CLK_ALTCENTURY); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 344 | year = cmos_read(RTC_CLK_YEAR); |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 345 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 346 | /* |
| 347 | * TODO: If century is 0xFF, 100% that the cmos is cleared. |
| 348 | * Other than that, so far rtc_year is the only entry to check |
| 349 | * if the date is valid. |
| 350 | */ |
| 351 | if (century > 0x99 || year > 0x99) /* Invalid date */ |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 352 | cmos_reset_date(); |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 355 | int rtc_set(const struct rtc_time *time){ |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 356 | cmos_write(bin2bcd(time->sec), RTC_CLK_SECOND); |
| 357 | cmos_write(bin2bcd(time->min), RTC_CLK_MINUTE); |
| 358 | cmos_write(bin2bcd(time->hour), RTC_CLK_HOUR); |
| 359 | cmos_write(bin2bcd(time->mday), RTC_CLK_DAYOFMONTH); |
| 360 | cmos_write(bin2bcd(time->mon), RTC_CLK_MONTH); |
| 361 | cmos_write(bin2bcd(time->year % 100), RTC_CLK_YEAR); |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 362 | /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */ |
| 363 | cmos_write(bin2bcd(time->year / 100), RTC_CLK_ALTCENTURY); |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 364 | cmos_write(bin2bcd(time->wday + 1), RTC_CLK_DAYOFWEEK); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 368 | int rtc_get(struct rtc_time *time) |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 369 | { |
| 370 | time->sec = bcd2bin(cmos_read(RTC_CLK_SECOND)); |
| 371 | time->min = bcd2bin(cmos_read(RTC_CLK_MINUTE)); |
| 372 | time->hour = bcd2bin(cmos_read(RTC_CLK_HOUR)); |
| 373 | time->mday = bcd2bin(cmos_read(RTC_CLK_DAYOFMONTH)); |
| 374 | time->mon = bcd2bin(cmos_read(RTC_CLK_MONTH)); |
| 375 | time->year = bcd2bin(cmos_read(RTC_CLK_YEAR)); |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 376 | /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */ |
| 377 | time->year += bcd2bin(cmos_read(RTC_CLK_ALTCENTURY)) * 100; |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 378 | time->wday = bcd2bin(cmos_read(RTC_CLK_DAYOFWEEK)) - 1; |
| 379 | return 0; |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 380 | } |