core.c 19 KB

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