OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [scsi/] [atari_scsi.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
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
 
67
 
68
#include <linux/config.h>
69
#include <linux/module.h>
70
 
71
#define NDEBUG (0)
72
 
73
#define NDEBUG_ABORT    0x800000
74
#define NDEBUG_TAGS     0x1000000
75
#define NDEBUG_MERGING  0x2000000
76
 
77
#define AUTOSENSE
78
/* For the Atari version, use only polled IO or REAL_DMA */
79
#define REAL_DMA
80
/* Support tagged queuing? (on devices that are able to... :-) */
81
#define SUPPORT_TAGS
82
#define MAX_TAGS 32
83
 
84
#include <linux/types.h>
85
#include <linux/stddef.h>
86
#include <linux/ctype.h>
87
#include <linux/delay.h>
88
#include <linux/mm.h>
89
#include <linux/blk.h>
90
#include <linux/interrupt.h>
91
 
92
#include <asm/bootinfo.h>
93
#include <asm/atarihw.h>
94
#include <asm/atariints.h>
95
#include <asm/page.h>
96
#include <asm/pgtable.h>
97
#include <asm/irq.h>
98
#include <asm/traps.h>
99
#include <asm/bitops.h>
100
 
101
#include "scsi.h"
102
#include "hosts.h"
103
#include "atari_scsi.h"
104
#include "NCR5380.h"
105
#include "constants.h"
106
#include <asm/atari_stdma.h>
107
#include <asm/io.h>
108
 
109
#include <linux/stat.h>
110
 
111
struct proc_dir_entry proc_scsi_atari = {
112
    PROC_SCSI_ATARI, 5, "Atari",
113
    S_IFDIR | S_IRUGO | S_IXUGO, 2
114
};
115
 
116
#define IS_A_TT()       ATARIHW_PRESENT(TT_SCSI)
117
 
118
#define SCSI_DMA_WRITE_P(elt,val)                               \
119
        do {                                                    \
120
                unsigned long v = val;                          \
121
                tt_scsi_dma.elt##_lo = v & 0xff;                \
122
                v >>= 8;                                        \
123
                tt_scsi_dma.elt##_lmd = v & 0xff;               \
124
                v >>= 8;                                        \
125
                tt_scsi_dma.elt##_hmd = v & 0xff;               \
126
                v >>= 8;                                        \
127
                tt_scsi_dma.elt##_hi = v & 0xff;                \
128
        } while(0)
129
 
130
#define SCSI_DMA_READ_P(elt)                                    \
131
        (((unsigned long)tt_scsi_dma.elt##_hi  << 24) |         \
132
         ((unsigned long)tt_scsi_dma.elt##_hmd << 16) |         \
133
         ((unsigned long)tt_scsi_dma.elt##_lmd << 8) |          \
134
          (unsigned long)tt_scsi_dma.elt##_lo)
135
 
136
 
137
#define SCSI_DMA_SETADR(adr)                            \
138
    do {                                                \
139
        unsigned long __adr = (adr);                    \
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
    } while(0)
149
 
150
#define SCSI_DMA_GETADR() ({                            \
151
    unsigned long       __adr;                          \
152
    __adr = st_dma.dma_lo;                              \
153
    MFPDELAY();                                         \
154
    __adr |= (st_dma.dma_md & 0xff) << 8;               \
155
    MFPDELAY();                                         \
156
    __adr |= (st_dma.dma_hi & 0xff) << 16;              \
157
    MFPDELAY();                                         \
158
    __adr;                                              \
159
})
160
 
161
#define ENABLE_IRQ()                                    \
162
        do {                                            \
163
                if (IS_A_TT())                          \
164
                        atari_enable_irq( IRQ_TT_MFP_SCSI );    \
165
                else                                    \
166
                        atari_enable_irq( IRQ_MFP_FSCSI );      \
167
        } while(0)
168
 
169
#define DISABLE_IRQ()                                   \
170
        do {                                            \
171
                if (IS_A_TT())                          \
172
                        atari_disable_irq( IRQ_TT_MFP_SCSI );   \
173
                else                                    \
174
                        atari_disable_irq( IRQ_MFP_FSCSI );     \
175
        } while(0)
176
 
177
 
178
#define HOSTDATA_DMALEN         (((struct NCR5380_hostdata *) \
179
                                (atari_scsi_host->hostdata))->dma_len)
180
 
181
/* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
182
 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
183
 * need ten times the standard value... */
184
#ifndef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
185
#define AFTER_RESET_DELAY       (HZ/2)
186
#else
187
#define AFTER_RESET_DELAY       (5*HZ/2)
188
#endif
189
 
190
/***************************** Prototypes *****************************/
191
 
192
#ifdef REAL_DMA
193
static int scsi_dma_is_ignored_buserr( unsigned char dma_stat );
194
static void atari_scsi_fetch_restbytes( void );
195
static long atari_scsi_dma_residual( struct Scsi_Host *instance );
196
static int falcon_classify_cmd( Scsi_Cmnd *cmd );
197
static unsigned long atari_dma_xfer_len( unsigned long wanted_len,
198
                                         Scsi_Cmnd *cmd, int write_flag );
199
#endif
200
static void scsi_tt_intr( int irq, struct pt_regs *fp, void *dummy);
201
static void scsi_falcon_intr( int irq, struct pt_regs *fp, void *dummy);
202
static void falcon_release_lock_if_possible( struct NCR5380_hostdata *
203
                                             hostdata );
204
static void falcon_get_lock( void );
205
static void atari_scsi_reset_boot( void );
206
static unsigned char atari_scsi_tt_reg_read( unsigned char reg );
207
static void atari_scsi_tt_reg_write( unsigned char reg, unsigned char value);
208
static unsigned char atari_scsi_falcon_reg_read( unsigned char reg );
209
static void atari_scsi_falcon_reg_write( unsigned char reg, unsigned char value );
210
 
211
/************************* End of Prototypes **************************/
212
 
213
 
214
static struct Scsi_Host *atari_scsi_host = NULL;
215
static unsigned char (*atari_scsi_reg_read)( unsigned char reg );
216
static void (*atari_scsi_reg_write)( unsigned char reg, unsigned char value );
217
 
218
#ifdef REAL_DMA
219
static unsigned long    atari_dma_residual, atari_dma_startaddr;
220
static short            atari_dma_active;
221
/* pointer to the dribble buffer */
222
static char             *atari_dma_buffer = NULL;
223
/* precalculated physical address of the dribble buffer */
224
static unsigned long    atari_dma_phys_buffer;
225
/* != 0 tells the Falcon int handler to copy data from the dribble buffer */
226
static char             *atari_dma_orig_addr;
227
/* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
228
 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
229
 * cases where requests to physical contiguous buffers have been merged, this
230
 * request is <= 4k (one page). So I don't think we have to split transfers
231
 * just due to this buffer size...
232
 */
233
#define STRAM_BUFFER_SIZE       (4096)
234
/* mask for address bits that can't be used with the ST-DMA */
235
static unsigned long    atari_dma_stram_mask;
236
#define STRAM_ADDR(a)   (((a) & atari_dma_stram_mask) == 0)
237
/* number of bytes to cut from a transfer to handle NCR overruns */
238
static int atari_read_overruns = 0;
239
#endif
240
 
241
int setup_can_queue = -1;
242
int setup_cmd_per_lun = -1;
243
int setup_sg_tablesize = -1;
244
#ifdef SUPPORT_TAGS
245
int setup_use_tagged_queuing = -1;
246
#endif
247
int setup_hostid = -1;
248
 
249
 
250
#if defined(REAL_DMA)
251
 
252
static int scsi_dma_is_ignored_buserr( unsigned char dma_stat )
253
{
254
        int i;
255
        unsigned long   addr = SCSI_DMA_READ_P( dma_addr ), end_addr;
256
 
257
        if (dma_stat & 0x01) {
258
 
259
                /* A bus error happens when DMA-ing from the last page of a
260
                 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
261
                 * Check for this case:
262
                 */
263
 
264
                for( i = 0; i < boot_info.num_memory; ++i ) {
265
                        end_addr = boot_info.memory[i].addr +
266
                                boot_info.memory[i].size;
267
                        if (end_addr <= addr && addr <= end_addr + 4)
268
                                return( 1 );
269
                }
270
        }
271
        return( 0 );
272
}
273
 
274
 
275
#if 0
276
/* Dead code... wasn't called anyway :-) and causes some trouble, because at
277
 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
278
 * to clear the DMA int pending bit before it allows other level 6 interrupts.
279
 */
280
static void scsi_dma_buserr (int irq, struct pt_regs *fp, void *dummy)
281
{
282
        unsigned char   dma_stat = tt_scsi_dma.dma_ctrl;
283
 
284
        /* Don't do anything if a NCR interrupt is pending. Probably it's just
285
         * masked... */
286
        if (atari_irq_pending( IRQ_TT_MFP_SCSI ))
287
                return;
288
 
289
        printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
290
               SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
291
        if (dma_stat & 0x80) {
292
                if (!scsi_dma_is_ignored_buserr( dma_stat ))
293
                        printk( "SCSI DMA bus error -- bad DMA programming!\n" );
294
        }
295
        else {
296
                /* Under normal circumstances we never should get to this point,
297
                 * since both interrupts are triggered simultaneously and the 5380
298
                 * int has higher priority. When this irq is handled, that DMA
299
                 * interrupt is cleared. So a warning message is printed here.
300
                 */
301
                printk( "SCSI DMA intr ?? -- this shouldn't happen!\n" );
302
        }
303
}
304
#endif
305
 
306
#endif
307
 
308
 
309
static void scsi_tt_intr (int irq, struct pt_regs *fp, void *dummy)
310
{
311
#ifdef REAL_DMA
312
        int dma_stat;
313
 
314
        dma_stat = tt_scsi_dma.dma_ctrl;
315
 
316
        INT_PRINTK("scsi%d: NCR5380 interrupt, DMA status = %02x\n",
317
                   atari_scsi_host->host_no, dma_stat & 0xff);
318
 
319
        /* Look if it was the DMA that has interrupted: First possibility
320
         * is that a bus error occurred...
321
         */
322
        if (dma_stat & 0x80) {
323
                if (!scsi_dma_is_ignored_buserr( dma_stat )) {
324
                        printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
325
                               SCSI_DMA_READ_P(dma_addr));
326
                        printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
327
                }
328
        }
329
 
330
        /* If the DMA is active but not finished, we have the case
331
         * that some other 5380 interrupt occurred within the DMA transfer.
332
         * This means we have residual bytes, if the desired end address
333
         * is not yet reached. Maybe we have to fetch some bytes from the
334
         * rest data register, too. The residual must be calculated from
335
         * the address pointer, not the counter register, because only the
336
         * addr reg counts bytes not yet written and pending in the rest
337
         * data reg!
338
         */
339
        if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
340
                atari_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P( dma_addr ) -
341
                                                                                                atari_dma_startaddr);
