Commit | Line | Data |
---|---|---|
b920de1b DH |
1 | /* MN10300 RTC management |
2 | * | |
3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
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. | |
10 | */ | |
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> | |
18 | #include <asm/rtc.h> | |
19 | ||
20 | DEFINE_SPINLOCK(rtc_lock); | |
21 | EXPORT_SYMBOL(rtc_lock); | |
22 | ||
23 | /* last time the RTC got updated */ | |
24 | static long last_rtc_update; | |
25 | ||
26 | /* time for RTC to update itself in ioclks */ | |
27 | static unsigned long mn10300_rtc_update_period; | |
28 | ||
f7a56575 | 29 | void read_persistent_clock(struct timespec *ts) |
b920de1b DH |
30 | { |
31 | struct rtc_time tm; | |
32 | ||
33 | get_rtc_time(&tm); | |
34 | ||
f7a56575 | 35 | ts->tv_sec = mktime(tm.tm_year, tm.tm_mon, tm.tm_mday, |
b920de1b | 36 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
f7a56575 | 37 | ts->tv_nsec = 0; |
b920de1b DH |
38 | } |
39 | ||
40 | /* | |
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 | |
45 | * sheet for details. | |
46 | * | |
47 | * BUG: This routine does not handle hour overflow properly; it just | |
48 | * sets the minutes. Usually you'll only notice that after reboot! | |
49 | */ | |
50 | static int set_rtc_mmss(unsigned long nowtime) | |
51 | { | |
52 | unsigned char save_control, save_freq_select; | |
53 | int retval = 0; | |
54 | int real_seconds, real_minutes, cmos_minutes; | |
55 | ||
56 | /* gets recalled with irq locally disabled */ | |
57 | spin_lock(&rtc_lock); | |
58 | save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being | |
59 | * set */ | |
60 | CMOS_WRITE(save_control | RTC_SET, RTC_CONTROL); | |
61 | ||
62 | save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset | |
63 | * prescaler */ | |
64 | CMOS_WRITE(save_freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT); | |
65 | ||
66 | cmos_minutes = CMOS_READ(RTC_MINUTES); | |
67 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) | |
f6a2298c | 68 | cmos_minutes = bcd2bin(cmos_minutes); |
b920de1b DH |
69 | |
70 | /* | |
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 | |
75 | */ | |
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 */ | |
80 | real_minutes += 30; | |
81 | real_minutes %= 60; | |
82 | ||
83 | if (abs(real_minutes - cmos_minutes) < 30) { | |
84 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { | |
f6a2298c AB |
85 | real_seconds = bin2bcd(real_seconds); |
86 | real_minutes = bin2bcd(real_minutes); | |
b920de1b DH |
87 | } |
88 | CMOS_WRITE(real_seconds, RTC_SECONDS); | |
89 | CMOS_WRITE(real_minutes, RTC_MINUTES); | |
90 | } else { | |
91 | printk(KERN_WARNING | |
92 | "set_rtc_mmss: can't update from %d to %d\n", | |
93 | cmos_minutes, real_minutes); | |
94 | retval = -1; | |
95 | } | |
96 | ||
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 | |
103 | */ | |
104 | CMOS_WRITE(save_control, RTC_CONTROL); | |
105 | CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); | |
106 | spin_unlock(&rtc_lock); | |
107 | ||
108 | return retval; | |
109 | } | |
110 | ||
f7a56575 | 111 | int update_persistent_clock(struct timespec now) |
b920de1b | 112 | { |
f7a56575 | 113 | return set_rtc_mms(now.tv_sec); |
b920de1b DH |
114 | } |
115 | ||
116 | /* | |
117 | * calibrate the TSC clock against the RTC | |
118 | */ | |
119 | void __init calibrate_clock(void) | |
120 | { | |
121 | unsigned long count0, counth, count1; | |
122 | unsigned char status; | |
123 | ||
124 | /* make sure the RTC is running and is set to operate in 24hr mode */ | |
125 | status = RTSRC; | |
126 | RTCRB |= RTCRB_SET; | |
127 | RTCRB |= RTCRB_TM_24HR; | |
128 | RTCRA |= RTCRA_DVR; | |
129 | RTCRA &= ~RTCRA_DVR; | |
130 | RTCRB &= ~RTCRB_SET; | |
131 | ||
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 | |
134 | * cycle (1 second) | |
135 | */ | |
136 | startup_timestamp_counter(); | |
137 | ||
138 | while (!(RTCRA & RTCRA_UIP)) {} | |
139 | while ((RTCRA & RTCRA_UIP)) {} | |
140 | ||
141 | count0 = TMTSCBC; | |
142 | ||
143 | while (!(RTCRA & RTCRA_UIP)) {} | |
144 | ||
145 | counth = TMTSCBC; | |
146 | ||
147 | while ((RTCRA & RTCRA_UIP)) {} | |
148 | ||
149 | count1 = TMTSCBC; | |
150 | ||
151 | shutdown_timestamp_counter(); | |
152 | ||
153 | MN10300_TSCCLK = count0 - count1; /* the timers count down */ | |
154 | mn10300_rtc_update_period = counth - count1; | |
155 | MN10300_TSC_PER_HZ = MN10300_TSCCLK / HZ; | |
156 | } |