1 /*- 2 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/malloc.h> 37 #include <sys/rman.h> 38 #include <sys/taskqueue.h> 39 #include <sys/timeet.h> 40 #include <sys/timepps.h> 41 #include <sys/timetc.h> 42 #include <sys/watchdog.h> 43 #include <machine/bus.h> 44 #include <machine/cpu.h> 45 #include <machine/intr.h> 46 47 #include "opt_ntp.h" 48 49 #include <dev/fdt/fdt_common.h> 50 #include <dev/ofw/openfirm.h> 51 #include <dev/ofw/ofw_bus.h> 52 #include <dev/ofw/ofw_bus_subr.h> 53 54 #include <machine/bus.h> 55 56 #include <arm/ti/ti_prcm.h> 57 #include <arm/ti/ti_scm.h> 58 59 #define AM335X_NUM_TIMERS 8 60 61 #define DMT_TIDR 0x00 /* Identification Register */ 62 #define DMT_TIOCP_CFG 0x10 /* OCP Configuration Reg */ 63 #define DMT_TIOCP_RESET (1 << 0) /* TIOCP perform soft reset */ 64 #define DMT_IQR_EOI 0x20 /* IRQ End-Of-Interrupt Reg */ 65 #define DMT_IRQSTATUS_RAW 0x24 /* IRQSTATUS Raw Reg */ 66 #define DMT_IRQSTATUS 0x28 /* IRQSTATUS Reg */ 67 #define DMT_IRQENABLE_SET 0x2c /* IRQSTATUS Set Reg */ 68 #define DMT_IRQENABLE_CLR 0x30 /* IRQSTATUS Clear Reg */ 69 #define DMT_IRQWAKEEN 0x34 /* IRQ Wakeup Enable Reg */ 70 #define DMT_IRQ_MAT (1 << 0) /* IRQ: Match */ 71 #define DMT_IRQ_OVF (1 << 1) /* IRQ: Overflow */ 72 #define DMT_IRQ_TCAR (1 << 2) /* IRQ: Capture */ 73 #define DMT_IRQ_MASK (DMT_IRQ_TCAR | DMT_IRQ_OVF | DMT_IRQ_MAT) 74 #define DMT_TCLR 0x38 /* Control Register */ 75 #define DMT_TCLR_START (1 << 0) /* Start timer */ 76 #define DMT_TCLR_AUTOLOAD (1 << 1) /* Auto-reload on overflow */ 77 #define DMT_TCLR_PRES_MASK (7 << 2) /* Prescaler mask */ 78 #define DMT_TCLR_PRES_ENABLE (1 << 5) /* Prescaler enable */ 79 #define DMT_TCLR_COMP_ENABLE (1 << 6) /* Compare enable */ 80 #define DMT_TCLR_PWM_HIGH (1 << 7) /* PWM default output high */ 81 #define DMT_TCLR_CAPTRAN_MASK (3 << 8) /* Capture transition mask */ 82 #define DMT_TCLR_CAPTRAN_NONE (0 << 8) /* Capture: none */ 83 #define DMT_TCLR_CAPTRAN_LOHI (1 << 8) /* Capture lo->hi transition */ 84 #define DMT_TCLR_CAPTRAN_HILO (2 << 8) /* Capture hi->lo transition */ 85 #define DMT_TCLR_CAPTRAN_BOTH (3 << 8) /* Capture both transitions */ 86 #define DMT_TCLR_TRGMODE_MASK (3 << 10) /* Trigger output mode mask */ 87 #define DMT_TCLR_TRGMODE_NONE (0 << 10) /* Trigger off */ 88 #define DMT_TCLR_TRGMODE_OVFL (1 << 10) /* Trigger on overflow */ 89 #define DMT_TCLR_TRGMODE_BOTH (2 << 10) /* Trigger on match + ovflow */ 90 #define DMT_TCLR_PWM_PTOGGLE (1 << 12) /* PWM toggles */ 91 #define DMT_TCLR_CAP_MODE_2ND (1 << 13) /* Capture second event mode */ 92 #define DMT_TCLR_GPO_CFG (1 << 14) /* (no descr in datasheet) */ 93 #define DMT_TCRR 0x3C /* Counter Register */ 94 #define DMT_TLDR 0x40 /* Load Reg */ 95 #define DMT_TTGR 0x44 /* Trigger Reg */ 96 #define DMT_TWPS 0x48 /* Write Posted Status Reg */ 97 #define DMT_TMAR 0x4C /* Match Reg */ 98 #define DMT_TCAR1 0x50 /* Capture Reg */ 99 #define DMT_TSICR 0x54 /* Synchr. Interface Ctrl Reg */ 100 #define DMT_TSICR_RESET (1 << 1) /* TSICR perform soft reset */ 101 #define DMT_TCAR2 0x48 /* Capture Reg */ 102 103 /* 104 * Use timer 2 for the eventtimer. When PPS support is not compiled in, there's 105 * no need to use a timer that has an associated capture-input pin, so use timer 106 * 3 for timecounter. When PPS is compiled in we ignore the default and use 107 * whichever of timers 4-7 have the capture pin configured. 108 */ 109 #define DEFAULT_ET_TIMER 2 110 #define DEFAULT_TC_TIMER 3 111 112 struct am335x_dmtimer_softc { 113 struct resource * tmr_mem_res[AM335X_NUM_TIMERS]; 114 struct resource * tmr_irq_res[AM335X_NUM_TIMERS]; 115 uint32_t sysclk_freq; 116 uint32_t tc_num; /* Which timer number is tc. */ 117 uint32_t tc_tclr; /* Cached tc TCLR register. */ 118 struct resource * tc_memres; /* Resources for tc timer. */ 119 uint32_t et_num; /* Which timer number is et. */ 120 uint32_t et_tclr; /* Cached et TCLR register. */ 121 struct resource * et_memres; /* Resources for et timer. */ 122 int pps_curmode; /* Edge mode now set in hw. */ 123 struct task pps_task; /* For pps_event handling. */ 124 struct cdev * pps_cdev; 125 struct pps_state pps; 126 struct timecounter tc; 127 struct eventtimer et; 128 }; 129 130 static struct am335x_dmtimer_softc *am335x_dmtimer_sc; 131 132 static struct resource_spec am335x_dmtimer_mem_spec[] = { 133 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 134 { SYS_RES_MEMORY, 1, RF_ACTIVE }, 135 { SYS_RES_MEMORY, 2, RF_ACTIVE }, 136 { SYS_RES_MEMORY, 3, RF_ACTIVE }, 137 { SYS_RES_MEMORY, 4, RF_ACTIVE }, 138 { SYS_RES_MEMORY, 5, RF_ACTIVE }, 139 { SYS_RES_MEMORY, 6, RF_ACTIVE }, 140 { SYS_RES_MEMORY, 7, RF_ACTIVE }, 141 { -1, 0, 0 } 142 }; 143 static struct resource_spec am335x_dmtimer_irq_spec[] = { 144 { SYS_RES_IRQ, 0, RF_ACTIVE }, 145 { SYS_RES_IRQ, 1, RF_ACTIVE }, 146 { SYS_RES_IRQ, 2, RF_ACTIVE }, 147 { SYS_RES_IRQ, 3, RF_ACTIVE }, 148 { SYS_RES_IRQ, 4, RF_ACTIVE }, 149 { SYS_RES_IRQ, 5, RF_ACTIVE }, 150 { SYS_RES_IRQ, 6, RF_ACTIVE }, 151 { SYS_RES_IRQ, 7, RF_ACTIVE }, 152 { -1, 0, 0 } 153 }; 154 155 static inline uint32_t 156 am335x_dmtimer_tc_read_4(struct am335x_dmtimer_softc *sc, uint32_t reg) 157 { 158 159 return (bus_read_4(sc->tc_memres, reg)); 160 } 161 162 static inline void 163 am335x_dmtimer_tc_write_4(struct am335x_dmtimer_softc *sc, uint32_t reg, 164 uint32_t val) 165 { 166 167 bus_write_4(sc->tc_memres, reg, val); 168 } 169 170 static inline uint32_t 171 am335x_dmtimer_et_read_4(struct am335x_dmtimer_softc *sc, uint32_t reg) 172 { 173 174 return (bus_read_4(sc->et_memres, reg)); 175 } 176 177 static inline void 178 am335x_dmtimer_et_write_4(struct am335x_dmtimer_softc *sc, uint32_t reg, 179 uint32_t val) 180 { 181 182 bus_write_4(sc->et_memres, reg, val); 183 } 184 185 /* 186 * PPS driver routines, included when the kernel is built with option PPS_SYNC. 187 * 188 * Note that this PPS driver does not use an interrupt. Instead it uses the 189 * hardware's ability to latch the timer's count register in response to a 190 * signal on an IO pin. Each of timers 4-7 have an associated pin, and this 191 * code allows any one of those to be used. 192 * 193 * The timecounter routines in kern_tc.c call the pps poll routine periodically 194 * to see if a new counter value has been latched. When a new value has been 195 * latched, the only processing done in the poll routine is to capture the 196 * current set of timecounter timehands (done with pps_capture()) and the 197 * latched value from the timer. The remaining work (done by pps_event()) is 198 * scheduled to be done later in a non-interrupt context. 199 */ 200 #ifdef PPS_SYNC 201 202 #define PPS_CDEV_NAME "dmtpps" 203 204 static void 205 am335x_dmtimer_set_capture_mode(struct am335x_dmtimer_softc *sc, bool force_off) 206 { 207 int newmode; 208 209 if (force_off) 210 newmode = 0; 211 else 212 newmode = sc->pps.ppsparam.mode & PPS_CAPTUREBOTH; 213 214 if (newmode == sc->pps_curmode) 215 return; 216 217 sc->pps_curmode = newmode; 218 sc->tc_tclr &= ~DMT_TCLR_CAPTRAN_MASK; 219 switch (newmode) { 220 case PPS_CAPTUREASSERT: 221 sc->tc_tclr |= DMT_TCLR_CAPTRAN_LOHI; 222 break; 223 case PPS_CAPTURECLEAR: 224 sc->tc_tclr |= DMT_TCLR_CAPTRAN_HILO; 225 break; 226 default: 227 /* It can't be BOTH, so it's disabled. */ 228 break; 229 } 230 am335x_dmtimer_tc_write_4(sc, DMT_TCLR, sc->tc_tclr); 231 } 232 233 static void 234 am335x_dmtimer_tc_poll_pps(struct timecounter *tc) 235 { 236 struct am335x_dmtimer_softc *sc; 237 238 sc = tc->tc_priv; 239 240 /* 241 * Note that we don't have the TCAR interrupt enabled, but the hardware 242 * still provides the status bits in the "RAW" status register even when 243 * they're masked from generating an irq. However, when clearing the 244 * TCAR status to re-arm the capture for the next second, we have to 245 * write to the IRQ status register, not the RAW register. Quirky. 246 */ 247 if (am335x_dmtimer_tc_read_4(sc, DMT_IRQSTATUS_RAW) & DMT_IRQ_TCAR) { 248 pps_capture(&sc->pps); 249 sc->pps.capcount = am335x_dmtimer_tc_read_4(sc, DMT_TCAR1); 250 am335x_dmtimer_tc_write_4(sc, DMT_IRQSTATUS, DMT_IRQ_TCAR); 251 taskqueue_enqueue_fast(taskqueue_fast, &sc->pps_task); 252 } 253 } 254 255 static void 256 am335x_dmtimer_process_pps_event(void *arg, int pending) 257 { 258 struct am335x_dmtimer_softc *sc; 259 260 sc = arg; 261 262 /* This is the task function that gets enqueued by poll_pps. Once the 263 * time has been captured in the hw interrupt context, the remaining 264 * (more expensive) work to process the event is done later in a 265 * non-fast-interrupt context. 266 * 267 * We only support capture of the rising or falling edge, not both at 268 * once; tell the kernel to process whichever mode is currently active. 269 */ 270 pps_event(&sc->pps, sc->pps.ppsparam.mode & PPS_CAPTUREBOTH); 271 } 272 273 static int 274 am335x_dmtimer_pps_open(struct cdev *dev, int flags, int fmt, 275 struct thread *td) 276 { 277 struct am335x_dmtimer_softc *sc; 278 279 sc = dev->si_drv1; 280 281 /* Enable capture on open. Harmless if already open. */ 282 am335x_dmtimer_set_capture_mode(sc, 0); 283 284 return 0; 285 } 286 287 static int 288 am335x_dmtimer_pps_close(struct cdev *dev, int flags, int fmt, 289 struct thread *td) 290 { 291 struct am335x_dmtimer_softc *sc; 292 293 sc = dev->si_drv1; 294 295 /* 296 * Disable capture on last close. Use the force-off flag to override 297 * the configured mode and turn off the hardware capture. 298 */ 299 am335x_dmtimer_set_capture_mode(sc, 1); 300 301 return 0; 302 } 303 304 static int 305 am335x_dmtimer_pps_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 306 int flags, struct thread *td) 307 { 308 struct am335x_dmtimer_softc *sc; 309 int err; 310 311 sc = dev->si_drv1; 312 313 /* 314 * The hardware has a "capture both edges" mode, but we can't do 315 * anything useful with it in terms of PPS capture, so don't even try. 316 */ 317 if ((sc->pps.ppsparam.mode & PPS_CAPTUREBOTH) == PPS_CAPTUREBOTH) 318 return (EINVAL); 319 320 /* Let the kernel do the heavy lifting for ioctl. */ 321 err = pps_ioctl(cmd, data, &sc->pps); 322 if (err != 0) 323 return (err); 324 325 /* 326 * The capture mode could have changed, set the hardware to whatever 327 * mode is now current. Effectively a no-op if nothing changed. 328 */ 329 am335x_dmtimer_set_capture_mode(sc, 0); 330 331 return (err); 332 } 333 334 static struct cdevsw am335x_dmtimer_pps_cdevsw = { 335 .d_version = D_VERSION, 336 .d_open = am335x_dmtimer_pps_open, 337 .d_close = am335x_dmtimer_pps_close, 338 .d_ioctl = am335x_dmtimer_pps_ioctl, 339 .d_name = PPS_CDEV_NAME, 340 }; 341 342 /* 343 * Set up the PPS cdev and the the kernel timepps stuff. 344 * 345 * Note that this routine cannot touch the hardware, because bus space resources 346 * are not fully set up yet when this is called. 347 */ 348 static int 349 am335x_dmtimer_pps_init(device_t dev, struct am335x_dmtimer_softc *sc) 350 { 351 int i, timer_num, unit; 352 unsigned int padstate; 353 const char * padmux; 354 struct padinfo { 355 char * ballname; 356 char * muxname; 357 int timer_num; 358 } padinfo[] = { 359 {"GPMC_ADVn_ALE", "timer4", 4}, 360 {"GPMC_BEn0_CLE", "timer5", 5}, 361 {"GPMC_WEn", "timer6", 6}, 362 {"GPMC_OEn_REn", "timer7", 7}, 363 }; 364 365 /* 366 * Figure out which pin the user has set up for pps. We'll use the 367 * first timer that has an external caputure pin configured as input. 368 * 369 * XXX The hieroglyphic "(padstate & (0x01 << 5)))" checks that the pin 370 * is configured for input. The right symbolic values aren't exported 371 * yet from ti_scm.h. 372 */ 373 timer_num = 0; 374 for (i = 0; i < nitems(padinfo) && timer_num == 0; ++i) { 375 if (ti_scm_padconf_get(padinfo[i].ballname, &padmux, 376 &padstate) == 0) { 377 if (strcasecmp(padinfo[i].muxname, padmux) == 0 && 378 (padstate & (0x01 << 5))) 379 timer_num = padinfo[i].timer_num; 380 } 381 } 382 383 if (timer_num == 0) { 384 device_printf(dev, "No DMTimer found with capture pin " 385 "configured as input; PPS driver disabled.\n"); 386 return (DEFAULT_TC_TIMER); 387 } 388 389 /* 390 * Indicate our capabilities (pretty much just capture of either edge). 391 * Have the kernel init its part of the pps_state struct and add its 392 * capabilities. 393 */ 394 sc->pps.ppscap = PPS_CAPTUREBOTH; 395 pps_init(&sc->pps); 396 397 /* 398 * Set up to capture the PPS via timecounter polling, and init the task 399 * that does deferred pps_event() processing after capture. 400 */ 401 sc->tc.tc_poll_pps = am335x_dmtimer_tc_poll_pps; 402 TASK_INIT(&sc->pps_task, 0, am335x_dmtimer_process_pps_event, sc); 403 404 /* Create the PPS cdev. */ 405 unit = device_get_unit(dev); 406 sc->pps_cdev = make_dev(&am335x_dmtimer_pps_cdevsw, unit, 407 UID_ROOT, GID_WHEEL, 0600, PPS_CDEV_NAME "%d", unit); 408 sc->pps_cdev->si_drv1 = sc; 409 410 device_printf(dev, "Using DMTimer%d for PPS device /dev/%s%d\n", 411 timer_num, PPS_CDEV_NAME, unit); 412 413 return (timer_num); 414 } 415 416 #else /* PPS_SYNC */ 417 418 static int 419 am335x_dmtimer_pps_init(device_t dev, struct am335x_dmtimer_softc *sc) 420 { 421 422 /* 423 * When PPS support is not compiled in, there's no need to use a timer 424 * that has an associated capture-input pin, so use the default. 425 */ 426 return (DEFAULT_TC_TIMER); 427 } 428 429 #endif /* PPS_SYNC */ 430 /* 431 * End of PPS driver code. 432 */ 433 434 static unsigned 435 am335x_dmtimer_tc_get_timecount(struct timecounter *tc) 436 { 437 struct am335x_dmtimer_softc *sc; 438 439 sc = tc->tc_priv; 440 441 return (am335x_dmtimer_tc_read_4(sc, DMT_TCRR)); 442 } 443 444 static int 445 am335x_dmtimer_start(struct eventtimer *et, sbintime_t first, sbintime_t period) 446 { 447 struct am335x_dmtimer_softc *sc; 448 uint32_t initial_count, reload_count; 449 450 sc = et->et_priv; 451 452 /* 453 * Stop the timer before changing it. This routine will often be called 454 * while the timer is still running, to either lengthen or shorten the 455 * current event time. We need to ensure the timer doesn't expire while 456 * we're working with it. 457 * 458 * Also clear any pending interrupt status, because it's at least 459 * theoretically possible that we're running in a primary interrupt 460 * context now, and a timer interrupt could be pending even before we 461 * stopped the timer. The more likely case is that we're being called 462 * from the et_event_cb() routine dispatched from our own handler, but 463 * it's not clear to me that that's the only case possible. 464 */ 465 sc->et_tclr &= ~(DMT_TCLR_START | DMT_TCLR_AUTOLOAD); 466 am335x_dmtimer_et_write_4(sc, DMT_TCLR, sc->et_tclr); 467 am335x_dmtimer_et_write_4(sc, DMT_IRQSTATUS, DMT_IRQ_OVF); 468 469 if (period != 0) { 470 reload_count = ((uint32_t)et->et_frequency * period) >> 32; 471 sc->et_tclr |= DMT_TCLR_AUTOLOAD; 472 } else { 473 reload_count = 0; 474 } 475 476 if (first != 0) 477 initial_count = ((uint32_t)et->et_frequency * first) >> 32; 478 else 479 initial_count = reload_count; 480 481 /* 482 * Set auto-reload and current-count values. This timer hardware counts 483 * up from the initial/reload value and interrupts on the zero rollover. 484 */ 485 am335x_dmtimer_et_write_4(sc, DMT_TLDR, 0xFFFFFFFF - reload_count); 486 am335x_dmtimer_et_write_4(sc, DMT_TCRR, 0xFFFFFFFF - initial_count); 487 488 /* Enable overflow interrupt, and start the timer. */ 489 am335x_dmtimer_et_write_4(sc, DMT_IRQENABLE_SET, DMT_IRQ_OVF); 490 sc->et_tclr |= DMT_TCLR_START; 491 am335x_dmtimer_et_write_4(sc, DMT_TCLR, sc->et_tclr); 492 493 return (0); 494 } 495 496 static int 497 am335x_dmtimer_stop(struct eventtimer *et) 498 { 499 struct am335x_dmtimer_softc *sc; 500 501 sc = et->et_priv; 502 503 /* Stop timer, disable and clear interrupt. */ 504 sc->et_tclr &= ~(DMT_TCLR_START | DMT_TCLR_AUTOLOAD); 505 am335x_dmtimer_et_write_4(sc, DMT_TCLR, sc->et_tclr); 506 am335x_dmtimer_et_write_4(sc, DMT_IRQENABLE_CLR, DMT_IRQ_OVF); 507 am335x_dmtimer_et_write_4(sc, DMT_IRQSTATUS, DMT_IRQ_OVF); 508 return (0); 509 } 510 511 static int 512 am335x_dmtimer_intr(void *arg) 513 { 514 struct am335x_dmtimer_softc *sc; 515 516 sc = arg; 517 518 /* Ack the interrupt, and invoke the callback if it's still enabled. */ 519 am335x_dmtimer_et_write_4(sc, DMT_IRQSTATUS, DMT_IRQ_OVF); 520 if (sc->et.et_active) 521 sc->et.et_event_cb(&sc->et, sc->et.et_arg); 522 523 return (FILTER_HANDLED); 524 } 525 526 static int 527 am335x_dmtimer_probe(device_t dev) 528 { 529 530 if (!ofw_bus_status_okay(dev)) 531 return (ENXIO); 532 533 if (ofw_bus_is_compatible(dev, "ti,am335x-dmtimer")) { 534 device_set_desc(dev, "AM335x DMTimer"); 535 return(BUS_PROBE_DEFAULT); 536 } 537 538 return (ENXIO); 539 } 540 541 static int 542 am335x_dmtimer_attach(device_t dev) 543 { 544 struct am335x_dmtimer_softc *sc; 545 void *ihl; 546 int err; 547 548 /* 549 * Note that if this routine returns an error status rather than running 550 * to completion it makes no attempt to clean up allocated resources; 551 * the system is essentially dead anyway without functional timers. 552 */ 553 554 sc = device_get_softc(dev); 555 556 if (am335x_dmtimer_sc != NULL) 557 return (EINVAL); 558 559 /* Get the base clock frequency. */ 560 err = ti_prcm_clk_get_source_freq(SYS_CLK, &sc->sysclk_freq); 561 if (err) { 562 device_printf(dev, "Error: could not get sysclk frequency\n"); 563 return (ENXIO); 564 } 565 566 /* Request the memory resources. */ 567 err = bus_alloc_resources(dev, am335x_dmtimer_mem_spec, 568 sc->tmr_mem_res); 569 if (err) { 570 device_printf(dev, "Error: could not allocate mem resources\n"); 571 return (ENXIO); 572 } 573 574 /* Request the IRQ resources. */ 575 err = bus_alloc_resources(dev, am335x_dmtimer_irq_spec, 576 sc->tmr_irq_res); 577 if (err) { 578 device_printf(dev, "Error: could not allocate irq resources\n"); 579 return (ENXIO); 580 } 581 582 /* 583 * Use the default eventtimer. Let the PPS init routine decide which 584 * timer to use for the timecounter. 585 */ 586 sc->et_num = DEFAULT_ET_TIMER; 587 sc->tc_num = am335x_dmtimer_pps_init(dev, sc); 588 589 sc->et_memres = sc->tmr_mem_res[sc->et_num]; 590 sc->tc_memres = sc->tmr_mem_res[sc->tc_num]; 591 592 /* Enable clocks and power on the chosen devices. */ 593 err = ti_prcm_clk_set_source(DMTIMER0_CLK + sc->et_num, SYSCLK_CLK); 594 err |= ti_prcm_clk_enable(DMTIMER0_CLK + sc->et_num); 595 err |= ti_prcm_clk_set_source(DMTIMER0_CLK + sc->tc_num, SYSCLK_CLK); 596 err |= ti_prcm_clk_enable(DMTIMER0_CLK + sc->tc_num); 597 if (err) { 598 device_printf(dev, "Error: could not enable timer clock\n"); 599 return (ENXIO); 600 } 601 602 /* Setup eventtimer interrupt handler. */ 603 if (bus_setup_intr(dev, sc->tmr_irq_res[sc->et_num], INTR_TYPE_CLK, 604 am335x_dmtimer_intr, NULL, sc, &ihl) != 0) { 605 device_printf(dev, "Unable to setup the clock irq handler.\n"); 606 return (ENXIO); 607 } 608 609 /* Set up timecounter, start it, register it. */ 610 am335x_dmtimer_tc_write_4(sc, DMT_TSICR, DMT_TSICR_RESET); 611 while (am335x_dmtimer_tc_read_4(sc, DMT_TIOCP_CFG) & DMT_TIOCP_RESET) 612 continue; 613 614 sc->tc_tclr |= DMT_TCLR_START | DMT_TCLR_AUTOLOAD; 615 am335x_dmtimer_tc_write_4(sc, DMT_TLDR, 0); 616 am335x_dmtimer_tc_write_4(sc, DMT_TCRR, 0); 617 am335x_dmtimer_tc_write_4(sc, DMT_TCLR, sc->tc_tclr); 618 619 sc->tc.tc_name = "AM335x Timecounter"; 620 sc->tc.tc_get_timecount = am335x_dmtimer_tc_get_timecount; 621 sc->tc.tc_counter_mask = ~0u; 622 sc->tc.tc_frequency = sc->sysclk_freq; 623 sc->tc.tc_quality = 1000; 624 sc->tc.tc_priv = sc; 625 tc_init(&sc->tc); 626 627 sc->et.et_name = "AM335x Eventtimer"; 628 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT; 629 sc->et.et_quality = 1000; 630 sc->et.et_frequency = sc->sysclk_freq; 631 sc->et.et_min_period = 632 ((0x00000005LLU << 32) / sc->et.et_frequency); 633 sc->et.et_max_period = 634 (0xfffffffeLLU << 32) / sc->et.et_frequency; 635 sc->et.et_start = am335x_dmtimer_start; 636 sc->et.et_stop = am335x_dmtimer_stop; 637 sc->et.et_priv = sc; 638 et_register(&sc->et); 639 640 /* Store a pointer to the softc for use in DELAY(). */ 641 am335x_dmtimer_sc = sc; 642 643 return (0); 644 } 645 646 static device_method_t am335x_dmtimer_methods[] = { 647 DEVMETHOD(device_probe, am335x_dmtimer_probe), 648 DEVMETHOD(device_attach, am335x_dmtimer_attach), 649 { 0, 0 } 650 }; 651 652 static driver_t am335x_dmtimer_driver = { 653 "am335x_dmtimer", 654 am335x_dmtimer_methods, 655 sizeof(struct am335x_dmtimer_softc), 656 }; 657 658 static devclass_t am335x_dmtimer_devclass; 659 660 DRIVER_MODULE(am335x_dmtimer, simplebus, am335x_dmtimer_driver, am335x_dmtimer_devclass, 0, 0); 661 MODULE_DEPEND(am335x_dmtimer, am335x_prcm, 1, 1, 1); 662 663 void 664 DELAY(int usec) 665 { 666 struct am335x_dmtimer_softc *sc; 667 int32_t counts; 668 uint32_t first, last; 669 670 sc = am335x_dmtimer_sc; 671 672 if (sc == NULL) { 673 for (; usec > 0; usec--) 674 for (counts = 200; counts > 0; counts--) 675 /* Prevent gcc from optimizing out the loop */ 676 cpufunc_nullop(); 677 return; 678 } 679 680 /* Get the number of times to count */ 681 counts = (usec + 1) * (sc->sysclk_freq / 1000000); 682 683 first = am335x_dmtimer_tc_read_4(sc, DMT_TCRR); 684 685 while (counts > 0) { 686 last = am335x_dmtimer_tc_read_4(sc, DMT_TCRR); 687 if (last > first) { 688 counts -= (int32_t)(last - first); 689 } else { 690 counts -= (int32_t)((0xFFFFFFFF - first) + last); 691 } 692 first = last; 693 } 694 } 695 696