Home | History | Annotate | Line # | Download | only in pcmcia
nca_pcmcia.c revision 1.16
      1 /*	$NetBSD: nca_pcmcia.c,v 1.16 2004/08/10 20:25:03 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2004 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.16 2004/08/10 20:25:03 mycroft 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_function *sc_pf;		/* our PCMCIA function */
     67 	void *sc_ih;				/* interrupt handler */
     68 
     69 	int sc_state;
     70 #define	NCA_PCMCIA_ATTACH1	1
     71 #define	NCA_PCMCIA_ATTACH2	2
     72 #define NCA_PCMCIA_ATTACHED	3
     73 };
     74 
     75 int	nca_pcmcia_match __P((struct device *, struct cfdata *, void *));
     76 int	nca_pcmcia_validate_config __P((struct pcmcia_config_entry *));
     77 void	nca_pcmcia_attach __P((struct device *, struct device *, void *));
     78 int	nca_pcmcia_detach __P((struct device *, int));
     79 int	nca_pcmcia_enable __P((struct device *, int));
     80 
     81 CFATTACH_DECL(nca_pcmcia, sizeof(struct nca_pcmcia_softc),
     82     nca_pcmcia_match, nca_pcmcia_attach, nca_pcmcia_detach, NULL);
     83 
     84 #define MIN_DMA_LEN 128
     85 
     86 /* Options for disconnect/reselect, DMA, and interrupts. */
     87 #define NCA_NO_DISCONNECT	0x00ff
     88 #define NCA_NO_PARITY_CHK	0xff00
     89 
     90 const struct pcmcia_product nca_pcmcia_products[] = {
     91 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
     92 	  PCMCIA_CIS_INVALID },
     93 };
     94 const size_t nca_pcmcia_nproducts =
     95     sizeof(nca_pcmcia_products) / sizeof(nca_pcmcia_products[0]);
     96 
     97 int
     98 nca_pcmcia_match(parent, match, aux)
     99 	struct device *parent;
    100 	struct cfdata *match;
    101 	void *aux;
    102 {
    103 	struct pcmcia_attach_args *pa = aux;
    104 
    105 	if (pcmcia_product_lookup(pa, nca_pcmcia_products, nca_pcmcia_nproducts,
    106 	    sizeof(nca_pcmcia_products[0]), NULL))
    107 		return (1);
    108 	return (0);
    109 }
    110 
    111 int
    112 nca_pcmcia_validate_config(cfe)
    113 	struct pcmcia_config_entry *cfe;
    114 {
    115 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    116 	    cfe->num_memspace != 0 ||
    117 	    cfe->num_iospace != 1)
    118 		return (EINVAL);
    119 	return (0);
    120 }
    121 
    122 void
    123 nca_pcmcia_attach(parent, self, aux)
    124 	struct device *parent, *self;
    125 	void *aux;
    126 {
    127 	struct nca_pcmcia_softc *esc = (void *)self;
    128 	struct ncr5380_softc *sc = &esc->sc_ncr5380;
    129 	struct pcmcia_attach_args *pa = aux;
    130 	struct pcmcia_config_entry *cfe;
    131 	struct pcmcia_function *pf = pa->pf;
    132 	int flags;
    133 	int error;
    134 
    135 	esc->sc_pf = pf;
    136 
    137 	error = pcmcia_function_configure(pf, nca_pcmcia_validate_config);
    138 	if (error) {
    139 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    140 		    error);
    141 		return;
    142 	}
    143 
    144 	cfe = pf->cfe;
    145 	sc->sc_regt = cfe->iospace[0].handle.iot;
    146 	sc->sc_regh = cfe->iospace[0].handle.ioh;
    147 
    148 	/* Initialize 5380 compatible register offsets. */
    149 	sc->sci_r0 = C400_5380_REG_OFFSET + 0;
    150 	sc->sci_r1 = C400_5380_REG_OFFSET + 1;
    151 	sc->sci_r2 = C400_5380_REG_OFFSET + 2;
    152 	sc->sci_r3 = C400_5380_REG_OFFSET + 3;
    153 	sc->sci_r4 = C400_5380_REG_OFFSET + 4;
    154 	sc->sci_r5 = C400_5380_REG_OFFSET + 5;
    155 	sc->sci_r6 = C400_5380_REG_OFFSET + 6;
    156 	sc->sci_r7 = C400_5380_REG_OFFSET + 7;
    157 
    158 	sc->sc_rev = NCR_VARIANT_NCR53C400;
    159 
    160 	/*
    161 	 * MD function pointers used by the MI code.
    162 	 */
    163 	sc->sc_pio_out = ncr5380_pio_out;
    164 	sc->sc_pio_in =  ncr5380_pio_in;
    165 	sc->sc_dma_alloc = NULL;
    166 	sc->sc_dma_free  = NULL;
    167 	sc->sc_dma_setup = NULL;
    168 	sc->sc_dma_start = NULL;
    169 	sc->sc_dma_poll  = NULL;
    170 	sc->sc_dma_eop   = NULL;
    171 	sc->sc_dma_stop  = NULL;
    172 	sc->sc_intr_on   = NULL;
    173 	sc->sc_intr_off  = NULL;
    174 
    175 	/*
    176 	 * Support the "options" (config file flags).
    177 	 * Disconnect/reselect is a per-target mask.
    178 	 * Interrupts and DMA are per-controller.
    179 	 */
    180 #if 0
    181 	flags = 0x0000;	/* no options */
    182 #else
    183 	flags = 0xffff;	/* all options except force poll */
    184 #endif
    185 
    186 	sc->sc_no_disconnect = (flags & NCA_NO_DISCONNECT);
    187 	sc->sc_parity_disable = (flags & NCA_NO_PARITY_CHK) >> 8;
    188 	sc->sc_min_dma_len = MIN_DMA_LEN;
    189 
    190 	error = nca_pcmcia_enable(self, 1);
    191 	if (error)
    192 		goto fail;
    193 
    194 	sc->sc_adapter.adapt_enable = nca_pcmcia_enable;
    195 
    196 	esc->sc_state = NCA_PCMCIA_ATTACH1;
    197 	ncr5380_attach(sc);
    198 	if (esc->sc_state == NCA_PCMCIA_ATTACH1)
    199 		nca_pcmcia_disable(self, 0);
    200 	esc->sc_state = NCA_PCMCIA_ATTACHED;
    201 	return;
    202 
    203 fail:
    204 	pcmcia_function_unconfigure(pf);
    205 }
    206 
    207 int
    208 nca_pcmcia_detach(self, flags)
    209 	struct device *self;
    210 	int flags;
    211 {
    212 	struct nca_pcmcia_softc *sc = (void *)self;
    213 	int error;
    214 
    215 	if (sc->sc_state != NCA_PCMCIA_ATTACHED)
    216 		return (0);
    217 
    218 	error = ncr5380_detach(&sc->sc_ncr5380, flags);
    219 	if (error)
    220 		return (error);
    221 
    222 	pcmcia_function_unconfigure(sc->sc_pf);
    223 
    224 	return (0);
    225 }
    226 
    227 int
    228 nca_pcmcia_enable(arg, onoff)
    229 	struct device *arg;
    230 	int onoff;
    231 {
    232 	struct nca_pcmcia_softc *sc = (struct nca_pcmcia_softc*)arg;
    233 	int error;
    234 
    235 	if (onoff) {
    236 		/*
    237 		 * If attach is in progress, we already have the device
    238 		 * powered up.
    239 		 */
    240 		if (sc->sc_state == NCA_PCMCIA_ATTACH1) {
    241 			sc->sc_state = NCA_PCMCIA_ATTACH2;
    242 		} else {
    243 			/* Establish the interrupt handler. */
    244 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    245 			    ncr5380_intr, &sc->sc_ncr5380);
    246 			if (!sc->sc_ih)
    247 				return (EIO);
    248 
    249 			error = pcmcia_function_enable(sc->sc_pf);
    250 			if (error) {
    251 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    252 				sc->sc_ih = 0;
    253 				return (error);
    254 			}
    255 
    256 			/* Initialize only chip.  */
    257 			ncr5380_init(&sc->sc_ncr5380);
    258 		}
    259 	} else {
    260 		pcmcia_function_disable(sc->sc_pf);
    261 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    262 		sc->sc_ih = 0;
    263 	}
    264 
    265 	return (0);
    266 }
    267