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>
28 #include <asm/system.h>
30 #include <asm/sun3ints.h>
32 #include <asm/idprom.h>
33 #include <asm/machines.h>
37 #undef SUN3_SCSI_DEBUG
44 #include <scsi/scsi_host.h>
45 #include "sun3_scsi.h"
47 extern int sun3_map_test(unsigned long, char *);
50 /*#define RESET_BOOT */
56 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
63 /* #define SUPPORT_TAGS */
65 //#define ENABLE_IRQ() enable_irq( SUN3_VEC_VMESCSI0 );
69 static irqreturn_t
scsi_sun3_intr(int irq
, void *dummy
);
70 static inline unsigned char sun3scsi_read(int reg
);
71 static inline void sun3scsi_write(int reg
, int value
);
73 static int setup_can_queue
= -1;
74 module_param(setup_can_queue
, int, 0);
75 static int setup_cmd_per_lun
= -1;
76 module_param(setup_cmd_per_lun
, int, 0);
77 static int setup_sg_tablesize
= -1;
78 module_param(setup_sg_tablesize
, int, 0);
80 static int setup_use_tagged_queuing
= -1;
81 module_param(setup_use_tagged_queuing
, int, 0);
83 static int setup_hostid
= -1;
84 module_param(setup_hostid
, int, 0);
86 static struct scsi_cmnd
*sun3_dma_setup_done
= NULL
;
88 #define AFTER_RESET_DELAY (HZ/2)
90 /* ms to wait after hitting dma regs */
91 #define SUN3_DMA_DELAY 10
93 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
94 #define SUN3_DVMA_BUFSIZE 0xe000
96 /* minimum number of bytes to do dma on */
97 #define SUN3_DMA_MINSIZE 128
99 static volatile unsigned char *sun3_scsi_regp
;
100 static volatile struct sun3_dma_regs
*dregs
;
102 static unsigned char *dmabuf
= NULL
; /* dma memory buffer */
104 static unsigned char *sun3_dma_orig_addr
= NULL
;
105 static unsigned long sun3_dma_orig_count
= 0;
106 static int sun3_dma_active
= 0;
107 static unsigned long last_residual
= 0;
110 * NCR 5380 register access functions
113 static inline unsigned char sun3scsi_read(int reg
)
115 return( sun3_scsi_regp
[reg
] );
118 static inline void sun3scsi_write(int reg
, int value
)
120 sun3_scsi_regp
[reg
] = value
;
126 static struct Scsi_Host
*default_instance
;
129 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
131 * Purpose : initializes mac NCR5380 driver based on the
132 * command line / compile time port and irq definitions.
134 * Inputs : tpnt - template for this SCSI adapter.
136 * Returns : 1 if a host adapter was found, 0 if not.
140 static int __init
sun3scsi_detect(struct scsi_host_template
* tpnt
)
142 unsigned long ioaddr
, irq
= 0;
143 static int called
= 0;
144 struct Scsi_Host
*instance
;
146 unsigned long addrs
[3] = { IOBASE_SUN3_VMESCSI
,
147 IOBASE_SUN3_VMESCSI
+ 0x4000,
149 unsigned long vecs
[3] = { SUN3_VEC_VMESCSI0
,
152 /* check that this machine has an onboard 5380 */
153 switch(idprom
->id_machtype
) {
154 case SM_SUN3
|SM_3_160
:
155 case SM_SUN3
|SM_3_260
:
165 tpnt
->proc_name
= "Sun3 5380 VME SCSI";
167 /* setup variables */
169 (setup_can_queue
> 0) ? setup_can_queue
: CAN_QUEUE
;
171 (setup_cmd_per_lun
> 0) ? setup_cmd_per_lun
: CMD_PER_LUN
;
173 (setup_sg_tablesize
>= 0) ? setup_sg_tablesize
: SG_TABLESIZE
;
175 if (setup_hostid
>= 0)
176 tpnt
->this_id
= setup_hostid
;
178 /* use 7 as default */
183 for(i
= 0; addrs
[i
] != 0; i
++) {
186 ioaddr
= (unsigned long)sun3_ioremap(addrs
[i
], PAGE_SIZE
,
187 SUN3_PAGE_TYPE_VME16
);
189 sun3_scsi_regp
= (unsigned char *)ioaddr
;
191 dregs
= (struct sun3_dma_regs
*)(((unsigned char *)ioaddr
) + 8);
193 if(sun3_map_test((unsigned long)dregs
, &x
)) {
194 unsigned short oldcsr
;
198 udelay(SUN3_DMA_DELAY
);
199 if(dregs
->csr
== 0x1400)
205 iounmap((void *)ioaddr
);
213 if (setup_use_tagged_queuing
< 0)
214 setup_use_tagged_queuing
= USE_TAGGED_QUEUING
;
217 instance
= scsi_register (tpnt
, sizeof(struct NCR5380_hostdata
));
221 default_instance
= instance
;
223 instance
->io_port
= (unsigned long) ioaddr
;
226 NCR5380_init(instance
, 0);
228 instance
->n_io_port
= 32;
230 ((struct NCR5380_hostdata
*)instance
->hostdata
)->ctrl
= 0;
232 if (request_irq(instance
->irq
, scsi_sun3_intr
,
233 0, "Sun3SCSI-5380VME", instance
)) {
235 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
236 instance
->host_no
, instance
->irq
);
237 instance
->irq
= SCSI_IRQ_NONE
;
239 printk("scsi%d: IRQ%d not free, bailing out\n",
240 instance
->host_no
, instance
->irq
);
245 printk("scsi%d: Sun3 5380 VME at port %lX irq", instance
->host_no
, instance
->io_port
);
246 if (instance
->irq
== SCSI_IRQ_NONE
)
247 printk ("s disabled");
249 printk (" %d", instance
->irq
);
250 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
251 instance
->can_queue
, instance
->cmd_per_lun
,
252 SUN3SCSI_PUBLIC_RELEASE
);
253 printk("\nscsi%d:", instance
->host_no
);
254 NCR5380_print_options(instance
);
258 udelay(SUN3_DMA_DELAY
);
259 dregs
->csr
= CSR_SCSI
| CSR_FIFO
| CSR_INTR
;
260 udelay(SUN3_DMA_DELAY
);
261 dregs
->fifo_count
= 0;
262 dregs
->fifo_count_hi
= 0;
263 dregs
->dma_addr_hi
= 0;
264 dregs
->dma_addr_lo
= 0;
265 dregs
->dma_count_hi
= 0;
266 dregs
->dma_count_lo
= 0;
268 dregs
->ivect
= VME_DATA24
| (instance
->irq
& 0xff);
273 sun3_scsi_reset_boot(instance
);
279 int sun3scsi_release (struct Scsi_Host
*shpnt
)
281 if (shpnt
->irq
!= SCSI_IRQ_NONE
)
282 free_irq(shpnt
->irq
, shpnt
);
284 iounmap((void *)sun3_scsi_regp
);
292 * Our 'bus reset on boot' function
295 static void sun3_scsi_reset_boot(struct Scsi_Host
*instance
)
299 NCR5380_local_declare();
300 NCR5380_setup(instance
);
303 * Do a SCSI reset to clean up the bus during initialization. No
304 * messing with the queues, interrupts, or locks necessary here.
307 printk( "Sun3 SCSI: resetting the SCSI bus..." );
309 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
310 // sun3_disable_irq( IRQ_SUN3_SCSI );
313 NCR5380_write( TARGET_COMMAND_REG
,
314 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG
) ));
317 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
319 /* The min. reset hold time is 25us, so 40us should be enough */
322 /* reset RST and interrupt */
323 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
);
324 NCR5380_read( RESET_PARITY_INTERRUPT_REG
);
326 for( end
= jiffies
+ AFTER_RESET_DELAY
; time_before(jiffies
, end
); )
329 /* switch on SCSI IRQ again */
330 // sun3_enable_irq( IRQ_SUN3_SCSI );
336 static const char * sun3scsi_info (struct Scsi_Host
*spnt
) {
340 // safe bits for the CSR
341 #define CSR_GOOD 0x060f
343 static irqreturn_t
scsi_sun3_intr(int irq
, void *dummy
)
345 unsigned short csr
= dregs
->csr
;
348 dregs
->csr
&= ~CSR_DMA_ENABLE
;
351 #ifdef SUN3_SCSI_DEBUG
352 printk("scsi_intr csr %x\n", csr
);
355 if(csr
& ~CSR_GOOD
) {
356 if(csr
& CSR_DMA_BUSERR
) {
357 printk("scsi%d: bus error in dma\n", default_instance
->host_no
);
358 #ifdef SUN3_SCSI_DEBUG
359 printk("scsi: residual %x count %x addr %p dmaaddr %x\n",
361 dregs
->dma_count_lo
| (dregs
->dma_count_hi
<< 16),
363 dregs
->dma_addr_lo
| (dregs
->dma_addr_hi
<< 16));
367 if(csr
& CSR_DMA_CONFLICT
) {
368 printk("scsi%d: dma conflict\n", default_instance
->host_no
);
373 if(csr
& (CSR_SDB_INT
| CSR_DMA_INT
)) {
374 NCR5380_intr(irq
, dummy
);
378 return IRQ_RETVAL(handled
);
382 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
383 * reentering NCR5380_print_status seems to have ugly side effects
386 /* this doesn't seem to get used at all -- sam */
388 void sun3_sun3_debug (void)
391 NCR5380_local_declare();
393 if (default_instance
) {
394 local_irq_save(flags
);
395 NCR5380_print_status(default_instance
);
396 local_irq_restore(flags
);
402 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
403 static unsigned long sun3scsi_dma_setup(void *data
, unsigned long count
, int write_flag
)
407 if(sun3_dma_orig_addr
!= NULL
)
408 dvma_unmap(sun3_dma_orig_addr
);
410 // addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
411 addr
= (void *)dvma_map_vme((unsigned long) data
, count
);
413 sun3_dma_orig_addr
= addr
;
414 sun3_dma_orig_count
= count
;
416 #ifdef SUN3_SCSI_DEBUG
417 printk("scsi: dma_setup addr %p count %x\n", addr
, count
);
420 // dregs->fifo_count = 0;
423 dregs
->csr
&= ~CSR_FIFO
;
424 dregs
->csr
|= CSR_FIFO
;
428 dregs
->csr
|= CSR_SEND
;
430 dregs
->csr
&= ~CSR_SEND
;
433 // dregs->csr &= ~CSR_FIFO;
434 // dregs->csr |= CSR_FIFO;
436 dregs
->csr
|= CSR_PACK_ENABLE
;
438 dregs
->dma_addr_hi
= ((unsigned long)addr
>> 16);
439 dregs
->dma_addr_lo
= ((unsigned long)addr
& 0xffff);
441 dregs
->dma_count_hi
= 0;
442 dregs
->dma_count_lo
= 0;
443 dregs
->fifo_count_hi
= 0;
444 dregs
->fifo_count
= 0;
446 #ifdef SUN3_SCSI_DEBUG
447 printk("scsi: dma_setup done csr %x\n", dregs
->csr
);
453 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host
*instance
)
455 return last_residual
;
458 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted
,
459 struct scsi_cmnd
*cmd
,
462 if (cmd
->request
->cmd_type
== REQ_TYPE_FS
)
468 static int sun3scsi_dma_start(unsigned long count
, char *data
)
474 #ifdef SUN3_SCSI_DEBUG
475 printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data
, count
, csr
, dregs
->fifo_count
);
478 dregs
->dma_count_hi
= (sun3_dma_orig_count
>> 16);
479 dregs
->dma_count_lo
= (sun3_dma_orig_count
& 0xffff);
481 dregs
->fifo_count_hi
= (sun3_dma_orig_count
>> 16);
482 dregs
->fifo_count
= (sun3_dma_orig_count
& 0xffff);
484 // if(!(csr & CSR_DMA_ENABLE))
485 // dregs->csr |= CSR_DMA_ENABLE;
490 /* clean up after our dma is done */
491 static int sun3scsi_dma_finish(int write_flag
)
498 dregs
->csr
&= ~CSR_DMA_ENABLE
;
500 fifo
= dregs
->fifo_count
;
502 if((fifo
> 0) && (fifo
< sun3_dma_orig_count
))
506 last_residual
= fifo
;
507 #ifdef SUN3_SCSI_DEBUG
508 printk("scsi: residual %x total %x\n", fifo
, sun3_dma_orig_count
);
510 /* empty bytes from the fifo which didn't make it */
511 if((!write_flag
) && (dregs
->csr
& CSR_LEFT
)) {
512 unsigned char *vaddr
;
514 #ifdef SUN3_SCSI_DEBUG
515 printk("scsi: got left over bytes\n");
518 vaddr
= (unsigned char *)dvma_vmetov(sun3_dma_orig_addr
);
520 vaddr
+= (sun3_dma_orig_count
- fifo
);
523 switch(dregs
->csr
& CSR_LEFT
) {
525 *vaddr
= (dregs
->bpack_lo
& 0xff00) >> 8;
529 *vaddr
= (dregs
->bpack_hi
& 0x00ff);
533 *vaddr
= (dregs
->bpack_hi
& 0xff00) >> 8;
540 dvma_unmap(sun3_dma_orig_addr
);
541 sun3_dma_orig_addr
= NULL
;
543 dregs
->dma_addr_hi
= 0;
544 dregs
->dma_addr_lo
= 0;
545 dregs
->dma_count_hi
= 0;
546 dregs
->dma_count_lo
= 0;
548 dregs
->fifo_count
= 0;
549 dregs
->fifo_count_hi
= 0;
551 dregs
->csr
&= ~CSR_SEND
;
553 // dregs->csr |= CSR_DMA_ENABLE;
557 dregs
->csr
&= ~CSR_FIFO
;
558 dregs
->csr
|= CSR_FIFO
;
560 sun3_dma_setup_done
= NULL
;
566 #include "sun3_NCR5380.c"
568 static struct scsi_host_template driver_template
= {
569 .name
= SUN3_SCSI_NAME
,
570 .detect
= sun3scsi_detect
,
571 .release
= sun3scsi_release
,
572 .info
= sun3scsi_info
,
573 .queuecommand
= sun3scsi_queue_command
,
574 .eh_abort_handler
= sun3scsi_abort
,
575 .eh_bus_reset_handler
= sun3scsi_bus_reset
,
576 .can_queue
= CAN_QUEUE
,
578 .sg_tablesize
= SG_TABLESIZE
,
579 .cmd_per_lun
= CMD_PER_LUN
,
580 .use_clustering
= DISABLE_CLUSTERING
584 #include "scsi_module.c"
586 MODULE_LICENSE("GPL");