core.c 68 KB

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