blob: ae9b25a3e3e72f0d53cfff111eb44c27dad971c4 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static struct Scsi_Host *a3000_host = NULL;
26
Adrian Bunk9387edb2009-03-04 12:06:08 -080027static int a3000_release(struct Scsi_Host *instance);
28
Geert Uytterhoeven21351012010-04-04 11:00:35 +020029static irqreturn_t a3000_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Geert Uytterhoevend7537222009-05-17 12:17:23 +020031 a3000_scsiregs *regs = (a3000_scsiregs *)(a3000_host->base);
32 unsigned int status = regs->ISTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 if (!(status & ISTR_INT_P))
36 return IRQ_NONE;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020037 if (status & ISTR_INTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 spin_lock_irqsave(a3000_host->host_lock, flags);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020039 wd33c93_intr(a3000_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 spin_unlock_irqrestore(a3000_host->host_lock, flags);
41 return IRQ_HANDLED;
42 }
43 printk("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
44 return IRQ_NONE;
45}
46
Henrik Kretzschmar65396412006-09-12 23:49:33 +020047static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020049 struct WD33C93_hostdata *hdata = shost_priv(a3000_host);
Geert Uytterhoevend7537222009-05-17 12:17:23 +020050 a3000_scsiregs *regs = (a3000_scsiregs *)(a3000_host->base);
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
Geert Uytterhoevend7537222009-05-17 12:17:23 +020087 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020088
89 /* setup DMA *physical* address */
Geert Uytterhoevend7537222009-05-17 12:17:23 +020090 regs->ACR = addr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020091
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 */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200102 regs->ST_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200103 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);
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200113 a3000_scsiregs *regs = (a3000_scsiregs *)(instance->base);
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200114
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200115 /* disable SCSI interrupts */
116 unsigned short cntr = CNTR_PDMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200118 if (!hdata->dma_dir)
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200119 cntr |= CNTR_DDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200121 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200122 mb(); /* make sure CNTR is updated before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200124 /* flush if we were reading */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200125 if (hdata->dma_dir) {
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200126 regs->FLUSH = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200127 mb(); /* don't allow prefetch */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200128 while (!(regs->ISTR & ISTR_FE_FLG))
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200129 barrier();
130 mb(); /* no IO until FLUSH is done */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200132
133 /* clear a possible interrupt */
134 /* I think that this CINT is only necessary if you are
135 * using the terminal count features. HM 7 Mar 1994
136 */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200137 regs->CINT = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200138
139 /* stop DMA */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200140 regs->SP_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200141 mb(); /* make sure DMA is stopped before next IO */
142
143 /* restore the CONTROL bits (minus the direction flag) */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200144 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200145 mb(); /* make sure CNTR is updated before next IO */
146
147 /* copy from a bounce buffer, if necessary */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200148 if (status && hdata->dma_bounce_buffer) {
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200149 if (SCpnt) {
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200150 if (hdata->dma_dir && SCpnt)
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200151 memcpy(SCpnt->SCp.ptr,
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200152 hdata->dma_bounce_buffer,
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200153 SCpnt->SCp.this_residual);
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200154 kfree(hdata->dma_bounce_buffer);
155 hdata->dma_bounce_buffer = NULL;
156 hdata->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200157 } else {
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200158 kfree(hdata->dma_bounce_buffer);
159 hdata->dma_bounce_buffer = NULL;
160 hdata->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200161 }
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Adrian Bunk9387edb2009-03-04 12:06:08 -0800165static int __init a3000_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200167 wd33c93_regs wdregs;
168 a3000_scsiregs *regs;
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200169 struct WD33C93_hostdata *hdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200171 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(A3000_SCSI))
172 return 0;
173 if (!request_mem_region(0xDD0000, 256, "wd33c93"))
174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200176 tpnt->proc_name = "A3000";
177 tpnt->proc_info = &wd33c93_proc_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200179 a3000_host = scsi_register(tpnt, sizeof(struct WD33C93_hostdata));
180 if (a3000_host == NULL)
181 goto fail_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200183 a3000_host->base = ZTWO_VADDR(0xDD0000);
184 a3000_host->irq = IRQ_AMIGA_PORTS;
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200185 regs = (a3000_scsiregs *)(a3000_host->base);
186 regs->DAWR = DAWR_A3000;
187 wdregs.SASR = &regs->SASR;
188 wdregs.SCMD = &regs->SCMD;
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200189 hdata = shost_priv(a3000_host);
190 hdata->no_sync = 0xff;
191 hdata->fast = 0;
192 hdata->dma_mode = CTRL_DMA;
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200193 wd33c93_init(a3000_host, wdregs, dma_setup, dma_stop,
194 WD33C93_FS_12_15);
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200195 if (request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, "A3000 SCSI",
196 a3000_intr))
197 goto fail_irq;
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200198 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200200 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202fail_irq:
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200203 scsi_unregister(a3000_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204fail_register:
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200205 release_mem_region(0xDD0000, 256);
206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200209static int a3000_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 /* FIXME perform bus-specific reset */
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200212
Jeff Garzik df0ae242005-05-28 07:57:14 -0400213 /* FIXME 2: kill this entire function, which should
214 cause mid-layer to call wd33c93_host_reset anyway? */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400215
216 spin_lock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 wd33c93_host_reset(cmd);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400218 spin_unlock_irq(cmd->device->host->host_lock);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return SUCCESS;
221}
222
223#define HOSTS_C
224
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100225static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 .proc_name = "A3000",
227 .name = "Amiga 3000 built-in SCSI",
228 .detect = a3000_detect,
229 .release = a3000_release,
230 .queuecommand = wd33c93_queuecommand,
231 .eh_abort_handler = wd33c93_abort,
232 .eh_bus_reset_handler = a3000_bus_reset,
233 .eh_host_reset_handler = wd33c93_host_reset,
234 .can_queue = CAN_QUEUE,
235 .this_id = 7,
236 .sg_tablesize = SG_ALL,
237 .cmd_per_lun = CMD_PER_LUN,
238 .use_clustering = ENABLE_CLUSTERING
239};
240
241
242#include "scsi_module.c"
243
Adrian Bunk9387edb2009-03-04 12:06:08 -0800244static int a3000_release(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200246 a3000_scsiregs *regs = (a3000_scsiregs *)(instance->base);
247
248 regs->CNTR = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200249 release_mem_region(0xDD0000, 256);
250 free_irq(IRQ_AMIGA_PORTS, a3000_intr);
251 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254MODULE_LICENSE("GPL");