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
;
30 * read the current RTC time
32 unsigned long __init
get_initial_rtc_time(void)
38 return mktime(tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
39 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
43 * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
44 * ms after the second nowtime has started, because when nowtime is written
45 * into the registers of the CMOS clock, it will jump to the next second
46 * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
49 * BUG: This routine does not handle hour overflow properly; it just
50 * sets the minutes. Usually you'll only notice that after reboot!
52 static int set_rtc_mmss(unsigned long nowtime
)
54 unsigned char save_control
, save_freq_select
;
56 int real_seconds
, real_minutes
, cmos_minutes
;
58 /* gets recalled with irq locally disabled */
60 save_control
= CMOS_READ(RTC_CONTROL
); /* tell the clock it's being
62 CMOS_WRITE(save_control
| RTC_SET
, RTC_CONTROL
);
64 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
); /* stop and reset
66 CMOS_WRITE(save_freq_select
| RTC_DIV_RESET2
, RTC_FREQ_SELECT
);
68 cmos_minutes
= CMOS_READ(RTC_MINUTES
);
69 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
70 cmos_minutes
= bcd2bin(cmos_minutes
);
73 * since we're only adjusting minutes and seconds,
74 * don't interfere with hour overflow. This avoids
75 * messing with unknown time zones but requires your
76 * RTC not to be off by more than 15 minutes
78 real_seconds
= nowtime
% 60;
79 real_minutes
= nowtime
/ 60;
80 if (((abs(real_minutes
- cmos_minutes
) + 15) / 30) & 1)
81 /* correct for half hour time zone */
85 if (abs(real_minutes
- cmos_minutes
) < 30) {
86 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
87 real_seconds
= bin2bcd(real_seconds
);
88 real_minutes
= bin2bcd(real_minutes
);
90 CMOS_WRITE(real_seconds
, RTC_SECONDS
);
91 CMOS_WRITE(real_minutes
, RTC_MINUTES
);
94 "set_rtc_mmss: can't update from %d to %d\n",
95 cmos_minutes
, real_minutes
);
99 /* The following flags have to be released exactly in this order,
100 * otherwise the DS12887 (popular MC146818A clone with integrated
101 * battery and quartz) will not reset the oscillator and will not
102 * update precisely 500 ms later. You won't find this mentioned in
103 * the Dallas Semiconductor data sheets, but who believes data
104 * sheets anyway ... -- Markus Kuhn
106 CMOS_WRITE(save_control
, RTC_CONTROL
);
107 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
108 spin_unlock(&rtc_lock
);
113 void check_rtc_time(void)
115 /* the RTC clock just finished ticking over again this second
116 * - if we have an externally synchronized Linux clock, then update
117 * RTC clock accordingly every ~11 minutes. set_rtc_mmss() has to be
118 * called as close as possible to 500 ms before the new second starts.
120 if ((time_status
& STA_UNSYNC
) == 0 &&
121 xtime
.tv_sec
> last_rtc_update
+ 660 &&
122 xtime
.tv_nsec
/ 1000 >= 500000 - ((unsigned) TICK_SIZE
) / 2 &&
123 xtime
.tv_nsec
/ 1000 <= 500000 + ((unsigned) TICK_SIZE
) / 2
125 if (set_rtc_mmss(xtime
.tv_sec
) == 0)
126 last_rtc_update
= xtime
.tv_sec
;
128 /* do it again in 60s */
129 last_rtc_update
= xtime
.tv_sec
- 600;
134 * calibrate the TSC clock against the RTC
136 void __init
calibrate_clock(void)
138 unsigned long count0
, counth
, count1
;
139 unsigned char status
;
141 /* make sure the RTC is running and is set to operate in 24hr mode */
144 RTCRB
|= RTCRB_TM_24HR
;
149 /* work out the clock speed by counting clock cycles between ends of
150 * the RTC update cycle - track the RTC through one complete update
153 startup_timestamp_counter();
155 while (!(RTCRA
& RTCRA_UIP
)) {}
156 while ((RTCRA
& RTCRA_UIP
)) {}
160 while (!(RTCRA
& RTCRA_UIP
)) {}
164 while ((RTCRA
& RTCRA_UIP
)) {}
168 shutdown_timestamp_counter();
170 MN10300_TSCCLK
= count0
- count1
; /* the timers count down */
171 mn10300_rtc_update_period
= counth
- count1
;
172 MN10300_TSC_PER_HZ
= MN10300_TSCCLK
/ HZ
;