1a2c472e7SAleksandr Rybalko /*- 2af3dc4a7SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3af3dc4a7SPedro F. Giffuni * 494f8d6fdSAleksandr Rybalko * Copyright (c) 2012, 2013 The FreeBSD Foundation 5a2c472e7SAleksandr Rybalko * 6a2c472e7SAleksandr Rybalko * This software was developed by Oleksandr Rybalko under sponsorship 7a2c472e7SAleksandr Rybalko * from the FreeBSD Foundation. 8a2c472e7SAleksandr Rybalko * 9a2c472e7SAleksandr Rybalko * Redistribution and use in source and binary forms, with or without 10a2c472e7SAleksandr Rybalko * modification, are permitted provided that the following conditions 11a2c472e7SAleksandr Rybalko * are met: 12a2c472e7SAleksandr Rybalko * 1. Redistributions of source code must retain the above copyright 13a2c472e7SAleksandr Rybalko * notice, this list of conditions and the following disclaimer. 14a2c472e7SAleksandr Rybalko * 2. Redistributions in binary form must reproduce the above copyright 15a2c472e7SAleksandr Rybalko * notice, this list of conditions and the following disclaimer in the 16a2c472e7SAleksandr Rybalko * documentation and/or other materials provided with the distribution. 17a2c472e7SAleksandr Rybalko * 18a2c472e7SAleksandr Rybalko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19a2c472e7SAleksandr Rybalko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20a2c472e7SAleksandr Rybalko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21a2c472e7SAleksandr Rybalko * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22a2c472e7SAleksandr Rybalko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23a2c472e7SAleksandr Rybalko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24a2c472e7SAleksandr Rybalko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25a2c472e7SAleksandr Rybalko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26a2c472e7SAleksandr Rybalko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27a2c472e7SAleksandr Rybalko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28a2c472e7SAleksandr Rybalko * SUCH DAMAGE. 29a2c472e7SAleksandr Rybalko */ 30a2c472e7SAleksandr Rybalko 31a2c472e7SAleksandr Rybalko #include <sys/cdefs.h> 32a2c472e7SAleksandr Rybalko __FBSDID("$FreeBSD$"); 33a2c472e7SAleksandr Rybalko 34a2c472e7SAleksandr Rybalko #include <sys/param.h> 35a2c472e7SAleksandr Rybalko #include <sys/systm.h> 36a2c472e7SAleksandr Rybalko #include <sys/bus.h> 37a2c472e7SAleksandr Rybalko #include <sys/kernel.h> 38a2c472e7SAleksandr Rybalko #include <sys/module.h> 39a2c472e7SAleksandr Rybalko #include <sys/rman.h> 40a2c472e7SAleksandr Rybalko #include <sys/timeet.h> 41a2c472e7SAleksandr Rybalko #include <sys/timetc.h> 42a2c472e7SAleksandr Rybalko #include <machine/bus.h> 43a2c472e7SAleksandr Rybalko #include <machine/intr.h> 444bd9887cSAndrew Turner #include <machine/machdep.h> /* For arm_set_delay */ 45a2c472e7SAleksandr Rybalko 46a2c472e7SAleksandr Rybalko #include <dev/ofw/openfirm.h> 47a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus.h> 48a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus_subr.h> 49a2c472e7SAleksandr Rybalko 50a7fa939bSIan Lepore #include <arm/freescale/imx/imx_ccmvar.h> 513d9df276SIan Lepore #include <arm/freescale/imx/imx_gptreg.h> 52a2c472e7SAleksandr Rybalko 53a2c472e7SAleksandr Rybalko #define WRITE4(_sc, _r, _v) \ 54a2c472e7SAleksandr Rybalko bus_space_write_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r), (_v)) 55a2c472e7SAleksandr Rybalko #define READ4(_sc, _r) \ 56a2c472e7SAleksandr Rybalko bus_space_read_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r)) 57a2c472e7SAleksandr Rybalko #define SET4(_sc, _r, _m) \ 58a2c472e7SAleksandr Rybalko WRITE4((_sc), (_r), READ4((_sc), (_r)) | (_m)) 59a2c472e7SAleksandr Rybalko #define CLEAR4(_sc, _r, _m) \ 60a2c472e7SAleksandr Rybalko WRITE4((_sc), (_r), READ4((_sc), (_r)) & ~(_m)) 61a2c472e7SAleksandr Rybalko 62a2c472e7SAleksandr Rybalko static u_int imx_gpt_get_timecount(struct timecounter *); 63a2c472e7SAleksandr Rybalko static int imx_gpt_timer_start(struct eventtimer *, sbintime_t, 64a2c472e7SAleksandr Rybalko sbintime_t); 65a2c472e7SAleksandr Rybalko static int imx_gpt_timer_stop(struct eventtimer *); 66a2c472e7SAleksandr Rybalko 674bd9887cSAndrew Turner static void imx_gpt_do_delay(int, void *); 684bd9887cSAndrew Turner 69a2c472e7SAleksandr Rybalko static int imx_gpt_intr(void *); 70a2c472e7SAleksandr Rybalko static int imx_gpt_probe(device_t); 71a2c472e7SAleksandr Rybalko static int imx_gpt_attach(device_t); 72a2c472e7SAleksandr Rybalko 73a2c472e7SAleksandr Rybalko static struct timecounter imx_gpt_timecounter = { 74f3549ad5SIan Lepore .tc_name = "iMXGPT", 75a2c472e7SAleksandr Rybalko .tc_get_timecount = imx_gpt_get_timecount, 76a2c472e7SAleksandr Rybalko .tc_counter_mask = ~0u, 77a2c472e7SAleksandr Rybalko .tc_frequency = 0, 78e0511b6cSIan Lepore .tc_quality = 1000, 79a2c472e7SAleksandr Rybalko }; 80a2c472e7SAleksandr Rybalko 813d9df276SIan Lepore struct imx_gpt_softc { 823d9df276SIan Lepore device_t sc_dev; 833d9df276SIan Lepore struct resource * res[2]; 843d9df276SIan Lepore bus_space_tag_t sc_iot; 853d9df276SIan Lepore bus_space_handle_t sc_ioh; 863d9df276SIan Lepore void * sc_ih; /* interrupt handler */ 873d9df276SIan Lepore uint32_t sc_period; 883d9df276SIan Lepore uint32_t sc_clksrc; 893d9df276SIan Lepore uint32_t clkfreq; 905e52290fSIan Lepore uint32_t ir_reg; 913d9df276SIan Lepore struct eventtimer et; 923d9df276SIan Lepore }; 933d9df276SIan Lepore 94e0511b6cSIan Lepore /* Try to divide down an available fast clock to this frequency. */ 95c90fadc0SIan Lepore #define TARGET_FREQUENCY 1000000000 96e0511b6cSIan Lepore 97a2c472e7SAleksandr Rybalko static struct resource_spec imx_gpt_spec[] = { 98a2c472e7SAleksandr Rybalko { SYS_RES_MEMORY, 0, RF_ACTIVE }, 99a2c472e7SAleksandr Rybalko { SYS_RES_IRQ, 0, RF_ACTIVE }, 100a2c472e7SAleksandr Rybalko { -1, 0 } 101a2c472e7SAleksandr Rybalko }; 102a2c472e7SAleksandr Rybalko 103eb756ebdSIan Lepore static struct ofw_compat_data compat_data[] = { 104c85d45a5SOleksandr Tymoshenko {"fsl,imx6dl-gpt", 1}, 105eb756ebdSIan Lepore {"fsl,imx6q-gpt", 1}, 106e0427caaSIan Lepore {"fsl,imx6ul-gpt", 1}, 107eb756ebdSIan Lepore {"fsl,imx53-gpt", 1}, 108eb756ebdSIan Lepore {"fsl,imx51-gpt", 1}, 109eb756ebdSIan Lepore {"fsl,imx31-gpt", 1}, 110eb756ebdSIan Lepore {"fsl,imx27-gpt", 1}, 111eb756ebdSIan Lepore {"fsl,imx25-gpt", 1}, 112eb756ebdSIan Lepore {NULL, 0} 113eb756ebdSIan Lepore }; 114eb756ebdSIan Lepore 115a2c472e7SAleksandr Rybalko static int 116a2c472e7SAleksandr Rybalko imx_gpt_probe(device_t dev) 117a2c472e7SAleksandr Rybalko { 118a2c472e7SAleksandr Rybalko 119add35ed5SIan Lepore if (!ofw_bus_status_okay(dev)) 120add35ed5SIan Lepore return (ENXIO); 121add35ed5SIan Lepore 122e0427caaSIan Lepore /* 123e0427caaSIan Lepore * We only support a single unit, because the only thing this driver 124e0427caaSIan Lepore * does with the complex timer hardware is supply the system 125e0427caaSIan Lepore * timecounter and eventtimer. There is nothing useful we can do with 126e0427caaSIan Lepore * the additional device instances that exist in some chips. 127e0427caaSIan Lepore */ 128e0427caaSIan Lepore if (device_get_unit(dev) > 0) 129e0427caaSIan Lepore return (ENXIO); 130e0427caaSIan Lepore 131eb756ebdSIan Lepore if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 132e0511b6cSIan Lepore device_set_desc(dev, "Freescale i.MX GPT timer"); 133a2c472e7SAleksandr Rybalko return (BUS_PROBE_DEFAULT); 134a2c472e7SAleksandr Rybalko } 135a2c472e7SAleksandr Rybalko 136eb756ebdSIan Lepore return (ENXIO); 137eb756ebdSIan Lepore } 138eb756ebdSIan Lepore 139a2c472e7SAleksandr Rybalko static int 140a2c472e7SAleksandr Rybalko imx_gpt_attach(device_t dev) 141a2c472e7SAleksandr Rybalko { 142a2c472e7SAleksandr Rybalko struct imx_gpt_softc *sc; 143e0511b6cSIan Lepore int ctlreg, err; 144928e4f22SIan Lepore uint32_t basefreq, prescale, setup_ticks, t1, t2; 145a2c472e7SAleksandr Rybalko 146a2c472e7SAleksandr Rybalko sc = device_get_softc(dev); 147a2c472e7SAleksandr Rybalko 148a2c472e7SAleksandr Rybalko if (bus_alloc_resources(dev, imx_gpt_spec, sc->res)) { 149a2c472e7SAleksandr Rybalko device_printf(dev, "could not allocate resources\n"); 150a2c472e7SAleksandr Rybalko return (ENXIO); 151a2c472e7SAleksandr Rybalko } 152a2c472e7SAleksandr Rybalko 153a2c472e7SAleksandr Rybalko sc->sc_dev = dev; 154a2c472e7SAleksandr Rybalko sc->sc_iot = rman_get_bustag(sc->res[0]); 155a2c472e7SAleksandr Rybalko sc->sc_ioh = rman_get_bushandle(sc->res[0]); 156a2c472e7SAleksandr Rybalko 157e0511b6cSIan Lepore /* 158e0511b6cSIan Lepore * For now, just automatically choose a good clock for the hardware 159e0511b6cSIan Lepore * we're running on. Eventually we could allow selection from the fdt; 160e0511b6cSIan Lepore * the code in this driver will cope with any clock frequency. 161e0511b6cSIan Lepore */ 162e0511b6cSIan Lepore sc->sc_clksrc = GPT_CR_CLKSRC_IPG; 163e0511b6cSIan Lepore 164e0511b6cSIan Lepore ctlreg = 0; 165e0511b6cSIan Lepore 166a2c472e7SAleksandr Rybalko switch (sc->sc_clksrc) { 167a2c472e7SAleksandr Rybalko case GPT_CR_CLKSRC_32K: 168e0511b6cSIan Lepore basefreq = 32768; 169e0511b6cSIan Lepore break; 170e0511b6cSIan Lepore case GPT_CR_CLKSRC_IPG: 171a7fa939bSIan Lepore basefreq = imx_ccm_ipg_hz(); 172a2c472e7SAleksandr Rybalko break; 173a2c472e7SAleksandr Rybalko case GPT_CR_CLKSRC_IPG_HIGH: 174a7fa939bSIan Lepore basefreq = imx_ccm_ipg_hz() * 2; 175a2c472e7SAleksandr Rybalko break; 176e0511b6cSIan Lepore case GPT_CR_CLKSRC_24M: 177e0511b6cSIan Lepore ctlreg |= GPT_CR_24MEN; 178e0511b6cSIan Lepore basefreq = 24000000; 179e0511b6cSIan Lepore break; 180e0511b6cSIan Lepore case GPT_CR_CLKSRC_NONE:/* Can't run without a clock. */ 181e0511b6cSIan Lepore case GPT_CR_CLKSRC_EXT: /* No way to get the freq of an ext clock. */ 182a2c472e7SAleksandr Rybalko default: 183e0511b6cSIan Lepore device_printf(dev, "Unsupported clock source '%d'\n", 184e0511b6cSIan Lepore sc->sc_clksrc); 185e0511b6cSIan Lepore return (EINVAL); 186a2c472e7SAleksandr Rybalko } 187a2c472e7SAleksandr Rybalko 188e0511b6cSIan Lepore /* 189e0511b6cSIan Lepore * The following setup sequence is from the I.MX6 reference manual, 190e0511b6cSIan Lepore * "Selecting the clock source". First, disable the clock and 191e0511b6cSIan Lepore * interrupts. This also clears input and output mode bits and in 192e0511b6cSIan Lepore * general completes several of the early steps in the procedure. 193e0511b6cSIan Lepore */ 194e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_CR, 0); 195a2c472e7SAleksandr Rybalko WRITE4(sc, IMX_GPT_IR, 0); 196a2c472e7SAleksandr Rybalko 197e0511b6cSIan Lepore /* Choose the clock and the power-saving behaviors. */ 198e0511b6cSIan Lepore ctlreg |= 199e0511b6cSIan Lepore sc->sc_clksrc | /* Use selected clock */ 200e0511b6cSIan Lepore GPT_CR_FRR | /* Just count (FreeRunner mode) */ 201e0511b6cSIan Lepore GPT_CR_STOPEN | /* Run in STOP mode */ 202e0511b6cSIan Lepore GPT_CR_DOZEEN | /* Run in DOZE mode */ 203e0511b6cSIan Lepore GPT_CR_WAITEN | /* Run in WAIT mode */ 204e0511b6cSIan Lepore GPT_CR_DBGEN; /* Run in DEBUG mode */ 205e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_CR, ctlreg); 206a2c472e7SAleksandr Rybalko 207e0511b6cSIan Lepore /* 208e0511b6cSIan Lepore * The datasheet says to do the software reset after choosing the clock 209e0511b6cSIan Lepore * source. It says nothing about needing to wait for the reset to 210e0511b6cSIan Lepore * complete, but the register description does document the fact that 211e0511b6cSIan Lepore * the reset isn't complete until the SWR bit reads 0, so let's be safe. 212e0511b6cSIan Lepore * The reset also clears all registers except for a few of the bits in 213e0511b6cSIan Lepore * CR, but we'll rewrite all the CR bits when we start the counter. 214e0511b6cSIan Lepore */ 215e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_CR, ctlreg | GPT_CR_SWR); 216e0511b6cSIan Lepore while (READ4(sc, IMX_GPT_CR) & GPT_CR_SWR) 217e0511b6cSIan Lepore continue; 218e0511b6cSIan Lepore 219e0511b6cSIan Lepore /* Set a prescaler value that gets us near the target frequency. */ 220e0511b6cSIan Lepore if (basefreq < TARGET_FREQUENCY) { 221e0511b6cSIan Lepore prescale = 0; 222e0511b6cSIan Lepore sc->clkfreq = basefreq; 223e0511b6cSIan Lepore } else { 224e0511b6cSIan Lepore prescale = basefreq / TARGET_FREQUENCY; 225e0511b6cSIan Lepore sc->clkfreq = basefreq / prescale; 226e0511b6cSIan Lepore prescale -= 1; /* 1..n range is 0..n-1 in hardware. */ 227e0511b6cSIan Lepore } 228e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_PR, prescale); 229e0511b6cSIan Lepore 230e0511b6cSIan Lepore /* Clear the status register. */ 231e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_SR, GPT_IR_ALL); 232e0511b6cSIan Lepore 233e0511b6cSIan Lepore /* Start the counter. */ 234e0511b6cSIan Lepore WRITE4(sc, IMX_GPT_CR, ctlreg | GPT_CR_EN); 235e0511b6cSIan Lepore 236e0511b6cSIan Lepore if (bootverbose) 237e0511b6cSIan Lepore device_printf(dev, "Running on %dKHz clock, base freq %uHz CR=0x%08x, PR=0x%08x\n", 238e0511b6cSIan Lepore sc->clkfreq / 1000, basefreq, READ4(sc, IMX_GPT_CR), READ4(sc, IMX_GPT_PR)); 239e0511b6cSIan Lepore 240e0511b6cSIan Lepore /* Setup the timer interrupt. */ 241a2c472e7SAleksandr Rybalko err = bus_setup_intr(dev, sc->res[1], INTR_TYPE_CLK, imx_gpt_intr, 242a2c472e7SAleksandr Rybalko NULL, sc, &sc->sc_ih); 243a2c472e7SAleksandr Rybalko if (err != 0) { 244a2c472e7SAleksandr Rybalko bus_release_resources(dev, imx_gpt_spec, sc->res); 245a2c472e7SAleksandr Rybalko device_printf(dev, "Unable to setup the clock irq handler, " 246a2c472e7SAleksandr Rybalko "err = %d\n", err); 247a2c472e7SAleksandr Rybalko return (ENXIO); 248a2c472e7SAleksandr Rybalko } 249a2c472e7SAleksandr Rybalko 250928e4f22SIan Lepore /* 251928e4f22SIan Lepore * Measure how many clock ticks it takes to setup a one-shot event (it's 252928e4f22SIan Lepore * longer than you might think, due to wait states in accessing gpt 253928e4f22SIan Lepore * registers). Scale up the result by a factor of 1.5 to be safe, 254928e4f22SIan Lepore * and use that to set the minimum eventtimer period we can schedule. In 255928e4f22SIan Lepore * the real world, the value works out to about 750ns on imx5 hardware. 256928e4f22SIan Lepore */ 257928e4f22SIan Lepore t1 = READ4(sc, IMX_GPT_CNT); 258928e4f22SIan Lepore WRITE4(sc, IMX_GPT_OCR3, 0); 259928e4f22SIan Lepore t2 = READ4(sc, IMX_GPT_CNT); 260928e4f22SIan Lepore setup_ticks = ((t2 - t1 + 1) * 3) / 2; 261928e4f22SIan Lepore 262e0511b6cSIan Lepore /* Register as an eventtimer. */ 263f3549ad5SIan Lepore sc->et.et_name = "iMXGPT"; 264a2c472e7SAleksandr Rybalko sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERIODIC; 265f3549ad5SIan Lepore sc->et.et_quality = 800; 266a2c472e7SAleksandr Rybalko sc->et.et_frequency = sc->clkfreq; 267928e4f22SIan Lepore sc->et.et_min_period = ((uint64_t)setup_ticks << 32) / sc->clkfreq; 268928e4f22SIan Lepore sc->et.et_max_period = ((uint64_t)0xfffffffe << 32) / sc->clkfreq; 269a2c472e7SAleksandr Rybalko sc->et.et_start = imx_gpt_timer_start; 270a2c472e7SAleksandr Rybalko sc->et.et_stop = imx_gpt_timer_stop; 271a2c472e7SAleksandr Rybalko sc->et.et_priv = sc; 272a2c472e7SAleksandr Rybalko et_register(&sc->et); 273a2c472e7SAleksandr Rybalko 274e0511b6cSIan Lepore /* Register as a timecounter. */ 275a2c472e7SAleksandr Rybalko imx_gpt_timecounter.tc_frequency = sc->clkfreq; 276b4fca5d4SAndrew Turner imx_gpt_timecounter.tc_priv = sc; 277a2c472e7SAleksandr Rybalko tc_init(&imx_gpt_timecounter); 278a2c472e7SAleksandr Rybalko 279e0511b6cSIan Lepore /* If this is the first unit, store the softc for use in DELAY. */ 2804bd9887cSAndrew Turner if (device_get_unit(dev) == 0) { 2814bd9887cSAndrew Turner arm_set_delay(imx_gpt_do_delay, sc); 2824bd9887cSAndrew Turner } 283a2c472e7SAleksandr Rybalko 284a2c472e7SAleksandr Rybalko return (0); 285a2c472e7SAleksandr Rybalko } 286a2c472e7SAleksandr Rybalko 287a2c472e7SAleksandr Rybalko static int 288a2c472e7SAleksandr Rybalko imx_gpt_timer_start(struct eventtimer *et, sbintime_t first, sbintime_t period) 289a2c472e7SAleksandr Rybalko { 290a2c472e7SAleksandr Rybalko struct imx_gpt_softc *sc; 291a2c472e7SAleksandr Rybalko uint32_t ticks; 292a2c472e7SAleksandr Rybalko 293a2c472e7SAleksandr Rybalko sc = (struct imx_gpt_softc *)et->et_priv; 294a2c472e7SAleksandr Rybalko 295a2c472e7SAleksandr Rybalko if (period != 0) { 296a2c472e7SAleksandr Rybalko sc->sc_period = ((uint32_t)et->et_frequency * period) >> 32; 297a2c472e7SAleksandr Rybalko /* Set expected value */ 298a2c472e7SAleksandr Rybalko WRITE4(sc, IMX_GPT_OCR2, READ4(sc, IMX_GPT_CNT) + sc->sc_period); 299a2c472e7SAleksandr Rybalko /* Enable compare register 2 Interrupt */ 3005e52290fSIan Lepore sc->ir_reg |= GPT_IR_OF2; 3015e52290fSIan Lepore WRITE4(sc, IMX_GPT_IR, sc->ir_reg); 30254fe6097SIan Lepore return (0); 303a2c472e7SAleksandr Rybalko } else if (first != 0) { 3045e52290fSIan Lepore /* Enable compare register 3 interrupt if not already on. */ 3055e52290fSIan Lepore if ((sc->ir_reg & GPT_IR_OF3) == 0) { 3065e52290fSIan Lepore sc->ir_reg |= GPT_IR_OF3; 3075e52290fSIan Lepore WRITE4(sc, IMX_GPT_IR, sc->ir_reg); 3085e52290fSIan Lepore } 309a2c472e7SAleksandr Rybalko ticks = ((uint32_t)et->et_frequency * first) >> 32; 310a2c472e7SAleksandr Rybalko /* Do not disturb, otherwise event will be lost */ 311a2c472e7SAleksandr Rybalko spinlock_enter(); 312a2c472e7SAleksandr Rybalko /* Set expected value */ 313f3549ad5SIan Lepore WRITE4(sc, IMX_GPT_OCR3, READ4(sc, IMX_GPT_CNT) + ticks); 314a2c472e7SAleksandr Rybalko /* Now everybody can relax */ 315a2c472e7SAleksandr Rybalko spinlock_exit(); 316a2c472e7SAleksandr Rybalko return (0); 317a2c472e7SAleksandr Rybalko } 318a2c472e7SAleksandr Rybalko 319a2c472e7SAleksandr Rybalko return (EINVAL); 320a2c472e7SAleksandr Rybalko } 321a2c472e7SAleksandr Rybalko 322a2c472e7SAleksandr Rybalko static int 323a2c472e7SAleksandr Rybalko imx_gpt_timer_stop(struct eventtimer *et) 324a2c472e7SAleksandr Rybalko { 325a2c472e7SAleksandr Rybalko struct imx_gpt_softc *sc; 326a2c472e7SAleksandr Rybalko 327a2c472e7SAleksandr Rybalko sc = (struct imx_gpt_softc *)et->et_priv; 328a2c472e7SAleksandr Rybalko 3295e52290fSIan Lepore /* Disable interrupts and clear any pending status. */ 3305e52290fSIan Lepore sc->ir_reg &= ~(GPT_IR_OF2 | GPT_IR_OF3); 3315e52290fSIan Lepore WRITE4(sc, IMX_GPT_IR, sc->ir_reg); 3325e52290fSIan Lepore WRITE4(sc, IMX_GPT_SR, GPT_IR_OF2 | GPT_IR_OF3); 333a2c472e7SAleksandr Rybalko sc->sc_period = 0; 334a2c472e7SAleksandr Rybalko 335a2c472e7SAleksandr Rybalko return (0); 336a2c472e7SAleksandr Rybalko } 337a2c472e7SAleksandr Rybalko 338a2c472e7SAleksandr Rybalko static int 339a2c472e7SAleksandr Rybalko imx_gpt_intr(void *arg) 340a2c472e7SAleksandr Rybalko { 341a2c472e7SAleksandr Rybalko struct imx_gpt_softc *sc; 342a2c472e7SAleksandr Rybalko uint32_t status; 343a2c472e7SAleksandr Rybalko 344a2c472e7SAleksandr Rybalko sc = (struct imx_gpt_softc *)arg; 345a2c472e7SAleksandr Rybalko 34654fe6097SIan Lepore status = READ4(sc, IMX_GPT_SR); 347a2c472e7SAleksandr Rybalko 34854fe6097SIan Lepore /* 34954fe6097SIan Lepore * Clear interrupt status before invoking event callbacks. The callback 35054fe6097SIan Lepore * often sets up a new one-shot timer event and if the interval is short 35154fe6097SIan Lepore * enough it can fire before we get out of this function. If we cleared 35254fe6097SIan Lepore * at the bottom we'd miss the interrupt and hang until the clock wraps. 35354fe6097SIan Lepore */ 35454fe6097SIan Lepore WRITE4(sc, IMX_GPT_SR, status); 35554fe6097SIan Lepore 35654fe6097SIan Lepore /* Handle one-shot timer events. */ 357f3549ad5SIan Lepore if (status & GPT_IR_OF3) { 358a2c472e7SAleksandr Rybalko if (sc->et.et_active) { 359a2c472e7SAleksandr Rybalko sc->et.et_event_cb(&sc->et, sc->et.et_arg); 360a2c472e7SAleksandr Rybalko } 361a2c472e7SAleksandr Rybalko } 36254fe6097SIan Lepore 36354fe6097SIan Lepore /* Handle periodic timer events. */ 364a2c472e7SAleksandr Rybalko if (status & GPT_IR_OF2) { 36554fe6097SIan Lepore if (sc->et.et_active) 366a2c472e7SAleksandr Rybalko sc->et.et_event_cb(&sc->et, sc->et.et_arg); 36754fe6097SIan Lepore if (sc->sc_period != 0) 368a2c472e7SAleksandr Rybalko WRITE4(sc, IMX_GPT_OCR2, READ4(sc, IMX_GPT_CNT) + 369a2c472e7SAleksandr Rybalko sc->sc_period); 370a2c472e7SAleksandr Rybalko } 371a2c472e7SAleksandr Rybalko 372a2c472e7SAleksandr Rybalko return (FILTER_HANDLED); 373a2c472e7SAleksandr Rybalko } 374a2c472e7SAleksandr Rybalko 375b4fca5d4SAndrew Turner static u_int 376a2c472e7SAleksandr Rybalko imx_gpt_get_timecount(struct timecounter *tc) 377a2c472e7SAleksandr Rybalko { 378b4fca5d4SAndrew Turner struct imx_gpt_softc *sc; 379a2c472e7SAleksandr Rybalko 380b4fca5d4SAndrew Turner sc = tc->tc_priv; 381b4fca5d4SAndrew Turner return (READ4(sc, IMX_GPT_CNT)); 382a2c472e7SAleksandr Rybalko } 383a2c472e7SAleksandr Rybalko 384a2c472e7SAleksandr Rybalko static device_method_t imx_gpt_methods[] = { 385a2c472e7SAleksandr Rybalko DEVMETHOD(device_probe, imx_gpt_probe), 386a2c472e7SAleksandr Rybalko DEVMETHOD(device_attach, imx_gpt_attach), 387a2c472e7SAleksandr Rybalko 388a2c472e7SAleksandr Rybalko DEVMETHOD_END 389a2c472e7SAleksandr Rybalko }; 390a2c472e7SAleksandr Rybalko 391a2c472e7SAleksandr Rybalko static driver_t imx_gpt_driver = { 392a2c472e7SAleksandr Rybalko "imx_gpt", 393a2c472e7SAleksandr Rybalko imx_gpt_methods, 394a2c472e7SAleksandr Rybalko sizeof(struct imx_gpt_softc), 395a2c472e7SAleksandr Rybalko }; 396a2c472e7SAleksandr Rybalko 397*ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(imx_gpt, simplebus, imx_gpt_driver, 0, 0, BUS_PASS_TIMER); 398a2c472e7SAleksandr Rybalko 3994bd9887cSAndrew Turner static void 4004bd9887cSAndrew Turner imx_gpt_do_delay(int usec, void *arg) 401a2c472e7SAleksandr Rybalko { 4024bd9887cSAndrew Turner struct imx_gpt_softc *sc = arg; 403e0511b6cSIan Lepore uint64_t curcnt, endcnt, startcnt, ticks; 404a2c472e7SAleksandr Rybalko 405e0511b6cSIan Lepore /* 406e0511b6cSIan Lepore * Calculate the tick count with 64-bit values so that it works for any 407e0511b6cSIan Lepore * clock frequency. Loop until the hardware count reaches start+ticks. 408e0511b6cSIan Lepore * If the 32-bit hardware count rolls over while we're looping, just 409e0511b6cSIan Lepore * manually do a carry into the high bits after each read; don't worry 410e0511b6cSIan Lepore * that doing this on each loop iteration is inefficient -- we're trying 411e0511b6cSIan Lepore * to waste time here. 412e0511b6cSIan Lepore */ 4134bd9887cSAndrew Turner ticks = 1 + ((uint64_t)usec * sc->clkfreq) / 1000000; 4144bd9887cSAndrew Turner curcnt = startcnt = READ4(sc, IMX_GPT_CNT); 415e0511b6cSIan Lepore endcnt = startcnt + ticks; 416e0511b6cSIan Lepore while (curcnt < endcnt) { 4174bd9887cSAndrew Turner curcnt = READ4(sc, IMX_GPT_CNT); 418e0511b6cSIan Lepore if (curcnt < startcnt) 419e0511b6cSIan Lepore curcnt += 1ULL << 32; 420a2c472e7SAleksandr Rybalko } 421a2c472e7SAleksandr Rybalko } 422