summaryrefslogtreecommitdiff
path: root/sys/dev/pci/weasel_pci.c
blob: 1b6bf4e874bae0f40afafddf84a1d1893d6ba340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*	$NetBSD: weasel_pci.c,v 1.16 2016/07/07 06:55:41 msaitoh Exp $	*/

/*-
 * Copyright (c) 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Herb Peyerl and Jason Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Device driver for the control space on the Middle Digital, Inc.
 * PCI-Weasel serial console board.
 *
 * Since the other functions of the PCI-Weasel already appear in
 * PCI configuration space, we just need to hook up the watchdog
 * timer.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: weasel_pci.c,v 1.16 2016/07/07 06:55:41 msaitoh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/wdog.h>
#include <sys/endian.h>

#include <sys/bus.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>

#include <dev/pci/weaselreg.h>

#include <dev/sysmon/sysmonvar.h>

struct weasel_softc {
	device_t sc_dev;
	bus_space_tag_t sc_st;
	bus_space_handle_t sc_sh;

	struct sysmon_wdog sc_smw;

	int sc_wdog_armed;
	int sc_wdog_period;
};

/* XXX */
extern int	sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);

static int	weasel_pci_wdog_setmode(struct sysmon_wdog *);
static int	weasel_pci_wdog_tickle(struct sysmon_wdog *);

static int	weasel_wait_response(struct weasel_softc *);
static int	weasel_issue_command(struct weasel_softc *, uint8_t cmd);

static int	weasel_pci_wdog_arm(struct weasel_softc *);
static int	weasel_pci_wdog_disarm(struct weasel_softc *);

static int	weasel_pci_wdog_query_state(struct weasel_softc *);

static int
weasel_pci_match(device_t parent, cfdata_t cf, void *aux)
{
	struct pci_attach_args *pa = aux;

	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_MIDDLE_DIGITAL &&
	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_MIDDLE_DIGITAL_WEASEL_CONTROL)
		return (1);

	return (0);
}