342
 
343
                DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n",
344
                           atari_dma_residual);
345
 
346
                if ((signed int)atari_dma_residual < 0)
347
                        atari_dma_residual = 0;
348
                if ((dma_stat & 1) == 0) {
349
                        /* After read operations, we maybe have to
350
                           transport some rest bytes */
351
                        atari_scsi_fetch_restbytes();
352
                }
353
                else {
354
                        /* There seems to be a nasty bug in some SCSI-DMA/NCR
355
                           combinations: If a target disconnects while a write
356
                           operation is going on, the address register of the
357
                           DMA may be a few bytes farer than it actually read.
358
                           This is probably due to DMA prefetching and a delay
359
                           between DMA and NCR.  Experiments showed that the
360
                           dma_addr is 9 bytes to high, but this could vary.
361
                           The problem is, that the residual is thus calculated
362
                           wrong and the next transfer will start behind where
363
                           it should.  So we round up the residual to the next
364
                           multiple of a sector size, if it isn't already a
365
                           multiple and the originally expected transfer size
366
                           was.  The latter condition is there to ensure that
367
                           the correction is taken only for "real" data
368
                           transfers and not for, e.g., the parameters of some
369
                           other command.  These shouldn't disconnect anyway.
370
                           */
371
                        if (atari_dma_residual & 0x1ff) {
372
                                DMA_PRINTK("SCSI DMA: DMA bug corrected, "
373
                                           "difference %ld bytes\n",
374
                                           512 - (atari_dma_residual & 0x1ff));
375
                                atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
376
                        }
377
                }
