Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / drivers / scsi / arm / arxescsi.c
1 /*
2  * linux/arch/arm/drivers/scsi/arxescsi.c
3  *
4  * Copyright (C) 1997-2000 Russell King, Stefan Hanske
5  *
6  * This driver is based on experimentation.  Hence, it may have made
7  * assumptions about the particular card that I have available, and
8  * may not be reliable!
9  *
10  * Changelog:
11  *  30-08-1997  RMK     0.0.0   Created, READONLY version as cumana_2.c
12  *  22-01-1998  RMK     0.0.1   Updated to 2.1.80
13  *  15-04-1998  RMK     0.0.1   Only do PIO if FAS216 will allow it.
14  *  11-06-1998  SH      0.0.2   Changed to support ARXE 16-bit SCSI card
15  *                              enabled writing
16  *  01-01-2000  SH      0.1.0   Added *real* pseudo dma writing
17  *                              (arxescsi_pseudo_dma_write)
18  *  02-04-2000  RMK     0.1.1   Updated for new error handling code.
19  *  22-10-2000  SH              Updated for new registering scheme.
20  */
21 #include <linux/module.h>
22 #include <linux/blkdev.h>
23 #include <linux/kernel.h>
24 #include <linux/string.h>
25 #include <linux/ioport.h>
26 #include <linux/sched.h>
27 #include <linux/proc_fs.h>
28 #include <linux/unistd.h>
29 #include <linux/stat.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/interrupt.h>
33
34 #include <asm/dma.h>
35 #include <asm/io.h>
36 #include <asm/irq.h>
37 #include <asm/ecard.h>
38
39 #include "../scsi.h"
40 #include <scsi/scsi_host.h>
41 #include "fas216.h"
42
43 struct arxescsi_info {
44         FAS216_Info             info;
45         struct expansion_card   *ec;
46         void __iomem            *base;
47 };
48
49 #define DMADATA_OFFSET  (0x200)
50
51 #define DMASTAT_OFFSET  (0x600)
52 #define DMASTAT_DRQ     (1 << 0)
53
54 #define CSTATUS_IRQ     (1 << 0)
55
56 #define VERSION "1.10 (23/01/2003 2.5.57)"
57
58 /*
59  * Function: int arxescsi_dma_setup(host, SCpnt, direction, min_type)
60  * Purpose : initialises DMA/PIO
61  * Params  : host      - host
62  *           SCpnt     - command
63  *           direction - DMA on to/off of card
64  *           min_type  - minimum DMA support that we must have for this transfer
65  * Returns : 0 if we should not set CMD_WITHDMA for transfer info command
66  */
67 static fasdmatype_t
68 arxescsi_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
69                        fasdmadir_t direction, fasdmatype_t min_type)
70 {
71         /*
72          * We don't do real DMA
73          */
74         return fasdma_pseudo;
75 }
76
77 static void arxescsi_pseudo_dma_write(unsigned char *addr, void __iomem *base)
78 {
79        __asm__ __volatile__(
80        "               stmdb   sp!, {r0-r12}\n"
81        "               mov     r3, %0\n"
82        "               mov     r1, %1\n"
83        "               add     r2, r1, #512\n"
84        "               mov     r4, #256\n"
85        ".loop_1:       ldmia   r3!, {r6, r8, r10, r12}\n"
86        "               mov     r5, r6, lsl #16\n"
87        "               mov     r7, r8, lsl #16\n"
88        ".loop_2:       ldrb    r0, [r1, #1536]\n"
89        "               tst     r0, #1\n"
90        "               beq     .loop_2\n"
91        "               stmia   r2, {r5-r8}\n\t"
92        "               mov     r9, r10, lsl #16\n"
93        "               mov     r11, r12, lsl #16\n"
94        ".loop_3:       ldrb    r0, [r1, #1536]\n"
95        "               tst     r0, #1\n"
96        "               beq     .loop_3\n"
97        "               stmia   r2, {r9-r12}\n"
98        "               subs    r4, r4, #16\n"
99        "               bne     .loop_1\n"
100        "               ldmia   sp!, {r0-r12}\n"
101        :
102        : "r" (addr), "r" (base));
103 }
104
105 /*
106  * Function: int arxescsi_dma_pseudo(host, SCpnt, direction, transfer)
107  * Purpose : handles pseudo DMA
108  * Params  : host      - host
109  *           SCpnt     - command
110  *           direction - DMA on to/off of card
111  *           transfer  - minimum number of bytes we expect to transfer
112  */
113 static void
114 arxescsi_dma_pseudo(struct Scsi_Host *host, Scsi_Pointer *SCp,
115                     fasdmadir_t direction, int transfer)
116 {
117         struct arxescsi_info *info = (struct arxescsi_info *)host->hostdata;
118         unsigned int length, error = 0;
119         void __iomem *base = info->info.scsi.io_base;
120         unsigned char *addr;
121
122         length = SCp->this_residual;
123         addr = SCp->ptr;
124
125         if (direction == DMA_OUT) {
126                 unsigned int word;
127                 while (length > 256) {
128                         if (readb(base + 0x80) & STAT_INT) {
129                                 error = 1;
130                                 break;
131                         }
132                         arxescsi_pseudo_dma_write(addr, base);
133                         addr += 256;
134                         length -= 256;
135                 }
136
137                 if (!error)
138                         while (length > 0) {
139                                 if (readb(base + 0x80) & STAT_INT)
140                                         break;
141          
142                                 if (!(readb(base + DMASTAT_OFFSET) & DMASTAT_DRQ))
143                                         continue;
144
145                                 word = *addr | *(addr + 1) << 8;
146
147                                 writew(word, base + DMADATA_OFFSET);
148                                 if (length > 1) {
149                                         addr += 2;
150                                         length -= 2;
151                                 } else {
152                                         addr += 1;
153                                         length -= 1;
154                                 }
155                         }
156         }
157         else {
158                 if (transfer && (transfer & 255)) {
159                         while (length >= 256) {
160                                 if (readb(base + 0x80) & STAT_INT) {
161                                         error = 1;
162                                         break;
163                                 }
164             
165                                 if (!(readb(base + DMASTAT_OFFSET) & DMASTAT_DRQ))
166                                         continue;
167
168                                 readsw(base + DMADATA_OFFSET, addr, 256 >> 1);
169                                 addr += 256;
170                                 length -= 256;
171                         }
172                 }
173
174                 if (!(error))
175                         while (length > 0) {
176                                 unsigned long word;
177
178                                 if (readb(base + 0x80) & STAT_INT)
179                                         break;
180
181                                 if (!(readb(base + DMASTAT_OFFSET) & DMASTAT_DRQ))
182                                         continue;
183
184                                 word = readw(base + DMADATA_OFFSET);
185                                 *addr++ = word;
186                                 if (--length > 0) {
187                                         *addr++ = word >> 8;
188                                         length --;
189                                 }
190                         }
191         }
192 }
193
194 /*
195  * Function: int arxescsi_dma_stop(host, SCpnt)
196  * Purpose : stops DMA/PIO
197  * Params  : host  - host
198  *           SCpnt - command
199  */
200 static void arxescsi_dma_stop(struct Scsi_Host *host, Scsi_Pointer *SCp)
201 {
202         /*
203          * no DMA to stop
204          */
205 }
206
207 /*
208  * Function: const char *arxescsi_info(struct Scsi_Host * host)
209  * Purpose : returns a descriptive string about this interface,
210  * Params  : host - driver host structure to return info for.
211  * Returns : pointer to a static buffer containing null terminated string.
212  */
213 static const char *arxescsi_info(struct Scsi_Host *host)
214 {
215         struct arxescsi_info *info = (struct arxescsi_info *)host->hostdata;
216         static char string[150];
217
218         sprintf(string, "%s (%s) in slot %d v%s",
219                 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
220                 VERSION);
221
222         return string;
223 }
224
225 /*
226  * Function: int arxescsi_proc_info(char *buffer, char **start, off_t offset,
227  *                                       int length, int host_no, int inout)
228  * Purpose : Return information about the driver to a user process accessing
229  *           the /proc filesystem.
230  * Params  : buffer - a buffer to write information to
231  *           start  - a pointer into this buffer set by this routine to the start
232  *                    of the required information.
233  *           offset - offset into information that we have read upto.
234  *           length - length of buffer
235  *           host_no - host number to return information for
236  *           inout  - 0 for reading, 1 for writing.
237  * Returns : length of data written to buffer.
238  */
239 static int
240 arxescsi_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length,
241                    int inout)
242 {
243         struct arxescsi_info *info;
244         char *p = buffer;
245         int pos;
246
247         info = (struct arxescsi_info *)host->hostdata;
248         if (inout == 1)
249                 return -EINVAL;
250
251         p += sprintf(p, "ARXE 16-bit SCSI driver v%s\n", VERSION);
252         p += fas216_print_host(&info->info, p);
253         p += fas216_print_stats(&info->info, p);
254         p += fas216_print_devices(&info->info, p);
255
256         *start = buffer + offset;
257         pos = p - buffer - offset;
258         if (pos > length)
259                 pos = length;
260
261         return pos;
262 }
263
264 static Scsi_Host_Template arxescsi_template = {
265         .proc_info                      = arxescsi_proc_info,
266         .name                           = "ARXE SCSI card",
267         .info                           = arxescsi_info,
268         .queuecommand                   = fas216_noqueue_command,
269         .eh_host_reset_handler          = fas216_eh_host_reset,
270         .eh_bus_reset_handler           = fas216_eh_bus_reset,
271         .eh_device_reset_handler        = fas216_eh_device_reset,
272         .eh_abort_handler               = fas216_eh_abort,
273         .can_queue                      = 0,
274         .this_id                        = 7,
275         .sg_tablesize                   = SG_ALL,
276         .cmd_per_lun                    = 1,
277         .use_clustering                 = DISABLE_CLUSTERING,
278         .proc_name                      = "arxescsi",
279 };
280
281 static int __devinit
282 arxescsi_probe(struct expansion_card *ec, const struct ecard_id *id)
283 {
284         struct Scsi_Host *host;
285         struct arxescsi_info *info;
286         unsigned long resbase, reslen;
287         void __iomem *base;
288         int ret;
289
290         ret = ecard_request_resources(ec);
291         if (ret)
292                 goto out;
293
294         resbase = ecard_resource_start(ec, ECARD_RES_MEMC);
295         reslen = ecard_resource_len(ec, ECARD_RES_MEMC);
296         base = ioremap(resbase, reslen);
297         if (!base) {
298                 ret = -ENOMEM;
299                 goto out_region;
300         }
301
302         host = scsi_host_alloc(&arxescsi_template, sizeof(struct arxescsi_info));
303         if (!host) {
304                 ret = -ENOMEM;
305                 goto out_unmap;
306         }
307
308         info = (struct arxescsi_info *)host->hostdata;
309         info->ec = ec;
310         info->base = base;
311
312         info->info.scsi.io_base         = base + 0x2000;
313         info->info.scsi.irq             = NO_IRQ;
314         info->info.scsi.dma             = NO_DMA;
315         info->info.scsi.io_shift        = 5;
316         info->info.ifcfg.clockrate      = 24; /* MHz */
317         info->info.ifcfg.select_timeout = 255;
318         info->info.ifcfg.asyncperiod    = 200; /* ns */
319         info->info.ifcfg.sync_max_depth = 0;
320         info->info.ifcfg.cntl3          = CNTL3_FASTSCSI | CNTL3_FASTCLK;
321         info->info.ifcfg.disconnect_ok  = 0;
322         info->info.ifcfg.wide_max_size  = 0;
323         info->info.ifcfg.capabilities   = FASCAP_PSEUDODMA;
324         info->info.dma.setup            = arxescsi_dma_setup;
325         info->info.dma.pseudo           = arxescsi_dma_pseudo;
326         info->info.dma.stop             = arxescsi_dma_stop;
327                 
328         ec->irqaddr = base;
329         ec->irqmask = CSTATUS_IRQ;
330
331         ret = fas216_init(host);
332         if (ret)
333                 goto out_unregister;
334
335         ret = fas216_add(host, &ec->dev);
336         if (ret == 0)
337                 goto out;
338
339         fas216_release(host);
340  out_unregister:
341         scsi_host_put(host);
342  out_unmap:
343         iounmap(base);
344  out_region:
345         ecard_release_resources(ec);
346  out:
347         return ret;
348 }
349
350 static void __devexit arxescsi_remove(struct expansion_card *ec)
351 {
352         struct Scsi_Host *host = ecard_get_drvdata(ec);
353         struct arxescsi_info *info = (struct arxescsi_info *)host->hostdata;
354
355         ecard_set_drvdata(ec, NULL);
356         fas216_remove(host);
357
358         iounmap(info->base);
359
360         fas216_release(host);
361         scsi_host_put(host);
362         ecard_release_resources(ec);
363 }
364
365 static const struct ecard_id arxescsi_cids[] = {
366         { MANU_ARXE, PROD_ARXE_SCSI },
367         { 0xffff, 0xffff },
368 };
369
370 static struct ecard_driver arxescsi_driver = {
371         .probe          = arxescsi_probe,
372         .remove         = __devexit_p(arxescsi_remove),
373         .id_table       = arxescsi_cids,
374         .drv = {
375                 .name           = "arxescsi",
376         },
377 };
378
379 static int __init init_arxe_scsi_driver(void)
380 {
381         return ecard_register_driver(&arxescsi_driver);
382 }
383
384 static void __exit exit_arxe_scsi_driver(void)
385 {
386         ecard_remove_driver(&arxescsi_driver);
387 }
388
389 module_init(init_arxe_scsi_driver);
390 module_exit(exit_arxe_scsi_driver);
391
392 MODULE_AUTHOR("Stefan Hanske");
393 MODULE_DESCRIPTION("ARXESCSI driver for Acorn machines");
394 MODULE_LICENSE("GPL");
395