phy-ulpi.c 6.5 KB

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