378
                tt_scsi_dma.dma_ctrl = 0;
379
        }
380
 
381
        /* If the DMA is finished, fetch the rest bytes and turn it off */
382
        if (dma_stat & 0x40) {
383
                atari_dma_residual = 0;
384
                if ((dma_stat & 1) == 0)
385
                        atari_scsi_fetch_restbytes();
386
                tt_scsi_dma.dma_ctrl = 0;
387
        }
388
 
389
#endif /* REAL_DMA */
390
 
391
        NCR5380_intr (0, 0, 0);
392
 
393
#if 0
394
        /* To be sure the int is not masked */
395
        atari_enable_irq( IRQ_TT_MFP_SCSI );
396
#endif
397
}
398
 
399
 
400
static void scsi_falcon_intr (int irq, struct pt_regs *fp, void *dummy)
401
{
402
#ifdef REAL_DMA
403
        int dma_stat;
404
 
405
        /* Turn off DMA and select sector counter register before
406
         * accessing the status register (Atari recommendation!)
407
         */
408
        st_dma.dma_mode_status = 0x90;
409
        dma_stat = st_dma.dma_mode_status;
410
 
411
        /* Bit 0 indicates some error in the DMA process... don't know
412
         * what happened exactly (no further docu).
413
         */
414
        if (!(dma_stat & 0x01)) {
415
                /* DMA error */
416
                printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
417
        }
418
 
419
        /* If the DMA was active, but now bit 1 is not clear, it is some
420
         * other 5380 interrupt that finishes the DMA transfer. We have to
421
         * calculate the number of residual bytes and give a warning if
422
         * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
423
         */
424
        if (atari_dma_active && (dma_stat & 0x02)) {
425
                unsigned long   transferred;
426
 
427
                transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
428
                /* The ST-DMA address is incremented in 2-byte steps, but the
429
                 * data are written only in 16-byte chunks. If the number of
430
                 * transferred bytes is not divisible by 16, the remainder is
431
                 * lost somewhere in outer space.
432
                 */
433
                if (transferred & 15)
434
                        printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
435
                               "ST-DMA fifo\n", transferred & 15);
436
 
437
                atari_dma_residual = HOSTDATA_DMALEN - transferred;
438
                DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n",
439
                           atari_dma_residual);
440
        }
441
        else
442
                atari_dma_residual = 0;
443
        atari_dma_active = 0;
444
 
445
        if (atari_dma_orig_addr) {
446
                /* If the dribble buffer was used on a read operation, copy the DMA-ed
447
                 * data to the original destination address.
448
                 */
449
                memcpy( atari_dma_orig_addr, (void *)PTOV(atari_dma_startaddr),
450
                       HOSTDATA_DMALEN - atari_dma_residual );
451
                atari_dma_orig_addr = NULL;
452
        }
453
 
454
#endif /* REAL_DMA */
455
 
456
        NCR5380_intr (0, 0, 0);
457
}
458
 
459
 
