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