2 * arch/ppc/kernel/todc_time.c
4 * Time of Day Clock support for the M48T35, M48T37, M48T59, and MC146818
5 * Real Time Clocks/Timekeepers.
7 * Author: Mark A. Greer
10 * Copyright 2001 MontaVista Software Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/time.h>
21 #include <linux/timex.h>
23 #include <asm/machdep.h>
29 * Depending on the hardware on your board and your board design, the
30 * RTC/NVRAM may be accessed either directly (like normal memory) or via
31 * address/data registers. If your board uses the direct method, set
32 * 'nvram_data' to the base address of your nvram and leave 'nvram_as0' and
33 * 'nvram_as1' NULL. If your board uses address/data regs to access nvram,
34 * set 'nvram_as0' to the address of the lower byte, set 'nvram_as1' to the
35 * address of the upper byte (leave NULL if using mv146818), and set
36 * 'nvram_data' to the address of the 8-bit data register.
38 * You also need to set 'ppc_md.nvram_read_val' and 'ppc_md.nvram_write_val' to
39 * the proper routines. There are standard ones defined further down in
40 * this file that you can use.
42 * There is a built in assumption that the RTC and NVRAM are accessed by the
43 * same mechanism (i.e., ppc_md.nvram_read_val, etc works for both).
45 * Note: Even though the documentation for the various RTC chips say that it
46 * take up to a second before it starts updating once the 'R' bit is
47 * cleared, they always seem to update even though we bang on it many
48 * times a second. This is true, except for the Dallas Semi 1746/1747
49 * (possibly others). Those chips seem to have a real problem whenever
50 * we set the 'R' bit before reading them, they basically stop counting.
54 extern spinlock_t rtc_lock;
57 * 'todc_info' should be initialized in your *_setup.c file to
58 * point to a fully initialized 'todc_info_t' structure.
59 * This structure holds all the register offsets for your particular
61 * TODC_ALLOC()/TODC_INIT() will allocate and initialize this table for you.
64 #ifdef RTC_FREQ_SELECT
65 #undef RTC_FREQ_SELECT
66 #define RTC_FREQ_SELECT control_b /* Register A */
71 #define RTC_CONTROL control_a /* Register B */
76 #define RTC_INTR_FLAGS watchdog /* Register C */
81 #define RTC_VALID interrupts /* Register D */
84 /* Access routines when RTC accessed directly (like normal memory) */
86 todc_direct_read_val(int addr)
88 return readb(todc_info->nvram_data + addr);
92 todc_direct_write_val(int addr, unsigned char val)
94 writeb(val, todc_info->nvram_data + addr);
98 /* Access routines for accessing m48txx type chips via addr/data regs */
100 todc_m48txx_read_val(int addr)
102 outb(addr, todc_info->nvram_as0);
103 outb(addr>>todc_info->as0_bits, todc_info->nvram_as1);
104 return inb(todc_info->nvram_data);
108 todc_m48txx_write_val(int addr, unsigned char val)
110 outb(addr, todc_info->nvram_as0);
111 outb(addr>>todc_info->as0_bits, todc_info->nvram_as1);
112 outb(val, todc_info->nvram_data);
116 /* Access routines for accessing mc146818 type chips via addr/data regs */
118 todc_mc146818_read_val(int addr)
120 outb(addr, todc_info->nvram_as0);
121 return inb(todc_info->nvram_data);
125 todc_mc146818_write_val(int addr, unsigned char val)
127 outb(addr, todc_info->nvram_as0);
128 outb(val, todc_info->nvram_data);
134 * Routines to make RTC chips with NVRAM buried behind an addr/data pair
135 * have the NVRAM and clock regs appear at the same level.
136 * The NVRAM will appear to start at addr 0 and the clock regs will appear
137 * to start immediately after the NVRAM (actually, start at offset
138 * todc_info->nvram_size).
141 todc_read_val(int addr)
145 if (todc_info->sw_flags & TODC_FLAG_2_LEVEL_NVRAM) {
146 if (addr < todc_info->nvram_size) { /* NVRAM */
147 ppc_md.nvram_write_val(todc_info->nvram_addr_reg, addr);
148 val = ppc_md.nvram_read_val(todc_info->nvram_data_reg);
150 else { /* Clock Reg */
151 addr -= todc_info->nvram_size;
152 val = ppc_md.nvram_read_val(addr);
156 val = ppc_md.nvram_read_val(addr);
163 todc_write_val(int addr, u_char val)
165 if (todc_info->sw_flags & TODC_FLAG_2_LEVEL_NVRAM) {
166 if (addr < todc_info->nvram_size) { /* NVRAM */
167 ppc_md.nvram_write_val(todc_info->nvram_addr_reg, addr);
168 ppc_md.nvram_write_val(todc_info->nvram_data_reg, val);
170 else { /* Clock Reg */
171 addr -= todc_info->nvram_size;
172 ppc_md.nvram_write_val(addr, val);
176 ppc_md.nvram_write_val(addr, val);
183 * There is some ugly stuff in that there are assumptions for the mc146818.
186 * - todc_info->control_a has the offset as mc146818 Register B reg
187 * - todc_info->control_b has the offset as mc146818 Register A reg
188 * - m48txx control reg's write enable or 'W' bit is same as
189 * mc146818 Register B 'SET' bit (i.e., 0x80)
191 * These assumptions were made to make the code simpler.
196 static u_char not_initialized = 1;
198 /* Make sure clocks are running */
199 if (not_initialized) {
202 cntl_b = todc_read_val(todc_info->control_b);
204 if (todc_info->rtc_type == TODC_TYPE_MC146818) {
205 if ((cntl_b & 0x70) != 0x20) {
206 printk(KERN_INFO "TODC %s %s\n",
207 "real-time-clock was stopped.",
213 todc_write_val(todc_info->control_b, cntl_b);
215 else if (todc_info->rtc_type == TODC_TYPE_DS1501) {
218 todc_info->enable_read = TODC_DS1501_CNTL_B_TE;
219 todc_info->enable_write = TODC_DS1501_CNTL_B_TE;
221 month = todc_read_val(todc_info->month);
223 if ((month & 0x80) == 0x80) {
224 printk(KERN_INFO "TODC %s %s\n",
225 "real-time-clock was stopped.",
228 todc_write_val(todc_info->month, month);
231 cntl_b &= ~TODC_DS1501_CNTL_B_TE;
232 todc_write_val(todc_info->control_b, cntl_b);
234 else { /* must be a m48txx type */
237 todc_info->enable_read = TODC_MK48TXX_CNTL_A_R;
238 todc_info->enable_write = TODC_MK48TXX_CNTL_A_W;
240 cntl_a = todc_read_val(todc_info->control_a);
242 /* Check & clear STOP bit in control B register */
243 if (cntl_b & TODC_MK48TXX_DAY_CB) {
244 printk(KERN_INFO "TODC %s %s\n",
245 "real-time-clock was stopped.",
248 cntl_a |= todc_info->enable_write;
249 cntl_b &= ~TODC_MK48TXX_DAY_CB;/* Start Oscil */
251 todc_write_val(todc_info->control_a, cntl_a);
252 todc_write_val(todc_info->control_b, cntl_b);
255 /* Make sure READ & WRITE bits are cleared. */
256 cntl_a &= ~(todc_info->enable_write |
257 todc_info->enable_read);
258 todc_write_val(todc_info->control_a, cntl_a);
269 * There is some ugly stuff in that there are assumptions that for a mc146818,
270 * the todc_info->control_a has the offset of the mc146818 Register B reg and
271 * that the register'ss 'SET' bit is the same as the m48txx's write enable
272 * bit in the control register of the m48txx (i.e., 0x80).
274 * It was done to make the code look simpler.
277 todc_get_rtc_time(void)
279 uint year, mon, day, hour, min, sec;
281 u_char save_control, uip;
283 spin_lock(&rtc_lock);
284 save_control = todc_read_val(todc_info->control_a);
286 if (todc_info->rtc_type != TODC_TYPE_MC146818) {
289 switch (todc_info->rtc_type) {
290 case TODC_TYPE_DS1557:
291 case TODC_TYPE_DS1743:
292 case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
293 case TODC_TYPE_DS1747:
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_DS1557:
325 case TODC_TYPE_DS1743:
326 case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
327 case TODC_TYPE_DS1747:
330 save_control &= ~(todc_info->enable_read);
331 todc_write_val(todc_info->control_a,
335 spin_unlock(&rtc_lock);
337 if ((todc_info->rtc_type != TODC_TYPE_MC146818) ||
338 ((save_control & RTC_DM_BINARY) == 0) ||
354 return mktime(year, mon, day, hour, min, sec);
358 todc_set_rtc_time(unsigned long nowtime)
361 u_char save_control, save_freq_select;
363 spin_lock(&rtc_lock);
366 save_control = todc_read_val(todc_info->control_a);
368 /* Assuming MK48T59_RTC_CA_WRITE & RTC_SET are equal */
369 todc_write_val(todc_info->control_a,
370 (save_control | todc_info->enable_write));
371 save_control &= ~(todc_info->enable_write); /* in case it was set */
373 if (todc_info->rtc_type == TODC_TYPE_MC146818) {
374 save_freq_select = todc_read_val(todc_info->RTC_FREQ_SELECT);
375 todc_write_val(todc_info->RTC_FREQ_SELECT,
376 save_freq_select | RTC_DIV_RESET2);
380 tm.tm_year = (tm.tm_year - 1900) % 100;
382 if ((todc_info->rtc_type != TODC_TYPE_MC146818) ||
383 ((save_control & RTC_DM_BINARY) == 0) ||
386 BIN_TO_BCD(tm.tm_sec);
387 BIN_TO_BCD(tm.tm_min);
388 BIN_TO_BCD(tm.tm_hour);
389 BIN_TO_BCD(tm.tm_mon);
390 BIN_TO_BCD(tm.tm_mday);
391 BIN_TO_BCD(tm.tm_year);
394 todc_write_val(todc_info->seconds, tm.tm_sec);
395 todc_write_val(todc_info->minutes, tm.tm_min);
396 todc_write_val(todc_info->hours, tm.tm_hour);
397 todc_write_val(todc_info->month, tm.tm_mon);
398 todc_write_val(todc_info->day_of_month, tm.tm_mday);
399 todc_write_val(todc_info->year, tm.tm_year);
401 todc_write_val(todc_info->control_a, save_control);
403 if (todc_info->rtc_type == TODC_TYPE_MC146818) {
404 todc_write_val(todc_info->RTC_FREQ_SELECT, save_freq_select);
406 spin_unlock(&rtc_lock);
412 * Manipulates read bit to reliably read seconds at a high rate.
414 static unsigned char __init todc_read_timereg(int addr)
416 unsigned char save_control, val;
418 switch (todc_info->rtc_type) {
419 case TODC_TYPE_DS1557:
420 case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
421 case TODC_TYPE_DS1747:
422 case TODC_TYPE_MC146818:
425 save_control = todc_read_val(todc_info->control_a);
426 todc_write_val(todc_info->control_a,
427 (save_control | todc_info->enable_read));
429 val = todc_read_val(addr);
431 switch (todc_info->rtc_type) {
432 case TODC_TYPE_DS1557:
433 case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
434 case TODC_TYPE_DS1747:
435 case TODC_TYPE_MC146818:
438 save_control &= ~(todc_info->enable_read);
439 todc_write_val(todc_info->control_a, save_control);
446 * This was taken from prep_setup.c
447 * Use the NVRAM RTC to time a second to calibrate the decrementer.
450 todc_calibrate_decr(void)
460 * Actually this is bad for precision, we should have a loop in
461 * which we only read the seconds counter. todc_read_val writes
462 * the address bytes on every call and this takes a lot of time.
463 * Perhaps an nvram_wait_change method returning a time
464 * stamp with a loop count as parameter would be the solution.
467 * Need to make sure the tbl doesn't roll over so if tbu increments
468 * during this test, we need to do it again.
472 sec = todc_read_timereg(todc_info->seconds) & 0x7f;
477 for (i = 0 ; i < 10000000 ; i++) {/* may take up to 1 second */
480 if ((todc_read_timereg(todc_info->seconds) & 0x7f) != sec) {
485 sec = todc_read_timereg(todc_info->seconds) & 0x7f;
487 for (i = 0 ; i < 10000000 ; i++) { /* Should take 1 second */
490 if ((todc_read_timereg(todc_info->seconds) & 0x7f) != sec) {
496 } while ((get_tbu() != tbu) && (++loop_count < 2));
498 printk("time_init: decrementer frequency = %lu.%.6lu MHz\n",
499 freq/1000000, freq%1000000);
501 tb_ticks_per_jiffy = freq / HZ;
502 tb_to_us = mulhwu_scale_factor(freq, 1000000);