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 * TODO: modify this driver to support multiple Sun3 SCSI VME boards
10 * Adapted from mac_scsinew.c:
13 * Generic Macintosh NCR5380 driver
15 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
17 * derived in part from:
20 * Generic Generic NCR5380 driver
22 * Copyright 1995, Russell King
25 #include <linux/types.h>
26 #include <linux/delay.h>
27 #include <linux/module.h>
28 #include <linux/ioport.h>
29 #include <linux/init.h>
30 #include <linux/blkdev.h>
31 #include <linux/platform_device.h>
36 #include <scsi/scsi_host.h>
37 #include "sun3_scsi.h"
39 /* Definitions for the core NCR5380 driver. */
42 /* #define SUPPORT_TAGS */
43 /* minimum number of bytes to do dma on */
44 #define DMA_MIN_SIZE 129
46 /* #define MAX_TAGS 32 */
48 #define NCR5380_implementation_fields /* none */
50 #define NCR5380_read(reg) sun3scsi_read(reg)
51 #define NCR5380_write(reg, value) sun3scsi_write(reg, value)
53 #define NCR5380_queue_command sun3scsi_queue_command
54 #define NCR5380_bus_reset sun3scsi_bus_reset
55 #define NCR5380_abort sun3scsi_abort
56 #define NCR5380_show_info sun3scsi_show_info
57 #define NCR5380_info sun3scsi_info
59 #define NCR5380_dma_read_setup(instance, data, count) \
60 sun3scsi_dma_setup(data, count, 0)
61 #define NCR5380_dma_write_setup(instance, data, count) \
62 sun3scsi_dma_setup(data, count, 1)
63 #define NCR5380_dma_residual(instance) \
64 sun3scsi_dma_residual(instance)
65 #define NCR5380_dma_xfer_len(instance, cmd, phase) \
66 sun3scsi_dma_xfer_len(cmd->SCp.this_residual, cmd, !((phase) & SR_IO))
68 #define NCR5380_acquire_dma_irq(instance) (1)
69 #define NCR5380_release_dma_irq(instance)
74 extern int sun3_map_test(unsigned long, char *);
76 static int setup_can_queue
= -1;
77 module_param(setup_can_queue
, int, 0);
78 static int setup_cmd_per_lun
= -1;
79 module_param(setup_cmd_per_lun
, int, 0);
80 static int setup_sg_tablesize
= -1;
81 module_param(setup_sg_tablesize
, int, 0);
83 static int setup_use_tagged_queuing
= -1;
84 module_param(setup_use_tagged_queuing
, int, 0);
86 static int setup_hostid
= -1;
87 module_param(setup_hostid
, int, 0);
89 /* #define RESET_BOOT */
91 #define AFTER_RESET_DELAY (HZ/2)
93 /* ms to wait after hitting dma regs */
94 #define SUN3_DMA_DELAY 10
96 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
97 #define SUN3_DVMA_BUFSIZE 0xe000
99 static struct scsi_cmnd
*sun3_dma_setup_done
;
100 static unsigned char *sun3_scsi_regp
;
101 static volatile struct sun3_dma_regs
*dregs
;
102 static struct sun3_udc_regs
*udc_regs
;
103 static unsigned char *sun3_dma_orig_addr
= NULL
;
104 static unsigned long sun3_dma_orig_count
= 0;
105 static int sun3_dma_active
= 0;
106 static unsigned long last_residual
= 0;
107 static struct Scsi_Host
*default_instance
;
110 * NCR 5380 register access functions
113 static inline unsigned char sun3scsi_read(int reg
)
115 return in_8(sun3_scsi_regp
+ reg
);
118 static inline void sun3scsi_write(int reg
, int value
)
120 out_8(sun3_scsi_regp
+ reg
, value
);
123 #ifndef SUN3_SCSI_VME
124 /* dma controller register access functions */
126 static inline unsigned short sun3_udc_read(unsigned char reg
)
130 dregs
->udc_addr
= UDC_CSR
;
131 udelay(SUN3_DMA_DELAY
);
132 ret
= dregs
->udc_data
;
133 udelay(SUN3_DMA_DELAY
);
138 static inline void sun3_udc_write(unsigned short val
, unsigned char reg
)
140 dregs
->udc_addr
= reg
;
141 udelay(SUN3_DMA_DELAY
);
142 dregs
->udc_data
= val
;
143 udelay(SUN3_DMA_DELAY
);
148 static void sun3_scsi_reset_boot(struct Scsi_Host
*instance
)
153 * Do a SCSI reset to clean up the bus during initialization. No
154 * messing with the queues, interrupts, or locks necessary here.
157 printk( "Sun3 SCSI: resetting the SCSI bus..." );
159 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
160 // sun3_disable_irq( IRQ_SUN3_SCSI );
163 NCR5380_write( TARGET_COMMAND_REG
,
164 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG
) ));
167 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
169 /* The min. reset hold time is 25us, so 40us should be enough */
172 /* reset RST and interrupt */
173 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
);
174 NCR5380_read( RESET_PARITY_INTERRUPT_REG
);
176 for( end
= jiffies
+ AFTER_RESET_DELAY
; time_before(jiffies
, end
); )
179 /* switch on SCSI IRQ again */
180 // sun3_enable_irq( IRQ_SUN3_SCSI );
186 // safe bits for the CSR
187 #define CSR_GOOD 0x060f
189 static irqreturn_t
scsi_sun3_intr(int irq
, void *dummy
)
191 unsigned short csr
= dregs
->csr
;
195 dregs
->csr
&= ~CSR_DMA_ENABLE
;
198 if(csr
& ~CSR_GOOD
) {
199 if(csr
& CSR_DMA_BUSERR
) {
200 printk("scsi%d: bus error in dma\n", default_instance
->host_no
);
203 if(csr
& CSR_DMA_CONFLICT
) {
204 printk("scsi%d: dma conflict\n", default_instance
->host_no
);
209 if(csr
& (CSR_SDB_INT
| CSR_DMA_INT
)) {
210 NCR5380_intr(irq
, dummy
);
214 return IRQ_RETVAL(handled
);
218 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
219 * reentering NCR5380_print_status seems to have ugly side effects
222 /* this doesn't seem to get used at all -- sam */
224 void sun3_sun3_debug (void)
228 if (default_instance
) {
229 local_irq_save(flags
);
230 NCR5380_print_status(default_instance
);
231 local_irq_restore(flags
);
237 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
238 static unsigned long sun3scsi_dma_setup(void *data
, unsigned long count
, int write_flag
)
242 if(sun3_dma_orig_addr
!= NULL
)
243 dvma_unmap(sun3_dma_orig_addr
);
246 addr
= (void *)dvma_map_vme((unsigned long) data
, count
);
248 addr
= (void *)dvma_map((unsigned long) data
, count
);
251 sun3_dma_orig_addr
= addr
;
252 sun3_dma_orig_count
= count
;
254 #ifndef SUN3_SCSI_VME
255 dregs
->fifo_count
= 0;
256 sun3_udc_write(UDC_RESET
, UDC_CSR
);
259 dregs
->csr
&= ~CSR_FIFO
;
260 dregs
->csr
|= CSR_FIFO
;
265 dregs
->csr
|= CSR_SEND
;
267 dregs
->csr
&= ~CSR_SEND
;
270 dregs
->csr
|= CSR_PACK_ENABLE
;
272 dregs
->dma_addr_hi
= ((unsigned long)addr
>> 16);
273 dregs
->dma_addr_lo
= ((unsigned long)addr
& 0xffff);
275 dregs
->dma_count_hi
= 0;
276 dregs
->dma_count_lo
= 0;
277 dregs
->fifo_count_hi
= 0;
278 dregs
->fifo_count
= 0;
280 /* byte count for fifo */
281 dregs
->fifo_count
= count
;
283 sun3_udc_write(UDC_RESET
, UDC_CSR
);
286 dregs
->csr
&= ~CSR_FIFO
;
287 dregs
->csr
|= CSR_FIFO
;
289 if(dregs
->fifo_count
!= count
) {
290 printk("scsi%d: fifo_mismatch %04x not %04x\n",
291 default_instance
->host_no
, dregs
->fifo_count
,
292 (unsigned int) count
);
293 NCR5380_dprint(NDEBUG_DMA
, default_instance
);
297 udc_regs
->addr_hi
= (((unsigned long)(addr
) & 0xff0000) >> 8);
298 udc_regs
->addr_lo
= ((unsigned long)(addr
) & 0xffff);
299 udc_regs
->count
= count
/2; /* count in words */
300 udc_regs
->mode_hi
= UDC_MODE_HIWORD
;
304 udc_regs
->mode_lo
= UDC_MODE_LSEND
;
305 udc_regs
->rsel
= UDC_RSEL_SEND
;
307 udc_regs
->mode_lo
= UDC_MODE_LRECV
;
308 udc_regs
->rsel
= UDC_RSEL_RECV
;
311 /* announce location of regs block */
312 sun3_udc_write(((dvma_vtob(udc_regs
) & 0xff0000) >> 8),
315 sun3_udc_write((dvma_vtob(udc_regs
) & 0xffff), UDC_CHN_LO
);
317 /* set dma master on */
318 sun3_udc_write(0xd, UDC_MODE
);
320 /* interrupt enable */
321 sun3_udc_write(UDC_INT_ENABLE
, UDC_CSR
);
328 #ifndef SUN3_SCSI_VME
329 static inline unsigned long sun3scsi_dma_count(struct Scsi_Host
*instance
)
331 unsigned short resid
;
333 dregs
->udc_addr
= 0x32;
334 udelay(SUN3_DMA_DELAY
);
335 resid
= dregs
->udc_data
;
336 udelay(SUN3_DMA_DELAY
);
339 return (unsigned long) resid
;
343 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host
*instance
)
345 return last_residual
;
348 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted
,
349 struct scsi_cmnd
*cmd
,
352 if (cmd
->request
->cmd_type
== REQ_TYPE_FS
)
358 static inline int sun3scsi_dma_start(unsigned long count
, unsigned char *data
)
365 dregs
->dma_count_hi
= (sun3_dma_orig_count
>> 16);
366 dregs
->dma_count_lo
= (sun3_dma_orig_count
& 0xffff);
368 dregs
->fifo_count_hi
= (sun3_dma_orig_count
>> 16);
369 dregs
->fifo_count
= (sun3_dma_orig_count
& 0xffff);
371 /* if(!(csr & CSR_DMA_ENABLE))
372 * dregs->csr |= CSR_DMA_ENABLE;
375 sun3_udc_write(UDC_CHN_START
, UDC_CSR
);
381 /* clean up after our dma is done */
382 static int sun3scsi_dma_finish(int write_flag
)
384 unsigned short __maybe_unused count
;
391 dregs
->csr
&= ~CSR_DMA_ENABLE
;
393 fifo
= dregs
->fifo_count
;
395 if ((fifo
> 0) && (fifo
< sun3_dma_orig_count
))
399 last_residual
= fifo
;
400 /* empty bytes from the fifo which didn't make it */
401 if ((!write_flag
) && (dregs
->csr
& CSR_LEFT
)) {
402 unsigned char *vaddr
;
404 vaddr
= (unsigned char *)dvma_vmetov(sun3_dma_orig_addr
);
406 vaddr
+= (sun3_dma_orig_count
- fifo
);
409 switch (dregs
->csr
& CSR_LEFT
) {
411 *vaddr
= (dregs
->bpack_lo
& 0xff00) >> 8;
415 *vaddr
= (dregs
->bpack_hi
& 0x00ff);
419 *vaddr
= (dregs
->bpack_hi
& 0xff00) >> 8;
424 // check to empty the fifo on a read
426 int tmo
= 20000; /* .2 sec */
429 if(dregs
->csr
& CSR_FIFO_EMPTY
)
433 printk("sun3scsi: fifo failed to empty!\n");
440 count
= sun3scsi_dma_count(default_instance
);
442 fifo
= dregs
->fifo_count
;
443 last_residual
= fifo
;
445 /* empty bytes from the fifo which didn't make it */
446 if((!write_flag
) && (count
- fifo
) == 2) {
448 unsigned char *vaddr
;
450 data
= dregs
->fifo_data
;
451 vaddr
= (unsigned char *)dvma_btov(sun3_dma_orig_addr
);
453 vaddr
+= (sun3_dma_orig_count
- fifo
);
455 vaddr
[-2] = (data
& 0xff00) >> 8;
456 vaddr
[-1] = (data
& 0xff);
460 dvma_unmap(sun3_dma_orig_addr
);
461 sun3_dma_orig_addr
= NULL
;
464 dregs
->dma_addr_hi
= 0;
465 dregs
->dma_addr_lo
= 0;
466 dregs
->dma_count_hi
= 0;
467 dregs
->dma_count_lo
= 0;
469 dregs
->fifo_count
= 0;
470 dregs
->fifo_count_hi
= 0;
472 dregs
->csr
&= ~CSR_SEND
;
473 /* dregs->csr |= CSR_DMA_ENABLE; */
475 sun3_udc_write(UDC_RESET
, UDC_CSR
);
476 dregs
->fifo_count
= 0;
477 dregs
->csr
&= ~CSR_SEND
;
480 dregs
->csr
&= ~CSR_FIFO
;
481 dregs
->csr
|= CSR_FIFO
;
484 sun3_dma_setup_done
= NULL
;
490 #include "atari_NCR5380.c"
493 #define SUN3_SCSI_NAME "Sun3 NCR5380 VME SCSI"
494 #define DRV_MODULE_NAME "sun3_scsi_vme"
496 #define SUN3_SCSI_NAME "Sun3 NCR5380 SCSI"
497 #define DRV_MODULE_NAME "sun3_scsi"
500 #define PFX DRV_MODULE_NAME ": "
502 static struct scsi_host_template sun3_scsi_template
= {
503 .module
= THIS_MODULE
,
504 .proc_name
= DRV_MODULE_NAME
,
505 .show_info
= sun3scsi_show_info
,
506 .name
= SUN3_SCSI_NAME
,
507 .info
= sun3scsi_info
,
508 .queuecommand
= sun3scsi_queue_command
,
509 .eh_abort_handler
= sun3scsi_abort
,
510 .eh_bus_reset_handler
= sun3scsi_bus_reset
,
513 .sg_tablesize
= SG_NONE
,
515 .use_clustering
= DISABLE_CLUSTERING
518 static int __init
sun3_scsi_probe(struct platform_device
*pdev
)
520 struct Scsi_Host
*instance
;
522 struct resource
*irq
, *mem
;
523 unsigned char *ioaddr
;
529 if (setup_can_queue
> 0)
530 sun3_scsi_template
.can_queue
= setup_can_queue
;
531 if (setup_cmd_per_lun
> 0)
532 sun3_scsi_template
.cmd_per_lun
= setup_cmd_per_lun
;
533 if (setup_sg_tablesize
>= 0)
534 sun3_scsi_template
.sg_tablesize
= setup_sg_tablesize
;
535 if (setup_hostid
>= 0)
536 sun3_scsi_template
.this_id
= setup_hostid
& 7;
540 for (i
= 0; i
< 2; i
++) {
543 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
);
544 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
548 ioaddr
= sun3_ioremap(mem
->start
, resource_size(mem
),
549 SUN3_PAGE_TYPE_VME16
);
550 dregs
= (struct sun3_dma_regs
*)(ioaddr
+ 8);
552 if (sun3_map_test((unsigned long)dregs
, &x
)) {
553 unsigned short oldcsr
;
557 udelay(SUN3_DMA_DELAY
);
558 if (dregs
->csr
== 0x1400)
570 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
571 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
575 ioaddr
= ioremap(mem
->start
, resource_size(mem
));
576 dregs
= (struct sun3_dma_regs
*)(ioaddr
+ 8);
578 udc_regs
= dvma_malloc(sizeof(struct sun3_udc_regs
));
580 pr_err(PFX
"couldn't allocate DVMA memory!\n");
586 sun3_scsi_regp
= ioaddr
;
588 instance
= scsi_host_alloc(&sun3_scsi_template
,
589 sizeof(struct NCR5380_hostdata
));
594 default_instance
= instance
;
596 instance
->io_port
= (unsigned long)ioaddr
;
597 instance
->irq
= irq
->start
;
600 host_flags
|= setup_use_tagged_queuing
> 0 ? FLAG_TAGGED_QUEUING
: 0;
603 NCR5380_init(instance
, host_flags
);
605 error
= request_irq(instance
->irq
, scsi_sun3_intr
, 0,
606 "NCR5380", instance
);
609 pr_err(PFX
"scsi%d: IRQ %d not free, bailing out\n",
610 instance
->host_no
, instance
->irq
);
613 pr_warn(PFX
"scsi%d: IRQ %d not free, interrupts disabled\n",
614 instance
->host_no
, instance
->irq
);
615 instance
->irq
= NO_IRQ
;
620 udelay(SUN3_DMA_DELAY
);
621 dregs
->csr
= CSR_SCSI
| CSR_FIFO
| CSR_INTR
;
622 udelay(SUN3_DMA_DELAY
);
623 dregs
->fifo_count
= 0;
625 dregs
->fifo_count_hi
= 0;
626 dregs
->dma_addr_hi
= 0;
627 dregs
->dma_addr_lo
= 0;
628 dregs
->dma_count_hi
= 0;
629 dregs
->dma_count_lo
= 0;
631 dregs
->ivect
= VME_DATA24
| (instance
->irq
& 0xff);
635 sun3_scsi_reset_boot(instance
);
638 error
= scsi_add_host(instance
, NULL
);
642 platform_set_drvdata(pdev
, instance
);
644 scsi_scan_host(instance
);
648 if (instance
->irq
!= NO_IRQ
)
649 free_irq(instance
->irq
, instance
);
651 NCR5380_exit(instance
);
652 scsi_host_put(instance
);
656 iounmap(sun3_scsi_regp
);
660 static int __exit
sun3_scsi_remove(struct platform_device
*pdev
)
662 struct Scsi_Host
*instance
= platform_get_drvdata(pdev
);
664 scsi_remove_host(instance
);
665 if (instance
->irq
!= NO_IRQ
)
666 free_irq(instance
->irq
, instance
);
667 NCR5380_exit(instance
);
668 scsi_host_put(instance
);
671 iounmap(sun3_scsi_regp
);
675 static struct platform_driver sun3_scsi_driver
= {
676 .remove
= __exit_p(sun3_scsi_remove
),
678 .name
= DRV_MODULE_NAME
,
679 .owner
= THIS_MODULE
,
683 module_platform_driver_probe(sun3_scsi_driver
, sun3_scsi_probe
);
685 MODULE_ALIAS("platform:" DRV_MODULE_NAME
);
686 MODULE_LICENSE("GPL");