xref: /freebsd/sys/x86/isa/atrtc.c (revision df615735960370255d3acc4ac2a6f4fd297b7461)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Poul-Henning Kamp
5  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_acpi.h"
32 #include "opt_isa.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/clock.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/kdb.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/proc.h>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46 #include <sys/timeet.h>
47 
48 #include <isa/rtc.h>
49 #ifdef DEV_ISA
50 #include <isa/isareg.h>
51 #include <isa/isavar.h>
52 #endif
53 #include <machine/intr_machdep.h>
54 #include "clock_if.h"
55 #ifdef DEV_ACPI
56 #include <contrib/dev/acpica/include/acpi.h>
57 #include <contrib/dev/acpica/include/accommon.h>
58 #include <dev/acpica/acpivar.h>
59 #include <machine/md_var.h>
60 #endif
61 
62 /* tunable to detect a power loss of the rtc */
63 static bool atrtc_power_lost = false;
64 SYSCTL_BOOL(_machdep, OID_AUTO, atrtc_power_lost, CTLFLAG_RD, &atrtc_power_lost,
65     false, "RTC lost power on last power cycle (probably caused by an empty cmos battery)");
66 
67 /*
68  * atrtc_lock protects low-level access to individual hardware registers.
69  * atrtc_time_lock protects the entire sequence of accessing multiple registers
70  * to read or write the date and time.
71  */
72 static struct mtx atrtc_lock;
73 MTX_SYSINIT(atrtc_lock_init, &atrtc_lock, "atrtc", MTX_SPIN);
74 
75 /* Force RTC enabled/disabled. */
76 static int atrtc_enabled = -1;
77 TUNABLE_INT("hw.atrtc.enabled", &atrtc_enabled);
78 
79 struct mtx atrtc_time_lock;
80 MTX_SYSINIT(atrtc_time_lock_init, &atrtc_time_lock, "atrtc_time", MTX_DEF);
81 
82 int	atrtcclock_disable = 0;
83 
84 static	int	rtc_century = 0;
85 static	int	rtc_reg = -1;
86 static	u_char	rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
87 static	u_char	rtc_statusb = RTCSB_24HR;
88 
89 #ifdef DEV_ACPI
90 #define	_COMPONENT	ACPI_TIMER
91 ACPI_MODULE_NAME("ATRTC")
92 #endif
93 
94 /*
95  * RTC support routines
96  */
97 
98 static inline u_char
rtcin_locked(int reg)99 rtcin_locked(int reg)
100 {
101 
102 	if (rtc_reg != reg) {
103 		inb(0x84);
104 		outb(IO_RTC, reg);
105 		rtc_reg = reg;
106 		inb(0x84);
107 	}
108 	return (inb(IO_RTC + 1));
109 }
110 
111 static inline void
rtcout_locked(int reg,u_char val)112 rtcout_locked(int reg, u_char val)
113 {
114 
115 	if (rtc_reg != reg) {
116 		inb(0x84);
117 		outb(IO_RTC, reg);
118 		rtc_reg = reg;
119 		inb(0x84);
120 	}
121 	outb(IO_RTC + 1, val);
122 	inb(0x84);
123 }
124 
125 int
rtcin(int reg)126 rtcin(int reg)
127 {
128 	u_char val;
129 
130 	mtx_lock_spin(&atrtc_lock);
131 	val = rtcin_locked(reg);
132 	mtx_unlock_spin(&atrtc_lock);
133 	return (val);
134 }
135 
136 void
writertc(int reg,u_char val)137 writertc(int reg, u_char val)
138 {
139 
140 	mtx_lock_spin(&atrtc_lock);
141 	rtcout_locked(reg, val);
142 	mtx_unlock_spin(&atrtc_lock);
143 }
144 
145 static void
atrtc_start(void)146 atrtc_start(void)
147 {
148 
149 	mtx_lock_spin(&atrtc_lock);
150 	rtcout_locked(RTC_STATUSA, rtc_statusa);
151 	rtcout_locked(RTC_STATUSB, RTCSB_24HR);
152 	mtx_unlock_spin(&atrtc_lock);
153 }
154 
155 static void
atrtc_rate(unsigned rate)156 atrtc_rate(unsigned rate)
157 {
158 
159 	rtc_statusa = RTCSA_DIVIDER | rate;
160 	writertc(RTC_STATUSA, rtc_statusa);
161 }
162 
163 static void
atrtc_enable_intr(void)164 atrtc_enable_intr(void)
165 {
166 
167 	rtc_statusb |= RTCSB_PINTR;
168 	mtx_lock_spin(&atrtc_lock);
169 	rtcout_locked(RTC_STATUSB, rtc_statusb);
170 	rtcin_locked(RTC_INTR);
171 	mtx_unlock_spin(&atrtc_lock);
172 }
173 
174 static void
atrtc_disable_intr(void)175 atrtc_disable_intr(void)
176 {
177 
178 	rtc_statusb &= ~RTCSB_PINTR;
179 	mtx_lock_spin(&atrtc_lock);
180 	rtcout_locked(RTC_STATUSB, rtc_statusb);
181 	rtcin_locked(RTC_INTR);
182 	mtx_unlock_spin(&atrtc_lock);
183 }
184 
185 void
atrtc_restore(void)186 atrtc_restore(void)
187 {
188 
189 	/* Restore all of the RTC's "status" (actually, control) registers. */
190 	mtx_lock_spin(&atrtc_lock);
191 	rtcin_locked(RTC_STATUSA);	/* dummy to get rtc_reg set */
192 	rtcout_locked(RTC_STATUSB, RTCSB_24HR);
193 	rtcout_locked(RTC_STATUSA, rtc_statusa);
194 	rtcout_locked(RTC_STATUSB, rtc_statusb);
195 	rtcin_locked(RTC_INTR);
196 	mtx_unlock_spin(&atrtc_lock);
197 }
198 
199 /**********************************************************************
200  * RTC driver for subr_rtc
201  */
202 
203 struct atrtc_softc {
204 	int port_rid, intr_rid;
205 	struct resource *port_res;
206 	struct resource *intr_res;
207 	void *intr_handler;
208 	struct eventtimer et;
209 #ifdef DEV_ACPI
210 	ACPI_HANDLE acpi_handle;
211 #endif
212 };
213 
214 static int
rtc_start(struct eventtimer * et,sbintime_t first,sbintime_t period)215 rtc_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
216 {
217 
218 	atrtc_rate(max(fls(period + (period >> 1)) - 17, 1));
219 	atrtc_enable_intr();
220 	return (0);
221 }
222 
223 static int
rtc_stop(struct eventtimer * et)224 rtc_stop(struct eventtimer *et)
225 {
226 
227 	atrtc_disable_intr();
228 	return (0);
229 }
230 
231 /*
232  * This routine receives statistical clock interrupts from the RTC.
233  * As explained above, these occur at 128 interrupts per second.
234  * When profiling, we receive interrupts at a rate of 1024 Hz.
235  *
236  * This does not actually add as much overhead as it sounds, because
237  * when the statistical clock is active, the hardclock driver no longer
238  * needs to keep (inaccurate) statistics on its own.  This decouples
239  * statistics gathering from scheduling interrupts.
240  *
241  * The RTC chip requires that we read status register C (RTC_INTR)
242  * to acknowledge an interrupt, before it will generate the next one.
243  * Under high interrupt load, rtcintr() can be indefinitely delayed and
244  * the clock can tick immediately after the read from RTC_INTR.  In this
245  * case, the mc146818A interrupt signal will not drop for long enough
246  * to register with the 8259 PIC.  If an interrupt is missed, the stat
247  * clock will halt, considerably degrading system performance.  This is
248  * why we use 'while' rather than a more straightforward 'if' below.
249  * Stat clock ticks can still be lost, causing minor loss of accuracy
250  * in the statistics, but the stat clock will no longer stop.
251  */
252 static int
rtc_intr(void * arg)253 rtc_intr(void *arg)
254 {
255 	struct atrtc_softc *sc = (struct atrtc_softc *)arg;
256 	int flag = 0;
257 
258 	while (rtcin(RTC_INTR) & RTCIR_PERIOD) {
259 		flag = 1;
260 		if (sc->et.et_active)
261 			sc->et.et_event_cb(&sc->et, sc->et.et_arg);
262 	}
263 	return(flag ? FILTER_HANDLED : FILTER_STRAY);
264 }
265 
266 #ifdef DEV_ACPI
267 /*
268  *  ACPI RTC CMOS address space handler
269  */
270 #define	ATRTC_LAST_REG	0x40
271 
272 static void
rtcin_region(int reg,void * buf,int len)273 rtcin_region(int reg, void *buf, int len)
274 {
275 	u_char *ptr = buf;
276 
277 	/* Drop lock after each IO as intr and settime have greater priority */
278 	while (len-- > 0)
279 		*ptr++ = rtcin(reg++) & 0xff;
280 }
281 
282 static void
rtcout_region(int reg,const void * buf,int len)283 rtcout_region(int reg, const void *buf, int len)
284 {
285 	const u_char *ptr = buf;
286 
287 	while (len-- > 0)
288 		writertc(reg++, *ptr++);
289 }
290 
291 static bool
atrtc_check_cmos_access(bool is_read,ACPI_PHYSICAL_ADDRESS addr,UINT32 len)292 atrtc_check_cmos_access(bool is_read, ACPI_PHYSICAL_ADDRESS addr, UINT32 len)
293 {
294 
295 	/* Block address space wrapping on out-of-bound access */
296 	if (addr >= ATRTC_LAST_REG || addr + len > ATRTC_LAST_REG)
297 		return (false);
298 
299 	if (is_read) {
300 		/* Reading 0x0C will muck with interrupts */
301 		if (addr <= RTC_INTR && addr + len > RTC_INTR)
302 			return (false);
303 	} else {
304 		/*
305 		 * Allow single-byte writes to alarm registers and
306 		 * multi-byte writes to addr >= 0x30, else deny.
307 		 */
308 		if (!((len == 1 && (addr == RTC_SECALRM ||
309 				    addr == RTC_MINALRM ||
310 				    addr == RTC_HRSALRM)) ||
311 		      addr >= 0x30))
312 			return (false);
313 	}
314 	return (true);
315 }
316 
317 static ACPI_STATUS
atrtc_acpi_cmos_handler(UINT32 func,ACPI_PHYSICAL_ADDRESS addr,UINT32 bitwidth,UINT64 * value,void * context,void * region_context)318 atrtc_acpi_cmos_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS addr,
319     UINT32 bitwidth, UINT64 *value, void *context, void *region_context)
320 {
321 	device_t dev = context;
322 	UINT32 bytewidth = howmany(bitwidth, 8);
323 	bool is_read = func == ACPI_READ;
324 
325 	/* ACPICA is very verbose on CMOS handler failures, so we, too */
326 #define	CMOS_HANDLER_ERR(fmt, ...) \
327 	device_printf(dev, "ACPI [SystemCMOS] handler: " fmt, ##__VA_ARGS__)
328 
329 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
330 
331 	if (value == NULL) {
332 		CMOS_HANDLER_ERR("NULL parameter\n");
333 		return (AE_BAD_PARAMETER);
334 	}
335 	if (bitwidth == 0 || (bitwidth & 0x07) != 0) {
336 		CMOS_HANDLER_ERR("Invalid bitwidth: %u\n", bitwidth);
337 		return (AE_BAD_PARAMETER);
338 	}
339 	if (!atrtc_check_cmos_access(is_read, addr, bytewidth)) {
340 		CMOS_HANDLER_ERR("%s access rejected: addr=%#04jx, len=%u\n",
341 		    is_read ? "Read" : "Write", (uintmax_t)addr, bytewidth);
342 		return (AE_BAD_PARAMETER);
343 	}
344 
345 	switch (func) {
346 	case ACPI_READ:
347 		rtcin_region(addr, value, bytewidth);
348 		break;
349 	case ACPI_WRITE:
350 		rtcout_region(addr, value, bytewidth);
351 		break;
352 	default:
353 		CMOS_HANDLER_ERR("Invalid function: %u\n", func);
354 		return (AE_BAD_PARAMETER);
355 	}
356 
357 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
358 	    "ACPI RTC CMOS %s access: addr=%#04x, len=%u, val=%*D\n",
359 	    is_read ? "read" : "write", (unsigned)addr, bytewidth,
360 	    bytewidth, value, " ");
361 
362 	return (AE_OK);
363 }
364 
365 static int
atrtc_reg_acpi_cmos_handler(device_t dev)366 atrtc_reg_acpi_cmos_handler(device_t dev)
367 {
368 	struct atrtc_softc *sc = device_get_softc(dev);
369 
370 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
371 
372 	/* Don't handle address space events if driver is disabled. */
373 	if (acpi_disabled("atrtc"))
374 		return (ENXIO);
375 
376 	if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sc->acpi_handle))) {
377 		return (ENXIO);
378 	}
379 
380 	if (sc->acpi_handle == NULL ||
381 	    ACPI_FAILURE(AcpiInstallAddressSpaceHandler(sc->acpi_handle,
382 	      ACPI_ADR_SPACE_CMOS, atrtc_acpi_cmos_handler, NULL, dev))) {
383 		sc->acpi_handle = NULL;
384 		device_printf(dev,
385 		    "Can't register ACPI CMOS address space handler\n");
386 		return (ENXIO);
387         }
388 
389         return (0);
390 }
391 
392 static int
atrtc_unreg_acpi_cmos_handler(device_t dev)393 atrtc_unreg_acpi_cmos_handler(device_t dev)
394 {
395 	struct atrtc_softc *sc = device_get_softc(dev);
396 
397 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
398 
399 	if (sc->acpi_handle != NULL)
400 		AcpiRemoveAddressSpaceHandler(sc->acpi_handle,
401 		    ACPI_ADR_SPACE_CMOS, atrtc_acpi_cmos_handler);
402 
403 	return (0);
404 }
405 #endif	/* DEV_ACPI */
406 
407 /*
408  * Attach to the ISA PnP descriptors for the timer and realtime clock.
409  */
410 static struct isa_pnp_id atrtc_ids[] = {
411 	{ 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
412 	{ 0 }
413 };
414 
415 static bool
atrtc_acpi_disabled(void)416 atrtc_acpi_disabled(void)
417 {
418 #ifdef DEV_ACPI
419 	uint16_t flags;
420 
421 	if (!acpi_get_fadt_bootflags(&flags))
422 		return (false);
423 	return ((flags & ACPI_FADT_NO_CMOS_RTC) != 0);
424 #else
425 	return (false);
426 #endif
427 }
428 
429 static int
rtc_acpi_century_get(void)430 rtc_acpi_century_get(void)
431 {
432 #ifdef DEV_ACPI
433 	ACPI_TABLE_FADT *fadt;
434 	vm_paddr_t physaddr;
435 	int century;
436 
437 	physaddr = acpi_find_table(ACPI_SIG_FADT);
438 	if (physaddr == 0)
439 		return (0);
440 
441 	fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
442 	if (fadt == NULL)
443 		return (0);
444 
445 	century = fadt->Century;
446 	acpi_unmap_table(fadt);
447 
448 	return (century);
449 #else
450 	return (0);
451 #endif
452 }
453 
454 static int
atrtc_probe(device_t dev)455 atrtc_probe(device_t dev)
456 {
457 	int result;
458 
459 	if ((atrtc_enabled == -1 && atrtc_acpi_disabled()) ||
460 	    (atrtc_enabled == 0))
461 		return (ENXIO);
462 
463 	result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
464 	/* ENOENT means no PnP-ID, device is hinted. */
465 	if (result == ENOENT) {
466 		device_set_desc(dev, "AT realtime clock");
467 		return (BUS_PROBE_LOW_PRIORITY);
468 	}
469 	rtc_century = rtc_acpi_century_get();
470 	return (result);
471 }
472 
473 static int
atrtc_attach(device_t dev)474 atrtc_attach(device_t dev)
475 {
476 	struct atrtc_softc *sc;
477 	rman_res_t s;
478 	int i;
479 
480 	sc = device_get_softc(dev);
481 	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
482 	    IO_RTC, IO_RTC + 1, 2, RF_ACTIVE);
483 	if (sc->port_res == NULL)
484 		device_printf(dev, "Warning: Couldn't map I/O.\n");
485 	atrtc_start();
486 	clock_register(dev, 1000000);
487 	bzero(&sc->et, sizeof(struct eventtimer));
488 	if (!atrtcclock_disable &&
489 	    (resource_int_value(device_get_name(dev), device_get_unit(dev),
490 	     "clock", &i) != 0 || i != 0)) {
491 		sc->intr_rid = 0;
492 		while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
493 		    &s, NULL) == 0 && s != 8)
494 			sc->intr_rid++;
495 		sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
496 		    &sc->intr_rid, 8, 8, 1, RF_ACTIVE);
497 		if (sc->intr_res == NULL) {
498 			device_printf(dev, "Can't map interrupt.\n");
499 			return (0);
500 		} else if ((bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
501 		    rtc_intr, NULL, sc, &sc->intr_handler))) {
502 			device_printf(dev, "Can't setup interrupt.\n");
503 			return (0);
504 		} else {
505 			/* Bind IRQ to BSP to avoid live migration. */
506 			bus_bind_intr(dev, sc->intr_res, 0);
507 		}
508 		sc->et.et_name = "RTC";
509 		sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_POW2DIV;
510 		sc->et.et_quality = 0;
511 		sc->et.et_frequency = 32768;
512 		sc->et.et_min_period = 0x00080000;
513 		sc->et.et_max_period = 0x80000000;
514 		sc->et.et_start = rtc_start;
515 		sc->et.et_stop = rtc_stop;
516 		sc->et.et_priv = dev;
517 		et_register(&sc->et);
518 	}
519 	return(0);
520 }
521 
522 static int
atrtc_isa_attach(device_t dev)523 atrtc_isa_attach(device_t dev)
524 {
525 
526 	return (atrtc_attach(dev));
527 }
528 
529 #ifdef DEV_ACPI
530 static int
atrtc_acpi_attach(device_t dev)531 atrtc_acpi_attach(device_t dev)
532 {
533 	int ret;
534 
535 	ret = atrtc_attach(dev);
536 	if (ret)
537 		return (ret);
538 
539 	(void)atrtc_reg_acpi_cmos_handler(dev);
540 
541 	return (0);
542 }
543 
544 static int
atrtc_acpi_detach(device_t dev)545 atrtc_acpi_detach(device_t dev)
546 {
547 
548 	(void)atrtc_unreg_acpi_cmos_handler(dev);
549 	return (0);
550 }
551 #endif	/* DEV_ACPI */
552 
553 static int
atrtc_resume(device_t dev)554 atrtc_resume(device_t dev)
555 {
556 
557 	atrtc_restore();
558 	return(0);
559 }
560 
561 static int
atrtc_settime(device_t dev __unused,struct timespec * ts)562 atrtc_settime(device_t dev __unused, struct timespec *ts)
563 {
564 	struct bcd_clocktime bct;
565 
566 	clock_ts_to_bcd(ts, &bct, false);
567 	clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bct);
568 
569 	mtx_lock(&atrtc_time_lock);
570 	mtx_lock_spin(&atrtc_lock);
571 
572 	/* Disable RTC updates and interrupts.  */
573 	rtcout_locked(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
574 
575 	/* Write all the time registers. */
576 	rtcout_locked(RTC_SEC,   bct.sec);
577 	rtcout_locked(RTC_MIN,   bct.min);
578 	rtcout_locked(RTC_HRS,   bct.hour);
579 	rtcout_locked(RTC_WDAY,  bct.dow + 1);
580 	rtcout_locked(RTC_DAY,   bct.day);
581 	rtcout_locked(RTC_MONTH, bct.mon);
582 	rtcout_locked(RTC_YEAR,  bct.year & 0xff);
583 	if (rtc_century)
584 		rtcout_locked(rtc_century, bct.year >> 8);
585 
586 	/*
587 	 * Re-enable RTC updates and interrupts.
588 	 */
589 	rtcout_locked(RTC_STATUSB, rtc_statusb);
590 	rtcin_locked(RTC_INTR);
591 
592 	mtx_unlock_spin(&atrtc_lock);
593 	mtx_unlock(&atrtc_time_lock);
594 
595 	return (0);
596 }
597 
598 static int
atrtc_gettime(device_t dev,struct timespec * ts)599 atrtc_gettime(device_t dev, struct timespec *ts)
600 {
601 	struct bcd_clocktime bct;
602 
603 	/* Look if we have a RTC present and the time is valid */
604 	if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
605 		atrtc_power_lost = true;
606 		device_printf(dev, "WARNING: Battery failure indication\n");
607 		return (EINVAL);
608 	}
609 
610 	/*
611 	 * wait for time update to complete
612 	 * If RTCSA_TUP is zero, we have at least 244us before next update.
613 	 * This is fast enough on most hardware, but a refinement would be
614 	 * to make sure that no more than 240us pass after we start reading,
615 	 * and try again if so.
616 	 */
617 	mtx_lock(&atrtc_time_lock);
618 	while (rtcin(RTC_STATUSA) & RTCSA_TUP)
619 		continue;
620 	mtx_lock_spin(&atrtc_lock);
621 	bct.sec  = rtcin_locked(RTC_SEC);
622 	bct.min  = rtcin_locked(RTC_MIN);
623 	bct.hour = rtcin_locked(RTC_HRS);
624 	bct.day  = rtcin_locked(RTC_DAY);
625 	bct.mon  = rtcin_locked(RTC_MONTH);
626 	bct.year = rtcin_locked(RTC_YEAR);
627 	if (rtc_century)
628 		bct.year |= rtcin_locked(rtc_century) << 8;
629 	mtx_unlock_spin(&atrtc_lock);
630 	mtx_unlock(&atrtc_time_lock);
631 	/* dow is unused in timespec conversion and we have no nsec info. */
632 	bct.dow  = 0;
633 	bct.nsec = 0;
634 	clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bct);
635 	return (clock_bcd_to_ts(&bct, ts, false));
636 }
637 
638 static device_method_t atrtc_isa_methods[] = {
639 	/* Device interface */
640 	DEVMETHOD(device_probe,		atrtc_probe),
641 	DEVMETHOD(device_attach,	atrtc_isa_attach),
642 		/* XXX stop statclock? */
643 	DEVMETHOD(device_resume,	atrtc_resume),
644 
645 	/* clock interface */
646 	DEVMETHOD(clock_gettime,	atrtc_gettime),
647 	DEVMETHOD(clock_settime,	atrtc_settime),
648 	{ 0, 0 }
649 };
650 
651 static driver_t atrtc_isa_driver = {
652 	"atrtc",
653 	atrtc_isa_methods,
654 	sizeof(struct atrtc_softc),
655 };
656 
657 #ifdef DEV_ACPI
658 static device_method_t atrtc_acpi_methods[] = {
659 	/* Device interface */
660 	DEVMETHOD(device_probe,		atrtc_probe),
661 	DEVMETHOD(device_attach,	atrtc_acpi_attach),
662 	DEVMETHOD(device_detach,	atrtc_acpi_detach),
663 		/* XXX stop statclock? */
664 	DEVMETHOD(device_resume,	atrtc_resume),
665 
666 	/* clock interface */
667 	DEVMETHOD(clock_gettime,	atrtc_gettime),
668 	DEVMETHOD(clock_settime,	atrtc_settime),
669 	{ 0, 0 }
670 };
671 
672 static driver_t atrtc_acpi_driver = {
673 	"atrtc",
674 	atrtc_acpi_methods,
675 	sizeof(struct atrtc_softc),
676 };
677 #endif	/* DEV_ACPI */
678 
679 DRIVER_MODULE(atrtc, isa, atrtc_isa_driver, 0, 0);
680 #ifdef DEV_ACPI
681 DRIVER_MODULE(atrtc, acpi, atrtc_acpi_driver, 0, 0);
682 #endif
683 ISA_PNP_INFO(atrtc_ids);
684