ulpi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. */
  30. #include <common.h>
  31. #include <exports.h>
  32. #include <usb/ulpi.h>
  33. #define ULPI_ID_REGS_COUNT 4
  34. #define ULPI_TEST_VALUE 0x55 /* 0x55 == 0b01010101 */
  35. static struct ulpi_regs *ulpi = (struct ulpi_regs *)0;
  36. static int ulpi_integrity_check(struct ulpi_viewport *ulpi_vp)
  37. {
  38. u32 val, tval = ULPI_TEST_VALUE;
  39. int err, i;
  40. /* Use the 'special' test value to check all bits */
  41. for (i = 0; i < 2; i++, tval <<= 1) {
  42. err = ulpi_write(ulpi_vp, &ulpi->scratch, tval);
  43. if (err)
  44. return err;
  45. val = ulpi_read(ulpi_vp, &ulpi->scratch);
  46. if (val != tval) {
  47. printf("ULPI integrity check failed\n");
  48. return val;
  49. }
  50. }
  51. return 0;
  52. }
  53. int ulpi_init(struct ulpi_viewport *ulpi_vp)
  54. {
  55. u32 val, id = 0;
  56. u8 *reg = &ulpi->product_id_high;
  57. int i;
  58. /* Assemble ID from four ULPI ID registers (8 bits each). */
  59. for (i = 0; i < ULPI_ID_REGS_COUNT; i++) {
  60. val = ulpi_read(ulpi_vp, reg - i);
  61. if (val == ULPI_ERROR)
  62. return val;
  63. id = (id << 8) | val;
  64. }
  65. /* Split ID into vendor and product ID. */
  66. debug("ULPI transceiver ID 0x%04x:0x%04x\n", id >> 16, id & 0xffff);
  67. return ulpi_integrity_check(ulpi_vp);
  68. }
  69. int ulpi_select_transceiver(struct ulpi_viewport *ulpi_vp, unsigned speed)
  70. {
  71. u32 tspeed = ULPI_FC_FULL_SPEED;
  72. u32 val;
  73. switch (speed) {
  74. case ULPI_FC_HIGH_SPEED:
  75. case ULPI_FC_FULL_SPEED:
  76. case ULPI_FC_LOW_SPEED:
  77. case ULPI_FC_FS4LS:
  78. tspeed = speed;
  79. break;
  80. default:
  81. printf("ULPI: %s: wrong transceiver speed specified: %u, "
  82. "falling back to full speed\n", __func__, speed);
  83. }
  84. val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
  85. if (val == ULPI_ERROR)
  86. return val;
  87. /* clear the previous speed setting */
  88. val = (val & ~ULPI_FC_XCVRSEL_MASK) | tspeed;
  89. return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val);
  90. }
  91. int ulpi_set_vbus(struct ulpi_viewport *ulpi_vp, int on, int ext_power)
  92. {
  93. u32 flags = ULPI_OTG_DRVVBUS;
  94. u8 *reg = on ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
  95. if (ext_power)
  96. flags |= ULPI_OTG_DRVVBUS_EXT;
  97. return ulpi_write(ulpi_vp, reg, flags);
  98. }
  99. int ulpi_set_vbus_indicator(struct ulpi_viewport *ulpi_vp, int external,
  100. int passthu, int complement)
  101. {
  102. u32 flags, val;
  103. u8 *reg;
  104. reg = external ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
  105. val = ulpi_write(ulpi_vp, reg, ULPI_OTG_EXTVBUSIND);
  106. if (val)
  107. return val;
  108. flags = passthu ? ULPI_IFACE_PASSTHRU : 0;
  109. flags |= complement ? ULPI_IFACE_EXTVBUS_COMPLEMENT : 0;
  110. val = ulpi_read(ulpi_vp, &ulpi->iface_ctrl);
  111. if (val == ULPI_ERROR)
  112. return val;
  113. val = val & ~(ULPI_IFACE_PASSTHRU & ULPI_IFACE_EXTVBUS_COMPLEMENT);
  114. val |= flags;
  115. val = ulpi_write(ulpi_vp, &ulpi->iface_ctrl, val);
  116. if (val)
  117. return val;
  118. return 0;
  119. }
  120. int ulpi_set_pd(struct ulpi_viewport *ulpi_vp, int enable)
  121. {
  122. u32 val = ULPI_OTG_DP_PULLDOWN | ULPI_OTG_DM_PULLDOWN;
  123. u8 *reg = enable ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
  124. return ulpi_write(ulpi_vp, reg, val);
  125. }
  126. int ulpi_opmode_sel(struct ulpi_viewport *ulpi_vp, unsigned opmode)
  127. {
  128. u32 topmode = ULPI_FC_OPMODE_NORMAL;
  129. u32 val;
  130. switch (opmode) {
  131. case ULPI_FC_OPMODE_NORMAL:
  132. case ULPI_FC_OPMODE_NONDRIVING:
  133. case ULPI_FC_OPMODE_DISABLE_NRZI:
  134. case ULPI_FC_OPMODE_NOSYNC_NOEOP:
  135. topmode = opmode;
  136. break;
  137. default:
  138. printf("ULPI: %s: wrong OpMode specified: %u, "
  139. "falling back to OpMode Normal\n", __func__, opmode);
  140. }
  141. val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
  142. if (val == ULPI_ERROR)
  143. return val;
  144. /* clear the previous opmode setting */
  145. val = (val & ~ULPI_FC_OPMODE_MASK) | topmode;
  146. return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val);
  147. }
  148. int ulpi_serial_mode_enable(struct ulpi_viewport *ulpi_vp, unsigned smode)
  149. {
  150. switch (smode) {
  151. case ULPI_IFACE_6_PIN_SERIAL_MODE:
  152. case ULPI_IFACE_3_PIN_SERIAL_MODE:
  153. break;
  154. default:
  155. printf("ULPI: %s: unrecognized Serial Mode specified: %u\n",
  156. __func__, smode);
  157. return ULPI_ERROR;
  158. }
  159. return ulpi_write(ulpi_vp, &ulpi->iface_ctrl_set, smode);
  160. }
  161. int ulpi_suspend(struct ulpi_viewport *ulpi_vp)
  162. {
  163. int err;
  164. err = ulpi_write(ulpi_vp, &ulpi->function_ctrl_clear,
  165. ULPI_FC_SUSPENDM);
  166. if (err)
  167. printf("ULPI: %s: failed writing the suspend bit\n", __func__);
  168. return err;
  169. }
  170. /*
  171. * Wait for ULPI PHY reset to complete.
  172. * Actual wait for reset must be done in a view port specific way,
  173. * because it involves checking the DIR line.
  174. */
  175. static int __ulpi_reset_wait(struct ulpi_viewport *ulpi_vp)
  176. {
  177. u32 val;
  178. int timeout = CONFIG_USB_ULPI_TIMEOUT;
  179. /* Wait for the RESET bit to become zero */
  180. while (--timeout) {
  181. /*
  182. * This function is generic and suppose to work
  183. * with any viewport, so we cheat here and don't check
  184. * for the error of ulpi_read(), if there is one, then
  185. * there will be a timeout.
  186. */
  187. val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
  188. if (!(val & ULPI_FC_RESET))
  189. return 0;
  190. udelay(1);
  191. }
  192. printf("ULPI: %s: reset timed out\n", __func__);
  193. return ULPI_ERROR;
  194. }
  195. int ulpi_reset_wait(struct ulpi_viewport *ulpi_vp)
  196. __attribute__((weak, alias("__ulpi_reset_wait")));
  197. int ulpi_reset(struct ulpi_viewport *ulpi_vp)
  198. {
  199. int err;
  200. err = ulpi_write(ulpi_vp,
  201. &ulpi->function_ctrl_set, ULPI_FC_RESET);
  202. if (err) {
  203. printf("ULPI: %s: failed writing reset bit\n", __func__);
  204. return err;
  205. }
  206. return ulpi_reset_wait(ulpi_vp);
  207. }