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 /* minimum number of bytes to do dma on */
40 #define DMA_MIN_SIZE 129
42 /* Definitions for the core NCR5380 driver. */
44 #define NCR5380_implementation_fields /* none */
46 #define NCR5380_read(reg) sun3scsi_read(reg)
47 #define NCR5380_write(reg, value) sun3scsi_write(reg, value)
49 #define NCR5380_queue_command sun3scsi_queue_command
50 #define NCR5380_bus_reset sun3scsi_bus_reset
51 #define NCR5380_abort sun3scsi_abort
52 #define NCR5380_info sun3scsi_info
54 #define NCR5380_dma_recv_setup(instance, data, count) (count)
55 #define NCR5380_dma_send_setup(instance, data, count) (count)
56 #define NCR5380_dma_residual(instance) \
57 sun3scsi_dma_residual(instance)
58 #define NCR5380_dma_xfer_len(instance, cmd, phase) \
59 sun3scsi_dma_xfer_len(cmd->SCp.this_residual, cmd)
61 #define NCR5380_acquire_dma_irq(instance) (1)
62 #define NCR5380_release_dma_irq(instance)
67 extern int sun3_map_test(unsigned long, char *);
69 static int setup_can_queue
= -1;
70 module_param(setup_can_queue
, int, 0);
71 static int setup_cmd_per_lun
= -1;
72 module_param(setup_cmd_per_lun
, int, 0);
73 static int setup_sg_tablesize
= -1;
74 module_param(setup_sg_tablesize
, int, 0);
75 static int setup_hostid
= -1;
76 module_param(setup_hostid
, int, 0);
78 /* ms to wait after hitting dma regs */
79 #define SUN3_DMA_DELAY 10
81 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
82 #define SUN3_DVMA_BUFSIZE 0xe000
84 static struct scsi_cmnd
*sun3_dma_setup_done
;
85 static unsigned char *sun3_scsi_regp
;
86 static volatile struct sun3_dma_regs
*dregs
;
87 static struct sun3_udc_regs
*udc_regs
;
88 static unsigned char *sun3_dma_orig_addr
;
89 static unsigned long sun3_dma_orig_count
;
90 static int sun3_dma_active
;
91 static unsigned long last_residual
;
94 * NCR 5380 register access functions
97 static inline unsigned char sun3scsi_read(int reg
)
99 return in_8(sun3_scsi_regp
+ reg
);
102 static inline void sun3scsi_write(int reg
, int value
)
104 out_8(sun3_scsi_regp
+ reg
, value
);
107 #ifndef SUN3_SCSI_VME
108 /* dma controller register access functions */
110 static inline unsigned short sun3_udc_read(unsigned char reg
)
114 dregs
->udc_addr
= UDC_CSR
;
115 udelay(SUN3_DMA_DELAY
);
116 ret
= dregs
->udc_data
;
117 udelay(SUN3_DMA_DELAY
);
122 static inline void sun3_udc_write(unsigned short val
, unsigned char reg
)
124 dregs
->udc_addr
= reg
;
125 udelay(SUN3_DMA_DELAY
);
126 dregs
->udc_data
= val
;
127 udelay(SUN3_DMA_DELAY
);
131 // safe bits for the CSR
132 #define CSR_GOOD 0x060f
134 static irqreturn_t
scsi_sun3_intr(int irq
, void *dev
)
136 struct Scsi_Host
*instance
= dev
;
137 unsigned short csr
= dregs
->csr
;
141 dregs
->csr
&= ~CSR_DMA_ENABLE
;
144 if(csr
& ~CSR_GOOD
) {
145 if (csr
& CSR_DMA_BUSERR
)
146 shost_printk(KERN_ERR
, instance
, "bus error in DMA\n");
147 if (csr
& CSR_DMA_CONFLICT
)
148 shost_printk(KERN_ERR
, instance
, "DMA conflict\n");
152 if(csr
& (CSR_SDB_INT
| CSR_DMA_INT
)) {
153 NCR5380_intr(irq
, dev
);
157 return IRQ_RETVAL(handled
);
160 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
161 static unsigned long sun3scsi_dma_setup(struct Scsi_Host
*instance
,
162 void *data
, unsigned long count
, int write_flag
)
166 if(sun3_dma_orig_addr
!= NULL
)
167 dvma_unmap(sun3_dma_orig_addr
);
170 addr
= (void *)dvma_map_vme((unsigned long) data
, count
);
172 addr
= (void *)dvma_map((unsigned long) data
, count
);
175 sun3_dma_orig_addr
= addr
;
176 sun3_dma_orig_count
= count
;
178 #ifndef SUN3_SCSI_VME
179 dregs
->fifo_count
= 0;
180 sun3_udc_write(UDC_RESET
, UDC_CSR
);
183 dregs
->csr
&= ~CSR_FIFO
;
184 dregs
->csr
|= CSR_FIFO
;
189 dregs
->csr
|= CSR_SEND
;
191 dregs
->csr
&= ~CSR_SEND
;
194 dregs
->csr
|= CSR_PACK_ENABLE
;
196 dregs
->dma_addr_hi
= ((unsigned long)addr
>> 16);
197 dregs
->dma_addr_lo
= ((unsigned long)addr
& 0xffff);
199 dregs
->dma_count_hi
= 0;
200 dregs
->dma_count_lo
= 0;
201 dregs
->fifo_count_hi
= 0;
202 dregs
->fifo_count
= 0;
204 /* byte count for fifo */
205 dregs
->fifo_count
= count
;
207 sun3_udc_write(UDC_RESET
, UDC_CSR
);
210 dregs
->csr
&= ~CSR_FIFO
;
211 dregs
->csr
|= CSR_FIFO
;
213 if(dregs
->fifo_count
!= count
) {
214 shost_printk(KERN_ERR
, instance
, "FIFO mismatch %04x not %04x\n",
215 dregs
->fifo_count
, (unsigned int) count
);
216 NCR5380_dprint(NDEBUG_DMA
, instance
);
220 udc_regs
->addr_hi
= (((unsigned long)(addr
) & 0xff0000) >> 8);
221 udc_regs
->addr_lo
= ((unsigned long)(addr
) & 0xffff);
222 udc_regs
->count
= count
/2; /* count in words */
223 udc_regs
->mode_hi
= UDC_MODE_HIWORD
;
227 udc_regs
->mode_lo
= UDC_MODE_LSEND
;
228 udc_regs
->rsel
= UDC_RSEL_SEND
;
230 udc_regs
->mode_lo
= UDC_MODE_LRECV
;
231 udc_regs
->rsel
= UDC_RSEL_RECV
;
234 /* announce location of regs block */
235 sun3_udc_write(((dvma_vtob(udc_regs
) & 0xff0000) >> 8),
238 sun3_udc_write((dvma_vtob(udc_regs
) & 0xffff), UDC_CHN_LO
);
240 /* set dma master on */
241 sun3_udc_write(0xd, UDC_MODE
);
243 /* interrupt enable */
244 sun3_udc_write(UDC_INT_ENABLE
, UDC_CSR
);
251 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host
*instance
)
253 return last_residual
;
256 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted_len
,
257 struct scsi_cmnd
*cmd
)
259 if (wanted_len
< DMA_MIN_SIZE
|| cmd
->request
->cmd_type
!= REQ_TYPE_FS
)
265 static inline int sun3scsi_dma_start(unsigned long count
, unsigned char *data
)
272 dregs
->dma_count_hi
= (sun3_dma_orig_count
>> 16);
273 dregs
->dma_count_lo
= (sun3_dma_orig_count
& 0xffff);
275 dregs
->fifo_count_hi
= (sun3_dma_orig_count
>> 16);
276 dregs
->fifo_count
= (sun3_dma_orig_count
& 0xffff);
278 /* if(!(csr & CSR_DMA_ENABLE))
279 * dregs->csr |= CSR_DMA_ENABLE;
282 sun3_udc_write(UDC_CHN_START
, UDC_CSR
);
288 /* clean up after our dma is done */
289 static int sun3scsi_dma_finish(int write_flag
)
291 unsigned short __maybe_unused count
;
298 dregs
->csr
&= ~CSR_DMA_ENABLE
;
300 fifo
= dregs
->fifo_count
;
302 if ((fifo
> 0) && (fifo
< sun3_dma_orig_count
))
306 last_residual
= fifo
;
307 /* empty bytes from the fifo which didn't make it */
308 if ((!write_flag
) && (dregs
->csr
& CSR_LEFT
)) {
309 unsigned char *vaddr
;
311 vaddr
= (unsigned char *)dvma_vmetov(sun3_dma_orig_addr
);
313 vaddr
+= (sun3_dma_orig_count
- fifo
);
316 switch (dregs
->csr
& CSR_LEFT
) {
318 *vaddr
= (dregs
->bpack_lo
& 0xff00) >> 8;
322 *vaddr
= (dregs
->bpack_hi
& 0x00ff);
326 *vaddr
= (dregs
->bpack_hi
& 0xff00) >> 8;
331 // check to empty the fifo on a read
333 int tmo
= 20000; /* .2 sec */
336 if(dregs
->csr
& CSR_FIFO_EMPTY
)
340 printk("sun3scsi: fifo failed to empty!\n");
347 dregs
->udc_addr
= 0x32;
348 udelay(SUN3_DMA_DELAY
);
349 count
= 2 * dregs
->udc_data
;
350 udelay(SUN3_DMA_DELAY
);
352 fifo
= dregs
->fifo_count
;
353 last_residual
= fifo
;
355 /* empty bytes from the fifo which didn't make it */
356 if((!write_flag
) && (count
- fifo
) == 2) {
358 unsigned char *vaddr
;
360 data
= dregs
->fifo_data
;
361 vaddr
= (unsigned char *)dvma_btov(sun3_dma_orig_addr
);
363 vaddr
+= (sun3_dma_orig_count
- fifo
);
365 vaddr
[-2] = (data
& 0xff00) >> 8;
366 vaddr
[-1] = (data
& 0xff);
370 dvma_unmap(sun3_dma_orig_addr
);
371 sun3_dma_orig_addr
= NULL
;
374 dregs
->dma_addr_hi
= 0;
375 dregs
->dma_addr_lo
= 0;
376 dregs
->dma_count_hi
= 0;
377 dregs
->dma_count_lo
= 0;
379 dregs
->fifo_count
= 0;
380 dregs
->fifo_count_hi
= 0;
382 dregs
->csr
&= ~CSR_SEND
;
383 /* dregs->csr |= CSR_DMA_ENABLE; */
385 sun3_udc_write(UDC_RESET
, UDC_CSR
);
386 dregs
->fifo_count
= 0;
387 dregs
->csr
&= ~CSR_SEND
;
390 dregs
->csr
&= ~CSR_FIFO
;
391 dregs
->csr
|= CSR_FIFO
;
394 sun3_dma_setup_done
= NULL
;
403 #define SUN3_SCSI_NAME "Sun3 NCR5380 VME SCSI"
404 #define DRV_MODULE_NAME "sun3_scsi_vme"
406 #define SUN3_SCSI_NAME "Sun3 NCR5380 SCSI"
407 #define DRV_MODULE_NAME "sun3_scsi"
410 #define PFX DRV_MODULE_NAME ": "
412 static struct scsi_host_template sun3_scsi_template
= {
413 .module
= THIS_MODULE
,
414 .proc_name
= DRV_MODULE_NAME
,
415 .name
= SUN3_SCSI_NAME
,
416 .info
= sun3scsi_info
,
417 .queuecommand
= sun3scsi_queue_command
,
418 .eh_abort_handler
= sun3scsi_abort
,
419 .eh_bus_reset_handler
= sun3scsi_bus_reset
,
422 .sg_tablesize
= SG_NONE
,
424 .use_clustering
= DISABLE_CLUSTERING
,
425 .cmd_size
= NCR5380_CMD_SIZE
,
428 static int __init
sun3_scsi_probe(struct platform_device
*pdev
)
430 struct Scsi_Host
*instance
;
432 struct resource
*irq
, *mem
;
433 unsigned char *ioaddr
;
439 if (setup_can_queue
> 0)
440 sun3_scsi_template
.can_queue
= setup_can_queue
;
441 if (setup_cmd_per_lun
> 0)
442 sun3_scsi_template
.cmd_per_lun
= setup_cmd_per_lun
;
443 if (setup_sg_tablesize
>= 0)
444 sun3_scsi_template
.sg_tablesize
= setup_sg_tablesize
;
445 if (setup_hostid
>= 0)
446 sun3_scsi_template
.this_id
= setup_hostid
& 7;
450 for (i
= 0; i
< 2; i
++) {
453 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
);
454 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
458 ioaddr
= sun3_ioremap(mem
->start
, resource_size(mem
),
459 SUN3_PAGE_TYPE_VME16
);
460 dregs
= (struct sun3_dma_regs
*)(ioaddr
+ 8);
462 if (sun3_map_test((unsigned long)dregs
, &x
)) {
463 unsigned short oldcsr
;
467 udelay(SUN3_DMA_DELAY
);
468 if (dregs
->csr
== 0x1400)
480 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
481 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
485 ioaddr
= ioremap(mem
->start
, resource_size(mem
));
486 dregs
= (struct sun3_dma_regs
*)(ioaddr
+ 8);
488 udc_regs
= dvma_malloc(sizeof(struct sun3_udc_regs
));
490 pr_err(PFX
"couldn't allocate DVMA memory!\n");
496 sun3_scsi_regp
= ioaddr
;
498 instance
= scsi_host_alloc(&sun3_scsi_template
,
499 sizeof(struct NCR5380_hostdata
));
505 instance
->io_port
= (unsigned long)ioaddr
;
506 instance
->irq
= irq
->start
;
508 error
= NCR5380_init(instance
, host_flags
);
512 error
= request_irq(instance
->irq
, scsi_sun3_intr
, 0,
513 "NCR5380", instance
);
515 pr_err(PFX
"scsi%d: IRQ %d not free, bailing out\n",
516 instance
->host_no
, instance
->irq
);
521 udelay(SUN3_DMA_DELAY
);
522 dregs
->csr
= CSR_SCSI
| CSR_FIFO
| CSR_INTR
;
523 udelay(SUN3_DMA_DELAY
);
524 dregs
->fifo_count
= 0;
526 dregs
->fifo_count_hi
= 0;
527 dregs
->dma_addr_hi
= 0;
528 dregs
->dma_addr_lo
= 0;
529 dregs
->dma_count_hi
= 0;
530 dregs
->dma_count_lo
= 0;
532 dregs
->ivect
= VME_DATA24
| (instance
->irq
& 0xff);
535 NCR5380_maybe_reset_bus(instance
);
537 error
= scsi_add_host(instance
, NULL
);
541 platform_set_drvdata(pdev
, instance
);
543 scsi_scan_host(instance
);
547 free_irq(instance
->irq
, instance
);
549 NCR5380_exit(instance
);
551 scsi_host_put(instance
);
555 iounmap(sun3_scsi_regp
);
559 static int __exit
sun3_scsi_remove(struct platform_device
*pdev
)
561 struct Scsi_Host
*instance
= platform_get_drvdata(pdev
);
563 scsi_remove_host(instance
);
564 free_irq(instance
->irq
, instance
);
565 NCR5380_exit(instance
);
566 scsi_host_put(instance
);
569 iounmap(sun3_scsi_regp
);
573 static struct platform_driver sun3_scsi_driver
= {
574 .remove
= __exit_p(sun3_scsi_remove
),
576 .name
= DRV_MODULE_NAME
,
580 module_platform_driver_probe(sun3_scsi_driver
, sun3_scsi_probe
);
582 MODULE_ALIAS("platform:" DRV_MODULE_NAME
);
583 MODULE_LICENSE("GPL");