phy-sunplus-usb2.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sunplus SP7021 USB 2.0 phy driver
  4. *
  5. * Copyright (C) 2022 Sunplus Technology Inc., All rights reserved.
  6. *
  7. * Note 1 : non-posted write command for the registers accesses of
  8. * Sunplus SP7021.
  9. *
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/nvmem-consumer.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/reset.h>
  21. #define HIGH_MASK_BITS GENMASK(31, 16)
  22. #define LOW_MASK_BITS GENMASK(15, 0)
  23. #define OTP_DISC_LEVEL_DEFAULT 0xd
  24. /* GROUP UPHY */
  25. #define CONFIG1 0x4
  26. #define J_HS_TX_PWRSAV BIT(5)
  27. #define CONFIG3 0xc
  28. #define J_FORCE_DISC_ON BIT(5)
  29. #define J_DEBUG_CTRL_ADDR_MACRO BIT(0)
  30. #define CONFIG7 0x1c
  31. #define J_DISC 0X1f
  32. #define CONFIG9 0x24
  33. #define J_ECO_PATH BIT(6)
  34. #define CONFIG16 0x40
  35. #define J_TBCWAIT_MASK GENMASK(6, 5)
  36. #define J_TBCWAIT_1P1_MS FIELD_PREP(J_TBCWAIT_MASK, 0)
  37. #define J_TVDM_SRC_DIS_MASK GENMASK(4, 3)
  38. #define J_TVDM_SRC_DIS_8P2_MS FIELD_PREP(J_TVDM_SRC_DIS_MASK, 3)
  39. #define J_TVDM_SRC_EN_MASK GENMASK(2, 1)
  40. #define J_TVDM_SRC_EN_1P6_MS FIELD_PREP(J_TVDM_SRC_EN_MASK, 0)
  41. #define J_BC_EN BIT(0)
  42. #define CONFIG17 0x44
  43. #define IBG_TRIM0_MASK GENMASK(7, 5)
  44. #define IBG_TRIM0_SSLVHT FIELD_PREP(IBG_TRIM0_MASK, 4)
  45. #define J_VDATREE_TRIM_MASK GENMASK(4, 1)
  46. #define J_VDATREE_TRIM_DEFAULT FIELD_PREP(J_VDATREE_TRIM_MASK, 9)
  47. #define CONFIG23 0x5c
  48. #define PROB_MASK GENMASK(5, 3)
  49. #define PROB FIELD_PREP(PROB_MASK, 7)
  50. /* GROUP MOON4 */
  51. #define UPHY_CONTROL0 0x0
  52. #define UPHY_CONTROL1 0x4
  53. #define UPHY_CONTROL2 0x8
  54. #define MO1_UPHY_RX_CLK_SEL BIT(6)
  55. #define MASK_MO1_UPHY_RX_CLK_SEL BIT(6 + 16)
  56. #define UPHY_CONTROL3 0xc
  57. #define MO1_UPHY_PLL_POWER_OFF_SEL BIT(7)
  58. #define MASK_MO1_UPHY_PLL_POWER_OFF_SEL BIT(7 + 16)
  59. #define MO1_UPHY_PLL_POWER_OFF BIT(3)
  60. #define MASK_UPHY_PLL_POWER_OFF BIT(3 + 16)
  61. struct sp_usbphy {
  62. struct device *dev;
  63. struct resource *phy_res_mem;
  64. struct resource *moon4_res_mem;
  65. struct reset_control *rstc;
  66. struct clk *phy_clk;
  67. void __iomem *phy_regs;
  68. void __iomem *moon4_regs;
  69. u32 disc_vol_addr_off;
  70. };
  71. static int update_disc_vol(struct sp_usbphy *usbphy)
  72. {
  73. struct nvmem_cell *cell;
  74. char *disc_name = "disc_vol";
  75. ssize_t otp_l = 0;
  76. char *otp_v;
  77. u32 val, set;
  78. cell = nvmem_cell_get(usbphy->dev, disc_name);
  79. if (IS_ERR_OR_NULL(cell)) {
  80. if (PTR_ERR(cell) == -EPROBE_DEFER)
  81. return -EPROBE_DEFER;
  82. }
  83. otp_v = nvmem_cell_read(cell, &otp_l);
  84. nvmem_cell_put(cell);
  85. if (!IS_ERR(otp_v)) {
  86. set = *(otp_v + 1);
  87. set = (set << (sizeof(char) * 8)) | *otp_v;
  88. set = (set >> usbphy->disc_vol_addr_off) & J_DISC;
  89. }
  90. if (IS_ERR(otp_v) || set == 0)
  91. set = OTP_DISC_LEVEL_DEFAULT;
  92. val = readl(usbphy->phy_regs + CONFIG7);
  93. val = (val & ~J_DISC) | set;
  94. writel(val, usbphy->phy_regs + CONFIG7);
  95. return 0;
  96. }
  97. static int sp_uphy_init(struct phy *phy)
  98. {
  99. struct sp_usbphy *usbphy = phy_get_drvdata(phy);
  100. u32 val;
  101. int ret;
  102. ret = clk_prepare_enable(usbphy->phy_clk);
  103. if (ret)
  104. goto err_clk;
  105. ret = reset_control_deassert(usbphy->rstc);
  106. if (ret)
  107. goto err_reset;
  108. /* Default value modification */
  109. writel(HIGH_MASK_BITS | 0x4002, usbphy->moon4_regs + UPHY_CONTROL0);
  110. writel(HIGH_MASK_BITS | 0x8747, usbphy->moon4_regs + UPHY_CONTROL1);
  111. /* disconnect voltage */
  112. ret = update_disc_vol(usbphy);
  113. if (ret < 0)
  114. return ret;
  115. /* board uphy 0 internal register modification for tid certification */
  116. val = readl(usbphy->phy_regs + CONFIG9);
  117. val &= ~(J_ECO_PATH);
  118. writel(val, usbphy->phy_regs + CONFIG9);
  119. val = readl(usbphy->phy_regs + CONFIG1);
  120. val &= ~(J_HS_TX_PWRSAV);
  121. writel(val, usbphy->phy_regs + CONFIG1);
  122. val = readl(usbphy->phy_regs + CONFIG23);
  123. val = (val & ~PROB) | PROB;
  124. writel(val, usbphy->phy_regs + CONFIG23);
  125. /* port 0 uphy clk fix */
  126. writel(MASK_MO1_UPHY_RX_CLK_SEL | MO1_UPHY_RX_CLK_SEL,
  127. usbphy->moon4_regs + UPHY_CONTROL2);
  128. /* battery charger */
  129. writel(J_TBCWAIT_1P1_MS | J_TVDM_SRC_DIS_8P2_MS | J_TVDM_SRC_EN_1P6_MS | J_BC_EN,
  130. usbphy->phy_regs + CONFIG16);
  131. writel(IBG_TRIM0_SSLVHT | J_VDATREE_TRIM_DEFAULT, usbphy->phy_regs + CONFIG17);
  132. /* chirp mode */
  133. writel(J_FORCE_DISC_ON | J_DEBUG_CTRL_ADDR_MACRO, usbphy->phy_regs + CONFIG3);
  134. return 0;
  135. err_reset:
  136. reset_control_assert(usbphy->rstc);
  137. err_clk:
  138. clk_disable_unprepare(usbphy->phy_clk);
  139. return ret;
  140. }
  141. static int sp_uphy_power_on(struct phy *phy)
  142. {
  143. struct sp_usbphy *usbphy = phy_get_drvdata(phy);
  144. u32 pll_pwr_on, pll_pwr_off;
  145. /* PLL power off/on twice */
  146. pll_pwr_off = (readl(usbphy->moon4_regs + UPHY_CONTROL3) & ~LOW_MASK_BITS)
  147. | MO1_UPHY_PLL_POWER_OFF_SEL | MO1_UPHY_PLL_POWER_OFF;
  148. pll_pwr_on = (readl(usbphy->moon4_regs + UPHY_CONTROL3) & ~LOW_MASK_BITS)
  149. | MO1_UPHY_PLL_POWER_OFF_SEL;
  150. writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_off,
  151. usbphy->moon4_regs + UPHY_CONTROL3);
  152. mdelay(1);
  153. writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_on,
  154. usbphy->moon4_regs + UPHY_CONTROL3);
  155. mdelay(1);
  156. writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_off,
  157. usbphy->moon4_regs + UPHY_CONTROL3);
  158. mdelay(1);
  159. writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_on,
  160. usbphy->moon4_regs + UPHY_CONTROL3);
  161. mdelay(1);
  162. writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | 0x0,
  163. usbphy->moon4_regs + UPHY_CONTROL3);
  164. return 0;
  165. }
  166. static int sp_uphy_power_off(struct phy *phy)
  167. {
  168. struct sp_usbphy *usbphy = phy_get_drvdata(phy);
  169. u32 pll_pwr_off;
  170. pll_pwr_off = (readl(usbphy->moon4_regs + UPHY_CONTROL3) & ~LOW_MASK_BITS)
  171. | MO1_UPHY_PLL_POWER_OFF_SEL | MO1_UPHY_PLL_POWER_OFF;
  172. writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_off,
  173. usbphy->moon4_regs + UPHY_CONTROL3);
  174. mdelay(1);
  175. writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | 0x0,
  176. usbphy->moon4_regs + UPHY_CONTROL3);
  177. return 0;
  178. }
  179. static int sp_uphy_exit(struct phy *phy)
  180. {
  181. struct sp_usbphy *usbphy = phy_get_drvdata(phy);
  182. reset_control_assert(usbphy->rstc);
  183. clk_disable_unprepare(usbphy->phy_clk);
  184. return 0;
  185. }
  186. static const struct phy_ops sp_uphy_ops = {
  187. .init = sp_uphy_init,
  188. .power_on = sp_uphy_power_on,
  189. .power_off = sp_uphy_power_off,
  190. .exit = sp_uphy_exit,
  191. };
  192. static const struct of_device_id sp_uphy_dt_ids[] = {
  193. {.compatible = "sunplus,sp7021-usb2-phy", },
  194. { }
  195. };
  196. MODULE_DEVICE_TABLE(of, sp_uphy_dt_ids);
  197. static int sp_usb_phy_probe(struct platform_device *pdev)
  198. {
  199. struct sp_usbphy *usbphy;
  200. struct phy_provider *phy_provider;
  201. struct phy *phy;
  202. int ret;
  203. usbphy = devm_kzalloc(&pdev->dev, sizeof(*usbphy), GFP_KERNEL);
  204. if (!usbphy)
  205. return -ENOMEM;
  206. usbphy->dev = &pdev->dev;
  207. usbphy->phy_res_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
  208. usbphy->phy_regs = devm_ioremap_resource(&pdev->dev, usbphy->phy_res_mem);
  209. if (IS_ERR(usbphy->phy_regs))
  210. return PTR_ERR(usbphy->phy_regs);
  211. usbphy->moon4_res_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "moon4");
  212. if (!usbphy->moon4_res_mem)
  213. return -EINVAL;
  214. usbphy->moon4_regs = devm_ioremap(&pdev->dev, usbphy->moon4_res_mem->start,
  215. resource_size(usbphy->moon4_res_mem));
  216. if (!usbphy->moon4_regs)
  217. return -ENOMEM;
  218. usbphy->phy_clk = devm_clk_get(&pdev->dev, NULL);
  219. if (IS_ERR(usbphy->phy_clk))
  220. return PTR_ERR(usbphy->phy_clk);
  221. usbphy->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  222. if (IS_ERR(usbphy->rstc))
  223. return PTR_ERR(usbphy->rstc);
  224. of_property_read_u32(pdev->dev.of_node, "sunplus,disc-vol-addr-off",
  225. &usbphy->disc_vol_addr_off);
  226. phy = devm_phy_create(&pdev->dev, NULL, &sp_uphy_ops);
  227. if (IS_ERR(phy)) {
  228. ret = -PTR_ERR(phy);
  229. return ret;
  230. }
  231. phy_set_drvdata(phy, usbphy);
  232. phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
  233. return PTR_ERR_OR_ZERO(phy_provider);
  234. }
  235. static struct platform_driver sunplus_usb_phy_driver = {
  236. .probe = sp_usb_phy_probe,
  237. .driver = {
  238. .name = "sunplus-usb2-phy",
  239. .of_match_table = sp_uphy_dt_ids,
  240. },
  241. };
  242. module_platform_driver(sunplus_usb_phy_driver);
  243. MODULE_AUTHOR("Vincent Shih <[email protected]>");
  244. MODULE_DESCRIPTION("Sunplus USB 2.0 phy driver");
  245. MODULE_LICENSE("GPL");