#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>
struct wdt_softc {
struct device sc_dev;
int sc_features;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
};
int wdt_probe(struct device *, void *, void *);
void wdt_attach(struct device *, struct device *, void *);
int wdt_activate(struct device *, int);
int wdt_is501(struct wdt_softc *);
void wdt_8254_count(struct wdt_softc *, int, u_int16_t);
void wdt_8254_mode(struct wdt_softc *, int, int);
int wdt_set_timeout(void *, int);
void wdt_init_timer(struct wdt_softc *);
void wdt_buzzer_off(struct wdt_softc *);
void wdt_timer_disable(struct wdt_softc *);
void wdt_buzzer_enable(struct wdt_softc *);
const struct cfattach wdt_ca = {
sizeof(struct wdt_softc), wdt_probe, wdt_attach,
NULL, wdt_activate
};
struct cfdriver wdt_cd = {
NULL, "wdt", DV_DULL
};
const struct pci_matchid wdt_devices[] = {
{ PCI_VENDOR_INDCOMPSRC, PCI_PRODUCT_INDCOMPSRC_WDT50X }
};
#define WDT_8254_TC_LO 0
#define WDT_8254_TC_HI 1
#define WDT_8254_BUZZER 2
#define WDT_8254_BASE 0
#define WDT_8254_CTL (WDT_8254_BASE + 3)
#define WDT_DISABLE_TIMER 7
#define WDT_ENABLE_TIMER 7
#define WDT_STATUS_REG 4
#define WDT_START_BUZZER 4
#define WDT_TEMPERATURE 5
#define WDT_STOP_BUZZER 5
int
wdt_probe(struct device *parent, void *match, void *aux)
{
return (pci_matchbyid((struct pci_attach_args *)aux, wdt_devices,
nitems(wdt_devices)));
}
void
wdt_attach(struct device *parent, struct device *self, void *aux)
{
struct wdt_softc *wdt = (struct wdt_softc *)self;
struct pci_attach_args *pa = (struct pci_attach_args *)aux;
bus_size_t iosize;
if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
&wdt->sc_iot, &wdt->sc_ioh, NULL, &iosize, 0) != 0) {
printf("%s: couldn't find PCI I/O region\n",
wdt->sc_dev.dv_xname);
return;
}
if (iosize != (bus_size_t)16) {
printf("%s: invalid I/O region size\n",
wdt->sc_dev.dv_xname);
return;
}
if (wdt_is501(wdt))
wdt->sc_features = 1;
else
wdt->sc_features = 0;
if (wdt->sc_features) {
wdt_buzzer_off(wdt);
wdt_buzzer_enable(wdt);
}
wdt_init_timer(wdt);
wdt_timer_disable(wdt);
wdog_register(wdt_set_timeout, wdt);
}
int
wdt_activate(struct device *self, int act)
{
switch (act) {
case DVACT_POWERDOWN:
wdog_shutdown(self);
break;
}
return (0);
}
int
wdt_is501(struct wdt_softc *wdt)
{
int v = bus_space_read_1(wdt->sc_iot, wdt->sc_ioh, WDT_TEMPERATURE);
if (v == 0 || v == 0xFF)
return(0);
return(1);
}
void
wdt_8254_count(struct wdt_softc *wdt, int counter, u_int16_t v)
{
bus_space_write_1(wdt->sc_iot, wdt->sc_ioh,
WDT_8254_BASE + counter, v & 0xFF);
bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_8254_BASE + counter, v >> 8);
}
void
wdt_8254_mode(struct wdt_softc *wdt, int counter, int mode)
{
bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_8254_CTL,
(counter << 6) | 0x30 | (mode << 1));
}
int
wdt_set_timeout(void *self, int seconds)
{
struct wdt_softc *wdt = (struct wdt_softc *)self;
u_int16_t v;
int s;
s = splclock();
wdt_timer_disable(wdt);
if (seconds == 0) {
splx(s);
return (0);
} else if (seconds < 2)
seconds = 2;
else if (seconds > 1800)
seconds = 1800;
v = (u_int16_t)seconds * 50;
wdt_8254_count(wdt, WDT_8254_TC_HI, v);
bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_ENABLE_TIMER, 0);
splx(s);
return (seconds);
}
void
wdt_timer_disable(struct wdt_softc *wdt)
{
(void)bus_space_read_1(wdt->sc_iot, wdt->sc_ioh, WDT_DISABLE_TIMER);
}
void
wdt_init_timer(struct wdt_softc *wdt)
{
wdt_8254_mode(wdt, WDT_8254_TC_LO, 3);
wdt_8254_mode(wdt, WDT_8254_TC_HI, 2);
wdt_8254_count(wdt, WDT_8254_TC_LO, 41666);
}
void
wdt_buzzer_off(struct wdt_softc *wdt)
{
bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_STOP_BUZZER, 0);
}
#ifndef WDT_DISABLE_BUZZER
void
wdt_buzzer_enable(struct wdt_softc *wdt)
{
bus_space_write_1(wdt->sc_iot, wdt->sc_ioh, WDT_8254_BUZZER, 1);
wdt_8254_mode(wdt, WDT_8254_BUZZER, 1);
}
#else
void
wdt_buzzer_disable(struct wdt_softc *wdt)
{
wdt_8254_mode(wdt, WDT_8254_BUZZER, 0);
}
#endif