xref: /freebsd/sys/x86/isa/atrtc.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_isa.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/clock.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/kdb.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/proc.h>
47 #include <sys/rman.h>
48 #include <sys/timeet.h>
49 
50 #include <isa/rtc.h>
51 #ifdef DEV_ISA
52 #include <isa/isareg.h>
53 #include <isa/isavar.h>
54 #endif
55 #include <machine/intr_machdep.h>
56 #include "clock_if.h"
57 #include <contrib/dev/acpica/include/acpi.h>
58 #include <machine/md_var.h>
59 
60 /*
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.
64  */
65 static struct mtx atrtc_lock;
66 MTX_SYSINIT(atrtc_lock_init, &atrtc_lock, "atrtc", MTX_SPIN);
67 
68 /* Force RTC enabled/disabled. */
69 static int atrtc_enabled = -1;
70 TUNABLE_INT("hw.atrtc.enabled", &atrtc_enabled);
71 
72 struct mtx atrtc_time_lock;
73 MTX_SYSINIT(atrtc_time_lock_init, &atrtc_time_lock, "atrtc_time", MTX_DEF);
74 
75 int	atrtcclock_disable = 0;
76 
77 static	int	rtc_reg = -1;
78 static	u_char	rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
79 static	u_char	rtc_statusb = RTCSB_24HR;
80 
81 /*
82  * RTC support routines
83  */
84 
85 static inline u_char
86 rtcin_locked(int reg)
87 {
88 
89 	if (rtc_reg != reg) {
90 		inb(0x84);
91 		outb(IO_RTC, reg);
92 		rtc_reg = reg;
93 		inb(0x84);
94 	}
95 	return (inb(IO_RTC + 1));
96 }
97 
98 static inline void
99 rtcout_locked(int reg, u_char val)
100 {
101 
102 	if (rtc_reg != reg) {
103 		inb(0x84);
104 		outb(IO_RTC, reg);
105 		rtc_reg = reg;
106 		inb(0x84);
107 	}
108 	outb(IO_RTC + 1, val);
109 	inb(0x84);
110 }
111 
112 int
113 rtcin(int reg)
114 {
115 	u_char val;
116 
117 	mtx_lock_spin(&atrtc_lock);
118 	val = rtcin_locked(reg);
119 	mtx_unlock_spin(&atrtc_lock);
120 	return (val);
121 }
122 
123 void
124 writertc(int reg, u_char val)
125 {
126 
127 	mtx_lock_spin(&atrtc_lock);
128 	rtcout_locked(reg, val);
129 	mtx_unlock_spin(&atrtc_lock);
130 }
131 
132 static void
133 atrtc_start(void)
134 {
135 
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);
140 }
141 
142 static void
143 atrtc_rate(unsigned rate)
144 {
145 
146 	rtc_statusa = RTCSA_DIVIDER | rate;
147 	writertc(RTC_STATUSA, rtc_statusa);
148 }
149 
150 static void
151 atrtc_enable_intr(void)
152 {
153 
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);
159 }
160 
161 static void
162 atrtc_disable_intr(void)
163 {
164 
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);
170 }
171 
172 void
173 atrtc_restore(void)
174 {
175 
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);
184 }
185 
186 /**********************************************************************
187  * RTC driver for subr_rtc
188  */
189 
190 struct atrtc_softc {
191 	int port_rid, intr_rid;
192 	struct resource *port_res;
193 	struct resource *intr_res;
194 	void *intr_handler;
195 	struct eventtimer et;
196 };
197 
198 static int
199 rtc_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
200 {
201 
202 	atrtc_rate(max(fls(period + (period >> 1)) - 17, 1));
203 	atrtc_enable_intr();
204 	return (0);
205 }
206 
207 static int
208 rtc_stop(struct eventtimer *et)
209 {
210 
211 	atrtc_disable_intr();
212 	return (0);
213 }
214 
215 /*
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.
219  *
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.
224  *
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.
235  */
236 static int
237 rtc_intr(void *arg)
238 {
239 	struct atrtc_softc *sc = (struct atrtc_softc *)arg;
240 	int flag = 0;
241 
242 	while (rtcin(RTC_INTR) & RTCIR_PERIOD) {
243 		flag = 1;
244 		if (sc->et.et_active)
245 			sc->et.et_event_cb(&sc->et, sc->et.et_arg);
246 	}
247 	return(flag ? FILTER_HANDLED : FILTER_STRAY);
248 }
249 
250 /*
251  * Attach to the ISA PnP descriptors for the timer and realtime clock.
252  */
253 static struct isa_pnp_id atrtc_ids[] = {
254 	{ 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
255 	{ 0 }
256 };
257 
258 static bool
259 atrtc_acpi_disabled(void)
260 {
261 	uint16_t flags;
262 
263 	if (!acpi_get_fadt_bootflags(&flags))
264 		return (false);
265 	return ((flags & ACPI_FADT_NO_CMOS_RTC) != 0);
266 		return (true);
267 }
268 
269 static int
270 atrtc_probe(device_t dev)
271 {
272 	int result;
273 
274 	if ((atrtc_enabled == -1 && atrtc_acpi_disabled()) ||
275 	    (atrtc_enabled == 0))
276 		return (ENXIO);
277 
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);
283 	}
284 	return (result);
285 }
286 
287 static int
288 atrtc_attach(device_t dev)
289 {
290 	struct atrtc_softc *sc;
291 	rman_res_t s;
292 	int i;
293 
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");
299 	atrtc_start();
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)) {
305 		sc->intr_rid = 0;
306 		while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
307 		    &s, NULL) == 0 && s != 8)
308 			sc->intr_rid++;
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");
313 			return (0);
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");
317 			return (0);
318 		} else {
319 			/* Bind IRQ to BSP to avoid live migration. */
320 			bus_bind_intr(dev, sc->intr_res, 0);
321 		}
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);
332 	}
333 	return(0);
334 }
335 
336 static int
337 atrtc_resume(device_t dev)
338 {
339 
340 	atrtc_restore();
341 	return(0);
342 }
343 
344 static int
345 atrtc_settime(device_t dev __unused, struct timespec *ts)
346 {
347 	struct bcd_clocktime bct;
348 
349 	clock_ts_to_bcd(ts, &bct, false);
350 	clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bct);
351 
352 	mtx_lock(&atrtc_time_lock);
353 	mtx_lock_spin(&atrtc_lock);
354 
355 	/* Disable RTC updates and interrupts.  */
356 	rtcout_locked(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
357 
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);
368 #endif
369 
370 	/*
371 	 * Re-enable RTC updates and interrupts.
372 	 */
373 	rtcout_locked(RTC_STATUSB, rtc_statusb);
374 	rtcin_locked(RTC_INTR);
375 
376 	mtx_unlock_spin(&atrtc_lock);
377 	mtx_unlock(&atrtc_time_lock);
378 
379 	return (0);
380 }
381 
382 static int
383 atrtc_gettime(device_t dev, struct timespec *ts)
384 {
385 	struct bcd_clocktime bct;
386 
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");
390 		return (EINVAL);
391 	}
392 
393 	/*
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.
399 	 */
400 	mtx_lock(&atrtc_time_lock);
401 	while (rtcin(RTC_STATUSA) & RTCSA_TUP)
402 		continue;
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;
412 #endif
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. */
416 	bct.dow  = 0;
417 	bct.nsec = 0;
418 	clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bct);
419 	return (clock_bcd_to_ts(&bct, ts, false));
420 }
421 
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),
431 
432 	/* clock interface */
433 	DEVMETHOD(clock_gettime,	atrtc_gettime),
434 	DEVMETHOD(clock_settime,	atrtc_settime),
435 
436 	{ 0, 0 }
437 };
438 
439 static driver_t atrtc_driver = {
440 	"atrtc",
441 	atrtc_methods,
442 	sizeof(struct atrtc_softc),
443 };
444 
445 static devclass_t atrtc_devclass;
446 
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);
450