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