Home | History | Annotate | Line # | Download | only in isa
itesio_isa.c revision 1.6.4.3
      1  1.6.4.3  yamt /*	$NetBSD: itesio_isa.c,v 1.6.4.3 2008/01/21 09:43:19 yamt Exp $ */
      2  1.6.4.2  yamt /*	Derived from $OpenBSD: it.c,v 1.19 2006/04/10 00:57:54 deraadt Exp $	*/
      3  1.6.4.2  yamt 
      4  1.6.4.2  yamt /*
      5  1.6.4.2  yamt  * Copyright (c) 2006-2007 Juan Romero Pardines <juan (at) xtrarom.org>
      6  1.6.4.2  yamt  * Copyright (c) 2003 Julien Bordet <zejames (at) greyhats.org>
      7  1.6.4.2  yamt  * All rights reserved.
      8  1.6.4.2  yamt  *
      9  1.6.4.2  yamt  * Redistribution and use in source and binary forms, with or without
     10  1.6.4.2  yamt  * modification, are permitted provided that the following conditions
     11  1.6.4.2  yamt  * are met:
     12  1.6.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     13  1.6.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     14  1.6.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.6.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     16  1.6.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     17  1.6.4.2  yamt  *
     18  1.6.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.6.4.2  yamt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITD TO, THE IMPLIED WARRANTIES
     20  1.6.4.2  yamt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.6.4.2  yamt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.6.4.2  yamt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.6.4.2  yamt  * NOT LIMITD TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.6.4.2  yamt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.6.4.2  yamt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.6.4.2  yamt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.6.4.2  yamt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.6.4.2  yamt  */
     29  1.6.4.2  yamt 
     30  1.6.4.2  yamt /*
     31  1.6.4.2  yamt  * Driver for the iTE IT87xxF Super I/O. Currently supporting
     32  1.6.4.3  yamt  * the Environmental Controller to monitor the sensors and the
     33  1.6.4.3  yamt  * Watchdog Timer.
     34  1.6.4.2  yamt  */
     35  1.6.4.2  yamt 
     36  1.6.4.2  yamt #include <sys/cdefs.h>
     37  1.6.4.3  yamt __KERNEL_RCSID(0, "$NetBSD: itesio_isa.c,v 1.6.4.3 2008/01/21 09:43:19 yamt Exp $");
     38  1.6.4.2  yamt 
     39  1.6.4.2  yamt #include <sys/param.h>
     40  1.6.4.2  yamt #include <sys/kernel.h>
     41  1.6.4.2  yamt #include <sys/device.h>
     42  1.6.4.2  yamt 
     43  1.6.4.2  yamt #include <sys/bus.h>
     44  1.6.4.2  yamt 
     45  1.6.4.2  yamt #include <dev/isa/isareg.h>
     46  1.6.4.2  yamt #include <dev/isa/isavar.h>
     47  1.6.4.2  yamt 
     48  1.6.4.2  yamt #include <dev/sysmon/sysmonvar.h>
     49  1.6.4.2  yamt 
     50  1.6.4.2  yamt #include <dev/isa/itesio_isavar.h>
     51  1.6.4.2  yamt 
     52  1.6.4.2  yamt #define IT_VOLTSTART_IDX 	3 	/* voltage start index */
     53  1.6.4.2  yamt #define IT_FANSTART_IDX 	12 	/* fan start index */
     54  1.6.4.2  yamt 
     55  1.6.4.2  yamt #if defined(ITESIO_DEBUG)
     56  1.6.4.2  yamt #define DPRINTF(x)		do { printf x; } while (0)
     57  1.6.4.2  yamt #else
     58  1.6.4.2  yamt #define DPRINTF(x)
     59  1.6.4.2  yamt #endif
     60  1.6.4.2  yamt 
     61  1.6.4.2  yamt /*
     62  1.6.4.2  yamt  * IT87-compatible chips can typically measure voltages up to 4.096 V.
     63  1.6.4.2  yamt  * To measure higher voltages the input is attenuated with (external)
     64  1.6.4.2  yamt  * resistors.  Negative voltages are measured using a reference
     65  1.6.4.2  yamt  * voltage.  So we have to convert the sensor values back to real
     66  1.6.4.2  yamt  * voltages by applying the appropriate resistor factor.
     67  1.6.4.2  yamt  */
     68  1.6.4.2  yamt #define RFACT_NONE	10000
     69  1.6.4.2  yamt #define RFACT(x, y)	(RFACT_NONE * ((x) + (y)) / (y))
     70  1.6.4.2  yamt 
     71  1.6.4.2  yamt /* autoconf(9) functions */
     72  1.6.4.2  yamt static int	itesio_isa_match(device_t, struct cfdata *, void *);
     73  1.6.4.2  yamt static void	itesio_isa_attach(device_t, device_t, void *);
     74  1.6.4.2  yamt static int	itesio_isa_detach(device_t, int);
     75  1.6.4.2  yamt 
     76  1.6.4.2  yamt CFATTACH_DECL_NEW(itesio, sizeof(struct itesio_softc),
     77  1.6.4.2  yamt     itesio_isa_match, itesio_isa_attach, itesio_isa_detach, NULL);
     78  1.6.4.2  yamt 
     79  1.6.4.2  yamt /* driver functions */
     80  1.6.4.2  yamt static uint8_t	itesio_ecreadreg(struct itesio_softc *, int);
     81  1.6.4.2  yamt static void	itesio_ecwritereg(struct itesio_softc *, int, int);
     82  1.6.4.2  yamt static uint8_t	itesio_readreg(bus_space_tag_t, bus_space_handle_t, int);
     83  1.6.4.2  yamt static void	itesio_writereg(bus_space_tag_t, bus_space_handle_t, int, int);
     84  1.6.4.2  yamt static void	itesio_enter(bus_space_tag_t, bus_space_handle_t);
     85  1.6.4.2  yamt static void	itesio_exit(bus_space_tag_t, bus_space_handle_t);
     86  1.6.4.2  yamt 
     87  1.6.4.3  yamt /* sysmon_envsys(9) glue */
     88  1.6.4.2  yamt static void	itesio_setup_sensors(struct itesio_softc *);
     89  1.6.4.2  yamt static void	itesio_refresh_temp(struct itesio_softc *, envsys_data_t *);
     90  1.6.4.2  yamt static void	itesio_refresh_volts(struct itesio_softc *, envsys_data_t *);
     91  1.6.4.2  yamt static void	itesio_refresh_fans(struct itesio_softc *, envsys_data_t *);
     92  1.6.4.2  yamt static void	itesio_refresh(struct sysmon_envsys *, envsys_data_t *);
     93  1.6.4.2  yamt 
     94  1.6.4.3  yamt /* sysmon_wdog glue */
     95  1.6.4.3  yamt static int	itesio_wdt_setmode(struct sysmon_wdog *);
     96  1.6.4.3  yamt static int 	itesio_wdt_tickle(struct sysmon_wdog *);
     97  1.6.4.3  yamt 
     98  1.6.4.2  yamt /* rfact values for voltage sensors */
     99  1.6.4.2  yamt static const int itesio_vrfact[] = {
    100  1.6.4.2  yamt 	RFACT_NONE,	/* VCORE_A	*/
    101  1.6.4.2  yamt 	RFACT_NONE,	/* VCORE_B	*/
    102  1.6.4.2  yamt 	RFACT_NONE,	/* +3.3V	*/
    103  1.6.4.2  yamt 	RFACT(68, 100),	/* +5V 		*/
    104  1.6.4.2  yamt 	RFACT(30, 10),	/* +12V 	*/
    105  1.6.4.2  yamt 	RFACT(21, 10),	/* -12V 	*/
    106  1.6.4.2  yamt 	RFACT(83, 20),	/* -5V 		*/
    107  1.6.4.2  yamt 	RFACT(68, 100),	/* STANDBY	*/
    108  1.6.4.2  yamt 	RFACT_NONE	/* VBAT		*/
    109  1.6.4.2  yamt };
    110  1.6.4.2  yamt 
    111  1.6.4.2  yamt static int
    112  1.6.4.2  yamt itesio_isa_match(device_t parent, struct cfdata *match, void *aux)
    113  1.6.4.2  yamt {
    114  1.6.4.2  yamt 	struct isa_attach_args *ia = aux;
    115  1.6.4.2  yamt 	bus_space_handle_t ioh;
    116  1.6.4.2  yamt 	uint16_t cr;
    117  1.6.4.2  yamt 
    118  1.6.4.2  yamt 	/* Must supply an address */
    119  1.6.4.2  yamt 	if (ia->ia_nio < 1)
    120  1.6.4.2  yamt 		return 0;
    121  1.6.4.2  yamt 
    122  1.6.4.2  yamt 	if (ISA_DIRECT_CONFIG(ia))
    123  1.6.4.2  yamt 		return 0;
    124  1.6.4.2  yamt 
    125  1.6.4.2  yamt 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    126  1.6.4.2  yamt 		return 0;
    127  1.6.4.2  yamt 
    128  1.6.4.2  yamt 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh))
    129  1.6.4.2  yamt 		return 0;
    130  1.6.4.2  yamt 
    131  1.6.4.2  yamt 	itesio_enter(ia->ia_iot, ioh);
    132  1.6.4.2  yamt 	cr = (itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID1) << 8);
    133  1.6.4.2  yamt 	cr |= itesio_readreg(ia->ia_iot, ioh, ITESIO_CHIPID2);
    134  1.6.4.2  yamt 	itesio_exit(ia->ia_iot, ioh);
    135  1.6.4.2  yamt 	bus_space_unmap(ia->ia_iot, ioh, 2);
    136  1.6.4.2  yamt 
    137  1.6.4.2  yamt 	switch (cr) {
    138  1.6.4.2  yamt 	case ITESIO_ID8705:
    139  1.6.4.2  yamt 	case ITESIO_ID8712:
    140  1.6.4.2  yamt 	case ITESIO_ID8716:
    141  1.6.4.2  yamt 	case ITESIO_ID8718:
    142  1.6.4.2  yamt 		ia->ia_nio = 1;
    143  1.6.4.2  yamt 		ia->ia_io[0].ir_size = 2;
    144  1.6.4.2  yamt 		ia->ia_niomem = 0;
    145  1.6.4.2  yamt 		ia->ia_nirq = 0;
    146  1.6.4.2  yamt 		ia->ia_ndrq = 0;
    147  1.6.4.2  yamt 		return 1;
    148  1.6.4.2  yamt 	default:
    149  1.6.4.2  yamt 		return 0;
    150  1.6.4.2  yamt 	}
    151  1.6.4.2  yamt }
    152  1.6.4.2  yamt 
    153  1.6.4.2  yamt static void
    154  1.6.4.2  yamt itesio_isa_attach(device_t parent, device_t self, void *aux)
    155  1.6.4.2  yamt {
    156  1.6.4.2  yamt 	struct itesio_softc *sc = device_private(self);
    157  1.6.4.2  yamt 	struct isa_attach_args *ia = aux;
    158  1.6.4.2  yamt 	int i;
    159  1.6.4.2  yamt 	uint8_t cr;
    160  1.6.4.2  yamt 
    161  1.6.4.3  yamt 	sc->sc_iot = ia->ia_iot;
    162  1.6.4.2  yamt 
    163  1.6.4.3  yamt 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, 2, 0,
    164  1.6.4.3  yamt 			  &sc->sc_ioh)) {
    165  1.6.4.2  yamt 		aprint_error(": can't map i/o space\n");
    166  1.6.4.2  yamt 		return;
    167  1.6.4.2  yamt 	}
    168  1.6.4.2  yamt 
    169  1.6.4.2  yamt 	aprint_naive("\n");
    170  1.6.4.2  yamt 
    171  1.6.4.2  yamt 	/*
    172  1.6.4.2  yamt 	 * Enter to the Super I/O MB PNP mode.
    173  1.6.4.2  yamt 	 */
    174  1.6.4.3  yamt 	itesio_enter(sc->sc_iot, sc->sc_ioh);
    175  1.6.4.2  yamt 	/*
    176  1.6.4.2  yamt 	 * Get info from the Super I/O Global Configuration Registers:
    177  1.6.4.2  yamt 	 * Chip IDs and Device Revision.
    178  1.6.4.2  yamt 	 */
    179  1.6.4.3  yamt 	sc->sc_chipid = (itesio_readreg(sc->sc_iot, sc->sc_ioh,
    180  1.6.4.3  yamt 	    ITESIO_CHIPID1) << 8);
    181  1.6.4.3  yamt 	sc->sc_chipid |= itesio_readreg(sc->sc_iot, sc->sc_ioh,
    182  1.6.4.3  yamt 	    ITESIO_CHIPID2);
    183  1.6.4.3  yamt 	sc->sc_devrev = (itesio_readreg(sc->sc_iot, sc->sc_ioh,
    184  1.6.4.3  yamt 	    ITESIO_DEVREV) & 0x0f);
    185  1.6.4.2  yamt 	/*
    186  1.6.4.2  yamt 	 * Select the EC LDN to get the Base Address.
    187  1.6.4.2  yamt 	 */
    188  1.6.4.3  yamt 	itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_LDNSEL, ITESIO_EC_LDN);
    189  1.6.4.2  yamt 	sc->sc_hwmon_baseaddr =
    190  1.6.4.3  yamt 	    (itesio_readreg(sc->sc_iot, sc->sc_ioh, ITESIO_EC_MSB) << 8);
    191  1.6.4.3  yamt 	sc->sc_hwmon_baseaddr |= itesio_readreg(sc->sc_iot, sc->sc_ioh,
    192  1.6.4.3  yamt 	    ITESIO_EC_LSB);
    193  1.6.4.2  yamt 	/*
    194  1.6.4.3  yamt 	 * We are done, exit MB PNP mode.
    195  1.6.4.2  yamt 	 */
    196  1.6.4.3  yamt 	itesio_exit(sc->sc_iot, sc->sc_ioh);
    197  1.6.4.2  yamt 
    198  1.6.4.2  yamt 	aprint_normal(": iTE IT%4xF Super I/O (rev %d)\n",
    199  1.6.4.2  yamt 	    sc->sc_chipid, sc->sc_devrev);
    200  1.6.4.2  yamt 	aprint_normal_dev(self, "Hardware Monitor registers at 0x%x\n",
    201  1.6.4.2  yamt 	    sc->sc_hwmon_baseaddr);
    202  1.6.4.2  yamt 
    203  1.6.4.3  yamt 	if (bus_space_map(sc->sc_ec_iot, sc->sc_hwmon_baseaddr, 8, 0,
    204  1.6.4.3  yamt 	    &sc->sc_ec_ioh)) {
    205  1.6.4.2  yamt 		aprint_error_dev(self, "cannot map hwmon i/o space\n");
    206  1.6.4.2  yamt 		return;
    207  1.6.4.2  yamt 	}
    208  1.6.4.2  yamt 
    209  1.6.4.2  yamt 	sc->sc_hwmon_mapped = true;
    210  1.6.4.2  yamt 
    211  1.6.4.2  yamt 	/* Activate monitoring */
    212  1.6.4.2  yamt 	cr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
    213  1.6.4.2  yamt 	SET(cr, 0x01);
    214  1.6.4.2  yamt 	itesio_ecwritereg(sc, ITESIO_EC_CONFIG, cr);
    215  1.6.4.2  yamt 
    216  1.6.4.2  yamt #ifdef notyet
    217  1.6.4.2  yamt 	/* Enable beep alarms */
    218  1.6.4.2  yamt 	cr = itesio_ecreadreg(sc, ITESIO_EC_BEEPEER);
    219  1.6.4.2  yamt 	SET(cr, 0x02);	/* Voltage exceeds limit */
    220  1.6.4.2  yamt 	SET(cr, 0x04);	/* Temperature exceeds limit */
    221  1.6.4.2  yamt 	itesio_ecwritereg(sc, ITESIO_EC_BEEPEER, cr);
    222  1.6.4.2  yamt #endif
    223  1.6.4.2  yamt 
    224  1.6.4.2  yamt 	/*
    225  1.6.4.2  yamt 	 * Initialize and attach sensors.
    226  1.6.4.2  yamt 	 */
    227  1.6.4.2  yamt 	itesio_setup_sensors(sc);
    228  1.6.4.2  yamt 	sc->sc_sme = sysmon_envsys_create();
    229  1.6.4.2  yamt 	for (i = 0; i < IT_NUM_SENSORS; i++) {
    230  1.6.4.2  yamt 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    231  1.6.4.2  yamt 						&sc->sc_sensor[i])) {
    232  1.6.4.2  yamt 			sysmon_envsys_destroy(sc->sc_sme);
    233  1.6.4.2  yamt 			return;
    234  1.6.4.2  yamt 		}
    235  1.6.4.2  yamt 	}
    236  1.6.4.2  yamt 	/*
    237  1.6.4.2  yamt 	 * Hook into the system monitor.
    238  1.6.4.2  yamt 	 */
    239  1.6.4.2  yamt 	sc->sc_sme->sme_name = device_xname(self);
    240  1.6.4.2  yamt 	sc->sc_sme->sme_cookie = sc;
    241  1.6.4.2  yamt 	sc->sc_sme->sme_refresh = itesio_refresh;
    242  1.6.4.2  yamt 
    243  1.6.4.2  yamt 	if ((i = sysmon_envsys_register(sc->sc_sme))) {
    244  1.6.4.2  yamt 		aprint_error_dev(self,
    245  1.6.4.2  yamt 		    "unable to register with sysmon (%d)\n", i);
    246  1.6.4.2  yamt 		sysmon_envsys_destroy(sc->sc_sme);
    247  1.6.4.3  yamt 		bus_space_unmap(sc->sc_ec_iot, sc->sc_ec_ioh, 8);
    248  1.6.4.2  yamt 		return;
    249  1.6.4.2  yamt 	}
    250  1.6.4.2  yamt 	sc->sc_hwmon_enabled = true;
    251  1.6.4.3  yamt 
    252  1.6.4.3  yamt 	if (!pmf_device_register(self, NULL, NULL))
    253  1.6.4.3  yamt 		aprint_error_dev(self, "couldn't establish power handler\n");
    254  1.6.4.3  yamt 
    255  1.6.4.3  yamt 	/* The IT8705 doesn't support a WDT */
    256  1.6.4.3  yamt 	if (sc->sc_chipid == ITESIO_ID8705) {
    257  1.6.4.3  yamt 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
    258  1.6.4.3  yamt 		return;
    259  1.6.4.3  yamt 	}
    260  1.6.4.3  yamt 
    261  1.6.4.3  yamt 	/*
    262  1.6.4.3  yamt 	 * Initialize the watchdog timer.
    263  1.6.4.3  yamt 	 */
    264  1.6.4.3  yamt 	sc->sc_smw.smw_name = device_xname(self);
    265  1.6.4.3  yamt 	sc->sc_smw.smw_cookie = sc;
    266  1.6.4.3  yamt 	sc->sc_smw.smw_setmode = itesio_wdt_setmode;
    267  1.6.4.3  yamt 	sc->sc_smw.smw_tickle = itesio_wdt_tickle;
    268  1.6.4.3  yamt 	sc->sc_smw.smw_period = 60;
    269  1.6.4.3  yamt 
    270  1.6.4.3  yamt 	if (sysmon_wdog_register(&sc->sc_smw)) {
    271  1.6.4.3  yamt 		aprint_error_dev(self, "unable to register watchdog timer\n");
    272  1.6.4.3  yamt 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
    273  1.6.4.3  yamt 		return;
    274  1.6.4.3  yamt 	}
    275  1.6.4.3  yamt 	sc->sc_wdt_enabled = true;
    276  1.6.4.3  yamt 	aprint_normal_dev(self, "Watchdog Timer present\n");
    277  1.6.4.2  yamt }
    278  1.6.4.2  yamt 
    279  1.6.4.2  yamt static int
    280  1.6.4.2  yamt itesio_isa_detach(device_t self, int flags)
    281  1.6.4.2  yamt {
    282  1.6.4.2  yamt 	struct itesio_softc *sc = device_private(self);
    283  1.6.4.2  yamt 
    284  1.6.4.2  yamt 	if (sc->sc_hwmon_enabled)
    285  1.6.4.2  yamt 		sysmon_envsys_unregister(sc->sc_sme);
    286  1.6.4.2  yamt 	if (sc->sc_hwmon_mapped)
    287  1.6.4.3  yamt 		bus_space_unmap(sc->sc_ec_iot, sc->sc_ec_ioh, 8);
    288  1.6.4.3  yamt 	if (sc->sc_wdt_enabled) {
    289  1.6.4.3  yamt 		sysmon_wdog_unregister(&sc->sc_smw);
    290  1.6.4.3  yamt 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
    291  1.6.4.3  yamt 	}
    292  1.6.4.3  yamt 
    293  1.6.4.2  yamt 	return 0;
    294  1.6.4.2  yamt }
    295  1.6.4.2  yamt 
    296  1.6.4.2  yamt /*
    297  1.6.4.2  yamt  * Functions to read/write to the Environmental Controller.
    298  1.6.4.2  yamt  */
    299  1.6.4.2  yamt static uint8_t
    300  1.6.4.2  yamt itesio_ecreadreg(struct itesio_softc *sc, int reg)
    301  1.6.4.2  yamt {
    302  1.6.4.3  yamt 	bus_space_write_1(sc->sc_ec_iot, sc->sc_ec_ioh, ITESIO_EC_ADDR, reg);
    303  1.6.4.3  yamt 	return bus_space_read_1(sc->sc_ec_iot, sc->sc_ec_ioh, ITESIO_EC_DATA);
    304  1.6.4.2  yamt }
    305  1.6.4.2  yamt 
    306  1.6.4.2  yamt static void
    307  1.6.4.2  yamt itesio_ecwritereg(struct itesio_softc *sc, int reg, int val)
    308  1.6.4.2  yamt {
    309  1.6.4.3  yamt 	bus_space_write_1(sc->sc_ec_iot, sc->sc_ec_ioh, ITESIO_EC_ADDR, reg);
    310  1.6.4.3  yamt 	bus_space_write_1(sc->sc_ec_iot, sc->sc_ec_ioh, ITESIO_EC_DATA, val);
    311  1.6.4.2  yamt }
    312  1.6.4.2  yamt 
    313  1.6.4.2  yamt /*
    314  1.6.4.2  yamt  * Functions to enter/exit/read/write to the Super I/O.
    315  1.6.4.2  yamt  */
    316  1.6.4.2  yamt static uint8_t
    317  1.6.4.2  yamt itesio_readreg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg)
    318  1.6.4.2  yamt {
    319  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
    320  1.6.4.2  yamt 	return bus_space_read_1(iot, ioh, ITESIO_DATA);
    321  1.6.4.2  yamt }
    322  1.6.4.2  yamt 
    323  1.6.4.2  yamt static void
    324  1.6.4.2  yamt itesio_writereg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg, int val)
    325  1.6.4.2  yamt {
    326  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_ADDR, reg);
    327  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_DATA, val);
    328  1.6.4.2  yamt }
    329  1.6.4.2  yamt 
    330  1.6.4.2  yamt static void
    331  1.6.4.2  yamt itesio_enter(bus_space_tag_t iot, bus_space_handle_t ioh)
    332  1.6.4.2  yamt {
    333  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x87);
    334  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x01);
    335  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
    336  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x55);
    337  1.6.4.2  yamt }
    338  1.6.4.2  yamt 
    339  1.6.4.2  yamt static void
    340  1.6.4.2  yamt itesio_exit(bus_space_tag_t iot, bus_space_handle_t ioh)
    341  1.6.4.2  yamt {
    342  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_ADDR, 0x02);
    343  1.6.4.2  yamt 	bus_space_write_1(iot, ioh, ITESIO_DATA, 0x02);
    344  1.6.4.2  yamt }
    345  1.6.4.2  yamt 
    346  1.6.4.2  yamt 
    347  1.6.4.2  yamt #define COPYDESCR(x, y)				\
    348  1.6.4.2  yamt 	do {					\
    349  1.6.4.2  yamt 		strlcpy((x), (y), sizeof(x));	\
    350  1.6.4.2  yamt 	} while (0)
    351  1.6.4.2  yamt /*
    352  1.6.4.2  yamt  * sysmon_envsys(9) glue.
    353  1.6.4.2  yamt  */
    354  1.6.4.2  yamt static void
    355  1.6.4.2  yamt itesio_setup_sensors(struct itesio_softc *sc)
    356  1.6.4.2  yamt {
    357  1.6.4.2  yamt 	int i;
    358  1.6.4.2  yamt 
    359  1.6.4.2  yamt 	/* temperatures */
    360  1.6.4.2  yamt 	for (i = 0; i < IT_VOLTSTART_IDX; i++)
    361  1.6.4.2  yamt 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    362  1.6.4.2  yamt 
    363  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[0].desc, "CPU Temp");
    364  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[1].desc, "System Temp");
    365  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[2].desc, "Aux Temp");
    366  1.6.4.2  yamt 
    367  1.6.4.2  yamt 	/* voltages */
    368  1.6.4.2  yamt 	for (i = IT_VOLTSTART_IDX; i < IT_FANSTART_IDX; i++) {
    369  1.6.4.2  yamt 		sc->sc_sensor[i].units = ENVSYS_SVOLTS_DC;
    370  1.6.4.2  yamt 		sc->sc_sensor[i].flags = ENVSYS_FCHANGERFACT;
    371  1.6.4.2  yamt 	}
    372  1.6.4.2  yamt 
    373  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[3].desc, "VCORE_A");
    374  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[4].desc, "VCORE_B");
    375  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[5].desc, "+3.3V");
    376  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[6].desc, "+5V");
    377  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[7].desc, "+12V");
    378  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[8].desc, "-12V");
    379  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[9].desc, "-5V");
    380  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[10].desc, "STANDBY");
    381  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[11].desc, "VBAT");
    382  1.6.4.2  yamt 
    383  1.6.4.2  yamt 	/* fans */
    384  1.6.4.2  yamt 	for (i = IT_FANSTART_IDX; i < IT_NUM_SENSORS; i++)
    385  1.6.4.2  yamt 		sc->sc_sensor[i].units = ENVSYS_SFANRPM;
    386  1.6.4.2  yamt 
    387  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[12].desc, "CPU Fan");
    388  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[13].desc, "System Fan");
    389  1.6.4.2  yamt 	COPYDESCR(sc->sc_sensor[14].desc, "Aux Fan");
    390  1.6.4.2  yamt }
    391  1.6.4.2  yamt #undef COPYDESCR
    392  1.6.4.2  yamt 
    393  1.6.4.2  yamt static void
    394  1.6.4.2  yamt itesio_refresh_temp(struct itesio_softc *sc, envsys_data_t *edata)
    395  1.6.4.2  yamt {
    396  1.6.4.2  yamt 	int sdata;
    397  1.6.4.2  yamt 
    398  1.6.4.2  yamt 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORTEMPBASE + edata->sensor);
    399  1.6.4.2  yamt 	/* sensor is not connected or reporting invalid data */
    400  1.6.4.2  yamt 	if (sdata == 0 || sdata >= 0xfa) {
    401  1.6.4.2  yamt 		edata->state = ENVSYS_SINVALID;
    402  1.6.4.2  yamt 		return;
    403  1.6.4.2  yamt 	}
    404  1.6.4.2  yamt 
    405  1.6.4.2  yamt 	DPRINTF(("%s: sdata[temp%d] 0x%x\n", __func__, edata->sensor, sdata));
    406  1.6.4.2  yamt 	/* Convert temperature to uK */
    407  1.6.4.2  yamt 	edata->value_cur = sdata * 1000000 + 273150000;
    408  1.6.4.2  yamt 	edata->state = ENVSYS_SVALID;
    409  1.6.4.2  yamt }
    410  1.6.4.2  yamt 
    411  1.6.4.2  yamt static void
    412  1.6.4.2  yamt itesio_refresh_volts(struct itesio_softc *sc, envsys_data_t *edata)
    413  1.6.4.2  yamt {
    414  1.6.4.2  yamt 	uint8_t vbatcr = 0;
    415  1.6.4.2  yamt 	int i, sdata;
    416  1.6.4.2  yamt 
    417  1.6.4.2  yamt 	i = edata->sensor - IT_VOLTSTART_IDX;
    418  1.6.4.2  yamt 
    419  1.6.4.2  yamt 	sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORVOLTBASE + i);
    420  1.6.4.2  yamt 	/* not connected */
    421  1.6.4.2  yamt 	if (sdata == 0 || sdata == 0xff) {
    422  1.6.4.2  yamt 		edata->state = ENVSYS_SINVALID;
    423  1.6.4.2  yamt 		return;
    424  1.6.4.2  yamt 	}
    425  1.6.4.2  yamt 
    426  1.6.4.2  yamt 	/*
    427  1.6.4.2  yamt 	 * update VBAT voltage reading every time we read it, to get
    428  1.6.4.2  yamt 	 * latest value.
    429  1.6.4.2  yamt 	 */
    430  1.6.4.2  yamt 	if (i == 8) {
    431  1.6.4.2  yamt 		vbatcr = itesio_ecreadreg(sc, ITESIO_EC_CONFIG);
    432  1.6.4.2  yamt 		SET(vbatcr, ITESIO_EC_UPDATEVBAT);
    433  1.6.4.2  yamt 		itesio_ecwritereg(sc, ITESIO_EC_CONFIG, vbatcr);
    434  1.6.4.2  yamt 	}
    435  1.6.4.2  yamt 
    436  1.6.4.2  yamt 	DPRINTF(("%s: sdata[volt%d] 0x%x\n", __func__, i, sdata));
    437  1.6.4.2  yamt 
    438  1.6.4.2  yamt 	/* voltage returned as (mV << 4) */
    439  1.6.4.2  yamt 	edata->value_cur = (sdata << 4);
    440  1.6.4.2  yamt 	/* rfact is (factor * 10^4) */
    441  1.6.4.2  yamt 	edata->value_cur *= itesio_vrfact[i];
    442  1.6.4.2  yamt 
    443  1.6.4.2  yamt 	if (edata->rfact)
    444  1.6.4.2  yamt 		edata->value_cur += edata->rfact;
    445  1.6.4.2  yamt 	else
    446  1.6.4.2  yamt 		edata->rfact = itesio_vrfact[i];
    447  1.6.4.2  yamt 
    448  1.6.4.2  yamt 	/* division by 10 gets us back to uVDC */
    449  1.6.4.2  yamt 	edata->value_cur /= 10;
    450  1.6.4.2  yamt 	edata->state = ENVSYS_SVALID;
    451  1.6.4.2  yamt }
    452  1.6.4.2  yamt 
    453  1.6.4.2  yamt static void
    454  1.6.4.2  yamt itesio_refresh_fans(struct itesio_softc *sc, envsys_data_t *edata)
    455  1.6.4.2  yamt {
    456  1.6.4.2  yamt 	uint8_t mode = 0;
    457  1.6.4.2  yamt 	uint16_t sdata = 0;
    458  1.6.4.2  yamt 	int i, divisor, odivisor, ndivisor;
    459  1.6.4.2  yamt 
    460  1.6.4.2  yamt 	i = edata->sensor - IT_FANSTART_IDX;
    461  1.6.4.2  yamt 	divisor = odivisor = ndivisor = 0;
    462  1.6.4.2  yamt 
    463  1.6.4.2  yamt 	if (sc->sc_chipid == ITESIO_ID8705 || sc->sc_chipid == ITESIO_ID8712) {
    464  1.6.4.2  yamt 		/*
    465  1.6.4.2  yamt 		 * Use the Fan Tachometer Divisor Register for
    466  1.6.4.2  yamt 		 * IT8705F and IT8712F.
    467  1.6.4.2  yamt 		 */
    468  1.6.4.2  yamt 		divisor = odivisor = ndivisor =
    469  1.6.4.2  yamt 		    itesio_ecreadreg(sc, ITESIO_EC_FAN_TDR);
    470  1.6.4.2  yamt 		sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    471  1.6.4.2  yamt 		if (sdata == 0xff) {
    472  1.6.4.2  yamt 			edata->state = ENVSYS_SINVALID;
    473  1.6.4.2  yamt 			if (i == 2)
    474  1.6.4.2  yamt 				ndivisor |= 0x40;
    475  1.6.4.2  yamt 			else {
    476  1.6.4.2  yamt 				ndivisor &= ~(7 << (i * 3));
    477  1.6.4.2  yamt 				ndivisor |= ((divisor + 1) & 7) << (i * 3);
    478  1.6.4.2  yamt 			}
    479  1.6.4.2  yamt 		} else {
    480  1.6.4.2  yamt 			if (i == 2)
    481  1.6.4.2  yamt 				divisor = divisor & 1 ? 3 : 1;
    482  1.6.4.2  yamt 
    483  1.6.4.2  yamt 			if ((sdata << (divisor & 7)) == 0)
    484  1.6.4.2  yamt 				edata->state = ENVSYS_SINVALID;
    485  1.6.4.2  yamt 			else {
    486  1.6.4.2  yamt 				edata->value_cur =
    487  1.6.4.2  yamt 				    1350000 / (sdata << (divisor & 7));
    488  1.6.4.2  yamt 				edata->state = ENVSYS_SVALID;
    489  1.6.4.2  yamt 			}
    490  1.6.4.2  yamt 		}
    491  1.6.4.2  yamt 		DPRINTF(("%s: 8bit sdata[fan%d] 0x%x div: 0x%x\n", __func__,
    492  1.6.4.2  yamt 		    i, sdata, divisor));
    493  1.6.4.2  yamt 		if (ndivisor != odivisor)
    494  1.6.4.2  yamt 			itesio_ecwritereg(sc, ITESIO_EC_FAN_TDR, ndivisor);
    495  1.6.4.2  yamt 	} else {
    496  1.6.4.2  yamt 		mode = itesio_ecreadreg(sc, ITESIO_EC_FAN16_CER);
    497  1.6.4.2  yamt 		sdata = itesio_ecreadreg(sc, ITESIO_EC_SENSORFANBASE + i);
    498  1.6.4.2  yamt 		if (mode & (1 << i))
    499  1.6.4.2  yamt 			sdata += (itesio_ecreadreg(sc,
    500  1.6.4.2  yamt 			    ITESIO_EC_SENSORFANEXTBASE + i) << 8);
    501  1.6.4.2  yamt 		edata->state = ENVSYS_SVALID;
    502  1.6.4.2  yamt 		if (sdata == 0 ||
    503  1.6.4.2  yamt 		    sdata == ((mode & (1 << i)) ? 0xffff : 0xff))
    504  1.6.4.2  yamt 			edata->state = ENVSYS_SINVALID;
    505  1.6.4.2  yamt 		else {
    506  1.6.4.2  yamt 			edata->value_cur = 1350000 / 2 / sdata;
    507  1.6.4.2  yamt 			edata->state = ENVSYS_SVALID;
    508  1.6.4.2  yamt 		}
    509  1.6.4.2  yamt 		DPRINTF(("%s: 16bit sdata[fan%d] 0x%x\n", __func__, i, sdata));
    510  1.6.4.2  yamt 	}
    511  1.6.4.2  yamt }
    512  1.6.4.2  yamt 
    513  1.6.4.2  yamt static void
    514  1.6.4.2  yamt itesio_refresh(struct sysmon_envsys *sme, struct envsys_data *edata)
    515  1.6.4.2  yamt {
    516  1.6.4.2  yamt 	struct itesio_softc *sc = sme->sme_cookie;
    517  1.6.4.2  yamt 
    518  1.6.4.2  yamt 	if (edata->sensor < IT_VOLTSTART_IDX)
    519  1.6.4.2  yamt 		itesio_refresh_temp(sc, edata);
    520  1.6.4.2  yamt 	else if (edata->sensor >= IT_VOLTSTART_IDX &&
    521  1.6.4.2  yamt 	         edata->sensor < IT_FANSTART_IDX)
    522  1.6.4.2  yamt 		itesio_refresh_volts(sc, edata);
    523  1.6.4.2  yamt 	else
    524  1.6.4.2  yamt 		itesio_refresh_fans(sc, edata);
    525  1.6.4.2  yamt }
    526  1.6.4.3  yamt 
    527  1.6.4.3  yamt static int
    528  1.6.4.3  yamt itesio_wdt_setmode(struct sysmon_wdog *smw)
    529  1.6.4.3  yamt {
    530  1.6.4.3  yamt 	struct itesio_softc *sc = smw->smw_cookie;
    531  1.6.4.3  yamt 	int period = smw->smw_period;
    532  1.6.4.3  yamt 
    533  1.6.4.3  yamt 	/* Enter MB PNP mode and select the WDT LDN */
    534  1.6.4.3  yamt 	itesio_enter(sc->sc_iot, sc->sc_ioh);
    535  1.6.4.3  yamt 	itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_LDNSEL, ITESIO_WDT_LDN);
    536  1.6.4.3  yamt 
    537  1.6.4.3  yamt 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    538  1.6.4.3  yamt 		/* Disable the watchdog */
    539  1.6.4.3  yamt 		itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_CTL, 0);
    540  1.6.4.3  yamt 		itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_CNF, 0);
    541  1.6.4.3  yamt 		itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_TMO_MSB, 0);
    542  1.6.4.3  yamt 		itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_TMO_LSB, 0);
    543  1.6.4.3  yamt 	} else {
    544  1.6.4.3  yamt 		/* Enable the watchdog */
    545  1.6.4.3  yamt 		if (period > ITESIO_WDT_MAXTIMO || period < 1)
    546  1.6.4.3  yamt 			period = smw->smw_period = ITESIO_WDT_MAXTIMO;
    547  1.6.4.3  yamt 
    548  1.6.4.3  yamt 		period *= 2;
    549  1.6.4.3  yamt 
    550  1.6.4.3  yamt 		/* set the timeout and start the watchdog */
    551  1.6.4.3  yamt 		itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_TMO_MSB,
    552  1.6.4.3  yamt 		    period >> 8);
    553  1.6.4.3  yamt 		itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_TMO_LSB,
    554  1.6.4.3  yamt 		    period & 0xff);
    555  1.6.4.3  yamt 		itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_CNF,
    556  1.6.4.3  yamt 		    ITESIO_WDT_CNF_SECS | ITESIO_WDT_CNF_KRST |
    557  1.6.4.3  yamt 		    ITESIO_WDT_CNF_PWROK);
    558  1.6.4.3  yamt 	}
    559  1.6.4.3  yamt 	/* we are done, exit MB PNP mode */
    560  1.6.4.3  yamt 	itesio_exit(sc->sc_iot, sc->sc_ioh);
    561  1.6.4.3  yamt 
    562  1.6.4.3  yamt 	return 0;
    563  1.6.4.3  yamt }
    564  1.6.4.3  yamt 
    565  1.6.4.3  yamt static int
    566  1.6.4.3  yamt itesio_wdt_tickle(struct sysmon_wdog *smw)
    567  1.6.4.3  yamt {
    568  1.6.4.3  yamt 	struct itesio_softc *sc = smw->smw_cookie;
    569  1.6.4.3  yamt 	int period = smw->smw_period * 2;
    570  1.6.4.3  yamt 
    571  1.6.4.3  yamt 	/* refresh timeout value and exit */
    572  1.6.4.3  yamt 	itesio_enter(sc->sc_iot, sc->sc_ioh);
    573  1.6.4.3  yamt 	itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_LDNSEL, ITESIO_WDT_LDN);
    574  1.6.4.3  yamt 	itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_TMO_MSB,
    575  1.6.4.3  yamt 	    period >> 8);
    576  1.6.4.3  yamt 	itesio_writereg(sc->sc_iot, sc->sc_ioh, ITESIO_WDT_TMO_LSB,
    577  1.6.4.3  yamt 	    period & 0xff);
    578  1.6.4.3  yamt 	itesio_exit(sc->sc_iot, sc->sc_ioh);
    579  1.6.4.3  yamt 
    580  1.6.4.3  yamt 	return 0;
    581  1.6.4.3  yamt }
    582