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 | */ |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 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 | |
| 36 | struct pata_imx_priv { |
| 37 | struct clk *clk; |
| 38 | /* timings/interrupt/control regs */ |
Sascha Hauer | 51b5539 | 2013-04-04 11:25:06 +0200 | [diff] [blame] | 39 | void __iomem *host_regs; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 40 | u32 ata_ctl; |
| 41 | }; |
| 42 | |
| 43 | static 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 | |
Wei Yongjun | 22c8be3 | 2012-12-03 23:38:58 -0500 | [diff] [blame] | 62 | ata_dev_info(dev, "configured for PIO\n"); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static struct scsi_host_template pata_imx_sht = { |
| 68 | ATA_PIO_SHT(DRV_NAME), |
| 69 | }; |
| 70 | |
| 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, |
| 76 | }; |
| 77 | |
| 78 | static void pata_imx_setup_port(struct ata_ioports *ioaddr) |
| 79 | { |
| 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); |
| 91 | } |
| 92 | |
Greg Kroah-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 93 | static int pata_imx_probe(struct platform_device *pdev) |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 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; |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 100 | int ret; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 101 | |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 102 | irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 3ef9cc3 | 2014-02-17 19:05:57 -0300 | [diff] [blame] | 103 | if (irq < 0) |
| 104 | return irq; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 105 | |
| 106 | priv = devm_kzalloc(&pdev->dev, |
| 107 | sizeof(struct pata_imx_priv), GFP_KERNEL); |
| 108 | if (!priv) |
| 109 | return -ENOMEM; |
| 110 | |
Sascha Hauer | 50f5a34 | 2013-04-04 11:25:04 +0200 | [diff] [blame] | 111 | priv->clk = devm_clk_get(&pdev->dev, NULL); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 112 | if (IS_ERR(priv->clk)) { |
| 113 | dev_err(&pdev->dev, "Failed to get clock\n"); |
| 114 | return PTR_ERR(priv->clk); |
| 115 | } |
| 116 | |
Fabio Estevam | a18dada | 2012-07-07 19:34:05 -0300 | [diff] [blame] | 117 | clk_prepare_enable(priv->clk); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 118 | |
| 119 | host = ata_host_alloc(&pdev->dev, 1); |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 120 | if (!host) { |
| 121 | ret = -ENOMEM; |
| 122 | goto err; |
| 123 | } |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 124 | |
| 125 | host->private_data = priv; |
| 126 | ap = host->ports[0]; |
| 127 | |
| 128 | ap->ops = &pata_imx_port_ops; |
| 129 | ap->pio_mask = ATA_PIO0; |
| 130 | ap->flags |= ATA_FLAG_SLAVE_POSS; |
| 131 | |
Fabio Estevam | b314fc7 | 2014-02-17 19:05:56 -0300 | [diff] [blame] | 132 | io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 133 | priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res); |
Bartlomiej Zolnierkiewicz | 13e8e78 | 2014-03-14 18:33:13 +0100 | [diff] [blame^] | 134 | if (IS_ERR(priv->host_regs)) { |
| 135 | ret = PTR_ERR(priv->host_regs); |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 136 | goto err; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA; |
| 140 | ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL; |
| 141 | |
| 142 | ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr; |
| 143 | |
| 144 | pata_imx_setup_port(&ap->ioaddr); |
| 145 | |
| 146 | ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx", |
| 147 | (unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA, |
| 148 | (unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL); |
| 149 | |
| 150 | /* deassert resets */ |
| 151 | __raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B | |
| 152 | PATA_IMX_ATA_CTRL_ATA_RST_B, |
| 153 | priv->host_regs + PATA_IMX_ATA_CONTROL); |
| 154 | /* enable interrupts */ |
| 155 | __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2, |
| 156 | priv->host_regs + PATA_IMX_ATA_INT_EN); |
| 157 | |
| 158 | /* activate */ |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 159 | ret = ata_host_activate(host, irq, ata_sff_interrupt, 0, |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 160 | &pata_imx_sht); |
| 161 | |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 162 | if (ret) |
| 163 | goto err; |
| 164 | |
| 165 | return 0; |
| 166 | err: |
Fabio Estevam | a18dada | 2012-07-07 19:34:05 -0300 | [diff] [blame] | 167 | clk_disable_unprepare(priv->clk); |
Sascha Hauer | 50f5a34 | 2013-04-04 11:25:04 +0200 | [diff] [blame] | 168 | |
Sascha Hauer | ff540d0 | 2013-04-04 11:25:05 +0200 | [diff] [blame] | 169 | return ret; |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Greg Kroah-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 172 | static int pata_imx_remove(struct platform_device *pdev) |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 173 | { |
Jingoo Han | d89995d | 2013-05-23 19:41:21 +0900 | [diff] [blame] | 174 | struct ata_host *host = platform_get_drvdata(pdev); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 175 | struct pata_imx_priv *priv = host->private_data; |
| 176 | |
| 177 | ata_host_detach(host); |
| 178 | |
| 179 | __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN); |
| 180 | |
Fabio Estevam | a18dada | 2012-07-07 19:34:05 -0300 | [diff] [blame] | 181 | clk_disable_unprepare(priv->clk); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | #ifdef CONFIG_PM |
| 187 | static int pata_imx_suspend(struct device *dev) |
| 188 | { |
| 189 | struct ata_host *host = dev_get_drvdata(dev); |
| 190 | struct pata_imx_priv *priv = host->private_data; |
| 191 | int ret; |
| 192 | |
| 193 | ret = ata_host_suspend(host, PMSG_SUSPEND); |
| 194 | if (!ret) { |
| 195 | __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN); |
| 196 | priv->ata_ctl = |
| 197 | __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL); |
Fabio Estevam | a18dada | 2012-07-07 19:34:05 -0300 | [diff] [blame] | 198 | clk_disable_unprepare(priv->clk); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static int pata_imx_resume(struct device *dev) |
| 205 | { |
| 206 | struct ata_host *host = dev_get_drvdata(dev); |
| 207 | struct pata_imx_priv *priv = host->private_data; |
| 208 | |
Fabio Estevam | a18dada | 2012-07-07 19:34:05 -0300 | [diff] [blame] | 209 | clk_prepare_enable(priv->clk); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 210 | |
| 211 | __raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL); |
| 212 | |
| 213 | __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2, |
| 214 | priv->host_regs + PATA_IMX_ATA_INT_EN); |
| 215 | |
| 216 | ata_host_resume(host); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static const struct dev_pm_ops pata_imx_pm_ops = { |
| 222 | .suspend = pata_imx_suspend, |
| 223 | .resume = pata_imx_resume, |
| 224 | }; |
| 225 | #endif |
| 226 | |
Sascha Hauer | b9d15db | 2013-04-04 11:25:07 +0200 | [diff] [blame] | 227 | static const struct of_device_id imx_pata_dt_ids[] = { |
| 228 | { |
| 229 | .compatible = "fsl,imx27-pata", |
| 230 | }, { |
| 231 | /* sentinel */ |
| 232 | } |
| 233 | }; |
Niels de Vos | 71ab1d5 | 2013-07-29 09:38:38 +0200 | [diff] [blame] | 234 | MODULE_DEVICE_TABLE(of, imx_pata_dt_ids); |
Sascha Hauer | b9d15db | 2013-04-04 11:25:07 +0200 | [diff] [blame] | 235 | |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 236 | static struct platform_driver pata_imx_driver = { |
| 237 | .probe = pata_imx_probe, |
Greg Kroah-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 238 | .remove = pata_imx_remove, |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 239 | .driver = { |
| 240 | .name = DRV_NAME, |
Sascha Hauer | b9d15db | 2013-04-04 11:25:07 +0200 | [diff] [blame] | 241 | .of_match_table = imx_pata_dt_ids, |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 242 | .owner = THIS_MODULE, |
| 243 | #ifdef CONFIG_PM |
| 244 | .pm = &pata_imx_pm_ops, |
| 245 | #endif |
| 246 | }, |
| 247 | }; |
| 248 | |
Axel Lin | 99c8ea3 | 2011-11-27 14:44:26 +0800 | [diff] [blame] | 249 | module_platform_driver(pata_imx_driver); |
Arnaud Patard (Rtp) | e39c75c | 2011-07-26 16:58:19 +0200 | [diff] [blame] | 250 | |
| 251 | MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>"); |
| 252 | MODULE_DESCRIPTION("low-level driver for iMX PATA"); |
| 253 | MODULE_LICENSE("GPL"); |
| 254 | MODULE_ALIAS("platform:" DRV_NAME); |