Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.52
      1  1.52   mycroft /*	$NetBSD: pcmcia.c,v 1.52 2004/08/09 20:02:36 mycroft Exp $	*/
      2  1.42   mycroft 
      3  1.42   mycroft /*
      4  1.42   mycroft  * Copyright (c) 2004 Charles M. Hannum.  All rights reserved.
      5  1.42   mycroft  *
      6  1.42   mycroft  * Redistribution and use in source and binary forms, with or without
      7  1.42   mycroft  * modification, are permitted provided that the following conditions
      8  1.42   mycroft  * are met:
      9  1.42   mycroft  * 1. Redistributions of source code must retain the above copyright
     10  1.42   mycroft  *    notice, this list of conditions and the following disclaimer.
     11  1.42   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.42   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13  1.42   mycroft  *    documentation and/or other materials provided with the distribution.
     14  1.42   mycroft  * 3. All advertising materials mentioning features or use of this software
     15  1.42   mycroft  *    must display the following acknowledgement:
     16  1.42   mycroft  *      This product includes software developed by Charles M. Hannum.
     17  1.42   mycroft  * 4. The name of the author may not be used to endorse or promote products
     18  1.42   mycroft  *    derived from this software without specific prior written permission.
     19  1.42   mycroft  */
     20   1.2   thorpej 
     21   1.2   thorpej /*
     22   1.2   thorpej  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
     23   1.2   thorpej  *
     24   1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     25   1.2   thorpej  * modification, are permitted provided that the following conditions
     26   1.2   thorpej  * are met:
     27   1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     28   1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     29   1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     30   1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     31   1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     32   1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     33   1.2   thorpej  *    must display the following acknowledgement:
     34   1.2   thorpej  *	This product includes software developed by Marc Horowitz.
     35   1.2   thorpej  * 4. The name of the author may not be used to endorse or promote products
     36   1.2   thorpej  *    derived from this software without specific prior written permission.
     37   1.2   thorpej  *
     38   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     39   1.2   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     40   1.2   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     41   1.2   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     42   1.2   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     43   1.2   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     44   1.2   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     45   1.2   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     46   1.2   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     47   1.2   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     48   1.2   thorpej  */
     49  1.26     lukem 
     50  1.26     lukem #include <sys/cdefs.h>
     51  1.52   mycroft __KERNEL_RCSID(0, "$NetBSD: pcmcia.c,v 1.52 2004/08/09 20:02:36 mycroft Exp $");
     52   1.2   thorpej 
     53   1.2   thorpej #include "opt_pcmciaverbose.h"
     54   1.2   thorpej 
     55   1.2   thorpej #include <sys/param.h>
     56   1.2   thorpej #include <sys/systm.h>
     57   1.2   thorpej #include <sys/device.h>
     58   1.2   thorpej 
     59   1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     60   1.2   thorpej #include <dev/pcmcia/pcmciachip.h>
     61   1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     62  1.21       uch #ifdef IT8368E_LEGACY_MODE /* XXX -uch */
     63  1.21       uch #include <arch/hpcmips/dev/it8368var.h>
     64  1.21       uch #endif
     65   1.2   thorpej 
     66   1.3     enami #include "locators.h"
     67   1.3     enami 
     68   1.2   thorpej #ifdef PCMCIADEBUG
     69   1.2   thorpej int	pcmcia_debug = 0;
     70   1.2   thorpej #define	DPRINTF(arg) if (pcmcia_debug) printf arg
     71   1.2   thorpej #else
     72   1.2   thorpej #define	DPRINTF(arg)
     73   1.2   thorpej #endif
     74   1.2   thorpej 
     75   1.2   thorpej #ifdef PCMCIAVERBOSE
     76   1.2   thorpej int	pcmcia_verbose = 1;
     77   1.2   thorpej #else
     78   1.2   thorpej int	pcmcia_verbose = 0;
     79   1.2   thorpej #endif
     80   1.2   thorpej 
     81   1.2   thorpej int	pcmcia_match __P((struct device *, struct cfdata *, void *));
     82   1.2   thorpej int	pcmcia_submatch __P((struct device *, struct cfdata *, void *));
     83   1.2   thorpej void	pcmcia_attach __P((struct device *, struct device *, void *));
     84   1.2   thorpej int	pcmcia_print __P((void *, const char *));
     85   1.2   thorpej 
     86  1.33   thorpej CFATTACH_DECL(pcmcia, sizeof(struct pcmcia_softc),
     87  1.34   thorpej     pcmcia_match, pcmcia_attach, NULL, NULL);
     88   1.2   thorpej 
     89   1.2   thorpej int
     90   1.2   thorpej pcmcia_ccr_read(pf, ccr)
     91   1.2   thorpej 	struct pcmcia_function *pf;
     92   1.2   thorpej 	int ccr;
     93   1.2   thorpej {
     94   1.2   thorpej 
     95   1.2   thorpej 	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
     96  1.43   mycroft 	    pf->pf_ccr_offset + ccr * 2));
     97   1.2   thorpej }
     98   1.2   thorpej 
     99   1.2   thorpej void
    100   1.2   thorpej pcmcia_ccr_write(pf, ccr, val)
    101   1.2   thorpej 	struct pcmcia_function *pf;
    102   1.2   thorpej 	int ccr;
    103   1.2   thorpej 	int val;
    104   1.2   thorpej {
    105   1.2   thorpej 
    106  1.43   mycroft 	if (pf->ccr_mask & (1 << ccr)) {
    107   1.2   thorpej 		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
    108  1.43   mycroft 		    pf->pf_ccr_offset + ccr * 2, val);
    109   1.2   thorpej 	}
    110   1.2   thorpej }
    111   1.2   thorpej 
    112   1.2   thorpej int
    113   1.2   thorpej pcmcia_match(parent, match, aux)
    114   1.2   thorpej 	struct device *parent;
    115   1.2   thorpej 	struct cfdata *match;
    116   1.2   thorpej 	void *aux;
    117   1.2   thorpej {
    118  1.14      haya 	struct pcmciabus_attach_args *paa = aux;
    119  1.14      haya 
    120  1.30   thorpej 	if (strcmp(paa->paa_busname, match->cf_name)) {
    121  1.14      haya 	    return 0;
    122  1.14      haya 	}
    123   1.2   thorpej 	/* if the autoconfiguration got this far, there's a socket here */
    124   1.2   thorpej 	return (1);
    125   1.2   thorpej }
    126   1.2   thorpej 
    127   1.2   thorpej void
    128   1.2   thorpej pcmcia_attach(parent, self, aux)
    129   1.2   thorpej 	struct device *parent, *self;
    130   1.2   thorpej 	void *aux;
    131   1.2   thorpej {
    132   1.2   thorpej 	struct pcmciabus_attach_args *paa = aux;
    133   1.2   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
    134   1.2   thorpej 
    135   1.2   thorpej 	printf("\n");
    136   1.2   thorpej 
    137   1.2   thorpej 	sc->pct = paa->pct;
    138   1.2   thorpej 	sc->pch = paa->pch;
    139   1.2   thorpej 	sc->iobase = paa->iobase;
    140   1.2   thorpej 	sc->iosize = paa->iosize;
    141   1.2   thorpej 
    142   1.2   thorpej 	sc->ih = NULL;
    143   1.2   thorpej }
    144   1.2   thorpej 
    145   1.2   thorpej int
    146   1.2   thorpej pcmcia_card_attach(dev)
    147   1.2   thorpej 	struct device *dev;
    148   1.2   thorpej {
    149   1.2   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    150   1.2   thorpej 	struct pcmcia_function *pf;
    151   1.2   thorpej 	struct pcmcia_attach_args paa;
    152   1.2   thorpej 	int attached;
    153   1.2   thorpej 
    154   1.2   thorpej 	/*
    155   1.2   thorpej 	 * this is here so that when socket_enable calls gettype, trt happens
    156   1.2   thorpej 	 */
    157  1.29     lukem 	SIMPLEQ_FIRST(&sc->card.pf_head) = NULL;
    158   1.2   thorpej 
    159   1.2   thorpej 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    160   1.2   thorpej 
    161   1.2   thorpej 	pcmcia_read_cis(sc);
    162   1.2   thorpej 
    163   1.2   thorpej 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    164  1.13      marc 
    165  1.13      marc 	pcmcia_check_cis_quirks(sc);
    166   1.2   thorpej 
    167   1.2   thorpej 	/*
    168   1.2   thorpej 	 * bail now if the card has no functions, or if there was an error in
    169   1.2   thorpej 	 * the cis.
    170   1.2   thorpej 	 */
    171   1.2   thorpej 
    172   1.2   thorpej 	if (sc->card.error)
    173   1.2   thorpej 		return (1);
    174  1.29     lukem 	if (SIMPLEQ_EMPTY(&sc->card.pf_head))
    175   1.2   thorpej 		return (1);
    176   1.2   thorpej 
    177   1.2   thorpej 	if (pcmcia_verbose)
    178   1.2   thorpej 		pcmcia_print_cis(sc);
    179   1.2   thorpej 
    180   1.2   thorpej 	attached = 0;
    181   1.2   thorpej 
    182  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    183  1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    184   1.2   thorpej 			continue;
    185   1.2   thorpej 
    186  1.11   thorpej #ifdef DIAGNOSTIC
    187  1.11   thorpej 		if (pf->child != NULL) {
    188  1.11   thorpej 			printf("%s: %s still attached to function %d!\n",
    189  1.11   thorpej 			    sc->dev.dv_xname, pf->child->dv_xname,
    190  1.11   thorpej 			    pf->number);
    191  1.11   thorpej 			panic("pcmcia_card_attach");
    192  1.11   thorpej 		}
    193  1.11   thorpej #endif
    194   1.2   thorpej 		pf->sc = sc;
    195  1.11   thorpej 		pf->child = NULL;
    196   1.2   thorpej 		pf->cfe = NULL;
    197  1.52   mycroft 		pf->pf_ih = NULL;
    198   1.2   thorpej 	}
    199   1.2   thorpej 
    200  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    201  1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    202   1.2   thorpej 			continue;
    203   1.2   thorpej 
    204   1.2   thorpej 		paa.manufacturer = sc->card.manufacturer;
    205   1.2   thorpej 		paa.product = sc->card.product;
    206   1.2   thorpej 		paa.card = &sc->card;
    207   1.2   thorpej 		paa.pf = pf;
    208   1.2   thorpej 
    209  1.11   thorpej 		if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
    210  1.20    chopps 		    pcmcia_submatch)) != NULL)
    211   1.2   thorpej 			attached++;
    212   1.2   thorpej 	}
    213   1.2   thorpej 
    214   1.2   thorpej 	return (attached ? 0 : 1);
    215   1.2   thorpej }
    216   1.2   thorpej 
    217   1.2   thorpej void
    218  1.11   thorpej pcmcia_card_detach(dev, flags)
    219   1.2   thorpej 	struct device *dev;
    220  1.11   thorpej 	int flags;		/* DETACH_* flags */
    221   1.2   thorpej {
    222  1.10   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    223  1.11   thorpej 	struct pcmcia_function *pf;
    224  1.11   thorpej 	int error;
    225  1.10   thorpej 
    226  1.11   thorpej 	/*
    227  1.11   thorpej 	 * We are running on either the PCMCIA socket's event thread
    228  1.11   thorpej 	 * or in user context detaching a device by user request.
    229  1.10   thorpej 	 */
    230  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    231  1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    232  1.11   thorpej 			continue;
    233  1.11   thorpej 		if (pf->child == NULL)
    234  1.11   thorpej 			continue;
    235  1.11   thorpej 		DPRINTF(("%s: detaching %s (function %d)\n",
    236  1.11   thorpej 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    237  1.11   thorpej 		if ((error = config_detach(pf->child, flags)) != 0) {
    238  1.11   thorpej 			printf("%s: error %d detaching %s (function %d)\n",
    239  1.11   thorpej 			    sc->dev.dv_xname, error, pf->child->dv_xname,
    240  1.11   thorpej 			    pf->number);
    241  1.11   thorpej 		} else
    242  1.11   thorpej 			pf->child = NULL;
    243  1.11   thorpej 	}
    244  1.11   thorpej }
    245  1.11   thorpej 
    246  1.11   thorpej void
    247  1.11   thorpej pcmcia_card_deactivate(dev)
    248  1.11   thorpej 	struct device *dev;
    249  1.11   thorpej {
    250  1.11   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    251  1.11   thorpej 	struct pcmcia_function *pf;
    252  1.10   thorpej 
    253  1.11   thorpej 	/*
    254  1.11   thorpej 	 * We're in the chip's card removal interrupt handler.
    255  1.11   thorpej 	 * Deactivate the child driver.  The PCMCIA socket's
    256  1.11   thorpej 	 * event thread will run later to finish the detach.
    257  1.11   thorpej 	 */
    258  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    259  1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    260  1.11   thorpej 			continue;
    261  1.11   thorpej 		if (pf->child == NULL)
    262  1.11   thorpej 			continue;
    263  1.11   thorpej 		DPRINTF(("%s: deactivating %s (function %d)\n",
    264  1.11   thorpej 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    265  1.11   thorpej 		config_deactivate(pf->child);
    266  1.11   thorpej 	}
    267   1.2   thorpej }
    268   1.2   thorpej 
    269   1.2   thorpej int
    270   1.2   thorpej pcmcia_submatch(parent, cf, aux)
    271   1.2   thorpej 	struct device *parent;
    272   1.2   thorpej 	struct cfdata *cf;
    273   1.2   thorpej 	void *aux;
    274   1.2   thorpej {
    275   1.2   thorpej 	struct pcmcia_attach_args *paa = aux;
    276   1.2   thorpej 
    277   1.3     enami 	if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
    278   1.3     enami 	    cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
    279   1.2   thorpej 		return (0);
    280   1.2   thorpej 
    281  1.31   thorpej 	return (config_match(parent, cf, aux));
    282   1.2   thorpej }
    283   1.2   thorpej 
    284   1.2   thorpej int
    285   1.2   thorpej pcmcia_print(arg, pnp)
    286   1.2   thorpej 	void *arg;
    287   1.2   thorpej 	const char *pnp;
    288   1.2   thorpej {
    289   1.2   thorpej 	struct pcmcia_attach_args *pa = arg;
    290   1.2   thorpej 	struct pcmcia_softc *sc = pa->pf->sc;
    291   1.2   thorpej 	struct pcmcia_card *card = &sc->card;
    292  1.18  augustss 	char devinfo[256];
    293   1.2   thorpej 
    294  1.38   mycroft 	if (pnp)
    295  1.38   mycroft 		aprint_normal("%s", pnp);
    296  1.38   mycroft 
    297  1.38   mycroft 	pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
    298  1.38   mycroft 
    299  1.38   mycroft 	aprint_normal(" function %d: %s", pa->pf->number, devinfo);
    300   1.2   thorpej 
    301   1.2   thorpej 	return (UNCONF);
    302  1.18  augustss }
    303  1.18  augustss 
    304  1.18  augustss void
    305  1.18  augustss pcmcia_devinfo(card, showhex, cp, cplen)
    306  1.18  augustss 	struct pcmcia_card *card;
    307  1.19     enami 	int showhex;
    308  1.18  augustss 	char *cp;
    309  1.38   mycroft 	size_t cplen;
    310  1.18  augustss {
    311  1.18  augustss 	int i, n;
    312  1.18  augustss 
    313  1.37   mycroft 	if (cplen > 1) {
    314  1.37   mycroft 		*cp++ = '<';
    315  1.38   mycroft 		*cp = '\0';
    316  1.37   mycroft 		cplen--;
    317  1.37   mycroft 	}
    318  1.37   mycroft 
    319  1.37   mycroft 	for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
    320  1.39   mycroft 		n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
    321  1.18  augustss 		        card->cis1_info[i]);
    322  1.18  augustss 		cp += n;
    323  1.38   mycroft 		if (cplen < n)
    324  1.38   mycroft 			return;
    325  1.18  augustss 		cplen -= n;
    326  1.18  augustss 	}
    327  1.37   mycroft 
    328  1.37   mycroft 	if (cplen > 1) {
    329  1.37   mycroft 		*cp++ = '>';
    330  1.38   mycroft 		*cp = '\0';
    331  1.37   mycroft 		cplen--;
    332  1.37   mycroft 	}
    333  1.37   mycroft 
    334  1.37   mycroft 	if (showhex && cplen > 1)
    335  1.37   mycroft 		snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
    336  1.37   mycroft 		    card->manufacturer, card->product);
    337  1.16       cgd }
    338  1.16       cgd 
    339  1.16       cgd const struct pcmcia_product *
    340  1.16       cgd pcmcia_product_lookup(pa, tab, ent_size, matchfn)
    341  1.16       cgd 	struct pcmcia_attach_args *pa;
    342  1.16       cgd 	const struct pcmcia_product *tab;
    343  1.16       cgd 	size_t ent_size;
    344  1.16       cgd 	pcmcia_product_match_fn matchfn;
    345  1.16       cgd {
    346  1.16       cgd         const struct pcmcia_product *ent;
    347  1.16       cgd 	int matches;
    348  1.16       cgd 
    349  1.16       cgd #ifdef DIAGNOSTIC
    350  1.16       cgd 	if (sizeof *ent > ent_size)
    351  1.17   nathanw 		panic("pcmcia_product_lookup: bogus ent_size %ld",
    352  1.17   nathanw 		      (long) ent_size);
    353  1.16       cgd #endif
    354  1.16       cgd 
    355  1.16       cgd         for (ent = tab;
    356  1.16       cgd 	    ent->pp_name != NULL;
    357  1.16       cgd 	    ent = (const struct pcmcia_product *)
    358  1.16       cgd 	      ((const char *)ent + ent_size)) {
    359  1.16       cgd 
    360  1.16       cgd 		/* see if it matches vendor/product/function */
    361  1.16       cgd 		matches = (pa->manufacturer == ent->pp_vendor) &&
    362  1.16       cgd 		    (pa->product == ent->pp_product) &&
    363  1.16       cgd 		    (pa->pf->number == ent->pp_expfunc);
    364  1.16       cgd 
    365  1.16       cgd 		/* if a separate match function is given, let it override */
    366  1.16       cgd 		if (matchfn != NULL)
    367  1.16       cgd 			matches = (*matchfn)(pa, ent, matches);
    368  1.16       cgd 
    369  1.16       cgd 		if (matches)
    370  1.16       cgd                         return (ent);
    371  1.16       cgd         }
    372  1.16       cgd         return (NULL);
    373   1.2   thorpej }
    374   1.2   thorpej 
    375   1.2   thorpej int
    376   1.2   thorpej pcmcia_card_gettype(dev)
    377   1.2   thorpej 	struct device  *dev;
    378   1.2   thorpej {
    379   1.7     enami 	struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
    380   1.7     enami 	struct pcmcia_function *pf;
    381   1.2   thorpej 
    382   1.2   thorpej 	/*
    383   1.2   thorpej 	 * set the iftype to memory if this card has no functions (not yet
    384   1.7     enami 	 * probed), or only one function, and that is not initialized yet or
    385   1.7     enami 	 * that is memory.
    386   1.2   thorpej 	 */
    387   1.7     enami 	pf = SIMPLEQ_FIRST(&sc->card.pf_head);
    388   1.7     enami 	if (pf == NULL ||
    389   1.7     enami 	    (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
    390   1.7     enami 	    (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
    391   1.2   thorpej 		return (PCMCIA_IFTYPE_MEMORY);
    392   1.2   thorpej 	else
    393   1.2   thorpej 		return (PCMCIA_IFTYPE_IO);
    394   1.2   thorpej }
    395   1.2   thorpej 
    396   1.2   thorpej /*
    397   1.2   thorpej  * Initialize a PCMCIA function.  May be called as long as the function is
    398   1.2   thorpej  * disabled.
    399   1.2   thorpej  */
    400   1.2   thorpej void
    401   1.2   thorpej pcmcia_function_init(pf, cfe)
    402   1.2   thorpej 	struct pcmcia_function *pf;
    403   1.2   thorpej 	struct pcmcia_config_entry *cfe;
    404   1.2   thorpej {
    405   1.2   thorpej 	if (pf->pf_flags & PFF_ENABLED)
    406   1.2   thorpej 		panic("pcmcia_function_init: function is enabled");
    407   1.2   thorpej 
    408   1.2   thorpej 	/* Remember which configuration entry we are using. */
    409   1.2   thorpej 	pf->cfe = cfe;
    410   1.2   thorpej }
    411   1.2   thorpej 
    412  1.40   mycroft void pcmcia_socket_enable(dev)
    413  1.40   mycroft 	struct device *dev;
    414   1.5      marc {
    415  1.40   mycroft 	struct pcmcia_softc *sc = (void *)dev;
    416  1.40   mycroft 
    417  1.40   mycroft 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    418   1.5      marc }
    419   1.5      marc 
    420  1.40   mycroft void pcmcia_socket_disable(dev)
    421  1.40   mycroft 	struct device *dev;
    422   1.5      marc {
    423  1.40   mycroft 	struct pcmcia_softc *sc = (void *)dev;
    424  1.40   mycroft 
    425  1.40   mycroft 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    426   1.5      marc }
    427   1.5      marc 
    428   1.2   thorpej /* Enable a PCMCIA function */
    429   1.2   thorpej int
    430   1.2   thorpej pcmcia_function_enable(pf)
    431   1.2   thorpej 	struct pcmcia_function *pf;
    432   1.2   thorpej {
    433   1.2   thorpej 	struct pcmcia_function *tmp;
    434   1.2   thorpej 	int reg;
    435   1.2   thorpej 
    436   1.2   thorpej 	if (pf->cfe == NULL)
    437   1.2   thorpej 		panic("pcmcia_function_enable: function not initialized");
    438   1.2   thorpej 
    439   1.5      marc 	/*
    440   1.5      marc 	 * Increase the reference count on the socket, enabling power, if
    441   1.5      marc 	 * necessary.
    442   1.5      marc 	 */
    443   1.5      marc 	if (pf->sc->sc_enabled_count++ == 0)
    444   1.5      marc 		pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
    445   1.5      marc 	DPRINTF(("%s: ++enabled_count = %d\n", pf->sc->dev.dv_xname,
    446   1.5      marc 		 pf->sc->sc_enabled_count));
    447   1.5      marc 
    448   1.2   thorpej 	if (pf->pf_flags & PFF_ENABLED) {
    449   1.2   thorpej 		/*
    450   1.2   thorpej 		 * Don't do anything if we're already enabled.
    451   1.2   thorpej 		 */
    452   1.2   thorpej 		return (0);
    453   1.2   thorpej 	}
    454   1.2   thorpej 
    455   1.2   thorpej 	/*
    456   1.2   thorpej 	 * it's possible for different functions' CCRs to be in the same
    457   1.2   thorpej 	 * underlying page.  Check for that.
    458   1.2   thorpej 	 */
    459   1.2   thorpej 
    460  1.29     lukem 	SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
    461   1.2   thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    462   1.2   thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    463   1.2   thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    464   1.2   thorpej 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    465   1.2   thorpej 		      tmp->pf_ccr_realsize))) {
    466   1.2   thorpej 			pf->pf_ccrt = tmp->pf_ccrt;
    467   1.2   thorpej 			pf->pf_ccrh = tmp->pf_ccrh;
    468   1.2   thorpej 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    469   1.2   thorpej 
    470   1.2   thorpej 			/*
    471   1.2   thorpej 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    472   1.2   thorpej 			 * tmp->ccr_base) + pf->ccr_base;
    473   1.2   thorpej 			 */
    474   1.2   thorpej 			pf->pf_ccr_offset =
    475   1.2   thorpej 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    476   1.2   thorpej 			    tmp->ccr_base;
    477   1.2   thorpej 			pf->pf_ccr_window = tmp->pf_ccr_window;
    478   1.2   thorpej 			break;
    479   1.2   thorpej 		}
    480   1.2   thorpej 	}
    481   1.2   thorpej 
    482   1.2   thorpej 	if (tmp == NULL) {
    483   1.2   thorpej 		if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
    484   1.2   thorpej 			goto bad;
    485   1.2   thorpej 
    486   1.2   thorpej 		if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    487   1.2   thorpej 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    488   1.2   thorpej 		    &pf->pf_ccr_window)) {
    489   1.2   thorpej 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    490   1.2   thorpej 			goto bad;
    491   1.2   thorpej 		}
    492   1.2   thorpej 	}
    493   1.2   thorpej 
    494  1.41   mycroft 	if (pcmcia_mfc(pf->sc)) {
    495  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    496  1.41   mycroft 				 (pf->pf_mfc_iobase[0] >> 0) & 0xff);
    497  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    498  1.41   mycroft 				 (pf->pf_mfc_iobase[0] >> 8) & 0xff);
    499  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
    500  1.41   mycroft 				 (pf->pf_mfc_iobase[1] >> 0) & 0xff);
    501  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
    502  1.41   mycroft 				 (pf->pf_mfc_iobase[1] >> 8) & 0xff);
    503  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, pf->pf_mfc_iomask);
    504  1.41   mycroft 	}
    505  1.41   mycroft 
    506   1.2   thorpej 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    507   1.2   thorpej 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    508   1.5      marc 	if (pcmcia_mfc(pf->sc)) {
    509   1.5      marc 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    510   1.5      marc 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    511  1.52   mycroft 		if (pf->pf_ih)
    512   1.5      marc 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    513   1.5      marc 
    514   1.5      marc 	}
    515   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    516   1.2   thorpej 
    517   1.2   thorpej 	reg = 0;
    518   1.2   thorpej 	if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
    519   1.2   thorpej 		reg |= PCMCIA_CCR_STATUS_IOIS8;
    520   1.2   thorpej 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    521   1.2   thorpej 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    522   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    523   1.2   thorpej 
    524   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    525   1.2   thorpej 
    526  1.12      marc #ifdef PCMCIADEBUG
    527  1.12      marc 	if (pcmcia_debug) {
    528  1.29     lukem 		SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
    529  1.12      marc 			printf("%s: function %d CCR at %d offset %lx: "
    530  1.12      marc 			       "%x %x %x %x, %x %x %x %x, %x\n",
    531  1.12      marc 			       tmp->sc->dev.dv_xname, tmp->number,
    532  1.15   aymeric 			       tmp->pf_ccr_window,
    533  1.15   aymeric 			       (unsigned long) tmp->pf_ccr_offset,
    534  1.43   mycroft 			       pcmcia_ccr_read(tmp, 0),
    535  1.43   mycroft 			       pcmcia_ccr_read(tmp, 1),
    536  1.43   mycroft 			       pcmcia_ccr_read(tmp, 2),
    537  1.43   mycroft 			       pcmcia_ccr_read(tmp, 3),
    538  1.43   mycroft 
    539  1.43   mycroft 			       pcmcia_ccr_read(tmp, 5),
    540  1.43   mycroft 			       pcmcia_ccr_read(tmp, 6),
    541  1.43   mycroft 			       pcmcia_ccr_read(tmp, 7),
    542  1.43   mycroft 			       pcmcia_ccr_read(tmp, 8),
    543   1.5      marc 
    544  1.43   mycroft 			       pcmcia_ccr_read(tmp, 9));
    545  1.12      marc 		}
    546  1.12      marc 	}
    547  1.12      marc #endif
    548   1.5      marc 
    549   1.2   thorpej 	pf->pf_flags |= PFF_ENABLED;
    550  1.21       uch 
    551  1.21       uch #ifdef IT8368E_LEGACY_MODE
    552  1.21       uch 	/* return to I/O mode */
    553  1.21       uch 	it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
    554  1.21       uch #endif
    555   1.2   thorpej 	return (0);
    556   1.2   thorpej 
    557   1.2   thorpej  bad:
    558   1.2   thorpej 	/*
    559   1.2   thorpej 	 * Decrement the reference count, and power down the socket, if
    560   1.2   thorpej 	 * necessary.
    561   1.2   thorpej 	 */
    562   1.5      marc 	if (--pf->sc->sc_enabled_count == 0)
    563   1.2   thorpej 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    564   1.5      marc 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    565   1.5      marc 		 pf->sc->sc_enabled_count));
    566   1.5      marc 
    567   1.2   thorpej 	return (1);
    568   1.2   thorpej }
    569   1.2   thorpej 
    570   1.2   thorpej /* Disable PCMCIA function. */
    571   1.2   thorpej void
    572   1.2   thorpej pcmcia_function_disable(pf)
    573   1.2   thorpej 	struct pcmcia_function *pf;
    574   1.2   thorpej {
    575   1.2   thorpej 	struct pcmcia_function *tmp;
    576  1.48   mycroft 	int reg;
    577   1.2   thorpej 
    578   1.2   thorpej 	if (pf->cfe == NULL)
    579   1.2   thorpej 		panic("pcmcia_function_enable: function not initialized");
    580   1.2   thorpej 
    581   1.2   thorpej 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    582   1.2   thorpej 		/*
    583  1.36    briggs 		 * Don't do anything but decrement if we're already disabled.
    584   1.2   thorpej 		 */
    585  1.36    briggs 		goto out;
    586   1.2   thorpej 	}
    587   1.2   thorpej 
    588  1.48   mycroft 	if (pcmcia_mfc(pf->sc)) {
    589  1.48   mycroft 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    590  1.48   mycroft 		reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
    591  1.48   mycroft 			 PCMCIA_CCR_OPTION_ADDR_DECODE|
    592  1.48   mycroft 		         PCMCIA_CCR_OPTION_IREQ_ENABLE);
    593  1.48   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    594  1.48   mycroft 	}
    595  1.48   mycroft 
    596   1.2   thorpej 	/*
    597   1.2   thorpej 	 * it's possible for different functions' CCRs to be in the same
    598   1.2   thorpej 	 * underlying page.  Check for that.  Note we mark us as disabled
    599   1.2   thorpej 	 * first to avoid matching ourself.
    600   1.2   thorpej 	 */
    601   1.2   thorpej 
    602   1.2   thorpej 	pf->pf_flags &= ~PFF_ENABLED;
    603  1.29     lukem 	SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
    604   1.2   thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    605   1.2   thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    606   1.2   thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    607   1.2   thorpej 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    608   1.2   thorpej 			break;
    609   1.2   thorpej 	}
    610   1.2   thorpej 
    611   1.2   thorpej 	/* Not used by anyone else; unmap the CCR. */
    612   1.2   thorpej 	if (tmp == NULL) {
    613   1.2   thorpej 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    614   1.2   thorpej 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    615   1.2   thorpej 	}
    616   1.2   thorpej 
    617  1.36    briggs out:
    618   1.2   thorpej 	/*
    619   1.2   thorpej 	 * Decrement the reference count, and power down the socket, if
    620   1.2   thorpej 	 * necessary.
    621   1.2   thorpej 	 */
    622   1.2   thorpej 	if (--pf->sc->sc_enabled_count == 0)
    623   1.2   thorpej 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    624   1.5      marc 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    625   1.5      marc 		 pf->sc->sc_enabled_count));
    626   1.2   thorpej }
    627   1.2   thorpej 
    628   1.2   thorpej int
    629  1.41   mycroft pcmcia_io_map(pf, width, pcihp, windowp)
    630   1.2   thorpej 	struct pcmcia_function *pf;
    631   1.2   thorpej 	int width;
    632   1.2   thorpej 	struct pcmcia_io_handle *pcihp;
    633   1.2   thorpej 	int *windowp;
    634   1.2   thorpej {
    635  1.49   mycroft 	int error;
    636   1.2   thorpej 
    637  1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    638  1.51   mycroft 		printf("pcmcia_io_map: function is enabled!\n");
    639  1.49   mycroft 
    640  1.49   mycroft 	error = pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
    641  1.49   mycroft 	    width, 0, pcihp->size, pcihp, windowp);
    642  1.49   mycroft 	if (error)
    643  1.49   mycroft 		return (error);
    644   1.2   thorpej 
    645   1.2   thorpej 	/*
    646   1.2   thorpej 	 * XXX in the multifunction multi-iospace-per-function case, this
    647   1.2   thorpej 	 * needs to cooperate with io_alloc to make sure that the spaces
    648   1.2   thorpej 	 * don't overlap, and that the ccr's are set correctly
    649   1.2   thorpej 	 */
    650   1.2   thorpej 
    651   1.2   thorpej 	if (pcmcia_mfc(pf->sc)) {
    652  1.41   mycroft 		int win;
    653  1.41   mycroft 		long iomask;
    654   1.5      marc 
    655   1.5      marc 		/* round up to nearest (2^n)-1 */
    656  1.41   mycroft 		for (iomask = 1; iomask < pcihp->size; iomask <<= 1)
    657   1.5      marc 			;
    658  1.41   mycroft 		iomask--;
    659   1.5      marc 
    660  1.41   mycroft 		win = pf->pf_mfc_windows;
    661  1.41   mycroft 		KASSERT(win < 2);
    662  1.41   mycroft printf("win %d = addr %lx size %lx iomask %lx\n", win, (long)pcihp->addr, (long)pcihp->size, (long)iomask);
    663  1.41   mycroft 		pf->pf_mfc_iobase[win] = pcihp->addr;
    664  1.41   mycroft 		pf->pf_mfc_iomask = iomask;
    665   1.2   thorpej 	}
    666  1.41   mycroft 
    667   1.2   thorpej 	return (0);
    668  1.11   thorpej }
    669  1.11   thorpej 
    670  1.11   thorpej void
    671  1.11   thorpej pcmcia_io_unmap(pf, window)
    672  1.11   thorpej 	struct pcmcia_function *pf;
    673  1.11   thorpej 	int window;
    674  1.11   thorpej {
    675  1.11   thorpej 
    676  1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    677  1.51   mycroft 		printf("pcmcia_io_unmap: function is enabled!\n");
    678  1.49   mycroft 
    679  1.41   mycroft 	if (pcmcia_mfc(pf->sc)) {
    680  1.41   mycroft 		/*
    681  1.41   mycroft 		 * Don't bother trying to unmap windows in the CCR here,
    682  1.41   mycroft 		 * because we generally get called when a card has been
    683  1.41   mycroft 		 * ejected, and the CCR isn't there any more.
    684  1.41   mycroft 		 */
    685  1.41   mycroft 		--pf->pf_mfc_windows;
    686  1.41   mycroft 	}
    687  1.41   mycroft 
    688  1.11   thorpej 	pcmcia_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
    689   1.2   thorpej }
    690   1.2   thorpej 
    691   1.2   thorpej void *
    692   1.2   thorpej pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
    693   1.2   thorpej 	struct pcmcia_function *pf;
    694   1.2   thorpej 	int ipl;
    695   1.2   thorpej 	int (*ih_fct) __P((void *));
    696   1.2   thorpej 	void *ih_arg;
    697   1.2   thorpej {
    698   1.2   thorpej 
    699  1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    700  1.51   mycroft 		printf("pcmcia_intr_establish: function is enabled!\n");
    701  1.52   mycroft 	if (pf->pf_ih)
    702  1.52   mycroft 		panic("pcmcia_intr_establish: already done\n");
    703  1.49   mycroft 
    704  1.52   mycroft 	pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
    705  1.52   mycroft 	    pf, ipl, ih_fct, ih_arg);
    706  1.52   mycroft 	return (pf->pf_ih);
    707   1.2   thorpej }
    708   1.2   thorpej 
    709   1.2   thorpej void
    710   1.2   thorpej pcmcia_intr_disestablish(pf, ih)
    711   1.2   thorpej 	struct pcmcia_function *pf;
    712   1.2   thorpej 	void *ih;
    713   1.2   thorpej {
    714  1.52   mycroft 
    715  1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    716  1.51   mycroft 		printf("pcmcia_intr_disestablish: function is enabled!\n");
    717  1.52   mycroft 	if (!pf->pf_ih)
    718  1.52   mycroft 		panic("pcmcia_intr_distestablish: already done\n");
    719  1.49   mycroft 
    720  1.52   mycroft 	pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
    721  1.52   mycroft 	pf->pf_ih = 0;
    722   1.2   thorpej }
    723