sti_usb_phy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  4. * Author(s): Patrice Chotard, <patrice.chotard@st.com> for STMicroelectronics.
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <bitfield.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <fdtdec.h>
  12. #include <generic-phy.h>
  13. #include <linux/libfdt.h>
  14. #include <regmap.h>
  15. #include <reset-uclass.h>
  16. #include <syscon.h>
  17. #include <wait_bit.h>
  18. #include <linux/bitops.h>
  19. #include <linux/compat.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. /* Default PHY_SEL and REFCLKSEL configuration */
  22. #define STIH407_USB_PICOPHY_CTRL_PORT_CONF 0x6
  23. /* ports parameters overriding */
  24. #define STIH407_USB_PICOPHY_PARAM_DEF 0x39a4dc
  25. #define PHYPARAM_REG 1
  26. #define PHYCTRL_REG 2
  27. #define PHYPARAM_NB 3
  28. struct sti_usb_phy {
  29. struct regmap *regmap;
  30. struct reset_ctl global_ctl;
  31. struct reset_ctl port_ctl;
  32. int param;
  33. int ctrl;
  34. };
  35. static int sti_usb_phy_deassert(struct sti_usb_phy *phy)
  36. {
  37. int ret;
  38. ret = reset_deassert(&phy->global_ctl);
  39. if (ret < 0) {
  40. pr_err("PHY global deassert failed: %d", ret);
  41. return ret;
  42. }
  43. ret = reset_deassert(&phy->port_ctl);
  44. if (ret < 0)
  45. pr_err("PHY port deassert failed: %d", ret);
  46. return ret;
  47. }
  48. static int sti_usb_phy_init(struct phy *usb_phy)
  49. {
  50. struct udevice *dev = usb_phy->dev;
  51. struct sti_usb_phy *phy = dev_get_priv(dev);
  52. void __iomem *reg;
  53. /* set ctrl picophy value */
  54. reg = (void __iomem *)phy->regmap->ranges[0].start + phy->ctrl;
  55. /* CTRL_PORT mask is 0x1f */
  56. clrsetbits_le32(reg, 0x1f, STIH407_USB_PICOPHY_CTRL_PORT_CONF);
  57. /* set ports parameters overriding */
  58. reg = (void __iomem *)phy->regmap->ranges[0].start + phy->param;
  59. /* PARAM_DEF mask is 0xffffffff */
  60. clrsetbits_le32(reg, 0xffffffff, STIH407_USB_PICOPHY_PARAM_DEF);
  61. return sti_usb_phy_deassert(phy);
  62. }
  63. static int sti_usb_phy_exit(struct phy *usb_phy)
  64. {
  65. struct udevice *dev = usb_phy->dev;
  66. struct sti_usb_phy *phy = dev_get_priv(dev);
  67. int ret;
  68. ret = reset_assert(&phy->port_ctl);
  69. if (ret < 0) {
  70. pr_err("PHY port assert failed: %d", ret);
  71. return ret;
  72. }
  73. ret = reset_assert(&phy->global_ctl);
  74. if (ret < 0)
  75. pr_err("PHY global assert failed: %d", ret);
  76. return ret;
  77. }
  78. struct phy_ops sti_usb_phy_ops = {
  79. .init = sti_usb_phy_init,
  80. .exit = sti_usb_phy_exit,
  81. };
  82. int sti_usb_phy_probe(struct udevice *dev)
  83. {
  84. struct sti_usb_phy *priv = dev_get_priv(dev);
  85. struct udevice *syscon;
  86. struct ofnode_phandle_args syscfg_phandle;
  87. u32 cells[PHYPARAM_NB];
  88. int ret, count;
  89. /* get corresponding syscon phandle */
  90. ret = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0,
  91. &syscfg_phandle);
  92. if (ret < 0) {
  93. pr_err("Can't get syscfg phandle: %d\n", ret);
  94. return ret;
  95. }
  96. ret = uclass_get_device_by_ofnode(UCLASS_SYSCON, syscfg_phandle.node,
  97. &syscon);
  98. if (ret) {
  99. pr_err("unable to find syscon device (%d)\n", ret);
  100. return ret;
  101. }
  102. priv->regmap = syscon_get_regmap(syscon);
  103. if (!priv->regmap) {
  104. pr_err("unable to find regmap\n");
  105. return -ENODEV;
  106. }
  107. /* get phy param offset */
  108. count = fdtdec_get_int_array_count(gd->fdt_blob, dev_of_offset(dev),
  109. "st,syscfg", cells,
  110. ARRAY_SIZE(cells));
  111. if (count < 0) {
  112. pr_err("Bad PHY st,syscfg property %d\n", count);
  113. return -EINVAL;
  114. }
  115. if (count > PHYPARAM_NB) {
  116. pr_err("Unsupported PHY param count %d\n", count);
  117. return -EINVAL;
  118. }
  119. priv->param = cells[PHYPARAM_REG];
  120. priv->ctrl = cells[PHYCTRL_REG];
  121. /* get global reset control */
  122. ret = reset_get_by_name(dev, "global", &priv->global_ctl);
  123. if (ret) {
  124. pr_err("can't get global reset for %s (%d)", dev->name, ret);
  125. return ret;
  126. }
  127. /* get port reset control */
  128. ret = reset_get_by_name(dev, "port", &priv->port_ctl);
  129. if (ret) {
  130. pr_err("can't get port reset for %s (%d)", dev->name, ret);
  131. return ret;
  132. }
  133. return 0;
  134. }
  135. static const struct udevice_id sti_usb_phy_ids[] = {
  136. { .compatible = "st,stih407-usb2-phy" },
  137. { }
  138. };
  139. U_BOOT_DRIVER(sti_usb_phy) = {
  140. .name = "sti_usb_phy",
  141. .id = UCLASS_PHY,
  142. .of_match = sti_usb_phy_ids,
  143. .probe = sti_usb_phy_probe,
  144. .ops = &sti_usb_phy_ops,
  145. .priv_auto_alloc_size = sizeof(struct sti_usb_phy),
  146. };