core.c 19 KB

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