[PATCH] irq-flags: PPC: Use the new IRQF_ constants
[linux-2.6/kvm.git] / arch / ppc / syslib / todc_time.c
bloba8168b8e5683b1fa94a6344a340e2ae28158a6ef
1 /*
2 * Time of Day Clock support for the M48T35, M48T37, M48T59, and MC146818
3 * Real Time Clocks/Timekeepers.
5 * Author: Mark A. Greer
6 * mgreer@mvista.com
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
11 * or implied.
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>
22 #include <asm/io.h>
23 #include <asm/time.h>
24 #include <asm/todc.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.
47 * --MAG
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
54 * TODC/RTC chip.
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 */
61 #endif
63 #ifdef RTC_CONTROL
64 #undef RTC_CONTROL
65 #define RTC_CONTROL control_a /* Register B */
66 #endif
68 #ifdef RTC_INTR_FLAGS
69 #undef RTC_INTR_FLAGS
70 #define RTC_INTR_FLAGS watchdog /* Register C */
71 #endif
73 #ifdef RTC_VALID
74 #undef RTC_VALID
75 #define RTC_VALID interrupts /* Register D */
76 #endif
78 /* Access routines when RTC accessed directly (like normal memory) */
79 u_char
80 todc_direct_read_val(int addr)
82 return readb((void __iomem *)(todc_info->nvram_data + addr));
85 void
86 todc_direct_write_val(int addr, unsigned char val)
88 writeb(val, (void __iomem *)(todc_info->nvram_data + addr));
89 return;
92 /* Access routines for accessing m48txx type chips via addr/data regs */
93 u_char
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);
101 void
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);
107 return;
110 /* Access routines for accessing mc146818 type chips via addr/data regs */
111 u_char
112 todc_mc146818_read_val(int addr)
114 outb_p(addr, todc_info->nvram_as0);
115 return inb_p(todc_info->nvram_data);
118 void
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).
133 static inline u_char
134 todc_read_val(int addr)
136 u_char val;
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);
148 else {
149 val = ppc_md.rtc_read_val(addr);
152 return val;
155 static inline void
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);
168 else {
169 ppc_md.rtc_write_val(addr, val);
174 * TODC routines
176 * There is some ugly stuff in that there are assumptions for the mc146818.
178 * Assumptions:
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.
186 long __init
187 todc_time_init(void)
189 u_char cntl_b;
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.",
202 "Now starting...");
203 cntl_b &= ~0x70;
204 cntl_b |= 0x20;
207 todc_write_val(todc_info->control_b, cntl_b);
208 } else if (todc_info->rtc_type == TODC_TYPE_DS17285) {
209 u_char mode;
211 mode = todc_read_val(TODC_TYPE_DS17285_CNTL_A);
212 /* Make sure countdown clear is not set */
213 mode &= ~0x40;
214 /* Enable oscillator, extended register set */
215 mode |= 0x30;
216 todc_write_val(TODC_TYPE_DS17285_CNTL_A, mode);
218 } else if (todc_info->rtc_type == TODC_TYPE_DS1501) {
219 u_char month;
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.",
229 "Now starting...");
230 month &= ~0x80;
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 */
237 u_char cntl_a;
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.",
248 "Now starting...");
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);
263 return 0;
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.
274 ulong
275 todc_get_rtc_time(void)
277 uint year = 0, mon = 0, day = 0, hour = 0, min = 0, sec = 0;
278 uint limit, i;
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) {
285 limit = 1;
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:
294 break;
295 default:
296 todc_write_val(todc_info->control_a,
297 (save_control | todc_info->enable_read));
300 else {
301 limit = 100000000;
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:
330 break;
331 default:
332 save_control &= ~(todc_info->enable_read);
333 todc_write_val(todc_info->control_a,
334 save_control);
337 spin_unlock(&rtc_lock);
339 if ((todc_info->rtc_type != TODC_TYPE_MC146818) ||
340 ((save_control & RTC_DM_BINARY) == 0) ||
341 RTC_ALWAYS_BCD) {
343 BCD_TO_BIN(sec);
344 BCD_TO_BIN(min);
345 BCD_TO_BIN(hour);
346 BCD_TO_BIN(day);
347 BCD_TO_BIN(mon);
348 BCD_TO_BIN(year);
351 year = year + 1900;
352 if (year < 1970) {
353 year += 100;
356 return mktime(year, mon, day, hour, min, sec);
360 todc_set_rtc_time(unsigned long nowtime)
362 struct rtc_time tm;
363 u_char save_control, save_freq_select = 0;
365 spin_lock(&rtc_lock);
366 to_tm(nowtime, &tm);
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) ||
386 RTC_ALWAYS_BCD) {
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);
410 return 0;
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:
427 break;
428 default:
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:
442 break;
443 default:
444 save_control &= ~(todc_info->enable_read);
445 todc_write_val(todc_info->control_a, save_control);
448 return val;
452 * This was taken from prep_setup.c
453 * Use the NVRAM RTC to time a second to calibrate the decrementer.
455 void __init
456 todc_calibrate_decr(void)
458 ulong freq;
459 ulong tbl, tbu;
460 long i, loop_count;
461 u_char sec;
463 todc_time_init();
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.
476 loop_count = 0;
478 sec = todc_read_timereg(todc_info->seconds) & 0x7f;
480 do {
481 tbu = get_tbu();
483 for (i = 0 ; i < 10000000 ; i++) {/* may take up to 1 second */
484 tbl = get_tbl();
486 if ((todc_read_timereg(todc_info->seconds) & 0x7f) != sec) {
487 break;
491 sec = todc_read_timereg(todc_info->seconds) & 0x7f;
493 for (i = 0 ; i < 10000000 ; i++) { /* Should take 1 second */
494 freq = get_tbl();
496 if ((todc_read_timereg(todc_info->seconds) & 0x7f) != sec) {
497 break;
501 freq -= tbl;
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);
510 return;