ncr5380: Enable PDMA for NCR53C400A
[linux-2.6-block.git] / drivers / scsi / g_NCR5380.c
CommitLineData
1da177e4
LT
1/*
2 * Generic Generic NCR5380 driver
3 *
4 * Copyright 1993, Drew Eckhardt
5 * Visionary Computing
6 * (Unix and Linux consulting and custom programming)
7 * drew@colorado.edu
8 * +1 (303) 440-4894
9 *
10 * NCR53C400 extensions (c) 1994,1995,1996, Kevin Lentin
11 * K.Lentin@cs.monash.edu.au
12 *
13 * NCR53C400A extensions (c) 1996, Ingmar Baumgart
14 * ingmar@gonzo.schwaben.de
15 *
16 * DTC3181E extensions (c) 1997, Ronald van Cuijlenborg
17 * ronald.van.cuijlenborg@tip.nl or nutty@dds.nl
18 *
19 * Added ISAPNP support for DTC436 adapters,
20 * Thomas Sailer, sailer@ife.ee.ethz.ch
1da177e4
LT
21 */
22
23/*
24 * TODO : flesh out DMA support, find some one actually using this (I have
25 * a memory mapped Trantor board that works fine)
26 */
27
28/*
1da177e4
LT
29 * The card is detected and initialized in one of several ways :
30 * 1. With command line overrides - NCR5380=port,irq may be
31 * used on the LILO command line to override the defaults.
32 *
33 * 2. With the GENERIC_NCR5380_OVERRIDE compile time define. This is
34 * specified as an array of address, irq, dma, board tuples. Ie, for
35 * one board at 0x350, IRQ5, no dma, I could say
36 * -DGENERIC_NCR5380_OVERRIDE={{0xcc000, 5, DMA_NONE, BOARD_NCR5380}}
37 *
38 * -1 should be specified for no or DMA interrupt, -2 to autoprobe for an
39 * IRQ line if overridden on the command line.
40 *
41 * 3. When included as a module, with arguments passed on the command line:
42 * ncr_irq=xx the interrupt
43 * ncr_addr=xx the port or base address (for port or memory
44 * mapped, resp.)
45 * ncr_dma=xx the DMA
46 * ncr_5380=1 to set up for a NCR5380 board
47 * ncr_53c400=1 to set up for a NCR53C400 board
48 * e.g.
49 * modprobe g_NCR5380 ncr_irq=5 ncr_addr=0x350 ncr_5380=1
50 * for a port mapped NCR5380 board or
51 * modprobe g_NCR5380 ncr_irq=255 ncr_addr=0xc8000 ncr_53c400=1
52 * for a memory mapped NCR53C400 board with interrupts disabled.
53 *
54 * 255 should be specified for no or DMA interrupt, 254 to autoprobe for an
55 * IRQ line if overridden on the command line.
56 *
57 */
58
1da177e4 59#define AUTOPROBE_IRQ
1da177e4
LT
60
61#ifdef CONFIG_SCSI_GENERIC_NCR53C400
1da177e4 62#define PSEUDO_DMA
1da177e4
LT
63#endif
64
1da177e4 65#include <asm/io.h>
1da177e4 66#include <linux/blkdev.h>
161c0059 67#include <linux/module.h>
1da177e4
LT
68#include <scsi/scsi_host.h>
69#include "g_NCR5380.h"
70#include "NCR5380.h"
1da177e4
LT
71#include <linux/init.h>
72#include <linux/ioport.h>
73#include <linux/isapnp.h>
1da177e4
LT
74#include <linux/interrupt.h>
75
c0965e63
FT
76static int ncr_irq;
77static int ncr_dma;
78static int ncr_addr;
79static int ncr_5380;
80static int ncr_53c400;
81static int ncr_53c400a;
82static int dtc_3181e;
1da177e4
LT
83
84static struct override {
c818cb64 85 NCR5380_map_type NCR5380_map_name;
1da177e4
LT
86 int irq;
87 int dma;
88 int board; /* Use NCR53c400, Ricoh, etc. extensions ? */
89} overrides
90#ifdef GENERIC_NCR5380_OVERRIDE
91[] __initdata = GENERIC_NCR5380_OVERRIDE;
92#else
93[1] __initdata = { { 0,},};
94#endif
95
6391a113 96#define NO_OVERRIDES ARRAY_SIZE(overrides)
1da177e4 97
6391a113 98#ifndef MODULE
1da177e4
LT
99
100/**
101 * internal_setup - handle lilo command string override
102 * @board: BOARD_* identifier for the board
103 * @str: unused
104 * @ints: numeric parameters
105 *
106 * Do LILO command line initialization of the overrides array. Display
107 * errors when needed
108 *
109 * Locks: none
110 */
111
112static void __init internal_setup(int board, char *str, int *ints)
113{
d5f7e65d 114 static int commandline_current;
1da177e4
LT
115 switch (board) {
116 case BOARD_NCR5380:
117 if (ints[0] != 2 && ints[0] != 3) {
118 printk(KERN_ERR "generic_NCR5380_setup : usage ncr5380=" STRVAL(NCR5380_map_name) ",irq,dma\n");
119 return;
120 }
121 break;
122 case BOARD_NCR53C400:
123 if (ints[0] != 2) {
124 printk(KERN_ERR "generic_NCR53C400_setup : usage ncr53c400=" STRVAL(NCR5380_map_name) ",irq\n");
125 return;
126 }
127 break;
128 case BOARD_NCR53C400A:
129 if (ints[0] != 2) {
130 printk(KERN_ERR "generic_NCR53C400A_setup : usage ncr53c400a=" STRVAL(NCR5380_map_name) ",irq\n");
131 return;
132 }
133 break;
134 case BOARD_DTC3181E:
135 if (ints[0] != 2) {
136 printk("generic_DTC3181E_setup : usage dtc3181e=" STRVAL(NCR5380_map_name) ",irq\n");
137 return;
138 }
139 break;
140 }
141
142 if (commandline_current < NO_OVERRIDES) {
143 overrides[commandline_current].NCR5380_map_name = (NCR5380_map_type) ints[1];
144 overrides[commandline_current].irq = ints[2];
145 if (ints[0] == 3)
146 overrides[commandline_current].dma = ints[3];
147 else
148 overrides[commandline_current].dma = DMA_NONE;
149 overrides[commandline_current].board = board;
150 ++commandline_current;
151 }
152}
153
154
155/**
156 * do_NCR53C80_setup - set up entry point
157 * @str: unused
158 *
159 * Setup function invoked at boot to parse the ncr5380= command
160 * line.
161 */
162
163static int __init do_NCR5380_setup(char *str)
164{
165 int ints[10];
166
6391a113 167 get_options(str, ARRAY_SIZE(ints), ints);
1da177e4
LT
168 internal_setup(BOARD_NCR5380, str, ints);
169 return 1;
170}
171
172/**
173 * do_NCR53C400_setup - set up entry point
174 * @str: unused
6391a113 175 * @ints: integer parameters from kernel setup code
1da177e4
LT
176 *
177 * Setup function invoked at boot to parse the ncr53c400= command
178 * line.
179 */
180
181static int __init do_NCR53C400_setup(char *str)
182{
183 int ints[10];
184
6391a113 185 get_options(str, ARRAY_SIZE(ints), ints);
1da177e4
LT
186 internal_setup(BOARD_NCR53C400, str, ints);
187 return 1;
188}
189
190/**
191 * do_NCR53C400A_setup - set up entry point
192 * @str: unused
6391a113 193 * @ints: integer parameters from kernel setup code
1da177e4
LT
194 *
195 * Setup function invoked at boot to parse the ncr53c400a= command
196 * line.
197 */
198
199static int __init do_NCR53C400A_setup(char *str)
200{
201 int ints[10];
202
6391a113 203 get_options(str, ARRAY_SIZE(ints), ints);
1da177e4
LT
204 internal_setup(BOARD_NCR53C400A, str, ints);
205 return 1;
206}
207
208/**
209 * do_DTC3181E_setup - set up entry point
210 * @str: unused
6391a113 211 * @ints: integer parameters from kernel setup code
1da177e4
LT
212 *
213 * Setup function invoked at boot to parse the dtc3181e= command
214 * line.
215 */
216
217static int __init do_DTC3181E_setup(char *str)
218{
219 int ints[10];
220
6391a113 221 get_options(str, ARRAY_SIZE(ints), ints);
1da177e4
LT
222 internal_setup(BOARD_DTC3181E, str, ints);
223 return 1;
224}
225
226#endif
227
228/**
229 * generic_NCR5380_detect - look for NCR5380 controllers
230 * @tpnt: the scsi template
231 *
232 * Scan for the present of NCR5380, NCR53C400, NCR53C400A, DTC3181E
233 * and DTC436(ISAPnP) controllers. If overrides have been set we use
234 * them.
235 *
1da177e4
LT
236 * Locks: none
237 */
238
ed8b9e7f 239static int __init generic_NCR5380_detect(struct scsi_host_template *tpnt)
1da177e4 240{
d5f7e65d 241 static int current_override;
702a98c6 242 int count;
1da177e4 243 unsigned int *ports;
702a98c6
OZ
244#ifndef SCSI_G_NCR5380_MEM
245 int i;
1da177e4 246 unsigned long region_size = 16;
702a98c6 247#endif
1da177e4
LT
248 static unsigned int __initdata ncr_53c400a_ports[] = {
249 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
250 };
251 static unsigned int __initdata dtc_3181e_ports[] = {
252 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
253 };
4d8c08c7 254 int flags;
1da177e4 255 struct Scsi_Host *instance;
12150797 256 struct NCR5380_hostdata *hostdata;
702a98c6 257#ifdef SCSI_G_NCR5380_MEM
c818cb64
AV
258 unsigned long base;
259 void __iomem *iomem;
260#endif
1da177e4 261
c0965e63 262 if (ncr_irq)
1da177e4 263 overrides[0].irq = ncr_irq;
c0965e63 264 if (ncr_dma)
1da177e4 265 overrides[0].dma = ncr_dma;
c0965e63 266 if (ncr_addr)
1da177e4 267 overrides[0].NCR5380_map_name = (NCR5380_map_type) ncr_addr;
c0965e63 268 if (ncr_5380)
1da177e4 269 overrides[0].board = BOARD_NCR5380;
c0965e63 270 else if (ncr_53c400)
1da177e4 271 overrides[0].board = BOARD_NCR53C400;
c0965e63 272 else if (ncr_53c400a)
1da177e4 273 overrides[0].board = BOARD_NCR53C400A;
c0965e63 274 else if (dtc_3181e)
1da177e4 275 overrides[0].board = BOARD_DTC3181E;
702a98c6 276#ifndef SCSI_G_NCR5380_MEM
1da177e4
LT
277 if (!current_override && isapnp_present()) {
278 struct pnp_dev *dev = NULL;
279 count = 0;
280 while ((dev = pnp_find_dev(NULL, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev))) {
281 if (count >= NO_OVERRIDES)
282 break;
c94babba 283 if (pnp_device_attach(dev) < 0)
1da177e4 284 continue;
1da177e4
LT
285 if (pnp_activate_dev(dev) < 0) {
286 printk(KERN_ERR "dtc436e probe: activate failed\n");
287 pnp_device_detach(dev);
288 continue;
289 }
290 if (!pnp_port_valid(dev, 0)) {
291 printk(KERN_ERR "dtc436e probe: no valid port\n");
292 pnp_device_detach(dev);
293 continue;
294 }
295 if (pnp_irq_valid(dev, 0))
296 overrides[count].irq = pnp_irq(dev, 0);
297 else
22f5f10d 298 overrides[count].irq = NO_IRQ;
1da177e4
LT
299 if (pnp_dma_valid(dev, 0))
300 overrides[count].dma = pnp_dma(dev, 0);
301 else
302 overrides[count].dma = DMA_NONE;
303 overrides[count].NCR5380_map_name = (NCR5380_map_type) pnp_port_start(dev, 0);
304 overrides[count].board = BOARD_DTC3181E;
305 count++;
306 }
307 }
702a98c6 308#endif
1da177e4
LT
309
310 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
311 if (!(overrides[current_override].NCR5380_map_name))
312 continue;
313
314 ports = NULL;
4d8c08c7 315 flags = 0;
1da177e4
LT
316 switch (overrides[current_override].board) {
317 case BOARD_NCR5380:
318 flags = FLAG_NO_PSEUDO_DMA;
319 break;
320 case BOARD_NCR53C400:
4d8c08c7 321#ifdef PSEUDO_DMA
55181be8 322 flags = FLAG_NO_DMA_FIXUP;
4d8c08c7 323#endif
1da177e4
LT
324 break;
325 case BOARD_NCR53C400A:
cecf3bee 326 flags = FLAG_NO_DMA_FIXUP;
1da177e4
LT
327 ports = ncr_53c400a_ports;
328 break;
329 case BOARD_DTC3181E:
be3f4121 330 flags = FLAG_NO_PSEUDO_DMA;
1da177e4
LT
331 ports = dtc_3181e_ports;
332 break;
333 }
334
702a98c6 335#ifndef SCSI_G_NCR5380_MEM
1da177e4
LT
336 if (ports) {
337 /* wakeup sequence for the NCR53C400A and DTC3181E */
338
339 /* Disable the adapter and look for a free io port */
340 outb(0x59, 0x779);
341 outb(0xb9, 0x379);
342 outb(0xc5, 0x379);
343 outb(0xae, 0x379);
344 outb(0xa6, 0x379);
345 outb(0x00, 0x379);
346
347 if (overrides[current_override].NCR5380_map_name != PORT_AUTO)
348 for (i = 0; ports[i]; i++) {
349 if (!request_region(ports[i], 16, "ncr53c80"))
350 continue;
351 if (overrides[current_override].NCR5380_map_name == ports[i])
352 break;
353 release_region(ports[i], 16);
354 } else
355 for (i = 0; ports[i]; i++) {
356 if (!request_region(ports[i], 16, "ncr53c80"))
357 continue;
358 if (inb(ports[i]) == 0xff)
359 break;
360 release_region(ports[i], 16);
361 }
362 if (ports[i]) {
363 /* At this point we have our region reserved */
364 outb(0x59, 0x779);
365 outb(0xb9, 0x379);
366 outb(0xc5, 0x379);
367 outb(0xae, 0x379);
368 outb(0xa6, 0x379);
369 outb(0x80 | i, 0x379); /* set io port to be used */
370 outb(0xc0, ports[i] + 9);
371 if (inb(ports[i] + 9) != 0x80)
372 continue;
373 else
374 overrides[current_override].NCR5380_map_name = ports[i];
375 } else
376 continue;
377 }
378 else
379 {
380 /* Not a 53C400A style setup - just grab */
381 if(!(request_region(overrides[current_override].NCR5380_map_name, NCR5380_region_size, "ncr5380")))
382 continue;
383 region_size = NCR5380_region_size;
384 }
385#else
c818cb64
AV
386 base = overrides[current_override].NCR5380_map_name;
387 if (!request_mem_region(base, NCR5380_region_size, "ncr5380"))
388 continue;
389 iomem = ioremap(base, NCR5380_region_size);
390 if (!iomem) {
391 release_mem_region(base, NCR5380_region_size);
1da177e4 392 continue;
c818cb64 393 }
1da177e4
LT
394#endif
395 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
0ad0eff9
FT
396 if (instance == NULL)
397 goto out_release;
12150797 398 hostdata = shost_priv(instance);
1da177e4 399
702a98c6 400#ifndef SCSI_G_NCR5380_MEM
b01ec348 401 instance->io_port = overrides[current_override].NCR5380_map_name;
1da177e4 402 instance->n_io_port = region_size;
4d8c08c7
FT
403
404 /*
405 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
406 * the base address.
407 */
cecf3bee
OZ
408 switch (overrides[current_override].board) {
409 case BOARD_NCR53C400:
4d8c08c7 410 instance->io_port += 8;
12150797
OZ
411 hostdata->c400_ctl_status = 0;
412 hostdata->c400_blk_cnt = 1;
413 hostdata->c400_host_buf = 4;
cecf3bee
OZ
414 break;
415 case BOARD_NCR53C400A:
416 hostdata->c400_ctl_status = 9;
417 hostdata->c400_blk_cnt = 10;
418 hostdata->c400_host_buf = 8;
419 break;
12150797 420 }
c818cb64 421#else
b01ec348 422 instance->base = overrides[current_override].NCR5380_map_name;
12150797 423 hostdata->iomem = iomem;
cecf3bee
OZ
424 switch (overrides[current_override].board) {
425 case BOARD_NCR53C400:
12150797
OZ
426 hostdata->c400_ctl_status = 0x100;
427 hostdata->c400_blk_cnt = 0x101;
428 hostdata->c400_host_buf = 0x104;
cecf3bee
OZ
429 break;
430 case BOARD_NCR53C400A:
431 pr_err(DRV_MODULE_NAME ": unknown register offsets\n");
432 goto out_unregister;
12150797 433 }
1da177e4
LT
434#endif
435
0ad0eff9
FT
436 if (NCR5380_init(instance, flags))
437 goto out_unregister;
1da177e4 438
cecf3bee
OZ
439 switch (overrides[current_override].board) {
440 case BOARD_NCR53C400:
441 case BOARD_NCR53C400A:
12150797 442 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
cecf3bee 443 }
4d8c08c7 444
b6488f97
FT
445 NCR5380_maybe_reset_bus(instance);
446
1da177e4
LT
447 if (overrides[current_override].irq != IRQ_AUTO)
448 instance->irq = overrides[current_override].irq;
449 else
450 instance->irq = NCR5380_probe_irq(instance, 0xffff);
451
22f5f10d
FT
452 /* Compatibility with documented NCR5380 kernel parameters */
453 if (instance->irq == 255)
454 instance->irq = NO_IRQ;
455
456 if (instance->irq != NO_IRQ)
1e641664 457 if (request_irq(instance->irq, generic_NCR5380_intr,
4909cc2b 458 0, "NCR5380", instance)) {
1da177e4 459 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
22f5f10d 460 instance->irq = NO_IRQ;
1da177e4
LT
461 }
462
22f5f10d 463 if (instance->irq == NO_IRQ) {
1da177e4
LT
464 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
465 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
466 }
467
1da177e4
LT
468 ++current_override;
469 ++count;
470 }
471 return count;
0ad0eff9
FT
472
473out_unregister:
474 scsi_unregister(instance);
475out_release:
476#ifndef SCSI_G_NCR5380_MEM
477 release_region(overrides[current_override].NCR5380_map_name, region_size);
478#else
479 iounmap(iomem);
480 release_mem_region(base, NCR5380_region_size);
481#endif
482 return count;
1da177e4
LT
483}
484
1da177e4
LT
485/**
486 * generic_NCR5380_release_resources - free resources
487 * @instance: host adapter to clean up
488 *
489 * Free the generic interface resources from this adapter.
490 *
491 * Locks: none
492 */
493
ed8b9e7f 494static int generic_NCR5380_release_resources(struct Scsi_Host *instance)
1da177e4 495{
22f5f10d 496 if (instance->irq != NO_IRQ)
1e641664 497 free_irq(instance->irq, instance);
1da177e4 498 NCR5380_exit(instance);
702a98c6 499#ifndef SCSI_G_NCR5380_MEM
b01ec348 500 release_region(instance->io_port, instance->n_io_port);
1da177e4 501#else
702a98c6 502 iounmap(((struct NCR5380_hostdata *)instance->hostdata)->iomem);
b01ec348 503 release_mem_region(instance->base, NCR5380_region_size);
1da177e4 504#endif
1da177e4
LT
505 return 0;
506}
507
508#ifdef BIOSPARAM
509/**
510 * generic_NCR5380_biosparam
511 * @disk: disk to compute geometry for
512 * @dev: device identifier for this disk
513 * @ip: sizes to fill in
514 *
515 * Generates a BIOS / DOS compatible H-C-S mapping for the specified
516 * device / size.
517 *
518 * XXX Most SCSI boards use this mapping, I could be incorrect. Someone
519 * using hard disks on a trantor should verify that this mapping
520 * corresponds to that used by the BIOS / ASPI driver by running the linux
521 * fdisk program and matching the H_C_S coordinates to what DOS uses.
522 *
523 * Locks: none
524 */
525
526static int
527generic_NCR5380_biosparam(struct scsi_device *sdev, struct block_device *bdev,
528 sector_t capacity, int *ip)
529{
530 ip[0] = 64;
531 ip[1] = 32;
532 ip[2] = capacity >> 11;
533 return 0;
534}
535#endif
536
4d8c08c7 537#ifdef PSEUDO_DMA
1da177e4
LT
538
539/**
540 * NCR5380_pread - pseudo DMA read
541 * @instance: adapter to read from
542 * @dst: buffer to read into
543 * @len: buffer length
544 *
25985edc 545 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
1da177e4
LT
546 * controller
547 */
548
549static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
550{
54d8fe44 551 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4
LT
552 int blocks = len / 128;
553 int start = 0;
1da177e4 554
12150797
OZ
555 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE | CSR_TRANS_DIR);
556 NCR5380_write(hostdata->c400_blk_cnt, blocks);
1da177e4 557 while (1) {
12150797 558 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
1da177e4 559 break;
12150797 560 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
1da177e4
LT
561 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
562 return -1;
563 }
12150797
OZ
564 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
565 ; /* FIXME - no timeout */
1da177e4 566
702a98c6 567#ifndef SCSI_G_NCR5380_MEM
12150797
OZ
568 insb(instance->io_port + hostdata->c400_host_buf,
569 dst + start, 128);
1da177e4 570#else
702a98c6 571 /* implies SCSI_G_NCR5380_MEM */
54d8fe44
FT
572 memcpy_fromio(dst + start,
573 hostdata->iomem + NCR53C400_host_buffer, 128);
1da177e4
LT
574#endif
575 start += 128;
576 blocks--;
577 }
578
579 if (blocks) {
12150797
OZ
580 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
581 ; /* FIXME - no timeout */
1da177e4 582
702a98c6 583#ifndef SCSI_G_NCR5380_MEM
12150797
OZ
584 insb(instance->io_port + hostdata->c400_host_buf,
585 dst + start, 128);
1da177e4 586#else
702a98c6 587 /* implies SCSI_G_NCR5380_MEM */
54d8fe44
FT
588 memcpy_fromio(dst + start,
589 hostdata->iomem + NCR53C400_host_buffer, 128);
1da177e4
LT
590#endif
591 start += 128;
592 blocks--;
593 }
594
12150797 595 if (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
1da177e4
LT
596 printk("53C400r: no 53C80 gated irq after transfer");
597
598#if 0
599 /*
600 * DON'T DO THIS - THEY NEVER ARRIVE!
601 */
602 printk("53C400r: Waiting for 53C80 registers\n");
12150797 603 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG)
1da177e4
LT
604 ;
605#endif
606 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
607 printk(KERN_ERR "53C400r: no end dma signal\n");
608
1da177e4
LT
609 return 0;
610}
611
612/**
613 * NCR5380_write - pseudo DMA write
614 * @instance: adapter to read from
615 * @dst: buffer to read into
616 * @len: buffer length
617 *
25985edc 618 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
1da177e4
LT
619 * controller
620 */
621
622static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
623{
54d8fe44 624 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4
LT
625 int blocks = len / 128;
626 int start = 0;
1da177e4
LT
627 int i;
628
12150797
OZ
629 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
630 NCR5380_write(hostdata->c400_blk_cnt, blocks);
1da177e4 631 while (1) {
12150797 632 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
1da177e4
LT
633 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
634 return -1;
635 }
636
12150797 637 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
1da177e4 638 break;
12150797 639 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
1da177e4 640 ; // FIXME - timeout
702a98c6 641#ifndef SCSI_G_NCR5380_MEM
12150797
OZ
642 outsb(instance->io_port + hostdata->c400_host_buf,
643 src + start, 128);
1da177e4 644#else
702a98c6 645 /* implies SCSI_G_NCR5380_MEM */
54d8fe44
FT
646 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
647 src + start, 128);
1da177e4
LT
648#endif
649 start += 128;
650 blocks--;
651 }
652 if (blocks) {
12150797 653 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
1da177e4
LT
654 ; // FIXME - no timeout
655
702a98c6 656#ifndef SCSI_G_NCR5380_MEM
12150797
OZ
657 outsb(instance->io_port + hostdata->c400_host_buf,
658 src + start, 128);
1da177e4 659#else
702a98c6 660 /* implies SCSI_G_NCR5380_MEM */
54d8fe44
FT
661 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
662 src + start, 128);
1da177e4
LT
663#endif
664 start += 128;
665 blocks--;
666 }
667
668#if 0
669 printk("53C400w: waiting for registers to be available\n");
12150797 670 THEY NEVER DO ! while (NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG);
1da177e4
LT
671 printk("53C400w: Got em\n");
672#endif
673
674 /* Let's wait for this instead - could be ugly */
675 /* All documentation says to check for this. Maybe my hardware is too
676 * fast. Waiting for it seems to work fine! KLL
677 */
12150797 678 while (!(i = NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
1da177e4
LT
679 ; // FIXME - no timeout
680
681 /*
682 * I know. i is certainly != 0 here but the loop is new. See previous
683 * comment.
684 */
685 if (i) {
686 if (!((i = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_END_DMA_TRANSFER))
687 printk(KERN_ERR "53C400w: No END OF DMA bit - WHOOPS! BASR=%0x\n", i);
688 } else
689 printk(KERN_ERR "53C400w: no 53C80 gated irq after transfer (last block)\n");
690
691#if 0
692 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
693 printk(KERN_ERR "53C400w: no end dma signal\n");
694 }
695#endif
696 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
697 ; // TIMEOUT
698 return 0;
699}
ff3d4578
FT
700
701static int generic_NCR5380_dma_xfer_len(struct scsi_cmnd *cmd)
702{
703 int transfersize = cmd->transfersize;
704
705 /* Limit transfers to 32K, for xx400 & xx406
706 * pseudoDMA that transfers in 128 bytes blocks.
707 */
708 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
709 !(cmd->SCp.this_residual % transfersize))
710 transfersize = 32 * 1024;
711
f0394621
OZ
712 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
713 if (transfersize % 128)
714 transfersize = 0;
715
ff3d4578
FT
716 return transfersize;
717}
718
4d8c08c7 719#endif /* PSEUDO_DMA */
1da177e4
LT
720
721/*
722 * Include the NCR5380 core code that we build our driver around
723 */
724
725#include "NCR5380.c"
726
d0be4a7d 727static struct scsi_host_template driver_template = {
aa2e2cb1 728 .proc_name = DRV_MODULE_NAME,
aa2e2cb1
FT
729 .name = "Generic NCR5380/NCR53C400 SCSI",
730 .detect = generic_NCR5380_detect,
731 .release = generic_NCR5380_release_resources,
732 .info = generic_NCR5380_info,
733 .queuecommand = generic_NCR5380_queue_command,
1da177e4
LT
734 .eh_abort_handler = generic_NCR5380_abort,
735 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
aa2e2cb1
FT
736 .bios_param = NCR5380_BIOSPARAM,
737 .can_queue = 16,
738 .this_id = 7,
739 .sg_tablesize = SG_ALL,
740 .cmd_per_lun = 2,
741 .use_clustering = DISABLE_CLUSTERING,
32b26a10 742 .cmd_size = NCR5380_CMD_SIZE,
0a4e3612 743 .max_sectors = 128,
1da177e4 744};
161c0059 745
1da177e4
LT
746#include "scsi_module.c"
747
748module_param(ncr_irq, int, 0);
749module_param(ncr_dma, int, 0);
750module_param(ncr_addr, int, 0);
751module_param(ncr_5380, int, 0);
752module_param(ncr_53c400, int, 0);
753module_param(ncr_53c400a, int, 0);
754module_param(dtc_3181e, int, 0);
755MODULE_LICENSE("GPL");
756
b026e8ed 757#if !defined(SCSI_G_NCR5380_MEM) && defined(MODULE)
6f039790 758static struct isapnp_device_id id_table[] = {
1da177e4
LT
759 {
760 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
761 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
762 0},
763 {0}
764};
765
766MODULE_DEVICE_TABLE(isapnp, id_table);
702a98c6 767#endif
1da177e4
LT
768
769__setup("ncr5380=", do_NCR5380_setup);
770__setup("ncr53c400=", do_NCR53C400_setup);
771__setup("ncr53c400a=", do_NCR53C400A_setup);
772__setup("dtc3181e=", do_DTC3181E_setup);