mm58167.c revision 1.12 1 /* $NetBSD: mm58167.c,v 1.12 2009/12/11 11:07:04 tsutsui Exp $ */
2
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthew Fredette.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * National Semiconductor MM58167 time-of-day chip subroutines.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: mm58167.c,v 1.12 2009/12/11 11:07:04 tsutsui Exp $");
38
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/device.h>
44
45 #include <sys/bus.h>
46 #include <dev/clock_subr.h>
47 #include <dev/ic/mm58167var.h>
48
49 int mm58167_gettime(todr_chip_handle_t, volatile struct timeval *);
50 int mm58167_settime(todr_chip_handle_t, volatile struct timeval *);
51
52 /*
53 * To quote SunOS's todreg.h:
54 * "This brain damaged chip insists on keeping the time in
55 * MM/DD HH:MM:SS format, even though it doesn't know about
56 * leap years and Feb. 29, thus making it nearly worthless."
57 */
58 #define mm58167_read(sc, r) \
59 bus_space_read_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r)
60 #define mm58167_write(sc, r, v) \
61 bus_space_write_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r, v)
62
63 todr_chip_handle_t
64 mm58167_attach(struct mm58167_softc *sc)
65 {
66 struct todr_chip_handle *handle;
67
68 aprint_normal(": mm58167");
69
70 handle = &sc->_mm58167_todr_handle;
71 memset(handle, 0, sizeof(handle));
72 handle->cookie = sc;
73 handle->todr_gettime = mm58167_gettime;
74 handle->todr_settime = mm58167_settime;
75 return handle;
76 }
77
78 /*
79 * Set up the system's time, given a `reasonable' time value.
80 */
81 int
82 mm58167_gettime(todr_chip_handle_t handle, volatile struct timeval *tv)
83 {
84 struct mm58167_softc *sc = handle->cookie;
85 struct clock_ymdhms dt_hardware;
86 struct clock_ymdhms dt_reasonable;
87 struct timeval now;
88 int s;
89 uint8_t byte_value;
90 int leap_year, had_leap_day;
91
92 /* First, read the date out of the chip. */
93
94 /* No interrupts while we're in the chip. */
95 s = splhigh();
96
97 /* Reset the status bit: */
98 byte_value = mm58167_read(sc, mm58167_status);
99
100 /*
101 * Read the date values until we get a coherent read (one
102 * where the status stays zero, indicating no increment was
103 * rippling through while we were reading).
104 */
105 do {
106 #define _MM58167_GET(dt_f, mm_f) \
107 byte_value = mm58167_read(sc, mm_f); \
108 dt_hardware.dt_f = FROMBCD(byte_value)
109
110 _MM58167_GET(dt_mon, mm58167_mon);
111 _MM58167_GET(dt_day, mm58167_day);
112 _MM58167_GET(dt_hour, mm58167_hour);
113 _MM58167_GET(dt_min, mm58167_min);
114 _MM58167_GET(dt_sec, mm58167_sec);
115 #undef _MM58167_GET
116 } while ((mm58167_read(sc, mm58167_status) & 1) == 0);
117
118 splx(s);
119
120 /* Convert the reasonable time into a date: */
121 getmicrotime(&now);
122 clock_secs_to_ymdhms(now.tv_sec, &dt_reasonable);
123 if (dt_reasonable.dt_year == POSIX_BASE_YEAR) {
124 /*
125 * Not a reasonable year.
126 * Assume called from inittodr(9) on boot and
127 * use file system time set in inittodr(9).
128 */
129 clock_secs_to_ymdhms(handle->base_time, &dt_reasonable);
130 }
131
132 /*
133 * We need to fake a hardware year. if the hardware MM/DD
134 * HH:MM:SS date is less than the reasonable MM/DD
135 * HH:MM:SS, call it the reasonable year plus one, else call
136 * it the reasonable year.
137 */
138 if (dt_hardware.dt_mon < dt_reasonable.dt_mon ||
139 (dt_hardware.dt_mon == dt_reasonable.dt_mon &&
140 (dt_hardware.dt_day < dt_reasonable.dt_day ||
141 (dt_hardware.dt_day == dt_reasonable.dt_day &&
142 (dt_hardware.dt_hour < dt_reasonable.dt_hour ||
143 (dt_hardware.dt_hour == dt_reasonable.dt_hour &&
144 (dt_hardware.dt_min < dt_reasonable.dt_min ||
145 (dt_hardware.dt_min == dt_reasonable.dt_min &&
146 (dt_hardware.dt_sec < dt_reasonable.dt_sec))))))))) {
147 dt_hardware.dt_year = dt_reasonable.dt_year + 1;
148 } else {
149 dt_hardware.dt_year = dt_reasonable.dt_year;
150 }
151
152 /* convert the hardware date into a time: */
153 tv->tv_sec = clock_ymdhms_to_secs(&dt_hardware);
154 tv->tv_usec = 0;
155
156 /*
157 * Make a reasonable effort to see if a leap day has passed
158 * that we need to account for. This does the right thing
159 * only when the system was shut down before a leap day, and
160 * it is now after that leap day. It doesn't do the right
161 * thing when a leap day happened while the machine was last
162 * up. When that happens, the hardware clock becomes
163 * instantly wrong forever, until it gets fixed for some
164 * reason. Use NTP to deal.
165 */
166
167 /*
168 * This may have happened if the hardware says we're into
169 * March in the following year. Check that following year for
170 * a leap day.
171 */
172 if (dt_hardware.dt_year > dt_reasonable.dt_year &&
173 dt_hardware.dt_mon >= 3) {
174 leap_year = dt_hardware.dt_year;
175 }
176
177 /*
178 * This may have happened if the hardware says we're in the
179 * following year, and the system was shut down before March
180 * the previous year. check that previous year for a leap
181 * day.
182 */
183 else if (dt_hardware.dt_year > dt_reasonable.dt_year &&
184 dt_reasonable.dt_mon < 3) {
185 leap_year = dt_reasonable.dt_year;
186 }
187
188 /*
189 * This may have happened if the hardware says we're in the
190 * same year, but we weren't to March before, and we're in or
191 * past March now. Check this year for a leap day.
192 */
193 else if (dt_hardware.dt_year == dt_reasonable.dt_year
194 && dt_reasonable.dt_mon < 3
195 && dt_hardware.dt_mon >= 3) {
196 leap_year = dt_reasonable.dt_year;
197 }
198
199 /*
200 * Otherwise, no leap year to check.
201 */
202 else {
203 leap_year = 0;
204 }
205
206 /* Do the real leap day check. */
207 had_leap_day = 0;
208 if (leap_year > 0) {
209 if ((leap_year & 3) == 0) {
210 had_leap_day = 1;
211 if ((leap_year % 100) == 0) {
212 had_leap_day = 0;
213 if ((leap_year % 400) == 0)
214 had_leap_day = 1;
215 }
216 }
217 }
218
219 /*
220 * If we had a leap day, adjust the value we will return, and
221 * also update the hardware clock.
222 */
223 /*
224 * XXX - Since this update just writes back a corrected
225 * version of what we read out above, we lose whatever
226 * amount of time the clock has advanced since that read.
227 * Use NTP to deal.
228 */
229 if (had_leap_day) {
230 tv->tv_sec += SECDAY;
231 todr_settime(handle, tv);
232 }
233
234 return 0;
235 }
236
237 int
238 mm58167_settime(todr_chip_handle_t handle, volatile struct timeval *tv)
239 {
240 struct mm58167_softc *sc = handle->cookie;
241 struct clock_ymdhms dt_hardware;
242 int s;
243 uint8_t byte_value;
244
245 /* Convert the seconds into ymdhms. */
246 clock_secs_to_ymdhms(tv->tv_sec, &dt_hardware);
247
248 /* No interrupts while we're in the chip. */
249 s = splhigh();
250
251 /*
252 * Issue a GO command to reset everything less significant
253 * than the minutes to zero.
254 */
255 mm58167_write(sc, mm58167_go, 0xFF);
256
257 /* Load everything. */
258 #define _MM58167_PUT(dt_f, mm_f) \
259 byte_value = TOBCD(dt_hardware.dt_f); \
260 mm58167_write(sc, mm_f, byte_value)
261
262 _MM58167_PUT(dt_mon, mm58167_mon);
263 _MM58167_PUT(dt_day, mm58167_day);
264 _MM58167_PUT(dt_hour, mm58167_hour);
265 _MM58167_PUT(dt_min, mm58167_min);
266 _MM58167_PUT(dt_sec, mm58167_sec);
267 #undef _MM58167_PUT
268
269 splx(s);
270 return 0;
271 }
272