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