Home | History | Annotate | Line # | Download | only in isa
wbsio.c revision 1.11
      1 /*	$NetBSD: wbsio.c,v 1.11 2017/07/07 08:42:15 msaitoh Exp $	*/
      2 /*	$OpenBSD: wbsio.c,v 1.10 2015/03/14 03:38:47 jsg Exp $	*/
      3 /*
      4  * Copyright (c) 2008 Mark Kettenis <kettenis (at) openbsd.org>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 /*
     20  * Winbond LPC Super I/O driver.
     21  */
     22 
     23 #include <sys/param.h>
     24 #include <sys/device.h>
     25 #include <sys/kernel.h>
     26 #include <sys/module.h>
     27 #include <sys/systm.h>
     28 
     29 #include <sys/bus.h>
     30 
     31 #include <dev/isa/isareg.h>
     32 #include <dev/isa/isavar.h>
     33 #include <dev/isa/wbsioreg.h>
     34 
     35 struct wbsio_softc {
     36 	device_t	sc_dev;
     37 	device_t	sc_lm_dev;
     38 
     39 	bus_space_tag_t		sc_iot;
     40 	bus_space_handle_t	sc_ioh;
     41 
     42 	struct isa_attach_args	sc_ia;
     43 	struct isa_io		sc_io;
     44 };
     45 
     46 int	wbsio_probe(device_t, cfdata_t, void *);
     47 void	wbsio_attach(device_t, device_t, void *);
     48 int	wbsio_detach(device_t, int);
     49 int	wbsio_rescan(device_t, const char *, const int *);
     50 void	wbsio_childdet(device_t, device_t);
     51 int	wbsio_print(void *, const char *);
     52 
     53 static int wbsio_search(device_t, cfdata_t, const int *, void *);
     54 
     55 CFATTACH_DECL2_NEW(wbsio, sizeof(struct wbsio_softc),
     56     wbsio_probe, wbsio_attach, wbsio_detach, NULL,
     57     wbsio_rescan, wbsio_childdet);
     58 
     59 static __inline void
     60 wbsio_conf_enable(bus_space_tag_t iot, bus_space_handle_t ioh)
     61 {
     62 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
     63 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
     64 }
     65 
     66 static __inline void
     67 wbsio_conf_disable(bus_space_tag_t iot, bus_space_handle_t ioh)
     68 {
     69 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_DS_MAGIC);
     70 }
     71 
     72 static __inline uint8_t
     73 wbsio_conf_read(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index)
     74 {
     75 	bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
     76 	return (bus_space_read_1(iot, ioh, WBSIO_DATA));
     77 }
     78 
     79 static __inline void
     80 wbsio_conf_write(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index,
     81     uint8_t data)
     82 {
     83 	bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
     84 	bus_space_write_1(iot, ioh, WBSIO_DATA, data);
     85 }
     86 
     87 int
     88 wbsio_probe(device_t parent, cfdata_t match, void *aux)
     89 {
     90 	struct isa_attach_args *ia = aux;
     91 	bus_space_tag_t iot;
     92 	bus_space_handle_t ioh;
     93 	uint8_t reg;
     94 
     95 	/* Must supply an address */
     96 	if (ia->ia_nio < 1)
     97 		return 0;
     98 
     99 	if (ISA_DIRECT_CONFIG(ia))
    100 		return 0;
    101 
    102 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    103 		return 0;
    104 
    105 	/* Match by device ID */
    106 	iot = ia->ia_iot;
    107 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WBSIO_IOSIZE, 0, &ioh))
    108 		return 0;
    109 	wbsio_conf_enable(iot, ioh);
    110 	reg = wbsio_conf_read(iot, ioh, WBSIO_ID);
    111 	aprint_debug("wbsio_probe: id 0x%02x\n", reg);
    112 	wbsio_conf_disable(iot, ioh);
    113 	bus_space_unmap(iot, ioh, WBSIO_IOSIZE);
    114 	switch (reg) {
    115 	case WBSIO_ID_W83627HF:
    116 	case WBSIO_ID_W83627DHG:
    117 	case WBSIO_ID_W83627DHGP:
    118 	case WBSIO_ID_W83627EHF:
    119 	case WBSIO_ID_W83627SF:
    120 	case WBSIO_ID_W83627THF:
    121 	case WBSIO_ID_W83627UHG:
    122 	case WBSIO_ID_W83637HF:
    123 	case WBSIO_ID_W83667HG:
    124 	case WBSIO_ID_W83667HGB:
    125 	case WBSIO_ID_W83687THF:
    126 	case WBSIO_ID_W83697HF:
    127 	case WBSIO_ID_W83697UG:
    128 	case WBSIO_ID_NCT5104D:
    129 	case WBSIO_ID_NCT6775:
    130 	case WBSIO_ID_NCT6776F:
    131 	case WBSIO_ID_NCT6779:
    132 	case WBSIO_ID_NCT6791:
    133 	case WBSIO_ID_NCT6792:
    134 		ia->ia_nio = 1;
    135 		ia->ia_io[0].ir_size = WBSIO_IOSIZE;
    136 		ia->ia_niomem = 0;
    137 		ia->ia_nirq = 0;
    138 		ia->ia_ndrq = 0;
    139 		return 1;
    140 	}
    141 
    142 	return 0;
    143 }
    144 
    145 void
    146 wbsio_attach(device_t parent, device_t self, void *aux)
    147 {
    148 	struct wbsio_softc *sc = device_private(self);
    149 	struct isa_attach_args *ia = aux;
    150 	const char *desc = NULL;
    151 	const char *vendor = "Winbond";
    152 	uint8_t reg;
    153 
    154 	sc->sc_dev = self;
    155 
    156 	sc->sc_ia = *ia;
    157 
    158 	/* Map ISA I/O space */
    159 	sc->sc_iot = ia->ia_iot;
    160 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
    161 	    WBSIO_IOSIZE, 0, &sc->sc_ioh)) {
    162 		aprint_error(": can't map i/o space\n");
    163 		return;
    164 	}
    165 
    166 	/* Enter configuration mode */
    167 	wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
    168 
    169 	/* Read device ID */
    170 	reg = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
    171 	switch (reg) {
    172 	case WBSIO_ID_W83627HF:
    173 		desc = "W83627HF";
    174 		break;
    175 	case WBSIO_ID_W83627DHG:
    176 		desc = "W83627DHG";
    177 		break;
    178 	case WBSIO_ID_W83627DHGP:
    179 		printf("W83627DHG-P");
    180 		break;
    181 	case WBSIO_ID_W83627EHF:
    182 		desc = "W83627EHF";
    183 		break;
    184 	case WBSIO_ID_W83627SF:
    185 		desc = "W83627SF";
    186 		break;
    187 	case WBSIO_ID_W83627THF:
    188 		desc = "W83627THF";
    189 		break;
    190 	case WBSIO_ID_W83627UHG:
    191 		printf("W83627UHG");
    192 		break;
    193 	case WBSIO_ID_W83637HF:
    194 		desc = "W83637HF";
    195 		break;
    196 	case WBSIO_ID_W83667HG:
    197 		desc = "W83667HG";
    198 		break;
    199 	case WBSIO_ID_W83667HGB:
    200 		desc = "W83667HGB";
    201 		break;
    202 	case WBSIO_ID_W83687THF:
    203 		desc = "W83687THF";
    204 		break;
    205 	case WBSIO_ID_W83697HF:
    206 		desc = "W83697HF";
    207 		break;
    208 	case WBSIO_ID_W83697UG:
    209 		desc = "W83697UG";
    210 		break;
    211 	case WBSIO_ID_NCT5104D:
    212 		vendor = "Nuvoton";
    213 		printf("NCT5104D");
    214 		break;
    215 	case WBSIO_ID_NCT6775:
    216 		vendor = "Nuvoton";
    217 		desc = "NCT6775";
    218 		break;
    219 	case WBSIO_ID_NCT6776F:
    220 		vendor = "Nuvoton";
    221 		desc = "NCT6776F";
    222 		break;
    223 	case WBSIO_ID_NCT6779:
    224 		vendor = "Nuvoton";
    225 		desc = "NCT6779";
    226 		break;
    227 	case WBSIO_ID_NCT6791:
    228 		vendor = "Nuvoton";
    229 		desc = "NCT6791";
    230 		break;
    231 	case WBSIO_ID_NCT6792:
    232 		vendor = "Nuvoton";
    233 		desc = "NCT6792";
    234 		break;
    235 	case WBSIO_ID_NCT6793:
    236 		vendor = "Nuvoton";
    237 		desc = "NCT6793";
    238 		break;
    239 	}
    240 	/* Read device revision */
    241 	reg = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
    242 
    243 	aprint_naive("\n");
    244 	aprint_normal(": %s LPC Super I/O %s rev 0x%02x\n", vendor, desc, reg);
    245 
    246 	/* Escape from configuration mode */
    247 	wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
    248 
    249 	if (!pmf_device_register(self, NULL, NULL))
    250 		aprint_error_dev(self, "couldn't establish power handler\n");
    251 	wbsio_rescan(self, "wbsio", NULL);
    252 }
    253 
    254 int
    255 wbsio_detach(device_t self, int flags)
    256 {
    257 	struct wbsio_softc *sc = device_private(self);
    258 	int rc;
    259 
    260 	if ((rc = config_detach_children(self, flags)) != 0)
    261 		return rc;
    262 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, WBSIO_IOSIZE);
    263 	pmf_device_deregister(self);
    264 	return 0;
    265 }
    266 
    267 int
    268 wbsio_rescan(device_t self, const char *ifattr, const int *locators)
    269 {
    270 
    271 	config_search_loc(wbsio_search, self, ifattr, locators, NULL);
    272 
    273 	return 0;
    274 }
    275 
    276 void
    277 wbsio_childdet(device_t self, device_t child)
    278 {
    279 	struct wbsio_softc *sc = device_private(self);
    280 
    281 	if (sc->sc_lm_dev == child)
    282 		sc->sc_lm_dev = NULL;
    283 }
    284 
    285 static int
    286 wbsio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
    287 {
    288 	struct wbsio_softc *sc = device_private(parent);
    289 	uint16_t iobase;
    290 	uint8_t reg0, reg1, devid;
    291 
    292 	/* Enter configuration mode */
    293 	wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
    294 
    295 	/* Select HM logical device */
    296 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
    297 
    298 	/*
    299 	 * The address should be 8-byte aligned, but it seems some
    300 	 * BIOSes ignore this.  They get away with it, because
    301 	 * Apparently the hardware simply ignores the lower three
    302 	 * bits.  We do the same here.
    303 	 */
    304 	reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB);
    305 	reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB);
    306 
    307 	/* Escape from configuration mode */
    308 	wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
    309 
    310 	iobase = (reg1 << 8) | (reg0 & ~0x7);
    311 
    312 	if (iobase == 0)
    313 		return -1;
    314 
    315 	/* Enter configuration mode */
    316 	wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
    317 	/* Read device ID */
    318 	devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
    319 	/* Escape from configuration mode */
    320 	wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
    321 
    322 	sc->sc_ia.ia_nio = 1;
    323 	sc->sc_ia.ia_io = &sc->sc_io;
    324 	sc->sc_ia.ia_io[0].ir_addr = iobase;
    325 	sc->sc_ia.ia_io[0].ir_size = 8;
    326 	sc->sc_ia.ia_niomem = 0;
    327 	sc->sc_ia.ia_nirq = 0;
    328 	sc->sc_ia.ia_ndrq = 0;
    329 	/* Store device-id to ia_aux */
    330 	sc->sc_ia.ia_aux = (void *)(uintptr_t)devid;
    331 	sc->sc_lm_dev = config_attach(parent, cf, &sc->sc_ia, wbsio_print);
    332 
    333 	return 0;
    334 }
    335 
    336 int
    337 wbsio_print(void *aux, const char *pnp)
    338 {
    339 	struct isa_attach_args *ia = aux;
    340 
    341 	if (pnp)
    342 		aprint_normal("%s", pnp);
    343 	if (ia->ia_io[0].ir_size)
    344 		aprint_normal(" port 0x%x", ia->ia_io[0].ir_addr);
    345 	if (ia->ia_io[0].ir_size > 1)
    346 		aprint_normal("-0x%x", ia->ia_io[0].ir_addr +
    347 		    ia->ia_io[0].ir_size - 1);
    348 	return (UNCONF);
    349 }
    350 
    351 MODULE(MODULE_CLASS_DRIVER, wbsio, NULL);
    352 
    353 #ifdef _MODULE
    354 #include "ioconf.c"
    355 #endif
    356 
    357 static int
    358 wbsio_modcmd(modcmd_t cmd, void *opaque)
    359 {
    360 	switch (cmd) {
    361 	case MODULE_CMD_INIT:
    362 #ifdef _MODULE
    363 		return config_init_component(cfdriver_ioconf_wbsio,
    364 		    cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
    365 #else
    366 		return 0;
    367 #endif
    368 	case MODULE_CMD_FINI:
    369 #ifdef _MODULE
    370 		return config_fini_component(cfdriver_ioconf_wbsio,
    371 		    cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
    372 #else
    373 		return 0;
    374 #endif
    375 	default:
    376 		return ENOTTY;
    377 	}
    378 }
    379