core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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 <clk.h>
  17. #include <cpu_func.h>
  18. #include <malloc.h>
  19. #include <dwc3-uboot.h>
  20. #include <dm/device_compat.h>
  21. #include <dm/devres.h>
  22. #include <linux/bug.h>
  23. #include <linux/delay.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/err.h>
  26. #include <linux/ioport.h>
  27. #include <dm.h>
  28. #include <generic-phy.h>
  29. #include <linux/usb/ch9.h>
  30. #include <linux/usb/gadget.h>
  31. #include <linux/bitfield.h>
  32. #include <linux/math64.h>
  33. #include "core.h"
  34. #include "gadget.h"
  35. #include "io.h"
  36. #include "linux-compat.h"
  37. #define NSEC_PER_SEC 1000000000L
  38. static LIST_HEAD(dwc3_list);
  39. /* -------------------------------------------------------------------------- */
  40. static 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. /* Before Resetting PHY, put Core in Reset */
  56. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  57. reg |= DWC3_GCTL_CORESOFTRESET;
  58. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  59. /* Assert USB3 PHY reset */
  60. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  61. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  62. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  63. /* Assert USB2 PHY reset */
  64. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  65. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  66. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  67. mdelay(100);
  68. /* Clear USB3 PHY reset */
  69. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  70. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  71. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  72. /* Clear USB2 PHY reset */
  73. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  74. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  75. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  76. mdelay(100);
  77. /* After PHYs are stable we can take Core out of reset state */
  78. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  79. reg &= ~DWC3_GCTL_CORESOFTRESET;
  80. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  81. return 0;
  82. }
  83. /*
  84. * dwc3_frame_length_adjustment - Adjusts frame length if required
  85. * @dwc3: Pointer to our controller context structure
  86. * @fladj: Value of GFLADJ_30MHZ to adjust frame length
  87. */
  88. static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj)
  89. {
  90. u32 reg;
  91. if (dwc->revision < DWC3_REVISION_250A)
  92. return;
  93. if (fladj == 0)
  94. return;
  95. reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
  96. reg &= ~DWC3_GFLADJ_30MHZ_MASK;
  97. reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | fladj;
  98. dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
  99. }
  100. /**
  101. * dwc3_ref_clk_period - Reference clock period configuration
  102. * Default reference clock period depends on hardware
  103. * configuration. For systems with reference clock that differs
  104. * from the default, this will set clock period in DWC3_GUCTL
  105. * register.
  106. * @dwc: Pointer to our controller context structure
  107. * @ref_clk_per: reference clock period in ns
  108. */
  109. static void dwc3_ref_clk_period(struct dwc3 *dwc)
  110. {
  111. unsigned long period;
  112. unsigned long fladj;
  113. unsigned long decr;
  114. unsigned long rate;
  115. u32 reg;
  116. if (dwc->ref_clk) {
  117. rate = clk_get_rate(dwc->ref_clk);
  118. if (!rate)
  119. return;
  120. period = NSEC_PER_SEC / rate;
  121. } else {
  122. return;
  123. }
  124. reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
  125. reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
  126. reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
  127. dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
  128. if (dwc->revision <= DWC3_REVISION_250A)
  129. return;
  130. /*
  131. * The calculation below is
  132. *
  133. * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
  134. *
  135. * but rearranged for fixed-point arithmetic. The division must be
  136. * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
  137. * neither does rate * period).
  138. *
  139. * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
  140. * nanoseconds of error caused by the truncation which happened during
  141. * the division when calculating rate or period (whichever one was
  142. * derived from the other). We first calculate the relative error, then
  143. * scale it to units of 8 ppm.
  144. */
  145. fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
  146. fladj -= 125000;
  147. /*
  148. * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
  149. */
  150. decr = 480000000 / rate;
  151. reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
  152. reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
  153. & ~DWC3_GFLADJ_240MHZDECR
  154. & ~DWC3_GFLADJ_240MHZDECR_PLS1;
  155. reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
  156. | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
  157. | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
  158. dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
  159. }
  160. /**
  161. * dwc3_free_one_event_buffer - Frees one event buffer
  162. * @dwc: Pointer to our controller context structure
  163. * @evt: Pointer to event buffer to be freed
  164. */
  165. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  166. struct dwc3_event_buffer *evt)
  167. {
  168. dma_free_coherent(evt->buf);
  169. }
  170. /**
  171. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  172. * @dwc: Pointer to our controller context structure
  173. * @length: size of the event buffer
  174. *
  175. * Returns a pointer to the allocated event buffer structure on success
  176. * otherwise ERR_PTR(errno).
  177. */
  178. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  179. unsigned length)
  180. {
  181. struct dwc3_event_buffer *evt;
  182. evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt),
  183. GFP_KERNEL);
  184. if (!evt)
  185. return ERR_PTR(-ENOMEM);
  186. evt->dwc = dwc;
  187. evt->length = length;
  188. evt->buf = dma_alloc_coherent(length,
  189. (unsigned long *)&evt->dma);
  190. if (!evt->buf)
  191. return ERR_PTR(-ENOMEM);
  192. dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
  193. return evt;
  194. }
  195. /**
  196. * dwc3_free_event_buffers - frees all allocated event buffers
  197. * @dwc: Pointer to our controller context structure
  198. */
  199. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  200. {
  201. struct dwc3_event_buffer *evt;
  202. int i;
  203. for (i = 0; i < dwc->num_event_buffers; i++) {
  204. evt = dwc->ev_buffs[i];
  205. if (evt)
  206. dwc3_free_one_event_buffer(dwc, evt);
  207. }
  208. }
  209. /**
  210. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  211. * @dwc: pointer to our controller context structure
  212. * @length: size of event buffer
  213. *
  214. * Returns 0 on success otherwise negative errno. In the error case, dwc
  215. * may contain some buffers allocated but not all which were requested.
  216. */
  217. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  218. {
  219. int num;
  220. int i;
  221. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  222. dwc->num_event_buffers = num;
  223. dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE,
  224. sizeof(*dwc->ev_buffs) * num);
  225. if (!dwc->ev_buffs)
  226. return -ENOMEM;
  227. for (i = 0; i < num; i++) {
  228. struct dwc3_event_buffer *evt;
  229. evt = dwc3_alloc_one_event_buffer(dwc, length);
  230. if (IS_ERR(evt)) {
  231. dev_err(dwc->dev, "can't allocate event buffer\n");
  232. return PTR_ERR(evt);
  233. }
  234. dwc->ev_buffs[i] = evt;
  235. }
  236. return 0;
  237. }
  238. /**
  239. * dwc3_event_buffers_setup - setup our allocated event buffers
  240. * @dwc: pointer to our controller context structure
  241. *
  242. * Returns 0 on success otherwise negative errno.
  243. */
  244. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  245. {
  246. struct dwc3_event_buffer *evt;
  247. int n;
  248. for (n = 0; n < dwc->num_event_buffers; n++) {
  249. evt = dwc->ev_buffs[n];
  250. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  251. evt->buf, (unsigned long long) evt->dma,
  252. evt->length);
  253. evt->lpos = 0;
  254. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  255. lower_32_bits(evt->dma));
  256. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  257. upper_32_bits(evt->dma));
  258. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  259. DWC3_GEVNTSIZ_SIZE(evt->length));
  260. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  261. }
  262. return 0;
  263. }
  264. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  265. {
  266. struct dwc3_event_buffer *evt;
  267. int n;
  268. for (n = 0; n < dwc->num_event_buffers; n++) {
  269. evt = dwc->ev_buffs[n];
  270. evt->lpos = 0;
  271. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  272. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  273. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
  274. | DWC3_GEVNTSIZ_SIZE(0));
  275. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  276. }
  277. }
  278. static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
  279. {
  280. if (!dwc->has_hibernation)
  281. return 0;
  282. if (!dwc->nr_scratch)
  283. return 0;
  284. dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
  285. DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
  286. if (!dwc->scratchbuf)
  287. return -ENOMEM;
  288. return 0;
  289. }
  290. static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
  291. {
  292. dma_addr_t scratch_addr;
  293. u32 param;
  294. int ret;
  295. if (!dwc->has_hibernation)
  296. return 0;
  297. if (!dwc->nr_scratch)
  298. return 0;
  299. scratch_addr = dma_map_single(dwc->scratchbuf,
  300. dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  301. DMA_BIDIRECTIONAL);
  302. if (dma_mapping_error(dwc->dev, scratch_addr)) {
  303. dev_err(dwc->dev, "failed to map scratch buffer\n");
  304. ret = -EFAULT;
  305. goto err0;
  306. }
  307. dwc->scratch_addr = scratch_addr;
  308. param = lower_32_bits(scratch_addr);
  309. ret = dwc3_send_gadget_generic_command(dwc,
  310. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
  311. if (ret < 0)
  312. goto err1;
  313. param = upper_32_bits(scratch_addr);
  314. ret = dwc3_send_gadget_generic_command(dwc,
  315. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
  316. if (ret < 0)
  317. goto err1;
  318. return 0;
  319. err1:
  320. dma_unmap_single(scratch_addr, dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  321. DMA_BIDIRECTIONAL);
  322. err0:
  323. return ret;
  324. }
  325. static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
  326. {
  327. if (!dwc->has_hibernation)
  328. return;
  329. if (!dwc->nr_scratch)
  330. return;
  331. dma_unmap_single(dwc->scratch_addr, dwc->nr_scratch *
  332. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  333. kfree(dwc->scratchbuf);
  334. }
  335. static void dwc3_core_num_eps(struct dwc3 *dwc)
  336. {
  337. struct dwc3_hwparams *parms = &dwc->hwparams;
  338. dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
  339. dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
  340. dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
  341. dwc->num_in_eps, dwc->num_out_eps);
  342. }
  343. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  344. {
  345. struct dwc3_hwparams *parms = &dwc->hwparams;
  346. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  347. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  348. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  349. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  350. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  351. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  352. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  353. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  354. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  355. }
  356. static void dwc3_hsphy_mode_setup(struct dwc3 *dwc)
  357. {
  358. enum usb_phy_interface hsphy_mode = dwc->hsphy_mode;
  359. u32 reg;
  360. /* Set dwc3 usb2 phy config */
  361. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  362. switch (hsphy_mode) {
  363. case USBPHY_INTERFACE_MODE_UTMI:
  364. reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
  365. DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
  366. reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
  367. DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
  368. break;
  369. case USBPHY_INTERFACE_MODE_UTMIW:
  370. reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
  371. DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
  372. reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
  373. DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
  374. break;
  375. default:
  376. break;
  377. }
  378. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  379. }
  380. /**
  381. * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
  382. * @dwc: Pointer to our controller context structure
  383. */
  384. static void dwc3_phy_setup(struct dwc3 *dwc)
  385. {
  386. u32 reg;
  387. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  388. /*
  389. * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
  390. * to '0' during coreConsultant configuration. So default value
  391. * will be '0' when the core is reset. Application needs to set it
  392. * to '1' after the core initialization is completed.
  393. */
  394. if (dwc->revision > DWC3_REVISION_194A)
  395. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  396. if (dwc->u2ss_inp3_quirk)
  397. reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
  398. if (dwc->req_p1p2p3_quirk)
  399. reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
  400. if (dwc->del_p1p2p3_quirk)
  401. reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
  402. if (dwc->del_phy_power_chg_quirk)
  403. reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
  404. if (dwc->lfps_filter_quirk)
  405. reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
  406. if (dwc->rx_detect_poll_quirk)
  407. reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
  408. if (dwc->tx_de_emphasis_quirk)
  409. reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
  410. if (dwc->dis_u3_susphy_quirk)
  411. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  412. if (dwc->dis_del_phy_power_chg_quirk)
  413. reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
  414. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  415. dwc3_hsphy_mode_setup(dwc);
  416. mdelay(100);
  417. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  418. /*
  419. * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
  420. * '0' during coreConsultant configuration. So default value will
  421. * be '0' when the core is reset. Application needs to set it to
  422. * '1' after the core initialization is completed.
  423. */
  424. if (dwc->revision > DWC3_REVISION_194A)
  425. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  426. if (dwc->dis_u2_susphy_quirk)
  427. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  428. if (dwc->dis_enblslpm_quirk)
  429. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  430. if (dwc->dis_u2_freeclk_exists_quirk)
  431. reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
  432. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  433. mdelay(100);
  434. }
  435. /* set global incr burst type configuration registers */
  436. static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
  437. {
  438. struct udevice *dev = dwc->dev;
  439. u32 cfg;
  440. if (!dwc->incrx_size)
  441. return;
  442. cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
  443. /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
  444. cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
  445. if (dwc->incrx_mode)
  446. cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
  447. switch (dwc->incrx_size) {
  448. case 256:
  449. cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
  450. break;
  451. case 128:
  452. cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
  453. break;
  454. case 64:
  455. cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
  456. break;
  457. case 32:
  458. cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
  459. break;
  460. case 16:
  461. cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
  462. break;
  463. case 8:
  464. cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
  465. break;
  466. case 4:
  467. cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
  468. break;
  469. case 1:
  470. break;
  471. default:
  472. dev_err(dev, "Invalid property\n");
  473. break;
  474. }
  475. dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
  476. }
  477. /**
  478. * dwc3_core_init - Low-level initialization of DWC3 Core
  479. * @dwc: Pointer to our controller context structure
  480. *
  481. * Returns 0 on success otherwise negative errno.
  482. */
  483. static int dwc3_core_init(struct dwc3 *dwc)
  484. {
  485. unsigned long timeout;
  486. u32 hwparams4 = dwc->hwparams.hwparams4;
  487. u32 reg;
  488. int ret;
  489. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  490. /* This should read as U3 followed by revision number */
  491. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  492. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  493. ret = -ENODEV;
  494. goto err0;
  495. }
  496. dwc->revision = reg;
  497. /* Handle USB2.0-only core configuration */
  498. if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  499. DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
  500. if (dwc->maximum_speed == USB_SPEED_SUPER)
  501. dwc->maximum_speed = USB_SPEED_HIGH;
  502. }
  503. /* issue device SoftReset too */
  504. timeout = 5000;
  505. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  506. while (timeout--) {
  507. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  508. if (!(reg & DWC3_DCTL_CSFTRST))
  509. break;
  510. };
  511. if (!timeout) {
  512. dev_err(dwc->dev, "Reset Timed Out\n");
  513. ret = -ETIMEDOUT;
  514. goto err0;
  515. }
  516. dwc3_phy_setup(dwc);
  517. ret = dwc3_core_soft_reset(dwc);
  518. if (ret)
  519. goto err0;
  520. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  521. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  522. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  523. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  524. /**
  525. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  526. * issue which would cause xHCI compliance tests to fail.
  527. *
  528. * Because of that we cannot enable clock gating on such
  529. * configurations.
  530. *
  531. * Refers to:
  532. *
  533. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  534. * SOF/ITP Mode Used
  535. */
  536. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  537. dwc->dr_mode == USB_DR_MODE_OTG) &&
  538. (dwc->revision >= DWC3_REVISION_210A &&
  539. dwc->revision <= DWC3_REVISION_250A))
  540. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  541. else
  542. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  543. break;
  544. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  545. /* enable hibernation here */
  546. dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
  547. /*
  548. * REVISIT Enabling this bit so that host-mode hibernation
  549. * will work. Device-mode hibernation is not yet implemented.
  550. */
  551. reg |= DWC3_GCTL_GBLHIBERNATIONEN;
  552. break;
  553. default:
  554. dev_dbg(dwc->dev, "No power optimization available\n");
  555. }
  556. /* check if current dwc3 is on simulation board */
  557. if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
  558. dev_dbg(dwc->dev, "it is on FPGA board\n");
  559. dwc->is_fpga = true;
  560. }
  561. if(dwc->disable_scramble_quirk && !dwc->is_fpga)
  562. WARN(true,
  563. "disable_scramble cannot be used on non-FPGA builds\n");
  564. if (dwc->disable_scramble_quirk && dwc->is_fpga)
  565. reg |= DWC3_GCTL_DISSCRAMBLE;
  566. else
  567. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  568. if (dwc->u2exit_lfps_quirk)
  569. reg |= DWC3_GCTL_U2EXIT_LFPS;
  570. /*
  571. * WORKAROUND: DWC3 revisions <1.90a have a bug
  572. * where the device can fail to connect at SuperSpeed
  573. * and falls back to high-speed mode which causes
  574. * the device to enter a Connect/Disconnect loop
  575. */
  576. if (dwc->revision < DWC3_REVISION_190A)
  577. reg |= DWC3_GCTL_U2RSTECN;
  578. dwc3_core_num_eps(dwc);
  579. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  580. ret = dwc3_alloc_scratch_buffers(dwc);
  581. if (ret)
  582. goto err0;
  583. ret = dwc3_setup_scratch_buffers(dwc);
  584. if (ret)
  585. goto err1;
  586. /* Adjust Frame Length */
  587. dwc3_frame_length_adjustment(dwc, dwc->fladj);
  588. /* Adjust Reference Clock Period */
  589. dwc3_ref_clk_period(dwc);
  590. dwc3_set_incr_burst_type(dwc);
  591. return 0;
  592. err1:
  593. dwc3_free_scratch_buffers(dwc);
  594. err0:
  595. return ret;
  596. }
  597. static void dwc3_core_exit(struct dwc3 *dwc)
  598. {
  599. dwc3_free_scratch_buffers(dwc);
  600. }
  601. static int dwc3_core_init_mode(struct dwc3 *dwc)
  602. {
  603. int ret;
  604. switch (dwc->dr_mode) {
  605. case USB_DR_MODE_PERIPHERAL:
  606. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  607. ret = dwc3_gadget_init(dwc);
  608. if (ret) {
  609. dev_err(dwc->dev, "failed to initialize gadget\n");
  610. return ret;
  611. }
  612. break;
  613. case USB_DR_MODE_HOST:
  614. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  615. ret = dwc3_host_init(dwc);
  616. if (ret) {
  617. dev_err(dwc->dev, "failed to initialize host\n");
  618. return ret;
  619. }
  620. break;
  621. case USB_DR_MODE_OTG:
  622. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  623. ret = dwc3_host_init(dwc);
  624. if (ret) {
  625. dev_err(dwc->dev, "failed to initialize host\n");
  626. return ret;
  627. }
  628. ret = dwc3_gadget_init(dwc);
  629. if (ret) {
  630. dev_err(dwc->dev, "failed to initialize gadget\n");
  631. return ret;
  632. }
  633. break;
  634. default:
  635. dev_err(dwc->dev,
  636. "Unsupported mode of operation %d\n", dwc->dr_mode);
  637. return -EINVAL;
  638. }
  639. return 0;
  640. }
  641. static void dwc3_gadget_run(struct dwc3 *dwc)
  642. {
  643. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_RUN_STOP);
  644. mdelay(100);
  645. }
  646. static void dwc3_core_stop(struct dwc3 *dwc)
  647. {
  648. u32 reg;
  649. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  650. dwc3_writel(dwc->regs, DWC3_DCTL, reg & ~(DWC3_DCTL_RUN_STOP));
  651. }
  652. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  653. {
  654. switch (dwc->dr_mode) {
  655. case USB_DR_MODE_PERIPHERAL:
  656. dwc3_gadget_exit(dwc);
  657. break;
  658. case USB_DR_MODE_HOST:
  659. dwc3_host_exit(dwc);
  660. break;
  661. case USB_DR_MODE_OTG:
  662. dwc3_host_exit(dwc);
  663. dwc3_gadget_exit(dwc);
  664. break;
  665. default:
  666. /* do nothing */
  667. break;
  668. }
  669. /*
  670. * switch back to peripheral mode
  671. * This enables the phy to enter idle and then, if enabled, suspend.
  672. */
  673. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  674. dwc3_gadget_run(dwc);
  675. }
  676. #define DWC3_ALIGN_MASK (16 - 1)
  677. /**
  678. * dwc3_uboot_init - dwc3 core uboot initialization code
  679. * @dwc3_dev: struct dwc3_device containing initialization data
  680. *
  681. * Entry point for dwc3 driver (equivalent to dwc3_probe in linux
  682. * kernel driver). Pointer to dwc3_device should be passed containing
  683. * base address and other initialization data. Returns '0' on success and
  684. * a negative value on failure.
  685. *
  686. * Generally called from board_usb_init() implemented in board file.
  687. */
  688. int dwc3_uboot_init(struct dwc3_device *dwc3_dev)
  689. {
  690. struct dwc3 *dwc;
  691. struct device *dev = NULL;
  692. u8 lpm_nyet_threshold;
  693. u8 tx_de_emphasis;
  694. u8 hird_threshold;
  695. int ret;
  696. void *mem;
  697. mem = devm_kzalloc((struct udevice *)dev,
  698. sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  699. if (!mem)
  700. return -ENOMEM;
  701. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  702. dwc->mem = mem;
  703. dwc->regs = (void *)(uintptr_t)(dwc3_dev->base +
  704. DWC3_GLOBALS_REGS_START);
  705. /* default to highest possible threshold */
  706. lpm_nyet_threshold = 0xff;
  707. /* default to -3.5dB de-emphasis */
  708. tx_de_emphasis = 1;
  709. /*
  710. * default to assert utmi_sleep_n and use maximum allowed HIRD
  711. * threshold value of 0b1100
  712. */
  713. hird_threshold = 12;
  714. dwc->maximum_speed = dwc3_dev->maximum_speed;
  715. dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum;
  716. if (dwc3_dev->lpm_nyet_threshold)
  717. lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold;
  718. dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend;
  719. if (dwc3_dev->hird_threshold)
  720. hird_threshold = dwc3_dev->hird_threshold;
  721. dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize;
  722. dwc->dr_mode = dwc3_dev->dr_mode;
  723. dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk;
  724. dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk;
  725. dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk;
  726. dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk;
  727. dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk;
  728. dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk;
  729. dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk;
  730. dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk;
  731. dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk;
  732. dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk;
  733. dwc->dis_del_phy_power_chg_quirk = dwc3_dev->dis_del_phy_power_chg_quirk;
  734. dwc->dis_tx_ipgap_linecheck_quirk = dwc3_dev->dis_tx_ipgap_linecheck_quirk;
  735. dwc->dis_enblslpm_quirk = dwc3_dev->dis_enblslpm_quirk;
  736. dwc->dis_u2_freeclk_exists_quirk = dwc3_dev->dis_u2_freeclk_exists_quirk;
  737. dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk;
  738. if (dwc3_dev->tx_de_emphasis)
  739. tx_de_emphasis = dwc3_dev->tx_de_emphasis;
  740. /* default to superspeed if no maximum_speed passed */
  741. if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
  742. dwc->maximum_speed = USB_SPEED_SUPER;
  743. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  744. dwc->tx_de_emphasis = tx_de_emphasis;
  745. dwc->hird_threshold = hird_threshold
  746. | (dwc->is_utmi_l1_suspend << 4);
  747. dwc->hsphy_mode = dwc3_dev->hsphy_mode;
  748. dwc->index = dwc3_dev->index;
  749. dwc3_cache_hwparams(dwc);
  750. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  751. if (ret) {
  752. dev_err(dwc->dev, "failed to allocate event buffers\n");
  753. return -ENOMEM;
  754. }
  755. if (!IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  756. dwc->dr_mode = USB_DR_MODE_HOST;
  757. else if (!IS_ENABLED(CONFIG_USB_HOST))
  758. dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
  759. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  760. dwc->dr_mode = USB_DR_MODE_OTG;
  761. ret = dwc3_core_init(dwc);
  762. if (ret) {
  763. dev_err(dwc->dev, "failed to initialize core\n");
  764. goto err0;
  765. }
  766. ret = dwc3_event_buffers_setup(dwc);
  767. if (ret) {
  768. dev_err(dwc->dev, "failed to setup event buffers\n");
  769. goto err1;
  770. }
  771. ret = dwc3_core_init_mode(dwc);
  772. if (ret)
  773. goto err2;
  774. list_add_tail(&dwc->list, &dwc3_list);
  775. return 0;
  776. err2:
  777. dwc3_event_buffers_cleanup(dwc);
  778. err1:
  779. dwc3_core_exit(dwc);
  780. err0:
  781. dwc3_free_event_buffers(dwc);
  782. return ret;
  783. }
  784. /**
  785. * dwc3_uboot_exit - dwc3 core uboot cleanup code
  786. * @index: index of this controller
  787. *
  788. * Performs cleanup of memory allocated in dwc3_uboot_init and other misc
  789. * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller
  790. * should be passed and should match with the index passed in
  791. * dwc3_device during init.
  792. *
  793. * Generally called from board file.
  794. */
  795. void dwc3_uboot_exit(int index)
  796. {
  797. struct dwc3 *dwc;
  798. list_for_each_entry(dwc, &dwc3_list, list) {
  799. if (dwc->index != index)
  800. continue;
  801. dwc3_core_exit_mode(dwc);
  802. dwc3_event_buffers_cleanup(dwc);
  803. dwc3_free_event_buffers(dwc);
  804. dwc3_core_exit(dwc);
  805. list_del(&dwc->list);
  806. kfree(dwc->mem);
  807. break;
  808. }
  809. }
  810. /**
  811. * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt
  812. * @index: index of this controller
  813. *
  814. * Invokes dwc3 gadget interrupts.
  815. *
  816. * Generally called from board file.
  817. */
  818. void dwc3_uboot_handle_interrupt(int index)
  819. {
  820. struct dwc3 *dwc = NULL;
  821. list_for_each_entry(dwc, &dwc3_list, list) {
  822. if (dwc->index != index)
  823. continue;
  824. dwc3_gadget_uboot_handle_interrupt(dwc);
  825. break;
  826. }
  827. }
  828. MODULE_ALIAS("platform:dwc3");
  829. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  830. MODULE_LICENSE("GPL v2");
  831. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
  832. #if CONFIG_IS_ENABLED(PHY) && CONFIG_IS_ENABLED(DM_USB)
  833. int dwc3_setup_phy(struct udevice *dev, struct phy_bulk *phys)
  834. {
  835. int ret;
  836. ret = generic_phy_get_bulk(dev, phys);
  837. if (ret)
  838. return ret;
  839. ret = generic_phy_init_bulk(phys);
  840. if (ret)
  841. return ret;
  842. ret = generic_phy_power_on_bulk(phys);
  843. if (ret)
  844. generic_phy_exit_bulk(phys);
  845. return ret;
  846. }
  847. int dwc3_shutdown_phy(struct udevice *dev, struct phy_bulk *phys)
  848. {
  849. int ret;
  850. ret = generic_phy_power_off_bulk(phys);
  851. ret |= generic_phy_exit_bulk(phys);
  852. return ret;
  853. }
  854. #endif
  855. #if CONFIG_IS_ENABLED(DM_USB)
  856. void dwc3_of_parse(struct dwc3 *dwc)
  857. {
  858. const u8 *tmp;
  859. struct udevice *dev = dwc->dev;
  860. u8 lpm_nyet_threshold;
  861. u8 tx_de_emphasis;
  862. u8 hird_threshold;
  863. u32 val;
  864. int i;
  865. /* default to highest possible threshold */
  866. lpm_nyet_threshold = 0xff;
  867. /* default to -3.5dB de-emphasis */
  868. tx_de_emphasis = 1;
  869. /*
  870. * default to assert utmi_sleep_n and use maximum allowed HIRD
  871. * threshold value of 0b1100
  872. */
  873. hird_threshold = 12;
  874. dwc->hsphy_mode = usb_get_phy_mode(dev_ofnode(dev));
  875. dwc->has_lpm_erratum = dev_read_bool(dev,
  876. "snps,has-lpm-erratum");
  877. tmp = dev_read_u8_array_ptr(dev, "snps,lpm-nyet-threshold", 1);
  878. if (tmp)
  879. lpm_nyet_threshold = *tmp;
  880. dwc->is_utmi_l1_suspend = dev_read_bool(dev,
  881. "snps,is-utmi-l1-suspend");
  882. tmp = dev_read_u8_array_ptr(dev, "snps,hird-threshold", 1);
  883. if (tmp)
  884. hird_threshold = *tmp;
  885. dwc->disable_scramble_quirk = dev_read_bool(dev,
  886. "snps,disable_scramble_quirk");
  887. dwc->u2exit_lfps_quirk = dev_read_bool(dev,
  888. "snps,u2exit_lfps_quirk");
  889. dwc->u2ss_inp3_quirk = dev_read_bool(dev,
  890. "snps,u2ss_inp3_quirk");
  891. dwc->req_p1p2p3_quirk = dev_read_bool(dev,
  892. "snps,req_p1p2p3_quirk");
  893. dwc->del_p1p2p3_quirk = dev_read_bool(dev,
  894. "snps,del_p1p2p3_quirk");
  895. dwc->del_phy_power_chg_quirk = dev_read_bool(dev,
  896. "snps,del_phy_power_chg_quirk");
  897. dwc->lfps_filter_quirk = dev_read_bool(dev,
  898. "snps,lfps_filter_quirk");
  899. dwc->rx_detect_poll_quirk = dev_read_bool(dev,
  900. "snps,rx_detect_poll_quirk");
  901. dwc->dis_u3_susphy_quirk = dev_read_bool(dev,
  902. "snps,dis_u3_susphy_quirk");
  903. dwc->dis_u2_susphy_quirk = dev_read_bool(dev,
  904. "snps,dis_u2_susphy_quirk");
  905. dwc->dis_del_phy_power_chg_quirk = dev_read_bool(dev,
  906. "snps,dis-del-phy-power-chg-quirk");
  907. dwc->dis_tx_ipgap_linecheck_quirk = dev_read_bool(dev,
  908. "snps,dis-tx-ipgap-linecheck-quirk");
  909. dwc->dis_enblslpm_quirk = dev_read_bool(dev,
  910. "snps,dis_enblslpm_quirk");
  911. dwc->dis_u2_freeclk_exists_quirk = dev_read_bool(dev,
  912. "snps,dis-u2-freeclk-exists-quirk");
  913. dwc->tx_de_emphasis_quirk = dev_read_bool(dev,
  914. "snps,tx_de_emphasis_quirk");
  915. tmp = dev_read_u8_array_ptr(dev, "snps,tx_de_emphasis", 1);
  916. if (tmp)
  917. tx_de_emphasis = *tmp;
  918. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  919. dwc->tx_de_emphasis = tx_de_emphasis;
  920. dwc->hird_threshold = hird_threshold
  921. | (dwc->is_utmi_l1_suspend << 4);
  922. dev_read_u32(dev, "snps,quirk-frame-length-adjustment", &dwc->fladj);
  923. /*
  924. * Handle property "snps,incr-burst-type-adjustment".
  925. * Get the number of value from this property:
  926. * result <= 0, means this property is not supported.
  927. * result = 1, means INCRx burst mode supported.
  928. * result > 1, means undefined length burst mode supported.
  929. */
  930. dwc->incrx_mode = INCRX_BURST_MODE;
  931. dwc->incrx_size = 0;
  932. for (i = 0; i < 8; i++) {
  933. if (dev_read_u32_index(dev, "snps,incr-burst-type-adjustment",
  934. i, &val))
  935. break;
  936. dwc->incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
  937. dwc->incrx_size = max(dwc->incrx_size, val);
  938. }
  939. }
  940. int dwc3_init(struct dwc3 *dwc)
  941. {
  942. int ret;
  943. u32 reg;
  944. dwc3_cache_hwparams(dwc);
  945. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  946. if (ret) {
  947. dev_err(dwc->dev, "failed to allocate event buffers\n");
  948. return -ENOMEM;
  949. }
  950. ret = dwc3_core_init(dwc);
  951. if (ret) {
  952. dev_err(dwc->dev, "failed to initialize core\n");
  953. goto core_fail;
  954. }
  955. ret = dwc3_event_buffers_setup(dwc);
  956. if (ret) {
  957. dev_err(dwc->dev, "failed to setup event buffers\n");
  958. goto event_fail;
  959. }
  960. if (dwc->revision >= DWC3_REVISION_250A) {
  961. reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
  962. /*
  963. * Enable hardware control of sending remote wakeup
  964. * in HS when the device is in the L1 state.
  965. */
  966. if (dwc->revision >= DWC3_REVISION_290A)
  967. reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
  968. if (dwc->dis_tx_ipgap_linecheck_quirk)
  969. reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
  970. dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
  971. }
  972. if (dwc->dr_mode == USB_DR_MODE_HOST ||
  973. dwc->dr_mode == USB_DR_MODE_OTG) {
  974. reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
  975. reg |= DWC3_GUCTL_HSTINAUTORETRY;
  976. dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
  977. }
  978. ret = dwc3_core_init_mode(dwc);
  979. if (ret)
  980. goto mode_fail;
  981. return 0;
  982. mode_fail:
  983. dwc3_event_buffers_cleanup(dwc);
  984. event_fail:
  985. dwc3_core_exit(dwc);
  986. core_fail:
  987. dwc3_free_event_buffers(dwc);
  988. return ret;
  989. }
  990. void dwc3_remove(struct dwc3 *dwc)
  991. {
  992. dwc3_core_exit_mode(dwc);
  993. dwc3_event_buffers_cleanup(dwc);
  994. dwc3_free_event_buffers(dwc);
  995. dwc3_core_stop(dwc);
  996. dwc3_core_exit(dwc);
  997. kfree(dwc->mem);
  998. }
  999. #endif