blob: ced21fe128c6a728967624c7cad32478671dfaba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/acorn/scsi/eesox.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 * 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!
13 *
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.
24 */
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>
36
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>
42
43#include "../scsi.h"
44#include <scsi/scsi_host.h>
45#include "fas216.h"
46#include "scsi.h"
47
48#include <scsi/scsicam.h>
49
50#define EESOX_FAS216_OFFSET 0x3000
51#define EESOX_FAS216_SHIFT 5
52
53#define EESOX_DMASTAT 0x2800
54#define EESOX_STAT_INTR 0x01
55#define EESOX_STAT_DMA 0x02
56
57#define EESOX_CONTROL 0x2800
58#define EESOX_INTR_ENABLE 0x04
59#define EESOX_TERM_ENABLE 0x02
60#define EESOX_RESET 0x01
61
62#define EESOX_DMADATA 0x3800
63
64#define VERSION "1.10 (17/01/2003 2.5.59)"
65
66/*
67 * Use term=0,1,0,0,0 to turn terminators on/off
68 */
69static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
70
71#define NR_SG 256
72
73struct eesoxscsi_info {
74 FAS216_Info info;
75 struct expansion_card *ec;
76 void __iomem *base;
77 void __iomem *ctl_port;
78 unsigned int control;
79 struct scatterlist sg[NR_SG]; /* Scatter DMA list */
80};
81
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
86 */
87static void
88eesoxscsi_irqenable(struct expansion_card *ec, int irqnr)
89{
90 struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
91
92 info->control |= EESOX_INTR_ENABLE;
93
94 writeb(info->control, info->ctl_port);
95}
96
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
101 */
102static void
103eesoxscsi_irqdisable(struct expansion_card *ec, int irqnr)
104{
105 struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
106
107 info->control &= ~EESOX_INTR_ENABLE;
108
109 writeb(info->control, info->ctl_port);
110}
111
112static const expansioncard_ops_t eesoxscsi_ops = {
113 .irqenable = eesoxscsi_irqenable,
114 .irqdisable = eesoxscsi_irqdisable,
115};
116
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
121 */
122static void
123eesoxscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
124{
125 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
126 unsigned long flags;
127
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;
133
134 writeb(info->control, info->ctl_port);
135 spin_unlock_irqrestore(host->host_lock, flags);
136}
137
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
143 */
144static irqreturn_t
145eesoxscsi_intr(int irq, void *dev_id, struct pt_regs *regs)
146{
147 struct eesoxscsi_info *info = dev_id;
148
149 return fas216_intr(&info->info);
150}
151
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
159 */
160static fasdmatype_t
161eesoxscsi_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
162 fasdmadir_t direction, fasdmatype_t min_type)
163{
164 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
165 struct device *dev = scsi_get_device(host);
166 int dmach = info->info.scsi.dma;
167
168 if (dmach != NO_DMA &&
169 (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
170 int bufs, map_dir, dma_dir;
171
172 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
173
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;
180
181 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
182
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;
188 }
189 /*
190 * We don't do DMA, we only do slow PIO
191 *
192 * Some day, we will do Pseudo DMA
193 */
194 return fasdma_pseudo;
195}
196
197static void eesoxscsi_buffer_in(void *buf, int length, void __iomem *base)
198{
199 const void __iomem *reg_fas = base + EESOX_FAS216_OFFSET;
200 const void __iomem *reg_dmastat = base + EESOX_DMASTAT;
201 const void __iomem *reg_dmadata = base + EESOX_DMADATA;
202 const register unsigned long mask = 0xffff;
203
204 do {
205 unsigned int status;
206
207 /*
208 * Interrupt request?
209 */
210 status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
211 if (status & STAT_INT)
212 break;
213
214 /*
215 * DMA request active?
216 */
217 status = readb(reg_dmastat);
218 if (!(status & EESOX_STAT_DMA))
219 continue;
220
221 /*
222 * Get number of bytes in FIFO
223 */
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;
229
230 /*
231 * Align buffer.
232 */
233 if (((u32)buf) & 2 && status >= 2) {
234 *(u16 *)buf = readl(reg_dmadata);
235 buf += 2;
236 status -= 2;
237 length -= 2;
238 }
239
240 if (status >= 8) {
241 unsigned long l1, l2;
242
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;
253 }
254
255 if (status >= 4) {
256 unsigned long l1;
257
258 l1 = readl(reg_dmadata) & mask;
259 l1 |= readl(reg_dmadata) << 16;
260
261 *(u32 *)buf = l1;
262 buf += 4;
263 length -= 4;
264 continue;
265 }
266
267 if (status >= 2) {
268 *(u16 *)buf = readl(reg_dmadata);
269 buf += 2;
270 length -= 2;
271 }
272 } while (length);
273}
274
275static void eesoxscsi_buffer_out(void *buf, int length, void __iomem *base)
276{
277 const void __iomem *reg_fas = base + EESOX_FAS216_OFFSET;
278 const void __iomem *reg_dmastat = base + EESOX_DMASTAT;
279 const void __iomem *reg_dmadata = base + EESOX_DMADATA;
280
281 do {
282 unsigned int status;
283
284 /*
285 * Interrupt request?
286 */
287 status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
288 if (status & STAT_INT)
289 break;
290
291 /*
292 * DMA request active?
293 */
294 status = readb(reg_dmastat);
295 if (!(status & EESOX_STAT_DMA))
296 continue;
297
298 /*
299 * Get number of bytes in FIFO
300 */
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;
308
309 /*
310 * Align buffer.
311 */
312 if (((u32)buf) & 2 && status >= 2) {
313 writel(*(u16 *)buf << 16, reg_dmadata);
314 buf += 2;
315 status -= 2;
316 length -= 2;
317 }
318
319 if (status >= 8) {
320 unsigned long l1, l2;
321
322 l1 = *(u32 *)buf;
323 buf += 4;
324 l2 = *(u32 *)buf;
325 buf += 4;
326
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;
333 }
334
335 if (status >= 4) {
336 unsigned long l1;
337
338 l1 = *(u32 *)buf;
339 buf += 4;
340
341 writel(l1 << 16, reg_dmadata);
342 writel(l1, reg_dmadata);
343 length -= 4;
344 continue;
345 }
346
347 if (status >= 2) {
348 writel(*(u16 *)buf << 16, reg_dmadata);
349 buf += 2;
350 length -= 2;
351 }
352 } while (length);
353}
354
355static void
356eesoxscsi_dma_pseudo(struct Scsi_Host *host, Scsi_Pointer *SCp,
357 fasdmadir_t dir, int transfer_size)
358{
359 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
360 if (dir == DMA_IN) {
361 eesoxscsi_buffer_in(SCp->ptr, SCp->this_residual, info->base);
362 } else {
363 eesoxscsi_buffer_out(SCp->ptr, SCp->this_residual, info->base);
364 }
365}
366
367/* Prototype: int eesoxscsi_dma_stop(host, SCpnt)
368 * Purpose : stops DMA/PIO
369 * Params : host - host
370 * SCpnt - command
371 */
372static void
373eesoxscsi_dma_stop(struct Scsi_Host *host, Scsi_Pointer *SCp)
374{
375 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
376 if (info->info.scsi.dma != NO_DMA)
377 disable_dma(info->info.scsi.dma);
378}
379
380/* Prototype: const char *eesoxscsi_info(struct Scsi_Host * host)
381 * Purpose : returns a descriptive string about this interface,
382 * Params : host - driver host structure to return info for.
383 * Returns : pointer to a static buffer containing null terminated string.
384 */
385const char *eesoxscsi_info(struct Scsi_Host *host)
386{
387 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
388 static char string[150];
389
390 sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
391 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
392 VERSION, info->control & EESOX_TERM_ENABLE ? "n" : "ff");
393
394 return string;
395}
396
397/* Prototype: int eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
398 * Purpose : Set a driver specific function
399 * Params : host - host to setup
400 * : buffer - buffer containing string describing operation
401 * : length - length of string
402 * Returns : -EINVAL, or 0
403 */
404static int
405eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
406{
407 int ret = length;
408
409 if (length >= 9 && strncmp(buffer, "EESOXSCSI", 9) == 0) {
410 buffer += 9;
411 length -= 9;
412
413 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
414 if (buffer[5] == '1')
415 eesoxscsi_terminator_ctl(host, 1);
416 else if (buffer[5] == '0')
417 eesoxscsi_terminator_ctl(host, 0);
418 else
419 ret = -EINVAL;
420 } else
421 ret = -EINVAL;
422 } else
423 ret = -EINVAL;
424
425 return ret;
426}
427
428/* Prototype: int eesoxscsi_proc_info(char *buffer, char **start, off_t offset,
429 * int length, int host_no, int inout)
430 * Purpose : Return information about the driver to a user process accessing
431 * the /proc filesystem.
432 * Params : buffer - a buffer to write information to
433 * start - a pointer into this buffer set by this routine to the start
434 * of the required information.
435 * offset - offset into information that we have read upto.
436 * length - length of buffer
437 * host_no - host number to return information for
438 * inout - 0 for reading, 1 for writing.
439 * Returns : length of data written to buffer.
440 */
441int eesoxscsi_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
442 int length, int inout)
443{
444 struct eesoxscsi_info *info;
445 char *p = buffer;
446 int pos;
447
448 if (inout == 1)
449 return eesoxscsi_set_proc_info(host, buffer, length);
450
451 info = (struct eesoxscsi_info *)host->hostdata;
452
453 p += sprintf(p, "EESOX SCSI driver v%s\n", VERSION);
454 p += fas216_print_host(&info->info, p);
455 p += sprintf(p, "Term : o%s\n",
456 info->control & EESOX_TERM_ENABLE ? "n" : "ff");
457
458 p += fas216_print_stats(&info->info, p);
459 p += fas216_print_devices(&info->info, p);
460
461 *start = buffer + offset;
462 pos = p - buffer - offset;
463 if (pos > length)
464 pos = length;
465
466 return pos;
467}
468
Yani Ioannou10523b32005-05-17 06:43:37 -0400469static ssize_t eesoxscsi_show_term(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct expansion_card *ec = ECARD_DEV(dev);
472 struct Scsi_Host *host = ecard_get_drvdata(ec);
473 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
474
475 return sprintf(buf, "%d\n", info->control & EESOX_TERM_ENABLE ? 1 : 0);
476}
477
Yani Ioannou10523b32005-05-17 06:43:37 -0400478static ssize_t eesoxscsi_store_term(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct expansion_card *ec = ECARD_DEV(dev);
481 struct Scsi_Host *host = ecard_get_drvdata(ec);
482 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
483 unsigned long flags;
484
485 if (len > 1) {
486 spin_lock_irqsave(host->host_lock, flags);
487 if (buf[0] != '0') {
488 info->control |= EESOX_TERM_ENABLE;
489 } else {
490 info->control &= ~EESOX_TERM_ENABLE;
491 }
492 writeb(info->control, info->ctl_port);
493 spin_unlock_irqrestore(host->host_lock, flags);
494 }
495
496 return len;
497}
498
499static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
500 eesoxscsi_show_term, eesoxscsi_store_term);
501
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100502static struct scsi_host_template eesox_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .module = THIS_MODULE,
504 .proc_info = eesoxscsi_proc_info,
505 .name = "EESOX SCSI",
506 .info = eesoxscsi_info,
507 .queuecommand = fas216_queue_command,
508 .eh_host_reset_handler = fas216_eh_host_reset,
509 .eh_bus_reset_handler = fas216_eh_bus_reset,
510 .eh_device_reset_handler = fas216_eh_device_reset,
511 .eh_abort_handler = fas216_eh_abort,
512 .can_queue = 1,
513 .this_id = 7,
514 .sg_tablesize = SG_ALL,
515 .cmd_per_lun = 1,
516 .use_clustering = DISABLE_CLUSTERING,
517 .proc_name = "eesox",
518};
519
520static int __devinit
521eesoxscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
522{
523 struct Scsi_Host *host;
524 struct eesoxscsi_info *info;
525 unsigned long resbase, reslen;
526 void __iomem *base;
527 int ret;
528
529 ret = ecard_request_resources(ec);
530 if (ret)
531 goto out;
532
533 resbase = ecard_resource_start(ec, ECARD_RES_IOCFAST);
534 reslen = ecard_resource_len(ec, ECARD_RES_IOCFAST);
535 base = ioremap(resbase, reslen);
536 if (!base) {
537 ret = -ENOMEM;
538 goto out_region;
539 }
540
541 host = scsi_host_alloc(&eesox_template,
542 sizeof(struct eesoxscsi_info));
543 if (!host) {
544 ret = -ENOMEM;
545 goto out_unmap;
546 }
547
548 ecard_set_drvdata(ec, host);
549
550 info = (struct eesoxscsi_info *)host->hostdata;
551 info->ec = ec;
552 info->base = base;
553 info->ctl_port = base + EESOX_CONTROL;
554 info->control = term[ec->slot_no] ? EESOX_TERM_ENABLE : 0;
555 writeb(info->control, info->ctl_port);
556
557 info->info.scsi.io_base = base + EESOX_FAS216_OFFSET;
558 info->info.scsi.io_shift = EESOX_FAS216_SHIFT;
559 info->info.scsi.irq = ec->irq;
560 info->info.scsi.dma = ec->dma;
561 info->info.ifcfg.clockrate = 40; /* MHz */
562 info->info.ifcfg.select_timeout = 255;
563 info->info.ifcfg.asyncperiod = 200; /* ns */
564 info->info.ifcfg.sync_max_depth = 7;
565 info->info.ifcfg.cntl3 = CNTL3_FASTSCSI | CNTL3_FASTCLK;
566 info->info.ifcfg.disconnect_ok = 1;
567 info->info.ifcfg.wide_max_size = 0;
568 info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
569 info->info.dma.setup = eesoxscsi_dma_setup;
570 info->info.dma.pseudo = eesoxscsi_dma_pseudo;
571 info->info.dma.stop = eesoxscsi_dma_stop;
572
573 ec->irqaddr = base + EESOX_DMASTAT;
574 ec->irqmask = EESOX_STAT_INTR;
575 ec->irq_data = info;
576 ec->ops = &eesoxscsi_ops;
577
578 device_create_file(&ec->dev, &dev_attr_bus_term);
579
580 ret = fas216_init(host);
581 if (ret)
582 goto out_free;
583
584 ret = request_irq(ec->irq, eesoxscsi_intr, 0, "eesoxscsi", info);
585 if (ret) {
586 printk("scsi%d: IRQ%d not free: %d\n",
587 host->host_no, ec->irq, ret);
588 goto out_remove;
589 }
590
591 if (info->info.scsi.dma != NO_DMA) {
592 if (request_dma(info->info.scsi.dma, "eesox")) {
593 printk("scsi%d: DMA%d not free, DMA disabled\n",
594 host->host_no, info->info.scsi.dma);
595 info->info.scsi.dma = NO_DMA;
596 } else {
597 set_dma_speed(info->info.scsi.dma, 180);
598 info->info.ifcfg.capabilities |= FASCAP_DMA;
599 info->info.ifcfg.cntl3 |= CNTL3_BS8;
600 }
601 }
602
603 ret = fas216_add(host, &ec->dev);
604 if (ret == 0)
605 goto out;
606
607 if (info->info.scsi.dma != NO_DMA)
608 free_dma(info->info.scsi.dma);
609 free_irq(ec->irq, host);
610
611 out_remove:
612 fas216_remove(host);
613
614 out_free:
615 device_remove_file(&ec->dev, &dev_attr_bus_term);
616 scsi_host_put(host);
617
618 out_unmap:
619 iounmap(base);
620
621 out_region:
622 ecard_release_resources(ec);
623
624 out:
625 return ret;
626}
627
628static void __devexit eesoxscsi_remove(struct expansion_card *ec)
629{
630 struct Scsi_Host *host = ecard_get_drvdata(ec);
631 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
632
633 ecard_set_drvdata(ec, NULL);
634 fas216_remove(host);
635
636 if (info->info.scsi.dma != NO_DMA)
637 free_dma(info->info.scsi.dma);
638 free_irq(ec->irq, info);
639
640 device_remove_file(&ec->dev, &dev_attr_bus_term);
641
642 iounmap(info->base);
643
644 fas216_release(host);
645 scsi_host_put(host);
646 ecard_release_resources(ec);
647}
648
649static const struct ecard_id eesoxscsi_cids[] = {
650 { MANU_EESOX, PROD_EESOX_SCSI2 },
651 { 0xffff, 0xffff },
652};
653
654static struct ecard_driver eesoxscsi_driver = {
655 .probe = eesoxscsi_probe,
656 .remove = __devexit_p(eesoxscsi_remove),
657 .id_table = eesoxscsi_cids,
658 .drv = {
659 .name = "eesoxscsi",
660 },
661};
662
663static int __init eesox_init(void)
664{
665 return ecard_register_driver(&eesoxscsi_driver);
666}
667
668static void __exit eesox_exit(void)
669{
670 ecard_remove_driver(&eesoxscsi_driver);
671}
672
673module_init(eesox_init);
674module_exit(eesox_exit);
675
676MODULE_AUTHOR("Russell King");
677MODULE_DESCRIPTION("EESOX 'Fast' SCSI driver for Acorn machines");
678MODULE_PARM(term, "1-8i");
679MODULE_PARM_DESC(term, "SCSI bus termination");
680MODULE_LICENSE("GPL");