core.c 27 KB

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