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