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 * Adapted from sun3_scsi.c -- see there for other headers
10 * TODO: modify this driver to support multiple Sun3 SCSI VME boards
16 #include <linux/types.h>
17 #include <linux/stddef.h>
18 #include <linux/ctype.h>
19 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/signal.h>
23 #include <linux/sched.h>
24 #include <linux/ioport.h>
25 #include <linux/init.h>
26 #include <linux/blk.h>
29 #include <asm/system.h>
31 #include <asm/sun3ints.h>
33 #include <asm/idprom.h>
34 #include <asm/machines.h>
38 #undef SUN3_SCSI_DEBUG
45 #include "sun3_scsi.h"
47 #include "constants.h"
49 extern int sun3_map_test(unsigned long, char *);
52 /*#define RESET_BOOT */
58 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
65 /* #define SUPPORT_TAGS */
67 //#define ENABLE_IRQ() enable_irq( SUN3_VEC_VMESCSI0 );
71 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp);
72 static inline unsigned char sun3scsi_read(int reg);
73 static inline void sun3scsi_write(int reg, int value);
75 static int setup_can_queue = -1;
76 MODULE_PARM(setup_can_queue, "i");
77 static int setup_cmd_per_lun = -1;
78 MODULE_PARM(setup_cmd_per_lun, "i");
79 static int setup_sg_tablesize = -1;
80 MODULE_PARM(setup_sg_tablesize, "i");
82 static int setup_use_tagged_queuing = -1;
83 MODULE_PARM(setup_use_tagged_queuing, "i");
85 static int setup_hostid = -1;
86 MODULE_PARM(setup_hostid, "i");
88 static Scsi_Cmnd *sun3_dma_setup_done = NULL;
90 #define AFTER_RESET_DELAY (HZ/2)
92 /* ms to wait after hitting dma regs */
93 #define SUN3_DMA_DELAY 10
95 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
96 #define SUN3_DVMA_BUFSIZE 0xe000
98 /* minimum number of bytes to do dma on */
99 #define SUN3_DMA_MINSIZE 128
101 static volatile unsigned char *sun3_scsi_regp;
102 static volatile struct sun3_dma_regs *dregs;
104 static unsigned char *dmabuf = NULL; /* dma memory buffer */
106 static unsigned char *sun3_dma_orig_addr = NULL;
107 static unsigned long sun3_dma_orig_count = 0;
108 static int sun3_dma_active = 0;
109 static unsigned long last_residual = 0;
112 * NCR 5380 register access functions
115 static inline unsigned char sun3scsi_read(int reg)
117 return( sun3_scsi_regp[reg] );
120 static inline void sun3scsi_write(int reg, int value)
122 sun3_scsi_regp[reg] = value;
128 static struct Scsi_Host *default_instance;
131 * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
133 * Purpose : initializes mac NCR5380 driver based on the
134 * command line / compile time port and irq definitions.
136 * Inputs : tpnt - template for this SCSI adapter.
138 * Returns : 1 if a host adapter was found, 0 if not.
142 static int sun3scsi_detect(Scsi_Host_Template * tpnt)
144 unsigned long ioaddr, irq;
145 static int called = 0;
146 struct Scsi_Host *instance;
148 unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
149 IOBASE_SUN3_VMESCSI + 0x4000,
151 unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
154 /* check that this machine has an onboard 5380 */
155 switch(idprom->id_machtype) {
156 case SM_SUN3|SM_3_160:
157 case SM_SUN3|SM_3_260:
167 tpnt->proc_name = "Sun3 5380 VME SCSI";
169 /* setup variables */
171 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
173 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
175 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
177 if (setup_hostid >= 0)
178 tpnt->this_id = setup_hostid;
180 /* use 7 as default */
185 for(i = 0; addrs[i] != 0; i++) {
188 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
189 SUN3_PAGE_TYPE_VME16);
191 sun3_scsi_regp = (unsigned char *)ioaddr;
193 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
195 if(sun3_map_test((unsigned long)dregs, &x)) {
196 unsigned short oldcsr;
200 udelay(SUN3_DMA_DELAY);
201 if(dregs->csr == 0x1400)
207 iounmap((void *)ioaddr);
215 if (setup_use_tagged_queuing < 0)
216 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
219 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
223 default_instance = instance;
225 instance->io_port = (unsigned long) ioaddr;
228 NCR5380_init(instance, 0);
230 instance->n_io_port = 32;
232 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
234 if (request_irq(instance->irq, scsi_sun3_intr,
235 0, "Sun3SCSI-5380VME", NULL)) {
237 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
238 instance->host_no, instance->irq);
239 instance->irq = SCSI_IRQ_NONE;
241 printk("scsi%d: IRQ%d not free, bailing out\n",
242 instance->host_no, instance->irq);
247 printk("scsi%d: Sun3 5380 VME at port %lX irq", instance->host_no, instance->io_port);
248 if (instance->irq == SCSI_IRQ_NONE)
249 printk ("s disabled");
251 printk (" %d", instance->irq);
252 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
253 instance->can_queue, instance->cmd_per_lun,
254 SUN3SCSI_PUBLIC_RELEASE);
255 printk("\nscsi%d:", instance->host_no);
256 NCR5380_print_options(instance);
260 udelay(SUN3_DMA_DELAY);
261 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
262 udelay(SUN3_DMA_DELAY);
263 dregs->fifo_count = 0;
264 dregs->fifo_count_hi = 0;
265 dregs->dma_addr_hi = 0;
266 dregs->dma_addr_lo = 0;
267 dregs->dma_count_hi = 0;
268 dregs->dma_count_lo = 0;
270 dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
275 sun3_scsi_reset_boot(instance);
282 int sun3scsi_release (struct Scsi_Host *shpnt)
284 if (shpnt->irq != SCSI_IRQ_NONE)
285 free_irq (shpnt->irq, NULL);
287 iounmap(sun3_scsi_regp);
295 * Our 'bus reset on boot' function
298 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
302 NCR5380_local_declare();
303 NCR5380_setup(instance);
306 * Do a SCSI reset to clean up the bus during initialization. No
307 * messing with the queues, interrupts, or locks necessary here.
310 printk( "Sun3 SCSI: resetting the SCSI bus..." );
312 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
313 // sun3_disable_irq( IRQ_SUN3_SCSI );
316 NCR5380_write( TARGET_COMMAND_REG,
317 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
320 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
322 /* The min. reset hold time is 25us, so 40us should be enough */
325 /* reset RST and interrupt */
326 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
327 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
329 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
332 /* switch on SCSI IRQ again */
333 // sun3_enable_irq( IRQ_SUN3_SCSI );
339 static const char * sun3scsi_info (struct Scsi_Host *spnt) {
343 // safe bits for the CSR
344 #define CSR_GOOD 0x060f
346 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
348 unsigned short csr = dregs->csr;
350 dregs->csr &= ~CSR_DMA_ENABLE;
353 #ifdef SUN3_SCSI_DEBUG
354 printk("scsi_intr csr %x\n", csr);
357 if(csr & ~CSR_GOOD) {
358 if(csr & CSR_DMA_BUSERR) {
359 printk("scsi%d: bus error in dma\n", default_instance->host_no);
360 #ifdef SUN3_SCSI_DEBUG
361 printk("scsi: residual %x count %x addr %p dmaaddr %x\n",
363 dregs->dma_count_lo | (dregs->dma_count_hi << 16),
365 dregs->dma_addr_lo | (dregs->dma_addr_hi << 16));
369 if(csr & CSR_DMA_CONFLICT) {
370 printk("scsi%d: dma conflict\n", default_instance->host_no);
374 if(csr & (CSR_SDB_INT | CSR_DMA_INT))
375 NCR5380_intr(irq, dummy, fp);
379 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
380 * reentering NCR5380_print_status seems to have ugly side effects
383 /* this doesn't seem to get used at all -- sam */
385 void sun3_sun3_debug (void)
388 NCR5380_local_declare();
390 if (default_instance) {
393 NCR5380_print_status(default_instance);
394 restore_flags(flags);
400 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
401 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
405 if(sun3_dma_orig_addr != NULL)
406 dvma_unmap(sun3_dma_orig_addr);
408 // addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
409 addr = (void *)dvma_map_vme((unsigned long) data, count);
411 sun3_dma_orig_addr = addr;
412 sun3_dma_orig_count = count;
414 #ifdef SUN3_SCSI_DEBUG
415 printk("scsi: dma_setup addr %p count %x\n", addr, count);
418 // dregs->fifo_count = 0;
421 dregs->csr &= ~CSR_FIFO;
422 dregs->csr |= CSR_FIFO;
426 dregs->csr |= CSR_SEND;
428 dregs->csr &= ~CSR_SEND;
431 // dregs->csr &= ~CSR_FIFO;
432 // dregs->csr |= CSR_FIFO;
434 dregs->csr |= CSR_PACK_ENABLE;
436 dregs->dma_addr_hi = ((unsigned long)addr >> 16);
437 dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
439 dregs->dma_count_hi = 0;
440 dregs->dma_count_lo = 0;
441 dregs->fifo_count_hi = 0;
442 dregs->fifo_count = 0;
444 #ifdef SUN3_SCSI_DEBUG
445 printk("scsi: dma_setup done csr %x\n", dregs->csr);
451 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
453 return last_residual;
456 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
459 if((cmd->request.cmd == 0) || (cmd->request.cmd == 1))
465 static int sun3scsi_dma_start(unsigned long count, char *data)
471 #ifdef SUN3_SCSI_DEBUG
472 printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data, count, csr, dregs->fifo_count);
475 dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
476 dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
478 dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
479 dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
481 // if(!(csr & CSR_DMA_ENABLE))
482 // dregs->csr |= CSR_DMA_ENABLE;
487 /* clean up after our dma is done */
488 static int sun3scsi_dma_finish(int write_flag)
495 dregs->csr &= ~CSR_DMA_ENABLE;
497 fifo = dregs->fifo_count;
499 if((fifo > 0) && (fifo < sun3_dma_orig_count))
503 last_residual = fifo;
504 #ifdef SUN3_SCSI_DEBUG
505 printk("scsi: residual %x total %x\n", fifo, sun3_dma_orig_count);
507 /* empty bytes from the fifo which didn't make it */
508 if((!write_flag) && (dregs->csr & CSR_LEFT)) {
509 unsigned char *vaddr;
511 #ifdef SUN3_SCSI_DEBUG
512 printk("scsi: got left over bytes\n");
515 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
517 vaddr += (sun3_dma_orig_count - fifo);
520 switch(dregs->csr & CSR_LEFT) {
522 *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
526 *vaddr = (dregs->bpack_hi & 0x00ff);
530 *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
537 dvma_unmap(sun3_dma_orig_addr);
538 sun3_dma_orig_addr = NULL;
540 dregs->dma_addr_hi = 0;
541 dregs->dma_addr_lo = 0;
542 dregs->dma_count_hi = 0;
543 dregs->dma_count_lo = 0;
545 dregs->fifo_count = 0;
546 dregs->fifo_count_hi = 0;
548 dregs->csr &= ~CSR_SEND;
550 // dregs->csr |= CSR_DMA_ENABLE;
554 dregs->csr &= ~CSR_FIFO;
555 dregs->csr |= CSR_FIFO;
557 sun3_dma_setup_done = NULL;
563 #include "sun3_NCR5380.c"
565 static Scsi_Host_Template driver_template = SUN3_NCR5380;
567 #include "scsi_module.c"