Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.138.18.1
      1 /*	$NetBSD: isa.c,v 1.138.18.1 2012/09/12 06:15:32 tls Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2001, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum; by Jason R. Thorpe of Wasabi Systems, Inc.
      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 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: isa.c,v 1.138.18.1 2012/09/12 06:15:32 tls Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/malloc.h>
     39 #include <sys/device.h>
     40 
     41 #include <sys/intr.h>
     42 
     43 #include <dev/isa/isareg.h>
     44 #include <dev/isa/isavar.h>
     45 #include <dev/isa/isadmareg.h>
     46 
     47 #include "isadma.h"
     48 
     49 #include "isapnp.h"
     50 #if NISAPNP > 0
     51 #include <dev/isapnp/isapnpreg.h>
     52 #include <dev/isapnp/isapnpvar.h>
     53 #endif
     54 
     55 #include "locators.h"
     56 
     57 int	isamatch(device_t, cfdata_t, void *);
     58 void	isaattach(device_t, device_t, void *);
     59 int	isadetach(device_t, int);
     60 int	isarescan(device_t, const char *, const int *);
     61 void	isachilddetached(device_t, device_t);
     62 int	isaprint(void *, const char *);
     63 
     64 CFATTACH_DECL2_NEW(isa, sizeof(struct isa_softc),
     65     isamatch, isaattach, isadetach, NULL, isarescan, isachilddetached);
     66 
     67 void	isa_attach_knowndevs(struct isa_softc *);
     68 void	isa_free_knowndevs(struct isa_softc *);
     69 
     70 int	isasubmatch(device_t, cfdata_t, const int *, void *);
     71 int	isasearch(device_t, cfdata_t, const int *, void *);
     72 
     73 static int	isa_slotcount = -1;	/* -1 == don't know how many */
     74 
     75 int
     76 isamatch(device_t parent, cfdata_t cf, void *aux)
     77 {
     78 	/* XXX check other indicators */
     79 
     80 	return (1);
     81 }
     82 
     83 void
     84 isaattach(device_t parent, device_t self, void *aux)
     85 {
     86 	struct isa_softc *sc = device_private(self);
     87 	struct isabus_attach_args *iba = aux;
     88 	static const int wildcard[ISACF_NLOCS] = {
     89 		ISACF_PORT_DEFAULT, ISACF_SIZE_DEFAULT,
     90 		ISACF_IOMEM_DEFAULT, ISACF_IOSIZ_DEFAULT,
     91 		ISACF_IRQ_DEFAULT, ISACF_DRQ_DEFAULT, ISACF_DRQ2_DEFAULT
     92 	};
     93 
     94 	/* Clamp the maximum transfer size.  The hook may clamp it further. */
     95 	self->dv_maxphys = MIN(parent->dv_maxphys, 64 * 1024);
     96 
     97 	TAILQ_INIT(&sc->sc_knowndevs);
     98 	sc->sc_dynamicdevs = 0;
     99 
    100 	sc->sc_dev = self;
    101 
    102 	isa_attach_hook(parent, self, iba);
    103 	aprint_naive("\n");
    104 	aprint_normal("\n");
    105 
    106 	/* XXX Add code to fetch known-devices. */
    107 
    108 	sc->sc_iot = iba->iba_iot;
    109 	sc->sc_memt = iba->iba_memt;
    110 	sc->sc_dmat = iba->iba_dmat;
    111 	sc->sc_ic = iba->iba_ic;
    112 
    113 #if NISAPNP > 0
    114 	/*
    115 	 * Reset isapnp cards that the bios configured for us
    116 	 */
    117 	isapnp_isa_attach_hook(sc);
    118 #endif
    119 
    120 #if NISADMA > 0
    121 	/*
    122 	 * Initialize our DMA state.
    123 	 */
    124 	isa_dmainit(sc->sc_ic, sc->sc_iot, sc->sc_dmat, self);
    125 #endif
    126 
    127 	/* Attach all direct-config children. */
    128 	isa_attach_knowndevs(sc);
    129 
    130 	/*
    131 	 * If we don't support dynamic hello/goodbye of devices,
    132 	 * then free the knowndevs info now.
    133 	 */
    134 	if (sc->sc_dynamicdevs == 0)
    135 		isa_free_knowndevs(sc);
    136 
    137 	/* Attach all indirect-config children. */
    138 	isarescan(self, "isa", wildcard);
    139 
    140 	if (!pmf_device_register(self, NULL, NULL))
    141 		aprint_error_dev(self, "couldn't establish power handler\n");
    142 }
    143 
    144 int
    145 isadetach(device_t self, int flags)
    146 {
    147 	struct isa_softc *sc = device_private(self);
    148 	int rc;
    149 
    150 	if ((rc = config_detach_children(self, flags)) != 0)
    151 		return rc;
    152 
    153 	pmf_device_deregister(self);
    154 
    155 	isa_free_knowndevs(sc);
    156 
    157 #if NISADMA > 0
    158 	isa_dmadestroy(sc->sc_ic);
    159 #endif
    160 	isa_detach_hook(sc->sc_ic, self);
    161 
    162 	return 0;
    163 }
    164 
    165 int
    166 isarescan(device_t self, const char *ifattr, const int *locators)
    167 {
    168 	prop_dictionary_t dict;
    169 	int locs[ISACF_NLOCS];
    170 	bool no_legacy_devices = false;
    171 
    172 	dict = device_properties(self);
    173 	if (prop_dictionary_get_bool(dict, "no-legacy-devices",
    174 	    &no_legacy_devices) == true) {
    175 		aprint_debug_dev(self, "platform reports no legacy devices\n");
    176 		return 0;
    177 	}
    178 
    179 	memcpy(locs, locators, sizeof(locs));
    180 
    181 	/*
    182 	 * XXX Bus independent code calling this function does not
    183 	 * know the locator default values. It assumes "-1" for now.
    184 	 * (should be made available by "config" one day)
    185 	 * So fixup where the "-1" is not correct.
    186 	 */
    187 	if (locs[ISACF_SIZE] == -1)
    188 		locs[ISACF_SIZE] = ISACF_SIZE_DEFAULT;
    189 	if (locs[ISACF_IOSIZ] == -1)
    190 		locs[ISACF_IOSIZ] = ISACF_IOSIZ_DEFAULT;
    191 
    192 	config_search_loc(isasearch, self, ifattr, locs, NULL);
    193 	return (0);
    194 }
    195 
    196 void
    197 isachilddetached(device_t self, device_t child)
    198 {
    199 	struct isa_knowndev *ik;
    200 	struct isa_softc *sc = device_private(self);
    201 
    202 	TAILQ_FOREACH(ik, &sc->sc_knowndevs, ik_list) {
    203 		if (ik->ik_claimed == child)
    204 			ik->ik_claimed = NULL;
    205 	}
    206 }
    207 
    208 void
    209 isa_attach_knowndevs(struct isa_softc *sc)
    210 {
    211 	struct isa_attach_args ia;
    212 	struct isa_knowndev *ik;
    213 
    214 	if (TAILQ_EMPTY(&sc->sc_knowndevs))
    215 		return;
    216 
    217 	TAILQ_FOREACH(ik, &sc->sc_knowndevs, ik_list) {
    218 		if (ik->ik_claimed != NULL)
    219 			continue;
    220 
    221 		ia.ia_iot = sc->sc_iot;
    222 		ia.ia_memt = sc->sc_memt;
    223 		ia.ia_dmat = sc->sc_dmat;
    224 		ia.ia_ic = sc->sc_ic;
    225 
    226 		ia.ia_pnpname = ik->ik_pnpname;
    227 		ia.ia_pnpcompatnames = ik->ik_pnpcompatnames;
    228 
    229 		ia.ia_io = ik->ik_io;
    230 		ia.ia_nio = ik->ik_nio;
    231 
    232 		ia.ia_iomem = ik->ik_iomem;
    233 		ia.ia_niomem = ik->ik_niomem;
    234 
    235 		ia.ia_irq = ik->ik_irq;
    236 		ia.ia_nirq = ik->ik_nirq;
    237 
    238 		ia.ia_drq = ik->ik_drq;
    239 		ia.ia_ndrq = ik->ik_ndrq;
    240 
    241 		ia.ia_aux = NULL;
    242 
    243 		/* XXX should setup locator array */
    244 
    245 		ik->ik_claimed = config_found_sm_loc(sc->sc_dev,
    246 		    "isa", 0, &ia, isaprint, isasubmatch);
    247 	}
    248 }
    249 
    250 void
    251 isa_free_knowndevs(struct isa_softc *sc)
    252 {
    253 	struct isa_knowndev *ik;
    254 	struct isa_pnpname *ipn;
    255 
    256 #define	FREEIT(x)	if (x != NULL) free(x, M_DEVBUF)
    257 
    258 	while ((ik = TAILQ_FIRST(&sc->sc_knowndevs)) != NULL) {
    259 		TAILQ_REMOVE(&sc->sc_knowndevs, ik, ik_list);
    260 		FREEIT(ik->ik_pnpname);
    261 		while ((ipn = ik->ik_pnpcompatnames) != NULL) {
    262 			ik->ik_pnpcompatnames = ipn->ipn_next;
    263 			free(ipn->ipn_name, M_DEVBUF);
    264 			free(ipn, M_DEVBUF);
    265 		}
    266 		FREEIT(ik->ik_io);
    267 		FREEIT(ik->ik_iomem);
    268 		FREEIT(ik->ik_irq);
    269 		FREEIT(ik->ik_drq);
    270 		free(ik, M_DEVBUF);
    271 	}
    272 
    273 #undef FREEIT
    274 }
    275 
    276 static int
    277 checkattachargs(struct isa_attach_args *ia, const int *loc)
    278 {
    279 	int i;
    280 
    281 	if (ia->ia_nio == 0) {
    282 		if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT)
    283 			return (0);
    284 	} else {
    285 		if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT &&
    286 		    loc[ISACF_PORT] != ia->ia_io[0].ir_addr)
    287 			return (0);
    288 	}
    289 
    290 	if (ia->ia_niomem == 0) {
    291 		if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT)
    292 			return (0);
    293 	} else {
    294 		if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT &&
    295 		    loc[ISACF_IOMEM] != ia->ia_iomem[0].ir_addr)
    296 			return (0);
    297 	}
    298 
    299 	if (ia->ia_nirq == 0) {
    300 		if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT)
    301 			return (0);
    302 	} else {
    303 		if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT &&
    304 		    loc[ISACF_IRQ] != ia->ia_irq[0].ir_irq)
    305 			return (0);
    306 	}
    307 
    308 	if (ia->ia_ndrq == 0) {
    309 		if (loc[ISACF_DRQ] != ISACF_DRQ_DEFAULT)
    310 			return (0);
    311 		if (loc[ISACF_DRQ2] != ISACF_DRQ2_DEFAULT)
    312 			return (0);
    313 	} else {
    314 		for (i = 0; i < 2; i++) {
    315 			if (i == ia->ia_ndrq)
    316 				break;
    317 			if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT &&
    318 			    loc[ISACF_DRQ + i] != ia->ia_drq[i].ir_drq)
    319 				return (0);
    320 		}
    321 		for (; i < 2; i++) {
    322 			if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT)
    323 				return (0);
    324 		}
    325 	}
    326 
    327 	return (1);
    328 }
    329 
    330 int
    331 isasubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    332 {
    333 	struct isa_attach_args *ia = aux;
    334 
    335 	if (!checkattachargs(ia, cf->cf_loc))
    336 		return (0);
    337 
    338 	return (config_match(parent, cf, aux));
    339 }
    340 
    341 int
    342 isaprint(void *aux, const char *isa)
    343 {
    344 	struct isa_attach_args *ia = aux;
    345 	const char *sep;
    346 	int i;
    347 
    348 	/*
    349 	 * This block of code only fires if we have a direct-config'd
    350 	 * device for which there is no driver match.
    351 	 */
    352 	if (isa != NULL) {
    353 		struct isa_pnpname *ipn;
    354 
    355 		if (ia->ia_pnpname != NULL)
    356 			aprint_normal("%s", ia->ia_pnpname);
    357 		if ((ipn = ia->ia_pnpcompatnames) != NULL) {
    358 			aprint_normal(" (");	/* ) */
    359 			for (sep = ""; ipn != NULL;
    360 			     ipn = ipn->ipn_next, sep = " ") {
    361 				aprint_normal("%s%s", sep, ipn->ipn_name);
    362 			}
    363 	/* ( */		aprint_normal(")");
    364 		}
    365 		aprint_normal(" at %s", isa);
    366 	}
    367 
    368 	if (ia->ia_nio) {
    369 		sep = "";
    370 		aprint_normal(" port ");
    371 		for (i = 0; i < ia->ia_nio; i++) {
    372 			if (ia->ia_io[i].ir_size == 0)
    373 				continue;
    374 			aprint_normal("%s0x%x", sep, ia->ia_io[i].ir_addr);
    375 			if (ia->ia_io[i].ir_size > 1)
    376 				aprint_normal("-0x%x", ia->ia_io[i].ir_addr +
    377 				    ia->ia_io[i].ir_size - 1);
    378 			sep = ",";
    379 		}
    380 	}
    381 
    382 	if (ia->ia_niomem) {
    383 		sep = "";
    384 		aprint_normal(" iomem ");
    385 		for (i = 0; i < ia->ia_niomem; i++) {
    386 			if (ia->ia_iomem[i].ir_size == 0)
    387 				continue;
    388 			aprint_normal("%s0x%x", sep, ia->ia_iomem[i].ir_addr);
    389 			if (ia->ia_iomem[i].ir_size > 1)
    390 				aprint_normal("-0x%x", ia->ia_iomem[i].ir_addr +
    391 				    ia->ia_iomem[i].ir_size - 1);
    392 			sep = ",";
    393 		}
    394 	}
    395 
    396 	if (ia->ia_nirq) {
    397 		sep = "";
    398 		aprint_normal(" irq ");
    399 		for (i = 0; i < ia->ia_nirq; i++) {
    400 			if (ia->ia_irq[i].ir_irq == ISACF_IRQ_DEFAULT)
    401 				continue;
    402 			aprint_normal("%s%d", sep, ia->ia_irq[i].ir_irq);
    403 			sep = ",";
    404 		}
    405 	}
    406 
    407 	if (ia->ia_ndrq) {
    408 		sep = "";
    409 		aprint_normal(" drq ");
    410 		for (i = 0; i < ia->ia_ndrq; i++) {
    411 			if (ia->ia_drq[i].ir_drq == ISACF_DRQ_DEFAULT)
    412 				continue;
    413 			aprint_normal("%s%d", sep, ia->ia_drq[i].ir_drq);
    414 			sep = ",";
    415 		}
    416 	}
    417 
    418 	return (UNCONF);
    419 }
    420 
    421 int
    422 isasearch(device_t parent, cfdata_t cf, const int *slocs, void *aux)
    423 {
    424 	struct isa_io res_io[1];
    425 	struct isa_iomem res_mem[1];
    426 	struct isa_irq res_irq[1];
    427 	struct isa_drq res_drq[2];
    428 	struct isa_softc *sc = device_private(parent);
    429 	struct isa_attach_args ia;
    430 	int flocs[ISACF_NLOCS];
    431 	int tryagain;
    432 
    433 	do {
    434 		ia.ia_pnpname = NULL;
    435 		ia.ia_pnpcompatnames = NULL;
    436 
    437 		res_io[0].ir_addr = cf->cf_loc[ISACF_PORT];
    438 		res_io[0].ir_size = 0;
    439 
    440 		res_mem[0].ir_addr = cf->cf_loc[ISACF_IOMEM];
    441 		res_mem[0].ir_size = cf->cf_loc[ISACF_IOSIZ];
    442 
    443 		res_irq[0].ir_irq =
    444 		    cf->cf_loc[ISACF_IRQ] == 2 ? 9 : cf->cf_loc[ISACF_IRQ];
    445 
    446 		res_drq[0].ir_drq = cf->cf_loc[ISACF_DRQ];
    447 		res_drq[1].ir_drq = cf->cf_loc[ISACF_DRQ2];
    448 
    449 		ia.ia_iot = sc->sc_iot;
    450 		ia.ia_memt = sc->sc_memt;
    451 		ia.ia_dmat = sc->sc_dmat;
    452 		ia.ia_ic = sc->sc_ic;
    453 
    454 		ia.ia_io = res_io;
    455 		ia.ia_nio = 1;
    456 
    457 		ia.ia_iomem = res_mem;
    458 		ia.ia_niomem = 1;
    459 
    460 		ia.ia_irq = res_irq;
    461 		ia.ia_nirq = 1;
    462 
    463 		ia.ia_drq = res_drq;
    464 		ia.ia_ndrq = 2;
    465 
    466 		if (!checkattachargs(&ia, slocs))
    467 			return (0);
    468 
    469 		tryagain = 0;
    470 		if (config_match(parent, cf, &ia) > 0) {
    471 			/*
    472 			 * This is not necessary for detach, but might
    473 			 * still be useful to collect device information.
    474 			 */
    475 			flocs[ISACF_PORT] = ia.ia_io[0].ir_addr;
    476 			flocs[ISACF_SIZE] = ia.ia_io[0].ir_size;
    477 			flocs[ISACF_IOMEM] = ia.ia_iomem[0].ir_addr;
    478 			flocs[ISACF_IOSIZ] = ia.ia_iomem[0].ir_size;
    479 			flocs[ISACF_IRQ] = ia.ia_irq[0].ir_irq;
    480 			flocs[ISACF_DRQ] = ia.ia_drq[0].ir_drq;
    481 			flocs[ISACF_DRQ2] = ia.ia_drq[1].ir_drq;
    482 			config_attach_loc(parent, cf, flocs, &ia, isaprint);
    483 			tryagain = (cf->cf_fstate == FSTATE_STAR);
    484 		}
    485 	} while (tryagain);
    486 
    487 	return (0);
    488 }
    489 
    490 const char *
    491 isa_intr_typename(int type)
    492 {
    493 
    494 	switch (type) {
    495 	case IST_NONE:
    496 		return ("none");
    497 	case IST_PULSE:
    498 		return ("pulsed");
    499 	case IST_EDGE:
    500 		return ("edge-triggered");
    501 	case IST_LEVEL:
    502 		return ("level-triggered");
    503 	default:
    504 		panic("isa_intr_typename: invalid type %d", type);
    505 	}
    506 }
    507 
    508 int
    509 isa_get_slotcount(void)
    510 {
    511 
    512 	return isa_slotcount;
    513 }
    514 
    515 void
    516 isa_set_slotcount(int arg)
    517 {
    518 
    519 	isa_slotcount = arg;
    520 }
    521