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