omap-usb2-phy.c 6.4 KB

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