1 #include <linux/types.h>
2 #include <linux/mm.h>
3 #include <linux/ioport.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 #include <linux/spinlock.h>
7 #include <linux/interrupt.h>
8 #include <linux/platform_device.h>
9 #include <linux/module.h>
10 
11 #include <asm/page.h>
12 #include <asm/pgtable.h>
13 #include <asm/amigaints.h>
14 #include <asm/amigahw.h>
15 
16 #include "scsi.h"
17 #include "wd33c93.h"
18 #include "a3000.h"
19 
20 
21 struct a3000_hostdata {
22 	struct WD33C93_hostdata wh;
23 	struct a3000_scsiregs *regs;
24 };
25 
a3000_intr(int irq,void * data)26 static irqreturn_t a3000_intr(int irq, void *data)
27 {
28 	struct Scsi_Host *instance = data;
29 	struct a3000_hostdata *hdata = shost_priv(instance);
30 	unsigned int status = hdata->regs->ISTR;
31 	unsigned long flags;
32 
33 	if (!(status & ISTR_INT_P))
34 		return IRQ_NONE;
35 	if (status & ISTR_INTS) {
36 		spin_lock_irqsave(instance->host_lock, flags);
37 		wd33c93_intr(instance);
38 		spin_unlock_irqrestore(instance->host_lock, flags);
39 		return IRQ_HANDLED;
40 	}
41 	pr_warning("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
42 	return IRQ_NONE;
43 }
44 
dma_setup(struct scsi_cmnd * cmd,int dir_in)45 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
46 {
47 	struct Scsi_Host *instance = cmd->device->host;
48 	struct a3000_hostdata *hdata = shost_priv(instance);
49 	struct WD33C93_hostdata *wh = &hdata->wh;
50 	struct a3000_scsiregs *regs = hdata->regs;
51 	unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
52 	unsigned long addr = virt_to_bus(cmd->SCp.ptr);
53 
54 	/*
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) {
61 		wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
62 		wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
63 						GFP_KERNEL);
64 
65 		/* can't allocate memory; use PIO */
66 		if (!wh->dma_bounce_buffer) {
67 			wh->dma_bounce_len = 0;
68 			return 1;
69 		}
70 
71 		if (!dir_in) {
72 			/* copy to bounce buffer for a write */
73 			memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
74 			       cmd->SCp.this_residual);
75 		}
76 
77 		addr = virt_to_bus(wh->dma_bounce_buffer);
78 	}
79 
80 	/* setup dma direction */
81 	if (!dir_in)
82 		cntr |= CNTR_DDIR;
83 
84 	/* remember direction */
85 	wh->dma_dir = dir_in;
86 
87 	regs->CNTR = cntr;
88 
89 	/* setup DMA *physical* address */
90 	regs->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);
98 	}
99 
100 	/* start DMA */
101 	mb();			/* make sure setup is completed */
102 	regs->ST_DMA = 1;
103 	mb();			/* make sure DMA has started before next IO */
104 
105 	/* return success */
106 	return 0;
107 }
108 
dma_stop(struct Scsi_Host * instance,struct scsi_cmnd * SCpnt,int status)109 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
110 		     int status)
111 {
112 	struct a3000_hostdata *hdata = shost_priv(instance);
113 	struct WD33C93_hostdata *wh = &hdata->wh;
114 	struct a3000_scsiregs *regs = hdata->regs;
115 
116 	/* disable SCSI interrupts */
117 	unsigned short cntr = CNTR_PDMD;
118 
119 	if (!wh->dma_dir)
120 		cntr |= CNTR_DDIR;
121 
122 	regs->CNTR = cntr;
123 	mb();			/* make sure CNTR is updated before next IO */
124 
125 	/* flush if we were reading */
126 	if (wh->dma_dir) {
127 		regs->FLUSH = 1;
128 		mb();		/* don't allow prefetch */
129 		while (!(regs->ISTR & ISTR_FE_FLG))
130 			barrier();
131 		mb();		/* no IO until FLUSH is done */
132 	}
133 
134 	/* clear a possible interrupt */
135 	/* I think that this CINT is only necessary if you are
136 	 * using the terminal count features.   HM 7 Mar 1994
137 	 */
138 	regs->CINT = 1;
139 
140 	/* stop DMA */
141 	regs->SP_DMA = 1;
142 	mb();			/* make sure DMA is stopped before next IO */
143 
144 	/* restore the CONTROL bits (minus the direction flag) */
145 	regs->CNTR = CNTR_PDMD | CNTR_INTEN;
146 	mb();			/* make sure CNTR is updated before next IO */
147 
148 	/* copy from a bounce buffer, if necessary */
149 	if (status && wh->dma_bounce_buffer) {
150 		if (SCpnt) {
151 			if (wh->dma_dir && SCpnt)
152 				memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
153 				       SCpnt->SCp.this_residual);
154 			kfree(wh->dma_bounce_buffer);
155 			wh->dma_bounce_buffer = NULL;
156 			wh->dma_bounce_len = 0;
157 		} else {
158 			kfree(wh->dma_bounce_buffer);
159 			wh->dma_bounce_buffer = NULL;
160 			wh->dma_bounce_len = 0;
161 		}
162 	}
163 }
164 
165 static struct scsi_host_template amiga_a3000_scsi_template = {
166 	.module			= THIS_MODULE,
167 	.name			= "Amiga 3000 built-in SCSI",
168 	.show_info		= wd33c93_show_info,
169 	.write_info		= wd33c93_write_info,
170 	.proc_name		= "A3000",
171 	.queuecommand		= wd33c93_queuecommand,
172 	.eh_abort_handler	= wd33c93_abort,
173 	.eh_host_reset_handler	= wd33c93_host_reset,
174 	.can_queue		= CAN_QUEUE,
175 	.this_id		= 7,
176 	.sg_tablesize		= SG_ALL,
177 	.cmd_per_lun		= CMD_PER_LUN,
178 	.use_clustering		= ENABLE_CLUSTERING
179 };
180 
amiga_a3000_scsi_probe(struct platform_device * pdev)181 static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
182 {
183 	struct resource *res;
184 	struct Scsi_Host *instance;
185 	int error;
186 	struct a3000_scsiregs *regs;
187 	wd33c93_regs wdregs;
188 	struct a3000_hostdata *hdata;
189 
190 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191 	if (!res)
192 		return -ENODEV;
193 
194 	if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
195 		return -EBUSY;
196 
197 	instance = scsi_host_alloc(&amiga_a3000_scsi_template,
198 				   sizeof(struct a3000_hostdata));
199 	if (!instance) {
200 		error = -ENOMEM;
201 		goto fail_alloc;
202 	}
203 
204 	instance->irq = IRQ_AMIGA_PORTS;
205 
206 	regs = ZTWO_VADDR(res->start);
207 	regs->DAWR = DAWR_A3000;
208 
209 	wdregs.SASR = &regs->SASR;
210 	wdregs.SCMD = &regs->SCMD;
211 
212 	hdata = shost_priv(instance);
213 	hdata->wh.no_sync = 0xff;
214 	hdata->wh.fast = 0;
215 	hdata->wh.dma_mode = CTRL_DMA;
216 	hdata->regs = regs;
217 
218 	wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
219 	error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
220 			    "A3000 SCSI", instance);
221 	if (error)
222 		goto fail_irq;
223 
224 	regs->CNTR = CNTR_PDMD | CNTR_INTEN;
225 
226 	error = scsi_add_host(instance, NULL);
227 	if (error)
228 		goto fail_host;
229 
230 	platform_set_drvdata(pdev, instance);
231 
232 	scsi_scan_host(instance);
233 	return 0;
234 
235 fail_host:
236 	free_irq(IRQ_AMIGA_PORTS, instance);
237 fail_irq:
238 	scsi_host_put(instance);
239 fail_alloc:
240 	release_mem_region(res->start, resource_size(res));
241 	return error;
242 }
243 
amiga_a3000_scsi_remove(struct platform_device * pdev)244 static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
245 {
246 	struct Scsi_Host *instance = platform_get_drvdata(pdev);
247 	struct a3000_hostdata *hdata = shost_priv(instance);
248 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249 
250 	hdata->regs->CNTR = 0;
251 	scsi_remove_host(instance);
252 	free_irq(IRQ_AMIGA_PORTS, instance);
253 	scsi_host_put(instance);
254 	release_mem_region(res->start, resource_size(res));
255 	return 0;
256 }
257 
258 static struct platform_driver amiga_a3000_scsi_driver = {
259 	.remove = __exit_p(amiga_a3000_scsi_remove),
260 	.driver   = {
261 		.name	= "amiga-a3000-scsi",
262 	},
263 };
264 
265 module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe);
266 
267 MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
268 MODULE_LICENSE("GPL");
269 MODULE_ALIAS("platform:amiga-a3000-scsi");
270