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