blob: 18f4a4f99336444dc27cd160f48e970943b95b21 [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#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/blkdev.h>
Finn Thain161c0052016-01-03 16:05:46 +110061#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <scsi/scsi_host.h>
63#include "g_NCR5380.h"
64#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/init.h>
66#include <linux/ioport.h>
67#include <linux/isapnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/interrupt.h>
69
Finn Thainc0965e62016-01-03 16:05:05 +110070static int ncr_irq;
71static int ncr_dma;
72static int ncr_addr;
73static int ncr_5380;
74static int ncr_53c400;
75static int ncr_53c400a;
76static int dtc_3181e;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +110077static int hp_c2502;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static struct override {
Al Viroc818cb62006-03-24 03:15:37 -080080 NCR5380_map_type NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int irq;
82 int dma;
83 int board; /* Use NCR53c400, Ricoh, etc. extensions ? */
84} overrides
85#ifdef GENERIC_NCR5380_OVERRIDE
86[] __initdata = GENERIC_NCR5380_OVERRIDE;
87#else
88[1] __initdata = { { 0,},};
89#endif
90
Tobias Klauser6391a112006-06-08 22:23:48 -070091#define NO_OVERRIDES ARRAY_SIZE(overrides)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Tobias Klauser6391a112006-06-08 22:23:48 -070093#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/**
96 * internal_setup - handle lilo command string override
97 * @board: BOARD_* identifier for the board
98 * @str: unused
99 * @ints: numeric parameters
100 *
101 * Do LILO command line initialization of the overrides array. Display
102 * errors when needed
103 *
104 * Locks: none
105 */
106
107static void __init internal_setup(int board, char *str, int *ints)
108{
Finn Thaind5f7e652016-01-03 16:05:03 +1100109 static int commandline_current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 switch (board) {
111 case BOARD_NCR5380:
112 if (ints[0] != 2 && ints[0] != 3) {
113 printk(KERN_ERR "generic_NCR5380_setup : usage ncr5380=" STRVAL(NCR5380_map_name) ",irq,dma\n");
114 return;
115 }
116 break;
117 case BOARD_NCR53C400:
118 if (ints[0] != 2) {
119 printk(KERN_ERR "generic_NCR53C400_setup : usage ncr53c400=" STRVAL(NCR5380_map_name) ",irq\n");
120 return;
121 }
122 break;
123 case BOARD_NCR53C400A:
124 if (ints[0] != 2) {
125 printk(KERN_ERR "generic_NCR53C400A_setup : usage ncr53c400a=" STRVAL(NCR5380_map_name) ",irq\n");
126 return;
127 }
128 break;
129 case BOARD_DTC3181E:
130 if (ints[0] != 2) {
131 printk("generic_DTC3181E_setup : usage dtc3181e=" STRVAL(NCR5380_map_name) ",irq\n");
132 return;
133 }
134 break;
135 }
136
137 if (commandline_current < NO_OVERRIDES) {
138 overrides[commandline_current].NCR5380_map_name = (NCR5380_map_type) ints[1];
139 overrides[commandline_current].irq = ints[2];
140 if (ints[0] == 3)
141 overrides[commandline_current].dma = ints[3];
142 else
143 overrides[commandline_current].dma = DMA_NONE;
144 overrides[commandline_current].board = board;
145 ++commandline_current;
146 }
147}
148
149
150/**
151 * do_NCR53C80_setup - set up entry point
152 * @str: unused
153 *
154 * Setup function invoked at boot to parse the ncr5380= command
155 * line.
156 */
157
158static int __init do_NCR5380_setup(char *str)
159{
160 int ints[10];
161
Tobias Klauser6391a112006-06-08 22:23:48 -0700162 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 internal_setup(BOARD_NCR5380, str, ints);
164 return 1;
165}
166
167/**
168 * do_NCR53C400_setup - set up entry point
169 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700170 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * Setup function invoked at boot to parse the ncr53c400= command
173 * line.
174 */
175
176static int __init do_NCR53C400_setup(char *str)
177{
178 int ints[10];
179
Tobias Klauser6391a112006-06-08 22:23:48 -0700180 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 internal_setup(BOARD_NCR53C400, str, ints);
182 return 1;
183}
184
185/**
186 * do_NCR53C400A_setup - set up entry point
187 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700188 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
190 * Setup function invoked at boot to parse the ncr53c400a= command
191 * line.
192 */
193
194static int __init do_NCR53C400A_setup(char *str)
195{
196 int ints[10];
197
Tobias Klauser6391a112006-06-08 22:23:48 -0700198 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 internal_setup(BOARD_NCR53C400A, str, ints);
200 return 1;
201}
202
203/**
204 * do_DTC3181E_setup - set up entry point
205 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700206 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
208 * Setup function invoked at boot to parse the dtc3181e= command
209 * line.
210 */
211
212static int __init do_DTC3181E_setup(char *str)
213{
214 int ints[10];
215
Tobias Klauser6391a112006-06-08 22:23:48 -0700216 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 internal_setup(BOARD_DTC3181E, str, ints);
218 return 1;
219}
220
221#endif
222
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100223#ifndef SCSI_G_NCR5380_MEM
224/*
225 * Configure I/O address of 53C400A or DTC436 by writing magic numbers
226 * to ports 0x779 and 0x379.
227 */
228static void magic_configure(int idx, u8 irq, u8 magic[])
229{
230 u8 cfg = 0;
231
232 outb(magic[0], 0x779);
233 outb(magic[1], 0x379);
234 outb(magic[2], 0x379);
235 outb(magic[3], 0x379);
236 outb(magic[4], 0x379);
237
238 /* allowed IRQs for HP C2502 */
239 if (irq != 2 && irq != 3 && irq != 4 && irq != 5 && irq != 7)
240 irq = 0;
241 if (idx >= 0 && idx <= 7)
242 cfg = 0x80 | idx | (irq << 4);
243 outb(cfg, 0x379);
244}
245#endif
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/**
248 * generic_NCR5380_detect - look for NCR5380 controllers
249 * @tpnt: the scsi template
250 *
251 * Scan for the present of NCR5380, NCR53C400, NCR53C400A, DTC3181E
252 * and DTC436(ISAPnP) controllers. If overrides have been set we use
253 * them.
254 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * Locks: none
256 */
257
Finn Thained8b9e72014-11-12 16:11:51 +1100258static int __init generic_NCR5380_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Finn Thaind5f7e652016-01-03 16:05:03 +1100260 static int current_override;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700261 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int *ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100263 u8 *magic = NULL;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700264#ifndef SCSI_G_NCR5380_MEM
265 int i;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100266 int port_idx = -1;
Finn Thain9d376402016-03-23 21:10:10 +1100267 unsigned long region_size;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 static unsigned int __initdata ncr_53c400a_ports[] = {
270 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
271 };
272 static unsigned int __initdata dtc_3181e_ports[] = {
273 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
274 };
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100275 static u8 ncr_53c400a_magic[] __initdata = { /* 53C400A & DTC436 */
276 0x59, 0xb9, 0xc5, 0xae, 0xa6
277 };
278 static u8 hp_c2502_magic[] __initdata = { /* HP C2502 */
279 0x0f, 0x22, 0xf0, 0x20, 0x80
280 };
Finn Thain4d8c08c2016-01-03 16:05:09 +1100281 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct Scsi_Host *instance;
Ondrej Zary12150792016-01-03 16:06:15 +1100283 struct NCR5380_hostdata *hostdata;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700284#ifdef SCSI_G_NCR5380_MEM
Al Viroc818cb62006-03-24 03:15:37 -0800285 unsigned long base;
286 void __iomem *iomem;
Finn Thain9d376402016-03-23 21:10:10 +1100287 resource_size_t iomem_size;
Al Viroc818cb62006-03-24 03:15:37 -0800288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Finn Thainc0965e62016-01-03 16:05:05 +1100290 if (ncr_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 overrides[0].irq = ncr_irq;
Finn Thainc0965e62016-01-03 16:05:05 +1100292 if (ncr_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 overrides[0].dma = ncr_dma;
Finn Thainc0965e62016-01-03 16:05:05 +1100294 if (ncr_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 overrides[0].NCR5380_map_name = (NCR5380_map_type) ncr_addr;
Finn Thainc0965e62016-01-03 16:05:05 +1100296 if (ncr_5380)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 overrides[0].board = BOARD_NCR5380;
Finn Thainc0965e62016-01-03 16:05:05 +1100298 else if (ncr_53c400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 overrides[0].board = BOARD_NCR53C400;
Finn Thainc0965e62016-01-03 16:05:05 +1100300 else if (ncr_53c400a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 overrides[0].board = BOARD_NCR53C400A;
Finn Thainc0965e62016-01-03 16:05:05 +1100302 else if (dtc_3181e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 overrides[0].board = BOARD_DTC3181E;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100304 else if (hp_c2502)
305 overrides[0].board = BOARD_HP_C2502;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700306#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!current_override && isapnp_present()) {
308 struct pnp_dev *dev = NULL;
309 count = 0;
310 while ((dev = pnp_find_dev(NULL, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev))) {
311 if (count >= NO_OVERRIDES)
312 break;
Ondrej Zaryc94babb2010-08-10 18:01:15 -0700313 if (pnp_device_attach(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (pnp_activate_dev(dev) < 0) {
316 printk(KERN_ERR "dtc436e probe: activate failed\n");
317 pnp_device_detach(dev);
318 continue;
319 }
320 if (!pnp_port_valid(dev, 0)) {
321 printk(KERN_ERR "dtc436e probe: no valid port\n");
322 pnp_device_detach(dev);
323 continue;
324 }
325 if (pnp_irq_valid(dev, 0))
326 overrides[count].irq = pnp_irq(dev, 0);
327 else
Finn Thain22f5f102014-11-12 16:11:56 +1100328 overrides[count].irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (pnp_dma_valid(dev, 0))
330 overrides[count].dma = pnp_dma(dev, 0);
331 else
332 overrides[count].dma = DMA_NONE;
333 overrides[count].NCR5380_map_name = (NCR5380_map_type) pnp_port_start(dev, 0);
334 overrides[count].board = BOARD_DTC3181E;
335 count++;
336 }
337 }
Ondrej Zary702a98c2010-08-10 18:01:16 -0700338#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
341 if (!(overrides[current_override].NCR5380_map_name))
342 continue;
343
344 ports = NULL;
Finn Thain4d8c08c2016-01-03 16:05:09 +1100345 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 switch (overrides[current_override].board) {
347 case BOARD_NCR5380:
Finn Thain1bb46002016-03-23 21:10:14 +1100348 flags = FLAG_NO_PSEUDO_DMA | FLAG_DMA_FIXUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 case BOARD_NCR53C400A:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ports = ncr_53c400a_ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100352 magic = ncr_53c400a_magic;
353 break;
354 case BOARD_HP_C2502:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100355 ports = ncr_53c400a_ports;
356 magic = hp_c2502_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 case BOARD_DTC3181E:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ports = dtc_3181e_ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100360 magic = ncr_53c400a_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 break;
362 }
363
Ondrej Zary702a98c2010-08-10 18:01:16 -0700364#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100365 if (ports && magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* wakeup sequence for the NCR53C400A and DTC3181E */
367
368 /* Disable the adapter and look for a free io port */
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100369 magic_configure(-1, 0, magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Finn Thain9d376402016-03-23 21:10:10 +1100371 region_size = 16;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (overrides[current_override].NCR5380_map_name != PORT_AUTO)
374 for (i = 0; ports[i]; i++) {
Finn Thain9d376402016-03-23 21:10:10 +1100375 if (!request_region(ports[i], region_size, "ncr53c80"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 continue;
377 if (overrides[current_override].NCR5380_map_name == ports[i])
378 break;
Finn Thain9d376402016-03-23 21:10:10 +1100379 release_region(ports[i], region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 } else
381 for (i = 0; ports[i]; i++) {
Finn Thain9d376402016-03-23 21:10:10 +1100382 if (!request_region(ports[i], region_size, "ncr53c80"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 continue;
384 if (inb(ports[i]) == 0xff)
385 break;
Finn Thain9d376402016-03-23 21:10:10 +1100386 release_region(ports[i], region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 if (ports[i]) {
389 /* At this point we have our region reserved */
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100390 magic_configure(i, 0, magic); /* no IRQ yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 outb(0xc0, ports[i] + 9);
392 if (inb(ports[i] + 9) != 0x80)
393 continue;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100394 overrides[current_override].NCR5380_map_name = ports[i];
395 port_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else
397 continue;
398 }
399 else
400 {
401 /* Not a 53C400A style setup - just grab */
Finn Thain9d376402016-03-23 21:10:10 +1100402 region_size = 8;
403 if (!request_region(overrides[current_override].NCR5380_map_name,
404 region_size, "ncr5380"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407#else
Al Viroc818cb62006-03-24 03:15:37 -0800408 base = overrides[current_override].NCR5380_map_name;
Finn Thain9d376402016-03-23 21:10:10 +1100409 iomem_size = NCR53C400_region_size;
410 if (!request_mem_region(base, iomem_size, "ncr5380"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 continue;
Finn Thain9d376402016-03-23 21:10:10 +1100412 iomem = ioremap(base, iomem_size);
Al Viroc818cb62006-03-24 03:15:37 -0800413 if (!iomem) {
Finn Thain9d376402016-03-23 21:10:10 +1100414 release_mem_region(base, iomem_size);
Al Viroc818cb62006-03-24 03:15:37 -0800415 continue;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#endif
418 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
Finn Thain0ad0eff2016-01-03 16:05:21 +1100419 if (instance == NULL)
420 goto out_release;
Ondrej Zary12150792016-01-03 16:06:15 +1100421 hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Ondrej Zary702a98c2010-08-10 18:01:16 -0700423#ifndef SCSI_G_NCR5380_MEM
Finn Thainb01ec342016-01-03 16:05:07 +1100424 instance->io_port = overrides[current_override].NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 instance->n_io_port = region_size;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100426 hostdata->io_width = 1; /* 8-bit PDMA by default */
Finn Thain4d8c08c2016-01-03 16:05:09 +1100427
428 /*
429 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
430 * the base address.
431 */
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100432 switch (overrides[current_override].board) {
433 case BOARD_NCR53C400:
Finn Thain4d8c08c2016-01-03 16:05:09 +1100434 instance->io_port += 8;
Ondrej Zary12150792016-01-03 16:06:15 +1100435 hostdata->c400_ctl_status = 0;
436 hostdata->c400_blk_cnt = 1;
437 hostdata->c400_host_buf = 4;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100438 break;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100439 case BOARD_DTC3181E:
440 hostdata->io_width = 2; /* 16-bit PDMA */
441 /* fall through */
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100442 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100443 case BOARD_HP_C2502:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100444 hostdata->c400_ctl_status = 9;
445 hostdata->c400_blk_cnt = 10;
446 hostdata->c400_host_buf = 8;
447 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100448 }
Al Viroc818cb62006-03-24 03:15:37 -0800449#else
Finn Thainb01ec342016-01-03 16:05:07 +1100450 instance->base = overrides[current_override].NCR5380_map_name;
Ondrej Zary12150792016-01-03 16:06:15 +1100451 hostdata->iomem = iomem;
Finn Thain9d376402016-03-23 21:10:10 +1100452 hostdata->iomem_size = iomem_size;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100453 switch (overrides[current_override].board) {
454 case BOARD_NCR53C400:
Ondrej Zary12150792016-01-03 16:06:15 +1100455 hostdata->c400_ctl_status = 0x100;
456 hostdata->c400_blk_cnt = 0x101;
457 hostdata->c400_host_buf = 0x104;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100458 break;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100459 case BOARD_DTC3181E:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100460 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100461 case BOARD_HP_C2502:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100462 pr_err(DRV_MODULE_NAME ": unknown register offsets\n");
463 goto out_unregister;
Ondrej Zary12150792016-01-03 16:06:15 +1100464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#endif
466
Finn Thain8053b0e2016-03-23 21:10:19 +1100467 if (NCR5380_init(instance, flags | FLAG_LATE_DMA_SETUP))
Finn Thain0ad0eff2016-01-03 16:05:21 +1100468 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100470 switch (overrides[current_override].board) {
471 case BOARD_NCR53C400:
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100472 case BOARD_DTC3181E:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100473 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100474 case BOARD_HP_C2502:
Ondrej Zary12150792016-01-03 16:06:15 +1100475 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100476 }
Finn Thain4d8c08c2016-01-03 16:05:09 +1100477
Finn Thainb6488f92016-01-03 16:05:08 +1100478 NCR5380_maybe_reset_bus(instance);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (overrides[current_override].irq != IRQ_AUTO)
481 instance->irq = overrides[current_override].irq;
482 else
483 instance->irq = NCR5380_probe_irq(instance, 0xffff);
484
Finn Thain22f5f102014-11-12 16:11:56 +1100485 /* Compatibility with documented NCR5380 kernel parameters */
486 if (instance->irq == 255)
487 instance->irq = NO_IRQ;
488
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100489 if (instance->irq != NO_IRQ) {
490#ifndef SCSI_G_NCR5380_MEM
491 /* set IRQ for HP C2502 */
492 if (overrides[current_override].board == BOARD_HP_C2502)
493 magic_configure(port_idx, instance->irq, magic);
494#endif
Jeff Garzik1e641662007-11-11 19:52:05 -0500495 if (request_irq(instance->irq, generic_NCR5380_intr,
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100496 0, "NCR5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
Finn Thain22f5f102014-11-12 16:11:56 +1100498 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Finn Thain22f5f102014-11-12 16:11:56 +1100502 if (instance->irq == NO_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
504 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 ++current_override;
508 ++count;
509 }
510 return count;
Finn Thain0ad0eff2016-01-03 16:05:21 +1100511
512out_unregister:
513 scsi_unregister(instance);
514out_release:
515#ifndef SCSI_G_NCR5380_MEM
516 release_region(overrides[current_override].NCR5380_map_name, region_size);
517#else
518 iounmap(iomem);
Finn Thain9d376402016-03-23 21:10:10 +1100519 release_mem_region(base, iomem_size);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100520#endif
521 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * generic_NCR5380_release_resources - free resources
526 * @instance: host adapter to clean up
527 *
528 * Free the generic interface resources from this adapter.
529 *
530 * Locks: none
531 */
532
Finn Thained8b9e72014-11-12 16:11:51 +1100533static int generic_NCR5380_release_resources(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Finn Thain22f5f102014-11-12 16:11:56 +1100535 if (instance->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500536 free_irq(instance->irq, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 NCR5380_exit(instance);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700538#ifndef SCSI_G_NCR5380_MEM
Finn Thainb01ec342016-01-03 16:05:07 +1100539 release_region(instance->io_port, instance->n_io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#else
Finn Thain9d376402016-03-23 21:10:10 +1100541 {
542 struct NCR5380_hostdata *hostdata = shost_priv(instance);
543
544 iounmap(hostdata->iomem);
545 release_mem_region(instance->base, hostdata->iomem_size);
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
Finn Thain6c4b88c2016-03-23 21:10:17 +1100552 * generic_NCR5380_pread - pseudo DMA read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * @instance: adapter to read from
554 * @dst: buffer to read into
555 * @len: buffer length
556 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300557 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * controller
559 */
560
Finn Thain6c4b88c2016-03-23 21:10:17 +1100561static inline int generic_NCR5380_pread(struct Scsi_Host *instance,
562 unsigned char *dst, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Finn Thain54d8fe42016-01-03 16:05:06 +1100564 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int blocks = len / 128;
566 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Ondrej Zary12150792016-01-03 16:06:15 +1100568 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE | CSR_TRANS_DIR);
569 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100571 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100573 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
575 return -1;
576 }
Ondrej Zary12150792016-01-03 16:06:15 +1100577 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
578 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ondrej Zary702a98c2010-08-10 18:01:16 -0700580#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100581 if (hostdata->io_width == 2)
582 insw(instance->io_port + hostdata->c400_host_buf,
583 dst + start, 64);
584 else
585 insb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100586 dst + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700588 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100589 memcpy_fromio(dst + start,
590 hostdata->iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#endif
592 start += 128;
593 blocks--;
594 }
595
596 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100597 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
598 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Ondrej Zary702a98c2010-08-10 18:01:16 -0700600#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100601 if (hostdata->io_width == 2)
602 insw(instance->io_port + hostdata->c400_host_buf,
603 dst + start, 64);
604 else
605 insb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100606 dst + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700608 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100609 memcpy_fromio(dst + start,
610 hostdata->iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#endif
612 start += 128;
613 blocks--;
614 }
615
Ondrej Zary12150792016-01-03 16:06:15 +1100616 if (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 printk("53C400r: no 53C80 gated irq after transfer");
618
Ondrej Zary42fc6372016-01-03 16:06:18 +1100619 /* wait for 53C80 registers to be available */
620 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 ;
Ondrej Zary42fc6372016-01-03 16:06:18 +1100622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
624 printk(KERN_ERR "53C400r: no end dma signal\n");
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627}
628
629/**
Finn Thain6c4b88c2016-03-23 21:10:17 +1100630 * generic_NCR5380_pwrite - pseudo DMA write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * @instance: adapter to read from
632 * @dst: buffer to read into
633 * @len: buffer length
634 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300635 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * controller
637 */
638
Finn Thain6c4b88c2016-03-23 21:10:17 +1100639static inline int generic_NCR5380_pwrite(struct Scsi_Host *instance,
640 unsigned char *src, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Finn Thain54d8fe42016-01-03 16:05:06 +1100642 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int blocks = len / 128;
644 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Ondrej Zary12150792016-01-03 16:06:15 +1100646 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
647 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100649 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
651 return -1;
652 }
653
Ondrej Zary12150792016-01-03 16:06:15 +1100654 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100656 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ; // FIXME - timeout
Ondrej Zary702a98c2010-08-10 18:01:16 -0700658#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100659 if (hostdata->io_width == 2)
660 outsw(instance->io_port + hostdata->c400_host_buf,
661 src + start, 64);
662 else
663 outsb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100664 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700666 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100667 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
668 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#endif
670 start += 128;
671 blocks--;
672 }
673 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100674 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 ; // FIXME - no timeout
676
Ondrej Zary702a98c2010-08-10 18:01:16 -0700677#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100678 if (hostdata->io_width == 2)
679 outsw(instance->io_port + hostdata->c400_host_buf,
680 src + start, 64);
681 else
682 outsb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100683 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700685 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100686 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
687 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688#endif
689 start += 128;
690 blocks--;
691 }
692
Ondrej Zary42fc6372016-01-03 16:06:18 +1100693 /* wait for 53C80 registers to be available */
694 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG)) {
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100695 udelay(4); /* DTC436 chip hangs without this */
696 /* FIXME - no timeout */
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
700 printk(KERN_ERR "53C400w: no end dma signal\n");
701 }
Ondrej Zary42fc6372016-01-03 16:06:18 +1100702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
704 ; // TIMEOUT
705 return 0;
706}
Finn Thainff3d4572016-01-03 16:05:25 +1100707
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100708static int generic_NCR5380_dma_xfer_len(struct Scsi_Host *instance,
709 struct scsi_cmnd *cmd)
Finn Thainff3d4572016-01-03 16:05:25 +1100710{
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100711 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainff3d4572016-01-03 16:05:25 +1100712 int transfersize = cmd->transfersize;
713
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100714 if (hostdata->flags & FLAG_NO_PSEUDO_DMA)
715 return 0;
716
Finn Thainff3d4572016-01-03 16:05:25 +1100717 /* Limit transfers to 32K, for xx400 & xx406
718 * pseudoDMA that transfers in 128 bytes blocks.
719 */
720 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
721 !(cmd->SCp.this_residual % transfersize))
722 transfersize = 32 * 1024;
723
Ondrej Zaryf0394622016-01-03 16:06:14 +1100724 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
725 if (transfersize % 128)
726 transfersize = 0;
727
Finn Thainff3d4572016-01-03 16:05:25 +1100728 return transfersize;
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/*
732 * Include the NCR5380 core code that we build our driver around
733 */
734
735#include "NCR5380.c"
736
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100737static struct scsi_host_template driver_template = {
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100738 .proc_name = DRV_MODULE_NAME,
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100739 .name = "Generic NCR5380/NCR53C400 SCSI",
740 .detect = generic_NCR5380_detect,
741 .release = generic_NCR5380_release_resources,
742 .info = generic_NCR5380_info,
743 .queuecommand = generic_NCR5380_queue_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 .eh_abort_handler = generic_NCR5380_abort,
745 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100746 .can_queue = 16,
747 .this_id = 7,
748 .sg_tablesize = SG_ALL,
749 .cmd_per_lun = 2,
750 .use_clustering = DISABLE_CLUSTERING,
Finn Thain32b26a12016-01-03 16:05:58 +1100751 .cmd_size = NCR5380_CMD_SIZE,
Finn Thain0a4e3612016-01-03 16:06:07 +1100752 .max_sectors = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753};
Finn Thain161c0052016-01-03 16:05:46 +1100754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#include "scsi_module.c"
756
757module_param(ncr_irq, int, 0);
758module_param(ncr_dma, int, 0);
759module_param(ncr_addr, int, 0);
760module_param(ncr_5380, int, 0);
761module_param(ncr_53c400, int, 0);
762module_param(ncr_53c400a, int, 0);
763module_param(dtc_3181e, int, 0);
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100764module_param(hp_c2502, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765MODULE_LICENSE("GPL");
766
Geert Uytterhoevenb026e8e2015-01-03 23:00:16 +0100767#if !defined(SCSI_G_NCR5380_MEM) && defined(MODULE)
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800768static struct isapnp_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 {
770 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
771 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
772 0},
773 {0}
774};
775
776MODULE_DEVICE_TABLE(isapnp, id_table);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700777#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779__setup("ncr5380=", do_NCR5380_setup);
780__setup("ncr53c400=", do_NCR53C400_setup);
781__setup("ncr53c400a=", do_NCR53C400A_setup);
782__setup("dtc3181e=", do_DTC3181E_setup);