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