xref: /linux/drivers/scsi/arm/cumana_2.c (revision 95298d63c67673c654c08952672d016212b26054)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/acorn/scsi/cumana_2.c
4  *
5  *  Copyright (C) 1997-2005 Russell King
6  *
7  *  Changelog:
8  *   30-08-1997	RMK	0.0.0	Created, READONLY version.
9  *   22-01-1998	RMK	0.0.1	Updated to 2.1.80.
10  *   15-04-1998	RMK	0.0.1	Only do PIO if FAS216 will allow it.
11  *   02-05-1998	RMK	0.0.2	Updated & added DMA support.
12  *   27-06-1998	RMK		Changed asm/delay.h to linux/delay.h
13  *   18-08-1998	RMK	0.0.3	Fixed synchronous transfer depth.
14  *   02-04-2000	RMK	0.0.4	Updated for new error handling code.
15  */
16 #include <linux/module.h>
17 #include <linux/blkdev.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/ioport.h>
21 #include <linux/proc_fs.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pgtable.h>
27 
28 #include <asm/dma.h>
29 #include <asm/ecard.h>
30 #include <asm/io.h>
31 
32 #include "../scsi.h"
33 #include <scsi/scsi_host.h>
34 #include "fas216.h"
35 #include "scsi.h"
36 
37 #include <scsi/scsicam.h>
38 
39 #define CUMANASCSI2_STATUS		(0x0000)
40 #define STATUS_INT			(1 << 0)
41 #define STATUS_DRQ			(1 << 1)
42 #define STATUS_LATCHED			(1 << 3)
43 
44 #define CUMANASCSI2_ALATCH		(0x0014)
45 #define ALATCH_ENA_INT			(3)
46 #define ALATCH_DIS_INT			(2)
47 #define ALATCH_ENA_TERM			(5)
48 #define ALATCH_DIS_TERM			(4)
49 #define ALATCH_ENA_BIT32		(11)
50 #define ALATCH_DIS_BIT32		(10)
51 #define ALATCH_ENA_DMA			(13)
52 #define ALATCH_DIS_DMA			(12)
53 #define ALATCH_DMA_OUT			(15)
54 #define ALATCH_DMA_IN			(14)
55 
56 #define CUMANASCSI2_PSEUDODMA		(0x0200)
57 
58 #define CUMANASCSI2_FAS216_OFFSET	(0x0300)
59 #define CUMANASCSI2_FAS216_SHIFT	2
60 
61 /*
62  * Version
63  */
64 #define VERSION "1.00 (13/11/2002 2.5.47)"
65 
66 /*
67  * Use term=0,1,0,0,0 to turn terminators on/off
68  */
69 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
70 
71 #define NR_SG	256
72 
73 struct cumanascsi2_info {
74 	FAS216_Info		info;
75 	struct expansion_card	*ec;
76 	void __iomem		*base;
77 	unsigned int		terms;		/* Terminator state	*/
78 	struct scatterlist	sg[NR_SG];	/* Scatter DMA list	*/
79 };
80 
81 #define CSTATUS_IRQ	(1 << 0)
82 #define CSTATUS_DRQ	(1 << 1)
83 
84 /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
85  * Purpose  : Enable interrupts on Cumana SCSI 2 card
86  * Params   : ec    - expansion card structure
87  *          : irqnr - interrupt number
88  */
89 static void
90 cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
91 {
92 	struct cumanascsi2_info *info = ec->irq_data;
93 	writeb(ALATCH_ENA_INT, info->base + CUMANASCSI2_ALATCH);
94 }
95 
96 /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
97  * Purpose  : Disable interrupts on Cumana SCSI 2 card
98  * Params   : ec    - expansion card structure
99  *          : irqnr - interrupt number
100  */
101 static void
102 cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
103 {
104 	struct cumanascsi2_info *info = ec->irq_data;
105 	writeb(ALATCH_DIS_INT, info->base + CUMANASCSI2_ALATCH);
106 }
107 
108 static const expansioncard_ops_t cumanascsi_2_ops = {
109 	.irqenable	= cumanascsi_2_irqenable,
110 	.irqdisable	= cumanascsi_2_irqdisable,
111 };
112 
113 /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
114  * Purpose  : Turn the Cumana SCSI 2 terminators on or off
115  * Params   : host   - card to turn on/off
116  *          : on_off - !0 to turn on, 0 to turn off
117  */
118 static void
119 cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
120 {
121 	struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
122 
123 	if (on_off) {
124 		info->terms = 1;
125 		writeb(ALATCH_ENA_TERM, info->base + CUMANASCSI2_ALATCH);
126 	} else {
127 		info->terms = 0;
128 		writeb(ALATCH_DIS_TERM, info->base + CUMANASCSI2_ALATCH);
129 	}
130 }
131 
132 /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
133  * Purpose  : handle interrupts from Cumana SCSI 2 card
134  * Params   : irq    - interrupt number
135  *	      dev_id - user-defined (Scsi_Host structure)
136  */
137 static irqreturn_t
138 cumanascsi_2_intr(int irq, void *dev_id)
139 {
140 	struct cumanascsi2_info *info = dev_id;
141 
142 	return fas216_intr(&info->info);
143 }
144 
145 /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
146  * Purpose  : initialises DMA/PIO
147  * Params   : host      - host
148  *	      SCpnt     - command
149  *	      direction - DMA on to/off of card
150  *	      min_type  - minimum DMA support that we must have for this transfer
151  * Returns  : type of transfer to be performed
152  */
153 static fasdmatype_t
154 cumanascsi_2_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
155 		       fasdmadir_t direction, fasdmatype_t min_type)
156 {
157 	struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
158 	struct device *dev = scsi_get_device(host);
159 	int dmach = info->info.scsi.dma;
160 
161 	writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
162 
163 	if (dmach != NO_DMA &&
164 	    (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
165 		int bufs, map_dir, dma_dir, alatch_dir;
166 
167 		bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
168 
169 		if (direction == DMA_OUT)
170 			map_dir = DMA_TO_DEVICE,
171 			dma_dir = DMA_MODE_WRITE,
172 			alatch_dir = ALATCH_DMA_OUT;
173 		else
174 			map_dir = DMA_FROM_DEVICE,
175 			dma_dir = DMA_MODE_READ,
176 			alatch_dir = ALATCH_DMA_IN;
177 
178 		dma_map_sg(dev, info->sg, bufs, map_dir);
179 
180 		disable_dma(dmach);
181 		set_dma_sg(dmach, info->sg, bufs);
182 		writeb(alatch_dir, info->base + CUMANASCSI2_ALATCH);
183 		set_dma_mode(dmach, dma_dir);
184 		enable_dma(dmach);
185 		writeb(ALATCH_ENA_DMA, info->base + CUMANASCSI2_ALATCH);
186 		writeb(ALATCH_DIS_BIT32, info->base + CUMANASCSI2_ALATCH);
187 		return fasdma_real_all;
188 	}
189 
190 	/*
191 	 * If we're not doing DMA,
192 	 *  we'll do pseudo DMA
193 	 */
194 	return fasdma_pio;
195 }
196 
197 /*
198  * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
199  * Purpose  : handles pseudo DMA
200  * Params   : host      - host
201  *	      SCpnt     - command
202  *	      direction - DMA on to/off of card
203  *	      transfer  - minimum number of bytes we expect to transfer
204  */
205 static void
206 cumanascsi_2_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp,
207 			fasdmadir_t direction, int transfer)
208 {
209 	struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
210 	unsigned int length;
211 	unsigned char *addr;
212 
213 	length = SCp->this_residual;
214 	addr = SCp->ptr;
215 
216 	if (direction == DMA_OUT)
217 #if 0
218 		while (length > 1) {
219 			unsigned long word;
220 			unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
221 
222 			if (status & STATUS_INT)
223 				goto end;
224 
225 			if (!(status & STATUS_DRQ))
226 				continue;
227 
228 			word = *addr | *(addr + 1) << 8;
229 			writew(word, info->base + CUMANASCSI2_PSEUDODMA);
230 			addr += 2;
231 			length -= 2;
232 		}
233 #else
234 		printk ("PSEUDO_OUT???\n");
235 #endif
236 	else {
237 		if (transfer && (transfer & 255)) {
238 			while (length >= 256) {
239 				unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
240 
241 				if (status & STATUS_INT)
242 					return;
243 
244 				if (!(status & STATUS_DRQ))
245 					continue;
246 
247 				readsw(info->base + CUMANASCSI2_PSEUDODMA,
248 				       addr, 256 >> 1);
249 				addr += 256;
250 				length -= 256;
251 			}
252 		}
253 
254 		while (length > 0) {
255 			unsigned long word;
256 			unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
257 
258 			if (status & STATUS_INT)
259 				return;
260 
261 			if (!(status & STATUS_DRQ))
262 				continue;
263 
264 			word = readw(info->base + CUMANASCSI2_PSEUDODMA);
265 			*addr++ = word;
266 			if (--length > 0) {
267 				*addr++ = word >> 8;
268 				length --;
269 			}
270 		}
271 	}
272 }
273 
274 /* Prototype: int cumanascsi_2_dma_stop(host, SCpnt)
275  * Purpose  : stops DMA/PIO
276  * Params   : host  - host
277  *	      SCpnt - command
278  */
279 static void
280 cumanascsi_2_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
281 {
282 	struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
283 	if (info->info.scsi.dma != NO_DMA) {
284 		writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
285 		disable_dma(info->info.scsi.dma);
286 	}
287 }
288 
289 /* Prototype: const char *cumanascsi_2_info(struct Scsi_Host * host)
290  * Purpose  : returns a descriptive string about this interface,
291  * Params   : host - driver host structure to return info for.
292  * Returns  : pointer to a static buffer containing null terminated string.
293  */
294 const char *cumanascsi_2_info(struct Scsi_Host *host)
295 {
296 	struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
297 	static char string[150];
298 
299 	sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
300 		host->hostt->name, info->info.scsi.type, info->ec->slot_no,
301 		VERSION, info->terms ? "n" : "ff");
302 
303 	return string;
304 }
305 
306 /* Prototype: int cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
307  * Purpose  : Set a driver specific function
308  * Params   : host   - host to setup
309  *          : buffer - buffer containing string describing operation
310  *          : length - length of string
311  * Returns  : -EINVAL, or 0
312  */
313 static int
314 cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
315 {
316 	int ret = length;
317 
318 	if (length >= 11 && strncmp(buffer, "CUMANASCSI2", 11) == 0) {
319 		buffer += 11;
320 		length -= 11;
321 
322 		if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
323 			if (buffer[5] == '1')
324 				cumanascsi_2_terminator_ctl(host, 1);
325 			else if (buffer[5] == '0')
326 				cumanascsi_2_terminator_ctl(host, 0);
327 			else
328 				ret = -EINVAL;
329 		} else
330 			ret = -EINVAL;
331 	} else
332 		ret = -EINVAL;
333 
334 	return ret;
335 }
336 
337 static int cumanascsi_2_show_info(struct seq_file *m, struct Scsi_Host *host)
338 {
339 	struct cumanascsi2_info *info;
340 	info = (struct cumanascsi2_info *)host->hostdata;
341 
342 	seq_printf(m, "Cumana SCSI II driver v%s\n", VERSION);
343 	fas216_print_host(&info->info, m);
344 	seq_printf(m, "Term    : o%s\n",
345 			info->terms ? "n" : "ff");
346 
347 	fas216_print_stats(&info->info, m);
348 	fas216_print_devices(&info->info, m);
349 	return 0;
350 }
351 
352 static struct scsi_host_template cumanascsi2_template = {
353 	.module				= THIS_MODULE,
354 	.show_info			= cumanascsi_2_show_info,
355 	.write_info			= cumanascsi_2_set_proc_info,
356 	.name				= "Cumana SCSI II",
357 	.info				= cumanascsi_2_info,
358 	.queuecommand			= fas216_queue_command,
359 	.eh_host_reset_handler		= fas216_eh_host_reset,
360 	.eh_bus_reset_handler		= fas216_eh_bus_reset,
361 	.eh_device_reset_handler	= fas216_eh_device_reset,
362 	.eh_abort_handler		= fas216_eh_abort,
363 	.can_queue			= 1,
364 	.this_id			= 7,
365 	.sg_tablesize			= SG_MAX_SEGMENTS,
366 	.dma_boundary			= IOMD_DMA_BOUNDARY,
367 	.proc_name			= "cumanascsi2",
368 };
369 
370 static int cumanascsi2_probe(struct expansion_card *ec,
371 			     const struct ecard_id *id)
372 {
373 	struct Scsi_Host *host;
374 	struct cumanascsi2_info *info;
375 	void __iomem *base;
376 	int ret;
377 
378 	ret = ecard_request_resources(ec);
379 	if (ret)
380 		goto out;
381 
382 	base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
383 	if (!base) {
384 		ret = -ENOMEM;
385 		goto out_region;
386 	}
387 
388 	host = scsi_host_alloc(&cumanascsi2_template,
389 			       sizeof(struct cumanascsi2_info));
390 	if (!host) {
391 		ret = -ENOMEM;
392 		goto out_region;
393 	}
394 
395 	ecard_set_drvdata(ec, host);
396 
397 	info = (struct cumanascsi2_info *)host->hostdata;
398 	info->ec	= ec;
399 	info->base	= base;
400 
401 	cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
402 
403 	info->info.scsi.io_base		= base + CUMANASCSI2_FAS216_OFFSET;
404 	info->info.scsi.io_shift	= CUMANASCSI2_FAS216_SHIFT;
405 	info->info.scsi.irq		= ec->irq;
406 	info->info.scsi.dma		= ec->dma;
407 	info->info.ifcfg.clockrate	= 40; /* MHz */
408 	info->info.ifcfg.select_timeout	= 255;
409 	info->info.ifcfg.asyncperiod	= 200; /* ns */
410 	info->info.ifcfg.sync_max_depth	= 7;
411 	info->info.ifcfg.cntl3		= CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
412 	info->info.ifcfg.disconnect_ok	= 1;
413 	info->info.ifcfg.wide_max_size	= 0;
414 	info->info.ifcfg.capabilities	= FASCAP_PSEUDODMA;
415 	info->info.dma.setup		= cumanascsi_2_dma_setup;
416 	info->info.dma.pseudo		= cumanascsi_2_dma_pseudo;
417 	info->info.dma.stop		= cumanascsi_2_dma_stop;
418 
419 	ec->irqaddr	= info->base + CUMANASCSI2_STATUS;
420 	ec->irqmask	= STATUS_INT;
421 
422 	ecard_setirq(ec, &cumanascsi_2_ops, info);
423 
424 	ret = fas216_init(host);
425 	if (ret)
426 		goto out_free;
427 
428 	ret = request_irq(ec->irq, cumanascsi_2_intr,
429 			  0, "cumanascsi2", info);
430 	if (ret) {
431 		printk("scsi%d: IRQ%d not free: %d\n",
432 		       host->host_no, ec->irq, ret);
433 		goto out_release;
434 	}
435 
436 	if (info->info.scsi.dma != NO_DMA) {
437 		if (request_dma(info->info.scsi.dma, "cumanascsi2")) {
438 			printk("scsi%d: DMA%d not free, using PIO\n",
439 			       host->host_no, info->info.scsi.dma);
440 			info->info.scsi.dma = NO_DMA;
441 		} else {
442 			set_dma_speed(info->info.scsi.dma, 180);
443 			info->info.ifcfg.capabilities |= FASCAP_DMA;
444 		}
445 	}
446 
447 	ret = fas216_add(host, &ec->dev);
448 	if (ret == 0)
449 		goto out;
450 
451 	if (info->info.scsi.dma != NO_DMA)
452 		free_dma(info->info.scsi.dma);
453 	free_irq(ec->irq, host);
454 
455  out_release:
456 	fas216_release(host);
457 
458  out_free:
459 	scsi_host_put(host);
460 
461  out_region:
462 	ecard_release_resources(ec);
463 
464  out:
465 	return ret;
466 }
467 
468 static void cumanascsi2_remove(struct expansion_card *ec)
469 {
470 	struct Scsi_Host *host = ecard_get_drvdata(ec);
471 	struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
472 
473 	ecard_set_drvdata(ec, NULL);
474 	fas216_remove(host);
475 
476 	if (info->info.scsi.dma != NO_DMA)
477 		free_dma(info->info.scsi.dma);
478 	free_irq(ec->irq, info);
479 
480 	fas216_release(host);
481 	scsi_host_put(host);
482 	ecard_release_resources(ec);
483 }
484 
485 static const struct ecard_id cumanascsi2_cids[] = {
486 	{ MANU_CUMANA, PROD_CUMANA_SCSI_2 },
487 	{ 0xffff, 0xffff },
488 };
489 
490 static struct ecard_driver cumanascsi2_driver = {
491 	.probe		= cumanascsi2_probe,
492 	.remove		= cumanascsi2_remove,
493 	.id_table	= cumanascsi2_cids,
494 	.drv = {
495 		.name		= "cumanascsi2",
496 	},
497 };
498 
499 static int __init cumanascsi2_init(void)
500 {
501 	return ecard_register_driver(&cumanascsi2_driver);
502 }
503 
504 static void __exit cumanascsi2_exit(void)
505 {
506 	ecard_remove_driver(&cumanascsi2_driver);
507 }
508 
509 module_init(cumanascsi2_init);
510 module_exit(cumanascsi2_exit);
511 
512 MODULE_AUTHOR("Russell King");
513 MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
514 module_param_array(term, int, NULL, 0);
515 MODULE_PARM_DESC(term, "SCSI bus termination");
516 MODULE_LICENSE("GPL");
517