xref: /netbsd-src/sys/arch/arm/xscale/ixp425_wdog.c (revision a2b8c7fb0742c36745ea1fd35822da4277b62383)
1 /*	$NetBSD: ixp425_wdog.c,v 1.4 2012/10/14 14:20:58 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Steve C. Woodford.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "opt_ddb.h"
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ixp425_wdog.c,v 1.4 2012/10/14 14:20:58 msaitoh Exp $");
36 
37 #include <sys/systm.h>
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/wdog.h>
42 
43 #include <dev/clock_subr.h>
44 #include <dev/sysmon/sysmonvar.h>
45 
46 #include <machine/intr.h>
47 #include <sys/bus.h>
48 
49 #include <arm/xscale/ixp425reg.h>
50 #include <arm/xscale/ixp425var.h>
51 #include <arm/xscale/ixp425_sipvar.h>
52 
53 #ifdef DDB
54 #include <machine/db_machdep.h>
55 #include <ddb/db_extern.h>
56 #endif
57 
58 struct ixpdog_softc {
59 	struct sysmon_wdog sc_smw;
60 	bus_space_tag_t sc_bust;
61 	bus_space_handle_t sc_bush;
62 	uint32_t sc_preset;
63 	int sc_armed;
64 };
65 
66 #define	IXPDOG_DEFAULT_PERIOD	8
67 #ifndef	IXP425_CLOCK_FREQ
68 #define	IXPDOG_COUNTS_PER_SEC	66666600
69 #else
70 #define	IXPDOG_COUNTS_PER_SEC	IXP425_CLOCK_FREQ
71 #endif
72 
73 static int ixpdog_match(device_t, cfdata_t, void *);
74 static void ixpdog_attach(device_t, device_t, void *);
75 
76 CFATTACH_DECL_NEW(ixpdog, sizeof(struct ixpdog_softc),
77     ixpdog_match, ixpdog_attach, NULL, NULL);
78 
79 static void ixpdog_control(struct ixpdog_softc *, int);
80 static int ixpdog_tickle(struct sysmon_wdog *);
81 static int ixpdog_setmode(struct sysmon_wdog *);
82 
83 #ifdef DDB
84 static struct ixpdog_softc *ixpdog_softc;
85 static void ixpdog_ddb_trap(int);
86 #endif
87 
88 
89 static int
ixpdog_match(device_t parent,cfdata_t cf,void * aux)90 ixpdog_match(device_t parent, cfdata_t cf, void *aux)
91 {
92 	struct ixpsip_attach_args *sa = aux;
93 
94 	return (sa->sa_addr == IXP425_OST_WDOG_HWBASE);
95 }
96 
97 static void
ixpdog_attach(device_t parent,device_t self,void * aux)98 ixpdog_attach(device_t parent, device_t self, void *aux)
99 {
100 	struct ixpdog_softc *sc = device_private(self);
101 	struct ixpsip_attach_args *sa = aux;
102 
103 	aprint_naive("\n");
104 	aprint_normal(": Watchdog Timer\n");
105 
106 	sc->sc_bust = sa->sa_iot;
107 
108 	if (bus_space_map(sc->sc_bust, sa->sa_addr, IXP425_OST_WDOG_SIZE, 0,
109 	    &sc->sc_bush)) {
110 		aprint_error_dev(self, "Failed to map watchdog registers\n");
111 		return;
112 	}
113 
114 	sc->sc_preset = IXPDOG_COUNTS_PER_SEC * hz * IXPDOG_DEFAULT_PERIOD;
115 	sc->sc_armed = 0;
116 	ixpdog_control(sc, 0);
117 
118 #ifdef DDB
119 	ixpdog_softc = sc;
120 	db_trap_callback = ixpdog_ddb_trap;
121 #endif
122 
123 	sc->sc_smw.smw_name = device_xname(self);
124 	sc->sc_smw.smw_cookie = sc;
125 	sc->sc_smw.smw_setmode = ixpdog_setmode;
126 	sc->sc_smw.smw_tickle = ixpdog_tickle;
127 	sc->sc_smw.smw_period = IXPDOG_DEFAULT_PERIOD;
128 
129 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
130 		aprint_error_dev(self, "unable to register watchdog with sysmon\n");
131 }
132 
133 static void
ixpdog_control(struct ixpdog_softc * sc,int enable)134 ixpdog_control(struct ixpdog_softc *sc, int enable)
135 {
136 	uint32_t reg;
137 	int s;
138 
139 	s = disable_interrupts(I32_bit);
140 	bus_space_write_2(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_KEY,
141 	    OST_WDOG_KEY_MAJICK);
142 	reg = bus_space_read_4(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_ENAB);
143 
144 	if (!enable)
145 		reg &= ~(OST_WDOG_ENAB_CNT_ENA | OST_WDOG_ENAB_RST_ENA);
146 	else {
147 		bus_space_write_4(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG,
148 		    sc->sc_preset);
149 		reg |= OST_WDOG_ENAB_CNT_ENA | OST_WDOG_ENAB_RST_ENA;
150 	}
151 
152 	bus_space_write_4(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_ENAB, reg);
153 	bus_space_write_2(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_KEY,
154 	    ~OST_WDOG_KEY_MAJICK);
155 	restore_interrupts(s);
156 }
157 
158 static int
ixpdog_tickle(struct sysmon_wdog * smw)159 ixpdog_tickle(struct sysmon_wdog *smw)
160 {
161 	struct ixpdog_softc *sc = smw->smw_cookie;
162 	int s;
163 
164 	s = disable_interrupts(I32_bit);
165 	bus_space_write_2(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_KEY,
166 	    OST_WDOG_KEY_MAJICK);
167 	bus_space_write_4(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG,
168 	    sc->sc_preset);
169 	bus_space_write_2(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_KEY,
170 	    ~OST_WDOG_KEY_MAJICK);
171 	restore_interrupts(s);
172 
173 	return (0);
174 }
175 
176 static int
ixpdog_setmode(struct sysmon_wdog * smw)177 ixpdog_setmode(struct sysmon_wdog *smw)
178 {
179 	struct ixpdog_softc *sc = smw->smw_cookie;
180 
181 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED)
182 		sc->sc_armed = 0;
183 	else {
184 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
185 			smw->smw_period = IXPDOG_DEFAULT_PERIOD;
186 		sc->sc_preset = IXPDOG_COUNTS_PER_SEC * hz * smw->smw_period;
187 		if (sc->sc_preset < (IXPDOG_COUNTS_PER_SEC * hz) ||
188 		    sc->sc_preset < smw->smw_period)
189 			return (EOPNOTSUPP);
190 
191 		sc->sc_armed = 1;
192 	}
193 
194 	ixpdog_control(sc, sc->sc_armed);
195 
196 	return (0);
197 }
198 
199 #ifdef DDB
200 static void
ixpdog_ddb_trap(int enter)201 ixpdog_ddb_trap(int enter)
202 {
203 	struct ixpdog_softc *sc;
204 
205 	if ((sc = ixpdog_softc) == NULL)
206 		return;
207 
208 	sc->sc_armed += enter ? (-1) : 1;
209 
210 	if ((enter && sc->sc_armed == 0) || (!enter && sc->sc_armed == 1))
211 		ixpdog_control(sc, sc->sc_armed);
212 }
213 #endif
214