Home | History | Annotate | Line # | Download | only in isa
      1 /*	$NetBSD: smsc.c,v 1.13 2022/06/29 15:56:58 mlelstv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Brett Lymn.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * This is a driver for the Standard Microsystems Corp (SMSC)
     34  * LPC47B397 "super i/o" chip.  This driver only handles the environment
     35  * monitoring capabilities of the chip, the other functions will be
     36  * probed/matched as "normal" PC hardware devices (serial ports, fdc, so on).
     37  * SMSC has not deigned to release a datasheet for this particular chip
     38  * (though they do for others they make) so this driver was written from
     39  * information contained in the comment block for the Linux driver.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: smsc.c,v 1.13 2022/06/29 15:56:58 mlelstv Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/device.h>
     48 #include <sys/module.h>
     49 #include <sys/bus.h>
     50 
     51 #include <dev/isa/isareg.h>
     52 #include <dev/isa/isavar.h>
     53 
     54 #include <dev/sysmon/sysmonvar.h>
     55 #include <dev/isa/smscvar.h>
     56 
     57 #if defined(LMDEBUG)
     58 #define DPRINTF(x)	do { printf x; } while (0)
     59 #else
     60 #define DPRINTF(x)
     61 #endif
     62 
     63 static int	smsc_match(device_t, cfdata_t, void *);
     64 static void	smsc_attach(device_t, device_t, void *);
     65 static int	smsc_detach(device_t, int);
     66 
     67 static uint8_t	smsc_readreg(bus_space_tag_t, bus_space_handle_t, int);
     68 static void 	smsc_writereg(bus_space_tag_t, bus_space_handle_t, int, int);
     69 
     70 static void 	smsc_refresh(struct sysmon_envsys *, envsys_data_t *);
     71 
     72 CFATTACH_DECL_NEW(smsc, sizeof(struct smsc_softc),
     73     smsc_match, smsc_attach, smsc_detach, NULL);
     74 
     75 /*
     76  * Probe for the SMSC Super I/O chip
     77  */
     78 static int
     79 smsc_match(device_t parent, cfdata_t match, void *aux)
     80 {
     81 	bus_space_handle_t ioh;
     82 	struct isa_attach_args *ia = aux;
     83 	int rv;
     84 	uint8_t cr;
     85 
     86 	/* Must supply an address */
     87 	if (ia->ia_nio < 1)
     88 		return 0;
     89 
     90 	if (ISA_DIRECT_CONFIG(ia))
     91 		return 0;
     92 
     93 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
     94 		return 0;
     95 
     96 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh))
     97 		return 0;
     98 
     99 	/* To get the device ID we must enter config mode... */
    100 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_CONFIG_START);
    101 
    102 	/* Then select the device id register */
    103 	cr = smsc_readreg(ia->ia_iot, ioh, SMSC_DEVICE_ID);
    104 
    105 	/* Exit config mode, apparently this is important to do */
    106 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_CONFIG_END);
    107 
    108 	switch (cr) {
    109 	case SMSC_ID_47B397:
    110 	case SMSC_ID_SCH5307NS:
    111 	case SMSC_ID_SCH5317:
    112 		rv = 1;
    113 		break;
    114 	default:
    115 		rv = 0;
    116 		break;
    117 	}
    118 
    119 	DPRINTF(("smsc: rv = %d, cr = %x\n", rv, cr));
    120 
    121 	bus_space_unmap(ia->ia_iot, ioh, 2);
    122 
    123 	if (rv) {
    124 		ia->ia_nio = 1;
    125 		ia->ia_io[0].ir_size = 2;
    126 
    127 		ia->ia_niomem = 0;
    128 		ia->ia_nirq = 0;
    129 		ia->ia_ndrq = 0;
    130 	}
    131 
    132 	return rv;
    133 }
    134 
    135 /*
    136  * Get the base address for the monitoring registers and set up the
    137  * sysmon_envsys(9) framework.
    138  */
    139 static void
    140 smsc_attach(device_t parent, device_t self, void *aux)
    141 {
    142 	struct smsc_softc *sc = device_private(self);
    143 	struct isa_attach_args *ia = aux;
    144 	bus_space_handle_t ioh;
    145 	uint8_t rev, msb, lsb, chipid;
    146 	unsigned address;
    147 	int i;
    148 
    149 	sc->sc_iot = ia->ia_iot;
    150 
    151 	aprint_naive("\n");
    152 
    153 	/*
    154 	 * To attach we need to find the actual Hardware Monitor
    155 	 * I/O address space.
    156 	 */
    157 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0,
    158 	    &ioh)) {
    159 		aprint_error(": can't map base i/o space\n");
    160 		return;
    161 	}
    162 
    163 	/* Enter config mode */
    164 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_CONFIG_START);
    165 
    166 	/*
    167 	 * While we have the base registers mapped, grab the chip
    168 	 * revision and device ID.
    169 	 */
    170 	rev = smsc_readreg(ia->ia_iot, ioh, SMSC_DEVICE_REVISION);
    171 	chipid = smsc_readreg(ia->ia_iot, ioh, SMSC_DEVICE_ID);
    172 
    173 	/* Select the Hardware Monitor LDN */
    174 	smsc_writereg(ia->ia_iot, ioh, SMSC_LOGICAL_DEV_SEL,
    175 	    SMSC_LOGICAL_DEVICE);
    176 
    177 	/* Read the base address for the registers. */
    178 	msb = smsc_readreg(ia->ia_iot, ioh, SMSC_IO_BASE_MSB);
    179 	lsb = smsc_readreg(ia->ia_iot, ioh, SMSC_IO_BASE_LSB);
    180 	address = (msb << 8) | lsb;
    181 
    182 	/* Exit config mode */
    183 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_CONFIG_END);
    184 	bus_space_unmap(ia->ia_iot, ioh, 2);
    185 
    186 	/* Map the Hardware Monitor I/O space. */
    187 	if (bus_space_map(ia->ia_iot, address, 2, 0, &sc->sc_ioh)) {
    188 		aprint_error(": can't map register i/o space\n");
    189 		return;
    190 	}
    191 
    192 	sc->sc_sme = sysmon_envsys_create();
    193 
    194 #define INITSENSOR(index, string, reg, type) 			\
    195 	do {							\
    196 		strlcpy(sc->sc_sensor[index].desc, string,	\
    197 		    sizeof(sc->sc_sensor[index].desc));		\
    198 		sc->sc_sensor[index].units = type;		\
    199 		sc->sc_regs[index] = reg;			\
    200 		sc->sc_sensor[index].state = ENVSYS_SVALID;	\
    201 	} while (/* CONSTCOND */ 0)
    202 
    203 	/* Temperature sensors */
    204 	INITSENSOR(0, "Temp0", SMSC_TEMP1, ENVSYS_STEMP);
    205 	INITSENSOR(1, "Temp1", SMSC_TEMP2, ENVSYS_STEMP);
    206 	INITSENSOR(2, "Temp2", SMSC_TEMP3, ENVSYS_STEMP);
    207 	INITSENSOR(3, "Temp3", SMSC_TEMP4, ENVSYS_STEMP);
    208 
    209 	/* Fan sensors */
    210 	INITSENSOR(4, "Fan0", SMSC_FAN1_LSB, ENVSYS_SFANRPM);
    211 	INITSENSOR(5, "Fan1", SMSC_FAN2_LSB, ENVSYS_SFANRPM);
    212 	INITSENSOR(6, "Fan2", SMSC_FAN3_LSB, ENVSYS_SFANRPM);
    213 	INITSENSOR(7, "Fan3", SMSC_FAN4_LSB, ENVSYS_SFANRPM);
    214 
    215 	for (i = 0; i < SMSC_MAX_SENSORS; i++) {
    216 		sc->sc_sensor[i].state = ENVSYS_SINVALID;
    217 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    218 						&sc->sc_sensor[i])) {
    219 			sysmon_envsys_destroy(sc->sc_sme);
    220 			sc->sc_sme = NULL;
    221 			bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
    222 			return;
    223 		}
    224 	}
    225 
    226 	sc->sc_sme->sme_name = device_xname(self);
    227 	sc->sc_sme->sme_cookie = sc;
    228 	sc->sc_sme->sme_refresh = smsc_refresh;
    229 
    230 	if ((i = sysmon_envsys_register(sc->sc_sme)) != 0) {
    231 		aprint_error(": unable to register with sysmon (%d)\n", i);
    232 		sysmon_envsys_destroy(sc->sc_sme);
    233 		sc->sc_sme = NULL;
    234 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
    235 		return;
    236 	}
    237 
    238 	switch (chipid) {
    239 	case SMSC_ID_47B397:
    240 		aprint_normal(": SMSC LPC47B397 Super I/O");
    241 		break;
    242 	case SMSC_ID_SCH5307NS:
    243 		aprint_normal(": SMSC SCH5307-NS Super I/O");
    244 		break;
    245 	case SMSC_ID_SCH5317:
    246 		aprint_normal(": SMSC SCH5317 Super I/O");
    247 		break;
    248 	}
    249 
    250 	aprint_normal(" (rev %u)\n", rev);
    251 	aprint_normal_dev(self, "Hardware Monitor registers at 0x%04x\n",
    252 	    address);
    253 }
    254 
    255 static int
    256 smsc_detach(device_t self, int flags)
    257 {
    258 	struct smsc_softc *sc = device_private(self);
    259 
    260 	if (sc->sc_sme != NULL)
    261 		sysmon_envsys_unregister(sc->sc_sme);
    262 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
    263 	return 0;
    264 }
    265 
    266 /*
    267  * Read the value of the given register
    268  */
    269 static uint8_t
    270 smsc_readreg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg)
    271 {
    272 	bus_space_write_1(iot, ioh, SMSC_ADDR, reg);
    273 	return bus_space_read_1(iot, ioh, SMSC_DATA);
    274 }
    275 
    276 /*
    277  * Write the given value to the given register.
    278  */
    279 static void
    280 smsc_writereg(bus_space_tag_t iot, bus_space_handle_t ioh, int reg, int val)
    281 {
    282 	bus_space_write_1(iot, ioh, SMSC_ADDR, reg);
    283 	bus_space_write_1(iot, ioh, SMSC_DATA, val);
    284 }
    285 
    286 /* convert temperature read from the chip to micro kelvin */
    287 static inline int
    288 smsc_temp2muk(uint8_t t)
    289 {
    290         int temp=t;
    291 
    292         return temp * 1000000 + 273150000U;
    293 }
    294 
    295 /*
    296  * convert register value read from chip into rpm using:
    297  *
    298  * RPM = 60/(Count * 11.111us)
    299  *
    300  * 1/1.1111us = 90kHz
    301  *
    302  */
    303 static inline int
    304 smsc_reg2rpm(unsigned int r)
    305 {
    306 	unsigned long rpm;
    307 
    308         if (r == 0x0)
    309                 return 0;
    310 
    311 	rpm = (90000 * 60) / ((unsigned long) r);
    312         return (int) rpm;
    313 }
    314 
    315 /* min and max temperatures in uK */
    316 #define SMSC_MIN_TEMP_UK ((-127 * 1000000) + 273150000)
    317 #define SMSC_MAX_TEMP_UK ((127 * 1000000) + 273150000)
    318 
    319 /*
    320  * Get the data for the requested sensor, update the sysmon structure
    321  * with the retrieved value.
    322  */
    323 static void
    324 smsc_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    325 {
    326 	struct smsc_softc *sc = sme->sme_cookie;
    327 	int reg;
    328 	unsigned int rpm;
    329 	uint8_t msb, lsb;
    330 
    331 	reg = sc->sc_regs[edata->sensor];
    332 
    333 	switch (edata->units) {
    334 	case ENVSYS_STEMP:
    335 		edata->value_cur =
    336 		    smsc_temp2muk(smsc_readreg(sc->sc_iot, sc->sc_ioh, reg));
    337 		break;
    338 
    339 	case ENVSYS_SFANRPM:
    340 		/* reading lsb first locks msb... */
    341 		lsb = smsc_readreg(sc->sc_iot, sc->sc_ioh, reg);
    342 		msb = smsc_readreg(sc->sc_iot, sc->sc_ioh, reg + 1);
    343 		rpm = (msb << 8) | lsb;
    344 		edata->value_cur = smsc_reg2rpm(rpm);
    345 		break;
    346 	}
    347 }
    348 
    349 MODULE(MODULE_CLASS_DRIVER, smsc, "sysmon_envsys");
    350 
    351 #ifdef _MODULE
    352 #include "ioconf.c"
    353 #endif
    354 
    355 static int
    356 smsc_modcmd(modcmd_t cmd, void *opaque)
    357 {
    358 	int error = 0;
    359 
    360 	switch (cmd) {
    361 	case MODULE_CMD_INIT:
    362 #ifdef _MODULE
    363 		error = config_init_component(cfdriver_ioconf_smsc,
    364 		    cfattach_ioconf_smsc, cfdata_ioconf_smsc);
    365 #endif
    366 		return error;
    367 	case MODULE_CMD_FINI:
    368 #ifdef _MODULE
    369 		error = config_fini_component(cfdriver_ioconf_smsc,
    370 		    cfattach_ioconf_smsc, cfdata_ioconf_smsc);
    371 #endif
    372 		return error;
    373 	default:
    374 		return ENOTTY;
    375 	}
    376 }
    377