core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * core.c - DesignWare HS OTG Controller common routines
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The names of the above-listed copyright holders may not be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * ALTERNATIVELY, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") as published by the Free Software
  22. * Foundation; either version 2 of the License, or (at your option) any
  23. * later version.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * The Core code provides basic services for accessing and managing the
  39. * DWC_otg hardware. These services are used by both the Host Controller
  40. * Driver and the Peripheral Controller Driver.
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/delay.h>
  49. #include <linux/io.h>
  50. #include <linux/slab.h>
  51. #include <linux/usb.h>
  52. #include <linux/usb/hcd.h>
  53. #include <linux/usb/ch11.h>
  54. #include "core.h"
  55. #include "hcd.h"
  56. /**
  57. * dwc2_backup_global_registers() - Backup global controller registers.
  58. * When suspending usb bus, registers needs to be backuped
  59. * if controller power is disabled once suspended.
  60. *
  61. * @hsotg: Programming view of the DWC_otg controller
  62. */
  63. int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
  64. {
  65. struct dwc2_gregs_backup *gr;
  66. dev_dbg(hsotg->dev, "%s\n", __func__);
  67. /* Backup global regs */
  68. gr = &hsotg->gr_backup;
  69. gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  70. gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  71. gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  72. gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  73. gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  74. gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  75. gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
  76. gr->pcgcctl1 = dwc2_readl(hsotg->regs + PCGCCTL1);
  77. gr->glpmcfg = dwc2_readl(hsotg->regs + GLPMCFG);
  78. gr->gi2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
  79. gr->pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  80. gr->valid = true;
  81. return 0;
  82. }
  83. /**
  84. * dwc2_restore_global_registers() - Restore controller global registers.
  85. * When resuming usb bus, device registers needs to be restored
  86. * if controller power were disabled.
  87. *
  88. * @hsotg: Programming view of the DWC_otg controller
  89. */
  90. int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
  91. {
  92. struct dwc2_gregs_backup *gr;
  93. dev_dbg(hsotg->dev, "%s\n", __func__);
  94. /* Restore global regs */
  95. gr = &hsotg->gr_backup;
  96. if (!gr->valid) {
  97. dev_err(hsotg->dev, "%s: no global registers to restore\n",
  98. __func__);
  99. return -EINVAL;
  100. }
  101. gr->valid = false;
  102. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  103. dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
  104. dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
  105. dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
  106. dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
  107. dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
  108. dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
  109. dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
  110. dwc2_writel(gr->pcgcctl1, hsotg->regs + PCGCCTL1);
  111. dwc2_writel(gr->glpmcfg, hsotg->regs + GLPMCFG);
  112. dwc2_writel(gr->pcgcctl, hsotg->regs + PCGCTL);
  113. dwc2_writel(gr->gi2cctl, hsotg->regs + GI2CCTL);
  114. return 0;
  115. }
  116. /**
  117. * dwc2_exit_partial_power_down() - Exit controller from Partial Power Down.
  118. *
  119. * @hsotg: Programming view of the DWC_otg controller
  120. * @restore: Controller registers need to be restored
  121. */
  122. int dwc2_exit_partial_power_down(struct dwc2_hsotg *hsotg, bool restore)
  123. {
  124. u32 pcgcctl;
  125. int ret = 0;
  126. if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL)
  127. return -ENOTSUPP;
  128. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  129. pcgcctl &= ~PCGCTL_STOPPCLK;
  130. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  131. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  132. pcgcctl &= ~PCGCTL_PWRCLMP;
  133. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  134. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  135. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  136. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  137. udelay(100);
  138. if (restore) {
  139. ret = dwc2_restore_global_registers(hsotg);
  140. if (ret) {
  141. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  142. __func__);
  143. return ret;
  144. }
  145. if (dwc2_is_host_mode(hsotg)) {
  146. ret = dwc2_restore_host_registers(hsotg);
  147. if (ret) {
  148. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  149. __func__);
  150. return ret;
  151. }
  152. } else {
  153. ret = dwc2_restore_device_registers(hsotg, 0);
  154. if (ret) {
  155. dev_err(hsotg->dev, "%s: failed to restore device registers\n",
  156. __func__);
  157. return ret;
  158. }
  159. }
  160. }
  161. return ret;
  162. }
  163. /**
  164. * dwc2_enter_partial_power_down() - Put controller in Partial Power Down.
  165. *
  166. * @hsotg: Programming view of the DWC_otg controller
  167. */
  168. int dwc2_enter_partial_power_down(struct dwc2_hsotg *hsotg)
  169. {
  170. u32 pcgcctl;
  171. int ret = 0;
  172. if (!hsotg->params.power_down)
  173. return -ENOTSUPP;
  174. /* Backup all registers */
  175. ret = dwc2_backup_global_registers(hsotg);
  176. if (ret) {
  177. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  178. __func__);
  179. return ret;
  180. }
  181. if (dwc2_is_host_mode(hsotg)) {
  182. ret = dwc2_backup_host_registers(hsotg);
  183. if (ret) {
  184. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  185. __func__);
  186. return ret;
  187. }
  188. } else {
  189. ret = dwc2_backup_device_registers(hsotg);
  190. if (ret) {
  191. dev_err(hsotg->dev, "%s: failed to backup device registers\n",
  192. __func__);
  193. return ret;
  194. }
  195. }
  196. /*
  197. * Clear any pending interrupts since dwc2 will not be able to
  198. * clear them after entering partial_power_down.
  199. */
  200. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  201. /* Put the controller in low power state */
  202. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  203. pcgcctl |= PCGCTL_PWRCLMP;
  204. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  205. ndelay(20);
  206. pcgcctl |= PCGCTL_RSTPDWNMODULE;
  207. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  208. ndelay(20);
  209. pcgcctl |= PCGCTL_STOPPCLK;
  210. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  211. return ret;
  212. }
  213. /**
  214. * dwc2_restore_essential_regs() - Restore essiential regs of core.
  215. *
  216. * @hsotg: Programming view of the DWC_otg controller
  217. * @rmode: Restore mode, enabled in case of remote-wakeup.
  218. * @is_host: Host or device mode.
  219. */
  220. static void dwc2_restore_essential_regs(struct dwc2_hsotg *hsotg, int rmode,
  221. int is_host)
  222. {
  223. u32 pcgcctl;
  224. struct dwc2_gregs_backup *gr;
  225. struct dwc2_dregs_backup *dr;
  226. struct dwc2_hregs_backup *hr;
  227. gr = &hsotg->gr_backup;
  228. dr = &hsotg->dr_backup;
  229. hr = &hsotg->hr_backup;
  230. dev_dbg(hsotg->dev, "%s: restoring essential regs\n", __func__);
  231. /* Load restore values for [31:14] bits */
  232. pcgcctl = (gr->pcgcctl & 0xffffc000);
  233. /* If High Speed */
  234. if (is_host) {
  235. if (!(pcgcctl & PCGCTL_P2HD_PRT_SPD_MASK))
  236. pcgcctl |= BIT(17);
  237. } else {
  238. if (!(pcgcctl & PCGCTL_P2HD_DEV_ENUM_SPD_MASK))
  239. pcgcctl |= BIT(17);
  240. }
  241. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  242. /* Umnask global Interrupt in GAHBCFG and restore it */
  243. dwc2_writel(gr->gahbcfg | GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
  244. /* Clear all pending interupts */
  245. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  246. /* Unmask restore done interrupt */
  247. dwc2_writel(GINTSTS_RESTOREDONE, hsotg->regs + GINTMSK);
  248. /* Restore GUSBCFG and HCFG/DCFG */
  249. dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
  250. if (is_host) {
  251. dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
  252. if (rmode)
  253. pcgcctl |= PCGCTL_RESTOREMODE;
  254. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  255. udelay(10);
  256. pcgcctl |= PCGCTL_ESS_REG_RESTORED;
  257. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  258. udelay(10);
  259. } else {
  260. dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
  261. if (!rmode)
  262. pcgcctl |= PCGCTL_RESTOREMODE | PCGCTL_RSTPDWNMODULE;
  263. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  264. udelay(10);
  265. pcgcctl |= PCGCTL_ESS_REG_RESTORED;
  266. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  267. udelay(10);
  268. }
  269. }
  270. /**
  271. * dwc2_hib_restore_common() - Common part of restore routine.
  272. *
  273. * @hsotg: Programming view of the DWC_otg controller
  274. * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
  275. * @is_host: Host or device mode.
  276. */
  277. void dwc2_hib_restore_common(struct dwc2_hsotg *hsotg, int rem_wakeup,
  278. int is_host)
  279. {
  280. u32 gpwrdn;
  281. /* Switch-on voltage to the core */
  282. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  283. gpwrdn &= ~GPWRDN_PWRDNSWTCH;
  284. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  285. udelay(10);
  286. /* Reset core */
  287. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  288. gpwrdn &= ~GPWRDN_PWRDNRSTN;
  289. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  290. udelay(10);
  291. /* Enable restore from PMU */
  292. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  293. gpwrdn |= GPWRDN_RESTORE;
  294. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  295. udelay(10);
  296. /* Disable Power Down Clamp */
  297. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  298. gpwrdn &= ~GPWRDN_PWRDNCLMP;
  299. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  300. udelay(50);
  301. if (!is_host && rem_wakeup)
  302. udelay(70);
  303. /* Deassert reset core */
  304. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  305. gpwrdn |= GPWRDN_PWRDNRSTN;
  306. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  307. udelay(10);
  308. /* Disable PMU interrupt */
  309. gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
  310. gpwrdn &= ~GPWRDN_PMUINTSEL;
  311. dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
  312. udelay(10);
  313. /* Set Restore Essential Regs bit in PCGCCTL register */
  314. dwc2_restore_essential_regs(hsotg, rem_wakeup, is_host);
  315. /*
  316. * Wait For Restore_done Interrupt. This mechanism of polling the
  317. * interrupt is introduced to avoid any possible race conditions
  318. */
  319. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, GINTSTS_RESTOREDONE,
  320. 20000)) {
  321. dev_dbg(hsotg->dev,
  322. "%s: Restore Done wan't generated here\n",
  323. __func__);
  324. } else {
  325. dev_dbg(hsotg->dev, "restore done generated here\n");
  326. }
  327. }
  328. /**
  329. * dwc2_wait_for_mode() - Waits for the controller mode.
  330. * @hsotg: Programming view of the DWC_otg controller.
  331. * @host_mode: If true, waits for host mode, otherwise device mode.
  332. */
  333. static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
  334. bool host_mode)
  335. {
  336. ktime_t start;
  337. ktime_t end;
  338. unsigned int timeout = 110;
  339. dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
  340. host_mode ? "host" : "device");
  341. start = ktime_get();
  342. while (1) {
  343. s64 ms;
  344. if (dwc2_is_host_mode(hsotg) == host_mode) {
  345. dev_vdbg(hsotg->dev, "%s mode set\n",
  346. host_mode ? "Host" : "Device");
  347. break;
  348. }
  349. end = ktime_get();
  350. ms = ktime_to_ms(ktime_sub(end, start));
  351. if (ms >= (s64)timeout) {
  352. dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
  353. __func__, host_mode ? "host" : "device");
  354. break;
  355. }
  356. usleep_range(1000, 2000);
  357. }
  358. }
  359. /**
  360. * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
  361. * filter is enabled.
  362. */
  363. static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
  364. {
  365. u32 gsnpsid;
  366. u32 ghwcfg4;
  367. if (!dwc2_hw_is_otg(hsotg))
  368. return false;
  369. /* Check if core configuration includes the IDDIG filter. */
  370. ghwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
  371. if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
  372. return false;
  373. /*
  374. * Check if the IDDIG debounce filter is bypassed. Available
  375. * in core version >= 3.10a.
  376. */
  377. gsnpsid = dwc2_readl(hsotg->regs + GSNPSID);
  378. if (gsnpsid >= DWC2_CORE_REV_3_10a) {
  379. u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  380. if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
  381. return false;
  382. }
  383. return true;
  384. }
  385. /*
  386. * dwc2_enter_hibernation() - Common function to enter hibernation.
  387. *
  388. * @hsotg: Programming view of the DWC_otg controller
  389. * @is_host: True if core is in host mode.
  390. *
  391. * Return: 0 if successful, negative error code otherwise
  392. */
  393. int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg, int is_host)
  394. {
  395. if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_HIBERNATION)
  396. return -ENOTSUPP;
  397. if (is_host)
  398. return dwc2_host_enter_hibernation(hsotg);
  399. else
  400. return dwc2_gadget_enter_hibernation(hsotg);
  401. }
  402. /*
  403. * dwc2_exit_hibernation() - Common function to exit from hibernation.
  404. *
  405. * @hsotg: Programming view of the DWC_otg controller
  406. * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
  407. * @reset: Enabled in case of restore with reset.
  408. * @is_host: True if core is in host mode.
  409. *
  410. * Return: 0 if successful, negative error code otherwise
  411. */
  412. int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  413. int reset, int is_host)
  414. {
  415. if (is_host)
  416. return dwc2_host_exit_hibernation(hsotg, rem_wakeup, reset);
  417. else
  418. return dwc2_gadget_exit_hibernation(hsotg, rem_wakeup, reset);
  419. }
  420. /*
  421. * Do core a soft reset of the core. Be careful with this because it
  422. * resets all the internal state machines of the core.
  423. */
  424. int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait)
  425. {
  426. u32 greset;
  427. bool wait_for_host_mode = false;
  428. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  429. /*
  430. * If the current mode is host, either due to the force mode
  431. * bit being set (which persists after core reset) or the
  432. * connector id pin, a core soft reset will temporarily reset
  433. * the mode to device. A delay from the IDDIG debounce filter
  434. * will occur before going back to host mode.
  435. *
  436. * Determine whether we will go back into host mode after a
  437. * reset and account for this delay after the reset.
  438. */
  439. if (dwc2_iddig_filter_enabled(hsotg)) {
  440. u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  441. u32 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  442. if (!(gotgctl & GOTGCTL_CONID_B) ||
  443. (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
  444. wait_for_host_mode = true;
  445. }
  446. }
  447. /* Core Soft Reset */
  448. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  449. greset |= GRSTCTL_CSFTRST;
  450. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  451. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_CSFTRST, 50)) {
  452. dev_warn(hsotg->dev, "%s: HANG! Soft Reset timeout GRSTCTL GRSTCTL_CSFTRST\n",
  453. __func__);
  454. return -EBUSY;
  455. }
  456. /* Wait for AHB master IDLE state */
  457. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 50)) {
  458. dev_warn(hsotg->dev, "%s: HANG! AHB Idle timeout GRSTCTL GRSTCTL_AHBIDLE\n",
  459. __func__);
  460. return -EBUSY;
  461. }
  462. if (wait_for_host_mode && !skip_wait)
  463. dwc2_wait_for_mode(hsotg, true);
  464. return 0;
  465. }
  466. /**
  467. * dwc2_force_mode() - Force the mode of the controller.
  468. *
  469. * Forcing the mode is needed for two cases:
  470. *
  471. * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
  472. * controller to stay in a particular mode regardless of ID pin
  473. * changes. We do this once during probe.
  474. *
  475. * 2) During probe we want to read reset values of the hw
  476. * configuration registers that are only available in either host or
  477. * device mode. We may need to force the mode if the current mode does
  478. * not allow us to access the register in the mode that we want.
  479. *
  480. * In either case it only makes sense to force the mode if the
  481. * controller hardware is OTG capable.
  482. *
  483. * Checks are done in this function to determine whether doing a force
  484. * would be valid or not.
  485. *
  486. * If a force is done, it requires a IDDIG debounce filter delay if
  487. * the filter is configured and enabled. We poll the current mode of
  488. * the controller to account for this delay.
  489. */
  490. void dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
  491. {
  492. u32 gusbcfg;
  493. u32 set;
  494. u32 clear;
  495. dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
  496. /*
  497. * Force mode has no effect if the hardware is not OTG.
  498. */
  499. if (!dwc2_hw_is_otg(hsotg))
  500. return;
  501. /*
  502. * If dr_mode is either peripheral or host only, there is no
  503. * need to ever force the mode to the opposite mode.
  504. */
  505. if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
  506. return;
  507. if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
  508. return;
  509. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  510. set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
  511. clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
  512. gusbcfg &= ~clear;
  513. gusbcfg |= set;
  514. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  515. dwc2_wait_for_mode(hsotg, host);
  516. return;
  517. }
  518. /**
  519. * dwc2_clear_force_mode() - Clears the force mode bits.
  520. *
  521. * After clearing the bits, wait up to 100 ms to account for any
  522. * potential IDDIG filter delay. We can't know if we expect this delay
  523. * or not because the value of the connector ID status is affected by
  524. * the force mode. We only need to call this once during probe if
  525. * dr_mode == OTG.
  526. */
  527. static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
  528. {
  529. u32 gusbcfg;
  530. if (!dwc2_hw_is_otg(hsotg))
  531. return;
  532. dev_dbg(hsotg->dev, "Clearing force mode bits\n");
  533. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  534. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  535. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  536. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  537. if (dwc2_iddig_filter_enabled(hsotg))
  538. msleep(100);
  539. }
  540. /*
  541. * Sets or clears force mode based on the dr_mode parameter.
  542. */
  543. void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
  544. {
  545. switch (hsotg->dr_mode) {
  546. case USB_DR_MODE_HOST:
  547. /*
  548. * NOTE: This is required for some rockchip soc based
  549. * platforms on their host-only dwc2.
  550. */
  551. if (!dwc2_hw_is_otg(hsotg))
  552. msleep(50);
  553. break;
  554. case USB_DR_MODE_PERIPHERAL:
  555. dwc2_force_mode(hsotg, false);
  556. break;
  557. case USB_DR_MODE_OTG:
  558. dwc2_clear_force_mode(hsotg);
  559. break;
  560. default:
  561. dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
  562. __func__, hsotg->dr_mode);
  563. break;
  564. }
  565. }
  566. /*
  567. * dwc2_enable_acg - enable active clock gating feature
  568. */
  569. void dwc2_enable_acg(struct dwc2_hsotg *hsotg)
  570. {
  571. if (hsotg->params.acg_enable) {
  572. u32 pcgcctl1 = dwc2_readl(hsotg->regs + PCGCCTL1);
  573. dev_dbg(hsotg->dev, "Enabling Active Clock Gating\n");
  574. pcgcctl1 |= PCGCCTL1_GATEEN;
  575. dwc2_writel(pcgcctl1, hsotg->regs + PCGCCTL1);
  576. }
  577. }
  578. /**
  579. * dwc2_dump_host_registers() - Prints the host registers
  580. *
  581. * @hsotg: Programming view of DWC_otg controller
  582. *
  583. * NOTE: This function will be removed once the peripheral controller code
  584. * is integrated and the driver is stable
  585. */
  586. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
  587. {
  588. #ifdef DEBUG
  589. u32 __iomem *addr;
  590. int i;
  591. dev_dbg(hsotg->dev, "Host Global Registers\n");
  592. addr = hsotg->regs + HCFG;
  593. dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
  594. (unsigned long)addr, dwc2_readl(addr));
  595. addr = hsotg->regs + HFIR;
  596. dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
  597. (unsigned long)addr, dwc2_readl(addr));
  598. addr = hsotg->regs + HFNUM;
  599. dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
  600. (unsigned long)addr, dwc2_readl(addr));
  601. addr = hsotg->regs + HPTXSTS;
  602. dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
  603. (unsigned long)addr, dwc2_readl(addr));
  604. addr = hsotg->regs + HAINT;
  605. dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
  606. (unsigned long)addr, dwc2_readl(addr));
  607. addr = hsotg->regs + HAINTMSK;
  608. dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
  609. (unsigned long)addr, dwc2_readl(addr));
  610. if (hsotg->params.dma_desc_enable) {
  611. addr = hsotg->regs + HFLBADDR;
  612. dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
  613. (unsigned long)addr, dwc2_readl(addr));
  614. }
  615. addr = hsotg->regs + HPRT0;
  616. dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
  617. (unsigned long)addr, dwc2_readl(addr));
  618. for (i = 0; i < hsotg->params.host_channels; i++) {
  619. dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
  620. addr = hsotg->regs + HCCHAR(i);
  621. dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
  622. (unsigned long)addr, dwc2_readl(addr));
  623. addr = hsotg->regs + HCSPLT(i);
  624. dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
  625. (unsigned long)addr, dwc2_readl(addr));
  626. addr = hsotg->regs + HCINT(i);
  627. dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
  628. (unsigned long)addr, dwc2_readl(addr));
  629. addr = hsotg->regs + HCINTMSK(i);
  630. dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
  631. (unsigned long)addr, dwc2_readl(addr));
  632. addr = hsotg->regs + HCTSIZ(i);
  633. dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
  634. (unsigned long)addr, dwc2_readl(addr));
  635. addr = hsotg->regs + HCDMA(i);
  636. dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
  637. (unsigned long)addr, dwc2_readl(addr));
  638. if (hsotg->params.dma_desc_enable) {
  639. addr = hsotg->regs + HCDMAB(i);
  640. dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
  641. (unsigned long)addr, dwc2_readl(addr));
  642. }
  643. }
  644. #endif
  645. }
  646. /**
  647. * dwc2_dump_global_registers() - Prints the core global registers
  648. *
  649. * @hsotg: Programming view of DWC_otg controller
  650. *
  651. * NOTE: This function will be removed once the peripheral controller code
  652. * is integrated and the driver is stable
  653. */
  654. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
  655. {
  656. #ifdef DEBUG
  657. u32 __iomem *addr;
  658. dev_dbg(hsotg->dev, "Core Global Registers\n");
  659. addr = hsotg->regs + GOTGCTL;
  660. dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
  661. (unsigned long)addr, dwc2_readl(addr));
  662. addr = hsotg->regs + GOTGINT;
  663. dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
  664. (unsigned long)addr, dwc2_readl(addr));
  665. addr = hsotg->regs + GAHBCFG;
  666. dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
  667. (unsigned long)addr, dwc2_readl(addr));
  668. addr = hsotg->regs + GUSBCFG;
  669. dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
  670. (unsigned long)addr, dwc2_readl(addr));
  671. addr = hsotg->regs + GRSTCTL;
  672. dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
  673. (unsigned long)addr, dwc2_readl(addr));
  674. addr = hsotg->regs + GINTSTS;
  675. dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
  676. (unsigned long)addr, dwc2_readl(addr));
  677. addr = hsotg->regs + GINTMSK;
  678. dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
  679. (unsigned long)addr, dwc2_readl(addr));
  680. addr = hsotg->regs + GRXSTSR;
  681. dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
  682. (unsigned long)addr, dwc2_readl(addr));
  683. addr = hsotg->regs + GRXFSIZ;
  684. dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
  685. (unsigned long)addr, dwc2_readl(addr));
  686. addr = hsotg->regs + GNPTXFSIZ;
  687. dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
  688. (unsigned long)addr, dwc2_readl(addr));
  689. addr = hsotg->regs + GNPTXSTS;
  690. dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
  691. (unsigned long)addr, dwc2_readl(addr));
  692. addr = hsotg->regs + GI2CCTL;
  693. dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
  694. (unsigned long)addr, dwc2_readl(addr));
  695. addr = hsotg->regs + GPVNDCTL;
  696. dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
  697. (unsigned long)addr, dwc2_readl(addr));
  698. addr = hsotg->regs + GGPIO;
  699. dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
  700. (unsigned long)addr, dwc2_readl(addr));
  701. addr = hsotg->regs + GUID;
  702. dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
  703. (unsigned long)addr, dwc2_readl(addr));
  704. addr = hsotg->regs + GSNPSID;
  705. dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
  706. (unsigned long)addr, dwc2_readl(addr));
  707. addr = hsotg->regs + GHWCFG1;
  708. dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
  709. (unsigned long)addr, dwc2_readl(addr));
  710. addr = hsotg->regs + GHWCFG2;
  711. dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
  712. (unsigned long)addr, dwc2_readl(addr));
  713. addr = hsotg->regs + GHWCFG3;
  714. dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
  715. (unsigned long)addr, dwc2_readl(addr));
  716. addr = hsotg->regs + GHWCFG4;
  717. dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
  718. (unsigned long)addr, dwc2_readl(addr));
  719. addr = hsotg->regs + GLPMCFG;
  720. dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
  721. (unsigned long)addr, dwc2_readl(addr));
  722. addr = hsotg->regs + GPWRDN;
  723. dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
  724. (unsigned long)addr, dwc2_readl(addr));
  725. addr = hsotg->regs + GDFIFOCFG;
  726. dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
  727. (unsigned long)addr, dwc2_readl(addr));
  728. addr = hsotg->regs + HPTXFSIZ;
  729. dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
  730. (unsigned long)addr, dwc2_readl(addr));
  731. addr = hsotg->regs + PCGCTL;
  732. dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
  733. (unsigned long)addr, dwc2_readl(addr));
  734. #endif
  735. }
  736. /**
  737. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  738. *
  739. * @hsotg: Programming view of DWC_otg controller
  740. * @num: Tx FIFO to flush
  741. */
  742. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  743. {
  744. u32 greset;
  745. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  746. /* Wait for AHB master IDLE state */
  747. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
  748. dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
  749. __func__);
  750. greset = GRSTCTL_TXFFLSH;
  751. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  752. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  753. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 10000))
  754. dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_TXFFLSH\n",
  755. __func__);
  756. /* Wait for at least 3 PHY Clocks */
  757. udelay(1);
  758. }
  759. /**
  760. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  761. *
  762. * @hsotg: Programming view of DWC_otg controller
  763. */
  764. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  765. {
  766. u32 greset;
  767. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  768. /* Wait for AHB master IDLE state */
  769. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
  770. dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
  771. __func__);
  772. greset = GRSTCTL_RXFFLSH;
  773. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  774. /* Wait for RxFIFO flush done */
  775. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_RXFFLSH, 10000))
  776. dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_RXFFLSH\n",
  777. __func__);
  778. /* Wait for at least 3 PHY Clocks */
  779. udelay(1);
  780. }
  781. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  782. {
  783. if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
  784. return false;
  785. else
  786. return true;
  787. }
  788. /**
  789. * dwc2_enable_global_interrupts() - Enables the controller's Global
  790. * Interrupt in the AHB Config register
  791. *
  792. * @hsotg: Programming view of DWC_otg controller
  793. */
  794. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  795. {
  796. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  797. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  798. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  799. }
  800. /**
  801. * dwc2_disable_global_interrupts() - Disables the controller's Global
  802. * Interrupt in the AHB Config register
  803. *
  804. * @hsotg: Programming view of DWC_otg controller
  805. */
  806. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  807. {
  808. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  809. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  810. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  811. }
  812. /* Returns the controller's GHWCFG2.OTG_MODE. */
  813. unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg)
  814. {
  815. u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
  816. return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  817. GHWCFG2_OP_MODE_SHIFT;
  818. }
  819. /* Returns true if the controller is capable of DRD. */
  820. bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
  821. {
  822. unsigned int op_mode = dwc2_op_mode(hsotg);
  823. return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
  824. (op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
  825. (op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
  826. }
  827. /* Returns true if the controller is host-only. */
  828. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
  829. {
  830. unsigned int op_mode = dwc2_op_mode(hsotg);
  831. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
  832. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
  833. }
  834. /* Returns true if the controller is device-only. */
  835. bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
  836. {
  837. unsigned int op_mode = dwc2_op_mode(hsotg);
  838. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
  839. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
  840. }
  841. /**
  842. * dwc2_hsotg_wait_bit_set - Waits for bit to be set.
  843. * @hsotg: Programming view of DWC_otg controller.
  844. * @offset: Register's offset where bit/bits must be set.
  845. * @mask: Mask of the bit/bits which must be set.
  846. * @timeout: Timeout to wait.
  847. *
  848. * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
  849. */
  850. int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
  851. u32 timeout)
  852. {
  853. u32 i;
  854. for (i = 0; i < timeout; i++) {
  855. if (dwc2_readl(hsotg->regs + offset) & mask)
  856. return 0;
  857. udelay(1);
  858. }
  859. return -ETIMEDOUT;
  860. }
  861. /**
  862. * dwc2_hsotg_wait_bit_clear - Waits for bit to be clear.
  863. * @hsotg: Programming view of DWC_otg controller.
  864. * @offset: Register's offset where bit/bits must be set.
  865. * @mask: Mask of the bit/bits which must be set.
  866. * @timeout: Timeout to wait.
  867. *
  868. * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
  869. */
  870. int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
  871. u32 timeout)
  872. {
  873. u32 i;
  874. for (i = 0; i < timeout; i++) {
  875. if (!(dwc2_readl(hsotg->regs + offset) & mask))
  876. return 0;
  877. udelay(1);
  878. }
  879. return -ETIMEDOUT;
  880. }
  881. MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
  882. MODULE_AUTHOR("Synopsys, Inc.");
  883. MODULE_LICENSE("Dual BSD/GPL");