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