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/ioport.h>
24 #include <linux/init.h>
25 #include <linux/blkdev.h>
29 #include <asm/sun3ints.h>
31 #include <asm/idprom.h>
32 #include <asm/machines.h>
36 #undef SUN3_SCSI_DEBUG
43 #include <scsi/scsi_host.h>
44 #include "sun3_scsi.h"
47 extern int sun3_map_test(unsigned long, char *);
50 /*#define RESET_BOOT */
54 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
61 /* #define SUPPORT_TAGS */
63 //#define ENABLE_IRQ() enable_irq( SUN3_VEC_VMESCSI0 );
67 static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
68 static inline unsigned char sun3scsi_read(int reg);
69 static inline void sun3scsi_write(int reg, int value);
71 static int setup_can_queue = -1;
72 module_param(setup_can_queue, int, 0);
73 static int setup_cmd_per_lun = -1;
74 module_param(setup_cmd_per_lun, int, 0);
75 static int setup_sg_tablesize = -1;
76 module_param(setup_sg_tablesize, int, 0);
78 static int setup_use_tagged_queuing = -1;
79 module_param(setup_use_tagged_queuing, int, 0);
81 static int setup_hostid = -1;
82 module_param(setup_hostid, int, 0);
84 static struct scsi_cmnd *sun3_dma_setup_done = NULL;
86 #define AFTER_RESET_DELAY (HZ/2)
88 /* ms to wait after hitting dma regs */
89 #define SUN3_DMA_DELAY 10
91 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
92 #define SUN3_DVMA_BUFSIZE 0xe000
94 /* minimum number of bytes to do dma on */
95 #define SUN3_DMA_MINSIZE 128
97 static volatile unsigned char *sun3_scsi_regp;
98 static volatile struct sun3_dma_regs *dregs;
100 static unsigned char *dmabuf = NULL; /* dma memory buffer */
102 static unsigned char *sun3_dma_orig_addr = NULL;
103 static unsigned long sun3_dma_orig_count = 0;
104 static int sun3_dma_active = 0;
105 static unsigned long last_residual = 0;
108 * NCR 5380 register access functions
111 static inline unsigned char sun3scsi_read(int reg)
113 return( sun3_scsi_regp[reg] );
116 static inline void sun3scsi_write(int reg, int value)
118 sun3_scsi_regp[reg] = value;
124 static struct Scsi_Host *default_instance;
127 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
129 * Purpose : initializes mac NCR5380 driver based on the
130 * command line / compile time port and irq definitions.
132 * Inputs : tpnt - template for this SCSI adapter.
134 * Returns : 1 if a host adapter was found, 0 if not.
138 static int __init sun3scsi_detect(struct scsi_host_template * tpnt)
140 unsigned long ioaddr, irq = 0;
141 static int called = 0;
142 struct Scsi_Host *instance;
144 unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
145 IOBASE_SUN3_VMESCSI + 0x4000,
147 unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
150 /* check that this machine has an onboard 5380 */
151 switch(idprom->id_machtype) {
152 case SM_SUN3|SM_3_160:
153 case SM_SUN3|SM_3_260:
163 tpnt->proc_name = "Sun3 5380 VME SCSI";
165 /* setup variables */
167 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
169 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
171 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
173 if (setup_hostid >= 0)
174 tpnt->this_id = setup_hostid;
176 /* use 7 as default */
181 for(i = 0; addrs[i] != 0; i++) {
184 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
185 SUN3_PAGE_TYPE_VME16);
187 sun3_scsi_regp = (unsigned char *)ioaddr;
189 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
191 if(sun3_map_test((unsigned long)dregs, &x)) {
192 unsigned short oldcsr;
196 udelay(SUN3_DMA_DELAY);
197 if(dregs->csr == 0x1400)
203 iounmap((void *)ioaddr);
211 if (setup_use_tagged_queuing < 0)
212 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
215 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
219 default_instance = instance;
221 instance->io_port = (unsigned long) ioaddr;
224 NCR5380_init(instance, 0);
226 instance->n_io_port = 32;
228 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
230 if (request_irq(instance->irq, scsi_sun3_intr,
231 0, "Sun3SCSI-5380VME", instance)) {
233 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
234 instance->host_no, instance->irq);
235 instance->irq = SCSI_IRQ_NONE;
237 printk("scsi%d: IRQ%d not free, bailing out\n",
238 instance->host_no, instance->irq);
243 printk("scsi%d: Sun3 5380 VME at port %lX irq", instance->host_no, instance->io_port);
244 if (instance->irq == SCSI_IRQ_NONE)
245 printk ("s disabled");
247 printk (" %d", instance->irq);
248 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
249 instance->can_queue, instance->cmd_per_lun,
250 SUN3SCSI_PUBLIC_RELEASE);
251 printk("\nscsi%d:", instance->host_no);
252 NCR5380_print_options(instance);
256 udelay(SUN3_DMA_DELAY);
257 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
258 udelay(SUN3_DMA_DELAY);
259 dregs->fifo_count = 0;
260 dregs->fifo_count_hi = 0;
261 dregs->dma_addr_hi = 0;
262 dregs->dma_addr_lo = 0;
263 dregs->dma_count_hi = 0;
264 dregs->dma_count_lo = 0;
266 dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
271 sun3_scsi_reset_boot(instance);
277 int sun3scsi_release (struct Scsi_Host *shpnt)
279 if (shpnt->irq != SCSI_IRQ_NONE)
280 free_irq(shpnt->irq, shpnt);
282 iounmap((void *)sun3_scsi_regp);
290 * Our 'bus reset on boot' function
293 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
297 NCR5380_local_declare();
298 NCR5380_setup(instance);
301 * Do a SCSI reset to clean up the bus during initialization. No
302 * messing with the queues, interrupts, or locks necessary here.
305 printk( "Sun3 SCSI: resetting the SCSI bus..." );
307 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
308 // sun3_disable_irq( IRQ_SUN3_SCSI );
311 NCR5380_write( TARGET_COMMAND_REG,
312 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
315 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
317 /* The min. reset hold time is 25us, so 40us should be enough */
320 /* reset RST and interrupt */
321 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
322 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
324 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
327 /* switch on SCSI IRQ again */
328 // sun3_enable_irq( IRQ_SUN3_SCSI );
334 static const char * sun3scsi_info (struct Scsi_Host *spnt) {
338 // safe bits for the CSR
339 #define CSR_GOOD 0x060f
341 static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
343 unsigned short csr = dregs->csr;
346 dregs->csr &= ~CSR_DMA_ENABLE;
349 #ifdef SUN3_SCSI_DEBUG
350 printk("scsi_intr csr %x\n", csr);
353 if(csr & ~CSR_GOOD) {
354 if(csr & CSR_DMA_BUSERR) {
355 printk("scsi%d: bus error in dma\n", default_instance->host_no);
356 #ifdef SUN3_SCSI_DEBUG
357 printk("scsi: residual %x count %x addr %p dmaaddr %x\n",
359 dregs->dma_count_lo | (dregs->dma_count_hi << 16),
361 dregs->dma_addr_lo | (dregs->dma_addr_hi << 16));
365 if(csr & CSR_DMA_CONFLICT) {
366 printk("scsi%d: dma conflict\n", default_instance->host_no);
371 if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
372 NCR5380_intr(irq, dummy);
376 return IRQ_RETVAL(handled);
380 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
381 * reentering NCR5380_print_status seems to have ugly side effects
384 /* this doesn't seem to get used at all -- sam */
386 void sun3_sun3_debug (void)
389 NCR5380_local_declare();
391 if (default_instance) {
392 local_irq_save(flags);
393 NCR5380_print_status(default_instance);
394 local_irq_restore(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,
457 struct scsi_cmnd *cmd,
460 if (cmd->request->cmd_type == REQ_TYPE_FS)
466 static int sun3scsi_dma_start(unsigned long count, char *data)
472 #ifdef SUN3_SCSI_DEBUG
473 printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data, count, csr, dregs->fifo_count);
476 dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
477 dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
479 dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
480 dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
482 // if(!(csr & CSR_DMA_ENABLE))
483 // dregs->csr |= CSR_DMA_ENABLE;
488 /* clean up after our dma is done */
489 static int sun3scsi_dma_finish(int write_flag)
496 dregs->csr &= ~CSR_DMA_ENABLE;
498 fifo = dregs->fifo_count;
500 if((fifo > 0) && (fifo < sun3_dma_orig_count))
504 last_residual = fifo;
505 #ifdef SUN3_SCSI_DEBUG
506 printk("scsi: residual %x total %x\n", fifo, sun3_dma_orig_count);
508 /* empty bytes from the fifo which didn't make it */
509 if((!write_flag) && (dregs->csr & CSR_LEFT)) {
510 unsigned char *vaddr;
512 #ifdef SUN3_SCSI_DEBUG
513 printk("scsi: got left over bytes\n");
516 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
518 vaddr += (sun3_dma_orig_count - fifo);
521 switch(dregs->csr & CSR_LEFT) {
523 *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
527 *vaddr = (dregs->bpack_hi & 0x00ff);
531 *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
538 dvma_unmap(sun3_dma_orig_addr);
539 sun3_dma_orig_addr = NULL;
541 dregs->dma_addr_hi = 0;
542 dregs->dma_addr_lo = 0;
543 dregs->dma_count_hi = 0;
544 dregs->dma_count_lo = 0;
546 dregs->fifo_count = 0;
547 dregs->fifo_count_hi = 0;
549 dregs->csr &= ~CSR_SEND;
551 // dregs->csr |= CSR_DMA_ENABLE;
555 dregs->csr &= ~CSR_FIFO;
556 dregs->csr |= CSR_FIFO;
558 sun3_dma_setup_done = NULL;
564 #include "sun3_NCR5380.c"
566 static struct scsi_host_template driver_template = {
567 .name = SUN3_SCSI_NAME,
568 .detect = sun3scsi_detect,
569 .release = sun3scsi_release,
570 .info = sun3scsi_info,
571 .queuecommand = sun3scsi_queue_command,
572 .eh_abort_handler = sun3scsi_abort,
573 .eh_bus_reset_handler = sun3scsi_bus_reset,
574 .can_queue = CAN_QUEUE,
576 .sg_tablesize = SG_TABLESIZE,
577 .cmd_per_lun = CMD_PER_LUN,
578 .use_clustering = DISABLE_CLUSTERING
582 #include "scsi_module.c"
584 MODULE_LICENSE("GPL");