blob: c34fc91ba48668c09970c5a83a6bd38fa90ccd12 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
30#include <linux/ioport.h>
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020031#include <linux/isa.h>
32#include <linux/pnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/interrupt.h>
34
Finn Thain14d739f2017-01-15 18:50:57 -050035/* Definitions for the core NCR5380 driver. */
36
37#define NCR5380_read(reg) \
38 ioread8(hostdata->io + hostdata->offset + (reg))
39#define NCR5380_write(reg, value) \
40 iowrite8(value, hostdata->io + hostdata->offset + (reg))
41
42#define NCR5380_implementation_fields \
43 int offset; \
44 int c400_ctl_status; \
45 int c400_blk_cnt; \
46 int c400_host_buf; \
47 int io_width
48
49#define NCR5380_dma_xfer_len generic_NCR5380_dma_xfer_len
50#define NCR5380_dma_recv_setup generic_NCR5380_pread
51#define NCR5380_dma_send_setup generic_NCR5380_pwrite
52#define NCR5380_dma_residual NCR5380_dma_residual_none
53
54#define NCR5380_intr generic_NCR5380_intr
55#define NCR5380_queue_command generic_NCR5380_queue_command
56#define NCR5380_abort generic_NCR5380_abort
57#define NCR5380_bus_reset generic_NCR5380_bus_reset
58#define NCR5380_info generic_NCR5380_info
59
60#define NCR5380_io_delay(x) udelay(x)
61
62#include "NCR5380.h"
63
64#define DRV_MODULE_NAME "g_NCR5380"
65
66#define NCR53C400_mem_base 0x3880
67#define NCR53C400_host_buffer 0x3900
68#define NCR53C400_region_size 0x3a00
69
70#define BOARD_NCR5380 0
71#define BOARD_NCR53C400 1
72#define BOARD_NCR53C400A 2
73#define BOARD_DTC3181E 3
74#define BOARD_HP_C2502 4
75
76#define IRQ_AUTO 254
77
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020078#define MAX_CARDS 8
79
80/* old-style parameters for compatibility */
Finn Thain70439e92016-12-05 01:07:20 -050081static int ncr_irq = -1;
Finn Thainc0965e62016-01-03 16:05:05 +110082static int ncr_addr;
83static int ncr_5380;
84static int ncr_53c400;
85static int ncr_53c400a;
86static int dtc_3181e;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +110087static int hp_c2502;
David Howells88f06b72017-04-04 16:54:27 +010088module_param_hw(ncr_irq, int, irq, 0);
89module_param_hw(ncr_addr, int, ioport, 0);
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020090module_param(ncr_5380, int, 0);
91module_param(ncr_53c400, int, 0);
92module_param(ncr_53c400a, int, 0);
93module_param(dtc_3181e, int, 0);
94module_param(hp_c2502, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Finn Thain70439e92016-12-05 01:07:20 -050096static int irq[] = { -1, -1, -1, -1, -1, -1, -1, -1 };
David Howells88f06b72017-04-04 16:54:27 +010097module_param_hw_array(irq, int, irq, NULL, 0);
Finn Thain70439e92016-12-05 01:07:20 -050098MODULE_PARM_DESC(irq, "IRQ number(s) (0=none, 254=auto [default])");
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020099
100static int base[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
David Howells88f06b72017-04-04 16:54:27 +0100101module_param_hw_array(base, int, ioport, NULL, 0);
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200102MODULE_PARM_DESC(base, "base address(es)");
103
104static int card[] = { -1, -1, -1, -1, -1, -1, -1, -1 };
105module_param_array(card, int, NULL, 0);
106MODULE_PARM_DESC(card, "card type (0=NCR5380, 1=NCR53C400, 2=NCR53C400A, 3=DTC3181E, 4=HP C2502)");
107
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400108MODULE_ALIAS("g_NCR5380_mmio");
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200109MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Ondrej Zary906e4a3c2016-12-05 01:07:20 -0500111static void g_NCR5380_trigger_irq(struct Scsi_Host *instance)
112{
113 struct NCR5380_hostdata *hostdata = shost_priv(instance);
114
115 /*
116 * An interrupt is triggered whenever BSY = false, SEL = true
117 * and a bit set in the SELECT_ENABLE_REG is asserted on the
118 * SCSI bus.
119 *
120 * Note that the bus is only driven when the phase control signals
121 * (I/O, C/D, and MSG) match those in the TCR.
122 */
123 NCR5380_write(TARGET_COMMAND_REG,
124 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
125 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
126 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
127 NCR5380_write(INITIATOR_COMMAND_REG,
128 ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
129
130 msleep(1);
131
132 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
133 NCR5380_write(SELECT_ENABLE_REG, 0);
134 NCR5380_write(TARGET_COMMAND_REG, 0);
135}
136
137/**
138 * g_NCR5380_probe_irq - find the IRQ of a NCR5380 or equivalent
139 * @instance: SCSI host instance
140 *
141 * Autoprobe for the IRQ line used by the card by triggering an IRQ
142 * and then looking to see what interrupt actually turned up.
143 */
144
145static int g_NCR5380_probe_irq(struct Scsi_Host *instance)
146{
147 struct NCR5380_hostdata *hostdata = shost_priv(instance);
148 int irq_mask, irq;
149
150 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
151 irq_mask = probe_irq_on();
152 g_NCR5380_trigger_irq(instance);
153 irq = probe_irq_off(irq_mask);
154 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
155
156 if (irq <= 0)
157 return NO_IRQ;
158 return irq;
159}
160
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100161/*
162 * Configure I/O address of 53C400A or DTC436 by writing magic numbers
163 * to ports 0x779 and 0x379.
164 */
165static void magic_configure(int idx, u8 irq, u8 magic[])
166{
167 u8 cfg = 0;
168
169 outb(magic[0], 0x779);
170 outb(magic[1], 0x379);
171 outb(magic[2], 0x379);
172 outb(magic[3], 0x379);
173 outb(magic[4], 0x379);
174
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500175 if (irq == 9)
176 irq = 2;
177
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100178 if (idx >= 0 && idx <= 7)
179 cfg = 0x80 | idx | (irq << 4);
180 outb(cfg, 0x379);
181}
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400182
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500183static irqreturn_t legacy_empty_irq_handler(int irq, void *dev_id)
184{
185 return IRQ_HANDLED;
186}
187
188static int legacy_find_free_irq(int *irq_table)
189{
190 while (*irq_table != -1) {
191 if (!request_irq(*irq_table, legacy_empty_irq_handler,
192 IRQF_PROBE_SHARED, "Test IRQ",
193 (void *)irq_table)) {
194 free_irq(*irq_table, (void *) irq_table);
195 return *irq_table;
196 }
197 irq_table++;
198 }
199 return -1;
200}
201
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400202static unsigned int ncr_53c400a_ports[] = {
203 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
204};
205static unsigned int dtc_3181e_ports[] = {
206 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
207};
208static u8 ncr_53c400a_magic[] = { /* 53C400A & DTC436 */
209 0x59, 0xb9, 0xc5, 0xae, 0xa6
210};
211static u8 hp_c2502_magic[] = { /* HP C2502 */
212 0x0f, 0x22, 0xf0, 0x20, 0x80
213};
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500214static int hp_c2502_irqs[] = {
215 9, 5, 7, 3, 4, -1
216};
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100217
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200218static int generic_NCR5380_init_one(struct scsi_host_template *tpnt,
219 struct device *pdev, int base, int irq, int board)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400221 bool is_pmio = base <= 0xffff;
222 int ret;
223 int flags = 0;
224 unsigned int *ports = NULL;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100225 u8 *magic = NULL;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700226 int i;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100227 int port_idx = -1;
Finn Thain9d376402016-03-23 21:10:10 +1100228 unsigned long region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct Scsi_Host *instance;
Ondrej Zary12150792016-01-03 16:06:15 +1100230 struct NCR5380_hostdata *hostdata;
Finn Thain820682b2016-10-10 00:46:53 -0400231 u8 __iomem *iomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200233 switch (board) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200234 case BOARD_NCR5380:
235 flags = FLAG_NO_PSEUDO_DMA | FLAG_DMA_FIXUP;
236 break;
237 case BOARD_NCR53C400A:
238 ports = ncr_53c400a_ports;
239 magic = ncr_53c400a_magic;
240 break;
241 case BOARD_HP_C2502:
242 ports = ncr_53c400a_ports;
243 magic = hp_c2502_magic;
244 break;
245 case BOARD_DTC3181E:
246 ports = dtc_3181e_ports;
247 magic = ncr_53c400a_magic;
248 break;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400251 if (is_pmio && ports && magic) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200252 /* wakeup sequence for the NCR53C400A and DTC3181E */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200254 /* Disable the adapter and look for a free io port */
255 magic_configure(-1, 0, magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200257 region_size = 16;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200258 if (base)
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200259 for (i = 0; ports[i]; i++) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200260 if (base == ports[i]) { /* index found */
261 if (!request_region(ports[i],
262 region_size,
263 "ncr53c80"))
264 return -EBUSY;
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200265 break;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200266 }
267 }
268 else
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200269 for (i = 0; ports[i]; i++) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200270 if (!request_region(ports[i], region_size,
271 "ncr53c80"))
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200272 continue;
273 if (inb(ports[i]) == 0xff)
274 break;
275 release_region(ports[i], region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200277 if (ports[i]) {
278 /* At this point we have our region reserved */
279 magic_configure(i, 0, magic); /* no IRQ yet */
Ondrej Zary7b93ca42016-11-11 10:00:20 +1100280 base = ports[i];
281 outb(0xc0, base + 9);
282 if (inb(base + 9) != 0x80) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200283 ret = -ENODEV;
284 goto out_release;
285 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200286 port_idx = i;
287 } else
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200288 return -EINVAL;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400289 } else if (is_pmio) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200290 /* NCR5380 - no configuration, just grab */
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200291 region_size = 8;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200292 if (!base || !request_region(base, region_size, "ncr5380"))
293 return -EBUSY;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400294 } else { /* MMIO */
295 region_size = NCR53C400_region_size;
296 if (!request_mem_region(base, region_size, "ncr5380"))
297 return -EBUSY;
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200298 }
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400299
300 if (is_pmio)
301 iomem = ioport_map(base, region_size);
302 else
303 iomem = ioremap(base, region_size);
304
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200305 if (!iomem) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200306 ret = -ENOMEM;
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200307 goto out_release;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200308 }
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400309
310 instance = scsi_host_alloc(tpnt, sizeof(struct NCR5380_hostdata));
311 if (instance == NULL) {
312 ret = -ENOMEM;
313 goto out_unmap;
314 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200315 hostdata = shost_priv(instance);
316
Finn Thain820682b2016-10-10 00:46:53 -0400317 hostdata->io = iomem;
318 hostdata->region_size = region_size;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400319
320 if (is_pmio) {
Finn Thain820682b2016-10-10 00:46:53 -0400321 hostdata->io_port = base;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400322 hostdata->io_width = 1; /* 8-bit PDMA by default */
323 hostdata->offset = 0;
324
325 /*
326 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
327 * the base address.
328 */
329 switch (board) {
330 case BOARD_NCR53C400:
Finn Thain820682b2016-10-10 00:46:53 -0400331 hostdata->io_port += 8;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400332 hostdata->c400_ctl_status = 0;
333 hostdata->c400_blk_cnt = 1;
334 hostdata->c400_host_buf = 4;
335 break;
336 case BOARD_DTC3181E:
337 hostdata->io_width = 2; /* 16-bit PDMA */
338 /* fall through */
339 case BOARD_NCR53C400A:
340 case BOARD_HP_C2502:
341 hostdata->c400_ctl_status = 9;
342 hostdata->c400_blk_cnt = 10;
343 hostdata->c400_host_buf = 8;
344 break;
345 }
346 } else {
Finn Thain820682b2016-10-10 00:46:53 -0400347 hostdata->base = base;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400348 hostdata->offset = NCR53C400_mem_base;
349 switch (board) {
350 case BOARD_NCR53C400:
351 hostdata->c400_ctl_status = 0x100;
352 hostdata->c400_blk_cnt = 0x101;
353 hostdata->c400_host_buf = 0x104;
354 break;
355 case BOARD_DTC3181E:
356 case BOARD_NCR53C400A:
357 case BOARD_HP_C2502:
358 pr_err(DRV_MODULE_NAME ": unknown register offsets\n");
359 ret = -EINVAL;
360 goto out_unregister;
361 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200362 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200363
Ondrej Zary89fa9b52016-12-05 01:07:19 -0500364 /* Check for vacant slot */
365 NCR5380_write(MODE_REG, 0);
366 if (NCR5380_read(MODE_REG) != 0) {
367 ret = -ENODEV;
368 goto out_unregister;
369 }
370
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200371 ret = NCR5380_init(instance, flags | FLAG_LATE_DMA_SETUP);
372 if (ret)
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200373 goto out_unregister;
374
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200375 switch (board) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200376 case BOARD_NCR53C400:
377 case BOARD_DTC3181E:
378 case BOARD_NCR53C400A:
379 case BOARD_HP_C2502:
380 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
381 }
382
383 NCR5380_maybe_reset_bus(instance);
384
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200385 /* Compatibility with documented NCR5380 kernel parameters */
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500386 if (irq == 255 || irq == 0)
387 irq = NO_IRQ;
Finn Thain70439e92016-12-05 01:07:20 -0500388 else if (irq == -1)
389 irq = IRQ_AUTO;
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500390
391 if (board == BOARD_HP_C2502) {
392 int *irq_table = hp_c2502_irqs;
393 int board_irq = -1;
394
395 switch (irq) {
396 case NO_IRQ:
397 board_irq = 0;
398 break;
399 case IRQ_AUTO:
400 board_irq = legacy_find_free_irq(irq_table);
401 break;
402 default:
403 while (*irq_table != -1)
404 if (*irq_table++ == irq)
405 board_irq = irq;
406 }
407
408 if (board_irq <= 0) {
409 board_irq = 0;
410 irq = NO_IRQ;
411 }
412
413 magic_configure(port_idx, board_irq, magic);
414 }
415
Finn Thain70439e92016-12-05 01:07:20 -0500416 if (irq == IRQ_AUTO) {
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500417 instance->irq = g_NCR5380_probe_irq(instance);
Finn Thain70439e92016-12-05 01:07:20 -0500418 if (instance->irq == NO_IRQ)
419 shost_printk(KERN_INFO, instance, "no irq detected\n");
420 } else {
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500421 instance->irq = irq;
Finn Thain70439e92016-12-05 01:07:20 -0500422 if (instance->irq == NO_IRQ)
423 shost_printk(KERN_INFO, instance, "no irq provided\n");
424 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200425
426 if (instance->irq != NO_IRQ) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200427 if (request_irq(instance->irq, generic_NCR5380_intr,
428 0, "NCR5380", instance)) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200429 instance->irq = NO_IRQ;
Finn Thain70439e92016-12-05 01:07:20 -0500430 shost_printk(KERN_INFO, instance,
431 "irq %d denied\n", instance->irq);
432 } else {
433 shost_printk(KERN_INFO, instance,
434 "irq %d acquired\n", instance->irq);
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200435 }
436 }
437
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200438 ret = scsi_add_host(instance, pdev);
439 if (ret)
440 goto out_free_irq;
441 scsi_scan_host(instance);
442 dev_set_drvdata(pdev, instance);
443 return 0;
Finn Thain0ad0eff2016-01-03 16:05:21 +1100444
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200445out_free_irq:
446 if (instance->irq != NO_IRQ)
447 free_irq(instance->irq, instance);
448 NCR5380_exit(instance);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100449out_unregister:
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200450 scsi_host_put(instance);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400451out_unmap:
Finn Thain0ad0eff2016-01-03 16:05:21 +1100452 iounmap(iomem);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400453out_release:
454 if (is_pmio)
455 release_region(base, region_size);
456 else
457 release_mem_region(base, region_size);
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200458 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200461static void generic_NCR5380_release_resources(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400463 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain820682b2016-10-10 00:46:53 -0400464 void __iomem *iomem = hostdata->io;
465 unsigned long io_port = hostdata->io_port;
466 unsigned long base = hostdata->base;
467 unsigned long region_size = hostdata->region_size;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400468
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200469 scsi_remove_host(instance);
Finn Thain22f5f102014-11-12 16:11:56 +1100470 if (instance->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500471 free_irq(instance->irq, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 NCR5380_exit(instance);
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200473 scsi_host_put(instance);
Finn Thain820682b2016-10-10 00:46:53 -0400474 iounmap(iomem);
475 if (io_port)
476 release_region(io_port, region_size);
477 else
478 release_mem_region(base, region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/**
Finn Thain6c4b88c2016-03-23 21:10:17 +1100482 * generic_NCR5380_pread - pseudo DMA read
Finn Thain4a98f892016-10-10 00:46:53 -0400483 * @hostdata: scsi host private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * @dst: buffer to read into
485 * @len: buffer length
486 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300487 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * controller
489 */
490
Finn Thain4a98f892016-10-10 00:46:53 -0400491static inline int generic_NCR5380_pread(struct NCR5380_hostdata *hostdata,
Finn Thain6c4b88c2016-03-23 21:10:17 +1100492 unsigned char *dst, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 int blocks = len / 128;
495 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Ondrej Zary12150792016-01-03 16:06:15 +1100497 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE | CSR_TRANS_DIR);
498 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100500 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100502 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
504 return -1;
505 }
Ondrej Zary12150792016-01-03 16:06:15 +1100506 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
507 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Finn Thain820682b2016-10-10 00:46:53 -0400509 if (hostdata->io_port && hostdata->io_width == 2)
510 insw(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100511 dst + start, 64);
Finn Thain820682b2016-10-10 00:46:53 -0400512 else if (hostdata->io_port)
513 insb(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100514 dst + start, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400515 else
516 memcpy_fromio(dst + start,
Finn Thain820682b2016-10-10 00:46:53 -0400517 hostdata->io + NCR53C400_host_buffer, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 start += 128;
520 blocks--;
521 }
522
523 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100524 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
525 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Finn Thain820682b2016-10-10 00:46:53 -0400527 if (hostdata->io_port && hostdata->io_width == 2)
528 insw(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100529 dst + start, 64);
Finn Thain820682b2016-10-10 00:46:53 -0400530 else if (hostdata->io_port)
531 insb(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100532 dst + start, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400533 else
534 memcpy_fromio(dst + start,
Finn Thain820682b2016-10-10 00:46:53 -0400535 hostdata->io + NCR53C400_host_buffer, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 start += 128;
538 blocks--;
539 }
540
Ondrej Zary12150792016-01-03 16:06:15 +1100541 if (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 printk("53C400r: no 53C80 gated irq after transfer");
543
Ondrej Zary42fc6372016-01-03 16:06:18 +1100544 /* wait for 53C80 registers to be available */
545 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ;
Ondrej Zary42fc6372016-01-03 16:06:18 +1100547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
549 printk(KERN_ERR "53C400r: no end dma signal\n");
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
554/**
Finn Thain6c4b88c2016-03-23 21:10:17 +1100555 * generic_NCR5380_pwrite - pseudo DMA write
Finn Thain4a98f892016-10-10 00:46:53 -0400556 * @hostdata: scsi host private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * @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
Finn Thain4a98f892016-10-10 00:46:53 -0400564static inline int generic_NCR5380_pwrite(struct NCR5380_hostdata *hostdata,
Finn Thain6c4b88c2016-03-23 21:10:17 +1100565 unsigned char *src, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 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);
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_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
575 return -1;
576 }
577
Ondrej Zary12150792016-01-03 16:06:15 +1100578 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100580 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ; // FIXME - timeout
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400582
Finn Thain820682b2016-10-10 00:46:53 -0400583 if (hostdata->io_port && hostdata->io_width == 2)
584 outsw(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100585 src + start, 64);
Finn Thain820682b2016-10-10 00:46:53 -0400586 else if (hostdata->io_port)
587 outsb(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100588 src + start, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400589 else
Finn Thain820682b2016-10-10 00:46:53 -0400590 memcpy_toio(hostdata->io + NCR53C400_host_buffer,
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400591 src + start, 128);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 start += 128;
594 blocks--;
595 }
596 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100597 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ; // FIXME - no timeout
599
Finn Thain820682b2016-10-10 00:46:53 -0400600 if (hostdata->io_port && hostdata->io_width == 2)
601 outsw(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100602 src + start, 64);
Finn Thain820682b2016-10-10 00:46:53 -0400603 else if (hostdata->io_port)
604 outsb(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100605 src + start, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400606 else
Finn Thain820682b2016-10-10 00:46:53 -0400607 memcpy_toio(hostdata->io + NCR53C400_host_buffer,
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400608 src + start, 128);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 start += 128;
611 blocks--;
612 }
613
Ondrej Zary42fc6372016-01-03 16:06:18 +1100614 /* wait for 53C80 registers to be available */
615 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG)) {
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100616 udelay(4); /* DTC436 chip hangs without this */
617 /* FIXME - no timeout */
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
621 printk(KERN_ERR "53C400w: no end dma signal\n");
622 }
Ondrej Zary42fc6372016-01-03 16:06:18 +1100623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
625 ; // TIMEOUT
626 return 0;
627}
Finn Thainff3d4572016-01-03 16:05:25 +1100628
Finn Thain4a98f892016-10-10 00:46:53 -0400629static int generic_NCR5380_dma_xfer_len(struct NCR5380_hostdata *hostdata,
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100630 struct scsi_cmnd *cmd)
Finn Thainff3d4572016-01-03 16:05:25 +1100631{
632 int transfersize = cmd->transfersize;
633
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100634 if (hostdata->flags & FLAG_NO_PSEUDO_DMA)
635 return 0;
636
Finn Thainff3d4572016-01-03 16:05:25 +1100637 /* Limit transfers to 32K, for xx400 & xx406
638 * pseudoDMA that transfers in 128 bytes blocks.
639 */
640 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
641 !(cmd->SCp.this_residual % transfersize))
642 transfersize = 32 * 1024;
643
Ondrej Zaryf0394622016-01-03 16:06:14 +1100644 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
645 if (transfersize % 128)
646 transfersize = 0;
647
Finn Thainff3d4572016-01-03 16:05:25 +1100648 return transfersize;
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/*
652 * Include the NCR5380 core code that we build our driver around
653 */
654
655#include "NCR5380.c"
656
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100657static struct scsi_host_template driver_template = {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200658 .module = THIS_MODULE,
Finn Thainaa2e2cb2016-01-03 16:05:48 +1100659 .proc_name = DRV_MODULE_NAME,
Finn Thainaa2e2cb2016-01-03 16:05:48 +1100660 .name = "Generic NCR5380/NCR53C400 SCSI",
Finn Thainaa2e2cb2016-01-03 16:05:48 +1100661 .info = generic_NCR5380_info,
662 .queuecommand = generic_NCR5380_queue_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .eh_abort_handler = generic_NCR5380_abort,
664 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
Finn Thainaa2e2cb2016-01-03 16:05:48 +1100665 .can_queue = 16,
666 .this_id = 7,
667 .sg_tablesize = SG_ALL,
668 .cmd_per_lun = 2,
669 .use_clustering = DISABLE_CLUSTERING,
Finn Thain32b26a12016-01-03 16:05:58 +1100670 .cmd_size = NCR5380_CMD_SIZE,
Finn Thain0a4e3612016-01-03 16:06:07 +1100671 .max_sectors = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672};
Finn Thain161c0052016-01-03 16:05:46 +1100673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200675static int generic_NCR5380_isa_match(struct device *pdev, unsigned int ndev)
676{
677 int ret = generic_NCR5380_init_one(&driver_template, pdev, base[ndev],
678 irq[ndev], card[ndev]);
679 if (ret) {
680 if (base[ndev])
681 printk(KERN_WARNING "Card not found at address 0x%03x\n",
682 base[ndev]);
683 return 0;
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200686 return 1;
687}
688
689static int generic_NCR5380_isa_remove(struct device *pdev,
690 unsigned int ndev)
691{
692 generic_NCR5380_release_resources(dev_get_drvdata(pdev));
693 dev_set_drvdata(pdev, NULL);
694 return 0;
695}
696
697static struct isa_driver generic_NCR5380_isa_driver = {
698 .match = generic_NCR5380_isa_match,
699 .remove = generic_NCR5380_isa_remove,
700 .driver = {
701 .name = DRV_MODULE_NAME
702 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703};
704
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400705#ifdef CONFIG_PNP
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200706static struct pnp_device_id generic_NCR5380_pnp_ids[] = {
707 { .id = "DTC436e", .driver_data = BOARD_DTC3181E },
708 { .id = "" }
709};
710MODULE_DEVICE_TABLE(pnp, generic_NCR5380_pnp_ids);
711
712static int generic_NCR5380_pnp_probe(struct pnp_dev *pdev,
713 const struct pnp_device_id *id)
714{
715 int base, irq;
716
717 if (pnp_activate_dev(pdev) < 0)
718 return -EBUSY;
719
720 base = pnp_port_start(pdev, 0);
721 irq = pnp_irq(pdev, 0);
722
723 return generic_NCR5380_init_one(&driver_template, &pdev->dev, base, irq,
724 id->driver_data);
725}
726
727static void generic_NCR5380_pnp_remove(struct pnp_dev *pdev)
728{
729 generic_NCR5380_release_resources(pnp_get_drvdata(pdev));
730 pnp_set_drvdata(pdev, NULL);
731}
732
733static struct pnp_driver generic_NCR5380_pnp_driver = {
734 .name = DRV_MODULE_NAME,
735 .id_table = generic_NCR5380_pnp_ids,
736 .probe = generic_NCR5380_pnp_probe,
737 .remove = generic_NCR5380_pnp_remove,
738};
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400739#endif /* defined(CONFIG_PNP) */
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200740
741static int pnp_registered, isa_registered;
742
743static int __init generic_NCR5380_init(void)
744{
745 int ret = 0;
746
747 /* compatibility with old-style parameters */
Finn Thain70439e92016-12-05 01:07:20 -0500748 if (irq[0] == -1 && base[0] == 0 && card[0] == -1) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200749 irq[0] = ncr_irq;
750 base[0] = ncr_addr;
751 if (ncr_5380)
752 card[0] = BOARD_NCR5380;
753 if (ncr_53c400)
754 card[0] = BOARD_NCR53C400;
755 if (ncr_53c400a)
756 card[0] = BOARD_NCR53C400A;
757 if (dtc_3181e)
758 card[0] = BOARD_DTC3181E;
759 if (hp_c2502)
760 card[0] = BOARD_HP_C2502;
761 }
762
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400763#ifdef CONFIG_PNP
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200764 if (!pnp_register_driver(&generic_NCR5380_pnp_driver))
765 pnp_registered = 1;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700766#endif
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200767 ret = isa_register_driver(&generic_NCR5380_isa_driver, MAX_CARDS);
768 if (!ret)
769 isa_registered = 1;
770
771 return (pnp_registered || isa_registered) ? 0 : ret;
772}
773
774static void __exit generic_NCR5380_exit(void)
775{
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400776#ifdef CONFIG_PNP
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200777 if (pnp_registered)
778 pnp_unregister_driver(&generic_NCR5380_pnp_driver);
779#endif
780 if (isa_registered)
781 isa_unregister_driver(&generic_NCR5380_isa_driver);
782}
783
784module_init(generic_NCR5380_init);
785module_exit(generic_NCR5380_exit);