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