xref: /netbsd/sys/dev/isa/weasel_isa.c (revision 4bf9ea45)
1*4bf9ea45Schristos /*	$NetBSD: weasel_isa.c,v 1.6 2015/08/20 14:40:18 christos Exp $	*/
2e53aabceSthorpej 
3e53aabceSthorpej /*-
4e53aabceSthorpej  * Copyright (c) 2000 Zembu Labs, Inc.
5e53aabceSthorpej  * All rights reserved.
6e53aabceSthorpej  *
7e53aabceSthorpej  * Author: Jason R. Thorpe <thorpej@zembu.com>
8e53aabceSthorpej  *
9e53aabceSthorpej  * Redistribution and use in source and binary forms, with or without
10e53aabceSthorpej  * modification, are permitted provided that the following conditions
11e53aabceSthorpej  * are met:
12e53aabceSthorpej  * 1. Redistributions of source code must retain the above copyright
13e53aabceSthorpej  *    notice, this list of conditions and the following disclaimer.
14e53aabceSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
15e53aabceSthorpej  *    notice, this list of conditions and the following disclaimer in the
16e53aabceSthorpej  *    documentation and/or other materials provided with the distribution.
17e53aabceSthorpej  * 3. All advertising materials mentioning features or use of this software
18e53aabceSthorpej  *    must display the following acknowledgement:
19e53aabceSthorpej  *	This product includes software developed by Zembu Labs, Inc.
20e53aabceSthorpej  * 4. Neither the name of Zembu Labs nor the names of its employees may
21e53aabceSthorpej  *    be used to endorse or promote products derived from this software
22e53aabceSthorpej  *    without specific prior written permission.
23e53aabceSthorpej  *
24e53aabceSthorpej  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
25e53aabceSthorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
26e53aabceSthorpej  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
27e53aabceSthorpej  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
28e53aabceSthorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29e53aabceSthorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30e53aabceSthorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31e53aabceSthorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32e53aabceSthorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33e53aabceSthorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34e53aabceSthorpej  */
35e53aabceSthorpej 
36e53aabceSthorpej /*
37e53aabceSthorpej  * Device driver for the Middle Digital, Inc. PC-Weasel serial
38e53aabceSthorpej  * console board.
39e53aabceSthorpej  *
40e53aabceSthorpej  * We're glued into the MDA display driver (`pcdisplay'), and
41e53aabceSthorpej  * handle things like the watchdog timer.
42e53aabceSthorpej  */
43e53aabceSthorpej 
44e53aabceSthorpej #include <sys/cdefs.h>
45*4bf9ea45Schristos __KERNEL_RCSID(0, "$NetBSD: weasel_isa.c,v 1.6 2015/08/20 14:40:18 christos Exp $");
46e53aabceSthorpej 
47e53aabceSthorpej #include <sys/param.h>
48e53aabceSthorpej #include <sys/systm.h>
49e53aabceSthorpej #include <sys/device.h>
50e53aabceSthorpej #include <sys/wdog.h>
51e53aabceSthorpej 
52a2a38285Sad #include <sys/bus.h>
53e53aabceSthorpej 
54e53aabceSthorpej #include <dev/isa/weaselreg.h>
55e53aabceSthorpej #include <dev/isa/weaselvar.h>
56e53aabceSthorpej 
57e53aabceSthorpej #include <dev/sysmon/sysmonvar.h>
58e53aabceSthorpej 
59*4bf9ea45Schristos #include "ioconf.h"
60*4bf9ea45Schristos 
61e53aabceSthorpej int	weasel_isa_wdog_setmode(struct sysmon_wdog *);
62e53aabceSthorpej int	weasel_isa_wdog_tickle(struct sysmon_wdog *);
63e53aabceSthorpej int	weasel_isa_wdog_arm_disarm(struct weasel_handle *, u_int8_t);
64e53aabceSthorpej int	weasel_isa_wdog_query_state(struct weasel_handle *);
65e53aabceSthorpej 
66e53aabceSthorpej 
67e53aabceSthorpej /* ARGSUSED */
68e53aabceSthorpej void
pcweaselattach(int count)69e53aabceSthorpej pcweaselattach(int count)
70e53aabceSthorpej {
71e53aabceSthorpej 
72e53aabceSthorpej 	/* Nothing to do; pseudo-device glue. */
73e53aabceSthorpej }
74e53aabceSthorpej 
75e53aabceSthorpej void
weasel_isa_init(struct weasel_handle * wh)76e53aabceSthorpej weasel_isa_init(struct weasel_handle *wh)
77e53aabceSthorpej {
78e53aabceSthorpej 	struct weasel_config_block cfg;
79e53aabceSthorpej 	int i, j;
80e53aabceSthorpej 	u_int8_t *cp, sum;
81e53aabceSthorpej 	const char *vers, *mode;
82e53aabceSthorpej 
83e53aabceSthorpej 	/*
84e53aabceSthorpej 	 * Write a NOP to the command register and see if it
85e53aabceSthorpej 	 * reverts back to READY within 1.5 seconds.
86e53aabceSthorpej 	 */
87e53aabceSthorpej 	bus_space_write_1(wh->wh_st, wh->wh_sh, WEASEL_MISC_COMMAND, OS_NOP);
88e53aabceSthorpej 	for (i = 0; i < 1500; i++) {
89e53aabceSthorpej 		delay(1000);
90e53aabceSthorpej 		sum = bus_space_read_1(wh->wh_st, wh->wh_sh,
91e53aabceSthorpej 		    WEASEL_MISC_COMMAND);
92e53aabceSthorpej 		if (sum == OS_READY)
93e53aabceSthorpej 			break;
94e53aabceSthorpej 	}
95e53aabceSthorpej 	if (sum != OS_READY) {
96e53aabceSthorpej 		/* This is not a Weasel. */
97e53aabceSthorpej 		return;
98e53aabceSthorpej 	}
99e53aabceSthorpej 
100e53aabceSthorpej 	/*
101e53aabceSthorpej 	 * It can take a while for the config block to be copied
102e53aabceSthorpej 	 * into the offscreen area, as the Weasel may be busy
103e53aabceSthorpej 	 * sending data to the terminal.  Wait up to 3 seconds,
104e53aabceSthorpej 	 * reading the block each time, and breaking out of the
105e53aabceSthorpej 	 * loop once the checksum passes.
106e53aabceSthorpej 	 */
107e53aabceSthorpej 
108e53aabceSthorpej 	bus_space_write_1(wh->wh_st, wh->wh_sh, WEASEL_MISC_COMMAND,
109e53aabceSthorpej 	    OS_CONFIG_COPY);
110e53aabceSthorpej 
111e53aabceSthorpej 	/* ...one second to get it started... */
112e53aabceSthorpej 	delay(1000 * 1000);
113e53aabceSthorpej 
114e53aabceSthorpej 	/* ...two seconds to let it finish... */
115e53aabceSthorpej 	for (i = 0; i < 2000; i++) {
116e53aabceSthorpej 		delay(1000);
117e53aabceSthorpej 		bus_space_read_region_1(wh->wh_st, wh->wh_sh,
118e53aabceSthorpej 		    WEASEL_CONFIG_BLOCK, &cfg, sizeof(cfg));
119e53aabceSthorpej 		/*
120e53aabceSthorpej 		 * Compute the checksum of the config block.
121e53aabceSthorpej 		 */
122e53aabceSthorpej 		for (cp = (u_int8_t *)&cfg, j = 0, sum = 1;
123e53aabceSthorpej 		     j < (sizeof(cfg) - 1); j++)
124e53aabceSthorpej 			sum += cp[j];
125e53aabceSthorpej 		if (sum == cfg.cksum)
126e53aabceSthorpej 			break;
127e53aabceSthorpej 	}
128e53aabceSthorpej 
129e53aabceSthorpej 	if (sum != cfg.cksum) {
130e53aabceSthorpej 		/*
131e53aabceSthorpej 		 * Checksum doesn't match; either it's not a Weasel,
132e53aabceSthorpej 		 * or something is wrong with it.
133e53aabceSthorpej 		 */
134e53aabceSthorpej 		printf("%s: PC-Weasel config block checksum mismatch "
135cf417aadScegger 		    "0x%02x != 0x%02x\n", device_xname(wh->wh_parent),
136e53aabceSthorpej 		    sum, cfg.cksum);
137e53aabceSthorpej 		return;
138e53aabceSthorpej 	}
139e53aabceSthorpej 
140e53aabceSthorpej 	switch (cfg.cfg_version) {
141e53aabceSthorpej 	case CFG_VERSION_1_0:
142e53aabceSthorpej 		vers = "1.0";
143e53aabceSthorpej 		switch (cfg.enable_duart_switching) {
144e53aabceSthorpej 		case 0:
145e53aabceSthorpej 			mode = "emulation";
146e53aabceSthorpej 			break;
147e53aabceSthorpej 
148e53aabceSthorpej 		case 1:
149e53aabceSthorpej 			mode = "autoswitch";
150e53aabceSthorpej 			break;
151e53aabceSthorpej 
152e53aabceSthorpej 		default:
153e53aabceSthorpej 			mode = "unknown";
154e53aabceSthorpej 		}
155e53aabceSthorpej 		break;
156e53aabceSthorpej 
157e53aabceSthorpej 	case CFG_VERSION_1_1:
158e53aabceSthorpej 		vers = "1.1";
159e53aabceSthorpej 		switch (cfg.enable_duart_switching) {
160e53aabceSthorpej 		case 0:
161e53aabceSthorpej 			mode = "emulation";
162e53aabceSthorpej 			break;
163e53aabceSthorpej 
164e53aabceSthorpej 		case 1:
165e53aabceSthorpej 			mode = "serial";
166e53aabceSthorpej 			break;
167e53aabceSthorpej 
168e53aabceSthorpej 		case 2:
169e53aabceSthorpej 			mode = "autoswitch";
170e53aabceSthorpej 			break;
171e53aabceSthorpej 
172e53aabceSthorpej 		default:
173e53aabceSthorpej 			mode = "unknown";
174e53aabceSthorpej 		}
175e53aabceSthorpej 		break;
176e53aabceSthorpej 
177e53aabceSthorpej 	default:
178e53aabceSthorpej 		vers = mode = NULL;
179e53aabceSthorpej 	}
180e53aabceSthorpej 
181cf417aadScegger 	printf("%s: PC-Weasel, ", device_xname(wh->wh_parent));
182e53aabceSthorpej 	if (vers != NULL)
183e53aabceSthorpej 		printf("version %s, %s mode", vers, mode);
184e53aabceSthorpej 	else
185e53aabceSthorpej 		printf("unknown version 0x%x", cfg.cfg_version);
186e53aabceSthorpej 	printf("\n");
187e53aabceSthorpej 
188cf417aadScegger 	printf("%s: break passthrough %s", device_xname(wh->wh_parent),
189e53aabceSthorpej 	    cfg.break_passthru ? "enabled" : "disabled");
190e53aabceSthorpej 	if (cfg.wdt_msec == 0) {
191e53aabceSthorpej 		/*
192e53aabceSthorpej 		 * Old firmware -- these Weasels have
193e53aabceSthorpej 		 * a 3000ms watchdog period.
194e53aabceSthorpej 		 */
195e53aabceSthorpej 		cfg.wdt_msec = 3000;
196e53aabceSthorpej 	}
197e53aabceSthorpej 
198e53aabceSthorpej 	if ((wh->wh_wdog_armed = weasel_isa_wdog_query_state(wh)) == -1)
199e53aabceSthorpej 		wh->wh_wdog_armed = 0;
200e53aabceSthorpej 	wh->wh_wdog_period = cfg.wdt_msec / 1000;
201e53aabceSthorpej 
202e53aabceSthorpej 	printf(", watchdog interval %d sec.\n", wh->wh_wdog_period);
203e53aabceSthorpej 
204e53aabceSthorpej 	/*
205e53aabceSthorpej 	 * Always register the Weasel watchdog timer in case user decides
206e53aabceSthorpej 	 * to set 'allow watchdog' to 'YES' after the machine has booted.
207e53aabceSthorpej 	 */
208e53aabceSthorpej 	wh->wh_smw.smw_name = "weasel";
209e53aabceSthorpej 	wh->wh_smw.smw_cookie = wh;
210e53aabceSthorpej 	wh->wh_smw.smw_setmode = weasel_isa_wdog_setmode;
211e53aabceSthorpej 	wh->wh_smw.smw_tickle = weasel_isa_wdog_tickle;
212e53aabceSthorpej 	wh->wh_smw.smw_period = wh->wh_wdog_period;
213e53aabceSthorpej 
214e53aabceSthorpej 	if (sysmon_wdog_register(&wh->wh_smw) != 0)
215cf417aadScegger 		aprint_error_dev(wh->wh_parent, "unable to register PC-Weasel watchdog "
216cf417aadScegger 		    "with sysmon\n");
217e53aabceSthorpej }
218e53aabceSthorpej 
219e53aabceSthorpej int
weasel_isa_wdog_setmode(struct sysmon_wdog * smw)220e53aabceSthorpej weasel_isa_wdog_setmode(struct sysmon_wdog *smw)
221e53aabceSthorpej {
222e53aabceSthorpej 	struct weasel_handle *wh = smw->smw_cookie;
223e53aabceSthorpej 	int error = 0;
224e53aabceSthorpej 
225e53aabceSthorpej 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
226e53aabceSthorpej 		error = weasel_isa_wdog_arm_disarm(wh, WDT_DISABLE);
227e53aabceSthorpej 	} else {
228e53aabceSthorpej 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
229e53aabceSthorpej 			smw->smw_period = wh->wh_wdog_period;
230e53aabceSthorpej 		else if (smw->smw_period != wh->wh_wdog_period) {
231e53aabceSthorpej 			/* Can't change the period on the Weasel. */
232e53aabceSthorpej 			return (EINVAL);
233e53aabceSthorpej 		}
234e53aabceSthorpej 		error = weasel_isa_wdog_arm_disarm(wh, WDT_ENABLE);
235e53aabceSthorpej 		weasel_isa_wdog_tickle(smw);
236e53aabceSthorpej 	}
237e53aabceSthorpej 
238e53aabceSthorpej 	return (error);
239e53aabceSthorpej }
240e53aabceSthorpej 
241e53aabceSthorpej int
weasel_isa_wdog_tickle(struct sysmon_wdog * smw)242e53aabceSthorpej weasel_isa_wdog_tickle(struct sysmon_wdog *smw)
243e53aabceSthorpej {
244e53aabceSthorpej 	struct weasel_handle *wh = smw->smw_cookie;
245e53aabceSthorpej 	u_int8_t reg;
246e53aabceSthorpej 	int x;
247e53aabceSthorpej 	int s;
248e53aabceSthorpej 	int error = 0;
249e53aabceSthorpej 
250e53aabceSthorpej 	s = splhigh();
251e53aabceSthorpej 	/*
252e53aabceSthorpej 	 * first we tickle the watchdog
253e53aabceSthorpej 	 */
254e53aabceSthorpej 	reg = bus_space_read_1(wh->wh_st, wh->wh_sh, WEASEL_WDT_TICKLE);
255e53aabceSthorpej 	bus_space_write_1(wh->wh_st, wh->wh_sh, WEASEL_WDT_TICKLE, ~reg);
256e53aabceSthorpej 
257e53aabceSthorpej 	/*
258e53aabceSthorpej 	 * then we check to make sure the weasel is still armed. If someone
259e53aabceSthorpej 	 * has rebooted the weasel for whatever reason (firmware update),
260e53aabceSthorpej 	 * then the watchdog timer would no longer be armed and we'd be
261e53aabceSthorpej 	 * servicing nothing. Let the user know that the machine is no
262e53aabceSthorpej 	 * longer being monitored by the weasel.
263e53aabceSthorpej 	 */
264e53aabceSthorpej 	if((x = weasel_isa_wdog_query_state(wh)) == -1)
265e53aabceSthorpej 		error = EIO;
266e53aabceSthorpej 	if (x == 1) {
267e53aabceSthorpej 		error = 0;
268e53aabceSthorpej 	} else {
269cf417aadScegger 		aprint_error_dev(wh->wh_parent, "Watchdog timer disabled on PC/Weasel! Disarming wdog.\n");
270e53aabceSthorpej 		wh->wh_wdog_armed = 0;
271e53aabceSthorpej 		error = 1;
272e53aabceSthorpej 	}
273e53aabceSthorpej 	splx(s);
274e53aabceSthorpej 
275e53aabceSthorpej 	return (error);
276e53aabceSthorpej }
277e53aabceSthorpej 
278e53aabceSthorpej int
weasel_isa_wdog_arm_disarm(struct weasel_handle * wh,u_int8_t mode)279e53aabceSthorpej weasel_isa_wdog_arm_disarm(struct weasel_handle *wh, u_int8_t mode)
280e53aabceSthorpej {
281e53aabceSthorpej 	u_int8_t reg;
282e53aabceSthorpej 	int timeout;
283e53aabceSthorpej 	int s, x;
284e53aabceSthorpej 	int error = 0;
285e53aabceSthorpej 
286e53aabceSthorpej 	s = splhigh();
287e53aabceSthorpej 
288e53aabceSthorpej 	bus_space_write_1(wh->wh_st, wh->wh_sh, WEASEL_WDT_SEMAPHORE,
289e53aabceSthorpej 		WDT_ATTENTION);
290e53aabceSthorpej 	for (timeout = 5000; timeout; timeout--) {
291e53aabceSthorpej 		delay(1500);
292e53aabceSthorpej 		reg = bus_space_read_1(wh->wh_st, wh->wh_sh,
293e53aabceSthorpej 			WEASEL_WDT_SEMAPHORE);
294e53aabceSthorpej 		if (reg == WDT_OK)
295e53aabceSthorpej 			break;
296e53aabceSthorpej 	}
297e53aabceSthorpej 	if (timeout == 0) {
298e53aabceSthorpej 		splx(s);
299e53aabceSthorpej 		return(EIO);
300e53aabceSthorpej 	}
301e53aabceSthorpej 	bus_space_write_1(wh->wh_st, wh->wh_sh, WEASEL_WDT_SEMAPHORE, mode);
302e53aabceSthorpej 	for (timeout = 500 ; timeout; timeout--) {
303e53aabceSthorpej 		delay(1500);
304e53aabceSthorpej 		reg = bus_space_read_1(wh->wh_st, wh->wh_sh,
305e53aabceSthorpej 			WEASEL_WDT_SEMAPHORE);
306e53aabceSthorpej 		if (reg != mode)
307e53aabceSthorpej 			break;
308e53aabceSthorpej 	}
309e53aabceSthorpej 	if (timeout == 0) {
310e53aabceSthorpej 		splx(s);
311e53aabceSthorpej 		return(EIO);
312e53aabceSthorpej 	}
313e53aabceSthorpej 	bus_space_write_1(wh->wh_st, wh->wh_sh, WEASEL_WDT_SEMAPHORE, ~reg);
314e53aabceSthorpej 	for (timeout = 500; timeout; timeout--) {
315e53aabceSthorpej 		delay(1500);
316e53aabceSthorpej 		reg = bus_space_read_1(wh->wh_st, wh->wh_sh,
317e53aabceSthorpej 			WEASEL_WDT_SEMAPHORE);
318e53aabceSthorpej 		if (reg == WDT_OK)
319e53aabceSthorpej 			break;
320e53aabceSthorpej 	}
321e53aabceSthorpej 
322e53aabceSthorpej 	/*
323e53aabceSthorpej 	 * Ensure that the Weasel thinks it's in the same mode we want it to
324e53aabceSthorpej 	 * be in.   EIO if not.
325e53aabceSthorpej 	 */
326e53aabceSthorpej 	x = weasel_isa_wdog_query_state(wh);
327e53aabceSthorpej 	switch (x) {
328e53aabceSthorpej 		case -1:
329e53aabceSthorpej 			error = EIO;
330e53aabceSthorpej 			break;
331e53aabceSthorpej 		case 0:
332e53aabceSthorpej 			if (mode == WDT_DISABLE) {
333e53aabceSthorpej 				wh->wh_wdog_armed = 0;
334e53aabceSthorpej 				error = 0;
335e53aabceSthorpej 			} else
336e53aabceSthorpej 				error = EIO;
337e53aabceSthorpej 			break;
338e53aabceSthorpej 		case 1:
339e53aabceSthorpej 			if (mode == WDT_ENABLE) {
340e53aabceSthorpej 				wh->wh_wdog_armed = 1;
341e53aabceSthorpej 				error = 0;
342e53aabceSthorpej 			} else
343e53aabceSthorpej 				error = EIO;
344e53aabceSthorpej 			break;
345e53aabceSthorpej 	}
346e53aabceSthorpej 
347e53aabceSthorpej 	splx(s);
348e53aabceSthorpej 	return(error);
349e53aabceSthorpej }
350e53aabceSthorpej 
351e53aabceSthorpej int
weasel_isa_wdog_query_state(struct weasel_handle * wh)352e53aabceSthorpej weasel_isa_wdog_query_state(struct weasel_handle *wh)
353e53aabceSthorpej {
354e53aabceSthorpej 	int timeout, reg;
355e53aabceSthorpej 
356e53aabceSthorpej 	bus_space_write_1(wh->wh_st, wh->wh_sh,
357e53aabceSthorpej 		WEASEL_MISC_COMMAND, OS_WDT_QUERY);
358e53aabceSthorpej 	for (timeout = 0; timeout < 1500; timeout++) {
359e53aabceSthorpej 		delay(1000);
360e53aabceSthorpej 		reg = bus_space_read_1(wh->wh_st, wh->wh_sh,
361e53aabceSthorpej 			WEASEL_MISC_COMMAND);
362e53aabceSthorpej 		if (reg == OS_READY)
363e53aabceSthorpej 			break;
364e53aabceSthorpej 	}
365e53aabceSthorpej 	return(bus_space_read_1(wh->wh_st, wh->wh_sh, WEASEL_MISC_RESPONSE));
366e53aabceSthorpej }
367