blob: b18f22f43d5158935b3ef7a62b88506bd5e12a37 [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{
59 int i;
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020060 u16 sum, old_sum;
Eric Biederman8ca8d762003-04-22 19:02:15 +000061 sum = 0;
Gabe Blackb3f08c62014-04-30 17:12:25 -070062 for (i = range_start; i <= range_end; i++)
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000063 sum += cmos_read(i);
Gabe Blackb3f08c62014-04-30 17:12:25 -070064 old_sum = ((cmos_read(cks_loc) << 8) | cmos_read(cks_loc + 1)) &
65 0x0ffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +000066 return sum == old_sum;
67}
68
Gabe Blackb3f08c62014-04-30 17:12:25 -070069static void cmos_set_checksum(int range_start, int range_end, int cks_loc)
Eric Biederman8ca8d762003-04-22 19:02:15 +000070{
71 int i;
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020072 u16 sum;
Eric Biederman8ca8d762003-04-22 19:02:15 +000073 sum = 0;
Gabe Blackb3f08c62014-04-30 17:12:25 -070074 for (i = range_start; i <= range_end; i++)
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000075 sum += cmos_read(i);
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000076 cmos_write(((sum >> 8) & 0x0ff), cks_loc);
Gabe Blackb3f08c62014-04-30 17:12:25 -070077 cmos_write(((sum >> 0) & 0x0ff), cks_loc + 1);
Eric Biederman8ca8d762003-04-22 19:02:15 +000078}
79
80#define RTC_CONTROL_DEFAULT (RTC_24H)
81#define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
Li-Ta Lo84a80282005-01-07 02:42:53 +000082
Duncan Laurie4b1610d2012-09-05 10:52:44 -070083#ifndef __SMM__
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060084void cmos_init(bool invalid)
Eric Biederman8ca8d762003-04-22 19:02:15 +000085{
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060086 bool cmos_invalid = false;
87 bool checksum_invalid = false;
88 bool clear_cmos;
89 size_t i;
90 uint8_t x;
Eric Biederman8ca8d762003-04-22 19:02:15 +000091
Martin Roth7312c542014-05-12 21:52:54 -060092#ifndef __PRE_RAM__
Stefan Reinauer86ce7f92013-05-28 12:37:08 -070093 /*
94 * Avoid clearing pending interrupts and resetting the RTC control
95 * register in the resume path because the Linux kernel relies on
96 * this to know if it should restart the RTC timer queue if the wake
97 * was due to the RTC alarm.
98 */
Kyösti Mälkki9d9eb1e2014-06-20 05:38:07 +030099 if (acpi_is_wakeup_s3())
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700100 return;
Martin Roth7312c542014-05-12 21:52:54 -0600101#endif /* __PRE_RAM__ */
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700102
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000103 printk(BIOS_DEBUG, "RTC Init\n");
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000104
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600105 if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) {
106 /* See if there has been a CMOS power problem. */
107 x = cmos_read(RTC_VALID);
108 cmos_invalid = !(x & RTC_VRT);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000109
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600110 /* See if there is a CMOS checksum error */
111 checksum_invalid = !cmos_checksum_valid(PC_CKS_RANGE_START,
112 PC_CKS_RANGE_END, PC_CKS_LOC);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000113
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600114 clear_cmos = false;
115 } else {
116 clear_cmos = true;
117 }
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700118
Eric Biederman8ca8d762003-04-22 19:02:15 +0000119 if (invalid || cmos_invalid || checksum_invalid) {
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600120 if (clear_cmos) {
121 cmos_write(0, 0x01);
122 cmos_write(0, 0x03);
123 cmos_write(0, 0x05);
124 for (i = 10; i < 128; i++)
125 cmos_write(0, i);
126 }
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700127
Gabe Blackb3f08c62014-04-30 17:12:25 -0700128 if (cmos_invalid)
Gabe Black03abaee212014-04-30 21:31:44 -0700129 cmos_reset_date();
Stefan Reinauerfeadfb72012-11-06 18:39:41 -0800130
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700131 printk(BIOS_WARNING, "RTC:%s%s%s%s\n",
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600132 invalid ? " Clear requested":"",
133 cmos_invalid ? " Power Problem":"",
134 checksum_invalid ? " Checksum invalid":"",
135 clear_cmos ? " zeroing cmos":"");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000136 }
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000137
138 /* Setup the real time clock */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000139 cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000140 /* Setup the frequency it operates at */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000141 cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT);
Vincent Palatinfc1b9ee2012-08-07 16:05:14 -0700142 /* Ensure all reserved bits are 0 in register D */
143 cmos_write(RTC_VRT, RTC_VALID);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000144
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600145 if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) {
146 /* See if there is a LB CMOS checksum error */
147 checksum_invalid = !cmos_checksum_valid(LB_CKS_RANGE_START,
148 LB_CKS_RANGE_END,LB_CKS_LOC);
149 if (checksum_invalid)
150 printk(BIOS_DEBUG, "RTC: coreboot checksum invalid\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000151
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600152 /* Make certain we have a valid checksum */
153 cmos_set_checksum(PC_CKS_RANGE_START, PC_CKS_RANGE_END, PC_CKS_LOC);
154 }
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000155
Eric Biederman8ca8d762003-04-22 19:02:15 +0000156 /* Clear any pending interrupts */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700157 cmos_read(RTC_INTR_FLAGS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000158}
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600159#endif /* __SMM__ */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000160
161
Gabe Blackb3f08c62014-04-30 17:12:25 -0700162/*
163 * This routine returns the value of the requested bits.
164 * input bit = bit count from the beginning of the cmos image
165 * length = number of bits to include in the value
166 * ret = a character pointer to where the value is to be returned
167 * returns CB_SUCCESS = successful, cb_err code if an error occurred
168 */
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600169static enum cb_err get_cmos_value(unsigned long bit, unsigned long length,
170 void *vret)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000171{
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000172 unsigned char *ret;
173 unsigned long byte,byte_bit;
174 unsigned long i;
175 unsigned char uchar;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176
Gabe Blackb3f08c62014-04-30 17:12:25 -0700177 /*
178 * The table is checked when it is built to ensure all
179 * values are valid.
180 */
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000181 ret = vret;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700182 byte = bit / 8; /* find the byte where the data starts */
183 byte_bit = bit % 8; /* find the bit in the byte where the data starts */
184 if (length < 9) { /* one byte or less */
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000185 uchar = cmos_read(byte); /* load the byte */
186 uchar >>= byte_bit; /* shift the bits to byte align */
187 /* clear unspecified bits */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700188 ret[0] = uchar & ((1 << length) - 1);
189 } else { /* more that one byte so transfer the whole bytes */
190 for (i = 0; length; i++, length -= 8, byte++) {
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000191 /* load the byte */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700192 ret[i] = cmos_read(byte);
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000193 }
194 }
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600195 return CB_SUCCESS;
Luc Verhaegen9ceae902009-06-03 10:47:19 +0000196}
197
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600198enum cb_err get_option(void *dest, const char *name)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000199{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000200 struct cmos_option_table *ct;
201 struct cmos_entries *ce;
202 size_t namelen;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700203 int found = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000204
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600205 if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE))
206 return CB_CMOS_OTABLE_DISABLED;
207
Eric Biederman8ca8d762003-04-22 19:02:15 +0000208 /* Figure out how long name is */
209 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000210
Eric Biederman8ca8d762003-04-22 19:02:15 +0000211 /* find the requested entry record */
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800212 ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin",
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100213 CBFS_COMPONENT_CMOS_LAYOUT, NULL);
Patrick Georgicef3b892011-01-18 14:28:45 +0000214 if (!ct) {
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700215 printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. "
216 "Options are disabled\n");
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600217 return CB_CMOS_LAYOUT_NOT_FOUND;
Patrick Georgicef3b892011-01-18 14:28:45 +0000218 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700219 ce = (struct cmos_entries*)((unsigned char *)ct + ct->header_length);
220 for(; ce->tag == LB_TAG_OPTION;
221 ce = (struct cmos_entries*)((unsigned char *)ce + ce->size)) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000222 if (memcmp(ce->name, name, namelen) == 0) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700223 found = 1;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000224 break;
225 }
226 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700227 if (!found) {
Stefan Reinauer8e726b72010-03-29 23:01:35 +0000228 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600229 return CB_CMOS_OPTION_NOT_FOUND;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000230 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000231
Gabe Blackb3f08c62014-04-30 17:12:25 -0700232 if (get_cmos_value(ce->bit, ce->length, dest) != CB_SUCCESS)
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600233 return CB_CMOS_ACCESS_ERROR;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700234 if (!cmos_checksum_valid(LB_CKS_RANGE_START, LB_CKS_RANGE_END, LB_CKS_LOC))
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600235 return CB_CMOS_CHECKSUM_INVALID;
236 return CB_SUCCESS;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000237}
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200238
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600239static enum cb_err set_cmos_value(unsigned long bit, unsigned long length,
240 void *vret)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200241{
242 unsigned char *ret;
243 unsigned long byte,byte_bit;
244 unsigned long i;
245 unsigned char uchar, mask;
246 unsigned int chksum_update_needed = 0;
247
248 ret = vret;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700249 byte = bit / 8; /* find the byte where the data starts */
250 byte_bit = bit % 8; /* find the bit where the data starts */
251 if (length <= 8) { /* one byte or less */
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200252 mask = (1 << length) - 1;
253 mask <<= byte_bit;
254
255 uchar = cmos_read(byte);
256 uchar &= ~mask;
257 uchar |= (ret[0] << byte_bit);
258 cmos_write(uchar, byte);
259 if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
260 chksum_update_needed = 1;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700261 } else { /* more that one byte so transfer the whole bytes */
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200262 if (byte_bit || length % 8)
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600263 return CB_ERR_ARG;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200264
Gabe Blackb3f08c62014-04-30 17:12:25 -0700265 for (i = 0; length; i++, length -= 8, byte++)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200266 cmos_write(ret[i], byte);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700267 if (byte >= LB_CKS_RANGE_START &&
268 byte <= LB_CKS_RANGE_END)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200269 chksum_update_needed = 1;
270 }
271
272 if (chksum_update_needed) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700273 cmos_set_checksum(LB_CKS_RANGE_START, LB_CKS_RANGE_END,
274 LB_CKS_LOC);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200275 }
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600276 return CB_SUCCESS;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200277}
278
279
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600280enum cb_err set_option(const char *name, void *value)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200281{
282 struct cmos_option_table *ct;
283 struct cmos_entries *ce;
284 unsigned long length;
285 size_t namelen;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700286 int found = 0;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200287
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600288 if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE))
289 return CB_CMOS_OTABLE_DISABLED;
290
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200291 /* Figure out how long name is */
292 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
293
294 /* find the requested entry record */
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800295 ct = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "cmos_layout.bin",
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100296 CBFS_COMPONENT_CMOS_LAYOUT, NULL);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200297 if (!ct) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700298 printk(BIOS_ERR, "cmos_layout.bin could not be found. "
299 "Options are disabled\n");
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600300 return CB_CMOS_LAYOUT_NOT_FOUND;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200301 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700302 ce = (struct cmos_entries*)((unsigned char *)ct + ct->header_length);
303 for(; ce->tag == LB_TAG_OPTION;
304 ce = (struct cmos_entries*)((unsigned char *)ce + ce->size)) {
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200305 if (memcmp(ce->name, name, namelen) == 0) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700306 found = 1;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200307 break;
308 }
309 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700310 if (!found) {
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200311 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600312 return CB_CMOS_OPTION_NOT_FOUND;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200313 }
314
315 length = ce->length;
316 if (ce->config == 's') {
317 length = MAX(strlen((const char *)value) * 8, ce->length - 8);
318 /* make sure the string is null terminated */
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600319 if (set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0})
320 != CB_SUCCESS)
321 return (CB_CMOS_ACCESS_ERROR);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200322 }
323
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600324 if (set_cmos_value(ce->bit, length, value) != CB_SUCCESS)
325 return (CB_CMOS_ACCESS_ERROR);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200326
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600327 return CB_SUCCESS;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200328}
zbaoa1e6a9c2012-08-02 19:02:26 +0800329
330/*
331 * If the CMOS is cleared, the rtc_reg has the invalid date. That
332 * hurts some OSes. Even if we don't set USE_OPTION_TABLE, we need
333 * to make sure the date is valid.
334 */
Gabe Black03abaee212014-04-30 21:31:44 -0700335void cmos_check_update_date()
zbaoa1e6a9c2012-08-02 19:02:26 +0800336{
337 u8 year, century;
338
Gabe Black03abaee212014-04-30 21:31:44 -0700339 /* Assume hardware always supports RTC_CLK_ALTCENTURY. */
340 century = cmos_read(RTC_CLK_ALTCENTURY);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700341 year = cmos_read(RTC_CLK_YEAR);
zbaoa1e6a9c2012-08-02 19:02:26 +0800342
Gabe Blackb3f08c62014-04-30 17:12:25 -0700343 /*
344 * TODO: If century is 0xFF, 100% that the cmos is cleared.
345 * Other than that, so far rtc_year is the only entry to check
346 * if the date is valid.
347 */
348 if (century > 0x99 || year > 0x99) /* Invalid date */
Gabe Black03abaee212014-04-30 21:31:44 -0700349 cmos_reset_date();
Marc Jones3cc685f2014-12-29 21:31:44 -0700350}
351
Gabe Black03abaee212014-04-30 21:31:44 -0700352int rtc_set(const struct rtc_time *time){
Marc Jones3cc685f2014-12-29 21:31:44 -0700353 cmos_write(bin2bcd(time->sec), RTC_CLK_SECOND);
354 cmos_write(bin2bcd(time->min), RTC_CLK_MINUTE);
355 cmos_write(bin2bcd(time->hour), RTC_CLK_HOUR);
356 cmos_write(bin2bcd(time->mday), RTC_CLK_DAYOFMONTH);
357 cmos_write(bin2bcd(time->mon), RTC_CLK_MONTH);
358 cmos_write(bin2bcd(time->year % 100), RTC_CLK_YEAR);
Gabe Black03abaee212014-04-30 21:31:44 -0700359 /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */
360 cmos_write(bin2bcd(time->year / 100), RTC_CLK_ALTCENTURY);
Marc Jones3cc685f2014-12-29 21:31:44 -0700361 cmos_write(bin2bcd(time->wday + 1), RTC_CLK_DAYOFWEEK);
362 return 0;
363}
364
Gabe Black03abaee212014-04-30 21:31:44 -0700365int rtc_get(struct rtc_time *time)
Marc Jones3cc685f2014-12-29 21:31:44 -0700366{
367 time->sec = bcd2bin(cmos_read(RTC_CLK_SECOND));
368 time->min = bcd2bin(cmos_read(RTC_CLK_MINUTE));
369 time->hour = bcd2bin(cmos_read(RTC_CLK_HOUR));
370 time->mday = bcd2bin(cmos_read(RTC_CLK_DAYOFMONTH));
371 time->mon = bcd2bin(cmos_read(RTC_CLK_MONTH));
372 time->year = bcd2bin(cmos_read(RTC_CLK_YEAR));
Gabe Black03abaee212014-04-30 21:31:44 -0700373 /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */
374 time->year += bcd2bin(cmos_read(RTC_CLK_ALTCENTURY)) * 100;
Marc Jones3cc685f2014-12-29 21:31:44 -0700375 time->wday = bcd2bin(cmos_read(RTC_CLK_DAYOFWEEK)) - 1;
376 return 0;
zbaoa1e6a9c2012-08-02 19:02:26 +0800377}