Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.5
      1 /*	$NetBSD: pcmcia.c,v 1.5 1998/02/01 23:49:02 marc 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 
    279 	/*
    280 	 * set the iftype to memory if this card has no functions (not yet
    281 	 * probed), or only one function, and that is memory.
    282 	 */
    283 	if (sc->card.pf_head.sqh_first == NULL ||
    284 	    (sc->card.pf_head.sqh_first != NULL &&
    285 	     sc->card.pf_head.sqh_first->pf_list.sqe_next == NULL &&
    286 	     (sc->card.pf_head.sqh_first->cfe_head.sqh_first->iftype ==
    287 	      PCMCIA_IFTYPE_MEMORY)))
    288 		return (PCMCIA_IFTYPE_MEMORY);
    289 	else
    290 		return (PCMCIA_IFTYPE_IO);
    291 }
    292 
    293 /*
    294  * Initialize a PCMCIA function.  May be called as long as the function is
    295  * disabled.
    296  */
    297 void
    298 pcmcia_function_init(pf, cfe)
    299 	struct pcmcia_function *pf;
    300 	struct pcmcia_config_entry *cfe;
    301 {
    302 	if (pf->pf_flags & PFF_ENABLED)
    303 		panic("pcmcia_function_init: function is enabled");
    304 
    305 	/* Remember which configuration entry we are using. */
    306 	pf->cfe = cfe;
    307 }
    308 
    309 static inline void pcmcia_socket_enable(pct, pch)
    310      pcmcia_chipset_tag_t pct;
    311      pcmcia_chipset_handle_t *pch;
    312 {
    313 	pcmcia_chip_socket_enable(pct, pch);
    314 }
    315 
    316 static inline void pcmcia_socket_disable(pct, pch)
    317      pcmcia_chipset_tag_t pct;
    318      pcmcia_chipset_handle_t *pch;
    319 {
    320 	pcmcia_chip_socket_disable(pct, pch);
    321 }
    322 
    323 /* Enable a PCMCIA function */
    324 int
    325 pcmcia_function_enable(pf)
    326 	struct pcmcia_function *pf;
    327 {
    328 	struct pcmcia_function *tmp;
    329 	int reg;
    330 
    331 	if (pf->cfe == NULL)
    332 		panic("pcmcia_function_enable: function not initialized");
    333 
    334 	/*
    335 	 * Increase the reference count on the socket, enabling power, if
    336 	 * necessary.
    337 	 */
    338 	if (pf->sc->sc_enabled_count++ == 0)
    339 		pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
    340 	DPRINTF(("%s: ++enabled_count = %d\n", pf->sc->dev.dv_xname,
    341 		 pf->sc->sc_enabled_count));
    342 
    343 	if (pf->pf_flags & PFF_ENABLED) {
    344 		/*
    345 		 * Don't do anything if we're already enabled.
    346 		 */
    347 		return (0);
    348 	}
    349 
    350 	/*
    351 	 * it's possible for different functions' CCRs to be in the same
    352 	 * underlying page.  Check for that.
    353 	 */
    354 
    355 	for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
    356 	    tmp = tmp->pf_list.sqe_next) {
    357 		if ((tmp->pf_flags & PFF_ENABLED) &&
    358 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    359 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    360 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    361 		      tmp->pf_ccr_realsize))) {
    362 			pf->pf_ccrt = tmp->pf_ccrt;
    363 			pf->pf_ccrh = tmp->pf_ccrh;
    364 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    365 
    366 			/*
    367 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    368 			 * tmp->ccr_base) + pf->ccr_base;
    369 			 */
    370 			pf->pf_ccr_offset =
    371 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    372 			    tmp->ccr_base;
    373 			pf->pf_ccr_window = tmp->pf_ccr_window;
    374 			break;
    375 		}
    376 	}
    377 
    378 	if (tmp == NULL) {
    379 		if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
    380 			goto bad;
    381 
    382 		if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    383 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    384 		    &pf->pf_ccr_window)) {
    385 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    386 			goto bad;
    387 		}
    388 	}
    389 
    390 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    391 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    392 	if (pcmcia_mfc(pf->sc)) {
    393 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    394 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    395 		if (pf->ih_fct)
    396 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    397 
    398 	}
    399 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    400 
    401 	reg = 0;
    402 
    403 	if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
    404 		reg |= PCMCIA_CCR_STATUS_IOIS8;
    405 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    406 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    407 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    408 
    409 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    410 
    411 	if (pcmcia_mfc(pf->sc)) {
    412 		long tmp, iosize;
    413 
    414 		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
    415 		/* round up to nearest (2^n)-1 */
    416 		for (iosize = 1; iosize < tmp; iosize <<= 1)
    417 			;
    418 		iosize--;
    419 
    420 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    421 				 pf->pf_mfc_iobase & 0xff);
    422 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    423 				 (pf->pf_mfc_iobase >> 8) & 0xff);
    424 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
    425 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
    426 
    427 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
    428 	}
    429 
    430 	DPRINTF(("%s: function %d CCR at %d offset %lx: "
    431 		 "%x %x %x %x, %x %x %x %x, %x\n",
    432 		 pf->sc->dev.dv_xname, pf->number,
    433 		 pf->pf_ccr_window, pf->pf_ccr_offset,
    434 		 pcmcia_ccr_read(pf, 0x00), pcmcia_ccr_read(pf, 0x02),
    435 		 pcmcia_ccr_read(pf, 0x04), pcmcia_ccr_read(pf, 0x06),
    436 
    437 		 pcmcia_ccr_read(pf, 0x0A), pcmcia_ccr_read(pf, 0x0C),
    438 		 pcmcia_ccr_read(pf, 0x0E), pcmcia_ccr_read(pf, 0x10),
    439 
    440 		 pcmcia_ccr_read(pf, 0x12)));
    441 
    442 	pf->pf_flags |= PFF_ENABLED;
    443 	return (0);
    444 
    445  bad:
    446 	/*
    447 	 * Decrement the reference count, and power down the socket, if
    448 	 * necessary.
    449 	 */
    450 	if (--pf->sc->sc_enabled_count == 0)
    451 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    452 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    453 		 pf->sc->sc_enabled_count));
    454 
    455 	return (1);
    456 }
    457 
    458 /* Disable PCMCIA function. */
    459 void
    460 pcmcia_function_disable(pf)
    461 	struct pcmcia_function *pf;
    462 {
    463 	struct pcmcia_function *tmp;
    464 
    465 	if (pf->cfe == NULL)
    466 		panic("pcmcia_function_enable: function not initialized");
    467 
    468 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    469 		/*
    470 		 * Don't do anything if we're already disabled.
    471 		 */
    472 		return;
    473 	}
    474 
    475 	/*
    476 	 * it's possible for different functions' CCRs to be in the same
    477 	 * underlying page.  Check for that.  Note we mark us as disabled
    478 	 * first to avoid matching ourself.
    479 	 */
    480 
    481 	pf->pf_flags &= ~PFF_ENABLED;
    482 	for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
    483 	    tmp = tmp->pf_list.sqe_next) {
    484 		if ((tmp->pf_flags & PFF_ENABLED) &&
    485 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    486 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    487 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    488 			break;
    489 	}
    490 
    491 	/* Not used by anyone else; unmap the CCR. */
    492 	if (tmp == NULL) {
    493 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    494 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    495 	}
    496 
    497 	/*
    498 	 * Decrement the reference count, and power down the socket, if
    499 	 * necessary.
    500 	 */
    501 	if (--pf->sc->sc_enabled_count == 0)
    502 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    503 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    504 		 pf->sc->sc_enabled_count));
    505 }
    506 
    507 int
    508 pcmcia_io_map(pf, width, offset, size, pcihp, windowp)
    509 	struct pcmcia_function *pf;
    510 	int width;
    511 	bus_addr_t offset;
    512 	bus_size_t size;
    513 	struct pcmcia_io_handle *pcihp;
    514 	int *windowp;
    515 {
    516 	int reg;
    517 
    518 	if (pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
    519 	    width, offset, size, pcihp, windowp))
    520 		return (1);
    521 
    522 	/*
    523 	 * XXX in the multifunction multi-iospace-per-function case, this
    524 	 * needs to cooperate with io_alloc to make sure that the spaces
    525 	 * don't overlap, and that the ccr's are set correctly
    526 	 */
    527 
    528 	if (pcmcia_mfc(pf->sc)) {
    529 		long tmp, iosize;
    530 
    531 		if (pf->pf_mfc_iomax == 0) {
    532 			pf->pf_mfc_iobase = pcihp->addr + offset;
    533 			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
    534 		} else {
    535 			/* this makes the assumption that nothing overlaps */
    536 			if (pf->pf_mfc_iobase > pcihp->addr + offset)
    537 				pf->pf_mfc_iobase = pcihp->addr + offset;
    538 			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
    539 				pf->pf_mfc_iomax = pcihp->addr + offset + size;
    540 		}
    541 
    542 		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
    543 		/* round up to nearest (2^n)-1 */
    544 		for (iosize = 1; iosize >= tmp; iosize <<= 1)
    545 			;
    546 		iosize--;
    547 
    548 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    549 				 pf->pf_mfc_iobase & 0xff);
    550 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    551 				 (pf->pf_mfc_iobase >> 8) & 0xff);
    552 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
    553 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
    554 
    555 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
    556 
    557 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    558 		reg |= PCMCIA_CCR_OPTION_ADDR_DECODE;
    559 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    560 	}
    561 	return (0);
    562 }
    563 
    564 void *
    565 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
    566 	struct pcmcia_function *pf;
    567 	int ipl;
    568 	int (*ih_fct) __P((void *));
    569 	void *ih_arg;
    570 {
    571 	void *ret;
    572 
    573 	/* behave differently if this is a multifunction card */
    574 
    575 	if (pcmcia_mfc(pf->sc)) {
    576 		int s, ihcnt, hiipl, reg;
    577 		struct pcmcia_function *pf2;
    578 
    579 		/* XXX splraise() use needs to go away! */
    580 
    581 		/*
    582 		 * mask all the ipl's which are already used by this card,
    583 		 * and find the highest ipl number (lowest priority)
    584 		 */
    585 
    586 		ihcnt = 0;
    587 		s = 0;		/* this is only here to keep the compiler
    588 				   happy */
    589 		hiipl = 0;	/* this is only here to keep the compiler
    590 				   happy */
    591 
    592 		for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
    593 		     pf2 = pf2->pf_list.sqe_next) {
    594 			if (pf2->ih_fct) {
    595 				DPRINTF(("%s: function %d has ih_fct %p\n",
    596 					 pf->sc->dev.dv_xname, pf2->number,
    597 					 pf2->ih_fct));
    598 
    599 				if (ihcnt == 0) {
    600 					hiipl = pf2->ih_ipl;
    601 				} else {
    602 					if (pf2->ih_ipl > hiipl)
    603 						hiipl = pf2->ih_ipl;
    604 				}
    605 
    606 				ihcnt++;
    607 			}
    608 		}
    609 
    610 		/*
    611 		 * establish the real interrupt, changing the ipl if
    612 		 * necessary
    613 		 */
    614 
    615 		if (ihcnt == 0) {
    616 			int reg;
    617 #ifdef DIAGNOSTIC
    618 			if (pf->sc->ih != NULL)
    619 				panic("card has intr handler, but no function does");
    620 #endif
    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 
    630 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    631 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    632 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    633 		} else if (ipl > hiipl) {
    634 #ifdef DIAGNOSTIC
    635 			if (pf->sc->ih == NULL)
    636 				panic("functions have ih, but the card does not");
    637 #endif
    638 
    639 			/* XXX need #ifdef for splserial on x86 */
    640 			s = splhigh();
    641 
    642 			/* set up the handler for the new function */
    643 
    644 			pf->ih_fct = ih_fct;
    645 			pf->ih_arg = ih_arg;
    646 			pf->ih_ipl = ipl;
    647 
    648 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    649 						      pf->sc->ih);
    650 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    651 			    pf->sc->pch, pf, ipl, pcmcia_card_intr, pf->sc);
    652 
    653 			splx(s);
    654 		} else {
    655 			s = splhigh();
    656 
    657 			/* set up the handler for the new function */
    658 
    659 			pf->ih_fct = ih_fct;
    660 			pf->ih_arg = ih_arg;
    661 			pf->ih_ipl = ipl;
    662 
    663 			splx(s);
    664 		}
    665 
    666 		ret = pf->sc->ih;
    667 
    668 		if (ret != NULL) {
    669 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    670 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    671 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    672 
    673 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    674 			reg |= PCMCIA_CCR_STATUS_INTRACK;
    675 			pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    676 		}
    677 	} else {
    678 		ret = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
    679 		    pf, ipl, ih_fct, ih_arg);
    680 	}
    681 
    682 	return (ret);
    683 }
    684 
    685 void
    686 pcmcia_intr_disestablish(pf, ih)
    687 	struct pcmcia_function *pf;
    688 	void *ih;
    689 {
    690 	/* behave differently if this is a multifunction card */
    691 
    692 	if (pcmcia_mfc(pf->sc)) {
    693 		int s, ihcnt, hiipl;
    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).  Skip
    699 		 * the current function.
    700 		 */
    701 
    702 		ihcnt = 0;
    703 		s = 0;		/* this is only here to keep the compipler
    704 				   happy */
    705 		hiipl = 0;	/* this is only here to keep the compipler
    706 				   happy */
    707 
    708 		for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
    709 		     pf2 = pf2->pf_list.sqe_next) {
    710 			if (pf2 == pf)
    711 				continue;
    712 
    713 			if (pf2->ih_fct) {
    714 				if (ihcnt == 0) {
    715 					hiipl = pf2->ih_ipl;
    716 				} else {
    717 					if (pf2->ih_ipl > hiipl)
    718 						hiipl = pf2->ih_ipl;
    719 				}
    720 				ihcnt++;
    721 			}
    722 		}
    723 
    724 		/*
    725 		 * if the ih being removed is lower priority than the lowest
    726 		 * priority remaining interrupt, up the priority.
    727 		 */
    728 
    729 		/* ihcnt is the number of interrupt handlers *not* including
    730 		   the one about to be removed. */
    731 
    732 		if (ihcnt == 0) {
    733 			int reg;
    734 
    735 #ifdef DIAGNOSTIC
    736 			if (pf->sc->ih == NULL)
    737 				panic("disestablishing last function, but card has no ih");
    738 #endif
    739 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    740 			    pf->sc->ih);
    741 
    742 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    743 			reg &= ~PCMCIA_CCR_OPTION_IREQ_ENABLE;
    744 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    745 
    746 			pf->ih_fct = NULL;
    747 			pf->ih_arg = NULL;
    748 
    749 			pf->sc->ih = NULL;
    750 		} else if (pf->ih_ipl > hiipl) {
    751 #ifdef DIAGNOSTIC
    752 			if (pf->sc->ih == NULL)
    753 				panic("changing ih ipl, but card has no ih");
    754 #endif
    755 			/* XXX need #ifdef for splserial on x86 */
    756 			s = splhigh();
    757 
    758 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    759 			    pf->sc->ih);
    760 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    761 			    pf->sc->pch, pf, hiipl, pcmcia_card_intr, pf->sc);
    762 
    763 			/* null out the handler for this function */
    764 
    765 			pf->ih_fct = NULL;
    766 			pf->ih_arg = NULL;
    767 
    768 			splx(s);
    769 		} else {
    770 			s = splhigh();
    771 
    772 			pf->ih_fct = NULL;
    773 			pf->ih_arg = NULL;
    774 
    775 			splx(s);
    776 		}
    777 	} else {
    778 		pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
    779 	}
    780 }
    781 
    782 int
    783 pcmcia_card_intr(arg)
    784 	void *arg;
    785 {
    786 	struct pcmcia_softc *sc = arg;
    787 	struct pcmcia_function *pf;
    788 	int reg, ret, ret2;
    789 
    790 	ret = 0;
    791 
    792 	for (pf = sc->card.pf_head.sqh_first; pf != NULL;
    793 	    pf = pf->pf_list.sqe_next) {
    794 #if 0
    795 		printf("%s: intr flags=%x fct=%d physaddr=%lx cor=%02x csr=%02x pin=%02x",
    796 		       sc->dev.dv_xname, pf->flags, pf->number,
    797 		       pmap_extract(pmap_kernel(),
    798 		           (vm_offset_t) pf->ccrh) + pf->ccr_offset,
    799 		       pcmcia_ccr_read(pf, PCMCIA_CCCR_OPTION),
    800 		       pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS),
    801 		       pcmcia_ccr_read(pf, PCMCIA_CCR_PIN));
    802 #endif
    803 		if (pf->ih_fct != NULL &&
    804 		    (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
    805 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    806 			if (reg & PCMCIA_CCR_STATUS_INTR) {
    807 				ret2 = (*pf->ih_fct)(pf->ih_arg);
    808 				if (ret2 != 0 && ret == 0)
    809 					ret = ret2;
    810 				reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    811 #if 0
    812 				printf("; csr %02x->%02x",
    813 				    reg, reg & ~PCMCIA_CCR_STATUS_INTR);
    814 #endif
    815 				pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
    816 				    reg & ~PCMCIA_CCR_STATUS_INTR);
    817 			}
    818 		}
    819 #if 0
    820 		printf("\n");
    821 #endif
    822 	}
    823 
    824 	return (ret);
    825 }
    826