Home | History | Annotate | Line # | Download | only in isa
itesio_isa.c revision 1.29
      1 /*	$NetBSD: itesio_isa.c,v 1.29 2021/07/03 04:44:16 nonaka 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.29 2021/07/03 04:44:16 nonaka 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 #include <sys/kmem.h>
     45 
     46 #include <dev/isa/isareg.h>
     47 #include <dev/isa/isavar.h>
     48 
     49 #include <dev/sysmon/sysmonvar.h>
     50 
     51 #include <dev/isa/itesio_isavar.h>
     52 
     53 #define IT_VOLTSTART_IDX 	3 	/* voltage start index */
     54 #define IT_FANSTART_IDX 	12 	/* fan start index */
     55 
     56 /* IT8625: 3 temps, 10 volts, 6 fans */
     57 #define IT8625_NUM_SENSORS	19
     58 #define IT8625_VOLTSTART_IDX 	3	/* voltage start index */
     59 #define IT8625_FANSTART_IDX 	13	/* fan start index */
     60 
     61 #if defined(ITESIO_DEBUG)
     62 #define DPRINTF(x)		do { printf x; } while (0)
     63 #else
     64 #define DPRINTF(x)
     65 #endif
     66 
     67 /*
     68  * IT87-compatible chips can typically measure voltages up to 4.096 V.
     69  * To measure higher voltages the input is attenuated with (external)
     70  * resistors.  Negative voltages are measured using a reference
     71  * voltage.  So we have to convert the sensor values back to real
     72  * voltages by applying the appropriate resistor factor.
     73  */
     74 #define RFACT_NONE	10000
     75 #define RFACT(x, y)	(RFACT_NONE * ((x) + (y)) / (y))
     76 
     77 /* autoconf(9) functions */
     78 static int	itesio_isa_match(device_t, cfdata_t, void *);
     79 static void	itesio_isa_attach(device_t, device_t, void *);
     80 static int	itesio_isa_detach(device_t, int);
     81 
     82 CFATTACH_DECL_NEW(itesio, sizeof(struct itesio_softc),
     83     itesio_isa_match, itesio_isa_attach, itesio_isa_detach, NULL);
     84 
     85 /* driver functions */
     86 static uint8_t	itesio_ecreadreg(struct itesio_softc *, int);
     87 static void	itesio_ecwritereg(struct itesio_softc *, int, int);
     88 static uint8_t	itesio_readreg(bus_space_tag_t, bus_space_handle_t, int);
     89 static void	itesio_writereg(bus_space_tag_t, bus_space_handle_t, int, int);
     90 static void	itesio_enter(bus_space_tag_t, bus_space_handle_t);
     91 static void	itesio_exit(bus_space_tag_t, bus_space_handle_t);
     92 
     93 /* sysmon_envsys(9) glue */
     94 static void	itesio_setup_sensors(struct itesio_softc *);
     95 static void	itesio_refresh_temp(struct itesio_softc *, envsys_data_t *);
     96 static void	itesio_refresh_volts(struct itesio_softc *, envsys_data_t *);
     97 static void	itesio_refresh_fans(struct itesio_softc *, envsys_data_t *);
     98 static void	itesio_refresh(struct sysmon_envsys *, envsys_data_t *);
     99 static void	itesio_refresh_it8705_fans(struct itesio_softc *,
    100 		    envsys_data_t *);
    101 static void	itesio_setup_it8625_sensors(struct itesio_softc *);
    102 static void	itesio_refresh_it8625_volts(struct itesio_softc *,
    103 		    envsys_data_t *);
    104 static void	itesio_refresh_it8625_fans(struct itesio_softc *,
    105 		    envsys_data_t *);
    106 
    107 /* sysmon_wdog glue */
    108 static bool	itesio_wdt_suspend(device_t, const pmf_qual_t *);
    109 static int	itesio_wdt_setmode(struct sysmon_wdog *);
    110 static int 	itesio_wdt_tickle(struct sysmon_wdog *);
    111 
    112 /* rfact values for voltage sensors */
    113 static const int itesio_vrfact[] = {
    114 	RFACT_NONE,	/* VCORE_A	*/
    115 	RFACT_NONE,	/* VCORE_B	*/
    116 	RFACT_NONE,	/* +3.3V	*/
    117 	RFACT(68, 100),	/* +5V 		*/
    118 	RFACT(30, 10),	/* +12V 	*/
    119 	RFACT(21, 10),	/* -5V 		*/
    120 	RFACT(83, 20),	/* -12V 	*/
    121 	RFACT(68, 100),	/* STANDBY	*/
    122 	RFACT_NONE	/* VBAT		*/
    123 };
    124 
    125 static const struct itesio_config itesio_config[] = {
    126 	{
    127 		.chipid = ITESIO_ID8625,
    128 		.no_wdt = true,
    129 		.num_sensors = IT8625_NUM_SENSORS,
    130 		.voltstart_idx = IT8625_VOLTSTART_IDX,
    131 		.fanstart_idx = IT8625_FANSTART_IDX,
    132 		.setup_sensors = itesio_setup_it8625_sensors,
    133 		.refresh_volts = itesio_refresh_it8625_volts,
    134 		.refresh_fans = itesio_refresh_it8625_fans,
    135 	},
    136 	{	.chipid = ITESIO_ID8628, },
    137 	{	.chipid = ITESIO_ID8655, },
    138 	{
    139 		.chipid = ITESIO_ID8705,
    140 		.no_wdt = true,
    141 		.refresh_fans = itesio_refresh_it8705_fans,
    142 	},
    143 	{
    144 		.chipid = ITESIO_ID8712,
    145 		.refresh_fans = itesio_refresh_it8705_fans,
    146 	},
    147 	{	.chipid = ITESIO_ID8716, },
    148 	{	.chipid = ITESIO_ID8718, },
    149 	{	.chipid = ITESIO_ID8720, },
    150 	{	.chipid = ITESIO_ID8721, },
    151 	{	.chipid = ITESIO_ID8726, },
    152 	{	.chipid = ITESIO_ID8728, },
    153 	{	.chipid = ITESIO_ID8771, },
    154 	{	.chipid = ITESIO_ID8772, },
    155 };
    156 
    157 static const struct itesio_config *
    158 itesio_isa_find_config(uint16_t chipid)
    159 {
    160 	const struct itesio_config *ic;
    161 	size_t i;
    162 
    163 	for (i = 0; i < __arraycount(itesio_config); i++) {
    164 		ic = &itesio_config[i];
    165 		if (chipid == ic->chipid)
    166 			return ic;
    167 	}
    168 	return NULL;
    169 }
    170 
    171 static int
    172 itesio_isa_match(device_t parent, cfdata_t match, void *aux)
    173 {
    174 	struct isa_attach_args *ia = aux;
    175 	bus_space_handle_t ioh;
    176 	const struct itesio_config *ic;
    177 	uint16_t cr;
    178 
    179 	/* Must supply an address */
    180 	if (ia->ia_nio < 1)
    181 		return 0;
    182 
    183 	if (ISA_DIRECT_CONFIG(ia))
    184 		return 0;
    185 
    186 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    187 		return 0;
    188 
    189 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh))
    190 		return 0;
    191 
    192 	itesio_enter(ia->ia_iot, ioh);
    193 	cr = (itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID1) << 8);
    194 	cr |= itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID2);
    195 	itesio_exit(ia->ia_iot, ioh);
    196 	bus_space_unmap(ia->ia_iot, ioh, 2);
    197 
    198 	ic = itesio_isa_find_config(cr);
    199 	if (ic == NULL)
    200 		return 0;
    201 
    202 	ia->ia_nio = 1;
    203 	ia->ia_io[0].ir_size = 2;
    204 	ia->ia_niomem = 0;
    205 	ia->ia_nirq = 0;
    206 	ia->ia_ndrq = 0;
    207 	return 1;
    208 }
    209 
    210 static void
    211 itesio_isa_attach(device_t parent, device_t self, void *aux)
    212 {
    213 	struct itesio_softc *sc = device_private(self);
    214 	struct isa_attach_args *ia = aux;
    215 	const struct itesio_config *ic;
    216 	uint32_t i;
    217 	int error;
    218 	uint8_t cr;
    219 
    220 	sc->sc_iot = ia->ia_iot;
    221 
    222 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, 2, 0,
    223 			  &sc->sc_pnp_ioh)) {
    224 		aprint_error(": can't map pnp i/o space\n");
    225 		return;
    226 	}
    227 
    228 	aprint_naive("\n");
    229 
    230 	/*
    231 	 * Enter to the Super I/O MB PNP mode.
    232 	 */
    233 	itesio_enter(sc->sc_iot, sc->sc_pnp_ioh);
    234 	/*
    235 	 * Get info from the Super I/O Global Configuration Registers:
    236 	 * Chip IDs and Device Revision.
    237 	 */
    238 	sc->sc_chipid = (itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh,
    239 	    ITESIO_CHIPID1) << 8);
    240 	sc->sc_chipid |= itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh,
    241 	    ITESIO_CHIPID2);
    242 	sc->sc_devrev = (itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh,
    243 	    ITESIO_DEVREV) & 0x0f);
    244 	/*
    245 	 * Select the EC LDN to get the Base Address.
    246 	 */
    247 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_LDNSEL,
    248 	    ITESIO_EC_LDN);
    249 	sc->sc_hwmon_baseaddr =
    250 	    (itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_EC_MSB) << 8);
    251 	sc->sc_hwmon_baseaddr |= itesio_readreg(sc->sc_iot, sc->sc_pnp_ioh,
    252 	    ITESIO_EC_LSB);
    253 	/*
    254 	 * We are done, exit MB PNP mode.
    255 	 */
    256 	itesio_exit(sc->sc_iot, sc->sc_pnp_ioh);
    257 
    258 	ic = itesio_isa_find_config(sc->sc_chipid);
    259 	if (ic == NULL) {
    260 		aprint_error(": unknown chipid: %04x", sc->sc_chipid);
    261 		goto out2;
    262 	}
    263 	sc->sc_config = *ic;
    264 	if (sc->sc_config.num_sensors == 0)
    265 		sc->sc_config.num_sensors = IT_NUM_SENSORS;
    266 	if (sc->sc_config.voltstart_idx == 0)
    267 		sc->sc_config.voltstart_idx = IT_VOLTSTART_IDX;
    268 	if (sc->sc_config.fanstart_idx == 0)
    269 		sc->sc_config.fanstart_idx = IT_FANSTART_IDX;
    270 	if (sc->sc_config.setup_sensors == NULL)
    271 		sc->sc_config.setup_sensors = itesio_setup_sensors;
    272 	if (sc->sc_config.refresh_temp == NULL)
    273 		sc->sc_config.refresh_temp = itesio_refresh_temp;
    274 	if (sc->sc_config.refresh_volts == NULL)
    275 		sc->sc_config.refresh_volts = itesio_refresh_volts;
    276 	if (sc->sc_config.refresh_fans == NULL)
    277 		sc->sc_config.refresh_fans = itesio_refresh_fans;
    278 
    279 	aprint_normal(": iTE IT%4xF Super I/O (rev %d)\n",
    280 	    sc->sc_chipid, sc->sc_devrev);
    281 	aprint_normal_dev(self, "Hardware Monitor registers at 0x%x\n",
    282 	    sc->sc_hwmon_baseaddr);
    283 
    284 	if (bus_space_map(sc->sc_iot, sc->sc_hwmon_baseaddr, 8, 0,
    285 	    &sc->sc_ec_ioh)) {
    286 		aprint_error_dev(self, "cannot map hwmon i/o space\n");
    287 		goto out2;
    288 	}
    289 
    290 	sc->sc_hwmon_mapped = true;
    291 
    292 	/* Activate monitoring */
    293 	cr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
    294 	SET(cr, 0x01);
    295 	itesio_ecwritereg(sc, ITESIO_EC_CONFIG, cr);
    296 
    297 #ifdef notyet
    298 	/* Enable beep alarms */
    299 	cr = itesio_ecreadreg(sc, ITESIO_EC_BEEPEER);
    300 	SET(cr, 0x02);	/* Voltage exceeds limit */
    301 	SET(cr, 0x04);	/* Temperature exceeds limit */
    302 	itesio_ecwritereg(sc, ITESIO_EC_BEEPEER, cr);
    303 #endif
    304 
    305 	/*
    306 	 * Initialize and attach sensors.
    307 	 */
    308 	(*sc->sc_config.setup_sensors)(sc);
    309 	sc->sc_sme = sysmon_envsys_create();
    310 	for (i = 0; i < sc->sc_config.num_sensors; i++) {
    311 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    312 						&sc->sc_sensor[i])) {
    313 			sysmon_envsys_destroy(sc->sc_sme);
    314 			goto out;
    315 		}
    316 	}
    317 	/*
    318 	 * Hook into the system monitor.
    319 	 */
    320 	sc->sc_sme->sme_name = device_xname(self);
    321 	sc->sc_sme->sme_cookie = sc;
    322 	sc->sc_sme->sme_refresh = itesio_refresh;
    323 
    324 	if ((error = sysmon_envsys_register(sc->sc_sme))) {
    325 		aprint_error_dev(self,
    326 		    "unable to register with sysmon (%d)\n", error);
    327 		sysmon_envsys_destroy(sc->sc_sme);
    328 		goto out;
    329 	}
    330 	sc->sc_hwmon_enabled = true;
    331 
    332 	if (!pmf_device_register(self, NULL, NULL))
    333 		aprint_error_dev(self, "couldn't establish power handler\n");
    334 
    335 	/* Some chips don't support the WDT */
    336 	if (sc->sc_config.no_wdt)
    337 		goto out2;
    338 
    339 	/*
    340 	 * Initialize the watchdog timer.
    341 	 */
    342 	sc->sc_smw.smw_name = device_xname(self);
    343 	sc->sc_smw.smw_cookie = sc;
    344 	sc->sc_smw.smw_setmode = itesio_wdt_setmode;
    345 	sc->sc_smw.smw_tickle = itesio_wdt_tickle;
    346 	sc->sc_smw.smw_period = 60;
    347 
    348 	if (sysmon_wdog_register(&sc->sc_smw)) {
    349 		aprint_error_dev(self, "unable to register watchdog timer\n");
    350 		goto out2;
    351 	}
    352 	sc->sc_wdt_enabled = true;
    353 	aprint_normal_dev(self, "Watchdog Timer present\n");
    354 
    355 	pmf_device_deregister(self);
    356 	if (!pmf_device_register(self, itesio_wdt_suspend, NULL))
    357 		aprint_error_dev(self, "couldn't establish power handler\n");
    358 
    359 	return;
    360 
    361 out:
    362 	bus_space_unmap(sc->sc_iot, sc->sc_ec_ioh, 8);
    363 out2:
    364 	bus_space_unmap(sc->sc_iot, sc->sc_pnp_ioh, 2);
    365 }
    366 
    367 static int
    368 itesio_isa_detach(device_t self, int flags)
    369 {
    370 	struct itesio_softc *sc = device_private(self);
    371 
    372 	if (sc->sc_hwmon_enabled)
    373 		sysmon_envsys_unregister(sc->sc_sme);
    374 	if (sc->sc_hwmon_mapped)
    375 		bus_space_unmap(sc->sc_iot, sc->sc_ec_ioh, 8);
    376 	if (sc->sc_wdt_enabled) {
    377 		sysmon_wdog_unregister(&sc->sc_smw);
    378 		bus_space_unmap(sc->sc_iot, sc->sc_pnp_ioh, 2);
    379 	}
    380 
    381 	if (sc->sc_sensor != NULL) {
    382 		kmem_free(sc->sc_sensor,
    383 		    sizeof(sc->sc_sensor[0]) * sc->sc_config.num_sensors);
    384 	}
    385 
    386 	return 0;
    387 }
    388 
    389 static bool
    390 itesio_wdt_suspend(device_t dev, const pmf_qual_t *qual)
    391 {
    392 	struct itesio_softc *sc = device_private(dev);
    393 
    394 	/* Don't allow suspend if watchdog is armed */
    395 	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
    396 		return false;
    397 	return true;
    398 }
    399 
    400 /*
    401  * Functions to read/write to the Environmental Controller.
    402  */
    403 static uint8_t
    404 itesio_ecreadreg(struct itesio_softc *sc, int reg)
    405 {
    406 	bus_space_write_1(sc->sc_iot, sc->sc_ec_ioh, ITESIO_EC_ADDR, reg);
    407 	return bus_space_read_1(sc->sc_iot, sc->sc_ec_ioh, ITESIO_EC_DATA);
    408 }
    409 
    410 static void
    411 itesio_ecwritereg(struct itesio_softc *sc, int reg, int val)
    412 {
    413 	bus_space_write_1(sc->sc_iot, sc->sc_ec_ioh, ITESIO_EC_ADDR, reg);
    414 	bus_space_write_1(sc->sc_iot, sc->sc_ec_ioh, ITESIO_EC_DATA, val);
    415 }
    416 
    417 /*
    418  * Functions to enter/exit/read/write to the Super I/O.
    419  */
    420 static uint8_t
    421 itesio_readreg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg)
    422 {
    423 	bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
    424 	return bus_space_read_1(iot, ioh, ITESIO_DATA);
    425 }
    426 
    427 static void
    428 itesio_writereg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg, int val)
    429 {
    430 	bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
    431 	bus_space_write_1(iot, ioh, ITESIO_DATA, val);
    432 }
    433 
    434 static void
    435 itesio_enter(bus_space_tag_t iot, bus_space_handle_t ioh)
    436 {
    437 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x87);
    438 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x01);
    439 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
    440 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
    441 }
    442 
    443 static void
    444 itesio_exit(bus_space_tag_t iot, bus_space_handle_t ioh)
    445 {
    446 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x02);
    447 	bus_space_write_1(iot, ioh, ITESIO_DATA, 0x02);
    448 }
    449 
    450 
    451 #define COPYDESCR(x, y)				\
    452 	do {					\
    453 		strlcpy((x), (y), sizeof(x));	\
    454 	} while (0)
    455 /*
    456  * sysmon_envsys(9) glue.
    457  */
    458 static void
    459 itesio_setup_sensors_common(struct itesio_softc *sc)
    460 {
    461 	const struct itesio_config *ic = &sc->sc_config;
    462 	size_t allocsz;
    463 	uint32_t i;
    464 
    465 	allocsz = sizeof(sc->sc_sensor[0]) * ic->num_sensors;
    466 	sc->sc_sensor = kmem_zalloc(allocsz, KM_SLEEP);
    467 
    468 	/* temperatures */
    469 	for (i = 0; i < ic->voltstart_idx; i++)
    470 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    471 
    472 	/* voltages */
    473 	for (i = ic->voltstart_idx; i < ic->fanstart_idx; i++) {
    474 		sc->sc_sensor[i].units = ENVSYS_SVOLTS_DC;
    475 		sc->sc_sensor[i].flags = ENVSYS_FCHANGERFACT;
    476 	}
    477 
    478 	/* fans */
    479 	for (i = ic->fanstart_idx; i < ic->num_sensors; i++)
    480 		sc->sc_sensor[i].units = ENVSYS_SFANRPM;
    481 
    482 	/* all */
    483 	for (i = 0; i < ic->num_sensors; i++)
    484 		sc->sc_sensor[i].state = ENVSYS_SINVALID;
    485 }
    486 
    487 static void
    488 itesio_setup_sensors(struct itesio_softc *sc)
    489 {
    490 
    491 	itesio_setup_sensors_common(sc);
    492 
    493 	/* temperatures */
    494 	COPYDESCR(sc->sc_sensor[0].desc, "CPU Temp");
    495 	COPYDESCR(sc->sc_sensor[1].desc, "System Temp");
    496 	COPYDESCR(sc->sc_sensor[2].desc, "Aux Temp");
    497 
    498 	/* voltages */
    499 	COPYDESCR(sc->sc_sensor[3].desc, "VCORE_A");
    500 	COPYDESCR(sc->sc_sensor[4].desc, "VCORE_B");
    501 	COPYDESCR(sc->sc_sensor[5].desc, "+3.3V");
    502 	COPYDESCR(sc->sc_sensor[6].desc, "+5V");
    503 	COPYDESCR(sc->sc_sensor[7].desc, "+12V");
    504 	COPYDESCR(sc->sc_sensor[8].desc, "-5V");
    505 	COPYDESCR(sc->sc_sensor[9].desc, "-12V");
    506 	COPYDESCR(sc->sc_sensor[10].desc, "STANDBY");
    507 	COPYDESCR(sc->sc_sensor[11].desc, "VBAT");
    508 
    509 	/* fans */
    510 	COPYDESCR(sc->sc_sensor[12].desc, "CPU Fan");
    511 	COPYDESCR(sc->sc_sensor[13].desc, "System Fan");
    512 	COPYDESCR(sc->sc_sensor[14].desc, "Aux Fan");
    513 }
    514 
    515 static void
    516 itesio_setup_it8625_sensors(struct itesio_softc *sc)
    517 {
    518 
    519 	itesio_setup_sensors_common(sc);
    520 
    521 	/* temperatures */
    522 	COPYDESCR(sc->sc_sensor[0].desc, "Temp0");
    523 	COPYDESCR(sc->sc_sensor[1].desc, "Temp1");
    524 	COPYDESCR(sc->sc_sensor[2].desc, "Temp2");
    525 
    526 	/* voltages */
    527 	COPYDESCR(sc->sc_sensor[3].desc, "VIN0");
    528 	COPYDESCR(sc->sc_sensor[4].desc, "VIN1");
    529 	COPYDESCR(sc->sc_sensor[5].desc, "VIN2");
    530 	COPYDESCR(sc->sc_sensor[6].desc, "VIN3");
    531 	COPYDESCR(sc->sc_sensor[7].desc, "VIN4");
    532 	COPYDESCR(sc->sc_sensor[8].desc, "VIN5");
    533 	COPYDESCR(sc->sc_sensor[9].desc, "VIN6");
    534 	COPYDESCR(sc->sc_sensor[10].desc, "Internal 3VSB");
    535 	COPYDESCR(sc->sc_sensor[11].desc, "VBAT");
    536 	COPYDESCR(sc->sc_sensor[12].desc, "Internal AVCC3");
    537 
    538 	/* fans */
    539 	COPYDESCR(sc->sc_sensor[13].desc, "Fan0");
    540 	COPYDESCR(sc->sc_sensor[14].desc, "Fan1");
    541 	COPYDESCR(sc->sc_sensor[15].desc, "Fan2");
    542 	COPYDESCR(sc->sc_sensor[16].desc, "Fan3");
    543 	COPYDESCR(sc->sc_sensor[17].desc, "Fan4");
    544 	COPYDESCR(sc->sc_sensor[18].desc, "Fan5");
    545 }
    546 #undef COPYDESCR
    547 
    548 static void
    549 itesio_refresh_temp(struct itesio_softc *sc, envsys_data_t *edata)
    550 {
    551 	int sdata;
    552 
    553 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORTEMPBASE + edata->sensor);
    554 	/* sensor is not connected or reporting invalid data */
    555 	if (sdata == 0 || sdata >= 0xfa) {
    556 		edata->state = ENVSYS_SINVALID;
    557 		return;
    558 	}
    559 
    560 	DPRINTF(("%s: sdata[temp%d] 0x%x\n", __func__, edata->sensor, sdata));
    561 	/* Convert temperature to uK */
    562 	edata->value_cur = sdata * 1000000 + 273150000;
    563 	edata->state = ENVSYS_SVALID;
    564 }
    565 
    566 static void
    567 itesio_refresh_volts(struct itesio_softc *sc, envsys_data_t *edata)
    568 {
    569 	uint8_t vbatcr = 0;
    570 	int i, sdata;
    571 
    572 	i = edata->sensor - sc->sc_config.voltstart_idx;
    573 
    574 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORVOLTBASE + i);
    575 	/* not connected */
    576 	if (sdata == 0 || sdata == 0xff) {
    577 		edata->state = ENVSYS_SINVALID;
    578 		return;
    579 	}
    580 
    581 	/*
    582 	 * update VBAT voltage reading every time we read it, to get
    583 	 * latest value.
    584 	 */
    585 	if (i == 8) {
    586 		vbatcr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
    587 		SET(vbatcr, ITESIO_EC_UPDATEVBAT);
    588 		itesio_ecwritereg(sc, ITESIO_EC_CONFIG, vbatcr);
    589 	}
    590 
    591 	DPRINTF(("%s: sdata[volt%d] 0x%x\n", __func__, i, sdata));
    592 
    593 	/* voltage returned as (mV << 4) */
    594 	edata->value_cur = (sdata << 4);
    595 	/* negative values */
    596 	if (i == 5 || i == 6)
    597 		edata->value_cur -= ITESIO_EC_VREF;
    598 	/* rfact is (factor * 10^4) */
    599 	if (edata->rfact)
    600 		edata->value_cur *= edata->rfact;
    601 	else
    602 		edata->value_cur *= itesio_vrfact[i];
    603 	/* division by 10 gets us back to uVDC */
    604 	edata->value_cur /= 10;
    605 	if (i == 5 || i == 6)
    606 		edata->value_cur += ITESIO_EC_VREF * 1000;
    607 
    608 	edata->state = ENVSYS_SVALID;
    609 }
    610 
    611 static void
    612 itesio_refresh_it8625_volts(struct itesio_softc *sc, envsys_data_t *edata)
    613 {
    614 	int i, sdata;
    615 
    616 	i = edata->sensor - sc->sc_config.voltstart_idx;
    617 
    618 	if (i < 9)
    619 		sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORVOLTBASE + i);
    620 	else
    621 		sdata = itesio_ecreadreg(sc,
    622 		    ITESIO_EC_SENSORVOLTEXTBASE + i - 9);
    623 	/* not connected */
    624 	if (sdata == 0 || sdata == 0xff) {
    625 		edata->state = ENVSYS_SINVALID;
    626 		return;
    627 	}
    628 
    629 	DPRINTF(("%s: sdata[volt%d] 0x%x\n", __func__, i, sdata));
    630 
    631 	if (i == 7) {
    632 		/* Internal 3VSB: reading value * 2 * 10.9mV */
    633 		edata->value_cur = sdata * 2 * 109;
    634 	} else {
    635 		/* other: reading value * 10.9mV */
    636 		edata->value_cur = sdata * 109;
    637 	}
    638 	/* rfact is (factor * 10^4) */
    639 	if (edata->rfact)
    640 		edata->value_cur *= edata->rfact;
    641 	else
    642 		edata->value_cur *= RFACT_NONE;
    643 	/* division by 100 gets us back to uVDC */
    644 	edata->value_cur /= 100;
    645 	edata->state = ENVSYS_SVALID;
    646 }
    647 
    648 static void
    649 itesio_refresh_fans(struct itesio_softc *sc, envsys_data_t *edata)
    650 {
    651 	uint8_t mode;
    652 	uint16_t sdata;
    653 	int i;
    654 
    655 	i = edata->sensor - sc->sc_config.fanstart_idx;
    656 
    657 	mode = itesio_ecreadreg(sc, ITESIO_EC_FAN16_CER);
    658 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    659 	if (mode & (1 << i))
    660 		sdata += (itesio_ecreadreg(sc,
    661 		    ITESIO_EC_SENSORFANEXTBASE + i) << 8);
    662 	edata->state = ENVSYS_SVALID;
    663 	if (sdata == 0 ||
    664 	    sdata == ((mode & (1 << i)) ? 0xffff : 0xff))
    665 		edata->state = ENVSYS_SINVALID;
    666 	else {
    667 		edata->value_cur = 1350000 / 2 / sdata;
    668 		edata->state = ENVSYS_SVALID;
    669 	}
    670 	DPRINTF(("%s: 16bit sdata[fan%d] 0x%x\n", __func__, i, sdata));
    671 }
    672 
    673 static void
    674 itesio_refresh_it8705_fans(struct itesio_softc *sc, envsys_data_t *edata)
    675 {
    676 	uint16_t sdata;
    677 	int i, divisor, odivisor, ndivisor;
    678 
    679 	i = edata->sensor - sc->sc_config.fanstart_idx;
    680 
    681 	/*
    682 	 * Use the Fan Tachometer Divisor Register for
    683 	 * IT8705F and IT8712F.
    684 	 */
    685 	divisor = odivisor = ndivisor =
    686 	    itesio_ecreadreg(sc, ITESIO_EC_FAN_TDR);
    687 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    688 	if (sdata == 0xff) {
    689 		edata->state = ENVSYS_SINVALID;
    690 		if (i == 2)
    691 			ndivisor |= 0x40;
    692 		else {
    693 			ndivisor &= ~(7 << (i * 3));
    694 			ndivisor |= ((divisor + 1) & 7) << (i * 3);
    695 		}
    696 	} else {
    697 		if (i == 2)
    698 			divisor = divisor & 1 ? 3 : 1;
    699 
    700 		if ((sdata << (divisor & 7)) == 0)
    701 			edata->state = ENVSYS_SINVALID;
    702 		else {
    703 			edata->value_cur =
    704 			    1350000 / (sdata << (divisor & 7));
    705 			edata->state = ENVSYS_SVALID;
    706 		}
    707 	}
    708 	DPRINTF(("%s: 8bit sdata[fan%d] 0x%x div: 0x%x\n", __func__,
    709 	    i, sdata, divisor));
    710 	if (ndivisor != odivisor)
    711 		itesio_ecwritereg(sc, ITESIO_EC_FAN_TDR, ndivisor);
    712 }
    713 
    714 static void
    715 itesio_refresh_it8625_fans(struct itesio_softc *sc, envsys_data_t *edata)
    716 {
    717 	uint16_t sdata;
    718 	int i;
    719 
    720 	i = edata->sensor - sc->sc_config.fanstart_idx;
    721 
    722 	switch (i) {
    723 	case 0:
    724 	case 1:
    725 	case 2:
    726 		sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    727 		sdata += (itesio_ecreadreg(sc,
    728 		    ITESIO_EC_SENSORFANEXTBASE + i) << 8);
    729 		break;
    730 	case 3:
    731 		sdata = itesio_ecreadreg(sc, IT8625_EC_SENSORFAN4_LSB);
    732 		sdata += itesio_ecreadreg(sc, IT8625_EC_SENSORFAN4_MSB) << 8;
    733 		break;
    734 	case 4:
    735 		sdata = itesio_ecreadreg(sc, IT8625_EC_SENSORFAN5_LSB);
    736 		sdata += itesio_ecreadreg(sc, IT8625_EC_SENSORFAN5_MSB) << 8;
    737 		break;
    738 	case 5:
    739 		sdata = itesio_ecreadreg(sc, IT8625_EC_SENSORFAN6_LSB);
    740 		sdata += itesio_ecreadreg(sc, IT8625_EC_SENSORFAN6_MSB) << 8;
    741 		break;
    742 	default:
    743 		edata->state = ENVSYS_SINVALID;
    744 		return;
    745 	}
    746 	edata->state = ENVSYS_SVALID;
    747 	if (sdata == 0 || sdata == 0xffff)
    748 		edata->state = ENVSYS_SINVALID;
    749 	else {
    750 		edata->value_cur = 1350000 / 2 / sdata;
    751 		edata->state = ENVSYS_SVALID;
    752 	}
    753 	DPRINTF(("%s: 16bit sdata[fan%d] 0x%x\n", __func__, i, sdata));
    754 }
    755 
    756 static void
    757 itesio_refresh(struct sysmon_envsys *sme, struct envsys_data *edata)
    758 {
    759 	struct itesio_softc *sc = sme->sme_cookie;
    760 
    761 	if (edata->sensor < sc->sc_config.voltstart_idx)
    762 		(*sc->sc_config.refresh_temp)(sc, edata);
    763 	else if (edata->sensor >= sc->sc_config.voltstart_idx &&
    764 	    edata->sensor < sc->sc_config.fanstart_idx)
    765 		(*sc->sc_config.refresh_volts)(sc, edata);
    766 	else
    767 		(*sc->sc_config.refresh_fans)(sc, edata);
    768 }
    769 
    770 static int
    771 itesio_wdt_setmode(struct sysmon_wdog *smw)
    772 {
    773 	struct itesio_softc *sc = smw->smw_cookie;
    774 	int period = smw->smw_period;
    775 
    776 	/* Enter MB PNP mode and select the WDT LDN */
    777 	itesio_enter(sc->sc_iot, sc->sc_pnp_ioh);
    778 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_LDNSEL,
    779 	    ITESIO_WDT_LDN);
    780 
    781 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    782 		/* Disable the watchdog */
    783 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_CTL, 0);
    784 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_CNF, 0);
    785 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_MSB, 0);
    786 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_LSB, 0);
    787 	} else {
    788 		/* Enable the watchdog */
    789 		if (period > ITESIO_WDT_MAXTIMO || period < 1)
    790 			period = smw->smw_period = ITESIO_WDT_MAXTIMO;
    791 
    792 		period *= 2;
    793 
    794 		/* set the timeout and start the watchdog */
    795 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_MSB,
    796 		    period >> 8);
    797 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_LSB,
    798 		    period & 0xff);
    799 		itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_CNF,
    800 		    ITESIO_WDT_CNF_SECS | ITESIO_WDT_CNF_KRST |
    801 		    ITESIO_WDT_CNF_PWROK);
    802 	}
    803 	/* we are done, exit MB PNP mode */
    804 	itesio_exit(sc->sc_iot, sc->sc_pnp_ioh);
    805 
    806 	return 0;
    807 }
    808 
    809 static int
    810 itesio_wdt_tickle(struct sysmon_wdog *smw)
    811 {
    812 	struct itesio_softc *sc = smw->smw_cookie;
    813 	int period = smw->smw_period * 2;
    814 
    815 	/* refresh timeout value and exit */
    816 	itesio_enter(sc->sc_iot, sc->sc_pnp_ioh);
    817 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_LDNSEL,
    818 	    ITESIO_WDT_LDN);
    819 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_MSB,
    820 	    period >> 8);
    821 	itesio_writereg(sc->sc_iot, sc->sc_pnp_ioh, ITESIO_WDT_TMO_LSB,
    822 	    period & 0xff);
    823 	itesio_exit(sc->sc_iot, sc->sc_pnp_ioh);
    824 
    825 	return 0;
    826 }
    827 
    828 MODULE(MODULE_CLASS_DRIVER, itesio, "sysmon_envsys,sysmon_wdog");
    829 
    830 #ifdef _MODULE
    831 #include "ioconf.c"
    832 #endif
    833 
    834 static int
    835 itesio_modcmd(modcmd_t cmd, void *opaque)
    836 {
    837 	switch (cmd) {
    838 	case MODULE_CMD_INIT:
    839 #ifdef _MODULE
    840 		return config_init_component(cfdriver_ioconf_itesio,
    841 		    cfattach_ioconf_itesio, cfdata_ioconf_itesio);
    842 #else
    843 		return 0;
    844 #endif
    845 	case MODULE_CMD_FINI:
    846 #ifdef _MODULE
    847 		return config_fini_component(cfdriver_ioconf_itesio,
    848 		    cfattach_ioconf_itesio, cfdata_ioconf_itesio);
    849 #else
    850 		return 0;
    851 #endif
    852 	default:
    853 		return ENOTTY;
    854 	}
    855 }
    856