2 * Freescale iMX PATA driver
4 * Copyright (C) 2011 Arnaud Patard <arnaud.patard@rtp-net.org>
6 * Based on pata_platform - Copyright (C) 2006 - 2007 Paul Mundt
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
14 * - check if timing stuff needed
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>
25 #define DRV_NAME "pata_imx"
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
36 struct pata_imx_priv {
38 /* timings/interrupt/control regs */
39 void __iomem *host_regs;
43 static int pata_imx_set_mode(struct ata_link *link, struct ata_device **unused)
45 struct ata_device *dev;
46 struct ata_port *ap = link->ap;
47 struct pata_imx_priv *priv = ap->host->private_data;
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;
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;
59 val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
60 __raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
62 ata_dev_info(dev, "configured for PIO\n");
67 static struct scsi_host_template pata_imx_sht = {
68 ATA_PIO_SHT(DRV_NAME),
71 static 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,
78 static void pata_imx_setup_port(struct ata_ioports *ioaddr)
80 /* Fixup the port shift for platforms that need it */
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);
93 static int pata_imx_probe(struct platform_device *pdev)
95 struct ata_host *host;
97 struct pata_imx_priv *priv;
99 struct resource *io_res;
102 irq = platform_get_irq(pdev, 0);
106 priv = devm_kzalloc(&pdev->dev,
107 sizeof(struct pata_imx_priv), GFP_KERNEL);
111 priv->clk = devm_clk_get(&pdev->dev, NULL);
112 if (IS_ERR(priv->clk)) {
113 dev_err(&pdev->dev, "Failed to get clock\n");
114 return PTR_ERR(priv->clk);
117 ret = clk_prepare_enable(priv->clk);
121 host = ata_host_alloc(&pdev->dev, 1);
127 host->private_data = priv;
130 ap->ops = &pata_imx_port_ops;
131 ap->pio_mask = ATA_PIO0;
132 ap->flags |= ATA_FLAG_SLAVE_POSS;
134 io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135 priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res);
136 if (IS_ERR(priv->host_regs)) {
137 ret = PTR_ERR(priv->host_regs);
141 ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
142 ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
144 ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
146 pata_imx_setup_port(&ap->ioaddr);
148 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
149 (unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA,
150 (unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL);
152 /* deassert resets */
153 __raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
154 PATA_IMX_ATA_CTRL_ATA_RST_B,
155 priv->host_regs + PATA_IMX_ATA_CONTROL);
156 /* enable interrupts */
157 __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
158 priv->host_regs + PATA_IMX_ATA_INT_EN);
161 ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
169 clk_disable_unprepare(priv->clk);
174 static int pata_imx_remove(struct platform_device *pdev)
176 struct ata_host *host = platform_get_drvdata(pdev);
177 struct pata_imx_priv *priv = host->private_data;
179 ata_host_detach(host);
181 __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
183 clk_disable_unprepare(priv->clk);
188 #ifdef CONFIG_PM_SLEEP
189 static int pata_imx_suspend(struct device *dev)
191 struct ata_host *host = dev_get_drvdata(dev);
192 struct pata_imx_priv *priv = host->private_data;
195 ret = ata_host_suspend(host, PMSG_SUSPEND);
197 __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
199 __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
200 clk_disable_unprepare(priv->clk);
206 static int pata_imx_resume(struct device *dev)
208 struct ata_host *host = dev_get_drvdata(dev);
209 struct pata_imx_priv *priv = host->private_data;
211 int ret = clk_prepare_enable(priv->clk);
215 __raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
217 __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
218 priv->host_regs + PATA_IMX_ATA_INT_EN);
220 ata_host_resume(host);
225 static const struct dev_pm_ops pata_imx_pm_ops = {
226 .suspend = pata_imx_suspend,
227 .resume = pata_imx_resume,
231 static const struct of_device_id imx_pata_dt_ids[] = {
233 .compatible = "fsl,imx27-pata",
238 MODULE_DEVICE_TABLE(of, imx_pata_dt_ids);
240 static struct platform_driver pata_imx_driver = {
241 .probe = pata_imx_probe,
242 .remove = pata_imx_remove,
245 .of_match_table = imx_pata_dt_ids,
246 .owner = THIS_MODULE,
247 #ifdef CONFIG_PM_SLEEP
248 .pm = &pata_imx_pm_ops,
253 module_platform_driver(pata_imx_driver);
255 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
256 MODULE_DESCRIPTION("low-level driver for iMX PATA");
257 MODULE_LICENSE("GPL");
258 MODULE_ALIAS("platform:" DRV_NAME);