2 * Generic Macintosh NCR5380 driver
4 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
6 * derived in part from:
9 * Generic Generic NCR5380 driver
11 * Copyright 1995, Russell King
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/blkdev.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
22 #include <asm/hwtest.h>
24 #include <asm/macints.h>
25 #include <asm/setup.h>
27 #include <scsi/scsi_host.h>
29 /* Definitions for the core NCR5380 driver. */
31 #define NCR5380_implementation_fields int pdma_residual
33 #define NCR5380_read(reg) in_8(hostdata->io + ((reg) << 4))
34 #define NCR5380_write(reg, value) out_8(hostdata->io + ((reg) << 4), value)
36 #define NCR5380_dma_xfer_len macscsi_dma_xfer_len
37 #define NCR5380_dma_recv_setup macscsi_pread
38 #define NCR5380_dma_send_setup macscsi_pwrite
39 #define NCR5380_dma_residual macscsi_dma_residual
41 #define NCR5380_intr macscsi_intr
42 #define NCR5380_queue_command macscsi_queue_command
43 #define NCR5380_abort macscsi_abort
44 #define NCR5380_host_reset macscsi_host_reset
45 #define NCR5380_info macscsi_info
49 static int setup_can_queue
= -1;
50 module_param(setup_can_queue
, int, 0);
51 static int setup_cmd_per_lun
= -1;
52 module_param(setup_cmd_per_lun
, int, 0);
53 static int setup_sg_tablesize
= -1;
54 module_param(setup_sg_tablesize
, int, 0);
55 static int setup_use_pdma
= -1;
56 module_param(setup_use_pdma
, int, 0);
57 static int setup_hostid
= -1;
58 module_param(setup_hostid
, int, 0);
59 static int setup_toshiba_delay
= -1;
60 module_param(setup_toshiba_delay
, int, 0);
63 static int __init
mac_scsi_setup(char *str
)
67 (void)get_options(str
, ARRAY_SIZE(ints
), ints
);
70 pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
74 setup_can_queue
= ints
[1];
76 setup_cmd_per_lun
= ints
[2];
78 setup_sg_tablesize
= ints
[3];
80 setup_hostid
= ints
[4];
81 /* ints[5] (use_tagged_queuing) is ignored */
83 setup_use_pdma
= ints
[6];
85 setup_toshiba_delay
= ints
[7];
89 __setup("mac5380=", mac_scsi_setup
);
92 /* Pseudo DMA asm originally by Ove Edlund */
94 #define CP_IO_TO_MEM(s,d,n) \
95 __asm__ __volatile__ \
103 " 1: move.b (%0),(%1)+\n" \
104 " 2: dbf %%d0,1b\n" \
105 " move.w %2,%%d0\n" \
108 " 3: move.l (%0),(%1)+\n" \
109 "31: move.l (%0),(%1)+\n" \
110 "32: move.l (%0),(%1)+\n" \
111 "33: move.l (%0),(%1)+\n" \
112 "34: move.l (%0),(%1)+\n" \
113 "35: move.l (%0),(%1)+\n" \
114 "36: move.l (%0),(%1)+\n" \
115 "37: move.l (%0),(%1)+\n" \
116 " 4: dbf %%d0,3b\n" \
117 " move.w %2,%%d0\n" \
121 " 5: move.l (%0),(%1)+\n" \
122 " 6: dbf %%d0,5b\n" \
125 " 7: move.b (%0),(%1)+\n" \
127 " moveq.l #0, %2\n" \
129 ".section .fixup,\"ax\"\n" \
131 "91: moveq.l #1, %2\n" \
133 "94: moveq.l #4, %2\n" \
136 ".section __ex_table,\"a\"\n" \
150 : "=a"(s), "=a"(d), "=d"(n) \
151 : "0"(s), "1"(d), "2"(n) \
154 static inline int macscsi_pread(struct NCR5380_hostdata
*hostdata
,
155 unsigned char *dst
, int len
)
157 u8 __iomem
*s
= hostdata
->pdma_io
+ (INPUT_DATA_REG
<< 4);
158 unsigned char *d
= dst
;
162 while (!NCR5380_poll_politely(hostdata
, BUS_AND_STATUS_REG
,
163 BASR_DRQ
| BASR_PHASE_MATCH
,
164 BASR_DRQ
| BASR_PHASE_MATCH
, HZ
/ 64)) {
165 CP_IO_TO_MEM(s
, d
, n
);
167 transferred
= d
- dst
- n
;
168 hostdata
->pdma_residual
= len
- transferred
;
174 /* Target changed phase early? */
175 if (NCR5380_poll_politely2(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
,
176 BUS_AND_STATUS_REG
, BASR_ACK
, BASR_ACK
, HZ
/ 64) < 0)
177 scmd_printk(KERN_ERR
, hostdata
->connected
,
178 "%s: !REQ and !ACK\n", __func__
);
179 if (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_PHASE_MATCH
))
182 dsprintk(NDEBUG_PSEUDO_DMA
, hostdata
->host
,
183 "%s: bus error (%d/%d)\n", __func__
, transferred
, len
);
184 NCR5380_dprint(NDEBUG_PSEUDO_DMA
, hostdata
->host
);
185 d
= dst
+ transferred
;
186 n
= len
- transferred
;
189 scmd_printk(KERN_ERR
, hostdata
->connected
,
190 "%s: phase mismatch or !DRQ\n", __func__
);
191 NCR5380_dprint(NDEBUG_PSEUDO_DMA
, hostdata
->host
);
196 #define CP_MEM_TO_IO(s,d,n) \
197 __asm__ __volatile__ \
200 " move.w %0,%%d0\n" \
205 " 1: move.b (%0)+,(%1)\n" \
206 " 2: dbf %%d0,1b\n" \
207 " move.w %2,%%d0\n" \
210 " 3: move.l (%0)+,(%1)\n" \
211 "31: move.l (%0)+,(%1)\n" \
212 "32: move.l (%0)+,(%1)\n" \
213 "33: move.l (%0)+,(%1)\n" \
214 "34: move.l (%0)+,(%1)\n" \
215 "35: move.l (%0)+,(%1)\n" \
216 "36: move.l (%0)+,(%1)\n" \
217 "37: move.l (%0)+,(%1)\n" \
218 " 4: dbf %%d0,3b\n" \
219 " move.w %2,%%d0\n" \
223 " 5: move.l (%0)+,(%1)\n" \
224 " 6: dbf %%d0,5b\n" \
227 " 7: move.b (%0)+,(%1)\n" \
229 " moveq.l #0, %2\n" \
231 ".section .fixup,\"ax\"\n" \
233 "91: moveq.l #1, %2\n" \
235 "94: moveq.l #4, %2\n" \
238 ".section __ex_table,\"a\"\n" \
252 : "=a"(s), "=a"(d), "=d"(n) \
253 : "0"(s), "1"(d), "2"(n) \
256 static inline int macscsi_pwrite(struct NCR5380_hostdata
*hostdata
,
257 unsigned char *src
, int len
)
259 unsigned char *s
= src
;
260 u8 __iomem
*d
= hostdata
->pdma_io
+ (OUTPUT_DATA_REG
<< 4);
264 while (!NCR5380_poll_politely(hostdata
, BUS_AND_STATUS_REG
,
265 BASR_DRQ
| BASR_PHASE_MATCH
,
266 BASR_DRQ
| BASR_PHASE_MATCH
, HZ
/ 64)) {
267 CP_MEM_TO_IO(s
, d
, n
);
269 transferred
= s
- src
- n
;
270 hostdata
->pdma_residual
= len
- transferred
;
272 /* Target changed phase early? */
273 if (NCR5380_poll_politely2(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
,
274 BUS_AND_STATUS_REG
, BASR_ACK
, BASR_ACK
, HZ
/ 64) < 0)
275 scmd_printk(KERN_ERR
, hostdata
->connected
,
276 "%s: !REQ and !ACK\n", __func__
);
277 if (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_PHASE_MATCH
))
282 if (NCR5380_poll_politely(hostdata
, TARGET_COMMAND_REG
,
284 TCR_LAST_BYTE_SENT
, HZ
/ 64) < 0)
285 scmd_printk(KERN_ERR
, hostdata
->connected
,
286 "%s: Last Byte Sent timeout\n", __func__
);
290 dsprintk(NDEBUG_PSEUDO_DMA
, hostdata
->host
,
291 "%s: bus error (%d/%d)\n", __func__
, transferred
, len
);
292 NCR5380_dprint(NDEBUG_PSEUDO_DMA
, hostdata
->host
);
293 s
= src
+ transferred
;
294 n
= len
- transferred
;
297 scmd_printk(KERN_ERR
, hostdata
->connected
,
298 "%s: phase mismatch or !DRQ\n", __func__
);
299 NCR5380_dprint(NDEBUG_PSEUDO_DMA
, hostdata
->host
);
304 static int macscsi_dma_xfer_len(struct NCR5380_hostdata
*hostdata
,
305 struct scsi_cmnd
*cmd
)
307 if (hostdata
->flags
& FLAG_NO_PSEUDO_DMA
||
308 cmd
->SCp
.this_residual
< 16)
311 return cmd
->SCp
.this_residual
;
314 static int macscsi_dma_residual(struct NCR5380_hostdata
*hostdata
)
316 return hostdata
->pdma_residual
;
321 #define DRV_MODULE_NAME "mac_scsi"
322 #define PFX DRV_MODULE_NAME ": "
324 static struct scsi_host_template mac_scsi_template
= {
325 .module
= THIS_MODULE
,
326 .proc_name
= DRV_MODULE_NAME
,
327 .name
= "Macintosh NCR5380 SCSI",
328 .info
= macscsi_info
,
329 .queuecommand
= macscsi_queue_command
,
330 .eh_abort_handler
= macscsi_abort
,
331 .eh_host_reset_handler
= macscsi_host_reset
,
336 .use_clustering
= DISABLE_CLUSTERING
,
337 .cmd_size
= NCR5380_CMD_SIZE
,
341 static int __init
mac_scsi_probe(struct platform_device
*pdev
)
343 struct Scsi_Host
*instance
;
344 struct NCR5380_hostdata
*hostdata
;
347 struct resource
*irq
, *pio_mem
, *pdma_mem
= NULL
;
349 pio_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
353 pdma_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
355 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
357 if (!hwreg_present((unsigned char *)pio_mem
->start
+
358 (STATUS_REG
<< 4))) {
359 pr_info(PFX
"no device detected at %pap\n", &pio_mem
->start
);
363 if (setup_can_queue
> 0)
364 mac_scsi_template
.can_queue
= setup_can_queue
;
365 if (setup_cmd_per_lun
> 0)
366 mac_scsi_template
.cmd_per_lun
= setup_cmd_per_lun
;
367 if (setup_sg_tablesize
>= 0)
368 mac_scsi_template
.sg_tablesize
= setup_sg_tablesize
;
369 if (setup_hostid
>= 0)
370 mac_scsi_template
.this_id
= setup_hostid
& 7;
372 instance
= scsi_host_alloc(&mac_scsi_template
,
373 sizeof(struct NCR5380_hostdata
));
378 instance
->irq
= irq
->start
;
380 instance
->irq
= NO_IRQ
;
382 hostdata
= shost_priv(instance
);
383 hostdata
->base
= pio_mem
->start
;
384 hostdata
->io
= (u8 __iomem
*)pio_mem
->start
;
386 if (pdma_mem
&& setup_use_pdma
)
387 hostdata
->pdma_io
= (u8 __iomem
*)pdma_mem
->start
;
389 host_flags
|= FLAG_NO_PSEUDO_DMA
;
391 host_flags
|= setup_toshiba_delay
> 0 ? FLAG_TOSHIBA_DELAY
: 0;
393 error
= NCR5380_init(instance
, host_flags
| FLAG_LATE_DMA_SETUP
);
397 if (instance
->irq
!= NO_IRQ
) {
398 error
= request_irq(instance
->irq
, macscsi_intr
, IRQF_SHARED
,
399 "NCR5380", instance
);
404 NCR5380_maybe_reset_bus(instance
);
406 error
= scsi_add_host(instance
, NULL
);
410 platform_set_drvdata(pdev
, instance
);
412 scsi_scan_host(instance
);
416 if (instance
->irq
!= NO_IRQ
)
417 free_irq(instance
->irq
, instance
);
419 NCR5380_exit(instance
);
421 scsi_host_put(instance
);
425 static int __exit
mac_scsi_remove(struct platform_device
*pdev
)
427 struct Scsi_Host
*instance
= platform_get_drvdata(pdev
);
429 scsi_remove_host(instance
);
430 if (instance
->irq
!= NO_IRQ
)
431 free_irq(instance
->irq
, instance
);
432 NCR5380_exit(instance
);
433 scsi_host_put(instance
);
437 static struct platform_driver mac_scsi_driver
= {
438 .remove
= __exit_p(mac_scsi_remove
),
440 .name
= DRV_MODULE_NAME
,
444 module_platform_driver_probe(mac_scsi_driver
, mac_scsi_probe
);
446 MODULE_ALIAS("platform:" DRV_MODULE_NAME
);
447 MODULE_LICENSE("GPL");