blob: 1e8f29f500d51a59480045f3977135dbd7b716ec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define AUTOPROBE_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#ifdef CONFIG_SCSI_GENERIC_NCR53C400
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/blkdev.h>
Finn Thain161c0052016-01-03 16:05:46 +110067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <scsi/scsi_host.h>
69#include "g_NCR5380.h"
70#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/init.h>
72#include <linux/ioport.h>
73#include <linux/isapnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/interrupt.h>
75
Finn Thainc0965e62016-01-03 16:05:05 +110076static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static struct override {
Al Viroc818cb62006-03-24 03:15:37 -080085 NCR5380_map_type NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 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
Tobias Klauser6391a112006-06-08 22:23:48 -070096#define NO_OVERRIDES ARRAY_SIZE(overrides)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Tobias Klauser6391a112006-06-08 22:23:48 -070098#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
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{
Finn Thaind5f7e652016-01-03 16:05:03 +1100114 static int commandline_current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 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
Tobias Klauser6391a112006-06-08 22:23:48 -0700167 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 internal_setup(BOARD_NCR5380, str, ints);
169 return 1;
170}
171
172/**
173 * do_NCR53C400_setup - set up entry point
174 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700175 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
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
Tobias Klauser6391a112006-06-08 22:23:48 -0700185 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 internal_setup(BOARD_NCR53C400, str, ints);
187 return 1;
188}
189
190/**
191 * do_NCR53C400A_setup - set up entry point
192 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700193 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
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
Tobias Klauser6391a112006-06-08 22:23:48 -0700203 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 internal_setup(BOARD_NCR53C400A, str, ints);
205 return 1;
206}
207
208/**
209 * do_DTC3181E_setup - set up entry point
210 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700211 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
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
Tobias Klauser6391a112006-06-08 22:23:48 -0700221 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Locks: none
237 */
238
Finn Thained8b9e72014-11-12 16:11:51 +1100239static int __init generic_NCR5380_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Finn Thaind5f7e652016-01-03 16:05:03 +1100241 static int current_override;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700242 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned int *ports;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700244#ifndef SCSI_G_NCR5380_MEM
245 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned long region_size = 16;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700247#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 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 };
Finn Thain4d8c08c2016-01-03 16:05:09 +1100254 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct Scsi_Host *instance;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700256#ifdef SCSI_G_NCR5380_MEM
Al Viroc818cb62006-03-24 03:15:37 -0800257 unsigned long base;
258 void __iomem *iomem;
259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Finn Thainc0965e62016-01-03 16:05:05 +1100261 if (ncr_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 overrides[0].irq = ncr_irq;
Finn Thainc0965e62016-01-03 16:05:05 +1100263 if (ncr_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 overrides[0].dma = ncr_dma;
Finn Thainc0965e62016-01-03 16:05:05 +1100265 if (ncr_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 overrides[0].NCR5380_map_name = (NCR5380_map_type) ncr_addr;
Finn Thainc0965e62016-01-03 16:05:05 +1100267 if (ncr_5380)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 overrides[0].board = BOARD_NCR5380;
Finn Thainc0965e62016-01-03 16:05:05 +1100269 else if (ncr_53c400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 overrides[0].board = BOARD_NCR53C400;
Finn Thainc0965e62016-01-03 16:05:05 +1100271 else if (ncr_53c400a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 overrides[0].board = BOARD_NCR53C400A;
Finn Thainc0965e62016-01-03 16:05:05 +1100273 else if (dtc_3181e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 overrides[0].board = BOARD_DTC3181E;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700275#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!current_override && isapnp_present()) {
277 struct pnp_dev *dev = NULL;
278 count = 0;
279 while ((dev = pnp_find_dev(NULL, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev))) {
280 if (count >= NO_OVERRIDES)
281 break;
Ondrej Zaryc94babb2010-08-10 18:01:15 -0700282 if (pnp_device_attach(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (pnp_activate_dev(dev) < 0) {
285 printk(KERN_ERR "dtc436e probe: activate failed\n");
286 pnp_device_detach(dev);
287 continue;
288 }
289 if (!pnp_port_valid(dev, 0)) {
290 printk(KERN_ERR "dtc436e probe: no valid port\n");
291 pnp_device_detach(dev);
292 continue;
293 }
294 if (pnp_irq_valid(dev, 0))
295 overrides[count].irq = pnp_irq(dev, 0);
296 else
Finn Thain22f5f102014-11-12 16:11:56 +1100297 overrides[count].irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (pnp_dma_valid(dev, 0))
299 overrides[count].dma = pnp_dma(dev, 0);
300 else
301 overrides[count].dma = DMA_NONE;
302 overrides[count].NCR5380_map_name = (NCR5380_map_type) pnp_port_start(dev, 0);
303 overrides[count].board = BOARD_DTC3181E;
304 count++;
305 }
306 }
Ondrej Zary702a98c2010-08-10 18:01:16 -0700307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 tpnt->proc_name = "g_NCR5380";
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;
Finn Thain4d8c08c2016-01-03 16:05:09 +1100315 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 switch (overrides[current_override].board) {
317 case BOARD_NCR5380:
318 flags = FLAG_NO_PSEUDO_DMA;
319 break;
320 case BOARD_NCR53C400:
Finn Thain4d8c08c2016-01-03 16:05:09 +1100321#ifdef PSEUDO_DMA
Finn Thain55181be2016-01-03 16:05:42 +1100322 flags = FLAG_NO_DMA_FIXUP;
Finn Thain4d8c08c2016-01-03 16:05:09 +1100323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 case BOARD_NCR53C400A:
326 flags = FLAG_NO_PSEUDO_DMA;
327 ports = ncr_53c400a_ports;
328 break;
329 case BOARD_DTC3181E:
330 flags = FLAG_NO_PSEUDO_DMA | FLAG_DTC3181E;
331 ports = dtc_3181e_ports;
332 break;
333 }
334
Ondrej Zary702a98c2010-08-10 18:01:16 -0700335#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 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
Al Viroc818cb62006-03-24 03:15:37 -0800386 base = overrides[current_override].NCR5380_map_name;
387 if (!request_mem_region(base, NCR5380_region_size, "ncr5380"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 continue;
Al Viroc818cb62006-03-24 03:15:37 -0800389 iomem = ioremap(base, NCR5380_region_size);
390 if (!iomem) {
391 release_mem_region(base, NCR5380_region_size);
392 continue;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#endif
395 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
Finn Thain0ad0eff2016-01-03 16:05:21 +1100396 if (instance == NULL)
397 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ondrej Zary702a98c2010-08-10 18:01:16 -0700399#ifndef SCSI_G_NCR5380_MEM
Finn Thainb01ec342016-01-03 16:05:07 +1100400 instance->io_port = overrides[current_override].NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 instance->n_io_port = region_size;
Finn Thain4d8c08c2016-01-03 16:05:09 +1100402
403 /*
404 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
405 * the base address.
406 */
407 if (overrides[current_override].board == BOARD_NCR53C400)
408 instance->io_port += 8;
Al Viroc818cb62006-03-24 03:15:37 -0800409#else
Finn Thainb01ec342016-01-03 16:05:07 +1100410 instance->base = overrides[current_override].NCR5380_map_name;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700411 ((struct NCR5380_hostdata *)instance->hostdata)->iomem = iomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#endif
413
Finn Thain0ad0eff2016-01-03 16:05:21 +1100414 if (NCR5380_init(instance, flags))
415 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Finn Thain4d8c08c2016-01-03 16:05:09 +1100417 if (overrides[current_override].board == BOARD_NCR53C400)
418 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
419
Finn Thainb6488f92016-01-03 16:05:08 +1100420 NCR5380_maybe_reset_bus(instance);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (overrides[current_override].irq != IRQ_AUTO)
423 instance->irq = overrides[current_override].irq;
424 else
425 instance->irq = NCR5380_probe_irq(instance, 0xffff);
426
Finn Thain22f5f102014-11-12 16:11:56 +1100427 /* Compatibility with documented NCR5380 kernel parameters */
428 if (instance->irq == 255)
429 instance->irq = NO_IRQ;
430
431 if (instance->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500432 if (request_irq(instance->irq, generic_NCR5380_intr,
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100433 0, "NCR5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
Finn Thain22f5f102014-11-12 16:11:56 +1100435 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
Finn Thain22f5f102014-11-12 16:11:56 +1100438 if (instance->irq == NO_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
440 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
441 }
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 ++current_override;
444 ++count;
445 }
446 return count;
Finn Thain0ad0eff2016-01-03 16:05:21 +1100447
448out_unregister:
449 scsi_unregister(instance);
450out_release:
451#ifndef SCSI_G_NCR5380_MEM
452 release_region(overrides[current_override].NCR5380_map_name, region_size);
453#else
454 iounmap(iomem);
455 release_mem_region(base, NCR5380_region_size);
456#endif
457 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * generic_NCR5380_release_resources - free resources
462 * @instance: host adapter to clean up
463 *
464 * Free the generic interface resources from this adapter.
465 *
466 * Locks: none
467 */
468
Finn Thained8b9e72014-11-12 16:11:51 +1100469static int generic_NCR5380_release_resources(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Finn Thain22f5f102014-11-12 16:11:56 +1100471 if (instance->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500472 free_irq(instance->irq, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 NCR5380_exit(instance);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700474#ifndef SCSI_G_NCR5380_MEM
Finn Thainb01ec342016-01-03 16:05:07 +1100475 release_region(instance->io_port, instance->n_io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700477 iounmap(((struct NCR5380_hostdata *)instance->hostdata)->iomem);
Finn Thainb01ec342016-01-03 16:05:07 +1100478 release_mem_region(instance->base, NCR5380_region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
481}
482
483#ifdef BIOSPARAM
484/**
485 * generic_NCR5380_biosparam
486 * @disk: disk to compute geometry for
487 * @dev: device identifier for this disk
488 * @ip: sizes to fill in
489 *
490 * Generates a BIOS / DOS compatible H-C-S mapping for the specified
491 * device / size.
492 *
493 * XXX Most SCSI boards use this mapping, I could be incorrect. Someone
494 * using hard disks on a trantor should verify that this mapping
495 * corresponds to that used by the BIOS / ASPI driver by running the linux
496 * fdisk program and matching the H_C_S coordinates to what DOS uses.
497 *
498 * Locks: none
499 */
500
501static int
502generic_NCR5380_biosparam(struct scsi_device *sdev, struct block_device *bdev,
503 sector_t capacity, int *ip)
504{
505 ip[0] = 64;
506 ip[1] = 32;
507 ip[2] = capacity >> 11;
508 return 0;
509}
510#endif
511
Finn Thain4d8c08c2016-01-03 16:05:09 +1100512#ifdef PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514/**
515 * NCR5380_pread - pseudo DMA read
516 * @instance: adapter to read from
517 * @dst: buffer to read into
518 * @len: buffer length
519 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300520 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * controller
522 */
523
524static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
525{
Finn Thain54d8fe42016-01-03 16:05:06 +1100526#ifdef SCSI_G_NCR5380_MEM
527 struct NCR5380_hostdata *hostdata = shost_priv(instance);
528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int blocks = len / 128;
530 int start = 0;
531 int bl;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE | CSR_TRANS_DIR);
534 NCR5380_write(C400_BLOCK_COUNTER_REG, blocks);
535 while (1) {
536 if ((bl = NCR5380_read(C400_BLOCK_COUNTER_REG)) == 0) {
537 break;
538 }
539 if (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ) {
540 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
541 return -1;
542 }
543 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY);
544
Ondrej Zary702a98c2010-08-10 18:01:16 -0700545#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 {
547 int i;
548 for (i = 0; i < 128; i++)
549 dst[start + i] = NCR5380_read(C400_HOST_BUFFER);
550 }
551#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700552 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100553 memcpy_fromio(dst + start,
554 hostdata->iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#endif
556 start += 128;
557 blocks--;
558 }
559
560 if (blocks) {
561 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
562 {
563 // FIXME - no timeout
564 }
565
Ondrej Zary702a98c2010-08-10 18:01:16 -0700566#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 {
568 int i;
569 for (i = 0; i < 128; i++)
570 dst[start + i] = NCR5380_read(C400_HOST_BUFFER);
571 }
572#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700573 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100574 memcpy_fromio(dst + start,
575 hostdata->iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576#endif
577 start += 128;
578 blocks--;
579 }
580
581 if (!(NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ))
582 printk("53C400r: no 53C80 gated irq after transfer");
583
584#if 0
585 /*
586 * DON'T DO THIS - THEY NEVER ARRIVE!
587 */
588 printk("53C400r: Waiting for 53C80 registers\n");
589 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_53C80_REG)
590 ;
591#endif
592 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
593 printk(KERN_ERR "53C400r: no end dma signal\n");
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
596}
597
598/**
599 * NCR5380_write - pseudo DMA write
600 * @instance: adapter to read from
601 * @dst: buffer to read into
602 * @len: buffer length
603 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * controller
606 */
607
608static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
609{
Finn Thain54d8fe42016-01-03 16:05:06 +1100610#ifdef SCSI_G_NCR5380_MEM
611 struct NCR5380_hostdata *hostdata = shost_priv(instance);
612#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int blocks = len / 128;
614 int start = 0;
615 int bl;
616 int i;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
619 NCR5380_write(C400_BLOCK_COUNTER_REG, blocks);
620 while (1) {
621 if (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ) {
622 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
623 return -1;
624 }
625
626 if ((bl = NCR5380_read(C400_BLOCK_COUNTER_REG)) == 0) {
627 break;
628 }
629 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
630 ; // FIXME - timeout
Ondrej Zary702a98c2010-08-10 18:01:16 -0700631#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 {
633 for (i = 0; i < 128; i++)
634 NCR5380_write(C400_HOST_BUFFER, src[start + i]);
635 }
636#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700637 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100638 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
639 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#endif
641 start += 128;
642 blocks--;
643 }
644 if (blocks) {
645 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
646 ; // FIXME - no timeout
647
Ondrej Zary702a98c2010-08-10 18:01:16 -0700648#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 {
650 for (i = 0; i < 128; i++)
651 NCR5380_write(C400_HOST_BUFFER, src[start + i]);
652 }
653#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700654 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100655 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
656 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#endif
658 start += 128;
659 blocks--;
660 }
661
662#if 0
663 printk("53C400w: waiting for registers to be available\n");
664 THEY NEVER DO ! while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_53C80_REG);
665 printk("53C400w: Got em\n");
666#endif
667
668 /* Let's wait for this instead - could be ugly */
669 /* All documentation says to check for this. Maybe my hardware is too
670 * fast. Waiting for it seems to work fine! KLL
671 */
672 while (!(i = NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ))
673 ; // FIXME - no timeout
674
675 /*
676 * I know. i is certainly != 0 here but the loop is new. See previous
677 * comment.
678 */
679 if (i) {
680 if (!((i = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_END_DMA_TRANSFER))
681 printk(KERN_ERR "53C400w: No END OF DMA bit - WHOOPS! BASR=%0x\n", i);
682 } else
683 printk(KERN_ERR "53C400w: no 53C80 gated irq after transfer (last block)\n");
684
685#if 0
686 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
687 printk(KERN_ERR "53C400w: no end dma signal\n");
688 }
689#endif
690 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
691 ; // TIMEOUT
692 return 0;
693}
Finn Thainff3d4572016-01-03 16:05:25 +1100694
695static int generic_NCR5380_dma_xfer_len(struct scsi_cmnd *cmd)
696{
697 int transfersize = cmd->transfersize;
698
699 /* Limit transfers to 32K, for xx400 & xx406
700 * pseudoDMA that transfers in 128 bytes blocks.
701 */
702 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
703 !(cmd->SCp.this_residual % transfersize))
704 transfersize = 32 * 1024;
705
706 return transfersize;
707}
708
Finn Thain4d8c08c2016-01-03 16:05:09 +1100709#endif /* PSEUDO_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711/*
712 * Include the NCR5380 core code that we build our driver around
713 */
714
715#include "NCR5380.c"
716
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100717static struct scsi_host_template driver_template = {
Al Virodd7ab712013-03-31 01:15:54 -0400718 .show_info = generic_NCR5380_show_info,
Finn Thain8c325132014-11-12 16:11:58 +1100719 .name = "Generic NCR5380/NCR53C400 SCSI",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 .detect = generic_NCR5380_detect,
721 .release = generic_NCR5380_release_resources,
722 .info = generic_NCR5380_info,
723 .queuecommand = generic_NCR5380_queue_command,
724 .eh_abort_handler = generic_NCR5380_abort,
725 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 .bios_param = NCR5380_BIOSPARAM,
727 .can_queue = CAN_QUEUE,
728 .this_id = 7,
729 .sg_tablesize = SG_ALL,
730 .cmd_per_lun = CMD_PER_LUN,
731 .use_clustering = DISABLE_CLUSTERING,
732};
Finn Thain161c0052016-01-03 16:05:46 +1100733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734#include "scsi_module.c"
735
736module_param(ncr_irq, int, 0);
737module_param(ncr_dma, int, 0);
738module_param(ncr_addr, int, 0);
739module_param(ncr_5380, int, 0);
740module_param(ncr_53c400, int, 0);
741module_param(ncr_53c400a, int, 0);
742module_param(dtc_3181e, int, 0);
743MODULE_LICENSE("GPL");
744
Geert Uytterhoevenb026e8e2015-01-03 23:00:16 +0100745#if !defined(SCSI_G_NCR5380_MEM) && defined(MODULE)
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800746static struct isapnp_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 {
748 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
749 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
750 0},
751 {0}
752};
753
754MODULE_DEVICE_TABLE(isapnp, id_table);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700755#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757__setup("ncr5380=", do_NCR5380_setup);
758__setup("ncr53c400=", do_NCR53C400_setup);
759__setup("ncr53c400a=", do_NCR53C400A_setup);
760__setup("dtc3181e=", do_DTC3181E_setup);