Home | History | Annotate | Line # | Download | only in dev
si_obio.c revision 1.29
      1  1.29       chs /*	$NetBSD: si_obio.c,v 1.29 2005/01/22 15:36:10 chs Exp $	*/
      2   1.1       gwr 
      3   1.7       gwr /*-
      4   1.7       gwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5   1.1       gwr  * All rights reserved.
      6   1.1       gwr  *
      7   1.7       gwr  * This code is derived from software contributed to The NetBSD Foundation
      8   1.7       gwr  * by Adam Glass, David Jones, and Gordon W. Ross.
      9   1.7       gwr  *
     10   1.1       gwr  * Redistribution and use in source and binary forms, with or without
     11   1.1       gwr  * modification, are permitted provided that the following conditions
     12   1.1       gwr  * are met:
     13   1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     14   1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     15   1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       gwr  *    documentation and/or other materials provided with the distribution.
     18   1.7       gwr  * 3. All advertising materials mentioning features or use of this software
     19   1.1       gwr  *    must display the following acknowledgement:
     20   1.7       gwr  *        This product includes software developed by the NetBSD
     21   1.7       gwr  *        Foundation, Inc. and its contributors.
     22   1.7       gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.7       gwr  *    contributors may be used to endorse or promote products derived
     24   1.7       gwr  *    from this software without specific prior written permission.
     25   1.1       gwr  *
     26   1.7       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.7       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.7       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.9       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.9       gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.7       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.7       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.7       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.7       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.7       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.7       gwr  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1       gwr  */
     38   1.1       gwr 
     39   1.1       gwr /*
     40   1.1       gwr  * This file contains only the machine-dependent parts of the
     41   1.1       gwr  * Sun3 SCSI driver.  (Autoconfig stuff and DMA functions.)
     42   1.1       gwr  * The machine-independent parts are in ncr5380sbc.c
     43   1.1       gwr  *
     44   1.1       gwr  * Supported hardware includes:
     45   1.1       gwr  * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60)
     46   1.1       gwr  * Sun SCSI-3 on VME (Sun3/160,Sun3/260)
     47   1.1       gwr  *
     48   1.1       gwr  * Could be made to support the Sun3/E if someone wanted to.
     49   1.1       gwr  *
     50   1.1       gwr  * Note:  Both supported variants of the Sun SCSI-3 adapter have
     51   1.1       gwr  * some really unusual "features" for this driver to deal with,
     52   1.1       gwr  * generally related to the DMA engine.  The OBIO variant will
     53   1.1       gwr  * ignore any attempt to write the FIFO count register while the
     54   1.1       gwr  * SCSI bus is in DATA_IN or DATA_OUT phase.  This is dealt with
     55   1.1       gwr  * by setting the FIFO count early in COMMAND or MSG_IN phase.
     56   1.1       gwr  *
     57   1.1       gwr  * The VME variant has a bit to enable or disable the DMA engine,
     58   1.1       gwr  * but that bit also gates the interrupt line from the NCR5380!
     59   1.1       gwr  * Therefore, in order to get any interrupt from the 5380, (i.e.
     60   1.1       gwr  * for reselect) one must clear the DMA engine transfer count and
     61   1.1       gwr  * then enable DMA.  This has the further complication that you
     62   1.1       gwr  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
     63   1.1       gwr  * we have to turn DMA back off before we even look at the 5380.
     64   1.1       gwr  *
     65   1.1       gwr  * What wonderfully whacky hardware this is!
     66   1.1       gwr  *
     67   1.1       gwr  * Credits, history:
     68   1.1       gwr  *
     69   1.1       gwr  * David Jones wrote the initial version of this module, which
     70   1.1       gwr  * included support for the VME adapter only. (no reselection).
     71   1.1       gwr  *
     72   1.1       gwr  * Gordon Ross added support for the OBIO adapter, and re-worked
     73   1.1       gwr  * both the VME and OBIO code to support disconnect/reselect.
     74   1.1       gwr  * (Required figuring out the hardware "features" noted above.)
     75   1.1       gwr  *
     76   1.1       gwr  * The autoconfiguration boilerplate came from Adam Glass.
     77   1.1       gwr  */
     78   1.1       gwr 
     79   1.1       gwr /*****************************************************************
     80   1.1       gwr  * OBIO functions for DMA
     81   1.1       gwr  ****************************************************************/
     82  1.27     lukem 
     83  1.27     lukem #include <sys/cdefs.h>
     84  1.29       chs __KERNEL_RCSID(0, "$NetBSD: si_obio.c,v 1.29 2005/01/22 15:36:10 chs Exp $");
     85   1.1       gwr 
     86   1.1       gwr #include <sys/param.h>
     87   1.1       gwr #include <sys/systm.h>
     88   1.1       gwr #include <sys/errno.h>
     89   1.1       gwr #include <sys/kernel.h>
     90   1.1       gwr #include <sys/malloc.h>
     91   1.1       gwr #include <sys/device.h>
     92   1.1       gwr #include <sys/buf.h>
     93   1.1       gwr #include <sys/proc.h>
     94   1.1       gwr #include <sys/user.h>
     95   1.1       gwr 
     96  1.14    bouyer #include <dev/scsipi/scsi_all.h>
     97  1.14    bouyer #include <dev/scsipi/scsipi_all.h>
     98  1.14    bouyer #include <dev/scsipi/scsipi_debug.h>
     99  1.14    bouyer #include <dev/scsipi/scsiconf.h>
    100   1.1       gwr 
    101   1.1       gwr #include <machine/autoconf.h>
    102   1.1       gwr #include <machine/dvma.h>
    103   1.1       gwr 
    104  1.18       gwr /* #define DEBUG XXX */
    105   1.1       gwr 
    106   1.1       gwr #include <dev/ic/ncr5380reg.h>
    107   1.1       gwr #include <dev/ic/ncr5380var.h>
    108   1.1       gwr 
    109   1.1       gwr #include "sireg.h"
    110   1.1       gwr #include "sivar.h"
    111   1.1       gwr #include "am9516.h"
    112   1.1       gwr 
    113   1.1       gwr /*
    114   1.1       gwr  * How many uS. to delay after touching the am9516 UDC.
    115   1.1       gwr  */
    116   1.1       gwr #define UDC_WAIT_USEC 5
    117   1.1       gwr 
    118  1.29       chs void si_obio_dma_setup(struct ncr5380_softc *);
    119  1.29       chs void si_obio_dma_start(struct ncr5380_softc *);
    120  1.29       chs void si_obio_dma_eop(struct ncr5380_softc *);
    121  1.29       chs void si_obio_dma_stop(struct ncr5380_softc *);
    122  1.29       chs 
    123  1.29       chs static void si_obio_reset(struct ncr5380_softc *);
    124  1.29       chs 
    125  1.29       chs static __inline__ void si_obio_udc_write(volatile struct si_regs *, int, int);
    126  1.29       chs static __inline__ int si_obio_udc_read(volatile struct si_regs *, int);
    127   1.8       gwr 
    128   1.8       gwr 
    129   1.1       gwr /*
    130   1.1       gwr  * New-style autoconfig attachment
    131   1.1       gwr  */
    132   1.1       gwr 
    133  1.29       chs static int	si_obio_match(struct device *, struct cfdata *, void *);
    134  1.29       chs static void	si_obio_attach(struct device *, struct device *, void *);
    135   1.1       gwr 
    136  1.24   thorpej CFATTACH_DECL(si_obio, sizeof(struct si_softc),
    137  1.25   thorpej     si_obio_match, si_obio_attach, NULL, NULL);
    138   1.1       gwr 
    139  1.10       gwr /*
    140  1.11       gwr  * Options for disconnect/reselect, DMA, and interrupts.
    141  1.11       gwr  * By default, allow disconnect/reselect on targets 4-6.
    142  1.11       gwr  * Those are normally tapes that really need it enabled.
    143  1.10       gwr  */
    144  1.16       gwr int si_obio_options = 0x0f;
    145   1.1       gwr 
    146   1.1       gwr 
    147  1.29       chs static int
    148  1.29       chs si_obio_match(struct device *parent, struct cfdata *cf, void *aux)
    149   1.1       gwr {
    150  1.16       gwr 	struct confargs *ca = aux;
    151   1.1       gwr 
    152  1.16       gwr 	/* Make sure something is there... */
    153   1.5       gwr 	if (bus_peek(ca->ca_bustype, ca->ca_paddr + 1, 1) == -1)
    154   1.1       gwr 		return (0);
    155   1.1       gwr 
    156   1.5       gwr 	/* Default interrupt priority. */
    157   1.5       gwr 	if (ca->ca_intpri == -1)
    158   1.5       gwr 		ca->ca_intpri = 2;
    159   1.1       gwr 
    160   1.5       gwr 	return (1);
    161   1.1       gwr }
    162   1.1       gwr 
    163  1.29       chs static void
    164  1.29       chs si_obio_attach(struct device *parent, struct device *self, void *args)
    165   1.1       gwr {
    166   1.1       gwr 	struct si_softc *sc = (struct si_softc *) self;
    167   1.1       gwr 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    168   1.1       gwr 	struct cfdata *cf = self->dv_cfdata;
    169   1.1       gwr 	struct confargs *ca = args;
    170   1.1       gwr 
    171  1.11       gwr 	/* Get options from config flags if specified. */
    172  1.11       gwr 	if (cf->cf_flags)
    173  1.11       gwr 		sc->sc_options = cf->cf_flags;
    174  1.11       gwr 	else
    175  1.11       gwr 		sc->sc_options = si_obio_options;
    176  1.11       gwr 
    177  1.11       gwr 	printf(": options=0x%x\n", sc->sc_options);
    178   1.1       gwr 
    179   1.1       gwr 	sc->sc_adapter_type = ca->ca_bustype;
    180  1.19       gwr 	sc->sc_regs = bus_mapin(ca->ca_bustype,
    181  1.19       gwr 		ca->ca_paddr, sizeof(struct si_regs));
    182   1.1       gwr 
    183   1.1       gwr 	/*
    184   1.1       gwr 	 * MD function pointers used by the MI code.
    185   1.1       gwr 	 */
    186   1.1       gwr 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    187   1.1       gwr 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    188   1.1       gwr 	ncr_sc->sc_dma_alloc = si_dma_alloc;
    189   1.1       gwr 	ncr_sc->sc_dma_free  = si_dma_free;
    190   1.1       gwr 	ncr_sc->sc_dma_setup = si_obio_dma_setup;
    191   1.1       gwr 	ncr_sc->sc_dma_start = si_obio_dma_start;
    192   1.2       gwr 	ncr_sc->sc_dma_poll  = si_dma_poll;
    193   1.2       gwr 	ncr_sc->sc_dma_eop   = si_obio_dma_eop;
    194   1.1       gwr 	ncr_sc->sc_dma_stop  = si_obio_dma_stop;
    195   1.1       gwr 	ncr_sc->sc_intr_on   = NULL;
    196   1.1       gwr 	ncr_sc->sc_intr_off  = NULL;
    197   1.1       gwr 
    198   1.1       gwr 	/* Need DVMA-capable memory for the UDC command block. */
    199   1.1       gwr 	sc->sc_dmacmd = dvma_malloc(sizeof (struct udc_table));
    200   1.1       gwr 
    201   1.1       gwr 	/* Attach interrupt handler. */
    202   1.5       gwr 	isr_add_autovect(si_intr, (void *)sc, ca->ca_intpri);
    203   1.1       gwr 
    204  1.16       gwr 	/* Reset the hardware. */
    205  1.16       gwr 	si_obio_reset(ncr_sc);
    206  1.16       gwr 
    207   1.1       gwr 	/* Do the common attach stuff. */
    208   1.1       gwr 	si_attach(sc);
    209   1.1       gwr }
    210   1.1       gwr 
    211  1.16       gwr static void
    212  1.16       gwr si_obio_reset(struct ncr5380_softc *ncr_sc)
    213  1.16       gwr {
    214  1.16       gwr 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    215  1.16       gwr 	volatile struct si_regs *si = sc->sc_regs;
    216  1.16       gwr 
    217  1.16       gwr #ifdef	DEBUG
    218  1.16       gwr 	if (si_debug) {
    219  1.16       gwr 		printf("si_obio_reset\n");
    220  1.16       gwr 	}
    221  1.16       gwr #endif
    222  1.16       gwr 
    223  1.16       gwr 	/*
    224  1.16       gwr 	 * The SCSI3 controller has an 8K FIFO to buffer data between the
    225  1.16       gwr 	 * 5380 and the DMA.  Make sure it starts out empty.
    226  1.16       gwr 	 *
    227  1.16       gwr 	 * The reset bits in the CSR are active low.
    228  1.16       gwr 	 */
    229  1.16       gwr 	si->si_csr = 0;
    230  1.16       gwr 	delay(10);
    231  1.16       gwr 	si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN;
    232  1.16       gwr 	delay(10);
    233  1.16       gwr 	si->fifo_count = 0;
    234  1.16       gwr }
    235   1.1       gwr 
    236  1.29       chs static __inline__ void
    237  1.29       chs si_obio_udc_write(volatile struct si_regs *si, int regnum, int value)
    238   1.1       gwr {
    239   1.1       gwr 	si->udc_addr = regnum;
    240   1.1       gwr 	delay(UDC_WAIT_USEC);
    241   1.1       gwr 	si->udc_data = value;
    242   1.1       gwr 	delay(UDC_WAIT_USEC);
    243   1.1       gwr }
    244   1.1       gwr 
    245  1.29       chs static __inline__ int
    246  1.29       chs si_obio_udc_read(volatile struct si_regs *si, int regnum)
    247   1.1       gwr {
    248   1.1       gwr 	int value;
    249   1.1       gwr 
    250   1.1       gwr 	si->udc_addr = regnum;
    251   1.1       gwr 	delay(UDC_WAIT_USEC);
    252   1.1       gwr 	value = si->udc_data;
    253   1.1       gwr 	delay(UDC_WAIT_USEC);
    254   1.1       gwr 
    255   1.1       gwr 	return (value);
    256   1.1       gwr }
    257   1.1       gwr 
    258   1.1       gwr 
    259   1.1       gwr /*
    260   1.1       gwr  * This function is called during the COMMAND or MSG_IN phase
    261  1.22       wiz  * that precedes a DATA_IN or DATA_OUT phase, in case we need
    262   1.1       gwr  * to setup the DMA engine before the bus enters a DATA phase.
    263   1.1       gwr  *
    264   1.1       gwr  * The OBIO "si" IGNORES any attempt to set the FIFO count
    265   1.1       gwr  * register after the SCSI bus goes into any DATA phase, so
    266   1.1       gwr  * this function has to setup the evil FIFO logic.
    267   1.1       gwr  */
    268  1.29       chs void
    269  1.29       chs si_obio_dma_setup(struct ncr5380_softc *ncr_sc)
    270   1.1       gwr {
    271   1.1       gwr 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    272   1.2       gwr 	struct sci_req *sr = ncr_sc->sc_current;
    273   1.2       gwr 	struct si_dma_handle *dh = sr->sr_dma_hand;
    274   1.1       gwr 	volatile struct si_regs *si = sc->sc_regs;
    275   1.2       gwr 	struct udc_table *cmd;
    276   1.2       gwr 	long data_pa, cmd_pa;
    277   1.2       gwr 	int xlen;
    278   1.2       gwr 
    279   1.2       gwr 	/*
    280   1.2       gwr 	 * Get the DVMA mapping for this segment.
    281   1.2       gwr 	 * XXX - Should separate allocation and mapin.
    282   1.2       gwr 	 */
    283   1.2       gwr 	data_pa = dvma_kvtopa(dh->dh_dvma, sc->sc_adapter_type);
    284   1.2       gwr 	data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
    285   1.2       gwr 	if (data_pa & 1)
    286  1.13      fair 		panic("si_dma_start: bad pa=0x%lx", data_pa);
    287   1.2       gwr 	xlen = ncr_sc->sc_datalen;
    288   1.2       gwr 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
    289   1.1       gwr 
    290   1.1       gwr #ifdef	DEBUG
    291   1.2       gwr 	if (si_debug & 2) {
    292  1.13      fair 		printf("si_dma_setup: dh=%p, pa=0x%lx, xlen=0x%x\n",
    293   1.2       gwr 			   dh, data_pa, xlen);
    294   1.1       gwr 	}
    295   1.1       gwr #endif
    296   1.1       gwr 
    297   1.1       gwr 	/* Reset the UDC. (In case not already reset?) */
    298   1.1       gwr 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
    299   1.1       gwr 
    300   1.1       gwr 	/* Reset the FIFO */
    301   1.1       gwr 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
    302   1.1       gwr 	si->si_csr |= SI_CSR_FIFO_RES;
    303   1.1       gwr 
    304   1.1       gwr 	/* Set direction (send/recv) */
    305   1.2       gwr 	if (dh->dh_flags & SIDH_OUT) {
    306   1.1       gwr 		si->si_csr |= SI_CSR_SEND;
    307   1.1       gwr 	} else {
    308   1.1       gwr 		si->si_csr &= ~SI_CSR_SEND;
    309   1.1       gwr 	}
    310   1.1       gwr 
    311   1.1       gwr 	/* Set the FIFO counter. */
    312   1.1       gwr 	si->fifo_count = xlen;
    313   1.1       gwr 
    314   1.2       gwr 	/* Reset the UDC. */
    315   1.2       gwr 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
    316   1.2       gwr 
    317   1.1       gwr 	/*
    318   1.2       gwr 	 * XXX: Reset the FIFO again!  Comment from Sprite:
    319  1.28       wiz 	 * Go through reset again because of the bug on the 3/50
    320   1.1       gwr 	 * where bytes occasionally linger in the DMA fifo.
    321   1.1       gwr 	 */
    322   1.1       gwr 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
    323   1.1       gwr 	si->si_csr |= SI_CSR_FIFO_RES;
    324   1.1       gwr 
    325   1.1       gwr #ifdef	DEBUG
    326   1.2       gwr 	/* Make sure the extra FIFO reset did not hit the count. */
    327   1.2       gwr 	if (si->fifo_count != xlen) {
    328   1.4  christos 		printf("si_dma_setup: fifo_count=0x%x, xlen=0x%x\n",
    329   1.1       gwr 			   si->fifo_count, xlen);
    330   1.1       gwr 		Debugger();
    331   1.1       gwr 	}
    332   1.1       gwr #endif
    333   1.1       gwr 
    334   1.1       gwr 	/*
    335   1.2       gwr 	 * Set up the DMA controller.  The DMA controller on
    336   1.2       gwr 	 * OBIO needs a command block in DVMA space.
    337   1.1       gwr 	 */
    338   1.1       gwr 	cmd = sc->sc_dmacmd;
    339   1.1       gwr 	cmd->addrh = ((data_pa & 0xFF0000) >> 8) | UDC_ADDR_INFO;
    340   1.1       gwr 	cmd->addrl = data_pa & 0xFFFF;
    341   1.1       gwr 	cmd->count = xlen / 2;	/* bytes -> words */
    342   1.1       gwr 	cmd->cmrh = UDC_CMR_HIGH;
    343   1.1       gwr 	if (dh->dh_flags & SIDH_OUT) {
    344   1.2       gwr 		if (xlen & 1)
    345   1.2       gwr 			cmd->count++;
    346   1.1       gwr 		cmd->cmrl = UDC_CMR_LSEND;
    347   1.1       gwr 		cmd->rsel = UDC_RSEL_SEND;
    348   1.1       gwr 	} else {
    349   1.1       gwr 		cmd->cmrl = UDC_CMR_LRECV;
    350   1.1       gwr 		cmd->rsel = UDC_RSEL_RECV;
    351   1.1       gwr 	}
    352   1.1       gwr 
    353   1.1       gwr 	/* Tell the DMA chip where the control block is. */
    354  1.17       gwr 	cmd_pa = dvma_kvtopa(cmd, BUS_OBIO);
    355   1.1       gwr 	si_obio_udc_write(si, UDC_ADR_CAR_HIGH,
    356   1.1       gwr 					  (cmd_pa & 0xff0000) >> 8);
    357   1.1       gwr 	si_obio_udc_write(si, UDC_ADR_CAR_LOW,
    358   1.1       gwr 					  (cmd_pa & 0xffff));
    359   1.1       gwr 
    360   1.1       gwr 	/* Tell the chip to be a DMA master. */
    361   1.1       gwr 	si_obio_udc_write(si, UDC_ADR_MODE, UDC_MODE);
    362   1.1       gwr 
    363   1.1       gwr 	/* Tell the chip to interrupt on error. */
    364   1.1       gwr 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_CIE);
    365   1.1       gwr 
    366   1.2       gwr 	/* Will do "start chain" command in _dma_start. */
    367   1.2       gwr }
    368   1.2       gwr 
    369   1.2       gwr 
    370  1.29       chs void
    371  1.29       chs si_obio_dma_start(struct ncr5380_softc *ncr_sc)
    372   1.2       gwr {
    373   1.2       gwr 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    374   1.2       gwr 	struct sci_req *sr = ncr_sc->sc_current;
    375   1.2       gwr 	struct si_dma_handle *dh = sr->sr_dma_hand;
    376   1.2       gwr 	volatile struct si_regs *si = sc->sc_regs;
    377   1.2       gwr 	int s;
    378   1.2       gwr 
    379   1.2       gwr #ifdef	DEBUG
    380   1.2       gwr 	if (si_debug & 2) {
    381   1.8       gwr 		printf("si_dma_start: sr=%p\n", sr);
    382   1.2       gwr 	}
    383   1.2       gwr #endif
    384   1.2       gwr 
    385   1.2       gwr 	/* This MAY be time critical (not sure). */
    386   1.2       gwr 	s = splhigh();
    387   1.1       gwr 
    388   1.1       gwr 	/* Finally, give the UDC a "start chain" command. */
    389   1.1       gwr 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_STRT_CHN);
    390   1.1       gwr 
    391   1.1       gwr 	/*
    392   1.1       gwr 	 * Acknowledge the phase change.  (After DMA setup!)
    393   1.1       gwr 	 * Put the SBIC into DMA mode, and start the transfer.
    394   1.1       gwr 	 */
    395   1.1       gwr 	if (dh->dh_flags & SIDH_OUT) {
    396   1.1       gwr 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
    397   1.1       gwr 		SCI_CLR_INTR(ncr_sc);
    398   1.1       gwr 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
    399   1.1       gwr 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    400   1.1       gwr 		*ncr_sc->sci_dma_send = 0;	/* start it */
    401   1.1       gwr 	} else {
    402   1.1       gwr 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
    403   1.1       gwr 		SCI_CLR_INTR(ncr_sc);
    404   1.1       gwr 		*ncr_sc->sci_icmd = 0;
    405   1.1       gwr 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    406   1.1       gwr 		*ncr_sc->sci_irecv = 0;	/* start it */
    407   1.1       gwr 	}
    408   1.1       gwr 
    409   1.2       gwr 	splx(s);
    410   1.1       gwr 	ncr_sc->sc_state |= NCR_DOINGDMA;
    411   1.1       gwr 
    412   1.1       gwr #ifdef	DEBUG
    413   1.1       gwr 	if (si_debug & 2) {
    414   1.4  christos 		printf("si_dma_start: started, flags=0x%x\n",
    415   1.1       gwr 			   ncr_sc->sc_state);
    416   1.1       gwr 	}
    417   1.1       gwr #endif
    418   1.1       gwr }
    419   1.1       gwr 
    420   1.1       gwr 
    421  1.29       chs void
    422  1.29       chs si_obio_dma_eop(struct ncr5380_softc *ncr_sc)
    423   1.1       gwr {
    424   1.1       gwr 
    425   1.1       gwr 	/* Not needed - DMA was stopped prior to examining sci_csr */
    426   1.1       gwr }
    427   1.1       gwr 
    428   1.1       gwr 
    429  1.29       chs void
    430  1.29       chs si_obio_dma_stop(struct ncr5380_softc *ncr_sc)
    431   1.1       gwr {
    432   1.1       gwr 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    433   1.1       gwr 	struct sci_req *sr = ncr_sc->sc_current;
    434   1.1       gwr 	struct si_dma_handle *dh = sr->sr_dma_hand;
    435   1.1       gwr 	volatile struct si_regs *si = sc->sc_regs;
    436   1.1       gwr 	int resid, ntrans, tmo, udc_cnt;
    437   1.1       gwr 
    438   1.1       gwr 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    439   1.1       gwr #ifdef	DEBUG
    440  1.26       wiz 		printf("si_dma_stop: DMA not running\n");
    441   1.1       gwr #endif
    442   1.1       gwr 		return;
    443   1.1       gwr 	}
    444   1.1       gwr 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    445   1.1       gwr 
    446   1.2       gwr 	NCR_TRACE("si_dma_stop: top, csr=0x%x\n", si->si_csr);
    447   1.2       gwr 
    448   1.2       gwr 	/* OK, have either phase mis-match or end of DMA. */
    449   1.2       gwr 	/* Set an impossible phase to prevent data movement? */
    450   1.2       gwr 	*ncr_sc->sci_tcmd = PHASE_INVALID;
    451   1.2       gwr 
    452   1.2       gwr 	/* Check for DMA errors. */
    453   1.1       gwr 	if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
    454   1.4  christos 		printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
    455   1.1       gwr 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
    456   1.1       gwr 		ncr_sc->sc_state |= NCR_ABORTING;
    457  1.16       gwr 		si_obio_reset(ncr_sc);
    458   1.2       gwr 		goto out;
    459   1.1       gwr 	}
    460   1.1       gwr 
    461   1.1       gwr 	/* Note that timeout may have set the error flag. */
    462   1.1       gwr 	if (ncr_sc->sc_state & NCR_ABORTING)
    463   1.1       gwr 		goto out;
    464   1.1       gwr 
    465   1.1       gwr 	/*
    466   1.1       gwr 	 * After a read, wait for the FIFO to empty.
    467   1.1       gwr 	 * Note: this only works on the OBIO version.
    468   1.1       gwr 	 */
    469   1.1       gwr 	if ((dh->dh_flags & SIDH_OUT) == 0) {
    470   1.1       gwr 		tmo = 200000;	/* X10 = 2 sec. */
    471   1.1       gwr 		for (;;) {
    472   1.1       gwr 			if (si->si_csr & SI_CSR_FIFO_EMPTY)
    473   1.1       gwr 				break;
    474   1.1       gwr 			if (--tmo <= 0) {
    475  1.26       wiz 				printf("si: DMA FIFO did not empty, reset\n");
    476   1.1       gwr 				ncr_sc->sc_state |= NCR_ABORTING;
    477  1.16       gwr 				/* si_obio_reset(ncr_sc); */
    478   1.1       gwr 				goto out;
    479   1.1       gwr 			}
    480   1.1       gwr 			delay(10);
    481   1.1       gwr 		}
    482   1.1       gwr 	}
    483   1.1       gwr 
    484   1.1       gwr 	/*
    485  1.15       gwr 	 * Now try to figure out how much actually transferred.
    486   1.1       gwr 	 * The fifo_count might not reflect how many bytes were
    487   1.2       gwr 	 * actually transferred.
    488   1.1       gwr 	 */
    489   1.1       gwr 	resid = si->fifo_count & 0xFFFF;
    490   1.1       gwr 	ntrans = sc->sc_reqlen - resid;
    491   1.1       gwr 
    492   1.1       gwr #ifdef	DEBUG
    493   1.1       gwr 	if (si_debug & 2) {
    494   1.4  christos 		printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
    495   1.1       gwr 		       resid, ntrans);
    496   1.1       gwr 	}
    497   1.1       gwr #endif
    498   1.1       gwr 
    499   1.1       gwr 	/* XXX: Treat (ntrans==0) as a special, non-error case? */
    500   1.1       gwr 	if (ntrans < MIN_DMA_LEN) {
    501   1.4  christos 		printf("si: fifo count: 0x%x\n", resid);
    502   1.1       gwr 		ncr_sc->sc_state |= NCR_ABORTING;
    503   1.1       gwr 		goto out;
    504   1.1       gwr 	}
    505   1.1       gwr 	if (ntrans > ncr_sc->sc_datalen)
    506   1.1       gwr 		panic("si_dma_stop: excess transfer");
    507   1.1       gwr 
    508   1.1       gwr 	/* Adjust data pointer */
    509   1.1       gwr 	ncr_sc->sc_dataptr += ntrans;
    510   1.1       gwr 	ncr_sc->sc_datalen -= ntrans;
    511   1.1       gwr 
    512   1.1       gwr 	/*
    513   1.1       gwr 	 * After a read, we may need to clean-up
    514   1.1       gwr 	 * "Left-over bytes" (yuck!)
    515   1.1       gwr 	 */
    516   1.1       gwr 	if ((dh->dh_flags & SIDH_OUT) == 0) {
    517   1.1       gwr 		/* If odd transfer count, grab last byte by hand. */
    518   1.1       gwr 		if (ntrans & 1) {
    519   1.1       gwr 			NCR_TRACE("si_dma_stop: leftover 1 at 0x%x\n",
    520   1.1       gwr 				(int) ncr_sc->sc_dataptr - 1);
    521   1.1       gwr 			ncr_sc->sc_dataptr[-1] =
    522   1.1       gwr 				(si->fifo_data & 0xff00) >> 8;
    523   1.1       gwr 			goto out;
    524   1.1       gwr 		}
    525   1.1       gwr 		/* UDC might not have transfered the last word. */
    526   1.1       gwr 		udc_cnt = si_obio_udc_read(si, UDC_ADR_COUNT);
    527   1.1       gwr 		if (((udc_cnt * 2) - resid) == 2) {
    528   1.1       gwr 			NCR_TRACE("si_dma_stop: leftover 2 at 0x%x\n",
    529   1.1       gwr 				(int) ncr_sc->sc_dataptr - 2);
    530   1.1       gwr 			ncr_sc->sc_dataptr[-2] =
    531   1.1       gwr 				(si->fifo_data & 0xff00) >> 8;
    532   1.1       gwr 			ncr_sc->sc_dataptr[-1] =
    533   1.1       gwr 				(si->fifo_data & 0x00ff);
    534   1.1       gwr 		}
    535   1.1       gwr 	}
    536   1.1       gwr 
    537   1.1       gwr out:
    538   1.1       gwr 	/* Reset the UDC. */
    539   1.1       gwr 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
    540   1.1       gwr 	si->fifo_count = 0;
    541   1.1       gwr 	si->si_csr &= ~SI_CSR_SEND;
    542   1.1       gwr 
    543   1.2       gwr 	/* Reset the FIFO */
    544   1.2       gwr 	si->si_csr &= ~SI_CSR_FIFO_RES;     /* active low */
    545   1.2       gwr 	si->si_csr |= SI_CSR_FIFO_RES;
    546   1.1       gwr 
    547   1.1       gwr 	/* Put SBIC back in PIO mode. */
    548   1.2       gwr 	/* XXX: set tcmd to PHASE_INVALID? */
    549   1.1       gwr 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
    550   1.1       gwr 	*ncr_sc->sci_icmd = 0;
    551   1.1       gwr }
    552   1.1       gwr 
    553