blob: c3b99c93637aaa1ec958fe0dde1c2b9998226ccb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic Generic NCR5380 driver
3 *
4 * Copyright 1995-2002, Russell King
5 */
6#include <linux/module.h>
7#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/ioport.h>
9#include <linux/delay.h>
10#include <linux/blkdev.h>
11#include <linux/init.h>
12
13#include <asm/ecard.h>
14#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include "../scsi.h"
17#include <scsi/scsi_host.h>
18
19#include <scsi/scsicam.h>
20
21#define AUTOSENSE
22#define PSEUDO_DMA
23
24#define CUMANASCSI_PUBLIC_RELEASE 1
25
Russell King8b801ea2007-07-20 10:38:54 +010026#define priv(host) ((struct NCR5380_hostdata *)(host)->hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define NCR5380_local_declare() struct Scsi_Host *_instance
28#define NCR5380_setup(instance) _instance = instance
29#define NCR5380_read(reg) cumanascsi_read(_instance, reg)
30#define NCR5380_write(reg, value) cumanascsi_write(_instance, reg, value)
31#define NCR5380_intr cumanascsi_intr
32#define NCR5380_queue_command cumanascsi_queue_command
33#define NCR5380_proc_info cumanascsi_proc_info
34
Russell King8b801ea2007-07-20 10:38:54 +010035#define NCR5380_implementation_fields \
36 unsigned ctrl; \
37 void __iomem *base; \
38 void __iomem *dma
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define BOARD_NORMAL 0
41#define BOARD_NCR53C400 1
42
43#include "../NCR5380.h"
44
45void cumanascsi_setup(char *str, int *ints)
46{
47}
48
49const char *cumanascsi_info(struct Scsi_Host *spnt)
50{
51 return "";
52}
53
Russell King8b801ea2007-07-20 10:38:54 +010054#define CTRL 0x16fc
55#define STAT 0x2004
56#define L(v) (((v)<<16)|((v) & 0x0000ffff))
57#define H(v) (((v)>>16)|((v) & 0xffff0000))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static inline int
Russell King8b801ea2007-07-20 10:38:54 +010060NCR5380_pwrite(struct Scsi_Host *host, unsigned char *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned long *laddr;
Russell King8b801ea2007-07-20 10:38:54 +010063 void __iomem *dma = priv(host)->dma + 0x2000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if(!len) return 0;
66
Russell King8b801ea2007-07-20 10:38:54 +010067 writeb(0x02, priv(host)->base + CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 laddr = (unsigned long *)addr;
69 while(len >= 32)
70 {
Russell King8b801ea2007-07-20 10:38:54 +010071 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long v;
Russell King8b801ea2007-07-20 10:38:54 +010073 status = readb(priv(host)->base + STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if(status & 0x80)
75 goto end;
76 if(!(status & 0x40))
77 continue;
Russell King8b801ea2007-07-20 10:38:54 +010078 v=*laddr++; writew(L(v), dma); writew(H(v), dma);
79 v=*laddr++; writew(L(v), dma); writew(H(v), dma);
80 v=*laddr++; writew(L(v), dma); writew(H(v), dma);
81 v=*laddr++; writew(L(v), dma); writew(H(v), dma);
82 v=*laddr++; writew(L(v), dma); writew(H(v), dma);
83 v=*laddr++; writew(L(v), dma); writew(H(v), dma);
84 v=*laddr++; writew(L(v), dma); writew(H(v), dma);
85 v=*laddr++; writew(L(v), dma); writew(H(v), dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 len -= 32;
87 if(len == 0)
88 break;
89 }
90
91 addr = (unsigned char *)laddr;
Russell King8b801ea2007-07-20 10:38:54 +010092 writeb(0x12, priv(host)->base + CTRL);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 while(len > 0)
95 {
Russell King8b801ea2007-07-20 10:38:54 +010096 unsigned int status;
97 status = readb(priv(host)->base + STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if(status & 0x80)
99 goto end;
100 if(status & 0x40)
101 {
Russell King8b801ea2007-07-20 10:38:54 +0100102 writeb(*addr++, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if(--len == 0)
104 break;
105 }
106
Russell King8b801ea2007-07-20 10:38:54 +0100107 status = readb(priv(host)->base + STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if(status & 0x80)
109 goto end;
110 if(status & 0x40)
111 {
Russell King8b801ea2007-07-20 10:38:54 +0100112 writeb(*addr++, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if(--len == 0)
114 break;
115 }
116 }
117end:
Russell King8b801ea2007-07-20 10:38:54 +0100118 writeb(priv(host)->ctrl | 0x40, priv(host)->base + CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return len;
120}
121
122static inline int
Russell King8b801ea2007-07-20 10:38:54 +0100123NCR5380_pread(struct Scsi_Host *host, unsigned char *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned long *laddr;
Russell King8b801ea2007-07-20 10:38:54 +0100126 void __iomem *dma = priv(host)->dma + 0x2000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if(!len) return 0;
129
Russell King8b801ea2007-07-20 10:38:54 +0100130 writeb(0x00, priv(host)->base + CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 laddr = (unsigned long *)addr;
132 while(len >= 32)
133 {
Russell King8b801ea2007-07-20 10:38:54 +0100134 unsigned int status;
135 status = readb(priv(host)->base + STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if(status & 0x80)
137 goto end;
138 if(!(status & 0x40))
139 continue;
Russell King8b801ea2007-07-20 10:38:54 +0100140 *laddr++ = readw(dma) | (readw(dma) << 16);
141 *laddr++ = readw(dma) | (readw(dma) << 16);
142 *laddr++ = readw(dma) | (readw(dma) << 16);
143 *laddr++ = readw(dma) | (readw(dma) << 16);
144 *laddr++ = readw(dma) | (readw(dma) << 16);
145 *laddr++ = readw(dma) | (readw(dma) << 16);
146 *laddr++ = readw(dma) | (readw(dma) << 16);
147 *laddr++ = readw(dma) | (readw(dma) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 len -= 32;
149 if(len == 0)
150 break;
151 }
152
153 addr = (unsigned char *)laddr;
Russell King8b801ea2007-07-20 10:38:54 +0100154 writeb(0x10, priv(host)->base + CTRL);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 while(len > 0)
157 {
Russell King8b801ea2007-07-20 10:38:54 +0100158 unsigned int status;
159 status = readb(priv(host)->base + STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if(status & 0x80)
161 goto end;
162 if(status & 0x40)
163 {
Russell King8b801ea2007-07-20 10:38:54 +0100164 *addr++ = readb(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if(--len == 0)
166 break;
167 }
168
Russell King8b801ea2007-07-20 10:38:54 +0100169 status = readb(priv(host)->base + STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if(status & 0x80)
171 goto end;
172 if(status & 0x40)
173 {
Russell King8b801ea2007-07-20 10:38:54 +0100174 *addr++ = readb(dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if(--len == 0)
176 break;
177 }
178 }
179end:
Russell King8b801ea2007-07-20 10:38:54 +0100180 writeb(priv(host)->ctrl | 0x40, priv(host)->base + CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return len;
182}
183
Russell King8b801ea2007-07-20 10:38:54 +0100184static unsigned char cumanascsi_read(struct Scsi_Host *host, unsigned int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Russell King8b801ea2007-07-20 10:38:54 +0100186 void __iomem *base = priv(host)->base;
187 unsigned char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Russell King8b801ea2007-07-20 10:38:54 +0100189 writeb(0, base + CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Russell King8b801ea2007-07-20 10:38:54 +0100191 val = readb(base + 0x2100 + (reg << 2));
192
193 priv(host)->ctrl = 0x40;
194 writeb(0x40, base + CTRL);
195
196 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Russell King8b801ea2007-07-20 10:38:54 +0100199static void cumanascsi_write(struct Scsi_Host *host, unsigned int reg, unsigned int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Russell King8b801ea2007-07-20 10:38:54 +0100201 void __iomem *base = priv(host)->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Russell King8b801ea2007-07-20 10:38:54 +0100203 writeb(0, base + CTRL);
204
205 writeb(value, base + 0x2100 + (reg << 2));
206
207 priv(host)->ctrl = 0x40;
208 writeb(0x40, base + CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#include "../NCR5380.c"
212
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100213static struct scsi_host_template cumanascsi_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .module = THIS_MODULE,
215 .name = "Cumana 16-bit SCSI",
216 .info = cumanascsi_info,
217 .queuecommand = cumanascsi_queue_command,
218 .eh_abort_handler = NCR5380_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 .eh_bus_reset_handler = NCR5380_bus_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .can_queue = 16,
221 .this_id = 7,
222 .sg_tablesize = SG_ALL,
223 .cmd_per_lun = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .use_clustering = DISABLE_CLUSTERING,
225 .proc_name = "CumanaSCSI-1",
226};
227
228static int __devinit
229cumanascsi1_probe(struct expansion_card *ec, const struct ecard_id *id)
230{
231 struct Scsi_Host *host;
Russell King8b801ea2007-07-20 10:38:54 +0100232 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Russell King8b801ea2007-07-20 10:38:54 +0100234 ret = ecard_request_resources(ec);
235 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto out;
237
Russell King8b801ea2007-07-20 10:38:54 +0100238 host = scsi_host_alloc(&cumanascsi_template, sizeof(struct NCR5380_hostdata));
239 if (!host) {
240 ret = -ENOMEM;
241 goto out_release;
242 }
243
244 priv(host)->base = ioremap(ecard_resource_start(ec, ECARD_RES_IOCSLOW),
245 ecard_resource_len(ec, ECARD_RES_IOCSLOW));
246 priv(host)->dma = ioremap(ecard_resource_start(ec, ECARD_RES_MEMC),
247 ecard_resource_len(ec, ECARD_RES_MEMC));
248 if (!priv(host)->base || !priv(host)->dma) {
249 ret = -ENOMEM;
250 goto out_unmap;
251 }
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 host->irq = ec->irq;
254
255 NCR5380_init(host, 0);
256
Russell King8b801ea2007-07-20 10:38:54 +0100257 priv(host)->ctrl = 0;
258 writeb(0, priv(host)->base + CTRL);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 host->n_io_port = 255;
261 if (!(request_region(host->io_port, host->n_io_port, "CumanaSCSI-1"))) {
262 ret = -EBUSY;
Russell King8b801ea2007-07-20 10:38:54 +0100263 goto out_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Thomas Gleixner1d6f3592006-07-01 19:29:42 -0700266 ret = request_irq(host->irq, cumanascsi_intr, IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 "CumanaSCSI-1", host);
268 if (ret) {
269 printk("scsi%d: IRQ%d not free: %d\n",
270 host->host_no, host->irq, ret);
Russell King8b801ea2007-07-20 10:38:54 +0100271 goto out_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 printk("scsi%d: at port 0x%08lx irq %d",
275 host->host_no, host->io_port, host->irq);
276 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
277 host->can_queue, host->cmd_per_lun, CUMANASCSI_PUBLIC_RELEASE);
278 printk("\nscsi%d:", host->host_no);
279 NCR5380_print_options(host);
280 printk("\n");
281
282 ret = scsi_add_host(host, &ec->dev);
283 if (ret)
284 goto out_free_irq;
285
286 scsi_scan_host(host);
287 goto out;
288
289 out_free_irq:
290 free_irq(host->irq, host);
Russell King8b801ea2007-07-20 10:38:54 +0100291 out_unmap:
292 iounmap(priv(host)->base);
293 iounmap(priv(host)->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 scsi_host_put(host);
Russell King8b801ea2007-07-20 10:38:54 +0100295 out_release:
296 ecard_release_resources(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 out:
298 return ret;
299}
300
301static void __devexit cumanascsi1_remove(struct expansion_card *ec)
302{
303 struct Scsi_Host *host = ecard_get_drvdata(ec);
304
305 ecard_set_drvdata(ec, NULL);
306
307 scsi_remove_host(host);
308 free_irq(host->irq, host);
309 NCR5380_exit(host);
Russell King8b801ea2007-07-20 10:38:54 +0100310 iounmap(priv(host)->base);
311 iounmap(priv(host)->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 scsi_host_put(host);
Russell King8b801ea2007-07-20 10:38:54 +0100313 ecard_release_resources(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316static const struct ecard_id cumanascsi1_cids[] = {
317 { MANU_CUMANA, PROD_CUMANA_SCSI_1 },
318 { 0xffff, 0xffff }
319};
320
321static struct ecard_driver cumanascsi1_driver = {
322 .probe = cumanascsi1_probe,
323 .remove = __devexit_p(cumanascsi1_remove),
324 .id_table = cumanascsi1_cids,
325 .drv = {
326 .name = "cumanascsi1",
327 },
328};
329
330static int __init cumanascsi_init(void)
331{
332 return ecard_register_driver(&cumanascsi1_driver);
333}
334
335static void __exit cumanascsi_exit(void)
336{
337 ecard_remove_driver(&cumanascsi1_driver);
338}
339
340module_init(cumanascsi_init);
341module_exit(cumanascsi_exit);
342
343MODULE_DESCRIPTION("Cumana SCSI-1 driver for Acorn machines");
344MODULE_LICENSE("GPL");