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
17 * derived in part from:
20 * Generic Generic NCR5380 driver
22 * Copyright 1995, Russell King
26 * For more information, please consult
29 * SCSI Protocol Controller
32 * NCR Microelectronics
33 * 1635 Aeroplaza Drive
34 * Colorado Springs, CO 80916
41 * This is from mac_scsi.h, but hey, maybe this is useful for Sun3 too! :)
45 * PARITY - enable parity checking. Not supported.
47 * SCSI2 - enable support for SCSI-II tagged queueing. Untested.
49 * USLEEP - enable support for devices that don't disconnect. Untested.
54 #include <linux/types.h>
55 #include <linux/stddef.h>
56 #include <linux/ctype.h>
57 #include <linux/delay.h>
59 #include <linux/module.h>
60 #include <linux/signal.h>
61 #include <linux/ioport.h>
62 #include <linux/init.h>
63 #include <linux/blkdev.h>
67 #include <asm/sun3ints.h>
69 #include <asm/idprom.h>
70 #include <asm/machines.h>
76 #include <scsi/scsi_host.h>
77 #include "sun3_scsi.h"
80 extern int sun3_map_test(unsigned long, char *);
83 /*#define RESET_BOOT */
87 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
94 /* #define SUPPORT_TAGS */
99 #define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
103 static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
104 static inline unsigned char sun3scsi_read(int reg);
105 static inline void sun3scsi_write(int reg, int value);
107 static int setup_can_queue = -1;
108 module_param(setup_can_queue, int, 0);
109 static int setup_cmd_per_lun = -1;
110 module_param(setup_cmd_per_lun, int, 0);
111 static int setup_sg_tablesize = -1;
112 module_param(setup_sg_tablesize, int, 0);
114 static int setup_use_tagged_queuing = -1;
115 module_param(setup_use_tagged_queuing, int, 0);
117 static int setup_hostid = -1;
118 module_param(setup_hostid, int, 0);
120 static struct scsi_cmnd *sun3_dma_setup_done = NULL;
122 #define RESET_RUN_DONE
124 #define AFTER_RESET_DELAY (HZ/2)
126 /* ms to wait after hitting dma regs */
127 #define SUN3_DMA_DELAY 10
129 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
130 #define SUN3_DVMA_BUFSIZE 0xe000
132 /* minimum number of bytes to do dma on */
133 #define SUN3_DMA_MINSIZE 128
135 static volatile unsigned char *sun3_scsi_regp;
136 static volatile struct sun3_dma_regs *dregs;
137 #ifndef SUN3_SCSI_VME
138 static struct sun3_udc_regs *udc_regs = NULL;
140 static unsigned char *sun3_dma_orig_addr = NULL;
141 static unsigned long sun3_dma_orig_count = 0;
142 static int sun3_dma_active = 0;
143 static unsigned long last_residual = 0;
146 * NCR 5380 register access functions
149 static inline unsigned char sun3scsi_read(int reg)
151 return( sun3_scsi_regp[reg] );
154 static inline void sun3scsi_write(int reg, int value)
156 sun3_scsi_regp[reg] = value;
159 #ifndef SUN3_SCSI_VME
160 /* dma controller register access functions */
162 static inline unsigned short sun3_udc_read(unsigned char reg)
166 dregs->udc_addr = UDC_CSR;
167 udelay(SUN3_DMA_DELAY);
168 ret = dregs->udc_data;
169 udelay(SUN3_DMA_DELAY);
174 static inline void sun3_udc_write(unsigned short val, unsigned char reg)
176 dregs->udc_addr = reg;
177 udelay(SUN3_DMA_DELAY);
178 dregs->udc_data = val;
179 udelay(SUN3_DMA_DELAY);
186 static struct Scsi_Host *default_instance;
189 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
191 * Purpose : initializes mac NCR5380 driver based on the
192 * command line / compile time port and irq definitions.
194 * Inputs : tpnt - template for this SCSI adapter.
196 * Returns : 1 if a host adapter was found, 0 if not.
200 static int __init sun3scsi_detect(struct scsi_host_template *tpnt)
202 unsigned long ioaddr, irq;
203 static int called = 0;
204 struct Scsi_Host *instance;
207 unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
208 IOBASE_SUN3_VMESCSI + 0x4000,
210 unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
215 /* check that this machine has an onboard 5380 */
216 switch(idprom->id_machtype) {
218 case SM_SUN3|SM_3_160:
219 case SM_SUN3|SM_3_260:
222 case SM_SUN3|SM_3_50:
223 case SM_SUN3|SM_3_60:
235 tpnt->proc_name = "Sun3 5380 VME SCSI";
237 tpnt->proc_name = "Sun3 5380 SCSI";
240 /* setup variables */
242 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
244 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
246 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
248 if (setup_hostid >= 0)
249 tpnt->this_id = setup_hostid;
251 /* use 7 as default */
257 for (i = 0; addrs[i] != 0; i++) {
260 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
261 SUN3_PAGE_TYPE_VME16);
263 sun3_scsi_regp = (unsigned char *)ioaddr;
265 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
267 if (sun3_map_test((unsigned long)dregs, &x)) {
268 unsigned short oldcsr;
272 udelay(SUN3_DMA_DELAY);
273 if (dregs->csr == 0x1400)
279 iounmap((void *)ioaddr);
287 ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
288 sun3_scsi_regp = (unsigned char *)ioaddr;
290 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
292 if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
294 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
299 if (setup_use_tagged_queuing < 0)
300 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
303 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
307 default_instance = instance;
309 instance->io_port = (unsigned long) ioaddr;
312 NCR5380_init(instance, 0);
314 instance->n_io_port = 32;
316 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
318 if (request_irq(instance->irq, scsi_sun3_intr,
319 0, "Sun3SCSI-5380", instance)) {
321 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
322 instance->host_no, instance->irq);
323 instance->irq = SCSI_IRQ_NONE;
325 printk("scsi%d: IRQ%d not free, bailing out\n",
326 instance->host_no, instance->irq);
331 pr_info("scsi%d: %s at port %lX irq", instance->host_no,
332 tpnt->proc_name, instance->io_port);
333 if (instance->irq == SCSI_IRQ_NONE)
334 printk ("s disabled");
336 printk (" %d", instance->irq);
337 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
338 instance->can_queue, instance->cmd_per_lun,
339 SUN3SCSI_PUBLIC_RELEASE);
340 printk("\nscsi%d:", instance->host_no);
341 NCR5380_print_options(instance);
345 udelay(SUN3_DMA_DELAY);
346 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
347 udelay(SUN3_DMA_DELAY);
348 dregs->fifo_count = 0;
350 dregs->fifo_count_hi = 0;
351 dregs->dma_addr_hi = 0;
352 dregs->dma_addr_lo = 0;
353 dregs->dma_count_hi = 0;
354 dregs->dma_count_lo = 0;
356 dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
362 sun3_scsi_reset_boot(instance);
368 int sun3scsi_release (struct Scsi_Host *shpnt)
370 if (shpnt->irq != SCSI_IRQ_NONE)
371 free_irq(shpnt->irq, shpnt);
373 iounmap((void *)sun3_scsi_regp);
381 * Our 'bus reset on boot' function
384 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
388 NCR5380_local_declare();
389 NCR5380_setup(instance);
392 * Do a SCSI reset to clean up the bus during initialization. No
393 * messing with the queues, interrupts, or locks necessary here.
396 printk( "Sun3 SCSI: resetting the SCSI bus..." );
398 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
399 // sun3_disable_irq( IRQ_SUN3_SCSI );
402 NCR5380_write( TARGET_COMMAND_REG,
403 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
406 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
408 /* The min. reset hold time is 25us, so 40us should be enough */
411 /* reset RST and interrupt */
412 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
413 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
415 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
418 /* switch on SCSI IRQ again */
419 // sun3_enable_irq( IRQ_SUN3_SCSI );
425 static const char *sun3scsi_info(struct Scsi_Host *spnt)
430 // safe bits for the CSR
431 #define CSR_GOOD 0x060f
433 static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
435 unsigned short csr = dregs->csr;
439 dregs->csr &= ~CSR_DMA_ENABLE;
442 if(csr & ~CSR_GOOD) {
443 if(csr & CSR_DMA_BUSERR) {
444 printk("scsi%d: bus error in dma\n", default_instance->host_no);
447 if(csr & CSR_DMA_CONFLICT) {
448 printk("scsi%d: dma conflict\n", default_instance->host_no);
453 if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
454 NCR5380_intr(irq, dummy);
458 return IRQ_RETVAL(handled);
462 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
463 * reentering NCR5380_print_status seems to have ugly side effects
466 /* this doesn't seem to get used at all -- sam */
468 void sun3_sun3_debug (void)
471 NCR5380_local_declare();
473 if (default_instance) {
474 local_irq_save(flags);
475 NCR5380_print_status(default_instance);
476 local_irq_restore(flags);
482 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
483 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
487 if(sun3_dma_orig_addr != NULL)
488 dvma_unmap(sun3_dma_orig_addr);
491 addr = (void *)dvma_map_vme((unsigned long) data, count);
493 addr = (void *)dvma_map((unsigned long) data, count);
496 sun3_dma_orig_addr = addr;
497 sun3_dma_orig_count = count;
499 #ifndef SUN3_SCSI_VME
500 dregs->fifo_count = 0;
501 sun3_udc_write(UDC_RESET, UDC_CSR);
504 dregs->csr &= ~CSR_FIFO;
505 dregs->csr |= CSR_FIFO;
510 dregs->csr |= CSR_SEND;
512 dregs->csr &= ~CSR_SEND;
515 dregs->csr |= CSR_PACK_ENABLE;
517 dregs->dma_addr_hi = ((unsigned long)addr >> 16);
518 dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
520 dregs->dma_count_hi = 0;
521 dregs->dma_count_lo = 0;
522 dregs->fifo_count_hi = 0;
523 dregs->fifo_count = 0;
525 /* byte count for fifo */
526 dregs->fifo_count = count;
528 sun3_udc_write(UDC_RESET, UDC_CSR);
531 dregs->csr &= ~CSR_FIFO;
532 dregs->csr |= CSR_FIFO;
534 if(dregs->fifo_count != count) {
535 printk("scsi%d: fifo_mismatch %04x not %04x\n",
536 default_instance->host_no, dregs->fifo_count,
537 (unsigned int) count);
538 NCR5380_dprint(NDEBUG_DMA, default_instance);
542 udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
543 udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
544 udc_regs->count = count/2; /* count in words */
545 udc_regs->mode_hi = UDC_MODE_HIWORD;
549 udc_regs->mode_lo = UDC_MODE_LSEND;
550 udc_regs->rsel = UDC_RSEL_SEND;
552 udc_regs->mode_lo = UDC_MODE_LRECV;
553 udc_regs->rsel = UDC_RSEL_RECV;
556 /* announce location of regs block */
557 sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
560 sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
562 /* set dma master on */
563 sun3_udc_write(0xd, UDC_MODE);
565 /* interrupt enable */
566 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
573 #ifndef SUN3_SCSI_VME
574 static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
576 unsigned short resid;
578 dregs->udc_addr = 0x32;
579 udelay(SUN3_DMA_DELAY);
580 resid = dregs->udc_data;
581 udelay(SUN3_DMA_DELAY);
584 return (unsigned long) resid;
588 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
590 return last_residual;
593 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
594 struct scsi_cmnd *cmd,
597 if (cmd->request->cmd_type == REQ_TYPE_FS)
603 static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
610 dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
611 dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
613 dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
614 dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
616 /* if(!(csr & CSR_DMA_ENABLE))
617 * dregs->csr |= CSR_DMA_ENABLE;
620 sun3_udc_write(UDC_CHN_START, UDC_CSR);
626 /* clean up after our dma is done */
627 static int sun3scsi_dma_finish(int write_flag)
629 unsigned short __maybe_unused count;
636 dregs->csr &= ~CSR_DMA_ENABLE;
638 fifo = dregs->fifo_count;
640 if ((fifo > 0) && (fifo < sun3_dma_orig_count))
644 last_residual = fifo;
645 /* empty bytes from the fifo which didn't make it */
646 if ((!write_flag) && (dregs->csr & CSR_LEFT)) {
647 unsigned char *vaddr;
649 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
651 vaddr += (sun3_dma_orig_count - fifo);
654 switch (dregs->csr & CSR_LEFT) {
656 *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
660 *vaddr = (dregs->bpack_hi & 0x00ff);
664 *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
669 // check to empty the fifo on a read
671 int tmo = 20000; /* .2 sec */
674 if(dregs->csr & CSR_FIFO_EMPTY)
678 printk("sun3scsi: fifo failed to empty!\n");
685 count = sun3scsi_dma_count(default_instance);
687 fifo = dregs->fifo_count;
688 last_residual = fifo;
690 /* empty bytes from the fifo which didn't make it */
691 if((!write_flag) && (count - fifo) == 2) {
693 unsigned char *vaddr;
695 data = dregs->fifo_data;
696 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
698 vaddr += (sun3_dma_orig_count - fifo);
700 vaddr[-2] = (data & 0xff00) >> 8;
701 vaddr[-1] = (data & 0xff);
705 dvma_unmap(sun3_dma_orig_addr);
706 sun3_dma_orig_addr = NULL;
709 dregs->dma_addr_hi = 0;
710 dregs->dma_addr_lo = 0;
711 dregs->dma_count_hi = 0;
712 dregs->dma_count_lo = 0;
714 dregs->fifo_count = 0;
715 dregs->fifo_count_hi = 0;
717 dregs->csr &= ~CSR_SEND;
718 /* dregs->csr |= CSR_DMA_ENABLE; */
720 sun3_udc_write(UDC_RESET, UDC_CSR);
721 dregs->fifo_count = 0;
722 dregs->csr &= ~CSR_SEND;
725 dregs->csr &= ~CSR_FIFO;
726 dregs->csr |= CSR_FIFO;
729 sun3_dma_setup_done = NULL;
735 #include "sun3_NCR5380.c"
737 static struct scsi_host_template driver_template = {
738 .show_info = sun3scsi_show_info,
739 .name = SUN3_SCSI_NAME,
740 .detect = sun3scsi_detect,
741 .release = sun3scsi_release,
742 .info = sun3scsi_info,
743 .queuecommand = sun3scsi_queue_command,
744 .eh_abort_handler = sun3scsi_abort,
745 .eh_bus_reset_handler = sun3scsi_bus_reset,
746 .can_queue = CAN_QUEUE,
748 .sg_tablesize = SG_TABLESIZE,
749 .cmd_per_lun = CMD_PER_LUN,
750 .use_clustering = DISABLE_CLUSTERING
754 #include "scsi_module.c"
756 MODULE_LICENSE("GPL");