Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.64
      1 /*	$NetBSD: pcmcia.c,v 1.64 2004/08/11 20:57:40 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.64 2004/08/11 20:57:40 mycroft Exp $");
     52 
     53 #include "opt_pcmciaverbose.h"
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/device.h>
     58 
     59 #include <dev/pcmcia/pcmciareg.h>
     60 #include <dev/pcmcia/pcmciachip.h>
     61 #include <dev/pcmcia/pcmciavar.h>
     62 #ifdef IT8368E_LEGACY_MODE /* XXX -uch */
     63 #include <arch/hpcmips/dev/it8368var.h>
     64 #endif
     65 
     66 #include "locators.h"
     67 
     68 #ifdef PCMCIADEBUG
     69 int	pcmcia_debug = 0;
     70 #define	DPRINTF(arg) if (pcmcia_debug) printf arg
     71 #else
     72 #define	DPRINTF(arg)
     73 #endif
     74 
     75 #ifdef PCMCIAVERBOSE
     76 int	pcmcia_verbose = 1;
     77 #else
     78 int	pcmcia_verbose = 0;
     79 #endif
     80 
     81 int	pcmcia_match __P((struct device *, struct cfdata *, void *));
     82 int	pcmcia_submatch __P((struct device *, struct cfdata *, void *));
     83 void	pcmcia_attach __P((struct device *, struct device *, void *));
     84 int	pcmcia_print __P((void *, const char *));
     85 
     86 CFATTACH_DECL(pcmcia, sizeof(struct pcmcia_softc),
     87     pcmcia_match, pcmcia_attach, NULL, NULL);
     88 
     89 int
     90 pcmcia_ccr_read(pf, ccr)
     91 	struct pcmcia_function *pf;
     92 	int ccr;
     93 {
     94 
     95 	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
     96 	    pf->pf_ccr_offset + ccr * 2));
     97 }
     98 
     99 void
    100 pcmcia_ccr_write(pf, ccr, val)
    101 	struct pcmcia_function *pf;
    102 	int ccr;
    103 	int val;
    104 {
    105 
    106 	if (pf->ccr_mask & (1 << ccr)) {
    107 		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
    108 		    pf->pf_ccr_offset + ccr * 2, val);
    109 	}
    110 }
    111 
    112 int
    113 pcmcia_match(parent, match, aux)
    114 	struct device *parent;
    115 	struct cfdata *match;
    116 	void *aux;
    117 {
    118 	struct pcmciabus_attach_args *paa = aux;
    119 
    120 	if (strcmp(paa->paa_busname, match->cf_name)) {
    121 	    return 0;
    122 	}
    123 	/* if the autoconfiguration got this far, there's a socket here */
    124 	return (1);
    125 }
    126 
    127 void
    128 pcmcia_attach(parent, self, aux)
    129 	struct device *parent, *self;
    130 	void *aux;
    131 {
    132 	struct pcmciabus_attach_args *paa = aux;
    133 	struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
    134 
    135 	printf("\n");
    136 
    137 	sc->pct = paa->pct;
    138 	sc->pch = paa->pch;
    139 	sc->iobase = paa->iobase;
    140 	sc->iosize = paa->iosize;
    141 
    142 	sc->ih = NULL;
    143 }
    144 
    145 int
    146 pcmcia_card_attach(dev)
    147 	struct device *dev;
    148 {
    149 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    150 	struct pcmcia_function *pf;
    151 	struct pcmcia_attach_args paa;
    152 	int attached = 0;
    153 
    154 	/*
    155 	 * this is here so that when socket_enable calls gettype, trt happens
    156 	 */
    157 	SIMPLEQ_FIRST(&sc->card.pf_head) = NULL;
    158 
    159 	pcmcia_socket_enable(dev);
    160 
    161 	pcmcia_read_cis(sc);
    162 	pcmcia_check_cis_quirks(sc);
    163 
    164 	/*
    165 	 * bail now if the card has no functions, or if there was an error in
    166 	 * the cis.
    167 	 */
    168 
    169 	if (sc->card.error ||
    170 	    SIMPLEQ_EMPTY(&sc->card.pf_head)) {
    171 		printf("%s: card appears to have bogus CIS\n",
    172 		    sc->dev.dv_xname);
    173 		goto done;
    174 	}
    175 
    176 	if (pcmcia_verbose)
    177 		pcmcia_print_cis(sc);
    178 
    179 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    180 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    181 			continue;
    182 
    183 #ifdef DIAGNOSTIC
    184 		if (pf->child != NULL) {
    185 			printf("%s: %s still attached to function %d!\n",
    186 			    sc->dev.dv_xname, pf->child->dv_xname,
    187 			    pf->number);
    188 			panic("pcmcia_card_attach");
    189 		}
    190 #endif
    191 		pf->sc = sc;
    192 		pf->child = NULL;
    193 		pf->cfe = NULL;
    194 		pf->pf_ih = NULL;
    195 	}
    196 
    197 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    198 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    199 			continue;
    200 
    201 		paa.manufacturer = sc->card.manufacturer;
    202 		paa.product = sc->card.product;
    203 		paa.card = &sc->card;
    204 		paa.pf = pf;
    205 
    206 		if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
    207 		    pcmcia_submatch)) != NULL)
    208 			attached++;
    209 	}
    210 
    211 done:
    212 	pcmcia_socket_disable(dev);
    213 	return (attached ? 0 : 1);
    214 }
    215 
    216 void
    217 pcmcia_card_detach(dev, flags)
    218 	struct device *dev;
    219 	int flags;		/* DETACH_* flags */
    220 {
    221 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    222 	struct pcmcia_function *pf;
    223 	int error;
    224 
    225 	/*
    226 	 * We are running on either the PCMCIA socket's event thread
    227 	 * or in user context detaching a device by user request.
    228 	 */
    229 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    230 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    231 			continue;
    232 		if (pf->child == NULL)
    233 			continue;
    234 		DPRINTF(("%s: detaching %s (function %d)\n",
    235 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    236 		if ((error = config_detach(pf->child, flags)) != 0) {
    237 			printf("%s: error %d detaching %s (function %d)\n",
    238 			    sc->dev.dv_xname, error, pf->child->dv_xname,
    239 			    pf->number);
    240 		} else
    241 			pf->child = NULL;
    242 	}
    243 
    244 	if (sc->sc_enabled_count != 0) {
    245 #ifdef DIAGNOSTIC
    246 		printf("pcmcia_card_detach: enabled_count should be 0 here??\n");
    247 #endif
    248 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    249 		sc->sc_enabled_count = 0;
    250 	}
    251 }
    252 
    253 void
    254 pcmcia_card_deactivate(dev)
    255 	struct device *dev;
    256 {
    257 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    258 	struct pcmcia_function *pf;
    259 
    260 	/*
    261 	 * We're in the chip's card removal interrupt handler.
    262 	 * Deactivate the child driver.  The PCMCIA socket's
    263 	 * event thread will run later to finish the detach.
    264 	 */
    265 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    266 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    267 			continue;
    268 		if (pf->child == NULL)
    269 			continue;
    270 		DPRINTF(("%s: deactivating %s (function %d)\n",
    271 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    272 		config_deactivate(pf->child);
    273 	}
    274 }
    275 
    276 int
    277 pcmcia_submatch(parent, cf, aux)
    278 	struct device *parent;
    279 	struct cfdata *cf;
    280 	void *aux;
    281 {
    282 	struct pcmcia_attach_args *paa = aux;
    283 
    284 	if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
    285 	    cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
    286 		return (0);
    287 
    288 	return (config_match(parent, cf, aux));
    289 }
    290 
    291 int
    292 pcmcia_print(arg, pnp)
    293 	void *arg;
    294 	const char *pnp;
    295 {
    296 	struct pcmcia_attach_args *pa = arg;
    297 	struct pcmcia_softc *sc = pa->pf->sc;
    298 	struct pcmcia_card *card = &sc->card;
    299 	char devinfo[256];
    300 
    301 	if (pnp)
    302 		aprint_normal("%s", pnp);
    303 
    304 	pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
    305 
    306 	aprint_normal(" function %d: %s\n", pa->pf->number, devinfo);
    307 
    308 	return (UNCONF);
    309 }
    310 
    311 void
    312 pcmcia_devinfo(card, showhex, cp, cplen)
    313 	struct pcmcia_card *card;
    314 	int showhex;
    315 	char *cp;
    316 	size_t cplen;
    317 {
    318 	int i, n;
    319 
    320 	if (cplen > 1) {
    321 		*cp++ = '<';
    322 		*cp = '\0';
    323 		cplen--;
    324 	}
    325 
    326 	for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
    327 		n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
    328 		        card->cis1_info[i]);
    329 		cp += n;
    330 		if (cplen < n)
    331 			return;
    332 		cplen -= n;
    333 	}
    334 
    335 	if (cplen > 1) {
    336 		*cp++ = '>';
    337 		*cp = '\0';
    338 		cplen--;
    339 	}
    340 
    341 	if (showhex && cplen > 1)
    342 		snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
    343 		    card->manufacturer, card->product);
    344 }
    345 
    346 const void *
    347 pcmcia_product_lookup(pa, tab, nent, ent_size, matchfn)
    348 	struct pcmcia_attach_args *pa;
    349 	const void *tab;
    350 	size_t nent;
    351 	size_t ent_size;
    352 	pcmcia_product_match_fn matchfn;
    353 {
    354         const struct pcmcia_product *pp;
    355 	int n;
    356 	int matches;
    357 
    358 #ifdef DIAGNOSTIC
    359 	if (sizeof *pp > ent_size)
    360 		panic("pcmcia_product_lookup: bogus ent_size %ld",
    361 		      (long) ent_size);
    362 #endif
    363 
    364         for (pp = tab, n = nent; n; pp = (const struct pcmcia_product *)
    365 	      ((const char *)pp + ent_size), n--) {
    366 		/* see if it matches vendor/product */
    367 		matches = 0;
    368 		if ((pp->pp_vendor != PCMCIA_VENDOR_INVALID &&
    369 		     pp->pp_vendor == pa->manufacturer) &&
    370 		    (pp->pp_product != PCMCIA_PRODUCT_INVALID &&
    371 		     pp->pp_product == pa->product))
    372 			matches = 1;
    373 		if ((pp->pp_cisinfo[0] && pa->card->cis1_info[0] &&
    374 		          !strcmp(pp->pp_cisinfo[0], pa->card->cis1_info[0])) &&
    375 		         (pp->pp_cisinfo[1] && pa->card->cis1_info[1] &&
    376 		          !strcmp(pp->pp_cisinfo[1], pa->card->cis1_info[1])) &&
    377 		         (!pp->pp_cisinfo[2] || (pa->card->cis1_info[2] &&
    378 		           !strcmp(pp->pp_cisinfo[2], pa->card->cis1_info[2]))) &&
    379 		         (!pp->pp_cisinfo[3] || (pa->card->cis1_info[3] &&
    380 		           !strcmp(pp->pp_cisinfo[3], pa->card->cis1_info[3]))))
    381 			matches = 1;
    382 
    383 		/* if a separate match function is given, let it override */
    384 		if (matchfn)
    385 			matches = (*matchfn)(pa, pp, matches);
    386 
    387 		if (matches)
    388                         return (pp);
    389         }
    390         return (0);
    391 }
    392 
    393 void
    394 pcmcia_socket_settype(dev)
    395 	struct device  *dev;
    396 {
    397 	struct pcmcia_softc *sc = (void *)dev;
    398 	struct pcmcia_function *pf;
    399 	int type;
    400 
    401 	/*
    402 	 * set the iftype to memory if this card has no functions (not yet
    403 	 * probed), or only one function, and that is not initialized yet or
    404 	 * that is memory.
    405 	 */
    406 	pf = SIMPLEQ_FIRST(&sc->card.pf_head);
    407 	if (pf == NULL ||
    408 	    (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
    409 	    (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
    410 		type = PCMCIA_IFTYPE_MEMORY;
    411 	else
    412 		type = PCMCIA_IFTYPE_IO;
    413 	pcmcia_chip_socket_settype(sc->pct, sc->pch, type);
    414 }
    415 
    416 /*
    417  * Initialize a PCMCIA function.  May be called as long as the function is
    418  * disabled.
    419  */
    420 void
    421 pcmcia_function_init(pf, cfe)
    422 	struct pcmcia_function *pf;
    423 	struct pcmcia_config_entry *cfe;
    424 {
    425 	if (pf->pf_flags & PFF_ENABLED)
    426 		panic("pcmcia_function_init: function is enabled");
    427 
    428 	/* Remember which configuration entry we are using. */
    429 	pf->cfe = cfe;
    430 }
    431 
    432 void
    433 pcmcia_socket_enable(dev)
    434 	struct device *dev;
    435 {
    436 	struct pcmcia_softc *sc = (void *)dev;
    437 
    438 	if (sc->sc_enabled_count++ == 0)
    439 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
    440 	DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname,
    441 		 sc->sc_enabled_count));
    442 }
    443 
    444 void
    445 pcmcia_socket_disable(dev)
    446 	struct device *dev;
    447 {
    448 	struct pcmcia_softc *sc = (void *)dev;
    449 
    450 	if (--sc->sc_enabled_count == 0)
    451 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    452 	DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname,
    453 		 sc->sc_enabled_count));
    454 }
    455 
    456 /* Enable a PCMCIA function */
    457 int
    458 pcmcia_function_enable(pf)
    459 	struct pcmcia_function *pf;
    460 {
    461 	struct pcmcia_softc *sc = pf->sc;
    462 	struct pcmcia_function *tmp;
    463 	int reg;
    464 	int error;
    465 
    466 	if (pf->cfe == NULL)
    467 		panic("pcmcia_function_enable: function not initialized");
    468 
    469 	/*
    470 	 * Increase the reference count on the socket, enabling power, if
    471 	 * necessary.
    472 	 */
    473 	pcmcia_socket_enable(&sc->dev);
    474 	pcmcia_socket_settype(&sc->dev);
    475 
    476 	if (pf->pf_flags & PFF_ENABLED) {
    477 		/*
    478 		 * Don't do anything if we're already enabled.
    479 		 */
    480 		return (0);
    481 	}
    482 
    483 	/*
    484 	 * it's possible for different functions' CCRs to be in the same
    485 	 * underlying page.  Check for that.
    486 	 */
    487 
    488 	SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    489 		if ((tmp->pf_flags & PFF_ENABLED) &&
    490 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    491 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    492 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    493 		      tmp->pf_ccr_realsize))) {
    494 			pf->pf_ccrt = tmp->pf_ccrt;
    495 			pf->pf_ccrh = tmp->pf_ccrh;
    496 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    497 
    498 			/*
    499 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    500 			 * tmp->ccr_base) + pf->ccr_base;
    501 			 */
    502 			pf->pf_ccr_offset =
    503 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    504 			    tmp->ccr_base;
    505 			pf->pf_ccr_window = tmp->pf_ccr_window;
    506 			break;
    507 		}
    508 	}
    509 
    510 	if (tmp == NULL) {
    511 		error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh);
    512 		if (error)
    513 			goto bad;
    514 
    515 		error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    516 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    517 		    &pf->pf_ccr_window);
    518 		if (error) {
    519 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    520 			goto bad;
    521 		}
    522 	}
    523 
    524 	if (pcmcia_mfc(sc)) {
    525 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    526 				 (pf->pf_mfc_iobase[0] >> 0) & 0xff);
    527 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    528 				 (pf->pf_mfc_iobase[0] >> 8) & 0xff);
    529 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
    530 				 (pf->pf_mfc_iobase[1] >> 0) & 0xff);
    531 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
    532 				 (pf->pf_mfc_iobase[1] >> 8) & 0xff);
    533 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, pf->pf_mfc_iomask);
    534 	}
    535 
    536 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    537 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    538 	if (pcmcia_mfc(sc)) {
    539 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    540 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    541 		if (pf->pf_ih)
    542 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    543 
    544 	}
    545 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    546 
    547 	reg = 0;
    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