core.c 44 KB

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