blob: 445c26724ba1f22f43498b0dfcdf734a078b52ae [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/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/interrupt.h>
71#include <linux/init.h>
72#include <linux/nvram.h>
73#include <linux/bitops.h>
Arnd Bergmanneff9cf82014-03-01 20:51:03 +130074#include <linux/wait.h>
Finn Thain3ff228a2014-11-12 16:12:09 +110075#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#include <asm/setup.h>
78#include <asm/atarihw.h>
79#include <asm/atariints.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <asm/atari_stdma.h>
81#include <asm/atari_stram.h>
82#include <asm/io.h>
83
Finn Thain3ff228a2014-11-12 16:12:09 +110084#include <scsi/scsi_host.h>
85
Finn Thaine63449c2016-03-23 21:10:13 +110086#define DMA_MIN_SIZE 32
87
Finn Thain4e705202014-11-12 16:12:10 +110088/* Definitions for the core NCR5380 driver. */
89
Finn Thain4e705202014-11-12 16:12:10 +110090#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
Finn Thain4e705202014-11-12 16:12:10 +1100100#define NCR5380_info atari_scsi_info
101
Finn Thain52d3e562016-03-23 21:10:20 +1100102#define NCR5380_dma_recv_setup(instance, data, count) \
Finn Thain4e705202014-11-12 16:12:10 +1100103 atari_scsi_dma_setup(instance, data, count, 0)
Finn Thain52d3e562016-03-23 21:10:20 +1100104#define NCR5380_dma_send_setup(instance, data, count) \
Finn Thain4e705202014-11-12 16:12:10 +1100105 atari_scsi_dma_setup(instance, data, count, 1)
106#define NCR5380_dma_residual(instance) \
107 atari_scsi_dma_residual(instance)
108#define NCR5380_dma_xfer_len(instance, cmd, phase) \
109 atari_dma_xfer_len(cmd->SCp.this_residual, cmd, !((phase) & SR_IO))
110
Finn Thaina53a21e2014-11-12 16:12:21 +1100111#define NCR5380_acquire_dma_irq(instance) falcon_get_lock(instance)
Finn Thaine3c3da62014-11-12 16:12:15 +1100112#define NCR5380_release_dma_irq(instance) falcon_release_lock()
113
Finn Thain3ff228a2014-11-12 16:12:09 +1100114#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Finn Thain4e705202014-11-12 16:12:10 +1100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define IS_A_TT() ATARIHW_PRESENT(TT_SCSI)
118
119#define SCSI_DMA_WRITE_P(elt,val) \
120 do { \
121 unsigned long v = val; \
122 tt_scsi_dma.elt##_lo = v & 0xff; \
123 v >>= 8; \
124 tt_scsi_dma.elt##_lmd = v & 0xff; \
125 v >>= 8; \
126 tt_scsi_dma.elt##_hmd = v & 0xff; \
127 v >>= 8; \
128 tt_scsi_dma.elt##_hi = v & 0xff; \
129 } while(0)
130
131#define SCSI_DMA_READ_P(elt) \
132 (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \
133 (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \
134 (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \
135 (unsigned long)tt_scsi_dma.elt##_lo)
136
137
138static inline void SCSI_DMA_SETADR(unsigned long adr)
139{
140 st_dma.dma_lo = (unsigned char)adr;
141 MFPDELAY();
142 adr >>= 8;
143 st_dma.dma_md = (unsigned char)adr;
144 MFPDELAY();
145 adr >>= 8;
146 st_dma.dma_hi = (unsigned char)adr;
147 MFPDELAY();
148}
149
150static inline unsigned long SCSI_DMA_GETADR(void)
151{
152 unsigned long adr;
153 adr = st_dma.dma_lo;
154 MFPDELAY();
155 adr |= (st_dma.dma_md & 0xff) << 8;
156 MFPDELAY();
157 adr |= (st_dma.dma_hi & 0xff) << 16;
158 MFPDELAY();
159 return adr;
160}
161
Roman Zippelc28bda22007-05-01 22:32:36 +0200162static void atari_scsi_fetch_restbytes(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Roman Zippelc28bda22007-05-01 22:32:36 +0200164static unsigned char (*atari_scsi_reg_read)(unsigned char reg);
165static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static unsigned long atari_dma_residual, atari_dma_startaddr;
168static short atari_dma_active;
169/* pointer to the dribble buffer */
Roman Zippelc28bda22007-05-01 22:32:36 +0200170static char *atari_dma_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/* precalculated physical address of the dribble buffer */
172static unsigned long atari_dma_phys_buffer;
173/* != 0 tells the Falcon int handler to copy data from the dribble buffer */
174static char *atari_dma_orig_addr;
175/* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
176 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
177 * cases where requests to physical contiguous buffers have been merged, this
178 * request is <= 4k (one page). So I don't think we have to split transfers
179 * just due to this buffer size...
180 */
181#define STRAM_BUFFER_SIZE (4096)
182/* mask for address bits that can't be used with the ST-DMA */
183static unsigned long atari_dma_stram_mask;
184#define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186static int setup_can_queue = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800187module_param(setup_can_queue, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static int setup_cmd_per_lun = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800189module_param(setup_cmd_per_lun, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int setup_sg_tablesize = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800191module_param(setup_sg_tablesize, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static int setup_use_tagged_queuing = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800193module_param(setup_use_tagged_queuing, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static int setup_hostid = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800195module_param(setup_hostid, int, 0);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100196static int setup_toshiba_delay = -1;
197module_param(setup_toshiba_delay, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199
Roman Zippelc28bda22007-05-01 22:32:36 +0200200static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 int i;
Roman Zippelc28bda22007-05-01 22:32:36 +0200203 unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (dma_stat & 0x01) {
206
207 /* A bus error happens when DMA-ing from the last page of a
208 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
209 * Check for this case:
210 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200211
212 for (i = 0; i < m68k_num_memory; ++i) {
213 end_addr = m68k_memory[i].addr + m68k_memory[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (end_addr <= addr && addr <= end_addr + 4)
Roman Zippelc28bda22007-05-01 22:32:36 +0200215 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221
222#if 0
223/* Dead code... wasn't called anyway :-) and causes some trouble, because at
224 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
225 * to clear the DMA int pending bit before it allows other level 6 interrupts.
226 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200227static void scsi_dma_buserr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Roman Zippelc28bda22007-05-01 22:32:36 +0200229 unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* Don't do anything if a NCR interrupt is pending. Probably it's just
232 * masked... */
Roman Zippelc28bda22007-05-01 22:32:36 +0200233 if (atari_irq_pending(IRQ_TT_MFP_SCSI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return;
Roman Zippelc28bda22007-05-01 22:32:36 +0200235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
237 SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
238 if (dma_stat & 0x80) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200239 if (!scsi_dma_is_ignored_buserr(dma_stat))
240 printk("SCSI DMA bus error -- bad DMA programming!\n");
241 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* Under normal circumstances we never should get to this point,
243 * since both interrupts are triggered simultaneously and the 5380
244 * int has higher priority. When this irq is handled, that DMA
245 * interrupt is cleared. So a warning message is printed here.
246 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200247 printk("SCSI DMA intr ?? -- this shouldn't happen!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249}
250#endif
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Finn Thaincd461402016-01-03 16:06:06 +1100253static irqreturn_t scsi_tt_intr(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Finn Thaincd461402016-01-03 16:06:06 +1100255 struct Scsi_Host *instance = dev;
256 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int dma_stat;
258
259 dma_stat = tt_scsi_dma.dma_ctrl;
260
Finn Thaincd461402016-01-03 16:06:06 +1100261 dsprintk(NDEBUG_INTR, instance, "NCR5380 interrupt, DMA status = %02x\n",
262 dma_stat & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Look if it was the DMA that has interrupted: First possibility
265 * is that a bus error occurred...
266 */
267 if (dma_stat & 0x80) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200268 if (!scsi_dma_is_ignored_buserr(dma_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
270 SCSI_DMA_READ_P(dma_addr));
271 printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
272 }
273 }
274
275 /* If the DMA is active but not finished, we have the case
276 * that some other 5380 interrupt occurred within the DMA transfer.
277 * This means we have residual bytes, if the desired end address
278 * is not yet reached. Maybe we have to fetch some bytes from the
279 * rest data register, too. The residual must be calculated from
280 * the address pointer, not the counter register, because only the
281 * addr reg counts bytes not yet written and pending in the rest
282 * data reg!
283 */
284 if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
Finn Thaincd461402016-01-03 16:06:06 +1100285 atari_dma_residual = hostdata->dma_len -
286 (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Finn Thaind65e6342014-03-18 11:42:20 +1100288 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 atari_dma_residual);
290
291 if ((signed int)atari_dma_residual < 0)
292 atari_dma_residual = 0;
293 if ((dma_stat & 1) == 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200294 /*
295 * After read operations, we maybe have to
296 * transport some rest bytes
297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 atari_scsi_fetch_restbytes();
Roman Zippelc28bda22007-05-01 22:32:36 +0200299 } else {
300 /*
301 * There seems to be a nasty bug in some SCSI-DMA/NCR
302 * combinations: If a target disconnects while a write
303 * operation is going on, the address register of the
304 * DMA may be a few bytes farer than it actually read.
305 * This is probably due to DMA prefetching and a delay
306 * between DMA and NCR. Experiments showed that the
307 * dma_addr is 9 bytes to high, but this could vary.
308 * The problem is, that the residual is thus calculated
309 * wrong and the next transfer will start behind where
310 * it should. So we round up the residual to the next
311 * multiple of a sector size, if it isn't already a
312 * multiple and the originally expected transfer size
313 * was. The latter condition is there to ensure that
314 * the correction is taken only for "real" data
315 * transfers and not for, e.g., the parameters of some
316 * other command. These shouldn't disconnect anyway.
317 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (atari_dma_residual & 0x1ff) {
Finn Thaind65e6342014-03-18 11:42:20 +1100319 dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 "difference %ld bytes\n",
321 512 - (atari_dma_residual & 0x1ff));
322 atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
323 }
324 }
325 tt_scsi_dma.dma_ctrl = 0;
326 }
327
328 /* If the DMA is finished, fetch the rest bytes and turn it off */
329 if (dma_stat & 0x40) {
330 atari_dma_residual = 0;
331 if ((dma_stat & 1) == 0)
332 atari_scsi_fetch_restbytes();
333 tt_scsi_dma.dma_ctrl = 0;
334 }
335
Finn Thaincd461402016-01-03 16:06:06 +1100336 NCR5380_intr(irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return IRQ_HANDLED;
339}
340
341
Finn Thaincd461402016-01-03 16:06:06 +1100342static irqreturn_t scsi_falcon_intr(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Finn Thaincd461402016-01-03 16:06:06 +1100344 struct Scsi_Host *instance = dev;
345 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int dma_stat;
347
348 /* Turn off DMA and select sector counter register before
349 * accessing the status register (Atari recommendation!)
350 */
351 st_dma.dma_mode_status = 0x90;
352 dma_stat = st_dma.dma_mode_status;
353
354 /* Bit 0 indicates some error in the DMA process... don't know
355 * what happened exactly (no further docu).
356 */
357 if (!(dma_stat & 0x01)) {
358 /* DMA error */
359 printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
360 }
361
362 /* If the DMA was active, but now bit 1 is not clear, it is some
363 * other 5380 interrupt that finishes the DMA transfer. We have to
364 * calculate the number of residual bytes and give a warning if
365 * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
366 */
367 if (atari_dma_active && (dma_stat & 0x02)) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200368 unsigned long transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
371 /* The ST-DMA address is incremented in 2-byte steps, but the
372 * data are written only in 16-byte chunks. If the number of
373 * transferred bytes is not divisible by 16, the remainder is
374 * lost somewhere in outer space.
375 */
376 if (transferred & 15)
377 printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
378 "ST-DMA fifo\n", transferred & 15);
379
Finn Thaincd461402016-01-03 16:06:06 +1100380 atari_dma_residual = hostdata->dma_len - transferred;
Finn Thaind65e6342014-03-18 11:42:20 +1100381 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 atari_dma_residual);
Roman Zippelc28bda22007-05-01 22:32:36 +0200383 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 atari_dma_residual = 0;
385 atari_dma_active = 0;
386
387 if (atari_dma_orig_addr) {
388 /* If the dribble buffer was used on a read operation, copy the DMA-ed
389 * data to the original destination address.
390 */
391 memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
Finn Thaincd461402016-01-03 16:06:06 +1100392 hostdata->dma_len - atari_dma_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 atari_dma_orig_addr = NULL;
394 }
395
Finn Thaincd461402016-01-03 16:06:06 +1100396 NCR5380_intr(irq, dev);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return IRQ_HANDLED;
399}
400
401
Roman Zippelc28bda22007-05-01 22:32:36 +0200402static void atari_scsi_fetch_restbytes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 int nr;
405 char *src, *dst;
406 unsigned long phys_dst;
407
408 /* fetch rest bytes in the DMA register */
409 phys_dst = SCSI_DMA_READ_P(dma_addr);
410 nr = phys_dst & 3;
411 if (nr) {
412 /* there are 'nr' bytes left for the last long address
413 before the DMA pointer */
414 phys_dst ^= nr;
Finn Thaind65e6342014-03-18 11:42:20 +1100415 dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 nr, phys_dst);
417 /* The content of the DMA pointer is a physical address! */
418 dst = phys_to_virt(phys_dst);
Finn Thaind65e6342014-03-18 11:42:20 +1100419 dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
421 *dst++ = *src++;
422 }
423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/* This function releases the lock on the DMA chip if there is no
Finn Thain16b29e72014-11-12 16:12:08 +1100427 * connected command and the disconnected queue is empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
429
Finn Thaine3c3da62014-11-12 16:12:15 +1100430static void falcon_release_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Roman Zippelc28bda22007-05-01 22:32:36 +0200432 if (IS_A_TT())
433 return;
434
Finn Thaine3c3da62014-11-12 16:12:15 +1100435 if (stdma_is_locked_by(scsi_falcon_intr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 stdma_release();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439/* This function manages the locking of the ST-DMA.
440 * If the DMA isn't locked already for SCSI, it tries to lock it by
441 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
442 * there are other drivers waiting for the chip, we do not issue the
Finn Thain16b29e72014-11-12 16:12:08 +1100443 * command immediately but tell the SCSI mid-layer to defer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
445
Finn Thaina53a21e2014-11-12 16:12:21 +1100446static int falcon_get_lock(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Roman Zippelc28bda22007-05-01 22:32:36 +0200448 if (IS_A_TT())
Finn Thain16b29e72014-11-12 16:12:08 +1100449 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Finn Thain16b29e72014-11-12 16:12:08 +1100451 if (in_interrupt())
Finn Thaina53a21e2014-11-12 16:12:21 +1100452 return stdma_try_lock(scsi_falcon_intr, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Finn Thaina53a21e2014-11-12 16:12:21 +1100454 stdma_lock(scsi_falcon_intr, instance);
Finn Thain16b29e72014-11-12 16:12:08 +1100455 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100458#ifndef MODULE
459static int __init atari_scsi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 /* Format of atascsi parameter is:
462 * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
Finn Thain3ff228a2014-11-12 16:12:09 +1100463 * Defaults depend on TT or Falcon, determined at run time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * Negative values mean don't change.
465 */
Finn Thain9c3f0e22016-01-03 16:05:11 +1100466 int ints[8];
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100467
468 get_options(str, ARRAY_SIZE(ints), ints);
Roman Zippelc28bda22007-05-01 22:32:36 +0200469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (ints[0] < 1) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200471 printk("atari_scsi_setup: no arguments!\n");
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Finn Thain3ff228a2014-11-12 16:12:09 +1100474 if (ints[0] >= 1)
475 setup_can_queue = ints[1];
476 if (ints[0] >= 2)
477 setup_cmd_per_lun = ints[2];
478 if (ints[0] >= 3)
479 setup_sg_tablesize = ints[3];
480 if (ints[0] >= 4)
481 setup_hostid = ints[4];
Finn Thain3ff228a2014-11-12 16:12:09 +1100482 if (ints[0] >= 5)
483 setup_use_tagged_queuing = ints[5];
Finn Thain9c3f0e22016-01-03 16:05:11 +1100484 /* ints[6] (use_pdma) is ignored */
485 if (ints[0] >= 7)
486 setup_toshiba_delay = ints[7];
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100487
488 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100491__setup("atascsi=", atari_scsi_setup);
492#endif /* !MODULE */
493
Roman Zippelc28bda22007-05-01 22:32:36 +0200494
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200495static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance,
496 void *data, unsigned long count,
497 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Roman Zippelc28bda22007-05-01 22:32:36 +0200499 unsigned long addr = virt_to_phys(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Finn Thaind65e6342014-03-18 11:42:20 +1100501 dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 "dir = %d\n", instance->host_no, data, addr, count, dir);
503
504 if (!IS_A_TT() && !STRAM_ADDR(addr)) {
505 /* If we have a non-DMAable address on a Falcon, use the dribble
506 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
507 * handler to copy data from the dribble buffer to the originally
508 * wanted address.
509 */
510 if (dir)
Roman Zippelc28bda22007-05-01 22:32:36 +0200511 memcpy(atari_dma_buffer, data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 else
513 atari_dma_orig_addr = data;
514 addr = atari_dma_phys_buffer;
515 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 atari_dma_startaddr = addr; /* Needed for calculating residual later. */
Roman Zippelc28bda22007-05-01 22:32:36 +0200518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* Cache cleanup stuff: On writes, push any dirty cache out before sending
520 * it to the peripheral. (Must be done before DMA setup, since at least
521 * the ST-DMA begins to fill internal buffers right after setup. For
522 * reads, invalidate any cache, may be altered after DMA without CPU
523 * knowledge.
Roman Zippelc28bda22007-05-01 22:32:36 +0200524 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * ++roman: For the Medusa, there's no need at all for that cache stuff,
526 * because the hardware does bus snooping (fine!).
527 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200528 dma_cache_maintenance(addr, count, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (IS_A_TT()) {
531 tt_scsi_dma.dma_ctrl = dir;
Roman Zippelc28bda22007-05-01 22:32:36 +0200532 SCSI_DMA_WRITE_P(dma_addr, addr);
533 SCSI_DMA_WRITE_P(dma_cnt, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 tt_scsi_dma.dma_ctrl = dir | 2;
Roman Zippelc28bda22007-05-01 22:32:36 +0200535 } else { /* ! IS_A_TT */
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* set address */
Roman Zippelc28bda22007-05-01 22:32:36 +0200538 SCSI_DMA_SETADR(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* toggle direction bit to clear FIFO and set DMA direction */
541 dir <<= 8;
542 st_dma.dma_mode_status = 0x90 | dir;
543 st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
544 st_dma.dma_mode_status = 0x90 | dir;
545 udelay(40);
546 /* On writes, round up the transfer length to the next multiple of 512
547 * (see also comment at atari_dma_xfer_len()). */
548 st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
549 udelay(40);
550 st_dma.dma_mode_status = 0x10 | dir;
551 udelay(40);
552 /* need not restore value of dir, only boolean value is tested */
553 atari_dma_active = 1;
554 }
555
Roman Zippelc28bda22007-05-01 22:32:36 +0200556 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559
Roman Zippelc28bda22007-05-01 22:32:36 +0200560static long atari_scsi_dma_residual(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Roman Zippelc28bda22007-05-01 22:32:36 +0200562 return atari_dma_residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565
566#define CMD_SURELY_BLOCK_MODE 0
567#define CMD_SURELY_BYTE_MODE 1
568#define CMD_MODE_UNKNOWN 2
569
Finn Thain710ddd02014-11-12 16:12:02 +1100570static int falcon_classify_cmd(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 unsigned char opcode = cmd->cmnd[0];
Roman Zippelc28bda22007-05-01 22:32:36 +0200573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
Roman Zippelc28bda22007-05-01 22:32:36 +0200575 opcode == READ_BUFFER)
576 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 else if (opcode == READ_6 || opcode == READ_10 ||
578 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
579 opcode == RECOVER_BUFFERED_DATA) {
580 /* In case of a sequential-access target (tape), special care is
581 * needed here: The transfer is block-mode only if the 'fixed' bit is
582 * set! */
583 if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
Roman Zippelc28bda22007-05-01 22:32:36 +0200584 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 else
Roman Zippelc28bda22007-05-01 22:32:36 +0200586 return CMD_SURELY_BLOCK_MODE;
587 } else
588 return CMD_MODE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591
592/* This function calculates the number of bytes that can be transferred via
593 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
594 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
595 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
596 * possible on the Falcon, since that would require to program the DMA for
597 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
598 * the overrun problem, so this question is academic :-)
599 */
600
Roman Zippelc28bda22007-05-01 22:32:36 +0200601static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
Finn Thain710ddd02014-11-12 16:12:02 +1100602 struct scsi_cmnd *cmd, int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 unsigned long possible_len, limit;
Adrian Bunk29c8a242008-10-13 21:58:59 +0200605
Finn Thaine63449c2016-03-23 21:10:13 +1100606 if (wanted_len < DMA_MIN_SIZE)
607 return 0;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (IS_A_TT())
610 /* TT SCSI DMA can transfer arbitrary #bytes */
Roman Zippelc28bda22007-05-01 22:32:36 +0200611 return wanted_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
614 * 255*512 bytes, but this should be enough)
615 *
616 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
617 * that return a number of bytes which cannot be known beforehand. In this
618 * case, the given transfer length is an "allocation length". Now it
619 * can happen that this allocation length is a multiple of 512 bytes and
620 * the DMA is used. But if not n*512 bytes really arrive, some input data
621 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
622 * between commands that do block transfers and those that do byte
623 * transfers. But this isn't easy... there are lots of vendor specific
624 * commands, and the user can issue any command via the
625 * SCSI_IOCTL_SEND_COMMAND.
626 *
627 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
628 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
629 * and 3), the thing to do is obvious: allow any number of blocks via DMA
630 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
631 * the transfer (allocation) length is < 1024, hoping that no cmd. not
632 * explicitly known as byte mode have such big allocation lengths...
633 * BTW, all the discussion above applies only to reads. DMA writes are
634 * unproblematic anyways, since the targets aborts the transfer after
635 * receiving a sufficient number of bytes.
636 *
637 * Another point: If the transfer is from/to an non-ST-RAM address, we
638 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
639 */
640
641 if (write_flag) {
642 /* Write operation can always use the DMA, but the transfer size must
643 * be rounded up to the next multiple of 512 (atari_dma_setup() does
644 * this).
645 */
646 possible_len = wanted_len;
Roman Zippelc28bda22007-05-01 22:32:36 +0200647 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* Read operations: if the wanted transfer length is not a multiple of
649 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
650 * (no interrupt on DMA finished!)
651 */
652 if (wanted_len & 0x1ff)
653 possible_len = 0;
654 else {
655 /* Now classify the command (see above) and decide whether it is
656 * allowed to do DMA at all */
Roman Zippelc28bda22007-05-01 22:32:36 +0200657 switch (falcon_classify_cmd(cmd)) {
658 case CMD_SURELY_BLOCK_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 possible_len = wanted_len;
660 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200661 case CMD_SURELY_BYTE_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 possible_len = 0; /* DMA prohibited */
663 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200664 case CMD_MODE_UNKNOWN:
665 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* For unknown commands assume block transfers if the transfer
667 * size/allocation length is >= 1024 */
668 possible_len = (wanted_len < 1024) ? 0 : wanted_len;
669 break;
670 }
671 }
672 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* Last step: apply the hard limit on DMA transfers */
Roman Zippelc28bda22007-05-01 22:32:36 +0200675 limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 STRAM_BUFFER_SIZE : 255*512;
677 if (possible_len > limit)
678 possible_len = limit;
679
680 if (possible_len != wanted_len)
Finn Thaind65e6342014-03-18 11:42:20 +1100681 dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 "instead of %ld\n", possible_len, wanted_len);
683
Roman Zippelc28bda22007-05-01 22:32:36 +0200684 return possible_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/* NCR5380 register access functions
689 *
690 * There are separate functions for TT and Falcon, because the access
691 * methods are quite different. The calling macros NCR5380_read and
692 * NCR5380_write call these functions via function pointers.
693 */
694
Roman Zippelc28bda22007-05-01 22:32:36 +0200695static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Roman Zippelc28bda22007-05-01 22:32:36 +0200697 return tt_scsi_regp[reg * 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Roman Zippelc28bda22007-05-01 22:32:36 +0200700static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 tt_scsi_regp[reg * 2] = value;
703}
704
Roman Zippelc28bda22007-05-01 22:32:36 +0200705static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 dma_wd.dma_mode_status= (u_short)(0x88 + reg);
Roman Zippelc28bda22007-05-01 22:32:36 +0200708 return (u_char)dma_wd.fdc_acces_seccount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Roman Zippelc28bda22007-05-01 22:32:36 +0200711static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 dma_wd.dma_mode_status = (u_short)(0x88 + reg);
714 dma_wd.fdc_acces_seccount = (u_short)value;
715}
716
717
Finn Thain52d3e562016-03-23 21:10:20 +1100718#include "NCR5380.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Finn Thain4d3d2a52014-11-12 16:11:52 +1100720static int atari_scsi_bus_reset(struct scsi_cmnd *cmd)
721{
722 int rv;
Finn Thaine3c3da62014-11-12 16:12:15 +1100723 unsigned long flags;
Finn Thain4d3d2a52014-11-12 16:11:52 +1100724
Finn Thaine3c3da62014-11-12 16:12:15 +1100725 local_irq_save(flags);
726
Finn Thaine3c3da62014-11-12 16:12:15 +1100727 /* Abort a maybe active DMA transfer */
Finn Thain4d3d2a52014-11-12 16:11:52 +1100728 if (IS_A_TT()) {
Finn Thain4d3d2a52014-11-12 16:11:52 +1100729 tt_scsi_dma.dma_ctrl = 0;
Finn Thain4d3d2a52014-11-12 16:11:52 +1100730 } else {
Finn Thain4d3d2a52014-11-12 16:11:52 +1100731 st_dma.dma_mode_status = 0x90;
732 atari_dma_active = 0;
733 atari_dma_orig_addr = NULL;
Finn Thain4d3d2a52014-11-12 16:11:52 +1100734 }
735
736 rv = NCR5380_bus_reset(cmd);
737
Finn Thaine3c3da62014-11-12 16:12:15 +1100738 /* The 5380 raises its IRQ line while _RST is active but the ST DMA
739 * "lock" has been released so this interrupt may end up handled by
740 * floppy or IDE driver (if one of them holds the lock). The NCR5380
741 * interrupt flag has been cleared already.
742 */
Finn Thain4d3d2a52014-11-12 16:11:52 +1100743
Finn Thaine3c3da62014-11-12 16:12:15 +1100744 local_irq_restore(flags);
Finn Thain4d3d2a52014-11-12 16:11:52 +1100745
746 return rv;
747}
748
Finn Thain3ff228a2014-11-12 16:12:09 +1100749#define DRV_MODULE_NAME "atari_scsi"
750#define PFX DRV_MODULE_NAME ": "
751
752static struct scsi_host_template atari_scsi_template = {
753 .module = THIS_MODULE,
754 .proc_name = DRV_MODULE_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 .name = "Atari native SCSI",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .info = atari_scsi_info,
757 .queuecommand = atari_scsi_queue_command,
758 .eh_abort_handler = atari_scsi_abort,
759 .eh_bus_reset_handler = atari_scsi_bus_reset,
Finn Thain3ff228a2014-11-12 16:12:09 +1100760 .this_id = 7,
Finn Thainaa2e2cb2016-01-03 16:05:48 +1100761 .use_clustering = DISABLE_CLUSTERING,
Finn Thain32b26a12016-01-03 16:05:58 +1100762 .cmd_size = NCR5380_CMD_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763};
764
Finn Thain3ff228a2014-11-12 16:12:09 +1100765static int __init atari_scsi_probe(struct platform_device *pdev)
766{
767 struct Scsi_Host *instance;
768 int error;
769 struct resource *irq;
Finn Thainef1081c2014-11-12 16:12:14 +1100770 int host_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Finn Thain3ff228a2014-11-12 16:12:09 +1100772 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
773 if (!irq)
774 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Finn Thain3ff228a2014-11-12 16:12:09 +1100776 if (ATARIHW_PRESENT(TT_SCSI)) {
777 atari_scsi_reg_read = atari_scsi_tt_reg_read;
778 atari_scsi_reg_write = atari_scsi_tt_reg_write;
779 } else {
780 atari_scsi_reg_read = atari_scsi_falcon_reg_read;
781 atari_scsi_reg_write = atari_scsi_falcon_reg_write;
782 }
783
784 /* The values for CMD_PER_LUN and CAN_QUEUE are somehow arbitrary.
785 * Higher values should work, too; try it!
786 * (But cmd_per_lun costs memory!)
787 *
788 * But there seems to be a bug somewhere that requires CAN_QUEUE to be
789 * 2*CMD_PER_LUN. At least on a TT, no spurious timeouts seen since
790 * changed CMD_PER_LUN...
791 *
792 * Note: The Falcon currently uses 8/1 setting due to unsolved problems
793 * with cmd_per_lun != 1
794 */
795 if (ATARIHW_PRESENT(TT_SCSI)) {
796 atari_scsi_template.can_queue = 16;
797 atari_scsi_template.cmd_per_lun = 8;
798 atari_scsi_template.sg_tablesize = SG_ALL;
799 } else {
800 atari_scsi_template.can_queue = 8;
801 atari_scsi_template.cmd_per_lun = 1;
802 atari_scsi_template.sg_tablesize = SG_NONE;
803 }
804
805 if (setup_can_queue > 0)
806 atari_scsi_template.can_queue = setup_can_queue;
807
808 if (setup_cmd_per_lun > 0)
809 atari_scsi_template.cmd_per_lun = setup_cmd_per_lun;
810
811 /* Leave sg_tablesize at 0 on a Falcon! */
812 if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize >= 0)
813 atari_scsi_template.sg_tablesize = setup_sg_tablesize;
814
815 if (setup_hostid >= 0) {
816 atari_scsi_template.this_id = setup_hostid & 7;
817 } else {
818 /* Test if a host id is set in the NVRam */
819 if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
Finn Thain6225a162016-01-03 16:05:02 +1100820 unsigned char b = nvram_read_byte(16);
Finn Thain3ff228a2014-11-12 16:12:09 +1100821
822 /* Arbitration enabled? (for TOS)
823 * If yes, use configured host ID
824 */
825 if (b & 0x80)
826 atari_scsi_template.this_id = b & 7;
827 }
828 }
829
Finn Thain3ff228a2014-11-12 16:12:09 +1100830 /* If running on a Falcon and if there's TT-Ram (i.e., more than one
831 * memory block, since there's always ST-Ram in a Falcon), then
832 * allocate a STRAM_BUFFER_SIZE byte dribble buffer for transfers
833 * from/to alternative Ram.
834 */
835 if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) &&
836 m68k_num_memory > 1) {
837 atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
838 if (!atari_dma_buffer) {
839 pr_err(PFX "can't allocate ST-RAM double buffer\n");
840 return -ENOMEM;
841 }
842 atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
843 atari_dma_orig_addr = 0;
844 }
Finn Thain3ff228a2014-11-12 16:12:09 +1100845
846 instance = scsi_host_alloc(&atari_scsi_template,
847 sizeof(struct NCR5380_hostdata));
848 if (!instance) {
849 error = -ENOMEM;
850 goto fail_alloc;
851 }
Finn Thain3ff228a2014-11-12 16:12:09 +1100852
Finn Thain3ff228a2014-11-12 16:12:09 +1100853 instance->irq = irq->start;
854
Finn Thainef1081c2014-11-12 16:12:14 +1100855 host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP;
Finn Thainca513fc2014-11-12 16:12:19 +1100856#ifdef SUPPORT_TAGS
857 host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
858#endif
Finn Thain9c3f0e22016-01-03 16:05:11 +1100859 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
Finn Thainca513fc2014-11-12 16:12:19 +1100860
Finn Thain0ad0eff2016-01-03 16:05:21 +1100861 error = NCR5380_init(instance, host_flags);
862 if (error)
863 goto fail_init;
Finn Thain3ff228a2014-11-12 16:12:09 +1100864
865 if (IS_A_TT()) {
866 error = request_irq(instance->irq, scsi_tt_intr, 0,
867 "NCR5380", instance);
868 if (error) {
869 pr_err(PFX "request irq %d failed, aborting\n",
870 instance->irq);
871 goto fail_irq;
872 }
873 tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
Finn Thaine4dec682016-03-23 21:10:12 +1100874
Finn Thain3ff228a2014-11-12 16:12:09 +1100875 tt_scsi_dma.dma_ctrl = 0;
876 atari_dma_residual = 0;
877
878 /* While the read overruns (described by Drew Eckhardt in
879 * NCR5380.c) never happened on TTs, they do in fact on the
880 * Medusa (This was the cause why SCSI didn't work right for
881 * so long there.) Since handling the overruns slows down
882 * a bit, I turned the #ifdef's into a runtime condition.
883 *
884 * In principle it should be sufficient to do max. 1 byte with
885 * PIO, but there is another problem on the Medusa with the DMA
Finn Thainef1081c2014-11-12 16:12:14 +1100886 * rest data register. So read_overruns is currently set
Finn Thain3ff228a2014-11-12 16:12:09 +1100887 * to 4 to avoid having transfers that aren't a multiple of 4.
888 * If the rest data bug is fixed, this can be lowered to 1.
889 */
Finn Thainef1081c2014-11-12 16:12:14 +1100890 if (MACH_IS_MEDUSA) {
891 struct NCR5380_hostdata *hostdata =
892 shost_priv(instance);
893
894 hostdata->read_overruns = 4;
895 }
Finn Thain3ff228a2014-11-12 16:12:09 +1100896 } else {
897 /* Nothing to do for the interrupt: the ST-DMA is initialized
898 * already.
899 */
Finn Thain3ff228a2014-11-12 16:12:09 +1100900 atari_dma_residual = 0;
901 atari_dma_active = 0;
902 atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
903 : 0xff000000);
Finn Thain3ff228a2014-11-12 16:12:09 +1100904 }
905
Finn Thain9c3f0e22016-01-03 16:05:11 +1100906 NCR5380_maybe_reset_bus(instance);
907
Finn Thain3ff228a2014-11-12 16:12:09 +1100908 error = scsi_add_host(instance, NULL);
909 if (error)
910 goto fail_host;
911
912 platform_set_drvdata(pdev, instance);
913
914 scsi_scan_host(instance);
915 return 0;
916
917fail_host:
918 if (IS_A_TT())
919 free_irq(instance->irq, instance);
920fail_irq:
921 NCR5380_exit(instance);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100922fail_init:
Finn Thain3ff228a2014-11-12 16:12:09 +1100923 scsi_host_put(instance);
924fail_alloc:
925 if (atari_dma_buffer)
926 atari_stram_free(atari_dma_buffer);
927 return error;
928}
929
930static int __exit atari_scsi_remove(struct platform_device *pdev)
931{
932 struct Scsi_Host *instance = platform_get_drvdata(pdev);
933
934 scsi_remove_host(instance);
935 if (IS_A_TT())
936 free_irq(instance->irq, instance);
937 NCR5380_exit(instance);
938 scsi_host_put(instance);
939 if (atari_dma_buffer)
940 atari_stram_free(atari_dma_buffer);
941 return 0;
942}
943
944static struct platform_driver atari_scsi_driver = {
945 .remove = __exit_p(atari_scsi_remove),
946 .driver = {
947 .name = DRV_MODULE_NAME,
Finn Thain3ff228a2014-11-12 16:12:09 +1100948 },
949};
950
951module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe);
952
953MODULE_ALIAS("platform:" DRV_MODULE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954MODULE_LICENSE("GPL");