core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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, GOTGCTL);
  70. gr->gintmsk = dwc2_readl(hsotg, GINTMSK);
  71. gr->gahbcfg = dwc2_readl(hsotg, GAHBCFG);
  72. gr->gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  73. gr->grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
  74. gr->gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
  75. gr->gdfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
  76. gr->pcgcctl1 = dwc2_readl(hsotg, PCGCCTL1);
  77. gr->glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  78. gr->gi2cctl = dwc2_readl(hsotg, GI2CCTL);
  79. gr->pcgcctl = dwc2_readl(hsotg, 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(hsotg, 0xffffffff, GINTSTS);
  103. dwc2_writel(hsotg, gr->gotgctl, GOTGCTL);
  104. dwc2_writel(hsotg, gr->gintmsk, GINTMSK);
  105. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  106. dwc2_writel(hsotg, gr->gahbcfg, GAHBCFG);
  107. dwc2_writel(hsotg, gr->grxfsiz, GRXFSIZ);
  108. dwc2_writel(hsotg, gr->gnptxfsiz, GNPTXFSIZ);
  109. dwc2_writel(hsotg, gr->gdfifocfg, GDFIFOCFG);
  110. dwc2_writel(hsotg, gr->pcgcctl1, PCGCCTL1);
  111. dwc2_writel(hsotg, gr->glpmcfg, GLPMCFG);
  112. dwc2_writel(hsotg, gr->pcgcctl, PCGCTL);
  113. dwc2_writel(hsotg, gr->gi2cctl, 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, PCGCTL);
  129. pcgcctl &= ~PCGCTL_STOPPCLK;
  130. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  131. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  132. pcgcctl &= ~PCGCTL_PWRCLMP;
  133. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  134. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  135. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  136. dwc2_writel(hsotg, pcgcctl, 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(hsotg, 0xffffffff, GINTSTS);
  201. /* Put the controller in low power state */
  202. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  203. pcgcctl |= PCGCTL_PWRCLMP;
  204. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  205. ndelay(20);
  206. pcgcctl |= PCGCTL_RSTPDWNMODULE;
  207. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  208. ndelay(20);
  209. pcgcctl |= PCGCTL_STOPPCLK;
  210. dwc2_writel(hsotg, pcgcctl, 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(hsotg, pcgcctl, PCGCTL);
  242. /* Umnask global Interrupt in GAHBCFG and restore it */
  243. dwc2_writel(hsotg, gr->gahbcfg | GAHBCFG_GLBL_INTR_EN, GAHBCFG);
  244. /* Clear all pending interupts */
  245. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  246. /* Unmask restore done interrupt */
  247. dwc2_writel(hsotg, GINTSTS_RESTOREDONE, GINTMSK);
  248. /* Restore GUSBCFG and HCFG/DCFG */
  249. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  250. if (is_host) {
  251. dwc2_writel(hsotg, hr->hcfg, HCFG);
  252. if (rmode)
  253. pcgcctl |= PCGCTL_RESTOREMODE;
  254. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  255. udelay(10);
  256. pcgcctl |= PCGCTL_ESS_REG_RESTORED;
  257. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  258. udelay(10);
  259. } else {
  260. dwc2_writel(hsotg, dr->dcfg, DCFG);
  261. if (!rmode)
  262. pcgcctl |= PCGCTL_RESTOREMODE | PCGCTL_RSTPDWNMODULE;
  263. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  264. udelay(10);
  265. pcgcctl |= PCGCTL_ESS_REG_RESTORED;
  266. dwc2_writel(hsotg, pcgcctl, 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, GPWRDN);
  283. gpwrdn &= ~GPWRDN_PWRDNSWTCH;
  284. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  285. udelay(10);
  286. /* Reset core */
  287. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  288. gpwrdn &= ~GPWRDN_PWRDNRSTN;
  289. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  290. udelay(10);
  291. /* Enable restore from PMU */
  292. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  293. gpwrdn |= GPWRDN_RESTORE;
  294. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  295. udelay(10);
  296. /* Disable Power Down Clamp */
  297. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  298. gpwrdn &= ~GPWRDN_PWRDNCLMP;
  299. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  300. udelay(50);
  301. if (!is_host && rem_wakeup)
  302. udelay(70);
  303. /* Deassert reset core */
  304. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  305. gpwrdn |= GPWRDN_PWRDNRSTN;
  306. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  307. udelay(10);
  308. /* Disable PMU interrupt */
  309. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  310. gpwrdn &= ~GPWRDN_PMUINTSEL;
  311. dwc2_writel(hsotg, gpwrdn, 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, 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, GSNPSID);
  380. if (gsnpsid >= DWC2_CORE_REV_3_10a) {
  381. u32 gotgctl = dwc2_readl(hsotg, 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, GOTGCTL);
  443. u32 gusbcfg = dwc2_readl(hsotg, 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, GRSTCTL);
  451. greset |= GRSTCTL_CSFTRST;
  452. dwc2_writel(hsotg, greset, GRSTCTL);
  453. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_CSFTRST, 10000)) {
  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, 10000)) {
  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, GUSBCFG);
  515. set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
  516. clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
  517. gusbcfg &= ~clear;
  518. gusbcfg |= set;
  519. dwc2_writel(hsotg, gusbcfg, 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, GUSBCFG);
  541. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  542. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  543. dwc2_writel(hsotg, gusbcfg, 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, PCGCCTL1);
  580. dev_dbg(hsotg->dev, "Enabling Active Clock Gating\n");
  581. pcgcctl1 |= PCGCCTL1_GATEEN;
  582. dwc2_writel(hsotg, pcgcctl1, 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(hsotg, HCFG));
  602. addr = hsotg->regs + HFIR;
  603. dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
  604. (unsigned long)addr, dwc2_readl(hsotg, HFIR));
  605. addr = hsotg->regs + HFNUM;
  606. dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
  607. (unsigned long)addr, dwc2_readl(hsotg, HFNUM));
  608. addr = hsotg->regs + HPTXSTS;
  609. dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
  610. (unsigned long)addr, dwc2_readl(hsotg, HPTXSTS));
  611. addr = hsotg->regs + HAINT;
  612. dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
  613. (unsigned long)addr, dwc2_readl(hsotg, HAINT));
  614. addr = hsotg->regs + HAINTMSK;
  615. dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
  616. (unsigned long)addr, dwc2_readl(hsotg, HAINTMSK));
  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(hsotg, HFLBADDR));
  621. }
  622. addr = hsotg->regs + HPRT0;
  623. dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
  624. (unsigned long)addr, dwc2_readl(hsotg, HPRT0));
  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(hsotg, HCCHAR(i)));
  630. addr = hsotg->regs + HCSPLT(i);
  631. dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
  632. (unsigned long)addr, dwc2_readl(hsotg, HCSPLT(i)));
  633. addr = hsotg->regs + HCINT(i);
  634. dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
  635. (unsigned long)addr, dwc2_readl(hsotg, HCINT(i)));
  636. addr = hsotg->regs + HCINTMSK(i);
  637. dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
  638. (unsigned long)addr, dwc2_readl(hsotg, HCINTMSK(i)));
  639. addr = hsotg->regs + HCTSIZ(i);
  640. dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
  641. (unsigned long)addr, dwc2_readl(hsotg, HCTSIZ(i)));
  642. addr = hsotg->regs + HCDMA(i);
  643. dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
  644. (unsigned long)addr, dwc2_readl(hsotg, HCDMA(i)));
  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(hsotg,
  649. HCDMAB(i)));
  650. }
  651. }
  652. #endif
  653. }
  654. /**
  655. * dwc2_dump_global_registers() - Prints the core global registers
  656. *
  657. * @hsotg: Programming view of DWC_otg controller
  658. *
  659. * NOTE: This function will be removed once the peripheral controller code
  660. * is integrated and the driver is stable
  661. */
  662. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
  663. {
  664. #ifdef DEBUG
  665. u32 __iomem *addr;
  666. dev_dbg(hsotg->dev, "Core Global Registers\n");
  667. addr = hsotg->regs + GOTGCTL;
  668. dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
  669. (unsigned long)addr, dwc2_readl(hsotg, GOTGCTL));
  670. addr = hsotg->regs + GOTGINT;
  671. dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
  672. (unsigned long)addr, dwc2_readl(hsotg, GOTGINT));
  673. addr = hsotg->regs + GAHBCFG;
  674. dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
  675. (unsigned long)addr, dwc2_readl(hsotg, GAHBCFG));
  676. addr = hsotg->regs + GUSBCFG;
  677. dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
  678. (unsigned long)addr, dwc2_readl(hsotg, GUSBCFG));
  679. addr = hsotg->regs + GRSTCTL;
  680. dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
  681. (unsigned long)addr, dwc2_readl(hsotg, GRSTCTL));
  682. addr = hsotg->regs + GINTSTS;
  683. dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
  684. (unsigned long)addr, dwc2_readl(hsotg, GINTSTS));
  685. addr = hsotg->regs + GINTMSK;
  686. dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
  687. (unsigned long)addr, dwc2_readl(hsotg, GINTMSK));
  688. addr = hsotg->regs + GRXSTSR;
  689. dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
  690. (unsigned long)addr, dwc2_readl(hsotg, GRXSTSR));
  691. addr = hsotg->regs + GRXFSIZ;
  692. dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
  693. (unsigned long)addr, dwc2_readl(hsotg, GRXFSIZ));
  694. addr = hsotg->regs + GNPTXFSIZ;
  695. dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
  696. (unsigned long)addr, dwc2_readl(hsotg, GNPTXFSIZ));
  697. addr = hsotg->regs + GNPTXSTS;
  698. dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
  699. (unsigned long)addr, dwc2_readl(hsotg, GNPTXSTS));
  700. addr = hsotg->regs + GI2CCTL;
  701. dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
  702. (unsigned long)addr, dwc2_readl(hsotg, GI2CCTL));
  703. addr = hsotg->regs + GPVNDCTL;
  704. dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
  705. (unsigned long)addr, dwc2_readl(hsotg, GPVNDCTL));
  706. addr = hsotg->regs + GGPIO;
  707. dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
  708. (unsigned long)addr, dwc2_readl(hsotg, GGPIO));
  709. addr = hsotg->regs + GUID;
  710. dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
  711. (unsigned long)addr, dwc2_readl(hsotg, GUID));
  712. addr = hsotg->regs + GSNPSID;
  713. dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
  714. (unsigned long)addr, dwc2_readl(hsotg, GSNPSID));
  715. addr = hsotg->regs + GHWCFG1;
  716. dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
  717. (unsigned long)addr, dwc2_readl(hsotg, GHWCFG1));
  718. addr = hsotg->regs + GHWCFG2;
  719. dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
  720. (unsigned long)addr, dwc2_readl(hsotg, GHWCFG2));
  721. addr = hsotg->regs + GHWCFG3;
  722. dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
  723. (unsigned long)addr, dwc2_readl(hsotg, GHWCFG3));
  724. addr = hsotg->regs + GHWCFG4;
  725. dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
  726. (unsigned long)addr, dwc2_readl(hsotg, GHWCFG4));
  727. addr = hsotg->regs + GLPMCFG;
  728. dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
  729. (unsigned long)addr, dwc2_readl(hsotg, GLPMCFG));
  730. addr = hsotg->regs + GPWRDN;
  731. dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
  732. (unsigned long)addr, dwc2_readl(hsotg, GPWRDN));
  733. addr = hsotg->regs + GDFIFOCFG;
  734. dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
  735. (unsigned long)addr, dwc2_readl(hsotg, GDFIFOCFG));
  736. addr = hsotg->regs + HPTXFSIZ;
  737. dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
  738. (unsigned long)addr, dwc2_readl(hsotg, HPTXFSIZ));
  739. addr = hsotg->regs + PCGCTL;
  740. dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
  741. (unsigned long)addr, dwc2_readl(hsotg, PCGCTL));
  742. #endif
  743. }
  744. /**
  745. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  746. *
  747. * @hsotg: Programming view of DWC_otg controller
  748. * @num: Tx FIFO to flush
  749. */
  750. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  751. {
  752. u32 greset;
  753. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  754. /* Wait for AHB master IDLE state */
  755. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
  756. dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
  757. __func__);
  758. greset = GRSTCTL_TXFFLSH;
  759. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  760. dwc2_writel(hsotg, greset, GRSTCTL);
  761. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 10000))
  762. dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_TXFFLSH\n",
  763. __func__);
  764. /* Wait for at least 3 PHY Clocks */
  765. udelay(1);
  766. }
  767. /**
  768. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  769. *
  770. * @hsotg: Programming view of DWC_otg controller
  771. */
  772. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  773. {
  774. u32 greset;
  775. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  776. /* Wait for AHB master IDLE state */
  777. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
  778. dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
  779. __func__);
  780. greset = GRSTCTL_RXFFLSH;
  781. dwc2_writel(hsotg, greset, GRSTCTL);
  782. /* Wait for RxFIFO flush done */
  783. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_RXFFLSH, 10000))
  784. dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_RXFFLSH\n",
  785. __func__);
  786. /* Wait for at least 3 PHY Clocks */
  787. udelay(1);
  788. }
  789. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  790. {
  791. if (dwc2_readl(hsotg, GSNPSID) == 0xffffffff)
  792. return false;
  793. else
  794. return true;
  795. }
  796. /**
  797. * dwc2_enable_global_interrupts() - Enables the controller's Global
  798. * Interrupt in the AHB Config register
  799. *
  800. * @hsotg: Programming view of DWC_otg controller
  801. */
  802. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  803. {
  804. u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  805. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  806. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  807. }
  808. /**
  809. * dwc2_disable_global_interrupts() - Disables the controller's Global
  810. * Interrupt in the AHB Config register
  811. *
  812. * @hsotg: Programming view of DWC_otg controller
  813. */
  814. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  815. {
  816. u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  817. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  818. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  819. }
  820. /* Returns the controller's GHWCFG2.OTG_MODE. */
  821. unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg)
  822. {
  823. u32 ghwcfg2 = dwc2_readl(hsotg, GHWCFG2);
  824. return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  825. GHWCFG2_OP_MODE_SHIFT;
  826. }
  827. /* Returns true if the controller is capable of DRD. */
  828. bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
  829. {
  830. unsigned int op_mode = dwc2_op_mode(hsotg);
  831. return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
  832. (op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
  833. (op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
  834. }
  835. /* Returns true if the controller is host-only. */
  836. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
  837. {
  838. unsigned int op_mode = dwc2_op_mode(hsotg);
  839. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
  840. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
  841. }
  842. /* Returns true if the controller is device-only. */
  843. bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
  844. {
  845. unsigned int op_mode = dwc2_op_mode(hsotg);
  846. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
  847. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
  848. }
  849. /**
  850. * dwc2_hsotg_wait_bit_set - Waits for bit to be set.
  851. * @hsotg: Programming view of DWC_otg controller.
  852. * @offset: Register's offset where bit/bits must be set.
  853. * @mask: Mask of the bit/bits which must be set.
  854. * @timeout: Timeout to wait.
  855. *
  856. * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
  857. */
  858. int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
  859. u32 timeout)
  860. {
  861. u32 i;
  862. for (i = 0; i < timeout; i++) {
  863. if (dwc2_readl(hsotg, offset) & mask)
  864. return 0;
  865. udelay(1);
  866. }
  867. return -ETIMEDOUT;
  868. }
  869. /**
  870. * dwc2_hsotg_wait_bit_clear - Waits for bit to be clear.
  871. * @hsotg: Programming view of DWC_otg controller.
  872. * @offset: Register's offset where bit/bits must be set.
  873. * @mask: Mask of the bit/bits which must be set.
  874. * @timeout: Timeout to wait.
  875. *
  876. * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
  877. */
  878. int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
  879. u32 timeout)
  880. {
  881. u32 i;
  882. for (i = 0; i < timeout; i++) {
  883. if (!(dwc2_readl(hsotg, offset) & mask))
  884. return 0;
  885. udelay(1);
  886. }
  887. return -ETIMEDOUT;
  888. }
  889. MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
  890. MODULE_AUTHOR("Synopsys, Inc.");
  891. MODULE_LICENSE("Dual BSD/GPL");