xref: /linux/drivers/ata/pata_ep93xx.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22fff2751SRafal Prylowski /*
32fff2751SRafal Prylowski  * EP93XX PATA controller driver.
42fff2751SRafal Prylowski  *
52fff2751SRafal Prylowski  * Copyright (c) 2012, Metasoft s.c.
62fff2751SRafal Prylowski  *	Rafal Prylowski <prylowski@metasoft.pl>
72fff2751SRafal Prylowski  *
82fff2751SRafal Prylowski  * Based on pata_scc.c, pata_icside.c and on earlier version of EP93XX
92fff2751SRafal Prylowski  * PATA driver by Lennert Buytenhek and Alessandro Zummo.
102fff2751SRafal Prylowski  * Read/Write timings, resource management and other improvements
112fff2751SRafal Prylowski  * from driver by Joao Ramos and Bartlomiej Zolnierkiewicz.
122fff2751SRafal Prylowski  * DMA engine support based on spi-ep93xx.c by Mika Westerberg.
132fff2751SRafal Prylowski  *
142fff2751SRafal Prylowski  * Original copyrights:
152fff2751SRafal Prylowski  *
162fff2751SRafal Prylowski  * Support for Cirrus Logic's EP93xx (EP9312, EP9315) CPUs
172fff2751SRafal Prylowski  * PATA host controller driver.
182fff2751SRafal Prylowski  *
192fff2751SRafal Prylowski  * Copyright (c) 2009, Bartlomiej Zolnierkiewicz
202fff2751SRafal Prylowski  *
212fff2751SRafal Prylowski  * Heavily based on the ep93xx-ide.c driver:
222fff2751SRafal Prylowski  *
232fff2751SRafal Prylowski  * Copyright (c) 2009, Joao Ramos <joao.ramos@inov.pt>
242fff2751SRafal Prylowski  *		      INESC Inovacao (INOV)
252fff2751SRafal Prylowski  *
262fff2751SRafal Prylowski  * EP93XX PATA controller driver.
272fff2751SRafal Prylowski  * Copyright (C) 2007 Lennert Buytenhek <buytenh@wantstofly.org>
282fff2751SRafal Prylowski  *
292fff2751SRafal Prylowski  * An ATA driver for the Cirrus Logic EP93xx PATA controller.
302fff2751SRafal Prylowski  *
312fff2751SRafal Prylowski  * Based on an earlier version by Alessandro Zummo, which is:
322fff2751SRafal Prylowski  *   Copyright (C) 2006 Tower Technologies
332fff2751SRafal Prylowski  */
342fff2751SRafal Prylowski 
3593316e26SThierry Reding #include <linux/err.h>
362fff2751SRafal Prylowski #include <linux/kernel.h>
372fff2751SRafal Prylowski #include <linux/module.h>
382fff2751SRafal Prylowski #include <linux/blkdev.h>
392fff2751SRafal Prylowski #include <scsi/scsi_host.h>
402fff2751SRafal Prylowski #include <linux/ata.h>
412fff2751SRafal Prylowski #include <linux/libata.h>
422fff2751SRafal Prylowski #include <linux/platform_device.h>
43*db15538bSNikita Shubin #include <linux/sys_soc.h>
442fff2751SRafal Prylowski #include <linux/delay.h>
452fff2751SRafal Prylowski #include <linux/dmaengine.h>
462fff2751SRafal Prylowski #include <linux/ktime.h>
472fff2751SRafal Prylowski #include <linux/mod_devicetable.h>
48a3b29245SArnd Bergmann 
4967e38f57SArnd Bergmann #include <linux/soc/cirrus/ep93xx.h>
502fff2751SRafal Prylowski 
512fff2751SRafal Prylowski #define DRV_NAME	"ep93xx-ide"
522fff2751SRafal Prylowski #define DRV_VERSION	"1.0"
532fff2751SRafal Prylowski 
542fff2751SRafal Prylowski enum {
552fff2751SRafal Prylowski 	/* IDE Control Register */
562fff2751SRafal Prylowski 	IDECTRL				= 0x00,
572fff2751SRafal Prylowski 	IDECTRL_CS0N			= (1 << 0),
582fff2751SRafal Prylowski 	IDECTRL_CS1N			= (1 << 1),
592fff2751SRafal Prylowski 	IDECTRL_DIORN			= (1 << 5),
602fff2751SRafal Prylowski 	IDECTRL_DIOWN			= (1 << 6),
612fff2751SRafal Prylowski 	IDECTRL_INTRQ			= (1 << 9),
622fff2751SRafal Prylowski 	IDECTRL_IORDY			= (1 << 10),
632fff2751SRafal Prylowski 	/*
642fff2751SRafal Prylowski 	 * the device IDE register to be accessed is selected through
652fff2751SRafal Prylowski 	 * IDECTRL register's specific bitfields 'DA', 'CS1N' and 'CS0N':
662fff2751SRafal Prylowski 	 *   b4   b3   b2    b1     b0
672fff2751SRafal Prylowski 	 *   A2   A1   A0   CS1N   CS0N
682fff2751SRafal Prylowski 	 * the values filled in this structure allows the value to be directly
692fff2751SRafal Prylowski 	 * ORed to the IDECTRL register, hence giving directly the A[2:0] and
702fff2751SRafal Prylowski 	 * CS1N/CS0N values for each IDE register.
712fff2751SRafal Prylowski 	 * The values correspond to the transformation:
722fff2751SRafal Prylowski 	 *   ((real IDE address) << 2) | CS1N value << 1 | CS0N value
732fff2751SRafal Prylowski 	 */
742fff2751SRafal Prylowski 	IDECTRL_ADDR_CMD		= 0 + 2, /* CS1 */
752fff2751SRafal Prylowski 	IDECTRL_ADDR_DATA		= (ATA_REG_DATA << 2) + 2,
762fff2751SRafal Prylowski 	IDECTRL_ADDR_ERROR		= (ATA_REG_ERR << 2) + 2,
772fff2751SRafal Prylowski 	IDECTRL_ADDR_FEATURE		= (ATA_REG_FEATURE << 2) + 2,
782fff2751SRafal Prylowski 	IDECTRL_ADDR_NSECT		= (ATA_REG_NSECT << 2) + 2,
792fff2751SRafal Prylowski 	IDECTRL_ADDR_LBAL		= (ATA_REG_LBAL << 2) + 2,
802fff2751SRafal Prylowski 	IDECTRL_ADDR_LBAM		= (ATA_REG_LBAM << 2) + 2,
812fff2751SRafal Prylowski 	IDECTRL_ADDR_LBAH		= (ATA_REG_LBAH << 2) + 2,
822fff2751SRafal Prylowski 	IDECTRL_ADDR_DEVICE		= (ATA_REG_DEVICE << 2) + 2,
832fff2751SRafal Prylowski 	IDECTRL_ADDR_STATUS		= (ATA_REG_STATUS << 2) + 2,
842fff2751SRafal Prylowski 	IDECTRL_ADDR_COMMAND		= (ATA_REG_CMD << 2) + 2,
852fff2751SRafal Prylowski 	IDECTRL_ADDR_ALTSTATUS		= (0x06 << 2) + 1, /* CS0 */
862fff2751SRafal Prylowski 	IDECTRL_ADDR_CTL		= (0x06 << 2) + 1, /* CS0 */
872fff2751SRafal Prylowski 
882fff2751SRafal Prylowski 	/* IDE Configuration Register */
892fff2751SRafal Prylowski 	IDECFG				= 0x04,
902fff2751SRafal Prylowski 	IDECFG_IDEEN			= (1 << 0),
912fff2751SRafal Prylowski 	IDECFG_PIO			= (1 << 1),
922fff2751SRafal Prylowski 	IDECFG_MDMA			= (1 << 2),
932fff2751SRafal Prylowski 	IDECFG_UDMA			= (1 << 3),
942fff2751SRafal Prylowski 	IDECFG_MODE_SHIFT		= 4,
952fff2751SRafal Prylowski 	IDECFG_MODE_MASK		= (0xf << 4),
962fff2751SRafal Prylowski 	IDECFG_WST_SHIFT		= 8,
972fff2751SRafal Prylowski 	IDECFG_WST_MASK			= (0x3 << 8),
982fff2751SRafal Prylowski 
992fff2751SRafal Prylowski 	/* MDMA Operation Register */
1002fff2751SRafal Prylowski 	IDEMDMAOP			= 0x08,
1012fff2751SRafal Prylowski 
1022fff2751SRafal Prylowski 	/* UDMA Operation Register */
1032fff2751SRafal Prylowski 	IDEUDMAOP			= 0x0c,
1042fff2751SRafal Prylowski 	IDEUDMAOP_UEN			= (1 << 0),
1052fff2751SRafal Prylowski 	IDEUDMAOP_RWOP			= (1 << 1),
1062fff2751SRafal Prylowski 
1072fff2751SRafal Prylowski 	/* PIO/MDMA/UDMA Data Registers */
1082fff2751SRafal Prylowski 	IDEDATAOUT			= 0x10,
1092fff2751SRafal Prylowski 	IDEDATAIN			= 0x14,
1102fff2751SRafal Prylowski 	IDEMDMADATAOUT			= 0x18,
1112fff2751SRafal Prylowski 	IDEMDMADATAIN			= 0x1c,
1122fff2751SRafal Prylowski 	IDEUDMADATAOUT			= 0x20,
1132fff2751SRafal Prylowski 	IDEUDMADATAIN			= 0x24,
1142fff2751SRafal Prylowski 
1152fff2751SRafal Prylowski 	/* UDMA Status Register */
1162fff2751SRafal Prylowski 	IDEUDMASTS			= 0x28,
1172fff2751SRafal Prylowski 	IDEUDMASTS_DMAIDE		= (1 << 16),
1182fff2751SRafal Prylowski 	IDEUDMASTS_INTIDE		= (1 << 17),
1192fff2751SRafal Prylowski 	IDEUDMASTS_SBUSY		= (1 << 18),
1202fff2751SRafal Prylowski 	IDEUDMASTS_NDO			= (1 << 24),
1212fff2751SRafal Prylowski 	IDEUDMASTS_NDI			= (1 << 25),
1222fff2751SRafal Prylowski 	IDEUDMASTS_N4X			= (1 << 26),
1232fff2751SRafal Prylowski 
1242fff2751SRafal Prylowski 	/* UDMA Debug Status Register */
1252fff2751SRafal Prylowski 	IDEUDMADEBUG			= 0x2c,
1262fff2751SRafal Prylowski };
1272fff2751SRafal Prylowski 
1282fff2751SRafal Prylowski struct ep93xx_pata_data {
1292fff2751SRafal Prylowski 	struct platform_device *pdev;
1302fff2751SRafal Prylowski 	void __iomem *ide_base;
1312fff2751SRafal Prylowski 	struct ata_timing t;
1322fff2751SRafal Prylowski 	bool iordy;
1332fff2751SRafal Prylowski 
1342fff2751SRafal Prylowski 	unsigned long udma_in_phys;
1352fff2751SRafal Prylowski 	unsigned long udma_out_phys;
1362fff2751SRafal Prylowski 
1372fff2751SRafal Prylowski 	struct dma_chan *dma_rx_channel;
1382fff2751SRafal Prylowski 	struct dma_chan *dma_tx_channel;
1392fff2751SRafal Prylowski };
1402fff2751SRafal Prylowski 
ep93xx_pata_clear_regs(void __iomem * base)1412fff2751SRafal Prylowski static void ep93xx_pata_clear_regs(void __iomem *base)
1422fff2751SRafal Prylowski {
1432fff2751SRafal Prylowski 	writel(IDECTRL_CS0N | IDECTRL_CS1N | IDECTRL_DIORN |
1442fff2751SRafal Prylowski 		IDECTRL_DIOWN, base + IDECTRL);
1452fff2751SRafal Prylowski 
1462fff2751SRafal Prylowski 	writel(0, base + IDECFG);
1472fff2751SRafal Prylowski 	writel(0, base + IDEMDMAOP);
1482fff2751SRafal Prylowski 	writel(0, base + IDEUDMAOP);
1492fff2751SRafal Prylowski 	writel(0, base + IDEDATAOUT);
1502fff2751SRafal Prylowski 	writel(0, base + IDEDATAIN);
1512fff2751SRafal Prylowski 	writel(0, base + IDEMDMADATAOUT);
1522fff2751SRafal Prylowski 	writel(0, base + IDEMDMADATAIN);
1532fff2751SRafal Prylowski 	writel(0, base + IDEUDMADATAOUT);
1542fff2751SRafal Prylowski 	writel(0, base + IDEUDMADATAIN);
1552fff2751SRafal Prylowski 	writel(0, base + IDEUDMADEBUG);
1562fff2751SRafal Prylowski }
1572fff2751SRafal Prylowski 
ep93xx_pata_check_iordy(void __iomem * base)1582fff2751SRafal Prylowski static bool ep93xx_pata_check_iordy(void __iomem *base)
1592fff2751SRafal Prylowski {
1602fff2751SRafal Prylowski 	return !!(readl(base + IDECTRL) & IDECTRL_IORDY);
1612fff2751SRafal Prylowski }
1622fff2751SRafal Prylowski 
1632fff2751SRafal Prylowski /*
1642fff2751SRafal Prylowski  * According to EP93xx User's Guide, WST field of IDECFG specifies number
1652fff2751SRafal Prylowski  * of HCLK cycles to hold the data bus after a PIO write operation.
1662fff2751SRafal Prylowski  * It should be programmed to guarantee following delays:
1672fff2751SRafal Prylowski  *
1682fff2751SRafal Prylowski  * PIO Mode   [ns]
1692fff2751SRafal Prylowski  * 0          30
1702fff2751SRafal Prylowski  * 1          20
1712fff2751SRafal Prylowski  * 2          15
1722fff2751SRafal Prylowski  * 3          10
1732fff2751SRafal Prylowski  * 4          5
1742fff2751SRafal Prylowski  *
1752fff2751SRafal Prylowski  * Maximum possible value for HCLK is 100MHz.
1762fff2751SRafal Prylowski  */
ep93xx_pata_get_wst(int pio_mode)1772fff2751SRafal Prylowski static int ep93xx_pata_get_wst(int pio_mode)
1782fff2751SRafal Prylowski {
1792fff2751SRafal Prylowski 	int val;
1802fff2751SRafal Prylowski 
1812fff2751SRafal Prylowski 	if (pio_mode == 0)
1822fff2751SRafal Prylowski 		val = 3;
1832fff2751SRafal Prylowski 	else if (pio_mode < 3)
1842fff2751SRafal Prylowski 		val = 2;
1852fff2751SRafal Prylowski 	else
1862fff2751SRafal Prylowski 		val = 1;
1872fff2751SRafal Prylowski 
1882fff2751SRafal Prylowski 	return val << IDECFG_WST_SHIFT;
1892fff2751SRafal Prylowski }
1902fff2751SRafal Prylowski 
ep93xx_pata_enable_pio(void __iomem * base,int pio_mode)1912fff2751SRafal Prylowski static void ep93xx_pata_enable_pio(void __iomem *base, int pio_mode)
1922fff2751SRafal Prylowski {
1932fff2751SRafal Prylowski 	writel(IDECFG_IDEEN | IDECFG_PIO |
1942fff2751SRafal Prylowski 		ep93xx_pata_get_wst(pio_mode) |
1952fff2751SRafal Prylowski 		(pio_mode << IDECFG_MODE_SHIFT), base + IDECFG);
1962fff2751SRafal Prylowski }
1972fff2751SRafal Prylowski 
1982fff2751SRafal Prylowski /*
1992fff2751SRafal Prylowski  * Based on delay loop found in mach-pxa/mp900.c.
2002fff2751SRafal Prylowski  *
2012fff2751SRafal Prylowski  * Single iteration should take 5 cpu cycles. This is 25ns assuming the
2022fff2751SRafal Prylowski  * fastest ep93xx cpu speed (200MHz) and is better optimized for PIO4 timings
2032fff2751SRafal Prylowski  * than eg. 20ns.
2042fff2751SRafal Prylowski  */
ep93xx_pata_delay(unsigned long count)2052fff2751SRafal Prylowski static void ep93xx_pata_delay(unsigned long count)
2062fff2751SRafal Prylowski {
2072fff2751SRafal Prylowski 	__asm__ volatile (
2082fff2751SRafal Prylowski 		"0:\n"
2092fff2751SRafal Prylowski 		"mov r0, r0\n"
2102fff2751SRafal Prylowski 		"subs %0, %1, #1\n"
2112fff2751SRafal Prylowski 		"bge 0b\n"
2122fff2751SRafal Prylowski 		: "=r" (count)
2132fff2751SRafal Prylowski 		: "0" (count)
2142fff2751SRafal Prylowski 	);
2152fff2751SRafal Prylowski }
2162fff2751SRafal Prylowski 
ep93xx_pata_wait_for_iordy(void __iomem * base,unsigned long t2)2172fff2751SRafal Prylowski static unsigned long ep93xx_pata_wait_for_iordy(void __iomem *base,
2182fff2751SRafal Prylowski 						unsigned long t2)
2192fff2751SRafal Prylowski {
2202fff2751SRafal Prylowski 	/*
2212fff2751SRafal Prylowski 	 * According to ATA specification, IORDY pin can be first sampled
2222fff2751SRafal Prylowski 	 * tA = 35ns after activation of DIOR-/DIOW-. Maximum IORDY pulse
2232fff2751SRafal Prylowski 	 * width is tB = 1250ns.
2242fff2751SRafal Prylowski 	 *
2252fff2751SRafal Prylowski 	 * We are already t2 delay loop iterations after activation of
2262fff2751SRafal Prylowski 	 * DIOR-/DIOW-, so we set timeout to (1250 + 35) / 25 - t2 additional
2272fff2751SRafal Prylowski 	 * delay loop iterations.
2282fff2751SRafal Prylowski 	 */
2292fff2751SRafal Prylowski 	unsigned long start = (1250 + 35) / 25 - t2;
2302fff2751SRafal Prylowski 	unsigned long counter = start;
2312fff2751SRafal Prylowski 
2322fff2751SRafal Prylowski 	while (!ep93xx_pata_check_iordy(base) && counter--)
2332fff2751SRafal Prylowski 		ep93xx_pata_delay(1);
2342fff2751SRafal Prylowski 	return start - counter;
2352fff2751SRafal Prylowski }
2362fff2751SRafal Prylowski 
2372fff2751SRafal Prylowski /* common part at start of ep93xx_pata_read/write() */
ep93xx_pata_rw_begin(void __iomem * base,unsigned long addr,unsigned long t1)2382fff2751SRafal Prylowski static void ep93xx_pata_rw_begin(void __iomem *base, unsigned long addr,
2392fff2751SRafal Prylowski 				 unsigned long t1)
2402fff2751SRafal Prylowski {
2412fff2751SRafal Prylowski 	writel(IDECTRL_DIOWN | IDECTRL_DIORN | addr, base + IDECTRL);
2422fff2751SRafal Prylowski 	ep93xx_pata_delay(t1);
2432fff2751SRafal Prylowski }
2442fff2751SRafal Prylowski 
2452fff2751SRafal Prylowski /* common part at end of ep93xx_pata_read/write() */
ep93xx_pata_rw_end(void __iomem * base,unsigned long addr,bool iordy,unsigned long t0,unsigned long t2,unsigned long t2i)2462fff2751SRafal Prylowski static void ep93xx_pata_rw_end(void __iomem *base, unsigned long addr,
2472fff2751SRafal Prylowski 			       bool iordy, unsigned long t0, unsigned long t2,
2482fff2751SRafal Prylowski 			       unsigned long t2i)
2492fff2751SRafal Prylowski {
2502fff2751SRafal Prylowski 	ep93xx_pata_delay(t2);
2512fff2751SRafal Prylowski 	/* lengthen t2 if needed */
2522fff2751SRafal Prylowski 	if (iordy)
2532fff2751SRafal Prylowski 		t2 += ep93xx_pata_wait_for_iordy(base, t2);
2542fff2751SRafal Prylowski 	writel(IDECTRL_DIOWN | IDECTRL_DIORN | addr, base + IDECTRL);
2552fff2751SRafal Prylowski 	if (t0 > t2 && t0 - t2 > t2i)
2562fff2751SRafal Prylowski 		ep93xx_pata_delay(t0 - t2);
2572fff2751SRafal Prylowski 	else
2582fff2751SRafal Prylowski 		ep93xx_pata_delay(t2i);
2592fff2751SRafal Prylowski }
2602fff2751SRafal Prylowski 
ep93xx_pata_read(struct ep93xx_pata_data * drv_data,unsigned long addr,bool reg)2612fff2751SRafal Prylowski static u16 ep93xx_pata_read(struct ep93xx_pata_data *drv_data,
2622fff2751SRafal Prylowski 			    unsigned long addr,
2632fff2751SRafal Prylowski 			    bool reg)
2642fff2751SRafal Prylowski {
2652fff2751SRafal Prylowski 	void __iomem *base = drv_data->ide_base;
2662fff2751SRafal Prylowski 	const struct ata_timing *t = &drv_data->t;
2672fff2751SRafal Prylowski 	unsigned long t0 = reg ? t->cyc8b : t->cycle;
2682fff2751SRafal Prylowski 	unsigned long t2 = reg ? t->act8b : t->active;
2692fff2751SRafal Prylowski 	unsigned long t2i = reg ? t->rec8b : t->recover;
2702fff2751SRafal Prylowski 
2712fff2751SRafal Prylowski 	ep93xx_pata_rw_begin(base, addr, t->setup);
2722fff2751SRafal Prylowski 	writel(IDECTRL_DIOWN | addr, base + IDECTRL);
2732fff2751SRafal Prylowski 	/*
2742fff2751SRafal Prylowski 	 * The IDEDATAIN register is loaded from the DD pins at the positive
2752fff2751SRafal Prylowski 	 * edge of the DIORN signal. (EP93xx UG p27-14)
2762fff2751SRafal Prylowski 	 */
2772fff2751SRafal Prylowski 	ep93xx_pata_rw_end(base, addr, drv_data->iordy, t0, t2, t2i);
2782fff2751SRafal Prylowski 	return readl(base + IDEDATAIN);
2792fff2751SRafal Prylowski }
2802fff2751SRafal Prylowski 
2812fff2751SRafal Prylowski /* IDE register read */
ep93xx_pata_read_reg(struct ep93xx_pata_data * drv_data,unsigned long addr)2822fff2751SRafal Prylowski static u16 ep93xx_pata_read_reg(struct ep93xx_pata_data *drv_data,
2832fff2751SRafal Prylowski 				unsigned long addr)
2842fff2751SRafal Prylowski {
2852fff2751SRafal Prylowski 	return ep93xx_pata_read(drv_data, addr, true);
2862fff2751SRafal Prylowski }
2872fff2751SRafal Prylowski 
2882fff2751SRafal Prylowski /* PIO data read */
ep93xx_pata_read_data(struct ep93xx_pata_data * drv_data,unsigned long addr)2892fff2751SRafal Prylowski static u16 ep93xx_pata_read_data(struct ep93xx_pata_data *drv_data,
2902fff2751SRafal Prylowski 				 unsigned long addr)
2912fff2751SRafal Prylowski {
2922fff2751SRafal Prylowski 	return ep93xx_pata_read(drv_data, addr, false);
2932fff2751SRafal Prylowski }
2942fff2751SRafal Prylowski 
ep93xx_pata_write(struct ep93xx_pata_data * drv_data,u16 value,unsigned long addr,bool reg)2952fff2751SRafal Prylowski static void ep93xx_pata_write(struct ep93xx_pata_data *drv_data,
2962fff2751SRafal Prylowski 			      u16 value, unsigned long addr,
2972fff2751SRafal Prylowski 			      bool reg)
2982fff2751SRafal Prylowski {
2992fff2751SRafal Prylowski 	void __iomem *base = drv_data->ide_base;
3002fff2751SRafal Prylowski 	const struct ata_timing *t = &drv_data->t;
3012fff2751SRafal Prylowski 	unsigned long t0 = reg ? t->cyc8b : t->cycle;
3022fff2751SRafal Prylowski 	unsigned long t2 = reg ? t->act8b : t->active;
3032fff2751SRafal Prylowski 	unsigned long t2i = reg ? t->rec8b : t->recover;
3042fff2751SRafal Prylowski 
3052fff2751SRafal Prylowski 	ep93xx_pata_rw_begin(base, addr, t->setup);
3062fff2751SRafal Prylowski 	/*
3072fff2751SRafal Prylowski 	 * Value from IDEDATAOUT register is driven onto the DD pins when
3082fff2751SRafal Prylowski 	 * DIOWN is low. (EP93xx UG p27-13)
3092fff2751SRafal Prylowski 	 */
3102fff2751SRafal Prylowski 	writel(value, base + IDEDATAOUT);
3112fff2751SRafal Prylowski 	writel(IDECTRL_DIORN | addr, base + IDECTRL);
3122fff2751SRafal Prylowski 	ep93xx_pata_rw_end(base, addr, drv_data->iordy, t0, t2, t2i);
3132fff2751SRafal Prylowski }
3142fff2751SRafal Prylowski 
3152fff2751SRafal Prylowski /* IDE register write */
ep93xx_pata_write_reg(struct ep93xx_pata_data * drv_data,u16 value,unsigned long addr)3162fff2751SRafal Prylowski static void ep93xx_pata_write_reg(struct ep93xx_pata_data *drv_data,
3172fff2751SRafal Prylowski 				  u16 value, unsigned long addr)
3182fff2751SRafal Prylowski {
3192fff2751SRafal Prylowski 	ep93xx_pata_write(drv_data, value, addr, true);
3202fff2751SRafal Prylowski }
3212fff2751SRafal Prylowski 
3222fff2751SRafal Prylowski /* PIO data write */
ep93xx_pata_write_data(struct ep93xx_pata_data * drv_data,u16 value,unsigned long addr)3232fff2751SRafal Prylowski static void ep93xx_pata_write_data(struct ep93xx_pata_data *drv_data,
3242fff2751SRafal Prylowski 				   u16 value, unsigned long addr)
3252fff2751SRafal Prylowski {
3262fff2751SRafal Prylowski 	ep93xx_pata_write(drv_data, value, addr, false);
3272fff2751SRafal Prylowski }
3282fff2751SRafal Prylowski 
ep93xx_pata_set_piomode(struct ata_port * ap,struct ata_device * adev)3292fff2751SRafal Prylowski static void ep93xx_pata_set_piomode(struct ata_port *ap,
3302fff2751SRafal Prylowski 				    struct ata_device *adev)
3312fff2751SRafal Prylowski {
3322fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
3332fff2751SRafal Prylowski 	struct ata_device *pair = ata_dev_pair(adev);
3342fff2751SRafal Prylowski 	/*
3352fff2751SRafal Prylowski 	 * Calculate timings for the delay loop, assuming ep93xx cpu speed
3362fff2751SRafal Prylowski 	 * is 200MHz (maximum possible for ep93xx). If actual cpu speed is
3372fff2751SRafal Prylowski 	 * slower, we will wait a bit longer in each delay.
3382fff2751SRafal Prylowski 	 * Additional division of cpu speed by 5, because single iteration
3392fff2751SRafal Prylowski 	 * of our delay loop takes 5 cpu cycles (25ns).
3402fff2751SRafal Prylowski 	 */
3412fff2751SRafal Prylowski 	unsigned long T = 1000000 / (200 / 5);
3422fff2751SRafal Prylowski 
3432fff2751SRafal Prylowski 	ata_timing_compute(adev, adev->pio_mode, &drv_data->t, T, 0);
3442fff2751SRafal Prylowski 	if (pair && pair->pio_mode) {
3452fff2751SRafal Prylowski 		struct ata_timing t;
3462fff2751SRafal Prylowski 		ata_timing_compute(pair, pair->pio_mode, &t, T, 0);
3472fff2751SRafal Prylowski 		ata_timing_merge(&t, &drv_data->t, &drv_data->t,
3482fff2751SRafal Prylowski 			ATA_TIMING_SETUP | ATA_TIMING_8BIT);
3492fff2751SRafal Prylowski 	}
3502fff2751SRafal Prylowski 	drv_data->iordy = ata_pio_need_iordy(adev);
3512fff2751SRafal Prylowski 
3522fff2751SRafal Prylowski 	ep93xx_pata_enable_pio(drv_data->ide_base,
3532fff2751SRafal Prylowski 			       adev->pio_mode - XFER_PIO_0);
3542fff2751SRafal Prylowski }
3552fff2751SRafal Prylowski 
3562fff2751SRafal Prylowski /* Note: original code is ata_sff_check_status */
ep93xx_pata_check_status(struct ata_port * ap)3572fff2751SRafal Prylowski static u8 ep93xx_pata_check_status(struct ata_port *ap)
3582fff2751SRafal Prylowski {
3592fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
3602fff2751SRafal Prylowski 
3612fff2751SRafal Prylowski 	return ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_STATUS);
3622fff2751SRafal Prylowski }
3632fff2751SRafal Prylowski 
ep93xx_pata_check_altstatus(struct ata_port * ap)3642fff2751SRafal Prylowski static u8 ep93xx_pata_check_altstatus(struct ata_port *ap)
3652fff2751SRafal Prylowski {
3662fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
3672fff2751SRafal Prylowski 
3682fff2751SRafal Prylowski 	return ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_ALTSTATUS);
3692fff2751SRafal Prylowski }
3702fff2751SRafal Prylowski 
3712fff2751SRafal Prylowski /* Note: original code is ata_sff_tf_load */
ep93xx_pata_tf_load(struct ata_port * ap,const struct ata_taskfile * tf)3722fff2751SRafal Prylowski static void ep93xx_pata_tf_load(struct ata_port *ap,
3732fff2751SRafal Prylowski 				const struct ata_taskfile *tf)
3742fff2751SRafal Prylowski {
3752fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
3762fff2751SRafal Prylowski 	unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
3772fff2751SRafal Prylowski 
3782fff2751SRafal Prylowski 	if (tf->ctl != ap->last_ctl) {
3792fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->ctl, IDECTRL_ADDR_CTL);
3802fff2751SRafal Prylowski 		ap->last_ctl = tf->ctl;
3812fff2751SRafal Prylowski 		ata_wait_idle(ap);
3822fff2751SRafal Prylowski 	}
3832fff2751SRafal Prylowski 
3842fff2751SRafal Prylowski 	if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
3852fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->hob_feature,
3862fff2751SRafal Prylowski 			IDECTRL_ADDR_FEATURE);
3872fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->hob_nsect,
3882fff2751SRafal Prylowski 			IDECTRL_ADDR_NSECT);
3892fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->hob_lbal,
3902fff2751SRafal Prylowski 			IDECTRL_ADDR_LBAL);
3912fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->hob_lbam,
3922fff2751SRafal Prylowski 			IDECTRL_ADDR_LBAM);
3932fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->hob_lbah,
3942fff2751SRafal Prylowski 			IDECTRL_ADDR_LBAH);
3952fff2751SRafal Prylowski 	}
3962fff2751SRafal Prylowski 
3972fff2751SRafal Prylowski 	if (is_addr) {
3982fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->feature,
3992fff2751SRafal Prylowski 			IDECTRL_ADDR_FEATURE);
4002fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->nsect, IDECTRL_ADDR_NSECT);
4012fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->lbal, IDECTRL_ADDR_LBAL);
4022fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->lbam, IDECTRL_ADDR_LBAM);
4032fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->lbah, IDECTRL_ADDR_LBAH);
4042fff2751SRafal Prylowski 	}
4052fff2751SRafal Prylowski 
4062fff2751SRafal Prylowski 	if (tf->flags & ATA_TFLAG_DEVICE)
4072fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->device,
4082fff2751SRafal Prylowski 			IDECTRL_ADDR_DEVICE);
4092fff2751SRafal Prylowski 
4102fff2751SRafal Prylowski 	ata_wait_idle(ap);
4112fff2751SRafal Prylowski }
4122fff2751SRafal Prylowski 
4132fff2751SRafal Prylowski /* Note: original code is ata_sff_tf_read */
ep93xx_pata_tf_read(struct ata_port * ap,struct ata_taskfile * tf)4142fff2751SRafal Prylowski static void ep93xx_pata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
4152fff2751SRafal Prylowski {
4162fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
4172fff2751SRafal Prylowski 
4182fff2751SRafal Prylowski 	tf->status = ep93xx_pata_check_status(ap);
4192fff2751SRafal Prylowski 	tf->error = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_FEATURE);
420efcef265SSergey Shtylyov 	tf->nsect = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_NSECT);
421efcef265SSergey Shtylyov 	tf->lbal = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_LBAL);
4222fff2751SRafal Prylowski 	tf->lbam = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_LBAM);
4232fff2751SRafal Prylowski 	tf->lbah = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_LBAH);
4242fff2751SRafal Prylowski 	tf->device = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_DEVICE);
4252fff2751SRafal Prylowski 
4262fff2751SRafal Prylowski 	if (tf->flags & ATA_TFLAG_LBA48) {
4272fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->ctl | ATA_HOB,
4282fff2751SRafal Prylowski 			IDECTRL_ADDR_CTL);
4292fff2751SRafal Prylowski 		tf->hob_feature = ep93xx_pata_read_reg(drv_data,
4302fff2751SRafal Prylowski 			IDECTRL_ADDR_FEATURE);
4312fff2751SRafal Prylowski 		tf->hob_nsect = ep93xx_pata_read_reg(drv_data,
4322fff2751SRafal Prylowski 			IDECTRL_ADDR_NSECT);
4332fff2751SRafal Prylowski 		tf->hob_lbal = ep93xx_pata_read_reg(drv_data,
4342fff2751SRafal Prylowski 			IDECTRL_ADDR_LBAL);
4352fff2751SRafal Prylowski 		tf->hob_lbam = ep93xx_pata_read_reg(drv_data,
4362fff2751SRafal Prylowski 			IDECTRL_ADDR_LBAM);
4372fff2751SRafal Prylowski 		tf->hob_lbah = ep93xx_pata_read_reg(drv_data,
4382fff2751SRafal Prylowski 			IDECTRL_ADDR_LBAH);
4392fff2751SRafal Prylowski 		ep93xx_pata_write_reg(drv_data, tf->ctl, IDECTRL_ADDR_CTL);
4402fff2751SRafal Prylowski 		ap->last_ctl = tf->ctl;
4412fff2751SRafal Prylowski 	}
4422fff2751SRafal Prylowski }
4432fff2751SRafal Prylowski 
4442fff2751SRafal Prylowski /* Note: original code is ata_sff_exec_command */
ep93xx_pata_exec_command(struct ata_port * ap,const struct ata_taskfile * tf)4452fff2751SRafal Prylowski static void ep93xx_pata_exec_command(struct ata_port *ap,
4462fff2751SRafal Prylowski 				     const struct ata_taskfile *tf)
4472fff2751SRafal Prylowski {
4482fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
4492fff2751SRafal Prylowski 
4502fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, tf->command,
4512fff2751SRafal Prylowski 			  IDECTRL_ADDR_COMMAND);
4522fff2751SRafal Prylowski 	ata_sff_pause(ap);
4532fff2751SRafal Prylowski }
4542fff2751SRafal Prylowski 
4552fff2751SRafal Prylowski /* Note: original code is ata_sff_dev_select */
ep93xx_pata_dev_select(struct ata_port * ap,unsigned int device)4562fff2751SRafal Prylowski static void ep93xx_pata_dev_select(struct ata_port *ap, unsigned int device)
4572fff2751SRafal Prylowski {
4582fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
4592fff2751SRafal Prylowski 	u8 tmp = ATA_DEVICE_OBS;
4602fff2751SRafal Prylowski 
4612fff2751SRafal Prylowski 	if (device != 0)
4622fff2751SRafal Prylowski 		tmp |= ATA_DEV1;
4632fff2751SRafal Prylowski 
4642fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, tmp, IDECTRL_ADDR_DEVICE);
4652fff2751SRafal Prylowski 	ata_sff_pause(ap);	/* needed; also flushes, for mmio */
4662fff2751SRafal Prylowski }
4672fff2751SRafal Prylowski 
4682fff2751SRafal Prylowski /* Note: original code is ata_sff_set_devctl */
ep93xx_pata_set_devctl(struct ata_port * ap,u8 ctl)4692fff2751SRafal Prylowski static void ep93xx_pata_set_devctl(struct ata_port *ap, u8 ctl)
4702fff2751SRafal Prylowski {
4712fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
4722fff2751SRafal Prylowski 
4732fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, ctl, IDECTRL_ADDR_CTL);
4742fff2751SRafal Prylowski }
4752fff2751SRafal Prylowski 
4762fff2751SRafal Prylowski /* Note: original code is ata_sff_data_xfer */
ep93xx_pata_data_xfer(struct ata_queued_cmd * qc,unsigned char * buf,unsigned int buflen,int rw)4772fff2751SRafal Prylowski static unsigned int ep93xx_pata_data_xfer(struct ata_queued_cmd *qc,
4782fff2751SRafal Prylowski 					  unsigned char *buf,
479989e0aacSBartlomiej Zolnierkiewicz 					  unsigned int buflen, int rw)
4802fff2751SRafal Prylowski {
4812fff2751SRafal Prylowski 	struct ata_port *ap = qc->dev->link->ap;
4822fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
483989e0aacSBartlomiej Zolnierkiewicz 	u16 *data = (u16 *)buf;
4842fff2751SRafal Prylowski 	unsigned int words = buflen >> 1;
4852fff2751SRafal Prylowski 
4862fff2751SRafal Prylowski 	/* Transfer multiple of 2 bytes */
4872fff2751SRafal Prylowski 	while (words--)
4882fff2751SRafal Prylowski 		if (rw == READ)
4892fff2751SRafal Prylowski 			*data++ = cpu_to_le16(
4902fff2751SRafal Prylowski 				ep93xx_pata_read_data(
4912fff2751SRafal Prylowski 					drv_data, IDECTRL_ADDR_DATA));
4922fff2751SRafal Prylowski 		else
4932fff2751SRafal Prylowski 			ep93xx_pata_write_data(drv_data, le16_to_cpu(*data++),
4942fff2751SRafal Prylowski 				IDECTRL_ADDR_DATA);
4952fff2751SRafal Prylowski 
4962fff2751SRafal Prylowski 	/* Transfer trailing 1 byte, if any. */
4972fff2751SRafal Prylowski 	if (unlikely(buflen & 0x01)) {
4982fff2751SRafal Prylowski 		unsigned char pad[2] = { };
4992fff2751SRafal Prylowski 
5002fff2751SRafal Prylowski 		buf += buflen - 1;
5012fff2751SRafal Prylowski 
5022fff2751SRafal Prylowski 		if (rw == READ) {
5032fff2751SRafal Prylowski 			*pad = cpu_to_le16(
5042fff2751SRafal Prylowski 				ep93xx_pata_read_data(
5052fff2751SRafal Prylowski 					drv_data, IDECTRL_ADDR_DATA));
5062fff2751SRafal Prylowski 			*buf = pad[0];
5072fff2751SRafal Prylowski 		} else {
5082fff2751SRafal Prylowski 			pad[0] = *buf;
5092fff2751SRafal Prylowski 			ep93xx_pata_write_data(drv_data, le16_to_cpu(*pad),
5102fff2751SRafal Prylowski 					  IDECTRL_ADDR_DATA);
5112fff2751SRafal Prylowski 		}
5122fff2751SRafal Prylowski 		words++;
5132fff2751SRafal Prylowski 	}
5142fff2751SRafal Prylowski 
5152fff2751SRafal Prylowski 	return words << 1;
5162fff2751SRafal Prylowski }
5172fff2751SRafal Prylowski 
5182fff2751SRafal Prylowski /* Note: original code is ata_devchk */
ep93xx_pata_device_is_present(struct ata_port * ap,unsigned int device)5192fff2751SRafal Prylowski static bool ep93xx_pata_device_is_present(struct ata_port *ap,
5202fff2751SRafal Prylowski 					  unsigned int device)
5212fff2751SRafal Prylowski {
5222fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
5232fff2751SRafal Prylowski 	u8 nsect, lbal;
5242fff2751SRafal Prylowski 
5252fff2751SRafal Prylowski 	ap->ops->sff_dev_select(ap, device);
5262fff2751SRafal Prylowski 
5272fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, 0x55, IDECTRL_ADDR_NSECT);
5282fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, 0xaa, IDECTRL_ADDR_LBAL);
5292fff2751SRafal Prylowski 
5302fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, 0xaa, IDECTRL_ADDR_NSECT);
5312fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, 0x55, IDECTRL_ADDR_LBAL);
5322fff2751SRafal Prylowski 
5332fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, 0x55, IDECTRL_ADDR_NSECT);
5342fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, 0xaa, IDECTRL_ADDR_LBAL);
5352fff2751SRafal Prylowski 
5362fff2751SRafal Prylowski 	nsect = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_NSECT);
5372fff2751SRafal Prylowski 	lbal = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_LBAL);
5382fff2751SRafal Prylowski 
5392fff2751SRafal Prylowski 	if ((nsect == 0x55) && (lbal == 0xaa))
5402fff2751SRafal Prylowski 		return true;
5412fff2751SRafal Prylowski 
5422fff2751SRafal Prylowski 	return false;
5432fff2751SRafal Prylowski }
5442fff2751SRafal Prylowski 
5452fff2751SRafal Prylowski /* Note: original code is ata_sff_wait_after_reset */
ep93xx_pata_wait_after_reset(struct ata_link * link,unsigned int devmask,unsigned long deadline)5462fff2751SRafal Prylowski static int ep93xx_pata_wait_after_reset(struct ata_link *link,
5472fff2751SRafal Prylowski 					unsigned int devmask,
5482fff2751SRafal Prylowski 					unsigned long deadline)
5492fff2751SRafal Prylowski {
5502fff2751SRafal Prylowski 	struct ata_port *ap = link->ap;
5512fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
5522fff2751SRafal Prylowski 	unsigned int dev0 = devmask & (1 << 0);
5532fff2751SRafal Prylowski 	unsigned int dev1 = devmask & (1 << 1);
5542fff2751SRafal Prylowski 	int rc, ret = 0;
5552fff2751SRafal Prylowski 
5562fff2751SRafal Prylowski 	ata_msleep(ap, ATA_WAIT_AFTER_RESET);
5572fff2751SRafal Prylowski 
5582fff2751SRafal Prylowski 	/* always check readiness of the master device */
5592fff2751SRafal Prylowski 	rc = ata_sff_wait_ready(link, deadline);
5602fff2751SRafal Prylowski 	/*
5612fff2751SRafal Prylowski 	 * -ENODEV means the odd clown forgot the D7 pulldown resistor
5622fff2751SRafal Prylowski 	 * and TF status is 0xff, bail out on it too.
5632fff2751SRafal Prylowski 	 */
5642fff2751SRafal Prylowski 	if (rc)
5652fff2751SRafal Prylowski 		return rc;
5662fff2751SRafal Prylowski 
5672fff2751SRafal Prylowski 	/*
5682fff2751SRafal Prylowski 	 * if device 1 was found in ata_devchk, wait for register
5692fff2751SRafal Prylowski 	 * access briefly, then wait for BSY to clear.
5702fff2751SRafal Prylowski 	 */
5712fff2751SRafal Prylowski 	if (dev1) {
5722fff2751SRafal Prylowski 		int i;
5732fff2751SRafal Prylowski 
5742fff2751SRafal Prylowski 		ap->ops->sff_dev_select(ap, 1);
5752fff2751SRafal Prylowski 
5762fff2751SRafal Prylowski 		/*
5772fff2751SRafal Prylowski 		 * Wait for register access.  Some ATAPI devices fail
5782fff2751SRafal Prylowski 		 * to set nsect/lbal after reset, so don't waste too
5792fff2751SRafal Prylowski 		 * much time on it.  We're gonna wait for !BSY anyway.
5802fff2751SRafal Prylowski 		 */
5812fff2751SRafal Prylowski 		for (i = 0; i < 2; i++) {
5822fff2751SRafal Prylowski 			u8 nsect, lbal;
5832fff2751SRafal Prylowski 
5842fff2751SRafal Prylowski 			nsect = ep93xx_pata_read_reg(drv_data,
5852fff2751SRafal Prylowski 				IDECTRL_ADDR_NSECT);
5862fff2751SRafal Prylowski 			lbal = ep93xx_pata_read_reg(drv_data,
5872fff2751SRafal Prylowski 				IDECTRL_ADDR_LBAL);
5882fff2751SRafal Prylowski 			if (nsect == 1 && lbal == 1)
5892fff2751SRafal Prylowski 				break;
5902fff2751SRafal Prylowski 			msleep(50);	/* give drive a breather */
5912fff2751SRafal Prylowski 		}
5922fff2751SRafal Prylowski 
5932fff2751SRafal Prylowski 		rc = ata_sff_wait_ready(link, deadline);
5942fff2751SRafal Prylowski 		if (rc) {
5952fff2751SRafal Prylowski 			if (rc != -ENODEV)
5962fff2751SRafal Prylowski 				return rc;
5972fff2751SRafal Prylowski 			ret = rc;
5982fff2751SRafal Prylowski 		}
5992fff2751SRafal Prylowski 	}
6002fff2751SRafal Prylowski 	/* is all this really necessary? */
6012fff2751SRafal Prylowski 	ap->ops->sff_dev_select(ap, 0);
6022fff2751SRafal Prylowski 	if (dev1)
6032fff2751SRafal Prylowski 		ap->ops->sff_dev_select(ap, 1);
6042fff2751SRafal Prylowski 	if (dev0)
6052fff2751SRafal Prylowski 		ap->ops->sff_dev_select(ap, 0);
6062fff2751SRafal Prylowski 
6072fff2751SRafal Prylowski 	return ret;
6082fff2751SRafal Prylowski }
6092fff2751SRafal Prylowski 
6102fff2751SRafal Prylowski /* Note: original code is ata_bus_softreset */
ep93xx_pata_bus_softreset(struct ata_port * ap,unsigned int devmask,unsigned long deadline)6112fff2751SRafal Prylowski static int ep93xx_pata_bus_softreset(struct ata_port *ap, unsigned int devmask,
6122fff2751SRafal Prylowski 				     unsigned long deadline)
6132fff2751SRafal Prylowski {
6142fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
6152fff2751SRafal Prylowski 
6162fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, ap->ctl, IDECTRL_ADDR_CTL);
6172fff2751SRafal Prylowski 	udelay(20);		/* FIXME: flush */
6182fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, ap->ctl | ATA_SRST, IDECTRL_ADDR_CTL);
6192fff2751SRafal Prylowski 	udelay(20);		/* FIXME: flush */
6202fff2751SRafal Prylowski 	ep93xx_pata_write_reg(drv_data, ap->ctl, IDECTRL_ADDR_CTL);
6212fff2751SRafal Prylowski 	ap->last_ctl = ap->ctl;
6222fff2751SRafal Prylowski 
6232fff2751SRafal Prylowski 	return ep93xx_pata_wait_after_reset(&ap->link, devmask, deadline);
6242fff2751SRafal Prylowski }
6252fff2751SRafal Prylowski 
ep93xx_pata_release_dma(struct ep93xx_pata_data * drv_data)6262fff2751SRafal Prylowski static void ep93xx_pata_release_dma(struct ep93xx_pata_data *drv_data)
6272fff2751SRafal Prylowski {
6282fff2751SRafal Prylowski 	if (drv_data->dma_rx_channel) {
6292fff2751SRafal Prylowski 		dma_release_channel(drv_data->dma_rx_channel);
6302fff2751SRafal Prylowski 		drv_data->dma_rx_channel = NULL;
6312fff2751SRafal Prylowski 	}
6322fff2751SRafal Prylowski 	if (drv_data->dma_tx_channel) {
6332fff2751SRafal Prylowski 		dma_release_channel(drv_data->dma_tx_channel);
6342fff2751SRafal Prylowski 		drv_data->dma_tx_channel = NULL;
6352fff2751SRafal Prylowski 	}
6362fff2751SRafal Prylowski }
6372fff2751SRafal Prylowski 
ep93xx_pata_dma_init(struct ep93xx_pata_data * drv_data)6382fff2751SRafal Prylowski static int ep93xx_pata_dma_init(struct ep93xx_pata_data *drv_data)
6392fff2751SRafal Prylowski {
6402fff2751SRafal Prylowski 	struct platform_device *pdev = drv_data->pdev;
6412fff2751SRafal Prylowski 	struct device *dev = &pdev->dev;
6422fff2751SRafal Prylowski 	dma_cap_mask_t mask;
6432fff2751SRafal Prylowski 	struct dma_slave_config conf;
6442fff2751SRafal Prylowski 	int ret;
6452fff2751SRafal Prylowski 
6462fff2751SRafal Prylowski 	dma_cap_zero(mask);
6472fff2751SRafal Prylowski 	dma_cap_set(DMA_SLAVE, mask);
6482fff2751SRafal Prylowski 
6492fff2751SRafal Prylowski 	/*
6502fff2751SRafal Prylowski 	 * Request two channels for IDE. Another possibility would be
6512fff2751SRafal Prylowski 	 * to request only one channel, and reprogram it's direction at
6522fff2751SRafal Prylowski 	 * start of new transfer.
6532fff2751SRafal Prylowski 	 */
6542fff2751SRafal Prylowski 	drv_data->dma_rx_channel = dma_request_chan(dev, "rx");
6552fff2751SRafal Prylowski 	if (IS_ERR(drv_data->dma_rx_channel))
6562fff2751SRafal Prylowski 		return dev_err_probe(dev, PTR_ERR(drv_data->dma_rx_channel),
6572fff2751SRafal Prylowski 				     "rx DMA setup failed\n");
6582fff2751SRafal Prylowski 
6592fff2751SRafal Prylowski 	drv_data->dma_tx_channel = dma_request_chan(&pdev->dev, "tx");
6602fff2751SRafal Prylowski 	if (IS_ERR(drv_data->dma_tx_channel)) {
6612fff2751SRafal Prylowski 		ret = dev_err_probe(dev, PTR_ERR(drv_data->dma_tx_channel),
6622fff2751SRafal Prylowski 				    "tx DMA setup failed\n");
6632fff2751SRafal Prylowski 		goto fail_release_rx;
6646adde4a3SNathan Chancellor 	}
6652fff2751SRafal Prylowski 
6662fff2751SRafal Prylowski 	/* Configure receive channel direction and source address */
6672fff2751SRafal Prylowski 	memset(&conf, 0, sizeof(conf));
6682fff2751SRafal Prylowski 	conf.direction = DMA_DEV_TO_MEM;
6692fff2751SRafal Prylowski 	conf.src_addr = drv_data->udma_in_phys;
6702fff2751SRafal Prylowski 	conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
6712fff2751SRafal Prylowski 	ret = dmaengine_slave_config(drv_data->dma_rx_channel, &conf);
6726adde4a3SNathan Chancellor 	if (ret) {
6732fff2751SRafal Prylowski 		dev_err_probe(dev, ret, "failed to configure rx dma channel");
6742fff2751SRafal Prylowski 		goto fail_release_dma;
6752fff2751SRafal Prylowski 	}
6762fff2751SRafal Prylowski 
6772fff2751SRafal Prylowski 	/* Configure transmit channel direction and destination address */
6782fff2751SRafal Prylowski 	memset(&conf, 0, sizeof(conf));
6792fff2751SRafal Prylowski 	conf.direction = DMA_MEM_TO_DEV;
6802fff2751SRafal Prylowski 	conf.dst_addr = drv_data->udma_out_phys;
6812fff2751SRafal Prylowski 	conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
6822fff2751SRafal Prylowski 	ret = dmaengine_slave_config(drv_data->dma_tx_channel, &conf);
6836adde4a3SNathan Chancellor 	if (ret) {
6842fff2751SRafal Prylowski 		dev_err_probe(dev, ret, "failed to configure tx dma channel");
6852fff2751SRafal Prylowski 		goto fail_release_dma;
6862fff2751SRafal Prylowski 	}
6872fff2751SRafal Prylowski 
6882fff2751SRafal Prylowski 	return 0;
6892fff2751SRafal Prylowski 
6902fff2751SRafal Prylowski fail_release_rx:
6912fff2751SRafal Prylowski 	dma_release_channel(drv_data->dma_rx_channel);
6922fff2751SRafal Prylowski fail_release_dma:
6932fff2751SRafal Prylowski 	ep93xx_pata_release_dma(drv_data);
6946adde4a3SNathan Chancellor 
6952fff2751SRafal Prylowski 	return ret;
6962fff2751SRafal Prylowski }
6972fff2751SRafal Prylowski 
ep93xx_pata_dma_start(struct ata_queued_cmd * qc)6982fff2751SRafal Prylowski static void ep93xx_pata_dma_start(struct ata_queued_cmd *qc)
6992fff2751SRafal Prylowski {
7002fff2751SRafal Prylowski 	struct dma_async_tx_descriptor *txd;
7012fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = qc->ap->host->private_data;
7022fff2751SRafal Prylowski 	void __iomem *base = drv_data->ide_base;
7032fff2751SRafal Prylowski 	struct ata_device *adev = qc->dev;
7042fff2751SRafal Prylowski 	u32 v = qc->dma_dir == DMA_TO_DEVICE ? IDEUDMAOP_RWOP : 0;
7052fff2751SRafal Prylowski 	struct dma_chan *channel = qc->dma_dir == DMA_TO_DEVICE
7062fff2751SRafal Prylowski 		? drv_data->dma_tx_channel : drv_data->dma_rx_channel;
7072fff2751SRafal Prylowski 
7082fff2751SRafal Prylowski 	txd = dmaengine_prep_slave_sg(channel, qc->sg, qc->n_elem, qc->dma_dir,
7092fff2751SRafal Prylowski 		DMA_CTRL_ACK);
7102fff2751SRafal Prylowski 	if (!txd) {
7112fff2751SRafal Prylowski 		dev_err(qc->ap->dev, "failed to prepare slave for sg dma\n");
7122fff2751SRafal Prylowski 		return;
71369493e0bSBarry Song 	}
71469493e0bSBarry Song 	txd->callback = NULL;
7152fff2751SRafal Prylowski 	txd->callback_param = NULL;
7162fff2751SRafal Prylowski 
7172fff2751SRafal Prylowski 	if (dmaengine_submit(txd) < 0) {
7182fff2751SRafal Prylowski 		dev_err(qc->ap->dev, "failed to submit dma transfer\n");
7192fff2751SRafal Prylowski 		return;
7202fff2751SRafal Prylowski 	}
7212fff2751SRafal Prylowski 	dma_async_issue_pending(channel);
7222fff2751SRafal Prylowski 
7232fff2751SRafal Prylowski 	/*
7242fff2751SRafal Prylowski 	 * When enabling UDMA operation, IDEUDMAOP register needs to be
7252fff2751SRafal Prylowski 	 * programmed in three step sequence:
7262fff2751SRafal Prylowski 	 * 1) set or clear the RWOP bit,
7272fff2751SRafal Prylowski 	 * 2) perform dummy read of the register,
7282fff2751SRafal Prylowski 	 * 3) set the UEN bit.
7292fff2751SRafal Prylowski 	 */
7302fff2751SRafal Prylowski 	writel(v, base + IDEUDMAOP);
7312fff2751SRafal Prylowski 	readl(base + IDEUDMAOP);
7322fff2751SRafal Prylowski 	writel(v | IDEUDMAOP_UEN, base + IDEUDMAOP);
7332fff2751SRafal Prylowski 
7342fff2751SRafal Prylowski 	writel(IDECFG_IDEEN | IDECFG_UDMA |
7352fff2751SRafal Prylowski 		((adev->xfer_mode - XFER_UDMA_0) << IDECFG_MODE_SHIFT),
7362fff2751SRafal Prylowski 		base + IDECFG);
7372fff2751SRafal Prylowski }
7382fff2751SRafal Prylowski 
ep93xx_pata_dma_stop(struct ata_queued_cmd * qc)7392fff2751SRafal Prylowski static void ep93xx_pata_dma_stop(struct ata_queued_cmd *qc)
7402fff2751SRafal Prylowski {
7412fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = qc->ap->host->private_data;
7422fff2751SRafal Prylowski 	void __iomem *base = drv_data->ide_base;
7432fff2751SRafal Prylowski 
7442fff2751SRafal Prylowski 	/* terminate all dma transfers, if not yet finished */
7452fff2751SRafal Prylowski 	dmaengine_terminate_all(drv_data->dma_rx_channel);
7462fff2751SRafal Prylowski 	dmaengine_terminate_all(drv_data->dma_tx_channel);
7472fff2751SRafal Prylowski 
7482fff2751SRafal Prylowski 	/*
7492fff2751SRafal Prylowski 	 * To properly stop IDE-DMA, IDEUDMAOP register must to be cleared
7502fff2751SRafal Prylowski 	 * and IDECTRL register must be set to default value.
7512fff2751SRafal Prylowski 	 */
7522fff2751SRafal Prylowski 	writel(0, base + IDEUDMAOP);
7532fff2751SRafal Prylowski 	writel(readl(base + IDECTRL) | IDECTRL_DIOWN | IDECTRL_DIORN |
7542fff2751SRafal Prylowski 		IDECTRL_CS0N | IDECTRL_CS1N, base + IDECTRL);
7552fff2751SRafal Prylowski 
7562fff2751SRafal Prylowski 	ep93xx_pata_enable_pio(drv_data->ide_base,
7572fff2751SRafal Prylowski 		qc->dev->pio_mode - XFER_PIO_0);
7582fff2751SRafal Prylowski 
7592fff2751SRafal Prylowski 	ata_sff_dma_pause(qc->ap);
7602fff2751SRafal Prylowski }
7612fff2751SRafal Prylowski 
ep93xx_pata_dma_setup(struct ata_queued_cmd * qc)7622fff2751SRafal Prylowski static void ep93xx_pata_dma_setup(struct ata_queued_cmd *qc)
7632fff2751SRafal Prylowski {
7642fff2751SRafal Prylowski 	qc->ap->ops->sff_exec_command(qc->ap, &qc->tf);
7652fff2751SRafal Prylowski }
7662fff2751SRafal Prylowski 
ep93xx_pata_dma_status(struct ata_port * ap)7672fff2751SRafal Prylowski static u8 ep93xx_pata_dma_status(struct ata_port *ap)
7682fff2751SRafal Prylowski {
7692fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
7702fff2751SRafal Prylowski 	u32 val = readl(drv_data->ide_base + IDEUDMASTS);
7712fff2751SRafal Prylowski 
7722fff2751SRafal Prylowski 	/*
7732fff2751SRafal Prylowski 	 * UDMA Status Register bits:
7742fff2751SRafal Prylowski 	 *
7752fff2751SRafal Prylowski 	 * DMAIDE - DMA request signal from UDMA state machine,
7762fff2751SRafal Prylowski 	 * INTIDE - INT line generated by UDMA because of errors in the
7772fff2751SRafal Prylowski 	 *          state machine,
7782fff2751SRafal Prylowski 	 * SBUSY - UDMA state machine busy, not in idle state,
7792fff2751SRafal Prylowski 	 * NDO   - error for data-out not completed,
7802fff2751SRafal Prylowski 	 * NDI   - error for data-in not completed,
7812fff2751SRafal Prylowski 	 * N4X   - error for data transferred not multiplies of four
7822fff2751SRafal Prylowski 	 *         32-bit words.
7832fff2751SRafal Prylowski 	 * (EP93xx UG p27-17)
7842fff2751SRafal Prylowski 	 */
7852fff2751SRafal Prylowski 	if (val & IDEUDMASTS_NDO || val & IDEUDMASTS_NDI ||
7862fff2751SRafal Prylowski 	    val & IDEUDMASTS_N4X || val & IDEUDMASTS_INTIDE)
7872fff2751SRafal Prylowski 		return ATA_DMA_ERR;
7882fff2751SRafal Prylowski 
7892fff2751SRafal Prylowski 	/* read INTRQ (INT[3]) pin input state */
7902fff2751SRafal Prylowski 	if (readl(drv_data->ide_base + IDECTRL) & IDECTRL_INTRQ)
7912fff2751SRafal Prylowski 		return ATA_DMA_INTR;
7922fff2751SRafal Prylowski 
7932fff2751SRafal Prylowski 	if (val & IDEUDMASTS_SBUSY || val & IDEUDMASTS_DMAIDE)
7942fff2751SRafal Prylowski 		return ATA_DMA_ACTIVE;
7952fff2751SRafal Prylowski 
7962fff2751SRafal Prylowski 	return 0;
7972fff2751SRafal Prylowski }
7982fff2751SRafal Prylowski 
7992fff2751SRafal Prylowski /* Note: original code is ata_sff_softreset */
ep93xx_pata_softreset(struct ata_link * al,unsigned int * classes,unsigned long deadline)8002fff2751SRafal Prylowski static int ep93xx_pata_softreset(struct ata_link *al, unsigned int *classes,
8012fff2751SRafal Prylowski 				 unsigned long deadline)
8022fff2751SRafal Prylowski {
8032fff2751SRafal Prylowski 	struct ata_port *ap = al->ap;
8042fff2751SRafal Prylowski 	unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
8052fff2751SRafal Prylowski 	unsigned int devmask = 0;
8062fff2751SRafal Prylowski 	int rc;
8072fff2751SRafal Prylowski 	u8 err;
8082fff2751SRafal Prylowski 
8092fff2751SRafal Prylowski 	/* determine if device 0/1 are present */
8102fff2751SRafal Prylowski 	if (ep93xx_pata_device_is_present(ap, 0))
8112fff2751SRafal Prylowski 		devmask |= (1 << 0);
8122fff2751SRafal Prylowski 	if (slave_possible && ep93xx_pata_device_is_present(ap, 1))
8132fff2751SRafal Prylowski 		devmask |= (1 << 1);
8142fff2751SRafal Prylowski 
8152fff2751SRafal Prylowski 	/* select device 0 again */
8162fff2751SRafal Prylowski 	ap->ops->sff_dev_select(al->ap, 0);
8172fff2751SRafal Prylowski 
8182fff2751SRafal Prylowski 	/* issue bus reset */
8192fff2751SRafal Prylowski 	rc = ep93xx_pata_bus_softreset(ap, devmask, deadline);
8202fff2751SRafal Prylowski 	/* if link is ocuppied, -ENODEV too is an error */
8212fff2751SRafal Prylowski 	if (rc && (rc != -ENODEV || sata_scr_valid(al))) {
8222fff2751SRafal Prylowski 		ata_link_err(al, "SRST failed (errno=%d)\n", rc);
8232fff2751SRafal Prylowski 		return rc;
8242fff2751SRafal Prylowski 	}
8252fff2751SRafal Prylowski 
8262fff2751SRafal Prylowski 	/* determine by signature whether we have ATA or ATAPI devices */
82799da09fcSWei Yongjun 	classes[0] = ata_sff_dev_classify(&al->device[0], devmask & (1 << 0),
8282fff2751SRafal Prylowski 					  &err);
8292fff2751SRafal Prylowski 	if (slave_possible && err != 0x81)
8302fff2751SRafal Prylowski 		classes[1] = ata_sff_dev_classify(&al->device[1],
8312fff2751SRafal Prylowski 						  devmask & (1 << 1), &err);
8322fff2751SRafal Prylowski 
8332fff2751SRafal Prylowski 	return 0;
8342fff2751SRafal Prylowski }
8352fff2751SRafal Prylowski 
8362fff2751SRafal Prylowski /* Note: original code is ata_sff_drain_fifo */
ep93xx_pata_drain_fifo(struct ata_queued_cmd * qc)8372fff2751SRafal Prylowski static void ep93xx_pata_drain_fifo(struct ata_queued_cmd *qc)
8382fff2751SRafal Prylowski {
8392fff2751SRafal Prylowski 	int count;
8402fff2751SRafal Prylowski 	struct ata_port *ap;
8412fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data;
8422fff2751SRafal Prylowski 
8432fff2751SRafal Prylowski 	/* We only need to flush incoming data when a command was running */
8442fff2751SRafal Prylowski 	if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
8452fff2751SRafal Prylowski 		return;
8462fff2751SRafal Prylowski 
8472fff2751SRafal Prylowski 	ap = qc->ap;
8482fff2751SRafal Prylowski 	drv_data = ap->host->private_data;
8492fff2751SRafal Prylowski 	/* Drain up to 64K of data before we give up this recovery method */
8502fff2751SRafal Prylowski 	for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
8512fff2751SRafal Prylowski 		     && count < 65536; count += 2)
8522fff2751SRafal Prylowski 		ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_DATA);
8532fff2751SRafal Prylowski 
8542fff2751SRafal Prylowski 	if (count)
8552fff2751SRafal Prylowski 		ata_port_dbg(ap, "drained %d bytes to clear DRQ.\n", count);
8562fff2751SRafal Prylowski 
8572fff2751SRafal Prylowski }
8582fff2751SRafal Prylowski 
ep93xx_pata_port_start(struct ata_port * ap)8592fff2751SRafal Prylowski static int ep93xx_pata_port_start(struct ata_port *ap)
86099da09fcSWei Yongjun {
8612fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = ap->host->private_data;
8622fff2751SRafal Prylowski 
8632fff2751SRafal Prylowski 	/*
8642fff2751SRafal Prylowski 	 * Set timings to safe values at startup (= number of ns from ATA
8652fff2751SRafal Prylowski 	 * specification), we'll switch to properly calculated values later.
8662fff2751SRafal Prylowski 	 */
8672fff2751SRafal Prylowski 	drv_data->t = *ata_timing_find_mode(XFER_PIO_0);
8682fff2751SRafal Prylowski 	return 0;
8692fff2751SRafal Prylowski }
8702fff2751SRafal Prylowski 
8712fff2751SRafal Prylowski static const struct scsi_host_template ep93xx_pata_sht = {
8722fff2751SRafal Prylowski 	ATA_BASE_SHT(DRV_NAME),
8732fff2751SRafal Prylowski 	/* ep93xx dma implementation limit */
8742fff2751SRafal Prylowski 	.sg_tablesize		= 32,
8752fff2751SRafal Prylowski 	/* ep93xx dma can't transfer 65536 bytes at once */
87625df73d9SBart Van Assche 	.dma_boundary		= 0x7fff,
8772fff2751SRafal Prylowski };
8782fff2751SRafal Prylowski 
8792fff2751SRafal Prylowski static struct ata_port_operations ep93xx_pata_port_ops = {
8802fff2751SRafal Prylowski 	.inherits		= &ata_bmdma_port_ops,
8812fff2751SRafal Prylowski 
8822fff2751SRafal Prylowski 	.softreset		= ep93xx_pata_softreset,
8832fff2751SRafal Prylowski 	.hardreset		= ATA_OP_NULL,
8842fff2751SRafal Prylowski 
8852fff2751SRafal Prylowski 	.sff_dev_select		= ep93xx_pata_dev_select,
8862fff2751SRafal Prylowski 	.sff_set_devctl		= ep93xx_pata_set_devctl,
8872fff2751SRafal Prylowski 	.sff_check_status	= ep93xx_pata_check_status,
8882fff2751SRafal Prylowski 	.sff_check_altstatus	= ep93xx_pata_check_altstatus,
8892fff2751SRafal Prylowski 	.sff_tf_load		= ep93xx_pata_tf_load,
8902fff2751SRafal Prylowski 	.sff_tf_read		= ep93xx_pata_tf_read,
8912fff2751SRafal Prylowski 	.sff_exec_command	= ep93xx_pata_exec_command,
8922fff2751SRafal Prylowski 	.sff_data_xfer		= ep93xx_pata_data_xfer,
8932fff2751SRafal Prylowski 	.sff_drain_fifo		= ep93xx_pata_drain_fifo,
8942fff2751SRafal Prylowski 	.sff_irq_clear		= ATA_OP_NULL,
8952fff2751SRafal Prylowski 
8962fff2751SRafal Prylowski 	.set_piomode		= ep93xx_pata_set_piomode,
8972fff2751SRafal Prylowski 
8982fff2751SRafal Prylowski 	.bmdma_setup		= ep93xx_pata_dma_setup,
8992fff2751SRafal Prylowski 	.bmdma_start		= ep93xx_pata_dma_start,
9002fff2751SRafal Prylowski 	.bmdma_stop		= ep93xx_pata_dma_stop,
9012fff2751SRafal Prylowski 	.bmdma_status		= ep93xx_pata_dma_status,
9022fff2751SRafal Prylowski 
9032fff2751SRafal Prylowski 	.cable_detect		= ata_cable_unknown,
9042fff2751SRafal Prylowski 	.port_start		= ep93xx_pata_port_start,
9052fff2751SRafal Prylowski };
9062fff2751SRafal Prylowski 
9072fff2751SRafal Prylowski static const struct soc_device_attribute ep93xx_soc_table[] = {
9082fff2751SRafal Prylowski 	{ .revision = "E1", .data = (void *)ATA_UDMA3 },
9092fff2751SRafal Prylowski 	{ .revision = "E2", .data = (void *)ATA_UDMA4 },
9102fff2751SRafal Prylowski 	{ /* sentinel */ }
9112fff2751SRafal Prylowski };
912*db15538bSNikita Shubin 
ep93xx_pata_probe(struct platform_device * pdev)913*db15538bSNikita Shubin static int ep93xx_pata_probe(struct platform_device *pdev)
914*db15538bSNikita Shubin {
915*db15538bSNikita Shubin 	struct ep93xx_pata_data *drv_data;
916*db15538bSNikita Shubin 	struct ata_host *host;
917*db15538bSNikita Shubin 	struct ata_port *ap;
9180ec24914SGreg Kroah-Hartman 	int irq;
9192fff2751SRafal Prylowski 	struct resource *mem_res;
9202fff2751SRafal Prylowski 	void __iomem *ide_base;
9212fff2751SRafal Prylowski 	int err;
9222fff2751SRafal Prylowski 
923a2a9e02bSAndrey Utkin 	/* INT[3] (IRQ_EP93XX_EXT3) line connected as pull down */
9242fff2751SRafal Prylowski 	irq = platform_get_irq(pdev, 0);
9252fff2751SRafal Prylowski 	if (irq < 0)
9262fff2751SRafal Prylowski 		return irq;
9272fff2751SRafal Prylowski 
9282fff2751SRafal Prylowski 	ide_base = devm_platform_get_and_ioremap_resource(pdev, 0, &mem_res);
9292fff2751SRafal Prylowski 	if (IS_ERR(ide_base))
9302fff2751SRafal Prylowski 		return PTR_ERR(ide_base);
9312fff2751SRafal Prylowski 
9322fff2751SRafal Prylowski 	drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
9332fff2751SRafal Prylowski 	if (!drv_data)
9342fff2751SRafal Prylowski 		return -ENOMEM;
9355c812126SSergey Shtylyov 
9362fff2751SRafal Prylowski 	drv_data->pdev = pdev;
9372fff2751SRafal Prylowski 	drv_data->ide_base = ide_base;
9382fff2751SRafal Prylowski 	drv_data->udma_in_phys = mem_res->start + IDEUDMADATAIN;
939aebf1e26SMinghao Chi 	drv_data->udma_out_phys = mem_res->start + IDEUDMADATAOUT;
94093316e26SThierry Reding 	err = ep93xx_pata_dma_init(drv_data);
94193316e26SThierry Reding 	if (err)
9422fff2751SRafal Prylowski 		return err;
9432fff2751SRafal Prylowski 
9442fff2751SRafal Prylowski 	/* allocate host */
9452fff2751SRafal Prylowski 	host = ata_host_alloc(&pdev->dev, 1);
9462fff2751SRafal Prylowski 	if (!host) {
94741203f93SNikita Shubin 		err = -ENOMEM;
9482fff2751SRafal Prylowski 		goto err_rel_dma;
9492fff2751SRafal Prylowski 	}
9502fff2751SRafal Prylowski 
9512fff2751SRafal Prylowski 	ep93xx_pata_clear_regs(ide_base);
9522fff2751SRafal Prylowski 
9532fff2751SRafal Prylowski 	host->private_data = drv_data;
9542fff2751SRafal Prylowski 
9552fff2751SRafal Prylowski 	ap = host->ports[0];
9562fff2751SRafal Prylowski 	ap->dev = &pdev->dev;
9572fff2751SRafal Prylowski 	ap->ops = &ep93xx_pata_port_ops;
9582fff2751SRafal Prylowski 	ap->flags |= ATA_FLAG_SLAVE_POSS;
9592fff2751SRafal Prylowski 	ap->pio_mask = ATA_PIO4;
96041203f93SNikita Shubin 
9612fff2751SRafal Prylowski 	/*
9622fff2751SRafal Prylowski 	 * Maximum UDMA modes:
9632fff2751SRafal Prylowski 	 * EP931x rev.E0 - UDMA2
9642fff2751SRafal Prylowski 	 * EP931x rev.E1 - UDMA3
9652fff2751SRafal Prylowski 	 * EP931x rev.E2 - UDMA4
9662fff2751SRafal Prylowski 	 *
9672fff2751SRafal Prylowski 	 * MWDMA support was removed from EP931x rev.E2,
9682fff2751SRafal Prylowski 	 * so this driver supports only UDMA modes.
9692fff2751SRafal Prylowski 	 */
9702fff2751SRafal Prylowski 	if (drv_data->dma_rx_channel && drv_data->dma_tx_channel) {
9712fff2751SRafal Prylowski 		const struct soc_device_attribute *match;
9722fff2751SRafal Prylowski 
9732fff2751SRafal Prylowski 		match = soc_device_match(ep93xx_soc_table);
9742fff2751SRafal Prylowski 		if (match)
9752fff2751SRafal Prylowski 			ap->udma_mask = (unsigned int) match->data;
9762fff2751SRafal Prylowski 		else
9772fff2751SRafal Prylowski 			ap->udma_mask = ATA_UDMA2;
9782fff2751SRafal Prylowski 	}
9792fff2751SRafal Prylowski 
9802fff2751SRafal Prylowski 	/* defaults, pio 0 */
9812fff2751SRafal Prylowski 	ep93xx_pata_enable_pio(ide_base, 0);
9822fff2751SRafal Prylowski 
9832fff2751SRafal Prylowski 	dev_info(&pdev->dev, "version " DRV_VERSION "\n");
984*db15538bSNikita Shubin 
9852fff2751SRafal Prylowski 	/* activate host */
986*db15538bSNikita Shubin 	err = ata_host_activate(host, irq, ata_bmdma_interrupt, 0,
987*db15538bSNikita Shubin 		&ep93xx_pata_sht);
988*db15538bSNikita Shubin 	if (err == 0)
9892fff2751SRafal Prylowski 		return 0;
9902fff2751SRafal Prylowski 
9912fff2751SRafal Prylowski err_rel_dma:
9922fff2751SRafal Prylowski 	ep93xx_pata_release_dma(drv_data);
9932fff2751SRafal Prylowski 	return err;
9942fff2751SRafal Prylowski }
9952fff2751SRafal Prylowski 
ep93xx_pata_remove(struct platform_device * pdev)9962fff2751SRafal Prylowski static void ep93xx_pata_remove(struct platform_device *pdev)
9972fff2751SRafal Prylowski {
9982fff2751SRafal Prylowski 	struct ata_host *host = platform_get_drvdata(pdev);
9992fff2751SRafal Prylowski 	struct ep93xx_pata_data *drv_data = host->private_data;
10002fff2751SRafal Prylowski 
10012fff2751SRafal Prylowski 	ata_host_detach(host);
10022fff2751SRafal Prylowski 	ep93xx_pata_release_dma(drv_data);
10032fff2751SRafal Prylowski 	ep93xx_pata_clear_regs(drv_data->ide_base);
10042fff2751SRafal Prylowski }
10052fff2751SRafal Prylowski 
10062fff2751SRafal Prylowski static const struct of_device_id ep93xx_pata_of_ids[] = {
10072fff2751SRafal Prylowski 	{ .compatible = "cirrus,ep9312-pata" },
10082fff2751SRafal Prylowski 	{ /* sentinel */ }
10092fff2751SRafal Prylowski };
10102fff2751SRafal Prylowski MODULE_DEVICE_TABLE(of, ep93xx_pata_of_ids);
10113e981d93SUwe Kleine-König 
10122fff2751SRafal Prylowski static struct platform_driver ep93xx_pata_platform_driver = {
10132fff2751SRafal Prylowski 	.driver = {
10142fff2751SRafal Prylowski 		.name = DRV_NAME,
10152fff2751SRafal Prylowski 		.of_match_table = ep93xx_pata_of_ids,
10162fff2751SRafal Prylowski 	},
10172fff2751SRafal Prylowski 	.probe = ep93xx_pata_probe,
10182fff2751SRafal Prylowski 	.remove_new = ep93xx_pata_remove,
10192fff2751SRafal Prylowski };
10202fff2751SRafal Prylowski 
10212fff2751SRafal Prylowski module_platform_driver(ep93xx_pata_platform_driver);
10222fff2751SRafal Prylowski 
10232fff2751SRafal Prylowski MODULE_AUTHOR("Alessandro Zummo, Lennert Buytenhek, Joao Ramos, "
10242fff2751SRafal Prylowski 		"Bartlomiej Zolnierkiewicz, Rafal Prylowski");
10252fff2751SRafal Prylowski MODULE_DESCRIPTION("low-level driver for cirrus ep93xx IDE controller");
10262fff2751SRafal Prylowski MODULE_LICENSE("GPL");
10273e981d93SUwe Kleine-König MODULE_VERSION(DRV_VERSION);
10282fff2751SRafal Prylowski MODULE_ALIAS("platform:pata_ep93xx");
10292fff2751SRafal Prylowski