phy-npcm-usb.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2021 Nuvoton Technology Corp.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <generic-phy.h>
  8. #include <regmap.h>
  9. #include <reset.h>
  10. #include <syscon.h>
  11. #include <dm/device_compat.h>
  12. #include <linux/bitfield.h>
  13. #include <linux/delay.h>
  14. /* GCR Register Offsets */
  15. #define GCR_INTCR3 0x9C
  16. #define GCR_USB1PHYCTL 0x140
  17. #define GCR_USB2PHYCTL 0x144
  18. #define GCR_USB3PHYCTL 0x148
  19. /* USBnPHYCTL bit fields */
  20. #define PHYCTL_RS BIT(28)
  21. #define USBPHY2SW GENMASK(13, 12)
  22. #define USBPHY3SW GENMASK(15, 14)
  23. #define USBPHY2SW_DEV9_PHY1 FIELD_PREP(USBPHY2SW, 0)
  24. #define USBPHY2SW_HOST1 FIELD_PREP(USBPHY2SW, 1)
  25. #define USBPHY2SW_DEV9_PHY2 FIELD_PREP(USBPHY2SW, 3)
  26. #define USBPHY3SW_DEV8_PHY1 FIELD_PREP(USBPHY3SW, 0)
  27. #define USBPHY3SW_HOST2 FIELD_PREP(USBPHY3SW, 1)
  28. #define USBPHY3SW_DEV8_PHY3 FIELD_PREP(USBPHY3SW, 3)
  29. enum controller_id {
  30. UDC0_7,
  31. UDC8,
  32. UDC9,
  33. USBH1,
  34. USBH2,
  35. };
  36. enum phy_id {
  37. PHY1 = 1,
  38. PHY2,
  39. PHY3,
  40. };
  41. /* Phy Switch Settings */
  42. #define USBDPHY1 ((PHY1 << 8) | UDC0_7) /* Connect UDC0~7 to PHY1 */
  43. #define USBD8PHY1 ((PHY1 << 8) | UDC8) /* Connect UDC8 to PHY1 */
  44. #define USBD9PHY1 ((PHY1 << 8) | UDC9) /* Connect UDC9 to PHY1 */
  45. #define USBD9PHY2 ((PHY2 << 8) | UDC9) /* Connect UDC9 to PHY2 */
  46. #define USBH1PHY2 ((PHY2 << 8) | USBH1) /* Connect USBH1 to PHY2 */
  47. #define USBD8PHY3 ((PHY3 << 8) | UDC8) /* Connect UDC8 to PHY3 */
  48. #define USBH2PHY3 ((PHY3 << 8) | USBH2) /* Connect USBH2 to PHY3 */
  49. struct npcm_usbphy {
  50. struct regmap *syscon;
  51. u8 id;
  52. u16 phy_switch; /* (phy_id << 8) | controller_id */
  53. };
  54. static int npcm_usb_phy_init(struct phy *phy)
  55. {
  56. struct npcm_usbphy *priv = dev_get_priv(phy->dev);
  57. struct reset_ctl reset;
  58. int ret;
  59. ret = reset_get_by_index(phy->dev, 0, &reset);
  60. if (ret && ret != -ENOENT && ret != -ENOTSUPP) {
  61. dev_err(phy->dev, "can't get phy reset ctrl (err %d)", ret);
  62. return ret;
  63. }
  64. /* setup PHY switch */
  65. switch (priv->phy_switch) {
  66. case USBD8PHY1:
  67. regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY3SW,
  68. USBPHY3SW_DEV8_PHY1);
  69. break;
  70. case USBD8PHY3:
  71. regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY3SW,
  72. USBPHY3SW_DEV8_PHY3);
  73. break;
  74. case USBD9PHY1:
  75. regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY2SW,
  76. USBPHY2SW_DEV9_PHY1);
  77. break;
  78. case USBD9PHY2:
  79. regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY2SW,
  80. USBPHY2SW_DEV9_PHY2);
  81. break;
  82. case USBH1PHY2:
  83. regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY2SW,
  84. USBPHY2SW_HOST1);
  85. break;
  86. case USBH2PHY3:
  87. regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY3SW,
  88. USBPHY3SW_HOST2);
  89. break;
  90. default:
  91. break;
  92. }
  93. /* reset phy */
  94. if (reset_valid(&reset))
  95. reset_assert(&reset);
  96. /* Wait for PHY clocks to stablize for 50us or more */
  97. udelay(100);
  98. /* release phy from reset */
  99. if (reset_valid(&reset))
  100. reset_deassert(&reset);
  101. /* PHY RS bit should be set after reset */
  102. switch (priv->id) {
  103. case PHY1:
  104. regmap_update_bits(priv->syscon, GCR_USB1PHYCTL, PHYCTL_RS, PHYCTL_RS);
  105. break;
  106. case PHY2:
  107. regmap_update_bits(priv->syscon, GCR_USB2PHYCTL, PHYCTL_RS, PHYCTL_RS);
  108. break;
  109. case PHY3:
  110. regmap_update_bits(priv->syscon, GCR_USB3PHYCTL, PHYCTL_RS, PHYCTL_RS);
  111. break;
  112. default:
  113. break;
  114. }
  115. return 0;
  116. }
  117. static int npcm_usb_phy_exit(struct phy *phy)
  118. {
  119. struct npcm_usbphy *priv = dev_get_priv(phy->dev);
  120. /* set PHY switch to default state */
  121. switch (priv->phy_switch) {
  122. case USBD8PHY1:
  123. case USBD8PHY3:
  124. regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY3SW,
  125. USBPHY3SW_HOST2);
  126. break;
  127. case USBD9PHY1:
  128. case USBD9PHY2:
  129. regmap_update_bits(priv->syscon, GCR_INTCR3, USBPHY2SW,
  130. USBPHY2SW_HOST1);
  131. break;
  132. default:
  133. break;
  134. }
  135. return 0;
  136. }
  137. static int npcm_usb_phy_xlate(struct phy *phy, struct ofnode_phandle_args *args)
  138. {
  139. struct npcm_usbphy *priv = dev_get_priv(phy->dev);
  140. u16 phy_switch;
  141. if (args->args_count < 1 || args->args[0] > USBH2)
  142. return -EINVAL;
  143. phy_switch = (priv->id << 8) | args->args[0];
  144. switch (phy_switch) {
  145. case USBD9PHY1:
  146. case USBH2PHY3:
  147. case USBD8PHY3:
  148. if (!IS_ENABLED(CONFIG_ARCH_NPCM8XX))
  149. return -EINVAL;
  150. case USBDPHY1:
  151. case USBD8PHY1:
  152. case USBD9PHY2:
  153. case USBH1PHY2:
  154. priv->phy_switch = phy_switch;
  155. return 0;
  156. default:
  157. return -EINVAL;
  158. }
  159. }
  160. static int npcm_usb_phy_probe(struct udevice *dev)
  161. {
  162. struct npcm_usbphy *priv = dev_get_priv(dev);
  163. priv->syscon = syscon_regmap_lookup_by_phandle(dev->parent, "syscon");
  164. if (IS_ERR(priv->syscon)) {
  165. dev_err(dev, "%s: unable to get syscon\n", __func__);
  166. return PTR_ERR(priv->syscon);
  167. }
  168. priv->id = dev_read_u32_default(dev, "reg", -1);
  169. return 0;
  170. }
  171. static const struct udevice_id npcm_phy_ids[] = {
  172. { .compatible = "nuvoton,npcm845-usb-phy",},
  173. { .compatible = "nuvoton,npcm750-usb-phy",},
  174. { }
  175. };
  176. static struct phy_ops npcm_phy_ops = {
  177. .init = npcm_usb_phy_init,
  178. .exit = npcm_usb_phy_exit,
  179. .of_xlate = npcm_usb_phy_xlate,
  180. };
  181. U_BOOT_DRIVER(npcm_phy) = {
  182. .name = "npcm-usb-phy",
  183. .id = UCLASS_PHY,
  184. .of_match = npcm_phy_ids,
  185. .ops = &npcm_phy_ops,
  186. .probe = npcm_usb_phy_probe,
  187. .priv_auto = sizeof(struct npcm_usbphy),
  188. };