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];
59 #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
60 platform_get_drvdata((struct platform_device *) \
63 static inline void mac_esp_write8(struct esp *esp, u8 val, unsigned long reg)
65 nubus_writeb(val, esp->regs + reg * 16);
68 static inline u8 mac_esp_read8(struct esp *esp, unsigned long reg)
70 return nubus_readb(esp->regs + reg * 16);
73 /* For pseudo DMA and PIO we need the virtual address
74 * so this address mapping is the identity mapping.
77 static dma_addr_t mac_esp_map_single(struct esp *esp, void *buf,
80 return (dma_addr_t)buf;
83 static int mac_esp_map_sg(struct esp *esp, struct scatterlist *sg,
88 for (i = 0; i < num_sg; i++)
89 sg[i].dma_address = (u32)sg_virt(&sg[i]);
93 static void mac_esp_unmap_single(struct esp *esp, dma_addr_t addr,
99 static void mac_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
105 static void mac_esp_reset_dma(struct esp *esp)
110 static void mac_esp_dma_drain(struct esp *esp)
115 static void mac_esp_dma_invalidate(struct esp *esp)
120 static int mac_esp_dma_error(struct esp *esp)
122 return MAC_ESP_GET_PRIV(esp)->error;
125 static inline int mac_esp_wait_for_empty_fifo(struct esp *esp)
127 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
131 if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES))
134 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
140 printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n",
141 esp_read8(ESP_STATUS));
146 static inline int mac_esp_wait_for_dreq(struct esp *esp)
148 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
152 if (mep->pdma_regs == NULL) {
153 if (via2_scsi_drq_pending())
156 if (nubus_readl(mep->pdma_regs) & 0x200)
160 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
166 printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n",
167 esp_read8(ESP_STATUS));
172 #define MAC_ESP_PDMA_LOOP(operands) \
176 "1: movew " operands " \n" \
177 "2: movew " operands " \n" \
178 "3: movew " operands " \n" \
179 "4: movew " operands " \n" \
180 "5: movew " operands " \n" \
181 "6: movew " operands " \n" \
182 "7: movew " operands " \n" \
183 "8: movew " operands " \n" \
184 "9: movew " operands " \n" \
185 "10: movew " operands " \n" \
186 "11: movew " operands " \n" \
187 "12: movew " operands " \n" \
188 "13: movew " operands " \n" \
189 "14: movew " operands " \n" \
190 "15: movew " operands " \n" \
191 "16: movew " operands " \n" \
196 "21: movew " operands " \n" \
201 "31: moveb " operands " \n" \
205 " .section __ex_table,\"a\" \n" \
216 " .long 10b,40b \n" \
217 " .long 11b,40b \n" \
218 " .long 12b,40b \n" \
219 " .long 13b,40b \n" \
220 " .long 14b,40b \n" \
221 " .long 15b,40b \n" \
222 " .long 16b,40b \n" \
223 " .long 21b,40b \n" \
224 " .long 31b,40b \n" \
225 " .long 32b,40b \n" \
227 : "+a" (addr), "+r" (count32), "+r" (count2) \
228 : "g" (count1), "a" (mep->pdma_io))
230 static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count,
231 u32 dma_count, int write, u8 cmd)
233 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
238 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
240 esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW);
241 esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED);
243 scsi_esp_cmd(esp, cmd);
246 unsigned int count32 = esp_count >> 5;
247 unsigned int count2 = (esp_count & 0x1F) >> 1;
248 unsigned int count1 = esp_count & 1;
249 unsigned int start_addr = addr;
251 if (mac_esp_wait_for_dreq(esp))
255 MAC_ESP_PDMA_LOOP("%4@,%0@+");
257 esp_count -= addr - start_addr;
261 MAC_ESP_PDMA_LOOP("%0@+,%4@");
263 if (mac_esp_wait_for_empty_fifo(esp))
266 n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW);
267 addr = start_addr + esp_count - n;
274 * Programmed IO routines follow.
277 static inline unsigned int mac_esp_wait_for_fifo(struct esp *esp)
282 unsigned int fbytes = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
290 printk(KERN_ERR PFX "FIFO is empty (sreg %02x)\n",
291 esp_read8(ESP_STATUS));
295 static inline int mac_esp_wait_for_intr(struct esp *esp)
297 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
301 esp->sreg = esp_read8(ESP_STATUS);
302 if (esp->sreg & ESP_STAT_INTR)
308 printk(KERN_ERR PFX "IRQ timeout (sreg %02x)\n", esp->sreg);
313 #define MAC_ESP_PIO_LOOP(operands, reg1) \
315 "1: moveb " operands " \n" \
318 : "+a" (addr), "+r" (reg1) \
321 #define MAC_ESP_PIO_FILL(operands, reg1) \
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" \
338 " moveb " operands " \n" \
341 : "+a" (addr), "+r" (reg1) \
344 #define MAC_ESP_FIFO_SIZE 16
346 static void mac_esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count,
347 u32 dma_count, int write, u8 cmd)
349 struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
350 u8 *fifo = esp->regs + ESP_FDATA * 16;
356 scsi_esp_cmd(esp, cmd);
361 n = mac_esp_wait_for_fifo(esp);
369 MAC_ESP_PIO_LOOP("%2@,%0@+", n);
374 if (mac_esp_wait_for_intr(esp))
377 if (((esp->sreg & ESP_STAT_PMASK) != ESP_DIP) &&
378 ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP))
381 esp->ireg = esp_read8(ESP_INTRPT);
382 if ((esp->ireg & (ESP_INTR_DC | ESP_INTR_BSERV)) !=
386 scsi_esp_cmd(esp, ESP_CMD_TI);
389 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
391 if (esp_count >= MAC_ESP_FIFO_SIZE)
392 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
394 MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count);
396 scsi_esp_cmd(esp, cmd);
401 if (mac_esp_wait_for_intr(esp))
404 if (((esp->sreg & ESP_STAT_PMASK) != ESP_DOP) &&
405 ((esp->sreg & ESP_STAT_PMASK) != ESP_MOP))
408 esp->ireg = esp_read8(ESP_INTRPT);
409 if ((esp->ireg & (ESP_INTR_DC | ESP_INTR_BSERV)) !=
413 n = MAC_ESP_FIFO_SIZE -
414 (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES);
418 if (n == MAC_ESP_FIFO_SIZE) {
419 MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
422 MAC_ESP_PIO_LOOP("%0@+,%2@", n);
425 scsi_esp_cmd(esp, ESP_CMD_TI);
430 static int mac_esp_irq_pending(struct esp *esp)
432 if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
437 static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
439 return dma_len > 0xFFFF ? 0xFFFF : dma_len;
442 static irqreturn_t mac_scsi_esp_intr(int irq, void *dev_id)
447 * This is an edge triggered IRQ, so we have to be careful to
448 * avoid missing a transition when it is shared by two ESP devices.
454 (mac_esp_read8(esp_chips[0], ESP_STATUS) & ESP_STAT_INTR)) {
455 (void)scsi_esp_intr(irq, esp_chips[0]);
459 (mac_esp_read8(esp_chips[1], ESP_STATUS) & ESP_STAT_INTR)) {
460 (void)scsi_esp_intr(irq, esp_chips[1]);
468 static struct esp_driver_ops mac_esp_ops = {
469 .esp_write8 = mac_esp_write8,
470 .esp_read8 = mac_esp_read8,
471 .map_single = mac_esp_map_single,
472 .map_sg = mac_esp_map_sg,
473 .unmap_single = mac_esp_unmap_single,
474 .unmap_sg = mac_esp_unmap_sg,
475 .irq_pending = mac_esp_irq_pending,
476 .dma_length_limit = mac_esp_dma_length_limit,
477 .reset_dma = mac_esp_reset_dma,
478 .dma_drain = mac_esp_dma_drain,
479 .dma_invalidate = mac_esp_dma_invalidate,
480 .send_dma_cmd = mac_esp_send_pdma_cmd,
481 .dma_error = mac_esp_dma_error,
484 static int esp_mac_probe(struct platform_device *dev)
486 struct scsi_host_template *tpnt = &scsi_esp_template;
487 struct Scsi_Host *host;
490 struct mac_esp_priv *mep;
498 host = scsi_host_alloc(tpnt, sizeof(struct esp));
505 host->use_clustering = DISABLE_CLUSTERING;
506 esp = shost_priv(host);
511 esp->command_block = kzalloc(16, GFP_KERNEL);
512 if (!esp->command_block)
514 esp->command_block_dma = (dma_addr_t)esp->command_block;
517 host->this_id = esp->scsi_id;
518 esp->scsi_id_mask = 1 << esp->scsi_id;
520 mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL);
522 goto fail_free_command_block;
524 platform_set_drvdata(dev, mep);
526 switch (macintosh_config->scsi_type) {
527 case MAC_SCSI_QUADRA:
528 esp->cfreq = 16500000;
529 esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA;
530 mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
531 mep->pdma_regs = NULL;
533 case MAC_SCSI_QUADRA2:
534 esp->cfreq = 25000000;
535 esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 +
536 dev->id * MAC_ESP_REGS_SPACING);
537 mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
538 mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG +
539 dev->id * MAC_ESP_PDMA_REG_SPACING);
540 nubus_writel(0x1d1, mep->pdma_regs);
542 case MAC_SCSI_QUADRA3:
543 /* These quadras have a real DMA controller (the PSC) but we
544 * don't know how to drive it so we must use PIO instead.
546 esp->cfreq = 25000000;
547 esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3;
549 mep->pdma_regs = NULL;
553 esp->ops = &mac_esp_ops;
554 if (mep->pdma_io == NULL) {
555 printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id);
556 esp_write8(0, ESP_TCLOW);
557 esp_write8(0, ESP_TCMED);
558 esp->flags = ESP_FLAG_DISABLE_SYNC;
559 mac_esp_ops.send_dma_cmd = mac_esp_send_pio_cmd;
561 printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id);
564 host->irq = IRQ_MAC_SCSI;
565 esp_chips[dev->id] = esp;
567 if (esp_chips[!dev->id] == NULL) {
568 err = request_irq(host->irq, mac_scsi_esp_intr, 0, "ESP", NULL);
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 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 = esp_mac_remove,
619 .name = DRV_MODULE_NAME,
623 static int __init mac_esp_init(void)
625 return platform_driver_register(&esp_mac_driver);
628 static void __exit mac_esp_exit(void)
630 platform_driver_unregister(&esp_mac_driver);
633 MODULE_DESCRIPTION("Mac ESP SCSI driver");
634 MODULE_AUTHOR("Finn Thain <fthain@telegraphics.com.au>");
635 MODULE_LICENSE("GPL v2");
636 MODULE_VERSION(DRV_VERSION);
637 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
639 module_init(mac_esp_init);
640 module_exit(mac_esp_exit);