Home | History | Annotate | Line # | Download | only in pcmcia
aic_pcmcia.c revision 1.16
      1 /*	$NetBSD: aic_pcmcia.c,v 1.16 2000/02/04 09:31:07 enami Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Marc Horowitz.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/select.h>
     35 #include <sys/device.h>
     36 
     37 #include <machine/cpu.h>
     38 #include <machine/bus.h>
     39 #include <machine/intr.h>
     40 
     41 #include <dev/scsipi/scsipi_all.h>
     42 #include <dev/scsipi/scsipiconf.h>
     43 #include <dev/scsipi/scsi_all.h>
     44 
     45 #include <dev/ic/aic6360var.h>
     46 
     47 #include <dev/pcmcia/pcmciareg.h>
     48 #include <dev/pcmcia/pcmciavar.h>
     49 #include <dev/pcmcia/pcmciadevs.h>
     50 
     51 int	aic_pcmcia_match __P((struct device *, struct cfdata *, void *));
     52 void	aic_pcmcia_attach __P((struct device *, struct device *, void *));
     53 int	aic_pcmcia_detach __P((struct device *, int));
     54 
     55 struct aic_pcmcia_softc {
     56 	struct aic_softc sc_aic;		/* real "aic" softc */
     57 
     58 	/* PCMCIA-specific goo. */
     59 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     60 	int sc_io_window;			/* our i/o window */
     61 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     62 	void *sc_ih;				/* interrupt handler */
     63 	int sc_flags;
     64 #define AIC_PCMCIA_ATTACH	0x0001		/* attach is in progress */
     65 };
     66 
     67 struct cfattach aic_pcmcia_ca = {
     68 	sizeof(struct aic_pcmcia_softc), aic_pcmcia_match, aic_pcmcia_attach,
     69 	aic_pcmcia_detach, aic_activate
     70 };
     71 
     72 int	aic_pcmcia_enable __P((void *, int));
     73 
     74 const struct pcmcia_product aic_pcmcia_products[] = {
     75 	{ PCMCIA_STR_ADAPTEC_APA1460,		PCMCIA_VENDOR_ADAPTEC,
     76 	  PCMCIA_PRODUCT_ADAPTEC_APA1460,	0 },
     77 
     78 	{ PCMCIA_STR_ADAPTEC_APA1460A,		PCMCIA_VENDOR_ADAPTEC,
     79 	  PCMCIA_PRODUCT_ADAPTEC_APA1460A,	0 },
     80 
     81 	{ PCMCIA_STR_NEWMEDIA_BUSTOASTER,	PCMCIA_VENDOR_NEWMEDIA,
     82 	  PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER,	0 },
     83 
     84 	{ NULL }
     85 };
     86 
     87 int
     88 aic_pcmcia_match(parent, match, aux)
     89 	struct device *parent;
     90 	struct cfdata *match;
     91 	void *aux;
     92 {
     93 	struct pcmcia_attach_args *pa = aux;
     94 
     95         if (pcmcia_product_lookup(pa, aic_pcmcia_products,
     96             sizeof aic_pcmcia_products[0], NULL) != NULL)
     97 		return (1);
     98 	return (0);
     99 }
    100 
    101 void
    102 aic_pcmcia_attach(parent, self, aux)
    103 	struct device *parent, *self;
    104 	void *aux;
    105 {
    106 	struct aic_pcmcia_softc *psc = (void *)self;
    107 	struct aic_softc *sc = &psc->sc_aic;
    108 	struct pcmcia_attach_args *pa = aux;
    109 	struct pcmcia_config_entry *cfe;
    110 	struct pcmcia_function *pf = pa->pf;
    111 	const struct pcmcia_product *pp;
    112 
    113 	psc->sc_pf = pf;
    114 
    115 	for (cfe = SIMPLEQ_FIRST(&pf->cfe_head); cfe != NULL;
    116 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    117 		if (cfe->num_memspace != 0 ||
    118 		    cfe->num_iospace != 1)
    119 			continue;
    120 
    121 		/*
    122 		 * The bustoaster has a default config as first
    123 		 * entry, we don't want to use that.
    124 		 */
    125 		if (pa->manufacturer == PCMCIA_VENDOR_NEWMEDIA &&
    126 		    pa->product == PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER &&
    127 		    cfe->iospace[0].start == 0)
    128 			continue;
    129 
    130 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    131 		    cfe->iospace[0].length, 0, &psc->sc_pcioh) == 0)
    132 			break;
    133 	}
    134 
    135 	if (cfe == 0) {
    136 		printf(": can't alloc i/o space\n");
    137 		goto no_config_entry;
    138 	}
    139 
    140 	sc->sc_iot = psc->sc_pcioh.iot;
    141 	sc->sc_ioh = psc->sc_pcioh.ioh;
    142 
    143 	/* Enable the card. */
    144 	pcmcia_function_init(pf, cfe);
    145 	if (pcmcia_function_enable(pf)) {
    146 		printf(": function enable failed\n");
    147 		goto enable_failed;
    148 	}
    149 
    150 	/* Map in the io space */
    151 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
    152 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    153 		printf(": can't map i/o space\n");
    154 		goto iomap_failed;
    155 	}
    156 
    157 	if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
    158 		printf(": unable to detect chip!\n");
    159 		goto no_aic_found;
    160 	}
    161 
    162 	pp = pcmcia_product_lookup(pa, aic_pcmcia_products,
    163 	    sizeof aic_pcmcia_products[0], NULL);
    164 	if (pp == NULL) {
    165 		printf("\n");
    166 		panic("aic_pcmcia_attach: impossible");
    167 	}
    168 
    169 	printf(": %s\n", pp->pp_name);
    170 
    171 	/* We can enable and disable the controller. */
    172 	sc->sc_adapter.scsipi_enable = aic_pcmcia_enable;
    173 
    174 	psc->sc_flags |= AIC_PCMCIA_ATTACH;
    175 	aicattach(sc);
    176 	psc->sc_flags &= ~AIC_PCMCIA_ATTACH;
    177 	return;
    178 
    179  no_aic_found:
    180 	/* Unmap our i/o window. */
    181 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    182 
    183  iomap_failed:
    184 	/* Disable the device. */
    185 	pcmcia_function_disable(psc->sc_pf);
    186 
    187  enable_failed:
    188 	/* Unmap our i/o space. */
    189 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    190 
    191  no_config_entry:
    192 	psc->sc_io_window = -1;
    193 }
    194 
    195 int
    196 aic_pcmcia_detach(self, flags)
    197 	struct device *self;
    198 	int flags;
    199 {
    200 	struct aic_pcmcia_softc *sc = (struct aic_pcmcia_softc *)self;
    201 	int error;
    202 
    203 	if (sc->sc_io_window == -1)
    204 		/* Nothing to detach. */
    205 		return (0);
    206 
    207 	if ((error = aic_detach(self, flags)) != 0)
    208 		return (error);
    209 
    210 	/* Unmap our i/o window and i/o space. */
    211 	pcmcia_io_unmap(sc->sc_pf, sc->sc_io_window);
    212 	pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
    213 
    214 	return (0);
    215 }
    216 int
    217 aic_pcmcia_enable(arg, onoff)
    218 	void *arg;
    219 	int onoff;
    220 {
    221 	struct aic_pcmcia_softc *psc = arg;
    222 
    223 	if (onoff) {
    224 		/* Establish the interrupt handler. */
    225 		psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_BIO,
    226 		    aicintr, &psc->sc_aic);
    227 		if (psc->sc_ih == NULL) {
    228 			printf("%s: couldn't establish interrupt handler\n",
    229 			    psc->sc_aic.sc_dev.dv_xname);
    230 			return (EIO);
    231 		}
    232 
    233 		/*
    234 		 * If attach is in progress, we know that card power is
    235 		 * enabled and chip will be initialized later.
    236 		 * Otherwise, enable and reset now.
    237 		 */
    238 		if ((psc->sc_flags & AIC_PCMCIA_ATTACH) == 0) {
    239 			if (pcmcia_function_enable(psc->sc_pf)) {
    240 				printf("%s: couldn't enable PCMCIA function\n",
    241 				    psc->sc_aic.sc_dev.dv_xname);
    242 				pcmcia_intr_disestablish(psc->sc_pf,
    243 				    psc->sc_ih);
    244 				return (EIO);
    245 			}
    246 
    247 			/* Initialize only chip.  */
    248 			aic_init(&psc->sc_aic, 0);
    249 		}
    250 	} else {
    251 		pcmcia_function_disable(psc->sc_pf);
    252 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    253 	}
    254 
    255 	return (0);
    256 }
    257