Home | History | Annotate | Line # | Download | only in isa
itesio_isa.c revision 1.1
      1 /*	$NetBSD: itesio_isa.c,v 1.1 2007/11/15 12:53:42 xtraeme Exp $ */
      2 /*	Derived from $NetBSD: itesio_isa.c,v 1.1 2007/11/15 12:53:42 xtraeme Exp $	*/
      3 /*	Derived from $OpenBSD: it.c,v 1.19 2006/04/10 00:57:54 deraadt Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 2006-2007 Juan Romero Pardines <juan (at) xtrarom.org>
      7  * Copyright (c) 2003 Julien Bordet <zejames (at) greyhats.org>
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITD TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITD TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Driver for the iTE IT87xxF Super I/O. Currently supporting
     33  * the Hardware monitor part in the Environmental Controller.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: itesio_isa.c,v 1.1 2007/11/15 12:53:42 xtraeme Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/errno.h>
     46 #include <sys/conf.h>
     47 #include <sys/envsys.h>
     48 #include <sys/time.h>
     49 
     50 #include <sys/bus.h>
     51 #include <sys/intr.h>
     52 
     53 #include <dev/isa/isareg.h>
     54 #include <dev/isa/isavar.h>
     55 
     56 #include <dev/sysmon/sysmonvar.h>
     57 
     58 #include <dev/isa/itesio_isavar.h>
     59 
     60 #define IT_VOLTSTART_IDX 	3 	/* voltage start index */
     61 #define IT_FANSTART_IDX 	12 	/* fan start index */
     62 
     63 #if defined(ITESIO_DEBUG)
     64 #define DPRINTF(x)		do { printf x; } while (0)
     65 #else
     66 #define DPRINTF(x)
     67 #endif
     68 
     69 /*
     70  * IT87-compatible chips can typically measure voltages up to 4.096 V.
     71  * To measure higher voltages the input is attenuated with (external)
     72  * resistors.  Negative voltages are measured using a reference
     73  * voltage.  So we have to convert the sensor values back to real
     74  * voltages by applying the appropriate resistor factor.
     75  */
     76 #define RFACT_NONE	10000
     77 #define RFACT(x, y)	(RFACT_NONE * ((x) + (y)) / (y))
     78 
     79 /* autoconf(9) functions */
     80 static int	itesio_isa_match(device_t, struct cfdata *, void *);
     81 static void	itesio_isa_attach(device_t, device_t, void *);
     82 static int	itesio_isa_detach(device_t, int);
     83 
     84 CFATTACH_DECL_NEW(itesio, sizeof(struct itesio_softc),
     85     itesio_isa_match, itesio_isa_attach, itesio_isa_detach, NULL);
     86 
     87 /* driver functions */
     88 static uint8_t	itesio_ecreadreg(struct itesio_softc *, int);
     89 static void	itesio_ecwritereg(struct itesio_softc *, int, int);
     90 static uint8_t	itesio_readreg(bus_space_tag_t, bus_space_handle_t, int);
     91 static void	itesio_writereg(bus_space_tag_t, bus_space_handle_t, int, int);
     92 static void	itesio_enter(bus_space_tag_t, bus_space_handle_t);
     93 static void	itesio_exit(bus_space_tag_t, bus_space_handle_t);
     94 
     95 /* envsys(9) glue */
     96 static void	itesio_setup_sensors(struct itesio_softc *);
     97 static void	itesio_refresh_temp(struct itesio_softc *, envsys_data_t *);
     98 static void	itesio_refresh_volts(struct itesio_softc *, envsys_data_t *);
     99 static void	itesio_refresh_fans(struct itesio_softc *, envsys_data_t *);
    100 static int	itesio_gtredata(struct sysmon_envsys *, envsys_data_t *);
    101 
    102 /* rfact values for voltage sensors */
    103 static const int itesio_vrfact[] = {
    104 	RFACT_NONE,	/* VCORE_A	*/
    105 	RFACT_NONE,	/* VCORE_B	*/
    106 	RFACT_NONE,	/* +3.3V	*/
    107 	RFACT(68, 100),	/* +5V 		*/
    108 	RFACT(30, 10),	/* +12V 	*/
    109 	RFACT(21, 10),	/* -12V 	*/
    110 	RFACT(83, 20),	/* -5V 		*/
    111 	RFACT(68, 100),	/* STANDBY	*/
    112 	RFACT_NONE	/* VBAT		*/
    113 };
    114 
    115 static int
    116 itesio_isa_match(device_t parent, struct cfdata *match, void *aux)
    117 {
    118 	struct isa_attach_args *ia = aux;
    119 	bus_space_handle_t ioh;
    120 	uint16_t cr;
    121 
    122 	/* Must supply an address */
    123 	if (ia->ia_nio < 1)
    124 		return 0;
    125 
    126 	if (ISA_DIRECT_CONFIG(ia))
    127 		return 0;
    128 
    129 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    130 		return 0;
    131 
    132 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh))
    133 		return 0;
    134 
    135 	itesio_enter(ia->ia_iot, ioh);
    136 	cr = (itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID1) << 8);
    137 	cr |= itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID2);
    138 	itesio_exit(ia->ia_iot, ioh);
    139 	bus_space_unmap(ia->ia_iot, ioh, 2);
    140 
    141 	switch (cr) {
    142 	case ITESIO_ID8705:
    143 	case ITESIO_ID8712:
    144 	case ITESIO_ID8716:
    145 	case ITESIO_ID8718:
    146 		ia->ia_nio = 1;
    147 		ia->ia_io[0].ir_size = 2;
    148 		ia->ia_niomem = 0;
    149 		ia->ia_nirq = 0;
    150 		ia->ia_ndrq = 0;
    151 		return 1;
    152 	default:
    153 		printf("itesio: unknown iTE Super I/O chip (cr=0x%x)\n", cr);
    154 		return 0;
    155 	}
    156 }
    157 
    158 static void
    159 itesio_isa_attach(device_t parent, device_t self, void *aux)
    160 {
    161 	struct itesio_softc *sc = device_private(self);
    162 	struct isa_attach_args *ia = aux;
    163 	bus_space_handle_t ioh;
    164 	int i;
    165 	uint8_t cr;
    166 
    167 	ia->ia_iot = sc->sc_iot;
    168 
    169 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh)) {
    170 		aprint_error(": can't map i/o space\n");
    171 		return;
    172 	}
    173 	/*
    174 	 * Enter to the Super I/O MB PNP mode.
    175 	 */
    176 	itesio_enter(ia->ia_iot, ioh);
    177 	/*
    178 	 * Get info from the Super I/O Global Configuration Registers:
    179 	 * Chip IDs and Device Revision.
    180 	 */
    181 	sc->sc_chipid = (itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID1) << 8);
    182 	sc->sc_chipid |= itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID2);
    183 	sc->sc_devrev = (itesio_readreg(ia->ia_iot, ioh, ITESIO_DEVREV) & 0x0f);
    184 	/*
    185 	 * Select the EC LDN to get the Base Address.
    186 	 */
    187 	itesio_writereg(ia->ia_iot, ioh, ITESIO_LDNSEL, ITESIO_EC_LDN);
    188 	sc->sc_hwmon_baseaddr =
    189 	    (itesio_readreg(ia->ia_iot, ioh, ITESIO_EC_MSB) << 8);
    190 	sc->sc_hwmon_baseaddr |= itesio_readreg(ia->ia_iot, ioh, ITESIO_EC_LSB);
    191 	/*
    192 	 * We are done, exit MB PNP mode and unmap I/O space.
    193 	 */
    194 	itesio_exit(ia->ia_iot, ioh);
    195 	bus_space_unmap(sc->sc_iot, ioh, 2);
    196 
    197 	aprint_normal(": iTE IT%4xF Super I/O (rev %d)\n",
    198 	    sc->sc_chipid, sc->sc_devrev);
    199 	aprint_normal_dev(self, "Hardware Monitor registers at 0x%x\n",
    200 	    sc->sc_hwmon_baseaddr);
    201 
    202 	if (bus_space_map(sc->sc_iot, sc->sc_hwmon_baseaddr, 8, 0,
    203 	    &sc->sc_ioh)) {
    204 		aprint_error_dev(self, "cannot map hwmon i/o space\n");
    205 		return;
    206 	}
    207 
    208 	sc->sc_hwmon_mapped = true;
    209 
    210 	/* Activate monitoring */
    211 	cr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
    212 	SET(cr, 0x01);
    213 	itesio_ecwritereg(sc, ITESIO_EC_CONFIG, cr);
    214 
    215 #ifdef notyet
    216 	/* Enable beep alarms */
    217 	cr = itesio_ecreadreg(sc, ITESIO_EC_BEEPEER);
    218 	SET(cr, 0x02);	/* Voltage exceeds limit */
    219 	SET(cr, 0x04);	/* Temperature exceeds limit */
    220 	itesio_ecwritereg(sc, ITESIO_EC_BEEPEER, cr);
    221 #endif
    222 
    223 	/* Initialize sensors */
    224 	for (i = 0; i < IT_NUM_SENSORS; ++i) {
    225 		sc->sc_data[i].sensor = i;
    226 		sc->sc_data[i].state = ENVSYS_SVALID;
    227 	}
    228 
    229 	itesio_setup_sensors(sc);
    230 
    231 	/*
    232 	 * Hook into the system monitor.
    233 	 */
    234 	sc->sc_sysmon.sme_name = device_xname(self);
    235 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    236 	sc->sc_sysmon.sme_cookie = sc;
    237 	sc->sc_sysmon.sme_gtredata = itesio_gtredata;
    238 	sc->sc_sysmon.sme_nsensors = IT_NUM_SENSORS;
    239 
    240 	if (sysmon_envsys_register(&sc->sc_sysmon)) {
    241 		aprint_error_dev(self, "unable to register with sysmon\n");
    242 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 8);
    243 	}
    244 	sc->sc_hwmon_enabled = true;
    245 }
    246 
    247 static int
    248 itesio_isa_detach(device_t self, int flags)
    249 {
    250 	struct itesio_softc *sc = device_private(self);
    251 
    252 	if (sc->sc_hwmon_enabled)
    253 		sysmon_envsys_unregister(&sc->sc_sysmon);
    254 	if (sc->sc_hwmon_mapped)
    255 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 8);
    256 	return 0;
    257 }
    258 
    259 /*
    260  * Functions to read/write to the Environmental Controller.
    261  */
    262 static uint8_t
    263 itesio_ecreadreg(struct itesio_softc *sc, int reg)
    264 {
    265 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITESIO_EC_ADDR, reg);
    266 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, ITESIO_EC_DATA);
    267 }
    268 
    269 static void
    270 itesio_ecwritereg(struct itesio_softc *sc, int reg, int val)
    271 {
    272 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITESIO_EC_ADDR, reg);
    273 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITESIO_EC_DATA, val);
    274 }
    275 
    276 /*
    277  * Functions to enter/exit/read/write to the Super I/O.
    278  */
    279 static uint8_t
    280 itesio_readreg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg)
    281 {
    282 	bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
    283 	return bus_space_read_1(iot, ioh, ITESIO_DATA);
    284 }
    285 
    286 static void
    287 itesio_writereg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg, int val)
    288 {
    289 	bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
    290 	bus_space_write_1(iot, ioh, ITESIO_DATA, val);
    291 }
    292 
    293 static void
    294 itesio_enter(bus_space_tag_t iot, bus_space_handle_t ioh)
    295 {
    296 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x87);
    297 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x01);
    298 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
    299 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
    300 }
    301 
    302 static void
    303 itesio_exit(bus_space_tag_t iot, bus_space_handle_t ioh)
    304 {
    305 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x02);
    306 	bus_space_write_1(iot, ioh, ITESIO_DATA, 0x02);
    307 }
    308 
    309 
    310 #define COPYDESCR(x, y)				\
    311 	do {					\
    312 		strlcpy((x), (y), sizeof(x));	\
    313 	} while (0)
    314 /*
    315  * sysmon_envsys(9) glue.
    316  */
    317 static void
    318 itesio_setup_sensors(struct itesio_softc *sc)
    319 {
    320 	int i;
    321 
    322 	/* temperatures */
    323 	for (i = 0; i < IT_VOLTSTART_IDX; i++)
    324 		sc->sc_data[i].units = ENVSYS_STEMP;
    325 
    326 	COPYDESCR(sc->sc_data[0].desc, "CPU Temp");
    327 	COPYDESCR(sc->sc_data[1].desc, "System Temp");
    328 	COPYDESCR(sc->sc_data[2].desc, "Aux Temp");
    329 
    330 	/* voltages */
    331 	for (i = IT_VOLTSTART_IDX; i < IT_FANSTART_IDX; i++) {
    332 		sc->sc_data[i].units = ENVSYS_SVOLTS_DC;
    333 		sc->sc_data[i].flags = ENVSYS_FCHANGERFACT;
    334 	}
    335 
    336 	COPYDESCR(sc->sc_data[3].desc, "VCORE_A");
    337 	COPYDESCR(sc->sc_data[4].desc, "VCORE_B");
    338 	COPYDESCR(sc->sc_data[5].desc, "+3.3V");
    339 	COPYDESCR(sc->sc_data[6].desc, "+5V");
    340 	COPYDESCR(sc->sc_data[7].desc, "+12V");
    341 	COPYDESCR(sc->sc_data[8].desc, "-12V");
    342 	COPYDESCR(sc->sc_data[9].desc, "-5V");
    343 	COPYDESCR(sc->sc_data[10].desc, "STANDBY");
    344 	COPYDESCR(sc->sc_data[11].desc, "VBAT");
    345 
    346 	/* fans */
    347 	for (i = IT_FANSTART_IDX; i < IT_NUM_SENSORS; i++)
    348 		sc->sc_data[i].units = ENVSYS_SFANRPM;
    349 
    350 	COPYDESCR(sc->sc_data[12].desc, "CPU Fan");
    351 	COPYDESCR(sc->sc_data[13].desc, "System Fan");
    352 	COPYDESCR(sc->sc_data[14].desc, "Aux Fan");
    353 }
    354 #undef COPYDESCR
    355 
    356 static void
    357 itesio_refresh_temp(struct itesio_softc *sc, envsys_data_t *edata)
    358 {
    359 	int sdata;
    360 
    361 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORTEMPBASE + edata->sensor);
    362 	/* sensor is not connected or reporting invalid data */
    363 	if (sdata == 0 || sdata >= 0xfa) {
    364 		edata->state = ENVSYS_SINVALID;
    365 		return;
    366 	}
    367 
    368 	DPRINTF(("%s: sdata[temp%d] 0x%x\n", __func__, edata->sensor, sdata));
    369 	/* Convert temperature to uK */
    370 	edata->value_cur = sdata * 1000000 + 273150000;
    371 	edata->state = ENVSYS_SVALID;
    372 }
    373 
    374 static void
    375 itesio_refresh_volts(struct itesio_softc *sc, envsys_data_t *edata)
    376 {
    377 	uint8_t vbatcr = 0;
    378 	int i, sdata;
    379 
    380 	i = edata->sensor - IT_VOLTSTART_IDX;
    381 
    382 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORVOLTBASE + i);
    383 	/* not connected */
    384 	if (sdata == 0 || sdata == 0xff) {
    385 		edata->state = ENVSYS_SINVALID;
    386 		return;
    387 	}
    388 
    389 	/*
    390 	 * update VBAT voltage reading every time we read it, to get
    391 	 * latest value.
    392 	 */
    393 	if (i == 8) {
    394 		vbatcr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
    395 		SET(vbatcr, ITESIO_EC_UPDATEVBAT);
    396 		itesio_ecwritereg(sc, ITESIO_EC_CONFIG, vbatcr);
    397 	}
    398 
    399 	DPRINTF(("%s: sdata[volt%d] 0x%x\n", __func__, i, sdata));
    400 
    401 	/* voltage returned as (mV << 4) */
    402 	edata->value_cur = (sdata << 4);
    403 	/* rfact is (factor * 10^4) */
    404 	edata->value_cur *= itesio_vrfact[i];
    405 
    406 	if (edata->rfact)
    407 		edata->value_cur += edata->rfact;
    408 	else
    409 		edata->rfact = itesio_vrfact[i];
    410 
    411 	/* division by 10 gets us back to uVDC */
    412 	edata->value_cur /= 10;
    413 	edata->state = ENVSYS_SVALID;
    414 }
    415 
    416 static void
    417 itesio_refresh_fans(struct itesio_softc *sc, envsys_data_t *edata)
    418 {
    419 	uint8_t mode = 0;
    420 	uint16_t sdata = 0;
    421 	int i, divisor, odivisor, ndivisor;
    422 
    423 	i = edata->sensor - IT_FANSTART_IDX;
    424 	divisor = odivisor = ndivisor = 0;
    425 
    426 	if (sc->sc_chipid == ITESIO_ID8705 || sc->sc_chipid == ITESIO_ID8712) {
    427 		/*
    428 		 * Use the Fan Tachometer Divisor Register for
    429 		 * IT8705F and IT8712F.
    430 		 */
    431 		divisor = odivisor = ndivisor =
    432 		    itesio_ecreadreg(sc, ITESIO_EC_FAN_TDR);
    433 		sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    434 		if (sdata == 0xff) {
    435 			edata->state = ENVSYS_SINVALID;
    436 			if (i == 2)
    437 				ndivisor |= 0x40;
    438 			else {
    439 				ndivisor &= ~(7 << (i * 3));
    440 				ndivisor |= ((divisor + 1) & 7) << (i * 3);
    441 			}
    442 		} else {
    443 			if (i == 2)
    444 				divisor = divisor & 1 ? 3 : 1;
    445 
    446 			if ((sdata << (divisor & 7)) == 0)
    447 				edata->state = ENVSYS_SINVALID;
    448 			else {
    449 				edata->value_cur =
    450 				    1350000 / (sdata << (divisor & 7));
    451 				edata->state = ENVSYS_SVALID;
    452 			}
    453 		}
    454 		DPRINTF(("%s: 8bit sdata[fan%d] 0x%x div: 0x%x\n", __func__,
    455 		    i, sdata, divisor));
    456 		if (ndivisor != odivisor)
    457 			itesio_ecwritereg(sc, ITESIO_EC_FAN_TDR, ndivisor);
    458 	} else {
    459 		mode = itesio_ecreadreg(sc, ITESIO_EC_FAN16_CER);
    460 		sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    461 		if (mode & (1 << i))
    462 			sdata += (itesio_ecreadreg(sc,
    463 			    ITESIO_EC_SENSORFANEXTBASE + i) << 8);
    464 		edata->state = ENVSYS_SVALID;
    465 		if (sdata == 0 ||
    466 		    sdata == ((mode & (1 << i)) ? 0xffff : 0xff))
    467 			edata->state = ENVSYS_SINVALID;
    468 		else {
    469 			edata->value_cur = 1350000 / 2 / sdata;
    470 			edata->state = ENVSYS_SVALID;
    471 		}
    472 		DPRINTF(("%s: 16bit sdata[fan%d] 0x%x\n", __func__, i, sdata));
    473 	}
    474 }
    475 
    476 static int
    477 itesio_gtredata(struct sysmon_envsys *sme, struct envsys_data *edata)
    478 {
    479 	struct itesio_softc *sc = sme->sme_cookie;
    480 
    481 	if (edata->sensor < IT_VOLTSTART_IDX)
    482 		itesio_refresh_temp(sc, edata);
    483 	else if (edata->sensor >= IT_VOLTSTART_IDX &&
    484 	         edata->sensor < IT_FANSTART_IDX)
    485 		itesio_refresh_volts(sc, edata);
    486 	else
    487 		itesio_refresh_fans(sc, edata);
    488 
    489 	return 0;
    490 }
    491