2 * linux/arch/m68k/atari/time.c
4 * Atari time and real time clock stuff
6 * Assembled of parts of former atari/config.c 97-12-18 by Roman Hodek
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive
13 #include <linux/types.h>
14 #include <linux/mc146818rtc.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <linux/rtc.h>
18 #include <linux/bcd.h>
19 #include <linux/clocksource.h>
20 #include <linux/delay.h>
21 #include <linux/export.h>
23 #include <asm/atariints.h>
24 #include <asm/machdep.h>
28 DEFINE_SPINLOCK(rtc_lock);
29 EXPORT_SYMBOL_GPL(rtc_lock);
31 static u64 atari_read_clk(struct clocksource *cs);
33 static struct clocksource atari_clk = {
36 .read = atari_read_clk,
37 .mask = CLOCKSOURCE_MASK(32),
38 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
42 static u8 last_timer_count;
44 static irqreturn_t mfp_timer_c_handler(int irq, void *dev_id)
48 local_irq_save(flags);
50 last_timer_count = st_mfp.tim_dt_c;
51 } while (last_timer_count == 1);
52 clk_total += INT_TICKS;
55 local_irq_restore(flags);
61 atari_sched_init(void)
63 /* set Timer C data Register */
64 st_mfp.tim_dt_c = INT_TICKS;
65 /* start timer C, div = 1:100 */
66 st_mfp.tim_ct_cd = (st_mfp.tim_ct_cd & 15) | 0x60;
67 /* install interrupt service routine for MFP Timer C */
68 if (request_irq(IRQ_MFP_TIMC, mfp_timer_c_handler, IRQF_TIMER, "timer",
70 pr_err("Couldn't register timer interrupt\n");
72 clocksource_register_hz(&atari_clk, INT_CLK);
75 /* ++andreas: gettimeoffset fixed to check for pending interrupt */
77 static u64 atari_read_clk(struct clocksource *cs)
83 local_irq_save(flags);
84 /* Ensure that the count is monotonically decreasing, even though
85 * the result may briefly stop changing after counter wrap-around.
87 count = min(st_mfp.tim_dt_c, last_timer_count);
88 last_timer_count = count;
90 ticks = INT_TICKS - count;
92 local_irq_restore(flags);
98 static void mste_read(struct MSTE_RTC *val)
100 #define COPY(v) val->v=(mste_rtc.v & 0xf)
102 COPY(sec_ones) ; COPY(sec_tens) ; COPY(min_ones) ;
103 COPY(min_tens) ; COPY(hr_ones) ; COPY(hr_tens) ;
104 COPY(weekday) ; COPY(day_ones) ; COPY(day_tens) ;
105 COPY(mon_ones) ; COPY(mon_tens) ; COPY(year_ones) ;
107 /* prevent from reading the clock while it changed */
108 } while (val->sec_ones != (mste_rtc.sec_ones & 0xf));
112 static void mste_write(struct MSTE_RTC *val)
114 #define COPY(v) mste_rtc.v=val->v
116 COPY(sec_ones) ; COPY(sec_tens) ; COPY(min_ones) ;
117 COPY(min_tens) ; COPY(hr_ones) ; COPY(hr_tens) ;
118 COPY(weekday) ; COPY(day_ones) ; COPY(day_tens) ;
119 COPY(mon_ones) ; COPY(mon_tens) ; COPY(year_ones) ;
121 /* prevent from writing the clock while it changed */
122 } while (val->sec_ones != (mste_rtc.sec_ones & 0xf));
126 #define RTC_READ(reg) \
127 ({ unsigned char __val; \
128 (void) atari_writeb(reg,&tt_rtc.regsel); \
129 __val = tt_rtc.data; \
133 #define RTC_WRITE(reg,val) \
135 atari_writeb(reg,&tt_rtc.regsel); \
136 tt_rtc.data = (val); \
140 #define HWCLK_POLL_INTERVAL 5
142 int atari_mste_hwclk( int op, struct rtc_time *t )
148 mste_rtc.mode=(mste_rtc.mode | 1);
149 hr24=mste_rtc.mon_tens & 1;
150 mste_rtc.mode=(mste_rtc.mode & ~1);
153 /* write: prepare values */
155 val.sec_ones = t->tm_sec % 10;
156 val.sec_tens = t->tm_sec / 10;
157 val.min_ones = t->tm_min % 10;
158 val.min_tens = t->tm_min / 10;
163 if (hour == 0 || hour == 20)
166 val.hr_ones = hour % 10;
167 val.hr_tens = hour / 10;
168 val.day_ones = t->tm_mday % 10;
169 val.day_tens = t->tm_mday / 10;
170 val.mon_ones = (t->tm_mon+1) % 10;
171 val.mon_tens = (t->tm_mon+1) / 10;
172 year = t->tm_year - 80;
173 val.year_ones = year % 10;
174 val.year_tens = year / 10;
175 val.weekday = t->tm_wday;
177 mste_rtc.mode=(mste_rtc.mode | 1);
178 val.year_ones = (year % 4); /* leap year register */
179 mste_rtc.mode=(mste_rtc.mode & ~1);
183 t->tm_sec = val.sec_ones + val.sec_tens * 10;
184 t->tm_min = val.min_ones + val.min_tens * 10;
185 hour = val.hr_ones + val.hr_tens * 10;
187 if (hour == 12 || hour == 12 + 20)
193 t->tm_mday = val.day_ones + val.day_tens * 10;
194 t->tm_mon = val.mon_ones + val.mon_tens * 10 - 1;
195 t->tm_year = val.year_ones + val.year_tens * 10 + 80;
196 t->tm_wday = val.weekday;
201 int atari_tt_hwclk( int op, struct rtc_time *t )
203 int sec=0, min=0, hour=0, day=0, mon=0, year=0, wday=0;
208 ctrl = RTC_READ(RTC_CONTROL); /* control registers are
209 * independent from the UIP */
212 /* write: prepare values */
219 year = t->tm_year - atari_rtc_year_offset;
220 wday = t->tm_wday + (t->tm_wday >= 0);
222 if (!(ctrl & RTC_24H)) {
232 if (!(ctrl & RTC_DM_BINARY)) {
235 hour = bin2bcd(hour);
238 year = bin2bcd(year);
240 wday = bin2bcd(wday);
244 /* Reading/writing the clock registers is a bit critical due to
245 * the regular update cycle of the RTC. While an update is in
246 * progress, registers 0..9 shouldn't be touched.
247 * The problem is solved like that: If an update is currently in
248 * progress (the UIP bit is set), the process sleeps for a while
249 * (50ms). This really should be enough, since the update cycle
250 * normally needs 2 ms.
251 * If the UIP bit reads as 0, we have at least 244 usecs until the
252 * update starts. This should be enough... But to be sure,
253 * additionally the RTC_SET bit is set to prevent an update cycle.
256 while( RTC_READ(RTC_FREQ_SELECT) & RTC_UIP ) {
257 if (in_atomic() || irqs_disabled())
260 schedule_timeout_interruptible(HWCLK_POLL_INTERVAL);
263 local_irq_save(flags);
264 RTC_WRITE( RTC_CONTROL, ctrl | RTC_SET );
266 sec = RTC_READ( RTC_SECONDS );
267 min = RTC_READ( RTC_MINUTES );
268 hour = RTC_READ( RTC_HOURS );
269 day = RTC_READ( RTC_DAY_OF_MONTH );
270 mon = RTC_READ( RTC_MONTH );
271 year = RTC_READ( RTC_YEAR );
272 wday = RTC_READ( RTC_DAY_OF_WEEK );
275 RTC_WRITE( RTC_SECONDS, sec );
276 RTC_WRITE( RTC_MINUTES, min );
277 RTC_WRITE( RTC_HOURS, hour + pm);
278 RTC_WRITE( RTC_DAY_OF_MONTH, day );
279 RTC_WRITE( RTC_MONTH, mon );
280 RTC_WRITE( RTC_YEAR, year );
281 if (wday >= 0) RTC_WRITE( RTC_DAY_OF_WEEK, wday );
283 RTC_WRITE( RTC_CONTROL, ctrl & ~RTC_SET );
284 local_irq_restore(flags);
287 /* read: adjust values */
294 if (!(ctrl & RTC_DM_BINARY)) {
297 hour = bcd2bin(hour);
300 year = bcd2bin(year);
301 wday = bcd2bin(wday);
304 if (!(ctrl & RTC_24H)) {
305 if (!pm && hour == 12)
307 else if (pm && hour != 12)
316 t->tm_year = year + atari_rtc_year_offset;
317 t->tm_wday = wday - 1;