special usb hub handling, IDE disks, and retries all over the place
[linux-2.4.git] / include / asm-arm / arch-riscstation / time.h
1 /*
2  *  linux/include/asm-arm/arch-rs/time.h
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * (c) 2002 Simtec Electronics / Ben Dooks
9  *
10  * Bits taken from linux/include/asm-arm/arch-rpc/time.h
11  *
12  * (c) 1996-2002 Russell King
13  *
14  * Bits taken from linux/include/asm-arm/arch-ebsa285/time.h
15  *
16  * Copyright (C) 1998 Russell King.
17  * Copyright (C) 1998 Phil Blundell
18 */
19
20 #define RTC_PORT(x)    (0x70 + (x))
21 #define RTC_ALWAYS_BCD (0)
22
23 #include <linux/mc146818rtc.h>
24 #include <asm/mach-types.h>
25
26 extern void ioctime_init(void);
27
28 /* timer interrut - update things like profiling information and our
29  * copy of the RTC's time
30 */
31
32 static void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
33 {
34         do_timer(regs);
35         do_set_rtc();
36         do_profile(regs);
37 }
38
39 /* get_isa_cmos_time()
40  *
41  * get the time from the CMOS RTC
42  *
43  * from linux/include/asm-arm/arch-ebsa285/time.h
44 */
45
46 static unsigned long get_isa_cmos_time(void)
47 {
48         unsigned int year, mon, day, hour, min, sec;
49         int i;
50
51         // check to see if the RTC makes sense.....
52         if ((CMOS_READ(RTC_VALID) & RTC_VRT) == 0)
53                 return mktime(1970, 1, 1, 0, 0, 0);
54
55         /* The Linux interpretation of the CMOS clock register contents:
56          * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
57          * RTC registers show the second which has precisely just started.
58          * Let's hope other operating systems interpret the RTC the same way.
59          */
60         /* read RTC exactly on falling edge of update flag */
61         for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
62                 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
63                         break;
64
65         for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
66                 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
67                         break;
68
69         do { /* Isn't this overkill ? UIP above should guarantee consistency */
70                 sec  = CMOS_READ(RTC_SECONDS);
71                 min  = CMOS_READ(RTC_MINUTES);
72                 hour = CMOS_READ(RTC_HOURS);
73                 day  = CMOS_READ(RTC_DAY_OF_MONTH);
74                 mon  = CMOS_READ(RTC_MONTH);
75                 year = CMOS_READ(RTC_YEAR);
76         } while (sec != CMOS_READ(RTC_SECONDS));
77
78         if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
79                 BCD_TO_BIN(sec);
80                 BCD_TO_BIN(min);
81                 BCD_TO_BIN(hour);
82                 BCD_TO_BIN(day);
83                 BCD_TO_BIN(mon);
84                 BCD_TO_BIN(year);
85         }
86         if ((year += 1900) < 1970)
87                 year += 100;
88         return mktime(year, mon, day, hour, min, sec);
89 }
90
91 /* set_isa_cmos_time()
92  *
93  * set the CMOS RTC time
94  *
95  * from linux/include/asm-arm/arch-ebsa285/time.h
96 */
97
98 static int
99 set_isa_cmos_time(void)
100 {
101         int retval = 0;
102         int real_seconds, real_minutes, cmos_minutes;
103         unsigned char save_control, save_freq_select;
104         unsigned long nowtime = xtime.tv_sec;
105
106         save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
107         CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
108
109         save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
110         CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
111
112         cmos_minutes = CMOS_READ(RTC_MINUTES);
113         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
114                 BCD_TO_BIN(cmos_minutes);
115
116         /*
117          * since we're only adjusting minutes and seconds,
118          * don't interfere with hour overflow. This avoids
119          * messing with unknown time zones but requires your
120          * RTC not to be off by more than 15 minutes
121          */
122         real_seconds = nowtime % 60;
123         real_minutes = nowtime / 60;
124         if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
125                 real_minutes += 30;             /* correct for half hour time zone */
126         real_minutes %= 60;
127
128         if (abs(real_minutes - cmos_minutes) < 30) {
129                 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
130                         BIN_TO_BCD(real_seconds);
131                         BIN_TO_BCD(real_minutes);
132                 }
133                 CMOS_WRITE(real_seconds,RTC_SECONDS);
134                 CMOS_WRITE(real_minutes,RTC_MINUTES);
135         } else
136                 retval = -1;
137
138         /* The following flags have to be released exactly in this order,
139          * otherwise the DS12887 (popular MC146818A clone with integrated
140          * battery and quartz) will not reset the oscillator and will not
141          * update precisely 500 ms later. You won't find this mentioned in
142          * the Dallas Semiconductor data sheets, but who believes data
143          * sheets anyway ...                           -- Markus Kuhn
144          */
145         CMOS_WRITE(save_control, RTC_CONTROL);
146         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
147
148         return retval;
149 }
150
151
152 /*
153  * Set up timer interrupt.
154  */
155 static inline void setup_timer(void)
156 {
157         int reg_b, reg_d;
158
159         ioctime_init();
160
161         /* ensure we have an RTC chip, and initialise it... */
162
163         reg_d = CMOS_READ(RTC_REG_D);
164
165         CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_REG_A);
166
167         /*
168          * Set control reg B
169          *   (24 hour mode, update enabled)
170          */
171         reg_b = CMOS_READ(RTC_REG_B) & 0x7f;
172         reg_b |= 2;
173         CMOS_WRITE(reg_b, RTC_REG_B);
174
175         if ((CMOS_READ(RTC_REG_A) & 0x7f) == RTC_REF_CLCK_32KHZ &&
176             CMOS_READ(RTC_REG_B) == reg_b) {
177
178                 /*
179                  * We have a RTC.  Check the battery
180                  */
181                 if ((reg_d & 0x80) == 0)
182                         printk(KERN_WARNING "RTC: *** warning: CMOS battery bad\n");
183
184                 printk("RTC: detected\n");
185
186                 xtime.tv_sec = get_isa_cmos_time();
187                 set_rtc = set_isa_cmos_time;
188         } else {
189                 printk("RTC: Warning: No RTC detected\n");
190         }
191
192         /* ensure we have the IOC time interrupt setup */
193
194         timer_irq.handler = timer_interrupt;
195
196         setup_arm_irq(IRQ_TIMER, &timer_irq);
197 }
198