460
#ifdef REAL_DMA
461
static void atari_scsi_fetch_restbytes( void )
462
{
463
        int nr;
464
        char    *src, *dst;
465
 
466
        /* fetch rest bytes in the DMA register */
467
        dst = (char *)SCSI_DMA_READ_P( dma_addr );
468
        if ((nr = ((long)dst & 3))) {
469
                /* there are 'nr' bytes left for the last long address before the
470
                   DMA pointer */
471
                dst = (char *)( (unsigned long)dst & ~3 );
472
                DMA_PRINTK("SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
473
                           nr, (long)dst);
474
                dst = (char *)PTOV(dst);  /* The content of the DMA pointer
475
                                           * is a physical address! */
476
                DMA_PRINTK(" = virt addr 0x%08lx\n", (long)dst);
477
                for( src = (char *)&tt_scsi_dma.dma_restdata; nr > 0; --nr )
478
                        *dst++ = *src++;
479
        }
480
}
481
#endif /* REAL_DMA */
482
 
483
 
484
static int falcon_got_lock = 0;
485
static struct wait_queue        *falcon_fairness_wait = NULL;
486
static int falcon_trying_lock = 0;
487
static struct wait_queue        *falcon_try_wait = NULL;
488
static int falcon_dont_release = 0;
489
 
490
/* This function releases the lock on the DMA chip if there is no
491
 * connected command and the disconnected queue is empty. On
492
 * releasing, instances of falcon_get_lock are awoken, that put
493
 * themselves to sleep for fairness. They can now try to get the lock
494
 * again (but others waiting longer more probably will win).
495
 */
496
 
497
static void
498
falcon_release_lock_if_possible( struct NCR5380_hostdata * hostdata )
499
{
500
        unsigned long   oldflags;
501
 
502
        if (IS_A_TT()) return;
503
 
504
        save_flags(oldflags);
505
        cli();
506
 
507
        if (falcon_got_lock &&
508
                !hostdata->disconnected_queue &&
509
                !hostdata->issue_queue &&
510
                !hostdata->connected) {
511
 
512
                if (falcon_dont_release) {
513
#if 0
514
                        printk("WARNING: Lock release not allowed. Ignored\n");
515
#endif
516
                        restore_flags(oldflags);
517
                        return;
518
                }
519
                falcon_got_lock = 0;
520
                stdma_release();
521
                wake_up( &falcon_fairness_wait );
522
        }
523
 
524
        restore_flags(oldflags);
525
}
526
 
527
/* This function manages the locking of the ST-DMA.
528
 * If the DMA isn't locked already for SCSI, it tries to lock it by
529
 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
530
 * there are other drivers waiting for the chip, we do not issue the
531
 * command immediately but wait on 'falcon_fairness_queue'. We will be
532
 * waked up when the DMA is unlocked by some SCSI interrupt. After that
533
 * we try to get the lock again.
534
 * But we must be prepared that more than one instance of
535
 * falcon_get_lock() is waiting on the fairness queue. They should not
536
 * try all at once to call stdma_lock(), one is enough! For that, the
537
 * first one sets 'falcon_trying_lock', others that see that variable
538
 * set wait on the queue 'falcon_try_wait'.
539
 * Complicated, complicated.... Sigh...
540
 */
541
 
542
static void falcon_get_lock( void )
543
{
544
        unsigned long   oldflags;
545
 
546
        if (IS_A_TT()) return;
547
 
548
        save_flags(oldflags);
549
        cli();
550
 
551
        while( intr_count == 0 && falcon_got_lock && stdma_others_waiting() )
552
                sleep_on( &falcon_fairness_wait );
553
 
554
        while (!falcon_got_lock) {
555
                if (intr_count > 0)
556
                        panic( "Falcon SCSI hasn't ST-DMA lock in interrupt" );
557
                if (!falcon_trying_lock) {
558
                        falcon_trying_lock = 1;
559
                        stdma_lock(scsi_falcon_intr, NULL);
560
                        falcon_got_lock = 1;
561
                        falcon_trying_lock = 0;
562
                        wake_up( &falcon_try_wait );
563
                }
564
                else {
565
                        sleep_on( &falcon_try_wait );
566
                }
567
        }
568
 
569
        restore_flags(oldflags);
570
        if (!falcon_got_lock)
571
                panic("Falcon SCSI: someone stole the lock :-(\n");
572
}
573
 
574
 
575
/* This is the wrapper function for NCR5380_queue_command(). It just
576
 * tries to get the lock on the ST-DMA (see above) and then calls the
577
 * original function.
578
 */
579
 
580
#if 0
581
int atari_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
582
{
583
        /* falcon_get_lock();
584
         * ++guenther: moved to NCR5380_queue_command() to prevent
585
         * race condition, see there for an explanation.
586
         */
587
        return( NCR5380_queue_command( cmd, done ) );
588
}
589
#endif
590
 
591
 
592
#define RTC_READ(reg)                           \
593
    ({  unsigned char   __val;                  \
594
                outb(reg,&tt_rtc.regsel);       \
595
                __val = tt_rtc.data;            \
596
                __val;                          \
597
        })
598
 
599
#define RTC_WRITE(reg,val)                      \
600
    do {                                        \
601
                outb(reg,&tt_rtc.regsel);       \
602
                tt_rtc.data = (val);            \
603
        } while(0)
604
 
605
 
