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