core.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844
  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 - https://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/of_graph.h>
  26. #include <linux/acpi.h>
  27. #include <linux/pci.h>
  28. #include <linux/pinctrl/consumer.h>
  29. #include <linux/pinctrl/devinfo.h>
  30. #include <linux/reset.h>
  31. #include <linux/bitfield.h>
  32. #include <linux/usb/ch9.h>
  33. #include <linux/usb/gadget.h>
  34. #include <linux/usb/of.h>
  35. #include <linux/usb/otg.h>
  36. #include "core.h"
  37. #include "gadget.h"
  38. #include "glue.h"
  39. #include "io.h"
  40. #include "debug.h"
  41. #include "../host/xhci-ext-caps.h"
  42. #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
  43. /**
  44. * dwc3_get_dr_mode - Validates and sets dr_mode
  45. * @dwc: pointer to our context structure
  46. */
  47. static int dwc3_get_dr_mode(struct dwc3 *dwc)
  48. {
  49. enum usb_dr_mode mode;
  50. struct device *dev = dwc->dev;
  51. unsigned int hw_mode;
  52. if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
  53. dwc->dr_mode = USB_DR_MODE_OTG;
  54. mode = dwc->dr_mode;
  55. hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
  56. switch (hw_mode) {
  57. case DWC3_GHWPARAMS0_MODE_GADGET:
  58. if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
  59. dev_err(dev,
  60. "Controller does not support host mode.\n");
  61. return -EINVAL;
  62. }
  63. mode = USB_DR_MODE_PERIPHERAL;
  64. break;
  65. case DWC3_GHWPARAMS0_MODE_HOST:
  66. if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
  67. dev_err(dev,
  68. "Controller does not support device mode.\n");
  69. return -EINVAL;
  70. }
  71. mode = USB_DR_MODE_HOST;
  72. break;
  73. default:
  74. if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
  75. mode = USB_DR_MODE_HOST;
  76. else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
  77. mode = USB_DR_MODE_PERIPHERAL;
  78. /*
  79. * DWC_usb31 and DWC_usb3 v3.30a and higher do not support OTG
  80. * mode. If the controller supports DRD but the dr_mode is not
  81. * specified or set to OTG, then set the mode to peripheral.
  82. */
  83. if (mode == USB_DR_MODE_OTG && !dwc->edev &&
  84. (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
  85. !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
  86. !DWC3_VER_IS_PRIOR(DWC3, 330A))
  87. mode = USB_DR_MODE_PERIPHERAL;
  88. }
  89. if (mode != dwc->dr_mode) {
  90. dev_warn(dev,
  91. "Configuration mismatch. dr_mode forced to %s\n",
  92. mode == USB_DR_MODE_HOST ? "host" : "gadget");
  93. dwc->dr_mode = mode;
  94. }
  95. return 0;
  96. }
  97. void dwc3_enable_susphy(struct dwc3 *dwc, bool enable)
  98. {
  99. u32 reg;
  100. int i;
  101. for (i = 0; i < dwc->num_usb3_ports; i++) {
  102. reg = dwc3_readl(dwc, DWC3_GUSB3PIPECTL(i));
  103. if (enable && !dwc->dis_u3_susphy_quirk)
  104. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  105. else
  106. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  107. dwc3_writel(dwc, DWC3_GUSB3PIPECTL(i), reg);
  108. }
  109. for (i = 0; i < dwc->num_usb2_ports; i++) {
  110. reg = dwc3_readl(dwc, DWC3_GUSB2PHYCFG(i));
  111. if (enable && !dwc->dis_u2_susphy_quirk)
  112. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  113. else
  114. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  115. dwc3_writel(dwc, DWC3_GUSB2PHYCFG(i), reg);
  116. }
  117. }
  118. EXPORT_SYMBOL_GPL(dwc3_enable_susphy);
  119. void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode, bool ignore_susphy)
  120. {
  121. unsigned int hw_mode;
  122. u32 reg;
  123. reg = dwc3_readl(dwc, DWC3_GCTL);
  124. /*
  125. * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE and
  126. * GUSB2PHYCFG.SUSPHY should be cleared during mode switching,
  127. * and they can be set after core initialization.
  128. */
  129. hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
  130. if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD && !ignore_susphy) {
  131. if (DWC3_GCTL_PRTCAP(reg) != mode)
  132. dwc3_enable_susphy(dwc, false);
  133. }
  134. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  135. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  136. dwc3_writel(dwc, DWC3_GCTL, reg);
  137. dwc->current_dr_role = mode;
  138. trace_dwc3_set_prtcap(dwc, mode);
  139. }
  140. EXPORT_SYMBOL_GPL(dwc3_set_prtcap);
  141. static void __dwc3_set_mode(struct work_struct *work)
  142. {
  143. struct dwc3 *dwc = work_to_dwc(work);
  144. unsigned long flags;
  145. int ret;
  146. u32 reg;
  147. u32 desired_dr_role;
  148. int i;
  149. mutex_lock(&dwc->mutex);
  150. spin_lock_irqsave(&dwc->lock, flags);
  151. desired_dr_role = dwc->desired_dr_role;
  152. spin_unlock_irqrestore(&dwc->lock, flags);
  153. pm_runtime_get_sync(dwc->dev);
  154. if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
  155. dwc3_otg_update(dwc, 0);
  156. if (!desired_dr_role)
  157. goto out;
  158. if (desired_dr_role == dwc->current_dr_role)
  159. goto out;
  160. if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
  161. goto out;
  162. switch (dwc->current_dr_role) {
  163. case DWC3_GCTL_PRTCAP_HOST:
  164. dwc3_host_exit(dwc);
  165. break;
  166. case DWC3_GCTL_PRTCAP_DEVICE:
  167. dwc3_gadget_exit(dwc);
  168. dwc3_event_buffers_cleanup(dwc);
  169. break;
  170. case DWC3_GCTL_PRTCAP_OTG:
  171. dwc3_otg_exit(dwc);
  172. spin_lock_irqsave(&dwc->lock, flags);
  173. dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
  174. spin_unlock_irqrestore(&dwc->lock, flags);
  175. dwc3_otg_update(dwc, 1);
  176. break;
  177. default:
  178. break;
  179. }
  180. /*
  181. * When current_dr_role is not set, there's no role switching.
  182. * Only perform GCTL.CoreSoftReset when there's DRD role switching.
  183. */
  184. if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
  185. DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
  186. desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
  187. reg = dwc3_readl(dwc, DWC3_GCTL);
  188. reg |= DWC3_GCTL_CORESOFTRESET;
  189. dwc3_writel(dwc, DWC3_GCTL, reg);
  190. /*
  191. * Wait for internal clocks to synchronized. DWC_usb31 and
  192. * DWC_usb32 may need at least 50ms (less for DWC_usb3). To
  193. * keep it consistent across different IPs, let's wait up to
  194. * 100ms before clearing GCTL.CORESOFTRESET.
  195. */
  196. msleep(100);
  197. reg = dwc3_readl(dwc, DWC3_GCTL);
  198. reg &= ~DWC3_GCTL_CORESOFTRESET;
  199. dwc3_writel(dwc, DWC3_GCTL, reg);
  200. }
  201. spin_lock_irqsave(&dwc->lock, flags);
  202. dwc3_set_prtcap(dwc, desired_dr_role, false);
  203. spin_unlock_irqrestore(&dwc->lock, flags);
  204. switch (desired_dr_role) {
  205. case DWC3_GCTL_PRTCAP_HOST:
  206. ret = dwc3_host_init(dwc);
  207. if (ret) {
  208. dev_err(dwc->dev, "failed to initialize host\n");
  209. } else {
  210. if (dwc->usb2_phy)
  211. otg_set_vbus(dwc->usb2_phy->otg, true);
  212. for (i = 0; i < dwc->num_usb2_ports; i++)
  213. phy_set_mode(dwc->usb2_generic_phy[i], PHY_MODE_USB_HOST);
  214. for (i = 0; i < dwc->num_usb3_ports; i++)
  215. phy_set_mode(dwc->usb3_generic_phy[i], PHY_MODE_USB_HOST);
  216. if (dwc->dis_split_quirk) {
  217. reg = dwc3_readl(dwc, DWC3_GUCTL3);
  218. reg |= DWC3_GUCTL3_SPLITDISABLE;
  219. dwc3_writel(dwc, DWC3_GUCTL3, reg);
  220. }
  221. }
  222. break;
  223. case DWC3_GCTL_PRTCAP_DEVICE:
  224. dwc3_core_soft_reset(dwc);
  225. dwc3_event_buffers_setup(dwc);
  226. if (dwc->usb2_phy)
  227. otg_set_vbus(dwc->usb2_phy->otg, false);
  228. phy_set_mode(dwc->usb2_generic_phy[0], PHY_MODE_USB_DEVICE);
  229. phy_set_mode(dwc->usb3_generic_phy[0], PHY_MODE_USB_DEVICE);
  230. ret = dwc3_gadget_init(dwc);
  231. if (ret)
  232. dev_err(dwc->dev, "failed to initialize peripheral\n");
  233. break;
  234. case DWC3_GCTL_PRTCAP_OTG:
  235. dwc3_otg_init(dwc);
  236. dwc3_otg_update(dwc, 0);
  237. break;
  238. default:
  239. break;
  240. }
  241. out:
  242. pm_runtime_put_autosuspend(dwc->dev);
  243. mutex_unlock(&dwc->mutex);
  244. }
  245. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  246. {
  247. unsigned long flags;
  248. if (dwc->dr_mode != USB_DR_MODE_OTG)
  249. return;
  250. spin_lock_irqsave(&dwc->lock, flags);
  251. dwc->desired_dr_role = mode;
  252. spin_unlock_irqrestore(&dwc->lock, flags);
  253. queue_work(system_freezable_wq, &dwc->drd_work);
  254. }
  255. u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
  256. {
  257. struct dwc3 *dwc = dep->dwc;
  258. u32 reg;
  259. dwc3_writel(dwc, DWC3_GDBGFIFOSPACE,
  260. DWC3_GDBGFIFOSPACE_NUM(dep->number) |
  261. DWC3_GDBGFIFOSPACE_TYPE(type));
  262. reg = dwc3_readl(dwc, DWC3_GDBGFIFOSPACE);
  263. return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
  264. }
  265. /**
  266. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  267. * @dwc: pointer to our context structure
  268. */
  269. int dwc3_core_soft_reset(struct dwc3 *dwc)
  270. {
  271. u32 reg;
  272. int retries = 1000;
  273. /*
  274. * We're resetting only the device side because, if we're in host mode,
  275. * XHCI driver will reset the host block. If dwc3 was configured for
  276. * host-only mode, then we can return early.
  277. */
  278. if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
  279. return 0;
  280. reg = dwc3_readl(dwc, DWC3_DCTL);
  281. reg |= DWC3_DCTL_CSFTRST;
  282. reg &= ~DWC3_DCTL_RUN_STOP;
  283. dwc3_gadget_dctl_write_safe(dwc, reg);
  284. /*
  285. * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit
  286. * is cleared only after all the clocks are synchronized. This can
  287. * take a little more than 50ms. Set the polling rate at 20ms
  288. * for 10 times instead.
  289. */
  290. if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
  291. retries = 10;
  292. do {
  293. reg = dwc3_readl(dwc, DWC3_DCTL);
  294. if (!(reg & DWC3_DCTL_CSFTRST))
  295. goto done;
  296. if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32))
  297. msleep(20);
  298. else
  299. udelay(1);
  300. } while (--retries);
  301. dev_warn(dwc->dev, "DWC3 controller soft reset failed.\n");
  302. return -ETIMEDOUT;
  303. done:
  304. /*
  305. * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit
  306. * is cleared, we must wait at least 50ms before accessing the PHY
  307. * domain (synchronization delay).
  308. */
  309. if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A))
  310. msleep(50);
  311. return 0;
  312. }
  313. /*
  314. * dwc3_frame_length_adjustment - Adjusts frame length if required
  315. * @dwc3: Pointer to our controller context structure
  316. */
  317. static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
  318. {
  319. u32 reg;
  320. u32 dft;
  321. if (DWC3_VER_IS_PRIOR(DWC3, 250A))
  322. return;
  323. if (dwc->fladj == 0)
  324. return;
  325. reg = dwc3_readl(dwc, DWC3_GFLADJ);
  326. dft = reg & DWC3_GFLADJ_30MHZ_MASK;
  327. if (dft != dwc->fladj) {
  328. reg &= ~DWC3_GFLADJ_30MHZ_MASK;
  329. reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
  330. dwc3_writel(dwc, DWC3_GFLADJ, reg);
  331. }
  332. }
  333. /**
  334. * dwc3_ref_clk_period - Reference clock period configuration
  335. * Default reference clock period depends on hardware
  336. * configuration. For systems with reference clock that differs
  337. * from the default, this will set clock period in DWC3_GUCTL
  338. * register.
  339. * @dwc: Pointer to our controller context structure
  340. */
  341. static void dwc3_ref_clk_period(struct dwc3 *dwc)
  342. {
  343. unsigned long period;
  344. unsigned long fladj;
  345. unsigned long decr;
  346. unsigned long rate;
  347. u32 reg;
  348. if (dwc->ref_clk) {
  349. rate = clk_get_rate(dwc->ref_clk);
  350. if (!rate)
  351. return;
  352. period = NSEC_PER_SEC / rate;
  353. } else if (dwc->ref_clk_per) {
  354. period = dwc->ref_clk_per;
  355. rate = NSEC_PER_SEC / period;
  356. } else {
  357. return;
  358. }
  359. reg = dwc3_readl(dwc, DWC3_GUCTL);
  360. reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
  361. reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
  362. dwc3_writel(dwc, DWC3_GUCTL, reg);
  363. if (DWC3_VER_IS_PRIOR(DWC3, 250A))
  364. return;
  365. /*
  366. * The calculation below is
  367. *
  368. * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
  369. *
  370. * but rearranged for fixed-point arithmetic. The division must be
  371. * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
  372. * neither does rate * period).
  373. *
  374. * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
  375. * nanoseconds of error caused by the truncation which happened during
  376. * the division when calculating rate or period (whichever one was
  377. * derived from the other). We first calculate the relative error, then
  378. * scale it to units of 8 ppm.
  379. */
  380. fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
  381. fladj -= 125000;
  382. /*
  383. * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
  384. */
  385. decr = 480000000 / rate;
  386. reg = dwc3_readl(dwc, DWC3_GFLADJ);
  387. reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
  388. & ~DWC3_GFLADJ_240MHZDECR
  389. & ~DWC3_GFLADJ_240MHZDECR_PLS1;
  390. reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
  391. | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
  392. | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
  393. if (dwc->gfladj_refclk_lpm_sel)
  394. reg |= DWC3_GFLADJ_REFCLK_LPM_SEL;
  395. dwc3_writel(dwc, DWC3_GFLADJ, reg);
  396. }
  397. /**
  398. * dwc3_free_one_event_buffer - Frees one event buffer
  399. * @dwc: Pointer to our controller context structure
  400. * @evt: Pointer to event buffer to be freed
  401. */
  402. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  403. struct dwc3_event_buffer *evt)
  404. {
  405. dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
  406. }
  407. /**
  408. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  409. * @dwc: Pointer to our controller context structure
  410. * @length: size of the event buffer
  411. *
  412. * Returns a pointer to the allocated event buffer structure on success
  413. * otherwise ERR_PTR(errno).
  414. */
  415. static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
  416. unsigned int length)
  417. {
  418. struct dwc3_event_buffer *evt;
  419. evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
  420. if (!evt)
  421. return ERR_PTR(-ENOMEM);
  422. evt->dwc = dwc;
  423. evt->length = length;
  424. evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
  425. if (!evt->cache)
  426. return ERR_PTR(-ENOMEM);
  427. evt->buf = dma_alloc_coherent(dwc->sysdev, length,
  428. &evt->dma, GFP_KERNEL);
  429. if (!evt->buf)
  430. return ERR_PTR(-ENOMEM);
  431. return evt;
  432. }
  433. /**
  434. * dwc3_free_event_buffers - frees all allocated event buffers
  435. * @dwc: Pointer to our controller context structure
  436. */
  437. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  438. {
  439. struct dwc3_event_buffer *evt;
  440. evt = dwc->ev_buf;
  441. if (evt)
  442. dwc3_free_one_event_buffer(dwc, evt);
  443. }
  444. /**
  445. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  446. * @dwc: pointer to our controller context structure
  447. * @length: size of event buffer
  448. *
  449. * Returns 0 on success otherwise negative errno. In the error case, dwc
  450. * may contain some buffers allocated but not all which were requested.
  451. */
  452. static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned int length)
  453. {
  454. struct dwc3_event_buffer *evt;
  455. unsigned int hw_mode;
  456. hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
  457. if (hw_mode == DWC3_GHWPARAMS0_MODE_HOST) {
  458. dwc->ev_buf = NULL;
  459. return 0;
  460. }
  461. evt = dwc3_alloc_one_event_buffer(dwc, length);
  462. if (IS_ERR(evt)) {
  463. dev_err(dwc->dev, "can't allocate event buffer\n");
  464. return PTR_ERR(evt);
  465. }
  466. dwc->ev_buf = evt;
  467. return 0;
  468. }
  469. /**
  470. * dwc3_event_buffers_setup - setup our allocated event buffers
  471. * @dwc: pointer to our controller context structure
  472. *
  473. * Returns 0 on success otherwise negative errno.
  474. */
  475. int dwc3_event_buffers_setup(struct dwc3 *dwc)
  476. {
  477. struct dwc3_event_buffer *evt;
  478. u32 reg;
  479. if (!dwc->ev_buf)
  480. return 0;
  481. evt = dwc->ev_buf;
  482. evt->lpos = 0;
  483. dwc3_writel(dwc, DWC3_GEVNTADRLO(0),
  484. lower_32_bits(evt->dma));
  485. dwc3_writel(dwc, DWC3_GEVNTADRHI(0),
  486. upper_32_bits(evt->dma));
  487. dwc3_writel(dwc, DWC3_GEVNTSIZ(0),
  488. DWC3_GEVNTSIZ_SIZE(evt->length));
  489. /* Clear any stale event */
  490. reg = dwc3_readl(dwc, DWC3_GEVNTCOUNT(0));
  491. dwc3_writel(dwc, DWC3_GEVNTCOUNT(0), reg);
  492. return 0;
  493. }
  494. void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  495. {
  496. struct dwc3_event_buffer *evt;
  497. u32 reg;
  498. if (!dwc->ev_buf)
  499. return;
  500. /*
  501. * Exynos platforms may not be able to access event buffer if the
  502. * controller failed to halt on dwc3_core_exit().
  503. */
  504. reg = dwc3_readl(dwc, DWC3_DSTS);
  505. if (!(reg & DWC3_DSTS_DEVCTRLHLT))
  506. return;
  507. evt = dwc->ev_buf;
  508. evt->lpos = 0;
  509. dwc3_writel(dwc, DWC3_GEVNTADRLO(0), 0);
  510. dwc3_writel(dwc, DWC3_GEVNTADRHI(0), 0);
  511. dwc3_writel(dwc, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
  512. | DWC3_GEVNTSIZ_SIZE(0));
  513. /* Clear any stale event */
  514. reg = dwc3_readl(dwc, DWC3_GEVNTCOUNT(0));
  515. dwc3_writel(dwc, DWC3_GEVNTCOUNT(0), reg);
  516. }
  517. static void dwc3_core_num_eps(struct dwc3 *dwc)
  518. {
  519. struct dwc3_hwparams *parms = &dwc->hwparams;
  520. dwc->num_eps = DWC3_NUM_EPS(parms);
  521. }
  522. static void dwc3_cache_hwparams(struct dwc3 *dwc)
  523. {
  524. struct dwc3_hwparams *parms = &dwc->hwparams;
  525. parms->hwparams0 = dwc3_readl(dwc, DWC3_GHWPARAMS0);
  526. parms->hwparams1 = dwc3_readl(dwc, DWC3_GHWPARAMS1);
  527. parms->hwparams2 = dwc3_readl(dwc, DWC3_GHWPARAMS2);
  528. parms->hwparams3 = dwc3_readl(dwc, DWC3_GHWPARAMS3);
  529. parms->hwparams4 = dwc3_readl(dwc, DWC3_GHWPARAMS4);
  530. parms->hwparams5 = dwc3_readl(dwc, DWC3_GHWPARAMS5);
  531. parms->hwparams6 = dwc3_readl(dwc, DWC3_GHWPARAMS6);
  532. parms->hwparams7 = dwc3_readl(dwc, DWC3_GHWPARAMS7);
  533. parms->hwparams8 = dwc3_readl(dwc, DWC3_GHWPARAMS8);
  534. if (DWC3_IP_IS(DWC32))
  535. parms->hwparams9 = dwc3_readl(dwc, DWC3_GHWPARAMS9);
  536. }
  537. static void dwc3_config_soc_bus(struct dwc3 *dwc)
  538. {
  539. if (dwc->gsbuscfg0_reqinfo != DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED) {
  540. u32 reg;
  541. reg = dwc3_readl(dwc, DWC3_GSBUSCFG0);
  542. reg &= ~DWC3_GSBUSCFG0_REQINFO(~0);
  543. reg |= DWC3_GSBUSCFG0_REQINFO(dwc->gsbuscfg0_reqinfo);
  544. dwc3_writel(dwc, DWC3_GSBUSCFG0, reg);
  545. }
  546. }
  547. static int dwc3_core_ulpi_init(struct dwc3 *dwc)
  548. {
  549. int intf;
  550. int ret = 0;
  551. intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
  552. if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
  553. (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
  554. dwc->hsphy_interface &&
  555. !strncmp(dwc->hsphy_interface, "ulpi", 4)))
  556. ret = dwc3_ulpi_init(dwc);
  557. return ret;
  558. }
  559. static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
  560. {
  561. u32 reg;
  562. reg = dwc3_readl(dwc, DWC3_GUSB3PIPECTL(index));
  563. /*
  564. * Make sure UX_EXIT_PX is cleared as that causes issues with some
  565. * PHYs. Also, this bit is not supposed to be used in normal operation.
  566. */
  567. reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
  568. /* Ensure the GUSB3PIPECTL.SUSPENDENABLE is cleared prior to phy init. */
  569. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  570. if (dwc->u2ss_inp3_quirk)
  571. reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
  572. if (dwc->dis_rxdet_inp3_quirk)
  573. reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
  574. if (dwc->req_p1p2p3_quirk)
  575. reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
  576. if (dwc->del_p1p2p3_quirk)
  577. reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
  578. if (dwc->del_phy_power_chg_quirk)
  579. reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
  580. if (dwc->lfps_filter_quirk)
  581. reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
  582. if (dwc->rx_detect_poll_quirk)
  583. reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
  584. if (dwc->tx_de_emphasis_quirk)
  585. reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
  586. if (dwc->dis_del_phy_power_chg_quirk)
  587. reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
  588. dwc3_writel(dwc, DWC3_GUSB3PIPECTL(index), reg);
  589. return 0;
  590. }
  591. static int dwc3_hs_phy_setup(struct dwc3 *dwc, int index)
  592. {
  593. u32 reg;
  594. reg = dwc3_readl(dwc, DWC3_GUSB2PHYCFG(index));
  595. /* Select the HS PHY interface */
  596. switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
  597. case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
  598. if (dwc->hsphy_interface &&
  599. !strncmp(dwc->hsphy_interface, "utmi", 4)) {
  600. reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
  601. break;
  602. } else if (dwc->hsphy_interface &&
  603. !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
  604. reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
  605. dwc3_writel(dwc, DWC3_GUSB2PHYCFG(index), reg);
  606. } else {
  607. /* Relying on default value. */
  608. if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
  609. break;
  610. }
  611. fallthrough;
  612. case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
  613. default:
  614. break;
  615. }
  616. switch (dwc->hsphy_mode) {
  617. case USBPHY_INTERFACE_MODE_UTMI:
  618. reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
  619. DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
  620. reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
  621. DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
  622. break;
  623. case USBPHY_INTERFACE_MODE_UTMIW:
  624. reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
  625. DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
  626. reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
  627. DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
  628. break;
  629. default:
  630. break;
  631. }
  632. /* Ensure the GUSB2PHYCFG.SUSPHY is cleared prior to phy init. */
  633. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  634. if (dwc->dis_enblslpm_quirk)
  635. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  636. else
  637. reg |= DWC3_GUSB2PHYCFG_ENBLSLPM;
  638. if (dwc->dis_u2_freeclk_exists_quirk || dwc->gfladj_refclk_lpm_sel)
  639. reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
  640. /*
  641. * Some ULPI USB PHY does not support internal VBUS supply, to drive
  642. * the CPEN pin requires the configuration of the ULPI DRVVBUSEXTERNAL
  643. * bit of OTG_CTRL register. Controller configures the USB2 PHY
  644. * ULPIEXTVBUSDRV bit[17] of the GUSB2PHYCFG register to drive vBus
  645. * with an external supply.
  646. */
  647. if (dwc->ulpi_ext_vbus_drv)
  648. reg |= DWC3_GUSB2PHYCFG_ULPIEXTVBUSDRV;
  649. dwc3_writel(dwc, DWC3_GUSB2PHYCFG(index), reg);
  650. return 0;
  651. }
  652. /**
  653. * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
  654. * @dwc: Pointer to our controller context structure
  655. *
  656. * Returns 0 on success. The USB PHY interfaces are configured but not
  657. * initialized. The PHY interfaces and the PHYs get initialized together with
  658. * the core in dwc3_core_init.
  659. */
  660. static int dwc3_phy_setup(struct dwc3 *dwc)
  661. {
  662. int i;
  663. int ret;
  664. for (i = 0; i < dwc->num_usb3_ports; i++) {
  665. ret = dwc3_ss_phy_setup(dwc, i);
  666. if (ret)
  667. return ret;
  668. }
  669. for (i = 0; i < dwc->num_usb2_ports; i++) {
  670. ret = dwc3_hs_phy_setup(dwc, i);
  671. if (ret)
  672. return ret;
  673. }
  674. return 0;
  675. }
  676. static int dwc3_phy_init(struct dwc3 *dwc)
  677. {
  678. int ret;
  679. int i;
  680. int j;
  681. usb_phy_init(dwc->usb2_phy);
  682. usb_phy_init(dwc->usb3_phy);
  683. for (i = 0; i < dwc->num_usb2_ports; i++) {
  684. ret = phy_init(dwc->usb2_generic_phy[i]);
  685. if (ret < 0)
  686. goto err_exit_usb2_phy;
  687. }
  688. for (j = 0; j < dwc->num_usb3_ports; j++) {
  689. ret = phy_init(dwc->usb3_generic_phy[j]);
  690. if (ret < 0)
  691. goto err_exit_usb3_phy;
  692. }
  693. /*
  694. * Above DWC_usb3.0 1.94a, it is recommended to set
  695. * DWC3_GUSB3PIPECTL_SUSPHY and DWC3_GUSB2PHYCFG_SUSPHY to '0' during
  696. * coreConsultant configuration. So default value will be '0' when the
  697. * core is reset. Application needs to set it to '1' after the core
  698. * initialization is completed.
  699. *
  700. * Certain phy requires to be in P0 power state during initialization.
  701. * Make sure GUSB3PIPECTL.SUSPENDENABLE and GUSB2PHYCFG.SUSPHY are clear
  702. * prior to phy init to maintain in the P0 state.
  703. *
  704. * After phy initialization, some phy operations can only be executed
  705. * while in lower P states. Ensure GUSB3PIPECTL.SUSPENDENABLE and
  706. * GUSB2PHYCFG.SUSPHY are set soon after initialization to avoid
  707. * blocking phy ops.
  708. */
  709. if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
  710. dwc3_enable_susphy(dwc, true);
  711. return 0;
  712. err_exit_usb3_phy:
  713. while (--j >= 0)
  714. phy_exit(dwc->usb3_generic_phy[j]);
  715. err_exit_usb2_phy:
  716. while (--i >= 0)
  717. phy_exit(dwc->usb2_generic_phy[i]);
  718. usb_phy_shutdown(dwc->usb3_phy);
  719. usb_phy_shutdown(dwc->usb2_phy);
  720. return ret;
  721. }
  722. static void dwc3_phy_exit(struct dwc3 *dwc)
  723. {
  724. int i;
  725. for (i = 0; i < dwc->num_usb3_ports; i++)
  726. phy_exit(dwc->usb3_generic_phy[i]);
  727. for (i = 0; i < dwc->num_usb2_ports; i++)
  728. phy_exit(dwc->usb2_generic_phy[i]);
  729. usb_phy_shutdown(dwc->usb3_phy);
  730. usb_phy_shutdown(dwc->usb2_phy);
  731. }
  732. static int dwc3_phy_power_on(struct dwc3 *dwc)
  733. {
  734. int ret;
  735. int i;
  736. int j;
  737. usb_phy_set_suspend(dwc->usb2_phy, 0);
  738. usb_phy_set_suspend(dwc->usb3_phy, 0);
  739. for (i = 0; i < dwc->num_usb2_ports; i++) {
  740. ret = phy_power_on(dwc->usb2_generic_phy[i]);
  741. if (ret < 0)
  742. goto err_power_off_usb2_phy;
  743. }
  744. for (j = 0; j < dwc->num_usb3_ports; j++) {
  745. ret = phy_power_on(dwc->usb3_generic_phy[j]);
  746. if (ret < 0)
  747. goto err_power_off_usb3_phy;
  748. }
  749. return 0;
  750. err_power_off_usb3_phy:
  751. while (--j >= 0)
  752. phy_power_off(dwc->usb3_generic_phy[j]);
  753. err_power_off_usb2_phy:
  754. while (--i >= 0)
  755. phy_power_off(dwc->usb2_generic_phy[i]);
  756. usb_phy_set_suspend(dwc->usb3_phy, 1);
  757. usb_phy_set_suspend(dwc->usb2_phy, 1);
  758. return ret;
  759. }
  760. static void dwc3_phy_power_off(struct dwc3 *dwc)
  761. {
  762. int i;
  763. for (i = 0; i < dwc->num_usb3_ports; i++)
  764. phy_power_off(dwc->usb3_generic_phy[i]);
  765. for (i = 0; i < dwc->num_usb2_ports; i++)
  766. phy_power_off(dwc->usb2_generic_phy[i]);
  767. usb_phy_set_suspend(dwc->usb3_phy, 1);
  768. usb_phy_set_suspend(dwc->usb2_phy, 1);
  769. }
  770. static int dwc3_clk_enable(struct dwc3 *dwc)
  771. {
  772. int ret;
  773. ret = clk_prepare_enable(dwc->bus_clk);
  774. if (ret)
  775. return ret;
  776. ret = clk_prepare_enable(dwc->ref_clk);
  777. if (ret)
  778. goto disable_bus_clk;
  779. ret = clk_prepare_enable(dwc->susp_clk);
  780. if (ret)
  781. goto disable_ref_clk;
  782. ret = clk_prepare_enable(dwc->utmi_clk);
  783. if (ret)
  784. goto disable_susp_clk;
  785. ret = clk_prepare_enable(dwc->pipe_clk);
  786. if (ret)
  787. goto disable_utmi_clk;
  788. return 0;
  789. disable_utmi_clk:
  790. clk_disable_unprepare(dwc->utmi_clk);
  791. disable_susp_clk:
  792. clk_disable_unprepare(dwc->susp_clk);
  793. disable_ref_clk:
  794. clk_disable_unprepare(dwc->ref_clk);
  795. disable_bus_clk:
  796. clk_disable_unprepare(dwc->bus_clk);
  797. return ret;
  798. }
  799. static void dwc3_clk_disable(struct dwc3 *dwc)
  800. {
  801. clk_disable_unprepare(dwc->pipe_clk);
  802. clk_disable_unprepare(dwc->utmi_clk);
  803. clk_disable_unprepare(dwc->susp_clk);
  804. clk_disable_unprepare(dwc->ref_clk);
  805. clk_disable_unprepare(dwc->bus_clk);
  806. }
  807. void dwc3_core_exit(struct dwc3 *dwc)
  808. {
  809. dwc3_event_buffers_cleanup(dwc);
  810. dwc3_phy_power_off(dwc);
  811. dwc3_phy_exit(dwc);
  812. dwc3_clk_disable(dwc);
  813. reset_control_assert(dwc->reset);
  814. }
  815. EXPORT_SYMBOL_GPL(dwc3_core_exit);
  816. static bool dwc3_core_is_valid(struct dwc3 *dwc)
  817. {
  818. u32 reg;
  819. reg = dwc3_readl(dwc, DWC3_GSNPSID);
  820. dwc->ip = DWC3_GSNPS_ID(reg);
  821. if (dwc->ip == DWC4_IP)
  822. dwc->ip = DWC32_IP;
  823. /* This should read as U3 followed by revision number */
  824. if (DWC3_IP_IS(DWC3)) {
  825. dwc->revision = reg;
  826. } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) {
  827. dwc->revision = dwc3_readl(dwc, DWC3_VER_NUMBER);
  828. dwc->version_type = dwc3_readl(dwc, DWC3_VER_TYPE);
  829. } else {
  830. return false;
  831. }
  832. return true;
  833. }
  834. static void dwc3_core_setup_global_control(struct dwc3 *dwc)
  835. {
  836. unsigned int power_opt;
  837. unsigned int hw_mode;
  838. u32 reg;
  839. reg = dwc3_readl(dwc, DWC3_GCTL);
  840. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  841. hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
  842. power_opt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
  843. switch (power_opt) {
  844. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  845. /**
  846. * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
  847. * issue which would cause xHCI compliance tests to fail.
  848. *
  849. * Because of that we cannot enable clock gating on such
  850. * configurations.
  851. *
  852. * Refers to:
  853. *
  854. * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
  855. * SOF/ITP Mode Used
  856. */
  857. if ((dwc->dr_mode == USB_DR_MODE_HOST ||
  858. dwc->dr_mode == USB_DR_MODE_OTG) &&
  859. DWC3_VER_IS_WITHIN(DWC3, 210A, 250A))
  860. reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
  861. else
  862. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  863. break;
  864. case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
  865. /*
  866. * REVISIT Enabling this bit so that host-mode hibernation
  867. * will work. Device-mode hibernation is not yet implemented.
  868. */
  869. reg |= DWC3_GCTL_GBLHIBERNATIONEN;
  870. break;
  871. default:
  872. /* nothing */
  873. break;
  874. }
  875. /*
  876. * This is a workaround for STAR#4846132, which only affects
  877. * DWC_usb31 version2.00a operating in host mode.
  878. *
  879. * There is a problem in DWC_usb31 version 2.00a operating
  880. * in host mode that would cause a CSR read timeout When CSR
  881. * read coincides with RAM Clock Gating Entry. By disable
  882. * Clock Gating, sacrificing power consumption for normal
  883. * operation.
  884. */
  885. if (power_opt != DWC3_GHWPARAMS1_EN_PWROPT_NO &&
  886. hw_mode != DWC3_GHWPARAMS0_MODE_GADGET && DWC3_VER_IS(DWC31, 200A))
  887. reg |= DWC3_GCTL_DSBLCLKGTNG;
  888. /* check if current dwc3 is on simulation board */
  889. if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
  890. dev_info(dwc->dev, "Running with FPGA optimizations\n");
  891. dwc->is_fpga = true;
  892. }
  893. WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
  894. "disable_scramble cannot be used on non-FPGA builds\n");
  895. if (dwc->disable_scramble_quirk && dwc->is_fpga)
  896. reg |= DWC3_GCTL_DISSCRAMBLE;
  897. else
  898. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  899. if (dwc->u2exit_lfps_quirk)
  900. reg |= DWC3_GCTL_U2EXIT_LFPS;
  901. /*
  902. * WORKAROUND: DWC3 revisions <1.90a have a bug
  903. * where the device can fail to connect at SuperSpeed
  904. * and falls back to high-speed mode which causes
  905. * the device to enter a Connect/Disconnect loop
  906. */
  907. if (DWC3_VER_IS_PRIOR(DWC3, 190A))
  908. reg |= DWC3_GCTL_U2RSTECN;
  909. dwc3_writel(dwc, DWC3_GCTL, reg);
  910. }
  911. static int dwc3_core_get_phy(struct dwc3 *dwc);
  912. static int dwc3_core_ulpi_init(struct dwc3 *dwc);
  913. /* set global incr burst type configuration registers */
  914. static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
  915. {
  916. struct device *dev = dwc->dev;
  917. /* incrx_mode : for INCR burst type. */
  918. bool incrx_mode;
  919. /* incrx_size : for size of INCRX burst. */
  920. u32 incrx_size;
  921. u32 *vals;
  922. u32 cfg;
  923. int ntype;
  924. int ret;
  925. int i;
  926. cfg = dwc3_readl(dwc, DWC3_GSBUSCFG0);
  927. /*
  928. * Handle property "snps,incr-burst-type-adjustment".
  929. * Get the number of value from this property:
  930. * result <= 0, means this property is not supported.
  931. * result = 1, means INCRx burst mode supported.
  932. * result > 1, means undefined length burst mode supported.
  933. */
  934. ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment");
  935. if (ntype <= 0)
  936. return;
  937. vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
  938. if (!vals)
  939. return;
  940. /* Get INCR burst type, and parse it */
  941. ret = device_property_read_u32_array(dev,
  942. "snps,incr-burst-type-adjustment", vals, ntype);
  943. if (ret) {
  944. kfree(vals);
  945. dev_err(dev, "Error to get property\n");
  946. return;
  947. }
  948. incrx_size = *vals;
  949. if (ntype > 1) {
  950. /* INCRX (undefined length) burst mode */
  951. incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
  952. for (i = 1; i < ntype; i++) {
  953. if (vals[i] > incrx_size)
  954. incrx_size = vals[i];
  955. }
  956. } else {
  957. /* INCRX burst mode */
  958. incrx_mode = INCRX_BURST_MODE;
  959. }
  960. kfree(vals);
  961. /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
  962. cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
  963. if (incrx_mode)
  964. cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
  965. switch (incrx_size) {
  966. case 256:
  967. cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
  968. break;
  969. case 128:
  970. cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
  971. break;
  972. case 64:
  973. cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
  974. break;
  975. case 32:
  976. cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
  977. break;
  978. case 16:
  979. cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
  980. break;
  981. case 8:
  982. cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
  983. break;
  984. case 4:
  985. cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
  986. break;
  987. case 1:
  988. break;
  989. default:
  990. dev_err(dev, "Invalid property\n");
  991. break;
  992. }
  993. dwc3_writel(dwc, DWC3_GSBUSCFG0, cfg);
  994. }
  995. static void dwc3_set_power_down_clk_scale(struct dwc3 *dwc)
  996. {
  997. u32 scale;
  998. u32 reg;
  999. if (!dwc->susp_clk)
  1000. return;
  1001. /*
  1002. * The power down scale field specifies how many suspend_clk
  1003. * periods fit into a 16KHz clock period. When performing
  1004. * the division, round up the remainder.
  1005. *
  1006. * The power down scale value is calculated using the fastest
  1007. * frequency of the suspend_clk. If it isn't fixed (but within
  1008. * the accuracy requirement), the driver may not know the max
  1009. * rate of the suspend_clk, so only update the power down scale
  1010. * if the default is less than the calculated value from
  1011. * clk_get_rate() or if the default is questionably high
  1012. * (3x or more) to be within the requirement.
  1013. */
  1014. scale = DIV_ROUND_UP(clk_get_rate(dwc->susp_clk), 16000);
  1015. reg = dwc3_readl(dwc, DWC3_GCTL);
  1016. if ((reg & DWC3_GCTL_PWRDNSCALE_MASK) < DWC3_GCTL_PWRDNSCALE(scale) ||
  1017. (reg & DWC3_GCTL_PWRDNSCALE_MASK) > DWC3_GCTL_PWRDNSCALE(scale*3)) {
  1018. reg &= ~(DWC3_GCTL_PWRDNSCALE_MASK);
  1019. reg |= DWC3_GCTL_PWRDNSCALE(scale);
  1020. dwc3_writel(dwc, DWC3_GCTL, reg);
  1021. }
  1022. }
  1023. static void dwc3_config_threshold(struct dwc3 *dwc)
  1024. {
  1025. u32 reg;
  1026. u8 rx_thr_num;
  1027. u8 rx_maxburst;
  1028. u8 tx_thr_num;
  1029. u8 tx_maxburst;
  1030. /*
  1031. * Must config both number of packets and max burst settings to enable
  1032. * RX and/or TX threshold.
  1033. */
  1034. if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) {
  1035. rx_thr_num = dwc->rx_thr_num_pkt_prd;
  1036. rx_maxburst = dwc->rx_max_burst_prd;
  1037. tx_thr_num = dwc->tx_thr_num_pkt_prd;
  1038. tx_maxburst = dwc->tx_max_burst_prd;
  1039. if (rx_thr_num && rx_maxburst) {
  1040. reg = dwc3_readl(dwc, DWC3_GRXTHRCFG);
  1041. reg |= DWC31_RXTHRNUMPKTSEL_PRD;
  1042. reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
  1043. reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
  1044. reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
  1045. reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
  1046. dwc3_writel(dwc, DWC3_GRXTHRCFG, reg);
  1047. }
  1048. if (tx_thr_num && tx_maxburst) {
  1049. reg = dwc3_readl(dwc, DWC3_GTXTHRCFG);
  1050. reg |= DWC31_TXTHRNUMPKTSEL_PRD;
  1051. reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
  1052. reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
  1053. reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
  1054. reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
  1055. dwc3_writel(dwc, DWC3_GTXTHRCFG, reg);
  1056. }
  1057. }
  1058. rx_thr_num = dwc->rx_thr_num_pkt;
  1059. rx_maxburst = dwc->rx_max_burst;
  1060. tx_thr_num = dwc->tx_thr_num_pkt;
  1061. tx_maxburst = dwc->tx_max_burst;
  1062. if (DWC3_IP_IS(DWC3)) {
  1063. if (rx_thr_num && rx_maxburst) {
  1064. reg = dwc3_readl(dwc, DWC3_GRXTHRCFG);
  1065. reg |= DWC3_GRXTHRCFG_PKTCNTSEL;
  1066. reg &= ~DWC3_GRXTHRCFG_RXPKTCNT(~0);
  1067. reg |= DWC3_GRXTHRCFG_RXPKTCNT(rx_thr_num);
  1068. reg &= ~DWC3_GRXTHRCFG_MAXRXBURSTSIZE(~0);
  1069. reg |= DWC3_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst);
  1070. dwc3_writel(dwc, DWC3_GRXTHRCFG, reg);
  1071. }
  1072. if (tx_thr_num && tx_maxburst) {
  1073. reg = dwc3_readl(dwc, DWC3_GTXTHRCFG);
  1074. reg |= DWC3_GTXTHRCFG_PKTCNTSEL;
  1075. reg &= ~DWC3_GTXTHRCFG_TXPKTCNT(~0);
  1076. reg |= DWC3_GTXTHRCFG_TXPKTCNT(tx_thr_num);
  1077. reg &= ~DWC3_GTXTHRCFG_MAXTXBURSTSIZE(~0);
  1078. reg |= DWC3_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst);
  1079. dwc3_writel(dwc, DWC3_GTXTHRCFG, reg);
  1080. }
  1081. } else {
  1082. if (rx_thr_num && rx_maxburst) {
  1083. reg = dwc3_readl(dwc, DWC3_GRXTHRCFG);
  1084. reg |= DWC31_GRXTHRCFG_PKTCNTSEL;
  1085. reg &= ~DWC31_GRXTHRCFG_RXPKTCNT(~0);
  1086. reg |= DWC31_GRXTHRCFG_RXPKTCNT(rx_thr_num);
  1087. reg &= ~DWC31_GRXTHRCFG_MAXRXBURSTSIZE(~0);
  1088. reg |= DWC31_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst);
  1089. dwc3_writel(dwc, DWC3_GRXTHRCFG, reg);
  1090. }
  1091. if (tx_thr_num && tx_maxburst) {
  1092. reg = dwc3_readl(dwc, DWC3_GTXTHRCFG);
  1093. reg |= DWC31_GTXTHRCFG_PKTCNTSEL;
  1094. reg &= ~DWC31_GTXTHRCFG_TXPKTCNT(~0);
  1095. reg |= DWC31_GTXTHRCFG_TXPKTCNT(tx_thr_num);
  1096. reg &= ~DWC31_GTXTHRCFG_MAXTXBURSTSIZE(~0);
  1097. reg |= DWC31_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst);
  1098. dwc3_writel(dwc, DWC3_GTXTHRCFG, reg);
  1099. }
  1100. }
  1101. }
  1102. /**
  1103. * dwc3_core_init - Low-level initialization of DWC3 Core
  1104. * @dwc: Pointer to our controller context structure
  1105. *
  1106. * Returns 0 on success otherwise negative errno.
  1107. */
  1108. int dwc3_core_init(struct dwc3 *dwc)
  1109. {
  1110. unsigned int hw_mode;
  1111. u32 reg;
  1112. int ret;
  1113. hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
  1114. /*
  1115. * Write Linux Version Code to our GUID register so it's easy to figure
  1116. * out which kernel version a bug was found.
  1117. */
  1118. dwc3_writel(dwc, DWC3_GUID, LINUX_VERSION_CODE);
  1119. ret = dwc3_phy_setup(dwc);
  1120. if (ret)
  1121. return ret;
  1122. if (!dwc->ulpi_ready) {
  1123. ret = dwc3_core_ulpi_init(dwc);
  1124. if (ret) {
  1125. if (ret == -ETIMEDOUT) {
  1126. dwc3_core_soft_reset(dwc);
  1127. ret = -EPROBE_DEFER;
  1128. }
  1129. return ret;
  1130. }
  1131. dwc->ulpi_ready = true;
  1132. }
  1133. if (!dwc->phys_ready) {
  1134. ret = dwc3_core_get_phy(dwc);
  1135. if (ret)
  1136. goto err_exit_ulpi;
  1137. dwc->phys_ready = true;
  1138. }
  1139. ret = dwc3_phy_init(dwc);
  1140. if (ret)
  1141. goto err_exit_ulpi;
  1142. ret = dwc3_core_soft_reset(dwc);
  1143. if (ret)
  1144. goto err_exit_phy;
  1145. dwc3_core_setup_global_control(dwc);
  1146. dwc3_core_num_eps(dwc);
  1147. /* Set power down scale of suspend_clk */
  1148. dwc3_set_power_down_clk_scale(dwc);
  1149. /* Adjust Frame Length */
  1150. dwc3_frame_length_adjustment(dwc);
  1151. /* Adjust Reference Clock Period */
  1152. dwc3_ref_clk_period(dwc);
  1153. dwc3_set_incr_burst_type(dwc);
  1154. dwc3_config_soc_bus(dwc);
  1155. ret = dwc3_phy_power_on(dwc);
  1156. if (ret)
  1157. goto err_exit_phy;
  1158. ret = dwc3_event_buffers_setup(dwc);
  1159. if (ret) {
  1160. dev_err(dwc->dev, "failed to setup event buffers\n");
  1161. goto err_power_off_phy;
  1162. }
  1163. /*
  1164. * ENDXFER polling is available on version 3.10a and later of
  1165. * the DWC_usb3 controller. It is NOT available in the
  1166. * DWC_usb31 controller.
  1167. */
  1168. if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) {
  1169. reg = dwc3_readl(dwc, DWC3_GUCTL2);
  1170. reg |= DWC3_GUCTL2_RST_ACTBITLATER;
  1171. dwc3_writel(dwc, DWC3_GUCTL2, reg);
  1172. }
  1173. /*
  1174. * STAR 9001285599: This issue affects DWC_usb3 version 3.20a
  1175. * only. If the PM TIMER ECM is enabled through GUCTL2[19], the
  1176. * link compliance test (TD7.21) may fail. If the ECN is not
  1177. * enabled (GUCTL2[19] = 0), the controller will use the old timer
  1178. * value (5us), which is still acceptable for the link compliance
  1179. * test. Therefore, do not enable PM TIMER ECM in 3.20a by
  1180. * setting GUCTL2[19] by default; instead, use GUCTL2[19] = 0.
  1181. */
  1182. if (DWC3_VER_IS(DWC3, 320A)) {
  1183. reg = dwc3_readl(dwc, DWC3_GUCTL2);
  1184. reg &= ~DWC3_GUCTL2_LC_TIMER;
  1185. dwc3_writel(dwc, DWC3_GUCTL2, reg);
  1186. }
  1187. /*
  1188. * When configured in HOST mode, after issuing U3/L2 exit controller
  1189. * fails to send proper CRC checksum in CRC5 field. Because of this
  1190. * behaviour Transaction Error is generated, resulting in reset and
  1191. * re-enumeration of usb device attached. All the termsel, xcvrsel,
  1192. * opmode becomes 0 during end of resume. Enabling bit 10 of GUCTL1
  1193. * will correct this problem. This option is to support certain
  1194. * legacy ULPI PHYs.
  1195. */
  1196. if (dwc->resume_hs_terminations) {
  1197. reg = dwc3_readl(dwc, DWC3_GUCTL1);
  1198. reg |= DWC3_GUCTL1_RESUME_OPMODE_HS_HOST;
  1199. dwc3_writel(dwc, DWC3_GUCTL1, reg);
  1200. }
  1201. if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) {
  1202. reg = dwc3_readl(dwc, DWC3_GUCTL1);
  1203. /*
  1204. * Enable hardware control of sending remote wakeup
  1205. * in HS when the device is in the L1 state.
  1206. */
  1207. if (!DWC3_VER_IS_PRIOR(DWC3, 290A))
  1208. reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
  1209. /*
  1210. * Decouple USB 2.0 L1 & L2 events which will allow for
  1211. * gadget driver to only receive U3/L2 suspend & wakeup
  1212. * events and prevent the more frequent L1 LPM transitions
  1213. * from interrupting the driver.
  1214. */
  1215. if (!DWC3_VER_IS_PRIOR(DWC3, 300A))
  1216. reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT;
  1217. if (dwc->dis_tx_ipgap_linecheck_quirk)
  1218. reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
  1219. if (dwc->parkmode_disable_ss_quirk)
  1220. reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS;
  1221. if (dwc->parkmode_disable_hs_quirk)
  1222. reg |= DWC3_GUCTL1_PARKMODE_DISABLE_HS;
  1223. if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY)) {
  1224. if (dwc->maximum_speed == USB_SPEED_FULL ||
  1225. dwc->maximum_speed == USB_SPEED_HIGH)
  1226. reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
  1227. else
  1228. reg &= ~DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK;
  1229. }
  1230. dwc3_writel(dwc, DWC3_GUCTL1, reg);
  1231. }
  1232. dwc3_config_threshold(dwc);
  1233. if (hw_mode != DWC3_GHWPARAMS0_MODE_GADGET &&
  1234. (DWC3_IP_IS(DWC31)) &&
  1235. dwc->maximum_speed == USB_SPEED_SUPER) {
  1236. int i;
  1237. for (i = 0; i < dwc->num_usb3_ports; i++) {
  1238. reg = dwc3_readl(dwc, DWC3_LLUCTL(i));
  1239. reg |= DWC3_LLUCTL_FORCE_GEN1;
  1240. dwc3_writel(dwc, DWC3_LLUCTL(i), reg);
  1241. }
  1242. }
  1243. /*
  1244. * STAR 9001346572: This issue affects DWC_usb31 versions 1.80a and
  1245. * prior. When an active endpoint not currently cached in the host
  1246. * controller is chosen to be cached to the same index as an endpoint
  1247. * receiving NAKs, the endpoint receiving NAKs enters continuous
  1248. * retry mode. This prevents it from being evicted from the host
  1249. * controller cache, blocking the new endpoint from being cached and
  1250. * serviced.
  1251. *
  1252. * To resolve this, for controller versions 1.70a and 1.80a, set the
  1253. * GUCTL3 bit[16] (USB2.0 Internal Retry Disable) to 1. This bit
  1254. * disables the USB2.0 internal retry feature. The GUCTL3[16] register
  1255. * function is available only from version 1.70a.
  1256. */
  1257. if (DWC3_VER_IS_WITHIN(DWC31, 170A, 180A)) {
  1258. reg = dwc3_readl(dwc, DWC3_GUCTL3);
  1259. reg |= DWC3_GUCTL3_USB20_RETRY_DISABLE;
  1260. dwc3_writel(dwc, DWC3_GUCTL3, reg);
  1261. }
  1262. return 0;
  1263. err_power_off_phy:
  1264. dwc3_phy_power_off(dwc);
  1265. err_exit_phy:
  1266. dwc3_phy_exit(dwc);
  1267. err_exit_ulpi:
  1268. dwc3_ulpi_exit(dwc);
  1269. return ret;
  1270. }
  1271. EXPORT_SYMBOL_GPL(dwc3_core_init);
  1272. static int dwc3_core_get_phy(struct dwc3 *dwc)
  1273. {
  1274. struct device *dev = dwc->dev;
  1275. struct device_node *node = dev->of_node;
  1276. char phy_name[9];
  1277. int ret;
  1278. u8 i;
  1279. if (node) {
  1280. dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  1281. dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
  1282. } else {
  1283. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  1284. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  1285. }
  1286. if (IS_ERR(dwc->usb2_phy)) {
  1287. ret = PTR_ERR(dwc->usb2_phy);
  1288. if (ret == -ENXIO || ret == -ENODEV)
  1289. dwc->usb2_phy = NULL;
  1290. else
  1291. return dev_err_probe(dev, ret, "no usb2 phy configured\n");
  1292. }
  1293. if (IS_ERR(dwc->usb3_phy)) {
  1294. ret = PTR_ERR(dwc->usb3_phy);
  1295. if (ret == -ENXIO || ret == -ENODEV)
  1296. dwc->usb3_phy = NULL;
  1297. else
  1298. return dev_err_probe(dev, ret, "no usb3 phy configured\n");
  1299. }
  1300. for (i = 0; i < dwc->num_usb2_ports; i++) {
  1301. if (dwc->num_usb2_ports == 1)
  1302. snprintf(phy_name, sizeof(phy_name), "usb2-phy");
  1303. else
  1304. snprintf(phy_name, sizeof(phy_name), "usb2-%u", i);
  1305. dwc->usb2_generic_phy[i] = devm_phy_get(dev, phy_name);
  1306. if (IS_ERR(dwc->usb2_generic_phy[i])) {
  1307. ret = PTR_ERR(dwc->usb2_generic_phy[i]);
  1308. if (ret == -ENOSYS || ret == -ENODEV)
  1309. dwc->usb2_generic_phy[i] = NULL;
  1310. else
  1311. return dev_err_probe(dev, ret, "failed to lookup phy %s\n",
  1312. phy_name);
  1313. }
  1314. }
  1315. for (i = 0; i < dwc->num_usb3_ports; i++) {
  1316. if (dwc->num_usb3_ports == 1)
  1317. snprintf(phy_name, sizeof(phy_name), "usb3-phy");
  1318. else
  1319. snprintf(phy_name, sizeof(phy_name), "usb3-%u", i);
  1320. dwc->usb3_generic_phy[i] = devm_phy_get(dev, phy_name);
  1321. if (IS_ERR(dwc->usb3_generic_phy[i])) {
  1322. ret = PTR_ERR(dwc->usb3_generic_phy[i]);
  1323. if (ret == -ENOSYS || ret == -ENODEV)
  1324. dwc->usb3_generic_phy[i] = NULL;
  1325. else
  1326. return dev_err_probe(dev, ret, "failed to lookup phy %s\n",
  1327. phy_name);
  1328. }
  1329. }
  1330. return 0;
  1331. }
  1332. static int dwc3_core_init_mode(struct dwc3 *dwc)
  1333. {
  1334. struct device *dev = dwc->dev;
  1335. int ret;
  1336. int i;
  1337. switch (dwc->dr_mode) {
  1338. case USB_DR_MODE_PERIPHERAL:
  1339. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE, false);
  1340. if (dwc->usb2_phy)
  1341. otg_set_vbus(dwc->usb2_phy->otg, false);
  1342. phy_set_mode(dwc->usb2_generic_phy[0], PHY_MODE_USB_DEVICE);
  1343. phy_set_mode(dwc->usb3_generic_phy[0], PHY_MODE_USB_DEVICE);
  1344. ret = dwc3_gadget_init(dwc);
  1345. if (ret)
  1346. return dev_err_probe(dev, ret, "failed to initialize gadget\n");
  1347. break;
  1348. case USB_DR_MODE_HOST:
  1349. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST, false);
  1350. if (dwc->usb2_phy)
  1351. otg_set_vbus(dwc->usb2_phy->otg, true);
  1352. for (i = 0; i < dwc->num_usb2_ports; i++)
  1353. phy_set_mode(dwc->usb2_generic_phy[i], PHY_MODE_USB_HOST);
  1354. for (i = 0; i < dwc->num_usb3_ports; i++)
  1355. phy_set_mode(dwc->usb3_generic_phy[i], PHY_MODE_USB_HOST);
  1356. ret = dwc3_host_init(dwc);
  1357. if (ret)
  1358. return dev_err_probe(dev, ret, "failed to initialize host\n");
  1359. break;
  1360. case USB_DR_MODE_OTG:
  1361. INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
  1362. ret = dwc3_drd_init(dwc);
  1363. if (ret)
  1364. return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
  1365. break;
  1366. default:
  1367. dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
  1368. return -EINVAL;
  1369. }
  1370. return 0;
  1371. }
  1372. static void dwc3_core_exit_mode(struct dwc3 *dwc)
  1373. {
  1374. switch (dwc->dr_mode) {
  1375. case USB_DR_MODE_PERIPHERAL:
  1376. dwc3_gadget_exit(dwc);
  1377. break;
  1378. case USB_DR_MODE_HOST:
  1379. dwc3_host_exit(dwc);
  1380. break;
  1381. case USB_DR_MODE_OTG:
  1382. dwc3_drd_exit(dwc);
  1383. break;
  1384. default:
  1385. /* do nothing */
  1386. break;
  1387. }
  1388. /* de-assert DRVVBUS for HOST and OTG mode */
  1389. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE, true);
  1390. }
  1391. static void dwc3_get_software_properties(struct dwc3 *dwc,
  1392. const struct dwc3_properties *properties)
  1393. {
  1394. struct device *tmpdev;
  1395. u16 gsbuscfg0_reqinfo;
  1396. int ret;
  1397. dwc->gsbuscfg0_reqinfo = DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED;
  1398. if (properties->gsbuscfg0_reqinfo !=
  1399. DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED) {
  1400. dwc->gsbuscfg0_reqinfo = properties->gsbuscfg0_reqinfo;
  1401. return;
  1402. }
  1403. /*
  1404. * Iterate over all parent nodes for finding swnode properties
  1405. * and non-DT (non-ABI) properties.
  1406. */
  1407. for (tmpdev = dwc->dev; tmpdev; tmpdev = tmpdev->parent) {
  1408. ret = device_property_read_u16(tmpdev,
  1409. "snps,gsbuscfg0-reqinfo",
  1410. &gsbuscfg0_reqinfo);
  1411. if (!ret)
  1412. dwc->gsbuscfg0_reqinfo = gsbuscfg0_reqinfo;
  1413. }
  1414. }
  1415. static void dwc3_get_properties(struct dwc3 *dwc)
  1416. {
  1417. struct device *dev = dwc->dev;
  1418. u8 lpm_nyet_threshold;
  1419. u8 tx_de_emphasis;
  1420. u8 hird_threshold;
  1421. u8 rx_thr_num_pkt = 0;
  1422. u8 rx_max_burst = 0;
  1423. u8 tx_thr_num_pkt = 0;
  1424. u8 tx_max_burst = 0;
  1425. u8 rx_thr_num_pkt_prd = 0;
  1426. u8 rx_max_burst_prd = 0;
  1427. u8 tx_thr_num_pkt_prd = 0;
  1428. u8 tx_max_burst_prd = 0;
  1429. u8 tx_fifo_resize_max_num;
  1430. u16 num_hc_interrupters;
  1431. /* default to highest possible threshold */
  1432. lpm_nyet_threshold = 0xf;
  1433. /* default to -3.5dB de-emphasis */
  1434. tx_de_emphasis = 1;
  1435. /*
  1436. * default to assert utmi_sleep_n and use maximum allowed HIRD
  1437. * threshold value of 0b1100
  1438. */
  1439. hird_threshold = 12;
  1440. /*
  1441. * default to a TXFIFO size large enough to fit 6 max packets. This
  1442. * allows for systems with larger bus latencies to have some headroom
  1443. * for endpoints that have a large bMaxBurst value.
  1444. */
  1445. tx_fifo_resize_max_num = 6;
  1446. /* default to a single XHCI interrupter */
  1447. num_hc_interrupters = 1;
  1448. dwc->maximum_speed = usb_get_maximum_speed(dev);
  1449. dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev);
  1450. dwc->dr_mode = usb_get_dr_mode(dev);
  1451. dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
  1452. dwc->sysdev_is_parent = device_property_read_bool(dev,
  1453. "linux,sysdev_is_parent");
  1454. if (dwc->sysdev_is_parent)
  1455. dwc->sysdev = dwc->dev->parent;
  1456. else
  1457. dwc->sysdev = dwc->dev;
  1458. dwc->sys_wakeup = device_may_wakeup(dwc->sysdev);
  1459. dwc->has_lpm_erratum = device_property_read_bool(dev,
  1460. "snps,has-lpm-erratum");
  1461. device_property_read_u8(dev, "snps,lpm-nyet-threshold",
  1462. &lpm_nyet_threshold);
  1463. dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
  1464. "snps,is-utmi-l1-suspend");
  1465. device_property_read_u8(dev, "snps,hird-threshold",
  1466. &hird_threshold);
  1467. dwc->dis_start_transfer_quirk = device_property_read_bool(dev,
  1468. "snps,dis-start-transfer-quirk");
  1469. dwc->usb3_lpm_capable = device_property_read_bool(dev,
  1470. "snps,usb3_lpm_capable");
  1471. dwc->usb2_lpm_disable = device_property_read_bool(dev,
  1472. "snps,usb2-lpm-disable");
  1473. dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev,
  1474. "snps,usb2-gadget-lpm-disable");
  1475. device_property_read_u8(dev, "snps,rx-thr-num-pkt",
  1476. &rx_thr_num_pkt);
  1477. device_property_read_u8(dev, "snps,rx-max-burst",
  1478. &rx_max_burst);
  1479. device_property_read_u8(dev, "snps,tx-thr-num-pkt",
  1480. &tx_thr_num_pkt);
  1481. device_property_read_u8(dev, "snps,tx-max-burst",
  1482. &tx_max_burst);
  1483. device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
  1484. &rx_thr_num_pkt_prd);
  1485. device_property_read_u8(dev, "snps,rx-max-burst-prd",
  1486. &rx_max_burst_prd);
  1487. device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
  1488. &tx_thr_num_pkt_prd);
  1489. device_property_read_u8(dev, "snps,tx-max-burst-prd",
  1490. &tx_max_burst_prd);
  1491. device_property_read_u16(dev, "num-hc-interrupters",
  1492. &num_hc_interrupters);
  1493. /* DWC3 core allowed to have a max of 8 interrupters */
  1494. if (num_hc_interrupters > 8)
  1495. num_hc_interrupters = 8;
  1496. dwc->do_fifo_resize = device_property_read_bool(dev,
  1497. "tx-fifo-resize");
  1498. if (dwc->do_fifo_resize)
  1499. device_property_read_u8(dev, "tx-fifo-max-num",
  1500. &tx_fifo_resize_max_num);
  1501. dwc->disable_scramble_quirk = device_property_read_bool(dev,
  1502. "snps,disable_scramble_quirk");
  1503. dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
  1504. "snps,u2exit_lfps_quirk");
  1505. dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
  1506. "snps,u2ss_inp3_quirk");
  1507. dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
  1508. "snps,req_p1p2p3_quirk");
  1509. dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
  1510. "snps,del_p1p2p3_quirk");
  1511. dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
  1512. "snps,del_phy_power_chg_quirk");
  1513. dwc->lfps_filter_quirk = device_property_read_bool(dev,
  1514. "snps,lfps_filter_quirk");
  1515. dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
  1516. "snps,rx_detect_poll_quirk");
  1517. dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
  1518. "snps,dis_u3_susphy_quirk");
  1519. dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
  1520. "snps,dis_u2_susphy_quirk");
  1521. dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
  1522. "snps,dis_enblslpm_quirk");
  1523. dwc->dis_u1_entry_quirk = device_property_read_bool(dev,
  1524. "snps,dis-u1-entry-quirk");
  1525. dwc->dis_u2_entry_quirk = device_property_read_bool(dev,
  1526. "snps,dis-u2-entry-quirk");
  1527. dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
  1528. "snps,dis_rxdet_inp3_quirk");
  1529. dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
  1530. "snps,dis-u2-freeclk-exists-quirk");
  1531. dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
  1532. "snps,dis-del-phy-power-chg-quirk");
  1533. dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
  1534. "snps,dis-tx-ipgap-linecheck-quirk");
  1535. dwc->resume_hs_terminations = device_property_read_bool(dev,
  1536. "snps,resume-hs-terminations");
  1537. dwc->ulpi_ext_vbus_drv = device_property_read_bool(dev,
  1538. "snps,ulpi-ext-vbus-drv");
  1539. dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev,
  1540. "snps,parkmode-disable-ss-quirk");
  1541. dwc->parkmode_disable_hs_quirk = device_property_read_bool(dev,
  1542. "snps,parkmode-disable-hs-quirk");
  1543. dwc->gfladj_refclk_lpm_sel = device_property_read_bool(dev,
  1544. "snps,gfladj-refclk-lpm-sel-quirk");
  1545. dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
  1546. "snps,tx_de_emphasis_quirk");
  1547. device_property_read_u8(dev, "snps,tx_de_emphasis",
  1548. &tx_de_emphasis);
  1549. device_property_read_string(dev, "snps,hsphy_interface",
  1550. &dwc->hsphy_interface);
  1551. device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
  1552. &dwc->fladj);
  1553. device_property_read_u32(dev, "snps,ref-clock-period-ns",
  1554. &dwc->ref_clk_per);
  1555. dwc->dis_metastability_quirk = device_property_read_bool(dev,
  1556. "snps,dis_metastability_quirk");
  1557. dwc->dis_split_quirk = device_property_read_bool(dev,
  1558. "snps,dis-split-quirk");
  1559. dwc->lpm_nyet_threshold = lpm_nyet_threshold;
  1560. dwc->tx_de_emphasis = tx_de_emphasis;
  1561. dwc->hird_threshold = hird_threshold;
  1562. dwc->rx_thr_num_pkt = rx_thr_num_pkt;
  1563. dwc->rx_max_burst = rx_max_burst;
  1564. dwc->tx_thr_num_pkt = tx_thr_num_pkt;
  1565. dwc->tx_max_burst = tx_max_burst;
  1566. dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
  1567. dwc->rx_max_burst_prd = rx_max_burst_prd;
  1568. dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
  1569. dwc->tx_max_burst_prd = tx_max_burst_prd;
  1570. dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num;
  1571. dwc->num_hc_interrupters = num_hc_interrupters;
  1572. }
  1573. /* check whether the core supports IMOD */
  1574. bool dwc3_has_imod(struct dwc3 *dwc)
  1575. {
  1576. return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) ||
  1577. DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) ||
  1578. DWC3_IP_IS(DWC32);
  1579. }
  1580. static void dwc3_check_params(struct dwc3 *dwc)
  1581. {
  1582. struct device *dev = dwc->dev;
  1583. unsigned int hwparam_gen =
  1584. DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3);
  1585. /*
  1586. * Enable IMOD for all supporting controllers.
  1587. *
  1588. * Particularly, DWC_usb3 v3.00a must enable this feature for
  1589. * the following reason:
  1590. *
  1591. * Workaround for STAR 9000961433 which affects only version
  1592. * 3.00a of the DWC_usb3 core. This prevents the controller
  1593. * interrupt from being masked while handling events. IMOD
  1594. * allows us to work around this issue. Enable it for the
  1595. * affected version.
  1596. */
  1597. if (dwc3_has_imod((dwc)))
  1598. dwc->imod_interval = 1;
  1599. /* Check the maximum_speed parameter */
  1600. switch (dwc->maximum_speed) {
  1601. case USB_SPEED_FULL:
  1602. case USB_SPEED_HIGH:
  1603. break;
  1604. case USB_SPEED_SUPER:
  1605. if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS)
  1606. dev_warn(dev, "UDC doesn't support Gen 1\n");
  1607. break;
  1608. case USB_SPEED_SUPER_PLUS:
  1609. if ((DWC3_IP_IS(DWC32) &&
  1610. hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) ||
  1611. (!DWC3_IP_IS(DWC32) &&
  1612. hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
  1613. dev_warn(dev, "UDC doesn't support SSP\n");
  1614. break;
  1615. default:
  1616. dev_err(dev, "invalid maximum_speed parameter %d\n",
  1617. dwc->maximum_speed);
  1618. fallthrough;
  1619. case USB_SPEED_UNKNOWN:
  1620. switch (hwparam_gen) {
  1621. case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
  1622. dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
  1623. break;
  1624. case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
  1625. if (DWC3_IP_IS(DWC32))
  1626. dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
  1627. else
  1628. dwc->maximum_speed = USB_SPEED_SUPER;
  1629. break;
  1630. case DWC3_GHWPARAMS3_SSPHY_IFC_DIS:
  1631. dwc->maximum_speed = USB_SPEED_HIGH;
  1632. break;
  1633. default:
  1634. dwc->maximum_speed = USB_SPEED_SUPER;
  1635. break;
  1636. }
  1637. break;
  1638. }
  1639. /*
  1640. * Currently the controller does not have visibility into the HW
  1641. * parameter to determine the maximum number of lanes the HW supports.
  1642. * If the number of lanes is not specified in the device property, then
  1643. * set the default to support dual-lane for DWC_usb32 and single-lane
  1644. * for DWC_usb31 for super-speed-plus.
  1645. */
  1646. if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) {
  1647. switch (dwc->max_ssp_rate) {
  1648. case USB_SSP_GEN_2x1:
  1649. if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1)
  1650. dev_warn(dev, "UDC only supports Gen 1\n");
  1651. break;
  1652. case USB_SSP_GEN_1x2:
  1653. case USB_SSP_GEN_2x2:
  1654. if (DWC3_IP_IS(DWC31))
  1655. dev_warn(dev, "UDC only supports single lane\n");
  1656. break;
  1657. case USB_SSP_GEN_UNKNOWN:
  1658. default:
  1659. switch (hwparam_gen) {
  1660. case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2:
  1661. if (DWC3_IP_IS(DWC32))
  1662. dwc->max_ssp_rate = USB_SSP_GEN_2x2;
  1663. else
  1664. dwc->max_ssp_rate = USB_SSP_GEN_2x1;
  1665. break;
  1666. case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1:
  1667. if (DWC3_IP_IS(DWC32))
  1668. dwc->max_ssp_rate = USB_SSP_GEN_1x2;
  1669. break;
  1670. }
  1671. break;
  1672. }
  1673. }
  1674. }
  1675. static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
  1676. {
  1677. struct device *dev = dwc->dev;
  1678. struct device_node *np_phy;
  1679. struct extcon_dev *edev = NULL;
  1680. const char *name;
  1681. if (device_property_present(dev, "extcon"))
  1682. return extcon_get_edev_by_phandle(dev, 0);
  1683. /*
  1684. * Device tree platforms should get extcon via phandle.
  1685. * On ACPI platforms, we get the name from a device property.
  1686. * This device property is for kernel internal use only and
  1687. * is expected to be set by the glue code.
  1688. */
  1689. if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
  1690. return extcon_get_extcon_dev(name);
  1691. /*
  1692. * Check explicitly if "usb-role-switch" is used since
  1693. * extcon_find_edev_by_node() can not be used to check the absence of
  1694. * an extcon device. In the absence of an device it will always return
  1695. * EPROBE_DEFER.
  1696. */
  1697. if (IS_ENABLED(CONFIG_USB_ROLE_SWITCH) &&
  1698. device_property_read_bool(dev, "usb-role-switch"))
  1699. return NULL;
  1700. /*
  1701. * Try to get an extcon device from the USB PHY controller's "port"
  1702. * node. Check if it has the "port" node first, to avoid printing the
  1703. * error message from underlying code, as it's a valid case: extcon
  1704. * device (and "port" node) may be missing in case of "usb-role-switch"
  1705. * or OTG mode.
  1706. */
  1707. np_phy = of_parse_phandle(dev->of_node, "phys", 0);
  1708. if (of_graph_is_present(np_phy)) {
  1709. struct device_node *np_conn;
  1710. np_conn = of_graph_get_remote_node(np_phy, -1, -1);
  1711. if (np_conn)
  1712. edev = extcon_find_edev_by_node(np_conn);
  1713. of_node_put(np_conn);
  1714. }
  1715. of_node_put(np_phy);
  1716. return edev;
  1717. }
  1718. static int dwc3_get_clocks(struct dwc3 *dwc)
  1719. {
  1720. struct device *dev = dwc->dev;
  1721. if (!dev->of_node)
  1722. return 0;
  1723. /*
  1724. * Clocks are optional, but new DT platforms should support all clocks
  1725. * as required by the DT-binding.
  1726. * Some devices have different clock names in legacy device trees,
  1727. * check for them to retain backwards compatibility.
  1728. */
  1729. dwc->bus_clk = devm_clk_get_optional(dev, "bus_early");
  1730. if (IS_ERR(dwc->bus_clk)) {
  1731. return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
  1732. "could not get bus clock\n");
  1733. }
  1734. if (dwc->bus_clk == NULL) {
  1735. dwc->bus_clk = devm_clk_get_optional(dev, "bus_clk");
  1736. if (IS_ERR(dwc->bus_clk)) {
  1737. return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
  1738. "could not get bus clock\n");
  1739. }
  1740. }
  1741. dwc->ref_clk = devm_clk_get_optional(dev, "ref");
  1742. if (IS_ERR(dwc->ref_clk)) {
  1743. return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
  1744. "could not get ref clock\n");
  1745. }
  1746. if (dwc->ref_clk == NULL) {
  1747. dwc->ref_clk = devm_clk_get_optional(dev, "ref_clk");
  1748. if (IS_ERR(dwc->ref_clk)) {
  1749. return dev_err_probe(dev, PTR_ERR(dwc->ref_clk),
  1750. "could not get ref clock\n");
  1751. }
  1752. }
  1753. dwc->susp_clk = devm_clk_get_optional(dev, "suspend");
  1754. if (IS_ERR(dwc->susp_clk)) {
  1755. return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
  1756. "could not get suspend clock\n");
  1757. }
  1758. if (dwc->susp_clk == NULL) {
  1759. dwc->susp_clk = devm_clk_get_optional(dev, "suspend_clk");
  1760. if (IS_ERR(dwc->susp_clk)) {
  1761. return dev_err_probe(dev, PTR_ERR(dwc->susp_clk),
  1762. "could not get suspend clock\n");
  1763. }
  1764. }
  1765. /* specific to Rockchip RK3588 */
  1766. dwc->utmi_clk = devm_clk_get_optional(dev, "utmi");
  1767. if (IS_ERR(dwc->utmi_clk)) {
  1768. return dev_err_probe(dev, PTR_ERR(dwc->utmi_clk),
  1769. "could not get utmi clock\n");
  1770. }
  1771. /* specific to Rockchip RK3588 */
  1772. dwc->pipe_clk = devm_clk_get_optional(dev, "pipe");
  1773. if (IS_ERR(dwc->pipe_clk)) {
  1774. return dev_err_probe(dev, PTR_ERR(dwc->pipe_clk),
  1775. "could not get pipe clock\n");
  1776. }
  1777. return 0;
  1778. }
  1779. static int dwc3_get_num_ports(struct dwc3 *dwc)
  1780. {
  1781. void __iomem *base;
  1782. u8 major_revision;
  1783. u32 offset;
  1784. u32 val;
  1785. /*
  1786. * Remap xHCI address space to access XHCI ext cap regs since it is
  1787. * needed to get information on number of ports present.
  1788. */
  1789. base = ioremap(dwc->xhci_resources[0].start,
  1790. resource_size(&dwc->xhci_resources[0]));
  1791. if (!base)
  1792. return -ENOMEM;
  1793. offset = 0;
  1794. do {
  1795. offset = xhci_find_next_ext_cap(base, offset,
  1796. XHCI_EXT_CAPS_PROTOCOL);
  1797. if (!offset)
  1798. break;
  1799. val = readl(base + offset);
  1800. major_revision = XHCI_EXT_PORT_MAJOR(val);
  1801. val = readl(base + offset + 0x08);
  1802. if (major_revision == 0x03) {
  1803. dwc->num_usb3_ports += XHCI_EXT_PORT_COUNT(val);
  1804. } else if (major_revision <= 0x02) {
  1805. dwc->num_usb2_ports += XHCI_EXT_PORT_COUNT(val);
  1806. } else {
  1807. dev_warn(dwc->dev, "unrecognized port major revision %d\n",
  1808. major_revision);
  1809. }
  1810. } while (1);
  1811. dev_dbg(dwc->dev, "hs-ports: %u ss-ports: %u\n",
  1812. dwc->num_usb2_ports, dwc->num_usb3_ports);
  1813. iounmap(base);
  1814. if (dwc->num_usb2_ports > DWC3_USB2_MAX_PORTS ||
  1815. dwc->num_usb3_ports > DWC3_USB3_MAX_PORTS)
  1816. return -EINVAL;
  1817. return 0;
  1818. }
  1819. static void dwc3_vbus_draw_work(struct work_struct *work)
  1820. {
  1821. struct dwc3 *dwc = container_of(work, struct dwc3, vbus_draw_work);
  1822. union power_supply_propval val = {0};
  1823. int ret;
  1824. val.intval = 1000 * (dwc->current_limit);
  1825. ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
  1826. if (ret < 0)
  1827. dev_dbg(dwc->dev, "Error (%d) setting vbus draw (%d mA)\n",
  1828. ret, dwc->current_limit);
  1829. }
  1830. static struct power_supply *dwc3_get_usb_power_supply(struct dwc3 *dwc)
  1831. {
  1832. struct power_supply *usb_psy;
  1833. const char *usb_psy_name;
  1834. int ret;
  1835. ret = device_property_read_string(dwc->dev, "usb-psy-name", &usb_psy_name);
  1836. if (ret < 0)
  1837. return NULL;
  1838. usb_psy = power_supply_get_by_name(usb_psy_name);
  1839. if (!usb_psy)
  1840. return ERR_PTR(-EPROBE_DEFER);
  1841. INIT_WORK(&dwc->vbus_draw_work, dwc3_vbus_draw_work);
  1842. return usb_psy;
  1843. }
  1844. int dwc3_core_probe(const struct dwc3_probe_data *data)
  1845. {
  1846. struct dwc3 *dwc = data->dwc;
  1847. struct device *dev = dwc->dev;
  1848. struct resource dwc_res;
  1849. unsigned int hw_mode;
  1850. void __iomem *regs;
  1851. struct resource *res = data->res;
  1852. int ret;
  1853. dwc->xhci_resources[0].start = res->start;
  1854. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  1855. DWC3_XHCI_REGS_END;
  1856. dwc->xhci_resources[0].flags = res->flags;
  1857. dwc->xhci_resources[0].name = res->name;
  1858. /*
  1859. * Request memory region but exclude xHCI regs,
  1860. * since it will be requested by the xhci-plat driver.
  1861. */
  1862. dwc_res = *res;
  1863. dwc_res.start += DWC3_GLOBALS_REGS_START;
  1864. if (dev->of_node) {
  1865. struct device_node *parent = of_get_parent(dev->of_node);
  1866. if (of_device_is_compatible(parent, "realtek,rtd-dwc3")) {
  1867. dwc_res.start -= DWC3_GLOBALS_REGS_START;
  1868. dwc_res.start += DWC3_RTK_RTD_GLOBALS_REGS_START;
  1869. }
  1870. of_node_put(parent);
  1871. }
  1872. regs = devm_ioremap_resource(dev, &dwc_res);
  1873. if (IS_ERR(regs))
  1874. return PTR_ERR(regs);
  1875. dwc->regs = regs;
  1876. dwc->regs_size = resource_size(&dwc_res);
  1877. dwc3_get_properties(dwc);
  1878. dwc3_get_software_properties(dwc, &data->properties);
  1879. dwc->usb_psy = dwc3_get_usb_power_supply(dwc);
  1880. if (IS_ERR(dwc->usb_psy))
  1881. return dev_err_probe(dev, PTR_ERR(dwc->usb_psy), "couldn't get usb power supply\n");
  1882. if (!data->ignore_clocks_and_resets) {
  1883. dwc->reset = devm_reset_control_array_get_optional_shared(dev);
  1884. if (IS_ERR(dwc->reset)) {
  1885. ret = PTR_ERR(dwc->reset);
  1886. goto err_put_psy;
  1887. }
  1888. ret = dwc3_get_clocks(dwc);
  1889. if (ret)
  1890. goto err_put_psy;
  1891. }
  1892. ret = reset_control_deassert(dwc->reset);
  1893. if (ret)
  1894. goto err_put_psy;
  1895. ret = dwc3_clk_enable(dwc);
  1896. if (ret)
  1897. goto err_assert_reset;
  1898. if (!dwc3_core_is_valid(dwc)) {
  1899. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  1900. ret = -ENODEV;
  1901. goto err_disable_clks;
  1902. }
  1903. dev_set_drvdata(dev, dwc);
  1904. dwc3_cache_hwparams(dwc);
  1905. if (!dev_is_pci(dwc->sysdev) &&
  1906. DWC3_GHWPARAMS0_AWIDTH(dwc->hwparams.hwparams0) == 64) {
  1907. ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64));
  1908. if (ret)
  1909. goto err_disable_clks;
  1910. }
  1911. /*
  1912. * Currently only DWC3 controllers that are host-only capable
  1913. * can have more than one port.
  1914. */
  1915. hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
  1916. if (hw_mode == DWC3_GHWPARAMS0_MODE_HOST) {
  1917. ret = dwc3_get_num_ports(dwc);
  1918. if (ret)
  1919. goto err_disable_clks;
  1920. } else {
  1921. dwc->num_usb2_ports = 1;
  1922. dwc->num_usb3_ports = 1;
  1923. }
  1924. spin_lock_init(&dwc->lock);
  1925. mutex_init(&dwc->mutex);
  1926. pm_runtime_get_noresume(dev);
  1927. pm_runtime_set_active(dev);
  1928. pm_runtime_use_autosuspend(dev);
  1929. pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
  1930. pm_runtime_enable(dev);
  1931. pm_runtime_forbid(dev);
  1932. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  1933. if (ret) {
  1934. dev_err(dwc->dev, "failed to allocate event buffers\n");
  1935. ret = -ENOMEM;
  1936. goto err_allow_rpm;
  1937. }
  1938. dwc->edev = dwc3_get_extcon(dwc);
  1939. if (IS_ERR(dwc->edev)) {
  1940. ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n");
  1941. goto err_free_event_buffers;
  1942. }
  1943. ret = dwc3_get_dr_mode(dwc);
  1944. if (ret)
  1945. goto err_free_event_buffers;
  1946. ret = dwc3_core_init(dwc);
  1947. if (ret) {
  1948. dev_err_probe(dev, ret, "failed to initialize core\n");
  1949. goto err_free_event_buffers;
  1950. }
  1951. dwc3_check_params(dwc);
  1952. dwc3_debugfs_init(dwc);
  1953. if (!data->skip_core_init_mode) {
  1954. ret = dwc3_core_init_mode(dwc);
  1955. if (ret)
  1956. goto err_exit_debugfs;
  1957. }
  1958. pm_runtime_put(dev);
  1959. dma_set_max_seg_size(dev, UINT_MAX);
  1960. return 0;
  1961. err_exit_debugfs:
  1962. dwc3_debugfs_exit(dwc);
  1963. dwc3_event_buffers_cleanup(dwc);
  1964. dwc3_phy_power_off(dwc);
  1965. dwc3_phy_exit(dwc);
  1966. dwc3_ulpi_exit(dwc);
  1967. err_free_event_buffers:
  1968. dwc3_free_event_buffers(dwc);
  1969. err_allow_rpm:
  1970. pm_runtime_allow(dev);
  1971. pm_runtime_disable(dev);
  1972. pm_runtime_dont_use_autosuspend(dev);
  1973. pm_runtime_set_suspended(dev);
  1974. pm_runtime_put_noidle(dev);
  1975. err_disable_clks:
  1976. dwc3_clk_disable(dwc);
  1977. err_assert_reset:
  1978. reset_control_assert(dwc->reset);
  1979. err_put_psy:
  1980. if (dwc->usb_psy)
  1981. power_supply_put(dwc->usb_psy);
  1982. return ret;
  1983. }
  1984. EXPORT_SYMBOL_GPL(dwc3_core_probe);
  1985. static int dwc3_probe(struct platform_device *pdev)
  1986. {
  1987. struct dwc3_probe_data probe_data = {};
  1988. struct resource *res;
  1989. struct dwc3 *dwc;
  1990. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1991. if (!res) {
  1992. dev_err(&pdev->dev, "missing memory resource\n");
  1993. return -ENODEV;
  1994. }
  1995. dwc = devm_kzalloc(&pdev->dev, sizeof(*dwc), GFP_KERNEL);
  1996. if (!dwc)
  1997. return -ENOMEM;
  1998. dwc->dev = &pdev->dev;
  1999. dwc->glue_ops = NULL;
  2000. probe_data.dwc = dwc;
  2001. probe_data.res = res;
  2002. probe_data.properties = DWC3_DEFAULT_PROPERTIES;
  2003. return dwc3_core_probe(&probe_data);
  2004. }
  2005. void dwc3_core_remove(struct dwc3 *dwc)
  2006. {
  2007. pm_runtime_get_sync(dwc->dev);
  2008. dwc3_core_exit_mode(dwc);
  2009. dwc3_debugfs_exit(dwc);
  2010. dwc3_core_exit(dwc);
  2011. dwc3_ulpi_exit(dwc);
  2012. pm_runtime_allow(dwc->dev);
  2013. pm_runtime_disable(dwc->dev);
  2014. pm_runtime_dont_use_autosuspend(dwc->dev);
  2015. pm_runtime_put_noidle(dwc->dev);
  2016. /*
  2017. * HACK: Clear the driver data, which is currently accessed by parent
  2018. * glue drivers, before allowing the parent to suspend.
  2019. */
  2020. dev_set_drvdata(dwc->dev, NULL);
  2021. pm_runtime_set_suspended(dwc->dev);
  2022. dwc3_free_event_buffers(dwc);
  2023. if (dwc->usb_psy) {
  2024. cancel_work_sync(&dwc->vbus_draw_work);
  2025. power_supply_put(dwc->usb_psy);
  2026. }
  2027. }
  2028. EXPORT_SYMBOL_GPL(dwc3_core_remove);
  2029. static void dwc3_remove(struct platform_device *pdev)
  2030. {
  2031. dwc3_core_remove(platform_get_drvdata(pdev));
  2032. }
  2033. #ifdef CONFIG_PM
  2034. static int dwc3_core_init_for_resume(struct dwc3 *dwc)
  2035. {
  2036. int ret;
  2037. ret = reset_control_deassert(dwc->reset);
  2038. if (ret)
  2039. return ret;
  2040. ret = dwc3_clk_enable(dwc);
  2041. if (ret)
  2042. goto assert_reset;
  2043. ret = dwc3_core_init(dwc);
  2044. if (ret)
  2045. goto disable_clks;
  2046. return 0;
  2047. disable_clks:
  2048. dwc3_clk_disable(dwc);
  2049. assert_reset:
  2050. reset_control_assert(dwc->reset);
  2051. return ret;
  2052. }
  2053. static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
  2054. {
  2055. u32 reg;
  2056. int i;
  2057. int ret;
  2058. if (!pm_runtime_suspended(dwc->dev) && !PMSG_IS_AUTO(msg)) {
  2059. dwc->susphy_state = (dwc3_readl(dwc, DWC3_GUSB2PHYCFG(0)) &
  2060. DWC3_GUSB2PHYCFG_SUSPHY) ||
  2061. (dwc3_readl(dwc, DWC3_GUSB3PIPECTL(0)) &
  2062. DWC3_GUSB3PIPECTL_SUSPHY);
  2063. /*
  2064. * TI AM62 platform requires SUSPHY to be
  2065. * enabled for system suspend to work.
  2066. */
  2067. if (!dwc->susphy_state)
  2068. dwc3_enable_susphy(dwc, true);
  2069. }
  2070. switch (dwc->current_dr_role) {
  2071. case DWC3_GCTL_PRTCAP_DEVICE:
  2072. if (pm_runtime_suspended(dwc->dev))
  2073. break;
  2074. ret = dwc3_gadget_suspend(dwc);
  2075. if (ret)
  2076. return ret;
  2077. synchronize_irq(dwc->irq_gadget);
  2078. dwc3_core_exit(dwc);
  2079. break;
  2080. case DWC3_GCTL_PRTCAP_HOST:
  2081. if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
  2082. dwc3_core_exit(dwc);
  2083. break;
  2084. }
  2085. /* Let controller to suspend HSPHY before PHY driver suspends */
  2086. if (dwc->dis_u2_susphy_quirk ||
  2087. dwc->dis_enblslpm_quirk) {
  2088. for (i = 0; i < dwc->num_usb2_ports; i++) {
  2089. reg = dwc3_readl(dwc, DWC3_GUSB2PHYCFG(i));
  2090. reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
  2091. DWC3_GUSB2PHYCFG_SUSPHY;
  2092. dwc3_writel(dwc, DWC3_GUSB2PHYCFG(i), reg);
  2093. }
  2094. /* Give some time for USB2 PHY to suspend */
  2095. usleep_range(5000, 6000);
  2096. }
  2097. for (i = 0; i < dwc->num_usb2_ports; i++)
  2098. phy_pm_runtime_put_sync(dwc->usb2_generic_phy[i]);
  2099. for (i = 0; i < dwc->num_usb3_ports; i++)
  2100. phy_pm_runtime_put_sync(dwc->usb3_generic_phy[i]);
  2101. break;
  2102. case DWC3_GCTL_PRTCAP_OTG:
  2103. /* do nothing during runtime_suspend */
  2104. if (PMSG_IS_AUTO(msg))
  2105. break;
  2106. if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  2107. ret = dwc3_gadget_suspend(dwc);
  2108. if (ret)
  2109. return ret;
  2110. synchronize_irq(dwc->irq_gadget);
  2111. }
  2112. dwc3_otg_exit(dwc);
  2113. dwc3_core_exit(dwc);
  2114. break;
  2115. default:
  2116. /* do nothing */
  2117. break;
  2118. }
  2119. return 0;
  2120. }
  2121. static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
  2122. {
  2123. int ret;
  2124. u32 reg;
  2125. int i;
  2126. switch (dwc->current_dr_role) {
  2127. case DWC3_GCTL_PRTCAP_DEVICE:
  2128. ret = dwc3_core_init_for_resume(dwc);
  2129. if (ret)
  2130. return ret;
  2131. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE, true);
  2132. dwc3_gadget_resume(dwc);
  2133. break;
  2134. case DWC3_GCTL_PRTCAP_HOST:
  2135. if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) {
  2136. ret = dwc3_core_init_for_resume(dwc);
  2137. if (ret)
  2138. return ret;
  2139. dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST, true);
  2140. break;
  2141. }
  2142. /* Restore GUSB2PHYCFG bits that were modified in suspend */
  2143. for (i = 0; i < dwc->num_usb2_ports; i++) {
  2144. reg = dwc3_readl(dwc, DWC3_GUSB2PHYCFG(i));
  2145. if (dwc->dis_u2_susphy_quirk)
  2146. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  2147. if (dwc->dis_enblslpm_quirk)
  2148. reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
  2149. dwc3_writel(dwc, DWC3_GUSB2PHYCFG(i), reg);
  2150. }
  2151. for (i = 0; i < dwc->num_usb2_ports; i++)
  2152. phy_pm_runtime_get_sync(dwc->usb2_generic_phy[i]);
  2153. for (i = 0; i < dwc->num_usb3_ports; i++)
  2154. phy_pm_runtime_get_sync(dwc->usb3_generic_phy[i]);
  2155. break;
  2156. case DWC3_GCTL_PRTCAP_OTG:
  2157. /* nothing to do on runtime_resume */
  2158. if (PMSG_IS_AUTO(msg))
  2159. break;
  2160. ret = dwc3_core_init_for_resume(dwc);
  2161. if (ret)
  2162. return ret;
  2163. dwc3_set_prtcap(dwc, dwc->current_dr_role, true);
  2164. dwc3_otg_init(dwc);
  2165. if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
  2166. dwc3_otg_host_init(dwc);
  2167. } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
  2168. dwc3_gadget_resume(dwc);
  2169. }
  2170. break;
  2171. default:
  2172. /* do nothing */
  2173. break;
  2174. }
  2175. if (!PMSG_IS_AUTO(msg)) {
  2176. /* restore SUSPHY state to that before system suspend. */
  2177. dwc3_enable_susphy(dwc, dwc->susphy_state);
  2178. }
  2179. return 0;
  2180. }
  2181. static int dwc3_runtime_checks(struct dwc3 *dwc)
  2182. {
  2183. switch (dwc->current_dr_role) {
  2184. case DWC3_GCTL_PRTCAP_DEVICE:
  2185. if (dwc->connected)
  2186. return -EBUSY;
  2187. break;
  2188. case DWC3_GCTL_PRTCAP_HOST:
  2189. default:
  2190. /* do nothing */
  2191. break;
  2192. }
  2193. return 0;
  2194. }
  2195. int dwc3_runtime_suspend(struct dwc3 *dwc)
  2196. {
  2197. int ret;
  2198. if (dwc3_runtime_checks(dwc))
  2199. return -EBUSY;
  2200. ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
  2201. if (ret)
  2202. return ret;
  2203. return 0;
  2204. }
  2205. EXPORT_SYMBOL_GPL(dwc3_runtime_suspend);
  2206. int dwc3_runtime_resume(struct dwc3 *dwc)
  2207. {
  2208. struct device *dev = dwc->dev;
  2209. int ret;
  2210. ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
  2211. if (ret)
  2212. return ret;
  2213. switch (dwc->current_dr_role) {
  2214. case DWC3_GCTL_PRTCAP_DEVICE:
  2215. if (dwc->pending_events) {
  2216. pm_runtime_put(dev);
  2217. dwc->pending_events = false;
  2218. enable_irq(dwc->irq_gadget);
  2219. }
  2220. break;
  2221. case DWC3_GCTL_PRTCAP_HOST:
  2222. default:
  2223. /* do nothing */
  2224. break;
  2225. }
  2226. pm_runtime_mark_last_busy(dev);
  2227. return 0;
  2228. }
  2229. EXPORT_SYMBOL_GPL(dwc3_runtime_resume);
  2230. int dwc3_runtime_idle(struct dwc3 *dwc)
  2231. {
  2232. struct device *dev = dwc->dev;
  2233. switch (dwc->current_dr_role) {
  2234. case DWC3_GCTL_PRTCAP_DEVICE:
  2235. if (dwc3_runtime_checks(dwc))
  2236. return -EBUSY;
  2237. break;
  2238. case DWC3_GCTL_PRTCAP_HOST:
  2239. default:
  2240. /* do nothing */
  2241. break;
  2242. }
  2243. pm_runtime_autosuspend(dev);
  2244. return 0;
  2245. }
  2246. EXPORT_SYMBOL_GPL(dwc3_runtime_idle);
  2247. static int dwc3_plat_runtime_suspend(struct device *dev)
  2248. {
  2249. return dwc3_runtime_suspend(dev_get_drvdata(dev));
  2250. }
  2251. static int dwc3_plat_runtime_resume(struct device *dev)
  2252. {
  2253. return dwc3_runtime_resume(dev_get_drvdata(dev));
  2254. }
  2255. static int dwc3_plat_runtime_idle(struct device *dev)
  2256. {
  2257. return dwc3_runtime_idle(dev_get_drvdata(dev));
  2258. }
  2259. #endif /* CONFIG_PM */
  2260. #ifdef CONFIG_PM_SLEEP
  2261. int dwc3_pm_suspend(struct dwc3 *dwc)
  2262. {
  2263. struct device *dev = dwc->dev;
  2264. int ret;
  2265. ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
  2266. if (ret)
  2267. return ret;
  2268. pinctrl_pm_select_sleep_state(dev);
  2269. return 0;
  2270. }
  2271. EXPORT_SYMBOL_GPL(dwc3_pm_suspend);
  2272. int dwc3_pm_resume(struct dwc3 *dwc)
  2273. {
  2274. struct device *dev = dwc->dev;
  2275. int ret = 0;
  2276. pinctrl_pm_select_default_state(dev);
  2277. pm_runtime_disable(dev);
  2278. ret = pm_runtime_set_active(dev);
  2279. if (ret)
  2280. goto out;
  2281. ret = dwc3_resume_common(dwc, PMSG_RESUME);
  2282. if (ret)
  2283. pm_runtime_set_suspended(dev);
  2284. out:
  2285. pm_runtime_enable(dev);
  2286. return ret;
  2287. }
  2288. EXPORT_SYMBOL_GPL(dwc3_pm_resume);
  2289. void dwc3_pm_complete(struct dwc3 *dwc)
  2290. {
  2291. u32 reg;
  2292. if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST &&
  2293. dwc->dis_split_quirk) {
  2294. reg = dwc3_readl(dwc, DWC3_GUCTL3);
  2295. reg |= DWC3_GUCTL3_SPLITDISABLE;
  2296. dwc3_writel(dwc, DWC3_GUCTL3, reg);
  2297. }
  2298. }
  2299. EXPORT_SYMBOL_GPL(dwc3_pm_complete);
  2300. int dwc3_pm_prepare(struct dwc3 *dwc)
  2301. {
  2302. struct device *dev = dwc->dev;
  2303. /*
  2304. * Indicate to the PM core that it may safely leave the device in
  2305. * runtime suspend if runtime-suspended already in device mode.
  2306. */
  2307. if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE &&
  2308. pm_runtime_suspended(dev) &&
  2309. !dev_pinctrl(dev))
  2310. return 1;
  2311. return 0;
  2312. }
  2313. EXPORT_SYMBOL_GPL(dwc3_pm_prepare);
  2314. static int dwc3_plat_suspend(struct device *dev)
  2315. {
  2316. return dwc3_pm_suspend(dev_get_drvdata(dev));
  2317. }
  2318. static int dwc3_plat_resume(struct device *dev)
  2319. {
  2320. return dwc3_pm_resume(dev_get_drvdata(dev));
  2321. }
  2322. static void dwc3_plat_complete(struct device *dev)
  2323. {
  2324. dwc3_pm_complete(dev_get_drvdata(dev));
  2325. }
  2326. static int dwc3_plat_prepare(struct device *dev)
  2327. {
  2328. return dwc3_pm_prepare(dev_get_drvdata(dev));
  2329. }
  2330. #else
  2331. #define dwc3_plat_complete NULL
  2332. #define dwc3_plat_prepare NULL
  2333. #endif /* CONFIG_PM_SLEEP */
  2334. static const struct dev_pm_ops dwc3_dev_pm_ops = {
  2335. SET_SYSTEM_SLEEP_PM_OPS(dwc3_plat_suspend, dwc3_plat_resume)
  2336. .complete = dwc3_plat_complete,
  2337. .prepare = dwc3_plat_prepare,
  2338. /*
  2339. * Runtime suspend halts the controller on disconnection. It relies on
  2340. * platforms with custom connection notification to start the controller
  2341. * again.
  2342. */
  2343. SET_RUNTIME_PM_OPS(dwc3_plat_runtime_suspend, dwc3_plat_runtime_resume,
  2344. dwc3_plat_runtime_idle)
  2345. };
  2346. #ifdef CONFIG_OF
  2347. static const struct of_device_id of_dwc3_match[] = {
  2348. {
  2349. .compatible = "snps,dwc3"
  2350. },
  2351. {
  2352. .compatible = "synopsys,dwc3"
  2353. },
  2354. { },
  2355. };
  2356. MODULE_DEVICE_TABLE(of, of_dwc3_match);
  2357. #endif
  2358. #ifdef CONFIG_ACPI
  2359. #define ACPI_ID_INTEL_BSW "808622B7"
  2360. static const struct acpi_device_id dwc3_acpi_match[] = {
  2361. { ACPI_ID_INTEL_BSW, 0 },
  2362. { },
  2363. };
  2364. MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
  2365. #endif
  2366. static struct platform_driver dwc3_driver = {
  2367. .probe = dwc3_probe,
  2368. .remove = dwc3_remove,
  2369. .driver = {
  2370. .name = "dwc3",
  2371. .of_match_table = of_match_ptr(of_dwc3_match),
  2372. .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
  2373. .pm = &dwc3_dev_pm_ops,
  2374. },
  2375. };
  2376. module_platform_driver(dwc3_driver);
  2377. MODULE_ALIAS("platform:dwc3");
  2378. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  2379. MODULE_LICENSE("GPL v2");
  2380. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");