2 * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
4 * Sun3 DMA routines added by Sam Creasey (sammy@sammy.net)
6 * VME support added by Sam Creasey
8 * TODO: modify this driver to support multiple Sun3 SCSI VME boards
10 * Adapted from mac_scsinew.c:
13 * Generic Macintosh NCR5380 driver
15 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
17 * derived in part from:
20 * Generic Generic NCR5380 driver
22 * Copyright 1995, Russell King
25 #include <linux/types.h>
26 #include <linux/stddef.h>
27 #include <linux/ctype.h>
28 #include <linux/delay.h>
30 #include <linux/module.h>
31 #include <linux/signal.h>
32 #include <linux/ioport.h>
33 #include <linux/init.h>
34 #include <linux/blkdev.h>
38 #include <asm/sun3ints.h>
40 #include <asm/idprom.h>
41 #include <asm/machines.h>
47 #include <scsi/scsi_host.h>
48 #include "sun3_scsi.h"
51 extern int sun3_map_test(unsigned long, char *);
53 /*#define RESET_BOOT */
54 /* #define SUPPORT_TAGS */
59 #define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
63 static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
65 static int setup_can_queue = -1;
66 module_param(setup_can_queue, int, 0);
67 static int setup_cmd_per_lun = -1;
68 module_param(setup_cmd_per_lun, int, 0);
69 static int setup_sg_tablesize = -1;
70 module_param(setup_sg_tablesize, int, 0);
72 static int setup_use_tagged_queuing = -1;
73 module_param(setup_use_tagged_queuing, int, 0);
75 static int setup_hostid = -1;
76 module_param(setup_hostid, int, 0);
78 static struct scsi_cmnd *sun3_dma_setup_done = NULL;
80 #define RESET_RUN_DONE
82 #define AFTER_RESET_DELAY (HZ/2)
84 /* ms to wait after hitting dma regs */
85 #define SUN3_DMA_DELAY 10
87 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
88 #define SUN3_DVMA_BUFSIZE 0xe000
90 /* minimum number of bytes to do dma on */
91 #define SUN3_DMA_MINSIZE 128
93 static volatile unsigned char *sun3_scsi_regp;
94 static volatile struct sun3_dma_regs *dregs;
96 static struct sun3_udc_regs *udc_regs = NULL;
98 static unsigned char *sun3_dma_orig_addr = NULL;
99 static unsigned long sun3_dma_orig_count = 0;
100 static int sun3_dma_active = 0;
101 static unsigned long last_residual = 0;
104 * NCR 5380 register access functions
107 static inline unsigned char sun3scsi_read(int reg)
109 return( sun3_scsi_regp[reg] );
112 static inline void sun3scsi_write(int reg, int value)
114 sun3_scsi_regp[reg] = value;
117 #ifndef SUN3_SCSI_VME
118 /* dma controller register access functions */
120 static inline unsigned short sun3_udc_read(unsigned char reg)
124 dregs->udc_addr = UDC_CSR;
125 udelay(SUN3_DMA_DELAY);
126 ret = dregs->udc_data;
127 udelay(SUN3_DMA_DELAY);
132 static inline void sun3_udc_write(unsigned short val, unsigned char reg)
134 dregs->udc_addr = reg;
135 udelay(SUN3_DMA_DELAY);
136 dregs->udc_data = val;
137 udelay(SUN3_DMA_DELAY);
144 static struct Scsi_Host *default_instance;
147 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
149 * Purpose : initializes mac NCR5380 driver based on the
150 * command line / compile time port and irq definitions.
152 * Inputs : tpnt - template for this SCSI adapter.
154 * Returns : 1 if a host adapter was found, 0 if not.
158 static int __init sun3scsi_detect(struct scsi_host_template *tpnt)
160 unsigned long ioaddr, irq;
161 static int called = 0;
162 struct Scsi_Host *instance;
165 unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
166 IOBASE_SUN3_VMESCSI + 0x4000,
168 unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
173 /* check that this machine has an onboard 5380 */
174 switch(idprom->id_machtype) {
176 case SM_SUN3|SM_3_160:
177 case SM_SUN3|SM_3_260:
180 case SM_SUN3|SM_3_50:
181 case SM_SUN3|SM_3_60:
193 tpnt->proc_name = "Sun3 5380 VME SCSI";
195 tpnt->proc_name = "Sun3 5380 SCSI";
198 /* setup variables */
199 if (setup_can_queue > 0)
200 tpnt->can_queue = setup_can_queue;
201 if (setup_cmd_per_lun > 0)
202 tpnt->cmd_per_lun = setup_cmd_per_lun;
203 if (setup_sg_tablesize >= 0)
204 tpnt->sg_tablesize = setup_sg_tablesize;
206 if (setup_hostid >= 0)
207 tpnt->this_id = setup_hostid;
209 /* use 7 as default */
215 for (i = 0; addrs[i] != 0; i++) {
218 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
219 SUN3_PAGE_TYPE_VME16);
221 sun3_scsi_regp = (unsigned char *)ioaddr;
223 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
225 if (sun3_map_test((unsigned long)dregs, &x)) {
226 unsigned short oldcsr;
230 udelay(SUN3_DMA_DELAY);
231 if (dregs->csr == 0x1400)
237 iounmap((void *)ioaddr);
245 ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
246 sun3_scsi_regp = (unsigned char *)ioaddr;
248 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
250 if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
252 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
257 if (setup_use_tagged_queuing < 0)
258 setup_use_tagged_queuing = 1;
261 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
265 default_instance = instance;
267 instance->io_port = (unsigned long) ioaddr;
270 NCR5380_init(instance, 0);
272 instance->n_io_port = 32;
274 if (request_irq(instance->irq, scsi_sun3_intr,
275 0, "Sun3SCSI-5380", instance)) {
277 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
278 instance->host_no, instance->irq);
279 instance->irq = NO_IRQ;
281 printk("scsi%d: IRQ%d not free, bailing out\n",
282 instance->host_no, instance->irq);
288 udelay(SUN3_DMA_DELAY);
289 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
290 udelay(SUN3_DMA_DELAY);
291 dregs->fifo_count = 0;
293 dregs->fifo_count_hi = 0;
294 dregs->dma_addr_hi = 0;
295 dregs->dma_addr_lo = 0;
296 dregs->dma_count_hi = 0;
297 dregs->dma_count_lo = 0;
299 dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
305 sun3_scsi_reset_boot(instance);
311 static int sun3scsi_release(struct Scsi_Host *shpnt)
313 if (shpnt->irq != NO_IRQ)
314 free_irq(shpnt->irq, shpnt);
316 iounmap((void *)sun3_scsi_regp);
324 * Our 'bus reset on boot' function
327 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
331 NCR5380_local_declare();
332 NCR5380_setup(instance);
335 * Do a SCSI reset to clean up the bus during initialization. No
336 * messing with the queues, interrupts, or locks necessary here.
339 printk( "Sun3 SCSI: resetting the SCSI bus..." );
341 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
342 // sun3_disable_irq( IRQ_SUN3_SCSI );
345 NCR5380_write( TARGET_COMMAND_REG,
346 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
349 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
351 /* The min. reset hold time is 25us, so 40us should be enough */
354 /* reset RST and interrupt */
355 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
356 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
358 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
361 /* switch on SCSI IRQ again */
362 // sun3_enable_irq( IRQ_SUN3_SCSI );
368 // safe bits for the CSR
369 #define CSR_GOOD 0x060f
371 static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
373 unsigned short csr = dregs->csr;
377 dregs->csr &= ~CSR_DMA_ENABLE;
380 if(csr & ~CSR_GOOD) {
381 if(csr & CSR_DMA_BUSERR) {
382 printk("scsi%d: bus error in dma\n", default_instance->host_no);
385 if(csr & CSR_DMA_CONFLICT) {
386 printk("scsi%d: dma conflict\n", default_instance->host_no);
391 if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
392 NCR5380_intr(irq, dummy);
396 return IRQ_RETVAL(handled);
400 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
401 * reentering NCR5380_print_status seems to have ugly side effects
404 /* this doesn't seem to get used at all -- sam */
406 void sun3_sun3_debug (void)
409 NCR5380_local_declare();
411 if (default_instance) {
412 local_irq_save(flags);
413 NCR5380_print_status(default_instance);
414 local_irq_restore(flags);
420 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
421 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
425 if(sun3_dma_orig_addr != NULL)
426 dvma_unmap(sun3_dma_orig_addr);
429 addr = (void *)dvma_map_vme((unsigned long) data, count);
431 addr = (void *)dvma_map((unsigned long) data, count);
434 sun3_dma_orig_addr = addr;
435 sun3_dma_orig_count = count;
437 #ifndef SUN3_SCSI_VME
438 dregs->fifo_count = 0;
439 sun3_udc_write(UDC_RESET, UDC_CSR);
442 dregs->csr &= ~CSR_FIFO;
443 dregs->csr |= CSR_FIFO;
448 dregs->csr |= CSR_SEND;
450 dregs->csr &= ~CSR_SEND;
453 dregs->csr |= CSR_PACK_ENABLE;
455 dregs->dma_addr_hi = ((unsigned long)addr >> 16);
456 dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
458 dregs->dma_count_hi = 0;
459 dregs->dma_count_lo = 0;
460 dregs->fifo_count_hi = 0;
461 dregs->fifo_count = 0;
463 /* byte count for fifo */
464 dregs->fifo_count = count;
466 sun3_udc_write(UDC_RESET, UDC_CSR);
469 dregs->csr &= ~CSR_FIFO;
470 dregs->csr |= CSR_FIFO;
472 if(dregs->fifo_count != count) {
473 printk("scsi%d: fifo_mismatch %04x not %04x\n",
474 default_instance->host_no, dregs->fifo_count,
475 (unsigned int) count);
476 NCR5380_dprint(NDEBUG_DMA, default_instance);
480 udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
481 udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
482 udc_regs->count = count/2; /* count in words */
483 udc_regs->mode_hi = UDC_MODE_HIWORD;
487 udc_regs->mode_lo = UDC_MODE_LSEND;
488 udc_regs->rsel = UDC_RSEL_SEND;
490 udc_regs->mode_lo = UDC_MODE_LRECV;
491 udc_regs->rsel = UDC_RSEL_RECV;
494 /* announce location of regs block */
495 sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
498 sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
500 /* set dma master on */
501 sun3_udc_write(0xd, UDC_MODE);
503 /* interrupt enable */
504 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
511 #ifndef SUN3_SCSI_VME
512 static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
514 unsigned short resid;
516 dregs->udc_addr = 0x32;
517 udelay(SUN3_DMA_DELAY);
518 resid = dregs->udc_data;
519 udelay(SUN3_DMA_DELAY);
522 return (unsigned long) resid;
526 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
528 return last_residual;
531 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
532 struct scsi_cmnd *cmd,
535 if (cmd->request->cmd_type == REQ_TYPE_FS)
541 static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
548 dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
549 dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
551 dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
552 dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
554 /* if(!(csr & CSR_DMA_ENABLE))
555 * dregs->csr |= CSR_DMA_ENABLE;
558 sun3_udc_write(UDC_CHN_START, UDC_CSR);
564 /* clean up after our dma is done */
565 static int sun3scsi_dma_finish(int write_flag)
567 unsigned short __maybe_unused count;
574 dregs->csr &= ~CSR_DMA_ENABLE;
576 fifo = dregs->fifo_count;
578 if ((fifo > 0) && (fifo < sun3_dma_orig_count))
582 last_residual = fifo;
583 /* empty bytes from the fifo which didn't make it */
584 if ((!write_flag) && (dregs->csr & CSR_LEFT)) {
585 unsigned char *vaddr;
587 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
589 vaddr += (sun3_dma_orig_count - fifo);
592 switch (dregs->csr & CSR_LEFT) {
594 *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
598 *vaddr = (dregs->bpack_hi & 0x00ff);
602 *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
607 // check to empty the fifo on a read
609 int tmo = 20000; /* .2 sec */
612 if(dregs->csr & CSR_FIFO_EMPTY)
616 printk("sun3scsi: fifo failed to empty!\n");
623 count = sun3scsi_dma_count(default_instance);
625 fifo = dregs->fifo_count;
626 last_residual = fifo;
628 /* empty bytes from the fifo which didn't make it */
629 if((!write_flag) && (count - fifo) == 2) {
631 unsigned char *vaddr;
633 data = dregs->fifo_data;
634 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
636 vaddr += (sun3_dma_orig_count - fifo);
638 vaddr[-2] = (data & 0xff00) >> 8;
639 vaddr[-1] = (data & 0xff);
643 dvma_unmap(sun3_dma_orig_addr);
644 sun3_dma_orig_addr = NULL;
647 dregs->dma_addr_hi = 0;
648 dregs->dma_addr_lo = 0;
649 dregs->dma_count_hi = 0;
650 dregs->dma_count_lo = 0;
652 dregs->fifo_count = 0;
653 dregs->fifo_count_hi = 0;
655 dregs->csr &= ~CSR_SEND;
656 /* dregs->csr |= CSR_DMA_ENABLE; */
658 sun3_udc_write(UDC_RESET, UDC_CSR);
659 dregs->fifo_count = 0;
660 dregs->csr &= ~CSR_SEND;
663 dregs->csr &= ~CSR_FIFO;
664 dregs->csr |= CSR_FIFO;
667 sun3_dma_setup_done = NULL;
673 #include "sun3_NCR5380.c"
675 static struct scsi_host_template driver_template = {
676 .show_info = sun3scsi_show_info,
677 .name = SUN3_SCSI_NAME,
678 .detect = sun3scsi_detect,
679 .release = sun3scsi_release,
680 .info = sun3scsi_info,
681 .queuecommand = sun3scsi_queue_command,
682 .eh_abort_handler = sun3scsi_abort,
683 .eh_bus_reset_handler = sun3scsi_bus_reset,
686 .sg_tablesize = SG_NONE,
688 .use_clustering = DISABLE_CLUSTERING
692 #include "scsi_module.c"
694 MODULE_LICENSE("GPL");