core.c 23 KB

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