core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. #define VERBOSE_DEBUG
  42. #include "usb_os_adapter.h"
  43. #include "trace.h"
  44. #include <asm/dma-mapping.h>
  45. #include <linux/usb/ch9.h>
  46. #include <linux/usb/gadget.h>
  47. #include "core.h"
  48. #include "hcd.h"
  49. /**
  50. * dwc2_backup_global_registers() - Backup global controller registers.
  51. * When suspending usb bus, registers needs to be backuped
  52. * if controller power is disabled once suspended.
  53. *
  54. * @hsotg: Programming view of the DWC_otg controller
  55. */
  56. static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
  57. {
  58. struct dwc2_gregs_backup *gr;
  59. int i;
  60. /* Backup global regs */
  61. gr = &hsotg->gr_backup;
  62. gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  63. gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  64. gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  65. gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  66. gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  67. gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  68. gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
  69. gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
  70. for (i = 0; i < MAX_EPS_CHANNELS; i++)
  71. gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
  72. gr->valid = true;
  73. return 0;
  74. }
  75. /**
  76. * dwc2_restore_global_registers() - Restore controller global registers.
  77. * When resuming usb bus, device registers needs to be restored
  78. * if controller power were disabled.
  79. *
  80. * @hsotg: Programming view of the DWC_otg controller
  81. */
  82. static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
  83. {
  84. struct dwc2_gregs_backup *gr;
  85. int i;
  86. dev_dbg(hsotg->dev, "%s\n", __func__);
  87. /* Restore global regs */
  88. gr = &hsotg->gr_backup;
  89. if (!gr->valid) {
  90. dev_err(hsotg->dev, "%s: no global registers to restore\n",
  91. __func__);
  92. return -EINVAL;
  93. }
  94. gr->valid = false;
  95. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  96. dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
  97. dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
  98. dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
  99. dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
  100. dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
  101. dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
  102. dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
  103. dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
  104. for (i = 0; i < MAX_EPS_CHANNELS; i++)
  105. dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
  106. return 0;
  107. }
  108. /**
  109. * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
  110. *
  111. * @hsotg: Programming view of the DWC_otg controller
  112. * @restore: Controller registers need to be restored
  113. */
  114. int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
  115. {
  116. u32 pcgcctl;
  117. int ret = 0;
  118. if (!hsotg->params.hibernation)
  119. return -ENOTSUPP;
  120. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  121. pcgcctl &= ~PCGCTL_STOPPCLK;
  122. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  123. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  124. pcgcctl &= ~PCGCTL_PWRCLMP;
  125. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  126. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  127. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  128. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  129. udelay(100);
  130. if (restore) {
  131. ret = dwc2_restore_global_registers(hsotg);
  132. if (ret) {
  133. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  134. __func__);
  135. return ret;
  136. }
  137. if (dwc2_is_host_mode(hsotg)) {
  138. ret = dwc2_restore_host_registers(hsotg);
  139. if (ret) {
  140. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  141. __func__);
  142. return ret;
  143. }
  144. } else {
  145. ret = dwc2_restore_device_registers(hsotg);
  146. if (ret) {
  147. dev_err(hsotg->dev, "%s: failed to restore device registers\n",
  148. __func__);
  149. return ret;
  150. }
  151. }
  152. }
  153. return ret;
  154. }
  155. /**
  156. * dwc2_enter_hibernation() - Put controller in Partial Power Down.
  157. *
  158. * @hsotg: Programming view of the DWC_otg controller
  159. */
  160. int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
  161. {
  162. u32 pcgcctl;
  163. int ret = 0;
  164. if (!hsotg->params.hibernation)
  165. return -ENOTSUPP;
  166. /* Backup all registers */
  167. ret = dwc2_backup_global_registers(hsotg);
  168. if (ret) {
  169. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  170. __func__);
  171. return ret;
  172. }
  173. if (dwc2_is_host_mode(hsotg)) {
  174. ret = dwc2_backup_host_registers(hsotg);
  175. if (ret) {
  176. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  177. __func__);
  178. return ret;
  179. }
  180. } else {
  181. ret = dwc2_backup_device_registers(hsotg);
  182. if (ret) {
  183. dev_err(hsotg->dev, "%s: failed to backup device registers\n",
  184. __func__);
  185. return ret;
  186. }
  187. }
  188. /*
  189. * Clear any pending interrupts since dwc2 will not be able to
  190. * clear them after entering hibernation.
  191. */
  192. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  193. /* Put the controller in low power state */
  194. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  195. pcgcctl |= PCGCTL_PWRCLMP;
  196. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  197. udelay(20);
  198. pcgcctl |= PCGCTL_RSTPDWNMODULE;
  199. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  200. udelay(20);
  201. pcgcctl |= PCGCTL_STOPPCLK;
  202. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  203. return ret;
  204. }
  205. /**
  206. * dwc2_wait_for_mode() - Waits for the controller mode.
  207. * @hsotg: Programming view of the DWC_otg controller.
  208. * @host_mode: If true, waits for host mode, otherwise device mode.
  209. */
  210. static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
  211. bool host_mode)
  212. {
  213. uint32_t tick = xTaskGetTickCount();
  214. unsigned int timeout = 110;//ms
  215. dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
  216. host_mode ? "host" : "device");
  217. timeout += tick;
  218. while (1) {
  219. __s64 ms;
  220. if (dwc2_is_host_mode(hsotg) == host_mode) {
  221. dev_vdbg(hsotg->dev, "%s mode set\n",
  222. host_mode ? "Host" : "Device");
  223. break;
  224. }
  225. ms = (__s64)xTaskGetTickCount();
  226. if (ms >= (__s64)timeout) {
  227. dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
  228. __func__, host_mode ? "host" : "device");
  229. break;
  230. }
  231. vTaskDelay(pdMS_TO_TICKS(1));
  232. }
  233. }
  234. /**
  235. * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
  236. * filter is enabled.
  237. */
  238. static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
  239. {
  240. u32 gsnpsid;
  241. u32 ghwcfg4;
  242. if (!dwc2_hw_is_otg(hsotg))
  243. return false;
  244. /* Check if core configuration includes the IDDIG filter. */
  245. ghwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
  246. if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
  247. return false;
  248. /*
  249. * Check if the IDDIG debounce filter is bypassed. Available
  250. * in core version >= 3.10a.
  251. */
  252. gsnpsid = dwc2_readl(hsotg->regs + GSNPSID);
  253. if (gsnpsid >= DWC2_CORE_REV_3_10a) {
  254. u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  255. if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
  256. return false;
  257. }
  258. return true;
  259. }
  260. /*
  261. * Do core a soft reset of the core. Be careful with this because it
  262. * resets all the internal state machines of the core.
  263. */
  264. int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait)
  265. {
  266. u32 greset;
  267. int count = 0;
  268. bool wait_for_host_mode = false;
  269. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  270. /*
  271. * If the current mode is host, either due to the force mode
  272. * bit being set (which persists after core reset) or the
  273. * connector id pin, a core soft reset will temporarily reset
  274. * the mode to device. A delay from the IDDIG debounce filter
  275. * will occur before going back to host mode.
  276. *
  277. * Determine whether we will go back into host mode after a
  278. * reset and account for this delay after the reset.
  279. */
  280. if (dwc2_iddig_filter_enabled(hsotg)) {
  281. u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  282. u32 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  283. if (!(gotgctl & GOTGCTL_CONID_B) ||
  284. (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
  285. wait_for_host_mode = true;
  286. }
  287. }
  288. /* Core Soft Reset */
  289. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  290. greset |= GRSTCTL_CSFTRST;
  291. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  292. do {
  293. udelay(1);
  294. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  295. if (++count > 50) {
  296. dev_warn(hsotg->dev,
  297. "%s() HANG! Soft Reset GRSTCTL=%0x\n",
  298. __func__, greset);
  299. return -EBUSY;
  300. }
  301. } while (greset & GRSTCTL_CSFTRST);
  302. /* Wait for AHB master IDLE state */
  303. count = 0;
  304. do {
  305. udelay(1);
  306. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  307. if (++count > 50) {
  308. dev_warn(hsotg->dev,
  309. "%s() HANG! AHB Idle GRSTCTL=%0x\n",
  310. __func__, greset);
  311. return -EBUSY;
  312. }
  313. } while (!(greset & GRSTCTL_AHBIDLE));
  314. if (wait_for_host_mode && !skip_wait)
  315. dwc2_wait_for_mode(hsotg, true);
  316. return 0;
  317. }
  318. /*
  319. * Force the mode of the controller.
  320. *
  321. * Forcing the mode is needed for two cases:
  322. *
  323. * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
  324. * controller to stay in a particular mode regardless of ID pin
  325. * changes. We do this usually after a core reset.
  326. *
  327. * 2) During probe we want to read reset values of the hw
  328. * configuration registers that are only available in either host or
  329. * device mode. We may need to force the mode if the current mode does
  330. * not allow us to access the register in the mode that we want.
  331. *
  332. * In either case it only makes sense to force the mode if the
  333. * controller hardware is OTG capable.
  334. *
  335. * Checks are done in this function to determine whether doing a force
  336. * would be valid or not.
  337. *
  338. * If a force is done, it requires a IDDIG debounce filter delay if
  339. * the filter is configured and enabled. We poll the current mode of
  340. * the controller to account for this delay.
  341. */
  342. static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
  343. {
  344. u32 gusbcfg;
  345. u32 set;
  346. u32 clear;
  347. dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
  348. /*
  349. * Force mode has no effect if the hardware is not OTG.
  350. */
  351. if (!dwc2_hw_is_otg(hsotg))
  352. return false;
  353. /*
  354. * If dr_mode is either peripheral or host only, there is no
  355. * need to ever force the mode to the opposite mode.
  356. */
  357. #ifndef NO_GNU
  358. if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
  359. return false;
  360. if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
  361. return false;
  362. #else
  363. WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL);
  364. WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST);
  365. #endif
  366. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  367. set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
  368. clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
  369. gusbcfg &= ~clear;
  370. gusbcfg |= set;
  371. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  372. dwc2_wait_for_mode(hsotg, host);
  373. return true;
  374. }
  375. /**
  376. * dwc2_clear_force_mode() - Clears the force mode bits.
  377. *
  378. * After clearing the bits, wait up to 100 ms to account for any
  379. * potential IDDIG filter delay. We can't know if we expect this delay
  380. * or not because the value of the connector ID status is affected by
  381. * the force mode. We only need to call this once during probe if
  382. * dr_mode == OTG.
  383. */
  384. void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
  385. {
  386. u32 gusbcfg;
  387. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  388. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  389. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  390. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  391. if (dwc2_iddig_filter_enabled(hsotg))
  392. msleep(100);
  393. }
  394. /*
  395. * Sets or clears force mode based on the dr_mode parameter.
  396. */
  397. void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
  398. {
  399. bool ret;
  400. switch (hsotg->dr_mode) {
  401. case USB_DR_MODE_HOST:
  402. ret = dwc2_force_mode(hsotg, true);
  403. /*
  404. * NOTE: This is required for some rockchip soc based
  405. * platforms on their host-only dwc2.
  406. */
  407. if (!ret)
  408. msleep(50);
  409. break;
  410. case USB_DR_MODE_PERIPHERAL:
  411. dwc2_force_mode(hsotg, false);
  412. break;
  413. case USB_DR_MODE_OTG:
  414. dwc2_clear_force_mode(hsotg);
  415. break;
  416. default:
  417. dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
  418. __func__, hsotg->dr_mode);
  419. break;
  420. }
  421. }
  422. /*
  423. * Do core a soft reset of the core. Be careful with this because it
  424. * resets all the internal state machines of the core.
  425. *
  426. * Additionally this will apply force mode as per the hsotg->dr_mode
  427. * parameter.
  428. */
  429. int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg *hsotg)
  430. {
  431. int retval;
  432. retval = dwc2_core_reset(hsotg, false);
  433. if (retval)
  434. return retval;
  435. dwc2_force_dr_mode(hsotg);
  436. return 0;
  437. }
  438. /**
  439. * dwc2_dump_host_registers() - Prints the host registers
  440. *
  441. * @hsotg: Programming view of DWC_otg controller
  442. *
  443. * NOTE: This function will be removed once the peripheral controller code
  444. * is integrated and the driver is stable
  445. */
  446. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
  447. {
  448. #ifdef DEBUG
  449. u32 addr;//u32 __iomem *addr;
  450. u32 value;
  451. int i;
  452. dev_dbg(hsotg->dev, "Host Global Registers\n");
  453. addr = hsotg->regs + HCFG;
  454. value = dwc2_readl(addr);
  455. dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
  456. (unsigned long)addr, dwc2_readl(addr));
  457. addr = hsotg->regs + HFIR;
  458. value = dwc2_readl(addr);
  459. dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
  460. (unsigned long)addr, dwc2_readl(addr));
  461. addr = hsotg->regs + HFNUM;
  462. dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
  463. (unsigned long)addr, dwc2_readl(addr));
  464. addr = hsotg->regs + HPTXSTS;
  465. value = dwc2_readl(addr);
  466. dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
  467. (unsigned long)addr, dwc2_readl(addr));
  468. addr = hsotg->regs + HAINT;
  469. value = dwc2_readl(addr);
  470. dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
  471. (unsigned long)addr, dwc2_readl(addr));
  472. addr = hsotg->regs + HAINTMSK;
  473. value = dwc2_readl(addr);
  474. dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
  475. (unsigned long)addr, dwc2_readl(addr));
  476. if (hsotg->params.dma_desc_enable) {
  477. addr = hsotg->regs + HFLBADDR;
  478. value = dwc2_readl(addr);
  479. dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
  480. (unsigned long)addr, dwc2_readl(addr));
  481. }
  482. addr = hsotg->regs + HPRT0;
  483. value = dwc2_readl(addr);
  484. dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
  485. (unsigned long)addr, dwc2_readl(addr));
  486. for (i = 0; i < hsotg->params.host_channels; i++) {
  487. dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
  488. addr = hsotg->regs + HCCHAR(i);
  489. value = dwc2_readl(addr);
  490. dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
  491. (unsigned long)addr, dwc2_readl(addr));
  492. addr = hsotg->regs + HCSPLT(i);
  493. value = dwc2_readl(addr);
  494. dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
  495. (unsigned long)addr, dwc2_readl(addr));
  496. addr = hsotg->regs + HCINT(i);
  497. value = dwc2_readl(addr);
  498. dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
  499. (unsigned long)addr, dwc2_readl(addr));
  500. addr = hsotg->regs + HCINTMSK(i);
  501. value = dwc2_readl(addr);
  502. dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
  503. (unsigned long)addr, dwc2_readl(addr));
  504. addr = hsotg->regs + HCTSIZ(i);
  505. value = dwc2_readl(addr);
  506. dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
  507. (unsigned long)addr, dwc2_readl(addr));
  508. addr = hsotg->regs + HCDMA(i);
  509. value = dwc2_readl(addr);
  510. dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
  511. (unsigned long)addr, dwc2_readl(addr));
  512. if (hsotg->params.dma_desc_enable) {
  513. addr = hsotg->regs + HCDMAB(i);
  514. value = dwc2_readl(addr);
  515. dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
  516. (unsigned long)addr, dwc2_readl(addr));
  517. }
  518. }
  519. USB_UNUSED(value);
  520. #endif
  521. }
  522. /**
  523. * dwc2_dump_global_registers() - Prints the core global registers
  524. *
  525. * @hsotg: Programming view of DWC_otg controller
  526. *
  527. * NOTE: This function will be removed once the peripheral controller code
  528. * is integrated and the driver is stable
  529. */
  530. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
  531. {
  532. #ifdef DEBUG
  533. //u32 __iomem *addr;
  534. u32 addr;
  535. dev_dbg(hsotg->dev, "Core Global Registers\n");
  536. addr = hsotg->regs + GOTGCTL;
  537. dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
  538. (unsigned long)addr, dwc2_readl(addr));
  539. addr = hsotg->regs + GOTGINT;
  540. dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
  541. (unsigned long)addr, dwc2_readl(addr));
  542. addr = hsotg->regs + GAHBCFG;
  543. dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
  544. (unsigned long)addr, dwc2_readl(addr));
  545. addr = hsotg->regs + GUSBCFG;
  546. dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
  547. (unsigned long)addr, dwc2_readl(addr));
  548. addr = hsotg->regs + GRSTCTL;
  549. dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
  550. (unsigned long)addr, dwc2_readl(addr));
  551. addr = hsotg->regs + GINTSTS;
  552. dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
  553. (unsigned long)addr, dwc2_readl(addr));
  554. addr = hsotg->regs + GINTMSK;
  555. dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
  556. (unsigned long)addr, dwc2_readl(addr));
  557. addr = hsotg->regs + GRXSTSR;
  558. dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
  559. (unsigned long)addr, dwc2_readl(addr));
  560. addr = hsotg->regs + GRXFSIZ;
  561. dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
  562. (unsigned long)addr, dwc2_readl(addr));
  563. addr = hsotg->regs + GNPTXFSIZ;
  564. dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
  565. (unsigned long)addr, dwc2_readl(addr));
  566. addr = hsotg->regs + GNPTXSTS;
  567. dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
  568. (unsigned long)addr, dwc2_readl(addr));
  569. addr = hsotg->regs + GI2CCTL;
  570. dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
  571. (unsigned long)addr, dwc2_readl(addr));
  572. addr = hsotg->regs + GPVNDCTL;
  573. dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
  574. (unsigned long)addr, dwc2_readl(addr));
  575. addr = hsotg->regs + GGPIO;
  576. dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
  577. (unsigned long)addr, dwc2_readl(addr));
  578. addr = hsotg->regs + GUID;
  579. dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
  580. (unsigned long)addr, dwc2_readl(addr));
  581. addr = hsotg->regs + GSNPSID;
  582. dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
  583. (unsigned long)addr, dwc2_readl(addr));
  584. addr = hsotg->regs + GHWCFG1;
  585. dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
  586. (unsigned long)addr, dwc2_readl(addr));
  587. addr = hsotg->regs + GHWCFG2;
  588. dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
  589. (unsigned long)addr, dwc2_readl(addr));
  590. addr = hsotg->regs + GHWCFG3;
  591. dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
  592. (unsigned long)addr, dwc2_readl(addr));
  593. addr = hsotg->regs + GHWCFG4;
  594. dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
  595. (unsigned long)addr, dwc2_readl(addr));
  596. addr = hsotg->regs + GLPMCFG;
  597. dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
  598. (unsigned long)addr, dwc2_readl(addr));
  599. addr = hsotg->regs + GPWRDN;
  600. dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
  601. (unsigned long)addr, dwc2_readl(addr));
  602. addr = hsotg->regs + GDFIFOCFG;
  603. dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
  604. (unsigned long)addr, dwc2_readl(addr));
  605. addr = hsotg->regs + HPTXFSIZ;
  606. dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
  607. (unsigned long)addr, dwc2_readl(addr));
  608. addr = hsotg->regs + PCGCTL;
  609. dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
  610. (unsigned long)addr, dwc2_readl(addr));
  611. USB_UNUSED(addr);
  612. #endif
  613. }
  614. /**
  615. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  616. *
  617. * @hsotg: Programming view of DWC_otg controller
  618. * @num: Tx FIFO to flush
  619. */
  620. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  621. {
  622. u32 greset;
  623. int count = 0;
  624. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  625. greset = GRSTCTL_TXFFLSH;
  626. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  627. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  628. do {
  629. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  630. if (++count > 10000) {
  631. dev_warn(hsotg->dev,
  632. "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  633. __func__, greset,
  634. dwc2_readl(hsotg->regs + GNPTXSTS));
  635. break;
  636. }
  637. udelay(1);
  638. } while (greset & GRSTCTL_TXFFLSH);
  639. /* Wait for at least 3 PHY Clocks */
  640. udelay(1);
  641. }
  642. /**
  643. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  644. *
  645. * @hsotg: Programming view of DWC_otg controller
  646. */
  647. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  648. {
  649. u32 greset;
  650. int count = 0;
  651. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  652. greset = GRSTCTL_RXFFLSH;
  653. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  654. do {
  655. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  656. if (++count > 10000) {
  657. dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
  658. __func__, greset);
  659. break;
  660. }
  661. udelay(1);
  662. } while (greset & GRSTCTL_RXFFLSH);
  663. /* Wait for at least 3 PHY Clocks */
  664. udelay(1);
  665. }
  666. /*
  667. * Forces either host or device mode if the controller is not
  668. * currently in that mode.
  669. *
  670. * Returns true if the mode was forced.
  671. */
  672. bool dwc2_force_mode_if_needed(struct dwc2_hsotg *hsotg, bool host)
  673. {
  674. if (host && dwc2_is_host_mode(hsotg))
  675. return false;
  676. else if (!host && dwc2_is_device_mode(hsotg))
  677. return false;
  678. return dwc2_force_mode(hsotg, host);
  679. }
  680. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  681. {
  682. if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
  683. return false;
  684. else
  685. return true;
  686. }
  687. /**
  688. * dwc2_enable_global_interrupts() - Enables the controller's Global
  689. * Interrupt in the AHB Config register
  690. *
  691. * @hsotg: Programming view of DWC_otg controller
  692. */
  693. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  694. {
  695. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  696. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  697. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  698. }
  699. /**
  700. * dwc2_disable_global_interrupts() - Disables the controller's Global
  701. * Interrupt in the AHB Config register
  702. *
  703. * @hsotg: Programming view of DWC_otg controller
  704. */
  705. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  706. {
  707. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  708. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  709. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  710. }
  711. /* Returns the controller's GHWCFG2.OTG_MODE. */
  712. unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg)
  713. {
  714. u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
  715. return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  716. GHWCFG2_OP_MODE_SHIFT;
  717. }
  718. /* Returns true if the controller is capable of DRD. */
  719. bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
  720. {
  721. unsigned int op_mode = dwc2_op_mode(hsotg);
  722. return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
  723. (op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
  724. (op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
  725. }
  726. /* Returns true if the controller is host-only. */
  727. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
  728. {
  729. unsigned int op_mode = dwc2_op_mode(hsotg);
  730. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
  731. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
  732. }
  733. /* Returns true if the controller is device-only. */
  734. bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
  735. {
  736. unsigned int op_mode = dwc2_op_mode(hsotg);
  737. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
  738. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
  739. }
  740. #ifndef NO_GNU
  741. MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
  742. MODULE_AUTHOR("Synopsys, Inc.");
  743. MODULE_LICENSE("Dual BSD/GPL");
  744. #endif