606
int atari_scsi_detect (Scsi_Host_Template *host)
607
{
608
        static int called = 0;
609
        struct Scsi_Host *instance;
610
 
611
        if (!MACH_IS_ATARI ||
612
            (!ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(TT_SCSI)) ||
613
            called)
614
                return( 0 );
615
 
616
        host->proc_dir = &proc_scsi_atari;
617
 
618
        atari_scsi_reg_read  = IS_A_TT() ? atari_scsi_tt_reg_read :
619
                                           atari_scsi_falcon_reg_read;
620
        atari_scsi_reg_write = IS_A_TT() ? atari_scsi_tt_reg_write :
621
                                           atari_scsi_falcon_reg_write;
622
 
623
        /* setup variables */
624
        host->can_queue =
625
                (setup_can_queue > 0) ? setup_can_queue :
626
                IS_A_TT() ? ATARI_TT_CAN_QUEUE : ATARI_FALCON_CAN_QUEUE;
627
        host->cmd_per_lun =
628
                (setup_cmd_per_lun > 0) ? setup_cmd_per_lun :
629
                IS_A_TT() ? ATARI_TT_CMD_PER_LUN : ATARI_FALCON_CMD_PER_LUN;
630
        /* Force sg_tablesize to 0 on a Falcon! */
631
        host->sg_tablesize =
632
                !IS_A_TT() ? ATARI_FALCON_SG_TABLESIZE :
633
                (setup_sg_tablesize >= 0) ? setup_sg_tablesize : ATARI_TT_SG_TABLESIZE;
634
 
635
        if (setup_hostid >= 0)
636
                host->this_id = setup_hostid;
637
        else {
638
                /* use 7 as default */
639
                host->this_id = 7;
640
                /* Test if a host id is set in the NVRam */
641
                if (ATARIHW_PRESENT(TT_CLK)) {
642
                        unsigned char sum = 0, b;
643
                        int i;
644
 
645
                        /* Make checksum */
646
                        for( i = 14; i < 62; ++i )
647
                                sum += RTC_READ(i);
648
 
649
                        if (/* NV-Ram checksum valid? */
650
                                RTC_READ(62) == sum && RTC_READ(63) == ~sum &&
651
                                /* Arbitration enabled? (for TOS) */
652
                                (b = RTC_READ( 30 )) & 0x80) {
653
                                host->this_id = b & 7;
654
                        }
655
                }
656
        }
657
 
658
#ifdef SUPPORT_TAGS
659
        if (setup_use_tagged_queuing < 0)
660
                setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
661
#endif
662
 
663
        /* If running on a Falcon and if there's TT-Ram (i.e., more than one
664
         * memory block, since there's always ST-Ram in a Falcon), then allocate a
665
         * STRAM_BUFFER_SIZE byte dribble buffer for transfers from/to alternative
666
         * Ram.
667
         */
668
        if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) &&
669
            !ATARIHW_PRESENT(EXTD_DMA) && boot_info.num_memory > 1) {
670
                atari_dma_buffer = scsi_init_malloc(STRAM_BUFFER_SIZE,
671
                                                    GFP_ATOMIC | GFP_DMA);
672
                atari_dma_phys_buffer = VTOP( atari_dma_buffer );
673
                atari_dma_orig_addr = 0;
674
        }
675
 
676
        instance = scsi_register (host, sizeof (struct NCR5380_hostdata));
677
        atari_scsi_host = instance;
678
        instance->irq = IS_A_TT() ? IRQ_TT_MFP_SCSI : IRQ_MFP_FSCSI;
679
 
680
        atari_scsi_reset_boot();
681
        NCR5380_init (instance, 0);
682
 
683
        if (IS_A_TT()) {
684
 
685
                /* This int is actually "pseudo-slow", i.e. it acts like a slow
686
                 * interrupt after having cleared the pending flag for the DMA
687
                 * interrupt. */
688
                add_isr(IRQ_TT_MFP_SCSI, scsi_tt_intr, IRQ_TYPE_SLOW,
689
                        NULL, "SCSI NCR5380");
690
                tt_mfp.active_edge |= 0x80;             /* SCSI int on L->H */
691
#ifdef REAL_DMA
692
                tt_scsi_dma.dma_ctrl = 0;
693
                atari_dma_residual = 0;
694
#endif /* REAL_DMA */
695
 
696
                if (is_medusa) {
697
                        /* While the read overruns (described by Drew Eckhardt in
698
                         * NCR5380.c) never happened on TTs, they do in fact on the Medusa
699
                         * (This was the cause why SCSI didn't work right for so long
700
                         * there.) Since handling the overruns slows down a bit, I turned
701
                         * the #ifdef's into a runtime condition.
702
                         *
703
                         * In principle it should be sufficient to do max. 1 byte with
704
                         * PIO, but there is another problem on the Medusa with the DMA
705
                         * rest data register. So 'atari_read_overruns' is currently set
706
                         * to 4 to avoid having transfers that aren't a multiple of 4. If
707
                         * the rest data bug is fixed, this can be lowered to 1.
708
                         */
709
                        atari_read_overruns = 4;
710
                }
711
 
712
        }
713
        else { /* ! IS_A_TT */
714
 
715
                /* Nothing to do for the interrupt: the ST-DMA is initialized
716
                 * already by atari_init_INTS()
717
                 */
718
 
719
#ifdef REAL_DMA
720
                atari_dma_residual = 0;
721
                atari_dma_active = 0;
722
                atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
723
                                        : 0xff000000);
