ulpi.c 5.6 KB

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