Attach to gpiobus. From OpenBSD.

This commit is contained in:
jmcneill 2005-09-27 02:56:27 +00:00
parent 4d7c3832dc
commit b8a32e9d53
2 changed files with 196 additions and 7 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: files.isa,v 1.140 2005/08/14 04:15:44 joff Exp $
# $NetBSD: files.isa,v 1.141 2005/09/27 02:56:27 jmcneill Exp $
#
# Config file and device description for machine-independent ISA code.
# Included by ports that need it. Requires that the SCSI files be
@ -484,7 +484,7 @@ attach joy at isa with joy_isa
file dev/isa/joy_isa.c joy_isa
# NSC PC87366 Super IO / monitor chip
device nsclpcsio: sysmon_envsys
device nsclpcsio: sysmon_envsys, gpiobus
attach nsclpcsio at isa with nsclpcsio_isa
file dev/isa/nsclpcsio_isa.c nsclpcsio_isa

View File

@ -1,4 +1,4 @@
/* $NetBSD: nsclpcsio_isa.c,v 1.10 2005/07/29 11:25:13 drochner Exp $ */
/* $NetBSD: nsclpcsio_isa.c,v 1.11 2005/09/27 02:56:27 jmcneill Exp $ */
/*
* Copyright (c) 2002
@ -27,30 +27,50 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nsclpcsio_isa.c,v 1.10 2005/07/29 11:25:13 drochner Exp $");
__KERNEL_RCSID(0, "$NetBSD: nsclpcsio_isa.c,v 1.11 2005/09/27 02:56:27 jmcneill Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/lock.h>
#include <sys/gpio.h>
#include <machine/bus.h>
#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/gpio/gpiovar.h>
#include <dev/sysmon/sysmonvar.h>
static int nsclpcsio_isa_match(struct device *, struct cfdata *, void *);
static void nsclpcsio_isa_attach(struct device *, struct device *, void *);
#define GPIO_NPINS 29
#define SIO_GPIO_CONF_OUTPUTEN (1 << 0)
#define SIO_GPIO_CONF_PUSHPULL (1 << 1)
#define SIO_GPIO_CONF_PULLUP (1 << 2)
struct nsclpcsio_softc {
struct device sc_dev;
bus_space_tag_t sc_iot, sc_tms_iot;
bus_space_handle_t sc_ioh, sc_tms_ioh;
bus_space_tag_t sc_iot, sc_gpio_iot, sc_tms_iot;
bus_space_handle_t sc_ioh, sc_gpio_ioh, sc_tms_ioh;
struct envsys_tre_data sc_data[3];
struct envsys_basic_info sc_info[3];
struct sysmon_envsys sc_sysmon;
struct simplelock sc_lock;
/* GPIO */
struct gpio_chipset_tag sc_gpio_gc;
struct gpio_pin sc_gpio_pins[GPIO_NPINS];
};
#define GPIO_READ(sc, reg) \
bus_space_read_1((sc)->sc_gpio_iot, \
(sc)->sc_gpio_ioh, (reg))
#define GPIO_WRITE(sc, reg, val) \
bus_space_write_1((sc)->sc_gpio_iot, \
(sc)->sc_gpio_ioh, (reg), (val))
CFATTACH_DECL(nsclpcsio_isa, sizeof(struct nsclpcsio_softc),
nsclpcsio_isa_match, nsclpcsio_isa_attach, NULL, NULL);
@ -66,6 +86,12 @@ static void tms_update(struct nsclpcsio_softc *, int);
static int tms_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
static int tms_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
static void nsclpcsio_gpio_init(struct nsclpcsio_softc *);
static void nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *, int);
static void nsclpcsio_gpio_pin_write(void *, int, int);
static int nsclpcsio_gpio_pin_read(void *, int);
static void nsclpcsio_gpio_pin_ctl(void *, int, int);
static u_int8_t
nsread(iot, ioh, idx)
bus_space_tag_t iot;
@ -157,10 +183,11 @@ nsclpcsio_isa_attach(parent, self, aux)
{
struct nsclpcsio_softc *sc = (void *)self;
struct isa_attach_args *ia = aux;
struct gpiobus_attach_args gba;
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_int8_t val;
int tms_iobase;
int tms_iobase, gpio_iobase = 0;
int i;
sc->sc_iot = iot = ia->ia_iot;
@ -171,6 +198,28 @@ nsclpcsio_isa_attach(parent, self, aux)
sc->sc_ioh = ioh;
printf(": NSC PC87366 rev. %d\n", nsread(iot, ioh, 0x27));
simple_lock_init(&sc->sc_lock);
nswrite(iot, ioh, 0x07, 0x07); /* select gpio */
val = nsread(iot, ioh, 0x30); /* control register */
if (!(val & 1)) {
printf("%s: GPIO disabled\n", sc->sc_dev.dv_xname);
} else {
gpio_iobase = (nsread(iot, ioh, 0x60) << 8) |
nsread(iot, ioh, 0x61);
sc->sc_gpio_iot = iot;
if (bus_space_map(iot, gpio_iobase, 0x2c, 0,
&sc->sc_gpio_ioh)) {
printf("%s: can't map GPIO i/o space\n",
sc->sc_dev.dv_xname);
return;
}
printf("%s: GPIO at 0x%x\n", sc->sc_dev.dv_xname, gpio_iobase);
nsclpcsio_gpio_init(sc);
}
nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
val = nsread(iot, ioh, 0x30); /* control register */
@ -237,6 +286,17 @@ nsclpcsio_isa_attach(parent, self, aux)
if (sysmon_envsys_register(&sc->sc_sysmon))
printf("%s: unable to register with sysmon\n",
sc->sc_dev.dv_xname);
/* attach GPIO framework */
if (gpio_iobase != 0) {
gba.gba_name = "gpio";
gba.gba_gc = &sc->sc_gpio_gc;
gba.gba_pins = sc->sc_gpio_pins;
gba.gba_npins = GPIO_NPINS;
config_found(&sc->sc_dev, &gba, NULL);
}
return;
}
static void
@ -249,6 +309,10 @@ tms_update(sc, chan)
u_int8_t status;
int8_t temp, ctemp; /* signed!! */
simple_lock(&sc->sc_lock);
nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
bus_space_write_1(iot, ioh, 0x09, chan); /* select */
status = bus_space_read_1(iot, ioh, 0x0a); /* config/status */
@ -257,6 +321,7 @@ tms_update(sc, chan)
sc->sc_info[chan].validflags = ENVSYS_FVALID;
}else {
sc->sc_info[chan].validflags = 0;
simple_unlock(&sc->sc_lock);
return;
}
@ -271,6 +336,7 @@ tms_update(sc, chan)
* XXX should have a warning for it
*/
sc->sc_data[chan].warnflags = ENVSYS_WARN_OK; /* XXX */
simple_unlock(&sc->sc_lock);
return;
}
@ -316,6 +382,10 @@ tms_update(sc, chan)
if (status & 0x0e)
bus_space_write_1(iot, ioh, 0x0a, status);
}
simple_unlock(&sc->sc_lock);
return;
}
static int
@ -344,3 +414,122 @@ tms_streinfo(sme, info)
return (0);
}
static void
nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *sc, int pin)
{
u_int8_t v;
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
v = ((pin / 8) << 4) | (pin % 8);
nswrite(iot, ioh, 0x07, 0x07); /* select gpio */
nswrite(iot, ioh, 0xf0, v);
return;
}
static void
nsclpcsio_gpio_init(struct nsclpcsio_softc *sc)
{
int i;
for (i = 0; i < GPIO_NPINS; i++) {
sc->sc_gpio_pins[i].pin_num = i;
sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
GPIO_PIN_PULLUP;
/* safe defaults */
sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
nsclpcsio_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
nsclpcsio_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
}
/* create controller tag */
sc->sc_gpio_gc.gp_cookie = sc;
sc->sc_gpio_gc.gp_pin_read = nsclpcsio_gpio_pin_read;
sc->sc_gpio_gc.gp_pin_write = nsclpcsio_gpio_pin_write;
sc->sc_gpio_gc.gp_pin_ctl = nsclpcsio_gpio_pin_ctl;
}
static int
nsclpcsio_gpio_pin_read(void *aux, int pin)
{
struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
int port, shift, reg;
u_int8_t v;
reg = 0x00;
port = pin / 8;
shift = pin % 8;
switch (port) {
case 0: reg = 0x00; break;
case 1: reg = 0x04; break;
case 2: reg = 0x08; break;
case 3: reg = 0x0a; break;
}
v = GPIO_READ(sc, reg);
return ((v >> shift) & 0x1);
}
static void
nsclpcsio_gpio_pin_write(void *aux, int pin, int v)
{
struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
int port, shift, reg;
u_int8_t d;
port = pin / 8;
shift = pin % 8;
switch (port) {
case 0: reg = 0x00; break;
case 1: reg = 0x04; break;
case 2: reg = 0x08; break;
case 3: reg = 0x0a; break;
default: reg = 0x00; break; /* shouldn't happen */
}
d = GPIO_READ(sc, reg);
if (v == 0)
d &= ~(1 << shift);
else if (v == 1)
d |= (1 << shift);
GPIO_WRITE(sc, reg, d);
return;
}
void
nsclpcsio_gpio_pin_ctl(void *aux, int pin, int flags)
{
struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
u_int8_t conf;
simple_lock(&sc->sc_lock);
nswrite(sc->sc_iot, sc->sc_ioh, 0x07, 0x07); /* select gpio */
nsclpcsio_gpio_pin_select(sc, pin);
conf = nsread(sc->sc_iot, sc->sc_ioh, 0xf1);
conf &= ~(SIO_GPIO_CONF_OUTPUTEN | SIO_GPIO_CONF_PUSHPULL |
SIO_GPIO_CONF_PULLUP);
if ((flags & GPIO_PIN_TRISTATE) == 0)
conf |= SIO_GPIO_CONF_OUTPUTEN;
if (flags & GPIO_PIN_PUSHPULL)
conf |= SIO_GPIO_CONF_PUSHPULL;
if (flags & GPIO_PIN_PULLUP)
conf |= SIO_GPIO_CONF_PULLUP;
nswrite(sc->sc_iot, sc->sc_ioh, 0xf1, conf);
simple_unlock(&sc->sc_lock);
return;
}