1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/blkdev.h>
19#include <scsi/scsi_host.h>
20#include <linux/ata.h>
21#include <linux/libata.h>
22#include <linux/platform_device.h>
23#include <linux/clk.h>
24
25#define DRV_NAME "pata_imx"
26
27#define PATA_IMX_ATA_CONTROL 0x24
28#define PATA_IMX_ATA_CTRL_FIFO_RST_B (1<<7)
29#define PATA_IMX_ATA_CTRL_ATA_RST_B (1<<6)
30#define PATA_IMX_ATA_CTRL_IORDY_EN (1<<0)
31#define PATA_IMX_ATA_INT_EN 0x2C
32#define PATA_IMX_ATA_INTR_ATA_INTRQ2 (1<<3)
33#define PATA_IMX_DRIVE_DATA 0xA0
34#define PATA_IMX_DRIVE_CONTROL 0xD8
35
36struct pata_imx_priv {
37 struct clk *clk;
38
39 void __iomem *host_regs;
40 u32 ata_ctl;
41};
42
43static int pata_imx_set_mode(struct ata_link *link, struct ata_device **unused)
44{
45 struct ata_device *dev;
46 struct ata_port *ap = link->ap;
47 struct pata_imx_priv *priv = ap->host->private_data;
48 u32 val;
49
50 ata_for_each_dev(dev, link, ENABLED) {
51 dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
52 dev->xfer_shift = ATA_SHIFT_PIO;
53 dev->flags |= ATA_DFLAG_PIO;
54
55 val = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
56 if (ata_pio_need_iordy(dev))
57 val |= PATA_IMX_ATA_CTRL_IORDY_EN;
58 else
59 val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
60 __raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
61
62 ata_dev_info(dev, "configured for PIO\n");
63 }
64 return 0;
65}
66
67static struct scsi_host_template pata_imx_sht = {
68 ATA_PIO_SHT(DRV_NAME),
69};
70
71static struct ata_port_operations pata_imx_port_ops = {
72 .inherits = &ata_sff_port_ops,
73 .sff_data_xfer = ata_sff_data_xfer_noirq,
74 .cable_detect = ata_cable_unknown,
75 .set_mode = pata_imx_set_mode,
76};
77
78static void pata_imx_setup_port(struct ata_ioports *ioaddr)
79{
80
81 ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
82 ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
83 ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
84 ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
85 ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
86 ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
87 ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
88 ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
89 ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
90 ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
91}
92
93static int pata_imx_probe(struct platform_device *pdev)
94{
95 struct ata_host *host;
96 struct ata_port *ap;
97 struct pata_imx_priv *priv;
98 int irq = 0;
99 struct resource *io_res;
100 int ret;
101
102 io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
103 if (io_res == NULL)
104 return -EINVAL;
105
106 irq = platform_get_irq(pdev, 0);
107 if (irq <= 0)
108 return -EINVAL;
109
110 priv = devm_kzalloc(&pdev->dev,
111 sizeof(struct pata_imx_priv), GFP_KERNEL);
112 if (!priv)
113 return -ENOMEM;
114
115 priv->clk = devm_clk_get(&pdev->dev, NULL);
116 if (IS_ERR(priv->clk)) {
117 dev_err(&pdev->dev, "Failed to get clock\n");
118 return PTR_ERR(priv->clk);
119 }
120
121 clk_prepare_enable(priv->clk);
122
123 host = ata_host_alloc(&pdev->dev, 1);
124 if (!host) {
125 ret = -ENOMEM;
126 goto err;
127 }
128
129 host->private_data = priv;
130 ap = host->ports[0];
131
132 ap->ops = &pata_imx_port_ops;
133 ap->pio_mask = ATA_PIO0;
134 ap->flags |= ATA_FLAG_SLAVE_POSS;
135
136 priv->host_regs = devm_ioremap(&pdev->dev, io_res->start,
137 resource_size(io_res));
138 if (!priv->host_regs) {
139 dev_err(&pdev->dev, "failed to map IO/CTL base\n");
140 ret = -EBUSY;
141 goto err;
142 }
143
144 ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
145 ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
146
147 ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
148
149 pata_imx_setup_port(&ap->ioaddr);
150
151 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
152 (unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA,
153 (unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL);
154
155
156 __raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
157 PATA_IMX_ATA_CTRL_ATA_RST_B,
158 priv->host_regs + PATA_IMX_ATA_CONTROL);
159
160 __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
161 priv->host_regs + PATA_IMX_ATA_INT_EN);
162
163
164 ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
165 &pata_imx_sht);
166
167 if (ret)
168 goto err;
169
170 return 0;
171err:
172 clk_disable_unprepare(priv->clk);
173
174 return ret;
175}
176
177static int pata_imx_remove(struct platform_device *pdev)
178{
179 struct ata_host *host = dev_get_drvdata(&pdev->dev);
180 struct pata_imx_priv *priv = host->private_data;
181
182 ata_host_detach(host);
183
184 __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
185
186 clk_disable_unprepare(priv->clk);
187
188 return 0;
189}
190
191#ifdef CONFIG_PM
192static int pata_imx_suspend(struct device *dev)
193{
194 struct ata_host *host = dev_get_drvdata(dev);
195 struct pata_imx_priv *priv = host->private_data;
196 int ret;
197
198 ret = ata_host_suspend(host, PMSG_SUSPEND);
199 if (!ret) {
200 __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
201 priv->ata_ctl =
202 __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
203 clk_disable_unprepare(priv->clk);
204 }
205
206 return ret;
207}
208
209static int pata_imx_resume(struct device *dev)
210{
211 struct ata_host *host = dev_get_drvdata(dev);
212 struct pata_imx_priv *priv = host->private_data;
213
214 clk_prepare_enable(priv->clk);
215
216 __raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
217
218 __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
219 priv->host_regs + PATA_IMX_ATA_INT_EN);
220
221 ata_host_resume(host);
222
223 return 0;
224}
225
226static const struct dev_pm_ops pata_imx_pm_ops = {
227 .suspend = pata_imx_suspend,
228 .resume = pata_imx_resume,
229};
230#endif
231
232static const struct of_device_id imx_pata_dt_ids[] = {
233 {
234 .compatible = "fsl,imx27-pata",
235 }, {
236
237 }
238};
239
240static struct platform_driver pata_imx_driver = {
241 .probe = pata_imx_probe,
242 .remove = pata_imx_remove,
243 .driver = {
244 .name = DRV_NAME,
245 .of_match_table = imx_pata_dt_ids,
246 .owner = THIS_MODULE,
247#ifdef CONFIG_PM
248 .pm = &pata_imx_pm_ops,
249#endif
250 },
251};
252
253module_platform_driver(pata_imx_driver);
254
255MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
256MODULE_DESCRIPTION("low-level driver for iMX PATA");
257MODULE_LICENSE("GPL");
258MODULE_ALIAS("platform:" DRV_NAME);
259