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