xref: /freebsd/sys/arm/freescale/imx/imx_gpt.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1a2c472e7SAleksandr Rybalko /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
32a2c472e7SAleksandr Rybalko #include <sys/systm.h>
33a2c472e7SAleksandr Rybalko #include <sys/bus.h>
34a2c472e7SAleksandr Rybalko #include <sys/kernel.h>
35a2c472e7SAleksandr Rybalko #include <sys/module.h>
36a2c472e7SAleksandr Rybalko #include <sys/rman.h>
37a2c472e7SAleksandr Rybalko #include <sys/timeet.h>
38a2c472e7SAleksandr Rybalko #include <sys/timetc.h>
39a2c472e7SAleksandr Rybalko #include <machine/bus.h>
40a2c472e7SAleksandr Rybalko #include <machine/intr.h>
414bd9887cSAndrew Turner #include <machine/machdep.h> /* For arm_set_delay */
42a2c472e7SAleksandr Rybalko 
43a2c472e7SAleksandr Rybalko #include <dev/ofw/openfirm.h>
44a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus.h>
45a2c472e7SAleksandr Rybalko #include <dev/ofw/ofw_bus_subr.h>
46a2c472e7SAleksandr Rybalko 
47a7fa939bSIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
483d9df276SIan Lepore #include <arm/freescale/imx/imx_gptreg.h>
49a2c472e7SAleksandr Rybalko 
50a2c472e7SAleksandr Rybalko #define	WRITE4(_sc, _r, _v)						\
51a2c472e7SAleksandr Rybalko 	    bus_space_write_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r), (_v))
52a2c472e7SAleksandr Rybalko #define	READ4(_sc, _r)							\
53a2c472e7SAleksandr Rybalko 	    bus_space_read_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r))
54a2c472e7SAleksandr Rybalko #define	SET4(_sc, _r, _m)						\
55a2c472e7SAleksandr Rybalko 	    WRITE4((_sc), (_r), READ4((_sc), (_r)) | (_m))
56a2c472e7SAleksandr Rybalko #define	CLEAR4(_sc, _r, _m)						\
57a2c472e7SAleksandr Rybalko 	    WRITE4((_sc), (_r), READ4((_sc), (_r)) & ~(_m))
58a2c472e7SAleksandr Rybalko 
59a2c472e7SAleksandr Rybalko static u_int	imx_gpt_get_timecount(struct timecounter *);
60a2c472e7SAleksandr Rybalko static int	imx_gpt_timer_start(struct eventtimer *, sbintime_t,
61a2c472e7SAleksandr Rybalko     sbintime_t);
62a2c472e7SAleksandr Rybalko static int	imx_gpt_timer_stop(struct eventtimer *);
63a2c472e7SAleksandr Rybalko 
644bd9887cSAndrew Turner static void imx_gpt_do_delay(int, void *);
654bd9887cSAndrew Turner 
66a2c472e7SAleksandr Rybalko static int imx_gpt_intr(void *);
67a2c472e7SAleksandr Rybalko static int imx_gpt_probe(device_t);
68a2c472e7SAleksandr Rybalko static int imx_gpt_attach(device_t);
69a2c472e7SAleksandr Rybalko 
70a2c472e7SAleksandr Rybalko static struct timecounter imx_gpt_timecounter = {
71f3549ad5SIan Lepore 	.tc_name           = "iMXGPT",
72a2c472e7SAleksandr Rybalko 	.tc_get_timecount  = imx_gpt_get_timecount,
73a2c472e7SAleksandr Rybalko 	.tc_counter_mask   = ~0u,
74a2c472e7SAleksandr Rybalko 	.tc_frequency      = 0,
75e0511b6cSIan Lepore 	.tc_quality        = 1000,
76a2c472e7SAleksandr Rybalko };
77a2c472e7SAleksandr Rybalko 
783d9df276SIan Lepore struct imx_gpt_softc {
793d9df276SIan Lepore 	device_t 		sc_dev;
803d9df276SIan Lepore 	struct resource *	res[2];
813d9df276SIan Lepore 	bus_space_tag_t 	sc_iot;
823d9df276SIan Lepore 	bus_space_handle_t	sc_ioh;
833d9df276SIan Lepore 	void *			sc_ih;			/* interrupt handler */
843d9df276SIan Lepore 	uint32_t 		sc_period;
853d9df276SIan Lepore 	uint32_t 		sc_clksrc;
863d9df276SIan Lepore 	uint32_t 		clkfreq;
875e52290fSIan Lepore 	uint32_t		ir_reg;
883d9df276SIan Lepore 	struct eventtimer 	et;
893d9df276SIan Lepore };
903d9df276SIan Lepore 
91e0511b6cSIan Lepore /* Try to divide down an available fast clock to this frequency. */
92c90fadc0SIan Lepore #define	TARGET_FREQUENCY	1000000000
93e0511b6cSIan Lepore 
94a2c472e7SAleksandr Rybalko static struct resource_spec imx_gpt_spec[] = {
95a2c472e7SAleksandr Rybalko 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
96a2c472e7SAleksandr Rybalko 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
97a2c472e7SAleksandr Rybalko 	{ -1, 0 }
98a2c472e7SAleksandr Rybalko };
99a2c472e7SAleksandr Rybalko 
100eb756ebdSIan Lepore static struct ofw_compat_data compat_data[] = {
101c85d45a5SOleksandr Tymoshenko 	{"fsl,imx6dl-gpt", 1},
102eb756ebdSIan Lepore 	{"fsl,imx6q-gpt",  1},
103e0427caaSIan Lepore 	{"fsl,imx6ul-gpt", 1},
104eb756ebdSIan Lepore 	{"fsl,imx53-gpt",  1},
105eb756ebdSIan Lepore 	{"fsl,imx51-gpt",  1},
106eb756ebdSIan Lepore 	{"fsl,imx31-gpt",  1},
107eb756ebdSIan Lepore 	{"fsl,imx27-gpt",  1},
108eb756ebdSIan Lepore 	{"fsl,imx25-gpt",  1},
109eb756ebdSIan Lepore 	{NULL,             0}
110eb756ebdSIan Lepore };
111eb756ebdSIan Lepore 
112a2c472e7SAleksandr Rybalko static int
imx_gpt_probe(device_t dev)113a2c472e7SAleksandr Rybalko imx_gpt_probe(device_t dev)
114a2c472e7SAleksandr Rybalko {
115a2c472e7SAleksandr Rybalko 
116add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
117add35ed5SIan Lepore 		return (ENXIO);
118add35ed5SIan Lepore 
119e0427caaSIan Lepore 	/*
120e0427caaSIan Lepore 	 *  We only support a single unit, because the only thing this driver
121e0427caaSIan Lepore 	 *  does with the complex timer hardware is supply the system
122e0427caaSIan Lepore 	 *  timecounter and eventtimer.  There is nothing useful we can do with
123e0427caaSIan Lepore 	 *  the additional device instances that exist in some chips.
124e0427caaSIan Lepore 	 */
125e0427caaSIan Lepore 	if (device_get_unit(dev) > 0)
126e0427caaSIan Lepore 		return (ENXIO);
127e0427caaSIan Lepore 
128eb756ebdSIan Lepore 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
129e0511b6cSIan Lepore 		device_set_desc(dev, "Freescale i.MX GPT timer");
130a2c472e7SAleksandr Rybalko 		return (BUS_PROBE_DEFAULT);
131a2c472e7SAleksandr Rybalko 	}
132a2c472e7SAleksandr Rybalko 
133eb756ebdSIan Lepore 	return (ENXIO);
134eb756ebdSIan Lepore }
135eb756ebdSIan Lepore 
136a2c472e7SAleksandr Rybalko static int
imx_gpt_attach(device_t dev)137a2c472e7SAleksandr Rybalko imx_gpt_attach(device_t dev)
138a2c472e7SAleksandr Rybalko {
139a2c472e7SAleksandr Rybalko 	struct imx_gpt_softc *sc;
140e0511b6cSIan Lepore 	int ctlreg, err;
141928e4f22SIan Lepore 	uint32_t basefreq, prescale, setup_ticks, t1, t2;
142a2c472e7SAleksandr Rybalko 
143a2c472e7SAleksandr Rybalko 	sc = device_get_softc(dev);
144a2c472e7SAleksandr Rybalko 
145a2c472e7SAleksandr Rybalko 	if (bus_alloc_resources(dev, imx_gpt_spec, sc->res)) {
146a2c472e7SAleksandr Rybalko 		device_printf(dev, "could not allocate resources\n");
147a2c472e7SAleksandr Rybalko 		return (ENXIO);
148a2c472e7SAleksandr Rybalko 	}
149a2c472e7SAleksandr Rybalko 
150a2c472e7SAleksandr Rybalko 	sc->sc_dev = dev;
151a2c472e7SAleksandr Rybalko 	sc->sc_iot = rman_get_bustag(sc->res[0]);
152a2c472e7SAleksandr Rybalko 	sc->sc_ioh = rman_get_bushandle(sc->res[0]);
153a2c472e7SAleksandr Rybalko 
154e0511b6cSIan Lepore 	/*
155e0511b6cSIan Lepore 	 * For now, just automatically choose a good clock for the hardware
156e0511b6cSIan Lepore 	 * we're running on.  Eventually we could allow selection from the fdt;
157e0511b6cSIan Lepore 	 * the code in this driver will cope with any clock frequency.
158e0511b6cSIan Lepore 	 */
159e0511b6cSIan Lepore 	sc->sc_clksrc = GPT_CR_CLKSRC_IPG;
160e0511b6cSIan Lepore 
161e0511b6cSIan Lepore 	ctlreg = 0;
162e0511b6cSIan Lepore 
163a2c472e7SAleksandr Rybalko 	switch (sc->sc_clksrc) {
164a2c472e7SAleksandr Rybalko 	case GPT_CR_CLKSRC_32K:
165e0511b6cSIan Lepore 		basefreq = 32768;
166e0511b6cSIan Lepore 		break;
167e0511b6cSIan Lepore 	case GPT_CR_CLKSRC_IPG:
168a7fa939bSIan Lepore 		basefreq = imx_ccm_ipg_hz();
169a2c472e7SAleksandr Rybalko 		break;
170a2c472e7SAleksandr Rybalko 	case GPT_CR_CLKSRC_IPG_HIGH:
171a7fa939bSIan Lepore 		basefreq = imx_ccm_ipg_hz() * 2;
172a2c472e7SAleksandr Rybalko 		break;
173e0511b6cSIan Lepore 	case GPT_CR_CLKSRC_24M:
174e0511b6cSIan Lepore 		ctlreg |= GPT_CR_24MEN;
175e0511b6cSIan Lepore 		basefreq = 24000000;
176e0511b6cSIan Lepore 		break;
177e0511b6cSIan Lepore 	case GPT_CR_CLKSRC_NONE:/* Can't run without a clock. */
178e0511b6cSIan Lepore 	case GPT_CR_CLKSRC_EXT:	/* No way to get the freq of an ext clock. */
179a2c472e7SAleksandr Rybalko 	default:
180e0511b6cSIan Lepore 		device_printf(dev, "Unsupported clock source '%d'\n",
181e0511b6cSIan Lepore 		    sc->sc_clksrc);
182e0511b6cSIan Lepore 		return (EINVAL);
183a2c472e7SAleksandr Rybalko 	}
184a2c472e7SAleksandr Rybalko 
185e0511b6cSIan Lepore 	/*
186e0511b6cSIan Lepore 	 * The following setup sequence is from the I.MX6 reference manual,
187e0511b6cSIan Lepore 	 * "Selecting the clock source".  First, disable the clock and
188e0511b6cSIan Lepore 	 * interrupts.  This also clears input and output mode bits and in
189e0511b6cSIan Lepore 	 * general completes several of the early steps in the procedure.
190e0511b6cSIan Lepore 	 */
191e0511b6cSIan Lepore 	WRITE4(sc, IMX_GPT_CR, 0);
192a2c472e7SAleksandr Rybalko 	WRITE4(sc, IMX_GPT_IR, 0);
193a2c472e7SAleksandr Rybalko 
194e0511b6cSIan Lepore 	/* Choose the clock and the power-saving behaviors. */
195e0511b6cSIan Lepore 	ctlreg |=
196e0511b6cSIan Lepore 	    sc->sc_clksrc |	/* Use selected clock */
197e0511b6cSIan Lepore 	    GPT_CR_FRR |	/* Just count (FreeRunner mode) */
198e0511b6cSIan Lepore 	    GPT_CR_STOPEN |	/* Run in STOP mode */
199e0511b6cSIan Lepore 	    GPT_CR_DOZEEN |	/* Run in DOZE mode */
200e0511b6cSIan Lepore 	    GPT_CR_WAITEN |	/* Run in WAIT mode */
201e0511b6cSIan Lepore 	    GPT_CR_DBGEN;	/* Run in DEBUG mode */
202e0511b6cSIan Lepore 	WRITE4(sc, IMX_GPT_CR, ctlreg);
203a2c472e7SAleksandr Rybalko 
204e0511b6cSIan Lepore 	/*
205e0511b6cSIan Lepore 	 * The datasheet says to do the software reset after choosing the clock
206e0511b6cSIan Lepore 	 * source.  It says nothing about needing to wait for the reset to
207e0511b6cSIan Lepore 	 * complete, but the register description does document the fact that
208e0511b6cSIan Lepore 	 * the reset isn't complete until the SWR bit reads 0, so let's be safe.
209e0511b6cSIan Lepore 	 * The reset also clears all registers except for a few of the bits in
210e0511b6cSIan Lepore 	 * CR, but we'll rewrite all the CR bits when we start the counter.
211e0511b6cSIan Lepore 	 */
212e0511b6cSIan Lepore 	WRITE4(sc, IMX_GPT_CR, ctlreg | GPT_CR_SWR);
213e0511b6cSIan Lepore 	while (READ4(sc, IMX_GPT_CR) & GPT_CR_SWR)
214e0511b6cSIan Lepore 		continue;
215e0511b6cSIan Lepore 
216e0511b6cSIan Lepore 	/* Set a prescaler value that gets us near the target frequency. */
217e0511b6cSIan Lepore 	if (basefreq < TARGET_FREQUENCY) {
218e0511b6cSIan Lepore 		prescale = 0;
219e0511b6cSIan Lepore 		sc->clkfreq = basefreq;
220e0511b6cSIan Lepore 	} else {
221e0511b6cSIan Lepore 		prescale = basefreq / TARGET_FREQUENCY;
222e0511b6cSIan Lepore 		sc->clkfreq = basefreq / prescale;
223e0511b6cSIan Lepore 		prescale -= 1; /* 1..n range is 0..n-1 in hardware. */
224e0511b6cSIan Lepore 	}
225e0511b6cSIan Lepore 	WRITE4(sc, IMX_GPT_PR, prescale);
226e0511b6cSIan Lepore 
227e0511b6cSIan Lepore 	/* Clear the status register. */
228e0511b6cSIan Lepore 	WRITE4(sc, IMX_GPT_SR, GPT_IR_ALL);
229e0511b6cSIan Lepore 
230e0511b6cSIan Lepore 	/* Start the counter. */
231e0511b6cSIan Lepore 	WRITE4(sc, IMX_GPT_CR, ctlreg | GPT_CR_EN);
232e0511b6cSIan Lepore 
233e0511b6cSIan Lepore 	if (bootverbose)
234e0511b6cSIan Lepore 		device_printf(dev, "Running on %dKHz clock, base freq %uHz CR=0x%08x, PR=0x%08x\n",
235e0511b6cSIan Lepore 		    sc->clkfreq / 1000, basefreq, READ4(sc, IMX_GPT_CR), READ4(sc, IMX_GPT_PR));
236e0511b6cSIan Lepore 
237e0511b6cSIan Lepore 	/* Setup the timer interrupt. */
238a2c472e7SAleksandr Rybalko 	err = bus_setup_intr(dev, sc->res[1], INTR_TYPE_CLK, imx_gpt_intr,
239a2c472e7SAleksandr Rybalko 	    NULL, sc, &sc->sc_ih);
240a2c472e7SAleksandr Rybalko 	if (err != 0) {
241a2c472e7SAleksandr Rybalko 		bus_release_resources(dev, imx_gpt_spec, sc->res);
242a2c472e7SAleksandr Rybalko 		device_printf(dev, "Unable to setup the clock irq handler, "
243a2c472e7SAleksandr Rybalko 		    "err = %d\n", err);
244a2c472e7SAleksandr Rybalko 		return (ENXIO);
245a2c472e7SAleksandr Rybalko 	}
246a2c472e7SAleksandr Rybalko 
247928e4f22SIan Lepore 	/*
248928e4f22SIan Lepore 	 * Measure how many clock ticks it takes to setup a one-shot event (it's
249928e4f22SIan Lepore 	 * longer than you might think, due to wait states in accessing gpt
250928e4f22SIan Lepore 	 * registers).  Scale up the result by a factor of 1.5 to be safe,
251928e4f22SIan Lepore 	 * and use that to set the minimum eventtimer period we can schedule. In
252928e4f22SIan Lepore 	 * the real world, the value works out to about 750ns on imx5 hardware.
253928e4f22SIan Lepore 	 */
254928e4f22SIan Lepore 	t1 = READ4(sc, IMX_GPT_CNT);
255928e4f22SIan Lepore 	WRITE4(sc, IMX_GPT_OCR3, 0);
256928e4f22SIan Lepore 	t2 = READ4(sc, IMX_GPT_CNT);
257928e4f22SIan Lepore 	setup_ticks = ((t2 - t1 + 1) * 3) / 2;
258928e4f22SIan Lepore 
259e0511b6cSIan Lepore 	/* Register as an eventtimer. */
260f3549ad5SIan Lepore 	sc->et.et_name = "iMXGPT";
261a2c472e7SAleksandr Rybalko 	sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERIODIC;
262f3549ad5SIan Lepore 	sc->et.et_quality = 800;
263a2c472e7SAleksandr Rybalko 	sc->et.et_frequency = sc->clkfreq;
264928e4f22SIan Lepore 	sc->et.et_min_period = ((uint64_t)setup_ticks << 32) / sc->clkfreq;
265928e4f22SIan Lepore 	sc->et.et_max_period = ((uint64_t)0xfffffffe  << 32) / sc->clkfreq;
266a2c472e7SAleksandr Rybalko 	sc->et.et_start = imx_gpt_timer_start;
267a2c472e7SAleksandr Rybalko 	sc->et.et_stop = imx_gpt_timer_stop;
268a2c472e7SAleksandr Rybalko 	sc->et.et_priv = sc;
269a2c472e7SAleksandr Rybalko 	et_register(&sc->et);
270a2c472e7SAleksandr Rybalko 
271e0511b6cSIan Lepore 	/* Register as a timecounter. */
272a2c472e7SAleksandr Rybalko 	imx_gpt_timecounter.tc_frequency = sc->clkfreq;
273b4fca5d4SAndrew Turner 	imx_gpt_timecounter.tc_priv = sc;
274a2c472e7SAleksandr Rybalko 	tc_init(&imx_gpt_timecounter);
275a2c472e7SAleksandr Rybalko 
276e0511b6cSIan Lepore 	/* If this is the first unit, store the softc for use in DELAY. */
2774bd9887cSAndrew Turner 	if (device_get_unit(dev) == 0) {
2784bd9887cSAndrew Turner 		arm_set_delay(imx_gpt_do_delay, sc);
2794bd9887cSAndrew Turner 	}
280a2c472e7SAleksandr Rybalko 
281a2c472e7SAleksandr Rybalko 	return (0);
282a2c472e7SAleksandr Rybalko }
283a2c472e7SAleksandr Rybalko 
284a2c472e7SAleksandr Rybalko static int
imx_gpt_timer_start(struct eventtimer * et,sbintime_t first,sbintime_t period)285a2c472e7SAleksandr Rybalko imx_gpt_timer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
286a2c472e7SAleksandr Rybalko {
287a2c472e7SAleksandr Rybalko 	struct imx_gpt_softc *sc;
288a2c472e7SAleksandr Rybalko 	uint32_t ticks;
289a2c472e7SAleksandr Rybalko 
290a2c472e7SAleksandr Rybalko 	sc = (struct imx_gpt_softc *)et->et_priv;
291a2c472e7SAleksandr Rybalko 
292a2c472e7SAleksandr Rybalko 	if (period != 0) {
293a2c472e7SAleksandr Rybalko 		sc->sc_period = ((uint32_t)et->et_frequency * period) >> 32;
294a2c472e7SAleksandr Rybalko 		/* Set expected value */
295a2c472e7SAleksandr Rybalko 		WRITE4(sc, IMX_GPT_OCR2, READ4(sc, IMX_GPT_CNT) + sc->sc_period);
296a2c472e7SAleksandr Rybalko 		/* Enable compare register 2 Interrupt */
2975e52290fSIan Lepore 		sc->ir_reg |= GPT_IR_OF2;
2985e52290fSIan Lepore 		WRITE4(sc, IMX_GPT_IR, sc->ir_reg);
29954fe6097SIan Lepore 		return (0);
300a2c472e7SAleksandr Rybalko 	} else if (first != 0) {
3015e52290fSIan Lepore 		/* Enable compare register 3 interrupt if not already on. */
3025e52290fSIan Lepore 		if ((sc->ir_reg & GPT_IR_OF3) == 0) {
3035e52290fSIan Lepore 			sc->ir_reg |= GPT_IR_OF3;
3045e52290fSIan Lepore 			WRITE4(sc, IMX_GPT_IR, sc->ir_reg);
3055e52290fSIan Lepore 		}
306a2c472e7SAleksandr Rybalko 		ticks = ((uint32_t)et->et_frequency * first) >> 32;
307a2c472e7SAleksandr Rybalko 		/* Do not disturb, otherwise event will be lost */
308a2c472e7SAleksandr Rybalko 		spinlock_enter();
309a2c472e7SAleksandr Rybalko 		/* Set expected value */
310f3549ad5SIan Lepore 		WRITE4(sc, IMX_GPT_OCR3, READ4(sc, IMX_GPT_CNT) + ticks);
311a2c472e7SAleksandr Rybalko 		/* Now everybody can relax */
312a2c472e7SAleksandr Rybalko 		spinlock_exit();
313a2c472e7SAleksandr Rybalko 		return (0);
314a2c472e7SAleksandr Rybalko 	}
315a2c472e7SAleksandr Rybalko 
316a2c472e7SAleksandr Rybalko 	return (EINVAL);
317a2c472e7SAleksandr Rybalko }
318a2c472e7SAleksandr Rybalko 
319a2c472e7SAleksandr Rybalko static int
imx_gpt_timer_stop(struct eventtimer * et)320a2c472e7SAleksandr Rybalko imx_gpt_timer_stop(struct eventtimer *et)
321a2c472e7SAleksandr Rybalko {
322a2c472e7SAleksandr Rybalko 	struct imx_gpt_softc *sc;
323a2c472e7SAleksandr Rybalko 
324a2c472e7SAleksandr Rybalko 	sc = (struct imx_gpt_softc *)et->et_priv;
325a2c472e7SAleksandr Rybalko 
3265e52290fSIan Lepore 	/* Disable interrupts and clear any pending status. */
3275e52290fSIan Lepore 	sc->ir_reg &= ~(GPT_IR_OF2 | GPT_IR_OF3);
3285e52290fSIan Lepore 	WRITE4(sc, IMX_GPT_IR, sc->ir_reg);
3295e52290fSIan Lepore 	WRITE4(sc, IMX_GPT_SR, GPT_IR_OF2 | GPT_IR_OF3);
330a2c472e7SAleksandr Rybalko 	sc->sc_period = 0;
331a2c472e7SAleksandr Rybalko 
332a2c472e7SAleksandr Rybalko 	return (0);
333a2c472e7SAleksandr Rybalko }
334a2c472e7SAleksandr Rybalko 
335a2c472e7SAleksandr Rybalko static int
imx_gpt_intr(void * arg)336a2c472e7SAleksandr Rybalko imx_gpt_intr(void *arg)
337a2c472e7SAleksandr Rybalko {
338a2c472e7SAleksandr Rybalko 	struct imx_gpt_softc *sc;
339a2c472e7SAleksandr Rybalko 	uint32_t status;
340a2c472e7SAleksandr Rybalko 
341a2c472e7SAleksandr Rybalko 	sc = (struct imx_gpt_softc *)arg;
342a2c472e7SAleksandr Rybalko 
34354fe6097SIan Lepore 	status = READ4(sc, IMX_GPT_SR);
344a2c472e7SAleksandr Rybalko 
34554fe6097SIan Lepore 	/*
34654fe6097SIan Lepore 	* Clear interrupt status before invoking event callbacks.  The callback
34754fe6097SIan Lepore 	* often sets up a new one-shot timer event and if the interval is short
34854fe6097SIan Lepore 	* enough it can fire before we get out of this function.  If we cleared
34954fe6097SIan Lepore 	* at the bottom we'd miss the interrupt and hang until the clock wraps.
35054fe6097SIan Lepore 	*/
35154fe6097SIan Lepore 	WRITE4(sc, IMX_GPT_SR, status);
35254fe6097SIan Lepore 
35354fe6097SIan Lepore 	/* Handle one-shot timer events. */
354f3549ad5SIan Lepore 	if (status & GPT_IR_OF3) {
355a2c472e7SAleksandr Rybalko 		if (sc->et.et_active) {
356a2c472e7SAleksandr Rybalko 			sc->et.et_event_cb(&sc->et, sc->et.et_arg);
357a2c472e7SAleksandr Rybalko 		}
358a2c472e7SAleksandr Rybalko 	}
35954fe6097SIan Lepore 
36054fe6097SIan Lepore 	/* Handle periodic timer events. */
361a2c472e7SAleksandr Rybalko 	if (status & GPT_IR_OF2) {
36254fe6097SIan Lepore 		if (sc->et.et_active)
363a2c472e7SAleksandr Rybalko 			sc->et.et_event_cb(&sc->et, sc->et.et_arg);
36454fe6097SIan Lepore 		if (sc->sc_period != 0)
365a2c472e7SAleksandr Rybalko 			WRITE4(sc, IMX_GPT_OCR2, READ4(sc, IMX_GPT_CNT) +
366a2c472e7SAleksandr Rybalko 			    sc->sc_period);
367a2c472e7SAleksandr Rybalko 	}
368a2c472e7SAleksandr Rybalko 
369a2c472e7SAleksandr Rybalko 	return (FILTER_HANDLED);
370a2c472e7SAleksandr Rybalko }
371a2c472e7SAleksandr Rybalko 
372b4fca5d4SAndrew Turner static u_int
imx_gpt_get_timecount(struct timecounter * tc)373a2c472e7SAleksandr Rybalko imx_gpt_get_timecount(struct timecounter *tc)
374a2c472e7SAleksandr Rybalko {
375b4fca5d4SAndrew Turner 	struct imx_gpt_softc *sc;
376a2c472e7SAleksandr Rybalko 
377b4fca5d4SAndrew Turner 	sc = tc->tc_priv;
378b4fca5d4SAndrew Turner 	return (READ4(sc, IMX_GPT_CNT));
379a2c472e7SAleksandr Rybalko }
380a2c472e7SAleksandr Rybalko 
381a2c472e7SAleksandr Rybalko static device_method_t imx_gpt_methods[] = {
382a2c472e7SAleksandr Rybalko 	DEVMETHOD(device_probe,		imx_gpt_probe),
383a2c472e7SAleksandr Rybalko 	DEVMETHOD(device_attach,	imx_gpt_attach),
384a2c472e7SAleksandr Rybalko 
385a2c472e7SAleksandr Rybalko 	DEVMETHOD_END
386a2c472e7SAleksandr Rybalko };
387a2c472e7SAleksandr Rybalko 
388a2c472e7SAleksandr Rybalko static driver_t imx_gpt_driver = {
389a2c472e7SAleksandr Rybalko 	"imx_gpt",
390a2c472e7SAleksandr Rybalko 	imx_gpt_methods,
391a2c472e7SAleksandr Rybalko 	sizeof(struct imx_gpt_softc),
392a2c472e7SAleksandr Rybalko };
393a2c472e7SAleksandr Rybalko 
394ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(imx_gpt, simplebus, imx_gpt_driver, 0, 0, BUS_PASS_TIMER);
395a2c472e7SAleksandr Rybalko 
3964bd9887cSAndrew Turner static void
imx_gpt_do_delay(int usec,void * arg)3974bd9887cSAndrew Turner imx_gpt_do_delay(int usec, void *arg)
398a2c472e7SAleksandr Rybalko {
3994bd9887cSAndrew Turner 	struct imx_gpt_softc *sc = arg;
400e0511b6cSIan Lepore 	uint64_t curcnt, endcnt, startcnt, ticks;
401a2c472e7SAleksandr Rybalko 
402e0511b6cSIan Lepore 	/*
403e0511b6cSIan Lepore 	 * Calculate the tick count with 64-bit values so that it works for any
404e0511b6cSIan Lepore 	 * clock frequency.  Loop until the hardware count reaches start+ticks.
405e0511b6cSIan Lepore 	 * If the 32-bit hardware count rolls over while we're looping, just
406e0511b6cSIan Lepore 	 * manually do a carry into the high bits after each read; don't worry
407e0511b6cSIan Lepore 	 * that doing this on each loop iteration is inefficient -- we're trying
408e0511b6cSIan Lepore 	 * to waste time here.
409e0511b6cSIan Lepore 	 */
4104bd9887cSAndrew Turner 	ticks = 1 + ((uint64_t)usec * sc->clkfreq) / 1000000;
4114bd9887cSAndrew Turner 	curcnt = startcnt = READ4(sc, IMX_GPT_CNT);
412e0511b6cSIan Lepore 	endcnt = startcnt + ticks;
413e0511b6cSIan Lepore 	while (curcnt < endcnt) {
4144bd9887cSAndrew Turner 		curcnt = READ4(sc, IMX_GPT_CNT);
415e0511b6cSIan Lepore 		if (curcnt < startcnt)
416e0511b6cSIan Lepore 			curcnt += 1ULL << 32;
417a2c472e7SAleksandr Rybalko 	}
418a2c472e7SAleksandr Rybalko }
419