1 /* MN10300 RTC management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/mc146818rtc.h>
15 #include <linux/bcd.h>
16 #include <linux/timex.h>
17 #include <asm/rtc-regs.h>
20 DEFINE_SPINLOCK(rtc_lock);
21 EXPORT_SYMBOL(rtc_lock);
23 /* last time the RTC got updated */
24 static long last_rtc_update;
26 /* time for RTC to update itself in ioclks */
27 static unsigned long mn10300_rtc_update_period;
29 void read_persistent_clock(struct timespec *ts)
35 ts->tv_sec = mktime(tm.tm_year, tm.tm_mon, tm.tm_mday,
36 tm.tm_hour, tm.tm_min, tm.tm_sec);
41 * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
42 * ms after the second nowtime has started, because when nowtime is written
43 * into the registers of the CMOS clock, it will jump to the next second
44 * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
47 * BUG: This routine does not handle hour overflow properly; it just
48 * sets the minutes. Usually you'll only notice that after reboot!
50 static int set_rtc_mmss(unsigned long nowtime)
52 unsigned char save_control, save_freq_select;
54 int real_seconds, real_minutes, cmos_minutes;
56 /* gets recalled with irq locally disabled */
58 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being
60 CMOS_WRITE(save_control | RTC_SET, RTC_CONTROL);
62 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset
64 CMOS_WRITE(save_freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
66 cmos_minutes = CMOS_READ(RTC_MINUTES);
67 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
68 cmos_minutes = bcd2bin(cmos_minutes);
71 * since we're only adjusting minutes and seconds,
72 * don't interfere with hour overflow. This avoids
73 * messing with unknown time zones but requires your
74 * RTC not to be off by more than 15 minutes
76 real_seconds = nowtime % 60;
77 real_minutes = nowtime / 60;
78 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
79 /* correct for half hour time zone */
83 if (abs(real_minutes - cmos_minutes) < 30) {
84 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
85 real_seconds = bin2bcd(real_seconds);
86 real_minutes = bin2bcd(real_minutes);
88 CMOS_WRITE(real_seconds, RTC_SECONDS);
89 CMOS_WRITE(real_minutes, RTC_MINUTES);
92 "set_rtc_mmss: can't update from %d to %d\n",
93 cmos_minutes, real_minutes);
97 /* The following flags have to be released exactly in this order,
98 * otherwise the DS12887 (popular MC146818A clone with integrated
99 * battery and quartz) will not reset the oscillator and will not
100 * update precisely 500 ms later. You won't find this mentioned in
101 * the Dallas Semiconductor data sheets, but who believes data
102 * sheets anyway ... -- Markus Kuhn
104 CMOS_WRITE(save_control, RTC_CONTROL);
105 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
106 spin_unlock(&rtc_lock);
111 int update_persistent_clock(struct timespec now)
113 return set_rtc_mms(now.tv_sec);
117 * calibrate the TSC clock against the RTC
119 void __init calibrate_clock(void)
121 unsigned long count0, counth, count1;
122 unsigned char status;
124 /* make sure the RTC is running and is set to operate in 24hr mode */
127 RTCRB |= RTCRB_TM_24HR;
132 /* work out the clock speed by counting clock cycles between ends of
133 * the RTC update cycle - track the RTC through one complete update
136 startup_timestamp_counter();
138 while (!(RTCRA & RTCRA_UIP)) {}
139 while ((RTCRA & RTCRA_UIP)) {}
143 while (!(RTCRA & RTCRA_UIP)) {}
147 while ((RTCRA & RTCRA_UIP)) {}
151 shutdown_timestamp_counter();
153 MN10300_TSCCLK = count0 - count1; /* the timers count down */
154 mn10300_rtc_update_period = counth - count1;
155 MN10300_TSC_PER_HZ = MN10300_TSCCLK / HZ;