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