core.c 25 KB

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