Home | History | Annotate | Line # | Download | only in isa
nsclpcsio_isa.c revision 1.26
      1  1.26   xtraeme /* $NetBSD: nsclpcsio_isa.c,v 1.26 2008/04/04 09:49:49 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.21   xtraeme /*
     30  1.21   xtraeme  * National Semiconductor PC87366 LPC Super I/O driver.
     31  1.21   xtraeme  * Supported logical devices: GPIO, TMS, VLM.
     32  1.21   xtraeme  */
     33  1.21   xtraeme 
     34   1.1  drochner #include <sys/cdefs.h>
     35  1.26   xtraeme __KERNEL_RCSID(0, "$NetBSD: nsclpcsio_isa.c,v 1.26 2008/04/04 09:49:49 xtraeme Exp $");
     36   1.1  drochner 
     37   1.1  drochner #include <sys/param.h>
     38   1.1  drochner #include <sys/systm.h>
     39   1.1  drochner #include <sys/device.h>
     40  1.16   xtraeme #include <sys/mutex.h>
     41  1.11  jmcneill #include <sys/gpio.h>
     42  1.20        ad #include <sys/bus.h>
     43   1.1  drochner 
     44  1.19   xtraeme /* Don't use gpio for now in the LKM */
     45  1.19   xtraeme #ifdef _LKM
     46  1.19   xtraeme #undef NGPIO
     47  1.19   xtraeme #endif
     48  1.19   xtraeme 
     49   1.1  drochner #include <dev/isa/isareg.h>
     50   1.1  drochner #include <dev/isa/isavar.h>
     51  1.19   xtraeme 
     52  1.19   xtraeme #ifndef _LKM
     53  1.12  drochner #include "gpio.h"
     54  1.19   xtraeme #endif
     55  1.12  drochner #if NGPIO > 0
     56  1.11  jmcneill #include <dev/gpio/gpiovar.h>
     57  1.12  drochner #endif
     58   1.1  drochner #include <dev/sysmon/sysmonvar.h>
     59   1.1  drochner 
     60  1.21   xtraeme #define SIO_REG_SID	0x20	/* Super I/O ID */
     61  1.21   xtraeme #define SIO_SID_PC87366	0xE9	/* PC87366 is identified by 0xE9.*/
     62  1.21   xtraeme 
     63  1.21   xtraeme #define SIO_REG_SRID	0x27	/* Super I/O Revision */
     64  1.21   xtraeme 
     65  1.21   xtraeme #define SIO_REG_LDN	0x07	/* Logical Device Number */
     66  1.21   xtraeme #define SIO_LDN_FDC	0x00	/* Floppy Disk Controller (FDC) */
     67  1.21   xtraeme #define SIO_LDN_PP	0x01	/* Parallel Port (PP) */
     68  1.21   xtraeme #define SIO_LDN_SP2	0x02	/* Serial Port 2 with IR (SP2) */
     69  1.21   xtraeme #define SIO_LDN_SP1	0x03	/* Serial Port 1 (SP1) */
     70  1.21   xtraeme #define SIO_LDN_SWC	0x04	/* System Wake-Up Control (SWC) */
     71  1.21   xtraeme #define SIO_LDN_KBCM	0x05	/* Mouse Controller (KBC) */
     72  1.21   xtraeme #define SIO_LDN_KBCK	0x06	/* Keyboard Controller (KBC) */
     73  1.21   xtraeme #define SIO_LDN_GPIO	0x07	/* General-Purpose I/O (GPIO) Ports */
     74  1.21   xtraeme #define SIO_LDN_ACB	0x08	/* ACCESS.bus Interface (ACB) */
     75  1.21   xtraeme #define SIO_LDN_FSCM	0x09	/* Fan Speed Control and Monitor (FSCM) */
     76  1.21   xtraeme #define SIO_LDN_WDT	0x0A	/* WATCHDOG Timer (WDT) */
     77  1.21   xtraeme #define SIO_LDN_GMP	0x0B	/* Game Port (GMP) */
     78  1.21   xtraeme #define SIO_LDN_MIDI	0x0C	/* Musical Instrument Digital Interface */
     79  1.21   xtraeme #define SIO_LDN_VLM	0x0D	/* Voltage Level Monitor (VLM) */
     80  1.21   xtraeme #define SIO_LDN_TMS	0x0E	/* Temperature Sensor (TMS) */
     81  1.21   xtraeme 
     82  1.21   xtraeme #define SIO_REG_ACTIVE	0x30	/* Logical Device Activate Register */
     83  1.21   xtraeme #define SIO_ACTIVE_EN		0x01	/* enabled */
     84  1.21   xtraeme 
     85  1.21   xtraeme #define SIO_REG_IO_MSB	0x60	/* I/O Port Base, bits 15-8 */
     86  1.21   xtraeme #define SIO_REG_IO_LSB	0x61	/* I/O Port Base, bits 7-0 */
     87  1.21   xtraeme 
     88  1.21   xtraeme #define SIO_LDNUM	15	/* total number of logical devices */
     89  1.21   xtraeme 
     90  1.21   xtraeme /* Supported logical devices description */
     91  1.21   xtraeme static const struct {
     92  1.21   xtraeme 	const char *ld_name;
     93  1.21   xtraeme 	int ld_num;
     94  1.21   xtraeme 	int ld_iosize;
     95  1.21   xtraeme } sio_ld[] = {
     96  1.21   xtraeme 	{ "GPIO",	SIO_LDN_GPIO,	16 },
     97  1.21   xtraeme 	{ "VLM",	SIO_LDN_VLM,	16 },
     98  1.21   xtraeme 	{ "TMS",	SIO_LDN_TMS,	16 }
     99  1.21   xtraeme };
    100  1.21   xtraeme 
    101  1.21   xtraeme /* GPIO */
    102  1.21   xtraeme #define SIO_GPIO_PINSEL	0xf0
    103  1.21   xtraeme #define SIO_GPIO_PINCFG	0xf1
    104  1.21   xtraeme #define SIO_GPIO_PINEV	0xf2
    105   1.1  drochner 
    106  1.11  jmcneill #define	SIO_GPIO_CONF_OUTPUTEN	(1 << 0)
    107  1.11  jmcneill #define	SIO_GPIO_CONF_PUSHPULL	(1 << 1)
    108  1.11  jmcneill #define	SIO_GPIO_CONF_PULLUP	(1 << 2)
    109  1.11  jmcneill 
    110  1.21   xtraeme #define SIO_GPDO0	0x00
    111  1.21   xtraeme #define SIO_GPDI0	0x01
    112  1.21   xtraeme #define SIO_GPEVEN0	0x02
    113  1.21   xtraeme #define SIO_GPEVST0	0x03
    114  1.21   xtraeme #define SIO_GPDO1	0x04
    115  1.21   xtraeme #define SIO_GPDI1	0x05
    116  1.21   xtraeme #define SIO_GPEVEN1	0x06
    117  1.21   xtraeme #define SIO_GPEVST1	0x07
    118  1.21   xtraeme #define SIO_GPDO2	0x08
    119  1.21   xtraeme #define SIO_GPDI2	0x09
    120  1.21   xtraeme #define SIO_GPDO3	0x0a
    121  1.21   xtraeme #define SIO_GPDI3	0x0b
    122  1.21   xtraeme 
    123  1.21   xtraeme #define SIO_GPIO_NPINS	29
    124  1.21   xtraeme 
    125  1.21   xtraeme /* TMS */
    126  1.21   xtraeme #define SIO_TEVSTS	0x00	/* Temperature Event Status */
    127  1.21   xtraeme #define SIO_TEVSMI	0x02	/* Temperature Event to SMI */
    128  1.21   xtraeme #define SIO_TEVIRQ	0x04	/* Temperature Event to IRQ */
    129  1.21   xtraeme #define SIO_TMSCFG	0x08	/* TMS Configuration */
    130  1.21   xtraeme #define SIO_TMSBS	0x09	/* TMS Bank Select */
    131  1.21   xtraeme #define SIO_TCHCFST	0x0a	/* Temperature Channel Config and Status */
    132  1.21   xtraeme #define SIO_RDCHT	0x0b	/* Read Channel Temperature */
    133  1.21   xtraeme #define SIO_CHTH	0x0c	/* Channel Temperature High Limit */
    134  1.21   xtraeme #define SIO_CHTL	0x0d	/* Channel Temperature Low Limit */
    135  1.21   xtraeme #define SIO_CHOTL	0x0e	/* Channel Overtemperature Limit */
    136  1.21   xtraeme 
    137  1.21   xtraeme /* VLM */
    138  1.21   xtraeme #define SIO_VEVSTS0	0x00	/* Voltage Event Status 0 */
    139  1.21   xtraeme #define SIO_VEVSTS1	0x01	/* Voltage Event Status 1 */
    140  1.21   xtraeme #define SIO_VEVSMI0	0x02	/* Voltage Event to SMI 0 */
    141  1.21   xtraeme #define SIO_VEVSMI1	0x03	/* Voltage Event to SMI 1 */
    142  1.21   xtraeme #define SIO_VEVIRQ0	0x04	/* Voltage Event to IRQ 0 */
    143  1.21   xtraeme #define SIO_VEVIRQ1	0x05	/* Voltage Event to IRQ 1 */
    144  1.21   xtraeme #define SIO_VID 	0x06	/* Voltage ID */
    145  1.21   xtraeme #define SIO_VCNVR	0x07	/* Voltage Conversion Rate */
    146  1.21   xtraeme #define SIO_VLMCFG	0x08	/* VLM Configuration */
    147  1.21   xtraeme #define SIO_VLMBS	0x09	/* VLM Bank Select */
    148  1.21   xtraeme #define SIO_VCHCFST	0x0a	/* Voltage Channel Config and Status */
    149  1.21   xtraeme #define SIO_RDCHV	0x0b	/* Read Channel Voltage */
    150  1.21   xtraeme #define SIO_CHVH	0x0c	/* Channel Voltage High Limit */
    151  1.21   xtraeme #define SIO_CHVL	0x0d	/* Channel Voltage Low Limit */
    152  1.21   xtraeme #define SIO_OTSL	0x0e	/* Overtemperature Shutdown Limit */
    153  1.21   xtraeme 
    154  1.21   xtraeme #define SIO_REG_SIOCF1	0x21
    155  1.21   xtraeme #define SIO_REG_SIOCF2	0x22
    156  1.21   xtraeme #define SIO_REG_SIOCF3	0x23
    157  1.21   xtraeme #define SIO_REG_SIOCF4	0x24
    158  1.21   xtraeme #define SIO_REG_SIOCF5	0x25
    159  1.21   xtraeme #define SIO_REG_SIOCF8	0x28
    160  1.21   xtraeme #define SIO_REG_SIOCFA	0x2a
    161  1.21   xtraeme #define SIO_REG_SIOCFB	0x2b
    162  1.21   xtraeme #define SIO_REG_SIOCFC	0x2c
    163  1.21   xtraeme #define SIO_REG_SIOCFD	0x2d
    164  1.21   xtraeme 
    165  1.21   xtraeme #define SIO_VLM_OFF	3
    166  1.21   xtraeme #define SIO_NUM_SENSORS	(SIO_VLM_OFF + 14)
    167  1.21   xtraeme #define SIO_VREF	1235	/* 1000.0 * VREF */
    168  1.21   xtraeme 
    169   1.1  drochner struct nsclpcsio_softc {
    170  1.26   xtraeme 	device_t sc_dev;
    171  1.26   xtraeme 
    172  1.21   xtraeme 	bus_space_tag_t sc_iot;
    173  1.21   xtraeme 	bus_space_handle_t sc_ioh;
    174  1.21   xtraeme 
    175  1.21   xtraeme 	bus_space_handle_t sc_ld_ioh[SIO_LDNUM];
    176  1.21   xtraeme 	int sc_ld_en[SIO_LDNUM];
    177   1.1  drochner 
    178  1.21   xtraeme 	/* TMS and VLM */
    179  1.22   xtraeme 	struct sysmon_envsys *sc_sme;
    180  1.21   xtraeme 	envsys_data_t sc_sensor[SIO_NUM_SENSORS];
    181  1.21   xtraeme 
    182  1.16   xtraeme 	kmutex_t sc_lock;
    183  1.12  drochner #if NGPIO > 0
    184  1.11  jmcneill 	/* GPIO */
    185  1.11  jmcneill 	struct gpio_chipset_tag sc_gpio_gc;
    186  1.21   xtraeme 	struct gpio_pin sc_gpio_pins[SIO_GPIO_NPINS];
    187  1.12  drochner #endif
    188   1.1  drochner };
    189   1.1  drochner 
    190  1.11  jmcneill #define GPIO_READ(sc, reg)			\
    191  1.21   xtraeme 	bus_space_read_1((sc)->sc_iot,			\
    192  1.21   xtraeme 	    (sc)->sc_ld_ioh[SIO_LDN_GPIO], (reg))
    193  1.11  jmcneill #define GPIO_WRITE(sc, reg, val)		\
    194  1.21   xtraeme 	bus_space_write_1((sc)->sc_iot,			\
    195  1.21   xtraeme 	    (sc)->sc_ld_ioh[SIO_LDN_GPIO], (reg), (val))
    196  1.21   xtraeme #define TMS_WRITE(sc, reg, val)				\
    197  1.21   xtraeme 	bus_space_write_1((sc)->sc_iot,			\
    198  1.21   xtraeme 	    (sc)->sc_ld_ioh[SIO_LDN_TMS], (reg), (val))
    199  1.21   xtraeme #define TMS_READ(sc, reg)				\
    200  1.21   xtraeme 	bus_space_read_1((sc)->sc_iot,			\
    201  1.21   xtraeme 	    (sc)->sc_ld_ioh[SIO_LDN_TMS], (reg))
    202  1.21   xtraeme #define VLM_WRITE(sc, reg, val)				\
    203  1.21   xtraeme 	bus_space_write_1((sc)->sc_iot,			\
    204  1.21   xtraeme 	    (sc)->sc_ld_ioh[SIO_LDN_VLM], (reg), (val))
    205  1.21   xtraeme #define VLM_READ(sc, reg)				\
    206  1.21   xtraeme 	bus_space_read_1((sc)->sc_iot,			\
    207  1.21   xtraeme 	    (sc)->sc_ld_ioh[SIO_LDN_VLM], (reg))
    208  1.21   xtraeme 
    209  1.26   xtraeme static int	nsclpcsio_isa_match(device_t, cfdata_t, void *);
    210  1.26   xtraeme static void	nsclpcsio_isa_attach(device_t, device_t, void *);
    211  1.26   xtraeme static int	nsclpcsio_isa_detach(device_t, int);
    212  1.11  jmcneill 
    213  1.26   xtraeme CFATTACH_DECL_NEW(nsclpcsio_isa, sizeof(struct nsclpcsio_softc),
    214  1.19   xtraeme     nsclpcsio_isa_match, nsclpcsio_isa_attach, nsclpcsio_isa_detach, NULL);
    215   1.1  drochner 
    216  1.21   xtraeme static uint8_t	nsread(bus_space_tag_t, bus_space_handle_t, int);
    217  1.21   xtraeme static void	nswrite(bus_space_tag_t, bus_space_handle_t, int, uint8_t);
    218  1.26   xtraeme static int	nscheck(bus_space_tag_t, int);
    219   1.1  drochner 
    220  1.21   xtraeme static void	nsclpcsio_tms_init(struct nsclpcsio_softc *);
    221  1.21   xtraeme static void	nsclpcsio_vlm_init(struct nsclpcsio_softc *);
    222  1.22   xtraeme static void	nsclpcsio_refresh(struct sysmon_envsys *, envsys_data_t *);
    223   1.1  drochner 
    224  1.12  drochner #if NGPIO > 0
    225  1.11  jmcneill static void nsclpcsio_gpio_init(struct nsclpcsio_softc *);
    226  1.11  jmcneill static void nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *, int);
    227  1.11  jmcneill static void nsclpcsio_gpio_pin_write(void *, int, int);
    228  1.11  jmcneill static int nsclpcsio_gpio_pin_read(void *, int);
    229  1.11  jmcneill static void nsclpcsio_gpio_pin_ctl(void *, int, int);
    230  1.12  drochner #endif
    231  1.11  jmcneill 
    232  1.21   xtraeme static uint8_t
    233  1.21   xtraeme nsread(bus_space_tag_t iot, bus_space_handle_t ioh, int idx)
    234   1.1  drochner {
    235   1.1  drochner 	bus_space_write_1(iot, ioh, 0, idx);
    236  1.21   xtraeme 	return bus_space_read_1(iot, ioh, 1);
    237   1.1  drochner }
    238   1.1  drochner 
    239   1.1  drochner static void
    240  1.21   xtraeme nswrite(bus_space_tag_t iot, bus_space_handle_t ioh, int idx, uint8_t data)
    241   1.1  drochner {
    242   1.1  drochner 	bus_space_write_1(iot, ioh, 0, idx);
    243   1.1  drochner 	bus_space_write_1(iot, ioh, 1, data);
    244   1.1  drochner }
    245   1.1  drochner 
    246   1.1  drochner static int
    247  1.21   xtraeme nscheck(bus_space_tag_t iot, int base)
    248   1.1  drochner {
    249   1.1  drochner 	bus_space_handle_t ioh;
    250   1.1  drochner 	int rv = 0;
    251   1.1  drochner 
    252   1.1  drochner 	if (bus_space_map(iot, base, 2, 0, &ioh))
    253  1.21   xtraeme 		return 0;
    254   1.1  drochner 
    255   1.1  drochner 	/* XXX this is for PC87366 only for now */
    256  1.21   xtraeme 	if (nsread(iot, ioh, SIO_REG_SID) == SIO_SID_PC87366)
    257   1.1  drochner 		rv = 1;
    258   1.1  drochner 
    259   1.1  drochner 	bus_space_unmap(iot, ioh, 2);
    260  1.21   xtraeme 	return rv;
    261   1.1  drochner }
    262   1.1  drochner 
    263   1.1  drochner static int
    264  1.26   xtraeme nsclpcsio_isa_match(device_t parent, cfdata_t match, void *aux)
    265   1.1  drochner {
    266   1.1  drochner 	struct isa_attach_args *ia = aux;
    267   1.1  drochner 	int iobase;
    268   1.1  drochner 
    269   1.1  drochner 	if (ISA_DIRECT_CONFIG(ia))
    270  1.21   xtraeme 		return 0;
    271   1.1  drochner 
    272   1.6  drochner 	if (ia->ia_nio > 0 && ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
    273   1.1  drochner 		/* XXX check for legal iobase ??? */
    274   1.1  drochner 		if (nscheck(ia->ia_iot, ia->ia_io[0].ir_addr)) {
    275   1.1  drochner 			iobase = ia->ia_io[0].ir_addr;
    276   1.1  drochner 			goto found;
    277   1.1  drochner 		}
    278  1.21   xtraeme 		return 0;
    279   1.1  drochner 	}
    280   1.1  drochner 
    281   1.1  drochner 	/* PC87366 has two possible locations depending on wiring */
    282   1.1  drochner 	if (nscheck(ia->ia_iot, 0x2e)) {
    283   1.1  drochner 		iobase = 0x2e;
    284   1.1  drochner 		goto found;
    285   1.1  drochner 	}
    286   1.1  drochner 	if (nscheck(ia->ia_iot, 0x4e)) {
    287   1.1  drochner 		iobase = 0x4e;
    288   1.1  drochner 		goto found;
    289   1.1  drochner 	}
    290  1.21   xtraeme 
    291  1.21   xtraeme 	return 0;
    292   1.1  drochner 
    293   1.1  drochner found:
    294   1.1  drochner 	ia->ia_nio = 1;
    295   1.1  drochner 	ia->ia_io[0].ir_addr = iobase;
    296   1.1  drochner 	ia->ia_io[0].ir_size = 2;
    297   1.1  drochner 	ia->ia_niomem = 0;
    298   1.1  drochner 	ia->ia_nirq = 0;
    299   1.1  drochner 	ia->ia_ndrq = 0;
    300  1.21   xtraeme 
    301  1.21   xtraeme 	return 1;
    302   1.1  drochner }
    303   1.1  drochner 
    304  1.25    dyoung static struct sysmon_envsys *
    305  1.25    dyoung nsclpcsio_envsys_init(struct nsclpcsio_softc *sc)
    306  1.25    dyoung {
    307  1.25    dyoung 	int i;
    308  1.25    dyoung 	struct sysmon_envsys *sme;
    309  1.25    dyoung 
    310  1.25    dyoung 	sme = sysmon_envsys_create();
    311  1.25    dyoung 	for (i = 0; i < SIO_NUM_SENSORS; i++) {
    312  1.25    dyoung 		if (sysmon_envsys_sensor_attach(sme, &sc->sc_sensor[i]) != 0) {
    313  1.26   xtraeme 			aprint_error_dev(sc->sc_dev,
    314  1.25    dyoung 			    "could not attach sensor %d", i);
    315  1.25    dyoung 			goto err;
    316  1.25    dyoung 		}
    317  1.25    dyoung 	}
    318  1.25    dyoung 
    319  1.25    dyoung 	/*
    320  1.25    dyoung 	 * Hook into the System Monitor.
    321  1.25    dyoung 	 */
    322  1.26   xtraeme 	sme->sme_name = device_xname(sc->sc_dev);
    323  1.25    dyoung 	sme->sme_cookie = sc;
    324  1.25    dyoung 	sme->sme_refresh = nsclpcsio_refresh;
    325  1.25    dyoung 
    326  1.26   xtraeme 	if ((i = sysmon_envsys_register(sme)) != 0) {
    327  1.26   xtraeme 		aprint_error_dev(sc->sc_dev,
    328  1.26   xtraeme 		    "unable to register with sysmon (%d)\n", i);
    329  1.25    dyoung 		goto err;
    330  1.25    dyoung 	}
    331  1.25    dyoung 	return sme;
    332  1.25    dyoung err:
    333  1.25    dyoung 	sysmon_envsys_destroy(sme);
    334  1.25    dyoung 	return NULL;
    335  1.25    dyoung }
    336  1.25    dyoung 
    337   1.1  drochner static void
    338  1.26   xtraeme nsclpcsio_isa_attach(device_t parent, device_t self, void *aux)
    339   1.1  drochner {
    340  1.21   xtraeme 	struct nsclpcsio_softc *sc = device_private(self);
    341   1.1  drochner 	struct isa_attach_args *ia = aux;
    342  1.12  drochner #if NGPIO > 0
    343  1.11  jmcneill 	struct gpiobus_attach_args gba;
    344  1.12  drochner #endif
    345  1.21   xtraeme 	int i, iobase;
    346   1.1  drochner 
    347  1.23        ad 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    348  1.11  jmcneill 
    349  1.26   xtraeme 	sc->sc_dev = self;
    350  1.21   xtraeme 	sc->sc_iot = ia->ia_iot;
    351  1.21   xtraeme 	iobase = ia->ia_io[0].ir_addr;
    352  1.11  jmcneill 
    353  1.21   xtraeme 	if (bus_space_map(ia->ia_iot, iobase, 2, 0, &sc->sc_ioh)) {
    354  1.21   xtraeme 		aprint_error(": can't map i/o space\n");
    355  1.24    dyoung 		return;
    356  1.24    dyoung 	}
    357  1.11  jmcneill 
    358  1.21   xtraeme 	aprint_normal(": NSC PC87366 rev. 0x%d ",
    359  1.21   xtraeme 	    nsread(sc->sc_iot, sc->sc_ioh, SIO_REG_SRID));
    360  1.11  jmcneill 
    361  1.21   xtraeme 	/* Configure all supported logical devices */
    362  1.21   xtraeme 	for (i = 0; i < __arraycount(sio_ld); i++) {
    363  1.21   xtraeme 		sc->sc_ld_en[sio_ld[i].ld_num] = 0;
    364  1.21   xtraeme 
    365  1.21   xtraeme 		/* Select the device and check if it's activated */
    366  1.21   xtraeme 		nswrite(sc->sc_iot, sc->sc_ioh, SIO_REG_LDN, sio_ld[i].ld_num);
    367  1.21   xtraeme 		if ((nsread(sc->sc_iot, sc->sc_ioh,
    368  1.21   xtraeme 		    SIO_REG_ACTIVE) & SIO_ACTIVE_EN) == 0)
    369  1.21   xtraeme 			continue;
    370  1.21   xtraeme 
    371  1.21   xtraeme 		/* Map I/O space if necessary */
    372  1.21   xtraeme 		if (sio_ld[i].ld_iosize != 0) {
    373  1.21   xtraeme 			iobase = (nsread(sc->sc_iot, sc->sc_ioh,
    374  1.21   xtraeme 			    SIO_REG_IO_MSB) << 8);
    375  1.21   xtraeme 			iobase |= nsread(sc->sc_iot, sc->sc_ioh,
    376  1.21   xtraeme 			    SIO_REG_IO_LSB);
    377  1.21   xtraeme 			if (bus_space_map(sc->sc_iot, iobase,
    378  1.21   xtraeme 			    sio_ld[i].ld_iosize, 0,
    379  1.21   xtraeme 			    &sc->sc_ld_ioh[sio_ld[i].ld_num]))
    380  1.21   xtraeme 				continue;
    381  1.22   xtraeme 		}
    382  1.21   xtraeme 
    383  1.21   xtraeme 		sc->sc_ld_en[sio_ld[i].ld_num] = 1;
    384  1.21   xtraeme 		aprint_normal("%s ", sio_ld[i].ld_name);
    385  1.21   xtraeme 	}
    386  1.21   xtraeme 
    387  1.21   xtraeme 	aprint_normal("\n");
    388  1.21   xtraeme 
    389  1.21   xtraeme #if NGPIO > 0
    390  1.21   xtraeme 	nsclpcsio_gpio_init(sc);
    391  1.21   xtraeme #endif
    392  1.21   xtraeme 	nsclpcsio_tms_init(sc);
    393  1.21   xtraeme 	nsclpcsio_vlm_init(sc);
    394  1.25    dyoung 	sc->sc_sme = nsclpcsio_envsys_init(sc);
    395  1.11  jmcneill 
    396  1.12  drochner #if NGPIO > 0
    397  1.11  jmcneill 	/* attach GPIO framework */
    398  1.21   xtraeme 	if (sc->sc_ld_en[SIO_LDN_GPIO]) {
    399  1.11  jmcneill 		gba.gba_gc = &sc->sc_gpio_gc;
    400  1.11  jmcneill 		gba.gba_pins = sc->sc_gpio_pins;
    401  1.21   xtraeme 		gba.gba_npins = SIO_GPIO_NPINS;
    402  1.12  drochner 		config_found_ia(&sc->sc_dev, "gpiobus", &gba, NULL);
    403  1.11  jmcneill 	}
    404  1.12  drochner #endif
    405   1.1  drochner }
    406   1.1  drochner 
    407  1.19   xtraeme static int
    408  1.26   xtraeme nsclpcsio_isa_detach(device_t self, int flags)
    409  1.19   xtraeme {
    410  1.25    dyoung 	int i, rc;
    411  1.19   xtraeme 	struct nsclpcsio_softc *sc = device_private(self);
    412  1.19   xtraeme 
    413  1.25    dyoung 	if ((rc = config_detach_children(self, flags)) != 0)
    414  1.25    dyoung 		return rc;
    415  1.25    dyoung 
    416  1.25    dyoung 	if (sc->sc_sme != NULL)
    417  1.25    dyoung 		sysmon_envsys_unregister(sc->sc_sme);
    418  1.21   xtraeme 	mutex_destroy(&sc->sc_lock);
    419  1.21   xtraeme 
    420  1.25    dyoung 	for (i = 0; i < __arraycount(sio_ld); i++) {
    421  1.25    dyoung 		if (sc->sc_ld_en[sio_ld[i].ld_num] &&
    422  1.25    dyoung 		    sio_ld[i].ld_iosize != 0) {
    423  1.25    dyoung 			bus_space_unmap(sc->sc_iot,
    424  1.25    dyoung 			    sc->sc_ld_ioh[sio_ld[i].ld_num],
    425  1.25    dyoung 			    sio_ld[i].ld_iosize);
    426  1.25    dyoung 		}
    427  1.25    dyoung 	}
    428  1.25    dyoung 
    429  1.19   xtraeme 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
    430  1.21   xtraeme 
    431  1.19   xtraeme 	return 0;
    432  1.19   xtraeme }
    433  1.19   xtraeme 
    434   1.1  drochner static void
    435  1.21   xtraeme nsclpcsio_tms_init(struct nsclpcsio_softc *sc)
    436  1.21   xtraeme {
    437  1.21   xtraeme 	int i;
    438   1.1  drochner 
    439  1.21   xtraeme 	/* Initialisation, PC87366.pdf, page 208 */
    440  1.21   xtraeme 	TMS_WRITE(sc, 0x08, 0x00);
    441  1.21   xtraeme 	TMS_WRITE(sc, 0x09, 0x0f);
    442  1.21   xtraeme 	TMS_WRITE(sc, 0x0a, 0x08);
    443  1.21   xtraeme 	TMS_WRITE(sc, 0x0b, 0x04);
    444  1.21   xtraeme 	TMS_WRITE(sc, 0x0c, 0x35);
    445  1.21   xtraeme 	TMS_WRITE(sc, 0x0d, 0x05);
    446  1.21   xtraeme 	TMS_WRITE(sc, 0x0e, 0x05);
    447  1.21   xtraeme 
    448  1.21   xtraeme 	TMS_WRITE(sc, SIO_TMSCFG, 0x00);
    449  1.21   xtraeme 
    450  1.21   xtraeme 	for (i = 0; i < SIO_VLM_OFF; i++) {
    451  1.21   xtraeme 		TMS_WRITE(sc, SIO_TMSBS, i);
    452  1.21   xtraeme 		TMS_WRITE(sc, SIO_TCHCFST, 0x01);
    453  1.21   xtraeme 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    454  1.21   xtraeme 	}
    455  1.21   xtraeme 
    456  1.21   xtraeme #define COPYDESCR(x, y)					\
    457  1.21   xtraeme 	do {						\
    458  1.21   xtraeme 		(void)strlcpy((x), (y), sizeof(x));	\
    459  1.21   xtraeme 	} while (/* CONSTCOND */ 0)
    460  1.21   xtraeme 
    461  1.21   xtraeme 	COPYDESCR(sc->sc_sensor[0].desc, "TSENS1");
    462  1.21   xtraeme 	COPYDESCR(sc->sc_sensor[1].desc, "TSENS2");
    463  1.21   xtraeme 	COPYDESCR(sc->sc_sensor[2].desc, "TNSC");
    464  1.21   xtraeme }
    465  1.21   xtraeme 
    466  1.21   xtraeme static void
    467  1.21   xtraeme nsclpcsio_vlm_init(struct nsclpcsio_softc *sc)
    468  1.21   xtraeme {
    469  1.21   xtraeme 	int i;
    470  1.21   xtraeme 	char tmp[16];
    471  1.25    dyoung 	envsys_data_t *sensor = &sc->sc_sensor[SIO_VLM_OFF];
    472  1.11  jmcneill 
    473  1.25    dyoung 	for (i = 0; i < SIO_NUM_SENSORS - SIO_VLM_OFF; i++) {
    474  1.25    dyoung 		VLM_WRITE(sc, SIO_VLMBS, i);
    475  1.21   xtraeme 		VLM_WRITE(sc, SIO_VCHCFST, 0x01);
    476  1.25    dyoung 		sensor[i].units = ENVSYS_SVOLTS_DC;
    477  1.21   xtraeme 	}
    478  1.21   xtraeme 
    479  1.21   xtraeme 	for (i = 0; i < 7; i++) {
    480  1.21   xtraeme 		(void)snprintf(tmp, sizeof(tmp), "VSENS%d", i);
    481  1.25    dyoung 		COPYDESCR(sensor[i].desc, tmp);
    482  1.21   xtraeme 	}
    483  1.21   xtraeme 
    484  1.25    dyoung 	COPYDESCR(sensor[7 ].desc, "VSB");
    485  1.25    dyoung 	COPYDESCR(sensor[8 ].desc, "VDD");
    486  1.25    dyoung 	COPYDESCR(sensor[9 ].desc, "VBAT");
    487  1.25    dyoung 	COPYDESCR(sensor[10].desc, "AVDD");
    488  1.25    dyoung 	COPYDESCR(sensor[11].desc, "TS1");
    489  1.25    dyoung 	COPYDESCR(sensor[12].desc, "TS2");
    490  1.25    dyoung 	COPYDESCR(sensor[13].desc, "TS3");
    491  1.21   xtraeme }
    492  1.11  jmcneill 
    493   1.1  drochner 
    494  1.22   xtraeme static void
    495  1.22   xtraeme nsclpcsio_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    496  1.21   xtraeme {
    497  1.21   xtraeme 	struct nsclpcsio_softc *sc = sme->sme_cookie;
    498  1.21   xtraeme 	uint8_t status, data;
    499  1.21   xtraeme 	int8_t sdata = 0;
    500  1.21   xtraeme 	int scale, rfact;
    501   1.1  drochner 
    502  1.21   xtraeme 	scale = rfact = 0;
    503  1.21   xtraeme 	status = data = 0;
    504   1.1  drochner 
    505  1.21   xtraeme 	mutex_enter(&sc->sc_lock);
    506  1.21   xtraeme 	/* TMS */
    507  1.21   xtraeme 	if (edata->sensor < SIO_VLM_OFF && sc->sc_ld_en[SIO_LDN_TMS]) {
    508  1.21   xtraeme 		TMS_WRITE(sc, SIO_TMSBS, edata->sensor);
    509  1.21   xtraeme 		status = TMS_READ(sc, SIO_TCHCFST);
    510  1.21   xtraeme 		if (!(status & 0x01))
    511  1.21   xtraeme 			edata->state = ENVSYS_SINVALID;
    512  1.21   xtraeme 
    513  1.21   xtraeme 		sdata = TMS_READ(sc, SIO_RDCHT);
    514  1.21   xtraeme 		edata->value_cur = sdata * 1000000 + 273150000;
    515  1.21   xtraeme 		edata->state = ENVSYS_SVALID;
    516  1.21   xtraeme 	/* VLM */
    517  1.21   xtraeme 	} else if (edata->sensor >= SIO_VLM_OFF &&
    518  1.21   xtraeme 		   edata->sensor < SIO_NUM_SENSORS &&
    519  1.21   xtraeme 		   sc->sc_ld_en[SIO_LDN_VLM]) {
    520  1.21   xtraeme 		VLM_WRITE(sc, SIO_VLMBS, edata->sensor - SIO_VLM_OFF);
    521  1.21   xtraeme 		status = VLM_READ(sc, SIO_VCHCFST);
    522  1.21   xtraeme 		if (!(status & 0x01)) {
    523  1.21   xtraeme 			edata->state = ENVSYS_SINVALID;
    524  1.21   xtraeme 		} else {
    525  1.21   xtraeme 			data = VLM_READ(sc, SIO_RDCHV);
    526  1.21   xtraeme 			scale = 1;
    527  1.21   xtraeme 			switch (edata->sensor - SIO_VLM_OFF) {
    528  1.21   xtraeme 			case 7:
    529  1.21   xtraeme 			case 8:
    530  1.21   xtraeme 			case 10:
    531  1.21   xtraeme 				scale = 2;
    532  1.21   xtraeme 				break;
    533  1.21   xtraeme 			}
    534  1.21   xtraeme 			/* Vi = (2.450.05)*VREF *RDCHVi / 256 */
    535  1.21   xtraeme 			rfact = 10 * scale * ((245 * SIO_VREF) >> 8);
    536  1.21   xtraeme 			edata->value_cur = data * rfact;
    537  1.21   xtraeme 			edata->state = ENVSYS_SVALID;
    538   1.1  drochner 		}
    539   1.1  drochner 	}
    540  1.16   xtraeme 	mutex_exit(&sc->sc_lock);
    541   1.1  drochner }
    542  1.11  jmcneill 
    543  1.12  drochner #if NGPIO > 0
    544  1.11  jmcneill static void
    545  1.11  jmcneill nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *sc, int pin)
    546  1.11  jmcneill {
    547  1.21   xtraeme 	uint8_t v;
    548  1.11  jmcneill 
    549  1.11  jmcneill 	v = ((pin / 8) << 4) | (pin % 8);
    550  1.11  jmcneill 
    551  1.21   xtraeme 	nswrite(sc->sc_iot, sc->sc_ioh, SIO_REG_LDN, SIO_LDN_GPIO);
    552  1.21   xtraeme 	nswrite(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINSEL, v);
    553  1.11  jmcneill }
    554  1.11  jmcneill 
    555  1.11  jmcneill static void
    556  1.11  jmcneill nsclpcsio_gpio_init(struct nsclpcsio_softc *sc)
    557  1.11  jmcneill {
    558  1.11  jmcneill 	int i;
    559  1.11  jmcneill 
    560  1.21   xtraeme 	for (i = 0; i < SIO_GPIO_NPINS; i++) {
    561  1.11  jmcneill 		sc->sc_gpio_pins[i].pin_num = i;
    562  1.11  jmcneill 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
    563  1.11  jmcneill 		    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
    564  1.11  jmcneill 		    GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
    565  1.11  jmcneill 		    GPIO_PIN_PULLUP;
    566  1.11  jmcneill 		/* safe defaults */
    567  1.11  jmcneill 		sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
    568  1.11  jmcneill 		sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
    569  1.11  jmcneill 		nsclpcsio_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
    570  1.11  jmcneill 		nsclpcsio_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
    571  1.11  jmcneill 	}
    572  1.11  jmcneill 
    573  1.11  jmcneill 	/* create controller tag */
    574  1.11  jmcneill 	sc->sc_gpio_gc.gp_cookie = sc;
    575  1.11  jmcneill 	sc->sc_gpio_gc.gp_pin_read = nsclpcsio_gpio_pin_read;
    576  1.11  jmcneill 	sc->sc_gpio_gc.gp_pin_write = nsclpcsio_gpio_pin_write;
    577  1.11  jmcneill 	sc->sc_gpio_gc.gp_pin_ctl = nsclpcsio_gpio_pin_ctl;
    578  1.11  jmcneill }
    579  1.11  jmcneill 
    580  1.11  jmcneill static int
    581  1.11  jmcneill nsclpcsio_gpio_pin_read(void *aux, int pin)
    582  1.11  jmcneill {
    583  1.11  jmcneill 	struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
    584  1.11  jmcneill 	int port, shift, reg;
    585  1.21   xtraeme 	uint8_t v;
    586  1.11  jmcneill 
    587  1.11  jmcneill 	port = pin / 8;
    588  1.11  jmcneill 	shift = pin % 8;
    589  1.11  jmcneill 
    590  1.11  jmcneill 	switch (port) {
    591  1.21   xtraeme 	case 0:
    592  1.21   xtraeme 		reg = SIO_GPDI0;
    593  1.21   xtraeme 		break;
    594  1.21   xtraeme 	case 1:
    595  1.21   xtraeme 		reg = SIO_GPDI1;
    596  1.21   xtraeme 		break;
    597  1.21   xtraeme 	case 2:
    598  1.21   xtraeme 		reg = SIO_GPDI2;
    599  1.21   xtraeme 		break;
    600  1.21   xtraeme 	case 3:
    601  1.21   xtraeme 		reg = SIO_GPDI3;
    602  1.21   xtraeme 		break;
    603  1.21   xtraeme 	default:
    604  1.21   xtraeme 		reg = SIO_GPDI0;
    605  1.21   xtraeme 		break;
    606  1.11  jmcneill 	}
    607  1.11  jmcneill 
    608  1.11  jmcneill 	v = GPIO_READ(sc, reg);
    609  1.11  jmcneill 
    610  1.11  jmcneill 	return ((v >> shift) & 0x1);
    611  1.11  jmcneill }
    612  1.11  jmcneill 
    613  1.11  jmcneill static void
    614  1.11  jmcneill nsclpcsio_gpio_pin_write(void *aux, int pin, int v)
    615  1.11  jmcneill {
    616  1.11  jmcneill 	struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
    617  1.11  jmcneill 	int port, shift, reg;
    618  1.21   xtraeme 	uint8_t d;
    619  1.11  jmcneill 
    620  1.11  jmcneill 	port = pin / 8;
    621  1.11  jmcneill 	shift = pin % 8;
    622  1.11  jmcneill 
    623  1.11  jmcneill 	switch (port) {
    624  1.21   xtraeme 	case 0:
    625  1.21   xtraeme 		reg = SIO_GPDO0;
    626  1.21   xtraeme 		break;
    627  1.21   xtraeme 	case 1:
    628  1.21   xtraeme 		reg = SIO_GPDO1;
    629  1.21   xtraeme 		break;
    630  1.21   xtraeme 	case 2:
    631  1.21   xtraeme 		reg = SIO_GPDO2;
    632  1.21   xtraeme 		break;
    633  1.21   xtraeme 	case 3:
    634  1.21   xtraeme 		reg = SIO_GPDO3;
    635  1.21   xtraeme 		break;
    636  1.21   xtraeme 	default:
    637  1.21   xtraeme 		reg = SIO_GPDO0;
    638  1.21   xtraeme 		break; /* shouldn't happen */
    639  1.11  jmcneill 	}
    640  1.11  jmcneill 
    641  1.11  jmcneill 	d = GPIO_READ(sc, reg);
    642  1.11  jmcneill 	if (v == 0)
    643  1.11  jmcneill 		d &= ~(1 << shift);
    644  1.11  jmcneill 	else if (v == 1)
    645  1.11  jmcneill 		d |= (1 << shift);
    646  1.11  jmcneill 	GPIO_WRITE(sc, reg, d);
    647  1.11  jmcneill }
    648  1.11  jmcneill 
    649  1.11  jmcneill void
    650  1.11  jmcneill nsclpcsio_gpio_pin_ctl(void *aux, int pin, int flags)
    651  1.11  jmcneill {
    652  1.11  jmcneill 	struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
    653  1.21   xtraeme 	uint8_t conf;
    654  1.11  jmcneill 
    655  1.16   xtraeme 	mutex_enter(&sc->sc_lock);
    656  1.11  jmcneill 
    657  1.21   xtraeme 	nswrite(sc->sc_iot, sc->sc_ioh, SIO_REG_LDN, SIO_LDN_GPIO);
    658  1.11  jmcneill 	nsclpcsio_gpio_pin_select(sc, pin);
    659  1.21   xtraeme 	conf = nsread(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINCFG);
    660  1.11  jmcneill 
    661  1.11  jmcneill 	conf &= ~(SIO_GPIO_CONF_OUTPUTEN | SIO_GPIO_CONF_PUSHPULL |
    662  1.11  jmcneill 	    SIO_GPIO_CONF_PULLUP);
    663  1.11  jmcneill 	if ((flags & GPIO_PIN_TRISTATE) == 0)
    664  1.11  jmcneill 		conf |= SIO_GPIO_CONF_OUTPUTEN;
    665  1.11  jmcneill 	if (flags & GPIO_PIN_PUSHPULL)
    666  1.11  jmcneill 		conf |= SIO_GPIO_CONF_PUSHPULL;
    667  1.11  jmcneill 	if (flags & GPIO_PIN_PULLUP)
    668  1.11  jmcneill 		conf |= SIO_GPIO_CONF_PULLUP;
    669  1.11  jmcneill 
    670  1.21   xtraeme 	nswrite(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINCFG, conf);
    671  1.11  jmcneill 
    672  1.16   xtraeme 	mutex_exit(&sc->sc_lock);
    673  1.11  jmcneill }
    674  1.12  drochner #endif /* NGPIO */
    675