static void
weasel_pci_attach(device_t parent, device_t self, void *aux)
{
	struct weasel_softc *sc = device_private(self);
	struct pci_attach_args *pa = aux;
	struct weasel_config_block cfg;
	const char *vers, *mode;
	uint8_t v, *cp;
	uint16_t cfg_size;
	uint8_t buf[8];

	sc->sc_dev = self;

	printf(": PCI-Weasel watchdog timer\n");

	if (pci_mapreg_map(pa, PCI_MAPREG_START,
	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
	    &sc->sc_st, &sc->sc_sh, NULL, NULL) != 0) {
		aprint_error_dev(self, "unable to map device registers\n");
		return;
	}

	/* Ping the Weasel to see if it's alive. */
	if (weasel_issue_command(sc, OS_CMD_PING)) {
		aprint_error_dev(self, "Weasel didn't respond to PING\n");
		return;
	}
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
	if ((v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD)) !=
	    OS_RET_PONG) {
		aprint_error_dev(self,
		    "unexpected PING response from Weasel: 0x%02x\n", v);
		return;
	}

	/* Read the config block. */
	if (weasel_issue_command(sc, OS_CMD_SHOW_CONFIG)) {
		aprint_error_dev(self,
		    "Weasel didn't respond to SHOW_CONFIG\n");
		return;
	}
	cfg_size = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);

	if (++cfg_size != sizeof(cfg)) {
		aprint_error_dev(self,
		    "weird config block size from Weasel: 0x%03x\n", cfg_size);
		return;
	}

	for (cp = (uint8_t *) &cfg; cfg_size != 0; cfg_size--) {
		if (weasel_wait_response(sc)) {
			aprint_error_dev(self,
			    "Weasel stopped providing config block(%d)\n",
			    cfg_size);
			return;
		}
		*cp++ = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
	}

	switch (cfg.cfg_version) {
	case CFG_VERSION_2:
		vers="2";
		switch (cfg.enable_duart_switching) {
		case 0:
			mode = "emulation";
			break;
		case 1:
			mode = "serial";
			break;
		case 2:
			mode = "autoswitch";
			break;
		default:
			mode = "unknown";
		}
		break;

	default:
		vers = mode = NULL;
	}

	if (vers != NULL)
		printf("%s: %s mode\n", device_xname(self), mode);
	else
		printf("%s: unknown config version 0x%02x\n",
		    device_xname(self), cfg.cfg_version);

	/*
	 * Fetch sw version.
	 */
	if (weasel_issue_command(sc, OS_CMD_QUERY_SW_VER)) {
		aprint_error_dev(self,
		    "didn't reply to software version query.\n");
	}
	else {
		v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
		if (v>7)
			printf("%s: weird length for version string(%d).\n",
			    device_xname(self), v);
		memset(buf, 0, sizeof(buf));
		for (cp = buf; v != 0; v--) {
			if (weasel_wait_response(sc)) {
				printf("%s: Weasel stopped providing version\n",
				    device_xname(self));
			}
			*cp++ = bus_space_read_1(sc->sc_st, sc->sc_sh,
			    WEASEL_DATA_RD);
			bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS,
			    0);
		}
		printf("%s: sw: %s", device_xname(self), buf);
	}
	/*
	 * Fetch logic version.
	 */
	if (weasel_issue_command(sc, OS_CMD_QUERY_L_VER)) {
		aprint_normal("\n");
		aprint_error_dev(self,
		    "didn't reply to logic version query.\n");
	}
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
	printf(" logic: %03d", v);
	/*
	 * Fetch vga bios version.
	 */
	if (weasel_issue_command(sc, OS_CMD_QUERY_VB_VER)) {
		aprint_normal("\n");
		aprint_error_dev(self,
		    "didn't reply to vga bios version query.\n");
	}
	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
	printf(" vga bios: %d.%d", (v>>4), (v&0x0f));
	/*
	 * Fetch hw version.
	 */
	if (weasel_issue_command(sc, OS_CMD_QUERY_HW_VER)) {
		aprint_normal("\n");
		aprint_error_dev(self,
		    "didn't reply to hardware version query.\n");
	}
	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
	printf(" hw: %d.%d", (v>>4), (v&0x0f));

	printf("\n%s: break passthrough %s", device_xname(self),
	    cfg.break_passthru ? "enabled" : "disabled");

	if ((sc->sc_wdog_armed = weasel_pci_wdog_query_state(sc)) == -1)
		sc->sc_wdog_armed = 0;

	/* Weasel is big-endian */
	sc->sc_wdog_period = be16toh(cfg.wdt_msec) / 1000;

	printf(", watchdog timer %d sec.\n", sc->sc_wdog_period);
	sc->sc_smw.smw_name = "weasel";
	sc->sc_smw.smw_cookie = sc;
	sc->sc_smw.smw_setmode = weasel_pci_wdog_setmode;
	sc->sc_smw.smw_tickle = weasel_pci_wdog_tickle;
	sc->sc_smw.smw_period = sc->sc_wdog_period;

	if (sysmon_wdog_register(&sc->sc_smw) != 0)
		aprint_error_dev(self, "unable to register PC-Weasel watchdog "
		    "with sysmon\n");
}

CFATTACH_DECL_NEW(weasel_pci, sizeof(struct weasel_softc),
    weasel_pci_match, weasel_pci_attach, NULL, NULL);

static int
weasel_wait_response(struct weasel_softc *sc)
{
	int i;

	for (i = 10000; i ; i--) {
		delay(100);
		if (bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS) ==
		    OS_WS_HOST_READ)
			return(0);
	}
	return (1);
}

static int
weasel_issue_command(struct weasel_softc *sc, uint8_t cmd)
{
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_WR, cmd);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_HOST_STATUS,
	    OS_HS_WEASEL_READ);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
	return (weasel_wait_response(sc));
}

