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