blob: d9468027fb6150caed5a9854623b5673ddff58a4 [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 Uytterhoeven2b21d5e2010-04-12 21:55:15 +020020struct a3000_hostdata {
21 struct WD33C93_hostdata wh;
22 struct a3000_scsiregs *regs;
23};
24
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020025static irqreturn_t a3000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020027 struct Scsi_Host *instance = data;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020028 struct a3000_hostdata *hdata = shost_priv(instance);
29 unsigned int status = hdata->regs->ISTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 if (!(status & ISTR_INT_P))
33 return IRQ_NONE;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020034 if (status & ISTR_INTS) {
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020035 spin_lock_irqsave(instance->host_lock, flags);
36 wd33c93_intr(instance);
37 spin_unlock_irqrestore(instance->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return IRQ_HANDLED;
39 }
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +020040 pr_warning("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return IRQ_NONE;
42}
43
Henrik Kretzschmar65396412006-09-12 23:49:33 +020044static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020046 struct Scsi_Host *instance = cmd->device->host;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020047 struct a3000_hostdata *hdata = shost_priv(instance);
48 struct WD33C93_hostdata *wh = &hdata->wh;
49 struct a3000_scsiregs *regs = hdata->regs;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020050 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
51 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Geert Uytterhoeven21351012010-04-04 11:00:35 +020053 /*
54 * if the physical address has the wrong alignment, or if
55 * physical address is bad, or if it is a write and at the
56 * end of a physical memory chunk, then allocate a bounce
57 * buffer
58 */
59 if (addr & A3000_XFER_MASK) {
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020060 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
61 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
62 GFP_KERNEL);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020063
64 /* can't allocate memory; use PIO */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020065 if (!wh->dma_bounce_buffer) {
66 wh->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020067 return 1;
68 }
69
70 if (!dir_in) {
71 /* copy to bounce buffer for a write */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020072 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020073 cmd->SCp.this_residual);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020074 }
75
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020076 addr = virt_to_bus(wh->dma_bounce_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
Geert Uytterhoeven21351012010-04-04 11:00:35 +020079 /* setup dma direction */
80 if (!dir_in)
81 cntr |= CNTR_DDIR;
82
83 /* remember direction */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020084 wh->dma_dir = dir_in;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020085
Geert Uytterhoevend7537222009-05-17 12:17:23 +020086 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020087
88 /* setup DMA *physical* address */
Geert Uytterhoevend7537222009-05-17 12:17:23 +020089 regs->ACR = addr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020090
91 if (dir_in) {
92 /* invalidate any cache */
93 cache_clear(addr, cmd->SCp.this_residual);
94 } else {
95 /* push any dirty cache */
96 cache_push(addr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
Geert Uytterhoeven21351012010-04-04 11:00:35 +020099 /* start DMA */
100 mb(); /* make sure setup is completed */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200101 regs->ST_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200102 mb(); /* make sure DMA has started before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200104 /* return success */
105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200108static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
109 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200111 struct a3000_hostdata *hdata = shost_priv(instance);
112 struct WD33C93_hostdata *wh = &hdata->wh;
113 struct a3000_scsiregs *regs = hdata->regs;
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 Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200118 if (!wh->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 Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200125 if (wh->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 Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200148 if (status && wh->dma_bounce_buffer) {
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200149 if (SCpnt) {
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200150 if (wh->dma_dir && SCpnt)
151 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200152 SCpnt->SCp.this_residual);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200153 kfree(wh->dma_bounce_buffer);
154 wh->dma_bounce_buffer = NULL;
155 wh->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200156 } else {
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200157 kfree(wh->dma_bounce_buffer);
158 wh->dma_bounce_buffer = NULL;
159 wh->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200160 }
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200164static int a3000_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200166 struct Scsi_Host *instance = cmd->device->host;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* FIXME perform bus-specific reset */
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200169
Jeff Garzik df0ae242005-05-28 07:57:14 -0400170 /* FIXME 2: kill this entire function, which should
171 cause mid-layer to call wd33c93_host_reset anyway? */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400172
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200173 spin_lock_irq(instance->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 wd33c93_host_reset(cmd);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200175 spin_unlock_irq(instance->host_lock);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return SUCCESS;
178}
179
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200180static struct scsi_host_template amiga_a3000_scsi_template = {
181 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .name = "Amiga 3000 built-in SCSI",
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200183 .proc_info = wd33c93_proc_info,
184 .proc_name = "A3000",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .queuecommand = wd33c93_queuecommand,
186 .eh_abort_handler = wd33c93_abort,
187 .eh_bus_reset_handler = a3000_bus_reset,
188 .eh_host_reset_handler = wd33c93_host_reset,
189 .can_queue = CAN_QUEUE,
190 .this_id = 7,
191 .sg_tablesize = SG_ALL,
192 .cmd_per_lun = CMD_PER_LUN,
193 .use_clustering = ENABLE_CLUSTERING
194};
195
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200196static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200198 struct resource *res;
199 struct Scsi_Host *instance;
200 int error;
201 struct a3000_scsiregs *regs;
202 wd33c93_regs wdregs;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200203 struct a3000_hostdata *hdata;
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200204
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200205 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
206 if (!res)
207 return -ENODEV;
208
209 if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
210 return -EBUSY;
211
212 instance = scsi_host_alloc(&amiga_a3000_scsi_template,
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200213 sizeof(struct a3000_hostdata));
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200214 if (!instance) {
215 error = -ENOMEM;
216 goto fail_alloc;
217 }
218
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200219 instance->irq = IRQ_AMIGA_PORTS;
220
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200221 regs = (struct a3000_scsiregs *)ZTWO_VADDR(res->start);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200222 regs->DAWR = DAWR_A3000;
223
224 wdregs.SASR = &regs->SASR;
225 wdregs.SCMD = &regs->SCMD;
226
227 hdata = shost_priv(instance);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200228 hdata->wh.no_sync = 0xff;
229 hdata->wh.fast = 0;
230 hdata->wh.dma_mode = CTRL_DMA;
231 hdata->regs = regs;
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200232
233 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
234 error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
235 "A3000 SCSI", instance);
236 if (error)
237 goto fail_irq;
238
239 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
240
241 error = scsi_add_host(instance, NULL);
242 if (error)
243 goto fail_host;
244
245 platform_set_drvdata(pdev, instance);
246
247 scsi_scan_host(instance);
248 return 0;
249
250fail_host:
251 free_irq(IRQ_AMIGA_PORTS, instance);
252fail_irq:
253 scsi_host_put(instance);
254fail_alloc:
255 release_mem_region(res->start, resource_size(res));
256 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200259static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
260{
261 struct Scsi_Host *instance = platform_get_drvdata(pdev);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200262 struct a3000_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200263 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
264
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200265 hdata->regs->CNTR = 0;
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200266 scsi_remove_host(instance);
267 free_irq(IRQ_AMIGA_PORTS, instance);
268 scsi_host_put(instance);
269 release_mem_region(res->start, resource_size(res));
270 return 0;
271}
272
273static struct platform_driver amiga_a3000_scsi_driver = {
274 .remove = __exit_p(amiga_a3000_scsi_remove),
275 .driver = {
276 .name = "amiga-a3000-scsi",
277 .owner = THIS_MODULE,
278 },
279};
280
281static int __init amiga_a3000_scsi_init(void)
282{
283 return platform_driver_probe(&amiga_a3000_scsi_driver,
284 amiga_a3000_scsi_probe);
285}
286module_init(amiga_a3000_scsi_init);
287
288static void __exit amiga_a3000_scsi_exit(void)
289{
290 platform_driver_unregister(&amiga_a3000_scsi_driver);
291}
292module_exit(amiga_a3000_scsi_exit);
293
294MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295MODULE_LICENSE("GPL");
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200296MODULE_ALIAS("platform:amiga-a3000-scsi");