Home | History | Annotate | Line # | Download | only in isa
nsclpcsio_isa.c revision 1.16
      1  1.16   xtraeme /* $NetBSD: nsclpcsio_isa.c,v 1.16 2007/02/18 23:34:55 xtraeme Exp $ */
      2   1.1  drochner 
      3   1.1  drochner /*
      4   1.1  drochner  * Copyright (c) 2002
      5   1.1  drochner  * 	Matthias Drochner.  All rights reserved.
      6   1.1  drochner  *
      7   1.1  drochner  * Redistribution and use in source and binary forms, with or without
      8   1.1  drochner  * modification, are permitted provided that the following conditions
      9   1.1  drochner  * are met:
     10   1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     11   1.1  drochner  *    notice, this list of conditions, and the following disclaimer.
     12   1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  drochner  *    documentation and/or other materials provided with the distribution.
     15   1.1  drochner  *
     16   1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17   1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20   1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  drochner  * SUCH DAMAGE.
     27   1.1  drochner  */
     28   1.1  drochner 
     29   1.1  drochner #include <sys/cdefs.h>
     30  1.16   xtraeme __KERNEL_RCSID(0, "$NetBSD: nsclpcsio_isa.c,v 1.16 2007/02/18 23:34:55 xtraeme Exp $");
     31   1.1  drochner 
     32   1.1  drochner #include <sys/param.h>
     33   1.1  drochner #include <sys/systm.h>
     34   1.1  drochner #include <sys/device.h>
     35  1.16   xtraeme #include <sys/mutex.h>
     36  1.11  jmcneill #include <sys/gpio.h>
     37   1.1  drochner #include <machine/bus.h>
     38   1.1  drochner 
     39   1.1  drochner #include <dev/isa/isareg.h>
     40   1.1  drochner #include <dev/isa/isavar.h>
     41  1.12  drochner #include "gpio.h"
     42  1.12  drochner #if NGPIO > 0
     43  1.11  jmcneill #include <dev/gpio/gpiovar.h>
     44  1.12  drochner #endif
     45   1.1  drochner #include <dev/sysmon/sysmonvar.h>
     46   1.1  drochner 
     47   1.7     perry static int nsclpcsio_isa_match(struct device *, struct cfdata *, void *);
     48   1.7     perry static void nsclpcsio_isa_attach(struct device *, struct device *, void *);
     49   1.1  drochner 
     50  1.11  jmcneill #define GPIO_NPINS 29
     51  1.11  jmcneill #define	SIO_GPIO_CONF_OUTPUTEN	(1 << 0)
     52  1.11  jmcneill #define	SIO_GPIO_CONF_PUSHPULL	(1 << 1)
     53  1.11  jmcneill #define	SIO_GPIO_CONF_PULLUP	(1 << 2)
     54  1.11  jmcneill 
     55   1.1  drochner struct nsclpcsio_softc {
     56   1.1  drochner 	struct device sc_dev;
     57  1.11  jmcneill 	bus_space_tag_t sc_iot, sc_gpio_iot, sc_tms_iot;
     58  1.11  jmcneill 	bus_space_handle_t sc_ioh, sc_gpio_ioh, sc_tms_ioh;
     59   1.1  drochner 
     60   1.1  drochner 	struct envsys_tre_data sc_data[3];
     61   1.1  drochner 	struct envsys_basic_info sc_info[3];
     62   1.1  drochner 	struct sysmon_envsys sc_sysmon;
     63  1.16   xtraeme 	kmutex_t sc_lock;
     64  1.11  jmcneill 
     65  1.12  drochner #if NGPIO > 0
     66  1.11  jmcneill 	/* GPIO */
     67  1.11  jmcneill 	struct gpio_chipset_tag sc_gpio_gc;
     68  1.11  jmcneill 	struct gpio_pin sc_gpio_pins[GPIO_NPINS];
     69  1.12  drochner #endif
     70   1.1  drochner };
     71   1.1  drochner 
     72  1.11  jmcneill #define GPIO_READ(sc, reg)			\
     73  1.11  jmcneill 	bus_space_read_1((sc)->sc_gpio_iot,	\
     74  1.11  jmcneill 	    (sc)->sc_gpio_ioh, (reg))
     75  1.11  jmcneill #define GPIO_WRITE(sc, reg, val)		\
     76  1.11  jmcneill 	bus_space_write_1((sc)->sc_gpio_iot,	\
     77  1.11  jmcneill 	    (sc)->sc_gpio_ioh, (reg), (val))
     78  1.11  jmcneill 
     79   1.5  drochner CFATTACH_DECL(nsclpcsio_isa, sizeof(struct nsclpcsio_softc),
     80   1.4   thorpej     nsclpcsio_isa_match, nsclpcsio_isa_attach, NULL, NULL);
     81   1.1  drochner 
     82   1.1  drochner static const struct envsys_range tms_ranges[] = {
     83   1.1  drochner 	{ 0, 2, ENVSYS_STEMP },
     84   1.1  drochner };
     85   1.1  drochner 
     86   1.1  drochner static u_int8_t nsread(bus_space_tag_t, bus_space_handle_t, int);
     87   1.1  drochner static void nswrite(bus_space_tag_t, bus_space_handle_t, int, u_int8_t);
     88   1.1  drochner static int nscheck(bus_space_tag_t, int);
     89   1.1  drochner 
     90   1.1  drochner static void tms_update(struct nsclpcsio_softc *, int);
     91   1.1  drochner static int tms_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
     92   1.1  drochner static int tms_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
     93   1.1  drochner 
     94  1.12  drochner #if NGPIO > 0
     95  1.11  jmcneill static void nsclpcsio_gpio_init(struct nsclpcsio_softc *);
     96  1.11  jmcneill static void nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *, int);
     97  1.11  jmcneill static void nsclpcsio_gpio_pin_write(void *, int, int);
     98  1.11  jmcneill static int nsclpcsio_gpio_pin_read(void *, int);
     99  1.11  jmcneill static void nsclpcsio_gpio_pin_ctl(void *, int, int);
    100  1.12  drochner #endif
    101  1.11  jmcneill 
    102   1.1  drochner static u_int8_t
    103   1.1  drochner nsread(iot, ioh, idx)
    104   1.1  drochner 	bus_space_tag_t iot;
    105   1.1  drochner 	bus_space_handle_t ioh;
    106   1.1  drochner 	int idx;
    107   1.1  drochner {
    108   1.1  drochner 
    109   1.1  drochner 	bus_space_write_1(iot, ioh, 0, idx);
    110   1.1  drochner 	return (bus_space_read_1(iot, ioh, 1));
    111   1.1  drochner }
    112   1.1  drochner 
    113   1.1  drochner static void
    114   1.1  drochner nswrite(iot, ioh, idx, data)
    115   1.1  drochner 	bus_space_tag_t iot;
    116   1.1  drochner 	bus_space_handle_t ioh;
    117   1.1  drochner 	int idx;
    118   1.1  drochner 	u_int8_t data;
    119   1.1  drochner {
    120   1.1  drochner 
    121   1.1  drochner 	bus_space_write_1(iot, ioh, 0, idx);
    122   1.1  drochner 	bus_space_write_1(iot, ioh, 1, data);
    123   1.1  drochner }
    124   1.1  drochner 
    125   1.1  drochner static int
    126   1.1  drochner nscheck(iot, base)
    127   1.1  drochner 	bus_space_tag_t iot;
    128   1.1  drochner 	int base;
    129   1.1  drochner {
    130   1.1  drochner 	bus_space_handle_t ioh;
    131   1.1  drochner 	int rv = 0;
    132   1.1  drochner 
    133   1.1  drochner 	if (bus_space_map(iot, base, 2, 0, &ioh))
    134   1.1  drochner 		return (0);
    135   1.1  drochner 
    136   1.1  drochner 	/* XXX this is for PC87366 only for now */
    137   1.1  drochner 	if (nsread(iot, ioh, 0x20) == 0xe9)
    138   1.1  drochner 		rv = 1;
    139   1.1  drochner 
    140   1.1  drochner 	bus_space_unmap(iot, ioh, 2);
    141   1.1  drochner 	return (rv);
    142   1.1  drochner }
    143   1.1  drochner 
    144   1.1  drochner static int
    145  1.15  christos nsclpcsio_isa_match(struct device *parent,
    146  1.15  christos     struct cfdata *match, void *aux)
    147   1.1  drochner {
    148   1.1  drochner 	struct isa_attach_args *ia = aux;
    149   1.1  drochner 	int iobase;
    150   1.1  drochner 
    151   1.1  drochner 	if (ISA_DIRECT_CONFIG(ia))
    152   1.1  drochner 		return (0);
    153   1.1  drochner 
    154   1.6  drochner 	if (ia->ia_nio > 0 && ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
    155   1.1  drochner 		/* XXX check for legal iobase ??? */
    156   1.1  drochner 		if (nscheck(ia->ia_iot, ia->ia_io[0].ir_addr)) {
    157   1.1  drochner 			iobase = ia->ia_io[0].ir_addr;
    158   1.1  drochner 			goto found;
    159   1.1  drochner 		}
    160   1.5  drochner 		return (0);
    161   1.1  drochner 	}
    162   1.1  drochner 
    163   1.1  drochner 	/* PC87366 has two possible locations depending on wiring */
    164   1.1  drochner 	if (nscheck(ia->ia_iot, 0x2e)) {
    165   1.1  drochner 		iobase = 0x2e;
    166   1.1  drochner 		goto found;
    167   1.1  drochner 	}
    168   1.1  drochner 	if (nscheck(ia->ia_iot, 0x4e)) {
    169   1.1  drochner 		iobase = 0x4e;
    170   1.1  drochner 		goto found;
    171   1.1  drochner 	}
    172   1.1  drochner 	return (0);
    173   1.1  drochner 
    174   1.1  drochner found:
    175   1.1  drochner 	ia->ia_nio = 1;
    176   1.1  drochner 	ia->ia_io[0].ir_addr = iobase;
    177   1.1  drochner 	ia->ia_io[0].ir_size = 2;
    178   1.1  drochner 	ia->ia_niomem = 0;
    179   1.1  drochner 	ia->ia_nirq = 0;
    180   1.1  drochner 	ia->ia_ndrq = 0;
    181   1.1  drochner 	return (1);
    182   1.1  drochner }
    183   1.1  drochner 
    184   1.1  drochner static void
    185  1.15  christos nsclpcsio_isa_attach(struct device *parent, struct device *self,
    186  1.14  christos     void *aux)
    187   1.1  drochner {
    188   1.1  drochner 	struct nsclpcsio_softc *sc = (void *)self;
    189   1.1  drochner 	struct isa_attach_args *ia = aux;
    190  1.12  drochner #if NGPIO > 0
    191  1.11  jmcneill 	struct gpiobus_attach_args gba;
    192  1.12  drochner #endif
    193   1.1  drochner 	bus_space_tag_t iot;
    194   1.1  drochner 	bus_space_handle_t ioh;
    195   1.1  drochner 	u_int8_t val;
    196  1.11  jmcneill 	int tms_iobase, gpio_iobase = 0;
    197   1.1  drochner 	int i;
    198   1.1  drochner 
    199   1.1  drochner 	sc->sc_iot = iot = ia->ia_iot;
    200   1.1  drochner 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh)) {
    201   1.1  drochner 		printf(": can't map i/o space\n");
    202   1.1  drochner 		return;
    203   1.1  drochner 	}
    204   1.1  drochner 	sc->sc_ioh = ioh;
    205   1.1  drochner 	printf(": NSC PC87366 rev. %d\n", nsread(iot, ioh, 0x27));
    206   1.1  drochner 
    207  1.16   xtraeme 	mutex_init(&sc->sc_lock, MUTEX_DRIVER, IPL_NONE);
    208  1.11  jmcneill 
    209  1.11  jmcneill 	nswrite(iot, ioh, 0x07, 0x07); /* select gpio */
    210  1.11  jmcneill 
    211  1.11  jmcneill 	val = nsread(iot, ioh, 0x30); /* control register */
    212  1.11  jmcneill 	if (!(val & 1)) {
    213  1.11  jmcneill 		printf("%s: GPIO disabled\n", sc->sc_dev.dv_xname);
    214  1.11  jmcneill 	} else {
    215  1.11  jmcneill 		gpio_iobase = (nsread(iot, ioh, 0x60) << 8) |
    216  1.11  jmcneill 			       nsread(iot, ioh, 0x61);
    217  1.11  jmcneill 		sc->sc_gpio_iot = iot;
    218  1.11  jmcneill 		if (bus_space_map(iot, gpio_iobase, 0x2c, 0,
    219  1.11  jmcneill 		    &sc->sc_gpio_ioh)) {
    220  1.11  jmcneill 			printf("%s: can't map GPIO i/o space\n",
    221  1.11  jmcneill 			    sc->sc_dev.dv_xname);
    222  1.11  jmcneill 			return;
    223  1.11  jmcneill 		}
    224  1.11  jmcneill 		printf("%s: GPIO at 0x%x\n", sc->sc_dev.dv_xname, gpio_iobase);
    225  1.11  jmcneill 
    226  1.12  drochner #if NGPIO > 0
    227  1.11  jmcneill 		nsclpcsio_gpio_init(sc);
    228  1.12  drochner #endif
    229  1.11  jmcneill 	}
    230  1.11  jmcneill 
    231   1.1  drochner 	nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
    232   1.1  drochner 
    233   1.1  drochner 	val = nsread(iot, ioh, 0x30); /* control register */
    234   1.1  drochner 	if (!(val & 1)) {
    235   1.1  drochner 		printf("%s: TMS disabled\n", sc->sc_dev.dv_xname);
    236   1.1  drochner 		return;
    237   1.1  drochner 	}
    238   1.1  drochner 
    239   1.1  drochner 	tms_iobase = (nsread(iot, ioh, 0x60) << 8) | nsread(iot, ioh, 0x61);
    240   1.1  drochner 	sc->sc_tms_iot = iot;
    241   1.1  drochner 	if (bus_space_map(iot, tms_iobase, 16, 0, &sc->sc_tms_ioh)) {
    242   1.1  drochner 		printf("%s: can't map TMS i/o space\n", sc->sc_dev.dv_xname);
    243   1.1  drochner 		return;
    244   1.1  drochner 	}
    245   1.1  drochner 	printf("%s: TMS at 0x%x\n", sc->sc_dev.dv_xname, tms_iobase);
    246   1.1  drochner 
    247   1.1  drochner 	if (bus_space_read_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08) & 1) {
    248   1.1  drochner 		printf("%s: TMS in standby mode\n", sc->sc_dev.dv_xname);
    249  1.10  drochner 
    250   1.9  drochner 		/* Wake up the TMS and enable all temperature sensors. */
    251   1.9  drochner 		bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08, 0x00);
    252   1.9  drochner 		bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x00);
    253   1.9  drochner 		bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
    254   1.9  drochner 		bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x01);
    255   1.9  drochner 		bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
    256   1.9  drochner 		bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x02);
    257   1.9  drochner 		bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
    258  1.10  drochner 
    259  1.10  drochner 		if (!(bus_space_read_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08)
    260  1.10  drochner 		      & 1)) {
    261   1.9  drochner 			printf("%s: TMS awoken\n", sc->sc_dev.dv_xname);
    262   1.9  drochner 		} else {
    263   1.9  drochner 			return;
    264   1.9  drochner 		}
    265   1.1  drochner 	}
    266   1.1  drochner 
    267   1.1  drochner 	/* Initialize sensor meta data */
    268   1.1  drochner 	for (i = 0; i < 3; i++) {
    269   1.1  drochner 		sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
    270   1.1  drochner 		sc->sc_data[i].units = sc->sc_info[i].units = ENVSYS_STEMP;
    271   1.1  drochner 	}
    272   1.1  drochner 	strcpy(sc->sc_info[0].desc, "TSENS1");
    273   1.1  drochner 	strcpy(sc->sc_info[1].desc, "TSENS2");
    274   1.1  drochner 	strcpy(sc->sc_info[2].desc, "TNSC");
    275   1.1  drochner 
    276   1.1  drochner 	/* Get initial set of sensor values. */
    277   1.1  drochner 	for (i = 0; i < 3; i++)
    278   1.1  drochner 		tms_update(sc, i);
    279   1.1  drochner 
    280   1.1  drochner 	/*
    281   1.1  drochner 	 * Hook into the System Monitor.
    282   1.1  drochner 	 */
    283   1.1  drochner 	sc->sc_sysmon.sme_ranges = tms_ranges;
    284   1.1  drochner 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    285   1.1  drochner 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    286   1.1  drochner 	sc->sc_sysmon.sme_cookie = sc;
    287   1.1  drochner 
    288   1.1  drochner 	sc->sc_sysmon.sme_gtredata = tms_gtredata;
    289   1.1  drochner 	sc->sc_sysmon.sme_streinfo = tms_streinfo;
    290   1.1  drochner 
    291   1.1  drochner 	sc->sc_sysmon.sme_nsensors = 3;
    292   1.1  drochner 	sc->sc_sysmon.sme_envsys_version = 1000;
    293   1.1  drochner 
    294   1.1  drochner 	if (sysmon_envsys_register(&sc->sc_sysmon))
    295   1.1  drochner 		printf("%s: unable to register with sysmon\n",
    296   1.1  drochner 		    sc->sc_dev.dv_xname);
    297  1.11  jmcneill 
    298  1.12  drochner #if NGPIO > 0
    299  1.11  jmcneill 	/* attach GPIO framework */
    300  1.11  jmcneill 	if (gpio_iobase != 0) {
    301  1.11  jmcneill 		gba.gba_gc = &sc->sc_gpio_gc;
    302  1.11  jmcneill 		gba.gba_pins = sc->sc_gpio_pins;
    303  1.11  jmcneill 		gba.gba_npins = GPIO_NPINS;
    304  1.12  drochner 		config_found_ia(&sc->sc_dev, "gpiobus", &gba, NULL);
    305  1.11  jmcneill 	}
    306  1.12  drochner #endif
    307  1.11  jmcneill 	return;
    308   1.1  drochner }
    309   1.1  drochner 
    310   1.1  drochner static void
    311   1.1  drochner tms_update(sc, chan)
    312   1.1  drochner 	struct nsclpcsio_softc *sc;
    313   1.1  drochner 	int chan;
    314   1.1  drochner {
    315   1.1  drochner 	bus_space_tag_t iot = sc->sc_tms_iot;
    316   1.1  drochner 	bus_space_handle_t ioh = sc->sc_tms_ioh;
    317   1.1  drochner 	u_int8_t status;
    318   1.1  drochner 	int8_t temp, ctemp; /* signed!! */
    319   1.1  drochner 
    320  1.16   xtraeme 	mutex_enter(&sc->sc_lock);
    321  1.11  jmcneill 
    322  1.11  jmcneill 	nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
    323  1.11  jmcneill 
    324   1.1  drochner 	bus_space_write_1(iot, ioh, 0x09, chan); /* select */
    325   1.1  drochner 
    326   1.1  drochner 	status = bus_space_read_1(iot, ioh, 0x0a); /* config/status */
    327   1.1  drochner 	if (status & 0x01) {
    328   1.1  drochner 		/* enabled */
    329   1.1  drochner 		sc->sc_info[chan].validflags = ENVSYS_FVALID;
    330   1.1  drochner 	}else {
    331   1.1  drochner 		sc->sc_info[chan].validflags = 0;
    332  1.16   xtraeme 		mutex_exit(&sc->sc_lock);
    333   1.1  drochner 		return;
    334   1.1  drochner 	}
    335   1.1  drochner 
    336   1.1  drochner 	/*
    337   1.1  drochner 	 * If the channel is enabled, it is considered valid.
    338   1.1  drochner 	 * An "open circuit" might be temporary.
    339   1.1  drochner 	 */
    340   1.1  drochner 	sc->sc_data[chan].validflags = ENVSYS_FVALID;
    341   1.1  drochner 	if (status & 0x40) {
    342   1.1  drochner 		/*
    343   1.1  drochner 		 * open circuit
    344   1.1  drochner 		 * XXX should have a warning for it
    345   1.1  drochner 		 */
    346   1.1  drochner 		sc->sc_data[chan].warnflags = ENVSYS_WARN_OK; /* XXX */
    347  1.16   xtraeme 		mutex_exit(&sc->sc_lock);
    348   1.1  drochner 		return;
    349   1.1  drochner 	}
    350   1.1  drochner 
    351   1.1  drochner 	/* get current temperature in signed degree celsius */
    352   1.1  drochner 	temp = bus_space_read_1(iot, ioh, 0x0b);
    353   1.1  drochner 	sc->sc_data[chan].cur.data_us = (int)temp * 1000000 + 273150000;
    354   1.1  drochner 	sc->sc_data[chan].validflags |= ENVSYS_FCURVALID;
    355   1.1  drochner 
    356   1.1  drochner 	if (status & 0x0e) { /* any temperature warning? */
    357   1.1  drochner 		/*
    358   1.1  drochner 		 * XXX the chip documentation is a bit fuzzy - it doesn't state
    359   1.1  drochner 		 * that the hardware OTS output depends on the "overtemp"
    360   1.1  drochner 		 * warning bit.
    361   1.1  drochner 		 * It seems the output gets cleared if the warning bit is reset.
    362   1.1  drochner 		 * This sucks.
    363   1.1  drochner 		 * The hardware might do something useful with output pins, eg
    364   1.1  drochner 		 * throttling the CPU, so we must do the comparision in
    365   1.1  drochner 		 * software, and only reset the bits if the reason is gone.
    366   1.1  drochner 		 */
    367   1.1  drochner 		if (status & 0x02) { /* low limit */
    368   1.1  drochner 			sc->sc_data[chan].warnflags = ENVSYS_WARN_UNDER;
    369   1.1  drochner 			/* read low limit */
    370   1.1  drochner 			ctemp = bus_space_read_1(iot, ioh, 0x0d);
    371   1.1  drochner 			if (temp <= ctemp) /* still valid, don't reset */
    372   1.1  drochner 				status &= ~0x02;
    373   1.1  drochner 		}
    374   1.1  drochner 		if (status & 0x04) { /* high limit */
    375   1.1  drochner 			sc->sc_data[chan].warnflags = ENVSYS_WARN_OVER;
    376   1.1  drochner 			/* read high limit */
    377   1.1  drochner 			ctemp = bus_space_read_1(iot, ioh, 0x0c);
    378   1.1  drochner 			if (temp >= ctemp) /* still valid, don't reset */
    379   1.1  drochner 				status &= ~0x04;
    380   1.1  drochner 		}
    381   1.1  drochner 		if (status & 0x08) { /* overtemperature */
    382   1.1  drochner 			sc->sc_data[chan].warnflags = ENVSYS_WARN_CRITOVER;
    383   1.1  drochner 			/* read overtemperature limit */
    384   1.1  drochner 			ctemp = bus_space_read_1(iot, ioh, 0x0e);
    385   1.1  drochner 			if (temp >= ctemp) /* still valid, don't reset */
    386   1.1  drochner 				status &= ~0x08;
    387   1.1  drochner 		}
    388   1.1  drochner 
    389   1.1  drochner 		/* clear outdated warnings */
    390   1.1  drochner 		if (status & 0x0e)
    391   1.1  drochner 			bus_space_write_1(iot, ioh, 0x0a, status);
    392   1.1  drochner 	}
    393  1.11  jmcneill 
    394  1.16   xtraeme 	mutex_exit(&sc->sc_lock);
    395  1.11  jmcneill 
    396  1.11  jmcneill 	return;
    397   1.1  drochner }
    398   1.1  drochner 
    399   1.1  drochner static int
    400   1.1  drochner tms_gtredata(sme, data)
    401   1.1  drochner 	struct sysmon_envsys *sme;
    402   1.1  drochner 	struct envsys_tre_data *data;
    403   1.1  drochner {
    404   1.1  drochner 	struct nsclpcsio_softc *sc = sme->sme_cookie;
    405   1.1  drochner 
    406   1.1  drochner 	tms_update(sc, data->sensor);
    407   1.1  drochner 
    408   1.1  drochner 	*data = sc->sc_data[data->sensor];
    409   1.1  drochner 	return (0);
    410   1.1  drochner }
    411   1.1  drochner 
    412   1.1  drochner static int
    413  1.15  christos tms_streinfo(struct sysmon_envsys *sme,
    414  1.14  christos     struct envsys_basic_info *info)
    415   1.1  drochner {
    416   1.1  drochner #if 0
    417   1.1  drochner 	struct nsclpcsio_softc *sc = sme->sme_cookie;
    418   1.1  drochner #endif
    419   1.1  drochner 	/* XXX Not implemented */
    420   1.1  drochner 	info->validflags = 0;
    421   1.8     perry 
    422   1.1  drochner 	return (0);
    423   1.1  drochner }
    424  1.11  jmcneill 
    425  1.12  drochner #if NGPIO > 0
    426  1.11  jmcneill static void
    427  1.11  jmcneill nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *sc, int pin)
    428  1.11  jmcneill {
    429  1.11  jmcneill 	u_int8_t v;
    430  1.11  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    431  1.11  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    432  1.11  jmcneill 
    433  1.11  jmcneill 	v = ((pin / 8) << 4) | (pin % 8);
    434  1.11  jmcneill 
    435  1.11  jmcneill 	nswrite(iot, ioh, 0x07, 0x07); /* select gpio */
    436  1.11  jmcneill 	nswrite(iot, ioh, 0xf0, v);
    437  1.11  jmcneill 
    438  1.11  jmcneill 	return;
    439  1.11  jmcneill }
    440  1.11  jmcneill 
    441  1.11  jmcneill static void
    442  1.11  jmcneill nsclpcsio_gpio_init(struct nsclpcsio_softc *sc)
    443  1.11  jmcneill {
    444  1.11  jmcneill 	int i;
    445  1.11  jmcneill 
    446  1.11  jmcneill 	for (i = 0; i < GPIO_NPINS; i++) {
    447  1.11  jmcneill 		sc->sc_gpio_pins[i].pin_num = i;
    448  1.11  jmcneill 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
    449  1.11  jmcneill 		    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
    450  1.11  jmcneill 		    GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
    451  1.11  jmcneill 		    GPIO_PIN_PULLUP;
    452  1.11  jmcneill 		/* safe defaults */
    453  1.11  jmcneill 		sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
    454  1.11  jmcneill 		sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
    455  1.11  jmcneill 		nsclpcsio_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
    456  1.11  jmcneill 		nsclpcsio_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
    457  1.11  jmcneill 	}
    458  1.11  jmcneill 
    459  1.11  jmcneill 	/* create controller tag */
    460  1.11  jmcneill 	sc->sc_gpio_gc.gp_cookie = sc;
    461  1.11  jmcneill 	sc->sc_gpio_gc.gp_pin_read = nsclpcsio_gpio_pin_read;
    462  1.11  jmcneill 	sc->sc_gpio_gc.gp_pin_write = nsclpcsio_gpio_pin_write;
    463  1.11  jmcneill 	sc->sc_gpio_gc.gp_pin_ctl = nsclpcsio_gpio_pin_ctl;
    464  1.11  jmcneill }
    465  1.11  jmcneill 
    466  1.11  jmcneill static int
    467  1.11  jmcneill nsclpcsio_gpio_pin_read(void *aux, int pin)
    468  1.11  jmcneill {
    469  1.11  jmcneill 	struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
    470  1.11  jmcneill 	int port, shift, reg;
    471  1.11  jmcneill 	u_int8_t v;
    472  1.11  jmcneill 
    473  1.11  jmcneill 	reg = 0x00;
    474  1.11  jmcneill 	port = pin / 8;
    475  1.11  jmcneill 	shift = pin % 8;
    476  1.11  jmcneill 
    477  1.11  jmcneill 	switch (port) {
    478  1.11  jmcneill 	case 0: reg = 0x00; break;
    479  1.11  jmcneill 	case 1: reg = 0x04; break;
    480  1.11  jmcneill 	case 2: reg = 0x08; break;
    481  1.11  jmcneill 	case 3: reg = 0x0a; break;
    482  1.11  jmcneill 	}
    483  1.11  jmcneill 
    484  1.11  jmcneill 	v = GPIO_READ(sc, reg);
    485  1.11  jmcneill 
    486  1.11  jmcneill 	return ((v >> shift) & 0x1);
    487  1.11  jmcneill }
    488  1.11  jmcneill 
    489  1.11  jmcneill static void
    490  1.11  jmcneill nsclpcsio_gpio_pin_write(void *aux, int pin, int v)
    491  1.11  jmcneill {
    492  1.11  jmcneill 	struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
    493  1.11  jmcneill 	int port, shift, reg;
    494  1.11  jmcneill 	u_int8_t d;
    495  1.11  jmcneill 
    496  1.11  jmcneill 	port = pin / 8;
    497  1.11  jmcneill 	shift = pin % 8;
    498  1.11  jmcneill 
    499  1.11  jmcneill 	switch (port) {
    500  1.11  jmcneill 	case 0: reg = 0x00; break;
    501  1.11  jmcneill 	case 1: reg = 0x04; break;
    502  1.11  jmcneill 	case 2: reg = 0x08; break;
    503  1.11  jmcneill 	case 3: reg = 0x0a; break;
    504  1.11  jmcneill 	default: reg = 0x00; break; /* shouldn't happen */
    505  1.11  jmcneill 	}
    506  1.11  jmcneill 
    507  1.11  jmcneill 	d = GPIO_READ(sc, reg);
    508  1.11  jmcneill 	if (v == 0)
    509  1.11  jmcneill 		d &= ~(1 << shift);
    510  1.11  jmcneill 	else if (v == 1)
    511  1.11  jmcneill 		d |= (1 << shift);
    512  1.11  jmcneill 	GPIO_WRITE(sc, reg, d);
    513  1.11  jmcneill 
    514  1.11  jmcneill 	return;
    515  1.11  jmcneill }
    516  1.11  jmcneill 
    517  1.11  jmcneill void
    518  1.11  jmcneill nsclpcsio_gpio_pin_ctl(void *aux, int pin, int flags)
    519  1.11  jmcneill {
    520  1.11  jmcneill 	struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
    521  1.11  jmcneill 	u_int8_t conf;
    522  1.11  jmcneill 
    523  1.16   xtraeme 	mutex_enter(&sc->sc_lock);
    524  1.11  jmcneill 
    525  1.11  jmcneill 	nswrite(sc->sc_iot, sc->sc_ioh, 0x07, 0x07); /* select gpio */
    526  1.11  jmcneill 	nsclpcsio_gpio_pin_select(sc, pin);
    527  1.11  jmcneill 	conf = nsread(sc->sc_iot, sc->sc_ioh, 0xf1);
    528  1.11  jmcneill 
    529  1.11  jmcneill 	conf &= ~(SIO_GPIO_CONF_OUTPUTEN | SIO_GPIO_CONF_PUSHPULL |
    530  1.11  jmcneill 	    SIO_GPIO_CONF_PULLUP);
    531  1.11  jmcneill 	if ((flags & GPIO_PIN_TRISTATE) == 0)
    532  1.11  jmcneill 		conf |= SIO_GPIO_CONF_OUTPUTEN;
    533  1.11  jmcneill 	if (flags & GPIO_PIN_PUSHPULL)
    534  1.11  jmcneill 		conf |= SIO_GPIO_CONF_PUSHPULL;
    535  1.11  jmcneill 	if (flags & GPIO_PIN_PULLUP)
    536  1.11  jmcneill 		conf |= SIO_GPIO_CONF_PULLUP;
    537  1.11  jmcneill 
    538  1.11  jmcneill 	nswrite(sc->sc_iot, sc->sc_ioh, 0xf1, conf);
    539  1.11  jmcneill 
    540  1.16   xtraeme 	mutex_exit(&sc->sc_lock);
    541  1.11  jmcneill 
    542  1.11  jmcneill 	return;
    543  1.11  jmcneill }
    544  1.12  drochner #endif /* NGPIO */
    545