blob: 078bde273c793fc0f233d795dac1116e1265e269 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <console/console.h>
2#include <arch/io.h>
3#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 Reinauer8e726b72010-03-29 23:01:35 +00006#if CONFIG_HAVE_OPTION_TABLE
7#include <option_table.h>
8#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00009
Eric Biederman8ca8d762003-04-22 19:02:15 +000010/* control registers - Moto names
11 */
12#define RTC_REG_A 10
13#define RTC_REG_B 11
14#define RTC_REG_C 12
15#define RTC_REG_D 13
16
17
18/**********************************************************************
19 * register details
20 **********************************************************************/
21#define RTC_FREQ_SELECT RTC_REG_A
22
23/* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus,
24 * reset after update (may take 1.984ms @ 32768Hz RefClock) is complete,
25 * totalling to a max high interval of 2.228 ms.
26 */
27# define RTC_UIP 0x80
28# define RTC_DIV_CTL 0x70
29 /* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */
30# define RTC_REF_CLCK_4MHZ 0x00
31# define RTC_REF_CLCK_1MHZ 0x10
32# define RTC_REF_CLCK_32KHZ 0x20
33 /* 2 values for divider stage reset, others for "testing purposes only" */
34# define RTC_DIV_RESET1 0x60
35# define RTC_DIV_RESET2 0x70
36 /* Periodic intr. / Square wave rate select. 0=none, 1=32.8kHz,... 15=2Hz */
37# define RTC_RATE_SELECT 0x0F
38# define RTC_RATE_NONE 0x00
39# define RTC_RATE_32786HZ 0x01
40# define RTC_RATE_16384HZ 0x02
41# define RTC_RATE_8192HZ 0x03
42# define RTC_RATE_4096HZ 0x04
43# define RTC_RATE_2048HZ 0x05
44# define RTC_RATE_1024HZ 0x06
45# define RTC_RATE_512HZ 0x07
46# define RTC_RATE_256HZ 0x08
47# define RTC_RATE_128HZ 0x09
48# define RTC_RATE_64HZ 0x0a
49# define RTC_RATE_32HZ 0x0b
50# define RTC_RATE_16HZ 0x0c
51# define RTC_RATE_8HZ 0x0d
52# define RTC_RATE_4HZ 0x0e
53# define RTC_RATE_2HZ 0x0f
54
55/**********************************************************************/
56#define RTC_CONTROL RTC_REG_B
57# define RTC_SET 0x80 /* disable updates for clock setting */
58# define RTC_PIE 0x40 /* periodic interrupt enable */
59# define RTC_AIE 0x20 /* alarm interrupt enable */
60# define RTC_UIE 0x10 /* update-finished interrupt enable */
61# define RTC_SQWE 0x08 /* enable square-wave output */
62# define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
63# define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
64# define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
65
66/**********************************************************************/
67#define RTC_INTR_FLAGS RTC_REG_C
68/* caution - cleared by read */
69# define RTC_IRQF 0x80 /* any of the following 3 is active */
70# define RTC_PF 0x40
71# define RTC_AF 0x20
72# define RTC_UF 0x10
73
74/**********************************************************************/
75#define RTC_VALID RTC_REG_D
76# define RTC_VRT 0x80 /* valid RAM and time */
77/**********************************************************************/
78
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000079static inline unsigned char cmos_read(unsigned char addr)
80{
81 int offs = 0;
82 if (addr >= 128) {
83 offs = 2;
84 addr -= 128;
85 }
86 outb(addr, RTC_BASE_PORT + offs + 0);
87 return inb(RTC_BASE_PORT + offs + 1);
88}
Eric Biederman8ca8d762003-04-22 19:02:15 +000089
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000090static inline void cmos_write(unsigned char val, unsigned char addr)
91{
92 int offs = 0;
93 if (addr >= 128) {
94 offs = 2;
95 addr -= 128;
96 }
97 outb(addr, RTC_BASE_PORT + offs + 0);
98 outb(val, RTC_BASE_PORT + offs + 1);
99}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000100
Stefan Reinauer2549d522010-03-17 03:44:45 +0000101#if CONFIG_HAVE_OPTION_TABLE
Eric Biederman8ca8d762003-04-22 19:02:15 +0000102static int rtc_checksum_valid(int range_start, int range_end, int cks_loc)
103{
104 int i;
105 unsigned sum, old_sum;
106 sum = 0;
107 for(i = range_start; i <= range_end; i++) {
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000108 sum += cmos_read(i);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000109 }
110 sum = (~sum)&0x0ffff;
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000111 old_sum = ((cmos_read(cks_loc)<<8) | cmos_read(cks_loc+1))&0x0ffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000112 return sum == old_sum;
113}
114
115static void rtc_set_checksum(int range_start, int range_end, int cks_loc)
116{
117 int i;
118 unsigned sum;
119 sum = 0;
120 for(i = range_start; i <= range_end; i++) {
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000121 sum += cmos_read(i);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000122 }
123 sum = ~(sum & 0x0ffff);
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000124 cmos_write(((sum >> 8) & 0x0ff), cks_loc);
125 cmos_write(((sum >> 0) & 0x0ff), cks_loc+1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000126}
Stefan Reinauer2549d522010-03-17 03:44:45 +0000127#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000128
Stefan Reinauer2fbbea02010-01-16 13:27:39 +0000129#if CONFIG_ARCH_X86
Eric Biederman8ca8d762003-04-22 19:02:15 +0000130#define RTC_CONTROL_DEFAULT (RTC_24H)
131#define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
Stefan Reinauer2fbbea02010-01-16 13:27:39 +0000132#else
133#if CONFIG_ARCH_ALPHA
Eric Biederman8ca8d762003-04-22 19:02:15 +0000134#define RTC_CONTROL_DEFAULT (RTC_SQWE | RTC_24H)
135#define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
136#endif
Stefan Reinauer2fbbea02010-01-16 13:27:39 +0000137#endif
Li-Ta Lo84a80282005-01-07 02:42:53 +0000138
Eric Biederman8ca8d762003-04-22 19:02:15 +0000139void rtc_init(int invalid)
140{
Maciej Pijankaea921852009-10-27 14:29:29 +0000141#if CONFIG_HAVE_OPTION_TABLE
Eric Biederman8ca8d762003-04-22 19:02:15 +0000142 unsigned char x;
143 int cmos_invalid, checksum_invalid;
Maciej Pijankaea921852009-10-27 14:29:29 +0000144#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000145
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000146 printk(BIOS_DEBUG, "RTC Init\n");
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000147
Stefan Reinauer08670622009-06-30 15:17:49 +0000148#if CONFIG_HAVE_OPTION_TABLE
Eric Biederman8ca8d762003-04-22 19:02:15 +0000149 /* See if there has been a CMOS power problem. */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000150 x = cmos_read(RTC_VALID);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000151 cmos_invalid = !(x & RTC_VRT);
152
153 /* See if there is a CMOS checksum error */
154 checksum_invalid = !rtc_checksum_valid(PC_CKS_RANGE_START,
155 PC_CKS_RANGE_END,PC_CKS_LOC);
156
157 if (invalid || cmos_invalid || checksum_invalid) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000158 printk(BIOS_WARNING, "RTC:%s%s%s zeroing cmos\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +0000159 invalid?" Clear requested":"",
Eric Biederman8ca8d762003-04-22 19:02:15 +0000160 cmos_invalid?" Power Problem":"",
161 checksum_invalid?" Checksum invalid":"");
162#if 0
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000163 cmos_write(0, 0x01);
164 cmos_write(0, 0x03);
165 cmos_write(0, 0x05);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000166 for(i = 10; i < 48; i++) {
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000167 cmos_write(0, i);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000169
Eric Biederman8ca8d762003-04-22 19:02:15 +0000170 if (cmos_invalid) {
171 /* Now setup a default date of Sat 1 January 2000 */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000172 cmos_write(0, 0x00); /* seconds */
173 cmos_write(0, 0x02); /* minutes */
174 cmos_write(1, 0x04); /* hours */
175 cmos_write(7, 0x06); /* day of week */
176 cmos_write(1, 0x07); /* day of month */
177 cmos_write(1, 0x08); /* month */
178 cmos_write(0, 0x09); /* year */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179 }
180#endif
181 }
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000182#endif
183
184 /* Setup the real time clock */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000185 cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000186 /* Setup the frequency it operates at */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000187 cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000188
Stefan Reinauer08670622009-06-30 15:17:49 +0000189#if CONFIG_HAVE_OPTION_TABLE
Eric Biederman8ca8d762003-04-22 19:02:15 +0000190 /* See if there is a LB CMOS checksum error */
Stefan Reinauerb5828d72010-03-29 17:14:28 +0000191 checksum_invalid = !rtc_checksum_valid(LB_CKS_RANGE_START,
192 LB_CKS_RANGE_END,LB_CKS_LOC);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000193 if(checksum_invalid)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000194 printk(BIOS_DEBUG, "Invalid CMOS LB checksum\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000195
Eric Biederman8ca8d762003-04-22 19:02:15 +0000196 /* Make certain we have a valid checksum */
197 rtc_set_checksum(PC_CKS_RANGE_START,
198 PC_CKS_RANGE_END,PC_CKS_LOC);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000199#endif
200
Eric Biederman8ca8d762003-04-22 19:02:15 +0000201 /* Clear any pending interrupts */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000202 (void) cmos_read(RTC_INTR_FLAGS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000203}
204
205
Stefan Reinauer08670622009-06-30 15:17:49 +0000206#if CONFIG_USE_OPTION_TABLE == 1
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000207/* This routine returns the value of the requested bits
208 input bit = bit count from the beginning of the cmos image
209 length = number of bits to include in the value
210 ret = a character pointer to where the value is to be returned
211 output the value placed in ret
212 returns 0 = successful, -1 = an error occurred
213*/
214static int get_cmos_value(unsigned long bit, unsigned long length, void *vret)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000215{
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000216 unsigned char *ret;
217 unsigned long byte,byte_bit;
218 unsigned long i;
219 unsigned char uchar;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000220
Stefan Reinauer14e22772010-04-27 06:56:47 +0000221 /* The table is checked when it is built to ensure all
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000222 values are valid. */
223 ret = vret;
224 byte=bit/8; /* find the byte where the data starts */
225 byte_bit=bit%8; /* find the bit in the byte where the data starts */
226 if(length<9) { /* one byte or less */
227 uchar = cmos_read(byte); /* load the byte */
228 uchar >>= byte_bit; /* shift the bits to byte align */
229 /* clear unspecified bits */
230 ret[0] = uchar & ((1 << length) -1);
Luc Verhaegen9ceae902009-06-03 10:47:19 +0000231 }
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000232 else { /* more that one byte so transfer the whole bytes */
233 for(i=0;length;i++,length-=8,byte++) {
234 /* load the byte */
235 ret[i]=cmos_read(byte);
236 }
237 }
238 return 0;
Luc Verhaegen9ceae902009-06-03 10:47:19 +0000239}
240
Myles Watson3fe6b702009-10-09 20:13:43 +0000241int get_option(void *dest, const char *name)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000242{
243 extern struct cmos_option_table option_table;
244 struct cmos_option_table *ct;
245 struct cmos_entries *ce;
246 size_t namelen;
247 int found=0;
248
249 /* Figure out how long name is */
250 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000251
Eric Biederman8ca8d762003-04-22 19:02:15 +0000252 /* find the requested entry record */
253 ct=&option_table;
254 ce=(struct cmos_entries*)((unsigned char *)ct + ct->header_length);
255 for(;ce->tag==LB_TAG_OPTION;
256 ce=(struct cmos_entries*)((unsigned char *)ce + ce->size)) {
257 if (memcmp(ce->name, name, namelen) == 0) {
258 found=1;
259 break;
260 }
261 }
262 if(!found) {
Stefan Reinauer8e726b72010-03-29 23:01:35 +0000263 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000264 return(-2);
265 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000266
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000267 if(get_cmos_value(ce->bit, ce->length, dest))
268 return(-3);
Stefan Reinauerb5828d72010-03-29 17:14:28 +0000269 if(!rtc_checksum_valid(LB_CKS_RANGE_START,
270 LB_CKS_RANGE_END,LB_CKS_LOC))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000271 return(-4);
272 return(0);
273}
Stefan Reinauer08670622009-06-30 15:17:49 +0000274#endif /* CONFIG_USE_OPTION_TABLE */