Blame view

drivers/ata/pata_imx.c 7.41 KB
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * Freescale iMX PATA driver
   *
   * Copyright (C) 2011 Arnaud Patard <arnaud.patard@rtp-net.org>
   *
   * Based on pata_platform - Copyright (C) 2006 - 2007  Paul Mundt
   *
   * This file is subject to the terms and conditions of the GNU General Public
   * License.  See the file "COPYING" in the main directory of this archive
   * for more details.
   *
   * TODO:
   * - dmaengine support
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
14
   */
07b9733fd   Vladimir Zapolskiy   pata: imx: sort h...
15

e39c75cf3   Arnaud Patard   ata: Add iMX pata...
16
  #include <linux/ata.h>
07b9733fd   Vladimir Zapolskiy   pata: imx: sort h...
17
  #include <linux/clk.h>
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
18
  #include <linux/libata.h>
07b9733fd   Vladimir Zapolskiy   pata: imx: sort h...
19
  #include <linux/module.h>
ac3167257   Randy Dunlap   headers: separate...
20
  #include <linux/mod_devicetable.h>
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
21
  #include <linux/platform_device.h>
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
22
23
  
  #define DRV_NAME "pata_imx"
fab43e143   Vladimir Zapolskiy   pata: imx: add su...
24
25
26
27
28
29
30
31
32
  #define PATA_IMX_ATA_TIME_OFF		0x00
  #define PATA_IMX_ATA_TIME_ON		0x01
  #define PATA_IMX_ATA_TIME_1		0x02
  #define PATA_IMX_ATA_TIME_2W		0x03
  #define PATA_IMX_ATA_TIME_2R		0x04
  #define PATA_IMX_ATA_TIME_AX		0x05
  #define PATA_IMX_ATA_TIME_PIO_RDX	0x06
  #define PATA_IMX_ATA_TIME_4		0x07
  #define PATA_IMX_ATA_TIME_9		0x08
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
33
34
35
36
37
38
39
40
  #define PATA_IMX_ATA_CONTROL		0x24
  #define PATA_IMX_ATA_CTRL_FIFO_RST_B	(1<<7)
  #define PATA_IMX_ATA_CTRL_ATA_RST_B	(1<<6)
  #define PATA_IMX_ATA_CTRL_IORDY_EN	(1<<0)
  #define PATA_IMX_ATA_INT_EN		0x2C
  #define PATA_IMX_ATA_INTR_ATA_INTRQ2	(1<<3)
  #define PATA_IMX_DRIVE_DATA		0xA0
  #define PATA_IMX_DRIVE_CONTROL		0xD8
fab43e143   Vladimir Zapolskiy   pata: imx: add su...
41
42
43
  static u32 pio_t4[] = { 30,  20,  15,  10,  10 };
  static u32 pio_t9[] = { 20,  15,  10,  10,  10 };
  static u32 pio_tA[] = { 35,  35,  35,  35,  35 };
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
44
45
46
  struct pata_imx_priv {
  	struct clk *clk;
  	/* timings/interrupt/control regs */
51b5539c0   Sascha Hauer   pata_imx: use voi...
47
  	void __iomem *host_regs;
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
48
49
  	u32 ata_ctl;
  };
