2 * linux/include/asm-arm/arch-ebsa285/time.h
4 * Copyright (C) 1998 Russell King.
5 * Copyright (C) 1998 Phil Blundell
7 * CATS has a real-time clock, though the evaluation board doesn't.
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
17 #define RTC_PORT(x) (rtc_base+(x))
18 #define RTC_ALWAYS_BCD 0
20 #include <linux/mc146818rtc.h>
21 #include <linux/bcd.h>
23 #include <asm/hardware/dec21285.h>
25 #include <asm/mach-types.h>
29 #define mSEC_10_from_14 ((14318180 + 100) / 200)
31 static unsigned long isa_gettimeoffset(void)
35 static int count_p = (mSEC_10_from_14/6); /* for the first call after boot */
36 static unsigned long jiffies_p = 0;
39 * cache volatile jiffies temporarily; we have IRQs turned off.
41 unsigned long jiffies_t;
43 /* timer count may underflow right here */
44 outb_p(0x00, 0x43); /* latch the count ASAP */
46 count = inb_p(0x40); /* read the latched count */
49 * We do this guaranteed double memory access instead of a _p
50 * postfix in the previous port access. Wheee, hackady hack
54 count |= inb_p(0x40) << 8;
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);
62 jiffies_p = jiffies_t;
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);
73 isa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
75 if (machine_is_netwinder())
85 static unsigned long __init get_isa_cmos_time(void)
87 unsigned int year, mon, day, hour, min, sec;
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);
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.
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)
104 for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
105 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
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));
117 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
125 if ((year += 1900) < 1970)
127 return mktime(year, mon, day, hour, min, sec);
131 set_isa_cmos_time(void)
134 int real_seconds, real_minutes, cmos_minutes;
135 unsigned char save_control, save_freq_select;
136 unsigned long nowtime = xtime.tv_sec;
138 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
139 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
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);
144 cmos_minutes = CMOS_READ(RTC_MINUTES);
145 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
146 BCD_TO_BIN(cmos_minutes);
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
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 */
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);
165 CMOS_WRITE(real_seconds,RTC_SECONDS);
166 CMOS_WRITE(real_minutes,RTC_MINUTES);
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
177 CMOS_WRITE(save_control, RTC_CONTROL);
178 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
184 static unsigned long timer1_latch;
186 static unsigned long timer1_gettimeoffset (void)
188 unsigned long value = timer1_latch - *CSR_TIMER1_VALUE;
190 return ((tick_nsec / 1000) * value) / timer1_latch;
194 timer1_interrupt(int irq, void *dev_id, struct pt_regs *regs)
198 /* Do the LEDs things */
208 * Set up timer interrupt.
210 void __init time_init(void)
214 if (machine_is_co285() ||
215 machine_is_personal_server())
217 * Add-in 21285s shouldn't access the RTC
229 reg_d = CMOS_READ(RTC_REG_D);
232 * make sure the divider is set
234 CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_REG_A);
238 * (24 hour mode, update enabled)
240 reg_b = CMOS_READ(RTC_REG_B) & 0x7f;
242 CMOS_WRITE(reg_b, RTC_REG_B);
244 if ((CMOS_READ(RTC_REG_A) & 0x7f) == RTC_REF_CLCK_32KHZ &&
245 CMOS_READ(RTC_REG_B) == reg_b) {
249 * We have a RTC. Check the battery
251 if ((reg_d & 0x80) == 0)
252 printk(KERN_WARNING "RTC: *** warning: CMOS battery bad\n");
255 tv.tv_sec = get_isa_cmos_time();
256 do_settimeofday(&tv);
257 set_rtc = set_isa_cmos_time;
262 if (machine_is_ebsa285() ||
263 machine_is_co285() ||
264 machine_is_personal_server()) {
265 gettimeoffset = timer1_gettimeoffset;
267 timer1_latch = (mem_fclk_21285 + 8 * HZ) / (16 * HZ);
270 *CSR_TIMER1_LOAD = timer1_latch;
271 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_AUTORELOAD | TIMER_CNTL_DIV16;
273 timer_irq.handler = timer1_interrupt;
276 /* enable PIT timer */
277 /* set for periodic (4) and LSB/MSB write (0x30) */
279 outb((mSEC_10_from_14/6) & 0xFF, 0x40);
280 outb((mSEC_10_from_14/6) >> 8, 0x40);
282 gettimeoffset = isa_gettimeoffset;
283 timer_irq.handler = isa_timer_interrupt;
286 setup_irq(irq, &timer_irq);