blob: 516bd6c4f4425036cd7a7bd84ab1d1a1ef3643c7 [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 *
Finn Thain9c41ab22016-03-23 21:10:28 +110022 * See Documentation/scsi/g_NCR5380.txt for more info.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/blkdev.h>
Finn Thain161c0052016-01-03 16:05:46 +110027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <scsi/scsi_host.h>
29#include "g_NCR5380.h"
30#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <linux/ioport.h>
33#include <linux/isapnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/interrupt.h>
35
Finn Thainc0965e62016-01-03 16:05:05 +110036static int ncr_irq;
37static int ncr_dma;
38static int ncr_addr;
39static int ncr_5380;
40static int ncr_53c400;
41static int ncr_53c400a;
42static int dtc_3181e;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +110043static int hp_c2502;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static struct override {
Al Viroc818cb62006-03-24 03:15:37 -080046 NCR5380_map_type NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int irq;
48 int dma;
49 int board; /* Use NCR53c400, Ricoh, etc. extensions ? */
50} overrides
51#ifdef GENERIC_NCR5380_OVERRIDE
52[] __initdata = GENERIC_NCR5380_OVERRIDE;
53#else
54[1] __initdata = { { 0,},};
55#endif
56
Tobias Klauser6391a112006-06-08 22:23:48 -070057#define NO_OVERRIDES ARRAY_SIZE(overrides)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Tobias Klauser6391a112006-06-08 22:23:48 -070059#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/**
62 * internal_setup - handle lilo command string override
63 * @board: BOARD_* identifier for the board
64 * @str: unused
65 * @ints: numeric parameters
66 *
67 * Do LILO command line initialization of the overrides array. Display
68 * errors when needed
69 *
70 * Locks: none
71 */
72
73static void __init internal_setup(int board, char *str, int *ints)
74{
Finn Thaind5f7e652016-01-03 16:05:03 +110075 static int commandline_current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 switch (board) {
77 case BOARD_NCR5380:
78 if (ints[0] != 2 && ints[0] != 3) {
79 printk(KERN_ERR "generic_NCR5380_setup : usage ncr5380=" STRVAL(NCR5380_map_name) ",irq,dma\n");
80 return;
81 }
82 break;
83 case BOARD_NCR53C400:
84 if (ints[0] != 2) {
85 printk(KERN_ERR "generic_NCR53C400_setup : usage ncr53c400=" STRVAL(NCR5380_map_name) ",irq\n");
86 return;
87 }
88 break;
89 case BOARD_NCR53C400A:
90 if (ints[0] != 2) {
91 printk(KERN_ERR "generic_NCR53C400A_setup : usage ncr53c400a=" STRVAL(NCR5380_map_name) ",irq\n");
92 return;
93 }
94 break;
95 case BOARD_DTC3181E:
96 if (ints[0] != 2) {
97 printk("generic_DTC3181E_setup : usage dtc3181e=" STRVAL(NCR5380_map_name) ",irq\n");
98 return;
99 }
100 break;
101 }
102
103 if (commandline_current < NO_OVERRIDES) {
104 overrides[commandline_current].NCR5380_map_name = (NCR5380_map_type) ints[1];
105 overrides[commandline_current].irq = ints[2];
106 if (ints[0] == 3)
107 overrides[commandline_current].dma = ints[3];
108 else
109 overrides[commandline_current].dma = DMA_NONE;
110 overrides[commandline_current].board = board;
111 ++commandline_current;
112 }
113}
114
115
116/**
117 * do_NCR53C80_setup - set up entry point
118 * @str: unused
119 *
120 * Setup function invoked at boot to parse the ncr5380= command
121 * line.
122 */
123
124static int __init do_NCR5380_setup(char *str)
125{
126 int ints[10];
127
Tobias Klauser6391a112006-06-08 22:23:48 -0700128 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 internal_setup(BOARD_NCR5380, str, ints);
130 return 1;
131}
132
133/**
134 * do_NCR53C400_setup - set up entry point
135 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700136 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * Setup function invoked at boot to parse the ncr53c400= command
139 * line.
140 */
141
142static int __init do_NCR53C400_setup(char *str)
143{
144 int ints[10];
145
Tobias Klauser6391a112006-06-08 22:23:48 -0700146 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 internal_setup(BOARD_NCR53C400, str, ints);
148 return 1;
149}
150
151/**
152 * do_NCR53C400A_setup - set up entry point
153 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700154 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 *
156 * Setup function invoked at boot to parse the ncr53c400a= command
157 * line.
158 */
159
160static int __init do_NCR53C400A_setup(char *str)
161{
162 int ints[10];
163
Tobias Klauser6391a112006-06-08 22:23:48 -0700164 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 internal_setup(BOARD_NCR53C400A, str, ints);
166 return 1;
167}
168
169/**
170 * do_DTC3181E_setup - set up entry point
171 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700172 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
174 * Setup function invoked at boot to parse the dtc3181e= command
175 * line.
176 */
177
178static int __init do_DTC3181E_setup(char *str)
179{
180 int ints[10];
181
Tobias Klauser6391a112006-06-08 22:23:48 -0700182 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 internal_setup(BOARD_DTC3181E, str, ints);
184 return 1;
185}
186
187#endif
188
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100189#ifndef SCSI_G_NCR5380_MEM
190/*
191 * Configure I/O address of 53C400A or DTC436 by writing magic numbers
192 * to ports 0x779 and 0x379.
193 */
194static void magic_configure(int idx, u8 irq, u8 magic[])
195{
196 u8 cfg = 0;
197
198 outb(magic[0], 0x779);
199 outb(magic[1], 0x379);
200 outb(magic[2], 0x379);
201 outb(magic[3], 0x379);
202 outb(magic[4], 0x379);
203
204 /* allowed IRQs for HP C2502 */
205 if (irq != 2 && irq != 3 && irq != 4 && irq != 5 && irq != 7)
206 irq = 0;
207 if (idx >= 0 && idx <= 7)
208 cfg = 0x80 | idx | (irq << 4);
209 outb(cfg, 0x379);
210}
211#endif
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/**
214 * generic_NCR5380_detect - look for NCR5380 controllers
215 * @tpnt: the scsi template
216 *
217 * Scan for the present of NCR5380, NCR53C400, NCR53C400A, DTC3181E
218 * and DTC436(ISAPnP) controllers. If overrides have been set we use
219 * them.
220 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Locks: none
222 */
223
Finn Thained8b9e72014-11-12 16:11:51 +1100224static int __init generic_NCR5380_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Finn Thaind5f7e652016-01-03 16:05:03 +1100226 static int current_override;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700227 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 unsigned int *ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100229 u8 *magic = NULL;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700230#ifndef SCSI_G_NCR5380_MEM
231 int i;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100232 int port_idx = -1;
Finn Thain9d376402016-03-23 21:10:10 +1100233 unsigned long region_size;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 static unsigned int __initdata ncr_53c400a_ports[] = {
236 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
237 };
238 static unsigned int __initdata dtc_3181e_ports[] = {
239 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
240 };
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100241 static u8 ncr_53c400a_magic[] __initdata = { /* 53C400A & DTC436 */
242 0x59, 0xb9, 0xc5, 0xae, 0xa6
243 };
244 static u8 hp_c2502_magic[] __initdata = { /* HP C2502 */
245 0x0f, 0x22, 0xf0, 0x20, 0x80
246 };
Finn Thain4d8c08c2016-01-03 16:05:09 +1100247 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct Scsi_Host *instance;
Ondrej Zary12150792016-01-03 16:06:15 +1100249 struct NCR5380_hostdata *hostdata;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700250#ifdef SCSI_G_NCR5380_MEM
Al Viroc818cb62006-03-24 03:15:37 -0800251 unsigned long base;
252 void __iomem *iomem;
Finn Thain9d376402016-03-23 21:10:10 +1100253 resource_size_t iomem_size;
Al Viroc818cb62006-03-24 03:15:37 -0800254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Finn Thainc0965e62016-01-03 16:05:05 +1100256 if (ncr_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 overrides[0].irq = ncr_irq;
Finn Thainc0965e62016-01-03 16:05:05 +1100258 if (ncr_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 overrides[0].dma = ncr_dma;
Finn Thainc0965e62016-01-03 16:05:05 +1100260 if (ncr_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 overrides[0].NCR5380_map_name = (NCR5380_map_type) ncr_addr;
Finn Thainc0965e62016-01-03 16:05:05 +1100262 if (ncr_5380)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 overrides[0].board = BOARD_NCR5380;
Finn Thainc0965e62016-01-03 16:05:05 +1100264 else if (ncr_53c400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 overrides[0].board = BOARD_NCR53C400;
Finn Thainc0965e62016-01-03 16:05:05 +1100266 else if (ncr_53c400a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 overrides[0].board = BOARD_NCR53C400A;
Finn Thainc0965e62016-01-03 16:05:05 +1100268 else if (dtc_3181e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 overrides[0].board = BOARD_DTC3181E;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100270 else if (hp_c2502)
271 overrides[0].board = BOARD_HP_C2502;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700272#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!current_override && isapnp_present()) {
274 struct pnp_dev *dev = NULL;
275 count = 0;
276 while ((dev = pnp_find_dev(NULL, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev))) {
277 if (count >= NO_OVERRIDES)
278 break;
Ondrej Zaryc94babb2010-08-10 18:01:15 -0700279 if (pnp_device_attach(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (pnp_activate_dev(dev) < 0) {
282 printk(KERN_ERR "dtc436e probe: activate failed\n");
283 pnp_device_detach(dev);
284 continue;
285 }
286 if (!pnp_port_valid(dev, 0)) {
287 printk(KERN_ERR "dtc436e probe: no valid port\n");
288 pnp_device_detach(dev);
289 continue;
290 }
291 if (pnp_irq_valid(dev, 0))
292 overrides[count].irq = pnp_irq(dev, 0);
293 else
Finn Thain22f5f102014-11-12 16:11:56 +1100294 overrides[count].irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (pnp_dma_valid(dev, 0))
296 overrides[count].dma = pnp_dma(dev, 0);
297 else
298 overrides[count].dma = DMA_NONE;
299 overrides[count].NCR5380_map_name = (NCR5380_map_type) pnp_port_start(dev, 0);
300 overrides[count].board = BOARD_DTC3181E;
301 count++;
302 }
303 }
Ondrej Zary702a98c2010-08-10 18:01:16 -0700304#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
307 if (!(overrides[current_override].NCR5380_map_name))
308 continue;
309
310 ports = NULL;
Finn Thain4d8c08c2016-01-03 16:05:09 +1100311 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 switch (overrides[current_override].board) {
313 case BOARD_NCR5380:
Finn Thain1bb46002016-03-23 21:10:14 +1100314 flags = FLAG_NO_PSEUDO_DMA | FLAG_DMA_FIXUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
316 case BOARD_NCR53C400A:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ports = ncr_53c400a_ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100318 magic = ncr_53c400a_magic;
319 break;
320 case BOARD_HP_C2502:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100321 ports = ncr_53c400a_ports;
322 magic = hp_c2502_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
324 case BOARD_DTC3181E:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ports = dtc_3181e_ports;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100326 magic = ncr_53c400a_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328 }
329
Ondrej Zary702a98c2010-08-10 18:01:16 -0700330#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100331 if (ports && magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* wakeup sequence for the NCR53C400A and DTC3181E */
333
334 /* Disable the adapter and look for a free io port */
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100335 magic_configure(-1, 0, magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Finn Thain9d376402016-03-23 21:10:10 +1100337 region_size = 16;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (overrides[current_override].NCR5380_map_name != PORT_AUTO)
340 for (i = 0; ports[i]; i++) {
Finn Thain9d376402016-03-23 21:10:10 +1100341 if (!request_region(ports[i], region_size, "ncr53c80"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 continue;
343 if (overrides[current_override].NCR5380_map_name == ports[i])
344 break;
Finn Thain9d376402016-03-23 21:10:10 +1100345 release_region(ports[i], region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 } else
347 for (i = 0; ports[i]; i++) {
Finn Thain9d376402016-03-23 21:10:10 +1100348 if (!request_region(ports[i], region_size, "ncr53c80"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 continue;
350 if (inb(ports[i]) == 0xff)
351 break;
Finn Thain9d376402016-03-23 21:10:10 +1100352 release_region(ports[i], region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 if (ports[i]) {
355 /* At this point we have our region reserved */
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100356 magic_configure(i, 0, magic); /* no IRQ yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 outb(0xc0, ports[i] + 9);
358 if (inb(ports[i] + 9) != 0x80)
359 continue;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100360 overrides[current_override].NCR5380_map_name = ports[i];
361 port_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 } else
363 continue;
364 }
365 else
366 {
367 /* Not a 53C400A style setup - just grab */
Finn Thain9d376402016-03-23 21:10:10 +1100368 region_size = 8;
369 if (!request_region(overrides[current_override].NCR5380_map_name,
370 region_size, "ncr5380"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373#else
Al Viroc818cb62006-03-24 03:15:37 -0800374 base = overrides[current_override].NCR5380_map_name;
Finn Thain9d376402016-03-23 21:10:10 +1100375 iomem_size = NCR53C400_region_size;
376 if (!request_mem_region(base, iomem_size, "ncr5380"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 continue;
Finn Thain9d376402016-03-23 21:10:10 +1100378 iomem = ioremap(base, iomem_size);
Al Viroc818cb62006-03-24 03:15:37 -0800379 if (!iomem) {
Finn Thain9d376402016-03-23 21:10:10 +1100380 release_mem_region(base, iomem_size);
Al Viroc818cb62006-03-24 03:15:37 -0800381 continue;
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#endif
384 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
Finn Thain0ad0eff2016-01-03 16:05:21 +1100385 if (instance == NULL)
386 goto out_release;
Ondrej Zary12150792016-01-03 16:06:15 +1100387 hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Ondrej Zary702a98c2010-08-10 18:01:16 -0700389#ifndef SCSI_G_NCR5380_MEM
Finn Thainb01ec342016-01-03 16:05:07 +1100390 instance->io_port = overrides[current_override].NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 instance->n_io_port = region_size;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100392 hostdata->io_width = 1; /* 8-bit PDMA by default */
Finn Thain4d8c08c2016-01-03 16:05:09 +1100393
394 /*
395 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
396 * the base address.
397 */
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100398 switch (overrides[current_override].board) {
399 case BOARD_NCR53C400:
Finn Thain4d8c08c2016-01-03 16:05:09 +1100400 instance->io_port += 8;
Ondrej Zary12150792016-01-03 16:06:15 +1100401 hostdata->c400_ctl_status = 0;
402 hostdata->c400_blk_cnt = 1;
403 hostdata->c400_host_buf = 4;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100404 break;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100405 case BOARD_DTC3181E:
406 hostdata->io_width = 2; /* 16-bit PDMA */
407 /* fall through */
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100408 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100409 case BOARD_HP_C2502:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100410 hostdata->c400_ctl_status = 9;
411 hostdata->c400_blk_cnt = 10;
412 hostdata->c400_host_buf = 8;
413 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100414 }
Al Viroc818cb62006-03-24 03:15:37 -0800415#else
Finn Thainb01ec342016-01-03 16:05:07 +1100416 instance->base = overrides[current_override].NCR5380_map_name;
Ondrej Zary12150792016-01-03 16:06:15 +1100417 hostdata->iomem = iomem;
Finn Thain9d376402016-03-23 21:10:10 +1100418 hostdata->iomem_size = iomem_size;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100419 switch (overrides[current_override].board) {
420 case BOARD_NCR53C400:
Ondrej Zary12150792016-01-03 16:06:15 +1100421 hostdata->c400_ctl_status = 0x100;
422 hostdata->c400_blk_cnt = 0x101;
423 hostdata->c400_host_buf = 0x104;
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100424 break;
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100425 case BOARD_DTC3181E:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100426 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100427 case BOARD_HP_C2502:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100428 pr_err(DRV_MODULE_NAME ": unknown register offsets\n");
429 goto out_unregister;
Ondrej Zary12150792016-01-03 16:06:15 +1100430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#endif
432
Finn Thain8053b0e2016-03-23 21:10:19 +1100433 if (NCR5380_init(instance, flags | FLAG_LATE_DMA_SETUP))
Finn Thain0ad0eff2016-01-03 16:05:21 +1100434 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100436 switch (overrides[current_override].board) {
437 case BOARD_NCR53C400:
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100438 case BOARD_DTC3181E:
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100439 case BOARD_NCR53C400A:
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100440 case BOARD_HP_C2502:
Ondrej Zary12150792016-01-03 16:06:15 +1100441 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
Ondrej Zarycecf3be2016-01-03 16:06:16 +1100442 }
Finn Thain4d8c08c2016-01-03 16:05:09 +1100443
Finn Thainb6488f92016-01-03 16:05:08 +1100444 NCR5380_maybe_reset_bus(instance);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (overrides[current_override].irq != IRQ_AUTO)
447 instance->irq = overrides[current_override].irq;
448 else
449 instance->irq = NCR5380_probe_irq(instance, 0xffff);
450
Finn Thain22f5f102014-11-12 16:11:56 +1100451 /* Compatibility with documented NCR5380 kernel parameters */
452 if (instance->irq == 255)
453 instance->irq = NO_IRQ;
454
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100455 if (instance->irq != NO_IRQ) {
456#ifndef SCSI_G_NCR5380_MEM
457 /* set IRQ for HP C2502 */
458 if (overrides[current_override].board == BOARD_HP_C2502)
459 magic_configure(port_idx, instance->irq, magic);
460#endif
Jeff Garzik1e641662007-11-11 19:52:05 -0500461 if (request_irq(instance->irq, generic_NCR5380_intr,
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100462 0, "NCR5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
Finn Thain22f5f102014-11-12 16:11:56 +1100464 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Finn Thain22f5f102014-11-12 16:11:56 +1100468 if (instance->irq == NO_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
470 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
471 }
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 ++current_override;
474 ++count;
475 }
476 return count;
Finn Thain0ad0eff2016-01-03 16:05:21 +1100477
478out_unregister:
479 scsi_unregister(instance);
480out_release:
481#ifndef SCSI_G_NCR5380_MEM
482 release_region(overrides[current_override].NCR5380_map_name, region_size);
483#else
484 iounmap(iomem);
Finn Thain9d376402016-03-23 21:10:10 +1100485 release_mem_region(base, iomem_size);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100486#endif
487 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * generic_NCR5380_release_resources - free resources
492 * @instance: host adapter to clean up
493 *
494 * Free the generic interface resources from this adapter.
495 *
496 * Locks: none
497 */
498
Finn Thained8b9e72014-11-12 16:11:51 +1100499static int generic_NCR5380_release_resources(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Finn Thain22f5f102014-11-12 16:11:56 +1100501 if (instance->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500502 free_irq(instance->irq, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 NCR5380_exit(instance);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700504#ifndef SCSI_G_NCR5380_MEM
Finn Thainb01ec342016-01-03 16:05:07 +1100505 release_region(instance->io_port, instance->n_io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#else
Finn Thain9d376402016-03-23 21:10:10 +1100507 {
508 struct NCR5380_hostdata *hostdata = shost_priv(instance);
509
510 iounmap(hostdata->iomem);
511 release_mem_region(instance->base, hostdata->iomem_size);
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/**
Finn Thain6c4b88c2016-03-23 21:10:17 +1100518 * generic_NCR5380_pread - pseudo DMA read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * @instance: adapter to read from
520 * @dst: buffer to read into
521 * @len: buffer length
522 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300523 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * controller
525 */
526
Finn Thain6c4b88c2016-03-23 21:10:17 +1100527static inline int generic_NCR5380_pread(struct Scsi_Host *instance,
528 unsigned char *dst, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Finn Thain54d8fe42016-01-03 16:05:06 +1100530 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int blocks = len / 128;
532 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Ondrej Zary12150792016-01-03 16:06:15 +1100534 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE | CSR_TRANS_DIR);
535 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100537 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100539 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
541 return -1;
542 }
Ondrej Zary12150792016-01-03 16:06:15 +1100543 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
544 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Ondrej Zary702a98c2010-08-10 18:01:16 -0700546#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100547 if (hostdata->io_width == 2)
548 insw(instance->io_port + hostdata->c400_host_buf,
549 dst + start, 64);
550 else
551 insb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100552 dst + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700554 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100555 memcpy_fromio(dst + start,
556 hostdata->iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
558 start += 128;
559 blocks--;
560 }
561
562 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100563 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
564 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Ondrej Zary702a98c2010-08-10 18:01:16 -0700566#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100567 if (hostdata->io_width == 2)
568 insw(instance->io_port + hostdata->c400_host_buf,
569 dst + start, 64);
570 else
571 insb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100572 dst + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700574 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100575 memcpy_fromio(dst + start,
576 hostdata->iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
578 start += 128;
579 blocks--;
580 }
581
Ondrej Zary12150792016-01-03 16:06:15 +1100582 if (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 printk("53C400r: no 53C80 gated irq after transfer");
584
Ondrej Zary42fc6372016-01-03 16:06:18 +1100585 /* wait for 53C80 registers to be available */
586 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ;
Ondrej Zary42fc6372016-01-03 16:06:18 +1100588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
590 printk(KERN_ERR "53C400r: no end dma signal\n");
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593}
594
595/**
Finn Thain6c4b88c2016-03-23 21:10:17 +1100596 * generic_NCR5380_pwrite - pseudo DMA write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * @instance: adapter to read from
598 * @dst: buffer to read into
599 * @len: buffer length
600 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300601 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * controller
603 */
604
Finn Thain6c4b88c2016-03-23 21:10:17 +1100605static inline int generic_NCR5380_pwrite(struct Scsi_Host *instance,
606 unsigned char *src, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Finn Thain54d8fe42016-01-03 16:05:06 +1100608 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int blocks = len / 128;
610 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Ondrej Zary12150792016-01-03 16:06:15 +1100612 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
613 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100615 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
617 return -1;
618 }
619
Ondrej Zary12150792016-01-03 16:06:15 +1100620 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100622 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 ; // FIXME - timeout
Ondrej Zary702a98c2010-08-10 18:01:16 -0700624#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100625 if (hostdata->io_width == 2)
626 outsw(instance->io_port + hostdata->c400_host_buf,
627 src + start, 64);
628 else
629 outsb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100630 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700632 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100633 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
634 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635#endif
636 start += 128;
637 blocks--;
638 }
639 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100640 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ; // FIXME - no timeout
642
Ondrej Zary702a98c2010-08-10 18:01:16 -0700643#ifndef SCSI_G_NCR5380_MEM
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100644 if (hostdata->io_width == 2)
645 outsw(instance->io_port + hostdata->c400_host_buf,
646 src + start, 64);
647 else
648 outsb(instance->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100649 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700651 /* implies SCSI_G_NCR5380_MEM */
Finn Thain54d8fe42016-01-03 16:05:06 +1100652 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
653 src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#endif
655 start += 128;
656 blocks--;
657 }
658
Ondrej Zary42fc6372016-01-03 16:06:18 +1100659 /* wait for 53C80 registers to be available */
660 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG)) {
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100661 udelay(4); /* DTC436 chip hangs without this */
662 /* FIXME - no timeout */
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
666 printk(KERN_ERR "53C400w: no end dma signal\n");
667 }
Ondrej Zary42fc6372016-01-03 16:06:18 +1100668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
670 ; // TIMEOUT
671 return 0;
672}
Finn Thainff3d4572016-01-03 16:05:25 +1100673
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100674static int generic_NCR5380_dma_xfer_len(struct Scsi_Host *instance,
675 struct scsi_cmnd *cmd)
Finn Thainff3d4572016-01-03 16:05:25 +1100676{
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100677 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainff3d4572016-01-03 16:05:25 +1100678 int transfersize = cmd->transfersize;
679
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100680 if (hostdata->flags & FLAG_NO_PSEUDO_DMA)
681 return 0;
682
Finn Thainff3d4572016-01-03 16:05:25 +1100683 /* Limit transfers to 32K, for xx400 & xx406
684 * pseudoDMA that transfers in 128 bytes blocks.
685 */
686 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
687 !(cmd->SCp.this_residual % transfersize))
688 transfersize = 32 * 1024;
689
Ondrej Zaryf0394622016-01-03 16:06:14 +1100690 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
691 if (transfersize % 128)
692 transfersize = 0;
693
Finn Thainff3d4572016-01-03 16:05:25 +1100694 return transfersize;
695}
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/*
698 * Include the NCR5380 core code that we build our driver around
699 */
700
701#include "NCR5380.c"
702
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100703static struct scsi_host_template driver_template = {
Finn Thainaa2e2cb2016-01-03 16:05:48 +1100704 .proc_name = DRV_MODULE_NAME,
Finn Thainaa2e2cb2016-01-03 16:05:48 +1100705 .name = "Generic NCR5380/NCR53C400 SCSI",
706 .detect = generic_NCR5380_detect,
707 .release = generic_NCR5380_release_resources,
708 .info = generic_NCR5380_info,
709 .queuecommand = generic_NCR5380_queue_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 .eh_abort_handler = generic_NCR5380_abort,
711 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
Finn Thainaa2e2cb2016-01-03 16:05:48 +1100712 .can_queue = 16,
713 .this_id = 7,
714 .sg_tablesize = SG_ALL,
715 .cmd_per_lun = 2,
716 .use_clustering = DISABLE_CLUSTERING,
Finn Thain32b26a12016-01-03 16:05:58 +1100717 .cmd_size = NCR5380_CMD_SIZE,
Finn Thain0a4e3612016-01-03 16:06:07 +1100718 .max_sectors = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719};
Finn Thain161c0052016-01-03 16:05:46 +1100720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#include "scsi_module.c"
722
723module_param(ncr_irq, int, 0);
724module_param(ncr_dma, int, 0);
725module_param(ncr_addr, int, 0);
726module_param(ncr_5380, int, 0);
727module_param(ncr_53c400, int, 0);
728module_param(ncr_53c400a, int, 0);
729module_param(dtc_3181e, int, 0);
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100730module_param(hp_c2502, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731MODULE_LICENSE("GPL");
732
Geert Uytterhoevenb026e8e2015-01-03 23:00:16 +0100733#if !defined(SCSI_G_NCR5380_MEM) && defined(MODULE)
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800734static struct isapnp_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 {
736 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
737 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
738 0},
739 {0}
740};
741
742MODULE_DEVICE_TABLE(isapnp, id_table);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700743#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745__setup("ncr5380=", do_NCR5380_setup);
746__setup("ncr53c400=", do_NCR53C400_setup);
747__setup("ncr53c400a=", do_NCR53C400A_setup);
748__setup("dtc3181e=", do_DTC3181E_setup);