1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/module.h>
36#include <linux/types.h>
37#include <linux/blkdev.h>
38#include <linux/interrupt.h>
39#include <linux/init.h>
40#include <linux/nvram.h>
41#include <linux/bitops.h>
42#include <linux/wait.h>
43#include <linux/platform_device.h>
44
45#include <asm/setup.h>
46#include <asm/atarihw.h>
47#include <asm/atariints.h>
48#include <asm/atari_stdma.h>
49#include <asm/atari_stram.h>
50#include <asm/io.h>
51
52#include <scsi/scsi_host.h>
53
54#define DMA_MIN_SIZE 32
55
56
57
58#define NCR5380_implementation_fields
59
60#define NCR5380_read(reg) atari_scsi_reg_read(reg)
61#define NCR5380_write(reg, value) atari_scsi_reg_write(reg, value)
62
63#define NCR5380_queue_command atari_scsi_queue_command
64#define NCR5380_abort atari_scsi_abort
65#define NCR5380_info atari_scsi_info
66
67#define NCR5380_dma_recv_setup(instance, data, count) \
68 atari_scsi_dma_setup(instance, data, count, 0)
69#define NCR5380_dma_send_setup(instance, data, count) \
70 atari_scsi_dma_setup(instance, data, count, 1)
71#define NCR5380_dma_residual(instance) \
72 atari_scsi_dma_residual(instance)
73#define NCR5380_dma_xfer_len(instance, cmd, phase) \
74 atari_dma_xfer_len(cmd->SCp.this_residual, cmd, !((phase) & SR_IO))
75
76#define NCR5380_acquire_dma_irq(instance) falcon_get_lock(instance)
77#define NCR5380_release_dma_irq(instance) falcon_release_lock()
78
79#include "NCR5380.h"
80
81
82#define IS_A_TT() ATARIHW_PRESENT(TT_SCSI)
83
84#define SCSI_DMA_WRITE_P(elt,val) \
85 do { \
86 unsigned long v = val; \
87 tt_scsi_dma.elt##_lo = v & 0xff; \
88 v >>= 8; \
89 tt_scsi_dma.elt##_lmd = v & 0xff; \
90 v >>= 8; \
91 tt_scsi_dma.elt##_hmd = v & 0xff; \
92 v >>= 8; \
93 tt_scsi_dma.elt##_hi = v & 0xff; \
94 } while(0)
95
96#define SCSI_DMA_READ_P(elt) \
97 (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \
98 (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \
99 (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \
100 (unsigned long)tt_scsi_dma.elt##_lo)
101
102
103static inline void SCSI_DMA_SETADR(unsigned long adr)
104{
105 st_dma.dma_lo = (unsigned char)adr;
106 MFPDELAY();
107 adr >>= 8;
108 st_dma.dma_md = (unsigned char)adr;
109 MFPDELAY();
110 adr >>= 8;
111 st_dma.dma_hi = (unsigned char)adr;
112 MFPDELAY();
113}
114
115static inline unsigned long SCSI_DMA_GETADR(void)
116{
117 unsigned long adr;
118 adr = st_dma.dma_lo;
119 MFPDELAY();
120 adr |= (st_dma.dma_md & 0xff) << 8;
121 MFPDELAY();
122 adr |= (st_dma.dma_hi & 0xff) << 16;
123 MFPDELAY();
124 return adr;
125}
126
127static void atari_scsi_fetch_restbytes(void);
128
129static unsigned char (*atari_scsi_reg_read)(unsigned char reg);
130static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value);
131
132static unsigned long atari_dma_residual, atari_dma_startaddr;
133static short atari_dma_active;
134
135static char *atari_dma_buffer;
136
137static unsigned long atari_dma_phys_buffer;
138
139static char *atari_dma_orig_addr;
140
141
142
143
144
145
146#define STRAM_BUFFER_SIZE (4096)
147
148static unsigned long atari_dma_stram_mask;
149#define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0)
150
151static int setup_can_queue = -1;
152module_param(setup_can_queue, int, 0);
153static int setup_cmd_per_lun = -1;
154module_param(setup_cmd_per_lun, int, 0);
155static int setup_sg_tablesize = -1;
156module_param(setup_sg_tablesize, int, 0);
157static int setup_hostid = -1;
158module_param(setup_hostid, int, 0);
159static int setup_toshiba_delay = -1;
160module_param(setup_toshiba_delay, int, 0);
161
162
163static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
164{
165 int i;
166 unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
167
168 if (dma_stat & 0x01) {
169
170
171
172
173
174
175 for (i = 0; i < m68k_num_memory; ++i) {
176 end_addr = m68k_memory[i].addr + m68k_memory[i].size;
177 if (end_addr <= addr && addr <= end_addr + 4)
178 return 1;
179 }
180 }
181 return 0;
182}
183
184
185#if 0
186
187
188
189
190static void scsi_dma_buserr(int irq, void *dummy)
191{
192 unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
193
194
195
196 if (atari_irq_pending(IRQ_TT_MFP_SCSI))
197 return;
198
199 printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
200 SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
201 if (dma_stat & 0x80) {
202 if (!scsi_dma_is_ignored_buserr(dma_stat))
203 printk("SCSI DMA bus error -- bad DMA programming!\n");
204 } else {
205
206
207
208
209
210 printk("SCSI DMA intr ?? -- this shouldn't happen!\n");
211 }
212}
213#endif
214
215
216static irqreturn_t scsi_tt_intr(int irq, void *dev)
217{
218 struct Scsi_Host *instance = dev;
219 struct NCR5380_hostdata *hostdata = shost_priv(instance);
220 int dma_stat;
221
222 dma_stat = tt_scsi_dma.dma_ctrl;
223
224 dsprintk(NDEBUG_INTR, instance, "NCR5380 interrupt, DMA status = %02x\n",
225 dma_stat & 0xff);
226
227
228
229
230 if (dma_stat & 0x80) {
231 if (!scsi_dma_is_ignored_buserr(dma_stat)) {
232 printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
233 SCSI_DMA_READ_P(dma_addr));
234 printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
235 }
236 }
237
238
239
240
241
242
243
244
245
246
247 if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
248 atari_dma_residual = hostdata->dma_len -
249 (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
250
251 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
252 atari_dma_residual);
253
254 if ((signed int)atari_dma_residual < 0)
255 atari_dma_residual = 0;
256 if ((dma_stat & 1) == 0) {
257
258
259
260
261 atari_scsi_fetch_restbytes();
262 } else {
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 if (atari_dma_residual & 0x1ff) {
282 dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
283 "difference %ld bytes\n",
284 512 - (atari_dma_residual & 0x1ff));
285 atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
286 }
287 }
288 tt_scsi_dma.dma_ctrl = 0;
289 }
290
291
292 if (dma_stat & 0x40) {
293 atari_dma_residual = 0;
294 if ((dma_stat & 1) == 0)
295 atari_scsi_fetch_restbytes();
296 tt_scsi_dma.dma_ctrl = 0;
297 }
298
299 NCR5380_intr(irq, dev);
300
301 return IRQ_HANDLED;
302}
303
304
305static irqreturn_t scsi_falcon_intr(int irq, void *dev)
306{
307 struct Scsi_Host *instance = dev;
308 struct NCR5380_hostdata *hostdata = shost_priv(instance);
309 int dma_stat;
310
311
312
313
314 st_dma.dma_mode_status = 0x90;
315 dma_stat = st_dma.dma_mode_status;
316
317
318
319
320 if (!(dma_stat & 0x01)) {
321
322 printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
323 }
324
325
326
327
328
329
330 if (atari_dma_active && (dma_stat & 0x02)) {
331 unsigned long transferred;
332
333 transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
334
335
336
337
338
339 if (transferred & 15)
340 printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
341 "ST-DMA fifo\n", transferred & 15);
342
343 atari_dma_residual = hostdata->dma_len - transferred;
344 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
345 atari_dma_residual);
346 } else
347 atari_dma_residual = 0;
348 atari_dma_active = 0;
349
350 if (atari_dma_orig_addr) {
351
352
353
354 memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
355 hostdata->dma_len - atari_dma_residual);
356 atari_dma_orig_addr = NULL;
357 }
358
359 NCR5380_intr(irq, dev);
360
361 return IRQ_HANDLED;
362}
363
364
365static void atari_scsi_fetch_restbytes(void)
366{
367 int nr;
368 char *src, *dst;
369 unsigned long phys_dst;
370
371
372 phys_dst = SCSI_DMA_READ_P(dma_addr);
373 nr = phys_dst & 3;
374 if (nr) {
375
376
377 phys_dst ^= nr;
378 dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
379 nr, phys_dst);
380
381 dst = phys_to_virt(phys_dst);
382 dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
383 for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
384 *dst++ = *src++;
385 }
386}
387
388
389
390
391
392
393static void falcon_release_lock(void)
394{
395 if (IS_A_TT())
396 return;
397
398 if (stdma_is_locked_by(scsi_falcon_intr))
399 stdma_release();
400}
401
402
403
404
405
406
407
408
409static int falcon_get_lock(struct Scsi_Host *instance)
410{
411 if (IS_A_TT())
412 return 1;
413
414 if (stdma_is_locked_by(scsi_falcon_intr) &&
415 instance->hostt->can_queue > 1)
416 return 1;
417
418 if (in_interrupt())
419 return stdma_try_lock(scsi_falcon_intr, instance);
420
421 stdma_lock(scsi_falcon_intr, instance);
422 return 1;
423}
424
425#ifndef MODULE
426static int __init atari_scsi_setup(char *str)
427{
428
429
430
431
432
433 int ints[8];
434
435 get_options(str, ARRAY_SIZE(ints), ints);
436
437 if (ints[0] < 1) {
438 printk("atari_scsi_setup: no arguments!\n");
439 return 0;
440 }
441 if (ints[0] >= 1)
442 setup_can_queue = ints[1];
443 if (ints[0] >= 2)
444 setup_cmd_per_lun = ints[2];
445 if (ints[0] >= 3)
446 setup_sg_tablesize = ints[3];
447 if (ints[0] >= 4)
448 setup_hostid = ints[4];
449
450
451 if (ints[0] >= 7)
452 setup_toshiba_delay = ints[7];
453
454 return 1;
455}
456
457__setup("atascsi=", atari_scsi_setup);
458#endif
459
460
461static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance,
462 void *data, unsigned long count,
463 int dir)
464{
465 unsigned long addr = virt_to_phys(data);
466
467 dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
468 "dir = %d\n", instance->host_no, data, addr, count, dir);
469
470 if (!IS_A_TT() && !STRAM_ADDR(addr)) {
471
472
473
474
475
476 if (dir)
477 memcpy(atari_dma_buffer, data, count);
478 else
479 atari_dma_orig_addr = data;
480 addr = atari_dma_phys_buffer;
481 }
482
483 atari_dma_startaddr = addr;
484
485
486
487
488
489
490
491
492
493
494 dma_cache_maintenance(addr, count, dir);
495
496 if (IS_A_TT()) {
497 tt_scsi_dma.dma_ctrl = dir;
498 SCSI_DMA_WRITE_P(dma_addr, addr);
499 SCSI_DMA_WRITE_P(dma_cnt, count);
500 tt_scsi_dma.dma_ctrl = dir | 2;
501 } else {
502
503
504 SCSI_DMA_SETADR(addr);
505
506
507 dir <<= 8;
508 st_dma.dma_mode_status = 0x90 | dir;
509 st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
510 st_dma.dma_mode_status = 0x90 | dir;
511 udelay(40);
512
513
514 st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
515 udelay(40);
516 st_dma.dma_mode_status = 0x10 | dir;
517 udelay(40);
518
519 atari_dma_active = 1;
520 }
521
522 return count;
523}
524
525
526static long atari_scsi_dma_residual(struct Scsi_Host *instance)
527{
528 return atari_dma_residual;
529}
530
531
532#define CMD_SURELY_BLOCK_MODE 0
533#define CMD_SURELY_BYTE_MODE 1
534#define CMD_MODE_UNKNOWN 2
535
536static int falcon_classify_cmd(struct scsi_cmnd *cmd)
537{
538 unsigned char opcode = cmd->cmnd[0];
539
540 if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
541 opcode == READ_BUFFER)
542 return CMD_SURELY_BYTE_MODE;
543 else if (opcode == READ_6 || opcode == READ_10 ||
544 opcode == 0xa8 || opcode == READ_REVERSE ||
545 opcode == RECOVER_BUFFERED_DATA) {
546
547
548
549 if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
550 return CMD_SURELY_BYTE_MODE;
551 else
552 return CMD_SURELY_BLOCK_MODE;
553 } else
554 return CMD_MODE_UNKNOWN;
555}
556
557
558
559
560
561
562
563
564
565
566
567static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
568 struct scsi_cmnd *cmd, int write_flag)
569{
570 unsigned long possible_len, limit;
571
572 if (wanted_len < DMA_MIN_SIZE)
573 return 0;
574
575 if (IS_A_TT())
576
577 return wanted_len;
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607 if (write_flag) {
608
609
610
611
612 possible_len = wanted_len;
613 } else {
614
615
616
617
618 if (wanted_len & 0x1ff)
619 possible_len = 0;
620 else {
621
622
623 switch (falcon_classify_cmd(cmd)) {
624 case CMD_SURELY_BLOCK_MODE:
625 possible_len = wanted_len;
626 break;
627 case CMD_SURELY_BYTE_MODE:
628 possible_len = 0;
629 break;
630 case CMD_MODE_UNKNOWN:
631 default:
632
633
634 possible_len = (wanted_len < 1024) ? 0 : wanted_len;
635 break;
636 }
637 }
638 }
639
640
641 limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
642 STRAM_BUFFER_SIZE : 255*512;
643 if (possible_len > limit)
644 possible_len = limit;
645
646 if (possible_len != wanted_len)
647 dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes "
648 "instead of %ld\n", possible_len, wanted_len);
649
650 return possible_len;
651}
652
653
654
655
656
657
658
659
660
661static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
662{
663 return tt_scsi_regp[reg * 2];
664}
665
666static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
667{
668 tt_scsi_regp[reg * 2] = value;
669}
670
671static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
672{
673 dma_wd.dma_mode_status= (u_short)(0x88 + reg);
674 return (u_char)dma_wd.fdc_acces_seccount;
675}
676
677static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
678{
679 dma_wd.dma_mode_status = (u_short)(0x88 + reg);
680 dma_wd.fdc_acces_seccount = (u_short)value;
681}
682
683
684#include "NCR5380.c"
685
686static int atari_scsi_bus_reset(struct scsi_cmnd *cmd)
687{
688 int rv;
689 unsigned long flags;
690
691 local_irq_save(flags);
692
693
694 if (IS_A_TT()) {
695 tt_scsi_dma.dma_ctrl = 0;
696 } else {
697 st_dma.dma_mode_status = 0x90;
698 atari_dma_active = 0;
699 atari_dma_orig_addr = NULL;
700 }
701
702 rv = NCR5380_bus_reset(cmd);
703
704
705
706
707
708
709
710 local_irq_restore(flags);
711
712 return rv;
713}
714
715#define DRV_MODULE_NAME "atari_scsi"
716#define PFX DRV_MODULE_NAME ": "
717
718static struct scsi_host_template atari_scsi_template = {
719 .module = THIS_MODULE,
720 .proc_name = DRV_MODULE_NAME,
721 .name = "Atari native SCSI",
722 .info = atari_scsi_info,
723 .queuecommand = atari_scsi_queue_command,
724 .eh_abort_handler = atari_scsi_abort,
725 .eh_bus_reset_handler = atari_scsi_bus_reset,
726 .this_id = 7,
727 .cmd_per_lun = 2,
728 .use_clustering = DISABLE_CLUSTERING,
729 .cmd_size = NCR5380_CMD_SIZE,
730};
731
732static int __init atari_scsi_probe(struct platform_device *pdev)
733{
734 struct Scsi_Host *instance;
735 int error;
736 struct resource *irq;
737 int host_flags = 0;
738
739 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
740 if (!irq)
741 return -ENODEV;
742
743 if (ATARIHW_PRESENT(TT_SCSI)) {
744 atari_scsi_reg_read = atari_scsi_tt_reg_read;
745 atari_scsi_reg_write = atari_scsi_tt_reg_write;
746 } else {
747 atari_scsi_reg_read = atari_scsi_falcon_reg_read;
748 atari_scsi_reg_write = atari_scsi_falcon_reg_write;
749 }
750
751 if (ATARIHW_PRESENT(TT_SCSI)) {
752 atari_scsi_template.can_queue = 16;
753 atari_scsi_template.sg_tablesize = SG_ALL;
754 } else {
755 atari_scsi_template.can_queue = 1;
756 atari_scsi_template.sg_tablesize = SG_NONE;
757 }
758
759 if (setup_can_queue > 0)
760 atari_scsi_template.can_queue = setup_can_queue;
761
762 if (setup_cmd_per_lun > 0)
763 atari_scsi_template.cmd_per_lun = setup_cmd_per_lun;
764
765
766 if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize >= 0)
767 atari_scsi_template.sg_tablesize = setup_sg_tablesize;
768
769 if (setup_hostid >= 0) {
770 atari_scsi_template.this_id = setup_hostid & 7;
771 } else {
772
773 if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
774 unsigned char b = nvram_read_byte(16);
775
776
777
778
779 if (b & 0x80)
780 atari_scsi_template.this_id = b & 7;
781 }
782 }
783
784
785
786
787
788
789 if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) &&
790 m68k_num_memory > 1) {
791 atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
792 if (!atari_dma_buffer) {
793 pr_err(PFX "can't allocate ST-RAM double buffer\n");
794 return -ENOMEM;
795 }
796 atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
797 atari_dma_orig_addr = 0;
798 }
799
800 instance = scsi_host_alloc(&atari_scsi_template,
801 sizeof(struct NCR5380_hostdata));
802 if (!instance) {
803 error = -ENOMEM;
804 goto fail_alloc;
805 }
806
807 instance->irq = irq->start;
808
809 host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP;
810 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
811
812 error = NCR5380_init(instance, host_flags);
813 if (error)
814 goto fail_init;
815
816 if (IS_A_TT()) {
817 error = request_irq(instance->irq, scsi_tt_intr, 0,
818 "NCR5380", instance);
819 if (error) {
820 pr_err(PFX "request irq %d failed, aborting\n",
821 instance->irq);
822 goto fail_irq;
823 }
824 tt_mfp.active_edge |= 0x80;
825
826 tt_scsi_dma.dma_ctrl = 0;
827 atari_dma_residual = 0;
828
829
830
831
832
833
834
835
836
837
838
839
840
841 if (MACH_IS_MEDUSA) {
842 struct NCR5380_hostdata *hostdata =
843 shost_priv(instance);
844
845 hostdata->read_overruns = 4;
846 }
847 } else {
848
849
850
851 atari_dma_residual = 0;
852 atari_dma_active = 0;
853 atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
854 : 0xff000000);
855 }
856
857 NCR5380_maybe_reset_bus(instance);
858
859 error = scsi_add_host(instance, NULL);
860 if (error)
861 goto fail_host;
862
863 platform_set_drvdata(pdev, instance);
864
865 scsi_scan_host(instance);
866 return 0;
867
868fail_host:
869 if (IS_A_TT())
870 free_irq(instance->irq, instance);
871fail_irq:
872 NCR5380_exit(instance);
873fail_init:
874 scsi_host_put(instance);
875fail_alloc:
876 if (atari_dma_buffer)
877 atari_stram_free(atari_dma_buffer);
878 return error;
879}
880
881static int __exit atari_scsi_remove(struct platform_device *pdev)
882{
883 struct Scsi_Host *instance = platform_get_drvdata(pdev);
884
885 scsi_remove_host(instance);
886 if (IS_A_TT())
887 free_irq(instance->irq, instance);
888 NCR5380_exit(instance);
889 scsi_host_put(instance);
890 if (atari_dma_buffer)
891 atari_stram_free(atari_dma_buffer);
892 return 0;
893}
894
895static struct platform_driver atari_scsi_driver = {
896 .remove = __exit_p(atari_scsi_remove),
897 .driver = {
898 .name = DRV_MODULE_NAME,
899 },
900};
901
902module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe);
903
904MODULE_ALIAS("platform:" DRV_MODULE_NAME);
905MODULE_LICENSE("GPL");
906