core.c 33 KB

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