Watchdog back-end driver for PC Weasel/PCI serial console and remote

management board.  Written by hpeyerl@realweasel.com and
thorpej@wasabisystems.com.  Requires Weasel firmware v2.04 and later.
This commit is contained in:
hpeyerl 2002-01-01 16:48:34 +00:00
parent e25a471c87
commit e59c0dedfb
2 changed files with 556 additions and 0 deletions

456
sys/dev/pci/weasel_pci.c Normal file
View File

@ -0,0 +1,456 @@
/* $NetBSD: weasel_pci.c,v 1.1 2002/01/01 16:48:34 hpeyerl 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 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.1 2002/01/01 16:48:34 hpeyerl Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/wdog.h>
#include <sys/endian.h>
#include <machine/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 {
struct device sc_dev; /* generic device glue */
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;
};
int weasel_pci_match(struct device *, struct cfdata *, void *);
void weasel_pci_attach(struct device *, struct device *, void *);
extern int sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);
struct cfattach weasel_pci_ca = {
sizeof(struct weasel_softc), weasel_pci_match, weasel_pci_attach,
};
int weasel_pci_wdog_setmode(struct sysmon_wdog *);
int weasel_pci_wdog_tickle(struct sysmon_wdog *);
int weasel_wait_response(struct weasel_softc *);
int weasel_issue_command(struct weasel_softc *, uint8_t cmd);
int weasel_pci_wdog_arm(struct weasel_softc *);
int weasel_pci_wdog_disarm(struct weasel_softc *);
int weasel_pci_wdog_query_state(struct weasel_softc *);
int
weasel_pci_match(struct device *parent, struct cfdata *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);
}
void
weasel_pci_attach(struct device *parent, struct device *self, void *aux)
{
struct weasel_softc *sc = (void *) 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];
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) {
printf("%s: unable to map device registers\n",
sc->sc_dev.dv_xname);
return;
}
/* Ping the Weasel to see if it's alive. */
if (weasel_issue_command(sc, OS_CMD_PING)) {
printf("%s: Weasel didn't respond to PING\n",
sc->sc_dev.dv_xname);
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) {
printf("%s: unexpected PING response from Weasel: 0x%02x\n",
sc->sc_dev.dv_xname, v);
return;
}
/* Read the config block. */
if (weasel_issue_command(sc, OS_CMD_SHOW_CONFIG)) {
printf("%s: Weasel didn't respond to SHOW_CONFIG\n",
sc->sc_dev.dv_xname);
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)) {
printf("%s: weird config block size from Weasel: 0x%03x\n",
sc->sc_dev.dv_xname, cfg_size);
return;
}
for (cp = (uint8_t *) &cfg; cfg_size != 0; cfg_size--) {
if (weasel_wait_response(sc)) {
printf("%s: Weasel stopped providing config block(%d)\n",
sc->sc_dev.dv_xname, 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", sc->sc_dev.dv_xname,
mode);
else
printf("%s: unknown config version 0x%02x\n", sc->sc_dev.dv_xname,
cfg.cfg_version);
/*
* Fetch sw version.
*/
if (weasel_issue_command(sc, OS_CMD_QUERY_SW_VER)) {
printf("%s: didn't reply to software version query.\n",
sc->sc_dev.dv_xname);
}
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",
sc->sc_dev.dv_xname, v);
bzero(buf, sizeof(buf));
for (cp = buf; v != 0; v--) {
if (weasel_wait_response(sc)) {
printf("%s: Weasel stopped providing version\n",
sc->sc_dev.dv_xname);
}
*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", sc->sc_dev.dv_xname, buf);
}
/*
* Fetch logic version.
*/
if (weasel_issue_command(sc, OS_CMD_QUERY_L_VER)) {
printf("\n%s: didn't reply to logic version query.\n",
sc->sc_dev.dv_xname);
}
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)) {
printf("\n%s: didn't reply to vga bios version query.\n",
sc->sc_dev.dv_xname);
}
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)) {
printf("\n%s: didn't reply to hardware version query.\n",
sc->sc_dev.dv_xname);
}
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", sc->sc_dev.dv_xname,
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)
printf("%s: unable to register PC-Weasel watchdog "
"with sysmon\n", sc->sc_dev.dv_xname);
}
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);
}
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));
}
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);
}
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",
sc->sc_dev.dv_xname);
sc->sc_wdog_armed = 0;
sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED, 0);
error = 1;
}
splx(s);
return (error);
}
int
weasel_pci_wdog_arm(struct weasel_softc *sc)
{
u_int8_t reg;
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",
sc->sc_dev.dv_xname);
error = EIO;
}
reg = 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);
}
int
weasel_pci_wdog_disarm(struct weasel_softc *sc)
{
u_int8_t reg;
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",
sc->sc_dev.dv_xname);
error = EIO;
}
reg = 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);
}
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",
sc->sc_dev.dv_xname);
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);
}

