Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 1 | /* |
| 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 Zapolskiy | 07b9733 | 2016-11-09 02:56:35 +0200 | [diff] [blame] | 16 | |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 17 | #include <linux/ata.h> |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 18 | #include <linux/clk.h> |
Vladimir Zapolskiy | 07b9733 | 2016-11-09 02:56:35 +0200 | [diff] [blame] | 19 | #include <linux/libata.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 22 | |
| 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 | |
| 34 | struct pata_imx_priv { |
| 35 | struct clk *clk; |
| 36 | /* timings/interrupt/control regs */ |
Sascha Hauer | 51b5539 | 2013-04-04 11:25:06 +0200 | [diff] [blame] | 37 | void __iomem *host_regs; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 38 | u32 ata_ctl; |
| 39 | }; |
| 40 | |
Vladimir Zapolskiy | 65a443e | 2016-11-09 02:56:36 +0200 | [diff] [blame^] | 41 | static void pata_imx_set_piomode(struct ata_port *ap, struct ata_device *adev) |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 42 | { |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 43 | struct pata_imx_priv *priv = ap->host->private_data; |
| 44 | u32 val; |
| 45 | |
Vladimir Zapolskiy | 65a443e | 2016-11-09 02:56:36 +0200 | [diff] [blame^] | 46 | 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) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static struct scsi_host_template pata_imx_sht = { |
| 55 | ATA_PIO_SHT(DRV_NAME), |
| 56 | }; |
| 57 | |
| 58 | static 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 Zapolskiy | 65a443e | 2016-11-09 02:56:36 +0200 | [diff] [blame^] | 62 | .set_piomode = pata_imx_set_piomode, |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static 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-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 80 | static int pata_imx_probe(struct platform_device *pdev) |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 81 | { |
| 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 Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 87 | int ret; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 88 | |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 89 | irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 3ef9cc3 | 2014-02-17 19:05:57 -0300 | [diff] [blame] | 90 | if (irq < 0) |
| 91 | return irq; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 92 | |
| 93 | priv = devm_kzalloc(&pdev->dev, |
| 94 | sizeof(struct pata_imx_priv), GFP_KERNEL); |
| 95 | if (!priv) |
| 96 | return -ENOMEM; |
| 97 | |
Sascha Hauer | 50f5a34 | 2013-04-04 11:25:04 +0200 | [diff] [blame] | 98 | priv->clk = devm_clk_get(&pdev->dev, NULL); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 99 | if (IS_ERR(priv->clk)) { |
| 100 | dev_err(&pdev->dev, "Failed to get clock\n"); |
| 101 | return PTR_ERR(priv->clk); |
| 102 | } |
| 103 | |
Fabio Estevam | 0475c94 | 2014-01-25 19:46:23 -0200 | [diff] [blame] | 104 | ret = clk_prepare_enable(priv->clk); |
| 105 | if (ret) |
| 106 | return ret; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 107 | |
| 108 | host = ata_host_alloc(&pdev->dev, 1); |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 109 | if (!host) { |
| 110 | ret = -ENOMEM; |
| 111 | goto err; |
| 112 | } |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 113 | |
| 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 Estevam | b314fc7 | 2014-02-17 19:05:56 -0300 | [diff] [blame] | 121 | io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 122 | priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res); |
Bartlomiej Zolnierkiewicz | 13e8e78 | 2014-03-14 18:33:13 +0100 | [diff] [blame] | 123 | if (IS_ERR(priv->host_regs)) { |
| 124 | ret = PTR_ERR(priv->host_regs); |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 125 | goto err; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 126 | } |
| 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 Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 148 | ret = ata_host_activate(host, irq, ata_sff_interrupt, 0, |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 149 | &pata_imx_sht); |
| 150 | |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 151 | if (ret) |
| 152 | goto err; |
| 153 | |
| 154 | return 0; |
| 155 | err: |
Fabio Estevam | a18dada | 2012-07-07 19:34:05 -0300 | [diff] [blame] | 156 | clk_disable_unprepare(priv->clk); |
Sascha Hauer | 50f5a34 | 2013-04-04 11:25:04 +0200 | [diff] [blame] | 157 | |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 158 | return ret; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Greg Kroah-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 161 | static int pata_imx_remove(struct platform_device *pdev) |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 162 | { |
Jingoo Han | d89995d | 2013-05-23 19:41:21 +0900 | [diff] [blame] | 163 | struct ata_host *host = platform_get_drvdata(pdev); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 164 | 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 Estevam | a18dada | 2012-07-07 19:34:05 -0300 | [diff] [blame] | 170 | clk_disable_unprepare(priv->clk); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Bartlomiej Zolnierkiewicz | 58eb8cd | 2014-05-07 17:17:44 +0200 | [diff] [blame] | 175 | #ifdef CONFIG_PM_SLEEP |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 176 | static 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 Estevam | a18dada | 2012-07-07 19:34:05 -0300 | [diff] [blame] | 187 | clk_disable_unprepare(priv->clk); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static 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 Estevam | 0475c94 | 2014-01-25 19:46:23 -0200 | [diff] [blame] | 198 | int ret = clk_prepare_enable(priv->clk); |
| 199 | if (ret) |
| 200 | return ret; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 201 | |
| 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) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 211 | #endif |
| 212 | |
Fabio Estevam | 36888e9 | 2014-10-04 09:03:06 -0300 | [diff] [blame] | 213 | static SIMPLE_DEV_PM_OPS(pata_imx_pm_ops, pata_imx_suspend, pata_imx_resume); |
| 214 | |
Sascha Hauer | b9d15db | 2013-04-04 11:25:07 +0200 | [diff] [blame] | 215 | static const struct of_device_id imx_pata_dt_ids[] = { |
| 216 | { |
| 217 | .compatible = "fsl,imx27-pata", |
| 218 | }, { |
| 219 | /* sentinel */ |
| 220 | } |
| 221 | }; |
Niels de Vos | 71ab1d5 | 2013-07-29 09:38:38 +0200 | [diff] [blame] | 222 | MODULE_DEVICE_TABLE(of, imx_pata_dt_ids); |
Sascha Hauer | b9d15db | 2013-04-04 11:25:07 +0200 | [diff] [blame] | 223 | |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 224 | static struct platform_driver pata_imx_driver = { |
| 225 | .probe = pata_imx_probe, |
Greg Kroah-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 226 | .remove = pata_imx_remove, |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 227 | .driver = { |
| 228 | .name = DRV_NAME, |
Sascha Hauer | b9d15db | 2013-04-04 11:25:07 +0200 | [diff] [blame] | 229 | .of_match_table = imx_pata_dt_ids, |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 230 | .pm = &pata_imx_pm_ops, |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 231 | }, |
| 232 | }; |
| 233 | |
Axel Lin | 99c8ea3 | 2011-11-27 14:44:26 +0800 | [diff] [blame] | 234 | module_platform_driver(pata_imx_driver); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 235 | |
| 236 | MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>"); |
| 237 | MODULE_DESCRIPTION("low-level driver for iMX PATA"); |
| 238 | MODULE_LICENSE("GPL"); |
| 239 | MODULE_ALIAS("platform:" DRV_NAME); |