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