2 * Copyright (c) 2006 Benno Rice.
3 * Copyright (C) 2007-2008 MARVELL INTERNATIONAL LTD.
6 * Adapted to Marvell SoC by Semihalf.
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * from: FreeBSD: //depot/projects/arm/src/sys/arm/xscale/pxa2x0/pxa2x0_timer.c, rev 1
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
41 #include <sys/timetc.h>
42 #include <sys/watchdog.h>
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 #include <machine/frame.h>
46 #include <machine/intr.h>
48 #include <arm/mv/mvreg.h>
49 #include <arm/mv/mvvar.h>
51 #define MV_TIMER_TICK (get_tclk() / hz)
52 #define INITIAL_TIMECOUNTER (0xffffffff)
53 #define MAX_WATCHDOG_TICKS (0xffffffff)
55 struct mv_timer_softc {
56 struct resource * timer_res[2];
57 bus_space_tag_t timer_bst;
58 bus_space_handle_t timer_bsh;
62 static struct resource_spec mv_timer_spec[] = {
63 { SYS_RES_MEMORY, 0, RF_ACTIVE },
64 { SYS_RES_IRQ, 0, RF_ACTIVE },
68 static struct mv_timer_softc *timer_softc = NULL;
69 static int timers_initialized = 0;
71 static int mv_timer_probe(device_t);
72 static int mv_timer_attach(device_t);
74 static int mv_hardclock(void *);
75 static unsigned mv_timer_get_timecount(struct timecounter *);
77 static uint32_t mv_get_timer_control(void);
78 static void mv_set_timer_control(uint32_t);
79 static uint32_t mv_get_timer(uint32_t);
80 static void mv_set_timer(uint32_t, uint32_t);
81 static void mv_set_timer_rel(uint32_t, uint32_t);
82 static void mv_watchdog_enable(void);
83 static void mv_watchdog_disable(void);
84 static void mv_watchdog_event(void *, unsigned int, int *);
85 static void mv_setup_timer(void);
86 static void mv_setup_timercount(void);
88 static struct timecounter mv_timer_timecounter = {
89 .tc_get_timecount = mv_timer_get_timecount,
90 .tc_name = "CPU Timer",
91 .tc_frequency = 0, /* This is assigned on the fly in the init sequence */
92 .tc_counter_mask = ~0u,
97 mv_timer_probe(device_t dev)
100 device_set_desc(dev, "Marvell CPU Timer");
105 mv_timer_attach(device_t dev)
109 struct mv_timer_softc *sc;
111 if (timer_softc != NULL)
114 sc = (struct mv_timer_softc *)device_get_softc(dev);
117 error = bus_alloc_resources(dev, mv_timer_spec, sc->timer_res);
119 device_printf(dev, "could not allocate resources\n");
123 sc->timer_bst = rman_get_bustag(sc->timer_res[0]);
124 sc->timer_bsh = rman_get_bushandle(sc->timer_res[0]);
126 mtx_init(&timer_softc->timer_mtx, "watchdog", NULL, MTX_DEF);
127 mv_watchdog_disable();
128 EVENTHANDLER_REGISTER(watchdog_list, mv_watchdog_event, sc, 0);
130 if (bus_setup_intr(dev, sc->timer_res[1], INTR_TYPE_CLK,
131 mv_hardclock, NULL, NULL, &ihl) != 0) {
132 bus_release_resources(dev, mv_timer_spec, sc->timer_res);
133 device_printf(dev, "could not setup hardclock interrupt\n");
137 mv_setup_timercount();
138 timers_initialized = 1;
144 mv_hardclock(void *arg)
147 struct trapframe *frame;
149 frame = (struct trapframe *)arg;
150 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
152 irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
153 irq_cause &= ~(IRQ_TIMER0);
154 write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
156 return (FILTER_HANDLED);
159 static device_method_t mv_timer_methods[] = {
160 DEVMETHOD(device_probe, mv_timer_probe),
161 DEVMETHOD(device_attach, mv_timer_attach),
166 static driver_t mv_timer_driver = {
169 sizeof(struct mv_timer_softc),
172 static devclass_t mv_timer_devclass;
174 DRIVER_MODULE(timer, mbus, mv_timer_driver, mv_timer_devclass, 0, 0);
177 mv_timer_get_timecount(struct timecounter *tc)
180 return (INITIAL_TIMECOUNTER - mv_get_timer(1));
186 uint32_t irq_cause, irq_mask;
190 irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
191 irq_cause &= ~(IRQ_TIMER0);
192 write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
194 irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
195 irq_mask |= IRQ_TIMER0_MASK;
196 write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
198 mv_timer_timecounter.tc_frequency = get_tclk();
199 tc_init(&mv_timer_timecounter);
203 cpu_startprofclock(void)
209 cpu_stopprofclock(void)
217 uint32_t val, val_temp;
220 if (!timers_initialized) {
221 for (; usec > 0; usec--)
222 for (val = 100; val > 0; val--)
227 val = mv_get_timer(1);
228 nticks = ((get_tclk() / 1000000 + 1) * usec);
231 val_temp = mv_get_timer(1);
233 nticks -= (val - val_temp);
235 nticks -= (val + (INITIAL_TIMECOUNTER - val_temp));
242 mv_get_timer_control(void)
245 return (bus_space_read_4(timer_softc->timer_bst,
246 timer_softc->timer_bsh, CPU_TIMER_CONTROL));
250 mv_set_timer_control(uint32_t val)
253 bus_space_write_4(timer_softc->timer_bst,
254 timer_softc->timer_bsh, CPU_TIMER_CONTROL, val);
258 mv_get_timer(uint32_t timer)
261 return (bus_space_read_4(timer_softc->timer_bst,
262 timer_softc->timer_bsh, CPU_TIMER0 + timer * 0x8));
266 mv_set_timer(uint32_t timer, uint32_t val)
269 bus_space_write_4(timer_softc->timer_bst,
270 timer_softc->timer_bsh, CPU_TIMER0 + timer * 0x8, val);
274 mv_set_timer_rel(uint32_t timer, uint32_t val)
277 bus_space_write_4(timer_softc->timer_bst,
278 timer_softc->timer_bsh, CPU_TIMER0_REL + timer * 0x8, val);
282 mv_watchdog_enable(void)
285 uint32_t irq_cause, irq_mask;
287 irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
288 irq_cause &= ~(IRQ_TIMER_WD);
289 write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
291 irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
292 irq_mask |= IRQ_TIMER_WD_MASK;
293 write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
295 val = read_cpu_ctrl(RSTOUTn_MASK);
296 val |= WD_RST_OUT_EN;
297 write_cpu_ctrl(RSTOUTn_MASK, val);
299 val = mv_get_timer_control();
300 val |= CPU_TIMER_WD_EN | CPU_TIMER_WD_AUTO;
301 mv_set_timer_control(val);
305 mv_watchdog_disable(void)
308 uint32_t irq_cause, irq_mask;
310 val = mv_get_timer_control();
311 val &= ~(CPU_TIMER_WD_EN | CPU_TIMER_WD_AUTO);
312 mv_set_timer_control(val);
314 val = read_cpu_ctrl(RSTOUTn_MASK);
315 val &= ~WD_RST_OUT_EN;
316 write_cpu_ctrl(RSTOUTn_MASK, val);
318 irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
319 irq_mask &= ~(IRQ_TIMER_WD_MASK);
320 write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
322 irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
323 irq_cause &= ~(IRQ_TIMER_WD);
324 write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
329 * Watchdog event handler.
332 mv_watchdog_event(void *arg, unsigned int cmd, int *error)
337 mtx_lock(&timer_softc->timer_mtx);
339 mv_watchdog_disable();
342 * Watchdog timeout is in nanosecs, calculation according to
345 ns = (uint64_t)1 << (cmd & WD_INTERVAL);
346 ticks = (uint64_t)(ns * get_tclk()) / 1000000000;
347 if (ticks > MAX_WATCHDOG_TICKS)
348 mv_watchdog_disable();
350 /* Timer 2 is the watchdog */
351 mv_set_timer(2, ticks);
352 mv_watchdog_enable();
356 mtx_unlock(&timer_softc->timer_mtx);
364 mv_set_timer_rel(0, MV_TIMER_TICK);
365 mv_set_timer(0, MV_TIMER_TICK);
366 val = mv_get_timer_control();
367 val |= CPU_TIMER0_EN | CPU_TIMER0_AUTO;
368 mv_set_timer_control(val);
372 mv_setup_timercount(void)
376 mv_set_timer_rel(1, INITIAL_TIMECOUNTER);
377 mv_set_timer(1, INITIAL_TIMECOUNTER);
378 val = mv_get_timer_control();
379 val |= CPU_TIMER1_EN | CPU_TIMER1_AUTO;
380 mv_set_timer_control(val);