phy-ulpi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Generic ULPI USB transceiver support
  4. *
  5. * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de>
  6. *
  7. * Based on sources from
  8. *
  9. * Sascha Hauer <s.hauer@pengutronix.de>
  10. * Freescale Semiconductors
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/export.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb/otg.h>
  17. #include <linux/usb/ulpi.h>
  18. struct ulpi_info {
  19. unsigned int id;
  20. char *name;
  21. };
  22. #define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
  23. #define ULPI_INFO(_id, _name) \
  24. { \
  25. .id = (_id), \
  26. .name = (_name), \
  27. }
  28. /* ULPI hardcoded IDs, used for probing */
  29. static struct ulpi_info ulpi_ids[] = {
  30. ULPI_INFO(ULPI_ID(0x04cc, 0x1504), "NXP ISP1504"),
  31. ULPI_INFO(ULPI_ID(0x0424, 0x0006), "SMSC USB331x"),
  32. ULPI_INFO(ULPI_ID(0x0424, 0x0007), "SMSC USB3320"),
  33. ULPI_INFO(ULPI_ID(0x0424, 0x0009), "SMSC USB334x"),
  34. ULPI_INFO(ULPI_ID(0x0451, 0x1507), "TI TUSB1210"),
  35. };
  36. static int ulpi_set_otg_flags(struct usb_phy *phy)
  37. {
  38. unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN |
  39. ULPI_OTG_CTRL_DM_PULLDOWN;
  40. if (phy->flags & ULPI_OTG_ID_PULLUP)
  41. flags |= ULPI_OTG_CTRL_ID_PULLUP;
  42. /*
  43. * ULPI Specification rev.1.1 default
  44. * for Dp/DmPulldown is enabled.
  45. */
  46. if (phy->flags & ULPI_OTG_DP_PULLDOWN_DIS)
  47. flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN;
  48. if (phy->flags & ULPI_OTG_DM_PULLDOWN_DIS)
  49. flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN;
  50. if (phy->flags & ULPI_OTG_EXTVBUSIND)
  51. flags |= ULPI_OTG_CTRL_EXTVBUSIND;
  52. return usb_phy_io_write(phy, flags, ULPI_OTG_CTRL);
  53. }
  54. static int ulpi_set_fc_flags(struct usb_phy *phy)
  55. {
  56. unsigned int flags = 0;
  57. /*
  58. * ULPI Specification rev.1.1 default
  59. * for XcvrSelect is Full Speed.
  60. */
  61. if (phy->flags & ULPI_FC_HS)
  62. flags |= ULPI_FUNC_CTRL_HIGH_SPEED;
  63. else if (phy->flags & ULPI_FC_LS)
  64. flags |= ULPI_FUNC_CTRL_LOW_SPEED;
  65. else if (phy->flags & ULPI_FC_FS4LS)
  66. flags |= ULPI_FUNC_CTRL_FS4LS;
  67. else
  68. flags |= ULPI_FUNC_CTRL_FULL_SPEED;
  69. if (phy->flags & ULPI_FC_TERMSEL)
  70. flags |= ULPI_FUNC_CTRL_TERMSELECT;
  71. /*
  72. * ULPI Specification rev.1.1 default
  73. * for OpMode is Normal Operation.
  74. */
  75. if (phy->flags & ULPI_FC_OP_NODRV)
  76. flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  77. else if (phy->flags & ULPI_FC_OP_DIS_NRZI)
  78. flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI;
  79. else if (phy->flags & ULPI_FC_OP_NSYNC_NEOP)
  80. flags |= ULPI_FUNC_CTRL_OPMODE_NOSYNC_NOEOP;
  81. else
  82. flags |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  83. /*
  84. * ULPI Specification rev.1.1 default
  85. * for SuspendM is Powered.
  86. */
  87. flags |= ULPI_FUNC_CTRL_SUSPENDM;
  88. return usb_phy_io_write(phy, flags, ULPI_FUNC_CTRL);
  89. }
  90. static int ulpi_set_ic_flags(struct usb_phy *phy)
  91. {
  92. unsigned int flags = 0;
  93. if (phy->flags & ULPI_IC_AUTORESUME)
  94. flags |= ULPI_IFC_CTRL_AUTORESUME;
  95. if (phy->flags & ULPI_IC_EXTVBUS_INDINV)
  96. flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS;
  97. if (phy->flags & ULPI_IC_IND_PASSTHRU)
  98. flags |= ULPI_IFC_CTRL_PASSTHRU;
  99. if (phy->flags & ULPI_IC_PROTECT_DIS)
  100. flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE;
  101. return usb_phy_io_write(phy, flags, ULPI_IFC_CTRL);
  102. }
  103. static int ulpi_set_flags(struct usb_phy *phy)
  104. {
  105. int ret;
  106. ret = ulpi_set_otg_flags(phy);
  107. if (ret)
  108. return ret;
  109. ret = ulpi_set_ic_flags(phy);
  110. if (ret)
  111. return ret;
  112. return ulpi_set_fc_flags(phy);
  113. }
  114. static int ulpi_check_integrity(struct usb_phy *phy)
  115. {
  116. int ret, i;
  117. unsigned int val = 0x55;
  118. for (i = 0; i < 2; i++) {
  119. ret = usb_phy_io_write(phy, val, ULPI_SCRATCH);
  120. if (ret < 0)
  121. return ret;
  122. ret = usb_phy_io_read(phy, ULPI_SCRATCH);
  123. if (ret < 0)
  124. return ret;
  125. if (ret != val) {
  126. pr_err("ULPI integrity check: failed!");
  127. return -ENODEV;
  128. }
  129. val = val << 1;
  130. }
  131. pr_info("ULPI integrity check: passed.\n");
  132. return 0;
  133. }
  134. static int ulpi_init(struct usb_phy *phy)
  135. {
  136. int i, vid, pid, ret;
  137. u32 ulpi_id = 0;
  138. for (i = 0; i < 4; i++) {
  139. ret = usb_phy_io_read(phy, ULPI_PRODUCT_ID_HIGH - i);
  140. if (ret < 0)
  141. return ret;
  142. ulpi_id = (ulpi_id << 8) | ret;
  143. }
  144. vid = ulpi_id & 0xffff;
  145. pid = ulpi_id >> 16;
  146. pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
  147. for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++) {
  148. if (ulpi_ids[i].id == ULPI_ID(vid, pid)) {
  149. pr_info("Found %s ULPI transceiver.\n",
  150. ulpi_ids[i].name);
  151. break;
  152. }
  153. }
  154. ret = ulpi_check_integrity(phy);
  155. if (ret)
  156. return ret;
  157. return ulpi_set_flags(phy);
  158. }
  159. static int ulpi_set_host(struct usb_otg *otg, struct usb_bus *host)
  160. {
  161. struct usb_phy *phy = otg->usb_phy;
  162. unsigned int flags = usb_phy_io_read(phy, ULPI_IFC_CTRL);
  163. if (!host) {
  164. otg->host = NULL;
  165. return 0;
  166. }
  167. otg->host = host;
  168. flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE |
  169. ULPI_IFC_CTRL_3_PIN_SERIAL_MODE |
  170. ULPI_IFC_CTRL_CARKITMODE);
  171. if (phy->flags & ULPI_IC_6PIN_SERIAL)
  172. flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE;
  173. else if (phy->flags & ULPI_IC_3PIN_SERIAL)
  174. flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE;
  175. else if (phy->flags & ULPI_IC_CARKIT)
  176. flags |= ULPI_IFC_CTRL_CARKITMODE;
  177. return usb_phy_io_write(phy, flags, ULPI_IFC_CTRL);
  178. }
  179. static int ulpi_set_vbus(struct usb_otg *otg, bool on)
  180. {
  181. struct usb_phy *phy = otg->usb_phy;
  182. unsigned int flags = usb_phy_io_read(phy, ULPI_OTG_CTRL);
  183. flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
  184. if (on) {
  185. if (phy->flags & ULPI_OTG_DRVVBUS)
  186. flags |= ULPI_OTG_CTRL_DRVVBUS;
  187. if (phy->flags & ULPI_OTG_DRVVBUS_EXT)
  188. flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
  189. }
  190. return usb_phy_io_write(phy, flags, ULPI_OTG_CTRL);
  191. }
  192. struct usb_phy *
  193. otg_ulpi_create(struct usb_phy_io_ops *ops,
  194. unsigned int flags)
  195. {
  196. struct usb_phy *phy;
  197. struct usb_otg *otg;
  198. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  199. if (!phy)
  200. return NULL;
  201. otg = kzalloc(sizeof(*otg), GFP_KERNEL);
  202. if (!otg) {
  203. kfree(phy);
  204. return NULL;
  205. }
  206. phy->label = "ULPI";
  207. phy->flags = flags;
  208. phy->io_ops = ops;
  209. phy->otg = otg;
  210. phy->init = ulpi_init;
  211. otg->usb_phy = phy;
  212. otg->set_host = ulpi_set_host;
  213. otg->set_vbus = ulpi_set_vbus;
  214. return phy;
  215. }
  216. EXPORT_SYMBOL_GPL(otg_ulpi_create);