sti_usb_phy.c 4.0 KB

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