pata_imx.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. */
  15. #include <linux/ata.h>
  16. #include <linux/clk.h>
  17. #include <linux/libata.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #define DRV_NAME "pata_imx"
  21. #define PATA_IMX_ATA_TIME_OFF 0x00
  22. #define PATA_IMX_ATA_TIME_ON 0x01
  23. #define PATA_IMX_ATA_TIME_1 0x02
  24. #define PATA_IMX_ATA_TIME_2W 0x03
  25. #define PATA_IMX_ATA_TIME_2R 0x04
  26. #define PATA_IMX_ATA_TIME_AX 0x05
  27. #define PATA_IMX_ATA_TIME_PIO_RDX 0x06
  28. #define PATA_IMX_ATA_TIME_4 0x07
  29. #define PATA_IMX_ATA_TIME_9 0x08
  30. #define PATA_IMX_ATA_CONTROL 0x24
  31. #define PATA_IMX_ATA_CTRL_FIFO_RST_B (1<<7)
  32. #define PATA_IMX_ATA_CTRL_ATA_RST_B (1<<6)
  33. #define PATA_IMX_ATA_CTRL_IORDY_EN (1<<0)
  34. #define PATA_IMX_ATA_INT_EN 0x2C
  35. #define PATA_IMX_ATA_INTR_ATA_INTRQ2 (1<<3)
  36. #define PATA_IMX_DRIVE_DATA 0xA0
  37. #define PATA_IMX_DRIVE_CONTROL 0xD8
  38. static u32 pio_t4[] = { 30, 20, 15, 10, 10 };
  39. static u32 pio_t9[] = { 20, 15, 10, 10, 10 };
  40. static u32 pio_tA[] = { 35, 35, 35, 35, 35 };
  41. struct pata_imx_priv {
  42. struct clk *clk;
  43. /* timings/interrupt/control regs */
  44. void __iomem *host_regs;
  45. u32 ata_ctl;
  46. };
  47. static void pata_imx_set_timing(struct ata_device *adev,
  48. struct pata_imx_priv *priv)
  49. {
  50. struct ata_timing timing;
  51. unsigned long clkrate;
  52. u32 T, mode;
  53. clkrate = clk_get_rate(priv->clk);
  54. if (adev->pio_mode < XFER_PIO_0 || adev->pio_mode > XFER_PIO_4 ||
  55. !clkrate)
  56. return;
  57. T = 1000000000 / clkrate;
  58. ata_timing_compute(adev, adev->pio_mode, &timing, T * 1000, 0);
  59. mode = adev->pio_mode - XFER_PIO_0;
  60. writeb(3, priv->host_regs + PATA_IMX_ATA_TIME_OFF);
  61. writeb(3, priv->host_regs + PATA_IMX_ATA_TIME_ON);
  62. writeb(timing.setup, priv->host_regs + PATA_IMX_ATA_TIME_1);
  63. writeb(timing.act8b, priv->host_regs + PATA_IMX_ATA_TIME_2W);
  64. writeb(timing.act8b, priv->host_regs + PATA_IMX_ATA_TIME_2R);
  65. writeb(1, priv->host_regs + PATA_IMX_ATA_TIME_PIO_RDX);
  66. writeb(pio_t4[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_4);
  67. writeb(pio_t9[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_9);
  68. writeb(pio_tA[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_AX);
  69. }
  70. static void pata_imx_set_piomode(struct ata_port *ap, struct ata_device *adev)
  71. {
  72. struct pata_imx_priv *priv = ap->host->private_data;
  73. u32 val;
  74. pata_imx_set_timing(adev, priv);
  75. val = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
  76. if (ata_pio_need_iordy(adev))
  77. val |= PATA_IMX_ATA_CTRL_IORDY_EN;
  78. else
  79. val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
  80. __raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
  81. }
  82. static struct scsi_host_template pata_imx_sht = {
  83. ATA_PIO_SHT(DRV_NAME),
  84. };
  85. static struct ata_port_operations pata_imx_port_ops = {
  86. .inherits = &ata_sff_port_ops,
  87. .sff_data_xfer = ata_sff_data_xfer_noirq,
  88. .cable_detect = ata_cable_unknown,
  89. .set_piomode = pata_imx_set_piomode,
  90. };
  91. static void pata_imx_setup_port(struct ata_ioports *ioaddr)
  92. {
  93. /* Fixup the port shift for platforms that need it */
  94. ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
  95. ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
  96. ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
  97. ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
  98. ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
  99. ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
  100. ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
  101. ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
  102. ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
  103. ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
  104. }
  105. static int pata_imx_probe(struct platform_device *pdev)
  106. {
  107. struct ata_host *host;
  108. struct ata_port *ap;
  109. struct pata_imx_priv *priv;
  110. int irq = 0;
  111. struct resource *io_res;
  112. int ret;
  113. irq = platform_get_irq(pdev, 0);
  114. if (irq < 0)
  115. return irq;
  116. priv = devm_kzalloc(&pdev->dev,
  117. sizeof(struct pata_imx_priv), GFP_KERNEL);
  118. if (!priv)
  119. return -ENOMEM;
  120. priv->clk = devm_clk_get(&pdev->dev, NULL);
  121. if (IS_ERR(priv->clk)) {
  122. dev_err(&pdev->dev, "Failed to get clock\n");
  123. return PTR_ERR(priv->clk);
  124. }
  125. ret = clk_prepare_enable(priv->clk);
  126. if (ret)
  127. return ret;
  128. host = ata_host_alloc(&pdev->dev, 1);
  129. if (!host) {
  130. ret = -ENOMEM;
  131. goto err;
  132. }
  133. host->private_data = priv;
  134. ap = host->ports[0];
  135. ap->ops = &pata_imx_port_ops;
  136. ap->pio_mask = ATA_PIO4;
  137. ap->flags |= ATA_FLAG_SLAVE_POSS;
  138. io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  139. priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res);
  140. if (IS_ERR(priv->host_regs)) {
  141. ret = PTR_ERR(priv->host_regs);
  142. goto err;
  143. }
  144. ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
  145. ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
  146. ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  147. 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);
  151. /* deassert resets */
  152. __raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
  153. PATA_IMX_ATA_CTRL_ATA_RST_B,
  154. priv->host_regs + PATA_IMX_ATA_CONTROL);
  155. /* enable interrupts */
  156. __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
  157. priv->host_regs + PATA_IMX_ATA_INT_EN);
  158. /* activate */
  159. ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
  160. &pata_imx_sht);
  161. if (ret)
  162. goto err;
  163. return 0;
  164. err:
  165. clk_disable_unprepare(priv->clk);
  166. return ret;
  167. }
  168. static int pata_imx_remove(struct platform_device *pdev)
  169. {
  170. struct ata_host *host = platform_get_drvdata(pdev);
  171. struct pata_imx_priv *priv = host->private_data;
  172. ata_host_detach(host);
  173. __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
  174. clk_disable_unprepare(priv->clk);
  175. return 0;
  176. }
  177. #ifdef CONFIG_PM_SLEEP
  178. static int pata_imx_suspend(struct device *dev)
  179. {
  180. struct ata_host *host = dev_get_drvdata(dev);
  181. struct pata_imx_priv *priv = host->private_data;
  182. int ret;
  183. ret = ata_host_suspend(host, PMSG_SUSPEND);
  184. if (!ret) {
  185. __raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
  186. priv->ata_ctl =
  187. __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
  188. clk_disable_unprepare(priv->clk);
  189. }
  190. return ret;
  191. }
  192. static int pata_imx_resume(struct device *dev)
  193. {
  194. struct ata_host *host = dev_get_drvdata(dev);
  195. struct pata_imx_priv *priv = host->private_data;
  196. int ret = clk_prepare_enable(priv->clk);
  197. if (ret)
  198. return ret;
  199. __raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
  200. __raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
  201. priv->host_regs + PATA_IMX_ATA_INT_EN);
  202. ata_host_resume(host);
  203. return 0;
  204. }
  205. #endif
  206. static SIMPLE_DEV_PM_OPS(pata_imx_pm_ops, pata_imx_suspend, pata_imx_resume);
  207. static const struct of_device_id imx_pata_dt_ids[] = {
  208. {
  209. .compatible = "fsl,imx27-pata",
  210. }, {
  211. /* sentinel */
  212. }
  213. };
  214. MODULE_DEVICE_TABLE(of, imx_pata_dt_ids);
  215. static struct platform_driver pata_imx_driver = {
  216. .probe = pata_imx_probe,
  217. .remove = pata_imx_remove,
  218. .driver = {
  219. .name = DRV_NAME,
  220. .of_match_table = imx_pata_dt_ids,
  221. .pm = &pata_imx_pm_ops,
  222. },
  223. };
  224. module_platform_driver(pata_imx_driver);
  225. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
  226. MODULE_DESCRIPTION("low-level driver for iMX PATA");
  227. MODULE_LICENSE("GPL");
  228. MODULE_ALIAS("platform:" DRV_NAME);