Home | History | Annotate | Line # | Download | only in ic
nslm7x.c revision 1.5
      1  1.4  thorpej /*	$NetBSD: nslm7x.c,v 1.5 2000/07/27 21:49:22 bouyer Exp $ */
      2  1.1     groo 
      3  1.1     groo /*-
      4  1.1     groo  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1     groo  * All rights reserved.
      6  1.1     groo  *
      7  1.1     groo  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     groo  * by Bill Squier.
      9  1.1     groo  *
     10  1.1     groo  * Redistribution and use in source and binary forms, with or without
     11  1.1     groo  * modification, are permitted provided that the following conditions
     12  1.1     groo  * are met:
     13  1.1     groo  * 1. Redistributions of source code must retain the above copyright
     14  1.1     groo  *    notice, this list of conditions and the following disclaimer.
     15  1.1     groo  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     groo  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     groo  *    documentation and/or other materials provided with the distribution.
     18  1.1     groo  * 3. All advertising materials mentioning features or use of this software
     19  1.1     groo  *    must display the following acknowledgement:
     20  1.1     groo  *        This product includes software developed by the NetBSD
     21  1.1     groo  *        Foundation, Inc. and its contributors.
     22  1.1     groo  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1     groo  *    contributors may be used to endorse or promote products derived
     24  1.1     groo  *    from this software without specific prior written permission.
     25  1.1     groo  *
     26  1.1     groo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1     groo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1     groo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1     groo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1     groo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1     groo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1     groo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1     groo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1     groo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1     groo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1     groo  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1     groo  */
     38  1.1     groo 
     39  1.1     groo #include <sys/param.h>
     40  1.1     groo #include <sys/systm.h>
     41  1.1     groo #include <sys/kernel.h>
     42  1.1     groo #include <sys/proc.h>
     43  1.1     groo #include <sys/device.h>
     44  1.1     groo #include <sys/malloc.h>
     45  1.1     groo #include <sys/errno.h>
     46  1.1     groo #include <sys/queue.h>
     47  1.1     groo #include <sys/lock.h>
     48  1.1     groo #include <sys/ioctl.h>
     49  1.1     groo #include <sys/conf.h>
     50  1.1     groo #include <sys/time.h>
     51  1.1     groo 
     52  1.1     groo #include <machine/bus.h>
     53  1.1     groo 
     54  1.1     groo #include <dev/isa/isareg.h>
     55  1.1     groo #include <dev/isa/isavar.h>
     56  1.1     groo 
     57  1.4  thorpej #include <dev/sysmon/sysmonvar.h>
     58  1.4  thorpej 
     59  1.1     groo #include <dev/ic/nslm7xvar.h>
     60  1.1     groo 
     61  1.1     groo #include <machine/intr.h>
     62  1.1     groo #include <machine/bus.h>
     63  1.1     groo 
     64  1.1     groo #if defined(LMDEBUG)
     65  1.1     groo #define DPRINTF(x)		do { printf x; } while (0)
     66  1.1     groo #else
     67  1.1     groo #define DPRINTF(x)
     68  1.1     groo #endif
     69  1.1     groo 
     70  1.4  thorpej const struct envsys_range lm_ranges[] = {	/* sc->sensors sub-intervals */
     71  1.5   bouyer 					/* for each unit type */
     72  1.1     groo 	{ 7, 7,    ENVSYS_STEMP   },
     73  1.1     groo 	{ 8, 10,   ENVSYS_SFANRPM },
     74  1.1     groo 	{ 1, 0,    ENVSYS_SVOLTS_AC },	/* None */
     75  1.1     groo 	{ 0, 6,    ENVSYS_SVOLTS_DC },
     76  1.1     groo 	{ 1, 0,    ENVSYS_SOHMS },	/* None */
     77  1.1     groo 	{ 1, 0,    ENVSYS_SWATTS },	/* None */
     78  1.1     groo 	{ 1, 0,    ENVSYS_SAMPS }	/* None */
     79  1.1     groo };
     80  1.1     groo 
     81  1.5   bouyer 
     82  1.1     groo u_int8_t lm_readreg __P((struct lm_softc *, int));
     83  1.1     groo void lm_writereg __P((struct lm_softc *, int, int));
     84  1.5   bouyer 
     85  1.5   bouyer int lm_match __P((struct lm_softc *));
     86  1.1     groo void lm_refresh_sensor_data __P((struct lm_softc *));
     87  1.5   bouyer 
     88  1.5   bouyer int wb_match __P((struct lm_softc *));
     89  1.5   bouyer void wb_refresh_sensor_data __P((struct lm_softc *));
     90  1.5   bouyer 
     91  1.5   bouyer int def_match __P((struct lm_softc *));
     92  1.5   bouyer void lm_common_match __P((struct lm_softc *));
     93  1.5   bouyer 
     94  1.4  thorpej int lm_gtredata __P((struct sysmon_envsys *, struct envsys_tre_data *));
     95  1.4  thorpej int lm_streinfo __P((struct sysmon_envsys *, struct envsys_basic_info *));
     96  1.5   bouyer int wb_streinfo __P((struct sysmon_envsys *, struct envsys_basic_info *));
     97  1.5   bouyer 
     98  1.5   bouyer struct lm_chip {
     99  1.5   bouyer 	int (*chip_match) __P((struct lm_softc *));
    100  1.5   bouyer };
    101  1.5   bouyer 
    102  1.5   bouyer struct lm_chip lm_chips[] = {
    103  1.5   bouyer 	{ lm_match},
    104  1.5   bouyer 	{ wb_match},
    105  1.5   bouyer 	{ def_match} /* Must be last */
    106  1.5   bouyer };
    107  1.5   bouyer 
    108  1.1     groo 
    109  1.1     groo u_int8_t
    110  1.1     groo lm_readreg(sc, reg)
    111  1.1     groo 	struct lm_softc *sc;
    112  1.1     groo 	int reg;
    113  1.1     groo {
    114  1.1     groo 	bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
    115  1.1     groo 	return (bus_space_read_1(sc->lm_iot, sc->lm_ioh, LMC_DATA));
    116  1.1     groo }
    117  1.1     groo 
    118  1.1     groo void
    119  1.1     groo lm_writereg(sc, reg, val)
    120  1.1     groo 	struct lm_softc *sc;
    121  1.1     groo 	int reg;
    122  1.1     groo 	int val;
    123  1.1     groo {
    124  1.1     groo 	bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
    125  1.1     groo 	bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_DATA, val);
    126  1.1     groo }
    127  1.1     groo 
    128  1.1     groo 
    129  1.1     groo /*
    130  1.2     groo  * bus independent probe
    131  1.2     groo  */
    132  1.2     groo int
    133  1.2     groo lm_probe(iot, ioh)
    134  1.2     groo 	bus_space_tag_t iot;
    135  1.2     groo 	bus_space_handle_t ioh;
    136  1.2     groo {
    137  1.2     groo 	u_int8_t cr;
    138  1.2     groo 	int rv;
    139  1.2     groo 
    140  1.2     groo 	/* Check for some power-on defaults */
    141  1.2     groo 	bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
    142  1.2     groo 
    143  1.2     groo 	/* Perform LM78 reset */
    144  1.2     groo 	bus_space_write_1(iot, ioh, LMC_DATA, 0x80);
    145  1.2     groo 
    146  1.2     groo 	/* XXX - Why do I have to reselect the register? */
    147  1.2     groo 	bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
    148  1.2     groo 	cr = bus_space_read_1(iot, ioh, LMC_DATA);
    149  1.2     groo 
    150  1.2     groo 	/* XXX - spec says *only* 0x08! */
    151  1.2     groo 	if ((cr == 0x08) || (cr == 0x01))
    152  1.2     groo 		rv = 1;
    153  1.2     groo 	else
    154  1.2     groo 		rv = 0;
    155  1.2     groo 
    156  1.2     groo 	DPRINTF(("lm: rv = %d, cr = %x\n", rv, cr));
    157  1.2     groo 
    158  1.2     groo 	return (rv);
    159  1.2     groo }
    160  1.2     groo 
    161  1.2     groo 
    162  1.2     groo /*
    163  1.1     groo  * pre:  lmsc contains valid busspace tag and handle
    164  1.1     groo  */
    165  1.1     groo void
    166  1.1     groo lm_attach(lmsc)
    167  1.1     groo 	struct lm_softc *lmsc;
    168  1.1     groo {
    169  1.1     groo 	int i;
    170  1.1     groo 
    171  1.5   bouyer 	for (i = 0; i < sizeof(lm_chips) / sizeof(lm_chips[0]); i++)
    172  1.5   bouyer 		if (lm_chips[i].chip_match(lmsc))
    173  1.5   bouyer 			break;
    174  1.1     groo 
    175  1.1     groo 	/* Start the monitoring loop */
    176  1.1     groo 	lm_writereg(lmsc, LMD_CONFIG, 0x01);
    177  1.1     groo 
    178  1.1     groo 	/* Indicate we have never read the registers */
    179  1.1     groo 	timerclear(&lmsc->lastread);
    180  1.1     groo 
    181  1.1     groo 	/* Initialize sensors */
    182  1.5   bouyer 	for (i = 0; i < lmsc->numsensors; ++i) {
    183  1.1     groo 		lmsc->sensors[i].sensor = lmsc->info[i].sensor = i;
    184  1.1     groo 		lmsc->sensors[i].validflags = (ENVSYS_FVALID|ENVSYS_FCURVALID);
    185  1.1     groo 		lmsc->info[i].validflags = ENVSYS_FVALID;
    186  1.1     groo 		lmsc->sensors[i].warnflags = ENVSYS_WARN_OK;
    187  1.1     groo 	}
    188  1.4  thorpej 	/*
    189  1.4  thorpej 	 * Hook into the System Monitor.
    190  1.4  thorpej 	 */
    191  1.4  thorpej 	lmsc->sc_sysmon.sme_ranges = lm_ranges;
    192  1.4  thorpej 	lmsc->sc_sysmon.sme_sensor_info = lmsc->info;
    193  1.4  thorpej 	lmsc->sc_sysmon.sme_sensor_data = lmsc->sensors;
    194  1.4  thorpej 	lmsc->sc_sysmon.sme_cookie = lmsc;
    195  1.4  thorpej 
    196  1.4  thorpej 	lmsc->sc_sysmon.sme_gtredata = lm_gtredata;
    197  1.5   bouyer 	/* sme_streinfo set in chip-specific attach */
    198  1.4  thorpej 
    199  1.5   bouyer 	lmsc->sc_sysmon.sme_nsensors = lmsc->numsensors;
    200  1.4  thorpej 	lmsc->sc_sysmon.sme_envsys_version = 1000;
    201  1.4  thorpej 
    202  1.4  thorpej 	if (sysmon_envsys_register(&lmsc->sc_sysmon))
    203  1.4  thorpej 		printf("%s: unable to register with sysmon\n",
    204  1.4  thorpej 		    lmsc->sc_dev.dv_xname);
    205  1.1     groo }
    206  1.1     groo 
    207  1.5   bouyer int
    208  1.5   bouyer lm_match(sc)
    209  1.5   bouyer 	struct lm_softc *sc;
    210  1.5   bouyer {
    211  1.5   bouyer 	int i;
    212  1.5   bouyer 
    213  1.5   bouyer 	/* See if we have an LM78 or LM79 */
    214  1.5   bouyer 	i = lm_readreg(sc, LMD_CHIPID) & LM_ID_MASK;
    215  1.5   bouyer 	switch(i) {
    216  1.5   bouyer 	case LM_ID_LM78:
    217  1.5   bouyer 		printf(": LM78\n");
    218  1.5   bouyer 		break;
    219  1.5   bouyer 	case LM_ID_LM78J:
    220  1.5   bouyer 		printf(": LM78J\n");
    221  1.5   bouyer 		break;
    222  1.5   bouyer 	case LM_ID_LM79:
    223  1.5   bouyer 		printf(": LM79\n");
    224  1.5   bouyer 		break;
    225  1.5   bouyer 	default:
    226  1.5   bouyer 		return 0;
    227  1.5   bouyer 	}
    228  1.5   bouyer 	lm_common_match(sc);
    229  1.5   bouyer 	return 1;
    230  1.5   bouyer }
    231  1.1     groo 
    232  1.1     groo int
    233  1.5   bouyer def_match(sc)
    234  1.5   bouyer 	struct lm_softc *sc;
    235  1.5   bouyer {
    236  1.5   bouyer 	int i;
    237  1.5   bouyer 
    238  1.5   bouyer 	i = lm_readreg(sc, LMD_CHIPID) & LM_ID_MASK;
    239  1.5   bouyer 	printf(": Unknow chip (ID %d)\n", i);
    240  1.5   bouyer 	lm_common_match(sc);
    241  1.5   bouyer 	return 1;
    242  1.5   bouyer }
    243  1.5   bouyer 
    244  1.5   bouyer void
    245  1.5   bouyer lm_common_match(sc)
    246  1.5   bouyer 	struct lm_softc *sc;
    247  1.1     groo {
    248  1.5   bouyer 	int i;
    249  1.5   bouyer 	sc->numsensors = LM_NUM_SENSORS;
    250  1.5   bouyer 	sc->refresh_sensor_data = lm_refresh_sensor_data;
    251  1.5   bouyer 
    252  1.5   bouyer 	for (i = 0; i < 7; ++i) {
    253  1.5   bouyer 		sc->sensors[i].units = sc->info[i].units =
    254  1.5   bouyer 		    ENVSYS_SVOLTS_DC;
    255  1.5   bouyer 		sprintf(sc->info[i].desc, "IN %d", i);
    256  1.5   bouyer 	}
    257  1.5   bouyer 
    258  1.5   bouyer 	/* default correction factors for resistors on higher voltage inputs */
    259  1.5   bouyer 	sc->info[0].rfact = sc->info[1].rfact =
    260  1.5   bouyer 	    sc->info[2].rfact = 10000;
    261  1.5   bouyer 	sc->info[3].rfact = (int)(( 90.9 / 60.4) * 10000);
    262  1.5   bouyer 	sc->info[4].rfact = (int)(( 38.0 / 10.0) * 10000);
    263  1.5   bouyer 	sc->info[5].rfact = (int)((210.0 / 60.4) * 10000);
    264  1.5   bouyer 	sc->info[6].rfact = (int)(( 90.9 / 60.4) * 10000);
    265  1.5   bouyer 
    266  1.5   bouyer 	sc->sensors[7].units = ENVSYS_STEMP;
    267  1.5   bouyer 	strcpy(sc->info[7].desc, "Temp");
    268  1.5   bouyer 
    269  1.5   bouyer 	for (i = 8; i < 11; ++i) {
    270  1.5   bouyer 		sc->sensors[i].units = sc->info[i].units = ENVSYS_SFANRPM;
    271  1.5   bouyer 		sprintf(sc->info[i].desc, "Fan %d", i - 7);
    272  1.4  thorpej 	}
    273  1.5   bouyer 	sc->sc_sysmon.sme_streinfo = lm_streinfo;
    274  1.5   bouyer }
    275  1.1     groo 
    276  1.5   bouyer int
    277  1.5   bouyer wb_match(sc)
    278  1.5   bouyer 	struct lm_softc *sc;
    279  1.5   bouyer {
    280  1.5   bouyer 	int i, j;
    281  1.1     groo 
    282  1.5   bouyer 	/* See if we have a winbond */
    283  1.5   bouyer 	i = lm_readreg(sc, LMD_CHIPID) & LM_ID_MASK;
    284  1.5   bouyer 	lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_HBAC);
    285  1.5   bouyer 	j = lm_readreg(sc, WB_VENDID) << 8;
    286  1.5   bouyer 	lm_writereg(sc, WB_BANKSEL, 0);
    287  1.5   bouyer 	j |= lm_readreg(sc, WB_VENDID);
    288  1.5   bouyer 	DPRINTF(("winbond vend id %d\n", j));
    289  1.5   bouyer 	if (j != WB_VENDID_WINBOND)
    290  1.5   bouyer 		return 0;
    291  1.5   bouyer 	printf(": W83627HF (device ID %d)\n", i);
    292  1.5   bouyer 	sc->numsensors = WB_NUM_SENSORS;
    293  1.5   bouyer 	sc->refresh_sensor_data = wb_refresh_sensor_data;
    294  1.5   bouyer 
    295  1.5   bouyer 	sc->sensors[0].units = sc->info[0].units = ENVSYS_SVOLTS_DC;
    296  1.5   bouyer 	sprintf(sc->info[0].desc, "VCORE A");
    297  1.5   bouyer 	sc->info[0].rfact = 10000;
    298  1.5   bouyer 	sc->sensors[1].units = sc->info[1].units = ENVSYS_SVOLTS_DC;
    299  1.5   bouyer 	sprintf(sc->info[1].desc, "VCORE B");
    300  1.5   bouyer 	sc->info[1].rfact = 10000;
    301  1.5   bouyer 	sc->sensors[2].units = sc->info[2].units = ENVSYS_SVOLTS_DC;
    302  1.5   bouyer 	sprintf(sc->info[2].desc, "+3.3V");
    303  1.5   bouyer 	sc->info[2].rfact = 10000;
    304  1.5   bouyer 	sc->sensors[3].units = sc->info[3].units = ENVSYS_SVOLTS_DC;
    305  1.5   bouyer 	sprintf(sc->info[3].desc, "+5V");
    306  1.5   bouyer 	sc->info[3].rfact = 16778;
    307  1.5   bouyer 	sc->sensors[4].units = sc->info[4].units = ENVSYS_SVOLTS_DC;
    308  1.5   bouyer 	sprintf(sc->info[4].desc, "+12V");
    309  1.5   bouyer 	sc->info[4].rfact = 38000;
    310  1.5   bouyer 	sc->sensors[5].units = sc->info[5].units = ENVSYS_SVOLTS_DC;
    311  1.5   bouyer 	sprintf(sc->info[5].desc, "-12V");
    312  1.5   bouyer 	sc->info[5].rfact = 10000;
    313  1.5   bouyer 	sc->sensors[6].units = sc->info[6].units = ENVSYS_SVOLTS_DC;
    314  1.5   bouyer 	sprintf(sc->info[6].desc, "-5V");
    315  1.5   bouyer 	sc->info[6].rfact = 10000;
    316  1.5   bouyer 	sc->sensors[7].units = sc->info[7].units = ENVSYS_SVOLTS_DC;
    317  1.5   bouyer 	sprintf(sc->info[7].desc, "+5VSB");
    318  1.5   bouyer 	sc->info[7].rfact = 15151;
    319  1.5   bouyer 	sc->sensors[8].units = sc->info[8].units = ENVSYS_SVOLTS_DC;
    320  1.5   bouyer 	sprintf(sc->info[8].desc, "VBAT");
    321  1.5   bouyer 	sc->info[8].rfact = 10000;
    322  1.5   bouyer 
    323  1.5   bouyer 	sc->sensors[9].units = ENVSYS_STEMP;
    324  1.5   bouyer 	strcpy(sc->info[9].desc, "Temp 1");
    325  1.5   bouyer 	sc->sensors[10].units = ENVSYS_STEMP;
    326  1.5   bouyer 	strcpy(sc->info[10].desc, "Temp 2");
    327  1.5   bouyer 	sc->sensors[11].units = ENVSYS_STEMP;
    328  1.5   bouyer 	strcpy(sc->info[11].desc, "Temp 3");
    329  1.5   bouyer 
    330  1.5   bouyer 	for (i = 12; i < 15; ++i) {
    331  1.5   bouyer 		sc->sensors[i].units = sc->info[i].units = ENVSYS_SFANRPM;
    332  1.5   bouyer 		sprintf(sc->info[i].desc, "Fan %d", i - 11);
    333  1.5   bouyer 	}
    334  1.5   bouyer 	sc->sc_sysmon.sme_streinfo = wb_streinfo;
    335  1.5   bouyer 	return 1;
    336  1.1     groo }
    337  1.1     groo 
    338  1.5   bouyer int
    339  1.5   bouyer lm_gtredata(sme, tred)
    340  1.5   bouyer 	 struct sysmon_envsys *sme;
    341  1.5   bouyer 	 struct envsys_tre_data *tred;
    342  1.5   bouyer {
    343  1.5   bouyer 	 static const struct timeval onepointfive = { 1, 500000 };
    344  1.5   bouyer 	 struct timeval t;
    345  1.5   bouyer 	 struct lm_softc *sc = sme->sme_cookie;
    346  1.5   bouyer 	 int i, s;
    347  1.5   bouyer 
    348  1.5   bouyer 	 /* read new values at most once every 1.5 seconds */
    349  1.5   bouyer 	 timeradd(&sc->lastread, &onepointfive, &t);
    350  1.5   bouyer 	 s = splclock();
    351  1.5   bouyer 	 i = timercmp(&mono_time, &t, >);
    352  1.5   bouyer 	 if (i) {
    353  1.5   bouyer 		  sc->lastread.tv_sec  = mono_time.tv_sec;
    354  1.5   bouyer 		  sc->lastread.tv_usec = mono_time.tv_usec;
    355  1.5   bouyer 	 }
    356  1.5   bouyer 	 splx(s);
    357  1.5   bouyer 
    358  1.5   bouyer 	 if (i)
    359  1.5   bouyer 		  sc->refresh_sensor_data(sc);
    360  1.5   bouyer 
    361  1.5   bouyer 	 *tred = sc->sensors[tred->sensor];
    362  1.5   bouyer 
    363  1.5   bouyer 	 return (0);
    364  1.5   bouyer }
    365  1.1     groo 
    366  1.1     groo int
    367  1.4  thorpej lm_streinfo(sme, binfo)
    368  1.5   bouyer 	 struct sysmon_envsys *sme;
    369  1.5   bouyer 	 struct envsys_basic_info *binfo;
    370  1.1     groo {
    371  1.5   bouyer 	 struct lm_softc *sc = sme->sme_cookie;
    372  1.5   bouyer 	 int divisor;
    373  1.5   bouyer 	 u_int8_t sdata;
    374  1.5   bouyer 
    375  1.5   bouyer 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
    376  1.5   bouyer 		  sc->info[binfo->sensor].rfact = binfo->rfact;
    377  1.5   bouyer 	 else {
    378  1.5   bouyer 		  /* FAN1 and FAN2 can have divisors set, but not FAN3 */
    379  1.5   bouyer 		  if ((sc->info[binfo->sensor].units == ENVSYS_SFANRPM)
    380  1.5   bouyer 		      && (binfo->sensor != 10)) {
    381  1.5   bouyer 
    382  1.5   bouyer 				if (binfo->rpms == 0) {
    383  1.5   bouyer 					 binfo->validflags = 0;
    384  1.5   bouyer 					 return (0);
    385  1.5   bouyer 				}
    386  1.5   bouyer 
    387  1.5   bouyer 				/* 153 is the nominal FAN speed value */
    388  1.5   bouyer 				divisor = 1350000 / (binfo->rpms * 153);
    389  1.5   bouyer 
    390  1.5   bouyer 				/* ...but we need lg(divisor) */
    391  1.5   bouyer 				if (divisor <= 1)
    392  1.5   bouyer 					 divisor = 0;
    393  1.5   bouyer 				else if (divisor <= 2)
    394  1.5   bouyer 					 divisor = 1;
    395  1.5   bouyer 				else if (divisor <= 4)
    396  1.5   bouyer 					 divisor = 2;
    397  1.5   bouyer 				else
    398  1.5   bouyer 					 divisor = 3;
    399  1.5   bouyer 
    400  1.5   bouyer 				/*
    401  1.5   bouyer 				 * FAN1 div is in bits <5:4>, FAN2 div is
    402  1.5   bouyer 				 * in <7:6>
    403  1.5   bouyer 				 */
    404  1.5   bouyer 				sdata = lm_readreg(sc, LMD_VIDFAN);
    405  1.5   bouyer 				if ( binfo->sensor == 8 ) {  /* FAN1 */
    406  1.5   bouyer 					 divisor <<= 4;
    407  1.5   bouyer 					 sdata = (sdata & 0xCF) | divisor;
    408  1.5   bouyer 				} else { /* FAN2 */
    409  1.5   bouyer 					 divisor <<= 6;
    410  1.5   bouyer 					 sdata = (sdata & 0x3F) | divisor;
    411  1.5   bouyer 				}
    412  1.5   bouyer 
    413  1.5   bouyer 				lm_writereg(sc, LMD_VIDFAN, sdata);
    414  1.5   bouyer 		  }
    415  1.5   bouyer 
    416  1.5   bouyer 		  memcpy(sc->info[binfo->sensor].desc, binfo->desc,
    417  1.5   bouyer 			sizeof(sc->info[binfo->sensor].desc));
    418  1.5   bouyer 		  sc->info[binfo->sensor].desc[
    419  1.5   bouyer 			sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
    420  1.5   bouyer 
    421  1.5   bouyer 		  binfo->validflags = ENVSYS_FVALID;
    422  1.5   bouyer 	 }
    423  1.5   bouyer 	 return (0);
    424  1.5   bouyer }
    425  1.5   bouyer 
    426  1.5   bouyer int
    427  1.5   bouyer wb_streinfo(sme, binfo)
    428  1.5   bouyer 	 struct sysmon_envsys *sme;
    429  1.5   bouyer 	 struct envsys_basic_info *binfo;
    430  1.5   bouyer {
    431  1.5   bouyer 	 struct lm_softc *sc = sme->sme_cookie;
    432  1.5   bouyer 	 int divisor;
    433  1.5   bouyer 	 u_int8_t sdata;
    434  1.5   bouyer 	 int i;
    435  1.5   bouyer 
    436  1.5   bouyer 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
    437  1.5   bouyer 		  sc->info[binfo->sensor].rfact = binfo->rfact;
    438  1.5   bouyer 	 else {
    439  1.5   bouyer 	 	if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
    440  1.4  thorpej 
    441  1.4  thorpej 			if (binfo->rpms == 0) {
    442  1.4  thorpej 				binfo->validflags = 0;
    443  1.4  thorpej 				return (0);
    444  1.1     groo 			}
    445  1.1     groo 
    446  1.4  thorpej 			/* 153 is the nominal FAN speed value */
    447  1.4  thorpej 			divisor = 1350000 / (binfo->rpms * 153);
    448  1.1     groo 
    449  1.4  thorpej 			/* ...but we need lg(divisor) */
    450  1.5   bouyer 			for (i = 0; i < 7; i++) {
    451  1.5   bouyer 				if (divisor <= (1 << i))
    452  1.5   bouyer 				 	break;
    453  1.5   bouyer 			}
    454  1.5   bouyer 			divisor = i;
    455  1.4  thorpej 
    456  1.5   bouyer 			if (binfo->sensor == 12 || binfo->sensor == 13) {
    457  1.5   bouyer 				/*
    458  1.5   bouyer 				 * FAN1 div is in bits <5:4>, FAN2 div
    459  1.5   bouyer 				 * is in <7:6>
    460  1.5   bouyer 				 */
    461  1.5   bouyer 				sdata = lm_readreg(sc, LMD_VIDFAN);
    462  1.5   bouyer 				if ( binfo->sensor == 12 ) {  /* FAN1 */
    463  1.5   bouyer 					 sdata = (sdata & 0xCF) |
    464  1.5   bouyer 					     ((divisor & 0x3) << 4);
    465  1.5   bouyer 				} else { /* FAN2 */
    466  1.5   bouyer 					 sdata = (sdata & 0x3F) |
    467  1.5   bouyer 					     ((divisor & 0x3) << 6);
    468  1.5   bouyer 				}
    469  1.5   bouyer 				lm_writereg(sc, LMD_VIDFAN, sdata);
    470  1.5   bouyer 			} else {
    471  1.5   bouyer 				/* FAN3 is in WB_PIN <7:6> */
    472  1.5   bouyer 				sdata = lm_readreg(sc, WB_PIN);
    473  1.5   bouyer 				sdata = (sdata & 0x3F) |
    474  1.5   bouyer 				     ((divisor & 0x3) << 6);
    475  1.5   bouyer 				lm_writereg(sc, LMD_VIDFAN, sdata);
    476  1.1     groo 			}
    477  1.5   bouyer 			/* Bit 2 of divisor is in WB_BANK0_FANBAT */
    478  1.5   bouyer 			lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B0);
    479  1.5   bouyer 			sdata = lm_readreg(sc, WB_BANK0_FANBAT);
    480  1.5   bouyer 			sdata &= ~(0x20 << (binfo->sensor - 12));
    481  1.5   bouyer 			sdata |= (divisor & 0x4) << (binfo->sensor - 9);
    482  1.5   bouyer 			lm_writereg(sc, WB_BANK0_FANBAT, sdata);
    483  1.1     groo 		}
    484  1.1     groo 
    485  1.4  thorpej 		memcpy(sc->info[binfo->sensor].desc, binfo->desc,
    486  1.4  thorpej 		    sizeof(sc->info[binfo->sensor].desc));
    487  1.4  thorpej 		sc->info[binfo->sensor].desc[
    488  1.4  thorpej 		    sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
    489  1.1     groo 
    490  1.4  thorpej 		binfo->validflags = ENVSYS_FVALID;
    491  1.1     groo 	}
    492  1.4  thorpej 	return (0);
    493  1.1     groo }
    494  1.1     groo 
    495  1.1     groo /*
    496  1.1     groo  * pre:  last read occured >= 1.5 seconds ago
    497  1.1     groo  * post: sensors[] current data are the latest from the chip
    498  1.1     groo  */
    499  1.1     groo void
    500  1.1     groo lm_refresh_sensor_data(sc)
    501  1.1     groo 	struct lm_softc *sc;
    502  1.1     groo {
    503  1.5   bouyer 	int sdata;
    504  1.1     groo 	int i, divisor;
    505  1.1     groo 
    506  1.1     groo 	/* Refresh our stored data for every sensor */
    507  1.1     groo 	for (i = 0; i < LM_NUM_SENSORS; ++i) {
    508  1.1     groo 		sdata = lm_readreg(sc, LMD_SENSORBASE + i);
    509  1.5   bouyer 
    510  1.1     groo 		switch (sc->sensors[i].units) {
    511  1.1     groo 		case ENVSYS_STEMP:
    512  1.1     groo 			/* temp is given in deg. C, we convert to uK */
    513  1.1     groo 			sc->sensors[i].cur.data_us = sdata * 1000000 +
    514  1.1     groo 			    273150000;
    515  1.1     groo 			break;
    516  1.5   bouyer 
    517  1.1     groo 		case ENVSYS_SVOLTS_DC:
    518  1.1     groo 			/* voltage returned as (mV >> 4), we convert to uVDC */
    519  1.3     groo 			sc->sensors[i].cur.data_s = (sdata << 4);
    520  1.3     groo 			/* rfact is (factor * 10^4) */
    521  1.3     groo 			sc->sensors[i].cur.data_s *= sc->info[i].rfact;
    522  1.3     groo 			/* division by 10 gets us back to uVDC */
    523  1.3     groo 			sc->sensors[i].cur.data_s /= 10;
    524  1.5   bouyer 
    525  1.1     groo 			/* these two are negative voltages */
    526  1.1     groo 			if ( (i == 5) || (i == 6) )
    527  1.1     groo 				sc->sensors[i].cur.data_s *= -1;
    528  1.3     groo 
    529  1.1     groo 			break;
    530  1.5   bouyer 
    531  1.1     groo 		case ENVSYS_SFANRPM:
    532  1.1     groo 			if (i == 10)
    533  1.1     groo 				divisor = 2;	/* Fixed divisor for FAN3 */
    534  1.1     groo 			else if (i == 9)	/* Bits 7 & 6 of VID/FAN  */
    535  1.1     groo 				divisor = (lm_readreg(sc, LMD_VIDFAN) >> 6) &
    536  1.1     groo 				    0x3;
    537  1.1     groo 			else
    538  1.1     groo 				divisor = (lm_readreg(sc, LMD_VIDFAN) >> 4) &
    539  1.1     groo 				    0x3;
    540  1.5   bouyer 
    541  1.5   bouyer 			if (sdata == 0xff || sdata == 0x00) {
    542  1.5   bouyer 				sc->sensors[i].cur.data_us = 0;
    543  1.5   bouyer 			} else {
    544  1.5   bouyer 				sc->sensors[i].cur.data_us = 1350000 /
    545  1.5   bouyer 				    (sdata << divisor);
    546  1.5   bouyer 			}
    547  1.1     groo 			break;
    548  1.5   bouyer 
    549  1.1     groo 		default:
    550  1.1     groo 			/* XXX - debug log something? */
    551  1.1     groo 			sc->sensors[i].validflags = 0;
    552  1.5   bouyer 
    553  1.1     groo 			break;
    554  1.5   bouyer 		}
    555  1.5   bouyer 	}
    556  1.5   bouyer }
    557  1.5   bouyer 
    558  1.5   bouyer void
    559  1.5   bouyer wb_refresh_sensor_data(sc)
    560  1.5   bouyer 	struct lm_softc *sc;
    561  1.5   bouyer {
    562  1.5   bouyer 	int sdata;
    563  1.5   bouyer 	int i, divisor;
    564  1.5   bouyer 
    565  1.5   bouyer 	/* Refresh our stored data for every sensor */
    566  1.5   bouyer 	/* first voltage sensors */
    567  1.5   bouyer 	for (i = 0; i < 9; ++i) {
    568  1.5   bouyer 		if (i < 7) {
    569  1.5   bouyer 			sdata = lm_readreg(sc, LMD_SENSORBASE + i);
    570  1.5   bouyer 		} else {
    571  1.5   bouyer 			/* from bank5 */
    572  1.5   bouyer 			lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B5);
    573  1.5   bouyer 			sdata = lm_readreg(sc, (i == 7) ?
    574  1.5   bouyer 			    WB_BANK5_5VSB : WB_BANK5_VBAT);
    575  1.5   bouyer 		}
    576  1.5   bouyer 		DPRINTF(("sdata[%d] 0x%x\n", i, sdata));
    577  1.5   bouyer 		/* voltage returned as (mV >> 4), we convert to uV */
    578  1.5   bouyer 		sdata =  sdata << 4;
    579  1.5   bouyer 		/* special case for negative voltages */
    580  1.5   bouyer 		if (i == 5) {
    581  1.5   bouyer 			/*
    582  1.5   bouyer 			 * -12Vdc, assume Winbond recommended values for
    583  1.5   bouyer 			 * resistors
    584  1.5   bouyer 			 */
    585  1.5   bouyer 			sdata = ((sdata * 1000) - (3600 * 805)) / 195;
    586  1.5   bouyer 		} else if (i == 6) {
    587  1.5   bouyer 			/*
    588  1.5   bouyer 			 * -5Vdc, assume Winbond recommended values for
    589  1.5   bouyer 			 * resistors
    590  1.5   bouyer 			 */
    591  1.5   bouyer 			sdata = ((sdata * 1000) - (3600 * 682)) / 318;
    592  1.5   bouyer 		}
    593  1.5   bouyer 		/* rfact is (factor * 10^4) */
    594  1.5   bouyer 		sc->sensors[i].cur.data_s = sdata * sc->info[i].rfact;
    595  1.5   bouyer 		/* division by 10 gets us back to uVDC */
    596  1.5   bouyer 		sc->sensors[i].cur.data_s /= 10;
    597  1.5   bouyer 	}
    598  1.5   bouyer 	/* temperatures. Given in dC, we convert to uK */
    599  1.5   bouyer 	sdata = lm_readreg(sc, LMD_SENSORBASE + 7);
    600  1.5   bouyer 	DPRINTF(("sdata[%d] 0x%x\n", 9, sdata));
    601  1.5   bouyer 	sc->sensors[9].cur.data_us = sdata * 1000000 + 273150000;
    602  1.5   bouyer 	/* from bank1 */
    603  1.5   bouyer 	lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B1);
    604  1.5   bouyer 	sdata = lm_readreg(sc, WB_BANK1_T2H) << 1;
    605  1.5   bouyer 	sdata |=  (lm_readreg(sc, WB_BANK1_T2L) & 0x80) >> 7;
    606  1.5   bouyer 	DPRINTF(("sdata[%d] 0x%x\n", 10, sdata));
    607  1.5   bouyer 	sc->sensors[10].cur.data_us = (sdata * 1000000) / 2 + 273150000;
    608  1.5   bouyer 	/* from bank2 */
    609  1.5   bouyer 	lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B2);
    610  1.5   bouyer 	sdata = lm_readreg(sc, WB_BANK2_T3H) << 1;
    611  1.5   bouyer 	sdata |=  (lm_readreg(sc, WB_BANK2_T3L) & 0x80) >> 7;
    612  1.5   bouyer 	DPRINTF(("sdata[%d] 0x%x\n", 11, sdata));
    613  1.5   bouyer 	sc->sensors[11].cur.data_us = (sdata * 1000000) / 2 + 273150000;
    614  1.5   bouyer 
    615  1.5   bouyer 	/* Fans */
    616  1.5   bouyer 	lm_writereg(sc, WB_BANKSEL, WB_BANKSEL_B0);
    617  1.5   bouyer 	for (i = 12; i < 15; i++) {
    618  1.5   bouyer 		sdata = lm_readreg(sc, LMD_SENSORBASE + i - 4);
    619  1.5   bouyer 		if (i == 12)
    620  1.5   bouyer 			divisor = (lm_readreg(sc, LMD_VIDFAN) >> 4) & 0x3;
    621  1.5   bouyer 		else if (i == 13)
    622  1.5   bouyer 			divisor = (lm_readreg(sc, LMD_VIDFAN) >> 6) & 0x3;
    623  1.5   bouyer 		else
    624  1.5   bouyer 			divisor = (lm_readreg(sc, WB_PIN) >> 6) & 0x3;
    625  1.5   bouyer 		divisor |= (lm_readreg(sc, WB_BANK0_FANBAT) >> (i - 9)) & 0x4;
    626  1.5   bouyer 
    627  1.5   bouyer 		DPRINTF(("sdata[%d] 0x%x div 0x%x\n", i, sdata, divisor));
    628  1.5   bouyer 		if (sdata == 0xff || sdata == 0x00) {
    629  1.5   bouyer 			sc->sensors[i].cur.data_us = 0;
    630  1.5   bouyer 		} else {
    631  1.5   bouyer 			sc->sensors[i].cur.data_us = 1350000 /
    632  1.5   bouyer 			    (sdata << divisor);
    633  1.1     groo 		}
    634  1.1     groo 	}
    635  1.1     groo }
    636