blob: 2e538726802b68674f172a7c76e459f743ed5e91 [file] [log] [blame]
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +02001/*
2 * Freescale iMX PATA driver
3 *
4 * Copyright (C) 2011 Arnaud Patard <arnaud.patard@rtp-net.org>
5 *
6 * Based on pata_platform - Copyright (C) 2006 - 2007 Paul Mundt
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
11 *
12 * TODO:
13 * - dmaengine support
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020014 */
Vladimir Zapolskiy07b97332016-11-09 02:56:35 +020015
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020016#include <linux/ata.h>
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020017#include <linux/clk.h>
Vladimir Zapolskiy07b97332016-11-09 02:56:35 +020018#include <linux/libata.h>
19#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070020#include <linux/mod_devicetable.h>
Vladimir Zapolskiy07b97332016-11-09 02:56:35 +020021#include <linux/platform_device.h>
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020022
23#define DRV_NAME "pata_imx"
24
Vladimir Zapolskiyfab43e12016-11-09 02:56:37 +020025#define PATA_IMX_ATA_TIME_OFF 0x00
26#define PATA_IMX_ATA_TIME_ON 0x01
27#define PATA_IMX_ATA_TIME_1 0x02
28#define PATA_IMX_ATA_TIME_2W 0x03
29#define PATA_IMX_ATA_TIME_2R 0x04
30#define PATA_IMX_ATA_TIME_AX 0x05
31#define PATA_IMX_ATA_TIME_PIO_RDX 0x06
32#define PATA_IMX_ATA_TIME_4 0x07
33#define PATA_IMX_ATA_TIME_9 0x08
34
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020035#define PATA_IMX_ATA_CONTROL 0x24
36#define PATA_IMX_ATA_CTRL_FIFO_RST_B (1<<7)
37#define PATA_IMX_ATA_CTRL_ATA_RST_B (1<<6)
38#define PATA_IMX_ATA_CTRL_IORDY_EN (1<<0)
39#define PATA_IMX_ATA_INT_EN 0x2C
40#define PATA_IMX_ATA_INTR_ATA_INTRQ2 (1<<3)
41#define PATA_IMX_DRIVE_DATA 0xA0
42#define PATA_IMX_DRIVE_CONTROL 0xD8
43
Vladimir Zapolskiyfab43e12016-11-09 02:56:37 +020044static u32 pio_t4[] = { 30, 20, 15, 10, 10 };
45static u32 pio_t9[] = { 20, 15, 10, 10, 10 };
46static u32 pio_tA[] = { 35, 35, 35, 35, 35 };
47
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020048struct pata_imx_priv {
49 struct clk *clk;
50 /* timings/interrupt/control regs */
Sascha Hauer51b55392013-04-04 11:25:06 +020051 void __iomem *host_regs;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020052 u32 ata_ctl;
53};
54
Vladimir Zapolskiyfab43e12016-11-09 02:56:37 +020055static void pata_imx_set_timing(struct ata_device *adev,
56 struct pata_imx_priv *priv)
57{
58 struct ata_timing timing;
59 unsigned long clkrate;
60 u32 T, mode;
61
62 clkrate = clk_get_rate(priv->clk);
63
64 if (adev->pio_mode < XFER_PIO_0 || adev->pio_mode > XFER_PIO_4 ||
65 !clkrate)
66 return;
67
68 T = 1000000000 / clkrate;
69 ata_timing_compute(adev, adev->pio_mode, &timing, T * 1000, 0);
70
71 mode = adev->pio_mode - XFER_PIO_0;
72
73 writeb(3, priv->host_regs + PATA_IMX_ATA_TIME_OFF);
74 writeb(3, priv->host_regs + PATA_IMX_ATA_TIME_ON);
75 writeb(timing.setup, priv->host_regs + PATA_IMX_ATA_TIME_1);
76 writeb(timing.act8b, priv->host_regs + PATA_IMX_ATA_TIME_2W);
77 writeb(timing.act8b, priv->host_regs + PATA_IMX_ATA_TIME_2R);
78 writeb(1, priv->host_regs + PATA_IMX_ATA_TIME_PIO_RDX);
79
80 writeb(pio_t4[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_4);
81 writeb(pio_t9[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_9);
82 writeb(pio_tA[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_AX);
83}
84
Vladimir Zapolskiy65a443e2016-11-09 02:56:36 +020085static void pata_imx_set_piomode(struct ata_port *ap, struct ata_device *adev)
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020086{
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020087 struct pata_imx_priv *priv = ap->host->private_data;
88 u32 val;
89
Vladimir Zapolskiyfab43e12016-11-09 02:56:37 +020090 pata_imx_set_timing(adev, priv);
91
Vladimir Zapolskiy65a443e2016-11-09 02:56:36 +020092 val = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
93 if (ata_pio_need_iordy(adev))
94 val |= PATA_IMX_ATA_CTRL_IORDY_EN;
95 else
96 val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
97 __raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020098}
99
100static struct scsi_host_template pata_imx_sht = {
101 ATA_PIO_SHT(DRV_NAME),
102};
103
104static struct ata_port_operations pata_imx_port_ops = {
105 .inherits = &ata_sff_port_ops,
Sebastian Andrzej Siewior23ebda22018-07-11 17:21:05 +0200106 .sff_data_xfer = ata_sff_data_xfer32,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200107 .cable_detect = ata_cable_unknown,
Vladimir Zapolskiy65a443e2016-11-09 02:56:36 +0200108 .set_piomode = pata_imx_set_piomode,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200109};
110
111static void pata_imx_setup_port(struct ata_ioports *ioaddr)
112{
113 /* Fixup the port shift for platforms that need it */
114 ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
115 ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
116 ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
117 ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
118 ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
119 ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
120 ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
121 ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
122 ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
123 ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
124}
125
Greg Kroah-Hartman0ec24912012-12-21 13:19:58 -0800126static int pata_imx_probe(struct platform_device *pdev)
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200127{
128 struct ata_host *host;
129 struct ata_port *ap;
130 struct pata_imx_priv *priv;
131 int irq = 0;
132 struct resource *io_res;
Sascha Hauerff540d02013-04-04 11:25:05 +0200133 int ret;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200134
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200135 irq = platform_get_irq(pdev, 0);
Fabio Estevam3ef9cc32014-02-17 19:05:57 -0300136 if (irq < 0)
137 return irq;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200138
139 priv = devm_kzalloc(&pdev->dev,
140 sizeof(struct pata_imx_priv), GFP_KERNEL);
141 if (!priv)
142 return -ENOMEM;
143
Sascha Hauer50f5a342013-04-04 11:25:04 +0200144 priv->clk = devm_clk_get(&pdev->dev, NULL);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200145 if (IS_ERR(priv->clk)) {
146 dev_err(&pdev->dev, "Failed to get clock\n");
147 return PTR_ERR(priv->clk);
148 }
149
Fabio Estevam0475c942014-01-25 19:46:23 -0200150 ret = clk_prepare_enable(priv->clk);
151 if (ret)
152 return ret;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200153
154 host = ata_host_alloc(&pdev->dev, 1);
Sascha Hauerff540d02013-04-04 11:25:05 +0200155 if (!host) {
156 ret = -ENOMEM;
157 goto err;
158 }
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200159
160 host->private_data = priv;
161 ap = host->ports[0];
162
163 ap->ops = &pata_imx_port_ops;
Vladimir Zapolskiyb9867232016-11-09 02:56:38 +0200164 ap->pio_mask = ATA_PIO4;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200165 ap->flags |= ATA_FLAG_SLAVE_POSS;
166
Fabio Estevamb314fc72014-02-17 19:05:56 -0300167 io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
168 priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res);
Bartlomiej Zolnierkiewicz13e8e782014-03-14 18:33:13 +0100169 if (IS_ERR(priv->host_regs)) {
170 ret = PTR_ERR(priv->host_regs);
Sascha Hauerff540d02013-04-04 11:25:05 +0200171 goto err;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200172 }
173
174 ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
175 ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
176
177 ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
178
179 pata_imx_setup_port(&ap->ioaddr);
180
181 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
182 (unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA,
183 (unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL);
184
185 /* deassert resets */
186 __raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
187 PATA_IMX_ATA_CTRL_ATA_RST_B,
188 priv->host_regs + PATA_IMX_ATA_CONTROL);
189 /* enable interrupts */
190 __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
191 priv->host_regs + PATA_IMX_ATA_INT_EN);
192
193 /* activate */
Sascha Hauerff540d02013-04-04 11:25:05 +0200194 ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200195 &pata_imx_sht);
196
Sascha Hauerff540d02013-04-04 11:25:05 +0200197 if (ret)
198 goto err;
199
200 return 0;
201err:
Fabio Estevama18dada2012-07-07 19:34:05 -0300202 clk_disable_unprepare(priv->clk);
Sascha Hauer50f5a342013-04-04 11:25:04 +0200203
Sascha Hauerff540d02013-04-04 11:25:05 +0200204 return ret;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200205}
206
Greg Kroah-Hartman0ec24912012-12-21 13:19:58 -0800207static int pata_imx_remove(struct platform_device *pdev)
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200208{
Jingoo Hand89995d2013-05-23 19:41:21 +0900209 struct ata_host *host = platform_get_drvdata(pdev);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200210 struct pata_imx_priv *priv = host->private_data;
211
212 ata_host_detach(host);
213
214 __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
215
Fabio Estevama18dada2012-07-07 19:34:05 -0300216 clk_disable_unprepare(priv->clk);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200217
218 return 0;
219}
220
Bartlomiej Zolnierkiewicz58eb8cd2014-05-07 17:17:44 +0200221#ifdef CONFIG_PM_SLEEP
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200222static int pata_imx_suspend(struct device *dev)
223{
224 struct ata_host *host = dev_get_drvdata(dev);
225 struct pata_imx_priv *priv = host->private_data;
226 int ret;
227
228 ret = ata_host_suspend(host, PMSG_SUSPEND);
229 if (!ret) {
230 __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
231 priv->ata_ctl =
232 __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
Fabio Estevama18dada2012-07-07 19:34:05 -0300233 clk_disable_unprepare(priv->clk);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200234 }
235
236 return ret;
237}
238
239static int pata_imx_resume(struct device *dev)
240{
241 struct ata_host *host = dev_get_drvdata(dev);
242 struct pata_imx_priv *priv = host->private_data;
243
Fabio Estevam0475c942014-01-25 19:46:23 -0200244 int ret = clk_prepare_enable(priv->clk);
245 if (ret)
246 return ret;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200247
248 __raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
249
250 __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
251 priv->host_regs + PATA_IMX_ATA_INT_EN);
252
253 ata_host_resume(host);
254
255 return 0;
256}
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200257#endif
258
Fabio Estevam36888e92014-10-04 09:03:06 -0300259static SIMPLE_DEV_PM_OPS(pata_imx_pm_ops, pata_imx_suspend, pata_imx_resume);
260
Sascha Hauerb9d15db2013-04-04 11:25:07 +0200261static const struct of_device_id imx_pata_dt_ids[] = {
262 {
263 .compatible = "fsl,imx27-pata",
264 }, {
265 /* sentinel */
266 }
267};
Niels de Vos71ab1d52013-07-29 09:38:38 +0200268MODULE_DEVICE_TABLE(of, imx_pata_dt_ids);
Sascha Hauerb9d15db2013-04-04 11:25:07 +0200269
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200270static struct platform_driver pata_imx_driver = {
271 .probe = pata_imx_probe,
Greg Kroah-Hartman0ec24912012-12-21 13:19:58 -0800272 .remove = pata_imx_remove,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200273 .driver = {
274 .name = DRV_NAME,
Sascha Hauerb9d15db2013-04-04 11:25:07 +0200275 .of_match_table = imx_pata_dt_ids,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200276 .pm = &pata_imx_pm_ops,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200277 },
278};
279
Axel Lin99c8ea32011-11-27 14:44:26 +0800280module_platform_driver(pata_imx_driver);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200281
282MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
283MODULE_DESCRIPTION("low-level driver for iMX PATA");
284MODULE_LICENSE("GPL");
285MODULE_ALIAS("platform:" DRV_NAME);