724
#endif
725
        }
726
 
727
        printk(KERN_INFO "scsi%d: options CAN_QUEUE=%d CMD_PER_LUN=%d SCAT-GAT=%d "
728
#ifdef SUPPORT_TAGS
729
                        "TAGGED-QUEUING=%s "
730
#endif
731
                        "HOSTID=%d",
732
                        instance->host_no, instance->hostt->can_queue,
733
                        instance->hostt->cmd_per_lun,
734
                        instance->hostt->sg_tablesize,
735
#ifdef SUPPORT_TAGS
736
                        setup_use_tagged_queuing ? "yes" : "no",
737
#endif
738
                        instance->hostt->this_id );
739
        NCR5380_print_options (instance);
740
        printk ("\n");
741
 
742
        called = 1;
743
        return( 1 );
744
}
745
 
746
#ifdef MODULE
747
int atari_scsi_release (struct Scsi_Host *sh)
748
{
749
        if (IS_A_TT())
750
                remove_isr (IRQ_TT_MFP_SCSI, scsi_tt_intr, NULL);
751
        if (atari_dma_buffer)
752
                scsi_init_free (atari_dma_buffer, STRAM_BUFFER_SIZE);
753
        return 1;
754
}
755
#endif
756
 
757
void atari_scsi_setup( char *str, int *ints )
758
{
759
        /* Format of atascsi parameter is:
760
         *   atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
761
         * Defaults depend on TT or Falcon, hostid determined at run time.
762
         * Negative values mean don't change.
763
         */
764
 
765
        /* Grmbl... the standard parameter parsing can't handle negative numbers
766
         * :-( So let's do it ourselves!
767
         */
768
 
769
        int i = ints[0]+1, fact;
770
 
771
        while( str && (isdigit(*str) || *str == '-') && i <= 10) {
772
                if (*str == '-')
773
                        fact = -1, ++str;
774
                else
775
                        fact = 1;
776
                ints[i++] = simple_strtoul( str, NULL, 0 ) * fact;
777
                if ((str = strchr( str, ',' )) != NULL)
778
                        ++str;
779
        }
780
        ints[0] = i-1;
781
 
782
        if (ints[0] < 1) {
783
                printk( "atari_scsi_setup: no arguments!\n" );
784
                return;
785
        }
786
 
787
        if (ints[0] >= 1) {
788
                if (ints[1] > 0)
789
                        /* no limits on this, just > 0 */
790
                        setup_can_queue = ints[1];
791
        }
792
        if (ints[0] >= 2) {
793
                if (ints[2] > 0)
794
                        setup_cmd_per_lun = ints[2];
795
        }
796
        if (ints[0] >= 3) {
797
                if (ints[3] >= 0) {
798
                        setup_sg_tablesize = ints[3];
799
                        /* Must be <= SG_ALL (255) */
800
                        if (setup_sg_tablesize > SG_ALL)
801
                                setup_sg_tablesize = SG_ALL;
802
                }
803
        }
804
        if (ints[0] >= 4) {
805
                /* Must be between 0 and 7 */
806
                if (ints[4] >= 0 && ints[4] <= 7)
807
                        setup_hostid = ints[4];
808
                else if (ints[4] > 7)
809
                        printk( "atari_scsi_setup: invalid host ID %d !\n", ints[4] );
810
        }
811
#ifdef SUPPORT_TAGS
812
        if (ints[0] >= 5) {
813
                if (ints[5] >= 0)
814
                        setup_use_tagged_queuing = !!ints[5];
815
        }
816
#endif
817
}
818
 
819
int atari_scsi_reset( Scsi_Cmnd *cmd, unsigned int reset_flags)
820
{
821
        int             rv;
822
        struct NCR5380_hostdata *hostdata =
823
                (struct NCR5380_hostdata *)cmd->host->hostdata;
824
 
825
        /* For doing the reset, SCSI interrupts must be disabled first,
826
         * since the 5380 raises its IRQ line while _RST is active and we
827
         * can't disable interrupts completely, since we need the timer.
828
         */
829
        /* And abort a maybe active DMA transfer */
830
        if (IS_A_TT()) {
831
                atari_turnoff_irq( IRQ_TT_MFP_SCSI );
832
#ifdef REAL_DMA
833
                tt_scsi_dma.dma_ctrl = 0;
834
#endif /* REAL_DMA */
835
        }
836
        else {
837
                atari_turnoff_irq( IRQ_MFP_FSCSI );
838
#ifdef REAL_DMA
839
                st_dma.dma_mode_status = 0x90;
840
                atari_dma_active = 0;
841
                atari_dma_orig_addr = NULL;
842
#endif /* REAL_DMA */
843
        }
844
 
845
        rv = NCR5380_reset(cmd, reset_flags);
846
 
847
        /* Re-enable ints */
848
        if (IS_A_TT()) {
849
                atari_turnon_irq( IRQ_TT_MFP_SCSI );
850
        }
851
        else {
852
                atari_turnon_irq( IRQ_MFP_FSCSI );
853
        }
854
        falcon_release_lock_if_possible(hostdata);
855
 
856
        return( rv );
857
}
858
 
859
 
