phy-qcom-m31.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014-2023, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/phy/phy.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reset.h>
  15. #include <linux/slab.h>
  16. #define USB2PHY_PORT_UTMI_CTRL1 0x40
  17. #define USB2PHY_PORT_UTMI_CTRL2 0x44
  18. #define UTMI_ULPI_SEL BIT(7)
  19. #define UTMI_TEST_MUX_SEL BIT(6)
  20. #define HS_PHY_CTRL_REG 0x10
  21. #define UTMI_OTG_VBUS_VALID BIT(20)
  22. #define SW_SESSVLD_SEL BIT(28)
  23. #define USB_PHY_UTMI_CTRL0 0x3c
  24. #define USB_PHY_UTMI_CTRL5 0x50
  25. #define POR_EN BIT(1)
  26. #define USB_PHY_HS_PHY_CTRL_COMMON0 0x54
  27. #define COMMONONN BIT(7)
  28. #define FSEL BIT(4)
  29. #define RETENABLEN BIT(3)
  30. #define FREQ_24MHZ (BIT(6) | BIT(4))
  31. #define USB_PHY_HS_PHY_CTRL2 0x64
  32. #define USB2_SUSPEND_N_SEL BIT(3)
  33. #define USB2_SUSPEND_N BIT(2)
  34. #define USB2_UTMI_CLK_EN BIT(1)
  35. #define USB_PHY_CFG0 0x94
  36. #define UTMI_PHY_OVERRIDE_EN BIT(1)
  37. #define USB_PHY_REFCLK_CTRL 0xa0
  38. #define CLKCORE BIT(1)
  39. #define USB2PHY_PORT_POWERDOWN 0xa4
  40. #define POWER_UP BIT(0)
  41. #define POWER_DOWN 0
  42. #define USB_PHY_FSEL_SEL 0xb8
  43. #define FREQ_SEL BIT(0)
  44. #define USB2PHY_USB_PHY_M31_XCFGI_1 0xbc
  45. #define USB2_0_TX_ENABLE BIT(2)
  46. #define USB2PHY_USB_PHY_M31_XCFGI_4 0xc8
  47. #define HSTX_SLEW_RATE_400PS GENMASK(2, 0)
  48. #define PLL_CHARGING_PUMP_CURRENT_35UA GENMASK(4, 3)
  49. #define ODT_VALUE_38_02_OHM GENMASK(7, 6)
  50. #define USB2PHY_USB_PHY_M31_XCFGI_5 0xcc
  51. #define HSTX_PRE_EMPHASIS_LEVEL_0_55MA BIT(0)
  52. #define USB2PHY_USB_PHY_M31_XCFGI_9 0xdc
  53. #define HSTX_CURRENT_17_1MA_385MV BIT(1)
  54. #define USB2PHY_USB_PHY_M31_XCFGI_11 0xe4
  55. #define XCFG_COARSE_TUNE_NUM BIT(1)
  56. #define XCFG_FINE_TUNE_NUM BIT(3)
  57. struct m31_phy_regs {
  58. u32 off;
  59. u32 val;
  60. u32 delay;
  61. };
  62. struct m31_priv_data {
  63. bool ulpi_mode;
  64. const struct m31_phy_regs *regs;
  65. unsigned int nregs;
  66. };
  67. static const struct m31_phy_regs m31_ipq5018_regs[] = {
  68. {
  69. .off = USB_PHY_CFG0,
  70. .val = UTMI_PHY_OVERRIDE_EN
  71. },
  72. {
  73. .off = USB_PHY_UTMI_CTRL5,
  74. .val = POR_EN,
  75. .delay = 15
  76. },
  77. {
  78. .off = USB_PHY_FSEL_SEL,
  79. .val = FREQ_SEL
  80. },
  81. {
  82. .off = USB_PHY_HS_PHY_CTRL_COMMON0,
  83. .val = COMMONONN | FSEL | RETENABLEN
  84. },
  85. {
  86. .off = USB_PHY_REFCLK_CTRL,
  87. .val = CLKCORE
  88. },
  89. {
  90. .off = USB_PHY_UTMI_CTRL5,
  91. .val = POR_EN
  92. },
  93. {
  94. .off = USB_PHY_HS_PHY_CTRL2,
  95. .val = USB2_SUSPEND_N_SEL | USB2_SUSPEND_N | USB2_UTMI_CLK_EN
  96. },
  97. {
  98. .off = USB_PHY_UTMI_CTRL5,
  99. .val = 0x0
  100. },
  101. {
  102. .off = USB_PHY_HS_PHY_CTRL2,
  103. .val = USB2_SUSPEND_N | USB2_UTMI_CLK_EN
  104. },
  105. {
  106. .off = USB_PHY_CFG0,
  107. .val = 0x0
  108. },
  109. };
  110. static struct m31_phy_regs m31_ipq5332_regs[] = {
  111. {
  112. USB_PHY_CFG0,
  113. UTMI_PHY_OVERRIDE_EN,
  114. 0
  115. },
  116. {
  117. USB_PHY_UTMI_CTRL5,
  118. POR_EN,
  119. 15
  120. },
  121. {
  122. USB_PHY_FSEL_SEL,
  123. FREQ_SEL,
  124. 0
  125. },
  126. {
  127. USB_PHY_HS_PHY_CTRL_COMMON0,
  128. COMMONONN | FREQ_24MHZ | RETENABLEN,
  129. 0
  130. },
  131. {
  132. USB_PHY_UTMI_CTRL5,
  133. POR_EN,
  134. 0
  135. },
  136. {
  137. USB_PHY_HS_PHY_CTRL2,
  138. USB2_SUSPEND_N_SEL | USB2_SUSPEND_N | USB2_UTMI_CLK_EN,
  139. 0
  140. },
  141. {
  142. USB2PHY_USB_PHY_M31_XCFGI_11,
  143. XCFG_COARSE_TUNE_NUM | XCFG_FINE_TUNE_NUM,
  144. 0
  145. },
  146. {
  147. USB2PHY_USB_PHY_M31_XCFGI_4,
  148. HSTX_SLEW_RATE_400PS | PLL_CHARGING_PUMP_CURRENT_35UA | ODT_VALUE_38_02_OHM,
  149. 0
  150. },
  151. {
  152. USB2PHY_USB_PHY_M31_XCFGI_1,
  153. USB2_0_TX_ENABLE,
  154. 0
  155. },
  156. {
  157. USB2PHY_USB_PHY_M31_XCFGI_5,
  158. HSTX_PRE_EMPHASIS_LEVEL_0_55MA,
  159. 4
  160. },
  161. {
  162. USB2PHY_USB_PHY_M31_XCFGI_9,
  163. HSTX_CURRENT_17_1MA_385MV,
  164. },
  165. {
  166. USB_PHY_UTMI_CTRL5,
  167. 0x0,
  168. 0
  169. },
  170. {
  171. USB_PHY_HS_PHY_CTRL2,
  172. USB2_SUSPEND_N | USB2_UTMI_CLK_EN,
  173. 0
  174. },
  175. };
  176. struct m31usb_phy {
  177. struct phy *phy;
  178. void __iomem *base;
  179. const struct m31_phy_regs *regs;
  180. int nregs;
  181. struct regulator *vreg;
  182. struct clk *clk;
  183. struct reset_control *reset;
  184. bool ulpi_mode;
  185. };
  186. static int m31usb_phy_init(struct phy *phy)
  187. {
  188. struct m31usb_phy *qphy = phy_get_drvdata(phy);
  189. const struct m31_phy_regs *regs = qphy->regs;
  190. int i, ret;
  191. ret = regulator_enable(qphy->vreg);
  192. if (ret) {
  193. dev_err(&phy->dev, "failed to enable regulator, %d\n", ret);
  194. return ret;
  195. }
  196. ret = clk_prepare_enable(qphy->clk);
  197. if (ret) {
  198. regulator_disable(qphy->vreg);
  199. dev_err(&phy->dev, "failed to enable cfg ahb clock, %d\n", ret);
  200. return ret;
  201. }
  202. /* Perform phy reset */
  203. reset_control_assert(qphy->reset);
  204. udelay(5);
  205. reset_control_deassert(qphy->reset);
  206. /* configure for ULPI mode if requested */
  207. if (qphy->ulpi_mode)
  208. writel(0x0, qphy->base + USB2PHY_PORT_UTMI_CTRL2);
  209. /* Enable the PHY */
  210. writel(POWER_UP, qphy->base + USB2PHY_PORT_POWERDOWN);
  211. /* Turn on phy ref clock */
  212. for (i = 0; i < qphy->nregs; i++) {
  213. writel(regs[i].val, qphy->base + regs[i].off);
  214. if (regs[i].delay)
  215. udelay(regs[i].delay);
  216. }
  217. return 0;
  218. }
  219. static int m31usb_phy_shutdown(struct phy *phy)
  220. {
  221. struct m31usb_phy *qphy = phy_get_drvdata(phy);
  222. /* Disable the PHY */
  223. writel_relaxed(POWER_DOWN, qphy->base + USB2PHY_PORT_POWERDOWN);
  224. clk_disable_unprepare(qphy->clk);
  225. regulator_disable(qphy->vreg);
  226. return 0;
  227. }
  228. static const struct phy_ops m31usb_phy_gen_ops = {
  229. .power_on = m31usb_phy_init,
  230. .power_off = m31usb_phy_shutdown,
  231. .owner = THIS_MODULE,
  232. };
  233. static int m31usb_phy_probe(struct platform_device *pdev)
  234. {
  235. struct phy_provider *phy_provider;
  236. const struct m31_priv_data *data;
  237. struct device *dev = &pdev->dev;
  238. struct m31usb_phy *qphy;
  239. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  240. if (!qphy)
  241. return -ENOMEM;
  242. qphy->base = devm_platform_ioremap_resource(pdev, 0);
  243. if (IS_ERR(qphy->base))
  244. return PTR_ERR(qphy->base);
  245. qphy->reset = devm_reset_control_get_exclusive_by_index(dev, 0);
  246. if (IS_ERR(qphy->reset))
  247. return PTR_ERR(qphy->reset);
  248. qphy->clk = devm_clk_get(dev, NULL);
  249. if (IS_ERR(qphy->clk))
  250. return dev_err_probe(dev, PTR_ERR(qphy->clk),
  251. "failed to get clk\n");
  252. data = of_device_get_match_data(dev);
  253. qphy->regs = data->regs;
  254. qphy->nregs = data->nregs;
  255. qphy->ulpi_mode = data->ulpi_mode;
  256. qphy->phy = devm_phy_create(dev, NULL, &m31usb_phy_gen_ops);
  257. if (IS_ERR(qphy->phy))
  258. return dev_err_probe(dev, PTR_ERR(qphy->phy),
  259. "failed to create phy\n");
  260. qphy->vreg = devm_regulator_get(dev, "vdd");
  261. if (IS_ERR(qphy->vreg))
  262. return dev_err_probe(dev, PTR_ERR(qphy->vreg),
  263. "failed to get vreg\n");
  264. phy_set_drvdata(qphy->phy, qphy);
  265. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  266. if (!IS_ERR(phy_provider))
  267. dev_info(dev, "Registered M31 USB phy\n");
  268. return PTR_ERR_OR_ZERO(phy_provider);
  269. }
  270. static const struct m31_priv_data m31_ipq5018_data = {
  271. .ulpi_mode = false,
  272. .regs = m31_ipq5018_regs,
  273. .nregs = ARRAY_SIZE(m31_ipq5018_regs),
  274. };
  275. static const struct m31_priv_data m31_ipq5332_data = {
  276. .ulpi_mode = false,
  277. .regs = m31_ipq5332_regs,
  278. .nregs = ARRAY_SIZE(m31_ipq5332_regs),
  279. };
  280. static const struct of_device_id m31usb_phy_id_table[] = {
  281. { .compatible = "qcom,ipq5018-usb-hsphy", .data = &m31_ipq5018_data },
  282. { .compatible = "qcom,ipq5332-usb-hsphy", .data = &m31_ipq5332_data },
  283. { },
  284. };
  285. MODULE_DEVICE_TABLE(of, m31usb_phy_id_table);
  286. static struct platform_driver m31usb_phy_driver = {
  287. .probe = m31usb_phy_probe,
  288. .driver = {
  289. .name = "qcom-m31usb-phy",
  290. .of_match_table = m31usb_phy_id_table,
  291. },
  292. };
  293. module_platform_driver(m31usb_phy_driver);
  294. MODULE_DESCRIPTION("USB2 Qualcomm M31 HSPHY driver");
  295. MODULE_LICENSE("GPL");