rtc.c revision 1.1 1 1.1 bouyer /* $NetBSD: rtc.c,v 1.1 2009/06/16 21:05:34 bouyer Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*-
4 1.1 bouyer * Copyright (c) 1990 The Regents of the University of California.
5 1.1 bouyer * All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * This code is derived from software contributed to Berkeley by
8 1.1 bouyer * William Jolitz and Don Ahn.
9 1.1 bouyer *
10 1.1 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1 bouyer * modification, are permitted provided that the following conditions
12 1.1 bouyer * are met:
13 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1 bouyer * documentation and/or other materials provided with the distribution.
18 1.1 bouyer * 3. Neither the name of the University nor the names of its contributors
19 1.1 bouyer * may be used to endorse or promote products derived from this software
20 1.1 bouyer * without specific prior written permission.
21 1.1 bouyer *
22 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 bouyer * SUCH DAMAGE.
33 1.1 bouyer *
34 1.1 bouyer * @(#)clock.c 7.2 (Berkeley) 5/12/91
35 1.1 bouyer */
36 1.1 bouyer /*-
37 1.1 bouyer * Copyright (c) 1993, 1994 Charles M. Hannum.
38 1.1 bouyer *
39 1.1 bouyer * This code is derived from software contributed to Berkeley by
40 1.1 bouyer * William Jolitz and Don Ahn.
41 1.1 bouyer *
42 1.1 bouyer * Redistribution and use in source and binary forms, with or without
43 1.1 bouyer * modification, are permitted provided that the following conditions
44 1.1 bouyer * are met:
45 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
46 1.1 bouyer * notice, this list of conditions and the following disclaimer.
47 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
49 1.1 bouyer * documentation and/or other materials provided with the distribution.
50 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
51 1.1 bouyer * must display the following acknowledgement:
52 1.1 bouyer * This product includes software developed by the University of
53 1.1 bouyer * California, Berkeley and its contributors.
54 1.1 bouyer * 4. Neither the name of the University nor the names of its contributors
55 1.1 bouyer * may be used to endorse or promote products derived from this software
56 1.1 bouyer * without specific prior written permission.
57 1.1 bouyer *
58 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.1 bouyer * SUCH DAMAGE.
69 1.1 bouyer *
70 1.1 bouyer * @(#)clock.c 7.2 (Berkeley) 5/12/91
71 1.1 bouyer */
72 1.1 bouyer /*
73 1.1 bouyer * Mach Operating System
74 1.1 bouyer * Copyright (c) 1991,1990,1989 Carnegie Mellon University
75 1.1 bouyer * All Rights Reserved.
76 1.1 bouyer *
77 1.1 bouyer * Permission to use, copy, modify and distribute this software and its
78 1.1 bouyer * documentation is hereby granted, provided that both the copyright
79 1.1 bouyer * notice and this permission notice appear in all copies of the
80 1.1 bouyer * software, derivative works or modified versions, and any portions
81 1.1 bouyer * thereof, and that both notices appear in supporting documentation.
82 1.1 bouyer *
83 1.1 bouyer * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
84 1.1 bouyer * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
85 1.1 bouyer * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
86 1.1 bouyer *
87 1.1 bouyer * Carnegie Mellon requests users of this software to return to
88 1.1 bouyer *
89 1.1 bouyer * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
90 1.1 bouyer * School of Computer Science
91 1.1 bouyer * Carnegie Mellon University
92 1.1 bouyer * Pittsburgh PA 15213-3890
93 1.1 bouyer *
94 1.1 bouyer * any improvements or extensions that they make and grant Carnegie Mellon
95 1.1 bouyer * the rights to redistribute these changes.
96 1.1 bouyer */
97 1.1 bouyer /*
98 1.1 bouyer Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
99 1.1 bouyer
100 1.1 bouyer All Rights Reserved
101 1.1 bouyer
102 1.1 bouyer Permission to use, copy, modify, and distribute this software and
103 1.1 bouyer its documentation for any purpose and without fee is hereby
104 1.1 bouyer granted, provided that the above copyright notice appears in all
105 1.1 bouyer copies and that both the copyright notice and this permission notice
106 1.1 bouyer appear in supporting documentation, and that the name of Intel
107 1.1 bouyer not be used in advertising or publicity pertaining to distribution
108 1.1 bouyer of the software without specific, written prior permission.
109 1.1 bouyer
110 1.1 bouyer INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
111 1.1 bouyer INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
112 1.1 bouyer IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
113 1.1 bouyer CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
114 1.1 bouyer LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
115 1.1 bouyer NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
116 1.1 bouyer WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
117 1.1 bouyer */
118 1.1 bouyer
119 1.1 bouyer /*
120 1.1 bouyer * Primitive RTC chip routines.
121 1.1 bouyer */
122 1.1 bouyer
123 1.1 bouyer #include <sys/cdefs.h>
124 1.1 bouyer __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.1 2009/06/16 21:05:34 bouyer Exp $");
125 1.1 bouyer
126 1.1 bouyer #include <sys/param.h>
127 1.1 bouyer #include <sys/systm.h>
128 1.1 bouyer #include <sys/time.h>
129 1.1 bouyer #include <sys/timetc.h>
130 1.1 bouyer #include <sys/kernel.h>
131 1.1 bouyer
132 1.1 bouyer #include <dev/isa/isareg.h>
133 1.1 bouyer #include <dev/isa/isavar.h>
134 1.1 bouyer #include <i386/isa/nvram.h>
135 1.1 bouyer
136 1.1 bouyer #include <machine/pio.h>
137 1.1 bouyer
138 1.1 bouyer #include <dev/ic/mc146818reg.h>
139 1.1 bouyer #include <x86/rtc.h>
140 1.1 bouyer
141 1.1 bouyer #ifndef __x86_64__
142 1.1 bouyer #include "mca.h"
143 1.1 bouyer #endif
144 1.1 bouyer #if NMCA > 0
145 1.1 bouyer #include <machine/mca_machdep.h> /* for MCA_system */
146 1.1 bouyer #endif
147 1.1 bouyer
148 1.1 bouyer static void rtcinit(void);
149 1.1 bouyer static int rtcget(mc_todregs *);
150 1.1 bouyer static void rtcput(mc_todregs *);
151 1.1 bouyer static int cmoscheck(void);
152 1.1 bouyer static int clock_expandyear(int);
153 1.1 bouyer
154 1.1 bouyer /* XXX use sc? */
155 1.1 bouyer u_int
156 1.1 bouyer mc146818_read(void *sc, u_int reg)
157 1.1 bouyer {
158 1.1 bouyer
159 1.1 bouyer outb(IO_RTC, reg);
160 1.1 bouyer return (inb(IO_RTC+1));
161 1.1 bouyer }
162 1.1 bouyer
163 1.1 bouyer void
164 1.1 bouyer mc146818_write(void *sc, u_int reg, u_int datum)
165 1.1 bouyer {
166 1.1 bouyer
167 1.1 bouyer outb(IO_RTC, reg);
168 1.1 bouyer outb(IO_RTC+1, datum);
169 1.1 bouyer }
170 1.1 bouyer
171 1.1 bouyer static void
172 1.1 bouyer rtcinit(void)
173 1.1 bouyer {
174 1.1 bouyer static int first_rtcopen_ever = 1;
175 1.1 bouyer
176 1.1 bouyer if (!first_rtcopen_ever)
177 1.1 bouyer return;
178 1.1 bouyer first_rtcopen_ever = 0;
179 1.1 bouyer
180 1.1 bouyer mc146818_write(NULL, MC_REGA, /* XXX softc */
181 1.1 bouyer MC_BASE_32_KHz | MC_RATE_1024_Hz);
182 1.1 bouyer mc146818_write(NULL, MC_REGB, MC_REGB_24HR); /* XXX softc */
183 1.1 bouyer }
184 1.1 bouyer
185 1.1 bouyer static int
186 1.1 bouyer rtcget(mc_todregs *regs)
187 1.1 bouyer {
188 1.1 bouyer
189 1.1 bouyer rtcinit();
190 1.1 bouyer if ((mc146818_read(NULL, MC_REGD) & MC_REGD_VRT) == 0) /* XXX softc */
191 1.1 bouyer return (-1);
192 1.1 bouyer MC146818_GETTOD(NULL, regs); /* XXX softc */
193 1.1 bouyer return (0);
194 1.1 bouyer }
195 1.1 bouyer
196 1.1 bouyer static void
197 1.1 bouyer rtcput(mc_todregs *regs)
198 1.1 bouyer {
199 1.1 bouyer
200 1.1 bouyer rtcinit();
201 1.1 bouyer MC146818_PUTTOD(NULL, regs); /* XXX softc */
202 1.1 bouyer }
203 1.1 bouyer
204 1.1 bouyer /*
205 1.1 bouyer * check whether the CMOS layout is "standard"-like (ie, not PS/2-like),
206 1.1 bouyer * to be called at splclock()
207 1.1 bouyer */
208 1.1 bouyer static int
209 1.1 bouyer cmoscheck(void)
210 1.1 bouyer {
211 1.1 bouyer int i;
212 1.1 bouyer unsigned short cksum = 0;
213 1.1 bouyer
214 1.1 bouyer for (i = 0x10; i <= 0x2d; i++)
215 1.1 bouyer cksum += mc146818_read(NULL, i); /* XXX softc */
216 1.1 bouyer
217 1.1 bouyer return (cksum == (mc146818_read(NULL, 0x2e) << 8)
218 1.1 bouyer + mc146818_read(NULL, 0x2f));
219 1.1 bouyer }
220 1.1 bouyer
221 1.1 bouyer #if NMCA > 0
222 1.1 bouyer /*
223 1.1 bouyer * Check whether the CMOS layout is PS/2 like, to be called at splclock().
224 1.1 bouyer */
225 1.1 bouyer static int cmoscheckps2(void);
226 1.1 bouyer static int
227 1.1 bouyer cmoscheckps2(void)
228 1.1 bouyer {
229 1.1 bouyer #if 0
230 1.1 bouyer /* Disabled until I find out the CRC checksum algorithm IBM uses */
231 1.1 bouyer int i;
232 1.1 bouyer unsigned short cksum = 0;
233 1.1 bouyer
234 1.1 bouyer for (i = 0x10; i <= 0x31; i++)
235 1.1 bouyer cksum += mc146818_read(NULL, i); /* XXX softc */
236 1.1 bouyer
237 1.1 bouyer return (cksum == (mc146818_read(NULL, 0x32) << 8)
238 1.1 bouyer + mc146818_read(NULL, 0x33));
239 1.1 bouyer #else
240 1.1 bouyer /* Check 'incorrect checksum' bit of IBM PS/2 Diagnostic Status Byte */
241 1.1 bouyer return ((mc146818_read(NULL, NVRAM_DIAG) & (1<<6)) == 0);
242 1.1 bouyer #endif
243 1.1 bouyer }
244 1.1 bouyer #endif /* NMCA > 0 */
245 1.1 bouyer
246 1.1 bouyer /*
247 1.1 bouyer * patchable to control century byte handling:
248 1.1 bouyer * 1: always update
249 1.1 bouyer * -1: never touch
250 1.1 bouyer * 0: try to figure out itself
251 1.1 bouyer */
252 1.1 bouyer int rtc_update_century = 0;
253 1.1 bouyer
254 1.1 bouyer /*
255 1.1 bouyer * Expand a two-digit year as read from the clock chip
256 1.1 bouyer * into full width.
257 1.1 bouyer * Being here, deal with the CMOS century byte.
258 1.1 bouyer */
259 1.1 bouyer static int centb = NVRAM_CENTURY;
260 1.1 bouyer static int
261 1.1 bouyer clock_expandyear(int clockyear)
262 1.1 bouyer {
263 1.1 bouyer int s, clockcentury, cmoscentury;
264 1.1 bouyer
265 1.1 bouyer clockcentury = (clockyear < 70) ? 20 : 19;
266 1.1 bouyer clockyear += 100 * clockcentury;
267 1.1 bouyer
268 1.1 bouyer if (rtc_update_century < 0)
269 1.1 bouyer return (clockyear);
270 1.1 bouyer
271 1.1 bouyer s = splclock();
272 1.1 bouyer if (cmoscheck())
273 1.1 bouyer cmoscentury = mc146818_read(NULL, NVRAM_CENTURY);
274 1.1 bouyer #if NMCA > 0
275 1.1 bouyer else if (MCA_system && cmoscheckps2())
276 1.1 bouyer cmoscentury = mc146818_read(NULL, (centb = 0x37));
277 1.1 bouyer #endif
278 1.1 bouyer else
279 1.1 bouyer cmoscentury = 0;
280 1.1 bouyer splx(s);
281 1.1 bouyer if (!cmoscentury) {
282 1.1 bouyer #ifdef DIAGNOSTIC
283 1.1 bouyer printf("clock: unknown CMOS layout\n");
284 1.1 bouyer #endif
285 1.1 bouyer return (clockyear);
286 1.1 bouyer }
287 1.1 bouyer cmoscentury = bcdtobin(cmoscentury);
288 1.1 bouyer
289 1.1 bouyer if (cmoscentury != clockcentury) {
290 1.1 bouyer /* XXX note: saying "century is 20" might confuse the naive. */
291 1.1 bouyer printf("WARNING: NVRAM century is %d but RTC year is %d\n",
292 1.1 bouyer cmoscentury, clockyear);
293 1.1 bouyer
294 1.1 bouyer /* Kludge to roll over century. */
295 1.1 bouyer if ((rtc_update_century > 0) ||
296 1.1 bouyer ((cmoscentury == 19) && (clockcentury == 20) &&
297 1.1 bouyer (clockyear == 2000))) {
298 1.1 bouyer printf("WARNING: Setting NVRAM century to %d\n",
299 1.1 bouyer clockcentury);
300 1.1 bouyer s = splclock();
301 1.1 bouyer mc146818_write(NULL, centb, bintobcd(clockcentury));
302 1.1 bouyer splx(s);
303 1.1 bouyer }
304 1.1 bouyer } else if (cmoscentury == 19 && rtc_update_century == 0)
305 1.1 bouyer rtc_update_century = 1; /* will update later in resettodr() */
306 1.1 bouyer
307 1.1 bouyer return (clockyear);
308 1.1 bouyer }
309 1.1 bouyer
310 1.1 bouyer int
311 1.1 bouyer rtc_get_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
312 1.1 bouyer {
313 1.1 bouyer int s;
314 1.1 bouyer mc_todregs rtclk;
315 1.1 bouyer
316 1.1 bouyer s = splclock();
317 1.1 bouyer if (rtcget(&rtclk)) {
318 1.1 bouyer splx(s);
319 1.1 bouyer return -1;
320 1.1 bouyer }
321 1.1 bouyer splx(s);
322 1.1 bouyer
323 1.1 bouyer dt->dt_sec = bcdtobin(rtclk[MC_SEC]);
324 1.1 bouyer dt->dt_min = bcdtobin(rtclk[MC_MIN]);
325 1.1 bouyer dt->dt_hour = bcdtobin(rtclk[MC_HOUR]);
326 1.1 bouyer dt->dt_day = bcdtobin(rtclk[MC_DOM]);
327 1.1 bouyer dt->dt_mon = bcdtobin(rtclk[MC_MONTH]);
328 1.1 bouyer dt->dt_year = clock_expandyear(bcdtobin(rtclk[MC_YEAR]));
329 1.1 bouyer
330 1.1 bouyer return 0;
331 1.1 bouyer }
332 1.1 bouyer
333 1.1 bouyer int
334 1.1 bouyer rtc_set_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
335 1.1 bouyer {
336 1.1 bouyer mc_todregs rtclk;
337 1.1 bouyer int century;
338 1.1 bouyer int s;
339 1.1 bouyer
340 1.1 bouyer s = splclock();
341 1.1 bouyer if (rtcget(&rtclk))
342 1.1 bouyer memset(&rtclk, 0, sizeof(rtclk));
343 1.1 bouyer splx(s);
344 1.1 bouyer
345 1.1 bouyer rtclk[MC_SEC] = bintobcd(dt->dt_sec);
346 1.1 bouyer rtclk[MC_MIN] = bintobcd(dt->dt_min);
347 1.1 bouyer rtclk[MC_HOUR] = bintobcd(dt->dt_hour);
348 1.1 bouyer rtclk[MC_DOW] = dt->dt_wday + 1;
349 1.1 bouyer rtclk[MC_YEAR] = bintobcd(dt->dt_year % 100);
350 1.1 bouyer rtclk[MC_MONTH] = bintobcd(dt->dt_mon);
351 1.1 bouyer rtclk[MC_DOM] = bintobcd(dt->dt_day);
352 1.1 bouyer
353 1.1 bouyer #ifdef DEBUG_CLOCK
354 1.1 bouyer printf("setclock: %x/%x/%x %x:%x:%x\n", rtclk[MC_YEAR], rtclk[MC_MONTH],
355 1.1 bouyer rtclk[MC_DOM], rtclk[MC_HOUR], rtclk[MC_MIN], rtclk[MC_SEC]);
356 1.1 bouyer #endif
357 1.1 bouyer s = splclock();
358 1.1 bouyer rtcput(&rtclk);
359 1.1 bouyer if (rtc_update_century > 0) {
360 1.1 bouyer century = bintobcd(dt->dt_year / 100);
361 1.1 bouyer mc146818_write(NULL, centb, century); /* XXX softc */
362 1.1 bouyer }
363 1.1 bouyer splx(s);
364 1.1 bouyer return 0;
365 1.1 bouyer
366 1.1 bouyer }
367 1.1 bouyer
368 1.1 bouyer void
369 1.1 bouyer rtc_register(void)
370 1.1 bouyer {
371 1.1 bouyer static struct todr_chip_handle tch;
372 1.1 bouyer tch.todr_gettime_ymdhms = rtc_get_ymdhms;
373 1.1 bouyer tch.todr_settime_ymdhms = rtc_set_ymdhms;
374 1.1 bouyer tch.todr_setwen = NULL;
375 1.1 bouyer
376 1.1 bouyer todr_attach(&tch);
377 1.1 bouyer }
378 1.1 bouyer
379