860
static void atari_scsi_reset_boot( void )
861
{
862
        unsigned long end;
863
 
864
        /*
865
         * Do a SCSI reset to clean up the bus during initialization. No messing
866
         * with the queues, interrupts, or locks necessary here.
867
         */
868
 
869
        printk( "Atari SCSI: resetting the SCSI bus..." );
870
 
871
        /* get in phase */
872
        NCR5380_write( TARGET_COMMAND_REG,
873
                      PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
874
 
875
        /* assert RST */
876
        NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
877
        /* The min. reset hold time is 25us, so 40us should be enough */
878
        udelay( 50 );
879
        /* reset RST and interrupt */
880
        NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
881
        NCR5380_read( RESET_PARITY_INTERRUPT_REG );
882
 
883
        for( end = jiffies + AFTER_RESET_DELAY; jiffies < end; )
884
                barrier();
885
 
886
        printk( " done\n" );
887
}
888
 
889
 
890
const char * atari_scsi_info (struct Scsi_Host *host)
891
{
892
        /* atari_scsi_detect() is verbose enough... */
893
        static const char string[] = "Atari native SCSI";
894
        return string;
895
}
896
 
897
 
898
#if defined(REAL_DMA)
899
 
900
unsigned long atari_scsi_dma_setup( struct Scsi_Host *instance, void *data,
901
                                   unsigned long count, int dir )
902
{
903
        unsigned long addr = VTOP( data );
904
 
905
        DMA_PRINTK("scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
906
                   "dir = %d\n", instance->host_no, data, addr, count, dir);
907
 
908
        if (!IS_A_TT() && !STRAM_ADDR(addr)) {
909
                /* If we have a non-DMAable address on a Falcon, use the dribble
910
                 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
911
                 * handler to copy data from the dribble buffer to the originally
912
                 * wanted address.
913
                 */
914
                if (dir)
915
                        memcpy( atari_dma_buffer, data, count );
916
                else
917
                        atari_dma_orig_addr = data;
918
                addr = atari_dma_phys_buffer;
919
        }
920
 
921
        atari_dma_startaddr = addr;     /* Needed for calculating residual later. */
922
 
923
        /* Cache cleanup stuff: On writes, push any dirty cache out before sending
924
         * it to the peripheral. (Must be done before DMA setup, since at least
925
         * the ST-DMA begins to fill internal buffers right after setup. For
926
         * reads, invalidate any cache, may be altered after DMA without CPU
927
         * knowledge.
928
         *
929
         * ++roman: For the Medusa, there's no need at all for that cache stuff,
930
         * because the hardware does bus snooping (fine!).
931
         */
932
        dma_cache_maintenance( addr, count, dir );
933
 
934
        if (count == 0)
935
                printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
936
 
937
        if (IS_A_TT()) {
938
                tt_scsi_dma.dma_ctrl = dir;
939
                SCSI_DMA_WRITE_P( dma_addr, addr );
940
                SCSI_DMA_WRITE_P( dma_cnt, count );
941
                tt_scsi_dma.dma_ctrl = dir | 2;
942
        }
943
        else { /* ! IS_A_TT */
944
 
945
                /* set address */
946
                SCSI_DMA_SETADR( addr );
947
 
948
                /* toggle direction bit to clear FIFO and set DMA direction */
949
                dir <<= 8;
950
                st_dma.dma_mode_status = 0x90 | dir;
951
                st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
952
                st_dma.dma_mode_status = 0x90 | dir;
953
                udelay(40);
954
                /* On writes, round up the transfer length to the next multiple of 512
955
                 * (see also comment at atari_dma_xfer_len()). */
956
                st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
957
                udelay(40);
958
                st_dma.dma_mode_status = 0x10 | dir;
959
                udelay(40);
960
                /* need not restore value of dir, only boolean value is tested */
961
                atari_dma_active = 1;
962
        }
963
 
964
        return( count );
965
}
966
 
967
 
968
static long atari_scsi_dma_residual( struct Scsi_Host *instance )
969
{
970
        return( atari_dma_residual );
971
}
972
 
973
 
974
#define CMD_SURELY_BLOCK_MODE   0
975
#define CMD_SURELY_BYTE_MODE    1
976
#define CMD_MODE_UNKNOWN                2
977
 
978
static int falcon_classify_cmd( Scsi_Cmnd *cmd )
979
{
980
        unsigned char opcode = cmd->cmnd[0];
981
 
982
        if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
983
                opcode == READ_BUFFER)
984
                return( CMD_SURELY_BYTE_MODE );
985
        else if (opcode == READ_6 || opcode == READ_10 ||
986
                 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
987
                 opcode == RECOVER_BUFFERED_DATA) {
988
                /* In case of a sequential-access target (tape), special care is
989
                 * needed here: The transfer is block-mode only if the 'fixed' bit is
990
                 * set! */
991
                if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
992
                        return( CMD_SURELY_BYTE_MODE );
993
                else
994
                        return( CMD_SURELY_BLOCK_MODE );
995
        }
996
        else
997
                return( CMD_MODE_UNKNOWN );
998
}
999
 
1000
 
1001
/* This function calculates the number of bytes that can be transferred via
1002
 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
1003
 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
1004
 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
1005
 * possible on the Falcon, since that would require to program the DMA for
1006
 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
1007
 * the overrun problem, so this question is academic :-)
1008
 */
