ulpi.c 6.1 KB

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