static int
weasel_pci_wdog_setmode(struct sysmon_wdog *smw)
{
	struct weasel_softc *sc = smw->smw_cookie;
	int error = 0;

	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
		error = weasel_pci_wdog_disarm(sc);
	} else {
		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
			smw->smw_period = sc->sc_wdog_period;
		else if (smw->smw_period != sc->sc_wdog_period) {
			/* Can't change the period on the Weasel. */
			return (EINVAL);
		}
		error = weasel_pci_wdog_arm(sc);
		weasel_pci_wdog_tickle(smw);
	}

	return (error);
}

static int
weasel_pci_wdog_tickle(struct sysmon_wdog *smw)
{
	struct weasel_softc *sc = smw->smw_cookie;
	u_int8_t reg;
	int x;
	int s;
	int error = 0;

	s = splhigh();
	/*
	 * first we tickle the watchdog
	 */
	reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_CHALLENGE);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_RESPONSE, ~reg);

	/*
	 * then we check to make sure the weasel is still armed. If someone
	 * has rebooted the weasel for whatever reason (firmware update),
	 * then the watchdog timer would no longer be armed and we'd be
	 * servicing nothing. Let the user know that the machine is no
	 * longer being monitored by the weasel.
	 */
	if((x = weasel_pci_wdog_query_state(sc)) == -1)
		error = EIO;
	if (x == 1) {
		error = 0;
	} else {
		printf("%s: Watchdog timer disabled on PC/Weasel! Disarming wdog.\n",
			device_xname(sc->sc_dev));
		sc->sc_wdog_armed = 0;
		sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED, 0);
		error = 1;
	}
	splx(s);

	return (error);
}

static int
weasel_pci_wdog_arm(struct weasel_softc *sc)
{
	int x;
	int s;
	int error = 0;

	s = splhigh();
	if (weasel_issue_command(sc, OS_CMD_WDT_ENABLE)) {
		printf("%s: no reply to watchdog enable. Check Weasel \"Allow Watchdog\" setting.\n",
			device_xname(sc->sc_dev));
		error = EIO;
	}
	(void)bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);

	/*
	 * Ensure that the Weasel thinks it's in the same mode we want it to
	 * be in.   EIO if not.
	 */
	x = weasel_pci_wdog_query_state(sc);
	switch (x) {
		case -1:
			error = EIO;
			break;
		case 0:
			sc->sc_wdog_armed = 0;
			error = EIO;
			break;
		case 1:
			sc->sc_wdog_armed = 1;
			error = 0;
			break;
	}

	splx(s);
	return(error);
}


static int
weasel_pci_wdog_disarm(struct weasel_softc *sc)
{
	int x;
	int s;
	int error = 0;

	s = splhigh();

	if (weasel_issue_command(sc, OS_CMD_WDT_DISABLE)) {
		printf("%s: didn't reply to watchdog disable.\n",
			device_xname(sc->sc_dev));
		error = EIO;
	}
	(void)bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);

	/*
	 * Ensure that the Weasel thinks it's in the same mode we want it to
	 * be in.   EIO if not.
	 */
	x = weasel_pci_wdog_query_state(sc);
	switch (x) {
		case -1:
			error = EIO;
			break;
		case 0:
			sc->sc_wdog_armed = 0;
			error = 0;
			break;
		case 1:
			sc->sc_wdog_armed = 1;
			error = EIO;
			break;
	}

	splx(s);
	return(error);
}

static int
weasel_pci_wdog_query_state(struct weasel_softc *sc)
{

	u_int8_t v;

	if (weasel_issue_command(sc, OS_CMD_WDT_QUERY)) {
		printf("%s: didn't reply to watchdog state query.\n",
			device_xname(sc->sc_dev));
		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
		return(-1);
	}
	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
	return(v);
}