core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * core.c - DesignWare USB3 DRD Controller Core file
  4. *
  5. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  9. *
  10. * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/core.c) and ported
  11. * to uboot.
  12. *
  13. * commit cd72f890d2 : usb: dwc3: core: enable phy suspend quirk on non-FPGA
  14. */
  15. #include <common.h>
  16. #include <malloc.h>
  17. #include <dwc3-uboot.h>
  18. #include <asm/dma-mapping.h>
  19. #include <linux/ioport.h>
  20. #include <dm.h>
  21. #include <linux/usb/ch9.h>
  22. #include <linux/usb/gadget.h>
  23. #include "core.h"
  24. #include "gadget.h"
  25. #include "io.h"
  26. #include "linux-compat.h"
  27. static LIST_HEAD(dwc3_list);
  28. /* -------------------------------------------------------------------------- */
  29. static void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  30. {
  31. u32 reg;
  32. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  33. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  34. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  35. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  36. }
  37. /**
  38. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  39. * @dwc: pointer to our context structure
  40. */
  41. static int dwc3_core_soft_reset(struct dwc3 *dwc)
  42. {
  43. u32 reg;
  44. /* Before Resetting PHY, put Core in Reset */
  45. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  46. reg |= DWC3_GCTL_CORESOFTRESET;
  47. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  48. /* Assert USB3 PHY reset */
  49. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  50. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  51. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  52. /* Assert USB2 PHY reset */
  53. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  54. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  55. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  56. mdelay(100);
  57. /* Clear USB3 PHY reset */
  58. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  59. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  60. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  61. /* Clear USB2 PHY reset */
  62. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  63. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  64. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  65. mdelay(100);
  66. /* After PHYs are stable we can take Core out of reset state */
  67. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  68. reg &= ~DWC3_GCTL_CORESOFTRESET;
  69. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  70. return 0;
  71. }
  72. /**
  73. * dwc3_free_one_event_buffer - Frees one event buffer
  74. * @dwc: Pointer to our controller context structure
  75. * @evt: Pointer to event buffer to be freed
  76. */
  77. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  78. struct dwc3_event_buffer *evt)
  79. {
  80. dma_free_coherent(evt->buf);
  81. }
  82. /**
  83. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  84. * @dwc: Pointer to our controller context structure
  85. * @length: size of the event buffer
  86. *
  87. * Returns a pointer to the allocated event buffer structure on success
  88. * otherwise ERR_PTR(errno).
  89. */
  90. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  91. unsigned length)
  92. {
  93. struct dwc3_event_buffer *evt;
  94. evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt),
  95. GFP_KERNEL);
  96. if (!evt)
  97. return ERR_PTR(-ENOMEM);
  98. evt->dwc = dwc;
  99. evt->length = length;
  100. evt->buf = dma_alloc_coherent(length,
  101. (unsigned long *)&evt->dma);
  102. if (!evt->buf)
  103. return ERR_PTR(-ENOMEM);
  104. dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
  105. return evt;
  106. }
  107. /**
  108. * dwc3_free_event_buffers - frees all allocated event buffers
  109. * @dwc: Pointer to our controller context structure
  110. */
  111. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  112. {
  113. struct dwc3_event_buffer *evt;
  114. int i;
  115. for (i = 0; i < dwc->num_event_buffers; i++) {
  116. evt = dwc->ev_buffs[i];
  117. if (evt)
  118. dwc3_free_one_event_buffer(dwc, evt);
  119. }
  120. }
  121. /**
  122. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  123. * @dwc: pointer to our controller context structure
  124. * @length: size of event buffer
  125. *
  126. * Returns 0 on success otherwise negative errno. In the error case, dwc
  127. * may contain some buffers allocated but not all which were requested.
  128. */
  129. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  130. {
  131. int num;
  132. int i;
  133. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  134. dwc->num_event_buffers = num;
  135. dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE,
  136. sizeof(*dwc->ev_buffs) * num);
  137. if (!dwc->ev_buffs)
  138. return -ENOMEM;
  139. for (i = 0; i < num; i++) {
  140. struct dwc3_event_buffer *evt;
  141. evt = dwc3_alloc_one_event_buffer(dwc, length);
  142. if (IS_ERR(evt)) {
  143. dev_err(dwc->dev, "can't allocate event buffer\n");
  144. return PTR_ERR(evt);
  145. }
  146. dwc->ev_buffs[i] = evt;
  147. }
  148. return 0;
  149. }
  150. /**
  151. * dwc3_event_buffers_setup - setup our allocated event buffers
  152. * @dwc: pointer to our controller context structure
  153. *
  154. * Returns 0 on success otherwise negative errno.
  155. */
  156. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  157. {
  158. struct dwc3_event_buffer *evt;
  159. int n;
  160. for (n = 0; n < dwc->num_event_buffers; n++) {
  161. evt = dwc->ev_buffs[n];
  162. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  163. evt->buf, (unsigned long long) evt->dma,
  164. evt->length);
  165. evt->lpos = 0;
  166. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  167. lower_32_bits(evt->dma));
  168. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  169. upper_32_bits(evt->dma));
  170. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  171. DWC3_GEVNTSIZ_SIZE(evt->length));
  172. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  173. }
  174. return 0;
  175. }
  176. static void dwc3_event_buffers_cleanup(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. evt->lpos = 0;
  183. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  184. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  185. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
  186. | DWC3_GEVNTSIZ_SIZE(0));
  187. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  188. }
  189. }
  190. static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
  191. {
  192. if (!dwc->has_hibernation)
  193. return 0;
  194. if (!dwc->nr_scratch)
  195. return 0;
  196. dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
  197. DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
  198. if (!dwc->scratchbuf)
  199. return -ENOMEM;
  200. return 0;
  201. }
  202. static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
  203. {
  204. dma_addr_t scratch_addr;
  205. u32 param;
  206. int ret;
  207. if (!dwc->has_hibernation)
  208. return 0;
  209. if (!dwc->nr_scratch)
  210. return 0;
  211. scratch_addr = dma_map_single(dwc->scratchbuf,
  212. dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  213. DMA_BIDIRECTIONAL);
  214. if (dma_mapping_error(dwc->dev, scratch_addr)) {
  215. dev_err(dwc->dev, "failed to map scratch buffer\n");
  216. ret = -EFAULT;
  217. goto err0;
  218. }
  219. dwc->scratch_addr = scratch_addr;
  220. param = lower_32_bits(scratch_addr);
  221. ret = dwc3_send_gadget_generic_command(dwc,
  222. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
  223. if (ret < 0)
  224. goto err1;
  225. param = upper_32_bits(scratch_addr);
  226. ret = dwc3_send_gadget_generic_command(dwc,
  227. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
  228. if (ret < 0)
  229. goto err1;
  230. return 0;
  231. err1:
  232. dma_unmap_single((void *)(uintptr_t)dwc->scratch_addr, dwc->nr_scratch *
  233. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  234. err0:
  235. return ret;
  236. }
  237. static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
  238. {
  239. if (!dwc->has_hibernation)
  240. return;
  241. if (!dwc->nr_scratch)
  242. return;
  243. dma_unmap_single((void *)(uintptr_t)dwc->scratch_addr, dwc->nr_scratch *
  244. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  245. kfree(dwc->scratchbuf);
  246. }
  247. static void dwc3_core_num_eps(struct dwc3 *dwc)
  248. {
  249. struct dwc3_hwparams *parms = &dwc->hwparams;
  250. dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
  251. dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
  252. dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
  253. dwc->num_in_eps, dwc->num_out_eps);
  254. }
  255. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  256. {
  257. struct dwc3_hwparams *parms = &dwc->hwparams;
  258. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  259. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  260. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  261. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  262. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  263. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  264. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  265. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  266. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  267. }
  268. /**
  269. * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
  270. * @dwc: Pointer to our controller context structure
  271. */
  272. static void dwc3_phy_setup(struct dwc3 *dwc)
  273. {
  274. u32 reg;
  275. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  276. /*
  277. * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
  278. * to '0' during coreConsultant configuration. So default value
  279. * will be '0' when the core is reset. Application needs to set it
  280. * to '1' after the core initialization is completed.
  281. */
  282. if (dwc->revision > DWC3_REVISION_194A)
  283. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  284. if (dwc->u2ss_inp3_quirk)
  285. reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
  286. if (dwc->req_p1p2p3_quirk)
  287. reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
  288. if (dwc->del_p1p2p3_quirk)
  289. reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
  290. if (dwc->del_phy_power_chg_quirk)
  291. reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
  292. if (dwc->lfps_filter_quirk)
  293. reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
  294. if (dwc->rx_detect_poll_quirk)
  295. reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
  296. if (dwc->tx_de_emphasis_quirk)
  297. reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
  298. if (dwc->dis_u3_susphy_quirk)
  299. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  300. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  301. mdelay(100);
  302. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  303. /*
  304. * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
  305. * '0' during coreConsultant configuration. So default value will
  306. * be '0' when the core is reset. Application needs to set it to
  307. * '1' after the core initialization is completed.
  308. */
  309. if (dwc->revision > DWC3_REVISION_194A)
  310. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  311. if (dwc->dis_u2_susphy_quirk)
  312. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  313. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  314. mdelay(100);
  315. }
  316. /**
  317. * dwc3_core_init - Low-level initialization of DWC3 Core
  318. * @dwc: Pointer to our controller context structure
  319. *
  320. * Returns 0 on success otherwise negative errno.
  321. */
  322. static int dwc3_core_init(struct dwc3 *dwc)
  323. {
  324. unsigned long timeout;
  325. u32 hwparams4 = dwc->hwparams.hwparams4;
  326. u32 reg;
  327. int ret;
  328. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  329. /* This should read as U3 followed by revision number */
  330. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  331. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  332. ret = -ENODEV;
  333. goto err0;
  334. }
  335. dwc->revision = reg;
  336. /* Handle USB2.0-only core configuration */
  337. if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  338. DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
  339. if (dwc->maximum_speed == USB_SPEED_SUPER)
  340. dwc->maximum_speed = USB_SPEED_HIGH;
  341. }
  342. /* issue device SoftReset too */
  343. timeout = 5000;
  344. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  345. while (timeout--) {
  346. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  347. if (!(reg & DWC3_DCTL_CSFTRST))
  348. break;
  349. };
  350. if (!timeout) {
  351. dev_err(dwc->dev, "Reset Timed Out\n");
  352. ret = -ETIMEDOUT;
  353. goto err0;
  354. }
  355. ret = dwc3_core_soft_reset(dwc);
  356. if (ret)
  357. goto err0;
  358. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  359. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  360. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  361. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  362. /**
  363. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  364. * issue which would cause xHCI compliance tests to fail.
  365. *
  366. * Because of that we cannot enable clock gating on such
  367. * configurations.
  368. *
  369. * Refers to:
  370. *
  371. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  372. * SOF/ITP Mode Used
  373. */
  374. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  375. dwc->dr_mode == USB_DR_MODE_OTG) &&
  376. (dwc->revision >= DWC3_REVISION_210A &&
  377. dwc->revision <= DWC3_REVISION_250A))
  378. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  379. else
  380. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  381. break;
  382. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  383. /* enable hibernation here */
  384. dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
  385. /*
  386. * REVISIT Enabling this bit so that host-mode hibernation
  387. * will work. Device-mode hibernation is not yet implemented.
  388. */
  389. reg |= DWC3_GCTL_GBLHIBERNATIONEN;
  390. break;
  391. default:
  392. dev_dbg(dwc->dev, "No power optimization available\n");
  393. }
  394. /* check if current dwc3 is on simulation board */
  395. if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
  396. dev_dbg(dwc->dev, "it is on FPGA board\n");
  397. dwc->is_fpga = true;
  398. }
  399. if(dwc->disable_scramble_quirk && !dwc->is_fpga)
  400. WARN(true,
  401. "disable_scramble cannot be used on non-FPGA builds\n");
  402. if (dwc->disable_scramble_quirk && dwc->is_fpga)
  403. reg |= DWC3_GCTL_DISSCRAMBLE;
  404. else
  405. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  406. if (dwc->u2exit_lfps_quirk)
  407. reg |= DWC3_GCTL_U2EXIT_LFPS;
  408. /*
  409. * WORKAROUND: DWC3 revisions <1.90a have a bug
  410. * where the device can fail to connect at SuperSpeed
  411. * and falls back to high-speed mode which causes
  412. * the device to enter a Connect/Disconnect loop
  413. */
  414. if (dwc->revision < DWC3_REVISION_190A)
  415. reg |= DWC3_GCTL_U2RSTECN;
  416. dwc3_core_num_eps(dwc);
  417. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  418. dwc3_phy_setup(dwc);
  419. ret = dwc3_alloc_scratch_buffers(dwc);
  420. if (ret)
  421. goto err0;
  422. ret = dwc3_setup_scratch_buffers(dwc);
  423. if (ret)
  424. goto err1;
  425. return 0;
  426. err1:
  427. dwc3_free_scratch_buffers(dwc);
  428. err0:
  429. return ret;
  430. }
  431. static void dwc3_core_exit(struct dwc3 *dwc)
  432. {
  433. dwc3_free_scratch_buffers(dwc);
  434. }
  435. static int dwc3_core_init_mode(struct dwc3 *dwc)
  436. {
  437. int ret;
  438. switch (dwc->dr_mode) {
  439. case USB_DR_MODE_PERIPHERAL:
  440. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  441. ret = dwc3_gadget_init(dwc);
  442. if (ret) {
  443. dev_err(dev, "failed to initialize gadget\n");
  444. return ret;
  445. }
  446. break;
  447. case USB_DR_MODE_HOST:
  448. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  449. ret = dwc3_host_init(dwc);
  450. if (ret) {
  451. dev_err(dev, "failed to initialize host\n");
  452. return ret;
  453. }
  454. break;
  455. case USB_DR_MODE_OTG:
  456. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  457. ret = dwc3_host_init(dwc);
  458. if (ret) {
  459. dev_err(dev, "failed to initialize host\n");
  460. return ret;
  461. }
  462. ret = dwc3_gadget_init(dwc);
  463. if (ret) {
  464. dev_err(dev, "failed to initialize gadget\n");
  465. return ret;
  466. }
  467. break;
  468. default:
  469. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  470. return -EINVAL;
  471. }
  472. return 0;
  473. }
  474. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  475. {
  476. switch (dwc->dr_mode) {
  477. case USB_DR_MODE_PERIPHERAL:
  478. dwc3_gadget_exit(dwc);
  479. break;
  480. case USB_DR_MODE_HOST:
  481. dwc3_host_exit(dwc);
  482. break;
  483. case USB_DR_MODE_OTG:
  484. dwc3_host_exit(dwc);
  485. dwc3_gadget_exit(dwc);
  486. break;
  487. default:
  488. /* do nothing */
  489. break;
  490. }
  491. }
  492. #define DWC3_ALIGN_MASK (16 - 1)
  493. /**
  494. * dwc3_uboot_init - dwc3 core uboot initialization code
  495. * @dwc3_dev: struct dwc3_device containing initialization data
  496. *
  497. * Entry point for dwc3 driver (equivalent to dwc3_probe in linux
  498. * kernel driver). Pointer to dwc3_device should be passed containing
  499. * base address and other initialization data. Returns '0' on success and
  500. * a negative value on failure.
  501. *
  502. * Generally called from board_usb_init() implemented in board file.
  503. */
  504. int dwc3_uboot_init(struct dwc3_device *dwc3_dev)
  505. {
  506. struct dwc3 *dwc;
  507. struct device *dev = NULL;
  508. u8 lpm_nyet_threshold;
  509. u8 tx_de_emphasis;
  510. u8 hird_threshold;
  511. int ret;
  512. void *mem;
  513. mem = devm_kzalloc((struct udevice *)dev,
  514. sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  515. if (!mem)
  516. return -ENOMEM;
  517. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  518. dwc->mem = mem;
  519. dwc->regs = (void *)(uintptr_t)(dwc3_dev->base +
  520. DWC3_GLOBALS_REGS_START);
  521. /* default to highest possible threshold */
  522. lpm_nyet_threshold = 0xff;
  523. /* default to -3.5dB de-emphasis */
  524. tx_de_emphasis = 1;
  525. /*
  526. * default to assert utmi_sleep_n and use maximum allowed HIRD
  527. * threshold value of 0b1100
  528. */
  529. hird_threshold = 12;
  530. dwc->maximum_speed = dwc3_dev->maximum_speed;
  531. dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum;
  532. if (dwc3_dev->lpm_nyet_threshold)
  533. lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold;
  534. dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend;
  535. if (dwc3_dev->hird_threshold)
  536. hird_threshold = dwc3_dev->hird_threshold;
  537. dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize;
  538. dwc->dr_mode = dwc3_dev->dr_mode;
  539. dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk;
  540. dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk;
  541. dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk;
  542. dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk;
  543. dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk;
  544. dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk;
  545. dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk;
  546. dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk;
  547. dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk;
  548. dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk;
  549. dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk;
  550. if (dwc3_dev->tx_de_emphasis)
  551. tx_de_emphasis = dwc3_dev->tx_de_emphasis;
  552. /* default to superspeed if no maximum_speed passed */
  553. if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
  554. dwc->maximum_speed = USB_SPEED_SUPER;
  555. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  556. dwc->tx_de_emphasis = tx_de_emphasis;
  557. dwc->hird_threshold = hird_threshold
  558. | (dwc->is_utmi_l1_suspend << 4);
  559. dwc->index = dwc3_dev->index;
  560. dwc3_cache_hwparams(dwc);
  561. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  562. if (ret) {
  563. dev_err(dwc->dev, "failed to allocate event buffers\n");
  564. return -ENOMEM;
  565. }
  566. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  567. dwc->dr_mode = USB_DR_MODE_HOST;
  568. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  569. dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
  570. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  571. dwc->dr_mode = USB_DR_MODE_OTG;
  572. ret = dwc3_core_init(dwc);
  573. if (ret) {
  574. dev_err(dev, "failed to initialize core\n");
  575. goto err0;
  576. }
  577. ret = dwc3_event_buffers_setup(dwc);
  578. if (ret) {
  579. dev_err(dwc->dev, "failed to setup event buffers\n");
  580. goto err1;
  581. }
  582. ret = dwc3_core_init_mode(dwc);
  583. if (ret)
  584. goto err2;
  585. list_add_tail(&dwc->list, &dwc3_list);
  586. return 0;
  587. err2:
  588. dwc3_event_buffers_cleanup(dwc);
  589. err1:
  590. dwc3_core_exit(dwc);
  591. err0:
  592. dwc3_free_event_buffers(dwc);
  593. return ret;
  594. }
  595. /**
  596. * dwc3_uboot_exit - dwc3 core uboot cleanup code
  597. * @index: index of this controller
  598. *
  599. * Performs cleanup of memory allocated in dwc3_uboot_init and other misc
  600. * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller
  601. * should be passed and should match with the index passed in
  602. * dwc3_device during init.
  603. *
  604. * Generally called from board file.
  605. */
  606. void dwc3_uboot_exit(int index)
  607. {
  608. struct dwc3 *dwc;
  609. list_for_each_entry(dwc, &dwc3_list, list) {
  610. if (dwc->index != index)
  611. continue;
  612. dwc3_core_exit_mode(dwc);
  613. dwc3_event_buffers_cleanup(dwc);
  614. dwc3_free_event_buffers(dwc);
  615. dwc3_core_exit(dwc);
  616. list_del(&dwc->list);
  617. kfree(dwc->mem);
  618. break;
  619. }
  620. }
  621. /**
  622. * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt
  623. * @index: index of this controller
  624. *
  625. * Invokes dwc3 gadget interrupts.
  626. *
  627. * Generally called from board file.
  628. */
  629. void dwc3_uboot_handle_interrupt(int index)
  630. {
  631. struct dwc3 *dwc = NULL;
  632. list_for_each_entry(dwc, &dwc3_list, list) {
  633. if (dwc->index != index)
  634. continue;
  635. dwc3_gadget_uboot_handle_interrupt(dwc);
  636. break;
  637. }
  638. }
  639. MODULE_ALIAS("platform:dwc3");
  640. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  641. MODULE_LICENSE("GPL v2");
  642. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
  643. #ifdef CONFIG_DM_USB
  644. int dwc3_init(struct dwc3 *dwc)
  645. {
  646. int ret;
  647. dwc3_cache_hwparams(dwc);
  648. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  649. if (ret) {
  650. dev_err(dwc->dev, "failed to allocate event buffers\n");
  651. return -ENOMEM;
  652. }
  653. ret = dwc3_core_init(dwc);
  654. if (ret) {
  655. dev_err(dev, "failed to initialize core\n");
  656. goto core_fail;
  657. }
  658. ret = dwc3_event_buffers_setup(dwc);
  659. if (ret) {
  660. dev_err(dwc->dev, "failed to setup event buffers\n");
  661. goto event_fail;
  662. }
  663. ret = dwc3_core_init_mode(dwc);
  664. if (ret)
  665. goto mode_fail;
  666. return 0;
  667. mode_fail:
  668. dwc3_event_buffers_cleanup(dwc);
  669. event_fail:
  670. dwc3_core_exit(dwc);
  671. core_fail:
  672. dwc3_free_event_buffers(dwc);
  673. return ret;
  674. }
  675. void dwc3_remove(struct dwc3 *dwc)
  676. {
  677. dwc3_core_exit_mode(dwc);
  678. dwc3_event_buffers_cleanup(dwc);
  679. dwc3_free_event_buffers(dwc);
  680. dwc3_core_exit(dwc);
  681. kfree(dwc->mem);
  682. }
  683. #endif