Merge branch 'master' into upstream-fixes
[sfrench/cifs-2.6.git] / drivers / scsi / arm / eesox.c
1 /*
2  *  linux/drivers/acorn/scsi/eesox.c
3  *
4  *  Copyright (C) 1997-2005 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  *  This driver is based on experimentation.  Hence, it may have made
11  *  assumptions about the particular card that I have available, and
12  *  may not be reliable!
13  *
14  *  Changelog:
15  *   01-10-1997 RMK             Created, READONLY version
16  *   15-02-1998 RMK             READ/WRITE version
17  *                              added DMA support and hardware definitions
18  *   14-03-1998 RMK             Updated DMA support
19  *                              Added terminator control
20  *   15-04-1998 RMK             Only do PIO if FAS216 will allow it.
21  *   27-06-1998 RMK             Changed asm/delay.h to linux/delay.h
22  *   02-04-2000 RMK     0.0.3   Fixed NO_IRQ/NO_DMA problem, updated for new
23  *                              error handling code.
24  */
25 #include <linux/module.h>
26 #include <linux/blkdev.h>
27 #include <linux/kernel.h>
28 #include <linux/string.h>
29 #include <linux/ioport.h>
30 #include <linux/sched.h>
31 #include <linux/proc_fs.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
34 #include <linux/init.h>
35 #include <linux/dma-mapping.h>
36
37 #include <asm/io.h>
38 #include <asm/dma.h>
39 #include <asm/ecard.h>
40 #include <asm/pgtable.h>
41
42 #include "../scsi.h"
43 #include <scsi/scsi_host.h>
44 #include "fas216.h"
45 #include "scsi.h"
46
47 #include <scsi/scsicam.h>
48
49 #define EESOX_FAS216_OFFSET     0x3000
50 #define EESOX_FAS216_SHIFT      5
51
52 #define EESOX_DMASTAT           0x2800
53 #define EESOX_STAT_INTR         0x01
54 #define EESOX_STAT_DMA          0x02
55
56 #define EESOX_CONTROL           0x2800
57 #define EESOX_INTR_ENABLE       0x04
58 #define EESOX_TERM_ENABLE       0x02
59 #define EESOX_RESET             0x01
60
61 #define EESOX_DMADATA           0x3800
62
63 #define VERSION "1.10 (17/01/2003 2.5.59)"
64
65 /*
66  * Use term=0,1,0,0,0 to turn terminators on/off
67  */
68 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
69
70 #define NR_SG   256
71
72 struct eesoxscsi_info {
73         FAS216_Info             info;
74         struct expansion_card   *ec;
75         void __iomem            *base;
76         void __iomem            *ctl_port;
77         unsigned int            control;
78         struct scatterlist      sg[NR_SG];      /* Scatter DMA list     */
79 };
80
81 /* Prototype: void eesoxscsi_irqenable(ec, irqnr)
82  * Purpose  : Enable interrupts on EESOX SCSI card
83  * Params   : ec    - expansion card structure
84  *          : irqnr - interrupt number
85  */
86 static void
87 eesoxscsi_irqenable(struct expansion_card *ec, int irqnr)
88 {
89         struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
90
91         info->control |= EESOX_INTR_ENABLE;
92
93         writeb(info->control, info->ctl_port);
94 }
95
96 /* Prototype: void eesoxscsi_irqdisable(ec, irqnr)
97  * Purpose  : Disable interrupts on EESOX SCSI card
98  * Params   : ec    - expansion card structure
99  *          : irqnr - interrupt number
100  */
101 static void
102 eesoxscsi_irqdisable(struct expansion_card *ec, int irqnr)
103 {
104         struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
105
106         info->control &= ~EESOX_INTR_ENABLE;
107
108         writeb(info->control, info->ctl_port);
109 }
110
111 static const expansioncard_ops_t eesoxscsi_ops = {
112         .irqenable      = eesoxscsi_irqenable,
113         .irqdisable     = eesoxscsi_irqdisable,
114 };
115
116 /* Prototype: void eesoxscsi_terminator_ctl(*host, on_off)
117  * Purpose  : Turn the EESOX SCSI terminators on or off
118  * Params   : host   - card to turn on/off
119  *          : on_off - !0 to turn on, 0 to turn off
120  */
121 static void
122 eesoxscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
123 {
124         struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
125         unsigned long flags;
126
127         spin_lock_irqsave(host->host_lock, flags);
128         if (on_off)
129                 info->control |= EESOX_TERM_ENABLE;
130         else
131                 info->control &= ~EESOX_TERM_ENABLE;
132
133         writeb(info->control, info->ctl_port);
134         spin_unlock_irqrestore(host->host_lock, flags);
135 }
136
137 /* Prototype: void eesoxscsi_intr(irq, *dev_id, *regs)
138  * Purpose  : handle interrupts from EESOX SCSI card
139  * Params   : irq    - interrupt number
140  *            dev_id - user-defined (Scsi_Host structure)
141  */
142 static irqreturn_t
143 eesoxscsi_intr(int irq, void *dev_id)
144 {
145         struct eesoxscsi_info *info = dev_id;
146
147         return fas216_intr(&info->info);
148 }
149
150 /* Prototype: fasdmatype_t eesoxscsi_dma_setup(host, SCpnt, direction, min_type)
151  * Purpose  : initialises DMA/PIO
152  * Params   : host      - host
153  *            SCpnt     - command
154  *            direction - DMA on to/off of card
155  *            min_type  - minimum DMA support that we must have for this transfer
156  * Returns  : type of transfer to be performed
157  */
158 static fasdmatype_t
159 eesoxscsi_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
160                        fasdmadir_t direction, fasdmatype_t min_type)
161 {
162         struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
163         struct device *dev = scsi_get_device(host);
164         int dmach = info->info.scsi.dma;
165
166         if (dmach != NO_DMA &&
167             (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
168                 int bufs, map_dir, dma_dir;
169
170                 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
171
172                 if (direction == DMA_OUT)
173                         map_dir = DMA_TO_DEVICE,
174                         dma_dir = DMA_MODE_WRITE;
175                 else
176                         map_dir = DMA_FROM_DEVICE,
177                         dma_dir = DMA_MODE_READ;
178
179                 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
180
181                 disable_dma(dmach);
182                 set_dma_sg(dmach, info->sg, bufs + 1);
183                 set_dma_mode(dmach, dma_dir);
184                 enable_dma(dmach);
185                 return fasdma_real_all;
186         }
187         /*
188          * We don't do DMA, we only do slow PIO
189          *
190          * Some day, we will do Pseudo DMA
191          */
192         return fasdma_pseudo;
193 }
194
195 static void eesoxscsi_buffer_in(void *buf, int length, void __iomem *base)
196 {
197         const void __iomem *reg_fas = base + EESOX_FAS216_OFFSET;
198         const void __iomem *reg_dmastat = base + EESOX_DMASTAT;
199         const void __iomem *reg_dmadata = base + EESOX_DMADATA;
200         const register unsigned long mask = 0xffff;
201
202         do {
203                 unsigned int status;
204
205                 /*
206                  * Interrupt request?
207                  */
208                 status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
209                 if (status & STAT_INT)
210                         break;
211
212                 /*
213                  * DMA request active?
214                  */
215                 status = readb(reg_dmastat);
216                 if (!(status & EESOX_STAT_DMA))
217                         continue;
218
219                 /*
220                  * Get number of bytes in FIFO
221                  */
222                 status = readb(reg_fas + (REG_CFIS << EESOX_FAS216_SHIFT)) & CFIS_CF;
223                 if (status > 16)
224                         status = 16;
225                 if (status > length)
226                         status = length;
227
228                 /*
229                  * Align buffer.
230                  */
231                 if (((u32)buf) & 2 && status >= 2) {
232                         *(u16 *)buf = readl(reg_dmadata);
233                         buf += 2;
234                         status -= 2;
235                         length -= 2;
236                 }
237
238                 if (status >= 8) {
239                         unsigned long l1, l2;
240
241                         l1 = readl(reg_dmadata) & mask;
242                         l1 |= readl(reg_dmadata) << 16;
243                         l2 = readl(reg_dmadata) & mask;
244                         l2 |= readl(reg_dmadata) << 16;
245                         *(u32 *)buf = l1;
246                         buf += 4;
247                         *(u32 *)buf = l2;
248                         buf += 4;
249                         length -= 8;
250                         continue;
251                 }
252
253                 if (status >= 4) {
254                         unsigned long l1;
255
256                         l1 = readl(reg_dmadata) & mask;
257                         l1 |= readl(reg_dmadata) << 16;
258
259                         *(u32 *)buf = l1;
260                         buf += 4;
261                         length -= 4;
262                         continue;
263                 }
264
265                 if (status >= 2) {
266                         *(u16 *)buf = readl(reg_dmadata);
267                         buf += 2;
268                         length -= 2;
269                 }
270         } while (length);
271 }
272
273 static void eesoxscsi_buffer_out(void *buf, int length, void __iomem *base)
274 {
275         const void __iomem *reg_fas = base + EESOX_FAS216_OFFSET;
276         const void __iomem *reg_dmastat = base + EESOX_DMASTAT;
277         const void __iomem *reg_dmadata = base + EESOX_DMADATA;
278
279         do {
280                 unsigned int status;
281
282                 /*
283                  * Interrupt request?
284                  */
285                 status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
286                 if (status & STAT_INT)
287                         break;
288
289                 /*
290                  * DMA request active?
291                  */
292                 status = readb(reg_dmastat);
293                 if (!(status & EESOX_STAT_DMA))
294                         continue;
295
296                 /*
297                  * Get number of bytes in FIFO
298                  */
299                 status = readb(reg_fas + (REG_CFIS << EESOX_FAS216_SHIFT)) & CFIS_CF;
300                 if (status > 16)
301                         status = 16;
302                 status = 16 - status;
303                 if (status > length)
304                         status = length;
305                 status &= ~1;
306
307                 /*
308                  * Align buffer.
309                  */
310                 if (((u32)buf) & 2 && status >= 2) {
311                         writel(*(u16 *)buf << 16, reg_dmadata);
312                         buf += 2;
313                         status -= 2;
314                         length -= 2;
315                 }
316
317                 if (status >= 8) {
318                         unsigned long l1, l2;
319
320                         l1 = *(u32 *)buf;
321                         buf += 4;
322                         l2 = *(u32 *)buf;
323                         buf += 4;
324
325                         writel(l1 << 16, reg_dmadata);
326                         writel(l1, reg_dmadata);
327                         writel(l2 << 16, reg_dmadata);
328                         writel(l2, reg_dmadata);
329                         length -= 8;
330                         continue;
331                 }
332
333                 if (status >= 4) {
334                         unsigned long l1;
335
336                         l1 = *(u32 *)buf;
337                         buf += 4;
338
339                         writel(l1 << 16, reg_dmadata);
340                         writel(l1, reg_dmadata);
341                         length -= 4;
342                         continue;
343                 }
344
345                 if (status >= 2) {
346                         writel(*(u16 *)buf << 16, reg_dmadata);
347                         buf += 2;
348                         length -= 2;
349                 }
350         } while (length);
351 }
352
353 static void
354 eesoxscsi_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp,
355                      fasdmadir_t dir, int transfer_size)
356 {
357         struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
358         if (dir == DMA_IN) {
359                 eesoxscsi_buffer_in(SCp->ptr, SCp->this_residual, info->base);
360         } else {
361                 eesoxscsi_buffer_out(SCp->ptr, SCp->this_residual, info->base);
362         }
363 }
364
365 /* Prototype: int eesoxscsi_dma_stop(host, SCpnt)
366  * Purpose  : stops DMA/PIO
367  * Params   : host  - host
368  *            SCpnt - command
369  */
370 static void
371 eesoxscsi_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
372 {
373         struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
374         if (info->info.scsi.dma != NO_DMA)
375                 disable_dma(info->info.scsi.dma);
376 }
377
378 /* Prototype: const char *eesoxscsi_info(struct Scsi_Host * host)
379  * Purpose  : returns a descriptive string about this interface,
380  * Params   : host - driver host structure to return info for.
381  * Returns  : pointer to a static buffer containing null terminated string.
382  */
383 const char *eesoxscsi_info(struct Scsi_Host *host)
384 {
385         struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
386         static char string[150];
387
388         sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
389                 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
390                 VERSION, info->control & EESOX_TERM_ENABLE ? "n" : "ff");
391
392         return string;
393 }
394
395 /* Prototype: int eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
396  * Purpose  : Set a driver specific function
397  * Params   : host   - host to setup
398  *          : buffer - buffer containing string describing operation
399  *          : length - length of string
400  * Returns  : -EINVAL, or 0
401  */
402 static int
403 eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
404 {
405         int ret = length;
406
407         if (length >= 9 && strncmp(buffer, "EESOXSCSI", 9) == 0) {
408                 buffer += 9;
409                 length -= 9;
410
411                 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
412                         if (buffer[5] == '1')
413                                 eesoxscsi_terminator_ctl(host, 1);
414                         else if (buffer[5] == '0')
415                                 eesoxscsi_terminator_ctl(host, 0);
416                         else
417                                 ret = -EINVAL;
418                 } else
419                         ret = -EINVAL;
420         } else
421                 ret = -EINVAL;
422
423         return ret;
424 }
425
426 /* Prototype: int eesoxscsi_proc_info(char *buffer, char **start, off_t offset,
427  *                                    int length, int host_no, int inout)
428  * Purpose  : Return information about the driver to a user process accessing
429  *            the /proc filesystem.
430  * Params   : buffer - a buffer to write information to
431  *            start  - a pointer into this buffer set by this routine to the start
432  *                     of the required information.
433  *            offset - offset into information that we have read upto.
434  *            length - length of buffer
435  *            host_no - host number to return information for
436  *            inout  - 0 for reading, 1 for writing.
437  * Returns  : length of data written to buffer.
438  */
439 int eesoxscsi_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
440                             int length, int inout)
441 {
442         struct eesoxscsi_info *info;
443         char *p = buffer;
444         int pos;
445
446         if (inout == 1)
447                 return eesoxscsi_set_proc_info(host, buffer, length);
448
449         info = (struct eesoxscsi_info *)host->hostdata;
450
451         p += sprintf(p, "EESOX SCSI driver v%s\n", VERSION);
452         p += fas216_print_host(&info->info, p);
453         p += sprintf(p, "Term    : o%s\n",
454                         info->control & EESOX_TERM_ENABLE ? "n" : "ff");
455
456         p += fas216_print_stats(&info->info, p);
457         p += fas216_print_devices(&info->info, p);
458
459         *start = buffer + offset;
460         pos = p - buffer - offset;
461         if (pos > length)
462                 pos = length;
463
464         return pos;
465 }
466
467 static ssize_t eesoxscsi_show_term(struct device *dev, struct device_attribute *attr, char *buf)
468 {
469         struct expansion_card *ec = ECARD_DEV(dev);
470         struct Scsi_Host *host = ecard_get_drvdata(ec);
471         struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
472
473         return sprintf(buf, "%d\n", info->control & EESOX_TERM_ENABLE ? 1 : 0);
474 }
475
476 static ssize_t eesoxscsi_store_term(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
477 {
478         struct expansion_card *ec = ECARD_DEV(dev);
479         struct Scsi_Host *host = ecard_get_drvdata(ec);
480         struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
481         unsigned long flags;
482
483         if (len > 1) {
484                 spin_lock_irqsave(host->host_lock, flags);
485                 if (buf[0] != '0') {
486                         info->control |= EESOX_TERM_ENABLE;
487                 } else {
488                         info->control &= ~EESOX_TERM_ENABLE;
489                 }
490                 writeb(info->control, info->ctl_port);
491                 spin_unlock_irqrestore(host->host_lock, flags);
492         }
493
494         return len;
495 }
496
497 static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
498                    eesoxscsi_show_term, eesoxscsi_store_term);
499
500 static struct scsi_host_template eesox_template = {
501         .module                         = THIS_MODULE,
502         .proc_info                      = eesoxscsi_proc_info,
503         .name                           = "EESOX SCSI",
504         .info                           = eesoxscsi_info,
505         .queuecommand                   = fas216_queue_command,
506         .eh_host_reset_handler          = fas216_eh_host_reset,
507         .eh_bus_reset_handler           = fas216_eh_bus_reset,
508         .eh_device_reset_handler        = fas216_eh_device_reset,
509         .eh_abort_handler               = fas216_eh_abort,
510         .can_queue                      = 1,
511         .this_id                        = 7,
512         .sg_tablesize                   = SG_ALL,
513         .cmd_per_lun                    = 1,
514         .use_clustering                 = DISABLE_CLUSTERING,
515         .proc_name                      = "eesox",
516 };
517
518 static int __devinit
519 eesoxscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
520 {
521         struct Scsi_Host *host;
522         struct eesoxscsi_info *info;
523         unsigned long resbase, reslen;
524         void __iomem *base;
525         int ret;
526
527         ret = ecard_request_resources(ec);
528         if (ret)
529                 goto out;
530
531         resbase = ecard_resource_start(ec, ECARD_RES_IOCFAST);
532         reslen = ecard_resource_len(ec, ECARD_RES_IOCFAST);
533         base = ioremap(resbase, reslen);
534         if (!base) {
535                 ret = -ENOMEM;
536                 goto out_region;
537         }
538
539         host = scsi_host_alloc(&eesox_template,
540                                sizeof(struct eesoxscsi_info));
541         if (!host) {
542                 ret = -ENOMEM;
543                 goto out_unmap;
544         }
545
546         ecard_set_drvdata(ec, host);
547
548         info = (struct eesoxscsi_info *)host->hostdata;
549         info->ec        = ec;
550         info->base      = base;
551         info->ctl_port  = base + EESOX_CONTROL;
552         info->control   = term[ec->slot_no] ? EESOX_TERM_ENABLE : 0;
553         writeb(info->control, info->ctl_port);
554
555         info->info.scsi.io_base         = base + EESOX_FAS216_OFFSET;
556         info->info.scsi.io_shift        = EESOX_FAS216_SHIFT;
557         info->info.scsi.irq             = ec->irq;
558         info->info.scsi.dma             = ec->dma;
559         info->info.ifcfg.clockrate      = 40; /* MHz */
560         info->info.ifcfg.select_timeout = 255;
561         info->info.ifcfg.asyncperiod    = 200; /* ns */
562         info->info.ifcfg.sync_max_depth = 7;
563         info->info.ifcfg.cntl3          = CNTL3_FASTSCSI | CNTL3_FASTCLK;
564         info->info.ifcfg.disconnect_ok  = 1;
565         info->info.ifcfg.wide_max_size  = 0;
566         info->info.ifcfg.capabilities   = FASCAP_PSEUDODMA;
567         info->info.dma.setup            = eesoxscsi_dma_setup;
568         info->info.dma.pseudo           = eesoxscsi_dma_pseudo;
569         info->info.dma.stop             = eesoxscsi_dma_stop;
570
571         ec->irqaddr     = base + EESOX_DMASTAT;
572         ec->irqmask     = EESOX_STAT_INTR;
573         ec->irq_data    = info;
574         ec->ops         = &eesoxscsi_ops;
575
576         device_create_file(&ec->dev, &dev_attr_bus_term);
577
578         ret = fas216_init(host);
579         if (ret)
580                 goto out_free;
581
582         ret = request_irq(ec->irq, eesoxscsi_intr, 0, "eesoxscsi", info);
583         if (ret) {
584                 printk("scsi%d: IRQ%d not free: %d\n",
585                        host->host_no, ec->irq, ret);
586                 goto out_remove;
587         }
588
589         if (info->info.scsi.dma != NO_DMA) {
590                 if (request_dma(info->info.scsi.dma, "eesox")) {
591                         printk("scsi%d: DMA%d not free, DMA disabled\n",
592                                host->host_no, info->info.scsi.dma);
593                         info->info.scsi.dma = NO_DMA;
594                 } else {
595                         set_dma_speed(info->info.scsi.dma, 180);
596                         info->info.ifcfg.capabilities |= FASCAP_DMA;
597                         info->info.ifcfg.cntl3 |= CNTL3_BS8;
598                 }
599         }
600
601         ret = fas216_add(host, &ec->dev);
602         if (ret == 0)
603                 goto out;
604
605         if (info->info.scsi.dma != NO_DMA)
606                 free_dma(info->info.scsi.dma);
607         free_irq(ec->irq, host);
608
609  out_remove:
610         fas216_remove(host);
611
612  out_free:
613         device_remove_file(&ec->dev, &dev_attr_bus_term);
614         scsi_host_put(host);
615
616  out_unmap:
617         iounmap(base);
618
619  out_region:
620         ecard_release_resources(ec);
621
622  out:
623         return ret;
624 }
625
626 static void __devexit eesoxscsi_remove(struct expansion_card *ec)
627 {
628         struct Scsi_Host *host = ecard_get_drvdata(ec);
629         struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
630
631         ecard_set_drvdata(ec, NULL);
632         fas216_remove(host);
633
634         if (info->info.scsi.dma != NO_DMA)
635                 free_dma(info->info.scsi.dma);
636         free_irq(ec->irq, info);
637
638         device_remove_file(&ec->dev, &dev_attr_bus_term);
639
640         iounmap(info->base);
641
642         fas216_release(host);
643         scsi_host_put(host);
644         ecard_release_resources(ec);
645 }
646
647 static const struct ecard_id eesoxscsi_cids[] = {
648         { MANU_EESOX, PROD_EESOX_SCSI2 },
649         { 0xffff, 0xffff },
650 };
651
652 static struct ecard_driver eesoxscsi_driver = {
653         .probe          = eesoxscsi_probe,
654         .remove         = __devexit_p(eesoxscsi_remove),
655         .id_table       = eesoxscsi_cids,
656         .drv = {
657                 .name           = "eesoxscsi",
658         },
659 };
660
661 static int __init eesox_init(void)
662 {
663         return ecard_register_driver(&eesoxscsi_driver);
664 }
665
666 static void __exit eesox_exit(void)
667 {
668         ecard_remove_driver(&eesoxscsi_driver);
669 }
670
671 module_init(eesox_init);
672 module_exit(eesox_exit);
673
674 MODULE_AUTHOR("Russell King");
675 MODULE_DESCRIPTION("EESOX 'Fast' SCSI driver for Acorn machines");
676 module_param_array(term, int, NULL, 0);
677 MODULE_PARM_DESC(term, "SCSI bus termination");
678 MODULE_LICENSE("GPL");