blob: aaeb6b6b4b16f8ee8c0619a11605d6dd8d052bf4 [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#define PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/blkdev.h>
Finn Thain161c0052016-01-03 16:05:46 +110064#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <scsi/scsi_host.h>
66#include "g_NCR5380.h"
67#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/init.h>
69#include <linux/ioport.h>
70#include <linux/isapnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/interrupt.h>
72
Finn Thainc0965e62016-01-03 16:05:05 +110073static int ncr_irq;
74static int ncr_dma;
75static int ncr_addr;
76static int ncr_5380;
77static int ncr_53c400;
78static int ncr_53c400a;
79static int dtc_3181e;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +110080static int hp_c2502;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static struct override {
Al Viroc818cb62006-03-24 03:15:37 -080083 NCR5380_map_type NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int irq;
85 int dma;
86 int board; /* Use NCR53c400, Ricoh, etc. extensions ? */
87} overrides
88#ifdef GENERIC_NCR5380_OVERRIDE
89[] __initdata = GENERIC_NCR5380_OVERRIDE;
90#else
91[1] __initdata = { { 0,},};
92#endif
93
Tobias Klauser6391a112006-06-08 22:23:48 -070094#define NO_OVERRIDES ARRAY_SIZE(overrides)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Tobias Klauser6391a112006-06-08 22:23:48 -070096#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/**
99 * internal_setup - handle lilo command string override
100 * @board: BOARD_* identifier for the board
101 * @str: unused
102 * @ints: numeric parameters
103 *
104 * Do LILO command line initialization of the overrides array. Display
105 * errors when needed
106 *
107 * Locks: none
108 */
109
110static void __init internal_setup(int board, char *str, int *ints)
111{
Finn Thaind5f7e652016-01-03 16:05:03 +1100112 static int commandline_current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 switch (board) {
114 case BOARD_NCR5380:
115 if (ints[0] != 2 && ints[0] != 3) {
116 printk(KERN_ERR "generic_NCR5380_setup : usage ncr5380=" STRVAL(NCR5380_map_name) ",irq,dma\n");
117 return;
118 }
119 break;
120 case BOARD_NCR53C400:
121 if (ints[0] != 2) {
122 printk(KERN_ERR "generic_NCR53C400_setup : usage ncr53c400=" STRVAL(NCR5380_map_name) ",irq\n");
123 return;
124 }
125 break;
126 case BOARD_NCR53C400A:
127 if (ints[0] != 2) {
128 printk(KERN_ERR "generic_NCR53C400A_setup : usage ncr53c400a=" STRVAL(NCR5380_map_name) ",irq\n");
129 return;
130 }
131 break;
132 case BOARD_DTC3181E:
133 if (ints[0] != 2) {
134 printk("generic_DTC3181E_setup : usage dtc3181e=" STRVAL(NCR5380_map_name) ",irq\n");
135 return;
136 }
137 break;
138 }
139
140 if (commandline_current < NO_OVERRIDES) {
141 overrides[commandline_current].NCR5380_map_name = (NCR5380_map_type) ints[1];
142 overrides[commandline_current].irq = ints[2];
143 if (ints[0] == 3)
144 overrides[commandline_current].dma = ints[3];
145 else
146 overrides[commandline_current].dma = DMA_NONE;
147 overrides[commandline_current].board = board;
148 ++commandline_current;
149 }
150}
151
152
153/**
154 * do_NCR53C80_setup - set up entry point
155 * @str: unused
156 *
157 * Setup function invoked at boot to parse the ncr5380= command
158 * line.
159 */
160
161static int __init do_NCR5380_setup(char *str)
162{
163 int ints[10];
164
Tobias Klauser6391a112006-06-08 22:23:48 -0700165 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 internal_setup(BOARD_NCR5380, str, ints);
167 return 1;
168}
169
170/**
171 * do_NCR53C400_setup - set up entry point
172 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700173 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Setup function invoked at boot to parse the ncr53c400= command
176 * line.
177 */
178
179static int __init do_NCR53C400_setup(char *str)
180{
181 int ints[10];
182
Tobias Klauser6391a112006-06-08 22:23:48 -0700183 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 internal_setup(BOARD_NCR53C400, str, ints);
185 return 1;
186}
187
188/**
189 * do_NCR53C400A_setup - set up entry point
190 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700191 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * Setup function invoked at boot to parse the ncr53c400a= command
194 * line.
195 */
196
197static int __init do_NCR53C400A_setup(char *str)
198{
199 int ints[10];
200
Tobias Klauser6391a112006-06-08 22:23:48 -0700201 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 internal_setup(BOARD_NCR53C400A, str, ints);
203 return 1;
204}
205
206/**
207 * do_DTC3181E_setup - set up entry point
208 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700209 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *
211 * Setup function invoked at boot to parse the dtc3181e= command
212 * line.
213 */
214
215static int __init do_DTC3181E_setup(char *str)
216{
217 int ints[10];
218
Tobias Klauser6391a112006-06-08 22:23:48 -0700219 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 internal_setup(BOARD_DTC3181E, str, ints);
221 return 1;
222}
223
224#endif
225
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100226#ifndef SCSI_G_NCR5380_MEM
227/*
228 * Configure I/O address of 53C400A or DTC436 by writing magic numbers
229 * to ports 0x779 and 0x379.
230 */
231static void magic_configure(int idx, u8 irq, u8 magic[])
232{
233 u8 cfg = 0;
234
235 outb(magic[0], 0x779);
236 outb(magic[1], 0x379);
237 outb(magic[2], 0x379);
238 outb(magic[3], 0x379);
239 outb(magic[4], 0x379);
240
241 /* allowed IRQs for HP C2502 */
242 if (irq != 2 && irq != 3 && irq != 4 && irq != 5 && irq != 7)
243 irq = 0;
244 if (idx >= 0 && idx <= 7)
245 cfg = 0x80 | idx | (irq << 4);
246 outb(cfg, 0x379);
247}
248#endif
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/**
251 * generic_NCR5380_detect - look for NCR5380 controllers
252 * @tpnt: the scsi template
253 *
254 * Scan for the present of NCR5380, NCR53C400, NCR53C400A, DTC3181E
255 * and DTC436(ISAPnP) controllers. If overrides have been set we use
256 * them.
257 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * Locks: none
259 */
260
Finn Thained8b9e72014-11-12 16:11:51 +1100261static int __init generic_NCR5380_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Finn Thaind5f7e652016-01-03 16:05:03 +1100263 static int current_override;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700264 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 unsigned int *ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100266 u8 *magic = NULL;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700267#ifndef SCSI_G_NCR5380_MEM
268 int i;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100269 int port_idx = -1;
Finn Thain9d376402016-03-23 21:10:10 +1100270 unsigned long region_size;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 static unsigned int __initdata ncr_53c400a_ports[] = {
273 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
274 };
275 static unsigned int __initdata dtc_3181e_ports[] = {
276 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
277 };
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100278 static u8 ncr_53c400a_magic[] __initdata = { /* 53C400A & DTC436 */
279 0x59, 0xb9, 0xc5, 0xae, 0xa6
280 };
281 static u8 hp_c2502_magic[] __initdata = { /* HP C2502 */
282 0x0f, 0x22, 0xf0, 0x20, 0x80
283 };
Finn Thain4d8c08c2016-01-03 16:05:09 +1100284 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct Scsi_Host *instance;
Ondrej Zary12150792016-01-03 16:06:15 +1100286 struct NCR5380_hostdata *hostdata;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700287#ifdef SCSI_G_NCR5380_MEM
Al Viroc818cb62006-03-24 03:15:37 -0800288 unsigned long base;
289 void __iomem *iomem;
Finn Thain9d376402016-03-23 21:10:10 +1100290 resource_size_t iomem_size;
Al Viroc818cb62006-03-24 03:15:37 -0800291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Finn Thainc0965e62016-01-03 16:05:05 +1100293 if (ncr_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 overrides[0].irq = ncr_irq;
Finn Thainc0965e62016-01-03 16:05:05 +1100295 if (ncr_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 overrides[0].dma = ncr_dma;
Finn Thainc0965e62016-01-03 16:05:05 +1100297 if (ncr_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 overrides[0].NCR5380_map_name = (NCR5380_map_type) ncr_addr;
Finn Thainc0965e62016-01-03 16:05:05 +1100299 if (ncr_5380)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 overrides[0].board = BOARD_NCR5380;
Finn Thainc0965e62016-01-03 16:05:05 +1100301 else if (ncr_53c400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 overrides[0].board = BOARD_NCR53C400;
Finn Thainc0965e62016-01-03 16:05:05 +1100303 else if (ncr_53c400a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 overrides[0].board = BOARD_NCR53C400A;
Finn Thainc0965e62016-01-03 16:05:05 +1100305 else if (dtc_3181e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 overrides[0].board = BOARD_DTC3181E;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100307 else if (hp_c2502)
308 overrides[0].board = BOARD_HP_C2502;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700309#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!current_override && isapnp_present()) {
311 struct pnp_dev *dev = NULL;
312 count = 0;
313 while ((dev = pnp_find_dev(NULL, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev))) {
314 if (count >= NO_OVERRIDES)
315 break;
Ondrej Zaryc94babb2010-08-10 18:01:15 -0700316 if (pnp_device_attach(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (pnp_activate_dev(dev) < 0) {
319 printk(KERN_ERR "dtc436e probe: activate failed\n");
320 pnp_device_detach(dev);
321 continue;
322 }
323 if (!pnp_port_valid(dev, 0)) {
324 printk(KERN_ERR "dtc436e probe: no valid port\n");
325 pnp_device_detach(dev);
326 continue;
327 }
328 if (pnp_irq_valid(dev, 0))
329 overrides[count].irq = pnp_irq(dev, 0);
330 else
Finn Thain22f5f102014-11-12 16:11:56 +1100331 overrides[count].irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (pnp_dma_valid(dev, 0))
333 overrides[count].dma = pnp_dma(dev, 0);
334 else
335 overrides[count].dma = DMA_NONE;
336 overrides[count].NCR5380_map_name = (NCR5380_map_type) pnp_port_start(dev, 0);
337 overrides[count].board = BOARD_DTC3181E;
338 count++;
339 }
340 }
Ondrej Zary702a98c2010-08-10 18:01:16 -0700341#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
344 if (!(overrides[current_override].NCR5380_map_name))
345 continue;
346
347 ports = NULL;
Finn Thain4d8c08c2016-01-03 16:05:09 +1100348 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 switch (overrides[current_override].board) {
350 case BOARD_NCR5380:
Finn Thain1bb46002016-03-23 21:10:14 +1100351 flags = FLAG_NO_PSEUDO_DMA | FLAG_DMA_FIXUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353 case BOARD_NCR53C400A:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 ports = ncr_53c400a_ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100355 magic = ncr_53c400a_magic;
356 break;
357 case BOARD_HP_C2502:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100358 ports = ncr_53c400a_ports;
359 magic = hp_c2502_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 case BOARD_DTC3181E:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 ports = dtc_3181e_ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100363 magic = ncr_53c400a_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
365 }
366
Ondrej Zary702a98c2010-08-10 18:01:16 -0700367#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100368 if (ports && magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* wakeup sequence for the NCR53C400A and DTC3181E */
370
371 /* Disable the adapter and look for a free io port */
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100372 magic_configure(-1, 0, magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Finn Thain9d376402016-03-23 21:10:10 +1100374 region_size = 16;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (overrides[current_override].NCR5380_map_name != PORT_AUTO)
377 for (i = 0; ports[i]; i++) {
Finn Thain9d376402016-03-23 21:10:10 +1100378 if (!request_region(ports[i], region_size, "ncr53c80"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 continue;
380 if (overrides[current_override].NCR5380_map_name == ports[i])
381 break;
Finn Thain9d376402016-03-23 21:10:10 +1100382 release_region(ports[i], region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 } else
384 for (i = 0; ports[i]; i++) {
Finn Thain9d376402016-03-23 21:10:10 +1100385 if (!request_region(ports[i], region_size, "ncr53c80"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 continue;
387 if (inb(ports[i]) == 0xff)
388 break;
Finn Thain9d376402016-03-23 21:10:10 +1100389 release_region(ports[i], region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 if (ports[i]) {
392 /* At this point we have our region reserved */
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100393 magic_configure(i, 0, magic); /* no IRQ yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 outb(0xc0, ports[i] + 9);
395 if (inb(ports[i] + 9) != 0x80)
396 continue;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100397 overrides[current_override].NCR5380_map_name = ports[i];
398 port_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 } else
400 continue;
401 }
402 else
403 {
404 /* Not a 53C400A style setup - just grab */
Finn Thain9d376402016-03-23 21:10:10 +1100405 region_size = 8;
406 if (!request_region(overrides[current_override].NCR5380_map_name,
407 region_size, "ncr5380"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410#else
Al Viroc818cb62006-03-24 03:15:37 -0800411 base = overrides[current_override].NCR5380_map_name;
Finn Thain9d376402016-03-23 21:10:10 +1100412 iomem_size = NCR53C400_region_size;
413 if (!request_mem_region(base, iomem_size, "ncr5380"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 continue;
Finn Thain9d376402016-03-23 21:10:10 +1100415 iomem = ioremap(base, iomem_size);
Al Viroc818cb62006-03-24 03:15:37 -0800416 if (!iomem) {
Finn Thain9d376402016-03-23 21:10:10 +1100417 release_mem_region(base, iomem_size);
Al Viroc818cb62006-03-24 03:15:37 -0800418 continue;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#endif
421 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
Finn Thain0ad0eff2016-01-03 16:05:21 +1100422 if (instance == NULL)
423 goto out_release;
Ondrej Zary12150792016-01-03 16:06:15 +1100424 hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Ondrej Zary702a98c2010-08-10 18:01:16 -0700426#ifndef SCSI_G_NCR5380_MEM
Finn Thainb01ec342016-01-03 16:05:07 +1100427 instance->io_port = overrides[current_override].NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 instance->n_io_port = region_size;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100429 hostdata->io_width = 1; /* 8-bit PDMA by default */
Finn Thain4d8c08c2016-01-03 16:05:09 +1100430
431 /*
432 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
433 * the base address.
434 */
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100435 switch (overrides[current_override].board) {
436 case BOARD_NCR53C400:
Finn Thain4d8c08c2016-01-03 16:05:09 +1100437 instance->io_port += 8;
Ondrej Zary12150792016-01-03 16:06:15 +1100438 hostdata->c400_ctl_status = 0;
439 hostdata->c400_blk_cnt = 1;
440 hostdata->c400_host_buf = 4;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100441 break;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100442 case BOARD_DTC3181E:
443 hostdata->io_width = 2; /* 16-bit PDMA */
444 /* fall through */
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100445 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100446 case BOARD_HP_C2502:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100447 hostdata->c400_ctl_status = 9;
448 hostdata->c400_blk_cnt = 10;
449 hostdata->c400_host_buf = 8;
450 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100451 }
Al Viroc818cb62006-03-24 03:15:37 -0800452#else
Finn Thainb01ec342016-01-03 16:05:07 +1100453 instance->base = overrides[current_override].NCR5380_map_name;
Ondrej Zary12150792016-01-03 16:06:15 +1100454 hostdata->iomem = iomem;
Finn Thain9d376402016-03-23 21:10:10 +1100455 hostdata->iomem_size = iomem_size;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100456 switch (overrides[current_override].board) {
457 case BOARD_NCR53C400:
Ondrej Zary12150792016-01-03 16:06:15 +1100458 hostdata->c400_ctl_status = 0x100;
459 hostdata->c400_blk_cnt = 0x101;
460 hostdata->c400_host_buf = 0x104;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100461 break;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100462 case BOARD_DTC3181E:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100463 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100464 case BOARD_HP_C2502:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100465 pr_err(DRV_MODULE_NAME ": unknown register offsets\n");
466 goto out_unregister;
Ondrej Zary12150792016-01-03 16:06:15 +1100467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#endif
469
Finn Thain0ad0eff2016-01-03 16:05:21 +1100470 if (NCR5380_init(instance, flags))
471 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100473 switch (overrides[current_override].board) {
474 case BOARD_NCR53C400:
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100475 case BOARD_DTC3181E:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100476 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100477 case BOARD_HP_C2502:
Ondrej Zary12150792016-01-03 16:06:15 +1100478 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100479 }
Finn Thain4d8c08c2016-01-03 16:05:09 +1100480
Finn Thainb6488f92016-01-03 16:05:08 +1100481 NCR5380_maybe_reset_bus(instance);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (overrides[current_override].irq != IRQ_AUTO)
484 instance->irq = overrides[current_override].irq;
485 else
486 instance->irq = NCR5380_probe_irq(instance, 0xffff);
487
Finn Thain22f5f102014-11-12 16:11:56 +1100488 /* Compatibility with documented NCR5380 kernel parameters */
489 if (instance->irq == 255)
490 instance->irq = NO_IRQ;
491
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100492 if (instance->irq != NO_IRQ) {
493#ifndef SCSI_G_NCR5380_MEM
494 /* set IRQ for HP C2502 */
495 if (overrides[current_override].board == BOARD_HP_C2502)
496 magic_configure(port_idx, instance->irq, magic);
497#endif
Jeff Garzik1e641662007-11-11 19:52:05 -0500498 if (request_irq(instance->irq, generic_NCR5380_intr,
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100499 0, "NCR5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
Finn Thain22f5f102014-11-12 16:11:56 +1100501 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Finn Thain22f5f102014-11-12 16:11:56 +1100505 if (instance->irq == NO_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
507 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
508 }
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ++current_override;
511 ++count;
512 }
513 return count;
Finn Thain0ad0eff2016-01-03 16:05:21 +1100514
515out_unregister:
516 scsi_unregister(instance);
517out_release:
518#ifndef SCSI_G_NCR5380_MEM
519 release_region(overrides[current_override].NCR5380_map_name, region_size);
520#else
521 iounmap(iomem);
Finn Thain9d376402016-03-23 21:10:10 +1100522 release_mem_region(base, iomem_size);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100523#endif
524 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * generic_NCR5380_release_resources - free resources
529 * @instance: host adapter to clean up
530 *
531 * Free the generic interface resources from this adapter.
532 *
533 * Locks: none
534 */
535
Finn Thained8b9e72014-11-12 16:11:51 +1100536static int generic_NCR5380_release_resources(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Finn Thain22f5f102014-11-12 16:11:56 +1100538 if (instance->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500539 free_irq(instance->irq, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 NCR5380_exit(instance);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700541#ifndef SCSI_G_NCR5380_MEM
Finn Thainb01ec342016-01-03 16:05:07 +1100542 release_region(instance->io_port, instance->n_io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#else
Finn Thain9d376402016-03-23 21:10:10 +1100544 {
545 struct NCR5380_hostdata *hostdata = shost_priv(instance);
546
547 iounmap(hostdata->iomem);
548 release_mem_region(instance->base, hostdata->iomem_size);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/**
555 * NCR5380_pread - pseudo DMA read
556 * @instance: adapter to read from
557 * @dst: buffer to read into
558 * @len: buffer length
559 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300560 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * controller
562 */
563
564static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
565{
Finn Thain54d8fe42016-01-03 16:05:06 +1100566 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int blocks = len / 128;
568 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Ondrej Zary12150792016-01-03 16:06:15 +1100570 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE | CSR_TRANS_DIR);
571 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100573 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100575 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
577 return -1;
578 }
Ondrej Zary12150792016-01-03 16:06:15 +1100579 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
580 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Ondrej Zary702a98c2010-08-10 18:01:16 -0700582#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100583 if (hostdata->io_width == 2)
584 insw(instance->io_port + hostdata->c400_host_buf,
585 dst + start, 64);
586 else
587 insb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100588 dst + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700590 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100591 memcpy_fromio(dst + start,
592 hostdata->iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#endif
594 start += 128;
595 blocks--;
596 }
597
598 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100599 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
600 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Ondrej Zary702a98c2010-08-10 18:01:16 -0700602#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100603 if (hostdata->io_width == 2)
604 insw(instance->io_port + hostdata->c400_host_buf,
605 dst + start, 64);
606 else
607 insb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100608 dst + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700610 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100611 memcpy_fromio(dst + start,
612 hostdata->iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
614 start += 128;
615 blocks--;
616 }
617
Ondrej Zary12150792016-01-03 16:06:15 +1100618 if (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 printk("53C400r: no 53C80 gated irq after transfer");
620
Ondrej Zary42fc6372016-01-03 16:06:18 +1100621 /* wait for 53C80 registers to be available */
622 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 ;
Ondrej Zary42fc6372016-01-03 16:06:18 +1100624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
626 printk(KERN_ERR "53C400r: no end dma signal\n");
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629}
630
631/**
632 * NCR5380_write - pseudo DMA write
633 * @instance: adapter to read from
634 * @dst: buffer to read into
635 * @len: buffer length
636 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300637 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * controller
639 */
640
641static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
642{
Finn Thain54d8fe42016-01-03 16:05:06 +1100643 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int blocks = len / 128;
645 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Ondrej Zary12150792016-01-03 16:06:15 +1100647 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
648 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100650 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
652 return -1;
653 }
654
Ondrej Zary12150792016-01-03 16:06:15 +1100655 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100657 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 ; // FIXME - timeout
Ondrej Zary702a98c2010-08-10 18:01:16 -0700659#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100660 if (hostdata->io_width == 2)
661 outsw(instance->io_port + hostdata->c400_host_buf,
662 src + start, 64);
663 else
664 outsb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100665 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700667 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100668 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
669 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#endif
671 start += 128;
672 blocks--;
673 }
674 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100675 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 ; // FIXME - no timeout
677
Ondrej Zary702a98c2010-08-10 18:01:16 -0700678#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100679 if (hostdata->io_width == 2)
680 outsw(instance->io_port + hostdata->c400_host_buf,
681 src + start, 64);
682 else
683 outsb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100684 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700686 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100687 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
688 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689#endif
690 start += 128;
691 blocks--;
692 }
693
Ondrej Zary42fc6372016-01-03 16:06:18 +1100694 /* wait for 53C80 registers to be available */
695 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG)) {
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100696 udelay(4); /* DTC436 chip hangs without this */
697 /* FIXME - no timeout */
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
701 printk(KERN_ERR "53C400w: no end dma signal\n");
702 }
Ondrej Zary42fc6372016-01-03 16:06:18 +1100703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
705 ; // TIMEOUT
706 return 0;
707}
Finn Thainff3d4572016-01-03 16:05:25 +1100708
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100709static int generic_NCR5380_dma_xfer_len(struct Scsi_Host *instance,
710 struct scsi_cmnd *cmd)
Finn Thainff3d4572016-01-03 16:05:25 +1100711{
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100712 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainff3d4572016-01-03 16:05:25 +1100713 int transfersize = cmd->transfersize;
714
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100715 if (hostdata->flags & FLAG_NO_PSEUDO_DMA)
716 return 0;
717
Finn Thainff3d4572016-01-03 16:05:25 +1100718 /* Limit transfers to 32K, for xx400 & xx406
719 * pseudoDMA that transfers in 128 bytes blocks.
720 */
721 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
722 !(cmd->SCp.this_residual % transfersize))
723 transfersize = 32 * 1024;
724
Ondrej Zaryf0394622016-01-03 16:06:14 +1100725 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
726 if (transfersize % 128)
727 transfersize = 0;
728
Finn Thainff3d4572016-01-03 16:05:25 +1100729 return transfersize;
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*
733 * Include the NCR5380 core code that we build our driver around
734 */
735
736#include "NCR5380.c"
737
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100738static struct scsi_host_template driver_template = {
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100739 .proc_name = DRV_MODULE_NAME,
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100740 .name = "Generic NCR5380/NCR53C400 SCSI",
741 .detect = generic_NCR5380_detect,
742 .release = generic_NCR5380_release_resources,
743 .info = generic_NCR5380_info,
744 .queuecommand = generic_NCR5380_queue_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .eh_abort_handler = generic_NCR5380_abort,
746 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100747 .can_queue = 16,
748 .this_id = 7,
749 .sg_tablesize = SG_ALL,
750 .cmd_per_lun = 2,
751 .use_clustering = DISABLE_CLUSTERING,
Finn Thain32b26a12016-01-03 16:05:58 +1100752 .cmd_size = NCR5380_CMD_SIZE,
Finn Thain0a4e3612016-01-03 16:06:07 +1100753 .max_sectors = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754};
Finn Thain161c0052016-01-03 16:05:46 +1100755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756#include "scsi_module.c"
757
758module_param(ncr_irq, int, 0);
759module_param(ncr_dma, int, 0);
760module_param(ncr_addr, int, 0);
761module_param(ncr_5380, int, 0);
762module_param(ncr_53c400, int, 0);
763module_param(ncr_53c400a, int, 0);
764module_param(dtc_3181e, int, 0);
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100765module_param(hp_c2502, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766MODULE_LICENSE("GPL");
767
Geert Uytterhoevenb026e8e2015-01-03 23:00:16 +0100768#if !defined(SCSI_G_NCR5380_MEM) && defined(MODULE)
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800769static struct isapnp_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 {
771 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
772 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
773 0},
774 {0}
775};
776
777MODULE_DEVICE_TABLE(isapnp, id_table);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700778#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780__setup("ncr5380=", do_NCR5380_setup);
781__setup("ncr53c400=", do_NCR53C400_setup);
782__setup("ncr53c400a=", do_NCR53C400A_setup);
783__setup("dtc3181e=", do_DTC3181E_setup);