Home | History | Annotate | Line # | Download | only in isa
nca_isa.c revision 1.8
      1 /*	$NetBSD: nca_isa.c,v 1.8 2001/11/13 08:01:26 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by John M. Ruschmeyer.
      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 /*
     40  * FreeBSD generic NCR-5380/NCR-53C400 SCSI driver
     41  *
     42  * Copyright (C) 1994 Serge Vakulenko (vak (at) cronyx.ru)
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPERS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  */
     65 
     66 #include <sys/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: nca_isa.c,v 1.8 2001/11/13 08:01:26 lukem Exp $");
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/device.h>
     72 #include <sys/buf.h>
     73 
     74 #include <machine/bus.h>
     75 #include <machine/intr.h>
     76 
     77 #include <dev/scsipi/scsi_all.h>
     78 #include <dev/scsipi/scsipi_all.h>
     79 #include <dev/scsipi/scsiconf.h>
     80 
     81 #include <dev/isa/isavar.h>
     82 #include <dev/isa/isadmavar.h>
     83 
     84 #include <dev/ic/ncr5380reg.h>
     85 #include <dev/ic/ncr5380var.h>
     86 #include <dev/ic/ncr53c400reg.h>
     87 
     88 struct nca_isa_softc {
     89 	struct ncr5380_softc	sc_ncr5380;	/* glue to MI code */
     90 
     91         void *sc_ih;
     92         int sc_irq;
     93 	int sc_options;
     94 };
     95 
     96 struct nca_isa_probe_data {
     97 	struct device sc_dev;
     98 	int sc_reg_offset;
     99 	int sc_host_type;
    100 };
    101 
    102 int	nca_isa_find __P((bus_space_tag_t, bus_space_handle_t, bus_size_t,
    103 	    struct nca_isa_probe_data *));
    104 int	nca_isa_match __P((struct device *, struct cfdata *, void *));
    105 void	nca_isa_attach __P((struct device *, struct device *, void *));
    106 int	nca_isa_test __P((bus_space_tag_t, bus_space_handle_t, bus_size_t));
    107 
    108 struct cfattach nca_isa_ca = {
    109 	sizeof(struct nca_isa_softc), nca_isa_match, nca_isa_attach
    110 };
    111 
    112 
    113 /* Supported controller types */
    114 #define MAX_NCA_CONTROLLER	3
    115 #define CTLR_NCR_5380	1
    116 #define	CTLR_NCR_53C400	2
    117 #define CTLR_PAS16	3
    118 
    119 #define NCA_ISA_IOSIZE 16
    120 #define MIN_DMA_LEN 128
    121 
    122 /* Options for disconnect/reselect, DMA, and interrupts. */
    123 #define NCA_NO_DISCONNECT    0xff
    124 #define NCA_NO_PARITY_CHK  0xff00
    125 #define NCA_FORCE_POLLING 0x10000
    126 
    127 
    128 /*
    129  * Initialization and test function used by nca_isa_find()
    130  */
    131 int
    132 nca_isa_test(iot, ioh, reg_offset)
    133 	bus_space_tag_t	iot;
    134 	bus_space_handle_t ioh;
    135 	bus_size_t reg_offset;
    136 {
    137 	/* Reset the SCSI bus. */
    138 	bus_space_write_1(iot, ioh, reg_offset + C80_ICR, SCI_ICMD_RST);
    139 	bus_space_write_1(iot, ioh, reg_offset + C80_ODR, 0);
    140 	/* Hold reset for at least 25 microseconds. */
    141 	delay(500);
    142 	/* Check that status cleared. */
    143 	if (bus_space_read_1(iot, ioh, reg_offset + C80_CSBR) != SCI_BUS_RST) {
    144 #ifdef DEBUG
    145 		printf("nca_isa_find: reset status not cleared [0x%x]\n",
    146 		    bus_space_read_1(iot, ioh, reg_offset+C80_CSBR));
    147 #endif
    148 		bus_space_write_1(iot, ioh, reg_offset+C80_ICR, 0);
    149 		return 0;
    150 	}
    151 	/* Clear reset. */
    152 	bus_space_write_1(iot, ioh, reg_offset + C80_ICR, 0);
    153 	/* Wait a Bus Clear Delay (800 ns + bus free delay 800 ns). */
    154 	delay(16000);
    155 
    156 	/* Read RPI port, resetting parity/interrupt state. */
    157 	bus_space_read_1(iot, ioh, reg_offset + C80_RPIR);
    158 
    159 	/* Test BSR: parity error, interrupt request and busy loss state
    160 	 * should be cleared. */
    161 	if (bus_space_read_1(iot, ioh, reg_offset + C80_BSR) & (SCI_CSR_PERR |
    162 	    SCI_CSR_INT | SCI_CSR_DISC)) {
    163 #ifdef DEBUG
    164 		printf("nca_isa_find: Parity/Interrupt/Busy not cleared [0x%x]\n",
    165 		    bus_space_read_1(iot, ioh, reg_offset+C80_BSR));
    166 #endif
    167 		return 0;
    168 	}
    169 
    170 	/* We must have found one */
    171 	return 1;
    172 }
    173 
    174 
    175 /*
    176  * Look for the board
    177  */
    178 int
    179 nca_isa_find(iot, ioh, max_offset, epd)
    180 	bus_space_tag_t iot;
    181 	bus_space_handle_t ioh;
    182 	bus_size_t max_offset;
    183 	struct nca_isa_probe_data *epd;
    184 {
    185 	/*
    186 	 * We check for the existence of a board by trying to initialize it,
    187 	 * Then sending the commands to reset the SCSI bus.
    188 	 * (Unfortunately, this duplicates code which is already in the MI
    189 	 * driver. Unavoidable as that code is not suited to this task.)
    190 	 * This is largely stolen from FreeBSD.
    191 	 */
    192 
    193 	int 		cont_type;
    194 	bus_size_t	base_offset, reg_offset = 0;
    195 
    196 	/*
    197 	 * Some notes:
    198 	 * In the case of a port-mapped board, we should be pointing
    199 	 * right at the chip registers (if they are there at all).
    200 	 * For a memory-mapped card, we loop through the 16K paragraph,
    201 	 * 8 bytes at a time, until we either find it or run out
    202 	 * of region. This means we will probably be doing things like
    203 	 * trying to write to ROMS, etc. Hopefully, this is not a problem.
    204 	 */
    205 
    206 	for (base_offset = 0; base_offset < max_offset; base_offset += 0x08) {
    207 #ifdef DEBUG
    208 		printf("nca_isa_find: testing offset 0x%x\n", (int)base_offset);
    209 #endif
    210 
    211 		/* See if anything is there */
    212 		if (bus_space_read_1(iot, ioh, base_offset) == 0xff)
    213 			continue;
    214 
    215 		/* Loop around for each board type */
    216 		for (cont_type = 1; cont_type <= MAX_NCA_CONTROLLER; cont_type++) {
    217 			/* Per-controller initialization */
    218 			switch (cont_type) {
    219 			case CTLR_NCR_5380:
    220 				/* No special inits */
    221 				reg_offset = 0;
    222 				break;
    223 			case CTLR_NCR_53C400:
    224 				/* Reset into 5380-compat. mode */
    225 				bus_space_write_1(iot, ioh,
    226 				    base_offset + C400_CSR,
    227 				    C400_CSR_5380_ENABLE);
    228 				reg_offset = C400_5380_REG_OFFSET;
    229 				break;
    230 			case CTLR_PAS16:
    231 				/* Not currently supported */
    232 				reg_offset = 0;
    233 				cont_type = 0;
    234 				continue;
    235 			}
    236 
    237 			/* Initialize controller and bus */
    238 			if (nca_isa_test(iot, ioh, base_offset+reg_offset)) {
    239 				epd->sc_reg_offset = base_offset;
    240 				epd->sc_host_type = cont_type;
    241 				return cont_type;	/* This must be it */
    242 			}
    243 		}
    244 	}
    245 
    246 	/* If we got here, we didn't find one */
    247 	return 0;
    248 }
    249 
    250 
    251 /*
    252  * See if there is anything at the config'd address.
    253  * If so, call the real probe to see what it is.
    254  */
    255 int
    256 nca_isa_match(parent, match, aux)
    257 	struct device *parent;
    258 	struct cfdata *match;
    259 	void *aux;
    260 {
    261 	struct isa_attach_args *ia = aux;
    262 	bus_space_tag_t iot = ia->ia_iot;
    263 	bus_space_tag_t memt = ia->ia_memt;
    264 	bus_space_handle_t ioh;
    265 	struct nca_isa_probe_data epd;
    266 	int rv = 0;
    267 
    268 	/* See if we are looking for a port- or memory-mapped adapter */
    269 	if (ia->ia_iobase != -1) {
    270 		/* Port-mapped card */
    271 		if (bus_space_map(iot, ia->ia_iobase, NCA_ISA_IOSIZE, 0, &ioh))
    272 			return 0;
    273 
    274 		/* See if a 53C80/53C400 is there */
    275 		rv = nca_isa_find(iot, ioh, 0x07, &epd);
    276 
    277 		bus_space_unmap(iot, ioh, NCA_ISA_IOSIZE);
    278 	} else {
    279 		/* Memory-mapped card */
    280 		if (bus_space_map(memt, ia->ia_maddr, 0x4000, 0, &ioh))
    281 			return 0;
    282 
    283 		/* See if a 53C80/53C400 is somewhere in this para. */
    284 		rv = nca_isa_find(memt, ioh, 0x03ff0, &epd);
    285 
    286 		bus_space_unmap(memt, ioh, 0x04000);
    287 	}
    288 
    289 	/* Adjust the attachment args if we found one */
    290 	if (rv) {
    291 		if (ia->ia_iobase != -1) {
    292 			/* Port-mapped */
    293 			ia->ia_iosize = NCA_ISA_IOSIZE;
    294 		} else {
    295 			/* Memory-mapped */
    296 			ia->ia_maddr += epd.sc_reg_offset;
    297 			ia->ia_msize = NCA_ISA_IOSIZE;
    298 			ia->ia_iosize = 0;
    299 		}
    300 	}
    301 
    302 	return rv;
    303 }
    304 
    305 /*
    306  * Attach this instance, and then all the sub-devices
    307  */
    308 void
    309 nca_isa_attach(parent, self, aux)
    310 	struct device *parent, *self;
    311 	void *aux;
    312 {
    313 	struct isa_attach_args *ia = aux;
    314 	struct nca_isa_softc *esc = (void *)self;
    315 	struct ncr5380_softc *sc = &esc->sc_ncr5380;
    316 	bus_space_tag_t iot = ia->ia_iot;
    317 	bus_space_handle_t ioh;
    318 	struct nca_isa_probe_data epd;
    319 	isa_chipset_tag_t ic = ia->ia_ic;
    320 
    321 	printf("\n");
    322 
    323 	if (ia->ia_iobase != -1) {
    324 		iot = ia->ia_iot;
    325 		if (bus_space_map(iot, ia->ia_iobase, NCA_ISA_IOSIZE, 0, &ioh)) {
    326 			printf("%s: can't map i/o space\n",
    327 			    sc->sc_dev.dv_xname);
    328 			return;
    329 		}
    330 	} else {
    331 		iot = ia->ia_memt;
    332 		if (bus_space_map(iot, ia->ia_maddr, NCA_ISA_IOSIZE, 0, &ioh)) {
    333 			printf("%s: can't map mem space\n",
    334 			    sc->sc_dev.dv_xname);
    335 			return;
    336 		}
    337 	}
    338 
    339 	switch (nca_isa_find(iot, ioh, NCA_ISA_IOSIZE, &epd)) {
    340 	case 0:
    341 		/* Not found- must have gone away */
    342 		printf("%s: nca_isa_find failed\n", sc->sc_dev.dv_xname);
    343 		return;
    344 	case CTLR_NCR_5380:
    345 		printf("%s: NCR 53C80 detected\n", sc->sc_dev.dv_xname);
    346 		sc->sci_r0 = 0;
    347 		sc->sci_r1 = 1;
    348 		sc->sci_r2 = 2;
    349 		sc->sci_r3 = 3;
    350 		sc->sci_r4 = 4;
    351 		sc->sci_r5 = 5;
    352 		sc->sci_r6 = 6;
    353 		sc->sci_r7 = 7;
    354 		sc->sc_rev = NCR_VARIANT_NCR5380;
    355 		break;
    356 	case CTLR_NCR_53C400:
    357 		printf("%s: NCR 53C400 detected\n", sc->sc_dev.dv_xname);
    358 		sc->sci_r0 = C400_5380_REG_OFFSET + 0;
    359 		sc->sci_r1 = C400_5380_REG_OFFSET + 1;
    360 		sc->sci_r2 = C400_5380_REG_OFFSET + 2;
    361 		sc->sci_r3 = C400_5380_REG_OFFSET + 3;
    362 		sc->sci_r4 = C400_5380_REG_OFFSET + 4;
    363 		sc->sci_r5 = C400_5380_REG_OFFSET + 5;
    364 		sc->sci_r6 = C400_5380_REG_OFFSET + 6;
    365 		sc->sci_r7 = C400_5380_REG_OFFSET + 7;
    366 		sc->sc_rev = NCR_VARIANT_NCR53C400;
    367 		break;
    368 	case CTLR_PAS16:
    369 		printf("%s: ProAudio Spectrum 16 detected\n", sc->sc_dev.dv_xname);
    370 		sc->sc_rev = NCR_VARIANT_PAS16;
    371 		break;
    372 	}
    373 
    374 
    375 	/*
    376 	 * MD function pointers used by the MI code.
    377 	 */
    378 	sc->sc_pio_out = ncr5380_pio_out;
    379 	sc->sc_pio_in =  ncr5380_pio_in;
    380 	sc->sc_dma_alloc = NULL;
    381 	sc->sc_dma_free  = NULL;
    382 	sc->sc_dma_setup = NULL;
    383 	sc->sc_dma_start = NULL;
    384 	sc->sc_dma_poll  = NULL;
    385 	sc->sc_dma_eop   = NULL;
    386 	sc->sc_dma_stop  = NULL;
    387 	sc->sc_intr_on   = NULL;
    388 	sc->sc_intr_off  = NULL;
    389 
    390 	if (ia->ia_irq != IRQUNK) {
    391 		esc->sc_ih = isa_intr_establish(ic, ia->ia_irq, IST_EDGE,
    392 				IPL_BIO, ncr5380_intr, esc);
    393 		if (esc->sc_ih == NULL) {
    394 			printf("nca: couldn't establish interrupt\n");
    395 			return;
    396 		}
    397 	} else
    398 		sc->sc_flags |= NCR5380_FORCE_POLLING;
    399 
    400 
    401 	/*
    402 	 * Support the "options" (config file flags).
    403 	 * Disconnect/reselect is a per-target mask.
    404 	 * Interrupts and DMA are per-controller.
    405 	 */
    406 #if 0
    407 	esc->sc_options = 0x00000;	/* no options */
    408 #else
    409 	esc->sc_options = 0x0ffff;	/* all options except force poll */
    410 #endif
    411 
    412 	sc->sc_no_disconnect =
    413 		(esc->sc_options & NCA_NO_DISCONNECT);
    414 	sc->sc_parity_disable =
    415 		(esc->sc_options & NCA_NO_PARITY_CHK) >> 8;
    416 	if (esc->sc_options & NCA_FORCE_POLLING)
    417 		sc->sc_flags |= NCR5380_FORCE_POLLING;
    418 	sc->sc_min_dma_len = MIN_DMA_LEN;
    419 
    420 
    421 	/*
    422 	 * Initialize fields used by the MI code
    423 	 */
    424 	sc->sc_regt = iot;
    425 	sc->sc_regh = ioh;
    426 
    427 	/*
    428 	 * Fill in our portion of the scsipi_adapter.
    429 	 */
    430 	sc->sc_adapter.adapt_request = ncr5380_scsipi_request;
    431 	sc->sc_adapter.adapt_minphys = minphys;
    432 
    433 	/*
    434 	 * Fill in our portion of the scsipi_channel.
    435 	 */
    436 
    437 	sc->sc_channel.chan_id = 7;
    438 
    439 	/*
    440 	 *  Initialize nca board itself.
    441 	 */
    442 	ncr5380_attach(sc);
    443 }
    444