core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /**
  2. * core.c - DesignWare USB3 DRD Controller Core file
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/version.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/ioport.h>
  30. #include <linux/io.h>
  31. #include <linux/list.h>
  32. #include <linux/delay.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/of.h>
  35. #include <linux/acpi.h>
  36. #include <linux/pinctrl/consumer.h>
  37. #include <linux/usb/ch9.h>
  38. #include <linux/usb/gadget.h>
  39. #include <linux/usb/of.h>
  40. #include <linux/usb/otg.h>
  41. #include "platform_data.h"
  42. #include "core.h"
  43. #include "gadget.h"
  44. #include "io.h"
  45. #include "debug.h"
  46. /* -------------------------------------------------------------------------- */
  47. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  48. {
  49. u32 reg;
  50. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  51. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  52. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  53. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  54. }
  55. /**
  56. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  57. * @dwc: pointer to our context structure
  58. */
  59. static int dwc3_core_soft_reset(struct dwc3 *dwc)
  60. {
  61. u32 reg;
  62. int ret;
  63. /* Before Resetting PHY, put Core in Reset */
  64. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  65. reg |= DWC3_GCTL_CORESOFTRESET;
  66. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  67. /* Assert USB3 PHY reset */
  68. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  69. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  70. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  71. /* Assert USB2 PHY reset */
  72. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  73. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  74. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  75. usb_phy_init(dwc->usb2_phy);
  76. usb_phy_init(dwc->usb3_phy);
  77. ret = phy_init(dwc->usb2_generic_phy);
  78. if (ret < 0)
  79. return ret;
  80. ret = phy_init(dwc->usb3_generic_phy);
  81. if (ret < 0) {
  82. phy_exit(dwc->usb2_generic_phy);
  83. return ret;
  84. }
  85. mdelay(100);
  86. /* Clear USB3 PHY reset */
  87. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  88. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  89. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  90. /* Clear USB2 PHY reset */
  91. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  92. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  93. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  94. mdelay(100);
  95. /* After PHYs are stable we can take Core out of reset state */
  96. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  97. reg &= ~DWC3_GCTL_CORESOFTRESET;
  98. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  99. return 0;
  100. }
  101. /**
  102. * dwc3_soft_reset - Issue soft reset
  103. * @dwc: Pointer to our controller context structure
  104. */
  105. static int dwc3_soft_reset(struct dwc3 *dwc)
  106. {
  107. unsigned long timeout;
  108. u32 reg;
  109. timeout = jiffies + msecs_to_jiffies(500);
  110. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  111. do {
  112. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  113. if (!(reg & DWC3_DCTL_CSFTRST))
  114. break;
  115. if (time_after(jiffies, timeout)) {
  116. dev_err(dwc->dev, "Reset Timed Out\n");
  117. return -ETIMEDOUT;
  118. }
  119. cpu_relax();
  120. } while (true);
  121. return 0;
  122. }
  123. /*
  124. * dwc3_frame_length_adjustment - Adjusts frame length if required
  125. * @dwc3: Pointer to our controller context structure
  126. * @fladj: Value of GFLADJ_30MHZ to adjust frame length
  127. */
  128. static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj)
  129. {
  130. u32 reg;
  131. u32 dft;
  132. if (dwc->revision < DWC3_REVISION_250A)
  133. return;
  134. if (fladj == 0)
  135. return;
  136. reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
  137. dft = reg & DWC3_GFLADJ_30MHZ_MASK;
  138. if (!dev_WARN_ONCE(dwc->dev, dft == fladj,
  139. "request value same as default, ignoring\n")) {
  140. reg &= ~DWC3_GFLADJ_30MHZ_MASK;
  141. reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | fladj;
  142. dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
  143. }
  144. }
  145. /**
  146. * dwc3_free_one_event_buffer - Frees one event buffer
  147. * @dwc: Pointer to our controller context structure
  148. * @evt: Pointer to event buffer to be freed
  149. */
  150. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  151. struct dwc3_event_buffer *evt)
  152. {
  153. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  154. }
  155. /**
  156. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  157. * @dwc: Pointer to our controller context structure
  158. * @length: size of the event buffer
  159. *
  160. * Returns a pointer to the allocated event buffer structure on success
  161. * otherwise ERR_PTR(errno).
  162. */
  163. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  164. unsigned length)
  165. {
  166. struct dwc3_event_buffer *evt;
  167. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  168. if (!evt)
  169. return ERR_PTR(-ENOMEM);
  170. evt->dwc = dwc;
  171. evt->length = length;
  172. evt->buf = dma_alloc_coherent(dwc->dev, length,
  173. &evt->dma, GFP_KERNEL);
  174. if (!evt->buf)
  175. return ERR_PTR(-ENOMEM);
  176. return evt;
  177. }
  178. /**
  179. * dwc3_free_event_buffers - frees all allocated event buffers
  180. * @dwc: Pointer to our controller context structure
  181. */
  182. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  183. {
  184. struct dwc3_event_buffer *evt;
  185. int i;
  186. for (i = 0; i < dwc->num_event_buffers; i++) {
  187. evt = dwc->ev_buffs[i];
  188. if (evt)
  189. dwc3_free_one_event_buffer(dwc, evt);
  190. }
  191. }
  192. /**
  193. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  194. * @dwc: pointer to our controller context structure
  195. * @length: size of event buffer
  196. *
  197. * Returns 0 on success otherwise negative errno. In the error case, dwc
  198. * may contain some buffers allocated but not all which were requested.
  199. */
  200. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  201. {
  202. int num;
  203. int i;
  204. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  205. dwc->num_event_buffers = num;
  206. dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
  207. GFP_KERNEL);
  208. if (!dwc->ev_buffs)
  209. return -ENOMEM;
  210. for (i = 0; i < num; i++) {
  211. struct dwc3_event_buffer *evt;
  212. evt = dwc3_alloc_one_event_buffer(dwc, length);
  213. if (IS_ERR(evt)) {
  214. dev_err(dwc->dev, "can't allocate event buffer\n");
  215. return PTR_ERR(evt);
  216. }
  217. dwc->ev_buffs[i] = evt;
  218. }
  219. return 0;
  220. }
  221. /**
  222. * dwc3_event_buffers_setup - setup our allocated event buffers
  223. * @dwc: pointer to our controller context structure
  224. *
  225. * Returns 0 on success otherwise negative errno.
  226. */
  227. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  228. {
  229. struct dwc3_event_buffer *evt;
  230. int n;
  231. for (n = 0; n < dwc->num_event_buffers; n++) {
  232. evt = dwc->ev_buffs[n];
  233. dwc3_trace(trace_dwc3_core,
  234. "Event buf %p dma %08llx length %d\n",
  235. evt->buf, (unsigned long long) evt->dma,
  236. evt->length);
  237. evt->lpos = 0;
  238. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  239. lower_32_bits(evt->dma));
  240. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  241. upper_32_bits(evt->dma));
  242. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  243. DWC3_GEVNTSIZ_SIZE(evt->length));
  244. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  245. }
  246. return 0;
  247. }
  248. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  249. {
  250. struct dwc3_event_buffer *evt;
  251. int n;
  252. for (n = 0; n < dwc->num_event_buffers; n++) {
  253. evt = dwc->ev_buffs[n];
  254. evt->lpos = 0;
  255. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  256. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  257. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
  258. | DWC3_GEVNTSIZ_SIZE(0));
  259. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  260. }
  261. }
  262. static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
  263. {
  264. if (!dwc->has_hibernation)
  265. return 0;
  266. if (!dwc->nr_scratch)
  267. return 0;
  268. dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
  269. DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
  270. if (!dwc->scratchbuf)
  271. return -ENOMEM;
  272. return 0;
  273. }
  274. static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
  275. {
  276. dma_addr_t scratch_addr;
  277. u32 param;
  278. int ret;
  279. if (!dwc->has_hibernation)
  280. return 0;
  281. if (!dwc->nr_scratch)
  282. return 0;
  283. /* should never fall here */
  284. if (!WARN_ON(dwc->scratchbuf))
  285. return 0;
  286. scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
  287. dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  288. DMA_BIDIRECTIONAL);
  289. if (dma_mapping_error(dwc->dev, scratch_addr)) {
  290. dev_err(dwc->dev, "failed to map scratch buffer\n");
  291. ret = -EFAULT;
  292. goto err0;
  293. }
  294. dwc->scratch_addr = scratch_addr;
  295. param = lower_32_bits(scratch_addr);
  296. ret = dwc3_send_gadget_generic_command(dwc,
  297. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
  298. if (ret < 0)
  299. goto err1;
  300. param = upper_32_bits(scratch_addr);
  301. ret = dwc3_send_gadget_generic_command(dwc,
  302. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
  303. if (ret < 0)
  304. goto err1;
  305. return 0;
  306. err1:
  307. dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
  308. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  309. err0:
  310. return ret;
  311. }
  312. static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
  313. {
  314. if (!dwc->has_hibernation)
  315. return;
  316. if (!dwc->nr_scratch)
  317. return;
  318. /* should never fall here */
  319. if (!WARN_ON(dwc->scratchbuf))
  320. return;
  321. dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
  322. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  323. kfree(dwc->scratchbuf);
  324. }
  325. static void dwc3_core_num_eps(struct dwc3 *dwc)
  326. {
  327. struct dwc3_hwparams *parms = &dwc->hwparams;
  328. dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
  329. dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
  330. dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
  331. dwc->num_in_eps, dwc->num_out_eps);
  332. }
  333. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  334. {
  335. struct dwc3_hwparams *parms = &dwc->hwparams;
  336. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  337. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  338. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  339. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  340. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  341. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  342. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  343. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  344. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  345. }
  346. /**
  347. * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
  348. * @dwc: Pointer to our controller context structure
  349. *
  350. * Returns 0 on success. The USB PHY interfaces are configured but not
  351. * initialized. The PHY interfaces and the PHYs get initialized together with
  352. * the core in dwc3_core_init.
  353. */
  354. static int dwc3_phy_setup(struct dwc3 *dwc)
  355. {
  356. u32 reg;
  357. int ret;
  358. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  359. /*
  360. * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
  361. * to '0' during coreConsultant configuration. So default value
  362. * will be '0' when the core is reset. Application needs to set it
  363. * to '1' after the core initialization is completed.
  364. */
  365. if (dwc->revision > DWC3_REVISION_194A)
  366. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  367. if (dwc->u2ss_inp3_quirk)
  368. reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
  369. if (dwc->req_p1p2p3_quirk)
  370. reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
  371. if (dwc->del_p1p2p3_quirk)
  372. reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
  373. if (dwc->del_phy_power_chg_quirk)
  374. reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
  375. if (dwc->lfps_filter_quirk)
  376. reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
  377. if (dwc->rx_detect_poll_quirk)
  378. reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
  379. if (dwc->tx_de_emphasis_quirk)
  380. reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
  381. if (dwc->dis_u3_susphy_quirk)
  382. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  383. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  384. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  385. /* Select the HS PHY interface */
  386. switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
  387. case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
  388. if (dwc->hsphy_interface &&
  389. !strncmp(dwc->hsphy_interface, "utmi", 4)) {
  390. reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
  391. break;
  392. } else if (dwc->hsphy_interface &&
  393. !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
  394. reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
  395. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  396. } else {
  397. /* Relying on default value. */
  398. if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
  399. break;
  400. }
  401. /* FALLTHROUGH */
  402. case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
  403. /* Making sure the interface and PHY are operational */
  404. ret = dwc3_soft_reset(dwc);
  405. if (ret)
  406. return ret;
  407. udelay(1);
  408. ret = dwc3_ulpi_init(dwc);
  409. if (ret)
  410. return ret;
  411. /* FALLTHROUGH */
  412. default:
  413. break;
  414. }
  415. /*
  416. * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
  417. * '0' during coreConsultant configuration. So default value will
  418. * be '0' when the core is reset. Application needs to set it to
  419. * '1' after the core initialization is completed.
  420. */
  421. if (dwc->revision > DWC3_REVISION_194A)
  422. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  423. if (dwc->dis_u2_susphy_quirk)
  424. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  425. if (dwc->dis_enblslpm_quirk)
  426. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  427. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  428. return 0;
  429. }
  430. /**
  431. * dwc3_core_init - Low-level initialization of DWC3 Core
  432. * @dwc: Pointer to our controller context structure
  433. *
  434. * Returns 0 on success otherwise negative errno.
  435. */
  436. static int dwc3_core_init(struct dwc3 *dwc)
  437. {
  438. u32 hwparams4 = dwc->hwparams.hwparams4;
  439. u32 reg;
  440. int ret;
  441. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  442. /* This should read as U3 followed by revision number */
  443. if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
  444. /* Detected DWC_usb3 IP */
  445. dwc->revision = reg;
  446. } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
  447. /* Detected DWC_usb31 IP */
  448. dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
  449. dwc->revision |= DWC3_REVISION_IS_DWC31;
  450. } else {
  451. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  452. ret = -ENODEV;
  453. goto err0;
  454. }
  455. /*
  456. * Write Linux Version Code to our GUID register so it's easy to figure
  457. * out which kernel version a bug was found.
  458. */
  459. dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
  460. /* Handle USB2.0-only core configuration */
  461. if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  462. DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
  463. if (dwc->maximum_speed == USB_SPEED_SUPER)
  464. dwc->maximum_speed = USB_SPEED_HIGH;
  465. }
  466. /* issue device SoftReset too */
  467. ret = dwc3_soft_reset(dwc);
  468. if (ret)
  469. goto err0;
  470. ret = dwc3_core_soft_reset(dwc);
  471. if (ret)
  472. goto err0;
  473. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  474. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  475. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  476. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  477. /**
  478. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  479. * issue which would cause xHCI compliance tests to fail.
  480. *
  481. * Because of that we cannot enable clock gating on such
  482. * configurations.
  483. *
  484. * Refers to:
  485. *
  486. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  487. * SOF/ITP Mode Used
  488. */
  489. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  490. dwc->dr_mode == USB_DR_MODE_OTG) &&
  491. (dwc->revision >= DWC3_REVISION_210A &&
  492. dwc->revision <= DWC3_REVISION_250A))
  493. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  494. else
  495. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  496. break;
  497. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  498. /* enable hibernation here */
  499. dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
  500. /*
  501. * REVISIT Enabling this bit so that host-mode hibernation
  502. * will work. Device-mode hibernation is not yet implemented.
  503. */
  504. reg |= DWC3_GCTL_GBLHIBERNATIONEN;
  505. break;
  506. default:
  507. dwc3_trace(trace_dwc3_core, "No power optimization available\n");
  508. }
  509. /* check if current dwc3 is on simulation board */
  510. if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
  511. dwc3_trace(trace_dwc3_core,
  512. "running on FPGA platform\n");
  513. dwc->is_fpga = true;
  514. }
  515. WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
  516. "disable_scramble cannot be used on non-FPGA builds\n");
  517. if (dwc->disable_scramble_quirk && dwc->is_fpga)
  518. reg |= DWC3_GCTL_DISSCRAMBLE;
  519. else
  520. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  521. if (dwc->u2exit_lfps_quirk)
  522. reg |= DWC3_GCTL_U2EXIT_LFPS;
  523. /*
  524. * WORKAROUND: DWC3 revisions <1.90a have a bug
  525. * where the device can fail to connect at SuperSpeed
  526. * and falls back to high-speed mode which causes
  527. * the device to enter a Connect/Disconnect loop
  528. */
  529. if (dwc->revision < DWC3_REVISION_190A)
  530. reg |= DWC3_GCTL_U2RSTECN;
  531. dwc3_core_num_eps(dwc);
  532. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  533. ret = dwc3_alloc_scratch_buffers(dwc);
  534. if (ret)
  535. goto err1;
  536. ret = dwc3_setup_scratch_buffers(dwc);
  537. if (ret)
  538. goto err2;
  539. return 0;
  540. err2:
  541. dwc3_free_scratch_buffers(dwc);
  542. err1:
  543. usb_phy_shutdown(dwc->usb2_phy);
  544. usb_phy_shutdown(dwc->usb3_phy);
  545. phy_exit(dwc->usb2_generic_phy);
  546. phy_exit(dwc->usb3_generic_phy);
  547. err0:
  548. return ret;
  549. }
  550. static void dwc3_core_exit(struct dwc3 *dwc)
  551. {
  552. dwc3_free_scratch_buffers(dwc);
  553. usb_phy_shutdown(dwc->usb2_phy);
  554. usb_phy_shutdown(dwc->usb3_phy);
  555. phy_exit(dwc->usb2_generic_phy);
  556. phy_exit(dwc->usb3_generic_phy);
  557. }
  558. static int dwc3_core_get_phy(struct dwc3 *dwc)
  559. {
  560. struct device *dev = dwc->dev;
  561. struct device_node *node = dev->of_node;
  562. int ret;
  563. if (node) {
  564. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  565. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  566. } else {
  567. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  568. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  569. }
  570. if (IS_ERR(dwc->usb2_phy)) {
  571. ret = PTR_ERR(dwc->usb2_phy);
  572. if (ret == -ENXIO || ret == -ENODEV) {
  573. dwc->usb2_phy = NULL;
  574. } else if (ret == -EPROBE_DEFER) {
  575. return ret;
  576. } else {
  577. dev_err(dev, "no usb2 phy configured\n");
  578. return ret;
  579. }
  580. }
  581. if (IS_ERR(dwc->usb3_phy)) {
  582. ret = PTR_ERR(dwc->usb3_phy);
  583. if (ret == -ENXIO || ret == -ENODEV) {
  584. dwc->usb3_phy = NULL;
  585. } else if (ret == -EPROBE_DEFER) {
  586. return ret;
  587. } else {
  588. dev_err(dev, "no usb3 phy configured\n");
  589. return ret;
  590. }
  591. }
  592. dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
  593. if (IS_ERR(dwc->usb2_generic_phy)) {
  594. ret = PTR_ERR(dwc->usb2_generic_phy);
  595. if (ret == -ENOSYS || ret == -ENODEV) {
  596. dwc->usb2_generic_phy = NULL;
  597. } else if (ret == -EPROBE_DEFER) {
  598. return ret;
  599. } else {
  600. dev_err(dev, "no usb2 phy configured\n");
  601. return ret;
  602. }
  603. }
  604. dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
  605. if (IS_ERR(dwc->usb3_generic_phy)) {
  606. ret = PTR_ERR(dwc->usb3_generic_phy);
  607. if (ret == -ENOSYS || ret == -ENODEV) {
  608. dwc->usb3_generic_phy = NULL;
  609. } else if (ret == -EPROBE_DEFER) {
  610. return ret;
  611. } else {
  612. dev_err(dev, "no usb3 phy configured\n");
  613. return ret;
  614. }
  615. }
  616. return 0;
  617. }
  618. static int dwc3_core_init_mode(struct dwc3 *dwc)
  619. {
  620. struct device *dev = dwc->dev;
  621. int ret;
  622. switch (dwc->dr_mode) {
  623. case USB_DR_MODE_PERIPHERAL:
  624. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  625. ret = dwc3_gadget_init(dwc);
  626. if (ret) {
  627. dev_err(dev, "failed to initialize gadget\n");
  628. return ret;
  629. }
  630. break;
  631. case USB_DR_MODE_HOST:
  632. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  633. ret = dwc3_host_init(dwc);
  634. if (ret) {
  635. dev_err(dev, "failed to initialize host\n");
  636. return ret;
  637. }
  638. break;
  639. case USB_DR_MODE_OTG:
  640. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  641. ret = dwc3_host_init(dwc);
  642. if (ret) {
  643. dev_err(dev, "failed to initialize host\n");
  644. return ret;
  645. }
  646. ret = dwc3_gadget_init(dwc);
  647. if (ret) {
  648. dev_err(dev, "failed to initialize gadget\n");
  649. return ret;
  650. }
  651. break;
  652. default:
  653. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  654. return -EINVAL;
  655. }
  656. return 0;
  657. }
  658. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  659. {
  660. switch (dwc->dr_mode) {
  661. case USB_DR_MODE_PERIPHERAL:
  662. dwc3_gadget_exit(dwc);
  663. break;
  664. case USB_DR_MODE_HOST:
  665. dwc3_host_exit(dwc);
  666. break;
  667. case USB_DR_MODE_OTG:
  668. dwc3_host_exit(dwc);
  669. dwc3_gadget_exit(dwc);
  670. break;
  671. default:
  672. /* do nothing */
  673. break;
  674. }
  675. }
  676. #define DWC3_ALIGN_MASK (16 - 1)
  677. static int dwc3_probe(struct platform_device *pdev)
  678. {
  679. struct device *dev = &pdev->dev;
  680. struct dwc3_platform_data *pdata = dev_get_platdata(dev);
  681. struct resource *res;
  682. struct dwc3 *dwc;
  683. u8 lpm_nyet_threshold;
  684. u8 tx_de_emphasis;
  685. u8 hird_threshold;
  686. u32 fladj = 0;
  687. int ret;
  688. void __iomem *regs;
  689. void *mem;
  690. mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  691. if (!mem)
  692. return -ENOMEM;
  693. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  694. dwc->mem = mem;
  695. dwc->dev = dev;
  696. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  697. if (!res) {
  698. dev_err(dev, "missing IRQ\n");
  699. return -ENODEV;
  700. }
  701. dwc->xhci_resources[1].start = res->start;
  702. dwc->xhci_resources[1].end = res->end;
  703. dwc->xhci_resources[1].flags = res->flags;
  704. dwc->xhci_resources[1].name = res->name;
  705. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  706. if (!res) {
  707. dev_err(dev, "missing memory resource\n");
  708. return -ENODEV;
  709. }
  710. dwc->xhci_resources[0].start = res->start;
  711. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  712. DWC3_XHCI_REGS_END;
  713. dwc->xhci_resources[0].flags = res->flags;
  714. dwc->xhci_resources[0].name = res->name;
  715. res->start += DWC3_GLOBALS_REGS_START;
  716. /*
  717. * Request memory region but exclude xHCI regs,
  718. * since it will be requested by the xhci-plat driver.
  719. */
  720. regs = devm_ioremap_resource(dev, res);
  721. if (IS_ERR(regs)) {
  722. ret = PTR_ERR(regs);
  723. goto err0;
  724. }
  725. dwc->regs = regs;
  726. dwc->regs_size = resource_size(res);
  727. /* default to highest possible threshold */
  728. lpm_nyet_threshold = 0xff;
  729. /* default to -3.5dB de-emphasis */
  730. tx_de_emphasis = 1;
  731. /*
  732. * default to assert utmi_sleep_n and use maximum allowed HIRD
  733. * threshold value of 0b1100
  734. */
  735. hird_threshold = 12;
  736. dwc->maximum_speed = usb_get_maximum_speed(dev);
  737. dwc->dr_mode = usb_get_dr_mode(dev);
  738. dwc->has_lpm_erratum = device_property_read_bool(dev,
  739. "snps,has-lpm-erratum");
  740. device_property_read_u8(dev, "snps,lpm-nyet-threshold",
  741. &lpm_nyet_threshold);
  742. dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
  743. "snps,is-utmi-l1-suspend");
  744. device_property_read_u8(dev, "snps,hird-threshold",
  745. &hird_threshold);
  746. dwc->usb3_lpm_capable = device_property_read_bool(dev,
  747. "snps,usb3_lpm_capable");
  748. dwc->needs_fifo_resize = device_property_read_bool(dev,
  749. "tx-fifo-resize");
  750. dwc->disable_scramble_quirk = device_property_read_bool(dev,
  751. "snps,disable_scramble_quirk");
  752. dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
  753. "snps,u2exit_lfps_quirk");
  754. dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
  755. "snps,u2ss_inp3_quirk");
  756. dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
  757. "snps,req_p1p2p3_quirk");
  758. dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
  759. "snps,del_p1p2p3_quirk");
  760. dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
  761. "snps,del_phy_power_chg_quirk");
  762. dwc->lfps_filter_quirk = device_property_read_bool(dev,
  763. "snps,lfps_filter_quirk");
  764. dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
  765. "snps,rx_detect_poll_quirk");
  766. dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
  767. "snps,dis_u3_susphy_quirk");
  768. dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
  769. "snps,dis_u2_susphy_quirk");
  770. dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
  771. "snps,dis_enblslpm_quirk");
  772. dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
  773. "snps,tx_de_emphasis_quirk");
  774. device_property_read_u8(dev, "snps,tx_de_emphasis",
  775. &tx_de_emphasis);
  776. device_property_read_string(dev, "snps,hsphy_interface",
  777. &dwc->hsphy_interface);
  778. device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
  779. &fladj);
  780. if (pdata) {
  781. dwc->maximum_speed = pdata->maximum_speed;
  782. dwc->has_lpm_erratum = pdata->has_lpm_erratum;
  783. if (pdata->lpm_nyet_threshold)
  784. lpm_nyet_threshold = pdata->lpm_nyet_threshold;
  785. dwc->is_utmi_l1_suspend = pdata->is_utmi_l1_suspend;
  786. if (pdata->hird_threshold)
  787. hird_threshold = pdata->hird_threshold;
  788. dwc->needs_fifo_resize = pdata->tx_fifo_resize;
  789. dwc->usb3_lpm_capable = pdata->usb3_lpm_capable;
  790. dwc->dr_mode = pdata->dr_mode;
  791. dwc->disable_scramble_quirk = pdata->disable_scramble_quirk;
  792. dwc->u2exit_lfps_quirk = pdata->u2exit_lfps_quirk;
  793. dwc->u2ss_inp3_quirk = pdata->u2ss_inp3_quirk;
  794. dwc->req_p1p2p3_quirk = pdata->req_p1p2p3_quirk;
  795. dwc->del_p1p2p3_quirk = pdata->del_p1p2p3_quirk;
  796. dwc->del_phy_power_chg_quirk = pdata->del_phy_power_chg_quirk;
  797. dwc->lfps_filter_quirk = pdata->lfps_filter_quirk;
  798. dwc->rx_detect_poll_quirk = pdata->rx_detect_poll_quirk;
  799. dwc->dis_u3_susphy_quirk = pdata->dis_u3_susphy_quirk;
  800. dwc->dis_u2_susphy_quirk = pdata->dis_u2_susphy_quirk;
  801. dwc->dis_enblslpm_quirk = pdata->dis_enblslpm_quirk;
  802. dwc->tx_de_emphasis_quirk = pdata->tx_de_emphasis_quirk;
  803. if (pdata->tx_de_emphasis)
  804. tx_de_emphasis = pdata->tx_de_emphasis;
  805. dwc->hsphy_interface = pdata->hsphy_interface;
  806. fladj = pdata->fladj_value;
  807. }
  808. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  809. dwc->tx_de_emphasis = tx_de_emphasis;
  810. dwc->hird_threshold = hird_threshold
  811. | (dwc->is_utmi_l1_suspend << 4);
  812. platform_set_drvdata(pdev, dwc);
  813. dwc3_cache_hwparams(dwc);
  814. ret = dwc3_phy_setup(dwc);
  815. if (ret)
  816. goto err0;
  817. ret = dwc3_core_get_phy(dwc);
  818. if (ret)
  819. goto err0;
  820. spin_lock_init(&dwc->lock);
  821. if (!dev->dma_mask) {
  822. dev->dma_mask = dev->parent->dma_mask;
  823. dev->dma_parms = dev->parent->dma_parms;
  824. dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
  825. }
  826. pm_runtime_enable(dev);
  827. pm_runtime_get_sync(dev);
  828. pm_runtime_forbid(dev);
  829. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  830. if (ret) {
  831. dev_err(dwc->dev, "failed to allocate event buffers\n");
  832. ret = -ENOMEM;
  833. goto err1;
  834. }
  835. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  836. dwc->dr_mode = USB_DR_MODE_HOST;
  837. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  838. dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
  839. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  840. dwc->dr_mode = USB_DR_MODE_OTG;
  841. ret = dwc3_core_init(dwc);
  842. if (ret) {
  843. dev_err(dev, "failed to initialize core\n");
  844. goto err1;
  845. }
  846. /* Check the maximum_speed parameter */
  847. switch (dwc->maximum_speed) {
  848. case USB_SPEED_LOW:
  849. case USB_SPEED_FULL:
  850. case USB_SPEED_HIGH:
  851. case USB_SPEED_SUPER:
  852. case USB_SPEED_SUPER_PLUS:
  853. break;
  854. default:
  855. dev_err(dev, "invalid maximum_speed parameter %d\n",
  856. dwc->maximum_speed);
  857. /* fall through */
  858. case USB_SPEED_UNKNOWN:
  859. /* default to superspeed */
  860. dwc->maximum_speed = USB_SPEED_SUPER;
  861. /*
  862. * default to superspeed plus if we are capable.
  863. */
  864. if (dwc3_is_usb31(dwc) &&
  865. (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  866. DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
  867. dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
  868. break;
  869. }
  870. /* Adjust Frame Length */
  871. dwc3_frame_length_adjustment(dwc, fladj);
  872. usb_phy_set_suspend(dwc->usb2_phy, 0);
  873. usb_phy_set_suspend(dwc->usb3_phy, 0);
  874. ret = phy_power_on(dwc->usb2_generic_phy);
  875. if (ret < 0)
  876. goto err2;
  877. ret = phy_power_on(dwc->usb3_generic_phy);
  878. if (ret < 0)
  879. goto err3;
  880. ret = dwc3_event_buffers_setup(dwc);
  881. if (ret) {
  882. dev_err(dwc->dev, "failed to setup event buffers\n");
  883. goto err4;
  884. }
  885. ret = dwc3_core_init_mode(dwc);
  886. if (ret)
  887. goto err5;
  888. ret = dwc3_debugfs_init(dwc);
  889. if (ret) {
  890. dev_err(dev, "failed to initialize debugfs\n");
  891. goto err6;
  892. }
  893. pm_runtime_allow(dev);
  894. return 0;
  895. err6:
  896. dwc3_core_exit_mode(dwc);
  897. err5:
  898. dwc3_event_buffers_cleanup(dwc);
  899. err4:
  900. phy_power_off(dwc->usb3_generic_phy);
  901. err3:
  902. phy_power_off(dwc->usb2_generic_phy);
  903. err2:
  904. usb_phy_set_suspend(dwc->usb2_phy, 1);
  905. usb_phy_set_suspend(dwc->usb3_phy, 1);
  906. dwc3_core_exit(dwc);
  907. err1:
  908. dwc3_free_event_buffers(dwc);
  909. dwc3_ulpi_exit(dwc);
  910. err0:
  911. /*
  912. * restore res->start back to its original value so that, in case the
  913. * probe is deferred, we don't end up getting error in request the
  914. * memory region the next time probe is called.
  915. */
  916. res->start -= DWC3_GLOBALS_REGS_START;
  917. return ret;
  918. }
  919. static int dwc3_remove(struct platform_device *pdev)
  920. {
  921. struct dwc3 *dwc = platform_get_drvdata(pdev);
  922. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  923. /*
  924. * restore res->start back to its original value so that, in case the
  925. * probe is deferred, we don't end up getting error in request the
  926. * memory region the next time probe is called.
  927. */
  928. res->start -= DWC3_GLOBALS_REGS_START;
  929. dwc3_debugfs_exit(dwc);
  930. dwc3_core_exit_mode(dwc);
  931. dwc3_event_buffers_cleanup(dwc);
  932. dwc3_free_event_buffers(dwc);
  933. usb_phy_set_suspend(dwc->usb2_phy, 1);
  934. usb_phy_set_suspend(dwc->usb3_phy, 1);
  935. phy_power_off(dwc->usb2_generic_phy);
  936. phy_power_off(dwc->usb3_generic_phy);
  937. dwc3_core_exit(dwc);
  938. dwc3_ulpi_exit(dwc);
  939. pm_runtime_put_sync(&pdev->dev);
  940. pm_runtime_disable(&pdev->dev);
  941. return 0;
  942. }
  943. #ifdef CONFIG_PM_SLEEP
  944. static int dwc3_suspend(struct device *dev)
  945. {
  946. struct dwc3 *dwc = dev_get_drvdata(dev);
  947. unsigned long flags;
  948. spin_lock_irqsave(&dwc->lock, flags);
  949. switch (dwc->dr_mode) {
  950. case USB_DR_MODE_PERIPHERAL:
  951. case USB_DR_MODE_OTG:
  952. dwc3_gadget_suspend(dwc);
  953. /* FALLTHROUGH */
  954. case USB_DR_MODE_HOST:
  955. default:
  956. dwc3_event_buffers_cleanup(dwc);
  957. break;
  958. }
  959. dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
  960. spin_unlock_irqrestore(&dwc->lock, flags);
  961. usb_phy_shutdown(dwc->usb3_phy);
  962. usb_phy_shutdown(dwc->usb2_phy);
  963. phy_exit(dwc->usb2_generic_phy);
  964. phy_exit(dwc->usb3_generic_phy);
  965. pinctrl_pm_select_sleep_state(dev);
  966. return 0;
  967. }
  968. static int dwc3_resume(struct device *dev)
  969. {
  970. struct dwc3 *dwc = dev_get_drvdata(dev);
  971. unsigned long flags;
  972. int ret;
  973. pinctrl_pm_select_default_state(dev);
  974. usb_phy_init(dwc->usb3_phy);
  975. usb_phy_init(dwc->usb2_phy);
  976. ret = phy_init(dwc->usb2_generic_phy);
  977. if (ret < 0)
  978. return ret;
  979. ret = phy_init(dwc->usb3_generic_phy);
  980. if (ret < 0)
  981. goto err_usb2phy_init;
  982. spin_lock_irqsave(&dwc->lock, flags);
  983. dwc3_event_buffers_setup(dwc);
  984. dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
  985. switch (dwc->dr_mode) {
  986. case USB_DR_MODE_PERIPHERAL:
  987. case USB_DR_MODE_OTG:
  988. dwc3_gadget_resume(dwc);
  989. /* FALLTHROUGH */
  990. case USB_DR_MODE_HOST:
  991. default:
  992. /* do nothing */
  993. break;
  994. }
  995. spin_unlock_irqrestore(&dwc->lock, flags);
  996. pm_runtime_disable(dev);
  997. pm_runtime_set_active(dev);
  998. pm_runtime_enable(dev);
  999. return 0;
  1000. err_usb2phy_init:
  1001. phy_exit(dwc->usb2_generic_phy);
  1002. return ret;
  1003. }
  1004. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  1005. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  1006. };
  1007. #define DWC3_PM_OPS &(dwc3_dev_pm_ops)
  1008. #else
  1009. #define DWC3_PM_OPS NULL
  1010. #endif
  1011. #ifdef CONFIG_OF
  1012. static const struct of_device_id of_dwc3_match[] = {
  1013. {
  1014. .compatible = "snps,dwc3"
  1015. },
  1016. {
  1017. .compatible = "synopsys,dwc3"
  1018. },
  1019. { },
  1020. };
  1021. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  1022. #endif
  1023. #ifdef CONFIG_ACPI
  1024. #define ACPI_ID_INTEL_BSW "808622B7"
  1025. static const struct acpi_device_id dwc3_acpi_match[] = {
  1026. { ACPI_ID_INTEL_BSW, 0 },
  1027. { },
  1028. };
  1029. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  1030. #endif
  1031. static struct platform_driver dwc3_driver = {
  1032. .probe = dwc3_probe,
  1033. .remove = dwc3_remove,
  1034. .driver = {
  1035. .name = "dwc3",
  1036. .of_match_table = of_match_ptr(of_dwc3_match),
  1037. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  1038. .pm = DWC3_PM_OPS,
  1039. },
  1040. };
  1041. module_platform_driver(dwc3_driver);
  1042. MODULE_ALIAS("platform:dwc3");
  1043. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1044. MODULE_LICENSE("GPL v2");
  1045. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");