blob: a102917d8d7465f3a18f16ef45c61f0ffc4d5652 [file] [log] [blame]
Marc Jones3cc685f2014-12-29 21:31:44 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 The Chromium OS Authors. All rights reserved.
Timothy Pearson7b22d842015-08-28 19:52:05 -05005 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Marc Jones3cc685f2014-12-29 21:31:44 -07006 *
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 Jones3cc685f2014-12-29 21:31:44 -070015 */
16
17#include <arch/acpi.h>
Kyösti Mälkki7ce1a752016-12-11 12:56:37 +020018#include <arch/io.h>
Marc Jones3cc685f2014-12-29 21:31:44 -070019#include <bcd.h>
Kyösti Mälkki7ce1a752016-12-11 12:56:37 +020020#include <fallback.h>
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020021#include <stdint.h>
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +020022#include <version.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000023#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000024#include <pc80/mc146818rtc.h>
Stefan Reinauerca374d42008-01-18 16:16:45 +000025#include <boot/coreboot_tables.h>
Marc Jones3cc685f2014-12-29 21:31:44 -070026#include <rtc.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000027#include <string.h>
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060028#include <cbfs.h>
29
30/* There's no way around this include guard. option_table.h is autogenerated */
Stefan Reinauer10ec0fe2010-09-25 10:40:47 +000031#if CONFIG_USE_OPTION_TABLE
32#include "option_table.h"
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060033#else
34#define LB_CKS_RANGE_START 0
35#define LB_CKS_RANGE_END 0
36#define LB_CKS_LOC 0
Stefan Reinauer10ec0fe2010-09-25 10:40:47 +000037#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000038
Timothy Pearson7b22d842015-08-28 19:52:05 -050039#include <smp/spinlock.h>
40
41#if (defined(__PRE_RAM__) && \
42IS_ENABLED(CONFIG_HAVE_ROMSTAGE_NVRAM_CBFS_SPINLOCK))
Stefan Reinauer86ddd732016-03-11 20:22:28 -080043 #define LOCK_NVRAM_CBFS_SPINLOCK() spin_lock(romstage_nvram_cbfs_lock())
44 #define UNLOCK_NVRAM_CBFS_SPINLOCK() spin_unlock(romstage_nvram_cbfs_lock())
Timothy Pearson7b22d842015-08-28 19:52:05 -050045#else
Stefan Reinauer86ddd732016-03-11 20:22:28 -080046 #define LOCK_NVRAM_CBFS_SPINLOCK() { }
47 #define UNLOCK_NVRAM_CBFS_SPINLOCK() { }
Timothy Pearson7b22d842015-08-28 19:52:05 -050048#endif
Gabe Blackb3f08c62014-04-30 17:12:25 -070049
Gabe Black03abaee212014-04-30 21:31:44 -070050static void cmos_reset_date(void)
zbaoa1e6a9c2012-08-02 19:02:26 +080051{
Vincent Palatinfa90fd42012-08-07 17:03:40 -070052 /* Now setup a default date equals to the build date */
Marc Jones3cc685f2014-12-29 21:31:44 -070053 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 Black03abaee212014-04-30 21:31:44 -070063 rtc_set(&time);
zbaoa1e6a9c2012-08-02 19:02:26 +080064}
65
Gabe Blackb3f08c62014-04-30 17:12:25 -070066static int cmos_checksum_valid(int range_start, int range_end, int cks_loc)
Eric Biederman8ca8d762003-04-22 19:02:15 +000067{
Stefan Reinauer86ddd732016-03-11 20:22:28 -080068 int i;
69 u16 sum, old_sum;
70
Timothy Pearsonf20c6e82015-02-14 16:15:31 -060071 if (IS_ENABLED(CONFIG_STATIC_OPTION_TABLE))
72 return 1;
73
Eric Biederman8ca8d762003-04-22 19:02:15 +000074 sum = 0;
Gabe Blackb3f08c62014-04-30 17:12:25 -070075 for (i = range_start; i <= range_end; i++)
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000076 sum += cmos_read(i);
Gabe Blackb3f08c62014-04-30 17:12:25 -070077 old_sum = ((cmos_read(cks_loc) << 8) | cmos_read(cks_loc + 1)) &
78 0x0ffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +000079 return sum == old_sum;
80}
81
Gabe Blackb3f08c62014-04-30 17:12:25 -070082static void cmos_set_checksum(int range_start, int range_end, int cks_loc)
Eric Biederman8ca8d762003-04-22 19:02:15 +000083{
84 int i;
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020085 u16 sum;
Stefan Reinauer86ddd732016-03-11 20:22:28 -080086
Eric Biederman8ca8d762003-04-22 19:02:15 +000087 sum = 0;
Gabe Blackb3f08c62014-04-30 17:12:25 -070088 for (i = range_start; i <= range_end; i++)
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000089 sum += cmos_read(i);
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000090 cmos_write(((sum >> 8) & 0x0ff), cks_loc);
Gabe Blackb3f08c62014-04-30 17:12:25 -070091 cmos_write(((sum >> 0) & 0x0ff), cks_loc + 1);
Eric Biederman8ca8d762003-04-22 19:02:15 +000092}
93
94#define RTC_CONTROL_DEFAULT (RTC_24H)
95#define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
Li-Ta Lo84a80282005-01-07 02:42:53 +000096
Duncan Laurie4b1610d2012-09-05 10:52:44 -070097#ifndef __SMM__
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060098void cmos_init(bool invalid)
Eric Biederman8ca8d762003-04-22 19:02:15 +000099{
Marshall Dawsonf8a274a2016-11-05 18:47:51 -0600100 bool cmos_invalid;
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600101 bool checksum_invalid = false;
102 bool clear_cmos;
103 size_t i;
104 uint8_t x;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000105
Martin Roth7312c542014-05-12 21:52:54 -0600106#ifndef __PRE_RAM__
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700107 /*
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älkki9d9eb1e2014-06-20 05:38:07 +0300113 if (acpi_is_wakeup_s3())
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700114 return;
Martin Roth7312c542014-05-12 21:52:54 -0600115#endif /* __PRE_RAM__ */
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700116
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000117 printk(BIOS_DEBUG, "RTC Init\n");
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000118
Marshall Dawsonf8a274a2016-11-05 18:47:51 -0600119 /* See if there has been a CMOS power problem. */
120 x = cmos_read(RTC_VALID);
121 cmos_invalid = !(x & RTC_VRT);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000122
Marshall Dawsonf8a274a2016-11-05 18:47:51 -0600123 if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) {
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600124 /* 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 Biederman8ca8d762003-04-22 19:02:15 +0000127
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600128 clear_cmos = false;
129 } else {
130 clear_cmos = true;
131 }
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700132
Marshall Dawsonf8a274a2016-11-05 18:47:51 -0600133 if (cmos_invalid || invalid)
134 cmos_write(cmos_read(RTC_CONTROL) | RTC_SET, RTC_CONTROL);
135
Eric Biederman8ca8d762003-04-22 19:02:15 +0000136 if (invalid || cmos_invalid || checksum_invalid) {
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600137 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 Palatinfa90fd42012-08-07 17:03:40 -0700144
Gabe Blackb3f08c62014-04-30 17:12:25 -0700145 if (cmos_invalid)
Gabe Black03abaee212014-04-30 21:31:44 -0700146 cmos_reset_date();
Stefan Reinauerfeadfb72012-11-06 18:39:41 -0800147
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700148 printk(BIOS_WARNING, "RTC:%s%s%s%s\n",
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600149 invalid ? " Clear requested":"",
150 cmos_invalid ? " Power Problem":"",
151 checksum_invalid ? " Checksum invalid":"",
152 clear_cmos ? " zeroing cmos":"");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000153 }
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000154
155 /* Setup the real time clock */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000156 cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000157 /* Setup the frequency it operates at */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000158 cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT);
Vincent Palatinfc1b9ee2012-08-07 16:05:14 -0700159 /* Ensure all reserved bits are 0 in register D */
160 cmos_write(RTC_VRT, RTC_VALID);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000161
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600162 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 Reinauer86ddd732016-03-11 20:22:28 -0800165 LB_CKS_RANGE_END, LB_CKS_LOC);
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600166 if (checksum_invalid)
167 printk(BIOS_DEBUG, "RTC: coreboot checksum invalid\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600169 /* 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. Magnani8cbc4752005-09-12 18:43:27 +0000172
Eric Biederman8ca8d762003-04-22 19:02:15 +0000173 /* Clear any pending interrupts */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700174 cmos_read(RTC_INTR_FLAGS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000175}
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600176#endif /* __SMM__ */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000177
178
Gabe Blackb3f08c62014-04-30 17:12:25 -0700179/*
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 Gagniucd7134e02013-11-23 18:54:44 -0600186static enum cb_err get_cmos_value(unsigned long bit, unsigned long length,
187 void *vret)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000188{
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000189 unsigned char *ret;
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800190 unsigned long byte, byte_bit;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000191 unsigned long i;
192 unsigned char uchar;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000193
Gabe Blackb3f08c62014-04-30 17:12:25 -0700194 /*
195 * The table is checked when it is built to ensure all
196 * values are valid.
197 */
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000198 ret = vret;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700199 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 Verhaegena9c5ea02009-06-03 14:19:33 +0000202 uchar = cmos_read(byte); /* load the byte */
203 uchar >>= byte_bit; /* shift the bits to byte align */
204 /* clear unspecified bits */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700205 ret[0] = uchar & ((1 << length) - 1);
Stefan Reinauer24850cc2016-05-04 16:16:47 -0700206 } else { /* more than one byte so transfer the whole bytes */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700207 for (i = 0; length; i++, length -= 8, byte++) {
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000208 /* load the byte */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700209 ret[i] = cmos_read(byte);
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000210 }
211 }
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600212 return CB_SUCCESS;
Luc Verhaegen9ceae902009-06-03 10:47:19 +0000213}
214
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600215enum cb_err get_option(void *dest, const char *name)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000216{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000217 struct cmos_option_table *ct;
218 struct cmos_entries *ce;
219 size_t namelen;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700220 int found = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000221
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600222 if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE))
223 return CB_CMOS_OTABLE_DISABLED;
224
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800225 LOCK_NVRAM_CBFS_SPINLOCK();
Timothy Pearson7b22d842015-08-28 19:52:05 -0500226
Eric Biederman8ca8d762003-04-22 19:02:15 +0000227 /* Figure out how long name is */
228 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000229
Eric Biederman8ca8d762003-04-22 19:02:15 +0000230 /* find the requested entry record */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500231 ct = cbfs_boot_map_with_leak("cmos_layout.bin",
232 CBFS_COMPONENT_CMOS_LAYOUT, NULL);
Patrick Georgicef3b892011-01-18 14:28:45 +0000233 if (!ct) {
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700234 printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. "
235 "Options are disabled\n");
Timothy Pearson7b22d842015-08-28 19:52:05 -0500236
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800237 UNLOCK_NVRAM_CBFS_SPINLOCK();
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600238 return CB_CMOS_LAYOUT_NOT_FOUND;
Patrick Georgicef3b892011-01-18 14:28:45 +0000239 }
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800240 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 Biederman8ca8d762003-04-22 19:02:15 +0000243 if (memcmp(ce->name, name, namelen) == 0) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700244 found = 1;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000245 break;
246 }
247 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700248 if (!found) {
Stefan Reinauer8e726b72010-03-29 23:01:35 +0000249 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800250 UNLOCK_NVRAM_CBFS_SPINLOCK();
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600251 return CB_CMOS_OPTION_NOT_FOUND;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000252 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000253
Timothy Pearson7b22d842015-08-28 19:52:05 -0500254 if (!cmos_checksum_valid(LB_CKS_RANGE_START, LB_CKS_RANGE_END, LB_CKS_LOC)) {
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800255 UNLOCK_NVRAM_CBFS_SPINLOCK();
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600256 return CB_CMOS_CHECKSUM_INVALID;
Timothy Pearson7b22d842015-08-28 19:52:05 -0500257 }
Martin Roth0e7a93f2017-01-25 11:00:18 -0700258 if (get_cmos_value(ce->bit, ce->length, dest) != CB_SUCCESS) {
259 UNLOCK_NVRAM_CBFS_SPINLOCK();
260 return CB_CMOS_ACCESS_ERROR;
261 }
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800262 UNLOCK_NVRAM_CBFS_SPINLOCK();
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600263 return CB_SUCCESS;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000264}
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200265
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600266static enum cb_err set_cmos_value(unsigned long bit, unsigned long length,
267 void *vret)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200268{
269 unsigned char *ret;
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800270 unsigned long byte, byte_bit;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200271 unsigned long i;
272 unsigned char uchar, mask;
273 unsigned int chksum_update_needed = 0;
274
275 ret = vret;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700276 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 Schnelled29e5bb2011-06-06 15:58:54 +0200279 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 Blackb3f08c62014-04-30 17:12:25 -0700288 } else { /* more that one byte so transfer the whole bytes */
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200289 if (byte_bit || length % 8)
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600290 return CB_ERR_ARG;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200291
Stefan Reinauer24850cc2016-05-04 16:16:47 -0700292 for (i = 0; length; i++, length -= 8, byte++) {
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200293 cmos_write(ret[i], byte);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700294 if (byte >= LB_CKS_RANGE_START &&
295 byte <= LB_CKS_RANGE_END)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200296 chksum_update_needed = 1;
Stefan Reinauer24850cc2016-05-04 16:16:47 -0700297 }
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200298 }
299
300 if (chksum_update_needed) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700301 cmos_set_checksum(LB_CKS_RANGE_START, LB_CKS_RANGE_END,
302 LB_CKS_LOC);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200303 }
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600304 return CB_SUCCESS;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200305}
306
307
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600308enum cb_err set_option(const char *name, void *value)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200309{
310 struct cmos_option_table *ct;
311 struct cmos_entries *ce;
312 unsigned long length;
313 size_t namelen;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700314 int found = 0;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200315
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600316 if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE))
317 return CB_CMOS_OTABLE_DISABLED;
318
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200319 /* Figure out how long name is */
320 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
321
322 /* find the requested entry record */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500323 ct = cbfs_boot_map_with_leak("cmos_layout.bin",
324 CBFS_COMPONENT_CMOS_LAYOUT, NULL);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200325 if (!ct) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700326 printk(BIOS_ERR, "cmos_layout.bin could not be found. "
327 "Options are disabled\n");
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600328 return CB_CMOS_LAYOUT_NOT_FOUND;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200329 }
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800330 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 Schnelled29e5bb2011-06-06 15:58:54 +0200333 if (memcmp(ce->name, name, namelen) == 0) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700334 found = 1;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200335 break;
336 }
337 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700338 if (!found) {
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200339 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600340 return CB_CMOS_OPTION_NOT_FOUND;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200341 }
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 Gagniucd7134e02013-11-23 18:54:44 -0600347 if (set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0})
348 != CB_SUCCESS)
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800349 return CB_CMOS_ACCESS_ERROR;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200350 }
351
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600352 if (set_cmos_value(ce->bit, length, value) != CB_SUCCESS)
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800353 return CB_CMOS_ACCESS_ERROR;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200354
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600355 return CB_SUCCESS;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200356}
zbaoa1e6a9c2012-08-02 19:02:26 +0800357
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 Reinauer86ddd732016-03-11 20:22:28 -0800363void cmos_check_update_date(void)
zbaoa1e6a9c2012-08-02 19:02:26 +0800364{
365 u8 year, century;
366
Gabe Black03abaee212014-04-30 21:31:44 -0700367 /* Assume hardware always supports RTC_CLK_ALTCENTURY. */
Marshall Dawson5a043fe2016-11-05 18:31:33 -0600368 wait_uip();
Gabe Black03abaee212014-04-30 21:31:44 -0700369 century = cmos_read(RTC_CLK_ALTCENTURY);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700370 year = cmos_read(RTC_CLK_YEAR);
zbaoa1e6a9c2012-08-02 19:02:26 +0800371
Gabe Blackb3f08c62014-04-30 17:12:25 -0700372 /*
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 Black03abaee212014-04-30 21:31:44 -0700378 cmos_reset_date();
Marc Jones3cc685f2014-12-29 21:31:44 -0700379}
380
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800381int rtc_set(const struct rtc_time *time)
382{
Marc Jones3cc685f2014-12-29 21:31:44 -0700383 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 Black03abaee212014-04-30 21:31:44 -0700389 /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */
390 cmos_write(bin2bcd(time->year / 100), RTC_CLK_ALTCENTURY);
Marc Jones3cc685f2014-12-29 21:31:44 -0700391 cmos_write(bin2bcd(time->wday + 1), RTC_CLK_DAYOFWEEK);
392 return 0;
393}
394
Gabe Black03abaee212014-04-30 21:31:44 -0700395int rtc_get(struct rtc_time *time)
Marc Jones3cc685f2014-12-29 21:31:44 -0700396{
Marshall Dawson5a043fe2016-11-05 18:31:33 -0600397 wait_uip();
Marc Jones3cc685f2014-12-29 21:31:44 -0700398 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 Black03abaee212014-04-30 21:31:44 -0700404 /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */
405 time->year += bcd2bin(cmos_read(RTC_CLK_ALTCENTURY)) * 100;
Marc Jones3cc685f2014-12-29 21:31:44 -0700406 time->wday = bcd2bin(cmos_read(RTC_CLK_DAYOFWEEK)) - 1;
407 return 0;
zbaoa1e6a9c2012-08-02 19:02:26 +0800408}
Kyösti Mälkki7ce1a752016-12-11 12:56:37 +0200409
410/*
411 * Signal coreboot proper completed -- just before running payload
412 * or jumping to ACPI S3 wakeup vector.
413 */
414void 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}