nslm7x.c revision 1.18 1 /* $NetBSD: nslm7x.c,v 1.18 2004/04/22 00:17:11 itojun 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.18 2004/04/22 00:17:11 itojun 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) do { printf x; } while (0)
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 __P((struct lm_softc *, int, int));
86 static void setup_temp __P((struct lm_softc *, int, int));
87 static void wb_setup_volt __P((struct lm_softc *));
88
89 int lm_match __P((struct lm_softc *));
90 int wb_match __P((struct lm_softc *));
91 int def_match __P((struct lm_softc *));
92 void lm_common_match __P((struct lm_softc *));
93 static int lm_generic_banksel __P((struct lm_softc *, int));
94
95 static void generic_stemp __P((struct lm_softc *, struct envsys_tre_data *));
96 static void generic_svolt __P((struct lm_softc *, struct envsys_tre_data *,
97 struct envsys_basic_info *));
98 static void generic_fanrpm __P((struct lm_softc *, struct envsys_tre_data *));
99
100 void lm_refresh_sensor_data __P((struct lm_softc *));
101
102 static void wb_svolt __P((struct lm_softc *));
103 static void wb_stemp __P((struct lm_softc *, struct envsys_tre_data *, int));
104 static void wb781_fanrpm __P((struct lm_softc *, struct envsys_tre_data *));
105 static void wb_fanrpm __P((struct lm_softc *, struct envsys_tre_data *));
106
107 void wb781_refresh_sensor_data __P((struct lm_softc *));
108 void wb782_refresh_sensor_data __P((struct lm_softc *));
109 void wb697_refresh_sensor_data __P((struct lm_softc *));
110
111 int lm_gtredata __P((struct sysmon_envsys *, struct envsys_tre_data *));
112
113 int generic_streinfo_fan __P((struct lm_softc *, struct envsys_basic_info *,
114 int, struct envsys_basic_info *));
115 int lm_streinfo __P((struct sysmon_envsys *, struct envsys_basic_info *));
116 int wb781_streinfo __P((struct sysmon_envsys *, struct envsys_basic_info *));
117 int wb782_streinfo __P((struct sysmon_envsys *, struct envsys_basic_info *));
118
119 struct lm_chip {
120 int (*chip_match) __P((struct lm_softc *));
121 };
122
123 struct lm_chip lm_chips[] = {
124 { wb_match },
125 { lm_match },
126 { def_match } /* Must be last */
127 };
128
129
130 int
131 lm_generic_banksel(lmsc, bank)
132 struct lm_softc *lmsc;
133 int bank;
134 {
135
136 (*lmsc->lm_writereg)(lmsc, WB_BANKSEL, bank);
137 return (0);
138 }
139
140
141 /*
142 * bus independent probe
143 */
144 int
145 lm_probe(iot, ioh)
146 bus_space_tag_t iot;
147 bus_space_handle_t ioh;
148 {
149 u_int8_t cr;
150 int rv;
151
152 /* Check for some power-on defaults */
153 bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
154
155 /* Perform LM78 reset */
156 bus_space_write_1(iot, ioh, LMC_DATA, 0x80);
157
158 /* XXX - Why do I have to reselect the register? */
159 bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
160 cr = bus_space_read_1(iot, ioh, LMC_DATA);
161
162 /* XXX - spec says *only* 0x08! */
163 if ((cr == 0x08) || (cr == 0x01))
164 rv = 1;
165 else
166 rv = 0;
167
168 DPRINTF(("lm: rv = %d, cr = %x\n", rv, cr));
169
170 return (rv);
171 }
172
173
174 /*
175 * pre: lmsc contains valid busspace tag and handle
176 */
177 void
178 lm_attach(lmsc)
179 struct lm_softc *lmsc;
180 {
181 u_int i;
182
183 /* Install default bank selection routine, if none given. */
184 if (lmsc->lm_banksel == NULL)
185 lmsc->lm_banksel = lm_generic_banksel;
186
187 for (i = 0; i < sizeof(lm_chips) / sizeof(lm_chips[0]); i++)
188 if (lm_chips[i].chip_match(lmsc))
189 break;
190
191 /* Start the monitoring loop */
192 (*lmsc->lm_writereg)(lmsc, LMD_CONFIG, 0x01);
193
194 /* Indicate we have never read the registers */
195 timerclear(&lmsc->lastread);
196
197 /* Initialize sensors */
198 for (i = 0; i < lmsc->numsensors; ++i) {
199 lmsc->sensors[i].sensor = lmsc->info[i].sensor = i;
200 lmsc->sensors[i].validflags = (ENVSYS_FVALID|ENVSYS_FCURVALID);
201 lmsc->info[i].validflags = ENVSYS_FVALID;
202 lmsc->sensors[i].warnflags = ENVSYS_WARN_OK;
203 }
204 /*
205 * Hook into the System Monitor.
206 */
207 lmsc->sc_sysmon.sme_ranges = lm_ranges;
208 lmsc->sc_sysmon.sme_sensor_info = lmsc->info;
209 lmsc->sc_sysmon.sme_sensor_data = lmsc->sensors;
210 lmsc->sc_sysmon.sme_cookie = lmsc;
211
212 lmsc->sc_sysmon.sme_gtredata = lm_gtredata;
213 /* sme_streinfo set in chip-specific attach */
214
215 lmsc->sc_sysmon.sme_nsensors = lmsc->numsensors;
216 lmsc->sc_sysmon.sme_envsys_version = 1000;
217
218 if (sysmon_envsys_register(&lmsc->sc_sysmon))
219 printf("%s: unable to register with sysmon\n",
220 lmsc->sc_dev.dv_xname);
221 }
222
223 int
224 lm_match(sc)
225 struct lm_softc *sc;
226 {
227 int i;
228
229 /* See if we have an LM78 or LM79 */
230 i = (*sc->lm_readreg)(sc, LMD_CHIPID) & LM_ID_MASK;
231 switch(i) {
232 case LM_ID_LM78:
233 printf(": LM78\n");
234 break;
235 case LM_ID_LM78J:
236 printf(": LM78J\n");
237 break;
238 case LM_ID_LM79:
239 printf(": LM79\n");
240 break;
241 case LM_ID_LM81:
242 printf(": LM81\n");
243 break;
244 default:
245 return 0;
246 }
247 lm_common_match(sc);
248 return 1;
249 }
250
251 int
252 def_match(sc)
253 struct lm_softc *sc;
254 {
255 int i;
256
257 i = (*sc->lm_readreg)(sc, LMD_CHIPID) & LM_ID_MASK;
258 printf(": Unknown chip (ID %d)\n", i);
259 lm_common_match(sc);
260 return 1;
261 }
262
263 void
264 lm_common_match(sc)
265 struct lm_softc *sc;
266 {
267 int i;
268 sc->numsensors = LM_NUM_SENSORS;
269 sc->refresh_sensor_data = lm_refresh_sensor_data;
270
271 for (i = 0; i < 7; ++i) {
272 sc->sensors[i].units = sc->info[i].units =
273 ENVSYS_SVOLTS_DC;
274 snprintf(sc->info[i].desc, sizeof(sc->info[i].desc),
275 "IN %d", i);
276 }
277
278 /* default correction factors for resistors on higher voltage inputs */
279 sc->info[0].rfact = sc->info[1].rfact =
280 sc->info[2].rfact = 10000;
281 sc->info[3].rfact = (int)(( 90.9 / 60.4) * 10000);
282 sc->info[4].rfact = (int)(( 38.0 / 10.0) * 10000);
283 sc->info[5].rfact = (int)((210.0 / 60.4) * 10000);
284 sc->info[6].rfact = (int)(( 90.9 / 60.4) * 10000);
285
286 sc->sensors[7].units = ENVSYS_STEMP;
287 strcpy(sc->info[7].desc, "Temp");
288
289 setup_fan(sc, 8, 3);
290 sc->sc_sysmon.sme_streinfo = lm_streinfo;
291 }
292
293 int
294 wb_match(sc)
295 struct lm_softc *sc;
296 {
297 int i, j;
298
299 (*sc->lm_writereg)(sc, WB_BANKSEL, WB_BANKSEL_HBAC);
300 j = (*sc->lm_readreg)(sc, WB_VENDID) << 8;
301 (*sc->lm_writereg)(sc, WB_BANKSEL, 0);
302 j |= (*sc->lm_readreg)(sc, WB_VENDID);
303 DPRINTF(("winbond vend id 0x%x\n", j));
304 if (j != WB_VENDID_WINBOND)
305 return 0;
306 /* read device ID */
307 (*sc->lm_banksel)(sc, 0);
308 j = (*sc->lm_readreg)(sc, WB_BANK0_CHIPID);
309 DPRINTF(("winbond chip id 0x%x\n", j));
310 switch(j) {
311 case WB_CHIPID_83781:
312 case WB_CHIPID_83781_2:
313 printf(": W83781D\n");
314
315 for (i = 0; i < 7; ++i) {
316 sc->sensors[i].units = sc->info[i].units =
317 ENVSYS_SVOLTS_DC;
318 snprintf(sc->info[i].desc, sizeof(sc->info[i].desc),
319 "IN %d", i);
320 }
321
322 /* default correction factors for higher voltage inputs */
323 sc->info[0].rfact = sc->info[1].rfact =
324 sc->info[2].rfact = 10000;
325 sc->info[3].rfact = (int)(( 90.9 / 60.4) * 10000);
326 sc->info[4].rfact = (int)(( 38.0 / 10.0) * 10000);
327 sc->info[5].rfact = (int)((210.0 / 60.4) * 10000);
328 sc->info[6].rfact = (int)(( 90.9 / 60.4) * 10000);
329
330 setup_temp(sc, 7, 3);
331 setup_fan(sc, 10, 3);
332
333 sc->numsensors = WB83781_NUM_SENSORS;
334 sc->refresh_sensor_data = wb781_refresh_sensor_data;
335 sc->sc_sysmon.sme_streinfo = wb781_streinfo;
336 return 1;
337 case WB_CHIPID_83697:
338 printf(": W83697HF\n");
339 wb_setup_volt(sc);
340 setup_temp(sc, 9, 2);
341 setup_fan(sc, 11, 3);
342 sc->numsensors = WB83697_NUM_SENSORS;
343 sc->refresh_sensor_data = wb697_refresh_sensor_data;
344 sc->sc_sysmon.sme_streinfo = wb782_streinfo;
345 return 1;
346 case WB_CHIPID_83782:
347 printf(": W83782D\n");
348 break;
349 case WB_CHIPID_83627:
350 printf(": W83627HF\n");
351 break;
352 default:
353 printf(": unknow winbond chip ID 0x%x\n", j);
354 /* handle as a standart lm7x */
355 lm_common_match(sc);
356 return 1;
357 }
358 /* common code for the W83782D and W83627HF */
359 wb_setup_volt(sc);
360 setup_temp(sc, 9, 3);
361 setup_fan(sc, 12, 3);
362 sc->numsensors = WB_NUM_SENSORS;
363 sc->refresh_sensor_data = wb782_refresh_sensor_data;
364 sc->sc_sysmon.sme_streinfo = wb782_streinfo;
365 return 1;
366 }
367
368 static void
369 wb_setup_volt(sc)
370 struct lm_softc *sc;
371 {
372 sc->sensors[0].units = sc->info[0].units = ENVSYS_SVOLTS_DC;
373 snprintf(sc->info[0].desc, sizeof(sc->info[0].desc), "VCORE A");
374 sc->info[0].rfact = 10000;
375 sc->sensors[1].units = sc->info[1].units = ENVSYS_SVOLTS_DC;
376 snprintf(sc->info[1].desc, sizeof(sc->info[1].desc), "VCORE B");
377 sc->info[1].rfact = 10000;
378 sc->sensors[2].units = sc->info[2].units = ENVSYS_SVOLTS_DC;
379 snprintf(sc->info[2].desc, sizeof(sc->info[2].desc), "+3.3V");
380 sc->info[2].rfact = 10000;
381 sc->sensors[3].units = sc->info[3].units = ENVSYS_SVOLTS_DC;
382 snprintf(sc->info[3].desc, sizeof(sc->info[3].desc), "+5V");
383 sc->info[3].rfact = 16778;
384 sc->sensors[4].units = sc->info[4].units = ENVSYS_SVOLTS_DC;
385 snprintf(sc->info[4].desc, sizeof(sc->info[4].desc), "+12V");
386 sc->info[4].rfact = 38000;
387 sc->sensors[5].units = sc->info[5].units = ENVSYS_SVOLTS_DC;
388 snprintf(sc->info[5].desc, sizeof(sc->info[5].desc), "-12V");
389 sc->info[5].rfact = 10000;
390 sc->sensors[6].units = sc->info[6].units = ENVSYS_SVOLTS_DC;
391 snprintf(sc->info[6].desc, sizeof(sc->info[6].desc), "-5V");
392 sc->info[6].rfact = 10000;
393 sc->sensors[7].units = sc->info[7].units = ENVSYS_SVOLTS_DC;
394 snprintf(sc->info[7].desc, sizeof(sc->info[7].desc), "+5VSB");
395 sc->info[7].rfact = 15151;
396 sc->sensors[8].units = sc->info[8].units = ENVSYS_SVOLTS_DC;
397 snprintf(sc->info[8].desc, sizeof(sc->info[8].desc), "VBAT");
398 sc->info[8].rfact = 10000;
399 }
400
401 static void
402 setup_temp(sc, start, n)
403 struct lm_softc *sc;
404 int start, n;
405 {
406 int i;
407
408 for (i = 0; i < n; i++) {
409 sc->sensors[start + i].units = ENVSYS_STEMP;
410 snprintf(sc->info[start + i].desc,
411 sizeof(sc->info[start + i].desc), "Temp %d", i + 1);
412 }
413 }
414
415
416 static void
417 setup_fan(sc, start, n)
418 struct lm_softc *sc;
419 int start, n;
420 {
421 int i;
422 for (i = 0; i < n; ++i) {
423 sc->sensors[start + i].units = ENVSYS_SFANRPM;
424 sc->info[start + i].units = ENVSYS_SFANRPM;
425 snprintf(sc->info[start + i].desc,
426 sizeof(sc->info[start + i].desc), "Fan %d", i + 1);
427 }
428 }
429
430 int
431 lm_gtredata(sme, tred)
432 struct sysmon_envsys *sme;
433 struct envsys_tre_data *tred;
434 {
435 static const struct timeval onepointfive = { 1, 500000 };
436 struct timeval t;
437 struct lm_softc *sc = sme->sme_cookie;
438 int i, s;
439
440 /* read new values at most once every 1.5 seconds */
441 timeradd(&sc->lastread, &onepointfive, &t);
442 s = splclock();
443 i = timercmp(&mono_time, &t, >);
444 if (i) {
445 sc->lastread.tv_sec = mono_time.tv_sec;
446 sc->lastread.tv_usec = mono_time.tv_usec;
447 }
448 splx(s);
449
450 if (i)
451 sc->refresh_sensor_data(sc);
452
453 *tred = sc->sensors[tred->sensor];
454
455 return (0);
456 }
457
458 int
459 generic_streinfo_fan(sc, info, n, binfo)
460 struct lm_softc *sc;
461 struct envsys_basic_info *info;
462 int n;
463 struct envsys_basic_info *binfo;
464 {
465 u_int8_t sdata;
466 int divisor;
467
468 /* FAN1 and FAN2 can have divisors set, but not FAN3 */
469 if ((sc->info[binfo->sensor].units == ENVSYS_SFANRPM)
470 && (n < 2)) {
471 if (binfo->rpms == 0) {
472 binfo->validflags = 0;
473 return (0);
474 }
475
476 /* write back the nominal FAN speed */
477 info->rpms = binfo->rpms;
478
479 /* 153 is the nominal FAN speed value */
480 divisor = 1350000 / (binfo->rpms * 153);
481
482 /* ...but we need lg(divisor) */
483 if (divisor <= 1)
484 divisor = 0;
485 else if (divisor <= 2)
486 divisor = 1;
487 else if (divisor <= 4)
488 divisor = 2;
489 else
490 divisor = 3;
491
492 /*
493 * FAN1 div is in bits <5:4>, FAN2 div is
494 * in <7:6>
495 */
496 sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
497 if ( n == 0 ) { /* FAN1 */
498 divisor <<= 4;
499 sdata = (sdata & 0xCF) | divisor;
500 } else { /* FAN2 */
501 divisor <<= 6;
502 sdata = (sdata & 0x3F) | divisor;
503 }
504
505 (*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
506 }
507 return (0);
508
509 }
510
511 int
512 lm_streinfo(sme, binfo)
513 struct sysmon_envsys *sme;
514 struct envsys_basic_info *binfo;
515 {
516 struct lm_softc *sc = sme->sme_cookie;
517
518 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
519 sc->info[binfo->sensor].rfact = binfo->rfact;
520 else {
521 if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
522 generic_streinfo_fan(sc, &sc->info[binfo->sensor],
523 binfo->sensor - 8, binfo);
524 }
525 memcpy(sc->info[binfo->sensor].desc, binfo->desc,
526 sizeof(sc->info[binfo->sensor].desc));
527 sc->info[binfo->sensor].desc[
528 sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
529
530 binfo->validflags = ENVSYS_FVALID;
531 }
532 return (0);
533 }
534
535 int
536 wb781_streinfo(sme, binfo)
537 struct sysmon_envsys *sme;
538 struct envsys_basic_info *binfo;
539 {
540 struct lm_softc *sc = sme->sme_cookie;
541 int divisor;
542 u_int8_t sdata;
543 int i;
544
545 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
546 sc->info[binfo->sensor].rfact = binfo->rfact;
547 else {
548 if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
549 if (binfo->rpms == 0) {
550 binfo->validflags = 0;
551 return (0);
552 }
553
554 /* write back the nominal FAN speed */
555 sc->info[binfo->sensor].rpms = binfo->rpms;
556
557 /* 153 is the nominal FAN speed value */
558 divisor = 1350000 / (binfo->rpms * 153);
559
560 /* ...but we need lg(divisor) */
561 for (i = 0; i < 7; i++) {
562 if (divisor <= (1 << i))
563 break;
564 }
565 divisor = i;
566
567 if (binfo->sensor == 10 || binfo->sensor == 11) {
568 /*
569 * FAN1 div is in bits <5:4>, FAN2 div
570 * is in <7:6>
571 */
572 sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
573 if ( binfo->sensor == 10 ) { /* FAN1 */
574 sdata = (sdata & 0xCF) |
575 ((divisor & 0x3) << 4);
576 } else { /* FAN2 */
577 sdata = (sdata & 0x3F) |
578 ((divisor & 0x3) << 6);
579 }
580 (*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
581 } else {
582 /* FAN3 is in WB_PIN <7:6> */
583 sdata = (*sc->lm_readreg)(sc, WB_PIN);
584 sdata = (sdata & 0x3F) |
585 ((divisor & 0x3) << 6);
586 (*sc->lm_writereg)(sc, WB_PIN, sdata);
587 }
588 }
589 memcpy(sc->info[binfo->sensor].desc, binfo->desc,
590 sizeof(sc->info[binfo->sensor].desc));
591 sc->info[binfo->sensor].desc[
592 sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
593
594 binfo->validflags = ENVSYS_FVALID;
595 }
596 return (0);
597 }
598
599 int
600 wb782_streinfo(sme, binfo)
601 struct sysmon_envsys *sme;
602 struct envsys_basic_info *binfo;
603 {
604 struct lm_softc *sc = sme->sme_cookie;
605 int divisor;
606 u_int8_t sdata;
607 int i;
608
609 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
610 sc->info[binfo->sensor].rfact = binfo->rfact;
611 else {
612 if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
613 if (binfo->rpms == 0) {
614 binfo->validflags = 0;
615 return (0);
616 }
617
618 /* write back the nominal FAN speed */
619 sc->info[binfo->sensor].rpms = binfo->rpms;
620
621 /* 153 is the nominal FAN speed value */
622 divisor = 1350000 / (binfo->rpms * 153);
623
624 /* ...but we need lg(divisor) */
625 for (i = 0; i < 7; i++) {
626 if (divisor <= (1 << i))
627 break;
628 }
629 divisor = i;
630
631 if (binfo->sensor == 12 || binfo->sensor == 13) {
632 /*
633 * FAN1 div is in bits <5:4>, FAN2 div
634 * is in <7:6>
635 */
636 sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
637 if ( binfo->sensor == 12 ) { /* FAN1 */
638 sdata = (sdata & 0xCF) |
639 ((divisor & 0x3) << 4);
640 } else { /* FAN2 */
641 sdata = (sdata & 0x3F) |
642 ((divisor & 0x3) << 6);
643 }
644 (*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
645 } else {
646 /* FAN3 is in WB_PIN <7:6> */
647 sdata = (*sc->lm_readreg)(sc, WB_PIN);
648 sdata = (sdata & 0x3F) |
649 ((divisor & 0x3) << 6);
650 (*sc->lm_writereg)(sc, WB_PIN, sdata);
651 }
652 /* Bit 2 of divisor is in WB_BANK0_FANBAT */
653 (*sc->lm_banksel)(sc, 0);
654 sdata = (*sc->lm_readreg)(sc, WB_BANK0_FANBAT);
655 sdata &= ~(0x20 << (binfo->sensor - 12));
656 sdata |= (divisor & 0x4) << (binfo->sensor - 9);
657 (*sc->lm_writereg)(sc, WB_BANK0_FANBAT, sdata);
658 }
659
660 memcpy(sc->info[binfo->sensor].desc, binfo->desc,
661 sizeof(sc->info[binfo->sensor].desc));
662 sc->info[binfo->sensor].desc[
663 sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
664
665 binfo->validflags = ENVSYS_FVALID;
666 }
667 return (0);
668 }
669
670 static void
671 generic_stemp(sc, sensor)
672 struct lm_softc *sc;
673 struct envsys_tre_data *sensor;
674 {
675 int sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 7);
676 DPRINTF(("sdata[temp] 0x%x\n", sdata));
677 /* temp is given in deg. C, we convert to uK */
678 sensor->cur.data_us = sdata * 1000000 + 273150000;
679 }
680
681 static void
682 generic_svolt(sc, sensors, infos)
683 struct lm_softc *sc;
684 struct envsys_tre_data *sensors;
685 struct envsys_basic_info *infos;
686 {
687 int i, sdata;
688
689 for (i = 0; i < 7; i++) {
690 sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i);
691 DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
692 /* voltage returned as (mV >> 4), we convert to uVDC */
693 sensors[i].cur.data_s = (sdata << 4);
694 /* rfact is (factor * 10^4) */
695 sensors[i].cur.data_s *= infos[i].rfact;
696 /* division by 10 gets us back to uVDC */
697 sensors[i].cur.data_s /= 10;
698
699 /* these two are negative voltages */
700 if ( (i == 5) || (i == 6) )
701 sensors[i].cur.data_s *= -1;
702 }
703 }
704
705 static void
706 generic_fanrpm(sc, sensors)
707 struct lm_softc *sc;
708 struct envsys_tre_data *sensors;
709 {
710 int i, sdata, divisor;
711 for (i = 0; i < 3; i++) {
712 sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 8 + i);
713 DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
714 if (i == 2)
715 divisor = 2; /* Fixed divisor for FAN3 */
716 else if (i == 1) /* Bits 7 & 6 of VID/FAN */
717 divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
718 else
719 divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
720
721 if (sdata == 0xff || sdata == 0x00) {
722 sensors[i].cur.data_us = 0;
723 } else {
724 sensors[i].cur.data_us = 1350000 / (sdata << divisor);
725 }
726 }
727 }
728
729 /*
730 * pre: last read occurred >= 1.5 seconds ago
731 * post: sensors[] current data are the latest from the chip
732 */
733 void
734 lm_refresh_sensor_data(sc)
735 struct lm_softc *sc;
736 {
737 /* Refresh our stored data for every sensor */
738 generic_stemp(sc, &sc->sensors[7]);
739 generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
740 generic_fanrpm(sc, &sc->sensors[8]);
741 }
742
743 static void
744 wb_svolt(sc)
745 struct lm_softc *sc;
746 {
747 int i, sdata;
748 for (i = 0; i < 9; ++i) {
749 if (i < 7) {
750 sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i);
751 } else {
752 /* from bank5 */
753 (*sc->lm_banksel)(sc, 5);
754 sdata = (*sc->lm_readreg)(sc, (i == 7) ?
755 WB_BANK5_5VSB : WB_BANK5_VBAT);
756 }
757 DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
758 /* voltage returned as (mV >> 4), we convert to uV */
759 sdata = sdata << 4;
760 /* special case for negative voltages */
761 if (i == 5) {
762 /*
763 * -12Vdc, assume Winbond recommended values for
764 * resistors
765 */
766 sdata = ((sdata * 1000) - (3600 * 805)) / 195;
767 } else if (i == 6) {
768 /*
769 * -5Vdc, assume Winbond recommended values for
770 * resistors
771 */
772 sdata = ((sdata * 1000) - (3600 * 682)) / 318;
773 }
774 /* rfact is (factor * 10^4) */
775 sc->sensors[i].cur.data_s = sdata * sc->info[i].rfact;
776 /* division by 10 gets us back to uVDC */
777 sc->sensors[i].cur.data_s /= 10;
778 }
779 }
780
781 static void
782 wb_stemp(sc, sensors, n)
783 struct lm_softc *sc;
784 struct envsys_tre_data *sensors;
785 int n;
786 {
787 int sdata;
788 /* temperatures. Given in dC, we convert to uK */
789 sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 7);
790 DPRINTF(("sdata[temp0] 0x%x\n", sdata));
791 sensors[0].cur.data_us = sdata * 1000000 + 273150000;
792 /* from bank1 */
793 if ((*sc->lm_banksel)(sc, 1))
794 sensors[1].validflags &= ~ENVSYS_FCURVALID;
795 else {
796 sdata = (*sc->lm_readreg)(sc, WB_BANK1_T2H) << 1;
797 sdata |= ((*sc->lm_readreg)(sc, WB_BANK1_T2L) & 0x80) >> 7;
798 DPRINTF(("sdata[temp1] 0x%x\n", sdata));
799 sensors[1].cur.data_us = (sdata * 1000000) / 2 + 273150000;
800 }
801 if (n < 3)
802 return;
803 /* from bank2 */
804 if ((*sc->lm_banksel)(sc, 2))
805 sensors[2].validflags &= ~ENVSYS_FCURVALID;
806 else {
807 sdata = (*sc->lm_readreg)(sc, WB_BANK2_T3H) << 1;
808 sdata |= ((*sc->lm_readreg)(sc, WB_BANK2_T3L) & 0x80) >> 7;
809 DPRINTF(("sdata[temp2] 0x%x\n", sdata));
810 sensors[2].cur.data_us = (sdata * 1000000) / 2 + 273150000;
811 }
812 }
813
814 static void
815 wb781_fanrpm(sc, sensors)
816 struct lm_softc *sc;
817 struct envsys_tre_data *sensors;
818 {
819 int i, divisor, sdata;
820 (*sc->lm_banksel)(sc, 0);
821 for (i = 0; i < 3; i++) {
822 sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i + 8);
823 DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
824 if (i == 0)
825 divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
826 else if (i == 1)
827 divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
828 else
829 divisor = ((*sc->lm_readreg)(sc, WB_PIN) >> 6) & 0x3;
830
831 DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
832 if (sdata == 0xff || sdata == 0x00) {
833 sensors[i].cur.data_us = 0;
834 } else {
835 sensors[i].cur.data_us = 1350000 /
836 (sdata << divisor);
837 }
838 }
839 }
840
841 static void
842 wb_fanrpm(sc, sensors)
843 struct lm_softc *sc;
844 struct envsys_tre_data *sensors;
845 {
846 int i, divisor, sdata;
847 (*sc->lm_banksel)(sc, 0);
848 for (i = 0; i < 3; i++) {
849 sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i + 8);
850 DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
851 if (i == 0)
852 divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
853 else if (i == 1)
854 divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
855 else
856 divisor = ((*sc->lm_readreg)(sc, WB_PIN) >> 6) & 0x3;
857 divisor |= ((*sc->lm_readreg)(sc, WB_BANK0_FANBAT) >> (i + 3)) & 0x4;
858
859 DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
860 if (sdata == 0xff || sdata == 0x00) {
861 sensors[i].cur.data_us = 0;
862 } else {
863 sensors[i].cur.data_us = 1350000 /
864 (sdata << divisor);
865 }
866 }
867 }
868
869 void
870 wb781_refresh_sensor_data(sc)
871 struct lm_softc *sc;
872 {
873 /* Refresh our stored data for every sensor */
874 /* we need to reselect bank0 to access common registers */
875 (*sc->lm_banksel)(sc, 0);
876 generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
877 (*sc->lm_banksel)(sc, 0);
878 wb_stemp(sc, &sc->sensors[7], 3);
879 (*sc->lm_banksel)(sc, 0);
880 wb781_fanrpm(sc, &sc->sensors[10]);
881 }
882
883 void
884 wb782_refresh_sensor_data(sc)
885 struct lm_softc *sc;
886 {
887 /* Refresh our stored data for every sensor */
888 wb_svolt(sc);
889 wb_stemp(sc, &sc->sensors[9], 3);
890 wb_fanrpm(sc, &sc->sensors[12]);
891 }
892
893 void
894 wb697_refresh_sensor_data(sc)
895 struct lm_softc *sc;
896 {
897 /* Refresh our stored data for every sensor */
898 wb_svolt(sc);
899 wb_stemp(sc, &sc->sensors[9], 2);
900 wb_fanrpm(sc, &sc->sensors[11]);
901 }
902