blob: 4f034a2db6a24de11ad905e946e7368d98f559e1 [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.
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 Reinauerea5c2b62011-10-27 18:42:53 +020022#include <stdint.h>
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +020023#include <version.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000024#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000025#include <pc80/mc146818rtc.h>
Stefan Reinauerca374d42008-01-18 16:16:45 +000026#include <boot/coreboot_tables.h>
Marc Jones3cc685f2014-12-29 21:31:44 -070027#include <rtc.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000028#include <string.h>
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060029#include <cbfs.h>
30
31/* There's no way around this include guard. option_table.h is autogenerated */
Stefan Reinauer10ec0fe2010-09-25 10:40:47 +000032#if CONFIG_USE_OPTION_TABLE
33#include "option_table.h"
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060034#else
35#define LB_CKS_RANGE_START 0
36#define LB_CKS_RANGE_END 0
37#define LB_CKS_LOC 0
Stefan Reinauer10ec0fe2010-09-25 10:40:47 +000038#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000039
Gabe Blackb3f08c62014-04-30 17:12:25 -070040
Gabe Black03abaee212014-04-30 21:31:44 -070041static void cmos_reset_date(void)
zbaoa1e6a9c2012-08-02 19:02:26 +080042{
Vincent Palatinfa90fd42012-08-07 17:03:40 -070043 /* Now setup a default date equals to the build date */
Marc Jones3cc685f2014-12-29 21:31:44 -070044 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 Black03abaee212014-04-30 21:31:44 -070054 rtc_set(&time);
zbaoa1e6a9c2012-08-02 19:02:26 +080055}
56
Gabe Blackb3f08c62014-04-30 17:12:25 -070057static int cmos_checksum_valid(int range_start, int range_end, int cks_loc)
Eric Biederman8ca8d762003-04-22 19:02:15 +000058{
Timothy Pearsonf20c6e82015-02-14 16:15:31 -060059 if (IS_ENABLED(CONFIG_STATIC_OPTION_TABLE))
60 return 1;
61
Eric Biederman8ca8d762003-04-22 19:02:15 +000062 int i;
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020063 u16 sum, old_sum;
Eric Biederman8ca8d762003-04-22 19:02:15 +000064 sum = 0;
Gabe Blackb3f08c62014-04-30 17:12:25 -070065 for (i = range_start; i <= range_end; i++)
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000066 sum += cmos_read(i);
Gabe Blackb3f08c62014-04-30 17:12:25 -070067 old_sum = ((cmos_read(cks_loc) << 8) | cmos_read(cks_loc + 1)) &
68 0x0ffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +000069 return sum == old_sum;
70}
71
Gabe Blackb3f08c62014-04-30 17:12:25 -070072static void cmos_set_checksum(int range_start, int range_end, int cks_loc)
Eric Biederman8ca8d762003-04-22 19:02:15 +000073{
74 int i;
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020075 u16 sum;
Eric Biederman8ca8d762003-04-22 19:02:15 +000076 sum = 0;
Gabe Blackb3f08c62014-04-30 17:12:25 -070077 for (i = range_start; i <= range_end; i++)
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000078 sum += cmos_read(i);
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000079 cmos_write(((sum >> 8) & 0x0ff), cks_loc);
Gabe Blackb3f08c62014-04-30 17:12:25 -070080 cmos_write(((sum >> 0) & 0x0ff), cks_loc + 1);
Eric Biederman8ca8d762003-04-22 19:02:15 +000081}
82
83#define RTC_CONTROL_DEFAULT (RTC_24H)
84#define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
Li-Ta Lo84a80282005-01-07 02:42:53 +000085
Duncan Laurie4b1610d2012-09-05 10:52:44 -070086#ifndef __SMM__
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060087void cmos_init(bool invalid)
Eric Biederman8ca8d762003-04-22 19:02:15 +000088{
Werner Zeha8b03da2015-02-09 08:17:40 +010089 bool cmos_invalid = invalid;
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060090 bool checksum_invalid = false;
91 bool clear_cmos;
92 size_t i;
93 uint8_t x;
Eric Biederman8ca8d762003-04-22 19:02:15 +000094
Martin Roth7312c542014-05-12 21:52:54 -060095#ifndef __PRE_RAM__
Stefan Reinauer86ce7f92013-05-28 12:37:08 -070096 /*
97 * Avoid clearing pending interrupts and resetting the RTC control
98 * register in the resume path because the Linux kernel relies on
99 * this to know if it should restart the RTC timer queue if the wake
100 * was due to the RTC alarm.
101 */
Kyösti Mälkki9d9eb1e2014-06-20 05:38:07 +0300102 if (acpi_is_wakeup_s3())
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700103 return;
Martin Roth7312c542014-05-12 21:52:54 -0600104#endif /* __PRE_RAM__ */
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700105
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000106 printk(BIOS_DEBUG, "RTC Init\n");
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000107
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600108 if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) {
109 /* See if there has been a CMOS power problem. */
110 x = cmos_read(RTC_VALID);
111 cmos_invalid = !(x & RTC_VRT);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000112
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600113 /* See if there is a CMOS checksum error */
114 checksum_invalid = !cmos_checksum_valid(PC_CKS_RANGE_START,
115 PC_CKS_RANGE_END, PC_CKS_LOC);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000116
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600117 clear_cmos = false;
118 } else {
119 clear_cmos = true;
120 }
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700121
Eric Biederman8ca8d762003-04-22 19:02:15 +0000122 if (invalid || cmos_invalid || checksum_invalid) {
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600123 if (clear_cmos) {
124 cmos_write(0, 0x01);
125 cmos_write(0, 0x03);
126 cmos_write(0, 0x05);
127 for (i = 10; i < 128; i++)
128 cmos_write(0, i);
129 }
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700130
Gabe Blackb3f08c62014-04-30 17:12:25 -0700131 if (cmos_invalid)
Gabe Black03abaee212014-04-30 21:31:44 -0700132 cmos_reset_date();
Stefan Reinauerfeadfb72012-11-06 18:39:41 -0800133
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700134 printk(BIOS_WARNING, "RTC:%s%s%s%s\n",
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600135 invalid ? " Clear requested":"",
136 cmos_invalid ? " Power Problem":"",
137 checksum_invalid ? " Checksum invalid":"",
138 clear_cmos ? " zeroing cmos":"");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000139 }
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000140
141 /* Setup the real time clock */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000142 cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000143 /* Setup the frequency it operates at */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000144 cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT);
Vincent Palatinfc1b9ee2012-08-07 16:05:14 -0700145 /* Ensure all reserved bits are 0 in register D */
146 cmos_write(RTC_VRT, RTC_VALID);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000147
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600148 if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) {
149 /* See if there is a LB CMOS checksum error */
150 checksum_invalid = !cmos_checksum_valid(LB_CKS_RANGE_START,
151 LB_CKS_RANGE_END,LB_CKS_LOC);
152 if (checksum_invalid)
153 printk(BIOS_DEBUG, "RTC: coreboot checksum invalid\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000154
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600155 /* Make certain we have a valid checksum */
156 cmos_set_checksum(PC_CKS_RANGE_START, PC_CKS_RANGE_END, PC_CKS_LOC);
157 }
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000158
Eric Biederman8ca8d762003-04-22 19:02:15 +0000159 /* Clear any pending interrupts */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700160 cmos_read(RTC_INTR_FLAGS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000161}
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600162#endif /* __SMM__ */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000163
164
Gabe Blackb3f08c62014-04-30 17:12:25 -0700165/*
166 * This routine returns the value of the requested bits.
167 * input bit = bit count from the beginning of the cmos image
168 * length = number of bits to include in the value
169 * ret = a character pointer to where the value is to be returned
170 * returns CB_SUCCESS = successful, cb_err code if an error occurred
171 */
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600172static enum cb_err get_cmos_value(unsigned long bit, unsigned long length,
173 void *vret)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174{
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000175 unsigned char *ret;
176 unsigned long byte,byte_bit;
177 unsigned long i;
178 unsigned char uchar;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179
Gabe Blackb3f08c62014-04-30 17:12:25 -0700180 /*
181 * The table is checked when it is built to ensure all
182 * values are valid.
183 */
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000184 ret = vret;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700185 byte = bit / 8; /* find the byte where the data starts */
186 byte_bit = bit % 8; /* find the bit in the byte where the data starts */
187 if (length < 9) { /* one byte or less */
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000188 uchar = cmos_read(byte); /* load the byte */
189 uchar >>= byte_bit; /* shift the bits to byte align */
190 /* clear unspecified bits */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700191 ret[0] = uchar & ((1 << length) - 1);
192 } else { /* more that one byte so transfer the whole bytes */
193 for (i = 0; length; i++, length -= 8, byte++) {
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000194 /* load the byte */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700195 ret[i] = cmos_read(byte);
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000196 }
197 }
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600198 return CB_SUCCESS;
Luc Verhaegen9ceae902009-06-03 10:47:19 +0000199}
200
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600201enum cb_err get_option(void *dest, const char *name)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000202{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000203 struct cmos_option_table *ct;
204 struct cmos_entries *ce;
205 size_t namelen;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700206 int found = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000207
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600208 if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE))
209 return CB_CMOS_OTABLE_DISABLED;
210
Eric Biederman8ca8d762003-04-22 19:02:15 +0000211 /* Figure out how long name is */
212 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000213
Eric Biederman8ca8d762003-04-22 19:02:15 +0000214 /* find the requested entry record */
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800215 ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin",
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100216 CBFS_COMPONENT_CMOS_LAYOUT, NULL);
Patrick Georgicef3b892011-01-18 14:28:45 +0000217 if (!ct) {
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700218 printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. "
219 "Options are disabled\n");
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600220 return CB_CMOS_LAYOUT_NOT_FOUND;
Patrick Georgicef3b892011-01-18 14:28:45 +0000221 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700222 ce = (struct cmos_entries*)((unsigned char *)ct + ct->header_length);
223 for(; ce->tag == LB_TAG_OPTION;
224 ce = (struct cmos_entries*)((unsigned char *)ce + ce->size)) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000225 if (memcmp(ce->name, name, namelen) == 0) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700226 found = 1;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000227 break;
228 }
229 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700230 if (!found) {
Stefan Reinauer8e726b72010-03-29 23:01:35 +0000231 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600232 return CB_CMOS_OPTION_NOT_FOUND;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000233 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000234
Gabe Blackb3f08c62014-04-30 17:12:25 -0700235 if (get_cmos_value(ce->bit, ce->length, dest) != CB_SUCCESS)
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600236 return CB_CMOS_ACCESS_ERROR;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700237 if (!cmos_checksum_valid(LB_CKS_RANGE_START, LB_CKS_RANGE_END, LB_CKS_LOC))
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600238 return CB_CMOS_CHECKSUM_INVALID;
239 return CB_SUCCESS;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000240}
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200241
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600242static enum cb_err set_cmos_value(unsigned long bit, unsigned long length,
243 void *vret)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200244{
245 unsigned char *ret;
246 unsigned long byte,byte_bit;
247 unsigned long i;
248 unsigned char uchar, mask;
249 unsigned int chksum_update_needed = 0;
250
251 ret = vret;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700252 byte = bit / 8; /* find the byte where the data starts */
253 byte_bit = bit % 8; /* find the bit where the data starts */
254 if (length <= 8) { /* one byte or less */
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200255 mask = (1 << length) - 1;
256 mask <<= byte_bit;
257
258 uchar = cmos_read(byte);
259 uchar &= ~mask;
260 uchar |= (ret[0] << byte_bit);
261 cmos_write(uchar, byte);
262 if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
263 chksum_update_needed = 1;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700264 } else { /* more that one byte so transfer the whole bytes */
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200265 if (byte_bit || length % 8)
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600266 return CB_ERR_ARG;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200267
Gabe Blackb3f08c62014-04-30 17:12:25 -0700268 for (i = 0; length; i++, length -= 8, byte++)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200269 cmos_write(ret[i], byte);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700270 if (byte >= LB_CKS_RANGE_START &&
271 byte <= LB_CKS_RANGE_END)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200272 chksum_update_needed = 1;
273 }
274
275 if (chksum_update_needed) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700276 cmos_set_checksum(LB_CKS_RANGE_START, LB_CKS_RANGE_END,
277 LB_CKS_LOC);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200278 }
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600279 return CB_SUCCESS;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200280}
281
282
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600283enum cb_err set_option(const char *name, void *value)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200284{
285 struct cmos_option_table *ct;
286 struct cmos_entries *ce;
287 unsigned long length;
288 size_t namelen;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700289 int found = 0;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200290
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600291 if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE))
292 return CB_CMOS_OTABLE_DISABLED;
293
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200294 /* Figure out how long name is */
295 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
296
297 /* find the requested entry record */
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800298 ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin",
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100299 CBFS_COMPONENT_CMOS_LAYOUT, NULL);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200300 if (!ct) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700301 printk(BIOS_ERR, "cmos_layout.bin could not be found. "
302 "Options are disabled\n");
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600303 return CB_CMOS_LAYOUT_NOT_FOUND;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200304 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700305 ce = (struct cmos_entries*)((unsigned char *)ct + ct->header_length);
306 for(; ce->tag == LB_TAG_OPTION;
307 ce = (struct cmos_entries*)((unsigned char *)ce + ce->size)) {
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200308 if (memcmp(ce->name, name, namelen) == 0) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700309 found = 1;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200310 break;
311 }
312 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700313 if (!found) {
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200314 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600315 return CB_CMOS_OPTION_NOT_FOUND;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200316 }
317
318 length = ce->length;
319 if (ce->config == 's') {
320 length = MAX(strlen((const char *)value) * 8, ce->length - 8);
321 /* make sure the string is null terminated */
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600322 if (set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0})
323 != CB_SUCCESS)
324 return (CB_CMOS_ACCESS_ERROR);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200325 }
326
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600327 if (set_cmos_value(ce->bit, length, value) != CB_SUCCESS)
328 return (CB_CMOS_ACCESS_ERROR);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200329
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600330 return CB_SUCCESS;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200331}
zbaoa1e6a9c2012-08-02 19:02:26 +0800332
333/*
334 * If the CMOS is cleared, the rtc_reg has the invalid date. That
335 * hurts some OSes. Even if we don't set USE_OPTION_TABLE, we need
336 * to make sure the date is valid.
337 */
Gabe Black03abaee212014-04-30 21:31:44 -0700338void cmos_check_update_date()
zbaoa1e6a9c2012-08-02 19:02:26 +0800339{
340 u8 year, century;
341
Gabe Black03abaee212014-04-30 21:31:44 -0700342 /* Assume hardware always supports RTC_CLK_ALTCENTURY. */
343 century = cmos_read(RTC_CLK_ALTCENTURY);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700344 year = cmos_read(RTC_CLK_YEAR);
zbaoa1e6a9c2012-08-02 19:02:26 +0800345
Gabe Blackb3f08c62014-04-30 17:12:25 -0700346 /*
347 * TODO: If century is 0xFF, 100% that the cmos is cleared.
348 * Other than that, so far rtc_year is the only entry to check
349 * if the date is valid.
350 */
351 if (century > 0x99 || year > 0x99) /* Invalid date */
Gabe Black03abaee212014-04-30 21:31:44 -0700352 cmos_reset_date();
Marc Jones3cc685f2014-12-29 21:31:44 -0700353}
354
Gabe Black03abaee212014-04-30 21:31:44 -0700355int rtc_set(const struct rtc_time *time){
Marc Jones3cc685f2014-12-29 21:31:44 -0700356 cmos_write(bin2bcd(time->sec), RTC_CLK_SECOND);
357 cmos_write(bin2bcd(time->min), RTC_CLK_MINUTE);
358 cmos_write(bin2bcd(time->hour), RTC_CLK_HOUR);
359 cmos_write(bin2bcd(time->mday), RTC_CLK_DAYOFMONTH);
360 cmos_write(bin2bcd(time->mon), RTC_CLK_MONTH);
361 cmos_write(bin2bcd(time->year % 100), RTC_CLK_YEAR);
Gabe Black03abaee212014-04-30 21:31:44 -0700362 /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */
363 cmos_write(bin2bcd(time->year / 100), RTC_CLK_ALTCENTURY);
Marc Jones3cc685f2014-12-29 21:31:44 -0700364 cmos_write(bin2bcd(time->wday + 1), RTC_CLK_DAYOFWEEK);
365 return 0;
366}
367
Gabe Black03abaee212014-04-30 21:31:44 -0700368int rtc_get(struct rtc_time *time)
Marc Jones3cc685f2014-12-29 21:31:44 -0700369{
370 time->sec = bcd2bin(cmos_read(RTC_CLK_SECOND));
371 time->min = bcd2bin(cmos_read(RTC_CLK_MINUTE));
372 time->hour = bcd2bin(cmos_read(RTC_CLK_HOUR));
373 time->mday = bcd2bin(cmos_read(RTC_CLK_DAYOFMONTH));
374 time->mon = bcd2bin(cmos_read(RTC_CLK_MONTH));
375 time->year = bcd2bin(cmos_read(RTC_CLK_YEAR));
Gabe Black03abaee212014-04-30 21:31:44 -0700376 /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */
377 time->year += bcd2bin(cmos_read(RTC_CLK_ALTCENTURY)) * 100;
Marc Jones3cc685f2014-12-29 21:31:44 -0700378 time->wday = bcd2bin(cmos_read(RTC_CLK_DAYOFWEEK)) - 1;
379 return 0;
zbaoa1e6a9c2012-08-02 19:02:26 +0800380}