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$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/clock.h>
42 #include <sys/mutex.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
48 #include <sys/timeet.h>
52 #include <isa/isareg.h>
53 #include <isa/isavar.h>
55 #include <machine/intr_machdep.h>
57 #include <contrib/dev/acpica/include/acpi.h>
58 #include <machine/md_var.h>
61 * atrtc_lock protects low-level access to individual hardware registers.
62 * atrtc_time_lock protects the entire sequence of accessing multiple registers
63 * to read or write the date and time.
65 static struct mtx atrtc_lock;
66 MTX_SYSINIT(atrtc_lock_init, &atrtc_lock, "atrtc", MTX_SPIN);
68 /* Force RTC enabled/disabled. */
69 static int atrtc_enabled = -1;
70 TUNABLE_INT("hw.atrtc.enabled", &atrtc_enabled);
72 struct mtx atrtc_time_lock;
73 MTX_SYSINIT(atrtc_time_lock_init, &atrtc_time_lock, "atrtc_time", MTX_DEF);
75 int atrtcclock_disable = 0;
77 static int rtc_reg = -1;
78 static u_char rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
79 static u_char rtc_statusb = RTCSB_24HR;
82 * RTC support routines
95 return (inb(IO_RTC + 1));
99 rtcout_locked(int reg, u_char val)
102 if (rtc_reg != reg) {
108 outb(IO_RTC + 1, val);
117 mtx_lock_spin(&atrtc_lock);
118 val = rtcin_locked(reg);
119 mtx_unlock_spin(&atrtc_lock);
124 writertc(int reg, u_char val)
127 mtx_lock_spin(&atrtc_lock);
128 rtcout_locked(reg, val);
129 mtx_unlock_spin(&atrtc_lock);
136 mtx_lock_spin(&atrtc_lock);
137 rtcout_locked(RTC_STATUSA, rtc_statusa);
138 rtcout_locked(RTC_STATUSB, RTCSB_24HR);
139 mtx_unlock_spin(&atrtc_lock);
143 atrtc_rate(unsigned rate)
146 rtc_statusa = RTCSA_DIVIDER | rate;
147 writertc(RTC_STATUSA, rtc_statusa);
151 atrtc_enable_intr(void)
154 rtc_statusb |= RTCSB_PINTR;
155 mtx_lock_spin(&atrtc_lock);
156 rtcout_locked(RTC_STATUSB, rtc_statusb);
157 rtcin_locked(RTC_INTR);
158 mtx_unlock_spin(&atrtc_lock);
162 atrtc_disable_intr(void)
165 rtc_statusb &= ~RTCSB_PINTR;
166 mtx_lock_spin(&atrtc_lock);
167 rtcout_locked(RTC_STATUSB, rtc_statusb);
168 rtcin_locked(RTC_INTR);
169 mtx_unlock_spin(&atrtc_lock);
176 /* Restore all of the RTC's "status" (actually, control) registers. */
177 mtx_lock_spin(&atrtc_lock);
178 rtcin_locked(RTC_STATUSA); /* dummy to get rtc_reg set */
179 rtcout_locked(RTC_STATUSB, RTCSB_24HR);
180 rtcout_locked(RTC_STATUSA, rtc_statusa);
181 rtcout_locked(RTC_STATUSB, rtc_statusb);
182 rtcin_locked(RTC_INTR);
183 mtx_unlock_spin(&atrtc_lock);
186 /**********************************************************************
187 * RTC driver for subr_rtc
191 int port_rid, intr_rid;
192 struct resource *port_res;
193 struct resource *intr_res;
195 struct eventtimer et;
199 rtc_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
202 atrtc_rate(max(fls(period + (period >> 1)) - 17, 1));
208 rtc_stop(struct eventtimer *et)
211 atrtc_disable_intr();
216 * This routine receives statistical clock interrupts from the RTC.
217 * As explained above, these occur at 128 interrupts per second.
218 * When profiling, we receive interrupts at a rate of 1024 Hz.
220 * This does not actually add as much overhead as it sounds, because
221 * when the statistical clock is active, the hardclock driver no longer
222 * needs to keep (inaccurate) statistics on its own. This decouples
223 * statistics gathering from scheduling interrupts.
225 * The RTC chip requires that we read status register C (RTC_INTR)
226 * to acknowledge an interrupt, before it will generate the next one.
227 * Under high interrupt load, rtcintr() can be indefinitely delayed and
228 * the clock can tick immediately after the read from RTC_INTR. In this
229 * case, the mc146818A interrupt signal will not drop for long enough
230 * to register with the 8259 PIC. If an interrupt is missed, the stat
231 * clock will halt, considerably degrading system performance. This is
232 * why we use 'while' rather than a more straightforward 'if' below.
233 * Stat clock ticks can still be lost, causing minor loss of accuracy
234 * in the statistics, but the stat clock will no longer stop.
239 struct atrtc_softc *sc = (struct atrtc_softc *)arg;
242 while (rtcin(RTC_INTR) & RTCIR_PERIOD) {
244 if (sc->et.et_active)
245 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
247 return(flag ? FILTER_HANDLED : FILTER_STRAY);
251 * Attach to the ISA PnP descriptors for the timer and realtime clock.
253 static struct isa_pnp_id atrtc_ids[] = {
254 { 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
259 atrtc_acpi_disabled(void)
263 if (!acpi_get_fadt_bootflags(&flags))
265 return ((flags & ACPI_FADT_NO_CMOS_RTC) != 0);
270 atrtc_probe(device_t dev)
274 if ((atrtc_enabled == -1 && atrtc_acpi_disabled()) ||
275 (atrtc_enabled == 0))
278 result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
279 /* ENOENT means no PnP-ID, device is hinted. */
280 if (result == ENOENT) {
281 device_set_desc(dev, "AT realtime clock");
282 return (BUS_PROBE_LOW_PRIORITY);
288 atrtc_attach(device_t dev)
290 struct atrtc_softc *sc;
294 sc = device_get_softc(dev);
295 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
296 IO_RTC, IO_RTC + 1, 2, RF_ACTIVE);
297 if (sc->port_res == NULL)
298 device_printf(dev, "Warning: Couldn't map I/O.\n");
300 clock_register(dev, 1000000);
301 bzero(&sc->et, sizeof(struct eventtimer));
302 if (!atrtcclock_disable &&
303 (resource_int_value(device_get_name(dev), device_get_unit(dev),
304 "clock", &i) != 0 || i != 0)) {
306 while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
307 &s, NULL) == 0 && s != 8)
309 sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
310 &sc->intr_rid, 8, 8, 1, RF_ACTIVE);
311 if (sc->intr_res == NULL) {
312 device_printf(dev, "Can't map interrupt.\n");
314 } else if ((bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
315 rtc_intr, NULL, sc, &sc->intr_handler))) {
316 device_printf(dev, "Can't setup interrupt.\n");
319 /* Bind IRQ to BSP to avoid live migration. */
320 bus_bind_intr(dev, sc->intr_res, 0);
322 sc->et.et_name = "RTC";
323 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_POW2DIV;
324 sc->et.et_quality = 0;
325 sc->et.et_frequency = 32768;
326 sc->et.et_min_period = 0x00080000;
327 sc->et.et_max_period = 0x80000000;
328 sc->et.et_start = rtc_start;
329 sc->et.et_stop = rtc_stop;
330 sc->et.et_priv = dev;
331 et_register(&sc->et);
337 atrtc_resume(device_t dev)
345 atrtc_settime(device_t dev __unused, struct timespec *ts)
347 struct bcd_clocktime bct;
349 clock_ts_to_bcd(ts, &bct, false);
350 clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bct);
352 mtx_lock(&atrtc_time_lock);
353 mtx_lock_spin(&atrtc_lock);
355 /* Disable RTC updates and interrupts. */
356 rtcout_locked(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
358 /* Write all the time registers. */
359 rtcout_locked(RTC_SEC, bct.sec);
360 rtcout_locked(RTC_MIN, bct.min);
361 rtcout_locked(RTC_HRS, bct.hour);
362 rtcout_locked(RTC_WDAY, bct.dow + 1);
363 rtcout_locked(RTC_DAY, bct.day);
364 rtcout_locked(RTC_MONTH, bct.mon);
365 rtcout_locked(RTC_YEAR, bct.year & 0xff);
366 #ifdef USE_RTC_CENTURY
367 rtcout_locked(RTC_CENTURY, bct.year >> 8);
371 * Re-enable RTC updates and interrupts.
373 rtcout_locked(RTC_STATUSB, rtc_statusb);
374 rtcin_locked(RTC_INTR);
376 mtx_unlock_spin(&atrtc_lock);
377 mtx_unlock(&atrtc_time_lock);
383 atrtc_gettime(device_t dev, struct timespec *ts)
385 struct bcd_clocktime bct;
387 /* Look if we have a RTC present and the time is valid */
388 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
389 device_printf(dev, "WARNING: Battery failure indication\n");
394 * wait for time update to complete
395 * If RTCSA_TUP is zero, we have at least 244us before next update.
396 * This is fast enough on most hardware, but a refinement would be
397 * to make sure that no more than 240us pass after we start reading,
398 * and try again if so.
400 mtx_lock(&atrtc_time_lock);
401 while (rtcin(RTC_STATUSA) & RTCSA_TUP)
403 mtx_lock_spin(&atrtc_lock);
404 bct.sec = rtcin_locked(RTC_SEC);
405 bct.min = rtcin_locked(RTC_MIN);
406 bct.hour = rtcin_locked(RTC_HRS);
407 bct.day = rtcin_locked(RTC_DAY);
408 bct.mon = rtcin_locked(RTC_MONTH);
409 bct.year = rtcin_locked(RTC_YEAR);
410 #ifdef USE_RTC_CENTURY
411 bct.year |= rtcin_locked(RTC_CENTURY) << 8;
413 mtx_unlock_spin(&atrtc_lock);
414 mtx_unlock(&atrtc_time_lock);
415 /* dow is unused in timespec conversion and we have no nsec info. */
418 clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bct);
419 return (clock_bcd_to_ts(&bct, ts, false));
422 static device_method_t atrtc_methods[] = {
423 /* Device interface */
424 DEVMETHOD(device_probe, atrtc_probe),
425 DEVMETHOD(device_attach, atrtc_attach),
426 DEVMETHOD(device_detach, bus_generic_detach),
427 DEVMETHOD(device_shutdown, bus_generic_shutdown),
428 DEVMETHOD(device_suspend, bus_generic_suspend),
429 /* XXX stop statclock? */
430 DEVMETHOD(device_resume, atrtc_resume),
432 /* clock interface */
433 DEVMETHOD(clock_gettime, atrtc_gettime),
434 DEVMETHOD(clock_settime, atrtc_settime),
439 static driver_t atrtc_driver = {
442 sizeof(struct atrtc_softc),
445 static devclass_t atrtc_devclass;
447 DRIVER_MODULE(atrtc, isa, atrtc_driver, atrtc_devclass, 0, 0);
448 DRIVER_MODULE(atrtc, acpi, atrtc_driver, atrtc_devclass, 0, 0);
449 ISA_PNP_INFO(atrtc_ids);