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>
28 #include <asm/mac_via.h>
30 #include <scsi/scsi_host.h>
34 #define DRV_MODULE_NAME "mac_esp"
35 #define PFX DRV_MODULE_NAME ": "
36 #define DRV_VERSION "1.000"
37 #define DRV_MODULE_RELDATE "Sept 15, 2007"
39 #define MAC_ESP_IO_BASE 0x50F00000
40 #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000)
41 #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000)
42 #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000)
43 #define MAC_ESP_REGS_SPACING 0x402
44 #define MAC_ESP_PDMA_REG 0xF9800024
45 #define MAC_ESP_PDMA_REG_SPACING 0x4
46 #define MAC_ESP_PDMA_IO_OFFSET 0x100
48 #define esp_read8(REG) mac_esp_read8(esp, REG)
49 #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
53 void __iomem *pdma_regs;
54 void __iomem *pdma_io;
57 static struct esp *esp_chips[2];
58 static DEFINE_SPINLOCK(esp_chips_lock);
60 #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
61 platform_get_drvdata((struct platform_device *) \
64 static inline void mac_esp_write8(struct esp *esp, u8 val, unsigned long reg)
66 nubus_writeb(val, esp->regs + reg * 16);
69 static inline u8 mac_esp_read8(struct esp *esp, unsigned long reg)
71 return nubus_readb(esp->regs + reg * 16);
74 /* For pseudo DMA and PIO we need the virtual address
75 * so this address mapping is the identity mapping.
78 static dma_addr_t mac_esp_map_single(struct esp *esp, void *buf,
81 return (dma_addr_t)buf;
84 static int mac_esp_map_sg(struct esp *esp, struct scatterlist *sg,
89 for (i = 0; i < num_sg; i++)
90 sg[i].dma_address = (u32)sg_virt(&sg[i]);
94 static void mac_esp_unmap_single(struct esp *esp, dma_addr_t addr,
100 static void mac_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
106 static void mac_esp_reset_dma(struct esp *esp)
111 static void mac_esp_dma_drain(struct esp *esp)
116 static void mac_esp_dma_invalidate(struct esp *esp)
121 static int mac_esp_dma_error(struct esp *esp)
123 return MAC_ESP_GET_PRIV(esp)->error;
126 static inline int mac_esp_wait_for_empty_fifo(struct esp *esp)
128 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
132 if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES))
135 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
141 printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n",
142 esp_read8(ESP_STATUS));
147 static inline int mac_esp_wait_for_dreq(struct esp *esp)
149 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
153 if (mep->pdma_regs == NULL) {
154 if (via2_scsi_drq_pending())
157 if (nubus_readl(mep->pdma_regs) & 0x200)
161 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
167 printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n",
168 esp_read8(ESP_STATUS));
173 #define MAC_ESP_PDMA_LOOP(operands) \
177 "1: movew " operands " \n" \
178 "2: movew " operands " \n" \
179 "3: movew " operands " \n" \
180 "4: movew " operands " \n" \
181 "5: movew " operands " \n" \
182 "6: movew " operands " \n" \
183 "7: movew " operands " \n" \
184 "8: movew " operands " \n" \
185 "9: movew " operands " \n" \
186 "10: movew " operands " \n" \
187 "11: movew " operands " \n" \
188 "12: movew " operands " \n" \
189 "13: movew " operands " \n" \
190 "14: movew " operands " \n" \
191 "15: movew " operands " \n" \
192 "16: movew " operands " \n" \
197 "21: movew " operands " \n" \
202 "31: moveb " operands " \n" \
206 " .section __ex_table,\"a\" \n" \
217 " .long 10b,40b \n" \
218 " .long 11b,40b \n" \
219 " .long 12b,40b \n" \
220 " .long 13b,40b \n" \
221 " .long 14b,40b \n" \
222 " .long 15b,40b \n" \
223 " .long 16b,40b \n" \
224 " .long 21b,40b \n" \
225 " .long 31b,40b \n" \
226 " .long 32b,40b \n" \
228 : "+a" (addr), "+r" (count32), "+r" (count2) \
229 : "g" (count1), "a" (mep->pdma_io))
231 static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count,
232 u32 dma_count, int write, u8 cmd)
234 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
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;
275 * Programmed IO routines follow.
278 static inline unsigned int mac_esp_wait_for_fifo(struct esp *esp)
283 unsigned int fbytes = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
291 printk(KERN_ERR PFX "FIFO is empty (sreg %02x)\n",
292 esp_read8(ESP_STATUS));
296 static inline int mac_esp_wait_for_intr(struct esp *esp)
298 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
302 esp->sreg = esp_read8(ESP_STATUS);
303 if (esp->sreg & ESP_STAT_INTR)
309 printk(KERN_ERR PFX "IRQ timeout (sreg %02x)\n", esp->sreg);
314 #define MAC_ESP_PIO_LOOP(operands, reg1) \
316 "1: moveb " operands " \n" \
319 : "+a" (addr), "+r" (reg1) \
322 #define MAC_ESP_PIO_FILL(operands, reg1) \
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" \
338 " moveb " operands " \n" \
339 " moveb " operands " \n" \
342 : "+a" (addr), "+r" (reg1) \
345 #define MAC_ESP_FIFO_SIZE 16
347 static void mac_esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count,
348 u32 dma_count, int write, u8 cmd)
350 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
351 u8 __iomem *fifo = esp->regs + ESP_FDATA * 16;
352 u8 phase = esp->sreg & ESP_STAT_PMASK;
358 u8 *dst = (u8 *)addr;
359 u8 mask = ~(phase == ESP_MIP ? ESP_INTR_FDONE : ESP_INTR_BSERV);
361 scsi_esp_cmd(esp, cmd);
364 if (!mac_esp_wait_for_fifo(esp))
367 *dst++ = esp_read8(ESP_FDATA);
373 if (mac_esp_wait_for_intr(esp))
376 if ((esp->sreg & ESP_STAT_PMASK) != phase)
379 esp->ireg = esp_read8(ESP_INTRPT);
380 if (esp->ireg & mask) {
385 if (phase == ESP_MIP)
386 scsi_esp_cmd(esp, ESP_CMD_MOK);
388 scsi_esp_cmd(esp, ESP_CMD_TI);
391 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
393 if (esp_count >= MAC_ESP_FIFO_SIZE)
394 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
396 MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count);
398 scsi_esp_cmd(esp, cmd);
403 if (mac_esp_wait_for_intr(esp))
406 if ((esp->sreg & ESP_STAT_PMASK) != phase)
409 esp->ireg = esp_read8(ESP_INTRPT);
410 if (esp->ireg & ~ESP_INTR_BSERV) {
415 n = MAC_ESP_FIFO_SIZE -
416 (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES);
420 if (n == MAC_ESP_FIFO_SIZE) {
421 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
424 MAC_ESP_PIO_LOOP("%0@+,%2@", n);
427 scsi_esp_cmd(esp, ESP_CMD_TI);
432 static int mac_esp_irq_pending(struct esp *esp)
434 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
439 static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
441 return dma_len > 0xFFFF ? 0xFFFF : dma_len;
444 static irqreturn_t mac_scsi_esp_intr(int irq, void *dev_id)
449 * This is an edge triggered IRQ, so we have to be careful to
450 * avoid missing a transition when it is shared by two ESP devices.
456 (mac_esp_read8(esp_chips[0], ESP_STATUS) & ESP_STAT_INTR)) {
457 (void)scsi_esp_intr(irq, esp_chips[0]);
461 (mac_esp_read8(esp_chips[1], ESP_STATUS) & ESP_STAT_INTR)) {
462 (void)scsi_esp_intr(irq, esp_chips[1]);
470 static struct esp_driver_ops mac_esp_ops = {
471 .esp_write8 = mac_esp_write8,
472 .esp_read8 = mac_esp_read8,
473 .map_single = mac_esp_map_single,
474 .map_sg = mac_esp_map_sg,
475 .unmap_single = mac_esp_unmap_single,
476 .unmap_sg = mac_esp_unmap_sg,
477 .irq_pending = mac_esp_irq_pending,
478 .dma_length_limit = mac_esp_dma_length_limit,
479 .reset_dma = mac_esp_reset_dma,
480 .dma_drain = mac_esp_dma_drain,
481 .dma_invalidate = mac_esp_dma_invalidate,
482 .send_dma_cmd = mac_esp_send_pdma_cmd,
483 .dma_error = mac_esp_dma_error,
486 static int esp_mac_probe(struct platform_device *dev)
488 struct scsi_host_template *tpnt = &scsi_esp_template;
489 struct Scsi_Host *host;
492 struct mac_esp_priv *mep;
500 host = scsi_host_alloc(tpnt, sizeof(struct esp));
507 host->use_clustering = DISABLE_CLUSTERING;
508 esp = shost_priv(host);
513 esp->command_block = kzalloc(16, GFP_KERNEL);
514 if (!esp->command_block)
516 esp->command_block_dma = (dma_addr_t)esp->command_block;
519 host->this_id = esp->scsi_id;
520 esp->scsi_id_mask = 1 << esp->scsi_id;
522 mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL);
524 goto fail_free_command_block;
526 platform_set_drvdata(dev, mep);
528 switch (macintosh_config->scsi_type) {
529 case MAC_SCSI_QUADRA:
530 esp->cfreq = 16500000;
531 esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA;
532 mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
533 mep->pdma_regs = NULL;
535 case MAC_SCSI_QUADRA2:
536 esp->cfreq = 25000000;
537 esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 +
538 dev->id * MAC_ESP_REGS_SPACING);
539 mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
540 mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG +
541 dev->id * MAC_ESP_PDMA_REG_SPACING);
542 nubus_writel(0x1d1, mep->pdma_regs);
544 case MAC_SCSI_QUADRA3:
545 /* These quadras have a real DMA controller (the PSC) but we
546 * don't know how to drive it so we must use PIO instead.
548 esp->cfreq = 25000000;
549 esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3;
551 mep->pdma_regs = NULL;
555 esp->ops = &mac_esp_ops;
556 if (mep->pdma_io == NULL) {
557 printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id);
558 esp_write8(0, ESP_TCLOW);
559 esp_write8(0, ESP_TCMED);
560 esp->flags = ESP_FLAG_DISABLE_SYNC;
561 mac_esp_ops.send_dma_cmd = mac_esp_send_pio_cmd;
563 printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id);
566 host->irq = IRQ_MAC_SCSI;
568 /* The request_irq() call is intended to succeed for the first device
569 * and fail for the second device.
571 err = request_irq(host->irq, mac_scsi_esp_intr, 0, "ESP", NULL);
572 spin_lock(&esp_chips_lock);
573 if (err < 0 && esp_chips[!dev->id] == NULL) {
574 spin_unlock(&esp_chips_lock);
577 esp_chips[dev->id] = esp;
578 spin_unlock(&esp_chips_lock);
580 err = scsi_esp_register(esp, &dev->dev);
587 spin_lock(&esp_chips_lock);
588 esp_chips[dev->id] = NULL;
589 if (esp_chips[!dev->id] == NULL) {
590 spin_unlock(&esp_chips_lock);
591 free_irq(host->irq, NULL);
593 spin_unlock(&esp_chips_lock);
596 fail_free_command_block:
597 kfree(esp->command_block);
604 static int esp_mac_remove(struct platform_device *dev)
606 struct mac_esp_priv *mep = platform_get_drvdata(dev);
607 struct esp *esp = mep->esp;
608 unsigned int irq = esp->host->irq;
610 scsi_esp_unregister(esp);
612 spin_lock(&esp_chips_lock);
613 esp_chips[dev->id] = NULL;
614 if (esp_chips[!dev->id] == NULL) {
615 spin_unlock(&esp_chips_lock);
618 spin_unlock(&esp_chips_lock);
622 kfree(esp->command_block);
624 scsi_host_put(esp->host);
629 static struct platform_driver esp_mac_driver = {
630 .probe = esp_mac_probe,
631 .remove = esp_mac_remove,
633 .name = DRV_MODULE_NAME,
637 static int __init mac_esp_init(void)
639 return platform_driver_register(&esp_mac_driver);
642 static void __exit mac_esp_exit(void)
644 platform_driver_unregister(&esp_mac_driver);
647 MODULE_DESCRIPTION("Mac ESP SCSI driver");
648 MODULE_AUTHOR("Finn Thain");
649 MODULE_LICENSE("GPL v2");
650 MODULE_VERSION(DRV_VERSION);
651 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
653 module_init(mac_esp_init);
654 module_exit(mac_esp_exit);