core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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/pinctrl/consumer.h>
  37. #include <linux/usb/ch9.h>
  38. #include <linux/usb/gadget.h>
  39. #include <linux/usb/of.h>
  40. #include <linux/usb/otg.h>
  41. #include "core.h"
  42. #include "gadget.h"
  43. #include "io.h"
  44. #include "debug.h"
  45. #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
  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. u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
  55. {
  56. struct dwc3 *dwc = dep->dwc;
  57. u32 reg;
  58. dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
  59. DWC3_GDBGFIFOSPACE_NUM(dep->number) |
  60. DWC3_GDBGFIFOSPACE_TYPE(type));
  61. reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
  62. return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
  63. }
  64. /**
  65. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  66. * @dwc: pointer to our context structure
  67. */
  68. static int dwc3_core_soft_reset(struct dwc3 *dwc)
  69. {
  70. u32 reg;
  71. int retries = 1000;
  72. int ret;
  73. usb_phy_init(dwc->usb2_phy);
  74. usb_phy_init(dwc->usb3_phy);
  75. ret = phy_init(dwc->usb2_generic_phy);
  76. if (ret < 0)
  77. return ret;
  78. ret = phy_init(dwc->usb3_generic_phy);
  79. if (ret < 0) {
  80. phy_exit(dwc->usb2_generic_phy);
  81. return ret;
  82. }
  83. /*
  84. * We're resetting only the device side because, if we're in host mode,
  85. * XHCI driver will reset the host block. If dwc3 was configured for
  86. * host-only mode, then we can return early.
  87. */
  88. if (dwc->dr_mode == USB_DR_MODE_HOST)
  89. return 0;
  90. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  91. reg |= DWC3_DCTL_CSFTRST;
  92. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  93. do {
  94. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  95. if (!(reg & DWC3_DCTL_CSFTRST))
  96. return 0;
  97. udelay(1);
  98. } while (--retries);
  99. return -ETIMEDOUT;
  100. }
  101. /**
  102. * dwc3_soft_reset - Issue soft reset
  103. * @dwc: Pointer to our controller context structure
  104. */
  105. static int dwc3_soft_reset(struct dwc3 *dwc)
  106. {
  107. unsigned long timeout;
  108. u32 reg;
  109. timeout = jiffies + msecs_to_jiffies(500);
  110. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  111. do {
  112. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  113. if (!(reg & DWC3_DCTL_CSFTRST))
  114. break;
  115. if (time_after(jiffies, timeout)) {
  116. dev_err(dwc->dev, "Reset Timed Out\n");
  117. return -ETIMEDOUT;
  118. }
  119. cpu_relax();
  120. } while (true);
  121. return 0;
  122. }
  123. /*
  124. * dwc3_frame_length_adjustment - Adjusts frame length if required
  125. * @dwc3: Pointer to our controller context structure
  126. */
  127. static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
  128. {
  129. u32 reg;
  130. u32 dft;
  131. if (dwc->revision < DWC3_REVISION_250A)
  132. return;
  133. if (dwc->fladj == 0)
  134. return;
  135. reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
  136. dft = reg & DWC3_GFLADJ_30MHZ_MASK;
  137. if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
  138. "request value same as default, ignoring\n")) {
  139. reg &= ~DWC3_GFLADJ_30MHZ_MASK;
  140. reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
  141. dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
  142. }
  143. }
  144. /**
  145. * dwc3_free_one_event_buffer - Frees one event buffer
  146. * @dwc: Pointer to our controller context structure
  147. * @evt: Pointer to event buffer to be freed
  148. */
  149. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  150. struct dwc3_event_buffer *evt)
  151. {
  152. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  153. }
  154. /**
  155. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  156. * @dwc: Pointer to our controller context structure
  157. * @length: size of the event buffer
  158. *
  159. * Returns a pointer to the allocated event buffer structure on success
  160. * otherwise ERR_PTR(errno).
  161. */
  162. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  163. unsigned length)
  164. {
  165. struct dwc3_event_buffer *evt;
  166. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  167. if (!evt)
  168. return ERR_PTR(-ENOMEM);
  169. evt->dwc = dwc;
  170. evt->length = length;
  171. evt->buf = dma_alloc_coherent(dwc->dev, length,
  172. &evt->dma, GFP_KERNEL);
  173. if (!evt->buf)
  174. return ERR_PTR(-ENOMEM);
  175. return evt;
  176. }
  177. /**
  178. * dwc3_free_event_buffers - frees all allocated event buffers
  179. * @dwc: Pointer to our controller context structure
  180. */
  181. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  182. {
  183. struct dwc3_event_buffer *evt;
  184. evt = dwc->ev_buf;
  185. if (evt)
  186. dwc3_free_one_event_buffer(dwc, evt);
  187. }
  188. /**
  189. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  190. * @dwc: pointer to our controller context structure
  191. * @length: size of event buffer
  192. *
  193. * Returns 0 on success otherwise negative errno. In the error case, dwc
  194. * may contain some buffers allocated but not all which were requested.
  195. */
  196. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  197. {
  198. struct dwc3_event_buffer *evt;
  199. evt = dwc3_alloc_one_event_buffer(dwc, length);
  200. if (IS_ERR(evt)) {
  201. dev_err(dwc->dev, "can't allocate event buffer\n");
  202. return PTR_ERR(evt);
  203. }
  204. dwc->ev_buf = evt;
  205. return 0;
  206. }
  207. /**
  208. * dwc3_event_buffers_setup - setup our allocated event buffers
  209. * @dwc: pointer to our controller context structure
  210. *
  211. * Returns 0 on success otherwise negative errno.
  212. */
  213. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  214. {
  215. struct dwc3_event_buffer *evt;
  216. evt = dwc->ev_buf;
  217. dwc3_trace(trace_dwc3_core,
  218. "Event buf %p dma %08llx length %d\n",
  219. evt->buf, (unsigned long long) evt->dma,
  220. evt->length);
  221. evt->lpos = 0;
  222. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
  223. lower_32_bits(evt->dma));
  224. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
  225. upper_32_bits(evt->dma));
  226. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
  227. DWC3_GEVNTSIZ_SIZE(evt->length));
  228. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
  229. return 0;
  230. }
  231. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  232. {
  233. struct dwc3_event_buffer *evt;
  234. evt = dwc->ev_buf;
  235. evt->lpos = 0;
  236. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
  237. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
  238. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
  239. | DWC3_GEVNTSIZ_SIZE(0));
  240. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
  241. }
  242. static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
  243. {
  244. if (!dwc->has_hibernation)
  245. return 0;
  246. if (!dwc->nr_scratch)
  247. return 0;
  248. dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
  249. DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
  250. if (!dwc->scratchbuf)
  251. return -ENOMEM;
  252. return 0;
  253. }
  254. static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
  255. {
  256. dma_addr_t scratch_addr;
  257. u32 param;
  258. int ret;
  259. if (!dwc->has_hibernation)
  260. return 0;
  261. if (!dwc->nr_scratch)
  262. return 0;
  263. /* should never fall here */
  264. if (!WARN_ON(dwc->scratchbuf))
  265. return 0;
  266. scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
  267. dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  268. DMA_BIDIRECTIONAL);
  269. if (dma_mapping_error(dwc->dev, scratch_addr)) {
  270. dev_err(dwc->dev, "failed to map scratch buffer\n");
  271. ret = -EFAULT;
  272. goto err0;
  273. }
  274. dwc->scratch_addr = scratch_addr;
  275. param = lower_32_bits(scratch_addr);
  276. ret = dwc3_send_gadget_generic_command(dwc,
  277. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
  278. if (ret < 0)
  279. goto err1;
  280. param = upper_32_bits(scratch_addr);
  281. ret = dwc3_send_gadget_generic_command(dwc,
  282. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
  283. if (ret < 0)
  284. goto err1;
  285. return 0;
  286. err1:
  287. dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
  288. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  289. err0:
  290. return ret;
  291. }
  292. static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
  293. {
  294. if (!dwc->has_hibernation)
  295. return;
  296. if (!dwc->nr_scratch)
  297. return;
  298. /* should never fall here */
  299. if (!WARN_ON(dwc->scratchbuf))
  300. return;
  301. dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
  302. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  303. kfree(dwc->scratchbuf);
  304. }
  305. static void dwc3_core_num_eps(struct dwc3 *dwc)
  306. {
  307. struct dwc3_hwparams *parms = &dwc->hwparams;
  308. dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
  309. dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
  310. dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
  311. dwc->num_in_eps, dwc->num_out_eps);
  312. }
  313. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  314. {
  315. struct dwc3_hwparams *parms = &dwc->hwparams;
  316. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  317. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  318. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  319. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  320. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  321. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  322. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  323. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  324. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  325. }
  326. /**
  327. * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
  328. * @dwc: Pointer to our controller context structure
  329. *
  330. * Returns 0 on success. The USB PHY interfaces are configured but not
  331. * initialized. The PHY interfaces and the PHYs get initialized together with
  332. * the core in dwc3_core_init.
  333. */
  334. static int dwc3_phy_setup(struct dwc3 *dwc)
  335. {
  336. u32 reg;
  337. int ret;
  338. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  339. /*
  340. * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
  341. * to '0' during coreConsultant configuration. So default value
  342. * will be '0' when the core is reset. Application needs to set it
  343. * to '1' after the core initialization is completed.
  344. */
  345. if (dwc->revision > DWC3_REVISION_194A)
  346. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  347. if (dwc->u2ss_inp3_quirk)
  348. reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
  349. if (dwc->dis_rxdet_inp3_quirk)
  350. reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
  351. if (dwc->req_p1p2p3_quirk)
  352. reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
  353. if (dwc->del_p1p2p3_quirk)
  354. reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
  355. if (dwc->del_phy_power_chg_quirk)
  356. reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
  357. if (dwc->lfps_filter_quirk)
  358. reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
  359. if (dwc->rx_detect_poll_quirk)
  360. reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
  361. if (dwc->tx_de_emphasis_quirk)
  362. reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
  363. if (dwc->dis_u3_susphy_quirk)
  364. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  365. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  366. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  367. /* Select the HS PHY interface */
  368. switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
  369. case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
  370. if (dwc->hsphy_interface &&
  371. !strncmp(dwc->hsphy_interface, "utmi", 4)) {
  372. reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
  373. break;
  374. } else if (dwc->hsphy_interface &&
  375. !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
  376. reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
  377. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  378. } else {
  379. /* Relying on default value. */
  380. if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
  381. break;
  382. }
  383. /* FALLTHROUGH */
  384. case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
  385. /* Making sure the interface and PHY are operational */
  386. ret = dwc3_soft_reset(dwc);
  387. if (ret)
  388. return ret;
  389. udelay(1);
  390. ret = dwc3_ulpi_init(dwc);
  391. if (ret)
  392. return ret;
  393. /* FALLTHROUGH */
  394. default:
  395. break;
  396. }
  397. /*
  398. * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
  399. * '0' during coreConsultant configuration. So default value will
  400. * be '0' when the core is reset. Application needs to set it to
  401. * '1' after the core initialization is completed.
  402. */
  403. if (dwc->revision > DWC3_REVISION_194A)
  404. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  405. if (dwc->dis_u2_susphy_quirk)
  406. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  407. if (dwc->dis_enblslpm_quirk)
  408. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  409. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  410. return 0;
  411. }
  412. static void dwc3_core_exit(struct dwc3 *dwc)
  413. {
  414. dwc3_event_buffers_cleanup(dwc);
  415. usb_phy_shutdown(dwc->usb2_phy);
  416. usb_phy_shutdown(dwc->usb3_phy);
  417. phy_exit(dwc->usb2_generic_phy);
  418. phy_exit(dwc->usb3_generic_phy);
  419. usb_phy_set_suspend(dwc->usb2_phy, 1);
  420. usb_phy_set_suspend(dwc->usb3_phy, 1);
  421. phy_power_off(dwc->usb2_generic_phy);
  422. phy_power_off(dwc->usb3_generic_phy);
  423. }
  424. /**
  425. * dwc3_core_init - Low-level initialization of DWC3 Core
  426. * @dwc: Pointer to our controller context structure
  427. *
  428. * Returns 0 on success otherwise negative errno.
  429. */
  430. static int dwc3_core_init(struct dwc3 *dwc)
  431. {
  432. u32 hwparams4 = dwc->hwparams.hwparams4;
  433. u32 reg;
  434. int ret;
  435. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  436. /* This should read as U3 followed by revision number */
  437. if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
  438. /* Detected DWC_usb3 IP */
  439. dwc->revision = reg;
  440. } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
  441. /* Detected DWC_usb31 IP */
  442. dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
  443. dwc->revision |= DWC3_REVISION_IS_DWC31;
  444. } else {
  445. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  446. ret = -ENODEV;
  447. goto err0;
  448. }
  449. /*
  450. * Write Linux Version Code to our GUID register so it's easy to figure
  451. * out which kernel version a bug was found.
  452. */
  453. dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
  454. /* Handle USB2.0-only core configuration */
  455. if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  456. DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
  457. if (dwc->maximum_speed == USB_SPEED_SUPER)
  458. dwc->maximum_speed = USB_SPEED_HIGH;
  459. }
  460. /* issue device SoftReset too */
  461. ret = dwc3_soft_reset(dwc);
  462. if (ret)
  463. goto err0;
  464. ret = dwc3_core_soft_reset(dwc);
  465. if (ret)
  466. goto err0;
  467. ret = dwc3_phy_setup(dwc);
  468. if (ret)
  469. goto err0;
  470. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  471. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  472. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  473. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  474. /**
  475. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  476. * issue which would cause xHCI compliance tests to fail.
  477. *
  478. * Because of that we cannot enable clock gating on such
  479. * configurations.
  480. *
  481. * Refers to:
  482. *
  483. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  484. * SOF/ITP Mode Used
  485. */
  486. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  487. dwc->dr_mode == USB_DR_MODE_OTG) &&
  488. (dwc->revision >= DWC3_REVISION_210A &&
  489. dwc->revision <= DWC3_REVISION_250A))
  490. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  491. else
  492. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  493. break;
  494. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  495. /* enable hibernation here */
  496. dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
  497. /*
  498. * REVISIT Enabling this bit so that host-mode hibernation
  499. * will work. Device-mode hibernation is not yet implemented.
  500. */
  501. reg |= DWC3_GCTL_GBLHIBERNATIONEN;
  502. break;
  503. default:
  504. dwc3_trace(trace_dwc3_core, "No power optimization available\n");
  505. }
  506. /* check if current dwc3 is on simulation board */
  507. if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
  508. dwc3_trace(trace_dwc3_core,
  509. "running on FPGA platform\n");
  510. dwc->is_fpga = true;
  511. }
  512. WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
  513. "disable_scramble cannot be used on non-FPGA builds\n");
  514. if (dwc->disable_scramble_quirk && dwc->is_fpga)
  515. reg |= DWC3_GCTL_DISSCRAMBLE;
  516. else
  517. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  518. if (dwc->u2exit_lfps_quirk)
  519. reg |= DWC3_GCTL_U2EXIT_LFPS;
  520. /*
  521. * WORKAROUND: DWC3 revisions <1.90a have a bug
  522. * where the device can fail to connect at SuperSpeed
  523. * and falls back to high-speed mode which causes
  524. * the device to enter a Connect/Disconnect loop
  525. */
  526. if (dwc->revision < DWC3_REVISION_190A)
  527. reg |= DWC3_GCTL_U2RSTECN;
  528. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  529. dwc3_core_num_eps(dwc);
  530. ret = dwc3_setup_scratch_buffers(dwc);
  531. if (ret)
  532. goto err1;
  533. /* Adjust Frame Length */
  534. dwc3_frame_length_adjustment(dwc);
  535. usb_phy_set_suspend(dwc->usb2_phy, 0);
  536. usb_phy_set_suspend(dwc->usb3_phy, 0);
  537. ret = phy_power_on(dwc->usb2_generic_phy);
  538. if (ret < 0)
  539. goto err2;
  540. ret = phy_power_on(dwc->usb3_generic_phy);
  541. if (ret < 0)
  542. goto err3;
  543. ret = dwc3_event_buffers_setup(dwc);
  544. if (ret) {
  545. dev_err(dwc->dev, "failed to setup event buffers\n");
  546. goto err4;
  547. }
  548. return 0;
  549. err4:
  550. phy_power_off(dwc->usb2_generic_phy);
  551. err3:
  552. phy_power_off(dwc->usb3_generic_phy);
  553. err2:
  554. usb_phy_set_suspend(dwc->usb2_phy, 1);
  555. usb_phy_set_suspend(dwc->usb3_phy, 1);
  556. dwc3_core_exit(dwc);
  557. err1:
  558. usb_phy_shutdown(dwc->usb2_phy);
  559. usb_phy_shutdown(dwc->usb3_phy);
  560. phy_exit(dwc->usb2_generic_phy);
  561. phy_exit(dwc->usb3_generic_phy);
  562. err0:
  563. return ret;
  564. }
  565. static int dwc3_core_get_phy(struct dwc3 *dwc)
  566. {
  567. struct device *dev = dwc->dev;
  568. struct device_node *node = dev->of_node;
  569. int ret;
  570. if (node) {
  571. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  572. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  573. } else {
  574. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  575. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  576. }
  577. if (IS_ERR(dwc->usb2_phy)) {
  578. ret = PTR_ERR(dwc->usb2_phy);
  579. if (ret == -ENXIO || ret == -ENODEV) {
  580. dwc->usb2_phy = NULL;
  581. } else if (ret == -EPROBE_DEFER) {
  582. return ret;
  583. } else {
  584. dev_err(dev, "no usb2 phy configured\n");
  585. return ret;
  586. }
  587. }
  588. if (IS_ERR(dwc->usb3_phy)) {
  589. ret = PTR_ERR(dwc->usb3_phy);
  590. if (ret == -ENXIO || ret == -ENODEV) {
  591. dwc->usb3_phy = NULL;
  592. } else if (ret == -EPROBE_DEFER) {
  593. return ret;
  594. } else {
  595. dev_err(dev, "no usb3 phy configured\n");
  596. return ret;
  597. }
  598. }
  599. dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
  600. if (IS_ERR(dwc->usb2_generic_phy)) {
  601. ret = PTR_ERR(dwc->usb2_generic_phy);
  602. if (ret == -ENOSYS || ret == -ENODEV) {
  603. dwc->usb2_generic_phy = NULL;
  604. } else if (ret == -EPROBE_DEFER) {
  605. return ret;
  606. } else {
  607. dev_err(dev, "no usb2 phy configured\n");
  608. return ret;
  609. }
  610. }
  611. dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
  612. if (IS_ERR(dwc->usb3_generic_phy)) {
  613. ret = PTR_ERR(dwc->usb3_generic_phy);
  614. if (ret == -ENOSYS || ret == -ENODEV) {
  615. dwc->usb3_generic_phy = NULL;
  616. } else if (ret == -EPROBE_DEFER) {
  617. return ret;
  618. } else {
  619. dev_err(dev, "no usb3 phy configured\n");
  620. return ret;
  621. }
  622. }
  623. return 0;
  624. }
  625. static int dwc3_core_init_mode(struct dwc3 *dwc)
  626. {
  627. struct device *dev = dwc->dev;
  628. int ret;
  629. switch (dwc->dr_mode) {
  630. case USB_DR_MODE_PERIPHERAL:
  631. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  632. ret = dwc3_gadget_init(dwc);
  633. if (ret) {
  634. if (ret != -EPROBE_DEFER)
  635. dev_err(dev, "failed to initialize gadget\n");
  636. return ret;
  637. }
  638. break;
  639. case USB_DR_MODE_HOST:
  640. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  641. ret = dwc3_host_init(dwc);
  642. if (ret) {
  643. if (ret != -EPROBE_DEFER)
  644. dev_err(dev, "failed to initialize host\n");
  645. return ret;
  646. }
  647. break;
  648. case USB_DR_MODE_OTG:
  649. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  650. ret = dwc3_host_init(dwc);
  651. if (ret) {
  652. if (ret != -EPROBE_DEFER)
  653. dev_err(dev, "failed to initialize host\n");
  654. return ret;
  655. }
  656. ret = dwc3_gadget_init(dwc);
  657. if (ret) {
  658. if (ret != -EPROBE_DEFER)
  659. dev_err(dev, "failed to initialize gadget\n");
  660. return ret;
  661. }
  662. break;
  663. default:
  664. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  665. return -EINVAL;
  666. }
  667. return 0;
  668. }
  669. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  670. {
  671. switch (dwc->dr_mode) {
  672. case USB_DR_MODE_PERIPHERAL:
  673. dwc3_gadget_exit(dwc);
  674. break;
  675. case USB_DR_MODE_HOST:
  676. dwc3_host_exit(dwc);
  677. break;
  678. case USB_DR_MODE_OTG:
  679. dwc3_host_exit(dwc);
  680. dwc3_gadget_exit(dwc);
  681. break;
  682. default:
  683. /* do nothing */
  684. break;
  685. }
  686. }
  687. #define DWC3_ALIGN_MASK (16 - 1)
  688. static int dwc3_probe(struct platform_device *pdev)
  689. {
  690. struct device *dev = &pdev->dev;
  691. struct resource *res;
  692. struct dwc3 *dwc;
  693. u8 lpm_nyet_threshold;
  694. u8 tx_de_emphasis;
  695. u8 hird_threshold;
  696. int ret;
  697. void __iomem *regs;
  698. void *mem;
  699. mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  700. if (!mem)
  701. return -ENOMEM;
  702. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  703. dwc->mem = mem;
  704. dwc->dev = dev;
  705. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  706. if (!res) {
  707. dev_err(dev, "missing memory resource\n");
  708. return -ENODEV;
  709. }
  710. dwc->xhci_resources[0].start = res->start;
  711. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  712. DWC3_XHCI_REGS_END;
  713. dwc->xhci_resources[0].flags = res->flags;
  714. dwc->xhci_resources[0].name = res->name;
  715. res->start += DWC3_GLOBALS_REGS_START;
  716. /*
  717. * Request memory region but exclude xHCI regs,
  718. * since it will be requested by the xhci-plat driver.
  719. */
  720. regs = devm_ioremap_resource(dev, res);
  721. if (IS_ERR(regs)) {
  722. ret = PTR_ERR(regs);
  723. goto err0;
  724. }
  725. dwc->regs = regs;
  726. dwc->regs_size = resource_size(res);
  727. /* default to highest possible threshold */
  728. lpm_nyet_threshold = 0xff;
  729. /* default to -3.5dB de-emphasis */
  730. tx_de_emphasis = 1;
  731. /*
  732. * default to assert utmi_sleep_n and use maximum allowed HIRD
  733. * threshold value of 0b1100
  734. */
  735. hird_threshold = 12;
  736. dwc->maximum_speed = usb_get_maximum_speed(dev);
  737. dwc->dr_mode = usb_get_dr_mode(dev);
  738. dwc->has_lpm_erratum = device_property_read_bool(dev,
  739. "snps,has-lpm-erratum");
  740. device_property_read_u8(dev, "snps,lpm-nyet-threshold",
  741. &lpm_nyet_threshold);
  742. dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
  743. "snps,is-utmi-l1-suspend");
  744. device_property_read_u8(dev, "snps,hird-threshold",
  745. &hird_threshold);
  746. dwc->usb3_lpm_capable = device_property_read_bool(dev,
  747. "snps,usb3_lpm_capable");
  748. dwc->disable_scramble_quirk = device_property_read_bool(dev,
  749. "snps,disable_scramble_quirk");
  750. dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
  751. "snps,u2exit_lfps_quirk");
  752. dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
  753. "snps,u2ss_inp3_quirk");
  754. dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
  755. "snps,req_p1p2p3_quirk");
  756. dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
  757. "snps,del_p1p2p3_quirk");
  758. dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
  759. "snps,del_phy_power_chg_quirk");
  760. dwc->lfps_filter_quirk = device_property_read_bool(dev,
  761. "snps,lfps_filter_quirk");
  762. dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
  763. "snps,rx_detect_poll_quirk");
  764. dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
  765. "snps,dis_u3_susphy_quirk");
  766. dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
  767. "snps,dis_u2_susphy_quirk");
  768. dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
  769. "snps,dis_enblslpm_quirk");
  770. dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
  771. "snps,dis_rxdet_inp3_quirk");
  772. dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
  773. "snps,tx_de_emphasis_quirk");
  774. device_property_read_u8(dev, "snps,tx_de_emphasis",
  775. &tx_de_emphasis);
  776. device_property_read_string(dev, "snps,hsphy_interface",
  777. &dwc->hsphy_interface);
  778. device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
  779. &dwc->fladj);
  780. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  781. dwc->tx_de_emphasis = tx_de_emphasis;
  782. dwc->hird_threshold = hird_threshold
  783. | (dwc->is_utmi_l1_suspend << 4);
  784. platform_set_drvdata(pdev, dwc);
  785. dwc3_cache_hwparams(dwc);
  786. ret = dwc3_core_get_phy(dwc);
  787. if (ret)
  788. goto err0;
  789. spin_lock_init(&dwc->lock);
  790. if (!dev->dma_mask) {
  791. dev->dma_mask = dev->parent->dma_mask;
  792. dev->dma_parms = dev->parent->dma_parms;
  793. dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
  794. }
  795. pm_runtime_set_active(dev);
  796. pm_runtime_use_autosuspend(dev);
  797. pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
  798. pm_runtime_enable(dev);
  799. ret = pm_runtime_get_sync(dev);
  800. if (ret < 0)
  801. goto err1;
  802. pm_runtime_forbid(dev);
  803. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  804. if (ret) {
  805. dev_err(dwc->dev, "failed to allocate event buffers\n");
  806. ret = -ENOMEM;
  807. goto err2;
  808. }
  809. if (IS_ENABLED(CONFIG_USB_DWC3_HOST) &&
  810. (dwc->dr_mode == USB_DR_MODE_OTG ||
  811. dwc->dr_mode == USB_DR_MODE_UNKNOWN))
  812. dwc->dr_mode = USB_DR_MODE_HOST;
  813. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET) &&
  814. (dwc->dr_mode == USB_DR_MODE_OTG ||
  815. dwc->dr_mode == USB_DR_MODE_UNKNOWN))
  816. dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
  817. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  818. dwc->dr_mode = USB_DR_MODE_OTG;
  819. ret = dwc3_alloc_scratch_buffers(dwc);
  820. if (ret)
  821. goto err3;
  822. ret = dwc3_core_init(dwc);
  823. if (ret) {
  824. dev_err(dev, "failed to initialize core\n");
  825. goto err4;
  826. }
  827. /* Check the maximum_speed parameter */
  828. switch (dwc->maximum_speed) {
  829. case USB_SPEED_LOW:
  830. case USB_SPEED_FULL:
  831. case USB_SPEED_HIGH:
  832. case USB_SPEED_SUPER:
  833. case USB_SPEED_SUPER_PLUS:
  834. break;
  835. default:
  836. dev_err(dev, "invalid maximum_speed parameter %d\n",
  837. dwc->maximum_speed);
  838. /* fall through */
  839. case USB_SPEED_UNKNOWN:
  840. /* default to superspeed */
  841. dwc->maximum_speed = USB_SPEED_SUPER;
  842. /*
  843. * default to superspeed plus if we are capable.
  844. */
  845. if (dwc3_is_usb31(dwc) &&
  846. (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  847. DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
  848. dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
  849. break;
  850. }
  851. ret = dwc3_core_init_mode(dwc);
  852. if (ret)
  853. goto err5;
  854. dwc3_debugfs_init(dwc);
  855. pm_runtime_put(dev);
  856. return 0;
  857. err5:
  858. dwc3_event_buffers_cleanup(dwc);
  859. err4:
  860. dwc3_free_scratch_buffers(dwc);
  861. err3:
  862. dwc3_free_event_buffers(dwc);
  863. dwc3_ulpi_exit(dwc);
  864. err2:
  865. pm_runtime_allow(&pdev->dev);
  866. err1:
  867. pm_runtime_put_sync(&pdev->dev);
  868. pm_runtime_disable(&pdev->dev);
  869. err0:
  870. /*
  871. * restore res->start back to its original value so that, in case the
  872. * probe is deferred, we don't end up getting error in request the
  873. * memory region the next time probe is called.
  874. */
  875. res->start -= DWC3_GLOBALS_REGS_START;
  876. return ret;
  877. }
  878. static int dwc3_remove(struct platform_device *pdev)
  879. {
  880. struct dwc3 *dwc = platform_get_drvdata(pdev);
  881. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  882. pm_runtime_get_sync(&pdev->dev);
  883. /*
  884. * restore res->start back to its original value so that, in case the
  885. * probe is deferred, we don't end up getting error in request the
  886. * memory region the next time probe is called.
  887. */
  888. res->start -= DWC3_GLOBALS_REGS_START;
  889. dwc3_debugfs_exit(dwc);
  890. dwc3_core_exit_mode(dwc);
  891. dwc3_core_exit(dwc);
  892. dwc3_ulpi_exit(dwc);
  893. pm_runtime_put_sync(&pdev->dev);
  894. pm_runtime_allow(&pdev->dev);
  895. pm_runtime_disable(&pdev->dev);
  896. dwc3_free_event_buffers(dwc);
  897. dwc3_free_scratch_buffers(dwc);
  898. return 0;
  899. }
  900. #ifdef CONFIG_PM
  901. static int dwc3_suspend_common(struct dwc3 *dwc)
  902. {
  903. unsigned long flags;
  904. switch (dwc->dr_mode) {
  905. case USB_DR_MODE_PERIPHERAL:
  906. case USB_DR_MODE_OTG:
  907. spin_lock_irqsave(&dwc->lock, flags);
  908. dwc3_gadget_suspend(dwc);
  909. spin_unlock_irqrestore(&dwc->lock, flags);
  910. break;
  911. case USB_DR_MODE_HOST:
  912. default:
  913. /* do nothing */
  914. break;
  915. }
  916. dwc3_core_exit(dwc);
  917. return 0;
  918. }
  919. static int dwc3_resume_common(struct dwc3 *dwc)
  920. {
  921. unsigned long flags;
  922. int ret;
  923. ret = dwc3_core_init(dwc);
  924. if (ret)
  925. return ret;
  926. switch (dwc->dr_mode) {
  927. case USB_DR_MODE_PERIPHERAL:
  928. case USB_DR_MODE_OTG:
  929. spin_lock_irqsave(&dwc->lock, flags);
  930. dwc3_gadget_resume(dwc);
  931. spin_unlock_irqrestore(&dwc->lock, flags);
  932. /* FALLTHROUGH */
  933. case USB_DR_MODE_HOST:
  934. default:
  935. /* do nothing */
  936. break;
  937. }
  938. return 0;
  939. }
  940. static int dwc3_runtime_checks(struct dwc3 *dwc)
  941. {
  942. switch (dwc->dr_mode) {
  943. case USB_DR_MODE_PERIPHERAL:
  944. case USB_DR_MODE_OTG:
  945. if (dwc->connected)
  946. return -EBUSY;
  947. break;
  948. case USB_DR_MODE_HOST:
  949. default:
  950. /* do nothing */
  951. break;
  952. }
  953. return 0;
  954. }
  955. static int dwc3_runtime_suspend(struct device *dev)
  956. {
  957. struct dwc3 *dwc = dev_get_drvdata(dev);
  958. int ret;
  959. if (dwc3_runtime_checks(dwc))
  960. return -EBUSY;
  961. ret = dwc3_suspend_common(dwc);
  962. if (ret)
  963. return ret;
  964. device_init_wakeup(dev, true);
  965. return 0;
  966. }
  967. static int dwc3_runtime_resume(struct device *dev)
  968. {
  969. struct dwc3 *dwc = dev_get_drvdata(dev);
  970. int ret;
  971. device_init_wakeup(dev, false);
  972. ret = dwc3_resume_common(dwc);
  973. if (ret)
  974. return ret;
  975. switch (dwc->dr_mode) {
  976. case USB_DR_MODE_PERIPHERAL:
  977. case USB_DR_MODE_OTG:
  978. dwc3_gadget_process_pending_events(dwc);
  979. break;
  980. case USB_DR_MODE_HOST:
  981. default:
  982. /* do nothing */
  983. break;
  984. }
  985. pm_runtime_mark_last_busy(dev);
  986. return 0;
  987. }
  988. static int dwc3_runtime_idle(struct device *dev)
  989. {
  990. struct dwc3 *dwc = dev_get_drvdata(dev);
  991. switch (dwc->dr_mode) {
  992. case USB_DR_MODE_PERIPHERAL:
  993. case USB_DR_MODE_OTG:
  994. if (dwc3_runtime_checks(dwc))
  995. return -EBUSY;
  996. break;
  997. case USB_DR_MODE_HOST:
  998. default:
  999. /* do nothing */
  1000. break;
  1001. }
  1002. pm_runtime_mark_last_busy(dev);
  1003. pm_runtime_autosuspend(dev);
  1004. return 0;
  1005. }
  1006. #endif /* CONFIG_PM */
  1007. #ifdef CONFIG_PM_SLEEP
  1008. static int dwc3_suspend(struct device *dev)
  1009. {
  1010. struct dwc3 *dwc = dev_get_drvdata(dev);
  1011. int ret;
  1012. ret = dwc3_suspend_common(dwc);
  1013. if (ret)
  1014. return ret;
  1015. pinctrl_pm_select_sleep_state(dev);
  1016. return 0;
  1017. }
  1018. static int dwc3_resume(struct device *dev)
  1019. {
  1020. struct dwc3 *dwc = dev_get_drvdata(dev);
  1021. int ret;
  1022. pinctrl_pm_select_default_state(dev);
  1023. ret = dwc3_resume_common(dwc);
  1024. if (ret)
  1025. return ret;
  1026. pm_runtime_disable(dev);
  1027. pm_runtime_set_active(dev);
  1028. pm_runtime_enable(dev);
  1029. return 0;
  1030. }
  1031. #endif /* CONFIG_PM_SLEEP */
  1032. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  1033. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  1034. SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
  1035. dwc3_runtime_idle)
  1036. };
  1037. #ifdef CONFIG_OF
  1038. static const struct of_device_id of_dwc3_match[] = {
  1039. {
  1040. .compatible = "snps,dwc3"
  1041. },
  1042. {
  1043. .compatible = "synopsys,dwc3"
  1044. },
  1045. { },
  1046. };
  1047. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  1048. #endif
  1049. #ifdef CONFIG_ACPI
  1050. #define ACPI_ID_INTEL_BSW "808622B7"
  1051. static const struct acpi_device_id dwc3_acpi_match[] = {
  1052. { ACPI_ID_INTEL_BSW, 0 },
  1053. { },
  1054. };
  1055. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  1056. #endif
  1057. static struct platform_driver dwc3_driver = {
  1058. .probe = dwc3_probe,
  1059. .remove = dwc3_remove,
  1060. .driver = {
  1061. .name = "dwc3",
  1062. .of_match_table = of_match_ptr(of_dwc3_match),
  1063. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  1064. .pm = &dwc3_dev_pm_ops,
  1065. },
  1066. };
  1067. module_platform_driver(dwc3_driver);
  1068. MODULE_ALIAS("platform:dwc3");
  1069. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1070. MODULE_LICENSE("GPL v2");
  1071. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");