2 * SCSI low-level driver for the 53c94 SCSI bus adaptor found
3 * on Power Macintosh computers, controlling the external SCSI chain.
4 * We assume the 53c94 is connected to a DBDMA (descriptor-based DMA)
7 * Paul Mackerras, August 1996.
8 * Copyright (C) 1996 Paul Mackerras.
10 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/blkdev.h>
16 #include <linux/proc_fs.h>
17 #include <linux/stat.h>
18 #include <linux/spinlock.h>
19 #include <linux/interrupt.h>
20 #include <asm/dbdma.h>
22 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/pci-bridge.h>
26 #include <asm/macio.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
44 struct mac53c94_regs __iomem
*regs
;
46 struct dbdma_regs __iomem
*dma
;
49 struct Scsi_Host
*host
;
50 struct scsi_cmnd
*request_q
;
51 struct scsi_cmnd
*request_qtail
;
52 struct scsi_cmnd
*current_req
; /* req we're currently working on */
53 enum fsc_phase phase
; /* what we're currently trying to do */
54 struct dbdma_cmd
*dma_cmds
; /* space for dbdma commands, aligned */
58 struct macio_dev
*mdev
;
61 static void mac53c94_init(struct fsc_state
*);
62 static void mac53c94_start(struct fsc_state
*);
63 static void mac53c94_interrupt(int, void *);
64 static irqreturn_t
do_mac53c94_interrupt(int, void *);
65 static void cmd_done(struct fsc_state
*, int result
);
66 static void set_dma_cmds(struct fsc_state
*, struct scsi_cmnd
*);
69 static int mac53c94_queue(struct scsi_cmnd
*cmd
, void (*done
)(struct scsi_cmnd
*))
71 struct fsc_state
*state
;
74 cmd
->scsi_done
= done
;
75 cmd
->host_scribble
= NULL
;
77 state
= (struct fsc_state
*) cmd
->device
->host
->hostdata
;
79 if (state
->request_q
== NULL
)
80 state
->request_q
= cmd
;
82 state
->request_qtail
->host_scribble
= (void *) cmd
;
83 state
->request_qtail
= cmd
;
85 if (state
->phase
== idle
)
86 mac53c94_start(state
);
91 static int mac53c94_host_reset(struct scsi_cmnd
*cmd
)
93 struct fsc_state
*state
= (struct fsc_state
*) cmd
->device
->host
->hostdata
;
94 struct mac53c94_regs __iomem
*regs
= state
->regs
;
95 struct dbdma_regs __iomem
*dma
= state
->dma
;
98 spin_lock_irqsave(cmd
->device
->host
->host_lock
, flags
);
100 writel((RUN
|PAUSE
|FLUSH
|WAKE
) << 16, &dma
->control
);
101 writeb(CMD_SCSI_RESET
, ®s
->command
); /* assert RST */
102 udelay(100); /* leave it on for a while (>= 25us) */
103 writeb(CMD_RESET
, ®s
->command
);
105 mac53c94_init(state
);
106 writeb(CMD_NOP
, ®s
->command
);
108 spin_unlock_irqrestore(cmd
->device
->host
->host_lock
, flags
);
112 static void mac53c94_init(struct fsc_state
*state
)
114 struct mac53c94_regs __iomem
*regs
= state
->regs
;
115 struct dbdma_regs __iomem
*dma
= state
->dma
;
118 writeb(state
->host
->this_id
| CF1_PAR_ENABLE
, ®s
->config1
);
119 writeb(TIMO_VAL(250), ®s
->sel_timeout
); /* 250ms */
120 writeb(CLKF_VAL(state
->clk_freq
), ®s
->clk_factor
);
121 writeb(CF2_FEATURE_EN
, ®s
->config2
);
122 writeb(0, ®s
->config3
);
123 writeb(0, ®s
->sync_period
);
124 writeb(0, ®s
->sync_offset
);
125 x
= readb(®s
->interrupt
);
126 writel((RUN
|PAUSE
|FLUSH
|WAKE
) << 16, &dma
->control
);
130 * Start the next command for a 53C94.
131 * Should be called with interrupts disabled.
133 static void mac53c94_start(struct fsc_state
*state
)
135 struct scsi_cmnd
*cmd
;
136 struct mac53c94_regs __iomem
*regs
= state
->regs
;
139 if (state
->phase
!= idle
|| state
->current_req
!= NULL
)
140 panic("inappropriate mac53c94_start (state=%p)", state
);
141 if (state
->request_q
== NULL
)
143 state
->current_req
= cmd
= state
->request_q
;
144 state
->request_q
= (struct scsi_cmnd
*) cmd
->host_scribble
;
147 writeb(0, ®s
->count_lo
);
148 writeb(0, ®s
->count_mid
);
149 writeb(0, ®s
->count_hi
);
150 writeb(CMD_NOP
+ CMD_DMA_MODE
, ®s
->command
);
152 writeb(CMD_FLUSH
, ®s
->command
);
154 writeb(cmd
->device
->id
, ®s
->dest_id
);
155 writeb(0, ®s
->sync_period
);
156 writeb(0, ®s
->sync_offset
);
158 /* load the command into the FIFO */
159 for (i
= 0; i
< cmd
->cmd_len
; ++i
)
160 writeb(cmd
->cmnd
[i
], ®s
->fifo
);
162 writeb(CMD_SELECT
, ®s
->command
);
163 state
->phase
= selecting
;
165 set_dma_cmds(state
, cmd
);
168 static irqreturn_t
do_mac53c94_interrupt(int irq
, void *dev_id
)
171 struct Scsi_Host
*dev
= ((struct fsc_state
*) dev_id
)->current_req
->device
->host
;
173 spin_lock_irqsave(dev
->host_lock
, flags
);
174 mac53c94_interrupt(irq
, dev_id
);
175 spin_unlock_irqrestore(dev
->host_lock
, flags
);
179 static void mac53c94_interrupt(int irq
, void *dev_id
)
181 struct fsc_state
*state
= (struct fsc_state
*) dev_id
;
182 struct mac53c94_regs __iomem
*regs
= state
->regs
;
183 struct dbdma_regs __iomem
*dma
= state
->dma
;
184 struct scsi_cmnd
*cmd
= state
->current_req
;
185 int nb
, stat
, seq
, intr
;
186 static int mac53c94_errors
;
189 * Apparently, reading the interrupt register unlatches
190 * the status and sequence step registers.
192 seq
= readb(®s
->seqstep
);
193 stat
= readb(®s
->status
);
194 intr
= readb(®s
->interrupt
);
197 if (intr
& INTR_RESET
) {
198 /* SCSI bus was reset */
199 printk(KERN_INFO
"external SCSI bus reset detected\n");
200 writeb(CMD_NOP
, ®s
->command
);
201 writel(RUN
<< 16, &dma
->control
); /* stop dma */
202 cmd_done(state
, DID_RESET
<< 16);
205 if (intr
& INTR_ILL_CMD
) {
206 printk(KERN_ERR
"53c94: invalid cmd, intr=%x stat=%x seq=%x phase=%d\n",
207 intr
, stat
, seq
, state
->phase
);
208 cmd_done(state
, DID_ERROR
<< 16);
211 if (stat
& STAT_ERROR
) {
213 writeb(CMD_NOP
+ CMD_DMA_MODE
, ®s
->command
);
216 printk(KERN_DEBUG
"53c94: interrupt with no command active?\n");
219 if (stat
& STAT_PARITY
) {
220 printk(KERN_ERR
"mac53c94: parity error\n");
221 cmd_done(state
, DID_PARITY
<< 16);
224 switch (state
->phase
) {
226 if (intr
& INTR_DISCONNECT
) {
227 /* selection timed out */
228 cmd_done(state
, DID_BAD_TARGET
<< 16);
231 if (intr
!= INTR_BUS_SERV
+ INTR_DONE
) {
232 printk(KERN_DEBUG
"got intr %x during selection\n", intr
);
233 cmd_done(state
, DID_ERROR
<< 16);
236 if ((seq
& SS_MASK
) != SS_DONE
) {
237 printk(KERN_DEBUG
"seq step %x after command\n", seq
);
238 cmd_done(state
, DID_ERROR
<< 16);
241 writeb(CMD_NOP
, ®s
->command
);
242 /* set DMA controller going if any data to transfer */
243 if ((stat
& (STAT_MSG
|STAT_CD
)) == 0
244 && (scsi_sg_count(cmd
) > 0 || scsi_bufflen(cmd
))) {
245 nb
= cmd
->SCp
.this_residual
;
248 cmd
->SCp
.this_residual
-= nb
;
249 writeb(nb
, ®s
->count_lo
);
250 writeb(nb
>> 8, ®s
->count_mid
);
251 writeb(CMD_DMA_MODE
+ CMD_NOP
, ®s
->command
);
252 writel(virt_to_phys(state
->dma_cmds
), &dma
->cmdptr
);
253 writel((RUN
<< 16) | RUN
, &dma
->control
);
254 writeb(CMD_DMA_MODE
+ CMD_XFER_DATA
, ®s
->command
);
255 state
->phase
= dataing
;
257 } else if ((stat
& STAT_PHASE
) == STAT_CD
+ STAT_IO
) {
258 /* up to status phase already */
259 writeb(CMD_I_COMPLETE
, ®s
->command
);
260 state
->phase
= completing
;
262 printk(KERN_DEBUG
"in unexpected phase %x after cmd\n",
264 cmd_done(state
, DID_ERROR
<< 16);
270 if (intr
!= INTR_BUS_SERV
) {
271 printk(KERN_DEBUG
"got intr %x before status\n", intr
);
272 cmd_done(state
, DID_ERROR
<< 16);
275 if (cmd
->SCp
.this_residual
!= 0
276 && (stat
& (STAT_MSG
|STAT_CD
)) == 0) {
277 /* Set up the count regs to transfer more */
278 nb
= cmd
->SCp
.this_residual
;
281 cmd
->SCp
.this_residual
-= nb
;
282 writeb(nb
, ®s
->count_lo
);
283 writeb(nb
>> 8, ®s
->count_mid
);
284 writeb(CMD_DMA_MODE
+ CMD_NOP
, ®s
->command
);
285 writeb(CMD_DMA_MODE
+ CMD_XFER_DATA
, ®s
->command
);
288 if ((stat
& STAT_PHASE
) != STAT_CD
+ STAT_IO
) {
289 printk(KERN_DEBUG
"intr %x before data xfer complete\n", intr
);
291 writel(RUN
<< 16, &dma
->control
); /* stop dma */
293 /* should check dma status */
294 writeb(CMD_I_COMPLETE
, ®s
->command
);
295 state
->phase
= completing
;
298 if (intr
!= INTR_DONE
) {
299 printk(KERN_DEBUG
"got intr %x on completion\n", intr
);
300 cmd_done(state
, DID_ERROR
<< 16);
303 cmd
->SCp
.Status
= readb(®s
->fifo
);
304 cmd
->SCp
.Message
= readb(®s
->fifo
);
305 cmd
->result
= CMD_ACCEPT_MSG
;
306 writeb(CMD_ACCEPT_MSG
, ®s
->command
);
307 state
->phase
= busfreeing
;
310 if (intr
!= INTR_DISCONNECT
) {
311 printk(KERN_DEBUG
"got intr %x when expected disconnect\n", intr
);
313 cmd_done(state
, (DID_OK
<< 16) + (cmd
->SCp
.Message
<< 8)
317 printk(KERN_DEBUG
"don't know about phase %d\n", state
->phase
);
321 static void cmd_done(struct fsc_state
*state
, int result
)
323 struct scsi_cmnd
*cmd
;
325 cmd
= state
->current_req
;
327 cmd
->result
= result
;
328 (*cmd
->scsi_done
)(cmd
);
329 state
->current_req
= NULL
;
332 mac53c94_start(state
);
336 * Set up DMA commands for transferring data.
338 static void set_dma_cmds(struct fsc_state
*state
, struct scsi_cmnd
*cmd
)
340 int i
, dma_cmd
, total
, nseg
;
341 struct scatterlist
*scl
;
342 struct dbdma_cmd
*dcmds
;
346 nseg
= scsi_dma_map(cmd
);
351 dma_cmd
= cmd
->sc_data_direction
== DMA_TO_DEVICE
?
352 OUTPUT_MORE
: INPUT_MORE
;
353 dcmds
= state
->dma_cmds
;
356 scsi_for_each_sg(cmd
, scl
, nseg
, i
) {
357 dma_addr
= sg_dma_address(scl
);
358 dma_len
= sg_dma_len(scl
);
359 if (dma_len
> 0xffff)
360 panic("mac53c94: scatterlist element >= 64k");
362 st_le16(&dcmds
->req_count
, dma_len
);
363 st_le16(&dcmds
->command
, dma_cmd
);
364 st_le32(&dcmds
->phy_addr
, dma_addr
);
365 dcmds
->xfer_status
= 0;
369 dma_cmd
+= OUTPUT_LAST
- OUTPUT_MORE
;
370 st_le16(&dcmds
[-1].command
, dma_cmd
);
371 st_le16(&dcmds
->command
, DBDMA_STOP
);
372 cmd
->SCp
.this_residual
= total
;
375 static struct scsi_host_template mac53c94_template
= {
376 .proc_name
= "53c94",
378 .queuecommand
= mac53c94_queue
,
379 .eh_host_reset_handler
= mac53c94_host_reset
,
382 .sg_tablesize
= SG_ALL
,
384 .use_clustering
= DISABLE_CLUSTERING
,
387 static int mac53c94_probe(struct macio_dev
*mdev
, const struct of_device_id
*match
)
389 struct device_node
*node
= macio_get_of_node(mdev
);
390 struct pci_dev
*pdev
= macio_get_pci_dev(mdev
);
391 struct fsc_state
*state
;
392 struct Scsi_Host
*host
;
394 const unsigned char *clkprop
;
395 int proplen
, rc
= -ENODEV
;
397 if (macio_resource_count(mdev
) != 2 || macio_irq_count(mdev
) != 2) {
398 printk(KERN_ERR
"mac53c94: expected 2 addrs and intrs"
400 macio_resource_count(mdev
), macio_irq_count(mdev
));
404 if (macio_request_resources(mdev
, "mac53c94") != 0) {
405 printk(KERN_ERR
"mac53c94: unable to request memory resources");
409 host
= scsi_host_alloc(&mac53c94_template
, sizeof(struct fsc_state
));
411 printk(KERN_ERR
"mac53c94: couldn't register host");
416 state
= (struct fsc_state
*) host
->hostdata
;
417 macio_set_drvdata(mdev
, state
);
422 state
->regs
= (struct mac53c94_regs __iomem
*)
423 ioremap(macio_resource_start(mdev
, 0), 0x1000);
424 state
->intr
= macio_irq(mdev
, 0);
425 state
->dma
= (struct dbdma_regs __iomem
*)
426 ioremap(macio_resource_start(mdev
, 1), 0x1000);
427 state
->dmaintr
= macio_irq(mdev
, 1);
428 if (state
->regs
== NULL
|| state
->dma
== NULL
) {
429 printk(KERN_ERR
"mac53c94: ioremap failed for %s\n",
434 clkprop
= of_get_property(node
, "clock-frequency", &proplen
);
435 if (clkprop
== NULL
|| proplen
!= sizeof(int)) {
436 printk(KERN_ERR
"%s: can't get clock frequency, "
437 "assuming 25MHz\n", node
->full_name
);
438 state
->clk_freq
= 25000000;
440 state
->clk_freq
= *(int *)clkprop
;
442 dma_cmd_space
= kmalloc((host
->sg_tablesize
+ 2) *
443 sizeof(struct dbdma_cmd
), GFP_KERNEL
);
444 if (dma_cmd_space
== 0) {
445 printk(KERN_ERR
"mac53c94: couldn't allocate dma "
446 "command space for %s\n", node
->full_name
);
450 state
->dma_cmds
= (struct dbdma_cmd
*)DBDMA_ALIGN(dma_cmd_space
);
451 memset(state
->dma_cmds
, 0, (host
->sg_tablesize
+ 1)
452 * sizeof(struct dbdma_cmd
));
453 state
->dma_cmd_space
= dma_cmd_space
;
455 mac53c94_init(state
);
457 if (request_irq(state
->intr
, do_mac53c94_interrupt
, 0, "53C94",state
)) {
458 printk(KERN_ERR
"mac53C94: can't get irq %d for %s\n",
459 state
->intr
, node
->full_name
);
463 rc
= scsi_add_host(host
, &mdev
->ofdev
.dev
);
465 goto out_release_irq
;
467 scsi_scan_host(host
);
471 free_irq(state
->intr
, state
);
473 kfree(state
->dma_cmd_space
);
475 if (state
->dma
!= NULL
)
477 if (state
->regs
!= NULL
)
478 iounmap(state
->regs
);
481 macio_release_resources(mdev
);
486 static int mac53c94_remove(struct macio_dev
*mdev
)
488 struct fsc_state
*fp
= (struct fsc_state
*)macio_get_drvdata(mdev
);
489 struct Scsi_Host
*host
= fp
->host
;
491 scsi_remove_host(host
);
493 free_irq(fp
->intr
, fp
);
499 kfree(fp
->dma_cmd_space
);
503 macio_release_resources(mdev
);
509 static struct of_device_id mac53c94_match
[] =
516 MODULE_DEVICE_TABLE (of
, mac53c94_match
);
518 static struct macio_driver mac53c94_driver
=
522 .owner
= THIS_MODULE
,
523 .of_match_table
= mac53c94_match
,
525 .probe
= mac53c94_probe
,
526 .remove
= mac53c94_remove
,
530 static int __init
init_mac53c94(void)
532 return macio_register_driver(&mac53c94_driver
);
535 static void __exit
exit_mac53c94(void)
537 return macio_unregister_driver(&mac53c94_driver
);
540 module_init(init_mac53c94
);
541 module_exit(exit_mac53c94
);
543 MODULE_DESCRIPTION("PowerMac 53c94 SCSI driver");
544 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
545 MODULE_LICENSE("GPL");