fab43e143   Vladimir Zapolskiy   pata: imx: add su...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  static void pata_imx_set_timing(struct ata_device *adev,
  				struct pata_imx_priv *priv)
  {
  	struct ata_timing timing;
  	unsigned long clkrate;
  	u32 T, mode;
  
  	clkrate = clk_get_rate(priv->clk);
  
  	if (adev->pio_mode < XFER_PIO_0 || adev->pio_mode > XFER_PIO_4 ||
  	    !clkrate)
  		return;
  
  	T = 1000000000 / clkrate;
  	ata_timing_compute(adev, adev->pio_mode, &timing, T * 1000, 0);
  
  	mode = adev->pio_mode - XFER_PIO_0;
  
  	writeb(3, priv->host_regs + PATA_IMX_ATA_TIME_OFF);
  	writeb(3, priv->host_regs + PATA_IMX_ATA_TIME_ON);
  	writeb(timing.setup, priv->host_regs + PATA_IMX_ATA_TIME_1);
  	writeb(timing.act8b, priv->host_regs + PATA_IMX_ATA_TIME_2W);
  	writeb(timing.act8b, priv->host_regs + PATA_IMX_ATA_TIME_2R);
  	writeb(1, priv->host_regs + PATA_IMX_ATA_TIME_PIO_RDX);
  
  	writeb(pio_t4[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_4);
  	writeb(pio_t9[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_9);
  	writeb(pio_tA[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_AX);
  }
65a443ea5   Vladimir Zapolskiy   pata: imx: set co...
79
  static void pata_imx_set_piomode(struct ata_port *ap, struct ata_device *adev)
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
80
  {
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
81
82
  	struct pata_imx_priv *priv = ap->host->private_data;
  	u32 val;
fab43e143   Vladimir Zapolskiy   pata: imx: add su...
83
  	pata_imx_set_timing(adev, priv);
65a443ea5   Vladimir Zapolskiy   pata: imx: set co...
84
85
86
87
88
89
  	val = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
  	if (ata_pio_need_iordy(adev))
  		val |= PATA_IMX_ATA_CTRL_IORDY_EN;
  	else
  		val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
  	__raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
90
91
92
93
94
95
96
97
  }
  
  static struct scsi_host_template pata_imx_sht = {
  	ATA_PIO_SHT(DRV_NAME),
  };
  
  static struct ata_port_operations pata_imx_port_ops = {
  	.inherits		= &ata_sff_port_ops,
23ebda2fc   Sebastian Andrzej Siewior   libata: remove at...
98
  	.sff_data_xfer		= ata_sff_data_xfer32,
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
99
  	.cable_detect		= ata_cable_unknown,
65a443ea5   Vladimir Zapolskiy   pata: imx: set co...
100
  	.set_piomode		= pata_imx_set_piomode,
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  };
  
  static void pata_imx_setup_port(struct ata_ioports *ioaddr)
  {
  	/* Fixup the port shift for platforms that need it */
  	ioaddr->data_addr	= ioaddr->cmd_addr + (ATA_REG_DATA    << 2);
  	ioaddr->error_addr	= ioaddr->cmd_addr + (ATA_REG_ERR     << 2);
  	ioaddr->feature_addr	= ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
  	ioaddr->nsect_addr	= ioaddr->cmd_addr + (ATA_REG_NSECT   << 2);
  	ioaddr->lbal_addr	= ioaddr->cmd_addr + (ATA_REG_LBAL    << 2);
  	ioaddr->lbam_addr	= ioaddr->cmd_addr + (ATA_REG_LBAM    << 2);
  	ioaddr->lbah_addr	= ioaddr->cmd_addr + (ATA_REG_LBAH    << 2);
  	ioaddr->device_addr	= ioaddr->cmd_addr + (ATA_REG_DEVICE  << 2);
  	ioaddr->status_addr	= ioaddr->cmd_addr + (ATA_REG_STATUS  << 2);
  	ioaddr->command_addr	= ioaddr->cmd_addr + (ATA_REG_CMD     << 2);
  }
0ec249146   Greg Kroah-Hartman   Drivers: ata: rem...
117
  static int pata_imx_probe(struct platform_device *pdev)
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
118
119
120
121
122
123
  {
  	struct ata_host *host;
  	struct ata_port *ap;
  	struct pata_imx_priv *priv;
  	int irq = 0;
  	struct resource *io_res;
ff540d029   Sascha Hauer   pata_imx: cleanup...
124
  	int ret;
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
125

e39c75cf3   Arnaud Patard   ata: Add iMX pata...
126
  	irq = platform_get_irq(pdev, 0);
3ef9cc31e   Fabio Estevam   pata_imx: Propaga...
127
128
  	if (irq < 0)
  		return irq;
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
129
130
131
132
133
  
  	priv = devm_kzalloc(&pdev->dev,
  				sizeof(struct pata_imx_priv), GFP_KERNEL);
  	if (!priv)
  		return -ENOMEM;
50f5a3415   Sascha Hauer   pata_imx: Use dev...
134
  	priv->clk = devm_clk_get(&pdev->dev, NULL);
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
135
136
137
138
139
  	if (IS_ERR(priv->clk)) {
  		dev_err(&pdev->dev, "Failed to get clock
  ");
  		return PTR_ERR(priv->clk);
  	}
0475c947d   Fabio Estevam   ata: pata_imx: Ch...
140
141
142
  	ret = clk_prepare_enable(priv->clk);
  	if (ret)
  		return ret;
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
143
144
  
  	host = ata_host_alloc(&pdev->dev, 1);
ff540d029   Sascha Hauer   pata_imx: cleanup...
145
146
147
148
  	if (!host) {
  		ret = -ENOMEM;
  		goto err;
  	}
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
149
150
151
152
153
  
  	host->private_data = priv;
  	ap = host->ports[0];
  
  	ap->ops = &pata_imx_port_ops;
b986723c4   Vladimir Zapolskiy   pata: imx: suppor...
154
  	ap->pio_mask = ATA_PIO4;
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
155
  	ap->flags |= ATA_FLAG_SLAVE_POSS;
b314fc775   Fabio Estevam   pata_imx: Use dev...
156
157
  	io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res);
13e8e78bd   Bartlomiej Zolnierkiewicz   ata: pata_imx: fi...
158
159
  	if (IS_ERR(priv->host_regs)) {
  		ret = PTR_ERR(priv->host_regs);
ff540d029   Sascha Hauer   pata_imx: cleanup...
160
  		goto err;
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  	}
  
  	ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
  	ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
  
  	ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  
  	pata_imx_setup_port(&ap->ioaddr);
  
  	ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  		(unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA,
  		(unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL);
  
  	/* deassert resets */
  	__raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
  			PATA_IMX_ATA_CTRL_ATA_RST_B,
  			priv->host_regs + PATA_IMX_ATA_CONTROL);
  	/* enable interrupts */
  	__raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
  			priv->host_regs + PATA_IMX_ATA_INT_EN);
  
  	/* activate */
ff540d029   Sascha Hauer   pata_imx: cleanup...
183
  	ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
184
  				&pata_imx_sht);
ff540d029   Sascha Hauer   pata_imx: cleanup...
185
186
187
188
189
  	if (ret)
  		goto err;
  
  	return 0;
  err:
a18dada08   Fabio Estevam   pata_imx: Convert...
190
  	clk_disable_unprepare(priv->clk);
50f5a3415   Sascha Hauer   pata_imx: Use dev...
191

ff540d029   Sascha Hauer   pata_imx: cleanup...
192
  	return ret;
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
193
  }
0ec249146   Greg Kroah-Hartman   Drivers: ata: rem...
194
  static int pata_imx_remove(struct platform_device *pdev)
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
195
  {
d89995db5   Jingoo Han   ata: use platform...
196
  	struct ata_host *host = platform_get_drvdata(pdev);
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
197
198
199
200
201
  	struct pata_imx_priv *priv = host->private_data;
  
  	ata_host_detach(host);
  
  	__raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
a18dada08   Fabio Estevam   pata_imx: Convert...
202
  	clk_disable_unprepare(priv->clk);
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
203
204
205
  
  	return 0;
  }
58eb8cd56   Bartlomiej Zolnierkiewicz   ata: use CONFIG_P...
206
  #ifdef CONFIG_PM_SLEEP
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
207
208
209
210
211
212
213
214
215
216
217
  static int pata_imx_suspend(struct device *dev)
  {
  	struct ata_host *host = dev_get_drvdata(dev);
  	struct pata_imx_priv *priv = host->private_data;
  	int ret;
  
  	ret = ata_host_suspend(host, PMSG_SUSPEND);
  	if (!ret) {
  		__raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
  		priv->ata_ctl =
  			__raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
a18dada08   Fabio Estevam   pata_imx: Convert...
218
  		clk_disable_unprepare(priv->clk);
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
219
220
221
222
223
224
225
226
227
  	}
  
  	return ret;
  }
  
  static int pata_imx_resume(struct device *dev)
  {
  	struct ata_host *host = dev_get_drvdata(dev);
  	struct pata_imx_priv *priv = host->private_data;
0475c947d   Fabio Estevam   ata: pata_imx: Ch...
228
229
230
  	int ret = clk_prepare_enable(priv->clk);
  	if (ret)
  		return ret;
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
231
232
233
234
235
236
237
238
239
240
  
  	__raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
  
  	__raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
  			priv->host_regs + PATA_IMX_ATA_INT_EN);
  
  	ata_host_resume(host);
  
  	return 0;
  }
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
241
  #endif
