3 #include <console/console.h>
4 #include <pc80/mc146818rtc.h>
5 #include <boot/coreboot_tables.h>
7 #if CONFIG_USE_OPTION_TABLE
8 #include "option_table.h"
12 /* control registers - Moto names
20 /**********************************************************************
22 **********************************************************************/
23 #define RTC_FREQ_SELECT RTC_REG_A
25 /* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus,
26 * reset after update (may take 1.984ms @ 32768Hz RefClock) is complete,
27 * totalling to a max high interval of 2.228 ms.
30 # define RTC_DIV_CTL 0x70
31 /* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */
32 # define RTC_REF_CLCK_4MHZ 0x00
33 # define RTC_REF_CLCK_1MHZ 0x10
34 # define RTC_REF_CLCK_32KHZ 0x20
35 /* 2 values for divider stage reset, others for "testing purposes only" */
36 # define RTC_DIV_RESET1 0x60
37 # define RTC_DIV_RESET2 0x70
38 /* Periodic intr. / Square wave rate select. 0=none, 1=32.8kHz,... 15=2Hz */
39 # define RTC_RATE_SELECT 0x0F
40 # define RTC_RATE_NONE 0x00
41 # define RTC_RATE_32786HZ 0x01
42 # define RTC_RATE_16384HZ 0x02
43 # define RTC_RATE_8192HZ 0x03
44 # define RTC_RATE_4096HZ 0x04
45 # define RTC_RATE_2048HZ 0x05
46 # define RTC_RATE_1024HZ 0x06
47 # define RTC_RATE_512HZ 0x07
48 # define RTC_RATE_256HZ 0x08
49 # define RTC_RATE_128HZ 0x09
50 # define RTC_RATE_64HZ 0x0a
51 # define RTC_RATE_32HZ 0x0b
52 # define RTC_RATE_16HZ 0x0c
53 # define RTC_RATE_8HZ 0x0d
54 # define RTC_RATE_4HZ 0x0e
55 # define RTC_RATE_2HZ 0x0f
57 /**********************************************************************/
58 #define RTC_CONTROL RTC_REG_B
59 # define RTC_SET 0x80 /* disable updates for clock setting */
60 # define RTC_PIE 0x40 /* periodic interrupt enable */
61 # define RTC_AIE 0x20 /* alarm interrupt enable */
62 # define RTC_UIE 0x10 /* update-finished interrupt enable */
63 # define RTC_SQWE 0x08 /* enable square-wave output */
64 # define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
65 # define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
66 # define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
68 /**********************************************************************/
69 #define RTC_INTR_FLAGS RTC_REG_C
70 /* caution - cleared by read */
71 # define RTC_IRQF 0x80 /* any of the following 3 is active */
76 /**********************************************************************/
77 #define RTC_VALID RTC_REG_D
78 # define RTC_VRT 0x80 /* valid RAM and time */
79 /**********************************************************************/
81 static void rtc_update_cmos_date(u8 has_century
)
83 /* Now setup a default date equals to the build date */
84 cmos_write(0, RTC_CLK_SECOND
);
85 cmos_write(0, RTC_CLK_MINUTE
);
86 cmos_write(1, RTC_CLK_HOUR
);
87 cmos_write(COREBOOT_BUILD_WEEKDAY
, RTC_CLK_DAYOFWEEK
);
88 cmos_write(COREBOOT_BUILD_DAY
, RTC_CLK_DAYOFMONTH
);
89 cmos_write(COREBOOT_BUILD_MONTH
, RTC_CLK_MINUTE
);
90 cmos_write(COREBOOT_BUILD_YEAR
, RTC_CLK_YEAR
);
91 if (has_century
) cmos_write(0x20, RTC_CLK_ALTCENTURY
);
94 #if CONFIG_USE_OPTION_TABLE
95 static int rtc_checksum_valid(int range_start
, int range_end
, int cks_loc
)
100 for(i
= range_start
; i
<= range_end
; i
++) {
103 old_sum
= ((cmos_read(cks_loc
)<<8) | cmos_read(cks_loc
+1))&0x0ffff;
104 return sum
== old_sum
;
107 static void rtc_set_checksum(int range_start
, int range_end
, int cks_loc
)
112 for(i
= range_start
; i
<= range_end
; i
++) {
115 cmos_write(((sum
>> 8) & 0x0ff), cks_loc
);
116 cmos_write(((sum
>> 0) & 0x0ff), cks_loc
+1);
121 #define RTC_CONTROL_DEFAULT (RTC_24H)
122 #define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
124 #if CONFIG_ARCH_ALPHA
125 #define RTC_CONTROL_DEFAULT (RTC_SQWE | RTC_24H)
126 #define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
130 void rtc_init(int invalid
)
132 int cmos_invalid
= 0;
133 int checksum_invalid
= 0;
134 #if CONFIG_USE_OPTION_TABLE
138 printk(BIOS_DEBUG
, "RTC Init\n");
140 #if CONFIG_USE_OPTION_TABLE
141 /* See if there has been a CMOS power problem. */
142 x
= cmos_read(RTC_VALID
);
143 cmos_invalid
= !(x
& RTC_VRT
);
145 /* See if there is a CMOS checksum error */
146 checksum_invalid
= !rtc_checksum_valid(PC_CKS_RANGE_START
,
147 PC_CKS_RANGE_END
,PC_CKS_LOC
);
154 if (invalid
|| cmos_invalid
|| checksum_invalid
) {
161 for(i
= 10; i
< 128; i
++) {
166 rtc_update_cmos_date(RTC_HAS_NO_ALTCENTURY
);
169 printk(BIOS_WARNING
, "RTC:%s%s%s%s\n",
170 invalid
?" Clear requested":"",
171 cmos_invalid
?" Power Problem":"",
172 checksum_invalid
?" Checksum invalid":"",
173 CLEAR_CMOS
?" zeroing cmos":"");
176 /* Setup the real time clock */
177 cmos_write(RTC_CONTROL_DEFAULT
, RTC_CONTROL
);
178 /* Setup the frequency it operates at */
179 cmos_write(RTC_FREQ_SELECT_DEFAULT
, RTC_FREQ_SELECT
);
180 /* Ensure all reserved bits are 0 in register D */
181 cmos_write(RTC_VRT
, RTC_VALID
);
183 #if CONFIG_USE_OPTION_TABLE
184 /* See if there is a LB CMOS checksum error */
185 checksum_invalid
= !rtc_checksum_valid(LB_CKS_RANGE_START
,
186 LB_CKS_RANGE_END
,LB_CKS_LOC
);
188 printk(BIOS_DEBUG
, "RTC: coreboot checksum invalid\n");
190 /* Make certain we have a valid checksum */
191 rtc_set_checksum(PC_CKS_RANGE_START
,
192 PC_CKS_RANGE_END
,PC_CKS_LOC
);
195 /* Clear any pending interrupts */
196 (void) cmos_read(RTC_INTR_FLAGS
);
200 #if CONFIG_USE_OPTION_TABLE
201 /* This routine returns the value of the requested bits
202 input bit = bit count from the beginning of the cmos image
203 length = number of bits to include in the value
204 ret = a character pointer to where the value is to be returned
205 output the value placed in ret
206 returns 0 = successful, -1 = an error occurred
208 static int get_cmos_value(unsigned long bit
, unsigned long length
, void *vret
)
211 unsigned long byte
,byte_bit
;
215 /* The table is checked when it is built to ensure all
218 byte
=bit
/8; /* find the byte where the data starts */
219 byte_bit
=bit
%8; /* find the bit in the byte where the data starts */
220 if(length
<9) { /* one byte or less */
221 uchar
= cmos_read(byte
); /* load the byte */
222 uchar
>>= byte_bit
; /* shift the bits to byte align */
223 /* clear unspecified bits */
224 ret
[0] = uchar
& ((1 << length
) -1);
226 else { /* more that one byte so transfer the whole bytes */
227 for(i
=0;length
;i
++,length
-=8,byte
++) {
229 ret
[i
]=cmos_read(byte
);
235 int get_option(void *dest
, const char *name
)
237 struct cmos_option_table
*ct
;
238 struct cmos_entries
*ce
;
242 /* Figure out how long name is */
243 namelen
= strnlen(name
, CMOS_MAX_NAME_LENGTH
);
245 /* find the requested entry record */
246 ct
=cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT
);
248 printk(BIOS_ERR
, "RTC: cmos_layout.bin could not be found. "
249 "Options are disabled\n");
252 ce
=(struct cmos_entries
*)((unsigned char *)ct
+ ct
->header_length
);
253 for(;ce
->tag
==LB_TAG_OPTION
;
254 ce
=(struct cmos_entries
*)((unsigned char *)ce
+ ce
->size
)) {
255 if (memcmp(ce
->name
, name
, namelen
) == 0) {
261 printk(BIOS_DEBUG
, "WARNING: No CMOS option '%s'.\n", name
);
265 if(get_cmos_value(ce
->bit
, ce
->length
, dest
))
267 if(!rtc_checksum_valid(LB_CKS_RANGE_START
,
268 LB_CKS_RANGE_END
,LB_CKS_LOC
))
273 static int set_cmos_value(unsigned long bit
, unsigned long length
, void *vret
)
276 unsigned long byte
,byte_bit
;
278 unsigned char uchar
, mask
;
279 unsigned int chksum_update_needed
= 0;
282 byte
= bit
/ 8; /* find the byte where the data starts */
283 byte_bit
= bit
% 8; /* find the bit in the byte where the data starts */
284 if(length
<= 8) { /* one byte or less */
285 mask
= (1 << length
) - 1;
288 uchar
= cmos_read(byte
);
290 uchar
|= (ret
[0] << byte_bit
);
291 cmos_write(uchar
, byte
);
292 if (byte
>= LB_CKS_RANGE_START
&& byte
<= LB_CKS_RANGE_END
)
293 chksum_update_needed
= 1;
294 } else { /* more that one byte so transfer the whole bytes */
295 if (byte_bit
|| length
% 8)
298 for(i
=0; length
; i
++, length
-=8, byte
++)
299 cmos_write(ret
[i
], byte
);
300 if (byte
>= LB_CKS_RANGE_START
&& byte
<= LB_CKS_RANGE_END
)
301 chksum_update_needed
= 1;
304 if (chksum_update_needed
) {
305 rtc_set_checksum(LB_CKS_RANGE_START
,
306 LB_CKS_RANGE_END
,LB_CKS_LOC
);
312 int set_option(const char *name
, void *value
)
314 struct cmos_option_table
*ct
;
315 struct cmos_entries
*ce
;
316 unsigned long length
;
320 /* Figure out how long name is */
321 namelen
= strnlen(name
, CMOS_MAX_NAME_LENGTH
);
323 /* find the requested entry record */
324 ct
=cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT
);
326 printk(BIOS_ERR
, "cmos_layout.bin could not be found. Options are disabled\n");
329 ce
=(struct cmos_entries
*)((unsigned char *)ct
+ ct
->header_length
);
330 for(;ce
->tag
==LB_TAG_OPTION
;
331 ce
=(struct cmos_entries
*)((unsigned char *)ce
+ ce
->size
)) {
332 if (memcmp(ce
->name
, name
, namelen
) == 0) {
338 printk(BIOS_DEBUG
, "WARNING: No CMOS option '%s'.\n", name
);
343 if (ce
->config
== 's') {
344 length
= MAX(strlen((const char *)value
) * 8, ce
->length
- 8);
345 /* make sure the string is null terminated */
346 if ((set_cmos_value(ce
->bit
+ ce
->length
- 8, 8, &(u8
[]){0})))
350 if ((set_cmos_value(ce
->bit
, length
, value
)))
355 #endif /* CONFIG_USE_OPTION_TABLE */
358 * If the CMOS is cleared, the rtc_reg has the invalid date. That
359 * hurts some OSes. Even if we don't set USE_OPTION_TABLE, we need
360 * to make sure the date is valid.
362 void rtc_check_update_cmos_date(u8 has_century
)
366 /* Note: We need to check if the hardware supports RTC_CLK_ALTCENTURY. */
367 century
= has_century
? cmos_read(RTC_CLK_ALTCENTURY
) : 0;
368 year
= cmos_read(RTC_CLK_YEAR
);
370 /* TODO: If century is 0xFF, 100% that the cmos is cleared.
371 * Other than that, so far rtc_year is the only entry to check if the date is valid. */
372 if (century
> 0x99 || year
> 0x99) { /* Invalid date */
373 rtc_update_cmos_date(has_century
);