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