Home | History | Annotate | Line # | Download | only in isa
nca_isa.c revision 1.2
      1 /*	$NetBSD: nca_isa.c,v 1.2 2000/03/18 13:17:03 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)  1998 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/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/device.h>
     69 #include <sys/buf.h>
     70 
     71 #include <machine/bus.h>
     72 #include <machine/intr.h>
     73 
     74 #include <dev/scsipi/scsi_all.h>
     75 #include <dev/scsipi/scsipi_all.h>
     76 #include <dev/scsipi/scsiconf.h>
     77 
     78 #include <dev/isa/isavar.h>
     79 #include <dev/isa/isadmavar.h>
     80 
     81 #include <dev/ic/ncr5380reg.h>
     82 #include <dev/ic/ncr5380var.h>
     83 #include <dev/ic/ncr53c400reg.h>
     84 
     85 #include <dev/isa/ncavar.h>
     86 
     87 int	nca_isa_find __P((bus_space_tag_t, bus_space_handle_t, bus_size_t,
     88 	    struct nca_isa_probe_data *));
     89 int	nca_isa_match __P((struct device *, struct cfdata *, void *));
     90 void	nca_isa_attach __P((struct device *, struct device *, void *));
     91 int	nca_isa_test __P((bus_space_tag_t, bus_space_handle_t, bus_size_t));
     92 
     93 struct cfattach nca_isa_ca = {
     94 	sizeof(struct nca_isa_softc), nca_isa_match, nca_isa_attach
     95 };
     96 
     97 struct scsipi_device nca_isa_dev = {
     98 	NULL,			/* Use default error handler */
     99 	NULL,			/* have a queue, served by this */
    100 	NULL,			/* have no async handler */
    101 	NULL,			/* Use default 'done' routine */
    102 };
    103 
    104 
    105 /* Supported controller types */
    106 #define MAX_NCA_CONTROLLER	3
    107 #define CTLR_NCR_5380	1
    108 #define	CTLR_NCR_53C400	2
    109 #define CTLR_PAS16	3
    110 
    111 #define NCA_ISA_IOSIZE 16
    112 #define MIN_DMA_LEN 128
    113 
    114 /* Options for disconnect/reselect, DMA, and interrupts. */
    115 #define NCA_NO_DISCONNECT    0xff
    116 #define NCA_NO_PARITY_CHK  0xff00
    117 #define NCA_FORCE_POLLING 0x10000
    118 #define NCA_DISABLE_DMA   0x20000
    119 
    120 
    121 /*
    122  * Initialization and test function used by nca_isa_find()
    123  */
    124 int
    125 nca_isa_test(iot, ioh, reg_offset)
    126 	bus_space_tag_t	iot;
    127 	bus_space_handle_t ioh;
    128 	bus_size_t reg_offset;
    129 {
    130 	/* Reset the SCSI bus. */
    131 	bus_space_write_1(iot, ioh, reg_offset + C80_ICR, SCI_ICMD_RST);
    132 	bus_space_write_1(iot, ioh, reg_offset + C80_ODR, 0);
    133 	/* Hold reset for at least 25 microseconds. */
    134 	delay(500);
    135 	/* Check that status cleared. */
    136 	if (bus_space_read_1(iot, ioh, reg_offset + C80_CSBR) != SCI_BUS_RST) {
    137 #ifdef DEBUG
    138 		printf("nca_isa_find: reset status not cleared [0x%x]\n",
    139 		    bus_space_read_1(iot, ioh, reg_offset+C80_CSBR));
    140 #endif
    141 		bus_space_write_1(iot, ioh, reg_offset+C80_ICR, 0);
    142 		return 0;
    143 	}
    144 	/* Clear reset. */
    145 	bus_space_write_1(iot, ioh, reg_offset + C80_ICR, 0);
    146 	/* Wait a Bus Clear Delay (800 ns + bus free delay 800 ns). */
    147 	delay(16000);
    148 
    149 	/* Read RPI port, resetting parity/interrupt state. */
    150 	bus_space_read_1(iot, ioh, reg_offset + C80_RPIR);
    151 
    152 	/* Test BSR: parity error, interrupt request and busy loss state
    153 	 * should be cleared. */
    154 	if (bus_space_read_1(iot, ioh, reg_offset + C80_BSR) & (SCI_CSR_PERR |
    155 	    SCI_CSR_INT | SCI_CSR_DISC)) {
    156 #ifdef DEBUG
    157 		printf("nca_isa_find: Parity/Interrupt/Busy not cleared [0x%x]\n",
    158 		    bus_space_read_1(iot, ioh, reg_offset+C80_BSR));
    159 #endif
    160 		return 0;
    161 	}
    162 
    163 	/* We must have found one */
    164 	return 1;
    165 }
    166 
    167 
    168 /*
    169  * Look for the board
    170  */
    171 int
    172 nca_isa_find(iot, ioh, max_offset, epd)
    173 	bus_space_tag_t iot;
    174 	bus_space_handle_t ioh;
    175 	bus_size_t max_offset;
    176 	struct nca_isa_probe_data *epd;
    177 {
    178 	/*
    179 	 * We check for the existence of a board by trying to initialize it,
    180 	 * Then sending the commands to reset the SCSI bus.
    181 	 * (Unfortunately, this duplicates code which is already in the MI
    182 	 * driver. Unavoidable as that code is not suited to this task.)
    183 	 * This is largely stolen from FreeBSD.
    184 	 */
    185 
    186 	int 		cont_type;
    187 	bus_size_t	base_offset, reg_offset = 0;
    188 
    189 	/*
    190 	 * Some notes:
    191 	 * In the case of a port-mapped board, we should be pointing
    192 	 * right at the chip registers (if they are there at all).
    193 	 * For a memory-mapped card, we loop through the 16K paragraph,
    194 	 * 8 bytes at a time, until we either find it or run out
    195 	 * of region. This means we will probably be doing things like
    196 	 * trying to write to ROMS, etc. Hopefully, this is not a problem.
    197 	 */
    198 
    199 	for (base_offset = 0; base_offset < max_offset; base_offset += 0x08) {
    200 #ifdef DEBUG
    201 		printf("nca_isa_find: testing offset 0x%x\n", (int)base_offset);
    202 #endif
    203 
    204 		/* See if anything is there */
    205 		if (bus_space_read_1(iot, ioh, base_offset) == 0xff)
    206 			continue;
    207 
    208 		/* Loop around for each board type */
    209 		for (cont_type = 1; cont_type <= MAX_NCA_CONTROLLER; cont_type++) {
    210 			/* Per-controller initialization */
    211 			switch (cont_type) {
    212 			case CTLR_NCR_5380:
    213 				/* No special inits */
    214 				reg_offset = 0;
    215 				break;
    216 			case CTLR_NCR_53C400:
    217 				/* Reset into 5380-compat. mode */
    218 				bus_space_write_1(iot, ioh,
    219 				    base_offset + C400_CSR,
    220 				    C400_CSR_5380_ENABLE);
    221 				reg_offset = C400_5380_REG_OFFSET;
    222 				break;
    223 			case CTLR_PAS16:
    224 				/* Not currently supported */
    225 				reg_offset = 0;
    226 				cont_type = 0;
    227 				continue;
    228 			}
    229 
    230 			/* Initialize controller and bus */
    231 			if (nca_isa_test(iot, ioh, base_offset+reg_offset)) {
    232 				epd->sc_reg_offset = base_offset;
    233 				epd->sc_host_type = cont_type;
    234 				return cont_type;	/* This must be it */
    235 			}
    236 		}
    237 	}
    238 
    239 	/* If we got here, we didn't find one */
    240 	return 0;
    241 }
    242 
    243 
    244 /*
    245  * See if there is anything at the config'd address.
    246  * If so, call the real probe to see what it is.
    247  */
    248 int
    249 nca_isa_match(parent, match, aux)
    250 	struct device *parent;
    251 	struct cfdata *match;
    252 	void *aux;
    253 {
    254 	struct isa_attach_args *ia = aux;
    255 	bus_space_tag_t iot = ia->ia_iot;
    256 	bus_space_tag_t memt = ia->ia_memt;
    257 	bus_space_handle_t ioh;
    258 	struct nca_isa_probe_data epd;
    259 	int rv = 0;
    260 
    261 	/* See if we are looking for a port- or memory-mapped adapter */
    262 	if (ia->ia_iobase != -1) {
    263 		/* Port-mapped card */
    264 		if (bus_space_map(iot, ia->ia_iobase, NCA_ISA_IOSIZE, 0, &ioh))
    265 			return 0;
    266 
    267 		/* See if a 53C80/53C400 is there */
    268 		rv = nca_isa_find(iot, ioh, 0x07, &epd);
    269 
    270 		bus_space_unmap(iot, ioh, NCA_ISA_IOSIZE);
    271 	} else {
    272 		/* Memory-mapped card */
    273 		if (bus_space_map(memt, ia->ia_maddr, 0x4000, 0, &ioh))
    274 			return 0;
    275 
    276 		/* See if a 53C80/53C400 is somewhere in this para. */
    277 		rv = nca_isa_find(memt, ioh, 0x03ff0, &epd);
    278 
    279 		bus_space_unmap(memt, ioh, 0x04000);
    280 	}
    281 
    282 	/* Adjust the attachment args if we found one */
    283 	if (rv) {
    284 		if (ia->ia_iobase != -1) {
    285 			/* Port-mapped */
    286 			ia->ia_iosize = NCA_ISA_IOSIZE;
    287 		} else {
    288 			/* Memory-mapped */
    289 			ia->ia_maddr += epd.sc_reg_offset;
    290 			ia->ia_msize = NCA_ISA_IOSIZE;
    291 			ia->ia_iosize = 0;
    292 		}
    293 	}
    294 
    295 	return rv;
    296 }
    297 
    298 /*
    299  * Attach this instance, and then all the sub-devices
    300  */
    301 void
    302 nca_isa_attach(parent, self, aux)
    303 	struct device *parent, *self;
    304 	void *aux;
    305 {
    306 	struct isa_attach_args *ia = aux;
    307 	struct nca_isa_softc *esc = (void *)self;
    308 	struct ncr5380_softc *sc = &esc->sc_ncr5380;
    309 	bus_space_tag_t iot = ia->ia_iot;
    310 	bus_space_handle_t ioh;
    311 	struct nca_isa_probe_data epd;
    312 	isa_chipset_tag_t ic = ia->ia_ic;
    313 
    314 	printf("\n");
    315 
    316 	if (ia->ia_iobase != -1) {
    317 		iot = ia->ia_iot;
    318 		if (bus_space_map(iot, ia->ia_iobase, NCA_ISA_IOSIZE, 0, &ioh)) {
    319 			printf("%s: can't map i/o space\n",
    320 			    sc->sc_dev.dv_xname);
    321 			return;
    322 		}
    323 	} else {
    324 		iot = ia->ia_memt;
    325 		if (bus_space_map(iot, ia->ia_maddr, NCA_ISA_IOSIZE, 0, &ioh)) {
    326 			printf("%s: can't map mem space\n",
    327 			    sc->sc_dev.dv_xname);
    328 			return;
    329 		}
    330 	}
    331 
    332 	switch (nca_isa_find(iot, ioh, NCA_ISA_IOSIZE, &epd)) {
    333 	case 0:
    334 		/* Not found- must have gone away */
    335 		printf("%s: nca_isa_find failed\n", sc->sc_dev.dv_xname);
    336 		return;
    337 	case CTLR_NCR_5380:
    338 		printf("%s: NCR 53C80 detected\n", sc->sc_dev.dv_xname);
    339 		sc->sci_r0 = 0;
    340 		sc->sci_r1 = 1;
    341 		sc->sci_r2 = 2;
    342 		sc->sci_r3 = 3;
    343 		sc->sci_r4 = 4;
    344 		sc->sci_r5 = 5;
    345 		sc->sci_r6 = 6;
    346 		sc->sci_r7 = 7;
    347 		break;
    348 	case CTLR_NCR_53C400:
    349 		printf("%s: NCR 53C400 detected\n", sc->sc_dev.dv_xname);
    350 		sc->sci_r0 = C400_5380_REG_OFFSET + 0;
    351 		sc->sci_r1 = C400_5380_REG_OFFSET + 1;
    352 		sc->sci_r2 = C400_5380_REG_OFFSET + 2;
    353 		sc->sci_r3 = C400_5380_REG_OFFSET + 3;
    354 		sc->sci_r4 = C400_5380_REG_OFFSET + 4;
    355 		sc->sci_r5 = C400_5380_REG_OFFSET + 5;
    356 		sc->sci_r6 = C400_5380_REG_OFFSET + 6;
    357 		sc->sci_r7 = C400_5380_REG_OFFSET + 7;
    358 		break;
    359 	case CTLR_PAS16:
    360 		printf("%s: ProAudio Spectrum 16 detected\n", sc->sc_dev.dv_xname);
    361 		break;
    362 	}
    363 
    364 
    365 	/*
    366 	 * MD function pointers used by the MI code.
    367 	 */
    368 	sc->sc_pio_out = ncr5380_pio_out;
    369 	sc->sc_pio_in =  ncr5380_pio_in;
    370 	sc->sc_dma_alloc = NULL;
    371 	sc->sc_dma_free  = NULL;
    372 	sc->sc_dma_setup = NULL;
    373 	sc->sc_dma_start = NULL;
    374 	sc->sc_dma_poll  = NULL;
    375 	sc->sc_dma_eop   = NULL;
    376 	sc->sc_dma_stop  = NULL;
    377 	sc->sc_intr_on   = NULL;
    378 	sc->sc_intr_off  = NULL;
    379 
    380 	if (ia->ia_irq != IRQUNK) {
    381 		esc->sc_ih = isa_intr_establish(ic, ia->ia_irq, IST_EDGE,
    382 				IPL_BIO, (int (*)(void *))ncr5380_intr, esc);
    383 		if (esc->sc_ih == NULL) {
    384 			printf("nca: couldn't establish interrupt\n");
    385 			return;
    386 		}
    387 	} else
    388 		sc->sc_flags |= NCR5380_FORCE_POLLING;
    389 
    390 
    391 	/*
    392 	 * Support the "options" (config file flags).
    393 	 * Disconnect/reselect is a per-target mask.
    394 	 * Interrupts and DMA are per-controller.
    395 	 */
    396 #if 0
    397 	esc->sc_options = 0x00000;	/* no options */
    398 #else
    399 	esc->sc_options = 0x2ffff;	/* all options except force poll */
    400 #endif
    401 
    402 	sc->sc_no_disconnect =
    403 		(esc->sc_options & NCA_NO_DISCONNECT);
    404 	sc->sc_parity_disable =
    405 		(esc->sc_options & NCA_NO_PARITY_CHK) >> 8;
    406 	if (esc->sc_options & NCA_FORCE_POLLING)
    407 		sc->sc_flags |= NCR5380_FORCE_POLLING;
    408 
    409 #if 1	/* XXX - Temporary */
    410 	/* XXX - In case we think DMA is completely broken... */
    411 	if (esc->sc_options & NCA_DISABLE_DMA) {
    412 		/* Override this function pointer. */
    413 		sc->sc_dma_alloc = NULL;
    414 	}
    415 #endif
    416 	sc->sc_min_dma_len = MIN_DMA_LEN;
    417 
    418 	/*
    419 	 * Fill in the adapter.
    420 	 */
    421 	sc->sc_adapter.scsipi_cmd = ncr5380_scsi_cmd;
    422 	sc->sc_adapter.scsipi_minphys = minphys;
    423 
    424 	/*
    425 	 * Fill in the prototype scsi_link.
    426 	 */
    427 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    428 	sc->sc_link.scsipi_scsi.adapter_target = 7;
    429 	sc->sc_link.scsipi_scsi.max_target = 7;
    430 	sc->sc_link.type = BUS_SCSI;
    431 	sc->sc_link.adapter_softc = sc;
    432 	sc->sc_link.adapter = &sc->sc_adapter;
    433 	sc->sc_link.device = &nca_isa_dev;
    434 	sc->sc_link.openings = 1;
    435 
    436 	/*
    437 	 * Initialize fields used by the MI code
    438 	 */
    439 	sc->sc_regt = iot;
    440 	sc->sc_regh = ioh;
    441 
    442 	/*
    443 	 * Allocate DMA handles.
    444 	 */
    445 
    446 	/*
    447 	 *  Initialize nca board itself.
    448 	 */
    449 	ncr5380_init(sc);
    450 	ncr5380_reset_scsibus(sc);
    451 	config_found(&(sc->sc_dev), &(sc->sc_link), scsiprint);
    452 }
    453