blob: 99d670de421d8c950f81baa4d501aac93c1b6100 [file] [log] [blame]
Stefan Reinauerea5c2b62011-10-27 18:42:53 +02001#include <stdint.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00002#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00003#include <pc80/mc146818rtc.h>
Stefan Reinauerca374d42008-01-18 16:16:45 +00004#include <boot/coreboot_tables.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00005#include <string.h>
Stefan Reinauer10ec0fe2010-09-25 10:40:47 +00006#if CONFIG_USE_OPTION_TABLE
7#include "option_table.h"
Patrick Georgi24479372011-01-18 13:56:36 +00008#include <cbfs.h>
Stefan Reinauer10ec0fe2010-09-25 10:40:47 +00009#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000010
Eric Biederman8ca8d762003-04-22 19:02:15 +000011/* control registers - Moto names
12 */
13#define RTC_REG_A 10
14#define RTC_REG_B 11
15#define RTC_REG_C 12
16#define RTC_REG_D 13
17
18
19/**********************************************************************
20 * register details
21 **********************************************************************/
22#define RTC_FREQ_SELECT RTC_REG_A
23
24/* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus,
25 * reset after update (may take 1.984ms @ 32768Hz RefClock) is complete,
26 * totalling to a max high interval of 2.228 ms.
27 */
28# define RTC_UIP 0x80
29# define RTC_DIV_CTL 0x70
30 /* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */
31# define RTC_REF_CLCK_4MHZ 0x00
32# define RTC_REF_CLCK_1MHZ 0x10
33# define RTC_REF_CLCK_32KHZ 0x20
34 /* 2 values for divider stage reset, others for "testing purposes only" */
35# define RTC_DIV_RESET1 0x60
36# define RTC_DIV_RESET2 0x70
37 /* Periodic intr. / Square wave rate select. 0=none, 1=32.8kHz,... 15=2Hz */
38# define RTC_RATE_SELECT 0x0F
39# define RTC_RATE_NONE 0x00
40# define RTC_RATE_32786HZ 0x01
41# define RTC_RATE_16384HZ 0x02
42# define RTC_RATE_8192HZ 0x03
43# define RTC_RATE_4096HZ 0x04
44# define RTC_RATE_2048HZ 0x05
45# define RTC_RATE_1024HZ 0x06
46# define RTC_RATE_512HZ 0x07
47# define RTC_RATE_256HZ 0x08
48# define RTC_RATE_128HZ 0x09
49# define RTC_RATE_64HZ 0x0a
50# define RTC_RATE_32HZ 0x0b
51# define RTC_RATE_16HZ 0x0c
52# define RTC_RATE_8HZ 0x0d
53# define RTC_RATE_4HZ 0x0e
54# define RTC_RATE_2HZ 0x0f
55
56/**********************************************************************/
57#define RTC_CONTROL RTC_REG_B
58# define RTC_SET 0x80 /* disable updates for clock setting */
59# define RTC_PIE 0x40 /* periodic interrupt enable */
60# define RTC_AIE 0x20 /* alarm interrupt enable */
61# define RTC_UIE 0x10 /* update-finished interrupt enable */
62# define RTC_SQWE 0x08 /* enable square-wave output */
63# define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
64# define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
65# define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
66
67/**********************************************************************/
68#define RTC_INTR_FLAGS RTC_REG_C
69/* caution - cleared by read */
70# define RTC_IRQF 0x80 /* any of the following 3 is active */
71# define RTC_PF 0x40
72# define RTC_AF 0x20
73# define RTC_UF 0x10
74
75/**********************************************************************/
76#define RTC_VALID RTC_REG_D
77# define RTC_VRT 0x80 /* valid RAM and time */
78/**********************************************************************/
79
Edwin Beasanteb50c7d2010-07-06 21:05:04 +000080#if CONFIG_USE_OPTION_TABLE
Eric Biederman8ca8d762003-04-22 19:02:15 +000081static int rtc_checksum_valid(int range_start, int range_end, int cks_loc)
82{
83 int i;
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020084 u16 sum, old_sum;
Eric Biederman8ca8d762003-04-22 19:02:15 +000085 sum = 0;
86 for(i = range_start; i <= range_end; i++) {
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000087 sum += cmos_read(i);
Eric Biederman8ca8d762003-04-22 19:02:15 +000088 }
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000089 old_sum = ((cmos_read(cks_loc)<<8) | cmos_read(cks_loc+1))&0x0ffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +000090 return sum == old_sum;
91}
92
93static void rtc_set_checksum(int range_start, int range_end, int cks_loc)
94{
95 int i;
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020096 u16 sum;
Eric Biederman8ca8d762003-04-22 19:02:15 +000097 sum = 0;
98 for(i = range_start; i <= range_end; i++) {
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000099 sum += cmos_read(i);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000100 }
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000101 cmos_write(((sum >> 8) & 0x0ff), cks_loc);
102 cmos_write(((sum >> 0) & 0x0ff), cks_loc+1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000103}
Stefan Reinauer2549d522010-03-17 03:44:45 +0000104#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000105
Stefan Reinauer2fbbea02010-01-16 13:27:39 +0000106#if CONFIG_ARCH_X86
Eric Biederman8ca8d762003-04-22 19:02:15 +0000107#define RTC_CONTROL_DEFAULT (RTC_24H)
108#define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
Stefan Reinauer2fbbea02010-01-16 13:27:39 +0000109#else
110#if CONFIG_ARCH_ALPHA
Eric Biederman8ca8d762003-04-22 19:02:15 +0000111#define RTC_CONTROL_DEFAULT (RTC_SQWE | RTC_24H)
112#define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
113#endif
Stefan Reinauer2fbbea02010-01-16 13:27:39 +0000114#endif
Li-Ta Lo84a80282005-01-07 02:42:53 +0000115
Eric Biederman8ca8d762003-04-22 19:02:15 +0000116void rtc_init(int invalid)
117{
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000118#if CONFIG_USE_OPTION_TABLE
Eric Biederman8ca8d762003-04-22 19:02:15 +0000119 unsigned char x;
120 int cmos_invalid, checksum_invalid;
Maciej Pijankaea921852009-10-27 14:29:29 +0000121#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000122
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000123 printk(BIOS_DEBUG, "RTC Init\n");
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000124
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000125#if CONFIG_USE_OPTION_TABLE
Eric Biederman8ca8d762003-04-22 19:02:15 +0000126 /* See if there has been a CMOS power problem. */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000127 x = cmos_read(RTC_VALID);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000128 cmos_invalid = !(x & RTC_VRT);
129
130 /* See if there is a CMOS checksum error */
131 checksum_invalid = !rtc_checksum_valid(PC_CKS_RANGE_START,
132 PC_CKS_RANGE_END,PC_CKS_LOC);
133
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700134#define CLEAR_CMOS 0
Eric Biederman8ca8d762003-04-22 19:02:15 +0000135 if (invalid || cmos_invalid || checksum_invalid) {
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700136 printk(BIOS_WARNING, "RTC:%s%s%s%s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +0000137 invalid?" Clear requested":"",
Eric Biederman8ca8d762003-04-22 19:02:15 +0000138 cmos_invalid?" Power Problem":"",
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700139 checksum_invalid?" Checksum invalid":"",
140 CLEAR_CMOS?" zeroing cmos":"");
141#if CLEAR_CMOS
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000142 cmos_write(0, 0x01);
143 cmos_write(0, 0x03);
144 cmos_write(0, 0x05);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000145 for(i = 10; i < 48; i++) {
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000146 cmos_write(0, i);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000147 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000148
Eric Biederman8ca8d762003-04-22 19:02:15 +0000149 if (cmos_invalid) {
150 /* Now setup a default date of Sat 1 January 2000 */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000151 cmos_write(0, 0x00); /* seconds */
152 cmos_write(0, 0x02); /* minutes */
153 cmos_write(1, 0x04); /* hours */
154 cmos_write(7, 0x06); /* day of week */
155 cmos_write(1, 0x07); /* day of month */
156 cmos_write(1, 0x08); /* month */
157 cmos_write(0, 0x09); /* year */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000158 }
159#endif
160 }
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000161#endif
162
163 /* Setup the real time clock */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000164 cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000165 /* Setup the frequency it operates at */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000166 cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000167
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000168#if CONFIG_USE_OPTION_TABLE
Eric Biederman8ca8d762003-04-22 19:02:15 +0000169 /* See if there is a LB CMOS checksum error */
Stefan Reinauerb5828d72010-03-29 17:14:28 +0000170 checksum_invalid = !rtc_checksum_valid(LB_CKS_RANGE_START,
171 LB_CKS_RANGE_END,LB_CKS_LOC);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172 if(checksum_invalid)
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700173 printk(BIOS_DEBUG, "RTC: coreboot checksum invalid\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174
Eric Biederman8ca8d762003-04-22 19:02:15 +0000175 /* Make certain we have a valid checksum */
176 rtc_set_checksum(PC_CKS_RANGE_START,
177 PC_CKS_RANGE_END,PC_CKS_LOC);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000178#endif
179
Eric Biederman8ca8d762003-04-22 19:02:15 +0000180 /* Clear any pending interrupts */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000181 (void) cmos_read(RTC_INTR_FLAGS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000182}
183
184
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000185#if CONFIG_USE_OPTION_TABLE
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000186/* This routine returns the value of the requested bits
187 input bit = bit count from the beginning of the cmos image
188 length = number of bits to include in the value
189 ret = a character pointer to where the value is to be returned
190 output the value placed in ret
191 returns 0 = successful, -1 = an error occurred
192*/
193static int get_cmos_value(unsigned long bit, unsigned long length, void *vret)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000194{
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000195 unsigned char *ret;
196 unsigned long byte,byte_bit;
197 unsigned long i;
198 unsigned char uchar;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000199
Stefan Reinauer14e22772010-04-27 06:56:47 +0000200 /* The table is checked when it is built to ensure all
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000201 values are valid. */
202 ret = vret;
203 byte=bit/8; /* find the byte where the data starts */
204 byte_bit=bit%8; /* find the bit in the byte where the data starts */
205 if(length<9) { /* one byte or less */
206 uchar = cmos_read(byte); /* load the byte */
207 uchar >>= byte_bit; /* shift the bits to byte align */
208 /* clear unspecified bits */
209 ret[0] = uchar & ((1 << length) -1);
Luc Verhaegen9ceae902009-06-03 10:47:19 +0000210 }
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000211 else { /* more that one byte so transfer the whole bytes */
212 for(i=0;length;i++,length-=8,byte++) {
213 /* load the byte */
214 ret[i]=cmos_read(byte);
215 }
216 }
217 return 0;
Luc Verhaegen9ceae902009-06-03 10:47:19 +0000218}
219
Myles Watson3fe6b702009-10-09 20:13:43 +0000220int get_option(void *dest, const char *name)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000221{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000222 struct cmos_option_table *ct;
223 struct cmos_entries *ce;
224 size_t namelen;
225 int found=0;
226
227 /* 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 */
Patrick Georgi5ccbbd92011-01-21 11:43:06 +0000231 ct=cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT);
Patrick Georgicef3b892011-01-18 14:28:45 +0000232 if (!ct) {
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700233 printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. "
234 "Options are disabled\n");
Patrick Georgicef3b892011-01-18 14:28:45 +0000235 return(-2);
236 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000237 ce=(struct cmos_entries*)((unsigned char *)ct + ct->header_length);
238 for(;ce->tag==LB_TAG_OPTION;
239 ce=(struct cmos_entries*)((unsigned char *)ce + ce->size)) {
240 if (memcmp(ce->name, name, namelen) == 0) {
241 found=1;
242 break;
243 }
244 }
245 if(!found) {
Stefan Reinauer8e726b72010-03-29 23:01:35 +0000246 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000247 return(-2);
248 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000249
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000250 if(get_cmos_value(ce->bit, ce->length, dest))
251 return(-3);
Stefan Reinauerb5828d72010-03-29 17:14:28 +0000252 if(!rtc_checksum_valid(LB_CKS_RANGE_START,
253 LB_CKS_RANGE_END,LB_CKS_LOC))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000254 return(-4);
255 return(0);
256}
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200257
258static int set_cmos_value(unsigned long bit, unsigned long length, void *vret)
259{
260 unsigned char *ret;
261 unsigned long byte,byte_bit;
262 unsigned long i;
263 unsigned char uchar, mask;
264 unsigned int chksum_update_needed = 0;
265
266 ret = vret;
267 byte = bit / 8; /* find the byte where the data starts */
268 byte_bit = bit % 8; /* find the bit in the byte where the data starts */
269 if(length <= 8) { /* one byte or less */
270 mask = (1 << length) - 1;
271 mask <<= byte_bit;
272
273 uchar = cmos_read(byte);
274 uchar &= ~mask;
275 uchar |= (ret[0] << byte_bit);
276 cmos_write(uchar, byte);
277 if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
278 chksum_update_needed = 1;
279 } else { /* more that one byte so transfer the whole bytes */
280 if (byte_bit || length % 8)
281 return -1;
282
283 for(i=0; length; i++, length-=8, byte++)
284 cmos_write(ret[i], byte);
285 if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
286 chksum_update_needed = 1;
287 }
288
289 if (chksum_update_needed) {
290 rtc_set_checksum(LB_CKS_RANGE_START,
291 LB_CKS_RANGE_END,LB_CKS_LOC);
292 }
293 return 0;
294}
295
296
297int set_option(const char *name, void *value)
298{
299 struct cmos_option_table *ct;
300 struct cmos_entries *ce;
301 unsigned long length;
302 size_t namelen;
303 int found=0;
304
305 /* Figure out how long name is */
306 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
307
308 /* find the requested entry record */
309 ct=cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT);
310 if (!ct) {
311 printk(BIOS_ERR, "cmos_layout.bin could not be found. Options are disabled\n");
312 return(-2);
313 }
314 ce=(struct cmos_entries*)((unsigned char *)ct + ct->header_length);
315 for(;ce->tag==LB_TAG_OPTION;
316 ce=(struct cmos_entries*)((unsigned char *)ce + ce->size)) {
317 if (memcmp(ce->name, name, namelen) == 0) {
318 found=1;
319 break;
320 }
321 }
322 if(!found) {
323 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
324 return(-2);
325 }
326
327 length = ce->length;
328 if (ce->config == 's') {
329 length = MAX(strlen((const char *)value) * 8, ce->length - 8);
330 /* make sure the string is null terminated */
331 if ((set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0})))
332 return (-3);
333 }
334
335 if ((set_cmos_value(ce->bit, length, value)))
336 return (-3);
337
338 return 0;
339}
Stefan Reinauer08670622009-06-30 15:17:49 +0000340#endif /* CONFIG_USE_OPTION_TABLE */