2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (c) 2012 Damjan Marion <dmarion@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
38 #include <sys/timeet.h>
39 #include <sys/timetc.h>
40 #include <sys/watchdog.h>
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/intr.h>
44 #include <machine/machdep.h>
46 #include <dev/ofw/openfirm.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
50 #include <machine/bus.h>
52 #define BCM2835_NUM_TIMERS 4
54 #define DEFAULT_TIMER 3
55 #define DEFAULT_TIMER_NAME "BCM2835-3"
56 #define DEFAULT_FREQUENCY 1000000
57 #define MIN_PERIOD 5LLU
59 #define SYSTIMER_CS 0x00
60 #define SYSTIMER_CLO 0x04
61 #define SYSTIMER_CHI 0x08
62 #define SYSTIMER_C0 0x0C
63 #define SYSTIMER_C1 0x10
64 #define SYSTIMER_C2 0x14
65 #define SYSTIMER_C3 0x18
73 struct bcm_systimer_softc {
74 struct resource* mem_res;
75 struct resource* irq_res[BCM2835_NUM_TIMERS];
76 void* intr_hl[BCM2835_NUM_TIMERS];
79 bus_space_handle_t bsh;
80 struct systimer st[BCM2835_NUM_TIMERS];
83 static struct resource_spec bcm_systimer_irq_spec[] = {
84 { SYS_RES_IRQ, 0, RF_ACTIVE },
85 { SYS_RES_IRQ, 1, RF_ACTIVE },
86 { SYS_RES_IRQ, 2, RF_ACTIVE },
87 { SYS_RES_IRQ, 3, RF_ACTIVE },
91 static struct bcm_systimer_softc *bcm_systimer_sc = NULL;
93 /* Read/Write macros for Timer used as timecounter */
94 #define bcm_systimer_tc_read_4(reg) \
95 bus_space_read_4(bcm_systimer_sc->bst, \
96 bcm_systimer_sc->bsh, reg)
98 #define bcm_systimer_tc_write_4(reg, val) \
99 bus_space_write_4(bcm_systimer_sc->bst, \
100 bcm_systimer_sc->bsh, reg, val)
102 static unsigned bcm_systimer_tc_get_timecount(struct timecounter *);
104 static delay_func bcm_systimer_delay;
106 static struct timecounter bcm_systimer_tc = {
107 .tc_name = DEFAULT_TIMER_NAME,
108 .tc_get_timecount = bcm_systimer_tc_get_timecount,
110 .tc_counter_mask = ~0u,
116 bcm_systimer_tc_get_timecount(struct timecounter *tc)
118 if (bcm_systimer_sc == NULL)
121 return bcm_systimer_tc_read_4(SYSTIMER_CLO);
125 bcm_systimer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
127 struct systimer *st = et->et_priv;
134 count = ((uint32_t)et->et_frequency * first) >> 32;
137 clo = bcm_systimer_tc_read_4(SYSTIMER_CLO);
141 * Clear pending interrupts
143 bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index));
144 bcm_systimer_tc_write_4(SYSTIMER_C0 + st->index*4, clo);
145 clo1 = bcm_systimer_tc_read_4(SYSTIMER_CLO);
146 if ((int32_t)(clo1 - clo) >= 0) {
161 bcm_systimer_stop(struct eventtimer *et)
163 struct systimer *st = et->et_priv;
170 bcm_systimer_intr(void *arg)
172 struct systimer *st = (struct systimer *)arg;
175 cs = bcm_systimer_tc_read_4(SYSTIMER_CS);
176 if ((cs & (1 << st->index)) == 0)
177 return (FILTER_STRAY);
180 bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index));
182 if (st->et.et_active) {
183 st->et.et_event_cb(&st->et, st->et.et_arg);
187 return (FILTER_HANDLED);
191 bcm_systimer_probe(device_t dev)
194 if (!ofw_bus_status_okay(dev))
197 if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-system-timer")) {
198 device_set_desc(dev, "BCM2835 System Timer");
199 return (BUS_PROBE_DEFAULT);
206 bcm_systimer_attach(device_t dev)
208 struct bcm_systimer_softc *sc = device_get_softc(dev);
212 if (bcm_systimer_sc != NULL)
215 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
216 if (sc->mem_res == NULL) {
217 device_printf(dev, "could not allocate memory resource\n");
221 sc->bst = rman_get_bustag(sc->mem_res);
222 sc->bsh = rman_get_bushandle(sc->mem_res);
224 /* Request the IRQ resources */
225 err = bus_alloc_resources(dev, bcm_systimer_irq_spec,
228 device_printf(dev, "Error: could not allocate irq resources\n");
232 /* TODO: get frequency from FDT */
233 sc->sysclk_freq = DEFAULT_FREQUENCY;
235 /* Setup and enable the timer */
236 if (bus_setup_intr(dev, sc->irq_res[DEFAULT_TIMER], INTR_TYPE_CLK,
237 bcm_systimer_intr, NULL, &sc->st[DEFAULT_TIMER],
238 &sc->intr_hl[DEFAULT_TIMER]) != 0) {
239 bus_release_resources(dev, bcm_systimer_irq_spec,
241 device_printf(dev, "Unable to setup the clock irq handler.\n");
245 sc->st[DEFAULT_TIMER].index = DEFAULT_TIMER;
246 sc->st[DEFAULT_TIMER].enabled = 0;
247 sc->st[DEFAULT_TIMER].et.et_name = DEFAULT_TIMER_NAME;
248 sc->st[DEFAULT_TIMER].et.et_flags = ET_FLAGS_ONESHOT;
249 sc->st[DEFAULT_TIMER].et.et_quality = 1000;
250 sc->st[DEFAULT_TIMER].et.et_frequency = sc->sysclk_freq;
251 sc->st[DEFAULT_TIMER].et.et_min_period =
252 (MIN_PERIOD << 32) / sc->st[DEFAULT_TIMER].et.et_frequency + 1;
253 sc->st[DEFAULT_TIMER].et.et_max_period =
254 (0x7ffffffeLLU << 32) / sc->st[DEFAULT_TIMER].et.et_frequency;
255 sc->st[DEFAULT_TIMER].et.et_start = bcm_systimer_start;
256 sc->st[DEFAULT_TIMER].et.et_stop = bcm_systimer_stop;
257 sc->st[DEFAULT_TIMER].et.et_priv = &sc->st[DEFAULT_TIMER];
258 et_register(&sc->st[DEFAULT_TIMER].et);
260 bcm_systimer_sc = sc;
262 if (device_get_unit(dev) == 0)
263 arm_set_delay(bcm_systimer_delay, sc);
265 bcm_systimer_tc.tc_frequency = DEFAULT_FREQUENCY;
266 tc_init(&bcm_systimer_tc);
271 static device_method_t bcm_systimer_methods[] = {
272 DEVMETHOD(device_probe, bcm_systimer_probe),
273 DEVMETHOD(device_attach, bcm_systimer_attach),
277 static driver_t bcm_systimer_driver = {
279 bcm_systimer_methods,
280 sizeof(struct bcm_systimer_softc),
283 static devclass_t bcm_systimer_devclass;
285 DRIVER_MODULE(bcm_systimer, simplebus, bcm_systimer_driver, bcm_systimer_devclass, 0, 0);
288 bcm_systimer_delay(int usec, void *arg)
290 struct bcm_systimer_softc *sc;
292 uint32_t first, last;
294 sc = (struct bcm_systimer_softc *) arg;
296 /* Get the number of times to count */
297 counts = usec * (bcm_systimer_tc.tc_frequency / 1000000) + 1;
299 first = bcm_systimer_tc_read_4(SYSTIMER_CLO);
302 last = bcm_systimer_tc_read_4(SYSTIMER_CLO);
306 counts -= (int32_t)(last - first);
308 counts -= (int32_t)((0xFFFFFFFF - first) + last);