Home | History | Annotate | Line # | Download | only in pcmcia
nca_pcmcia.c revision 1.1
      1 /*	$NetBSD: nca_pcmcia.c,v 1.1 2000/03/18 17:59:52 mycroft 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/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/buf.h>
     43 
     44 #include <machine/bus.h>
     45 #include <machine/intr.h>
     46 
     47 #include <dev/scsipi/scsi_all.h>
     48 #include <dev/scsipi/scsipi_all.h>
     49 #include <dev/scsipi/scsiconf.h>
     50 
     51 #include <dev/pcmcia/pcmciareg.h>
     52 #include <dev/pcmcia/pcmciavar.h>
     53 #include <dev/pcmcia/pcmciadevs.h>
     54 
     55 #include <dev/ic/ncr5380reg.h>
     56 #include <dev/ic/ncr5380var.h>
     57 #include <dev/ic/ncr53c400reg.h>
     58 
     59 struct nca_pcmcia_softc {
     60 	struct ncr5380_softc	sc_ncr5380;	/* glue to MI code */
     61 
     62 	/* PCMCIA-specific goo. */
     63 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     64 	int sc_io_window;			/* our i/o window */
     65 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     66 	void *sc_ih;				/* interrupt handler */
     67 	int sc_flags;
     68 #define	NCA_PCMCIA_ATTACHED	1		/* attach completed */
     69 #define NCA_PCMCIA_ATTACHING	2		/* attach in progress */
     70 };
     71 
     72 int	nca_pcmcia_match __P((struct device *, struct cfdata *, void *));
     73 void	nca_pcmcia_attach __P((struct device *, struct device *, void *));
     74 int	nca_pcmcia_detach __P((struct device *, int));
     75 int	nca_pcmcia_enable __P((void *, int));
     76 
     77 struct cfattach nca_pcmcia_ca = {
     78 	sizeof(struct nca_pcmcia_softc), nca_pcmcia_match, nca_pcmcia_attach,
     79 	nca_pcmcia_detach
     80 };
     81 
     82 #define MIN_DMA_LEN 128
     83 
     84 /* Options for disconnect/reselect, DMA, and interrupts. */
     85 #define NCA_NO_DISCONNECT	0x00ff
     86 #define NCA_NO_PARITY_CHK	0xff00
     87 
     88 const struct pcmcia_product nca_pcmcia_products[] = {
     89 	{ PCMCIA_STR_PANASONIC_KXLC002,		PCMCIA_VENDOR_PANASONIC,
     90 	  PCMCIA_PRODUCT_PANASONIC_KXLC002,	0 },
     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 void
    110 nca_pcmcia_attach(parent, self, aux)
    111 	struct device *parent, *self;
    112 	void *aux;
    113 {
    114 	struct nca_pcmcia_softc *esc = (void *)self;
    115 	struct ncr5380_softc *sc = &esc->sc_ncr5380;
    116 	struct pcmcia_attach_args *pa = aux;
    117 	struct pcmcia_config_entry *cfe;
    118 	struct pcmcia_function *pf = pa->pf;
    119 	const struct pcmcia_product *pp;
    120 	int flags;
    121 
    122 	esc->sc_pf = pf;
    123 
    124 	for (cfe = SIMPLEQ_FIRST(&pf->cfe_head); cfe != NULL;
    125 	    cfe = SIMPLEQ_NEXT(cfe, 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.scsipi_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 	/*
    181 	 * MD function pointers used by the MI code.
    182 	 */
    183 	sc->sc_pio_out = ncr5380_pio_out;
    184 	sc->sc_pio_in =  ncr5380_pio_in;
    185 	sc->sc_dma_alloc = NULL;
    186 	sc->sc_dma_free  = NULL;
    187 	sc->sc_dma_setup = NULL;
    188 	sc->sc_dma_start = NULL;
    189 	sc->sc_dma_poll  = NULL;
    190 	sc->sc_dma_eop   = NULL;
    191 	sc->sc_dma_stop  = NULL;
    192 	sc->sc_intr_on   = NULL;
    193 	sc->sc_intr_off  = NULL;
    194 
    195 
    196 	/*
    197 	 * Support the "options" (config file flags).
    198 	 * Disconnect/reselect is a per-target mask.
    199 	 * Interrupts and DMA are per-controller.
    200 	 */
    201 #if 0
    202 	flags = 0x0000;	/* no options */
    203 #else
    204 	flags = 0xffff;	/* all options except force poll */
    205 #endif
    206 
    207 	sc->sc_no_disconnect = (flags & NCA_NO_DISCONNECT);
    208 	sc->sc_parity_disable = (flags & NCA_NO_PARITY_CHK) >> 8;
    209 	sc->sc_min_dma_len = MIN_DMA_LEN;
    210 
    211 	/*
    212 	 * Initialize fields used by the MI code
    213 	 */
    214 	sc->sc_regt = esc->sc_pcioh.iot;
    215 	sc->sc_regh = esc->sc_pcioh.ioh;
    216 
    217 	/*
    218 	 *  Initialize nca board itself.
    219 	 */
    220 	esc->sc_flags |= NCA_PCMCIA_ATTACHING;
    221 	ncr5380_attach(sc);
    222 	esc->sc_flags &= ~NCA_PCMCIA_ATTACHING;
    223 	esc->sc_flags |= NCA_PCMCIA_ATTACHED;
    224 	return;
    225 
    226 iomap_failed:
    227 	/* Disable the device. */
    228 	pcmcia_function_disable(esc->sc_pf);
    229 
    230 enable_failed:
    231 	/* Unmap our I/O space. */
    232 	pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
    233 
    234 no_config_entry:
    235 	return;
    236 }
    237 
    238 int
    239 nca_pcmcia_detach(self, flags)
    240 	struct device *self;
    241 	int flags;
    242 {
    243 	struct nca_pcmcia_softc *esc = (void *)self;
    244 	int error;
    245 
    246 	if ((esc->sc_flags & NCA_PCMCIA_ATTACHED) == 0) {
    247 		/* Nothing to detach. */
    248 		return (0);
    249 	}
    250 
    251 	if ((error = ncr5380_detach(&esc->sc_ncr5380, flags)) != 0)
    252 		return (error);
    253 
    254 	/* Unmap our i/o window and i/o space. */
    255 	pcmcia_io_unmap(esc->sc_pf, esc->sc_io_window);
    256 	pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
    257 
    258 	return (0);
    259 }
    260 
    261 int
    262 nca_pcmcia_enable(arg, onoff)
    263 	void *arg;
    264 	int onoff;
    265 {
    266 	struct nca_pcmcia_softc *esc = arg;
    267 
    268 	if (onoff) {
    269 		/* Establish the interrupt handler. */
    270 		esc->sc_ih = pcmcia_intr_establish(esc->sc_pf, IPL_BIO,
    271 		    ncr5380_intr, &esc->sc_ncr5380);
    272 		if (esc->sc_ih == NULL) {
    273 			printf("%s: couldn't establish interrupt handler\n",
    274 			    esc->sc_ncr5380.sc_dev.dv_xname);
    275 			return (EIO);
    276 		}
    277 
    278 		/*
    279 		 * If attach is in progress, we know that card power is
    280 		 * enabled and chip will be initialized later.
    281 		 * Otherwise, enable and reset now.
    282 		 */
    283 		if ((esc->sc_flags & NCA_PCMCIA_ATTACHING) == 0) {
    284 			if (pcmcia_function_enable(esc->sc_pf)) {
    285 				printf("%s: couldn't enable PCMCIA function\n",
    286 				    esc->sc_ncr5380.sc_dev.dv_xname);
    287 				pcmcia_intr_disestablish(esc->sc_pf,
    288 				    esc->sc_ih);
    289 				return (EIO);
    290 			}
    291 
    292 			/* Initialize only chip.  */
    293 			ncr5380_init(&esc->sc_ncr5380);
    294 		}
    295 	} else {
    296 		pcmcia_function_disable(esc->sc_pf);
    297 		pcmcia_intr_disestablish(esc->sc_pf, esc->sc_ih);
    298 	}
    299 
    300 	return (0);
    301 }
    302