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