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