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