xref: /netbsd-src/sys/dev/ic/nslm7x.c (revision aa73cae19608873cc4d1f712c4a0f8f8435f1ffa)
1 /*	$NetBSD: nslm7x.c,v 1.21 2005/02/27 00:27:02 perry 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.21 2005/02/27 00:27:02 perry 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;
431 	/* do the same thing as in  lm_probe() */
432 	if ((*sc->lm_readreg)(sc, ITEC_RES48) != ITEC_RES48_DEFAULT)
433 		return 0;
434 
435 	if ((*sc->lm_readreg)(sc, ITEC_RES52) != ITEC_RES52_DEFAULT)
436 		return 0;
437 
438 	vendor=(*sc->lm_readreg)(sc, ITEC_VENDID);
439 
440 	if (vendor == ITEC_VENDID_ITE)
441 		printf(": iTE IT8705f\n");
442 	else
443 		printf(": unknown IT8705f compatible, vendorid 0x%02x\n",
444 		    vendor);
445 
446 	/*
447 	 * XXX this is a litle bit lame...
448 	 * All VIN inputs work exactly the same way, it depends of the
449 	 * external wiring what voltages they monitor and which correction
450 	 * factors are needed. We assume a pretty standard setup here
451 	 */
452 	wb_setup_volt(sc);
453 	strlcpy(sc->info[0].desc, "CPU", sizeof(sc->info[0].desc));
454 	strlcpy(sc->info[1].desc, "AGP", sizeof(sc->info[1].desc));
455 	strlcpy(sc->info[6].desc, "+2.5V", sizeof(sc->info[6].desc));
456 	sc->info[5].rfact = 51100;
457 	sc->info[7].rfact = 16778;
458 
459 	setup_temp(sc, 9, 3);
460 	setup_fan(sc, 12, 3);
461 	sc->numsensors = ITEC_NUM_SENSORS;
462 	sc->refresh_sensor_data = itec_refresh_sensor_data;
463 	sc->sc_sysmon.sme_streinfo = itec_streinfo;
464 
465 	return 1;
466 }
467 
468 
469 static void
470 setup_temp(sc, start, n)
471 	struct lm_softc *sc;
472 	int start, n;
473 {
474 	int i;
475 
476 	for (i = 0; i < n; i++) {
477 		sc->sensors[start + i].units = ENVSYS_STEMP;
478 		snprintf(sc->info[start + i].desc,
479 		    sizeof(sc->info[start + i].desc), "Temp %d", i + 1);
480 	}
481 }
482 
483 
484 static void
485 setup_fan(sc, start, n)
486 	struct lm_softc *sc;
487 	int start, n;
488 {
489 	int i;
490 	for (i = 0; i < n; ++i) {
491 		sc->sensors[start + i].units = ENVSYS_SFANRPM;
492 		sc->info[start + i].units = ENVSYS_SFANRPM;
493 		snprintf(sc->info[start + i].desc,
494 		    sizeof(sc->info[start + i].desc), "Fan %d", i + 1);
495 	}
496 }
497 
498 int
499 lm_gtredata(sme, tred)
500 	 struct sysmon_envsys *sme;
501 	 struct envsys_tre_data *tred;
502 {
503 	 static const struct timeval onepointfive = { 1, 500000 };
504 	 struct timeval t;
505 	 struct lm_softc *sc = sme->sme_cookie;
506 	 int i, s;
507 
508 	 /* read new values at most once every 1.5 seconds */
509 	 timeradd(&sc->lastread, &onepointfive, &t);
510 	 s = splclock();
511 	 i = timercmp(&mono_time, &t, >);
512 	 if (i) {
513 		  sc->lastread.tv_sec  = mono_time.tv_sec;
514 		  sc->lastread.tv_usec = mono_time.tv_usec;
515 	 }
516 	 splx(s);
517 
518 	 if (i)
519 		  sc->refresh_sensor_data(sc);
520 
521 	 *tred = sc->sensors[tred->sensor];
522 
523 	 return 0;
524 }
525 
526 int
527 generic_streinfo_fan(sc, info, n, binfo)
528 	struct lm_softc *sc;
529 	struct envsys_basic_info *info;
530 	int n;
531 	struct envsys_basic_info *binfo;
532 {
533 	u_int8_t sdata;
534 	int divisor;
535 
536 	/* FAN1 and FAN2 can have divisors set, but not FAN3 */
537 	if ((sc->info[binfo->sensor].units == ENVSYS_SFANRPM)
538 	    && (n < 2)) {
539 		if (binfo->rpms == 0) {
540 			binfo->validflags = 0;
541 			return 0;
542 		}
543 
544 		/* write back the nominal FAN speed  */
545 		info->rpms = binfo->rpms;
546 
547 		/* 153 is the nominal FAN speed value */
548 		divisor = 1350000 / (binfo->rpms * 153);
549 
550 		/* ...but we need lg(divisor) */
551 		if (divisor <= 1)
552 		    divisor = 0;
553 		else if (divisor <= 2)
554 		    divisor = 1;
555 		else if (divisor <= 4)
556 		    divisor = 2;
557 		else
558 		    divisor = 3;
559 
560 		/*
561 		 * FAN1 div is in bits <5:4>, FAN2 div is
562 		 * in <7:6>
563 		 */
564 		sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
565 		if ( n == 0 ) {  /* FAN1 */
566 		    divisor <<= 4;
567 		    sdata = (sdata & 0xCF) | divisor;
568 		} else { /* FAN2 */
569 		    divisor <<= 6;
570 		    sdata = (sdata & 0x3F) | divisor;
571 		}
572 
573 		(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
574 	}
575 	return 0;
576 
577 }
578 
579 int
580 lm_streinfo(sme, binfo)
581 	 struct sysmon_envsys *sme;
582 	 struct envsys_basic_info *binfo;
583 {
584 	 struct lm_softc *sc = sme->sme_cookie;
585 
586 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
587 		  sc->info[binfo->sensor].rfact = binfo->rfact;
588 	 else {
589 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
590 			generic_streinfo_fan(sc, &sc->info[binfo->sensor],
591 			    binfo->sensor - 8, binfo);
592 		}
593 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
594 		    sizeof(sc->info[binfo->sensor].desc));
595 		binfo->validflags = ENVSYS_FVALID;
596 	 }
597 	 return 0;
598 }
599 
600 int
601 wb781_streinfo(sme, binfo)
602 	 struct sysmon_envsys *sme;
603 	 struct envsys_basic_info *binfo;
604 {
605 	 struct lm_softc *sc = sme->sme_cookie;
606 	 int divisor;
607 	 u_int8_t sdata;
608 	 int i;
609 
610 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
611 		  sc->info[binfo->sensor].rfact = binfo->rfact;
612 	 else {
613 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
614 			if (binfo->rpms == 0) {
615 				binfo->validflags = 0;
616 				return 0;
617 			}
618 
619 			/* write back the nominal FAN speed  */
620 			sc->info[binfo->sensor].rpms = binfo->rpms;
621 
622 			/* 153 is the nominal FAN speed value */
623 			divisor = 1350000 / (binfo->rpms * 153);
624 
625 			/* ...but we need lg(divisor) */
626 			for (i = 0; i < 7; i++) {
627 				if (divisor <= (1 << i))
628 				 	break;
629 			}
630 			divisor = i;
631 
632 			if (binfo->sensor == 10 || binfo->sensor == 11) {
633 				/*
634 				 * FAN1 div is in bits <5:4>, FAN2 div
635 				 * is in <7:6>
636 				 */
637 				sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
638 				if ( binfo->sensor == 10 ) {  /* FAN1 */
639 					 sdata = (sdata & 0xCF) |
640 					     ((divisor & 0x3) << 4);
641 				} else { /* FAN2 */
642 					 sdata = (sdata & 0x3F) |
643 					     ((divisor & 0x3) << 6);
644 				}
645 				(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
646 			} else {
647 				/* FAN3 is in WB_PIN <7:6> */
648 				sdata = (*sc->lm_readreg)(sc, WB_PIN);
649 				sdata = (sdata & 0x3F) |
650 				     ((divisor & 0x3) << 6);
651 				(*sc->lm_writereg)(sc, WB_PIN, sdata);
652 			}
653 		}
654 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
655 		    sizeof(sc->info[binfo->sensor].desc));
656 		binfo->validflags = ENVSYS_FVALID;
657 	 }
658 	 return 0;
659 }
660 
661 int
662 wb782_streinfo(sme, binfo)
663 	 struct sysmon_envsys *sme;
664 	 struct envsys_basic_info *binfo;
665 {
666 	 struct lm_softc *sc = sme->sme_cookie;
667 	 int divisor;
668 	 u_int8_t sdata;
669 	 int i;
670 
671 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
672 		  sc->info[binfo->sensor].rfact = binfo->rfact;
673 	 else {
674 	 	if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
675 			if (binfo->rpms == 0) {
676 				binfo->validflags = 0;
677 				return 0;
678 			}
679 
680 			/* write back the nominal FAN speed  */
681 			sc->info[binfo->sensor].rpms = binfo->rpms;
682 
683 			/* 153 is the nominal FAN speed value */
684 			divisor = 1350000 / (binfo->rpms * 153);
685 
686 			/* ...but we need lg(divisor) */
687 			for (i = 0; i < 7; i++) {
688 				if (divisor <= (1 << i))
689 				 	break;
690 			}
691 			divisor = i;
692 
693 			if (binfo->sensor == 12 || binfo->sensor == 13) {
694 				/*
695 				 * FAN1 div is in bits <5:4>, FAN2 div
696 				 * is in <7:6>
697 				 */
698 				sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
699 				if ( binfo->sensor == 12 ) {  /* FAN1 */
700 					 sdata = (sdata & 0xCF) |
701 					     ((divisor & 0x3) << 4);
702 				} else { /* FAN2 */
703 					 sdata = (sdata & 0x3F) |
704 					     ((divisor & 0x3) << 6);
705 				}
706 				(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
707 			} else {
708 				/* FAN3 is in WB_PIN <7:6> */
709 				sdata = (*sc->lm_readreg)(sc, WB_PIN);
710 				sdata = (sdata & 0x3F) |
711 				     ((divisor & 0x3) << 6);
712 				(*sc->lm_writereg)(sc, WB_PIN, sdata);
713 			}
714 			/* Bit 2 of divisor is in WB_BANK0_FANBAT */
715 			(*sc->lm_banksel)(sc, 0);
716 			sdata = (*sc->lm_readreg)(sc, WB_BANK0_FANBAT);
717 			sdata &= ~(0x20 << (binfo->sensor - 12));
718 			sdata |= (divisor & 0x4) << (binfo->sensor - 9);
719 			(*sc->lm_writereg)(sc, WB_BANK0_FANBAT, sdata);
720 		}
721 
722 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
723 		    sizeof(sc->info[binfo->sensor].desc));
724 		binfo->validflags = ENVSYS_FVALID;
725 	}
726 	return 0;
727 }
728 
729 int
730 itec_streinfo(sme, binfo)
731 	 struct sysmon_envsys *sme;
732 	 struct envsys_basic_info *binfo;
733 {
734 	 struct lm_softc *sc = sme->sme_cookie;
735 	 int divisor;
736 	 u_int8_t sdata;
737 	 int i;
738 
739 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
740 		  sc->info[binfo->sensor].rfact = binfo->rfact;
741 	 else {
742 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
743 			if (binfo->rpms == 0) {
744 				binfo->validflags = 0;
745 				return 0;
746 			}
747 
748 			/* write back the nominal FAN speed  */
749 			sc->info[binfo->sensor].rpms = binfo->rpms;
750 
751 			/* 153 is the nominal FAN speed value */
752 			divisor = 1350000 / (binfo->rpms * 153);
753 
754 			/* ...but we need lg(divisor) */
755 			for (i = 0; i < 7; i++) {
756 				if (divisor <= (1 << i))
757 				 	break;
758 			}
759 			divisor = i;
760 
761 			sdata = (*sc->lm_readreg)(sc, ITEC_FANDIV);
762 			/*
763 			 * FAN1 div is in bits <0:2>, FAN2 is in <3:5>
764 			 * FAN3 is in <6>, if set divisor is 8, else 2
765 			 */
766 			if ( binfo->sensor == 10 ) {  /* FAN1 */
767 				 sdata = (sdata & 0xf8) | divisor;
768 			} else if ( binfo->sensor == 11 ) { /* FAN2 */
769 				 sdata = (sdata & 0xc7) | divisor << 3;
770 			} else { /* FAN3 */
771 				if (divisor>2)
772 					sdata = sdata & 0xbf;
773 				else
774 					sdata = sdata | 0x40;
775 			}
776 			(*sc->lm_writereg)(sc, ITEC_FANDIV, sdata);
777 		}
778 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
779 		    sizeof(sc->info[binfo->sensor].desc));
780 		binfo->validflags = ENVSYS_FVALID;
781 	 }
782 	 return 0;
783 }
784 
785 static void
786 generic_stemp(sc, sensor)
787 	struct lm_softc *sc;
788 	struct envsys_tre_data *sensor;
789 {
790 	int sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 7);
791 	DPRINTF(("sdata[temp] 0x%x\n", sdata));
792 	/* temp is given in deg. C, we convert to uK */
793 	sensor->cur.data_us = sdata * 1000000 + 273150000;
794 }
795 
796 static void
797 generic_svolt(sc, sensors, infos)
798 	struct lm_softc *sc;
799 	struct envsys_tre_data *sensors;
800 	struct envsys_basic_info *infos;
801 {
802 	int i, sdata;
803 
804 	for (i = 0; i < 7; i++) {
805 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i);
806 		DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
807 		/* voltage returned as (mV >> 4), we convert to uVDC */
808 		sensors[i].cur.data_s = (sdata << 4);
809 		/* rfact is (factor * 10^4) */
810 		sensors[i].cur.data_s *= infos[i].rfact;
811 		/* division by 10 gets us back to uVDC */
812 		sensors[i].cur.data_s /= 10;
813 
814 		/* these two are negative voltages */
815 		if ( (i == 5) || (i == 6) )
816 			sensors[i].cur.data_s *= -1;
817 	}
818 }
819 
820 static void
821 generic_fanrpm(sc, sensors)
822 	struct lm_softc *sc;
823 	struct envsys_tre_data *sensors;
824 {
825 	int i, sdata, divisor;
826 	for (i = 0; i < 3; i++) {
827 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 8 + i);
828 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
829 		if (i == 2)
830 			divisor = 2;	/* Fixed divisor for FAN3 */
831 		else if (i == 1)	/* Bits 7 & 6 of VID/FAN  */
832 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
833 		else
834 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
835 
836 		if (sdata == 0xff || sdata == 0x00) {
837 			sensors[i].cur.data_us = 0;
838 		} else {
839 			sensors[i].cur.data_us = 1350000 / (sdata << divisor);
840 		}
841 	}
842 }
843 
844 /*
845  * pre:  last read occurred >= 1.5 seconds ago
846  * post: sensors[] current data are the latest from the chip
847  */
848 void
849 lm_refresh_sensor_data(sc)
850 	struct lm_softc *sc;
851 {
852 	/* Refresh our stored data for every sensor */
853 	generic_stemp(sc, &sc->sensors[7]);
854 	generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
855 	generic_fanrpm(sc, &sc->sensors[8]);
856 }
857 
858 static void
859 wb_svolt(sc)
860 	struct lm_softc *sc;
861 {
862 	int i, sdata;
863 	for (i = 0; i < 9; ++i) {
864 		if (i < 7) {
865 			sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i);
866 		} else {
867 			/* from bank5 */
868 			(*sc->lm_banksel)(sc, 5);
869 			sdata = (*sc->lm_readreg)(sc, (i == 7) ?
870 			    WB_BANK5_5VSB : WB_BANK5_VBAT);
871 		}
872 		DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
873 		/* voltage returned as (mV >> 4), we convert to uV */
874 		sdata =  sdata << 4;
875 		/* special case for negative voltages */
876 		if (i == 5) {
877 			/*
878 			 * -12Vdc, assume Winbond recommended values for
879 			 * resistors
880 			 */
881 			sdata = ((sdata * 1000) - (3600 * 805)) / 195;
882 		} else if (i == 6) {
883 			/*
884 			 * -5Vdc, assume Winbond recommended values for
885 			 * resistors
886 			 */
887 			sdata = ((sdata * 1000) - (3600 * 682)) / 318;
888 		}
889 		/* rfact is (factor * 10^4) */
890 		sc->sensors[i].cur.data_s = sdata * sc->info[i].rfact;
891 		/* division by 10 gets us back to uVDC */
892 		sc->sensors[i].cur.data_s /= 10;
893 	}
894 }
895 
896 static void
897 wb_stemp(sc, sensors, n)
898 	struct lm_softc *sc;
899 	struct  envsys_tre_data *sensors;
900 	int n;
901 {
902 	int sdata;
903 	/* temperatures. Given in dC, we convert to uK */
904 	sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 7);
905 	DPRINTF(("sdata[temp0] 0x%x\n", sdata));
906 	sensors[0].cur.data_us = sdata * 1000000 + 273150000;
907 	/* from bank1 */
908 	if ((*sc->lm_banksel)(sc, 1))
909 		sensors[1].validflags &= ~ENVSYS_FCURVALID;
910 	else {
911 		sdata = (*sc->lm_readreg)(sc, WB_BANK1_T2H) << 1;
912 		sdata |=  ((*sc->lm_readreg)(sc, WB_BANK1_T2L) & 0x80) >> 7;
913 		DPRINTF(("sdata[temp1] 0x%x\n", sdata));
914 		sensors[1].cur.data_us = (sdata * 1000000) / 2 + 273150000;
915 	}
916 	if (n < 3)
917 		return;
918 	/* from bank2 */
919 	if ((*sc->lm_banksel)(sc, 2))
920 		sensors[2].validflags &= ~ENVSYS_FCURVALID;
921 	else {
922 		sdata = (*sc->lm_readreg)(sc, WB_BANK2_T3H) << 1;
923 		sdata |=  ((*sc->lm_readreg)(sc, WB_BANK2_T3L) & 0x80) >> 7;
924 		DPRINTF(("sdata[temp2] 0x%x\n", sdata));
925 		sensors[2].cur.data_us = (sdata * 1000000) / 2 + 273150000;
926 	}
927 }
928 
929 static void
930 wb781_fanrpm(sc, sensors)
931 	struct lm_softc *sc;
932 	struct envsys_tre_data *sensors;
933 {
934 	int i, divisor, sdata;
935 	(*sc->lm_banksel)(sc, 0);
936 	for (i = 0; i < 3; i++) {
937 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i + 8);
938 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
939 		if (i == 0)
940 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
941 		else if (i == 1)
942 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
943 		else
944 			divisor = ((*sc->lm_readreg)(sc, WB_PIN) >> 6) & 0x3;
945 
946 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
947 		if (sdata == 0xff || sdata == 0x00) {
948 			sensors[i].cur.data_us = 0;
949 		} else {
950 			sensors[i].cur.data_us = 1350000 /
951 			    (sdata << divisor);
952 		}
953 	}
954 }
955 
956 static void
957 wb_fanrpm(sc, sensors)
958 	struct lm_softc *sc;
959 	struct envsys_tre_data *sensors;
960 {
961 	int i, divisor, sdata;
962 	(*sc->lm_banksel)(sc, 0);
963 	for (i = 0; i < 3; i++) {
964 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i + 8);
965 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
966 		if (i == 0)
967 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
968 		else if (i == 1)
969 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
970 		else
971 			divisor = ((*sc->lm_readreg)(sc, WB_PIN) >> 6) & 0x3;
972 		divisor |= ((*sc->lm_readreg)(sc, WB_BANK0_FANBAT) >> (i + 3)) & 0x4;
973 
974 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
975 		if (sdata == 0xff || sdata == 0x00) {
976 			sensors[i].cur.data_us = 0;
977 		} else {
978 			sensors[i].cur.data_us = 1350000 /
979 			    (sdata << divisor);
980 		}
981 	}
982 }
983 
984 void
985 wb781_refresh_sensor_data(sc)
986 	struct lm_softc *sc;
987 {
988 	/* Refresh our stored data for every sensor */
989 	/* we need to reselect bank0 to access common registers */
990 	(*sc->lm_banksel)(sc, 0);
991 	generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
992 	(*sc->lm_banksel)(sc, 0);
993 	wb_stemp(sc, &sc->sensors[7], 3);
994 	(*sc->lm_banksel)(sc, 0);
995 	wb781_fanrpm(sc, &sc->sensors[10]);
996 }
997 
998 void
999 wb782_refresh_sensor_data(sc)
1000 	struct lm_softc *sc;
1001 {
1002 	/* Refresh our stored data for every sensor */
1003 	wb_svolt(sc);
1004 	wb_stemp(sc, &sc->sensors[9], 3);
1005 	wb_fanrpm(sc, &sc->sensors[12]);
1006 }
1007 
1008 void
1009 wb697_refresh_sensor_data(sc)
1010 	struct lm_softc *sc;
1011 {
1012 	/* Refresh our stored data for every sensor */
1013 	wb_svolt(sc);
1014 	wb_stemp(sc, &sc->sensors[9], 2);
1015 	wb_fanrpm(sc, &sc->sensors[11]);
1016 }
1017 
1018 static void
1019 itec_svolt(sc, sensors, infos)
1020 	struct lm_softc *sc;
1021 	struct envsys_tre_data *sensors;
1022 	struct envsys_basic_info *infos;
1023 {
1024 	int i, sdata;
1025 
1026 	for (i = 0; i < 9; i++) {
1027 		sdata = (*sc->lm_readreg)(sc, ITEC_VIN0 + i);
1028 		DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
1029 		/* voltage returned as (mV >> 4), we convert to uVDC */
1030 		sensors[i].cur.data_s = ( sdata << 4 );
1031 		/* rfact is (factor * 10^4) */
1032 
1033 		sensors[i].cur.data_s *= infos[i].rfact;
1034 		/*
1035 		 * XXX We assume input 5 is wired the way iTE suggests to
1036 		 * monitor a negative voltage. I'd prefer using negative rfacts
1037 		 * for detecting those cases but since rfact is an u_int this
1038 		 * isn't possible.
1039 		 */
1040 		if (i == 5)
1041 			sensors[i].cur.data_s -=
1042 			    (infos[i].rfact - 10000) * ITEC_VREF;
1043 		/* division by 10 gets us back to uVDC */
1044 		sensors[i].cur.data_s /= 10;
1045 	}
1046 }
1047 
1048 static void
1049 itec_stemp(sc, sensors)
1050 	struct lm_softc *sc;
1051 	struct  envsys_tre_data *sensors;
1052 {
1053 	int i, sdata;
1054 
1055 	/* temperatures. Given in dC, we convert to uK */
1056 	for (i = 0; i < 3; i++) {
1057 		sdata = (*sc->lm_readreg)(sc, ITEC_TEMP1 + i);
1058 		DPRINTF(("sdata[temp%d] 0x%x\n",i, sdata));
1059 		sensors[i].cur.data_us = sdata * 1000000 + 273150000;
1060 	}
1061 }
1062 
1063 static void
1064 itec_fanrpm(sc, sensors)
1065 	struct lm_softc *sc;
1066 	struct envsys_tre_data *sensors;
1067 {
1068 	int i, fandiv, divisor, sdata;
1069 	(*sc->lm_banksel)(sc, 0);
1070 	fandiv = ((*sc->lm_readreg)(sc, ITEC_FANDIV));
1071 
1072 	for (i = 0; i < 3; i++) {
1073 		sdata = (*sc->lm_readreg)(sc, ITEC_FAN1 + i);
1074 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
1075 		switch (i) {
1076 		case 0:
1077 			divisor = fandiv & 0x7;
1078 			break;
1079 		case 1:
1080 			divisor = (fandiv >> 3) & 0x7;
1081 			break;
1082 		case 2:
1083 		default:	/* XXX */
1084 			divisor = (fandiv & 0x40) ? 3 : 1;
1085 			break;
1086 		}
1087 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
1088 		if (sdata == 0xff || sdata == 0x00) {
1089 			sensors[i].cur.data_us = 0;
1090 		} else {
1091 			sensors[i].cur.data_us = 1350000 /
1092 			    (sdata << divisor);
1093 		}
1094 	}
1095 
1096 }
1097 
1098 void
1099 itec_refresh_sensor_data(sc)
1100 	struct lm_softc *sc;
1101 {
1102 	itec_svolt(sc, &sc->sensors[0], &sc->info[0]);
1103 	itec_stemp(sc, &sc->sensors[9]);
1104 	itec_fanrpm(sc, &sc->sensors[12]);
1105 }
1106