ulpi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C) 2011 Jana Rapava <fermata7@gmail.com>
  3. * Copyright (C) 2011 CompuLab, Ltd. <www.compulab.co.il>
  4. *
  5. * Authors: Jana Rapava <fermata7@gmail.com>
  6. * Igor Grinberg <grinberg@compulab.co.il>
  7. *
  8. * Based on:
  9. * linux/drivers/usb/otg/ulpi.c
  10. * Generic ULPI USB transceiver support
  11. *
  12. * Original Copyright follow:
  13. * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de>
  14. *
  15. * Based on sources from
  16. *
  17. * Sascha Hauer <s.hauer@pengutronix.de>
  18. * Freescale Semiconductors
  19. *
  20. * SPDX-License-Identifier: GPL-2.0+
  21. */
  22. #include <common.h>
  23. #include <exports.h>
  24. #include <usb/ulpi.h>
  25. #define ULPI_ID_REGS_COUNT 4
  26. #define ULPI_TEST_VALUE 0x55 /* 0x55 == 0b01010101 */
  27. static struct ulpi_regs *ulpi = (struct ulpi_regs *)0;
  28. static int ulpi_integrity_check(struct ulpi_viewport *ulpi_vp)
  29. {
  30. u32 val, tval = ULPI_TEST_VALUE;
  31. int err, i;
  32. /* Use the 'special' test value to check all bits */
  33. for (i = 0; i < 2; i++, tval <<= 1) {
  34. err = ulpi_write(ulpi_vp, &ulpi->scratch, tval);
  35. if (err)
  36. return err;
  37. val = ulpi_read(ulpi_vp, &ulpi->scratch);
  38. if (val != tval) {
  39. printf("ULPI integrity check failed\n");
  40. return val;
  41. }
  42. }
  43. return 0;
  44. }
  45. int ulpi_init(struct ulpi_viewport *ulpi_vp)
  46. {
  47. u32 val, id = 0;
  48. u8 *reg = &ulpi->product_id_high;
  49. int i;
  50. /* Assemble ID from four ULPI ID registers (8 bits each). */
  51. for (i = 0; i < ULPI_ID_REGS_COUNT; i++) {
  52. val = ulpi_read(ulpi_vp, reg - i);
  53. if (val == ULPI_ERROR)
  54. return val;
  55. id = (id << 8) | val;
  56. }
  57. /* Split ID into vendor and product ID. */
  58. debug("ULPI transceiver ID 0x%04x:0x%04x\n", id >> 16, id & 0xffff);
  59. return ulpi_integrity_check(ulpi_vp);
  60. }
  61. int ulpi_select_transceiver(struct ulpi_viewport *ulpi_vp, unsigned speed)
  62. {
  63. u32 tspeed = ULPI_FC_FULL_SPEED;
  64. u32 val;
  65. switch (speed) {
  66. case ULPI_FC_HIGH_SPEED:
  67. case ULPI_FC_FULL_SPEED:
  68. case ULPI_FC_LOW_SPEED:
  69. case ULPI_FC_FS4LS:
  70. tspeed = speed;
  71. break;
  72. default:
  73. printf("ULPI: %s: wrong transceiver speed specified: %u, "
  74. "falling back to full speed\n", __func__, speed);
  75. }
  76. val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
  77. if (val == ULPI_ERROR)
  78. return val;
  79. /* clear the previous speed setting */
  80. val = (val & ~ULPI_FC_XCVRSEL_MASK) | tspeed;
  81. return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val);
  82. }
  83. int ulpi_set_vbus(struct ulpi_viewport *ulpi_vp, int on, int ext_power)
  84. {
  85. u32 flags = ULPI_OTG_DRVVBUS;
  86. u8 *reg = on ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
  87. if (ext_power)
  88. flags |= ULPI_OTG_DRVVBUS_EXT;
  89. return ulpi_write(ulpi_vp, reg, flags);
  90. }
  91. int ulpi_set_vbus_indicator(struct ulpi_viewport *ulpi_vp, int external,
  92. int passthu, int complement)
  93. {
  94. u32 flags, val;
  95. u8 *reg;
  96. reg = external ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
  97. val = ulpi_write(ulpi_vp, reg, ULPI_OTG_EXTVBUSIND);
  98. if (val)
  99. return val;
  100. flags = passthu ? ULPI_IFACE_PASSTHRU : 0;
  101. flags |= complement ? ULPI_IFACE_EXTVBUS_COMPLEMENT : 0;
  102. val = ulpi_read(ulpi_vp, &ulpi->iface_ctrl);
  103. if (val == ULPI_ERROR)
  104. return val;
  105. val = val & ~(ULPI_IFACE_PASSTHRU & ULPI_IFACE_EXTVBUS_COMPLEMENT);
  106. val |= flags;
  107. val = ulpi_write(ulpi_vp, &ulpi->iface_ctrl, val);
  108. if (val)
  109. return val;
  110. return 0;
  111. }
  112. int ulpi_set_pd(struct ulpi_viewport *ulpi_vp, int enable)
  113. {
  114. u32 val = ULPI_OTG_DP_PULLDOWN | ULPI_OTG_DM_PULLDOWN;
  115. u8 *reg = enable ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
  116. return ulpi_write(ulpi_vp, reg, val);
  117. }
  118. int ulpi_opmode_sel(struct ulpi_viewport *ulpi_vp, unsigned opmode)
  119. {
  120. u32 topmode = ULPI_FC_OPMODE_NORMAL;
  121. u32 val;
  122. switch (opmode) {
  123. case ULPI_FC_OPMODE_NORMAL:
  124. case ULPI_FC_OPMODE_NONDRIVING:
  125. case ULPI_FC_OPMODE_DISABLE_NRZI:
  126. case ULPI_FC_OPMODE_NOSYNC_NOEOP:
  127. topmode = opmode;
  128. break;
  129. default:
  130. printf("ULPI: %s: wrong OpMode specified: %u, "
  131. "falling back to OpMode Normal\n", __func__, opmode);
  132. }
  133. val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
  134. if (val == ULPI_ERROR)
  135. return val;
  136. /* clear the previous opmode setting */
  137. val = (val & ~ULPI_FC_OPMODE_MASK) | topmode;
  138. return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val);
  139. }
  140. int ulpi_serial_mode_enable(struct ulpi_viewport *ulpi_vp, unsigned smode)
  141. {
  142. switch (smode) {
  143. case ULPI_IFACE_6_PIN_SERIAL_MODE:
  144. case ULPI_IFACE_3_PIN_SERIAL_MODE:
  145. break;
  146. default:
  147. printf("ULPI: %s: unrecognized Serial Mode specified: %u\n",
  148. __func__, smode);
  149. return ULPI_ERROR;
  150. }
  151. return ulpi_write(ulpi_vp, &ulpi->iface_ctrl_set, smode);
  152. }
  153. int ulpi_suspend(struct ulpi_viewport *ulpi_vp)
  154. {
  155. int err;
  156. err = ulpi_write(ulpi_vp, &ulpi->function_ctrl_clear,
  157. ULPI_FC_SUSPENDM);
  158. if (err)
  159. printf("ULPI: %s: failed writing the suspend bit\n", __func__);
  160. return err;
  161. }
  162. /*
  163. * Wait for ULPI PHY reset to complete.
  164. * Actual wait for reset must be done in a view port specific way,
  165. * because it involves checking the DIR line.
  166. */
  167. static int __ulpi_reset_wait(struct ulpi_viewport *ulpi_vp)
  168. {
  169. u32 val;
  170. int timeout = CONFIG_USB_ULPI_TIMEOUT;
  171. /* Wait for the RESET bit to become zero */
  172. while (--timeout) {
  173. /*
  174. * This function is generic and suppose to work
  175. * with any viewport, so we cheat here and don't check
  176. * for the error of ulpi_read(), if there is one, then
  177. * there will be a timeout.
  178. */
  179. val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
  180. if (!(val & ULPI_FC_RESET))
  181. return 0;
  182. udelay(1);
  183. }
  184. printf("ULPI: %s: reset timed out\n", __func__);
  185. return ULPI_ERROR;
  186. }
  187. int ulpi_reset_wait(struct ulpi_viewport *ulpi_vp)
  188. __attribute__((weak, alias("__ulpi_reset_wait")));
  189. int ulpi_reset(struct ulpi_viewport *ulpi_vp)
  190. {
  191. int err;
  192. err = ulpi_write(ulpi_vp,
  193. &ulpi->function_ctrl_set, ULPI_FC_RESET);
  194. if (err) {
  195. printf("ULPI: %s: failed writing reset bit\n", __func__);
  196. return err;
  197. }
  198. return ulpi_reset_wait(ulpi_vp);
  199. }