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