Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.58
      1 /*	$NetBSD: pcmcia.c,v 1.58 2004/08/10 19:15:08 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.58 2004/08/10 19:15:08 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;
    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_chip_socket_enable(sc->pct, sc->pch);
    160 
    161 	pcmcia_read_cis(sc);
    162 
    163 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    164 
    165 	pcmcia_check_cis_quirks(sc);
    166 
    167 	/*
    168 	 * bail now if the card has no functions, or if there was an error in
    169 	 * the cis.
    170 	 */
    171 
    172 	if (sc->card.error)
    173 		return (1);
    174 	if (SIMPLEQ_EMPTY(&sc->card.pf_head))
    175 		return (1);
    176 
    177 	if (pcmcia_verbose)
    178 		pcmcia_print_cis(sc);
    179 
    180 	attached = 0;
    181 
    182 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    183 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    184 			continue;
    185 
    186 #ifdef DIAGNOSTIC
    187 		if (pf->child != NULL) {
    188 			printf("%s: %s still attached to function %d!\n",
    189 			    sc->dev.dv_xname, pf->child->dv_xname,
    190 			    pf->number);
    191 			panic("pcmcia_card_attach");
    192 		}
    193 #endif
    194 		pf->sc = sc;
    195 		pf->child = NULL;
    196 		pf->cfe = NULL;
    197 		pf->pf_ih = NULL;
    198 	}
    199 
    200 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    201 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    202 			continue;
    203 
    204 		paa.manufacturer = sc->card.manufacturer;
    205 		paa.product = sc->card.product;
    206 		paa.card = &sc->card;
    207 		paa.pf = pf;
    208 
    209 		if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
    210 		    pcmcia_submatch)) != NULL)
    211 			attached++;
    212 	}
    213 
    214 	return (attached ? 0 : 1);
    215 }
    216 
    217 void
    218 pcmcia_card_detach(dev, flags)
    219 	struct device *dev;
    220 	int flags;		/* DETACH_* flags */
    221 {
    222 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    223 	struct pcmcia_function *pf;
    224 	int error;
    225 
    226 	/*
    227 	 * We are running on either the PCMCIA socket's event thread
    228 	 * or in user context detaching a device by user request.
    229 	 */
    230 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    231 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    232 			continue;
    233 		if (pf->child == NULL)
    234 			continue;
    235 		DPRINTF(("%s: detaching %s (function %d)\n",
    236 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    237 		if ((error = config_detach(pf->child, flags)) != 0) {
    238 			printf("%s: error %d detaching %s (function %d)\n",
    239 			    sc->dev.dv_xname, error, pf->child->dv_xname,
    240 			    pf->number);
    241 		} else
    242 			pf->child = NULL;
    243 	}
    244 
    245 	if (sc->sc_enabled_count != 0) {
    246 #ifdef DIAGNOSTIC
    247 		printf("pcmcia_card_detach: enabled_count should be 0 here??\n");
    248 #endif
    249 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    250 		sc->sc_enabled_count = 0;
    251 	}
    252 }
    253 
    254 void
    255 pcmcia_card_deactivate(dev)
    256 	struct device *dev;
    257 {
    258 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    259 	struct pcmcia_function *pf;
    260 
    261 	/*
    262 	 * We're in the chip's card removal interrupt handler.
    263 	 * Deactivate the child driver.  The PCMCIA socket's
    264 	 * event thread will run later to finish the detach.
    265 	 */
    266 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    267 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    268 			continue;
    269 		if (pf->child == NULL)
    270 			continue;
    271 		DPRINTF(("%s: deactivating %s (function %d)\n",
    272 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    273 		config_deactivate(pf->child);
    274 	}
    275 }
    276 
    277 int
    278 pcmcia_submatch(parent, cf, aux)
    279 	struct device *parent;
    280 	struct cfdata *cf;
    281 	void *aux;
    282 {
    283 	struct pcmcia_attach_args *paa = aux;
    284 
    285 	if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
    286 	    cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
    287 		return (0);
    288 
    289 	return (config_match(parent, cf, aux));
    290 }
    291 
    292 int
    293 pcmcia_print(arg, pnp)
    294 	void *arg;
    295 	const char *pnp;
    296 {
    297 	struct pcmcia_attach_args *pa = arg;
    298 	struct pcmcia_softc *sc = pa->pf->sc;
    299 	struct pcmcia_card *card = &sc->card;
    300 	char devinfo[256];
    301 
    302 	if (pnp)
    303 		aprint_normal("%s", pnp);
    304 
    305 	pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
    306 
    307 	aprint_normal(" function %d: %s\n", pa->pf->number, devinfo);
    308 
    309 	return (UNCONF);
    310 }
    311 
    312 void
    313 pcmcia_devinfo(card, showhex, cp, cplen)
    314 	struct pcmcia_card *card;
    315 	int showhex;
    316 	char *cp;
    317 	size_t cplen;
    318 {
    319 	int i, n;
    320 
    321 	if (cplen > 1) {
    322 		*cp++ = '<';
    323 		*cp = '\0';
    324 		cplen--;
    325 	}
    326 
    327 	for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
    328 		n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
    329 		        card->cis1_info[i]);
    330 		cp += n;
    331 		if (cplen < n)
    332 			return;
    333 		cplen -= n;
    334 	}
    335 
    336 	if (cplen > 1) {
    337 		*cp++ = '>';
    338 		*cp = '\0';
    339 		cplen--;
    340 	}
    341 
    342 	if (showhex && cplen > 1)
    343 		snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
    344 		    card->manufacturer, card->product);
    345 }
    346 
    347 const void *
    348 pcmcia_product_lookup(pa, tab, nent, ent_size, matchfn)
    349 	struct pcmcia_attach_args *pa;
    350 	const void *tab;
    351 	size_t nent;
    352 	size_t ent_size;
    353 	pcmcia_product_match_fn matchfn;
    354 {
    355         const struct pcmcia_product *pp;
    356 	int n;
    357 	int matches;
    358 
    359 #ifdef DIAGNOSTIC
    360 	if (sizeof *pp > ent_size)
    361 		panic("pcmcia_product_lookup: bogus ent_size %ld",
    362 		      (long) ent_size);
    363 #endif
    364 
    365         for (pp = tab, n = nent; n; pp = (const struct pcmcia_product *)
    366 	      ((const char *)pp + ent_size), n--) {
    367 		/* see if it matches vendor/product */
    368 		matches = 0;
    369 		if ((pp->pp_vendor != PCMCIA_VENDOR_INVALID &&
    370 		     pp->pp_vendor == pa->manufacturer) &&
    371 		    (pp->pp_product != PCMCIA_PRODUCT_INVALID &&
    372 		     pp->pp_product == pa->product))
    373 			matches = 1;
    374 		if ((pp->pp_cisinfo[0] && pa->card->cis1_info[0] &&
    375 		          !strcmp(pp->pp_cisinfo[0], pa->card->cis1_info[0])) &&
    376 		         (pp->pp_cisinfo[1] && pa->card->cis1_info[1] &&
    377 		          !strcmp(pp->pp_cisinfo[1], pa->card->cis1_info[1])) &&
    378 		         (!pp->pp_cisinfo[2] || (pa->card->cis1_info[2] &&
    379 		           !strcmp(pp->pp_cisinfo[2], pa->card->cis1_info[2]))) &&
    380 		         (!pp->pp_cisinfo[3] || (pa->card->cis1_info[3] &&
    381 		           !strcmp(pp->pp_cisinfo[3], pa->card->cis1_info[3]))))
    382 			matches = 1;
    383 
    384 		/* if a separate match function is given, let it override */
    385 		if (matchfn)
    386 			matches = (*matchfn)(pa, pp, matches);
    387 
    388 		if (matches)
    389                         return (pp);
    390         }
    391         return (0);
    392 }
    393 
    394 int
    395 pcmcia_card_gettype(dev)
    396 	struct device  *dev;
    397 {
    398 	struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
    399 	struct pcmcia_function *pf;
    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 		return (PCMCIA_IFTYPE_MEMORY);
    411 	else
    412 		return (PCMCIA_IFTYPE_IO);
    413 }
    414 
    415 /*
    416  * Initialize a PCMCIA function.  May be called as long as the function is
    417  * disabled.
    418  */
    419 void
    420 pcmcia_function_init(pf, cfe)
    421 	struct pcmcia_function *pf;
    422 	struct pcmcia_config_entry *cfe;
    423 {
    424 	if (pf->pf_flags & PFF_ENABLED)
    425 		panic("pcmcia_function_init: function is enabled");
    426 
    427 	/* Remember which configuration entry we are using. */
    428 	pf->cfe = cfe;
    429 }
    430 
    431 void pcmcia_socket_enable(dev)
    432 	struct device *dev;
    433 {
    434 	struct pcmcia_softc *sc = (void *)dev;
    435 
    436 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    437 }
    438 
    439 void pcmcia_socket_disable(dev)
    440 	struct device *dev;
    441 {
    442 	struct pcmcia_softc *sc = (void *)dev;
    443 
    444 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    445 }
    446 
    447 /* Enable a PCMCIA function */
    448 int
    449 pcmcia_function_enable(pf)
    450 	struct pcmcia_function *pf;
    451 {
    452 	struct pcmcia_softc *sc = pf->sc;
    453 	struct pcmcia_function *tmp;
    454 	int reg;
    455 	int error;
    456 
    457 	if (pf->cfe == NULL)
    458 		panic("pcmcia_function_enable: function not initialized");
    459 
    460 	/*
    461 	 * Increase the reference count on the socket, enabling power, if
    462 	 * necessary.
    463 	 */
    464 	if (sc->sc_enabled_count++ == 0)
    465 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
    466 	DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname,
    467 		 sc->sc_enabled_count));
    468 
    469 	if (pf->pf_flags & PFF_ENABLED) {
    470 		/*
    471 		 * Don't do anything if we're already enabled.
    472 		 */
    473 		return (0);
    474 	}
    475 
    476 	/*
    477 	 * it's possible for different functions' CCRs to be in the same
    478 	 * underlying page.  Check for that.
    479 	 */
    480 
    481 	SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    482 		if ((tmp->pf_flags & PFF_ENABLED) &&
    483 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    484 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    485 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    486 		      tmp->pf_ccr_realsize))) {
    487 			pf->pf_ccrt = tmp->pf_ccrt;
    488 			pf->pf_ccrh = tmp->pf_ccrh;
    489 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    490 
    491 			/*
    492 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    493 			 * tmp->ccr_base) + pf->ccr_base;
    494 			 */
    495 			pf->pf_ccr_offset =
    496 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    497 			    tmp->ccr_base;
    498 			pf->pf_ccr_window = tmp->pf_ccr_window;
    499 			break;
    500 		}
    501 	}
    502 
    503 	if (tmp == NULL) {
    504 		error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh);
    505 		if (error)
    506 			goto bad;
    507 
    508 		error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    509 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    510 		    &pf->pf_ccr_window);
    511 		if (error) {
    512 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    513 			goto bad;
    514 		}
    515 	}
    516 
    517 	if (pcmcia_mfc(sc)) {
    518 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    519 				 (pf->pf_mfc_iobase[0] >> 0) & 0xff);
    520 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    521 				 (pf->pf_mfc_iobase[0] >> 8) & 0xff);
    522 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
    523 				 (pf->pf_mfc_iobase[1] >> 0) & 0xff);
    524 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
    525 				 (pf->pf_mfc_iobase[1] >> 8) & 0xff);
    526 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, pf->pf_mfc_iomask);
    527 	}
    528 
    529 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    530 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    531 	if (pcmcia_mfc(sc)) {
    532 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    533 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    534 		if (pf->pf_ih)
    535 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    536 
    537 	}
    538 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    539 
    540 	reg = 0;
    541 	if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
    542 		reg |= PCMCIA_CCR_STATUS_IOIS8;
    543 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    544 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    545 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    546 
    547 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    548 
    549 #ifdef PCMCIADEBUG
    550 	if (pcmcia_debug) {
    551 		SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    552 			printf("%s: function %d CCR at %d offset %lx: "
    553 			       "%x %x %x %x, %x %x %x %x, %x\n",
    554 			       tmp->sc->dev.dv_xname, tmp->number,
    555 			       tmp->pf_ccr_window,
    556 			       (unsigned long) tmp->pf_ccr_offset,
    557 			       pcmcia_ccr_read(tmp, 0),
    558 			       pcmcia_ccr_read(tmp, 1),
    559 			       pcmcia_ccr_read(tmp, 2),
    560 			       pcmcia_ccr_read(tmp, 3),
    561 
    562 			       pcmcia_ccr_read(tmp, 5),
    563 			       pcmcia_ccr_read(tmp, 6),
    564 			       pcmcia_ccr_read(tmp, 7),
    565 			       pcmcia_ccr_read(tmp, 8),
    566 
    567 			       pcmcia_ccr_read(tmp, 9));
    568 		}
    569 	}
    570 #endif
    571 
    572 	pf->pf_flags |= PFF_ENABLED;
    573 
    574 #ifdef IT8368E_LEGACY_MODE
    575 	/* return to I/O mode */
    576 	it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
    577 #endif
    578 	return (0);
    579 
    580 bad:
    581 	/*
    582 	 * Decrement the reference count, and power down the socket, if
    583 	 * necessary.
    584 	 */
    585 	if (--sc->sc_enabled_count == 0)
    586 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    587 	DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname,
    588 		 sc->sc_enabled_count));
    589 	printf("%s: couldn't map the CCR\n", pf->child->dv_xname);
    590 
    591 	return (error);
    592 }
    593 
    594 /* Disable PCMCIA function. */
    595 void
    596 pcmcia_function_disable(pf)
    597 	struct pcmcia_function *pf;
    598 {
    599 	struct pcmcia_function *tmp;
    600 	int reg;
    601 
    602 	if (pf->cfe == NULL)
    603 		panic("pcmcia_function_enable: function not initialized");
    604 
    605 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    606 		/*
    607 		 * Don't do anything but decrement if we're already disabled.
    608 		 */
    609 		goto out;
    610 	}
    611 
    612 	if (pcmcia_mfc(pf->sc)) {
    613 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    614 		reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
    615 			 PCMCIA_CCR_OPTION_ADDR_DECODE|
    616 		         PCMCIA_CCR_OPTION_IREQ_ENABLE);
    617 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    618 	}
    619 
    620 	/*
    621 	 * it's possible for different functions' CCRs to be in the same
    622 	 * underlying page.  Check for that.  Note we mark us as disabled
    623 	 * first to avoid matching ourself.
    624 	 */
    625 
    626 	pf->pf_flags &= ~PFF_ENABLED;
    627 	SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
    628 		if ((tmp->pf_flags & PFF_ENABLED) &&
    629 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    630 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    631 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    632 			break;
    633 	}
    634 
    635 	/* Not used by anyone else; unmap the CCR. */
    636 	if (tmp == NULL) {
    637 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    638 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    639 	}
    640 
    641 out:
    642 	/*
    643 	 * Decrement the reference count, and power down the socket, if
    644 	 * necessary.
    645 	 */
    646 	if (--pf->sc->sc_enabled_count == 0)
    647 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    648 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    649 		 pf->sc->sc_enabled_count));
    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;
    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 (0);
    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;
    826 	int n, m;
    827 
    828 	for (n = 0; n < cfe->num_iospace; n++) {
    829 		int width;
    830 
    831 		switch (cfe->flags & (PCMCIA_CFE_IO8|PCMCIA_CFE_IO16)) {
    832 		case PCMCIA_CFE_IO8:
    833 			width = PCMCIA_WIDTH_IO8;
    834 			break;
    835 		case PCMCIA_CFE_IO16:
    836 			width = PCMCIA_WIDTH_IO16;
    837 			break;
    838 		case PCMCIA_CFE_IO8|PCMCIA_CFE_IO16:
    839 		default:
    840 			width = PCMCIA_WIDTH_AUTO;
    841 			break;
    842 		}
    843 		error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle,
    844 		    &cfe->iospace[n].window);
    845 		if (error)
    846 			break;
    847 	}
    848 	if (n < cfe->num_iospace) {
    849 		for (m = 0; m < n; m++)
    850 			pcmcia_io_unmap(pf, cfe->iospace[m].window);
    851 		return (error);
    852 	}
    853 
    854 	for (n = 0; n < cfe->num_memspace; n++) {
    855 		bus_size_t length = cfe->memspace[n].length;
    856 		int width;
    857 
    858 		DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
    859 		    (long)length));
    860 
    861 		/*XXX*/
    862 		width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON;
    863 		error = pcmcia_mem_map(pf, width, 0, length,
    864 		    &cfe->memspace[n].handle, &cfe->memspace[n].offset,
    865 		    &cfe->memspace[n].window);
    866 		if (error)
    867 			break;
    868 	}
    869 	if (n < cfe->num_memspace) {
    870 		for (m = 0; m < cfe->num_iospace; m++)
    871 			pcmcia_io_unmap(pf, cfe->iospace[m].window);
    872 		for (m = 0; m < n; m++)
    873 			pcmcia_mem_unmap(pf, cfe->memspace[m].window);
    874 		return (error);
    875 	}
    876 
    877 	/* This one's good! */
    878 	return (0);
    879 }
    880 
    881 void
    882 pcmcia_config_unmap(pf)
    883 	struct pcmcia_function *pf;
    884 {
    885 	struct pcmcia_config_entry *cfe = pf->cfe;
    886 	int m;
    887 
    888 	for (m = 0; m < cfe->num_iospace; m++)
    889 		pcmcia_io_unmap(pf, cfe->iospace[m].window);
    890 	for (m = 0; m < cfe->num_memspace; m++)
    891 		pcmcia_mem_unmap(pf, cfe->memspace[m].window);
    892 }
    893 
    894 int
    895 pcmcia_function_configure(pf, validator)
    896 	struct pcmcia_function *pf;
    897 	int (*validator) __P((struct pcmcia_config_entry *));
    898 {
    899 	struct pcmcia_config_entry *cfe;
    900 	int error = ENOENT;
    901 
    902 	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
    903 		error = validator(cfe);
    904 		if (error)
    905 			continue;
    906 		error = pcmcia_config_alloc(pf, cfe);
    907 		if (!error)
    908 			break;
    909 	}
    910 	if (!cfe) {
    911 		DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n",
    912 		    error));
    913 		return (error);
    914 	}
    915 
    916 	/* Remember which configuration entry we are using. */
    917 	pf->cfe = cfe;
    918 
    919 	error = pcmcia_config_map(pf);
    920 	if (error) {
    921 		DPRINTF(("pcmcia_function_configure: map failed, error=%d\n",
    922 		    error));
    923 		return (error);
    924 	}
    925 
    926 	return (0);
    927 }
    928 
    929 void
    930 pcmcia_function_unconfigure(pf)
    931 	struct pcmcia_function *pf;
    932 {
    933 
    934 	pcmcia_config_unmap(pf);
    935 	pcmcia_config_free(pf);
    936 }
    937