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