blob: e031f6dbcebc7f54fc82231b086fe0c81abe2012 [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
92
93#define NCR5380_implementation_fields /* none */
94
95#define NCR5380_read(reg) atari_scsi_reg_read(reg)
96#define NCR5380_write(reg, value) atari_scsi_reg_write(reg, value)
97
98#define NCR5380_queue_command atari_scsi_queue_command
99#define NCR5380_abort atari_scsi_abort
100#define NCR5380_show_info atari_scsi_show_info
101#define NCR5380_info atari_scsi_info
102
103#define NCR5380_dma_read_setup(instance, data, count) \
104 atari_scsi_dma_setup(instance, data, count, 0)
105#define NCR5380_dma_write_setup(instance, data, count) \
106 atari_scsi_dma_setup(instance, data, count, 1)
107#define NCR5380_dma_residual(instance) \
108 atari_scsi_dma_residual(instance)
109#define NCR5380_dma_xfer_len(instance, cmd, phase) \
110 atari_dma_xfer_len(cmd->SCp.this_residual, cmd, !((phase) & SR_IO))
111
Finn Thain3ff228a2014-11-12 16:12:09 +1100112#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Finn Thain4e705202014-11-12 16:12:10 +1100114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define IS_A_TT() ATARIHW_PRESENT(TT_SCSI)
116
117#define SCSI_DMA_WRITE_P(elt,val) \
118 do { \
119 unsigned long v = val; \
120 tt_scsi_dma.elt##_lo = v & 0xff; \
121 v >>= 8; \
122 tt_scsi_dma.elt##_lmd = v & 0xff; \
123 v >>= 8; \
124 tt_scsi_dma.elt##_hmd = v & 0xff; \
125 v >>= 8; \
126 tt_scsi_dma.elt##_hi = v & 0xff; \
127 } while(0)
128
129#define SCSI_DMA_READ_P(elt) \
130 (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \
131 (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \
132 (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \
133 (unsigned long)tt_scsi_dma.elt##_lo)
134
135
136static inline void SCSI_DMA_SETADR(unsigned long adr)
137{
138 st_dma.dma_lo = (unsigned char)adr;
139 MFPDELAY();
140 adr >>= 8;
141 st_dma.dma_md = (unsigned char)adr;
142 MFPDELAY();
143 adr >>= 8;
144 st_dma.dma_hi = (unsigned char)adr;
145 MFPDELAY();
146}
147
148static inline unsigned long SCSI_DMA_GETADR(void)
149{
150 unsigned long adr;
151 adr = st_dma.dma_lo;
152 MFPDELAY();
153 adr |= (st_dma.dma_md & 0xff) << 8;
154 MFPDELAY();
155 adr |= (st_dma.dma_hi & 0xff) << 16;
156 MFPDELAY();
157 return adr;
158}
159
160static inline void ENABLE_IRQ(void)
161{
162 if (IS_A_TT())
163 atari_enable_irq(IRQ_TT_MFP_SCSI);
164 else
165 atari_enable_irq(IRQ_MFP_FSCSI);
166}
167
168static inline void DISABLE_IRQ(void)
169{
170 if (IS_A_TT())
171 atari_disable_irq(IRQ_TT_MFP_SCSI);
172 else
173 atari_disable_irq(IRQ_MFP_FSCSI);
174}
175
176
177#define HOSTDATA_DMALEN (((struct NCR5380_hostdata *) \
178 (atari_scsi_host->hostdata))->dma_len)
179
180/* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
181 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
182 * need ten times the standard value... */
183#ifndef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
184#define AFTER_RESET_DELAY (HZ/2)
185#else
186#define AFTER_RESET_DELAY (5*HZ/2)
187#endif
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +0200190static void atari_scsi_fetch_restbytes(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Roman Zippelc28bda22007-05-01 22:32:36 +0200193static struct Scsi_Host *atari_scsi_host;
194static unsigned char (*atari_scsi_reg_read)(unsigned char reg);
195static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197#ifdef REAL_DMA
198static unsigned long atari_dma_residual, atari_dma_startaddr;
199static short atari_dma_active;
200/* pointer to the dribble buffer */
Roman Zippelc28bda22007-05-01 22:32:36 +0200201static char *atari_dma_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* precalculated physical address of the dribble buffer */
203static unsigned long atari_dma_phys_buffer;
204/* != 0 tells the Falcon int handler to copy data from the dribble buffer */
205static char *atari_dma_orig_addr;
206/* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
207 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
208 * cases where requests to physical contiguous buffers have been merged, this
209 * request is <= 4k (one page). So I don't think we have to split transfers
210 * just due to this buffer size...
211 */
212#define STRAM_BUFFER_SIZE (4096)
213/* mask for address bits that can't be used with the ST-DMA */
214static unsigned long atari_dma_stram_mask;
215#define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0)
216/* number of bytes to cut from a transfer to handle NCR overruns */
Roman Zippelc28bda22007-05-01 22:32:36 +0200217static int atari_read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
219
220static int setup_can_queue = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800221module_param(setup_can_queue, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static int setup_cmd_per_lun = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800223module_param(setup_cmd_per_lun, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int setup_sg_tablesize = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800225module_param(setup_sg_tablesize, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#ifdef SUPPORT_TAGS
227static int setup_use_tagged_queuing = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800228module_param(setup_use_tagged_queuing, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#endif
230static int setup_hostid = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800231module_param(setup_hostid, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#if defined(REAL_DMA)
235
Roman Zippelc28bda22007-05-01 22:32:36 +0200236static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 int i;
Roman Zippelc28bda22007-05-01 22:32:36 +0200239 unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (dma_stat & 0x01) {
242
243 /* A bus error happens when DMA-ing from the last page of a
244 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
245 * Check for this case:
246 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200247
248 for (i = 0; i < m68k_num_memory; ++i) {
249 end_addr = m68k_memory[i].addr + m68k_memory[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (end_addr <= addr && addr <= end_addr + 4)
Roman Zippelc28bda22007-05-01 22:32:36 +0200251 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257
258#if 0
259/* Dead code... wasn't called anyway :-) and causes some trouble, because at
260 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
261 * to clear the DMA int pending bit before it allows other level 6 interrupts.
262 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200263static void scsi_dma_buserr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Roman Zippelc28bda22007-05-01 22:32:36 +0200265 unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* Don't do anything if a NCR interrupt is pending. Probably it's just
268 * masked... */
Roman Zippelc28bda22007-05-01 22:32:36 +0200269 if (atari_irq_pending(IRQ_TT_MFP_SCSI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return;
Roman Zippelc28bda22007-05-01 22:32:36 +0200271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
273 SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
274 if (dma_stat & 0x80) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200275 if (!scsi_dma_is_ignored_buserr(dma_stat))
276 printk("SCSI DMA bus error -- bad DMA programming!\n");
277 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* Under normal circumstances we never should get to this point,
279 * since both interrupts are triggered simultaneously and the 5380
280 * int has higher priority. When this irq is handled, that DMA
281 * interrupt is cleared. So a warning message is printed here.
282 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200283 printk("SCSI DMA intr ?? -- this shouldn't happen!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285}
286#endif
287
288#endif
289
290
Roman Zippelc28bda22007-05-01 22:32:36 +0200291static irqreturn_t scsi_tt_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293#ifdef REAL_DMA
294 int dma_stat;
295
296 dma_stat = tt_scsi_dma.dma_ctrl;
297
Finn Thaind65e6342014-03-18 11:42:20 +1100298 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 interrupt, DMA status = %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 atari_scsi_host->host_no, dma_stat & 0xff);
300
301 /* Look if it was the DMA that has interrupted: First possibility
302 * is that a bus error occurred...
303 */
304 if (dma_stat & 0x80) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200305 if (!scsi_dma_is_ignored_buserr(dma_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
307 SCSI_DMA_READ_P(dma_addr));
308 printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
309 }
310 }
311
312 /* If the DMA is active but not finished, we have the case
313 * that some other 5380 interrupt occurred within the DMA transfer.
314 * This means we have residual bytes, if the desired end address
315 * is not yet reached. Maybe we have to fetch some bytes from the
316 * rest data register, too. The residual must be calculated from
317 * the address pointer, not the counter register, because only the
318 * addr reg counts bytes not yet written and pending in the rest
319 * data reg!
320 */
321 if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200322 atari_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Finn Thaind65e6342014-03-18 11:42:20 +1100324 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 atari_dma_residual);
326
327 if ((signed int)atari_dma_residual < 0)
328 atari_dma_residual = 0;
329 if ((dma_stat & 1) == 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200330 /*
331 * After read operations, we maybe have to
332 * transport some rest bytes
333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 atari_scsi_fetch_restbytes();
Roman Zippelc28bda22007-05-01 22:32:36 +0200335 } else {
336 /*
337 * There seems to be a nasty bug in some SCSI-DMA/NCR
338 * combinations: If a target disconnects while a write
339 * operation is going on, the address register of the
340 * DMA may be a few bytes farer than it actually read.
341 * This is probably due to DMA prefetching and a delay
342 * between DMA and NCR. Experiments showed that the
343 * dma_addr is 9 bytes to high, but this could vary.
344 * The problem is, that the residual is thus calculated
345 * wrong and the next transfer will start behind where
346 * it should. So we round up the residual to the next
347 * multiple of a sector size, if it isn't already a
348 * multiple and the originally expected transfer size
349 * was. The latter condition is there to ensure that
350 * the correction is taken only for "real" data
351 * transfers and not for, e.g., the parameters of some
352 * other command. These shouldn't disconnect anyway.
353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (atari_dma_residual & 0x1ff) {
Finn Thaind65e6342014-03-18 11:42:20 +1100355 dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 "difference %ld bytes\n",
357 512 - (atari_dma_residual & 0x1ff));
358 atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
359 }
360 }
361 tt_scsi_dma.dma_ctrl = 0;
362 }
363
364 /* If the DMA is finished, fetch the rest bytes and turn it off */
365 if (dma_stat & 0x40) {
366 atari_dma_residual = 0;
367 if ((dma_stat & 1) == 0)
368 atari_scsi_fetch_restbytes();
369 tt_scsi_dma.dma_ctrl = 0;
370 }
371
372#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +0200373
Jeff Garzik1e641662007-11-11 19:52:05 -0500374 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376#if 0
377 /* To be sure the int is not masked */
Roman Zippelc28bda22007-05-01 22:32:36 +0200378 atari_enable_irq(IRQ_TT_MFP_SCSI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379#endif
380 return IRQ_HANDLED;
381}
382
383
Roman Zippelc28bda22007-05-01 22:32:36 +0200384static irqreturn_t scsi_falcon_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386#ifdef REAL_DMA
387 int dma_stat;
388
389 /* Turn off DMA and select sector counter register before
390 * accessing the status register (Atari recommendation!)
391 */
392 st_dma.dma_mode_status = 0x90;
393 dma_stat = st_dma.dma_mode_status;
394
395 /* Bit 0 indicates some error in the DMA process... don't know
396 * what happened exactly (no further docu).
397 */
398 if (!(dma_stat & 0x01)) {
399 /* DMA error */
400 printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
401 }
402
403 /* If the DMA was active, but now bit 1 is not clear, it is some
404 * other 5380 interrupt that finishes the DMA transfer. We have to
405 * calculate the number of residual bytes and give a warning if
406 * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
407 */
408 if (atari_dma_active && (dma_stat & 0x02)) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200409 unsigned long transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
412 /* The ST-DMA address is incremented in 2-byte steps, but the
413 * data are written only in 16-byte chunks. If the number of
414 * transferred bytes is not divisible by 16, the remainder is
415 * lost somewhere in outer space.
416 */
417 if (transferred & 15)
418 printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
419 "ST-DMA fifo\n", transferred & 15);
420
421 atari_dma_residual = HOSTDATA_DMALEN - transferred;
Finn Thaind65e6342014-03-18 11:42:20 +1100422 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 atari_dma_residual);
Roman Zippelc28bda22007-05-01 22:32:36 +0200424 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 atari_dma_residual = 0;
426 atari_dma_active = 0;
427
428 if (atari_dma_orig_addr) {
429 /* If the dribble buffer was used on a read operation, copy the DMA-ed
430 * data to the original destination address.
431 */
432 memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
433 HOSTDATA_DMALEN - atari_dma_residual);
434 atari_dma_orig_addr = NULL;
435 }
436
437#endif /* REAL_DMA */
438
Jeff Garzik1e641662007-11-11 19:52:05 -0500439 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return IRQ_HANDLED;
441}
442
443
444#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +0200445static void atari_scsi_fetch_restbytes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 int nr;
448 char *src, *dst;
449 unsigned long phys_dst;
450
451 /* fetch rest bytes in the DMA register */
452 phys_dst = SCSI_DMA_READ_P(dma_addr);
453 nr = phys_dst & 3;
454 if (nr) {
455 /* there are 'nr' bytes left for the last long address
456 before the DMA pointer */
457 phys_dst ^= nr;
Finn Thaind65e6342014-03-18 11:42:20 +1100458 dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 nr, phys_dst);
460 /* The content of the DMA pointer is a physical address! */
461 dst = phys_to_virt(phys_dst);
Finn Thaind65e6342014-03-18 11:42:20 +1100462 dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
464 *dst++ = *src++;
465 }
466}
467#endif /* REAL_DMA */
468
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470static int falcon_dont_release = 0;
471
472/* This function releases the lock on the DMA chip if there is no
Finn Thain16b29e72014-11-12 16:12:08 +1100473 * connected command and the disconnected queue is empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
475
Roman Zippelc28bda22007-05-01 22:32:36 +0200476static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 unsigned long flags;
Roman Zippelc28bda22007-05-01 22:32:36 +0200479
480 if (IS_A_TT())
481 return;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 local_irq_save(flags);
484
Finn Thain16b29e72014-11-12 16:12:08 +1100485 if (!hostdata->disconnected_queue &&
486 !hostdata->issue_queue &&
487 !hostdata->connected &&
488 !falcon_dont_release &&
489 stdma_is_locked_by(scsi_falcon_intr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 stdma_release();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 local_irq_restore(flags);
493}
494
495/* This function manages the locking of the ST-DMA.
496 * If the DMA isn't locked already for SCSI, it tries to lock it by
497 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
498 * there are other drivers waiting for the chip, we do not issue the
Finn Thain16b29e72014-11-12 16:12:08 +1100499 * command immediately but tell the SCSI mid-layer to defer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
501
Finn Thain16b29e72014-11-12 16:12:08 +1100502static int falcon_get_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Roman Zippelc28bda22007-05-01 22:32:36 +0200504 if (IS_A_TT())
Finn Thain16b29e72014-11-12 16:12:08 +1100505 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Finn Thain16b29e72014-11-12 16:12:08 +1100507 if (in_interrupt())
508 return stdma_try_lock(scsi_falcon_intr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Finn Thain16b29e72014-11-12 16:12:08 +1100510 stdma_lock(scsi_falcon_intr, NULL);
511 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100514#ifndef MODULE
515static int __init atari_scsi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 /* Format of atascsi parameter is:
518 * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
Finn Thain3ff228a2014-11-12 16:12:09 +1100519 * Defaults depend on TT or Falcon, determined at run time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * Negative values mean don't change.
521 */
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100522 int ints[6];
523
524 get_options(str, ARRAY_SIZE(ints), ints);
Roman Zippelc28bda22007-05-01 22:32:36 +0200525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (ints[0] < 1) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200527 printk("atari_scsi_setup: no arguments!\n");
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Finn Thain3ff228a2014-11-12 16:12:09 +1100530 if (ints[0] >= 1)
531 setup_can_queue = ints[1];
532 if (ints[0] >= 2)
533 setup_cmd_per_lun = ints[2];
534 if (ints[0] >= 3)
535 setup_sg_tablesize = ints[3];
536 if (ints[0] >= 4)
537 setup_hostid = ints[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#ifdef SUPPORT_TAGS
Finn Thain3ff228a2014-11-12 16:12:09 +1100539 if (ints[0] >= 5)
540 setup_use_tagged_queuing = ints[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#endif
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100542
543 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100546__setup("atascsi=", atari_scsi_setup);
547#endif /* !MODULE */
548
Roman Zippelc28bda22007-05-01 22:32:36 +0200549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
551static void __init atari_scsi_reset_boot(void)
552{
553 unsigned long end;
Roman Zippelc28bda22007-05-01 22:32:36 +0200554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
556 * Do a SCSI reset to clean up the bus during initialization. No messing
557 * with the queues, interrupts, or locks necessary here.
558 */
559
Roman Zippelc28bda22007-05-01 22:32:36 +0200560 printk("Atari SCSI: resetting the SCSI bus...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /* get in phase */
Roman Zippelc28bda22007-05-01 22:32:36 +0200563 NCR5380_write(TARGET_COMMAND_REG,
564 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* assert RST */
Roman Zippelc28bda22007-05-01 22:32:36 +0200567 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* The min. reset hold time is 25us, so 40us should be enough */
Roman Zippelc28bda22007-05-01 22:32:36 +0200569 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* reset RST and interrupt */
Roman Zippelc28bda22007-05-01 22:32:36 +0200571 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
572 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 end = jiffies + AFTER_RESET_DELAY;
575 while (time_before(jiffies, end))
576 barrier();
577
Roman Zippelc28bda22007-05-01 22:32:36 +0200578 printk(" done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580#endif
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#if defined(REAL_DMA)
583
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200584static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance,
585 void *data, unsigned long count,
586 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Roman Zippelc28bda22007-05-01 22:32:36 +0200588 unsigned long addr = virt_to_phys(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Finn Thaind65e6342014-03-18 11:42:20 +1100590 dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 "dir = %d\n", instance->host_no, data, addr, count, dir);
592
593 if (!IS_A_TT() && !STRAM_ADDR(addr)) {
594 /* If we have a non-DMAable address on a Falcon, use the dribble
595 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
596 * handler to copy data from the dribble buffer to the originally
597 * wanted address.
598 */
599 if (dir)
Roman Zippelc28bda22007-05-01 22:32:36 +0200600 memcpy(atari_dma_buffer, data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 else
602 atari_dma_orig_addr = data;
603 addr = atari_dma_phys_buffer;
604 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 atari_dma_startaddr = addr; /* Needed for calculating residual later. */
Roman Zippelc28bda22007-05-01 22:32:36 +0200607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Cache cleanup stuff: On writes, push any dirty cache out before sending
609 * it to the peripheral. (Must be done before DMA setup, since at least
610 * the ST-DMA begins to fill internal buffers right after setup. For
611 * reads, invalidate any cache, may be altered after DMA without CPU
612 * knowledge.
Roman Zippelc28bda22007-05-01 22:32:36 +0200613 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * ++roman: For the Medusa, there's no need at all for that cache stuff,
615 * because the hardware does bus snooping (fine!).
616 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200617 dma_cache_maintenance(addr, count, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (count == 0)
620 printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
621
622 if (IS_A_TT()) {
623 tt_scsi_dma.dma_ctrl = dir;
Roman Zippelc28bda22007-05-01 22:32:36 +0200624 SCSI_DMA_WRITE_P(dma_addr, addr);
625 SCSI_DMA_WRITE_P(dma_cnt, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 tt_scsi_dma.dma_ctrl = dir | 2;
Roman Zippelc28bda22007-05-01 22:32:36 +0200627 } else { /* ! IS_A_TT */
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* set address */
Roman Zippelc28bda22007-05-01 22:32:36 +0200630 SCSI_DMA_SETADR(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* toggle direction bit to clear FIFO and set DMA direction */
633 dir <<= 8;
634 st_dma.dma_mode_status = 0x90 | dir;
635 st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
636 st_dma.dma_mode_status = 0x90 | dir;
637 udelay(40);
638 /* On writes, round up the transfer length to the next multiple of 512
639 * (see also comment at atari_dma_xfer_len()). */
640 st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
641 udelay(40);
642 st_dma.dma_mode_status = 0x10 | dir;
643 udelay(40);
644 /* need not restore value of dir, only boolean value is tested */
645 atari_dma_active = 1;
646 }
647
Roman Zippelc28bda22007-05-01 22:32:36 +0200648 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651
Roman Zippelc28bda22007-05-01 22:32:36 +0200652static long atari_scsi_dma_residual(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Roman Zippelc28bda22007-05-01 22:32:36 +0200654 return atari_dma_residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657
658#define CMD_SURELY_BLOCK_MODE 0
659#define CMD_SURELY_BYTE_MODE 1
660#define CMD_MODE_UNKNOWN 2
661
Finn Thain710ddd02014-11-12 16:12:02 +1100662static int falcon_classify_cmd(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 unsigned char opcode = cmd->cmnd[0];
Roman Zippelc28bda22007-05-01 22:32:36 +0200665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
Roman Zippelc28bda22007-05-01 22:32:36 +0200667 opcode == READ_BUFFER)
668 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 else if (opcode == READ_6 || opcode == READ_10 ||
670 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
671 opcode == RECOVER_BUFFERED_DATA) {
672 /* In case of a sequential-access target (tape), special care is
673 * needed here: The transfer is block-mode only if the 'fixed' bit is
674 * set! */
675 if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
Roman Zippelc28bda22007-05-01 22:32:36 +0200676 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 else
Roman Zippelc28bda22007-05-01 22:32:36 +0200678 return CMD_SURELY_BLOCK_MODE;
679 } else
680 return CMD_MODE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683
684/* This function calculates the number of bytes that can be transferred via
685 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
686 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
687 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
688 * possible on the Falcon, since that would require to program the DMA for
689 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
690 * the overrun problem, so this question is academic :-)
691 */
692
Roman Zippelc28bda22007-05-01 22:32:36 +0200693static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
Finn Thain710ddd02014-11-12 16:12:02 +1100694 struct scsi_cmnd *cmd, int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 unsigned long possible_len, limit;
Adrian Bunk29c8a242008-10-13 21:58:59 +0200697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (IS_A_TT())
699 /* TT SCSI DMA can transfer arbitrary #bytes */
Roman Zippelc28bda22007-05-01 22:32:36 +0200700 return wanted_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
703 * 255*512 bytes, but this should be enough)
704 *
705 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
706 * that return a number of bytes which cannot be known beforehand. In this
707 * case, the given transfer length is an "allocation length". Now it
708 * can happen that this allocation length is a multiple of 512 bytes and
709 * the DMA is used. But if not n*512 bytes really arrive, some input data
710 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
711 * between commands that do block transfers and those that do byte
712 * transfers. But this isn't easy... there are lots of vendor specific
713 * commands, and the user can issue any command via the
714 * SCSI_IOCTL_SEND_COMMAND.
715 *
716 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
717 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
718 * and 3), the thing to do is obvious: allow any number of blocks via DMA
719 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
720 * the transfer (allocation) length is < 1024, hoping that no cmd. not
721 * explicitly known as byte mode have such big allocation lengths...
722 * BTW, all the discussion above applies only to reads. DMA writes are
723 * unproblematic anyways, since the targets aborts the transfer after
724 * receiving a sufficient number of bytes.
725 *
726 * Another point: If the transfer is from/to an non-ST-RAM address, we
727 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
728 */
729
730 if (write_flag) {
731 /* Write operation can always use the DMA, but the transfer size must
732 * be rounded up to the next multiple of 512 (atari_dma_setup() does
733 * this).
734 */
735 possible_len = wanted_len;
Roman Zippelc28bda22007-05-01 22:32:36 +0200736 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* Read operations: if the wanted transfer length is not a multiple of
738 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
739 * (no interrupt on DMA finished!)
740 */
741 if (wanted_len & 0x1ff)
742 possible_len = 0;
743 else {
744 /* Now classify the command (see above) and decide whether it is
745 * allowed to do DMA at all */
Roman Zippelc28bda22007-05-01 22:32:36 +0200746 switch (falcon_classify_cmd(cmd)) {
747 case CMD_SURELY_BLOCK_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 possible_len = wanted_len;
749 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200750 case CMD_SURELY_BYTE_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 possible_len = 0; /* DMA prohibited */
752 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200753 case CMD_MODE_UNKNOWN:
754 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* For unknown commands assume block transfers if the transfer
756 * size/allocation length is >= 1024 */
757 possible_len = (wanted_len < 1024) ? 0 : wanted_len;
758 break;
759 }
760 }
761 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* Last step: apply the hard limit on DMA transfers */
Roman Zippelc28bda22007-05-01 22:32:36 +0200764 limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 STRAM_BUFFER_SIZE : 255*512;
766 if (possible_len > limit)
767 possible_len = limit;
768
769 if (possible_len != wanted_len)
Finn Thaind65e6342014-03-18 11:42:20 +1100770 dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 "instead of %ld\n", possible_len, wanted_len);
772
Roman Zippelc28bda22007-05-01 22:32:36 +0200773 return possible_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776
777#endif /* REAL_DMA */
778
779
780/* NCR5380 register access functions
781 *
782 * There are separate functions for TT and Falcon, because the access
783 * methods are quite different. The calling macros NCR5380_read and
784 * NCR5380_write call these functions via function pointers.
785 */
786
Roman Zippelc28bda22007-05-01 22:32:36 +0200787static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Roman Zippelc28bda22007-05-01 22:32:36 +0200789 return tt_scsi_regp[reg * 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Roman Zippelc28bda22007-05-01 22:32:36 +0200792static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 tt_scsi_regp[reg * 2] = value;
795}
796
Roman Zippelc28bda22007-05-01 22:32:36 +0200797static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 dma_wd.dma_mode_status= (u_short)(0x88 + reg);
Roman Zippelc28bda22007-05-01 22:32:36 +0200800 return (u_char)dma_wd.fdc_acces_seccount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Roman Zippelc28bda22007-05-01 22:32:36 +0200803static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 dma_wd.dma_mode_status = (u_short)(0x88 + reg);
806 dma_wd.fdc_acces_seccount = (u_short)value;
807}
808
809
810#include "atari_NCR5380.c"
811
Finn Thain4d3d2a52014-11-12 16:11:52 +1100812static int atari_scsi_bus_reset(struct scsi_cmnd *cmd)
813{
814 int rv;
815 struct NCR5380_hostdata *hostdata = shost_priv(cmd->device->host);
816
817 /* For doing the reset, SCSI interrupts must be disabled first,
818 * since the 5380 raises its IRQ line while _RST is active and we
819 * can't disable interrupts completely, since we need the timer.
820 */
821 /* And abort a maybe active DMA transfer */
822 if (IS_A_TT()) {
823 atari_turnoff_irq(IRQ_TT_MFP_SCSI);
824#ifdef REAL_DMA
825 tt_scsi_dma.dma_ctrl = 0;
826#endif
827 } else {
828 atari_turnoff_irq(IRQ_MFP_FSCSI);
829#ifdef REAL_DMA
830 st_dma.dma_mode_status = 0x90;
831 atari_dma_active = 0;
832 atari_dma_orig_addr = NULL;
833#endif
834 }
835
836 rv = NCR5380_bus_reset(cmd);
837
838 if (IS_A_TT())
839 atari_turnon_irq(IRQ_TT_MFP_SCSI);
840 else
841 atari_turnon_irq(IRQ_MFP_FSCSI);
842
843 if (rv == SUCCESS)
844 falcon_release_lock_if_possible(hostdata);
845
846 return rv;
847}
848
Finn Thain3ff228a2014-11-12 16:12:09 +1100849#define DRV_MODULE_NAME "atari_scsi"
850#define PFX DRV_MODULE_NAME ": "
851
852static struct scsi_host_template atari_scsi_template = {
853 .module = THIS_MODULE,
854 .proc_name = DRV_MODULE_NAME,
Al Virod89537e2013-03-31 13:24:44 -0400855 .show_info = atari_scsi_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 .name = "Atari native SCSI",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 .info = atari_scsi_info,
858 .queuecommand = atari_scsi_queue_command,
859 .eh_abort_handler = atari_scsi_abort,
860 .eh_bus_reset_handler = atari_scsi_bus_reset,
Finn Thain3ff228a2014-11-12 16:12:09 +1100861 .this_id = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 .use_clustering = DISABLE_CLUSTERING
863};
864
Finn Thain3ff228a2014-11-12 16:12:09 +1100865static int __init atari_scsi_probe(struct platform_device *pdev)
866{
867 struct Scsi_Host *instance;
868 int error;
869 struct resource *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Finn Thain3ff228a2014-11-12 16:12:09 +1100871 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
872 if (!irq)
873 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Finn Thain3ff228a2014-11-12 16:12:09 +1100875 if (ATARIHW_PRESENT(TT_SCSI)) {
876 atari_scsi_reg_read = atari_scsi_tt_reg_read;
877 atari_scsi_reg_write = atari_scsi_tt_reg_write;
878 } else {
879 atari_scsi_reg_read = atari_scsi_falcon_reg_read;
880 atari_scsi_reg_write = atari_scsi_falcon_reg_write;
881 }
882
883 /* The values for CMD_PER_LUN and CAN_QUEUE are somehow arbitrary.
884 * Higher values should work, too; try it!
885 * (But cmd_per_lun costs memory!)
886 *
887 * But there seems to be a bug somewhere that requires CAN_QUEUE to be
888 * 2*CMD_PER_LUN. At least on a TT, no spurious timeouts seen since
889 * changed CMD_PER_LUN...
890 *
891 * Note: The Falcon currently uses 8/1 setting due to unsolved problems
892 * with cmd_per_lun != 1
893 */
894 if (ATARIHW_PRESENT(TT_SCSI)) {
895 atari_scsi_template.can_queue = 16;
896 atari_scsi_template.cmd_per_lun = 8;
897 atari_scsi_template.sg_tablesize = SG_ALL;
898 } else {
899 atari_scsi_template.can_queue = 8;
900 atari_scsi_template.cmd_per_lun = 1;
901 atari_scsi_template.sg_tablesize = SG_NONE;
902 }
903
904 if (setup_can_queue > 0)
905 atari_scsi_template.can_queue = setup_can_queue;
906
907 if (setup_cmd_per_lun > 0)
908 atari_scsi_template.cmd_per_lun = setup_cmd_per_lun;
909
910 /* Leave sg_tablesize at 0 on a Falcon! */
911 if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize >= 0)
912 atari_scsi_template.sg_tablesize = setup_sg_tablesize;
913
914 if (setup_hostid >= 0) {
915 atari_scsi_template.this_id = setup_hostid & 7;
916 } else {
917 /* Test if a host id is set in the NVRam */
918 if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
919 unsigned char b = nvram_read_byte(14);
920
921 /* Arbitration enabled? (for TOS)
922 * If yes, use configured host ID
923 */
924 if (b & 0x80)
925 atari_scsi_template.this_id = b & 7;
926 }
927 }
928
929#ifdef SUPPORT_TAGS
930 if (setup_use_tagged_queuing < 0)
931 setup_use_tagged_queuing = 0;
932#endif
933
934#ifdef REAL_DMA
935 /* If running on a Falcon and if there's TT-Ram (i.e., more than one
936 * memory block, since there's always ST-Ram in a Falcon), then
937 * allocate a STRAM_BUFFER_SIZE byte dribble buffer for transfers
938 * from/to alternative Ram.
939 */
940 if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) &&
941 m68k_num_memory > 1) {
942 atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
943 if (!atari_dma_buffer) {
944 pr_err(PFX "can't allocate ST-RAM double buffer\n");
945 return -ENOMEM;
946 }
947 atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
948 atari_dma_orig_addr = 0;
949 }
950#endif
951
952 instance = scsi_host_alloc(&atari_scsi_template,
953 sizeof(struct NCR5380_hostdata));
954 if (!instance) {
955 error = -ENOMEM;
956 goto fail_alloc;
957 }
958 atari_scsi_host = instance;
959
960#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
961 atari_scsi_reset_boot();
962#endif
963
964 instance->irq = irq->start;
965
966 NCR5380_init(instance, 0);
967
968 if (IS_A_TT()) {
969 error = request_irq(instance->irq, scsi_tt_intr, 0,
970 "NCR5380", instance);
971 if (error) {
972 pr_err(PFX "request irq %d failed, aborting\n",
973 instance->irq);
974 goto fail_irq;
975 }
976 tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
977#ifdef REAL_DMA
978 tt_scsi_dma.dma_ctrl = 0;
979 atari_dma_residual = 0;
980
981 /* While the read overruns (described by Drew Eckhardt in
982 * NCR5380.c) never happened on TTs, they do in fact on the
983 * Medusa (This was the cause why SCSI didn't work right for
984 * so long there.) Since handling the overruns slows down
985 * a bit, I turned the #ifdef's into a runtime condition.
986 *
987 * In principle it should be sufficient to do max. 1 byte with
988 * PIO, but there is another problem on the Medusa with the DMA
989 * rest data register. So 'atari_read_overruns' is currently set
990 * to 4 to avoid having transfers that aren't a multiple of 4.
991 * If the rest data bug is fixed, this can be lowered to 1.
992 */
993 if (MACH_IS_MEDUSA)
994 atari_read_overruns = 4;
995#endif
996 } else {
997 /* Nothing to do for the interrupt: the ST-DMA is initialized
998 * already.
999 */
1000#ifdef REAL_DMA
1001 atari_dma_residual = 0;
1002 atari_dma_active = 0;
1003 atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
1004 : 0xff000000);
1005#endif
1006 }
1007
1008 error = scsi_add_host(instance, NULL);
1009 if (error)
1010 goto fail_host;
1011
1012 platform_set_drvdata(pdev, instance);
1013
1014 scsi_scan_host(instance);
1015 return 0;
1016
1017fail_host:
1018 if (IS_A_TT())
1019 free_irq(instance->irq, instance);
1020fail_irq:
1021 NCR5380_exit(instance);
1022 scsi_host_put(instance);
1023fail_alloc:
1024 if (atari_dma_buffer)
1025 atari_stram_free(atari_dma_buffer);
1026 return error;
1027}
1028
1029static int __exit atari_scsi_remove(struct platform_device *pdev)
1030{
1031 struct Scsi_Host *instance = platform_get_drvdata(pdev);
1032
1033 scsi_remove_host(instance);
1034 if (IS_A_TT())
1035 free_irq(instance->irq, instance);
1036 NCR5380_exit(instance);
1037 scsi_host_put(instance);
1038 if (atari_dma_buffer)
1039 atari_stram_free(atari_dma_buffer);
1040 return 0;
1041}
1042
1043static struct platform_driver atari_scsi_driver = {
1044 .remove = __exit_p(atari_scsi_remove),
1045 .driver = {
1046 .name = DRV_MODULE_NAME,
1047 .owner = THIS_MODULE,
1048 },
1049};
1050
1051module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe);
1052
1053MODULE_ALIAS("platform:" DRV_MODULE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054MODULE_LICENSE("GPL");