pata_imx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/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. #define DRV_NAME "pata_imx"
  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. struct pata_imx_priv {
  34. struct clk *clk;
  35. /* timings/interrupt/control regs */
  36. void __iomem *host_regs;
  37. u32 ata_ctl;
  38. };
  39. static int pata_imx_set_mode(struct ata_link *link, struct ata_device **unused)
  40. {
  41. struct ata_device *dev;
  42. struct ata_port *ap = link->ap;
  43. struct pata_imx_priv *priv = ap->host->private_data;
  44. u32 val;
  45. ata_for_each_dev(dev, link, ENABLED) {
  46. dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
  47. dev->xfer_shift = ATA_SHIFT_PIO;
  48. dev->flags |= ATA_DFLAG_PIO;
  49. val = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
  50. if (ata_pio_need_iordy(dev))
  51. val |= PATA_IMX_ATA_CTRL_IORDY_EN;
  52. else
  53. val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
  54. __raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
  55. ata_dev_info(dev, "configured for PIO\n");
  56. }
  57. return 0;
  58. }
  59. static struct scsi_host_template pata_imx_sht = {
  60. ATA_PIO_SHT(DRV_NAME),
  61. };
  62. static struct ata_port_operations pata_imx_port_ops = {
  63. .inherits = &ata_sff_port_ops,
  64. .sff_data_xfer = ata_sff_data_xfer_noirq,
  65. .cable_detect = ata_cable_unknown,
  66. .set_mode = pata_imx_set_mode,
  67. };
  68. static void pata_imx_setup_port(struct ata_ioports *ioaddr)
  69. {
  70. /* Fixup the port shift for platforms that need it */
  71. ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
  72. ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
  73. ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
  74. ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
  75. ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
  76. ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
  77. ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
  78. ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
  79. ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
  80. ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
  81. }
  82. static int pata_imx_probe(struct platform_device *pdev)
  83. {
  84. struct ata_host *host;
  85. struct ata_port *ap;
  86. struct pata_imx_priv *priv;
  87. int irq = 0;
  88. struct resource *io_res;
  89. int ret;
  90. irq = platform_get_irq(pdev, 0);
  91. if (irq < 0)
  92. return irq;
  93. priv = devm_kzalloc(&pdev->dev,
  94. sizeof(struct pata_imx_priv), GFP_KERNEL);
  95. if (!priv)
  96. return -ENOMEM;
  97. priv->clk = devm_clk_get(&pdev->dev, NULL);
  98. if (IS_ERR(priv->clk)) {
  99. dev_err(&pdev->dev, "Failed to get clock\n");
  100. return PTR_ERR(priv->clk);
  101. }
  102. ret = clk_prepare_enable(priv->clk);
  103. if (ret)
  104. return ret;
  105. host = ata_host_alloc(&pdev->dev, 1);
  106. if (!host) {
  107. ret = -ENOMEM;
  108. goto err;
  109. }
  110. host->private_data = priv;
  111. ap = host->ports[0];
  112. ap->ops = &pata_imx_port_ops;
  113. ap->pio_mask = ATA_PIO0;
  114. ap->flags |= ATA_FLAG_SLAVE_POSS;
  115. io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  116. priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res);
  117. if (IS_ERR(priv->host_regs)) {
  118. ret = PTR_ERR(priv->host_regs);
  119. goto err;
  120. }
  121. ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
  122. ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
  123. ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  124. pata_imx_setup_port(&ap->ioaddr);
  125. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  126. (unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA,
  127. (unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL);
  128. /* deassert resets */
  129. __raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
  130. PATA_IMX_ATA_CTRL_ATA_RST_B,
  131. priv->host_regs + PATA_IMX_ATA_CONTROL);
  132. /* enable interrupts */
  133. __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
  134. priv->host_regs + PATA_IMX_ATA_INT_EN);
  135. /* activate */
  136. ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
  137. &pata_imx_sht);
  138. if (ret)
  139. goto err;
  140. return 0;
  141. err:
  142. clk_disable_unprepare(priv->clk);
  143. return ret;
  144. }
  145. static int pata_imx_remove(struct platform_device *pdev)
  146. {
  147. struct ata_host *host = platform_get_drvdata(pdev);
  148. struct pata_imx_priv *priv = host->private_data;
  149. ata_host_detach(host);
  150. __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
  151. clk_disable_unprepare(priv->clk);
  152. return 0;
  153. }
  154. #ifdef CONFIG_PM_SLEEP
  155. static int pata_imx_suspend(struct device *dev)
  156. {
  157. struct ata_host *host = dev_get_drvdata(dev);
  158. struct pata_imx_priv *priv = host->private_data;
  159. int ret;
  160. ret = ata_host_suspend(host, PMSG_SUSPEND);
  161. if (!ret) {
  162. __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
  163. priv->ata_ctl =
  164. __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
  165. clk_disable_unprepare(priv->clk);
  166. }
  167. return ret;
  168. }
  169. static int pata_imx_resume(struct device *dev)
  170. {
  171. struct ata_host *host = dev_get_drvdata(dev);
  172. struct pata_imx_priv *priv = host->private_data;
  173. int ret = clk_prepare_enable(priv->clk);
  174. if (ret)
  175. return ret;
  176. __raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
  177. __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
  178. priv->host_regs + PATA_IMX_ATA_INT_EN);
  179. ata_host_resume(host);
  180. return 0;
  181. }
  182. #endif
  183. static SIMPLE_DEV_PM_OPS(pata_imx_pm_ops, pata_imx_suspend, pata_imx_resume);
  184. static const struct of_device_id imx_pata_dt_ids[] = {
  185. {
  186. .compatible = "fsl,imx27-pata",
  187. }, {
  188. /* sentinel */
  189. }
  190. };
  191. MODULE_DEVICE_TABLE(of, imx_pata_dt_ids);
  192. static struct platform_driver pata_imx_driver = {
  193. .probe = pata_imx_probe,
  194. .remove = pata_imx_remove,
  195. .driver = {
  196. .name = DRV_NAME,
  197. .of_match_table = imx_pata_dt_ids,
  198. .pm = &pata_imx_pm_ops,
  199. },
  200. };
  201. module_platform_driver(pata_imx_driver);
  202. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
  203. MODULE_DESCRIPTION("low-level driver for iMX PATA");
  204. MODULE_LICENSE("GPL");
  205. MODULE_ALIAS("platform:" DRV_NAME);