Home | History | Annotate | Line # | Download | only in pci
weasel_pci.c revision 1.10
      1 /*	$NetBSD: weasel_pci.c,v 1.10 2007/10/19 12:00:56 ad 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.10 2007/10/19 12:00:56 ad 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 <sys/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,
     95     void *aux)
     96 {
     97 	struct pci_attach_args *pa = aux;
     98 
     99 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_MIDDLE_DIGITAL &&
    100 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_MIDDLE_DIGITAL_WEASEL_CONTROL)
    101 		return (1);
    102 
    103 	return (0);
    104 }
    105 
    106 static void
    107 weasel_pci_attach(struct device *parent, struct device *self,
    108     void *aux)
    109 {
    110 	struct weasel_softc *sc = (void *) self;
    111 	struct pci_attach_args *pa = aux;
    112 	struct weasel_config_block cfg;
    113 	const char *vers, *mode;
    114 	uint8_t v, *cp;
    115 	uint16_t cfg_size;
    116 	uint8_t buf[8];
    117 
    118 	printf(": PCI-Weasel watchdog timer\n");
    119 
    120 	if (pci_mapreg_map(pa, PCI_MAPREG_START,
    121 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    122 	    &sc->sc_st, &sc->sc_sh, NULL, NULL) != 0) {
    123 		printf("%s: unable to map device registers\n",
    124 		    sc->sc_dev.dv_xname);
    125 		return;
    126 	}
    127 
    128 	/* Ping the Weasel to see if it's alive. */
    129 	if (weasel_issue_command(sc, OS_CMD_PING)) {
    130 		printf("%s: Weasel didn't respond to PING\n",
    131 		    sc->sc_dev.dv_xname);
    132 		return;
    133 	}
    134 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    135 	if ((v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD)) !=
    136 	    OS_RET_PONG) {
    137 		printf("%s: unexpected PING response from Weasel: 0x%02x\n",
    138 		    sc->sc_dev.dv_xname, v);
    139 		return;
    140 	}
    141 
    142 	/* Read the config block. */
    143 	if (weasel_issue_command(sc, OS_CMD_SHOW_CONFIG)) {
    144 		printf("%s: Weasel didn't respond to SHOW_CONFIG\n",
    145 		    sc->sc_dev.dv_xname);
    146 		return;
    147 	}
    148 	cfg_size = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    149 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    150 
    151 	if (++cfg_size != sizeof(cfg)) {
    152 		printf("%s: weird config block size from Weasel: 0x%03x\n",
    153 		    sc->sc_dev.dv_xname, cfg_size);
    154 		return;
    155 	}
    156 
    157 	for (cp = (uint8_t *) &cfg; cfg_size != 0; cfg_size--) {
    158 		if (weasel_wait_response(sc)) {
    159 			printf("%s: Weasel stopped providing config block(%d)\n",
    160 			    sc->sc_dev.dv_xname, cfg_size);
    161 			return;
    162 		}
    163 		*cp++ = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    164 		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    165 	}
    166 
    167 	switch (cfg.cfg_version) {
    168 	case CFG_VERSION_2:
    169 		vers="2";
    170 		switch (cfg.enable_duart_switching) {
    171 		case 0:
    172 			mode = "emulation";
    173 			break;
    174 		case 1:
    175 			mode = "serial";
    176 			break;
    177 		case 2:
    178 			mode = "autoswitch";
    179 			break;
    180 		default:
    181 			mode = "unknown";
    182 		}
    183 		break;
    184 
    185 	default:
    186 		vers = mode = NULL;
    187 	}
    188 
    189 	if (vers != NULL)
    190 		printf("%s: %s mode\n", sc->sc_dev.dv_xname,
    191 		    mode);
    192 	else
    193 		printf("%s: unknown config version 0x%02x\n", sc->sc_dev.dv_xname,
    194 		    cfg.cfg_version);
    195 
    196 	/*
    197 	 * Fetch sw version.
    198 	 */
    199 	if (weasel_issue_command(sc, OS_CMD_QUERY_SW_VER)) {
    200 		printf("%s: didn't reply to software version query.\n",
    201 			sc->sc_dev.dv_xname);
    202 	}
    203 	else {
    204 		v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    205 		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    206 		if (v>7)
    207 			printf("%s: weird length for version string(%d).\n",
    208 					sc->sc_dev.dv_xname, v);
    209 		bzero(buf, sizeof(buf));
    210 		for (cp = buf; v != 0; v--) {
    211 			if (weasel_wait_response(sc)) {
    212 				printf("%s: Weasel stopped providing version\n",
    213 				    sc->sc_dev.dv_xname);
    214 			}
    215 			*cp++ = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    216 			bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    217 		}
    218 		printf("%s: sw: %s", sc->sc_dev.dv_xname, buf);
    219 	}
    220 	/*
    221 	 * Fetch logic version.
    222 	 */
    223 	if (weasel_issue_command(sc, OS_CMD_QUERY_L_VER)) {
    224 		printf("\n%s: didn't reply to logic version query.\n",
    225 			sc->sc_dev.dv_xname);
    226 	}
    227 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    228 	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    229 	printf(" logic: %03d", v);
    230 	/*
    231 	 * Fetch vga bios version.
    232 	 */
    233 	if (weasel_issue_command(sc, OS_CMD_QUERY_VB_VER)) {
    234 		printf("\n%s: didn't reply to vga bios version query.\n",
    235 			sc->sc_dev.dv_xname);
    236 	}
    237 	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    238 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    239 	printf(" vga bios: %d.%d", (v>>4), (v&0x0f));
    240 	/*
    241 	 * Fetch hw version.
    242 	 */
    243 	if (weasel_issue_command(sc, OS_CMD_QUERY_HW_VER)) {
    244 		printf("\n%s: didn't reply to hardware version query.\n",
    245 			sc->sc_dev.dv_xname);
    246 	}
    247 	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    248 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    249 	printf(" hw: %d.%d", (v>>4), (v&0x0f));
    250 
    251 	printf("\n%s: break passthrough %s", sc->sc_dev.dv_xname,
    252 	    cfg.break_passthru ? "enabled" : "disabled");
    253 
    254 	if ((sc->sc_wdog_armed = weasel_pci_wdog_query_state(sc)) == -1)
    255 		sc->sc_wdog_armed = 0;
    256 
    257 	/* Weasel is big-endian */
    258 	sc->sc_wdog_period = be16toh(cfg.wdt_msec) / 1000;
    259 
    260 	printf(", watchdog timer %d sec.\n", sc->sc_wdog_period);
    261 	sc->sc_smw.smw_name = "weasel";
    262 	sc->sc_smw.smw_cookie = sc;
    263 	sc->sc_smw.smw_setmode = weasel_pci_wdog_setmode;
    264 	sc->sc_smw.smw_tickle = weasel_pci_wdog_tickle;
    265 	sc->sc_smw.smw_period = sc->sc_wdog_period;
    266 
    267 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    268 		printf("%s: unable to register PC-Weasel watchdog "
    269 		    "with sysmon\n", sc->sc_dev.dv_xname);
    270 }
    271 
    272 CFATTACH_DECL(weasel_pci, sizeof(struct weasel_softc),
    273     weasel_pci_match, weasel_pci_attach, NULL, NULL);
    274 
    275 static int
    276 weasel_wait_response(struct weasel_softc *sc)
    277 {
    278 	int i;
    279 
    280 	for (i = 10000; i ; i--) {
    281 		delay(100);
    282 		if (bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS) ==
    283 		    OS_WS_HOST_READ)
    284 			return(0);
    285 	}
    286 	return (1);
    287 }
    288 
    289 static int
    290 weasel_issue_command(struct weasel_softc *sc, uint8_t cmd)
    291 {
    292 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_WR, cmd);
    293 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_HOST_STATUS, OS_HS_WEASEL_READ);
    294 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    295 	return (weasel_wait_response(sc));
    296 }
    297 
    298 static int
    299 weasel_pci_wdog_setmode(struct sysmon_wdog *smw)
    300 {
    301 	struct weasel_softc *sc = smw->smw_cookie;
    302 	int error = 0;
    303 
    304 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    305 		error = weasel_pci_wdog_disarm(sc);
    306 	} else {
    307 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    308 			smw->smw_period = sc->sc_wdog_period;
    309 		else if (smw->smw_period != sc->sc_wdog_period) {
    310 			/* Can't change the period on the Weasel. */
    311 			return (EINVAL);
    312 		}
    313 		error = weasel_pci_wdog_arm(sc);
    314 		weasel_pci_wdog_tickle(smw);
    315 	}
    316 
    317 	return (error);
    318 }
    319 
    320 static int
    321 weasel_pci_wdog_tickle(struct sysmon_wdog *smw)
    322 {
    323 	struct weasel_softc *sc = smw->smw_cookie;
    324 	u_int8_t reg;
    325 	int x;
    326 	int s;
    327 	int error = 0;
    328 
    329 	s = splhigh();
    330 	/*
    331 	 * first we tickle the watchdog
    332 	 */
    333 	reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_CHALLENGE);
    334 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_RESPONSE, ~reg);
    335 
    336 	/*
    337 	 * then we check to make sure the weasel is still armed. If someone
    338 	 * has rebooted the weasel for whatever reason (firmware update),
    339 	 * then the watchdog timer would no longer be armed and we'd be
    340 	 * servicing nothing. Let the user know that the machine is no
    341 	 * longer being monitored by the weasel.
    342 	 */
    343 	if((x = weasel_pci_wdog_query_state(sc)) == -1)
    344 		error = EIO;
    345 	if (x == 1) {
    346 		error = 0;
    347 	} else {
    348 		printf("%s: Watchdog timer disabled on PC/Weasel! Disarming wdog.\n",
    349 			sc->sc_dev.dv_xname);
    350 		sc->sc_wdog_armed = 0;
    351 		sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED, 0);
    352 		error = 1;
    353 	}
    354 	splx(s);
    355 
    356 	return (error);
    357 }
    358 
    359 static int
    360 weasel_pci_wdog_arm(struct weasel_softc *sc)
    361 {
    362 	u_int8_t reg;
    363 	int x;
    364 	int s;
    365 	int error = 0;
    366 
    367 	s = splhigh();
    368 	if (weasel_issue_command(sc, OS_CMD_WDT_ENABLE)) {
    369 		printf("%s: no reply to watchdog enable. Check Weasel \"Allow Watchdog\" setting.\n",
    370 			sc->sc_dev.dv_xname);
    371 		error = EIO;
    372 	}
    373 	reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    374 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    375 
    376 	/*
    377 	 * Ensure that the Weasel thinks it's in the same mode we want it to
    378 	 * be in.   EIO if not.
    379 	 */
    380 	x = weasel_pci_wdog_query_state(sc);
    381 	switch (x) {
    382 		case -1:
    383 			error = EIO;
    384 			break;
    385 		case 0:
    386 			sc->sc_wdog_armed = 0;
    387 			error = EIO;
    388 			break;
    389 		case 1:
    390 			sc->sc_wdog_armed = 1;
    391 			error = 0;
    392 			break;
    393 	}
    394 
    395 	splx(s);
    396 	return(error);
    397 }
    398 
    399 
    400 static int
    401 weasel_pci_wdog_disarm(struct weasel_softc *sc)
    402 {
    403 	u_int8_t reg;
    404 	int x;
    405 	int s;
    406 	int error = 0;
    407 
    408 	s = splhigh();
    409 
    410 	if (weasel_issue_command(sc, OS_CMD_WDT_DISABLE)) {
    411 		printf("%s: didn't reply to watchdog disable.\n",
    412 			sc->sc_dev.dv_xname);
    413 		error = EIO;
    414 	}
    415 	reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    416 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    417 
    418 	/*
    419 	 * Ensure that the Weasel thinks it's in the same mode we want it to
    420 	 * be in.   EIO if not.
    421 	 */
    422 	x = weasel_pci_wdog_query_state(sc);
    423 	switch (x) {
    424 		case -1:
    425 			error = EIO;
    426 			break;
    427 		case 0:
    428 			sc->sc_wdog_armed = 0;
    429 			error = 0;
    430 			break;
    431 		case 1:
    432 			sc->sc_wdog_armed = 1;
    433 			error = EIO;
    434 			break;
    435 	}
    436 
    437 	splx(s);
    438 	return(error);
    439 }
    440 
    441 static int
    442 weasel_pci_wdog_query_state(struct weasel_softc *sc)
    443 {
    444 
    445 	u_int8_t v;
    446 
    447 	if (weasel_issue_command(sc, OS_CMD_WDT_QUERY)) {
    448 		printf("%s: didn't reply to watchdog state query.\n",
    449 			sc->sc_dev.dv_xname);
    450 		bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    451 		return(-1);
    452 	}
    453 	v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD);
    454 	bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0);
    455 	return(v);
    456 }
    457