ncr5380: Adopt uniform DMA setup convention
[cascardo/linux.git] / drivers / scsi / arm / cumana_1.c
1 /*
2  * Generic Generic NCR5380 driver
3  *
4  * Copyright 1995-2002, Russell King
5  */
6 #include <linux/module.h>
7 #include <linux/ioport.h>
8 #include <linux/blkdev.h>
9 #include <linux/init.h>
10
11 #include <asm/ecard.h>
12 #include <asm/io.h>
13
14 #include <scsi/scsi_host.h>
15
16 #define priv(host)                      ((struct NCR5380_hostdata *)(host)->hostdata)
17 #define NCR5380_read(reg)               cumanascsi_read(instance, reg)
18 #define NCR5380_write(reg, value)       cumanascsi_write(instance, reg, value)
19
20 #define NCR5380_dma_xfer_len(instance, cmd, phase)      (cmd->transfersize)
21 #define NCR5380_dma_recv_setup          cumanascsi_pread
22 #define NCR5380_dma_send_setup          cumanascsi_pwrite
23
24 #define NCR5380_intr                    cumanascsi_intr
25 #define NCR5380_queue_command           cumanascsi_queue_command
26 #define NCR5380_info                    cumanascsi_info
27
28 #define NCR5380_implementation_fields   \
29         unsigned ctrl;                  \
30         void __iomem *base;             \
31         void __iomem *dma
32
33 #include "../NCR5380.h"
34
35 void cumanascsi_setup(char *str, int *ints)
36 {
37 }
38
39 #define CTRL    0x16fc
40 #define STAT    0x2004
41 #define L(v)    (((v)<<16)|((v) & 0x0000ffff))
42 #define H(v)    (((v)>>16)|((v) & 0xffff0000))
43
44 static inline int cumanascsi_pwrite(struct Scsi_Host *host,
45                                     unsigned char *addr, int len)
46 {
47   unsigned long *laddr;
48   void __iomem *dma = priv(host)->dma + 0x2000;
49
50   if(!len) return 0;
51
52   writeb(0x02, priv(host)->base + CTRL);
53   laddr = (unsigned long *)addr;
54   while(len >= 32)
55   {
56     unsigned int status;
57     unsigned long v;
58     status = readb(priv(host)->base + STAT);
59     if(status & 0x80)
60       goto end;
61     if(!(status & 0x40))
62       continue;
63     v=*laddr++; writew(L(v), dma); writew(H(v), dma);
64     v=*laddr++; writew(L(v), dma); writew(H(v), dma);
65     v=*laddr++; writew(L(v), dma); writew(H(v), dma);
66     v=*laddr++; writew(L(v), dma); writew(H(v), dma);
67     v=*laddr++; writew(L(v), dma); writew(H(v), dma);
68     v=*laddr++; writew(L(v), dma); writew(H(v), dma);
69     v=*laddr++; writew(L(v), dma); writew(H(v), dma);
70     v=*laddr++; writew(L(v), dma); writew(H(v), dma);
71     len -= 32;
72     if(len == 0)
73       break;
74   }
75
76   addr = (unsigned char *)laddr;
77   writeb(0x12, priv(host)->base + CTRL);
78
79   while(len > 0)
80   {
81     unsigned int status;
82     status = readb(priv(host)->base + STAT);
83     if(status & 0x80)
84       goto end;
85     if(status & 0x40)
86     {
87       writeb(*addr++, dma);
88       if(--len == 0)
89         break;
90     }
91
92     status = readb(priv(host)->base + STAT);
93     if(status & 0x80)
94       goto end;
95     if(status & 0x40)
96     {
97       writeb(*addr++, dma);
98       if(--len == 0)
99         break;
100     }
101   }
102 end:
103   writeb(priv(host)->ctrl | 0x40, priv(host)->base + CTRL);
104
105         if (len)
106                 return -1;
107         return 0;
108 }
109
110 static inline int cumanascsi_pread(struct Scsi_Host *host,
111                                    unsigned char *addr, int len)
112 {
113   unsigned long *laddr;
114   void __iomem *dma = priv(host)->dma + 0x2000;
115
116   if(!len) return 0;
117
118   writeb(0x00, priv(host)->base + CTRL);
119   laddr = (unsigned long *)addr;
120   while(len >= 32)
121   {
122     unsigned int status;
123     status = readb(priv(host)->base + STAT);
124     if(status & 0x80)
125       goto end;
126     if(!(status & 0x40))
127       continue;
128     *laddr++ = readw(dma) | (readw(dma) << 16);
129     *laddr++ = readw(dma) | (readw(dma) << 16);
130     *laddr++ = readw(dma) | (readw(dma) << 16);
131     *laddr++ = readw(dma) | (readw(dma) << 16);
132     *laddr++ = readw(dma) | (readw(dma) << 16);
133     *laddr++ = readw(dma) | (readw(dma) << 16);
134     *laddr++ = readw(dma) | (readw(dma) << 16);
135     *laddr++ = readw(dma) | (readw(dma) << 16);
136     len -= 32;
137     if(len == 0)
138       break;
139   }
140
141   addr = (unsigned char *)laddr;
142   writeb(0x10, priv(host)->base + CTRL);
143
144   while(len > 0)
145   {
146     unsigned int status;
147     status = readb(priv(host)->base + STAT);
148     if(status & 0x80)
149       goto end;
150     if(status & 0x40)
151     {
152       *addr++ = readb(dma);
153       if(--len == 0)
154         break;
155     }
156
157     status = readb(priv(host)->base + STAT);
158     if(status & 0x80)
159       goto end;
160     if(status & 0x40)
161     {
162       *addr++ = readb(dma);
163       if(--len == 0)
164         break;
165     }
166   }
167 end:
168   writeb(priv(host)->ctrl | 0x40, priv(host)->base + CTRL);
169
170         if (len)
171                 return -1;
172         return 0;
173 }
174
175 static unsigned char cumanascsi_read(struct Scsi_Host *host, unsigned int reg)
176 {
177         void __iomem *base = priv(host)->base;
178         unsigned char val;
179
180         writeb(0, base + CTRL);
181
182         val = readb(base + 0x2100 + (reg << 2));
183
184         priv(host)->ctrl = 0x40;
185         writeb(0x40, base + CTRL);
186
187         return val;
188 }
189
190 static void cumanascsi_write(struct Scsi_Host *host, unsigned int reg, unsigned int value)
191 {
192         void __iomem *base = priv(host)->base;
193
194         writeb(0, base + CTRL);
195
196         writeb(value, base + 0x2100 + (reg << 2));
197
198         priv(host)->ctrl = 0x40;
199         writeb(0x40, base + CTRL);
200 }
201
202 #include "../NCR5380.c"
203
204 static struct scsi_host_template cumanascsi_template = {
205         .module                 = THIS_MODULE,
206         .name                   = "Cumana 16-bit SCSI",
207         .info                   = cumanascsi_info,
208         .queuecommand           = cumanascsi_queue_command,
209         .eh_abort_handler       = NCR5380_abort,
210         .eh_bus_reset_handler   = NCR5380_bus_reset,
211         .can_queue              = 16,
212         .this_id                = 7,
213         .sg_tablesize           = SG_ALL,
214         .cmd_per_lun            = 2,
215         .use_clustering         = DISABLE_CLUSTERING,
216         .proc_name              = "CumanaSCSI-1",
217         .cmd_size               = NCR5380_CMD_SIZE,
218         .max_sectors            = 128,
219 };
220
221 static int cumanascsi1_probe(struct expansion_card *ec,
222                              const struct ecard_id *id)
223 {
224         struct Scsi_Host *host;
225         int ret;
226
227         ret = ecard_request_resources(ec);
228         if (ret)
229                 goto out;
230
231         host = scsi_host_alloc(&cumanascsi_template, sizeof(struct NCR5380_hostdata));
232         if (!host) {
233                 ret = -ENOMEM;
234                 goto out_release;
235         }
236
237         priv(host)->base = ioremap(ecard_resource_start(ec, ECARD_RES_IOCSLOW),
238                                    ecard_resource_len(ec, ECARD_RES_IOCSLOW));
239         priv(host)->dma = ioremap(ecard_resource_start(ec, ECARD_RES_MEMC),
240                                   ecard_resource_len(ec, ECARD_RES_MEMC));
241         if (!priv(host)->base || !priv(host)->dma) {
242                 ret = -ENOMEM;
243                 goto out_unmap;
244         }
245
246         host->irq = ec->irq;
247
248         ret = NCR5380_init(host, FLAG_DMA_FIXUP);
249         if (ret)
250                 goto out_unmap;
251
252         NCR5380_maybe_reset_bus(host);
253
254         priv(host)->ctrl = 0;
255         writeb(0, priv(host)->base + CTRL);
256
257         ret = request_irq(host->irq, cumanascsi_intr, 0,
258                           "CumanaSCSI-1", host);
259         if (ret) {
260                 printk("scsi%d: IRQ%d not free: %d\n",
261                     host->host_no, host->irq, ret);
262                 goto out_exit;
263         }
264
265         ret = scsi_add_host(host, &ec->dev);
266         if (ret)
267                 goto out_free_irq;
268
269         scsi_scan_host(host);
270         goto out;
271
272  out_free_irq:
273         free_irq(host->irq, host);
274  out_exit:
275         NCR5380_exit(host);
276  out_unmap:
277         iounmap(priv(host)->base);
278         iounmap(priv(host)->dma);
279         scsi_host_put(host);
280  out_release:
281         ecard_release_resources(ec);
282  out:
283         return ret;
284 }
285
286 static void cumanascsi1_remove(struct expansion_card *ec)
287 {
288         struct Scsi_Host *host = ecard_get_drvdata(ec);
289
290         ecard_set_drvdata(ec, NULL);
291
292         scsi_remove_host(host);
293         free_irq(host->irq, host);
294         NCR5380_exit(host);
295         iounmap(priv(host)->base);
296         iounmap(priv(host)->dma);
297         scsi_host_put(host);
298         ecard_release_resources(ec);
299 }
300
301 static const struct ecard_id cumanascsi1_cids[] = {
302         { MANU_CUMANA, PROD_CUMANA_SCSI_1 },
303         { 0xffff, 0xffff }
304 };
305
306 static struct ecard_driver cumanascsi1_driver = {
307         .probe          = cumanascsi1_probe,
308         .remove         = cumanascsi1_remove,
309         .id_table       = cumanascsi1_cids,
310         .drv = {
311                 .name           = "cumanascsi1",
312         },
313 };
314
315 static int __init cumanascsi_init(void)
316 {
317         return ecard_register_driver(&cumanascsi1_driver);
318 }
319
320 static void __exit cumanascsi_exit(void)
321 {
322         ecard_remove_driver(&cumanascsi1_driver);
323 }
324
325 module_init(cumanascsi_init);
326 module_exit(cumanascsi_exit);
327
328 MODULE_DESCRIPTION("Cumana SCSI-1 driver for Acorn machines");
329 MODULE_LICENSE("GPL");