2 * Time of Day Clock support for the M48T35, M48T37, M48T59, and MC146818
3 * Real Time Clocks/Timekeepers.
5 * Author: Mark A. Greer
8 * 2001-2004 (c) MontaVista, Software, Inc. This file is licensed under
9 * the terms of the GNU General Public License version 2. This program
10 * is licensed "as is" without any warranty of any kind, whether express
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/time.h>
17 #include <linux/timex.h>
18 #include <linux/bcd.h>
19 #include <linux/mc146818rtc.h>
21 #include <asm/machdep.h>
27 * Depending on the hardware on your board and your board design, the
28 * RTC/NVRAM may be accessed either directly (like normal memory) or via
29 * address/data registers. If your board uses the direct method, set
30 * 'nvram_data' to the base address of your nvram and leave 'nvram_as0' and
31 * 'nvram_as1' NULL. If your board uses address/data regs to access nvram,
32 * set 'nvram_as0' to the address of the lower byte, set 'nvram_as1' to the
33 * address of the upper byte (leave NULL if using mc146818), and set
34 * 'nvram_data' to the address of the 8-bit data register.
36 * In order to break the assumption that the RTC and NVRAM are accessed by
37 * the same mechanism, you need to explicitly set 'ppc_md.rtc_read_val' and
38 * 'ppc_md.rtc_write_val', otherwise the values of 'ppc_md.rtc_read_val'
39 * and 'ppc_md.rtc_write_val' will be used.
41 * Note: Even though the documentation for the various RTC chips say that it
42 * take up to a second before it starts updating once the 'R' bit is
43 * cleared, they always seem to update even though we bang on it many
44 * times a second. This is true, except for the Dallas Semi 1746/1747
45 * (possibly others). Those chips seem to have a real problem whenever
46 * we set the 'R' bit before reading them, they basically stop counting.
51 * 'todc_info' should be initialized in your *_setup.c file to
52 * point to a fully initialized 'todc_info_t' structure.
53 * This structure holds all the register offsets for your particular
55 * TODC_ALLOC()/TODC_INIT() will allocate and initialize this table for you.
58 #ifdef RTC_FREQ_SELECT
59 #undef RTC_FREQ_SELECT
60 #define RTC_FREQ_SELECT control_b /* Register A */
65 #define RTC_CONTROL control_a /* Register B */
70 #define RTC_INTR_FLAGS watchdog /* Register C */
75 #define RTC_VALID interrupts /* Register D */
78 /* Access routines when RTC accessed directly (like normal memory) */
80 todc_direct_read_val(int addr)
82 return readb((void __iomem *)(todc_info->nvram_data + addr));
86 todc_direct_write_val(int addr, unsigned char val)
88 writeb(val, (void __iomem *)(todc_info->nvram_data + addr));
92 /* Access routines for accessing m48txx type chips via addr/data regs */
94 todc_m48txx_read_val(int addr)
96 outb(addr, todc_info->nvram_as0);
97 outb(addr>>todc_info->as0_bits, todc_info->nvram_as1);
98 return inb(todc_info->nvram_data);
102 todc_m48txx_write_val(int addr, unsigned char val)
104 outb(addr, todc_info->nvram_as0);
105 outb(addr>>todc_info->as0_bits, todc_info->nvram_as1);
106 outb(val, todc_info->nvram_data);
110 /* Access routines for accessing mc146818 type chips via addr/data regs */
112 todc_mc146818_read_val(int addr)
114 outb_p(addr, todc_info->nvram_as0);
115 return inb_p(todc_info->nvram_data);
119 todc_mc146818_write_val(int addr, unsigned char val)
121 outb_p(addr, todc_info->nvram_as0);
122 outb_p(val, todc_info->nvram_data);
127 * Routines to make RTC chips with NVRAM buried behind an addr/data pair
128 * have the NVRAM and clock regs appear at the same level.
129 * The NVRAM will appear to start at addr 0 and the clock regs will appear
130 * to start immediately after the NVRAM (actually, start at offset
131 * todc_info->nvram_size).
134 todc_read_val(int addr)
138 if (todc_info->sw_flags & TODC_FLAG_2_LEVEL_NVRAM) {
139 if (addr < todc_info->nvram_size) { /* NVRAM */
140 ppc_md.rtc_write_val(todc_info->nvram_addr_reg, addr);
141 val = ppc_md.rtc_read_val(todc_info->nvram_data_reg);
143 else { /* Clock Reg */
144 addr -= todc_info->nvram_size;
145 val = ppc_md.rtc_read_val(addr);
149 val = ppc_md.rtc_read_val(addr);
156 todc_write_val(int addr, u_char val)
158 if (todc_info->sw_flags & TODC_FLAG_2_LEVEL_NVRAM) {
159 if (addr < todc_info->nvram_size) { /* NVRAM */
160 ppc_md.rtc_write_val(todc_info->nvram_addr_reg, addr);
161 ppc_md.rtc_write_val(todc_info->nvram_data_reg, val);
163 else { /* Clock Reg */
164 addr -= todc_info->nvram_size;
165 ppc_md.rtc_write_val(addr, val);
169 ppc_md.rtc_write_val(addr, val);
176 * There is some ugly stuff in that there are assumptions for the mc146818.
179 * - todc_info->control_a has the offset as mc146818 Register B reg
180 * - todc_info->control_b has the offset as mc146818 Register A reg
181 * - m48txx control reg's write enable or 'W' bit is same as
182 * mc146818 Register B 'SET' bit (i.e., 0x80)
184 * These assumptions were made to make the code simpler.
191 if (!ppc_md.rtc_read_val)
192 ppc_md.rtc_read_val = ppc_md.nvram_read_val;
193 if (!ppc_md.rtc_write_val)
194 ppc_md.rtc_write_val = ppc_md.nvram_write_val;
196 cntl_b = todc_read_val(todc_info->control_b);
198 if (todc_info->rtc_type == TODC_TYPE_MC146818) {
199 if ((cntl_b & 0x70) != 0x20) {
200 printk(KERN_INFO "TODC %s %s\n",
201 "real-time-clock was stopped.",
207 todc_write_val(todc_info->control_b, cntl_b);
208 } else if (todc_info->rtc_type == TODC_TYPE_DS17285) {
211 mode = todc_read_val(TODC_TYPE_DS17285_CNTL_A);
212 /* Make sure countdown clear is not set */
214 /* Enable oscillator, extended register set */
216 todc_write_val(TODC_TYPE_DS17285_CNTL_A, mode);
218 } else if (todc_info->rtc_type == TODC_TYPE_DS1501) {
221 todc_info->enable_read = TODC_DS1501_CNTL_B_TE;
222 todc_info->enable_write = TODC_DS1501_CNTL_B_TE;
224 month = todc_read_val(todc_info->month);
226 if ((month & 0x80) == 0x80) {
227 printk(KERN_INFO "TODC %s %s\n",
228 "real-time-clock was stopped.",
231 todc_write_val(todc_info->month, month);
234 cntl_b &= ~TODC_DS1501_CNTL_B_TE;
235 todc_write_val(todc_info->control_b, cntl_b);
236 } else { /* must be a m48txx type */
239 todc_info->enable_read = TODC_MK48TXX_CNTL_A_R;
240 todc_info->enable_write = TODC_MK48TXX_CNTL_A_W;
242 cntl_a = todc_read_val(todc_info->control_a);
244 /* Check & clear STOP bit in control B register */
245 if (cntl_b & TODC_MK48TXX_DAY_CB) {
246 printk(KERN_INFO "TODC %s %s\n",
247 "real-time-clock was stopped.",
250 cntl_a |= todc_info->enable_write;
251 cntl_b &= ~TODC_MK48TXX_DAY_CB;/* Start Oscil */
253 todc_write_val(todc_info->control_a, cntl_a);
254 todc_write_val(todc_info->control_b, cntl_b);
257 /* Make sure READ & WRITE bits are cleared. */
258 cntl_a &= ~(todc_info->enable_write |
259 todc_info->enable_read);
260 todc_write_val(todc_info->control_a, cntl_a);
267 * There is some ugly stuff in that there are assumptions that for a mc146818,
268 * the todc_info->control_a has the offset of the mc146818 Register B reg and
269 * that the register'ss 'SET' bit is the same as the m48txx's write enable
270 * bit in the control register of the m48txx (i.e., 0x80).
272 * It was done to make the code look simpler.
275 todc_get_rtc_time(void)
277 uint year = 0, mon = 0, day = 0, hour = 0, min = 0, sec = 0;
279 u_char save_control, uip = 0;
281 spin_lock(&rtc_lock);
282 save_control = todc_read_val(todc_info->control_a);
284 if (todc_info->rtc_type != TODC_TYPE_MC146818) {
287 switch (todc_info->rtc_type) {
288 case TODC_TYPE_DS1553:
289 case TODC_TYPE_DS1557:
290 case TODC_TYPE_DS1743:
291 case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
292 case TODC_TYPE_DS1747:
293 case TODC_TYPE_DS17285:
296 todc_write_val(todc_info->control_a,
297 (save_control | todc_info->enable_read));
304 for (i=0; i<limit; i++) {
305 if (todc_info->rtc_type == TODC_TYPE_MC146818) {
306 uip = todc_read_val(todc_info->RTC_FREQ_SELECT);
309 sec = todc_read_val(todc_info->seconds) & 0x7f;
310 min = todc_read_val(todc_info->minutes) & 0x7f;
311 hour = todc_read_val(todc_info->hours) & 0x3f;
312 day = todc_read_val(todc_info->day_of_month) & 0x3f;
313 mon = todc_read_val(todc_info->month) & 0x1f;
314 year = todc_read_val(todc_info->year) & 0xff;
316 if (todc_info->rtc_type == TODC_TYPE_MC146818) {
317 uip |= todc_read_val(todc_info->RTC_FREQ_SELECT);
318 if ((uip & RTC_UIP) == 0) break;
322 if (todc_info->rtc_type != TODC_TYPE_MC146818) {
323 switch (todc_info->rtc_type) {
324 case TODC_TYPE_DS1553:
325 case TODC_TYPE_DS1557:
326 case TODC_TYPE_DS1743:
327 case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
328 case TODC_TYPE_DS1747:
329 case TODC_TYPE_DS17285:
332 save_control &= ~(todc_info->enable_read);
333 todc_write_val(todc_info->control_a,
337 spin_unlock(&rtc_lock);
339 if ((todc_info->rtc_type != TODC_TYPE_MC146818) ||
340 ((save_control & RTC_DM_BINARY) == 0) ||
356 return mktime(year, mon, day, hour, min, sec);
360 todc_set_rtc_time(unsigned long nowtime)
363 u_char save_control, save_freq_select = 0;
365 spin_lock(&rtc_lock);
368 save_control = todc_read_val(todc_info->control_a);
370 /* Assuming MK48T59_RTC_CA_WRITE & RTC_SET are equal */
371 todc_write_val(todc_info->control_a,
372 (save_control | todc_info->enable_write));
373 save_control &= ~(todc_info->enable_write); /* in case it was set */
375 if (todc_info->rtc_type == TODC_TYPE_MC146818) {
376 save_freq_select = todc_read_val(todc_info->RTC_FREQ_SELECT);
377 todc_write_val(todc_info->RTC_FREQ_SELECT,
378 save_freq_select | RTC_DIV_RESET2);
382 tm.tm_year = (tm.tm_year - 1900) % 100;
384 if ((todc_info->rtc_type != TODC_TYPE_MC146818) ||
385 ((save_control & RTC_DM_BINARY) == 0) ||
388 BIN_TO_BCD(tm.tm_sec);
389 BIN_TO_BCD(tm.tm_min);
390 BIN_TO_BCD(tm.tm_hour);
391 BIN_TO_BCD(tm.tm_mon);
392 BIN_TO_BCD(tm.tm_mday);
393 BIN_TO_BCD(tm.tm_year);
396 todc_write_val(todc_info->seconds, tm.tm_sec);
397 todc_write_val(todc_info->minutes, tm.tm_min);
398 todc_write_val(todc_info->hours, tm.tm_hour);
399 todc_write_val(todc_info->month, tm.tm_mon);
400 todc_write_val(todc_info->day_of_month, tm.tm_mday);
401 todc_write_val(todc_info->year, tm.tm_year);
403 todc_write_val(todc_info->control_a, save_control);
405 if (todc_info->rtc_type == TODC_TYPE_MC146818) {
406 todc_write_val(todc_info->RTC_FREQ_SELECT, save_freq_select);
408 spin_unlock(&rtc_lock);
414 * Manipulates read bit to reliably read seconds at a high rate.
416 static unsigned char __init todc_read_timereg(int addr)
418 unsigned char save_control = 0, val;
420 switch (todc_info->rtc_type) {
421 case TODC_TYPE_DS1553:
422 case TODC_TYPE_DS1557:
423 case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
424 case TODC_TYPE_DS1747:
425 case TODC_TYPE_DS17285:
426 case TODC_TYPE_MC146818:
429 save_control = todc_read_val(todc_info->control_a);
430 todc_write_val(todc_info->control_a,
431 (save_control | todc_info->enable_read));
433 val = todc_read_val(addr);
435 switch (todc_info->rtc_type) {
436 case TODC_TYPE_DS1553:
437 case TODC_TYPE_DS1557:
438 case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
439 case TODC_TYPE_DS1747:
440 case TODC_TYPE_DS17285:
441 case TODC_TYPE_MC146818:
444 save_control &= ~(todc_info->enable_read);
445 todc_write_val(todc_info->control_a, save_control);
452 * This was taken from prep_setup.c
453 * Use the NVRAM RTC to time a second to calibrate the decrementer.
456 todc_calibrate_decr(void)
466 * Actually this is bad for precision, we should have a loop in
467 * which we only read the seconds counter. todc_read_val writes
468 * the address bytes on every call and this takes a lot of time.
469 * Perhaps an nvram_wait_change method returning a time
470 * stamp with a loop count as parameter would be the solution.
473 * Need to make sure the tbl doesn't roll over so if tbu increments
474 * during this test, we need to do it again.
478 sec = todc_read_timereg(todc_info->seconds) & 0x7f;
483 for (i = 0 ; i < 10000000 ; i++) {/* may take up to 1 second */
486 if ((todc_read_timereg(todc_info->seconds) & 0x7f) != sec) {
491 sec = todc_read_timereg(todc_info->seconds) & 0x7f;
493 for (i = 0 ; i < 10000000 ; i++) { /* Should take 1 second */
496 if ((todc_read_timereg(todc_info->seconds) & 0x7f) != sec) {
502 } while ((get_tbu() != tbu) && (++loop_count < 2));
504 printk("time_init: decrementer frequency = %lu.%.6lu MHz\n",
505 freq/1000000, freq%1000000);
507 tb_ticks_per_jiffy = freq / HZ;
508 tb_to_us = mulhwu_scale_factor(freq, 1000000);