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>
25 #include <asm/macints.h>
26 #include <asm/macintosh.h>
28 #include <scsi/scsi_host.h>
32 #define DRV_MODULE_NAME "mac_esp"
33 #define PFX DRV_MODULE_NAME ": "
34 #define DRV_VERSION "1.000"
35 #define DRV_MODULE_RELDATE "Sept 15, 2007"
37 #define MAC_ESP_IO_BASE 0x50F00000
38 #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000)
39 #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000)
40 #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000)
41 #define MAC_ESP_REGS_SPACING 0x402
42 #define MAC_ESP_PDMA_REG 0xF9800024
43 #define MAC_ESP_PDMA_REG_SPACING 0x4
44 #define MAC_ESP_PDMA_IO_OFFSET 0x100
46 #define esp_read8(REG) mac_esp_read8(esp, REG)
47 #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
51 void __iomem
*pdma_regs
;
52 void __iomem
*pdma_io
;
55 static struct esp
*esp_chips
[2];
57 #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
58 platform_get_drvdata((struct platform_device *) \
61 static inline void mac_esp_write8(struct esp
*esp
, u8 val
, unsigned long reg
)
63 nubus_writeb(val
, esp
->regs
+ reg
* 16);
66 static inline u8
mac_esp_read8(struct esp
*esp
, unsigned long reg
)
68 return nubus_readb(esp
->regs
+ reg
* 16);
71 /* For pseudo DMA and PIO we need the virtual address
72 * so this address mapping is the identity mapping.
75 static dma_addr_t
mac_esp_map_single(struct esp
*esp
, void *buf
,
78 return (dma_addr_t
)buf
;
81 static int mac_esp_map_sg(struct esp
*esp
, struct scatterlist
*sg
,
86 for (i
= 0; i
< num_sg
; i
++)
87 sg
[i
].dma_address
= (u32
)sg_virt(&sg
[i
]);
91 static void mac_esp_unmap_single(struct esp
*esp
, dma_addr_t addr
,
97 static void mac_esp_unmap_sg(struct esp
*esp
, struct scatterlist
*sg
,
103 static void mac_esp_reset_dma(struct esp
*esp
)
108 static void mac_esp_dma_drain(struct esp
*esp
)
113 static void mac_esp_dma_invalidate(struct esp
*esp
)
118 static int mac_esp_dma_error(struct esp
*esp
)
120 return MAC_ESP_GET_PRIV(esp
)->error
;
123 static inline int mac_esp_wait_for_empty_fifo(struct esp
*esp
)
125 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
129 if (!(esp_read8(ESP_FFLAGS
) & ESP_FF_FBYTES
))
132 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
138 printk(KERN_ERR PFX
"FIFO is not empty (sreg %02x)\n",
139 esp_read8(ESP_STATUS
));
144 static inline int mac_esp_wait_for_dreq(struct esp
*esp
)
146 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
150 if (mep
->pdma_regs
== NULL
) {
151 if (mac_irq_pending(IRQ_MAC_SCSIDRQ
))
154 if (nubus_readl(mep
->pdma_regs
) & 0x200)
158 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
164 printk(KERN_ERR PFX
"PDMA timeout (sreg %02x)\n",
165 esp_read8(ESP_STATUS
));
170 #define MAC_ESP_PDMA_LOOP(operands) \
174 "1: movew " operands " \n" \
175 "2: movew " operands " \n" \
176 "3: movew " operands " \n" \
177 "4: movew " operands " \n" \
178 "5: movew " operands " \n" \
179 "6: movew " operands " \n" \
180 "7: movew " operands " \n" \
181 "8: movew " operands " \n" \
182 "9: movew " operands " \n" \
183 "10: movew " operands " \n" \
184 "11: movew " operands " \n" \
185 "12: movew " operands " \n" \
186 "13: movew " operands " \n" \
187 "14: movew " operands " \n" \
188 "15: movew " operands " \n" \
189 "16: movew " operands " \n" \
194 "21: movew " operands " \n" \
199 "31: moveb " operands " \n" \
203 " .section __ex_table,\"a\" \n" \
214 " .long 10b,40b \n" \
215 " .long 11b,40b \n" \
216 " .long 12b,40b \n" \
217 " .long 13b,40b \n" \
218 " .long 14b,40b \n" \
219 " .long 15b,40b \n" \
220 " .long 16b,40b \n" \
221 " .long 21b,40b \n" \
222 " .long 31b,40b \n" \
223 " .long 32b,40b \n" \
225 : "+a" (addr), "+r" (count32), "+r" (count2) \
226 : "g" (count1), "a" (mep->pdma_io))
228 static void mac_esp_send_pdma_cmd(struct esp
*esp
, u32 addr
, u32 esp_count
,
229 u32 dma_count
, int write
, u8 cmd
)
231 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
234 local_irq_save(flags
);
239 scsi_esp_cmd(esp
, ESP_CMD_FLUSH
);
241 esp_write8((esp_count
>> 0) & 0xFF, ESP_TCLOW
);
242 esp_write8((esp_count
>> 8) & 0xFF, ESP_TCMED
);
244 scsi_esp_cmd(esp
, cmd
);
247 unsigned int count32
= esp_count
>> 5;
248 unsigned int count2
= (esp_count
& 0x1F) >> 1;
249 unsigned int count1
= esp_count
& 1;
250 unsigned int start_addr
= addr
;
252 if (mac_esp_wait_for_dreq(esp
))
256 MAC_ESP_PDMA_LOOP("%4@,%0@+");
258 esp_count
-= addr
- start_addr
;
262 MAC_ESP_PDMA_LOOP("%0@+,%4@");
264 if (mac_esp_wait_for_empty_fifo(esp
))
267 n
= (esp_read8(ESP_TCMED
) << 8) + esp_read8(ESP_TCLOW
);
268 addr
= start_addr
+ esp_count
- n
;
273 local_irq_restore(flags
);
277 * Programmed IO routines follow.
280 static inline unsigned int mac_esp_wait_for_fifo(struct esp
*esp
)
285 unsigned int fbytes
= esp_read8(ESP_FFLAGS
) & ESP_FF_FBYTES
;
293 printk(KERN_ERR PFX
"FIFO is empty (sreg %02x)\n",
294 esp_read8(ESP_STATUS
));
298 static inline int mac_esp_wait_for_intr(struct esp
*esp
)
300 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
304 esp
->sreg
= esp_read8(ESP_STATUS
);
305 if (esp
->sreg
& ESP_STAT_INTR
)
311 printk(KERN_ERR PFX
"IRQ timeout (sreg %02x)\n", esp
->sreg
);
316 #define MAC_ESP_PIO_LOOP(operands, reg1) \
318 "1: moveb " operands " \n" \
321 : "+a" (addr), "+r" (reg1) \
324 #define MAC_ESP_PIO_FILL(operands, reg1) \
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" \
338 " moveb " operands " \n" \
339 " moveb " operands " \n" \
340 " moveb " operands " \n" \
341 " moveb " operands " \n" \
344 : "+a" (addr), "+r" (reg1) \
347 #define MAC_ESP_FIFO_SIZE 16
349 static void mac_esp_send_pio_cmd(struct esp
*esp
, u32 addr
, u32 esp_count
,
350 u32 dma_count
, int write
, u8 cmd
)
352 struct mac_esp_priv
*mep
= MAC_ESP_GET_PRIV(esp
);
353 u8
*fifo
= esp
->regs
+ ESP_FDATA
* 16;
355 disable_irq(esp
->host
->irq
);
361 scsi_esp_cmd(esp
, cmd
);
366 n
= mac_esp_wait_for_fifo(esp
);
374 MAC_ESP_PIO_LOOP("%2@,%0@+", n
);
379 if (mac_esp_wait_for_intr(esp
))
382 if (((esp
->sreg
& ESP_STAT_PMASK
) != ESP_DIP
) &&
383 ((esp
->sreg
& ESP_STAT_PMASK
) != ESP_MIP
))
386 esp
->ireg
= esp_read8(ESP_INTRPT
);
387 if ((esp
->ireg
& (ESP_INTR_DC
| ESP_INTR_BSERV
)) !=
391 scsi_esp_cmd(esp
, ESP_CMD_TI
);
394 scsi_esp_cmd(esp
, ESP_CMD_FLUSH
);
396 if (esp_count
>= MAC_ESP_FIFO_SIZE
)
397 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count
);
399 MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count
);
401 scsi_esp_cmd(esp
, cmd
);
406 if (mac_esp_wait_for_intr(esp
))
409 if (((esp
->sreg
& ESP_STAT_PMASK
) != ESP_DOP
) &&
410 ((esp
->sreg
& ESP_STAT_PMASK
) != ESP_MOP
))
413 esp
->ireg
= esp_read8(ESP_INTRPT
);
414 if ((esp
->ireg
& (ESP_INTR_DC
| ESP_INTR_BSERV
)) !=
418 n
= MAC_ESP_FIFO_SIZE
-
419 (esp_read8(ESP_FFLAGS
) & ESP_FF_FBYTES
);
423 if (n
== MAC_ESP_FIFO_SIZE
) {
424 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count
);
427 MAC_ESP_PIO_LOOP("%0@+,%2@", n
);
430 scsi_esp_cmd(esp
, ESP_CMD_TI
);
434 enable_irq(esp
->host
->irq
);
437 static int mac_esp_irq_pending(struct esp
*esp
)
439 if (esp_read8(ESP_STATUS
) & ESP_STAT_INTR
)
444 static u32
mac_esp_dma_length_limit(struct esp
*esp
, u32 dma_addr
, u32 dma_len
)
446 return dma_len
> 0xFFFF ? 0xFFFF : dma_len
;
449 static irqreturn_t
mac_scsi_esp_intr(int irq
, void *dev_id
)
454 * This is an edge triggered IRQ, so we have to be careful to
455 * avoid missing a transition when it is shared by two ESP devices.
461 (mac_esp_read8(esp_chips
[0], ESP_STATUS
) & ESP_STAT_INTR
)) {
462 (void)scsi_esp_intr(irq
, esp_chips
[0]);
466 (mac_esp_read8(esp_chips
[1], ESP_STATUS
) & ESP_STAT_INTR
)) {
467 (void)scsi_esp_intr(irq
, esp_chips
[1]);
475 static struct esp_driver_ops mac_esp_ops
= {
476 .esp_write8
= mac_esp_write8
,
477 .esp_read8
= mac_esp_read8
,
478 .map_single
= mac_esp_map_single
,
479 .map_sg
= mac_esp_map_sg
,
480 .unmap_single
= mac_esp_unmap_single
,
481 .unmap_sg
= mac_esp_unmap_sg
,
482 .irq_pending
= mac_esp_irq_pending
,
483 .dma_length_limit
= mac_esp_dma_length_limit
,
484 .reset_dma
= mac_esp_reset_dma
,
485 .dma_drain
= mac_esp_dma_drain
,
486 .dma_invalidate
= mac_esp_dma_invalidate
,
487 .send_dma_cmd
= mac_esp_send_pdma_cmd
,
488 .dma_error
= mac_esp_dma_error
,
491 static int __devinit
esp_mac_probe(struct platform_device
*dev
)
493 struct scsi_host_template
*tpnt
= &scsi_esp_template
;
494 struct Scsi_Host
*host
;
497 struct mac_esp_priv
*mep
;
505 host
= scsi_host_alloc(tpnt
, sizeof(struct esp
));
512 host
->use_clustering
= DISABLE_CLUSTERING
;
513 esp
= shost_priv(host
);
518 esp
->command_block
= kzalloc(16, GFP_KERNEL
);
519 if (!esp
->command_block
)
521 esp
->command_block_dma
= (dma_addr_t
)esp
->command_block
;
524 host
->this_id
= esp
->scsi_id
;
525 esp
->scsi_id_mask
= 1 << esp
->scsi_id
;
527 mep
= kzalloc(sizeof(struct mac_esp_priv
), GFP_KERNEL
);
529 goto fail_free_command_block
;
531 platform_set_drvdata(dev
, mep
);
533 switch (macintosh_config
->scsi_type
) {
534 case MAC_SCSI_QUADRA
:
535 esp
->cfreq
= 16500000;
536 esp
->regs
= (void __iomem
*)MAC_ESP_REGS_QUADRA
;
537 mep
->pdma_io
= esp
->regs
+ MAC_ESP_PDMA_IO_OFFSET
;
538 mep
->pdma_regs
= NULL
;
540 case MAC_SCSI_QUADRA2
:
541 esp
->cfreq
= 25000000;
542 esp
->regs
= (void __iomem
*)(MAC_ESP_REGS_QUADRA2
+
543 dev
->id
* MAC_ESP_REGS_SPACING
);
544 mep
->pdma_io
= esp
->regs
+ MAC_ESP_PDMA_IO_OFFSET
;
545 mep
->pdma_regs
= (void __iomem
*)(MAC_ESP_PDMA_REG
+
546 dev
->id
* MAC_ESP_PDMA_REG_SPACING
);
547 nubus_writel(0x1d1, mep
->pdma_regs
);
549 case MAC_SCSI_QUADRA3
:
550 /* These quadras have a real DMA controller (the PSC) but we
551 * don't know how to drive it so we must use PIO instead.
553 esp
->cfreq
= 25000000;
554 esp
->regs
= (void __iomem
*)MAC_ESP_REGS_QUADRA3
;
556 mep
->pdma_regs
= NULL
;
560 esp
->ops
= &mac_esp_ops
;
561 if (mep
->pdma_io
== NULL
) {
562 printk(KERN_INFO PFX
"using PIO for controller %d\n", dev
->id
);
563 esp_write8(0, ESP_TCLOW
);
564 esp_write8(0, ESP_TCMED
);
565 esp
->flags
= ESP_FLAG_DISABLE_SYNC
;
566 mac_esp_ops
.send_dma_cmd
= mac_esp_send_pio_cmd
;
568 printk(KERN_INFO PFX
"using PDMA for controller %d\n", dev
->id
);
571 host
->irq
= IRQ_MAC_SCSI
;
572 esp_chips
[dev
->id
] = esp
;
574 if (esp_chips
[!dev
->id
] == NULL
) {
575 err
= request_irq(host
->irq
, mac_scsi_esp_intr
, 0,
578 esp_chips
[dev
->id
] = NULL
;
583 err
= scsi_esp_register(esp
, &dev
->dev
);
590 if (esp_chips
[!dev
->id
] == NULL
)
591 free_irq(host
->irq
, esp
);
594 fail_free_command_block
:
595 kfree(esp
->command_block
);
602 static int __devexit
esp_mac_remove(struct platform_device
*dev
)
604 struct mac_esp_priv
*mep
= platform_get_drvdata(dev
);
605 struct esp
*esp
= mep
->esp
;
606 unsigned int irq
= esp
->host
->irq
;
608 scsi_esp_unregister(esp
);
610 esp_chips
[dev
->id
] = NULL
;
611 if (!(esp_chips
[0] || esp_chips
[1]))
616 kfree(esp
->command_block
);
618 scsi_host_put(esp
->host
);
623 static struct platform_driver esp_mac_driver
= {
624 .probe
= esp_mac_probe
,
625 .remove
= __devexit_p(esp_mac_remove
),
627 .name
= DRV_MODULE_NAME
,
628 .owner
= THIS_MODULE
,
632 static int __init
mac_esp_init(void)
634 return platform_driver_register(&esp_mac_driver
);
637 static void __exit
mac_esp_exit(void)
639 platform_driver_unregister(&esp_mac_driver
);
642 MODULE_DESCRIPTION("Mac ESP SCSI driver");
643 MODULE_AUTHOR("Finn Thain <fthain@telegraphics.com.au>");
644 MODULE_LICENSE("GPL v2");
645 MODULE_VERSION(DRV_VERSION
);
646 MODULE_ALIAS("platform:" DRV_MODULE_NAME
);
648 module_init(mac_esp_init
);
649 module_exit(mac_esp_exit
);