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