core.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * core.c - DesignWare HS OTG Controller common routines
  3. *
  4. * Copyright (C) 2004-2013 Synopsys, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the above-listed copyright holders may not be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * ALTERNATIVELY, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at your option) any
  22. * later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * The Core code provides basic services for accessing and managing the
  38. * DWC_otg hardware. These services are used by both the Host Controller
  39. * Driver and the Peripheral Controller Driver.
  40. */
  41. #include "usb_os_adapter.h"
  42. #include "trace.h"
  43. #include <asm/dma-mapping.h>
  44. #include <linux/usb/ch9.h>
  45. #include <linux/usb/gadget.h>
  46. #include "core.h"
  47. #include "hcd.h"
  48. /**
  49. * dwc2_wait_for_mode() - Waits for the controller mode.
  50. * @hsotg: Programming view of the DWC_otg controller.
  51. * @host_mode: If true, waits for host mode, otherwise device mode.
  52. */
  53. static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
  54. bool host_mode)
  55. {
  56. uint32_t tick = get_timer(0);
  57. unsigned int timeout = 110;//ms
  58. dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
  59. host_mode ? "host" : "device");
  60. while (1) {
  61. __s64 ms;
  62. if (dwc2_is_host_mode(hsotg) == host_mode) {
  63. dev_vdbg(hsotg->dev, "%s mode set\n",
  64. host_mode ? "Host" : "Device");
  65. break;
  66. }
  67. ms = get_timer(tick);
  68. if (ms >= (__s64)timeout) {
  69. dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
  70. __func__, host_mode ? "host" : "device");
  71. break;
  72. }
  73. //vTaskDelay(2000 / portTICK_RATE_MS);
  74. mdelay(10);
  75. }
  76. }
  77. /*
  78. * Do core a soft reset of the core. Be careful with this because it
  79. * resets all the internal state machines of the core.
  80. */
  81. int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait)
  82. {
  83. u32 greset;
  84. int count = 0;
  85. bool wait_for_host_mode = false;
  86. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  87. /*
  88. * If the current mode is host, either due to the force mode
  89. * bit being set (which persists after core reset) or the
  90. * connector id pin, a core soft reset will temporarily reset
  91. * the mode to device. A delay from the IDDIG debounce filter
  92. * will occur before going back to host mode.
  93. *
  94. * Determine whether we will go back into host mode after a
  95. * reset and account for this delay after the reset.
  96. */
  97. /* Core Soft Reset */
  98. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  99. greset |= GRSTCTL_CSFTRST;
  100. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  101. do {
  102. udelay(1);
  103. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  104. if (++count > 50) {
  105. dev_warn(hsotg->dev,
  106. "%s() HANG! Soft Reset GRSTCTL=%0x\n",
  107. __func__, greset);
  108. return -EBUSY;
  109. }
  110. } while (greset & GRSTCTL_CSFTRST);
  111. /* Wait for AHB master IDLE state */
  112. count = 0;
  113. do {
  114. udelay(1);
  115. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  116. if (++count > 50) {
  117. dev_warn(hsotg->dev,
  118. "%s() HANG! AHB Idle GRSTCTL=%0x\n",
  119. __func__, greset);
  120. return -EBUSY;
  121. }
  122. } while (!(greset & GRSTCTL_AHBIDLE));
  123. if (wait_for_host_mode && !skip_wait)
  124. dwc2_wait_for_mode(hsotg, true);
  125. return 0;
  126. }
  127. /*
  128. * Sets or clears force mode based on the dr_mode parameter.
  129. */
  130. void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
  131. {
  132. switch (hsotg->dr_mode) {
  133. case USB_DR_MODE_HOST:
  134. /*
  135. * NOTE: This is required for some rockchip soc based
  136. * platforms on their host-only dwc2.
  137. */
  138. msleep(50);
  139. break;
  140. default:
  141. dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
  142. __func__, hsotg->dr_mode);
  143. break;
  144. }
  145. }
  146. /*
  147. * Do core a soft reset of the core. Be careful with this because it
  148. * resets all the internal state machines of the core.
  149. *
  150. * Additionally this will apply force mode as per the hsotg->dr_mode
  151. * parameter.
  152. */
  153. int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg *hsotg)
  154. {
  155. int retval;
  156. retval = dwc2_core_reset(hsotg, false);
  157. if (retval)
  158. return retval;
  159. dwc2_force_dr_mode(hsotg);
  160. return 0;
  161. }
  162. /**
  163. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  164. *
  165. * @hsotg: Programming view of DWC_otg controller
  166. * @num: Tx FIFO to flush
  167. */
  168. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  169. {
  170. u32 greset;
  171. int count = 0;
  172. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  173. greset = GRSTCTL_TXFFLSH;
  174. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  175. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  176. do {
  177. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  178. if (++count > 10000) {
  179. dev_warn(hsotg->dev,
  180. "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  181. __func__, greset,
  182. dwc2_readl(hsotg->regs + GNPTXSTS));
  183. break;
  184. }
  185. udelay(1);
  186. } while (greset & GRSTCTL_TXFFLSH);
  187. /* Wait for at least 3 PHY Clocks */
  188. udelay(1);
  189. }
  190. /**
  191. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  192. *
  193. * @hsotg: Programming view of DWC_otg controller
  194. */
  195. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  196. {
  197. u32 greset;
  198. int count = 0;
  199. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  200. greset = GRSTCTL_RXFFLSH;
  201. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  202. do {
  203. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  204. if (++count > 10000) {
  205. dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
  206. __func__, greset);
  207. break;
  208. }
  209. udelay(1);
  210. } while (greset & GRSTCTL_RXFFLSH);
  211. /* Wait for at least 3 PHY Clocks */
  212. udelay(1);
  213. }
  214. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  215. {
  216. if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
  217. return false;
  218. else
  219. return true;
  220. }
  221. /**
  222. * dwc2_enable_global_interrupts() - Enables the controller's Global
  223. * Interrupt in the AHB Config register
  224. *
  225. * @hsotg: Programming view of DWC_otg controller
  226. */
  227. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  228. {
  229. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  230. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  231. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  232. }
  233. /**
  234. * dwc2_disable_global_interrupts() - Disables the controller's Global
  235. * Interrupt in the AHB Config register
  236. *
  237. * @hsotg: Programming view of DWC_otg controller
  238. */
  239. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  240. {
  241. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  242. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  243. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  244. }
  245. /* Returns the controller's GHWCFG2.OTG_MODE. */
  246. unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg)
  247. {
  248. u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
  249. return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  250. GHWCFG2_OP_MODE_SHIFT;
  251. }
  252. /* Returns true if the controller is host-only. */
  253. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
  254. {
  255. unsigned int op_mode = dwc2_op_mode(hsotg);
  256. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
  257. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
  258. }