initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / scsi / arm / eesox.c
blob5f037d24caeecdec354b2fc164e17bd2e0e5e8f9
1 /*
2 * linux/drivers/acorn/scsi/eesox.c
4 * Copyright (C) 1997-2003 Russell King
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.
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!
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.
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>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39 #include <asm/dma.h>
40 #include <asm/ecard.h>
41 #include <asm/pgtable.h>
43 #include "../scsi.h"
44 #include <scsi/scsi_host.h>
45 #include "fas216.h"
46 #include "scsi.h"
48 #include <scsi/scsicam.h>
50 #define EESOX_FAS216_OFFSET 0x3000
51 #define EESOX_FAS216_SHIFT 5
53 #define EESOX_DMASTAT 0x2800
54 #define EESOX_STAT_INTR 0x01
55 #define EESOX_STAT_DMA 0x02
57 #define EESOX_CONTROL 0x2800
58 #define EESOX_INTR_ENABLE 0x04
59 #define EESOX_TERM_ENABLE 0x02
60 #define EESOX_RESET 0x01
62 #define EESOX_DMADATA 0x3800
64 #define VERSION "1.10 (17/01/2003 2.5.59)"
67 * Use term=0,1,0,0,0 to turn terminators on/off
69 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
71 #define NR_SG 256
73 struct eesoxscsi_info {
74 FAS216_Info info;
75 struct expansion_card *ec;
77 void *ctl_port;
78 unsigned int control;
79 struct scatterlist sg[NR_SG]; /* Scatter DMA list */
82 /* Prototype: void eesoxscsi_irqenable(ec, irqnr)
83 * Purpose : Enable interrupts on EESOX SCSI card
84 * Params : ec - expansion card structure
85 * : irqnr - interrupt number
87 static void
88 eesoxscsi_irqenable(struct expansion_card *ec, int irqnr)
90 struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
92 info->control |= EESOX_INTR_ENABLE;
94 writeb(info->control, info->ctl_port);
97 /* Prototype: void eesoxscsi_irqdisable(ec, irqnr)
98 * Purpose : Disable interrupts on EESOX SCSI card
99 * Params : ec - expansion card structure
100 * : irqnr - interrupt number
102 static void
103 eesoxscsi_irqdisable(struct expansion_card *ec, int irqnr)
105 struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
107 info->control &= ~EESOX_INTR_ENABLE;
109 writeb(info->control, info->ctl_port);
112 static const expansioncard_ops_t eesoxscsi_ops = {
113 .irqenable = eesoxscsi_irqenable,
114 .irqdisable = eesoxscsi_irqdisable,
117 /* Prototype: void eesoxscsi_terminator_ctl(*host, on_off)
118 * Purpose : Turn the EESOX SCSI terminators on or off
119 * Params : host - card to turn on/off
120 * : on_off - !0 to turn on, 0 to turn off
122 static void
123 eesoxscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
125 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
126 unsigned long flags;
128 spin_lock_irqsave(host->host_lock, flags);
129 if (on_off)
130 info->control |= EESOX_TERM_ENABLE;
131 else
132 info->control &= ~EESOX_TERM_ENABLE;
134 writeb(info->control, info->ctl_port);
135 spin_unlock_irqrestore(host->host_lock, flags);
138 /* Prototype: void eesoxscsi_intr(irq, *dev_id, *regs)
139 * Purpose : handle interrupts from EESOX SCSI card
140 * Params : irq - interrupt number
141 * dev_id - user-defined (Scsi_Host structure)
142 * regs - processor registers at interrupt
144 static irqreturn_t
145 eesoxscsi_intr(int irq, void *dev_id, struct pt_regs *regs)
147 struct eesoxscsi_info *info = dev_id;
149 return fas216_intr(&info->info);
152 /* Prototype: fasdmatype_t eesoxscsi_dma_setup(host, SCpnt, direction, min_type)
153 * Purpose : initialises DMA/PIO
154 * Params : host - host
155 * SCpnt - command
156 * direction - DMA on to/off of card
157 * min_type - minimum DMA support that we must have for this transfer
158 * Returns : type of transfer to be performed
160 static fasdmatype_t
161 eesoxscsi_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
162 fasdmadir_t direction, fasdmatype_t min_type)
164 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
165 struct device *dev = scsi_get_device(host);
166 int dmach = host->dma_channel;
168 if (dmach != NO_DMA &&
169 (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
170 int bufs, map_dir, dma_dir;
172 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
174 if (direction == DMA_OUT)
175 map_dir = DMA_TO_DEVICE,
176 dma_dir = DMA_MODE_WRITE;
177 else
178 map_dir = DMA_FROM_DEVICE,
179 dma_dir = DMA_MODE_READ;
181 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
183 disable_dma(dmach);
184 set_dma_sg(dmach, info->sg, bufs + 1);
185 set_dma_mode(dmach, dma_dir);
186 enable_dma(dmach);
187 return fasdma_real_all;
190 * We don't do DMA, we only do slow PIO
192 * Some day, we will do Pseudo DMA
194 return fasdma_pseudo;
197 static void eesoxscsi_buffer_in(void *buf, int length, void *base)
199 const void *reg_fas = base + EESOX_FAS216_OFFSET;
200 const void *reg_dmastat = base + EESOX_DMASTAT;
201 const void *reg_dmadata = base + EESOX_DMADATA;
202 const register unsigned long mask = 0xffff;
204 do {
205 unsigned int status;
208 * Interrupt request?
210 status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
211 if (status & STAT_INT)
212 break;
215 * DMA request active?
217 status = readb(reg_dmastat);
218 if (!(status & EESOX_STAT_DMA))
219 continue;
222 * Get number of bytes in FIFO
224 status = readb(reg_fas + (REG_CFIS << EESOX_FAS216_SHIFT)) & CFIS_CF;
225 if (status > 16)
226 status = 16;
227 if (status > length)
228 status = length;
231 * Align buffer.
233 if (((u32)buf) & 2 && status >= 2) {
234 *(u16 *)buf = readl(reg_dmadata);
235 buf += 2;
236 status -= 2;
237 length -= 2;
240 if (status >= 8) {
241 unsigned long l1, l2;
243 l1 = readl(reg_dmadata) & mask;
244 l1 |= readl(reg_dmadata) << 16;
245 l2 = readl(reg_dmadata) & mask;
246 l2 |= readl(reg_dmadata) << 16;
247 *(u32 *)buf = l1;
248 buf += 4;
249 *(u32 *)buf = l2;
250 buf += 4;
251 length -= 8;
252 continue;
255 if (status >= 4) {
256 unsigned long l1;
258 l1 = readl(reg_dmadata) & mask;
259 l1 |= readl(reg_dmadata) << 16;
261 *(u32 *)buf = l1;
262 buf += 4;
263 length -= 4;
264 continue;
267 if (status >= 2) {
268 *(u16 *)buf = readl(reg_dmadata);
269 buf += 2;
270 length -= 2;
272 } while (length);
275 static void eesoxscsi_buffer_out(void *buf, int length, void *base)
277 const void *reg_fas = base + EESOX_FAS216_OFFSET;
278 const void *reg_dmastat = base + EESOX_DMASTAT;
279 const void *reg_dmadata = base + EESOX_DMADATA;
281 do {
282 unsigned int status;
285 * Interrupt request?
287 status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
288 if (status & STAT_INT)
289 break;
292 * DMA request active?
294 status = readb(reg_dmastat);
295 if (!(status & EESOX_STAT_DMA))
296 continue;
299 * Get number of bytes in FIFO
301 status = readb(reg_fas + (REG_CFIS << EESOX_FAS216_SHIFT)) & CFIS_CF;
302 if (status > 16)
303 status = 16;
304 status = 16 - status;
305 if (status > length)
306 status = length;
307 status &= ~1;
310 * Align buffer.
312 if (((u32)buf) & 2 && status >= 2) {
313 writel(*(u16 *)buf << 16, reg_dmadata);
314 buf += 2;
315 status -= 2;
316 length -= 2;
319 if (status >= 8) {
320 unsigned long l1, l2;
322 l1 = *(u32 *)buf;
323 buf += 4;
324 l2 = *(u32 *)buf;
325 buf += 4;
327 writel(l1 << 16, reg_dmadata);
328 writel(l1, reg_dmadata);
329 writel(l2 << 16, reg_dmadata);
330 writel(l2, reg_dmadata);
331 length -= 8;
332 continue;
335 if (status >= 4) {
336 unsigned long l1;
338 l1 = *(u32 *)buf;
339 buf += 4;
341 writel(l1 << 16, reg_dmadata);
342 writel(l1, reg_dmadata);
343 length -= 4;
344 continue;
347 if (status >= 2) {
348 writel(*(u16 *)buf << 16, reg_dmadata);
349 buf += 2;
350 length -= 2;
352 } while (length);
355 static void
356 eesoxscsi_dma_pseudo(struct Scsi_Host *host, Scsi_Pointer *SCp,
357 fasdmadir_t dir, int transfer_size)
359 void *base = (void *)host->base;
360 if (dir == DMA_IN) {
361 eesoxscsi_buffer_in(SCp->ptr, SCp->this_residual, base);
362 } else {
363 eesoxscsi_buffer_out(SCp->ptr, SCp->this_residual, base);
367 /* Prototype: int eesoxscsi_dma_stop(host, SCpnt)
368 * Purpose : stops DMA/PIO
369 * Params : host - host
370 * SCpnt - command
372 static void
373 eesoxscsi_dma_stop(struct Scsi_Host *host, Scsi_Pointer *SCp)
375 if (host->dma_channel != NO_DMA)
376 disable_dma(host->dma_channel);
379 /* Prototype: const char *eesoxscsi_info(struct Scsi_Host * host)
380 * Purpose : returns a descriptive string about this interface,
381 * Params : host - driver host structure to return info for.
382 * Returns : pointer to a static buffer containing null terminated string.
384 const char *eesoxscsi_info(struct Scsi_Host *host)
386 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
387 static char string[150];
389 sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
390 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
391 VERSION, info->control & EESOX_TERM_ENABLE ? "n" : "ff");
393 return string;
396 /* Prototype: int eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
397 * Purpose : Set a driver specific function
398 * Params : host - host to setup
399 * : buffer - buffer containing string describing operation
400 * : length - length of string
401 * Returns : -EINVAL, or 0
403 static int
404 eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
406 int ret = length;
408 if (length >= 9 && strncmp(buffer, "EESOXSCSI", 9) == 0) {
409 buffer += 9;
410 length -= 9;
412 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
413 if (buffer[5] == '1')
414 eesoxscsi_terminator_ctl(host, 1);
415 else if (buffer[5] == '0')
416 eesoxscsi_terminator_ctl(host, 0);
417 else
418 ret = -EINVAL;
419 } else
420 ret = -EINVAL;
421 } else
422 ret = -EINVAL;
424 return ret;
427 /* Prototype: int eesoxscsi_proc_info(char *buffer, char **start, off_t offset,
428 * int length, int host_no, int inout)
429 * Purpose : Return information about the driver to a user process accessing
430 * the /proc filesystem.
431 * Params : buffer - a buffer to write information to
432 * start - a pointer into this buffer set by this routine to the start
433 * of the required information.
434 * offset - offset into information that we have read upto.
435 * length - length of buffer
436 * host_no - host number to return information for
437 * inout - 0 for reading, 1 for writing.
438 * Returns : length of data written to buffer.
440 int eesoxscsi_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
441 int length, int inout)
443 struct eesoxscsi_info *info;
444 char *p = buffer;
445 int pos;
447 if (inout == 1)
448 return eesoxscsi_set_proc_info(host, buffer, length);
450 info = (struct eesoxscsi_info *)host->hostdata;
452 p += sprintf(p, "EESOX SCSI driver v%s\n", VERSION);
453 p += fas216_print_host(&info->info, p);
454 p += sprintf(p, "Term : o%s\n",
455 info->control & EESOX_TERM_ENABLE ? "n" : "ff");
457 p += fas216_print_stats(&info->info, p);
458 p += fas216_print_devices(&info->info, p);
460 *start = buffer + offset;
461 pos = p - buffer - offset;
462 if (pos > length)
463 pos = length;
465 return pos;
468 static ssize_t eesoxscsi_show_term(struct device *dev, char *buf)
470 struct expansion_card *ec = ECARD_DEV(dev);
471 struct Scsi_Host *host = ecard_get_drvdata(ec);
472 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
474 return sprintf(buf, "%d\n", info->control & EESOX_TERM_ENABLE ? 1 : 0);
477 static ssize_t eesoxscsi_store_term(struct device *dev, const char *buf, size_t len)
479 struct expansion_card *ec = ECARD_DEV(dev);
480 struct Scsi_Host *host = ecard_get_drvdata(ec);
481 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
482 unsigned long flags;
484 if (len > 1) {
485 spin_lock_irqsave(host->host_lock, flags);
486 if (buf[0] != '0') {
487 info->control |= EESOX_TERM_ENABLE;
488 } else {
489 info->control &= ~EESOX_TERM_ENABLE;
491 writeb(info->control, info->ctl_port);
492 spin_unlock_irqrestore(host->host_lock, flags);
495 return len;
498 static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
499 eesoxscsi_show_term, eesoxscsi_store_term);
501 static Scsi_Host_Template eesox_template = {
502 .module = THIS_MODULE,
503 .proc_info = eesoxscsi_proc_info,
504 .name = "EESOX SCSI",
505 .info = eesoxscsi_info,
506 .queuecommand = fas216_queue_command,
507 .eh_host_reset_handler = fas216_eh_host_reset,
508 .eh_bus_reset_handler = fas216_eh_bus_reset,
509 .eh_device_reset_handler = fas216_eh_device_reset,
510 .eh_abort_handler = fas216_eh_abort,
511 .can_queue = 1,
512 .this_id = 7,
513 .sg_tablesize = SG_ALL,
514 .cmd_per_lun = 1,
515 .use_clustering = DISABLE_CLUSTERING,
516 .proc_name = "eesox",
519 static int __devinit
520 eesoxscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
522 struct Scsi_Host *host;
523 struct eesoxscsi_info *info;
524 unsigned long resbase, reslen;
525 unsigned char *base;
526 int ret;
528 resbase = ecard_resource_start(ec, ECARD_RES_IOCFAST);
529 reslen = ecard_resource_len(ec, ECARD_RES_IOCFAST);
531 if (!request_mem_region(resbase, reslen, "eesoxscsi")) {
532 ret = -EBUSY;
533 goto out;
536 base = ioremap(resbase, reslen);
537 if (!base) {
538 ret = -ENOMEM;
539 goto out_region;
542 host = scsi_host_alloc(&eesox_template,
543 sizeof(struct eesoxscsi_info));
544 if (!host) {
545 ret = -ENOMEM;
546 goto out_unmap;
549 host->base = (unsigned long)base;
550 host->irq = ec->irq;
551 host->dma_channel = ec->dma;
553 ecard_set_drvdata(ec, host);
555 info = (struct eesoxscsi_info *)host->hostdata;
556 info->ec = ec;
557 info->ctl_port = base + EESOX_CONTROL;
558 info->control = term[ec->slot_no] ? EESOX_TERM_ENABLE : 0;
559 writeb(info->control, info->ctl_port);
561 ec->irqaddr = base + EESOX_DMASTAT;
562 ec->irqmask = EESOX_STAT_INTR;
563 ec->irq_data = info;
564 ec->ops = &eesoxscsi_ops;
566 info->info.scsi.io_base = base + EESOX_FAS216_OFFSET;
567 info->info.scsi.io_shift = EESOX_FAS216_SHIFT;
568 info->info.scsi.irq = host->irq;
569 info->info.ifcfg.clockrate = 40; /* MHz */
570 info->info.ifcfg.select_timeout = 255;
571 info->info.ifcfg.asyncperiod = 200; /* ns */
572 info->info.ifcfg.sync_max_depth = 7;
573 info->info.ifcfg.cntl3 = CNTL3_FASTSCSI | CNTL3_FASTCLK;
574 info->info.ifcfg.disconnect_ok = 1;
575 info->info.ifcfg.wide_max_size = 0;
576 info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
577 info->info.dma.setup = eesoxscsi_dma_setup;
578 info->info.dma.pseudo = eesoxscsi_dma_pseudo;
579 info->info.dma.stop = eesoxscsi_dma_stop;
581 device_create_file(&ec->dev, &dev_attr_bus_term);
583 ret = fas216_init(host);
584 if (ret)
585 goto out_free;
587 ret = request_irq(host->irq, eesoxscsi_intr, 0, "eesoxscsi", info);
588 if (ret) {
589 printk("scsi%d: IRQ%d not free: %d\n",
590 host->host_no, host->irq, ret);
591 goto out_remove;
594 if (host->dma_channel != NO_DMA) {
595 if (request_dma(host->dma_channel, "eesox")) {
596 printk("scsi%d: DMA%d not free, DMA disabled\n",
597 host->host_no, host->dma_channel);
598 host->dma_channel = NO_DMA;
599 } else {
600 set_dma_speed(host->dma_channel, 180);
601 info->info.ifcfg.capabilities |= FASCAP_DMA;
602 info->info.ifcfg.cntl3 |= CNTL3_BS8;
606 ret = fas216_add(host, &ec->dev);
607 if (ret == 0)
608 goto out;
610 if (host->dma_channel != NO_DMA)
611 free_dma(host->dma_channel);
612 free_irq(host->irq, host);
614 out_remove:
615 fas216_remove(host);
617 out_free:
618 device_remove_file(&ec->dev, &dev_attr_bus_term);
619 scsi_host_put(host);
621 out_unmap:
622 iounmap(base);
624 out_region:
625 release_mem_region(resbase, reslen);
627 out:
628 return ret;
631 static void __devexit eesoxscsi_remove(struct expansion_card *ec)
633 struct Scsi_Host *host = ecard_get_drvdata(ec);
634 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
635 unsigned long resbase, reslen;
637 ecard_set_drvdata(ec, NULL);
638 fas216_remove(host);
640 if (host->dma_channel != NO_DMA)
641 free_dma(host->dma_channel);
642 free_irq(host->irq, info);
644 device_remove_file(&ec->dev, &dev_attr_bus_term);
646 iounmap((void *)host->base);
648 resbase = ecard_resource_start(ec, ECARD_RES_IOCFAST);
649 reslen = ecard_resource_len(ec, ECARD_RES_IOCFAST);
651 release_mem_region(resbase, reslen);
653 fas216_release(host);
654 scsi_host_put(host);
657 static const struct ecard_id eesoxscsi_cids[] = {
658 { MANU_EESOX, PROD_EESOX_SCSI2 },
659 { 0xffff, 0xffff },
662 static struct ecard_driver eesoxscsi_driver = {
663 .probe = eesoxscsi_probe,
664 .remove = __devexit_p(eesoxscsi_remove),
665 .id_table = eesoxscsi_cids,
666 .drv = {
667 .name = "eesoxscsi",
671 static int __init eesox_init(void)
673 return ecard_register_driver(&eesoxscsi_driver);
676 static void __exit eesox_exit(void)
678 ecard_remove_driver(&eesoxscsi_driver);
681 module_init(eesox_init);
682 module_exit(eesox_exit);
684 MODULE_AUTHOR("Russell King");
685 MODULE_DESCRIPTION("EESOX 'Fast' SCSI driver for Acorn machines");
686 MODULE_PARM(term, "1-8i");
687 MODULE_PARM_DESC(term, "SCSI bus termination");
688 MODULE_LICENSE("GPL");