ahci_imx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * copyright (c) 2013 Freescale Semiconductor, Inc.
  3. * Freescale IMX AHCI SATA platform driver
  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 <linux/libata.h>
  28. #include "ahci.h"
  29. enum {
  30. PORT_PHY_CTL = 0x178, /* Port0 PHY Control */
  31. PORT_PHY_CTL_PDDQ_LOC = 0x100000, /* PORT_PHY_CTL bits */
  32. HOST_TIMER1MS = 0xe0, /* Timer 1-ms */
  33. };
  34. struct imx_ahci_priv {
  35. struct platform_device *ahci_pdev;
  36. struct clk *sata_ref_clk;
  37. struct clk *ahb_clk;
  38. struct regmap *gpr;
  39. bool no_device;
  40. bool first_time;
  41. };
  42. static int ahci_imx_hotplug;
  43. module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
  44. MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
  45. static void ahci_imx_error_handler(struct ata_port *ap)
  46. {
  47. u32 reg_val;
  48. struct ata_device *dev;
  49. struct ata_host *host = dev_get_drvdata(ap->dev);
  50. struct ahci_host_priv *hpriv = host->private_data;
  51. void __iomem *mmio = hpriv->mmio;
  52. struct imx_ahci_priv *imxpriv = dev_get_drvdata(ap->dev->parent);
  53. ahci_error_handler(ap);
  54. if (!(imxpriv->first_time) || ahci_imx_hotplug)
  55. return;
  56. imxpriv->first_time = false;
  57. ata_for_each_dev(dev, &ap->link, ENABLED)
  58. return;
  59. /*
  60. * Disable link to save power. An imx ahci port can't be recovered
  61. * without full reset once the pddq mode is enabled making it
  62. * impossible to use as part of libata LPM.
  63. */
  64. reg_val = readl(mmio + PORT_PHY_CTL);
  65. writel(reg_val | PORT_PHY_CTL_PDDQ_LOC, mmio + PORT_PHY_CTL);
  66. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  67. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  68. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  69. clk_disable_unprepare(imxpriv->sata_ref_clk);
  70. imxpriv->no_device = true;
  71. }
  72. static struct ata_port_operations ahci_imx_ops = {
  73. .inherits = &ahci_platform_ops,
  74. .error_handler = ahci_imx_error_handler,
  75. };
  76. static const struct ata_port_info ahci_imx_port_info = {
  77. .flags = AHCI_FLAG_COMMON,
  78. .pio_mask = ATA_PIO4,
  79. .udma_mask = ATA_UDMA6,
  80. .port_ops = &ahci_imx_ops,
  81. };
  82. static int imx6q_sata_init(struct device *dev, void __iomem *mmio)
  83. {
  84. int ret = 0;
  85. unsigned int reg_val;
  86. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  87. imxpriv->gpr =
  88. syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
  89. if (IS_ERR(imxpriv->gpr)) {
  90. dev_err(dev, "failed to find fsl,imx6q-iomux-gpr regmap\n");
  91. return PTR_ERR(imxpriv->gpr);
  92. }
  93. ret = clk_prepare_enable(imxpriv->sata_ref_clk);
  94. if (ret < 0) {
  95. dev_err(dev, "prepare-enable sata_ref clock err:%d\n", ret);
  96. return ret;
  97. }
  98. /*
  99. * set PHY Paremeters, two steps to configure the GPR13,
  100. * one write for rest of parameters, mask of first write
  101. * is 0x07fffffd, and the other one write for setting
  102. * the mpll_clk_en.
  103. */
  104. regmap_update_bits(imxpriv->gpr, 0x34, IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK
  105. | IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK
  106. | IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK
  107. | IMX6Q_GPR13_SATA_SPD_MODE_MASK
  108. | IMX6Q_GPR13_SATA_MPLL_SS_EN
  109. | IMX6Q_GPR13_SATA_TX_ATTEN_MASK
  110. | IMX6Q_GPR13_SATA_TX_BOOST_MASK
  111. | IMX6Q_GPR13_SATA_TX_LVL_MASK
  112. | IMX6Q_GPR13_SATA_TX_EDGE_RATE
  113. , IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB
  114. | IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M
  115. | IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F
  116. | IMX6Q_GPR13_SATA_SPD_MODE_3P0G
  117. | IMX6Q_GPR13_SATA_MPLL_SS_EN
  118. | IMX6Q_GPR13_SATA_TX_ATTEN_9_16
  119. | IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB
  120. | IMX6Q_GPR13_SATA_TX_LVL_1_025_V);
  121. regmap_update_bits(imxpriv->gpr, 0x34, IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  122. IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  123. usleep_range(100, 200);
  124. /*
  125. * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL,
  126. * and IP vendor specific register HOST_TIMER1MS.
  127. * Configure CAP_SSS (support stagered spin up).
  128. * Implement the port0.
  129. * Get the ahb clock rate, and configure the TIMER1MS register.
  130. */
  131. reg_val = readl(mmio + HOST_CAP);
  132. if (!(reg_val & HOST_CAP_SSS)) {
  133. reg_val |= HOST_CAP_SSS;
  134. writel(reg_val, mmio + HOST_CAP);
  135. }
  136. reg_val = readl(mmio + HOST_PORTS_IMPL);
  137. if (!(reg_val & 0x1)) {
  138. reg_val |= 0x1;
  139. writel(reg_val, mmio + HOST_PORTS_IMPL);
  140. }
  141. reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
  142. writel(reg_val, mmio + HOST_TIMER1MS);
  143. return 0;
  144. }
  145. static void imx6q_sata_exit(struct device *dev)
  146. {
  147. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  148. regmap_update_bits(imxpriv->gpr, 0x34, IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  149. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  150. clk_disable_unprepare(imxpriv->sata_ref_clk);
  151. }
  152. static int imx_ahci_suspend(struct device *dev)
  153. {
  154. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  155. /*
  156. * If no_device is set, The CLKs had been gated off in the
  157. * initialization so don't do it again here.
  158. */
  159. if (!imxpriv->no_device) {
  160. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  161. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  162. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  163. clk_disable_unprepare(imxpriv->sata_ref_clk);
  164. }
  165. return 0;
  166. }
  167. static int imx_ahci_resume(struct device *dev)
  168. {
  169. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  170. int ret;
  171. if (!imxpriv->no_device) {
  172. ret = clk_prepare_enable(imxpriv->sata_ref_clk);
  173. if (ret < 0) {
  174. dev_err(dev, "pre-enable sata_ref clock err:%d\n", ret);
  175. return ret;
  176. }
  177. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  178. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  179. IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  180. usleep_range(1000, 2000);
  181. }
  182. return 0;
  183. }
  184. static struct ahci_platform_data imx6q_sata_pdata = {
  185. .init = imx6q_sata_init,
  186. .exit = imx6q_sata_exit,
  187. .ata_port_info = &ahci_imx_port_info,
  188. .suspend = imx_ahci_suspend,
  189. .resume = imx_ahci_resume,
  190. };
  191. static const struct of_device_id imx_ahci_of_match[] = {
  192. { .compatible = "fsl,imx6q-ahci", .data = &imx6q_sata_pdata},
  193. {},
  194. };
  195. MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
  196. static int imx_ahci_probe(struct platform_device *pdev)
  197. {
  198. struct device *dev = &pdev->dev;
  199. struct resource *mem, *irq, res[2];
  200. const struct of_device_id *of_id;
  201. const struct ahci_platform_data *pdata = NULL;
  202. struct imx_ahci_priv *imxpriv;
  203. struct device *ahci_dev;
  204. struct platform_device *ahci_pdev;
  205. int ret;
  206. imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
  207. if (!imxpriv) {
  208. dev_err(dev, "can't alloc ahci_host_priv\n");
  209. return -ENOMEM;
  210. }
  211. ahci_pdev = platform_device_alloc("ahci", -1);
  212. if (!ahci_pdev)
  213. return -ENODEV;
  214. ahci_dev = &ahci_pdev->dev;
  215. ahci_dev->parent = dev;
  216. imxpriv->no_device = false;
  217. imxpriv->first_time = true;
  218. imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
  219. if (IS_ERR(imxpriv->ahb_clk)) {
  220. dev_err(dev, "can't get ahb clock.\n");
  221. ret = PTR_ERR(imxpriv->ahb_clk);
  222. goto err_out;
  223. }
  224. imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
  225. if (IS_ERR(imxpriv->sata_ref_clk)) {
  226. dev_err(dev, "can't get sata_ref clock.\n");
  227. ret = PTR_ERR(imxpriv->sata_ref_clk);
  228. goto err_out;
  229. }
  230. imxpriv->ahci_pdev = ahci_pdev;
  231. platform_set_drvdata(pdev, imxpriv);
  232. of_id = of_match_device(imx_ahci_of_match, dev);
  233. if (of_id) {
  234. pdata = of_id->data;
  235. } else {
  236. ret = -EINVAL;
  237. goto err_out;
  238. }
  239. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  240. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  241. if (!mem || !irq) {
  242. dev_err(dev, "no mmio/irq resource\n");
  243. ret = -ENOMEM;
  244. goto err_out;
  245. }
  246. res[0] = *mem;
  247. res[1] = *irq;
  248. ahci_dev->coherent_dma_mask = DMA_BIT_MASK(32);
  249. ahci_dev->dma_mask = &ahci_dev->coherent_dma_mask;
  250. ahci_dev->of_node = dev->of_node;
  251. ret = platform_device_add_resources(ahci_pdev, res, 2);
  252. if (ret)
  253. goto err_out;
  254. ret = platform_device_add_data(ahci_pdev, pdata, sizeof(*pdata));
  255. if (ret)
  256. goto err_out;
  257. ret = platform_device_add(ahci_pdev);
  258. if (ret) {
  259. err_out:
  260. platform_device_put(ahci_pdev);
  261. return ret;
  262. }
  263. return 0;
  264. }
  265. static int imx_ahci_remove(struct platform_device *pdev)
  266. {
  267. struct imx_ahci_priv *imxpriv = platform_get_drvdata(pdev);
  268. struct platform_device *ahci_pdev = imxpriv->ahci_pdev;
  269. platform_device_unregister(ahci_pdev);
  270. return 0;
  271. }
  272. static struct platform_driver imx_ahci_driver = {
  273. .probe = imx_ahci_probe,
  274. .remove = imx_ahci_remove,
  275. .driver = {
  276. .name = "ahci-imx",
  277. .owner = THIS_MODULE,
  278. .of_match_table = imx_ahci_of_match,
  279. },
  280. };
  281. module_platform_driver(imx_ahci_driver);
  282. MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
  283. MODULE_AUTHOR("Richard Zhu <Hong-Xing.Zhu@freescale.com>");
  284. MODULE_LICENSE("GPL");
  285. MODULE_ALIAS("ahci:imx");