ahci_imx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Freescale IMX AHCI SATA platform driver
  3. * Copyright 2013 Freescale Semiconductor, Inc.
  4. *
  5. * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/ahci_platform.h>
  24. #include <linux/of_device.h>
  25. #include <linux/mfd/syscon.h>
  26. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  27. #include "ahci.h"
  28. enum {
  29. HOST_TIMER1MS = 0xe0, /* Timer 1-ms */
  30. };
  31. struct imx_ahci_priv {
  32. struct platform_device *ahci_pdev;
  33. struct clk *sata_ref_clk;
  34. struct clk *ahb_clk;
  35. struct regmap *gpr;
  36. };
  37. static int imx6q_sata_init(struct device *dev, void __iomem *mmio)
  38. {
  39. int ret = 0;
  40. unsigned int reg_val;
  41. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  42. imxpriv->gpr =
  43. syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
  44. if (IS_ERR(imxpriv->gpr)) {
  45. dev_err(dev, "failed to find fsl,imx6q-iomux-gpr regmap\n");
  46. return PTR_ERR(imxpriv->gpr);
  47. }
  48. ret = clk_prepare_enable(imxpriv->sata_ref_clk);
  49. if (ret < 0) {
  50. dev_err(dev, "prepare-enable sata_ref clock err:%d\n", ret);
  51. return ret;
  52. }
  53. /*
  54. * set PHY Paremeters, two steps to configure the GPR13,
  55. * one write for rest of parameters, mask of first write
  56. * is 0x07fffffd, and the other one write for setting
  57. * the mpll_clk_en.
  58. */
  59. regmap_update_bits(imxpriv->gpr, 0x34, IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK
  60. | IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK
  61. | IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK
  62. | IMX6Q_GPR13_SATA_SPD_MODE_MASK
  63. | IMX6Q_GPR13_SATA_MPLL_SS_EN
  64. | IMX6Q_GPR13_SATA_TX_ATTEN_MASK
  65. | IMX6Q_GPR13_SATA_TX_BOOST_MASK
  66. | IMX6Q_GPR13_SATA_TX_LVL_MASK
  67. | IMX6Q_GPR13_SATA_TX_EDGE_RATE
  68. , IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB
  69. | IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M
  70. | IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F
  71. | IMX6Q_GPR13_SATA_SPD_MODE_3P0G
  72. | IMX6Q_GPR13_SATA_MPLL_SS_EN
  73. | IMX6Q_GPR13_SATA_TX_ATTEN_9_16
  74. | IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB
  75. | IMX6Q_GPR13_SATA_TX_LVL_1_025_V);
  76. regmap_update_bits(imxpriv->gpr, 0x34, IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  77. IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  78. usleep_range(100, 200);
  79. /*
  80. * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL,
  81. * and IP vendor specific register HOST_TIMER1MS.
  82. * Configure CAP_SSS (support stagered spin up).
  83. * Implement the port0.
  84. * Get the ahb clock rate, and configure the TIMER1MS register.
  85. */
  86. reg_val = readl(mmio + HOST_CAP);
  87. if (!(reg_val & HOST_CAP_SSS)) {
  88. reg_val |= HOST_CAP_SSS;
  89. writel(reg_val, mmio + HOST_CAP);
  90. }
  91. reg_val = readl(mmio + HOST_PORTS_IMPL);
  92. if (!(reg_val & 0x1)) {
  93. reg_val |= 0x1;
  94. writel(reg_val, mmio + HOST_PORTS_IMPL);
  95. }
  96. reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
  97. writel(reg_val, mmio + HOST_TIMER1MS);
  98. return 0;
  99. }
  100. static void imx6q_sata_exit(struct device *dev)
  101. {
  102. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  103. regmap_update_bits(imxpriv->gpr, 0x34, IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  104. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  105. clk_disable_unprepare(imxpriv->sata_ref_clk);
  106. }
  107. static struct ahci_platform_data imx6q_sata_pdata = {
  108. .init = imx6q_sata_init,
  109. .exit = imx6q_sata_exit,
  110. };
  111. static const struct of_device_id imx_ahci_of_match[] = {
  112. { .compatible = "fsl,imx6q-ahci", .data = &imx6q_sata_pdata},
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
  116. static int imx_ahci_probe(struct platform_device *pdev)
  117. {
  118. struct device *dev = &pdev->dev;
  119. struct resource *mem, *irq, res[2];
  120. const struct of_device_id *of_id;
  121. const struct ahci_platform_data *pdata = NULL;
  122. struct imx_ahci_priv *imxpriv;
  123. struct device *ahci_dev;
  124. struct platform_device *ahci_pdev;
  125. int ret;
  126. imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
  127. if (!imxpriv) {
  128. dev_err(dev, "can't alloc ahci_host_priv\n");
  129. return -ENOMEM;
  130. }
  131. ahci_pdev = platform_device_alloc("ahci", -1);
  132. if (!ahci_pdev)
  133. return -ENODEV;
  134. ahci_dev = &ahci_pdev->dev;
  135. ahci_dev->parent = dev;
  136. imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
  137. if (IS_ERR(imxpriv->ahb_clk)) {
  138. dev_err(dev, "can't get ahb clock.\n");
  139. ret = PTR_ERR(imxpriv->ahb_clk);
  140. goto err_out;
  141. }
  142. imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
  143. if (IS_ERR(imxpriv->sata_ref_clk)) {
  144. dev_err(dev, "can't get sata_ref clock.\n");
  145. ret = PTR_ERR(imxpriv->sata_ref_clk);
  146. goto err_out;
  147. }
  148. imxpriv->ahci_pdev = ahci_pdev;
  149. platform_set_drvdata(pdev, imxpriv);
  150. of_id = of_match_device(imx_ahci_of_match, dev);
  151. if (of_id) {
  152. pdata = of_id->data;
  153. } else {
  154. ret = -EINVAL;
  155. goto err_out;
  156. }
  157. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  159. if (!mem || !irq) {
  160. dev_err(dev, "no mmio/irq resource\n");
  161. ret = -ENOMEM;
  162. goto err_out;
  163. }
  164. res[0] = *mem;
  165. res[1] = *irq;
  166. ahci_dev->coherent_dma_mask = DMA_BIT_MASK(32);
  167. ahci_dev->dma_mask = &ahci_dev->coherent_dma_mask;
  168. ahci_dev->of_node = dev->of_node;
  169. ret = platform_device_add_resources(ahci_pdev, res, 2);
  170. if (ret)
  171. goto err_out;
  172. ret = platform_device_add_data(ahci_pdev, pdata, sizeof(*pdata));
  173. if (ret)
  174. goto err_out;
  175. ret = platform_device_add(ahci_pdev);
  176. if (ret) {
  177. err_out:
  178. platform_device_put(ahci_pdev);
  179. return ret;
  180. }
  181. return 0;
  182. }
  183. static int imx_ahci_remove(struct platform_device *pdev)
  184. {
  185. struct imx_ahci_priv *imxpriv = platform_get_drvdata(pdev);
  186. struct platform_device *ahci_pdev = imxpriv->ahci_pdev;
  187. platform_device_unregister(ahci_pdev);
  188. return 0;
  189. }
  190. static struct platform_driver imx_ahci_driver = {
  191. .probe = imx_ahci_probe,
  192. .remove = imx_ahci_remove,
  193. .driver = {
  194. .name = "ahci-imx",
  195. .owner = THIS_MODULE,
  196. .of_match_table = imx_ahci_of_match,
  197. },
  198. };
  199. module_platform_driver(imx_ahci_driver);
  200. MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
  201. MODULE_AUTHOR("Richard Zhu <Hong-Xing.Zhu@freescale.com>");
  202. MODULE_LICENSE("GPL");
  203. MODULE_ALIAS("ahci:imx");