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