blob: 6f9665d50d84bb485d3ba9cf99da05bae0f1c018 [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>
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020033#include <linux/isa.h>
34#include <linux/pnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
36
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020037#define MAX_CARDS 8
38
39/* old-style parameters for compatibility */
Finn Thain70439e92016-12-05 01:07:20 -050040static int ncr_irq = -1;
Finn Thainc0965e62016-01-03 16:05:05 +110041static int ncr_addr;
42static int ncr_5380;
43static int ncr_53c400;
44static int ncr_53c400a;
45static int dtc_3181e;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +110046static int hp_c2502;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020047module_param(ncr_irq, int, 0);
48module_param(ncr_addr, int, 0);
49module_param(ncr_5380, int, 0);
50module_param(ncr_53c400, int, 0);
51module_param(ncr_53c400a, int, 0);
52module_param(dtc_3181e, int, 0);
53module_param(hp_c2502, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Finn Thain70439e92016-12-05 01:07:20 -050055static int irq[] = { -1, -1, -1, -1, -1, -1, -1, -1 };
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020056module_param_array(irq, int, NULL, 0);
Finn Thain70439e92016-12-05 01:07:20 -050057MODULE_PARM_DESC(irq, "IRQ number(s) (0=none, 254=auto [default])");
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020058
59static int base[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
60module_param_array(base, int, NULL, 0);
61MODULE_PARM_DESC(base, "base address(es)");
62
63static int card[] = { -1, -1, -1, -1, -1, -1, -1, -1 };
64module_param_array(card, int, NULL, 0);
65MODULE_PARM_DESC(card, "card type (0=NCR5380, 1=NCR53C400, 2=NCR53C400A, 3=DTC3181E, 4=HP C2502)");
66
Ondrej Zaryb61bacb2016-10-10 00:46:52 -040067MODULE_ALIAS("g_NCR5380_mmio");
Ondrej Zarya8cfbca2016-09-27 21:00:25 +020068MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Ondrej Zary906e4a3c2016-12-05 01:07:20 -050070static void g_NCR5380_trigger_irq(struct Scsi_Host *instance)
71{
72 struct NCR5380_hostdata *hostdata = shost_priv(instance);
73
74 /*
75 * An interrupt is triggered whenever BSY = false, SEL = true
76 * and a bit set in the SELECT_ENABLE_REG is asserted on the
77 * SCSI bus.
78 *
79 * Note that the bus is only driven when the phase control signals
80 * (I/O, C/D, and MSG) match those in the TCR.
81 */
82 NCR5380_write(TARGET_COMMAND_REG,
83 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
84 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
85 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
86 NCR5380_write(INITIATOR_COMMAND_REG,
87 ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
88
89 msleep(1);
90
91 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
92 NCR5380_write(SELECT_ENABLE_REG, 0);
93 NCR5380_write(TARGET_COMMAND_REG, 0);
94}
95
96/**
97 * g_NCR5380_probe_irq - find the IRQ of a NCR5380 or equivalent
98 * @instance: SCSI host instance
99 *
100 * Autoprobe for the IRQ line used by the card by triggering an IRQ
101 * and then looking to see what interrupt actually turned up.
102 */
103
104static int g_NCR5380_probe_irq(struct Scsi_Host *instance)
105{
106 struct NCR5380_hostdata *hostdata = shost_priv(instance);
107 int irq_mask, irq;
108
109 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
110 irq_mask = probe_irq_on();
111 g_NCR5380_trigger_irq(instance);
112 irq = probe_irq_off(irq_mask);
113 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
114
115 if (irq <= 0)
116 return NO_IRQ;
117 return irq;
118}
119
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100120/*
121 * Configure I/O address of 53C400A or DTC436 by writing magic numbers
122 * to ports 0x779 and 0x379.
123 */
124static void magic_configure(int idx, u8 irq, u8 magic[])
125{
126 u8 cfg = 0;
127
128 outb(magic[0], 0x779);
129 outb(magic[1], 0x379);
130 outb(magic[2], 0x379);
131 outb(magic[3], 0x379);
132 outb(magic[4], 0x379);
133
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500134 if (irq == 9)
135 irq = 2;
136
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100137 if (idx >= 0 && idx <= 7)
138 cfg = 0x80 | idx | (irq << 4);
139 outb(cfg, 0x379);
140}
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400141
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500142static irqreturn_t legacy_empty_irq_handler(int irq, void *dev_id)
143{
144 return IRQ_HANDLED;
145}
146
147static int legacy_find_free_irq(int *irq_table)
148{
149 while (*irq_table != -1) {
150 if (!request_irq(*irq_table, legacy_empty_irq_handler,
151 IRQF_PROBE_SHARED, "Test IRQ",
152 (void *)irq_table)) {
153 free_irq(*irq_table, (void *) irq_table);
154 return *irq_table;
155 }
156 irq_table++;
157 }
158 return -1;
159}
160
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400161static unsigned int ncr_53c400a_ports[] = {
162 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
163};
164static unsigned int dtc_3181e_ports[] = {
165 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
166};
167static u8 ncr_53c400a_magic[] = { /* 53C400A & DTC436 */
168 0x59, 0xb9, 0xc5, 0xae, 0xa6
169};
170static u8 hp_c2502_magic[] = { /* HP C2502 */
171 0x0f, 0x22, 0xf0, 0x20, 0x80
172};
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500173static int hp_c2502_irqs[] = {
174 9, 5, 7, 3, 4, -1
175};
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100176
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200177static int generic_NCR5380_init_one(struct scsi_host_template *tpnt,
178 struct device *pdev, int base, int irq, int board)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400180 bool is_pmio = base <= 0xffff;
181 int ret;
182 int flags = 0;
183 unsigned int *ports = NULL;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100184 u8 *magic = NULL;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700185 int i;
Ondrej Zaryc6084cb2016-01-03 16:06:19 +1100186 int port_idx = -1;
Finn Thain9d376402016-03-23 21:10:10 +1100187 unsigned long region_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct Scsi_Host *instance;
Ondrej Zary12150792016-01-03 16:06:15 +1100189 struct NCR5380_hostdata *hostdata;
Finn Thain820682b2016-10-10 00:46:53 -0400190 u8 __iomem *iomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200192 switch (board) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200193 case BOARD_NCR5380:
194 flags = FLAG_NO_PSEUDO_DMA | FLAG_DMA_FIXUP;
195 break;
196 case BOARD_NCR53C400A:
197 ports = ncr_53c400a_ports;
198 magic = ncr_53c400a_magic;
199 break;
200 case BOARD_HP_C2502:
201 ports = ncr_53c400a_ports;
202 magic = hp_c2502_magic;
203 break;
204 case BOARD_DTC3181E:
205 ports = dtc_3181e_ports;
206 magic = ncr_53c400a_magic;
207 break;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400210 if (is_pmio && ports && magic) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200211 /* wakeup sequence for the NCR53C400A and DTC3181E */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200213 /* Disable the adapter and look for a free io port */
214 magic_configure(-1, 0, magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200216 region_size = 16;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200217 if (base)
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200218 for (i = 0; ports[i]; i++) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200219 if (base == ports[i]) { /* index found */
220 if (!request_region(ports[i],
221 region_size,
222 "ncr53c80"))
223 return -EBUSY;
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200224 break;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200225 }
226 }
227 else
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200228 for (i = 0; ports[i]; i++) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200229 if (!request_region(ports[i], region_size,
230 "ncr53c80"))
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200231 continue;
232 if (inb(ports[i]) == 0xff)
233 break;
234 release_region(ports[i], region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200236 if (ports[i]) {
237 /* At this point we have our region reserved */
238 magic_configure(i, 0, magic); /* no IRQ yet */
Ondrej Zary7b93ca42016-11-11 10:00:20 +1100239 base = ports[i];
240 outb(0xc0, base + 9);
241 if (inb(base + 9) != 0x80) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200242 ret = -ENODEV;
243 goto out_release;
244 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200245 port_idx = i;
246 } else
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200247 return -EINVAL;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400248 } else if (is_pmio) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200249 /* NCR5380 - no configuration, just grab */
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200250 region_size = 8;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200251 if (!base || !request_region(base, region_size, "ncr5380"))
252 return -EBUSY;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400253 } else { /* MMIO */
254 region_size = NCR53C400_region_size;
255 if (!request_mem_region(base, region_size, "ncr5380"))
256 return -EBUSY;
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200257 }
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400258
259 if (is_pmio)
260 iomem = ioport_map(base, region_size);
261 else
262 iomem = ioremap(base, region_size);
263
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200264 if (!iomem) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200265 ret = -ENOMEM;
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200266 goto out_release;
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200267 }
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400268
269 instance = scsi_host_alloc(tpnt, sizeof(struct NCR5380_hostdata));
270 if (instance == NULL) {
271 ret = -ENOMEM;
272 goto out_unmap;
273 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200274 hostdata = shost_priv(instance);
275
Finn Thain820682b2016-10-10 00:46:53 -0400276 hostdata->io = iomem;
277 hostdata->region_size = region_size;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400278
279 if (is_pmio) {
Finn Thain820682b2016-10-10 00:46:53 -0400280 hostdata->io_port = base;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400281 hostdata->io_width = 1; /* 8-bit PDMA by default */
282 hostdata->offset = 0;
283
284 /*
285 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
286 * the base address.
287 */
288 switch (board) {
289 case BOARD_NCR53C400:
Finn Thain820682b2016-10-10 00:46:53 -0400290 hostdata->io_port += 8;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400291 hostdata->c400_ctl_status = 0;
292 hostdata->c400_blk_cnt = 1;
293 hostdata->c400_host_buf = 4;
294 break;
295 case BOARD_DTC3181E:
296 hostdata->io_width = 2; /* 16-bit PDMA */
297 /* fall through */
298 case BOARD_NCR53C400A:
299 case BOARD_HP_C2502:
300 hostdata->c400_ctl_status = 9;
301 hostdata->c400_blk_cnt = 10;
302 hostdata->c400_host_buf = 8;
303 break;
304 }
305 } else {
Finn Thain820682b2016-10-10 00:46:53 -0400306 hostdata->base = base;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400307 hostdata->offset = NCR53C400_mem_base;
308 switch (board) {
309 case BOARD_NCR53C400:
310 hostdata->c400_ctl_status = 0x100;
311 hostdata->c400_blk_cnt = 0x101;
312 hostdata->c400_host_buf = 0x104;
313 break;
314 case BOARD_DTC3181E:
315 case BOARD_NCR53C400A:
316 case BOARD_HP_C2502:
317 pr_err(DRV_MODULE_NAME ": unknown register offsets\n");
318 ret = -EINVAL;
319 goto out_unregister;
320 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200321 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200322
Ondrej Zary89fa9b52016-12-05 01:07:19 -0500323 /* Check for vacant slot */
324 NCR5380_write(MODE_REG, 0);
325 if (NCR5380_read(MODE_REG) != 0) {
326 ret = -ENODEV;
327 goto out_unregister;
328 }
329
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200330 ret = NCR5380_init(instance, flags | FLAG_LATE_DMA_SETUP);
331 if (ret)
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200332 goto out_unregister;
333
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200334 switch (board) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200335 case BOARD_NCR53C400:
336 case BOARD_DTC3181E:
337 case BOARD_NCR53C400A:
338 case BOARD_HP_C2502:
339 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
340 }
341
342 NCR5380_maybe_reset_bus(instance);
343
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200344 /* Compatibility with documented NCR5380 kernel parameters */
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500345 if (irq == 255 || irq == 0)
346 irq = NO_IRQ;
Finn Thain70439e92016-12-05 01:07:20 -0500347 else if (irq == -1)
348 irq = IRQ_AUTO;
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500349
350 if (board == BOARD_HP_C2502) {
351 int *irq_table = hp_c2502_irqs;
352 int board_irq = -1;
353
354 switch (irq) {
355 case NO_IRQ:
356 board_irq = 0;
357 break;
358 case IRQ_AUTO:
359 board_irq = legacy_find_free_irq(irq_table);
360 break;
361 default:
362 while (*irq_table != -1)
363 if (*irq_table++ == irq)
364 board_irq = irq;
365 }
366
367 if (board_irq <= 0) {
368 board_irq = 0;
369 irq = NO_IRQ;
370 }
371
372 magic_configure(port_idx, board_irq, magic);
373 }
374
Finn Thain70439e92016-12-05 01:07:20 -0500375 if (irq == IRQ_AUTO) {
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500376 instance->irq = g_NCR5380_probe_irq(instance);
Finn Thain70439e92016-12-05 01:07:20 -0500377 if (instance->irq == NO_IRQ)
378 shost_printk(KERN_INFO, instance, "no irq detected\n");
379 } else {
Finn Thain145c3ae4c2016-12-05 01:07:20 -0500380 instance->irq = irq;
Finn Thain70439e92016-12-05 01:07:20 -0500381 if (instance->irq == NO_IRQ)
382 shost_printk(KERN_INFO, instance, "no irq provided\n");
383 }
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200384
385 if (instance->irq != NO_IRQ) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200386 if (request_irq(instance->irq, generic_NCR5380_intr,
387 0, "NCR5380", instance)) {
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200388 instance->irq = NO_IRQ;
Finn Thain70439e92016-12-05 01:07:20 -0500389 shost_printk(KERN_INFO, instance,
390 "irq %d denied\n", instance->irq);
391 } else {
392 shost_printk(KERN_INFO, instance,
393 "irq %d acquired\n", instance->irq);
Ondrej Zaryd91f5af2016-09-27 21:00:24 +0200394 }
395 }
396
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200397 ret = scsi_add_host(instance, pdev);
398 if (ret)
399 goto out_free_irq;
400 scsi_scan_host(instance);
401 dev_set_drvdata(pdev, instance);
402 return 0;
Finn Thain0ad0eff2016-01-03 16:05:21 +1100403
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200404out_free_irq:
405 if (instance->irq != NO_IRQ)
406 free_irq(instance->irq, instance);
407 NCR5380_exit(instance);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100408out_unregister:
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200409 scsi_host_put(instance);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400410out_unmap:
Finn Thain0ad0eff2016-01-03 16:05:21 +1100411 iounmap(iomem);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400412out_release:
413 if (is_pmio)
414 release_region(base, region_size);
415 else
416 release_mem_region(base, region_size);
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200420static void generic_NCR5380_release_resources(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400422 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain820682b2016-10-10 00:46:53 -0400423 void __iomem *iomem = hostdata->io;
424 unsigned long io_port = hostdata->io_port;
425 unsigned long base = hostdata->base;
426 unsigned long region_size = hostdata->region_size;
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400427
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200428 scsi_remove_host(instance);
Finn Thain22f5f102014-11-12 16:11:56 +1100429 if (instance->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500430 free_irq(instance->irq, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 NCR5380_exit(instance);
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200432 scsi_host_put(instance);
Finn Thain820682b2016-10-10 00:46:53 -0400433 iounmap(iomem);
434 if (io_port)
435 release_region(io_port, region_size);
436 else
437 release_mem_region(base, region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/**
Finn Thain6c4b88c2016-03-23 21:10:17 +1100441 * generic_NCR5380_pread - pseudo DMA read
Finn Thain4a98f892016-10-10 00:46:53 -0400442 * @hostdata: scsi host private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * @dst: buffer to read into
444 * @len: buffer length
445 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300446 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * controller
448 */
449
Finn Thain4a98f892016-10-10 00:46:53 -0400450static inline int generic_NCR5380_pread(struct NCR5380_hostdata *hostdata,
Finn Thain6c4b88c2016-03-23 21:10:17 +1100451 unsigned char *dst, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 int blocks = len / 128;
454 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Ondrej Zary12150792016-01-03 16:06:15 +1100456 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE | CSR_TRANS_DIR);
457 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100459 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
Ondrej Zary12150792016-01-03 16:06:15 +1100461 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
463 return -1;
464 }
Ondrej Zary12150792016-01-03 16:06:15 +1100465 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
466 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Finn Thain820682b2016-10-10 00:46:53 -0400468 if (hostdata->io_port && hostdata->io_width == 2)
469 insw(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100470 dst + start, 64);
Finn Thain820682b2016-10-10 00:46:53 -0400471 else if (hostdata->io_port)
472 insb(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100473 dst + start, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400474 else
475 memcpy_fromio(dst + start,
Finn Thain820682b2016-10-10 00:46:53 -0400476 hostdata->io + NCR53C400_host_buffer, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 start += 128;
479 blocks--;
480 }
481
482 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100483 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
484 ; /* FIXME - no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Finn Thain820682b2016-10-10 00:46:53 -0400486 if (hostdata->io_port && hostdata->io_width == 2)
487 insw(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100488 dst + start, 64);
Finn Thain820682b2016-10-10 00:46:53 -0400489 else if (hostdata->io_port)
490 insb(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100491 dst + start, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400492 else
493 memcpy_fromio(dst + start,
Finn Thain820682b2016-10-10 00:46:53 -0400494 hostdata->io + NCR53C400_host_buffer, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 start += 128;
497 blocks--;
498 }
499
Ondrej Zary12150792016-01-03 16:06:15 +1100500 if (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 printk("53C400r: no 53C80 gated irq after transfer");
502
Ondrej Zary42fc6372016-01-03 16:06:18 +1100503 /* wait for 53C80 registers to be available */
504 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ;
Ondrej Zary42fc6372016-01-03 16:06:18 +1100506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
508 printk(KERN_ERR "53C400r: no end dma signal\n");
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
511}
512
513/**
Finn Thain6c4b88c2016-03-23 21:10:17 +1100514 * generic_NCR5380_pwrite - pseudo DMA write
Finn Thain4a98f892016-10-10 00:46:53 -0400515 * @hostdata: scsi host private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * @dst: buffer to read into
517 * @len: buffer length
518 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300519 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * controller
521 */
522
Finn Thain4a98f892016-10-10 00:46:53 -0400523static inline int generic_NCR5380_pwrite(struct NCR5380_hostdata *hostdata,
Finn Thain6c4b88c2016-03-23 21:10:17 +1100524 unsigned char *src, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 int blocks = len / 128;
527 int start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Ondrej Zary12150792016-01-03 16:06:15 +1100529 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
530 NCR5380_write(hostdata->c400_blk_cnt, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 while (1) {
Ondrej Zary12150792016-01-03 16:06:15 +1100532 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
534 return -1;
535 }
536
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 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ; // FIXME - timeout
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400541
Finn Thain820682b2016-10-10 00:46:53 -0400542 if (hostdata->io_port && hostdata->io_width == 2)
543 outsw(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100544 src + start, 64);
Finn Thain820682b2016-10-10 00:46:53 -0400545 else if (hostdata->io_port)
546 outsb(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100547 src + start, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400548 else
Finn Thain820682b2016-10-10 00:46:53 -0400549 memcpy_toio(hostdata->io + NCR53C400_host_buffer,
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400550 src + start, 128);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 start += 128;
553 blocks--;
554 }
555 if (blocks) {
Ondrej Zary12150792016-01-03 16:06:15 +1100556 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 ; // FIXME - no timeout
558
Finn Thain820682b2016-10-10 00:46:53 -0400559 if (hostdata->io_port && hostdata->io_width == 2)
560 outsw(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100561 src + start, 64);
Finn Thain820682b2016-10-10 00:46:53 -0400562 else if (hostdata->io_port)
563 outsb(hostdata->io_port + hostdata->c400_host_buf,
Ondrej Zary12150792016-01-03 16:06:15 +1100564 src + start, 128);
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400565 else
Finn Thain820682b2016-10-10 00:46:53 -0400566 memcpy_toio(hostdata->io + NCR53C400_host_buffer,
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400567 src + start, 128);
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 start += 128;
570 blocks--;
571 }
572
Ondrej Zary42fc6372016-01-03 16:06:18 +1100573 /* wait for 53C80 registers to be available */
574 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG)) {
Ondrej Zaryaeb51152016-01-03 16:06:17 +1100575 udelay(4); /* DTC436 chip hangs without this */
576 /* FIXME - no timeout */
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
580 printk(KERN_ERR "53C400w: no end dma signal\n");
581 }
Ondrej Zary42fc6372016-01-03 16:06:18 +1100582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
584 ; // TIMEOUT
585 return 0;
586}
Finn Thainff3d4572016-01-03 16:05:25 +1100587
Finn Thain4a98f892016-10-10 00:46:53 -0400588static int generic_NCR5380_dma_xfer_len(struct NCR5380_hostdata *hostdata,
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100589 struct scsi_cmnd *cmd)
Finn Thainff3d4572016-01-03 16:05:25 +1100590{
591 int transfersize = cmd->transfersize;
592
Finn Thain7e9ec8d2016-03-23 21:10:11 +1100593 if (hostdata->flags & FLAG_NO_PSEUDO_DMA)
594 return 0;
595
Finn Thainff3d4572016-01-03 16:05:25 +1100596 /* Limit transfers to 32K, for xx400 & xx406
597 * pseudoDMA that transfers in 128 bytes blocks.
598 */
599 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
600 !(cmd->SCp.this_residual % transfersize))
601 transfersize = 32 * 1024;
602
Ondrej Zaryf0394622016-01-03 16:06:14 +1100603 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
604 if (transfersize % 128)
605 transfersize = 0;
606
Finn Thainff3d4572016-01-03 16:05:25 +1100607 return transfersize;
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
611 * Include the NCR5380 core code that we build our driver around
612 */
613
614#include "NCR5380.c"
615
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100616static struct scsi_host_template driver_template = {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200617 .module = THIS_MODULE,
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100618 .proc_name = DRV_MODULE_NAME,
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100619 .name = "Generic NCR5380/NCR53C400 SCSI",
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100620 .info = generic_NCR5380_info,
621 .queuecommand = generic_NCR5380_queue_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .eh_abort_handler = generic_NCR5380_abort,
623 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
Finn Thainaa2e2cb12016-01-03 16:05:48 +1100624 .can_queue = 16,
625 .this_id = 7,
626 .sg_tablesize = SG_ALL,
627 .cmd_per_lun = 2,
628 .use_clustering = DISABLE_CLUSTERING,
Finn Thain32b26a12016-01-03 16:05:58 +1100629 .cmd_size = NCR5380_CMD_SIZE,
Finn Thain0a4e3612016-01-03 16:06:07 +1100630 .max_sectors = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631};
Finn Thain161c0052016-01-03 16:05:46 +1100632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200634static int generic_NCR5380_isa_match(struct device *pdev, unsigned int ndev)
635{
636 int ret = generic_NCR5380_init_one(&driver_template, pdev, base[ndev],
637 irq[ndev], card[ndev]);
638 if (ret) {
639 if (base[ndev])
640 printk(KERN_WARNING "Card not found at address 0x%03x\n",
641 base[ndev]);
642 return 0;
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200645 return 1;
646}
647
648static int generic_NCR5380_isa_remove(struct device *pdev,
649 unsigned int ndev)
650{
651 generic_NCR5380_release_resources(dev_get_drvdata(pdev));
652 dev_set_drvdata(pdev, NULL);
653 return 0;
654}
655
656static struct isa_driver generic_NCR5380_isa_driver = {
657 .match = generic_NCR5380_isa_match,
658 .remove = generic_NCR5380_isa_remove,
659 .driver = {
660 .name = DRV_MODULE_NAME
661 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662};
663
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400664#ifdef CONFIG_PNP
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200665static struct pnp_device_id generic_NCR5380_pnp_ids[] = {
666 { .id = "DTC436e", .driver_data = BOARD_DTC3181E },
667 { .id = "" }
668};
669MODULE_DEVICE_TABLE(pnp, generic_NCR5380_pnp_ids);
670
671static int generic_NCR5380_pnp_probe(struct pnp_dev *pdev,
672 const struct pnp_device_id *id)
673{
674 int base, irq;
675
676 if (pnp_activate_dev(pdev) < 0)
677 return -EBUSY;
678
679 base = pnp_port_start(pdev, 0);
680 irq = pnp_irq(pdev, 0);
681
682 return generic_NCR5380_init_one(&driver_template, &pdev->dev, base, irq,
683 id->driver_data);
684}
685
686static void generic_NCR5380_pnp_remove(struct pnp_dev *pdev)
687{
688 generic_NCR5380_release_resources(pnp_get_drvdata(pdev));
689 pnp_set_drvdata(pdev, NULL);
690}
691
692static struct pnp_driver generic_NCR5380_pnp_driver = {
693 .name = DRV_MODULE_NAME,
694 .id_table = generic_NCR5380_pnp_ids,
695 .probe = generic_NCR5380_pnp_probe,
696 .remove = generic_NCR5380_pnp_remove,
697};
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400698#endif /* defined(CONFIG_PNP) */
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200699
700static int pnp_registered, isa_registered;
701
702static int __init generic_NCR5380_init(void)
703{
704 int ret = 0;
705
706 /* compatibility with old-style parameters */
Finn Thain70439e92016-12-05 01:07:20 -0500707 if (irq[0] == -1 && base[0] == 0 && card[0] == -1) {
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200708 irq[0] = ncr_irq;
709 base[0] = ncr_addr;
710 if (ncr_5380)
711 card[0] = BOARD_NCR5380;
712 if (ncr_53c400)
713 card[0] = BOARD_NCR53C400;
714 if (ncr_53c400a)
715 card[0] = BOARD_NCR53C400A;
716 if (dtc_3181e)
717 card[0] = BOARD_DTC3181E;
718 if (hp_c2502)
719 card[0] = BOARD_HP_C2502;
720 }
721
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400722#ifdef CONFIG_PNP
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200723 if (!pnp_register_driver(&generic_NCR5380_pnp_driver))
724 pnp_registered = 1;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700725#endif
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200726 ret = isa_register_driver(&generic_NCR5380_isa_driver, MAX_CARDS);
727 if (!ret)
728 isa_registered = 1;
729
730 return (pnp_registered || isa_registered) ? 0 : ret;
731}
732
733static void __exit generic_NCR5380_exit(void)
734{
Ondrej Zaryb61bacb2016-10-10 00:46:52 -0400735#ifdef CONFIG_PNP
Ondrej Zarya8cfbca2016-09-27 21:00:25 +0200736 if (pnp_registered)
737 pnp_unregister_driver(&generic_NCR5380_pnp_driver);
738#endif
739 if (isa_registered)
740 isa_unregister_driver(&generic_NCR5380_isa_driver);
741}
742
743module_init(generic_NCR5380_init);
744module_exit(generic_NCR5380_exit);