Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.68
      1 /*	$NetBSD: pcmcia.c,v 1.68 2004/08/12 17:26:51 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.68 2004/08/12 17:26:51 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 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    231 			continue;
    232 		if (pf->child == NULL)
    233 			continue;
    234 		DPRINTF(("%s: detaching %s (function %d)\n",
    235 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    236 		if ((error = config_detach(pf->child, flags)) != 0) {
    237 			printf("%s: error %d detaching %s (function %d)\n",
    238 			    sc->dev.dv_xname, error, pf->child->dv_xname,
    239 			    pf->number);
    240 		} else
    241 			pf->child = NULL;
    242 	}
    243 
    244 	if (sc->sc_enabled_count != 0) {
    245 #ifdef DIAGNOSTIC
    246 		printf("pcmcia_card_detach: enabled_count should be 0 here??\n");
    247 #endif
    248 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    249 		sc->sc_enabled_count = 0;
    250 	}
    251 }
    252 
    253 void
    254 pcmcia_card_deactivate(dev)
    255 	struct device *dev;
    256 {
    257 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    258 	struct pcmcia_function *pf;
    259 
    260 	/*
    261 	 * We're in the chip's card removal interrupt handler.
    262 	 * Deactivate the child driver.  The PCMCIA socket's
    263 	 * event thread will run later to finish the detach.
    264 	 */
    265 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    266 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    267 			continue;
    268 		if (pf->child == NULL)
    269 			continue;
    270 		DPRINTF(("%s: deactivating %s (function %d)\n",
    271 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    272 		config_deactivate(pf->child);
    273 	}
    274 }
    275 
    276 int
    277 pcmcia_submatch(parent, cf, aux)
    278 	struct device *parent;
    279 	struct cfdata *cf;
    280 	void *aux;
    281 {
    282 	struct pcmcia_attach_args *paa = aux;
    283 
    284 	if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
    285 	    cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
    286 		return (0);
    287 
    288 	return (config_match(parent, cf, aux));
    289 }
    290 
    291 int
    292 pcmcia_print(arg, pnp)
    293 	void *arg;
    294 	const char *pnp;
    295 {
    296 	struct pcmcia_attach_args *pa = arg;
    297 	struct pcmcia_softc *sc = pa->pf->sc;
    298 	struct pcmcia_card *card = &sc->card;
    299 	char devinfo[256];
    300 
    301 	if (pnp)
    302 		aprint_normal("%s", pnp);
    303 
    304 	pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
    305 
    306 	aprint_normal(" function %d: %s\n", pa->pf->number, devinfo);
    307 
    308 	return (UNCONF);
    309 }
    310 
    311 void
    312 pcmcia_devinfo(card, showhex, cp, cplen)
    313 	struct pcmcia_card *card;
    314 	int showhex;
    315 	char *cp;
    316 	size_t cplen;
    317 {
    318 	int i, n;
    319 
    320 	if (cplen > 1) {
    321 		*cp++ = '<';
    322 		*cp = '\0';
    323 		cplen--;
    324 	}
    325 
    326 	for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
    327 		n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
    328 		        card->cis1_info[i]);
    329 		cp += n;
    330 		if (cplen < n)
    331 			return;
    332 		cplen -= n;
    333 	}
    334 
    335 	if (cplen > 1) {
    336 		*cp++ = '>';
    337 		*cp = '\0';
    338 		cplen--;
    339 	}
    340 
    341 	if (showhex && cplen > 1)
    342 		snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
    343 		    card->manufacturer, card->product);
    344 }
    345 
    346 const void *
    347 pcmcia_product_lookup(pa, tab, nent, ent_size, matchfn)
    348 	struct pcmcia_attach_args *pa;
    349 	const void *tab;
    350 	size_t nent;
    351 	size_t ent_size;
    352 	pcmcia_product_match_fn matchfn;
    353 {
    354         const struct pcmcia_product *pp;
    355 	int n;
    356 	int matches;
    357 
    358 #ifdef DIAGNOSTIC
    359 	if (sizeof *pp > ent_size)
    360 		panic("pcmcia_product_lookup: bogus ent_size %ld",
    361 		      (long) ent_size);
    362 #endif
    363 
    364         for (pp = tab, n = nent; n; pp = (const struct pcmcia_product *)
    365 	      ((const char *)pp + ent_size), n--) {
    366 		/* see if it matches vendor/product */
    367 		matches = 0;
    368 		if ((pp->pp_vendor != PCMCIA_VENDOR_INVALID &&
    369 		     pp->pp_vendor == pa->manufacturer) &&
    370 		    (pp->pp_product != PCMCIA_PRODUCT_INVALID &&
    371 		     pp->pp_product == pa->product))
    372 			matches = 1;
    373 		if ((pp->pp_cisinfo[0] && pa->card->cis1_info[0] &&
    374 		          !strcmp(pp->pp_cisinfo[0], pa->card->cis1_info[0])) &&
    375 		         (pp->pp_cisinfo[1] && pa->card->cis1_info[1] &&
    376 		          !strcmp(pp->pp_cisinfo[1], pa->card->cis1_info[1])) &&
    377 		         (!pp->pp_cisinfo[2] || (pa->card->cis1_info[2] &&
    378 		           !strcmp(pp->pp_cisinfo[2], pa->card->cis1_info[2]))) &&
    379 		         (!pp->pp_cisinfo[3] || (pa->card->cis1_info[3] &&
    380 		           !strcmp(pp->pp_cisinfo[3], pa->card->cis1_info[3]))))
    381 			matches = 1;
    382 
    383 		/* if a separate match function is given, let it override */
    384 		if (matchfn)
    385 			matches = (*matchfn)(pa, pp, matches);
    386 
    387 		if (matches)
    388                         return (pp);
    389         }
    390         return (0);
    391 }
    392 
    393 void
    394 pcmcia_socket_settype(dev)
    395 	struct device  *dev;
    396 {
    397 	struct pcmcia_softc *sc = (void *)dev;
    398 	struct pcmcia_function *pf;
    399 	int type;
    400 
    401 	/*
    402 	 * set the iftype to memory if this card has no functions (not yet
    403 	 * probed), or only one function, and that is not initialized yet or
    404 	 * that is memory.
    405 	 */
    406 	pf = SIMPLEQ_FIRST(&sc->card.pf_head);
    407 	if (pf == NULL ||
    408 	    (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
    409 	    (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
    410 		type = PCMCIA_IFTYPE_MEMORY;
    411 	else
    412 		type = PCMCIA_IFTYPE_IO;
    413 	pcmcia_chip_socket_settype(sc->pct, sc->pch, type);
    414 }
    415 
    416 /*
    417  * Initialize a PCMCIA function.  May be called as long as the function is
    418  * disabled.
    419  */
    420 void
    421 pcmcia_function_init(pf, cfe)
    422 	struct pcmcia_function *pf;
    423 	struct pcmcia_config_entry *cfe;
    424 {
    425 	if (pf->pf_flags & PFF_ENABLED)
    426 		panic("pcmcia_function_init: function is enabled");
    427 
    428 	/* Remember which configuration entry we are using. */
    429 	pf->cfe = cfe;
    430 }
    431 
    432 void
    433 pcmcia_socket_enable(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_enable(sc->pct, sc->pch);
    440 	DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname,
    441 		 sc->sc_enabled_count));
    442 }
    443 
    444 void
    445 pcmcia_socket_disable(dev)
    446 	struct device *dev;
    447 {
    448 	struct pcmcia_softc *sc = (void *)dev;
    449 
    450 	if (--sc->sc_enabled_count == 0)
    451 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    452 	DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname,
    453 		 sc->sc_enabled_count));
    454 }
    455 
    456 /* Enable a PCMCIA function */
    457 int
    458 pcmcia_function_enable(pf)
    459 	struct pcmcia_function *pf;
    460 {
    461 	struct pcmcia_softc *sc = pf->sc;
    462 	struct pcmcia_function *tmp;
    463 	int reg;
    464 	int error;
    465 
    466 	if (pf->cfe == NULL)
    467 		panic("pcmcia_function_enable: function not initialized");
    468 
    469 	/*
    470 	 * Increase the reference count on the socket, enabling power, if
    471 	 * necessary.
    472 	 */
    473 	pcmcia_socket_enable(&sc->dev);
    474 
    475 	if (pf->pf_flags & PFF_ENABLED) {
    476 		/*
    477 		 * Don't do anything if we're already enabled.
    478 		 */
    479 		return (0);
    480 	}
    481 
    482 	/*
    483 	 * it's possible for different functions' CCRs to be in the same
    484 	 * underlying page.  Check for that.
    485 	 */
    486 
    487 	SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    488 		if ((tmp->pf_flags & PFF_ENABLED) &&
    489 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    490 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    491 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    492 		      tmp->pf_ccr_realsize))) {
    493 			pf->pf_ccrt = tmp->pf_ccrt;
    494 			pf->pf_ccrh = tmp->pf_ccrh;
    495 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    496 
    497 			/*
    498 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    499 			 * tmp->ccr_base) + pf->ccr_base;
    500 			 */
    501 			pf->pf_ccr_offset =
    502 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    503 			    tmp->ccr_base;
    504 			pf->pf_ccr_window = tmp->pf_ccr_window;
    505 			break;
    506 		}
    507 	}
    508 
    509 	if (tmp == NULL) {
    510 		error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh);
    511 		if (error)
    512 			goto bad;
    513 
    514 		error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    515 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    516 		    &pf->pf_ccr_window);
    517 		if (error) {
    518 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    519 			goto bad;
    520 		}
    521 	}
    522 
    523 	if (pcmcia_mfc(sc) || 1) {
    524 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    525 				 (pf->pf_mfc_iobase >>  0) & 0xff);
    526 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    527 				 (pf->pf_mfc_iobase >>  8) & 0xff);
    528 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
    529 				 (pf->pf_mfc_iobase >> 16) & 0xff);
    530 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
    531 				 (pf->pf_mfc_iobase >> 24) & 0xff);
    532 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOLIMIT,
    533 				 pf->pf_mfc_iomax - pf->pf_mfc_iobase);
    534 	}
    535 
    536 	reg = 0;
    537 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    538 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    539 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    540 
    541 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    542 
    543 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    544 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    545 	if (pcmcia_mfc(sc)) {
    546 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    547 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    548 		if (pf->pf_ih)
    549 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    550 
    551 	}
    552 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    553 
    554 #ifdef PCMCIADEBUG
    555 	if (pcmcia_debug) {
    556 		SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    557 			printf("%s: function %d CCR at %d offset %lx: "
    558 			       "%x %x %x %x, %x %x %x %x, %x\n",
    559 			       tmp->sc->dev.dv_xname, tmp->number,
    560 			       tmp->pf_ccr_window,
    561 			       (unsigned long) tmp->pf_ccr_offset,
    562 			       pcmcia_ccr_read(tmp, 0),
    563 			       pcmcia_ccr_read(tmp, 1),
    564 			       pcmcia_ccr_read(tmp, 2),
    565 			       pcmcia_ccr_read(tmp, 3),
    566 
    567 			       pcmcia_ccr_read(tmp, 5),
    568 			       pcmcia_ccr_read(tmp, 6),
    569 			       pcmcia_ccr_read(tmp, 7),
    570 			       pcmcia_ccr_read(tmp, 8),
    571 
    572 			       pcmcia_ccr_read(tmp, 9));
    573 		}
    574 	}
    575 #endif
    576 
    577 	pcmcia_socket_settype(&sc->dev);
    578 
    579 #ifdef IT8368E_LEGACY_MODE
    580 	/* return to I/O mode */
    581 	it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
    582 #endif
    583 
    584 	pf->pf_flags |= PFF_ENABLED;
    585 	return (0);
    586 
    587 bad:
    588 	/*
    589 	 * Decrement the reference count, and power down the socket, if
    590 	 * necessary.
    591 	 */
    592 	printf("%s: couldn't map the CCR\n", pf->child->dv_xname);
    593 	pcmcia_socket_disable(&sc->dev);
    594 
    595 	return (error);
    596 }
    597 
    598 /* Disable PCMCIA function. */
    599 void
    600 pcmcia_function_disable(pf)
    601 	struct pcmcia_function *pf;
    602 {
    603 	struct pcmcia_softc *sc = pf->sc;
    604 	struct pcmcia_function *tmp;
    605 	int reg;
    606 
    607 	if (pf->cfe == NULL)
    608 		panic("pcmcia_function_enable: function not initialized");
    609 
    610 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    611 		/*
    612 		 * Don't do anything but decrement if we're already disabled.
    613 		 */
    614 		goto out;
    615 	}
    616 
    617 	if (pcmcia_mfc(sc)) {
    618 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    619 		reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
    620 			 PCMCIA_CCR_OPTION_ADDR_DECODE|
    621 		         PCMCIA_CCR_OPTION_IREQ_ENABLE);
    622 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    623 	}
    624 
    625 	/*
    626 	 * it's possible for different functions' CCRs to be in the same
    627 	 * underlying page.  Check for that.  Note we mark us as disabled
    628 	 * first to avoid matching ourself.
    629 	 */
    630 
    631 	pf->pf_flags &= ~PFF_ENABLED;
    632 	SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    633 		if ((tmp->pf_flags & PFF_ENABLED) &&
    634 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    635 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    636 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    637 			break;
    638 	}
    639 
    640 	/* Not used by anyone else; unmap the CCR. */
    641 	if (tmp == NULL) {
    642 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    643 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    644 	}
    645 
    646 out:
    647 	/*
    648 	 * Decrement the reference count, and power down the socket, if
    649 	 * necessary.
    650 	 */
    651 	pcmcia_socket_disable(&sc->dev);
    652 }
    653 
    654 int
    655 pcmcia_io_map(pf, width, pcihp, windowp)
    656 	struct pcmcia_function *pf;
    657 	int width;
    658 	struct pcmcia_io_handle *pcihp;
    659 	int *windowp;
    660 {
    661 	struct pcmcia_softc *sc = pf->sc;
    662 	int error;
    663 
    664 	if (pf->pf_flags & PFF_ENABLED)
    665 		printf("pcmcia_io_map: function is enabled!\n");
    666 
    667 	error = pcmcia_chip_io_map(sc->pct, sc->pch,
    668 	    width, 0, pcihp->size, pcihp, windowp);
    669 	if (error)
    670 		return (error);
    671 
    672 	/*
    673 	 * XXX in the multifunction multi-iospace-per-function case, this
    674 	 * needs to cooperate with io_alloc to make sure that the spaces
    675 	 * don't overlap, and that the ccr's are set correctly
    676 	 */
    677 
    678 	if (pcmcia_mfc(sc) || 1) {
    679 		bus_addr_t iobase = pcihp->addr;
    680 		bus_addr_t iomax = pcihp->addr + pcihp->size - 1;
    681 
    682 		DPRINTF(("window iobase %lx iomax %lx\n", (long)iobase,
    683 		    (long)iomax));
    684 		if (pf->pf_mfc_iobase == 0) {
    685 			pf->pf_mfc_iobase = iobase;
    686 			pf->pf_mfc_iomax = iomax;
    687 		} else {
    688 			if (iobase < pf->pf_mfc_iobase)
    689 				pf->pf_mfc_iobase = iobase;
    690 			if (iomax > pf->pf_mfc_iomax)
    691 				pf->pf_mfc_iomax = iomax;
    692 		}
    693 		DPRINTF(("function iobase %lx iomax %lx\n",
    694 		    (long)pf->pf_mfc_iobase, (long)pf->pf_mfc_iomax));
    695 	}
    696 
    697 	return (0);
    698 }
    699 
    700 void
    701 pcmcia_io_unmap(pf, window)
    702 	struct pcmcia_function *pf;
    703 	int window;
    704 {
    705 	struct pcmcia_softc *sc = pf->sc;
    706 
    707 	if (pf->pf_flags & PFF_ENABLED)
    708 		printf("pcmcia_io_unmap: function is enabled!\n");
    709 
    710 	pcmcia_chip_io_unmap(sc->pct, sc->pch, window);
    711 }
    712 
    713 void *
    714 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
    715 	struct pcmcia_function *pf;
    716 	int ipl;
    717 	int (*ih_fct) __P((void *));
    718 	void *ih_arg;
    719 {
    720 
    721 	if (pf->pf_flags & PFF_ENABLED)
    722 		printf("pcmcia_intr_establish: function is enabled!\n");
    723 	if (pf->pf_ih)
    724 		panic("pcmcia_intr_establish: already done\n");
    725 
    726 	pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
    727 	    pf, ipl, ih_fct, ih_arg);
    728 	if (!pf->pf_ih)
    729 		printf("%s: interrupt establish failed\n", pf->child->dv_xname);
    730 	return (pf->pf_ih);
    731 }
    732 
    733 void
    734 pcmcia_intr_disestablish(pf, ih)
    735 	struct pcmcia_function *pf;
    736 	void *ih;
    737 {
    738 
    739 	if (pf->pf_flags & PFF_ENABLED)
    740 		printf("pcmcia_intr_disestablish: function is enabled!\n");
    741 	if (!pf->pf_ih)
    742 		panic("pcmcia_intr_distestablish: already done\n");
    743 
    744 	pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
    745 	pf->pf_ih = 0;
    746 }
    747 
    748 int
    749 pcmcia_config_alloc(pf, cfe)
    750 	struct pcmcia_function *pf;
    751 	struct pcmcia_config_entry *cfe;
    752 {
    753 	int error = 0;
    754 	int n, m;
    755 
    756 	for (n = 0; n < cfe->num_iospace; n++) {
    757 		bus_addr_t start = cfe->iospace[n].start;
    758 		bus_size_t length = cfe->iospace[n].length;
    759 		bus_size_t align = cfe->iomask ? (1 << cfe->iomask) :
    760 		    length;
    761 		bus_size_t skew = start & (align - 1);
    762 
    763 		if ((start - skew) == 0 && align < 0x400) {
    764 			if (skew)
    765 				printf("Drats!  I need a skew!\n");
    766 			start = 0;
    767 		}
    768 
    769 		DPRINTF(("pcmcia_config_alloc: io %d start=%lx length=%lx align=%lx skew=%lx\n",
    770 		    n, (long)start, (long)length, (long)align, (long)skew));
    771 
    772 		error = pcmcia_io_alloc(pf, start, length, align,
    773 		    &cfe->iospace[n].handle);
    774 		if (error)
    775 			break;
    776 	}
    777 	if (n < cfe->num_iospace) {
    778 		for (m = 0; m < n; m++)
    779 			pcmcia_io_free(pf, &cfe->iospace[m].handle);
    780 		return (error);
    781 	}
    782 
    783 	for (n = 0; n < cfe->num_memspace; n++) {
    784 		bus_size_t length = cfe->memspace[n].length;
    785 
    786 		DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
    787 		    (long)length));
    788 
    789 		error = pcmcia_mem_alloc(pf, length, &cfe->memspace[n].handle);
    790 		if (error)
    791 			break;
    792 	}
    793 	if (n < cfe->num_memspace) {
    794 		for (m = 0; m < cfe->num_iospace; m++)
    795 			pcmcia_io_free(pf, &cfe->iospace[m].handle);
    796 		for (m = 0; m < n; m++)
    797 			pcmcia_mem_free(pf, &cfe->memspace[m].handle);
    798 		return (error);
    799 	}
    800 
    801 	/* This one's good! */
    802 	return (error);
    803 }
    804 
    805 void
    806 pcmcia_config_free(pf)
    807 	struct pcmcia_function *pf;
    808 {
    809 	struct pcmcia_config_entry *cfe = pf->cfe;
    810 	int m;
    811 
    812 	for (m = 0; m < cfe->num_iospace; m++)
    813 		pcmcia_io_free(pf, &cfe->iospace[m].handle);
    814 	for (m = 0; m < cfe->num_memspace; m++)
    815 		pcmcia_mem_free(pf, &cfe->memspace[m].handle);
    816 }
    817 
    818 int
    819 pcmcia_config_map(pf)
    820 	struct pcmcia_function *pf;
    821 {
    822 	struct pcmcia_config_entry *cfe = pf->cfe;
    823 	int error = 0;
    824 	int n, m;
    825 
    826 	for (n = 0; n < cfe->num_iospace; n++) {
    827 		int width;
    828 
    829 		if (cfe->flags & PCMCIA_CFE_IO16)
    830 			width = PCMCIA_WIDTH_AUTO;
    831 		else
    832 			width = PCMCIA_WIDTH_IO8;
    833 		error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle,
    834 		    &cfe->iospace[n].window);
    835 		if (error)
    836 			break;
    837 	}
    838 	if (n < cfe->num_iospace) {
    839 		for (m = 0; m < n; m++)
    840 			pcmcia_io_unmap(pf, cfe->iospace[m].window);
    841 		return (error);
    842 	}
    843 
    844 	for (n = 0; n < cfe->num_memspace; n++) {
    845 		bus_size_t length = cfe->memspace[n].length;
    846 		int width;
    847 
    848 		DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
    849 		    (long)length));
    850 
    851 		/*XXX*/
    852 		width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON;
    853 		error = pcmcia_mem_map(pf, width, 0, length,
    854 		    &cfe->memspace[n].handle, &cfe->memspace[n].offset,
    855 		    &cfe->memspace[n].window);
    856 		if (error)
    857 			break;
    858 	}
    859 	if (n < cfe->num_memspace) {
    860 		for (m = 0; m < cfe->num_iospace; m++)
    861 			pcmcia_io_unmap(pf, cfe->iospace[m].window);
    862 		for (m = 0; m < n; m++)
    863 			pcmcia_mem_unmap(pf, cfe->memspace[m].window);
    864 		return (error);
    865 	}
    866 
    867 	/* This one's good! */
    868 	return (error);
    869 }
    870 
    871 void
    872 pcmcia_config_unmap(pf)
    873 	struct pcmcia_function *pf;
    874 {
    875 	struct pcmcia_config_entry *cfe = pf->cfe;
    876 	int m;
    877 
    878 	for (m = 0; m < cfe->num_iospace; m++)
    879 		pcmcia_io_unmap(pf, cfe->iospace[m].window);
    880 	for (m = 0; m < cfe->num_memspace; m++)
    881 		pcmcia_mem_unmap(pf, cfe->memspace[m].window);
    882 }
    883 
    884 int
    885 pcmcia_function_configure(pf, validator)
    886 	struct pcmcia_function *pf;
    887 	int (*validator) __P((struct pcmcia_config_entry *));
    888 {
    889 	struct pcmcia_config_entry *cfe;
    890 	int error = ENOENT;
    891 
    892 	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
    893 		error = validator(cfe);
    894 		if (error)
    895 			continue;
    896 		error = pcmcia_config_alloc(pf, cfe);
    897 		if (!error)
    898 			break;
    899 	}
    900 	if (!cfe) {
    901 		DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n",
    902 		    error));
    903 		return (error);
    904 	}
    905 
    906 	/* Remember which configuration entry we are using. */
    907 	pf->cfe = cfe;
    908 
    909 	error = pcmcia_config_map(pf);
    910 	if (error) {
    911 		DPRINTF(("pcmcia_function_configure: map failed, error=%d\n",
    912 		    error));
    913 		return (error);
    914 	}
    915 
    916 	return (0);
    917 }
    918 
    919 void
    920 pcmcia_function_unconfigure(pf)
    921 	struct pcmcia_function *pf;
    922 {
    923 
    924 	pcmcia_config_unmap(pf);
    925 	pcmcia_config_free(pf);
    926 }
    927