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