blob: bc6eb69f5fd09554ffe714e5339e3e9ec67b7a40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/ioport.h>
6#include <linux/init.h>
7#include <linux/spinlock.h>
8#include <linux/interrupt.h>
9
10#include <asm/setup.h>
11#include <asm/page.h>
12#include <asm/pgtable.h>
13#include <asm/amigaints.h>
14#include <asm/amigahw.h>
15#include <asm/irq.h>
16
17#include "scsi.h"
18#include <scsi/scsi_host.h>
19#include "wd33c93.h"
20#include "a3000.h"
21
Geert Uytterhoeven21351012010-04-04 11:00:35 +020022#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Geert Uytterhoeven21351012010-04-04 11:00:35 +020024
25#define DMA(ptr) ((a3000_scsiregs *)((ptr)->base))
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static struct Scsi_Host *a3000_host = NULL;
28
Adrian Bunk9387edb2009-03-04 12:06:08 -080029static int a3000_release(struct Scsi_Host *instance);
30
Geert Uytterhoeven21351012010-04-04 11:00:35 +020031static irqreturn_t a3000_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 unsigned long flags;
34 unsigned int status = DMA(a3000_host)->ISTR;
35
36 if (!(status & ISTR_INT_P))
37 return IRQ_NONE;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020038 if (status & ISTR_INTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 spin_lock_irqsave(a3000_host->host_lock, flags);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020040 wd33c93_intr(a3000_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 spin_unlock_irqrestore(a3000_host->host_lock, flags);
42 return IRQ_HANDLED;
43 }
44 printk("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
45 return IRQ_NONE;
46}
47
Henrik Kretzschmar65396412006-09-12 23:49:33 +020048static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020050 struct WD33C93_hostdata *hdata = shost_priv(a3000_host);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020051 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
52 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Geert Uytterhoeven21351012010-04-04 11:00:35 +020054 /*
55 * if the physical address has the wrong alignment, or if
56 * physical address is bad, or if it is a write and at the
57 * end of a physical memory chunk, then allocate a bounce
58 * buffer
59 */
60 if (addr & A3000_XFER_MASK) {
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020061 hdata->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
62 hdata->dma_bounce_buffer = kmalloc(hdata->dma_bounce_len,
63 GFP_KERNEL);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020064
65 /* can't allocate memory; use PIO */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020066 if (!hdata->dma_bounce_buffer) {
67 hdata->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020068 return 1;
69 }
70
71 if (!dir_in) {
72 /* copy to bounce buffer for a write */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020073 memcpy(hdata->dma_bounce_buffer, cmd->SCp.ptr,
74 cmd->SCp.this_residual);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020075 }
76
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020077 addr = virt_to_bus(hdata->dma_bounce_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
Geert Uytterhoeven21351012010-04-04 11:00:35 +020080 /* setup dma direction */
81 if (!dir_in)
82 cntr |= CNTR_DDIR;
83
84 /* remember direction */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020085 hdata->dma_dir = dir_in;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020086
87 DMA(a3000_host)->CNTR = cntr;
88
89 /* setup DMA *physical* address */
90 DMA(a3000_host)->ACR = addr;
91
92 if (dir_in) {
93 /* invalidate any cache */
94 cache_clear(addr, cmd->SCp.this_residual);
95 } else {
96 /* push any dirty cache */
97 cache_push(addr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200100 /* start DMA */
101 mb(); /* make sure setup is completed */
102 DMA(a3000_host)->ST_DMA = 1;
103 mb(); /* make sure DMA has started before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200105 /* return success */
106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200109static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
110 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200112 struct WD33C93_hostdata *hdata = shost_priv(instance);
113
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200114 /* disable SCSI interrupts */
115 unsigned short cntr = CNTR_PDMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200117 if (!hdata->dma_dir)
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200118 cntr |= CNTR_DDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200120 DMA(instance)->CNTR = cntr;
121 mb(); /* make sure CNTR is updated before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200123 /* flush if we were reading */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200124 if (hdata->dma_dir) {
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200125 DMA(instance)->FLUSH = 1;
126 mb(); /* don't allow prefetch */
127 while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
128 barrier();
129 mb(); /* no IO until FLUSH is done */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200131
132 /* clear a possible interrupt */
133 /* I think that this CINT is only necessary if you are
134 * using the terminal count features. HM 7 Mar 1994
135 */
136 DMA(instance)->CINT = 1;
137
138 /* stop DMA */
139 DMA(instance)->SP_DMA = 1;
140 mb(); /* make sure DMA is stopped before next IO */
141
142 /* restore the CONTROL bits (minus the direction flag) */
143 DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
144 mb(); /* make sure CNTR is updated before next IO */
145
146 /* copy from a bounce buffer, if necessary */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200147 if (status && hdata->dma_bounce_buffer) {
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200148 if (SCpnt) {
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200149 if (hdata->dma_dir && SCpnt)
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200150 memcpy(SCpnt->SCp.ptr,
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200151 hdata->dma_bounce_buffer,
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200152 SCpnt->SCp.this_residual);
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200153 kfree(hdata->dma_bounce_buffer);
154 hdata->dma_bounce_buffer = NULL;
155 hdata->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200156 } else {
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200157 kfree(hdata->dma_bounce_buffer);
158 hdata->dma_bounce_buffer = NULL;
159 hdata->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200160 }
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Adrian Bunk9387edb2009-03-04 12:06:08 -0800164static int __init a3000_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200166 wd33c93_regs regs;
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200167 struct WD33C93_hostdata *hdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200169 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(A3000_SCSI))
170 return 0;
171 if (!request_mem_region(0xDD0000, 256, "wd33c93"))
172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200174 tpnt->proc_name = "A3000";
175 tpnt->proc_info = &wd33c93_proc_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200177 a3000_host = scsi_register(tpnt, sizeof(struct WD33C93_hostdata));
178 if (a3000_host == NULL)
179 goto fail_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200181 a3000_host->base = ZTWO_VADDR(0xDD0000);
182 a3000_host->irq = IRQ_AMIGA_PORTS;
183 DMA(a3000_host)->DAWR = DAWR_A3000;
184 regs.SASR = &(DMA(a3000_host)->SASR);
185 regs.SCMD = &(DMA(a3000_host)->SCMD);
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200186 hdata = shost_priv(a3000_host);
187 hdata->no_sync = 0xff;
188 hdata->fast = 0;
189 hdata->dma_mode = CTRL_DMA;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200190 wd33c93_init(a3000_host, regs, dma_setup, dma_stop, WD33C93_FS_12_15);
191 if (request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, "A3000 SCSI",
192 a3000_intr))
193 goto fail_irq;
194 DMA(a3000_host)->CNTR = CNTR_PDMD | CNTR_INTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200196 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198fail_irq:
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200199 scsi_unregister(a3000_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200fail_register:
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200201 release_mem_region(0xDD0000, 256);
202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200205static int a3000_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 /* FIXME perform bus-specific reset */
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200208
Jeff Garzik df0ae242005-05-28 07:57:14 -0400209 /* FIXME 2: kill this entire function, which should
210 cause mid-layer to call wd33c93_host_reset anyway? */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400211
212 spin_lock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 wd33c93_host_reset(cmd);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400214 spin_unlock_irq(cmd->device->host->host_lock);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return SUCCESS;
217}
218
219#define HOSTS_C
220
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100221static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .proc_name = "A3000",
223 .name = "Amiga 3000 built-in SCSI",
224 .detect = a3000_detect,
225 .release = a3000_release,
226 .queuecommand = wd33c93_queuecommand,
227 .eh_abort_handler = wd33c93_abort,
228 .eh_bus_reset_handler = a3000_bus_reset,
229 .eh_host_reset_handler = wd33c93_host_reset,
230 .can_queue = CAN_QUEUE,
231 .this_id = 7,
232 .sg_tablesize = SG_ALL,
233 .cmd_per_lun = CMD_PER_LUN,
234 .use_clustering = ENABLE_CLUSTERING
235};
236
237
238#include "scsi_module.c"
239
Adrian Bunk9387edb2009-03-04 12:06:08 -0800240static int a3000_release(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200242 DMA(instance)->CNTR = 0;
243 release_mem_region(0xDD0000, 256);
244 free_irq(IRQ_AMIGA_PORTS, a3000_intr);
245 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248MODULE_LICENSE("GPL");