core.c 25 KB

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