2 * Copyright 2013-2015 John Wehle <john@feith.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * Amlogic aml8726 timer driver.
31 * 16 bit Timer A is used for the event timer / hard clock.
32 * 32 bit Timer E is used for the timecounter / DELAY.
34 * The current implementation doesn't use Timers B-D. Another approach is
35 * to split the timers between the cores implementing per cpu event timers.
37 * The timers all share the MUX register which requires a mutex to serialize
38 * access. The mutex is also used to avoid potential problems between the
39 * interrupt handler and timer_start / timer_stop.
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/malloc.h>
52 #include <sys/timetc.h>
53 #include <sys/timeet.h>
55 #include <machine/bus.h>
56 #include <machine/cpu.h>
58 #include <dev/fdt/fdt_common.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
62 struct aml8726_timer_softc {
64 struct resource * res[2];
69 uint32_t period_ticks;
70 struct timecounter tc;
73 static struct resource_spec aml8726_timer_spec[] = {
74 { SYS_RES_MEMORY, 0, RF_ACTIVE },
75 { SYS_RES_IRQ, 0, RF_ACTIVE }, /* INT_TIMER_A */
80 * devclass_get_device / device_get_softc could be used
81 * to dynamically locate this, however the timers are a
82 * required device which can't be unloaded so there's
83 * no need for the overhead.
85 static struct aml8726_timer_softc *aml8726_timer_sc = NULL;
87 #define AML_TIMER_LOCK(sc) mtx_lock_spin(&(sc)->mtx)
88 #define AML_TIMER_UNLOCK(sc) mtx_unlock_spin(&(sc)->mtx)
89 #define AML_TIMER_LOCK_INIT(sc) \
90 mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \
92 #define AML_TIMER_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx);
94 #define AML_TIMER_MUX_REG 0
95 #define AML_TIMER_INPUT_1us 0
96 #define AML_TIMER_INPUT_10us 1
97 #define AML_TIMER_INPUT_100us 2
98 #define AML_TIMER_INPUT_1ms 3
99 #define AML_TIMER_INPUT_MASK 3
100 #define AML_TIMER_A_INPUT_MASK 3
101 #define AML_TIMER_A_INPUT_SHIFT 0
102 #define AML_TIMER_B_INPUT_MASK (3 << 2)
103 #define AML_TIMER_B_INPUT_SHIFT 2
104 #define AML_TIMER_C_INPUT_MASK (3 << 4)
105 #define AML_TIMER_C_INPUT_SHIFT 4
106 #define AML_TIMER_D_INPUT_MASK (3 << 6)
107 #define AML_TIMER_D_INPUT_SHIFT 6
108 #define AML_TIMER_E_INPUT_SYS 0
109 #define AML_TIMER_E_INPUT_1us 1
110 #define AML_TIMER_E_INPUT_10us 2
111 #define AML_TIMER_E_INPUT_100us 3
112 #define AML_TIMER_E_INPUT_1ms 4
113 #define AML_TIMER_E_INPUT_MASK (7 << 8)
114 #define AML_TIMER_E_INPUT_SHIFT 8
115 #define AML_TIMER_A_PERIODIC (1 << 12)
116 #define AML_TIMER_B_PERIODIC (1 << 13)
117 #define AML_TIMER_C_PERIODIC (1 << 14)
118 #define AML_TIMER_D_PERIODIC (1 << 15)
119 #define AML_TIMER_A_EN (1 << 16)
120 #define AML_TIMER_B_EN (1 << 17)
121 #define AML_TIMER_C_EN (1 << 18)
122 #define AML_TIMER_D_EN (1 << 19)
123 #define AML_TIMER_E_EN (1 << 20)
124 #define AML_TIMER_A_REG 4
125 #define AML_TIMER_B_REG 8
126 #define AML_TIMER_C_REG 12
127 #define AML_TIMER_D_REG 16
128 #define AML_TIMER_E_REG 20
130 #define CSR_WRITE_4(sc, reg, val) bus_write_4((sc)->res[0], reg, (val))
131 #define CSR_READ_4(sc, reg) bus_read_4((sc)->res[0], reg)
134 aml8726_get_timecount(struct timecounter *tc)
136 struct aml8726_timer_softc *sc =
137 (struct aml8726_timer_softc *)tc->tc_priv;
139 return CSR_READ_4(sc, AML_TIMER_E_REG);
143 aml8726_hardclock(void *arg)
145 struct aml8726_timer_softc *sc = (struct aml8726_timer_softc *)arg;
149 if (sc->first_ticks != 0 && sc->period_ticks != 0) {
152 CSR_WRITE_4(sc, AML_TIMER_A_REG, sc->period_ticks);
153 CSR_WRITE_4(sc, AML_TIMER_MUX_REG,
154 (CSR_READ_4(sc, AML_TIMER_MUX_REG) |
155 AML_TIMER_A_PERIODIC | AML_TIMER_A_EN));
158 AML_TIMER_UNLOCK(sc);
160 if (sc->et.et_active)
161 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
163 return (FILTER_HANDLED);
167 aml8726_timer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
169 struct aml8726_timer_softc *sc =
170 (struct aml8726_timer_softc *)et->et_priv;
171 uint32_t first_ticks;
172 uint32_t period_ticks;
176 first_ticks = (first * et->et_frequency) / SBT_1S;
177 period_ticks = (period * et->et_frequency) / SBT_1S;
179 if (first_ticks != 0) {
184 ticks = period_ticks;
185 periodic = AML_TIMER_A_PERIODIC;
193 sc->first_ticks = first_ticks;
194 sc->period_ticks = period_ticks;
196 CSR_WRITE_4(sc, AML_TIMER_A_REG, ticks);
197 CSR_WRITE_4(sc, AML_TIMER_MUX_REG,
198 ((CSR_READ_4(sc, AML_TIMER_MUX_REG) & ~AML_TIMER_A_PERIODIC) |
199 AML_TIMER_A_EN | periodic));
201 AML_TIMER_UNLOCK(sc);
207 aml8726_timer_stop(struct eventtimer *et)
209 struct aml8726_timer_softc *sc =
210 (struct aml8726_timer_softc *)et->et_priv;
214 CSR_WRITE_4(sc, AML_TIMER_MUX_REG,
215 (CSR_READ_4(sc, AML_TIMER_MUX_REG) & ~AML_TIMER_A_EN));
217 AML_TIMER_UNLOCK(sc);
223 aml8726_timer_probe(device_t dev)
226 if (!ofw_bus_status_okay(dev))
229 if (!ofw_bus_is_compatible(dev, "amlogic,meson6-timer"))
232 device_set_desc(dev, "Amlogic aml8726 timer");
234 return (BUS_PROBE_DEFAULT);
238 aml8726_timer_attach(device_t dev)
240 struct aml8726_timer_softc *sc = device_get_softc(dev);
242 /* There should be exactly one instance. */
243 if (aml8726_timer_sc != NULL)
248 if (bus_alloc_resources(dev, aml8726_timer_spec, sc->res)) {
249 device_printf(dev, "can not allocate resources for device\n");
254 * Disable the timers, select the input for each timer,
255 * clear timer E, and then enable timer E.
257 CSR_WRITE_4(sc, AML_TIMER_MUX_REG,
258 ((CSR_READ_4(sc, AML_TIMER_MUX_REG) &
259 ~(AML_TIMER_A_EN | AML_TIMER_A_INPUT_MASK |
260 AML_TIMER_E_EN | AML_TIMER_E_INPUT_MASK)) |
261 (AML_TIMER_INPUT_1us << AML_TIMER_A_INPUT_SHIFT) |
262 (AML_TIMER_E_INPUT_1us << AML_TIMER_E_INPUT_SHIFT)));
264 CSR_WRITE_4(sc, AML_TIMER_E_REG, 0);
266 CSR_WRITE_4(sc, AML_TIMER_MUX_REG,
267 (CSR_READ_4(sc, AML_TIMER_MUX_REG) | AML_TIMER_E_EN));
270 * Initialize the mutex prior to installing the interrupt handler
271 * in case of a spurious interrupt.
273 AML_TIMER_LOCK_INIT(sc);
275 if (bus_setup_intr(dev, sc->res[1], INTR_TYPE_CLK,
276 aml8726_hardclock, NULL, sc, &sc->ih_cookie)) {
277 device_printf(dev, "could not setup interrupt handler\n");
278 bus_release_resources(dev, aml8726_timer_spec, sc->res);
279 AML_TIMER_LOCK_DESTROY(sc);
283 aml8726_timer_sc = sc;
285 sc->et.et_name = "aml8726 timer A";
286 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
287 sc->et.et_frequency = 1000000;
288 sc->et.et_quality = 1000;
289 sc->et.et_min_period = (0x00000002LLU * SBT_1S) / sc->et.et_frequency;
290 sc->et.et_max_period = (0x0000fffeLLU * SBT_1S) / sc->et.et_frequency;
291 sc->et.et_start = aml8726_timer_start;
292 sc->et.et_stop = aml8726_timer_stop;
295 et_register(&sc->et);
297 sc->tc.tc_get_timecount = aml8726_get_timecount;
298 sc->tc.tc_name = "aml8726 timer E";
299 sc->tc.tc_frequency = 1000000;
300 sc->tc.tc_counter_mask = ~0u;
301 sc->tc.tc_quality = 1000;
310 aml8726_timer_detach(device_t dev)
316 static device_method_t aml8726_timer_methods[] = {
317 /* Device interface */
318 DEVMETHOD(device_probe, aml8726_timer_probe),
319 DEVMETHOD(device_attach, aml8726_timer_attach),
320 DEVMETHOD(device_detach, aml8726_timer_detach),
325 static driver_t aml8726_timer_driver = {
327 aml8726_timer_methods,
328 sizeof(struct aml8726_timer_softc),
331 static devclass_t aml8726_timer_devclass;
333 EARLY_DRIVER_MODULE(timer, simplebus, aml8726_timer_driver,
334 aml8726_timer_devclass, 0, 0, BUS_PASS_TIMER);
340 uint32_t delta, now, previous, remaining;
342 /* Timer has not yet been initialized */
343 if (aml8726_timer_sc == NULL) {
344 for (; usec > 0; usec--)
345 for (counter = 200; counter > 0; counter--) {
346 /* Prevent gcc from optimizing out the loop */
353 * Some of the other timers in the source tree do this calculation as:
355 * usec * ((sc->tc.tc_frequency / 1000000) + 1)
357 * which gives a fairly pessimistic result when tc_frequency is an exact
358 * multiple of 1000000. Given the data type and typical values for
359 * tc_frequency adding 999999 shouldn't overflow.
361 remaining = usec * ((aml8726_timer_sc->tc.tc_frequency + 999999) /
365 * We add one since the first iteration may catch the counter just
370 previous = aml8726_get_timecount(&aml8726_timer_sc->tc);
373 now = aml8726_get_timecount(&aml8726_timer_sc->tc);
376 * If the timer has rolled over, then we have the case:
378 * if (previous > now) {
379 * delta = (0 - previous) + now
382 * which is really no different then the normal case.
383 * Both cases are simply:
385 * delta = now - previous.
387 delta = now - previous;
389 if (delta >= remaining)