phy-qcom-m31.c 7.2 KB

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