1 /* mac_esp.c: ESP front-end for Macintosh Quadra systems.
3 * Adapted from jazz_esp.c and the old mac_esp.c.
5 * The pseudo DMA algorithm is based on the one used in NetBSD.
6 * See sys/arch/mac68k/obio/esp.c for some background information.
8 * Copyright (C) 2007-2008 Finn Thain
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/scatterlist.h>
19 #include <linux/delay.h>
21 #include <linux/nubus.h>
22 #include <linux/slab.h>
26 #include <asm/macints.h>
27 #include <asm/macintosh.h>
29 #include <scsi/scsi_host.h>
33 #define DRV_MODULE_NAME "mac_esp"
34 #define PFX DRV_MODULE_NAME ": "
35 #define DRV_VERSION "1.000"
36 #define DRV_MODULE_RELDATE "Sept 15, 2007"
38 #define MAC_ESP_IO_BASE 0x50F00000
39 #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000)
40 #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000)
41 #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000)
42 #define MAC_ESP_REGS_SPACING 0x402
43 #define MAC_ESP_PDMA_REG 0xF9800024
44 #define MAC_ESP_PDMA_REG_SPACING 0x4
45 #define MAC_ESP_PDMA_IO_OFFSET 0x100
47 #define esp_read8(REG) mac_esp_read8(esp, REG)
48 #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
52 void __iomem
*pdma_regs
;
53 void __iomem
*pdma_io
;
56 static struct esp
*esp_chips
[2];
58 #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
59 platform_get_drvdata((struct platform_device *) \
62 static inline void mac_esp_write8(struct esp
*esp
, u8 val
, unsigned long reg
)
64 nubus_writeb(val
, esp
->regs
+ reg
* 16);
67 static inline u8
mac_esp_read8(struct esp
*esp
, unsigned long reg
)
69 return nubus_readb(esp
->regs
+ reg
* 16);
72 /* For pseudo DMA and PIO we need the virtual address
73 * so this address mapping is the identity mapping.
76 static dma_addr_t
mac_esp_map_single(struct esp
*esp
, void *buf
,
79 return (dma_addr_t
)buf
;
82 static int mac_esp_map_sg(struct esp
*esp
, struct scatterlist
*sg
,
87 for (i
= 0; i
< num_sg
; i
++)
88 sg
[i
].dma_address
= (u32
)sg_virt(&sg
[i
]);
92 static void mac_esp_unmap_single(struct esp
*esp
, dma_addr_t addr
,
98 static void mac_esp_unmap_sg(struct esp
*esp
, struct scatterlist
*sg
,
104 static void mac_esp_reset_dma(struct esp
*esp
)
109 static void mac_esp_dma_drain(struct esp
*esp
)
114 static void mac_esp_dma_invalidate(struct esp
*esp
)
119 static int mac_esp_dma_error(struct esp
*esp
)
121 return MAC_ESP_GET_PRIV(esp
)->error
;
124 static inline int mac_esp_wait_for_empty_fifo(struct esp
*esp
)
126 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
130 if (!(esp_read8(ESP_FFLAGS
) & ESP_FF_FBYTES
))
133 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
139 printk(KERN_ERR PFX
"FIFO is not empty (sreg %02x)\n",
140 esp_read8(ESP_STATUS
));
145 static inline int mac_esp_wait_for_dreq(struct esp
*esp
)
147 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
151 if (mep
->pdma_regs
== NULL
) {
152 if (mac_irq_pending(IRQ_MAC_SCSIDRQ
))
155 if (nubus_readl(mep
->pdma_regs
) & 0x200)
159 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
165 printk(KERN_ERR PFX
"PDMA timeout (sreg %02x)\n",
166 esp_read8(ESP_STATUS
));
171 #define MAC_ESP_PDMA_LOOP(operands) \
175 "1: movew " operands " \n" \
176 "2: movew " operands " \n" \
177 "3: movew " operands " \n" \
178 "4: movew " operands " \n" \
179 "5: movew " operands " \n" \
180 "6: movew " operands " \n" \
181 "7: movew " operands " \n" \
182 "8: movew " operands " \n" \
183 "9: movew " operands " \n" \
184 "10: movew " operands " \n" \
185 "11: movew " operands " \n" \
186 "12: movew " operands " \n" \
187 "13: movew " operands " \n" \
188 "14: movew " operands " \n" \
189 "15: movew " operands " \n" \
190 "16: movew " operands " \n" \
195 "21: movew " operands " \n" \
200 "31: moveb " operands " \n" \
204 " .section __ex_table,\"a\" \n" \
215 " .long 10b,40b \n" \
216 " .long 11b,40b \n" \
217 " .long 12b,40b \n" \
218 " .long 13b,40b \n" \
219 " .long 14b,40b \n" \
220 " .long 15b,40b \n" \
221 " .long 16b,40b \n" \
222 " .long 21b,40b \n" \
223 " .long 31b,40b \n" \
224 " .long 32b,40b \n" \
226 : "+a" (addr), "+r" (count32), "+r" (count2) \
227 : "g" (count1), "a" (mep->pdma_io))
229 static void mac_esp_send_pdma_cmd(struct esp
*esp
, u32 addr
, u32 esp_count
,
230 u32 dma_count
, int write
, u8 cmd
)
232 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
237 scsi_esp_cmd(esp
, ESP_CMD_FLUSH
);
239 esp_write8((esp_count
>> 0) & 0xFF, ESP_TCLOW
);
240 esp_write8((esp_count
>> 8) & 0xFF, ESP_TCMED
);
242 scsi_esp_cmd(esp
, cmd
);
245 unsigned int count32
= esp_count
>> 5;
246 unsigned int count2
= (esp_count
& 0x1F) >> 1;
247 unsigned int count1
= esp_count
& 1;
248 unsigned int start_addr
= addr
;
250 if (mac_esp_wait_for_dreq(esp
))
254 MAC_ESP_PDMA_LOOP("%4@,%0@+");
256 esp_count
-= addr
- start_addr
;
260 MAC_ESP_PDMA_LOOP("%0@+,%4@");
262 if (mac_esp_wait_for_empty_fifo(esp
))
265 n
= (esp_read8(ESP_TCMED
) << 8) + esp_read8(ESP_TCLOW
);
266 addr
= start_addr
+ esp_count
- n
;
273 * Programmed IO routines follow.
276 static inline unsigned int mac_esp_wait_for_fifo(struct esp
*esp
)
281 unsigned int fbytes
= esp_read8(ESP_FFLAGS
) & ESP_FF_FBYTES
;
289 printk(KERN_ERR PFX
"FIFO is empty (sreg %02x)\n",
290 esp_read8(ESP_STATUS
));
294 static inline int mac_esp_wait_for_intr(struct esp
*esp
)
296 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
300 esp
->sreg
= esp_read8(ESP_STATUS
);
301 if (esp
->sreg
& ESP_STAT_INTR
)
307 printk(KERN_ERR PFX
"IRQ timeout (sreg %02x)\n", esp
->sreg
);
312 #define MAC_ESP_PIO_LOOP(operands, reg1) \
314 "1: moveb " operands " \n" \
317 : "+a" (addr), "+r" (reg1) \
320 #define MAC_ESP_PIO_FILL(operands, reg1) \
322 " moveb " operands " \n" \
323 " moveb " operands " \n" \
324 " moveb " operands " \n" \
325 " moveb " operands " \n" \
326 " moveb " operands " \n" \
327 " moveb " operands " \n" \
328 " moveb " operands " \n" \
329 " moveb " operands " \n" \
330 " moveb " operands " \n" \
331 " moveb " operands " \n" \
332 " moveb " operands " \n" \
333 " moveb " operands " \n" \
334 " moveb " operands " \n" \
335 " moveb " operands " \n" \
336 " moveb " operands " \n" \
337 " moveb " operands " \n" \
340 : "+a" (addr), "+r" (reg1) \
343 #define MAC_ESP_FIFO_SIZE 16
345 static void mac_esp_send_pio_cmd(struct esp
*esp
, u32 addr
, u32 esp_count
,
346 u32 dma_count
, int write
, u8 cmd
)
348 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
349 u8
*fifo
= esp
->regs
+ ESP_FDATA
* 16;
355 scsi_esp_cmd(esp
, cmd
);
360 n
= mac_esp_wait_for_fifo(esp
);
368 MAC_ESP_PIO_LOOP("%2@,%0@+", n
);
373 if (mac_esp_wait_for_intr(esp
))
376 if (((esp
->sreg
& ESP_STAT_PMASK
) != ESP_DIP
) &&
377 ((esp
->sreg
& ESP_STAT_PMASK
) != ESP_MIP
))
380 esp
->ireg
= esp_read8(ESP_INTRPT
);
381 if ((esp
->ireg
& (ESP_INTR_DC
| ESP_INTR_BSERV
)) !=
385 scsi_esp_cmd(esp
, ESP_CMD_TI
);
388 scsi_esp_cmd(esp
, ESP_CMD_FLUSH
);
390 if (esp_count
>= MAC_ESP_FIFO_SIZE
)
391 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count
);
393 MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count
);
395 scsi_esp_cmd(esp
, cmd
);
400 if (mac_esp_wait_for_intr(esp
))
403 if (((esp
->sreg
& ESP_STAT_PMASK
) != ESP_DOP
) &&
404 ((esp
->sreg
& ESP_STAT_PMASK
) != ESP_MOP
))
407 esp
->ireg
= esp_read8(ESP_INTRPT
);
408 if ((esp
->ireg
& (ESP_INTR_DC
| ESP_INTR_BSERV
)) !=
412 n
= MAC_ESP_FIFO_SIZE
-
413 (esp_read8(ESP_FFLAGS
) & ESP_FF_FBYTES
);
417 if (n
== MAC_ESP_FIFO_SIZE
) {
418 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count
);
421 MAC_ESP_PIO_LOOP("%0@+,%2@", n
);
424 scsi_esp_cmd(esp
, ESP_CMD_TI
);
429 static int mac_esp_irq_pending(struct esp
*esp
)
431 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
436 static u32
mac_esp_dma_length_limit(struct esp
*esp
, u32 dma_addr
, u32 dma_len
)
438 return dma_len
> 0xFFFF ? 0xFFFF : dma_len
;
441 static irqreturn_t
mac_scsi_esp_intr(int irq
, void *dev_id
)
446 * This is an edge triggered IRQ, so we have to be careful to
447 * avoid missing a transition when it is shared by two ESP devices.
453 (mac_esp_read8(esp_chips
[0], ESP_STATUS
) & ESP_STAT_INTR
)) {
454 (void)scsi_esp_intr(irq
, esp_chips
[0]);
458 (mac_esp_read8(esp_chips
[1], ESP_STATUS
) & ESP_STAT_INTR
)) {
459 (void)scsi_esp_intr(irq
, esp_chips
[1]);
467 static struct esp_driver_ops mac_esp_ops
= {
468 .esp_write8
= mac_esp_write8
,
469 .esp_read8
= mac_esp_read8
,
470 .map_single
= mac_esp_map_single
,
471 .map_sg
= mac_esp_map_sg
,
472 .unmap_single
= mac_esp_unmap_single
,
473 .unmap_sg
= mac_esp_unmap_sg
,
474 .irq_pending
= mac_esp_irq_pending
,
475 .dma_length_limit
= mac_esp_dma_length_limit
,
476 .reset_dma
= mac_esp_reset_dma
,
477 .dma_drain
= mac_esp_dma_drain
,
478 .dma_invalidate
= mac_esp_dma_invalidate
,
479 .send_dma_cmd
= mac_esp_send_pdma_cmd
,
480 .dma_error
= mac_esp_dma_error
,
483 static int __devinit
esp_mac_probe(struct platform_device
*dev
)
485 struct scsi_host_template
*tpnt
= &scsi_esp_template
;
486 struct Scsi_Host
*host
;
489 struct mac_esp_priv
*mep
;
497 host
= scsi_host_alloc(tpnt
, sizeof(struct esp
));
504 host
->use_clustering
= DISABLE_CLUSTERING
;
505 esp
= shost_priv(host
);
510 esp
->command_block
= kzalloc(16, GFP_KERNEL
);
511 if (!esp
->command_block
)
513 esp
->command_block_dma
= (dma_addr_t
)esp
->command_block
;
516 host
->this_id
= esp
->scsi_id
;
517 esp
->scsi_id_mask
= 1 << esp
->scsi_id
;
519 mep
= kzalloc(sizeof(struct mac_esp_priv
), GFP_KERNEL
);
521 goto fail_free_command_block
;
523 platform_set_drvdata(dev
, mep
);
525 switch (macintosh_config
->scsi_type
) {
526 case MAC_SCSI_QUADRA
:
527 esp
->cfreq
= 16500000;
528 esp
->regs
= (void __iomem
*)MAC_ESP_REGS_QUADRA
;
529 mep
->pdma_io
= esp
->regs
+ MAC_ESP_PDMA_IO_OFFSET
;
530 mep
->pdma_regs
= NULL
;
532 case MAC_SCSI_QUADRA2
:
533 esp
->cfreq
= 25000000;
534 esp
->regs
= (void __iomem
*)(MAC_ESP_REGS_QUADRA2
+
535 dev
->id
* MAC_ESP_REGS_SPACING
);
536 mep
->pdma_io
= esp
->regs
+ MAC_ESP_PDMA_IO_OFFSET
;
537 mep
->pdma_regs
= (void __iomem
*)(MAC_ESP_PDMA_REG
+
538 dev
->id
* MAC_ESP_PDMA_REG_SPACING
);
539 nubus_writel(0x1d1, mep
->pdma_regs
);
541 case MAC_SCSI_QUADRA3
:
542 /* These quadras have a real DMA controller (the PSC) but we
543 * don't know how to drive it so we must use PIO instead.
545 esp
->cfreq
= 25000000;
546 esp
->regs
= (void __iomem
*)MAC_ESP_REGS_QUADRA3
;
548 mep
->pdma_regs
= NULL
;
552 esp
->ops
= &mac_esp_ops
;
553 if (mep
->pdma_io
== NULL
) {
554 printk(KERN_INFO PFX
"using PIO for controller %d\n", dev
->id
);
555 esp_write8(0, ESP_TCLOW
);
556 esp_write8(0, ESP_TCMED
);
557 esp
->flags
= ESP_FLAG_DISABLE_SYNC
;
558 mac_esp_ops
.send_dma_cmd
= mac_esp_send_pio_cmd
;
560 printk(KERN_INFO PFX
"using PDMA for controller %d\n", dev
->id
);
563 host
->irq
= IRQ_MAC_SCSI
;
564 esp_chips
[dev
->id
] = esp
;
566 if (esp_chips
[!dev
->id
] == NULL
) {
567 err
= request_irq(host
->irq
, mac_scsi_esp_intr
, 0,
570 esp_chips
[dev
->id
] = NULL
;
575 err
= scsi_esp_register(esp
, &dev
->dev
);
582 if (esp_chips
[!dev
->id
] == NULL
)
583 free_irq(host
->irq
, esp
);
586 fail_free_command_block
:
587 kfree(esp
->command_block
);
594 static int __devexit
esp_mac_remove(struct platform_device
*dev
)
596 struct mac_esp_priv
*mep
= platform_get_drvdata(dev
);
597 struct esp
*esp
= mep
->esp
;
598 unsigned int irq
= esp
->host
->irq
;
600 scsi_esp_unregister(esp
);
602 esp_chips
[dev
->id
] = NULL
;
603 if (!(esp_chips
[0] || esp_chips
[1]))
608 kfree(esp
->command_block
);
610 scsi_host_put(esp
->host
);
615 static struct platform_driver esp_mac_driver
= {
616 .probe
= esp_mac_probe
,
617 .remove
= __devexit_p(esp_mac_remove
),
619 .name
= DRV_MODULE_NAME
,
620 .owner
= THIS_MODULE
,
624 static int __init
mac_esp_init(void)
626 return platform_driver_register(&esp_mac_driver
);
629 static void __exit
mac_esp_exit(void)
631 platform_driver_unregister(&esp_mac_driver
);
634 MODULE_DESCRIPTION("Mac ESP SCSI driver");
635 MODULE_AUTHOR("Finn Thain <fthain@telegraphics.com.au>");
636 MODULE_LICENSE("GPL v2");
637 MODULE_VERSION(DRV_VERSION
);
638 MODULE_ALIAS("platform:" DRV_MODULE_NAME
);
640 module_init(mac_esp_init
);
641 module_exit(mac_esp_exit
);