Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.128.18.3
      1 /*	$NetBSD: isa.c,v 1.128.18.3 2007/10/26 15:45:21 joerg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2001 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: isa.c,v 1.128.18.3 2007/10/26 15:45:21 joerg Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/malloc.h>
     46 #include <sys/device.h>
     47 
     48 #include <sys/intr.h>
     49 
     50 #include <dev/isa/isareg.h>
     51 #include <dev/isa/isavar.h>
     52 #include <dev/isa/isadmareg.h>
     53 
     54 #include "isadma.h"
     55 
     56 #include "isapnp.h"
     57 #if NISAPNP > 0
     58 #include <dev/isapnp/isapnpreg.h>
     59 #include <dev/isapnp/isapnpvar.h>
     60 #endif
     61 
     62 #include "locators.h"
     63 
     64 int	isamatch(struct device *, struct cfdata *, void *);
     65 void	isaattach(struct device *, struct device *, void *);
     66 int	isarescan(struct device *, const char *, const int *);
     67 void	isachilddetached(struct device *, struct device *);
     68 int	isaprint(void *, const char *);
     69 
     70 CFATTACH_DECL2(isa, sizeof(struct isa_softc),
     71     isamatch, isaattach, NULL, NULL, isarescan, isachilddetached);
     72 
     73 void	isa_attach_knowndevs(struct isa_softc *);
     74 void	isa_free_knowndevs(struct isa_softc *);
     75 
     76 int	isasubmatch(struct device *, struct cfdata *, const int *, void *);
     77 int	isasearch(struct device *, struct cfdata *, const int *, void *);
     78 
     79 int
     80 isamatch(struct device *parent, struct cfdata *cf,
     81     void *aux)
     82 {
     83 	/* XXX check other indicators */
     84 
     85 	return (1);
     86 }
     87 
     88 void
     89 isaattach(struct device *parent, struct device *self, void *aux)
     90 {
     91 	struct isa_softc *sc = (struct isa_softc *)self;
     92 	struct isabus_attach_args *iba = aux;
     93 	static const int wildcard[ISACF_NLOCS] = {
     94 		ISACF_PORT_DEFAULT, ISACF_SIZE_DEFAULT,
     95 		ISACF_IOMEM_DEFAULT, ISACF_IOSIZ_DEFAULT,
     96 		ISACF_IRQ_DEFAULT, ISACF_DRQ_DEFAULT, ISACF_DRQ2_DEFAULT
     97 	};
     98 
     99 	TAILQ_INIT(&sc->sc_knowndevs);
    100 	sc->sc_dynamicdevs = 0;
    101 
    102 	isa_attach_hook(parent, self, iba);
    103 	printf("\n");
    104 
    105 	/* XXX Add code to fetch known-devices. */
    106 
    107 	sc->sc_iot = iba->iba_iot;
    108 	sc->sc_memt = iba->iba_memt;
    109 	sc->sc_dmat = iba->iba_dmat;
    110 	sc->sc_ic = iba->iba_ic;
    111 
    112 #if NISAPNP > 0
    113 	/*
    114 	 * Reset isapnp cards that the bios configured for us
    115 	 */
    116 	isapnp_isa_attach_hook(sc);
    117 #endif
    118 
    119 #if NISADMA > 0
    120 	/*
    121 	 * Initialize our DMA state.
    122 	 */
    123 	isa_dmainit(sc->sc_ic, sc->sc_iot, sc->sc_dmat, self);
    124 #endif
    125 
    126 	/* Attach all direct-config children. */
    127 	isa_attach_knowndevs(sc);
    128 
    129 	/*
    130 	 * If we don't support dynamic hello/goodbye of devices,
    131 	 * then free the knowndevs info now.
    132 	 */
    133 	if (sc->sc_dynamicdevs == 0)
    134 		isa_free_knowndevs(sc);
    135 
    136 	/* Attach all indrect-config children. */
    137 	isarescan(self, "isa", wildcard);
    138 
    139 	(void)pnp_register(self, pnp_generic_power);
    140 }
    141 
    142 int
    143 isarescan(struct device *self, const char *ifattr, const int *locators)
    144 {
    145 	int locs[ISACF_NLOCS];
    146 
    147 	memcpy(locs, locators, sizeof(locs));
    148 
    149 	/*
    150 	 * XXX Bus independent code calling this function does not
    151 	 * know the locator default values. It assumes "-1" for now.
    152 	 * (should be made available by "config" one day)
    153 	 * So fixup where the "-1" is not correct.
    154 	 */
    155 	if (locs[ISACF_SIZE] == -1)
    156 		locs[ISACF_SIZE] = ISACF_SIZE_DEFAULT;
    157 	if (locs[ISACF_IOSIZ] == -1)
    158 		locs[ISACF_IOSIZ] = ISACF_IOSIZ_DEFAULT;
    159 
    160 	config_search_loc(isasearch, self, ifattr, locs, NULL);
    161 	return (0);
    162 }
    163 
    164 void
    165 isachilddetached(struct device *self, struct device *child)
    166 {
    167 	/* nothing to do */
    168 }
    169 
    170 void
    171 isa_attach_knowndevs(struct isa_softc *sc)
    172 {
    173 	struct isa_attach_args ia;
    174 	struct isa_knowndev *ik;
    175 
    176 	if (TAILQ_EMPTY(&sc->sc_knowndevs))
    177 		return;
    178 
    179 	TAILQ_FOREACH(ik, &sc->sc_knowndevs, ik_list) {
    180 		if (ik->ik_claimed != NULL)
    181 			continue;
    182 
    183 		ia.ia_iot = sc->sc_iot;
    184 		ia.ia_memt = sc->sc_memt;
    185 		ia.ia_dmat = sc->sc_dmat;
    186 		ia.ia_ic = sc->sc_ic;
    187 
    188 		ia.ia_pnpname = ik->ik_pnpname;
    189 		ia.ia_pnpcompatnames = ik->ik_pnpcompatnames;
    190 
    191 		ia.ia_io = ik->ik_io;
    192 		ia.ia_nio = ik->ik_nio;
    193 
    194 		ia.ia_iomem = ik->ik_iomem;
    195 		ia.ia_niomem = ik->ik_niomem;
    196 
    197 		ia.ia_irq = ik->ik_irq;
    198 		ia.ia_nirq = ik->ik_nirq;
    199 
    200 		ia.ia_drq = ik->ik_drq;
    201 		ia.ia_ndrq = ik->ik_ndrq;
    202 
    203 		ia.ia_aux = NULL;
    204 
    205 		/* XXX should setup locator array */
    206 
    207 		ik->ik_claimed = config_found_sm_loc(&sc->sc_dev,
    208 		    "isa", 0, &ia, isaprint, isasubmatch);
    209 	}
    210 }
    211 
    212 void
    213 isa_free_knowndevs(struct isa_softc *sc)
    214 {
    215 	struct isa_knowndev *ik;
    216 	struct isa_pnpname *ipn;
    217 
    218 #define	FREEIT(x)	if (x != NULL) free(x, M_DEVBUF)
    219 
    220 	while ((ik = TAILQ_FIRST(&sc->sc_knowndevs)) != NULL) {
    221 		TAILQ_REMOVE(&sc->sc_knowndevs, ik, ik_list);
    222 		FREEIT(ik->ik_pnpname);
    223 		while ((ipn = ik->ik_pnpcompatnames) != NULL) {
    224 			ik->ik_pnpcompatnames = ipn->ipn_next;
    225 			free(ipn->ipn_name, M_DEVBUF);
    226 			free(ipn, M_DEVBUF);
    227 		}
    228 		FREEIT(ik->ik_io);
    229 		FREEIT(ik->ik_iomem);
    230 		FREEIT(ik->ik_irq);
    231 		FREEIT(ik->ik_drq);
    232 		free(ik, M_DEVBUF);
    233 	}
    234 
    235 #undef FREEIT
    236 }
    237 
    238 static int
    239 checkattachargs(struct isa_attach_args *ia, const int *loc)
    240 {
    241 	int i;
    242 
    243 	if (ia->ia_nio == 0) {
    244 		if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT)
    245 			return (0);
    246 	} else {
    247 		if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT &&
    248 		    loc[ISACF_PORT] != ia->ia_io[0].ir_addr)
    249 			return (0);
    250 	}
    251 
    252 	if (ia->ia_niomem == 0) {
    253 		if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT)
    254 			return (0);
    255 	} else {
    256 		if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT &&
    257 		    loc[ISACF_IOMEM] != ia->ia_iomem[0].ir_addr)
    258 			return (0);
    259 	}
    260 
    261 	if (ia->ia_nirq == 0) {
    262 		if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT)
    263 			return (0);
    264 	} else {
    265 		if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT &&
    266 		    loc[ISACF_IRQ] != ia->ia_irq[0].ir_irq)
    267 			return (0);
    268 	}
    269 
    270 	if (ia->ia_ndrq == 0) {
    271 		if (loc[ISACF_DRQ] != ISACF_DRQ_DEFAULT)
    272 			return (0);
    273 		if (loc[ISACF_DRQ2] != ISACF_DRQ2_DEFAULT)
    274 			return (0);
    275 	} else {
    276 		for (i = 0; i < 2; i++) {
    277 			if (i == ia->ia_ndrq)
    278 				break;
    279 			if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT &&
    280 			    loc[ISACF_DRQ + i] != ia->ia_drq[i].ir_drq)
    281 				return (0);
    282 		}
    283 		for (; i < 2; i++) {
    284 			if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT)
    285 				return (0);
    286 		}
    287 	}
    288 
    289 	return (1);
    290 }
    291 
    292 int
    293 isasubmatch(struct device *parent, struct cfdata *cf,
    294     const int *ldesc, void *aux)
    295 {
    296 	struct isa_attach_args *ia = aux;
    297 
    298 	if (!checkattachargs(ia, cf->cf_loc))
    299 		return (0);
    300 
    301 	return (config_match(parent, cf, aux));
    302 }
    303 
    304 int
    305 isaprint(void *aux, const char *isa)
    306 {
    307 	struct isa_attach_args *ia = aux;
    308 	const char *sep;
    309 	int i;
    310 
    311 	/*
    312 	 * This block of code only fires if we have a direct-config'd
    313 	 * device for which there is no driver match.
    314 	 */
    315 	if (isa != NULL) {
    316 		struct isa_pnpname *ipn;
    317 
    318 		if (ia->ia_pnpname != NULL)
    319 			aprint_normal("%s", ia->ia_pnpname);
    320 		if ((ipn = ia->ia_pnpcompatnames) != NULL) {
    321 			aprint_normal(" (");	/* ) */
    322 			for (sep = ""; ipn != NULL;
    323 			     ipn = ipn->ipn_next, sep = " ") {
    324 				aprint_normal("%s%s", sep, ipn->ipn_name);
    325 			}
    326 	/* ( */		aprint_normal(")");
    327 		}
    328 		aprint_normal(" at %s", isa);
    329 	}
    330 
    331 	if (ia->ia_nio) {
    332 		sep = "";
    333 		aprint_normal(" port ");
    334 		for (i = 0; i < ia->ia_nio; i++) {
    335 			if (ia->ia_io[i].ir_size == 0)
    336 				continue;
    337 			aprint_normal("%s0x%x", sep, ia->ia_io[i].ir_addr);
    338 			if (ia->ia_io[i].ir_size > 1)
    339 				aprint_normal("-0x%x", ia->ia_io[i].ir_addr +
    340 				    ia->ia_io[i].ir_size - 1);
    341 			sep = ",";
    342 		}
    343 	}
    344 
    345 	if (ia->ia_niomem) {
    346 		sep = "";
    347 		aprint_normal(" iomem ");
    348 		for (i = 0; i < ia->ia_niomem; i++) {
    349 			if (ia->ia_iomem[i].ir_size == 0)
    350 				continue;
    351 			aprint_normal("%s0x%x", sep, ia->ia_iomem[i].ir_addr);
    352 			if (ia->ia_iomem[i].ir_size > 1)
    353 				aprint_normal("-0x%x", ia->ia_iomem[i].ir_addr +
    354 				    ia->ia_iomem[i].ir_size - 1);
    355 			sep = ",";
    356 		}
    357 	}
    358 
    359 	if (ia->ia_nirq) {
    360 		sep = "";
    361 		aprint_normal(" irq ");
    362 		for (i = 0; i < ia->ia_nirq; i++) {
    363 			if (ia->ia_irq[i].ir_irq == ISACF_IRQ_DEFAULT)
    364 				continue;
    365 			aprint_normal("%s%d", sep, ia->ia_irq[i].ir_irq);
    366 			sep = ",";
    367 		}
    368 	}
    369 
    370 	if (ia->ia_ndrq) {
    371 		sep = "";
    372 		aprint_normal(" drq ");
    373 		for (i = 0; i < ia->ia_ndrq; i++) {
    374 			if (ia->ia_drq[i].ir_drq == ISACF_DRQ_DEFAULT)
    375 				continue;
    376 			aprint_normal("%s%d", sep, ia->ia_drq[i].ir_drq);
    377 			sep = ",";
    378 		}
    379 	}
    380 
    381 	return (UNCONF);
    382 }
    383 
    384 int
    385 isasearch(struct device *parent, struct cfdata *cf,
    386     const int *slocs, void *aux)
    387 {
    388 	struct isa_io res_io[1];
    389 	struct isa_iomem res_mem[1];
    390 	struct isa_irq res_irq[1];
    391 	struct isa_drq res_drq[2];
    392 	struct isa_softc *sc = (struct isa_softc *)parent;
    393 	struct isa_attach_args ia;
    394 	int flocs[ISACF_NLOCS];
    395 	int tryagain;
    396 
    397 	do {
    398 		ia.ia_pnpname = NULL;
    399 		ia.ia_pnpcompatnames = NULL;
    400 
    401 		res_io[0].ir_addr = cf->cf_loc[ISACF_PORT];
    402 		res_io[0].ir_size = 0;
    403 
    404 		res_mem[0].ir_addr = cf->cf_loc[ISACF_IOMEM];
    405 		res_mem[0].ir_size = cf->cf_loc[ISACF_IOSIZ];
    406 
    407 		res_irq[0].ir_irq =
    408 		    cf->cf_loc[ISACF_IRQ] == 2 ? 9 : cf->cf_loc[ISACF_IRQ];
    409 
    410 		res_drq[0].ir_drq = cf->cf_loc[ISACF_DRQ];
    411 		res_drq[1].ir_drq = cf->cf_loc[ISACF_DRQ2];
    412 
    413 		ia.ia_iot = sc->sc_iot;
    414 		ia.ia_memt = sc->sc_memt;
    415 		ia.ia_dmat = sc->sc_dmat;
    416 		ia.ia_ic = sc->sc_ic;
    417 
    418 		ia.ia_io = res_io;
    419 		ia.ia_nio = 1;
    420 
    421 		ia.ia_iomem = res_mem;
    422 		ia.ia_niomem = 1;
    423 
    424 		ia.ia_irq = res_irq;
    425 		ia.ia_nirq = 1;
    426 
    427 		ia.ia_drq = res_drq;
    428 		ia.ia_ndrq = 2;
    429 
    430 		if (!checkattachargs(&ia, slocs))
    431 			return (0);
    432 
    433 		tryagain = 0;
    434 		if (config_match(parent, cf, &ia) > 0) {
    435 			/*
    436 			 * This is not necessary for detach, but might
    437 			 * still be useful to collect device information.
    438 			 */
    439 			flocs[ISACF_PORT] = ia.ia_io[0].ir_addr;
    440 			flocs[ISACF_SIZE] = ia.ia_io[0].ir_size;
    441 			flocs[ISACF_IOMEM] = ia.ia_iomem[0].ir_addr;
    442 			flocs[ISACF_IOSIZ] = ia.ia_iomem[0].ir_size;
    443 			flocs[ISACF_IRQ] = ia.ia_irq[0].ir_irq;
    444 			flocs[ISACF_DRQ] = ia.ia_drq[0].ir_drq;
    445 			flocs[ISACF_DRQ2] = ia.ia_drq[1].ir_drq;
    446 			config_attach_loc(parent, cf, flocs, &ia, isaprint);
    447 			tryagain = (cf->cf_fstate == FSTATE_STAR);
    448 		}
    449 	} while (tryagain);
    450 
    451 	return (0);
    452 }
    453 
    454 const char *
    455 isa_intr_typename(int type)
    456 {
    457 
    458 	switch (type) {
    459 	case IST_NONE:
    460 		return ("none");
    461 	case IST_PULSE:
    462 		return ("pulsed");
    463 	case IST_EDGE:
    464 		return ("edge-triggered");
    465 	case IST_LEVEL:
    466 		return ("level-triggered");
    467 	default:
    468 		panic("isa_intr_typename: invalid type %d", type);
    469 	}
    470 }
    471 
    472 struct isa_generic_power {
    473 	pnp_state_t p_cur_state;
    474 	void (*p_resume)(device_t);
    475 	void (*p_suspend)(device_t);
    476 };
    477 
    478 static pnp_status_t
    479 isa_generic_power(device_t dv, pnp_request_t req, void *opaque)
    480 {
    481 	struct isa_generic_power *arg = device_power_private(dv);
    482 	pnp_state_t *state;
    483 	pnp_capabilities_t *caps;
    484 
    485 	switch (req) {
    486 	case PNP_REQUEST_GET_CAPABILITIES:
    487 		caps = opaque;
    488 
    489 		caps->state = PNP_STATE_D0 | PNP_STATE_D3;
    490 		return PNP_STATUS_SUCCESS;
    491 
    492 	case PNP_REQUEST_SET_STATE:
    493 		state = opaque;
    494 		switch (*state) {
    495 		case PNP_STATE_D0:
    496 			if (arg->p_resume)
    497 				(*arg->p_resume)(dv);
    498 			break;
    499 		case PNP_STATE_D3:
    500 			if (arg->p_suspend)
    501 				(*arg->p_suspend)(dv);
    502 			break;
    503 		default:
    504 			return PNP_STATUS_UNSUPPORTED;
    505 		}
    506 		arg->p_cur_state = *state;
    507 		return PNP_STATUS_SUCCESS;
    508 
    509 	case PNP_REQUEST_GET_STATE:
    510 		state = opaque;
    511 		*state = arg->p_cur_state;
    512 		return PNP_STATUS_SUCCESS;
    513 
    514 	default:
    515 		return PNP_STATUS_UNSUPPORTED;
    516 	}
    517 }
    518 
    519 static pnp_status_t
    520 isa_generic_power_register_internal(device_t dv,
    521     void (*p_suspend)(device_t), void (*p_resume)(device_t))
    522 {
    523 	struct isa_generic_power *arg = device_power_private(dv);
    524 
    525 	arg->p_cur_state = PNP_STATE_D0;
    526 	arg->p_resume = p_resume;
    527 	arg->p_suspend = p_suspend;
    528 
    529 	return pnp_register(dv, isa_generic_power);
    530 }
    531 
    532 static void
    533 isa_generic_power_deregister_internal(device_t dv)
    534 {
    535 	pnp_deregister(dv);
    536 }
    537 
    538 pnp_status_t
    539 isa_generic_power_register(device_t dv,
    540     void (*p_suspend)(device_t), void (*p_resume)(device_t))
    541 {
    542 	struct isa_generic_power *arg;
    543 	pnp_status_t status;
    544 
    545 	arg = malloc(sizeof(*arg), M_DEVBUF, M_ZERO | M_WAITOK);
    546 	device_power_set_private(dv, arg);
    547 
    548 	status = isa_generic_power_register_internal(dv, p_suspend, p_resume);
    549 	if (status != PNP_STATUS_SUCCESS) {
    550 		free(arg, M_DEVBUF);
    551 		device_power_set_private(dv, NULL);
    552 	}
    553 	return status;
    554 }
    555 
    556 void
    557 isa_generic_power_deregister(device_t dv)
    558 {
    559 	struct isa_generic_power *arg = device_power_private(dv);
    560 
    561 	if (arg == NULL)
    562 		return;
    563 
    564 	isa_generic_power_deregister_internal(dv);
    565 	free(arg, M_DEVBUF);
    566 }
    567