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