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