blob: 00df18b07fa68cb297f399fa3648f07e5b0492b2 [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
14 * - check if timing stuff needed
15 */
Vladimir Zapolskiy07b97332016-11-09 02:56:35 +020016
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020017#include <linux/ata.h>
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020018#include <linux/clk.h>
Vladimir Zapolskiy07b97332016-11-09 02:56:35 +020019#include <linux/libata.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020022
23#define DRV_NAME "pata_imx"
24
25#define PATA_IMX_ATA_CONTROL 0x24
26#define PATA_IMX_ATA_CTRL_FIFO_RST_B (1<<7)
27#define PATA_IMX_ATA_CTRL_ATA_RST_B (1<<6)
28#define PATA_IMX_ATA_CTRL_IORDY_EN (1<<0)
29#define PATA_IMX_ATA_INT_EN 0x2C
30#define PATA_IMX_ATA_INTR_ATA_INTRQ2 (1<<3)
31#define PATA_IMX_DRIVE_DATA 0xA0
32#define PATA_IMX_DRIVE_CONTROL 0xD8
33
34struct pata_imx_priv {
35 struct clk *clk;
36 /* timings/interrupt/control regs */
Sascha Hauer51b55392013-04-04 11:25:06 +020037 void __iomem *host_regs;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020038 u32 ata_ctl;
39};
40
Vladimir Zapolskiy65a443e2016-11-09 02:56:36 +020041static void pata_imx_set_piomode(struct ata_port *ap, struct ata_device *adev)
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020042{
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020043 struct pata_imx_priv *priv = ap->host->private_data;
44 u32 val;
45
Vladimir Zapolskiy65a443e2016-11-09 02:56:36 +020046 val = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
47 if (ata_pio_need_iordy(adev))
48 val |= PATA_IMX_ATA_CTRL_IORDY_EN;
49 else
50 val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
51 __raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020052}
53
54static struct scsi_host_template pata_imx_sht = {
55 ATA_PIO_SHT(DRV_NAME),
56};
57
58static struct ata_port_operations pata_imx_port_ops = {
59 .inherits = &ata_sff_port_ops,
60 .sff_data_xfer = ata_sff_data_xfer_noirq,
61 .cable_detect = ata_cable_unknown,
Vladimir Zapolskiy65a443e2016-11-09 02:56:36 +020062 .set_piomode = pata_imx_set_piomode,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020063};
64
65static void pata_imx_setup_port(struct ata_ioports *ioaddr)
66{
67 /* Fixup the port shift for platforms that need it */
68 ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
69 ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
70 ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
71 ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
72 ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
73 ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
74 ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
75 ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
76 ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
77 ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
78}
79
Greg Kroah-Hartman0ec24912012-12-21 13:19:58 -080080static int pata_imx_probe(struct platform_device *pdev)
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020081{
82 struct ata_host *host;
83 struct ata_port *ap;
84 struct pata_imx_priv *priv;
85 int irq = 0;
86 struct resource *io_res;
Sascha Hauerff540d02013-04-04 11:25:05 +020087 int ret;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020088
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020089 irq = platform_get_irq(pdev, 0);
Fabio Estevam3ef9cc32014-02-17 19:05:57 -030090 if (irq < 0)
91 return irq;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020092
93 priv = devm_kzalloc(&pdev->dev,
94 sizeof(struct pata_imx_priv), GFP_KERNEL);
95 if (!priv)
96 return -ENOMEM;
97
Sascha Hauer50f5a342013-04-04 11:25:04 +020098 priv->clk = devm_clk_get(&pdev->dev, NULL);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +020099 if (IS_ERR(priv->clk)) {
100 dev_err(&pdev->dev, "Failed to get clock\n");
101 return PTR_ERR(priv->clk);
102 }
103
Fabio Estevam0475c942014-01-25 19:46:23 -0200104 ret = clk_prepare_enable(priv->clk);
105 if (ret)
106 return ret;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200107
108 host = ata_host_alloc(&pdev->dev, 1);
Sascha Hauerff540d02013-04-04 11:25:05 +0200109 if (!host) {
110 ret = -ENOMEM;
111 goto err;
112 }
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200113
114 host->private_data = priv;
115 ap = host->ports[0];
116
117 ap->ops = &pata_imx_port_ops;
118 ap->pio_mask = ATA_PIO0;
119 ap->flags |= ATA_FLAG_SLAVE_POSS;
120
Fabio Estevamb314fc72014-02-17 19:05:56 -0300121 io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122 priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res);
Bartlomiej Zolnierkiewicz13e8e782014-03-14 18:33:13 +0100123 if (IS_ERR(priv->host_regs)) {
124 ret = PTR_ERR(priv->host_regs);
Sascha Hauerff540d02013-04-04 11:25:05 +0200125 goto err;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200126 }
127
128 ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
129 ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
130
131 ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
132
133 pata_imx_setup_port(&ap->ioaddr);
134
135 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
136 (unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA,
137 (unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL);
138
139 /* deassert resets */
140 __raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
141 PATA_IMX_ATA_CTRL_ATA_RST_B,
142 priv->host_regs + PATA_IMX_ATA_CONTROL);
143 /* enable interrupts */
144 __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
145 priv->host_regs + PATA_IMX_ATA_INT_EN);
146
147 /* activate */
Sascha Hauerff540d02013-04-04 11:25:05 +0200148 ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200149 &pata_imx_sht);
150
Sascha Hauerff540d02013-04-04 11:25:05 +0200151 if (ret)
152 goto err;
153
154 return 0;
155err:
Fabio Estevama18dada2012-07-07 19:34:05 -0300156 clk_disable_unprepare(priv->clk);
Sascha Hauer50f5a342013-04-04 11:25:04 +0200157
Sascha Hauerff540d02013-04-04 11:25:05 +0200158 return ret;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200159}
160
Greg Kroah-Hartman0ec24912012-12-21 13:19:58 -0800161static int pata_imx_remove(struct platform_device *pdev)
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200162{
Jingoo Hand89995d2013-05-23 19:41:21 +0900163 struct ata_host *host = platform_get_drvdata(pdev);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200164 struct pata_imx_priv *priv = host->private_data;
165
166 ata_host_detach(host);
167
168 __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
169
Fabio Estevama18dada2012-07-07 19:34:05 -0300170 clk_disable_unprepare(priv->clk);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200171
172 return 0;
173}
174
Bartlomiej Zolnierkiewicz58eb8cd2014-05-07 17:17:44 +0200175#ifdef CONFIG_PM_SLEEP
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200176static int pata_imx_suspend(struct device *dev)
177{
178 struct ata_host *host = dev_get_drvdata(dev);
179 struct pata_imx_priv *priv = host->private_data;
180 int ret;
181
182 ret = ata_host_suspend(host, PMSG_SUSPEND);
183 if (!ret) {
184 __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
185 priv->ata_ctl =
186 __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
Fabio Estevama18dada2012-07-07 19:34:05 -0300187 clk_disable_unprepare(priv->clk);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200188 }
189
190 return ret;
191}
192
193static int pata_imx_resume(struct device *dev)
194{
195 struct ata_host *host = dev_get_drvdata(dev);
196 struct pata_imx_priv *priv = host->private_data;
197
Fabio Estevam0475c942014-01-25 19:46:23 -0200198 int ret = clk_prepare_enable(priv->clk);
199 if (ret)
200 return ret;
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200201
202 __raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
203
204 __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
205 priv->host_regs + PATA_IMX_ATA_INT_EN);
206
207 ata_host_resume(host);
208
209 return 0;
210}
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200211#endif
212
Fabio Estevam36888e92014-10-04 09:03:06 -0300213static SIMPLE_DEV_PM_OPS(pata_imx_pm_ops, pata_imx_suspend, pata_imx_resume);
214
Sascha Hauerb9d15db2013-04-04 11:25:07 +0200215static const struct of_device_id imx_pata_dt_ids[] = {
216 {
217 .compatible = "fsl,imx27-pata",
218 }, {
219 /* sentinel */
220 }
221};
Niels de Vos71ab1d52013-07-29 09:38:38 +0200222MODULE_DEVICE_TABLE(of, imx_pata_dt_ids);
Sascha Hauerb9d15db2013-04-04 11:25:07 +0200223
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200224static struct platform_driver pata_imx_driver = {
225 .probe = pata_imx_probe,
Greg Kroah-Hartman0ec24912012-12-21 13:19:58 -0800226 .remove = pata_imx_remove,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200227 .driver = {
228 .name = DRV_NAME,
Sascha Hauerb9d15db2013-04-04 11:25:07 +0200229 .of_match_table = imx_pata_dt_ids,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200230 .pm = &pata_imx_pm_ops,
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200231 },
232};
233
Axel Lin99c8ea32011-11-27 14:44:26 +0800234module_platform_driver(pata_imx_driver);
Arnaud Patard (Rtp)e39c75c2011-07-26 16:58:19 +0200235
236MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
237MODULE_DESCRIPTION("low-level driver for iMX PATA");
238MODULE_LICENSE("GPL");
239MODULE_ALIAS("platform:" DRV_NAME);