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 #define NDEBUG_ABORT 0x00100000
45 #define NDEBUG_TAGS 0x00200000
46 #define NDEBUG_MERGING 0x00400000
50 #include <scsi/scsi_host.h>
51 #include "sun3_scsi.h"
53 extern int sun3_map_test(unsigned long, char *);
56 /*#define RESET_BOOT */
60 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
67 /* #define SUPPORT_TAGS */
69 //#define ENABLE_IRQ() enable_irq( SUN3_VEC_VMESCSI0 );
73 static irqreturn_t
scsi_sun3_intr(int irq
, void *dummy
);
74 static inline unsigned char sun3scsi_read(int reg
);
75 static inline void sun3scsi_write(int reg
, int value
);
77 static int setup_can_queue
= -1;
78 module_param(setup_can_queue
, int, 0);
79 static int setup_cmd_per_lun
= -1;
80 module_param(setup_cmd_per_lun
, int, 0);
81 static int setup_sg_tablesize
= -1;
82 module_param(setup_sg_tablesize
, int, 0);
84 static int setup_use_tagged_queuing
= -1;
85 module_param(setup_use_tagged_queuing
, int, 0);
87 static int setup_hostid
= -1;
88 module_param(setup_hostid
, int, 0);
90 static struct scsi_cmnd
*sun3_dma_setup_done
= NULL
;
92 #define AFTER_RESET_DELAY (HZ/2)
94 /* ms to wait after hitting dma regs */
95 #define SUN3_DMA_DELAY 10
97 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
98 #define SUN3_DVMA_BUFSIZE 0xe000
100 /* minimum number of bytes to do dma on */
101 #define SUN3_DMA_MINSIZE 128
103 static volatile unsigned char *sun3_scsi_regp
;
104 static volatile struct sun3_dma_regs
*dregs
;
106 static unsigned char *dmabuf
= NULL
; /* dma memory buffer */
108 static unsigned char *sun3_dma_orig_addr
= NULL
;
109 static unsigned long sun3_dma_orig_count
= 0;
110 static int sun3_dma_active
= 0;
111 static unsigned long last_residual
= 0;
114 * NCR 5380 register access functions
117 static inline unsigned char sun3scsi_read(int reg
)
119 return( sun3_scsi_regp
[reg
] );
122 static inline void sun3scsi_write(int reg
, int value
)
124 sun3_scsi_regp
[reg
] = value
;
130 static struct Scsi_Host
*default_instance
;
133 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
135 * Purpose : initializes mac NCR5380 driver based on the
136 * command line / compile time port and irq definitions.
138 * Inputs : tpnt - template for this SCSI adapter.
140 * Returns : 1 if a host adapter was found, 0 if not.
144 static int __init
sun3scsi_detect(struct scsi_host_template
* tpnt
)
146 unsigned long ioaddr
, irq
= 0;
147 static int called
= 0;
148 struct Scsi_Host
*instance
;
150 unsigned long addrs
[3] = { IOBASE_SUN3_VMESCSI
,
151 IOBASE_SUN3_VMESCSI
+ 0x4000,
153 unsigned long vecs
[3] = { SUN3_VEC_VMESCSI0
,
156 /* check that this machine has an onboard 5380 */
157 switch(idprom
->id_machtype
) {
158 case SM_SUN3
|SM_3_160
:
159 case SM_SUN3
|SM_3_260
:
169 tpnt
->proc_name
= "Sun3 5380 VME SCSI";
171 /* setup variables */
173 (setup_can_queue
> 0) ? setup_can_queue
: CAN_QUEUE
;
175 (setup_cmd_per_lun
> 0) ? setup_cmd_per_lun
: CMD_PER_LUN
;
177 (setup_sg_tablesize
>= 0) ? setup_sg_tablesize
: SG_TABLESIZE
;
179 if (setup_hostid
>= 0)
180 tpnt
->this_id
= setup_hostid
;
182 /* use 7 as default */
187 for(i
= 0; addrs
[i
] != 0; i
++) {
190 ioaddr
= (unsigned long)sun3_ioremap(addrs
[i
], PAGE_SIZE
,
191 SUN3_PAGE_TYPE_VME16
);
193 sun3_scsi_regp
= (unsigned char *)ioaddr
;
195 dregs
= (struct sun3_dma_regs
*)(((unsigned char *)ioaddr
) + 8);
197 if(sun3_map_test((unsigned long)dregs
, &x
)) {
198 unsigned short oldcsr
;
202 udelay(SUN3_DMA_DELAY
);
203 if(dregs
->csr
== 0x1400)
209 iounmap((void *)ioaddr
);
217 if (setup_use_tagged_queuing
< 0)
218 setup_use_tagged_queuing
= USE_TAGGED_QUEUING
;
221 instance
= scsi_register (tpnt
, sizeof(struct NCR5380_hostdata
));
225 default_instance
= instance
;
227 instance
->io_port
= (unsigned long) ioaddr
;
230 NCR5380_init(instance
, 0);
232 instance
->n_io_port
= 32;
234 ((struct NCR5380_hostdata
*)instance
->hostdata
)->ctrl
= 0;
236 if (request_irq(instance
->irq
, scsi_sun3_intr
,
237 0, "Sun3SCSI-5380VME", instance
)) {
239 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
240 instance
->host_no
, instance
->irq
);
241 instance
->irq
= SCSI_IRQ_NONE
;
243 printk("scsi%d: IRQ%d not free, bailing out\n",
244 instance
->host_no
, instance
->irq
);
249 printk("scsi%d: Sun3 5380 VME at port %lX irq", instance
->host_no
, instance
->io_port
);
250 if (instance
->irq
== SCSI_IRQ_NONE
)
251 printk ("s disabled");
253 printk (" %d", instance
->irq
);
254 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
255 instance
->can_queue
, instance
->cmd_per_lun
,
256 SUN3SCSI_PUBLIC_RELEASE
);
257 printk("\nscsi%d:", instance
->host_no
);
258 NCR5380_print_options(instance
);
262 udelay(SUN3_DMA_DELAY
);
263 dregs
->csr
= CSR_SCSI
| CSR_FIFO
| CSR_INTR
;
264 udelay(SUN3_DMA_DELAY
);
265 dregs
->fifo_count
= 0;
266 dregs
->fifo_count_hi
= 0;
267 dregs
->dma_addr_hi
= 0;
268 dregs
->dma_addr_lo
= 0;
269 dregs
->dma_count_hi
= 0;
270 dregs
->dma_count_lo
= 0;
272 dregs
->ivect
= VME_DATA24
| (instance
->irq
& 0xff);
277 sun3_scsi_reset_boot(instance
);
283 int sun3scsi_release (struct Scsi_Host
*shpnt
)
285 if (shpnt
->irq
!= SCSI_IRQ_NONE
)
286 free_irq(shpnt
->irq
, shpnt
);
288 iounmap((void *)sun3_scsi_regp
);
296 * Our 'bus reset on boot' function
299 static void sun3_scsi_reset_boot(struct Scsi_Host
*instance
)
303 NCR5380_local_declare();
304 NCR5380_setup(instance
);
307 * Do a SCSI reset to clean up the bus during initialization. No
308 * messing with the queues, interrupts, or locks necessary here.
311 printk( "Sun3 SCSI: resetting the SCSI bus..." );
313 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
314 // sun3_disable_irq( IRQ_SUN3_SCSI );
317 NCR5380_write( TARGET_COMMAND_REG
,
318 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG
) ));
321 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
323 /* The min. reset hold time is 25us, so 40us should be enough */
326 /* reset RST and interrupt */
327 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
);
328 NCR5380_read( RESET_PARITY_INTERRUPT_REG
);
330 for( end
= jiffies
+ AFTER_RESET_DELAY
; time_before(jiffies
, end
); )
333 /* switch on SCSI IRQ again */
334 // sun3_enable_irq( IRQ_SUN3_SCSI );
340 static const char * sun3scsi_info (struct Scsi_Host
*spnt
) {
344 // safe bits for the CSR
345 #define CSR_GOOD 0x060f
347 static irqreturn_t
scsi_sun3_intr(int irq
, void *dummy
)
349 unsigned short csr
= dregs
->csr
;
352 dregs
->csr
&= ~CSR_DMA_ENABLE
;
355 #ifdef SUN3_SCSI_DEBUG
356 printk("scsi_intr csr %x\n", csr
);
359 if(csr
& ~CSR_GOOD
) {
360 if(csr
& CSR_DMA_BUSERR
) {
361 printk("scsi%d: bus error in dma\n", default_instance
->host_no
);
362 #ifdef SUN3_SCSI_DEBUG
363 printk("scsi: residual %x count %x addr %p dmaaddr %x\n",
365 dregs
->dma_count_lo
| (dregs
->dma_count_hi
<< 16),
367 dregs
->dma_addr_lo
| (dregs
->dma_addr_hi
<< 16));
371 if(csr
& CSR_DMA_CONFLICT
) {
372 printk("scsi%d: dma conflict\n", default_instance
->host_no
);
377 if(csr
& (CSR_SDB_INT
| CSR_DMA_INT
)) {
378 NCR5380_intr(irq
, dummy
);
382 return IRQ_RETVAL(handled
);
386 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
387 * reentering NCR5380_print_status seems to have ugly side effects
390 /* this doesn't seem to get used at all -- sam */
392 void sun3_sun3_debug (void)
395 NCR5380_local_declare();
397 if (default_instance
) {
398 local_irq_save(flags
);
399 NCR5380_print_status(default_instance
);
400 local_irq_restore(flags
);
406 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
407 static unsigned long sun3scsi_dma_setup(void *data
, unsigned long count
, int write_flag
)
411 if(sun3_dma_orig_addr
!= NULL
)
412 dvma_unmap(sun3_dma_orig_addr
);
414 // addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
415 addr
= (void *)dvma_map_vme((unsigned long) data
, count
);
417 sun3_dma_orig_addr
= addr
;
418 sun3_dma_orig_count
= count
;
420 #ifdef SUN3_SCSI_DEBUG
421 printk("scsi: dma_setup addr %p count %x\n", addr
, count
);
424 // dregs->fifo_count = 0;
427 dregs
->csr
&= ~CSR_FIFO
;
428 dregs
->csr
|= CSR_FIFO
;
432 dregs
->csr
|= CSR_SEND
;
434 dregs
->csr
&= ~CSR_SEND
;
437 // dregs->csr &= ~CSR_FIFO;
438 // dregs->csr |= CSR_FIFO;
440 dregs
->csr
|= CSR_PACK_ENABLE
;
442 dregs
->dma_addr_hi
= ((unsigned long)addr
>> 16);
443 dregs
->dma_addr_lo
= ((unsigned long)addr
& 0xffff);
445 dregs
->dma_count_hi
= 0;
446 dregs
->dma_count_lo
= 0;
447 dregs
->fifo_count_hi
= 0;
448 dregs
->fifo_count
= 0;
450 #ifdef SUN3_SCSI_DEBUG
451 printk("scsi: dma_setup done csr %x\n", dregs
->csr
);
457 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host
*instance
)
459 return last_residual
;
462 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted
,
463 struct scsi_cmnd
*cmd
,
466 if (cmd
->request
->cmd_type
== REQ_TYPE_FS
)
472 static int sun3scsi_dma_start(unsigned long count
, char *data
)
478 #ifdef SUN3_SCSI_DEBUG
479 printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data
, count
, csr
, dregs
->fifo_count
);
482 dregs
->dma_count_hi
= (sun3_dma_orig_count
>> 16);
483 dregs
->dma_count_lo
= (sun3_dma_orig_count
& 0xffff);
485 dregs
->fifo_count_hi
= (sun3_dma_orig_count
>> 16);
486 dregs
->fifo_count
= (sun3_dma_orig_count
& 0xffff);
488 // if(!(csr & CSR_DMA_ENABLE))
489 // dregs->csr |= CSR_DMA_ENABLE;
494 /* clean up after our dma is done */
495 static int sun3scsi_dma_finish(int write_flag
)
502 dregs
->csr
&= ~CSR_DMA_ENABLE
;
504 fifo
= dregs
->fifo_count
;
506 if((fifo
> 0) && (fifo
< sun3_dma_orig_count
))
510 last_residual
= fifo
;
511 #ifdef SUN3_SCSI_DEBUG
512 printk("scsi: residual %x total %x\n", fifo
, sun3_dma_orig_count
);
514 /* empty bytes from the fifo which didn't make it */
515 if((!write_flag
) && (dregs
->csr
& CSR_LEFT
)) {
516 unsigned char *vaddr
;
518 #ifdef SUN3_SCSI_DEBUG
519 printk("scsi: got left over bytes\n");
522 vaddr
= (unsigned char *)dvma_vmetov(sun3_dma_orig_addr
);
524 vaddr
+= (sun3_dma_orig_count
- fifo
);
527 switch(dregs
->csr
& CSR_LEFT
) {
529 *vaddr
= (dregs
->bpack_lo
& 0xff00) >> 8;
533 *vaddr
= (dregs
->bpack_hi
& 0x00ff);
537 *vaddr
= (dregs
->bpack_hi
& 0xff00) >> 8;
544 dvma_unmap(sun3_dma_orig_addr
);
545 sun3_dma_orig_addr
= NULL
;
547 dregs
->dma_addr_hi
= 0;
548 dregs
->dma_addr_lo
= 0;
549 dregs
->dma_count_hi
= 0;
550 dregs
->dma_count_lo
= 0;
552 dregs
->fifo_count
= 0;
553 dregs
->fifo_count_hi
= 0;
555 dregs
->csr
&= ~CSR_SEND
;
557 // dregs->csr |= CSR_DMA_ENABLE;
561 dregs
->csr
&= ~CSR_FIFO
;
562 dregs
->csr
|= CSR_FIFO
;
564 sun3_dma_setup_done
= NULL
;
570 #include "sun3_NCR5380.c"
572 static struct scsi_host_template driver_template
= {
573 .name
= SUN3_SCSI_NAME
,
574 .detect
= sun3scsi_detect
,
575 .release
= sun3scsi_release
,
576 .info
= sun3scsi_info
,
577 .queuecommand
= sun3scsi_queue_command
,
578 .eh_abort_handler
= sun3scsi_abort
,
579 .eh_bus_reset_handler
= sun3scsi_bus_reset
,
580 .can_queue
= CAN_QUEUE
,
582 .sg_tablesize
= SG_TABLESIZE
,
583 .cmd_per_lun
= CMD_PER_LUN
,
584 .use_clustering
= DISABLE_CLUSTERING
588 #include "scsi_module.c"
590 MODULE_LICENSE("GPL");