1239278Sgonzo/*-
2239278Sgonzo * Copyright (c) 2011 Jakub Wojciech Klama <jceel@FreeBSD.org>
3239278Sgonzo * All rights reserved.
4239278Sgonzo *
5239278Sgonzo * Redistribution and use in source and binary forms, with or without
6239278Sgonzo * modification, are permitted provided that the following conditions
7239278Sgonzo * are met:
8239278Sgonzo * 1. Redistributions of source code must retain the above copyright
9239278Sgonzo *    notice, this list of conditions and the following disclaimer.
10239278Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
11239278Sgonzo *    notice, this list of conditions and the following disclaimer in the
12239278Sgonzo *    documentation and/or other materials provided with the distribution.
13239278Sgonzo *
14239278Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15239278Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16239278Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17239278Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18239278Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19239278Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20239278Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21239278Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22239278Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23239278Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24239278Sgonzo * SUCH DAMAGE.
25239278Sgonzo *
26239278Sgonzo */
27239278Sgonzo#include <sys/cdefs.h>
28239278Sgonzo__FBSDID("$FreeBSD: stable/11/sys/arm/lpc/lpc_timer.c 314503 2017-03-01 18:53:05Z ian $");
29239278Sgonzo
30239278Sgonzo#include <sys/param.h>
31239278Sgonzo#include <sys/systm.h>
32239278Sgonzo#include <sys/bus.h>
33239278Sgonzo#include <sys/kernel.h>
34239278Sgonzo#include <sys/module.h>
35239278Sgonzo#include <sys/malloc.h>
36239278Sgonzo#include <sys/rman.h>
37239278Sgonzo#include <sys/timetc.h>
38239278Sgonzo#include <sys/timeet.h>
39239278Sgonzo#include <machine/bus.h>
40239278Sgonzo#include <machine/cpu.h>
41239278Sgonzo#include <machine/intr.h>
42239278Sgonzo
43239278Sgonzo#include <dev/ofw/ofw_bus.h>
44239278Sgonzo#include <dev/ofw/ofw_bus_subr.h>
45239278Sgonzo
46239278Sgonzo#include <arm/lpc/lpcreg.h>
47239278Sgonzo#include <arm/lpc/lpcvar.h>
48239278Sgonzo
49239278Sgonzostruct lpc_timer_softc {
50239278Sgonzo	device_t		lt_dev;
51239278Sgonzo	struct eventtimer	lt_et;
52239278Sgonzo	struct resource	*	lt_res[5];
53239278Sgonzo	bus_space_tag_t		lt_bst0;
54239278Sgonzo	bus_space_handle_t	lt_bsh0;
55239278Sgonzo	bus_space_tag_t		lt_bst1;
56239278Sgonzo	bus_space_handle_t	lt_bsh1;
57239278Sgonzo	int			lt_oneshot;
58239278Sgonzo	uint32_t		lt_period;
59239278Sgonzo};
60239278Sgonzo
61239278Sgonzostatic struct resource_spec lpc_timer_spec[] = {
62239278Sgonzo	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
63239278Sgonzo	{ SYS_RES_MEMORY,	1,	RF_ACTIVE },
64239278Sgonzo	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
65239278Sgonzo	{ SYS_RES_IRQ,		1,	RF_ACTIVE },
66239278Sgonzo	{ -1, 0 }
67239278Sgonzo};
68239278Sgonzo
69239278Sgonzostatic struct lpc_timer_softc *timer_softc = NULL;
70239278Sgonzostatic int lpc_timer_initialized = 0;
71239278Sgonzostatic int lpc_timer_probe(device_t);
72239278Sgonzostatic int lpc_timer_attach(device_t);
73247463Smavstatic int lpc_timer_start(struct eventtimer *,
74247463Smav    sbintime_t first, sbintime_t period);
75239278Sgonzostatic int lpc_timer_stop(struct eventtimer *et);
76239278Sgonzostatic unsigned lpc_get_timecount(struct timecounter *);
77239278Sgonzostatic int lpc_hardclock(void *);
78239278Sgonzo
79239278Sgonzo#define	timer0_read_4(sc, reg)			\
80239278Sgonzo    bus_space_read_4(sc->lt_bst0, sc->lt_bsh0, reg)
81239278Sgonzo#define	timer0_write_4(sc, reg, val)		\
82239278Sgonzo    bus_space_write_4(sc->lt_bst0, sc->lt_bsh0, reg, val)
83239278Sgonzo#define	timer0_clear(sc)			\
84239278Sgonzo    do {					\
85239278Sgonzo	    timer0_write_4(sc, LPC_TIMER_TC, 0);	\
86239278Sgonzo	    timer0_write_4(sc, LPC_TIMER_PR, 0);	\
87239278Sgonzo	    timer0_write_4(sc, LPC_TIMER_PC, 0);	\
88239278Sgonzo    } while(0)
89239278Sgonzo
90239278Sgonzo#define	timer1_read_4(sc, reg)			\
91239278Sgonzo    bus_space_read_4(sc->lt_bst1, sc->lt_bsh1, reg)
92239278Sgonzo#define	timer1_write_4(sc, reg, val)		\
93239278Sgonzo    bus_space_write_4(sc->lt_bst1, sc->lt_bsh1, reg, val)
94239278Sgonzo#define	timer1_clear(sc)			\
95239278Sgonzo    do {					\
96239278Sgonzo	    timer1_write_4(sc, LPC_TIMER_TC, 0);	\
97239278Sgonzo	    timer1_write_4(sc, LPC_TIMER_PR, 0);	\
98239278Sgonzo	    timer1_write_4(sc, LPC_TIMER_PC, 0);	\
99239278Sgonzo    } while(0)
100239278Sgonzo
101239278Sgonzostatic struct timecounter lpc_timecounter = {
102239278Sgonzo	.tc_get_timecount = lpc_get_timecount,
103239278Sgonzo	.tc_name = "LPC32x0 Timer1",
104239278Sgonzo	.tc_frequency = 0, /* will be filled later */
105239278Sgonzo	.tc_counter_mask = ~0u,
106239278Sgonzo	.tc_quality = 1000,
107239278Sgonzo};
108239278Sgonzo
109239278Sgonzostatic int
110239278Sgonzolpc_timer_probe(device_t dev)
111239278Sgonzo{
112239278Sgonzo
113261410Sian	if (!ofw_bus_status_okay(dev))
114261410Sian		return (ENXIO);
115261410Sian
116239278Sgonzo	if (!ofw_bus_is_compatible(dev, "lpc,timer"))
117239278Sgonzo		return (ENXIO);
118239278Sgonzo
119239278Sgonzo	device_set_desc(dev, "LPC32x0 timer");
120239278Sgonzo	return (BUS_PROBE_DEFAULT);
121239278Sgonzo}
122239278Sgonzo
123239278Sgonzostatic int
124239278Sgonzolpc_timer_attach(device_t dev)
125239278Sgonzo{
126239278Sgonzo	void *intrcookie;
127239278Sgonzo	struct lpc_timer_softc *sc = device_get_softc(dev);
128239278Sgonzo	phandle_t node;
129239278Sgonzo	uint32_t freq;
130239278Sgonzo
131239278Sgonzo	if (timer_softc)
132239278Sgonzo		return (ENXIO);
133239278Sgonzo
134239278Sgonzo	timer_softc = sc;
135239278Sgonzo
136239278Sgonzo	if (bus_alloc_resources(dev, lpc_timer_spec, sc->lt_res)) {
137239278Sgonzo		device_printf(dev, "could not allocate resources\n");
138239278Sgonzo		return (ENXIO);
139239278Sgonzo	}
140239278Sgonzo
141239278Sgonzo	sc->lt_bst0 = rman_get_bustag(sc->lt_res[0]);
142239278Sgonzo	sc->lt_bsh0 = rman_get_bushandle(sc->lt_res[0]);
143239278Sgonzo	sc->lt_bst1 = rman_get_bustag(sc->lt_res[1]);
144239278Sgonzo	sc->lt_bsh1 = rman_get_bushandle(sc->lt_res[1]);
145239278Sgonzo
146239278Sgonzo	if (bus_setup_intr(dev, sc->lt_res[2], INTR_TYPE_CLK,
147239278Sgonzo	    lpc_hardclock, NULL, sc, &intrcookie)) {
148239278Sgonzo		device_printf(dev, "could not setup interrupt handler\n");
149239278Sgonzo		bus_release_resources(dev, lpc_timer_spec, sc->lt_res);
150239278Sgonzo		return (ENXIO);
151239278Sgonzo	}
152239278Sgonzo
153239278Sgonzo	/* Enable timer clock */
154239278Sgonzo	lpc_pwr_write(dev, LPC_CLKPWR_TIMCLK_CTRL1,
155239278Sgonzo	    LPC_CLKPWR_TIMCLK_CTRL1_TIMER0 |
156239278Sgonzo	    LPC_CLKPWR_TIMCLK_CTRL1_TIMER1);
157239278Sgonzo
158239278Sgonzo	/* Get PERIPH_CLK encoded in parent bus 'bus-frequency' property */
159239278Sgonzo	node = ofw_bus_get_node(dev);
160314503Sian	if (OF_getencprop(OF_parent(node), "bus-frequency", &freq,
161239278Sgonzo	    sizeof(pcell_t)) <= 0) {
162239278Sgonzo		bus_release_resources(dev, lpc_timer_spec, sc->lt_res);
163239278Sgonzo		bus_teardown_intr(dev, sc->lt_res[2], intrcookie);
164239278Sgonzo		device_printf(dev, "could not obtain base clock frequency\n");
165239278Sgonzo		return (ENXIO);
166239278Sgonzo	}
167239278Sgonzo
168239278Sgonzo	/* Set desired frequency in event timer and timecounter */
169239278Sgonzo	sc->lt_et.et_frequency = (uint64_t)freq;
170239278Sgonzo	lpc_timecounter.tc_frequency = (uint64_t)freq;
171239278Sgonzo
172239278Sgonzo	sc->lt_et.et_name = "LPC32x0 Timer0";
173239278Sgonzo	sc->lt_et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
174239278Sgonzo	sc->lt_et.et_quality = 1000;
175247463Smav	sc->lt_et.et_min_period = (0x00000002LLU << 32) / sc->lt_et.et_frequency;
176247463Smav	sc->lt_et.et_max_period = (0xfffffffeLLU << 32) / sc->lt_et.et_frequency;
177239278Sgonzo	sc->lt_et.et_start = lpc_timer_start;
178239278Sgonzo	sc->lt_et.et_stop = lpc_timer_stop;
179239278Sgonzo	sc->lt_et.et_priv = sc;
180239278Sgonzo
181239278Sgonzo	et_register(&sc->lt_et);
182239278Sgonzo	tc_init(&lpc_timecounter);
183239278Sgonzo
184239278Sgonzo	/* Reset and enable timecounter */
185239278Sgonzo	timer1_write_4(sc, LPC_TIMER_TCR, LPC_TIMER_TCR_RESET);
186239278Sgonzo	timer1_write_4(sc, LPC_TIMER_TCR, 0);
187239278Sgonzo	timer1_clear(sc);
188239278Sgonzo	timer1_write_4(sc, LPC_TIMER_TCR, LPC_TIMER_TCR_ENABLE);
189239278Sgonzo
190239278Sgonzo	/* DELAY() now can work properly */
191239278Sgonzo	lpc_timer_initialized = 1;
192239278Sgonzo
193239278Sgonzo	return (0);
194239278Sgonzo}
195239278Sgonzo
196239278Sgonzostatic int
197247463Smavlpc_timer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
198239278Sgonzo{
199239278Sgonzo	struct lpc_timer_softc *sc = (struct lpc_timer_softc *)et->et_priv;
200239278Sgonzo	uint32_t ticks;
201239278Sgonzo
202247463Smav	if (period == 0) {
203239278Sgonzo		sc->lt_oneshot = 1;
204247463Smav		sc->lt_period = 0;
205247463Smav	} else {
206239278Sgonzo		sc->lt_oneshot = 0;
207247463Smav		sc->lt_period = ((uint32_t)et->et_frequency * period) >> 32;
208239278Sgonzo	}
209239278Sgonzo
210247463Smav	if (first == 0)
211239278Sgonzo		ticks = sc->lt_period;
212247463Smav	else
213247463Smav		ticks = ((uint32_t)et->et_frequency * first) >> 32;
214239278Sgonzo
215239278Sgonzo	/* Reset timer */
216239278Sgonzo	timer0_write_4(sc, LPC_TIMER_TCR, LPC_TIMER_TCR_RESET);
217239278Sgonzo	timer0_write_4(sc, LPC_TIMER_TCR, 0);
218239278Sgonzo
219239278Sgonzo	/* Start timer */
220239278Sgonzo	timer0_clear(sc);
221239278Sgonzo	timer0_write_4(sc, LPC_TIMER_MR0, ticks);
222239278Sgonzo	timer0_write_4(sc, LPC_TIMER_MCR, LPC_TIMER_MCR_MR0I | LPC_TIMER_MCR_MR0S);
223239278Sgonzo	timer0_write_4(sc, LPC_TIMER_TCR, LPC_TIMER_TCR_ENABLE);
224239278Sgonzo	return (0);
225239278Sgonzo}
226239278Sgonzo
227239278Sgonzostatic int
228239278Sgonzolpc_timer_stop(struct eventtimer *et)
229239278Sgonzo{
230239278Sgonzo	struct lpc_timer_softc *sc = (struct lpc_timer_softc *)et->et_priv;
231239278Sgonzo
232239278Sgonzo	timer0_write_4(sc, LPC_TIMER_TCR, 0);
233239278Sgonzo	return (0);
234239278Sgonzo}
235239278Sgonzo
236239278Sgonzostatic device_method_t lpc_timer_methods[] = {
237239278Sgonzo	DEVMETHOD(device_probe,		lpc_timer_probe),
238239278Sgonzo	DEVMETHOD(device_attach,	lpc_timer_attach),
239239278Sgonzo	{ 0, 0 }
240239278Sgonzo};
241239278Sgonzo
242239278Sgonzostatic driver_t lpc_timer_driver = {
243239278Sgonzo	"timer",
244239278Sgonzo	lpc_timer_methods,
245239278Sgonzo	sizeof(struct lpc_timer_softc),
246239278Sgonzo};
247239278Sgonzo
248239278Sgonzostatic devclass_t lpc_timer_devclass;
249239278Sgonzo
250239278SgonzoDRIVER_MODULE(timer, simplebus, lpc_timer_driver, lpc_timer_devclass, 0, 0);
251239278Sgonzo
252239278Sgonzostatic int
253239278Sgonzolpc_hardclock(void *arg)
254239278Sgonzo{
255239278Sgonzo	struct lpc_timer_softc *sc = (struct lpc_timer_softc *)arg;
256239278Sgonzo
257239278Sgonzo	/* Reset pending interrupt */
258239278Sgonzo	timer0_write_4(sc, LPC_TIMER_IR, 0xffffffff);
259239278Sgonzo
260239278Sgonzo	/* Start timer again */
261239278Sgonzo	if (!sc->lt_oneshot) {
262239278Sgonzo		timer0_clear(sc);
263239278Sgonzo		timer0_write_4(sc, LPC_TIMER_MR0, sc->lt_period);
264239278Sgonzo		timer0_write_4(sc, LPC_TIMER_TCR, LPC_TIMER_TCR_ENABLE);
265239278Sgonzo	}
266239278Sgonzo
267239278Sgonzo	if (sc->lt_et.et_active)
268239278Sgonzo		sc->lt_et.et_event_cb(&sc->lt_et, sc->lt_et.et_arg);
269239278Sgonzo
270239278Sgonzo	return (FILTER_HANDLED);
271239278Sgonzo}
272239278Sgonzo
273239278Sgonzostatic unsigned
274239278Sgonzolpc_get_timecount(struct timecounter *tc)
275239278Sgonzo{
276239278Sgonzo	return timer1_read_4(timer_softc, LPC_TIMER_TC);
277239278Sgonzo}
278239278Sgonzo
279239278Sgonzovoid
280239278SgonzoDELAY(int usec)
281239278Sgonzo{
282239278Sgonzo	uint32_t counter;
283239278Sgonzo	uint32_t first, last;
284239278Sgonzo	int val = (lpc_timecounter.tc_frequency / 1000000 + 1) * usec;
285239278Sgonzo
286239278Sgonzo	/* Timer is not initialized yet */
287239278Sgonzo	if (!lpc_timer_initialized) {
288239278Sgonzo		for (; usec > 0; usec--)
289239278Sgonzo			for (counter = 100; counter > 0; counter--)
290239278Sgonzo				;
291239278Sgonzo		return;
292239278Sgonzo	}
293239278Sgonzo
294239278Sgonzo	first = lpc_get_timecount(&lpc_timecounter);
295239278Sgonzo	while (val > 0) {
296239278Sgonzo		last = lpc_get_timecount(&lpc_timecounter);
297239278Sgonzo		if (last < first) {
298239278Sgonzo			/* Timer rolled over */
299239278Sgonzo			last = first;
300239278Sgonzo		}
301239278Sgonzo
302239278Sgonzo		val -= (last - first);
303239278Sgonzo		first = last;
304239278Sgonzo	}
305239278Sgonzo}
306