1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Loosely based on the work of Robert De Vries' team and added:
7*4882a593Smuzhiyun * - working real DMA
8*4882a593Smuzhiyun * - Falcon support (untested yet!) ++bjoern fixed and now it works
9*4882a593Smuzhiyun * - lots of extensions and bug fixes.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * This file is subject to the terms and conditions of the GNU General Public
12*4882a593Smuzhiyun * License. See the file COPYING in the main directory of this archive
13*4882a593Smuzhiyun * for more details.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun * Notes for Falcon SCSI DMA
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * The 5380 device is one of several that all share the DMA chip. Hence
21*4882a593Smuzhiyun * "locking" and "unlocking" access to this chip is required.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Two possible schemes for ST DMA acquisition by atari_scsi are:
24*4882a593Smuzhiyun * 1) The lock is taken for each command separately (i.e. can_queue == 1).
25*4882a593Smuzhiyun * 2) The lock is taken when the first command arrives and released
26*4882a593Smuzhiyun * when the last command is finished (i.e. can_queue > 1).
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * The first alternative limits SCSI bus utilization, since interleaving
29*4882a593Smuzhiyun * commands is not possible. The second gives better performance but is
30*4882a593Smuzhiyun * unfair to other drivers needing to use the ST DMA chip. In order to
31*4882a593Smuzhiyun * allow the IDE and floppy drivers equal access to the ST DMA chip
32*4882a593Smuzhiyun * the default is can_queue == 1.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include <linux/module.h>
36*4882a593Smuzhiyun #include <linux/types.h>
37*4882a593Smuzhiyun #include <linux/blkdev.h>
38*4882a593Smuzhiyun #include <linux/interrupt.h>
39*4882a593Smuzhiyun #include <linux/init.h>
40*4882a593Smuzhiyun #include <linux/nvram.h>
41*4882a593Smuzhiyun #include <linux/bitops.h>
42*4882a593Smuzhiyun #include <linux/wait.h>
43*4882a593Smuzhiyun #include <linux/platform_device.h>
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #include <asm/setup.h>
46*4882a593Smuzhiyun #include <asm/atarihw.h>
47*4882a593Smuzhiyun #include <asm/atariints.h>
48*4882a593Smuzhiyun #include <asm/atari_stdma.h>
49*4882a593Smuzhiyun #include <asm/atari_stram.h>
50*4882a593Smuzhiyun #include <asm/io.h>
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #include <scsi/scsi_host.h>
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #define DMA_MIN_SIZE 32
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* Definitions for the core NCR5380 driver. */
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun #define NCR5380_implementation_fields /* none */
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun static u8 (*atari_scsi_reg_read)(unsigned int);
61*4882a593Smuzhiyun static void (*atari_scsi_reg_write)(unsigned int, u8);
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun #define NCR5380_read(reg) atari_scsi_reg_read(reg)
64*4882a593Smuzhiyun #define NCR5380_write(reg, value) atari_scsi_reg_write(reg, value)
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #define NCR5380_queue_command atari_scsi_queue_command
67*4882a593Smuzhiyun #define NCR5380_abort atari_scsi_abort
68*4882a593Smuzhiyun #define NCR5380_info atari_scsi_info
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun #define NCR5380_dma_xfer_len atari_scsi_dma_xfer_len
71*4882a593Smuzhiyun #define NCR5380_dma_recv_setup atari_scsi_dma_recv_setup
72*4882a593Smuzhiyun #define NCR5380_dma_send_setup atari_scsi_dma_send_setup
73*4882a593Smuzhiyun #define NCR5380_dma_residual atari_scsi_dma_residual
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun #define NCR5380_acquire_dma_irq(instance) falcon_get_lock(instance)
76*4882a593Smuzhiyun #define NCR5380_release_dma_irq(instance) falcon_release_lock()
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #include "NCR5380.h"
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun #define IS_A_TT() ATARIHW_PRESENT(TT_SCSI)
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun #define SCSI_DMA_WRITE_P(elt,val) \
84*4882a593Smuzhiyun do { \
85*4882a593Smuzhiyun unsigned long v = val; \
86*4882a593Smuzhiyun tt_scsi_dma.elt##_lo = v & 0xff; \
87*4882a593Smuzhiyun v >>= 8; \
88*4882a593Smuzhiyun tt_scsi_dma.elt##_lmd = v & 0xff; \
89*4882a593Smuzhiyun v >>= 8; \
90*4882a593Smuzhiyun tt_scsi_dma.elt##_hmd = v & 0xff; \
91*4882a593Smuzhiyun v >>= 8; \
92*4882a593Smuzhiyun tt_scsi_dma.elt##_hi = v & 0xff; \
93*4882a593Smuzhiyun } while(0)
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun #define SCSI_DMA_READ_P(elt) \
96*4882a593Smuzhiyun (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \
97*4882a593Smuzhiyun (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \
98*4882a593Smuzhiyun (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \
99*4882a593Smuzhiyun (unsigned long)tt_scsi_dma.elt##_lo)
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun
SCSI_DMA_SETADR(unsigned long adr)102*4882a593Smuzhiyun static inline void SCSI_DMA_SETADR(unsigned long adr)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun st_dma.dma_lo = (unsigned char)adr;
105*4882a593Smuzhiyun MFPDELAY();
106*4882a593Smuzhiyun adr >>= 8;
107*4882a593Smuzhiyun st_dma.dma_md = (unsigned char)adr;
108*4882a593Smuzhiyun MFPDELAY();
109*4882a593Smuzhiyun adr >>= 8;
110*4882a593Smuzhiyun st_dma.dma_hi = (unsigned char)adr;
111*4882a593Smuzhiyun MFPDELAY();
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
SCSI_DMA_GETADR(void)114*4882a593Smuzhiyun static inline unsigned long SCSI_DMA_GETADR(void)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun unsigned long adr;
117*4882a593Smuzhiyun adr = st_dma.dma_lo;
118*4882a593Smuzhiyun MFPDELAY();
119*4882a593Smuzhiyun adr |= (st_dma.dma_md & 0xff) << 8;
120*4882a593Smuzhiyun MFPDELAY();
121*4882a593Smuzhiyun adr |= (st_dma.dma_hi & 0xff) << 16;
122*4882a593Smuzhiyun MFPDELAY();
123*4882a593Smuzhiyun return adr;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun static void atari_scsi_fetch_restbytes(void);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun static unsigned long atari_dma_residual, atari_dma_startaddr;
129*4882a593Smuzhiyun static short atari_dma_active;
130*4882a593Smuzhiyun /* pointer to the dribble buffer */
131*4882a593Smuzhiyun static char *atari_dma_buffer;
132*4882a593Smuzhiyun /* precalculated physical address of the dribble buffer */
133*4882a593Smuzhiyun static unsigned long atari_dma_phys_buffer;
134*4882a593Smuzhiyun /* != 0 tells the Falcon int handler to copy data from the dribble buffer */
135*4882a593Smuzhiyun static char *atari_dma_orig_addr;
136*4882a593Smuzhiyun /* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
137*4882a593Smuzhiyun * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
138*4882a593Smuzhiyun * cases where requests to physical contiguous buffers have been merged, this
139*4882a593Smuzhiyun * request is <= 4k (one page). So I don't think we have to split transfers
140*4882a593Smuzhiyun * just due to this buffer size...
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun #define STRAM_BUFFER_SIZE (4096)
143*4882a593Smuzhiyun /* mask for address bits that can't be used with the ST-DMA */
144*4882a593Smuzhiyun static unsigned long atari_dma_stram_mask;
145*4882a593Smuzhiyun #define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0)
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun static int setup_can_queue = -1;
148*4882a593Smuzhiyun module_param(setup_can_queue, int, 0);
149*4882a593Smuzhiyun static int setup_cmd_per_lun = -1;
150*4882a593Smuzhiyun module_param(setup_cmd_per_lun, int, 0);
151*4882a593Smuzhiyun static int setup_sg_tablesize = -1;
152*4882a593Smuzhiyun module_param(setup_sg_tablesize, int, 0);
153*4882a593Smuzhiyun static int setup_hostid = -1;
154*4882a593Smuzhiyun module_param(setup_hostid, int, 0);
155*4882a593Smuzhiyun static int setup_toshiba_delay = -1;
156*4882a593Smuzhiyun module_param(setup_toshiba_delay, int, 0);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun
scsi_dma_is_ignored_buserr(unsigned char dma_stat)159*4882a593Smuzhiyun static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun int i;
162*4882a593Smuzhiyun unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if (dma_stat & 0x01) {
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /* A bus error happens when DMA-ing from the last page of a
167*4882a593Smuzhiyun * physical memory chunk (DMA prefetch!), but that doesn't hurt.
168*4882a593Smuzhiyun * Check for this case:
169*4882a593Smuzhiyun */
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun for (i = 0; i < m68k_num_memory; ++i) {
172*4882a593Smuzhiyun end_addr = m68k_memory[i].addr + m68k_memory[i].size;
173*4882a593Smuzhiyun if (end_addr <= addr && addr <= end_addr + 4)
174*4882a593Smuzhiyun return 1;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun return 0;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun
scsi_tt_intr(int irq,void * dev)181*4882a593Smuzhiyun static irqreturn_t scsi_tt_intr(int irq, void *dev)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun struct Scsi_Host *instance = dev;
184*4882a593Smuzhiyun struct NCR5380_hostdata *hostdata = shost_priv(instance);
185*4882a593Smuzhiyun int dma_stat;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun dma_stat = tt_scsi_dma.dma_ctrl;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun dsprintk(NDEBUG_INTR, instance, "NCR5380 interrupt, DMA status = %02x\n",
190*4882a593Smuzhiyun dma_stat & 0xff);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* Look if it was the DMA that has interrupted: First possibility
193*4882a593Smuzhiyun * is that a bus error occurred...
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun if (dma_stat & 0x80) {
196*4882a593Smuzhiyun if (!scsi_dma_is_ignored_buserr(dma_stat)) {
197*4882a593Smuzhiyun printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
198*4882a593Smuzhiyun SCSI_DMA_READ_P(dma_addr));
199*4882a593Smuzhiyun printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /* If the DMA is active but not finished, we have the case
204*4882a593Smuzhiyun * that some other 5380 interrupt occurred within the DMA transfer.
205*4882a593Smuzhiyun * This means we have residual bytes, if the desired end address
206*4882a593Smuzhiyun * is not yet reached. Maybe we have to fetch some bytes from the
207*4882a593Smuzhiyun * rest data register, too. The residual must be calculated from
208*4882a593Smuzhiyun * the address pointer, not the counter register, because only the
209*4882a593Smuzhiyun * addr reg counts bytes not yet written and pending in the rest
210*4882a593Smuzhiyun * data reg!
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
213*4882a593Smuzhiyun atari_dma_residual = hostdata->dma_len -
214*4882a593Smuzhiyun (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
217*4882a593Smuzhiyun atari_dma_residual);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun if ((signed int)atari_dma_residual < 0)
220*4882a593Smuzhiyun atari_dma_residual = 0;
221*4882a593Smuzhiyun if ((dma_stat & 1) == 0) {
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun * After read operations, we maybe have to
224*4882a593Smuzhiyun * transport some rest bytes
225*4882a593Smuzhiyun */
226*4882a593Smuzhiyun atari_scsi_fetch_restbytes();
227*4882a593Smuzhiyun } else {
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * There seems to be a nasty bug in some SCSI-DMA/NCR
230*4882a593Smuzhiyun * combinations: If a target disconnects while a write
231*4882a593Smuzhiyun * operation is going on, the address register of the
232*4882a593Smuzhiyun * DMA may be a few bytes farer than it actually read.
233*4882a593Smuzhiyun * This is probably due to DMA prefetching and a delay
234*4882a593Smuzhiyun * between DMA and NCR. Experiments showed that the
235*4882a593Smuzhiyun * dma_addr is 9 bytes to high, but this could vary.
236*4882a593Smuzhiyun * The problem is, that the residual is thus calculated
237*4882a593Smuzhiyun * wrong and the next transfer will start behind where
238*4882a593Smuzhiyun * it should. So we round up the residual to the next
239*4882a593Smuzhiyun * multiple of a sector size, if it isn't already a
240*4882a593Smuzhiyun * multiple and the originally expected transfer size
241*4882a593Smuzhiyun * was. The latter condition is there to ensure that
242*4882a593Smuzhiyun * the correction is taken only for "real" data
243*4882a593Smuzhiyun * transfers and not for, e.g., the parameters of some
244*4882a593Smuzhiyun * other command. These shouldn't disconnect anyway.
245*4882a593Smuzhiyun */
246*4882a593Smuzhiyun if (atari_dma_residual & 0x1ff) {
247*4882a593Smuzhiyun dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
248*4882a593Smuzhiyun "difference %ld bytes\n",
249*4882a593Smuzhiyun 512 - (atari_dma_residual & 0x1ff));
250*4882a593Smuzhiyun atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun tt_scsi_dma.dma_ctrl = 0;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /* If the DMA is finished, fetch the rest bytes and turn it off */
257*4882a593Smuzhiyun if (dma_stat & 0x40) {
258*4882a593Smuzhiyun atari_dma_residual = 0;
259*4882a593Smuzhiyun if ((dma_stat & 1) == 0)
260*4882a593Smuzhiyun atari_scsi_fetch_restbytes();
261*4882a593Smuzhiyun tt_scsi_dma.dma_ctrl = 0;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun NCR5380_intr(irq, dev);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun return IRQ_HANDLED;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun
scsi_falcon_intr(int irq,void * dev)270*4882a593Smuzhiyun static irqreturn_t scsi_falcon_intr(int irq, void *dev)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun struct Scsi_Host *instance = dev;
273*4882a593Smuzhiyun struct NCR5380_hostdata *hostdata = shost_priv(instance);
274*4882a593Smuzhiyun int dma_stat;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /* Turn off DMA and select sector counter register before
277*4882a593Smuzhiyun * accessing the status register (Atari recommendation!)
278*4882a593Smuzhiyun */
279*4882a593Smuzhiyun st_dma.dma_mode_status = 0x90;
280*4882a593Smuzhiyun dma_stat = st_dma.dma_mode_status;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /* Bit 0 indicates some error in the DMA process... don't know
283*4882a593Smuzhiyun * what happened exactly (no further docu).
284*4882a593Smuzhiyun */
285*4882a593Smuzhiyun if (!(dma_stat & 0x01)) {
286*4882a593Smuzhiyun /* DMA error */
287*4882a593Smuzhiyun printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /* If the DMA was active, but now bit 1 is not clear, it is some
291*4882a593Smuzhiyun * other 5380 interrupt that finishes the DMA transfer. We have to
292*4882a593Smuzhiyun * calculate the number of residual bytes and give a warning if
293*4882a593Smuzhiyun * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
294*4882a593Smuzhiyun */
295*4882a593Smuzhiyun if (atari_dma_active && (dma_stat & 0x02)) {
296*4882a593Smuzhiyun unsigned long transferred;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
299*4882a593Smuzhiyun /* The ST-DMA address is incremented in 2-byte steps, but the
300*4882a593Smuzhiyun * data are written only in 16-byte chunks. If the number of
301*4882a593Smuzhiyun * transferred bytes is not divisible by 16, the remainder is
302*4882a593Smuzhiyun * lost somewhere in outer space.
303*4882a593Smuzhiyun */
304*4882a593Smuzhiyun if (transferred & 15)
305*4882a593Smuzhiyun printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
306*4882a593Smuzhiyun "ST-DMA fifo\n", transferred & 15);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun atari_dma_residual = hostdata->dma_len - transferred;
309*4882a593Smuzhiyun dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
310*4882a593Smuzhiyun atari_dma_residual);
311*4882a593Smuzhiyun } else
312*4882a593Smuzhiyun atari_dma_residual = 0;
313*4882a593Smuzhiyun atari_dma_active = 0;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun if (atari_dma_orig_addr) {
316*4882a593Smuzhiyun /* If the dribble buffer was used on a read operation, copy the DMA-ed
317*4882a593Smuzhiyun * data to the original destination address.
318*4882a593Smuzhiyun */
319*4882a593Smuzhiyun memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
320*4882a593Smuzhiyun hostdata->dma_len - atari_dma_residual);
321*4882a593Smuzhiyun atari_dma_orig_addr = NULL;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun NCR5380_intr(irq, dev);
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun return IRQ_HANDLED;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun
atari_scsi_fetch_restbytes(void)330*4882a593Smuzhiyun static void atari_scsi_fetch_restbytes(void)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun int nr;
333*4882a593Smuzhiyun char *src, *dst;
334*4882a593Smuzhiyun unsigned long phys_dst;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun /* fetch rest bytes in the DMA register */
337*4882a593Smuzhiyun phys_dst = SCSI_DMA_READ_P(dma_addr);
338*4882a593Smuzhiyun nr = phys_dst & 3;
339*4882a593Smuzhiyun if (nr) {
340*4882a593Smuzhiyun /* there are 'nr' bytes left for the last long address
341*4882a593Smuzhiyun before the DMA pointer */
342*4882a593Smuzhiyun phys_dst ^= nr;
343*4882a593Smuzhiyun dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
344*4882a593Smuzhiyun nr, phys_dst);
345*4882a593Smuzhiyun /* The content of the DMA pointer is a physical address! */
346*4882a593Smuzhiyun dst = phys_to_virt(phys_dst);
347*4882a593Smuzhiyun dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
348*4882a593Smuzhiyun for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
349*4882a593Smuzhiyun *dst++ = *src++;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /* This function releases the lock on the DMA chip if there is no
355*4882a593Smuzhiyun * connected command and the disconnected queue is empty.
356*4882a593Smuzhiyun */
357*4882a593Smuzhiyun
falcon_release_lock(void)358*4882a593Smuzhiyun static void falcon_release_lock(void)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun if (IS_A_TT())
361*4882a593Smuzhiyun return;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if (stdma_is_locked_by(scsi_falcon_intr))
364*4882a593Smuzhiyun stdma_release();
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /* This function manages the locking of the ST-DMA.
368*4882a593Smuzhiyun * If the DMA isn't locked already for SCSI, it tries to lock it by
369*4882a593Smuzhiyun * calling stdma_lock(). But if the DMA is locked by the SCSI code and
370*4882a593Smuzhiyun * there are other drivers waiting for the chip, we do not issue the
371*4882a593Smuzhiyun * command immediately but tell the SCSI mid-layer to defer.
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun
falcon_get_lock(struct Scsi_Host * instance)374*4882a593Smuzhiyun static int falcon_get_lock(struct Scsi_Host *instance)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun if (IS_A_TT())
377*4882a593Smuzhiyun return 1;
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun if (stdma_is_locked_by(scsi_falcon_intr) &&
380*4882a593Smuzhiyun instance->hostt->can_queue > 1)
381*4882a593Smuzhiyun return 1;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun if (in_interrupt())
384*4882a593Smuzhiyun return stdma_try_lock(scsi_falcon_intr, instance);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun stdma_lock(scsi_falcon_intr, instance);
387*4882a593Smuzhiyun return 1;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun #ifndef MODULE
atari_scsi_setup(char * str)391*4882a593Smuzhiyun static int __init atari_scsi_setup(char *str)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun /* Format of atascsi parameter is:
394*4882a593Smuzhiyun * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
395*4882a593Smuzhiyun * Defaults depend on TT or Falcon, determined at run time.
396*4882a593Smuzhiyun * Negative values mean don't change.
397*4882a593Smuzhiyun */
398*4882a593Smuzhiyun int ints[8];
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun get_options(str, ARRAY_SIZE(ints), ints);
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun if (ints[0] < 1) {
403*4882a593Smuzhiyun printk("atari_scsi_setup: no arguments!\n");
404*4882a593Smuzhiyun return 0;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun if (ints[0] >= 1)
407*4882a593Smuzhiyun setup_can_queue = ints[1];
408*4882a593Smuzhiyun if (ints[0] >= 2)
409*4882a593Smuzhiyun setup_cmd_per_lun = ints[2];
410*4882a593Smuzhiyun if (ints[0] >= 3)
411*4882a593Smuzhiyun setup_sg_tablesize = ints[3];
412*4882a593Smuzhiyun if (ints[0] >= 4)
413*4882a593Smuzhiyun setup_hostid = ints[4];
414*4882a593Smuzhiyun /* ints[5] (use_tagged_queuing) is ignored */
415*4882a593Smuzhiyun /* ints[6] (use_pdma) is ignored */
416*4882a593Smuzhiyun if (ints[0] >= 7)
417*4882a593Smuzhiyun setup_toshiba_delay = ints[7];
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun return 1;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun __setup("atascsi=", atari_scsi_setup);
423*4882a593Smuzhiyun #endif /* !MODULE */
424*4882a593Smuzhiyun
atari_scsi_dma_setup(struct NCR5380_hostdata * hostdata,void * data,unsigned long count,int dir)425*4882a593Smuzhiyun static unsigned long atari_scsi_dma_setup(struct NCR5380_hostdata *hostdata,
426*4882a593Smuzhiyun void *data, unsigned long count,
427*4882a593Smuzhiyun int dir)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun unsigned long addr = virt_to_phys(data);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, dir = %d\n",
432*4882a593Smuzhiyun hostdata->host->host_no, data, addr, count, dir);
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun if (!IS_A_TT() && !STRAM_ADDR(addr)) {
435*4882a593Smuzhiyun /* If we have a non-DMAable address on a Falcon, use the dribble
436*4882a593Smuzhiyun * buffer; 'orig_addr' != 0 in the read case tells the interrupt
437*4882a593Smuzhiyun * handler to copy data from the dribble buffer to the originally
438*4882a593Smuzhiyun * wanted address.
439*4882a593Smuzhiyun */
440*4882a593Smuzhiyun if (dir)
441*4882a593Smuzhiyun memcpy(atari_dma_buffer, data, count);
442*4882a593Smuzhiyun else
443*4882a593Smuzhiyun atari_dma_orig_addr = data;
444*4882a593Smuzhiyun addr = atari_dma_phys_buffer;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun atari_dma_startaddr = addr; /* Needed for calculating residual later. */
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /* Cache cleanup stuff: On writes, push any dirty cache out before sending
450*4882a593Smuzhiyun * it to the peripheral. (Must be done before DMA setup, since at least
451*4882a593Smuzhiyun * the ST-DMA begins to fill internal buffers right after setup. For
452*4882a593Smuzhiyun * reads, invalidate any cache, may be altered after DMA without CPU
453*4882a593Smuzhiyun * knowledge.
454*4882a593Smuzhiyun *
455*4882a593Smuzhiyun * ++roman: For the Medusa, there's no need at all for that cache stuff,
456*4882a593Smuzhiyun * because the hardware does bus snooping (fine!).
457*4882a593Smuzhiyun */
458*4882a593Smuzhiyun dma_cache_maintenance(addr, count, dir);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun if (IS_A_TT()) {
461*4882a593Smuzhiyun tt_scsi_dma.dma_ctrl = dir;
462*4882a593Smuzhiyun SCSI_DMA_WRITE_P(dma_addr, addr);
463*4882a593Smuzhiyun SCSI_DMA_WRITE_P(dma_cnt, count);
464*4882a593Smuzhiyun tt_scsi_dma.dma_ctrl = dir | 2;
465*4882a593Smuzhiyun } else { /* ! IS_A_TT */
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun /* set address */
468*4882a593Smuzhiyun SCSI_DMA_SETADR(addr);
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun /* toggle direction bit to clear FIFO and set DMA direction */
471*4882a593Smuzhiyun dir <<= 8;
472*4882a593Smuzhiyun st_dma.dma_mode_status = 0x90 | dir;
473*4882a593Smuzhiyun st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
474*4882a593Smuzhiyun st_dma.dma_mode_status = 0x90 | dir;
475*4882a593Smuzhiyun udelay(40);
476*4882a593Smuzhiyun /* On writes, round up the transfer length to the next multiple of 512
477*4882a593Smuzhiyun * (see also comment at atari_dma_xfer_len()). */
478*4882a593Smuzhiyun st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
479*4882a593Smuzhiyun udelay(40);
480*4882a593Smuzhiyun st_dma.dma_mode_status = 0x10 | dir;
481*4882a593Smuzhiyun udelay(40);
482*4882a593Smuzhiyun /* need not restore value of dir, only boolean value is tested */
483*4882a593Smuzhiyun atari_dma_active = 1;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun return count;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
atari_scsi_dma_recv_setup(struct NCR5380_hostdata * hostdata,unsigned char * data,int count)489*4882a593Smuzhiyun static inline int atari_scsi_dma_recv_setup(struct NCR5380_hostdata *hostdata,
490*4882a593Smuzhiyun unsigned char *data, int count)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun return atari_scsi_dma_setup(hostdata, data, count, 0);
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun
atari_scsi_dma_send_setup(struct NCR5380_hostdata * hostdata,unsigned char * data,int count)495*4882a593Smuzhiyun static inline int atari_scsi_dma_send_setup(struct NCR5380_hostdata *hostdata,
496*4882a593Smuzhiyun unsigned char *data, int count)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun return atari_scsi_dma_setup(hostdata, data, count, 1);
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun
atari_scsi_dma_residual(struct NCR5380_hostdata * hostdata)501*4882a593Smuzhiyun static int atari_scsi_dma_residual(struct NCR5380_hostdata *hostdata)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun return atari_dma_residual;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun #define CMD_SURELY_BLOCK_MODE 0
508*4882a593Smuzhiyun #define CMD_SURELY_BYTE_MODE 1
509*4882a593Smuzhiyun #define CMD_MODE_UNKNOWN 2
510*4882a593Smuzhiyun
falcon_classify_cmd(struct scsi_cmnd * cmd)511*4882a593Smuzhiyun static int falcon_classify_cmd(struct scsi_cmnd *cmd)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun unsigned char opcode = cmd->cmnd[0];
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
516*4882a593Smuzhiyun opcode == READ_BUFFER)
517*4882a593Smuzhiyun return CMD_SURELY_BYTE_MODE;
518*4882a593Smuzhiyun else if (opcode == READ_6 || opcode == READ_10 ||
519*4882a593Smuzhiyun opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
520*4882a593Smuzhiyun opcode == RECOVER_BUFFERED_DATA) {
521*4882a593Smuzhiyun /* In case of a sequential-access target (tape), special care is
522*4882a593Smuzhiyun * needed here: The transfer is block-mode only if the 'fixed' bit is
523*4882a593Smuzhiyun * set! */
524*4882a593Smuzhiyun if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
525*4882a593Smuzhiyun return CMD_SURELY_BYTE_MODE;
526*4882a593Smuzhiyun else
527*4882a593Smuzhiyun return CMD_SURELY_BLOCK_MODE;
528*4882a593Smuzhiyun } else
529*4882a593Smuzhiyun return CMD_MODE_UNKNOWN;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /* This function calculates the number of bytes that can be transferred via
534*4882a593Smuzhiyun * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
535*4882a593Smuzhiyun * ST-DMA chip. There are only multiples of 512 bytes possible and max.
536*4882a593Smuzhiyun * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
537*4882a593Smuzhiyun * possible on the Falcon, since that would require to program the DMA for
538*4882a593Smuzhiyun * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
539*4882a593Smuzhiyun * the overrun problem, so this question is academic :-)
540*4882a593Smuzhiyun */
541*4882a593Smuzhiyun
atari_scsi_dma_xfer_len(struct NCR5380_hostdata * hostdata,struct scsi_cmnd * cmd)542*4882a593Smuzhiyun static int atari_scsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
543*4882a593Smuzhiyun struct scsi_cmnd *cmd)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun int wanted_len = cmd->SCp.this_residual;
546*4882a593Smuzhiyun int possible_len, limit;
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun if (wanted_len < DMA_MIN_SIZE)
549*4882a593Smuzhiyun return 0;
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun if (IS_A_TT())
552*4882a593Smuzhiyun /* TT SCSI DMA can transfer arbitrary #bytes */
553*4882a593Smuzhiyun return wanted_len;
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
556*4882a593Smuzhiyun * 255*512 bytes, but this should be enough)
557*4882a593Smuzhiyun *
558*4882a593Smuzhiyun * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
559*4882a593Smuzhiyun * that return a number of bytes which cannot be known beforehand. In this
560*4882a593Smuzhiyun * case, the given transfer length is an "allocation length". Now it
561*4882a593Smuzhiyun * can happen that this allocation length is a multiple of 512 bytes and
562*4882a593Smuzhiyun * the DMA is used. But if not n*512 bytes really arrive, some input data
563*4882a593Smuzhiyun * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
564*4882a593Smuzhiyun * between commands that do block transfers and those that do byte
565*4882a593Smuzhiyun * transfers. But this isn't easy... there are lots of vendor specific
566*4882a593Smuzhiyun * commands, and the user can issue any command via the
567*4882a593Smuzhiyun * SCSI_IOCTL_SEND_COMMAND.
568*4882a593Smuzhiyun *
569*4882a593Smuzhiyun * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
570*4882a593Smuzhiyun * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
571*4882a593Smuzhiyun * and 3), the thing to do is obvious: allow any number of blocks via DMA
572*4882a593Smuzhiyun * or none. In case 2), we apply some heuristic: Byte mode is assumed if
573*4882a593Smuzhiyun * the transfer (allocation) length is < 1024, hoping that no cmd. not
574*4882a593Smuzhiyun * explicitly known as byte mode have such big allocation lengths...
575*4882a593Smuzhiyun * BTW, all the discussion above applies only to reads. DMA writes are
576*4882a593Smuzhiyun * unproblematic anyways, since the targets aborts the transfer after
577*4882a593Smuzhiyun * receiving a sufficient number of bytes.
578*4882a593Smuzhiyun *
579*4882a593Smuzhiyun * Another point: If the transfer is from/to an non-ST-RAM address, we
580*4882a593Smuzhiyun * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
581*4882a593Smuzhiyun */
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun if (cmd->sc_data_direction == DMA_TO_DEVICE) {
584*4882a593Smuzhiyun /* Write operation can always use the DMA, but the transfer size must
585*4882a593Smuzhiyun * be rounded up to the next multiple of 512 (atari_dma_setup() does
586*4882a593Smuzhiyun * this).
587*4882a593Smuzhiyun */
588*4882a593Smuzhiyun possible_len = wanted_len;
589*4882a593Smuzhiyun } else {
590*4882a593Smuzhiyun /* Read operations: if the wanted transfer length is not a multiple of
591*4882a593Smuzhiyun * 512, we cannot use DMA, since the ST-DMA cannot split transfers
592*4882a593Smuzhiyun * (no interrupt on DMA finished!)
593*4882a593Smuzhiyun */
594*4882a593Smuzhiyun if (wanted_len & 0x1ff)
595*4882a593Smuzhiyun possible_len = 0;
596*4882a593Smuzhiyun else {
597*4882a593Smuzhiyun /* Now classify the command (see above) and decide whether it is
598*4882a593Smuzhiyun * allowed to do DMA at all */
599*4882a593Smuzhiyun switch (falcon_classify_cmd(cmd)) {
600*4882a593Smuzhiyun case CMD_SURELY_BLOCK_MODE:
601*4882a593Smuzhiyun possible_len = wanted_len;
602*4882a593Smuzhiyun break;
603*4882a593Smuzhiyun case CMD_SURELY_BYTE_MODE:
604*4882a593Smuzhiyun possible_len = 0; /* DMA prohibited */
605*4882a593Smuzhiyun break;
606*4882a593Smuzhiyun case CMD_MODE_UNKNOWN:
607*4882a593Smuzhiyun default:
608*4882a593Smuzhiyun /* For unknown commands assume block transfers if the transfer
609*4882a593Smuzhiyun * size/allocation length is >= 1024 */
610*4882a593Smuzhiyun possible_len = (wanted_len < 1024) ? 0 : wanted_len;
611*4882a593Smuzhiyun break;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun /* Last step: apply the hard limit on DMA transfers */
617*4882a593Smuzhiyun limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
618*4882a593Smuzhiyun STRAM_BUFFER_SIZE : 255*512;
619*4882a593Smuzhiyun if (possible_len > limit)
620*4882a593Smuzhiyun possible_len = limit;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun if (possible_len != wanted_len)
623*4882a593Smuzhiyun dprintk(NDEBUG_DMA, "DMA transfer now %d bytes instead of %d\n",
624*4882a593Smuzhiyun possible_len, wanted_len);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun return possible_len;
627*4882a593Smuzhiyun }
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun /* NCR5380 register access functions
631*4882a593Smuzhiyun *
632*4882a593Smuzhiyun * There are separate functions for TT and Falcon, because the access
633*4882a593Smuzhiyun * methods are quite different. The calling macros NCR5380_read and
634*4882a593Smuzhiyun * NCR5380_write call these functions via function pointers.
635*4882a593Smuzhiyun */
636*4882a593Smuzhiyun
atari_scsi_tt_reg_read(unsigned int reg)637*4882a593Smuzhiyun static u8 atari_scsi_tt_reg_read(unsigned int reg)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun return tt_scsi_regp[reg * 2];
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun
atari_scsi_tt_reg_write(unsigned int reg,u8 value)642*4882a593Smuzhiyun static void atari_scsi_tt_reg_write(unsigned int reg, u8 value)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun tt_scsi_regp[reg * 2] = value;
645*4882a593Smuzhiyun }
646*4882a593Smuzhiyun
atari_scsi_falcon_reg_read(unsigned int reg)647*4882a593Smuzhiyun static u8 atari_scsi_falcon_reg_read(unsigned int reg)
648*4882a593Smuzhiyun {
649*4882a593Smuzhiyun unsigned long flags;
650*4882a593Smuzhiyun u8 result;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun reg += 0x88;
653*4882a593Smuzhiyun local_irq_save(flags);
654*4882a593Smuzhiyun dma_wd.dma_mode_status = (u_short)reg;
655*4882a593Smuzhiyun result = (u8)dma_wd.fdc_acces_seccount;
656*4882a593Smuzhiyun local_irq_restore(flags);
657*4882a593Smuzhiyun return result;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun
atari_scsi_falcon_reg_write(unsigned int reg,u8 value)660*4882a593Smuzhiyun static void atari_scsi_falcon_reg_write(unsigned int reg, u8 value)
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun unsigned long flags;
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun reg += 0x88;
665*4882a593Smuzhiyun local_irq_save(flags);
666*4882a593Smuzhiyun dma_wd.dma_mode_status = (u_short)reg;
667*4882a593Smuzhiyun dma_wd.fdc_acces_seccount = (u_short)value;
668*4882a593Smuzhiyun local_irq_restore(flags);
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun #include "NCR5380.c"
673*4882a593Smuzhiyun
atari_scsi_host_reset(struct scsi_cmnd * cmd)674*4882a593Smuzhiyun static int atari_scsi_host_reset(struct scsi_cmnd *cmd)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun int rv;
677*4882a593Smuzhiyun unsigned long flags;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun local_irq_save(flags);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun /* Abort a maybe active DMA transfer */
682*4882a593Smuzhiyun if (IS_A_TT()) {
683*4882a593Smuzhiyun tt_scsi_dma.dma_ctrl = 0;
684*4882a593Smuzhiyun } else {
685*4882a593Smuzhiyun if (stdma_is_locked_by(scsi_falcon_intr))
686*4882a593Smuzhiyun st_dma.dma_mode_status = 0x90;
687*4882a593Smuzhiyun atari_dma_active = 0;
688*4882a593Smuzhiyun atari_dma_orig_addr = NULL;
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun rv = NCR5380_host_reset(cmd);
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /* The 5380 raises its IRQ line while _RST is active but the ST DMA
694*4882a593Smuzhiyun * "lock" has been released so this interrupt may end up handled by
695*4882a593Smuzhiyun * floppy or IDE driver (if one of them holds the lock). The NCR5380
696*4882a593Smuzhiyun * interrupt flag has been cleared already.
697*4882a593Smuzhiyun */
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun local_irq_restore(flags);
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun return rv;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun #define DRV_MODULE_NAME "atari_scsi"
705*4882a593Smuzhiyun #define PFX DRV_MODULE_NAME ": "
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun static struct scsi_host_template atari_scsi_template = {
708*4882a593Smuzhiyun .module = THIS_MODULE,
709*4882a593Smuzhiyun .proc_name = DRV_MODULE_NAME,
710*4882a593Smuzhiyun .name = "Atari native SCSI",
711*4882a593Smuzhiyun .info = atari_scsi_info,
712*4882a593Smuzhiyun .queuecommand = atari_scsi_queue_command,
713*4882a593Smuzhiyun .eh_abort_handler = atari_scsi_abort,
714*4882a593Smuzhiyun .eh_host_reset_handler = atari_scsi_host_reset,
715*4882a593Smuzhiyun .this_id = 7,
716*4882a593Smuzhiyun .cmd_per_lun = 2,
717*4882a593Smuzhiyun .dma_boundary = PAGE_SIZE - 1,
718*4882a593Smuzhiyun .cmd_size = NCR5380_CMD_SIZE,
719*4882a593Smuzhiyun };
720*4882a593Smuzhiyun
atari_scsi_probe(struct platform_device * pdev)721*4882a593Smuzhiyun static int __init atari_scsi_probe(struct platform_device *pdev)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun struct Scsi_Host *instance;
724*4882a593Smuzhiyun int error;
725*4882a593Smuzhiyun struct resource *irq;
726*4882a593Smuzhiyun int host_flags = 0;
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
729*4882a593Smuzhiyun if (!irq)
730*4882a593Smuzhiyun return -ENODEV;
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun if (ATARIHW_PRESENT(TT_SCSI)) {
733*4882a593Smuzhiyun atari_scsi_reg_read = atari_scsi_tt_reg_read;
734*4882a593Smuzhiyun atari_scsi_reg_write = atari_scsi_tt_reg_write;
735*4882a593Smuzhiyun } else {
736*4882a593Smuzhiyun atari_scsi_reg_read = atari_scsi_falcon_reg_read;
737*4882a593Smuzhiyun atari_scsi_reg_write = atari_scsi_falcon_reg_write;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun if (ATARIHW_PRESENT(TT_SCSI)) {
741*4882a593Smuzhiyun atari_scsi_template.can_queue = 16;
742*4882a593Smuzhiyun atari_scsi_template.sg_tablesize = SG_ALL;
743*4882a593Smuzhiyun } else {
744*4882a593Smuzhiyun atari_scsi_template.can_queue = 1;
745*4882a593Smuzhiyun atari_scsi_template.sg_tablesize = 1;
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun if (setup_can_queue > 0)
749*4882a593Smuzhiyun atari_scsi_template.can_queue = setup_can_queue;
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun if (setup_cmd_per_lun > 0)
752*4882a593Smuzhiyun atari_scsi_template.cmd_per_lun = setup_cmd_per_lun;
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun /* Don't increase sg_tablesize on Falcon! */
755*4882a593Smuzhiyun if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize > 0)
756*4882a593Smuzhiyun atari_scsi_template.sg_tablesize = setup_sg_tablesize;
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun if (setup_hostid >= 0) {
759*4882a593Smuzhiyun atari_scsi_template.this_id = setup_hostid & 7;
760*4882a593Smuzhiyun } else if (IS_REACHABLE(CONFIG_NVRAM)) {
761*4882a593Smuzhiyun /* Test if a host id is set in the NVRam */
762*4882a593Smuzhiyun if (ATARIHW_PRESENT(TT_CLK)) {
763*4882a593Smuzhiyun unsigned char b;
764*4882a593Smuzhiyun loff_t offset = 16;
765*4882a593Smuzhiyun ssize_t count = nvram_read(&b, 1, &offset);
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun /* Arbitration enabled? (for TOS)
768*4882a593Smuzhiyun * If yes, use configured host ID
769*4882a593Smuzhiyun */
770*4882a593Smuzhiyun if ((count == 1) && (b & 0x80))
771*4882a593Smuzhiyun atari_scsi_template.this_id = b & 7;
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun /* If running on a Falcon and if there's TT-Ram (i.e., more than one
776*4882a593Smuzhiyun * memory block, since there's always ST-Ram in a Falcon), then
777*4882a593Smuzhiyun * allocate a STRAM_BUFFER_SIZE byte dribble buffer for transfers
778*4882a593Smuzhiyun * from/to alternative Ram.
779*4882a593Smuzhiyun */
780*4882a593Smuzhiyun if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) &&
781*4882a593Smuzhiyun m68k_realnum_memory > 1) {
782*4882a593Smuzhiyun atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
783*4882a593Smuzhiyun if (!atari_dma_buffer) {
784*4882a593Smuzhiyun pr_err(PFX "can't allocate ST-RAM double buffer\n");
785*4882a593Smuzhiyun return -ENOMEM;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
788*4882a593Smuzhiyun atari_dma_orig_addr = NULL;
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun instance = scsi_host_alloc(&atari_scsi_template,
792*4882a593Smuzhiyun sizeof(struct NCR5380_hostdata));
793*4882a593Smuzhiyun if (!instance) {
794*4882a593Smuzhiyun error = -ENOMEM;
795*4882a593Smuzhiyun goto fail_alloc;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun instance->irq = irq->start;
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP;
801*4882a593Smuzhiyun host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun error = NCR5380_init(instance, host_flags);
804*4882a593Smuzhiyun if (error)
805*4882a593Smuzhiyun goto fail_init;
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun if (IS_A_TT()) {
808*4882a593Smuzhiyun error = request_irq(instance->irq, scsi_tt_intr, 0,
809*4882a593Smuzhiyun "NCR5380", instance);
810*4882a593Smuzhiyun if (error) {
811*4882a593Smuzhiyun pr_err(PFX "request irq %d failed, aborting\n",
812*4882a593Smuzhiyun instance->irq);
813*4882a593Smuzhiyun goto fail_irq;
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun tt_scsi_dma.dma_ctrl = 0;
818*4882a593Smuzhiyun atari_dma_residual = 0;
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun /* While the read overruns (described by Drew Eckhardt in
821*4882a593Smuzhiyun * NCR5380.c) never happened on TTs, they do in fact on the
822*4882a593Smuzhiyun * Medusa (This was the cause why SCSI didn't work right for
823*4882a593Smuzhiyun * so long there.) Since handling the overruns slows down
824*4882a593Smuzhiyun * a bit, I turned the #ifdef's into a runtime condition.
825*4882a593Smuzhiyun *
826*4882a593Smuzhiyun * In principle it should be sufficient to do max. 1 byte with
827*4882a593Smuzhiyun * PIO, but there is another problem on the Medusa with the DMA
828*4882a593Smuzhiyun * rest data register. So read_overruns is currently set
829*4882a593Smuzhiyun * to 4 to avoid having transfers that aren't a multiple of 4.
830*4882a593Smuzhiyun * If the rest data bug is fixed, this can be lowered to 1.
831*4882a593Smuzhiyun */
832*4882a593Smuzhiyun if (MACH_IS_MEDUSA) {
833*4882a593Smuzhiyun struct NCR5380_hostdata *hostdata =
834*4882a593Smuzhiyun shost_priv(instance);
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun hostdata->read_overruns = 4;
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun } else {
839*4882a593Smuzhiyun /* Nothing to do for the interrupt: the ST-DMA is initialized
840*4882a593Smuzhiyun * already.
841*4882a593Smuzhiyun */
842*4882a593Smuzhiyun atari_dma_residual = 0;
843*4882a593Smuzhiyun atari_dma_active = 0;
844*4882a593Smuzhiyun atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
845*4882a593Smuzhiyun : 0xff000000);
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun NCR5380_maybe_reset_bus(instance);
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun error = scsi_add_host(instance, NULL);
851*4882a593Smuzhiyun if (error)
852*4882a593Smuzhiyun goto fail_host;
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun platform_set_drvdata(pdev, instance);
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun scsi_scan_host(instance);
857*4882a593Smuzhiyun return 0;
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun fail_host:
860*4882a593Smuzhiyun if (IS_A_TT())
861*4882a593Smuzhiyun free_irq(instance->irq, instance);
862*4882a593Smuzhiyun fail_irq:
863*4882a593Smuzhiyun NCR5380_exit(instance);
864*4882a593Smuzhiyun fail_init:
865*4882a593Smuzhiyun scsi_host_put(instance);
866*4882a593Smuzhiyun fail_alloc:
867*4882a593Smuzhiyun if (atari_dma_buffer)
868*4882a593Smuzhiyun atari_stram_free(atari_dma_buffer);
869*4882a593Smuzhiyun return error;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun
atari_scsi_remove(struct platform_device * pdev)872*4882a593Smuzhiyun static int __exit atari_scsi_remove(struct platform_device *pdev)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun struct Scsi_Host *instance = platform_get_drvdata(pdev);
875*4882a593Smuzhiyun
876*4882a593Smuzhiyun scsi_remove_host(instance);
877*4882a593Smuzhiyun if (IS_A_TT())
878*4882a593Smuzhiyun free_irq(instance->irq, instance);
879*4882a593Smuzhiyun NCR5380_exit(instance);
880*4882a593Smuzhiyun scsi_host_put(instance);
881*4882a593Smuzhiyun if (atari_dma_buffer)
882*4882a593Smuzhiyun atari_stram_free(atari_dma_buffer);
883*4882a593Smuzhiyun return 0;
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun static struct platform_driver atari_scsi_driver = {
887*4882a593Smuzhiyun .remove = __exit_p(atari_scsi_remove),
888*4882a593Smuzhiyun .driver = {
889*4882a593Smuzhiyun .name = DRV_MODULE_NAME,
890*4882a593Smuzhiyun },
891*4882a593Smuzhiyun };
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe);
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun MODULE_ALIAS("platform:" DRV_MODULE_NAME);
896*4882a593Smuzhiyun MODULE_LICENSE("GPL");
897