2 * linux/include/asm-arm/arch-rs/time.h
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.
8 * (c) 2002 Simtec Electronics / Ben Dooks
10 * Bits taken from linux/include/asm-arm/arch-rpc/time.h
12 * (c) 1996-2002 Russell King
14 * Bits taken from linux/include/asm-arm/arch-ebsa285/time.h
16 * Copyright (C) 1998 Russell King.
17 * Copyright (C) 1998 Phil Blundell
20 #define RTC_PORT(x) (0x70 + (x))
21 #define RTC_ALWAYS_BCD (0)
23 #include <linux/mc146818rtc.h>
24 #include <asm/mach-types.h>
26 extern void ioctime_init(void);
28 /* timer interrut - update things like profiling information and our
29 * copy of the RTC's time
32 static void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
39 /* get_isa_cmos_time()
41 * get the time from the CMOS RTC
43 * from linux/include/asm-arm/arch-ebsa285/time.h
46 static unsigned long get_isa_cmos_time(void)
48 unsigned int year, mon, day, hour, min, sec;
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);
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.
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)
65 for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
66 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
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));
78 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
86 if ((year += 1900) < 1970)
88 return mktime(year, mon, day, hour, min, sec);
91 /* set_isa_cmos_time()
93 * set the CMOS RTC time
95 * from linux/include/asm-arm/arch-ebsa285/time.h
99 set_isa_cmos_time(void)
102 int real_seconds, real_minutes, cmos_minutes;
103 unsigned char save_control, save_freq_select;
104 unsigned long nowtime = xtime.tv_sec;
106 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
107 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
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);
112 cmos_minutes = CMOS_READ(RTC_MINUTES);
113 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
114 BCD_TO_BIN(cmos_minutes);
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
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 */
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);
133 CMOS_WRITE(real_seconds,RTC_SECONDS);
134 CMOS_WRITE(real_minutes,RTC_MINUTES);
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
145 CMOS_WRITE(save_control, RTC_CONTROL);
146 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
153 * Set up timer interrupt.
155 static inline void setup_timer(void)
161 /* ensure we have an RTC chip, and initialise it... */
163 reg_d = CMOS_READ(RTC_REG_D);
165 CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_REG_A);
169 * (24 hour mode, update enabled)
171 reg_b = CMOS_READ(RTC_REG_B) & 0x7f;
173 CMOS_WRITE(reg_b, RTC_REG_B);
175 if ((CMOS_READ(RTC_REG_A) & 0x7f) == RTC_REF_CLCK_32KHZ &&
176 CMOS_READ(RTC_REG_B) == reg_b) {
179 * We have a RTC. Check the battery
181 if ((reg_d & 0x80) == 0)
182 printk(KERN_WARNING "RTC: *** warning: CMOS battery bad\n");
184 printk("RTC: detected\n");
186 xtime.tv_sec = get_isa_cmos_time();
187 set_rtc = set_isa_cmos_time;
189 printk("RTC: Warning: No RTC detected\n");
192 /* ensure we have the IOC time interrupt setup */
194 timer_irq.handler = timer_interrupt;
196 setup_arm_irq(IRQ_TIMER, &timer_irq);