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