atrtc.c revision 331521
1/*-
2 * Copyright (c) 2008 Poul-Henning Kamp
3 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: stable/11/sys/x86/isa/atrtc.c 331521 2018-03-25 01:52:38Z ian $
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: stable/11/sys/x86/isa/atrtc.c 331521 2018-03-25 01:52:38Z ian $");
32
33#include "opt_isa.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/bus.h>
38#include <sys/clock.h>
39#include <sys/lock.h>
40#include <sys/mutex.h>
41#include <sys/kdb.h>
42#include <sys/kernel.h>
43#include <sys/module.h>
44#include <sys/proc.h>
45#include <sys/rman.h>
46#include <sys/timeet.h>
47
48#include <isa/rtc.h>
49#ifdef DEV_ISA
50#include <isa/isareg.h>
51#include <isa/isavar.h>
52#endif
53#include <machine/intr_machdep.h>
54#include "clock_if.h"
55
56/*
57 * atrtc_lock protects low-level access to individual hardware registers.
58 * atrtc_time_lock protects the entire sequence of accessing multiple registers
59 * to read or write the date and time.
60 */
61static struct mtx atrtc_lock;
62MTX_SYSINIT(atrtc_lock_init, &atrtc_lock, "atrtc", MTX_SPIN);
63
64struct mtx atrtc_time_lock;
65MTX_SYSINIT(atrtc_time_lock_init, &atrtc_time_lock, "atrtc_time", MTX_DEF);
66
67int	atrtcclock_disable = 0;
68
69static	int	rtc_reg = -1;
70static	u_char	rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
71static	u_char	rtc_statusb = RTCSB_24HR;
72
73/*
74 * RTC support routines
75 */
76
77static inline u_char
78rtcin_locked(int reg)
79{
80
81	if (rtc_reg != reg) {
82		inb(0x84);
83		outb(IO_RTC, reg);
84		rtc_reg = reg;
85		inb(0x84);
86	}
87	return (inb(IO_RTC + 1));
88}
89
90static inline void
91rtcout_locked(int reg, u_char val)
92{
93
94	if (rtc_reg != reg) {
95		inb(0x84);
96		outb(IO_RTC, reg);
97		rtc_reg = reg;
98		inb(0x84);
99	}
100	outb(IO_RTC + 1, val);
101	inb(0x84);
102}
103
104int
105rtcin(int reg)
106{
107	u_char val;
108
109	mtx_lock_spin(&atrtc_lock);
110	val = rtcin_locked(reg);
111	mtx_unlock_spin(&atrtc_lock);
112	return (val);
113}
114
115void
116writertc(int reg, u_char val)
117{
118
119	mtx_lock_spin(&atrtc_lock);
120	rtcout_locked(reg, val);
121	mtx_unlock_spin(&atrtc_lock);
122}
123
124static void
125atrtc_start(void)
126{
127
128	mtx_lock_spin(&atrtc_lock);
129	rtcout_locked(RTC_STATUSA, rtc_statusa);
130	rtcout_locked(RTC_STATUSB, RTCSB_24HR);
131	mtx_unlock_spin(&atrtc_lock);
132}
133
134static void
135atrtc_rate(unsigned rate)
136{
137
138	rtc_statusa = RTCSA_DIVIDER | rate;
139	writertc(RTC_STATUSA, rtc_statusa);
140}
141
142static void
143atrtc_enable_intr(void)
144{
145
146	rtc_statusb |= RTCSB_PINTR;
147	mtx_lock_spin(&atrtc_lock);
148	rtcout_locked(RTC_STATUSB, rtc_statusb);
149	rtcin_locked(RTC_INTR);
150	mtx_unlock_spin(&atrtc_lock);
151}
152
153static void
154atrtc_disable_intr(void)
155{
156
157	rtc_statusb &= ~RTCSB_PINTR;
158	mtx_lock_spin(&atrtc_lock);
159	rtcout_locked(RTC_STATUSB, rtc_statusb);
160	rtcin_locked(RTC_INTR);
161	mtx_unlock_spin(&atrtc_lock);
162}
163
164void
165atrtc_restore(void)
166{
167
168	/* Restore all of the RTC's "status" (actually, control) registers. */
169	mtx_lock_spin(&atrtc_lock);
170	rtcin_locked(RTC_STATUSA);	/* dummy to get rtc_reg set */
171	rtcout_locked(RTC_STATUSB, RTCSB_24HR);
172	rtcout_locked(RTC_STATUSA, rtc_statusa);
173	rtcout_locked(RTC_STATUSB, rtc_statusb);
174	rtcin_locked(RTC_INTR);
175	mtx_unlock_spin(&atrtc_lock);
176}
177
178/**********************************************************************
179 * RTC driver for subr_rtc
180 */
181
182struct atrtc_softc {
183	int port_rid, intr_rid;
184	struct resource *port_res;
185	struct resource *intr_res;
186	void *intr_handler;
187	struct eventtimer et;
188};
189
190static int
191rtc_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
192{
193
194	atrtc_rate(max(fls(period + (period >> 1)) - 17, 1));
195	atrtc_enable_intr();
196	return (0);
197}
198
199static int
200rtc_stop(struct eventtimer *et)
201{
202
203	atrtc_disable_intr();
204	return (0);
205}
206
207/*
208 * This routine receives statistical clock interrupts from the RTC.
209 * As explained above, these occur at 128 interrupts per second.
210 * When profiling, we receive interrupts at a rate of 1024 Hz.
211 *
212 * This does not actually add as much overhead as it sounds, because
213 * when the statistical clock is active, the hardclock driver no longer
214 * needs to keep (inaccurate) statistics on its own.  This decouples
215 * statistics gathering from scheduling interrupts.
216 *
217 * The RTC chip requires that we read status register C (RTC_INTR)
218 * to acknowledge an interrupt, before it will generate the next one.
219 * Under high interrupt load, rtcintr() can be indefinitely delayed and
220 * the clock can tick immediately after the read from RTC_INTR.  In this
221 * case, the mc146818A interrupt signal will not drop for long enough
222 * to register with the 8259 PIC.  If an interrupt is missed, the stat
223 * clock will halt, considerably degrading system performance.  This is
224 * why we use 'while' rather than a more straightforward 'if' below.
225 * Stat clock ticks can still be lost, causing minor loss of accuracy
226 * in the statistics, but the stat clock will no longer stop.
227 */
228static int
229rtc_intr(void *arg)
230{
231	struct atrtc_softc *sc = (struct atrtc_softc *)arg;
232	int flag = 0;
233
234	while (rtcin(RTC_INTR) & RTCIR_PERIOD) {
235		flag = 1;
236		if (sc->et.et_active)
237			sc->et.et_event_cb(&sc->et, sc->et.et_arg);
238	}
239	return(flag ? FILTER_HANDLED : FILTER_STRAY);
240}
241
242/*
243 * Attach to the ISA PnP descriptors for the timer and realtime clock.
244 */
245static struct isa_pnp_id atrtc_ids[] = {
246	{ 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
247	{ 0 }
248};
249
250static int
251atrtc_probe(device_t dev)
252{
253	int result;
254
255	result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
256	/* ENOENT means no PnP-ID, device is hinted. */
257	if (result == ENOENT) {
258		device_set_desc(dev, "AT realtime clock");
259		return (BUS_PROBE_LOW_PRIORITY);
260	}
261	return (result);
262}
263
264static int
265atrtc_attach(device_t dev)
266{
267	struct atrtc_softc *sc;
268	rman_res_t s;
269	int i;
270
271	sc = device_get_softc(dev);
272	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
273	    IO_RTC, IO_RTC + 1, 2, RF_ACTIVE);
274	if (sc->port_res == NULL)
275		device_printf(dev, "Warning: Couldn't map I/O.\n");
276	atrtc_start();
277	clock_register(dev, 1000000);
278	bzero(&sc->et, sizeof(struct eventtimer));
279	if (!atrtcclock_disable &&
280	    (resource_int_value(device_get_name(dev), device_get_unit(dev),
281	     "clock", &i) != 0 || i != 0)) {
282		sc->intr_rid = 0;
283		while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
284		    &s, NULL) == 0 && s != 8)
285			sc->intr_rid++;
286		sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
287		    &sc->intr_rid, 8, 8, 1, RF_ACTIVE);
288		if (sc->intr_res == NULL) {
289			device_printf(dev, "Can't map interrupt.\n");
290			return (0);
291		} else if ((bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
292		    rtc_intr, NULL, sc, &sc->intr_handler))) {
293			device_printf(dev, "Can't setup interrupt.\n");
294			return (0);
295		} else {
296			/* Bind IRQ to BSP to avoid live migration. */
297			bus_bind_intr(dev, sc->intr_res, 0);
298		}
299		sc->et.et_name = "RTC";
300		sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_POW2DIV;
301		sc->et.et_quality = 0;
302		sc->et.et_frequency = 32768;
303		sc->et.et_min_period = 0x00080000;
304		sc->et.et_max_period = 0x80000000;
305		sc->et.et_start = rtc_start;
306		sc->et.et_stop = rtc_stop;
307		sc->et.et_priv = dev;
308		et_register(&sc->et);
309	}
310	return(0);
311}
312
313static int
314atrtc_resume(device_t dev)
315{
316
317	atrtc_restore();
318	return(0);
319}
320
321static int
322atrtc_settime(device_t dev __unused, struct timespec *ts)
323{
324	struct bcd_clocktime bct;
325
326	clock_ts_to_bcd(ts, &bct, false);
327	clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bct);
328
329	mtx_lock(&atrtc_time_lock);
330	mtx_lock_spin(&atrtc_lock);
331
332	/* Disable RTC updates and interrupts.  */
333	rtcout_locked(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
334
335	/* Write all the time registers. */
336	rtcout_locked(RTC_SEC,   bct.sec);
337	rtcout_locked(RTC_MIN,   bct.min);
338	rtcout_locked(RTC_HRS,   bct.hour);
339	rtcout_locked(RTC_WDAY,  bct.dow + 1);
340	rtcout_locked(RTC_DAY,   bct.day);
341	rtcout_locked(RTC_MONTH, bct.mon);
342	rtcout_locked(RTC_YEAR,  bct.year & 0xff);
343#ifdef USE_RTC_CENTURY
344	rtcout_locked(RTC_CENTURY, bct.year >> 8);
345#endif
346
347	/*
348	 * Re-enable RTC updates and interrupts.
349	 */
350	rtcout_locked(RTC_STATUSB, rtc_statusb);
351	rtcin_locked(RTC_INTR);
352
353	mtx_unlock_spin(&atrtc_lock);
354	mtx_unlock(&atrtc_time_lock);
355
356	return (0);
357}
358
359static int
360atrtc_gettime(device_t dev, struct timespec *ts)
361{
362	struct bcd_clocktime bct;
363
364	/* Look if we have a RTC present and the time is valid */
365	if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
366		device_printf(dev, "WARNING: Battery failure indication\n");
367		return (EINVAL);
368	}
369
370	/*
371	 * wait for time update to complete
372	 * If RTCSA_TUP is zero, we have at least 244us before next update.
373	 * This is fast enough on most hardware, but a refinement would be
374	 * to make sure that no more than 240us pass after we start reading,
375	 * and try again if so.
376	 */
377	mtx_lock(&atrtc_time_lock);
378	while (rtcin(RTC_STATUSA) & RTCSA_TUP)
379		continue;
380	mtx_lock_spin(&atrtc_lock);
381	bct.sec  = rtcin_locked(RTC_SEC);
382	bct.min  = rtcin_locked(RTC_MIN);
383	bct.hour = rtcin_locked(RTC_HRS);
384	bct.day  = rtcin_locked(RTC_DAY);
385	bct.mon  = rtcin_locked(RTC_MONTH);
386	bct.year = rtcin_locked(RTC_YEAR);
387#ifdef USE_RTC_CENTURY
388	bct.year |= rtcin_locked(RTC_CENTURY) << 8;
389#endif
390	mtx_unlock_spin(&atrtc_lock);
391	mtx_unlock(&atrtc_time_lock);
392	/* dow is unused in timespec conversion and we have no nsec info. */
393	bct.dow  = 0;
394	bct.nsec = 0;
395	clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bct);
396	return (clock_bcd_to_ts(&bct, ts, false));
397}
398
399static device_method_t atrtc_methods[] = {
400	/* Device interface */
401	DEVMETHOD(device_probe,		atrtc_probe),
402	DEVMETHOD(device_attach,	atrtc_attach),
403	DEVMETHOD(device_detach,	bus_generic_detach),
404	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
405	DEVMETHOD(device_suspend,	bus_generic_suspend),
406		/* XXX stop statclock? */
407	DEVMETHOD(device_resume,	atrtc_resume),
408
409	/* clock interface */
410	DEVMETHOD(clock_gettime,	atrtc_gettime),
411	DEVMETHOD(clock_settime,	atrtc_settime),
412
413	{ 0, 0 }
414};
415
416static driver_t atrtc_driver = {
417	"atrtc",
418	atrtc_methods,
419	sizeof(struct atrtc_softc),
420};
421
422static devclass_t atrtc_devclass;
423
424DRIVER_MODULE(atrtc, isa, atrtc_driver, atrtc_devclass, 0, 0);
425DRIVER_MODULE(atrtc, acpi, atrtc_driver, atrtc_devclass, 0, 0);
426