blob: d1c37a386947b03d080524dac5f18c244580bd08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port
3 *
4 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5 *
6 * Loosely based on the work of Robert De Vries' team and added:
7 * - working real DMA
8 * - Falcon support (untested yet!) ++bjoern fixed and now it works
9 * - lots of extensions and bug fixes.
10 *
11 * This file is subject to the terms and conditions of the GNU General Public
12 * License. See the file COPYING in the main directory of this archive
13 * for more details.
14 *
15 */
16
17
18/**************************************************************************/
19/* */
20/* Notes for Falcon SCSI: */
21/* ---------------------- */
22/* */
23/* Since the Falcon SCSI uses the ST-DMA chip, that is shared among */
24/* several device drivers, locking and unlocking the access to this */
25/* chip is required. But locking is not possible from an interrupt, */
26/* since it puts the process to sleep if the lock is not available. */
27/* This prevents "late" locking of the DMA chip, i.e. locking it just */
28/* before using it, since in case of disconnection-reconnection */
29/* commands, the DMA is started from the reselection interrupt. */
30/* */
31/* Two possible schemes for ST-DMA-locking would be: */
32/* 1) The lock is taken for each command separately and disconnecting */
33/* is forbidden (i.e. can_queue = 1). */
34/* 2) The DMA chip is locked when the first command comes in and */
35/* released when the last command is finished and all queues are */
36/* empty. */
37/* The first alternative would result in bad performance, since the */
38/* interleaving of commands would not be used. The second is unfair to */
39/* other drivers using the ST-DMA, because the queues will seldom be */
40/* totally empty if there is a lot of disk traffic. */
41/* */
42/* For this reasons I decided to employ a more elaborate scheme: */
43/* - First, we give up the lock every time we can (for fairness), this */
44/* means every time a command finishes and there are no other commands */
45/* on the disconnected queue. */
46/* - If there are others waiting to lock the DMA chip, we stop */
47/* issuing commands, i.e. moving them onto the issue queue. */
48/* Because of that, the disconnected queue will run empty in a */
49/* while. Instead we go to sleep on a 'fairness_queue'. */
50/* - If the lock is released, all processes waiting on the fairness */
51/* queue will be woken. The first of them tries to re-lock the DMA, */
52/* the others wait for the first to finish this task. After that, */
53/* they can all run on and do their commands... */
54/* This sounds complicated (and it is it :-(), but it seems to be a */
55/* good compromise between fairness and performance: As long as no one */
56/* else wants to work with the ST-DMA chip, SCSI can go along as */
57/* usual. If now someone else comes, this behaviour is changed to a */
58/* "fairness mode": just already initiated commands are finished and */
59/* then the lock is released. The other one waiting will probably win */
60/* the race for locking the DMA, since it was waiting for longer. And */
61/* after it has finished, SCSI can go ahead again. Finally: I hope I */
62/* have not produced any deadlock possibilities! */
63/* */
64/**************************************************************************/
65
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/interrupt.h>
72#include <linux/init.h>
73#include <linux/nvram.h>
74#include <linux/bitops.h>
Arnd Bergmanneff9cf82014-03-01 20:51:03 +130075#include <linux/wait.h>
Finn Thain3ff228a2014-11-12 16:12:09 +110076#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#include <asm/setup.h>
79#include <asm/atarihw.h>
80#include <asm/atariints.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <asm/atari_stdma.h>
82#include <asm/atari_stram.h>
83#include <asm/io.h>
84
Finn Thain3ff228a2014-11-12 16:12:09 +110085#include <scsi/scsi_host.h>
86
Finn Thain4e705202014-11-12 16:12:10 +110087/* Definitions for the core NCR5380 driver. */
88
89#define REAL_DMA
90#define SUPPORT_TAGS
91#define MAX_TAGS 32
Finn Thaine3f463b2014-11-12 16:12:16 +110092#define DMA_MIN_SIZE 32
Finn Thain4e705202014-11-12 16:12:10 +110093
94#define NCR5380_implementation_fields /* none */
95
96#define NCR5380_read(reg) atari_scsi_reg_read(reg)
97#define NCR5380_write(reg, value) atari_scsi_reg_write(reg, value)
98
99#define NCR5380_queue_command atari_scsi_queue_command
100#define NCR5380_abort atari_scsi_abort
101#define NCR5380_show_info atari_scsi_show_info
102#define NCR5380_info atari_scsi_info
103
104#define NCR5380_dma_read_setup(instance, data, count) \
105 atari_scsi_dma_setup(instance, data, count, 0)
106#define NCR5380_dma_write_setup(instance, data, count) \
107 atari_scsi_dma_setup(instance, data, count, 1)
108#define NCR5380_dma_residual(instance) \
109 atari_scsi_dma_residual(instance)
110#define NCR5380_dma_xfer_len(instance, cmd, phase) \
111 atari_dma_xfer_len(cmd->SCp.this_residual, cmd, !((phase) & SR_IO))
112
Finn Thaina53a21e2014-11-12 16:12:21 +1100113#define NCR5380_acquire_dma_irq(instance) falcon_get_lock(instance)
Finn Thaine3c3da62014-11-12 16:12:15 +1100114#define NCR5380_release_dma_irq(instance) falcon_release_lock()
115
Finn Thain3ff228a2014-11-12 16:12:09 +1100116#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Finn Thain4e705202014-11-12 16:12:10 +1100118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define IS_A_TT() ATARIHW_PRESENT(TT_SCSI)
120
121#define SCSI_DMA_WRITE_P(elt,val) \
122 do { \
123 unsigned long v = val; \
124 tt_scsi_dma.elt##_lo = v & 0xff; \
125 v >>= 8; \
126 tt_scsi_dma.elt##_lmd = v & 0xff; \
127 v >>= 8; \
128 tt_scsi_dma.elt##_hmd = v & 0xff; \
129 v >>= 8; \
130 tt_scsi_dma.elt##_hi = v & 0xff; \
131 } while(0)
132
133#define SCSI_DMA_READ_P(elt) \
134 (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \
135 (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \
136 (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \
137 (unsigned long)tt_scsi_dma.elt##_lo)
138
139
140static inline void SCSI_DMA_SETADR(unsigned long adr)
141{
142 st_dma.dma_lo = (unsigned char)adr;
143 MFPDELAY();
144 adr >>= 8;
145 st_dma.dma_md = (unsigned char)adr;
146 MFPDELAY();
147 adr >>= 8;
148 st_dma.dma_hi = (unsigned char)adr;
149 MFPDELAY();
150}
151
152static inline unsigned long SCSI_DMA_GETADR(void)
153{
154 unsigned long adr;
155 adr = st_dma.dma_lo;
156 MFPDELAY();
157 adr |= (st_dma.dma_md & 0xff) << 8;
158 MFPDELAY();
159 adr |= (st_dma.dma_hi & 0xff) << 16;
160 MFPDELAY();
161 return adr;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define HOSTDATA_DMALEN (((struct NCR5380_hostdata *) \
165 (atari_scsi_host->hostdata))->dma_len)
166
167/* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
168 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
169 * need ten times the standard value... */
170#ifndef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
171#define AFTER_RESET_DELAY (HZ/2)
172#else
173#define AFTER_RESET_DELAY (5*HZ/2)
174#endif
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +0200177static void atari_scsi_fetch_restbytes(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Roman Zippelc28bda22007-05-01 22:32:36 +0200180static struct Scsi_Host *atari_scsi_host;
181static unsigned char (*atari_scsi_reg_read)(unsigned char reg);
182static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184#ifdef REAL_DMA
185static unsigned long atari_dma_residual, atari_dma_startaddr;
186static short atari_dma_active;
187/* pointer to the dribble buffer */
Roman Zippelc28bda22007-05-01 22:32:36 +0200188static char *atari_dma_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* precalculated physical address of the dribble buffer */
190static unsigned long atari_dma_phys_buffer;
191/* != 0 tells the Falcon int handler to copy data from the dribble buffer */
192static char *atari_dma_orig_addr;
193/* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
194 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
195 * cases where requests to physical contiguous buffers have been merged, this
196 * request is <= 4k (one page). So I don't think we have to split transfers
197 * just due to this buffer size...
198 */
199#define STRAM_BUFFER_SIZE (4096)
200/* mask for address bits that can't be used with the ST-DMA */
201static unsigned long atari_dma_stram_mask;
202#define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#endif
204
205static int setup_can_queue = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800206module_param(setup_can_queue, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static int setup_cmd_per_lun = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800208module_param(setup_cmd_per_lun, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int setup_sg_tablesize = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800210module_param(setup_sg_tablesize, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#ifdef SUPPORT_TAGS
212static int setup_use_tagged_queuing = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800213module_param(setup_use_tagged_queuing, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#endif
215static int setup_hostid = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800216module_param(setup_hostid, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#if defined(REAL_DMA)
220
Roman Zippelc28bda22007-05-01 22:32:36 +0200221static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int i;
Roman Zippelc28bda22007-05-01 22:32:36 +0200224 unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (dma_stat & 0x01) {
227
228 /* A bus error happens when DMA-ing from the last page of a
229 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
230 * Check for this case:
231 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200232
233 for (i = 0; i < m68k_num_memory; ++i) {
234 end_addr = m68k_memory[i].addr + m68k_memory[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (end_addr <= addr && addr <= end_addr + 4)
Roman Zippelc28bda22007-05-01 22:32:36 +0200236 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242
243#if 0
244/* Dead code... wasn't called anyway :-) and causes some trouble, because at
245 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
246 * to clear the DMA int pending bit before it allows other level 6 interrupts.
247 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200248static void scsi_dma_buserr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Roman Zippelc28bda22007-05-01 22:32:36 +0200250 unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Don't do anything if a NCR interrupt is pending. Probably it's just
253 * masked... */
Roman Zippelc28bda22007-05-01 22:32:36 +0200254 if (atari_irq_pending(IRQ_TT_MFP_SCSI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return;
Roman Zippelc28bda22007-05-01 22:32:36 +0200256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
258 SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
259 if (dma_stat & 0x80) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200260 if (!scsi_dma_is_ignored_buserr(dma_stat))
261 printk("SCSI DMA bus error -- bad DMA programming!\n");
262 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* Under normal circumstances we never should get to this point,
264 * since both interrupts are triggered simultaneously and the 5380
265 * int has higher priority. When this irq is handled, that DMA
266 * interrupt is cleared. So a warning message is printed here.
267 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200268 printk("SCSI DMA intr ?? -- this shouldn't happen!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270}
271#endif
272
273#endif
274
275
Roman Zippelc28bda22007-05-01 22:32:36 +0200276static irqreturn_t scsi_tt_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278#ifdef REAL_DMA
279 int dma_stat;
280
281 dma_stat = tt_scsi_dma.dma_ctrl;
282
Finn Thaind65e6342014-03-18 11:42:20 +1100283 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 interrupt, DMA status = %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 atari_scsi_host->host_no, dma_stat & 0xff);
285
286 /* Look if it was the DMA that has interrupted: First possibility
287 * is that a bus error occurred...
288 */
289 if (dma_stat & 0x80) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200290 if (!scsi_dma_is_ignored_buserr(dma_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
292 SCSI_DMA_READ_P(dma_addr));
293 printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
294 }
295 }
296
297 /* If the DMA is active but not finished, we have the case
298 * that some other 5380 interrupt occurred within the DMA transfer.
299 * This means we have residual bytes, if the desired end address
300 * is not yet reached. Maybe we have to fetch some bytes from the
301 * rest data register, too. The residual must be calculated from
302 * the address pointer, not the counter register, because only the
303 * addr reg counts bytes not yet written and pending in the rest
304 * data reg!
305 */
306 if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200307 atari_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Finn Thaind65e6342014-03-18 11:42:20 +1100309 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 atari_dma_residual);
311
312 if ((signed int)atari_dma_residual < 0)
313 atari_dma_residual = 0;
314 if ((dma_stat & 1) == 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200315 /*
316 * After read operations, we maybe have to
317 * transport some rest bytes
318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 atari_scsi_fetch_restbytes();
Roman Zippelc28bda22007-05-01 22:32:36 +0200320 } else {
321 /*
322 * There seems to be a nasty bug in some SCSI-DMA/NCR
323 * combinations: If a target disconnects while a write
324 * operation is going on, the address register of the
325 * DMA may be a few bytes farer than it actually read.
326 * This is probably due to DMA prefetching and a delay
327 * between DMA and NCR. Experiments showed that the
328 * dma_addr is 9 bytes to high, but this could vary.
329 * The problem is, that the residual is thus calculated
330 * wrong and the next transfer will start behind where
331 * it should. So we round up the residual to the next
332 * multiple of a sector size, if it isn't already a
333 * multiple and the originally expected transfer size
334 * was. The latter condition is there to ensure that
335 * the correction is taken only for "real" data
336 * transfers and not for, e.g., the parameters of some
337 * other command. These shouldn't disconnect anyway.
338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (atari_dma_residual & 0x1ff) {
Finn Thaind65e6342014-03-18 11:42:20 +1100340 dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 "difference %ld bytes\n",
342 512 - (atari_dma_residual & 0x1ff));
343 atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
344 }
345 }
346 tt_scsi_dma.dma_ctrl = 0;
347 }
348
349 /* If the DMA is finished, fetch the rest bytes and turn it off */
350 if (dma_stat & 0x40) {
351 atari_dma_residual = 0;
352 if ((dma_stat & 1) == 0)
353 atari_scsi_fetch_restbytes();
354 tt_scsi_dma.dma_ctrl = 0;
355 }
356
357#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +0200358
Jeff Garzik1e641662007-11-11 19:52:05 -0500359 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return IRQ_HANDLED;
362}
363
364
Roman Zippelc28bda22007-05-01 22:32:36 +0200365static irqreturn_t scsi_falcon_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367#ifdef REAL_DMA
368 int dma_stat;
369
370 /* Turn off DMA and select sector counter register before
371 * accessing the status register (Atari recommendation!)
372 */
373 st_dma.dma_mode_status = 0x90;
374 dma_stat = st_dma.dma_mode_status;
375
376 /* Bit 0 indicates some error in the DMA process... don't know
377 * what happened exactly (no further docu).
378 */
379 if (!(dma_stat & 0x01)) {
380 /* DMA error */
381 printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
382 }
383
384 /* If the DMA was active, but now bit 1 is not clear, it is some
385 * other 5380 interrupt that finishes the DMA transfer. We have to
386 * calculate the number of residual bytes and give a warning if
387 * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
388 */
389 if (atari_dma_active && (dma_stat & 0x02)) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200390 unsigned long transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
393 /* The ST-DMA address is incremented in 2-byte steps, but the
394 * data are written only in 16-byte chunks. If the number of
395 * transferred bytes is not divisible by 16, the remainder is
396 * lost somewhere in outer space.
397 */
398 if (transferred & 15)
399 printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
400 "ST-DMA fifo\n", transferred & 15);
401
402 atari_dma_residual = HOSTDATA_DMALEN - transferred;
Finn Thaind65e6342014-03-18 11:42:20 +1100403 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 atari_dma_residual);
Roman Zippelc28bda22007-05-01 22:32:36 +0200405 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 atari_dma_residual = 0;
407 atari_dma_active = 0;
408
409 if (atari_dma_orig_addr) {
410 /* If the dribble buffer was used on a read operation, copy the DMA-ed
411 * data to the original destination address.
412 */
413 memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
414 HOSTDATA_DMALEN - atari_dma_residual);
415 atari_dma_orig_addr = NULL;
416 }
417
418#endif /* REAL_DMA */
419
Jeff Garzik1e641662007-11-11 19:52:05 -0500420 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return IRQ_HANDLED;
422}
423
424
425#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +0200426static void atari_scsi_fetch_restbytes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int nr;
429 char *src, *dst;
430 unsigned long phys_dst;
431
432 /* fetch rest bytes in the DMA register */
433 phys_dst = SCSI_DMA_READ_P(dma_addr);
434 nr = phys_dst & 3;
435 if (nr) {
436 /* there are 'nr' bytes left for the last long address
437 before the DMA pointer */
438 phys_dst ^= nr;
Finn Thaind65e6342014-03-18 11:42:20 +1100439 dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 nr, phys_dst);
441 /* The content of the DMA pointer is a physical address! */
442 dst = phys_to_virt(phys_dst);
Finn Thaind65e6342014-03-18 11:42:20 +1100443 dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
445 *dst++ = *src++;
446 }
447}
448#endif /* REAL_DMA */
449
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/* This function releases the lock on the DMA chip if there is no
Finn Thain16b29e72014-11-12 16:12:08 +1100452 * connected command and the disconnected queue is empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
454
Finn Thaine3c3da62014-11-12 16:12:15 +1100455static void falcon_release_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Roman Zippelc28bda22007-05-01 22:32:36 +0200457 if (IS_A_TT())
458 return;
459
Finn Thaine3c3da62014-11-12 16:12:15 +1100460 if (stdma_is_locked_by(scsi_falcon_intr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 stdma_release();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/* This function manages the locking of the ST-DMA.
465 * If the DMA isn't locked already for SCSI, it tries to lock it by
466 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
467 * there are other drivers waiting for the chip, we do not issue the
Finn Thain16b29e72014-11-12 16:12:08 +1100468 * command immediately but tell the SCSI mid-layer to defer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
470
Finn Thaina53a21e2014-11-12 16:12:21 +1100471static int falcon_get_lock(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Roman Zippelc28bda22007-05-01 22:32:36 +0200473 if (IS_A_TT())
Finn Thain16b29e72014-11-12 16:12:08 +1100474 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Finn Thain16b29e72014-11-12 16:12:08 +1100476 if (in_interrupt())
Finn Thaina53a21e2014-11-12 16:12:21 +1100477 return stdma_try_lock(scsi_falcon_intr, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Finn Thaina53a21e2014-11-12 16:12:21 +1100479 stdma_lock(scsi_falcon_intr, instance);
Finn Thain16b29e72014-11-12 16:12:08 +1100480 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100483#ifndef MODULE
484static int __init atari_scsi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 /* Format of atascsi parameter is:
487 * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
Finn Thain3ff228a2014-11-12 16:12:09 +1100488 * Defaults depend on TT or Falcon, determined at run time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * Negative values mean don't change.
490 */
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100491 int ints[6];
492
493 get_options(str, ARRAY_SIZE(ints), ints);
Roman Zippelc28bda22007-05-01 22:32:36 +0200494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (ints[0] < 1) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200496 printk("atari_scsi_setup: no arguments!\n");
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Finn Thain3ff228a2014-11-12 16:12:09 +1100499 if (ints[0] >= 1)
500 setup_can_queue = ints[1];
501 if (ints[0] >= 2)
502 setup_cmd_per_lun = ints[2];
503 if (ints[0] >= 3)
504 setup_sg_tablesize = ints[3];
505 if (ints[0] >= 4)
506 setup_hostid = ints[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507#ifdef SUPPORT_TAGS
Finn Thain3ff228a2014-11-12 16:12:09 +1100508 if (ints[0] >= 5)
509 setup_use_tagged_queuing = ints[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#endif
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100511
512 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100515__setup("atascsi=", atari_scsi_setup);
516#endif /* !MODULE */
517
Roman Zippelc28bda22007-05-01 22:32:36 +0200518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
520static void __init atari_scsi_reset_boot(void)
521{
522 unsigned long end;
Roman Zippelc28bda22007-05-01 22:32:36 +0200523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /*
525 * Do a SCSI reset to clean up the bus during initialization. No messing
526 * with the queues, interrupts, or locks necessary here.
527 */
528
Roman Zippelc28bda22007-05-01 22:32:36 +0200529 printk("Atari SCSI: resetting the SCSI bus...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* get in phase */
Roman Zippelc28bda22007-05-01 22:32:36 +0200532 NCR5380_write(TARGET_COMMAND_REG,
533 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* assert RST */
Roman Zippelc28bda22007-05-01 22:32:36 +0200536 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* The min. reset hold time is 25us, so 40us should be enough */
Roman Zippelc28bda22007-05-01 22:32:36 +0200538 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* reset RST and interrupt */
Roman Zippelc28bda22007-05-01 22:32:36 +0200540 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
541 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 end = jiffies + AFTER_RESET_DELAY;
544 while (time_before(jiffies, end))
545 barrier();
546
Roman Zippelc28bda22007-05-01 22:32:36 +0200547 printk(" done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549#endif
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#if defined(REAL_DMA)
552
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200553static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance,
554 void *data, unsigned long count,
555 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Roman Zippelc28bda22007-05-01 22:32:36 +0200557 unsigned long addr = virt_to_phys(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Finn Thaind65e6342014-03-18 11:42:20 +1100559 dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 "dir = %d\n", instance->host_no, data, addr, count, dir);
561
562 if (!IS_A_TT() && !STRAM_ADDR(addr)) {
563 /* If we have a non-DMAable address on a Falcon, use the dribble
564 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
565 * handler to copy data from the dribble buffer to the originally
566 * wanted address.
567 */
568 if (dir)
Roman Zippelc28bda22007-05-01 22:32:36 +0200569 memcpy(atari_dma_buffer, data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 else
571 atari_dma_orig_addr = data;
572 addr = atari_dma_phys_buffer;
573 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 atari_dma_startaddr = addr; /* Needed for calculating residual later. */
Roman Zippelc28bda22007-05-01 22:32:36 +0200576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* Cache cleanup stuff: On writes, push any dirty cache out before sending
578 * it to the peripheral. (Must be done before DMA setup, since at least
579 * the ST-DMA begins to fill internal buffers right after setup. For
580 * reads, invalidate any cache, may be altered after DMA without CPU
581 * knowledge.
Roman Zippelc28bda22007-05-01 22:32:36 +0200582 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * ++roman: For the Medusa, there's no need at all for that cache stuff,
584 * because the hardware does bus snooping (fine!).
585 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200586 dma_cache_maintenance(addr, count, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (count == 0)
589 printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
590
591 if (IS_A_TT()) {
592 tt_scsi_dma.dma_ctrl = dir;
Roman Zippelc28bda22007-05-01 22:32:36 +0200593 SCSI_DMA_WRITE_P(dma_addr, addr);
594 SCSI_DMA_WRITE_P(dma_cnt, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 tt_scsi_dma.dma_ctrl = dir | 2;
Roman Zippelc28bda22007-05-01 22:32:36 +0200596 } else { /* ! IS_A_TT */
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* set address */
Roman Zippelc28bda22007-05-01 22:32:36 +0200599 SCSI_DMA_SETADR(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* toggle direction bit to clear FIFO and set DMA direction */
602 dir <<= 8;
603 st_dma.dma_mode_status = 0x90 | dir;
604 st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
605 st_dma.dma_mode_status = 0x90 | dir;
606 udelay(40);
607 /* On writes, round up the transfer length to the next multiple of 512
608 * (see also comment at atari_dma_xfer_len()). */
609 st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
610 udelay(40);
611 st_dma.dma_mode_status = 0x10 | dir;
612 udelay(40);
613 /* need not restore value of dir, only boolean value is tested */
614 atari_dma_active = 1;
615 }
616
Roman Zippelc28bda22007-05-01 22:32:36 +0200617 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620
Roman Zippelc28bda22007-05-01 22:32:36 +0200621static long atari_scsi_dma_residual(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Roman Zippelc28bda22007-05-01 22:32:36 +0200623 return atari_dma_residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626
627#define CMD_SURELY_BLOCK_MODE 0
628#define CMD_SURELY_BYTE_MODE 1
629#define CMD_MODE_UNKNOWN 2
630
Finn Thain710ddd02014-11-12 16:12:02 +1100631static int falcon_classify_cmd(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 unsigned char opcode = cmd->cmnd[0];
Roman Zippelc28bda22007-05-01 22:32:36 +0200634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
Roman Zippelc28bda22007-05-01 22:32:36 +0200636 opcode == READ_BUFFER)
637 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 else if (opcode == READ_6 || opcode == READ_10 ||
639 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
640 opcode == RECOVER_BUFFERED_DATA) {
641 /* In case of a sequential-access target (tape), special care is
642 * needed here: The transfer is block-mode only if the 'fixed' bit is
643 * set! */
644 if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
Roman Zippelc28bda22007-05-01 22:32:36 +0200645 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 else
Roman Zippelc28bda22007-05-01 22:32:36 +0200647 return CMD_SURELY_BLOCK_MODE;
648 } else
649 return CMD_MODE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652
653/* This function calculates the number of bytes that can be transferred via
654 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
655 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
656 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
657 * possible on the Falcon, since that would require to program the DMA for
658 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
659 * the overrun problem, so this question is academic :-)
660 */
661
Roman Zippelc28bda22007-05-01 22:32:36 +0200662static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
Finn Thain710ddd02014-11-12 16:12:02 +1100663 struct scsi_cmnd *cmd, int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 unsigned long possible_len, limit;
Adrian Bunk29c8a242008-10-13 21:58:59 +0200666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (IS_A_TT())
668 /* TT SCSI DMA can transfer arbitrary #bytes */
Roman Zippelc28bda22007-05-01 22:32:36 +0200669 return wanted_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
672 * 255*512 bytes, but this should be enough)
673 *
674 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
675 * that return a number of bytes which cannot be known beforehand. In this
676 * case, the given transfer length is an "allocation length". Now it
677 * can happen that this allocation length is a multiple of 512 bytes and
678 * the DMA is used. But if not n*512 bytes really arrive, some input data
679 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
680 * between commands that do block transfers and those that do byte
681 * transfers. But this isn't easy... there are lots of vendor specific
682 * commands, and the user can issue any command via the
683 * SCSI_IOCTL_SEND_COMMAND.
684 *
685 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
686 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
687 * and 3), the thing to do is obvious: allow any number of blocks via DMA
688 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
689 * the transfer (allocation) length is < 1024, hoping that no cmd. not
690 * explicitly known as byte mode have such big allocation lengths...
691 * BTW, all the discussion above applies only to reads. DMA writes are
692 * unproblematic anyways, since the targets aborts the transfer after
693 * receiving a sufficient number of bytes.
694 *
695 * Another point: If the transfer is from/to an non-ST-RAM address, we
696 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
697 */
698
699 if (write_flag) {
700 /* Write operation can always use the DMA, but the transfer size must
701 * be rounded up to the next multiple of 512 (atari_dma_setup() does
702 * this).
703 */
704 possible_len = wanted_len;
Roman Zippelc28bda22007-05-01 22:32:36 +0200705 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* Read operations: if the wanted transfer length is not a multiple of
707 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
708 * (no interrupt on DMA finished!)
709 */
710 if (wanted_len & 0x1ff)
711 possible_len = 0;
712 else {
713 /* Now classify the command (see above) and decide whether it is
714 * allowed to do DMA at all */
Roman Zippelc28bda22007-05-01 22:32:36 +0200715 switch (falcon_classify_cmd(cmd)) {
716 case CMD_SURELY_BLOCK_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 possible_len = wanted_len;
718 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200719 case CMD_SURELY_BYTE_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 possible_len = 0; /* DMA prohibited */
721 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200722 case CMD_MODE_UNKNOWN:
723 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /* For unknown commands assume block transfers if the transfer
725 * size/allocation length is >= 1024 */
726 possible_len = (wanted_len < 1024) ? 0 : wanted_len;
727 break;
728 }
729 }
730 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* Last step: apply the hard limit on DMA transfers */
Roman Zippelc28bda22007-05-01 22:32:36 +0200733 limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 STRAM_BUFFER_SIZE : 255*512;
735 if (possible_len > limit)
736 possible_len = limit;
737
738 if (possible_len != wanted_len)
Finn Thaind65e6342014-03-18 11:42:20 +1100739 dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 "instead of %ld\n", possible_len, wanted_len);
741
Roman Zippelc28bda22007-05-01 22:32:36 +0200742 return possible_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745
746#endif /* REAL_DMA */
747
748
749/* NCR5380 register access functions
750 *
751 * There are separate functions for TT and Falcon, because the access
752 * methods are quite different. The calling macros NCR5380_read and
753 * NCR5380_write call these functions via function pointers.
754 */
755
Roman Zippelc28bda22007-05-01 22:32:36 +0200756static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Roman Zippelc28bda22007-05-01 22:32:36 +0200758 return tt_scsi_regp[reg * 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Roman Zippelc28bda22007-05-01 22:32:36 +0200761static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 tt_scsi_regp[reg * 2] = value;
764}
765
Roman Zippelc28bda22007-05-01 22:32:36 +0200766static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 dma_wd.dma_mode_status= (u_short)(0x88 + reg);
Roman Zippelc28bda22007-05-01 22:32:36 +0200769 return (u_char)dma_wd.fdc_acces_seccount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Roman Zippelc28bda22007-05-01 22:32:36 +0200772static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 dma_wd.dma_mode_status = (u_short)(0x88 + reg);
775 dma_wd.fdc_acces_seccount = (u_short)value;
776}
777
778
779#include "atari_NCR5380.c"
780
Finn Thain4d3d2a52014-11-12 16:11:52 +1100781static int atari_scsi_bus_reset(struct scsi_cmnd *cmd)
782{
783 int rv;
Finn Thaine3c3da62014-11-12 16:12:15 +1100784 unsigned long flags;
Finn Thain4d3d2a52014-11-12 16:11:52 +1100785
Finn Thaine3c3da62014-11-12 16:12:15 +1100786 local_irq_save(flags);
787
788#ifdef REAL_DMA
789 /* Abort a maybe active DMA transfer */
Finn Thain4d3d2a52014-11-12 16:11:52 +1100790 if (IS_A_TT()) {
Finn Thain4d3d2a52014-11-12 16:11:52 +1100791 tt_scsi_dma.dma_ctrl = 0;
Finn Thain4d3d2a52014-11-12 16:11:52 +1100792 } else {
Finn Thain4d3d2a52014-11-12 16:11:52 +1100793 st_dma.dma_mode_status = 0x90;
794 atari_dma_active = 0;
795 atari_dma_orig_addr = NULL;
Finn Thain4d3d2a52014-11-12 16:11:52 +1100796 }
Finn Thaine3c3da62014-11-12 16:12:15 +1100797#endif
Finn Thain4d3d2a52014-11-12 16:11:52 +1100798
799 rv = NCR5380_bus_reset(cmd);
800
Finn Thaine3c3da62014-11-12 16:12:15 +1100801 /* The 5380 raises its IRQ line while _RST is active but the ST DMA
802 * "lock" has been released so this interrupt may end up handled by
803 * floppy or IDE driver (if one of them holds the lock). The NCR5380
804 * interrupt flag has been cleared already.
805 */
Finn Thain4d3d2a52014-11-12 16:11:52 +1100806
Finn Thaine3c3da62014-11-12 16:12:15 +1100807 local_irq_restore(flags);
Finn Thain4d3d2a52014-11-12 16:11:52 +1100808
809 return rv;
810}
811
Finn Thain3ff228a2014-11-12 16:12:09 +1100812#define DRV_MODULE_NAME "atari_scsi"
813#define PFX DRV_MODULE_NAME ": "
814
815static struct scsi_host_template atari_scsi_template = {
816 .module = THIS_MODULE,
817 .proc_name = DRV_MODULE_NAME,
Al Virod89537e2013-03-31 13:24:44 -0400818 .show_info = atari_scsi_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 .name = "Atari native SCSI",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 .info = atari_scsi_info,
821 .queuecommand = atari_scsi_queue_command,
822 .eh_abort_handler = atari_scsi_abort,
823 .eh_bus_reset_handler = atari_scsi_bus_reset,
Finn Thain3ff228a2014-11-12 16:12:09 +1100824 .this_id = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .use_clustering = DISABLE_CLUSTERING
826};
827
Finn Thain3ff228a2014-11-12 16:12:09 +1100828static int __init atari_scsi_probe(struct platform_device *pdev)
829{
830 struct Scsi_Host *instance;
831 int error;
832 struct resource *irq;
Finn Thainef1081c2014-11-12 16:12:14 +1100833 int host_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Finn Thain3ff228a2014-11-12 16:12:09 +1100835 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
836 if (!irq)
837 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Finn Thain3ff228a2014-11-12 16:12:09 +1100839 if (ATARIHW_PRESENT(TT_SCSI)) {
840 atari_scsi_reg_read = atari_scsi_tt_reg_read;
841 atari_scsi_reg_write = atari_scsi_tt_reg_write;
842 } else {
843 atari_scsi_reg_read = atari_scsi_falcon_reg_read;
844 atari_scsi_reg_write = atari_scsi_falcon_reg_write;
845 }
846
847 /* The values for CMD_PER_LUN and CAN_QUEUE are somehow arbitrary.
848 * Higher values should work, too; try it!
849 * (But cmd_per_lun costs memory!)
850 *
851 * But there seems to be a bug somewhere that requires CAN_QUEUE to be
852 * 2*CMD_PER_LUN. At least on a TT, no spurious timeouts seen since
853 * changed CMD_PER_LUN...
854 *
855 * Note: The Falcon currently uses 8/1 setting due to unsolved problems
856 * with cmd_per_lun != 1
857 */
858 if (ATARIHW_PRESENT(TT_SCSI)) {
859 atari_scsi_template.can_queue = 16;
860 atari_scsi_template.cmd_per_lun = 8;
861 atari_scsi_template.sg_tablesize = SG_ALL;
862 } else {
863 atari_scsi_template.can_queue = 8;
864 atari_scsi_template.cmd_per_lun = 1;
865 atari_scsi_template.sg_tablesize = SG_NONE;
866 }
867
868 if (setup_can_queue > 0)
869 atari_scsi_template.can_queue = setup_can_queue;
870
871 if (setup_cmd_per_lun > 0)
872 atari_scsi_template.cmd_per_lun = setup_cmd_per_lun;
873
874 /* Leave sg_tablesize at 0 on a Falcon! */
875 if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize >= 0)
876 atari_scsi_template.sg_tablesize = setup_sg_tablesize;
877
878 if (setup_hostid >= 0) {
879 atari_scsi_template.this_id = setup_hostid & 7;
880 } else {
881 /* Test if a host id is set in the NVRam */
882 if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
883 unsigned char b = nvram_read_byte(14);
884
885 /* Arbitration enabled? (for TOS)
886 * If yes, use configured host ID
887 */
888 if (b & 0x80)
889 atari_scsi_template.this_id = b & 7;
890 }
891 }
892
Finn Thain3ff228a2014-11-12 16:12:09 +1100893
894#ifdef REAL_DMA
895 /* If running on a Falcon and if there's TT-Ram (i.e., more than one
896 * memory block, since there's always ST-Ram in a Falcon), then
897 * allocate a STRAM_BUFFER_SIZE byte dribble buffer for transfers
898 * from/to alternative Ram.
899 */
900 if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) &&
901 m68k_num_memory > 1) {
902 atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
903 if (!atari_dma_buffer) {
904 pr_err(PFX "can't allocate ST-RAM double buffer\n");
905 return -ENOMEM;
906 }
907 atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
908 atari_dma_orig_addr = 0;
909 }
910#endif
911
912 instance = scsi_host_alloc(&atari_scsi_template,
913 sizeof(struct NCR5380_hostdata));
914 if (!instance) {
915 error = -ENOMEM;
916 goto fail_alloc;
917 }
918 atari_scsi_host = instance;
919
920#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
921 atari_scsi_reset_boot();
922#endif
923
924 instance->irq = irq->start;
925
Finn Thainef1081c2014-11-12 16:12:14 +1100926 host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP;
927
Finn Thainca513fc2014-11-12 16:12:19 +1100928#ifdef SUPPORT_TAGS
929 host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
930#endif
931
Finn Thainef1081c2014-11-12 16:12:14 +1100932 NCR5380_init(instance, host_flags);
Finn Thain3ff228a2014-11-12 16:12:09 +1100933
934 if (IS_A_TT()) {
935 error = request_irq(instance->irq, scsi_tt_intr, 0,
936 "NCR5380", instance);
937 if (error) {
938 pr_err(PFX "request irq %d failed, aborting\n",
939 instance->irq);
940 goto fail_irq;
941 }
942 tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
943#ifdef REAL_DMA
944 tt_scsi_dma.dma_ctrl = 0;
945 atari_dma_residual = 0;
946
947 /* While the read overruns (described by Drew Eckhardt in
948 * NCR5380.c) never happened on TTs, they do in fact on the
949 * Medusa (This was the cause why SCSI didn't work right for
950 * so long there.) Since handling the overruns slows down
951 * a bit, I turned the #ifdef's into a runtime condition.
952 *
953 * In principle it should be sufficient to do max. 1 byte with
954 * PIO, but there is another problem on the Medusa with the DMA
Finn Thainef1081c2014-11-12 16:12:14 +1100955 * rest data register. So read_overruns is currently set
Finn Thain3ff228a2014-11-12 16:12:09 +1100956 * to 4 to avoid having transfers that aren't a multiple of 4.
957 * If the rest data bug is fixed, this can be lowered to 1.
958 */
Finn Thainef1081c2014-11-12 16:12:14 +1100959 if (MACH_IS_MEDUSA) {
960 struct NCR5380_hostdata *hostdata =
961 shost_priv(instance);
962
963 hostdata->read_overruns = 4;
964 }
Finn Thain3ff228a2014-11-12 16:12:09 +1100965#endif
966 } else {
967 /* Nothing to do for the interrupt: the ST-DMA is initialized
968 * already.
969 */
970#ifdef REAL_DMA
971 atari_dma_residual = 0;
972 atari_dma_active = 0;
973 atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
974 : 0xff000000);
975#endif
976 }
977
978 error = scsi_add_host(instance, NULL);
979 if (error)
980 goto fail_host;
981
982 platform_set_drvdata(pdev, instance);
983
984 scsi_scan_host(instance);
985 return 0;
986
987fail_host:
988 if (IS_A_TT())
989 free_irq(instance->irq, instance);
990fail_irq:
991 NCR5380_exit(instance);
992 scsi_host_put(instance);
993fail_alloc:
994 if (atari_dma_buffer)
995 atari_stram_free(atari_dma_buffer);
996 return error;
997}
998
999static int __exit atari_scsi_remove(struct platform_device *pdev)
1000{
1001 struct Scsi_Host *instance = platform_get_drvdata(pdev);
1002
1003 scsi_remove_host(instance);
1004 if (IS_A_TT())
1005 free_irq(instance->irq, instance);
1006 NCR5380_exit(instance);
1007 scsi_host_put(instance);
1008 if (atari_dma_buffer)
1009 atari_stram_free(atari_dma_buffer);
1010 return 0;
1011}
1012
1013static struct platform_driver atari_scsi_driver = {
1014 .remove = __exit_p(atari_scsi_remove),
1015 .driver = {
1016 .name = DRV_MODULE_NAME,
1017 .owner = THIS_MODULE,
1018 },
1019};
1020
1021module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe);
1022
1023MODULE_ALIAS("platform:" DRV_MODULE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024MODULE_LICENSE("GPL");