itesio_isa.c revision 1.6.6.1 1 /* $NetBSD: itesio_isa.c,v 1.6.6.1 2007/12/11 15:29:20 yamt 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 <juan (at) xtrarom.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 Hardware monitor part in the Environmental Controller.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: itesio_isa.c,v 1.6.6.1 2007/12/11 15:29:20 yamt Exp $");
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41
42 #include <sys/bus.h>
43
44 #include <dev/isa/isareg.h>
45 #include <dev/isa/isavar.h>
46
47 #include <dev/sysmon/sysmonvar.h>
48
49 #include <dev/isa/itesio_isavar.h>
50
51 #define IT_VOLTSTART_IDX 3 /* voltage start index */
52 #define IT_FANSTART_IDX 12 /* fan start index */
53
54 #if defined(ITESIO_DEBUG)
55 #define DPRINTF(x) do { printf x; } while (0)
56 #else
57 #define DPRINTF(x)
58 #endif
59
60 /*
61 * IT87-compatible chips can typically measure voltages up to 4.096 V.
62 * To measure higher voltages the input is attenuated with (external)
63 * resistors. Negative voltages are measured using a reference
64 * voltage. So we have to convert the sensor values back to real
65 * voltages by applying the appropriate resistor factor.
66 */
67 #define RFACT_NONE 10000
68 #define RFACT(x, y) (RFACT_NONE * ((x) + (y)) / (y))
69
70 /* autoconf(9) functions */
71 static int itesio_isa_match(device_t, struct cfdata *, void *);
72 static void itesio_isa_attach(device_t, device_t, void *);
73 static int itesio_isa_detach(device_t, int);
74
75 CFATTACH_DECL_NEW(itesio, sizeof(struct itesio_softc),
76 itesio_isa_match, itesio_isa_attach, itesio_isa_detach, NULL);
77
78 /* driver functions */
79 static uint8_t itesio_ecreadreg(struct itesio_softc *, int);
80 static void itesio_ecwritereg(struct itesio_softc *, int, int);
81 static uint8_t itesio_readreg(bus_space_tag_t, bus_space_handle_t, int);
82 static void itesio_writereg(bus_space_tag_t, bus_space_handle_t, int, int);
83 static void itesio_enter(bus_space_tag_t, bus_space_handle_t);
84 static void itesio_exit(bus_space_tag_t, bus_space_handle_t);
85
86 /* envsys(9) glue */
87 static void itesio_setup_sensors(struct itesio_softc *);
88 static void itesio_refresh_temp(struct itesio_softc *, envsys_data_t *);
89 static void itesio_refresh_volts(struct itesio_softc *, envsys_data_t *);
90 static void itesio_refresh_fans(struct itesio_softc *, envsys_data_t *);
91 static void itesio_refresh(struct sysmon_envsys *, envsys_data_t *);
92
93 /* rfact values for voltage sensors */
94 static const int itesio_vrfact[] = {
95 RFACT_NONE, /* VCORE_A */
96 RFACT_NONE, /* VCORE_B */
97 RFACT_NONE, /* +3.3V */
98 RFACT(68, 100), /* +5V */
99 RFACT(30, 10), /* +12V */
100 RFACT(21, 10), /* -12V */
101 RFACT(83, 20), /* -5V */
102 RFACT(68, 100), /* STANDBY */
103 RFACT_NONE /* VBAT */
104 };
105
106 static int
107 itesio_isa_match(device_t parent, struct cfdata *match, void *aux)
108 {
109 struct isa_attach_args *ia = aux;
110 bus_space_handle_t ioh;
111 uint16_t cr;
112
113 /* Must supply an address */
114 if (ia->ia_nio < 1)
115 return 0;
116
117 if (ISA_DIRECT_CONFIG(ia))
118 return 0;
119
120 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
121 return 0;
122
123 if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh))
124 return 0;
125
126 itesio_enter(ia->ia_iot, ioh);
127 cr = (itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID1) << 8);
128 cr |= itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID2);
129 itesio_exit(ia->ia_iot, ioh);
130 bus_space_unmap(ia->ia_iot, ioh, 2);
131
132 switch (cr) {
133 case ITESIO_ID8705:
134 case ITESIO_ID8712:
135 case ITESIO_ID8716:
136 case ITESIO_ID8718:
137 ia->ia_nio = 1;
138 ia->ia_io[0].ir_size = 2;
139 ia->ia_niomem = 0;
140 ia->ia_nirq = 0;
141 ia->ia_ndrq = 0;
142 return 1;
143 default:
144 return 0;
145 }
146 }
147
148 static void
149 itesio_isa_attach(device_t parent, device_t self, void *aux)
150 {
151 struct itesio_softc *sc = device_private(self);
152 struct isa_attach_args *ia = aux;
153 bus_space_handle_t ioh;
154 int i;
155 uint8_t cr;
156
157 ia->ia_iot = sc->sc_iot;
158
159 if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh)) {
160 aprint_error(": can't map i/o space\n");
161 return;
162 }
163
164 aprint_naive("\n");
165
166 /*
167 * Enter to the Super I/O MB PNP mode.
168 */
169 itesio_enter(ia->ia_iot, ioh);
170 /*
171 * Get info from the Super I/O Global Configuration Registers:
172 * Chip IDs and Device Revision.
173 */
174 sc->sc_chipid = (itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID1) << 8);
175 sc->sc_chipid |= itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID2);
176 sc->sc_devrev = (itesio_readreg(ia->ia_iot, ioh, ITESIO_DEVREV) & 0x0f);
177 /*
178 * Select the EC LDN to get the Base Address.
179 */
180 itesio_writereg(ia->ia_iot, ioh, ITESIO_LDNSEL, ITESIO_EC_LDN);
181 sc->sc_hwmon_baseaddr =
182 (itesio_readreg(ia->ia_iot, ioh, ITESIO_EC_MSB) << 8);
183 sc->sc_hwmon_baseaddr |= itesio_readreg(ia->ia_iot, ioh, ITESIO_EC_LSB);
184 /*
185 * We are done, exit MB PNP mode and unmap I/O space.
186 */
187 itesio_exit(ia->ia_iot, ioh);
188 bus_space_unmap(sc->sc_iot, ioh, 2);
189
190 aprint_normal(": iTE IT%4xF Super I/O (rev %d)\n",
191 sc->sc_chipid, sc->sc_devrev);
192 aprint_normal_dev(self, "Hardware Monitor registers at 0x%x\n",
193 sc->sc_hwmon_baseaddr);
194
195 if (bus_space_map(sc->sc_iot, sc->sc_hwmon_baseaddr, 8, 0,
196 &sc->sc_ioh)) {
197 aprint_error_dev(self, "cannot map hwmon i/o space\n");
198 return;
199 }
200
201 sc->sc_hwmon_mapped = true;
202
203 /* Activate monitoring */
204 cr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
205 SET(cr, 0x01);
206 itesio_ecwritereg(sc, ITESIO_EC_CONFIG, cr);
207
208 #ifdef notyet
209 /* Enable beep alarms */
210 cr = itesio_ecreadreg(sc, ITESIO_EC_BEEPEER);
211 SET(cr, 0x02); /* Voltage exceeds limit */
212 SET(cr, 0x04); /* Temperature exceeds limit */
213 itesio_ecwritereg(sc, ITESIO_EC_BEEPEER, cr);
214 #endif
215
216 /*
217 * Initialize and attach sensors.
218 */
219 itesio_setup_sensors(sc);
220 sc->sc_sme = sysmon_envsys_create();
221 for (i = 0; i < IT_NUM_SENSORS; i++) {
222 if (sysmon_envsys_sensor_attach(sc->sc_sme,
223 &sc->sc_sensor[i])) {
224 sysmon_envsys_destroy(sc->sc_sme);
225 return;
226 }
227 }
228 /*
229 * Hook into the system monitor.
230 */
231 sc->sc_sme->sme_name = device_xname(self);
232 sc->sc_sme->sme_cookie = sc;
233 sc->sc_sme->sme_refresh = itesio_refresh;
234
235 if ((i = sysmon_envsys_register(sc->sc_sme))) {
236 aprint_error_dev(self,
237 "unable to register with sysmon (%d)\n", i);
238 sysmon_envsys_destroy(sc->sc_sme);
239 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 8);
240 return;
241 }
242 sc->sc_hwmon_enabled = true;
243 pmf_device_register(self, NULL, NULL);
244 }
245
246 static int
247 itesio_isa_detach(device_t self, int flags)
248 {
249 struct itesio_softc *sc = device_private(self);
250
251 if (sc->sc_hwmon_enabled)
252 sysmon_envsys_unregister(sc->sc_sme);
253 if (sc->sc_hwmon_mapped)
254 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 8);
255 return 0;
256 }
257
258 /*
259 * Functions to read/write to the Environmental Controller.
260 */
261 static uint8_t
262 itesio_ecreadreg(struct itesio_softc *sc, int reg)
263 {
264 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITESIO_EC_ADDR, reg);
265 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, ITESIO_EC_DATA);
266 }
267
268 static void
269 itesio_ecwritereg(struct itesio_softc *sc, int reg, int val)
270 {
271 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITESIO_EC_ADDR, reg);
272 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ITESIO_EC_DATA, val);
273 }
274
275 /*
276 * Functions to enter/exit/read/write to the Super I/O.
277 */
278 static uint8_t
279 itesio_readreg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg)
280 {
281 bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
282 return bus_space_read_1(iot, ioh, ITESIO_DATA);
283 }
284
285 static void
286 itesio_writereg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg, int val)
287 {
288 bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
289 bus_space_write_1(iot, ioh, ITESIO_DATA, val);
290 }
291
292 static void
293 itesio_enter(bus_space_tag_t iot, bus_space_handle_t ioh)
294 {
295 bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x87);
296 bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x01);
297 bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
298 bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
299 }
300
301 static void
302 itesio_exit(bus_space_tag_t iot, bus_space_handle_t ioh)
303 {
304 bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x02);
305 bus_space_write_1(iot, ioh, ITESIO_DATA, 0x02);
306 }
307
308
309 #define COPYDESCR(x, y) \
310 do { \
311 strlcpy((x), (y), sizeof(x)); \
312 } while (0)
313 /*
314 * sysmon_envsys(9) glue.
315 */
316 static void
317 itesio_setup_sensors(struct itesio_softc *sc)
318 {
319 int i;
320
321 /* temperatures */
322 for (i = 0; i < IT_VOLTSTART_IDX; i++)
323 sc->sc_sensor[i].units = ENVSYS_STEMP;
324
325 COPYDESCR(sc->sc_sensor[0].desc, "CPU Temp");
326 COPYDESCR(sc->sc_sensor[1].desc, "System Temp");
327 COPYDESCR(sc->sc_sensor[2].desc, "Aux Temp");
328
329 /* voltages */
330 for (i = IT_VOLTSTART_IDX; i < IT_FANSTART_IDX; i++) {
331 sc->sc_sensor[i].units = ENVSYS_SVOLTS_DC;
332 sc->sc_sensor[i].flags = ENVSYS_FCHANGERFACT;
333 }
334
335 COPYDESCR(sc->sc_sensor[3].desc, "VCORE_A");
336 COPYDESCR(sc->sc_sensor[4].desc, "VCORE_B");
337 COPYDESCR(sc->sc_sensor[5].desc, "+3.3V");
338 COPYDESCR(sc->sc_sensor[6].desc, "+5V");
339 COPYDESCR(sc->sc_sensor[7].desc, "+12V");
340 COPYDESCR(sc->sc_sensor[8].desc, "-12V");
341 COPYDESCR(sc->sc_sensor[9].desc, "-5V");
342 COPYDESCR(sc->sc_sensor[10].desc, "STANDBY");
343 COPYDESCR(sc->sc_sensor[11].desc, "VBAT");
344
345 /* fans */
346 for (i = IT_FANSTART_IDX; i < IT_NUM_SENSORS; i++)
347 sc->sc_sensor[i].units = ENVSYS_SFANRPM;
348
349 COPYDESCR(sc->sc_sensor[12].desc, "CPU Fan");
350 COPYDESCR(sc->sc_sensor[13].desc, "System Fan");
351 COPYDESCR(sc->sc_sensor[14].desc, "Aux Fan");
352 }
353 #undef COPYDESCR
354
355 static void
356 itesio_refresh_temp(struct itesio_softc *sc, envsys_data_t *edata)
357 {
358 int sdata;
359
360 sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORTEMPBASE + edata->sensor);
361 /* sensor is not connected or reporting invalid data */
362 if (sdata == 0 || sdata >= 0xfa) {
363 edata->state = ENVSYS_SINVALID;
364 return;
365 }
366
367 DPRINTF(("%s: sdata[temp%d] 0x%x\n", __func__, edata->sensor, sdata));
368 /* Convert temperature to uK */
369 edata->value_cur = sdata * 1000000 + 273150000;
370 edata->state = ENVSYS_SVALID;
371 }
372
373 static void
374 itesio_refresh_volts(struct itesio_softc *sc, envsys_data_t *edata)
375 {
376 uint8_t vbatcr = 0;
377 int i, sdata;
378
379 i = edata->sensor - IT_VOLTSTART_IDX;
380
381 sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORVOLTBASE + i);
382 /* not connected */
383 if (sdata == 0 || sdata == 0xff) {
384 edata->state = ENVSYS_SINVALID;
385 return;
386 }
387
388 /*
389 * update VBAT voltage reading every time we read it, to get
390 * latest value.
391 */
392 if (i == 8) {
393 vbatcr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
394 SET(vbatcr, ITESIO_EC_UPDATEVBAT);
395 itesio_ecwritereg(sc, ITESIO_EC_CONFIG, vbatcr);
396 }
397
398 DPRINTF(("%s: sdata[volt%d] 0x%x\n", __func__, i, sdata));
399
400 /* voltage returned as (mV << 4) */
401 edata->value_cur = (sdata << 4);
402 /* rfact is (factor * 10^4) */
403 edata->value_cur *= itesio_vrfact[i];
404
405 if (edata->rfact)
406 edata->value_cur += edata->rfact;
407 else
408 edata->rfact = itesio_vrfact[i];
409
410 /* division by 10 gets us back to uVDC */
411 edata->value_cur /= 10;
412 edata->state = ENVSYS_SVALID;
413 }
414
415 static void
416 itesio_refresh_fans(struct itesio_softc *sc, envsys_data_t *edata)
417 {
418 uint8_t mode = 0;
419 uint16_t sdata = 0;
420 int i, divisor, odivisor, ndivisor;
421
422 i = edata->sensor - IT_FANSTART_IDX;
423 divisor = odivisor = ndivisor = 0;
424
425 if (sc->sc_chipid == ITESIO_ID8705 || sc->sc_chipid == ITESIO_ID8712) {
426 /*
427 * Use the Fan Tachometer Divisor Register for
428 * IT8705F and IT8712F.
429 */
430 divisor = odivisor = ndivisor =
431 itesio_ecreadreg(sc, ITESIO_EC_FAN_TDR);
432 sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
433 if (sdata == 0xff) {
434 edata->state = ENVSYS_SINVALID;
435 if (i == 2)
436 ndivisor |= 0x40;
437 else {
438 ndivisor &= ~(7 << (i * 3));
439 ndivisor |= ((divisor + 1) & 7) << (i * 3);
440 }
441 } else {
442 if (i == 2)
443 divisor = divisor & 1 ? 3 : 1;
444
445 if ((sdata << (divisor & 7)) == 0)
446 edata->state = ENVSYS_SINVALID;
447 else {
448 edata->value_cur =
449 1350000 / (sdata << (divisor & 7));
450 edata->state = ENVSYS_SVALID;
451 }
452 }
453 DPRINTF(("%s: 8bit sdata[fan%d] 0x%x div: 0x%x\n", __func__,
454 i, sdata, divisor));
455 if (ndivisor != odivisor)
456 itesio_ecwritereg(sc, ITESIO_EC_FAN_TDR, ndivisor);
457 } else {
458 mode = itesio_ecreadreg(sc, ITESIO_EC_FAN16_CER);
459 sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
460 if (mode & (1 << i))
461 sdata += (itesio_ecreadreg(sc,
462 ITESIO_EC_SENSORFANEXTBASE + i) << 8);
463 edata->state = ENVSYS_SVALID;
464 if (sdata == 0 ||
465 sdata == ((mode & (1 << i)) ? 0xffff : 0xff))
466 edata->state = ENVSYS_SINVALID;
467 else {
468 edata->value_cur = 1350000 / 2 / sdata;
469 edata->state = ENVSYS_SVALID;
470 }
471 DPRINTF(("%s: 16bit sdata[fan%d] 0x%x\n", __func__, i, sdata));
472 }
473 }
474
475 static void
476 itesio_refresh(struct sysmon_envsys *sme, struct envsys_data *edata)
477 {
478 struct itesio_softc *sc = sme->sme_cookie;
479
480 if (edata->sensor < IT_VOLTSTART_IDX)
481 itesio_refresh_temp(sc, edata);
482 else if (edata->sensor >= IT_VOLTSTART_IDX &&
483 edata->sensor < IT_FANSTART_IDX)
484 itesio_refresh_volts(sc, edata);
485 else
486 itesio_refresh_fans(sc, edata);
487 }
488