blob: 7f09d89100a133a894b79c7332641043c31c2814 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/ioport.h>
4#include <linux/init.h>
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +02005#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/spinlock.h>
7#include <linux/interrupt.h>
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +02008#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/page.h>
11#include <asm/pgtable.h>
12#include <asm/amigaints.h>
13#include <asm/amigahw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include "scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "wd33c93.h"
17#include "a3000.h"
18
Adrian Bunk9387edb2009-03-04 12:06:08 -080019
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020020static irqreturn_t a3000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020022 struct Scsi_Host *instance = data;
Geert Uytterhoevenc57c1ca2009-05-17 21:05:53 +020023 struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
Geert Uytterhoevend7537222009-05-17 12:17:23 +020024 unsigned int status = regs->ISTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 if (!(status & ISTR_INT_P))
28 return IRQ_NONE;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020029 if (status & ISTR_INTS) {
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020030 spin_lock_irqsave(instance->host_lock, flags);
31 wd33c93_intr(instance);
32 spin_unlock_irqrestore(instance->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return IRQ_HANDLED;
34 }
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +020035 pr_warning("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return IRQ_NONE;
37}
38
Henrik Kretzschmar65396412006-09-12 23:49:33 +020039static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020041 struct Scsi_Host *instance = cmd->device->host;
42 struct WD33C93_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenc57c1ca2009-05-17 21:05:53 +020043 struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020044 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
45 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Geert Uytterhoeven21351012010-04-04 11:00:35 +020047 /*
48 * if the physical address has the wrong alignment, or if
49 * physical address is bad, or if it is a write and at the
50 * end of a physical memory chunk, then allocate a bounce
51 * buffer
52 */
53 if (addr & A3000_XFER_MASK) {
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020054 hdata->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
55 hdata->dma_bounce_buffer = kmalloc(hdata->dma_bounce_len,
56 GFP_KERNEL);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020057
58 /* can't allocate memory; use PIO */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020059 if (!hdata->dma_bounce_buffer) {
60 hdata->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020061 return 1;
62 }
63
64 if (!dir_in) {
65 /* copy to bounce buffer for a write */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020066 memcpy(hdata->dma_bounce_buffer, cmd->SCp.ptr,
67 cmd->SCp.this_residual);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020068 }
69
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020070 addr = virt_to_bus(hdata->dma_bounce_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72
Geert Uytterhoeven21351012010-04-04 11:00:35 +020073 /* setup dma direction */
74 if (!dir_in)
75 cntr |= CNTR_DDIR;
76
77 /* remember direction */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020078 hdata->dma_dir = dir_in;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020079
Geert Uytterhoevend7537222009-05-17 12:17:23 +020080 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020081
82 /* setup DMA *physical* address */
Geert Uytterhoevend7537222009-05-17 12:17:23 +020083 regs->ACR = addr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020084
85 if (dir_in) {
86 /* invalidate any cache */
87 cache_clear(addr, cmd->SCp.this_residual);
88 } else {
89 /* push any dirty cache */
90 cache_push(addr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
Geert Uytterhoeven21351012010-04-04 11:00:35 +020093 /* start DMA */
94 mb(); /* make sure setup is completed */
Geert Uytterhoevend7537222009-05-17 12:17:23 +020095 regs->ST_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020096 mb(); /* make sure DMA has started before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Geert Uytterhoeven21351012010-04-04 11:00:35 +020098 /* return success */
99 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200102static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
103 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200105 struct WD33C93_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenc57c1ca2009-05-17 21:05:53 +0200106 struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200107
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200108 /* disable SCSI interrupts */
109 unsigned short cntr = CNTR_PDMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200111 if (!hdata->dma_dir)
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200112 cntr |= CNTR_DDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200114 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200115 mb(); /* make sure CNTR is updated before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200117 /* flush if we were reading */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200118 if (hdata->dma_dir) {
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200119 regs->FLUSH = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200120 mb(); /* don't allow prefetch */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200121 while (!(regs->ISTR & ISTR_FE_FLG))
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200122 barrier();
123 mb(); /* no IO until FLUSH is done */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200125
126 /* clear a possible interrupt */
127 /* I think that this CINT is only necessary if you are
128 * using the terminal count features. HM 7 Mar 1994
129 */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200130 regs->CINT = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200131
132 /* stop DMA */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200133 regs->SP_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200134 mb(); /* make sure DMA is stopped before next IO */
135
136 /* restore the CONTROL bits (minus the direction flag) */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200137 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200138 mb(); /* make sure CNTR is updated before next IO */
139
140 /* copy from a bounce buffer, if necessary */
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200141 if (status && hdata->dma_bounce_buffer) {
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200142 if (SCpnt) {
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200143 if (hdata->dma_dir && SCpnt)
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200144 memcpy(SCpnt->SCp.ptr,
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200145 hdata->dma_bounce_buffer,
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200146 SCpnt->SCp.this_residual);
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200147 kfree(hdata->dma_bounce_buffer);
148 hdata->dma_bounce_buffer = NULL;
149 hdata->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200150 } else {
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200151 kfree(hdata->dma_bounce_buffer);
152 hdata->dma_bounce_buffer = NULL;
153 hdata->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200154 }
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200158static int a3000_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200160 struct Scsi_Host *instance = cmd->device->host;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* FIXME perform bus-specific reset */
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200163
Jeff Garzik df0ae242005-05-28 07:57:14 -0400164 /* FIXME 2: kill this entire function, which should
165 cause mid-layer to call wd33c93_host_reset anyway? */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400166
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200167 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 wd33c93_host_reset(cmd);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200169 spin_unlock_irq(instance->host_lock);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return SUCCESS;
172}
173
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200174static struct scsi_host_template amiga_a3000_scsi_template = {
175 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .name = "Amiga 3000 built-in SCSI",
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200177 .proc_info = wd33c93_proc_info,
178 .proc_name = "A3000",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .queuecommand = wd33c93_queuecommand,
180 .eh_abort_handler = wd33c93_abort,
181 .eh_bus_reset_handler = a3000_bus_reset,
182 .eh_host_reset_handler = wd33c93_host_reset,
183 .can_queue = CAN_QUEUE,
184 .this_id = 7,
185 .sg_tablesize = SG_ALL,
186 .cmd_per_lun = CMD_PER_LUN,
187 .use_clustering = ENABLE_CLUSTERING
188};
189
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200190static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200192 struct resource *res;
193 struct Scsi_Host *instance;
194 int error;
195 struct a3000_scsiregs *regs;
196 wd33c93_regs wdregs;
197 struct WD33C93_hostdata *hdata;
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200198
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200199 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 if (!res)
201 return -ENODEV;
202
203 if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
204 return -EBUSY;
205
206 instance = scsi_host_alloc(&amiga_a3000_scsi_template,
207 sizeof(struct WD33C93_hostdata));
208 if (!instance) {
209 error = -ENOMEM;
210 goto fail_alloc;
211 }
212
213 instance->base = ZTWO_VADDR(res->start);
214 instance->irq = IRQ_AMIGA_PORTS;
215
216 regs = (struct a3000_scsiregs *)(instance->base);
217 regs->DAWR = DAWR_A3000;
218
219 wdregs.SASR = &regs->SASR;
220 wdregs.SCMD = &regs->SCMD;
221
222 hdata = shost_priv(instance);
223 hdata->no_sync = 0xff;
224 hdata->fast = 0;
225 hdata->dma_mode = CTRL_DMA;
226
227 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
228 error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
229 "A3000 SCSI", instance);
230 if (error)
231 goto fail_irq;
232
233 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
234
235 error = scsi_add_host(instance, NULL);
236 if (error)
237 goto fail_host;
238
239 platform_set_drvdata(pdev, instance);
240
241 scsi_scan_host(instance);
242 return 0;
243
244fail_host:
245 free_irq(IRQ_AMIGA_PORTS, instance);
246fail_irq:
247 scsi_host_put(instance);
248fail_alloc:
249 release_mem_region(res->start, resource_size(res));
250 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200253static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
254{
255 struct Scsi_Host *instance = platform_get_drvdata(pdev);
256 struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
257 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258
259 regs->CNTR = 0;
260 scsi_remove_host(instance);
261 free_irq(IRQ_AMIGA_PORTS, instance);
262 scsi_host_put(instance);
263 release_mem_region(res->start, resource_size(res));
264 return 0;
265}
266
267static struct platform_driver amiga_a3000_scsi_driver = {
268 .remove = __exit_p(amiga_a3000_scsi_remove),
269 .driver = {
270 .name = "amiga-a3000-scsi",
271 .owner = THIS_MODULE,
272 },
273};
274
275static int __init amiga_a3000_scsi_init(void)
276{
277 return platform_driver_probe(&amiga_a3000_scsi_driver,
278 amiga_a3000_scsi_probe);
279}
280module_init(amiga_a3000_scsi_init);
281
282static void __exit amiga_a3000_scsi_exit(void)
283{
284 platform_driver_unregister(&amiga_a3000_scsi_driver);
285}
286module_exit(amiga_a3000_scsi_exit);
287
288MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289MODULE_LICENSE("GPL");
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200290MODULE_ALIAS("platform:amiga-a3000-scsi");