core.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. * @hsotg: Programming view of DWC_otg controller
  364. */
  365. static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
  366. {
  367. u32 gsnpsid;
  368. u32 ghwcfg4;
  369. if (!dwc2_hw_is_otg(hsotg))
  370. return false;
  371. /* Check if core configuration includes the IDDIG filter. */
  372. ghwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
  373. if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
  374. return false;
  375. /*
  376. * Check if the IDDIG debounce filter is bypassed. Available
  377. * in core version >= 3.10a.
  378. */
  379. gsnpsid = dwc2_readl(hsotg->regs + GSNPSID);
  380. if (gsnpsid >= DWC2_CORE_REV_3_10a) {
  381. u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  382. if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
  383. return false;
  384. }
  385. return true;
  386. }
  387. /*
  388. * dwc2_enter_hibernation() - Common function to enter hibernation.
  389. *
  390. * @hsotg: Programming view of the DWC_otg controller
  391. * @is_host: True if core is in host mode.
  392. *
  393. * Return: 0 if successful, negative error code otherwise
  394. */
  395. int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg, int is_host)
  396. {
  397. if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_HIBERNATION)
  398. return -ENOTSUPP;
  399. if (is_host)
  400. return dwc2_host_enter_hibernation(hsotg);
  401. else
  402. return dwc2_gadget_enter_hibernation(hsotg);
  403. }
  404. /*
  405. * dwc2_exit_hibernation() - Common function to exit from hibernation.
  406. *
  407. * @hsotg: Programming view of the DWC_otg controller
  408. * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
  409. * @reset: Enabled in case of restore with reset.
  410. * @is_host: True if core is in host mode.
  411. *
  412. * Return: 0 if successful, negative error code otherwise
  413. */
  414. int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  415. int reset, int is_host)
  416. {
  417. if (is_host)
  418. return dwc2_host_exit_hibernation(hsotg, rem_wakeup, reset);
  419. else
  420. return dwc2_gadget_exit_hibernation(hsotg, rem_wakeup, reset);
  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. int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait)
  427. {
  428. u32 greset;
  429. bool wait_for_host_mode = false;
  430. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  431. /*
  432. * If the current mode is host, either due to the force mode
  433. * bit being set (which persists after core reset) or the
  434. * connector id pin, a core soft reset will temporarily reset
  435. * the mode to device. A delay from the IDDIG debounce filter
  436. * will occur before going back to host mode.
  437. *
  438. * Determine whether we will go back into host mode after a
  439. * reset and account for this delay after the reset.
  440. */
  441. if (dwc2_iddig_filter_enabled(hsotg)) {
  442. u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  443. u32 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  444. if (!(gotgctl & GOTGCTL_CONID_B) ||
  445. (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
  446. wait_for_host_mode = true;
  447. }
  448. }
  449. /* Core Soft Reset */
  450. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  451. greset |= GRSTCTL_CSFTRST;
  452. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  453. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_CSFTRST, 50)) {
  454. dev_warn(hsotg->dev, "%s: HANG! Soft Reset timeout GRSTCTL GRSTCTL_CSFTRST\n",
  455. __func__);
  456. return -EBUSY;
  457. }
  458. /* Wait for AHB master IDLE state */
  459. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 50)) {
  460. dev_warn(hsotg->dev, "%s: HANG! AHB Idle timeout GRSTCTL GRSTCTL_AHBIDLE\n",
  461. __func__);
  462. return -EBUSY;
  463. }
  464. if (wait_for_host_mode && !skip_wait)
  465. dwc2_wait_for_mode(hsotg, true);
  466. return 0;
  467. }
  468. /**
  469. * dwc2_force_mode() - Force the mode of the controller.
  470. *
  471. * Forcing the mode is needed for two cases:
  472. *
  473. * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
  474. * controller to stay in a particular mode regardless of ID pin
  475. * changes. We do this once during probe.
  476. *
  477. * 2) During probe we want to read reset values of the hw
  478. * configuration registers that are only available in either host or
  479. * device mode. We may need to force the mode if the current mode does
  480. * not allow us to access the register in the mode that we want.
  481. *
  482. * In either case it only makes sense to force the mode if the
  483. * controller hardware is OTG capable.
  484. *
  485. * Checks are done in this function to determine whether doing a force
  486. * would be valid or not.
  487. *
  488. * If a force is done, it requires a IDDIG debounce filter delay if
  489. * the filter is configured and enabled. We poll the current mode of
  490. * the controller to account for this delay.
  491. *
  492. * @hsotg: Programming view of DWC_otg controller
  493. * @host: Host mode flag
  494. */
  495. void dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
  496. {
  497. u32 gusbcfg;
  498. u32 set;
  499. u32 clear;
  500. dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
  501. /*
  502. * Force mode has no effect if the hardware is not OTG.
  503. */
  504. if (!dwc2_hw_is_otg(hsotg))
  505. return;
  506. /*
  507. * If dr_mode is either peripheral or host only, there is no
  508. * need to ever force the mode to the opposite mode.
  509. */
  510. if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
  511. return;
  512. if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
  513. return;
  514. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  515. set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
  516. clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
  517. gusbcfg &= ~clear;
  518. gusbcfg |= set;
  519. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  520. dwc2_wait_for_mode(hsotg, host);
  521. return;
  522. }
  523. /**
  524. * dwc2_clear_force_mode() - Clears the force mode bits.
  525. *
  526. * After clearing the bits, wait up to 100 ms to account for any
  527. * potential IDDIG filter delay. We can't know if we expect this delay
  528. * or not because the value of the connector ID status is affected by
  529. * the force mode. We only need to call this once during probe if
  530. * dr_mode == OTG.
  531. *
  532. * @hsotg: Programming view of DWC_otg controller
  533. */
  534. static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
  535. {
  536. u32 gusbcfg;
  537. if (!dwc2_hw_is_otg(hsotg))
  538. return;
  539. dev_dbg(hsotg->dev, "Clearing force mode bits\n");
  540. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  541. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  542. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  543. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  544. if (dwc2_iddig_filter_enabled(hsotg))
  545. msleep(100);
  546. }
  547. /*
  548. * Sets or clears force mode based on the dr_mode parameter.
  549. */
  550. void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
  551. {
  552. switch (hsotg->dr_mode) {
  553. case USB_DR_MODE_HOST:
  554. /*
  555. * NOTE: This is required for some rockchip soc based
  556. * platforms on their host-only dwc2.
  557. */
  558. if (!dwc2_hw_is_otg(hsotg))
  559. msleep(50);
  560. break;
  561. case USB_DR_MODE_PERIPHERAL:
  562. dwc2_force_mode(hsotg, false);
  563. break;
  564. case USB_DR_MODE_OTG:
  565. dwc2_clear_force_mode(hsotg);
  566. break;
  567. default:
  568. dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
  569. __func__, hsotg->dr_mode);
  570. break;
  571. }
  572. }
  573. /*
  574. * dwc2_enable_acg - enable active clock gating feature
  575. */
  576. void dwc2_enable_acg(struct dwc2_hsotg *hsotg)
  577. {
  578. if (hsotg->params.acg_enable) {
  579. u32 pcgcctl1 = dwc2_readl(hsotg->regs + PCGCCTL1);
  580. dev_dbg(hsotg->dev, "Enabling Active Clock Gating\n");
  581. pcgcctl1 |= PCGCCTL1_GATEEN;
  582. dwc2_writel(pcgcctl1, hsotg->regs + PCGCCTL1);
  583. }
  584. }
  585. /**
  586. * dwc2_dump_host_registers() - Prints the host registers
  587. *
  588. * @hsotg: Programming view of DWC_otg controller
  589. *
  590. * NOTE: This function will be removed once the peripheral controller code
  591. * is integrated and the driver is stable
  592. */
  593. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
  594. {
  595. #ifdef DEBUG
  596. u32 __iomem *addr;
  597. int i;
  598. dev_dbg(hsotg->dev, "Host Global Registers\n");
  599. addr = hsotg->regs + HCFG;
  600. dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
  601. (unsigned long)addr, dwc2_readl(addr));
  602. addr = hsotg->regs + HFIR;
  603. dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
  604. (unsigned long)addr, dwc2_readl(addr));
  605. addr = hsotg->regs + HFNUM;
  606. dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
  607. (unsigned long)addr, dwc2_readl(addr));
  608. addr = hsotg->regs + HPTXSTS;
  609. dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
  610. (unsigned long)addr, dwc2_readl(addr));
  611. addr = hsotg->regs + HAINT;
  612. dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
  613. (unsigned long)addr, dwc2_readl(addr));
  614. addr = hsotg->regs + HAINTMSK;
  615. dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
  616. (unsigned long)addr, dwc2_readl(addr));
  617. if (hsotg->params.dma_desc_enable) {
  618. addr = hsotg->regs + HFLBADDR;
  619. dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
  620. (unsigned long)addr, dwc2_readl(addr));
  621. }
  622. addr = hsotg->regs + HPRT0;
  623. dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
  624. (unsigned long)addr, dwc2_readl(addr));
  625. for (i = 0; i < hsotg->params.host_channels; i++) {
  626. dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
  627. addr = hsotg->regs + HCCHAR(i);
  628. dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
  629. (unsigned long)addr, dwc2_readl(addr));
  630. addr = hsotg->regs + HCSPLT(i);
  631. dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
  632. (unsigned long)addr, dwc2_readl(addr));
  633. addr = hsotg->regs + HCINT(i);
  634. dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
  635. (unsigned long)addr, dwc2_readl(addr));
  636. addr = hsotg->regs + HCINTMSK(i);
  637. dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
  638. (unsigned long)addr, dwc2_readl(addr));
  639. addr = hsotg->regs + HCTSIZ(i);
  640. dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
  641. (unsigned long)addr, dwc2_readl(addr));
  642. addr = hsotg->regs + HCDMA(i);
  643. dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
  644. (unsigned long)addr, dwc2_readl(addr));
  645. if (hsotg->params.dma_desc_enable) {
  646. addr = hsotg->regs + HCDMAB(i);
  647. dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
  648. (unsigned long)addr, dwc2_readl(addr));
  649. }
  650. }
  651. #endif
  652. }
  653. /**
  654. * dwc2_dump_global_registers() - Prints the core global registers
  655. *
  656. * @hsotg: Programming view of DWC_otg controller
  657. *
  658. * NOTE: This function will be removed once the peripheral controller code
  659. * is integrated and the driver is stable
  660. */
  661. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
  662. {
  663. #ifdef DEBUG
  664. u32 __iomem *addr;
  665. dev_dbg(hsotg->dev, "Core Global Registers\n");
  666. addr = hsotg->regs + GOTGCTL;
  667. dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
  668. (unsigned long)addr, dwc2_readl(addr));
  669. addr = hsotg->regs + GOTGINT;
  670. dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
  671. (unsigned long)addr, dwc2_readl(addr));
  672. addr = hsotg->regs + GAHBCFG;
  673. dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
  674. (unsigned long)addr, dwc2_readl(addr));
  675. addr = hsotg->regs + GUSBCFG;
  676. dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
  677. (unsigned long)addr, dwc2_readl(addr));
  678. addr = hsotg->regs + GRSTCTL;
  679. dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
  680. (unsigned long)addr, dwc2_readl(addr));
  681. addr = hsotg->regs + GINTSTS;
  682. dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
  683. (unsigned long)addr, dwc2_readl(addr));
  684. addr = hsotg->regs + GINTMSK;
  685. dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
  686. (unsigned long)addr, dwc2_readl(addr));
  687. addr = hsotg->regs + GRXSTSR;
  688. dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
  689. (unsigned long)addr, dwc2_readl(addr));
  690. addr = hsotg->regs + GRXFSIZ;
  691. dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
  692. (unsigned long)addr, dwc2_readl(addr));
  693. addr = hsotg->regs + GNPTXFSIZ;
  694. dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
  695. (unsigned long)addr, dwc2_readl(addr));
  696. addr = hsotg->regs + GNPTXSTS;
  697. dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
  698. (unsigned long)addr, dwc2_readl(addr));
  699. addr = hsotg->regs + GI2CCTL;
  700. dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
  701. (unsigned long)addr, dwc2_readl(addr));
  702. addr = hsotg->regs + GPVNDCTL;
  703. dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
  704. (unsigned long)addr, dwc2_readl(addr));
  705. addr = hsotg->regs + GGPIO;
  706. dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
  707. (unsigned long)addr, dwc2_readl(addr));
  708. addr = hsotg->regs + GUID;
  709. dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
  710. (unsigned long)addr, dwc2_readl(addr));
  711. addr = hsotg->regs + GSNPSID;
  712. dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
  713. (unsigned long)addr, dwc2_readl(addr));
  714. addr = hsotg->regs + GHWCFG1;
  715. dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
  716. (unsigned long)addr, dwc2_readl(addr));
  717. addr = hsotg->regs + GHWCFG2;
  718. dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
  719. (unsigned long)addr, dwc2_readl(addr));
  720. addr = hsotg->regs + GHWCFG3;
  721. dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
  722. (unsigned long)addr, dwc2_readl(addr));
  723. addr = hsotg->regs + GHWCFG4;
  724. dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
  725. (unsigned long)addr, dwc2_readl(addr));
  726. addr = hsotg->regs + GLPMCFG;
  727. dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
  728. (unsigned long)addr, dwc2_readl(addr));
  729. addr = hsotg->regs + GPWRDN;
  730. dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
  731. (unsigned long)addr, dwc2_readl(addr));
  732. addr = hsotg->regs + GDFIFOCFG;
  733. dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
  734. (unsigned long)addr, dwc2_readl(addr));
  735. addr = hsotg->regs + HPTXFSIZ;
  736. dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
  737. (unsigned long)addr, dwc2_readl(addr));
  738. addr = hsotg->regs + PCGCTL;
  739. dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
  740. (unsigned long)addr, dwc2_readl(addr));
  741. #endif
  742. }
  743. /**
  744. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  745. *
  746. * @hsotg: Programming view of DWC_otg controller
  747. * @num: Tx FIFO to flush
  748. */
  749. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  750. {
  751. u32 greset;
  752. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  753. /* Wait for AHB master IDLE state */
  754. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
  755. dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
  756. __func__);
  757. greset = GRSTCTL_TXFFLSH;
  758. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  759. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  760. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 10000))
  761. dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_TXFFLSH\n",
  762. __func__);
  763. /* Wait for at least 3 PHY Clocks */
  764. udelay(1);
  765. }
  766. /**
  767. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  768. *
  769. * @hsotg: Programming view of DWC_otg controller
  770. */
  771. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  772. {
  773. u32 greset;
  774. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  775. /* Wait for AHB master IDLE state */
  776. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
  777. dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
  778. __func__);
  779. greset = GRSTCTL_RXFFLSH;
  780. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  781. /* Wait for RxFIFO flush done */
  782. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_RXFFLSH, 10000))
  783. dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_RXFFLSH\n",
  784. __func__);
  785. /* Wait for at least 3 PHY Clocks */
  786. udelay(1);
  787. }
  788. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  789. {
  790. if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
  791. return false;
  792. else
  793. return true;
  794. }
  795. /**
  796. * dwc2_enable_global_interrupts() - Enables the controller's Global
  797. * Interrupt in the AHB Config register
  798. *
  799. * @hsotg: Programming view of DWC_otg controller
  800. */
  801. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  802. {
  803. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  804. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  805. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  806. }
  807. /**
  808. * dwc2_disable_global_interrupts() - Disables the controller's Global
  809. * Interrupt in the AHB Config register
  810. *
  811. * @hsotg: Programming view of DWC_otg controller
  812. */
  813. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  814. {
  815. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  816. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  817. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  818. }
  819. /* Returns the controller's GHWCFG2.OTG_MODE. */
  820. unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg)
  821. {
  822. u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
  823. return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  824. GHWCFG2_OP_MODE_SHIFT;
  825. }
  826. /* Returns true if the controller is capable of DRD. */
  827. bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
  828. {
  829. unsigned int op_mode = dwc2_op_mode(hsotg);
  830. return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
  831. (op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
  832. (op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
  833. }
  834. /* Returns true if the controller is host-only. */
  835. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
  836. {
  837. unsigned int op_mode = dwc2_op_mode(hsotg);
  838. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
  839. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
  840. }
  841. /* Returns true if the controller is device-only. */
  842. bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
  843. {
  844. unsigned int op_mode = dwc2_op_mode(hsotg);
  845. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
  846. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
  847. }
  848. /**
  849. * dwc2_hsotg_wait_bit_set - Waits for bit to be set.
  850. * @hsotg: Programming view of DWC_otg controller.
  851. * @offset: Register's offset where bit/bits must be set.
  852. * @mask: Mask of the bit/bits which must be set.
  853. * @timeout: Timeout to wait.
  854. *
  855. * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
  856. */
  857. int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
  858. u32 timeout)
  859. {
  860. u32 i;
  861. for (i = 0; i < timeout; i++) {
  862. if (dwc2_readl(hsotg->regs + offset) & mask)
  863. return 0;
  864. udelay(1);
  865. }
  866. return -ETIMEDOUT;
  867. }
  868. /**
  869. * dwc2_hsotg_wait_bit_clear - Waits for bit to be clear.
  870. * @hsotg: Programming view of DWC_otg controller.
  871. * @offset: Register's offset where bit/bits must be set.
  872. * @mask: Mask of the bit/bits which must be set.
  873. * @timeout: Timeout to wait.
  874. *
  875. * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
  876. */
  877. int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
  878. u32 timeout)
  879. {
  880. u32 i;
  881. for (i = 0; i < timeout; i++) {
  882. if (!(dwc2_readl(hsotg->regs + offset) & mask))
  883. return 0;
  884. udelay(1);
  885. }
  886. return -ETIMEDOUT;
  887. }
  888. MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
  889. MODULE_AUTHOR("Synopsys, Inc.");
  890. MODULE_LICENSE("Dual BSD/GPL");