xref: /netbsd/sys/arch/evbmips/atheros/wdog.c (revision 6550d01e)
1 /* $NetBSD: wdog.c,v 1.5 2008/02/11 20:27:01 dyoung Exp $ */
2 /*-
3  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
4  * Copyright (c) 2006 Garrett D'Amore.
5  * All rights reserved.
6  *
7  * Portions of this code were written by Garrett D'Amore for the
8  * Champaign-Urbana Community Wireless Network Project.
9  *
10  * Redistribution and use in source and binary forms, with or
11  * without modification, are permitted provided that the following
12  * conditions 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
16  *    copyright notice, this list of conditions and the following
17  *    disclaimer in the documentation and/or other materials provided
18  *    with the distribution.
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgements:
21  *      This product includes software developed by the Urbana-Champaign
22  *      Independent Media Center.
23  *	This product includes software developed by Garrett D'Amore.
24  * 4. Urbana-Champaign Independent Media Center's name and Garrett
25  *    D'Amore's name may not be used to endorse or promote products
26  *    derived from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
29  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
33  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  */
42 /*
43  * Copyright (c) 2002 Wasabi Systems, Inc.
44  * All rights reserved.
45  *
46  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. All advertising materials mentioning features or use of this software
57  *    must display the following acknowledgement:
58  *	This product includes software developed for the NetBSD Project by
59  *	Wasabi Systems, Inc.
60  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
61  *    or promote products derived from this software without specific prior
62  *    written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
66  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
68  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
71  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
72  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
74  * POSSIBILITY OF SUCH DAMAGE.
75  */
76 
77 /*
78  * Watchdog timer support for the AR531x.
79  */
80 
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.5 2008/02/11 20:27:01 dyoung Exp $");
83 
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/device.h>
87 #include <sys/wdog.h>
88 
89 #include <machine/cpu.h>
90 
91 #include <mips/atheros/include/ar531xvar.h>
92 
93 #include <dev/sysmon/sysmonvar.h>
94 
95 #ifndef	WDOG_DEFAULT_PERIOD
96 #define	WDOG_DEFAULT_PERIOD	5
97 #endif
98 
99 static int wdog_match(device_t, struct cfdata *, void *);
100 static void wdog_attach(device_t, device_t, void *);
101 static int wdog_tickle(struct sysmon_wdog *);
102 static int wdog_setmode(struct sysmon_wdog *);
103 
104 struct wdog_softc {
105 	struct sysmon_wdog	sc_smw;
106 	int			sc_wdog_period;
107 	int			sc_wdog_max;
108 	uint32_t		sc_wdog_reload;
109 	uint32_t		sc_mult;
110 };
111 
112 CFATTACH_DECL_NEW(wdog, sizeof(struct wdog_softc),
113     wdog_match, wdog_attach, NULL, NULL);
114 
115 static int
116 wdog_match(device_t parent, struct cfdata *cf, void *aux)
117 {
118 
119 	return (1);
120 }
121 
122 static void
123 wdog_attach(device_t parent, device_t self, void *aux)
124 {
125 	struct wdog_softc *sc = device_private(self);
126 
127 	sc->sc_mult = ar531x_bus_freq();
128 	sc->sc_wdog_period = WDOG_DEFAULT_PERIOD;
129 	sc->sc_wdog_max = 0xffffffffU / sc->sc_mult;
130 	sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
131 	aprint_normal(": %d second period\n", sc->sc_wdog_period);
132 
133 	sc->sc_smw.smw_name = device_xname(self);
134 	sc->sc_smw.smw_cookie = sc;
135 	sc->sc_smw.smw_setmode = wdog_setmode;
136 	sc->sc_smw.smw_tickle = wdog_tickle;
137 	sc->sc_smw.smw_period = sc->sc_wdog_period;
138 
139 	if (sysmon_wdog_register(&sc->sc_smw) != 0) {
140 		aprint_error_dev(self,
141 		    "unable to register with sysmon\n");
142 	}
143 }
144 
145 static int
146 wdog_tickle(struct sysmon_wdog *smw)
147 {
148 	struct wdog_softc *sc = smw->smw_cookie;
149 
150 	ar531x_wdog(sc->sc_wdog_reload);
151 	return (0);
152 }
153 
154 static int
155 wdog_setmode(struct sysmon_wdog *smw)
156 {
157 	struct wdog_softc *sc = smw->smw_cookie;
158 
159 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
160 		ar531x_wdog(0);
161 	} else {
162 
163 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
164 			smw->smw_period = sc->sc_wdog_period;
165 		else if (smw->smw_period != sc->sc_wdog_period) {
166 			/*
167 			 * we can't set watchdog periods in excess of
168 			 * the processor maximum.
169 			 */
170 			if (smw->smw_period > sc->sc_wdog_max) {
171 				return EOPNOTSUPP;
172 			}
173 			sc->sc_wdog_period = smw->smw_period;
174 			sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
175 		}
176 
177 		ar531x_wdog(sc->sc_wdog_reload);
178 	}
179 
180 	return (0);
181 }
182