Home | History | Annotate | Line # | Download | only in dev
atari5380.c revision 1.42.44.4
      1  1.42.44.4      yamt /*	$NetBSD: atari5380.c,v 1.42.44.4 2010/08/11 22:51:44 yamt Exp $	*/
      2        1.1       leo 
      3        1.1       leo /*
      4        1.1       leo  * Copyright (c) 1995 Leo Weppelman.
      5        1.1       leo  * All rights reserved.
      6        1.1       leo  *
      7        1.1       leo  * Redistribution and use in source and binary forms, with or without
      8        1.1       leo  * modification, are permitted provided that the following conditions
      9        1.1       leo  * are met:
     10        1.1       leo  * 1. Redistributions of source code must retain the above copyright
     11        1.1       leo  *    notice, this list of conditions and the following disclaimer.
     12        1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       leo  *    documentation and/or other materials provided with the distribution.
     15        1.1       leo  *
     16        1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17        1.1       leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18        1.1       leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19        1.1       leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20        1.1       leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21        1.1       leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22        1.1       leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23        1.1       leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24        1.1       leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25        1.1       leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26        1.1       leo  */
     27       1.36     lukem 
     28       1.36     lukem #include <sys/cdefs.h>
     29  1.42.44.4      yamt __KERNEL_RCSID(0, "$NetBSD: atari5380.c,v 1.42.44.4 2010/08/11 22:51:44 yamt Exp $");
     30       1.32       leo 
     31       1.32       leo #include "opt_atariscsi.h"
     32        1.1       leo 
     33        1.1       leo #include <sys/param.h>
     34        1.1       leo #include <sys/systm.h>
     35        1.1       leo #include <sys/kernel.h>
     36        1.1       leo #include <sys/device.h>
     37        1.1       leo #include <sys/buf.h>
     38       1.28    bouyer #include <dev/scsipi/scsi_all.h>
     39       1.28    bouyer #include <dev/scsipi/scsipi_all.h>
     40       1.28    bouyer #include <dev/scsipi/scsi_message.h>
     41       1.28    bouyer #include <dev/scsipi/scsiconf.h>
     42       1.34   thorpej 
     43       1.34   thorpej #include <uvm/uvm_extern.h>
     44        1.1       leo 
     45       1.18       leo #include <m68k/asm_single.h>
     46       1.26       leo #include <m68k/cpu.h>
     47       1.26       leo #include <m68k/cacheops.h>
     48       1.18       leo 
     49       1.11       leo #include <atari/atari/stalloc.h>
     50       1.11       leo 
     51        1.1       leo /*
     52        1.1       leo  * Include the driver definitions
     53        1.1       leo  */
     54        1.1       leo #include <atari/dev/ncr5380reg.h>
     55        1.1       leo 
     56        1.1       leo #include <machine/stdarg.h>
     57        1.1       leo #include <machine/iomap.h>
     58        1.1       leo #include <machine/mfp.h>
     59  1.42.44.2      yamt #include <machine/intr.h>
     60       1.24       leo 
     61        1.1       leo #if defined(FALCON_SCSI)
     62        1.1       leo #include <machine/dma.h>
     63        1.1       leo #endif
     64        1.1       leo 
     65        1.1       leo /*
     66        1.1       leo  * Set the various driver options
     67        1.1       leo  */
     68        1.1       leo #define	NREQ		18	/* Size of issue queue			*/
     69        1.1       leo #define	AUTO_SENSE	1	/* Automatically issue a request-sense 	*/
     70        1.1       leo 
     71        1.2       leo #define	DRNAME		ncrscsi	/* used in various prints		*/
     72        1.1       leo #undef	DBG_SEL			/* Show the selection process		*/
     73        1.1       leo #undef	DBG_REQ			/* Show enqueued/ready requests		*/
     74        1.2       leo #undef	DBG_ERR_RET		/* Show requests with != 0 return code	*/
     75        1.1       leo #undef	DBG_NOWRITE		/* Do not allow writes to the targets	*/
     76        1.1       leo #undef	DBG_PIO			/* Show the polled-I/O process		*/
     77        1.1       leo #undef	DBG_INF			/* Show information transfer process	*/
     78        1.1       leo #define	DBG_NOSTATIC		/* No static functions, all in DDB trace*/
     79       1.14       leo #define	DBG_PID		15	/* Keep track of driver			*/
     80        1.1       leo #define	REAL_DMA		/* Use DMA if sensible			*/
     81       1.17       leo #if defined(FALCON_SCSI)
     82        1.1       leo #define	REAL_DMA_POLL	1	/* 1: Poll for end of DMA-transfer	*/
     83        1.1       leo #else
     84        1.1       leo #define	REAL_DMA_POLL	0	/* 1: Poll for end of DMA-transfer	*/
     85        1.1       leo #endif
     86        1.1       leo #undef	USE_PDMA		/* Use special pdma-transfer function	*/
     87        1.4       leo #define MIN_PHYS	65536	/*BARF!!!!*/
     88        1.1       leo 
     89        1.1       leo /*
     90        1.8       leo  * Include more driver definitions
     91        1.8       leo  */
     92        1.8       leo #include <atari/dev/ncr5380var.h>
     93        1.8       leo 
     94        1.8       leo /*
     95        1.1       leo  * The atari specific driver options
     96        1.1       leo  */
     97        1.1       leo #undef	NO_TTRAM_DMA		/* Do not use DMA to TT-ram. This	*/
     98        1.1       leo 				/*    fails on older atari's		*/
     99        1.1       leo #define	ENABLE_NCR5380(sc)	cur_softc = sc;
    100        1.1       leo 
    101  1.42.44.4      yamt static uint8_t	*alloc_bounceb(u_long);
    102  1.42.44.4      yamt static void	free_bounceb(uint8_t *);
    103  1.42.44.1      yamt static int	machine_match(struct device *, void *, void *,
    104  1.42.44.4      yamt 		    struct cfdriver *);
    105  1.42.44.4      yamt 
    106  1.42.44.4      yamt void	scsi_ctrl(int);
    107  1.42.44.4      yamt void	scsi_dma(int);
    108        1.9       leo 
    109        1.3       leo /*
    110        1.3       leo  * Functions that do nothing on the atari
    111        1.3       leo  */
    112        1.3       leo #define	pdma_ready()		0
    113        1.1       leo 
    114        1.1       leo #if defined(TT_SCSI)
    115       1.18       leo 
    116  1.42.44.1      yamt void	ncr5380_drq_intr(int);
    117       1.18       leo 
    118        1.1       leo /*
    119        1.1       leo  * Define all the things we need of the DMA-controller
    120        1.1       leo  */
    121        1.1       leo #define	SCSI_DMA	((struct scsi_dma *)AD_SCSI_DMA)
    122        1.1       leo #define	SCSI_5380	((struct scsi_5380 *)AD_NCR5380)
    123        1.1       leo 
    124        1.1       leo struct scsi_dma {
    125  1.42.44.4      yamt 	volatile uint8_t	s_dma_ptr[8];	/* use only the odd bytes */
    126  1.42.44.4      yamt 	volatile uint8_t	s_dma_cnt[8];	/* use only the odd bytes */
    127  1.42.44.4      yamt 	volatile uint8_t	s_dma_res[4];	/* data residue register  */
    128  1.42.44.4      yamt 	volatile uint8_t	s_dma_gap;	/* not used		  */
    129  1.42.44.4      yamt 	volatile uint8_t	s_dma_ctrl;	/* control register	  */
    130  1.42.44.4      yamt 	volatile uint8_t	s_dma_gap2;	/* not used		  */
    131  1.42.44.4      yamt 	volatile uint8_t	s_hdma_ctrl;	/* Hades control register */
    132        1.1       leo };
    133        1.1       leo 
    134  1.42.44.4      yamt static inline void set_scsi_dma(volatile uint8_t *, u_long);
    135  1.42.44.4      yamt static inline u_long get_scsi_dma(volatile uint8_t *);
    136  1.42.44.1      yamt 
    137  1.42.44.1      yamt static inline void
    138  1.42.44.4      yamt set_scsi_dma(volatile uint8_t *addr, u_long val)
    139  1.42.44.1      yamt {
    140  1.42.44.4      yamt 	volatile uint8_t *address;
    141  1.42.44.1      yamt 
    142  1.42.44.1      yamt 	address = addr + 1;
    143  1.42.44.1      yamt 	__asm("movepl	%0, %1@(0)": :"d" (val), "a" (address));
    144  1.42.44.1      yamt }
    145  1.42.44.1      yamt 
    146  1.42.44.1      yamt static inline u_long
    147  1.42.44.4      yamt get_scsi_dma(volatile uint8_t *addr)
    148  1.42.44.1      yamt {
    149  1.42.44.4      yamt 	volatile uint8_t *address;
    150  1.42.44.1      yamt 	u_long	nval;
    151  1.42.44.1      yamt 
    152  1.42.44.1      yamt 	address = addr + 1;
    153  1.42.44.1      yamt 	__asm("movepl	%1@(0), %0": "=d" (nval) : "a" (address));
    154  1.42.44.1      yamt 
    155  1.42.44.1      yamt 	return nval;
    156  1.42.44.1      yamt }
    157        1.1       leo 
    158        1.1       leo /*
    159        1.1       leo  * Defines for TT-DMA control register
    160        1.1       leo  */
    161        1.1       leo #define	SD_BUSERR	0x80		/* 1 = transfer caused bus error*/
    162        1.1       leo #define	SD_ZERO		0x40		/* 1 = byte counter is zero	*/
    163        1.1       leo #define	SD_ENABLE	0x02		/* 1 = Enable DMA		*/
    164        1.1       leo #define	SD_OUT		0x01		/* Direction: memory to SCSI	*/
    165        1.1       leo #define	SD_IN		0x00		/* Direction: SCSI to memory	*/
    166        1.1       leo 
    167        1.1       leo /*
    168       1.18       leo  * Defines for Hades-DMA control register
    169       1.18       leo  */
    170       1.18       leo #define	SDH_BUSERR	0x02		/* 1 = Bus error		*/
    171       1.18       leo #define SDH_EOP		0x01		/* 1 = Signal EOP on 5380	*/
    172       1.18       leo #define	SDH_ZERO	0x40		/* 1 = Byte counter is zero	*/
    173       1.18       leo 
    174       1.18       leo /*
    175        1.1       leo  * Define the 5380 register set
    176        1.1       leo  */
    177        1.1       leo struct scsi_5380 {
    178  1.42.44.4      yamt 	volatile uint8_t scsi_5380[16];	/* use only the odd bytes	*/
    179        1.1       leo };
    180        1.1       leo #endif /* TT_SCSI */
    181        1.1       leo 
    182        1.1       leo /**********************************************
    183        1.1       leo  * Variables present for both TT and Falcon.  *
    184        1.1       leo  **********************************************/
    185        1.1       leo 
    186        1.1       leo /*
    187        1.1       leo  * Softc of currently active controller (a bit of fake; we only have one)
    188        1.1       leo  */
    189        1.1       leo static struct ncr_softc	*cur_softc;
    190        1.1       leo 
    191        1.1       leo #if defined(TT_SCSI) && !defined(FALCON_SCSI)
    192        1.1       leo /*
    193        1.1       leo  * We can be more efficient for some functions when only TT_SCSI is selected
    194        1.1       leo  */
    195        1.1       leo #define	GET_5380_REG(rnum)	SCSI_5380->scsi_5380[(rnum << 1) | 1]
    196        1.1       leo #define	SET_5380_REG(rnum,val)	(SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
    197        1.1       leo 
    198        1.1       leo #define scsi_mach_init(sc)	scsi_tt_init(sc)
    199        1.1       leo #define scsi_ienable()		scsi_tt_ienable()
    200        1.1       leo #define scsi_idisable()		scsi_tt_idisable()
    201        1.1       leo #define	scsi_clr_ipend()	scsi_tt_clr_ipend()
    202        1.7       leo #define scsi_ipending()		(GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)
    203        1.1       leo #define scsi_dma_setup(r,p,m)	scsi_tt_dmasetup(r, p, m)
    204        1.1       leo #define wrong_dma_range(r,d)	tt_wrong_dma_range(r, d)
    205        1.1       leo #define poll_edma(reqp)		tt_poll_edma(reqp)
    206        1.1       leo #define get_dma_result(r, b)	tt_get_dma_result(r, b)
    207       1.16       leo #define	can_access_5380()	1
    208       1.18       leo #define emulated_dma()		((machineid & ATARI_HADES) ? 1 : 0)
    209        1.1       leo 
    210        1.1       leo #define fair_to_keep_dma()	1
    211        1.1       leo #define claimed_dma()		1
    212        1.1       leo #define reconsider_dma()
    213        1.1       leo 
    214        1.1       leo #endif /* defined(TT_SCSI) && !defined(FALCON_SCSI) */
    215        1.1       leo 
    216        1.1       leo #if defined(TT_SCSI)
    217        1.1       leo 
    218        1.1       leo /*
    219        1.9       leo  * Prototype functions defined below
    220        1.9       leo  */
    221        1.9       leo #ifdef NO_TTRAM_DMA
    222  1.42.44.1      yamt static int tt_wrong_dma_range(SC_REQ *, struct dma_chain *);
    223        1.9       leo #endif
    224  1.42.44.1      yamt static void	scsi_tt_init(struct ncr_softc *);
    225  1.42.44.4      yamt static uint8_t	get_tt_5380_reg(u_short);
    226  1.42.44.1      yamt static void	set_tt_5380_reg(u_short, u_short);
    227  1.42.44.4      yamt static void	scsi_tt_dmasetup(SC_REQ *, u_int, uint8_t);
    228  1.42.44.1      yamt static int	tt_poll_edma(SC_REQ *);
    229  1.42.44.4      yamt static uint8_t	*ptov(SC_REQ *, u_long*);
    230  1.42.44.1      yamt static int	tt_get_dma_result(SC_REQ *, u_long *);
    231  1.42.44.4      yamt 
    232  1.42.44.4      yamt void	scsi_tt_ienable(void);
    233  1.42.44.4      yamt void	scsi_tt_idisable(void);
    234  1.42.44.4      yamt void	scsi_tt_clr_ipend(void);
    235        1.9       leo 
    236        1.9       leo /*
    237        1.1       leo  * Define these too, so we can use them locally...
    238        1.1       leo  */
    239        1.1       leo #define	GET_TT_REG(rnum)	SCSI_5380->scsi_5380[(rnum << 1) | 1]
    240        1.1       leo #define	SET_TT_REG(rnum,val)	(SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
    241        1.1       leo 
    242        1.1       leo #ifdef NO_TTRAM_DMA
    243        1.9       leo static int
    244  1.42.44.1      yamt tt_wrong_dma_range(SC_REQ *reqp, struct dma_chain *dm)
    245        1.1       leo {
    246  1.42.44.4      yamt 
    247        1.1       leo 	if (dm->dm_addr & 0xff000000) {
    248        1.1       leo 		reqp->dr_flag |= DRIVER_BOUNCING;
    249  1.42.44.4      yamt 		return 1;
    250        1.1       leo 	}
    251  1.42.44.4      yamt 	return 0;
    252        1.1       leo }
    253        1.1       leo #else
    254        1.1       leo #define	tt_wrong_dma_range(reqp, dm)	0
    255        1.1       leo #endif
    256        1.1       leo 
    257        1.9       leo static void
    258        1.9       leo scsi_tt_init(struct ncr_softc *sc)
    259        1.1       leo {
    260  1.42.44.4      yamt 
    261        1.1       leo 	/*
    262        1.1       leo 	 * Enable SCSI-related interrupts
    263        1.1       leo 	 */
    264        1.1       leo 	MFP2->mf_aer  |= 0x80;		/* SCSI IRQ goes HIGH!!!!!	*/
    265        1.1       leo 
    266       1.18       leo 	if (machineid & ATARI_TT) {
    267       1.35       wiz 		/* SCSI-DMA interrupts		*/
    268       1.18       leo 		MFP2->mf_ierb |= IB_SCDM;
    269  1.42.44.4      yamt 		MFP2->mf_iprb  = (uint8_t)~IB_SCDM;
    270       1.18       leo 		MFP2->mf_imrb |= IB_SCDM;
    271  1.42.44.4      yamt 	} else if (machineid & ATARI_HADES) {
    272       1.24       leo 		SCSI_DMA->s_hdma_ctrl = 0;
    273       1.24       leo 
    274       1.24       leo 		if (intr_establish(2, AUTO_VEC, 0,
    275  1.42.44.4      yamt 		    (hw_ifun_t)ncr5380_drq_intr, NULL) == NULL)
    276       1.24       leo 		panic("scsi_tt_init: Can't establish drq-interrupt");
    277  1.42.44.4      yamt 	} else
    278  1.42.44.4      yamt 		panic("scsi_tt_init: should not come here");
    279        1.1       leo 
    280        1.1       leo 	MFP2->mf_iera |= IA_SCSI;	/* SCSI-5380 interrupts		*/
    281  1.42.44.4      yamt 	MFP2->mf_ipra  = (uint8_t)~IA_SCSI;
    282        1.1       leo 	MFP2->mf_imra |= IA_SCSI;
    283        1.1       leo 
    284        1.1       leo 	/*
    285  1.42.44.1      yamt 	 * LWP: DMA transfers to TT-ram causes data to be garbled
    286  1.42.44.1      yamt 	 * without notice on some TT-mainboard revisions.
    287  1.42.44.1      yamt 	 * If programs generate mysterious Segmentations faults,
    288  1.42.44.1      yamt 	 * try enabling NO_TTRAM_DMA.
    289        1.1       leo 	 */
    290        1.1       leo #ifdef NO_TTRAM_DMA
    291       1.20  christos 	printf(": DMA to TT-RAM is disabled!");
    292        1.1       leo #endif
    293        1.1       leo }
    294        1.1       leo 
    295  1.42.44.4      yamt static uint8_t
    296        1.9       leo get_tt_5380_reg(u_short rnum)
    297        1.1       leo {
    298  1.42.44.4      yamt 
    299  1.42.44.4      yamt 	return SCSI_5380->scsi_5380[(rnum << 1) | 1];
    300        1.1       leo }
    301        1.1       leo 
    302        1.9       leo static void
    303        1.9       leo set_tt_5380_reg(u_short rnum, u_short val)
    304        1.1       leo {
    305  1.42.44.4      yamt 
    306        1.1       leo 	SCSI_5380->scsi_5380[(rnum << 1) | 1] = val;
    307        1.1       leo }
    308        1.1       leo 
    309  1.42.44.4      yamt static inline void
    310        1.9       leo scsi_tt_ienable(void)
    311        1.1       leo {
    312  1.42.44.4      yamt 
    313       1.18       leo 	if (machineid & ATARI_TT)
    314       1.18       leo 		single_inst_bset_b(MFP2->mf_imrb, IB_SCDM);
    315       1.18       leo 	single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
    316        1.1       leo }
    317        1.1       leo 
    318  1.42.44.4      yamt static inline void
    319        1.9       leo scsi_tt_idisable(void)
    320        1.1       leo {
    321  1.42.44.4      yamt 
    322       1.18       leo 	if (machineid & ATARI_TT)
    323       1.18       leo 		single_inst_bclr_b(MFP2->mf_imrb, IB_SCDM);
    324       1.18       leo 	single_inst_bclr_b(MFP2->mf_imra, IA_SCSI);
    325        1.1       leo }
    326        1.1       leo 
    327  1.42.44.4      yamt static inline void
    328        1.9       leo scsi_tt_clr_ipend(void)
    329        1.1       leo {
    330  1.42.44.4      yamt 	int tmp;
    331        1.1       leo 
    332        1.1       leo 	SCSI_DMA->s_dma_ctrl = 0;
    333        1.1       leo 	tmp = GET_TT_REG(NCR5380_IRCV);
    334       1.18       leo 	if (machineid & ATARI_TT)
    335  1.42.44.4      yamt 		MFP2->mf_iprb = (uint8_t)~IB_SCDM;
    336  1.42.44.4      yamt 	MFP2->mf_ipra = (uint8_t)~IA_SCSI;
    337       1.21       leo 
    338       1.21       leo 	/*
    339       1.21       leo 	 * Remove interrupts already scheduled.
    340       1.21       leo 	 */
    341       1.21       leo 	rem_sicallback((si_farg)ncr_ctrl_intr);
    342       1.21       leo 	rem_sicallback((si_farg)ncr_dma_intr);
    343        1.1       leo }
    344        1.1       leo 
    345        1.9       leo static void
    346  1.42.44.4      yamt scsi_tt_dmasetup(SC_REQ *reqp, u_int phase, uint8_t mode)
    347        1.1       leo {
    348  1.42.44.4      yamt 
    349        1.1       leo 	if (PH_IN(phase)) {
    350        1.1       leo 		SCSI_DMA->s_dma_ctrl = SD_IN;
    351       1.18       leo 		if (machineid & ATARI_HADES)
    352  1.42.44.4      yamt 			SCSI_DMA->s_hdma_ctrl &= ~(SDH_BUSERR|SDH_EOP);
    353  1.42.44.1      yamt 		set_scsi_dma(SCSI_DMA->s_dma_ptr, reqp->dm_cur->dm_addr);
    354  1.42.44.1      yamt 		set_scsi_dma(SCSI_DMA->s_dma_cnt, reqp->dm_cur->dm_count);
    355        1.1       leo 		SET_TT_REG(NCR5380_ICOM, 0);
    356        1.1       leo 		SET_TT_REG(NCR5380_MODE, mode);
    357        1.1       leo 		SCSI_DMA->s_dma_ctrl = SD_ENABLE;
    358        1.1       leo 		SET_TT_REG(NCR5380_IRCV, 0);
    359  1.42.44.4      yamt 	} else {
    360        1.1       leo 		SCSI_DMA->s_dma_ctrl = SD_OUT;
    361       1.18       leo 		if (machineid & ATARI_HADES)
    362  1.42.44.4      yamt 			SCSI_DMA->s_hdma_ctrl &= ~(SDH_BUSERR|SDH_EOP);
    363  1.42.44.1      yamt 		set_scsi_dma(SCSI_DMA->s_dma_ptr, reqp->dm_cur->dm_addr);
    364  1.42.44.1      yamt 		set_scsi_dma(SCSI_DMA->s_dma_cnt, reqp->dm_cur->dm_count);
    365        1.1       leo 		SET_TT_REG(NCR5380_MODE, mode);
    366        1.1       leo 		SET_TT_REG(NCR5380_ICOM, SC_ADTB);
    367        1.1       leo 		SET_TT_REG(NCR5380_DMSTAT, 0);
    368        1.1       leo 		SCSI_DMA->s_dma_ctrl = SD_ENABLE|SD_OUT;
    369        1.1       leo 	}
    370        1.1       leo }
    371        1.1       leo 
    372        1.1       leo static int
    373        1.8       leo tt_poll_edma(SC_REQ *reqp)
    374        1.1       leo {
    375  1.42.44.4      yamt 	uint8_t	dmstat, dmastat;
    376        1.1       leo 	int	timeout = 9000; /* XXX */
    377        1.1       leo 
    378        1.1       leo 	/*
    379        1.1       leo 	 * We wait here until the DMA has finished. This can be
    380        1.1       leo 	 * achieved by checking the following conditions:
    381        1.1       leo 	 *   - 5380:
    382        1.1       leo 	 *	- End of DMA flag is set
    383        1.1       leo 	 *	- We lost BSY (error!!)
    384       1.33       wiz 	 *	- A phase mismatch has occurred (partial transfer)
    385        1.1       leo 	 *   - DMA-controller:
    386        1.1       leo 	 *	- A bus error occurred (Kernel error!!)
    387        1.1       leo 	 *	- All bytes are transferred
    388        1.1       leo 	 * If one of the terminating conditions was met, we call
    389        1.1       leo 	 * 'dma_ready' to check errors and perform the bookkeeping.
    390        1.1       leo 	 */
    391        1.1       leo 
    392       1.29       leo 	scsi_tt_idisable();
    393        1.1       leo 	for (;;) {
    394        1.1       leo 		delay(20);
    395        1.1       leo 		if (--timeout <= 0) {
    396        1.1       leo 			ncr_tprint(reqp, "timeout on polled transfer\n");
    397       1.11       leo 			reqp->xs->error = XS_TIMEOUT;
    398       1.29       leo 			scsi_tt_ienable();
    399  1.42.44.4      yamt 			return 0;
    400        1.1       leo 		}
    401        1.1       leo 		dmstat  = GET_TT_REG(NCR5380_DMSTAT);
    402       1.25       leo 
    403       1.25       leo 		if ((machineid & ATARI_HADES) && (dmstat & SC_DMA_REQ)) {
    404       1.27       leo 			ncr5380_drq_intr(1);
    405       1.25       leo 			dmstat  = GET_TT_REG(NCR5380_DMSTAT);
    406       1.25       leo 		}
    407       1.25       leo 
    408        1.1       leo 		dmastat = SCSI_DMA->s_dma_ctrl;
    409        1.1       leo 		if (dmstat & (SC_END_DMA|SC_BSY_ERR|SC_IRQ_SET))
    410        1.1       leo 			break;
    411  1.42.44.4      yamt 		if ((dmstat & SC_PHS_MTCH) == 0)
    412        1.1       leo 			break;
    413        1.1       leo 		if (dmastat & (SD_BUSERR|SD_ZERO))
    414        1.1       leo 			break;
    415        1.1       leo 	}
    416       1.29       leo 	scsi_tt_ienable();
    417  1.42.44.4      yamt 	return 1;
    418        1.1       leo }
    419        1.1       leo 
    420        1.2       leo /*
    421        1.2       leo  * Convert physical DMA address to a virtual address.
    422        1.2       leo  */
    423  1.42.44.4      yamt static uint8_t *
    424        1.8       leo ptov(SC_REQ *reqp, u_long *phaddr)
    425        1.2       leo {
    426        1.2       leo 	struct dma_chain	*dm;
    427  1.42.44.4      yamt 	uint8_t			*vaddr;
    428        1.2       leo 
    429        1.2       leo 	dm = reqp->dm_chain;
    430        1.2       leo 	vaddr = reqp->xdata_ptr;
    431  1.42.44.4      yamt 	for (; dm < reqp->dm_cur; dm++)
    432        1.2       leo 		vaddr += dm->dm_count;
    433        1.2       leo 	vaddr += (u_long)phaddr - dm->dm_addr;
    434  1.42.44.4      yamt 	return vaddr;
    435        1.2       leo }
    436        1.2       leo 
    437        1.1       leo static int
    438        1.8       leo tt_get_dma_result(SC_REQ *reqp, u_long *bytes_left)
    439        1.1       leo {
    440        1.1       leo 	int	dmastat, dmstat;
    441  1.42.44.4      yamt 	uint8_t	*byte_p;
    442  1.42.44.1      yamt 	u_long	leftover;
    443        1.1       leo 
    444        1.1       leo 	dmastat = SCSI_DMA->s_dma_ctrl;
    445        1.1       leo 	dmstat  = GET_TT_REG(NCR5380_DMSTAT);
    446  1.42.44.1      yamt 	leftover = get_scsi_dma(SCSI_DMA->s_dma_cnt);
    447  1.42.44.4      yamt 	byte_p = (uint8_t *)get_scsi_dma(SCSI_DMA->s_dma_ptr);
    448        1.1       leo 
    449        1.1       leo 	if (dmastat & SD_BUSERR) {
    450        1.1       leo 		/*
    451        1.1       leo 		 * The DMA-controller seems to access 8 bytes beyond
    452        1.1       leo 		 * it's limits on output. Therefore check also the byte
    453        1.1       leo 		 * count. If it's zero, ignore the bus error.
    454        1.1       leo 		 */
    455        1.1       leo 		if (leftover != 0) {
    456        1.1       leo 			ncr_tprint(reqp,
    457  1.42.44.4      yamt 			    "SCSI-DMA buserror - accessing 0x%x\n", byte_p);
    458        1.1       leo 			reqp->xs->error = XS_DRIVER_STUFFUP;
    459        1.1       leo 		}
    460        1.1       leo 	}
    461        1.1       leo 
    462        1.1       leo 	/*
    463        1.1       leo 	 * We handle the following special condition below:
    464        1.1       leo 	 *  -- The device disconnects in the middle of a write operation --
    465        1.1       leo 	 * In this case, the 5380 has already pre-fetched the next byte from
    466        1.1       leo 	 * the DMA-controller before the phase mismatch occurs. Therefore,
    467        1.1       leo 	 * leftover is 1 too low.
    468        1.1       leo 	 * This does not always happen! Therefore, we only do this when
    469        1.1       leo 	 * leftover is odd. This assumes that DMA transfers are _even_! This
    470        1.1       leo 	 * is normally the case on disks and types but might not always be.
    471        1.1       leo 	 * XXX: Check if ACK is consistently high on these occasions LWP
    472        1.1       leo 	 */
    473  1.42.44.4      yamt 	if ((leftover & 1) &&
    474  1.42.44.4      yamt 	    (dmstat & SC_PHS_MTCH) == 0 && PH_OUT(reqp->phase))
    475        1.1       leo 		leftover++;
    476        1.1       leo 
    477        1.1       leo 	/*
    478        1.1       leo 	 * Check if there are some 'restbytes' left in the DMA-controller.
    479        1.1       leo 	 */
    480  1.42.44.4      yamt 	if ((machineid & ATARI_TT) &&
    481  1.42.44.4      yamt 	    ((u_long)byte_p & 3) && PH_IN(reqp->phase)) {
    482  1.42.44.4      yamt 		uint8_t	*p;
    483  1.42.44.4      yamt 		volatile uint8_t *q;
    484        1.1       leo 
    485        1.8       leo 		p = ptov(reqp, (u_long *)((u_long)byte_p & ~3));
    486  1.42.44.1      yamt 		q = SCSI_DMA->s_dma_res;
    487        1.2       leo 		switch ((u_long)byte_p & 3) {
    488        1.1       leo 			case 3: *p++ = *q++;
    489        1.1       leo 			case 2: *p++ = *q++;
    490        1.1       leo 			case 1: *p++ = *q++;
    491        1.1       leo 		}
    492        1.1       leo 	}
    493        1.1       leo 	*bytes_left = leftover;
    494  1.42.44.4      yamt 	return (dmastat & (SD_BUSERR|SD_ZERO)) ? 1 : 0;
    495        1.1       leo }
    496        1.1       leo 
    497  1.42.44.4      yamt static uint8_t *dma_ptr;
    498       1.18       leo void
    499  1.42.44.1      yamt ncr5380_drq_intr(int poll)
    500       1.18       leo {
    501  1.42.44.4      yamt 	extern int *nofault;
    502  1.42.44.4      yamt 	label_t faultbuf;
    503  1.42.44.4      yamt 	int write;
    504  1.42.44.4      yamt 	u_long count;
    505  1.42.44.4      yamt 	volatile uint8_t *data_p = (volatile uint8_t *)(stio_addr + 0x741);
    506       1.18       leo 
    507       1.18       leo 	/*
    508       1.18       leo 	 * Block SCSI interrupts while emulating DMA. They come
    509       1.18       leo 	 * at a higher priority.
    510       1.18       leo 	 */
    511       1.22       leo 	single_inst_bclr_b(MFP2->mf_imra, IA_SCSI);
    512       1.18       leo 
    513       1.18       leo 	/*
    514       1.18       leo 	 * Setup for a possible bus error caused by SCSI controller
    515       1.18       leo 	 * switching out of DATA-IN/OUT before we're done with the
    516       1.18       leo 	 * current transfer.
    517       1.18       leo 	 */
    518  1.42.44.4      yamt 	nofault = (int *)&faultbuf;
    519       1.18       leo 
    520       1.18       leo 	if (setjmp((label_t *) nofault)) {
    521  1.42.44.4      yamt 		u_long cnt, tmp;
    522       1.18       leo 
    523       1.18       leo 		PID("drq berr");
    524  1.42.44.4      yamt 		nofault = (int *)0;
    525       1.18       leo 
    526       1.18       leo 		/*
    527       1.18       leo 		 * Determine number of bytes transferred
    528       1.18       leo 		 */
    529  1.42.44.1      yamt 		cnt = (u_long)dma_ptr - get_scsi_dma(SCSI_DMA->s_dma_ptr);
    530       1.18       leo 
    531       1.18       leo 		if (cnt != 0) {
    532       1.18       leo 			/*
    533       1.35       wiz 			 * Update the DMA pointer/count fields
    534       1.18       leo 			 */
    535  1.42.44.1      yamt 			set_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)dma_ptr);
    536  1.42.44.1      yamt 			tmp = get_scsi_dma(SCSI_DMA->s_dma_cnt);
    537       1.18       leo 			set_scsi_dma(SCSI_DMA->s_dma_cnt, tmp - cnt);
    538       1.18       leo 
    539       1.18       leo 			if (tmp > cnt) {
    540       1.18       leo 				/*
    541       1.18       leo 				 * Still more to transfer
    542       1.18       leo 				 */
    543       1.27       leo 				if (!poll)
    544  1.42.44.4      yamt 					single_inst_bset_b(MFP2->mf_imra,
    545  1.42.44.4      yamt 					    IA_SCSI);
    546       1.18       leo 				return;
    547       1.18       leo 			}
    548       1.18       leo 
    549       1.18       leo 			/*
    550       1.18       leo 			 * Signal EOP to 5380
    551       1.18       leo 			 */
    552       1.18       leo 			SCSI_DMA->s_hdma_ctrl |= SDH_EOP;
    553  1.42.44.4      yamt 		} else {
    554  1.42.44.4      yamt 			nofault = (int *)&faultbuf;
    555       1.25       leo 
    556       1.18       leo 			/*
    557       1.25       leo 			 * Try to figure out if the byte-count was
    558       1.25       leo 			 * zero because there was no (more) data or
    559       1.25       leo 			 * because the dma_ptr is bogus.
    560       1.18       leo 			 */
    561       1.25       leo 			if (setjmp((label_t *) nofault)) {
    562       1.25       leo 				/*
    563       1.25       leo 				 * Set the bus-error bit
    564       1.25       leo 				 */
    565       1.25       leo 				SCSI_DMA->s_hdma_ctrl |= SDH_BUSERR;
    566       1.25       leo 			}
    567       1.39     perry 			__asm volatile ("tstb	%0@(0)": : "a" (dma_ptr));
    568       1.25       leo 			nofault = (int *)0;
    569       1.18       leo 		}
    570       1.18       leo 
    571       1.18       leo 		/*
    572       1.18       leo 		 * Schedule an interrupt
    573       1.18       leo 		 */
    574       1.27       leo 		if (!poll && (SCSI_DMA->s_dma_ctrl & SD_ENABLE))
    575  1.42.44.4      yamt 			add_sicallback((si_farg)ncr_dma_intr,
    576  1.42.44.4      yamt 			    (void *)cur_softc, 0);
    577       1.18       leo 
    578       1.18       leo 		/*
    579       1.18       leo 		 * Clear DMA-mode
    580       1.18       leo 		 */
    581       1.18       leo 		SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE;
    582       1.27       leo 		if (!poll)
    583       1.27       leo 			single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
    584       1.18       leo 
    585       1.18       leo 		return;
    586       1.18       leo 	}
    587       1.18       leo 
    588       1.18       leo 	write = (SCSI_DMA->s_dma_ctrl & SD_OUT) ? 1 : 0;
    589       1.18       leo #if DBG_PID
    590       1.18       leo 	if (write) {
    591       1.18       leo 		PID("drq (in)");
    592       1.18       leo 	} else {
    593       1.18       leo 		PID("drq (out)");
    594       1.18       leo 	}
    595       1.18       leo #endif
    596       1.18       leo 
    597  1.42.44.1      yamt 	count = get_scsi_dma(SCSI_DMA->s_dma_cnt);
    598  1.42.44.4      yamt 	dma_ptr = (uint8_t *)get_scsi_dma(SCSI_DMA->s_dma_ptr);
    599       1.18       leo 
    600       1.18       leo 	/*
    601       1.18       leo 	 * Keep pushing bytes until we're done or a bus-error
    602       1.18       leo 	 * signals that the SCSI controller is not ready.
    603       1.18       leo 	 * NOTE: I tried some optimalizations in these loops,
    604       1.18       leo 	 *       but they had no effect on transfer speed.
    605       1.18       leo 	 */
    606       1.18       leo 	if (write) {
    607  1.42.44.4      yamt 		while (count--) {
    608       1.18       leo 			*data_p = *dma_ptr++;
    609       1.18       leo 		}
    610  1.42.44.4      yamt 	} else {
    611  1.42.44.4      yamt 		while (count--) {
    612       1.18       leo 			*dma_ptr++ = *data_p;
    613       1.18       leo 		}
    614       1.18       leo 	}
    615       1.18       leo 
    616       1.18       leo 	/*
    617       1.29       leo 	 * OK.  No bus error occurred above.  Clear the nofault flag
    618       1.29       leo 	 * so we no longer short-circuit bus errors.
    619       1.29       leo 	 */
    620  1.42.44.4      yamt 	nofault = (int *)0;
    621       1.29       leo 
    622       1.29       leo 	/*
    623       1.18       leo 	 * Schedule an interrupt
    624       1.18       leo 	 */
    625       1.27       leo 	if (!poll && (SCSI_DMA->s_dma_ctrl & SD_ENABLE))
    626  1.42.44.4      yamt 		add_sicallback((si_farg)ncr_dma_intr, (void *)cur_softc, 0);
    627       1.18       leo 
    628       1.18       leo 	/*
    629       1.18       leo 	 * Clear DMA-mode
    630       1.18       leo 	 */
    631       1.18       leo 	SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE;
    632       1.18       leo 
    633       1.18       leo 	/*
    634       1.18       leo 	 * Update the DMA 'registers' to reflect that all bytes
    635       1.18       leo 	 * have been transfered and tell this to the 5380 too.
    636       1.18       leo 	 */
    637       1.18       leo 	set_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)dma_ptr);
    638       1.18       leo 	set_scsi_dma(SCSI_DMA->s_dma_cnt, 0);
    639       1.18       leo 	SCSI_DMA->s_hdma_ctrl |= SDH_EOP;
    640       1.18       leo 
    641       1.18       leo 	PID("end drq");
    642       1.27       leo 	if (!poll)
    643       1.27       leo 		single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
    644       1.18       leo 
    645       1.18       leo }
    646       1.18       leo 
    647        1.1       leo #endif /* defined(TT_SCSI) */
    648        1.1       leo 
    649        1.1       leo #if defined(FALCON_SCSI) && !defined(TT_SCSI)
    650        1.1       leo 
    651        1.1       leo #define	GET_5380_REG(rnum)	get_falcon_5380_reg(rnum)
    652        1.1       leo #define	SET_5380_REG(rnum,val)	set_falcon_5380_reg(rnum, val)
    653        1.1       leo #define scsi_mach_init(sc)	scsi_falcon_init(sc)
    654        1.1       leo #define scsi_ienable()		scsi_falcon_ienable()
    655        1.1       leo #define scsi_idisable()		scsi_falcon_idisable()
    656        1.1       leo #define	scsi_clr_ipend()	scsi_falcon_clr_ipend()
    657        1.7       leo #define	scsi_ipending()		scsi_falcon_ipending()
    658        1.1       leo #define scsi_dma_setup(r,p,m)	scsi_falcon_dmasetup(r, p, m)
    659        1.1       leo #define wrong_dma_range(r,d)	falcon_wrong_dma_range(r, d)
    660        1.1       leo #define poll_edma(reqp)		falcon_poll_edma(reqp)
    661        1.1       leo #define get_dma_result(r, b)	falcon_get_dma_result(r, b)
    662       1.16       leo #define	can_access_5380()	falcon_can_access_5380()
    663       1.18       leo #define	emulated_dma()		0
    664        1.1       leo 
    665        1.1       leo #define fair_to_keep_dma()	(!st_dmawanted())
    666        1.1       leo #define claimed_dma()		falcon_claimed_dma()
    667        1.1       leo #define reconsider_dma()	falcon_reconsider_dma()
    668        1.1       leo 
    669        1.1       leo #endif /* defined(FALCON_SCSI) && !defined(TT_SCSI) */
    670        1.1       leo 
    671        1.1       leo #if defined(FALCON_SCSI)
    672        1.1       leo 
    673        1.9       leo /*
    674        1.9       leo  * Prototype functions defined below
    675        1.9       leo  */
    676  1.42.44.1      yamt static void	scsi_falcon_init(struct ncr_softc *);
    677  1.42.44.4      yamt static uint8_t	get_falcon_5380_reg(u_short);
    678  1.42.44.1      yamt static void	set_falcon_5380_reg(u_short, u_short);
    679  1.42.44.1      yamt static int	falcon_wrong_dma_range(SC_REQ *, struct dma_chain *);
    680  1.42.44.1      yamt static void	fal1_dma(u_int, u_int, SC_REQ *);
    681  1.42.44.4      yamt static void	scsi_falcon_dmasetup(SC_REQ  *, u_int, uint8_t);
    682  1.42.44.1      yamt static int	falcon_poll_edma(SC_REQ  *);
    683  1.42.44.1      yamt static int	falcon_get_dma_result(SC_REQ  *, u_long *);
    684  1.42.44.4      yamt 
    685  1.42.44.4      yamt int	falcon_can_access_5380(void);
    686  1.42.44.4      yamt void	scsi_falcon_clr_ipend(void);
    687  1.42.44.4      yamt void	scsi_falcon_idisable(void);
    688  1.42.44.4      yamt void	scsi_falcon_ienable(void);
    689  1.42.44.4      yamt int	scsi_falcon_ipending(void);
    690  1.42.44.4      yamt int	falcon_claimed_dma(void);
    691  1.42.44.4      yamt void	falcon_reconsider_dma(void);
    692        1.1       leo 
    693        1.9       leo static void
    694  1.42.44.1      yamt scsi_falcon_init(struct ncr_softc *sc)
    695        1.1       leo {
    696  1.42.44.4      yamt 
    697        1.1       leo 	/*
    698        1.1       leo 	 * Enable disk related interrupts
    699        1.1       leo 	 */
    700        1.1       leo 	MFP->mf_ierb  |= IB_DINT;
    701  1.42.44.4      yamt 	MFP->mf_iprb   = (uint8_t)~IB_DINT;
    702        1.1       leo 	MFP->mf_imrb  |= IB_DINT;
    703        1.1       leo }
    704        1.1       leo 
    705  1.42.44.4      yamt static uint8_t
    706  1.42.44.1      yamt get_falcon_5380_reg(u_short rnum)
    707        1.1       leo {
    708  1.42.44.4      yamt 
    709        1.1       leo 	DMA->dma_mode = DMA_SCSI + rnum;
    710  1.42.44.4      yamt 	return DMA->dma_data;
    711        1.1       leo }
    712        1.1       leo 
    713        1.9       leo static void
    714  1.42.44.1      yamt set_falcon_5380_reg(u_short rnum, u_short val)
    715        1.1       leo {
    716  1.42.44.4      yamt 
    717        1.1       leo 	DMA->dma_mode = DMA_SCSI + rnum;
    718        1.1       leo 	DMA->dma_data = val;
    719        1.1       leo }
    720        1.1       leo 
    721  1.42.44.4      yamt static inline void
    722  1.42.44.1      yamt scsi_falcon_ienable(void)
    723        1.1       leo {
    724  1.42.44.4      yamt 
    725       1.23       leo 	single_inst_bset_b(MFP->mf_imrb, IB_DINT);
    726        1.1       leo }
    727        1.1       leo 
    728  1.42.44.4      yamt static inline void
    729  1.42.44.1      yamt scsi_falcon_idisable(void)
    730        1.1       leo {
    731  1.42.44.4      yamt 
    732       1.23       leo 	single_inst_bclr_b(MFP->mf_imrb, IB_DINT);
    733        1.1       leo }
    734        1.1       leo 
    735  1.42.44.4      yamt static inline void
    736  1.42.44.1      yamt scsi_falcon_clr_ipend(void)
    737        1.1       leo {
    738  1.42.44.4      yamt 	int tmp;
    739        1.1       leo 
    740        1.1       leo 	tmp = get_falcon_5380_reg(NCR5380_IRCV);
    741       1.15       leo 	rem_sicallback((si_farg)ncr_ctrl_intr);
    742        1.1       leo }
    743        1.1       leo 
    744  1.42.44.4      yamt static inline int
    745  1.42.44.1      yamt scsi_falcon_ipending(void)
    746        1.7       leo {
    747  1.42.44.4      yamt 
    748       1.13       leo 	if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
    749       1.13       leo 		/*
    750       1.13       leo 		 *  XXX: When DMA is running, we are only allowed to
    751       1.13       leo 		 *       check the 5380 when DMA _might_ be finished.
    752       1.13       leo 		 */
    753       1.13       leo 		if (MFP->mf_gpip & IO_DINT)
    754  1.42.44.4      yamt 			/* XXX: Actually: we're not allowed to check */
    755  1.42.44.4      yamt 			return 0;
    756       1.16       leo 
    757       1.35       wiz 		/* LWP: 28-06, must be a DMA interrupt! should the
    758       1.35       wiz 		 * ST-DMA unit be taken out of DMA mode?????
    759       1.16       leo 		 */
    760       1.16       leo 		DMA->dma_mode = 0x90;
    761       1.16       leo 
    762       1.13       leo 	}
    763  1.42.44.4      yamt 	return get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET;
    764        1.7       leo }
    765        1.7       leo 
    766        1.9       leo static int
    767  1.42.44.1      yamt falcon_wrong_dma_range(SC_REQ *reqp, struct dma_chain *dm)
    768        1.1       leo {
    769  1.42.44.4      yamt 
    770        1.1       leo 	/*
    771        1.1       leo 	 * Do not allow chains yet! See also comment with
    772        1.1       leo 	 * falcon_poll_edma() !!!
    773        1.1       leo 	 */
    774        1.1       leo 	if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
    775        1.1       leo 		reqp->dr_flag |= DRIVER_BOUNCING;
    776  1.42.44.4      yamt 		return 1;
    777        1.1       leo 	}
    778        1.2       leo 	/*
    779        1.2       leo 	 * Never allow DMA to happen on a Falcon when the transfer
    780        1.2       leo 	 * size is no multiple of 512. This is the transfer unit of the
    781        1.2       leo 	 * ST DMA-controller.
    782        1.2       leo 	 */
    783  1.42.44.4      yamt 	if (dm->dm_count & 511)
    784  1.42.44.4      yamt 		return 1;
    785  1.42.44.4      yamt 	return 0;
    786        1.1       leo }
    787        1.1       leo 
    788        1.1       leo static	int falcon_lock = 0;
    789        1.1       leo 
    790  1.42.44.4      yamt static inline int
    791  1.42.44.1      yamt falcon_claimed_dma(void)
    792        1.1       leo {
    793  1.42.44.4      yamt 
    794       1.11       leo 	if (falcon_lock != DMA_LOCK_GRANT) {
    795       1.11       leo 		if (falcon_lock == DMA_LOCK_REQ) {
    796        1.1       leo 			/*
    797        1.1       leo 			 * DMA access is being claimed.
    798        1.1       leo 			 */
    799  1.42.44.4      yamt 			return 0;
    800        1.1       leo 		}
    801  1.42.44.4      yamt 		if (st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
    802  1.42.44.4      yamt 		    cur_softc, &falcon_lock, 1) == 0)
    803  1.42.44.4      yamt 			return 0;
    804        1.1       leo 	}
    805  1.42.44.4      yamt 	return 1;
    806        1.1       leo }
    807        1.1       leo 
    808  1.42.44.4      yamt static inline void
    809  1.42.44.1      yamt falcon_reconsider_dma(void)
    810        1.1       leo {
    811  1.42.44.4      yamt 
    812        1.1       leo 	if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
    813        1.1       leo 		/*
    814        1.1       leo 		 * No need to keep DMA locked by us as we are not currently
    815        1.1       leo 		 * connected and no disconnected jobs are pending.
    816        1.1       leo 		 */
    817       1.15       leo 		rem_sicallback((si_farg)ncr_ctrl_intr);
    818       1.15       leo 		st_dmafree(cur_softc, &falcon_lock);
    819        1.1       leo 	}
    820        1.1       leo 
    821        1.1       leo 	if (!falcon_lock && (issue_q != NULL)) {
    822        1.1       leo 		/*
    823        1.1       leo 		 * We must (re)claim DMA access as there are jobs
    824        1.1       leo 		 * waiting in the issue queue.
    825        1.1       leo 		 */
    826       1.15       leo 		st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
    827  1.42.44.4      yamt 		    cur_softc, &falcon_lock, 0);
    828        1.1       leo 	}
    829        1.1       leo }
    830        1.1       leo 
    831        1.9       leo static void
    832  1.42.44.1      yamt fal1_dma(u_int dir, u_int nsects, SC_REQ *reqp)
    833        1.1       leo {
    834  1.42.44.4      yamt 
    835        1.1       leo 	dir <<= 8;
    836       1.42  christos 	st_dmaaddr_set((void *)reqp->dm_cur->dm_addr);
    837        1.1       leo 	DMA->dma_mode = 0x90 | dir;
    838        1.1       leo 	DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
    839        1.1       leo 	DMA->dma_mode = 0x90 | dir;
    840        1.1       leo 	DMA->dma_data = nsects;
    841        1.7       leo 	delay(2);	/* _really_ needed (Thomas Gerner) */
    842        1.1       leo 	DMA->dma_mode = 0x10 | dir;
    843        1.1       leo }
    844        1.1       leo 
    845        1.9       leo static void
    846  1.42.44.4      yamt scsi_falcon_dmasetup(SC_REQ *reqp, u_int phase, uint8_t mode)
    847        1.1       leo {
    848  1.42.44.4      yamt 	int nsects = reqp->dm_cur->dm_count / 512; /* XXX */
    849        1.1       leo 
    850        1.1       leo 	/*
    851        1.1       leo 	 * XXX: We should probably clear the fifo before putting the
    852        1.1       leo 	 *      5380 into DMA-mode.
    853        1.1       leo 	 */
    854        1.1       leo 	if (PH_IN(phase)) {
    855        1.1       leo 		set_falcon_5380_reg(NCR5380_ICOM, 0);
    856        1.1       leo 		set_falcon_5380_reg(NCR5380_MODE, mode);
    857        1.1       leo 		set_falcon_5380_reg(NCR5380_IRCV, 0);
    858        1.1       leo 		fal1_dma(0, nsects, reqp);
    859  1.42.44.4      yamt 	} else {
    860        1.1       leo 		set_falcon_5380_reg(NCR5380_MODE, mode);
    861        1.1       leo 		set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
    862        1.1       leo 		set_falcon_5380_reg(NCR5380_DMSTAT, 0);
    863        1.1       leo 		fal1_dma(1, nsects, reqp);
    864        1.1       leo 	}
    865        1.1       leo }
    866        1.1       leo 
    867        1.1       leo static int
    868  1.42.44.1      yamt falcon_poll_edma(SC_REQ *reqp)
    869        1.1       leo {
    870  1.42.44.4      yamt 	int timeout = 9000; /* XXX */
    871        1.1       leo 
    872        1.1       leo 	/*
    873        1.1       leo 	 * Because of the Falcon hardware, it is impossible to reach
    874        1.1       leo 	 * the 5380 while doing DMA-transfers. So we have to rely on
    875        1.1       leo 	 * the interrupt line to determine if DMA-has finished. the
    876        1.1       leo 	 * DMA-controller itself will never fire an interrupt. This means
    877        1.1       leo 	 * that 'broken-up' DMA transfers are not (yet) possible on the
    878        1.1       leo 	 * Falcon.
    879        1.1       leo 	 */
    880        1.1       leo 	for (;;) {
    881        1.1       leo 		delay(20);
    882        1.1       leo 		if (--timeout <= 0) {
    883        1.1       leo 			ncr_tprint(reqp, "Timeout on polled transfer\n");
    884       1.11       leo 			reqp->xs->error = XS_TIMEOUT;
    885  1.42.44.4      yamt 			return 0;
    886        1.1       leo 		}
    887  1.42.44.4      yamt 		if ((MFP->mf_gpip & IO_DINT) == 0)
    888        1.1       leo 			break;
    889        1.1       leo 	}
    890  1.42.44.4      yamt 	return 1;
    891        1.1       leo }
    892        1.1       leo 
    893        1.1       leo static int
    894  1.42.44.1      yamt falcon_get_dma_result(SC_REQ *reqp, u_long *bytes_left)
    895        1.1       leo {
    896        1.1       leo 	int	rv = 0;
    897        1.1       leo 	int	st_dmastat;
    898        1.1       leo 	u_long	bytes_done;
    899        1.1       leo 
    900        1.1       leo 	/*
    901        1.1       leo 	 * Select sector counter register first (See Atari docu.)
    902        1.1       leo 	 */
    903        1.1       leo 	DMA->dma_mode = 0x90;
    904        1.1       leo 	if (!(st_dmastat = DMA->dma_stat) & 0x01) {
    905        1.1       leo 		/*
    906        1.1       leo 		 * Misc. DMA-error according to Atari...
    907        1.1       leo 		 */
    908        1.1       leo 		ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
    909  1.42.44.4      yamt 		    st_dmaaddr_get());
    910        1.1       leo 		reqp->xs->error = XS_DRIVER_STUFFUP;
    911        1.1       leo 		rv = 1;
    912        1.1       leo 	}
    913       1.16       leo 	/*
    914       1.16       leo 	 * Because we NEVER start DMA on the Falcon when the data size
    915       1.16       leo 	 * is not a multiple of 512 bytes, we can safely round down the
    916       1.16       leo 	 * byte count on writes. We need to because in case of a disconnect,
    917       1.16       leo 	 * the DMA has already prefetched the next couple of bytes.
    918       1.16       leo 	 * On read, these byte counts are an error. They are logged and
    919       1.16       leo 	 * should be handled by the mi-part of the driver.
    920       1.16       leo 	 * NOTE: We formerly did this by using the 'byte-count-zero' bit
    921       1.16       leo 	 *       of the DMA controller, but this didn't seem to work???
    922       1.16       leo          *       [lwp 29/06/96]
    923       1.16       leo 	 */
    924       1.16       leo 	bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
    925       1.16       leo 	if (bytes_done & 511) {
    926       1.16       leo 		if (PH_IN(reqp->phase)) {
    927       1.16       leo 			ncr_tprint(reqp, "Byte count on read not a multiple "
    928  1.42.44.4      yamt 			    "of 512 (%ld)\n", bytes_done);
    929        1.1       leo 		}
    930       1.16       leo 		bytes_done &= ~511;
    931        1.1       leo 	}
    932       1.16       leo 	if ((*bytes_left = reqp->dm_cur->dm_count - bytes_done) == 0)
    933        1.1       leo 		rv = 1;
    934  1.42.44.4      yamt 	return rv;
    935        1.1       leo }
    936        1.1       leo 
    937       1.16       leo static int
    938       1.16       leo falcon_can_access_5380()
    939       1.16       leo {
    940  1.42.44.4      yamt 
    941  1.42.44.4      yamt 	if (connected && (connected->dr_flag & DRIVER_IN_DMA) &&
    942  1.42.44.4      yamt 	    (MFP->mf_gpip & IO_DINT))
    943  1.42.44.4      yamt 		return 0;
    944  1.42.44.4      yamt 	return 1;
    945       1.16       leo }
    946       1.16       leo 
    947        1.1       leo #endif /* defined(FALCON_SCSI) */
    948        1.1       leo 
    949        1.1       leo #if defined(TT_SCSI) && defined(FALCON_SCSI)
    950        1.1       leo /*
    951        1.1       leo  * Define some functions to support _both_ TT and Falcon SCSI
    952        1.1       leo  */
    953        1.1       leo 
    954        1.1       leo /*
    955        1.9       leo  * The prototypes first...
    956        1.9       leo  */
    957  1.42.44.1      yamt static void	scsi_mach_init(struct ncr_softc *);
    958  1.42.44.4      yamt 
    959  1.42.44.4      yamt void	scsi_ienable(void);
    960  1.42.44.4      yamt void	scsi_idisable(void);
    961  1.42.44.4      yamt void	scsi_clr_ipend(void);
    962  1.42.44.4      yamt int	scsi_ipending(void);
    963  1.42.44.4      yamt void	scsi_dma_setup(SC_REQ *, u_int, uint8_t);
    964  1.42.44.4      yamt int	wrong_dma_range(SC_REQ *, struct dma_chain *);
    965  1.42.44.4      yamt int	poll_edma(SC_REQ *);
    966  1.42.44.4      yamt int	get_dma_result(SC_REQ *, u_long *);
    967  1.42.44.4      yamt int	can_access_5380(void);
    968        1.9       leo 
    969        1.9       leo /*
    970        1.1       leo  * Register access will be done through the following 2 function pointers.
    971        1.1       leo  */
    972  1.42.44.4      yamt static uint8_t	(*get_5380_reg)(u_short);
    973  1.42.44.1      yamt static void	(*set_5380_reg)(u_short, u_short);
    974        1.1       leo 
    975        1.1       leo #define	GET_5380_REG	(*get_5380_reg)
    976        1.1       leo #define	SET_5380_REG	(*set_5380_reg)
    977        1.1       leo 
    978        1.9       leo static void
    979  1.42.44.1      yamt scsi_mach_init(struct ncr_softc *sc)
    980        1.1       leo {
    981  1.42.44.4      yamt 
    982        1.1       leo 	if (machineid & ATARI_FALCON) {
    983        1.1       leo 		get_5380_reg = get_falcon_5380_reg;
    984        1.1       leo 		set_5380_reg = set_falcon_5380_reg;
    985        1.1       leo 		scsi_falcon_init(sc);
    986  1.42.44.4      yamt 	} else {
    987        1.1       leo 		get_5380_reg = get_tt_5380_reg;
    988        1.1       leo 		set_5380_reg = set_tt_5380_reg;
    989        1.1       leo 		scsi_tt_init(sc);
    990        1.1       leo 	}
    991        1.1       leo }
    992        1.1       leo 
    993  1.42.44.4      yamt static inline void
    994  1.42.44.1      yamt scsi_ienable(void)
    995        1.1       leo {
    996  1.42.44.4      yamt 
    997        1.1       leo 	if (machineid & ATARI_FALCON)
    998        1.1       leo 		scsi_falcon_ienable();
    999  1.42.44.4      yamt 	else
   1000  1.42.44.4      yamt 		scsi_tt_ienable();
   1001        1.1       leo }
   1002        1.1       leo 
   1003  1.42.44.4      yamt static inline void
   1004  1.42.44.1      yamt scsi_idisable(void)
   1005        1.1       leo {
   1006  1.42.44.4      yamt 
   1007        1.1       leo 	if (machineid & ATARI_FALCON)
   1008        1.1       leo 		scsi_falcon_idisable();
   1009  1.42.44.4      yamt 	else
   1010  1.42.44.4      yamt 		scsi_tt_idisable();
   1011        1.1       leo }
   1012        1.1       leo 
   1013  1.42.44.4      yamt static inline void
   1014  1.42.44.1      yamt scsi_clr_ipend(void)
   1015        1.1       leo {
   1016  1.42.44.4      yamt 
   1017        1.1       leo 	if (machineid & ATARI_FALCON)
   1018        1.1       leo 		scsi_falcon_clr_ipend();
   1019  1.42.44.4      yamt 	else
   1020  1.42.44.4      yamt 		scsi_tt_clr_ipend();
   1021        1.7       leo }
   1022        1.7       leo 
   1023  1.42.44.4      yamt static inline int
   1024  1.42.44.1      yamt scsi_ipending(void)
   1025        1.7       leo {
   1026  1.42.44.4      yamt 
   1027        1.7       leo 	if (machineid & ATARI_FALCON)
   1028  1.42.44.4      yamt 		return scsi_falcon_ipending();
   1029  1.42.44.4      yamt 	else
   1030  1.42.44.4      yamt 		return GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET;
   1031        1.1       leo }
   1032        1.1       leo 
   1033  1.42.44.4      yamt static inline void
   1034  1.42.44.4      yamt scsi_dma_setup(SC_REQ *reqp, u_int phase, uint8_t mbase)
   1035        1.1       leo {
   1036  1.42.44.4      yamt 
   1037        1.1       leo 	if (machineid & ATARI_FALCON)
   1038        1.1       leo 		scsi_falcon_dmasetup(reqp, phase, mbase);
   1039  1.42.44.4      yamt 	else
   1040  1.42.44.4      yamt 		scsi_tt_dmasetup(reqp, phase, mbase);
   1041        1.1       leo }
   1042        1.1       leo 
   1043  1.42.44.4      yamt static inline int
   1044  1.42.44.1      yamt wrong_dma_range(SC_REQ *reqp, struct dma_chain *dm)
   1045        1.1       leo {
   1046  1.42.44.4      yamt 
   1047        1.1       leo 	if (machineid & ATARI_FALCON)
   1048  1.42.44.4      yamt 		return falcon_wrong_dma_range(reqp, dm);
   1049  1.42.44.4      yamt 	else
   1050  1.42.44.4      yamt 		return tt_wrong_dma_range(reqp, dm);
   1051        1.1       leo }
   1052        1.1       leo 
   1053  1.42.44.4      yamt static inline int
   1054  1.42.44.1      yamt poll_edma(SC_REQ *reqp)
   1055        1.1       leo {
   1056  1.42.44.4      yamt 
   1057        1.1       leo 	if (machineid & ATARI_FALCON)
   1058  1.42.44.4      yamt 		return falcon_poll_edma(reqp);
   1059  1.42.44.4      yamt 	else
   1060  1.42.44.4      yamt 		return tt_poll_edma(reqp);
   1061        1.1       leo }
   1062        1.1       leo 
   1063  1.42.44.4      yamt static inline int
   1064  1.42.44.1      yamt get_dma_result(SC_REQ *reqp, u_long *bytes_left)
   1065        1.1       leo {
   1066  1.42.44.4      yamt 
   1067        1.1       leo 	if (machineid & ATARI_FALCON)
   1068  1.42.44.4      yamt 		return falcon_get_dma_result(reqp, bytes_left);
   1069  1.42.44.4      yamt 	else
   1070  1.42.44.4      yamt 		return tt_get_dma_result(reqp, bytes_left);
   1071        1.1       leo }
   1072        1.1       leo 
   1073  1.42.44.4      yamt static inline int
   1074  1.42.44.4      yamt can_access_5380(void)
   1075       1.16       leo {
   1076  1.42.44.4      yamt 
   1077       1.16       leo 	if (machineid & ATARI_FALCON)
   1078  1.42.44.4      yamt 		return falcon_can_access_5380();
   1079  1.42.44.4      yamt 	return 1;
   1080       1.16       leo }
   1081       1.18       leo 
   1082       1.18       leo #define emulated_dma()		((machineid & ATARI_HADES) ? 1 : 0)
   1083       1.16       leo 
   1084        1.1       leo /*
   1085        1.1       leo  * Locking stuff. All turns into NOP's on the TT.
   1086        1.1       leo  */
   1087  1.42.44.4      yamt #define	fair_to_keep_dma()						\
   1088  1.42.44.4      yamt 	((machineid & ATARI_FALCON) ? !st_dmawanted() : 1)
   1089  1.42.44.4      yamt #define	claimed_dma()							\
   1090  1.42.44.4      yamt 	((machineid & ATARI_FALCON) ? falcon_claimed_dma() : 1)
   1091  1.42.44.4      yamt #define reconsider_dma()						\
   1092  1.42.44.4      yamt 	do {								\
   1093  1.42.44.4      yamt 		if (machineid & ATARI_FALCON)				\
   1094  1.42.44.4      yamt 			falcon_reconsider_dma();			\
   1095  1.42.44.4      yamt 	} while (/* CONSTCOND */ 0)
   1096        1.1       leo #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
   1097        1.1       leo 
   1098        1.1       leo /**********************************************
   1099        1.1       leo  * Functions present for both TT and Falcon.  *
   1100        1.1       leo  **********************************************/
   1101        1.1       leo /*
   1102        1.1       leo  * Our autoconfig matching function
   1103        1.1       leo  */
   1104        1.1       leo static int
   1105  1.42.44.4      yamt machine_match(struct device *pdp, void *match, void *auxp, struct cfdriver *cd)
   1106        1.1       leo {
   1107  1.42.44.4      yamt 	static int we_matched = 0; /* Only one unit	*/
   1108       1.10   thorpej 
   1109       1.31       leo 	if (strcmp(auxp, cd->cd_name) || we_matched)
   1110  1.42.44.4      yamt 		return 0;
   1111       1.31       leo 
   1112       1.31       leo 	we_matched = 1;
   1113  1.42.44.4      yamt 	return 1;
   1114        1.1       leo }
   1115        1.1       leo 
   1116        1.1       leo /*
   1117        1.1       leo  * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
   1118        1.1       leo  * pool. Allocation here is both contiguous and in the lower 16Mb of
   1119        1.1       leo  * the address space. Thus being DMA-able for all controllers.
   1120        1.1       leo  */
   1121  1.42.44.4      yamt static uint8_t *
   1122        1.8       leo alloc_bounceb(u_long len)
   1123        1.1       leo {
   1124  1.42.44.4      yamt 	void *tmp;
   1125        1.1       leo 
   1126  1.42.44.4      yamt 	return (uint8_t *)alloc_stmem(len, &tmp);
   1127        1.1       leo }
   1128        1.1       leo 
   1129        1.1       leo static void
   1130  1.42.44.4      yamt free_bounceb(uint8_t *bounceb)
   1131        1.1       leo {
   1132  1.42.44.4      yamt 
   1133        1.1       leo 	free_stmem(bounceb);
   1134        1.1       leo }
   1135        1.1       leo 
   1136        1.1       leo /*
   1137        1.1       leo  * 5380 interrupt.
   1138        1.1       leo  */
   1139        1.8       leo void
   1140        1.8       leo scsi_ctrl(int sr)
   1141        1.1       leo {
   1142  1.42.44.4      yamt 
   1143        1.1       leo 	if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
   1144        1.1       leo 		scsi_idisable();
   1145  1.42.44.4      yamt 		add_sicallback((si_farg)ncr_ctrl_intr, (void *)cur_softc, 0);
   1146        1.1       leo 	}
   1147        1.1       leo }
   1148        1.1       leo 
   1149        1.1       leo /*
   1150        1.1       leo  * DMA controller interrupt
   1151        1.1       leo  */
   1152        1.8       leo void
   1153        1.8       leo scsi_dma(int sr)
   1154        1.1       leo {
   1155        1.1       leo 	SC_REQ	*reqp;
   1156        1.1       leo 
   1157        1.1       leo 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
   1158        1.1       leo 		scsi_idisable();
   1159  1.42.44.4      yamt 		add_sicallback((si_farg)ncr_dma_intr, (void *)cur_softc, 0);
   1160        1.1       leo 	}
   1161        1.1       leo }
   1162        1.1       leo 
   1163        1.1       leo /*
   1164        1.1       leo  * Last but not least... Include the general driver code
   1165        1.1       leo  */
   1166        1.6   mycroft #include <atari/dev/ncr5380.c>
   1167