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/timeet.h>
42 #include <sys/timetc.h>
43 #include <sys/watchdog.h>
44 #include <machine/bus.h>
45 #include <machine/cpu.h>
46 #include <machine/intr.h>
48 #include <arm/mv/mvreg.h>
49 #include <arm/mv/mvvar.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
54 #define INITIAL_TIMECOUNTER (0xffffffff)
55 #define MAX_WATCHDOG_TICKS (0xffffffff)
61 #if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X)
62 #define MV_CLOCK_SRC 25000000 /* Timers' 25MHz mode */
64 #define MV_CLOCK_SRC get_tclk()
67 #if defined(SOC_MV_ARMADA38X)
68 #define WATCHDOG_TIMER 4
70 #define WATCHDOG_TIMER 2
73 struct mv_timer_softc {
74 struct resource * timer_res[2];
75 bus_space_tag_t timer_bst;
76 bus_space_handle_t timer_bsh;
82 static struct resource_spec mv_timer_spec[] = {
83 { SYS_RES_MEMORY, 0, RF_ACTIVE },
84 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_OPTIONAL },
88 /* Interrupt is not required by MV_WDT devices */
89 static struct ofw_compat_data mv_timer_compat[] = {
90 {"mrvl,timer", MV_TMR | MV_WDT },
91 {"marvell,armada-380-wdt", MV_WDT },
95 static struct mv_timer_softc *timer_softc = NULL;
96 static int timers_initialized = 0;
98 static int mv_timer_probe(device_t);
99 static int mv_timer_attach(device_t);
101 static int mv_hardclock(void *);
102 static unsigned mv_timer_get_timecount(struct timecounter *);
104 static uint32_t mv_get_timer_control(void);
105 static void mv_set_timer_control(uint32_t);
106 static uint32_t mv_get_timer(uint32_t);
107 static void mv_set_timer(uint32_t, uint32_t);
108 static void mv_set_timer_rel(uint32_t, uint32_t);
109 static void mv_watchdog_enable(void);
110 static void mv_watchdog_disable(void);
111 static void mv_watchdog_event(void *, unsigned int, int *);
112 static int mv_timer_start(struct eventtimer *et,
113 sbintime_t first, sbintime_t period);
114 static int mv_timer_stop(struct eventtimer *et);
115 static void mv_setup_timers(void);
117 static struct timecounter mv_timer_timecounter = {
118 .tc_get_timecount = mv_timer_get_timecount,
119 .tc_name = "CPUTimer1",
120 .tc_frequency = 0, /* This is assigned on the fly in the init sequence */
121 .tc_counter_mask = ~0u,
126 mv_timer_probe(device_t dev)
129 if (!ofw_bus_status_okay(dev))
132 if (ofw_bus_search_compatible(dev, mv_timer_compat)->ocd_data == MV_NONE)
135 device_set_desc(dev, "Marvell CPU Timer");
140 mv_timer_attach(device_t dev)
144 struct mv_timer_softc *sc;
145 #if !defined(SOC_MV_ARMADAXP) && !defined(SOC_MV_ARMADA38X)
146 uint32_t irq_cause, irq_mask;
149 if (timer_softc != NULL)
152 sc = (struct mv_timer_softc *)device_get_softc(dev);
155 error = bus_alloc_resources(dev, mv_timer_spec, sc->timer_res);
157 device_printf(dev, "could not allocate resources\n");
161 sc->timer_bst = rman_get_bustag(sc->timer_res[0]);
162 sc->timer_bsh = rman_get_bushandle(sc->timer_res[0]);
164 sc->has_wdt = ofw_bus_has_prop(dev, "mrvl,has-wdt") ||
165 ofw_bus_is_compatible(dev, "marvell,armada-380-wdt");
167 mtx_init(&timer_softc->timer_mtx, "watchdog", NULL, MTX_DEF);
170 mv_watchdog_disable();
171 EVENTHANDLER_REGISTER(watchdog_list, mv_watchdog_event, sc, 0);
174 if (ofw_bus_search_compatible(dev, mv_timer_compat)->ocd_data
176 /* Don't set timers for wdt-only entry. */
177 device_printf(dev, "only watchdog attached\n");
179 } else if (sc->timer_res[1] == NULL) {
180 device_printf(dev, "no interrupt resource\n");
181 bus_release_resources(dev, mv_timer_spec, sc->timer_res);
185 if (bus_setup_intr(dev, sc->timer_res[1], INTR_TYPE_CLK,
186 mv_hardclock, NULL, sc, &ihl) != 0) {
187 bus_release_resources(dev, mv_timer_spec, sc->timer_res);
188 device_printf(dev, "Could not setup interrupt.\n");
193 #if !defined(SOC_MV_ARMADAXP) && !defined(SOC_MV_ARMADA38X)
194 irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
195 irq_cause &= IRQ_TIMER0_CLR;
197 write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
198 irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
199 irq_mask |= IRQ_TIMER0_MASK;
200 irq_mask &= ~IRQ_TIMER1_MASK;
201 write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
203 sc->et.et_name = "CPUTimer0";
204 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
205 sc->et.et_quality = 1000;
207 sc->et.et_frequency = MV_CLOCK_SRC;
208 sc->et.et_min_period = (0x00000002LLU << 32) / sc->et.et_frequency;
209 sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency;
210 sc->et.et_start = mv_timer_start;
211 sc->et.et_stop = mv_timer_stop;
213 et_register(&sc->et);
214 mv_timer_timecounter.tc_frequency = MV_CLOCK_SRC;
215 tc_init(&mv_timer_timecounter);
221 mv_hardclock(void *arg)
223 struct mv_timer_softc *sc;
226 irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
227 irq_cause &= IRQ_TIMER0_CLR;
228 write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
230 sc = (struct mv_timer_softc *)arg;
231 if (sc->et.et_active)
232 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
234 return (FILTER_HANDLED);
237 static device_method_t mv_timer_methods[] = {
238 DEVMETHOD(device_probe, mv_timer_probe),
239 DEVMETHOD(device_attach, mv_timer_attach),
244 static driver_t mv_timer_driver = {
247 sizeof(struct mv_timer_softc),
250 static devclass_t mv_timer_devclass;
252 DRIVER_MODULE(timer, simplebus, mv_timer_driver, mv_timer_devclass, 0, 0);
255 mv_timer_get_timecount(struct timecounter *tc)
258 return (INITIAL_TIMECOUNTER - mv_get_timer(1));
264 uint32_t val, val_temp;
267 if (!timers_initialized) {
268 for (; usec > 0; usec--)
269 for (val = 100; val > 0; val--)
270 __asm __volatile("nop" ::: "memory");
274 val = mv_get_timer(1);
275 nticks = ((MV_CLOCK_SRC / 1000000 + 1) * usec);
278 val_temp = mv_get_timer(1);
280 nticks -= (val - val_temp);
282 nticks -= (val + (INITIAL_TIMECOUNTER - val_temp));
289 mv_get_timer_control(void)
292 return (bus_space_read_4(timer_softc->timer_bst,
293 timer_softc->timer_bsh, CPU_TIMER_CONTROL));
297 mv_set_timer_control(uint32_t val)
300 bus_space_write_4(timer_softc->timer_bst,
301 timer_softc->timer_bsh, CPU_TIMER_CONTROL, val);
305 mv_get_timer(uint32_t timer)
308 return (bus_space_read_4(timer_softc->timer_bst,
309 timer_softc->timer_bsh, CPU_TIMER0 + timer * 0x8));
313 mv_set_timer(uint32_t timer, uint32_t val)
316 bus_space_write_4(timer_softc->timer_bst,
317 timer_softc->timer_bsh, CPU_TIMER0 + timer * 0x8, val);
321 mv_set_timer_rel(uint32_t timer, uint32_t val)
324 bus_space_write_4(timer_softc->timer_bst,
325 timer_softc->timer_bsh, CPU_TIMER0_REL + timer * 0x8, val);
329 mv_watchdog_enable(void)
331 uint32_t val, irq_cause;
332 #if !defined(SOC_MV_ARMADAXP) && !defined(SOC_MV_ARMADA38X)
336 irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
337 irq_cause &= IRQ_TIMER_WD_CLR;
338 write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
340 #if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X)
341 val = read_cpu_mp_clocks(WD_RSTOUTn_MASK);
342 val |= (WD_GLOBAL_MASK | WD_CPU0_MASK);
343 write_cpu_mp_clocks(WD_RSTOUTn_MASK, val);
345 val = read_cpu_misc(RSTOUTn_MASK);
346 val &= ~RSTOUTn_MASK_WD;
347 write_cpu_misc(RSTOUTn_MASK, val);
349 irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
350 irq_mask |= IRQ_TIMER_WD_MASK;
351 write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
353 val = read_cpu_ctrl(RSTOUTn_MASK);
354 val |= WD_RST_OUT_EN;
355 write_cpu_ctrl(RSTOUTn_MASK, val);
358 val = mv_get_timer_control();
359 #if defined(SOC_MV_ARMADA38X)
360 val |= CPU_TIMER_WD_EN | CPU_TIMER_WD_AUTO | CPU_TIMER_WD_25MHZ_EN;
361 #elif defined(SOC_MV_ARMADAXP)
362 val |= CPU_TIMER2_EN | CPU_TIMER2_AUTO | CPU_TIMER_WD_25MHZ_EN;
364 val |= CPU_TIMER2_EN | CPU_TIMER2_AUTO;
366 mv_set_timer_control(val);
370 mv_watchdog_disable(void)
372 uint32_t val, irq_cause;
373 #if !defined(SOC_MV_ARMADAXP) && !defined(SOC_MV_ARMADA38X)
377 val = mv_get_timer_control();
378 #if defined(SOC_MV_ARMADA38X)
379 val &= ~(CPU_TIMER_WD_EN | CPU_TIMER_WD_AUTO);
381 val &= ~(CPU_TIMER2_EN | CPU_TIMER2_AUTO);
383 mv_set_timer_control(val);
385 #if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X)
386 val = read_cpu_mp_clocks(WD_RSTOUTn_MASK);
387 val &= ~(WD_GLOBAL_MASK | WD_CPU0_MASK);
388 write_cpu_mp_clocks(WD_RSTOUTn_MASK, val);
390 val = read_cpu_misc(RSTOUTn_MASK);
391 val |= RSTOUTn_MASK_WD;
392 write_cpu_misc(RSTOUTn_MASK, RSTOUTn_MASK_WD);
394 val = read_cpu_ctrl(RSTOUTn_MASK);
395 val &= ~WD_RST_OUT_EN;
396 write_cpu_ctrl(RSTOUTn_MASK, val);
398 irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
399 irq_mask &= ~(IRQ_TIMER_WD_MASK);
400 write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
403 irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
404 irq_cause &= IRQ_TIMER_WD_CLR;
405 write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
410 * Watchdog event handler.
413 mv_watchdog_event(void *arg, unsigned int cmd, int *error)
418 mtx_lock(&timer_softc->timer_mtx);
420 mv_watchdog_disable();
423 * Watchdog timeout is in nanosecs, calculation according to
426 ns = (uint64_t)1 << (cmd & WD_INTERVAL);
427 ticks = (uint64_t)(ns * MV_CLOCK_SRC) / 1000000000;
428 if (ticks > MAX_WATCHDOG_TICKS)
429 mv_watchdog_disable();
431 mv_set_timer(WATCHDOG_TIMER, ticks);
432 mv_watchdog_enable();
436 mtx_unlock(&timer_softc->timer_mtx);
440 mv_timer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
442 struct mv_timer_softc *sc;
445 /* Calculate dividers. */
446 sc = (struct mv_timer_softc *)et->et_priv;
448 val = ((uint32_t)sc->et.et_frequency * period) >> 32;
452 val1 = ((uint32_t)sc->et.et_frequency * first) >> 32;
456 /* Apply configuration. */
457 mv_set_timer_rel(0, val);
458 mv_set_timer(0, val1);
459 val = mv_get_timer_control();
460 val |= CPU_TIMER0_EN;
462 val |= CPU_TIMER0_AUTO;
464 val &= ~CPU_TIMER0_AUTO;
465 mv_set_timer_control(val);
470 mv_timer_stop(struct eventtimer *et)
474 val = mv_get_timer_control();
475 val &= ~(CPU_TIMER0_EN | CPU_TIMER0_AUTO);
476 mv_set_timer_control(val);
481 mv_setup_timers(void)
485 mv_set_timer_rel(1, INITIAL_TIMECOUNTER);
486 mv_set_timer(1, INITIAL_TIMECOUNTER);
487 val = mv_get_timer_control();
488 val &= ~(CPU_TIMER0_EN | CPU_TIMER0_AUTO);
489 val |= CPU_TIMER1_EN | CPU_TIMER1_AUTO;
490 #if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X)
491 /* Enable 25MHz mode */
492 val |= CPU_TIMER0_25MHZ_EN | CPU_TIMER1_25MHZ_EN;
494 mv_set_timer_control(val);
495 timers_initialized = 1;