xref: /linux/drivers/ata/pata_ftide010.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2be4e456eSLinus Walleij /*
3be4e456eSLinus Walleij  * Faraday Technology FTIDE010 driver
4be4e456eSLinus Walleij  * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
5be4e456eSLinus Walleij  *
6be4e456eSLinus Walleij  * Includes portions of the SL2312/SL3516/Gemini PATA driver
7be4e456eSLinus Walleij  * Copyright (C) 2003 StorLine, Inc <jason@storlink.com.tw>
8be4e456eSLinus Walleij  * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
9be4e456eSLinus Walleij  * Copyright (C) 2010 Frederic Pecourt <opengemini@free.fr>
10be4e456eSLinus Walleij  * Copyright (C) 2011 Tobias Waldvogel <tobias.waldvogel@gmail.com>
11be4e456eSLinus Walleij  */
12be4e456eSLinus Walleij 
13be4e456eSLinus Walleij #include <linux/platform_device.h>
14be4e456eSLinus Walleij #include <linux/module.h>
15be4e456eSLinus Walleij #include <linux/libata.h>
16be4e456eSLinus Walleij #include <linux/bitops.h>
1761e6ae71SRob Herring #include <linux/of.h>
18be4e456eSLinus Walleij #include <linux/clk.h>
19be4e456eSLinus Walleij #include "sata_gemini.h"
20be4e456eSLinus Walleij 
21be4e456eSLinus Walleij #define DRV_NAME "pata_ftide010"
22be4e456eSLinus Walleij 
23be4e456eSLinus Walleij /**
24be4e456eSLinus Walleij  * struct ftide010 - state container for the Faraday FTIDE010
25be4e456eSLinus Walleij  * @dev: pointer back to the device representing this controller
26be4e456eSLinus Walleij  * @base: remapped I/O space address
27be4e456eSLinus Walleij  * @pclk: peripheral clock for the IDE block
28be4e456eSLinus Walleij  * @host: pointer to the ATA host for this device
29be4e456eSLinus Walleij  * @master_cbl: master cable type
30be4e456eSLinus Walleij  * @slave_cbl: slave cable type
31be4e456eSLinus Walleij  * @sg: Gemini SATA bridge pointer, if running on the Gemini
32be4e456eSLinus Walleij  * @master_to_sata0: Gemini SATA bridge: the ATA master is connected
33be4e456eSLinus Walleij  * to the SATA0 bridge
34be4e456eSLinus Walleij  * @slave_to_sata0: Gemini SATA bridge: the ATA slave is connected
35be4e456eSLinus Walleij  * to the SATA0 bridge
36be4e456eSLinus Walleij  * @master_to_sata1: Gemini SATA bridge: the ATA master is connected
37be4e456eSLinus Walleij  * to the SATA1 bridge
38be4e456eSLinus Walleij  * @slave_to_sata1: Gemini SATA bridge: the ATA slave is connected
39be4e456eSLinus Walleij  * to the SATA1 bridge
40be4e456eSLinus Walleij  */
41be4e456eSLinus Walleij struct ftide010 {
42be4e456eSLinus Walleij 	struct device *dev;
43be4e456eSLinus Walleij 	void __iomem *base;
44be4e456eSLinus Walleij 	struct clk *pclk;
45be4e456eSLinus Walleij 	struct ata_host *host;
46be4e456eSLinus Walleij 	unsigned int master_cbl;
47be4e456eSLinus Walleij 	unsigned int slave_cbl;
48be4e456eSLinus Walleij 	/* Gemini-specific properties */
49be4e456eSLinus Walleij 	struct sata_gemini *sg;
50be4e456eSLinus Walleij 	bool master_to_sata0;
51be4e456eSLinus Walleij 	bool slave_to_sata0;
52be4e456eSLinus Walleij 	bool master_to_sata1;
53be4e456eSLinus Walleij 	bool slave_to_sata1;
54be4e456eSLinus Walleij };
55be4e456eSLinus Walleij 
56be4e456eSLinus Walleij #define FTIDE010_DMA_REG	0x00
57be4e456eSLinus Walleij #define FTIDE010_DMA_STATUS	0x02
58be4e456eSLinus Walleij #define FTIDE010_IDE_BMDTPR	0x04
59be4e456eSLinus Walleij #define FTIDE010_IDE_DEVICE_ID	0x08
60be4e456eSLinus Walleij #define FTIDE010_PIO_TIMING	0x10
61be4e456eSLinus Walleij #define FTIDE010_MWDMA_TIMING	0x11
62be4e456eSLinus Walleij #define FTIDE010_UDMA_TIMING0	0x12 /* Master */
63be4e456eSLinus Walleij #define FTIDE010_UDMA_TIMING1	0x13 /* Slave */
64be4e456eSLinus Walleij #define FTIDE010_CLK_MOD	0x14
65be4e456eSLinus Walleij /* These registers are mapped directly to the IDE registers */
66be4e456eSLinus Walleij #define FTIDE010_CMD_DATA	0x20
67be4e456eSLinus Walleij #define FTIDE010_ERROR_FEATURES	0x21
68be4e456eSLinus Walleij #define FTIDE010_NSECT		0x22
69be4e456eSLinus Walleij #define FTIDE010_LBAL		0x23
70be4e456eSLinus Walleij #define FTIDE010_LBAM		0x24
71be4e456eSLinus Walleij #define FTIDE010_LBAH		0x25
72be4e456eSLinus Walleij #define FTIDE010_DEVICE		0x26
73be4e456eSLinus Walleij #define FTIDE010_STATUS_COMMAND	0x27
74be4e456eSLinus Walleij #define FTIDE010_ALTSTAT_CTRL	0x36
75be4e456eSLinus Walleij 
76be4e456eSLinus Walleij /* Set this bit for UDMA mode 5 and 6 */
77be4e456eSLinus Walleij #define FTIDE010_UDMA_TIMING_MODE_56	BIT(7)
78be4e456eSLinus Walleij 
79be4e456eSLinus Walleij /* 0 = 50 MHz, 1 = 66 MHz */
80be4e456eSLinus Walleij #define FTIDE010_CLK_MOD_DEV0_CLK_SEL	BIT(0)
81be4e456eSLinus Walleij #define FTIDE010_CLK_MOD_DEV1_CLK_SEL	BIT(1)
82be4e456eSLinus Walleij /* Enable UDMA on a device */
83be4e456eSLinus Walleij #define FTIDE010_CLK_MOD_DEV0_UDMA_EN	BIT(4)
84be4e456eSLinus Walleij #define FTIDE010_CLK_MOD_DEV1_UDMA_EN	BIT(5)
85be4e456eSLinus Walleij 
8625df73d9SBart Van Assche static const struct scsi_host_template pata_ftide010_sht = {
87be4e456eSLinus Walleij 	ATA_BMDMA_SHT(DRV_NAME),
88be4e456eSLinus Walleij };
89be4e456eSLinus Walleij 
90be4e456eSLinus Walleij /*
91be4e456eSLinus Walleij  * Bus timings
92be4e456eSLinus Walleij  *
93be4e456eSLinus Walleij  * The unit of the below required timings is two clock periods of the ATA
94be4e456eSLinus Walleij  * reference clock which is 30 nanoseconds per unit at 66MHz and 20
95be4e456eSLinus Walleij  * nanoseconds per unit at 50 MHz. The PIO timings assume 33MHz speed for
96be4e456eSLinus Walleij  * PIO.
97be4e456eSLinus Walleij  *
98be4e456eSLinus Walleij  * pio_active_time: array of 5 elements for T2 timing for Mode 0,
99be4e456eSLinus Walleij  * 1, 2, 3 and 4. Range 0..15.
100be4e456eSLinus Walleij  * pio_recovery_time: array of 5 elements for T2l timing for Mode 0,
101be4e456eSLinus Walleij  * 1, 2, 3 and 4. Range 0..15.
102be4e456eSLinus Walleij  * mdma_50_active_time: array of 4 elements for Td timing for multi
103be4e456eSLinus Walleij  * word DMA, Mode 0, 1, and 2 at 50 MHz. Range 0..15.
104be4e456eSLinus Walleij  * mdma_50_recovery_time: array of 4 elements for Tk timing for
105be4e456eSLinus Walleij  * multi word DMA, Mode 0, 1 and 2 at 50 MHz. Range 0..15.
106be4e456eSLinus Walleij  * mdma_66_active_time: array of 4 elements for Td timing for multi
107be4e456eSLinus Walleij  * word DMA, Mode 0, 1 and 2 at 66 MHz. Range 0..15.
108be4e456eSLinus Walleij  * mdma_66_recovery_time: array of 4 elements for Tk timing for
109be4e456eSLinus Walleij  * multi word DMA, Mode 0, 1 and 2 at 66 MHz. Range 0..15.
110be4e456eSLinus Walleij  * udma_50_setup_time: array of 4 elements for Tvds timing for ultra
111be4e456eSLinus Walleij  * DMA, Mode 0, 1, 2, 3, 4 and 5 at 50 MHz. Range 0..7.
112be4e456eSLinus Walleij  * udma_50_hold_time: array of 4 elements for Tdvh timing for
113be4e456eSLinus Walleij  * multi word DMA, Mode 0, 1, 2, 3, 4 and 5 at 50 MHz, Range 0..7.
114be4e456eSLinus Walleij  * udma_66_setup_time: array of 4 elements for Tvds timing for multi
115be4e456eSLinus Walleij  * word DMA, Mode 0, 1, 2, 3, 4, 5 and 6 at 66 MHz. Range 0..7.
116be4e456eSLinus Walleij  * udma_66_hold_time: array of 4 elements for Tdvh timing for
117be4e456eSLinus Walleij  * multi word DMA, Mode 0, 1, 2, 3, 4, 5 and 6 at 66 MHz. Range 0..7.
118be4e456eSLinus Walleij  */
119be4e456eSLinus Walleij static const u8 pio_active_time[5] = {10, 10, 10, 3, 3};
120be4e456eSLinus Walleij static const u8 pio_recovery_time[5] = {10, 3, 1, 3, 1};
121be4e456eSLinus Walleij static const u8 mwdma_50_active_time[3] = {6, 2, 2};
122be4e456eSLinus Walleij static const u8 mwdma_50_recovery_time[3] = {6, 2, 1};
123be4e456eSLinus Walleij static const u8 mwdma_66_active_time[3] = {8, 3, 3};
124be4e456eSLinus Walleij static const u8 mwdma_66_recovery_time[3] = {8, 2, 1};
125be4e456eSLinus Walleij static const u8 udma_50_setup_time[6] = {3, 3, 2, 2, 1, 1};
126be4e456eSLinus Walleij static const u8 udma_50_hold_time[6] = {3, 1, 1, 1, 1, 1};
127be4e456eSLinus Walleij static const u8 udma_66_setup_time[7] = {4, 4, 3, 2, };
128be4e456eSLinus Walleij static const u8 udma_66_hold_time[7] = {};
129be4e456eSLinus Walleij 
130be4e456eSLinus Walleij /*
131be4e456eSLinus Walleij  * We set 66 MHz for all MWDMA modes
132be4e456eSLinus Walleij  */
133be4e456eSLinus Walleij static const bool set_mdma_66_mhz[] = { true, true, true, true };
134be4e456eSLinus Walleij 
135be4e456eSLinus Walleij /*
136be4e456eSLinus Walleij  * We set 66 MHz for UDMA modes 3, 4 and 6 and no others
137be4e456eSLinus Walleij  */
138be4e456eSLinus Walleij static const bool set_udma_66_mhz[] = { false, false, false, true, true, false, true };
139be4e456eSLinus Walleij 
ftide010_set_dmamode(struct ata_port * ap,struct ata_device * adev)140be4e456eSLinus Walleij static void ftide010_set_dmamode(struct ata_port *ap, struct ata_device *adev)
141be4e456eSLinus Walleij {
142be4e456eSLinus Walleij 	struct ftide010 *ftide = ap->host->private_data;
143be4e456eSLinus Walleij 	u8 speed = adev->dma_mode;
144be4e456eSLinus Walleij 	u8 devno = adev->devno & 1;
145be4e456eSLinus Walleij 	u8 udma_en_mask;
146be4e456eSLinus Walleij 	u8 f66m_en_mask;
147be4e456eSLinus Walleij 	u8 clkreg;
148be4e456eSLinus Walleij 	u8 timreg;
149be4e456eSLinus Walleij 	u8 i;
150be4e456eSLinus Walleij 
151be4e456eSLinus Walleij 	/* Target device 0 (master) or 1 (slave) */
152be4e456eSLinus Walleij 	if (!devno) {
153be4e456eSLinus Walleij 		udma_en_mask = FTIDE010_CLK_MOD_DEV0_UDMA_EN;
154be4e456eSLinus Walleij 		f66m_en_mask = FTIDE010_CLK_MOD_DEV0_CLK_SEL;
155be4e456eSLinus Walleij 	} else {
156be4e456eSLinus Walleij 		udma_en_mask = FTIDE010_CLK_MOD_DEV1_UDMA_EN;
157be4e456eSLinus Walleij 		f66m_en_mask = FTIDE010_CLK_MOD_DEV1_CLK_SEL;
158be4e456eSLinus Walleij 	}
159be4e456eSLinus Walleij 
160be4e456eSLinus Walleij 	clkreg = readb(ftide->base + FTIDE010_CLK_MOD);
161be4e456eSLinus Walleij 	clkreg &= ~udma_en_mask;
162be4e456eSLinus Walleij 	clkreg &= ~f66m_en_mask;
163be4e456eSLinus Walleij 
164be4e456eSLinus Walleij 	if (speed & XFER_UDMA_0) {
165be4e456eSLinus Walleij 		i = speed & ~XFER_UDMA_0;
166be4e456eSLinus Walleij 		dev_dbg(ftide->dev, "set UDMA mode %02x, index %d\n",
167be4e456eSLinus Walleij 			speed, i);
168be4e456eSLinus Walleij 
169be4e456eSLinus Walleij 		clkreg |= udma_en_mask;
170be4e456eSLinus Walleij 		if (set_udma_66_mhz[i]) {
171be4e456eSLinus Walleij 			clkreg |= f66m_en_mask;
172be4e456eSLinus Walleij 			timreg = udma_66_setup_time[i] << 4 |
173be4e456eSLinus Walleij 				udma_66_hold_time[i];
174be4e456eSLinus Walleij 		} else {
175be4e456eSLinus Walleij 			timreg = udma_50_setup_time[i] << 4 |
176be4e456eSLinus Walleij 				udma_50_hold_time[i];
177be4e456eSLinus Walleij 		}
178be4e456eSLinus Walleij 
179be4e456eSLinus Walleij 		/* A special bit needs to be set for modes 5 and 6 */
180be4e456eSLinus Walleij 		if (i >= 5)
181be4e456eSLinus Walleij 			timreg |= FTIDE010_UDMA_TIMING_MODE_56;
182be4e456eSLinus Walleij 
183be4e456eSLinus Walleij 		dev_dbg(ftide->dev, "UDMA write clkreg = %02x, timreg = %02x\n",
184be4e456eSLinus Walleij 			clkreg, timreg);
185be4e456eSLinus Walleij 
186be4e456eSLinus Walleij 		writeb(clkreg, ftide->base + FTIDE010_CLK_MOD);
187be4e456eSLinus Walleij 		writeb(timreg, ftide->base + FTIDE010_UDMA_TIMING0 + devno);
188be4e456eSLinus Walleij 	} else {
189be4e456eSLinus Walleij 		i = speed & ~XFER_MW_DMA_0;
190be4e456eSLinus Walleij 		dev_dbg(ftide->dev, "set MWDMA mode %02x, index %d\n",
191be4e456eSLinus Walleij 			speed, i);
192be4e456eSLinus Walleij 
193be4e456eSLinus Walleij 		if (set_mdma_66_mhz[i]) {
194be4e456eSLinus Walleij 			clkreg |= f66m_en_mask;
195be4e456eSLinus Walleij 			timreg = mwdma_66_active_time[i] << 4 |
196be4e456eSLinus Walleij 				mwdma_66_recovery_time[i];
197be4e456eSLinus Walleij 		} else {
198be4e456eSLinus Walleij 			timreg = mwdma_50_active_time[i] << 4 |
199be4e456eSLinus Walleij 				mwdma_50_recovery_time[i];
200be4e456eSLinus Walleij 		}
201be4e456eSLinus Walleij 		dev_dbg(ftide->dev,
202be4e456eSLinus Walleij 			"MWDMA write clkreg = %02x, timreg = %02x\n",
203be4e456eSLinus Walleij 			clkreg, timreg);
204be4e456eSLinus Walleij 		/* This will affect all devices */
205be4e456eSLinus Walleij 		writeb(clkreg, ftide->base + FTIDE010_CLK_MOD);
206be4e456eSLinus Walleij 		writeb(timreg, ftide->base + FTIDE010_MWDMA_TIMING);
207be4e456eSLinus Walleij 	}
208be4e456eSLinus Walleij 
209be4e456eSLinus Walleij 	/*
210be4e456eSLinus Walleij 	 * Store the current device (master or slave) in ap->private_data
211be4e456eSLinus Walleij 	 * so that .qc_issue() can detect if this changes and reprogram
212be4e456eSLinus Walleij 	 * the DMA settings.
213be4e456eSLinus Walleij 	 */
214be4e456eSLinus Walleij 	ap->private_data = adev;
215be4e456eSLinus Walleij 
216be4e456eSLinus Walleij 	return;
217be4e456eSLinus Walleij }
218be4e456eSLinus Walleij 
ftide010_set_piomode(struct ata_port * ap,struct ata_device * adev)219be4e456eSLinus Walleij static void ftide010_set_piomode(struct ata_port *ap, struct ata_device *adev)
220be4e456eSLinus Walleij {
221be4e456eSLinus Walleij 	struct ftide010 *ftide = ap->host->private_data;
222be4e456eSLinus Walleij 	u8 pio = adev->pio_mode - XFER_PIO_0;
223be4e456eSLinus Walleij 
224be4e456eSLinus Walleij 	dev_dbg(ftide->dev, "set PIO mode %02x, index %d\n",
225be4e456eSLinus Walleij 		adev->pio_mode, pio);
226be4e456eSLinus Walleij 	writeb(pio_active_time[pio] << 4 | pio_recovery_time[pio],
227be4e456eSLinus Walleij 	       ftide->base + FTIDE010_PIO_TIMING);
228be4e456eSLinus Walleij }
229be4e456eSLinus Walleij 
230be4e456eSLinus Walleij /*
231be4e456eSLinus Walleij  * We implement our own qc_issue() callback since we may need to set up
232be4e456eSLinus Walleij  * the timings differently for master and slave transfers: the CLK_MOD_REG
233be4e456eSLinus Walleij  * and MWDMA_TIMING_REG is shared between master and slave, so reprogramming
234be4e456eSLinus Walleij  * this may be necessary.
235be4e456eSLinus Walleij  */
ftide010_qc_issue(struct ata_queued_cmd * qc)236be4e456eSLinus Walleij static unsigned int ftide010_qc_issue(struct ata_queued_cmd *qc)
237be4e456eSLinus Walleij {
238be4e456eSLinus Walleij 	struct ata_port *ap = qc->ap;
239be4e456eSLinus Walleij 	struct ata_device *adev = qc->dev;
240be4e456eSLinus Walleij 
241be4e456eSLinus Walleij 	/*
242be4e456eSLinus Walleij 	 * If the device changed, i.e. slave->master, master->slave,
243be4e456eSLinus Walleij 	 * then set up the DMA mode again so we are sure the timings
244be4e456eSLinus Walleij 	 * are correct.
245be4e456eSLinus Walleij 	 */
246be4e456eSLinus Walleij 	if (adev != ap->private_data && ata_dma_enabled(adev))
247be4e456eSLinus Walleij 		ftide010_set_dmamode(ap, adev);
248be4e456eSLinus Walleij 
249be4e456eSLinus Walleij 	return ata_bmdma_qc_issue(qc);
250be4e456eSLinus Walleij }
251be4e456eSLinus Walleij 
252be4e456eSLinus Walleij static struct ata_port_operations pata_ftide010_port_ops = {
253be4e456eSLinus Walleij 	.inherits	= &ata_bmdma_port_ops,
254be4e456eSLinus Walleij 	.set_dmamode	= ftide010_set_dmamode,
255be4e456eSLinus Walleij 	.set_piomode	= ftide010_set_piomode,
256be4e456eSLinus Walleij 	.qc_issue	= ftide010_qc_issue,
257be4e456eSLinus Walleij };
258be4e456eSLinus Walleij 
25946cb52adSLinus Walleij static struct ata_port_info ftide010_port_info = {
260be4e456eSLinus Walleij 	.flags		= ATA_FLAG_SLAVE_POSS,
261be4e456eSLinus Walleij 	.mwdma_mask	= ATA_MWDMA2,
262be4e456eSLinus Walleij 	.udma_mask	= ATA_UDMA6,
263be4e456eSLinus Walleij 	.pio_mask	= ATA_PIO4,
264be4e456eSLinus Walleij 	.port_ops	= &pata_ftide010_port_ops,
265be4e456eSLinus Walleij };
266be4e456eSLinus Walleij 
267be4e456eSLinus Walleij #if IS_ENABLED(CONFIG_SATA_GEMINI)
268be4e456eSLinus Walleij 
pata_ftide010_gemini_port_start(struct ata_port * ap)269be4e456eSLinus Walleij static int pata_ftide010_gemini_port_start(struct ata_port *ap)
270be4e456eSLinus Walleij {
271be4e456eSLinus Walleij 	struct ftide010 *ftide = ap->host->private_data;
272be4e456eSLinus Walleij 	struct device *dev = ftide->dev;
273be4e456eSLinus Walleij 	struct sata_gemini *sg = ftide->sg;
274be4e456eSLinus Walleij 	int bridges = 0;
275be4e456eSLinus Walleij 	int ret;
276be4e456eSLinus Walleij 
277be4e456eSLinus Walleij 	ret = ata_bmdma_port_start(ap);
278be4e456eSLinus Walleij 	if (ret)
279be4e456eSLinus Walleij 		return ret;
280be4e456eSLinus Walleij 
281be4e456eSLinus Walleij 	if (ftide->master_to_sata0) {
282be4e456eSLinus Walleij 		dev_info(dev, "SATA0 (master) start\n");
283be4e456eSLinus Walleij 		ret = gemini_sata_start_bridge(sg, 0);
284be4e456eSLinus Walleij 		if (!ret)
285be4e456eSLinus Walleij 			bridges++;
286be4e456eSLinus Walleij 	}
287be4e456eSLinus Walleij 	if (ftide->master_to_sata1) {
288be4e456eSLinus Walleij 		dev_info(dev, "SATA1 (master) start\n");
289be4e456eSLinus Walleij 		ret = gemini_sata_start_bridge(sg, 1);
290be4e456eSLinus Walleij 		if (!ret)
291be4e456eSLinus Walleij 			bridges++;
292be4e456eSLinus Walleij 	}
293be4e456eSLinus Walleij 	/* Avoid double-starting */
294be4e456eSLinus Walleij 	if (ftide->slave_to_sata0 && !ftide->master_to_sata0) {
295be4e456eSLinus Walleij 		dev_info(dev, "SATA0 (slave) start\n");
296be4e456eSLinus Walleij 		ret = gemini_sata_start_bridge(sg, 0);
297be4e456eSLinus Walleij 		if (!ret)
298be4e456eSLinus Walleij 			bridges++;
299be4e456eSLinus Walleij 	}
300be4e456eSLinus Walleij 	/* Avoid double-starting */
301be4e456eSLinus Walleij 	if (ftide->slave_to_sata1 && !ftide->master_to_sata1) {
302be4e456eSLinus Walleij 		dev_info(dev, "SATA1 (slave) start\n");
303be4e456eSLinus Walleij 		ret = gemini_sata_start_bridge(sg, 1);
304be4e456eSLinus Walleij 		if (!ret)
305be4e456eSLinus Walleij 			bridges++;
306be4e456eSLinus Walleij 	}
307be4e456eSLinus Walleij 
308be4e456eSLinus Walleij 	dev_info(dev, "brought %d bridges online\n", bridges);
309be4e456eSLinus Walleij 	return (bridges > 0) ? 0 : -EINVAL; // -ENODEV;
310be4e456eSLinus Walleij }
311be4e456eSLinus Walleij 
pata_ftide010_gemini_port_stop(struct ata_port * ap)312be4e456eSLinus Walleij static void pata_ftide010_gemini_port_stop(struct ata_port *ap)
313be4e456eSLinus Walleij {
314be4e456eSLinus Walleij 	struct ftide010 *ftide = ap->host->private_data;
315be4e456eSLinus Walleij 	struct device *dev = ftide->dev;
316be4e456eSLinus Walleij 	struct sata_gemini *sg = ftide->sg;
317be4e456eSLinus Walleij 
318be4e456eSLinus Walleij 	if (ftide->master_to_sata0) {
319be4e456eSLinus Walleij 		dev_info(dev, "SATA0 (master) stop\n");
320be4e456eSLinus Walleij 		gemini_sata_stop_bridge(sg, 0);
321be4e456eSLinus Walleij 	}
322be4e456eSLinus Walleij 	if (ftide->master_to_sata1) {
323be4e456eSLinus Walleij 		dev_info(dev, "SATA1 (master) stop\n");
324be4e456eSLinus Walleij 		gemini_sata_stop_bridge(sg, 1);
325be4e456eSLinus Walleij 	}
326be4e456eSLinus Walleij 	/* Avoid double-stopping */
327be4e456eSLinus Walleij 	if (ftide->slave_to_sata0 && !ftide->master_to_sata0) {
328be4e456eSLinus Walleij 		dev_info(dev, "SATA0 (slave) stop\n");
329be4e456eSLinus Walleij 		gemini_sata_stop_bridge(sg, 0);
330be4e456eSLinus Walleij 	}
331be4e456eSLinus Walleij 	/* Avoid double-stopping */
332be4e456eSLinus Walleij 	if (ftide->slave_to_sata1 && !ftide->master_to_sata1) {
333be4e456eSLinus Walleij 		dev_info(dev, "SATA1 (slave) stop\n");
334be4e456eSLinus Walleij 		gemini_sata_stop_bridge(sg, 1);
335be4e456eSLinus Walleij 	}
336be4e456eSLinus Walleij }
337be4e456eSLinus Walleij 
pata_ftide010_gemini_cable_detect(struct ata_port * ap)338be4e456eSLinus Walleij static int pata_ftide010_gemini_cable_detect(struct ata_port *ap)
339be4e456eSLinus Walleij {
340be4e456eSLinus Walleij 	struct ftide010 *ftide = ap->host->private_data;
341be4e456eSLinus Walleij 
342be4e456eSLinus Walleij 	/*
343be4e456eSLinus Walleij 	 * Return the master cable, I have no clue how to return a different
344be4e456eSLinus Walleij 	 * cable for the slave than for the master.
345be4e456eSLinus Walleij 	 */
346be4e456eSLinus Walleij 	return ftide->master_cbl;
347be4e456eSLinus Walleij }
348be4e456eSLinus Walleij 
pata_ftide010_gemini_init(struct ftide010 * ftide,struct ata_port_info * pi,bool is_ata1)349be4e456eSLinus Walleij static int pata_ftide010_gemini_init(struct ftide010 *ftide,
35046cb52adSLinus Walleij 				     struct ata_port_info *pi,
351be4e456eSLinus Walleij 				     bool is_ata1)
352be4e456eSLinus Walleij {
353be4e456eSLinus Walleij 	struct device *dev = ftide->dev;
354be4e456eSLinus Walleij 	struct sata_gemini *sg;
355be4e456eSLinus Walleij 	enum gemini_muxmode muxmode;
356be4e456eSLinus Walleij 
357be4e456eSLinus Walleij 	/* Look up SATA bridge */
358be4e456eSLinus Walleij 	sg = gemini_sata_bridge_get();
359be4e456eSLinus Walleij 	if (IS_ERR(sg))
360be4e456eSLinus Walleij 		return PTR_ERR(sg);
361be4e456eSLinus Walleij 	ftide->sg = sg;
362be4e456eSLinus Walleij 
363be4e456eSLinus Walleij 	muxmode = gemini_sata_get_muxmode(sg);
364be4e456eSLinus Walleij 
365be4e456eSLinus Walleij 	/* Special ops */
366be4e456eSLinus Walleij 	pata_ftide010_port_ops.port_start =
367be4e456eSLinus Walleij 		pata_ftide010_gemini_port_start;
368be4e456eSLinus Walleij 	pata_ftide010_port_ops.port_stop =
369be4e456eSLinus Walleij 		pata_ftide010_gemini_port_stop;
370be4e456eSLinus Walleij 	pata_ftide010_port_ops.cable_detect =
371be4e456eSLinus Walleij 		pata_ftide010_gemini_cable_detect;
372be4e456eSLinus Walleij 
373be4e456eSLinus Walleij 	/* Flag port as SATA-capable */
374be4e456eSLinus Walleij 	if (gemini_sata_bridge_enabled(sg, is_ata1))
37546cb52adSLinus Walleij 		pi->flags |= ATA_FLAG_SATA;
37646cb52adSLinus Walleij 
37746cb52adSLinus Walleij 	/* This device has broken DMA, only PIO works */
37846cb52adSLinus Walleij 	if (of_machine_is_compatible("itian,sq201")) {
37946cb52adSLinus Walleij 		pi->mwdma_mask = 0;
38046cb52adSLinus Walleij 		pi->udma_mask = 0;
38146cb52adSLinus Walleij 	}
382be4e456eSLinus Walleij 
383be4e456eSLinus Walleij 	/*
384be4e456eSLinus Walleij 	 * We assume that a simple 40-wire cable is used in the PATA mode.
385be4e456eSLinus Walleij 	 * if you're adding a system using the PATA interface, make sure
386be4e456eSLinus Walleij 	 * the right cable is set up here, it might be necessary to use
387be4e456eSLinus Walleij 	 * special hardware detection or encode the cable type in the device
388be4e456eSLinus Walleij 	 * tree with special properties.
389be4e456eSLinus Walleij 	 */
390be4e456eSLinus Walleij 	if (!is_ata1) {
391be4e456eSLinus Walleij 		switch (muxmode) {
392be4e456eSLinus Walleij 		case GEMINI_MUXMODE_0:
393be4e456eSLinus Walleij 			ftide->master_cbl = ATA_CBL_SATA;
394be4e456eSLinus Walleij 			ftide->slave_cbl = ATA_CBL_PATA40;
395be4e456eSLinus Walleij 			ftide->master_to_sata0 = true;
396be4e456eSLinus Walleij 			break;
397be4e456eSLinus Walleij 		case GEMINI_MUXMODE_1:
398be4e456eSLinus Walleij 			ftide->master_cbl = ATA_CBL_SATA;
399be4e456eSLinus Walleij 			ftide->slave_cbl = ATA_CBL_NONE;
400be4e456eSLinus Walleij 			ftide->master_to_sata0 = true;
401be4e456eSLinus Walleij 			break;
402be4e456eSLinus Walleij 		case GEMINI_MUXMODE_2:
403be4e456eSLinus Walleij 			ftide->master_cbl = ATA_CBL_PATA40;
404be4e456eSLinus Walleij 			ftide->slave_cbl = ATA_CBL_PATA40;
405be4e456eSLinus Walleij 			break;
406be4e456eSLinus Walleij 		case GEMINI_MUXMODE_3:
407be4e456eSLinus Walleij 			ftide->master_cbl = ATA_CBL_SATA;
408be4e456eSLinus Walleij 			ftide->slave_cbl = ATA_CBL_SATA;
409be4e456eSLinus Walleij 			ftide->master_to_sata0 = true;
410be4e456eSLinus Walleij 			ftide->slave_to_sata1 = true;
411be4e456eSLinus Walleij 			break;
412be4e456eSLinus Walleij 		}
413be4e456eSLinus Walleij 	} else {
414be4e456eSLinus Walleij 		switch (muxmode) {
415be4e456eSLinus Walleij 		case GEMINI_MUXMODE_0:
416be4e456eSLinus Walleij 			ftide->master_cbl = ATA_CBL_SATA;
417be4e456eSLinus Walleij 			ftide->slave_cbl = ATA_CBL_NONE;
418be4e456eSLinus Walleij 			ftide->master_to_sata1 = true;
419be4e456eSLinus Walleij 			break;
420be4e456eSLinus Walleij 		case GEMINI_MUXMODE_1:
421be4e456eSLinus Walleij 			ftide->master_cbl = ATA_CBL_SATA;
422be4e456eSLinus Walleij 			ftide->slave_cbl = ATA_CBL_PATA40;
423be4e456eSLinus Walleij 			ftide->master_to_sata1 = true;
424be4e456eSLinus Walleij 			break;
425be4e456eSLinus Walleij 		case GEMINI_MUXMODE_2:
426be4e456eSLinus Walleij 			ftide->master_cbl = ATA_CBL_SATA;
427be4e456eSLinus Walleij 			ftide->slave_cbl = ATA_CBL_SATA;
428be4e456eSLinus Walleij 			ftide->slave_to_sata0 = true;
429be4e456eSLinus Walleij 			ftide->master_to_sata1 = true;
430be4e456eSLinus Walleij 			break;
431be4e456eSLinus Walleij 		case GEMINI_MUXMODE_3:
432be4e456eSLinus Walleij 			ftide->master_cbl = ATA_CBL_PATA40;
433be4e456eSLinus Walleij 			ftide->slave_cbl = ATA_CBL_PATA40;
434be4e456eSLinus Walleij 			break;
435be4e456eSLinus Walleij 		}
436be4e456eSLinus Walleij 	}
437be4e456eSLinus Walleij 	dev_info(dev, "set up Gemini PATA%d\n", is_ata1);
438be4e456eSLinus Walleij 
439be4e456eSLinus Walleij 	return 0;
440be4e456eSLinus Walleij }
441be4e456eSLinus Walleij #else
pata_ftide010_gemini_init(struct ftide010 * ftide,struct ata_port_info * pi,bool is_ata1)442be4e456eSLinus Walleij static int pata_ftide010_gemini_init(struct ftide010 *ftide,
44346cb52adSLinus Walleij 				     struct ata_port_info *pi,
444be4e456eSLinus Walleij 				     bool is_ata1)
445be4e456eSLinus Walleij {
446be4e456eSLinus Walleij 	return -ENOTSUPP;
447be4e456eSLinus Walleij }
448be4e456eSLinus Walleij #endif
449be4e456eSLinus Walleij 
450be4e456eSLinus Walleij 
pata_ftide010_probe(struct platform_device * pdev)451be4e456eSLinus Walleij static int pata_ftide010_probe(struct platform_device *pdev)
452be4e456eSLinus Walleij {
453be4e456eSLinus Walleij 	struct device *dev = &pdev->dev;
454be4e456eSLinus Walleij 	struct device_node *np = dev->of_node;
45546cb52adSLinus Walleij 	struct ata_port_info pi = ftide010_port_info;
456be4e456eSLinus Walleij 	const struct ata_port_info *ppi[] = { &pi, NULL };
457be4e456eSLinus Walleij 	struct ftide010 *ftide;
458be4e456eSLinus Walleij 	struct resource *res;
459be4e456eSLinus Walleij 	int irq;
460be4e456eSLinus Walleij 	int ret;
461be4e456eSLinus Walleij 	int i;
462be4e456eSLinus Walleij 
463be4e456eSLinus Walleij 	ftide = devm_kzalloc(dev, sizeof(*ftide), GFP_KERNEL);
464be4e456eSLinus Walleij 	if (!ftide)
465be4e456eSLinus Walleij 		return -ENOMEM;
466be4e456eSLinus Walleij 	ftide->dev = dev;
467be4e456eSLinus Walleij 
468be4e456eSLinus Walleij 	irq = platform_get_irq(pdev, 0);
469be4e456eSLinus Walleij 	if (irq < 0)
470be4e456eSLinus Walleij 		return irq;
471be4e456eSLinus Walleij 
4722ade2891SYangtao Li 	ftide->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
473be4e456eSLinus Walleij 	if (IS_ERR(ftide->base))
474be4e456eSLinus Walleij 		return PTR_ERR(ftide->base);
475be4e456eSLinus Walleij 
476be4e456eSLinus Walleij 	ftide->pclk = devm_clk_get(dev, "PCLK");
477be4e456eSLinus Walleij 	if (!IS_ERR(ftide->pclk)) {
478be4e456eSLinus Walleij 		ret = clk_prepare_enable(ftide->pclk);
479be4e456eSLinus Walleij 		if (ret) {
480be4e456eSLinus Walleij 			dev_err(dev, "failed to enable PCLK\n");
481be4e456eSLinus Walleij 			return ret;
482be4e456eSLinus Walleij 		}
483be4e456eSLinus Walleij 	}
484be4e456eSLinus Walleij 
485be4e456eSLinus Walleij 	/* Some special Cortina Gemini init, if needed */
486be4e456eSLinus Walleij 	if (of_device_is_compatible(np, "cortina,gemini-pata")) {
487be4e456eSLinus Walleij 		/*
488be4e456eSLinus Walleij 		 * We need to know which instance is probing (the
489be4e456eSLinus Walleij 		 * Gemini has two instances of FTIDE010) and we do
490be4e456eSLinus Walleij 		 * this simply by looking at the physical base
491be4e456eSLinus Walleij 		 * address, which is 0x63400000 for ATA1, else we
492be4e456eSLinus Walleij 		 * are ATA0. This will also set up the cable types.
493be4e456eSLinus Walleij 		 */
494be4e456eSLinus Walleij 		ret = pata_ftide010_gemini_init(ftide,
49546cb52adSLinus Walleij 				&pi,
496be4e456eSLinus Walleij 				(res->start == 0x63400000));
497be4e456eSLinus Walleij 		if (ret)
498be4e456eSLinus Walleij 			goto err_dis_clk;
499be4e456eSLinus Walleij 	} else {
500be4e456eSLinus Walleij 		/* Else assume we are connected using PATA40 */
501be4e456eSLinus Walleij 		ftide->master_cbl = ATA_CBL_PATA40;
502be4e456eSLinus Walleij 		ftide->slave_cbl = ATA_CBL_PATA40;
503be4e456eSLinus Walleij 	}
504be4e456eSLinus Walleij 
505be4e456eSLinus Walleij 	ftide->host = ata_host_alloc_pinfo(dev, ppi, 1);
506be4e456eSLinus Walleij 	if (!ftide->host) {
507be4e456eSLinus Walleij 		ret = -ENOMEM;
508be4e456eSLinus Walleij 		goto err_dis_clk;
509be4e456eSLinus Walleij 	}
510be4e456eSLinus Walleij 	ftide->host->private_data = ftide;
511be4e456eSLinus Walleij 
512be4e456eSLinus Walleij 	for (i = 0; i < ftide->host->n_ports; i++) {
513be4e456eSLinus Walleij 		struct ata_port *ap = ftide->host->ports[i];
514be4e456eSLinus Walleij 		struct ata_ioports *ioaddr = &ap->ioaddr;
515be4e456eSLinus Walleij 
516be4e456eSLinus Walleij 		ioaddr->bmdma_addr = ftide->base + FTIDE010_DMA_REG;
517be4e456eSLinus Walleij 		ioaddr->cmd_addr = ftide->base + FTIDE010_CMD_DATA;
518be4e456eSLinus Walleij 		ioaddr->ctl_addr = ftide->base + FTIDE010_ALTSTAT_CTRL;
519be4e456eSLinus Walleij 		ioaddr->altstatus_addr = ftide->base + FTIDE010_ALTSTAT_CTRL;
520be4e456eSLinus Walleij 		ata_sff_std_ports(ioaddr);
521be4e456eSLinus Walleij 	}
522be4e456eSLinus Walleij 
523d0318fb3SArnd Bergmann 	dev_info(dev, "device ID %08x, irq %d, reg %pR\n",
524d0318fb3SArnd Bergmann 		 readl(ftide->base + FTIDE010_IDE_DEVICE_ID), irq, res);
525be4e456eSLinus Walleij 
526be4e456eSLinus Walleij 	ret = ata_host_activate(ftide->host, irq, ata_bmdma_interrupt,
527be4e456eSLinus Walleij 				0, &pata_ftide010_sht);
528be4e456eSLinus Walleij 	if (ret)
529be4e456eSLinus Walleij 		goto err_dis_clk;
530be4e456eSLinus Walleij 
531be4e456eSLinus Walleij 	return 0;
532be4e456eSLinus Walleij 
533be4e456eSLinus Walleij err_dis_clk:
534be4e456eSLinus Walleij 	clk_disable_unprepare(ftide->pclk);
53571abb4dfSWan Jiabing 
536be4e456eSLinus Walleij 	return ret;
537be4e456eSLinus Walleij }
538be4e456eSLinus Walleij 
pata_ftide010_remove(struct platform_device * pdev)53920102597SUwe Kleine-König static void pata_ftide010_remove(struct platform_device *pdev)
540be4e456eSLinus Walleij {
541be4e456eSLinus Walleij 	struct ata_host *host = platform_get_drvdata(pdev);
542be4e456eSLinus Walleij 	struct ftide010 *ftide = host->private_data;
543be4e456eSLinus Walleij 
544be4e456eSLinus Walleij 	ata_host_detach(ftide->host);
545be4e456eSLinus Walleij 	clk_disable_unprepare(ftide->pclk);
546be4e456eSLinus Walleij }
547be4e456eSLinus Walleij 
548be4e456eSLinus Walleij static const struct of_device_id pata_ftide010_of_match[] = {
5495e776d7bSGeert Uytterhoeven 	{ .compatible = "faraday,ftide010", },
5505e776d7bSGeert Uytterhoeven 	{ /* sentinel */ }
551be4e456eSLinus Walleij };
552*4b4e1a96SLiao Chen MODULE_DEVICE_TABLE(of, pata_ftide010_of_match);
553be4e456eSLinus Walleij 
554be4e456eSLinus Walleij static struct platform_driver pata_ftide010_driver = {
555be4e456eSLinus Walleij 	.driver = {
556be4e456eSLinus Walleij 		.name = DRV_NAME,
557dc62c7e6SDamien Le Moal 		.of_match_table = pata_ftide010_of_match,
558be4e456eSLinus Walleij 	},
559be4e456eSLinus Walleij 	.probe = pata_ftide010_probe,
56020102597SUwe Kleine-König 	.remove_new = pata_ftide010_remove,
561be4e456eSLinus Walleij };
562be4e456eSLinus Walleij module_platform_driver(pata_ftide010_driver);
563be4e456eSLinus Walleij 
5647274eef5SDamien Le Moal MODULE_DESCRIPTION("low level driver for Faraday Technology FTIDE010");
565be4e456eSLinus Walleij MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
566be4e456eSLinus Walleij MODULE_LICENSE("GPL");
567be4e456eSLinus Walleij MODULE_ALIAS("platform:" DRV_NAME);
568