xref: /netbsd-src/sys/dev/pci/weasel_pci.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: weasel_pci.c,v 1.7 2005/12/11 12:22:51 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Herb Peyerl and Jason Thorpe.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Device driver for the control space on the Middle Digital, Inc.
41  * PCI-Weasel serial console board.
42  *
43  * Since the other functions of the PCI-Weasel already appear in
44  * PCI configuration space, we just need to hook up the watchdog
45  * timer.
46  */
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: weasel_pci.c,v 1.7 2005/12/11 12:22:51 christos Exp $");
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/wdog.h>
55 #include <sys/endian.h>
56 
57 #include <machine/bus.h>
58 
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/pcidevs.h>
62 
63 #include <dev/pci/weaselreg.h>
64 
65 #include <dev/sysmon/sysmonvar.h>
66 
67 struct weasel_softc {
68 	struct device sc_dev;		/* generic device glue */
69 
70 	bus_space_tag_t sc_st;
71 	bus_space_handle_t sc_sh;
72 
73 	struct sysmon_wdog sc_smw;
74 
75 	int sc_wdog_armed;
76 	int sc_wdog_period;
77 };
78 
79 /* XXX */
80 extern int	sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);
81 
82 static int	weasel_pci_wdog_setmode(struct sysmon_wdog *);
83 static int	weasel_pci_wdog_tickle(struct sysmon_wdog *);
84 
85 static int	weasel_wait_response(struct weasel_softc *);
86 static int	weasel_issue_command(struct weasel_softc *, uint8_t cmd);
87 
88 static int	weasel_pci_wdog_arm(struct weasel_softc *);
89 static int	weasel_pci_wdog_disarm(struct weasel_softc *);
90 
91 static int	weasel_pci_wdog_query_state(struct weasel_softc *);
92 
93 static int
94 weasel_pci_match(struct device *parent, struct cfdata *cf, void *aux)
95 {
96 	struct pci_attach_args *pa = aux;
97 
98 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_MIDDLE_DIGITAL &&
99 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_MIDDLE_DIGITAL_WEASEL_CONTROL)
100 		return (1);
101 
102 	return (0);
103 }
104 
105 static void
106 weasel_pci_attach(struct device *parent, struct device *self, void *aux)
107 {
108 	struct weasel_softc *sc = (void *) self;
109 	struct pci_attach_args *pa = aux;
110 	struct weasel_config_block cfg;
111 	const char *vers, *mode;
112 	uint8_t v, *cp;
113 	uint16_t cfg_size;
114 	uint8_t buf[8];
115 
116 	printf(": PCI-Weasel watchdog timer\n");
117 
118 	if (pci_mapreg_map(pa, PCI_MAPREG_START,
119 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
120 	    &sc->sc_st, &sc->sc_sh, NULL, NULL) != 0) {
121 		printf("%s: unable to map device registers\n",
122 		    sc->sc_dev.dv_xname);
123 		return;
124 	}
125 
126 	/* Ping the Weasel to see if it's alive. */
127 	if (weasel_issue_command(sc, OS_CMD_PING)) {
128 		printf("%s: Weasel didn't respond to PING\n",
129 		    sc->sc_dev.dv_xname);
130 		return;
131 	}
132 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
133 	if ((v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD)) !=
134 	    OS_RET_PONG) {
135 		printf("%s: unexpected PING response from Weasel: 0x%02x\n",
136 		    sc->sc_dev.dv_xname, v);
137 		return;
138 	}
139 
140 	/* Read the config block. */
141 	if (weasel_issue_command(sc, OS_CMD_SHOW_CONFIG)) {
142 		printf("%s: Weasel didn't respond to SHOW_CONFIG\n",
143 		    sc->sc_dev.dv_xname);
144 		return;
145 	}
146 	cfg_size = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
147 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
148 
149 	if (++cfg_size != sizeof(cfg)) {
150 		printf("%s: weird config block size from Weasel: 0x%03x\n",
151 		    sc->sc_dev.dv_xname, cfg_size);
152 		return;
153 	}
154 
155 	for (cp = (uint8_t *) &cfg; cfg_size != 0; cfg_size--) {
156 		if (weasel_wait_response(sc)) {
157 			printf("%s: Weasel stopped providing config block(%d)\n",
158 			    sc->sc_dev.dv_xname, cfg_size);
159 			return;
160 		}
161 		*cp++ = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
162 		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
163 	}
164 
165 	switch (cfg.cfg_version) {
166 	case CFG_VERSION_2:
167 		vers="2";
168 		switch (cfg.enable_duart_switching) {
169 		case 0:
170 			mode = "emulation";
171 			break;
172 		case 1:
173 			mode = "serial";
174 			break;
175 		case 2:
176 			mode = "autoswitch";
177 			break;
178 		default:
179 			mode = "unknown";
180 		}
181 		break;
182 
183 	default:
184 		vers = mode = NULL;
185 	}
186 
187 	if (vers != NULL)
188 		printf("%s: %s mode\n", sc->sc_dev.dv_xname,
189 		    mode);
190 	else
191 		printf("%s: unknown config version 0x%02x\n", sc->sc_dev.dv_xname,
192 		    cfg.cfg_version);
193 
194 	/*
195 	 * Fetch sw version.
196 	 */
197 	if (weasel_issue_command(sc, OS_CMD_QUERY_SW_VER)) {
198 		printf("%s: didn't reply to software version query.\n",
199 			sc->sc_dev.dv_xname);
200 	}
201 	else {
202 		v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
203 		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
204 		if (v>7)
205 			printf("%s: weird length for version string(%d).\n",
206 					sc->sc_dev.dv_xname, v);
207 		bzero(buf, sizeof(buf));
208 		for (cp = buf; v != 0; v--) {
209 			if (weasel_wait_response(sc)) {
210 				printf("%s: Weasel stopped providing version\n",
211 				    sc->sc_dev.dv_xname);
212 			}
213 			*cp++ = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
214 			bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
215 		}
216 		printf("%s: sw: %s", sc->sc_dev.dv_xname, buf);
217 	}
218 	/*
219 	 * Fetch logic version.
220 	 */
221 	if (weasel_issue_command(sc, OS_CMD_QUERY_L_VER)) {
222 		printf("\n%s: didn't reply to logic version query.\n",
223 			sc->sc_dev.dv_xname);
224 	}
225 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
226 	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
227 	printf(" logic: %03d", v);
228 	/*
229 	 * Fetch vga bios version.
230 	 */
231 	if (weasel_issue_command(sc, OS_CMD_QUERY_VB_VER)) {
232 		printf("\n%s: didn't reply to vga bios version query.\n",
233 			sc->sc_dev.dv_xname);
234 	}
235 	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
236 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
237 	printf(" vga bios: %d.%d", (v>>4), (v&0x0f));
238 	/*
239 	 * Fetch hw version.
240 	 */
241 	if (weasel_issue_command(sc, OS_CMD_QUERY_HW_VER)) {
242 		printf("\n%s: didn't reply to hardware version query.\n",
243 			sc->sc_dev.dv_xname);
244 	}
245 	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
246 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
247 	printf(" hw: %d.%d", (v>>4), (v&0x0f));
248 
249 	printf("\n%s: break passthrough %s", sc->sc_dev.dv_xname,
250 	    cfg.break_passthru ? "enabled" : "disabled");
251 
252 	if ((sc->sc_wdog_armed = weasel_pci_wdog_query_state(sc)) == -1)
253 		sc->sc_wdog_armed = 0;
254 
255 	/* Weasel is big-endian */
256 	sc->sc_wdog_period = be16toh(cfg.wdt_msec) / 1000;
257 
258 	printf(", watchdog timer %d sec.\n", sc->sc_wdog_period);
259 	sc->sc_smw.smw_name = "weasel";
260 	sc->sc_smw.smw_cookie = sc;
261 	sc->sc_smw.smw_setmode = weasel_pci_wdog_setmode;
262 	sc->sc_smw.smw_tickle = weasel_pci_wdog_tickle;
263 	sc->sc_smw.smw_period = sc->sc_wdog_period;
264 
265 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
266 		printf("%s: unable to register PC-Weasel watchdog "
267 		    "with sysmon\n", sc->sc_dev.dv_xname);
268 }
269 
270 CFATTACH_DECL(weasel_pci, sizeof(struct weasel_softc),
271     weasel_pci_match, weasel_pci_attach, NULL, NULL);
272 
273 static int
274 weasel_wait_response(struct weasel_softc *sc)
275 {
276 	int i;
277 
278 	for (i = 10000; i ; i--) {
279 		delay(100);
280 		if (bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS) ==
281 		    OS_WS_HOST_READ)
282 			return(0);
283 	}
284 	return (1);
285 }
286 
287 static int
288 weasel_issue_command(struct weasel_softc *sc, uint8_t cmd)
289 {
290 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_WR, cmd);
291 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_HOST_STATUS, OS_HS_WEASEL_READ);
292 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
293 	return (weasel_wait_response(sc));
294 }
295 
296 static int
297 weasel_pci_wdog_setmode(struct sysmon_wdog *smw)
298 {
299 	struct weasel_softc *sc = smw->smw_cookie;
300 	int error = 0;
301 
302 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
303 		error = weasel_pci_wdog_disarm(sc);
304 	} else {
305 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
306 			smw->smw_period = sc->sc_wdog_period;
307 		else if (smw->smw_period != sc->sc_wdog_period) {
308 			/* Can't change the period on the Weasel. */
309 			return (EINVAL);
310 		}
311 		error = weasel_pci_wdog_arm(sc);
312 		weasel_pci_wdog_tickle(smw);
313 	}
314 
315 	return (error);
316 }
317 
318 static int
319 weasel_pci_wdog_tickle(struct sysmon_wdog *smw)
320 {
321 	struct weasel_softc *sc = smw->smw_cookie;
322 	u_int8_t reg;
323 	int x;
324 	int s;
325 	int error = 0;
326 
327 	s = splhigh();
328 	/*
329 	 * first we tickle the watchdog
330 	 */
331 	reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_CHALLENGE);
332 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_RESPONSE, ~reg);
333 
334 	/*
335 	 * then we check to make sure the weasel is still armed. If someone
336 	 * has rebooted the weasel for whatever reason (firmware update),
337 	 * then the watchdog timer would no longer be armed and we'd be
338 	 * servicing nothing. Let the user know that the machine is no
339 	 * longer being monitored by the weasel.
340 	 */
341 	if((x = weasel_pci_wdog_query_state(sc)) == -1)
342 		error = EIO;
343 	if (x == 1) {
344 		error = 0;
345 	} else {
346 		printf("%s: Watchdog timer disabled on PC/Weasel! Disarming wdog.\n",
347 			sc->sc_dev.dv_xname);
348 		sc->sc_wdog_armed = 0;
349 		sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED, 0);
350 		error = 1;
351 	}
352 	splx(s);
353 
354 	return (error);
355 }
356 
357 static int
358 weasel_pci_wdog_arm(struct weasel_softc *sc)
359 {
360 	u_int8_t reg;
361 	int x;
362 	int s;
363 	int error = 0;
364 
365 	s = splhigh();
366 	if (weasel_issue_command(sc, OS_CMD_WDT_ENABLE)) {
367 		printf("%s: no reply to watchdog enable. Check Weasel \"Allow Watchdog\" setting.\n",
368 			sc->sc_dev.dv_xname);
369 		error = EIO;
370 	}
371 	reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
372 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
373 
374 	/*
375 	 * Ensure that the Weasel thinks it's in the same mode we want it to
376 	 * be in.   EIO if not.
377 	 */
378 	x = weasel_pci_wdog_query_state(sc);
379 	switch (x) {
380 		case -1:
381 			error = EIO;
382 			break;
383 		case 0:
384 			sc->sc_wdog_armed = 0;
385 			error = EIO;
386 			break;
387 		case 1:
388 			sc->sc_wdog_armed = 1;
389 			error = 0;
390 			break;
391 	}
392 
393 	splx(s);
394 	return(error);
395 }
396 
397 
398 static int
399 weasel_pci_wdog_disarm(struct weasel_softc *sc)
400 {
401 	u_int8_t reg;
402 	int x;
403 	int s;
404 	int error = 0;
405 
406 	s = splhigh();
407 
408 	if (weasel_issue_command(sc, OS_CMD_WDT_DISABLE)) {
409 		printf("%s: didn't reply to watchdog disable.\n",
410 			sc->sc_dev.dv_xname);
411 		error = EIO;
412 	}
413 	reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
414 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
415 
416 	/*
417 	 * Ensure that the Weasel thinks it's in the same mode we want it to
418 	 * be in.   EIO if not.
419 	 */
420 	x = weasel_pci_wdog_query_state(sc);
421 	switch (x) {
422 		case -1:
423 			error = EIO;
424 			break;
425 		case 0:
426 			sc->sc_wdog_armed = 0;
427 			error = 0;
428 			break;
429 		case 1:
430 			sc->sc_wdog_armed = 1;
431 			error = EIO;
432 			break;
433 	}
434 
435 	splx(s);
436 	return(error);
437 }
438 
439 static int
440 weasel_pci_wdog_query_state(struct weasel_softc *sc)
441 {
442 
443 	u_int8_t v;
444 
445 	if (weasel_issue_command(sc, OS_CMD_WDT_QUERY)) {
446 		printf("%s: didn't reply to watchdog state query.\n",
447 			sc->sc_dev.dv_xname);
448 		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
449 		return(-1);
450 	}
451 	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
452 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
453 	return(v);
454 }
455