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