Home | History | Annotate | Line # | Download | only in mca
esp_mca.c revision 1.17
      1 /*	$NetBSD: esp_mca.c,v 1.17 2008/04/08 20:41:00 cegger Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jaromir Dolecek <jdolecek (at) NetBSD.org>.
      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  * Driver for NCR 53c90, MCA version, with 86c01 DMA controller chip.
     41  *
     42  * Some of the information used to write this driver was taken
     43  * from Tymm Twillman <tymm (at) computer.org>'s Linux MCA NC53c90 driver,
     44  * in drivers/scsi/mca_53c9x.c
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: esp_mca.c,v 1.17 2008/04/08 20:41:00 cegger Exp $");
     49 
     50 #include <sys/types.h>
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/errno.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/device.h>
     57 #include <sys/buf.h>
     58 #include <sys/proc.h>
     59 #include <sys/user.h>
     60 #include <sys/queue.h>
     61 
     62 #include <dev/scsipi/scsi_all.h>
     63 #include <dev/scsipi/scsipi_all.h>
     64 #include <dev/scsipi/scsiconf.h>
     65 #include <dev/scsipi/scsi_message.h>
     66 
     67 #include <sys/bus.h>
     68 #include <sys/cpu.h>
     69 
     70 #include <dev/ic/ncr53c9xreg.h>
     71 #include <dev/ic/ncr53c9xvar.h>
     72 
     73 #include <dev/mca/espvar.h>
     74 #include <dev/mca/espreg.h>
     75 
     76 #include <dev/mca/mcavar.h>
     77 #include <dev/mca/mcareg.h>
     78 #include <dev/mca/mcadevs.h>
     79 
     80 #if 0
     81 #if defined(DEBUG) && !defined(NCR53C9X_DEBUG)
     82 #define NCR53C9X_DEBUG
     83 #endif
     84 #endif
     85 
     86 #ifdef NCR53C9X_DEBUG
     87 static int esp_mca_debug = 0;
     88 #define DPRINTF(x) if (esp_mca_debug) printf x;
     89 #else
     90 #define DPRINTF(x)
     91 #endif
     92 
     93 #define ESP_MCA_IOSIZE  0x20
     94 #define ESP_REG_OFFSET	0x10
     95 
     96 static void	esp_mca_attach(struct device *, struct device *, void *);
     97 static int	esp_mca_match(struct device *, struct cfdata *, void *);
     98 
     99 CFATTACH_DECL(esp_mca, sizeof(struct esp_softc),
    100     esp_mca_match, esp_mca_attach, NULL, NULL);
    101 
    102 /*
    103  * Functions and the switch for the MI code.
    104  */
    105 static u_char	esp_read_reg(struct ncr53c9x_softc *, int);
    106 static void	esp_write_reg(struct ncr53c9x_softc *, int, u_char);
    107 static int	esp_dma_isintr(struct ncr53c9x_softc *);
    108 static void	esp_dma_reset(struct ncr53c9x_softc *);
    109 static int	esp_dma_intr(struct ncr53c9x_softc *);
    110 static int	esp_dma_setup(struct ncr53c9x_softc *, void **,
    111 	    size_t *, int, size_t *);
    112 static void	esp_dma_go(struct ncr53c9x_softc *);
    113 static void	esp_dma_stop(struct ncr53c9x_softc *);
    114 static int	esp_dma_isactive(struct ncr53c9x_softc *);
    115 
    116 static struct ncr53c9x_glue esp_glue = {
    117 	esp_read_reg,
    118 	esp_write_reg,
    119 	esp_dma_isintr,
    120 	esp_dma_reset,
    121 	esp_dma_intr,
    122 	esp_dma_setup,
    123 	esp_dma_go,
    124 	esp_dma_stop,
    125 	esp_dma_isactive,
    126 	NULL,			/* gl_clear_latched_intr */
    127 };
    128 
    129 static int
    130 esp_mca_match(
    131 	struct device *parent,
    132 	struct cfdata *cf,
    133 	void *aux
    134 )
    135 {
    136 	struct mca_attach_args *ma = aux;
    137 
    138 	switch (ma->ma_id) {
    139 	case MCA_PRODUCT_NCR53C90:
    140 		return 1;
    141 	}
    142 
    143 	return 0;
    144 }
    145 
    146 static void
    147 esp_mca_attach(
    148 	struct device *parent,
    149 	struct device *self,
    150 	void *aux
    151 )
    152 {
    153 	struct mca_attach_args *ma = aux;
    154 	struct esp_softc *esc = device_private(self);
    155 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    156 	u_int16_t iobase;
    157 	int scsi_id, irq, drq, error;
    158 	bus_space_handle_t ioh;
    159 	int pos2, pos3, pos5;
    160 
    161 	static const u_int16_t ncrmca_iobase[] = {
    162 		0, 0x240, 0x340, 0x400, 0x420, 0x3240, 0x8240, 0xa240
    163 	};
    164 
    165 	/*
    166 	 * NCR SCSI Adapter (ADF 7f4f)
    167 	 *
    168 	 * POS register 2: (adf pos0)
    169 	 *
    170 	 * 7 6 5 4 3 2 1 0
    171 	 *     \_/ \___/ \__ enable: 0=adapter disabled, 1=adapter enabled
    172 	 *      |      \____ I/O base (32B): 001=0x240 010=0x340 011=0x400
    173 	 *      |              100=0x420 101=0x3240 110=0x8240 111=0xa240
    174 	 *       \__________ IRQ: 00=3 01=5 10=7 11=9
    175 	 *
    176 	 * POS register 3: (adf pos1)
    177 	 *
    178 	 * 7 6 5 4 3 2 1 0
    179 	 * 1 1 1 | \_____/
    180 	 *       |       \__ DMA level
    181 	 *        \_________ Fairness: 1=enabled 0=disabled
    182 	 *
    183 	 * POS register 5: (adf pos3)
    184 	 *
    185 	 * 7 6 5 4 3 2 1 0
    186 	 * 1   |     \___/
    187 	 *     |         \__ Static Ram: 0xC8000-0xC87FF + XX*0x4000
    188 	 *      \___________ Host Adapter ID: 1=7 0=6
    189 	 */
    190 
    191 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
    192 	pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
    193 	pos5 = mca_conf_read(ma->ma_mc, ma->ma_slot, 5);
    194 
    195 	iobase = ncrmca_iobase[(pos2 & 0x0e) >> 1];
    196 	irq = 3 + 2*((pos2 & 0x30) >> 4);
    197 	drq = (pos3 & 0x0f);
    198 	scsi_id = 6 + ((pos5 & 0x20) ? 1 : 0);
    199 
    200 	printf(" slot %d irq %d drq %d: NCR SCSI Adapter\n",
    201 		ma->ma_slot + 1, irq, drq);
    202 
    203 	/* Map the 86C01 registers */
    204 	if (bus_space_map(ma->ma_iot, iobase, ESP_MCA_IOSIZE, 0, &ioh)) {
    205 		aprint_error_dev(&sc->sc_dev, "can't map i/o space\n");
    206 		return;
    207 	}
    208 
    209 	esc->sc_iot = ma->ma_iot;
    210 	esc->sc_ioh = ioh;
    211 
    212 	/* Submap the 'esp' registers */
    213 	if (bus_space_subregion(ma->ma_iot, ioh, ESP_REG_OFFSET,
    214 	    ESP_MCA_IOSIZE-ESP_REG_OFFSET, &esc->sc_esp_ioh)) {
    215 		aprint_error_dev(&sc->sc_dev, "can't subregion i/o space\n");
    216 		return;
    217 	}
    218 
    219 	/* Setup DMA map */
    220 	esc->sc_dmat = ma->ma_dmat;
    221 	if ((error = mca_dmamap_create(esc->sc_dmat, MAXPHYS,
    222             BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW | MCABUS_DMA_IOPORT,
    223 	    &esc->sc_xfer, drq)) != 0){
    224                 aprint_error_dev(&sc->sc_dev, "couldn't create DMA map - error %d\n", error);
    225                 return;
    226         }
    227 
    228 	/* MI code glue */
    229 	sc->sc_id = scsi_id;
    230 	sc->sc_freq = 25;		/* MHz */
    231 
    232 	sc->sc_glue = &esp_glue;
    233 
    234 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB; //| NCRCFG1_SLOW;
    235 	/* No point setting sc_cfg[2345], they won't be used */
    236 
    237 	sc->sc_rev = NCR_VARIANT_NCR53C90_86C01;
    238 	sc->sc_minsync = 0;
    239 
    240 	/* max 64KB DMA */
    241 	sc->sc_maxxfer = 64 * 1024;
    242 
    243 	/* Establish interrupt */
    244 	esc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_BIO, ncr53c9x_intr,
    245 			esc);
    246 	if (esc->sc_ih == NULL) {
    247 		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt\n");
    248 		return;
    249 	}
    250 
    251 	/*
    252 	 * Massage the 86C01 chip - setup MCA DMA controller for DMA via
    253 	 * the 86C01 register, and enable 86C01 interrupts.
    254 	 */
    255 	mca_dma_set_ioport(drq, iobase + N86C01_PIO);
    256 
    257 	bus_space_write_1(esc->sc_iot, esc->sc_ioh, N86C01_MODE_ENABLE,
    258 		bus_space_read_1(esc->sc_iot, esc->sc_ioh, N86C01_MODE_ENABLE)
    259 		| N86C01_INTR_ENABLE);
    260 
    261 	/*
    262 	 * Now try to attach all the sub-devices
    263 	 */
    264 	sc->sc_adapter.adapt_minphys = minphys;
    265 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    266 
    267 	/* Do the common parts of attachment. */
    268 	printf("%s", device_xname(&sc->sc_dev));
    269 	ncr53c9x_attach(sc);
    270 }
    271 
    272 /*
    273  * Glue functions.
    274  */
    275 
    276 static u_char
    277 esp_read_reg(sc, reg)
    278 	struct ncr53c9x_softc *sc;
    279 	int reg;
    280 {
    281 	struct esp_softc *esc = (struct esp_softc *)sc;
    282 
    283 	return (bus_space_read_1(esc->sc_iot, esc->sc_esp_ioh, reg));
    284 }
    285 
    286 static void
    287 esp_write_reg(sc, reg, val)
    288 	struct ncr53c9x_softc *sc;
    289 	int reg;
    290 	u_char val;
    291 {
    292 	struct esp_softc *esc = (struct esp_softc *)sc;
    293 
    294 	bus_space_write_1(esc->sc_iot, esc->sc_esp_ioh, reg, val);
    295 }
    296 
    297 static int
    298 esp_dma_isintr(sc)
    299 	struct ncr53c9x_softc *sc;
    300 {
    301 	struct esp_softc *esc = (struct esp_softc *)sc;
    302 
    303 	DPRINTF(("[esp_dma_isintr] "));
    304 	return (bus_space_read_1(esc->sc_iot, esc->sc_ioh,
    305 		N86C01_STATUS) & N86C01_IRQ_PEND);
    306 }
    307 
    308 static void
    309 esp_dma_reset(sc)
    310 	struct ncr53c9x_softc *sc;
    311 {
    312 	struct esp_softc *esc = (struct esp_softc *)sc;
    313 
    314 	DPRINTF(("[esp_dma_reset] "));
    315 
    316 	if (esc->sc_flags & ESP_XFER_LOADED) {
    317 		bus_dmamap_unload(esc->sc_dmat, esc->sc_xfer);
    318 		esc->sc_flags &= ~ESP_XFER_LOADED;
    319 	}
    320 
    321 	if (esc->sc_flags & ESP_XFER_ACTIVE) {
    322 		esc->sc_flags &= ~ESP_XFER_ACTIVE;
    323 		mca_disk_unbusy();
    324 	}
    325 }
    326 
    327 static int
    328 esp_dma_intr(sc)
    329 	struct ncr53c9x_softc *sc;
    330 {
    331 	struct esp_softc *esc = (struct esp_softc *) sc;
    332 	DPRINTF(("[esp_dma_intr] "));
    333 
    334 	if ((esc->sc_flags & ESP_XFER_ACTIVE) == 0) {
    335 		printf("%s: dma_intr--inactive DMA\n", device_xname(&sc->sc_dev));
    336 		return (-1);
    337 	}
    338 
    339 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    340 		esc->sc_flags &= ~ESP_XFER_ACTIVE;
    341 		mca_disk_unbusy();
    342 		return (0);
    343 	}
    344 
    345 	sc->sc_espstat |= NCRSTAT_TC;	/* XXX */
    346 
    347 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    348 		printf("%s: DMA not complete?\n", device_xname(&sc->sc_dev));
    349 		return (1);
    350 	}
    351 
    352 	bus_dmamap_sync(esc->sc_dmat, esc->sc_xfer, 0,
    353 		*esc->sc_xfer_len,
    354 		(esc->sc_flags & ESP_XFER_READ)
    355 			? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    356 
    357 	bus_dmamap_unload(esc->sc_dmat, esc->sc_xfer);
    358 	esc->sc_flags &= ~ESP_XFER_LOADED;
    359 
    360 	*esc->sc_xfer_addr +=  *esc->sc_xfer_len;
    361 	*esc->sc_xfer_len = 0;
    362 
    363 	esc->sc_flags &= ~ESP_XFER_ACTIVE;
    364 	mca_disk_unbusy();
    365 
    366 	return (0);
    367 }
    368 
    369 /*
    370  * Setup DMA transfer.
    371  */
    372 static int
    373 esp_dma_setup(
    374 	struct ncr53c9x_softc *sc,
    375 	void **addr,
    376 	size_t *len,
    377 	int datain,
    378 	size_t *dmasize
    379 )
    380 {
    381 	struct esp_softc *esc = (struct esp_softc *) sc;
    382 	int error;
    383 	int fl;
    384 
    385 	DPRINTF(("[esp_dma_setup] "));
    386 
    387 	if (esc->sc_flags & ESP_XFER_LOADED) {
    388 		aprint_error_dev(&sc->sc_dev, "esp_dma_setup: unloading leaked xfer\n");
    389 		bus_dmamap_unload(esc->sc_dmat, esc->sc_xfer);
    390 		esc->sc_flags &= ~ESP_XFER_LOADED;
    391 	}
    392 
    393 	/* Load the buffer for DMA transfer. */
    394 	fl = (datain) ? BUS_DMA_READ : BUS_DMA_WRITE;
    395 
    396 	if ((error = bus_dmamap_load(esc->sc_dmat, esc->sc_xfer, *addr,
    397 	    *len, NULL, BUS_DMA_STREAMING|fl))) {
    398 		aprint_error_dev(&sc->sc_dev, "esp_dma_setup: unable to load DMA buffer - error %d\n", error);
    399 		return (error);
    400 	}
    401 
    402 	bus_dmamap_sync(esc->sc_dmat, esc->sc_xfer, 0,
    403 		*len, (datain) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    404 
    405 	esc->sc_flags |= ESP_XFER_LOADED | (datain ? ESP_XFER_READ : 0);
    406 	esc->sc_xfer_addr = addr;
    407 	esc->sc_xfer_len  = len;
    408 
    409 	return (0);
    410 }
    411 
    412 static void
    413 esp_dma_go(sc)
    414 	struct ncr53c9x_softc *sc;
    415 {
    416 	struct esp_softc *esc = (struct esp_softc *) sc;
    417 	DPRINTF(("[esp_dma_go] "));
    418 
    419 	esc->sc_flags |= ESP_XFER_ACTIVE;
    420 	mca_disk_busy();
    421 }
    422 
    423 static void
    424 esp_dma_stop(sc)
    425 	struct ncr53c9x_softc *sc;
    426 {
    427 	DPRINTF(("[esp_dma_stop] "));
    428 
    429 	panic("%s: stop not yet implemented", device_xname(&sc->sc_dev));
    430 }
    431 
    432 static int
    433 esp_dma_isactive(sc)
    434 	struct ncr53c9x_softc *sc;
    435 {
    436 	struct esp_softc *esc = (struct esp_softc *) sc;
    437 	DPRINTF(("[esp_dma_isactive] "));
    438 
    439 	return (esc->sc_flags & ESP_XFER_ACTIVE);
    440 }
    441