2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Poul-Henning Kamp
5 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/clock.h>
43 #include <sys/mutex.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
49 #include <sys/timeet.h>
53 #include <isa/isareg.h>
54 #include <isa/isavar.h>
56 #include <machine/intr_machdep.h>
59 #include <contrib/dev/acpica/include/acpi.h>
60 #include <contrib/dev/acpica/include/accommon.h>
61 #include <dev/acpica/acpivar.h>
62 #include <machine/md_var.h>
66 * atrtc_lock protects low-level access to individual hardware registers.
67 * atrtc_time_lock protects the entire sequence of accessing multiple registers
68 * to read or write the date and time.
70 static struct mtx atrtc_lock;
71 MTX_SYSINIT(atrtc_lock_init, &atrtc_lock, "atrtc", MTX_SPIN);
73 /* Force RTC enabled/disabled. */
74 static int atrtc_enabled = -1;
75 TUNABLE_INT("hw.atrtc.enabled", &atrtc_enabled);
77 struct mtx atrtc_time_lock;
78 MTX_SYSINIT(atrtc_time_lock_init, &atrtc_time_lock, "atrtc_time", MTX_DEF);
80 int atrtcclock_disable = 0;
82 static int rtc_reg = -1;
83 static u_char rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
84 static u_char rtc_statusb = RTCSB_24HR;
87 #define _COMPONENT ACPI_TIMER
88 ACPI_MODULE_NAME("ATRTC")
92 * RTC support routines
105 return (inb(IO_RTC + 1));
109 rtcout_locked(int reg, u_char val)
112 if (rtc_reg != reg) {
118 outb(IO_RTC + 1, val);
127 mtx_lock_spin(&atrtc_lock);
128 val = rtcin_locked(reg);
129 mtx_unlock_spin(&atrtc_lock);
134 writertc(int reg, u_char val)
137 mtx_lock_spin(&atrtc_lock);
138 rtcout_locked(reg, val);
139 mtx_unlock_spin(&atrtc_lock);
146 mtx_lock_spin(&atrtc_lock);
147 rtcout_locked(RTC_STATUSA, rtc_statusa);
148 rtcout_locked(RTC_STATUSB, RTCSB_24HR);
149 mtx_unlock_spin(&atrtc_lock);
153 atrtc_rate(unsigned rate)
156 rtc_statusa = RTCSA_DIVIDER | rate;
157 writertc(RTC_STATUSA, rtc_statusa);
161 atrtc_enable_intr(void)
164 rtc_statusb |= RTCSB_PINTR;
165 mtx_lock_spin(&atrtc_lock);
166 rtcout_locked(RTC_STATUSB, rtc_statusb);
167 rtcin_locked(RTC_INTR);
168 mtx_unlock_spin(&atrtc_lock);
172 atrtc_disable_intr(void)
175 rtc_statusb &= ~RTCSB_PINTR;
176 mtx_lock_spin(&atrtc_lock);
177 rtcout_locked(RTC_STATUSB, rtc_statusb);
178 rtcin_locked(RTC_INTR);
179 mtx_unlock_spin(&atrtc_lock);
186 /* Restore all of the RTC's "status" (actually, control) registers. */
187 mtx_lock_spin(&atrtc_lock);
188 rtcin_locked(RTC_STATUSA); /* dummy to get rtc_reg set */
189 rtcout_locked(RTC_STATUSB, RTCSB_24HR);
190 rtcout_locked(RTC_STATUSA, rtc_statusa);
191 rtcout_locked(RTC_STATUSB, rtc_statusb);
192 rtcin_locked(RTC_INTR);
193 mtx_unlock_spin(&atrtc_lock);
196 /**********************************************************************
197 * RTC driver for subr_rtc
201 int port_rid, intr_rid;
202 struct resource *port_res;
203 struct resource *intr_res;
205 struct eventtimer et;
207 ACPI_HANDLE acpi_handle;
212 rtc_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
215 atrtc_rate(max(fls(period + (period >> 1)) - 17, 1));
221 rtc_stop(struct eventtimer *et)
224 atrtc_disable_intr();
229 * This routine receives statistical clock interrupts from the RTC.
230 * As explained above, these occur at 128 interrupts per second.
231 * When profiling, we receive interrupts at a rate of 1024 Hz.
233 * This does not actually add as much overhead as it sounds, because
234 * when the statistical clock is active, the hardclock driver no longer
235 * needs to keep (inaccurate) statistics on its own. This decouples
236 * statistics gathering from scheduling interrupts.
238 * The RTC chip requires that we read status register C (RTC_INTR)
239 * to acknowledge an interrupt, before it will generate the next one.
240 * Under high interrupt load, rtcintr() can be indefinitely delayed and
241 * the clock can tick immediately after the read from RTC_INTR. In this
242 * case, the mc146818A interrupt signal will not drop for long enough
243 * to register with the 8259 PIC. If an interrupt is missed, the stat
244 * clock will halt, considerably degrading system performance. This is
245 * why we use 'while' rather than a more straightforward 'if' below.
246 * Stat clock ticks can still be lost, causing minor loss of accuracy
247 * in the statistics, but the stat clock will no longer stop.
252 struct atrtc_softc *sc = (struct atrtc_softc *)arg;
255 while (rtcin(RTC_INTR) & RTCIR_PERIOD) {
257 if (sc->et.et_active)
258 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
260 return(flag ? FILTER_HANDLED : FILTER_STRAY);
265 * ACPI RTC CMOS address space handler
267 #define ATRTC_LAST_REG 0x40
270 rtcin_region(int reg, void *buf, int len)
274 /* Drop lock after each IO as intr and settime have greater priority */
276 *ptr++ = rtcin(reg++) & 0xff;
280 rtcout_region(int reg, const void *buf, int len)
282 const u_char *ptr = buf;
285 writertc(reg++, *ptr++);
289 atrtc_check_cmos_access(bool is_read, ACPI_PHYSICAL_ADDRESS addr, UINT32 len)
292 /* Block address space wrapping on out-of-bound access */
293 if (addr >= ATRTC_LAST_REG || addr + len > ATRTC_LAST_REG)
297 /* Reading 0x0C will muck with interrupts */
298 if (addr <= RTC_INTR && addr + len > RTC_INTR)
302 * Allow single-byte writes to alarm registers and
303 * multi-byte writes to addr >= 0x30, else deny.
305 if (!((len == 1 && (addr == RTC_SECALRM ||
306 addr == RTC_MINALRM ||
307 addr == RTC_HRSALRM)) ||
315 atrtc_acpi_cmos_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS addr,
316 UINT32 bitwidth, UINT64 *value, void *context, void *region_context)
318 device_t dev = context;
319 UINT32 bytewidth = howmany(bitwidth, 8);
320 bool is_read = func == ACPI_READ;
322 /* ACPICA is very verbose on CMOS handler failures, so we, too */
323 #define CMOS_HANDLER_ERR(fmt, ...) \
324 device_printf(dev, "ACPI [SystemCMOS] handler: " fmt, ##__VA_ARGS__)
326 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
329 CMOS_HANDLER_ERR("NULL parameter\n");
330 return (AE_BAD_PARAMETER);
332 if (bitwidth == 0 || (bitwidth & 0x07) != 0) {
333 CMOS_HANDLER_ERR("Invalid bitwidth: %u\n", bitwidth);
334 return (AE_BAD_PARAMETER);
336 if (!atrtc_check_cmos_access(is_read, addr, bytewidth)) {
337 CMOS_HANDLER_ERR("%s access rejected: addr=%#04jx, len=%u\n",
338 is_read ? "Read" : "Write", (uintmax_t)addr, bytewidth);
339 return (AE_BAD_PARAMETER);
344 rtcin_region(addr, value, bytewidth);
347 rtcout_region(addr, value, bytewidth);
350 CMOS_HANDLER_ERR("Invalid function: %u\n", func);
351 return (AE_BAD_PARAMETER);
354 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
355 "ACPI RTC CMOS %s access: addr=%#04x, len=%u, val=%*D\n",
356 is_read ? "read" : "write", (unsigned)addr, bytewidth,
357 bytewidth, value, " ");
363 atrtc_reg_acpi_cmos_handler(device_t dev)
365 struct atrtc_softc *sc = device_get_softc(dev);
367 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
369 /* Don't handle address space events if driver is disabled. */
370 if (acpi_disabled("atrtc"))
373 sc->acpi_handle = acpi_get_handle(dev);
374 if (sc->acpi_handle == NULL ||
375 ACPI_FAILURE(AcpiInstallAddressSpaceHandler(sc->acpi_handle,
376 ACPI_ADR_SPACE_CMOS, atrtc_acpi_cmos_handler, NULL, dev))) {
377 sc->acpi_handle = NULL;
379 "Can't register ACPI CMOS address space handler\n");
387 atrtc_unreg_acpi_cmos_handler(device_t dev)
389 struct atrtc_softc *sc = device_get_softc(dev);
391 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
393 if (sc->acpi_handle != NULL)
394 AcpiRemoveAddressSpaceHandler(sc->acpi_handle,
395 ACPI_ADR_SPACE_CMOS, atrtc_acpi_cmos_handler);
399 #endif /* DEV_ACPI */
402 * Attach to the ISA PnP descriptors for the timer and realtime clock.
404 static struct isa_pnp_id atrtc_ids[] = {
405 { 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
410 atrtc_acpi_disabled(void)
415 if (!acpi_get_fadt_bootflags(&flags))
417 return ((flags & ACPI_FADT_NO_CMOS_RTC) != 0);
424 atrtc_probe(device_t dev)
428 if ((atrtc_enabled == -1 && atrtc_acpi_disabled()) ||
429 (atrtc_enabled == 0))
432 result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
433 /* ENOENT means no PnP-ID, device is hinted. */
434 if (result == ENOENT) {
435 device_set_desc(dev, "AT realtime clock");
436 return (BUS_PROBE_LOW_PRIORITY);
442 atrtc_attach(device_t dev)
444 struct atrtc_softc *sc;
448 sc = device_get_softc(dev);
449 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
450 IO_RTC, IO_RTC + 1, 2, RF_ACTIVE);
451 if (sc->port_res == NULL)
452 device_printf(dev, "Warning: Couldn't map I/O.\n");
454 clock_register(dev, 1000000);
455 bzero(&sc->et, sizeof(struct eventtimer));
456 if (!atrtcclock_disable &&
457 (resource_int_value(device_get_name(dev), device_get_unit(dev),
458 "clock", &i) != 0 || i != 0)) {
460 while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
461 &s, NULL) == 0 && s != 8)
463 sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
464 &sc->intr_rid, 8, 8, 1, RF_ACTIVE);
465 if (sc->intr_res == NULL) {
466 device_printf(dev, "Can't map interrupt.\n");
468 } else if ((bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
469 rtc_intr, NULL, sc, &sc->intr_handler))) {
470 device_printf(dev, "Can't setup interrupt.\n");
473 /* Bind IRQ to BSP to avoid live migration. */
474 bus_bind_intr(dev, sc->intr_res, 0);
476 sc->et.et_name = "RTC";
477 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_POW2DIV;
478 sc->et.et_quality = 0;
479 sc->et.et_frequency = 32768;
480 sc->et.et_min_period = 0x00080000;
481 sc->et.et_max_period = 0x80000000;
482 sc->et.et_start = rtc_start;
483 sc->et.et_stop = rtc_stop;
484 sc->et.et_priv = dev;
485 et_register(&sc->et);
491 atrtc_isa_attach(device_t dev)
494 return (atrtc_attach(dev));
499 atrtc_acpi_attach(device_t dev)
503 ret = atrtc_attach(dev);
507 (void)atrtc_reg_acpi_cmos_handler(dev);
513 atrtc_acpi_detach(device_t dev)
516 (void)atrtc_unreg_acpi_cmos_handler(dev);
519 #endif /* DEV_ACPI */
522 atrtc_resume(device_t dev)
530 atrtc_settime(device_t dev __unused, struct timespec *ts)
532 struct bcd_clocktime bct;
534 clock_ts_to_bcd(ts, &bct, false);
535 clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bct);
537 mtx_lock(&atrtc_time_lock);
538 mtx_lock_spin(&atrtc_lock);
540 /* Disable RTC updates and interrupts. */
541 rtcout_locked(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
543 /* Write all the time registers. */
544 rtcout_locked(RTC_SEC, bct.sec);
545 rtcout_locked(RTC_MIN, bct.min);
546 rtcout_locked(RTC_HRS, bct.hour);
547 rtcout_locked(RTC_WDAY, bct.dow + 1);
548 rtcout_locked(RTC_DAY, bct.day);
549 rtcout_locked(RTC_MONTH, bct.mon);
550 rtcout_locked(RTC_YEAR, bct.year & 0xff);
551 #ifdef USE_RTC_CENTURY
552 rtcout_locked(RTC_CENTURY, bct.year >> 8);
556 * Re-enable RTC updates and interrupts.
558 rtcout_locked(RTC_STATUSB, rtc_statusb);
559 rtcin_locked(RTC_INTR);
561 mtx_unlock_spin(&atrtc_lock);
562 mtx_unlock(&atrtc_time_lock);
568 atrtc_gettime(device_t dev, struct timespec *ts)
570 struct bcd_clocktime bct;
572 /* Look if we have a RTC present and the time is valid */
573 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
574 device_printf(dev, "WARNING: Battery failure indication\n");
579 * wait for time update to complete
580 * If RTCSA_TUP is zero, we have at least 244us before next update.
581 * This is fast enough on most hardware, but a refinement would be
582 * to make sure that no more than 240us pass after we start reading,
583 * and try again if so.
585 mtx_lock(&atrtc_time_lock);
586 while (rtcin(RTC_STATUSA) & RTCSA_TUP)
588 mtx_lock_spin(&atrtc_lock);
589 bct.sec = rtcin_locked(RTC_SEC);
590 bct.min = rtcin_locked(RTC_MIN);
591 bct.hour = rtcin_locked(RTC_HRS);
592 bct.day = rtcin_locked(RTC_DAY);
593 bct.mon = rtcin_locked(RTC_MONTH);
594 bct.year = rtcin_locked(RTC_YEAR);
595 #ifdef USE_RTC_CENTURY
596 bct.year |= rtcin_locked(RTC_CENTURY) << 8;
598 mtx_unlock_spin(&atrtc_lock);
599 mtx_unlock(&atrtc_time_lock);
600 /* dow is unused in timespec conversion and we have no nsec info. */
603 clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bct);
604 return (clock_bcd_to_ts(&bct, ts, false));
607 static device_method_t atrtc_isa_methods[] = {
608 /* Device interface */
609 DEVMETHOD(device_probe, atrtc_probe),
610 DEVMETHOD(device_attach, atrtc_isa_attach),
611 DEVMETHOD(device_detach, bus_generic_detach),
612 DEVMETHOD(device_shutdown, bus_generic_shutdown),
613 DEVMETHOD(device_suspend, bus_generic_suspend),
614 /* XXX stop statclock? */
615 DEVMETHOD(device_resume, atrtc_resume),
617 /* clock interface */
618 DEVMETHOD(clock_gettime, atrtc_gettime),
619 DEVMETHOD(clock_settime, atrtc_settime),
623 static driver_t atrtc_isa_driver = {
626 sizeof(struct atrtc_softc),
630 static device_method_t atrtc_acpi_methods[] = {
631 /* Device interface */
632 DEVMETHOD(device_probe, atrtc_probe),
633 DEVMETHOD(device_attach, atrtc_acpi_attach),
634 DEVMETHOD(device_detach, atrtc_acpi_detach),
635 /* XXX stop statclock? */
636 DEVMETHOD(device_resume, atrtc_resume),
638 /* clock interface */
639 DEVMETHOD(clock_gettime, atrtc_gettime),
640 DEVMETHOD(clock_settime, atrtc_settime),
644 static driver_t atrtc_acpi_driver = {
647 sizeof(struct atrtc_softc),
649 #endif /* DEV_ACPI */
651 static devclass_t atrtc_devclass;
653 DRIVER_MODULE(atrtc, isa, atrtc_isa_driver, atrtc_devclass, 0, 0);
655 DRIVER_MODULE(atrtc, acpi, atrtc_acpi_driver, atrtc_devclass, 0, 0);
657 ISA_PNP_INFO(atrtc_ids);