36888e955   Fabio Estevam   ata: pata_imx: Us...
242
  static SIMPLE_DEV_PM_OPS(pata_imx_pm_ops, pata_imx_suspend, pata_imx_resume);
b9d15db2b   Sascha Hauer   pata_imx: add dev...
243
244
245
246
247
248
249
  static const struct of_device_id imx_pata_dt_ids[] = {
  	{
  		.compatible = "fsl,imx27-pata",
  	}, {
  		/* sentinel */
  	}
  };
71ab1d58c   Niels de Vos   pata_imx: expose ...
250
  MODULE_DEVICE_TABLE(of, imx_pata_dt_ids);
b9d15db2b   Sascha Hauer   pata_imx: add dev...
251

e39c75cf3   Arnaud Patard   ata: Add iMX pata...
252
253
  static struct platform_driver pata_imx_driver = {
  	.probe		= pata_imx_probe,
0ec249146   Greg Kroah-Hartman   Drivers: ata: rem...
254
  	.remove		= pata_imx_remove,
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
255
256
  	.driver = {
  		.name		= DRV_NAME,
b9d15db2b   Sascha Hauer   pata_imx: add dev...
257
  		.of_match_table	= imx_pata_dt_ids,
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
258
  		.pm		= &pata_imx_pm_ops,
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
259
260
  	},
  };
99c8ea3e5   Axel Lin   SATA/PATA: conver...
261
  module_platform_driver(pata_imx_driver);
e39c75cf3   Arnaud Patard   ata: Add iMX pata...
262
263
264
265
266
  
  MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
  MODULE_DESCRIPTION("low-level driver for iMX PATA");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS("platform:" DRV_NAME);