Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.14
      1  1.14      haya /*	$NetBSD: pcmcia.c,v 1.14 1999/10/15 06:07:31 haya Exp $	*/
      2   1.2   thorpej 
      3   1.2   thorpej #define	PCMCIADEBUG
      4   1.2   thorpej 
      5   1.2   thorpej /*
      6   1.2   thorpej  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      7   1.2   thorpej  *
      8   1.2   thorpej  * Redistribution and use in source and binary forms, with or without
      9   1.2   thorpej  * modification, are permitted provided that the following conditions
     10   1.2   thorpej  * are met:
     11   1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     12   1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     13   1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     15   1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     16   1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     17   1.2   thorpej  *    must display the following acknowledgement:
     18   1.2   thorpej  *	This product includes software developed by Marc Horowitz.
     19   1.2   thorpej  * 4. The name of the author may not be used to endorse or promote products
     20   1.2   thorpej  *    derived from this software without specific prior written permission.
     21   1.2   thorpej  *
     22   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.2   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.2   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.2   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.2   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.2   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.2   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.2   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.2   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.2   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.2   thorpej  */
     33   1.2   thorpej 
     34   1.2   thorpej #include "opt_pcmciaverbose.h"
     35   1.2   thorpej 
     36   1.2   thorpej #include <sys/types.h>
     37   1.2   thorpej #include <sys/param.h>
     38   1.2   thorpej #include <sys/systm.h>
     39   1.2   thorpej #include <sys/device.h>
     40   1.2   thorpej 
     41   1.2   thorpej 
     42   1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     43   1.2   thorpej #include <dev/pcmcia/pcmciachip.h>
     44   1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     45   1.2   thorpej 
     46   1.3     enami #include "locators.h"
     47   1.3     enami 
     48   1.2   thorpej #ifdef PCMCIADEBUG
     49   1.2   thorpej int	pcmcia_debug = 0;
     50   1.2   thorpej #define	DPRINTF(arg) if (pcmcia_debug) printf arg
     51  1.12      marc int	pcmciaintr_debug = 0;
     52  1.12      marc /* this is done this way to avoid doing lots of conditionals
     53  1.12      marc    at interrupt level.  */
     54  1.12      marc #define PCMCIA_CARD_INTR (pcmciaintr_debug?pcmcia_card_intrdebug:pcmcia_card_intr)
     55   1.2   thorpej #else
     56   1.2   thorpej #define	DPRINTF(arg)
     57  1.12      marc #define PCMCIA_CARD_INTR (pcmcia_card_intr)
     58   1.2   thorpej #endif
     59   1.2   thorpej 
     60   1.2   thorpej #ifdef PCMCIAVERBOSE
     61   1.2   thorpej int	pcmcia_verbose = 1;
     62   1.2   thorpej #else
     63   1.2   thorpej int	pcmcia_verbose = 0;
     64   1.2   thorpej #endif
     65   1.2   thorpej 
     66   1.2   thorpej int	pcmcia_match __P((struct device *, struct cfdata *, void *));
     67   1.2   thorpej int	pcmcia_submatch __P((struct device *, struct cfdata *, void *));
     68   1.2   thorpej void	pcmcia_attach __P((struct device *, struct device *, void *));
     69   1.2   thorpej int	pcmcia_print __P((void *, const char *));
     70   1.2   thorpej 
     71   1.5      marc static inline void pcmcia_socket_enable __P((pcmcia_chipset_tag_t,
     72   1.5      marc 					     pcmcia_chipset_handle_t *));
     73   1.5      marc static inline void pcmcia_socket_disable __P((pcmcia_chipset_tag_t,
     74   1.5      marc 					      pcmcia_chipset_handle_t *));
     75   1.5      marc 
     76   1.5      marc int pcmcia_card_intr __P((void *));
     77  1.12      marc #ifdef PCMCIADEBUG
     78  1.12      marc int pcmcia_card_intrdebug __P((void *));
     79  1.12      marc #endif
     80   1.2   thorpej 
     81   1.2   thorpej struct cfattach pcmcia_ca = {
     82   1.2   thorpej 	sizeof(struct pcmcia_softc), pcmcia_match, pcmcia_attach
     83   1.2   thorpej };
     84   1.2   thorpej 
     85   1.2   thorpej int
     86   1.2   thorpej pcmcia_ccr_read(pf, ccr)
     87   1.2   thorpej 	struct pcmcia_function *pf;
     88   1.2   thorpej 	int ccr;
     89   1.2   thorpej {
     90   1.2   thorpej 
     91   1.2   thorpej 	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
     92   1.2   thorpej 	    pf->pf_ccr_offset + ccr));
     93   1.2   thorpej }
     94   1.2   thorpej 
     95   1.2   thorpej void
     96   1.2   thorpej pcmcia_ccr_write(pf, ccr, val)
     97   1.2   thorpej 	struct pcmcia_function *pf;
     98   1.2   thorpej 	int ccr;
     99   1.2   thorpej 	int val;
    100   1.2   thorpej {
    101   1.2   thorpej 
    102   1.2   thorpej 	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
    103   1.2   thorpej 		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
    104   1.2   thorpej 		    pf->pf_ccr_offset + ccr, val);
    105   1.2   thorpej 	}
    106   1.2   thorpej }
    107   1.2   thorpej 
    108   1.2   thorpej int
    109   1.2   thorpej pcmcia_match(parent, match, aux)
    110   1.2   thorpej 	struct device *parent;
    111   1.2   thorpej 	struct cfdata *match;
    112   1.2   thorpej 	void *aux;
    113   1.2   thorpej {
    114  1.14      haya 	struct pcmciabus_attach_args *paa = aux;
    115  1.14      haya 
    116  1.14      haya 	if (strcmp(paa->paa_busname, match->cf_driver->cd_name)) {
    117  1.14      haya 	    return 0;
    118  1.14      haya 	}
    119   1.2   thorpej 	/* if the autoconfiguration got this far, there's a socket here */
    120   1.2   thorpej 	return (1);
    121   1.2   thorpej }
    122   1.2   thorpej 
    123   1.2   thorpej void
    124   1.2   thorpej pcmcia_attach(parent, self, aux)
    125   1.2   thorpej 	struct device *parent, *self;
    126   1.2   thorpej 	void *aux;
    127   1.2   thorpej {
    128   1.2   thorpej 	struct pcmciabus_attach_args *paa = aux;
    129   1.2   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
    130   1.2   thorpej 
    131   1.2   thorpej 	printf("\n");
    132   1.2   thorpej 
    133   1.2   thorpej 	sc->pct = paa->pct;
    134   1.2   thorpej 	sc->pch = paa->pch;
    135   1.2   thorpej 	sc->iobase = paa->iobase;
    136   1.2   thorpej 	sc->iosize = paa->iosize;
    137   1.2   thorpej 
    138   1.2   thorpej 	sc->ih = NULL;
    139   1.2   thorpej }
    140   1.2   thorpej 
    141   1.2   thorpej int
    142   1.2   thorpej pcmcia_card_attach(dev)
    143   1.2   thorpej 	struct device *dev;
    144   1.2   thorpej {
    145   1.2   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    146   1.2   thorpej 	struct pcmcia_function *pf;
    147   1.2   thorpej 	struct pcmcia_attach_args paa;
    148   1.2   thorpej 	int attached;
    149   1.2   thorpej 
    150   1.2   thorpej 	/*
    151   1.2   thorpej 	 * this is here so that when socket_enable calls gettype, trt happens
    152   1.2   thorpej 	 */
    153   1.2   thorpej 	sc->card.pf_head.sqh_first = NULL;
    154   1.2   thorpej 
    155   1.2   thorpej 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    156   1.2   thorpej 
    157   1.2   thorpej 	pcmcia_read_cis(sc);
    158   1.2   thorpej 
    159   1.2   thorpej 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    160  1.13      marc 
    161  1.13      marc 	pcmcia_check_cis_quirks(sc);
    162   1.2   thorpej 
    163   1.2   thorpej 	/*
    164   1.2   thorpej 	 * bail now if the card has no functions, or if there was an error in
    165   1.2   thorpej 	 * the cis.
    166   1.2   thorpej 	 */
    167   1.2   thorpej 
    168   1.2   thorpej 	if (sc->card.error)
    169   1.2   thorpej 		return (1);
    170   1.2   thorpej 	if (sc->card.pf_head.sqh_first == NULL)
    171   1.2   thorpej 		return (1);
    172   1.2   thorpej 
    173   1.2   thorpej 	if (pcmcia_verbose)
    174   1.2   thorpej 		pcmcia_print_cis(sc);
    175   1.2   thorpej 
    176   1.2   thorpej 	attached = 0;
    177   1.2   thorpej 
    178   1.2   thorpej 	for (pf = sc->card.pf_head.sqh_first; pf != NULL;
    179   1.2   thorpej 	    pf = pf->pf_list.sqe_next) {
    180   1.2   thorpej 		if (pf->cfe_head.sqh_first == NULL)
    181   1.2   thorpej 			continue;
    182   1.2   thorpej 
    183  1.11   thorpej #ifdef DIAGNOSTIC
    184  1.11   thorpej 		if (pf->child != NULL) {
    185  1.11   thorpej 			printf("%s: %s still attached to function %d!\n",
    186  1.11   thorpej 			    sc->dev.dv_xname, pf->child->dv_xname,
    187  1.11   thorpej 			    pf->number);
    188  1.11   thorpej 			panic("pcmcia_card_attach");
    189  1.11   thorpej 		}
    190  1.11   thorpej #endif
    191   1.2   thorpej 		pf->sc = sc;
    192  1.11   thorpej 		pf->child = NULL;
    193   1.2   thorpej 		pf->cfe = NULL;
    194   1.2   thorpej 		pf->ih_fct = NULL;
    195   1.2   thorpej 		pf->ih_arg = NULL;
    196   1.2   thorpej 	}
    197   1.2   thorpej 
    198   1.2   thorpej 	for (pf = sc->card.pf_head.sqh_first; pf != NULL;
    199   1.2   thorpej 	    pf = pf->pf_list.sqe_next) {
    200   1.2   thorpej 		if (pf->cfe_head.sqh_first == NULL)
    201   1.2   thorpej 			continue;
    202   1.2   thorpej 
    203   1.2   thorpej 		paa.manufacturer = sc->card.manufacturer;
    204   1.2   thorpej 		paa.product = sc->card.product;
    205   1.2   thorpej 		paa.card = &sc->card;
    206   1.2   thorpej 		paa.pf = pf;
    207   1.2   thorpej 
    208  1.11   thorpej 		if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
    209  1.11   thorpej 		    pcmcia_submatch)) != NULL) {
    210   1.2   thorpej 			attached++;
    211   1.2   thorpej 
    212   1.2   thorpej 			DPRINTF(("%s: function %d CCR at %d "
    213   1.2   thorpej 			     "offset %lx: %x %x %x %x, %x %x %x %x, %x\n",
    214   1.2   thorpej 			     sc->dev.dv_xname, pf->number,
    215   1.2   thorpej 			     pf->pf_ccr_window, pf->pf_ccr_offset,
    216   1.2   thorpej 			     pcmcia_ccr_read(pf, 0x00),
    217   1.2   thorpej 			pcmcia_ccr_read(pf, 0x02), pcmcia_ccr_read(pf, 0x04),
    218   1.2   thorpej 			pcmcia_ccr_read(pf, 0x06), pcmcia_ccr_read(pf, 0x0A),
    219   1.2   thorpej 			pcmcia_ccr_read(pf, 0x0C), pcmcia_ccr_read(pf, 0x0E),
    220   1.2   thorpej 			pcmcia_ccr_read(pf, 0x10), pcmcia_ccr_read(pf, 0x12)));
    221   1.2   thorpej 		}
    222   1.2   thorpej 	}
    223   1.2   thorpej 
    224   1.2   thorpej 	return (attached ? 0 : 1);
    225   1.2   thorpej }
    226   1.2   thorpej 
    227   1.2   thorpej void
    228  1.11   thorpej pcmcia_card_detach(dev, flags)
    229   1.2   thorpej 	struct device *dev;
    230  1.11   thorpej 	int flags;		/* DETACH_* flags */
    231   1.2   thorpej {
    232  1.10   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    233  1.11   thorpej 	struct pcmcia_function *pf;
    234  1.11   thorpej 	int error;
    235  1.10   thorpej 
    236  1.11   thorpej 	/*
    237  1.11   thorpej 	 * We are running on either the PCMCIA socket's event thread
    238  1.11   thorpej 	 * or in user context detaching a device by user request.
    239  1.10   thorpej 	 */
    240  1.11   thorpej 	for (pf = SIMPLEQ_FIRST(&sc->card.pf_head); pf != NULL;
    241  1.11   thorpej 	     pf = SIMPLEQ_NEXT(pf, pf_list)) {
    242  1.11   thorpej 		if (SIMPLEQ_FIRST(&pf->cfe_head) == NULL)
    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.11   thorpej 	for (pf = SIMPLEQ_FIRST(&sc->card.pf_head); pf != NULL;
    270  1.11   thorpej 	     pf = SIMPLEQ_NEXT(pf, pf_list)) {
    271  1.11   thorpej 		if (SIMPLEQ_FIRST(&pf->cfe_head) == NULL)
    272  1.11   thorpej 			continue;
    273  1.11   thorpej 		if (pf->child == NULL)
    274  1.11   thorpej 			continue;
    275  1.11   thorpej 		DPRINTF(("%s: deactivating %s (function %d)\n",
    276  1.11   thorpej 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    277  1.11   thorpej 		config_deactivate(pf->child);
    278  1.11   thorpej 	}
    279   1.2   thorpej }
    280   1.2   thorpej 
    281   1.2   thorpej int
    282   1.2   thorpej pcmcia_submatch(parent, cf, aux)
    283   1.2   thorpej 	struct device *parent;
    284   1.2   thorpej 	struct cfdata *cf;
    285   1.2   thorpej 	void *aux;
    286   1.2   thorpej {
    287   1.2   thorpej 	struct pcmcia_attach_args *paa = aux;
    288   1.2   thorpej 
    289   1.3     enami 	if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
    290   1.3     enami 	    cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
    291   1.2   thorpej 		return (0);
    292   1.2   thorpej 
    293   1.2   thorpej 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    294   1.2   thorpej }
    295   1.2   thorpej 
    296   1.2   thorpej int
    297   1.2   thorpej pcmcia_print(arg, pnp)
    298   1.2   thorpej 	void *arg;
    299   1.2   thorpej 	const char *pnp;
    300   1.2   thorpej {
    301   1.2   thorpej 	struct pcmcia_attach_args *pa = arg;
    302   1.2   thorpej 	struct pcmcia_softc *sc = pa->pf->sc;
    303   1.2   thorpej 	struct pcmcia_card *card = &sc->card;
    304   1.2   thorpej 	int i;
    305   1.2   thorpej 
    306   1.2   thorpej 	if (pnp) {
    307   1.2   thorpej 		for (i = 0; i < 4; i++) {
    308   1.2   thorpej 			if (card->cis1_info[i] == NULL)
    309   1.2   thorpej 				break;
    310   1.2   thorpej 			if (i)
    311   1.2   thorpej 				printf(", ");
    312   1.2   thorpej 			printf("%s", card->cis1_info[i]);
    313   1.2   thorpej 		}
    314   1.2   thorpej 		if (i)
    315   1.2   thorpej 			printf(" ");
    316   1.2   thorpej 		printf("(manufacturer 0x%x, product 0x%x)", card->manufacturer,
    317   1.2   thorpej 		       card->product);
    318   1.2   thorpej 	}
    319   1.2   thorpej 	printf(" function %d", pa->pf->number);
    320   1.2   thorpej 
    321   1.2   thorpej 	return (UNCONF);
    322   1.2   thorpej }
    323   1.2   thorpej 
    324   1.2   thorpej int
    325   1.2   thorpej pcmcia_card_gettype(dev)
    326   1.2   thorpej 	struct device  *dev;
    327   1.2   thorpej {
    328   1.7     enami 	struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
    329   1.7     enami 	struct pcmcia_function *pf;
    330   1.2   thorpej 
    331   1.2   thorpej 	/*
    332   1.2   thorpej 	 * set the iftype to memory if this card has no functions (not yet
    333   1.7     enami 	 * probed), or only one function, and that is not initialized yet or
    334   1.7     enami 	 * that is memory.
    335   1.2   thorpej 	 */
    336   1.7     enami 	pf = SIMPLEQ_FIRST(&sc->card.pf_head);
    337   1.7     enami 	if (pf == NULL ||
    338   1.7     enami 	    (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
    339   1.7     enami 	    (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
    340   1.2   thorpej 		return (PCMCIA_IFTYPE_MEMORY);
    341   1.2   thorpej 	else
    342   1.2   thorpej 		return (PCMCIA_IFTYPE_IO);
    343   1.2   thorpej }
    344   1.2   thorpej 
    345   1.2   thorpej /*
    346   1.2   thorpej  * Initialize a PCMCIA function.  May be called as long as the function is
    347   1.2   thorpej  * disabled.
    348   1.2   thorpej  */
    349   1.2   thorpej void
    350   1.2   thorpej pcmcia_function_init(pf, cfe)
    351   1.2   thorpej 	struct pcmcia_function *pf;
    352   1.2   thorpej 	struct pcmcia_config_entry *cfe;
    353   1.2   thorpej {
    354   1.2   thorpej 	if (pf->pf_flags & PFF_ENABLED)
    355   1.2   thorpej 		panic("pcmcia_function_init: function is enabled");
    356   1.2   thorpej 
    357   1.2   thorpej 	/* Remember which configuration entry we are using. */
    358   1.2   thorpej 	pf->cfe = cfe;
    359   1.2   thorpej }
    360   1.2   thorpej 
    361   1.5      marc static inline void pcmcia_socket_enable(pct, pch)
    362   1.5      marc      pcmcia_chipset_tag_t pct;
    363   1.5      marc      pcmcia_chipset_handle_t *pch;
    364   1.5      marc {
    365   1.5      marc 	pcmcia_chip_socket_enable(pct, pch);
    366   1.5      marc }
    367   1.5      marc 
    368   1.5      marc static inline void pcmcia_socket_disable(pct, pch)
    369   1.5      marc      pcmcia_chipset_tag_t pct;
    370   1.5      marc      pcmcia_chipset_handle_t *pch;
    371   1.5      marc {
    372   1.5      marc 	pcmcia_chip_socket_disable(pct, pch);
    373   1.5      marc }
    374   1.5      marc 
    375   1.2   thorpej /* Enable a PCMCIA function */
    376   1.2   thorpej int
    377   1.2   thorpej pcmcia_function_enable(pf)
    378   1.2   thorpej 	struct pcmcia_function *pf;
    379   1.2   thorpej {
    380   1.2   thorpej 	struct pcmcia_function *tmp;
    381   1.2   thorpej 	int reg;
    382   1.2   thorpej 
    383   1.2   thorpej 	if (pf->cfe == NULL)
    384   1.2   thorpej 		panic("pcmcia_function_enable: function not initialized");
    385   1.2   thorpej 
    386   1.5      marc 	/*
    387   1.5      marc 	 * Increase the reference count on the socket, enabling power, if
    388   1.5      marc 	 * necessary.
    389   1.5      marc 	 */
    390   1.5      marc 	if (pf->sc->sc_enabled_count++ == 0)
    391   1.5      marc 		pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
    392   1.5      marc 	DPRINTF(("%s: ++enabled_count = %d\n", pf->sc->dev.dv_xname,
    393   1.5      marc 		 pf->sc->sc_enabled_count));
    394   1.5      marc 
    395   1.2   thorpej 	if (pf->pf_flags & PFF_ENABLED) {
    396   1.2   thorpej 		/*
    397   1.2   thorpej 		 * Don't do anything if we're already enabled.
    398   1.2   thorpej 		 */
    399   1.2   thorpej 		return (0);
    400   1.2   thorpej 	}
    401   1.2   thorpej 
    402   1.2   thorpej 	/*
    403   1.2   thorpej 	 * it's possible for different functions' CCRs to be in the same
    404   1.2   thorpej 	 * underlying page.  Check for that.
    405   1.2   thorpej 	 */
    406   1.2   thorpej 
    407   1.2   thorpej 	for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
    408   1.2   thorpej 	    tmp = tmp->pf_list.sqe_next) {
    409   1.2   thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    410   1.2   thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    411   1.2   thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    412   1.2   thorpej 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    413   1.2   thorpej 		      tmp->pf_ccr_realsize))) {
    414   1.2   thorpej 			pf->pf_ccrt = tmp->pf_ccrt;
    415   1.2   thorpej 			pf->pf_ccrh = tmp->pf_ccrh;
    416   1.2   thorpej 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    417   1.2   thorpej 
    418   1.2   thorpej 			/*
    419   1.2   thorpej 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    420   1.2   thorpej 			 * tmp->ccr_base) + pf->ccr_base;
    421   1.2   thorpej 			 */
    422   1.2   thorpej 			pf->pf_ccr_offset =
    423   1.2   thorpej 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    424   1.2   thorpej 			    tmp->ccr_base;
    425   1.2   thorpej 			pf->pf_ccr_window = tmp->pf_ccr_window;
    426   1.2   thorpej 			break;
    427   1.2   thorpej 		}
    428   1.2   thorpej 	}
    429   1.2   thorpej 
    430   1.2   thorpej 	if (tmp == NULL) {
    431   1.2   thorpej 		if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
    432   1.2   thorpej 			goto bad;
    433   1.2   thorpej 
    434   1.2   thorpej 		if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    435   1.2   thorpej 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    436   1.2   thorpej 		    &pf->pf_ccr_window)) {
    437   1.2   thorpej 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    438   1.2   thorpej 			goto bad;
    439   1.2   thorpej 		}
    440   1.2   thorpej 	}
    441   1.2   thorpej 
    442   1.2   thorpej 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    443   1.2   thorpej 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    444   1.5      marc 	if (pcmcia_mfc(pf->sc)) {
    445   1.5      marc 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    446   1.5      marc 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    447   1.5      marc 		if (pf->ih_fct)
    448   1.5      marc 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    449   1.5      marc 
    450   1.5      marc 	}
    451   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    452   1.2   thorpej 
    453   1.2   thorpej 	reg = 0;
    454   1.2   thorpej 
    455   1.2   thorpej 	if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
    456   1.2   thorpej 		reg |= PCMCIA_CCR_STATUS_IOIS8;
    457   1.2   thorpej 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    458   1.2   thorpej 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    459   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    460   1.2   thorpej 
    461   1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    462   1.2   thorpej 
    463   1.5      marc 	if (pcmcia_mfc(pf->sc)) {
    464   1.5      marc 		long tmp, iosize;
    465   1.5      marc 
    466   1.5      marc 		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
    467   1.5      marc 		/* round up to nearest (2^n)-1 */
    468   1.5      marc 		for (iosize = 1; iosize < tmp; iosize <<= 1)
    469   1.5      marc 			;
    470   1.5      marc 		iosize--;
    471   1.5      marc 
    472   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    473   1.5      marc 				 pf->pf_mfc_iobase & 0xff);
    474   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    475   1.5      marc 				 (pf->pf_mfc_iobase >> 8) & 0xff);
    476   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
    477   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
    478   1.5      marc 
    479   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
    480   1.5      marc 	}
    481   1.5      marc 
    482  1.12      marc #ifdef PCMCIADEBUG
    483  1.12      marc 	if (pcmcia_debug) {
    484  1.12      marc 		for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
    485  1.12      marc 		     tmp = tmp->pf_list.sqe_next) {
    486  1.12      marc 			printf("%s: function %d CCR at %d offset %lx: "
    487  1.12      marc 			       "%x %x %x %x, %x %x %x %x, %x\n",
    488  1.12      marc 			       tmp->sc->dev.dv_xname, tmp->number,
    489  1.12      marc 			       tmp->pf_ccr_window, tmp->pf_ccr_offset,
    490  1.12      marc 			       pcmcia_ccr_read(tmp, 0x00),
    491  1.12      marc 			       pcmcia_ccr_read(tmp, 0x02),
    492  1.12      marc 			       pcmcia_ccr_read(tmp, 0x04),
    493  1.12      marc 			       pcmcia_ccr_read(tmp, 0x06),
    494  1.12      marc 
    495  1.12      marc 			       pcmcia_ccr_read(tmp, 0x0A),
    496  1.12      marc 			       pcmcia_ccr_read(tmp, 0x0C),
    497  1.12      marc 			       pcmcia_ccr_read(tmp, 0x0E),
    498  1.12      marc 			       pcmcia_ccr_read(tmp, 0x10),
    499   1.5      marc 
    500  1.12      marc 			       pcmcia_ccr_read(tmp, 0x12));
    501  1.12      marc 		}
    502  1.12      marc 	}
    503  1.12      marc #endif
    504   1.5      marc 
    505   1.2   thorpej 	pf->pf_flags |= PFF_ENABLED;
    506   1.2   thorpej 	return (0);
    507   1.2   thorpej 
    508   1.2   thorpej  bad:
    509   1.2   thorpej 	/*
    510   1.2   thorpej 	 * Decrement the reference count, and power down the socket, if
    511   1.2   thorpej 	 * necessary.
    512   1.2   thorpej 	 */
    513   1.5      marc 	if (--pf->sc->sc_enabled_count == 0)
    514   1.2   thorpej 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    515   1.5      marc 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    516   1.5      marc 		 pf->sc->sc_enabled_count));
    517   1.5      marc 
    518   1.2   thorpej 	return (1);
    519   1.2   thorpej }
    520   1.2   thorpej 
    521   1.2   thorpej /* Disable PCMCIA function. */
    522   1.2   thorpej void
    523   1.2   thorpej pcmcia_function_disable(pf)
    524   1.2   thorpej 	struct pcmcia_function *pf;
    525   1.2   thorpej {
    526   1.2   thorpej 	struct pcmcia_function *tmp;
    527   1.2   thorpej 
    528   1.2   thorpej 	if (pf->cfe == NULL)
    529   1.2   thorpej 		panic("pcmcia_function_enable: function not initialized");
    530   1.2   thorpej 
    531   1.2   thorpej 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    532   1.2   thorpej 		/*
    533   1.2   thorpej 		 * Don't do anything if we're already disabled.
    534   1.2   thorpej 		 */
    535   1.2   thorpej 		return;
    536   1.2   thorpej 	}
    537   1.2   thorpej 
    538   1.2   thorpej 	/*
    539   1.2   thorpej 	 * it's possible for different functions' CCRs to be in the same
    540   1.2   thorpej 	 * underlying page.  Check for that.  Note we mark us as disabled
    541   1.2   thorpej 	 * first to avoid matching ourself.
    542   1.2   thorpej 	 */
    543   1.2   thorpej 
    544   1.2   thorpej 	pf->pf_flags &= ~PFF_ENABLED;
    545   1.2   thorpej 	for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
    546   1.2   thorpej 	    tmp = tmp->pf_list.sqe_next) {
    547   1.2   thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    548   1.2   thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    549   1.2   thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    550   1.2   thorpej 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    551   1.2   thorpej 			break;
    552   1.2   thorpej 	}
    553   1.2   thorpej 
    554   1.2   thorpej 	/* Not used by anyone else; unmap the CCR. */
    555   1.2   thorpej 	if (tmp == NULL) {
    556   1.2   thorpej 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    557   1.2   thorpej 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    558   1.2   thorpej 	}
    559   1.2   thorpej 
    560   1.2   thorpej 	/*
    561   1.2   thorpej 	 * Decrement the reference count, and power down the socket, if
    562   1.2   thorpej 	 * necessary.
    563   1.2   thorpej 	 */
    564   1.2   thorpej 	if (--pf->sc->sc_enabled_count == 0)
    565   1.2   thorpej 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    566   1.5      marc 	DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
    567   1.5      marc 		 pf->sc->sc_enabled_count));
    568   1.2   thorpej }
    569   1.2   thorpej 
    570   1.2   thorpej int
    571   1.2   thorpej pcmcia_io_map(pf, width, offset, size, pcihp, windowp)
    572   1.2   thorpej 	struct pcmcia_function *pf;
    573   1.2   thorpej 	int width;
    574   1.2   thorpej 	bus_addr_t offset;
    575   1.2   thorpej 	bus_size_t size;
    576   1.2   thorpej 	struct pcmcia_io_handle *pcihp;
    577   1.2   thorpej 	int *windowp;
    578   1.2   thorpej {
    579   1.2   thorpej 	int reg;
    580   1.2   thorpej 
    581   1.2   thorpej 	if (pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
    582   1.2   thorpej 	    width, offset, size, pcihp, windowp))
    583   1.2   thorpej 		return (1);
    584   1.2   thorpej 
    585   1.2   thorpej 	/*
    586   1.2   thorpej 	 * XXX in the multifunction multi-iospace-per-function case, this
    587   1.2   thorpej 	 * needs to cooperate with io_alloc to make sure that the spaces
    588   1.2   thorpej 	 * don't overlap, and that the ccr's are set correctly
    589   1.2   thorpej 	 */
    590   1.2   thorpej 
    591   1.2   thorpej 	if (pcmcia_mfc(pf->sc)) {
    592   1.5      marc 		long tmp, iosize;
    593   1.5      marc 
    594   1.5      marc 		if (pf->pf_mfc_iomax == 0) {
    595   1.5      marc 			pf->pf_mfc_iobase = pcihp->addr + offset;
    596   1.5      marc 			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
    597   1.5      marc 		} else {
    598   1.5      marc 			/* this makes the assumption that nothing overlaps */
    599   1.5      marc 			if (pf->pf_mfc_iobase > pcihp->addr + offset)
    600   1.5      marc 				pf->pf_mfc_iobase = pcihp->addr + offset;
    601   1.5      marc 			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
    602   1.5      marc 				pf->pf_mfc_iomax = pcihp->addr + offset + size;
    603   1.5      marc 		}
    604   1.5      marc 
    605   1.5      marc 		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
    606   1.5      marc 		/* round up to nearest (2^n)-1 */
    607   1.5      marc 		for (iosize = 1; iosize >= tmp; iosize <<= 1)
    608   1.5      marc 			;
    609   1.5      marc 		iosize--;
    610   1.5      marc 
    611   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    612   1.5      marc 				 pf->pf_mfc_iobase & 0xff);
    613   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    614   1.5      marc 				 (pf->pf_mfc_iobase >> 8) & 0xff);
    615   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
    616   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
    617   1.5      marc 
    618   1.5      marc 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
    619   1.2   thorpej 
    620   1.2   thorpej 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    621   1.2   thorpej 		reg |= PCMCIA_CCR_OPTION_ADDR_DECODE;
    622   1.2   thorpej 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    623   1.2   thorpej 	}
    624   1.2   thorpej 	return (0);
    625  1.11   thorpej }
    626  1.11   thorpej 
    627  1.11   thorpej void
    628  1.11   thorpej pcmcia_io_unmap(pf, window)
    629  1.11   thorpej 	struct pcmcia_function *pf;
    630  1.11   thorpej 	int window;
    631  1.11   thorpej {
    632  1.11   thorpej 
    633  1.11   thorpej 	pcmcia_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
    634  1.11   thorpej 
    635  1.11   thorpej 	/* XXX Anything for multi-function cards? */
    636   1.2   thorpej }
    637   1.2   thorpej 
    638   1.2   thorpej void *
    639   1.2   thorpej pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
    640   1.2   thorpej 	struct pcmcia_function *pf;
    641   1.2   thorpej 	int ipl;
    642   1.2   thorpej 	int (*ih_fct) __P((void *));
    643   1.2   thorpej 	void *ih_arg;
    644   1.2   thorpej {
    645   1.2   thorpej 	void *ret;
    646   1.2   thorpej 
    647   1.2   thorpej 	/* behave differently if this is a multifunction card */
    648   1.2   thorpej 
    649   1.2   thorpej 	if (pcmcia_mfc(pf->sc)) {
    650   1.2   thorpej 		int s, ihcnt, hiipl, reg;
    651   1.2   thorpej 		struct pcmcia_function *pf2;
    652   1.2   thorpej 
    653   1.2   thorpej 		/*
    654   1.2   thorpej 		 * mask all the ipl's which are already used by this card,
    655   1.2   thorpej 		 * and find the highest ipl number (lowest priority)
    656   1.2   thorpej 		 */
    657   1.2   thorpej 
    658   1.2   thorpej 		ihcnt = 0;
    659   1.5      marc 		s = 0;		/* this is only here to keep the compiler
    660   1.2   thorpej 				   happy */
    661   1.5      marc 		hiipl = 0;	/* this is only here to keep the compiler
    662   1.2   thorpej 				   happy */
    663   1.2   thorpej 
    664   1.2   thorpej 		for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
    665   1.2   thorpej 		     pf2 = pf2->pf_list.sqe_next) {
    666   1.2   thorpej 			if (pf2->ih_fct) {
    667   1.5      marc 				DPRINTF(("%s: function %d has ih_fct %p\n",
    668   1.5      marc 					 pf->sc->dev.dv_xname, pf2->number,
    669   1.5      marc 					 pf2->ih_fct));
    670   1.5      marc 
    671   1.2   thorpej 				if (ihcnt == 0) {
    672   1.2   thorpej 					hiipl = pf2->ih_ipl;
    673   1.2   thorpej 				} else {
    674   1.2   thorpej 					if (pf2->ih_ipl > hiipl)
    675   1.2   thorpej 						hiipl = pf2->ih_ipl;
    676   1.2   thorpej 				}
    677   1.5      marc 
    678   1.5      marc 				ihcnt++;
    679   1.2   thorpej 			}
    680   1.2   thorpej 		}
    681   1.2   thorpej 
    682   1.2   thorpej 		/*
    683   1.2   thorpej 		 * establish the real interrupt, changing the ipl if
    684   1.2   thorpej 		 * necessary
    685   1.2   thorpej 		 */
    686   1.2   thorpej 
    687   1.2   thorpej 		if (ihcnt == 0) {
    688   1.2   thorpej #ifdef DIAGNOSTIC
    689   1.2   thorpej 			if (pf->sc->ih != NULL)
    690   1.2   thorpej 				panic("card has intr handler, but no function does");
    691   1.2   thorpej #endif
    692   1.6  christos 			s = splhigh();
    693   1.6  christos 
    694   1.5      marc 			/* set up the handler for the new function */
    695   1.5      marc 
    696   1.5      marc 			pf->ih_fct = ih_fct;
    697   1.5      marc 			pf->ih_arg = ih_arg;
    698   1.5      marc 			pf->ih_ipl = ipl;
    699   1.2   thorpej 
    700   1.2   thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    701  1.12      marc 			    pf->sc->pch, pf, ipl, PCMCIA_CARD_INTR, pf->sc);
    702   1.6  christos 			splx(s);
    703   1.2   thorpej 		} else if (ipl > hiipl) {
    704   1.2   thorpej #ifdef DIAGNOSTIC
    705   1.2   thorpej 			if (pf->sc->ih == NULL)
    706   1.2   thorpej 				panic("functions have ih, but the card does not");
    707   1.2   thorpej #endif
    708   1.2   thorpej 
    709   1.5      marc 			/* XXX need #ifdef for splserial on x86 */
    710   1.5      marc 			s = splhigh();
    711   1.5      marc 
    712   1.6  christos 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    713   1.6  christos 						      pf->sc->ih);
    714   1.6  christos 
    715   1.5      marc 			/* set up the handler for the new function */
    716   1.5      marc 			pf->ih_fct = ih_fct;
    717   1.5      marc 			pf->ih_arg = ih_arg;
    718   1.5      marc 			pf->ih_ipl = ipl;
    719   1.5      marc 
    720   1.2   thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    721  1.12      marc 			    pf->sc->pch, pf, ipl, PCMCIA_CARD_INTR, pf->sc);
    722   1.5      marc 
    723   1.5      marc 			splx(s);
    724   1.5      marc 		} else {
    725   1.5      marc 			s = splhigh();
    726   1.5      marc 
    727   1.5      marc 			/* set up the handler for the new function */
    728   1.5      marc 
    729   1.5      marc 			pf->ih_fct = ih_fct;
    730   1.5      marc 			pf->ih_arg = ih_arg;
    731   1.5      marc 			pf->ih_ipl = ipl;
    732   1.5      marc 
    733   1.5      marc 			splx(s);
    734   1.2   thorpej 		}
    735   1.2   thorpej 
    736   1.2   thorpej 		ret = pf->sc->ih;
    737   1.2   thorpej 
    738   1.2   thorpej 		if (ret != NULL) {
    739   1.2   thorpej 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    740   1.2   thorpej 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    741   1.2   thorpej 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    742   1.2   thorpej 
    743   1.2   thorpej 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    744   1.2   thorpej 			reg |= PCMCIA_CCR_STATUS_INTRACK;
    745   1.2   thorpej 			pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    746   1.2   thorpej 		}
    747   1.2   thorpej 	} else {
    748   1.2   thorpej 		ret = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
    749   1.2   thorpej 		    pf, ipl, ih_fct, ih_arg);
    750   1.2   thorpej 	}
    751   1.2   thorpej 
    752   1.2   thorpej 	return (ret);
    753   1.2   thorpej }
    754   1.2   thorpej 
    755   1.2   thorpej void
    756   1.2   thorpej pcmcia_intr_disestablish(pf, ih)
    757   1.2   thorpej 	struct pcmcia_function *pf;
    758   1.2   thorpej 	void *ih;
    759   1.2   thorpej {
    760   1.2   thorpej 	/* behave differently if this is a multifunction card */
    761   1.2   thorpej 
    762   1.2   thorpej 	if (pcmcia_mfc(pf->sc)) {
    763   1.2   thorpej 		int s, ihcnt, hiipl;
    764   1.2   thorpej 		struct pcmcia_function *pf2;
    765   1.2   thorpej 
    766   1.2   thorpej 		/*
    767   1.2   thorpej 		 * mask all the ipl's which are already used by this card,
    768   1.2   thorpej 		 * and find the highest ipl number (lowest priority).  Skip
    769   1.2   thorpej 		 * the current function.
    770   1.2   thorpej 		 */
    771   1.2   thorpej 
    772   1.2   thorpej 		ihcnt = 0;
    773   1.2   thorpej 		s = 0;		/* this is only here to keep the compipler
    774   1.2   thorpej 				   happy */
    775   1.2   thorpej 		hiipl = 0;	/* this is only here to keep the compipler
    776   1.2   thorpej 				   happy */
    777   1.2   thorpej 
    778   1.2   thorpej 		for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
    779   1.2   thorpej 		     pf2 = pf2->pf_list.sqe_next) {
    780   1.2   thorpej 			if (pf2 == pf)
    781   1.2   thorpej 				continue;
    782   1.2   thorpej 
    783   1.2   thorpej 			if (pf2->ih_fct) {
    784   1.2   thorpej 				if (ihcnt == 0) {
    785   1.2   thorpej 					hiipl = pf2->ih_ipl;
    786   1.2   thorpej 				} else {
    787   1.2   thorpej 					if (pf2->ih_ipl > hiipl)
    788   1.2   thorpej 						hiipl = pf2->ih_ipl;
    789   1.2   thorpej 				}
    790   1.5      marc 				ihcnt++;
    791   1.2   thorpej 			}
    792   1.2   thorpej 		}
    793   1.2   thorpej 
    794   1.2   thorpej 		/*
    795   1.2   thorpej 		 * if the ih being removed is lower priority than the lowest
    796   1.2   thorpej 		 * priority remaining interrupt, up the priority.
    797   1.2   thorpej 		 */
    798   1.2   thorpej 
    799   1.5      marc 		/* ihcnt is the number of interrupt handlers *not* including
    800   1.5      marc 		   the one about to be removed. */
    801   1.5      marc 
    802   1.2   thorpej 		if (ihcnt == 0) {
    803   1.5      marc 			int reg;
    804   1.5      marc 
    805   1.2   thorpej #ifdef DIAGNOSTIC
    806   1.2   thorpej 			if (pf->sc->ih == NULL)
    807   1.2   thorpej 				panic("disestablishing last function, but card has no ih");
    808   1.2   thorpej #endif
    809   1.2   thorpej 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    810   1.2   thorpej 			    pf->sc->ih);
    811   1.5      marc 
    812   1.5      marc 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    813   1.5      marc 			reg &= ~PCMCIA_CCR_OPTION_IREQ_ENABLE;
    814   1.5      marc 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    815   1.5      marc 
    816   1.5      marc 			pf->ih_fct = NULL;
    817   1.5      marc 			pf->ih_arg = NULL;
    818   1.5      marc 
    819   1.2   thorpej 			pf->sc->ih = NULL;
    820   1.2   thorpej 		} else if (pf->ih_ipl > hiipl) {
    821   1.2   thorpej #ifdef DIAGNOSTIC
    822   1.2   thorpej 			if (pf->sc->ih == NULL)
    823   1.2   thorpej 				panic("changing ih ipl, but card has no ih");
    824   1.2   thorpej #endif
    825   1.5      marc 			/* XXX need #ifdef for splserial on x86 */
    826   1.5      marc 			s = splhigh();
    827   1.5      marc 
    828   1.2   thorpej 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    829   1.2   thorpej 			    pf->sc->ih);
    830   1.2   thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    831  1.12      marc 			    pf->sc->pch, pf, hiipl, PCMCIA_CARD_INTR, pf->sc);
    832   1.5      marc 
    833   1.5      marc 			/* null out the handler for this function */
    834   1.5      marc 
    835   1.5      marc 			pf->ih_fct = NULL;
    836   1.5      marc 			pf->ih_arg = NULL;
    837   1.5      marc 
    838   1.5      marc 			splx(s);
    839   1.5      marc 		} else {
    840   1.5      marc 			s = splhigh();
    841   1.5      marc 
    842   1.5      marc 			pf->ih_fct = NULL;
    843   1.5      marc 			pf->ih_arg = NULL;
    844   1.5      marc 
    845   1.5      marc 			splx(s);
    846   1.2   thorpej 		}
    847   1.2   thorpej 	} else {
    848   1.2   thorpej 		pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
    849   1.2   thorpej 	}
    850   1.2   thorpej }
    851   1.2   thorpej 
    852   1.2   thorpej int
    853   1.2   thorpej pcmcia_card_intr(arg)
    854   1.2   thorpej 	void *arg;
    855   1.2   thorpej {
    856   1.2   thorpej 	struct pcmcia_softc *sc = arg;
    857   1.2   thorpej 	struct pcmcia_function *pf;
    858   1.2   thorpej 	int reg, ret, ret2;
    859   1.2   thorpej 
    860   1.2   thorpej 	ret = 0;
    861   1.2   thorpej 
    862   1.2   thorpej 	for (pf = sc->card.pf_head.sqh_first; pf != NULL;
    863   1.2   thorpej 	    pf = pf->pf_list.sqe_next) {
    864  1.12      marc 		if (pf->ih_fct != NULL &&
    865  1.12      marc 		    (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
    866  1.12      marc 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    867  1.12      marc 			if (reg & PCMCIA_CCR_STATUS_INTR) {
    868  1.12      marc 				ret2 = (*pf->ih_fct)(pf->ih_arg);
    869  1.12      marc 				if (ret2 != 0 && ret == 0)
    870  1.12      marc 					ret = ret2;
    871  1.12      marc 				reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    872  1.12      marc 				pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
    873  1.12      marc 				    reg & ~PCMCIA_CCR_STATUS_INTR);
    874  1.12      marc 			}
    875  1.12      marc 		}
    876  1.12      marc 	}
    877  1.12      marc 
    878  1.12      marc 	return (ret);
    879  1.12      marc }
    880  1.12      marc 
    881  1.12      marc #ifdef PCMCIADEBUG
    882  1.12      marc int
    883  1.12      marc pcmcia_card_intrdebug(arg)
    884  1.12      marc 	void *arg;
    885  1.12      marc {
    886  1.12      marc 	struct pcmcia_softc *sc = arg;
    887  1.12      marc 	struct pcmcia_function *pf;
    888  1.12      marc 	int reg, ret, ret2;
    889  1.12      marc 
    890  1.12      marc 	ret = 0;
    891  1.12      marc 
    892  1.12      marc 	for (pf = sc->card.pf_head.sqh_first; pf != NULL;
    893  1.12      marc 	    pf = pf->pf_list.sqe_next) {
    894  1.12      marc 		printf("%s: intr flags=%x fct=%d cor=%02x csr=%02x pin=%02x",
    895   1.6  christos 		       sc->dev.dv_xname, pf->pf_flags, pf->number,
    896   1.6  christos 		       pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION),
    897   1.5      marc 		       pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS),
    898   1.5      marc 		       pcmcia_ccr_read(pf, PCMCIA_CCR_PIN));
    899   1.2   thorpej 		if (pf->ih_fct != NULL &&
    900   1.2   thorpej 		    (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
    901   1.2   thorpej 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    902   1.2   thorpej 			if (reg & PCMCIA_CCR_STATUS_INTR) {
    903   1.2   thorpej 				ret2 = (*pf->ih_fct)(pf->ih_arg);
    904   1.2   thorpej 				if (ret2 != 0 && ret == 0)
    905   1.2   thorpej 					ret = ret2;
    906   1.2   thorpej 				reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    907   1.2   thorpej 				printf("; csr %02x->%02x",
    908   1.2   thorpej 				    reg, reg & ~PCMCIA_CCR_STATUS_INTR);
    909   1.2   thorpej 				pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
    910   1.2   thorpej 				    reg & ~PCMCIA_CCR_STATUS_INTR);
    911   1.2   thorpej 			}
    912   1.2   thorpej 		}
    913   1.2   thorpej 		printf("\n");
    914   1.2   thorpej 	}
    915   1.2   thorpej 
    916   1.2   thorpej 	return (ret);
    917   1.2   thorpej }
    918  1.12      marc #endif
    919