#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: adm5120_extio.c,v 1.8 2021/08/07 16:18:58 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <mips/cache.h>
#include <mips/cpuregs.h>
#include <mips/adm5120/include/adm5120reg.h>
#include <mips/adm5120/include/adm5120var.h>
#include <mips/adm5120/include/adm5120_mainbusvar.h>
#include <mips/adm5120/include/adm5120_extiovar.h>
#include "locators.h"
#ifdef EXTIO_DEBUG
int extio_debug = 1;
#define EXTIO_DPRINTF(__fmt, ...) \
do { \
if (extio_debug) \
printf((__fmt), __VA_ARGS__); \
} while (0)
#else
#define EXTIO_DPRINTF(__fmt, ...) do { } while (0)
#endif
static int extio_match(device_t, cfdata_t, void *);
static void extio_attach(device_t, device_t, void *);
static int extio_submatch(device_t, cfdata_t, const int *, void *);
static int extio_print(void *, const char *);
CFATTACH_DECL_NEW(extio, sizeof(struct extio_softc),
extio_match, extio_attach, NULL, NULL);
int extio_found;
struct extiodev {
const char *ed_name;
bus_addr_t ed_addr;
int ed_irq;
uint32_t ed_gpio_mask;
int ed_cfio;
};
struct extiodev extiodevs[] = {
{"wdc", ADM5120_BASE_EXTIO0, 0, __BIT(4), 1},
{NULL, 0, 0, 0x0, 0},
};
static int
extio_match(device_t parent, cfdata_t match, void *aux)
{
return !extio_found;
}
static void
extio_attach_args_create(struct extio_attach_args *ea, struct extiodev *ed,
void *gpio, bus_space_tag_t st)
{
ea->ea_name = ed->ed_name;
ea->ea_addr = ed->ed_addr;
ea->ea_irq = ed->ed_irq;
ea->ea_st = st;
ea->ea_gpio = gpio;
ea->ea_gpio_mask = ed->ed_gpio_mask;
ea->ea_cfio = ed->ed_cfio;
}
static void
extio_mpmc_dump(struct extio_softc *sc)
{
EXTIO_DPRINTF("%s: regs:\n"
" ctl 0x%08" PRIx32 "\n"
" sts 0x%08" PRIx32 "\n"
" sc 0x%08" PRIx32 "\n"
" sww 0x%08" PRIx32 "\n"
" swo 0x%08" PRIx32 "\n"
" swr 0x%08" PRIx32 "\n"
" swp 0x%08" PRIx32 "\n"
" swwr 0x%08" PRIx32 "\n"
" swt 0x%08" PRIx32 "\n", __func__,
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL),
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_STATUS),
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2)),
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2)),
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2)),
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2)),
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2)),
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2)),
bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2)));
}
static void
extio_mpmc_init(struct extio_softc *sc)
{
int i, s;
#if 0
uint32_t control;
#endif
uint32_t status;
if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
&sc->sc_mpmch) != 0) {
aprint_error_dev(sc->sc_dev, "unable to map MPMC\n");
return;
}
extio_mpmc_dump(sc);
#if 0
control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_DWB;
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
control);
#endif
s = splhigh();
for (i = 1000; --i > 0; ) {
status = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
ADM5120_MPMC_STATUS);
if ((status &
(ADM5120_MPMC_STATUS_WBS|ADM5120_MPMC_STATUS_BU)) == 0)
break;
delay(10);
}
if (i == 0) {
aprint_error_dev(sc->sc_dev,
"timeout waiting for MPMC idle\n");
splx(s);
return;
} else
EXTIO_DPRINTF("%s: MPMC idle\n", device_xname(sc->sc_dev));
#if 0
control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_ME |
ADM5120_MPMC_CONTROL_LPM;
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
control);
#endif
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2),
ADM5120_MPMC_SC_BLS|ADM5120_MPMC_SC_PM|ADM5120_MPMC_SC_MW_8B);
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2),
__SHIFTIN(2, ADM5120_MPMC_SWW_WWE));
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2),
__SHIFTIN(3, ADM5120_MPMC_SWO_WOE));
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2),
__SHIFTIN(26, ADM5120_MPMC_SWR_NMRW));
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2),
__SHIFTIN(29, ADM5120_MPMC_SWP_WPS));
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2),
__SHIFTIN(20, ADM5120_MPMC_SWWR_WWS));
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2),
__SHIFTIN(9, ADM5120_MPMC_SWT_WAITTURN));
#if 0
control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
ADM5120_MPMC_CONTROL) &
~(ADM5120_MPMC_CONTROL_LPM|ADM5120_MPMC_CONTROL_DWB);
bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
control);
splx(s);
#endif
extio_mpmc_dump(sc);
}
static void
extio_attach(device_t parent, device_t self, void *aux)
{
struct extio_softc *sc = device_private(self);
struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux;
struct extio_attach_args ea;
struct extiodev *ed;
struct adm5120_config *admc = &adm5120_configuration;
extio_found = 1;
printf("\n");
sc->sc_dev = self;
sc->sc_gpio = ma->ma_gpio;
sc->sc_obiot = ma->ma_obiot;
sc->sc_gpioh = ma->ma_gpioh;
EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
sc->sc_pm.pm_map = &sc->sc_map[0];
if (gpio_pin_map(sc->sc_gpio, 0, __BITS(0, 4), &sc->sc_pm) != 0) {
aprint_error_dev(sc->sc_dev, "failed to map GPIO[1:2]\n");
}
EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 0, GPIO_PIN_INPUT);
gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 1, GPIO_PIN_OUTPUT);
gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 2, GPIO_PIN_INPUT);
gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 3, GPIO_PIN_OUTPUT);
gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 4, GPIO_PIN_INPUT);
gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 1, 0);
gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 3, 0);
EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
if (gpio_pin_read(sc->sc_gpio, &sc->sc_pm, 0) == GPIO_PIN_HIGH) {
EXTIO_DPRINTF("%s: WAIT# inactive\n",
device_xname(sc->sc_dev));
bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
ADM5120_GPIO2_EW | ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
} else {
aprint_error_dev(sc->sc_dev, "WAIT# active; may be stuck\n");
bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
}
EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
&sc->sc_mpmch) != 0) {
aprint_error_dev(sc->sc_dev, "unable to map MPMC\n");
return;
}
extio_mpmc_init(sc);
EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
cfio_bus_mem_init(&sc->sc_cfio, &admc->extio_space);
for (ed = extiodevs; ed->ed_name != NULL; ed++) {
EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
extio_attach_args_create(&ea, ed, sc->sc_gpio,
(ed->ed_cfio) ? &sc->sc_cfio : &admc->extio_space);
EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
config_found(self, &ea, extio_print,
CFARGS(.submatch = extio_submatch,
.iattr = "extio"));
}
EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
extio_mpmc_dump(sc);
}
static int
extio_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct extio_attach_args *ea = aux;
if (cf->cf_loc[EXTIOCF_CFIO] != EXTIOCF_CFIO_DEFAULT &&
cf->cf_loc[EXTIOCF_CFIO] != ea->ea_cfio)
return 0;
if (cf->cf_loc[EXTIOCF_GPIO_MASK] != EXTIOCF_GPIO_MASK_DEFAULT &&
cf->cf_loc[EXTIOCF_GPIO_MASK] != ea->ea_gpio_mask)
return 0;
if (cf->cf_loc[EXTIOCF_IRQ] != EXTIOCF_IRQ_DEFAULT &&
cf->cf_loc[EXTIOCF_IRQ] != ea->ea_irq)
return 0;
if (cf->cf_loc[EXTIOCF_ADDR] != EXTIOCF_ADDR_DEFAULT &&
cf->cf_loc[EXTIOCF_ADDR] != ea->ea_addr)
return 0;
return config_match(parent, cf, aux);
}
static int
extio_print(void *aux, const char *pnp)
{
struct extio_attach_args *ea = aux;
if (pnp != NULL)
aprint_normal("%s at %s", ea->ea_name, pnp);
if (ea->ea_cfio != EXTIOCF_CFIO_DEFAULT)
aprint_normal(" cfio");
if (ea->ea_addr != EXTIOCF_ADDR_DEFAULT)
aprint_normal(" addr 0x%"PRIxBUSADDR, ea->ea_addr);
if (ea->ea_gpio_mask != EXTIOCF_GPIO_MASK_DEFAULT)
aprint_normal(" gpio_mask 0x%02x", ea->ea_gpio_mask);
return UNCONF;
}