1a2c472e7SAleksandr Rybalko /*- 294f8d6fdSAleksandr Rybalko * Copyright (c) 2012, 2013 The FreeBSD Foundation 3a2c472e7SAleksandr Rybalko * All rights reserved. 4a2c472e7SAleksandr Rybalko * 5a2c472e7SAleksandr Rybalko * This software was developed by Oleksandr Rybalko under sponsorship 6a2c472e7SAleksandr Rybalko * from the FreeBSD Foundation. 7a2c472e7SAleksandr Rybalko * 8a2c472e7SAleksandr Rybalko * Redistribution and use in source and binary forms, with or without 9a2c472e7SAleksandr Rybalko * modification, are permitted provided that the following conditions 10a2c472e7SAleksandr Rybalko * are met: 11a2c472e7SAleksandr Rybalko * 1. Redistributions of source code must retain the above copyright 12a2c472e7SAleksandr Rybalko * notice, this list of conditions and the following disclaimer. 13a2c472e7SAleksandr Rybalko * 2. Redistributions in binary form must reproduce the above copyright 14a2c472e7SAleksandr Rybalko * notice, this list of conditions and the following disclaimer in the 15a2c472e7SAleksandr Rybalko * documentation and/or other materials provided with the distribution. 16a2c472e7SAleksandr Rybalko * 17a2c472e7SAleksandr Rybalko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18a2c472e7SAleksandr Rybalko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19a2c472e7SAleksandr Rybalko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20a2c472e7SAleksandr Rybalko * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21a2c472e7SAleksandr Rybalko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22a2c472e7SAleksandr Rybalko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23a2c472e7SAleksandr Rybalko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24a2c472e7SAleksandr Rybalko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25a2c472e7SAleksandr Rybalko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26a2c472e7SAleksandr Rybalko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27a2c472e7SAleksandr Rybalko * SUCH DAMAGE. 28a2c472e7SAleksandr Rybalko */ 29a2c472e7SAleksandr Rybalko 30a2c472e7SAleksandr Rybalko #include <sys/cdefs.h> 31a2c472e7SAleksandr Rybalko __FBSDID("$FreeBSD$"); 32a2c472e7SAleksandr Rybalko 33a2c472e7SAleksandr Rybalko #include <sys/param.h> 34a2c472e7SAleksandr Rybalko #include <sys/systm.h> 35a2c472e7SAleksandr Rybalko #include <sys/bus.h> 36a2c472e7SAleksandr Rybalko #include <sys/kernel.h> 37a2c472e7SAleksandr Rybalko #include <sys/module.h> 38a2c472e7SAleksandr Rybalko #include <sys/malloc.h> 39a2c472e7SAleksandr Rybalko #include <sys/rman.h> 40a2c472e7SAleksandr Rybalko #include <sys/timeet.h> 41a2c472e7SAleksandr Rybalko #include <sys/timetc.h> 42a2c472e7SAleksandr Rybalko #include <sys/watchdog.h> 43a2c472e7SAleksandr Rybalko #include <machine/bus.h> 44a2c472e7SAleksandr Rybalko #include <machine/cpu.h> 45a2c472e7SAleksandr Rybalko #include <machine/intr.h> 46a2c472e7SAleksandr Rybalko 47a2c472e7SAleksandr Rybalko #include <dev/ofw/openfirm.h> 48a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus.h> 49a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus_subr.h> 50a2c472e7SAleksandr Rybalko 51a2c472e7SAleksandr Rybalko #include <arm/freescale/imx/imx_gptvar.h> 52a2c472e7SAleksandr Rybalko #include <arm/freescale/imx/imx_gptreg.h> 53a2c472e7SAleksandr Rybalko 54a2c472e7SAleksandr Rybalko #include <sys/kdb.h> 55a7fa939bSIan Lepore #include <arm/freescale/imx/imx_ccmvar.h> 56a2c472e7SAleksandr Rybalko 57a2c472e7SAleksandr Rybalko #define WRITE4(_sc, _r, _v) \ 58a2c472e7SAleksandr Rybalko bus_space_write_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r), (_v)) 59a2c472e7SAleksandr Rybalko #define READ4(_sc, _r) \ 60a2c472e7SAleksandr Rybalko bus_space_read_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r)) 61a2c472e7SAleksandr Rybalko #define SET4(_sc, _r, _m) \ 62a2c472e7SAleksandr Rybalko WRITE4((_sc), (_r), READ4((_sc), (_r)) | (_m)) 63a2c472e7SAleksandr Rybalko #define CLEAR4(_sc, _r, _m) \ 64a2c472e7SAleksandr Rybalko WRITE4((_sc), (_r), READ4((_sc), (_r)) & ~(_m)) 65a2c472e7SAleksandr Rybalko 66a2c472e7SAleksandr Rybalko static u_int imx_gpt_get_timecount(struct timecounter *); 67a2c472e7SAleksandr Rybalko static int imx_gpt_timer_start(struct eventtimer *, sbintime_t, 68a2c472e7SAleksandr Rybalko sbintime_t); 69a2c472e7SAleksandr Rybalko static int imx_gpt_timer_stop(struct eventtimer *); 70a2c472e7SAleksandr Rybalko 71a2c472e7SAleksandr Rybalko static int imx_gpt_intr(void *); 72a2c472e7SAleksandr Rybalko static int imx_gpt_probe(device_t); 73a2c472e7SAleksandr Rybalko static int imx_gpt_attach(device_t); 74a2c472e7SAleksandr Rybalko 75a2c472e7SAleksandr Rybalko static struct timecounter imx_gpt_timecounter = { 76f3549ad5SIan Lepore .tc_name = "iMXGPT", 77a2c472e7SAleksandr Rybalko .tc_get_timecount = imx_gpt_get_timecount, 78a2c472e7SAleksandr Rybalko .tc_counter_mask = ~0u, 79a2c472e7SAleksandr Rybalko .tc_frequency = 0, 80e0511b6cSIan Lepore .tc_quality = 1000, 81a2c472e7SAleksandr Rybalko }; 82a2c472e7SAleksandr Rybalko 83e0511b6cSIan Lepore /* Global softc pointer for use in DELAY(). */ 84a2c472e7SAleksandr Rybalko struct imx_gpt_softc *imx_gpt_sc = NULL; 85e0511b6cSIan Lepore 86e0511b6cSIan Lepore /* 87e0511b6cSIan Lepore * Hand-calibrated delay-loop counter. This was calibrated on an i.MX6 running 88e0511b6cSIan Lepore * at 792mhz. It will delay a bit too long on slower processors -- that's 89e0511b6cSIan Lepore * better than not delaying long enough. In practice this is unlikely to get 90e0511b6cSIan Lepore * used much since the clock driver is one of the first to start up, and once 91e0511b6cSIan Lepore * we're attached the delay loop switches to using the timer hardware. 92e0511b6cSIan Lepore */ 93e0511b6cSIan Lepore static const int imx_gpt_delay_count = 78; 94e0511b6cSIan Lepore 95e0511b6cSIan Lepore /* Try to divide down an available fast clock to this frequency. */ 96c90fadc0SIan Lepore #define TARGET_FREQUENCY 1000000000 97e0511b6cSIan Lepore 98e0511b6cSIan Lepore /* Don't try to set an event timer period smaller than this. */ 99e0511b6cSIan Lepore #define MIN_ET_PERIOD 10LLU 100e0511b6cSIan Lepore 101a2c472e7SAleksandr Rybalko 102a2c472e7SAleksandr Rybalko static struct resource_spec imx_gpt_spec[] = { 103a2c472e7SAleksandr Rybalko { SYS_RES_MEMORY, 0, RF_ACTIVE }, 104a2c472e7SAleksandr Rybalko { SYS_RES_IRQ, 0, RF_ACTIVE }, 105a2c472e7SAleksandr Rybalko { -1, 0 } 106a2c472e7SAleksandr Rybalko }; 107a2c472e7SAleksandr Rybalko 108eb756ebdSIan Lepore static struct ofw_compat_data compat_data[] = { 109*c85d45a5SOleksandr Tymoshenko {"fsl,imx6dl-gpt", 1}, 110eb756ebdSIan Lepore {"fsl,imx6q-gpt", 1}, 111eb756ebdSIan Lepore {"fsl,imx53-gpt", 1}, 112eb756ebdSIan Lepore {"fsl,imx51-gpt", 1}, 113eb756ebdSIan Lepore {"fsl,imx31-gpt", 1}, 114eb756ebdSIan Lepore {"fsl,imx27-gpt", 1}, 115eb756ebdSIan Lepore {"fsl,imx25-gpt", 1}, 116eb756ebdSIan Lepore {NULL, 0} 117eb756ebdSIan Lepore }; 118eb756ebdSIan Lepore 119a2c472e7SAleksandr Rybalko static int 120a2c472e7SAleksandr Rybalko imx_gpt_probe(device_t dev) 121a2c472e7SAleksandr Rybalko { 122a2c472e7SAleksandr Rybalko 123add35ed5SIan Lepore if (!ofw_bus_status_okay(dev)) 124add35ed5SIan Lepore return (ENXIO); 125add35ed5SIan Lepore 126eb756ebdSIan Lepore if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 127e0511b6cSIan Lepore device_set_desc(dev, "Freescale i.MX GPT timer"); 128a2c472e7SAleksandr Rybalko return (BUS_PROBE_DEFAULT); 129a2c472e7SAleksandr Rybalko } 130a2c472e7SAleksandr Rybalko 131eb756ebdSIan Lepore return (ENXIO); 132eb756ebdSIan Lepore } 133eb756ebdSIan Lepore 134a2c472e7SAleksandr Rybalko static int 135a2c472e7SAleksandr Rybalko imx_gpt_attach(device_t dev) 136a2c472e7SAleksandr Rybalko { 137a2c472e7SAleksandr Rybalko struct imx_gpt_softc *sc; 138e0511b6cSIan Lepore int ctlreg, err; 139e0511b6cSIan Lepore uint32_t basefreq, prescale; 140a2c472e7SAleksandr Rybalko 141a2c472e7SAleksandr Rybalko sc = device_get_softc(dev); 142a2c472e7SAleksandr Rybalko 143a2c472e7SAleksandr Rybalko if (bus_alloc_resources(dev, imx_gpt_spec, sc->res)) { 144a2c472e7SAleksandr Rybalko device_printf(dev, "could not allocate resources\n"); 145a2c472e7SAleksandr Rybalko return (ENXIO); 146a2c472e7SAleksandr Rybalko } 147a2c472e7SAleksandr Rybalko 148a2c472e7SAleksandr Rybalko sc->sc_dev = dev; 149a2c472e7SAleksandr Rybalko sc->sc_iot = rman_get_bustag(sc->res[0]); 150a2c472e7SAleksandr Rybalko sc->sc_ioh = rman_get_bushandle(sc->res[0]); 151a2c472e7SAleksandr Rybalko 152e0511b6cSIan Lepore /* 153e0511b6cSIan Lepore * For now, just automatically choose a good clock for the hardware 154e0511b6cSIan Lepore * we're running on. Eventually we could allow selection from the fdt; 155e0511b6cSIan Lepore * the code in this driver will cope with any clock frequency. 156e0511b6cSIan Lepore */ 157e0511b6cSIan Lepore sc->sc_clksrc = GPT_CR_CLKSRC_IPG; 158e0511b6cSIan Lepore 159e0511b6cSIan Lepore ctlreg = 0; 160e0511b6cSIan Lepore 161a2c472e7SAleksandr Rybalko switch (sc->sc_clksrc) { 162a2c472e7SAleksandr Rybalko case GPT_CR_CLKSRC_32K: 163e0511b6cSIan Lepore basefreq = 32768; 164e0511b6cSIan Lepore break; 165e0511b6cSIan Lepore case GPT_CR_CLKSRC_IPG: 166a7fa939bSIan Lepore basefreq = imx_ccm_ipg_hz(); 167a2c472e7SAleksandr Rybalko break; 168a2c472e7SAleksandr Rybalko case GPT_CR_CLKSRC_IPG_HIGH: 169a7fa939bSIan Lepore basefreq = imx_ccm_ipg_hz() * 2; 170a2c472e7SAleksandr Rybalko break; 171e0511b6cSIan Lepore case GPT_CR_CLKSRC_24M: 172e0511b6cSIan Lepore ctlreg |= GPT_CR_24MEN; 173e0511b6cSIan Lepore basefreq = 24000000; 174e0511b6cSIan Lepore break; 175e0511b6cSIan Lepore case GPT_CR_CLKSRC_NONE:/* Can't run without a clock. */ 176e0511b6cSIan Lepore case GPT_CR_CLKSRC_EXT: /* No way to get the freq of an ext clock. */ 177a2c472e7SAleksandr Rybalko default: 178e0511b6cSIan Lepore device_printf(dev, "Unsupported clock source '%d'\n", 179e0511b6cSIan Lepore sc->sc_clksrc); 180e0511b6cSIan Lepore return (EINVAL); 181a2c472e7SAleksandr Rybalko } 182a2c472e7SAleksandr Rybalko 183e0511b6cSIan Lepore /* 184e0511b6cSIan Lepore * The following setup sequence is from the I.MX6 reference manual, 185e0511b6cSIan Lepore * "Selecting the clock source". First, disable the clock and 186e0511b6cSIan Lepore * interrupts. This also clears input and output mode bits and in 187e0511b6cSIan Lepore * general completes several of the early steps in the procedure. 188e0511b6cSIan Lepore */ 189e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_CR, 0); 190a2c472e7SAleksandr Rybalko WRITE4(sc, IMX_GPT_IR, 0); 191a2c472e7SAleksandr Rybalko 192e0511b6cSIan Lepore /* Choose the clock and the power-saving behaviors. */ 193e0511b6cSIan Lepore ctlreg |= 194e0511b6cSIan Lepore sc->sc_clksrc | /* Use selected clock */ 195e0511b6cSIan Lepore GPT_CR_FRR | /* Just count (FreeRunner mode) */ 196e0511b6cSIan Lepore GPT_CR_STOPEN | /* Run in STOP mode */ 197e0511b6cSIan Lepore GPT_CR_DOZEEN | /* Run in DOZE mode */ 198e0511b6cSIan Lepore GPT_CR_WAITEN | /* Run in WAIT mode */ 199e0511b6cSIan Lepore GPT_CR_DBGEN; /* Run in DEBUG mode */ 200e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_CR, ctlreg); 201a2c472e7SAleksandr Rybalko 202e0511b6cSIan Lepore /* 203e0511b6cSIan Lepore * The datasheet says to do the software reset after choosing the clock 204e0511b6cSIan Lepore * source. It says nothing about needing to wait for the reset to 205e0511b6cSIan Lepore * complete, but the register description does document the fact that 206e0511b6cSIan Lepore * the reset isn't complete until the SWR bit reads 0, so let's be safe. 207e0511b6cSIan Lepore * The reset also clears all registers except for a few of the bits in 208e0511b6cSIan Lepore * CR, but we'll rewrite all the CR bits when we start the counter. 209e0511b6cSIan Lepore */ 210e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_CR, ctlreg | GPT_CR_SWR); 211e0511b6cSIan Lepore while (READ4(sc, IMX_GPT_CR) & GPT_CR_SWR) 212e0511b6cSIan Lepore continue; 213e0511b6cSIan Lepore 214e0511b6cSIan Lepore /* Set a prescaler value that gets us near the target frequency. */ 215e0511b6cSIan Lepore if (basefreq < TARGET_FREQUENCY) { 216e0511b6cSIan Lepore prescale = 0; 217e0511b6cSIan Lepore sc->clkfreq = basefreq; 218e0511b6cSIan Lepore } else { 219e0511b6cSIan Lepore prescale = basefreq / TARGET_FREQUENCY; 220e0511b6cSIan Lepore sc->clkfreq = basefreq / prescale; 221e0511b6cSIan Lepore prescale -= 1; /* 1..n range is 0..n-1 in hardware. */ 222e0511b6cSIan Lepore } 223e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_PR, prescale); 224e0511b6cSIan Lepore 225e0511b6cSIan Lepore /* Clear the status register. */ 226e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_SR, GPT_IR_ALL); 227e0511b6cSIan Lepore 228e0511b6cSIan Lepore /* Start the counter. */ 229e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_CR, ctlreg | GPT_CR_EN); 230e0511b6cSIan Lepore 231e0511b6cSIan Lepore if (bootverbose) 232e0511b6cSIan Lepore device_printf(dev, "Running on %dKHz clock, base freq %uHz CR=0x%08x, PR=0x%08x\n", 233e0511b6cSIan Lepore sc->clkfreq / 1000, basefreq, READ4(sc, IMX_GPT_CR), READ4(sc, IMX_GPT_PR)); 234e0511b6cSIan Lepore 235e0511b6cSIan Lepore /* Setup the timer interrupt. */ 236a2c472e7SAleksandr Rybalko err = bus_setup_intr(dev, sc->res[1], INTR_TYPE_CLK, imx_gpt_intr, 237a2c472e7SAleksandr Rybalko NULL, sc, &sc->sc_ih); 238a2c472e7SAleksandr Rybalko if (err != 0) { 239a2c472e7SAleksandr Rybalko bus_release_resources(dev, imx_gpt_spec, sc->res); 240a2c472e7SAleksandr Rybalko device_printf(dev, "Unable to setup the clock irq handler, " 241a2c472e7SAleksandr Rybalko "err = %d\n", err); 242a2c472e7SAleksandr Rybalko return (ENXIO); 243a2c472e7SAleksandr Rybalko } 244a2c472e7SAleksandr Rybalko 245e0511b6cSIan Lepore /* Register as an eventtimer. */ 246f3549ad5SIan Lepore sc->et.et_name = "iMXGPT"; 247a2c472e7SAleksandr Rybalko sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERIODIC; 248f3549ad5SIan Lepore sc->et.et_quality = 800; 249a2c472e7SAleksandr Rybalko sc->et.et_frequency = sc->clkfreq; 250e0511b6cSIan Lepore sc->et.et_min_period = (MIN_ET_PERIOD << 32) / sc->et.et_frequency; 251a2c472e7SAleksandr Rybalko sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency; 252a2c472e7SAleksandr Rybalko sc->et.et_start = imx_gpt_timer_start; 253a2c472e7SAleksandr Rybalko sc->et.et_stop = imx_gpt_timer_stop; 254a2c472e7SAleksandr Rybalko sc->et.et_priv = sc; 255a2c472e7SAleksandr Rybalko et_register(&sc->et); 256a2c472e7SAleksandr Rybalko 257e0511b6cSIan Lepore /* Register as a timecounter. */ 258a2c472e7SAleksandr Rybalko imx_gpt_timecounter.tc_frequency = sc->clkfreq; 259a2c472e7SAleksandr Rybalko tc_init(&imx_gpt_timecounter); 260a2c472e7SAleksandr Rybalko 261e0511b6cSIan Lepore /* If this is the first unit, store the softc for use in DELAY. */ 262e0511b6cSIan Lepore if (device_get_unit(dev) == 0) 263e0511b6cSIan Lepore imx_gpt_sc = sc; 264a2c472e7SAleksandr Rybalko 265a2c472e7SAleksandr Rybalko return (0); 266a2c472e7SAleksandr Rybalko } 267a2c472e7SAleksandr Rybalko 268a2c472e7SAleksandr Rybalko static int 269a2c472e7SAleksandr Rybalko imx_gpt_timer_start(struct eventtimer *et, sbintime_t first, sbintime_t period) 270a2c472e7SAleksandr Rybalko { 271a2c472e7SAleksandr Rybalko struct imx_gpt_softc *sc; 272a2c472e7SAleksandr Rybalko uint32_t ticks; 273a2c472e7SAleksandr Rybalko 274a2c472e7SAleksandr Rybalko sc = (struct imx_gpt_softc *)et->et_priv; 275a2c472e7SAleksandr Rybalko 276a2c472e7SAleksandr Rybalko if (period != 0) { 277a2c472e7SAleksandr Rybalko sc->sc_period = ((uint32_t)et->et_frequency * period) >> 32; 278a2c472e7SAleksandr Rybalko /* Set expected value */ 279a2c472e7SAleksandr Rybalko WRITE4(sc, IMX_GPT_OCR2, READ4(sc, IMX_GPT_CNT) + sc->sc_period); 280a2c472e7SAleksandr Rybalko /* Enable compare register 2 Interrupt */ 281a2c472e7SAleksandr Rybalko SET4(sc, IMX_GPT_IR, GPT_IR_OF2); 28254fe6097SIan Lepore return (0); 283a2c472e7SAleksandr Rybalko } else if (first != 0) { 284a2c472e7SAleksandr Rybalko ticks = ((uint32_t)et->et_frequency * first) >> 32; 285a2c472e7SAleksandr Rybalko /* Do not disturb, otherwise event will be lost */ 286a2c472e7SAleksandr Rybalko spinlock_enter(); 287a2c472e7SAleksandr Rybalko /* Set expected value */ 288f3549ad5SIan Lepore WRITE4(sc, IMX_GPT_OCR3, READ4(sc, IMX_GPT_CNT) + ticks); 289a2c472e7SAleksandr Rybalko /* Enable compare register 1 Interrupt */ 290f3549ad5SIan Lepore SET4(sc, IMX_GPT_IR, GPT_IR_OF3); 291a2c472e7SAleksandr Rybalko /* Now everybody can relax */ 292a2c472e7SAleksandr Rybalko spinlock_exit(); 293a2c472e7SAleksandr Rybalko return (0); 294a2c472e7SAleksandr Rybalko } 295a2c472e7SAleksandr Rybalko 296a2c472e7SAleksandr Rybalko return (EINVAL); 297a2c472e7SAleksandr Rybalko } 298a2c472e7SAleksandr Rybalko 299a2c472e7SAleksandr Rybalko static int 300a2c472e7SAleksandr Rybalko imx_gpt_timer_stop(struct eventtimer *et) 301a2c472e7SAleksandr Rybalko { 302a2c472e7SAleksandr Rybalko struct imx_gpt_softc *sc; 303a2c472e7SAleksandr Rybalko 304a2c472e7SAleksandr Rybalko sc = (struct imx_gpt_softc *)et->et_priv; 305a2c472e7SAleksandr Rybalko 306a2c472e7SAleksandr Rybalko /* Disable OF2 Interrupt */ 307a2c472e7SAleksandr Rybalko CLEAR4(sc, IMX_GPT_IR, GPT_IR_OF2); 308a2c472e7SAleksandr Rybalko WRITE4(sc, IMX_GPT_SR, GPT_IR_OF2); 309a2c472e7SAleksandr Rybalko sc->sc_period = 0; 310a2c472e7SAleksandr Rybalko 311a2c472e7SAleksandr Rybalko return (0); 312a2c472e7SAleksandr Rybalko } 313a2c472e7SAleksandr Rybalko 314a2c472e7SAleksandr Rybalko int 315a2c472e7SAleksandr Rybalko imx_gpt_get_timerfreq(struct imx_gpt_softc *sc) 316a2c472e7SAleksandr Rybalko { 317a2c472e7SAleksandr Rybalko 318a2c472e7SAleksandr Rybalko return (sc->clkfreq); 319a2c472e7SAleksandr Rybalko } 320a2c472e7SAleksandr Rybalko 321a2c472e7SAleksandr Rybalko static int 322a2c472e7SAleksandr Rybalko imx_gpt_intr(void *arg) 323a2c472e7SAleksandr Rybalko { 324a2c472e7SAleksandr Rybalko struct imx_gpt_softc *sc; 325a2c472e7SAleksandr Rybalko uint32_t status; 326a2c472e7SAleksandr Rybalko 327a2c472e7SAleksandr Rybalko sc = (struct imx_gpt_softc *)arg; 328a2c472e7SAleksandr Rybalko 32954fe6097SIan Lepore status = READ4(sc, IMX_GPT_SR); 330a2c472e7SAleksandr Rybalko 33154fe6097SIan Lepore /* 33254fe6097SIan Lepore * Clear interrupt status before invoking event callbacks. The callback 33354fe6097SIan Lepore * often sets up a new one-shot timer event and if the interval is short 33454fe6097SIan Lepore * enough it can fire before we get out of this function. If we cleared 33554fe6097SIan Lepore * at the bottom we'd miss the interrupt and hang until the clock wraps. 33654fe6097SIan Lepore */ 33754fe6097SIan Lepore WRITE4(sc, IMX_GPT_SR, status); 33854fe6097SIan Lepore 33954fe6097SIan Lepore /* Handle one-shot timer events. */ 340f3549ad5SIan Lepore if (status & GPT_IR_OF3) { 341a2c472e7SAleksandr Rybalko if (sc->et.et_active) { 342a2c472e7SAleksandr Rybalko sc->et.et_event_cb(&sc->et, sc->et.et_arg); 343a2c472e7SAleksandr Rybalko } 344a2c472e7SAleksandr Rybalko } 34554fe6097SIan Lepore 34654fe6097SIan Lepore /* Handle periodic timer events. */ 347a2c472e7SAleksandr Rybalko if (status & GPT_IR_OF2) { 34854fe6097SIan Lepore if (sc->et.et_active) 349a2c472e7SAleksandr Rybalko sc->et.et_event_cb(&sc->et, sc->et.et_arg); 35054fe6097SIan Lepore if (sc->sc_period != 0) 351a2c472e7SAleksandr Rybalko WRITE4(sc, IMX_GPT_OCR2, READ4(sc, IMX_GPT_CNT) + 352a2c472e7SAleksandr Rybalko sc->sc_period); 353a2c472e7SAleksandr Rybalko } 354a2c472e7SAleksandr Rybalko 355a2c472e7SAleksandr Rybalko return (FILTER_HANDLED); 356a2c472e7SAleksandr Rybalko } 357a2c472e7SAleksandr Rybalko 358a2c472e7SAleksandr Rybalko u_int 359a2c472e7SAleksandr Rybalko imx_gpt_get_timecount(struct timecounter *tc) 360a2c472e7SAleksandr Rybalko { 361a2c472e7SAleksandr Rybalko 362a2c472e7SAleksandr Rybalko if (imx_gpt_sc == NULL) 363a2c472e7SAleksandr Rybalko return (0); 364a2c472e7SAleksandr Rybalko 365a2c472e7SAleksandr Rybalko return (READ4(imx_gpt_sc, IMX_GPT_CNT)); 366a2c472e7SAleksandr Rybalko } 367a2c472e7SAleksandr Rybalko 368a2c472e7SAleksandr Rybalko static device_method_t imx_gpt_methods[] = { 369a2c472e7SAleksandr Rybalko DEVMETHOD(device_probe, imx_gpt_probe), 370a2c472e7SAleksandr Rybalko DEVMETHOD(device_attach, imx_gpt_attach), 371a2c472e7SAleksandr Rybalko 372a2c472e7SAleksandr Rybalko DEVMETHOD_END 373a2c472e7SAleksandr Rybalko }; 374a2c472e7SAleksandr Rybalko 375a2c472e7SAleksandr Rybalko static driver_t imx_gpt_driver = { 376a2c472e7SAleksandr Rybalko "imx_gpt", 377a2c472e7SAleksandr Rybalko imx_gpt_methods, 378a2c472e7SAleksandr Rybalko sizeof(struct imx_gpt_softc), 379a2c472e7SAleksandr Rybalko }; 380a2c472e7SAleksandr Rybalko 381a2c472e7SAleksandr Rybalko static devclass_t imx_gpt_devclass; 382a2c472e7SAleksandr Rybalko 383a2c472e7SAleksandr Rybalko EARLY_DRIVER_MODULE(imx_gpt, simplebus, imx_gpt_driver, imx_gpt_devclass, 0, 384a2c472e7SAleksandr Rybalko 0, BUS_PASS_TIMER); 385a2c472e7SAleksandr Rybalko 386a2c472e7SAleksandr Rybalko void 387a2c472e7SAleksandr Rybalko DELAY(int usec) 388a2c472e7SAleksandr Rybalko { 389e0511b6cSIan Lepore uint64_t curcnt, endcnt, startcnt, ticks; 390a2c472e7SAleksandr Rybalko 391e0511b6cSIan Lepore /* If the timer hardware is not accessible, just use a loop. */ 392e0511b6cSIan Lepore if (imx_gpt_sc == NULL) { 393e0511b6cSIan Lepore while (usec-- > 0) 394e0511b6cSIan Lepore for (ticks = 0; ticks < imx_gpt_delay_count; ++ticks) 395a2c472e7SAleksandr Rybalko cpufunc_nullop(); 396a2c472e7SAleksandr Rybalko return; 397a2c472e7SAleksandr Rybalko } 398a2c472e7SAleksandr Rybalko 399e0511b6cSIan Lepore /* 400e0511b6cSIan Lepore * Calculate the tick count with 64-bit values so that it works for any 401e0511b6cSIan Lepore * clock frequency. Loop until the hardware count reaches start+ticks. 402e0511b6cSIan Lepore * If the 32-bit hardware count rolls over while we're looping, just 403e0511b6cSIan Lepore * manually do a carry into the high bits after each read; don't worry 404e0511b6cSIan Lepore * that doing this on each loop iteration is inefficient -- we're trying 405e0511b6cSIan Lepore * to waste time here. 406e0511b6cSIan Lepore */ 407e0511b6cSIan Lepore ticks = 1 + ((uint64_t)usec * imx_gpt_sc->clkfreq) / 1000000; 408e0511b6cSIan Lepore curcnt = startcnt = READ4(imx_gpt_sc, IMX_GPT_CNT); 409e0511b6cSIan Lepore endcnt = startcnt + ticks; 410e0511b6cSIan Lepore while (curcnt < endcnt) { 411e0511b6cSIan Lepore curcnt = READ4(imx_gpt_sc, IMX_GPT_CNT); 412e0511b6cSIan Lepore if (curcnt < startcnt) 413e0511b6cSIan Lepore curcnt += 1ULL << 32; 414a2c472e7SAleksandr Rybalko } 415a2c472e7SAleksandr Rybalko } 416