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>
65 /* tunable to detect a power loss of the rtc */
66 static bool atrtc_power_lost = false;
67 SYSCTL_BOOL(_machdep, OID_AUTO, atrtc_power_lost, CTLFLAG_RD, &atrtc_power_lost,
68 false, "RTC lost power on last power cycle (probably caused by an emtpy cmos battery)");
71 * atrtc_lock protects low-level access to individual hardware registers.
72 * atrtc_time_lock protects the entire sequence of accessing multiple registers
73 * to read or write the date and time.
75 static struct mtx atrtc_lock;
76 MTX_SYSINIT(atrtc_lock_init, &atrtc_lock, "atrtc", MTX_SPIN);
78 /* Force RTC enabled/disabled. */
79 static int atrtc_enabled = -1;
80 TUNABLE_INT("hw.atrtc.enabled", &atrtc_enabled);
82 struct mtx atrtc_time_lock;
83 MTX_SYSINIT(atrtc_time_lock_init, &atrtc_time_lock, "atrtc_time", MTX_DEF);
85 int atrtcclock_disable = 0;
87 static int rtc_century = 0;
88 static int rtc_reg = -1;
89 static u_char rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
90 static u_char rtc_statusb = RTCSB_24HR;
93 #define _COMPONENT ACPI_TIMER
94 ACPI_MODULE_NAME("ATRTC")
98 * RTC support routines
102 rtcin_locked(int reg)
105 if (rtc_reg != reg) {
111 return (inb(IO_RTC + 1));
115 rtcout_locked(int reg, u_char val)
118 if (rtc_reg != reg) {
124 outb(IO_RTC + 1, val);
133 mtx_lock_spin(&atrtc_lock);
134 val = rtcin_locked(reg);
135 mtx_unlock_spin(&atrtc_lock);
140 writertc(int reg, u_char val)
143 mtx_lock_spin(&atrtc_lock);
144 rtcout_locked(reg, val);
145 mtx_unlock_spin(&atrtc_lock);
152 mtx_lock_spin(&atrtc_lock);
153 rtcout_locked(RTC_STATUSA, rtc_statusa);
154 rtcout_locked(RTC_STATUSB, RTCSB_24HR);
155 mtx_unlock_spin(&atrtc_lock);
159 atrtc_rate(unsigned rate)
162 rtc_statusa = RTCSA_DIVIDER | rate;
163 writertc(RTC_STATUSA, rtc_statusa);
167 atrtc_enable_intr(void)
170 rtc_statusb |= RTCSB_PINTR;
171 mtx_lock_spin(&atrtc_lock);
172 rtcout_locked(RTC_STATUSB, rtc_statusb);
173 rtcin_locked(RTC_INTR);
174 mtx_unlock_spin(&atrtc_lock);
178 atrtc_disable_intr(void)
181 rtc_statusb &= ~RTCSB_PINTR;
182 mtx_lock_spin(&atrtc_lock);
183 rtcout_locked(RTC_STATUSB, rtc_statusb);
184 rtcin_locked(RTC_INTR);
185 mtx_unlock_spin(&atrtc_lock);
192 /* Restore all of the RTC's "status" (actually, control) registers. */
193 mtx_lock_spin(&atrtc_lock);
194 rtcin_locked(RTC_STATUSA); /* dummy to get rtc_reg set */
195 rtcout_locked(RTC_STATUSB, RTCSB_24HR);
196 rtcout_locked(RTC_STATUSA, rtc_statusa);
197 rtcout_locked(RTC_STATUSB, rtc_statusb);
198 rtcin_locked(RTC_INTR);
199 mtx_unlock_spin(&atrtc_lock);
202 /**********************************************************************
203 * RTC driver for subr_rtc
207 int port_rid, intr_rid;
208 struct resource *port_res;
209 struct resource *intr_res;
211 struct eventtimer et;
213 ACPI_HANDLE acpi_handle;
218 rtc_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
221 atrtc_rate(max(fls(period + (period >> 1)) - 17, 1));
227 rtc_stop(struct eventtimer *et)
230 atrtc_disable_intr();
235 * This routine receives statistical clock interrupts from the RTC.
236 * As explained above, these occur at 128 interrupts per second.
237 * When profiling, we receive interrupts at a rate of 1024 Hz.
239 * This does not actually add as much overhead as it sounds, because
240 * when the statistical clock is active, the hardclock driver no longer
241 * needs to keep (inaccurate) statistics on its own. This decouples
242 * statistics gathering from scheduling interrupts.
244 * The RTC chip requires that we read status register C (RTC_INTR)
245 * to acknowledge an interrupt, before it will generate the next one.
246 * Under high interrupt load, rtcintr() can be indefinitely delayed and
247 * the clock can tick immediately after the read from RTC_INTR. In this
248 * case, the mc146818A interrupt signal will not drop for long enough
249 * to register with the 8259 PIC. If an interrupt is missed, the stat
250 * clock will halt, considerably degrading system performance. This is
251 * why we use 'while' rather than a more straightforward 'if' below.
252 * Stat clock ticks can still be lost, causing minor loss of accuracy
253 * in the statistics, but the stat clock will no longer stop.
258 struct atrtc_softc *sc = (struct atrtc_softc *)arg;
261 while (rtcin(RTC_INTR) & RTCIR_PERIOD) {
263 if (sc->et.et_active)
264 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
266 return(flag ? FILTER_HANDLED : FILTER_STRAY);
271 * ACPI RTC CMOS address space handler
273 #define ATRTC_LAST_REG 0x40
276 rtcin_region(int reg, void *buf, int len)
280 /* Drop lock after each IO as intr and settime have greater priority */
282 *ptr++ = rtcin(reg++) & 0xff;
286 rtcout_region(int reg, const void *buf, int len)
288 const u_char *ptr = buf;
291 writertc(reg++, *ptr++);
295 atrtc_check_cmos_access(bool is_read, ACPI_PHYSICAL_ADDRESS addr, UINT32 len)
298 /* Block address space wrapping on out-of-bound access */
299 if (addr >= ATRTC_LAST_REG || addr + len > ATRTC_LAST_REG)
303 /* Reading 0x0C will muck with interrupts */
304 if (addr <= RTC_INTR && addr + len > RTC_INTR)
308 * Allow single-byte writes to alarm registers and
309 * multi-byte writes to addr >= 0x30, else deny.
311 if (!((len == 1 && (addr == RTC_SECALRM ||
312 addr == RTC_MINALRM ||
313 addr == RTC_HRSALRM)) ||
321 atrtc_acpi_cmos_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS addr,
322 UINT32 bitwidth, UINT64 *value, void *context, void *region_context)
324 device_t dev = context;
325 UINT32 bytewidth = howmany(bitwidth, 8);
326 bool is_read = func == ACPI_READ;
328 /* ACPICA is very verbose on CMOS handler failures, so we, too */
329 #define CMOS_HANDLER_ERR(fmt, ...) \
330 device_printf(dev, "ACPI [SystemCMOS] handler: " fmt, ##__VA_ARGS__)
332 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
335 CMOS_HANDLER_ERR("NULL parameter\n");
336 return (AE_BAD_PARAMETER);
338 if (bitwidth == 0 || (bitwidth & 0x07) != 0) {
339 CMOS_HANDLER_ERR("Invalid bitwidth: %u\n", bitwidth);
340 return (AE_BAD_PARAMETER);
342 if (!atrtc_check_cmos_access(is_read, addr, bytewidth)) {
343 CMOS_HANDLER_ERR("%s access rejected: addr=%#04jx, len=%u\n",
344 is_read ? "Read" : "Write", (uintmax_t)addr, bytewidth);
345 return (AE_BAD_PARAMETER);
350 rtcin_region(addr, value, bytewidth);
353 rtcout_region(addr, value, bytewidth);
356 CMOS_HANDLER_ERR("Invalid function: %u\n", func);
357 return (AE_BAD_PARAMETER);
360 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
361 "ACPI RTC CMOS %s access: addr=%#04x, len=%u, val=%*D\n",
362 is_read ? "read" : "write", (unsigned)addr, bytewidth,
363 bytewidth, value, " ");
369 atrtc_reg_acpi_cmos_handler(device_t dev)
371 struct atrtc_softc *sc = device_get_softc(dev);
373 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
375 /* Don't handle address space events if driver is disabled. */
376 if (acpi_disabled("atrtc"))
379 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sc->acpi_handle))) {
383 if (sc->acpi_handle == NULL ||
384 ACPI_FAILURE(AcpiInstallAddressSpaceHandler(sc->acpi_handle,
385 ACPI_ADR_SPACE_CMOS, atrtc_acpi_cmos_handler, NULL, dev))) {
386 sc->acpi_handle = NULL;
388 "Can't register ACPI CMOS address space handler\n");
396 atrtc_unreg_acpi_cmos_handler(device_t dev)
398 struct atrtc_softc *sc = device_get_softc(dev);
400 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
402 if (sc->acpi_handle != NULL)
403 AcpiRemoveAddressSpaceHandler(sc->acpi_handle,
404 ACPI_ADR_SPACE_CMOS, atrtc_acpi_cmos_handler);
408 #endif /* DEV_ACPI */
411 * Attach to the ISA PnP descriptors for the timer and realtime clock.
413 static struct isa_pnp_id atrtc_ids[] = {
414 { 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
419 atrtc_acpi_disabled(void)
424 if (!acpi_get_fadt_bootflags(&flags))
426 return ((flags & ACPI_FADT_NO_CMOS_RTC) != 0);
433 rtc_acpi_century_get(void)
436 ACPI_TABLE_FADT *fadt;
440 physaddr = acpi_find_table(ACPI_SIG_FADT);
444 fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
448 century = fadt->Century;
449 acpi_unmap_table(fadt);
458 atrtc_probe(device_t dev)
462 if ((atrtc_enabled == -1 && atrtc_acpi_disabled()) ||
463 (atrtc_enabled == 0))
466 result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
467 /* ENOENT means no PnP-ID, device is hinted. */
468 if (result == ENOENT) {
469 device_set_desc(dev, "AT realtime clock");
470 return (BUS_PROBE_LOW_PRIORITY);
472 rtc_century = rtc_acpi_century_get();
477 atrtc_attach(device_t dev)
479 struct atrtc_softc *sc;
483 sc = device_get_softc(dev);
484 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
485 IO_RTC, IO_RTC + 1, 2, RF_ACTIVE);
486 if (sc->port_res == NULL)
487 device_printf(dev, "Warning: Couldn't map I/O.\n");
489 clock_register(dev, 1000000);
490 bzero(&sc->et, sizeof(struct eventtimer));
491 if (!atrtcclock_disable &&
492 (resource_int_value(device_get_name(dev), device_get_unit(dev),
493 "clock", &i) != 0 || i != 0)) {
495 while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
496 &s, NULL) == 0 && s != 8)
498 sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
499 &sc->intr_rid, 8, 8, 1, RF_ACTIVE);
500 if (sc->intr_res == NULL) {
501 device_printf(dev, "Can't map interrupt.\n");
503 } else if ((bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
504 rtc_intr, NULL, sc, &sc->intr_handler))) {
505 device_printf(dev, "Can't setup interrupt.\n");
508 /* Bind IRQ to BSP to avoid live migration. */
509 bus_bind_intr(dev, sc->intr_res, 0);
511 sc->et.et_name = "RTC";
512 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_POW2DIV;
513 sc->et.et_quality = 0;
514 sc->et.et_frequency = 32768;
515 sc->et.et_min_period = 0x00080000;
516 sc->et.et_max_period = 0x80000000;
517 sc->et.et_start = rtc_start;
518 sc->et.et_stop = rtc_stop;
519 sc->et.et_priv = dev;
520 et_register(&sc->et);
526 atrtc_isa_attach(device_t dev)
529 return (atrtc_attach(dev));
534 atrtc_acpi_attach(device_t dev)
538 ret = atrtc_attach(dev);
542 (void)atrtc_reg_acpi_cmos_handler(dev);
548 atrtc_acpi_detach(device_t dev)
551 (void)atrtc_unreg_acpi_cmos_handler(dev);
554 #endif /* DEV_ACPI */
557 atrtc_resume(device_t dev)
565 atrtc_settime(device_t dev __unused, struct timespec *ts)
567 struct bcd_clocktime bct;
569 clock_ts_to_bcd(ts, &bct, false);
570 clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bct);
572 mtx_lock(&atrtc_time_lock);
573 mtx_lock_spin(&atrtc_lock);
575 /* Disable RTC updates and interrupts. */
576 rtcout_locked(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
578 /* Write all the time registers. */
579 rtcout_locked(RTC_SEC, bct.sec);
580 rtcout_locked(RTC_MIN, bct.min);
581 rtcout_locked(RTC_HRS, bct.hour);
582 rtcout_locked(RTC_WDAY, bct.dow + 1);
583 rtcout_locked(RTC_DAY, bct.day);
584 rtcout_locked(RTC_MONTH, bct.mon);
585 rtcout_locked(RTC_YEAR, bct.year & 0xff);
587 rtcout_locked(rtc_century, bct.year >> 8);
590 * Re-enable RTC updates and interrupts.
592 rtcout_locked(RTC_STATUSB, rtc_statusb);
593 rtcin_locked(RTC_INTR);
595 mtx_unlock_spin(&atrtc_lock);
596 mtx_unlock(&atrtc_time_lock);
602 atrtc_gettime(device_t dev, struct timespec *ts)
604 struct bcd_clocktime bct;
606 /* Look if we have a RTC present and the time is valid */
607 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
608 atrtc_power_lost = true;
609 device_printf(dev, "WARNING: Battery failure indication\n");
614 * wait for time update to complete
615 * If RTCSA_TUP is zero, we have at least 244us before next update.
616 * This is fast enough on most hardware, but a refinement would be
617 * to make sure that no more than 240us pass after we start reading,
618 * and try again if so.
620 mtx_lock(&atrtc_time_lock);
621 while (rtcin(RTC_STATUSA) & RTCSA_TUP)
623 mtx_lock_spin(&atrtc_lock);
624 bct.sec = rtcin_locked(RTC_SEC);
625 bct.min = rtcin_locked(RTC_MIN);
626 bct.hour = rtcin_locked(RTC_HRS);
627 bct.day = rtcin_locked(RTC_DAY);
628 bct.mon = rtcin_locked(RTC_MONTH);
629 bct.year = rtcin_locked(RTC_YEAR);
631 bct.year |= rtcin_locked(rtc_century) << 8;
632 mtx_unlock_spin(&atrtc_lock);
633 mtx_unlock(&atrtc_time_lock);
634 /* dow is unused in timespec conversion and we have no nsec info. */
637 clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bct);
638 return (clock_bcd_to_ts(&bct, ts, false));
641 static device_method_t atrtc_isa_methods[] = {
642 /* Device interface */
643 DEVMETHOD(device_probe, atrtc_probe),
644 DEVMETHOD(device_attach, atrtc_isa_attach),
645 DEVMETHOD(device_detach, bus_generic_detach),
646 DEVMETHOD(device_shutdown, bus_generic_shutdown),
647 DEVMETHOD(device_suspend, bus_generic_suspend),
648 /* XXX stop statclock? */
649 DEVMETHOD(device_resume, atrtc_resume),
651 /* clock interface */
652 DEVMETHOD(clock_gettime, atrtc_gettime),
653 DEVMETHOD(clock_settime, atrtc_settime),
657 static driver_t atrtc_isa_driver = {
660 sizeof(struct atrtc_softc),
664 static device_method_t atrtc_acpi_methods[] = {
665 /* Device interface */
666 DEVMETHOD(device_probe, atrtc_probe),
667 DEVMETHOD(device_attach, atrtc_acpi_attach),
668 DEVMETHOD(device_detach, atrtc_acpi_detach),
669 /* XXX stop statclock? */
670 DEVMETHOD(device_resume, atrtc_resume),
672 /* clock interface */
673 DEVMETHOD(clock_gettime, atrtc_gettime),
674 DEVMETHOD(clock_settime, atrtc_settime),
678 static driver_t atrtc_acpi_driver = {
681 sizeof(struct atrtc_softc),
683 #endif /* DEV_ACPI */
685 DRIVER_MODULE(atrtc, isa, atrtc_isa_driver, 0, 0);
687 DRIVER_MODULE(atrtc, acpi, atrtc_acpi_driver, 0, 0);
689 ISA_PNP_INFO(atrtc_ids);