1 /*
2 * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
3 *
4 * Sun3 DMA routines added by Sam Creasey (sammy@sammy.net)
5 *
6 * VME support added by Sam Creasey
7 *
8 * Adapted from sun3_scsi.c -- see there for other headers
9 *
10 * TODO: modify this driver to support multiple Sun3 SCSI VME boards
11 *
12 */
13
14#define AUTOSENSE
15
16#include <linux/types.h>
17#include <linux/stddef.h>
18#include <linux/ctype.h>
19#include <linux/delay.h>
20
21#include <linux/module.h>
22#include <linux/signal.h>
23#include <linux/ioport.h>
24#include <linux/init.h>
25#include <linux/blkdev.h>
26
27#include <asm/io.h>
28#include <asm/system.h>
29
30#include <asm/sun3ints.h>
31#include <asm/dvma.h>
32#include <asm/idprom.h>
33#include <asm/machines.h>
34
35#define SUN3_SCSI_VME
36
37#undef SUN3_SCSI_DEBUG
38
39/* dma on! */
40#define REAL_DMA
41
42#include "scsi.h"
43#include "initio.h"
44#include <scsi/scsi_host.h>
45#include "sun3_scsi.h"
46
47extern int sun3_map_test(unsigned long, char *);
48
49#define USE_WRAPPER
50/*#define RESET_BOOT */
51#define DRIVER_SETUP
52
53#define NDEBUG 0
54
55/*
56 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
57 */
58#ifdef BUG
59#undef RESET_BOOT
60#undef DRIVER_SETUP
61#endif
62
63/* #define SUPPORT_TAGS */
64
65//#define	ENABLE_IRQ()	enable_irq( SUN3_VEC_VMESCSI0 );
66#define ENABLE_IRQ()
67
68
69static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
70static inline unsigned char sun3scsi_read(int reg);
71static inline void sun3scsi_write(int reg, int value);
72
73static int setup_can_queue = -1;
74module_param(setup_can_queue, int, 0);
75static int setup_cmd_per_lun = -1;
76module_param(setup_cmd_per_lun, int, 0);
77static int setup_sg_tablesize = -1;
78module_param(setup_sg_tablesize, int, 0);
79#ifdef SUPPORT_TAGS
80static int setup_use_tagged_queuing = -1;
81module_param(setup_use_tagged_queuing, int, 0);
82#endif
83static int setup_hostid = -1;
84module_param(setup_hostid, int, 0);
85
86static struct scsi_cmnd *sun3_dma_setup_done = NULL;
87
88#define	AFTER_RESET_DELAY	(HZ/2)
89
90/* ms to wait after hitting dma regs */
91#define SUN3_DMA_DELAY 10
92
93/* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
94#define SUN3_DVMA_BUFSIZE 0xe000
95
96/* minimum number of bytes to do dma on */
97#define SUN3_DMA_MINSIZE 128
98
99static volatile unsigned char *sun3_scsi_regp;
100static volatile struct sun3_dma_regs *dregs;
101#ifdef OLDDMA
102static unsigned char *dmabuf = NULL; /* dma memory buffer */
103#endif
104static unsigned char *sun3_dma_orig_addr = NULL;
105static unsigned long sun3_dma_orig_count = 0;
106static int sun3_dma_active = 0;
107static unsigned long last_residual = 0;
108
109/*
110 * NCR 5380 register access functions
111 */
112
113static inline unsigned char sun3scsi_read(int reg)
114{
115	return( sun3_scsi_regp[reg] );
116}
117
118static inline void sun3scsi_write(int reg, int value)
119{
120	sun3_scsi_regp[reg] = value;
121}
122
123static struct Scsi_Host *default_instance;
124
125/*
126 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
127 *
128 * Purpose : initializes mac NCR5380 driver based on the
129 *	command line / compile time port and irq definitions.
130 *
131 * Inputs : tpnt - template for this SCSI adapter.
132 *
133 * Returns : 1 if a host adapter was found, 0 if not.
134 *
135 */
136
137static int sun3scsi_detect(struct scsi_host_template * tpnt)
138{
139	unsigned long ioaddr, irq = 0;
140	static int called = 0;
141	struct Scsi_Host *instance;
142	int i;
143	unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
144				   IOBASE_SUN3_VMESCSI + 0x4000,
145				   0 };
146	unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
147				  SUN3_VEC_VMESCSI1,
148				  0 };
149	/* check that this machine has an onboard 5380 */
150	switch(idprom->id_machtype) {
151	case SM_SUN3|SM_3_160:
152	case SM_SUN3|SM_3_260:
153		break;
154
155	default:
156		return 0;
157	}
158
159	if(called)
160		return 0;
161
162	tpnt->proc_name = "Sun3 5380 VME SCSI";
163
164	/* setup variables */
165	tpnt->can_queue =
166		(setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
167	tpnt->cmd_per_lun =
168		(setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
169	tpnt->sg_tablesize =
170		(setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
171
172	if (setup_hostid >= 0)
173		tpnt->this_id = setup_hostid;
174	else {
175		/* use 7 as default */
176		tpnt->this_id = 7;
177	}
178
179	ioaddr = 0;
180	for(i = 0; addrs[i] != 0; i++) {
181		unsigned char x;
182
183		ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
184						     SUN3_PAGE_TYPE_VME16);
185		irq = vecs[i];
186		sun3_scsi_regp = (unsigned char *)ioaddr;
187
188		dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
189
190		if(sun3_map_test((unsigned long)dregs, &x)) {
191			unsigned short oldcsr;
192
193			oldcsr = dregs->csr;
194			dregs->csr = 0;
195			udelay(SUN3_DMA_DELAY);
196			if(dregs->csr == 0x1400)
197				break;
198
199			dregs->csr = oldcsr;
200		}
201
202		iounmap((void *)ioaddr);
203		ioaddr = 0;
204	}
205
206	if(!ioaddr)
207		return 0;
208
209#ifdef SUPPORT_TAGS
210	if (setup_use_tagged_queuing < 0)
211		setup_use_tagged_queuing = USE_TAGGED_QUEUING;
212#endif
213
214	instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
215	if(instance == NULL)
216		return 0;
217
218	default_instance = instance;
219
220        instance->io_port = (unsigned long) ioaddr;
221	instance->irq = irq;
222
223	NCR5380_init(instance, 0);
224
225	instance->n_io_port = 32;
226
227        ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
228
229	if (request_irq(instance->irq, scsi_sun3_intr,
230			     0, "Sun3SCSI-5380VME", NULL)) {
231#ifndef REAL_DMA
232		printk("scsi%d: IRQ%d not free, interrupts disabled\n",
233		       instance->host_no, instance->irq);
234		instance->irq = SCSI_IRQ_NONE;
235#else
236		printk("scsi%d: IRQ%d not free, bailing out\n",
237		       instance->host_no, instance->irq);
238		return 0;
239#endif
240	}
241
242	printk("scsi%d: Sun3 5380 VME at port %lX irq", instance->host_no, instance->io_port);
243	if (instance->irq == SCSI_IRQ_NONE)
244		printk ("s disabled");
245	else
246		printk (" %d", instance->irq);
247	printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
248	       instance->can_queue, instance->cmd_per_lun,
249	       SUN3SCSI_PUBLIC_RELEASE);
250	printk("\nscsi%d:", instance->host_no);
251	NCR5380_print_options(instance);
252	printk("\n");
253
254	dregs->csr = 0;
255	udelay(SUN3_DMA_DELAY);
256	dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
257	udelay(SUN3_DMA_DELAY);
258	dregs->fifo_count = 0;
259	dregs->fifo_count_hi = 0;
260	dregs->dma_addr_hi = 0;
261	dregs->dma_addr_lo = 0;
262	dregs->dma_count_hi = 0;
263	dregs->dma_count_lo = 0;
264
265	dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
266
267	called = 1;
268
269#ifdef RESET_BOOT
270	sun3_scsi_reset_boot(instance);
271#endif
272
273	return 1;
274}
275
276int sun3scsi_release (struct Scsi_Host *shpnt)
277{
278	if (shpnt->irq != SCSI_IRQ_NONE)
279		free_irq (shpnt->irq, NULL);
280
281	iounmap((void *)sun3_scsi_regp);
282
283	return 0;
284}
285
286#ifdef RESET_BOOT
287/*
288 * Our 'bus reset on boot' function
289 */
290
291static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
292{
293	unsigned long end;
294
295	NCR5380_local_declare();
296	NCR5380_setup(instance);
297
298	/*
299	 * Do a SCSI reset to clean up the bus during initialization. No
300	 * messing with the queues, interrupts, or locks necessary here.
301	 */
302
303	printk( "Sun3 SCSI: resetting the SCSI bus..." );
304
305	/* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
306//       	sun3_disable_irq( IRQ_SUN3_SCSI );
307
308	/* get in phase */
309	NCR5380_write( TARGET_COMMAND_REG,
310		      PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
311
312	/* assert RST */
313	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
314
315	/* The min. reset hold time is 25us, so 40us should be enough */
316	udelay( 50 );
317
318	/* reset RST and interrupt */
319	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
320	NCR5380_read( RESET_PARITY_INTERRUPT_REG );
321
322	for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
323		barrier();
324
325	/* switch on SCSI IRQ again */
326//       	sun3_enable_irq( IRQ_SUN3_SCSI );
327
328	printk( " done\n" );
329}
330#endif
331
332static const char * sun3scsi_info (struct Scsi_Host *spnt) {
333    return "";
334}
335
336// safe bits for the CSR
337#define CSR_GOOD 0x060f
338
339static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
340{
341	unsigned short csr = dregs->csr;
342	int handled = 0;
343
344	dregs->csr &= ~CSR_DMA_ENABLE;
345
346
347#ifdef SUN3_SCSI_DEBUG
348	printk("scsi_intr csr %x\n", csr);
349#endif
350
351	if(csr & ~CSR_GOOD) {
352		if(csr & CSR_DMA_BUSERR) {
353			printk("scsi%d: bus error in dma\n", default_instance->host_no);
354#ifdef SUN3_SCSI_DEBUG
355			printk("scsi: residual %x count %x addr %p dmaaddr %x\n",
356			       dregs->fifo_count,
357			       dregs->dma_count_lo | (dregs->dma_count_hi << 16),
358			       sun3_dma_orig_addr,
359			       dregs->dma_addr_lo | (dregs->dma_addr_hi << 16));
360#endif
361		}
362
363		if(csr & CSR_DMA_CONFLICT) {
364			printk("scsi%d: dma conflict\n", default_instance->host_no);
365		}
366		handled = 1;
367	}
368
369	if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
370		NCR5380_intr(irq, dummy);
371		handled = 1;
372	}
373
374	return IRQ_RETVAL(handled);
375}
376
377/*
378 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
379 * reentering NCR5380_print_status seems to have ugly side effects
380 */
381
382/* this doesn't seem to get used at all -- sam */
383
384
385/* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
386static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
387{
388	void *addr;
389
390	if(sun3_dma_orig_addr != NULL)
391		dvma_unmap(sun3_dma_orig_addr);
392
393//	addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
394	addr = (void *)dvma_map_vme((unsigned long) data, count);
395
396	sun3_dma_orig_addr = addr;
397	sun3_dma_orig_count = count;
398
399#ifdef SUN3_SCSI_DEBUG
400	printk("scsi: dma_setup addr %p count %x\n", addr, count);
401#endif
402
403//	dregs->fifo_count = 0;
404	/* set direction */
405	if(write_flag)
406		dregs->csr |= CSR_SEND;
407	else
408		dregs->csr &= ~CSR_SEND;
409
410	/* reset fifo */
411//	dregs->csr &= ~CSR_FIFO;
412//	dregs->csr |= CSR_FIFO;
413
414	dregs->csr |= CSR_PACK_ENABLE;
415
416	dregs->dma_addr_hi = ((unsigned long)addr >> 16);
417	dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
418
419	dregs->dma_count_hi = 0;
420	dregs->dma_count_lo = 0;
421	dregs->fifo_count_hi = 0;
422	dregs->fifo_count = 0;
423
424#ifdef SUN3_SCSI_DEBUG
425	printk("scsi: dma_setup done csr %x\n", dregs->csr);
426#endif
427       	return count;
428
429}
430
431static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
432{
433	return last_residual;
434}
435
436static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
437						  struct scsi_cmnd *cmd,
438						  int write_flag)
439{
440	if(blk_fs_request(cmd->request))
441 		return wanted;
442	else
443		return 0;
444}
445
446static int sun3scsi_dma_start(unsigned long count, char *data)
447{
448
449	unsigned short csr;
450
451	csr = dregs->csr;
452#ifdef SUN3_SCSI_DEBUG
453	printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data, count, csr, dregs->fifo_count);
454#endif
455
456	dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
457	dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
458
459	dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
460	dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
461
462//	if(!(csr & CSR_DMA_ENABLE))
463//		dregs->csr |= CSR_DMA_ENABLE;
464
465	return 0;
466}
467
468/* clean up after our dma is done */
469static int sun3scsi_dma_finish(int write_flag)
470{
471	unsigned short fifo;
472	int ret = 0;
473
474	sun3_dma_active = 0;
475
476	dregs->csr &= ~CSR_DMA_ENABLE;
477
478	fifo = dregs->fifo_count;
479	if(write_flag) {
480		if((fifo > 0) && (fifo < sun3_dma_orig_count))
481			fifo++;
482	}
483
484	last_residual = fifo;
485#ifdef SUN3_SCSI_DEBUG
486	printk("scsi: residual %x total %x\n", fifo, sun3_dma_orig_count);
487#endif
488	/* empty bytes from the fifo which didn't make it */
489	if((!write_flag) && (dregs->csr & CSR_LEFT)) {
490		unsigned char *vaddr;
491
492#ifdef SUN3_SCSI_DEBUG
493		printk("scsi: got left over bytes\n");
494#endif
495
496		vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
497
498		vaddr += (sun3_dma_orig_count - fifo);
499		vaddr--;
500
501		switch(dregs->csr & CSR_LEFT) {
502		case CSR_LEFT_3:
503			*vaddr = (dregs->bpack_lo & 0xff00) >> 8;
504			vaddr--;
505
506		case CSR_LEFT_2:
507			*vaddr = (dregs->bpack_hi & 0x00ff);
508			vaddr--;
509
510		case CSR_LEFT_1:
511			*vaddr = (dregs->bpack_hi & 0xff00) >> 8;
512			break;
513		}
514
515
516	}
517
518	dvma_unmap(sun3_dma_orig_addr);
519	sun3_dma_orig_addr = NULL;
520
521	dregs->dma_addr_hi = 0;
522	dregs->dma_addr_lo = 0;
523	dregs->dma_count_hi = 0;
524	dregs->dma_count_lo = 0;
525
526	dregs->fifo_count = 0;
527	dregs->fifo_count_hi = 0;
528
529	dregs->csr &= ~CSR_SEND;
530
531//	dregs->csr |= CSR_DMA_ENABLE;
532
533	sun3_dma_setup_done = NULL;
534
535	return ret;
536
537}
538
539#include "sun3_NCR5380.c"
540
541static struct scsi_host_template driver_template = {
542	.name			= SUN3_SCSI_NAME,
543	.detect			= sun3scsi_detect,
544	.release		= sun3scsi_release,
545	.info			= sun3scsi_info,
546	.queuecommand		= sun3scsi_queue_command,
547	.eh_abort_handler      	= sun3scsi_abort,
548	.eh_bus_reset_handler  	= sun3scsi_bus_reset,
549	.can_queue		= CAN_QUEUE,
550	.this_id		= 7,
551	.sg_tablesize		= SG_TABLESIZE,
552	.cmd_per_lun		= CMD_PER_LUN,
553	.use_clustering		= DISABLE_CLUSTERING
554};
555
556
557#include "scsi_module.c"
558