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