16fe07aafSFinn Thain /* mac_esp.c: ESP front-end for Macintosh Quadra systems. 26fe07aafSFinn Thain * 36fe07aafSFinn Thain * Adapted from jazz_esp.c and the old mac_esp.c. 46fe07aafSFinn Thain * 56fe07aafSFinn Thain * The pseudo DMA algorithm is based on the one used in NetBSD. 66fe07aafSFinn Thain * See sys/arch/mac68k/obio/esp.c for some background information. 76fe07aafSFinn Thain * 86fe07aafSFinn Thain * Copyright (C) 2007-2008 Finn Thain 96fe07aafSFinn Thain */ 106fe07aafSFinn Thain 116fe07aafSFinn Thain #include <linux/kernel.h> 126fe07aafSFinn Thain #include <linux/types.h> 136fe07aafSFinn Thain #include <linux/module.h> 146fe07aafSFinn Thain #include <linux/init.h> 156fe07aafSFinn Thain #include <linux/interrupt.h> 166fe07aafSFinn Thain #include <linux/platform_device.h> 176fe07aafSFinn Thain #include <linux/dma-mapping.h> 186fe07aafSFinn Thain #include <linux/scatterlist.h> 196fe07aafSFinn Thain #include <linux/delay.h> 206fe07aafSFinn Thain #include <linux/io.h> 216fe07aafSFinn Thain #include <linux/nubus.h> 225a0e3ad6STejun Heo #include <linux/slab.h> 236fe07aafSFinn Thain 246fe07aafSFinn Thain #include <asm/irq.h> 256fe07aafSFinn Thain #include <asm/dma.h> 266fe07aafSFinn Thain #include <asm/macints.h> 276fe07aafSFinn Thain #include <asm/macintosh.h> 2830c0527dSFinn Thain #include <asm/mac_via.h> 296fe07aafSFinn Thain 306fe07aafSFinn Thain #include <scsi/scsi_host.h> 316fe07aafSFinn Thain 326fe07aafSFinn Thain #include "esp_scsi.h" 336fe07aafSFinn Thain 346fe07aafSFinn Thain #define DRV_MODULE_NAME "mac_esp" 356fe07aafSFinn Thain #define PFX DRV_MODULE_NAME ": " 366fe07aafSFinn Thain #define DRV_VERSION "1.000" 376fe07aafSFinn Thain #define DRV_MODULE_RELDATE "Sept 15, 2007" 386fe07aafSFinn Thain 396fe07aafSFinn Thain #define MAC_ESP_IO_BASE 0x50F00000 406fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000) 416fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000) 426fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000) 436fe07aafSFinn Thain #define MAC_ESP_REGS_SPACING 0x402 446fe07aafSFinn Thain #define MAC_ESP_PDMA_REG 0xF9800024 456fe07aafSFinn Thain #define MAC_ESP_PDMA_REG_SPACING 0x4 466fe07aafSFinn Thain #define MAC_ESP_PDMA_IO_OFFSET 0x100 476fe07aafSFinn Thain 486fe07aafSFinn Thain #define esp_read8(REG) mac_esp_read8(esp, REG) 496fe07aafSFinn Thain #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG) 506fe07aafSFinn Thain 516fe07aafSFinn Thain struct mac_esp_priv { 526fe07aafSFinn Thain struct esp *esp; 536fe07aafSFinn Thain void __iomem *pdma_regs; 546fe07aafSFinn Thain void __iomem *pdma_io; 556fe07aafSFinn Thain int error; 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 *) \ 61*98cda6a2SChristoph Hellwig dev_get_drvdata((esp)->dev)) 626fe07aafSFinn Thain 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 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 736fe07aafSFinn Thain /* For pseudo DMA and PIO we need the virtual address 746fe07aafSFinn Thain * so this address mapping is the identity mapping. 756fe07aafSFinn Thain */ 766fe07aafSFinn Thain 776fe07aafSFinn Thain static dma_addr_t mac_esp_map_single(struct esp *esp, void *buf, 786fe07aafSFinn Thain size_t sz, int dir) 796fe07aafSFinn Thain { 806fe07aafSFinn Thain return (dma_addr_t)buf; 816fe07aafSFinn Thain } 826fe07aafSFinn Thain 836fe07aafSFinn Thain static int mac_esp_map_sg(struct esp *esp, struct scatterlist *sg, 846fe07aafSFinn Thain int num_sg, int dir) 856fe07aafSFinn Thain { 866fe07aafSFinn Thain int i; 876fe07aafSFinn Thain 886fe07aafSFinn Thain for (i = 0; i < num_sg; i++) 896fe07aafSFinn Thain sg[i].dma_address = (u32)sg_virt(&sg[i]); 906fe07aafSFinn Thain return num_sg; 916fe07aafSFinn Thain } 926fe07aafSFinn Thain 936fe07aafSFinn Thain static void mac_esp_unmap_single(struct esp *esp, dma_addr_t addr, 946fe07aafSFinn Thain size_t sz, int dir) 956fe07aafSFinn Thain { 966fe07aafSFinn Thain /* Nothing to do. */ 976fe07aafSFinn Thain } 986fe07aafSFinn Thain 996fe07aafSFinn Thain static void mac_esp_unmap_sg(struct esp *esp, struct scatterlist *sg, 1006fe07aafSFinn Thain int num_sg, int dir) 1016fe07aafSFinn Thain { 1026fe07aafSFinn Thain /* Nothing to do. */ 1036fe07aafSFinn Thain } 1046fe07aafSFinn Thain 1056fe07aafSFinn Thain static void mac_esp_reset_dma(struct esp *esp) 1066fe07aafSFinn Thain { 1076fe07aafSFinn Thain /* Nothing to do. */ 1086fe07aafSFinn Thain } 1096fe07aafSFinn Thain 1106fe07aafSFinn Thain static void mac_esp_dma_drain(struct esp *esp) 1116fe07aafSFinn Thain { 1126fe07aafSFinn Thain /* Nothing to do. */ 1136fe07aafSFinn Thain } 1146fe07aafSFinn Thain 1156fe07aafSFinn Thain static void mac_esp_dma_invalidate(struct esp *esp) 1166fe07aafSFinn Thain { 1176fe07aafSFinn Thain /* Nothing to do. */ 1186fe07aafSFinn Thain } 1196fe07aafSFinn Thain 1206fe07aafSFinn Thain static int mac_esp_dma_error(struct esp *esp) 1216fe07aafSFinn Thain { 1226fe07aafSFinn Thain return MAC_ESP_GET_PRIV(esp)->error; 1236fe07aafSFinn Thain } 1246fe07aafSFinn Thain 1256fe07aafSFinn Thain static inline int mac_esp_wait_for_empty_fifo(struct esp *esp) 1266fe07aafSFinn Thain { 1276fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 1286fe07aafSFinn Thain int i = 500000; 1296fe07aafSFinn Thain 1306fe07aafSFinn Thain do { 1316fe07aafSFinn Thain if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES)) 1326fe07aafSFinn Thain return 0; 1336fe07aafSFinn Thain 1346fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR) 1356fe07aafSFinn Thain return 1; 1366fe07aafSFinn Thain 1376fe07aafSFinn Thain udelay(2); 1386fe07aafSFinn Thain } while (--i); 1396fe07aafSFinn Thain 1406fe07aafSFinn Thain printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n", 1416fe07aafSFinn Thain esp_read8(ESP_STATUS)); 1426fe07aafSFinn Thain mep->error = 1; 1436fe07aafSFinn Thain return 1; 1446fe07aafSFinn Thain } 1456fe07aafSFinn Thain 1466fe07aafSFinn Thain static inline int mac_esp_wait_for_dreq(struct esp *esp) 1476fe07aafSFinn Thain { 1486fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 1496fe07aafSFinn Thain int i = 500000; 1506fe07aafSFinn Thain 1516fe07aafSFinn Thain do { 1526fe07aafSFinn Thain if (mep->pdma_regs == NULL) { 15330c0527dSFinn Thain if (via2_scsi_drq_pending()) 1546fe07aafSFinn Thain return 0; 1556fe07aafSFinn Thain } else { 1566fe07aafSFinn Thain if (nubus_readl(mep->pdma_regs) & 0x200) 1576fe07aafSFinn Thain return 0; 1586fe07aafSFinn Thain } 1596fe07aafSFinn Thain 1606fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR) 1616fe07aafSFinn Thain return 1; 1626fe07aafSFinn Thain 1636fe07aafSFinn Thain udelay(2); 1646fe07aafSFinn Thain } while (--i); 1656fe07aafSFinn Thain 1666fe07aafSFinn Thain printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n", 1676fe07aafSFinn Thain esp_read8(ESP_STATUS)); 1686fe07aafSFinn Thain mep->error = 1; 1696fe07aafSFinn Thain return 1; 1706fe07aafSFinn Thain } 1716fe07aafSFinn Thain 1726fe07aafSFinn Thain #define MAC_ESP_PDMA_LOOP(operands) \ 1736fe07aafSFinn Thain asm volatile ( \ 17409e13e91SFinn Thain " tstw %1 \n" \ 1756fe07aafSFinn Thain " jbeq 20f \n" \ 1766fe07aafSFinn Thain "1: movew " operands " \n" \ 1776fe07aafSFinn Thain "2: movew " operands " \n" \ 1786fe07aafSFinn Thain "3: movew " operands " \n" \ 1796fe07aafSFinn Thain "4: movew " operands " \n" \ 1806fe07aafSFinn Thain "5: movew " operands " \n" \ 1816fe07aafSFinn Thain "6: movew " operands " \n" \ 1826fe07aafSFinn Thain "7: movew " operands " \n" \ 1836fe07aafSFinn Thain "8: movew " operands " \n" \ 1846fe07aafSFinn Thain "9: movew " operands " \n" \ 1856fe07aafSFinn Thain "10: movew " operands " \n" \ 1866fe07aafSFinn Thain "11: movew " operands " \n" \ 1876fe07aafSFinn Thain "12: movew " operands " \n" \ 1886fe07aafSFinn Thain "13: movew " operands " \n" \ 1896fe07aafSFinn Thain "14: movew " operands " \n" \ 1906fe07aafSFinn Thain "15: movew " operands " \n" \ 1916fe07aafSFinn Thain "16: movew " operands " \n" \ 19209e13e91SFinn Thain " subqw #1,%1 \n" \ 1936fe07aafSFinn Thain " jbne 1b \n" \ 19409e13e91SFinn Thain "20: tstw %2 \n" \ 1956fe07aafSFinn Thain " jbeq 30f \n" \ 1966fe07aafSFinn Thain "21: movew " operands " \n" \ 19709e13e91SFinn Thain " subqw #1,%2 \n" \ 1986fe07aafSFinn Thain " jbne 21b \n" \ 19909e13e91SFinn Thain "30: tstw %3 \n" \ 2006fe07aafSFinn Thain " jbeq 40f \n" \ 2016fe07aafSFinn Thain "31: moveb " operands " \n" \ 2026fe07aafSFinn Thain "32: nop \n" \ 2036fe07aafSFinn Thain "40: \n" \ 2046fe07aafSFinn Thain " \n" \ 2056fe07aafSFinn Thain " .section __ex_table,\"a\" \n" \ 2066fe07aafSFinn Thain " .align 4 \n" \ 2076fe07aafSFinn Thain " .long 1b,40b \n" \ 2086fe07aafSFinn Thain " .long 2b,40b \n" \ 2096fe07aafSFinn Thain " .long 3b,40b \n" \ 2106fe07aafSFinn Thain " .long 4b,40b \n" \ 2116fe07aafSFinn Thain " .long 5b,40b \n" \ 2126fe07aafSFinn Thain " .long 6b,40b \n" \ 2136fe07aafSFinn Thain " .long 7b,40b \n" \ 2146fe07aafSFinn Thain " .long 8b,40b \n" \ 2156fe07aafSFinn Thain " .long 9b,40b \n" \ 2166fe07aafSFinn Thain " .long 10b,40b \n" \ 2176fe07aafSFinn Thain " .long 11b,40b \n" \ 2186fe07aafSFinn Thain " .long 12b,40b \n" \ 2196fe07aafSFinn Thain " .long 13b,40b \n" \ 2206fe07aafSFinn Thain " .long 14b,40b \n" \ 2216fe07aafSFinn Thain " .long 15b,40b \n" \ 2226fe07aafSFinn Thain " .long 16b,40b \n" \ 2236fe07aafSFinn Thain " .long 21b,40b \n" \ 2246fe07aafSFinn Thain " .long 31b,40b \n" \ 2256fe07aafSFinn Thain " .long 32b,40b \n" \ 2266fe07aafSFinn Thain " .previous \n" \ 22709e13e91SFinn Thain : "+a" (addr), "+r" (count32), "+r" (count2) \ 22809e13e91SFinn Thain : "g" (count1), "a" (mep->pdma_io)) 2296fe07aafSFinn Thain 2306fe07aafSFinn Thain static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count, 2316fe07aafSFinn Thain u32 dma_count, int write, u8 cmd) 2326fe07aafSFinn Thain { 2336fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 2346fe07aafSFinn Thain 2356fe07aafSFinn Thain mep->error = 0; 2366fe07aafSFinn Thain 2376fe07aafSFinn Thain if (!write) 2386fe07aafSFinn Thain scsi_esp_cmd(esp, ESP_CMD_FLUSH); 2396fe07aafSFinn Thain 2406fe07aafSFinn Thain esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW); 2416fe07aafSFinn Thain esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED); 2426fe07aafSFinn Thain 2436fe07aafSFinn Thain scsi_esp_cmd(esp, cmd); 2446fe07aafSFinn Thain 2456fe07aafSFinn Thain do { 2466fe07aafSFinn Thain unsigned int count32 = esp_count >> 5; 2476fe07aafSFinn Thain unsigned int count2 = (esp_count & 0x1F) >> 1; 24809e13e91SFinn Thain unsigned int count1 = esp_count & 1; 2496fe07aafSFinn Thain unsigned int start_addr = addr; 2506fe07aafSFinn Thain 2516fe07aafSFinn Thain if (mac_esp_wait_for_dreq(esp)) 2526fe07aafSFinn Thain break; 2536fe07aafSFinn Thain 2546fe07aafSFinn Thain if (write) { 25509e13e91SFinn Thain MAC_ESP_PDMA_LOOP("%4@,%0@+"); 2566fe07aafSFinn Thain 2576fe07aafSFinn Thain esp_count -= addr - start_addr; 2586fe07aafSFinn Thain } else { 2596fe07aafSFinn Thain unsigned int n; 2606fe07aafSFinn Thain 26109e13e91SFinn Thain MAC_ESP_PDMA_LOOP("%0@+,%4@"); 2626fe07aafSFinn Thain 2636fe07aafSFinn Thain if (mac_esp_wait_for_empty_fifo(esp)) 2646fe07aafSFinn Thain break; 2656fe07aafSFinn Thain 2666fe07aafSFinn Thain n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW); 2676fe07aafSFinn Thain addr = start_addr + esp_count - n; 2686fe07aafSFinn Thain esp_count = n; 2696fe07aafSFinn Thain } 2706fe07aafSFinn Thain } while (esp_count); 2716fe07aafSFinn Thain } 2726fe07aafSFinn Thain 2736fe07aafSFinn Thain /* 2746fe07aafSFinn Thain * Programmed IO routines follow. 2756fe07aafSFinn Thain */ 2766fe07aafSFinn Thain 27702507a80SFinn Thain static inline unsigned int mac_esp_wait_for_fifo(struct esp *esp) 2786fe07aafSFinn Thain { 2796fe07aafSFinn Thain int i = 500000; 2806fe07aafSFinn Thain 2816fe07aafSFinn Thain do { 28202507a80SFinn Thain unsigned int fbytes = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES; 28302507a80SFinn Thain 28402507a80SFinn Thain if (fbytes) 28502507a80SFinn Thain return fbytes; 2866fe07aafSFinn Thain 2876fe07aafSFinn Thain udelay(2); 2886fe07aafSFinn Thain } while (--i); 2896fe07aafSFinn Thain 2906fe07aafSFinn Thain printk(KERN_ERR PFX "FIFO is empty (sreg %02x)\n", 2916fe07aafSFinn Thain esp_read8(ESP_STATUS)); 29202507a80SFinn Thain return 0; 2936fe07aafSFinn Thain } 2946fe07aafSFinn Thain 2956fe07aafSFinn Thain static inline int mac_esp_wait_for_intr(struct esp *esp) 2966fe07aafSFinn Thain { 29702507a80SFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 2986fe07aafSFinn Thain int i = 500000; 2996fe07aafSFinn Thain 3006fe07aafSFinn Thain do { 3016fe07aafSFinn Thain esp->sreg = esp_read8(ESP_STATUS); 3026fe07aafSFinn Thain if (esp->sreg & ESP_STAT_INTR) 3036fe07aafSFinn Thain return 0; 3046fe07aafSFinn Thain 3056fe07aafSFinn Thain udelay(2); 3066fe07aafSFinn Thain } while (--i); 3076fe07aafSFinn Thain 3086fe07aafSFinn Thain printk(KERN_ERR PFX "IRQ timeout (sreg %02x)\n", esp->sreg); 30902507a80SFinn Thain mep->error = 1; 3106fe07aafSFinn Thain return 1; 3116fe07aafSFinn Thain } 3126fe07aafSFinn Thain 3136fe07aafSFinn Thain #define MAC_ESP_PIO_LOOP(operands, reg1) \ 3146fe07aafSFinn Thain asm volatile ( \ 3156fe07aafSFinn Thain "1: moveb " operands " \n" \ 3166fe07aafSFinn Thain " subqw #1,%1 \n" \ 3176fe07aafSFinn Thain " jbne 1b \n" \ 3186fe07aafSFinn Thain : "+a" (addr), "+r" (reg1) \ 3196fe07aafSFinn Thain : "a" (fifo)) 3206fe07aafSFinn Thain 3216fe07aafSFinn Thain #define MAC_ESP_PIO_FILL(operands, reg1) \ 3226fe07aafSFinn Thain asm volatile ( \ 3236fe07aafSFinn Thain " moveb " operands " \n" \ 3246fe07aafSFinn Thain " moveb " operands " \n" \ 3256fe07aafSFinn Thain " moveb " operands " \n" \ 3266fe07aafSFinn Thain " moveb " operands " \n" \ 3276fe07aafSFinn Thain " moveb " operands " \n" \ 3286fe07aafSFinn Thain " moveb " operands " \n" \ 3296fe07aafSFinn Thain " moveb " operands " \n" \ 3306fe07aafSFinn Thain " moveb " operands " \n" \ 3316fe07aafSFinn Thain " moveb " operands " \n" \ 3326fe07aafSFinn Thain " moveb " operands " \n" \ 3336fe07aafSFinn Thain " moveb " operands " \n" \ 3346fe07aafSFinn Thain " moveb " operands " \n" \ 3356fe07aafSFinn Thain " moveb " operands " \n" \ 3366fe07aafSFinn Thain " moveb " operands " \n" \ 3376fe07aafSFinn Thain " moveb " operands " \n" \ 3386fe07aafSFinn Thain " moveb " operands " \n" \ 3396fe07aafSFinn Thain " subqw #8,%1 \n" \ 3406fe07aafSFinn Thain " subqw #8,%1 \n" \ 3416fe07aafSFinn Thain : "+a" (addr), "+r" (reg1) \ 3426fe07aafSFinn Thain : "a" (fifo)) 3436fe07aafSFinn Thain 3446fe07aafSFinn Thain #define MAC_ESP_FIFO_SIZE 16 3456fe07aafSFinn Thain 3466fe07aafSFinn Thain static void mac_esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count, 3476fe07aafSFinn Thain u32 dma_count, int write, u8 cmd) 3486fe07aafSFinn Thain { 3496fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 350b36c7db9SFinn Thain u8 __iomem *fifo = esp->regs + ESP_FDATA * 16; 3517640d91dSFinn Thain u8 phase = esp->sreg & ESP_STAT_PMASK; 3526fe07aafSFinn Thain 3536fe07aafSFinn Thain cmd &= ~ESP_CMD_DMA; 3546fe07aafSFinn Thain mep->error = 0; 3556fe07aafSFinn Thain 3566fe07aafSFinn Thain if (write) { 3577640d91dSFinn Thain u8 *dst = (u8 *)addr; 3587640d91dSFinn Thain u8 mask = ~(phase == ESP_MIP ? ESP_INTR_FDONE : ESP_INTR_BSERV); 3597640d91dSFinn Thain 3606fe07aafSFinn Thain scsi_esp_cmd(esp, cmd); 3616fe07aafSFinn Thain 36202507a80SFinn Thain while (1) { 3637640d91dSFinn Thain if (!mac_esp_wait_for_fifo(esp)) 36402507a80SFinn Thain break; 36502507a80SFinn Thain 3667640d91dSFinn Thain *dst++ = esp_read8(ESP_FDATA); 3677640d91dSFinn Thain --esp_count; 36802507a80SFinn Thain 36902507a80SFinn Thain if (!esp_count) 37002507a80SFinn Thain break; 37102507a80SFinn Thain 37202507a80SFinn Thain if (mac_esp_wait_for_intr(esp)) 37302507a80SFinn Thain break; 37402507a80SFinn Thain 3757640d91dSFinn Thain if ((esp->sreg & ESP_STAT_PMASK) != phase) 37602507a80SFinn Thain break; 37702507a80SFinn Thain 37802507a80SFinn Thain esp->ireg = esp_read8(ESP_INTRPT); 3797640d91dSFinn Thain if (esp->ireg & mask) { 3807640d91dSFinn Thain mep->error = 1; 38102507a80SFinn Thain break; 3827640d91dSFinn Thain } 3837640d91dSFinn Thain 3847640d91dSFinn Thain if (phase == ESP_MIP) 3857640d91dSFinn Thain scsi_esp_cmd(esp, ESP_CMD_MOK); 38602507a80SFinn Thain 38702507a80SFinn Thain scsi_esp_cmd(esp, ESP_CMD_TI); 3886fe07aafSFinn Thain } 3896fe07aafSFinn Thain } else { 3906fe07aafSFinn Thain scsi_esp_cmd(esp, ESP_CMD_FLUSH); 3916fe07aafSFinn Thain 3926fe07aafSFinn Thain if (esp_count >= MAC_ESP_FIFO_SIZE) 3936fe07aafSFinn Thain MAC_ESP_PIO_FILL("%0@+,%2@", esp_count); 3946fe07aafSFinn Thain else 3956fe07aafSFinn Thain MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count); 3966fe07aafSFinn Thain 3976fe07aafSFinn Thain scsi_esp_cmd(esp, cmd); 3986fe07aafSFinn Thain 3996fe07aafSFinn Thain while (esp_count) { 4006fe07aafSFinn Thain unsigned int n; 4016fe07aafSFinn Thain 40202507a80SFinn Thain if (mac_esp_wait_for_intr(esp)) 4036fe07aafSFinn Thain break; 4046fe07aafSFinn Thain 4057640d91dSFinn Thain if ((esp->sreg & ESP_STAT_PMASK) != phase) 40602507a80SFinn Thain break; 40702507a80SFinn Thain 4086fe07aafSFinn Thain esp->ireg = esp_read8(ESP_INTRPT); 4097640d91dSFinn Thain if (esp->ireg & ~ESP_INTR_BSERV) { 4107640d91dSFinn Thain mep->error = 1; 4116fe07aafSFinn Thain break; 4127640d91dSFinn Thain } 4136fe07aafSFinn Thain 41402507a80SFinn Thain n = MAC_ESP_FIFO_SIZE - 41502507a80SFinn Thain (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES); 4166fe07aafSFinn Thain if (n > esp_count) 4176fe07aafSFinn Thain n = esp_count; 4186fe07aafSFinn Thain 4196fe07aafSFinn Thain if (n == MAC_ESP_FIFO_SIZE) { 4206fe07aafSFinn Thain MAC_ESP_PIO_FILL("%0@+,%2@", esp_count); 4216fe07aafSFinn Thain } else { 4226fe07aafSFinn Thain esp_count -= n; 4236fe07aafSFinn Thain MAC_ESP_PIO_LOOP("%0@+,%2@", n); 4246fe07aafSFinn Thain } 4256fe07aafSFinn Thain 4266fe07aafSFinn Thain scsi_esp_cmd(esp, ESP_CMD_TI); 4276fe07aafSFinn Thain } 4286fe07aafSFinn Thain } 4296fe07aafSFinn Thain } 4306fe07aafSFinn Thain 4316fe07aafSFinn Thain static int mac_esp_irq_pending(struct esp *esp) 4326fe07aafSFinn Thain { 4336fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR) 4346fe07aafSFinn Thain return 1; 4356fe07aafSFinn Thain return 0; 4366fe07aafSFinn Thain } 4376fe07aafSFinn Thain 4386fe07aafSFinn Thain static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len) 4396fe07aafSFinn Thain { 4406fe07aafSFinn Thain return dma_len > 0xFFFF ? 0xFFFF : dma_len; 4416fe07aafSFinn Thain } 4426fe07aafSFinn Thain 443da244654SFinn Thain static irqreturn_t mac_scsi_esp_intr(int irq, void *dev_id) 444da244654SFinn Thain { 445da244654SFinn Thain int got_intr; 446da244654SFinn Thain 447da244654SFinn Thain /* 448da244654SFinn Thain * This is an edge triggered IRQ, so we have to be careful to 449da244654SFinn Thain * avoid missing a transition when it is shared by two ESP devices. 450da244654SFinn Thain */ 451da244654SFinn Thain 452da244654SFinn Thain do { 453da244654SFinn Thain got_intr = 0; 454da244654SFinn Thain if (esp_chips[0] && 455da244654SFinn Thain (mac_esp_read8(esp_chips[0], ESP_STATUS) & ESP_STAT_INTR)) { 456da244654SFinn Thain (void)scsi_esp_intr(irq, esp_chips[0]); 457da244654SFinn Thain got_intr = 1; 458da244654SFinn Thain } 459da244654SFinn Thain if (esp_chips[1] && 460da244654SFinn Thain (mac_esp_read8(esp_chips[1], ESP_STATUS) & ESP_STAT_INTR)) { 461da244654SFinn Thain (void)scsi_esp_intr(irq, esp_chips[1]); 462da244654SFinn Thain got_intr = 1; 463da244654SFinn Thain } 464da244654SFinn Thain } while (got_intr); 465da244654SFinn Thain 466da244654SFinn Thain return IRQ_HANDLED; 467da244654SFinn Thain } 468da244654SFinn Thain 4696fe07aafSFinn Thain static struct esp_driver_ops mac_esp_ops = { 4706fe07aafSFinn Thain .esp_write8 = mac_esp_write8, 4716fe07aafSFinn Thain .esp_read8 = mac_esp_read8, 4726fe07aafSFinn Thain .map_single = mac_esp_map_single, 4736fe07aafSFinn Thain .map_sg = mac_esp_map_sg, 4746fe07aafSFinn Thain .unmap_single = mac_esp_unmap_single, 4756fe07aafSFinn Thain .unmap_sg = mac_esp_unmap_sg, 4766fe07aafSFinn Thain .irq_pending = mac_esp_irq_pending, 4776fe07aafSFinn Thain .dma_length_limit = mac_esp_dma_length_limit, 4786fe07aafSFinn Thain .reset_dma = mac_esp_reset_dma, 4796fe07aafSFinn Thain .dma_drain = mac_esp_dma_drain, 4806fe07aafSFinn Thain .dma_invalidate = mac_esp_dma_invalidate, 4816fe07aafSFinn Thain .send_dma_cmd = mac_esp_send_pdma_cmd, 4826fe07aafSFinn Thain .dma_error = mac_esp_dma_error, 4836fe07aafSFinn Thain }; 4846fe07aafSFinn Thain 4856f039790SGreg Kroah-Hartman static int esp_mac_probe(struct platform_device *dev) 4866fe07aafSFinn Thain { 4876fe07aafSFinn Thain struct scsi_host_template *tpnt = &scsi_esp_template; 4886fe07aafSFinn Thain struct Scsi_Host *host; 4896fe07aafSFinn Thain struct esp *esp; 4906fe07aafSFinn Thain int err; 4916fe07aafSFinn Thain struct mac_esp_priv *mep; 4926fe07aafSFinn Thain 4936fe07aafSFinn Thain if (!MACH_IS_MAC) 4946fe07aafSFinn Thain return -ENODEV; 4956fe07aafSFinn Thain 496cff75f1fSFinn Thain if (dev->id > 1) 4976fe07aafSFinn Thain return -ENODEV; 4986fe07aafSFinn Thain 4996fe07aafSFinn Thain host = scsi_host_alloc(tpnt, sizeof(struct esp)); 5006fe07aafSFinn Thain 5016fe07aafSFinn Thain err = -ENOMEM; 5026fe07aafSFinn Thain if (!host) 5036fe07aafSFinn Thain goto fail; 5046fe07aafSFinn Thain 5056fe07aafSFinn Thain host->max_id = 8; 5066fe07aafSFinn Thain host->use_clustering = DISABLE_CLUSTERING; 5076fe07aafSFinn Thain esp = shost_priv(host); 5086fe07aafSFinn Thain 5096fe07aafSFinn Thain esp->host = host; 510*98cda6a2SChristoph Hellwig esp->dev = &dev->dev; 5116fe07aafSFinn Thain 5126fe07aafSFinn Thain esp->command_block = kzalloc(16, GFP_KERNEL); 5136fe07aafSFinn Thain if (!esp->command_block) 5146fe07aafSFinn Thain goto fail_unlink; 5156fe07aafSFinn Thain esp->command_block_dma = (dma_addr_t)esp->command_block; 5166fe07aafSFinn Thain 5176fe07aafSFinn Thain esp->scsi_id = 7; 5186fe07aafSFinn Thain host->this_id = esp->scsi_id; 5196fe07aafSFinn Thain esp->scsi_id_mask = 1 << esp->scsi_id; 5206fe07aafSFinn Thain 5216fe07aafSFinn Thain mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL); 5226fe07aafSFinn Thain if (!mep) 5236fe07aafSFinn Thain goto fail_free_command_block; 5246fe07aafSFinn Thain mep->esp = esp; 5256fe07aafSFinn Thain platform_set_drvdata(dev, mep); 5266fe07aafSFinn Thain 5276fe07aafSFinn Thain switch (macintosh_config->scsi_type) { 5286fe07aafSFinn Thain case MAC_SCSI_QUADRA: 5296fe07aafSFinn Thain esp->cfreq = 16500000; 5306fe07aafSFinn Thain esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA; 5316fe07aafSFinn Thain mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET; 5326fe07aafSFinn Thain mep->pdma_regs = NULL; 5336fe07aafSFinn Thain break; 5346fe07aafSFinn Thain case MAC_SCSI_QUADRA2: 5356fe07aafSFinn Thain esp->cfreq = 25000000; 5366fe07aafSFinn Thain esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 + 5376fe07aafSFinn Thain dev->id * MAC_ESP_REGS_SPACING); 5386fe07aafSFinn Thain mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET; 5396fe07aafSFinn Thain mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG + 5406fe07aafSFinn Thain dev->id * MAC_ESP_PDMA_REG_SPACING); 5416fe07aafSFinn Thain nubus_writel(0x1d1, mep->pdma_regs); 5426fe07aafSFinn Thain break; 5436fe07aafSFinn Thain case MAC_SCSI_QUADRA3: 5446fe07aafSFinn Thain /* These quadras have a real DMA controller (the PSC) but we 5456fe07aafSFinn Thain * don't know how to drive it so we must use PIO instead. 5466fe07aafSFinn Thain */ 5476fe07aafSFinn Thain esp->cfreq = 25000000; 5486fe07aafSFinn Thain esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3; 5496fe07aafSFinn Thain mep->pdma_io = NULL; 5506fe07aafSFinn Thain mep->pdma_regs = NULL; 5516fe07aafSFinn Thain break; 5526fe07aafSFinn Thain } 5536fe07aafSFinn Thain 5546fe07aafSFinn Thain esp->ops = &mac_esp_ops; 5556fe07aafSFinn Thain if (mep->pdma_io == NULL) { 5566fe07aafSFinn Thain printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id); 5576fe07aafSFinn Thain esp_write8(0, ESP_TCLOW); 5586fe07aafSFinn Thain esp_write8(0, ESP_TCMED); 5596fe07aafSFinn Thain esp->flags = ESP_FLAG_DISABLE_SYNC; 5606fe07aafSFinn Thain mac_esp_ops.send_dma_cmd = mac_esp_send_pio_cmd; 5616fe07aafSFinn Thain } else { 5626fe07aafSFinn Thain printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id); 5636fe07aafSFinn Thain } 5646fe07aafSFinn Thain 5656fe07aafSFinn Thain host->irq = IRQ_MAC_SCSI; 5664da2b1ebSFinn Thain 5674da2b1ebSFinn Thain /* The request_irq() call is intended to succeed for the first device 5684da2b1ebSFinn Thain * and fail for the second device. 5694da2b1ebSFinn Thain */ 570c808d3d8SFinn Thain err = request_irq(host->irq, mac_scsi_esp_intr, 0, "ESP", NULL); 5714da2b1ebSFinn Thain spin_lock(&esp_chips_lock); 5724da2b1ebSFinn Thain if (err < 0 && esp_chips[!dev->id] == NULL) { 5734da2b1ebSFinn Thain spin_unlock(&esp_chips_lock); 5746fe07aafSFinn Thain goto fail_free_priv; 575da244654SFinn Thain } 5764da2b1ebSFinn Thain esp_chips[dev->id] = esp; 5774da2b1ebSFinn Thain spin_unlock(&esp_chips_lock); 5786fe07aafSFinn Thain 5796fe07aafSFinn Thain err = scsi_esp_register(esp, &dev->dev); 5806fe07aafSFinn Thain if (err) 5816fe07aafSFinn Thain goto fail_free_irq; 5826fe07aafSFinn Thain 5836fe07aafSFinn Thain return 0; 5846fe07aafSFinn Thain 5856fe07aafSFinn Thain fail_free_irq: 5864da2b1ebSFinn Thain spin_lock(&esp_chips_lock); 5874da2b1ebSFinn Thain esp_chips[dev->id] = NULL; 5884da2b1ebSFinn Thain if (esp_chips[!dev->id] == NULL) { 5894da2b1ebSFinn Thain spin_unlock(&esp_chips_lock); 590c0e3a6acSWei Yongjun free_irq(host->irq, NULL); 5914da2b1ebSFinn Thain } else 5924da2b1ebSFinn Thain spin_unlock(&esp_chips_lock); 5936fe07aafSFinn Thain fail_free_priv: 5946fe07aafSFinn Thain kfree(mep); 5956fe07aafSFinn Thain fail_free_command_block: 5966fe07aafSFinn Thain kfree(esp->command_block); 5976fe07aafSFinn Thain fail_unlink: 5986fe07aafSFinn Thain scsi_host_put(host); 5996fe07aafSFinn Thain fail: 6006fe07aafSFinn Thain return err; 6016fe07aafSFinn Thain } 6026fe07aafSFinn Thain 6036f039790SGreg Kroah-Hartman static int esp_mac_remove(struct platform_device *dev) 6046fe07aafSFinn Thain { 6056fe07aafSFinn Thain struct mac_esp_priv *mep = platform_get_drvdata(dev); 6066fe07aafSFinn Thain struct esp *esp = mep->esp; 6076fe07aafSFinn Thain unsigned int irq = esp->host->irq; 6086fe07aafSFinn Thain 6096fe07aafSFinn Thain scsi_esp_unregister(esp); 6106fe07aafSFinn Thain 6114da2b1ebSFinn Thain spin_lock(&esp_chips_lock); 612da244654SFinn Thain esp_chips[dev->id] = NULL; 6134da2b1ebSFinn Thain if (esp_chips[!dev->id] == NULL) { 6144da2b1ebSFinn Thain spin_unlock(&esp_chips_lock); 615da244654SFinn Thain free_irq(irq, NULL); 6164da2b1ebSFinn Thain } else 6174da2b1ebSFinn Thain spin_unlock(&esp_chips_lock); 6186fe07aafSFinn Thain 6196fe07aafSFinn Thain kfree(mep); 6206fe07aafSFinn Thain 6216fe07aafSFinn Thain kfree(esp->command_block); 6226fe07aafSFinn Thain 6236fe07aafSFinn Thain scsi_host_put(esp->host); 6246fe07aafSFinn Thain 6256fe07aafSFinn Thain return 0; 6266fe07aafSFinn Thain } 6276fe07aafSFinn Thain 6286fe07aafSFinn Thain static struct platform_driver esp_mac_driver = { 6296fe07aafSFinn Thain .probe = esp_mac_probe, 6306f039790SGreg Kroah-Hartman .remove = esp_mac_remove, 6316fe07aafSFinn Thain .driver = { 6326fe07aafSFinn Thain .name = DRV_MODULE_NAME, 6336fe07aafSFinn Thain }, 6346fe07aafSFinn Thain }; 6356fe07aafSFinn Thain 6366fe07aafSFinn Thain static int __init mac_esp_init(void) 6376fe07aafSFinn Thain { 638cff75f1fSFinn Thain return platform_driver_register(&esp_mac_driver); 6396fe07aafSFinn Thain } 6406fe07aafSFinn Thain 6416fe07aafSFinn Thain static void __exit mac_esp_exit(void) 6426fe07aafSFinn Thain { 6436fe07aafSFinn Thain platform_driver_unregister(&esp_mac_driver); 6446fe07aafSFinn Thain } 6456fe07aafSFinn Thain 6466fe07aafSFinn Thain MODULE_DESCRIPTION("Mac ESP SCSI driver"); 647d0c2c269SFinn Thain MODULE_AUTHOR("Finn Thain"); 6486fe07aafSFinn Thain MODULE_LICENSE("GPL v2"); 6496fe07aafSFinn Thain MODULE_VERSION(DRV_VERSION); 650cff75f1fSFinn Thain MODULE_ALIAS("platform:" DRV_MODULE_NAME); 6516fe07aafSFinn Thain 6526fe07aafSFinn Thain module_init(mac_esp_init); 6536fe07aafSFinn Thain module_exit(mac_esp_exit); 654