2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Benno Rice.
5 * Copyright (C) 2007-2008 MARVELL INTERNATIONAL LTD.
8 * Adapted to Marvell SoC by Semihalf.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * from: FreeBSD: //depot/projects/arm/src/sys/arm/xscale/pxa2x0/pxa2x0_timer.c, rev 1
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/malloc.h>
44 #include <sys/timeet.h>
45 #include <sys/timetc.h>
46 #include <sys/watchdog.h>
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/intr.h>
50 #include <machine/machdep.h>
52 #include <arm/mv/mvreg.h>
53 #include <arm/mv/mvvar.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
58 #define INITIAL_TIMECOUNTER (0xffffffff)
59 #define MAX_WATCHDOG_TICKS (0xffffffff)
65 #define MV_CLOCK_SRC_ARMV7 25000000 /* Timers' 25MHz mode */
67 #define WATCHDOG_TIMER_ARMV5 2
69 typedef void (*mv_watchdog_enable_t)(void);
70 typedef void (*mv_watchdog_disable_t)(void);
72 struct mv_timer_config {
73 enum soc_family soc_family;
74 mv_watchdog_enable_t watchdog_enable;
75 mv_watchdog_disable_t watchdog_disable;
76 unsigned int clock_src;
77 uint32_t bridge_irq_cause;
78 uint32_t irq_timer0_clr;
79 uint32_t irq_timer_wd_clr;
82 struct mv_timer_softc {
83 struct resource * timer_res[2];
84 bus_space_tag_t timer_bst;
85 bus_space_handle_t timer_bsh;
89 struct mv_timer_config* config;
92 static struct resource_spec mv_timer_spec[] = {
93 { SYS_RES_MEMORY, 0, RF_ACTIVE },
94 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_OPTIONAL },
98 /* Interrupt is not required by MV_WDT devices */
99 static struct ofw_compat_data mv_timer_compat[] = {
100 {"marvell,armada-380-timer", MV_NONE },
101 {"marvell,armada-xp-timer", MV_TMR | MV_WDT },
102 {"mrvl,timer", MV_TMR | MV_WDT },
106 static struct mv_timer_softc *timer_softc = NULL;
107 static int timers_initialized = 0;
109 static int mv_timer_probe(device_t);
110 static int mv_timer_attach(device_t);
112 static int mv_hardclock(void *);
113 static unsigned mv_timer_get_timecount(struct timecounter *);
115 static uint32_t mv_get_timer_control(void);
116 static void mv_set_timer_control(uint32_t);
117 static uint32_t mv_get_timer(uint32_t);
118 static void mv_set_timer(uint32_t, uint32_t);
119 static void mv_set_timer_rel(uint32_t, uint32_t);
120 static void mv_watchdog_event(void *, unsigned int, int *);
121 static int mv_timer_start(struct eventtimer *et,
122 sbintime_t first, sbintime_t period);
123 static int mv_timer_stop(struct eventtimer *et);
124 static void mv_setup_timers(void);
126 static void mv_watchdog_enable_armv5(void);
127 static void mv_watchdog_enable_armadaxp(void);
128 static void mv_watchdog_disable_armv5(void);
129 static void mv_watchdog_disable_armadaxp(void);
131 static void mv_delay(int usec, void* arg);
133 static struct mv_timer_config timer_armadaxp_config =
136 &mv_watchdog_enable_armadaxp,
137 &mv_watchdog_disable_armadaxp,
139 BRIDGE_IRQ_CAUSE_ARMADAXP,
140 IRQ_TIMER0_CLR_ARMADAXP,
141 IRQ_TIMER_WD_CLR_ARMADAXP,
143 static struct mv_timer_config timer_armv5_config =
146 &mv_watchdog_enable_armv5,
147 &mv_watchdog_disable_armv5,
154 static struct ofw_compat_data mv_timer_soc_config[] = {
155 {"marvell,armada-xp-timer", (uintptr_t)&timer_armadaxp_config },
156 {"mrvl,timer", (uintptr_t)&timer_armv5_config },
157 {NULL, (uintptr_t)NULL },
160 static struct timecounter mv_timer_timecounter = {
161 .tc_get_timecount = mv_timer_get_timecount,
162 .tc_name = "CPUTimer1",
163 .tc_frequency = 0, /* This is assigned on the fly in the init sequence */
164 .tc_counter_mask = ~0u,
169 mv_timer_probe(device_t dev)
172 if (!ofw_bus_status_okay(dev))
175 if (ofw_bus_search_compatible(dev, mv_timer_compat)->ocd_data == MV_NONE)
178 device_set_desc(dev, "Marvell CPU Timer");
183 mv_timer_attach(device_t dev)
187 struct mv_timer_softc *sc;
188 uint32_t irq_cause, irq_mask;
190 if (timer_softc != NULL)
193 sc = (struct mv_timer_softc *)device_get_softc(dev);
196 sc->config = (struct mv_timer_config*)
197 ofw_bus_search_compatible(dev, mv_timer_soc_config)->ocd_data;
199 if (sc->config->clock_src == 0)
200 sc->config->clock_src = get_tclk();
202 error = bus_alloc_resources(dev, mv_timer_spec, sc->timer_res);
204 device_printf(dev, "could not allocate resources\n");
208 sc->timer_bst = rman_get_bustag(sc->timer_res[0]);
209 sc->timer_bsh = rman_get_bushandle(sc->timer_res[0]);
211 sc->has_wdt = ofw_bus_has_prop(dev, "mrvl,has-wdt");
213 mtx_init(&timer_softc->timer_mtx, "watchdog", NULL, MTX_DEF);
216 if (sc->config->watchdog_disable)
217 sc->config->watchdog_disable();
218 EVENTHANDLER_REGISTER(watchdog_list, mv_watchdog_event, sc, 0);
221 if (ofw_bus_search_compatible(dev, mv_timer_compat)->ocd_data
223 /* Don't set timers for wdt-only entry. */
224 device_printf(dev, "only watchdog attached\n");
226 } else if (sc->timer_res[1] == NULL) {
227 device_printf(dev, "no interrupt resource\n");
228 bus_release_resources(dev, mv_timer_spec, sc->timer_res);
232 if (bus_setup_intr(dev, sc->timer_res[1], INTR_TYPE_CLK,
233 mv_hardclock, NULL, sc, &ihl) != 0) {
234 bus_release_resources(dev, mv_timer_spec, sc->timer_res);
235 device_printf(dev, "Could not setup interrupt.\n");
240 if (sc->config->soc_family != MV_SOC_ARMADA_XP ) {
241 irq_cause = read_cpu_ctrl(sc->config->bridge_irq_cause);
242 irq_cause &= sc->config->irq_timer0_clr;
244 write_cpu_ctrl(sc->config->bridge_irq_cause, irq_cause);
245 irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
246 irq_mask |= IRQ_TIMER0_MASK;
247 irq_mask &= ~IRQ_TIMER1_MASK;
248 write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
250 sc->et.et_name = "CPUTimer0";
251 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;
252 sc->et.et_quality = 1000;
254 sc->et.et_frequency = sc->config->clock_src;
255 sc->et.et_min_period = (0x00000002LLU << 32) / sc->et.et_frequency;
256 sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency;
257 sc->et.et_start = mv_timer_start;
258 sc->et.et_stop = mv_timer_stop;
260 et_register(&sc->et);
261 mv_timer_timecounter.tc_frequency = sc->config->clock_src;
262 tc_init(&mv_timer_timecounter);
265 arm_set_delay(mv_delay, NULL);
271 mv_hardclock(void *arg)
273 struct mv_timer_softc *sc;
276 irq_cause = read_cpu_ctrl(timer_softc->config->bridge_irq_cause);
277 irq_cause &= timer_softc->config->irq_timer0_clr;
278 write_cpu_ctrl(timer_softc->config->bridge_irq_cause, irq_cause);
280 sc = (struct mv_timer_softc *)arg;
281 if (sc->et.et_active)
282 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
284 return (FILTER_HANDLED);
287 static device_method_t mv_timer_methods[] = {
288 DEVMETHOD(device_probe, mv_timer_probe),
289 DEVMETHOD(device_attach, mv_timer_attach),
294 static driver_t mv_timer_driver = {
297 sizeof(struct mv_timer_softc),
300 static devclass_t mv_timer_devclass;
302 DRIVER_MODULE(timer_mv, simplebus, mv_timer_driver, mv_timer_devclass, 0, 0);
305 mv_timer_get_timecount(struct timecounter *tc)
308 return (INITIAL_TIMECOUNTER - mv_get_timer(1));
312 mv_delay(int usec, void* arg)
314 uint32_t val, val_temp;
317 val = mv_get_timer(1);
318 nticks = ((timer_softc->config->clock_src / 1000000 + 1) * usec);
321 val_temp = mv_get_timer(1);
323 nticks -= (val - val_temp);
325 nticks -= (val + (INITIAL_TIMECOUNTER - val_temp));
337 if (!timers_initialized) {
338 for (; usec > 0; usec--)
339 for (val = 100; val > 0; val--)
340 __asm __volatile("nop" ::: "memory");
343 mv_delay(usec, NULL);
350 mv_get_timer_control(void)
353 return (bus_space_read_4(timer_softc->timer_bst,
354 timer_softc->timer_bsh, CPU_TIMER_CONTROL));
358 mv_set_timer_control(uint32_t val)
361 bus_space_write_4(timer_softc->timer_bst,
362 timer_softc->timer_bsh, CPU_TIMER_CONTROL, val);
366 mv_get_timer(uint32_t timer)
369 return (bus_space_read_4(timer_softc->timer_bst,
370 timer_softc->timer_bsh, CPU_TIMER0 + timer * 0x8));
374 mv_set_timer(uint32_t timer, uint32_t val)
377 bus_space_write_4(timer_softc->timer_bst,
378 timer_softc->timer_bsh, CPU_TIMER0 + timer * 0x8, val);
382 mv_set_timer_rel(uint32_t timer, uint32_t val)
385 bus_space_write_4(timer_softc->timer_bst,
386 timer_softc->timer_bsh, CPU_TIMER0_REL + timer * 0x8, val);
390 mv_watchdog_enable_armv5(void)
392 uint32_t val, irq_cause, irq_mask;
394 irq_cause = read_cpu_ctrl(timer_softc->config->bridge_irq_cause);
395 irq_cause &= timer_softc->config->irq_timer_wd_clr;
396 write_cpu_ctrl(timer_softc->config->bridge_irq_cause, irq_cause);
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);
402 val = read_cpu_ctrl(RSTOUTn_MASK);
403 val |= WD_RST_OUT_EN;
404 write_cpu_ctrl(RSTOUTn_MASK, val);
406 val = mv_get_timer_control();
407 val |= CPU_TIMER2_EN | CPU_TIMER2_AUTO;
408 mv_set_timer_control(val);
412 mv_watchdog_enable_armadaxp(void)
414 uint32_t irq_cause, val;
416 irq_cause = read_cpu_ctrl(timer_softc->config->bridge_irq_cause);
417 irq_cause &= timer_softc->config->irq_timer_wd_clr;
418 write_cpu_ctrl(timer_softc->config->bridge_irq_cause, irq_cause);
420 val = read_cpu_mp_clocks(WD_RSTOUTn_MASK);
421 val |= (WD_GLOBAL_MASK | WD_CPU0_MASK);
422 write_cpu_mp_clocks(WD_RSTOUTn_MASK, val);
424 val = read_cpu_misc(RSTOUTn_MASK_ARMV7);
425 val &= ~RSTOUTn_MASK_WD;
426 write_cpu_misc(RSTOUTn_MASK_ARMV7, val);
428 val = mv_get_timer_control();
429 val |= CPU_TIMER2_EN | CPU_TIMER2_AUTO | CPU_TIMER_WD_25MHZ_EN;
430 mv_set_timer_control(val);
434 mv_watchdog_disable_armv5(void)
436 uint32_t val, irq_cause,irq_mask;
438 val = mv_get_timer_control();
439 val &= ~(CPU_TIMER2_EN | CPU_TIMER2_AUTO);
440 mv_set_timer_control(val);
442 val = read_cpu_ctrl(RSTOUTn_MASK);
443 val &= ~WD_RST_OUT_EN;
444 write_cpu_ctrl(RSTOUTn_MASK, val);
446 irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
447 irq_mask &= ~(IRQ_TIMER_WD_MASK);
448 write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
450 irq_cause = read_cpu_ctrl(timer_softc->config->bridge_irq_cause);
451 irq_cause &= timer_softc->config->irq_timer_wd_clr;
452 write_cpu_ctrl(timer_softc->config->bridge_irq_cause, irq_cause);
456 mv_watchdog_disable_armadaxp(void)
458 uint32_t val, irq_cause;
460 val = read_cpu_mp_clocks(WD_RSTOUTn_MASK);
461 val &= ~(WD_GLOBAL_MASK | WD_CPU0_MASK);
462 write_cpu_mp_clocks(WD_RSTOUTn_MASK, val);
464 val = read_cpu_misc(RSTOUTn_MASK_ARMV7);
465 val |= RSTOUTn_MASK_WD;
466 write_cpu_misc(RSTOUTn_MASK_ARMV7, RSTOUTn_MASK_WD);
468 irq_cause = read_cpu_ctrl(timer_softc->config->bridge_irq_cause);
469 irq_cause &= timer_softc->config->irq_timer_wd_clr;
470 write_cpu_ctrl(timer_softc->config->bridge_irq_cause, irq_cause);
472 val = mv_get_timer_control();
473 val &= ~(CPU_TIMER2_EN | CPU_TIMER2_AUTO);
474 mv_set_timer_control(val);
478 * Watchdog event handler.
481 mv_watchdog_event(void *arg, unsigned int cmd, int *error)
486 mtx_lock(&timer_softc->timer_mtx);
488 if (timer_softc->config->watchdog_disable != NULL)
489 timer_softc->config->watchdog_disable();
492 * Watchdog timeout is in nanosecs, calculation according to
495 ns = (uint64_t)1 << (cmd & WD_INTERVAL);
496 ticks = (uint64_t)(ns * timer_softc->config->clock_src) / 1000000000;
497 if (ticks > MAX_WATCHDOG_TICKS) {
498 if (timer_softc->config->watchdog_disable != NULL)
499 timer_softc->config->watchdog_disable();
501 mv_set_timer(WATCHDOG_TIMER_ARMV5, ticks);
502 if (timer_softc->config->watchdog_enable != NULL)
503 timer_softc->config->watchdog_enable();
507 mtx_unlock(&timer_softc->timer_mtx);
511 mv_timer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
513 struct mv_timer_softc *sc;
516 /* Calculate dividers. */
517 sc = (struct mv_timer_softc *)et->et_priv;
519 val = ((uint32_t)sc->et.et_frequency * period) >> 32;
523 val1 = ((uint32_t)sc->et.et_frequency * first) >> 32;
527 /* Apply configuration. */
528 mv_set_timer_rel(0, val);
529 mv_set_timer(0, val1);
530 val = mv_get_timer_control();
531 val |= CPU_TIMER0_EN;
533 val |= CPU_TIMER0_AUTO;
535 val &= ~CPU_TIMER0_AUTO;
536 mv_set_timer_control(val);
541 mv_timer_stop(struct eventtimer *et)
545 val = mv_get_timer_control();
546 val &= ~(CPU_TIMER0_EN | CPU_TIMER0_AUTO);
547 mv_set_timer_control(val);
552 mv_setup_timers(void)
556 mv_set_timer_rel(1, INITIAL_TIMECOUNTER);
557 mv_set_timer(1, INITIAL_TIMECOUNTER);
558 val = mv_get_timer_control();
559 val &= ~(CPU_TIMER0_EN | CPU_TIMER0_AUTO);
560 val |= CPU_TIMER1_EN | CPU_TIMER1_AUTO;
562 if (timer_softc->config->soc_family == MV_SOC_ARMADA_XP) {
563 /* Enable 25MHz mode */
564 val |= CPU_TIMER0_25MHZ_EN | CPU_TIMER1_25MHZ_EN;
567 mv_set_timer_control(val);
568 timers_initialized = 1;