ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-arm / arch-ebsa285 / time.h
1 /*
2  *  linux/include/asm-arm/arch-ebsa285/time.h
3  *
4  *  Copyright (C) 1998 Russell King.
5  *  Copyright (C) 1998 Phil Blundell
6  *
7  * CATS has a real-time clock, though the evaluation board doesn't.
8  *
9  * Changelog:
10  *  21-Mar-1998 RMK     Created
11  *  27-Aug-1998 PJB     CATS support
12  *  28-Dec-1998 APH     Made leds optional
13  *  20-Jan-1999 RMK     Started merge of EBSA285, CATS and NetWinder
14  *  16-Mar-1999 RMK     More support for EBSA285-like machines with RTCs in
15  */
16
17 #define RTC_PORT(x)             (rtc_base+(x))
18 #define RTC_ALWAYS_BCD          0
19
20 #include <linux/mc146818rtc.h>
21 #include <linux/bcd.h>
22
23 #include <asm/hardware/dec21285.h>
24 #include <asm/leds.h>
25 #include <asm/mach-types.h>
26
27 static int rtc_base;
28
29 #define mSEC_10_from_14 ((14318180 + 100) / 200)
30
31 static unsigned long isa_gettimeoffset(void)
32 {
33         int count;
34
35         static int count_p = (mSEC_10_from_14/6);    /* for the first call after boot */
36         static unsigned long jiffies_p = 0;
37
38         /*
39          * cache volatile jiffies temporarily; we have IRQs turned off. 
40          */
41         unsigned long jiffies_t;
42
43         /* timer count may underflow right here */
44         outb_p(0x00, 0x43);     /* latch the count ASAP */
45
46         count = inb_p(0x40);    /* read the latched count */
47
48         /*
49          * We do this guaranteed double memory access instead of a _p 
50          * postfix in the previous port access. Wheee, hackady hack
51          */
52         jiffies_t = jiffies;
53
54         count |= inb_p(0x40) << 8;
55
56         /* Detect timer underflows.  If we haven't had a timer tick since 
57            the last time we were called, and time is apparently going
58            backwards, the counter must have wrapped during this routine. */
59         if ((jiffies_t == jiffies_p) && (count > count_p))
60                 count -= (mSEC_10_from_14/6);
61         else
62                 jiffies_p = jiffies_t;
63
64         count_p = count;
65
66         count = (((mSEC_10_from_14/6)-1) - count) * (tick_nsec / 1000);
67         count = (count + (mSEC_10_from_14/6)/2) / (mSEC_10_from_14/6);
68
69         return count;
70 }
71
72 static irqreturn_t
73 isa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
74 {
75         if (machine_is_netwinder())
76                 do_leds();
77
78         do_timer(regs);
79         do_set_rtc();
80         do_profile(regs);
81
82         return IRQ_HANDLED;
83 }
84
85 static unsigned long __init get_isa_cmos_time(void)
86 {
87         unsigned int year, mon, day, hour, min, sec;
88         int i;
89
90         // check to see if the RTC makes sense.....
91         if ((CMOS_READ(RTC_VALID) & RTC_VRT) == 0)
92                 return mktime(1970, 1, 1, 0, 0, 0);
93
94         /* The Linux interpretation of the CMOS clock register contents:
95          * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
96          * RTC registers show the second which has precisely just started.
97          * Let's hope other operating systems interpret the RTC the same way.
98          */
99         /* read RTC exactly on falling edge of update flag */
100         for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
101                 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
102                         break;
103
104         for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
105                 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
106                         break;
107
108         do { /* Isn't this overkill ? UIP above should guarantee consistency */
109                 sec  = CMOS_READ(RTC_SECONDS);
110                 min  = CMOS_READ(RTC_MINUTES);
111                 hour = CMOS_READ(RTC_HOURS);
112                 day  = CMOS_READ(RTC_DAY_OF_MONTH);
113                 mon  = CMOS_READ(RTC_MONTH);
114                 year = CMOS_READ(RTC_YEAR);
115         } while (sec != CMOS_READ(RTC_SECONDS));
116
117         if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
118                 BCD_TO_BIN(sec);
119                 BCD_TO_BIN(min);
120                 BCD_TO_BIN(hour);
121                 BCD_TO_BIN(day);
122                 BCD_TO_BIN(mon);
123                 BCD_TO_BIN(year);
124         }
125         if ((year += 1900) < 1970)
126                 year += 100;
127         return mktime(year, mon, day, hour, min, sec);
128 }
129
130 static int
131 set_isa_cmos_time(void)
132 {
133         int retval = 0;
134         int real_seconds, real_minutes, cmos_minutes;
135         unsigned char save_control, save_freq_select;
136         unsigned long nowtime = xtime.tv_sec;
137
138         save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
139         CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
140
141         save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
142         CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
143
144         cmos_minutes = CMOS_READ(RTC_MINUTES);
145         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
146                 BCD_TO_BIN(cmos_minutes);
147
148         /*
149          * since we're only adjusting minutes and seconds,
150          * don't interfere with hour overflow. This avoids
151          * messing with unknown time zones but requires your
152          * RTC not to be off by more than 15 minutes
153          */
154         real_seconds = nowtime % 60;
155         real_minutes = nowtime / 60;
156         if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
157                 real_minutes += 30;             /* correct for half hour time zone */
158         real_minutes %= 60;
159
160         if (abs(real_minutes - cmos_minutes) < 30) {
161                 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
162                         BIN_TO_BCD(real_seconds);
163                         BIN_TO_BCD(real_minutes);
164                 }
165                 CMOS_WRITE(real_seconds,RTC_SECONDS);
166                 CMOS_WRITE(real_minutes,RTC_MINUTES);
167         } else
168                 retval = -1;
169
170         /* The following flags have to be released exactly in this order,
171          * otherwise the DS12887 (popular MC146818A clone with integrated
172          * battery and quartz) will not reset the oscillator and will not
173          * update precisely 500 ms later. You won't find this mentioned in
174          * the Dallas Semiconductor data sheets, but who believes data
175          * sheets anyway ...                           -- Markus Kuhn
176          */
177         CMOS_WRITE(save_control, RTC_CONTROL);
178         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
179
180         return retval;
181 }
182
183
184 static unsigned long timer1_latch;
185
186 static unsigned long timer1_gettimeoffset (void)
187 {
188         unsigned long value = timer1_latch - *CSR_TIMER1_VALUE;
189
190         return ((tick_nsec / 1000) * value) / timer1_latch;
191 }
192
193 static irqreturn_t
194 timer1_interrupt(int irq, void *dev_id, struct pt_regs *regs)
195 {
196         *CSR_TIMER1_CLR = 0;
197
198         /* Do the LEDs things */
199         do_leds();
200         do_timer(regs);
201         do_set_rtc();
202         do_profile(regs);
203
204         return IRQ_HANDLED;
205 }
206
207 /*
208  * Set up timer interrupt.
209  */
210 void __init time_init(void)
211 {
212         int irq;
213
214         if (machine_is_co285() ||
215             machine_is_personal_server())
216                 /*
217                  * Add-in 21285s shouldn't access the RTC
218                  */
219                 rtc_base = 0;
220         else
221                 rtc_base = 0x70;
222
223         if (rtc_base) {
224                 int reg_d, reg_b;
225
226                 /*
227                  * Probe for the RTC.
228                  */
229                 reg_d = CMOS_READ(RTC_REG_D);
230
231                 /*
232                  * make sure the divider is set
233                  */
234                 CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_REG_A);
235
236                 /*
237                  * Set control reg B
238                  *   (24 hour mode, update enabled)
239                  */
240                 reg_b = CMOS_READ(RTC_REG_B) & 0x7f;
241                 reg_b |= 2;
242                 CMOS_WRITE(reg_b, RTC_REG_B);
243
244                 if ((CMOS_READ(RTC_REG_A) & 0x7f) == RTC_REF_CLCK_32KHZ &&
245                     CMOS_READ(RTC_REG_B) == reg_b) {
246                         struct timespec tv;
247
248                         /*
249                          * We have a RTC.  Check the battery
250                          */
251                         if ((reg_d & 0x80) == 0)
252                                 printk(KERN_WARNING "RTC: *** warning: CMOS battery bad\n");
253
254                         tv.tv_nsec = 0;
255                         tv.tv_sec = get_isa_cmos_time();
256                         do_settimeofday(&tv);
257                         set_rtc = set_isa_cmos_time;
258                 } else
259                         rtc_base = 0;
260         }
261
262         if (machine_is_ebsa285() ||
263             machine_is_co285() ||
264             machine_is_personal_server()) {
265                 gettimeoffset = timer1_gettimeoffset;
266
267                 timer1_latch = (mem_fclk_21285 + 8 * HZ) / (16 * HZ);
268
269                 *CSR_TIMER1_CLR  = 0;
270                 *CSR_TIMER1_LOAD = timer1_latch;
271                 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_AUTORELOAD | TIMER_CNTL_DIV16;
272
273                 timer_irq.handler = timer1_interrupt;
274                 irq = IRQ_TIMER1;
275         } else {
276                 /* enable PIT timer */
277                 /* set for periodic (4) and LSB/MSB write (0x30) */
278                 outb(0x34, 0x43);
279                 outb((mSEC_10_from_14/6) & 0xFF, 0x40);
280                 outb((mSEC_10_from_14/6) >> 8, 0x40);
281
282                 gettimeoffset = isa_gettimeoffset;
283                 timer_irq.handler = isa_timer_interrupt;
284                 irq = IRQ_ISA_TIMER;
285         }
286         setup_irq(irq, &timer_irq);
287 }