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