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