2 * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
4 * Sun3 DMA routines added by Sam Creasey (sammy@oh.verio.com)
6 * Adapted from mac_scsinew.c:
9 * Generic Macintosh NCR5380 driver
11 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
13 * derived in part from:
16 * Generic Generic NCR5380 driver
18 * Copyright 1995, Russell King
22 * For more information, please consult
25 * SCSI Protocol Controller
28 * NCR Microelectronics
29 * 1635 Aeroplaza Drive
30 * Colorado Springs, CO 80916
37 * This is from mac_scsi.h, but hey, maybe this is usefull for Sun3 too! :)
41 * PARITY - enable parity checking. Not supported.
43 * SCSI2 - enable support for SCSI-II tagged queueing. Untested.
45 * USLEEP - enable support for devices that don't disconnect. Untested.
49 * $Log: sun3_NCR5380.c,v $
54 #include <linux/types.h>
55 #include <linux/stddef.h>
56 #include <linux/ctype.h>
57 #include <linux/delay.h>
59 #include <linux/module.h>
60 #include <linux/signal.h>
61 #include <linux/sched.h>
62 #include <linux/ioport.h>
63 #include <linux/init.h>
64 #include <linux/blk.h>
67 #include <asm/system.h>
69 #include <asm/sun3ints.h>
76 #include "sun3_scsi.h"
78 #include "constants.h"
87 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
96 #define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
99 static void scsi_sun3_intr(int irq
, void *dummy
, struct pt_regs
*fp
);
100 static inline unsigned char sun3scsi_read(int reg
);
101 static inline void sun3scsi_write(int reg
, int value
);
103 static int setup_can_queue
= -1;
104 static int setup_cmd_per_lun
= -1;
105 static int setup_sg_tablesize
= -1;
107 static int setup_use_tagged_queuing
= -1;
109 static int setup_hostid
= -1;
111 static Scsi_Cmnd
*sun3_dma_setup_done
= NULL
;
113 #define AFTER_RESET_DELAY (HZ/2)
115 /* ms to wait after hitting dma regs */
116 #define SUN3_DMA_DELAY 5
118 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
119 #define SUN3_DVMA_BUFSIZE 0xe000
121 /* minimum number of bytes to to dma on */
122 #define SUN3_DMA_MINSIZE 128
124 static volatile unsigned char *sun3_scsi_regp
;
125 static volatile struct sun3_dma_regs
*dregs
;
126 static unsigned char *dmabuf
= NULL
; /* dma memory buffer */
127 static struct sun3_udc_regs
*udc_regs
= NULL
;
128 static void *sun3_dma_orig_addr
= NULL
;
129 static unsigned long sun3_dma_orig_count
= 0;
130 static int sun3_dma_active
= 0;
133 * NCR 5380 register access functions
136 static inline unsigned char sun3scsi_read(int reg
)
138 return( sun3_scsi_regp
[reg
] );
141 static inline void sun3scsi_write(int reg
, int value
)
143 sun3_scsi_regp
[reg
] = value
;
146 /* dma controller register access functions */
148 static inline unsigned short sun3_udc_read(unsigned char reg
)
152 dregs
->udc_addr
= UDC_CSR
;
153 udelay(SUN3_DMA_DELAY
);
154 ret
= dregs
->udc_data
;
155 udelay(SUN3_DMA_DELAY
);
160 static inline void sun3_udc_write(unsigned short val
, unsigned char reg
)
162 dregs
->udc_addr
= reg
;
163 udelay(SUN3_DMA_DELAY
);
164 dregs
->udc_data
= val
;
165 udelay(SUN3_DMA_DELAY
);
171 static struct Scsi_Host
*default_instance
;
174 * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
176 * Purpose : initializes mac NCR5380 driver based on the
177 * command line / compile time port and irq definitions.
179 * Inputs : tpnt - template for this SCSI adapter.
181 * Returns : 1 if a host adapter was found, 0 if not.
185 int sun3scsi_detect(Scsi_Host_Template
* tpnt
)
187 unsigned long ioaddr
, iopte
;
189 static int called
= 0;
190 struct Scsi_Host
*instance
;
195 tpnt
->proc_name
= "Sun3 5380 SCSI";
197 /* setup variables */
199 (setup_can_queue
> 0) ? setup_can_queue
: CAN_QUEUE
;
201 (setup_cmd_per_lun
> 0) ? setup_cmd_per_lun
: CMD_PER_LUN
;
203 (setup_sg_tablesize
>= 0) ? setup_sg_tablesize
: SG_TABLESIZE
;
205 if (setup_hostid
>= 0)
206 tpnt
->this_id
= setup_hostid
;
208 /* use 7 as default */
212 /* Taken from Sammy's lance driver: */
213 /* IOBASE_SUN3_SCSI can be found within the IO pmeg with some effort */
214 for(ioaddr
= 0xfe00000; ioaddr
< (0xfe00000 + SUN3_PMEG_SIZE
);
215 ioaddr
+= SUN3_PTE_SIZE
) {
217 iopte
= sun3_get_pte(ioaddr
);
218 if(!(iopte
& SUN3_PAGE_TYPE_IO
)) /* this an io page? */
221 if(((iopte
& SUN3_PAGE_PGNUM_MASK
) << PAGE_SHIFT
) ==
229 printk("No Sun3 NCR5380 found!\n");
233 sun3_scsi_regp
= (unsigned char *)ioaddr
;
234 dregs
= (struct sun3_dma_regs
*)(((unsigned char *)ioaddr
) + 8);
236 if((dmabuf
= sun3_dvma_malloc(SUN3_DVMA_BUFSIZE
)) == NULL
) {
237 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
241 if((udc_regs
= sun3_dvma_malloc(sizeof(struct sun3_udc_regs
)))
243 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
248 if (setup_use_tagged_queuing
< 0)
249 setup_use_tagged_queuing
= DEFAULT_USE_TAGGED_QUEUING
;
252 instance
= scsi_register (tpnt
, sizeof(struct NCR5380_hostdata
));
256 default_instance
= instance
;
258 instance
->io_port
= (unsigned long) ioaddr
;
259 instance
->irq
= IRQ_SUN3_SCSI
;
261 NCR5380_init(instance
, 0);
263 instance
->n_io_port
= 32;
265 ((struct NCR5380_hostdata
*)instance
->hostdata
)->ctrl
= 0;
267 if (request_irq(instance
->irq
, scsi_sun3_intr
,
268 0, "Sun3SCSI-5380", NULL
)) {
270 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
271 instance
->host_no
, instance
->irq
);
272 instance
->irq
= IRQ_NONE
;
274 printk("scsi%d: IRQ%d not free, bailing out\n",
275 instance
->host_no
, instance
->irq
);
280 printk("scsi%d: Sun3 5380 at port %lX irq", instance
->host_no
, instance
->io_port
);
281 if (instance
->irq
== IRQ_NONE
)
282 printk ("s disabled");
284 printk (" %d", instance
->irq
);
285 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
286 instance
->can_queue
, instance
->cmd_per_lun
,
287 SUN3SCSI_PUBLIC_RELEASE
);
288 printk("\nscsi%d:", instance
->host_no
);
289 NCR5380_print_options(instance
);
293 udelay(SUN3_DMA_DELAY
);
294 dregs
->csr
= CSR_SCSI
| CSR_FIFO
| CSR_INTR
;
295 udelay(SUN3_DMA_DELAY
);
296 dregs
->fifo_count
= 0;
302 int sun3scsi_release (struct Scsi_Host
*shpnt
)
304 if (shpnt
->irq
!= IRQ_NONE
)
305 free_irq (shpnt
->irq
, NULL
);
312 * Our 'bus reset on boot' function
315 static void sun3_scsi_reset_boot(struct Scsi_Host
*instance
)
319 NCR5380_local_declare();
320 NCR5380_setup(instance
);
323 * Do a SCSI reset to clean up the bus during initialization. No
324 * messing with the queues, interrupts, or locks necessary here.
327 printk( "Sun3 SCSI: resetting the SCSI bus..." );
329 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
330 sun3_disable_irq( IRQ_SUN3_SCSI
);
333 NCR5380_write( TARGET_COMMAND_REG
,
334 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG
) ));
337 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
339 /* The min. reset hold time is 25us, so 40us should be enough */
342 /* reset RST and interrupt */
343 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
);
344 NCR5380_read( RESET_PARITY_INTERRUPT_REG
);
346 for( end
= jiffies
+ AFTER_RESET_DELAY
; jiffies
< end
; )
349 /* switch on SCSI IRQ again */
350 sun3_enable_irq( IRQ_SUN3_SCSI
);
356 const char * sun3scsi_info (struct Scsi_Host
*spnt
) {
360 // safe bits for the CSR
361 #define CSR_GOOD 0x060f
363 static void scsi_sun3_intr(int irq
, void *dummy
, struct pt_regs
*fp
)
365 unsigned short csr
= dregs
->csr
;
367 if(csr
& ~CSR_GOOD
) {
368 if(csr
& CSR_DMA_BUSERR
) {
369 printk("scsi%d: bus error in dma\n", default_instance
->host_no
);
372 if(csr
& CSR_DMA_CONFLICT
) {
373 printk("scsi%d: dma conflict\n", default_instance
->host_no
);
377 if(csr
& (CSR_SDB_INT
| CSR_DMA_INT
))
378 NCR5380_intr(irq
, dummy
, fp
);
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
) {
396 NCR5380_print_status(default_instance
);
397 restore_flags(flags
);
403 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
404 static unsigned long sun3scsi_dma_setup(void *data
, unsigned long count
, int write_flag
)
407 memcpy(dmabuf
, data
, count
);
409 sun3_dma_orig_addr
= data
;
410 sun3_dma_orig_count
= count
;
413 dregs
->fifo_count
= 0;
414 sun3_udc_write(UDC_RESET
, UDC_CSR
);
417 dregs
->csr
&= ~CSR_FIFO
;
418 dregs
->csr
|= CSR_FIFO
;
422 dregs
->csr
|= CSR_SEND
;
424 dregs
->csr
&= ~CSR_SEND
;
426 /* byte count for fifo */
427 dregs
->fifo_count
= count
;
429 sun3_udc_write(UDC_RESET
, UDC_CSR
);
432 dregs
->csr
&= ~CSR_FIFO
;
433 dregs
->csr
|= CSR_FIFO
;
436 if(dregs
->fifo_count
!= count
) {
437 printk("scsi%d: fifo_mismatch %04x not %04x\n",
438 default_instance
->host_no
, dregs
->fifo_count
,
439 (unsigned int) count
);
440 NCR5380_print(default_instance
);
444 udc_regs
->addr_hi
= ((sun3_dvma_vtop(dmabuf
) & 0xff0000) >> 8);
445 udc_regs
->addr_lo
= (sun3_dvma_vtop(dmabuf
) & 0xffff);
446 udc_regs
->count
= count
/2; /* count in words */
447 udc_regs
->mode_hi
= UDC_MODE_HIWORD
;
451 udc_regs
->mode_lo
= UDC_MODE_LSEND
;
452 udc_regs
->rsel
= UDC_RSEL_SEND
;
454 udc_regs
->mode_lo
= UDC_MODE_LRECV
;
455 udc_regs
->rsel
= UDC_RSEL_RECV
;
458 /* announce location of regs block */
459 sun3_udc_write(((sun3_dvma_vtop(udc_regs
) & 0xff0000) >> 8),
462 sun3_udc_write((sun3_dvma_vtop(udc_regs
) & 0xffff), UDC_CHN_LO
);
464 /* set dma master on */
465 sun3_udc_write(0xd, UDC_MODE
);
467 /* interrupt enable */
468 sun3_udc_write(UDC_INT_ENABLE
, UDC_CSR
);
474 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host
*instance
)
476 unsigned short resid
;
478 dregs
->udc_addr
= 0x32;
479 udelay(SUN3_DMA_DELAY
);
480 resid
= dregs
->udc_data
;
481 udelay(SUN3_DMA_DELAY
);
484 return (unsigned long) resid
;
487 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted
, Scsi_Cmnd
*cmd
,
493 /* clean up after our dma is done */
494 static int sun3scsi_dma_finish(void)
496 unsigned short count
;
499 count
= sun3scsi_dma_residual(default_instance
);
503 /* if we've finished a read, copy out the data we read */
504 if(sun3_dma_orig_addr
) {
505 /* check for residual bytes after dma end */
506 if(count
&& (NCR5380_read(BUS_AND_STATUS_REG
) &
507 (BASR_PHASE_MATCH
| BASR_ACK
))) {
508 printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance
->host_no
);
509 printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG
));
513 /* copy in what we dma'd no matter what */
514 memcpy(sun3_dma_orig_addr
, dmabuf
, sun3_dma_orig_count
);
515 sun3_dma_orig_addr
= NULL
;
519 sun3_udc_write(UDC_RESET
, UDC_CSR
);
521 dregs
->csr
&= ~CSR_SEND
;
524 dregs
->csr
&= ~CSR_FIFO
;
525 dregs
->csr
|= CSR_FIFO
;
527 sun3_dma_setup_done
= NULL
;
533 #include "sun3_NCR5380.c"
535 static Scsi_Host_Template driver_template
= SUN3_NCR5380
;
537 #include "scsi_module.c"