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>
59 * clock_lock protects low-level access to individual hardware registers.
60 * atrtc_time_lock protects the entire sequence of accessing multiple registers
61 * to read or write the date and time.
63 #define RTC_LOCK do { if (!kdb_active) mtx_lock_spin(&clock_lock); } while (0)
64 #define RTC_UNLOCK do { if (!kdb_active) mtx_unlock_spin(&clock_lock); } while (0)
66 struct mtx atrtc_time_lock;
67 MTX_SYSINIT(atrtc_lock_init, &atrtc_time_lock, "atrtc", MTX_DEF);
69 int atrtcclock_disable = 0;
71 static int rtc_reg = -1;
72 static u_char rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
73 static u_char rtc_statusb = RTCSB_24HR;
76 * RTC support routines
91 val = inb(IO_RTC + 1);
97 writertc(int reg, u_char val)
101 if (rtc_reg != reg) {
107 outb(IO_RTC + 1, val);
117 readval = rtcin(port);
118 if (readval >= 0 && (readval & 0xf) < 0xa && (readval & 0xf0) < 0xa0)
119 return (bcd2bin(readval));
127 writertc(RTC_STATUSA, rtc_statusa);
128 writertc(RTC_STATUSB, RTCSB_24HR);
132 atrtc_rate(unsigned rate)
135 rtc_statusa = RTCSA_DIVIDER | rate;
136 writertc(RTC_STATUSA, rtc_statusa);
140 atrtc_enable_intr(void)
143 rtc_statusb |= RTCSB_PINTR;
144 writertc(RTC_STATUSB, rtc_statusb);
149 atrtc_disable_intr(void)
152 rtc_statusb &= ~RTCSB_PINTR;
153 writertc(RTC_STATUSB, rtc_statusb);
161 /* Restore all of the RTC's "status" (actually, control) registers. */
162 rtcin(RTC_STATUSA); /* dummy to get rtc_reg set */
163 writertc(RTC_STATUSB, RTCSB_24HR);
164 writertc(RTC_STATUSA, rtc_statusa);
165 writertc(RTC_STATUSB, rtc_statusb);
170 atrtc_set(struct timespec *ts)
174 clock_ts_to_ct(ts, &ct);
176 mtx_lock(&atrtc_time_lock);
178 /* Disable RTC updates and interrupts. */
179 writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
181 writertc(RTC_SEC, bin2bcd(ct.sec)); /* Write back Seconds */
182 writertc(RTC_MIN, bin2bcd(ct.min)); /* Write back Minutes */
183 writertc(RTC_HRS, bin2bcd(ct.hour)); /* Write back Hours */
185 writertc(RTC_WDAY, ct.dow + 1); /* Write back Weekday */
186 writertc(RTC_DAY, bin2bcd(ct.day)); /* Write back Day */
187 writertc(RTC_MONTH, bin2bcd(ct.mon)); /* Write back Month */
188 writertc(RTC_YEAR, bin2bcd(ct.year % 100)); /* Write back Year */
189 #ifdef USE_RTC_CENTURY
190 writertc(RTC_CENTURY, bin2bcd(ct.year / 100)); /* ... and Century */
193 /* Re-enable RTC updates and interrupts. */
194 writertc(RTC_STATUSB, rtc_statusb);
197 mtx_unlock(&atrtc_time_lock);
200 /**********************************************************************
201 * RTC driver for subr_rtc
205 int port_rid, intr_rid;
206 struct resource *port_res;
207 struct resource *intr_res;
209 struct eventtimer et;
213 rtc_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
216 atrtc_rate(max(fls(period + (period >> 1)) - 17, 1));
222 rtc_stop(struct eventtimer *et)
225 atrtc_disable_intr();
230 * This routine receives statistical clock interrupts from the RTC.
231 * As explained above, these occur at 128 interrupts per second.
232 * When profiling, we receive interrupts at a rate of 1024 Hz.
234 * This does not actually add as much overhead as it sounds, because
235 * when the statistical clock is active, the hardclock driver no longer
236 * needs to keep (inaccurate) statistics on its own. This decouples
237 * statistics gathering from scheduling interrupts.
239 * The RTC chip requires that we read status register C (RTC_INTR)
240 * to acknowledge an interrupt, before it will generate the next one.
241 * Under high interrupt load, rtcintr() can be indefinitely delayed and
242 * the clock can tick immediately after the read from RTC_INTR. In this
243 * case, the mc146818A interrupt signal will not drop for long enough
244 * to register with the 8259 PIC. If an interrupt is missed, the stat
245 * clock will halt, considerably degrading system performance. This is
246 * why we use 'while' rather than a more straightforward 'if' below.
247 * Stat clock ticks can still be lost, causing minor loss of accuracy
248 * in the statistics, but the stat clock will no longer stop.
253 struct atrtc_softc *sc = (struct atrtc_softc *)arg;
256 while (rtcin(RTC_INTR) & RTCIR_PERIOD) {
258 if (sc->et.et_active)
259 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
261 return(flag ? FILTER_HANDLED : FILTER_STRAY);
265 * Attach to the ISA PnP descriptors for the timer and realtime clock.
267 static struct isa_pnp_id atrtc_ids[] = {
268 { 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
273 atrtc_probe(device_t dev)
277 result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
278 /* ENOENT means no PnP-ID, device is hinted. */
279 if (result == ENOENT) {
280 device_set_desc(dev, "AT realtime clock");
281 return (BUS_PROBE_LOW_PRIORITY);
287 atrtc_attach(device_t dev)
289 struct atrtc_softc *sc;
293 sc = device_get_softc(dev);
294 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
295 IO_RTC, IO_RTC + 1, 2, RF_ACTIVE);
296 if (sc->port_res == NULL)
297 device_printf(dev, "Warning: Couldn't map I/O.\n");
299 clock_register(dev, 1000000);
300 bzero(&sc->et, sizeof(struct eventtimer));
301 if (!atrtcclock_disable &&
302 (resource_int_value(device_get_name(dev), device_get_unit(dev),
303 "clock", &i) != 0 || i != 0)) {
305 while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
306 &s, NULL) == 0 && s != 8)
308 sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
309 &sc->intr_rid, 8, 8, 1, RF_ACTIVE);
310 if (sc->intr_res == NULL) {
311 device_printf(dev, "Can't map interrupt.\n");
313 } else if ((bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
314 rtc_intr, NULL, sc, &sc->intr_handler))) {
315 device_printf(dev, "Can't setup interrupt.\n");
318 /* Bind IRQ to BSP to avoid live migration. */
319 bus_bind_intr(dev, sc->intr_res, 0);
321 sc->et.et_name = "RTC";
322 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_POW2DIV;
323 sc->et.et_quality = 0;
324 sc->et.et_frequency = 32768;
325 sc->et.et_min_period = 0x00080000;
326 sc->et.et_max_period = 0x80000000;
327 sc->et.et_start = rtc_start;
328 sc->et.et_stop = rtc_stop;
329 sc->et.et_priv = dev;
330 et_register(&sc->et);
336 atrtc_resume(device_t dev)
344 atrtc_settime(device_t dev __unused, struct timespec *ts)
352 atrtc_gettime(device_t dev, struct timespec *ts)
356 /* Look if we have a RTC present and the time is valid */
357 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
358 device_printf(dev, "WARNING: Battery failure indication\n");
363 * wait for time update to complete
364 * If RTCSA_TUP is zero, we have at least 244us before next update.
365 * This is fast enough on most hardware, but a refinement would be
366 * to make sure that no more than 240us pass after we start reading,
367 * and try again if so.
369 mtx_lock(&atrtc_time_lock);
370 while (rtcin(RTC_STATUSA) & RTCSA_TUP)
374 ct.sec = readrtc(RTC_SEC);
375 ct.min = readrtc(RTC_MIN);
376 ct.hour = readrtc(RTC_HRS);
377 ct.day = readrtc(RTC_DAY);
378 ct.dow = readrtc(RTC_WDAY) - 1;
379 ct.mon = readrtc(RTC_MONTH);
380 ct.year = readrtc(RTC_YEAR);
381 #ifdef USE_RTC_CENTURY
382 ct.year += readrtc(RTC_CENTURY) * 100;
384 ct.year += (ct.year < 80 ? 2000 : 1900);
387 mtx_unlock(&atrtc_time_lock);
388 /* Set dow = -1 because some clocks don't set it correctly. */
390 return (clock_ct_to_ts(&ct, ts));
393 static device_method_t atrtc_methods[] = {
394 /* Device interface */
395 DEVMETHOD(device_probe, atrtc_probe),
396 DEVMETHOD(device_attach, atrtc_attach),
397 DEVMETHOD(device_detach, bus_generic_detach),
398 DEVMETHOD(device_shutdown, bus_generic_shutdown),
399 DEVMETHOD(device_suspend, bus_generic_suspend),
400 /* XXX stop statclock? */
401 DEVMETHOD(device_resume, atrtc_resume),
403 /* clock interface */
404 DEVMETHOD(clock_gettime, atrtc_gettime),
405 DEVMETHOD(clock_settime, atrtc_settime),
410 static driver_t atrtc_driver = {
413 sizeof(struct atrtc_softc),
416 static devclass_t atrtc_devclass;
418 DRIVER_MODULE(atrtc, isa, atrtc_driver, atrtc_devclass, 0, 0);
419 DRIVER_MODULE(atrtc, acpi, atrtc_driver, atrtc_devclass, 0, 0);
425 DB_SHOW_COMMAND(rtc, rtc)
427 printf("%02x/%02x/%02x %02x:%02x:%02x, A = %02x, B = %02x, C = %02x\n",
428 rtcin(RTC_YEAR), rtcin(RTC_MONTH), rtcin(RTC_DAY),
429 rtcin(RTC_HRS), rtcin(RTC_MIN), rtcin(RTC_SEC),
430 rtcin(RTC_STATUSA), rtcin(RTC_STATUSB), rtcin(RTC_INTR));