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