Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.57
      1 /*	$NetBSD: pcmcia.c,v 1.57 2004/08/10 18:43:50 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  */
     20 
     21 /*
     22  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
     23  *
     24  * Redistribution and use in source and binary forms, with or without
     25  * modification, are permitted provided that the following conditions
     26  * are met:
     27  * 1. Redistributions of source code must retain the above copyright
     28  *    notice, this list of conditions and the following disclaimer.
     29  * 2. Redistributions in binary form must reproduce the above copyright
     30  *    notice, this list of conditions and the following disclaimer in the
     31  *    documentation and/or other materials provided with the distribution.
     32  * 3. All advertising materials mentioning features or use of this software
     33  *    must display the following acknowledgement:
     34  *	This product includes software developed by Marc Horowitz.
     35  * 4. The name of the author may not be used to endorse or promote products
     36  *    derived from this software without specific prior written permission.
     37  *
     38  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     39  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     41  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     42  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     43  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     47  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: pcmcia.c,v 1.57 2004/08/10 18:43:50 mycroft Exp $");
     52 
     53 #include "opt_pcmciaverbose.h"
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/device.h>
     58 
     59 #include <dev/pcmcia/pcmciareg.h>
     60 #include <dev/pcmcia/pcmciachip.h>
     61 #include <dev/pcmcia/pcmciavar.h>
     62 #ifdef IT8368E_LEGACY_MODE /* XXX -uch */
     63 #include <arch/hpcmips/dev/it8368var.h>
     64 #endif
     65 
     66 #include "locators.h"
     67 
     68 #ifdef PCMCIADEBUG
     69 int	pcmcia_debug = 0;
     70 #define	DPRINTF(arg) if (pcmcia_debug) printf arg
     71 #else
     72 #define	DPRINTF(arg)
     73 #endif
     74 
     75 #ifdef PCMCIAVERBOSE
     76 int	pcmcia_verbose = 1;
     77 #else
     78 int	pcmcia_verbose = 0;
     79 #endif
     80 
     81 int	pcmcia_match __P((struct device *, struct cfdata *, void *));
     82 int	pcmcia_submatch __P((struct device *, struct cfdata *, void *));
     83 void	pcmcia_attach __P((struct device *, struct device *, void *));
     84 int	pcmcia_print __P((void *, const char *));
     85 
     86 CFATTACH_DECL(pcmcia, sizeof(struct pcmcia_softc),
     87     pcmcia_match, pcmcia_attach, NULL, NULL);
     88 
     89 int
     90 pcmcia_ccr_read(pf, ccr)
     91 	struct pcmcia_function *pf;
     92 	int ccr;
     93 {
     94 
     95 	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
     96 	    pf->pf_ccr_offset + ccr * 2));
     97 }
     98 
     99 void
    100 pcmcia_ccr_write(pf, ccr, val)
    101 	struct pcmcia_function *pf;
    102 	int ccr;
    103 	int val;
    104 {
    105 
    106 	if (pf->ccr_mask & (1 << ccr)) {
    107 		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
    108 		    pf->pf_ccr_offset + ccr * 2, val);
    109 	}
    110 }
    111 
    112 int
    113 pcmcia_match(parent, match, aux)
    114 	struct device *parent;
    115 	struct cfdata *match;
    116 	void *aux;
    117 {
    118 	struct pcmciabus_attach_args *paa = aux;
    119 
    120 	if (strcmp(paa->paa_busname, match->cf_name)) {
    121 	    return 0;
    122 	}
    123 	/* if the autoconfiguration got this far, there's a socket here */
    124 	return (1);
    125 }
    126 
    127 void
    128 pcmcia_attach(parent, self, aux)
    129 	struct device *parent, *self;
    130 	void *aux;
    131 {
    132 	struct pcmciabus_attach_args *paa = aux;
    133 	struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
    134 
    135 	printf("\n");
    136 
    137 	sc->pct = paa->pct;
    138 	sc->pch = paa->pch;
    139 	sc->iobase = paa->iobase;
    140 	sc->iosize = paa->iosize;
    141 
    142 	sc->ih = NULL;
    143 }
    144 
    145 int
    146 pcmcia_card_attach(dev)
    147 	struct device *dev;
    148 {
    149 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    150 	struct pcmcia_function *pf;
    151 	struct pcmcia_attach_args paa;
    152 	int attached;
    153 
    154 	/*
    155 	 * this is here so that when socket_enable calls gettype, trt happens
    156 	 */
    157 	SIMPLEQ_FIRST(&sc->card.pf_head) = NULL;
    158 
    159 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    160 
    161 	pcmcia_read_cis(sc);
    162 
    163 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    164 
    165 	pcmcia_check_cis_quirks(sc);
    166 
    167 	/*
    168 	 * bail now if the card has no functions, or if there was an error in
    169 	 * the cis.
    170 	 */
    171 
    172 	if (sc->card.error)
    173 		return (1);
    174 	if (SIMPLEQ_EMPTY(&sc->card.pf_head))
    175 		return (1);
    176 
    177 	if (pcmcia_verbose)
    178 		pcmcia_print_cis(sc);
    179 
    180 	attached = 0;
    181 
    182 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    183 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    184 			continue;
    185 
    186 #ifdef DIAGNOSTIC
    187 		if (pf->child != NULL) {
    188 			printf("%s: %s still attached to function %d!\n",
    189 			    sc->dev.dv_xname, pf->child->dv_xname,
    190 			    pf->number);
    191 			panic("pcmcia_card_attach");
    192 		}
    193 #endif
    194 		pf->sc = sc;
    195 		pf->child = NULL;
    196 		pf->cfe = NULL;
    197 		pf->pf_ih = NULL;
    198 	}
    199 
    200 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    201 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    202 			continue;
    203 
    204 		paa.manufacturer = sc->card.manufacturer;
    205 		paa.product = sc->card.product;
    206 		paa.card = &sc->card;
    207 		paa.pf = pf;
    208 
    209 		if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
    210 		    pcmcia_submatch)) != NULL)
    211 			attached++;
    212 	}
    213 
    214 	return (attached ? 0 : 1);
    215 }
    216 
    217 void
    218 pcmcia_card_detach(dev, flags)
    219 	struct device *dev;
    220 	int flags;		/* DETACH_* flags */
    221 {
    222 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    223 	struct pcmcia_function *pf;
    224 	int error;
    225 
    226 	/*
    227 	 * We are running on either the PCMCIA socket's event thread
    228 	 * or in user context detaching a device by user request.
    229 	 */
    230 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    231 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    232 			continue;
    233 		if (pf->child == NULL)
    234 			continue;
    235 		DPRINTF(("%s: detaching %s (function %d)\n",
    236 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    237 		if ((error = config_detach(pf->child, flags)) != 0) {
    238 			printf("%s: error %d detaching %s (function %d)\n",
    239 			    sc->dev.dv_xname, error, pf->child->dv_xname,
    240 			    pf->number);
    241 		} else
    242 			pf->child = NULL;
    243 	}
    244 
    245 	if (sc->sc_enabled_count != 0) {
    246 #ifdef DIAGNOSTIC
    247 		printf("pcmcia_card_detach: enabled_count should be 0 here??\n");
    248 #endif
    249 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    250 		sc->sc_enabled_count = 0;
    251 	}
    252 }
    253 
    254 void
    255 pcmcia_card_deactivate(dev)
    256 	struct device *dev;
    257 {
    258 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    259 	struct pcmcia_function *pf;
    260 
    261 	/*
    262 	 * We're in the chip's card removal interrupt handler.
    263 	 * Deactivate the child driver.  The PCMCIA socket's
    264 	 * event thread will run later to finish the detach.
    265 	 */
    266 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    267 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    268 			continue;
    269 		if (pf->child == NULL)
    270 			continue;
    271 		DPRINTF(("%s: deactivating %s (function %d)\n",
    272 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    273 		config_deactivate(pf->child);
    274 	}
    275 }
    276 
    277 int
    278 pcmcia_submatch(parent, cf, aux)
    279 	struct device *parent;
    280 	struct cfdata *cf;
    281 	void *aux;
    282 {
    283 	struct pcmcia_attach_args *paa = aux;
    284 
    285 	if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
    286 	    cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
    287 		return (0);
    288 
    289 	return (config_match(parent, cf, aux));
    290 }
    291 
    292 int
    293 pcmcia_print(arg, pnp)
    294 	void *arg;
    295 	const char *pnp;
    296 {
    297 	struct pcmcia_attach_args *pa = arg;
    298 	struct pcmcia_softc *sc = pa->pf->sc;
    299 	struct pcmcia_card *card = &sc->card;
    300 	char devinfo[256];
    301 
    302 	if (pnp)
    303 		aprint_normal("%s", pnp);
    304 
    305 	pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
    306 
    307 	aprint_normal(" function %d: %s\n", pa->pf->number, devinfo);
    308 
    309 	return (UNCONF);
    310 }
    311 
    312 void
    313 pcmcia_devinfo(card, showhex, cp, cplen)
    314 	struct pcmcia_card *card;
    315 	int showhex;
    316 	char *cp;
    317 	size_t cplen;
    318 {
    319 	int i, n;
    320 
    321 	if (cplen > 1) {
    322 		*cp++ = '<';
    323 		*cp = '\0';
    324 		cplen--;
    325 	}
    326 
    327 	for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
    328 		n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
    329 		        card->cis1_info[i]);
    330 		cp += n;
    331 		if (cplen < n)
    332 			return;
    333 		cplen -= n;
    334 	}
    335 
    336 	if (cplen > 1) {
    337 		*cp++ = '>';
    338 		*cp = '\0';
    339 		cplen--;
    340 	}
    341 
    342 	if (showhex && cplen > 1)
    343 		snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
    344 		    card->manufacturer, card->product);
    345 }
    346 
    347 const void *
    348 pcmcia_product_lookup(pa, tab, nent, ent_size, matchfn)
    349 	struct pcmcia_attach_args *pa;
    350 	const void *tab;
    351 	size_t nent;
    352 	size_t ent_size;
    353 	pcmcia_product_match_fn matchfn;
    354 {
    355         const struct pcmcia_product *pp;
    356 	int n;
    357 	int matches;
    358 
    359 #ifdef DIAGNOSTIC
    360 	if (sizeof *pp > ent_size)
    361 		panic("pcmcia_product_lookup: bogus ent_size %ld",
    362 		      (long) ent_size);
    363 #endif
    364 
    365         for (pp = tab, n = nent; n; pp = (const struct pcmcia_product *)
    366 	      ((const char *)pp + ent_size), n--) {
    367 		/* see if it matches vendor/product */
    368 		if ((pp->pp_vendor != PCMCIA_VENDOR_INVALID &&
    369 		     pp->pp_vendor == pa->manufacturer) &&
    370 		    (pp->pp_product != PCMCIA_PRODUCT_INVALID &&
    371 		     pp->pp_product == pa->product))
    372 			matches = 1;
    373 		else if ((pp->pp_cisinfo[0] && pa->card->cis1_info[0] &&
    374 		          !strcmp(pp->pp_cisinfo[0], pa->card->cis1_info[0])) &&
    375 		         (pp->pp_cisinfo[1] && pa->card->cis1_info[1] &&
    376 		          !strcmp(pp->pp_cisinfo[1], pa->card->cis1_info[1])) &&
    377 		         (!pp->pp_cisinfo[2] || (pa->card->cis1_info[2] &&
    378 		           !strcmp(pp->pp_cisinfo[2], pa->card->cis1_info[2]))) &&
    379 		         (!pp->pp_cisinfo[3] || (pa->card->cis1_info[3] &&
    380 		           !strcmp(pp->pp_cisinfo[3], pa->card->cis1_info[3]))))
    381 			matches = 1;
    382 		else
    383 			matches = 0;
    384 
    385 		/* if a separate match function is given, let it override */
    386 		if (matchfn)
    387 			matches = (*matchfn)(pa, pp, matches);
    388 
    389 		if (matches)
    390                         return (pp);
    391         }
    392         return (0);
    393 }
    394 
    395 int
    396 pcmcia_card_gettype(dev)
    397 	struct device  *dev;
    398 {
    399 	struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
    400 	struct pcmcia_function *pf;
    401 
    402 	/*
    403 	 * set the iftype to memory if this card has no functions (not yet
    404 	 * probed), or only one function, and that is not initialized yet or
    405 	 * that is memory.
    406 	 */
    407 	pf = SIMPLEQ_FIRST(&sc->card.pf_head);
    408 	if (pf == NULL ||
    409 	    (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
    410 	    (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
    411 		return (PCMCIA_IFTYPE_MEMORY);
    412 	else
    413 		return (PCMCIA_IFTYPE_IO);
    414 }
    415 
    416 /*
    417  * Initialize a PCMCIA function.  May be called as long as the function is
    418  * disabled.
    419  */
    420 void
    421 pcmcia_function_init(pf, cfe)
    422 	struct pcmcia_function *pf;
    423 	struct pcmcia_config_entry *cfe;
    424 {
    425 	if (pf->pf_flags & PFF_ENABLED)
    426 		panic("pcmcia_function_init: function is enabled");
    427 
    428 	/* Remember which configuration entry we are using. */
    429 	pf->cfe = cfe;
    430 }
    431 
    432 void pcmcia_socket_enable(dev)
    433 	struct device *dev;
    434 {
    435 	struct pcmcia_softc *sc = (void *)dev;
    436 
    437 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    438 }
    439 
    440 void pcmcia_socket_disable(dev)
    441 	struct device *dev;
    442 {
    443 	struct pcmcia_softc *sc = (void *)dev;
    444 
    445 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    446 }
    447 
    448 /* Enable a PCMCIA function */
    449 int
    450 pcmcia_function_enable(pf)
    451 	struct pcmcia_function *pf;
    452 {
    453 	struct pcmcia_softc *sc = pf->sc;
    454 	struct pcmcia_function *tmp;
    455 	int reg;
    456 	int error;
    457 
    458 	if (pf->cfe == NULL)
    459 		panic("pcmcia_function_enable: function not initialized");
    460 
    461 	/*
    462 	 * Increase the reference count on the socket, enabling power, if
    463 	 * necessary.
    464 	 */
    465 	if (sc->sc_enabled_count++ == 0)
    466 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
    467 	DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname,
    468 		 sc->sc_enabled_count));
    469 
    470 	if (pf->pf_flags & PFF_ENABLED) {
    471 		/*
    472 		 * Don't do anything if we're already enabled.
    473 		 */
    474 		return (0);
    475 	}
    476 
    477 	/*
    478 	 * it's possible for different functions' CCRs to be in the same
    479 	 * underlying page.  Check for that.
    480 	 */
    481 
    482 	SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    483 		if ((tmp->pf_flags & PFF_ENABLED) &&
    484 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    485 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    486 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    487 		      tmp->pf_ccr_realsize))) {
    488 			pf->pf_ccrt = tmp->pf_ccrt;
    489 			pf->pf_ccrh = tmp->pf_ccrh;
    490 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    491 
    492 			/*
    493 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    494 			 * tmp->ccr_base) + pf->ccr_base;
    495 			 */
    496 			pf->pf_ccr_offset =
    497 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    498 			    tmp->ccr_base;
    499 			pf->pf_ccr_window = tmp->pf_ccr_window;
    500 			break;
    501 		}
    502 	}
    503 
    504 	if (tmp == NULL) {
    505 		error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh);
    506 		if (error)
    507 			goto bad;
    508 
    509 		error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    510 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    511 		    &pf->pf_ccr_window);
    512 		if (error) {
    513 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    514 			goto bad;
    515 		}
    516 	}
    517 
    518 	if (pcmcia_mfc(sc)) {
    519 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    520 				 (pf->pf_mfc_iobase[0] >> 0) & 0xff);
    521 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    522 				 (pf->pf_mfc_iobase[0] >> 8) & 0xff);
    523 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
    524 				 (pf->pf_mfc_iobase[1] >> 0) & 0xff);
    525 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
    526 				 (pf->pf_mfc_iobase[1] >> 8) & 0xff);
    527 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, pf->pf_mfc_iomask);
    528 	}
    529 
    530 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    531 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    532 	if (pcmcia_mfc(sc)) {
    533 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    534 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    535 		if (pf->pf_ih)
    536 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    537 
    538 	}
    539 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    540 
    541 	reg = 0;
    542 	if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
    543 		reg |= PCMCIA_CCR_STATUS_IOIS8;
    544 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    545 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    546 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    547 
    548 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    549 
    550 #ifdef PCMCIADEBUG
    551 	if (pcmcia_debug) {
    552 		SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    553 			printf("%s: function %d CCR at %d offset %lx: "
    554 			       "%x %x %x %x, %x %x %x %x, %x\n",
    555 			       tmp->sc->dev.dv_xname, tmp->number,
    556 			       tmp->pf_ccr_window,
    557 			       (unsigned long) tmp->pf_ccr_offset,
    558 			       pcmcia_ccr_read(tmp, 0),
    559 			       pcmcia_ccr_read(tmp, 1),
    560 			       pcmcia_ccr_read(tmp, 2),
    561 			       pcmcia_ccr_read(tmp, 3),
    562 
    563 			       pcmcia_ccr_read(tmp, 5),
    564 			       pcmcia_ccr_read(tmp, 6),
    565 			       pcmcia_ccr_read(tmp, 7),
    566 			       pcmcia_ccr_read(tmp, 8),
    567 
    568 			       pcmcia_ccr_read(tmp, 9));
    569 		}
    570 	}
    571 #endif
    572 
    573 	pf->pf_flags |= PFF_ENABLED;
    574 
    575 #ifdef IT8368E_LEGACY_MODE
    576 	/* return to I/O mode */
    577 	it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
    578 #endif
    579 	return (0);
    580 
    581 bad:
    582 	/*
    583 	 * Decrement the reference count, and power down the socket, if
    584 	 * necessary.
    585 	 */
    586 	if (--sc->sc_enabled_count == 0)
    587 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    588 	DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname,
    589 		 sc->sc_enabled_count));
    590 	printf("%s: couldn't map the CCR\n", pf->child->dv_xname);
    591 
    592 	return (error);
    593 }
    594 
    595 /* Disable PCMCIA function. */
    596 void
    597 pcmcia_function_disable(pf)
    598 	struct pcmcia_function *pf;
    599 {
    600 	struct pcmcia_function *tmp;
    601 	int reg;
    602 
    603 	if (pf->cfe == NULL)
    604 		panic("pcmcia_function_enable: function not initialized");
    605 
    606 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    607 		/*
    608 		 * Don't do anything but decrement if we're already disabled.
    609 		 */
    610 		goto out;
    611 	}
    612 
    613 	if (pcmcia_mfc(pf->sc)) {
    614 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    615 		reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
    616 			 PCMCIA_CCR_OPTION_ADDR_DECODE|
    617 		         PCMCIA_CCR_OPTION_IREQ_ENABLE);
    618 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    619 	}
    620 
    621 	/*
    622 	 * it's possible for different functions' CCRs to be in the same
    623 	 * underlying page.  Check for that.  Note we mark us as disabled
    624 	 * first to avoid matching ourself.
    625 	 */
    626 
    627 	pf->pf_flags &= ~PFF_ENABLED;
    628 	SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
    629 		if ((tmp->pf_flags & PFF_ENABLED) &&
    630 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    631 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    632 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    633 			break;
    634 	}
    635 
    636 	/* Not used by anyone else; unmap the CCR. */
    637 	if (tmp == NULL) {
    638 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    639 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    640 	}
    641 
    642 out:
    643 	/*
    644 	 * Decrement the reference count, and power down the socket, if
    645 	 * necessary.
    646 	 */
    647 	if (--pf->sc->sc_enabled_count == 0)
    648 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    649 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    650 		 pf->sc->sc_enabled_count));
    651 }
    652 
    653 int
    654 pcmcia_io_map(pf, width, pcihp, windowp)
    655 	struct pcmcia_function *pf;
    656 	int width;
    657 	struct pcmcia_io_handle *pcihp;
    658 	int *windowp;
    659 {
    660 	int error;
    661 
    662 	if (pf->pf_flags & PFF_ENABLED)
    663 		printf("pcmcia_io_map: function is enabled!\n");
    664 
    665 	error = pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
    666 	    width, 0, pcihp->size, pcihp, windowp);
    667 	if (error)
    668 		return (error);
    669 
    670 	/*
    671 	 * XXX in the multifunction multi-iospace-per-function case, this
    672 	 * needs to cooperate with io_alloc to make sure that the spaces
    673 	 * don't overlap, and that the ccr's are set correctly
    674 	 */
    675 
    676 	if (pcmcia_mfc(pf->sc)) {
    677 		int win;
    678 		long iomask;
    679 
    680 		/* round up to nearest (2^n)-1 */
    681 		for (iomask = 1; iomask < pcihp->size; iomask <<= 1)
    682 			;
    683 		iomask--;
    684 
    685 		win = pf->pf_mfc_windows;
    686 		KASSERT(win < 2);
    687 printf("win %d = addr %lx size %lx iomask %lx\n", win, (long)pcihp->addr, (long)pcihp->size, (long)iomask);
    688 		pf->pf_mfc_iobase[win] = pcihp->addr;
    689 		pf->pf_mfc_iomask = iomask;
    690 	}
    691 
    692 	return (0);
    693 }
    694 
    695 void
    696 pcmcia_io_unmap(pf, window)
    697 	struct pcmcia_function *pf;
    698 	int window;
    699 {
    700 
    701 	if (pf->pf_flags & PFF_ENABLED)
    702 		printf("pcmcia_io_unmap: function is enabled!\n");
    703 
    704 	if (pcmcia_mfc(pf->sc)) {
    705 		/*
    706 		 * Don't bother trying to unmap windows in the CCR here,
    707 		 * because we generally get called when a card has been
    708 		 * ejected, and the CCR isn't there any more.
    709 		 */
    710 		--pf->pf_mfc_windows;
    711 	}
    712 
    713 	pcmcia_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
    714 }
    715 
    716 void *
    717 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
    718 	struct pcmcia_function *pf;
    719 	int ipl;
    720 	int (*ih_fct) __P((void *));
    721 	void *ih_arg;
    722 {
    723 
    724 	if (pf->pf_flags & PFF_ENABLED)
    725 		printf("pcmcia_intr_establish: function is enabled!\n");
    726 	if (pf->pf_ih)
    727 		panic("pcmcia_intr_establish: already done\n");
    728 
    729 	pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
    730 	    pf, ipl, ih_fct, ih_arg);
    731 	if (!pf->pf_ih)
    732 		printf("%s: interrupt establish failed\n", pf->child->dv_xname);
    733 	return (pf->pf_ih);
    734 }
    735 
    736 void
    737 pcmcia_intr_disestablish(pf, ih)
    738 	struct pcmcia_function *pf;
    739 	void *ih;
    740 {
    741 
    742 	if (pf->pf_flags & PFF_ENABLED)
    743 		printf("pcmcia_intr_disestablish: function is enabled!\n");
    744 	if (!pf->pf_ih)
    745 		panic("pcmcia_intr_distestablish: already done\n");
    746 
    747 	pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
    748 	pf->pf_ih = 0;
    749 }
    750 
    751 int
    752 pcmcia_config_alloc(pf, cfe)
    753 	struct pcmcia_function *pf;
    754 	struct pcmcia_config_entry *cfe;
    755 {
    756 	int error;
    757 	int n, m;
    758 
    759 	for (n = 0; n < cfe->num_iospace; n++) {
    760 		bus_addr_t start = cfe->iospace[n].start;
    761 		bus_size_t length = cfe->iospace[n].length;
    762 		bus_size_t align = cfe->iomask ? (1 << cfe->iomask) :
    763 		    length;
    764 		bus_size_t skew = start & (align - 1);
    765 
    766 		if ((start - skew) == 0 && align < 0x400) {
    767 			if (skew)
    768 				printf("Drats!  I need a skew!\n");
    769 			start = 0;
    770 		}
    771 
    772 		DPRINTF(("pcmcia_config_alloc: io %d start=%lx length=%lx align=%lx skew=%lx\n",
    773 		    n, (long)start, (long)length, (long)align, (long)skew));
    774 
    775 		error = pcmcia_io_alloc(pf, start, length, align,
    776 		    &cfe->iospace[n].handle);
    777 		if (error)
    778 			break;
    779 	}
    780 	if (n < cfe->num_iospace) {
    781 		for (m = 0; m < n; m++)
    782 			pcmcia_io_free(pf, &cfe->iospace[m].handle);
    783 		return (error);
    784 	}
    785 
    786 	for (n = 0; n < cfe->num_memspace; n++) {
    787 		bus_size_t length = cfe->memspace[n].length;
    788 
    789 		DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
    790 		    (long)length));
    791 
    792 		error = pcmcia_mem_alloc(pf, length, &cfe->memspace[n].handle);
    793 		if (error)
    794 			break;
    795 	}
    796 	if (n < cfe->num_memspace) {
    797 		for (m = 0; m < cfe->num_iospace; m++)
    798 			pcmcia_io_free(pf, &cfe->iospace[m].handle);
    799 		for (m = 0; m < n; m++)
    800 			pcmcia_mem_free(pf, &cfe->memspace[m].handle);
    801 		return (error);
    802 	}
    803 
    804 	/* This one's good! */
    805 	return (0);
    806 }
    807 
    808 void
    809 pcmcia_config_free(pf)
    810 	struct pcmcia_function *pf;
    811 {
    812 	struct pcmcia_config_entry *cfe = pf->cfe;
    813 	int m;
    814 
    815 	for (m = 0; m < cfe->num_iospace; m++)
    816 		pcmcia_io_free(pf, &cfe->iospace[m].handle);
    817 	for (m = 0; m < cfe->num_memspace; m++)
    818 		pcmcia_mem_free(pf, &cfe->memspace[m].handle);
    819 }
    820 
    821 int
    822 pcmcia_config_map(pf)
    823 	struct pcmcia_function *pf;
    824 {
    825 	struct pcmcia_config_entry *cfe = pf->cfe;
    826 	int error;
    827 	int n, m;
    828 
    829 	for (n = 0; n < cfe->num_iospace; n++) {
    830 		int width;
    831 
    832 		switch (cfe->flags & (PCMCIA_CFE_IO8|PCMCIA_CFE_IO16)) {
    833 		case PCMCIA_CFE_IO8:
    834 			width = PCMCIA_WIDTH_IO8;
    835 			break;
    836 		case PCMCIA_CFE_IO16:
    837 			width = PCMCIA_WIDTH_IO16;
    838 			break;
    839 		case PCMCIA_CFE_IO8|PCMCIA_CFE_IO16:
    840 		default:
    841 			width = PCMCIA_WIDTH_AUTO;
    842 			break;
    843 		}
    844 		error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle,
    845 		    &cfe->iospace[n].window);
    846 		if (error)
    847 			break;
    848 	}
    849 	if (n < cfe->num_iospace) {
    850 		for (m = 0; m < n; m++)
    851 			pcmcia_io_unmap(pf, cfe->iospace[m].window);
    852 		return (error);
    853 	}
    854 
    855 	for (n = 0; n < cfe->num_memspace; n++) {
    856 		bus_size_t length = cfe->memspace[n].length;
    857 		int width;
    858 
    859 		DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
    860 		    (long)length));
    861 
    862 		/*XXX*/
    863 		width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON;
    864 		error = pcmcia_mem_map(pf, width, 0, length,
    865 		    &cfe->memspace[n].handle, &cfe->memspace[n].offset,
    866 		    &cfe->memspace[n].window);
    867 		if (error)
    868 			break;
    869 	}
    870 	if (n < cfe->num_memspace) {
    871 		for (m = 0; m < cfe->num_iospace; m++)
    872 			pcmcia_io_unmap(pf, cfe->iospace[m].window);
    873 		for (m = 0; m < n; m++)
    874 			pcmcia_mem_unmap(pf, cfe->memspace[m].window);
    875 		return (error);
    876 	}
    877 
    878 	/* This one's good! */
    879 	return (0);
    880 }
    881 
    882 void
    883 pcmcia_config_unmap(pf)
    884 	struct pcmcia_function *pf;
    885 {
    886 	struct pcmcia_config_entry *cfe = pf->cfe;
    887 	int m;
    888 
    889 	for (m = 0; m < cfe->num_iospace; m++)
    890 		pcmcia_io_unmap(pf, cfe->iospace[m].window);
    891 	for (m = 0; m < cfe->num_memspace; m++)
    892 		pcmcia_mem_unmap(pf, cfe->memspace[m].window);
    893 }
    894 
    895 int
    896 pcmcia_function_configure(pf, validator)
    897 	struct pcmcia_function *pf;
    898 	int (*validator) __P((struct pcmcia_config_entry *));
    899 {
    900 	struct pcmcia_config_entry *cfe;
    901 	int error = ENOENT;
    902 
    903 	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
    904 		error = validator(cfe);
    905 		if (error)
    906 			continue;
    907 		error = pcmcia_config_alloc(pf, cfe);
    908 		if (!error)
    909 			break;
    910 	}
    911 	if (!cfe) {
    912 		DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n",
    913 		    error));
    914 		return (error);
    915 	}
    916 
    917 	/* Remember which configuration entry we are using. */
    918 	pf->cfe = cfe;
    919 
    920 	error = pcmcia_config_map(pf);
    921 	if (error) {
    922 		DPRINTF(("pcmcia_function_configure: map failed, error=%d\n",
    923 		    error));
    924 		return (error);
    925 	}
    926 
    927 	return (0);
    928 }
    929 
    930 void
    931 pcmcia_function_unconfigure(pf)
    932 	struct pcmcia_function *pf;
    933 {
    934 
    935 	pcmcia_config_unmap(pf);
    936 	pcmcia_config_free(pf);
    937 }
    938