ci_hdrc_msm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/usb/chipidea.h>
  11. #include <linux/clk.h>
  12. #include <linux/reset.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/regmap.h>
  15. #include <linux/io.h>
  16. #include <linux/reset-controller.h>
  17. #include <linux/extcon.h>
  18. #include <linux/of.h>
  19. #include "ci.h"
  20. #define HS_PHY_AHB_MODE 0x0098
  21. #define HS_PHY_GENCONFIG 0x009c
  22. #define HS_PHY_TXFIFO_IDLE_FORCE_DIS BIT(4)
  23. #define HS_PHY_GENCONFIG_2 0x00a0
  24. #define HS_PHY_SESS_VLD_CTRL_EN BIT(7)
  25. #define HS_PHY_ULPI_TX_PKT_EN_CLR_FIX BIT(19)
  26. #define HSPHY_SESS_VLD_CTRL BIT(25)
  27. /* Vendor base starts at 0x200 beyond CI base */
  28. #define HS_PHY_CTRL 0x0040
  29. #define HS_PHY_SEC_CTRL 0x0078
  30. #define HS_PHY_DIG_CLAMP_N BIT(16)
  31. #define HS_PHY_POR_ASSERT BIT(0)
  32. struct ci_hdrc_msm {
  33. struct platform_device *ci;
  34. struct clk *core_clk;
  35. struct clk *iface_clk;
  36. struct clk *fs_clk;
  37. struct ci_hdrc_platform_data pdata;
  38. struct reset_controller_dev rcdev;
  39. bool secondary_phy;
  40. bool hsic;
  41. void __iomem *base;
  42. };
  43. static int
  44. ci_hdrc_msm_por_reset(struct reset_controller_dev *r, unsigned long id)
  45. {
  46. struct ci_hdrc_msm *ci_msm = container_of(r, struct ci_hdrc_msm, rcdev);
  47. void __iomem *addr = ci_msm->base;
  48. u32 val;
  49. if (id)
  50. addr += HS_PHY_SEC_CTRL;
  51. else
  52. addr += HS_PHY_CTRL;
  53. val = readl_relaxed(addr);
  54. val |= HS_PHY_POR_ASSERT;
  55. writel(val, addr);
  56. /*
  57. * wait for minimum 10 microseconds as suggested by manual.
  58. * Use a slightly larger value since the exact value didn't
  59. * work 100% of the time.
  60. */
  61. udelay(12);
  62. val &= ~HS_PHY_POR_ASSERT;
  63. writel(val, addr);
  64. return 0;
  65. }
  66. static const struct reset_control_ops ci_hdrc_msm_reset_ops = {
  67. .reset = ci_hdrc_msm_por_reset,
  68. };
  69. static int ci_hdrc_msm_notify_event(struct ci_hdrc *ci, unsigned event)
  70. {
  71. struct device *dev = ci->dev->parent;
  72. struct ci_hdrc_msm *msm_ci = dev_get_drvdata(dev);
  73. int ret;
  74. switch (event) {
  75. case CI_HDRC_CONTROLLER_RESET_EVENT:
  76. dev_dbg(dev, "CI_HDRC_CONTROLLER_RESET_EVENT received\n");
  77. hw_phymode_configure(ci);
  78. if (msm_ci->secondary_phy) {
  79. u32 val = readl_relaxed(msm_ci->base + HS_PHY_SEC_CTRL);
  80. val |= HS_PHY_DIG_CLAMP_N;
  81. writel_relaxed(val, msm_ci->base + HS_PHY_SEC_CTRL);
  82. }
  83. ret = phy_init(ci->phy);
  84. if (ret)
  85. return ret;
  86. ret = phy_power_on(ci->phy);
  87. if (ret) {
  88. phy_exit(ci->phy);
  89. return ret;
  90. }
  91. /* use AHB transactor, allow posted data writes */
  92. hw_write_id_reg(ci, HS_PHY_AHB_MODE, 0xffffffff, 0x8);
  93. /* workaround for rx buffer collision issue */
  94. hw_write_id_reg(ci, HS_PHY_GENCONFIG,
  95. HS_PHY_TXFIFO_IDLE_FORCE_DIS, 0);
  96. if (!msm_ci->hsic)
  97. hw_write_id_reg(ci, HS_PHY_GENCONFIG_2,
  98. HS_PHY_ULPI_TX_PKT_EN_CLR_FIX, 0);
  99. if (!IS_ERR(ci->platdata->vbus_extcon.edev)) {
  100. hw_write_id_reg(ci, HS_PHY_GENCONFIG_2,
  101. HS_PHY_SESS_VLD_CTRL_EN,
  102. HS_PHY_SESS_VLD_CTRL_EN);
  103. hw_write(ci, OP_USBCMD, HSPHY_SESS_VLD_CTRL,
  104. HSPHY_SESS_VLD_CTRL);
  105. }
  106. break;
  107. case CI_HDRC_CONTROLLER_STOPPED_EVENT:
  108. dev_dbg(dev, "CI_HDRC_CONTROLLER_STOPPED_EVENT received\n");
  109. phy_power_off(ci->phy);
  110. phy_exit(ci->phy);
  111. break;
  112. default:
  113. dev_dbg(dev, "unknown ci_hdrc event\n");
  114. break;
  115. }
  116. return 0;
  117. }
  118. static int ci_hdrc_msm_mux_phy(struct ci_hdrc_msm *ci,
  119. struct platform_device *pdev)
  120. {
  121. struct regmap *regmap;
  122. struct device *dev = &pdev->dev;
  123. struct of_phandle_args args;
  124. u32 val;
  125. int ret;
  126. ret = of_parse_phandle_with_fixed_args(dev->of_node, "phy-select", 2, 0,
  127. &args);
  128. if (ret)
  129. return 0;
  130. regmap = syscon_node_to_regmap(args.np);
  131. of_node_put(args.np);
  132. if (IS_ERR(regmap))
  133. return PTR_ERR(regmap);
  134. ret = regmap_write(regmap, args.args[0], args.args[1]);
  135. if (ret)
  136. return ret;
  137. ci->secondary_phy = !!args.args[1];
  138. if (ci->secondary_phy) {
  139. val = readl_relaxed(ci->base + HS_PHY_SEC_CTRL);
  140. val |= HS_PHY_DIG_CLAMP_N;
  141. writel_relaxed(val, ci->base + HS_PHY_SEC_CTRL);
  142. }
  143. return 0;
  144. }
  145. static int ci_hdrc_msm_probe(struct platform_device *pdev)
  146. {
  147. struct ci_hdrc_msm *ci;
  148. struct platform_device *plat_ci;
  149. struct clk *clk;
  150. struct reset_control *reset;
  151. struct resource *res;
  152. int ret;
  153. struct device_node *ulpi_node, *phy_node;
  154. dev_dbg(&pdev->dev, "ci_hdrc_msm_probe\n");
  155. ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL);
  156. if (!ci)
  157. return -ENOMEM;
  158. platform_set_drvdata(pdev, ci);
  159. ci->pdata.name = "ci_hdrc_msm";
  160. ci->pdata.capoffset = DEF_CAPOFFSET;
  161. ci->pdata.flags = CI_HDRC_REGS_SHARED | CI_HDRC_DISABLE_STREAMING |
  162. CI_HDRC_OVERRIDE_AHB_BURST |
  163. CI_HDRC_OVERRIDE_PHY_CONTROL;
  164. ci->pdata.notify_event = ci_hdrc_msm_notify_event;
  165. reset = devm_reset_control_get(&pdev->dev, "core");
  166. if (IS_ERR(reset))
  167. return PTR_ERR(reset);
  168. ci->core_clk = clk = devm_clk_get(&pdev->dev, "core");
  169. if (IS_ERR(clk))
  170. return PTR_ERR(clk);
  171. ci->iface_clk = clk = devm_clk_get(&pdev->dev, "iface");
  172. if (IS_ERR(clk))
  173. return PTR_ERR(clk);
  174. ci->fs_clk = clk = devm_clk_get(&pdev->dev, "fs");
  175. if (IS_ERR(clk)) {
  176. if (PTR_ERR(clk) == -EPROBE_DEFER)
  177. return -EPROBE_DEFER;
  178. ci->fs_clk = NULL;
  179. }
  180. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  181. ci->base = devm_ioremap_resource(&pdev->dev, res);
  182. if (IS_ERR(ci->base))
  183. return PTR_ERR(ci->base);
  184. ci->rcdev.owner = THIS_MODULE;
  185. ci->rcdev.ops = &ci_hdrc_msm_reset_ops;
  186. ci->rcdev.of_node = pdev->dev.of_node;
  187. ci->rcdev.nr_resets = 2;
  188. ret = reset_controller_register(&ci->rcdev);
  189. if (ret)
  190. return ret;
  191. ret = clk_prepare_enable(ci->fs_clk);
  192. if (ret)
  193. goto err_fs;
  194. reset_control_assert(reset);
  195. usleep_range(10000, 12000);
  196. reset_control_deassert(reset);
  197. clk_disable_unprepare(ci->fs_clk);
  198. ret = clk_prepare_enable(ci->core_clk);
  199. if (ret)
  200. goto err_fs;
  201. ret = clk_prepare_enable(ci->iface_clk);
  202. if (ret)
  203. goto err_iface;
  204. ret = ci_hdrc_msm_mux_phy(ci, pdev);
  205. if (ret)
  206. goto err_mux;
  207. ulpi_node = of_find_node_by_name(of_node_get(pdev->dev.of_node), "ulpi");
  208. if (ulpi_node) {
  209. phy_node = of_get_next_available_child(ulpi_node, NULL);
  210. ci->hsic = of_device_is_compatible(phy_node, "qcom,usb-hsic-phy");
  211. of_node_put(phy_node);
  212. }
  213. of_node_put(ulpi_node);
  214. plat_ci = ci_hdrc_add_device(&pdev->dev, pdev->resource,
  215. pdev->num_resources, &ci->pdata);
  216. if (IS_ERR(plat_ci)) {
  217. ret = PTR_ERR(plat_ci);
  218. if (ret != -EPROBE_DEFER)
  219. dev_err(&pdev->dev, "ci_hdrc_add_device failed!\n");
  220. goto err_mux;
  221. }
  222. ci->ci = plat_ci;
  223. pm_runtime_set_active(&pdev->dev);
  224. pm_runtime_no_callbacks(&pdev->dev);
  225. pm_runtime_enable(&pdev->dev);
  226. return 0;
  227. err_mux:
  228. clk_disable_unprepare(ci->iface_clk);
  229. err_iface:
  230. clk_disable_unprepare(ci->core_clk);
  231. err_fs:
  232. reset_controller_unregister(&ci->rcdev);
  233. return ret;
  234. }
  235. static int ci_hdrc_msm_remove(struct platform_device *pdev)
  236. {
  237. struct ci_hdrc_msm *ci = platform_get_drvdata(pdev);
  238. pm_runtime_disable(&pdev->dev);
  239. ci_hdrc_remove_device(ci->ci);
  240. clk_disable_unprepare(ci->iface_clk);
  241. clk_disable_unprepare(ci->core_clk);
  242. reset_controller_unregister(&ci->rcdev);
  243. return 0;
  244. }
  245. static const struct of_device_id msm_ci_dt_match[] = {
  246. { .compatible = "qcom,ci-hdrc", },
  247. { }
  248. };
  249. MODULE_DEVICE_TABLE(of, msm_ci_dt_match);
  250. static struct platform_driver ci_hdrc_msm_driver = {
  251. .probe = ci_hdrc_msm_probe,
  252. .remove = ci_hdrc_msm_remove,
  253. .driver = {
  254. .name = "msm_hsusb",
  255. .of_match_table = msm_ci_dt_match,
  256. },
  257. };
  258. module_platform_driver(ci_hdrc_msm_driver);
  259. MODULE_ALIAS("platform:msm_hsusb");
  260. MODULE_ALIAS("platform:ci13xxx_msm");
  261. MODULE_LICENSE("GPL v2");