xref: /netbsd-src/sys/dev/ic/nslm7x.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: nslm7x.c,v 1.22 2005/04/29 02:02:52 xtraeme Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bill Squier.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: nslm7x.c,v 1.22 2005/04/29 02:02:52 xtraeme Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/device.h>
47 #include <sys/malloc.h>
48 #include <sys/errno.h>
49 #include <sys/queue.h>
50 #include <sys/lock.h>
51 #include <sys/ioctl.h>
52 #include <sys/conf.h>
53 #include <sys/time.h>
54 
55 #include <machine/bus.h>
56 
57 #include <dev/isa/isareg.h>
58 #include <dev/isa/isavar.h>
59 
60 #include <dev/sysmon/sysmonvar.h>
61 
62 #include <dev/ic/nslm7xvar.h>
63 
64 #include <machine/intr.h>
65 #include <machine/bus.h>
66 
67 #if defined(LMDEBUG)
68 #define DPRINTF(x)		printf x
69 #else
70 #define DPRINTF(x)
71 #endif
72 
73 const struct envsys_range lm_ranges[] = {	/* sc->sensors sub-intervals */
74 					/* for each unit type */
75 	{ 7, 7,    ENVSYS_STEMP   },
76 	{ 8, 10,   ENVSYS_SFANRPM },
77 	{ 1, 0,    ENVSYS_SVOLTS_AC },	/* None */
78 	{ 0, 6,    ENVSYS_SVOLTS_DC },
79 	{ 1, 0,    ENVSYS_SOHMS },	/* None */
80 	{ 1, 0,    ENVSYS_SWATTS },	/* None */
81 	{ 1, 0,    ENVSYS_SAMPS }	/* None */
82 };
83 
84 
85 static void setup_fan(struct lm_softc *, int, int);
86 static void setup_temp(struct lm_softc *, int, int);
87 static void wb_setup_volt(struct lm_softc *);
88 
89 int lm_match(struct lm_softc *);
90 int wb_match(struct lm_softc *);
91 int itec_match(struct lm_softc *);
92 int def_match(struct lm_softc *);
93 void lm_common_match(struct lm_softc *);
94 static int lm_generic_banksel(struct lm_softc *, int);
95 
96 static void generic_stemp(struct lm_softc *, struct envsys_tre_data *);
97 static void generic_svolt(struct lm_softc *, struct envsys_tre_data *,
98     struct envsys_basic_info *);
99 static void generic_fanrpm(struct lm_softc *, struct envsys_tre_data *);
100 
101 void lm_refresh_sensor_data(struct lm_softc *);
102 
103 static void wb_svolt(struct lm_softc *);
104 static void wb_stemp(struct lm_softc *, struct envsys_tre_data *, int);
105 static void wb781_fanrpm(struct lm_softc *, struct envsys_tre_data *);
106 static void wb_fanrpm(struct lm_softc *, struct envsys_tre_data *);
107 
108 void wb781_refresh_sensor_data(struct lm_softc *);
109 void wb782_refresh_sensor_data(struct lm_softc *);
110 void wb697_refresh_sensor_data(struct lm_softc *);
111 
112 static void itec_svolt(struct lm_softc *, struct envsys_tre_data *,
113     struct envsys_basic_info *);
114 static void itec_stemp(struct lm_softc *, struct envsys_tre_data *);
115 static void itec_fanrpm(struct lm_softc *, struct envsys_tre_data *);
116 void itec_refresh_sensor_data(struct lm_softc *);
117 
118 int lm_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
119 
120 int generic_streinfo_fan(struct lm_softc *, struct envsys_basic_info *,
121            int, struct envsys_basic_info *);
122 int lm_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
123 int wb781_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
124 int wb782_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
125 int itec_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
126 
127 struct lm_chip {
128 	int (*chip_match)(struct lm_softc *);
129 };
130 
131 struct lm_chip lm_chips[] = {
132 	{ itec_match },
133 	{ wb_match },
134 	{ lm_match },
135 	{ def_match } /* Must be last */
136 };
137 
138 
139 int
140 lm_generic_banksel(lmsc, bank)
141 	struct lm_softc *lmsc;
142 	int bank;
143 {
144 
145 	(*lmsc->lm_writereg)(lmsc, WB_BANKSEL, bank);
146 	return 0;
147 }
148 
149 
150 /*
151  * bus independent probe
152  */
153 int
154 lm_probe(iot, ioh)
155 	bus_space_tag_t iot;
156 	bus_space_handle_t ioh;
157 {
158 	u_int8_t cr;
159 	int rv;
160 
161 	/*
162 	 * Check for it8705f, before we do the chip reset.
163 	 * In case of an it8705f this might reset all the fan control
164 	 * parameters to defaults which would void all settings done by
165 	 * the BOOTROM/BIOS.
166 	 */
167 	bus_space_write_1(iot, ioh, LMC_ADDR, ITEC_RES48);
168 	cr = bus_space_read_1(iot, ioh, LMC_DATA);
169 
170 	if (cr == ITEC_RES48_DEFAULT) {
171 		bus_space_write_1(iot, ioh, LMC_ADDR, ITEC_RES52);
172 		cr = bus_space_read_1(iot, ioh, LMC_DATA);
173 		if (cr == ITEC_RES52_DEFAULT)
174 			return 1;
175 	}
176 
177 	/* Check for some power-on defaults */
178 	bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
179 
180 	/* Perform LM78 reset */
181 	bus_space_write_1(iot, ioh, LMC_DATA, 0x80);
182 
183 	/* XXX - Why do I have to reselect the register? */
184 	bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
185 	cr = bus_space_read_1(iot, ioh, LMC_DATA);
186 
187 	/* XXX - spec says *only* 0x08! */
188 	if ((cr == 0x08) || (cr == 0x01))
189 		rv = 1;
190 	else
191 		rv = 0;
192 
193 	DPRINTF(("lm: rv = %d, cr = %x\n", rv, cr));
194 
195 	return (rv);
196 }
197 
198 
199 /*
200  * pre:  lmsc contains valid busspace tag and handle
201  */
202 void
203 lm_attach(lmsc)
204 	struct lm_softc *lmsc;
205 {
206 	u_int i;
207 
208 	/* Install default bank selection routine, if none given. */
209 	if (lmsc->lm_banksel == NULL)
210 		lmsc->lm_banksel = lm_generic_banksel;
211 
212 	for (i = 0; i < sizeof(lm_chips) / sizeof(lm_chips[0]); i++)
213 		if (lm_chips[i].chip_match(lmsc))
214 			break;
215 
216 	/* Start the monitoring loop */
217 	(*lmsc->lm_writereg)(lmsc, LMD_CONFIG, 0x01);
218 
219 	/* Indicate we have never read the registers */
220 	timerclear(&lmsc->lastread);
221 
222 	/* Initialize sensors */
223 	for (i = 0; i < lmsc->numsensors; ++i) {
224 		lmsc->sensors[i].sensor = lmsc->info[i].sensor = i;
225 		lmsc->sensors[i].validflags = (ENVSYS_FVALID|ENVSYS_FCURVALID);
226 		lmsc->info[i].validflags = ENVSYS_FVALID;
227 		lmsc->sensors[i].warnflags = ENVSYS_WARN_OK;
228 	}
229 	/*
230 	 * Hook into the System Monitor.
231 	 */
232 	lmsc->sc_sysmon.sme_ranges = lm_ranges;
233 	lmsc->sc_sysmon.sme_sensor_info = lmsc->info;
234 	lmsc->sc_sysmon.sme_sensor_data = lmsc->sensors;
235 	lmsc->sc_sysmon.sme_cookie = lmsc;
236 
237 	lmsc->sc_sysmon.sme_gtredata = lm_gtredata;
238 	/* sme_streinfo set in chip-specific attach */
239 
240 	lmsc->sc_sysmon.sme_nsensors = lmsc->numsensors;
241 	lmsc->sc_sysmon.sme_envsys_version = 1000;
242 
243 	if (sysmon_envsys_register(&lmsc->sc_sysmon))
244 		printf("%s: unable to register with sysmon\n",
245 		    lmsc->sc_dev.dv_xname);
246 }
247 
248 int
249 lm_match(sc)
250 	struct lm_softc *sc;
251 {
252 	int i;
253 
254 	/* See if we have an LM78 or LM79 */
255 	i = (*sc->lm_readreg)(sc, LMD_CHIPID) & LM_ID_MASK;
256 	switch(i) {
257 	case LM_ID_LM78:
258 		printf(": LM78\n");
259 		break;
260 	case LM_ID_LM78J:
261 		printf(": LM78J\n");
262 		break;
263 	case LM_ID_LM79:
264 		printf(": LM79\n");
265 		break;
266 	case LM_ID_LM81:
267 		printf(": LM81\n");
268 		break;
269 	default:
270 		return 0;
271 	}
272 	lm_common_match(sc);
273 	return 1;
274 }
275 
276 int
277 def_match(sc)
278 	struct lm_softc *sc;
279 {
280 	int i;
281 
282 	i = (*sc->lm_readreg)(sc, LMD_CHIPID) & LM_ID_MASK;
283 	printf(": Unknown chip (ID %d)\n", i);
284 	lm_common_match(sc);
285 	return 1;
286 }
287 
288 void
289 lm_common_match(sc)
290 	struct lm_softc *sc;
291 {
292 	int i;
293 	sc->numsensors = LM_NUM_SENSORS;
294 	sc->refresh_sensor_data = lm_refresh_sensor_data;
295 
296 	for (i = 0; i < 7; ++i) {
297 		sc->sensors[i].units = sc->info[i].units =
298 		    ENVSYS_SVOLTS_DC;
299 		snprintf(sc->info[i].desc, sizeof(sc->info[i].desc),
300 		    "IN %d", i);
301 	}
302 
303 	/* default correction factors for resistors on higher voltage inputs */
304 	sc->info[0].rfact = sc->info[1].rfact =
305 	    sc->info[2].rfact = 10000;
306 	sc->info[3].rfact = (int)(( 90.9 / 60.4) * 10000);
307 	sc->info[4].rfact = (int)(( 38.0 / 10.0) * 10000);
308 	sc->info[5].rfact = (int)((210.0 / 60.4) * 10000);
309 	sc->info[6].rfact = (int)(( 90.9 / 60.4) * 10000);
310 
311 	sc->sensors[7].units = ENVSYS_STEMP;
312 	strcpy(sc->info[7].desc, "Temp");
313 
314 	setup_fan(sc, 8, 3);
315 	sc->sc_sysmon.sme_streinfo = lm_streinfo;
316 }
317 
318 int
319 wb_match(sc)
320 	struct lm_softc *sc;
321 {
322 	int i, j;
323 
324 	(*sc->lm_writereg)(sc, WB_BANKSEL, WB_BANKSEL_HBAC);
325 	j = (*sc->lm_readreg)(sc, WB_VENDID) << 8;
326 	(*sc->lm_writereg)(sc, WB_BANKSEL, 0);
327 	j |= (*sc->lm_readreg)(sc, WB_VENDID);
328 	DPRINTF(("winbond vend id 0x%x\n", j));
329 	if (j != WB_VENDID_WINBOND)
330 		return 0;
331 	/* read device ID */
332 	(*sc->lm_banksel)(sc, 0);
333 	j = (*sc->lm_readreg)(sc, WB_BANK0_CHIPID);
334 	DPRINTF(("winbond chip id 0x%x\n", j));
335 	switch(j) {
336 	case WB_CHIPID_83781:
337 	case WB_CHIPID_83781_2:
338 		printf(": W83781D\n");
339 
340 		for (i = 0; i < 7; ++i) {
341 			sc->sensors[i].units = sc->info[i].units =
342 			    ENVSYS_SVOLTS_DC;
343 			snprintf(sc->info[i].desc, sizeof(sc->info[i].desc),
344 			    "IN %d", i);
345 		}
346 
347 		/* default correction factors for higher voltage inputs */
348 		sc->info[0].rfact = sc->info[1].rfact =
349 		    sc->info[2].rfact = 10000;
350 		sc->info[3].rfact = (int)(( 90.9 / 60.4) * 10000);
351 		sc->info[4].rfact = (int)(( 38.0 / 10.0) * 10000);
352 		sc->info[5].rfact = (int)((210.0 / 60.4) * 10000);
353 		sc->info[6].rfact = (int)(( 90.9 / 60.4) * 10000);
354 
355 		setup_temp(sc, 7, 3);
356 		setup_fan(sc, 10, 3);
357 
358 		sc->numsensors = WB83781_NUM_SENSORS;
359 		sc->refresh_sensor_data = wb781_refresh_sensor_data;
360 		sc->sc_sysmon.sme_streinfo = wb781_streinfo;
361 		return 1;
362 	case WB_CHIPID_83697:
363 		printf(": W83697HF\n");
364 		wb_setup_volt(sc);
365 		setup_temp(sc, 9, 2);
366 		setup_fan(sc, 11, 3);
367 		sc->numsensors = WB83697_NUM_SENSORS;
368 		sc->refresh_sensor_data = wb697_refresh_sensor_data;
369 		sc->sc_sysmon.sme_streinfo = wb782_streinfo;
370 		return 1;
371 	case WB_CHIPID_83782:
372 		printf(": W83782D\n");
373 		break;
374 	case WB_CHIPID_83627:
375 		printf(": W83627HF\n");
376 		break;
377 	default:
378 		printf(": unknow winbond chip ID 0x%x\n", j);
379 		/* handle as a standart lm7x */
380 		lm_common_match(sc);
381 		return 1;
382 	}
383 	/* common code for the W83782D and W83627HF */
384 	wb_setup_volt(sc);
385 	setup_temp(sc, 9, 3);
386 	setup_fan(sc, 12, 3);
387 	sc->numsensors = WB_NUM_SENSORS;
388 	sc->refresh_sensor_data = wb782_refresh_sensor_data;
389 	sc->sc_sysmon.sme_streinfo = wb782_streinfo;
390 	return 1;
391 }
392 
393 static void
394 wb_setup_volt(sc)
395 	struct lm_softc *sc;
396 {
397 	sc->sensors[0].units = sc->info[0].units = ENVSYS_SVOLTS_DC;
398 	snprintf(sc->info[0].desc, sizeof(sc->info[0].desc), "VCORE A");
399 	sc->info[0].rfact = 10000;
400 	sc->sensors[1].units = sc->info[1].units = ENVSYS_SVOLTS_DC;
401 	snprintf(sc->info[1].desc, sizeof(sc->info[1].desc), "VCORE B");
402 	sc->info[1].rfact = 10000;
403 	sc->sensors[2].units = sc->info[2].units = ENVSYS_SVOLTS_DC;
404 	snprintf(sc->info[2].desc, sizeof(sc->info[2].desc), "+3.3V");
405 	sc->info[2].rfact = 10000;
406 	sc->sensors[3].units = sc->info[3].units = ENVSYS_SVOLTS_DC;
407 	snprintf(sc->info[3].desc, sizeof(sc->info[3].desc), "+5V");
408 	sc->info[3].rfact = 16778;
409 	sc->sensors[4].units = sc->info[4].units = ENVSYS_SVOLTS_DC;
410 	snprintf(sc->info[4].desc, sizeof(sc->info[4].desc), "+12V");
411 	sc->info[4].rfact = 38000;
412 	sc->sensors[5].units = sc->info[5].units = ENVSYS_SVOLTS_DC;
413 	snprintf(sc->info[5].desc, sizeof(sc->info[5].desc), "-12V");
414 	sc->info[5].rfact = 10000;
415 	sc->sensors[6].units = sc->info[6].units = ENVSYS_SVOLTS_DC;
416 	snprintf(sc->info[6].desc, sizeof(sc->info[6].desc), "-5V");
417 	sc->info[6].rfact = 10000;
418 	sc->sensors[7].units = sc->info[7].units = ENVSYS_SVOLTS_DC;
419 	snprintf(sc->info[7].desc, sizeof(sc->info[7].desc), "+5VSB");
420 	sc->info[7].rfact = 15151;
421 	sc->sensors[8].units = sc->info[8].units = ENVSYS_SVOLTS_DC;
422 	snprintf(sc->info[8].desc, sizeof(sc->info[8].desc), "VBAT");
423 	sc->info[8].rfact = 10000;
424 }
425 
426 int
427 itec_match(sc)
428 	struct lm_softc *sc;
429 {
430 	int vendor, coreid;
431 
432 	/* do the same thing as in  lm_probe() */
433 	if ((*sc->lm_readreg)(sc, ITEC_RES48) != ITEC_RES48_DEFAULT)
434 		return 0;
435 
436 	if ((*sc->lm_readreg)(sc, ITEC_RES52) != ITEC_RES52_DEFAULT)
437 		return 0;
438 
439 	/* We check for the core ID register (0x5B), which is available
440 	 * only in the 8712F, if that fails, we check the vendor ID
441 	 * register, available on 8705F and 8712F */
442 
443 	coreid = (*sc->lm_readreg)(sc, ITEC_COREID);
444 
445 	if (coreid == ITEC_COREID_ITE)
446 		printf(": ITE8712F\n");
447 	else {
448 		vendor = (*sc->lm_readreg)(sc, ITEC_VENDID);
449 		if (vendor == ITEC_VENDID_ITE)
450 			printf(": ITE8705F\n");
451 		else
452 			printf(": unknown ITE87%02x compatible\n", vendor);
453 	}
454 
455 	/*
456 	 * XXX this is a litle bit lame...
457 	 * All VIN inputs work exactly the same way, it depends of the
458 	 * external wiring what voltages they monitor and which correction
459 	 * factors are needed. We assume a pretty standard setup here
460 	 */
461 	wb_setup_volt(sc);
462 	strlcpy(sc->info[0].desc, "CPU", sizeof(sc->info[0].desc));
463 	strlcpy(sc->info[1].desc, "AGP", sizeof(sc->info[1].desc));
464 	strlcpy(sc->info[6].desc, "+2.5V", sizeof(sc->info[6].desc));
465 	sc->info[5].rfact = 51100;
466 	sc->info[7].rfact = 16778;
467 
468 	setup_temp(sc, 9, 3);
469 	setup_fan(sc, 12, 3);
470 	sc->numsensors = ITEC_NUM_SENSORS;
471 	sc->refresh_sensor_data = itec_refresh_sensor_data;
472 	sc->sc_sysmon.sme_streinfo = itec_streinfo;
473 
474 	return 1;
475 }
476 
477 
478 static void
479 setup_temp(sc, start, n)
480 	struct lm_softc *sc;
481 	int start, n;
482 {
483 	int i;
484 
485 	for (i = 0; i < n; i++) {
486 		sc->sensors[start + i].units = ENVSYS_STEMP;
487 		snprintf(sc->info[start + i].desc,
488 		    sizeof(sc->info[start + i].desc), "Temp %d", i + 1);
489 	}
490 }
491 
492 
493 static void
494 setup_fan(sc, start, n)
495 	struct lm_softc *sc;
496 	int start, n;
497 {
498 	int i;
499 	for (i = 0; i < n; ++i) {
500 		sc->sensors[start + i].units = ENVSYS_SFANRPM;
501 		sc->info[start + i].units = ENVSYS_SFANRPM;
502 		snprintf(sc->info[start + i].desc,
503 		    sizeof(sc->info[start + i].desc), "Fan %d", i + 1);
504 	}
505 }
506 
507 int
508 lm_gtredata(sme, tred)
509 	 struct sysmon_envsys *sme;
510 	 struct envsys_tre_data *tred;
511 {
512 	 static const struct timeval onepointfive = { 1, 500000 };
513 	 struct timeval t;
514 	 struct lm_softc *sc = sme->sme_cookie;
515 	 int i, s;
516 
517 	 /* read new values at most once every 1.5 seconds */
518 	 timeradd(&sc->lastread, &onepointfive, &t);
519 	 s = splclock();
520 	 i = timercmp(&mono_time, &t, >);
521 	 if (i) {
522 		  sc->lastread.tv_sec  = mono_time.tv_sec;
523 		  sc->lastread.tv_usec = mono_time.tv_usec;
524 	 }
525 	 splx(s);
526 
527 	 if (i)
528 		  sc->refresh_sensor_data(sc);
529 
530 	 *tred = sc->sensors[tred->sensor];
531 
532 	 return 0;
533 }
534 
535 int
536 generic_streinfo_fan(sc, info, n, binfo)
537 	struct lm_softc *sc;
538 	struct envsys_basic_info *info;
539 	int n;
540 	struct envsys_basic_info *binfo;
541 {
542 	u_int8_t sdata;
543 	int divisor;
544 
545 	/* FAN1 and FAN2 can have divisors set, but not FAN3 */
546 	if ((sc->info[binfo->sensor].units == ENVSYS_SFANRPM)
547 	    && (n < 2)) {
548 		if (binfo->rpms == 0) {
549 			binfo->validflags = 0;
550 			return 0;
551 		}
552 
553 		/* write back the nominal FAN speed  */
554 		info->rpms = binfo->rpms;
555 
556 		/* 153 is the nominal FAN speed value */
557 		divisor = 1350000 / (binfo->rpms * 153);
558 
559 		/* ...but we need lg(divisor) */
560 		if (divisor <= 1)
561 		    divisor = 0;
562 		else if (divisor <= 2)
563 		    divisor = 1;
564 		else if (divisor <= 4)
565 		    divisor = 2;
566 		else
567 		    divisor = 3;
568 
569 		/*
570 		 * FAN1 div is in bits <5:4>, FAN2 div is
571 		 * in <7:6>
572 		 */
573 		sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
574 		if ( n == 0 ) {  /* FAN1 */
575 		    divisor <<= 4;
576 		    sdata = (sdata & 0xCF) | divisor;
577 		} else { /* FAN2 */
578 		    divisor <<= 6;
579 		    sdata = (sdata & 0x3F) | divisor;
580 		}
581 
582 		(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
583 	}
584 	return 0;
585 
586 }
587 
588 int
589 lm_streinfo(sme, binfo)
590 	 struct sysmon_envsys *sme;
591 	 struct envsys_basic_info *binfo;
592 {
593 	 struct lm_softc *sc = sme->sme_cookie;
594 
595 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
596 		  sc->info[binfo->sensor].rfact = binfo->rfact;
597 	 else {
598 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
599 			generic_streinfo_fan(sc, &sc->info[binfo->sensor],
600 			    binfo->sensor - 8, binfo);
601 		}
602 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
603 		    sizeof(sc->info[binfo->sensor].desc));
604 		binfo->validflags = ENVSYS_FVALID;
605 	 }
606 	 return 0;
607 }
608 
609 int
610 wb781_streinfo(sme, binfo)
611 	 struct sysmon_envsys *sme;
612 	 struct envsys_basic_info *binfo;
613 {
614 	 struct lm_softc *sc = sme->sme_cookie;
615 	 int divisor;
616 	 u_int8_t sdata;
617 	 int i;
618 
619 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
620 		  sc->info[binfo->sensor].rfact = binfo->rfact;
621 	 else {
622 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
623 			if (binfo->rpms == 0) {
624 				binfo->validflags = 0;
625 				return 0;
626 			}
627 
628 			/* write back the nominal FAN speed  */
629 			sc->info[binfo->sensor].rpms = binfo->rpms;
630 
631 			/* 153 is the nominal FAN speed value */
632 			divisor = 1350000 / (binfo->rpms * 153);
633 
634 			/* ...but we need lg(divisor) */
635 			for (i = 0; i < 7; i++) {
636 				if (divisor <= (1 << i))
637 				 	break;
638 			}
639 			divisor = i;
640 
641 			if (binfo->sensor == 10 || binfo->sensor == 11) {
642 				/*
643 				 * FAN1 div is in bits <5:4>, FAN2 div
644 				 * is in <7:6>
645 				 */
646 				sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
647 				if ( binfo->sensor == 10 ) {  /* FAN1 */
648 					 sdata = (sdata & 0xCF) |
649 					     ((divisor & 0x3) << 4);
650 				} else { /* FAN2 */
651 					 sdata = (sdata & 0x3F) |
652 					     ((divisor & 0x3) << 6);
653 				}
654 				(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
655 			} else {
656 				/* FAN3 is in WB_PIN <7:6> */
657 				sdata = (*sc->lm_readreg)(sc, WB_PIN);
658 				sdata = (sdata & 0x3F) |
659 				     ((divisor & 0x3) << 6);
660 				(*sc->lm_writereg)(sc, WB_PIN, sdata);
661 			}
662 		}
663 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
664 		    sizeof(sc->info[binfo->sensor].desc));
665 		binfo->validflags = ENVSYS_FVALID;
666 	 }
667 	 return 0;
668 }
669 
670 int
671 wb782_streinfo(sme, binfo)
672 	 struct sysmon_envsys *sme;
673 	 struct envsys_basic_info *binfo;
674 {
675 	 struct lm_softc *sc = sme->sme_cookie;
676 	 int divisor;
677 	 u_int8_t sdata;
678 	 int i;
679 
680 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
681 		  sc->info[binfo->sensor].rfact = binfo->rfact;
682 	 else {
683 	 	if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
684 			if (binfo->rpms == 0) {
685 				binfo->validflags = 0;
686 				return 0;
687 			}
688 
689 			/* write back the nominal FAN speed  */
690 			sc->info[binfo->sensor].rpms = binfo->rpms;
691 
692 			/* 153 is the nominal FAN speed value */
693 			divisor = 1350000 / (binfo->rpms * 153);
694 
695 			/* ...but we need lg(divisor) */
696 			for (i = 0; i < 7; i++) {
697 				if (divisor <= (1 << i))
698 				 	break;
699 			}
700 			divisor = i;
701 
702 			if (binfo->sensor == 12 || binfo->sensor == 13) {
703 				/*
704 				 * FAN1 div is in bits <5:4>, FAN2 div
705 				 * is in <7:6>
706 				 */
707 				sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
708 				if ( binfo->sensor == 12 ) {  /* FAN1 */
709 					 sdata = (sdata & 0xCF) |
710 					     ((divisor & 0x3) << 4);
711 				} else { /* FAN2 */
712 					 sdata = (sdata & 0x3F) |
713 					     ((divisor & 0x3) << 6);
714 				}
715 				(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
716 			} else {
717 				/* FAN3 is in WB_PIN <7:6> */
718 				sdata = (*sc->lm_readreg)(sc, WB_PIN);
719 				sdata = (sdata & 0x3F) |
720 				     ((divisor & 0x3) << 6);
721 				(*sc->lm_writereg)(sc, WB_PIN, sdata);
722 			}
723 			/* Bit 2 of divisor is in WB_BANK0_FANBAT */
724 			(*sc->lm_banksel)(sc, 0);
725 			sdata = (*sc->lm_readreg)(sc, WB_BANK0_FANBAT);
726 			sdata &= ~(0x20 << (binfo->sensor - 12));
727 			sdata |= (divisor & 0x4) << (binfo->sensor - 9);
728 			(*sc->lm_writereg)(sc, WB_BANK0_FANBAT, sdata);
729 		}
730 
731 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
732 		    sizeof(sc->info[binfo->sensor].desc));
733 		binfo->validflags = ENVSYS_FVALID;
734 	}
735 	return 0;
736 }
737 
738 int
739 itec_streinfo(sme, binfo)
740 	 struct sysmon_envsys *sme;
741 	 struct envsys_basic_info *binfo;
742 {
743 	 struct lm_softc *sc = sme->sme_cookie;
744 	 int divisor;
745 	 u_int8_t sdata;
746 	 int i;
747 
748 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
749 		  sc->info[binfo->sensor].rfact = binfo->rfact;
750 	 else {
751 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
752 			if (binfo->rpms == 0) {
753 				binfo->validflags = 0;
754 				return 0;
755 			}
756 
757 			/* write back the nominal FAN speed  */
758 			sc->info[binfo->sensor].rpms = binfo->rpms;
759 
760 			/* 153 is the nominal FAN speed value */
761 			divisor = 1350000 / (binfo->rpms * 153);
762 
763 			/* ...but we need lg(divisor) */
764 			for (i = 0; i < 7; i++) {
765 				if (divisor <= (1 << i))
766 				 	break;
767 			}
768 			divisor = i;
769 
770 			sdata = (*sc->lm_readreg)(sc, ITEC_FANDIV);
771 			/*
772 			 * FAN1 div is in bits <0:2>, FAN2 is in <3:5>
773 			 * FAN3 is in <6>, if set divisor is 8, else 2
774 			 */
775 			if ( binfo->sensor == 10 ) {  /* FAN1 */
776 				 sdata = (sdata & 0xf8) | divisor;
777 			} else if ( binfo->sensor == 11 ) { /* FAN2 */
778 				 sdata = (sdata & 0xc7) | divisor << 3;
779 			} else { /* FAN3 */
780 				if (divisor>2)
781 					sdata = sdata & 0xbf;
782 				else
783 					sdata = sdata | 0x40;
784 			}
785 			(*sc->lm_writereg)(sc, ITEC_FANDIV, sdata);
786 		}
787 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
788 		    sizeof(sc->info[binfo->sensor].desc));
789 		binfo->validflags = ENVSYS_FVALID;
790 	 }
791 	 return 0;
792 }
793 
794 static void
795 generic_stemp(sc, sensor)
796 	struct lm_softc *sc;
797 	struct envsys_tre_data *sensor;
798 {
799 	int sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 7);
800 	DPRINTF(("sdata[temp] 0x%x\n", sdata));
801 	/* temp is given in deg. C, we convert to uK */
802 	sensor->cur.data_us = sdata * 1000000 + 273150000;
803 }
804 
805 static void
806 generic_svolt(sc, sensors, infos)
807 	struct lm_softc *sc;
808 	struct envsys_tre_data *sensors;
809 	struct envsys_basic_info *infos;
810 {
811 	int i, sdata;
812 
813 	for (i = 0; i < 7; i++) {
814 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i);
815 		DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
816 		/* voltage returned as (mV >> 4), we convert to uVDC */
817 		sensors[i].cur.data_s = (sdata << 4);
818 		/* rfact is (factor * 10^4) */
819 		sensors[i].cur.data_s *= infos[i].rfact;
820 		/* division by 10 gets us back to uVDC */
821 		sensors[i].cur.data_s /= 10;
822 
823 		/* these two are negative voltages */
824 		if ( (i == 5) || (i == 6) )
825 			sensors[i].cur.data_s *= -1;
826 	}
827 }
828 
829 static void
830 generic_fanrpm(sc, sensors)
831 	struct lm_softc *sc;
832 	struct envsys_tre_data *sensors;
833 {
834 	int i, sdata, divisor;
835 	for (i = 0; i < 3; i++) {
836 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 8 + i);
837 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
838 		if (i == 2)
839 			divisor = 2;	/* Fixed divisor for FAN3 */
840 		else if (i == 1)	/* Bits 7 & 6 of VID/FAN  */
841 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
842 		else
843 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
844 
845 		if (sdata == 0xff || sdata == 0x00) {
846 			sensors[i].cur.data_us = 0;
847 		} else {
848 			sensors[i].cur.data_us = 1350000 / (sdata << divisor);
849 		}
850 	}
851 }
852 
853 /*
854  * pre:  last read occurred >= 1.5 seconds ago
855  * post: sensors[] current data are the latest from the chip
856  */
857 void
858 lm_refresh_sensor_data(sc)
859 	struct lm_softc *sc;
860 {
861 	/* Refresh our stored data for every sensor */
862 	generic_stemp(sc, &sc->sensors[7]);
863 	generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
864 	generic_fanrpm(sc, &sc->sensors[8]);
865 }
866 
867 static void
868 wb_svolt(sc)
869 	struct lm_softc *sc;
870 {
871 	int i, sdata;
872 	for (i = 0; i < 9; ++i) {
873 		if (i < 7) {
874 			sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i);
875 		} else {
876 			/* from bank5 */
877 			(*sc->lm_banksel)(sc, 5);
878 			sdata = (*sc->lm_readreg)(sc, (i == 7) ?
879 			    WB_BANK5_5VSB : WB_BANK5_VBAT);
880 		}
881 		DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
882 		/* voltage returned as (mV >> 4), we convert to uV */
883 		sdata =  sdata << 4;
884 		/* special case for negative voltages */
885 		if (i == 5) {
886 			/*
887 			 * -12Vdc, assume Winbond recommended values for
888 			 * resistors
889 			 */
890 			sdata = ((sdata * 1000) - (3600 * 805)) / 195;
891 		} else if (i == 6) {
892 			/*
893 			 * -5Vdc, assume Winbond recommended values for
894 			 * resistors
895 			 */
896 			sdata = ((sdata * 1000) - (3600 * 682)) / 318;
897 		}
898 		/* rfact is (factor * 10^4) */
899 		sc->sensors[i].cur.data_s = sdata * sc->info[i].rfact;
900 		/* division by 10 gets us back to uVDC */
901 		sc->sensors[i].cur.data_s /= 10;
902 	}
903 }
904 
905 static void
906 wb_stemp(sc, sensors, n)
907 	struct lm_softc *sc;
908 	struct  envsys_tre_data *sensors;
909 	int n;
910 {
911 	int sdata;
912 	/* temperatures. Given in dC, we convert to uK */
913 	sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 7);
914 	DPRINTF(("sdata[temp0] 0x%x\n", sdata));
915 	sensors[0].cur.data_us = sdata * 1000000 + 273150000;
916 	/* from bank1 */
917 	if ((*sc->lm_banksel)(sc, 1))
918 		sensors[1].validflags &= ~ENVSYS_FCURVALID;
919 	else {
920 		sdata = (*sc->lm_readreg)(sc, WB_BANK1_T2H) << 1;
921 		sdata |=  ((*sc->lm_readreg)(sc, WB_BANK1_T2L) & 0x80) >> 7;
922 		DPRINTF(("sdata[temp1] 0x%x\n", sdata));
923 		sensors[1].cur.data_us = (sdata * 1000000) / 2 + 273150000;
924 	}
925 	if (n < 3)
926 		return;
927 	/* from bank2 */
928 	if ((*sc->lm_banksel)(sc, 2))
929 		sensors[2].validflags &= ~ENVSYS_FCURVALID;
930 	else {
931 		sdata = (*sc->lm_readreg)(sc, WB_BANK2_T3H) << 1;
932 		sdata |=  ((*sc->lm_readreg)(sc, WB_BANK2_T3L) & 0x80) >> 7;
933 		DPRINTF(("sdata[temp2] 0x%x\n", sdata));
934 		sensors[2].cur.data_us = (sdata * 1000000) / 2 + 273150000;
935 	}
936 }
937 
938 static void
939 wb781_fanrpm(sc, sensors)
940 	struct lm_softc *sc;
941 	struct envsys_tre_data *sensors;
942 {
943 	int i, divisor, sdata;
944 	(*sc->lm_banksel)(sc, 0);
945 	for (i = 0; i < 3; i++) {
946 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i + 8);
947 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
948 		if (i == 0)
949 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
950 		else if (i == 1)
951 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
952 		else
953 			divisor = ((*sc->lm_readreg)(sc, WB_PIN) >> 6) & 0x3;
954 
955 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
956 		if (sdata == 0xff || sdata == 0x00) {
957 			sensors[i].cur.data_us = 0;
958 		} else {
959 			sensors[i].cur.data_us = 1350000 /
960 			    (sdata << divisor);
961 		}
962 	}
963 }
964 
965 static void
966 wb_fanrpm(sc, sensors)
967 	struct lm_softc *sc;
968 	struct envsys_tre_data *sensors;
969 {
970 	int i, divisor, sdata;
971 	(*sc->lm_banksel)(sc, 0);
972 	for (i = 0; i < 3; i++) {
973 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i + 8);
974 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
975 		if (i == 0)
976 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
977 		else if (i == 1)
978 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
979 		else
980 			divisor = ((*sc->lm_readreg)(sc, WB_PIN) >> 6) & 0x3;
981 		divisor |= ((*sc->lm_readreg)(sc, WB_BANK0_FANBAT) >> (i + 3)) & 0x4;
982 
983 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
984 		if (sdata == 0xff || sdata == 0x00) {
985 			sensors[i].cur.data_us = 0;
986 		} else {
987 			sensors[i].cur.data_us = 1350000 /
988 			    (sdata << divisor);
989 		}
990 	}
991 }
992 
993 void
994 wb781_refresh_sensor_data(sc)
995 	struct lm_softc *sc;
996 {
997 	/* Refresh our stored data for every sensor */
998 	/* we need to reselect bank0 to access common registers */
999 	(*sc->lm_banksel)(sc, 0);
1000 	generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
1001 	(*sc->lm_banksel)(sc, 0);
1002 	wb_stemp(sc, &sc->sensors[7], 3);
1003 	(*sc->lm_banksel)(sc, 0);
1004 	wb781_fanrpm(sc, &sc->sensors[10]);
1005 }
1006 
1007 void
1008 wb782_refresh_sensor_data(sc)
1009 	struct lm_softc *sc;
1010 {
1011 	/* Refresh our stored data for every sensor */
1012 	wb_svolt(sc);
1013 	wb_stemp(sc, &sc->sensors[9], 3);
1014 	wb_fanrpm(sc, &sc->sensors[12]);
1015 }
1016 
1017 void
1018 wb697_refresh_sensor_data(sc)
1019 	struct lm_softc *sc;
1020 {
1021 	/* Refresh our stored data for every sensor */
1022 	wb_svolt(sc);
1023 	wb_stemp(sc, &sc->sensors[9], 2);
1024 	wb_fanrpm(sc, &sc->sensors[11]);
1025 }
1026 
1027 static void
1028 itec_svolt(sc, sensors, infos)
1029 	struct lm_softc *sc;
1030 	struct envsys_tre_data *sensors;
1031 	struct envsys_basic_info *infos;
1032 {
1033 	int i, sdata;
1034 
1035 	for (i = 0; i < 9; i++) {
1036 		sdata = (*sc->lm_readreg)(sc, ITEC_VIN0 + i);
1037 		DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
1038 		/* voltage returned as (mV >> 4), we convert to uVDC */
1039 		sensors[i].cur.data_s = ( sdata << 4 );
1040 		/* rfact is (factor * 10^4) */
1041 
1042 		sensors[i].cur.data_s *= infos[i].rfact;
1043 		/*
1044 		 * XXX We assume input 5 is wired the way iTE suggests to
1045 		 * monitor a negative voltage. I'd prefer using negative rfacts
1046 		 * for detecting those cases but since rfact is an u_int this
1047 		 * isn't possible.
1048 		 */
1049 		if (i == 5)
1050 			sensors[i].cur.data_s -=
1051 			    (infos[i].rfact - 10000) * ITEC_VREF;
1052 		/* division by 10 gets us back to uVDC */
1053 		sensors[i].cur.data_s /= 10;
1054 	}
1055 }
1056 
1057 static void
1058 itec_stemp(sc, sensors)
1059 	struct lm_softc *sc;
1060 	struct  envsys_tre_data *sensors;
1061 {
1062 	int i, sdata;
1063 
1064 	/* temperatures. Given in dC, we convert to uK */
1065 	for (i = 0; i < 3; i++) {
1066 		sdata = (*sc->lm_readreg)(sc, ITEC_TEMP1 + i);
1067 		DPRINTF(("sdata[temp%d] 0x%x\n",i, sdata));
1068 		sensors[i].cur.data_us = sdata * 1000000 + 273150000;
1069 	}
1070 }
1071 
1072 static void
1073 itec_fanrpm(sc, sensors)
1074 	struct lm_softc *sc;
1075 	struct envsys_tre_data *sensors;
1076 {
1077 	int i, fandiv, divisor, sdata;
1078 	(*sc->lm_banksel)(sc, 0);
1079 	fandiv = ((*sc->lm_readreg)(sc, ITEC_FANDIV));
1080 
1081 	for (i = 0; i < 3; i++) {
1082 		sdata = (*sc->lm_readreg)(sc, ITEC_FAN1 + i);
1083 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
1084 		switch (i) {
1085 		case 0:
1086 			divisor = fandiv & 0x7;
1087 			break;
1088 		case 1:
1089 			divisor = (fandiv >> 3) & 0x7;
1090 			break;
1091 		case 2:
1092 		default:	/* XXX */
1093 			divisor = (fandiv & 0x40) ? 3 : 1;
1094 			break;
1095 		}
1096 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
1097 		if (sdata == 0xff || sdata == 0x00) {
1098 			sensors[i].cur.data_us = 0;
1099 		} else {
1100 			sensors[i].cur.data_us = 1350000 /
1101 			    (sdata << divisor);
1102 		}
1103 	}
1104 
1105 }
1106 
1107 void
1108 itec_refresh_sensor_data(sc)
1109 	struct lm_softc *sc;
1110 {
1111 	itec_svolt(sc, &sc->sensors[0], &sc->info[0]);
1112 	itec_stemp(sc, &sc->sensors[9]);
1113 	itec_fanrpm(sc, &sc->sensors[12]);
1114 }
1115