xref: /netbsd/sys/dev/ic/mm58167.c (revision 6550d01e)
1 /*	$NetBSD: mm58167.c,v 1.14 2009/12/12 16:12:05 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.14 2009/12/12 16:12:05 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 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 *);
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_ymdhms = mm58167_gettime_ymdhms;
74 	handle->todr_settime_ymdhms = mm58167_settime_ymdhms;
75 	return handle;
76 }
77 
78 /*
79  * Set up the system's time, given a `reasonable' time value.
80  */
81 int
82 mm58167_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
83 {
84 	struct mm58167_softc *sc = handle->cookie;
85 	struct clock_ymdhms dt_reasonable;
86 	struct timeval now;
87 	int s;
88 	uint8_t byte_value;
89 	int leap_year, had_leap_day;
90 
91 	/* First, read the date out of the chip. */
92 
93 	/* No interrupts while we're in the chip. */
94 	s = splhigh();
95 
96 	/* Reset the status bit: */
97 	byte_value = mm58167_read(sc, mm58167_status);
98 
99 	/*
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).
103 	 */
104 	do {
105 #define _MM58167_GET(dt_f, mm_f)					\
106 	byte_value = mm58167_read(sc, mm_f);				\
107 	dt->dt_f = FROMBCD(byte_value)
108 
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);
114 #undef _MM58167_GET
115 	} while ((mm58167_read(sc, mm58167_status) & 1) == 0);
116 
117 	splx(s);
118 
119 	/* Convert the reasonable time into a date: */
120 	getmicrotime(&now);
121 	clock_secs_to_ymdhms(now.tv_sec, &dt_reasonable);
122 	if (dt_reasonable.dt_year == POSIX_BASE_YEAR) {
123 		/*
124 		 * Not a reasonable year.
125 		 * Assume called from inittodr(9) on boot and
126 		 * use file system time set in inittodr(9).
127 		 */
128 		clock_secs_to_ymdhms(handle->base_time, &dt_reasonable);
129 	}
130 
131 	/*
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.
136 	 */
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;
147 	} else {
148 		dt->dt_year = dt_reasonable.dt_year;
149 	}
150 
151 	/*
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.
160 	 */
161 
162 	/*
163 	 * This may have happened if the hardware says we're into
164 	 * March in the following year.  Check that following year for
165 	 * a leap day.
166 	 */
167 	if (dt->dt_year > dt_reasonable.dt_year &&
168 	    dt->dt_mon >= 3) {
169 		leap_year = dt->dt_year;
170 	}
171 
172 	/*
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
176 	 * day.
177 	 */
178 	else if (dt->dt_year > dt_reasonable.dt_year &&
179 	    dt_reasonable.dt_mon < 3) {
180 		leap_year = dt_reasonable.dt_year;
181 	}
182 
183 	/*
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.
187 	 */
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;
192 	}
193 
194 	/*
195 	 * Otherwise, no leap year to check.
196 	 */
197 	else {
198 		leap_year = 0;
199 	}
200 
201 	/* Do the real leap day check. */
202 	had_leap_day = 0;
203 	if (leap_year > 0) {
204 		if ((leap_year & 3) == 0) {
205 			had_leap_day = 1;
206 			if ((leap_year % 100) == 0) {
207 				had_leap_day = 0;
208 				if ((leap_year % 400) == 0)
209 					had_leap_day = 1;
210 			}
211 		}
212 	}
213 
214 	/*
215 	 * If we had a leap day, adjust the value we will return, and
216 	 * also update the hardware clock.
217 	 */
218 	/*
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.
222 	 * Use NTP to deal.
223 	 */
224 	if (had_leap_day) {
225 		mm58167_settime_ymdhms(handle, dt);
226 	}
227 
228 	return 0;
229 }
230 
231 int
232 mm58167_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
233 {
234 	struct mm58167_softc *sc = handle->cookie;
235 	int s;
236 	uint8_t byte_value;
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)					\
249 	byte_value = TOBCD(dt->dt_f);					\
250 	mm58167_write(sc, mm_f, byte_value)
251 
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);
257 #undef _MM58167_PUT
258 
259 	splx(s);
260 	return 0;
261 }
262