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