4bbfa9ea045025a2e3f6b4ab3dd41d30aa48ca0d
[coreboot.git] / src / pc80 / mc146818rtc.c
1 #include <console/console.h>
2 #include <pc80/mc146818rtc.h>
3 #include <boot/coreboot_tables.h>
4 #include <string.h>
5 #if CONFIG_USE_OPTION_TABLE
6 #include "option_table.h"
7 #include <cbfs.h>
8 #endif
9
10 /* 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
79 #if CONFIG_USE_OPTION_TABLE
80 static int rtc_checksum_valid(int range_start, int range_end, int cks_loc)
81 {
82         int i;
83         unsigned sum, old_sum;
84         sum = 0;
85         for(i = range_start; i <= range_end; i++) {
86                 sum += cmos_read(i);
87         }
88         sum = (~sum)&0x0ffff;
89         old_sum = ((cmos_read(cks_loc)<<8) | cmos_read(cks_loc+1))&0x0ffff;
90         return sum == old_sum;
91 }
92
93 static void rtc_set_checksum(int range_start, int range_end, int cks_loc)
94 {
95         int i;
96         unsigned sum;
97         sum = 0;
98         for(i = range_start; i <= range_end; i++) {
99                 sum += cmos_read(i);
100         }
101         sum = ~(sum & 0x0ffff);
102         cmos_write(((sum >> 8) & 0x0ff), cks_loc);
103         cmos_write(((sum >> 0) & 0x0ff), cks_loc+1);
104 }
105 #endif
106
107 #if CONFIG_ARCH_X86
108 #define RTC_CONTROL_DEFAULT (RTC_24H)
109 #define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
110 #else
111 #if CONFIG_ARCH_ALPHA
112 #define RTC_CONTROL_DEFAULT (RTC_SQWE | RTC_24H)
113 #define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
114 #endif
115 #endif
116
117 void rtc_init(int invalid)
118 {
119 #if CONFIG_USE_OPTION_TABLE
120         unsigned char x;
121         int cmos_invalid, checksum_invalid;
122 #endif
123
124         printk(BIOS_DEBUG, "RTC Init\n");
125
126 #if CONFIG_USE_OPTION_TABLE
127         /* See if there has been a CMOS power problem. */
128         x = cmos_read(RTC_VALID);
129         cmos_invalid = !(x & RTC_VRT);
130
131         /* See if there is a CMOS checksum error */
132         checksum_invalid = !rtc_checksum_valid(PC_CKS_RANGE_START,
133                         PC_CKS_RANGE_END,PC_CKS_LOC);
134
135         if (invalid || cmos_invalid || checksum_invalid) {
136                 printk(BIOS_WARNING, "RTC:%s%s%s zeroing cmos\n",
137                         invalid?" Clear requested":"",
138                         cmos_invalid?" Power Problem":"",
139                         checksum_invalid?" Checksum invalid":"");
140 #if 0
141                 cmos_write(0, 0x01);
142                 cmos_write(0, 0x03);
143                 cmos_write(0, 0x05);
144                 for(i = 10; i < 48; i++) {
145                         cmos_write(0, i);
146                 }
147
148                 if (cmos_invalid) {
149                         /* Now setup a default date of Sat 1 January 2000 */
150                         cmos_write(0, 0x00); /* seconds */
151                         cmos_write(0, 0x02); /* minutes */
152                         cmos_write(1, 0x04); /* hours */
153                         cmos_write(7, 0x06); /* day of week */
154                         cmos_write(1, 0x07); /* day of month */
155                         cmos_write(1, 0x08); /* month */
156                         cmos_write(0, 0x09); /* year */
157                 }
158 #endif
159         }
160 #endif
161
162         /* Setup the real time clock */
163         cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL);
164         /* Setup the frequency it operates at */
165         cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT);
166
167 #if CONFIG_USE_OPTION_TABLE
168         /* See if there is a LB CMOS checksum error */
169         checksum_invalid = !rtc_checksum_valid(LB_CKS_RANGE_START,
170                         LB_CKS_RANGE_END,LB_CKS_LOC);
171         if(checksum_invalid)
172                 printk(BIOS_DEBUG, "Invalid CMOS LB checksum\n");
173
174         /* Make certain we have a valid checksum */
175         rtc_set_checksum(PC_CKS_RANGE_START,
176                         PC_CKS_RANGE_END,PC_CKS_LOC);
177 #endif
178
179         /* Clear any pending interrupts */
180         (void) cmos_read(RTC_INTR_FLAGS);
181 }
182
183
184 #if CONFIG_USE_OPTION_TABLE
185 /* This routine returns the value of the requested bits
186         input bit = bit count from the beginning of the cmos image
187               length = number of bits to include in the value
188               ret = a character pointer to where the value is to be returned
189         output the value placed in ret
190               returns 0 = successful, -1 = an error occurred
191 */
192 static int get_cmos_value(unsigned long bit, unsigned long length, void *vret)
193 {
194         unsigned char *ret;
195         unsigned long byte,byte_bit;
196         unsigned long i;
197         unsigned char uchar;
198
199         /* The table is checked when it is built to ensure all
200                 values are valid. */
201         ret = vret;
202         byte=bit/8;     /* find the byte where the data starts */
203         byte_bit=bit%8; /* find the bit in the byte where the data starts */
204         if(length<9) {  /* one byte or less */
205                 uchar = cmos_read(byte); /* load the byte */
206                 uchar >>= byte_bit;     /* shift the bits to byte align */
207                 /* clear unspecified bits */
208                 ret[0] = uchar & ((1 << length) -1);
209         }
210         else {  /* more that one byte so transfer the whole bytes */
211                 for(i=0;length;i++,length-=8,byte++) {
212                         /* load the byte */
213                         ret[i]=cmos_read(byte);
214                 }
215         }
216         return 0;
217 }
218
219 int get_option(void *dest, const char *name)
220 {
221         struct cmos_option_table *ct;
222         struct cmos_entries *ce;
223         size_t namelen;
224         int found=0;
225
226         /* Figure out how long name is */
227         namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
228
229         /* find the requested entry record */
230         ct=cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT);
231         if (!ct) {
232                 printk(BIOS_ERR, "cmos_layout.bin could not be found. Options are disabled\n");
233                 return(-2);
234         }
235         ce=(struct cmos_entries*)((unsigned char *)ct + ct->header_length);
236         for(;ce->tag==LB_TAG_OPTION;
237                 ce=(struct cmos_entries*)((unsigned char *)ce + ce->size)) {
238                 if (memcmp(ce->name, name, namelen) == 0) {
239                         found=1;
240                         break;
241                 }
242         }
243         if(!found) {
244                 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
245                 return(-2);
246         }
247
248         if(get_cmos_value(ce->bit, ce->length, dest))
249                 return(-3);
250         if(!rtc_checksum_valid(LB_CKS_RANGE_START,
251                         LB_CKS_RANGE_END,LB_CKS_LOC))
252                 return(-4);
253         return(0);
254 }
255
256 static int set_cmos_value(unsigned long bit, unsigned long length, void *vret)
257 {
258         unsigned char *ret;
259         unsigned long byte,byte_bit;
260         unsigned long i;
261         unsigned char uchar, mask;
262         unsigned int chksum_update_needed = 0;
263
264         ret = vret;
265         byte = bit / 8;                 /* find the byte where the data starts */
266         byte_bit = bit % 8;             /* find the bit in the byte where the data starts */
267         if(length <= 8) {               /* one byte or less */
268                 mask = (1 << length) - 1;
269                 mask <<= byte_bit;
270
271                 uchar = cmos_read(byte);
272                 uchar &= ~mask;
273                 uchar |= (ret[0] << byte_bit);
274                 cmos_write(uchar, byte);
275                 if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
276                         chksum_update_needed = 1;
277         } else {                        /* more that one byte so transfer the whole bytes */
278                 if (byte_bit || length % 8)
279                         return -1;
280
281                 for(i=0; length; i++, length-=8, byte++)
282                         cmos_write(ret[i], byte);
283                         if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
284                                 chksum_update_needed = 1;
285         }
286
287         if (chksum_update_needed) {
288                 rtc_set_checksum(LB_CKS_RANGE_START,
289                         LB_CKS_RANGE_END,LB_CKS_LOC);
290         }
291         return 0;
292 }
293
294
295 int set_option(const char *name, void *value)
296 {
297         struct cmos_option_table *ct;
298         struct cmos_entries *ce;
299         unsigned long length;
300         size_t namelen;
301         int found=0;
302
303         /* Figure out how long name is */
304         namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
305
306         /* find the requested entry record */
307         ct=cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT);
308         if (!ct) {
309                 printk(BIOS_ERR, "cmos_layout.bin could not be found. Options are disabled\n");
310                 return(-2);
311         }
312         ce=(struct cmos_entries*)((unsigned char *)ct + ct->header_length);
313         for(;ce->tag==LB_TAG_OPTION;
314                 ce=(struct cmos_entries*)((unsigned char *)ce + ce->size)) {
315                 if (memcmp(ce->name, name, namelen) == 0) {
316                         found=1;
317                         break;
318                 }
319         }
320         if(!found) {
321                 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
322                 return(-2);
323         }
324
325         length = ce->length;
326         if (ce->config == 's') {
327                 length = MAX(strlen((const char *)value) * 8, ce->length - 8);
328                 /* make sure the string is null terminated */
329                 if ((set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0})))
330                         return (-3);
331         }
332
333         if ((set_cmos_value(ce->bit, length, value)))
334                 return (-3);
335
336         return 0;
337 }
338 #endif /* CONFIG_USE_OPTION_TABLE */