xref: /freebsd/sys/x86/isa/atrtc.c (revision d6b92ffa)
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$
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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  * clock_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  */
61 #define	RTC_LOCK	do { if (!kdb_active) mtx_lock_spin(&clock_lock); } while (0)
62 #define	RTC_UNLOCK	do { if (!kdb_active) mtx_unlock_spin(&clock_lock); } while (0)
63 
64 struct mtx atrtc_time_lock;
65 MTX_SYSINIT(atrtc_lock_init, &atrtc_time_lock, "atrtc", MTX_DEF);
66 
67 int	atrtcclock_disable = 0;
68 
69 static	int	rtc_reg = -1;
70 static	u_char	rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
71 static	u_char	rtc_statusb = RTCSB_24HR;
72 
73 /*
74  * RTC support routines
75  */
76 
77 int
78 rtcin(int reg)
79 {
80 	u_char val;
81 
82 	RTC_LOCK;
83 	if (rtc_reg != reg) {
84 		inb(0x84);
85 		outb(IO_RTC, reg);
86 		rtc_reg = reg;
87 		inb(0x84);
88 	}
89 	val = inb(IO_RTC + 1);
90 	RTC_UNLOCK;
91 	return (val);
92 }
93 
94 void
95 writertc(int reg, u_char val)
96 {
97 
98 	RTC_LOCK;
99 	if (rtc_reg != reg) {
100 		inb(0x84);
101 		outb(IO_RTC, reg);
102 		rtc_reg = reg;
103 		inb(0x84);
104 	}
105 	outb(IO_RTC + 1, val);
106 	inb(0x84);
107 	RTC_UNLOCK;
108 }
109 
110 static __inline int
111 readrtc(int port)
112 {
113 	int readval;
114 
115 	readval = rtcin(port);
116 	if (readval >= 0 && (readval & 0xf) < 0xa && (readval & 0xf0) < 0xa0)
117 		return (bcd2bin(readval));
118 	return (0);
119 }
120 
121 static void
122 atrtc_start(void)
123 {
124 
125 	writertc(RTC_STATUSA, rtc_statusa);
126 	writertc(RTC_STATUSB, RTCSB_24HR);
127 }
128 
129 static void
130 atrtc_rate(unsigned rate)
131 {
132 
133 	rtc_statusa = RTCSA_DIVIDER | rate;
134 	writertc(RTC_STATUSA, rtc_statusa);
135 }
136 
137 static void
138 atrtc_enable_intr(void)
139 {
140 
141 	rtc_statusb |= RTCSB_PINTR;
142 	writertc(RTC_STATUSB, rtc_statusb);
143 	rtcin(RTC_INTR);
144 }
145 
146 static void
147 atrtc_disable_intr(void)
148 {
149 
150 	rtc_statusb &= ~RTCSB_PINTR;
151 	writertc(RTC_STATUSB, rtc_statusb);
152 	rtcin(RTC_INTR);
153 }
154 
155 void
156 atrtc_restore(void)
157 {
158 
159 	/* Restore all of the RTC's "status" (actually, control) registers. */
160 	rtcin(RTC_STATUSA);	/* dummy to get rtc_reg set */
161 	writertc(RTC_STATUSB, RTCSB_24HR);
162 	writertc(RTC_STATUSA, rtc_statusa);
163 	writertc(RTC_STATUSB, rtc_statusb);
164 	rtcin(RTC_INTR);
165 }
166 
167 void
168 atrtc_set(struct timespec *ts)
169 {
170 	struct clocktime ct;
171 
172 	clock_ts_to_ct(ts, &ct);
173 
174 	mtx_lock(&atrtc_time_lock);
175 
176 	/* Disable RTC updates and interrupts. */
177 	writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
178 
179 	writertc(RTC_SEC, bin2bcd(ct.sec)); 		/* Write back Seconds */
180 	writertc(RTC_MIN, bin2bcd(ct.min)); 		/* Write back Minutes */
181 	writertc(RTC_HRS, bin2bcd(ct.hour));		/* Write back Hours   */
182 
183 	writertc(RTC_WDAY, ct.dow + 1);			/* Write back Weekday */
184 	writertc(RTC_DAY, bin2bcd(ct.day));		/* Write back Day */
185 	writertc(RTC_MONTH, bin2bcd(ct.mon));           /* Write back Month   */
186 	writertc(RTC_YEAR, bin2bcd(ct.year % 100));	/* Write back Year    */
187 #ifdef USE_RTC_CENTURY
188 	writertc(RTC_CENTURY, bin2bcd(ct.year / 100));	/* ... and Century    */
189 #endif
190 
191 	/* Re-enable RTC updates and interrupts. */
192 	writertc(RTC_STATUSB, rtc_statusb);
193 	rtcin(RTC_INTR);
194 
195 	mtx_unlock(&atrtc_time_lock);
196 }
197 
198 /**********************************************************************
199  * RTC driver for subr_rtc
200  */
201 
202 struct atrtc_softc {
203 	int port_rid, intr_rid;
204 	struct resource *port_res;
205 	struct resource *intr_res;
206 	void *intr_handler;
207 	struct eventtimer et;
208 };
209 
210 static int
211 rtc_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
212 {
213 
214 	atrtc_rate(max(fls(period + (period >> 1)) - 17, 1));
215 	atrtc_enable_intr();
216 	return (0);
217 }
218 
219 static int
220 rtc_stop(struct eventtimer *et)
221 {
222 
223 	atrtc_disable_intr();
224 	return (0);
225 }
226 
227 /*
228  * This routine receives statistical clock interrupts from the RTC.
229  * As explained above, these occur at 128 interrupts per second.
230  * When profiling, we receive interrupts at a rate of 1024 Hz.
231  *
232  * This does not actually add as much overhead as it sounds, because
233  * when the statistical clock is active, the hardclock driver no longer
234  * needs to keep (inaccurate) statistics on its own.  This decouples
235  * statistics gathering from scheduling interrupts.
236  *
237  * The RTC chip requires that we read status register C (RTC_INTR)
238  * to acknowledge an interrupt, before it will generate the next one.
239  * Under high interrupt load, rtcintr() can be indefinitely delayed and
240  * the clock can tick immediately after the read from RTC_INTR.  In this
241  * case, the mc146818A interrupt signal will not drop for long enough
242  * to register with the 8259 PIC.  If an interrupt is missed, the stat
243  * clock will halt, considerably degrading system performance.  This is
244  * why we use 'while' rather than a more straightforward 'if' below.
245  * Stat clock ticks can still be lost, causing minor loss of accuracy
246  * in the statistics, but the stat clock will no longer stop.
247  */
248 static int
249 rtc_intr(void *arg)
250 {
251 	struct atrtc_softc *sc = (struct atrtc_softc *)arg;
252 	int flag = 0;
253 
254 	while (rtcin(RTC_INTR) & RTCIR_PERIOD) {
255 		flag = 1;
256 		if (sc->et.et_active)
257 			sc->et.et_event_cb(&sc->et, sc->et.et_arg);
258 	}
259 	return(flag ? FILTER_HANDLED : FILTER_STRAY);
260 }
261 
262 /*
263  * Attach to the ISA PnP descriptors for the timer and realtime clock.
264  */
265 static struct isa_pnp_id atrtc_ids[] = {
266 	{ 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
267 	{ 0 }
268 };
269 
270 static int
271 atrtc_probe(device_t dev)
272 {
273 	int result;
274 
275 	result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
276 	/* ENOENT means no PnP-ID, device is hinted. */
277 	if (result == ENOENT) {
278 		device_set_desc(dev, "AT realtime clock");
279 		return (BUS_PROBE_LOW_PRIORITY);
280 	}
281 	return (result);
282 }
283 
284 static int
285 atrtc_attach(device_t dev)
286 {
287 	struct atrtc_softc *sc;
288 	rman_res_t s;
289 	int i;
290 
291 	sc = device_get_softc(dev);
292 	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
293 	    IO_RTC, IO_RTC + 1, 2, RF_ACTIVE);
294 	if (sc->port_res == NULL)
295 		device_printf(dev, "Warning: Couldn't map I/O.\n");
296 	atrtc_start();
297 	clock_register(dev, 1000000);
298 	bzero(&sc->et, sizeof(struct eventtimer));
299 	if (!atrtcclock_disable &&
300 	    (resource_int_value(device_get_name(dev), device_get_unit(dev),
301 	     "clock", &i) != 0 || i != 0)) {
302 		sc->intr_rid = 0;
303 		while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
304 		    &s, NULL) == 0 && s != 8)
305 			sc->intr_rid++;
306 		sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
307 		    &sc->intr_rid, 8, 8, 1, RF_ACTIVE);
308 		if (sc->intr_res == NULL) {
309 			device_printf(dev, "Can't map interrupt.\n");
310 			return (0);
311 		} else if ((bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
312 		    rtc_intr, NULL, sc, &sc->intr_handler))) {
313 			device_printf(dev, "Can't setup interrupt.\n");
314 			return (0);
315 		} else {
316 			/* Bind IRQ to BSP to avoid live migration. */
317 			bus_bind_intr(dev, sc->intr_res, 0);
318 		}
319 		sc->et.et_name = "RTC";
320 		sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_POW2DIV;
321 		sc->et.et_quality = 0;
322 		sc->et.et_frequency = 32768;
323 		sc->et.et_min_period = 0x00080000;
324 		sc->et.et_max_period = 0x80000000;
325 		sc->et.et_start = rtc_start;
326 		sc->et.et_stop = rtc_stop;
327 		sc->et.et_priv = dev;
328 		et_register(&sc->et);
329 	}
330 	return(0);
331 }
332 
333 static int
334 atrtc_resume(device_t dev)
335 {
336 
337 	atrtc_restore();
338 	return(0);
339 }
340 
341 static int
342 atrtc_settime(device_t dev __unused, struct timespec *ts)
343 {
344 
345 	atrtc_set(ts);
346 	return (0);
347 }
348 
349 static int
350 atrtc_gettime(device_t dev, struct timespec *ts)
351 {
352 	struct clocktime ct;
353 
354 	/* Look if we have a RTC present and the time is valid */
355 	if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
356 		device_printf(dev, "WARNING: Battery failure indication\n");
357 		return (EINVAL);
358 	}
359 
360 	/*
361 	 * wait for time update to complete
362 	 * If RTCSA_TUP is zero, we have at least 244us before next update.
363 	 * This is fast enough on most hardware, but a refinement would be
364 	 * to make sure that no more than 240us pass after we start reading,
365 	 * and try again if so.
366 	 */
367 	mtx_lock(&atrtc_time_lock);
368 	while (rtcin(RTC_STATUSA) & RTCSA_TUP)
369 		continue;
370 	critical_enter();
371 	ct.nsec = 0;
372 	ct.sec = readrtc(RTC_SEC);
373 	ct.min = readrtc(RTC_MIN);
374 	ct.hour = readrtc(RTC_HRS);
375 	ct.day = readrtc(RTC_DAY);
376 	ct.dow = readrtc(RTC_WDAY) - 1;
377 	ct.mon = readrtc(RTC_MONTH);
378 	ct.year = readrtc(RTC_YEAR);
379 #ifdef USE_RTC_CENTURY
380 	ct.year += readrtc(RTC_CENTURY) * 100;
381 #else
382 	ct.year += (ct.year < 80 ? 2000 : 1900);
383 #endif
384 	critical_exit();
385 	mtx_unlock(&atrtc_time_lock);
386 	/* Set dow = -1 because some clocks don't set it correctly. */
387 	ct.dow = -1;
388 	return (clock_ct_to_ts(&ct, ts));
389 }
390 
391 static device_method_t atrtc_methods[] = {
392 	/* Device interface */
393 	DEVMETHOD(device_probe,		atrtc_probe),
394 	DEVMETHOD(device_attach,	atrtc_attach),
395 	DEVMETHOD(device_detach,	bus_generic_detach),
396 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
397 	DEVMETHOD(device_suspend,	bus_generic_suspend),
398 		/* XXX stop statclock? */
399 	DEVMETHOD(device_resume,	atrtc_resume),
400 
401 	/* clock interface */
402 	DEVMETHOD(clock_gettime,	atrtc_gettime),
403 	DEVMETHOD(clock_settime,	atrtc_settime),
404 
405 	{ 0, 0 }
406 };
407 
408 static driver_t atrtc_driver = {
409 	"atrtc",
410 	atrtc_methods,
411 	sizeof(struct atrtc_softc),
412 };
413 
414 static devclass_t atrtc_devclass;
415 
416 DRIVER_MODULE(atrtc, isa, atrtc_driver, atrtc_devclass, 0, 0);
417 DRIVER_MODULE(atrtc, acpi, atrtc_driver, atrtc_devclass, 0, 0);
418 
419 #include "opt_ddb.h"
420 #ifdef DDB
421 #include <ddb/ddb.h>
422 
423 DB_SHOW_COMMAND(rtc, rtc)
424 {
425 	printf("%02x/%02x/%02x %02x:%02x:%02x, A = %02x, B = %02x, C = %02x\n",
426 		rtcin(RTC_YEAR), rtcin(RTC_MONTH), rtcin(RTC_DAY),
427 		rtcin(RTC_HRS), rtcin(RTC_MIN), rtcin(RTC_SEC),
428 		rtcin(RTC_STATUSA), rtcin(RTC_STATUSB), rtcin(RTC_INTR));
429 }
430 #endif /* DDB */
431