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