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