2 * linux/arch/mips/dec/time.c
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
5 * Copyright (C) 2000, 2003 Maciej W. Rozycki
7 * This file contains the time handling details for PC-style clocks as
8 * found in some MIPS systems.
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/mc146818rtc.h>
16 #include <linux/param.h>
17 #include <linux/time.h>
19 #include <asm/processor.h>
22 #include <asm/dec/interrupts.h>
23 #include <asm/dec/ioasic.h>
24 #include <asm/dec/ioasic_addrs.h>
25 #include <asm/dec/machtype.h>
28 static unsigned long dec_rtc_get_time(void)
30 unsigned int year, mon, day, hour, min, sec, real_year;
33 /* The Linux interpretation of the DS1287 clock register contents:
34 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
35 * RTC registers show the second which has precisely just started.
36 * Let's hope other operating systems interpret the RTC the same way.
38 /* read RTC exactly on falling edge of update flag */
39 for (i = 0; i < 1000000; i++) /* may take up to 1 second... */
40 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
42 for (i = 0; i < 1000000; i++) /* must try at least 2.228 ms */
43 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
45 /* Isn't this overkill? UIP above should guarantee consistency */
47 sec = CMOS_READ(RTC_SECONDS);
48 min = CMOS_READ(RTC_MINUTES);
49 hour = CMOS_READ(RTC_HOURS);
50 day = CMOS_READ(RTC_DAY_OF_MONTH);
51 mon = CMOS_READ(RTC_MONTH);
52 year = CMOS_READ(RTC_YEAR);
53 } while (sec != CMOS_READ(RTC_SECONDS));
54 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
63 * The PROM will reset the year to either '72 or '73.
64 * Therefore we store the real year separately, in one
65 * of unused BBU RAM locations.
67 real_year = CMOS_READ(RTC_DEC_YEAR);
68 year += real_year - 72 + 2000;
70 return mktime(year, mon, day, hour, min, sec);
74 * In order to set the CMOS clock precisely, dec_rtc_set_mmss has to
75 * be called 500 ms after the second nowtime has started, because when
76 * nowtime is written into the registers of the CMOS clock, it will
77 * jump to the next second precisely 500 ms later. Check the Dallas
78 * DS1287 data sheet for details.
80 static int dec_rtc_set_mmss(unsigned long nowtime)
83 int real_seconds, real_minutes, cmos_minutes;
84 unsigned char save_control, save_freq_select;
86 /* tell the clock it's being set */
87 save_control = CMOS_READ(RTC_CONTROL);
88 CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
90 /* stop and reset prescaler */
91 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
92 CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
94 cmos_minutes = CMOS_READ(RTC_MINUTES);
95 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
96 BCD_TO_BIN(cmos_minutes);
99 * since we're only adjusting minutes and seconds,
100 * don't interfere with hour overflow. This avoids
101 * messing with unknown time zones but requires your
102 * RTC not to be off by more than 15 minutes
104 real_seconds = nowtime % 60;
105 real_minutes = nowtime / 60;
106 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
107 real_minutes += 30; /* correct for half hour time zone */
110 if (abs(real_minutes - cmos_minutes) < 30) {
111 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
112 BIN_TO_BCD(real_seconds);
113 BIN_TO_BCD(real_minutes);
115 CMOS_WRITE(real_seconds, RTC_SECONDS);
116 CMOS_WRITE(real_minutes, RTC_MINUTES);
119 "set_rtc_mmss: can't update from %d to %d\n",
120 cmos_minutes, real_minutes);
124 /* The following flags have to be released exactly in this order,
125 * otherwise the DS1287 will not reset the oscillator and will not
126 * update precisely 500 ms later. You won't find this mentioned
127 * in the Dallas Semiconductor data sheets, but who believes data
128 * sheets anyway ... -- Markus Kuhn
130 CMOS_WRITE(save_control, RTC_CONTROL);
131 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
137 static int dec_timer_state(void)
139 return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0;
142 static void dec_timer_ack(void)
144 CMOS_READ(RTC_REG_C); /* Ack the RTC interrupt. */
147 static unsigned int dec_ioasic_hpt_read(void)
150 * The free-running counter is 32-bit which is good for about
151 * 2 minutes, 50 seconds at possible count rates of up to 25MHz.
153 return ioasic_read(IO_REG_FCTR);
156 static void dec_ioasic_hpt_init(unsigned int count)
158 ioasic_write(IO_REG_FCTR, ioasic_read(IO_REG_FCTR) - count);
162 void __init dec_time_init(void)
164 rtc_get_time = dec_rtc_get_time;
165 rtc_set_mmss = dec_rtc_set_mmss;
167 mips_timer_state = dec_timer_state;
168 mips_timer_ack = dec_timer_ack;
170 if (!cpu_has_counter && IOASIC) {
171 /* For pre-R4k systems we use the I/O ASIC's counter. */
172 mips_hpt_read = dec_ioasic_hpt_read;
173 mips_hpt_init = dec_ioasic_hpt_init;
176 /* Set up the rate of periodic DS1287 interrupts. */
177 CMOS_WRITE(RTC_REF_CLCK_32KHZ | (16 - LOG_2_HZ), RTC_REG_A);
180 void __init dec_timer_setup(struct irqaction *irq)
182 setup_irq(dec_interrupt[DEC_IRQ_RTC], irq);
184 /* Enable periodic DS1287 interrupts. */
185 CMOS_WRITE(CMOS_READ(RTC_REG_B) | RTC_PIE, RTC_REG_B);