xref: /openbmc/linux/drivers/scsi/a2091.c (revision 53555fb7bceb14f5fdb9358290daf64d0ea0f56a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/init.h>
4 #include <linux/interrupt.h>
5 #include <linux/mm.h>
6 #include <linux/slab.h>
7 #include <linux/spinlock.h>
8 #include <linux/zorro.h>
9 #include <linux/module.h>
10 
11 #include <asm/page.h>
12 #include <asm/amigaints.h>
13 #include <asm/amigahw.h>
14 
15 #include <scsi/scsi.h>
16 #include <scsi/scsi_cmnd.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_eh.h>
19 #include <scsi/scsi_tcq.h>
20 #include "wd33c93.h"
21 #include "a2091.h"
22 
23 
24 struct a2091_hostdata {
25 	struct WD33C93_hostdata wh;
26 	struct a2091_scsiregs *regs;
27 };
28 
29 static irqreturn_t a2091_intr(int irq, void *data)
30 {
31 	struct Scsi_Host *instance = data;
32 	struct a2091_hostdata *hdata = shost_priv(instance);
33 	unsigned int status = hdata->regs->ISTR;
34 	unsigned long flags;
35 
36 	if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
37 		return IRQ_NONE;
38 
39 	spin_lock_irqsave(instance->host_lock, flags);
40 	wd33c93_intr(instance);
41 	spin_unlock_irqrestore(instance->host_lock, flags);
42 	return IRQ_HANDLED;
43 }
44 
45 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
46 {
47 	struct Scsi_Host *instance = cmd->device->host;
48 	struct a2091_hostdata *hdata = shost_priv(instance);
49 	struct WD33C93_hostdata *wh = &hdata->wh;
50 	struct a2091_scsiregs *regs = hdata->regs;
51 	unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
52 	unsigned long addr = virt_to_bus(cmd->SCp.ptr);
53 
54 	/* don't allow DMA if the physical address is bad */
55 	if (addr & A2091_XFER_MASK) {
56 		wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
57 		wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
58 						GFP_KERNEL);
59 
60 		/* can't allocate memory; use PIO */
61 		if (!wh->dma_bounce_buffer) {
62 			wh->dma_bounce_len = 0;
63 			return 1;
64 		}
65 
66 		/* get the physical address of the bounce buffer */
67 		addr = virt_to_bus(wh->dma_bounce_buffer);
68 
69 		/* the bounce buffer may not be in the first 16M of physmem */
70 		if (addr & A2091_XFER_MASK) {
71 			/* we could use chipmem... maybe later */
72 			kfree(wh->dma_bounce_buffer);
73 			wh->dma_bounce_buffer = NULL;
74 			wh->dma_bounce_len = 0;
75 			return 1;
76 		}
77 
78 		if (!dir_in) {
79 			/* copy to bounce buffer for a write */
80 			memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
81 			       cmd->SCp.this_residual);
82 		}
83 	}
84 
85 	/* setup dma direction */
86 	if (!dir_in)
87 		cntr |= CNTR_DDIR;
88 
89 	/* remember direction */
90 	wh->dma_dir = dir_in;
91 
92 	regs->CNTR = cntr;
93 
94 	/* setup DMA *physical* address */
95 	regs->ACR = addr;
96 
97 	if (dir_in) {
98 		/* invalidate any cache */
99 		cache_clear(addr, cmd->SCp.this_residual);
100 	} else {
101 		/* push any dirty cache */
102 		cache_push(addr, cmd->SCp.this_residual);
103 	}
104 	/* start DMA */
105 	regs->ST_DMA = 1;
106 
107 	/* return success */
108 	return 0;
109 }
110 
111 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
112 		     int status)
113 {
114 	struct a2091_hostdata *hdata = shost_priv(instance);
115 	struct WD33C93_hostdata *wh = &hdata->wh;
116 	struct a2091_scsiregs *regs = hdata->regs;
117 
118 	/* disable SCSI interrupts */
119 	unsigned short cntr = CNTR_PDMD;
120 
121 	if (!wh->dma_dir)
122 		cntr |= CNTR_DDIR;
123 
124 	/* disable SCSI interrupts */
125 	regs->CNTR = cntr;
126 
127 	/* flush if we were reading */
128 	if (wh->dma_dir) {
129 		regs->FLUSH = 1;
130 		while (!(regs->ISTR & ISTR_FE_FLG))
131 			;
132 	}
133 
134 	/* clear a possible interrupt */
135 	regs->CINT = 1;
136 
137 	/* stop DMA */
138 	regs->SP_DMA = 1;
139 
140 	/* restore the CONTROL bits (minus the direction flag) */
141 	regs->CNTR = CNTR_PDMD | CNTR_INTEN;
142 
143 	/* copy from a bounce buffer, if necessary */
144 	if (status && wh->dma_bounce_buffer) {
145 		if (wh->dma_dir)
146 			memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
147 			       SCpnt->SCp.this_residual);
148 		kfree(wh->dma_bounce_buffer);
149 		wh->dma_bounce_buffer = NULL;
150 		wh->dma_bounce_len = 0;
151 	}
152 }
153 
154 static struct scsi_host_template a2091_scsi_template = {
155 	.module			= THIS_MODULE,
156 	.name			= "Commodore A2091/A590 SCSI",
157 	.show_info		= wd33c93_show_info,
158 	.write_info		= wd33c93_write_info,
159 	.proc_name		= "A2901",
160 	.queuecommand		= wd33c93_queuecommand,
161 	.eh_abort_handler	= wd33c93_abort,
162 	.eh_host_reset_handler	= wd33c93_host_reset,
163 	.can_queue		= CAN_QUEUE,
164 	.this_id		= 7,
165 	.sg_tablesize		= SG_ALL,
166 	.cmd_per_lun		= CMD_PER_LUN,
167 	.dma_boundary		= PAGE_SIZE - 1,
168 };
169 
170 static int a2091_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
171 {
172 	struct Scsi_Host *instance;
173 	int error;
174 	struct a2091_scsiregs *regs;
175 	wd33c93_regs wdregs;
176 	struct a2091_hostdata *hdata;
177 
178 	if (!request_mem_region(z->resource.start, 256, "wd33c93"))
179 		return -EBUSY;
180 
181 	instance = scsi_host_alloc(&a2091_scsi_template,
182 				   sizeof(struct a2091_hostdata));
183 	if (!instance) {
184 		error = -ENOMEM;
185 		goto fail_alloc;
186 	}
187 
188 	instance->irq = IRQ_AMIGA_PORTS;
189 	instance->unique_id = z->slotaddr;
190 
191 	regs = ZTWO_VADDR(z->resource.start);
192 	regs->DAWR = DAWR_A2091;
193 
194 	wdregs.SASR = &regs->SASR;
195 	wdregs.SCMD = &regs->SCMD;
196 
197 	hdata = shost_priv(instance);
198 	hdata->wh.no_sync = 0xff;
199 	hdata->wh.fast = 0;
200 	hdata->wh.dma_mode = CTRL_DMA;
201 	hdata->regs = regs;
202 
203 	wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
204 	error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
205 			    "A2091 SCSI", instance);
206 	if (error)
207 		goto fail_irq;
208 
209 	regs->CNTR = CNTR_PDMD | CNTR_INTEN;
210 
211 	error = scsi_add_host(instance, NULL);
212 	if (error)
213 		goto fail_host;
214 
215 	zorro_set_drvdata(z, instance);
216 
217 	scsi_scan_host(instance);
218 	return 0;
219 
220 fail_host:
221 	free_irq(IRQ_AMIGA_PORTS, instance);
222 fail_irq:
223 	scsi_host_put(instance);
224 fail_alloc:
225 	release_mem_region(z->resource.start, 256);
226 	return error;
227 }
228 
229 static void a2091_remove(struct zorro_dev *z)
230 {
231 	struct Scsi_Host *instance = zorro_get_drvdata(z);
232 	struct a2091_hostdata *hdata = shost_priv(instance);
233 
234 	hdata->regs->CNTR = 0;
235 	scsi_remove_host(instance);
236 	free_irq(IRQ_AMIGA_PORTS, instance);
237 	scsi_host_put(instance);
238 	release_mem_region(z->resource.start, 256);
239 }
240 
241 static struct zorro_device_id a2091_zorro_tbl[] = {
242 	{ ZORRO_PROD_CBM_A590_A2091_1 },
243 	{ ZORRO_PROD_CBM_A590_A2091_2 },
244 	{ 0 }
245 };
246 MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
247 
248 static struct zorro_driver a2091_driver = {
249 	.name		= "a2091",
250 	.id_table	= a2091_zorro_tbl,
251 	.probe		= a2091_probe,
252 	.remove		= a2091_remove,
253 };
254 
255 static int __init a2091_init(void)
256 {
257 	return zorro_register_driver(&a2091_driver);
258 }
259 module_init(a2091_init);
260 
261 static void __exit a2091_exit(void)
262 {
263 	zorro_unregister_driver(&a2091_driver);
264 }
265 module_exit(a2091_exit);
266 
267 MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
268 MODULE_LICENSE("GPL");
269