1 /* $NetBSD: itesio_isa.c,v 1.16 2008/04/26 12:59:24 xtraeme Exp $ */
2 /* Derived from $OpenBSD: it.c,v 1.19 2006/04/10 00:57:54 deraadt Exp $ */
5 * Copyright (c) 2006-2007 Juan Romero Pardines <xtraeme@netbsd.org>
6 * Copyright (c) 2003 Julien Bordet <zejames@greyhats.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
31 * Driver for the iTE IT87xxF Super I/O. Currently supporting
32 * the Environmental Controller to monitor the sensors and the
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: itesio_isa.c,v 1.16 2008/04/26 12:59:24 xtraeme Exp $");
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
45 #include <dev/isa/isareg.h>
46 #include <dev/isa/isavar.h>
48 #include <dev/sysmon/sysmonvar.h>
50 #include <dev/isa/itesio_isavar.h>
52 #define IT_VOLTSTART_IDX 3 /* voltage start index */
53 #define IT_FANSTART_IDX 12 /* fan start index */
55 #if defined(ITESIO_DEBUG)
56 #define DPRINTF(x) do { printf x; } while (0)
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.
68 #define RFACT_NONE 10000
69 #define RFACT(x, y) (RFACT_NONE * ((x) + (y)) / (y))
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);
76 CFATTACH_DECL_NEW(itesio
, sizeof(struct itesio_softc
),
77 itesio_isa_match
, itesio_isa_attach
, itesio_isa_detach
, NULL
);
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
);
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
*);
94 /* sysmon_wdog glue */
95 static int itesio_wdt_setmode(struct sysmon_wdog
*);
96 static int itesio_wdt_tickle(struct sysmon_wdog
*);
98 /* rfact values for voltage sensors */
99 static const int itesio_vrfact
[] = {
100 RFACT_NONE
, /* VCORE_A */
101 RFACT_NONE
, /* VCORE_B */
102 RFACT_NONE
, /* +3.3V */
103 RFACT(68, 100), /* +5V */
104 RFACT(30, 10), /* +12V */
105 RFACT(21, 10), /* -5V */
106 RFACT(83, 20), /* -12V */
107 RFACT(68, 100), /* STANDBY */
108 RFACT_NONE
/* VBAT */
112 itesio_isa_match(device_t parent
, cfdata_t match
, void *aux
)
114 struct isa_attach_args
*ia
= aux
;
115 bus_space_handle_t ioh
;
118 /* Must supply an address */
122 if (ISA_DIRECT_CONFIG(ia
))
125 if (ia
->ia_io
[0].ir_addr
== ISA_UNKNOWN_PORT
)
128 if (bus_space_map(ia
->ia_iot
, ia
->ia_io
[0].ir_addr
, 2, 0, &ioh
))
131 itesio_enter(ia
->ia_iot
, ioh
);
132 cr
= (itesio_readreg(ia
->ia_iot
, ioh
, ITESIO_CHIPID1
) << 8);
133 cr
|= itesio_readreg(ia
->ia_iot
, ioh
, ITESIO_CHIPID2
);
134 itesio_exit(ia
->ia_iot
, ioh
);
135 bus_space_unmap(ia
->ia_iot
, ioh
, 2);
144 ia
->ia_io
[0].ir_size
= 2;
155 itesio_isa_attach(device_t parent
, device_t self
, void *aux
)
157 struct itesio_softc
*sc
= device_private(self
);
158 struct isa_attach_args
*ia
= aux
;
162 sc
->sc_iot
= ia
->ia_iot
;
164 if (bus_space_map(sc
->sc_iot
, ia
->ia_io
[0].ir_addr
, 2, 0,
166 aprint_error(": can't map i/o space\n");
173 * Enter to the Super I/O MB PNP mode.
175 itesio_enter(sc
->sc_iot
, sc
->sc_ioh
);
177 * Get info from the Super I/O Global Configuration Registers:
178 * Chip IDs and Device Revision.
180 sc
->sc_chipid
= (itesio_readreg(sc
->sc_iot
, sc
->sc_ioh
,
181 ITESIO_CHIPID1
) << 8);
182 sc
->sc_chipid
|= itesio_readreg(sc
->sc_iot
, sc
->sc_ioh
,
184 sc
->sc_devrev
= (itesio_readreg(sc
->sc_iot
, sc
->sc_ioh
,
185 ITESIO_DEVREV
) & 0x0f);
187 * Select the EC LDN to get the Base Address.
189 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_LDNSEL
, ITESIO_EC_LDN
);
190 sc
->sc_hwmon_baseaddr
=
191 (itesio_readreg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_EC_MSB
) << 8);
192 sc
->sc_hwmon_baseaddr
|= itesio_readreg(sc
->sc_iot
, sc
->sc_ioh
,
195 * We are done, exit MB PNP mode.
197 itesio_exit(sc
->sc_iot
, sc
->sc_ioh
);
199 aprint_normal(": iTE IT%4xF Super I/O (rev %d)\n",
200 sc
->sc_chipid
, sc
->sc_devrev
);
201 aprint_normal_dev(self
, "Hardware Monitor registers at 0x%x\n",
202 sc
->sc_hwmon_baseaddr
);
204 if (bus_space_map(sc
->sc_ec_iot
, sc
->sc_hwmon_baseaddr
, 8, 0,
206 aprint_error_dev(self
, "cannot map hwmon i/o space\n");
210 sc
->sc_hwmon_mapped
= true;
212 /* Activate monitoring */
213 cr
= itesio_ecreadreg(sc
, ITESIO_EC_CONFIG
);
215 itesio_ecwritereg(sc
, ITESIO_EC_CONFIG
, cr
);
218 /* Enable beep alarms */
219 cr
= itesio_ecreadreg(sc
, ITESIO_EC_BEEPEER
);
220 SET(cr
, 0x02); /* Voltage exceeds limit */
221 SET(cr
, 0x04); /* Temperature exceeds limit */
222 itesio_ecwritereg(sc
, ITESIO_EC_BEEPEER
, cr
);
226 * Initialize and attach sensors.
228 itesio_setup_sensors(sc
);
229 sc
->sc_sme
= sysmon_envsys_create();
230 for (i
= 0; i
< IT_NUM_SENSORS
; i
++) {
231 if (sysmon_envsys_sensor_attach(sc
->sc_sme
,
232 &sc
->sc_sensor
[i
])) {
233 sysmon_envsys_destroy(sc
->sc_sme
);
238 * Hook into the system monitor.
240 sc
->sc_sme
->sme_name
= device_xname(self
);
241 sc
->sc_sme
->sme_cookie
= sc
;
242 sc
->sc_sme
->sme_refresh
= itesio_refresh
;
244 if ((i
= sysmon_envsys_register(sc
->sc_sme
))) {
245 aprint_error_dev(self
,
246 "unable to register with sysmon (%d)\n", i
);
247 sysmon_envsys_destroy(sc
->sc_sme
);
250 sc
->sc_hwmon_enabled
= true;
252 if (!pmf_device_register(self
, NULL
, NULL
))
253 aprint_error_dev(self
, "couldn't establish power handler\n");
255 /* The IT8705 doesn't support the WDT */
256 if (sc
->sc_chipid
== ITESIO_ID8705
)
260 * Initialize the watchdog timer.
262 sc
->sc_smw
.smw_name
= device_xname(self
);
263 sc
->sc_smw
.smw_cookie
= sc
;
264 sc
->sc_smw
.smw_setmode
= itesio_wdt_setmode
;
265 sc
->sc_smw
.smw_tickle
= itesio_wdt_tickle
;
266 sc
->sc_smw
.smw_period
= 60;
268 if (sysmon_wdog_register(&sc
->sc_smw
)) {
269 aprint_error_dev(self
, "unable to register watchdog timer\n");
272 sc
->sc_wdt_enabled
= true;
273 aprint_normal_dev(self
, "Watchdog Timer present\n");
277 bus_space_unmap(sc
->sc_ec_iot
, sc
->sc_ec_ioh
, 8);
279 bus_space_unmap(sc
->sc_iot
, sc
->sc_ioh
, 2);
283 itesio_isa_detach(device_t self
, int flags
)
285 struct itesio_softc
*sc
= device_private(self
);
287 if (sc
->sc_hwmon_enabled
)
288 sysmon_envsys_unregister(sc
->sc_sme
);
289 if (sc
->sc_hwmon_mapped
)
290 bus_space_unmap(sc
->sc_ec_iot
, sc
->sc_ec_ioh
, 8);
291 if (sc
->sc_wdt_enabled
) {
292 sysmon_wdog_unregister(&sc
->sc_smw
);
293 bus_space_unmap(sc
->sc_iot
, sc
->sc_ioh
, 2);
300 * Functions to read/write to the Environmental Controller.
303 itesio_ecreadreg(struct itesio_softc
*sc
, int reg
)
305 bus_space_write_1(sc
->sc_ec_iot
, sc
->sc_ec_ioh
, ITESIO_EC_ADDR
, reg
);
306 return bus_space_read_1(sc
->sc_ec_iot
, sc
->sc_ec_ioh
, ITESIO_EC_DATA
);
310 itesio_ecwritereg(struct itesio_softc
*sc
, int reg
, int val
)
312 bus_space_write_1(sc
->sc_ec_iot
, sc
->sc_ec_ioh
, ITESIO_EC_ADDR
, reg
);
313 bus_space_write_1(sc
->sc_ec_iot
, sc
->sc_ec_ioh
, ITESIO_EC_DATA
, val
);
317 * Functions to enter/exit/read/write to the Super I/O.
320 itesio_readreg(bus_space_tag_t iot
, bus_space_handle_t ioh
, int reg
)
322 bus_space_write_1(iot
, ioh
, ITESIO_ADDR
, reg
);
323 return bus_space_read_1(iot
, ioh
, ITESIO_DATA
);
327 itesio_writereg(bus_space_tag_t iot
, bus_space_handle_t ioh
, int reg
, int val
)
329 bus_space_write_1(iot
, ioh
, ITESIO_ADDR
, reg
);
330 bus_space_write_1(iot
, ioh
, ITESIO_DATA
, val
);
334 itesio_enter(bus_space_tag_t iot
, bus_space_handle_t ioh
)
336 bus_space_write_1(iot
, ioh
, ITESIO_ADDR
, 0x87);
337 bus_space_write_1(iot
, ioh
, ITESIO_ADDR
, 0x01);
338 bus_space_write_1(iot
, ioh
, ITESIO_ADDR
, 0x55);
339 bus_space_write_1(iot
, ioh
, ITESIO_ADDR
, 0x55);
343 itesio_exit(bus_space_tag_t iot
, bus_space_handle_t ioh
)
345 bus_space_write_1(iot
, ioh
, ITESIO_ADDR
, 0x02);
346 bus_space_write_1(iot
, ioh
, ITESIO_DATA
, 0x02);
350 #define COPYDESCR(x, y) \
352 strlcpy((x), (y), sizeof(x)); \
355 * sysmon_envsys(9) glue.
358 itesio_setup_sensors(struct itesio_softc
*sc
)
363 for (i
= 0; i
< IT_VOLTSTART_IDX
; i
++)
364 sc
->sc_sensor
[i
].units
= ENVSYS_STEMP
;
366 COPYDESCR(sc
->sc_sensor
[0].desc
, "CPU Temp");
367 COPYDESCR(sc
->sc_sensor
[1].desc
, "System Temp");
368 COPYDESCR(sc
->sc_sensor
[2].desc
, "Aux Temp");
371 for (i
= IT_VOLTSTART_IDX
; i
< IT_FANSTART_IDX
; i
++) {
372 sc
->sc_sensor
[i
].units
= ENVSYS_SVOLTS_DC
;
373 sc
->sc_sensor
[i
].flags
= ENVSYS_FCHANGERFACT
;
376 COPYDESCR(sc
->sc_sensor
[3].desc
, "VCORE_A");
377 COPYDESCR(sc
->sc_sensor
[4].desc
, "VCORE_B");
378 COPYDESCR(sc
->sc_sensor
[5].desc
, "+3.3V");
379 COPYDESCR(sc
->sc_sensor
[6].desc
, "+5V");
380 COPYDESCR(sc
->sc_sensor
[7].desc
, "+12V");
381 COPYDESCR(sc
->sc_sensor
[8].desc
, "-5V");
382 COPYDESCR(sc
->sc_sensor
[9].desc
, "-12V");
383 COPYDESCR(sc
->sc_sensor
[10].desc
, "STANDBY");
384 COPYDESCR(sc
->sc_sensor
[11].desc
, "VBAT");
387 for (i
= IT_FANSTART_IDX
; i
< IT_NUM_SENSORS
; i
++)
388 sc
->sc_sensor
[i
].units
= ENVSYS_SFANRPM
;
390 COPYDESCR(sc
->sc_sensor
[12].desc
, "CPU Fan");
391 COPYDESCR(sc
->sc_sensor
[13].desc
, "System Fan");
392 COPYDESCR(sc
->sc_sensor
[14].desc
, "Aux Fan");
397 itesio_refresh_temp(struct itesio_softc
*sc
, envsys_data_t
*edata
)
401 sdata
= itesio_ecreadreg(sc
, ITESIO_EC_SENSORTEMPBASE
+ edata
->sensor
);
402 /* sensor is not connected or reporting invalid data */
403 if (sdata
== 0 || sdata
>= 0xfa) {
404 edata
->state
= ENVSYS_SINVALID
;
408 DPRINTF(("%s: sdata[temp%d] 0x%x\n", __func__
, edata
->sensor
, sdata
));
409 /* Convert temperature to uK */
410 edata
->value_cur
= sdata
* 1000000 + 273150000;
411 edata
->state
= ENVSYS_SVALID
;
415 itesio_refresh_volts(struct itesio_softc
*sc
, envsys_data_t
*edata
)
420 i
= edata
->sensor
- IT_VOLTSTART_IDX
;
422 sdata
= itesio_ecreadreg(sc
, ITESIO_EC_SENSORVOLTBASE
+ i
);
424 if (sdata
== 0 || sdata
== 0xff) {
425 edata
->state
= ENVSYS_SINVALID
;
430 * update VBAT voltage reading every time we read it, to get
434 vbatcr
= itesio_ecreadreg(sc
, ITESIO_EC_CONFIG
);
435 SET(vbatcr
, ITESIO_EC_UPDATEVBAT
);
436 itesio_ecwritereg(sc
, ITESIO_EC_CONFIG
, vbatcr
);
439 DPRINTF(("%s: sdata[volt%d] 0x%x\n", __func__
, i
, sdata
));
441 /* voltage returned as (mV << 4) */
442 edata
->value_cur
= (sdata
<< 4);
443 /* negative values */
444 if (i
== 5 || i
== 6)
445 edata
->value_cur
-= ITESIO_EC_VREF
;
446 /* rfact is (factor * 10^4) */
447 edata
->value_cur
*= itesio_vrfact
[i
];
449 edata
->value_cur
+= edata
->rfact
;
450 /* division by 10 gets us back to uVDC */
451 edata
->value_cur
/= 10;
452 if (i
== 5 || i
== 6)
453 edata
->value_cur
+= ITESIO_EC_VREF
* 1000;
455 edata
->state
= ENVSYS_SVALID
;
459 itesio_refresh_fans(struct itesio_softc
*sc
, envsys_data_t
*edata
)
463 int i
, divisor
, odivisor
, ndivisor
;
465 i
= edata
->sensor
- IT_FANSTART_IDX
;
466 divisor
= odivisor
= ndivisor
= 0;
468 if (sc
->sc_chipid
== ITESIO_ID8705
|| sc
->sc_chipid
== ITESIO_ID8712
) {
470 * Use the Fan Tachometer Divisor Register for
471 * IT8705F and IT8712F.
473 divisor
= odivisor
= ndivisor
=
474 itesio_ecreadreg(sc
, ITESIO_EC_FAN_TDR
);
475 sdata
= itesio_ecreadreg(sc
, ITESIO_EC_SENSORFANBASE
+ i
);
477 edata
->state
= ENVSYS_SINVALID
;
481 ndivisor
&= ~(7 << (i
* 3));
482 ndivisor
|= ((divisor
+ 1) & 7) << (i
* 3);
486 divisor
= divisor
& 1 ? 3 : 1;
488 if ((sdata
<< (divisor
& 7)) == 0)
489 edata
->state
= ENVSYS_SINVALID
;
492 1350000 / (sdata
<< (divisor
& 7));
493 edata
->state
= ENVSYS_SVALID
;
496 DPRINTF(("%s: 8bit sdata[fan%d] 0x%x div: 0x%x\n", __func__
,
498 if (ndivisor
!= odivisor
)
499 itesio_ecwritereg(sc
, ITESIO_EC_FAN_TDR
, ndivisor
);
501 mode
= itesio_ecreadreg(sc
, ITESIO_EC_FAN16_CER
);
502 sdata
= itesio_ecreadreg(sc
, ITESIO_EC_SENSORFANBASE
+ i
);
504 sdata
+= (itesio_ecreadreg(sc
,
505 ITESIO_EC_SENSORFANEXTBASE
+ i
) << 8);
506 edata
->state
= ENVSYS_SVALID
;
508 sdata
== ((mode
& (1 << i
)) ? 0xffff : 0xff))
509 edata
->state
= ENVSYS_SINVALID
;
511 edata
->value_cur
= 1350000 / 2 / sdata
;
512 edata
->state
= ENVSYS_SVALID
;
514 DPRINTF(("%s: 16bit sdata[fan%d] 0x%x\n", __func__
, i
, sdata
));
519 itesio_refresh(struct sysmon_envsys
*sme
, struct envsys_data
*edata
)
521 struct itesio_softc
*sc
= sme
->sme_cookie
;
523 if (edata
->sensor
< IT_VOLTSTART_IDX
)
524 itesio_refresh_temp(sc
, edata
);
525 else if (edata
->sensor
>= IT_VOLTSTART_IDX
&&
526 edata
->sensor
< IT_FANSTART_IDX
)
527 itesio_refresh_volts(sc
, edata
);
529 itesio_refresh_fans(sc
, edata
);
533 itesio_wdt_setmode(struct sysmon_wdog
*smw
)
535 struct itesio_softc
*sc
= smw
->smw_cookie
;
536 int period
= smw
->smw_period
;
538 /* Enter MB PNP mode and select the WDT LDN */
539 itesio_enter(sc
->sc_iot
, sc
->sc_ioh
);
540 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_LDNSEL
, ITESIO_WDT_LDN
);
542 if ((smw
->smw_mode
& WDOG_MODE_MASK
) == WDOG_MODE_DISARMED
) {
543 /* Disable the watchdog */
544 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_CTL
, 0);
545 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_CNF
, 0);
546 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_TMO_MSB
, 0);
547 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_TMO_LSB
, 0);
549 /* Enable the watchdog */
550 if (period
> ITESIO_WDT_MAXTIMO
|| period
< 1)
551 period
= smw
->smw_period
= ITESIO_WDT_MAXTIMO
;
555 /* set the timeout and start the watchdog */
556 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_TMO_MSB
,
558 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_TMO_LSB
,
560 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_CNF
,
561 ITESIO_WDT_CNF_SECS
| ITESIO_WDT_CNF_KRST
|
562 ITESIO_WDT_CNF_PWROK
);
564 /* we are done, exit MB PNP mode */
565 itesio_exit(sc
->sc_iot
, sc
->sc_ioh
);
571 itesio_wdt_tickle(struct sysmon_wdog
*smw
)
573 struct itesio_softc
*sc
= smw
->smw_cookie
;
574 int period
= smw
->smw_period
* 2;
576 /* refresh timeout value and exit */
577 itesio_enter(sc
->sc_iot
, sc
->sc_ioh
);
578 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_LDNSEL
, ITESIO_WDT_LDN
);
579 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_TMO_MSB
,
581 itesio_writereg(sc
->sc_iot
, sc
->sc_ioh
, ITESIO_WDT_TMO_LSB
,
583 itesio_exit(sc
->sc_iot
, sc
->sc_ioh
);