xref: /openbsd/sys/dev/pci/wdt.c (revision fc61954a)
1 /*	$OpenBSD: wdt.c,v 1.22 2014/12/10 12:27:57 mikeb Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998,1999 Alex Nash
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 
36 #include <machine/bus.h>
37 
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcidevs.h>
41 
42 struct wdt_softc {
43 	/* wdt_dev must be the first item in the struct */
44 	struct device		sc_dev;
45 
46 	/* feature set: 0 = none   1 = temp, buzzer, etc. */
47 	int			sc_features;
48 
49 	/* device access through bus space */
50 	bus_space_tag_t		sc_iot;
51 	bus_space_handle_t	sc_ioh;
52 };
53 
54 int	wdt_probe(struct device *, void *, void *);
55 void	wdt_attach(struct device *, struct device *, void *);
56 int	wdt_activate(struct device *, int);
57 
58 int	wdt_is501(struct wdt_softc *);
59 void	wdt_8254_count(struct wdt_softc *, int, u_int16_t);
60 void	wdt_8254_mode(struct wdt_softc *, int, int);
61 int	wdt_set_timeout(void *, int);
62 void	wdt_init_timer(struct wdt_softc *);
63 void	wdt_buzzer_off(struct wdt_softc *);
64 void	wdt_timer_disable(struct wdt_softc *);
65 void	wdt_buzzer_enable(struct wdt_softc *);
66 
67 struct cfattach wdt_ca = {
68 	sizeof(struct wdt_softc), wdt_probe, wdt_attach,
69 	NULL, wdt_activate
70 };
71 
72 struct cfdriver wdt_cd = {
73 	NULL, "wdt", DV_DULL
74 };
75 
76 const struct pci_matchid wdt_devices[] = {
77 	{ PCI_VENDOR_INDCOMPSRC, PCI_PRODUCT_INDCOMPSRC_WDT50x }
78 };
79 
80 /*
81  *	8254 counter mappings
82  */
83 #define WDT_8254_TC_LO		0	/* low 16 bits of timeout counter  */
84 #define	WDT_8254_TC_HI		1	/* high 16 bits of timeout counter */
85 #define WDT_8254_BUZZER		2
86 
87 /*
88  *	WDT500/501 ports
89  */
90 #define WDT_8254_BASE		0
91 #define WDT_8254_CTL		(WDT_8254_BASE + 3)
92 #define WDT_DISABLE_TIMER	7
93 #define WDT_ENABLE_TIMER	7
94 
95 /*
96  *	WDT501 specific ports
97  */
98 #define WDT_STATUS_REG		4
99 #define WDT_START_BUZZER	4
100 #define WDT_TEMPERATURE		5
101 #define WDT_STOP_BUZZER		5
102 
103 int
104 wdt_probe(struct device *parent, void *match, void *aux)
105 {
106 	return (pci_matchbyid((struct pci_attach_args *)aux, wdt_devices,
107 	    nitems(wdt_devices)));
108 }
109 
110 void
111 wdt_attach(struct device *parent, struct device *self, void *aux)
112 {
113 	struct wdt_softc *wdt = (struct wdt_softc *)self;
114 	struct pci_attach_args *const pa = (struct pci_attach_args *)aux;
115 	bus_size_t iosize;
116 
117 	/* retrieve the I/O region (BAR2) */
118 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
119 	    &wdt->sc_iot, &wdt->sc_ioh, NULL, &iosize, 0) != 0) {
120 		printf("%s: couldn't find PCI I/O region\n",
121 		    wdt->sc_dev.dv_xname);
122 		return;
123 	}
124 
125 	/* sanity check I/O size */
126 	if (iosize != (bus_size_t)16) {
127 		printf("%s: invalid I/O region size\n",
128 		    wdt->sc_dev.dv_xname);
129 		return;
130 	}
131 
132 	/* initialize the watchdog timer structure */
133 
134 	/* check the feature set available */
135 	if (wdt_is501(wdt))
136 		wdt->sc_features = 1;
137 	else
138 		wdt->sc_features = 0;
139 
140 	if (wdt->sc_features) {
141 		/*
142 		 * turn off the buzzer, it may have been activated
143 		 * by a previous timeout
144 		 */
145 		wdt_buzzer_off(wdt);
146 
147 		wdt_buzzer_enable(wdt);
148 	}
149 
150 	/* initialize the timer modes and the lower 16-bit counter */
151 	wdt_init_timer(wdt);
152 
153 	/*
154 	 * ensure that the watchdog is disabled
155 	 */
156 	wdt_timer_disable(wdt);
157 
158 	/*
159 	 * register with the watchdog framework
160 	 */
161 	wdog_register(wdt_set_timeout, wdt);
162 }
163 
164 int
165 wdt_activate(struct device *self, int act)
166 {
167 	switch (act) {
168 	case DVACT_POWERDOWN:
169 		wdog_shutdown(self);
170 		break;
171 	}
172 
173 	return (0);
174 }
175 
176 /*
177  *	wdt_is501
178  *
179  *	Returns non-zero if the card is a 501 model.
180  */
181 int
182 wdt_is501(struct wdt_softc *wdt)
183 {
184 	/*
185 	 *	It makes too much sense to detect the card type
186 	 *	by the device ID, so we have to resort to testing
187 	 *	the presence of a register to determine the type.
188 	 */
189 	int v = bus_space_read_1(wdt->sc_iot, wdt->sc_ioh, WDT_TEMPERATURE);
190 
191 	/* XXX may not be reliable */
192 	if (v == 0 || v == 0xFF)
193 		return(0);
194 
195 	return(1);
196 }
197 
198 /*
199  *	wdt_8254_count
200  *
201  *	Loads the specified counter with the 16-bit value 'v'.
202  */
203 void
204 wdt_8254_count(struct wdt_softc *wdt, int counter, u_int16_t v)
205 {
206 	bus_space_write_1(wdt->sc_iot, wdt->sc_ioh,
207 			WDT_8254_BASE + counter, v & 0xFF);
208 	bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_8254_BASE + counter, v >> 8);
209 }
210 
211 /*
212  *	wdt_8254_mode
213  *
214  *	Sets the mode of the specified counter.
215  */
216 void
217 wdt_8254_mode(struct wdt_softc *wdt, int counter, int mode)
218 {
219 	bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_8254_CTL,
220 		(counter << 6) | 0x30 | (mode << 1));
221 }
222 
223 /*
224  *	wdt_set_timeout
225  *
226  *	Load the watchdog timer with the specified number of seconds.
227  *	Clamp seconds to be in the interval [2; 1800].
228  */
229 int
230 wdt_set_timeout(void *self, int seconds)
231 {
232 	struct wdt_softc *wdt = (struct wdt_softc *)self;
233 
234 	u_int16_t v;
235 	int s;
236 
237 	s = splclock();
238 
239 	wdt_timer_disable(wdt);
240 
241 	if (seconds == 0) {
242 		splx(s);
243 		return (0);
244 	} else if (seconds < 2)
245 		seconds = 2;
246 	else if (seconds > 1800)
247 		seconds = 1800;
248 
249 	/* 8254 has been programmed with a 2ms period */
250 	v = (u_int16_t)seconds * 50;
251 
252 	/* load the new timeout count */
253 	wdt_8254_count(wdt, WDT_8254_TC_HI, v);
254 
255 	/* enable the timer */
256 	bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_ENABLE_TIMER, 0);
257 
258 	splx(s);
259 
260 	return (seconds);
261 }
262 
263 /*
264  *	wdt_timer_disable
265  *
266  *	Disables the watchdog timer and cancels the scheduled (if any)
267  *	kernel timeout.
268  */
269 void
270 wdt_timer_disable(struct wdt_softc *wdt)
271 {
272 	(void)bus_space_read_1(wdt->sc_iot, wdt->sc_ioh, WDT_DISABLE_TIMER);
273 }
274 
275 /*
276  *	wdt_init_timer
277  *
278  *	Configure the modes for the watchdog counters and initialize
279  *	the low 16-bits of the watchdog counter to have a period of
280  *	approximately 1/50th of a second.
281  */
282 void
283 wdt_init_timer(struct wdt_softc *wdt)
284 {
285 	wdt_8254_mode(wdt, WDT_8254_TC_LO, 3);
286 	wdt_8254_mode(wdt, WDT_8254_TC_HI, 2);
287 	wdt_8254_count(wdt, WDT_8254_TC_LO, 41666);
288 }
289 
290 /*******************************************************************
291  *	WDT501 specific functions
292  *******************************************************************/
293 
294 /*
295  *	wdt_buzzer_off
296  *
297  *	Turns the buzzer off.
298  */
299 void
300 wdt_buzzer_off(struct wdt_softc *wdt)
301 {
302 	bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_STOP_BUZZER, 0);
303 }
304 
305 #ifndef WDT_DISABLE_BUZZER
306 /*
307  *	wdt_buzzer_enable
308  *
309  *	Enables the buzzer when the watchdog counter expires.
310  */
311 void
312 wdt_buzzer_enable(struct wdt_softc *wdt)
313 {
314 	bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_8254_BUZZER, 1);
315 	wdt_8254_mode(wdt, WDT_8254_BUZZER, 1);
316 }
317 #else
318 /*
319  *	wdt_buzzer_disable
320  *
321  *	Disables the buzzer from sounding when the watchdog counter
322  *	expires.
323  */
324 void
325 wdt_buzzer_disable(struct wdt_softc *wdt)
326 {
327 	wdt_8254_mode(wdt, WDT_8254_BUZZER, 0);
328 }
329 #endif
330