core.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. /*
  8. * The Core code provides basic services for accessing and managing the
  9. * DWC_otg hardware. These services are used by both the Host Controller
  10. * Driver and the Peripheral Controller Driver.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/delay.h>
  19. #include <linux/io.h>
  20. #include <linux/slab.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/hcd.h>
  23. #include <linux/usb/ch11.h>
  24. #include "core.h"
  25. #include "hcd.h"
  26. /**
  27. * dwc2_backup_global_registers() - Backup global controller registers.
  28. * When suspending usb bus, registers needs to be backuped
  29. * if controller power is disabled once suspended.
  30. *
  31. * @hsotg: Programming view of the DWC_otg controller
  32. */
  33. int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
  34. {
  35. struct dwc2_gregs_backup *gr;
  36. dev_dbg(hsotg->dev, "%s\n", __func__);
  37. /* Backup global regs */
  38. gr = &hsotg->gr_backup;
  39. gr->gotgctl = dwc2_readl(hsotg, GOTGCTL);
  40. gr->gintmsk = dwc2_readl(hsotg, GINTMSK);
  41. gr->gahbcfg = dwc2_readl(hsotg, GAHBCFG);
  42. gr->gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  43. gr->grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
  44. gr->gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
  45. gr->gdfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
  46. gr->pcgcctl1 = dwc2_readl(hsotg, PCGCCTL1);
  47. gr->glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  48. gr->gi2cctl = dwc2_readl(hsotg, GI2CCTL);
  49. gr->pcgcctl = dwc2_readl(hsotg, PCGCTL);
  50. gr->valid = true;
  51. return 0;
  52. }
  53. /**
  54. * dwc2_restore_global_registers() - Restore controller global registers.
  55. * When resuming usb bus, device registers needs to be restored
  56. * if controller power were disabled.
  57. *
  58. * @hsotg: Programming view of the DWC_otg controller
  59. */
  60. int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
  61. {
  62. struct dwc2_gregs_backup *gr;
  63. dev_dbg(hsotg->dev, "%s\n", __func__);
  64. /* Restore global regs */
  65. gr = &hsotg->gr_backup;
  66. if (!gr->valid) {
  67. dev_err(hsotg->dev, "%s: no global registers to restore\n",
  68. __func__);
  69. return -EINVAL;
  70. }
  71. gr->valid = false;
  72. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  73. dwc2_writel(hsotg, gr->gotgctl, GOTGCTL);
  74. dwc2_writel(hsotg, gr->gintmsk, GINTMSK);
  75. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  76. dwc2_writel(hsotg, gr->gahbcfg, GAHBCFG);
  77. dwc2_writel(hsotg, gr->grxfsiz, GRXFSIZ);
  78. dwc2_writel(hsotg, gr->gnptxfsiz, GNPTXFSIZ);
  79. dwc2_writel(hsotg, gr->gdfifocfg, GDFIFOCFG);
  80. dwc2_writel(hsotg, gr->pcgcctl1, PCGCCTL1);
  81. dwc2_writel(hsotg, gr->glpmcfg, GLPMCFG);
  82. dwc2_writel(hsotg, gr->pcgcctl, PCGCTL);
  83. dwc2_writel(hsotg, gr->gi2cctl, GI2CCTL);
  84. return 0;
  85. }
  86. /**
  87. * dwc2_exit_partial_power_down() - Exit controller from Partial Power Down.
  88. *
  89. * @hsotg: Programming view of the DWC_otg controller
  90. * @rem_wakeup: indicates whether resume is initiated by Reset.
  91. * @restore: Controller registers need to be restored
  92. */
  93. int dwc2_exit_partial_power_down(struct dwc2_hsotg *hsotg, int rem_wakeup,
  94. bool restore)
  95. {
  96. struct dwc2_gregs_backup *gr;
  97. gr = &hsotg->gr_backup;
  98. /*
  99. * Restore host or device regisers with the same mode core enterted
  100. * to partial power down by checking "GOTGCTL_CURMODE_HOST" backup
  101. * value of the "gotgctl" register.
  102. */
  103. if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
  104. return dwc2_host_exit_partial_power_down(hsotg, rem_wakeup,
  105. restore);
  106. else
  107. return dwc2_gadget_exit_partial_power_down(hsotg, restore);
  108. }
  109. /**
  110. * dwc2_enter_partial_power_down() - Put controller in Partial Power Down.
  111. *
  112. * @hsotg: Programming view of the DWC_otg controller
  113. */
  114. int dwc2_enter_partial_power_down(struct dwc2_hsotg *hsotg)
  115. {
  116. if (dwc2_is_host_mode(hsotg))
  117. return dwc2_host_enter_partial_power_down(hsotg);
  118. else
  119. return dwc2_gadget_enter_partial_power_down(hsotg);
  120. }
  121. /**
  122. * dwc2_restore_essential_regs() - Restore essiential regs of core.
  123. *
  124. * @hsotg: Programming view of the DWC_otg controller
  125. * @rmode: Restore mode, enabled in case of remote-wakeup.
  126. * @is_host: Host or device mode.
  127. */
  128. static void dwc2_restore_essential_regs(struct dwc2_hsotg *hsotg, int rmode,
  129. int is_host)
  130. {
  131. u32 pcgcctl;
  132. struct dwc2_gregs_backup *gr;
  133. struct dwc2_dregs_backup *dr;
  134. struct dwc2_hregs_backup *hr;
  135. gr = &hsotg->gr_backup;
  136. dr = &hsotg->dr_backup;
  137. hr = &hsotg->hr_backup;
  138. dev_dbg(hsotg->dev, "%s: restoring essential regs\n", __func__);
  139. /* Load restore values for [31:14] bits */
  140. pcgcctl = (gr->pcgcctl & 0xffffc000);
  141. /* If High Speed */
  142. if (is_host) {
  143. if (!(pcgcctl & PCGCTL_P2HD_PRT_SPD_MASK))
  144. pcgcctl |= BIT(17);
  145. } else {
  146. if (!(pcgcctl & PCGCTL_P2HD_DEV_ENUM_SPD_MASK))
  147. pcgcctl |= BIT(17);
  148. }
  149. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  150. /* Umnask global Interrupt in GAHBCFG and restore it */
  151. dwc2_writel(hsotg, gr->gahbcfg | GAHBCFG_GLBL_INTR_EN, GAHBCFG);
  152. /* Clear all pending interupts */
  153. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  154. /* Unmask restore done interrupt */
  155. dwc2_writel(hsotg, GINTSTS_RESTOREDONE, GINTMSK);
  156. /* Restore GUSBCFG and HCFG/DCFG */
  157. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  158. if (is_host) {
  159. dwc2_writel(hsotg, hr->hcfg, HCFG);
  160. if (rmode)
  161. pcgcctl |= PCGCTL_RESTOREMODE;
  162. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  163. udelay(10);
  164. pcgcctl |= PCGCTL_ESS_REG_RESTORED;
  165. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  166. udelay(10);
  167. } else {
  168. dwc2_writel(hsotg, dr->dcfg, DCFG);
  169. if (!rmode)
  170. pcgcctl |= PCGCTL_RESTOREMODE | PCGCTL_RSTPDWNMODULE;
  171. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  172. udelay(10);
  173. pcgcctl |= PCGCTL_ESS_REG_RESTORED;
  174. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  175. udelay(10);
  176. }
  177. }
  178. /**
  179. * dwc2_hib_restore_common() - Common part of restore routine.
  180. *
  181. * @hsotg: Programming view of the DWC_otg controller
  182. * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
  183. * @is_host: Host or device mode.
  184. */
  185. void dwc2_hib_restore_common(struct dwc2_hsotg *hsotg, int rem_wakeup,
  186. int is_host)
  187. {
  188. u32 gpwrdn;
  189. /* Switch-on voltage to the core */
  190. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  191. gpwrdn &= ~GPWRDN_PWRDNSWTCH;
  192. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  193. udelay(10);
  194. /* Reset core */
  195. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  196. gpwrdn &= ~GPWRDN_PWRDNRSTN;
  197. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  198. udelay(10);
  199. /* Enable restore from PMU */
  200. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  201. gpwrdn |= GPWRDN_RESTORE;
  202. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  203. udelay(10);
  204. /* Disable Power Down Clamp */
  205. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  206. gpwrdn &= ~GPWRDN_PWRDNCLMP;
  207. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  208. udelay(50);
  209. if (!is_host && rem_wakeup)
  210. udelay(70);
  211. /* Deassert reset core */
  212. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  213. gpwrdn |= GPWRDN_PWRDNRSTN;
  214. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  215. udelay(10);
  216. /* Reset ULPI latch */
  217. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  218. gpwrdn &= ~GPWRDN_ULPI_LATCH_EN_DURING_HIB_ENTRY;
  219. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  220. /* Disable PMU interrupt */
  221. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  222. gpwrdn &= ~GPWRDN_PMUINTSEL;
  223. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  224. udelay(10);
  225. /* Set Restore Essential Regs bit in PCGCCTL register */
  226. dwc2_restore_essential_regs(hsotg, rem_wakeup, is_host);
  227. /*
  228. * Wait For Restore_done Interrupt. This mechanism of polling the
  229. * interrupt is introduced to avoid any possible race conditions
  230. */
  231. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, GINTSTS_RESTOREDONE,
  232. 20000)) {
  233. dev_dbg(hsotg->dev,
  234. "%s: Restore Done wasn't generated here\n",
  235. __func__);
  236. } else {
  237. dev_dbg(hsotg->dev, "restore done generated here\n");
  238. /*
  239. * To avoid restore done interrupt storm after restore is
  240. * generated clear GINTSTS_RESTOREDONE bit.
  241. */
  242. dwc2_writel(hsotg, GINTSTS_RESTOREDONE, GINTSTS);
  243. }
  244. }
  245. /**
  246. * dwc2_wait_for_mode() - Waits for the controller mode.
  247. * @hsotg: Programming view of the DWC_otg controller.
  248. * @host_mode: If true, waits for host mode, otherwise device mode.
  249. */
  250. static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
  251. bool host_mode)
  252. {
  253. ktime_t start;
  254. ktime_t end;
  255. unsigned int timeout = 110;
  256. dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
  257. host_mode ? "host" : "device");
  258. start = ktime_get();
  259. while (1) {
  260. s64 ms;
  261. if (dwc2_is_host_mode(hsotg) == host_mode) {
  262. dev_vdbg(hsotg->dev, "%s mode set\n",
  263. host_mode ? "Host" : "Device");
  264. break;
  265. }
  266. end = ktime_get();
  267. ms = ktime_to_ms(ktime_sub(end, start));
  268. if (ms >= (s64)timeout) {
  269. dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
  270. __func__, host_mode ? "host" : "device");
  271. break;
  272. }
  273. usleep_range(1000, 2000);
  274. }
  275. }
  276. /**
  277. * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
  278. * filter is enabled.
  279. *
  280. * @hsotg: Programming view of DWC_otg controller
  281. */
  282. static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
  283. {
  284. u32 gsnpsid;
  285. u32 ghwcfg4;
  286. if (!dwc2_hw_is_otg(hsotg))
  287. return false;
  288. /* Check if core configuration includes the IDDIG filter. */
  289. ghwcfg4 = dwc2_readl(hsotg, GHWCFG4);
  290. if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
  291. return false;
  292. /*
  293. * Check if the IDDIG debounce filter is bypassed. Available
  294. * in core version >= 3.10a.
  295. */
  296. gsnpsid = dwc2_readl(hsotg, GSNPSID);
  297. if (gsnpsid >= DWC2_CORE_REV_3_10a) {
  298. u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
  299. if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
  300. return false;
  301. }
  302. return true;
  303. }
  304. /*
  305. * dwc2_enter_hibernation() - Common function to enter hibernation.
  306. *
  307. * @hsotg: Programming view of the DWC_otg controller
  308. * @is_host: True if core is in host mode.
  309. *
  310. * Return: 0 if successful, negative error code otherwise
  311. */
  312. int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg, int is_host)
  313. {
  314. if (is_host)
  315. return dwc2_host_enter_hibernation(hsotg);
  316. else
  317. return dwc2_gadget_enter_hibernation(hsotg);
  318. }
  319. /*
  320. * dwc2_exit_hibernation() - Common function to exit from hibernation.
  321. *
  322. * @hsotg: Programming view of the DWC_otg controller
  323. * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
  324. * @reset: Enabled in case of restore with reset.
  325. * @is_host: True if core is in host mode.
  326. *
  327. * Return: 0 if successful, negative error code otherwise
  328. */
  329. int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  330. int reset, int is_host)
  331. {
  332. if (is_host)
  333. return dwc2_host_exit_hibernation(hsotg, rem_wakeup, reset);
  334. else
  335. return dwc2_gadget_exit_hibernation(hsotg, rem_wakeup, reset);
  336. }
  337. /*
  338. * Do core a soft reset of the core. Be careful with this because it
  339. * resets all the internal state machines of the core.
  340. */
  341. int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait)
  342. {
  343. u32 greset;
  344. bool wait_for_host_mode = false;
  345. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  346. /*
  347. * If the current mode is host, either due to the force mode
  348. * bit being set (which persists after core reset) or the
  349. * connector id pin, a core soft reset will temporarily reset
  350. * the mode to device. A delay from the IDDIG debounce filter
  351. * will occur before going back to host mode.
  352. *
  353. * Determine whether we will go back into host mode after a
  354. * reset and account for this delay after the reset.
  355. */
  356. if (dwc2_iddig_filter_enabled(hsotg)) {
  357. u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
  358. u32 gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  359. if (!(gotgctl & GOTGCTL_CONID_B) ||
  360. (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
  361. wait_for_host_mode = true;
  362. }
  363. }
  364. /* Core Soft Reset */
  365. greset = dwc2_readl(hsotg, GRSTCTL);
  366. greset |= GRSTCTL_CSFTRST;
  367. dwc2_writel(hsotg, greset, GRSTCTL);
  368. if ((hsotg->hw_params.snpsid & DWC2_CORE_REV_MASK) <
  369. (DWC2_CORE_REV_4_20a & DWC2_CORE_REV_MASK)) {
  370. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL,
  371. GRSTCTL_CSFTRST, 10000)) {
  372. dev_warn(hsotg->dev, "%s: HANG! Soft Reset timeout GRSTCTL_CSFTRST\n",
  373. __func__);
  374. return -EBUSY;
  375. }
  376. } else {
  377. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
  378. GRSTCTL_CSFTRST_DONE, 10000)) {
  379. dev_warn(hsotg->dev, "%s: HANG! Soft Reset timeout GRSTCTL_CSFTRST_DONE\n",
  380. __func__);
  381. return -EBUSY;
  382. }
  383. greset = dwc2_readl(hsotg, GRSTCTL);
  384. greset &= ~GRSTCTL_CSFTRST;
  385. greset |= GRSTCTL_CSFTRST_DONE;
  386. dwc2_writel(hsotg, greset, GRSTCTL);
  387. }
  388. /*
  389. * Switching from device mode to host mode by disconnecting
  390. * device cable core enters and exits form hibernation.
  391. * However, the fifo map remains not cleared. It results
  392. * to a WARNING (WARNING: CPU: 5 PID: 0 at drivers/usb/dwc2/
  393. * gadget.c:307 dwc2_hsotg_init_fifo+0x12/0x152 [dwc2])
  394. * if in host mode we disconnect the micro a to b host
  395. * cable. Because core reset occurs.
  396. * To avoid the WARNING, fifo_map should be cleared
  397. * in dwc2_core_reset() function by taking into account configs.
  398. * fifo_map must be cleared only if driver is configured in
  399. * "CONFIG_USB_DWC2_PERIPHERAL" or "CONFIG_USB_DWC2_DUAL_ROLE"
  400. * mode.
  401. */
  402. dwc2_clear_fifo_map(hsotg);
  403. /* Wait for AHB master IDLE state */
  404. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000)) {
  405. dev_warn(hsotg->dev, "%s: HANG! AHB Idle timeout GRSTCTL GRSTCTL_AHBIDLE\n",
  406. __func__);
  407. return -EBUSY;
  408. }
  409. if (wait_for_host_mode && !skip_wait)
  410. dwc2_wait_for_mode(hsotg, true);
  411. return 0;
  412. }
  413. /**
  414. * dwc2_force_mode() - Force the mode of the controller.
  415. *
  416. * Forcing the mode is needed for two cases:
  417. *
  418. * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
  419. * controller to stay in a particular mode regardless of ID pin
  420. * changes. We do this once during probe.
  421. *
  422. * 2) During probe we want to read reset values of the hw
  423. * configuration registers that are only available in either host or
  424. * device mode. We may need to force the mode if the current mode does
  425. * not allow us to access the register in the mode that we want.
  426. *
  427. * In either case it only makes sense to force the mode if the
  428. * controller hardware is OTG capable.
  429. *
  430. * Checks are done in this function to determine whether doing a force
  431. * would be valid or not.
  432. *
  433. * If a force is done, it requires a IDDIG debounce filter delay if
  434. * the filter is configured and enabled. We poll the current mode of
  435. * the controller to account for this delay.
  436. *
  437. * @hsotg: Programming view of DWC_otg controller
  438. * @host: Host mode flag
  439. */
  440. void dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
  441. {
  442. u32 gusbcfg;
  443. u32 set;
  444. u32 clear;
  445. dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
  446. /*
  447. * Force mode has no effect if the hardware is not OTG.
  448. */
  449. if (!dwc2_hw_is_otg(hsotg))
  450. return;
  451. /*
  452. * If dr_mode is either peripheral or host only, there is no
  453. * need to ever force the mode to the opposite mode.
  454. */
  455. if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
  456. return;
  457. if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
  458. return;
  459. gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  460. set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
  461. clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
  462. gusbcfg &= ~clear;
  463. gusbcfg |= set;
  464. dwc2_writel(hsotg, gusbcfg, GUSBCFG);
  465. dwc2_wait_for_mode(hsotg, host);
  466. return;
  467. }
  468. /**
  469. * dwc2_clear_force_mode() - Clears the force mode bits.
  470. *
  471. * After clearing the bits, wait up to 100 ms to account for any
  472. * potential IDDIG filter delay. We can't know if we expect this delay
  473. * or not because the value of the connector ID status is affected by
  474. * the force mode. We only need to call this once during probe if
  475. * dr_mode == OTG.
  476. *
  477. * @hsotg: Programming view of DWC_otg controller
  478. */
  479. static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
  480. {
  481. u32 gusbcfg;
  482. if (!dwc2_hw_is_otg(hsotg))
  483. return;
  484. dev_dbg(hsotg->dev, "Clearing force mode bits\n");
  485. gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  486. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  487. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  488. dwc2_writel(hsotg, gusbcfg, GUSBCFG);
  489. if (dwc2_iddig_filter_enabled(hsotg))
  490. msleep(100);
  491. }
  492. /*
  493. * Sets or clears force mode based on the dr_mode parameter.
  494. */
  495. void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
  496. {
  497. switch (hsotg->dr_mode) {
  498. case USB_DR_MODE_HOST:
  499. /*
  500. * NOTE: This is required for some rockchip soc based
  501. * platforms on their host-only dwc2.
  502. */
  503. if (!dwc2_hw_is_otg(hsotg))
  504. msleep(50);
  505. break;
  506. case USB_DR_MODE_PERIPHERAL:
  507. dwc2_force_mode(hsotg, false);
  508. break;
  509. case USB_DR_MODE_OTG:
  510. dwc2_clear_force_mode(hsotg);
  511. break;
  512. default:
  513. dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
  514. __func__, hsotg->dr_mode);
  515. break;
  516. }
  517. }
  518. /*
  519. * dwc2_enable_acg - enable active clock gating feature
  520. */
  521. void dwc2_enable_acg(struct dwc2_hsotg *hsotg)
  522. {
  523. if (hsotg->params.acg_enable) {
  524. u32 pcgcctl1 = dwc2_readl(hsotg, PCGCCTL1);
  525. dev_dbg(hsotg->dev, "Enabling Active Clock Gating\n");
  526. pcgcctl1 |= PCGCCTL1_GATEEN;
  527. dwc2_writel(hsotg, pcgcctl1, PCGCCTL1);
  528. }
  529. }
  530. /**
  531. * dwc2_dump_host_registers() - Prints the host registers
  532. *
  533. * @hsotg: Programming view of DWC_otg controller
  534. *
  535. * NOTE: This function will be removed once the peripheral controller code
  536. * is integrated and the driver is stable
  537. */
  538. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
  539. {
  540. #ifdef DEBUG
  541. u32 __iomem *addr;
  542. int i;
  543. dev_dbg(hsotg->dev, "Host Global Registers\n");
  544. addr = hsotg->regs + HCFG;
  545. dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
  546. (unsigned long)addr, dwc2_readl(hsotg, HCFG));
  547. addr = hsotg->regs + HFIR;
  548. dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
  549. (unsigned long)addr, dwc2_readl(hsotg, HFIR));
  550. addr = hsotg->regs + HFNUM;
  551. dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
  552. (unsigned long)addr, dwc2_readl(hsotg, HFNUM));
  553. addr = hsotg->regs + HPTXSTS;
  554. dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
  555. (unsigned long)addr, dwc2_readl(hsotg, HPTXSTS));
  556. addr = hsotg->regs + HAINT;
  557. dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
  558. (unsigned long)addr, dwc2_readl(hsotg, HAINT));
  559. addr = hsotg->regs + HAINTMSK;
  560. dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
  561. (unsigned long)addr, dwc2_readl(hsotg, HAINTMSK));
  562. if (hsotg->params.dma_desc_enable) {
  563. addr = hsotg->regs + HFLBADDR;
  564. dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
  565. (unsigned long)addr, dwc2_readl(hsotg, HFLBADDR));
  566. }
  567. addr = hsotg->regs + HPRT0;
  568. dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
  569. (unsigned long)addr, dwc2_readl(hsotg, HPRT0));
  570. for (i = 0; i < hsotg->params.host_channels; i++) {
  571. dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
  572. addr = hsotg->regs + HCCHAR(i);
  573. dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
  574. (unsigned long)addr, dwc2_readl(hsotg, HCCHAR(i)));
  575. addr = hsotg->regs + HCSPLT(i);
  576. dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
  577. (unsigned long)addr, dwc2_readl(hsotg, HCSPLT(i)));
  578. addr = hsotg->regs + HCINT(i);
  579. dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
  580. (unsigned long)addr, dwc2_readl(hsotg, HCINT(i)));
  581. addr = hsotg->regs + HCINTMSK(i);
  582. dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
  583. (unsigned long)addr, dwc2_readl(hsotg, HCINTMSK(i)));
  584. addr = hsotg->regs + HCTSIZ(i);
  585. dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
  586. (unsigned long)addr, dwc2_readl(hsotg, HCTSIZ(i)));
  587. addr = hsotg->regs + HCDMA(i);
  588. dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
  589. (unsigned long)addr, dwc2_readl(hsotg, HCDMA(i)));
  590. if (hsotg->params.dma_desc_enable) {
  591. addr = hsotg->regs + HCDMAB(i);
  592. dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
  593. (unsigned long)addr, dwc2_readl(hsotg,
  594. HCDMAB(i)));
  595. }
  596. }
  597. #endif
  598. }
  599. /**
  600. * dwc2_dump_global_registers() - Prints the core global registers
  601. *
  602. * @hsotg: Programming view of DWC_otg controller
  603. *
  604. * NOTE: This function will be removed once the peripheral controller code
  605. * is integrated and the driver is stable
  606. */
  607. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
  608. {
  609. #ifdef DEBUG
  610. u32 __iomem *addr;
  611. dev_dbg(hsotg->dev, "Core Global Registers\n");
  612. addr = hsotg->regs + GOTGCTL;
  613. dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
  614. (unsigned long)addr, dwc2_readl(hsotg, GOTGCTL));
  615. addr = hsotg->regs + GOTGINT;
  616. dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
  617. (unsigned long)addr, dwc2_readl(hsotg, GOTGINT));
  618. addr = hsotg->regs + GAHBCFG;
  619. dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
  620. (unsigned long)addr, dwc2_readl(hsotg, GAHBCFG));
  621. addr = hsotg->regs + GUSBCFG;
  622. dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
  623. (unsigned long)addr, dwc2_readl(hsotg, GUSBCFG));
  624. addr = hsotg->regs + GRSTCTL;
  625. dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
  626. (unsigned long)addr, dwc2_readl(hsotg, GRSTCTL));
  627. addr = hsotg->regs + GINTSTS;
  628. dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
  629. (unsigned long)addr, dwc2_readl(hsotg, GINTSTS));
  630. addr = hsotg->regs + GINTMSK;
  631. dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
  632. (unsigned long)addr, dwc2_readl(hsotg, GINTMSK));
  633. addr = hsotg->regs + GRXSTSR;
  634. dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
  635. (unsigned long)addr, dwc2_readl(hsotg, GRXSTSR));
  636. addr = hsotg->regs + GRXFSIZ;
  637. dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
  638. (unsigned long)addr, dwc2_readl(hsotg, GRXFSIZ));
  639. addr = hsotg->regs + GNPTXFSIZ;
  640. dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
  641. (unsigned long)addr, dwc2_readl(hsotg, GNPTXFSIZ));
  642. addr = hsotg->regs + GNPTXSTS;
  643. dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
  644. (unsigned long)addr, dwc2_readl(hsotg, GNPTXSTS));
  645. addr = hsotg->regs + GI2CCTL;
  646. dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
  647. (unsigned long)addr, dwc2_readl(hsotg, GI2CCTL));
  648. addr = hsotg->regs + GPVNDCTL;
  649. dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
  650. (unsigned long)addr, dwc2_readl(hsotg, GPVNDCTL));
  651. addr = hsotg->regs + GGPIO;
  652. dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
  653. (unsigned long)addr, dwc2_readl(hsotg, GGPIO));
  654. addr = hsotg->regs + GUID;
  655. dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
  656. (unsigned long)addr, dwc2_readl(hsotg, GUID));
  657. addr = hsotg->regs + GSNPSID;
  658. dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
  659. (unsigned long)addr, dwc2_readl(hsotg, GSNPSID));
  660. addr = hsotg->regs + GHWCFG1;
  661. dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
  662. (unsigned long)addr, dwc2_readl(hsotg, GHWCFG1));
  663. addr = hsotg->regs + GHWCFG2;
  664. dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
  665. (unsigned long)addr, dwc2_readl(hsotg, GHWCFG2));
  666. addr = hsotg->regs + GHWCFG3;
  667. dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
  668. (unsigned long)addr, dwc2_readl(hsotg, GHWCFG3));
  669. addr = hsotg->regs + GHWCFG4;
  670. dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
  671. (unsigned long)addr, dwc2_readl(hsotg, GHWCFG4));
  672. addr = hsotg->regs + GLPMCFG;
  673. dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
  674. (unsigned long)addr, dwc2_readl(hsotg, GLPMCFG));
  675. addr = hsotg->regs + GPWRDN;
  676. dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
  677. (unsigned long)addr, dwc2_readl(hsotg, GPWRDN));
  678. addr = hsotg->regs + GDFIFOCFG;
  679. dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
  680. (unsigned long)addr, dwc2_readl(hsotg, GDFIFOCFG));
  681. addr = hsotg->regs + HPTXFSIZ;
  682. dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
  683. (unsigned long)addr, dwc2_readl(hsotg, HPTXFSIZ));
  684. addr = hsotg->regs + PCGCTL;
  685. dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
  686. (unsigned long)addr, dwc2_readl(hsotg, PCGCTL));
  687. #endif
  688. }
  689. /**
  690. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  691. *
  692. * @hsotg: Programming view of DWC_otg controller
  693. * @num: Tx FIFO to flush
  694. */
  695. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  696. {
  697. u32 greset;
  698. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  699. /* Wait for AHB master IDLE state */
  700. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
  701. dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
  702. __func__);
  703. greset = GRSTCTL_TXFFLSH;
  704. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  705. dwc2_writel(hsotg, greset, GRSTCTL);
  706. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 10000))
  707. dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_TXFFLSH\n",
  708. __func__);
  709. /* Wait for at least 3 PHY Clocks */
  710. udelay(1);
  711. }
  712. /**
  713. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  714. *
  715. * @hsotg: Programming view of DWC_otg controller
  716. */
  717. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  718. {
  719. u32 greset;
  720. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  721. /* Wait for AHB master IDLE state */
  722. if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
  723. dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
  724. __func__);
  725. greset = GRSTCTL_RXFFLSH;
  726. dwc2_writel(hsotg, greset, GRSTCTL);
  727. /* Wait for RxFIFO flush done */
  728. if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_RXFFLSH, 10000))
  729. dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_RXFFLSH\n",
  730. __func__);
  731. /* Wait for at least 3 PHY Clocks */
  732. udelay(1);
  733. }
  734. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  735. {
  736. if (dwc2_readl(hsotg, GSNPSID) == 0xffffffff)
  737. return false;
  738. else
  739. return true;
  740. }
  741. /**
  742. * dwc2_enable_global_interrupts() - Enables the controller's Global
  743. * Interrupt in the AHB Config register
  744. *
  745. * @hsotg: Programming view of DWC_otg controller
  746. */
  747. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  748. {
  749. u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  750. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  751. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  752. }
  753. /**
  754. * dwc2_disable_global_interrupts() - Disables the controller's Global
  755. * Interrupt in the AHB Config register
  756. *
  757. * @hsotg: Programming view of DWC_otg controller
  758. */
  759. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  760. {
  761. u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  762. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  763. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  764. }
  765. /* Returns the controller's GHWCFG2.OTG_MODE. */
  766. unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg)
  767. {
  768. u32 ghwcfg2 = dwc2_readl(hsotg, GHWCFG2);
  769. return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  770. GHWCFG2_OP_MODE_SHIFT;
  771. }
  772. /* Returns true if the controller is capable of DRD. */
  773. bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
  774. {
  775. unsigned int op_mode = dwc2_op_mode(hsotg);
  776. return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
  777. (op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
  778. (op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
  779. }
  780. /* Returns true if the controller is host-only. */
  781. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
  782. {
  783. unsigned int op_mode = dwc2_op_mode(hsotg);
  784. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
  785. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
  786. }
  787. /* Returns true if the controller is device-only. */
  788. bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
  789. {
  790. unsigned int op_mode = dwc2_op_mode(hsotg);
  791. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
  792. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
  793. }
  794. /**
  795. * dwc2_hsotg_wait_bit_set - Waits for bit to be set.
  796. * @hsotg: Programming view of DWC_otg controller.
  797. * @offset: Register's offset where bit/bits must be set.
  798. * @mask: Mask of the bit/bits which must be set.
  799. * @timeout: Timeout to wait.
  800. *
  801. * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
  802. */
  803. int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
  804. u32 timeout)
  805. {
  806. u32 i;
  807. for (i = 0; i < timeout; i++) {
  808. if (dwc2_readl(hsotg, offset) & mask)
  809. return 0;
  810. udelay(1);
  811. }
  812. return -ETIMEDOUT;
  813. }
  814. /**
  815. * dwc2_hsotg_wait_bit_clear - Waits for bit to be clear.
  816. * @hsotg: Programming view of DWC_otg controller.
  817. * @offset: Register's offset where bit/bits must be set.
  818. * @mask: Mask of the bit/bits which must be set.
  819. * @timeout: Timeout to wait.
  820. *
  821. * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
  822. */
  823. int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
  824. u32 timeout)
  825. {
  826. u32 i;
  827. for (i = 0; i < timeout; i++) {
  828. if (!(dwc2_readl(hsotg, offset) & mask))
  829. return 0;
  830. udelay(1);
  831. }
  832. return -ETIMEDOUT;
  833. }
  834. /*
  835. * Initializes the FSLSPClkSel field of the HCFG register depending on the
  836. * PHY type
  837. */
  838. void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
  839. {
  840. u32 hcfg, val;
  841. if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  842. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
  843. hsotg->params.ulpi_fs_ls) ||
  844. hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  845. /* Full speed PHY */
  846. val = HCFG_FSLSPCLKSEL_48_MHZ;
  847. } else {
  848. /* High speed PHY running at full speed or high speed */
  849. val = HCFG_FSLSPCLKSEL_30_60_MHZ;
  850. }
  851. dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
  852. hcfg = dwc2_readl(hsotg, HCFG);
  853. hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
  854. hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
  855. dwc2_writel(hsotg, hcfg, HCFG);
  856. }
  857. static void dwc2_set_clock_switch_timer(struct dwc2_hsotg *hsotg)
  858. {
  859. u32 grstctl, gsnpsid, val = 0;
  860. gsnpsid = dwc2_readl(hsotg, GSNPSID);
  861. /*
  862. * Applicable only to HSOTG core v5.00a or higher.
  863. * Not applicable to HS/FS IOT devices.
  864. */
  865. if ((gsnpsid & ~DWC2_CORE_REV_MASK) != DWC2_OTG_ID ||
  866. gsnpsid < DWC2_CORE_REV_5_00a)
  867. return;
  868. if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI &&
  869. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED) ||
  870. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  871. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED) ||
  872. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED &&
  873. hsotg->hw_params.fs_phy_type != GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED)) {
  874. val = GRSTCTL_CLOCK_SWITH_TIMER_VALUE_DIS;
  875. }
  876. if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW &&
  877. hsotg->hw_params.hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED &&
  878. hsotg->hw_params.fs_phy_type != GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED) {
  879. val = GRSTCTL_CLOCK_SWITH_TIMER_VALUE_147;
  880. }
  881. grstctl = dwc2_readl(hsotg, GRSTCTL);
  882. grstctl &= ~GRSTCTL_CLOCK_SWITH_TIMER_MASK;
  883. grstctl |= GRSTCTL_CLOCK_SWITH_TIMER(val);
  884. dwc2_writel(hsotg, grstctl, GRSTCTL);
  885. }
  886. static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  887. {
  888. u32 usbcfg, ggpio, i2cctl;
  889. int retval = 0;
  890. /*
  891. * core_init() is now called on every switch so only call the
  892. * following for the first time through
  893. */
  894. if (select_phy) {
  895. dev_dbg(hsotg->dev, "FS PHY selected\n");
  896. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  897. if (!(usbcfg & GUSBCFG_PHYSEL)) {
  898. usbcfg |= GUSBCFG_PHYSEL;
  899. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  900. dwc2_set_clock_switch_timer(hsotg);
  901. /* Reset after a PHY select */
  902. retval = dwc2_core_reset(hsotg, false);
  903. if (retval) {
  904. dev_err(hsotg->dev,
  905. "%s: Reset failed, aborting", __func__);
  906. return retval;
  907. }
  908. }
  909. if (hsotg->params.activate_stm_fs_transceiver) {
  910. ggpio = dwc2_readl(hsotg, GGPIO);
  911. if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
  912. dev_dbg(hsotg->dev, "Activating transceiver\n");
  913. /*
  914. * STM32F4x9 uses the GGPIO register as general
  915. * core configuration register.
  916. */
  917. ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
  918. dwc2_writel(hsotg, ggpio, GGPIO);
  919. }
  920. }
  921. }
  922. /*
  923. * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  924. * do this on HNP Dev/Host mode switches (done in dev_init and
  925. * host_init).
  926. */
  927. if (dwc2_is_host_mode(hsotg))
  928. dwc2_init_fs_ls_pclk_sel(hsotg);
  929. if (hsotg->params.i2c_enable) {
  930. dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
  931. /* Program GUSBCFG.OtgUtmiFsSel to I2C */
  932. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  933. usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
  934. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  935. /* Program GI2CCTL.I2CEn */
  936. i2cctl = dwc2_readl(hsotg, GI2CCTL);
  937. i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
  938. i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
  939. i2cctl &= ~GI2CCTL_I2CEN;
  940. dwc2_writel(hsotg, i2cctl, GI2CCTL);
  941. i2cctl |= GI2CCTL_I2CEN;
  942. dwc2_writel(hsotg, i2cctl, GI2CCTL);
  943. }
  944. return retval;
  945. }
  946. static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  947. {
  948. u32 usbcfg, usbcfg_old;
  949. int retval = 0;
  950. if (!select_phy)
  951. return 0;
  952. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  953. usbcfg_old = usbcfg;
  954. /*
  955. * HS PHY parameters. These parameters are preserved during soft reset
  956. * so only program the first time. Do a soft reset immediately after
  957. * setting phyif.
  958. */
  959. switch (hsotg->params.phy_type) {
  960. case DWC2_PHY_TYPE_PARAM_ULPI:
  961. /* ULPI interface */
  962. dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
  963. usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
  964. usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
  965. if (hsotg->params.phy_ulpi_ddr)
  966. usbcfg |= GUSBCFG_DDRSEL;
  967. /* Set external VBUS indicator as needed. */
  968. if (hsotg->params.oc_disable)
  969. usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
  970. GUSBCFG_INDICATORPASSTHROUGH);
  971. break;
  972. case DWC2_PHY_TYPE_PARAM_UTMI:
  973. /* UTMI+ interface */
  974. dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
  975. usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
  976. if (hsotg->params.phy_utmi_width == 16)
  977. usbcfg |= GUSBCFG_PHYIF16;
  978. break;
  979. default:
  980. dev_err(hsotg->dev, "FS PHY selected at HS!\n");
  981. break;
  982. }
  983. if (usbcfg != usbcfg_old) {
  984. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  985. /* Reset after setting the PHY parameters */
  986. retval = dwc2_core_reset(hsotg, false);
  987. if (retval) {
  988. dev_err(hsotg->dev,
  989. "%s: Reset failed, aborting", __func__);
  990. return retval;
  991. }
  992. }
  993. return retval;
  994. }
  995. static void dwc2_set_turnaround_time(struct dwc2_hsotg *hsotg)
  996. {
  997. u32 usbcfg;
  998. if (hsotg->params.phy_type != DWC2_PHY_TYPE_PARAM_UTMI)
  999. return;
  1000. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1001. usbcfg &= ~GUSBCFG_USBTRDTIM_MASK;
  1002. if (hsotg->params.phy_utmi_width == 16)
  1003. usbcfg |= 5 << GUSBCFG_USBTRDTIM_SHIFT;
  1004. else
  1005. usbcfg |= 9 << GUSBCFG_USBTRDTIM_SHIFT;
  1006. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1007. }
  1008. int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  1009. {
  1010. u32 usbcfg;
  1011. u32 otgctl;
  1012. int retval = 0;
  1013. if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  1014. hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
  1015. hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  1016. /* If FS/LS mode with FS/LS PHY */
  1017. retval = dwc2_fs_phy_init(hsotg, select_phy);
  1018. if (retval)
  1019. return retval;
  1020. } else {
  1021. /* High speed PHY */
  1022. retval = dwc2_hs_phy_init(hsotg, select_phy);
  1023. if (retval)
  1024. return retval;
  1025. if (dwc2_is_device_mode(hsotg))
  1026. dwc2_set_turnaround_time(hsotg);
  1027. }
  1028. if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  1029. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
  1030. hsotg->params.ulpi_fs_ls) {
  1031. dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
  1032. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1033. usbcfg |= GUSBCFG_ULPI_FS_LS;
  1034. usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
  1035. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1036. } else {
  1037. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1038. usbcfg &= ~GUSBCFG_ULPI_FS_LS;
  1039. usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
  1040. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1041. }
  1042. if (!hsotg->params.activate_ingenic_overcurrent_detection) {
  1043. if (dwc2_is_host_mode(hsotg)) {
  1044. otgctl = readl(hsotg->regs + GOTGCTL);
  1045. otgctl |= GOTGCTL_VBVALOEN | GOTGCTL_VBVALOVAL;
  1046. writel(otgctl, hsotg->regs + GOTGCTL);
  1047. }
  1048. }
  1049. return retval;
  1050. }
  1051. MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
  1052. MODULE_AUTHOR("Synopsys, Inc.");
  1053. MODULE_LICENSE("Dual BSD/GPL");