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