Home | History | Annotate | Line # | Download | only in boot
scsi.c revision 1.14
      1  1.14   tsutsui /*      $NetBSD: scsi.c,v 1.14 2023/02/12 10:04:56 tsutsui Exp $        */
      2   1.1       dbj /*
      3   1.1       dbj  * Copyright (c) 1994, 1997 Rolf Grossmann
      4   1.1       dbj  * All rights reserved.
      5   1.1       dbj  *
      6   1.1       dbj  * Redistribution and use in source and binary forms, with or without
      7   1.1       dbj  * modification, are permitted provided that the following conditions
      8   1.1       dbj  * are met:
      9   1.1       dbj  * 1. Redistributions of source code must retain the above copyright
     10   1.1       dbj  *    notice, this list of conditions and the following disclaimer.
     11   1.1       dbj  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1       dbj  *    notice, this list of conditions and the following disclaimer in the
     13   1.1       dbj  *    documentation and/or other materials provided with the distribution.
     14   1.1       dbj  * 3. All advertising materials mentioning features or use of this software
     15   1.1       dbj  *    must display the following acknowledgement:
     16   1.1       dbj  *      This product includes software developed by Rolf Grossmann.
     17   1.1       dbj  * 4. The name of the author may not be used to endorse or promote products
     18   1.1       dbj  *    derived from this software without specific prior written permission
     19   1.1       dbj  *
     20   1.1       dbj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.1       dbj  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.1       dbj  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.1       dbj  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.1       dbj  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.1       dbj  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.1       dbj  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.1       dbj  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.1       dbj  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.1       dbj  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1       dbj  */
     31   1.1       dbj 
     32   1.1       dbj #include <sys/param.h>
     33   1.1       dbj #include <next68k/dev/espreg.h>
     34   1.3  jdolecek #include <dev/ic/ncr53c9xreg.h>
     35   1.1       dbj #include <dev/scsipi/scsi_message.h>
     36   1.1       dbj #include <next68k/next68k/nextrom.h>
     37   1.1       dbj #include "scsireg.h"
     38   1.1       dbj #include "dmareg.h"
     39   1.1       dbj #include "scsivar.h"
     40   1.1       dbj 
     41   1.1       dbj #include <lib/libsa/stand.h>
     42   1.1       dbj 
     43  1.13   tsutsui #include "samachdep.h"
     44  1.13   tsutsui 
     45   1.1       dbj struct  scsi_softc scsi_softc, *sc = &scsi_softc;
     46   1.1       dbj char the_dma_buffer[MAX_DMASIZE+DMA_ENDALIGNMENT], *dma_buffer;
     47   1.1       dbj 
     48   1.1       dbj int scsi_msgin(void);
     49   1.1       dbj int dma_start(char *addr, int len);
     50   1.1       dbj int dma_done(void);
     51   1.1       dbj 
     52   1.2       dbj void scsierror(char *error);
     53   1.9        he short scsi_getbyte(volatile uint8_t *sr);
     54   1.2       dbj int scsi_wait_for_intr(void);
     55   1.2       dbj 
     56   1.4  christos #define NDPRINTF(x)
     57   1.4  christos #define PRINTF(x)
     58   1.4  christos /* printf x; */
     59  1.12   tsutsui #ifdef SCSI_DEBUG
     60   1.1       dbj #define DPRINTF(x) printf x;
     61   1.1       dbj #else
     62   1.1       dbj #define DPRINTF(x)
     63   1.1       dbj #endif
     64   1.1       dbj 
     65   1.1       dbj void
     66   1.1       dbj scsi_init(void)
     67   1.1       dbj {
     68   1.9        he     volatile uint8_t *sr;
     69   1.1       dbj     struct dma_dev *dma;
     70   1.1       dbj 
     71   1.1       dbj     sr = P_SCSI;
     72   1.1       dbj     dma = (struct dma_dev *)P_SCSI_CSR;
     73   1.1       dbj 
     74   1.1       dbj     dma_buffer = DMA_ALIGN(char *, the_dma_buffer);
     75  1.11   tsutsui 
     76   1.1       dbj     P_FLOPPY[FLP_CTRL] &= ~FLC_82077_SEL;	/* select SCSI chip */
     77   1.1       dbj 
     78   1.6       wiz     /* first reset DMA */
     79   1.1       dbj     dma->dd_csr        = DMACSR_RESET;
     80   1.1       dbj     DELAY(200);
     81   1.1       dbj     sr[ESP_DCTL]       = ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_RESET;
     82   1.1       dbj     DELAY(10);
     83   1.1       dbj     sr[ESP_DCTL]       = ESPDCTL_20MHZ | ESPDCTL_INTENB;
     84   1.1       dbj     DELAY(10);
     85   1.1       dbj 
     86   1.1       dbj     /* then reset the SCSI chip */
     87   1.3  jdolecek     sr[NCR_CMD]        = NCRCMD_RSTCHIP;
     88   1.3  jdolecek     sr[NCR_CMD]        = NCRCMD_NOP;
     89   1.1       dbj     DELAY(500);
     90   1.1       dbj 
     91   1.1       dbj     /* now reset the SCSI bus */
     92   1.3  jdolecek     sr[NCR_CMD]        = NCRCMD_RSTSCSI;
     93  1.14   tsutsui     /* wait 2 seconds after SCSI bus reset */
     94  1.14   tsutsui     DELAY(2 * 1000 * 1000);
     95  1.11   tsutsui 
     96   1.1       dbj     /* then reset the SCSI chip again and initialize it properly */
     97   1.3  jdolecek     sr[NCR_CMD]        = NCRCMD_RSTCHIP;
     98   1.3  jdolecek     sr[NCR_CMD]        = NCRCMD_NOP;
     99   1.1       dbj     DELAY(500);
    100   1.3  jdolecek     sr[NCR_CFG1]       = NCRCFG1_SLOW | NCRCFG1_BUSID;
    101   1.3  jdolecek     sr[NCR_CFG2]       = 0;
    102   1.3  jdolecek     sr[NCR_CCF]        = 4; /* S5RCLKCONV_FACTOR(20); */
    103   1.3  jdolecek     sr[NCR_TIMEOUT]    = 152; /* S5RSELECT_TIMEOUT(20,250); */
    104   1.3  jdolecek     sr[NCR_SYNCOFF]    = 0;
    105   1.3  jdolecek     sr[NCR_SYNCTP]     = 5;
    106   1.1       dbj    /*
    107   1.1       dbj     sc->sc_intrstatus  = sr->s5r_intrstatus;
    108   1.1       dbj     sc->sc_intrstatus  = sr->s5r_intrstatus;
    109   1.1       dbj     */
    110   1.3  jdolecek     sr[NCR_CFG1]       = NCRCFG1_PARENB | NCRCFG1_BUSID;
    111   1.1       dbj 
    112   1.1       dbj     sc->sc_state       = SCSI_IDLE;
    113   1.1       dbj }
    114   1.1       dbj 
    115   1.1       dbj void
    116   1.1       dbj scsierror(char *error)
    117   1.1       dbj {
    118   1.1       dbj     printf("scsierror: %s.\n", error);
    119   1.1       dbj }
    120   1.1       dbj 
    121   1.1       dbj short
    122   1.9        he scsi_getbyte(volatile uint8_t *sr)
    123   1.1       dbj {
    124  1.11   tsutsui     if ((sr[NCR_FFLAG] & NCRFIFO_FF) == 0)
    125   1.1       dbj     {
    126   1.1       dbj 	printf("getbyte: no data!\n");
    127   1.1       dbj 	return -1;
    128   1.1       dbj     }
    129   1.3  jdolecek     return sr[NCR_FIFO];
    130   1.1       dbj }
    131   1.1       dbj 
    132   1.1       dbj int
    133   1.1       dbj scsi_wait_for_intr(void)
    134   1.1       dbj {
    135   1.1       dbj #define	MON(type, off) (*(type *)((u_int) (mg) + off))
    136   1.1       dbj   volatile int *intrstat = MON(volatile int *,MG_intrstat);
    137   1.4  christos #ifdef SCSI_DEBUG
    138   1.4  christos /*   volatile int *intrmask = MON(volatile int *,MG_intrmask); */
    139   1.4  christos #endif
    140   1.1       dbj     int count;
    141   1.1       dbj 
    142   1.2       dbj     for(count = 0; count < SCSI_TIMEOUT; count++) {
    143   1.4  christos 			NDPRINTF(("  *intrstat = 0x%x\t*intrmask = 0x%x\n",*intrstat,*intrmask));
    144   1.2       dbj 
    145   1.1       dbj 	if (*intrstat & SCSI_INTR)
    146   1.1       dbj 	    return 0;
    147   1.2       dbj 		}
    148   1.1       dbj 
    149   1.1       dbj     printf("scsiicmd: timed out.\n");
    150   1.1       dbj     return -1;
    151   1.1       dbj }
    152   1.1       dbj 
    153   1.1       dbj int
    154   1.1       dbj scsiicmd(char target, char lun,
    155   1.1       dbj 	 u_char *cbuf, int clen,
    156   1.4  christos 	 char *addr, int *len)
    157   1.1       dbj {
    158   1.9        he     volatile uint8_t *sr;
    159   1.1       dbj     int i;
    160   1.1       dbj 
    161   1.1       dbj     DPRINTF(("scsiicmd: [%x, %d] -> %d (%lx, %d)\n",*cbuf, clen,
    162   1.4  christos 	     target, (long)addr, *len));
    163   1.1       dbj     sr = P_SCSI;
    164   1.1       dbj 
    165   1.1       dbj     if (sc->sc_state != SCSI_IDLE) {
    166   1.1       dbj         scsierror("scsiiscmd: bad state");
    167   1.1       dbj 	return EIO;
    168   1.1       dbj     }
    169   1.1       dbj     sc->sc_result = 0;
    170   1.1       dbj 
    171   1.1       dbj     /* select target */
    172   1.3  jdolecek     sr[NCR_CMD]   = NCRCMD_FLUSH;
    173   1.1       dbj     DELAY(10);
    174   1.3  jdolecek     sr[NCR_SELID] = target;
    175   1.3  jdolecek     sr[NCR_FIFO]  = MSG_IDENTIFY(lun, 0);
    176   1.1       dbj     for (i=0; i<clen; i++)
    177   1.3  jdolecek 	sr[NCR_FIFO] = cbuf[i];
    178   1.3  jdolecek     sr[NCR_CMD]   = NCRCMD_SELATN;
    179   1.1       dbj     sc->sc_state  = SCSI_SELECTING;
    180  1.11   tsutsui 
    181   1.1       dbj     while(sc->sc_state != SCSI_DONE) {
    182   1.1       dbj 	if (scsi_wait_for_intr()) /* maybe we'd better use real intrs ? */
    183   1.1       dbj 	    return EIO;
    184   1.1       dbj 
    185   1.1       dbj 	if (sc->sc_state == SCSI_DMA)
    186   1.1       dbj 	{
    187   1.6       wiz 	    /* registers are not valid on DMA intr */
    188   1.1       dbj 	    sc->sc_status = sc->sc_seqstep = sc->sc_intrstatus = 0;
    189   1.6       wiz 	    DPRINTF(("scsiicmd: DMA intr\n"));
    190   1.4  christos 	    sr[ESP_DCTL] = ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD;
    191   1.1       dbj 	}
    192   1.4  christos 
    193   1.4  christos 	/* scsi processing */
    194   1.4  christos 	sc->sc_status     = sr[NCR_STAT];
    195   1.4  christos 	sc->sc_seqstep    = sr[NCR_STEP];
    196   1.4  christos 	sc->sc_intrstatus = sr[NCR_INTR];
    197   1.4  christos     redo:
    198   1.4  christos 	DPRINTF(("scsiicmd: regs[intr=%x, stat=%x, step=%x]\n",
    199   1.4  christos 		 sc->sc_intrstatus, sc->sc_status, sc->sc_seqstep));
    200  1.11   tsutsui 
    201   1.3  jdolecek 	if (sc->sc_intrstatus & NCRINTR_SBR) {
    202   1.1       dbj 	    scsierror("scsi bus reset");
    203   1.1       dbj 	    return EIO;
    204   1.1       dbj 	}
    205  1.11   tsutsui 
    206   1.3  jdolecek 	if ((sc->sc_status & NCRSTAT_GE)
    207   1.3  jdolecek 	    || (sc->sc_intrstatus & NCRINTR_ILL)) {
    208   1.1       dbj 	    scsierror("software error");
    209   1.1       dbj 	    return EIO;
    210   1.1       dbj 	}
    211   1.3  jdolecek 	if (sc->sc_status & NCRSTAT_PE)
    212   1.1       dbj 	{
    213   1.1       dbj 	    scsierror("parity error");
    214   1.1       dbj 	    return EIO;
    215   1.1       dbj 	}
    216   1.1       dbj 
    217   1.1       dbj 	switch(sc->sc_state)
    218   1.1       dbj 	{
    219   1.1       dbj 	  case SCSI_SELECTING:
    220  1.11   tsutsui 	      if (sc->sc_intrstatus & NCRINTR_DIS)
    221   1.1       dbj 	      {
    222   1.1       dbj 		  sc->sc_state = SCSI_IDLE;
    223   1.1       dbj 		  return EUNIT;	/* device not present */
    224   1.1       dbj 	      }
    225  1.11   tsutsui 
    226   1.3  jdolecek #define NCRINTR_DONE (NCRINTR_BS | NCRINTR_FC)
    227   1.3  jdolecek 	      if ((sc->sc_intrstatus & NCRINTR_DONE) != NCRINTR_DONE)
    228   1.1       dbj 	      {
    229   1.1       dbj 		  scsierror("selection failed");
    230   1.1       dbj 		  return EIO;
    231   1.1       dbj 	      }
    232   1.1       dbj 	      sc->sc_state = SCSI_HASBUS;
    233   1.1       dbj 	      break;
    234   1.1       dbj 	  case SCSI_HASBUS:
    235   1.3  jdolecek 	      if (sc->sc_intrstatus & NCRINTR_DIS)
    236   1.1       dbj 	      {
    237   1.1       dbj 		  scsierror("target disconnected");
    238   1.1       dbj 		  return EIO;
    239   1.1       dbj 	      }
    240   1.1       dbj 	      break;
    241   1.1       dbj 	  case SCSI_DMA:
    242   1.3  jdolecek 	      if (sc->sc_intrstatus & NCRINTR_DIS)
    243   1.1       dbj 	      {
    244   1.1       dbj 		  scsierror("target disconnected");
    245   1.1       dbj 		  return EIO;
    246   1.1       dbj 	      }
    247   1.4  christos 	      *len = dma_done();
    248   1.4  christos 	      if (*len < 0) {
    249   1.4  christos 		      *len = 0;
    250   1.4  christos 		      return EIO;
    251   1.4  christos 	      }
    252   1.4  christos 	      /* continue; */
    253   1.4  christos 	      sc->sc_status     = sr[NCR_STAT];
    254   1.4  christos 	      goto redo;
    255   1.4  christos 	      break;
    256   1.1       dbj 	  case SCSI_CLEANUP:
    257   1.3  jdolecek 	      if (sc->sc_intrstatus & NCRINTR_DIS)
    258   1.1       dbj 	      {
    259   1.1       dbj 		  sc->sc_state = SCSI_DONE;
    260   1.1       dbj 		  continue;
    261   1.1       dbj 	      }
    262   1.1       dbj 	      DPRINTF(("hmm ... no disconnect on cleanup?\n"));
    263   1.1       dbj 	      sc->sc_state = SCSI_DONE;	/* maybe ... */
    264   1.1       dbj 	      break;
    265   1.1       dbj 	}
    266   1.1       dbj 
    267   1.1       dbj 	/* transfer information now */
    268   1.3  jdolecek 	switch(sc->sc_status & NCRSTAT_PHASE)
    269   1.1       dbj 	{
    270   1.1       dbj 	  case DATA_IN_PHASE:
    271   1.4  christos 		  sr[NCR_CMD] = NCRCMD_FLUSH;
    272   1.4  christos 	      if (dma_start(addr, *len) != 0)
    273   1.1       dbj 		  return EIO;
    274   1.1       dbj 	      break;
    275   1.1       dbj 	  case DATA_OUT_PHASE:
    276   1.1       dbj 	      scsierror("data out phase not implemented");
    277   1.1       dbj 	      return EIO;
    278   1.1       dbj 	  case STATUS_PHASE:
    279   1.1       dbj 	      DPRINTF(("status phase: "));
    280   1.3  jdolecek 	      sr[NCR_CMD] = NCRCMD_ICCS;
    281   1.1       dbj 	      sc->sc_result = scsi_getbyte(sr);
    282   1.1       dbj 	      DPRINTF(("status is 0x%x.\n", sc->sc_result));
    283   1.1       dbj 	      break;
    284   1.1       dbj 	  case MSG_IN_PHASE:
    285   1.4  christos 		if ((sc->sc_intrstatus & NCRINTR_BS) != 0) {
    286   1.4  christos 			sr[NCR_CMD] = NCRCMD_FLUSH;
    287   1.4  christos 			sr[NCR_CMD] = NCRCMD_TRANS;
    288   1.4  christos 		} else
    289   1.4  christos 			if (scsi_msgin() != 0)
    290   1.4  christos 				return EIO;
    291   1.4  christos 		break;
    292   1.1       dbj 	  default:
    293   1.1       dbj 	      DPRINTF(("phase not implemented: 0x%x.\n",
    294   1.3  jdolecek 		      sc->sc_status & NCRSTAT_PHASE));
    295   1.1       dbj               scsierror("bad phase");
    296   1.1       dbj 	      return EIO;
    297   1.1       dbj 	}
    298   1.1       dbj     }
    299   1.1       dbj 
    300   1.1       dbj     sc->sc_state = SCSI_IDLE;
    301   1.1       dbj     return -sc->sc_result;
    302   1.1       dbj }
    303  1.11   tsutsui 
    304   1.1       dbj int
    305   1.1       dbj scsi_msgin(void)
    306   1.1       dbj {
    307   1.9        he     volatile uint8_t *sr;
    308   1.1       dbj     u_char msg;
    309   1.1       dbj 
    310   1.1       dbj     sr = P_SCSI;
    311   1.1       dbj 
    312   1.1       dbj     msg = scsi_getbyte(sr);
    313   1.1       dbj     if (msg)
    314   1.1       dbj     {
    315   1.1       dbj 	printf("unexpected msg: 0x%x.\n",msg);
    316   1.1       dbj 	return -1;
    317   1.1       dbj     }
    318   1.3  jdolecek     if ((sc->sc_intrstatus & NCRINTR_FC) == 0)
    319   1.1       dbj     {
    320   1.1       dbj 	printf("not function complete.\n");
    321   1.1       dbj 	return -1;
    322   1.1       dbj     }
    323   1.1       dbj     sc->sc_state = SCSI_CLEANUP;
    324   1.3  jdolecek     sr[NCR_CMD]  = NCRCMD_MSGOK;
    325   1.1       dbj     return 0;
    326   1.1       dbj }
    327   1.1       dbj 
    328   1.1       dbj int
    329   1.1       dbj dma_start(char *addr, int len)
    330   1.1       dbj {
    331   1.9        he     volatile uint8_t *sr;
    332   1.1       dbj     struct dma_dev *dma;
    333  1.11   tsutsui 
    334   1.1       dbj     sr = P_SCSI;
    335   1.1       dbj     dma = (struct dma_dev *)P_SCSI_CSR;
    336  1.11   tsutsui 
    337   1.1       dbj     if (len > MAX_DMASIZE)
    338   1.1       dbj     {
    339   1.6       wiz 	scsierror("DMA too long");
    340   1.1       dbj 	return -1;
    341   1.1       dbj     }
    342   1.1       dbj 
    343   1.1       dbj     if (addr == NULL || len == 0)
    344   1.1       dbj     {
    345   1.1       dbj #if 0 /* I'd take that as an error in my code */
    346   1.6       wiz 	DPRINTF(("hmm ... no DMA requested.\n"));
    347   1.3  jdolecek 	sr[NCR_TCL] = 0;
    348   1.3  jdolecek 	sr[NCR_TCM] = 1;
    349   1.3  jdolecek 	sr[NCR_CMD] = NCRCMD_NOP;
    350   1.3  jdolecek 	sr[NCR_CMD] = NCRCMD_DMA | NCRCMD_TRPAD;
    351   1.1       dbj 	return 0;
    352   1.1       dbj #else
    353   1.6       wiz 	scsierror("unrequested DMA");
    354   1.1       dbj 	return -1;
    355   1.1       dbj #endif
    356   1.1       dbj     }
    357  1.11   tsutsui 
    358   1.6       wiz     PRINTF(("DMA start: %lx, %d byte.\n", (long)addr, len));
    359   1.2       dbj 
    360  1.11   tsutsui     DPRINTF(("dma_bufffer: start: 0x%lx end: 0x%lx \n",
    361   1.2       dbj 				(long)dma_buffer,(long)DMA_ENDALIGN(char *, dma_buffer+len)));
    362   1.2       dbj 
    363   1.1       dbj     sc->dma_addr = addr;
    364   1.1       dbj     sc->dma_len = len;
    365  1.11   tsutsui 
    366   1.3  jdolecek     sr[NCR_TCL]  = len & 0xff;
    367   1.3  jdolecek     sr[NCR_TCM]  = len >> 8;
    368   1.3  jdolecek     sr[NCR_CMD]  = NCRCMD_DMA | NCRCMD_NOP;
    369   1.3  jdolecek     sr[NCR_CMD]  = NCRCMD_DMA | NCRCMD_TRANS;
    370   1.2       dbj 
    371   1.2       dbj #if 0
    372   1.1       dbj     dma->dd_csr = DMACSR_READ | DMACSR_RESET;
    373   1.1       dbj     dma->dd_next_initbuf = dma_buffer;
    374   1.1       dbj     dma->dd_limit = DMA_ENDALIGN(char *, dma_buffer+len);
    375   1.1       dbj     dma->dd_csr = DMACSR_READ | DMACSR_SETENABLE;
    376   1.2       dbj #else
    377   1.2       dbj     dma->dd_csr = 0;
    378   1.2       dbj     dma->dd_csr = DMACSR_INITBUF | DMACSR_READ | DMACSR_RESET;
    379   1.4  christos     dma->dd_next = dma_buffer;
    380   1.2       dbj     dma->dd_limit = DMA_ENDALIGN(char *, dma_buffer+len);
    381   1.2       dbj     dma->dd_csr = DMACSR_READ | DMACSR_SETENABLE;
    382   1.2       dbj #endif
    383   1.2       dbj 
    384   1.1       dbj     sr[ESP_DCTL] = ESPDCTL_20MHZ|ESPDCTL_INTENB|ESPDCTL_DMAMOD|ESPDCTL_DMARD;
    385   1.1       dbj 
    386   1.1       dbj     sc->sc_state = SCSI_DMA;
    387   1.1       dbj     return 0;
    388   1.1       dbj }
    389   1.1       dbj 
    390   1.1       dbj int
    391   1.1       dbj dma_done(void)
    392   1.1       dbj {
    393   1.9        he     volatile uint8_t *sr;
    394   1.1       dbj     struct dma_dev *dma;
    395   1.4  christos     int resid, state;
    396   1.4  christos     int flushcount = 0;
    397  1.11   tsutsui 
    398   1.1       dbj     sr = P_SCSI;
    399   1.1       dbj     dma = (struct dma_dev *)P_SCSI_CSR;
    400   1.1       dbj 
    401   1.1       dbj     state = dma->dd_csr & (DMACSR_BUSEXC | DMACSR_COMPLETE
    402   1.1       dbj 			   | DMACSR_SUPDATE | DMACSR_ENABLE);
    403   1.1       dbj 
    404   1.4  christos     sr[ESP_DCTL] = ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD;
    405   1.4  christos     resid = sr[NCR_TCM]<<8 | sr[NCR_TCL];
    406   1.6       wiz     DPRINTF(("DMA state = 0x%x, remain = %d.\n", state, resid));
    407   1.4  christos 
    408   1.4  christos     if (!(sr[NCR_FFLAG] & NCRFIFO_FF)) {
    409   1.4  christos 	    sr[ESP_DCTL] = ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD
    410   1.4  christos 		    | ESPDCTL_DMARD;
    411  1.11   tsutsui 	    while (!(state & DMACSR_COMPLETE) && (state & DMACSR_ENABLE) && flushcount < 16)
    412   1.4  christos 	    {
    413  1.11   tsutsui 
    414   1.6       wiz 		    DPRINTF(("DMA still enabled, flushing DCTL.\n"));
    415  1.11   tsutsui 
    416   1.4  christos 		    sr[ESP_DCTL] = ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD
    417   1.4  christos 			    | ESPDCTL_DMARD | ESPDCTL_FLUSH;
    418   1.4  christos 		    sr[ESP_DCTL] = ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD
    419   1.4  christos 			    | ESPDCTL_DMARD;
    420  1.11   tsutsui 
    421   1.4  christos 		    flushcount++;
    422   1.4  christos 		    state = dma->dd_csr & (DMACSR_BUSEXC | DMACSR_COMPLETE
    423   1.4  christos 					   | DMACSR_SUPDATE | DMACSR_ENABLE);
    424   1.4  christos 	    }
    425   1.1       dbj     }
    426   1.4  christos     sr[ESP_DCTL] = ESPDCTL_20MHZ | ESPDCTL_INTENB;
    427   1.4  christos     resid = (sr[NCR_TCM]<<8) + sr[NCR_TCL];
    428   1.1       dbj 
    429   1.4  christos     dma->dd_csr = DMACSR_CLRCOMPLETE | DMACSR_RESET;
    430   1.1       dbj 
    431   1.6       wiz     DPRINTF(("DMA done. remain = %d, state = 0x%x, fifo = 0x%x.\n", resid, state, sr[NCR_FFLAG] & NCRFIFO_FF));
    432   1.1       dbj 
    433   1.4  christos     if (resid != 0)
    434   1.1       dbj     {
    435   1.5   mycroft #if 1
    436   1.6       wiz       printf("WARNING: unexpected %d characters remain in DMA\n",resid);
    437   1.6       wiz 	scsierror("DMA transfer incomplete");
    438   1.1       dbj 	return -1;
    439   1.1       dbj #endif
    440   1.1       dbj     }
    441   1.1       dbj 
    442   1.1       dbj     if (state & DMACSR_BUSEXC)
    443   1.1       dbj     {
    444   1.5   mycroft #if 0
    445   1.6       wiz 	scsierror("DMA failed");
    446   1.1       dbj 	return -1;
    447   1.5   mycroft #endif
    448   1.1       dbj     }
    449   1.4  christos 
    450   1.4  christos     sc->dma_len -= resid;
    451   1.4  christos     if (sc->dma_len < 0)
    452   1.4  christos 	    sc->dma_len = 0;
    453  1.10        he     memcpy(sc->dma_addr, dma_buffer, sc->dma_len);
    454   1.4  christos     sc->sc_state = SCSI_HASBUS;
    455   1.6       wiz     DPRINTF(("DMA done. got %d.\n", sc->dma_len));
    456   1.4  christos     return sc->dma_len;
    457   1.4  christos 
    458   1.6       wiz     /* scsierror("DMA not completed\n"); */
    459  1.11   tsutsui 
    460   1.4  christos     return 0;
    461   1.1       dbj }
    462