Home | History | Annotate | Line # | Download | only in ic
nslm7x.c revision 1.8
      1 /*	$NetBSD: nslm7x.c,v 1.8 2000/08/02 21:50:37 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 		sc->numsensors = WB83781_NUM_SENSORS;
    313 		sc->refresh_sensor_data = wb781_refresh_sensor_data;
    314 
    315 		for (i = 0; i < 7; ++i) {
    316 			sc->sensors[i].units = sc->info[i].units =
    317 			    ENVSYS_SVOLTS_DC;
    318 			sprintf(sc->info[i].desc, "IN %d", i);
    319 		}
    320 
    321 		/* default correction factors for higher voltage inputs */
    322 		sc->info[0].rfact = sc->info[1].rfact =
    323 		    sc->info[2].rfact = 10000;
    324 		sc->info[3].rfact = (int)(( 90.9 / 60.4) * 10000);
    325 		sc->info[4].rfact = (int)(( 38.0 / 10.0) * 10000);
    326 		sc->info[5].rfact = (int)((210.0 / 60.4) * 10000);
    327 		sc->info[6].rfact = (int)(( 90.9 / 60.4) * 10000);
    328 
    329 		for (i = 7; i < 10; ++i) {
    330 			sc->sensors[i].units = sc->info[i].units =
    331 			    ENVSYS_STEMP;
    332 			sprintf(sc->info[i].desc, "Temp%d", i - 6);
    333 		}
    334 
    335 		for (i = 10; i < 13; ++i) {
    336 			sc->sensors[i].units = sc->info[i].units =
    337 			    ENVSYS_SFANRPM;
    338 			sprintf(sc->info[i].desc, "Fan %d", i - 9);
    339 		}
    340 		sc->sc_sysmon.sme_streinfo = wb781_streinfo;
    341 		return 1;
    342 	case WB_CHIPID_83697:
    343 		printf(": W83697HF\n");
    344 		wb_setup_volt(sc);
    345 		setup_temp(sc, 9, 2);
    346 		setup_fan(sc, 11, 3);
    347 		sc->numsensors = WB83697_NUM_SENSORS;
    348 		sc->refresh_sensor_data = wb697_refresh_sensor_data;
    349 		sc->sc_sysmon.sme_streinfo = wb782_streinfo;
    350 	return 1;
    351 		break;
    352 	case WB_CHIPID_83782:
    353 		printf(": W83782D\n");
    354 		break;
    355 	case WB_CHIPID_83627:
    356 		printf(": W83627HF\n");
    357 		break;
    358 	default:
    359 		printf(": unknow winbond chip ID 0x%x\n", j);
    360 		/* handle as a standart lm7x */
    361 		lm_common_match(sc);
    362 		return 1;
    363 	}
    364 	/* common code for the W83782D and W83627HF */
    365 	wb_setup_volt(sc);
    366 	setup_temp(sc, 9, 3);
    367 	setup_fan(sc, 12, 3);
    368 	sc->numsensors = WB_NUM_SENSORS;
    369 	sc->refresh_sensor_data = wb782_refresh_sensor_data;
    370 	sc->sc_sysmon.sme_streinfo = wb782_streinfo;
    371 	return 1;
    372 }
    373 
    374 static void
    375 wb_setup_volt(sc)
    376 	struct lm_softc *sc;
    377 {
    378 	sc->sensors[0].units = sc->info[0].units = ENVSYS_SVOLTS_DC;
    379 	sprintf(sc->info[0].desc, "VCORE A");
    380 	sc->info[0].rfact = 10000;
    381 	sc->sensors[1].units = sc->info[1].units = ENVSYS_SVOLTS_DC;
    382 	sprintf(sc->info[1].desc, "VCORE B");
    383 	sc->info[1].rfact = 10000;
    384 	sc->sensors[2].units = sc->info[2].units = ENVSYS_SVOLTS_DC;
    385 	sprintf(sc->info[2].desc, "+3.3V");
    386 	sc->info[2].rfact = 10000;
    387 	sc->sensors[3].units = sc->info[3].units = ENVSYS_SVOLTS_DC;
    388 	sprintf(sc->info[3].desc, "+5V");
    389 	sc->info[3].rfact = 16778;
    390 	sc->sensors[4].units = sc->info[4].units = ENVSYS_SVOLTS_DC;
    391 	sprintf(sc->info[4].desc, "+12V");
    392 	sc->info[4].rfact = 38000;
    393 	sc->sensors[5].units = sc->info[5].units = ENVSYS_SVOLTS_DC;
    394 	sprintf(sc->info[5].desc, "-12V");
    395 	sc->info[5].rfact = 10000;
    396 	sc->sensors[6].units = sc->info[6].units = ENVSYS_SVOLTS_DC;
    397 	sprintf(sc->info[6].desc, "-5V");
    398 	sc->info[6].rfact = 10000;
    399 	sc->sensors[7].units = sc->info[7].units = ENVSYS_SVOLTS_DC;
    400 	sprintf(sc->info[7].desc, "+5VSB");
    401 	sc->info[7].rfact = 15151;
    402 	sc->sensors[8].units = sc->info[8].units = ENVSYS_SVOLTS_DC;
    403 	sprintf(sc->info[8].desc, "VBAT");
    404 	sc->info[8].rfact = 10000;
    405 }
    406 
    407 static void
    408 setup_temp(sc, start, n)
    409 	struct lm_softc *sc;
    410 	int start, n;
    411 {
    412 	int i;
    413 
    414 	for (i = 0; i < n; i++) {
    415 		sc->sensors[start + i].units = ENVSYS_STEMP;
    416 		sprintf(sc->info[start + i].desc, "Temp %d", i + 1);
    417 	}
    418 }
    419 
    420 
    421 static void
    422 setup_fan(sc, start, n)
    423 	struct lm_softc *sc;
    424 	int start, n;
    425 {
    426 	int i;
    427 	for (i = 0; i < n; ++i) {
    428 		sc->sensors[start + i].units = ENVSYS_SFANRPM;
    429 		sc->info[start + i].units = ENVSYS_SFANRPM;
    430 		sprintf(sc->info[start + i].desc, "Fan %d", i + 1);
    431 	}
    432 }
    433 
    434 int
    435 lm_gtredata(sme, tred)
    436 	 struct sysmon_envsys *sme;
    437 	 struct envsys_tre_data *tred;
    438 {
    439 	 static const struct timeval onepointfive = { 1, 500000 };
    440 	 struct timeval t;
    441 	 struct lm_softc *sc = sme->sme_cookie;
    442 	 int i, s;
    443 
    444 	 /* read new values at most once every 1.5 seconds */
    445 	 timeradd(&sc->lastread, &onepointfive, &t);
    446 	 s = splclock();
    447 	 i = timercmp(&mono_time, &t, >);
    448 	 if (i) {
    449 		  sc->lastread.tv_sec  = mono_time.tv_sec;
    450 		  sc->lastread.tv_usec = mono_time.tv_usec;
    451 	 }
    452 	 splx(s);
    453 
    454 	 if (i)
    455 		  sc->refresh_sensor_data(sc);
    456 
    457 	 *tred = sc->sensors[tred->sensor];
    458 
    459 	 return (0);
    460 }
    461 
    462 int
    463 generic_streinfo_fan(sc, info, n, binfo)
    464 	struct lm_softc *sc;
    465 	struct envsys_basic_info *info;
    466 	int n;
    467 	struct envsys_basic_info *binfo;
    468 {
    469 	u_int8_t sdata;
    470 	int divisor;
    471 
    472 	/* FAN1 and FAN2 can have divisors set, but not FAN3 */
    473 	if ((sc->info[binfo->sensor].units == ENVSYS_SFANRPM)
    474 	    && (binfo->sensor != 2)) {
    475 		if (binfo->rpms == 0) {
    476 			binfo->validflags = 0;
    477 			return (0);
    478 		}
    479 
    480 		/* 153 is the nominal FAN speed value */
    481 		divisor = 1350000 / (binfo->rpms * 153);
    482 
    483 		/* ...but we need lg(divisor) */
    484 		if (divisor <= 1)
    485 		    divisor = 0;
    486 		else if (divisor <= 2)
    487 		    divisor = 1;
    488 		else if (divisor <= 4)
    489 		    divisor = 2;
    490 		else
    491 		    divisor = 3;
    492 
    493 		/*
    494 		 * FAN1 div is in bits <5:4>, FAN2 div is
    495 		 * in <7:6>
    496 		 */
    497 		sdata = lm_readreg(sc, LMD_VIDFAN);
    498 		if ( binfo->sensor == 0 ) {  /* FAN1 */
    499 		    divisor <<= 4;
    500 		    sdata = (sdata & 0xCF) | divisor;
    501 		} else { /* FAN2 */
    502 		    divisor <<= 6;
    503 		    sdata = (sdata & 0x3F) | divisor;
    504 		}
    505 
    506 		lm_writereg(sc, LMD_VIDFAN, sdata);
    507 	}
    508 	return (0);
    509 
    510 }
    511 
    512 int
    513 lm_streinfo(sme, binfo)
    514 	 struct sysmon_envsys *sme;
    515 	 struct envsys_basic_info *binfo;
    516 {
    517 	 struct lm_softc *sc = sme->sme_cookie;
    518 
    519 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
    520 		  sc->info[binfo->sensor].rfact = binfo->rfact;
    521 	 else {
    522 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
    523 			generic_streinfo_fan(sc, &sc->info[binfo->sensor],
    524 			    binfo->sensor - 8, binfo);
    525 		}
    526 		memcpy(sc->info[binfo->sensor].desc, binfo->desc,
    527 		    sizeof(sc->info[binfo->sensor].desc));
    528 		sc->info[binfo->sensor].desc[
    529 		    sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
    530 
    531 		binfo->validflags = ENVSYS_FVALID;
    532 	 }
    533 	 return (0);
    534 }
    535 
    536 int
    537 wb781_streinfo(sme, binfo)
    538 	 struct sysmon_envsys *sme;
    539 	 struct envsys_basic_info *binfo;
    540 {
    541 	 struct lm_softc *sc = sme->sme_cookie;
    542 
    543 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
    544 		  sc->info[binfo->sensor].rfact = binfo->rfact;
    545 	 else {
    546 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
    547 			generic_streinfo_fan(sc, &sc->info[binfo->sensor],
    548 			    binfo->sensor - 10, binfo);
    549 		}
    550 		memcpy(sc->info[binfo->sensor].desc, binfo->desc,
    551 		    sizeof(sc->info[binfo->sensor].desc));
    552 		sc->info[binfo->sensor].desc[
    553 		    sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
    554 
    555 		binfo->validflags = ENVSYS_FVALID;
    556 	 }
    557 	 return (0);
    558 }
    559 
    560 int
    561 wb782_streinfo(sme, binfo)
    562 	 struct sysmon_envsys *sme;
    563 	 struct envsys_basic_info *binfo;
    564 {
    565 	 struct lm_softc *sc = sme->sme_cookie;
    566 	 int divisor;
    567 	 u_int8_t sdata;
    568 	 int i;
    569 
    570 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
    571 		  sc->info[binfo->sensor].rfact = binfo->rfact;
    572 	 else {
    573 	 	if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
    574 			if (binfo->rpms == 0) {
    575 				binfo->validflags = 0;
    576 				return (0);
    577 			}
    578 
    579 			/* 153 is the nominal FAN speed value */
    580 			divisor = 1350000 / (binfo->rpms * 153);
    581 
    582 			/* ...but we need lg(divisor) */
    583 			for (i = 0; i < 7; i++) {
    584 				if (divisor <= (1 << i))
    585 				 	break;
    586 			}
    587 			divisor = i;
    588 
    589 			if (binfo->sensor == 12 || binfo->sensor == 13) {
    590 				/*
    591 				 * FAN1 div is in bits <5:4>, FAN2 div
    592 				 * is in <7:6>
    593 				 */
    594 				sdata = lm_readreg(sc, LMD_VIDFAN);
    595 				if ( binfo->sensor == 12 ) {  /* FAN1 */
    596 					 sdata = (sdata & 0xCF) |
    597 					     ((divisor & 0x3) << 4);
    598 				} else { /* FAN2 */
    599 					 sdata = (sdata & 0x3F) |
    600 					     ((divisor & 0x3) << 6);
    601 				}
    602 				lm_writereg(sc, LMD_VIDFAN, sdata);
    603 			} else {
    604 				/* FAN3 is in WB_PIN <7:6> */
    605 				sdata = lm_readreg(sc, WB_PIN);
    606 				sdata = (sdata & 0x3F) |
    607 				     ((divisor & 0x3) << 6);
    608 				lm_writereg(sc, LMD_VIDFAN, sdata);
    609 			}
    610 			/* Bit 2 of divisor is in WB_BANK0_FANBAT */
    611 			lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B0);
    612 			sdata = lm_readreg(sc, WB_BANK0_FANBAT);
    613 			sdata &= ~(0x20 << (binfo->sensor - 12));
    614 			sdata |= (divisor & 0x4) << (binfo->sensor - 9);
    615 			lm_writereg(sc, WB_BANK0_FANBAT, sdata);
    616 		}
    617 
    618 		memcpy(sc->info[binfo->sensor].desc, binfo->desc,
    619 		    sizeof(sc->info[binfo->sensor].desc));
    620 		sc->info[binfo->sensor].desc[
    621 		    sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
    622 
    623 		binfo->validflags = ENVSYS_FVALID;
    624 	}
    625 	return (0);
    626 }
    627 
    628 static void
    629 generic_stemp(sc, sensor)
    630 	struct lm_softc *sc;
    631 	struct envsys_tre_data *sensor;
    632 {
    633 	int sdata = lm_readreg(sc, LMD_SENSORBASE + 7);
    634 	/* temp is given in deg. C, we convert to uK */
    635 	sensor->cur.data_us = sdata * 1000000 + 273150000;
    636 }
    637 
    638 static void
    639 generic_svolt(sc, sensors, infos)
    640 	struct lm_softc *sc;
    641 	struct envsys_tre_data *sensors;
    642 	struct envsys_basic_info *infos;
    643 {
    644 	int i, sdata;
    645 
    646 	for (i = 0; i < 7; i++) {
    647 		sdata = lm_readreg(sc, LMD_SENSORBASE + i);
    648 		/* voltage returned as (mV >> 4), we convert to uVDC */
    649 		sensors[i].cur.data_s = (sdata << 4);
    650 		/* rfact is (factor * 10^4) */
    651 		sensors[i].cur.data_s *= infos[i].rfact;
    652 		/* division by 10 gets us back to uVDC */
    653 		sensors[i].cur.data_s /= 10;
    654 
    655 		/* these two are negative voltages */
    656 		if ( (i == 5) || (i == 6) )
    657 			sensors[i].cur.data_s *= -1;
    658 	}
    659 }
    660 
    661 static void
    662 generic_fanrpm(sc, sensors)
    663 	struct lm_softc *sc;
    664 	struct envsys_tre_data *sensors;
    665 {
    666 	int i, sdata, divisor;
    667 	for (i = 0; i < 3; i++) {
    668 		sdata = lm_readreg(sc, LMD_SENSORBASE + 8 + i);
    669 		if (i == 2)
    670 			divisor = 2;	/* Fixed divisor for FAN3 */
    671 		else if (i == 1)	/* Bits 7 & 6 of VID/FAN  */
    672 			divisor = (lm_readreg(sc, LMD_VIDFAN) >> 6) & 0x3;
    673 		else
    674 			divisor = (lm_readreg(sc, LMD_VIDFAN) >> 4) & 0x3;
    675 
    676 		if (sdata == 0xff || sdata == 0x00) {
    677 			sensors[i].cur.data_us = 0;
    678 		} else {
    679 			sensors[i].cur.data_us = 1350000 / (sdata << divisor);
    680 		}
    681 	}
    682 }
    683 
    684 /*
    685  * pre:  last read occured >= 1.5 seconds ago
    686  * post: sensors[] current data are the latest from the chip
    687  */
    688 void
    689 lm_refresh_sensor_data(sc)
    690 	struct lm_softc *sc;
    691 {
    692 	/* Refresh our stored data for every sensor */
    693 	generic_stemp(sc, &sc->sensors[7]);
    694 	generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
    695 	generic_fanrpm(sc, &sc->sensors[8]);
    696 }
    697 
    698 static void
    699 wb_svolt(sc)
    700 	struct lm_softc *sc;
    701 {
    702 	int i, sdata;
    703 	for (i = 0; i < 9; ++i) {
    704 		if (i < 7) {
    705 			sdata = lm_readreg(sc, LMD_SENSORBASE + i);
    706 		} else {
    707 			/* from bank5 */
    708 			lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B5);
    709 			sdata = lm_readreg(sc, (i == 7) ?
    710 			    WB_BANK5_5VSB : WB_BANK5_VBAT);
    711 		}
    712 		DPRINTF(("sdata[%d] 0x%x\n", i, sdata));
    713 		/* voltage returned as (mV >> 4), we convert to uV */
    714 		sdata =  sdata << 4;
    715 		/* special case for negative voltages */
    716 		if (i == 5) {
    717 			/*
    718 			 * -12Vdc, assume Winbond recommended values for
    719 			 * resistors
    720 			 */
    721 			sdata = ((sdata * 1000) - (3600 * 805)) / 195;
    722 		} else if (i == 6) {
    723 			/*
    724 			 * -5Vdc, assume Winbond recommended values for
    725 			 * resistors
    726 			 */
    727 			sdata = ((sdata * 1000) - (3600 * 682)) / 318;
    728 		}
    729 		/* rfact is (factor * 10^4) */
    730 		sc->sensors[i].cur.data_s = sdata * sc->info[i].rfact;
    731 		/* division by 10 gets us back to uVDC */
    732 		sc->sensors[i].cur.data_s /= 10;
    733 	}
    734 }
    735 
    736 static void
    737 wb_stemp(sc, sensors, n)
    738 	struct lm_softc *sc;
    739 	struct  envsys_tre_data *sensors;
    740 	int n;
    741 {
    742 	int sdata;
    743 	/* temperatures. Given in dC, we convert to uK */
    744 	sdata = lm_readreg(sc, LMD_SENSORBASE + 7);
    745 	DPRINTF(("sdata[%d] 0x%x\n", 9, sdata));
    746 	sensors[0].cur.data_us = sdata * 1000000 + 273150000;
    747 	/* from bank1 */
    748 	lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B1);
    749 	sdata = lm_readreg(sc, WB_BANK1_T2H) << 1;
    750 	sdata |=  (lm_readreg(sc, WB_BANK1_T2L) & 0x80) >> 7;
    751 	DPRINTF(("sdata[%d] 0x%x\n", 10, sdata));
    752 	sensors[1].cur.data_us = (sdata * 1000000) / 2 + 273150000;
    753 	if (n < 3)
    754 		return;
    755 	/* from bank2 */
    756 	lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B2);
    757 	sdata = lm_readreg(sc, WB_BANK2_T3H) << 1;
    758 	sdata |=  (lm_readreg(sc, WB_BANK2_T3L) & 0x80) >> 7;
    759 	DPRINTF(("sdata[%d] 0x%x\n", 11, sdata));
    760 	sensors[2].cur.data_us = (sdata * 1000000) / 2 + 273150000;
    761 }
    762 
    763 static void
    764 wb_fanrpm(sc, sensors)
    765 	struct lm_softc *sc;
    766 	struct envsys_tre_data *sensors;
    767 {
    768 	int i, divisor, sdata;
    769 	lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B0);
    770 	for (i = 0; i < 3; i++) {
    771 		sdata = lm_readreg(sc, LMD_SENSORBASE + i + 8);
    772 		if (i == 0)
    773 			divisor = (lm_readreg(sc, LMD_VIDFAN) >> 4) & 0x3;
    774 		else if (i == 1)
    775 			divisor = (lm_readreg(sc, LMD_VIDFAN) >> 6) & 0x3;
    776 		else
    777 			divisor = (lm_readreg(sc, WB_PIN) >> 6) & 0x3;
    778 		divisor |= (lm_readreg(sc, WB_BANK0_FANBAT) >> (i + 3)) & 0x4;
    779 
    780 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
    781 		if (sdata == 0xff || sdata == 0x00) {
    782 			sensors[i].cur.data_us = 0;
    783 		} else {
    784 			sensors[i].cur.data_us = 1350000 /
    785 			    (sdata << divisor);
    786 		}
    787 	}
    788 }
    789 
    790 void
    791 wb781_refresh_sensor_data(sc)
    792 	struct lm_softc *sc;
    793 {
    794 	/* Refresh our stored data for every sensor */
    795 	generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
    796 	wb_stemp(sc, &sc->sensors[7], 3);
    797 	generic_fanrpm(sc, &sc->sensors[10]);
    798 }
    799 
    800 void
    801 wb782_refresh_sensor_data(sc)
    802 	struct lm_softc *sc;
    803 {
    804 	/* Refresh our stored data for every sensor */
    805 	wb_svolt(sc);
    806 	wb_stemp(sc, &sc->sensors[9], 3);
    807 	wb_fanrpm(sc, &sc->sensors[12]);
    808 }
    809 
    810 void
    811 wb697_refresh_sensor_data(sc)
    812 	struct lm_softc *sc;
    813 {
    814 	/* Refresh our stored data for every sensor */
    815 	wb_svolt(sc);
    816 	wb_stemp(sc, &sc->sensors[9], 2);
    817 	wb_fanrpm(sc, &sc->sensors[11]);
    818 }
    819