Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: pcmcia.c,v 1.2 1997/10/16 23:27:30 thorpej 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 /* XXX only needed for intr debugging */
     42  1.2  thorpej #include <vm/vm.h>
     43  1.2  thorpej 
     44  1.2  thorpej #include <dev/pcmcia/pcmciareg.h>
     45  1.2  thorpej #include <dev/pcmcia/pcmciachip.h>
     46  1.2  thorpej #include <dev/pcmcia/pcmciavar.h>
     47  1.2  thorpej 
     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.2  thorpej #else
     52  1.2  thorpej #define	DPRINTF(arg)
     53  1.2  thorpej #endif
     54  1.2  thorpej 
     55  1.2  thorpej #ifdef PCMCIAVERBOSE
     56  1.2  thorpej int	pcmcia_verbose = 1;
     57  1.2  thorpej #else
     58  1.2  thorpej int	pcmcia_verbose = 0;
     59  1.2  thorpej #endif
     60  1.2  thorpej 
     61  1.2  thorpej #ifdef	__BROKEN_INDIRECT_CONFIG
     62  1.2  thorpej int	pcmcia_match __P((struct device *, void *, void *));
     63  1.2  thorpej int	pcmcia_submatch __P((struct device *, void *, void *));
     64  1.2  thorpej #else
     65  1.2  thorpej int	pcmcia_match __P((struct device *, struct cfdata *, void *));
     66  1.2  thorpej int	pcmcia_submatch __P((struct device *, struct cfdata *, void *));
     67  1.2  thorpej #endif
     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.2  thorpej int	pcmcia_card_intr __P((void *));
     72  1.2  thorpej 
     73  1.2  thorpej struct cfdriver pcmcia_cd = {
     74  1.2  thorpej 	NULL, "pcmcia", DV_DULL
     75  1.2  thorpej };
     76  1.2  thorpej 
     77  1.2  thorpej struct cfattach pcmcia_ca = {
     78  1.2  thorpej 	sizeof(struct pcmcia_softc), pcmcia_match, pcmcia_attach
     79  1.2  thorpej };
     80  1.2  thorpej 
     81  1.2  thorpej int
     82  1.2  thorpej pcmcia_ccr_read(pf, ccr)
     83  1.2  thorpej 	struct pcmcia_function *pf;
     84  1.2  thorpej 	int ccr;
     85  1.2  thorpej {
     86  1.2  thorpej 
     87  1.2  thorpej 	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
     88  1.2  thorpej 	    pf->pf_ccr_offset + ccr));
     89  1.2  thorpej }
     90  1.2  thorpej 
     91  1.2  thorpej void
     92  1.2  thorpej pcmcia_ccr_write(pf, ccr, val)
     93  1.2  thorpej 	struct pcmcia_function *pf;
     94  1.2  thorpej 	int ccr;
     95  1.2  thorpej 	int val;
     96  1.2  thorpej {
     97  1.2  thorpej 
     98  1.2  thorpej 	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
     99  1.2  thorpej 		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
    100  1.2  thorpej 		    pf->pf_ccr_offset + ccr, val);
    101  1.2  thorpej 	}
    102  1.2  thorpej }
    103  1.2  thorpej 
    104  1.2  thorpej int
    105  1.2  thorpej pcmcia_match(parent, match, aux)
    106  1.2  thorpej 	struct device *parent;
    107  1.2  thorpej #ifdef	__BROKEN_INDIRECT_CONFIG
    108  1.2  thorpej 	void *match;
    109  1.2  thorpej #else
    110  1.2  thorpej 	struct cfdata *match;
    111  1.2  thorpej #endif
    112  1.2  thorpej 	void *aux;
    113  1.2  thorpej {
    114  1.2  thorpej 
    115  1.2  thorpej 	/* if the autoconfiguration got this far, there's a socket here */
    116  1.2  thorpej 	return (1);
    117  1.2  thorpej }
    118  1.2  thorpej 
    119  1.2  thorpej void
    120  1.2  thorpej pcmcia_attach(parent, self, aux)
    121  1.2  thorpej 	struct device *parent, *self;
    122  1.2  thorpej 	void *aux;
    123  1.2  thorpej {
    124  1.2  thorpej 	struct pcmciabus_attach_args *paa = aux;
    125  1.2  thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
    126  1.2  thorpej 
    127  1.2  thorpej 	printf("\n");
    128  1.2  thorpej 
    129  1.2  thorpej 	sc->pct = paa->pct;
    130  1.2  thorpej 	sc->pch = paa->pch;
    131  1.2  thorpej 	sc->iobase = paa->iobase;
    132  1.2  thorpej 	sc->iosize = paa->iosize;
    133  1.2  thorpej 
    134  1.2  thorpej 	sc->ih = NULL;
    135  1.2  thorpej }
    136  1.2  thorpej 
    137  1.2  thorpej int
    138  1.2  thorpej pcmcia_card_attach(dev)
    139  1.2  thorpej 	struct device *dev;
    140  1.2  thorpej {
    141  1.2  thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    142  1.2  thorpej 	struct pcmcia_function *pf;
    143  1.2  thorpej 	struct pcmcia_attach_args paa;
    144  1.2  thorpej 	int attached;
    145  1.2  thorpej 
    146  1.2  thorpej 	/*
    147  1.2  thorpej 	 * this is here so that when socket_enable calls gettype, trt happens
    148  1.2  thorpej 	 */
    149  1.2  thorpej 	sc->card.pf_head.sqh_first = NULL;
    150  1.2  thorpej 
    151  1.2  thorpej 	pcmcia_chip_socket_enable(sc->pct, sc->pch);
    152  1.2  thorpej 
    153  1.2  thorpej 	pcmcia_read_cis(sc);
    154  1.2  thorpej 
    155  1.2  thorpej 	pcmcia_chip_socket_disable(sc->pct, sc->pch);
    156  1.2  thorpej 
    157  1.2  thorpej 	/*
    158  1.2  thorpej 	 * bail now if the card has no functions, or if there was an error in
    159  1.2  thorpej 	 * the cis.
    160  1.2  thorpej 	 */
    161  1.2  thorpej 
    162  1.2  thorpej 	if (sc->card.error)
    163  1.2  thorpej 		return (1);
    164  1.2  thorpej 	if (sc->card.pf_head.sqh_first == NULL)
    165  1.2  thorpej 		return (1);
    166  1.2  thorpej 
    167  1.2  thorpej 	if (pcmcia_verbose)
    168  1.2  thorpej 		pcmcia_print_cis(sc);
    169  1.2  thorpej 
    170  1.2  thorpej 	attached = 0;
    171  1.2  thorpej 
    172  1.2  thorpej 	for (pf = sc->card.pf_head.sqh_first; pf != NULL;
    173  1.2  thorpej 	    pf = pf->pf_list.sqe_next) {
    174  1.2  thorpej 		if (pf->cfe_head.sqh_first == NULL)
    175  1.2  thorpej 			continue;
    176  1.2  thorpej 
    177  1.2  thorpej 		pf->sc = sc;
    178  1.2  thorpej 		pf->cfe = NULL;
    179  1.2  thorpej 		pf->ih_fct = NULL;
    180  1.2  thorpej 		pf->ih_arg = NULL;
    181  1.2  thorpej 	}
    182  1.2  thorpej 
    183  1.2  thorpej 	for (pf = sc->card.pf_head.sqh_first; pf != NULL;
    184  1.2  thorpej 	    pf = pf->pf_list.sqe_next) {
    185  1.2  thorpej 		if (pf->cfe_head.sqh_first == NULL)
    186  1.2  thorpej 			continue;
    187  1.2  thorpej 
    188  1.2  thorpej 		paa.manufacturer = sc->card.manufacturer;
    189  1.2  thorpej 		paa.product = sc->card.product;
    190  1.2  thorpej 		paa.card = &sc->card;
    191  1.2  thorpej 		paa.pf = pf;
    192  1.2  thorpej 
    193  1.2  thorpej 		if (config_found_sm(&sc->dev, &paa, pcmcia_print,
    194  1.2  thorpej 		    pcmcia_submatch)) {
    195  1.2  thorpej 			attached++;
    196  1.2  thorpej 
    197  1.2  thorpej 			DPRINTF(("%s: function %d CCR at %d "
    198  1.2  thorpej 			     "offset %lx: %x %x %x %x, %x %x %x %x, %x\n",
    199  1.2  thorpej 			     sc->dev.dv_xname, pf->number,
    200  1.2  thorpej 			     pf->pf_ccr_window, pf->pf_ccr_offset,
    201  1.2  thorpej 			     pcmcia_ccr_read(pf, 0x00),
    202  1.2  thorpej 			pcmcia_ccr_read(pf, 0x02), pcmcia_ccr_read(pf, 0x04),
    203  1.2  thorpej 			pcmcia_ccr_read(pf, 0x06), pcmcia_ccr_read(pf, 0x0A),
    204  1.2  thorpej 			pcmcia_ccr_read(pf, 0x0C), pcmcia_ccr_read(pf, 0x0E),
    205  1.2  thorpej 			pcmcia_ccr_read(pf, 0x10), pcmcia_ccr_read(pf, 0x12)));
    206  1.2  thorpej 		}
    207  1.2  thorpej 	}
    208  1.2  thorpej 
    209  1.2  thorpej 	return (attached ? 0 : 1);
    210  1.2  thorpej }
    211  1.2  thorpej 
    212  1.2  thorpej void
    213  1.2  thorpej pcmcia_card_detach(dev)
    214  1.2  thorpej 	struct device *dev;
    215  1.2  thorpej {
    216  1.2  thorpej 	/* struct pcmcia_softc *sc = (struct pcmcia_softc *) dev; */
    217  1.2  thorpej 	/* don't do anything yet */
    218  1.2  thorpej }
    219  1.2  thorpej 
    220  1.2  thorpej int
    221  1.2  thorpej #ifdef __BROKEN_INDIRECT_CONFIG
    222  1.2  thorpej pcmcia_submatch(parent, match, aux)
    223  1.2  thorpej 	struct device *parent;
    224  1.2  thorpej 	void *match, *aux;
    225  1.2  thorpej {
    226  1.2  thorpej 	struct cfdata *cf = match;
    227  1.2  thorpej #else
    228  1.2  thorpej pcmcia_submatch(parent, cf, aux)
    229  1.2  thorpej 	struct device *parent;
    230  1.2  thorpej 	struct cfdata *cf;
    231  1.2  thorpej 	void *aux;
    232  1.2  thorpej {
    233  1.2  thorpej #endif
    234  1.2  thorpej 	struct pcmcia_attach_args *paa = aux;
    235  1.2  thorpej 
    236  1.2  thorpej 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != paa->pf->number)
    237  1.2  thorpej 		return (0);
    238  1.2  thorpej 
    239  1.2  thorpej 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    240  1.2  thorpej }
    241  1.2  thorpej 
    242  1.2  thorpej int
    243  1.2  thorpej pcmcia_print(arg, pnp)
    244  1.2  thorpej 	void *arg;
    245  1.2  thorpej 	const char *pnp;
    246  1.2  thorpej {
    247  1.2  thorpej 	struct pcmcia_attach_args *pa = arg;
    248  1.2  thorpej 	struct pcmcia_softc *sc = pa->pf->sc;
    249  1.2  thorpej 	struct pcmcia_card *card = &sc->card;
    250  1.2  thorpej 	int i;
    251  1.2  thorpej 
    252  1.2  thorpej 	if (pnp) {
    253  1.2  thorpej 		for (i = 0; i < 4; i++) {
    254  1.2  thorpej 			if (card->cis1_info[i] == NULL)
    255  1.2  thorpej 				break;
    256  1.2  thorpej 			if (i)
    257  1.2  thorpej 				printf(", ");
    258  1.2  thorpej 			printf("%s", card->cis1_info[i]);
    259  1.2  thorpej 		}
    260  1.2  thorpej 		if (i)
    261  1.2  thorpej 			printf(" ");
    262  1.2  thorpej 		printf("(manufacturer 0x%x, product 0x%x)", card->manufacturer,
    263  1.2  thorpej 		       card->product);
    264  1.2  thorpej 	}
    265  1.2  thorpej 	printf(" function %d", pa->pf->number);
    266  1.2  thorpej 
    267  1.2  thorpej 	return (UNCONF);
    268  1.2  thorpej }
    269  1.2  thorpej 
    270  1.2  thorpej int
    271  1.2  thorpej pcmcia_card_gettype(dev)
    272  1.2  thorpej 	struct device  *dev;
    273  1.2  thorpej {
    274  1.2  thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    275  1.2  thorpej 
    276  1.2  thorpej 	/*
    277  1.2  thorpej 	 * set the iftype to memory if this card has no functions (not yet
    278  1.2  thorpej 	 * probed), or only one function, and that is memory.
    279  1.2  thorpej 	 */
    280  1.2  thorpej 	if (sc->card.pf_head.sqh_first == NULL ||
    281  1.2  thorpej 	    (sc->card.pf_head.sqh_first != NULL &&
    282  1.2  thorpej 	     sc->card.pf_head.sqh_first->pf_list.sqe_next == NULL &&
    283  1.2  thorpej 	     (sc->card.pf_head.sqh_first->cfe_head.sqh_first->iftype ==
    284  1.2  thorpej 	      PCMCIA_IFTYPE_MEMORY)))
    285  1.2  thorpej 		return (PCMCIA_IFTYPE_MEMORY);
    286  1.2  thorpej 	else
    287  1.2  thorpej 		return (PCMCIA_IFTYPE_IO);
    288  1.2  thorpej }
    289  1.2  thorpej 
    290  1.2  thorpej /*
    291  1.2  thorpej  * Initialize a PCMCIA function.  May be called as long as the function is
    292  1.2  thorpej  * disabled.
    293  1.2  thorpej  */
    294  1.2  thorpej void
    295  1.2  thorpej pcmcia_function_init(pf, cfe)
    296  1.2  thorpej 	struct pcmcia_function *pf;
    297  1.2  thorpej 	struct pcmcia_config_entry *cfe;
    298  1.2  thorpej {
    299  1.2  thorpej 
    300  1.2  thorpej 	if (pf->pf_flags & PFF_ENABLED)
    301  1.2  thorpej 		panic("pcmcia_function_init: function is enabled");
    302  1.2  thorpej 
    303  1.2  thorpej 	/* Remember which configuration entry we are using. */
    304  1.2  thorpej 	pf->cfe = cfe;
    305  1.2  thorpej }
    306  1.2  thorpej 
    307  1.2  thorpej /* Enable a PCMCIA function */
    308  1.2  thorpej int
    309  1.2  thorpej pcmcia_function_enable(pf)
    310  1.2  thorpej 	struct pcmcia_function *pf;
    311  1.2  thorpej {
    312  1.2  thorpej 	struct pcmcia_function *tmp;
    313  1.2  thorpej 	int reg;
    314  1.2  thorpej 
    315  1.2  thorpej 	if (pf->cfe == NULL)
    316  1.2  thorpej 		panic("pcmcia_function_enable: function not initialized");
    317  1.2  thorpej 
    318  1.2  thorpej 	if (pf->pf_flags & PFF_ENABLED) {
    319  1.2  thorpej 		/*
    320  1.2  thorpej 		 * Don't do anything if we're already enabled.
    321  1.2  thorpej 		 */
    322  1.2  thorpej 		return (0);
    323  1.2  thorpej 	}
    324  1.2  thorpej 
    325  1.2  thorpej 	/*
    326  1.2  thorpej 	 * Increase the reference count on the socket, enabling power, if
    327  1.2  thorpej 	 * necessary.
    328  1.2  thorpej 	 */
    329  1.2  thorpej 	if (pf->sc->sc_enabled_count++ == 0)
    330  1.2  thorpej 		pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
    331  1.2  thorpej 
    332  1.2  thorpej 	/*
    333  1.2  thorpej 	 * it's possible for different functions' CCRs to be in the same
    334  1.2  thorpej 	 * underlying page.  Check for that.
    335  1.2  thorpej 	 */
    336  1.2  thorpej 
    337  1.2  thorpej 	for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
    338  1.2  thorpej 	    tmp = tmp->pf_list.sqe_next) {
    339  1.2  thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    340  1.2  thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    341  1.2  thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    342  1.2  thorpej 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    343  1.2  thorpej 		      tmp->pf_ccr_realsize))) {
    344  1.2  thorpej 			pf->pf_ccrt = tmp->pf_ccrt;
    345  1.2  thorpej 			pf->pf_ccrh = tmp->pf_ccrh;
    346  1.2  thorpej 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    347  1.2  thorpej 
    348  1.2  thorpej 			/*
    349  1.2  thorpej 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    350  1.2  thorpej 			 * tmp->ccr_base) + pf->ccr_base;
    351  1.2  thorpej 			 */
    352  1.2  thorpej 			pf->pf_ccr_offset =
    353  1.2  thorpej 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    354  1.2  thorpej 			    tmp->ccr_base;
    355  1.2  thorpej 			pf->pf_ccr_window = tmp->pf_ccr_window;
    356  1.2  thorpej 			break;
    357  1.2  thorpej 		}
    358  1.2  thorpej 	}
    359  1.2  thorpej 
    360  1.2  thorpej 	if (tmp == NULL) {
    361  1.2  thorpej 		if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
    362  1.2  thorpej 			goto bad;
    363  1.2  thorpej 
    364  1.2  thorpej 		if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    365  1.2  thorpej 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    366  1.2  thorpej 		    &pf->pf_ccr_window)) {
    367  1.2  thorpej 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    368  1.2  thorpej 			goto bad;
    369  1.2  thorpej 		}
    370  1.2  thorpej 	}
    371  1.2  thorpej 	DPRINTF(("%s: function %d CCR at %d offset %lx: "
    372  1.2  thorpej 	    "%x %x %x %x, %x %x %x %x, %x\n",
    373  1.2  thorpej 	    pf->sc->dev.dv_xname, pf->number,
    374  1.2  thorpej 	    pf->pf_ccr_window, pf->pf_ccr_offset,
    375  1.2  thorpej 	    pcmcia_ccr_read(pf, 0x00),
    376  1.2  thorpej 	    pcmcia_ccr_read(pf, 0x02), pcmcia_ccr_read(pf, 0x04),
    377  1.2  thorpej 	    pcmcia_ccr_read(pf, 0x06), pcmcia_ccr_read(pf, 0x0A),
    378  1.2  thorpej 	    pcmcia_ccr_read(pf, 0x0C), pcmcia_ccr_read(pf, 0x0E),
    379  1.2  thorpej 	    pcmcia_ccr_read(pf, 0x10), pcmcia_ccr_read(pf, 0x12)));
    380  1.2  thorpej 
    381  1.2  thorpej 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    382  1.2  thorpej 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    383  1.2  thorpej 	if (pcmcia_mfc(pf->sc))
    384  1.2  thorpej 		reg |= PCMCIA_CCR_OPTION_FUNC_ENABLE;
    385  1.2  thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    386  1.2  thorpej 
    387  1.2  thorpej 	reg = 0;
    388  1.2  thorpej 
    389  1.2  thorpej 	if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
    390  1.2  thorpej 		reg |= PCMCIA_CCR_STATUS_IOIS8;
    391  1.2  thorpej 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    392  1.2  thorpej 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    393  1.2  thorpej 	/* Not really needed, since we start with 0. */
    394  1.2  thorpej 	if (pf->cfe->flags & PCMCIA_CFE_POWERDOWN)
    395  1.2  thorpej 		reg &= ~PCMCIA_CCR_STATUS_PWRDWN;
    396  1.2  thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    397  1.2  thorpej 
    398  1.2  thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    399  1.2  thorpej 
    400  1.2  thorpej 	pf->pf_flags |= PFF_ENABLED;
    401  1.2  thorpej 	return (0);
    402  1.2  thorpej 
    403  1.2  thorpej  bad:
    404  1.2  thorpej 	/*
    405  1.2  thorpej 	 * Decrement the reference count, and power down the socket, if
    406  1.2  thorpej 	 * necessary.
    407  1.2  thorpej 	 */
    408  1.2  thorpej 	if (pf->sc->sc_enabled_count-- == 1)
    409  1.2  thorpej 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    410  1.2  thorpej 	return (1);
    411  1.2  thorpej }
    412  1.2  thorpej 
    413  1.2  thorpej /* Disable PCMCIA function. */
    414  1.2  thorpej void
    415  1.2  thorpej pcmcia_function_disable(pf)
    416  1.2  thorpej 	struct pcmcia_function *pf;
    417  1.2  thorpej {
    418  1.2  thorpej 	struct pcmcia_function *tmp;
    419  1.2  thorpej 	int reg;
    420  1.2  thorpej 
    421  1.2  thorpej 	if (pf->cfe == NULL)
    422  1.2  thorpej 		panic("pcmcia_function_enable: function not initialized");
    423  1.2  thorpej 
    424  1.2  thorpej 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    425  1.2  thorpej 		/*
    426  1.2  thorpej 		 * Don't do anything if we're already disabled.
    427  1.2  thorpej 		 */
    428  1.2  thorpej 		return;
    429  1.2  thorpej 	}
    430  1.2  thorpej 
    431  1.2  thorpej 	/* Power down the function if the card supports it. */
    432  1.2  thorpej 	if (pf->cfe->flags & PCMCIA_CFE_POWERDOWN) {
    433  1.2  thorpej 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    434  1.2  thorpej 		reg |= PCMCIA_CCR_STATUS_PWRDWN;
    435  1.2  thorpej 		pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    436  1.2  thorpej 	}
    437  1.2  thorpej 
    438  1.2  thorpej 	/*
    439  1.2  thorpej 	 * it's possible for different functions' CCRs to be in the same
    440  1.2  thorpej 	 * underlying page.  Check for that.  Note we mark us as disabled
    441  1.2  thorpej 	 * first to avoid matching ourself.
    442  1.2  thorpej 	 */
    443  1.2  thorpej 
    444  1.2  thorpej 	pf->pf_flags &= ~PFF_ENABLED;
    445  1.2  thorpej 	for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
    446  1.2  thorpej 	    tmp = tmp->pf_list.sqe_next) {
    447  1.2  thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    448  1.2  thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    449  1.2  thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    450  1.2  thorpej 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    451  1.2  thorpej 			break;
    452  1.2  thorpej 	}
    453  1.2  thorpej 
    454  1.2  thorpej 	/* Not used by anyone else; unmap the CCR. */
    455  1.2  thorpej 	if (tmp == NULL) {
    456  1.2  thorpej 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    457  1.2  thorpej 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    458  1.2  thorpej 	}
    459  1.2  thorpej 
    460  1.2  thorpej 	/*
    461  1.2  thorpej 	 * Decrement the reference count, and power down the socket, if
    462  1.2  thorpej 	 * necessary.
    463  1.2  thorpej 	 */
    464  1.2  thorpej 	if (--pf->sc->sc_enabled_count == 0)
    465  1.2  thorpej 		pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
    466  1.2  thorpej }
    467  1.2  thorpej 
    468  1.2  thorpej int
    469  1.2  thorpej pcmcia_io_map(pf, width, offset, size, pcihp, windowp)
    470  1.2  thorpej 	struct pcmcia_function *pf;
    471  1.2  thorpej 	int width;
    472  1.2  thorpej 	bus_addr_t offset;
    473  1.2  thorpej 	bus_size_t size;
    474  1.2  thorpej 	struct pcmcia_io_handle *pcihp;
    475  1.2  thorpej 	int *windowp;
    476  1.2  thorpej {
    477  1.2  thorpej 	bus_addr_t ioaddr;
    478  1.2  thorpej 	int reg;
    479  1.2  thorpej 
    480  1.2  thorpej 	if (pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
    481  1.2  thorpej 	    width, offset, size, pcihp, windowp))
    482  1.2  thorpej 		return (1);
    483  1.2  thorpej 
    484  1.2  thorpej 	ioaddr = pcihp->addr + offset;
    485  1.2  thorpej 
    486  1.2  thorpej 	/*
    487  1.2  thorpej 	 * XXX in the multifunction multi-iospace-per-function case, this
    488  1.2  thorpej 	 * needs to cooperate with io_alloc to make sure that the spaces
    489  1.2  thorpej 	 * don't overlap, and that the ccr's are set correctly
    490  1.2  thorpej 	 */
    491  1.2  thorpej 
    492  1.2  thorpej 	if (pcmcia_mfc(pf->sc)) {
    493  1.2  thorpej 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0, ioaddr & 0xff);
    494  1.2  thorpej 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1, (ioaddr >> 8) & 0xff);
    495  1.2  thorpej 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, size - 1);
    496  1.2  thorpej 
    497  1.2  thorpej 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    498  1.2  thorpej 		reg |= PCMCIA_CCR_OPTION_ADDR_DECODE;
    499  1.2  thorpej 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    500  1.2  thorpej 	}
    501  1.2  thorpej 	return (0);
    502  1.2  thorpej }
    503  1.2  thorpej 
    504  1.2  thorpej void *
    505  1.2  thorpej pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
    506  1.2  thorpej 	struct pcmcia_function *pf;
    507  1.2  thorpej 	int ipl;
    508  1.2  thorpej 	int (*ih_fct) __P((void *));
    509  1.2  thorpej 	void *ih_arg;
    510  1.2  thorpej {
    511  1.2  thorpej 	void *ret;
    512  1.2  thorpej 
    513  1.2  thorpej 	/* behave differently if this is a multifunction card */
    514  1.2  thorpej 
    515  1.2  thorpej 	if (pcmcia_mfc(pf->sc)) {
    516  1.2  thorpej 		int s, ihcnt, hiipl, reg;
    517  1.2  thorpej 		struct pcmcia_function *pf2;
    518  1.2  thorpej 
    519  1.2  thorpej 		/* XXX splraise() use needs to go away! */
    520  1.2  thorpej 
    521  1.2  thorpej 		/*
    522  1.2  thorpej 		 * mask all the ipl's which are already used by this card,
    523  1.2  thorpej 		 * and find the highest ipl number (lowest priority)
    524  1.2  thorpej 		 */
    525  1.2  thorpej 
    526  1.2  thorpej 		ihcnt = 0;
    527  1.2  thorpej 		s = 0;		/* this is only here to keep the compipler
    528  1.2  thorpej 				   happy */
    529  1.2  thorpej 		hiipl = 0;	/* this is only here to keep the compipler
    530  1.2  thorpej 				   happy */
    531  1.2  thorpej 
    532  1.2  thorpej 		for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
    533  1.2  thorpej 		     pf2 = pf2->pf_list.sqe_next) {
    534  1.2  thorpej 			if (pf2->ih_fct) {
    535  1.2  thorpej 				if (ihcnt == 0) {
    536  1.2  thorpej 					s = splraise(pf2->ih_ipl);
    537  1.2  thorpej 					hiipl = pf2->ih_ipl;
    538  1.2  thorpej 					ihcnt++;
    539  1.2  thorpej 				} else {
    540  1.2  thorpej 					splraise(pf2->ih_ipl);
    541  1.2  thorpej 					if (pf2->ih_ipl > hiipl)
    542  1.2  thorpej 						hiipl = pf2->ih_ipl;
    543  1.2  thorpej 				}
    544  1.2  thorpej 			}
    545  1.2  thorpej 		}
    546  1.2  thorpej 
    547  1.2  thorpej 		/* set up the handler for the new function */
    548  1.2  thorpej 
    549  1.2  thorpej 		pf->ih_fct = ih_fct;
    550  1.2  thorpej 		pf->ih_arg = ih_arg;
    551  1.2  thorpej 		pf->ih_ipl = ipl;
    552  1.2  thorpej 
    553  1.2  thorpej 		/*
    554  1.2  thorpej 		 * establish the real interrupt, changing the ipl if
    555  1.2  thorpej 		 * necessary
    556  1.2  thorpej 		 */
    557  1.2  thorpej 
    558  1.2  thorpej 		if (ihcnt == 0) {
    559  1.2  thorpej #ifdef DIAGNOSTIC
    560  1.2  thorpej 			if (pf->sc->ih != NULL)
    561  1.2  thorpej 				panic("card has intr handler, but no function does");
    562  1.2  thorpej #endif
    563  1.2  thorpej 
    564  1.2  thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    565  1.2  thorpej 			    pf->sc->pch, pf, ipl, pcmcia_card_intr, pf->sc);
    566  1.2  thorpej 		} else if (ipl > hiipl) {
    567  1.2  thorpej #ifdef DIAGNOSTIC
    568  1.2  thorpej 			if (pf->sc->ih == NULL)
    569  1.2  thorpej 				panic("functions have ih, but the card does not");
    570  1.2  thorpej #endif
    571  1.2  thorpej 
    572  1.2  thorpej 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    573  1.2  thorpej 			    pf->sc->ih);
    574  1.2  thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    575  1.2  thorpej 			    pf->sc->pch, pf, ipl, pcmcia_card_intr, pf->sc);
    576  1.2  thorpej 		}
    577  1.2  thorpej 		if (ihcnt)
    578  1.2  thorpej 			splx(s);
    579  1.2  thorpej 
    580  1.2  thorpej 		ret = pf->sc->ih;
    581  1.2  thorpej 
    582  1.2  thorpej 		if (ret != NULL) {
    583  1.2  thorpej 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    584  1.2  thorpej 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    585  1.2  thorpej 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    586  1.2  thorpej 
    587  1.2  thorpej 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    588  1.2  thorpej 			reg |= PCMCIA_CCR_STATUS_INTRACK;
    589  1.2  thorpej 			pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    590  1.2  thorpej 		}
    591  1.2  thorpej 	} else {
    592  1.2  thorpej 		ret = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
    593  1.2  thorpej 		    pf, ipl, ih_fct, ih_arg);
    594  1.2  thorpej 	}
    595  1.2  thorpej 
    596  1.2  thorpej 	return (ret);
    597  1.2  thorpej }
    598  1.2  thorpej 
    599  1.2  thorpej void
    600  1.2  thorpej pcmcia_intr_disestablish(pf, ih)
    601  1.2  thorpej 	struct pcmcia_function *pf;
    602  1.2  thorpej 	void *ih;
    603  1.2  thorpej {
    604  1.2  thorpej 
    605  1.2  thorpej 	/* behave differently if this is a multifunction card */
    606  1.2  thorpej 
    607  1.2  thorpej 	if (pcmcia_mfc(pf->sc)) {
    608  1.2  thorpej 		int s, ihcnt, hiipl;
    609  1.2  thorpej 		struct pcmcia_function *pf2;
    610  1.2  thorpej 
    611  1.2  thorpej 		/*
    612  1.2  thorpej 		 * mask all the ipl's which are already used by this card,
    613  1.2  thorpej 		 * and find the highest ipl number (lowest priority).  Skip
    614  1.2  thorpej 		 * the current function.
    615  1.2  thorpej 		 */
    616  1.2  thorpej 
    617  1.2  thorpej 		ihcnt = 0;
    618  1.2  thorpej 		s = 0;		/* this is only here to keep the compipler
    619  1.2  thorpej 				   happy */
    620  1.2  thorpej 		hiipl = 0;	/* this is only here to keep the compipler
    621  1.2  thorpej 				   happy */
    622  1.2  thorpej 
    623  1.2  thorpej 		for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
    624  1.2  thorpej 		     pf2 = pf2->pf_list.sqe_next) {
    625  1.2  thorpej 			if (pf2 == pf)
    626  1.2  thorpej 				continue;
    627  1.2  thorpej 
    628  1.2  thorpej 			if (pf2->ih_fct) {
    629  1.2  thorpej 				if (ihcnt == 0) {
    630  1.2  thorpej 					s = splraise(pf2->ih_ipl);
    631  1.2  thorpej 					hiipl = pf2->ih_ipl;
    632  1.2  thorpej 					ihcnt++;
    633  1.2  thorpej 				} else {
    634  1.2  thorpej 					splraise(pf2->ih_ipl);
    635  1.2  thorpej 					if (pf2->ih_ipl > hiipl)
    636  1.2  thorpej 						hiipl = pf2->ih_ipl;
    637  1.2  thorpej 				}
    638  1.2  thorpej 			}
    639  1.2  thorpej 		}
    640  1.2  thorpej 
    641  1.2  thorpej 		/* null out the handler for this function */
    642  1.2  thorpej 
    643  1.2  thorpej 		pf->ih_fct = NULL;
    644  1.2  thorpej 		pf->ih_arg = NULL;
    645  1.2  thorpej 
    646  1.2  thorpej 		/*
    647  1.2  thorpej 		 * if the ih being removed is lower priority than the lowest
    648  1.2  thorpej 		 * priority remaining interrupt, up the priority.
    649  1.2  thorpej 		 */
    650  1.2  thorpej 
    651  1.2  thorpej #ifdef DIAGNOSTIC
    652  1.2  thorpej 		if (ihcnt == 0) {
    653  1.2  thorpej 			panic("can't remove a handler from a card which has none");
    654  1.2  thorpej 		} else
    655  1.2  thorpej #endif
    656  1.2  thorpej 		if (ihcnt == 1) {
    657  1.2  thorpej #ifdef DIAGNOSTIC
    658  1.2  thorpej 			if (pf->sc->ih == NULL)
    659  1.2  thorpej 				panic("disestablishing last function, but card has no ih");
    660  1.2  thorpej #endif
    661  1.2  thorpej 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    662  1.2  thorpej 			    pf->sc->ih);
    663  1.2  thorpej 			pf->sc->ih = NULL;
    664  1.2  thorpej 		} else if (pf->ih_ipl > hiipl) {
    665  1.2  thorpej #ifdef DIAGNOSTIC
    666  1.2  thorpej 			if (pf->sc->ih == NULL)
    667  1.2  thorpej 				panic("changing ih ipl, but card has no ih");
    668  1.2  thorpej #endif
    669  1.2  thorpej 			pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
    670  1.2  thorpej 			    pf->sc->ih);
    671  1.2  thorpej 			pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
    672  1.2  thorpej 			    pf->sc->pch, pf, hiipl, pcmcia_card_intr, pf->sc);
    673  1.2  thorpej 		}
    674  1.2  thorpej 		if (ihcnt)
    675  1.2  thorpej 			splx(s);
    676  1.2  thorpej 	} else {
    677  1.2  thorpej 		pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
    678  1.2  thorpej 	}
    679  1.2  thorpej }
    680  1.2  thorpej 
    681  1.2  thorpej int
    682  1.2  thorpej pcmcia_card_intr(arg)
    683  1.2  thorpej 	void *arg;
    684  1.2  thorpej {
    685  1.2  thorpej 	struct pcmcia_softc *sc = arg;
    686  1.2  thorpej 	struct pcmcia_function *pf;
    687  1.2  thorpej 	int reg, ret, ret2;
    688  1.2  thorpej 
    689  1.2  thorpej 	ret = 0;
    690  1.2  thorpej 
    691  1.2  thorpej 	for (pf = sc->card.pf_head.sqh_first; pf != NULL;
    692  1.2  thorpej 	    pf = pf->pf_list.sqe_next) {
    693  1.2  thorpej #if 0
    694  1.2  thorpej 		printf("%s: intr fct=%d physaddr=%lx cor=%02x csr=%02x pin=%02x",
    695  1.2  thorpej 		       sc->dev.dv_xname, pf->number,
    696  1.2  thorpej 		       pmap_extract(pmap_kernel(),
    697  1.2  thorpej 		           (vm_offset_t) pf->ccrh) + pf->ccr_offset,
    698  1.2  thorpej 		       bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
    699  1.2  thorpej 		           pf->pf_ccr_offset + PCMCIA_CCR_OPTION),
    700  1.2  thorpej 		       bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
    701  1.2  thorpej 		           pf->pf_ccr_offset + PCMCIA_CCR_STATUS),
    702  1.2  thorpej 		       bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
    703  1.2  thorpej 		           pf->pf_ccr_offset + PCMCIA_CCR_PIN));
    704  1.2  thorpej #endif
    705  1.2  thorpej 		if (pf->ih_fct != NULL &&
    706  1.2  thorpej 		    (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
    707  1.2  thorpej 			reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    708  1.2  thorpej 			if (reg & PCMCIA_CCR_STATUS_INTR) {
    709  1.2  thorpej 				ret2 = (*pf->ih_fct)(pf->ih_arg);
    710  1.2  thorpej 				if (ret2 != 0 && ret == 0)
    711  1.2  thorpej 					ret = ret2;
    712  1.2  thorpej 				reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
    713  1.2  thorpej #if 0
    714  1.2  thorpej 				printf("; csr %02x->%02x",
    715  1.2  thorpej 				    reg, reg & ~PCMCIA_CCR_STATUS_INTR);
    716  1.2  thorpej #endif
    717  1.2  thorpej 				pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
    718  1.2  thorpej 				    reg & ~PCMCIA_CCR_STATUS_INTR);
    719  1.2  thorpej 			}
    720  1.2  thorpej 		}
    721  1.2  thorpej #if 0
    722  1.2  thorpej 		printf("\n");
    723  1.2  thorpej #endif
    724  1.2  thorpej 	}
    725  1.2  thorpej 
    726  1.2  thorpej 	return (ret);
    727  1.2  thorpej }
    728