109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26fe07aafSFinn Thain /* mac_esp.c: ESP front-end for Macintosh Quadra systems.
36fe07aafSFinn Thain *
46fe07aafSFinn Thain * Adapted from jazz_esp.c and the old mac_esp.c.
56fe07aafSFinn Thain *
66fe07aafSFinn Thain * The pseudo DMA algorithm is based on the one used in NetBSD.
76fe07aafSFinn Thain * See sys/arch/mac68k/obio/esp.c for some background information.
86fe07aafSFinn Thain *
96fe07aafSFinn Thain * Copyright (C) 2007-2008 Finn Thain
106fe07aafSFinn Thain */
116fe07aafSFinn Thain
126fe07aafSFinn Thain #include <linux/kernel.h>
136fe07aafSFinn Thain #include <linux/types.h>
146fe07aafSFinn Thain #include <linux/module.h>
156fe07aafSFinn Thain #include <linux/init.h>
166fe07aafSFinn Thain #include <linux/interrupt.h>
176fe07aafSFinn Thain #include <linux/platform_device.h>
186fe07aafSFinn Thain #include <linux/dma-mapping.h>
196fe07aafSFinn Thain #include <linux/scatterlist.h>
206fe07aafSFinn Thain #include <linux/delay.h>
216fe07aafSFinn Thain #include <linux/io.h>
226fe07aafSFinn Thain #include <linux/nubus.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
246fe07aafSFinn Thain
256fe07aafSFinn Thain #include <asm/irq.h>
266fe07aafSFinn Thain #include <asm/dma.h>
276fe07aafSFinn Thain #include <asm/macints.h>
286fe07aafSFinn Thain #include <asm/macintosh.h>
2930c0527dSFinn Thain #include <asm/mac_via.h>
306fe07aafSFinn Thain
316fe07aafSFinn Thain #include <scsi/scsi_host.h>
326fe07aafSFinn Thain
336fe07aafSFinn Thain #include "esp_scsi.h"
346fe07aafSFinn Thain
356fe07aafSFinn Thain #define DRV_MODULE_NAME "mac_esp"
366fe07aafSFinn Thain #define PFX DRV_MODULE_NAME ": "
376fe07aafSFinn Thain #define DRV_VERSION "1.000"
386fe07aafSFinn Thain #define DRV_MODULE_RELDATE "Sept 15, 2007"
396fe07aafSFinn Thain
406fe07aafSFinn Thain #define MAC_ESP_IO_BASE 0x50F00000
416fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000)
426fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000)
436fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000)
446fe07aafSFinn Thain #define MAC_ESP_REGS_SPACING 0x402
456fe07aafSFinn Thain #define MAC_ESP_PDMA_REG 0xF9800024
466fe07aafSFinn Thain #define MAC_ESP_PDMA_REG_SPACING 0x4
476fe07aafSFinn Thain #define MAC_ESP_PDMA_IO_OFFSET 0x100
486fe07aafSFinn Thain
496fe07aafSFinn Thain #define esp_read8(REG) mac_esp_read8(esp, REG)
506fe07aafSFinn Thain #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
516fe07aafSFinn Thain
526fe07aafSFinn Thain struct mac_esp_priv {
536fe07aafSFinn Thain struct esp *esp;
546fe07aafSFinn Thain void __iomem *pdma_regs;
556fe07aafSFinn Thain void __iomem *pdma_io;
566fe07aafSFinn Thain };
57da244654SFinn Thain static struct esp *esp_chips[2];
584da2b1ebSFinn Thain static DEFINE_SPINLOCK(esp_chips_lock);
596fe07aafSFinn Thain
606fe07aafSFinn Thain #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
6198cda6a2SChristoph Hellwig dev_get_drvdata((esp)->dev))
626fe07aafSFinn Thain
mac_esp_write8(struct esp * esp,u8 val,unsigned long reg)636fe07aafSFinn Thain static inline void mac_esp_write8(struct esp *esp, u8 val, unsigned long reg)
646fe07aafSFinn Thain {
656fe07aafSFinn Thain nubus_writeb(val, esp->regs + reg * 16);
666fe07aafSFinn Thain }
676fe07aafSFinn Thain
mac_esp_read8(struct esp * esp,unsigned long reg)686fe07aafSFinn Thain static inline u8 mac_esp_read8(struct esp *esp, unsigned long reg)
696fe07aafSFinn Thain {
706fe07aafSFinn Thain return nubus_readb(esp->regs + reg * 16);
716fe07aafSFinn Thain }
726fe07aafSFinn Thain
mac_esp_reset_dma(struct esp * esp)736fe07aafSFinn Thain static void mac_esp_reset_dma(struct esp *esp)
746fe07aafSFinn Thain {
756fe07aafSFinn Thain /* Nothing to do. */
766fe07aafSFinn Thain }
776fe07aafSFinn Thain
mac_esp_dma_drain(struct esp * esp)786fe07aafSFinn Thain static void mac_esp_dma_drain(struct esp *esp)
796fe07aafSFinn Thain {
806fe07aafSFinn Thain /* Nothing to do. */
816fe07aafSFinn Thain }
826fe07aafSFinn Thain
mac_esp_dma_invalidate(struct esp * esp)836fe07aafSFinn Thain static void mac_esp_dma_invalidate(struct esp *esp)
846fe07aafSFinn Thain {
856fe07aafSFinn Thain /* Nothing to do. */
866fe07aafSFinn Thain }
876fe07aafSFinn Thain
mac_esp_dma_error(struct esp * esp)886fe07aafSFinn Thain static int mac_esp_dma_error(struct esp *esp)
896fe07aafSFinn Thain {
9053dce332SFinn Thain return esp->send_cmd_error;
916fe07aafSFinn Thain }
926fe07aafSFinn Thain
mac_esp_wait_for_empty_fifo(struct esp * esp)936fe07aafSFinn Thain static inline int mac_esp_wait_for_empty_fifo(struct esp *esp)
946fe07aafSFinn Thain {
956fe07aafSFinn Thain int i = 500000;
966fe07aafSFinn Thain
976fe07aafSFinn Thain do {
986fe07aafSFinn Thain if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES))
996fe07aafSFinn Thain return 0;
1006fe07aafSFinn Thain
1016fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
1026fe07aafSFinn Thain return 1;
1036fe07aafSFinn Thain
1046fe07aafSFinn Thain udelay(2);
1056fe07aafSFinn Thain } while (--i);
1066fe07aafSFinn Thain
1076fe07aafSFinn Thain printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n",
1086fe07aafSFinn Thain esp_read8(ESP_STATUS));
10953dce332SFinn Thain esp->send_cmd_error = 1;
1106fe07aafSFinn Thain return 1;
1116fe07aafSFinn Thain }
1126fe07aafSFinn Thain
mac_esp_wait_for_dreq(struct esp * esp)1136fe07aafSFinn Thain static inline int mac_esp_wait_for_dreq(struct esp *esp)
1146fe07aafSFinn Thain {
1156fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
1166fe07aafSFinn Thain int i = 500000;
1176fe07aafSFinn Thain
1186fe07aafSFinn Thain do {
1196fe07aafSFinn Thain if (mep->pdma_regs == NULL) {
12030c0527dSFinn Thain if (via2_scsi_drq_pending())
1216fe07aafSFinn Thain return 0;
1226fe07aafSFinn Thain } else {
1236fe07aafSFinn Thain if (nubus_readl(mep->pdma_regs) & 0x200)
1246fe07aafSFinn Thain return 0;
1256fe07aafSFinn Thain }
1266fe07aafSFinn Thain
1276fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
1286fe07aafSFinn Thain return 1;
1296fe07aafSFinn Thain
1306fe07aafSFinn Thain udelay(2);
1316fe07aafSFinn Thain } while (--i);
1326fe07aafSFinn Thain
1336fe07aafSFinn Thain printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n",
1346fe07aafSFinn Thain esp_read8(ESP_STATUS));
13553dce332SFinn Thain esp->send_cmd_error = 1;
1366fe07aafSFinn Thain return 1;
1376fe07aafSFinn Thain }
1386fe07aafSFinn Thain
1396fe07aafSFinn Thain #define MAC_ESP_PDMA_LOOP(operands) \
1406fe07aafSFinn Thain asm volatile ( \
14109e13e91SFinn Thain " tstw %1 \n" \
1426fe07aafSFinn Thain " jbeq 20f \n" \
1436fe07aafSFinn Thain "1: movew " operands " \n" \
1446fe07aafSFinn Thain "2: movew " operands " \n" \
1456fe07aafSFinn Thain "3: movew " operands " \n" \
1466fe07aafSFinn Thain "4: movew " operands " \n" \
1476fe07aafSFinn Thain "5: movew " operands " \n" \
1486fe07aafSFinn Thain "6: movew " operands " \n" \
1496fe07aafSFinn Thain "7: movew " operands " \n" \
1506fe07aafSFinn Thain "8: movew " operands " \n" \
1516fe07aafSFinn Thain "9: movew " operands " \n" \
1526fe07aafSFinn Thain "10: movew " operands " \n" \
1536fe07aafSFinn Thain "11: movew " operands " \n" \
1546fe07aafSFinn Thain "12: movew " operands " \n" \
1556fe07aafSFinn Thain "13: movew " operands " \n" \
1566fe07aafSFinn Thain "14: movew " operands " \n" \
1576fe07aafSFinn Thain "15: movew " operands " \n" \
1586fe07aafSFinn Thain "16: movew " operands " \n" \
15909e13e91SFinn Thain " subqw #1,%1 \n" \
1606fe07aafSFinn Thain " jbne 1b \n" \
16109e13e91SFinn Thain "20: tstw %2 \n" \
1626fe07aafSFinn Thain " jbeq 30f \n" \
1636fe07aafSFinn Thain "21: movew " operands " \n" \
16409e13e91SFinn Thain " subqw #1,%2 \n" \
1656fe07aafSFinn Thain " jbne 21b \n" \
16609e13e91SFinn Thain "30: tstw %3 \n" \
1676fe07aafSFinn Thain " jbeq 40f \n" \
1686fe07aafSFinn Thain "31: moveb " operands " \n" \
1696fe07aafSFinn Thain "32: nop \n" \
1706fe07aafSFinn Thain "40: \n" \
1716fe07aafSFinn Thain " \n" \
1726fe07aafSFinn Thain " .section __ex_table,\"a\" \n" \
1736fe07aafSFinn Thain " .align 4 \n" \
1746fe07aafSFinn Thain " .long 1b,40b \n" \
1756fe07aafSFinn Thain " .long 2b,40b \n" \
1766fe07aafSFinn Thain " .long 3b,40b \n" \
1776fe07aafSFinn Thain " .long 4b,40b \n" \
1786fe07aafSFinn Thain " .long 5b,40b \n" \
1796fe07aafSFinn Thain " .long 6b,40b \n" \
1806fe07aafSFinn Thain " .long 7b,40b \n" \
1816fe07aafSFinn Thain " .long 8b,40b \n" \
1826fe07aafSFinn Thain " .long 9b,40b \n" \
1836fe07aafSFinn Thain " .long 10b,40b \n" \
1846fe07aafSFinn Thain " .long 11b,40b \n" \
1856fe07aafSFinn Thain " .long 12b,40b \n" \
1866fe07aafSFinn Thain " .long 13b,40b \n" \
1876fe07aafSFinn Thain " .long 14b,40b \n" \
1886fe07aafSFinn Thain " .long 15b,40b \n" \
1896fe07aafSFinn Thain " .long 16b,40b \n" \
1906fe07aafSFinn Thain " .long 21b,40b \n" \
1916fe07aafSFinn Thain " .long 31b,40b \n" \
1926fe07aafSFinn Thain " .long 32b,40b \n" \
1936fe07aafSFinn Thain " .previous \n" \
19409e13e91SFinn Thain : "+a" (addr), "+r" (count32), "+r" (count2) \
19509e13e91SFinn Thain : "g" (count1), "a" (mep->pdma_io))
1966fe07aafSFinn Thain
mac_esp_send_pdma_cmd(struct esp * esp,u32 addr,u32 esp_count,u32 dma_count,int write,u8 cmd)1976fe07aafSFinn Thain static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count,
1986fe07aafSFinn Thain u32 dma_count, int write, u8 cmd)
1996fe07aafSFinn Thain {
2006fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
2016fe07aafSFinn Thain
20253dce332SFinn Thain esp->send_cmd_error = 0;
2036fe07aafSFinn Thain
2046fe07aafSFinn Thain if (!write)
2056fe07aafSFinn Thain scsi_esp_cmd(esp, ESP_CMD_FLUSH);
2066fe07aafSFinn Thain
2076fe07aafSFinn Thain esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW);
2086fe07aafSFinn Thain esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED);
2096fe07aafSFinn Thain
2106fe07aafSFinn Thain scsi_esp_cmd(esp, cmd);
2116fe07aafSFinn Thain
2126fe07aafSFinn Thain do {
2136fe07aafSFinn Thain unsigned int count32 = esp_count >> 5;
2146fe07aafSFinn Thain unsigned int count2 = (esp_count & 0x1F) >> 1;
21509e13e91SFinn Thain unsigned int count1 = esp_count & 1;
2166fe07aafSFinn Thain unsigned int start_addr = addr;
2176fe07aafSFinn Thain
2186fe07aafSFinn Thain if (mac_esp_wait_for_dreq(esp))
2196fe07aafSFinn Thain break;
2206fe07aafSFinn Thain
2216fe07aafSFinn Thain if (write) {
22209e13e91SFinn Thain MAC_ESP_PDMA_LOOP("%4@,%0@+");
2236fe07aafSFinn Thain
2246fe07aafSFinn Thain esp_count -= addr - start_addr;
2256fe07aafSFinn Thain } else {
2266fe07aafSFinn Thain unsigned int n;
2276fe07aafSFinn Thain
22809e13e91SFinn Thain MAC_ESP_PDMA_LOOP("%0@+,%4@");
2296fe07aafSFinn Thain
2306fe07aafSFinn Thain if (mac_esp_wait_for_empty_fifo(esp))
2316fe07aafSFinn Thain break;
2326fe07aafSFinn Thain
2336fe07aafSFinn Thain n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW);
2346fe07aafSFinn Thain addr = start_addr + esp_count - n;
2356fe07aafSFinn Thain esp_count = n;
2366fe07aafSFinn Thain }
2376fe07aafSFinn Thain } while (esp_count);
2386fe07aafSFinn Thain }
2396fe07aafSFinn Thain
mac_esp_irq_pending(struct esp * esp)2406fe07aafSFinn Thain static int mac_esp_irq_pending(struct esp *esp)
2416fe07aafSFinn Thain {
2426fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
2436fe07aafSFinn Thain return 1;
2446fe07aafSFinn Thain return 0;
2456fe07aafSFinn Thain }
2466fe07aafSFinn Thain
mac_esp_dma_length_limit(struct esp * esp,u32 dma_addr,u32 dma_len)2476fe07aafSFinn Thain static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
2486fe07aafSFinn Thain {
2496fe07aafSFinn Thain return dma_len > 0xFFFF ? 0xFFFF : dma_len;
2506fe07aafSFinn Thain }
2516fe07aafSFinn Thain
mac_scsi_esp_intr(int irq,void * dev_id)252da244654SFinn Thain static irqreturn_t mac_scsi_esp_intr(int irq, void *dev_id)
253da244654SFinn Thain {
254da244654SFinn Thain int got_intr;
255da244654SFinn Thain
256da244654SFinn Thain /*
257da244654SFinn Thain * This is an edge triggered IRQ, so we have to be careful to
258da244654SFinn Thain * avoid missing a transition when it is shared by two ESP devices.
259da244654SFinn Thain */
260da244654SFinn Thain
261da244654SFinn Thain do {
262da244654SFinn Thain got_intr = 0;
263da244654SFinn Thain if (esp_chips[0] &&
264da244654SFinn Thain (mac_esp_read8(esp_chips[0], ESP_STATUS) & ESP_STAT_INTR)) {
265da244654SFinn Thain (void)scsi_esp_intr(irq, esp_chips[0]);
266da244654SFinn Thain got_intr = 1;
267da244654SFinn Thain }
268da244654SFinn Thain if (esp_chips[1] &&
269da244654SFinn Thain (mac_esp_read8(esp_chips[1], ESP_STATUS) & ESP_STAT_INTR)) {
270da244654SFinn Thain (void)scsi_esp_intr(irq, esp_chips[1]);
271da244654SFinn Thain got_intr = 1;
272da244654SFinn Thain }
273da244654SFinn Thain } while (got_intr);
274da244654SFinn Thain
275da244654SFinn Thain return IRQ_HANDLED;
276da244654SFinn Thain }
277da244654SFinn Thain
2786fe07aafSFinn Thain static struct esp_driver_ops mac_esp_ops = {
2796fe07aafSFinn Thain .esp_write8 = mac_esp_write8,
2806fe07aafSFinn Thain .esp_read8 = mac_esp_read8,
2816fe07aafSFinn Thain .irq_pending = mac_esp_irq_pending,
2826fe07aafSFinn Thain .dma_length_limit = mac_esp_dma_length_limit,
2836fe07aafSFinn Thain .reset_dma = mac_esp_reset_dma,
2846fe07aafSFinn Thain .dma_drain = mac_esp_dma_drain,
2856fe07aafSFinn Thain .dma_invalidate = mac_esp_dma_invalidate,
2866fe07aafSFinn Thain .send_dma_cmd = mac_esp_send_pdma_cmd,
2876fe07aafSFinn Thain .dma_error = mac_esp_dma_error,
2886fe07aafSFinn Thain };
2896fe07aafSFinn Thain
esp_mac_probe(struct platform_device * dev)2906f039790SGreg Kroah-Hartman static int esp_mac_probe(struct platform_device *dev)
2916fe07aafSFinn Thain {
292*3b465a14SBart Van Assche const struct scsi_host_template *tpnt = &scsi_esp_template;
2936fe07aafSFinn Thain struct Scsi_Host *host;
2946fe07aafSFinn Thain struct esp *esp;
2956fe07aafSFinn Thain int err;
2966fe07aafSFinn Thain struct mac_esp_priv *mep;
2976fe07aafSFinn Thain
2986fe07aafSFinn Thain if (!MACH_IS_MAC)
2996fe07aafSFinn Thain return -ENODEV;
3006fe07aafSFinn Thain
301cff75f1fSFinn Thain if (dev->id > 1)
3026fe07aafSFinn Thain return -ENODEV;
3036fe07aafSFinn Thain
3046fe07aafSFinn Thain host = scsi_host_alloc(tpnt, sizeof(struct esp));
3056fe07aafSFinn Thain
3066fe07aafSFinn Thain err = -ENOMEM;
3076fe07aafSFinn Thain if (!host)
3086fe07aafSFinn Thain goto fail;
3096fe07aafSFinn Thain
3106fe07aafSFinn Thain host->max_id = 8;
3114af14d11SChristoph Hellwig host->dma_boundary = PAGE_SIZE - 1;
3126fe07aafSFinn Thain esp = shost_priv(host);
3136fe07aafSFinn Thain
3146fe07aafSFinn Thain esp->host = host;
31598cda6a2SChristoph Hellwig esp->dev = &dev->dev;
3166fe07aafSFinn Thain
3176fe07aafSFinn Thain esp->command_block = kzalloc(16, GFP_KERNEL);
3186fe07aafSFinn Thain if (!esp->command_block)
3196fe07aafSFinn Thain goto fail_unlink;
3206fe07aafSFinn Thain esp->command_block_dma = (dma_addr_t)esp->command_block;
3216fe07aafSFinn Thain
3226fe07aafSFinn Thain esp->scsi_id = 7;
3236fe07aafSFinn Thain host->this_id = esp->scsi_id;
3246fe07aafSFinn Thain esp->scsi_id_mask = 1 << esp->scsi_id;
3256fe07aafSFinn Thain
3266fe07aafSFinn Thain mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL);
3276fe07aafSFinn Thain if (!mep)
3286fe07aafSFinn Thain goto fail_free_command_block;
3296fe07aafSFinn Thain mep->esp = esp;
3306fe07aafSFinn Thain platform_set_drvdata(dev, mep);
3316fe07aafSFinn Thain
3326fe07aafSFinn Thain switch (macintosh_config->scsi_type) {
3336fe07aafSFinn Thain case MAC_SCSI_QUADRA:
3346fe07aafSFinn Thain esp->cfreq = 16500000;
3356fe07aafSFinn Thain esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA;
3366fe07aafSFinn Thain mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
3376fe07aafSFinn Thain mep->pdma_regs = NULL;
3386fe07aafSFinn Thain break;
3396fe07aafSFinn Thain case MAC_SCSI_QUADRA2:
3406fe07aafSFinn Thain esp->cfreq = 25000000;
3416fe07aafSFinn Thain esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 +
3426fe07aafSFinn Thain dev->id * MAC_ESP_REGS_SPACING);
3436fe07aafSFinn Thain mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
3446fe07aafSFinn Thain mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG +
3456fe07aafSFinn Thain dev->id * MAC_ESP_PDMA_REG_SPACING);
3466fe07aafSFinn Thain nubus_writel(0x1d1, mep->pdma_regs);
3476fe07aafSFinn Thain break;
3486fe07aafSFinn Thain case MAC_SCSI_QUADRA3:
3496fe07aafSFinn Thain /* These quadras have a real DMA controller (the PSC) but we
3506fe07aafSFinn Thain * don't know how to drive it so we must use PIO instead.
3516fe07aafSFinn Thain */
3526fe07aafSFinn Thain esp->cfreq = 25000000;
3536fe07aafSFinn Thain esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3;
3546fe07aafSFinn Thain mep->pdma_io = NULL;
3556fe07aafSFinn Thain mep->pdma_regs = NULL;
3566fe07aafSFinn Thain break;
3576fe07aafSFinn Thain }
35853dce332SFinn Thain esp->fifo_reg = esp->regs + ESP_FDATA * 16;
3596fe07aafSFinn Thain
3606fe07aafSFinn Thain esp->ops = &mac_esp_ops;
3613f9295b6SChristoph Hellwig esp->flags = ESP_FLAG_NO_DMA_MAP;
3626fe07aafSFinn Thain if (mep->pdma_io == NULL) {
3636fe07aafSFinn Thain printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id);
3646fe07aafSFinn Thain esp_write8(0, ESP_TCLOW);
3656fe07aafSFinn Thain esp_write8(0, ESP_TCMED);
3663f9295b6SChristoph Hellwig esp->flags |= ESP_FLAG_DISABLE_SYNC;
36753dce332SFinn Thain mac_esp_ops.send_dma_cmd = esp_send_pio_cmd;
3686fe07aafSFinn Thain } else {
3696fe07aafSFinn Thain printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id);
3706fe07aafSFinn Thain }
3716fe07aafSFinn Thain
3726fe07aafSFinn Thain host->irq = IRQ_MAC_SCSI;
3734da2b1ebSFinn Thain
3744da2b1ebSFinn Thain /* The request_irq() call is intended to succeed for the first device
3754da2b1ebSFinn Thain * and fail for the second device.
3764da2b1ebSFinn Thain */
377c808d3d8SFinn Thain err = request_irq(host->irq, mac_scsi_esp_intr, 0, "ESP", NULL);
3784da2b1ebSFinn Thain spin_lock(&esp_chips_lock);
3794da2b1ebSFinn Thain if (err < 0 && esp_chips[!dev->id] == NULL) {
3804da2b1ebSFinn Thain spin_unlock(&esp_chips_lock);
3816fe07aafSFinn Thain goto fail_free_priv;
382da244654SFinn Thain }
3834da2b1ebSFinn Thain esp_chips[dev->id] = esp;
3844da2b1ebSFinn Thain spin_unlock(&esp_chips_lock);
3856fe07aafSFinn Thain
38644b1b4d2SChristoph Hellwig err = scsi_esp_register(esp);
3876fe07aafSFinn Thain if (err)
3886fe07aafSFinn Thain goto fail_free_irq;
3896fe07aafSFinn Thain
3906fe07aafSFinn Thain return 0;
3916fe07aafSFinn Thain
3926fe07aafSFinn Thain fail_free_irq:
3934da2b1ebSFinn Thain spin_lock(&esp_chips_lock);
3944da2b1ebSFinn Thain esp_chips[dev->id] = NULL;
3954da2b1ebSFinn Thain if (esp_chips[!dev->id] == NULL) {
3964da2b1ebSFinn Thain spin_unlock(&esp_chips_lock);
397c0e3a6acSWei Yongjun free_irq(host->irq, NULL);
3984da2b1ebSFinn Thain } else
3994da2b1ebSFinn Thain spin_unlock(&esp_chips_lock);
4006fe07aafSFinn Thain fail_free_priv:
4016fe07aafSFinn Thain kfree(mep);
4026fe07aafSFinn Thain fail_free_command_block:
4036fe07aafSFinn Thain kfree(esp->command_block);
4046fe07aafSFinn Thain fail_unlink:
4056fe07aafSFinn Thain scsi_host_put(host);
4066fe07aafSFinn Thain fail:
4076fe07aafSFinn Thain return err;
4086fe07aafSFinn Thain }
4096fe07aafSFinn Thain
esp_mac_remove(struct platform_device * dev)4106f039790SGreg Kroah-Hartman static int esp_mac_remove(struct platform_device *dev)
4116fe07aafSFinn Thain {
4126fe07aafSFinn Thain struct mac_esp_priv *mep = platform_get_drvdata(dev);
4136fe07aafSFinn Thain struct esp *esp = mep->esp;
4146fe07aafSFinn Thain unsigned int irq = esp->host->irq;
4156fe07aafSFinn Thain
4166fe07aafSFinn Thain scsi_esp_unregister(esp);
4176fe07aafSFinn Thain
4184da2b1ebSFinn Thain spin_lock(&esp_chips_lock);
419da244654SFinn Thain esp_chips[dev->id] = NULL;
4204da2b1ebSFinn Thain if (esp_chips[!dev->id] == NULL) {
4214da2b1ebSFinn Thain spin_unlock(&esp_chips_lock);
422da244654SFinn Thain free_irq(irq, NULL);
4234da2b1ebSFinn Thain } else
4244da2b1ebSFinn Thain spin_unlock(&esp_chips_lock);
4256fe07aafSFinn Thain
4266fe07aafSFinn Thain kfree(mep);
4276fe07aafSFinn Thain
4286fe07aafSFinn Thain kfree(esp->command_block);
4296fe07aafSFinn Thain
4306fe07aafSFinn Thain scsi_host_put(esp->host);
4316fe07aafSFinn Thain
4326fe07aafSFinn Thain return 0;
4336fe07aafSFinn Thain }
4346fe07aafSFinn Thain
4356fe07aafSFinn Thain static struct platform_driver esp_mac_driver = {
4366fe07aafSFinn Thain .probe = esp_mac_probe,
4376f039790SGreg Kroah-Hartman .remove = esp_mac_remove,
4386fe07aafSFinn Thain .driver = {
4396fe07aafSFinn Thain .name = DRV_MODULE_NAME,
4406fe07aafSFinn Thain },
4416fe07aafSFinn Thain };
442d30fbf2cSLiu Shixin module_platform_driver(esp_mac_driver);
4436fe07aafSFinn Thain
4446fe07aafSFinn Thain MODULE_DESCRIPTION("Mac ESP SCSI driver");
445d0c2c269SFinn Thain MODULE_AUTHOR("Finn Thain");
4466fe07aafSFinn Thain MODULE_LICENSE("GPL v2");
4476fe07aafSFinn Thain MODULE_VERSION(DRV_VERSION);
448cff75f1fSFinn Thain MODULE_ALIAS("platform:" DRV_MODULE_NAME);
449