xref: /openbmc/linux/drivers/scsi/arm/eesox.c (revision 27ab1c1c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/acorn/scsi/eesox.c
4  *
5  *  Copyright (C) 1997-2005 Russell King
6  *
7  *  This driver is based on experimentation.  Hence, it may have made
8  *  assumptions about the particular card that I have available, and
9  *  may not be reliable!
10  *
11  *  Changelog:
12  *   01-10-1997	RMK		Created, READONLY version
13  *   15-02-1998	RMK		READ/WRITE version
14  *				added DMA support and hardware definitions
15  *   14-03-1998	RMK		Updated DMA support
16  *				Added terminator control
17  *   15-04-1998	RMK		Only do PIO if FAS216 will allow it.
18  *   27-06-1998	RMK		Changed asm/delay.h to linux/delay.h
19  *   02-04-2000	RMK	0.0.3	Fixed NO_IRQ/NO_DMA problem, updated for new
20  *				error handling code.
21  */
22 #include <linux/module.h>
23 #include <linux/blkdev.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/ioport.h>
27 #include <linux/proc_fs.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/init.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/pgtable.h>
33 
34 #include <asm/io.h>
35 #include <asm/dma.h>
36 #include <asm/ecard.h>
37 
38 #include "../scsi.h"
39 #include <scsi/scsi_host.h>
40 #include "fas216.h"
41 #include "scsi.h"
42 
43 #include <scsi/scsicam.h>
44 
45 #define EESOX_FAS216_OFFSET	0x3000
46 #define EESOX_FAS216_SHIFT	5
47 
48 #define EESOX_DMASTAT		0x2800
49 #define EESOX_STAT_INTR		0x01
50 #define EESOX_STAT_DMA		0x02
51 
52 #define EESOX_CONTROL		0x2800
53 #define EESOX_INTR_ENABLE	0x04
54 #define EESOX_TERM_ENABLE	0x02
55 #define EESOX_RESET		0x01
56 
57 #define EESOX_DMADATA		0x3800
58 
59 #define VERSION "1.10 (17/01/2003 2.5.59)"
60 
61 /*
62  * Use term=0,1,0,0,0 to turn terminators on/off
63  */
64 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
65 
66 #define NR_SG	256
67 
68 struct eesoxscsi_info {
69 	FAS216_Info		info;
70 	struct expansion_card	*ec;
71 	void __iomem		*base;
72 	void __iomem		*ctl_port;
73 	unsigned int		control;
74 	struct scatterlist	sg[NR_SG];	/* Scatter DMA list	*/
75 };
76 
77 /* Prototype: void eesoxscsi_irqenable(ec, irqnr)
78  * Purpose  : Enable interrupts on EESOX SCSI card
79  * Params   : ec    - expansion card structure
80  *          : irqnr - interrupt number
81  */
82 static void
83 eesoxscsi_irqenable(struct expansion_card *ec, int irqnr)
84 {
85 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
86 
87 	info->control |= EESOX_INTR_ENABLE;
88 
89 	writeb(info->control, info->ctl_port);
90 }
91 
92 /* Prototype: void eesoxscsi_irqdisable(ec, irqnr)
93  * Purpose  : Disable interrupts on EESOX SCSI card
94  * Params   : ec    - expansion card structure
95  *          : irqnr - interrupt number
96  */
97 static void
98 eesoxscsi_irqdisable(struct expansion_card *ec, int irqnr)
99 {
100 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
101 
102 	info->control &= ~EESOX_INTR_ENABLE;
103 
104 	writeb(info->control, info->ctl_port);
105 }
106 
107 static const expansioncard_ops_t eesoxscsi_ops = {
108 	.irqenable	= eesoxscsi_irqenable,
109 	.irqdisable	= eesoxscsi_irqdisable,
110 };
111 
112 /* Prototype: void eesoxscsi_terminator_ctl(*host, on_off)
113  * Purpose  : Turn the EESOX SCSI terminators on or off
114  * Params   : host   - card to turn on/off
115  *          : on_off - !0 to turn on, 0 to turn off
116  */
117 static void
118 eesoxscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
119 {
120 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
121 	unsigned long flags;
122 
123 	spin_lock_irqsave(host->host_lock, flags);
124 	if (on_off)
125 		info->control |= EESOX_TERM_ENABLE;
126 	else
127 		info->control &= ~EESOX_TERM_ENABLE;
128 
129 	writeb(info->control, info->ctl_port);
130 	spin_unlock_irqrestore(host->host_lock, flags);
131 }
132 
133 /* Prototype: void eesoxscsi_intr(irq, *dev_id, *regs)
134  * Purpose  : handle interrupts from EESOX SCSI card
135  * Params   : irq    - interrupt number
136  *	      dev_id - user-defined (Scsi_Host structure)
137  */
138 static irqreturn_t
139 eesoxscsi_intr(int irq, void *dev_id)
140 {
141 	struct eesoxscsi_info *info = dev_id;
142 
143 	return fas216_intr(&info->info);
144 }
145 
146 /* Prototype: fasdmatype_t eesoxscsi_dma_setup(host, SCpnt, direction, min_type)
147  * Purpose  : initialises DMA/PIO
148  * Params   : host      - host
149  *	      SCpnt     - command
150  *	      direction - DMA on to/off of card
151  *	      min_type  - minimum DMA support that we must have for this transfer
152  * Returns  : type of transfer to be performed
153  */
154 static fasdmatype_t
155 eesoxscsi_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
156 		       fasdmadir_t direction, fasdmatype_t min_type)
157 {
158 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
159 	struct device *dev = scsi_get_device(host);
160 	int dmach = info->info.scsi.dma;
161 
162 	if (dmach != NO_DMA &&
163 	    (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
164 		int bufs, map_dir, dma_dir;
165 
166 		bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
167 
168 		if (direction == DMA_OUT) {
169 			map_dir = DMA_TO_DEVICE;
170 			dma_dir = DMA_MODE_WRITE;
171 		} else {
172 			map_dir = DMA_FROM_DEVICE;
173 			dma_dir = DMA_MODE_READ;
174 		}
175 
176 		dma_map_sg(dev, info->sg, bufs, map_dir);
177 
178 		disable_dma(dmach);
179 		set_dma_sg(dmach, info->sg, bufs);
180 		set_dma_mode(dmach, dma_dir);
181 		enable_dma(dmach);
182 		return fasdma_real_all;
183 	}
184 	/*
185 	 * We don't do DMA, we only do slow PIO
186 	 *
187 	 * Some day, we will do Pseudo DMA
188 	 */
189 	return fasdma_pseudo;
190 }
191 
192 static void eesoxscsi_buffer_in(void *buf, int length, void __iomem *base)
193 {
194 	const void __iomem *reg_fas = base + EESOX_FAS216_OFFSET;
195 	const void __iomem *reg_dmastat = base + EESOX_DMASTAT;
196 	const void __iomem *reg_dmadata = base + EESOX_DMADATA;
197 	register const unsigned long mask = 0xffff;
198 
199 	do {
200 		unsigned int status;
201 
202 		/*
203 		 * Interrupt request?
204 		 */
205 		status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
206 		if (status & STAT_INT)
207 			break;
208 
209 		/*
210 		 * DMA request active?
211 		 */
212 		status = readb(reg_dmastat);
213 		if (!(status & EESOX_STAT_DMA))
214 			continue;
215 
216 		/*
217 		 * Get number of bytes in FIFO
218 		 */
219 		status = readb(reg_fas + (REG_CFIS << EESOX_FAS216_SHIFT)) & CFIS_CF;
220 		if (status > 16)
221 			status = 16;
222 		if (status > length)
223 			status = length;
224 
225 		/*
226 		 * Align buffer.
227 		 */
228 		if (((u32)buf) & 2 && status >= 2) {
229 			*(u16 *)buf = readl(reg_dmadata);
230 			buf += 2;
231 			status -= 2;
232 			length -= 2;
233 		}
234 
235 		if (status >= 8) {
236 			unsigned long l1, l2;
237 
238 			l1 = readl(reg_dmadata) & mask;
239 			l1 |= readl(reg_dmadata) << 16;
240 			l2 = readl(reg_dmadata) & mask;
241 			l2 |= readl(reg_dmadata) << 16;
242 			*(u32 *)buf = l1;
243 			buf += 4;
244 			*(u32 *)buf = l2;
245 			buf += 4;
246 			length -= 8;
247 			continue;
248 		}
249 
250 		if (status >= 4) {
251 			unsigned long l1;
252 
253 			l1 = readl(reg_dmadata) & mask;
254 			l1 |= readl(reg_dmadata) << 16;
255 
256 			*(u32 *)buf = l1;
257 			buf += 4;
258 			length -= 4;
259 			continue;
260 		}
261 
262 		if (status >= 2) {
263 			*(u16 *)buf = readl(reg_dmadata);
264 			buf += 2;
265 			length -= 2;
266 		}
267 	} while (length);
268 }
269 
270 static void eesoxscsi_buffer_out(void *buf, int length, void __iomem *base)
271 {
272 	const void __iomem *reg_fas = base + EESOX_FAS216_OFFSET;
273 	const void __iomem *reg_dmastat = base + EESOX_DMASTAT;
274 	void __iomem *reg_dmadata = base + EESOX_DMADATA;
275 
276 	do {
277 		unsigned int status;
278 
279 		/*
280 		 * Interrupt request?
281 		 */
282 		status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
283 		if (status & STAT_INT)
284 			break;
285 
286 		/*
287 		 * DMA request active?
288 		 */
289 		status = readb(reg_dmastat);
290 		if (!(status & EESOX_STAT_DMA))
291 			continue;
292 
293 		/*
294 		 * Get number of bytes in FIFO
295 		 */
296 		status = readb(reg_fas + (REG_CFIS << EESOX_FAS216_SHIFT)) & CFIS_CF;
297 		if (status > 16)
298 			status = 16;
299 		status = 16 - status;
300 		if (status > length)
301 			status = length;
302 		status &= ~1;
303 
304 		/*
305 		 * Align buffer.
306 		 */
307 		if (((u32)buf) & 2 && status >= 2) {
308 			writel(*(u16 *)buf << 16, reg_dmadata);
309 			buf += 2;
310 			status -= 2;
311 			length -= 2;
312 		}
313 
314 		if (status >= 8) {
315 			unsigned long l1, l2;
316 
317 			l1 = *(u32 *)buf;
318 			buf += 4;
319 			l2 = *(u32 *)buf;
320 			buf += 4;
321 
322 			writel(l1 << 16, reg_dmadata);
323 			writel(l1, reg_dmadata);
324 			writel(l2 << 16, reg_dmadata);
325 			writel(l2, reg_dmadata);
326 			length -= 8;
327 			continue;
328 		}
329 
330 		if (status >= 4) {
331 			unsigned long l1;
332 
333 			l1 = *(u32 *)buf;
334 			buf += 4;
335 
336 			writel(l1 << 16, reg_dmadata);
337 			writel(l1, reg_dmadata);
338 			length -= 4;
339 			continue;
340 		}
341 
342 		if (status >= 2) {
343 			writel(*(u16 *)buf << 16, reg_dmadata);
344 			buf += 2;
345 			length -= 2;
346 		}
347 	} while (length);
348 }
349 
350 static void
351 eesoxscsi_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp,
352 		     fasdmadir_t dir, int transfer_size)
353 {
354 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
355 	if (dir == DMA_IN) {
356 		eesoxscsi_buffer_in(SCp->ptr, SCp->this_residual, info->base);
357 	} else {
358 		eesoxscsi_buffer_out(SCp->ptr, SCp->this_residual, info->base);
359 	}
360 }
361 
362 /* Prototype: int eesoxscsi_dma_stop(host, SCpnt)
363  * Purpose  : stops DMA/PIO
364  * Params   : host  - host
365  *	      SCpnt - command
366  */
367 static void
368 eesoxscsi_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
369 {
370 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
371 	if (info->info.scsi.dma != NO_DMA)
372 		disable_dma(info->info.scsi.dma);
373 }
374 
375 /* Prototype: const char *eesoxscsi_info(struct Scsi_Host * host)
376  * Purpose  : returns a descriptive string about this interface,
377  * Params   : host - driver host structure to return info for.
378  * Returns  : pointer to a static buffer containing null terminated string.
379  */
380 const char *eesoxscsi_info(struct Scsi_Host *host)
381 {
382 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
383 	static char string[150];
384 
385 	sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
386 		host->hostt->name, info->info.scsi.type, info->ec->slot_no,
387 		VERSION, info->control & EESOX_TERM_ENABLE ? "n" : "ff");
388 
389 	return string;
390 }
391 
392 /* Prototype: int eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
393  * Purpose  : Set a driver specific function
394  * Params   : host   - host to setup
395  *          : buffer - buffer containing string describing operation
396  *          : length - length of string
397  * Returns  : -EINVAL, or 0
398  */
399 static int
400 eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
401 {
402 	int ret = length;
403 
404 	if (length >= 9 && strncmp(buffer, "EESOXSCSI", 9) == 0) {
405 		buffer += 9;
406 		length -= 9;
407 
408 		if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
409 			if (buffer[5] == '1')
410 				eesoxscsi_terminator_ctl(host, 1);
411 			else if (buffer[5] == '0')
412 				eesoxscsi_terminator_ctl(host, 0);
413 			else
414 				ret = -EINVAL;
415 		} else
416 			ret = -EINVAL;
417 	} else
418 		ret = -EINVAL;
419 
420 	return ret;
421 }
422 
423 static int eesoxscsi_show_info(struct seq_file *m, struct Scsi_Host *host)
424 {
425 	struct eesoxscsi_info *info;
426 
427 	info = (struct eesoxscsi_info *)host->hostdata;
428 
429 	seq_printf(m, "EESOX SCSI driver v%s\n", VERSION);
430 	fas216_print_host(&info->info, m);
431 	seq_printf(m, "Term    : o%s\n",
432 			info->control & EESOX_TERM_ENABLE ? "n" : "ff");
433 
434 	fas216_print_stats(&info->info, m);
435 	fas216_print_devices(&info->info, m);
436 	return 0;
437 }
438 
439 static ssize_t eesoxscsi_show_term(struct device *dev, struct device_attribute *attr, char *buf)
440 {
441 	struct expansion_card *ec = ECARD_DEV(dev);
442 	struct Scsi_Host *host = ecard_get_drvdata(ec);
443 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
444 
445 	return sprintf(buf, "%d\n", info->control & EESOX_TERM_ENABLE ? 1 : 0);
446 }
447 
448 static ssize_t eesoxscsi_store_term(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
449 {
450 	struct expansion_card *ec = ECARD_DEV(dev);
451 	struct Scsi_Host *host = ecard_get_drvdata(ec);
452 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
453 	unsigned long flags;
454 
455 	if (len > 1) {
456 		spin_lock_irqsave(host->host_lock, flags);
457 		if (buf[0] != '0') {
458 			info->control |= EESOX_TERM_ENABLE;
459 		} else {
460 			info->control &= ~EESOX_TERM_ENABLE;
461 		}
462 		writeb(info->control, info->ctl_port);
463 		spin_unlock_irqrestore(host->host_lock, flags);
464 	}
465 
466 	return len;
467 }
468 
469 static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
470 		   eesoxscsi_show_term, eesoxscsi_store_term);
471 
472 static struct scsi_host_template eesox_template = {
473 	.module				= THIS_MODULE,
474 	.show_info			= eesoxscsi_show_info,
475 	.write_info			= eesoxscsi_set_proc_info,
476 	.name				= "EESOX SCSI",
477 	.info				= eesoxscsi_info,
478 	.queuecommand			= fas216_queue_command,
479 	.eh_host_reset_handler		= fas216_eh_host_reset,
480 	.eh_bus_reset_handler		= fas216_eh_bus_reset,
481 	.eh_device_reset_handler	= fas216_eh_device_reset,
482 	.eh_abort_handler		= fas216_eh_abort,
483 	.can_queue			= 1,
484 	.this_id			= 7,
485 	.sg_tablesize			= SG_MAX_SEGMENTS,
486 	.dma_boundary			= IOMD_DMA_BOUNDARY,
487 	.proc_name			= "eesox",
488 };
489 
490 static int eesoxscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
491 {
492 	struct Scsi_Host *host;
493 	struct eesoxscsi_info *info;
494 	void __iomem *base;
495 	int ret;
496 
497 	ret = ecard_request_resources(ec);
498 	if (ret)
499 		goto out;
500 
501 	base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
502 	if (!base) {
503 		ret = -ENOMEM;
504 		goto out_region;
505 	}
506 
507 	host = scsi_host_alloc(&eesox_template,
508 			       sizeof(struct eesoxscsi_info));
509 	if (!host) {
510 		ret = -ENOMEM;
511 		goto out_region;
512 	}
513 
514 	ecard_set_drvdata(ec, host);
515 
516 	info = (struct eesoxscsi_info *)host->hostdata;
517 	info->ec	= ec;
518 	info->base	= base;
519 	info->ctl_port	= base + EESOX_CONTROL;
520 	info->control	= term[ec->slot_no] ? EESOX_TERM_ENABLE : 0;
521 	writeb(info->control, info->ctl_port);
522 
523 	info->info.scsi.io_base		= base + EESOX_FAS216_OFFSET;
524 	info->info.scsi.io_shift	= EESOX_FAS216_SHIFT;
525 	info->info.scsi.irq		= ec->irq;
526 	info->info.scsi.dma		= ec->dma;
527 	info->info.ifcfg.clockrate	= 40; /* MHz */
528 	info->info.ifcfg.select_timeout	= 255;
529 	info->info.ifcfg.asyncperiod	= 200; /* ns */
530 	info->info.ifcfg.sync_max_depth	= 7;
531 	info->info.ifcfg.cntl3		= CNTL3_FASTSCSI | CNTL3_FASTCLK;
532 	info->info.ifcfg.disconnect_ok	= 1;
533 	info->info.ifcfg.wide_max_size	= 0;
534 	info->info.ifcfg.capabilities	= FASCAP_PSEUDODMA;
535 	info->info.dma.setup		= eesoxscsi_dma_setup;
536 	info->info.dma.pseudo		= eesoxscsi_dma_pseudo;
537 	info->info.dma.stop		= eesoxscsi_dma_stop;
538 
539 	ec->irqaddr	= base + EESOX_DMASTAT;
540 	ec->irqmask	= EESOX_STAT_INTR;
541 
542 	ecard_setirq(ec, &eesoxscsi_ops, info);
543 
544 	device_create_file(&ec->dev, &dev_attr_bus_term);
545 
546 	ret = fas216_init(host);
547 	if (ret)
548 		goto out_free;
549 
550 	ret = request_irq(ec->irq, eesoxscsi_intr, 0, "eesoxscsi", info);
551 	if (ret) {
552 		printk("scsi%d: IRQ%d not free: %d\n",
553 		       host->host_no, ec->irq, ret);
554 		goto out_remove;
555 	}
556 
557 	if (info->info.scsi.dma != NO_DMA) {
558 		if (request_dma(info->info.scsi.dma, "eesox")) {
559 			printk("scsi%d: DMA%d not free, DMA disabled\n",
560 			       host->host_no, info->info.scsi.dma);
561 			info->info.scsi.dma = NO_DMA;
562 		} else {
563 			set_dma_speed(info->info.scsi.dma, 180);
564 			info->info.ifcfg.capabilities |= FASCAP_DMA;
565 			info->info.ifcfg.cntl3 |= CNTL3_BS8;
566 		}
567 	}
568 
569 	ret = fas216_add(host, &ec->dev);
570 	if (ret == 0)
571 		goto out;
572 
573 	if (info->info.scsi.dma != NO_DMA)
574 		free_dma(info->info.scsi.dma);
575 	free_irq(ec->irq, info);
576 
577  out_remove:
578 	fas216_remove(host);
579 
580  out_free:
581 	device_remove_file(&ec->dev, &dev_attr_bus_term);
582 	scsi_host_put(host);
583 
584  out_region:
585 	ecard_release_resources(ec);
586 
587  out:
588 	return ret;
589 }
590 
591 static void eesoxscsi_remove(struct expansion_card *ec)
592 {
593 	struct Scsi_Host *host = ecard_get_drvdata(ec);
594 	struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
595 
596 	ecard_set_drvdata(ec, NULL);
597 	fas216_remove(host);
598 
599 	if (info->info.scsi.dma != NO_DMA)
600 		free_dma(info->info.scsi.dma);
601 	free_irq(ec->irq, info);
602 
603 	device_remove_file(&ec->dev, &dev_attr_bus_term);
604 
605 	fas216_release(host);
606 	scsi_host_put(host);
607 	ecard_release_resources(ec);
608 }
609 
610 static const struct ecard_id eesoxscsi_cids[] = {
611 	{ MANU_EESOX, PROD_EESOX_SCSI2 },
612 	{ 0xffff, 0xffff },
613 };
614 
615 static struct ecard_driver eesoxscsi_driver = {
616 	.probe		= eesoxscsi_probe,
617 	.remove		= eesoxscsi_remove,
618 	.id_table	= eesoxscsi_cids,
619 	.drv = {
620 		.name		= "eesoxscsi",
621 	},
622 };
623 
624 static int __init eesox_init(void)
625 {
626 	return ecard_register_driver(&eesoxscsi_driver);
627 }
628 
629 static void __exit eesox_exit(void)
630 {
631 	ecard_remove_driver(&eesoxscsi_driver);
632 }
633 
634 module_init(eesox_init);
635 module_exit(eesox_exit);
636 
637 MODULE_AUTHOR("Russell King");
638 MODULE_DESCRIPTION("EESOX 'Fast' SCSI driver for Acorn machines");
639 module_param_array(term, int, NULL, 0);
640 MODULE_PARM_DESC(term, "SCSI bus termination");
641 MODULE_LICENSE("GPL");
642