100
sys/dev/pci/weaselreg.h Normal file
View File

@ -0,0 +1,100 @@
/* $NetBSD: weaselreg.h,v 1.1 2002/01/01 16:48:34 hpeyerl 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 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.
*/
#ifndef _DEV_PCI_WEASELREG_H_
#define _DEV_PCI_WEASELREG_H_
/*
* PCI-Weasel configuration block.
*/
#define CFG_VERSION_2 0x02
struct weasel_config_block {
uint8_t cfg_version; /* version of config */
uint8_t weasel_attn; /* attention character */
uint8_t debug; /* debug level */
uint8_t reset_pc_on_boot; /* reset PC Weasel on boot */
uint8_t duart_baud; /* UART baud rate */
uint8_t duart_parity; /* 0=none, 1=even, 2=odd */
uint8_t duart_bits; /* 7 or 8 */
uint8_t enable_duart_switching; /* UART switching mode */
uint8_t wdt_allow; /* WDOG is allowed */
uint16_t wdt_msec; /* WDOG period in msec. */
uint8_t duart_flow; /* RTS/CTS enabled */
uint8_t break_passthru; /* BRK passed through to 16550 */
uint8_t serial_number[10]; /* board serial number */
uint8_t default_partition; /* default Weasel boot image */
uint8_t color; /* use color */
uint8_t future_use[229]; /* reserved for future use */
uint8_t cksum; /* always last element. */
};
#define WEASEL_DATA_RD 0x00
#define OS_RET_WDT_ACTIVE 0x01
#define OS_RET_WDT_INACTIVE 0x02
#define OS_RET_PONG 0x03
#define WEASEL_DATA_WR 0x01
#define OS_CMD_WDT_ENABLE 0x01
#define OS_CMD_WDT_DISABLE 0x02
#define OS_CMD_WDT_QUERY 0x03
#define OS_CMD_SHOW_CONFIG 0x04
#define OS_CMD_QUERY_SW_VER 0x05
#define OS_CMD_QUERY_HW_VER 0x06
#define OS_CMD_QUERY_L_VER 0x07
#define OS_CMD_PING 0x08
#define OS_CMD_QUERY_VB_VER 0x09
#define OS_CMD_QUERT_BR_VER 0x0a
#define OS_CMD_RESET_SM 0xff
#define WEASEL_STATUS 0x02
#define OS_WS_HOST_WRITE 0x00
#define OS_WS_HOST_READ 0x01
#define OS_WS_WDT_RDY 0x02
#define OS_WS_BUSY 0xff
#define WEASEL_HOST_STATUS 0x03
#define OS_HS_WEASEL_WRITE 0x00
#define OS_HS_WEASEL_READ 0x01
#define OS_HS_WDT_RDY 0x02
#define WEASEL_CHALLENGE 0x04
#define WEASEL_RESPONSE 0x05
#endif /* _DEV_PCI_WEASELREG_H_ */