xref: /openbmc/linux/drivers/scsi/a3000.c (revision c2a24a4ca1137473971842461612e56a654e7edb)
11da177e4SLinus Torvalds #include <linux/types.h>
21da177e4SLinus Torvalds #include <linux/mm.h>
31da177e4SLinus Torvalds #include <linux/ioport.h>
41da177e4SLinus Torvalds #include <linux/init.h>
5*c2a24a4cSGeert Uytterhoeven #include <linux/slab.h>
61da177e4SLinus Torvalds #include <linux/spinlock.h>
71da177e4SLinus Torvalds #include <linux/interrupt.h>
8*c2a24a4cSGeert Uytterhoeven #include <linux/platform_device.h>
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <asm/page.h>
111da177e4SLinus Torvalds #include <asm/pgtable.h>
121da177e4SLinus Torvalds #include <asm/amigaints.h>
131da177e4SLinus Torvalds #include <asm/amigahw.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include "scsi.h"
161da177e4SLinus Torvalds #include "wd33c93.h"
171da177e4SLinus Torvalds #include "a3000.h"
181da177e4SLinus Torvalds 
199387edbeSAdrian Bunk 
20a8169e60SGeert Uytterhoeven static irqreturn_t a3000_intr(int irq, void *data)
211da177e4SLinus Torvalds {
22a8169e60SGeert Uytterhoeven 	struct Scsi_Host *instance = data;
23c57c1cabSGeert Uytterhoeven 	struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
24d753722eSGeert Uytterhoeven 	unsigned int status = regs->ISTR;
251da177e4SLinus Torvalds 	unsigned long flags;
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds 	if (!(status & ISTR_INT_P))
281da177e4SLinus Torvalds 		return IRQ_NONE;
2921351013SGeert Uytterhoeven 	if (status & ISTR_INTS) {
30a8169e60SGeert Uytterhoeven 		spin_lock_irqsave(instance->host_lock, flags);
31a8169e60SGeert Uytterhoeven 		wd33c93_intr(instance);
32a8169e60SGeert Uytterhoeven 		spin_unlock_irqrestore(instance->host_lock, flags);
331da177e4SLinus Torvalds 		return IRQ_HANDLED;
341da177e4SLinus Torvalds 	}
35*c2a24a4cSGeert Uytterhoeven 	pr_warning("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
361da177e4SLinus Torvalds 	return IRQ_NONE;
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds 
3965396410SHenrik Kretzschmar static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
401da177e4SLinus Torvalds {
41a8169e60SGeert Uytterhoeven 	struct Scsi_Host *instance = cmd->device->host;
42a8169e60SGeert Uytterhoeven 	struct WD33C93_hostdata *hdata = shost_priv(instance);
43c57c1cabSGeert Uytterhoeven 	struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
441da177e4SLinus Torvalds 	unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
451da177e4SLinus Torvalds 	unsigned long addr = virt_to_bus(cmd->SCp.ptr);
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds 	/*
481da177e4SLinus Torvalds 	 * if the physical address has the wrong alignment, or if
491da177e4SLinus Torvalds 	 * physical address is bad, or if it is a write and at the
501da177e4SLinus Torvalds 	 * end of a physical memory chunk, then allocate a bounce
511da177e4SLinus Torvalds 	 * buffer
521da177e4SLinus Torvalds 	 */
5321351013SGeert Uytterhoeven 	if (addr & A3000_XFER_MASK) {
54afdbbc16SGeert Uytterhoeven 		hdata->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
55afdbbc16SGeert Uytterhoeven 		hdata->dma_bounce_buffer = kmalloc(hdata->dma_bounce_len,
56afdbbc16SGeert Uytterhoeven 						   GFP_KERNEL);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 		/* can't allocate memory; use PIO */
59afdbbc16SGeert Uytterhoeven 		if (!hdata->dma_bounce_buffer) {
60afdbbc16SGeert Uytterhoeven 			hdata->dma_bounce_len = 0;
611da177e4SLinus Torvalds 			return 1;
621da177e4SLinus Torvalds 		}
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 		if (!dir_in) {
651da177e4SLinus Torvalds 			/* copy to bounce buffer for a write */
66afdbbc16SGeert Uytterhoeven 			memcpy(hdata->dma_bounce_buffer, cmd->SCp.ptr,
67afdbbc16SGeert Uytterhoeven 			       cmd->SCp.this_residual);
681da177e4SLinus Torvalds 		}
691da177e4SLinus Torvalds 
70afdbbc16SGeert Uytterhoeven 		addr = virt_to_bus(hdata->dma_bounce_buffer);
711da177e4SLinus Torvalds 	}
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	/* setup dma direction */
741da177e4SLinus Torvalds 	if (!dir_in)
751da177e4SLinus Torvalds 		cntr |= CNTR_DDIR;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	/* remember direction */
78afdbbc16SGeert Uytterhoeven 	hdata->dma_dir = dir_in;
791da177e4SLinus Torvalds 
80d753722eSGeert Uytterhoeven 	regs->CNTR = cntr;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	/* setup DMA *physical* address */
83d753722eSGeert Uytterhoeven 	regs->ACR = addr;
841da177e4SLinus Torvalds 
8521351013SGeert Uytterhoeven 	if (dir_in) {
861da177e4SLinus Torvalds 		/* invalidate any cache */
871da177e4SLinus Torvalds 		cache_clear(addr, cmd->SCp.this_residual);
8821351013SGeert Uytterhoeven 	} else {
891da177e4SLinus Torvalds 		/* push any dirty cache */
901da177e4SLinus Torvalds 		cache_push(addr, cmd->SCp.this_residual);
9121351013SGeert Uytterhoeven 	}
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	/* start DMA */
941da177e4SLinus Torvalds 	mb();			/* make sure setup is completed */
95d753722eSGeert Uytterhoeven 	regs->ST_DMA = 1;
961da177e4SLinus Torvalds 	mb();			/* make sure DMA has started before next IO */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	/* return success */
991da177e4SLinus Torvalds 	return 0;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
10265396410SHenrik Kretzschmar static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
1031da177e4SLinus Torvalds 		     int status)
1041da177e4SLinus Torvalds {
105afdbbc16SGeert Uytterhoeven 	struct WD33C93_hostdata *hdata = shost_priv(instance);
106c57c1cabSGeert Uytterhoeven 	struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
107afdbbc16SGeert Uytterhoeven 
1081da177e4SLinus Torvalds 	/* disable SCSI interrupts */
1091da177e4SLinus Torvalds 	unsigned short cntr = CNTR_PDMD;
1101da177e4SLinus Torvalds 
111afdbbc16SGeert Uytterhoeven 	if (!hdata->dma_dir)
1121da177e4SLinus Torvalds 		cntr |= CNTR_DDIR;
1131da177e4SLinus Torvalds 
114d753722eSGeert Uytterhoeven 	regs->CNTR = cntr;
1151da177e4SLinus Torvalds 	mb();			/* make sure CNTR is updated before next IO */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	/* flush if we were reading */
118afdbbc16SGeert Uytterhoeven 	if (hdata->dma_dir) {
119d753722eSGeert Uytterhoeven 		regs->FLUSH = 1;
1201da177e4SLinus Torvalds 		mb();		/* don't allow prefetch */
121d753722eSGeert Uytterhoeven 		while (!(regs->ISTR & ISTR_FE_FLG))
1221da177e4SLinus Torvalds 			barrier();
1231da177e4SLinus Torvalds 		mb();		/* no IO until FLUSH is done */
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	/* clear a possible interrupt */
1271da177e4SLinus Torvalds 	/* I think that this CINT is only necessary if you are
1281da177e4SLinus Torvalds 	 * using the terminal count features.   HM 7 Mar 1994
1291da177e4SLinus Torvalds 	 */
130d753722eSGeert Uytterhoeven 	regs->CINT = 1;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	/* stop DMA */
133d753722eSGeert Uytterhoeven 	regs->SP_DMA = 1;
1341da177e4SLinus Torvalds 	mb();			/* make sure DMA is stopped before next IO */
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/* restore the CONTROL bits (minus the direction flag) */
137d753722eSGeert Uytterhoeven 	regs->CNTR = CNTR_PDMD | CNTR_INTEN;
1381da177e4SLinus Torvalds 	mb();			/* make sure CNTR is updated before next IO */
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	/* copy from a bounce buffer, if necessary */
141afdbbc16SGeert Uytterhoeven 	if (status && hdata->dma_bounce_buffer) {
142cc0455faSBoaz Harrosh 		if (SCpnt) {
143afdbbc16SGeert Uytterhoeven 			if (hdata->dma_dir && SCpnt)
1441da177e4SLinus Torvalds 				memcpy(SCpnt->SCp.ptr,
145afdbbc16SGeert Uytterhoeven 				       hdata->dma_bounce_buffer,
1461da177e4SLinus Torvalds 				       SCpnt->SCp.this_residual);
147afdbbc16SGeert Uytterhoeven 			kfree(hdata->dma_bounce_buffer);
148afdbbc16SGeert Uytterhoeven 			hdata->dma_bounce_buffer = NULL;
149afdbbc16SGeert Uytterhoeven 			hdata->dma_bounce_len = 0;
1501da177e4SLinus Torvalds 		} else {
151afdbbc16SGeert Uytterhoeven 			kfree(hdata->dma_bounce_buffer);
152afdbbc16SGeert Uytterhoeven 			hdata->dma_bounce_buffer = NULL;
153afdbbc16SGeert Uytterhoeven 			hdata->dma_bounce_len = 0;
1541da177e4SLinus Torvalds 		}
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
15865396410SHenrik Kretzschmar static int a3000_bus_reset(struct scsi_cmnd *cmd)
1591da177e4SLinus Torvalds {
160*c2a24a4cSGeert Uytterhoeven 	struct Scsi_Host *instance = cmd->device->host;
161*c2a24a4cSGeert Uytterhoeven 
1621da177e4SLinus Torvalds 	/* FIXME perform bus-specific reset */
163df0ae249SJeff Garzik  
164df0ae249SJeff Garzik  	/* FIXME 2: kill this entire function, which should
165df0ae249SJeff Garzik  	   cause mid-layer to call wd33c93_host_reset anyway? */
16668b3aa7cSJeff Garzik  
167*c2a24a4cSGeert Uytterhoeven 	spin_lock_irq(instance->host_lock);
1681da177e4SLinus Torvalds 	wd33c93_host_reset(cmd);
169*c2a24a4cSGeert Uytterhoeven 	spin_unlock_irq(instance->host_lock);
17068b3aa7cSJeff Garzik  
1711da177e4SLinus Torvalds 	return SUCCESS;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
174*c2a24a4cSGeert Uytterhoeven static struct scsi_host_template amiga_a3000_scsi_template = {
175*c2a24a4cSGeert Uytterhoeven 	.module			= THIS_MODULE,
1761da177e4SLinus Torvalds 	.name			= "Amiga 3000 built-in SCSI",
177*c2a24a4cSGeert Uytterhoeven 	.proc_info		= wd33c93_proc_info,
178*c2a24a4cSGeert Uytterhoeven 	.proc_name		= "A3000",
1791da177e4SLinus Torvalds 	.queuecommand		= wd33c93_queuecommand,
1801da177e4SLinus Torvalds 	.eh_abort_handler	= wd33c93_abort,
1811da177e4SLinus Torvalds 	.eh_bus_reset_handler	= a3000_bus_reset,
1821da177e4SLinus Torvalds 	.eh_host_reset_handler	= wd33c93_host_reset,
1831da177e4SLinus Torvalds 	.can_queue		= CAN_QUEUE,
1841da177e4SLinus Torvalds 	.this_id		= 7,
1851da177e4SLinus Torvalds 	.sg_tablesize		= SG_ALL,
1861da177e4SLinus Torvalds 	.cmd_per_lun		= CMD_PER_LUN,
1871da177e4SLinus Torvalds 	.use_clustering		= ENABLE_CLUSTERING
1881da177e4SLinus Torvalds };
1891da177e4SLinus Torvalds 
190*c2a24a4cSGeert Uytterhoeven static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
1911da177e4SLinus Torvalds {
192*c2a24a4cSGeert Uytterhoeven 	struct resource *res;
193*c2a24a4cSGeert Uytterhoeven 	struct Scsi_Host *instance;
194*c2a24a4cSGeert Uytterhoeven 	int error;
195*c2a24a4cSGeert Uytterhoeven 	struct a3000_scsiregs *regs;
196*c2a24a4cSGeert Uytterhoeven 	wd33c93_regs wdregs;
197*c2a24a4cSGeert Uytterhoeven 	struct WD33C93_hostdata *hdata;
198d753722eSGeert Uytterhoeven 
199*c2a24a4cSGeert Uytterhoeven 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200*c2a24a4cSGeert Uytterhoeven 	if (!res)
201*c2a24a4cSGeert Uytterhoeven 		return -ENODEV;
202*c2a24a4cSGeert Uytterhoeven 
203*c2a24a4cSGeert Uytterhoeven 	if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
204*c2a24a4cSGeert Uytterhoeven 		return -EBUSY;
205*c2a24a4cSGeert Uytterhoeven 
206*c2a24a4cSGeert Uytterhoeven 	instance = scsi_host_alloc(&amiga_a3000_scsi_template,
207*c2a24a4cSGeert Uytterhoeven 				   sizeof(struct WD33C93_hostdata));
208*c2a24a4cSGeert Uytterhoeven 	if (!instance) {
209*c2a24a4cSGeert Uytterhoeven 		error = -ENOMEM;
210*c2a24a4cSGeert Uytterhoeven 		goto fail_alloc;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
213*c2a24a4cSGeert Uytterhoeven 	instance->base = ZTWO_VADDR(res->start);
214*c2a24a4cSGeert Uytterhoeven 	instance->irq = IRQ_AMIGA_PORTS;
215*c2a24a4cSGeert Uytterhoeven 
216*c2a24a4cSGeert Uytterhoeven 	regs = (struct a3000_scsiregs *)(instance->base);
217*c2a24a4cSGeert Uytterhoeven 	regs->DAWR = DAWR_A3000;
218*c2a24a4cSGeert Uytterhoeven 
219*c2a24a4cSGeert Uytterhoeven 	wdregs.SASR = &regs->SASR;
220*c2a24a4cSGeert Uytterhoeven 	wdregs.SCMD = &regs->SCMD;
221*c2a24a4cSGeert Uytterhoeven 
222*c2a24a4cSGeert Uytterhoeven 	hdata = shost_priv(instance);
223*c2a24a4cSGeert Uytterhoeven 	hdata->no_sync = 0xff;
224*c2a24a4cSGeert Uytterhoeven 	hdata->fast = 0;
225*c2a24a4cSGeert Uytterhoeven 	hdata->dma_mode = CTRL_DMA;
226*c2a24a4cSGeert Uytterhoeven 
227*c2a24a4cSGeert Uytterhoeven 	wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
228*c2a24a4cSGeert Uytterhoeven 	error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
229*c2a24a4cSGeert Uytterhoeven 			    "A3000 SCSI", instance);
230*c2a24a4cSGeert Uytterhoeven 	if (error)
231*c2a24a4cSGeert Uytterhoeven 		goto fail_irq;
232*c2a24a4cSGeert Uytterhoeven 
233*c2a24a4cSGeert Uytterhoeven 	regs->CNTR = CNTR_PDMD | CNTR_INTEN;
234*c2a24a4cSGeert Uytterhoeven 
235*c2a24a4cSGeert Uytterhoeven 	error = scsi_add_host(instance, NULL);
236*c2a24a4cSGeert Uytterhoeven 	if (error)
237*c2a24a4cSGeert Uytterhoeven 		goto fail_host;
238*c2a24a4cSGeert Uytterhoeven 
239*c2a24a4cSGeert Uytterhoeven 	platform_set_drvdata(pdev, instance);
240*c2a24a4cSGeert Uytterhoeven 
241*c2a24a4cSGeert Uytterhoeven 	scsi_scan_host(instance);
242*c2a24a4cSGeert Uytterhoeven 	return 0;
243*c2a24a4cSGeert Uytterhoeven 
244*c2a24a4cSGeert Uytterhoeven fail_host:
245*c2a24a4cSGeert Uytterhoeven 	free_irq(IRQ_AMIGA_PORTS, instance);
246*c2a24a4cSGeert Uytterhoeven fail_irq:
247*c2a24a4cSGeert Uytterhoeven 	scsi_host_put(instance);
248*c2a24a4cSGeert Uytterhoeven fail_alloc:
249*c2a24a4cSGeert Uytterhoeven 	release_mem_region(res->start, resource_size(res));
250*c2a24a4cSGeert Uytterhoeven 	return error;
251*c2a24a4cSGeert Uytterhoeven }
252*c2a24a4cSGeert Uytterhoeven 
253*c2a24a4cSGeert Uytterhoeven static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
254*c2a24a4cSGeert Uytterhoeven {
255*c2a24a4cSGeert Uytterhoeven 	struct Scsi_Host *instance = platform_get_drvdata(pdev);
256*c2a24a4cSGeert Uytterhoeven 	struct a3000_scsiregs *regs = (struct a3000_scsiregs *)(instance->base);
257*c2a24a4cSGeert Uytterhoeven 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258*c2a24a4cSGeert Uytterhoeven 
259*c2a24a4cSGeert Uytterhoeven 	regs->CNTR = 0;
260*c2a24a4cSGeert Uytterhoeven 	scsi_remove_host(instance);
261*c2a24a4cSGeert Uytterhoeven 	free_irq(IRQ_AMIGA_PORTS, instance);
262*c2a24a4cSGeert Uytterhoeven 	scsi_host_put(instance);
263*c2a24a4cSGeert Uytterhoeven 	release_mem_region(res->start, resource_size(res));
264*c2a24a4cSGeert Uytterhoeven 	return 0;
265*c2a24a4cSGeert Uytterhoeven }
266*c2a24a4cSGeert Uytterhoeven 
267*c2a24a4cSGeert Uytterhoeven static struct platform_driver amiga_a3000_scsi_driver = {
268*c2a24a4cSGeert Uytterhoeven 	.remove = __exit_p(amiga_a3000_scsi_remove),
269*c2a24a4cSGeert Uytterhoeven 	.driver   = {
270*c2a24a4cSGeert Uytterhoeven 		.name	= "amiga-a3000-scsi",
271*c2a24a4cSGeert Uytterhoeven 		.owner	= THIS_MODULE,
272*c2a24a4cSGeert Uytterhoeven 	},
273*c2a24a4cSGeert Uytterhoeven };
274*c2a24a4cSGeert Uytterhoeven 
275*c2a24a4cSGeert Uytterhoeven static int __init amiga_a3000_scsi_init(void)
276*c2a24a4cSGeert Uytterhoeven {
277*c2a24a4cSGeert Uytterhoeven 	return platform_driver_probe(&amiga_a3000_scsi_driver,
278*c2a24a4cSGeert Uytterhoeven 				     amiga_a3000_scsi_probe);
279*c2a24a4cSGeert Uytterhoeven }
280*c2a24a4cSGeert Uytterhoeven module_init(amiga_a3000_scsi_init);
281*c2a24a4cSGeert Uytterhoeven 
282*c2a24a4cSGeert Uytterhoeven static void __exit amiga_a3000_scsi_exit(void)
283*c2a24a4cSGeert Uytterhoeven {
284*c2a24a4cSGeert Uytterhoeven 	platform_driver_unregister(&amiga_a3000_scsi_driver);
285*c2a24a4cSGeert Uytterhoeven }
286*c2a24a4cSGeert Uytterhoeven module_exit(amiga_a3000_scsi_exit);
287*c2a24a4cSGeert Uytterhoeven 
288*c2a24a4cSGeert Uytterhoeven MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
2891da177e4SLinus Torvalds MODULE_LICENSE("GPL");
290*c2a24a4cSGeert Uytterhoeven MODULE_ALIAS("platform:amiga-a3000-scsi");
291