Home | History | Annotate | Line # | Download | only in isa
itesio_isa.c revision 1.24
      1 /*	$NetBSD: itesio_isa.c,v 1.24 2012/11/15 04:45:01 msaitoh 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 <xtraeme (at) netbsd.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 Environmental Controller to monitor the sensors and the
     33  * Watchdog Timer.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: itesio_isa.c,v 1.24 2012/11/15 04:45:01 msaitoh Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/module.h>
     43 #include <sys/bus.h>
     44 
     45 #include <dev/isa/isareg.h>
     46 #include <dev/isa/isavar.h>
     47 
     48 #include <dev/sysmon/sysmonvar.h>
     49 
     50 #include <dev/isa/itesio_isavar.h>
     51 
     52 #define IT_VOLTSTART_IDX 	3 	/* voltage start index */
     53 #define IT_FANSTART_IDX 	12 	/* fan start index */
     54 
     55 #if defined(ITESIO_DEBUG)
     56 #define DPRINTF(x)		do { printf x; } while (0)
     57 #else
     58 #define DPRINTF(x)
     59 #endif
     60 
     61 /*
     62  * IT87-compatible chips can typically measure voltages up to 4.096 V.
     63  * To measure higher voltages the input is attenuated with (external)
     64  * resistors.  Negative voltages are measured using a reference
     65  * voltage.  So we have to convert the sensor values back to real
     66  * voltages by applying the appropriate resistor factor.
     67  */
     68 #define RFACT_NONE	10000
     69 #define RFACT(x, y)	(RFACT_NONE * ((x) + (y)) / (y))
     70 
     71 /* autoconf(9) functions */
     72 static int	itesio_isa_match(device_t, cfdata_t, void *);
     73 static void	itesio_isa_attach(device_t, device_t, void *);
     74 static int	itesio_isa_detach(device_t, int);
     75 
     76 CFATTACH_DECL_NEW(itesio, sizeof(struct itesio_softc),
     77     itesio_isa_match, itesio_isa_attach, itesio_isa_detach, NULL);
     78 
     79 /* driver functions */
     80 static uint8_t	itesio_ecreadreg(struct itesio_softc *, int);
     81 static void	itesio_ecwritereg(struct itesio_softc *, int, int);
     82 static uint8_t	itesio_readreg(bus_space_tag_t, bus_space_handle_t, int);
     83 static void	itesio_writereg(bus_space_tag_t, bus_space_handle_t, int, int);
     84 static void	itesio_enter(bus_space_tag_t, bus_space_handle_t);
     85 static void	itesio_exit(bus_space_tag_t, bus_space_handle_t);
     86 
     87 /* sysmon_envsys(9) glue */
     88 static void	itesio_setup_sensors(struct itesio_softc *);
     89 static void	itesio_refresh_temp(struct itesio_softc *, envsys_data_t *);
     90 static void	itesio_refresh_volts(struct itesio_softc *, envsys_data_t *);
     91 static void	itesio_refresh_fans(struct itesio_softc *, envsys_data_t *);
     92 static void	itesio_refresh(struct sysmon_envsys *, envsys_data_t *);
     93 
     94 /* sysmon_wdog glue */
     95 static bool	itesio_wdt_suspend(device_t, const pmf_qual_t *);
     96 static int	itesio_wdt_setmode(struct sysmon_wdog *);
     97 static int 	itesio_wdt_tickle(struct sysmon_wdog *);
     98 
     99 /* rfact values for voltage sensors */
    100 static const int itesio_vrfact[] = {
    101 	RFACT_NONE,	/* VCORE_A	*/
    102 	RFACT_NONE,	/* VCORE_B	*/
    103 	RFACT_NONE,	/* +3.3V	*/
    104 	RFACT(68, 100),	/* +5V 		*/
    105 	RFACT(30, 10),	/* +12V 	*/
    106 	RFACT(21, 10),	/* -5V 		*/
    107 	RFACT(83, 20),	/* -12V 	*/
    108 	RFACT(68, 100),	/* STANDBY	*/
    109 	RFACT_NONE	/* VBAT		*/
    110 };
    111 
    112 static int
    113 itesio_isa_match(device_t parent, cfdata_t match, void *aux)
    114 {
    115 	struct isa_attach_args *ia = aux;
    116 	bus_space_handle_t ioh;
    117 	uint16_t cr;
    118 
    119 	/* Must supply an address */
    120 	if (ia->ia_nio < 1)
    121 		return 0;
    122 
    123 	if (ISA_DIRECT_CONFIG(ia))
    124 		return 0;
    125 
    126 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    127 		return 0;
    128 
    129 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh))
    130 		return 0;
    131 
    132 	itesio_enter(ia->ia_iot, ioh);
    133 	cr = (itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID1) << 8);
    134 	cr |= itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID2);
    135 	itesio_exit(ia->ia_iot, ioh);
    136 	bus_space_unmap(ia->ia_iot, ioh, 2);
    137 
    138 	switch (cr) {
    139 	case ITESIO_ID8705:
    140 	case ITESIO_ID8712:
    141 	case ITESIO_ID8716:
    142 	case ITESIO_ID8718:
    143 	case ITESIO_ID8720:
    144 	case ITESIO_ID8721:
    145 	case ITESIO_ID8726:
    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 		return 0;
    154 	}
    155 }
    156 
    157 static void
    158 itesio_isa_attach(device_t parent, device_t self, void *aux)
    159 {
    160 	struct itesio_softc *sc = device_private(self);
    161 	struct isa_attach_args *ia = aux;
    162 	int i;
    163 	uint8_t cr;
    164 
    165 	sc->sc_iot = ia->ia_iot;
    166 
    167 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, 2, 0,
    168 			  &sc->sc_pnp_ioh)) {
    169 		aprint_error(": can't map pnp i/o space\n");
    170 		return;
    171 	}
    172 
    173 	aprint_naive("\n");
    174 
    175 	/*
    176 	 * Enter to the Super I/O MB PNP mode.
    177 	 */
    178 	itesio_enter(sc->sc_iot, sc->sc_pnp_ioh);
    179 	/*
    180 	 * Get info from the Super I/O Global Configuration Registers:
    181 	 * Chip IDs and Device Revision.
    182 	 */
    183 	sc->sc_chipid = (itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh,
    184 	    ITESIO_CHIPID1) << 8);
    185 	sc->sc_chipid |= itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh,
    186 	    ITESIO_CHIPID2);
    187 	sc->sc_devrev = (itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh,
    188 	    ITESIO_DEVREV) & 0x0f);
    189 	/*
    190 	 * Select the EC LDN to get the Base Address.
    191 	 */
    192 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_LDNSEL,
    193 	    ITESIO_EC_LDN);
    194 	sc->sc_hwmon_baseaddr =
    195 	    (itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_EC_MSB) << 8);
    196 	sc->sc_hwmon_baseaddr |= itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh,
    197 	    ITESIO_EC_LSB);
    198 	/*
    199 	 * We are done, exit MB PNP mode.
    200 	 */
    201 	itesio_exit(sc->sc_iot, sc->sc_pnp_ioh);
    202 
    203 	aprint_normal(": iTE IT%4xF Super I/O (rev %d)\n",
    204 	    sc->sc_chipid, sc->sc_devrev);
    205 	aprint_normal_dev(self, "Hardware Monitor registers at 0x%x\n",
    206 	    sc->sc_hwmon_baseaddr);
    207 
    208 	if (bus_space_map(sc->sc_iot, sc->sc_hwmon_baseaddr, 8, 0,
    209 	    &sc->sc_ec_ioh)) {
    210 		aprint_error_dev(self, "cannot map hwmon i/o space\n");
    211 		goto out2;
    212 	}
    213 
    214 	sc->sc_hwmon_mapped = true;
    215 
    216 	/* Activate monitoring */
    217 	cr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
    218 	SET(cr, 0x01);
    219 	itesio_ecwritereg(sc, ITESIO_EC_CONFIG, cr);
    220 
    221 #ifdef notyet
    222 	/* Enable beep alarms */
    223 	cr = itesio_ecreadreg(sc, ITESIO_EC_BEEPEER);
    224 	SET(cr, 0x02);	/* Voltage exceeds limit */
    225 	SET(cr, 0x04);	/* Temperature exceeds limit */
    226 	itesio_ecwritereg(sc, ITESIO_EC_BEEPEER, cr);
    227 #endif
    228 
    229 	/*
    230 	 * Initialize and attach sensors.
    231 	 */
    232 	itesio_setup_sensors(sc);
    233 	sc->sc_sme = sysmon_envsys_create();
    234 	for (i = 0; i < IT_NUM_SENSORS; i++) {
    235 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    236 						&sc->sc_sensor[i])) {
    237 			sysmon_envsys_destroy(sc->sc_sme);
    238 			goto out;
    239 		}
    240 	}
    241 	/*
    242 	 * Hook into the system monitor.
    243 	 */
    244 	sc->sc_sme->sme_name = device_xname(self);
    245 	sc->sc_sme->sme_cookie = sc;
    246 	sc->sc_sme->sme_refresh = itesio_refresh;
    247 
    248 	if ((i = sysmon_envsys_register(sc->sc_sme))) {
    249 		aprint_error_dev(self,
    250 		    "unable to register with sysmon (%d)\n", i);
    251 		sysmon_envsys_destroy(sc->sc_sme);
    252 		goto out;
    253 	}
    254 	sc->sc_hwmon_enabled = true;
    255 
    256 	if (!pmf_device_register(self, NULL, NULL))
    257 		aprint_error_dev(self, "couldn't establish power handler\n");
    258 
    259 	/* The IT8705 doesn't support the WDT */
    260 	if (sc->sc_chipid == ITESIO_ID8705)
    261 		goto out2;
    262 
    263 	/*
    264 	 * Initialize the watchdog timer.
    265 	 */
    266 	sc->sc_smw.smw_name = device_xname(self);
    267 	sc->sc_smw.smw_cookie = sc;
    268 	sc->sc_smw.smw_setmode = itesio_wdt_setmode;
    269 	sc->sc_smw.smw_tickle = itesio_wdt_tickle;
    270 	sc->sc_smw.smw_period = 60;
    271 
    272 	if (sysmon_wdog_register(&sc->sc_smw)) {
    273 		aprint_error_dev(self, "unable to register watchdog timer\n");
    274 		goto out2;
    275 	}
    276 	sc->sc_wdt_enabled = true;
    277 	aprint_normal_dev(self, "Watchdog Timer present\n");
    278 
    279 	pmf_device_deregister(self);
    280 	if (!pmf_device_register(self, itesio_wdt_suspend, NULL))
    281 		aprint_error_dev(self, "couldn't establish power handler\n");
    282 
    283 	return;
    284 
    285 out:
    286 	bus_space_unmap(sc->sc_iot, sc->sc_ec_ioh, 8);
    287 out2:
    288 	bus_space_unmap(sc->sc_iot, sc->sc_pnp_ioh, 2);
    289 }
    290 
    291 static int
    292 itesio_isa_detach(device_t self, int flags)
    293 {
    294 	struct itesio_softc *sc = device_private(self);
    295 
    296 	if (sc->sc_hwmon_enabled)
    297 		sysmon_envsys_unregister(sc->sc_sme);
    298 	if (sc->sc_hwmon_mapped)
    299 		bus_space_unmap(sc->sc_iot, sc->sc_ec_ioh, 8);
    300 	if (sc->sc_wdt_enabled) {
    301 		sysmon_wdog_unregister(&sc->sc_smw);
    302 		bus_space_unmap(sc->sc_iot, sc->sc_pnp_ioh, 2);
    303 	}
    304 
    305 	return 0;
    306 }
    307 
    308 static bool
    309 itesio_wdt_suspend(device_t dev, const pmf_qual_t *qual)
    310 {
    311 	struct itesio_softc *sc = device_private(dev);
    312 
    313 	/* Don't allow suspend if watchdog is armed */
    314 	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
    315 		return false;
    316 	return true;
    317 }
    318 
    319 /*
    320  * Functions to read/write to the Environmental Controller.
    321  */
    322 static uint8_t
    323 itesio_ecreadreg(struct itesio_softc *sc, int reg)
    324 {
    325 	bus_space_write_1(sc->sc_iot, sc->sc_ec_ioh, ITESIO_EC_ADDR, reg);
    326 	return bus_space_read_1(sc->sc_iot, sc->sc_ec_ioh, ITESIO_EC_DATA);
    327 }
    328 
    329 static void
    330 itesio_ecwritereg(struct itesio_softc *sc, int reg, int val)
    331 {
    332 	bus_space_write_1(sc->sc_iot, sc->sc_ec_ioh, ITESIO_EC_ADDR, reg);
    333 	bus_space_write_1(sc->sc_iot, sc->sc_ec_ioh, ITESIO_EC_DATA, val);
    334 }
    335 
    336 /*
    337  * Functions to enter/exit/read/write to the Super I/O.
    338  */
    339 static uint8_t
    340 itesio_readreg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg)
    341 {
    342 	bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
    343 	return bus_space_read_1(iot, ioh, ITESIO_DATA);
    344 }
    345 
    346 static void
    347 itesio_writereg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg, int val)
    348 {
    349 	bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
    350 	bus_space_write_1(iot, ioh, ITESIO_DATA, val);
    351 }
    352 
    353 static void
    354 itesio_enter(bus_space_tag_t iot, bus_space_handle_t ioh)
    355 {
    356 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x87);
    357 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x01);
    358 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
    359 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
    360 }
    361 
    362 static void
    363 itesio_exit(bus_space_tag_t iot, bus_space_handle_t ioh)
    364 {
    365 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x02);
    366 	bus_space_write_1(iot, ioh, ITESIO_DATA, 0x02);
    367 }
    368 
    369 
    370 #define COPYDESCR(x, y)				\
    371 	do {					\
    372 		strlcpy((x), (y), sizeof(x));	\
    373 	} while (0)
    374 /*
    375  * sysmon_envsys(9) glue.
    376  */
    377 static void
    378 itesio_setup_sensors(struct itesio_softc *sc)
    379 {
    380 	int i;
    381 
    382 	/* temperatures */
    383 	for (i = 0; i < IT_VOLTSTART_IDX; i++)
    384 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    385 
    386 	COPYDESCR(sc->sc_sensor[0].desc, "CPU Temp");
    387 	COPYDESCR(sc->sc_sensor[1].desc, "System Temp");
    388 	COPYDESCR(sc->sc_sensor[2].desc, "Aux Temp");
    389 
    390 	/* voltages */
    391 	for (i = IT_VOLTSTART_IDX; i < IT_FANSTART_IDX; i++) {
    392 		sc->sc_sensor[i].units = ENVSYS_SVOLTS_DC;
    393 		sc->sc_sensor[i].flags = ENVSYS_FCHANGERFACT;
    394 	}
    395 
    396 	COPYDESCR(sc->sc_sensor[3].desc, "VCORE_A");
    397 	COPYDESCR(sc->sc_sensor[4].desc, "VCORE_B");
    398 	COPYDESCR(sc->sc_sensor[5].desc, "+3.3V");
    399 	COPYDESCR(sc->sc_sensor[6].desc, "+5V");
    400 	COPYDESCR(sc->sc_sensor[7].desc, "+12V");
    401 	COPYDESCR(sc->sc_sensor[8].desc, "-5V");
    402 	COPYDESCR(sc->sc_sensor[9].desc, "-12V");
    403 	COPYDESCR(sc->sc_sensor[10].desc, "STANDBY");
    404 	COPYDESCR(sc->sc_sensor[11].desc, "VBAT");
    405 
    406 	/* fans */
    407 	for (i = IT_FANSTART_IDX; i < IT_NUM_SENSORS; i++)
    408 		sc->sc_sensor[i].units = ENVSYS_SFANRPM;
    409 
    410 	COPYDESCR(sc->sc_sensor[12].desc, "CPU Fan");
    411 	COPYDESCR(sc->sc_sensor[13].desc, "System Fan");
    412 	COPYDESCR(sc->sc_sensor[14].desc, "Aux Fan");
    413 
    414 	/* all */
    415 	for (i = 0; i < IT_NUM_SENSORS; i++)
    416 		sc->sc_sensor[i].state = ENVSYS_SINVALID;
    417 }
    418 #undef COPYDESCR
    419 
    420 static void
    421 itesio_refresh_temp(struct itesio_softc *sc, envsys_data_t *edata)
    422 {
    423 	int sdata;
    424 
    425 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORTEMPBASE + edata->sensor);
    426 	/* sensor is not connected or reporting invalid data */
    427 	if (sdata == 0 || sdata >= 0xfa) {
    428 		edata->state = ENVSYS_SINVALID;
    429 		return;
    430 	}
    431 
    432 	DPRINTF(("%s: sdata[temp%d] 0x%x\n", __func__, edata->sensor, sdata));
    433 	/* Convert temperature to uK */
    434 	edata->value_cur = sdata * 1000000 + 273150000;
    435 	edata->state = ENVSYS_SVALID;
    436 }
    437 
    438 static void
    439 itesio_refresh_volts(struct itesio_softc *sc, envsys_data_t *edata)
    440 {
    441 	uint8_t vbatcr = 0;
    442 	int i, sdata;
    443 
    444 	i = edata->sensor - IT_VOLTSTART_IDX;
    445 
    446 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORVOLTBASE + i);
    447 	/* not connected */
    448 	if (sdata == 0 || sdata == 0xff) {
    449 		edata->state = ENVSYS_SINVALID;
    450 		return;
    451 	}
    452 
    453 	/*
    454 	 * update VBAT voltage reading every time we read it, to get
    455 	 * latest value.
    456 	 */
    457 	if (i == 8) {
    458 		vbatcr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
    459 		SET(vbatcr, ITESIO_EC_UPDATEVBAT);
    460 		itesio_ecwritereg(sc, ITESIO_EC_CONFIG, vbatcr);
    461 	}
    462 
    463 	DPRINTF(("%s: sdata[volt%d] 0x%x\n", __func__, i, sdata));
    464 
    465 	/* voltage returned as (mV << 4) */
    466 	edata->value_cur = (sdata << 4);
    467 	/* negative values */
    468 	if (i == 5 || i == 6)
    469 		edata->value_cur -= ITESIO_EC_VREF;
    470 	/* rfact is (factor * 10^4) */
    471 	if (edata->rfact)
    472 		edata->value_cur *= edata->rfact;
    473 	else
    474 		edata->value_cur *= itesio_vrfact[i];
    475 	/* division by 10 gets us back to uVDC */
    476 	edata->value_cur /= 10;
    477 	if (i == 5 || i == 6)
    478 		edata->value_cur += ITESIO_EC_VREF * 1000;
    479 
    480 	edata->state = ENVSYS_SVALID;
    481 }
    482 
    483 static void
    484 itesio_refresh_fans(struct itesio_softc *sc, envsys_data_t *edata)
    485 {
    486 	uint8_t mode = 0;
    487 	uint16_t sdata = 0;
    488 	int i, divisor, odivisor, ndivisor;
    489 
    490 	i = edata->sensor - IT_FANSTART_IDX;
    491 	divisor = odivisor = ndivisor = 0;
    492 
    493 	if (sc->sc_chipid == ITESIO_ID8705 || sc->sc_chipid == ITESIO_ID8712) {
    494 		/*
    495 		 * Use the Fan Tachometer Divisor Register for
    496 		 * IT8705F and IT8712F.
    497 		 */
    498 		divisor = odivisor = ndivisor =
    499 		    itesio_ecreadreg(sc, ITESIO_EC_FAN_TDR);
    500 		sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    501 		if (sdata == 0xff) {
    502 			edata->state = ENVSYS_SINVALID;
    503 			if (i == 2)
    504 				ndivisor |= 0x40;
    505 			else {
    506 				ndivisor &= ~(7 << (i * 3));
    507 				ndivisor |= ((divisor + 1) & 7) << (i * 3);
    508 			}
    509 		} else {
    510 			if (i == 2)
    511 				divisor = divisor & 1 ? 3 : 1;
    512 
    513 			if ((sdata << (divisor & 7)) == 0)
    514 				edata->state = ENVSYS_SINVALID;
    515 			else {
    516 				edata->value_cur =
    517 				    1350000 / (sdata << (divisor & 7));
    518 				edata->state = ENVSYS_SVALID;
    519 			}
    520 		}
    521 		DPRINTF(("%s: 8bit sdata[fan%d] 0x%x div: 0x%x\n", __func__,
    522 		    i, sdata, divisor));
    523 		if (ndivisor != odivisor)
    524 			itesio_ecwritereg(sc, ITESIO_EC_FAN_TDR, ndivisor);
    525 	} else {
    526 		mode = itesio_ecreadreg(sc, ITESIO_EC_FAN16_CER);
    527 		sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    528 		if (mode & (1 << i))
    529 			sdata += (itesio_ecreadreg(sc,
    530 			    ITESIO_EC_SENSORFANEXTBASE + i) << 8);
    531 		edata->state = ENVSYS_SVALID;
    532 		if (sdata == 0 ||
    533 		    sdata == ((mode & (1 << i)) ? 0xffff : 0xff))
    534 			edata->state = ENVSYS_SINVALID;
    535 		else {
    536 			edata->value_cur = 1350000 / 2 / sdata;
    537 			edata->state = ENVSYS_SVALID;
    538 		}
    539 		DPRINTF(("%s: 16bit sdata[fan%d] 0x%x\n", __func__, i, sdata));
    540 	}
    541 }
    542 
    543 static void
    544 itesio_refresh(struct sysmon_envsys *sme, struct envsys_data *edata)
    545 {
    546 	struct itesio_softc *sc = sme->sme_cookie;
    547 
    548 	if (edata->sensor < IT_VOLTSTART_IDX)
    549 		itesio_refresh_temp(sc, edata);
    550 	else if (edata->sensor >= IT_VOLTSTART_IDX &&
    551 	         edata->sensor < IT_FANSTART_IDX)
    552 		itesio_refresh_volts(sc, edata);
    553 	else
    554 		itesio_refresh_fans(sc, edata);
    555 }
    556 
    557 static int
    558 itesio_wdt_setmode(struct sysmon_wdog *smw)
    559 {
    560 	struct itesio_softc *sc = smw->smw_cookie;
    561 	int period = smw->smw_period;
    562 
    563 	/* Enter MB PNP mode and select the WDT LDN */
    564 	itesio_enter(sc->sc_iot, sc->sc_pnp_ioh);
    565 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_LDNSEL,
    566 	    ITESIO_WDT_LDN);
    567 
    568 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    569 		/* Disable the watchdog */
    570 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_CTL, 0);
    571 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_CNF, 0);
    572 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_MSB, 0);
    573 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_LSB, 0);
    574 	} else {
    575 		/* Enable the watchdog */
    576 		if (period > ITESIO_WDT_MAXTIMO || period < 1)
    577 			period = smw->smw_period = ITESIO_WDT_MAXTIMO;
    578 
    579 		period *= 2;
    580 
    581 		/* set the timeout and start the watchdog */
    582 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_MSB,
    583 		    period >> 8);
    584 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_LSB,
    585 		    period & 0xff);
    586 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_CNF,
    587 		    ITESIO_WDT_CNF_SECS | ITESIO_WDT_CNF_KRST |
    588 		    ITESIO_WDT_CNF_PWROK);
    589 	}
    590 	/* we are done, exit MB PNP mode */
    591 	itesio_exit(sc->sc_iot, sc->sc_pnp_ioh);
    592 
    593 	return 0;
    594 }
    595 
    596 static int
    597 itesio_wdt_tickle(struct sysmon_wdog *smw)
    598 {
    599 	struct itesio_softc *sc = smw->smw_cookie;
    600 	int period = smw->smw_period * 2;
    601 
    602 	/* refresh timeout value and exit */
    603 	itesio_enter(sc->sc_iot, sc->sc_pnp_ioh);
    604 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_LDNSEL,
    605 	    ITESIO_WDT_LDN);
    606 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_MSB,
    607 	    period >> 8);
    608 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_LSB,
    609 	    period & 0xff);
    610 	itesio_exit(sc->sc_iot, sc->sc_pnp_ioh);
    611 
    612 	return 0;
    613 }
    614 
    615 MODULE(MODULE_CLASS_DRIVER, itesio, NULL);
    616 
    617 #ifdef _MODULE
    618 #include "ioconf.c"
    619 #endif
    620 
    621 static int
    622 itesio_modcmd(modcmd_t cmd, void *opaque)
    623 {
    624 	switch (cmd) {
    625 	case MODULE_CMD_INIT:
    626 #ifdef _MODULE
    627 		return config_init_component(cfdriver_ioconf_itesio,
    628 		    cfattach_ioconf_itesio, cfdata_ioconf_itesio);
    629 #else
    630 		return 0;
    631 #endif
    632 	case MODULE_CMD_FINI:
    633 #ifdef _MODULE
    634 		return config_fini_component(cfdriver_ioconf_itesio,
    635 		    cfattach_ioconf_itesio, cfdata_ioconf_itesio);
    636 #else
    637 		return 0;
    638 #endif
    639 	default:
    640 		return ENOTTY;
    641 	}
    642 }
    643