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