Home | History | Annotate | Line # | Download | only in dev
sbc.c revision 1.27
      1  1.27    scottr /*	$NetBSD: sbc.c,v 1.27 1997/06/29 06:10:37 scottr Exp $	*/
      2   1.1    scottr 
      3   1.1    scottr /*
      4  1.19    scottr  * Copyright (C) 1996 Scott Reynolds.  All rights reserved.
      5   1.1    scottr  *
      6   1.1    scottr  * Redistribution and use in source and binary forms, with or without
      7   1.1    scottr  * modification, are permitted provided that the following conditions
      8   1.1    scottr  * are met:
      9   1.1    scottr  * 1. Redistributions of source code must retain the above copyright
     10   1.1    scottr  *    notice, this list of conditions and the following disclaimer.
     11   1.1    scottr  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1    scottr  *    notice, this list of conditions and the following disclaimer in the
     13   1.1    scottr  *    documentation and/or other materials provided with the distribution.
     14  1.19    scottr  * 3. All advertising materials mentioning features or use of this software
     15  1.19    scottr  *    must display the following acknowledgement:
     16  1.19    scottr  *      This product includes software developed by Scott Reynolds for
     17  1.19    scottr  *      the NetBSD Project.
     18  1.19    scottr  * 4. The name of the author may not be used to endorse or promote products
     19  1.19    scottr  *    derived from this software without specific prior written permission
     20   1.1    scottr  *
     21  1.19    scottr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1    scottr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1    scottr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.19    scottr  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1    scottr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1    scottr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1    scottr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1    scottr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1    scottr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1    scottr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1    scottr  */
     32   1.1    scottr 
     33   1.1    scottr /*
     34   1.1    scottr  * This file contains only the machine-dependent parts of the mac68k
     35   1.1    scottr  * NCR 5380 SCSI driver.  (Autoconfig stuff and PDMA functions.)
     36   1.1    scottr  * The machine-independent parts are in ncr5380sbc.c
     37   1.1    scottr  *
     38   1.1    scottr  * Supported hardware includes:
     39   1.1    scottr  * Macintosh II family 5380-based controller
     40   1.1    scottr  *
     41   1.1    scottr  * Credits, history:
     42   1.1    scottr  *
     43   1.1    scottr  * Scott Reynolds wrote this module, based on work by Allen Briggs
     44   1.9    scottr  * (mac68k), Gordon W. Ross and David Jones (sun3), and Leo Weppelman
     45   1.9    scottr  * (atari).  Thanks to Allen for supplying crucial interpretation of the
     46   1.9    scottr  * NetBSD/mac68k 1.1 'ncrscsi' driver.  Also, Allen, Gordon, and Jason
     47   1.9    scottr  * Thorpe all helped to refine this code, and were considerable sources
     48   1.9    scottr  * of moral support.
     49   1.1    scottr  */
     50   1.1    scottr 
     51   1.1    scottr #include <sys/types.h>
     52   1.1    scottr #include <sys/param.h>
     53   1.1    scottr #include <sys/systm.h>
     54   1.1    scottr #include <sys/kernel.h>
     55   1.1    scottr #include <sys/errno.h>
     56   1.1    scottr #include <sys/device.h>
     57   1.1    scottr #include <sys/buf.h>
     58   1.1    scottr #include <sys/proc.h>
     59   1.1    scottr #include <sys/user.h>
     60   1.1    scottr 
     61   1.1    scottr #include <scsi/scsi_all.h>
     62   1.1    scottr #include <scsi/scsi_debug.h>
     63   1.1    scottr #include <scsi/scsiconf.h>
     64   1.1    scottr 
     65   1.1    scottr #include <dev/ic/ncr5380reg.h>
     66   1.1    scottr #include <dev/ic/ncr5380var.h>
     67   1.1    scottr 
     68   1.8    scottr #include <machine/cpu.h>
     69   1.1    scottr #include <machine/viareg.h>
     70   1.1    scottr 
     71   1.2    scottr #include "sbcreg.h"
     72  1.22    scottr #include "sbcvar.h"
     73   1.1    scottr 
     74  1.22    scottr int	sbc_debug = 0 /* | SBC_DB_INTR | SBC_DB_DMA */;
     75  1.22    scottr int	sbc_link_flags = 0 /* | SDEV_DB2 */;
     76  1.24    scottr int	sbc_options = 0 /* | SBC_PDMA */;
     77   1.1    scottr 
     78   1.1    scottr static	void	sbc_minphys __P((struct buf *bp));
     79   1.1    scottr 
     80  1.22    scottr struct scsi_adapter	sbc_ops = {
     81   1.1    scottr 	ncr5380_scsi_cmd,		/* scsi_cmd()		*/
     82   1.1    scottr 	sbc_minphys,			/* scsi_minphys()	*/
     83   1.1    scottr 	NULL,				/* open_target_lu()	*/
     84   1.1    scottr 	NULL,				/* close_target_lu()	*/
     85   1.1    scottr };
     86   1.1    scottr 
     87   1.1    scottr /* This is copied from julian's bt driver */
     88   1.1    scottr /* "so we have a default dev struct for our link struct." */
     89  1.22    scottr struct scsi_device sbc_dev = {
     90   1.1    scottr 	NULL,		/* Use default error handler.	    */
     91   1.1    scottr 	NULL,		/* Use default start handler.		*/
     92   1.1    scottr 	NULL,		/* Use default async handler.	    */
     93   1.1    scottr 	NULL,		/* Use default "done" routine.	    */
     94   1.1    scottr };
     95   1.1    scottr 
     96   1.1    scottr struct cfdriver sbc_cd = {
     97   1.1    scottr 	NULL, "sbc", DV_DULL
     98   1.1    scottr };
     99   1.1    scottr 
    100  1.22    scottr static	int	sbc_ready __P((struct ncr5380_softc *));
    101  1.23    scottr static	void	sbc_wait_not_req __P((struct ncr5380_softc *));
    102   1.1    scottr 
    103   1.1    scottr static void
    104   1.1    scottr sbc_minphys(struct buf *bp)
    105   1.1    scottr {
    106   1.1    scottr 	if (bp->b_bcount > MAX_DMA_LEN)
    107   1.1    scottr 		bp->b_bcount = MAX_DMA_LEN;
    108   1.1    scottr 	return (minphys(bp));
    109   1.1    scottr }
    110   1.1    scottr 
    111   1.1    scottr 
    112   1.1    scottr /***
    113   1.1    scottr  * General support for Mac-specific SCSI logic.
    114   1.1    scottr  ***/
    115   1.1    scottr 
    116   1.1    scottr void
    117   1.1    scottr sbc_irq_intr(p)
    118   1.1    scottr 	void *p;
    119   1.1    scottr {
    120  1.23    scottr 	struct ncr5380_softc *ncr_sc = p;
    121  1.23    scottr 	int claimed = 0;
    122   1.1    scottr 
    123   1.1    scottr 	/* How we ever arrive here without IRQ set is a mystery... */
    124   1.1    scottr 	if (*ncr_sc->sci_csr & SCI_CSR_INT) {
    125   1.3    scottr #ifdef SBC_DEBUG
    126   1.3    scottr 		if (sbc_debug & SBC_DB_INTR)
    127   1.3    scottr 			decode_5380_intr(ncr_sc);
    128   1.3    scottr #endif
    129   1.1    scottr 		claimed = ncr5380_intr(ncr_sc);
    130   1.1    scottr 		if (!claimed) {
    131   1.1    scottr 			if (((*ncr_sc->sci_csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT)
    132   1.3    scottr 			    && ((*ncr_sc->sci_bus_csr & ~SCI_BUS_RST) == 0))
    133   1.1    scottr 				SCI_CLR_INTR(ncr_sc);	/* RST interrupt */
    134   1.1    scottr #ifdef SBC_DEBUG
    135   1.1    scottr 			else {
    136  1.13  christos 				printf("%s: spurious intr\n",
    137   1.1    scottr 				    ncr_sc->sc_dev.dv_xname);
    138   1.3    scottr 				SBC_BREAK;
    139   1.1    scottr 			}
    140   1.1    scottr #endif
    141   1.1    scottr 		}
    142   1.1    scottr 	}
    143   1.1    scottr }
    144   1.1    scottr 
    145   1.3    scottr #ifdef SBC_DEBUG
    146   1.3    scottr void
    147   1.3    scottr decode_5380_intr(ncr_sc)
    148   1.3    scottr 	struct ncr5380_softc *ncr_sc;
    149   1.3    scottr {
    150  1.23    scottr 	u_char csr = *ncr_sc->sci_csr;
    151  1.23    scottr 	u_char bus_csr = *ncr_sc->sci_bus_csr;
    152   1.3    scottr 
    153   1.3    scottr 	if (((csr & ~(SCI_CSR_PHASE_MATCH | SCI_CSR_ATN)) == SCI_CSR_INT) &&
    154   1.3    scottr 	    ((bus_csr & ~(SCI_BUS_MSG | SCI_BUS_CD | SCI_BUS_IO | SCI_BUS_DBP)) == SCI_BUS_SEL)) {
    155   1.3    scottr 		if (csr & SCI_BUS_IO)
    156  1.13  christos 			printf("%s: reselect\n", ncr_sc->sc_dev.dv_xname);
    157   1.3    scottr 		else
    158  1.13  christos 			printf("%s: select\n", ncr_sc->sc_dev.dv_xname);
    159   1.3    scottr 	} else if (((csr & ~SCI_CSR_ACK) == (SCI_CSR_DONE | SCI_CSR_INT)) &&
    160   1.3    scottr 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
    161  1.13  christos 		printf("%s: dma eop\n", ncr_sc->sc_dev.dv_xname);
    162   1.3    scottr 	else if (((csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT) &&
    163   1.3    scottr 	    ((bus_csr & ~SCI_BUS_RST) == 0))
    164  1.13  christos 		printf("%s: bus reset\n", ncr_sc->sc_dev.dv_xname);
    165   1.3    scottr 	else if (((csr & ~(SCI_CSR_DREQ | SCI_CSR_ATN | SCI_CSR_ACK)) == (SCI_CSR_PERR | SCI_CSR_INT | SCI_CSR_PHASE_MATCH)) &&
    166   1.3    scottr 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
    167  1.13  christos 		printf("%s: parity error\n", ncr_sc->sc_dev.dv_xname);
    168   1.3    scottr 	else if (((csr & ~SCI_CSR_ATN) == SCI_CSR_INT) &&
    169   1.3    scottr 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_REQ | SCI_BUS_SEL)) == (SCI_BUS_BSY | SCI_BUS_REQ)))
    170  1.13  christos 		printf("%s: phase mismatch\n", ncr_sc->sc_dev.dv_xname);
    171   1.3    scottr 	else if (((csr & ~SCI_CSR_PHASE_MATCH) == (SCI_CSR_INT | SCI_CSR_DISC)) &&
    172   1.3    scottr 	    (bus_csr == 0))
    173  1.13  christos 		printf("%s: disconnect\n", ncr_sc->sc_dev.dv_xname);
    174   1.3    scottr 	else
    175  1.13  christos 		printf("%s: unknown intr: csr=%x, bus_csr=%x\n",
    176   1.3    scottr 		    ncr_sc->sc_dev.dv_xname, csr, bus_csr);
    177   1.3    scottr }
    178   1.3    scottr #endif
    179   1.1    scottr 
    180   1.8    scottr 
    181   1.1    scottr /***
    182   1.1    scottr  * The following code implements polled PDMA.
    183   1.1    scottr  ***/
    184   1.1    scottr 
    185  1.23    scottr #define	TIMEOUT	5000000			/* x 2 usec = 10 sec */
    186  1.23    scottr 
    187  1.23    scottr static __inline__ int
    188  1.23    scottr sbc_ready(sc)
    189  1.23    scottr 	struct ncr5380_softc *sc;
    190   1.1    scottr {
    191  1.23    scottr 	int i = TIMEOUT;
    192  1.23    scottr 
    193  1.23    scottr 	for (;;) {
    194  1.23    scottr 		if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) ==
    195  1.23    scottr 		    (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
    196  1.23    scottr 			return 1;
    197  1.23    scottr 		if (((*sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0) ||
    198  1.23    scottr 		    (SCI_BUSY(sc) == 0))
    199  1.23    scottr 			return 0;
    200  1.23    scottr 		if (--i < 0)
    201  1.23    scottr 			break;
    202  1.23    scottr 		delay(2);
    203  1.23    scottr 	}
    204   1.1    scottr 
    205  1.23    scottr 	printf("%s: ready timeout\n", sc->sc_dev.dv_xname);
    206  1.23    scottr 	return 0;
    207  1.23    scottr }
    208   1.1    scottr 
    209  1.23    scottr static __inline__ void
    210  1.23    scottr sbc_wait_not_req(sc)
    211  1.23    scottr 	struct ncr5380_softc *sc;
    212  1.23    scottr {
    213  1.23    scottr 	int i = TIMEOUT;
    214   1.1    scottr 
    215  1.23    scottr 	for (;;) {
    216  1.23    scottr 		if ((*sc->sci_bus_csr & SCI_BUS_REQ) == 0 ||
    217  1.23    scottr 		    (*sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0 ||
    218  1.23    scottr 		    SCI_BUSY(sc) == 0) {
    219  1.23    scottr 			return;
    220   1.1    scottr 		}
    221  1.23    scottr 		if (--i < 0)
    222  1.23    scottr 			break;
    223  1.23    scottr 		delay(2);
    224  1.23    scottr 	}
    225  1.23    scottr 	printf("%s: pdma not_req timeout\n", sc->sc_dev.dv_xname);
    226  1.23    scottr }
    227   1.1    scottr 
    228  1.23    scottr int
    229  1.23    scottr sbc_pdma_in(ncr_sc, phase, datalen, data)
    230  1.23    scottr 	struct ncr5380_softc *ncr_sc;
    231  1.23    scottr 	int phase, datalen;
    232  1.23    scottr 	u_char *data;
    233  1.23    scottr {
    234  1.23    scottr 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    235  1.24    scottr 	volatile u_int32_t *long_data = (u_int32_t *)sc->sc_drq_addr;
    236  1.24    scottr 	volatile u_int8_t *byte_data = (u_int8_t *)sc->sc_nodrq_addr;
    237  1.23    scottr 	int resid, s;
    238  1.23    scottr 
    239  1.23    scottr 	s = splbio();
    240  1.23    scottr 	*ncr_sc->sci_mode |= SCI_MODE_DMA;
    241  1.23    scottr 	*ncr_sc->sci_irecv = 0;
    242   1.1    scottr 
    243  1.24    scottr #define R4	*((u_int32_t *)data)++ = *long_data++
    244  1.24    scottr #define R1	*data++ = *byte_data++
    245  1.23    scottr 	for (resid = datalen; resid >= 128; resid -= 128) {
    246  1.23    scottr 		if (sbc_ready(ncr_sc) == 0)
    247  1.23    scottr 			goto interrupt;
    248  1.23    scottr 		R4; R4; R4; R4; R4; R4; R4; R4;
    249  1.23    scottr 		R4; R4; R4; R4; R4; R4; R4; R4;
    250  1.23    scottr 		R4; R4; R4; R4; R4; R4; R4; R4;
    251  1.23    scottr 		R4; R4; R4; R4; R4; R4; R4; R4;
    252  1.24    scottr 
    253  1.24    scottr 		long_data = (u_int32_t *)sc->sc_drq_addr;
    254  1.24    scottr 		byte_data = (u_int8_t *)sc->sc_nodrq_addr;
    255  1.23    scottr 	}
    256  1.23    scottr 	while (resid) {
    257  1.23    scottr 		if (sbc_ready(ncr_sc) == 0)
    258  1.23    scottr 			goto interrupt;
    259  1.23    scottr 		R1;
    260  1.23    scottr 		resid--;
    261   1.1    scottr 	}
    262  1.23    scottr #undef R4
    263  1.23    scottr #undef R1
    264   1.1    scottr 
    265  1.23    scottr 	sbc_wait_not_req(ncr_sc);
    266  1.23    scottr interrupt:
    267   1.1    scottr 	SCI_CLR_INTR(ncr_sc);
    268   1.1    scottr 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    269  1.23    scottr 	splx(s);
    270  1.23    scottr 	return datalen - resid;
    271   1.1    scottr }
    272   1.1    scottr 
    273  1.22    scottr int
    274  1.23    scottr sbc_pdma_out(ncr_sc, phase, datalen, data)
    275   1.1    scottr 	struct ncr5380_softc *ncr_sc;
    276  1.23    scottr 	int phase, datalen;
    277   1.1    scottr 	u_char *data;
    278   1.1    scottr {
    279   1.1    scottr 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    280  1.24    scottr 	volatile u_int32_t *long_data = (u_int32_t *)sc->sc_drq_addr;
    281  1.24    scottr 	volatile u_int8_t *byte_data = (u_int8_t *)sc->sc_nodrq_addr;
    282  1.23    scottr 	int i, s, resid;
    283  1.23    scottr 	u_char icmd;
    284  1.23    scottr 
    285  1.25    scottr 	if (datalen < ncr_sc->sc_min_dma_len)
    286  1.24    scottr 		return ncr5380_pio_out(ncr_sc, phase, datalen, data);
    287  1.24    scottr 
    288  1.23    scottr 	s = splbio();
    289  1.23    scottr 	icmd = *(ncr_sc->sci_icmd) & SCI_ICMD_RMASK;
    290  1.23    scottr 	*ncr_sc->sci_icmd = icmd | SCI_ICMD_DATA;
    291  1.23    scottr 	*ncr_sc->sci_mode |= SCI_MODE_DMA;
    292  1.23    scottr 	*ncr_sc->sci_dma_send = 0;
    293  1.23    scottr 
    294  1.23    scottr 	resid = datalen;
    295  1.23    scottr 	if (sbc_ready(ncr_sc) == 0)
    296  1.23    scottr 		goto interrupt;
    297   1.1    scottr 
    298  1.24    scottr #define W1	*byte_data++ = *data++
    299  1.24    scottr #define W4	*long_data++ = *((u_int32_t *)data)++
    300  1.23    scottr 	while (resid >= 64) {
    301  1.23    scottr 		if (sbc_ready(ncr_sc) == 0)
    302  1.23    scottr 			goto interrupt;
    303  1.23    scottr 		W1;
    304  1.23    scottr 		resid--;
    305  1.23    scottr 		if (sbc_ready(ncr_sc) == 0)
    306  1.23    scottr 			goto interrupt;
    307  1.23    scottr 		W1;
    308  1.23    scottr 		resid--;
    309  1.23    scottr 		if (sbc_ready(ncr_sc) == 0)
    310  1.23    scottr 			goto interrupt;
    311  1.23    scottr 		W1;
    312  1.23    scottr 		resid--;
    313  1.23    scottr 		if (sbc_ready(ncr_sc) == 0)
    314  1.23    scottr 			goto interrupt;
    315  1.23    scottr 		W1;
    316  1.23    scottr 		resid--;
    317  1.23    scottr 		if (sbc_ready(ncr_sc) == 0)
    318  1.23    scottr 			goto interrupt;
    319  1.23    scottr 		W4; W4; W4; W4;
    320  1.23    scottr 		W4; W4; W4; W4;
    321  1.23    scottr 		W4; W4; W4; W4;
    322  1.23    scottr 		W4; W4; W4;
    323  1.23    scottr 		resid -= 60;
    324  1.24    scottr 
    325  1.24    scottr 		long_data = (u_int32_t *)sc->sc_drq_addr;
    326  1.24    scottr 		byte_data = (u_int8_t *)sc->sc_nodrq_addr;
    327  1.23    scottr 	}
    328  1.23    scottr 	for (; resid; resid--) {
    329  1.23    scottr 		if (sbc_ready(ncr_sc) == 0)
    330  1.23    scottr 			goto interrupt;
    331  1.23    scottr 		W1;
    332  1.23    scottr 	}
    333  1.23    scottr #undef  W1
    334  1.23    scottr #undef  W4
    335   1.1    scottr 
    336  1.23    scottr 	for (i = TIMEOUT; i > 0; i--) {
    337  1.23    scottr 		if ((*ncr_sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
    338  1.23    scottr 		    != SCI_CSR_DREQ)
    339  1.23    scottr 			break;
    340   1.1    scottr 	}
    341  1.26    scottr 	if (i == 0)
    342  1.26    scottr 		printf("%s: timeout waiting for final SCI_DSR_DREQ.\n",
    343  1.26    scottr 			ncr_sc->sc_dev.dv_xname);
    344  1.25    scottr #ifdef __notyet__	/* not sure why this is ever necessary... */
    345  1.26    scottr 	else
    346  1.23    scottr 		*byte_data = 0;
    347  1.25    scottr #endif
    348   1.1    scottr 
    349  1.23    scottr 	sbc_wait_not_req(ncr_sc);
    350  1.23    scottr interrupt:
    351   1.1    scottr 	SCI_CLR_INTR(ncr_sc);
    352   1.1    scottr 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    353  1.23    scottr 	*ncr_sc->sci_icmd = icmd;
    354  1.23    scottr 	splx(s);
    355  1.23    scottr 	return (datalen - resid);
    356   1.1    scottr }
    357   1.1    scottr 
    358   1.1    scottr 
    359   1.1    scottr /***
    360   1.1    scottr  * The following code implements interrupt-driven PDMA.
    361   1.1    scottr  ***/
    362   1.1    scottr 
    363   1.1    scottr /*
    364   1.1    scottr  * This is the meat of the PDMA transfer.
    365   1.1    scottr  * When we get here, we shove data as fast as the mac can take it.
    366   1.1    scottr  * We depend on several things:
    367   1.1    scottr  *   * All macs after the Mac Plus that have a 5380 chip should have a general
    368   1.1    scottr  *     logic IC that handshakes data for blind transfers.
    369   1.1    scottr  *   * If the SCSI controller finishes sending/receiving data before we do,
    370   1.1    scottr  *     the same general logic IC will generate a /BERR for us in short order.
    371   1.1    scottr  *   * The fault address for said /BERR minus the base address for the
    372   1.1    scottr  *     transfer will be the amount of data that was actually written.
    373   1.1    scottr  *
    374   1.1    scottr  * We use the nofault flag and the setjmp/longjmp in locore.s so we can
    375   1.1    scottr  * detect and handle the bus error for early termination of a command.
    376   1.1    scottr  * This is usually caused by a disconnecting target.
    377   1.1    scottr  */
    378   1.1    scottr void
    379   1.1    scottr sbc_drq_intr(p)
    380   1.1    scottr 	void *p;
    381   1.1    scottr {
    382  1.27    scottr 	extern int *nofault, m68k_fault_addr;
    383  1.24    scottr 	struct sbc_softc *sc = (struct sbc_softc *)p;
    384  1.24    scottr 	struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *)p;
    385  1.23    scottr 	struct sci_req *sr = ncr_sc->sc_current;
    386  1.23    scottr 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    387  1.23    scottr 	label_t faultbuf;
    388  1.23    scottr 	volatile u_int32_t *long_drq;
    389  1.23    scottr 	u_int32_t *long_data;
    390  1.23    scottr 	volatile u_int8_t *drq;
    391  1.23    scottr 	u_int8_t *data;
    392  1.23    scottr 	int count, dcount, resid;
    393  1.23    scottr 	u_int8_t tmp;
    394  1.26    scottr 
    395  1.26    scottr 	/* Work around lame gcc initialization bug */
    396  1.26    scottr 	(void)&drq;
    397   1.1    scottr 
    398   1.1    scottr 	/*
    399   1.1    scottr 	 * If we're not ready to xfer data, or have no more, just return.
    400   1.1    scottr 	 */
    401   1.3    scottr 	if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0 || dh->dh_len == 0)
    402   1.1    scottr 		return;
    403   1.1    scottr 
    404   1.1    scottr #ifdef SBC_DEBUG
    405   1.1    scottr 	if (sbc_debug & SBC_DB_INTR)
    406  1.13  christos 		printf("%s: drq intr, dh_len=0x%x, dh_flags=0x%x\n",
    407   1.1    scottr 		    ncr_sc->sc_dev.dv_xname, dh->dh_len, dh->dh_flags);
    408   1.1    scottr #endif
    409   1.1    scottr 
    410   1.1    scottr 	/*
    411   1.1    scottr 	 * Setup for a possible bus error caused by SCSI controller
    412   1.1    scottr 	 * switching out of DATA-IN/OUT before we're done with the
    413   1.1    scottr 	 * current transfer.
    414   1.1    scottr 	 */
    415  1.26    scottr 	nofault = (int *)&faultbuf;
    416   1.1    scottr 
    417  1.24    scottr 	if (setjmp((label_t *)nofault)) {
    418  1.26    scottr 		nofault = (int *)0;
    419   1.8    scottr 		if ((dh->dh_flags & SBC_DH_DONE) == 0) {
    420  1.27    scottr 			count = ((  (u_long)m68k_fault_addr
    421  1.24    scottr 				  - (u_long)sc->sc_drq_addr));
    422   1.8    scottr 
    423   1.8    scottr 			if ((count < 0) || (count > dh->dh_len)) {
    424  1.13  christos 				printf("%s: complete=0x%x (pending 0x%x)\n",
    425   1.8    scottr 				    ncr_sc->sc_dev.dv_xname, count, dh->dh_len);
    426   1.8    scottr 				panic("something is wrong");
    427   1.8    scottr 			}
    428   1.1    scottr 
    429   1.8    scottr 			dh->dh_addr += count;
    430   1.8    scottr 			dh->dh_len -= count;
    431  1.18    scottr 		} else
    432  1.18    scottr 			count = 0;
    433   1.8    scottr 
    434   1.1    scottr #ifdef SBC_DEBUG
    435   1.1    scottr 		if (sbc_debug & SBC_DB_INTR)
    436  1.13  christos 			printf("%s: drq /berr, complete=0x%x (pending 0x%x)\n",
    437   1.8    scottr 			   ncr_sc->sc_dev.dv_xname, count, dh->dh_len);
    438   1.1    scottr #endif
    439  1.27    scottr 		m68k_fault_addr = 0;
    440   1.3    scottr 
    441   1.1    scottr 		return;
    442   1.1    scottr 	}
    443   1.1    scottr 
    444   1.1    scottr 	if (dh->dh_flags & SBC_DH_OUT) { /* Data Out */
    445  1.26    scottr 		dcount = 0;
    446  1.26    scottr 
    447   1.1    scottr 		/*
    448   1.1    scottr 		 * Get the source address aligned.
    449   1.1    scottr 		 */
    450   1.6    scottr 		resid =
    451  1.24    scottr 		    count = min(dh->dh_len, 4 - (((int)dh->dh_addr) & 0x3));
    452   1.1    scottr 		if (count && count < 4) {
    453  1.24    scottr 			drq = (volatile u_int8_t *)sc->sc_drq_addr;
    454  1.24    scottr 			data = (u_int8_t *)dh->dh_addr;
    455   1.8    scottr 
    456   1.1    scottr #define W1		*drq++ = *data++
    457   1.1    scottr 			while (count) {
    458   1.1    scottr 				W1; count--;
    459   1.1    scottr 			}
    460   1.1    scottr #undef W1
    461   1.1    scottr 			dh->dh_addr += resid;
    462   1.1    scottr 			dh->dh_len -= resid;
    463   1.1    scottr 		}
    464   1.1    scottr 
    465   1.1    scottr 		/*
    466   1.8    scottr 		 * Start the transfer.
    467   1.1    scottr 		 */
    468   1.1    scottr 		while (dh->dh_len) {
    469   1.1    scottr 			dcount = count = min(dh->dh_len, MAX_DMA_LEN);
    470  1.24    scottr 			long_drq = (volatile u_int32_t *)sc->sc_drq_addr;
    471  1.24    scottr 			long_data = (u_int32_t *)dh->dh_addr;
    472   1.1    scottr 
    473   1.1    scottr #define W4		*long_drq++ = *long_data++
    474   1.1    scottr 			while (count >= 64) {
    475   1.1    scottr 				W4; W4; W4; W4; W4; W4; W4; W4;
    476   1.1    scottr 				W4; W4; W4; W4; W4; W4; W4; W4; /*  64 */
    477   1.1    scottr 				count -= 64;
    478   1.1    scottr 			}
    479   1.1    scottr 			while (count >= 4) {
    480   1.1    scottr 				W4; count -= 4;
    481   1.1    scottr 			}
    482   1.1    scottr #undef W4
    483  1.24    scottr 			data = (u_int8_t *)long_data;
    484  1.24    scottr 			drq = (u_int8_t *)long_drq;
    485   1.8    scottr 
    486   1.7    scottr #define W1		*drq++ = *data++
    487   1.7    scottr 			while (count) {
    488   1.7    scottr 				W1; count--;
    489   1.7    scottr 			}
    490   1.7    scottr #undef W1
    491   1.7    scottr 			dh->dh_len -= dcount;
    492   1.7    scottr 			dh->dh_addr += dcount;
    493   1.7    scottr 		}
    494   1.8    scottr 		dh->dh_flags |= SBC_DH_DONE;
    495   1.7    scottr 
    496   1.7    scottr 		/*
    497   1.8    scottr 		 * XXX -- Read a byte from the SBC to trigger a /BERR.
    498   1.8    scottr 		 * This seems to be necessary for us to notice that
    499   1.8    scottr 		 * the target has disconnected.  Ick.  06 jun 1996 (sr)
    500   1.7    scottr 		 */
    501  1.26    scottr 		if (dcount >= MAX_DMA_LEN)
    502  1.24    scottr 			drq = (volatile u_int8_t *)sc->sc_drq_addr;
    503   1.8    scottr 		tmp = *drq;
    504   1.1    scottr 	} else {	/* Data In */
    505   1.1    scottr 		/*
    506   1.1    scottr 		 * Get the dest address aligned.
    507   1.1    scottr 		 */
    508   1.6    scottr 		resid =
    509  1.24    scottr 		    count = min(dh->dh_len, 4 - (((int)dh->dh_addr) & 0x3));
    510   1.1    scottr 		if (count && count < 4) {
    511  1.24    scottr 			data = (u_int8_t *)dh->dh_addr;
    512  1.24    scottr 			drq = (volatile u_int8_t *)sc->sc_drq_addr;
    513   1.8    scottr 
    514   1.1    scottr #define R1		*data++ = *drq++
    515   1.1    scottr 			while (count) {
    516   1.1    scottr 				R1; count--;
    517   1.1    scottr 			}
    518   1.1    scottr #undef R1
    519   1.1    scottr 			dh->dh_addr += resid;
    520   1.1    scottr 			dh->dh_len -= resid;
    521   1.1    scottr 		}
    522   1.1    scottr 
    523   1.1    scottr 		/*
    524   1.8    scottr 		 * Start the transfer.
    525   1.1    scottr 		 */
    526   1.1    scottr 		while (dh->dh_len) {
    527   1.1    scottr 			dcount = count = min(dh->dh_len, MAX_DMA_LEN);
    528  1.24    scottr 			long_data = (u_int32_t *)dh->dh_addr;
    529  1.24    scottr 			long_drq = (volatile u_int32_t *)sc->sc_drq_addr;
    530   1.1    scottr 
    531   1.1    scottr #define R4		*long_data++ = *long_drq++
    532   1.8    scottr 			while (count >= 64) {
    533   1.1    scottr 				R4; R4; R4; R4; R4; R4; R4; R4;
    534   1.1    scottr 				R4; R4; R4; R4; R4; R4; R4; R4;	/* 64 */
    535   1.8    scottr 				count -= 64;
    536   1.1    scottr 			}
    537   1.1    scottr 			while (count >= 4) {
    538   1.1    scottr 				R4; count -= 4;
    539   1.1    scottr 			}
    540   1.1    scottr #undef R4
    541  1.24    scottr 			data = (u_int8_t *)long_data;
    542  1.24    scottr 			drq = (volatile u_int8_t *)long_drq;
    543   1.8    scottr 
    544   1.1    scottr #define R1		*data++ = *drq++
    545   1.1    scottr 			while (count) {
    546   1.1    scottr 				R1; count--;
    547   1.1    scottr 			}
    548   1.1    scottr #undef R1
    549   1.1    scottr 			dh->dh_len -= dcount;
    550   1.1    scottr 			dh->dh_addr += dcount;
    551   1.1    scottr 		}
    552   1.8    scottr 		dh->dh_flags |= SBC_DH_DONE;
    553   1.1    scottr 	}
    554   1.1    scottr 
    555   1.1    scottr 	/*
    556   1.1    scottr 	 * OK.  No bus error occurred above.  Clear the nofault flag
    557   1.1    scottr 	 * so we no longer short-circuit bus errors.
    558   1.1    scottr 	 */
    559   1.1    scottr 	nofault = (int *) 0;
    560   1.7    scottr 
    561   1.7    scottr #ifdef SBC_DEBUG
    562   1.7    scottr 	if (sbc_debug & (SBC_DB_REG | SBC_DB_INTR))
    563  1.13  christos 		printf("%s: drq intr complete: csr=0x%x, bus_csr=0x%x\n",
    564   1.7    scottr 		    ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
    565   1.7    scottr 		    *ncr_sc->sci_bus_csr);
    566   1.7    scottr #endif
    567   1.1    scottr }
    568   1.1    scottr 
    569   1.1    scottr void
    570   1.1    scottr sbc_dma_alloc(ncr_sc)
    571   1.1    scottr 	struct ncr5380_softc *ncr_sc;
    572   1.1    scottr {
    573  1.24    scottr 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    574   1.1    scottr 	struct sci_req *sr = ncr_sc->sc_current;
    575   1.1    scottr 	struct scsi_xfer *xs = sr->sr_xs;
    576   1.1    scottr 	struct sbc_pdma_handle *dh;
    577   1.1    scottr 	int		i, xlen;
    578   1.1    scottr 
    579   1.6    scottr #ifdef DIAGNOSTIC
    580   1.1    scottr 	if (sr->sr_dma_hand != NULL)
    581   1.1    scottr 		panic("sbc_dma_alloc: already have PDMA handle");
    582   1.1    scottr #endif
    583   1.1    scottr 
    584   1.1    scottr 	/* Polled transfers shouldn't allocate a PDMA handle. */
    585   1.1    scottr 	if (sr->sr_flags & SR_IMMED)
    586   1.1    scottr 		return;
    587   1.1    scottr 
    588   1.1    scottr 	xlen = ncr_sc->sc_datalen;
    589   1.1    scottr 
    590   1.1    scottr 	/* Make sure our caller checked sc_min_dma_len. */
    591   1.1    scottr 	if (xlen < MIN_DMA_LEN)
    592   1.1    scottr 		panic("sbc_dma_alloc: len=0x%x\n", xlen);
    593   1.1    scottr 
    594   1.1    scottr 	/*
    595   1.1    scottr 	 * Find free PDMA handle.  Guaranteed to find one since we
    596   1.1    scottr 	 * have as many PDMA handles as the driver has processes.
    597   1.1    scottr 	 * (instances?)
    598   1.1    scottr 	 */
    599   1.1    scottr 	 for (i = 0; i < SCI_OPENINGS; i++) {
    600   1.1    scottr 		if ((sc->sc_pdma[i].dh_flags & SBC_DH_BUSY) == 0)
    601   1.1    scottr 			goto found;
    602   1.1    scottr 	}
    603   1.1    scottr 	panic("sbc: no free PDMA handles");
    604   1.1    scottr found:
    605   1.1    scottr 	dh = &sc->sc_pdma[i];
    606   1.1    scottr 	dh->dh_flags = SBC_DH_BUSY;
    607   1.1    scottr 	dh->dh_addr = ncr_sc->sc_dataptr;
    608   1.1    scottr 	dh->dh_len = xlen;
    609   1.1    scottr 
    610   1.1    scottr 	/* Copy the 'write' flag for convenience. */
    611   1.1    scottr 	if (xs->flags & SCSI_DATA_OUT)
    612   1.1    scottr 		dh->dh_flags |= SBC_DH_OUT;
    613   1.1    scottr 
    614   1.1    scottr 	sr->sr_dma_hand = dh;
    615   1.1    scottr }
    616   1.1    scottr 
    617   1.1    scottr void
    618   1.1    scottr sbc_dma_free(ncr_sc)
    619   1.1    scottr 	struct ncr5380_softc *ncr_sc;
    620   1.1    scottr {
    621   1.1    scottr 	struct sci_req *sr = ncr_sc->sc_current;
    622   1.1    scottr 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    623   1.1    scottr 
    624   1.6    scottr #ifdef DIAGNOSTIC
    625   1.1    scottr 	if (sr->sr_dma_hand == NULL)
    626   1.1    scottr 		panic("sbc_dma_free: no DMA handle");
    627   1.1    scottr #endif
    628   1.1    scottr 
    629   1.1    scottr 	if (ncr_sc->sc_state & NCR_DOINGDMA)
    630   1.1    scottr 		panic("sbc_dma_free: free while in progress");
    631   1.1    scottr 
    632   1.1    scottr 	if (dh->dh_flags & SBC_DH_BUSY) {
    633   1.1    scottr 		dh->dh_flags = 0;
    634   1.1    scottr 		dh->dh_addr = NULL;
    635   1.1    scottr 		dh->dh_len = 0;
    636   1.1    scottr 	}
    637   1.1    scottr 	sr->sr_dma_hand = NULL;
    638   1.1    scottr }
    639   1.1    scottr 
    640   1.1    scottr void
    641   1.1    scottr sbc_dma_poll(ncr_sc)
    642   1.1    scottr 	struct ncr5380_softc *ncr_sc;
    643   1.1    scottr {
    644   1.1    scottr 	struct sci_req *sr = ncr_sc->sc_current;
    645   1.1    scottr 
    646   1.3    scottr 	/*
    647   1.3    scottr 	 * We shouldn't arrive here; if SR_IMMED is set, then
    648   1.3    scottr 	 * dma_alloc() should have refused to allocate a handle
    649   1.3    scottr 	 * for the transfer.  This forces the polled PDMA code
    650   1.3    scottr 	 * to handle the request...
    651   1.3    scottr 	 */
    652   1.6    scottr #ifdef SBC_DEBUG
    653   1.1    scottr 	if (sbc_debug & SBC_DB_DMA)
    654  1.13  christos 		printf("%s: lost DRQ interrupt?\n", ncr_sc->sc_dev.dv_xname);
    655   1.1    scottr #endif
    656   1.3    scottr 	sr->sr_flags |= SR_OVERDUE;
    657   1.1    scottr }
    658   1.1    scottr 
    659   1.1    scottr void
    660   1.1    scottr sbc_dma_setup(ncr_sc)
    661   1.1    scottr 	struct ncr5380_softc *ncr_sc;
    662   1.1    scottr {
    663   1.1    scottr 	/* Not needed; we don't have real DMA */
    664   1.1    scottr }
    665   1.1    scottr 
    666   1.1    scottr void
    667   1.1    scottr sbc_dma_start(ncr_sc)
    668   1.1    scottr 	struct ncr5380_softc *ncr_sc;
    669   1.1    scottr {
    670  1.24    scottr 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    671   1.1    scottr 	struct sci_req *sr = ncr_sc->sc_current;
    672   1.1    scottr 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    673   1.1    scottr 
    674   1.1    scottr 	/*
    675   1.7    scottr 	 * Match bus phase, clear pending interrupts, set DMA mode, and
    676   1.7    scottr 	 * assert data bus (for writing only), then start the transfer.
    677   1.1    scottr 	 */
    678   1.1    scottr 	if (dh->dh_flags & SBC_DH_OUT) {
    679   1.1    scottr 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
    680   1.1    scottr 		SCI_CLR_INTR(ncr_sc);
    681  1.22    scottr 		if (sc->sc_clrintr)
    682  1.22    scottr 			(*sc->sc_clrintr)(ncr_sc);
    683   1.1    scottr 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
    684   1.1    scottr 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
    685   1.1    scottr 		*ncr_sc->sci_dma_send = 0;
    686   1.1    scottr 	} else {
    687   1.1    scottr 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
    688   1.1    scottr 		SCI_CLR_INTR(ncr_sc);
    689  1.22    scottr 		if (sc->sc_clrintr)
    690  1.22    scottr 			(*sc->sc_clrintr)(ncr_sc);
    691   1.1    scottr 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
    692   1.1    scottr 		*ncr_sc->sci_icmd = 0;
    693   1.1    scottr 		*ncr_sc->sci_irecv = 0;
    694   1.1    scottr 	}
    695   1.3    scottr 	ncr_sc->sc_state |= NCR_DOINGDMA;
    696   1.1    scottr 
    697   1.6    scottr #ifdef SBC_DEBUG
    698   1.1    scottr 	if (sbc_debug & SBC_DB_DMA)
    699  1.13  christos 		printf("%s: PDMA started, va=%p, len=0x%x\n",
    700   1.1    scottr 		    ncr_sc->sc_dev.dv_xname, dh->dh_addr, dh->dh_len);
    701   1.1    scottr #endif
    702   1.1    scottr }
    703   1.1    scottr 
    704   1.1    scottr void
    705   1.1    scottr sbc_dma_eop(ncr_sc)
    706   1.1    scottr 	struct ncr5380_softc *ncr_sc;
    707   1.1    scottr {
    708   1.1    scottr 	/* Not used; the EOP pin is wired high (GMFH, pp. 389-390) */
    709   1.1    scottr }
    710   1.1    scottr 
    711   1.1    scottr void
    712   1.1    scottr sbc_dma_stop(ncr_sc)
    713   1.1    scottr 	struct ncr5380_softc *ncr_sc;
    714   1.1    scottr {
    715  1.24    scottr 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
    716   1.1    scottr 	struct sci_req *sr = ncr_sc->sc_current;
    717   1.1    scottr 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
    718  1.23    scottr 	int ntrans;
    719   1.1    scottr 
    720   1.1    scottr 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    721   1.1    scottr #ifdef SBC_DEBUG
    722   1.1    scottr 		if (sbc_debug & SBC_DB_DMA)
    723  1.13  christos 			printf("%s: dma_stop: DMA not running\n",
    724   1.1    scottr 			    ncr_sc->sc_dev.dv_xname);
    725   1.1    scottr #endif
    726   1.1    scottr 		return;
    727   1.1    scottr 	}
    728   1.1    scottr 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    729   1.1    scottr 
    730   1.3    scottr 	if ((ncr_sc->sc_state & NCR_ABORTING) == 0) {
    731   1.1    scottr 		ntrans = ncr_sc->sc_datalen - dh->dh_len;
    732   1.1    scottr 
    733   1.1    scottr #ifdef SBC_DEBUG
    734   1.1    scottr 		if (sbc_debug & SBC_DB_DMA)
    735  1.13  christos 			printf("%s: dma_stop: ntrans=0x%x\n",
    736   1.1    scottr 			    ncr_sc->sc_dev.dv_xname, ntrans);
    737   1.1    scottr #endif
    738   1.1    scottr 
    739   1.1    scottr 		if (ntrans > ncr_sc->sc_datalen)
    740   1.1    scottr 			panic("sbc_dma_stop: excess transfer\n");
    741   1.1    scottr 
    742   1.1    scottr 		/* Adjust data pointer */
    743   1.1    scottr 		ncr_sc->sc_dataptr += ntrans;
    744   1.1    scottr 		ncr_sc->sc_datalen -= ntrans;
    745   1.1    scottr 
    746   1.1    scottr 		/* Clear any pending interrupts. */
    747   1.1    scottr 		SCI_CLR_INTR(ncr_sc);
    748  1.22    scottr 		if (sc->sc_clrintr)
    749  1.22    scottr 			(*sc->sc_clrintr)(ncr_sc);
    750   1.1    scottr 	}
    751   1.1    scottr 
    752   1.1    scottr 	/* Put SBIC back into PIO mode. */
    753   1.1    scottr 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
    754   1.1    scottr 	*ncr_sc->sci_icmd = 0;
    755   1.1    scottr 
    756   1.1    scottr #ifdef SBC_DEBUG
    757   1.3    scottr 	if (sbc_debug & SBC_DB_REG)
    758  1.13  christos 		printf("%s: dma_stop: csr=0x%x, bus_csr=0x%x\n",
    759   1.1    scottr 		    ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
    760   1.1    scottr 		    *ncr_sc->sci_bus_csr);
    761   1.1    scottr #endif
    762   1.1    scottr }
    763