#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/resource.h>
#include <sys/wdog.h>
#include <bus/isa/isavar.h>
#include <bus/pci/pcivar.h>
#include "amd_chipset.h"
#define AMDSB_WD_CTRL 0x00
#define AMDSB_WD_RUN 0x01
#define AMDSB_WD_FIRED 0x02
#define AMDSB_WD_SHUTDOWN 0x04
#define AMDSB_WD_DISABLE 0x08
#define AMDSB_WD_RESERVED 0x70
#define AMDSB_WD_RELOAD 0x80
#define AMDSB_WD_COUNT 0x04
#define AMDSB_WD_COUNT_MASK 0xffff
#define AMDSB_WDIO_REG_WIDTH 4
#define amdsbwd_verbose_printf(dev, ...) \
do { \
if (bootverbose) \
device_printf(dev, __VA_ARGS__);\
} while (0)
struct amdsbwd_softc {
device_t dev;
struct watchdog wdog;
struct resource *res_ctrl;
struct resource *res_count;
int rid_ctrl;
int rid_count;
int ms_per_tick;
int max_ticks;
int active;
unsigned int timeout;
};
static void amdsbwd_identify(driver_t *driver, device_t parent);
static int amdsbwd_probe(device_t dev);
static int amdsbwd_attach(device_t dev);
static int amdsbwd_detach(device_t dev);
static int amdsbwd_suspend(device_t dev);
static int amdsbwd_resume(device_t dev);
static device_method_t amdsbwd_methods[] = {
DEVMETHOD(device_identify, amdsbwd_identify),
DEVMETHOD(device_probe, amdsbwd_probe),
DEVMETHOD(device_attach, amdsbwd_attach),
DEVMETHOD(device_detach, amdsbwd_detach),
DEVMETHOD(device_suspend, amdsbwd_suspend),
DEVMETHOD(device_resume, amdsbwd_resume),
#if 0
DEVMETHOD(device_shutdown, amdsbwd_detach),
#endif
DEVMETHOD_END
};
static devclass_t amdsbwd_devclass;
static driver_t amdsbwd_driver = {
"amdsbwd",
amdsbwd_methods,
sizeof(struct amdsbwd_softc)
};
DRIVER_MODULE(amdsbwd, isa, amdsbwd_driver, amdsbwd_devclass, NULL, NULL);
MODULE_VERSION(amdsbwd, 1);
static uint8_t
pmio_read(struct resource *res, uint8_t reg)
{
bus_write_1(res, 0, reg);
return (bus_read_1(res, 1));
}
static void
pmio_write(struct resource *res, uint8_t reg, uint8_t val)
{
bus_write_1(res, 0, reg);
bus_write_1(res, 1, val);
}
static uint32_t
wdctrl_read(struct amdsbwd_softc *sc)
{
return (bus_read_4(sc->res_ctrl, 0));
}
static void
wdctrl_write(struct amdsbwd_softc *sc, uint32_t val)
{
bus_write_4(sc->res_ctrl, 0, val);
}
static __unused uint32_t
wdcount_read(struct amdsbwd_softc *sc)
{
return (bus_read_4(sc->res_count, 0));
}
static void
wdcount_write(struct amdsbwd_softc *sc, uint32_t val)
{
bus_write_4(sc->res_count, 0, val);
}
static void
amdsbwd_tmr_enable(struct amdsbwd_softc *sc)
{
uint32_t val;
val = wdctrl_read(sc);
val |= AMDSB_WD_RUN;
wdctrl_write(sc, val);
sc->active = 1;
amdsbwd_verbose_printf(sc->dev, "timer enabled\n");
}
static void
amdsbwd_tmr_disable(struct amdsbwd_softc *sc)
{
uint32_t val;
val = wdctrl_read(sc);
val &= ~AMDSB_WD_RUN;
wdctrl_write(sc, val);
sc->active = 0;
amdsbwd_verbose_printf(sc->dev, "timer disabled\n");
}
static void
amdsbwd_tmr_reload(struct amdsbwd_softc *sc)
{
uint32_t val;
val = wdctrl_read(sc);
val |= AMDSB_WD_RELOAD;
wdctrl_write(sc, val);
}
static void
amdsbwd_tmr_set(struct amdsbwd_softc *sc, uint16_t timeout)
{
timeout &= AMDSB_WD_COUNT_MASK;
wdcount_write(sc, timeout);
sc->timeout = timeout;
amdsbwd_verbose_printf(sc->dev, "timeout set to %u ticks\n", timeout);
}
static int
amdsb_watchdog(void *arg, int period)
{
unsigned int timeout;
struct amdsbwd_softc *sc = arg;
timeout = (period * 1000) / sc->ms_per_tick;
if (timeout > sc->max_ticks)
timeout = sc->max_ticks;
if (timeout != sc->timeout) {
amdsbwd_tmr_set(sc, timeout);
if (!sc->active)
amdsbwd_tmr_enable(sc);
}
amdsbwd_tmr_reload(sc);
return period;
}
static void
amdsbwd_identify(driver_t *driver, device_t parent)
{
device_t child;
device_t smb_dev;
if (resource_disabled("amdsbwd", 0))
return;
if (device_find_child(parent, "amdsbwd", -1) != NULL)
return;
smb_dev = pci_find_bsf(0, 20, 0);
if (smb_dev == NULL)
return;
if (pci_get_devid(smb_dev) != AMDSB_SMBUS_DEVID &&
pci_get_devid(smb_dev) != AMDFCH_SMBUS_DEVID &&
pci_get_devid(smb_dev) != AMDCZ_SMBUS_DEVID) {
return;
}
child = BUS_ADD_CHILD(parent, parent, ISA_ORDER_SPECULATIVE,
"amdsbwd", -1);
if (child == NULL)
device_printf(parent, "add amdsbwd child failed\n");
}
static void
amdsbwd_probe_sb7xx(device_t dev, struct resource *pmres, uint32_t *addr)
{
uint8_t val;
int i;
val = pmio_read(pmres, AMDSB_PM_RESET_STATUS0);
if (val != 0)
amdsbwd_verbose_printf(dev, "ResetStatus0 = %#04x\n", val);
val = pmio_read(pmres, AMDSB_PM_RESET_STATUS1);
if (val != 0)
amdsbwd_verbose_printf(dev, "ResetStatus1 = %#04x\n", val);
if ((val & AMDSB_WD_RST_STS) != 0)
device_printf(dev, "Previous Reset was caused by Watchdog\n");
for (*addr = 0, i = 0; i < 4; i++) {
*addr <<= 8;
*addr |= pmio_read(pmres, AMDSB_PM_WDT_BASE_MSB - i);
}
*addr &= ~0x07U;
val = pmio_read(pmres, AMDSB_PM_WDT_CTRL);
val &= ~AMDSB_WDT_RES_MASK;
val |= AMDSB_WDT_RES_1S;
pmio_write(pmres, AMDSB_PM_WDT_CTRL, val);
val = pmio_read(pmres, AMDSB_PM_WDT_CTRL);
val &= ~AMDSB_WDT_DISABLE;
pmio_write(pmres, AMDSB_PM_WDT_CTRL, val);
device_set_desc(dev, "AMD SB600/SB7xx Watchdog Timer");
}
static void
amdsbwd_probe_sb8xx(device_t dev, struct resource *pmres, uint32_t *addr)
{
uint32_t val;
int i;
val = pmio_read(pmres, AMDSB8_PM_RESET_CTRL);
if ((val & AMDSB8_RST_STS_DIS) != 0) {
val &= ~AMDSB8_RST_STS_DIS;
pmio_write(pmres, AMDSB8_PM_RESET_CTRL, val);
}
val = 0;
for (i = 3; i >= 0; i--) {
val <<= 8;
val |= pmio_read(pmres, AMDSB8_PM_RESET_STATUS + i);
}
if (val != 0)
amdsbwd_verbose_printf(dev, "ResetStatus = 0x%08x\n", val);
if ((val & AMDSB8_WD_RST_STS) != 0)
device_printf(dev, "Previous Reset was caused by Watchdog\n");
for (*addr = 0, i = 0; i < 4; i++) {
*addr <<= 8;
*addr |= pmio_read(pmres, AMDSB8_PM_WDT_EN + 3 - i);
}
*addr &= ~0x07u;
val = pmio_read(pmres, AMDSB8_PM_WDT_CTRL);
val &= ~AMDSB8_WDT_RES_MASK;
val |= AMDSB8_WDT_1HZ;
pmio_write(pmres, AMDSB8_PM_WDT_CTRL, val);
#ifdef AMDSBWD_DEBUG
val = pmio_read(pmres, AMDSB8_PM_WDT_CTRL);
amdsbwd_verbose_printf(dev, "AMDSB8_PM_WDT_CTRL value = %#04x\n", val);
#endif
val = pmio_read(pmres, AMDSB8_PM_WDT_EN);
val &= ~AMDSB8_WDT_DISABLE;
val |= AMDSB8_WDT_DEC_EN;
pmio_write(pmres, AMDSB8_PM_WDT_EN, val);
#ifdef AMDSBWD_DEBUG
val = pmio_read(pmres, AMDSB8_PM_WDT_EN);
device_printf(dev, "AMDSB8_PM_WDT_EN value = %#02x\n", val);
#endif
device_set_desc(dev, "AMD SB8xx/SB9xx/Axx Watchdog Timer");
}
static int
amdsbwd_probe_fch41(device_t dev, struct resource *pmres, uint32_t *addr,
int use_pm_mmio)
{
uint8_t val;
if (use_pm_mmio) {
val = bus_read_1(pmres, AMDFCH41_PM_DECODE_EN0);
val |= AMDFCH41_WDT_EN;
bus_write_1(pmres, AMDFCH41_PM_DECODE_EN0, val);
val = bus_read_1(pmres, AMDFCH41_PM_DECODE_EN0);
if ((val & AMDFCH41_WDT_EN) == 0) {
device_printf(dev,
"watchdog decode enable bit did not stick\n");
return (ENXIO);
}
#ifdef AMDSBWD_DEBUG
device_printf(dev, "AMDFCH41_PM_DECODE_EN0 value = %#04x\n",
val);
#endif
val = bus_read_1(pmres, AMDFCH41_PM_ISA_CTRL);
if ((val & AMDFCH41_MMIO_EN) != 0) {
amdsbwd_verbose_printf(dev,
"ACPI MMIO range is enabled\n");
*addr = AMDFCH41_MMIO_ADDR + AMDFCH41_MMIO_WDT_OFF;
} else {
*addr = AMDFCH41_WDT_FIXED_ADDR;
}
val = bus_read_1(pmres, AMDFCH41_PM_DECODE_EN3);
val &= ~AMDFCH41_WDT_RES_MASK;
val |= AMDFCH41_WDT_RES_1S;
val &= ~AMDFCH41_WDT_EN_MASK;
val |= AMDFCH41_WDT_ENABLE;
bus_write_1(pmres, AMDFCH41_PM_DECODE_EN3, val);
#ifdef AMDSBWD_DEBUG
val = bus_read_1(pmres, AMDFCH41_PM_DECODE_EN3);
amdsbwd_verbose_printf(dev,
"AMDFCH41_PM_DECODE_EN3 value = %#04x\n", val);
#endif
device_set_desc(dev, "AMD FCH Rev 41h+ Watchdog Timer");
return (0);
}
val = pmio_read(pmres, AMDFCH41_PM_ISA_CTRL);
if ((val & AMDFCH41_MMIO_EN) != 0) {
amdsbwd_verbose_printf(dev, "ACPI MMIO range is enabled\n");
*addr = AMDFCH41_MMIO_ADDR + AMDFCH41_MMIO_WDT_OFF;
} else {
val = pmio_read(pmres, AMDFCH41_PM_DECODE_EN0);
val |= AMDFCH41_WDT_EN;
pmio_write(pmres, AMDFCH41_PM_DECODE_EN0, val);
#ifdef AMDSBWD_DEBUG
val = pmio_read(pmres, AMDFCH41_PM_DECODE_EN0);
device_printf(dev, "AMDFCH41_PM_DECODE_EN0 value = %#04x\n",
val);
#endif
*addr = AMDFCH41_WDT_FIXED_ADDR;
}
val = pmio_read(pmres, AMDFCH41_PM_DECODE_EN3);
val &= ~AMDFCH41_WDT_RES_MASK;
val |= AMDFCH41_WDT_RES_1S;
val &= ~AMDFCH41_WDT_EN_MASK;
val |= AMDFCH41_WDT_ENABLE;
pmio_write(pmres, AMDFCH41_PM_DECODE_EN3, val);
#ifdef AMDSBWD_DEBUG
val = pmio_read(pmres, AMDFCH41_PM_DECODE_EN3);
amdsbwd_verbose_printf(dev, "AMDFCH41_PM_DECODE_EN3 value = %#04x\n",
val);
#endif
device_set_desc(dev, "AMD FCH Rev 41h+ Watchdog Timer");
return (0);
}
static int
amdsbwd_probe(device_t dev)
{
struct resource *res;
device_t smb_dev;
uint32_t addr;
uint32_t pmaddr;
int rid;
int rc;
int pmtype;
int pmwidth;
uint32_t devid;
uint8_t revid;
if (isa_get_logicalid(dev) != 0)
return (ENXIO);
smb_dev = pci_find_bsf(0, 20, 0);
KASSERT(smb_dev != NULL, ("can't find SMBus PCI device\n"));
devid = pci_get_devid(smb_dev);
revid = pci_get_revid(smb_dev);
if (devid == AMDCZ_SMBUS_DEVID && revid >= AMDCZ51_SMBUS_REVID) {
pmtype = SYS_RES_MEMORY;
pmaddr = AMDFCH41_MMIO_ADDR + AMDFCH41_MMIO_PM_OFF;
pmwidth = AMDFCH41_MMIO_PM_WIDTH;
} else {
pmtype = SYS_RES_IOPORT;
pmaddr = AMDSB_PMIO_INDEX;
pmwidth = AMDSB_PMIO_WIDTH;
}
rc = bus_set_resource(dev, pmtype, 0, pmaddr, pmwidth, -1);
if (rc != 0) {
device_printf(dev, "bus_set_resource for PM failed\n");
return (ENXIO);
}
rid = 0;
res = bus_alloc_resource(dev, pmtype, &rid, 0ul, ~0ul, pmwidth,
RF_ACTIVE | RF_SHAREABLE);
if (res == NULL) {
device_printf(dev, "bus_alloc_resource for PM failed\n");
return (ENXIO);
}
if (devid == AMDSB_SMBUS_DEVID && revid < AMDSB8_SMBUS_REVID) {
amdsbwd_probe_sb7xx(dev, res, &addr);
rc = 0;
} else if (devid == AMDSB_SMBUS_DEVID ||
(devid == AMDFCH_SMBUS_DEVID && revid < AMDFCH41_SMBUS_REVID) ||
(devid == AMDCZ_SMBUS_DEVID && revid < AMDCZ49_SMBUS_REVID)) {
amdsbwd_probe_sb8xx(dev, res, &addr);
rc = 0;
} else {
rc = amdsbwd_probe_fch41(dev, res, &addr,
pmtype == SYS_RES_MEMORY);
}
bus_release_resource(dev, pmtype, rid, res);
bus_delete_resource(dev, pmtype, rid);
if (rc != 0)
return (rc);
amdsbwd_verbose_printf(dev, "memory base address = %#010x\n", addr);
rc = bus_set_resource(dev, SYS_RES_MEMORY, 0, addr + AMDSB_WD_CTRL,
AMDSB_WDIO_REG_WIDTH, -1);
if (rc != 0) {
device_printf(dev, "bus_set_resource for control failed\n");
return (ENXIO);
}
rc = bus_set_resource(dev, SYS_RES_MEMORY, 1, addr + AMDSB_WD_COUNT,
AMDSB_WDIO_REG_WIDTH, -1);
if (rc != 0) {
device_printf(dev, "bus_set_resource for count failed\n");
return (ENXIO);
}
return (0);
}
static int
amdsbwd_attach_sb(device_t dev, struct amdsbwd_softc *sc)
{
sc->max_ticks = UINT16_MAX;
sc->rid_ctrl = 0;
sc->rid_count = 1;
sc->ms_per_tick = 1000;
sc->res_ctrl = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
&sc->rid_ctrl, RF_ACTIVE);
if (sc->res_ctrl == NULL) {
device_printf(dev, "bus_alloc_resource for ctrl failed\n");
return (ENXIO);
}
sc->res_count = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
&sc->rid_count, RF_ACTIVE);
if (sc->res_count == NULL) {
device_printf(dev, "bus_alloc_resource for count failed\n");
return (ENXIO);
}
return (0);
}
static int
amdsbwd_attach(device_t dev)
{
struct amdsbwd_softc *sc;
int rc;
sc = device_get_softc(dev);
sc->dev = dev;
rc = amdsbwd_attach_sb(dev, sc);
if (rc != 0)
goto fail;
#ifdef AMDSBWD_DEBUG
device_printf(dev, "wd ctrl = %#04x\n", wdctrl_read(sc));
device_printf(dev, "wd count = %#04x\n", wdcount_read(sc));
#endif
wdctrl_write(sc, AMDSB_WD_FIRED);
if (wdctrl_read(sc) & AMDSB_WD_DISABLE) {
device_printf(dev, "watchdog hardware is disabled\n");
goto fail;
}
sc->wdog.name = "AMD southbridge";
sc->wdog.wdog_fn = amdsb_watchdog;
sc->wdog.arg = sc;
sc->wdog.period_max = (UINT16_MAX*1000) / 10;
wdog_register(&sc->wdog);
return (0);
fail:
amdsbwd_detach(dev);
return (ENXIO);
}
static int
amdsbwd_detach(device_t dev)
{
struct amdsbwd_softc *sc;
sc = device_get_softc(dev);
if (sc->wdog.name)
wdog_unregister(&sc->wdog);
if (sc->active)
amdsbwd_tmr_disable(sc);
if (sc->res_ctrl != NULL)
bus_release_resource(dev, SYS_RES_MEMORY, sc->rid_ctrl,
sc->res_ctrl);
if (sc->res_count != NULL)
bus_release_resource(dev, SYS_RES_MEMORY, sc->rid_count,
sc->res_count);
return (0);
}
static int
amdsbwd_suspend(device_t dev)
{
struct amdsbwd_softc *sc;
uint32_t val;
sc = device_get_softc(dev);
val = wdctrl_read(sc);
val &= ~AMDSB_WD_RUN;
wdctrl_write(sc, val);
return (0);
}
static int
amdsbwd_resume(device_t dev)
{
struct amdsbwd_softc *sc;
sc = device_get_softc(dev);
wdctrl_write(sc, AMDSB_WD_FIRED);
if (sc->active) {
amdsbwd_tmr_set(sc, sc->timeout);
amdsbwd_tmr_enable(sc);
amdsbwd_tmr_reload(sc);
}
return (0);
}