1 /* $NetBSD: mm58167.c,v 1.13 2009/12/12 14:44:10 tsutsui Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * National Semiconductor MM58167 time-of-day chip subroutines.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: mm58167.c,v 1.13 2009/12/12 14:44:10 tsutsui Exp $");
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>
46 #include <dev/clock_subr.h>
47 #include <dev/ic/mm58167var.h>
49 static int mm58167_gettime_ymdhms(todr_chip_handle_t
, struct clock_ymdhms
*);
50 static int mm58167_settime_ymdhms(todr_chip_handle_t
, struct clock_ymdhms
*);
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."
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)
64 mm58167_attach(struct mm58167_softc
*sc
)
66 struct todr_chip_handle
*handle
;
68 aprint_normal(": mm58167");
70 handle
= &sc
->_mm58167_todr_handle
;
71 memset(handle
, 0, sizeof(handle
));
73 handle
->todr_gettime_ymdhms
= mm58167_gettime_ymdhms
;
74 handle
->todr_settime_ymdhms
= mm58167_settime_ymdhms
;
79 * Set up the system's time, given a `reasonable' time value.
82 mm58167_gettime_ymdhms(todr_chip_handle_t handle
, struct clock_ymdhms
*dt
)
84 struct mm58167_softc
*sc
= handle
->cookie
;
85 struct clock_ymdhms dt_reasonable
;
89 int leap_year
, had_leap_day
;
91 /* First, read the date out of the chip. */
93 /* No interrupts while we're in the chip. */
96 /* Reset the status bit: */
97 byte_value
= mm58167_read(sc
, mm58167_status
);
100 * Read the date values until we get a coherent read (one
101 * where the status stays zero, indicating no increment was
102 * rippling through while we were reading).
105 #define _MM58167_GET(dt_f, mm_f) \
106 byte_value = mm58167_read(sc, mm_f); \
107 dt->dt_f = FROMBCD(byte_value)
109 _MM58167_GET(dt_mon
, mm58167_mon
);
110 _MM58167_GET(dt_day
, mm58167_day
);
111 _MM58167_GET(dt_hour
, mm58167_hour
);
112 _MM58167_GET(dt_min
, mm58167_min
);
113 _MM58167_GET(dt_sec
, mm58167_sec
);
115 } while ((mm58167_read(sc
, mm58167_status
) & 1) == 0);
119 /* Convert the reasonable time into a date: */
121 clock_secs_to_ymdhms(now
.tv_sec
, &dt_reasonable
);
122 if (dt_reasonable
.dt_year
== POSIX_BASE_YEAR
) {
124 * Not a reasonable year.
125 * Assume called from inittodr(9) on boot and
126 * use file system time set in inittodr(9).
128 clock_secs_to_ymdhms(handle
->base_time
, &dt_reasonable
);
132 * We need to fake a hardware year. if the hardware MM/DD
133 * HH:MM:SS date is less than the reasonable MM/DD
134 * HH:MM:SS, call it the reasonable year plus one, else call
135 * it the reasonable year.
137 if (dt
->dt_mon
< dt_reasonable
.dt_mon
||
138 (dt
->dt_mon
== dt_reasonable
.dt_mon
&&
139 (dt
->dt_day
< dt_reasonable
.dt_day
||
140 (dt
->dt_day
== dt_reasonable
.dt_day
&&
141 (dt
->dt_hour
< dt_reasonable
.dt_hour
||
142 (dt
->dt_hour
== dt_reasonable
.dt_hour
&&
143 (dt
->dt_min
< dt_reasonable
.dt_min
||
144 (dt
->dt_min
== dt_reasonable
.dt_min
&&
145 (dt
->dt_sec
< dt_reasonable
.dt_sec
))))))))) {
146 dt
->dt_year
= dt_reasonable
.dt_year
+ 1;
148 dt
->dt_year
= dt_reasonable
.dt_year
;
152 * Make a reasonable effort to see if a leap day has passed
153 * that we need to account for. This does the right thing
154 * only when the system was shut down before a leap day, and
155 * it is now after that leap day. It doesn't do the right
156 * thing when a leap day happened while the machine was last
157 * up. When that happens, the hardware clock becomes
158 * instantly wrong forever, until it gets fixed for some
159 * reason. Use NTP to deal.
163 * This may have happened if the hardware says we're into
164 * March in the following year. Check that following year for
167 if (dt
->dt_year
> dt_reasonable
.dt_year
&&
169 leap_year
= dt
->dt_year
;
173 * This may have happened if the hardware says we're in the
174 * following year, and the system was shut down before March
175 * the previous year. check that previous year for a leap
178 else if (dt
->dt_year
> dt_reasonable
.dt_year
&&
179 dt_reasonable
.dt_mon
< 3) {
180 leap_year
= dt_reasonable
.dt_year
;
184 * This may have happened if the hardware says we're in the
185 * same year, but we weren't to March before, and we're in or
186 * past March now. Check this year for a leap day.
188 else if (dt
->dt_year
== dt_reasonable
.dt_year
189 && dt_reasonable
.dt_mon
< 3
190 && dt
->dt_mon
>= 3) {
191 leap_year
= dt_reasonable
.dt_year
;
195 * Otherwise, no leap year to check.
201 /* Do the real leap day check. */
204 if ((leap_year
& 3) == 0) {
206 if ((leap_year
% 100) == 0) {
208 if ((leap_year
% 400) == 0)
215 * If we had a leap day, adjust the value we will return, and
216 * also update the hardware clock.
219 * XXX - Since this update just writes back a corrected
220 * version of what we read out above, we lose whatever
221 * amount of time the clock has advanced since that read.
225 mm58167_settime_ymdhms(handle
, dt
);
232 mm58167_settime_ymdhms(todr_chip_handle_t handle
, struct clock_ymdhms
*dt
)
234 struct mm58167_softc
*sc
= handle
->cookie
;
238 /* No interrupts while we're in the chip. */
242 * Issue a GO command to reset everything less significant
243 * than the minutes to zero.
245 mm58167_write(sc
, mm58167_go
, 0xFF);
247 /* Load everything. */
248 #define _MM58167_PUT(dt_f, mm_f) \
249 byte_value = TOBCD(dt->dt_f); \
250 mm58167_write(sc, mm_f, byte_value)
252 _MM58167_PUT(dt_mon
, mm58167_mon
);
253 _MM58167_PUT(dt_day
, mm58167_day
);
254 _MM58167_PUT(dt_hour
, mm58167_hour
);
255 _MM58167_PUT(dt_min
, mm58167_min
);
256 _MM58167_PUT(dt_sec
, mm58167_sec
);