xref: /netbsd-src/sys/dev/ic/mm58167.c (revision f82d7874c259b2a6cc59b714f844919f32bf7b51)
1 /*	$NetBSD: mm58167.c,v 1.10 2008/04/28 20:23:50 martin 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.10 2008/04/28 20:23:50 martin 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) bus_space_read_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r)
59 #define mm58167_write(sc, r, v) bus_space_write_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r, v)
60 
61 todr_chip_handle_t
62 mm58167_attach(sc)
63 	struct mm58167_softc *sc;
64 {
65 	struct todr_chip_handle *handle;
66 
67 	printf(": mm58167");
68 
69 	handle = &sc->_mm58167_todr_handle;
70 	memset(handle, 0, sizeof(handle));
71 	handle->cookie = sc;
72 	handle->todr_gettime = mm58167_gettime;
73 	handle->todr_settime = mm58167_settime;
74 	return (handle);
75 }
76 
77 /*
78  * Set up the system's time, given a `reasonable' time value.
79  */
80 int
81 mm58167_gettime(handle, tv)
82 	todr_chip_handle_t handle;
83 	volatile struct timeval *tv;
84 {
85 	struct mm58167_softc *sc = handle->cookie;
86 	struct clock_ymdhms dt_hardware;
87 	struct clock_ymdhms dt_reasonable;
88 	int s;
89 	u_int8_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) byte_value = mm58167_read(sc, mm_f); dt_hardware.dt_f = FROMBCD(byte_value)
107 		_MM58167_GET(dt_mon, mm58167_mon);
108 		_MM58167_GET(dt_day, mm58167_day);
109 		_MM58167_GET(dt_hour, mm58167_hour);
110 		_MM58167_GET(dt_min, mm58167_min);
111 		_MM58167_GET(dt_sec, mm58167_sec);
112 #undef _MM58167_GET
113 	} while ((mm58167_read(sc, mm58167_status) & 1) == 0);
114 
115 	splx(s);
116 
117 	/* Convert the reasonable time into a date: */
118 	clock_secs_to_ymdhms(tv->tv_sec, &dt_reasonable);
119 
120 	/*
121 	 * We need to fake a hardware year.  if the hardware MM/DD
122 	 * HH:MM:SS date is less than the reasonable MM/DD
123 	 * HH:MM:SS, call it the reasonable year plus one, else call
124 	 * it the reasonable year.
125 	 */
126 	if (dt_hardware.dt_mon < dt_reasonable.dt_mon ||
127 	    (dt_hardware.dt_mon == dt_reasonable.dt_mon &&
128 		(dt_hardware.dt_day < dt_reasonable.dt_day ||
129 		    (dt_hardware.dt_day == dt_reasonable.dt_day &&
130 			(dt_hardware.dt_hour < dt_reasonable.dt_hour ||
131 			    (dt_hardware.dt_hour == dt_reasonable.dt_hour &&
132 				(dt_hardware.dt_min < dt_reasonable.dt_min ||
133 				    (dt_hardware.dt_min == dt_reasonable.dt_min &&
134 					(dt_hardware.dt_sec < dt_reasonable.dt_sec))))))))) {
135 	  dt_hardware.dt_year = dt_reasonable.dt_year + 1;
136 	} else {
137 	  dt_hardware.dt_year = dt_reasonable.dt_year;
138 	}
139 
140 	/* convert the hardware date into a time: */
141 	tv->tv_sec = clock_ymdhms_to_secs(&dt_hardware);
142 	tv->tv_usec = 0;
143 
144 	/*
145 	 * Make a reasonable effort to see if a leap day has passed
146 	 * that we need to account for.  This does the right thing
147 	 * only when the system was shut down before a leap day, and
148 	 * it is now after that leap day.  It doesn't do the right
149 	 * thing when a leap day happened while the machine was last
150 	 * up.  When that happens, the hardware clock becomes
151 	 * instantly wrong forever, until it gets fixed for some
152 	 * reason.  Use NTP to deal.
153 	 */
154 
155 	/*
156 	 * This may have happened if the hardware says we're into
157 	 * March in the following year.  Check that following year for
158 	 * a leap day.
159 	 */
160 	if (dt_hardware.dt_year > dt_reasonable.dt_year &&
161 	    dt_hardware.dt_mon >= 3) {
162 	  leap_year = dt_hardware.dt_year;
163 	}
164 
165 	/*
166 	 * This may have happened if the hardware says we're in the
167 	 * following year, and the system was shut down before March
168 	 * the previous year.  check that previous year for a leap
169 	 * day.
170 	 */
171 	else if (dt_hardware.dt_year > dt_reasonable.dt_year &&
172 		 dt_reasonable.dt_mon < 3) {
173 	  leap_year = dt_reasonable.dt_year;
174 	}
175 
176 	/*
177 	 * This may have happened if the hardware says we're in the
178 	 * same year, but we weren't to March before, and we're in or
179 	 * past March now.  Check this year for a leap day.
180 	 */
181 	else if (dt_hardware.dt_year == dt_reasonable.dt_year
182 		 && dt_reasonable.dt_mon < 3
183 		 && dt_hardware.dt_mon >= 3) {
184 	  leap_year = dt_reasonable.dt_year;
185 	}
186 
187 	/*
188 	 * Otherwise, no leap year to check.
189 	 */
190 	else {
191 	  leap_year = 0;
192 	}
193 
194 	/* Do the real leap day check. */
195 	had_leap_day = 0;
196 	if (leap_year > 0) {
197 		if ((leap_year & 3) == 0) {
198 			had_leap_day = 1;
199 			if ((leap_year % 100) == 0) {
200 				had_leap_day = 0;
201 				if ((leap_year % 400) == 0)
202 					had_leap_day = 1;
203 			}
204 		}
205 	}
206 
207 	/*
208 	 * If we had a leap day, adjust the value we will return, and
209 	 * also update the hardware clock.
210 	 */
211 	/*
212 	 * XXX - Since this update just writes back a corrected
213 	 * version of what we read out above, we lose whatever
214 	 * amount of time the clock has advanced since that read.
215 	 * Use NTP to deal.
216 	 */
217 	if (had_leap_day) {
218 	  tv->tv_sec += SECDAY;
219 	  todr_settime(handle, tv);
220 	}
221 
222 	return (0);
223 }
224 
225 int
226 mm58167_settime(handle, tv)
227 	todr_chip_handle_t handle;
228 	volatile struct timeval *tv;
229 {
230 	struct mm58167_softc *sc = handle->cookie;
231 	struct clock_ymdhms dt_hardware;
232 	int s;
233 	u_int8_t byte_value;
234 
235 	/* Convert the seconds into ymdhms. */
236 	clock_secs_to_ymdhms(tv->tv_sec, &dt_hardware);
237 
238 	/* No interrupts while we're in the chip. */
239 	s = splhigh();
240 
241 	/*
242 	 * Issue a GO command to reset everything less significant
243 	 * than the minutes to zero.
244 	 */
245 	mm58167_write(sc, mm58167_go, 0xFF);
246 
247 	/* Load everything. */
248 #define _MM58167_PUT(dt_f, mm_f) byte_value = TOBCD(dt_hardware.dt_f); mm58167_write(sc, mm_f, byte_value)
249 	_MM58167_PUT(dt_mon, mm58167_mon);
250 	_MM58167_PUT(dt_day, mm58167_day);
251 	_MM58167_PUT(dt_hour, mm58167_hour);
252 	_MM58167_PUT(dt_min, mm58167_min);
253 	_MM58167_PUT(dt_sec, mm58167_sec);
254 #undef _MM58167_PUT
255 
256 	splx(s);
257 	return (0);
258 }
259