omap-usb2-phy.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * OMAP USB2 PHY LAYER
  4. *
  5. * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com
  6. * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
  7. */
  8. #include <common.h>
  9. #include <asm/io.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <generic-phy.h>
  13. #include <regmap.h>
  14. #include <syscon.h>
  15. #include <linux/bitops.h>
  16. #include <linux/err.h>
  17. #define OMAP_USB2_CALIBRATE_FALSE_DISCONNECT BIT(0)
  18. #define OMAP_USB2_DISABLE_CHG_DET BIT(1)
  19. #define OMAP_DEV_PHY_PD BIT(0)
  20. #define OMAP_USB2_PHY_PD BIT(28)
  21. #define AM437X_USB2_PHY_PD BIT(0)
  22. #define AM437X_USB2_OTG_PD BIT(1)
  23. #define AM437X_USB2_OTGVDET_EN BIT(19)
  24. #define AM437X_USB2_OTGSESSEND_EN BIT(20)
  25. #define USB2PHY_DISCON_BYP_LATCH BIT(31)
  26. #define USB2PHY_ANA_CONFIG1 (0x4c)
  27. #define AM654_USB2_OTG_PD BIT(8)
  28. #define AM654_USB2_VBUS_DET_EN BIT(5)
  29. #define AM654_USB2_VBUSVALID_DET_EN BIT(4)
  30. #define USB2PHY_CHRG_DET 0x14
  31. #define USB2PHY_USE_CHG_DET_REG BIT(29)
  32. #define USB2PHY_DIS_CHG_DET BIT(28)
  33. DECLARE_GLOBAL_DATA_PTR;
  34. struct omap_usb2_phy {
  35. struct regmap *pwr_regmap;
  36. ulong flags;
  37. void *phy_base;
  38. u32 pwr_reg_offset;
  39. };
  40. struct usb_phy_data {
  41. const char *label;
  42. u8 flags;
  43. u32 mask;
  44. u32 power_on;
  45. u32 power_off;
  46. };
  47. static const struct usb_phy_data omap5_usb2_data = {
  48. .label = "omap5_usb2",
  49. .flags = 0,
  50. .mask = OMAP_DEV_PHY_PD,
  51. .power_off = OMAP_DEV_PHY_PD,
  52. };
  53. static const struct usb_phy_data dra7x_usb2_data = {
  54. .label = "dra7x_usb2",
  55. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  56. .mask = OMAP_DEV_PHY_PD,
  57. .power_off = OMAP_DEV_PHY_PD,
  58. };
  59. static const struct usb_phy_data dra7x_usb2_phy2_data = {
  60. .label = "dra7x_usb2_phy2",
  61. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  62. .mask = OMAP_USB2_PHY_PD,
  63. .power_off = OMAP_USB2_PHY_PD,
  64. };
  65. static const struct usb_phy_data am437x_usb2_data = {
  66. .label = "am437x_usb2",
  67. .flags = 0,
  68. .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
  69. AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
  70. .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
  71. .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
  72. };
  73. static const struct usb_phy_data am654_usb2_data = {
  74. .label = "am654_usb2",
  75. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  76. .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN |
  77. AM654_USB2_VBUSVALID_DET_EN,
  78. .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN,
  79. .power_off = AM654_USB2_OTG_PD,
  80. };
  81. static const struct udevice_id omap_usb2_id_table[] = {
  82. {
  83. .compatible = "ti,omap5-usb2",
  84. .data = (ulong)&omap5_usb2_data,
  85. },
  86. {
  87. .compatible = "ti,dra7x-usb2",
  88. .data = (ulong)&dra7x_usb2_data,
  89. },
  90. {
  91. .compatible = "ti,dra7x-usb2-phy2",
  92. .data = (ulong)&dra7x_usb2_phy2_data,
  93. },
  94. {
  95. .compatible = "ti,am437x-usb2",
  96. .data = (ulong)&am437x_usb2_data,
  97. },
  98. {
  99. .compatible = "ti,am654-usb2",
  100. .data = (ulong)&am654_usb2_data,
  101. },
  102. {},
  103. };
  104. static int omap_usb_phy_power(struct phy *usb_phy, bool on)
  105. {
  106. struct udevice *dev = usb_phy->dev;
  107. const struct usb_phy_data *data;
  108. const struct omap_usb2_phy *phy = dev_get_priv(dev);
  109. u32 val;
  110. int rc;
  111. data = (const struct usb_phy_data *)dev_get_driver_data(dev);
  112. if (!data)
  113. return -EINVAL;
  114. rc = regmap_read(phy->pwr_regmap, phy->pwr_reg_offset, &val);
  115. if (rc)
  116. return rc;
  117. val &= ~data->mask;
  118. if (on)
  119. val |= data->power_on;
  120. else
  121. val |= data->power_off;
  122. rc = regmap_write(phy->pwr_regmap, phy->pwr_reg_offset, val);
  123. if (rc)
  124. return rc;
  125. return 0;
  126. }
  127. static int omap_usb2_phy_init(struct phy *usb_phy)
  128. {
  129. struct udevice *dev = usb_phy->dev;
  130. struct omap_usb2_phy *priv = dev_get_priv(dev);
  131. u32 val;
  132. if (priv->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
  133. /*
  134. *
  135. * Reduce the sensitivity of internal PHY by enabling the
  136. * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
  137. * resolves issues with certain devices which can otherwise
  138. * be prone to false disconnects.
  139. *
  140. */
  141. val = readl(priv->phy_base + USB2PHY_ANA_CONFIG1);
  142. val |= USB2PHY_DISCON_BYP_LATCH;
  143. writel(val, priv->phy_base + USB2PHY_ANA_CONFIG1);
  144. }
  145. if (priv->flags & OMAP_USB2_DISABLE_CHG_DET) {
  146. val = readl(priv->phy_base + USB2PHY_CHRG_DET);
  147. val |= USB2PHY_USE_CHG_DET_REG | USB2PHY_DIS_CHG_DET;
  148. writel(val, priv->phy_base + USB2PHY_CHRG_DET);
  149. }
  150. return 0;
  151. }
  152. static int omap_usb2_phy_power_on(struct phy *usb_phy)
  153. {
  154. return omap_usb_phy_power(usb_phy, true);
  155. }
  156. static int omap_usb2_phy_power_off(struct phy *usb_phy)
  157. {
  158. return omap_usb_phy_power(usb_phy, false);
  159. }
  160. static int omap_usb2_phy_exit(struct phy *usb_phy)
  161. {
  162. return omap_usb_phy_power(usb_phy, false);
  163. }
  164. struct phy_ops omap_usb2_phy_ops = {
  165. .init = omap_usb2_phy_init,
  166. .power_on = omap_usb2_phy_power_on,
  167. .power_off = omap_usb2_phy_power_off,
  168. .exit = omap_usb2_phy_exit,
  169. };
  170. int omap_usb2_phy_probe(struct udevice *dev)
  171. {
  172. int rc;
  173. struct regmap *regmap;
  174. struct omap_usb2_phy *priv = dev_get_priv(dev);
  175. const struct usb_phy_data *data;
  176. u32 tmp[2];
  177. data = (const struct usb_phy_data *)dev_get_driver_data(dev);
  178. if (!data)
  179. return -EINVAL;
  180. priv->phy_base = dev_read_addr_ptr(dev);
  181. if (!priv->phy_base)
  182. return -EINVAL;
  183. if (data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT)
  184. priv->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
  185. /*
  186. * AM654x PG1.0 has a silicon bug that D+ is pulled high after
  187. * POR, which could cause enumeration failure with some USB hubs.
  188. * Disabling the USB2_PHY Charger Detect function will put D+
  189. * into the normal state.
  190. *
  191. * Using property "ti,dis-chg-det-quirk" in the DT usb2-phy node
  192. * to enable this workaround for AM654x PG1.0.
  193. */
  194. if (dev_read_bool(dev, "ti,dis-chg-det-quirk"))
  195. priv->flags |= OMAP_USB2_DISABLE_CHG_DET;
  196. regmap = syscon_regmap_lookup_by_phandle(dev, "syscon-phy-power");
  197. if (!IS_ERR(regmap)) {
  198. priv->pwr_regmap = regmap;
  199. rc = dev_read_u32_array(dev, "syscon-phy-power", tmp, 2);
  200. if (rc) {
  201. printf("couldn't get power reg. offset (err %d)\n", rc);
  202. return rc;
  203. }
  204. priv->pwr_reg_offset = tmp[1];
  205. return 0;
  206. }
  207. regmap = syscon_regmap_lookup_by_phandle(dev, "ctrl-module");
  208. if (!IS_ERR(regmap)) {
  209. priv->pwr_regmap = regmap;
  210. priv->pwr_reg_offset = 0;
  211. return 0;
  212. }
  213. printf("can't get regmap (err %ld)\n", PTR_ERR(regmap));
  214. return PTR_ERR(regmap);
  215. }
  216. U_BOOT_DRIVER(omap_usb2_phy) = {
  217. .name = "omap_usb2_phy",
  218. .id = UCLASS_PHY,
  219. .of_match = omap_usb2_id_table,
  220. .probe = omap_usb2_phy_probe,
  221. .ops = &omap_usb2_phy_ops,
  222. .priv_auto_alloc_size = sizeof(struct omap_usb2_phy),
  223. };