2 * Copyright (c) 2008 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/clock.h>
39 #include <sys/mutex.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
45 #include <isa/isareg.h>
46 #include <isa/isavar.h>
49 #define RTC_LOCK mtx_lock_spin(&clock_lock)
50 #define RTC_UNLOCK mtx_unlock_spin(&clock_lock)
52 static int rtc_reg = -1;
53 static u_char rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
54 static u_char rtc_statusb = RTCSB_24HR;
57 * RTC support routines
72 val = inb(IO_RTC + 1);
78 writertc(int reg, u_char val)
88 outb(IO_RTC + 1, val);
96 return(bcd2bin(rtcin(port)));
103 writertc(RTC_STATUSA, rtc_statusa);
104 writertc(RTC_STATUSB, RTCSB_24HR);
108 atrtc_rate(unsigned rate)
111 rtc_statusa = RTCSA_DIVIDER | rate;
112 writertc(RTC_STATUSA, rtc_statusa);
116 atrtc_enable_intr(void)
119 rtc_statusb |= RTCSB_PINTR;
120 writertc(RTC_STATUSB, rtc_statusb);
128 /* Restore all of the RTC's "status" (actually, control) registers. */
129 rtcin(RTC_STATUSA); /* dummy to get rtc_reg set */
130 writertc(RTC_STATUSB, RTCSB_24HR);
131 writertc(RTC_STATUSA, rtc_statusa);
132 writertc(RTC_STATUSB, rtc_statusb);
136 /**********************************************************************
137 * RTC driver for subr_rtc
140 #include "clock_if.h"
142 #include <sys/rman.h>
145 int port_rid, intr_rid;
146 struct resource *port_res;
147 struct resource *intr_res;
151 * Attach to the ISA PnP descriptors for the timer and realtime clock.
153 static struct isa_pnp_id atrtc_ids[] = {
154 { 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
159 atrtc_probe(device_t dev)
163 device_set_desc(dev, "AT Real Time Clock");
164 result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
165 /* ENXIO if wrong PnP-ID, ENOENT ifno PnP-ID, zero if good PnP-iD */
166 if (result != ENOENT)
168 /* All PC's have an RTC, and we're hosed without it, so... */
169 return (BUS_PROBE_LOW_PRIORITY);
173 atrtc_attach(device_t dev)
175 struct atrtc_softc *sc;
178 * Not that we need them or anything, but grab our resources
179 * so they show up, correctly attributed, in the big picture.
182 sc = device_get_softc(dev);
183 if (!(sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
184 &sc->port_rid, IO_RTC, IO_RTC + 1, 2, RF_ACTIVE)))
185 device_printf(dev,"Warning: Couldn't map I/O.\n");
186 if (!(sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
187 &sc->intr_rid, 8, 8, 1, RF_ACTIVE)))
188 device_printf(dev,"Warning: Couldn't map Interrupt.\n");
189 clock_register(dev, 1000000);
195 atrtc_settime(device_t dev __unused, struct timespec *ts)
199 clock_ts_to_ct(ts, &ct);
201 /* Disable RTC updates and interrupts. */
202 writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
204 writertc(RTC_SEC, bin2bcd(ct.sec)); /* Write back Seconds */
205 writertc(RTC_MIN, bin2bcd(ct.min)); /* Write back Minutes */
206 writertc(RTC_HRS, bin2bcd(ct.hour)); /* Write back Hours */
208 writertc(RTC_WDAY, ct.dow + 1); /* Write back Weekday */
209 writertc(RTC_DAY, bin2bcd(ct.day)); /* Write back Day */
210 writertc(RTC_MONTH, bin2bcd(ct.mon)); /* Write back Month */
211 writertc(RTC_YEAR, bin2bcd(ct.year % 100)); /* Write back Year */
212 #ifdef USE_RTC_CENTURY
213 writertc(RTC_CENTURY, bin2bcd(ct.year / 100)); /* ... and Century */
216 /* Reenable RTC updates and interrupts. */
217 writertc(RTC_STATUSB, rtc_statusb);
223 atrtc_gettime(device_t dev, struct timespec *ts)
228 /* Look if we have a RTC present and the time is valid */
229 if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
230 device_printf(dev, "WARNING: Battery failure indication\n");
234 /* wait for time update to complete */
235 /* If RTCSA_TUP is zero, we have at least 244us before next update */
237 while (rtcin(RTC_STATUSA) & RTCSA_TUP) {
242 ct.sec = readrtc(RTC_SEC);
243 ct.min = readrtc(RTC_MIN);
244 ct.hour = readrtc(RTC_HRS);
245 ct.day = readrtc(RTC_DAY);
246 ct.dow = readrtc(RTC_WDAY) - 1;
247 ct.mon = readrtc(RTC_MONTH);
248 ct.year = readrtc(RTC_YEAR);
249 #ifdef USE_RTC_CENTURY
250 ct.year += readrtc(RTC_CENTURY) * 100;
254 /* Set dow = -1 because some clocks don't set it correctly. */
256 return (clock_ct_to_ts(&ct, ts));
259 static device_method_t atrtc_methods[] = {
260 /* Device interface */
261 DEVMETHOD(device_probe, atrtc_probe),
262 DEVMETHOD(device_attach, atrtc_attach),
263 DEVMETHOD(device_detach, bus_generic_detach),
264 DEVMETHOD(device_shutdown, bus_generic_shutdown),
265 DEVMETHOD(device_suspend, bus_generic_suspend),
266 /* XXX stop statclock? */
267 DEVMETHOD(device_resume, bus_generic_resume),
268 /* XXX restart statclock? */
270 /* clock interface */
271 DEVMETHOD(clock_gettime, atrtc_gettime),
272 DEVMETHOD(clock_settime, atrtc_settime),
277 static driver_t atrtc_driver = {
280 sizeof(struct atrtc_softc),
283 static devclass_t atrtc_devclass;
285 DRIVER_MODULE(atrtc, isa, atrtc_driver, atrtc_devclass, 0, 0);
286 DRIVER_MODULE(atrtc, acpi, atrtc_driver, atrtc_devclass, 0, 0);
292 DB_SHOW_COMMAND(rtc, rtc)
294 printf("%02x/%02x/%02x %02x:%02x:%02x, A = %02x, B = %02x, C = %02x\n",
295 rtcin(RTC_YEAR), rtcin(RTC_MONTH), rtcin(RTC_DAY),
296 rtcin(RTC_HRS), rtcin(RTC_MIN), rtcin(RTC_SEC),
297 rtcin(RTC_STATUSA), rtcin(RTC_STATUSB), rtcin(RTC_INTR));