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> 22*5a0e3ad6STejun 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> 286fe07aafSFinn Thain 296fe07aafSFinn Thain #include <scsi/scsi_host.h> 306fe07aafSFinn Thain 316fe07aafSFinn Thain #include "esp_scsi.h" 326fe07aafSFinn Thain 336fe07aafSFinn Thain #define DRV_MODULE_NAME "mac_esp" 346fe07aafSFinn Thain #define PFX DRV_MODULE_NAME ": " 356fe07aafSFinn Thain #define DRV_VERSION "1.000" 366fe07aafSFinn Thain #define DRV_MODULE_RELDATE "Sept 15, 2007" 376fe07aafSFinn Thain 386fe07aafSFinn Thain #define MAC_ESP_IO_BASE 0x50F00000 396fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000) 406fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000) 416fe07aafSFinn Thain #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000) 426fe07aafSFinn Thain #define MAC_ESP_REGS_SPACING 0x402 436fe07aafSFinn Thain #define MAC_ESP_PDMA_REG 0xF9800024 446fe07aafSFinn Thain #define MAC_ESP_PDMA_REG_SPACING 0x4 456fe07aafSFinn Thain #define MAC_ESP_PDMA_IO_OFFSET 0x100 466fe07aafSFinn Thain 476fe07aafSFinn Thain #define esp_read8(REG) mac_esp_read8(esp, REG) 486fe07aafSFinn Thain #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG) 496fe07aafSFinn Thain 506fe07aafSFinn Thain struct mac_esp_priv { 516fe07aafSFinn Thain struct esp *esp; 526fe07aafSFinn Thain void __iomem *pdma_regs; 536fe07aafSFinn Thain void __iomem *pdma_io; 546fe07aafSFinn Thain int error; 556fe07aafSFinn Thain }; 56da244654SFinn Thain static struct esp *esp_chips[2]; 576fe07aafSFinn Thain 586fe07aafSFinn Thain #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \ 596fe07aafSFinn Thain platform_get_drvdata((struct platform_device *) \ 606fe07aafSFinn Thain (esp->dev))) 616fe07aafSFinn Thain 626fe07aafSFinn Thain static inline void mac_esp_write8(struct esp *esp, u8 val, unsigned long reg) 636fe07aafSFinn Thain { 646fe07aafSFinn Thain nubus_writeb(val, esp->regs + reg * 16); 656fe07aafSFinn Thain } 666fe07aafSFinn Thain 676fe07aafSFinn Thain static inline u8 mac_esp_read8(struct esp *esp, unsigned long reg) 686fe07aafSFinn Thain { 696fe07aafSFinn Thain return nubus_readb(esp->regs + reg * 16); 706fe07aafSFinn Thain } 716fe07aafSFinn Thain 726fe07aafSFinn Thain /* For pseudo DMA and PIO we need the virtual address 736fe07aafSFinn Thain * so this address mapping is the identity mapping. 746fe07aafSFinn Thain */ 756fe07aafSFinn Thain 766fe07aafSFinn Thain static dma_addr_t mac_esp_map_single(struct esp *esp, void *buf, 776fe07aafSFinn Thain size_t sz, int dir) 786fe07aafSFinn Thain { 796fe07aafSFinn Thain return (dma_addr_t)buf; 806fe07aafSFinn Thain } 816fe07aafSFinn Thain 826fe07aafSFinn Thain static int mac_esp_map_sg(struct esp *esp, struct scatterlist *sg, 836fe07aafSFinn Thain int num_sg, int dir) 846fe07aafSFinn Thain { 856fe07aafSFinn Thain int i; 866fe07aafSFinn Thain 876fe07aafSFinn Thain for (i = 0; i < num_sg; i++) 886fe07aafSFinn Thain sg[i].dma_address = (u32)sg_virt(&sg[i]); 896fe07aafSFinn Thain return num_sg; 906fe07aafSFinn Thain } 916fe07aafSFinn Thain 926fe07aafSFinn Thain static void mac_esp_unmap_single(struct esp *esp, dma_addr_t addr, 936fe07aafSFinn Thain size_t sz, int dir) 946fe07aafSFinn Thain { 956fe07aafSFinn Thain /* Nothing to do. */ 966fe07aafSFinn Thain } 976fe07aafSFinn Thain 986fe07aafSFinn Thain static void mac_esp_unmap_sg(struct esp *esp, struct scatterlist *sg, 996fe07aafSFinn Thain int num_sg, int dir) 1006fe07aafSFinn Thain { 1016fe07aafSFinn Thain /* Nothing to do. */ 1026fe07aafSFinn Thain } 1036fe07aafSFinn Thain 1046fe07aafSFinn Thain static void mac_esp_reset_dma(struct esp *esp) 1056fe07aafSFinn Thain { 1066fe07aafSFinn Thain /* Nothing to do. */ 1076fe07aafSFinn Thain } 1086fe07aafSFinn Thain 1096fe07aafSFinn Thain static void mac_esp_dma_drain(struct esp *esp) 1106fe07aafSFinn Thain { 1116fe07aafSFinn Thain /* Nothing to do. */ 1126fe07aafSFinn Thain } 1136fe07aafSFinn Thain 1146fe07aafSFinn Thain static void mac_esp_dma_invalidate(struct esp *esp) 1156fe07aafSFinn Thain { 1166fe07aafSFinn Thain /* Nothing to do. */ 1176fe07aafSFinn Thain } 1186fe07aafSFinn Thain 1196fe07aafSFinn Thain static int mac_esp_dma_error(struct esp *esp) 1206fe07aafSFinn Thain { 1216fe07aafSFinn Thain return MAC_ESP_GET_PRIV(esp)->error; 1226fe07aafSFinn Thain } 1236fe07aafSFinn Thain 1246fe07aafSFinn Thain static inline int mac_esp_wait_for_empty_fifo(struct esp *esp) 1256fe07aafSFinn Thain { 1266fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 1276fe07aafSFinn Thain int i = 500000; 1286fe07aafSFinn Thain 1296fe07aafSFinn Thain do { 1306fe07aafSFinn Thain if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES)) 1316fe07aafSFinn Thain return 0; 1326fe07aafSFinn Thain 1336fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR) 1346fe07aafSFinn Thain return 1; 1356fe07aafSFinn Thain 1366fe07aafSFinn Thain udelay(2); 1376fe07aafSFinn Thain } while (--i); 1386fe07aafSFinn Thain 1396fe07aafSFinn Thain printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n", 1406fe07aafSFinn Thain esp_read8(ESP_STATUS)); 1416fe07aafSFinn Thain mep->error = 1; 1426fe07aafSFinn Thain return 1; 1436fe07aafSFinn Thain } 1446fe07aafSFinn Thain 1456fe07aafSFinn Thain static inline int mac_esp_wait_for_dreq(struct esp *esp) 1466fe07aafSFinn Thain { 1476fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 1486fe07aafSFinn Thain int i = 500000; 1496fe07aafSFinn Thain 1506fe07aafSFinn Thain do { 1516fe07aafSFinn Thain if (mep->pdma_regs == NULL) { 1526fe07aafSFinn Thain if (mac_irq_pending(IRQ_MAC_SCSIDRQ)) 1536fe07aafSFinn Thain return 0; 1546fe07aafSFinn Thain } else { 1556fe07aafSFinn Thain if (nubus_readl(mep->pdma_regs) & 0x200) 1566fe07aafSFinn Thain return 0; 1576fe07aafSFinn Thain } 1586fe07aafSFinn Thain 1596fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR) 1606fe07aafSFinn Thain return 1; 1616fe07aafSFinn Thain 1626fe07aafSFinn Thain udelay(2); 1636fe07aafSFinn Thain } while (--i); 1646fe07aafSFinn Thain 1656fe07aafSFinn Thain printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n", 1666fe07aafSFinn Thain esp_read8(ESP_STATUS)); 1676fe07aafSFinn Thain mep->error = 1; 1686fe07aafSFinn Thain return 1; 1696fe07aafSFinn Thain } 1706fe07aafSFinn Thain 1716fe07aafSFinn Thain #define MAC_ESP_PDMA_LOOP(operands) \ 1726fe07aafSFinn Thain asm volatile ( \ 17309e13e91SFinn Thain " tstw %1 \n" \ 1746fe07aafSFinn Thain " jbeq 20f \n" \ 1756fe07aafSFinn Thain "1: movew " operands " \n" \ 1766fe07aafSFinn Thain "2: movew " operands " \n" \ 1776fe07aafSFinn Thain "3: movew " operands " \n" \ 1786fe07aafSFinn Thain "4: movew " operands " \n" \ 1796fe07aafSFinn Thain "5: movew " operands " \n" \ 1806fe07aafSFinn Thain "6: movew " operands " \n" \ 1816fe07aafSFinn Thain "7: movew " operands " \n" \ 1826fe07aafSFinn Thain "8: movew " operands " \n" \ 1836fe07aafSFinn Thain "9: movew " operands " \n" \ 1846fe07aafSFinn Thain "10: movew " operands " \n" \ 1856fe07aafSFinn Thain "11: movew " operands " \n" \ 1866fe07aafSFinn Thain "12: movew " operands " \n" \ 1876fe07aafSFinn Thain "13: movew " operands " \n" \ 1886fe07aafSFinn Thain "14: movew " operands " \n" \ 1896fe07aafSFinn Thain "15: movew " operands " \n" \ 1906fe07aafSFinn Thain "16: movew " operands " \n" \ 19109e13e91SFinn Thain " subqw #1,%1 \n" \ 1926fe07aafSFinn Thain " jbne 1b \n" \ 19309e13e91SFinn Thain "20: tstw %2 \n" \ 1946fe07aafSFinn Thain " jbeq 30f \n" \ 1956fe07aafSFinn Thain "21: movew " operands " \n" \ 19609e13e91SFinn Thain " subqw #1,%2 \n" \ 1976fe07aafSFinn Thain " jbne 21b \n" \ 19809e13e91SFinn Thain "30: tstw %3 \n" \ 1996fe07aafSFinn Thain " jbeq 40f \n" \ 2006fe07aafSFinn Thain "31: moveb " operands " \n" \ 2016fe07aafSFinn Thain "32: nop \n" \ 2026fe07aafSFinn Thain "40: \n" \ 2036fe07aafSFinn Thain " \n" \ 2046fe07aafSFinn Thain " .section __ex_table,\"a\" \n" \ 2056fe07aafSFinn Thain " .align 4 \n" \ 2066fe07aafSFinn Thain " .long 1b,40b \n" \ 2076fe07aafSFinn Thain " .long 2b,40b \n" \ 2086fe07aafSFinn Thain " .long 3b,40b \n" \ 2096fe07aafSFinn Thain " .long 4b,40b \n" \ 2106fe07aafSFinn Thain " .long 5b,40b \n" \ 2116fe07aafSFinn Thain " .long 6b,40b \n" \ 2126fe07aafSFinn Thain " .long 7b,40b \n" \ 2136fe07aafSFinn Thain " .long 8b,40b \n" \ 2146fe07aafSFinn Thain " .long 9b,40b \n" \ 2156fe07aafSFinn Thain " .long 10b,40b \n" \ 2166fe07aafSFinn Thain " .long 11b,40b \n" \ 2176fe07aafSFinn Thain " .long 12b,40b \n" \ 2186fe07aafSFinn Thain " .long 13b,40b \n" \ 2196fe07aafSFinn Thain " .long 14b,40b \n" \ 2206fe07aafSFinn Thain " .long 15b,40b \n" \ 2216fe07aafSFinn Thain " .long 16b,40b \n" \ 2226fe07aafSFinn Thain " .long 21b,40b \n" \ 2236fe07aafSFinn Thain " .long 31b,40b \n" \ 2246fe07aafSFinn Thain " .long 32b,40b \n" \ 2256fe07aafSFinn Thain " .previous \n" \ 22609e13e91SFinn Thain : "+a" (addr), "+r" (count32), "+r" (count2) \ 22709e13e91SFinn Thain : "g" (count1), "a" (mep->pdma_io)) 2286fe07aafSFinn Thain 2296fe07aafSFinn Thain static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count, 2306fe07aafSFinn Thain u32 dma_count, int write, u8 cmd) 2316fe07aafSFinn Thain { 2326fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 2336fe07aafSFinn Thain unsigned long flags; 2346fe07aafSFinn Thain 2356fe07aafSFinn Thain local_irq_save(flags); 2366fe07aafSFinn Thain 2376fe07aafSFinn Thain mep->error = 0; 2386fe07aafSFinn Thain 2396fe07aafSFinn Thain if (!write) 2406fe07aafSFinn Thain scsi_esp_cmd(esp, ESP_CMD_FLUSH); 2416fe07aafSFinn Thain 2426fe07aafSFinn Thain esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW); 2436fe07aafSFinn Thain esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED); 2446fe07aafSFinn Thain 2456fe07aafSFinn Thain scsi_esp_cmd(esp, cmd); 2466fe07aafSFinn Thain 2476fe07aafSFinn Thain do { 2486fe07aafSFinn Thain unsigned int count32 = esp_count >> 5; 2496fe07aafSFinn Thain unsigned int count2 = (esp_count & 0x1F) >> 1; 25009e13e91SFinn Thain unsigned int count1 = esp_count & 1; 2516fe07aafSFinn Thain unsigned int start_addr = addr; 2526fe07aafSFinn Thain 2536fe07aafSFinn Thain if (mac_esp_wait_for_dreq(esp)) 2546fe07aafSFinn Thain break; 2556fe07aafSFinn Thain 2566fe07aafSFinn Thain if (write) { 25709e13e91SFinn Thain MAC_ESP_PDMA_LOOP("%4@,%0@+"); 2586fe07aafSFinn Thain 2596fe07aafSFinn Thain esp_count -= addr - start_addr; 2606fe07aafSFinn Thain } else { 2616fe07aafSFinn Thain unsigned int n; 2626fe07aafSFinn Thain 26309e13e91SFinn Thain MAC_ESP_PDMA_LOOP("%0@+,%4@"); 2646fe07aafSFinn Thain 2656fe07aafSFinn Thain if (mac_esp_wait_for_empty_fifo(esp)) 2666fe07aafSFinn Thain break; 2676fe07aafSFinn Thain 2686fe07aafSFinn Thain n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW); 2696fe07aafSFinn Thain addr = start_addr + esp_count - n; 2706fe07aafSFinn Thain esp_count = n; 2716fe07aafSFinn Thain } 2726fe07aafSFinn Thain } while (esp_count); 2736fe07aafSFinn Thain 2746fe07aafSFinn Thain local_irq_restore(flags); 2756fe07aafSFinn Thain } 2766fe07aafSFinn Thain 2776fe07aafSFinn Thain /* 2786fe07aafSFinn Thain * Programmed IO routines follow. 2796fe07aafSFinn Thain */ 2806fe07aafSFinn Thain 28102507a80SFinn Thain static inline unsigned int mac_esp_wait_for_fifo(struct esp *esp) 2826fe07aafSFinn Thain { 2836fe07aafSFinn Thain int i = 500000; 2846fe07aafSFinn Thain 2856fe07aafSFinn Thain do { 28602507a80SFinn Thain unsigned int fbytes = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES; 28702507a80SFinn Thain 28802507a80SFinn Thain if (fbytes) 28902507a80SFinn Thain return fbytes; 2906fe07aafSFinn Thain 2916fe07aafSFinn Thain udelay(2); 2926fe07aafSFinn Thain } while (--i); 2936fe07aafSFinn Thain 2946fe07aafSFinn Thain printk(KERN_ERR PFX "FIFO is empty (sreg %02x)\n", 2956fe07aafSFinn Thain esp_read8(ESP_STATUS)); 29602507a80SFinn Thain return 0; 2976fe07aafSFinn Thain } 2986fe07aafSFinn Thain 2996fe07aafSFinn Thain static inline int mac_esp_wait_for_intr(struct esp *esp) 3006fe07aafSFinn Thain { 30102507a80SFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 3026fe07aafSFinn Thain int i = 500000; 3036fe07aafSFinn Thain 3046fe07aafSFinn Thain do { 3056fe07aafSFinn Thain esp->sreg = esp_read8(ESP_STATUS); 3066fe07aafSFinn Thain if (esp->sreg & ESP_STAT_INTR) 3076fe07aafSFinn Thain return 0; 3086fe07aafSFinn Thain 3096fe07aafSFinn Thain udelay(2); 3106fe07aafSFinn Thain } while (--i); 3116fe07aafSFinn Thain 3126fe07aafSFinn Thain printk(KERN_ERR PFX "IRQ timeout (sreg %02x)\n", esp->sreg); 31302507a80SFinn Thain mep->error = 1; 3146fe07aafSFinn Thain return 1; 3156fe07aafSFinn Thain } 3166fe07aafSFinn Thain 3176fe07aafSFinn Thain #define MAC_ESP_PIO_LOOP(operands, reg1) \ 3186fe07aafSFinn Thain asm volatile ( \ 3196fe07aafSFinn Thain "1: moveb " operands " \n" \ 3206fe07aafSFinn Thain " subqw #1,%1 \n" \ 3216fe07aafSFinn Thain " jbne 1b \n" \ 3226fe07aafSFinn Thain : "+a" (addr), "+r" (reg1) \ 3236fe07aafSFinn Thain : "a" (fifo)) 3246fe07aafSFinn Thain 3256fe07aafSFinn Thain #define MAC_ESP_PIO_FILL(operands, reg1) \ 3266fe07aafSFinn Thain asm volatile ( \ 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 " moveb " operands " \n" \ 3406fe07aafSFinn Thain " moveb " operands " \n" \ 3416fe07aafSFinn Thain " moveb " operands " \n" \ 3426fe07aafSFinn Thain " moveb " operands " \n" \ 3436fe07aafSFinn Thain " subqw #8,%1 \n" \ 3446fe07aafSFinn Thain " subqw #8,%1 \n" \ 3456fe07aafSFinn Thain : "+a" (addr), "+r" (reg1) \ 3466fe07aafSFinn Thain : "a" (fifo)) 3476fe07aafSFinn Thain 3486fe07aafSFinn Thain #define MAC_ESP_FIFO_SIZE 16 3496fe07aafSFinn Thain 3506fe07aafSFinn Thain static void mac_esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count, 3516fe07aafSFinn Thain u32 dma_count, int write, u8 cmd) 3526fe07aafSFinn Thain { 3536fe07aafSFinn Thain struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp); 3546fe07aafSFinn Thain u8 *fifo = esp->regs + ESP_FDATA * 16; 3556fe07aafSFinn Thain 35602507a80SFinn Thain disable_irq(esp->host->irq); 3576fe07aafSFinn Thain 3586fe07aafSFinn Thain cmd &= ~ESP_CMD_DMA; 3596fe07aafSFinn Thain mep->error = 0; 3606fe07aafSFinn Thain 3616fe07aafSFinn Thain if (write) { 3626fe07aafSFinn Thain scsi_esp_cmd(esp, cmd); 3636fe07aafSFinn Thain 36402507a80SFinn Thain while (1) { 36502507a80SFinn Thain unsigned int n; 36602507a80SFinn Thain 36702507a80SFinn Thain n = mac_esp_wait_for_fifo(esp); 36802507a80SFinn Thain if (!n) 36902507a80SFinn Thain break; 37002507a80SFinn Thain 37102507a80SFinn Thain if (n > esp_count) 37202507a80SFinn Thain n = esp_count; 37302507a80SFinn Thain esp_count -= n; 37402507a80SFinn Thain 37502507a80SFinn Thain MAC_ESP_PIO_LOOP("%2@,%0@+", n); 37602507a80SFinn Thain 37702507a80SFinn Thain if (!esp_count) 37802507a80SFinn Thain break; 37902507a80SFinn Thain 38002507a80SFinn Thain if (mac_esp_wait_for_intr(esp)) 38102507a80SFinn Thain break; 38202507a80SFinn Thain 38302507a80SFinn Thain if (((esp->sreg & ESP_STAT_PMASK) != ESP_DIP) && 38402507a80SFinn Thain ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)) 38502507a80SFinn Thain break; 38602507a80SFinn Thain 38702507a80SFinn Thain esp->ireg = esp_read8(ESP_INTRPT); 38802507a80SFinn Thain if ((esp->ireg & (ESP_INTR_DC | ESP_INTR_BSERV)) != 38902507a80SFinn Thain ESP_INTR_BSERV) 39002507a80SFinn Thain break; 39102507a80SFinn Thain 39202507a80SFinn Thain scsi_esp_cmd(esp, ESP_CMD_TI); 3936fe07aafSFinn Thain } 3946fe07aafSFinn Thain } else { 3956fe07aafSFinn Thain scsi_esp_cmd(esp, ESP_CMD_FLUSH); 3966fe07aafSFinn Thain 3976fe07aafSFinn Thain if (esp_count >= MAC_ESP_FIFO_SIZE) 3986fe07aafSFinn Thain MAC_ESP_PIO_FILL("%0@+,%2@", esp_count); 3996fe07aafSFinn Thain else 4006fe07aafSFinn Thain MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count); 4016fe07aafSFinn Thain 4026fe07aafSFinn Thain scsi_esp_cmd(esp, cmd); 4036fe07aafSFinn Thain 4046fe07aafSFinn Thain while (esp_count) { 4056fe07aafSFinn Thain unsigned int n; 4066fe07aafSFinn Thain 40702507a80SFinn Thain if (mac_esp_wait_for_intr(esp)) 4086fe07aafSFinn Thain break; 4096fe07aafSFinn Thain 41002507a80SFinn Thain if (((esp->sreg & ESP_STAT_PMASK) != ESP_DOP) && 41102507a80SFinn Thain ((esp->sreg & ESP_STAT_PMASK) != ESP_MOP)) 41202507a80SFinn Thain break; 41302507a80SFinn Thain 4146fe07aafSFinn Thain esp->ireg = esp_read8(ESP_INTRPT); 41502507a80SFinn Thain if ((esp->ireg & (ESP_INTR_DC | ESP_INTR_BSERV)) != 41602507a80SFinn Thain ESP_INTR_BSERV) 4176fe07aafSFinn Thain break; 4186fe07aafSFinn Thain 41902507a80SFinn Thain n = MAC_ESP_FIFO_SIZE - 42002507a80SFinn Thain (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES); 4216fe07aafSFinn Thain if (n > esp_count) 4226fe07aafSFinn Thain n = esp_count; 4236fe07aafSFinn Thain 4246fe07aafSFinn Thain if (n == MAC_ESP_FIFO_SIZE) { 4256fe07aafSFinn Thain MAC_ESP_PIO_FILL("%0@+,%2@", esp_count); 4266fe07aafSFinn Thain } else { 4276fe07aafSFinn Thain esp_count -= n; 4286fe07aafSFinn Thain MAC_ESP_PIO_LOOP("%0@+,%2@", n); 4296fe07aafSFinn Thain } 4306fe07aafSFinn Thain 4316fe07aafSFinn Thain scsi_esp_cmd(esp, ESP_CMD_TI); 4326fe07aafSFinn Thain } 4336fe07aafSFinn Thain } 4346fe07aafSFinn Thain 43502507a80SFinn Thain enable_irq(esp->host->irq); 4366fe07aafSFinn Thain } 4376fe07aafSFinn Thain 4386fe07aafSFinn Thain static int mac_esp_irq_pending(struct esp *esp) 4396fe07aafSFinn Thain { 4406fe07aafSFinn Thain if (esp_read8(ESP_STATUS) & ESP_STAT_INTR) 4416fe07aafSFinn Thain return 1; 4426fe07aafSFinn Thain return 0; 4436fe07aafSFinn Thain } 4446fe07aafSFinn Thain 4456fe07aafSFinn Thain static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len) 4466fe07aafSFinn Thain { 4476fe07aafSFinn Thain return dma_len > 0xFFFF ? 0xFFFF : dma_len; 4486fe07aafSFinn Thain } 4496fe07aafSFinn Thain 450da244654SFinn Thain static irqreturn_t mac_scsi_esp_intr(int irq, void *dev_id) 451da244654SFinn Thain { 452da244654SFinn Thain int got_intr; 453da244654SFinn Thain 454da244654SFinn Thain /* 455da244654SFinn Thain * This is an edge triggered IRQ, so we have to be careful to 456da244654SFinn Thain * avoid missing a transition when it is shared by two ESP devices. 457da244654SFinn Thain */ 458da244654SFinn Thain 459da244654SFinn Thain do { 460da244654SFinn Thain got_intr = 0; 461da244654SFinn Thain if (esp_chips[0] && 462da244654SFinn Thain (mac_esp_read8(esp_chips[0], ESP_STATUS) & ESP_STAT_INTR)) { 463da244654SFinn Thain (void)scsi_esp_intr(irq, esp_chips[0]); 464da244654SFinn Thain got_intr = 1; 465da244654SFinn Thain } 466da244654SFinn Thain if (esp_chips[1] && 467da244654SFinn Thain (mac_esp_read8(esp_chips[1], ESP_STATUS) & ESP_STAT_INTR)) { 468da244654SFinn Thain (void)scsi_esp_intr(irq, esp_chips[1]); 469da244654SFinn Thain got_intr = 1; 470da244654SFinn Thain } 471da244654SFinn Thain } while (got_intr); 472da244654SFinn Thain 473da244654SFinn Thain return IRQ_HANDLED; 474da244654SFinn Thain } 475da244654SFinn Thain 4766fe07aafSFinn Thain static struct esp_driver_ops mac_esp_ops = { 4776fe07aafSFinn Thain .esp_write8 = mac_esp_write8, 4786fe07aafSFinn Thain .esp_read8 = mac_esp_read8, 4796fe07aafSFinn Thain .map_single = mac_esp_map_single, 4806fe07aafSFinn Thain .map_sg = mac_esp_map_sg, 4816fe07aafSFinn Thain .unmap_single = mac_esp_unmap_single, 4826fe07aafSFinn Thain .unmap_sg = mac_esp_unmap_sg, 4836fe07aafSFinn Thain .irq_pending = mac_esp_irq_pending, 4846fe07aafSFinn Thain .dma_length_limit = mac_esp_dma_length_limit, 4856fe07aafSFinn Thain .reset_dma = mac_esp_reset_dma, 4866fe07aafSFinn Thain .dma_drain = mac_esp_dma_drain, 4876fe07aafSFinn Thain .dma_invalidate = mac_esp_dma_invalidate, 4886fe07aafSFinn Thain .send_dma_cmd = mac_esp_send_pdma_cmd, 4896fe07aafSFinn Thain .dma_error = mac_esp_dma_error, 4906fe07aafSFinn Thain }; 4916fe07aafSFinn Thain 4926fe07aafSFinn Thain static int __devinit esp_mac_probe(struct platform_device *dev) 4936fe07aafSFinn Thain { 4946fe07aafSFinn Thain struct scsi_host_template *tpnt = &scsi_esp_template; 4956fe07aafSFinn Thain struct Scsi_Host *host; 4966fe07aafSFinn Thain struct esp *esp; 4976fe07aafSFinn Thain int err; 4986fe07aafSFinn Thain struct mac_esp_priv *mep; 4996fe07aafSFinn Thain 5006fe07aafSFinn Thain if (!MACH_IS_MAC) 5016fe07aafSFinn Thain return -ENODEV; 5026fe07aafSFinn Thain 503cff75f1fSFinn Thain if (dev->id > 1) 5046fe07aafSFinn Thain return -ENODEV; 5056fe07aafSFinn Thain 5066fe07aafSFinn Thain host = scsi_host_alloc(tpnt, sizeof(struct esp)); 5076fe07aafSFinn Thain 5086fe07aafSFinn Thain err = -ENOMEM; 5096fe07aafSFinn Thain if (!host) 5106fe07aafSFinn Thain goto fail; 5116fe07aafSFinn Thain 5126fe07aafSFinn Thain host->max_id = 8; 5136fe07aafSFinn Thain host->use_clustering = DISABLE_CLUSTERING; 5146fe07aafSFinn Thain esp = shost_priv(host); 5156fe07aafSFinn Thain 5166fe07aafSFinn Thain esp->host = host; 5176fe07aafSFinn Thain esp->dev = dev; 5186fe07aafSFinn Thain 5196fe07aafSFinn Thain esp->command_block = kzalloc(16, GFP_KERNEL); 5206fe07aafSFinn Thain if (!esp->command_block) 5216fe07aafSFinn Thain goto fail_unlink; 5226fe07aafSFinn Thain esp->command_block_dma = (dma_addr_t)esp->command_block; 5236fe07aafSFinn Thain 5246fe07aafSFinn Thain esp->scsi_id = 7; 5256fe07aafSFinn Thain host->this_id = esp->scsi_id; 5266fe07aafSFinn Thain esp->scsi_id_mask = 1 << esp->scsi_id; 5276fe07aafSFinn Thain 5286fe07aafSFinn Thain mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL); 5296fe07aafSFinn Thain if (!mep) 5306fe07aafSFinn Thain goto fail_free_command_block; 5316fe07aafSFinn Thain mep->esp = esp; 5326fe07aafSFinn Thain platform_set_drvdata(dev, mep); 5336fe07aafSFinn Thain 5346fe07aafSFinn Thain switch (macintosh_config->scsi_type) { 5356fe07aafSFinn Thain case MAC_SCSI_QUADRA: 5366fe07aafSFinn Thain esp->cfreq = 16500000; 5376fe07aafSFinn Thain esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA; 5386fe07aafSFinn Thain mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET; 5396fe07aafSFinn Thain mep->pdma_regs = NULL; 5406fe07aafSFinn Thain break; 5416fe07aafSFinn Thain case MAC_SCSI_QUADRA2: 5426fe07aafSFinn Thain esp->cfreq = 25000000; 5436fe07aafSFinn Thain esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 + 5446fe07aafSFinn Thain dev->id * MAC_ESP_REGS_SPACING); 5456fe07aafSFinn Thain mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET; 5466fe07aafSFinn Thain mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG + 5476fe07aafSFinn Thain dev->id * MAC_ESP_PDMA_REG_SPACING); 5486fe07aafSFinn Thain nubus_writel(0x1d1, mep->pdma_regs); 5496fe07aafSFinn Thain break; 5506fe07aafSFinn Thain case MAC_SCSI_QUADRA3: 5516fe07aafSFinn Thain /* These quadras have a real DMA controller (the PSC) but we 5526fe07aafSFinn Thain * don't know how to drive it so we must use PIO instead. 5536fe07aafSFinn Thain */ 5546fe07aafSFinn Thain esp->cfreq = 25000000; 5556fe07aafSFinn Thain esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3; 5566fe07aafSFinn Thain mep->pdma_io = NULL; 5576fe07aafSFinn Thain mep->pdma_regs = NULL; 5586fe07aafSFinn Thain break; 5596fe07aafSFinn Thain } 5606fe07aafSFinn Thain 5616fe07aafSFinn Thain esp->ops = &mac_esp_ops; 5626fe07aafSFinn Thain if (mep->pdma_io == NULL) { 5636fe07aafSFinn Thain printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id); 5646fe07aafSFinn Thain esp_write8(0, ESP_TCLOW); 5656fe07aafSFinn Thain esp_write8(0, ESP_TCMED); 5666fe07aafSFinn Thain esp->flags = ESP_FLAG_DISABLE_SYNC; 5676fe07aafSFinn Thain mac_esp_ops.send_dma_cmd = mac_esp_send_pio_cmd; 5686fe07aafSFinn Thain } else { 5696fe07aafSFinn Thain printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id); 5706fe07aafSFinn Thain } 5716fe07aafSFinn Thain 5726fe07aafSFinn Thain host->irq = IRQ_MAC_SCSI; 573da244654SFinn Thain esp_chips[dev->id] = esp; 574da244654SFinn Thain mb(); 575da244654SFinn Thain if (esp_chips[!dev->id] == NULL) { 576da244654SFinn Thain err = request_irq(host->irq, mac_scsi_esp_intr, 0, 577da244654SFinn Thain "Mac ESP", NULL); 578da244654SFinn Thain if (err < 0) { 579da244654SFinn Thain esp_chips[dev->id] = NULL; 5806fe07aafSFinn Thain goto fail_free_priv; 581da244654SFinn Thain } 582da244654SFinn Thain } 5836fe07aafSFinn Thain 5846fe07aafSFinn Thain err = scsi_esp_register(esp, &dev->dev); 5856fe07aafSFinn Thain if (err) 5866fe07aafSFinn Thain goto fail_free_irq; 5876fe07aafSFinn Thain 5886fe07aafSFinn Thain return 0; 5896fe07aafSFinn Thain 5906fe07aafSFinn Thain fail_free_irq: 591da244654SFinn Thain if (esp_chips[!dev->id] == NULL) 5926fe07aafSFinn Thain free_irq(host->irq, esp); 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 6036fe07aafSFinn Thain static int __devexit 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 611da244654SFinn Thain esp_chips[dev->id] = NULL; 612da244654SFinn Thain if (!(esp_chips[0] || esp_chips[1])) 613da244654SFinn Thain free_irq(irq, NULL); 6146fe07aafSFinn Thain 6156fe07aafSFinn Thain kfree(mep); 6166fe07aafSFinn Thain 6176fe07aafSFinn Thain kfree(esp->command_block); 6186fe07aafSFinn Thain 6196fe07aafSFinn Thain scsi_host_put(esp->host); 6206fe07aafSFinn Thain 6216fe07aafSFinn Thain return 0; 6226fe07aafSFinn Thain } 6236fe07aafSFinn Thain 6246fe07aafSFinn Thain static struct platform_driver esp_mac_driver = { 6256fe07aafSFinn Thain .probe = esp_mac_probe, 6266fe07aafSFinn Thain .remove = __devexit_p(esp_mac_remove), 6276fe07aafSFinn Thain .driver = { 6286fe07aafSFinn Thain .name = DRV_MODULE_NAME, 629cff75f1fSFinn Thain .owner = THIS_MODULE, 6306fe07aafSFinn Thain }, 6316fe07aafSFinn Thain }; 6326fe07aafSFinn Thain 6336fe07aafSFinn Thain static int __init mac_esp_init(void) 6346fe07aafSFinn Thain { 635cff75f1fSFinn Thain return platform_driver_register(&esp_mac_driver); 6366fe07aafSFinn Thain } 6376fe07aafSFinn Thain 6386fe07aafSFinn Thain static void __exit mac_esp_exit(void) 6396fe07aafSFinn Thain { 6406fe07aafSFinn Thain platform_driver_unregister(&esp_mac_driver); 6416fe07aafSFinn Thain } 6426fe07aafSFinn Thain 6436fe07aafSFinn Thain MODULE_DESCRIPTION("Mac ESP SCSI driver"); 6446fe07aafSFinn Thain MODULE_AUTHOR("Finn Thain <fthain@telegraphics.com.au>"); 6456fe07aafSFinn Thain MODULE_LICENSE("GPL v2"); 6466fe07aafSFinn Thain MODULE_VERSION(DRV_VERSION); 647cff75f1fSFinn Thain MODULE_ALIAS("platform:" DRV_MODULE_NAME); 6486fe07aafSFinn Thain 6496fe07aafSFinn Thain module_init(mac_esp_init); 6506fe07aafSFinn Thain module_exit(mac_esp_exit); 651