rtc.c revision 1.3 1 1.3 jakllsch /* $NetBSD: rtc.c,v 1.3 2025/01/07 22:37:13 jakllsch 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.3 jakllsch __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.3 2025/01/07 22:37:13 jakllsch 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.2 jakllsch #include "acpica.h"
149 1.2 jakllsch #if NACPICA > 0
150 1.2 jakllsch #include <dev/acpi/acpivar.h>
151 1.2 jakllsch #endif
152 1.2 jakllsch
153 1.1 bouyer static void rtcinit(void);
154 1.1 bouyer static int rtcget(mc_todregs *);
155 1.1 bouyer static void rtcput(mc_todregs *);
156 1.1 bouyer static int cmoscheck(void);
157 1.1 bouyer static int clock_expandyear(int);
158 1.1 bouyer
159 1.1 bouyer /* XXX use sc? */
160 1.1 bouyer u_int
161 1.1 bouyer mc146818_read(void *sc, u_int reg)
162 1.1 bouyer {
163 1.1 bouyer
164 1.1 bouyer outb(IO_RTC, reg);
165 1.1 bouyer return (inb(IO_RTC+1));
166 1.1 bouyer }
167 1.1 bouyer
168 1.1 bouyer void
169 1.1 bouyer mc146818_write(void *sc, u_int reg, u_int datum)
170 1.1 bouyer {
171 1.1 bouyer
172 1.1 bouyer outb(IO_RTC, reg);
173 1.1 bouyer outb(IO_RTC+1, datum);
174 1.1 bouyer }
175 1.1 bouyer
176 1.1 bouyer static void
177 1.1 bouyer rtcinit(void)
178 1.1 bouyer {
179 1.1 bouyer static int first_rtcopen_ever = 1;
180 1.1 bouyer
181 1.1 bouyer if (!first_rtcopen_ever)
182 1.1 bouyer return;
183 1.1 bouyer first_rtcopen_ever = 0;
184 1.1 bouyer
185 1.1 bouyer mc146818_write(NULL, MC_REGA, /* XXX softc */
186 1.1 bouyer MC_BASE_32_KHz | MC_RATE_1024_Hz);
187 1.1 bouyer mc146818_write(NULL, MC_REGB, MC_REGB_24HR); /* XXX softc */
188 1.1 bouyer }
189 1.1 bouyer
190 1.1 bouyer static int
191 1.1 bouyer rtcget(mc_todregs *regs)
192 1.1 bouyer {
193 1.1 bouyer
194 1.1 bouyer rtcinit();
195 1.1 bouyer if ((mc146818_read(NULL, MC_REGD) & MC_REGD_VRT) == 0) /* XXX softc */
196 1.1 bouyer return (-1);
197 1.1 bouyer MC146818_GETTOD(NULL, regs); /* XXX softc */
198 1.1 bouyer return (0);
199 1.1 bouyer }
200 1.1 bouyer
201 1.1 bouyer static void
202 1.1 bouyer rtcput(mc_todregs *regs)
203 1.1 bouyer {
204 1.1 bouyer
205 1.1 bouyer rtcinit();
206 1.1 bouyer MC146818_PUTTOD(NULL, regs); /* XXX softc */
207 1.1 bouyer }
208 1.1 bouyer
209 1.1 bouyer /*
210 1.1 bouyer * check whether the CMOS layout is "standard"-like (ie, not PS/2-like),
211 1.1 bouyer * to be called at splclock()
212 1.1 bouyer */
213 1.1 bouyer static int
214 1.1 bouyer cmoscheck(void)
215 1.1 bouyer {
216 1.1 bouyer int i;
217 1.1 bouyer unsigned short cksum = 0;
218 1.1 bouyer
219 1.1 bouyer for (i = 0x10; i <= 0x2d; i++)
220 1.1 bouyer cksum += mc146818_read(NULL, i); /* XXX softc */
221 1.1 bouyer
222 1.1 bouyer return (cksum == (mc146818_read(NULL, 0x2e) << 8)
223 1.1 bouyer + mc146818_read(NULL, 0x2f));
224 1.1 bouyer }
225 1.1 bouyer
226 1.1 bouyer #if NMCA > 0
227 1.1 bouyer /*
228 1.1 bouyer * Check whether the CMOS layout is PS/2 like, to be called at splclock().
229 1.1 bouyer */
230 1.1 bouyer static int cmoscheckps2(void);
231 1.1 bouyer static int
232 1.1 bouyer cmoscheckps2(void)
233 1.1 bouyer {
234 1.1 bouyer #if 0
235 1.1 bouyer /* Disabled until I find out the CRC checksum algorithm IBM uses */
236 1.1 bouyer int i;
237 1.1 bouyer unsigned short cksum = 0;
238 1.1 bouyer
239 1.1 bouyer for (i = 0x10; i <= 0x31; i++)
240 1.1 bouyer cksum += mc146818_read(NULL, i); /* XXX softc */
241 1.1 bouyer
242 1.1 bouyer return (cksum == (mc146818_read(NULL, 0x32) << 8)
243 1.1 bouyer + mc146818_read(NULL, 0x33));
244 1.1 bouyer #else
245 1.1 bouyer /* Check 'incorrect checksum' bit of IBM PS/2 Diagnostic Status Byte */
246 1.1 bouyer return ((mc146818_read(NULL, NVRAM_DIAG) & (1<<6)) == 0);
247 1.1 bouyer #endif
248 1.1 bouyer }
249 1.1 bouyer #endif /* NMCA > 0 */
250 1.1 bouyer
251 1.1 bouyer /*
252 1.1 bouyer * patchable to control century byte handling:
253 1.1 bouyer * 1: always update
254 1.1 bouyer * -1: never touch
255 1.1 bouyer * 0: try to figure out itself
256 1.1 bouyer */
257 1.1 bouyer int rtc_update_century = 0;
258 1.1 bouyer
259 1.1 bouyer /*
260 1.1 bouyer * Expand a two-digit year as read from the clock chip
261 1.1 bouyer * into full width.
262 1.1 bouyer * Being here, deal with the CMOS century byte.
263 1.1 bouyer */
264 1.1 bouyer static int centb = NVRAM_CENTURY;
265 1.1 bouyer static int
266 1.1 bouyer clock_expandyear(int clockyear)
267 1.1 bouyer {
268 1.1 bouyer int s, clockcentury, cmoscentury;
269 1.1 bouyer
270 1.1 bouyer clockcentury = (clockyear < 70) ? 20 : 19;
271 1.1 bouyer clockyear += 100 * clockcentury;
272 1.1 bouyer
273 1.1 bouyer if (rtc_update_century < 0)
274 1.1 bouyer return (clockyear);
275 1.1 bouyer
276 1.1 bouyer s = splclock();
277 1.2 jakllsch #if NACPICA > 0
278 1.3 jakllsch if (acpi_active && AcpiGbl_FADT.Century >= MC_NVRAM_START &&
279 1.3 jakllsch AcpiGbl_FADT.Century < (MC_NVRAM_START + MC_NVRAM_SIZE))
280 1.2 jakllsch cmoscentury = mc146818_read(NULL,
281 1.2 jakllsch (centb = AcpiGbl_FADT.Century));
282 1.2 jakllsch else
283 1.2 jakllsch #endif
284 1.1 bouyer if (cmoscheck())
285 1.1 bouyer cmoscentury = mc146818_read(NULL, NVRAM_CENTURY);
286 1.1 bouyer #if NMCA > 0
287 1.1 bouyer else if (MCA_system && cmoscheckps2())
288 1.1 bouyer cmoscentury = mc146818_read(NULL, (centb = 0x37));
289 1.1 bouyer #endif
290 1.1 bouyer else
291 1.1 bouyer cmoscentury = 0;
292 1.1 bouyer splx(s);
293 1.1 bouyer if (!cmoscentury) {
294 1.1 bouyer #ifdef DIAGNOSTIC
295 1.1 bouyer printf("clock: unknown CMOS layout\n");
296 1.1 bouyer #endif
297 1.1 bouyer return (clockyear);
298 1.1 bouyer }
299 1.1 bouyer cmoscentury = bcdtobin(cmoscentury);
300 1.1 bouyer
301 1.1 bouyer if (cmoscentury != clockcentury) {
302 1.1 bouyer /* XXX note: saying "century is 20" might confuse the naive. */
303 1.1 bouyer printf("WARNING: NVRAM century is %d but RTC year is %d\n",
304 1.1 bouyer cmoscentury, clockyear);
305 1.1 bouyer
306 1.1 bouyer /* Kludge to roll over century. */
307 1.1 bouyer if ((rtc_update_century > 0) ||
308 1.1 bouyer ((cmoscentury == 19) && (clockcentury == 20) &&
309 1.1 bouyer (clockyear == 2000))) {
310 1.1 bouyer printf("WARNING: Setting NVRAM century to %d\n",
311 1.1 bouyer clockcentury);
312 1.1 bouyer s = splclock();
313 1.1 bouyer mc146818_write(NULL, centb, bintobcd(clockcentury));
314 1.1 bouyer splx(s);
315 1.1 bouyer }
316 1.1 bouyer } else if (cmoscentury == 19 && rtc_update_century == 0)
317 1.1 bouyer rtc_update_century = 1; /* will update later in resettodr() */
318 1.1 bouyer
319 1.1 bouyer return (clockyear);
320 1.1 bouyer }
321 1.1 bouyer
322 1.1 bouyer int
323 1.1 bouyer rtc_get_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
324 1.1 bouyer {
325 1.1 bouyer int s;
326 1.1 bouyer mc_todregs rtclk;
327 1.1 bouyer
328 1.1 bouyer s = splclock();
329 1.1 bouyer if (rtcget(&rtclk)) {
330 1.1 bouyer splx(s);
331 1.1 bouyer return -1;
332 1.1 bouyer }
333 1.1 bouyer splx(s);
334 1.1 bouyer
335 1.1 bouyer dt->dt_sec = bcdtobin(rtclk[MC_SEC]);
336 1.1 bouyer dt->dt_min = bcdtobin(rtclk[MC_MIN]);
337 1.1 bouyer dt->dt_hour = bcdtobin(rtclk[MC_HOUR]);
338 1.1 bouyer dt->dt_day = bcdtobin(rtclk[MC_DOM]);
339 1.1 bouyer dt->dt_mon = bcdtobin(rtclk[MC_MONTH]);
340 1.1 bouyer dt->dt_year = clock_expandyear(bcdtobin(rtclk[MC_YEAR]));
341 1.1 bouyer
342 1.1 bouyer return 0;
343 1.1 bouyer }
344 1.1 bouyer
345 1.1 bouyer int
346 1.1 bouyer rtc_set_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
347 1.1 bouyer {
348 1.1 bouyer mc_todregs rtclk;
349 1.1 bouyer int century;
350 1.1 bouyer int s;
351 1.1 bouyer
352 1.1 bouyer s = splclock();
353 1.1 bouyer if (rtcget(&rtclk))
354 1.1 bouyer memset(&rtclk, 0, sizeof(rtclk));
355 1.1 bouyer splx(s);
356 1.1 bouyer
357 1.1 bouyer rtclk[MC_SEC] = bintobcd(dt->dt_sec);
358 1.1 bouyer rtclk[MC_MIN] = bintobcd(dt->dt_min);
359 1.1 bouyer rtclk[MC_HOUR] = bintobcd(dt->dt_hour);
360 1.1 bouyer rtclk[MC_DOW] = dt->dt_wday + 1;
361 1.1 bouyer rtclk[MC_YEAR] = bintobcd(dt->dt_year % 100);
362 1.1 bouyer rtclk[MC_MONTH] = bintobcd(dt->dt_mon);
363 1.1 bouyer rtclk[MC_DOM] = bintobcd(dt->dt_day);
364 1.1 bouyer
365 1.1 bouyer #ifdef DEBUG_CLOCK
366 1.1 bouyer printf("setclock: %x/%x/%x %x:%x:%x\n", rtclk[MC_YEAR], rtclk[MC_MONTH],
367 1.1 bouyer rtclk[MC_DOM], rtclk[MC_HOUR], rtclk[MC_MIN], rtclk[MC_SEC]);
368 1.1 bouyer #endif
369 1.1 bouyer s = splclock();
370 1.1 bouyer rtcput(&rtclk);
371 1.1 bouyer if (rtc_update_century > 0) {
372 1.1 bouyer century = bintobcd(dt->dt_year / 100);
373 1.1 bouyer mc146818_write(NULL, centb, century); /* XXX softc */
374 1.1 bouyer }
375 1.1 bouyer splx(s);
376 1.1 bouyer return 0;
377 1.1 bouyer
378 1.1 bouyer }
379 1.1 bouyer
380 1.1 bouyer void
381 1.1 bouyer rtc_register(void)
382 1.1 bouyer {
383 1.1 bouyer static struct todr_chip_handle tch;
384 1.1 bouyer tch.todr_gettime_ymdhms = rtc_get_ymdhms;
385 1.1 bouyer tch.todr_settime_ymdhms = rtc_set_ymdhms;
386 1.1 bouyer tch.todr_setwen = NULL;
387 1.1 bouyer
388 1.1 bouyer todr_attach(&tch);
389 1.1 bouyer }
390 1.1 bouyer
391