xref: /freebsd/sys/arm/mv/armada/wdt.c (revision 1ccf882c6b34d4734c3a7b5b14dcdb0c3497f204)
192ce47d9SZbigniew Bodek /*-
292ce47d9SZbigniew Bodek  * Copyright (c) 2006 Benno Rice.
392ce47d9SZbigniew Bodek  * Copyright (C) 2007-2008 MARVELL INTERNATIONAL LTD.
492ce47d9SZbigniew Bodek  * All rights reserved.
592ce47d9SZbigniew Bodek  *
692ce47d9SZbigniew Bodek  * Adapted to Marvell SoC by Semihalf.
792ce47d9SZbigniew Bodek  *
892ce47d9SZbigniew Bodek  * Redistribution and use in source and binary forms, with or without
992ce47d9SZbigniew Bodek  * modification, are permitted provided that the following conditions
1092ce47d9SZbigniew Bodek  * are met:
1192ce47d9SZbigniew Bodek  * 1. Redistributions of source code must retain the above copyright
1292ce47d9SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer.
1392ce47d9SZbigniew Bodek  * 2. Redistributions in binary form must reproduce the above copyright
1492ce47d9SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer in the
1592ce47d9SZbigniew Bodek  *    documentation and/or other materials provided with the distribution.
1692ce47d9SZbigniew Bodek  *
1792ce47d9SZbigniew Bodek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1892ce47d9SZbigniew Bodek  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1992ce47d9SZbigniew Bodek  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2092ce47d9SZbigniew Bodek  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2192ce47d9SZbigniew Bodek  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2292ce47d9SZbigniew Bodek  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2392ce47d9SZbigniew Bodek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2492ce47d9SZbigniew Bodek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2592ce47d9SZbigniew Bodek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2692ce47d9SZbigniew Bodek  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2792ce47d9SZbigniew Bodek  *
2892ce47d9SZbigniew Bodek  * from: FreeBSD: //depot/projects/arm/src/sys/arm/xscale/pxa2x0/pxa2x0_timer.c, rev 1
2992ce47d9SZbigniew Bodek  */
3092ce47d9SZbigniew Bodek 
3192ce47d9SZbigniew Bodek #include <sys/param.h>
3292ce47d9SZbigniew Bodek #include <sys/systm.h>
3392ce47d9SZbigniew Bodek #include <sys/bus.h>
34e2e050c8SConrad Meyer #include <sys/eventhandler.h>
3592ce47d9SZbigniew Bodek #include <sys/kernel.h>
3692ce47d9SZbigniew Bodek #include <sys/module.h>
3792ce47d9SZbigniew Bodek #include <sys/malloc.h>
3892ce47d9SZbigniew Bodek #include <sys/rman.h>
3992ce47d9SZbigniew Bodek #include <sys/kdb.h>
4092ce47d9SZbigniew Bodek #include <sys/timeet.h>
4192ce47d9SZbigniew Bodek #include <sys/timetc.h>
4292ce47d9SZbigniew Bodek #include <sys/watchdog.h>
4392ce47d9SZbigniew Bodek #include <machine/bus.h>
4492ce47d9SZbigniew Bodek #include <machine/cpu.h>
4592ce47d9SZbigniew Bodek 
4692ce47d9SZbigniew Bodek #include <arm/mv/mvreg.h>
4792ce47d9SZbigniew Bodek #include <arm/mv/mvvar.h>
4892ce47d9SZbigniew Bodek 
4992ce47d9SZbigniew Bodek #include <dev/ofw/ofw_bus.h>
5092ce47d9SZbigniew Bodek #include <dev/ofw/ofw_bus_subr.h>
5192ce47d9SZbigniew Bodek 
5292ce47d9SZbigniew Bodek #define INITIAL_TIMECOUNTER	(0xffffffff)
5392ce47d9SZbigniew Bodek #define MAX_WATCHDOG_TICKS	(0xffffffff)
54d100eecfSMarcin Wojtas #define WD_RST_OUT_EN           0x00000002
5592ce47d9SZbigniew Bodek 
56d100eecfSMarcin Wojtas #define	MV_CLOCK_SRC_ARMV7	25000000	/* Timers' 25MHz mode */
5792ce47d9SZbigniew Bodek 
58d100eecfSMarcin Wojtas struct mv_wdt_config {
59d100eecfSMarcin Wojtas 	uint32_t wdt_timer;
60d100eecfSMarcin Wojtas 	void (*wdt_enable)(void);
61d100eecfSMarcin Wojtas 	void (*wdt_disable)(void);
62d100eecfSMarcin Wojtas 	unsigned int wdt_clock_src;
63d100eecfSMarcin Wojtas };
64d100eecfSMarcin Wojtas 
65d100eecfSMarcin Wojtas static void mv_wdt_enable_armv5(void);
66d100eecfSMarcin Wojtas static void mv_wdt_enable_armada_38x(void);
67d100eecfSMarcin Wojtas static void mv_wdt_enable_armada_xp(void);
68*e5d7c5c8SKornel Dulęba static inline void mv_wdt_enable_armada_38x_xp_helper(void);
69d100eecfSMarcin Wojtas 
70d100eecfSMarcin Wojtas static void mv_wdt_disable_armv5(void);
71d100eecfSMarcin Wojtas static void mv_wdt_disable_armada_38x(void);
72d100eecfSMarcin Wojtas static void mv_wdt_disable_armada_xp(void);
73d100eecfSMarcin Wojtas 
74d100eecfSMarcin Wojtas static struct mv_wdt_config mv_wdt_armada_38x_config = {
75d100eecfSMarcin Wojtas 	.wdt_timer = 4,
76d100eecfSMarcin Wojtas 	.wdt_enable = &mv_wdt_enable_armada_38x,
77d100eecfSMarcin Wojtas 	.wdt_disable = &mv_wdt_disable_armada_38x,
78d100eecfSMarcin Wojtas 	.wdt_clock_src = MV_CLOCK_SRC_ARMV7,
79d100eecfSMarcin Wojtas };
80d100eecfSMarcin Wojtas 
81d100eecfSMarcin Wojtas static struct mv_wdt_config mv_wdt_armada_xp_config = {
82d100eecfSMarcin Wojtas 	.wdt_timer = 2,
83d100eecfSMarcin Wojtas 	.wdt_enable = &mv_wdt_enable_armada_xp,
84d100eecfSMarcin Wojtas 	.wdt_disable = &mv_wdt_disable_armada_xp,
85d100eecfSMarcin Wojtas 	.wdt_clock_src = MV_CLOCK_SRC_ARMV7,
86d100eecfSMarcin Wojtas };
87d100eecfSMarcin Wojtas 
88d100eecfSMarcin Wojtas static struct mv_wdt_config mv_wdt_armv5_config = {
89d100eecfSMarcin Wojtas 	.wdt_timer = 2,
90d100eecfSMarcin Wojtas 	.wdt_enable = &mv_wdt_enable_armv5,
91d100eecfSMarcin Wojtas 	.wdt_disable = &mv_wdt_disable_armv5,
92d100eecfSMarcin Wojtas 	.wdt_clock_src = 0,
93d100eecfSMarcin Wojtas };
9492ce47d9SZbigniew Bodek 
9592ce47d9SZbigniew Bodek struct mv_wdt_softc {
9692ce47d9SZbigniew Bodek 	struct resource	*	wdt_res;
9792ce47d9SZbigniew Bodek 	struct mtx		wdt_mtx;
98d100eecfSMarcin Wojtas 	struct mv_wdt_config *	wdt_config;
9992ce47d9SZbigniew Bodek };
10092ce47d9SZbigniew Bodek 
10192ce47d9SZbigniew Bodek static struct resource_spec mv_wdt_spec[] = {
10292ce47d9SZbigniew Bodek 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
10392ce47d9SZbigniew Bodek 	{ -1, 0 }
10492ce47d9SZbigniew Bodek };
10592ce47d9SZbigniew Bodek 
10692ce47d9SZbigniew Bodek static struct ofw_compat_data mv_wdt_compat[] = {
107d100eecfSMarcin Wojtas 	{"marvell,armada-380-wdt",	(uintptr_t)&mv_wdt_armada_38x_config},
108d100eecfSMarcin Wojtas 	{"marvell,armada-xp-wdt",	(uintptr_t)&mv_wdt_armada_xp_config},
109d100eecfSMarcin Wojtas 	{"marvell,orion-wdt",		(uintptr_t)&mv_wdt_armv5_config},
110d100eecfSMarcin Wojtas 	{NULL,				(uintptr_t)NULL}
11192ce47d9SZbigniew Bodek };
11292ce47d9SZbigniew Bodek 
11392ce47d9SZbigniew Bodek static struct mv_wdt_softc *wdt_softc = NULL;
11492ce47d9SZbigniew Bodek int timers_initialized = 0;
11592ce47d9SZbigniew Bodek 
11692ce47d9SZbigniew Bodek static int mv_wdt_probe(device_t);
11792ce47d9SZbigniew Bodek static int mv_wdt_attach(device_t);
11892ce47d9SZbigniew Bodek 
11992ce47d9SZbigniew Bodek static uint32_t	mv_get_timer_control(void);
12092ce47d9SZbigniew Bodek static void mv_set_timer_control(uint32_t);
12192ce47d9SZbigniew Bodek static void mv_set_timer(uint32_t, uint32_t);
12292ce47d9SZbigniew Bodek 
12392ce47d9SZbigniew Bodek static void mv_watchdog_event(void *, unsigned int, int *);
12492ce47d9SZbigniew Bodek 
12592ce47d9SZbigniew Bodek static device_method_t mv_wdt_methods[] = {
12692ce47d9SZbigniew Bodek 	DEVMETHOD(device_probe, mv_wdt_probe),
12792ce47d9SZbigniew Bodek 	DEVMETHOD(device_attach, mv_wdt_attach),
12892ce47d9SZbigniew Bodek 	{ 0, 0 }
12992ce47d9SZbigniew Bodek };
13092ce47d9SZbigniew Bodek 
13192ce47d9SZbigniew Bodek static driver_t mv_wdt_driver = {
13292ce47d9SZbigniew Bodek 	"wdt",
13392ce47d9SZbigniew Bodek 	mv_wdt_methods,
13492ce47d9SZbigniew Bodek 	sizeof(struct mv_wdt_softc),
13592ce47d9SZbigniew Bodek };
13692ce47d9SZbigniew Bodek 
137a3b866cbSJohn Baldwin DRIVER_MODULE(wdt, simplebus, mv_wdt_driver, 0, 0);
13892ce47d9SZbigniew Bodek 
13992ce47d9SZbigniew Bodek static int
mv_wdt_probe(device_t dev)14092ce47d9SZbigniew Bodek mv_wdt_probe(device_t dev)
14192ce47d9SZbigniew Bodek {
14292ce47d9SZbigniew Bodek 
14392ce47d9SZbigniew Bodek 	if (!ofw_bus_status_okay(dev))
14492ce47d9SZbigniew Bodek 		return (ENXIO);
14592ce47d9SZbigniew Bodek 
14692ce47d9SZbigniew Bodek 	if (!ofw_bus_search_compatible(dev, mv_wdt_compat)->ocd_data)
14792ce47d9SZbigniew Bodek 		return (ENXIO);
14892ce47d9SZbigniew Bodek 
14992ce47d9SZbigniew Bodek 	device_set_desc(dev, "Marvell Watchdog Timer");
15092ce47d9SZbigniew Bodek 	return (0);
15192ce47d9SZbigniew Bodek }
15292ce47d9SZbigniew Bodek 
15392ce47d9SZbigniew Bodek static int
mv_wdt_attach(device_t dev)15492ce47d9SZbigniew Bodek mv_wdt_attach(device_t dev)
15592ce47d9SZbigniew Bodek {
15692ce47d9SZbigniew Bodek 	struct mv_wdt_softc *sc;
15792ce47d9SZbigniew Bodek 	int error;
15892ce47d9SZbigniew Bodek 
15992ce47d9SZbigniew Bodek 	if (wdt_softc != NULL)
16092ce47d9SZbigniew Bodek 		return (ENXIO);
16192ce47d9SZbigniew Bodek 
16292ce47d9SZbigniew Bodek 	sc = device_get_softc(dev);
16392ce47d9SZbigniew Bodek 	wdt_softc = sc;
16492ce47d9SZbigniew Bodek 
16592ce47d9SZbigniew Bodek 	error = bus_alloc_resources(dev, mv_wdt_spec, &sc->wdt_res);
16692ce47d9SZbigniew Bodek 	if (error) {
16792ce47d9SZbigniew Bodek 		device_printf(dev, "could not allocate resources\n");
16892ce47d9SZbigniew Bodek 		return (ENXIO);
16992ce47d9SZbigniew Bodek 	}
17092ce47d9SZbigniew Bodek 
17192ce47d9SZbigniew Bodek 	mtx_init(&sc->wdt_mtx, "watchdog", NULL, MTX_DEF);
17292ce47d9SZbigniew Bodek 
173d100eecfSMarcin Wojtas 	sc->wdt_config = (struct mv_wdt_config *)
174d100eecfSMarcin Wojtas 	   ofw_bus_search_compatible(dev, mv_wdt_compat)->ocd_data;
175d100eecfSMarcin Wojtas 
176d100eecfSMarcin Wojtas 	if (sc->wdt_config->wdt_clock_src == 0)
177d100eecfSMarcin Wojtas 		sc->wdt_config->wdt_clock_src = get_tclk();
178d100eecfSMarcin Wojtas 
179d100eecfSMarcin Wojtas 	if (wdt_softc->wdt_config->wdt_disable != NULL)
180d100eecfSMarcin Wojtas 		wdt_softc->wdt_config->wdt_disable();
18192ce47d9SZbigniew Bodek 	EVENTHANDLER_REGISTER(watchdog_list, mv_watchdog_event, sc, 0);
18292ce47d9SZbigniew Bodek 
18392ce47d9SZbigniew Bodek 	return (0);
18492ce47d9SZbigniew Bodek }
18592ce47d9SZbigniew Bodek 
18692ce47d9SZbigniew Bodek static __inline uint32_t
mv_get_timer_control(void)18792ce47d9SZbigniew Bodek mv_get_timer_control(void)
18892ce47d9SZbigniew Bodek {
18992ce47d9SZbigniew Bodek 
19092ce47d9SZbigniew Bodek 	return (bus_read_4(wdt_softc->wdt_res, CPU_TIMER_CONTROL));
19192ce47d9SZbigniew Bodek }
19292ce47d9SZbigniew Bodek 
19392ce47d9SZbigniew Bodek static __inline void
mv_set_timer_control(uint32_t val)19492ce47d9SZbigniew Bodek mv_set_timer_control(uint32_t val)
19592ce47d9SZbigniew Bodek {
19692ce47d9SZbigniew Bodek 
19792ce47d9SZbigniew Bodek 	bus_write_4(wdt_softc->wdt_res, CPU_TIMER_CONTROL, val);
19892ce47d9SZbigniew Bodek }
19992ce47d9SZbigniew Bodek 
20092ce47d9SZbigniew Bodek static __inline void
mv_set_timer(uint32_t timer,uint32_t val)20192ce47d9SZbigniew Bodek mv_set_timer(uint32_t timer, uint32_t val)
20292ce47d9SZbigniew Bodek {
20392ce47d9SZbigniew Bodek 
20492ce47d9SZbigniew Bodek 	bus_write_4(wdt_softc->wdt_res, CPU_TIMER0 + timer * 0x8, val);
20592ce47d9SZbigniew Bodek }
20692ce47d9SZbigniew Bodek static void
mv_wdt_enable_armv5(void)207d100eecfSMarcin Wojtas mv_wdt_enable_armv5(void)
20892ce47d9SZbigniew Bodek {
209d100eecfSMarcin Wojtas 	uint32_t val, irq_cause, irq_mask;
21092ce47d9SZbigniew Bodek 
21192ce47d9SZbigniew Bodek 	irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
21292ce47d9SZbigniew Bodek 	irq_cause &= IRQ_TIMER_WD_CLR;
21392ce47d9SZbigniew Bodek 	write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
21492ce47d9SZbigniew Bodek 
21592ce47d9SZbigniew Bodek 	irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
21692ce47d9SZbigniew Bodek 	irq_mask |= IRQ_TIMER_WD_MASK;
21792ce47d9SZbigniew Bodek 	write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
21892ce47d9SZbigniew Bodek 
21992ce47d9SZbigniew Bodek 	val = read_cpu_ctrl(RSTOUTn_MASK);
22092ce47d9SZbigniew Bodek 	val |= WD_RST_OUT_EN;
22192ce47d9SZbigniew Bodek 	write_cpu_ctrl(RSTOUTn_MASK, val);
22292ce47d9SZbigniew Bodek 
22392ce47d9SZbigniew Bodek 	val = mv_get_timer_control();
22492ce47d9SZbigniew Bodek 	val |= CPU_TIMER2_EN | CPU_TIMER2_AUTO;
225d100eecfSMarcin Wojtas 	mv_set_timer_control(val);
226d100eecfSMarcin Wojtas }
227d100eecfSMarcin Wojtas 
228d100eecfSMarcin Wojtas static inline void
mv_wdt_enable_armada_38x_xp_helper(void)229*e5d7c5c8SKornel Dulęba mv_wdt_enable_armada_38x_xp_helper(void)
230d100eecfSMarcin Wojtas {
231d100eecfSMarcin Wojtas 	uint32_t val, irq_cause;
232d100eecfSMarcin Wojtas 
233d100eecfSMarcin Wojtas 	irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
234d100eecfSMarcin Wojtas 	irq_cause &= IRQ_TIMER_WD_CLR;
235d100eecfSMarcin Wojtas 	write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
236d100eecfSMarcin Wojtas 
237d100eecfSMarcin Wojtas 	val = read_cpu_mp_clocks(WD_RSTOUTn_MASK);
238d100eecfSMarcin Wojtas 	val |= (WD_GLOBAL_MASK | WD_CPU0_MASK);
239d100eecfSMarcin Wojtas 	write_cpu_mp_clocks(WD_RSTOUTn_MASK, val);
240d100eecfSMarcin Wojtas 
24104bb9a66SMarcin Wojtas 	val = read_cpu_misc(RSTOUTn_MASK_ARMV7);
242d100eecfSMarcin Wojtas 	val &= ~RSTOUTn_MASK_WD;
24304bb9a66SMarcin Wojtas 	write_cpu_misc(RSTOUTn_MASK_ARMV7, val);
244d100eecfSMarcin Wojtas }
245d100eecfSMarcin Wojtas 
246d100eecfSMarcin Wojtas static void
mv_wdt_enable_armada_38x(void)247d100eecfSMarcin Wojtas mv_wdt_enable_armada_38x(void)
248d100eecfSMarcin Wojtas {
249789bbd4dSMarcin Wojtas 	uint32_t val, irq_cause;
250789bbd4dSMarcin Wojtas 
251789bbd4dSMarcin Wojtas 	irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
252789bbd4dSMarcin Wojtas 	irq_cause &= IRQ_TIMER_WD_CLR;
253789bbd4dSMarcin Wojtas 	write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
254d100eecfSMarcin Wojtas 
255d100eecfSMarcin Wojtas 	mv_wdt_enable_armada_38x_xp_helper();
256d100eecfSMarcin Wojtas 
257d100eecfSMarcin Wojtas 	val = mv_get_timer_control();
258d100eecfSMarcin Wojtas 	val |= CPU_TIMER_WD_EN | CPU_TIMER_WD_AUTO | CPU_TIMER_WD_25MHZ_EN;
25992ce47d9SZbigniew Bodek 	mv_set_timer_control(val);
26092ce47d9SZbigniew Bodek }
26192ce47d9SZbigniew Bodek 
26292ce47d9SZbigniew Bodek static void
mv_wdt_enable_armada_xp(void)263d100eecfSMarcin Wojtas mv_wdt_enable_armada_xp(void)
26492ce47d9SZbigniew Bodek {
265789bbd4dSMarcin Wojtas 	uint32_t val, irq_cause;
266789bbd4dSMarcin Wojtas 	irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE_ARMADAXP);
267789bbd4dSMarcin Wojtas 	irq_cause &= IRQ_TIMER_WD_CLR_ARMADAXP;
268789bbd4dSMarcin Wojtas 	write_cpu_ctrl(BRIDGE_IRQ_CAUSE_ARMADAXP, irq_cause);
269d100eecfSMarcin Wojtas 
270d100eecfSMarcin Wojtas 	mv_wdt_enable_armada_38x_xp_helper();
27192ce47d9SZbigniew Bodek 
27292ce47d9SZbigniew Bodek 	val = mv_get_timer_control();
273d100eecfSMarcin Wojtas 	val |= CPU_TIMER2_EN | CPU_TIMER2_AUTO | CPU_TIMER_WD_25MHZ_EN;
274d100eecfSMarcin Wojtas 	mv_set_timer_control(val);
275d100eecfSMarcin Wojtas }
276d100eecfSMarcin Wojtas 
277d100eecfSMarcin Wojtas static void
mv_wdt_disable_armv5(void)278d100eecfSMarcin Wojtas mv_wdt_disable_armv5(void)
279d100eecfSMarcin Wojtas {
280d100eecfSMarcin Wojtas 	uint32_t val, irq_cause, irq_mask;
281d100eecfSMarcin Wojtas 
282d100eecfSMarcin Wojtas 	val = mv_get_timer_control();
28392ce47d9SZbigniew Bodek 	val &= ~(CPU_TIMER2_EN | CPU_TIMER2_AUTO);
28492ce47d9SZbigniew Bodek 	mv_set_timer_control(val);
28592ce47d9SZbigniew Bodek 
28692ce47d9SZbigniew Bodek 	val = read_cpu_ctrl(RSTOUTn_MASK);
28792ce47d9SZbigniew Bodek 	val &= ~WD_RST_OUT_EN;
28892ce47d9SZbigniew Bodek 	write_cpu_ctrl(RSTOUTn_MASK, val);
28992ce47d9SZbigniew Bodek 
29092ce47d9SZbigniew Bodek 	irq_mask = read_cpu_ctrl(BRIDGE_IRQ_MASK);
29192ce47d9SZbigniew Bodek 	irq_mask &= ~(IRQ_TIMER_WD_MASK);
29292ce47d9SZbigniew Bodek 	write_cpu_ctrl(BRIDGE_IRQ_MASK, irq_mask);
29392ce47d9SZbigniew Bodek 
29492ce47d9SZbigniew Bodek 	irq_cause = read_cpu_ctrl(BRIDGE_IRQ_CAUSE);
29592ce47d9SZbigniew Bodek 	irq_cause &= IRQ_TIMER_WD_CLR;
29692ce47d9SZbigniew Bodek 	write_cpu_ctrl(BRIDGE_IRQ_CAUSE, irq_cause);
29792ce47d9SZbigniew Bodek }
29892ce47d9SZbigniew Bodek 
299d100eecfSMarcin Wojtas static __inline void
mv_wdt_disable_armada_38x_xp_helper(void)300d100eecfSMarcin Wojtas mv_wdt_disable_armada_38x_xp_helper(void)
301d100eecfSMarcin Wojtas {
302d100eecfSMarcin Wojtas 	uint32_t val;
303d100eecfSMarcin Wojtas 
304d100eecfSMarcin Wojtas 	val = read_cpu_mp_clocks(WD_RSTOUTn_MASK);
305d100eecfSMarcin Wojtas 	val &= ~(WD_GLOBAL_MASK | WD_CPU0_MASK);
306d100eecfSMarcin Wojtas 	write_cpu_mp_clocks(WD_RSTOUTn_MASK, val);
307d100eecfSMarcin Wojtas 
30804bb9a66SMarcin Wojtas 	val = read_cpu_misc(RSTOUTn_MASK_ARMV7);
309d100eecfSMarcin Wojtas 	val |= RSTOUTn_MASK_WD;
31004bb9a66SMarcin Wojtas 	write_cpu_misc(RSTOUTn_MASK_ARMV7, RSTOUTn_MASK_WD);
311d100eecfSMarcin Wojtas }
312d100eecfSMarcin Wojtas 
313d100eecfSMarcin Wojtas static void
mv_wdt_disable_armada_38x(void)314d100eecfSMarcin Wojtas mv_wdt_disable_armada_38x(void)
315d100eecfSMarcin Wojtas {
316d100eecfSMarcin Wojtas 	uint32_t val;
317d100eecfSMarcin Wojtas 
318d100eecfSMarcin Wojtas 	val = mv_get_timer_control();
319d100eecfSMarcin Wojtas 	val &= ~(CPU_TIMER_WD_EN | CPU_TIMER_WD_AUTO);
320d100eecfSMarcin Wojtas 	mv_set_timer_control(val);
321d100eecfSMarcin Wojtas 
322d100eecfSMarcin Wojtas 	mv_wdt_disable_armada_38x_xp_helper();
323d100eecfSMarcin Wojtas }
324d100eecfSMarcin Wojtas 
325d100eecfSMarcin Wojtas static void
mv_wdt_disable_armada_xp(void)326d100eecfSMarcin Wojtas mv_wdt_disable_armada_xp(void)
327d100eecfSMarcin Wojtas {
328d100eecfSMarcin Wojtas 	uint32_t val;
329d100eecfSMarcin Wojtas 
330d100eecfSMarcin Wojtas 	val = mv_get_timer_control();
331d100eecfSMarcin Wojtas 	val &= ~(CPU_TIMER2_EN | CPU_TIMER2_AUTO);
332d100eecfSMarcin Wojtas 	mv_set_timer_control(val);
333d100eecfSMarcin Wojtas 
334d100eecfSMarcin Wojtas 	mv_wdt_disable_armada_38x_xp_helper();
335d100eecfSMarcin Wojtas }
336d100eecfSMarcin Wojtas 
33792ce47d9SZbigniew Bodek /*
33892ce47d9SZbigniew Bodek  * Watchdog event handler.
33992ce47d9SZbigniew Bodek  */
34092ce47d9SZbigniew Bodek static void
mv_watchdog_event(void * arg,unsigned int cmd,int * error)34192ce47d9SZbigniew Bodek mv_watchdog_event(void *arg, unsigned int cmd, int *error)
34292ce47d9SZbigniew Bodek {
34392ce47d9SZbigniew Bodek 	struct mv_wdt_softc *sc;
34492ce47d9SZbigniew Bodek 	uint64_t ns;
34592ce47d9SZbigniew Bodek 	uint64_t ticks;
34692ce47d9SZbigniew Bodek 
34792ce47d9SZbigniew Bodek 	sc = arg;
34892ce47d9SZbigniew Bodek 	mtx_lock(&sc->wdt_mtx);
349d100eecfSMarcin Wojtas 	if (cmd == 0) {
350d100eecfSMarcin Wojtas 		if (wdt_softc->wdt_config->wdt_disable != NULL)
351d100eecfSMarcin Wojtas 			wdt_softc->wdt_config->wdt_disable();
352d100eecfSMarcin Wojtas 	} else {
35392ce47d9SZbigniew Bodek 		/*
35492ce47d9SZbigniew Bodek 		 * Watchdog timeout is in nanosecs, calculation according to
35592ce47d9SZbigniew Bodek 		 * watchdog(9)
35692ce47d9SZbigniew Bodek 		 */
35792ce47d9SZbigniew Bodek 		ns = (uint64_t)1 << (cmd & WD_INTERVAL);
358d100eecfSMarcin Wojtas 		ticks = (uint64_t)(ns * sc->wdt_config->wdt_clock_src) / 1000000000;
359d100eecfSMarcin Wojtas 		if (ticks > MAX_WATCHDOG_TICKS) {
360d100eecfSMarcin Wojtas 			if (wdt_softc->wdt_config->wdt_disable != NULL)
361d100eecfSMarcin Wojtas 				wdt_softc->wdt_config->wdt_disable();
362d100eecfSMarcin Wojtas 		}
36392ce47d9SZbigniew Bodek 		else {
364d100eecfSMarcin Wojtas 			mv_set_timer(wdt_softc->wdt_config->wdt_timer, ticks);
365d100eecfSMarcin Wojtas 			if (wdt_softc->wdt_config->wdt_enable != NULL)
366d100eecfSMarcin Wojtas 				wdt_softc->wdt_config->wdt_enable();
36792ce47d9SZbigniew Bodek 			*error = 0;
36892ce47d9SZbigniew Bodek 		}
36992ce47d9SZbigniew Bodek 	}
37092ce47d9SZbigniew Bodek 	mtx_unlock(&sc->wdt_mtx);
37192ce47d9SZbigniew Bodek }
372