ulpi.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/usb.h>
  28. #include <linux/usb/otg.h>
  29. #include <linux/usb/ulpi.h>
  30. #define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
  31. /* ULPI hardcoded IDs, used for probing */
  32. static unsigned int ulpi_ids[] = {
  33. ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */
  34. ULPI_ID(0x0424, 0x0006), /* SMSC USB3319 */
  35. };
  36. static int ulpi_set_otg_flags(struct otg_transceiver *otg)
  37. {
  38. unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN |
  39. ULPI_OTG_CTRL_DM_PULLDOWN;
  40. if (otg->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 (otg->flags & ULPI_OTG_DP_PULLDOWN_DIS)
  47. flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN;
  48. if (otg->flags & ULPI_OTG_DM_PULLDOWN_DIS)
  49. flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN;
  50. if (otg->flags & ULPI_OTG_EXTVBUSIND)
  51. flags |= ULPI_OTG_CTRL_EXTVBUSIND;
  52. return otg_io_write(otg, flags, ULPI_OTG_CTRL);
  53. }
  54. static int ulpi_set_fc_flags(struct otg_transceiver *otg)
  55. {
  56. unsigned int flags = 0;
  57. /*
  58. * ULPI Specification rev.1.1 default
  59. * for XcvrSelect is Full Speed.
  60. */
  61. if (otg->flags & ULPI_FC_HS)
  62. flags |= ULPI_FUNC_CTRL_HIGH_SPEED;
  63. else if (otg->flags & ULPI_FC_LS)
  64. flags |= ULPI_FUNC_CTRL_LOW_SPEED;
  65. else if (otg->flags & ULPI_FC_FS4LS)
  66. flags |= ULPI_FUNC_CTRL_FS4LS;
  67. else
  68. flags |= ULPI_FUNC_CTRL_FULL_SPEED;
  69. if (otg->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 (otg->flags & ULPI_FC_OP_NODRV)
  76. flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  77. else if (otg->flags & ULPI_FC_OP_DIS_NRZI)
  78. flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI;
  79. else if (otg->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 otg_io_write(otg, flags, ULPI_FUNC_CTRL);
  89. }
  90. static int ulpi_set_ic_flags(struct otg_transceiver *otg)
  91. {
  92. unsigned int flags = 0;
  93. if (otg->flags & ULPI_IC_AUTORESUME)
  94. flags |= ULPI_IFC_CTRL_AUTORESUME;
  95. if (otg->flags & ULPI_IC_EXTVBUS_INDINV)
  96. flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS;
  97. if (otg->flags & ULPI_IC_IND_PASSTHRU)
  98. flags |= ULPI_IFC_CTRL_PASSTHRU;
  99. if (otg->flags & ULPI_IC_PROTECT_DIS)
  100. flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE;
  101. return otg_io_write(otg, flags, ULPI_IFC_CTRL);
  102. }
  103. static int ulpi_set_flags(struct otg_transceiver *otg)
  104. {
  105. int ret;
  106. ret = ulpi_set_otg_flags(otg);
  107. if (ret)
  108. return ret;
  109. ret = ulpi_set_ic_flags(otg);
  110. if (ret)
  111. return ret;
  112. return ulpi_set_fc_flags(otg);
  113. }
  114. static int ulpi_init(struct otg_transceiver *otg)
  115. {
  116. int i, vid, pid, ret;
  117. u32 ulpi_id = 0;
  118. for (i = 0; i < 4; i++) {
  119. ret = otg_io_read(otg, ULPI_PRODUCT_ID_HIGH - i);
  120. if (ret < 0)
  121. return ret;
  122. ulpi_id = (ulpi_id << 8) | ret;
  123. }
  124. vid = ulpi_id & 0xffff;
  125. pid = ulpi_id >> 16;
  126. pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
  127. for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++)
  128. if (ulpi_ids[i] == ULPI_ID(vid, pid))
  129. return ulpi_set_flags(otg);
  130. pr_err("ULPI ID does not match any known transceiver.\n");
  131. return -ENODEV;
  132. }
  133. static int ulpi_set_host(struct otg_transceiver *otg, struct usb_bus *host)
  134. {
  135. unsigned int flags = otg_io_read(otg, ULPI_IFC_CTRL);
  136. if (!host) {
  137. otg->host = NULL;
  138. return 0;
  139. }
  140. otg->host = host;
  141. flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE |
  142. ULPI_IFC_CTRL_3_PIN_SERIAL_MODE |
  143. ULPI_IFC_CTRL_CARKITMODE);
  144. if (otg->flags & ULPI_IC_6PIN_SERIAL)
  145. flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE;
  146. else if (otg->flags & ULPI_IC_3PIN_SERIAL)
  147. flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE;
  148. else if (otg->flags & ULPI_IC_CARKIT)
  149. flags |= ULPI_IFC_CTRL_CARKITMODE;
  150. return otg_io_write(otg, flags, ULPI_IFC_CTRL);
  151. }
  152. static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
  153. {
  154. unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL);
  155. flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
  156. if (on) {
  157. if (otg->flags & ULPI_OTG_DRVVBUS)
  158. flags |= ULPI_OTG_CTRL_DRVVBUS;
  159. if (otg->flags & ULPI_OTG_DRVVBUS_EXT)
  160. flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
  161. }
  162. return otg_io_write(otg, flags, ULPI_OTG_CTRL);
  163. }
  164. struct otg_transceiver *
  165. otg_ulpi_create(struct otg_io_access_ops *ops,
  166. unsigned int flags)
  167. {
  168. struct otg_transceiver *otg;
  169. otg = kzalloc(sizeof(*otg), GFP_KERNEL);
  170. if (!otg)
  171. return NULL;
  172. otg->label = "ULPI";
  173. otg->flags = flags;
  174. otg->io_ops = ops;
  175. otg->init = ulpi_init;
  176. otg->set_host = ulpi_set_host;
  177. otg->set_vbus = ulpi_set_vbus;
  178. return otg;
  179. }
  180. EXPORT_SYMBOL_GPL(otg_ulpi_create);