core.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * core.c - DesignWare USB3 DRD Controller Core file
  4. *
  5. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/version.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/ioport.h>
  20. #include <linux/io.h>
  21. #include <linux/list.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/of.h>
  25. #include <linux/acpi.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/reset.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include <linux/usb/of.h>
  31. #include <linux/usb/otg.h>
  32. #include "core.h"
  33. #include "gadget.h"
  34. #include "io.h"
  35. #include "debug.h"
  36. #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
  37. /**
  38. * dwc3_get_dr_mode - Validates and sets dr_mode
  39. * @dwc: pointer to our context structure
  40. */
  41. static int dwc3_get_dr_mode(struct dwc3 *dwc)
  42. {
  43. enum usb_dr_mode mode;
  44. struct device *dev = dwc->dev;
  45. unsigned int hw_mode;
  46. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  47. dwc->dr_mode = USB_DR_MODE_OTG;
  48. mode = dwc->dr_mode;
  49. hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
  50. switch (hw_mode) {
  51. case DWC3_GHWPARAMS0_MODE_GADGET:
  52. if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
  53. dev_err(dev,
  54. "Controller does not support host mode.\n");
  55. return -EINVAL;
  56. }
  57. mode = USB_DR_MODE_PERIPHERAL;
  58. break;
  59. case DWC3_GHWPARAMS0_MODE_HOST:
  60. if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
  61. dev_err(dev,
  62. "Controller does not support device mode.\n");
  63. return -EINVAL;
  64. }
  65. mode = USB_DR_MODE_HOST;
  66. break;
  67. default:
  68. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  69. mode = USB_DR_MODE_HOST;
  70. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  71. mode = USB_DR_MODE_PERIPHERAL;
  72. /*
  73. * dwc_usb31 does not support OTG mode. If the controller
  74. * supports DRD but the dr_mode is not specified or set to OTG,
  75. * then set the mode to peripheral.
  76. */
  77. if (mode == USB_DR_MODE_OTG && dwc3_is_usb31(dwc))
  78. mode = USB_DR_MODE_PERIPHERAL;
  79. }
  80. if (mode != dwc->dr_mode) {
  81. dev_warn(dev,
  82. "Configuration mismatch. dr_mode forced to %s\n",
  83. mode == USB_DR_MODE_HOST ? "host" : "gadget");
  84. dwc->dr_mode = mode;
  85. }
  86. return 0;
  87. }
  88. void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
  89. {
  90. u32 reg;
  91. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  92. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  93. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  94. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  95. dwc->current_dr_role = mode;
  96. }
  97. static void __dwc3_set_mode(struct work_struct *work)
  98. {
  99. struct dwc3 *dwc = work_to_dwc(work);
  100. unsigned long flags;
  101. int ret;
  102. if (dwc->dr_mode != USB_DR_MODE_OTG)
  103. return;
  104. if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
  105. dwc3_otg_update(dwc, 0);
  106. if (!dwc->desired_dr_role)
  107. return;
  108. if (dwc->desired_dr_role == dwc->current_dr_role)
  109. return;
  110. if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
  111. return;
  112. switch (dwc->current_dr_role) {
  113. case DWC3_GCTL_PRTCAP_HOST:
  114. dwc3_host_exit(dwc);
  115. break;
  116. case DWC3_GCTL_PRTCAP_DEVICE:
  117. dwc3_gadget_exit(dwc);
  118. dwc3_event_buffers_cleanup(dwc);
  119. break;
  120. case DWC3_GCTL_PRTCAP_OTG:
  121. dwc3_otg_exit(dwc);
  122. spin_lock_irqsave(&dwc->lock, flags);
  123. dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
  124. spin_unlock_irqrestore(&dwc->lock, flags);
  125. dwc3_otg_update(dwc, 1);
  126. break;
  127. default:
  128. break;
  129. }
  130. spin_lock_irqsave(&dwc->lock, flags);
  131. dwc3_set_prtcap(dwc, dwc->desired_dr_role);
  132. spin_unlock_irqrestore(&dwc->lock, flags);
  133. switch (dwc->desired_dr_role) {
  134. case DWC3_GCTL_PRTCAP_HOST:
  135. ret = dwc3_host_init(dwc);
  136. if (ret) {
  137. dev_err(dwc->dev, "failed to initialize host\n");
  138. } else {
  139. if (dwc->usb2_phy)
  140. otg_set_vbus(dwc->usb2_phy->otg, true);
  141. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
  142. phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
  143. phy_calibrate(dwc->usb2_generic_phy);
  144. }
  145. break;
  146. case DWC3_GCTL_PRTCAP_DEVICE:
  147. dwc3_event_buffers_setup(dwc);
  148. if (dwc->usb2_phy)
  149. otg_set_vbus(dwc->usb2_phy->otg, false);
  150. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
  151. phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
  152. ret = dwc3_gadget_init(dwc);
  153. if (ret)
  154. dev_err(dwc->dev, "failed to initialize peripheral\n");
  155. break;
  156. case DWC3_GCTL_PRTCAP_OTG:
  157. dwc3_otg_init(dwc);
  158. dwc3_otg_update(dwc, 0);
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  165. {
  166. unsigned long flags;
  167. spin_lock_irqsave(&dwc->lock, flags);
  168. dwc->desired_dr_role = mode;
  169. spin_unlock_irqrestore(&dwc->lock, flags);
  170. queue_work(system_freezable_wq, &dwc->drd_work);
  171. }
  172. u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
  173. {
  174. struct dwc3 *dwc = dep->dwc;
  175. u32 reg;
  176. dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
  177. DWC3_GDBGFIFOSPACE_NUM(dep->number) |
  178. DWC3_GDBGFIFOSPACE_TYPE(type));
  179. reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
  180. return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
  181. }
  182. /**
  183. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  184. * @dwc: pointer to our context structure
  185. */
  186. static int dwc3_core_soft_reset(struct dwc3 *dwc)
  187. {
  188. u32 reg;
  189. int retries = 1000;
  190. int ret;
  191. usb_phy_init(dwc->usb2_phy);
  192. usb_phy_init(dwc->usb3_phy);
  193. ret = phy_init(dwc->usb2_generic_phy);
  194. if (ret < 0)
  195. return ret;
  196. ret = phy_init(dwc->usb3_generic_phy);
  197. if (ret < 0) {
  198. phy_exit(dwc->usb2_generic_phy);
  199. return ret;
  200. }
  201. /*
  202. * We're resetting only the device side because, if we're in host mode,
  203. * XHCI driver will reset the host block. If dwc3 was configured for
  204. * host-only mode, then we can return early.
  205. */
  206. if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
  207. return 0;
  208. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  209. reg |= DWC3_DCTL_CSFTRST;
  210. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  211. do {
  212. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  213. if (!(reg & DWC3_DCTL_CSFTRST))
  214. goto done;
  215. udelay(1);
  216. } while (--retries);
  217. phy_exit(dwc->usb3_generic_phy);
  218. phy_exit(dwc->usb2_generic_phy);
  219. return -ETIMEDOUT;
  220. done:
  221. /*
  222. * For DWC_usb31 controller, once DWC3_DCTL_CSFTRST bit is cleared,
  223. * we must wait at least 50ms before accessing the PHY domain
  224. * (synchronization delay). DWC_usb31 programming guide section 1.3.2.
  225. */
  226. if (dwc3_is_usb31(dwc))
  227. msleep(50);
  228. return 0;
  229. }
  230. static const struct clk_bulk_data dwc3_core_clks[] = {
  231. { .id = "ref" },
  232. { .id = "bus_early" },
  233. { .id = "suspend" },
  234. };
  235. /*
  236. * dwc3_frame_length_adjustment - Adjusts frame length if required
  237. * @dwc3: Pointer to our controller context structure
  238. */
  239. static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
  240. {
  241. u32 reg;
  242. u32 dft;
  243. if (dwc->revision < DWC3_REVISION_250A)
  244. return;
  245. if (dwc->fladj == 0)
  246. return;
  247. reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
  248. dft = reg & DWC3_GFLADJ_30MHZ_MASK;
  249. if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
  250. "request value same as default, ignoring\n")) {
  251. reg &= ~DWC3_GFLADJ_30MHZ_MASK;
  252. reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
  253. dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
  254. }
  255. }
  256. /**
  257. * dwc3_free_one_event_buffer - Frees one event buffer
  258. * @dwc: Pointer to our controller context structure
  259. * @evt: Pointer to event buffer to be freed
  260. */
  261. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  262. struct dwc3_event_buffer *evt)
  263. {
  264. dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
  265. }
  266. /**
  267. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  268. * @dwc: Pointer to our controller context structure
  269. * @length: size of the event buffer
  270. *
  271. * Returns a pointer to the allocated event buffer structure on success
  272. * otherwise ERR_PTR(errno).
  273. */
  274. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  275. unsigned length)
  276. {
  277. struct dwc3_event_buffer *evt;
  278. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  279. if (!evt)
  280. return ERR_PTR(-ENOMEM);
  281. evt->dwc = dwc;
  282. evt->length = length;
  283. evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
  284. if (!evt->cache)
  285. return ERR_PTR(-ENOMEM);
  286. evt->buf = dma_alloc_coherent(dwc->sysdev, length,
  287. &evt->dma, GFP_KERNEL);
  288. if (!evt->buf)
  289. return ERR_PTR(-ENOMEM);
  290. return evt;
  291. }
  292. /**
  293. * dwc3_free_event_buffers - frees all allocated event buffers
  294. * @dwc: Pointer to our controller context structure
  295. */
  296. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  297. {
  298. struct dwc3_event_buffer *evt;
  299. evt = dwc->ev_buf;
  300. if (evt)
  301. dwc3_free_one_event_buffer(dwc, evt);
  302. }
  303. /**
  304. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  305. * @dwc: pointer to our controller context structure
  306. * @length: size of event buffer
  307. *
  308. * Returns 0 on success otherwise negative errno. In the error case, dwc
  309. * may contain some buffers allocated but not all which were requested.
  310. */
  311. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  312. {
  313. struct dwc3_event_buffer *evt;
  314. evt = dwc3_alloc_one_event_buffer(dwc, length);
  315. if (IS_ERR(evt)) {
  316. dev_err(dwc->dev, "can't allocate event buffer\n");
  317. return PTR_ERR(evt);
  318. }
  319. dwc->ev_buf = evt;
  320. return 0;
  321. }
  322. /**
  323. * dwc3_event_buffers_setup - setup our allocated event buffers
  324. * @dwc: pointer to our controller context structure
  325. *
  326. * Returns 0 on success otherwise negative errno.
  327. */
  328. int dwc3_event_buffers_setup(struct dwc3 *dwc)
  329. {
  330. struct dwc3_event_buffer *evt;
  331. evt = dwc->ev_buf;
  332. evt->lpos = 0;
  333. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
  334. lower_32_bits(evt->dma));
  335. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
  336. upper_32_bits(evt->dma));
  337. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
  338. DWC3_GEVNTSIZ_SIZE(evt->length));
  339. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
  340. return 0;
  341. }
  342. void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  343. {
  344. struct dwc3_event_buffer *evt;
  345. evt = dwc->ev_buf;
  346. evt->lpos = 0;
  347. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
  348. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
  349. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
  350. | DWC3_GEVNTSIZ_SIZE(0));
  351. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
  352. }
  353. static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
  354. {
  355. if (!dwc->has_hibernation)
  356. return 0;
  357. if (!dwc->nr_scratch)
  358. return 0;
  359. dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
  360. DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
  361. if (!dwc->scratchbuf)
  362. return -ENOMEM;
  363. return 0;
  364. }
  365. static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
  366. {
  367. dma_addr_t scratch_addr;
  368. u32 param;
  369. int ret;
  370. if (!dwc->has_hibernation)
  371. return 0;
  372. if (!dwc->nr_scratch)
  373. return 0;
  374. /* should never fall here */
  375. if (!WARN_ON(dwc->scratchbuf))
  376. return 0;
  377. scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
  378. dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
  379. DMA_BIDIRECTIONAL);
  380. if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
  381. dev_err(dwc->sysdev, "failed to map scratch buffer\n");
  382. ret = -EFAULT;
  383. goto err0;
  384. }
  385. dwc->scratch_addr = scratch_addr;
  386. param = lower_32_bits(scratch_addr);
  387. ret = dwc3_send_gadget_generic_command(dwc,
  388. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
  389. if (ret < 0)
  390. goto err1;
  391. param = upper_32_bits(scratch_addr);
  392. ret = dwc3_send_gadget_generic_command(dwc,
  393. DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
  394. if (ret < 0)
  395. goto err1;
  396. return 0;
  397. err1:
  398. dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
  399. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  400. err0:
  401. return ret;
  402. }
  403. static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
  404. {
  405. if (!dwc->has_hibernation)
  406. return;
  407. if (!dwc->nr_scratch)
  408. return;
  409. /* should never fall here */
  410. if (!WARN_ON(dwc->scratchbuf))
  411. return;
  412. dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
  413. DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
  414. kfree(dwc->scratchbuf);
  415. }
  416. static void dwc3_core_num_eps(struct dwc3 *dwc)
  417. {
  418. struct dwc3_hwparams *parms = &dwc->hwparams;
  419. dwc->num_eps = DWC3_NUM_EPS(parms);
  420. }
  421. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  422. {
  423. struct dwc3_hwparams *parms = &dwc->hwparams;
  424. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  425. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  426. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  427. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  428. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  429. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  430. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  431. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  432. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  433. }
  434. static int dwc3_core_ulpi_init(struct dwc3 *dwc)
  435. {
  436. int intf;
  437. int ret = 0;
  438. intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
  439. if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
  440. (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
  441. dwc->hsphy_interface &&
  442. !strncmp(dwc->hsphy_interface, "ulpi", 4)))
  443. ret = dwc3_ulpi_init(dwc);
  444. return ret;
  445. }
  446. /**
  447. * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
  448. * @dwc: Pointer to our controller context structure
  449. *
  450. * Returns 0 on success. The USB PHY interfaces are configured but not
  451. * initialized. The PHY interfaces and the PHYs get initialized together with
  452. * the core in dwc3_core_init.
  453. */
  454. static int dwc3_phy_setup(struct dwc3 *dwc)
  455. {
  456. u32 reg;
  457. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  458. /*
  459. * Make sure UX_EXIT_PX is cleared as that causes issues with some
  460. * PHYs. Also, this bit is not supposed to be used in normal operation.
  461. */
  462. reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
  463. /*
  464. * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
  465. * to '0' during coreConsultant configuration. So default value
  466. * will be '0' when the core is reset. Application needs to set it
  467. * to '1' after the core initialization is completed.
  468. */
  469. if (dwc->revision > DWC3_REVISION_194A)
  470. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  471. if (dwc->u2ss_inp3_quirk)
  472. reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
  473. if (dwc->dis_rxdet_inp3_quirk)
  474. reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
  475. if (dwc->req_p1p2p3_quirk)
  476. reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
  477. if (dwc->del_p1p2p3_quirk)
  478. reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
  479. if (dwc->del_phy_power_chg_quirk)
  480. reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
  481. if (dwc->lfps_filter_quirk)
  482. reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
  483. if (dwc->rx_detect_poll_quirk)
  484. reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
  485. if (dwc->tx_de_emphasis_quirk)
  486. reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
  487. if (dwc->dis_u3_susphy_quirk)
  488. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  489. if (dwc->dis_del_phy_power_chg_quirk)
  490. reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
  491. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  492. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  493. /* Select the HS PHY interface */
  494. switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
  495. case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
  496. if (dwc->hsphy_interface &&
  497. !strncmp(dwc->hsphy_interface, "utmi", 4)) {
  498. reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
  499. break;
  500. } else if (dwc->hsphy_interface &&
  501. !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
  502. reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
  503. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  504. } else {
  505. /* Relying on default value. */
  506. if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
  507. break;
  508. }
  509. /* FALLTHROUGH */
  510. case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
  511. /* FALLTHROUGH */
  512. default:
  513. break;
  514. }
  515. switch (dwc->hsphy_mode) {
  516. case USBPHY_INTERFACE_MODE_UTMI:
  517. reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
  518. DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
  519. reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
  520. DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
  521. break;
  522. case USBPHY_INTERFACE_MODE_UTMIW:
  523. reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
  524. DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
  525. reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
  526. DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
  527. break;
  528. default:
  529. break;
  530. }
  531. /*
  532. * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
  533. * '0' during coreConsultant configuration. So default value will
  534. * be '0' when the core is reset. Application needs to set it to
  535. * '1' after the core initialization is completed.
  536. */
  537. if (dwc->revision > DWC3_REVISION_194A)
  538. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  539. if (dwc->dis_u2_susphy_quirk)
  540. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  541. if (dwc->dis_enblslpm_quirk)
  542. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  543. if (dwc->dis_u2_freeclk_exists_quirk)
  544. reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
  545. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  546. return 0;
  547. }
  548. static void dwc3_core_exit(struct dwc3 *dwc)
  549. {
  550. dwc3_event_buffers_cleanup(dwc);
  551. usb_phy_shutdown(dwc->usb2_phy);
  552. usb_phy_shutdown(dwc->usb3_phy);
  553. phy_exit(dwc->usb2_generic_phy);
  554. phy_exit(dwc->usb3_generic_phy);
  555. usb_phy_set_suspend(dwc->usb2_phy, 1);
  556. usb_phy_set_suspend(dwc->usb3_phy, 1);
  557. phy_power_off(dwc->usb2_generic_phy);
  558. phy_power_off(dwc->usb3_generic_phy);
  559. clk_bulk_disable(dwc->num_clks, dwc->clks);
  560. clk_bulk_unprepare(dwc->num_clks, dwc->clks);
  561. reset_control_assert(dwc->reset);
  562. }
  563. static bool dwc3_core_is_valid(struct dwc3 *dwc)
  564. {
  565. u32 reg;
  566. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  567. /* This should read as U3 followed by revision number */
  568. if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
  569. /* Detected DWC_usb3 IP */
  570. dwc->revision = reg;
  571. } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
  572. /* Detected DWC_usb31 IP */
  573. dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
  574. dwc->revision |= DWC3_REVISION_IS_DWC31;
  575. } else {
  576. return false;
  577. }
  578. return true;
  579. }
  580. static void dwc3_core_setup_global_control(struct dwc3 *dwc)
  581. {
  582. u32 hwparams4 = dwc->hwparams.hwparams4;
  583. u32 reg;
  584. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  585. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  586. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  587. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  588. /**
  589. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  590. * issue which would cause xHCI compliance tests to fail.
  591. *
  592. * Because of that we cannot enable clock gating on such
  593. * configurations.
  594. *
  595. * Refers to:
  596. *
  597. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  598. * SOF/ITP Mode Used
  599. */
  600. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  601. dwc->dr_mode == USB_DR_MODE_OTG) &&
  602. (dwc->revision >= DWC3_REVISION_210A &&
  603. dwc->revision <= DWC3_REVISION_250A))
  604. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  605. else
  606. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  607. break;
  608. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  609. /* enable hibernation here */
  610. dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
  611. /*
  612. * REVISIT Enabling this bit so that host-mode hibernation
  613. * will work. Device-mode hibernation is not yet implemented.
  614. */
  615. reg |= DWC3_GCTL_GBLHIBERNATIONEN;
  616. break;
  617. default:
  618. /* nothing */
  619. break;
  620. }
  621. /* check if current dwc3 is on simulation board */
  622. if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
  623. dev_info(dwc->dev, "Running with FPGA optimizations\n");
  624. dwc->is_fpga = true;
  625. }
  626. WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
  627. "disable_scramble cannot be used on non-FPGA builds\n");
  628. if (dwc->disable_scramble_quirk && dwc->is_fpga)
  629. reg |= DWC3_GCTL_DISSCRAMBLE;
  630. else
  631. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  632. if (dwc->u2exit_lfps_quirk)
  633. reg |= DWC3_GCTL_U2EXIT_LFPS;
  634. /*
  635. * WORKAROUND: DWC3 revisions <1.90a have a bug
  636. * where the device can fail to connect at SuperSpeed
  637. * and falls back to high-speed mode which causes
  638. * the device to enter a Connect/Disconnect loop
  639. */
  640. if (dwc->revision < DWC3_REVISION_190A)
  641. reg |= DWC3_GCTL_U2RSTECN;
  642. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  643. }
  644. static int dwc3_core_get_phy(struct dwc3 *dwc);
  645. static int dwc3_core_ulpi_init(struct dwc3 *dwc);
  646. /* set global incr burst type configuration registers */
  647. static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
  648. {
  649. struct device *dev = dwc->dev;
  650. /* incrx_mode : for INCR burst type. */
  651. bool incrx_mode;
  652. /* incrx_size : for size of INCRX burst. */
  653. u32 incrx_size;
  654. u32 *vals;
  655. u32 cfg;
  656. int ntype;
  657. int ret;
  658. int i;
  659. cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
  660. /*
  661. * Handle property "snps,incr-burst-type-adjustment".
  662. * Get the number of value from this property:
  663. * result <= 0, means this property is not supported.
  664. * result = 1, means INCRx burst mode supported.
  665. * result > 1, means undefined length burst mode supported.
  666. */
  667. ntype = device_property_read_u32_array(dev,
  668. "snps,incr-burst-type-adjustment", NULL, 0);
  669. if (ntype <= 0)
  670. return;
  671. vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
  672. if (!vals) {
  673. dev_err(dev, "Error to get memory\n");
  674. return;
  675. }
  676. /* Get INCR burst type, and parse it */
  677. ret = device_property_read_u32_array(dev,
  678. "snps,incr-burst-type-adjustment", vals, ntype);
  679. if (ret) {
  680. dev_err(dev, "Error to get property\n");
  681. return;
  682. }
  683. incrx_size = *vals;
  684. if (ntype > 1) {
  685. /* INCRX (undefined length) burst mode */
  686. incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
  687. for (i = 1; i < ntype; i++) {
  688. if (vals[i] > incrx_size)
  689. incrx_size = vals[i];
  690. }
  691. } else {
  692. /* INCRX burst mode */
  693. incrx_mode = INCRX_BURST_MODE;
  694. }
  695. /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
  696. cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
  697. if (incrx_mode)
  698. cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
  699. switch (incrx_size) {
  700. case 256:
  701. cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
  702. break;
  703. case 128:
  704. cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
  705. break;
  706. case 64:
  707. cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
  708. break;
  709. case 32:
  710. cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
  711. break;
  712. case 16:
  713. cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
  714. break;
  715. case 8:
  716. cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
  717. break;
  718. case 4:
  719. cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
  720. break;
  721. case 1:
  722. break;
  723. default:
  724. dev_err(dev, "Invalid property\n");
  725. break;
  726. }
  727. dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
  728. }
  729. /**
  730. * dwc3_core_init - Low-level initialization of DWC3 Core
  731. * @dwc: Pointer to our controller context structure
  732. *
  733. * Returns 0 on success otherwise negative errno.
  734. */
  735. static int dwc3_core_init(struct dwc3 *dwc)
  736. {
  737. u32 reg;
  738. int ret;
  739. if (!dwc3_core_is_valid(dwc)) {
  740. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  741. ret = -ENODEV;
  742. goto err0;
  743. }
  744. /*
  745. * Write Linux Version Code to our GUID register so it's easy to figure
  746. * out which kernel version a bug was found.
  747. */
  748. dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
  749. /* Handle USB2.0-only core configuration */
  750. if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  751. DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
  752. if (dwc->maximum_speed == USB_SPEED_SUPER)
  753. dwc->maximum_speed = USB_SPEED_HIGH;
  754. }
  755. ret = dwc3_phy_setup(dwc);
  756. if (ret)
  757. goto err0;
  758. if (!dwc->ulpi_ready) {
  759. ret = dwc3_core_ulpi_init(dwc);
  760. if (ret)
  761. goto err0;
  762. dwc->ulpi_ready = true;
  763. }
  764. if (!dwc->phys_ready) {
  765. ret = dwc3_core_get_phy(dwc);
  766. if (ret)
  767. goto err0a;
  768. dwc->phys_ready = true;
  769. }
  770. ret = dwc3_core_soft_reset(dwc);
  771. if (ret)
  772. goto err0a;
  773. dwc3_core_setup_global_control(dwc);
  774. dwc3_core_num_eps(dwc);
  775. ret = dwc3_setup_scratch_buffers(dwc);
  776. if (ret)
  777. goto err1;
  778. /* Adjust Frame Length */
  779. dwc3_frame_length_adjustment(dwc);
  780. dwc3_set_incr_burst_type(dwc);
  781. usb_phy_set_suspend(dwc->usb2_phy, 0);
  782. usb_phy_set_suspend(dwc->usb3_phy, 0);
  783. ret = phy_power_on(dwc->usb2_generic_phy);
  784. if (ret < 0)
  785. goto err2;
  786. ret = phy_power_on(dwc->usb3_generic_phy);
  787. if (ret < 0)
  788. goto err3;
  789. ret = dwc3_event_buffers_setup(dwc);
  790. if (ret) {
  791. dev_err(dwc->dev, "failed to setup event buffers\n");
  792. goto err4;
  793. }
  794. /*
  795. * ENDXFER polling is available on version 3.10a and later of
  796. * the DWC_usb3 controller. It is NOT available in the
  797. * DWC_usb31 controller.
  798. */
  799. if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
  800. reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
  801. reg |= DWC3_GUCTL2_RST_ACTBITLATER;
  802. dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
  803. }
  804. if (dwc->revision >= DWC3_REVISION_250A) {
  805. reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
  806. /*
  807. * Enable hardware control of sending remote wakeup
  808. * in HS when the device is in the L1 state.
  809. */
  810. if (dwc->revision >= DWC3_REVISION_290A)
  811. reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
  812. if (dwc->dis_tx_ipgap_linecheck_quirk)
  813. reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
  814. dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
  815. }
  816. if (dwc->dr_mode == USB_DR_MODE_HOST ||
  817. dwc->dr_mode == USB_DR_MODE_OTG) {
  818. reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
  819. /*
  820. * Enable Auto retry Feature to make the controller operating in
  821. * Host mode on seeing transaction errors(CRC errors or internal
  822. * overrun scenerios) on IN transfers to reply to the device
  823. * with a non-terminating retry ACK (i.e, an ACK transcation
  824. * packet with Retry=1 & Nump != 0)
  825. */
  826. reg |= DWC3_GUCTL_HSTINAUTORETRY;
  827. dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
  828. }
  829. /*
  830. * Must config both number of packets and max burst settings to enable
  831. * RX and/or TX threshold.
  832. */
  833. if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
  834. u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
  835. u8 rx_maxburst = dwc->rx_max_burst_prd;
  836. u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
  837. u8 tx_maxburst = dwc->tx_max_burst_prd;
  838. if (rx_thr_num && rx_maxburst) {
  839. reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
  840. reg |= DWC31_RXTHRNUMPKTSEL_PRD;
  841. reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
  842. reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
  843. reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
  844. reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
  845. dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
  846. }
  847. if (tx_thr_num && tx_maxburst) {
  848. reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
  849. reg |= DWC31_TXTHRNUMPKTSEL_PRD;
  850. reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
  851. reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
  852. reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
  853. reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
  854. dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
  855. }
  856. }
  857. return 0;
  858. err4:
  859. phy_power_off(dwc->usb3_generic_phy);
  860. err3:
  861. phy_power_off(dwc->usb2_generic_phy);
  862. err2:
  863. usb_phy_set_suspend(dwc->usb2_phy, 1);
  864. usb_phy_set_suspend(dwc->usb3_phy, 1);
  865. err1:
  866. usb_phy_shutdown(dwc->usb2_phy);
  867. usb_phy_shutdown(dwc->usb3_phy);
  868. phy_exit(dwc->usb2_generic_phy);
  869. phy_exit(dwc->usb3_generic_phy);
  870. err0a:
  871. dwc3_ulpi_exit(dwc);
  872. err0:
  873. return ret;
  874. }
  875. static int dwc3_core_get_phy(struct dwc3 *dwc)
  876. {
  877. struct device *dev = dwc->dev;
  878. struct device_node *node = dev->of_node;
  879. int ret;
  880. if (node) {
  881. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  882. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  883. } else {
  884. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  885. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  886. }
  887. if (IS_ERR(dwc->usb2_phy)) {
  888. ret = PTR_ERR(dwc->usb2_phy);
  889. if (ret == -ENXIO || ret == -ENODEV) {
  890. dwc->usb2_phy = NULL;
  891. } else if (ret == -EPROBE_DEFER) {
  892. return ret;
  893. } else {
  894. dev_err(dev, "no usb2 phy configured\n");
  895. return ret;
  896. }
  897. }
  898. if (IS_ERR(dwc->usb3_phy)) {
  899. ret = PTR_ERR(dwc->usb3_phy);
  900. if (ret == -ENXIO || ret == -ENODEV) {
  901. dwc->usb3_phy = NULL;
  902. } else if (ret == -EPROBE_DEFER) {
  903. return ret;
  904. } else {
  905. dev_err(dev, "no usb3 phy configured\n");
  906. return ret;
  907. }
  908. }
  909. dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
  910. if (IS_ERR(dwc->usb2_generic_phy)) {
  911. ret = PTR_ERR(dwc->usb2_generic_phy);
  912. if (ret == -ENOSYS || ret == -ENODEV) {
  913. dwc->usb2_generic_phy = NULL;
  914. } else if (ret == -EPROBE_DEFER) {
  915. return ret;
  916. } else {
  917. dev_err(dev, "no usb2 phy configured\n");
  918. return ret;
  919. }
  920. }
  921. dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
  922. if (IS_ERR(dwc->usb3_generic_phy)) {
  923. ret = PTR_ERR(dwc->usb3_generic_phy);
  924. if (ret == -ENOSYS || ret == -ENODEV) {
  925. dwc->usb3_generic_phy = NULL;
  926. } else if (ret == -EPROBE_DEFER) {
  927. return ret;
  928. } else {
  929. dev_err(dev, "no usb3 phy configured\n");
  930. return ret;
  931. }
  932. }
  933. return 0;
  934. }
  935. static int dwc3_core_init_mode(struct dwc3 *dwc)
  936. {
  937. struct device *dev = dwc->dev;
  938. int ret;
  939. switch (dwc->dr_mode) {
  940. case USB_DR_MODE_PERIPHERAL:
  941. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  942. if (dwc->usb2_phy)
  943. otg_set_vbus(dwc->usb2_phy->otg, false);
  944. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
  945. phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
  946. ret = dwc3_gadget_init(dwc);
  947. if (ret) {
  948. if (ret != -EPROBE_DEFER)
  949. dev_err(dev, "failed to initialize gadget\n");
  950. return ret;
  951. }
  952. break;
  953. case USB_DR_MODE_HOST:
  954. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
  955. if (dwc->usb2_phy)
  956. otg_set_vbus(dwc->usb2_phy->otg, true);
  957. phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
  958. phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
  959. ret = dwc3_host_init(dwc);
  960. if (ret) {
  961. if (ret != -EPROBE_DEFER)
  962. dev_err(dev, "failed to initialize host\n");
  963. return ret;
  964. }
  965. phy_calibrate(dwc->usb2_generic_phy);
  966. break;
  967. case USB_DR_MODE_OTG:
  968. INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
  969. ret = dwc3_drd_init(dwc);
  970. if (ret) {
  971. if (ret != -EPROBE_DEFER)
  972. dev_err(dev, "failed to initialize dual-role\n");
  973. return ret;
  974. }
  975. break;
  976. default:
  977. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  978. return -EINVAL;
  979. }
  980. return 0;
  981. }
  982. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  983. {
  984. switch (dwc->dr_mode) {
  985. case USB_DR_MODE_PERIPHERAL:
  986. dwc3_gadget_exit(dwc);
  987. break;
  988. case USB_DR_MODE_HOST:
  989. dwc3_host_exit(dwc);
  990. break;
  991. case USB_DR_MODE_OTG:
  992. dwc3_drd_exit(dwc);
  993. break;
  994. default:
  995. /* do nothing */
  996. break;
  997. }
  998. }
  999. static void dwc3_get_properties(struct dwc3 *dwc)
  1000. {
  1001. struct device *dev = dwc->dev;
  1002. u8 lpm_nyet_threshold;
  1003. u8 tx_de_emphasis;
  1004. u8 hird_threshold;
  1005. u8 rx_thr_num_pkt_prd;
  1006. u8 rx_max_burst_prd;
  1007. u8 tx_thr_num_pkt_prd;
  1008. u8 tx_max_burst_prd;
  1009. /* default to highest possible threshold */
  1010. lpm_nyet_threshold = 0xff;
  1011. /* default to -3.5dB de-emphasis */
  1012. tx_de_emphasis = 1;
  1013. /*
  1014. * default to assert utmi_sleep_n and use maximum allowed HIRD
  1015. * threshold value of 0b1100
  1016. */
  1017. hird_threshold = 12;
  1018. dwc->maximum_speed = usb_get_maximum_speed(dev);
  1019. dwc->dr_mode = usb_get_dr_mode(dev);
  1020. dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
  1021. dwc->sysdev_is_parent = device_property_read_bool(dev,
  1022. "linux,sysdev_is_parent");
  1023. if (dwc->sysdev_is_parent)
  1024. dwc->sysdev = dwc->dev->parent;
  1025. else
  1026. dwc->sysdev = dwc->dev;
  1027. dwc->has_lpm_erratum = device_property_read_bool(dev,
  1028. "snps,has-lpm-erratum");
  1029. device_property_read_u8(dev, "snps,lpm-nyet-threshold",
  1030. &lpm_nyet_threshold);
  1031. dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
  1032. "snps,is-utmi-l1-suspend");
  1033. device_property_read_u8(dev, "snps,hird-threshold",
  1034. &hird_threshold);
  1035. dwc->usb3_lpm_capable = device_property_read_bool(dev,
  1036. "snps,usb3_lpm_capable");
  1037. device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
  1038. &rx_thr_num_pkt_prd);
  1039. device_property_read_u8(dev, "snps,rx-max-burst-prd",
  1040. &rx_max_burst_prd);
  1041. device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
  1042. &tx_thr_num_pkt_prd);
  1043. device_property_read_u8(dev, "snps,tx-max-burst-prd",
  1044. &tx_max_burst_prd);
  1045. dwc->disable_scramble_quirk = device_property_read_bool(dev,
  1046. "snps,disable_scramble_quirk");
  1047. dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
  1048. "snps,u2exit_lfps_quirk");
  1049. dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
  1050. "snps,u2ss_inp3_quirk");
  1051. dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
  1052. "snps,req_p1p2p3_quirk");
  1053. dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
  1054. "snps,del_p1p2p3_quirk");
  1055. dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
  1056. "snps,del_phy_power_chg_quirk");
  1057. dwc->lfps_filter_quirk = device_property_read_bool(dev,
  1058. "snps,lfps_filter_quirk");
  1059. dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
  1060. "snps,rx_detect_poll_quirk");
  1061. dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
  1062. "snps,dis_u3_susphy_quirk");
  1063. dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
  1064. "snps,dis_u2_susphy_quirk");
  1065. dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
  1066. "snps,dis_enblslpm_quirk");
  1067. dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
  1068. "snps,dis_rxdet_inp3_quirk");
  1069. dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
  1070. "snps,dis-u2-freeclk-exists-quirk");
  1071. dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
  1072. "snps,dis-del-phy-power-chg-quirk");
  1073. dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
  1074. "snps,dis-tx-ipgap-linecheck-quirk");
  1075. dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
  1076. "snps,tx_de_emphasis_quirk");
  1077. device_property_read_u8(dev, "snps,tx_de_emphasis",
  1078. &tx_de_emphasis);
  1079. device_property_read_string(dev, "snps,hsphy_interface",
  1080. &dwc->hsphy_interface);
  1081. device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
  1082. &dwc->fladj);
  1083. dwc->dis_metastability_quirk = device_property_read_bool(dev,
  1084. "snps,dis_metastability_quirk");
  1085. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  1086. dwc->tx_de_emphasis = tx_de_emphasis;
  1087. dwc->hird_threshold = hird_threshold
  1088. | (dwc->is_utmi_l1_suspend << 4);
  1089. dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
  1090. dwc->rx_max_burst_prd = rx_max_burst_prd;
  1091. dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
  1092. dwc->tx_max_burst_prd = tx_max_burst_prd;
  1093. dwc->imod_interval = 0;
  1094. }
  1095. /* check whether the core supports IMOD */
  1096. bool dwc3_has_imod(struct dwc3 *dwc)
  1097. {
  1098. return ((dwc3_is_usb3(dwc) &&
  1099. dwc->revision >= DWC3_REVISION_300A) ||
  1100. (dwc3_is_usb31(dwc) &&
  1101. dwc->revision >= DWC3_USB31_REVISION_120A));
  1102. }
  1103. static void dwc3_check_params(struct dwc3 *dwc)
  1104. {
  1105. struct device *dev = dwc->dev;
  1106. /* Check for proper value of imod_interval */
  1107. if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
  1108. dev_warn(dwc->dev, "Interrupt moderation not supported\n");
  1109. dwc->imod_interval = 0;
  1110. }
  1111. /*
  1112. * Workaround for STAR 9000961433 which affects only version
  1113. * 3.00a of the DWC_usb3 core. This prevents the controller
  1114. * interrupt from being masked while handling events. IMOD
  1115. * allows us to work around this issue. Enable it for the
  1116. * affected version.
  1117. */
  1118. if (!dwc->imod_interval &&
  1119. (dwc->revision == DWC3_REVISION_300A))
  1120. dwc->imod_interval = 1;
  1121. /* Check the maximum_speed parameter */
  1122. switch (dwc->maximum_speed) {
  1123. case USB_SPEED_LOW:
  1124. case USB_SPEED_FULL:
  1125. case USB_SPEED_HIGH:
  1126. case USB_SPEED_SUPER:
  1127. case USB_SPEED_SUPER_PLUS:
  1128. break;
  1129. default:
  1130. dev_err(dev, "invalid maximum_speed parameter %d\n",
  1131. dwc->maximum_speed);
  1132. /* fall through */
  1133. case USB_SPEED_UNKNOWN:
  1134. /* default to superspeed */
  1135. dwc->maximum_speed = USB_SPEED_SUPER;
  1136. /*
  1137. * default to superspeed plus if we are capable.
  1138. */
  1139. if (dwc3_is_usb31(dwc) &&
  1140. (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
  1141. DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
  1142. dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
  1143. break;
  1144. }
  1145. }
  1146. static int dwc3_probe(struct platform_device *pdev)
  1147. {
  1148. struct device *dev = &pdev->dev;
  1149. struct resource *res, dwc_res;
  1150. struct dwc3 *dwc;
  1151. int ret;
  1152. void __iomem *regs;
  1153. dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
  1154. if (!dwc)
  1155. return -ENOMEM;
  1156. dwc->clks = devm_kmemdup(dev, dwc3_core_clks, sizeof(dwc3_core_clks),
  1157. GFP_KERNEL);
  1158. if (!dwc->clks)
  1159. return -ENOMEM;
  1160. dwc->dev = dev;
  1161. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1162. if (!res) {
  1163. dev_err(dev, "missing memory resource\n");
  1164. return -ENODEV;
  1165. }
  1166. dwc->xhci_resources[0].start = res->start;
  1167. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  1168. DWC3_XHCI_REGS_END;
  1169. dwc->xhci_resources[0].flags = res->flags;
  1170. dwc->xhci_resources[0].name = res->name;
  1171. /*
  1172. * Request memory region but exclude xHCI regs,
  1173. * since it will be requested by the xhci-plat driver.
  1174. */
  1175. dwc_res = *res;
  1176. dwc_res.start += DWC3_GLOBALS_REGS_START;
  1177. regs = devm_ioremap_resource(dev, &dwc_res);
  1178. if (IS_ERR(regs))
  1179. return PTR_ERR(regs);
  1180. dwc->regs = regs;
  1181. dwc->regs_size = resource_size(&dwc_res);
  1182. dwc3_get_properties(dwc);
  1183. dwc->reset = devm_reset_control_get_optional_shared(dev, NULL);
  1184. if (IS_ERR(dwc->reset))
  1185. return PTR_ERR(dwc->reset);
  1186. if (dev->of_node) {
  1187. dwc->num_clks = ARRAY_SIZE(dwc3_core_clks);
  1188. ret = clk_bulk_get(dev, dwc->num_clks, dwc->clks);
  1189. if (ret == -EPROBE_DEFER)
  1190. return ret;
  1191. /*
  1192. * Clocks are optional, but new DT platforms should support all
  1193. * clocks as required by the DT-binding.
  1194. */
  1195. if (ret)
  1196. dwc->num_clks = 0;
  1197. }
  1198. ret = reset_control_deassert(dwc->reset);
  1199. if (ret)
  1200. goto put_clks;
  1201. ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
  1202. if (ret)
  1203. goto assert_reset;
  1204. ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
  1205. if (ret)
  1206. goto unprepare_clks;
  1207. platform_set_drvdata(pdev, dwc);
  1208. dwc3_cache_hwparams(dwc);
  1209. spin_lock_init(&dwc->lock);
  1210. pm_runtime_set_active(dev);
  1211. pm_runtime_use_autosuspend(dev);
  1212. pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
  1213. pm_runtime_enable(dev);
  1214. ret = pm_runtime_get_sync(dev);
  1215. if (ret < 0)
  1216. goto err1;
  1217. pm_runtime_forbid(dev);
  1218. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  1219. if (ret) {
  1220. dev_err(dwc->dev, "failed to allocate event buffers\n");
  1221. ret = -ENOMEM;
  1222. goto err2;
  1223. }
  1224. ret = dwc3_get_dr_mode(dwc);
  1225. if (ret)
  1226. goto err3;
  1227. ret = dwc3_alloc_scratch_buffers(dwc);
  1228. if (ret)
  1229. goto err3;
  1230. ret = dwc3_core_init(dwc);
  1231. if (ret) {
  1232. dev_err(dev, "failed to initialize core\n");
  1233. goto err4;
  1234. }
  1235. dwc3_check_params(dwc);
  1236. ret = dwc3_core_init_mode(dwc);
  1237. if (ret)
  1238. goto err5;
  1239. dwc3_debugfs_init(dwc);
  1240. pm_runtime_put(dev);
  1241. return 0;
  1242. err5:
  1243. dwc3_event_buffers_cleanup(dwc);
  1244. dwc3_ulpi_exit(dwc);
  1245. err4:
  1246. dwc3_free_scratch_buffers(dwc);
  1247. err3:
  1248. dwc3_free_event_buffers(dwc);
  1249. err2:
  1250. pm_runtime_allow(&pdev->dev);
  1251. err1:
  1252. pm_runtime_put_sync(&pdev->dev);
  1253. pm_runtime_disable(&pdev->dev);
  1254. clk_bulk_disable(dwc->num_clks, dwc->clks);
  1255. unprepare_clks:
  1256. clk_bulk_unprepare(dwc->num_clks, dwc->clks);
  1257. assert_reset:
  1258. reset_control_assert(dwc->reset);
  1259. put_clks:
  1260. clk_bulk_put(dwc->num_clks, dwc->clks);
  1261. return ret;
  1262. }
  1263. static int dwc3_remove(struct platform_device *pdev)
  1264. {
  1265. struct dwc3 *dwc = platform_get_drvdata(pdev);
  1266. pm_runtime_get_sync(&pdev->dev);
  1267. dwc3_debugfs_exit(dwc);
  1268. dwc3_core_exit_mode(dwc);
  1269. dwc3_core_exit(dwc);
  1270. dwc3_ulpi_exit(dwc);
  1271. pm_runtime_put_sync(&pdev->dev);
  1272. pm_runtime_allow(&pdev->dev);
  1273. pm_runtime_disable(&pdev->dev);
  1274. dwc3_free_event_buffers(dwc);
  1275. dwc3_free_scratch_buffers(dwc);
  1276. clk_bulk_put(dwc->num_clks, dwc->clks);
  1277. return 0;
  1278. }
  1279. #ifdef CONFIG_PM
  1280. static int dwc3_core_init_for_resume(struct dwc3 *dwc)
  1281. {
  1282. int ret;
  1283. ret = reset_control_deassert(dwc->reset);
  1284. if (ret)
  1285. return ret;
  1286. ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
  1287. if (ret)
  1288. goto assert_reset;
  1289. ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
  1290. if (ret)
  1291. goto unprepare_clks;
  1292. ret = dwc3_core_init(dwc);
  1293. if (ret)
  1294. goto disable_clks;
  1295. return 0;
  1296. disable_clks:
  1297. clk_bulk_disable(dwc->num_clks, dwc->clks);
  1298. unprepare_clks:
  1299. clk_bulk_unprepare(dwc->num_clks, dwc->clks);
  1300. assert_reset:
  1301. reset_control_assert(dwc->reset);
  1302. return ret;
  1303. }
  1304. static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
  1305. {
  1306. unsigned long flags;
  1307. u32 reg;
  1308. switch (dwc->current_dr_role) {
  1309. case DWC3_GCTL_PRTCAP_DEVICE:
  1310. spin_lock_irqsave(&dwc->lock, flags);
  1311. dwc3_gadget_suspend(dwc);
  1312. spin_unlock_irqrestore(&dwc->lock, flags);
  1313. dwc3_core_exit(dwc);
  1314. break;
  1315. case DWC3_GCTL_PRTCAP_HOST:
  1316. if (!PMSG_IS_AUTO(msg)) {
  1317. dwc3_core_exit(dwc);
  1318. break;
  1319. }
  1320. /* Let controller to suspend HSPHY before PHY driver suspends */
  1321. if (dwc->dis_u2_susphy_quirk ||
  1322. dwc->dis_enblslpm_quirk) {
  1323. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1324. reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
  1325. DWC3_GUSB2PHYCFG_SUSPHY;
  1326. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1327. /* Give some time for USB2 PHY to suspend */
  1328. usleep_range(5000, 6000);
  1329. }
  1330. phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
  1331. phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
  1332. break;
  1333. case DWC3_GCTL_PRTCAP_OTG:
  1334. /* do nothing during runtime_suspend */
  1335. if (PMSG_IS_AUTO(msg))
  1336. break;
  1337. if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  1338. spin_lock_irqsave(&dwc->lock, flags);
  1339. dwc3_gadget_suspend(dwc);
  1340. spin_unlock_irqrestore(&dwc->lock, flags);
  1341. }
  1342. dwc3_otg_exit(dwc);
  1343. dwc3_core_exit(dwc);
  1344. break;
  1345. default:
  1346. /* do nothing */
  1347. break;
  1348. }
  1349. return 0;
  1350. }
  1351. static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
  1352. {
  1353. unsigned long flags;
  1354. int ret;
  1355. u32 reg;
  1356. switch (dwc->current_dr_role) {
  1357. case DWC3_GCTL_PRTCAP_DEVICE:
  1358. ret = dwc3_core_init_for_resume(dwc);
  1359. if (ret)
  1360. return ret;
  1361. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  1362. spin_lock_irqsave(&dwc->lock, flags);
  1363. dwc3_gadget_resume(dwc);
  1364. spin_unlock_irqrestore(&dwc->lock, flags);
  1365. break;
  1366. case DWC3_GCTL_PRTCAP_HOST:
  1367. if (!PMSG_IS_AUTO(msg)) {
  1368. ret = dwc3_core_init_for_resume(dwc);
  1369. if (ret)
  1370. return ret;
  1371. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
  1372. break;
  1373. }
  1374. /* Restore GUSB2PHYCFG bits that were modified in suspend */
  1375. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1376. if (dwc->dis_u2_susphy_quirk)
  1377. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  1378. if (dwc->dis_enblslpm_quirk)
  1379. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  1380. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1381. phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
  1382. phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
  1383. break;
  1384. case DWC3_GCTL_PRTCAP_OTG:
  1385. /* nothing to do on runtime_resume */
  1386. if (PMSG_IS_AUTO(msg))
  1387. break;
  1388. ret = dwc3_core_init(dwc);
  1389. if (ret)
  1390. return ret;
  1391. dwc3_set_prtcap(dwc, dwc->current_dr_role);
  1392. dwc3_otg_init(dwc);
  1393. if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
  1394. dwc3_otg_host_init(dwc);
  1395. } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  1396. spin_lock_irqsave(&dwc->lock, flags);
  1397. dwc3_gadget_resume(dwc);
  1398. spin_unlock_irqrestore(&dwc->lock, flags);
  1399. }
  1400. break;
  1401. default:
  1402. /* do nothing */
  1403. break;
  1404. }
  1405. return 0;
  1406. }
  1407. static int dwc3_runtime_checks(struct dwc3 *dwc)
  1408. {
  1409. switch (dwc->current_dr_role) {
  1410. case DWC3_GCTL_PRTCAP_DEVICE:
  1411. if (dwc->connected)
  1412. return -EBUSY;
  1413. break;
  1414. case DWC3_GCTL_PRTCAP_HOST:
  1415. default:
  1416. /* do nothing */
  1417. break;
  1418. }
  1419. return 0;
  1420. }
  1421. static int dwc3_runtime_suspend(struct device *dev)
  1422. {
  1423. struct dwc3 *dwc = dev_get_drvdata(dev);
  1424. int ret;
  1425. if (dwc3_runtime_checks(dwc))
  1426. return -EBUSY;
  1427. ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
  1428. if (ret)
  1429. return ret;
  1430. device_init_wakeup(dev, true);
  1431. return 0;
  1432. }
  1433. static int dwc3_runtime_resume(struct device *dev)
  1434. {
  1435. struct dwc3 *dwc = dev_get_drvdata(dev);
  1436. int ret;
  1437. device_init_wakeup(dev, false);
  1438. ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
  1439. if (ret)
  1440. return ret;
  1441. switch (dwc->current_dr_role) {
  1442. case DWC3_GCTL_PRTCAP_DEVICE:
  1443. dwc3_gadget_process_pending_events(dwc);
  1444. break;
  1445. case DWC3_GCTL_PRTCAP_HOST:
  1446. default:
  1447. /* do nothing */
  1448. break;
  1449. }
  1450. pm_runtime_mark_last_busy(dev);
  1451. return 0;
  1452. }
  1453. static int dwc3_runtime_idle(struct device *dev)
  1454. {
  1455. struct dwc3 *dwc = dev_get_drvdata(dev);
  1456. switch (dwc->current_dr_role) {
  1457. case DWC3_GCTL_PRTCAP_DEVICE:
  1458. if (dwc3_runtime_checks(dwc))
  1459. return -EBUSY;
  1460. break;
  1461. case DWC3_GCTL_PRTCAP_HOST:
  1462. default:
  1463. /* do nothing */
  1464. break;
  1465. }
  1466. pm_runtime_mark_last_busy(dev);
  1467. pm_runtime_autosuspend(dev);
  1468. return 0;
  1469. }
  1470. #endif /* CONFIG_PM */
  1471. #ifdef CONFIG_PM_SLEEP
  1472. static int dwc3_suspend(struct device *dev)
  1473. {
  1474. struct dwc3 *dwc = dev_get_drvdata(dev);
  1475. int ret;
  1476. ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
  1477. if (ret)
  1478. return ret;
  1479. pinctrl_pm_select_sleep_state(dev);
  1480. return 0;
  1481. }
  1482. static int dwc3_resume(struct device *dev)
  1483. {
  1484. struct dwc3 *dwc = dev_get_drvdata(dev);
  1485. int ret;
  1486. pinctrl_pm_select_default_state(dev);
  1487. ret = dwc3_resume_common(dwc, PMSG_RESUME);
  1488. if (ret)
  1489. return ret;
  1490. pm_runtime_disable(dev);
  1491. pm_runtime_set_active(dev);
  1492. pm_runtime_enable(dev);
  1493. return 0;
  1494. }
  1495. #endif /* CONFIG_PM_SLEEP */
  1496. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  1497. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  1498. SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
  1499. dwc3_runtime_idle)
  1500. };
  1501. #ifdef CONFIG_OF
  1502. static const struct of_device_id of_dwc3_match[] = {
  1503. {
  1504. .compatible = "snps,dwc3"
  1505. },
  1506. {
  1507. .compatible = "synopsys,dwc3"
  1508. },
  1509. { },
  1510. };
  1511. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  1512. #endif
  1513. #ifdef CONFIG_ACPI
  1514. #define ACPI_ID_INTEL_BSW "808622B7"
  1515. static const struct acpi_device_id dwc3_acpi_match[] = {
  1516. { ACPI_ID_INTEL_BSW, 0 },
  1517. { },
  1518. };
  1519. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  1520. #endif
  1521. static struct platform_driver dwc3_driver = {
  1522. .probe = dwc3_probe,
  1523. .remove = dwc3_remove,
  1524. .driver = {
  1525. .name = "dwc3",
  1526. .of_match_table = of_match_ptr(of_dwc3_match),
  1527. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  1528. .pm = &dwc3_dev_pm_ops,
  1529. },
  1530. };
  1531. module_platform_driver(dwc3_driver);
  1532. MODULE_ALIAS("platform:dwc3");
  1533. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1534. MODULE_LICENSE("GPL v2");
  1535. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");