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