ulpi.c 6.3 KB

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