#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nct.c,v 1.5 2021/08/07 16:19:12 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/device.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/gpio.h>
#include <machine/autoconf.h>
#include <dev/isa/isavar.h>
#include <dev/gpio/gpiovar.h>
#define NCT_IOBASE_A 0x2e
#define NCT_IOBASE_B 0x4e
#define NCT_IOSIZE 2
#define NCT_CHIP_ID_1 0x1061
#define NCT_CHIP_ID_2 0xc452
#define NCT_NUM_PINS 17
#define NCT_KEY_UNLOCK 0x87
#define NCT_KEY_LOCK 0xaa
#define NCT_PORT_SELECT 0
#define NCT_PORT_DATA 1
#define GD_DEVSEL 0x0007
#define GD_MULTIFUN 0x001c
#define GD_MULTIFUN_GPIO1 0x04
#define GD_MULTIFUN_GPIO0 0x08
#define GD_MULTIFUN_GPIO67 0x10
#define GD_GLOBOPT 0x0027
#define GD_GLOBOPT_GPIO67 0x04
#define GD_ID_HIGH 0x0020
#define GD_ID_LOW 0x0021
#define LD7_ENABLE 0x0730
#define LD7_ENABLE_GPIO0 0x01
#define LD7_ENABLE_GPIO1 0x02
#define LD7_ENABLE_GPIO67 0x40
#define LD7_GPIO0_DIRECTION 0x07e0
#define LD7_GPIO0_DATA 0x07e1
#define LD7_GPIO0_INVERSION 0x07e2
#define LD7_GPIO0_STATUS 0x07e3
#define LD7_GPIO1_DIRECTION 0x07e4
#define LD7_GPIO1_DATA 0x07e5
#define LD7_GPIO1_INVERSION 0x07e6
#define LD7_GPIO1_STATUS 0x07e7
#define LD7_GPIO67_DIRECTION 0x07f8
#define LD7_GPIO67_DATA 0x07f9
#define LD7_GPIO67_INVERSION 0x07fa
#define LD7_GPIO67_STATUS 0x07fb
#define LD8_DEVCFG 0x0830
#define LD8_GPIO0_MULTIFUNC 0x08e0
#define LD8_GPIO1_MULTIFUNC 0x08e1
#define LD8_GPIO67_MULTIFUNC 0x08e7
#define LDA_UARTC_ENABLE 0x0a30
#define LDB_UARTD_ENABLE 0x0b30
#define LDF_GPIO0_OUTMODE 0x0fe0
#define LDF_GPIO1_OUTMODE 0x0fe1
#define LDF_GPIO67_OUTMODE 0x0fe6
struct nct_bank {
u_int8_t nb_firstpin;
u_int8_t nb_numpins;
u_int8_t nb_enabled;
u_int8_t nb_val_dir;
u_int8_t nb_val_inv;
u_int8_t nb_val_mode;
u_int16_t nb_reg_dir;
u_int16_t nb_reg_data;
u_int16_t nb_reg_inv;
u_int16_t nb_reg_stat;
u_int16_t nb_reg_mode;
};
struct nct_softc {
device_t sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
struct gpio_chipset_tag sc_gc;
gpio_pin_t sc_pins[NCT_NUM_PINS];
kmutex_t sc_lock;
int sc_curdev;
int sc_curreg;
struct nct_bank sc_bank[3];
};
static void nct_attach(device_t, device_t, void *);
static int nct_detach(device_t, int);
static void nct_gpio_ctl(void *, int, int);
static int nct_gpio_read(void *, int);
static void nct_gpio_write(void *, int, int);
static int nct_match(device_t, cfdata_t , void *);
static u_int8_t nct_rd(struct nct_softc *, int);
static struct nct_bank *nct_sel(struct nct_softc *, int, u_int8_t *);
static void nct_wr(struct nct_softc *, int, u_int8_t);
static inline void
nct_outb(struct nct_softc *sc, int reg, u_int8_t data)
{
bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, data);
}
static inline u_int8_t
nct_inb(struct nct_softc *sc, int reg)
{
return bus_space_read_1(sc->sc_iot, sc->sc_ioh, reg);
}
CFATTACH_DECL_NEW(nct,
sizeof(struct nct_softc),
nct_match,
nct_attach,
nct_detach,
NULL);
MODULE(MODULE_CLASS_DRIVER, nct, "gpio");
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
nct_modcmd(modcmd_t cmd, void *priv)
{
int error = 0;
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
error = config_init_component(cfdriver_ioconf_nct,
cfattach_ioconf_nct, cfdata_ioconf_nct);
#endif
return error;
case MODULE_CMD_FINI:
#ifdef _MODULE
error = config_fini_component(cfdriver_ioconf_nct,
cfattach_ioconf_nct, cfdata_ioconf_nct);
#endif
return error;
default:
return ENOTTY;
}
}
static int
nct_match(device_t parent, cfdata_t match, void *aux)
{
int ioaddrs[2] = { 0x2e, 0x4e };
struct isa_attach_args *ia = aux;
bus_space_handle_t ioh;
int nioaddr, i;
u_int8_t low, high;
u_int16_t id;
const char *vendor;
if (ia->ia_nio > 0 && ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
ioaddrs[0] = ia->ia_io[0].ir_addr;
nioaddr = 1;
} else {
vendor = pmf_get_platform("system-vendor");
if (vendor != NULL && strstr(vendor, "PC Engines") != NULL) {
nioaddr = __arraycount(ioaddrs);
} else {
nioaddr = 0;
}
}
for (i = 0; i < nioaddr; i++) {
if (bus_space_map(ia->ia_iot, ioaddrs[i], NCT_IOSIZE, 0,
&ioh) != 0) {
continue;
}
bus_space_write_1(ia->ia_iot, ioh, NCT_PORT_SELECT,
NCT_KEY_UNLOCK);
bus_space_write_1(ia->ia_iot, ioh, NCT_PORT_SELECT,
NCT_KEY_UNLOCK);
bus_space_write_1(ia->ia_iot, ioh, NCT_PORT_SELECT, GD_ID_LOW);
low = bus_space_read_1(ia->ia_iot, ioh, NCT_PORT_DATA);
bus_space_write_1(ia->ia_iot, ioh, NCT_PORT_SELECT, GD_ID_HIGH);
high = bus_space_read_1(ia->ia_iot, ioh, NCT_PORT_DATA);
id = (u_int16_t)low | ((u_int16_t)high << 8);
bus_space_unmap(ia->ia_iot, ioh, NCT_IOSIZE);
if (id == NCT_CHIP_ID_1 || id == NCT_CHIP_ID_2) {
ia->ia_nirq = 0;
ia->ia_ndrq = 0;
ia->ia_niomem = 0;
ia->ia_nio = 1;
ia->ia_io[0].ir_size = NCT_IOSIZE;
ia->ia_io[0].ir_addr = ioaddrs[i];
return 1;
}
}
return 0;
}
static void
nct_attach(device_t parent, device_t self, void *aux)
{
struct nct_softc *sc = device_private(self);
struct isa_attach_args *ia = aux;
struct gpiobus_attach_args gba;
struct nct_bank *nb;
u_int8_t multifun, enable;
int i, j;
if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr,
ia->ia_io[0].ir_size, 0, &sc->sc_ioh) != 0) {
aprint_normal(": can't map i/o space\n");
return;
}
aprint_normal(": Nuvoton NCT5104D GPIO\n");
sc->sc_dev = self;
sc->sc_iot = ia->ia_iot;
sc->sc_curdev = -1;
sc->sc_curreg = -1;
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
mutex_spin_enter(&sc->sc_lock);
nct_wr(sc, LD8_DEVCFG, 0);
multifun = nct_rd(sc, GD_MULTIFUN);
nct_wr(sc, GD_MULTIFUN,
multifun & ~(GD_MULTIFUN_GPIO0 | GD_MULTIFUN_GPIO1));
nct_wr(sc, LDA_UARTC_ENABLE, 0);
nct_wr(sc, LD8_GPIO0_MULTIFUNC, 0);
nct_wr(sc, LDB_UARTD_ENABLE, 0);
nct_wr(sc, LD8_GPIO1_MULTIFUNC, 0);
multifun = nct_rd(sc, GD_MULTIFUN);
enable = nct_rd(sc, LD7_ENABLE) | LD7_ENABLE_GPIO0 | LD7_ENABLE_GPIO1;
nb = &sc->sc_bank[0];
nb->nb_firstpin = 0;
nb->nb_numpins = 8;
nb->nb_enabled = 0xff;
nb->nb_reg_dir = LD7_GPIO0_DIRECTION;
nb->nb_reg_data = LD7_GPIO0_DATA;
nb->nb_reg_inv = LD7_GPIO0_INVERSION;
nb->nb_reg_stat = LD7_GPIO0_STATUS;
nb->nb_reg_mode = LDF_GPIO0_OUTMODE;
nb = &sc->sc_bank[1];
nb->nb_firstpin = 8;
nb->nb_numpins = 8;
nb->nb_enabled = 0xff;
nb->nb_reg_dir = LD7_GPIO1_DIRECTION;
nb->nb_reg_data = LD7_GPIO1_DATA;
nb->nb_reg_inv = LD7_GPIO1_INVERSION;
nb->nb_reg_stat = LD7_GPIO1_STATUS;
nb->nb_reg_mode = LDF_GPIO1_OUTMODE;
nb = &sc->sc_bank[2];
nb->nb_firstpin = 16;
nb->nb_numpins = 1;
nb->nb_reg_dir = LD7_GPIO67_DIRECTION;
nb->nb_reg_data = LD7_GPIO67_DATA;
nb->nb_reg_stat = LD7_GPIO67_STATUS;
nb->nb_reg_mode = LDF_GPIO67_OUTMODE;
if ((multifun & GD_MULTIFUN_GPIO67) != 0 &&
(nct_rd(sc, GD_GLOBOPT) & GD_GLOBOPT_GPIO67) == 0) {
nct_wr(sc, LD8_GPIO67_MULTIFUNC, 0);
nb->nb_enabled = 0x01;
enable |= LD7_ENABLE_GPIO67;
} else {
sc->sc_bank[2].nb_enabled = 0;
}
nct_wr(sc, LD7_ENABLE, enable);
mutex_spin_exit(&sc->sc_lock);
aprint_normal_dev(self,
"enabled pins: GPIO0(%02x) GPIO1(%02x) GPIO67(%01x)\n",
(unsigned)sc->sc_bank[0].nb_enabled,
(unsigned)sc->sc_bank[1].nb_enabled,
(unsigned)sc->sc_bank[2].nb_enabled);
memset(sc->sc_pins, 0, sizeof(sc->sc_pins));
for (i = 0; i < __arraycount(sc->sc_bank); i++) {
nb = &sc->sc_bank[i];
mutex_spin_enter(&sc->sc_lock);
nb->nb_val_dir = nct_rd(sc, nb->nb_reg_dir);
nb->nb_val_inv = nct_rd(sc, nb->nb_reg_inv);
nb->nb_val_mode = nct_rd(sc, nb->nb_reg_mode);
mutex_spin_exit(&sc->sc_lock);
for (j = 0; j < nb->nb_numpins; j++) {
gpio_pin_t *pin = &sc->sc_pins[nb->nb_firstpin + j];
pin->pin_num = nb->nb_firstpin + j;
if ((nb->nb_enabled & (1 << j)) == 0) {
continue;
}
pin->pin_caps =
GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
GPIO_PIN_OPENDRAIN |
GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
pin->pin_flags =
GPIO_PIN_INPUT | GPIO_PIN_OPENDRAIN;
nct_gpio_ctl(sc, pin->pin_num, pin->pin_flags);
pin->pin_state = nct_gpio_read(sc, pin->pin_num);
}
}
sc->sc_gc.gp_cookie = sc;
sc->sc_gc.gp_pin_read = nct_gpio_read;
sc->sc_gc.gp_pin_write = nct_gpio_write;
sc->sc_gc.gp_pin_ctl = nct_gpio_ctl;
gba.gba_gc = &sc->sc_gc;
gba.gba_pins = sc->sc_pins;
gba.gba_npins = NCT_NUM_PINS;
(void)config_found(sc->sc_dev, &gba, gpiobus_print, CFARGS_NONE);
}
static int
nct_detach(device_t self, int flags)
{
struct nct_softc *sc = device_private(self);
bus_space_unmap(sc->sc_iot, sc->sc_ioh, NCT_IOSIZE);
mutex_destroy(&sc->sc_lock);
return 0;
}
static u_int8_t
nct_rd(struct nct_softc *sc, int reg)
{
int dev;
KASSERT(mutex_owned(&sc->sc_lock));
dev = reg >> 8;
reg &= 0xff;
if (dev != sc->sc_curdev && dev != 0x00) {
sc->sc_curdev = dev;
sc->sc_curreg = reg;
nct_outb(sc, NCT_PORT_SELECT, GD_DEVSEL);
nct_outb(sc, NCT_PORT_DATA, dev);
nct_outb(sc, NCT_PORT_SELECT, reg);
return nct_inb(sc, NCT_PORT_DATA);
} else if (reg != sc->sc_curreg) {
sc->sc_curreg = reg;
nct_outb(sc, NCT_PORT_SELECT, reg);
return nct_inb(sc, NCT_PORT_DATA);
} else {
return nct_inb(sc, NCT_PORT_DATA);
}
}
static void
nct_wr(struct nct_softc *sc, int reg, u_int8_t data)
{
int dev;
KASSERT(mutex_owned(&sc->sc_lock));
dev = reg >> 8;
reg &= 0xff;
if (dev != sc->sc_curdev && dev != 0x00) {
sc->sc_curdev = dev;
sc->sc_curreg = reg;
nct_outb(sc, NCT_PORT_SELECT, GD_DEVSEL);
nct_outb(sc, NCT_PORT_DATA, dev);
nct_outb(sc, NCT_PORT_SELECT, reg);
nct_outb(sc, NCT_PORT_DATA, data);
} else if (reg != sc->sc_curreg) {
sc->sc_curreg = reg;
nct_outb(sc, NCT_PORT_SELECT, reg);
nct_outb(sc, NCT_PORT_DATA, data);
} else {
nct_outb(sc, NCT_PORT_DATA, data);
}
}
static struct nct_bank *
nct_sel(struct nct_softc *sc, int pin, u_int8_t *mask)
{
struct nct_bank *nb;
KASSERT(pin >= 0 && pin < NCT_NUM_PINS);
nb = &sc->sc_bank[pin >> 3];
KASSERT(pin >= nb->nb_firstpin);
KASSERT((pin & 7) < nb->nb_numpins);
*mask = (u_int8_t)(1 << (pin & 7)) & nb->nb_enabled;
return nb;
}
static int
nct_gpio_read(void *arg, int pin)
{
struct nct_softc *sc = arg;
struct nct_bank *nb;
u_int8_t data, mask;
int rv = GPIO_PIN_LOW;
nb = nct_sel(sc, pin, &mask);
if (__predict_true(mask != 0)) {
mutex_spin_enter(&sc->sc_lock);
data = nct_rd(sc, nb->nb_reg_data);
if ((data & mask) != 0) {
rv = GPIO_PIN_HIGH;
}
mutex_spin_exit(&sc->sc_lock);
}
return rv;
}
static void
nct_gpio_write(void *arg, int pin, int val)
{
struct nct_softc *sc = arg;
struct nct_bank *nb;
u_int8_t data, mask;
nb = nct_sel(sc, pin, &mask);
if (__predict_true(mask != 0)) {
mutex_spin_enter(&sc->sc_lock);
data = nct_rd(sc, nb->nb_reg_data);
if (val == GPIO_PIN_LOW) {
data &= ~mask;
} else if (val == GPIO_PIN_HIGH) {
data |= mask;
}
nct_wr(sc, nb->nb_reg_data, data);
mutex_spin_exit(&sc->sc_lock);
}
}
static void
nct_gpio_ctl(void *arg, int pin, int flg)
{
struct nct_softc *sc = arg;
struct nct_bank *nb;
u_int8_t data, mask;
nb = nct_sel(sc, pin, &mask);
if (__predict_false(mask == 0)) {
return;
}
mutex_spin_enter(&sc->sc_lock);
data = nb->nb_val_dir;
if ((flg & (GPIO_PIN_INPUT | GPIO_PIN_TRISTATE)) != 0) {
data |= mask;
}
if (data != nb->nb_val_dir) {
nct_wr(sc, nb->nb_reg_dir, data);
nb->nb_val_dir = data;
}
data = nb->nb_val_inv;
if ((flg & (GPIO_PIN_OUTPUT | GPIO_PIN_INVOUT)) ==
(GPIO_PIN_OUTPUT | GPIO_PIN_INVOUT) ||
(flg & (GPIO_PIN_INPUT | GPIO_PIN_INVIN)) ==
(GPIO_PIN_INPUT | GPIO_PIN_INVIN)) {
data |= mask;
} else {
data &= ~mask;
}
if (data != nb->nb_val_inv) {
nct_wr(sc, nb->nb_reg_inv, data);
nb->nb_val_inv = data;
}
data = nb->nb_val_mode;
if ((flg & GPIO_PIN_PUSHPULL) != 0) {
data |= mask;
} else {
data &= ~mask;
}
if (data != nb->nb_val_mode) {
nct_wr(sc, nb->nb_reg_mode, data);
nb->nb_val_mode = data;
}
data = nb->nb_val_dir;
if ((flg & (GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE)) == GPIO_PIN_OUTPUT) {
data &= ~mask;
}
if (data != nb->nb_val_dir) {
nct_wr(sc, nb->nb_reg_dir, data);
nb->nb_val_dir = data;
}
mutex_spin_exit(&sc->sc_lock);
}