2 * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
4 * Sun3 DMA routines added by Sam Creasey (sammy@oh.verio.com)
6 * Adapted from mac_scsinew.c:
9 * Generic Macintosh NCR5380 driver
11 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
13 * derived in part from:
16 * Generic Generic NCR5380 driver
18 * Copyright 1995, Russell King
22 * For more information, please consult
25 * SCSI Protocol Controller
28 * NCR Microelectronics
29 * 1635 Aeroplaza Drive
30 * Colorado Springs, CO 80916
37 * This is from mac_scsi.h, but hey, maybe this is useful for Sun3 too! :)
41 * PARITY - enable parity checking. Not supported.
43 * SCSI2 - enable support for SCSI-II tagged queueing. Untested.
45 * USLEEP - enable support for devices that don't disconnect. Untested.
49 * $Log: sun3_scsi.c,v $
50 * Revision 1.1.1.1 2005/04/11 02:50:37 jack
53 * Revision 1.1.1.1 2005/01/10 13:16:36 jack
60 #include <linux/types.h>
61 #include <linux/stddef.h>
62 #include <linux/ctype.h>
63 #include <linux/delay.h>
65 #include <linux/module.h>
66 #include <linux/signal.h>
67 #include <linux/sched.h>
68 #include <linux/ioport.h>
69 #include <linux/init.h>
70 #include <linux/blk.h>
73 #include <asm/system.h>
75 #include <asm/sun3ints.h>
77 #include <asm/idprom.h>
78 #include <asm/machines.h>
85 #include "sun3_scsi.h"
87 #include "constants.h"
92 /*#define RESET_BOOT */
98 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
105 /* #define SUPPORT_TAGS */
107 #define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
110 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp);
111 static inline unsigned char sun3scsi_read(int reg);
112 static inline void sun3scsi_write(int reg, int value);
114 static int setup_can_queue = -1;
115 static int setup_cmd_per_lun = -1;
116 static int setup_sg_tablesize = -1;
118 static int setup_use_tagged_queuing = -1;
120 static int setup_hostid = -1;
122 static Scsi_Cmnd *sun3_dma_setup_done = NULL;
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;
138 static unsigned char *dmabuf = NULL; /* dma memory buffer */
140 static struct sun3_udc_regs *udc_regs = NULL;
141 static unsigned char *sun3_dma_orig_addr = NULL;
142 static unsigned long sun3_dma_orig_count = 0;
143 static int sun3_dma_active = 0;
144 static unsigned long last_residual = 0;
147 * NCR 5380 register access functions
150 static inline unsigned char sun3scsi_read(int reg)
152 return( sun3_scsi_regp[reg] );
155 static inline void sun3scsi_write(int reg, int value)
157 sun3_scsi_regp[reg] = value;
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);
185 static struct Scsi_Host *default_instance;
188 * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
190 * Purpose : initializes mac NCR5380 driver based on the
191 * command line / compile time port and irq definitions.
193 * Inputs : tpnt - template for this SCSI adapter.
195 * Returns : 1 if a host adapter was found, 0 if not.
199 int sun3scsi_detect(Scsi_Host_Template * tpnt)
201 unsigned long ioaddr, iopte;
203 static int called = 0;
204 struct Scsi_Host *instance;
206 /* check that this machine has an onboard 5380 */
207 switch(idprom->id_machtype) {
208 case SM_SUN3|SM_3_50:
209 case SM_SUN3|SM_3_60:
219 tpnt->proc_name = "Sun3 5380 SCSI";
221 /* setup variables */
223 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
225 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
227 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
229 if (setup_hostid >= 0)
230 tpnt->this_id = setup_hostid;
232 /* use 7 as default */
236 /* Taken from Sammy's lance driver: */
237 /* IOBASE_SUN3_SCSI can be found within the IO pmeg with some effort */
238 for(ioaddr = 0xfe00000; ioaddr < (0xfe00000 + SUN3_PMEG_SIZE);
239 ioaddr += SUN3_PTE_SIZE) {
241 iopte = sun3_get_pte(ioaddr);
242 if(!(iopte & SUN3_PAGE_TYPE_IO)) /* this an io page? */
245 if(((iopte & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT) ==
253 printk("No Sun3 NCR5380 found!\n");
257 sun3_scsi_regp = (unsigned char *)ioaddr;
258 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
260 if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
262 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
266 if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
267 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
272 if (setup_use_tagged_queuing < 0)
273 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
276 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
280 default_instance = instance;
282 instance->io_port = (unsigned long) ioaddr;
283 instance->irq = IRQ_SUN3_SCSI;
285 NCR5380_init(instance, 0);
287 instance->n_io_port = 32;
289 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
291 if (request_irq(instance->irq, scsi_sun3_intr,
292 0, "Sun3SCSI-5380", NULL)) {
294 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
295 instance->host_no, instance->irq);
296 instance->irq = IRQ_NONE;
298 printk("scsi%d: IRQ%d not free, bailing out\n",
299 instance->host_no, instance->irq);
304 printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
305 if (instance->irq == IRQ_NONE)
306 printk ("s disabled");
308 printk (" %d", instance->irq);
309 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
310 instance->can_queue, instance->cmd_per_lun,
311 SUN3SCSI_PUBLIC_RELEASE);
312 printk("\nscsi%d:", instance->host_no);
313 NCR5380_print_options(instance);
317 udelay(SUN3_DMA_DELAY);
318 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
319 udelay(SUN3_DMA_DELAY);
320 dregs->fifo_count = 0;
325 sun3_scsi_reset_boot(instance);
331 int sun3scsi_release (struct Scsi_Host *shpnt)
333 if (shpnt->irq != IRQ_NONE)
334 free_irq (shpnt->irq, NULL);
341 * Our 'bus reset on boot' function
344 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
348 NCR5380_local_declare();
349 NCR5380_setup(instance);
352 * Do a SCSI reset to clean up the bus during initialization. No
353 * messing with the queues, interrupts, or locks necessary here.
356 printk( "Sun3 SCSI: resetting the SCSI bus..." );
358 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
359 // sun3_disable_irq( IRQ_SUN3_SCSI );
362 NCR5380_write( TARGET_COMMAND_REG,
363 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
366 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
368 /* The min. reset hold time is 25us, so 40us should be enough */
371 /* reset RST and interrupt */
372 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
373 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
375 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
378 /* switch on SCSI IRQ again */
379 // sun3_enable_irq( IRQ_SUN3_SCSI );
385 const char * sun3scsi_info (struct Scsi_Host *spnt) {
389 // safe bits for the CSR
390 #define CSR_GOOD 0x060f
392 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
394 unsigned short csr = dregs->csr;
396 if(csr & ~CSR_GOOD) {
397 if(csr & CSR_DMA_BUSERR) {
398 printk("scsi%d: bus error in dma\n", default_instance->host_no);
401 if(csr & CSR_DMA_CONFLICT) {
402 printk("scsi%d: dma conflict\n", default_instance->host_no);
406 if(csr & (CSR_SDB_INT | CSR_DMA_INT))
407 NCR5380_intr(irq, dummy, fp);
411 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
412 * reentering NCR5380_print_status seems to have ugly side effects
415 /* this doesn't seem to get used at all -- sam */
417 void sun3_sun3_debug (void)
420 NCR5380_local_declare();
422 if (default_instance) {
425 NCR5380_print_status(default_instance);
426 restore_flags(flags);
432 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
433 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
437 memcpy(dmabuf, data, count);
439 sun3_dma_orig_addr = data;
440 sun3_dma_orig_count = count;
445 if(sun3_dma_orig_addr != NULL)
446 dvma_unmap(sun3_dma_orig_addr);
448 // addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
449 addr = (void *)dvma_map((unsigned long) data, count);
451 sun3_dma_orig_addr = addr;
452 sun3_dma_orig_count = count;
454 dregs->fifo_count = 0;
455 sun3_udc_write(UDC_RESET, UDC_CSR);
458 dregs->csr &= ~CSR_FIFO;
459 dregs->csr |= CSR_FIFO;
463 dregs->csr |= CSR_SEND;
465 dregs->csr &= ~CSR_SEND;
467 /* byte count for fifo */
468 dregs->fifo_count = count;
470 sun3_udc_write(UDC_RESET, UDC_CSR);
473 dregs->csr &= ~CSR_FIFO;
474 dregs->csr |= CSR_FIFO;
476 if(dregs->fifo_count != count) {
477 printk("scsi%d: fifo_mismatch %04x not %04x\n",
478 default_instance->host_no, dregs->fifo_count,
479 (unsigned int) count);
480 NCR5380_print(default_instance);
485 udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
486 udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
488 udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
489 udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
491 udc_regs->count = count/2; /* count in words */
492 udc_regs->mode_hi = UDC_MODE_HIWORD;
496 udc_regs->mode_lo = UDC_MODE_LSEND;
497 udc_regs->rsel = UDC_RSEL_SEND;
499 udc_regs->mode_lo = UDC_MODE_LRECV;
500 udc_regs->rsel = UDC_RSEL_RECV;
503 /* announce location of regs block */
504 sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
507 sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
509 /* set dma master on */
510 sun3_udc_write(0xd, UDC_MODE);
512 /* interrupt enable */
513 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
519 static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
521 unsigned short resid;
523 dregs->udc_addr = 0x32;
524 udelay(SUN3_DMA_DELAY);
525 resid = dregs->udc_data;
526 udelay(SUN3_DMA_DELAY);
529 return (unsigned long) resid;
532 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
534 return last_residual;
537 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
540 if((cmd->request.cmd == 0) || (cmd->request.cmd == 1))
546 /* clean up after our dma is done */
547 static int sun3scsi_dma_finish(int write_flag)
549 unsigned short count;
555 // check to empty the fifo on a read
557 int tmo = 20000; /* .2 sec */
560 if(dregs->csr & CSR_FIFO_EMPTY)
572 count = sun3scsi_dma_count(default_instance);
575 /* if we've finished a read, copy out the data we read */
576 if(sun3_dma_orig_addr) {
577 /* check for residual bytes after dma end */
578 if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
579 (BASR_PHASE_MATCH | BASR_ACK))) {
580 printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
581 printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG));
585 /* copy in what we dma'd no matter what */
586 memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
587 sun3_dma_orig_addr = NULL;
592 fifo = dregs->fifo_count;
593 last_residual = fifo;
595 /* empty bytes from the fifo which didn't make it */
596 if((!write_flag) && (count - fifo) == 2) {
598 unsigned char *vaddr;
600 data = dregs->fifo_data;
601 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
603 vaddr += (sun3_dma_orig_count - fifo);
605 vaddr[-2] = (data & 0xff00) >> 8;
606 vaddr[-1] = (data & 0xff);
609 dvma_unmap(sun3_dma_orig_addr);
610 sun3_dma_orig_addr = NULL;
612 sun3_udc_write(UDC_RESET, UDC_CSR);
613 dregs->fifo_count = 0;
614 dregs->csr &= ~CSR_SEND;
617 dregs->csr &= ~CSR_FIFO;
618 dregs->csr |= CSR_FIFO;
620 sun3_dma_setup_done = NULL;
626 #include "sun3_NCR5380.c"
628 static Scsi_Host_Template driver_template = SUN3_NCR5380;
630 #include "scsi_module.c"
632 MODULE_LICENSE("GPL");