1009
 
1010
static unsigned long atari_dma_xfer_len( unsigned long wanted_len,
1011
                                        Scsi_Cmnd *cmd,
1012
                                        int write_flag )
1013
{
1014
        unsigned long   possible_len, limit;
1015
 
1016
        if (IS_A_TT())
1017
                /* TT SCSI DMA can transfer arbitrary #bytes */
1018
                return( wanted_len );
1019
 
1020
        /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
1021
         * 255*512 bytes, but this should be enough)
1022
         *
1023
         * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
1024
         * that return a number of bytes which cannot be known beforehand. In this
1025
         * case, the given transfer length is an "allocation length". Now it
1026
         * can happen that this allocation length is a multiple of 512 bytes and
1027
         * the DMA is used. But if not n*512 bytes really arrive, some input data
1028
         * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
1029
         * between commands that do block transfers and those that do byte
1030
         * transfers. But this isn't easy... there are lots of vendor specific
1031
         * commands, and the user can issue any command via the
1032
         * SCSI_IOCTL_SEND_COMMAND.
1033
         *
1034
         * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
1035
         * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
1036
         * and 3), the thing to do is obvious: allow any number of blocks via DMA
1037
         * or none. In case 2), we apply some heuristic: Byte mode is assumed if
1038
         * the transfer (allocation) length is < 1024, hoping that no cmd. not
1039
         * explicitly known as byte mode have such big allocation lengths...
1040
         * BTW, all the discussion above applies only to reads. DMA writes are
1041
         * unproblematic anyways, since the targets aborts the transfer after
1042
         * receiving a sufficient number of bytes.
1043
         *
1044
         * Another point: If the transfer is from/to an non-ST-RAM address, we
1045
         * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
1046
         */
1047
 
1048
        if (write_flag) {
1049
                /* Write operation can always use the DMA, but the transfer size must
1050
                 * be rounded up to the next multiple of 512 (atari_dma_setup() does
1051
                 * this).
1052
                 */
1053
                possible_len = wanted_len;
1054
        }
1055
        else {
1056
                /* Read operations: if the wanted transfer length is not a multiple of
1057
                 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
1058
                 * (no interrupt on DMA finished!)
1059
                 */
1060
                if (wanted_len & 0x1ff)
1061
                        possible_len = 0;
1062
                else {
1063
                        /* Now classify the command (see above) and decide whether it is
1064
                         * allowed to do DMA at all */
1065
                        switch( falcon_classify_cmd( cmd )) {
1066
                          case CMD_SURELY_BLOCK_MODE:
1067
                                possible_len = wanted_len;
1068
                                break;
1069
                          case CMD_SURELY_BYTE_MODE:
1070
                                possible_len = 0; /* DMA prohibited */
1071
                                break;
1072
                          case CMD_MODE_UNKNOWN:
1073
                          default:
1074
                                /* For unknown commands assume block transfers if the transfer
1075
                                 * size/allocation length is >= 1024 */
1076
                                possible_len = (wanted_len < 1024) ? 0 : wanted_len;
1077
                                break;
1078
                        }
1079
                }
1080
        }
1081
 
1082
        /* Last step: apply the hard limit on DMA transfers */
1083
        limit = (atari_dma_buffer && !STRAM_ADDR( VTOP(cmd->SCp.ptr) )) ?
1084
                    STRAM_BUFFER_SIZE : 255*512;
1085
        if (possible_len > limit)
1086
                possible_len = limit;
1087
 
1088
        DMA_PRINTK("Sorry, must cut DMA transfer size to %ld bytes instead "
1089
                   "of %ld\n", possible_len, wanted_len);
1090
 
1091
        return( possible_len );
1092
}
1093
 
1094
 
1095
#endif  /* REAL_DMA */
1096
 
1097
 
1098
/* NCR5380 register access functions
1099
 *
1100
 * There are separate functions for TT and Falcon, because the access
1101
 * methods are quite different. The calling macros NCR5380_read and
1102
 * NCR5380_write call these functions via function pointers.
1103
 */
1104
 
1105
static unsigned char atari_scsi_tt_reg_read( unsigned char reg )
1106
{
1107
        return( tt_scsi_regp[reg * 2] );
1108
}
1109
 
1110
static void atari_scsi_tt_reg_write( unsigned char reg, unsigned char value )
1111
{
1112
        tt_scsi_regp[reg * 2] = value;
1113
}
1114
 
1115
static unsigned char atari_scsi_falcon_reg_read( unsigned char reg )
1116
{
1117
        dma_wd.dma_mode_status= (u_short)(0x88 + reg);
1118
        return( (u_char)dma_wd.fdc_acces_seccount );
1119
}
1120
 
1121
static void atari_scsi_falcon_reg_write( unsigned char reg, unsigned char value )
1122
{
1123
        dma_wd.dma_mode_status = (u_short)(0x88 + reg);
1124
        dma_wd.fdc_acces_seccount = (u_short)value;
1125
}
1126
 
1127
 
1128
#include "atari_NCR5380.c"
1129
 
1130
#ifdef MODULE
1131
Scsi_Host_Template driver_template = ATARI_SCSI;
1132
 
1133
#include "scsi_module.c"
1134
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.