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