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