Home | History | Annotate | Line # | Download | only in pcmcia
nca_pcmcia.c revision 1.7
      1 /*	$NetBSD: nca_pcmcia.c,v 1.7 2002/09/27 20:41:07 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: nca_pcmcia.c,v 1.7 2002/09/27 20:41:07 thorpej Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 #include <sys/buf.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/intr.h>
     49 
     50 #include <dev/scsipi/scsi_all.h>
     51 #include <dev/scsipi/scsipi_all.h>
     52 #include <dev/scsipi/scsiconf.h>
     53 
     54 #include <dev/pcmcia/pcmciareg.h>
     55 #include <dev/pcmcia/pcmciavar.h>
     56 #include <dev/pcmcia/pcmciadevs.h>
     57 
     58 #include <dev/ic/ncr5380reg.h>
     59 #include <dev/ic/ncr5380var.h>
     60 #include <dev/ic/ncr53c400reg.h>
     61 
     62 struct nca_pcmcia_softc {
     63 	struct ncr5380_softc	sc_ncr5380;	/* glue to MI code */
     64 
     65 	/* PCMCIA-specific goo. */
     66 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     67 	int sc_io_window;			/* our i/o window */
     68 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     69 	void *sc_ih;				/* interrupt handler */
     70 	int sc_flags;
     71 #define	NCA_PCMCIA_ATTACHED	1		/* attach completed */
     72 #define NCA_PCMCIA_ATTACHING	2		/* attach in progress */
     73 };
     74 
     75 int	nca_pcmcia_match __P((struct device *, struct cfdata *, void *));
     76 void	nca_pcmcia_attach __P((struct device *, struct device *, void *));
     77 int	nca_pcmcia_detach __P((struct device *, int));
     78 int	nca_pcmcia_enable __P((struct device *, int));
     79 
     80 const struct cfattach nca_pcmcia_ca = {
     81 	sizeof(struct nca_pcmcia_softc), nca_pcmcia_match, nca_pcmcia_attach,
     82 	nca_pcmcia_detach
     83 };
     84 
     85 #define MIN_DMA_LEN 128
     86 
     87 /* Options for disconnect/reselect, DMA, and interrupts. */
     88 #define NCA_NO_DISCONNECT	0x00ff
     89 #define NCA_NO_PARITY_CHK	0xff00
     90 
     91 const struct pcmcia_product nca_pcmcia_products[] = {
     92 
     93 	{ NULL }
     94 };
     95 
     96 int
     97 nca_pcmcia_match(parent, match, aux)
     98 	struct device *parent;
     99 	struct cfdata *match;
    100 	void *aux;
    101 {
    102 	struct pcmcia_attach_args *pa = aux;
    103 
    104 	if (pcmcia_product_lookup(pa, nca_pcmcia_products,
    105 	    sizeof nca_pcmcia_products[0], NULL) != NULL)
    106 		return (1);
    107 	return (0);
    108 }
    109 
    110 void
    111 nca_pcmcia_attach(parent, self, aux)
    112 	struct device *parent, *self;
    113 	void *aux;
    114 {
    115 	struct nca_pcmcia_softc *esc = (void *)self;
    116 	struct ncr5380_softc *sc = &esc->sc_ncr5380;
    117 	struct pcmcia_attach_args *pa = aux;
    118 	struct pcmcia_config_entry *cfe;
    119 	struct pcmcia_function *pf = pa->pf;
    120 	const struct pcmcia_product *pp;
    121 	int flags;
    122 
    123 	esc->sc_pf = pf;
    124 
    125 	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
    126 		if (cfe->num_memspace != 0 ||
    127 		    cfe->num_iospace != 1)
    128 			continue;
    129 
    130 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    131 		    cfe->iospace[0].length, 0, &esc->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 	/* Enable the card. */
    141 	pcmcia_function_init(pf, cfe);
    142 	if (pcmcia_function_enable(pf)) {
    143 		printf(": function enable failed\n");
    144 		goto enable_failed;
    145 	}
    146 
    147 	/* Map in the I/O space */
    148 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, esc->sc_pcioh.size,
    149 	    &esc->sc_pcioh, &esc->sc_io_window)) {
    150 		printf(": can't map i/o space\n");
    151 		goto iomap_failed;
    152 	}
    153 
    154 	pp = pcmcia_product_lookup(pa, nca_pcmcia_products,
    155 	    sizeof nca_pcmcia_products[0], NULL);
    156 	if (pp == NULL) {
    157 		printf("\n");
    158 		panic("nca_pcmcia_attach: impossible");
    159 	}
    160 
    161 	printf(": %s\n", pp->pp_name);
    162 
    163 	/* We can enable and disable the controller. */
    164 	sc->sc_adapter.adapt_enable = nca_pcmcia_enable;
    165 
    166 	/* Reset into 5380-compat. mode */
    167 	bus_space_write_1(esc->sc_pcioh.iot, esc->sc_pcioh.ioh, C400_CSR,
    168 	    C400_CSR_5380_ENABLE);
    169 
    170 	/* Initialize 5380 compatible register offsets. */
    171 	sc->sci_r0 = C400_5380_REG_OFFSET + 0;
    172 	sc->sci_r1 = C400_5380_REG_OFFSET + 1;
    173 	sc->sci_r2 = C400_5380_REG_OFFSET + 2;
    174 	sc->sci_r3 = C400_5380_REG_OFFSET + 3;
    175 	sc->sci_r4 = C400_5380_REG_OFFSET + 4;
    176 	sc->sci_r5 = C400_5380_REG_OFFSET + 5;
    177 	sc->sci_r6 = C400_5380_REG_OFFSET + 6;
    178 	sc->sci_r7 = C400_5380_REG_OFFSET + 7;
    179 
    180 	sc->sc_rev = NCR_VARIANT_NCR53C400;
    181 
    182 	/*
    183 	 * MD function pointers used by the MI code.
    184 	 */
    185 	sc->sc_pio_out = ncr5380_pio_out;
    186 	sc->sc_pio_in =  ncr5380_pio_in;
    187 	sc->sc_dma_alloc = NULL;
    188 	sc->sc_dma_free  = NULL;
    189 	sc->sc_dma_setup = NULL;
    190 	sc->sc_dma_start = NULL;
    191 	sc->sc_dma_poll  = NULL;
    192 	sc->sc_dma_eop   = NULL;
    193 	sc->sc_dma_stop  = NULL;
    194 	sc->sc_intr_on   = NULL;
    195 	sc->sc_intr_off  = NULL;
    196 
    197 
    198 	/*
    199 	 * Support the "options" (config file flags).
    200 	 * Disconnect/reselect is a per-target mask.
    201 	 * Interrupts and DMA are per-controller.
    202 	 */
    203 #if 0
    204 	flags = 0x0000;	/* no options */
    205 #else
    206 	flags = 0xffff;	/* all options except force poll */
    207 #endif
    208 
    209 	sc->sc_no_disconnect = (flags & NCA_NO_DISCONNECT);
    210 	sc->sc_parity_disable = (flags & NCA_NO_PARITY_CHK) >> 8;
    211 	sc->sc_min_dma_len = MIN_DMA_LEN;
    212 
    213 	/*
    214 	 * Initialize fields used by the MI code
    215 	 */
    216 	sc->sc_regt = esc->sc_pcioh.iot;
    217 	sc->sc_regh = esc->sc_pcioh.ioh;
    218 
    219 	/*
    220 	 *  Initialize nca board itself.
    221 	 */
    222 	esc->sc_flags |= NCA_PCMCIA_ATTACHING;
    223 	ncr5380_attach(sc);
    224 	esc->sc_flags &= ~NCA_PCMCIA_ATTACHING;
    225 	esc->sc_flags |= NCA_PCMCIA_ATTACHED;
    226 	return;
    227 
    228 iomap_failed:
    229 	/* Disable the device. */
    230 	pcmcia_function_disable(esc->sc_pf);
    231 
    232 enable_failed:
    233 	/* Unmap our I/O space. */
    234 	pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
    235 
    236 no_config_entry:
    237 	return;
    238 }
    239 
    240 int
    241 nca_pcmcia_detach(self, flags)
    242 	struct device *self;
    243 	int flags;
    244 {
    245 	struct nca_pcmcia_softc *esc = (void *)self;
    246 	int error;
    247 
    248 	if ((esc->sc_flags & NCA_PCMCIA_ATTACHED) == 0) {
    249 		/* Nothing to detach. */
    250 		return (0);
    251 	}
    252 
    253 	if ((error = ncr5380_detach(&esc->sc_ncr5380, flags)) != 0)
    254 		return (error);
    255 
    256 	/* Unmap our i/o window and i/o space. */
    257 	pcmcia_io_unmap(esc->sc_pf, esc->sc_io_window);
    258 	pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
    259 
    260 	return (0);
    261 }
    262 
    263 int
    264 nca_pcmcia_enable(arg, onoff)
    265 	struct device *arg;
    266 	int onoff;
    267 {
    268 	struct nca_pcmcia_softc *esc = (struct nca_pcmcia_softc*)arg;
    269 
    270 	if (onoff) {
    271 		/* Establish the interrupt handler. */
    272 		esc->sc_ih = pcmcia_intr_establish(esc->sc_pf, IPL_BIO,
    273 		    ncr5380_intr, &esc->sc_ncr5380);
    274 		if (esc->sc_ih == NULL) {
    275 			printf("%s: couldn't establish interrupt handler\n",
    276 			    esc->sc_ncr5380.sc_dev.dv_xname);
    277 			return (EIO);
    278 		}
    279 
    280 		/*
    281 		 * If attach is in progress, we know that card power is
    282 		 * enabled and chip will be initialized later.
    283 		 * Otherwise, enable and reset now.
    284 		 */
    285 		if ((esc->sc_flags & NCA_PCMCIA_ATTACHING) == 0) {
    286 			if (pcmcia_function_enable(esc->sc_pf)) {
    287 				printf("%s: couldn't enable PCMCIA function\n",
    288 				    esc->sc_ncr5380.sc_dev.dv_xname);
    289 				pcmcia_intr_disestablish(esc->sc_pf,
    290 				    esc->sc_ih);
    291 				return (EIO);
    292 			}
    293 
    294 			/* Initialize only chip.  */
    295 			ncr5380_init(&esc->sc_ncr5380);
    296 		}
    297 	} else {
    298 		pcmcia_function_disable(esc->sc_pf);
    299 		pcmcia_intr_disestablish(esc->sc_pf, esc->sc_ih);
    300 	}
    301 
    302 	return (0);
    303 }
    304