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