ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / arm / cumana_2.c
1 /*
2  *  linux/drivers/acorn/scsi/cumana_2.c
3  *
4  *  Copyright (C) 1997-2002 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Changelog:
11  *   30-08-1997 RMK     0.0.0   Created, READONLY version.
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  *   02-05-1998 RMK     0.0.2   Updated & added DMA support.
15  *   27-06-1998 RMK             Changed asm/delay.h to linux/delay.h
16  *   18-08-1998 RMK     0.0.3   Fixed synchronous transfer depth.
17  *   02-04-2000 RMK     0.0.4   Updated for new error handling code.
18  */
19 #include <linux/module.h>
20 #include <linux/blkdev.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/proc_fs.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/dma-mapping.h>
30
31 #include <asm/dma.h>
32 #include <asm/ecard.h>
33 #include <asm/io.h>
34 #include <asm/irq.h>
35 #include <asm/pgtable.h>
36
37 #include "../scsi.h"
38 #include "../hosts.h"
39 #include "fas216.h"
40 #include "scsi.h"
41
42 #include <scsi/scsicam.h>
43
44 #define CUMANASCSI2_STATUS              (0x0000)
45 #define STATUS_INT                      (1 << 0)
46 #define STATUS_DRQ                      (1 << 1)
47 #define STATUS_LATCHED                  (1 << 3)
48
49 #define CUMANASCSI2_ALATCH              (0x0014)
50 #define ALATCH_ENA_INT                  (3)
51 #define ALATCH_DIS_INT                  (2)
52 #define ALATCH_ENA_TERM                 (5)
53 #define ALATCH_DIS_TERM                 (4)
54 #define ALATCH_ENA_BIT32                (11)
55 #define ALATCH_DIS_BIT32                (10)
56 #define ALATCH_ENA_DMA                  (13)
57 #define ALATCH_DIS_DMA                  (12)
58 #define ALATCH_DMA_OUT                  (15)
59 #define ALATCH_DMA_IN                   (14)
60
61 #define CUMANASCSI2_PSEUDODMA           (0x0200)
62
63 #define CUMANASCSI2_FAS216_OFFSET       (0x0300)
64 #define CUMANASCSI2_FAS216_SHIFT        2
65
66 /*
67  * Version
68  */
69 #define VERSION "1.00 (13/11/2002 2.5.47)"
70
71 /*
72  * Use term=0,1,0,0,0 to turn terminators on/off
73  */
74 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
75
76 #define NR_SG   256
77
78 struct cumanascsi2_info {
79         FAS216_Info             info;
80         struct expansion_card   *ec;
81
82         void                    *status;        /* card status register */
83         void                    *alatch;        /* Control register     */
84         unsigned int            terms;          /* Terminator state     */
85         void                    *dmaarea;       /* Pseudo DMA area      */
86         struct scatterlist      sg[NR_SG];      /* Scatter DMA list     */
87 };
88
89 #define CSTATUS_IRQ     (1 << 0)
90 #define CSTATUS_DRQ     (1 << 1)
91
92 /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
93  * Purpose  : Enable interrupts on Cumana SCSI 2 card
94  * Params   : ec    - expansion card structure
95  *          : irqnr - interrupt number
96  */
97 static void
98 cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
99 {
100         writeb(ALATCH_ENA_INT, ec->irq_data);
101 }
102
103 /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
104  * Purpose  : Disable interrupts on Cumana SCSI 2 card
105  * Params   : ec    - expansion card structure
106  *          : irqnr - interrupt number
107  */
108 static void
109 cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
110 {
111         writeb(ALATCH_DIS_INT, ec->irq_data);
112 }
113
114 static const expansioncard_ops_t cumanascsi_2_ops = {
115         .irqenable      = cumanascsi_2_irqenable,
116         .irqdisable     = cumanascsi_2_irqdisable,
117 };
118
119 /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
120  * Purpose  : Turn the Cumana SCSI 2 terminators on or off
121  * Params   : host   - card to turn on/off
122  *          : on_off - !0 to turn on, 0 to turn off
123  */
124 static void
125 cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
126 {
127         struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
128
129         if (on_off) {
130                 info->terms = 1;
131                 writeb(ALATCH_ENA_TERM, info->alatch);
132         } else {
133                 info->terms = 0;
134                 writeb(ALATCH_DIS_TERM, info->alatch);
135         }
136 }
137
138 /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
139  * Purpose  : handle interrupts from Cumana SCSI 2 card
140  * Params   : irq    - interrupt number
141  *            dev_id - user-defined (Scsi_Host structure)
142  *            regs   - processor registers at interrupt
143  */
144 static irqreturn_t
145 cumanascsi_2_intr(int irq, void *dev_id, struct pt_regs *regs)
146 {
147         struct cumanascsi2_info *info = dev_id;
148
149         return fas216_intr(&info->info);
150 }
151
152 /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
153  * Purpose  : initialises DMA/PIO
154  * Params   : host      - host
155  *            SCpnt     - command
156  *            direction - DMA on to/off of card
157  *            min_type  - minimum DMA support that we must have for this transfer
158  * Returns  : type of transfer to be performed
159  */
160 static fasdmatype_t
161 cumanascsi_2_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
162                        fasdmadir_t direction, fasdmatype_t min_type)
163 {
164         struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
165         struct device *dev = scsi_get_device(host);
166         int dmach = host->dma_channel;
167
168         writeb(ALATCH_DIS_DMA, info->alatch);
169
170         if (dmach != NO_DMA &&
171             (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
172                 int bufs, map_dir, dma_dir, alatch_dir;
173
174                 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
175
176                 if (direction == DMA_OUT)
177                         map_dir = DMA_TO_DEVICE,
178                         dma_dir = DMA_MODE_WRITE,
179                         alatch_dir = ALATCH_DMA_OUT;
180                 else
181                         map_dir = DMA_FROM_DEVICE,
182                         dma_dir = DMA_MODE_READ,
183                         alatch_dir = ALATCH_DMA_IN;
184
185                 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
186
187                 disable_dma(dmach);
188                 set_dma_sg(dmach, info->sg, bufs + 1);
189                 writeb(alatch_dir, info->alatch);
190                 set_dma_mode(dmach, dma_dir);
191                 enable_dma(dmach);
192                 writeb(ALATCH_ENA_DMA, info->alatch);
193                 writeb(ALATCH_DIS_BIT32, info->alatch);
194                 return fasdma_real_all;
195         }
196
197         /*
198          * If we're not doing DMA,
199          *  we'll do pseudo DMA
200          */
201         return fasdma_pio;
202 }
203
204 /*
205  * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
206  * Purpose  : handles pseudo DMA
207  * Params   : host      - host
208  *            SCpnt     - command
209  *            direction - DMA on to/off of card
210  *            transfer  - minimum number of bytes we expect to transfer
211  */
212 static void
213 cumanascsi_2_dma_pseudo(struct Scsi_Host *host, Scsi_Pointer *SCp,
214                         fasdmadir_t direction, int transfer)
215 {
216         struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
217         unsigned int length;
218         unsigned char *addr;
219
220         length = SCp->this_residual;
221         addr = SCp->ptr;
222
223         if (direction == DMA_OUT)
224 #if 0
225                 while (length > 1) {
226                         unsigned long word;
227                         unsigned int status = readb(info->status);
228
229                         if (status & STATUS_INT)
230                                 goto end;
231
232                         if (!(status & STATUS_DRQ))
233                                 continue;
234
235                         word = *addr | *(addr + 1) << 8;
236                         writew(word, info->dmaarea);
237                         addr += 2;
238                         length -= 2;
239                 }
240 #else
241                 printk ("PSEUDO_OUT???\n");
242 #endif
243         else {
244                 if (transfer && (transfer & 255)) {
245                         while (length >= 256) {
246                                 unsigned int status = readb(info->status);
247
248                                 if (status & STATUS_INT)
249                                         return;
250             
251                                 if (!(status & STATUS_DRQ))
252                                         continue;
253
254                                 readsw(info->dmaarea, addr, 256 >> 1);
255                                 addr += 256;
256                                 length -= 256;
257                         }
258                 }
259
260                 while (length > 0) {
261                         unsigned long word;
262                         unsigned int status = readb(info->status);
263
264                         if (status & STATUS_INT)
265                                 return;
266
267                         if (!(status & STATUS_DRQ))
268                                 continue;
269
270                         word = readw(info->dmaarea);
271                         *addr++ = word;
272                         if (--length > 0) {
273                                 *addr++ = word >> 8;
274                                 length --;
275                         }
276                 }
277         }
278 }
279
280 /* Prototype: int cumanascsi_2_dma_stop(host, SCpnt)
281  * Purpose  : stops DMA/PIO
282  * Params   : host  - host
283  *            SCpnt - command
284  */
285 static void
286 cumanascsi_2_dma_stop(struct Scsi_Host *host, Scsi_Pointer *SCp)
287 {
288         struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
289         if (host->dma_channel != NO_DMA) {
290                 writeb(ALATCH_DIS_DMA, info->alatch);
291                 disable_dma(host->dma_channel);
292         }
293 }
294
295 /* Prototype: const char *cumanascsi_2_info(struct Scsi_Host * host)
296  * Purpose  : returns a descriptive string about this interface,
297  * Params   : host - driver host structure to return info for.
298  * Returns  : pointer to a static buffer containing null terminated string.
299  */
300 const char *cumanascsi_2_info(struct Scsi_Host *host)
301 {
302         struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
303         static char string[150];
304
305         sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
306                 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
307                 VERSION, info->terms ? "n" : "ff");
308
309         return string;
310 }
311
312 /* Prototype: int cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
313  * Purpose  : Set a driver specific function
314  * Params   : host   - host to setup
315  *          : buffer - buffer containing string describing operation
316  *          : length - length of string
317  * Returns  : -EINVAL, or 0
318  */
319 static int
320 cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
321 {
322         int ret = length;
323
324         if (length >= 11 && strcmp(buffer, "CUMANASCSI2") == 0) {
325                 buffer += 11;
326                 length -= 11;
327
328                 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
329                         if (buffer[5] == '1')
330                                 cumanascsi_2_terminator_ctl(host, 1);
331                         else if (buffer[5] == '0')
332                                 cumanascsi_2_terminator_ctl(host, 0);
333                         else
334                                 ret = -EINVAL;
335                 } else
336                         ret = -EINVAL;
337         } else
338                 ret = -EINVAL;
339
340         return ret;
341 }
342
343 /* Prototype: int cumanascsi_2_proc_info(char *buffer, char **start, off_t offset,
344  *                                       int length, int host_no, int inout)
345  * Purpose  : Return information about the driver to a user process accessing
346  *            the /proc filesystem.
347  * Params   : buffer - a buffer to write information to
348  *            start  - a pointer into this buffer set by this routine to the start
349  *                     of the required information.
350  *            offset - offset into information that we have read upto.
351  *            length - length of buffer
352  *            host_no - host number to return information for
353  *            inout  - 0 for reading, 1 for writing.
354  * Returns  : length of data written to buffer.
355  */
356 int cumanascsi_2_proc_info (struct Scsi_Host *host, char *buffer, char **start, off_t offset,
357                             int length, int inout)
358 {
359         struct cumanascsi2_info *info;
360         char *p = buffer;
361         int pos;
362
363         if (inout == 1)
364                 return cumanascsi_2_set_proc_info(host, buffer, length);
365
366         info = (struct cumanascsi2_info *)host->hostdata;
367
368         p += sprintf(p, "Cumana SCSI II driver v%s\n", VERSION);
369         p += fas216_print_host(&info->info, p);
370         p += sprintf(p, "Term    : o%s\n",
371                         info->terms ? "n" : "ff");
372
373         p += fas216_print_stats(&info->info, p);
374         p += fas216_print_devices(&info->info, p);
375
376         *start = buffer + offset;
377         pos = p - buffer - offset;
378         if (pos > length)
379                 pos = length;
380
381         return pos;
382 }
383
384 static Scsi_Host_Template cumanascsi2_template = {
385         .module                         = THIS_MODULE,
386         .proc_info                      = cumanascsi_2_proc_info,
387         .name                           = "Cumana SCSI II",
388         .info                           = cumanascsi_2_info,
389         .queuecommand                   = fas216_queue_command,
390         .eh_host_reset_handler          = fas216_eh_host_reset,
391         .eh_bus_reset_handler           = fas216_eh_bus_reset,
392         .eh_device_reset_handler        = fas216_eh_device_reset,
393         .eh_abort_handler               = fas216_eh_abort,
394         .can_queue                      = 1,
395         .this_id                        = 7,
396         .sg_tablesize                   = SG_ALL,
397         .cmd_per_lun                    = 1,
398         .use_clustering                 = DISABLE_CLUSTERING,
399         .proc_name                      = "cumanascsi2",
400 };
401
402 static int __devinit
403 cumanascsi2_probe(struct expansion_card *ec, const struct ecard_id *id)
404 {
405         struct Scsi_Host *host;
406         struct cumanascsi2_info *info;
407         unsigned long resbase, reslen;
408         unsigned char *base;
409         int ret;
410
411         resbase = ecard_resource_start(ec, ECARD_RES_MEMC);
412         reslen = ecard_resource_len(ec, ECARD_RES_MEMC);
413
414         if (!request_mem_region(resbase, reslen, "cumanascsi2")) {
415                 ret = -EBUSY;
416                 goto out;
417         }
418
419         base = ioremap(resbase, reslen);
420         if (!base) {
421                 ret = -ENOMEM;
422                 goto out_region;
423         }
424
425         host = scsi_host_alloc(&cumanascsi2_template,
426                                sizeof(struct cumanascsi2_info));
427         if (!host) {
428                 ret = -ENOMEM;
429                 goto out_unmap;
430         }
431
432         host->base        = (unsigned long)base;
433         host->irq         = ec->irq;
434         host->dma_channel = ec->dma;
435
436         ecard_set_drvdata(ec, host);
437
438         info = (struct cumanascsi2_info *)host->hostdata;
439         info->ec        = ec;
440         info->dmaarea   = base + CUMANASCSI2_PSEUDODMA;
441         info->status    = base + CUMANASCSI2_STATUS;
442         info->alatch    = base + CUMANASCSI2_ALATCH;
443
444         ec->irqaddr     = info->status;
445         ec->irqmask     = STATUS_INT;
446         ec->irq_data    = base + CUMANASCSI2_ALATCH;
447         ec->ops         = &cumanascsi_2_ops;
448
449         cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
450
451         info->info.scsi.io_base         = base + CUMANASCSI2_FAS216_OFFSET;
452         info->info.scsi.io_shift        = CUMANASCSI2_FAS216_SHIFT;
453         info->info.scsi.irq             = host->irq;
454         info->info.ifcfg.clockrate      = 40; /* MHz */
455         info->info.ifcfg.select_timeout = 255;
456         info->info.ifcfg.asyncperiod    = 200; /* ns */
457         info->info.ifcfg.sync_max_depth = 7;
458         info->info.ifcfg.cntl3          = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
459         info->info.ifcfg.disconnect_ok  = 1;
460         info->info.ifcfg.wide_max_size  = 0;
461         info->info.ifcfg.capabilities   = FASCAP_PSEUDODMA;
462         info->info.dma.setup            = cumanascsi_2_dma_setup;
463         info->info.dma.pseudo           = cumanascsi_2_dma_pseudo;
464         info->info.dma.stop             = cumanascsi_2_dma_stop;
465
466         ret = fas216_init(host);
467         if (ret)
468                 goto out_free;
469
470         ret = request_irq(host->irq, cumanascsi_2_intr,
471                           SA_INTERRUPT, "cumanascsi2", info);
472         if (ret) {
473                 printk("scsi%d: IRQ%d not free: %d\n",
474                        host->host_no, host->irq, ret);
475                 goto out_release;
476         }
477
478         if (host->dma_channel != NO_DMA) {
479                 if (request_dma(host->dma_channel, "cumanascsi2")) {
480                         printk("scsi%d: DMA%d not free, using PIO\n",
481                                host->host_no, host->dma_channel);
482                         host->dma_channel = NO_DMA;
483                 } else {
484                         set_dma_speed(host->dma_channel, 180);
485                         info->info.ifcfg.capabilities |= FASCAP_DMA;
486                 }
487         }
488
489         ret = fas216_add(host, &ec->dev);
490         if (ret == 0)
491                 goto out;
492
493         if (host->dma_channel != NO_DMA)
494                 free_dma(host->dma_channel);
495         free_irq(host->irq, host);
496
497  out_release:
498         fas216_release(host);
499
500  out_free:
501         scsi_host_put(host);
502
503  out_unmap:
504         iounmap(base);
505
506  out_region:
507         release_mem_region(resbase, reslen);
508
509  out:
510         return ret;
511 }
512
513 static void __devexit cumanascsi2_remove(struct expansion_card *ec)
514 {
515         struct Scsi_Host *host = ecard_get_drvdata(ec);
516         struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
517         unsigned long resbase, reslen;
518
519         ecard_set_drvdata(ec, NULL);
520         fas216_remove(host);
521
522         if (host->dma_channel != NO_DMA)
523                 free_dma(host->dma_channel);
524         free_irq(host->irq, info);
525
526         iounmap((void *)host->base);
527
528         resbase = ecard_resource_start(ec, ECARD_RES_MEMC);
529         reslen = ecard_resource_len(ec, ECARD_RES_MEMC);
530
531         release_mem_region(resbase, reslen);
532
533         fas216_release(host);
534         scsi_host_put(host);
535 }
536
537 static const struct ecard_id cumanascsi2_cids[] = {
538         { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
539         { 0xffff, 0xffff },
540 };
541
542 static struct ecard_driver cumanascsi2_driver = {
543         .probe          = cumanascsi2_probe,
544         .remove         = __devexit_p(cumanascsi2_remove),
545         .id_table       = cumanascsi2_cids,
546         .drv = {
547                 .name           = "cumanascsi2",
548         },
549 };
550
551 static int __init cumanascsi2_init(void)
552 {
553         return ecard_register_driver(&cumanascsi2_driver);
554 }
555
556 static void __exit cumanascsi2_exit(void)
557 {
558         ecard_remove_driver(&cumanascsi2_driver);
559 }
560
561 module_init(cumanascsi2_init);
562 module_exit(cumanascsi2_exit);
563
564 MODULE_AUTHOR("Russell King");
565 MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
566 MODULE_PARM(term, "1-8i");
567 MODULE_PARM_DESC(term, "SCSI bus termination");
568 MODULE_LICENSE("GPL");