core.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  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 optmizations\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. err4:
  1245. dwc3_free_scratch_buffers(dwc);
  1246. err3:
  1247. dwc3_free_event_buffers(dwc);
  1248. err2:
  1249. pm_runtime_allow(&pdev->dev);
  1250. err1:
  1251. pm_runtime_put_sync(&pdev->dev);
  1252. pm_runtime_disable(&pdev->dev);
  1253. clk_bulk_disable(dwc->num_clks, dwc->clks);
  1254. unprepare_clks:
  1255. clk_bulk_unprepare(dwc->num_clks, dwc->clks);
  1256. assert_reset:
  1257. reset_control_assert(dwc->reset);
  1258. put_clks:
  1259. clk_bulk_put(dwc->num_clks, dwc->clks);
  1260. return ret;
  1261. }
  1262. static int dwc3_remove(struct platform_device *pdev)
  1263. {
  1264. struct dwc3 *dwc = platform_get_drvdata(pdev);
  1265. pm_runtime_get_sync(&pdev->dev);
  1266. dwc3_debugfs_exit(dwc);
  1267. dwc3_core_exit_mode(dwc);
  1268. dwc3_core_exit(dwc);
  1269. dwc3_ulpi_exit(dwc);
  1270. pm_runtime_put_sync(&pdev->dev);
  1271. pm_runtime_allow(&pdev->dev);
  1272. pm_runtime_disable(&pdev->dev);
  1273. dwc3_free_event_buffers(dwc);
  1274. dwc3_free_scratch_buffers(dwc);
  1275. clk_bulk_put(dwc->num_clks, dwc->clks);
  1276. return 0;
  1277. }
  1278. #ifdef CONFIG_PM
  1279. static int dwc3_core_init_for_resume(struct dwc3 *dwc)
  1280. {
  1281. int ret;
  1282. ret = reset_control_deassert(dwc->reset);
  1283. if (ret)
  1284. return ret;
  1285. ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
  1286. if (ret)
  1287. goto assert_reset;
  1288. ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
  1289. if (ret)
  1290. goto unprepare_clks;
  1291. ret = dwc3_core_init(dwc);
  1292. if (ret)
  1293. goto disable_clks;
  1294. return 0;
  1295. disable_clks:
  1296. clk_bulk_disable(dwc->num_clks, dwc->clks);
  1297. unprepare_clks:
  1298. clk_bulk_unprepare(dwc->num_clks, dwc->clks);
  1299. assert_reset:
  1300. reset_control_assert(dwc->reset);
  1301. return ret;
  1302. }
  1303. static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
  1304. {
  1305. unsigned long flags;
  1306. u32 reg;
  1307. switch (dwc->current_dr_role) {
  1308. case DWC3_GCTL_PRTCAP_DEVICE:
  1309. spin_lock_irqsave(&dwc->lock, flags);
  1310. dwc3_gadget_suspend(dwc);
  1311. spin_unlock_irqrestore(&dwc->lock, flags);
  1312. dwc3_core_exit(dwc);
  1313. break;
  1314. case DWC3_GCTL_PRTCAP_HOST:
  1315. if (!PMSG_IS_AUTO(msg)) {
  1316. dwc3_core_exit(dwc);
  1317. break;
  1318. }
  1319. /* Let controller to suspend HSPHY before PHY driver suspends */
  1320. if (dwc->dis_u2_susphy_quirk ||
  1321. dwc->dis_enblslpm_quirk) {
  1322. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1323. reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
  1324. DWC3_GUSB2PHYCFG_SUSPHY;
  1325. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1326. /* Give some time for USB2 PHY to suspend */
  1327. usleep_range(5000, 6000);
  1328. }
  1329. phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
  1330. phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
  1331. break;
  1332. case DWC3_GCTL_PRTCAP_OTG:
  1333. /* do nothing during runtime_suspend */
  1334. if (PMSG_IS_AUTO(msg))
  1335. break;
  1336. if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  1337. spin_lock_irqsave(&dwc->lock, flags);
  1338. dwc3_gadget_suspend(dwc);
  1339. spin_unlock_irqrestore(&dwc->lock, flags);
  1340. }
  1341. dwc3_otg_exit(dwc);
  1342. dwc3_core_exit(dwc);
  1343. break;
  1344. default:
  1345. /* do nothing */
  1346. break;
  1347. }
  1348. return 0;
  1349. }
  1350. static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
  1351. {
  1352. unsigned long flags;
  1353. int ret;
  1354. u32 reg;
  1355. switch (dwc->current_dr_role) {
  1356. case DWC3_GCTL_PRTCAP_DEVICE:
  1357. ret = dwc3_core_init_for_resume(dwc);
  1358. if (ret)
  1359. return ret;
  1360. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  1361. spin_lock_irqsave(&dwc->lock, flags);
  1362. dwc3_gadget_resume(dwc);
  1363. spin_unlock_irqrestore(&dwc->lock, flags);
  1364. break;
  1365. case DWC3_GCTL_PRTCAP_HOST:
  1366. if (!PMSG_IS_AUTO(msg)) {
  1367. ret = dwc3_core_init_for_resume(dwc);
  1368. if (ret)
  1369. return ret;
  1370. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
  1371. break;
  1372. }
  1373. /* Restore GUSB2PHYCFG bits that were modified in suspend */
  1374. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1375. if (dwc->dis_u2_susphy_quirk)
  1376. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  1377. if (dwc->dis_enblslpm_quirk)
  1378. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  1379. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1380. phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
  1381. phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
  1382. break;
  1383. case DWC3_GCTL_PRTCAP_OTG:
  1384. /* nothing to do on runtime_resume */
  1385. if (PMSG_IS_AUTO(msg))
  1386. break;
  1387. ret = dwc3_core_init(dwc);
  1388. if (ret)
  1389. return ret;
  1390. dwc3_set_prtcap(dwc, dwc->current_dr_role);
  1391. dwc3_otg_init(dwc);
  1392. if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
  1393. dwc3_otg_host_init(dwc);
  1394. } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  1395. spin_lock_irqsave(&dwc->lock, flags);
  1396. dwc3_gadget_resume(dwc);
  1397. spin_unlock_irqrestore(&dwc->lock, flags);
  1398. }
  1399. break;
  1400. default:
  1401. /* do nothing */
  1402. break;
  1403. }
  1404. return 0;
  1405. }
  1406. static int dwc3_runtime_checks(struct dwc3 *dwc)
  1407. {
  1408. switch (dwc->current_dr_role) {
  1409. case DWC3_GCTL_PRTCAP_DEVICE:
  1410. if (dwc->connected)
  1411. return -EBUSY;
  1412. break;
  1413. case DWC3_GCTL_PRTCAP_HOST:
  1414. default:
  1415. /* do nothing */
  1416. break;
  1417. }
  1418. return 0;
  1419. }
  1420. static int dwc3_runtime_suspend(struct device *dev)
  1421. {
  1422. struct dwc3 *dwc = dev_get_drvdata(dev);
  1423. int ret;
  1424. if (dwc3_runtime_checks(dwc))
  1425. return -EBUSY;
  1426. ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
  1427. if (ret)
  1428. return ret;
  1429. device_init_wakeup(dev, true);
  1430. return 0;
  1431. }
  1432. static int dwc3_runtime_resume(struct device *dev)
  1433. {
  1434. struct dwc3 *dwc = dev_get_drvdata(dev);
  1435. int ret;
  1436. device_init_wakeup(dev, false);
  1437. ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
  1438. if (ret)
  1439. return ret;
  1440. switch (dwc->current_dr_role) {
  1441. case DWC3_GCTL_PRTCAP_DEVICE:
  1442. dwc3_gadget_process_pending_events(dwc);
  1443. break;
  1444. case DWC3_GCTL_PRTCAP_HOST:
  1445. default:
  1446. /* do nothing */
  1447. break;
  1448. }
  1449. pm_runtime_mark_last_busy(dev);
  1450. return 0;
  1451. }
  1452. static int dwc3_runtime_idle(struct device *dev)
  1453. {
  1454. struct dwc3 *dwc = dev_get_drvdata(dev);
  1455. switch (dwc->current_dr_role) {
  1456. case DWC3_GCTL_PRTCAP_DEVICE:
  1457. if (dwc3_runtime_checks(dwc))
  1458. return -EBUSY;
  1459. break;
  1460. case DWC3_GCTL_PRTCAP_HOST:
  1461. default:
  1462. /* do nothing */
  1463. break;
  1464. }
  1465. pm_runtime_mark_last_busy(dev);
  1466. pm_runtime_autosuspend(dev);
  1467. return 0;
  1468. }
  1469. #endif /* CONFIG_PM */
  1470. #ifdef CONFIG_PM_SLEEP
  1471. static int dwc3_suspend(struct device *dev)
  1472. {
  1473. struct dwc3 *dwc = dev_get_drvdata(dev);
  1474. int ret;
  1475. ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
  1476. if (ret)
  1477. return ret;
  1478. pinctrl_pm_select_sleep_state(dev);
  1479. return 0;
  1480. }
  1481. static int dwc3_resume(struct device *dev)
  1482. {
  1483. struct dwc3 *dwc = dev_get_drvdata(dev);
  1484. int ret;
  1485. pinctrl_pm_select_default_state(dev);
  1486. ret = dwc3_resume_common(dwc, PMSG_RESUME);
  1487. if (ret)
  1488. return ret;
  1489. pm_runtime_disable(dev);
  1490. pm_runtime_set_active(dev);
  1491. pm_runtime_enable(dev);
  1492. return 0;
  1493. }
  1494. #endif /* CONFIG_PM_SLEEP */
  1495. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  1496. SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
  1497. SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
  1498. dwc3_runtime_idle)
  1499. };
  1500. #ifdef CONFIG_OF
  1501. static const struct of_device_id of_dwc3_match[] = {
  1502. {
  1503. .compatible = "snps,dwc3"
  1504. },
  1505. {
  1506. .compatible = "synopsys,dwc3"
  1507. },
  1508. { },
  1509. };
  1510. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  1511. #endif
  1512. #ifdef CONFIG_ACPI
  1513. #define ACPI_ID_INTEL_BSW "808622B7"
  1514. static const struct acpi_device_id dwc3_acpi_match[] = {
  1515. { ACPI_ID_INTEL_BSW, 0 },
  1516. { },
  1517. };
  1518. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  1519. #endif
  1520. static struct platform_driver dwc3_driver = {
  1521. .probe = dwc3_probe,
  1522. .remove = dwc3_remove,
  1523. .driver = {
  1524. .name = "dwc3",
  1525. .of_match_table = of_match_ptr(of_dwc3_match),
  1526. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  1527. .pm = &dwc3_dev_pm_ops,
  1528. },
  1529. };
  1530. module_platform_driver(dwc3_driver);
  1531. MODULE_ALIAS("platform:dwc3");
  1532. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1533. MODULE_LICENSE("GPL v2");
  1534. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");