core.c 23 KB

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