Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.49
      1  1.49   mycroft /*	$NetBSD: pcmcia.c,v 1.49 2004/08/09 19:08:19 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.49   mycroft __KERNEL_RCSID(0, "$NetBSD: pcmcia.c,v 1.49 2004/08/09 19:08:19 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.12      marc int	pcmciaintr_debug = 0;
     72  1.12      marc /* this is done this way to avoid doing lots of conditionals
     73  1.12      marc    at interrupt level.  */
     74  1.12      marc #define PCMCIA_CARD_INTR (pcmciaintr_debug?pcmcia_card_intrdebug:pcmcia_card_intr)
     75   1.2   thorpej #else
     76   1.2   thorpej #define	DPRINTF(arg)
     77  1.12      marc #define PCMCIA_CARD_INTR (pcmcia_card_intr)
     78   1.2   thorpej #endif
     79   1.2   thorpej 
     80   1.2   thorpej #ifdef PCMCIAVERBOSE
     81   1.2   thorpej int	pcmcia_verbose = 1;
     82   1.2   thorpej #else
     83   1.2   thorpej int	pcmcia_verbose = 0;
     84   1.2   thorpej #endif
     85   1.2   thorpej 
     86   1.2   thorpej int	pcmcia_match __P((struct device *, struct cfdata *, void *));
     87   1.2   thorpej int	pcmcia_submatch __P((struct device *, struct cfdata *, void *));
     88   1.2   thorpej void	pcmcia_attach __P((struct device *, struct device *, void *));
     89   1.2   thorpej int	pcmcia_print __P((void *, const char *));
     90   1.2   thorpej 
     91   1.5      marc int pcmcia_card_intr __P((void *));
     92  1.12      marc #ifdef PCMCIADEBUG
     93  1.12      marc int pcmcia_card_intrdebug __P((void *));
     94  1.12      marc #endif
     95   1.2   thorpej 
     96  1.33   thorpej CFATTACH_DECL(pcmcia, sizeof(struct pcmcia_softc),
     97  1.34   thorpej     pcmcia_match, pcmcia_attach, NULL, NULL);
     98   1.2   thorpej 
     99   1.2   thorpej int
    100   1.2   thorpej pcmcia_ccr_read(pf, ccr)
    101   1.2   thorpej 	struct pcmcia_function *pf;
    102   1.2   thorpej 	int ccr;
    103   1.2   thorpej {
    104   1.2   thorpej 
    105   1.2   thorpej 	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
    106  1.43   mycroft 	    pf->pf_ccr_offset + ccr * 2));
    107   1.2   thorpej }
    108   1.2   thorpej 
    109   1.2   thorpej void
    110   1.2   thorpej pcmcia_ccr_write(pf, ccr, val)
    111   1.2   thorpej 	struct pcmcia_function *pf;
    112   1.2   thorpej 	int ccr;
    113   1.2   thorpej 	int val;
    114   1.2   thorpej {
    115   1.2   thorpej 
    116  1.43   mycroft 	if (pf->ccr_mask & (1 << ccr)) {
    117   1.2   thorpej 		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
    118  1.43   mycroft 		    pf->pf_ccr_offset + ccr * 2, val);
    119   1.2   thorpej 	}
    120   1.2   thorpej }
    121   1.2   thorpej 
    122   1.2   thorpej int
    123   1.2   thorpej pcmcia_match(parent, match, aux)
    124   1.2   thorpej 	struct device *parent;
    125   1.2   thorpej 	struct cfdata *match;
    126   1.2   thorpej 	void *aux;
    127   1.2   thorpej {
    128  1.14      haya 	struct pcmciabus_attach_args *paa = aux;
    129  1.14      haya 
    130  1.30   thorpej 	if (strcmp(paa->paa_busname, match->cf_name)) {
    131  1.14      haya 	    return 0;
    132  1.14      haya 	}
    133   1.2   thorpej 	/* if the autoconfiguration got this far, there's a socket here */
    134   1.2   thorpej 	return (1);
    135   1.2   thorpej }
    136   1.2   thorpej 
    137   1.2   thorpej void
    138   1.2   thorpej pcmcia_attach(parent, self, aux)
    139   1.2   thorpej 	struct device *parent, *self;
    140   1.2   thorpej 	void *aux;
    141   1.2   thorpej {
    142   1.2   thorpej 	struct pcmciabus_attach_args *paa = aux;
    143   1.2   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
    144   1.2   thorpej 
    145   1.2   thorpej 	printf("\n");
    146   1.2   thorpej 
    147   1.2   thorpej 	sc->pct = paa->pct;
    148   1.2   thorpej 	sc->pch = paa->pch;
    149   1.2   thorpej 	sc->iobase = paa->iobase;
    150   1.2   thorpej 	sc->iosize = paa->iosize;
    151   1.2   thorpej 
    152   1.2   thorpej 	sc->ih = NULL;
    153   1.2   thorpej }
    154   1.2   thorpej 
    155   1.2   thorpej int
    156   1.2   thorpej pcmcia_card_attach(dev)
    157   1.2   thorpej 	struct device *dev;
    158   1.2   thorpej {
    159   1.2   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    160   1.2   thorpej 	struct pcmcia_function *pf;
    161   1.2   thorpej 	struct pcmcia_attach_args paa;
    162   1.2   thorpej 	int attached;
    163   1.2   thorpej 
    164   1.2   thorpej 	/*
    165   1.2   thorpej 	 * this is here so that when socket_enable calls gettype, trt happens
    166   1.2   thorpej 	 */
    167  1.29     lukem 	SIMPLEQ_FIRST(&sc->card.pf_head) = NULL;
    168   1.2   thorpej 
    169   1.2   thorpej 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    170   1.2   thorpej 
    171   1.2   thorpej 	pcmcia_read_cis(sc);
    172   1.2   thorpej 
    173   1.2   thorpej 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    174  1.13      marc 
    175  1.13      marc 	pcmcia_check_cis_quirks(sc);
    176   1.2   thorpej 
    177   1.2   thorpej 	/*
    178   1.2   thorpej 	 * bail now if the card has no functions, or if there was an error in
    179   1.2   thorpej 	 * the cis.
    180   1.2   thorpej 	 */
    181   1.2   thorpej 
    182   1.2   thorpej 	if (sc->card.error)
    183   1.2   thorpej 		return (1);
    184  1.29     lukem 	if (SIMPLEQ_EMPTY(&sc->card.pf_head))
    185   1.2   thorpej 		return (1);
    186   1.2   thorpej 
    187   1.2   thorpej 	if (pcmcia_verbose)
    188   1.2   thorpej 		pcmcia_print_cis(sc);
    189   1.2   thorpej 
    190   1.2   thorpej 	attached = 0;
    191   1.2   thorpej 
    192  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    193  1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    194   1.2   thorpej 			continue;
    195   1.2   thorpej 
    196  1.11   thorpej #ifdef DIAGNOSTIC
    197  1.11   thorpej 		if (pf->child != NULL) {
    198  1.11   thorpej 			printf("%s: %s still attached to function %d!\n",
    199  1.11   thorpej 			    sc->dev.dv_xname, pf->child->dv_xname,
    200  1.11   thorpej 			    pf->number);
    201  1.11   thorpej 			panic("pcmcia_card_attach");
    202  1.11   thorpej 		}
    203  1.11   thorpej #endif
    204   1.2   thorpej 		pf->sc = sc;
    205  1.11   thorpej 		pf->child = NULL;
    206   1.2   thorpej 		pf->cfe = NULL;
    207   1.2   thorpej 		pf->ih_fct = NULL;
    208   1.2   thorpej 		pf->ih_arg = NULL;
    209   1.2   thorpej 	}
    210   1.2   thorpej 
    211  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    212  1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    213   1.2   thorpej 			continue;
    214   1.2   thorpej 
    215   1.2   thorpej 		paa.manufacturer = sc->card.manufacturer;
    216   1.2   thorpej 		paa.product = sc->card.product;
    217   1.2   thorpej 		paa.card = &sc->card;
    218   1.2   thorpej 		paa.pf = pf;
    219   1.2   thorpej 
    220  1.11   thorpej 		if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
    221  1.20    chopps 		    pcmcia_submatch)) != NULL)
    222   1.2   thorpej 			attached++;
    223   1.2   thorpej 	}
    224   1.2   thorpej 
    225   1.2   thorpej 	return (attached ? 0 : 1);
    226   1.2   thorpej }
    227   1.2   thorpej 
    228   1.2   thorpej void
    229  1.11   thorpej pcmcia_card_detach(dev, flags)
    230   1.2   thorpej 	struct device *dev;
    231  1.11   thorpej 	int flags;		/* DETACH_* flags */
    232   1.2   thorpej {
    233  1.10   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    234  1.11   thorpej 	struct pcmcia_function *pf;
    235  1.11   thorpej 	int error;
    236  1.10   thorpej 
    237  1.11   thorpej 	/*
    238  1.11   thorpej 	 * We are running on either the PCMCIA socket's event thread
    239  1.11   thorpej 	 * or in user context detaching a device by user request.
    240  1.10   thorpej 	 */
    241  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    242  1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    243  1.11   thorpej 			continue;
    244  1.11   thorpej 		if (pf->child == NULL)
    245  1.11   thorpej 			continue;
    246  1.11   thorpej 		DPRINTF(("%s: detaching %s (function %d)\n",
    247  1.11   thorpej 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    248  1.11   thorpej 		if ((error = config_detach(pf->child, flags)) != 0) {
    249  1.11   thorpej 			printf("%s: error %d detaching %s (function %d)\n",
    250  1.11   thorpej 			    sc->dev.dv_xname, error, pf->child->dv_xname,
    251  1.11   thorpej 			    pf->number);
    252  1.11   thorpej 		} else
    253  1.11   thorpej 			pf->child = NULL;
    254  1.11   thorpej 	}
    255  1.11   thorpej }
    256  1.11   thorpej 
    257  1.11   thorpej void
    258  1.11   thorpej pcmcia_card_deactivate(dev)
    259  1.11   thorpej 	struct device *dev;
    260  1.11   thorpej {
    261  1.11   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    262  1.11   thorpej 	struct pcmcia_function *pf;
    263  1.10   thorpej 
    264  1.11   thorpej 	/*
    265  1.11   thorpej 	 * We're in the chip's card removal interrupt handler.
    266  1.11   thorpej 	 * Deactivate the child driver.  The PCMCIA socket's
    267  1.11   thorpej 	 * event thread will run later to finish the detach.
    268  1.11   thorpej 	 */
    269  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    270  1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    271  1.11   thorpej 			continue;
    272  1.11   thorpej 		if (pf->child == NULL)
    273  1.11   thorpej 			continue;
    274  1.11   thorpej 		DPRINTF(("%s: deactivating %s (function %d)\n",
    275  1.11   thorpej 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    276  1.11   thorpej 		config_deactivate(pf->child);
    277  1.11   thorpej 	}
    278   1.2   thorpej }
    279   1.2   thorpej 
    280   1.2   thorpej int
    281   1.2   thorpej pcmcia_submatch(parent, cf, aux)
    282   1.2   thorpej 	struct device *parent;
    283   1.2   thorpej 	struct cfdata *cf;
    284   1.2   thorpej 	void *aux;
    285   1.2   thorpej {
    286   1.2   thorpej 	struct pcmcia_attach_args *paa = aux;
    287   1.2   thorpej 
    288   1.3     enami 	if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
    289   1.3     enami 	    cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
    290   1.2   thorpej 		return (0);
    291   1.2   thorpej 
    292  1.31   thorpej 	return (config_match(parent, cf, aux));
    293   1.2   thorpej }
    294   1.2   thorpej 
    295   1.2   thorpej int
    296   1.2   thorpej pcmcia_print(arg, pnp)
    297   1.2   thorpej 	void *arg;
    298   1.2   thorpej 	const char *pnp;
    299   1.2   thorpej {
    300   1.2   thorpej 	struct pcmcia_attach_args *pa = arg;
    301   1.2   thorpej 	struct pcmcia_softc *sc = pa->pf->sc;
    302   1.2   thorpej 	struct pcmcia_card *card = &sc->card;
    303  1.18  augustss 	char devinfo[256];
    304   1.2   thorpej 
    305  1.38   mycroft 	if (pnp)
    306  1.38   mycroft 		aprint_normal("%s", pnp);
    307  1.38   mycroft 
    308  1.38   mycroft 	pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
    309  1.38   mycroft 
    310  1.38   mycroft 	aprint_normal(" function %d: %s", pa->pf->number, devinfo);
    311   1.2   thorpej 
    312   1.2   thorpej 	return (UNCONF);
    313  1.18  augustss }
    314  1.18  augustss 
    315  1.18  augustss void
    316  1.18  augustss pcmcia_devinfo(card, showhex, cp, cplen)
    317  1.18  augustss 	struct pcmcia_card *card;
    318  1.19     enami 	int showhex;
    319  1.18  augustss 	char *cp;
    320  1.38   mycroft 	size_t cplen;
    321  1.18  augustss {
    322  1.18  augustss 	int i, n;
    323  1.18  augustss 
    324  1.37   mycroft 	if (cplen > 1) {
    325  1.37   mycroft 		*cp++ = '<';
    326  1.38   mycroft 		*cp = '\0';
    327  1.37   mycroft 		cplen--;
    328  1.37   mycroft 	}
    329  1.37   mycroft 
    330  1.37   mycroft 	for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
    331  1.39   mycroft 		n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
    332  1.18  augustss 		        card->cis1_info[i]);
    333  1.18  augustss 		cp += n;
    334  1.38   mycroft 		if (cplen < n)
    335  1.38   mycroft 			return;
    336  1.18  augustss 		cplen -= n;
    337  1.18  augustss 	}
    338  1.37   mycroft 
    339  1.37   mycroft 	if (cplen > 1) {
    340  1.37   mycroft 		*cp++ = '>';
    341  1.38   mycroft 		*cp = '\0';
    342  1.37   mycroft 		cplen--;
    343  1.37   mycroft 	}
    344  1.37   mycroft 
    345  1.37   mycroft 	if (showhex && cplen > 1)
    346  1.37   mycroft 		snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
    347  1.37   mycroft 		    card->manufacturer, card->product);
    348  1.16       cgd }
    349  1.16       cgd 
    350  1.16       cgd const struct pcmcia_product *
    351  1.16       cgd pcmcia_product_lookup(pa, tab, ent_size, matchfn)
    352  1.16       cgd 	struct pcmcia_attach_args *pa;
    353  1.16       cgd 	const struct pcmcia_product *tab;
    354  1.16       cgd 	size_t ent_size;
    355  1.16       cgd 	pcmcia_product_match_fn matchfn;
    356  1.16       cgd {
    357  1.16       cgd         const struct pcmcia_product *ent;
    358  1.16       cgd 	int matches;
    359  1.16       cgd 
    360  1.16       cgd #ifdef DIAGNOSTIC
    361  1.16       cgd 	if (sizeof *ent > ent_size)
    362  1.17   nathanw 		panic("pcmcia_product_lookup: bogus ent_size %ld",
    363  1.17   nathanw 		      (long) ent_size);
    364  1.16       cgd #endif
    365  1.16       cgd 
    366  1.16       cgd         for (ent = tab;
    367  1.16       cgd 	    ent->pp_name != NULL;
    368  1.16       cgd 	    ent = (const struct pcmcia_product *)
    369  1.16       cgd 	      ((const char *)ent + ent_size)) {
    370  1.16       cgd 
    371  1.16       cgd 		/* see if it matches vendor/product/function */
    372  1.16       cgd 		matches = (pa->manufacturer == ent->pp_vendor) &&
    373  1.16       cgd 		    (pa->product == ent->pp_product) &&
    374  1.16       cgd 		    (pa->pf->number == ent->pp_expfunc);
    375  1.16       cgd 
    376  1.16       cgd 		/* if a separate match function is given, let it override */
    377  1.16       cgd 		if (matchfn != NULL)
    378  1.16       cgd 			matches = (*matchfn)(pa, ent, matches);
    379  1.16       cgd 
    380  1.16       cgd 		if (matches)
    381  1.16       cgd                         return (ent);
    382  1.16       cgd         }
    383  1.16       cgd         return (NULL);
    384   1.2   thorpej }
    385   1.2   thorpej 
    386   1.2   thorpej int
    387   1.2   thorpej pcmcia_card_gettype(dev)
    388   1.2   thorpej 	struct device  *dev;
    389   1.2   thorpej {
    390   1.7     enami 	struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
    391   1.7     enami 	struct pcmcia_function *pf;
    392   1.2   thorpej 
    393   1.2   thorpej 	/*
    394   1.2   thorpej 	 * set the iftype to memory if this card has no functions (not yet
    395   1.7     enami 	 * probed), or only one function, and that is not initialized yet or
    396   1.7     enami 	 * that is memory.
    397   1.2   thorpej 	 */
    398   1.7     enami 	pf = SIMPLEQ_FIRST(&sc->card.pf_head);
    399   1.7     enami 	if (pf == NULL ||
    400   1.7     enami 	    (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
    401   1.7     enami 	    (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
    402   1.2   thorpej 		return (PCMCIA_IFTYPE_MEMORY);
    403   1.2   thorpej 	else
    404   1.2   thorpej 		return (PCMCIA_IFTYPE_IO);
    405   1.2   thorpej }
    406   1.2   thorpej 
    407   1.2   thorpej /*
    408   1.2   thorpej  * Initialize a PCMCIA function.  May be called as long as the function is
    409   1.2   thorpej  * disabled.
    410   1.2   thorpej  */
    411   1.2   thorpej void
    412   1.2   thorpej pcmcia_function_init(pf, cfe)
    413   1.2   thorpej 	struct pcmcia_function *pf;
    414   1.2   thorpej 	struct pcmcia_config_entry *cfe;
    415   1.2   thorpej {
    416   1.2   thorpej 	if (pf->pf_flags & PFF_ENABLED)
    417   1.2   thorpej 		panic("pcmcia_function_init: function is enabled");
    418   1.2   thorpej 
    419   1.2   thorpej 	/* Remember which configuration entry we are using. */
    420   1.2   thorpej 	pf->cfe = cfe;
    421   1.2   thorpej }
    422   1.2   thorpej 
    423  1.40   mycroft void pcmcia_socket_enable(dev)
    424  1.40   mycroft 	struct device *dev;
    425   1.5      marc {
    426  1.40   mycroft 	struct pcmcia_softc *sc = (void *)dev;
    427  1.40   mycroft 
    428  1.40   mycroft 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    429   1.5      marc }
    430   1.5      marc 
    431  1.40   mycroft void pcmcia_socket_disable(dev)
    432  1.40   mycroft 	struct device *dev;
    433   1.5      marc {
    434  1.40   mycroft 	struct pcmcia_softc *sc = (void *)dev;
    435  1.40   mycroft 
    436  1.40   mycroft 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    437   1.5      marc }
    438   1.5      marc 
    439   1.2   thorpej /* Enable a PCMCIA function */
    440   1.2   thorpej int
    441   1.2   thorpej pcmcia_function_enable(pf)
    442   1.2   thorpej 	struct pcmcia_function *pf;
    443   1.2   thorpej {
    444   1.2   thorpej 	struct pcmcia_function *tmp;
    445   1.2   thorpej 	int reg;
    446   1.2   thorpej 
    447   1.2   thorpej 	if (pf->cfe == NULL)
    448   1.2   thorpej 		panic("pcmcia_function_enable: function not initialized");
    449   1.2   thorpej 
    450   1.5      marc 	/*
    451   1.5      marc 	 * Increase the reference count on the socket, enabling power, if
    452   1.5      marc 	 * necessary.
    453   1.5      marc 	 */
    454   1.5      marc 	if (pf->sc->sc_enabled_count++ == 0)
    455   1.5      marc 		pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
    456   1.5      marc 	DPRINTF(("%s: ++enabled_count = %d\n", pf->sc->dev.dv_xname,
    457   1.5      marc 		 pf->sc->sc_enabled_count));
    458   1.5      marc 
    459   1.2   thorpej 	if (pf->pf_flags & PFF_ENABLED) {
    460   1.2   thorpej 		/*
    461   1.2   thorpej 		 * Don't do anything if we're already enabled.
    462   1.2   thorpej 		 */
    463   1.2   thorpej 		return (0);
    464   1.2   thorpej 	}
    465   1.2   thorpej 
    466   1.2   thorpej 	/*
    467   1.2   thorpej 	 * it's possible for different functions' CCRs to be in the same
    468   1.2   thorpej 	 * underlying page.  Check for that.
    469   1.2   thorpej 	 */
    470   1.2   thorpej 
    471  1.29     lukem 	SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
    472   1.2   thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    473   1.2   thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    474   1.2   thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    475   1.2   thorpej 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    476   1.2   thorpej 		      tmp->pf_ccr_realsize))) {
    477   1.2   thorpej 			pf->pf_ccrt = tmp->pf_ccrt;
    478   1.2   thorpej 			pf->pf_ccrh = tmp->pf_ccrh;
    479   1.2   thorpej 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    480   1.2   thorpej 
    481   1.2   thorpej 			/*
    482   1.2   thorpej 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    483   1.2   thorpej 			 * tmp->ccr_base) + pf->ccr_base;
    484   1.2   thorpej 			 */
    485   1.2   thorpej 			pf->pf_ccr_offset =
    486   1.2   thorpej 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    487   1.2   thorpej 			    tmp->ccr_base;
    488   1.2   thorpej 			pf->pf_ccr_window = tmp->pf_ccr_window;
    489   1.2   thorpej 			break;
    490   1.2   thorpej 		}
    491   1.2   thorpej 	}
    492   1.2   thorpej 
    493   1.2   thorpej 	if (tmp == NULL) {
    494   1.2   thorpej 		if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
    495   1.2   thorpej 			goto bad;
    496   1.2   thorpej 
    497   1.2   thorpej 		if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    498   1.2   thorpej 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    499   1.2   thorpej 		    &pf->pf_ccr_window)) {
    500   1.2   thorpej 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    501   1.2   thorpej 			goto bad;
    502   1.2   thorpej 		}
    503   1.2   thorpej 	}
    504   1.2   thorpej 
    505  1.41   mycroft 	if (pcmcia_mfc(pf->sc)) {
    506  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    507  1.41   mycroft 				 (pf->pf_mfc_iobase[0] >> 0) & 0xff);
    508  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    509  1.41   mycroft 				 (pf->pf_mfc_iobase[0] >> 8) & 0xff);
    510  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
    511  1.41   mycroft 				 (pf->pf_mfc_iobase[1] >> 0) & 0xff);
    512  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
    513  1.41   mycroft 				 (pf->pf_mfc_iobase[1] >> 8) & 0xff);
    514  1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, pf->pf_mfc_iomask);
    515  1.41   mycroft 	}
    516  1.41   mycroft 
    517   1.2   thorpej 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    518   1.2   thorpej 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    519   1.5      marc 	if (pcmcia_mfc(pf->sc)) {
    520   1.5      marc 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    521   1.5      marc 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    522   1.5      marc 		if (pf->ih_fct)
    523   1.5      marc 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    524   1.5      marc 
    525   1.5      marc 	}
    526   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    527   1.2   thorpej 
    528   1.2   thorpej 	reg = 0;
    529   1.2   thorpej 	if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
    530   1.2   thorpej 		reg |= PCMCIA_CCR_STATUS_IOIS8;
    531   1.2   thorpej 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    532   1.2   thorpej 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    533   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    534   1.2   thorpej 
    535   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    536   1.2   thorpej 
    537  1.12      marc #ifdef PCMCIADEBUG
    538  1.12      marc 	if (pcmcia_debug) {
    539  1.29     lukem 		SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
    540  1.12      marc 			printf("%s: function %d CCR at %d offset %lx: "
    541  1.12      marc 			       "%x %x %x %x, %x %x %x %x, %x\n",
    542  1.12      marc 			       tmp->sc->dev.dv_xname, tmp->number,
    543  1.15   aymeric 			       tmp->pf_ccr_window,
    544  1.15   aymeric 			       (unsigned long) tmp->pf_ccr_offset,
    545  1.43   mycroft 			       pcmcia_ccr_read(tmp, 0),
    546  1.43   mycroft 			       pcmcia_ccr_read(tmp, 1),
    547  1.43   mycroft 			       pcmcia_ccr_read(tmp, 2),
    548  1.43   mycroft 			       pcmcia_ccr_read(tmp, 3),
    549  1.43   mycroft 
    550  1.43   mycroft 			       pcmcia_ccr_read(tmp, 5),
    551  1.43   mycroft 			       pcmcia_ccr_read(tmp, 6),
    552  1.43   mycroft 			       pcmcia_ccr_read(tmp, 7),
    553  1.43   mycroft 			       pcmcia_ccr_read(tmp, 8),
    554   1.5      marc 
    555  1.43   mycroft 			       pcmcia_ccr_read(tmp, 9));
    556  1.12      marc 		}
    557  1.12      marc 	}
    558  1.12      marc #endif
    559   1.5      marc 
    560   1.2   thorpej 	pf->pf_flags |= PFF_ENABLED;
    561  1.21       uch 
    562  1.21       uch #ifdef IT8368E_LEGACY_MODE
    563  1.21       uch 	/* return to I/O mode */
    564  1.21       uch 	it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
    565  1.21       uch #endif
    566   1.2   thorpej 	return (0);
    567   1.2   thorpej 
    568   1.2   thorpej  bad:
    569   1.2   thorpej 	/*
    570   1.2   thorpej 	 * Decrement the reference count, and power down the socket, if
    571   1.2   thorpej 	 * necessary.
    572   1.2   thorpej 	 */
    573   1.5      marc 	if (--pf->sc->sc_enabled_count == 0)
    574   1.2   thorpej 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    575   1.5      marc 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    576   1.5      marc 		 pf->sc->sc_enabled_count));
    577   1.5      marc 
    578   1.2   thorpej 	return (1);
    579   1.2   thorpej }
    580   1.2   thorpej 
    581   1.2   thorpej /* Disable PCMCIA function. */
    582   1.2   thorpej void
    583   1.2   thorpej pcmcia_function_disable(pf)
    584   1.2   thorpej 	struct pcmcia_function *pf;
    585   1.2   thorpej {
    586   1.2   thorpej 	struct pcmcia_function *tmp;
    587  1.48   mycroft 	int reg;
    588   1.2   thorpej 
    589   1.2   thorpej 	if (pf->cfe == NULL)
    590   1.2   thorpej 		panic("pcmcia_function_enable: function not initialized");
    591   1.2   thorpej 
    592   1.2   thorpej 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    593   1.2   thorpej 		/*
    594  1.36    briggs 		 * Don't do anything but decrement if we're already disabled.
    595   1.2   thorpej 		 */
    596  1.36    briggs 		goto out;
    597   1.2   thorpej 	}
    598   1.2   thorpej 
    599  1.48   mycroft 	if (pcmcia_mfc(pf->sc)) {
    600  1.48   mycroft 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    601  1.48   mycroft 		reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
    602  1.48   mycroft 			 PCMCIA_CCR_OPTION_ADDR_DECODE|
    603  1.48   mycroft 		         PCMCIA_CCR_OPTION_IREQ_ENABLE);
    604  1.48   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    605  1.48   mycroft 	}
    606  1.48   mycroft 
    607   1.2   thorpej 	/*
    608   1.2   thorpej 	 * it's possible for different functions' CCRs to be in the same
    609   1.2   thorpej 	 * underlying page.  Check for that.  Note we mark us as disabled
    610   1.2   thorpej 	 * first to avoid matching ourself.
    611   1.2   thorpej 	 */
    612   1.2   thorpej 
    613   1.2   thorpej 	pf->pf_flags &= ~PFF_ENABLED;
    614  1.29     lukem 	SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
    615   1.2   thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    616   1.2   thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    617   1.2   thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    618   1.2   thorpej 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    619   1.2   thorpej 			break;
    620   1.2   thorpej 	}
    621   1.2   thorpej 
    622   1.2   thorpej 	/* Not used by anyone else; unmap the CCR. */
    623   1.2   thorpej 	if (tmp == NULL) {
    624   1.2   thorpej 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    625   1.2   thorpej 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    626   1.2   thorpej 	}
    627   1.2   thorpej 
    628  1.36    briggs out:
    629   1.2   thorpej 	/*
    630   1.2   thorpej 	 * Decrement the reference count, and power down the socket, if
    631   1.2   thorpej 	 * necessary.
    632   1.2   thorpej 	 */
    633   1.2   thorpej 	if (--pf->sc->sc_enabled_count == 0)
    634   1.2   thorpej 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    635   1.5      marc 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    636   1.5      marc 		 pf->sc->sc_enabled_count));
    637   1.2   thorpej }
    638   1.2   thorpej 
    639   1.2   thorpej int
    640  1.41   mycroft pcmcia_io_map(pf, width, pcihp, windowp)
    641   1.2   thorpej 	struct pcmcia_function *pf;
    642   1.2   thorpej 	int width;
    643   1.2   thorpej 	struct pcmcia_io_handle *pcihp;
    644   1.2   thorpej 	int *windowp;
    645   1.2   thorpej {
    646  1.49   mycroft 	int error;
    647   1.2   thorpej 	int reg;
    648   1.2   thorpej 
    649  1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    650  1.49   mycroft 		printf("pcmcia_io_map: function is enabled!");
    651  1.49   mycroft 
    652  1.49   mycroft 	error = pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
    653  1.49   mycroft 	    width, 0, pcihp->size, pcihp, windowp);
    654  1.49   mycroft 	if (error)
    655  1.49   mycroft 		return (error);
    656   1.2   thorpej 
    657   1.2   thorpej 	/*
    658   1.2   thorpej 	 * XXX in the multifunction multi-iospace-per-function case, this
    659   1.2   thorpej 	 * needs to cooperate with io_alloc to make sure that the spaces
    660   1.2   thorpej 	 * don't overlap, and that the ccr's are set correctly
    661   1.2   thorpej 	 */
    662   1.2   thorpej 
    663   1.2   thorpej 	if (pcmcia_mfc(pf->sc)) {
    664  1.41   mycroft 		int win;
    665  1.41   mycroft 		long iomask;
    666   1.5      marc 
    667   1.5      marc 		/* round up to nearest (2^n)-1 */
    668  1.41   mycroft 		for (iomask = 1; iomask < pcihp->size; iomask <<= 1)
    669   1.5      marc 			;
    670  1.41   mycroft 		iomask--;
    671   1.5      marc 
    672  1.41   mycroft 		win = pf->pf_mfc_windows;
    673  1.41   mycroft 		KASSERT(win < 2);
    674  1.41   mycroft printf("win %d = addr %lx size %lx iomask %lx\n", win, (long)pcihp->addr, (long)pcihp->size, (long)iomask);
    675  1.41   mycroft 		pf->pf_mfc_iobase[win] = pcihp->addr;
    676  1.41   mycroft 		pf->pf_mfc_iomask = iomask;
    677   1.2   thorpej 	}
    678  1.41   mycroft 
    679   1.2   thorpej 	return (0);
    680  1.11   thorpej }
    681  1.11   thorpej 
    682  1.11   thorpej void
    683  1.11   thorpej pcmcia_io_unmap(pf, window)
    684  1.11   thorpej 	struct pcmcia_function *pf;
    685  1.11   thorpej 	int window;
    686  1.11   thorpej {
    687  1.11   thorpej 
    688  1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    689  1.49   mycroft 		printf("pcmcia_io_unmap: function is enabled!");
    690  1.49   mycroft 
    691  1.41   mycroft 	if (pcmcia_mfc(pf->sc)) {
    692  1.41   mycroft 		/*
    693  1.41   mycroft 		 * Don't bother trying to unmap windows in the CCR here,
    694  1.41   mycroft 		 * because we generally get called when a card has been
    695  1.41   mycroft 		 * ejected, and the CCR isn't there any more.
    696  1.41   mycroft 		 */
    697  1.41   mycroft 		--pf->pf_mfc_windows;
    698  1.41   mycroft 	}
    699  1.41   mycroft 
    700  1.11   thorpej 	pcmcia_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
    701   1.2   thorpej }
    702   1.2   thorpej 
    703   1.2   thorpej void *
    704   1.2   thorpej pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
    705   1.2   thorpej 	struct pcmcia_function *pf;
    706   1.2   thorpej 	int ipl;
    707   1.2   thorpej 	int (*ih_fct) __P((void *));
    708   1.2   thorpej 	void *ih_arg;
    709   1.2   thorpej {
    710   1.2   thorpej 	void *ret;
    711   1.2   thorpej 
    712  1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    713  1.49   mycroft 		printf("pcmcia_intr_establish: function is enabled!");
    714  1.49   mycroft 
    715   1.2   thorpej 	/* behave differently if this is a multifunction card */
    716   1.2   thorpej 	if (pcmcia_mfc(pf->sc)) {
    717  1.49   mycroft 		int s, ihcnt, hiipl;
    718   1.2   thorpej 		struct pcmcia_function *pf2;
    719   1.2   thorpej 
    720   1.2   thorpej 		/*
    721   1.2   thorpej 		 * mask all the ipl's which are already used by this card,
    722   1.2   thorpej 		 * and find the highest ipl number (lowest priority)
    723   1.2   thorpej 		 */
    724   1.2   thorpej 
    725   1.2   thorpej 		ihcnt = 0;
    726   1.5      marc 		s = 0;		/* this is only here to keep the compiler
    727   1.2   thorpej 				   happy */
    728   1.5      marc 		hiipl = 0;	/* this is only here to keep the compiler
    729   1.2   thorpej 				   happy */
    730   1.2   thorpej 
    731  1.29     lukem 		SIMPLEQ_FOREACH(pf2, &pf->sc->card.pf_head, pf_list) {
    732   1.2   thorpej 			if (pf2->ih_fct) {
    733   1.5      marc 				DPRINTF(("%s: function %d has ih_fct %p\n",
    734   1.5      marc 					 pf->sc->dev.dv_xname, pf2->number,
    735   1.5      marc 					 pf2->ih_fct));
    736   1.5      marc 
    737   1.2   thorpej 				if (ihcnt == 0) {
    738   1.2   thorpej 					hiipl = pf2->ih_ipl;
    739   1.2   thorpej 				} else {
    740   1.2   thorpej 					if (pf2->ih_ipl > hiipl)
    741   1.2   thorpej 						hiipl = pf2->ih_ipl;
    742   1.2   thorpej 				}
    743   1.5      marc 
    744   1.5      marc 				ihcnt++;
    745   1.2   thorpej 			}
    746   1.2   thorpej 		}
    747   1.2   thorpej 
    748   1.2   thorpej 		/*
    749   1.2   thorpej 		 * establish the real interrupt, changing the ipl if
    750   1.2   thorpej 		 * necessary
    751   1.2   thorpej 		 */
    752   1.2   thorpej 
    753   1.2   thorpej 		if (ihcnt == 0) {
    754   1.2   thorpej #ifdef DIAGNOSTIC
    755   1.2   thorpej 			if (pf->sc->ih != NULL)
    756   1.2   thorpej 				panic("card has intr handler, but no function does");
    757   1.2   thorpej #endif
    758   1.6  christos 			s = splhigh();
    759   1.6  christos 
    760   1.5      marc 			/* set up the handler for the new function */
    761   1.5      marc 
    762   1.5      marc 			pf->ih_fct = ih_fct;
    763   1.5      marc 			pf->ih_arg = ih_arg;
    764   1.5      marc 			pf->ih_ipl = ipl;
    765   1.2   thorpej 
    766   1.2   thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    767  1.12      marc 			    pf->sc->pch, pf, ipl, PCMCIA_CARD_INTR, pf->sc);
    768   1.6  christos 			splx(s);
    769   1.2   thorpej 		} else if (ipl > hiipl) {
    770   1.2   thorpej #ifdef DIAGNOSTIC
    771   1.2   thorpej 			if (pf->sc->ih == NULL)
    772   1.2   thorpej 				panic("functions have ih, but the card does not");
    773   1.2   thorpej #endif
    774   1.2   thorpej 
    775   1.5      marc 			/* XXX need #ifdef for splserial on x86 */
    776   1.5      marc 			s = splhigh();
    777   1.5      marc 
    778   1.6  christos 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    779   1.6  christos 						      pf->sc->ih);
    780   1.6  christos 
    781   1.5      marc 			/* set up the handler for the new function */
    782   1.5      marc 			pf->ih_fct = ih_fct;
    783   1.5      marc 			pf->ih_arg = ih_arg;
    784   1.5      marc 			pf->ih_ipl = ipl;
    785   1.5      marc 
    786   1.2   thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    787  1.12      marc 			    pf->sc->pch, pf, ipl, PCMCIA_CARD_INTR, pf->sc);
    788   1.5      marc 
    789   1.5      marc 			splx(s);
    790   1.5      marc 		} else {
    791   1.5      marc 			s = splhigh();
    792   1.5      marc 
    793   1.5      marc 			/* set up the handler for the new function */
    794   1.5      marc 
    795   1.5      marc 			pf->ih_fct = ih_fct;
    796   1.5      marc 			pf->ih_arg = ih_arg;
    797   1.5      marc 			pf->ih_ipl = ipl;
    798   1.5      marc 
    799   1.5      marc 			splx(s);
    800   1.2   thorpej 		}
    801   1.2   thorpej 
    802   1.2   thorpej 		ret = pf->sc->ih;
    803   1.2   thorpej 	} else {
    804   1.2   thorpej 		ret = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
    805   1.2   thorpej 		    pf, ipl, ih_fct, ih_arg);
    806   1.2   thorpej 	}
    807   1.2   thorpej 
    808   1.2   thorpej 	return (ret);
    809   1.2   thorpej }
    810   1.2   thorpej 
    811   1.2   thorpej void
    812   1.2   thorpej pcmcia_intr_disestablish(pf, ih)
    813   1.2   thorpej 	struct pcmcia_function *pf;
    814   1.2   thorpej 	void *ih;
    815   1.2   thorpej {
    816  1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    817  1.49   mycroft 		printf("pcmcia_intr_disestablish: function is enabled!");
    818  1.49   mycroft 
    819   1.2   thorpej 	/* behave differently if this is a multifunction card */
    820   1.2   thorpej 	if (pcmcia_mfc(pf->sc)) {
    821   1.2   thorpej 		int s, ihcnt, hiipl;
    822   1.2   thorpej 		struct pcmcia_function *pf2;
    823   1.2   thorpej 
    824   1.2   thorpej 		/*
    825   1.2   thorpej 		 * mask all the ipl's which are already used by this card,
    826   1.2   thorpej 		 * and find the highest ipl number (lowest priority).  Skip
    827   1.2   thorpej 		 * the current function.
    828   1.2   thorpej 		 */
    829   1.2   thorpej 
    830   1.2   thorpej 		ihcnt = 0;
    831  1.22  augustss 		s = 0;		/* avoid compiler warning */
    832  1.22  augustss 		hiipl = 0;	/* avoid compiler warning */
    833   1.2   thorpej 
    834  1.29     lukem 		SIMPLEQ_FOREACH(pf2, &pf->sc->card.pf_head, pf_list) {
    835   1.2   thorpej 			if (pf2 == pf)
    836   1.2   thorpej 				continue;
    837   1.2   thorpej 
    838   1.2   thorpej 			if (pf2->ih_fct) {
    839   1.2   thorpej 				if (ihcnt == 0) {
    840   1.2   thorpej 					hiipl = pf2->ih_ipl;
    841   1.2   thorpej 				} else {
    842   1.2   thorpej 					if (pf2->ih_ipl > hiipl)
    843   1.2   thorpej 						hiipl = pf2->ih_ipl;
    844   1.2   thorpej 				}
    845   1.5      marc 				ihcnt++;
    846   1.2   thorpej 			}
    847   1.2   thorpej 		}
    848   1.2   thorpej 
    849   1.2   thorpej 		/*
    850  1.22  augustss 		 * If the ih being removed is lower priority than the lowest
    851   1.2   thorpej 		 * priority remaining interrupt, up the priority.
    852   1.2   thorpej 		 */
    853   1.2   thorpej 
    854  1.22  augustss 		/*
    855  1.22  augustss 		 * ihcnt is the number of interrupt handlers *not* including
    856  1.22  augustss 		 * the one about to be removed.
    857  1.22  augustss 		 */
    858   1.5      marc 
    859   1.2   thorpej 		if (ihcnt == 0) {
    860   1.2   thorpej #ifdef DIAGNOSTIC
    861   1.2   thorpej 			if (pf->sc->ih == NULL)
    862   1.2   thorpej 				panic("disestablishing last function, but card has no ih");
    863   1.2   thorpej #endif
    864   1.2   thorpej 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    865   1.2   thorpej 			    pf->sc->ih);
    866   1.5      marc 
    867   1.5      marc 			pf->ih_fct = NULL;
    868   1.5      marc 			pf->ih_arg = NULL;
    869   1.5      marc 
    870   1.2   thorpej 			pf->sc->ih = NULL;
    871   1.2   thorpej 		} else if (pf->ih_ipl > hiipl) {
    872   1.2   thorpej #ifdef DIAGNOSTIC
    873   1.2   thorpej 			if (pf->sc->ih == NULL)
    874   1.2   thorpej 				panic("changing ih ipl, but card has no ih");
    875   1.2   thorpej #endif
    876   1.5      marc 			/* XXX need #ifdef for splserial on x86 */
    877   1.5      marc 			s = splhigh();
    878   1.5      marc 
    879   1.2   thorpej 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    880   1.2   thorpej 			    pf->sc->ih);
    881   1.2   thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    882  1.12      marc 			    pf->sc->pch, pf, hiipl, PCMCIA_CARD_INTR, pf->sc);
    883   1.5      marc 
    884   1.5      marc 			/* null out the handler for this function */
    885   1.5      marc 
    886   1.5      marc 			pf->ih_fct = NULL;
    887   1.5      marc 			pf->ih_arg = NULL;
    888   1.5      marc 
    889   1.5      marc 			splx(s);
    890   1.5      marc 		} else {
    891   1.5      marc 			s = splhigh();
    892   1.5      marc 
    893   1.5      marc 			pf->ih_fct = NULL;
    894   1.5      marc 			pf->ih_arg = NULL;
    895   1.5      marc 
    896   1.5      marc 			splx(s);
    897   1.2   thorpej 		}
    898   1.2   thorpej 	} else {
    899   1.2   thorpej 		pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
    900   1.2   thorpej 	}
    901   1.2   thorpej }
    902   1.2   thorpej 
    903   1.2   thorpej int
    904   1.2   thorpej pcmcia_card_intr(arg)
    905   1.2   thorpej 	void *arg;
    906   1.2   thorpej {
    907   1.2   thorpej 	struct pcmcia_softc *sc = arg;
    908   1.2   thorpej 	struct pcmcia_function *pf;
    909  1.43   mycroft 	int reg, ret;
    910   1.2   thorpej 
    911   1.2   thorpej 	ret = 0;
    912   1.2   thorpej 
    913  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    914  1.43   mycroft 		if ((pf->pf_flags & PFF_ENABLED) == 0 ||
    915  1.43   mycroft 		    pf->ih_fct == NULL)
    916  1.43   mycroft 			continue;
    917  1.43   mycroft 		if (pf->ccr_mask & (1 << PCMCIA_CCR_STATUS)) {
    918  1.12      marc 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    919  1.12      marc 			if (reg & PCMCIA_CCR_STATUS_INTR) {
    920  1.43   mycroft 				ret |= (*pf->ih_fct)(pf->ih_arg);
    921  1.44   mycroft 				if (reg & PCMCIA_CCR_STATUS_INTRACK)
    922  1.44   mycroft 					pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
    923  1.44   mycroft 					    reg & ~PCMCIA_CCR_STATUS_INTR);
    924  1.12      marc 			}
    925  1.43   mycroft 		} else
    926  1.43   mycroft 			ret |= (*pf->ih_fct)(pf->ih_arg);
    927  1.12      marc 	}
    928  1.12      marc 
    929  1.12      marc 	return (ret);
    930  1.12      marc }
    931  1.12      marc 
    932  1.12      marc #ifdef PCMCIADEBUG
    933  1.12      marc int
    934  1.12      marc pcmcia_card_intrdebug(arg)
    935  1.12      marc 	void *arg;
    936  1.12      marc {
    937  1.12      marc 	struct pcmcia_softc *sc = arg;
    938  1.12      marc 	struct pcmcia_function *pf;
    939  1.43   mycroft 	int reg, ret;
    940  1.12      marc 
    941  1.12      marc 	ret = 0;
    942  1.12      marc 
    943  1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    944  1.12      marc 		printf("%s: intr flags=%x fct=%d cor=%02x csr=%02x pin=%02x",
    945   1.6  christos 		       sc->dev.dv_xname, pf->pf_flags, pf->number,
    946   1.6  christos 		       pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION),
    947   1.5      marc 		       pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS),
    948   1.5      marc 		       pcmcia_ccr_read(pf, PCMCIA_CCR_PIN));
    949  1.43   mycroft 		if ((pf->pf_flags & PFF_ENABLED) == 0 ||
    950  1.46   mycroft 		    pf->ih_fct == NULL) {
    951  1.46   mycroft 			printf("\n");
    952  1.43   mycroft 			continue;
    953  1.46   mycroft 		}
    954  1.43   mycroft 		if (pf->ccr_mask & (1 << PCMCIA_CCR_STATUS)) {
    955   1.2   thorpej 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    956   1.2   thorpej 			if (reg & PCMCIA_CCR_STATUS_INTR) {
    957  1.43   mycroft 				ret |= (*pf->ih_fct)(pf->ih_arg);
    958  1.44   mycroft 				if (reg & PCMCIA_CCR_STATUS_INTRACK) {
    959  1.44   mycroft 					printf("; csr %02x->%02x",
    960  1.44   mycroft 					    reg, reg & ~PCMCIA_CCR_STATUS_INTR);
    961  1.44   mycroft 					pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
    962  1.44   mycroft 					    reg & ~PCMCIA_CCR_STATUS_INTR);
    963  1.44   mycroft 				}
    964   1.2   thorpej 			}
    965  1.43   mycroft 		} else
    966  1.43   mycroft 			ret |= (*pf->ih_fct)(pf->ih_arg);
    967   1.2   thorpej 		printf("\n");
    968   1.2   thorpej 	}
    969   1.2   thorpej 
    970   1.2   thorpej 	return (ret);
    971   1.2   thorpej }
    972  1.12      marc #endif
    973