Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[pandora-kernel.git] / drivers / scsi / arm / powertec.c
1 /*
2  *  linux/drivers/acorn/scsi/powertec.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 #include <linux/module.h>
11 #include <linux/blkdev.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/ioport.h>
15 #include <linux/sched.h>
16 #include <linux/proc_fs.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/dma-mapping.h>
21
22 #include <asm/dma.h>
23 #include <asm/ecard.h>
24 #include <asm/io.h>
25 #include <asm/pgtable.h>
26
27 #include "../scsi.h"
28 #include <scsi/scsi_host.h>
29 #include "fas216.h"
30 #include "scsi.h"
31
32 #include <scsi/scsicam.h>
33
34 #define POWERTEC_FAS216_OFFSET  0x3000
35 #define POWERTEC_FAS216_SHIFT   6
36
37 #define POWERTEC_INTR_STATUS    0x2000
38 #define POWERTEC_INTR_BIT       0x80
39
40 #define POWERTEC_RESET_CONTROL  0x1018
41 #define POWERTEC_RESET_BIT      1
42
43 #define POWERTEC_TERM_CONTROL   0x2018
44 #define POWERTEC_TERM_ENABLE    1
45
46 #define POWERTEC_INTR_CONTROL   0x101c
47 #define POWERTEC_INTR_ENABLE    1
48 #define POWERTEC_INTR_DISABLE   0
49
50 #define VERSION "1.10 (19/01/2003 2.5.59)"
51
52 /*
53  * Use term=0,1,0,0,0 to turn terminators on/off.
54  * One entry per slot.
55  */
56 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
57
58 #define NR_SG   256
59
60 struct powertec_info {
61         FAS216_Info             info;
62         struct expansion_card   *ec;
63         void __iomem            *base;
64         unsigned int            term_ctl;
65         struct scatterlist      sg[NR_SG];
66 };
67
68 /* Prototype: void powertecscsi_irqenable(ec, irqnr)
69  * Purpose  : Enable interrupts on Powertec SCSI card
70  * Params   : ec    - expansion card structure
71  *          : irqnr - interrupt number
72  */
73 static void
74 powertecscsi_irqenable(struct expansion_card *ec, int irqnr)
75 {
76         struct powertec_info *info = ec->irq_data;
77         writeb(POWERTEC_INTR_ENABLE, info->base + POWERTEC_INTR_CONTROL);
78 }
79
80 /* Prototype: void powertecscsi_irqdisable(ec, irqnr)
81  * Purpose  : Disable interrupts on Powertec SCSI card
82  * Params   : ec    - expansion card structure
83  *          : irqnr - interrupt number
84  */
85 static void
86 powertecscsi_irqdisable(struct expansion_card *ec, int irqnr)
87 {
88         struct powertec_info *info = ec->irq_data;
89         writeb(POWERTEC_INTR_DISABLE, info->base + POWERTEC_INTR_CONTROL);
90 }
91
92 static const expansioncard_ops_t powertecscsi_ops = {
93         .irqenable      = powertecscsi_irqenable,
94         .irqdisable     = powertecscsi_irqdisable,
95 };
96
97 /* Prototype: void powertecscsi_terminator_ctl(host, on_off)
98  * Purpose  : Turn the Powertec SCSI terminators on or off
99  * Params   : host   - card to turn on/off
100  *          : on_off - !0 to turn on, 0 to turn off
101  */
102 static void
103 powertecscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
104 {
105         struct powertec_info *info = (struct powertec_info *)host->hostdata;
106
107         info->term_ctl = on_off ? POWERTEC_TERM_ENABLE : 0;
108         writeb(info->term_ctl, info->base + POWERTEC_TERM_CONTROL);
109 }
110
111 /* Prototype: void powertecscsi_intr(irq, *dev_id, *regs)
112  * Purpose  : handle interrupts from Powertec SCSI card
113  * Params   : irq    - interrupt number
114  *            dev_id - user-defined (Scsi_Host structure)
115  *            regs   - processor registers at interrupt
116  */
117 static irqreturn_t
118 powertecscsi_intr(int irq, void *dev_id, struct pt_regs *regs)
119 {
120         struct powertec_info *info = dev_id;
121
122         return fas216_intr(&info->info);
123 }
124
125 /* Prototype: fasdmatype_t powertecscsi_dma_setup(host, SCpnt, direction, min_type)
126  * Purpose  : initialises DMA/PIO
127  * Params   : host      - host
128  *            SCpnt     - command
129  *            direction - DMA on to/off of card
130  *            min_type  - minimum DMA support that we must have for this transfer
131  * Returns  : type of transfer to be performed
132  */
133 static fasdmatype_t
134 powertecscsi_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
135                        fasdmadir_t direction, fasdmatype_t min_type)
136 {
137         struct powertec_info *info = (struct powertec_info *)host->hostdata;
138         struct device *dev = scsi_get_device(host);
139         int dmach = info->info.scsi.dma;
140
141         if (info->info.ifcfg.capabilities & FASCAP_DMA &&
142             min_type == fasdma_real_all) {
143                 int bufs, map_dir, dma_dir;
144
145                 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
146
147                 if (direction == DMA_OUT)
148                         map_dir = DMA_TO_DEVICE,
149                         dma_dir = DMA_MODE_WRITE;
150                 else
151                         map_dir = DMA_FROM_DEVICE,
152                         dma_dir = DMA_MODE_READ;
153
154                 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
155
156                 disable_dma(dmach);
157                 set_dma_sg(dmach, info->sg, bufs + 1);
158                 set_dma_mode(dmach, dma_dir);
159                 enable_dma(dmach);
160                 return fasdma_real_all;
161         }
162
163         /*
164          * If we're not doing DMA,
165          *  we'll do slow PIO
166          */
167         return fasdma_pio;
168 }
169
170 /* Prototype: int powertecscsi_dma_stop(host, SCpnt)
171  * Purpose  : stops DMA/PIO
172  * Params   : host  - host
173  *            SCpnt - command
174  */
175 static void
176 powertecscsi_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
177 {
178         struct powertec_info *info = (struct powertec_info *)host->hostdata;
179         if (info->info.scsi.dma != NO_DMA)
180                 disable_dma(info->info.scsi.dma);
181 }
182
183 /* Prototype: const char *powertecscsi_info(struct Scsi_Host * host)
184  * Purpose  : returns a descriptive string about this interface,
185  * Params   : host - driver host structure to return info for.
186  * Returns  : pointer to a static buffer containing null terminated string.
187  */
188 const char *powertecscsi_info(struct Scsi_Host *host)
189 {
190         struct powertec_info *info = (struct powertec_info *)host->hostdata;
191         static char string[150];
192
193         sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
194                 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
195                 VERSION, info->term_ctl ? "n" : "ff");
196
197         return string;
198 }
199
200 /* Prototype: int powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
201  * Purpose  : Set a driver specific function
202  * Params   : host   - host to setup
203  *          : buffer - buffer containing string describing operation
204  *          : length - length of string
205  * Returns  : -EINVAL, or 0
206  */
207 static int
208 powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
209 {
210         int ret = length;
211
212         if (length >= 12 && strncmp(buffer, "POWERTECSCSI", 12) == 0) {
213                 buffer += 12;
214                 length -= 12;
215
216                 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
217                         if (buffer[5] == '1')
218                                 powertecscsi_terminator_ctl(host, 1);
219                         else if (buffer[5] == '0')
220                                 powertecscsi_terminator_ctl(host, 0);
221                         else
222                                 ret = -EINVAL;
223                 } else
224                         ret = -EINVAL;
225         } else
226                 ret = -EINVAL;
227
228         return ret;
229 }
230
231 /* Prototype: int powertecscsi_proc_info(char *buffer, char **start, off_t offset,
232  *                                      int length, int host_no, int inout)
233  * Purpose  : Return information about the driver to a user process accessing
234  *            the /proc filesystem.
235  * Params   : buffer  - a buffer to write information to
236  *            start   - a pointer into this buffer set by this routine to the start
237  *                      of the required information.
238  *            offset  - offset into information that we have read upto.
239  *            length  - length of buffer
240  *            inout   - 0 for reading, 1 for writing.
241  * Returns  : length of data written to buffer.
242  */
243 int powertecscsi_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
244                             int length, int inout)
245 {
246         struct powertec_info *info;
247         char *p = buffer;
248         int pos;
249
250         if (inout == 1)
251                 return powertecscsi_set_proc_info(host, buffer, length);
252
253         info = (struct powertec_info *)host->hostdata;
254
255         p += sprintf(p, "PowerTec SCSI driver v%s\n", VERSION);
256         p += fas216_print_host(&info->info, p);
257         p += sprintf(p, "Term    : o%s\n",
258                         info->term_ctl ? "n" : "ff");
259
260         p += fas216_print_stats(&info->info, p);
261         p += fas216_print_devices(&info->info, p);
262
263         *start = buffer + offset;
264         pos = p - buffer - offset;
265         if (pos > length)
266                 pos = length;
267
268         return pos;
269 }
270
271 static ssize_t powertecscsi_show_term(struct device *dev, struct device_attribute *attr, char *buf)
272 {
273         struct expansion_card *ec = ECARD_DEV(dev);
274         struct Scsi_Host *host = ecard_get_drvdata(ec);
275         struct powertec_info *info = (struct powertec_info *)host->hostdata;
276
277         return sprintf(buf, "%d\n", info->term_ctl ? 1 : 0);
278 }
279
280 static ssize_t
281 powertecscsi_store_term(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
282 {
283         struct expansion_card *ec = ECARD_DEV(dev);
284         struct Scsi_Host *host = ecard_get_drvdata(ec);
285
286         if (len > 1)
287                 powertecscsi_terminator_ctl(host, buf[0] != '0');
288
289         return len;
290 }
291
292 static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
293                    powertecscsi_show_term, powertecscsi_store_term);
294
295 static struct scsi_host_template powertecscsi_template = {
296         .module                         = THIS_MODULE,
297         .proc_info                      = powertecscsi_proc_info,
298         .name                           = "PowerTec SCSI",
299         .info                           = powertecscsi_info,
300         .queuecommand                   = fas216_queue_command,
301         .eh_host_reset_handler          = fas216_eh_host_reset,
302         .eh_bus_reset_handler           = fas216_eh_bus_reset,
303         .eh_device_reset_handler        = fas216_eh_device_reset,
304         .eh_abort_handler               = fas216_eh_abort,
305
306         .can_queue                      = 8,
307         .this_id                        = 7,
308         .sg_tablesize                   = SG_ALL,
309         .cmd_per_lun                    = 2,
310         .use_clustering                 = ENABLE_CLUSTERING,
311         .proc_name                      = "powertec",
312 };
313
314 static int __devinit
315 powertecscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
316 {
317         struct Scsi_Host *host;
318         struct powertec_info *info;
319         unsigned long resbase, reslen;
320         void __iomem *base;
321         int ret;
322
323         ret = ecard_request_resources(ec);
324         if (ret)
325                 goto out;
326
327         resbase = ecard_resource_start(ec, ECARD_RES_IOCFAST);
328         reslen = ecard_resource_len(ec, ECARD_RES_IOCFAST);
329         base = ioremap(resbase, reslen);
330         if (!base) {
331                 ret = -ENOMEM;
332                 goto out_region;
333         }
334
335         host = scsi_host_alloc(&powertecscsi_template,
336                                sizeof (struct powertec_info));
337         if (!host) {
338                 ret = -ENOMEM;
339                 goto out_unmap;
340         }
341
342         ecard_set_drvdata(ec, host);
343
344         info = (struct powertec_info *)host->hostdata;
345         info->base = base;
346         powertecscsi_terminator_ctl(host, term[ec->slot_no]);
347
348         info->info.scsi.io_base         = base + POWERTEC_FAS216_OFFSET;
349         info->info.scsi.io_shift        = POWERTEC_FAS216_SHIFT;
350         info->info.scsi.irq             = ec->irq;
351         info->info.scsi.dma             = ec->dma;
352         info->info.ifcfg.clockrate      = 40; /* MHz */
353         info->info.ifcfg.select_timeout = 255;
354         info->info.ifcfg.asyncperiod    = 200; /* ns */
355         info->info.ifcfg.sync_max_depth = 7;
356         info->info.ifcfg.cntl3          = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
357         info->info.ifcfg.disconnect_ok  = 1;
358         info->info.ifcfg.wide_max_size  = 0;
359         info->info.ifcfg.capabilities   = 0;
360         info->info.dma.setup            = powertecscsi_dma_setup;
361         info->info.dma.pseudo           = NULL;
362         info->info.dma.stop             = powertecscsi_dma_stop;
363
364         ec->irqaddr     = base + POWERTEC_INTR_STATUS;
365         ec->irqmask     = POWERTEC_INTR_BIT;
366         ec->irq_data    = info;
367         ec->ops         = &powertecscsi_ops;
368
369         device_create_file(&ec->dev, &dev_attr_bus_term);
370
371         ret = fas216_init(host);
372         if (ret)
373                 goto out_free;
374
375         ret = request_irq(ec->irq, powertecscsi_intr,
376                           IRQF_DISABLED, "powertec", info);
377         if (ret) {
378                 printk("scsi%d: IRQ%d not free: %d\n",
379                        host->host_no, ec->irq, ret);
380                 goto out_release;
381         }
382
383         if (info->info.scsi.dma != NO_DMA) {
384                 if (request_dma(info->info.scsi.dma, "powertec")) {
385                         printk("scsi%d: DMA%d not free, using PIO\n",
386                                host->host_no, info->info.scsi.dma);
387                         info->info.scsi.dma = NO_DMA;
388                 } else {
389                         set_dma_speed(info->info.scsi.dma, 180);
390                         info->info.ifcfg.capabilities |= FASCAP_DMA;
391                 }
392         }
393
394         ret = fas216_add(host, &ec->dev);
395         if (ret == 0)
396                 goto out;
397
398         if (info->info.scsi.dma != NO_DMA)
399                 free_dma(info->info.scsi.dma);
400         free_irq(ec->irq, host);
401
402  out_release:
403         fas216_release(host);
404
405  out_free:
406         device_remove_file(&ec->dev, &dev_attr_bus_term);
407         scsi_host_put(host);
408
409  out_unmap:
410         iounmap(base);
411
412  out_region:
413         ecard_release_resources(ec);
414
415  out:
416         return ret;
417 }
418
419 static void __devexit powertecscsi_remove(struct expansion_card *ec)
420 {
421         struct Scsi_Host *host = ecard_get_drvdata(ec);
422         struct powertec_info *info = (struct powertec_info *)host->hostdata;
423
424         ecard_set_drvdata(ec, NULL);
425         fas216_remove(host);
426
427         device_remove_file(&ec->dev, &dev_attr_bus_term);
428
429         if (info->info.scsi.dma != NO_DMA)
430                 free_dma(info->info.scsi.dma);
431         free_irq(ec->irq, info);
432
433         iounmap(info->base);
434
435         fas216_release(host);
436         scsi_host_put(host);
437         ecard_release_resources(ec);
438 }
439
440 static const struct ecard_id powertecscsi_cids[] = {
441         { MANU_ALSYSTEMS, PROD_ALSYS_SCSIATAPI },
442         { 0xffff, 0xffff },
443 };
444
445 static struct ecard_driver powertecscsi_driver = {
446         .probe          = powertecscsi_probe,
447         .remove         = __devexit_p(powertecscsi_remove),
448         .id_table       = powertecscsi_cids,
449         .drv = {
450                 .name           = "powertecscsi",
451         },
452 };
453
454 static int __init powertecscsi_init(void)
455 {
456         return ecard_register_driver(&powertecscsi_driver);
457 }
458
459 static void __exit powertecscsi_exit(void)
460 {
461         ecard_remove_driver(&powertecscsi_driver);
462 }
463
464 module_init(powertecscsi_init);
465 module_exit(powertecscsi_exit);
466
467 MODULE_AUTHOR("Russell King");
468 MODULE_DESCRIPTION("Powertec SCSI driver");
469 module_param_array(term, int, NULL, 0);
470 MODULE_PARM_DESC(term, "SCSI bus termination");
471 MODULE_LICENSE("GPL");