Home | History | Annotate | Line # | Download | only in pcmcia
aic_pcmcia.c revision 1.15
      1 /*	$NetBSD: aic_pcmcia.c,v 1.15 2000/02/04 01:27:12 cgd 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 		return;
    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 		return;
    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 		return;
    155 	}
    156 
    157 	if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
    158 		printf(": unable to detect chip!\n");
    159 		return;
    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 }
    178 
    179 int
    180 aic_pcmcia_detach(self, flags)
    181 	struct device *self;
    182 	int flags;
    183 {
    184 	struct aic_pcmcia_softc *sc = (struct aic_pcmcia_softc *)self;
    185 	int error;
    186 
    187 	if ((error = aic_detach(self, flags)) != 0)
    188 		return (error);
    189 
    190 	/* Unmap our i/o window and i/o space. */
    191 	pcmcia_io_unmap(sc->sc_pf, sc->sc_io_window);
    192 	pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
    193 
    194 	return (0);
    195 }
    196 int
    197 aic_pcmcia_enable(arg, onoff)
    198 	void *arg;
    199 	int onoff;
    200 {
    201 	struct aic_pcmcia_softc *psc = arg;
    202 
    203 	if (onoff) {
    204 		/* Establish the interrupt handler. */
    205 		psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_BIO,
    206 		    aicintr, &psc->sc_aic);
    207 		if (psc->sc_ih == NULL) {
    208 			printf("%s: couldn't establish interrupt handler\n",
    209 			    psc->sc_aic.sc_dev.dv_xname);
    210 			return (EIO);
    211 		}
    212 
    213 		/*
    214 		 * If attach is in progress, we know that card power is
    215 		 * enabled and chip will be initialized later.
    216 		 * Otherwise, enable and reset now.
    217 		 */
    218 		if ((psc->sc_flags & AIC_PCMCIA_ATTACH) == 0) {
    219 			if (pcmcia_function_enable(psc->sc_pf)) {
    220 				printf("%s: couldn't enable PCMCIA function\n",
    221 				    psc->sc_aic.sc_dev.dv_xname);
    222 				pcmcia_intr_disestablish(psc->sc_pf,
    223 				    psc->sc_ih);
    224 				return (EIO);
    225 			}
    226 
    227 			/* Initialize only chip.  */
    228 			aic_init(&psc->sc_aic, 0);
    229 		}
    230 	} else {
    231 		pcmcia_function_disable(psc->sc_pf);
    232 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    233 	}
    234 
    235 	return (0);
    236 }
    237