import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / arch / mips / dec / time.c
1 /*
2  *  linux/arch/mips/dec/time.c
3  *
4  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
5  *  Copyright (C) 2000, 2003  Maciej W. Rozycki
6  *
7  * This file contains the time handling details for PC-style clocks as
8  * found in some MIPS systems.
9  *
10  */
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>
18
19 #include <asm/processor.h>
20 #include <asm/time.h>
21
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>
26
27
28 static unsigned long dec_rtc_get_time(void)
29 {
30         unsigned int year, mon, day, hour, min, sec, real_year;
31         int i;
32
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.
37          */
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)
41                         break;
42         for (i = 0; i < 1000000; i++)   /* must try at least 2.228 ms */
43                 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
44                         break;
45         /* Isn't this overkill?  UIP above should guarantee consistency */
46         do {
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) {
55                 BCD_TO_BIN(sec);
56                 BCD_TO_BIN(min);
57                 BCD_TO_BIN(hour);
58                 BCD_TO_BIN(day);
59                 BCD_TO_BIN(mon);
60                 BCD_TO_BIN(year);
61         }
62         /*
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.
66          */
67         real_year = CMOS_READ(RTC_DEC_YEAR);
68         year += real_year - 72 + 2000;
69
70         return mktime(year, mon, day, hour, min, sec);
71 }
72
73 /*
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.
79  */
80 static int dec_rtc_set_mmss(unsigned long nowtime)
81 {
82         int retval = 0;
83         int real_seconds, real_minutes, cmos_minutes;
84         unsigned char save_control, save_freq_select;
85
86         /* tell the clock it's being set */
87         save_control = CMOS_READ(RTC_CONTROL);
88         CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
89
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);
93
94         cmos_minutes = CMOS_READ(RTC_MINUTES);
95         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
96                 BCD_TO_BIN(cmos_minutes);
97
98         /*
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
103          */
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 */
108         real_minutes %= 60;
109
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);
114                 }
115                 CMOS_WRITE(real_seconds, RTC_SECONDS);
116                 CMOS_WRITE(real_minutes, RTC_MINUTES);
117         } else {
118                 printk(KERN_WARNING
119                        "set_rtc_mmss: can't update from %d to %d\n",
120                        cmos_minutes, real_minutes);
121                 retval = -1;
122         }
123
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
129          */
130         CMOS_WRITE(save_control, RTC_CONTROL);
131         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
132
133         return retval;
134 }
135
136
137 static int dec_timer_state(void)
138 {
139         return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0;
140 }
141
142 static void dec_timer_ack(void)
143 {
144         CMOS_READ(RTC_REG_C);                   /* Ack the RTC interrupt.  */
145 }
146
147 static unsigned int dec_ioasic_hpt_read(void)
148 {
149         /*
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.
152          */
153         return ioasic_read(IO_REG_FCTR);
154 }
155
156 static void dec_ioasic_hpt_init(unsigned int count)
157 {
158         ioasic_write(IO_REG_FCTR, ioasic_read(IO_REG_FCTR) - count);
159 }
160
161
162 void __init dec_time_init(void)
163 {
164         rtc_get_time = dec_rtc_get_time;
165         rtc_set_mmss = dec_rtc_set_mmss;
166
167         mips_timer_state = dec_timer_state;
168         mips_timer_ack = dec_timer_ack;
169
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;
174         }
175
176         /* Set up the rate of periodic DS1287 interrupts.  */
177         CMOS_WRITE(RTC_REF_CLCK_32KHZ | (16 - LOG_2_HZ), RTC_REG_A);
178 }
179
180 void __init dec_timer_setup(struct irqaction *irq)
181 {
182         setup_irq(dec_interrupt[DEC_IRQ_RTC], irq);
183
184         /* Enable periodic DS1287 interrupts.  */
185         CMOS_WRITE(CMOS_READ(RTC_REG_B) | RTC_PIE, RTC_REG_B);
186 }