Home | History | Annotate | Line # | Download | only in ic
nslm7x.c revision 1.26
      1 /*	$NetBSD: nslm7x.c,v 1.26 2006/06/07 22:33:36 kardel 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.26 2006/06/07 22:33:36 kardel Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/device.h>
     47 #include <sys/malloc.h>
     48 #include <sys/errno.h>
     49 #include <sys/queue.h>
     50 #include <sys/lock.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/conf.h>
     53 #include <sys/time.h>
     54 
     55 #include <machine/bus.h>
     56 
     57 #include <dev/isa/isareg.h>
     58 #include <dev/isa/isavar.h>
     59 
     60 #include <dev/sysmon/sysmonvar.h>
     61 
     62 #include <dev/ic/nslm7xvar.h>
     63 
     64 #include <machine/intr.h>
     65 #include <machine/bus.h>
     66 
     67 #if defined(LMDEBUG)
     68 #define DPRINTF(x)		printf x
     69 #else
     70 #define DPRINTF(x)
     71 #endif
     72 
     73 const struct envsys_range lm_ranges[] = {	/* sc->sensors sub-intervals */
     74 					/* for each unit type */
     75 	{ 7, 7,    ENVSYS_STEMP   },
     76 	{ 8, 10,   ENVSYS_SFANRPM },
     77 	{ 1, 0,    ENVSYS_SVOLTS_AC },	/* None */
     78 	{ 0, 6,    ENVSYS_SVOLTS_DC },
     79 	{ 1, 0,    ENVSYS_SOHMS },	/* None */
     80 	{ 1, 0,    ENVSYS_SWATTS },	/* None */
     81 	{ 1, 0,    ENVSYS_SAMPS }	/* None */
     82 };
     83 
     84 
     85 static void setup_fan(struct lm_softc *, int, int);
     86 static void setup_temp(struct lm_softc *, int, int);
     87 static void wb_setup_volt(struct lm_softc *);
     88 
     89 int lm_match(struct lm_softc *);
     90 int wb_match(struct lm_softc *);
     91 int def_match(struct lm_softc *);
     92 void lm_common_match(struct lm_softc *);
     93 static int lm_generic_banksel(struct lm_softc *, int);
     94 
     95 static void generic_stemp(struct lm_softc *, struct envsys_tre_data *);
     96 static void generic_svolt(struct lm_softc *, struct envsys_tre_data *,
     97     struct envsys_basic_info *);
     98 static void generic_fanrpm(struct lm_softc *, struct envsys_tre_data *);
     99 
    100 void lm_refresh_sensor_data(struct lm_softc *);
    101 
    102 static void wb_svolt(struct lm_softc *);
    103 static void wb_stemp(struct lm_softc *, struct envsys_tre_data *, int);
    104 static void wb781_fanrpm(struct lm_softc *, struct envsys_tre_data *);
    105 static void wb_fanrpm(struct lm_softc *, struct envsys_tre_data *);
    106 
    107 void wb781_refresh_sensor_data(struct lm_softc *);
    108 void wb782_refresh_sensor_data(struct lm_softc *);
    109 void wb697_refresh_sensor_data(struct lm_softc *);
    110 
    111 int lm_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
    112 
    113 int generic_streinfo_fan(struct lm_softc *, struct envsys_basic_info *,
    114            int, struct envsys_basic_info *);
    115 int lm_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
    116 int wb781_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
    117 int wb782_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
    118 
    119 struct lm_chip {
    120 	int (*chip_match)(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) || (cr == 0x03))
    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 	case WB_CHIPID_83627THF:
    353 		printf(": W83627THF\n");
    354 		break;
    355 	default:
    356 		printf(": unknow winbond chip ID 0x%x\n", j);
    357 		/* handle as a standart lm7x */
    358 		lm_common_match(sc);
    359 		return 1;
    360 	}
    361 	/* common code for the W83782D and W83627HF */
    362 	wb_setup_volt(sc);
    363 	setup_temp(sc, 9, 3);
    364 	setup_fan(sc, 12, 3);
    365 	sc->numsensors = WB_NUM_SENSORS;
    366 	sc->refresh_sensor_data = wb782_refresh_sensor_data;
    367 	sc->sc_sysmon.sme_streinfo = wb782_streinfo;
    368 	return 1;
    369 }
    370 
    371 static void
    372 wb_setup_volt(sc)
    373 	struct lm_softc *sc;
    374 {
    375 	sc->sensors[0].units = sc->info[0].units = ENVSYS_SVOLTS_DC;
    376 	snprintf(sc->info[0].desc, sizeof(sc->info[0].desc), "VCORE A");
    377 	sc->info[0].rfact = 10000;
    378 	sc->sensors[1].units = sc->info[1].units = ENVSYS_SVOLTS_DC;
    379 	snprintf(sc->info[1].desc, sizeof(sc->info[1].desc), "VCORE B");
    380 	sc->info[1].rfact = 10000;
    381 	sc->sensors[2].units = sc->info[2].units = ENVSYS_SVOLTS_DC;
    382 	snprintf(sc->info[2].desc, sizeof(sc->info[2].desc), "+3.3V");
    383 	sc->info[2].rfact = 10000;
    384 	sc->sensors[3].units = sc->info[3].units = ENVSYS_SVOLTS_DC;
    385 	snprintf(sc->info[3].desc, sizeof(sc->info[3].desc), "+5V");
    386 	sc->info[3].rfact = 16778;
    387 	sc->sensors[4].units = sc->info[4].units = ENVSYS_SVOLTS_DC;
    388 	snprintf(sc->info[4].desc, sizeof(sc->info[4].desc), "+12V");
    389 	sc->info[4].rfact = 38000;
    390 	sc->sensors[5].units = sc->info[5].units = ENVSYS_SVOLTS_DC;
    391 	snprintf(sc->info[5].desc, sizeof(sc->info[5].desc), "-12V");
    392 	sc->info[5].rfact = 10000;
    393 	sc->sensors[6].units = sc->info[6].units = ENVSYS_SVOLTS_DC;
    394 	snprintf(sc->info[6].desc, sizeof(sc->info[6].desc), "-5V");
    395 	sc->info[6].rfact = 10000;
    396 	sc->sensors[7].units = sc->info[7].units = ENVSYS_SVOLTS_DC;
    397 	snprintf(sc->info[7].desc, sizeof(sc->info[7].desc), "+5VSB");
    398 	sc->info[7].rfact = 15151;
    399 	sc->sensors[8].units = sc->info[8].units = ENVSYS_SVOLTS_DC;
    400 	snprintf(sc->info[8].desc, sizeof(sc->info[8].desc), "VBAT");
    401 	sc->info[8].rfact = 10000;
    402 }
    403 
    404 static void
    405 setup_temp(sc, start, n)
    406 	struct lm_softc *sc;
    407 	int start, n;
    408 {
    409 	int i;
    410 
    411 	for (i = 0; i < n; i++) {
    412 		sc->sensors[start + i].units = ENVSYS_STEMP;
    413 		snprintf(sc->info[start + i].desc,
    414 		    sizeof(sc->info[start + i].desc), "Temp %d", i + 1);
    415 	}
    416 }
    417 
    418 
    419 static void
    420 setup_fan(sc, start, n)
    421 	struct lm_softc *sc;
    422 	int start, n;
    423 {
    424 	int i;
    425 	for (i = 0; i < n; ++i) {
    426 		sc->sensors[start + i].units = ENVSYS_SFANRPM;
    427 		sc->info[start + i].units = ENVSYS_SFANRPM;
    428 		snprintf(sc->info[start + i].desc,
    429 		    sizeof(sc->info[start + i].desc), "Fan %d", i + 1);
    430 	}
    431 }
    432 
    433 int
    434 lm_gtredata(sme, tred)
    435 	 struct sysmon_envsys *sme;
    436 	 struct envsys_tre_data *tred;
    437 {
    438 	static const struct timeval onepointfive = { 1, 500000 };
    439 	struct timeval t, utv;
    440 	struct lm_softc *sc = sme->sme_cookie;
    441 
    442 	/* read new values at most once every 1.5 seconds */
    443 	getmicrouptime(&utv);
    444 	timeradd(&sc->lastread, &onepointfive, &t);
    445 	if (timercmp(&utv, &t, >)) {
    446 		sc->lastread = utv;
    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 	    && (n < 2)) {
    467 		if (binfo->rpms == 0) {
    468 			binfo->validflags = 0;
    469 			return 0;
    470 		}
    471 
    472 		/* write back the nominal FAN speed  */
    473 		info->rpms = binfo->rpms;
    474 
    475 		/* 153 is the nominal FAN speed value */
    476 		divisor = 1350000 / (binfo->rpms * 153);
    477 
    478 		/* ...but we need lg(divisor) */
    479 		if (divisor <= 1)
    480 		    divisor = 0;
    481 		else if (divisor <= 2)
    482 		    divisor = 1;
    483 		else if (divisor <= 4)
    484 		    divisor = 2;
    485 		else
    486 		    divisor = 3;
    487 
    488 		/*
    489 		 * FAN1 div is in bits <5:4>, FAN2 div is
    490 		 * in <7:6>
    491 		 */
    492 		sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
    493 		if ( n == 0 ) {  /* FAN1 */
    494 		    divisor <<= 4;
    495 		    sdata = (sdata & 0xCF) | divisor;
    496 		} else { /* FAN2 */
    497 		    divisor <<= 6;
    498 		    sdata = (sdata & 0x3F) | divisor;
    499 		}
    500 
    501 		(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
    502 	}
    503 	return 0;
    504 
    505 }
    506 
    507 int
    508 lm_streinfo(sme, binfo)
    509 	 struct sysmon_envsys *sme;
    510 	 struct envsys_basic_info *binfo;
    511 {
    512 	 struct lm_softc *sc = sme->sme_cookie;
    513 
    514 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
    515 		  sc->info[binfo->sensor].rfact = binfo->rfact;
    516 	 else {
    517 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
    518 			generic_streinfo_fan(sc, &sc->info[binfo->sensor],
    519 			    binfo->sensor - 8, binfo);
    520 		}
    521 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
    522 		    sizeof(sc->info[binfo->sensor].desc));
    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 	 int divisor;
    535 	 u_int8_t sdata;
    536 	 int i;
    537 
    538 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
    539 		  sc->info[binfo->sensor].rfact = binfo->rfact;
    540 	 else {
    541 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
    542 			if (binfo->rpms == 0) {
    543 				binfo->validflags = 0;
    544 				return 0;
    545 			}
    546 
    547 			/* write back the nominal FAN speed  */
    548 			sc->info[binfo->sensor].rpms = binfo->rpms;
    549 
    550 			/* 153 is the nominal FAN speed value */
    551 			divisor = 1350000 / (binfo->rpms * 153);
    552 
    553 			/* ...but we need lg(divisor) */
    554 			for (i = 0; i < 7; i++) {
    555 				if (divisor <= (1 << i))
    556 				 	break;
    557 			}
    558 			divisor = i;
    559 
    560 			if (binfo->sensor == 10 || binfo->sensor == 11) {
    561 				/*
    562 				 * FAN1 div is in bits <5:4>, FAN2 div
    563 				 * is in <7:6>
    564 				 */
    565 				sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
    566 				if ( binfo->sensor == 10 ) {  /* FAN1 */
    567 					 sdata = (sdata & 0xCF) |
    568 					     ((divisor & 0x3) << 4);
    569 				} else { /* FAN2 */
    570 					 sdata = (sdata & 0x3F) |
    571 					     ((divisor & 0x3) << 6);
    572 				}
    573 				(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
    574 			} else {
    575 				/* FAN3 is in WB_PIN <7:6> */
    576 				sdata = (*sc->lm_readreg)(sc, WB_PIN);
    577 				sdata = (sdata & 0x3F) |
    578 				     ((divisor & 0x3) << 6);
    579 				(*sc->lm_writereg)(sc, WB_PIN, sdata);
    580 			}
    581 		}
    582 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
    583 		    sizeof(sc->info[binfo->sensor].desc));
    584 		binfo->validflags = ENVSYS_FVALID;
    585 	 }
    586 	 return 0;
    587 }
    588 
    589 int
    590 wb782_streinfo(sme, binfo)
    591 	 struct sysmon_envsys *sme;
    592 	 struct envsys_basic_info *binfo;
    593 {
    594 	 struct lm_softc *sc = sme->sme_cookie;
    595 	 int divisor;
    596 	 u_int8_t sdata;
    597 	 int i;
    598 
    599 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
    600 		  sc->info[binfo->sensor].rfact = binfo->rfact;
    601 	 else {
    602 	 	if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
    603 			if (binfo->rpms == 0) {
    604 				binfo->validflags = 0;
    605 				return 0;
    606 			}
    607 
    608 			/* write back the nominal FAN speed  */
    609 			sc->info[binfo->sensor].rpms = binfo->rpms;
    610 
    611 			/* 153 is the nominal FAN speed value */
    612 			divisor = 1350000 / (binfo->rpms * 153);
    613 
    614 			/* ...but we need lg(divisor) */
    615 			for (i = 0; i < 7; i++) {
    616 				if (divisor <= (1 << i))
    617 				 	break;
    618 			}
    619 			divisor = i;
    620 
    621 			if (binfo->sensor == 12 || binfo->sensor == 13) {
    622 				/*
    623 				 * FAN1 div is in bits <5:4>, FAN2 div
    624 				 * is in <7:6>
    625 				 */
    626 				sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
    627 				if ( binfo->sensor == 12 ) {  /* FAN1 */
    628 					 sdata = (sdata & 0xCF) |
    629 					     ((divisor & 0x3) << 4);
    630 				} else { /* FAN2 */
    631 					 sdata = (sdata & 0x3F) |
    632 					     ((divisor & 0x3) << 6);
    633 				}
    634 				(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
    635 			} else {
    636 				/* FAN3 is in WB_PIN <7:6> */
    637 				sdata = (*sc->lm_readreg)(sc, WB_PIN);
    638 				sdata = (sdata & 0x3F) |
    639 				     ((divisor & 0x3) << 6);
    640 				(*sc->lm_writereg)(sc, WB_PIN, sdata);
    641 			}
    642 			/* Bit 2 of divisor is in WB_BANK0_FANBAT */
    643 			(*sc->lm_banksel)(sc, 0);
    644 			sdata = (*sc->lm_readreg)(sc, WB_BANK0_FANBAT);
    645 			sdata &= ~(0x20 << (binfo->sensor - 12));
    646 			sdata |= (divisor & 0x4) << (binfo->sensor - 9);
    647 			(*sc->lm_writereg)(sc, WB_BANK0_FANBAT, sdata);
    648 		}
    649 
    650 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
    651 		    sizeof(sc->info[binfo->sensor].desc));
    652 		binfo->validflags = ENVSYS_FVALID;
    653 	}
    654 	return 0;
    655 }
    656 
    657 static void
    658 generic_stemp(sc, sensor)
    659 	struct lm_softc *sc;
    660 	struct envsys_tre_data *sensor;
    661 {
    662 	int sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 7);
    663 	DPRINTF(("sdata[temp] 0x%x\n", sdata));
    664 	/* temp is given in deg. C, we convert to uK */
    665 	sensor->cur.data_us = sdata * 1000000 + 273150000;
    666 }
    667 
    668 static void
    669 generic_svolt(sc, sensors, infos)
    670 	struct lm_softc *sc;
    671 	struct envsys_tre_data *sensors;
    672 	struct envsys_basic_info *infos;
    673 {
    674 	int i, sdata;
    675 
    676 	for (i = 0; i < 7; i++) {
    677 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i);
    678 		DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
    679 		/* voltage returned as (mV >> 4), we convert to uVDC */
    680 		sensors[i].cur.data_s = (sdata << 4);
    681 		/* rfact is (factor * 10^4) */
    682 		sensors[i].cur.data_s *= infos[i].rfact;
    683 		/* division by 10 gets us back to uVDC */
    684 		sensors[i].cur.data_s /= 10;
    685 
    686 		/* these two are negative voltages */
    687 		if ( (i == 5) || (i == 6) )
    688 			sensors[i].cur.data_s *= -1;
    689 	}
    690 }
    691 
    692 static void
    693 generic_fanrpm(sc, sensors)
    694 	struct lm_softc *sc;
    695 	struct envsys_tre_data *sensors;
    696 {
    697 	int i, sdata, divisor;
    698 	for (i = 0; i < 3; i++) {
    699 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 8 + i);
    700 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
    701 		if (i == 2)
    702 			divisor = 2;	/* Fixed divisor for FAN3 */
    703 		else if (i == 1)	/* Bits 7 & 6 of VID/FAN  */
    704 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
    705 		else
    706 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
    707 
    708 		if (sdata == 0xff || sdata == 0x00) {
    709 			sensors[i].cur.data_us = 0;
    710 		} else {
    711 			sensors[i].cur.data_us = 1350000 / (sdata << divisor);
    712 		}
    713 	}
    714 }
    715 
    716 /*
    717  * pre:  last read occurred >= 1.5 seconds ago
    718  * post: sensors[] current data are the latest from the chip
    719  */
    720 void
    721 lm_refresh_sensor_data(sc)
    722 	struct lm_softc *sc;
    723 {
    724 	/* Refresh our stored data for every sensor */
    725 	generic_stemp(sc, &sc->sensors[7]);
    726 	generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
    727 	generic_fanrpm(sc, &sc->sensors[8]);
    728 }
    729 
    730 static void
    731 wb_svolt(sc)
    732 	struct lm_softc *sc;
    733 {
    734 	int i, sdata;
    735 	for (i = 0; i < 9; ++i) {
    736 		if (i < 7) {
    737 			sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i);
    738 		} else {
    739 			/* from bank5 */
    740 			(*sc->lm_banksel)(sc, 5);
    741 			sdata = (*sc->lm_readreg)(sc, (i == 7) ?
    742 			    WB_BANK5_5VSB : WB_BANK5_VBAT);
    743 		}
    744 		DPRINTF(("sdata[volt%d] 0x%x\n", i, sdata));
    745 		/* voltage returned as (mV >> 4), we convert to uV */
    746 		sdata =  sdata << 4;
    747 		/* special case for negative voltages */
    748 		if (i == 5) {
    749 			/*
    750 			 * -12Vdc, assume Winbond recommended values for
    751 			 * resistors
    752 			 */
    753 			sdata = ((sdata * 1000) - (3600 * 805)) / 195;
    754 		} else if (i == 6) {
    755 			/*
    756 			 * -5Vdc, assume Winbond recommended values for
    757 			 * resistors
    758 			 */
    759 			sdata = ((sdata * 1000) - (3600 * 682)) / 318;
    760 		}
    761 		/* rfact is (factor * 10^4) */
    762 		sc->sensors[i].cur.data_s = sdata * sc->info[i].rfact;
    763 		/* division by 10 gets us back to uVDC */
    764 		sc->sensors[i].cur.data_s /= 10;
    765 	}
    766 }
    767 
    768 static void
    769 wb_stemp(sc, sensors, n)
    770 	struct lm_softc *sc;
    771 	struct  envsys_tre_data *sensors;
    772 	int n;
    773 {
    774 	int sdata;
    775 	/* temperatures. Given in dC, we convert to uK */
    776 	sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + 7);
    777 	DPRINTF(("sdata[temp0] 0x%x\n", sdata));
    778 	sensors[0].cur.data_us = sdata * 1000000 + 273150000;
    779 	/* from bank1 */
    780 	if ((*sc->lm_banksel)(sc, 1))
    781 		sensors[1].validflags &= ~ENVSYS_FCURVALID;
    782 	else {
    783 		sdata = (*sc->lm_readreg)(sc, WB_BANK1_T2H) << 1;
    784 		sdata |=  ((*sc->lm_readreg)(sc, WB_BANK1_T2L) & 0x80) >> 7;
    785 		DPRINTF(("sdata[temp1] 0x%x\n", sdata));
    786 		sensors[1].cur.data_us = (sdata * 1000000) / 2 + 273150000;
    787 	}
    788 	if (n < 3)
    789 		return;
    790 	/* from bank2 */
    791 	if ((*sc->lm_banksel)(sc, 2))
    792 		sensors[2].validflags &= ~ENVSYS_FCURVALID;
    793 	else {
    794 		sdata = (*sc->lm_readreg)(sc, WB_BANK2_T3H) << 1;
    795 		sdata |=  ((*sc->lm_readreg)(sc, WB_BANK2_T3L) & 0x80) >> 7;
    796 		DPRINTF(("sdata[temp2] 0x%x\n", sdata));
    797 		sensors[2].cur.data_us = (sdata * 1000000) / 2 + 273150000;
    798 	}
    799 }
    800 
    801 static void
    802 wb781_fanrpm(sc, sensors)
    803 	struct lm_softc *sc;
    804 	struct envsys_tre_data *sensors;
    805 {
    806 	int i, divisor, sdata;
    807 	(*sc->lm_banksel)(sc, 0);
    808 	for (i = 0; i < 3; i++) {
    809 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i + 8);
    810 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
    811 		if (i == 0)
    812 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
    813 		else if (i == 1)
    814 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
    815 		else
    816 			divisor = ((*sc->lm_readreg)(sc, WB_PIN) >> 6) & 0x3;
    817 
    818 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
    819 		if (sdata == 0xff || sdata == 0x00) {
    820 			sensors[i].cur.data_us = 0;
    821 		} else {
    822 			sensors[i].cur.data_us = 1350000 /
    823 			    (sdata << divisor);
    824 		}
    825 	}
    826 }
    827 
    828 static void
    829 wb_fanrpm(sc, sensors)
    830 	struct lm_softc *sc;
    831 	struct envsys_tre_data *sensors;
    832 {
    833 	int i, divisor, sdata;
    834 	(*sc->lm_banksel)(sc, 0);
    835 	for (i = 0; i < 3; i++) {
    836 		sdata = (*sc->lm_readreg)(sc, LMD_SENSORBASE + i + 8);
    837 		DPRINTF(("sdata[fan%d] 0x%x\n", i, sdata));
    838 		if (i == 0)
    839 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 4) & 0x3;
    840 		else if (i == 1)
    841 			divisor = ((*sc->lm_readreg)(sc, LMD_VIDFAN) >> 6) & 0x3;
    842 		else
    843 			divisor = ((*sc->lm_readreg)(sc, WB_PIN) >> 6) & 0x3;
    844 		divisor |= ((*sc->lm_readreg)(sc, WB_BANK0_FANBAT) >> (i + 3)) & 0x4;
    845 
    846 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
    847 		if (sdata == 0xff || sdata == 0x00) {
    848 			sensors[i].cur.data_us = 0;
    849 		} else {
    850 			sensors[i].cur.data_us = 1350000 /
    851 			    (sdata << divisor);
    852 		}
    853 	}
    854 }
    855 
    856 void
    857 wb781_refresh_sensor_data(sc)
    858 	struct lm_softc *sc;
    859 {
    860 	/* Refresh our stored data for every sensor */
    861 	/* we need to reselect bank0 to access common registers */
    862 	(*sc->lm_banksel)(sc, 0);
    863 	generic_svolt(sc, &sc->sensors[0], &sc->info[0]);
    864 	(*sc->lm_banksel)(sc, 0);
    865 	wb_stemp(sc, &sc->sensors[7], 3);
    866 	(*sc->lm_banksel)(sc, 0);
    867 	wb781_fanrpm(sc, &sc->sensors[10]);
    868 }
    869 
    870 void
    871 wb782_refresh_sensor_data(sc)
    872 	struct lm_softc *sc;
    873 {
    874 	/* Refresh our stored data for every sensor */
    875 	wb_svolt(sc);
    876 	wb_stemp(sc, &sc->sensors[9], 3);
    877 	wb_fanrpm(sc, &sc->sensors[12]);
    878 }
    879 
    880 void
    881 wb697_refresh_sensor_data(sc)
    882 	struct lm_softc *sc;
    883 {
    884 	/* Refresh our stored data for every sensor */
    885 	wb_svolt(sc);
    886 	wb_stemp(sc, &sc->sensors[9], 2);
    887 	wb_fanrpm(sc, &sc->sensors[11]);
    888 }
    889