2 * Copyright (c) 2011 The FreeBSD Foundation
3 * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
6 * Based on mpcore_timer.c developed by Ben Gray <ben.r.gray@gmail.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the company nor the name of the author may be used to
17 * endorse or promote products derived from this software without specific
18 * prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Cortex-A7, Cortex-A15, ARMv8 and later Generic Timer
38 #include "opt_platform.h"
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/malloc.h>
50 #include <sys/timeet.h>
51 #include <sys/timetc.h>
54 #include <sys/watchdog.h>
55 #include <machine/bus.h>
56 #include <machine/cpu.h>
57 #include <machine/intr.h>
58 #include <machine/md_var.h>
61 #include <machine/machdep.h> /* For arm_set_delay */
65 #include <dev/ofw/openfirm.h>
66 #include <dev/ofw/ofw_bus.h>
67 #include <dev/ofw/ofw_bus_subr.h>
71 #include <contrib/dev/acpica/include/acpi.h>
72 #include <dev/acpica/acpivar.h>
75 #define GT_CTRL_ENABLE (1 << 0)
76 #define GT_CTRL_INT_MASK (1 << 1)
77 #define GT_CTRL_INT_STAT (1 << 2)
81 #define GT_CNTKCTL_PL0PTEN (1 << 9) /* PL0 Physical timer reg access */
82 #define GT_CNTKCTL_PL0VTEN (1 << 8) /* PL0 Virtual timer reg access */
83 #define GT_CNTKCTL_EVNTI (0xf << 4) /* Virtual counter event bits */
84 #define GT_CNTKCTL_EVNTDIR (1 << 3) /* Virtual counter event transition */
85 #define GT_CNTKCTL_EVNTEN (1 << 2) /* Enables virtual counter events */
86 #define GT_CNTKCTL_PL0VCTEN (1 << 1) /* PL0 CNTVCT and CNTFRQ access */
87 #define GT_CNTKCTL_PL0PCTEN (1 << 0) /* PL0 CNTPCT and CNTFRQ access */
89 struct arm_tmr_softc {
90 struct resource *res[4];
97 static struct arm_tmr_softc *arm_tmr_sc = NULL;
99 static struct resource_spec timer_spec[] = {
100 { SYS_RES_IRQ, 0, RF_ACTIVE }, /* Secure */
101 { SYS_RES_IRQ, 1, RF_ACTIVE }, /* Non-secure */
102 { SYS_RES_IRQ, 2, RF_ACTIVE | RF_OPTIONAL }, /* Virt */
103 { SYS_RES_IRQ, 3, RF_ACTIVE | RF_OPTIONAL }, /* Hyp */
107 static uint32_t arm_tmr_fill_vdso_timehands(struct vdso_timehands *vdso_th,
108 struct timecounter *tc);
109 static void arm_tmr_do_delay(int usec, void *);
111 static timecounter_get_t arm_tmr_get_timecount;
113 static struct timecounter arm_tmr_timecount = {
114 .tc_name = "ARM MPCore Timecounter",
115 .tc_get_timecount = arm_tmr_get_timecount,
117 .tc_counter_mask = ~0u,
120 .tc_fill_vdso_timehands = arm_tmr_fill_vdso_timehands,
124 #define get_el0(x) cp15_## x ##_get()
125 #define get_el1(x) cp15_## x ##_get()
126 #define set_el0(x, val) cp15_## x ##_set(val)
127 #define set_el1(x, val) cp15_## x ##_set(val)
128 #else /* __aarch64__ */
129 #define get_el0(x) READ_SPECIALREG(x ##_el0)
130 #define get_el1(x) READ_SPECIALREG(x ##_el1)
131 #define set_el0(x, val) WRITE_SPECIALREG(x ##_el0, val)
132 #define set_el1(x, val) WRITE_SPECIALREG(x ##_el1, val)
138 return (get_el0(cntfrq));
142 get_cntxct(bool physical)
148 val = get_el0(cntpct);
150 val = get_el0(cntvct);
156 set_ctrl(uint32_t val, bool physical)
160 set_el0(cntp_ctl, val);
162 set_el0(cntv_ctl, val);
169 set_tval(uint32_t val, bool physical)
173 set_el0(cntp_tval, val);
175 set_el0(cntv_tval, val);
182 get_ctrl(bool physical)
187 val = get_el0(cntp_ctl);
189 val = get_el0(cntv_ctl);
195 setup_user_access(void *arg __unused)
199 cntkctl = get_el1(cntkctl);
200 cntkctl &= ~(GT_CNTKCTL_PL0PTEN | GT_CNTKCTL_PL0VTEN |
202 if (arm_tmr_sc->physical) {
203 cntkctl |= GT_CNTKCTL_PL0PCTEN;
204 cntkctl &= ~GT_CNTKCTL_PL0VCTEN;
206 cntkctl |= GT_CNTKCTL_PL0VCTEN;
207 cntkctl &= ~GT_CNTKCTL_PL0PCTEN;
209 set_el1(cntkctl, cntkctl);
214 tmr_setup_user_access(void *arg __unused)
217 if (arm_tmr_sc != NULL)
218 smp_rendezvous(NULL, setup_user_access, NULL, NULL);
220 SYSINIT(tmr_ua, SI_SUB_SMP, SI_ORDER_SECOND, tmr_setup_user_access, NULL);
223 arm_tmr_get_timecount(struct timecounter *tc)
226 return (get_cntxct(arm_tmr_sc->physical));
230 arm_tmr_start(struct eventtimer *et, sbintime_t first,
231 sbintime_t period __unused)
233 struct arm_tmr_softc *sc;
236 sc = (struct arm_tmr_softc *)et->et_priv;
239 counts = ((uint32_t)et->et_frequency * first) >> 32;
240 ctrl = get_ctrl(sc->physical);
241 ctrl &= ~GT_CTRL_INT_MASK;
242 ctrl |= GT_CTRL_ENABLE;
243 set_tval(counts, sc->physical);
244 set_ctrl(ctrl, sc->physical);
253 arm_tmr_disable(bool physical)
257 ctrl = get_ctrl(physical);
258 ctrl &= ~GT_CTRL_ENABLE;
259 set_ctrl(ctrl, physical);
263 arm_tmr_stop(struct eventtimer *et)
265 struct arm_tmr_softc *sc;
267 sc = (struct arm_tmr_softc *)et->et_priv;
268 arm_tmr_disable(sc->physical);
274 arm_tmr_intr(void *arg)
276 struct arm_tmr_softc *sc;
279 sc = (struct arm_tmr_softc *)arg;
280 ctrl = get_ctrl(sc->physical);
281 if (ctrl & GT_CTRL_INT_STAT) {
282 ctrl |= GT_CTRL_INT_MASK;
283 set_ctrl(ctrl, sc->physical);
286 if (sc->et.et_active)
287 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
289 return (FILTER_HANDLED);
294 arm_tmr_fdt_probe(device_t dev)
297 if (!ofw_bus_status_okay(dev))
300 if (ofw_bus_is_compatible(dev, "arm,armv7-timer")) {
301 device_set_desc(dev, "ARMv7 Generic Timer");
302 return (BUS_PROBE_DEFAULT);
303 } else if (ofw_bus_is_compatible(dev, "arm,armv8-timer")) {
304 device_set_desc(dev, "ARMv8 Generic Timer");
305 return (BUS_PROBE_DEFAULT);
314 arm_tmr_acpi_identify(driver_t *driver, device_t parent)
316 ACPI_TABLE_GTDT *gtdt;
320 physaddr = acpi_find_table(ACPI_SIG_GTDT);
324 gtdt = acpi_map_table(physaddr, ACPI_SIG_GTDT);
326 device_printf(parent, "gic: Unable to map the GTDT\n");
330 dev = BUS_ADD_CHILD(parent, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE,
331 "generic_timer", -1);
333 device_printf(parent, "add gic child failed\n");
337 BUS_SET_RESOURCE(parent, dev, SYS_RES_IRQ, 0,
338 gtdt->SecureEl1Interrupt, 1);
339 BUS_SET_RESOURCE(parent, dev, SYS_RES_IRQ, 1,
340 gtdt->NonSecureEl1Interrupt, 1);
341 BUS_SET_RESOURCE(parent, dev, SYS_RES_IRQ, 2,
342 gtdt->VirtualTimerInterrupt, 1);
345 acpi_unmap_table(gtdt);
349 arm_tmr_acpi_probe(device_t dev)
352 device_set_desc(dev, "ARM Generic Timer");
353 return (BUS_PROBE_NOWILDCARD);
359 arm_tmr_attach(device_t dev)
361 struct arm_tmr_softc *sc;
369 sc = device_get_softc(dev);
374 /* Get the base clock frequency */
375 node = ofw_bus_get_node(dev);
377 error = OF_getencprop(node, "clock-frequency", &clock,
384 if (sc->clkfreq == 0) {
385 /* Try to get clock frequency from timer */
386 sc->clkfreq = get_freq();
389 if (sc->clkfreq == 0) {
390 device_printf(dev, "No clock frequency specified\n");
394 if (bus_alloc_resources(dev, timer_spec, sc->res)) {
395 device_printf(dev, "could not allocate resources\n");
401 #else /* __aarch64__ */
402 /* If we do not have a virtual timer use the physical. */
403 sc->physical = (sc->res[2] == NULL) ? true : false;
408 /* Setup secure, non-secure and virtual IRQs handler */
409 for (i = 0; i < 3; i++) {
410 /* If we do not have the interrupt, skip it. */
411 if (sc->res[i] == NULL)
413 error = bus_setup_intr(dev, sc->res[i], INTR_TYPE_CLK,
414 arm_tmr_intr, NULL, sc, &sc->ihl[i]);
416 device_printf(dev, "Unable to alloc int resource.\n");
421 /* Disable the virtual timer until we are ready */
422 if (sc->res[2] != NULL)
423 arm_tmr_disable(false);
424 /* And the physical */
426 arm_tmr_disable(true);
428 arm_tmr_timecount.tc_frequency = sc->clkfreq;
429 tc_init(&arm_tmr_timecount);
431 sc->et.et_name = "ARM MPCore Eventtimer";
432 sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
433 sc->et.et_quality = 1000;
435 sc->et.et_frequency = sc->clkfreq;
436 sc->et.et_min_period = (0x00000010LLU << 32) / sc->et.et_frequency;
437 sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency;
438 sc->et.et_start = arm_tmr_start;
439 sc->et.et_stop = arm_tmr_stop;
441 et_register(&sc->et);
444 arm_set_delay(arm_tmr_do_delay, sc);
451 static device_method_t arm_tmr_fdt_methods[] = {
452 DEVMETHOD(device_probe, arm_tmr_fdt_probe),
453 DEVMETHOD(device_attach, arm_tmr_attach),
457 static driver_t arm_tmr_fdt_driver = {
460 sizeof(struct arm_tmr_softc),
463 static devclass_t arm_tmr_fdt_devclass;
465 EARLY_DRIVER_MODULE(timer, simplebus, arm_tmr_fdt_driver, arm_tmr_fdt_devclass,
466 0, 0, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
467 EARLY_DRIVER_MODULE(timer, ofwbus, arm_tmr_fdt_driver, arm_tmr_fdt_devclass,
468 0, 0, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
472 static device_method_t arm_tmr_acpi_methods[] = {
473 DEVMETHOD(device_identify, arm_tmr_acpi_identify),
474 DEVMETHOD(device_probe, arm_tmr_acpi_probe),
475 DEVMETHOD(device_attach, arm_tmr_attach),
479 static driver_t arm_tmr_acpi_driver = {
481 arm_tmr_acpi_methods,
482 sizeof(struct arm_tmr_softc),
485 static devclass_t arm_tmr_acpi_devclass;
487 EARLY_DRIVER_MODULE(timer, acpi, arm_tmr_acpi_driver, arm_tmr_acpi_devclass,
488 0, 0, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
492 arm_tmr_do_delay(int usec, void *arg)
494 struct arm_tmr_softc *sc = arg;
495 int32_t counts, counts_per_usec;
496 uint32_t first, last;
498 /* Get the number of times to count */
499 counts_per_usec = ((arm_tmr_timecount.tc_frequency / 1000000) + 1);
502 * Clamp the timeout at a maximum value (about 32 seconds with
503 * a 66MHz clock). *Nobody* should be delay()ing for anywhere
504 * near that length of time and if they are, they should be hung
507 if (usec >= (0x80000000U / counts_per_usec))
508 counts = (0x80000000U / counts_per_usec) - 1;
510 counts = usec * counts_per_usec;
512 first = get_cntxct(sc->physical);
515 last = get_cntxct(sc->physical);
516 counts -= (int32_t)(last - first);
521 #if defined(__aarch64__)
528 * Check the timers are setup, if not just
529 * use a for loop for the meantime
531 if (arm_tmr_sc == NULL) {
532 for (; usec > 0; usec--)
533 for (counts = 200; counts > 0; counts--)
535 * Prevent the compiler from optimizing
540 arm_tmr_do_delay(usec, arm_tmr_sc);
545 arm_tmr_fill_vdso_timehands(struct vdso_timehands *vdso_th,
546 struct timecounter *tc)
549 vdso_th->th_algo = VDSO_TH_ALGO_ARM_GENTIM;
550 vdso_th->th_physical = arm_tmr_sc->physical;
551 bzero(vdso_th->th_res, sizeof(vdso_th->th_res));