core.c 27 KB

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