gadget.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
  4. *
  5. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/list.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. #include "debug.h"
  23. #include "core.h"
  24. #include "gadget.h"
  25. #include "io.h"
  26. /**
  27. * dwc3_gadget_set_test_mode - enables usb2 test modes
  28. * @dwc: pointer to our context structure
  29. * @mode: the mode to set (J, K SE0 NAK, Force Enable)
  30. *
  31. * Caller should take care of locking. This function will return 0 on
  32. * success or -EINVAL if wrong Test Selector is passed.
  33. */
  34. int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
  35. {
  36. u32 reg;
  37. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  38. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  39. switch (mode) {
  40. case TEST_J:
  41. case TEST_K:
  42. case TEST_SE0_NAK:
  43. case TEST_PACKET:
  44. case TEST_FORCE_EN:
  45. reg |= mode << 1;
  46. break;
  47. default:
  48. return -EINVAL;
  49. }
  50. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  51. return 0;
  52. }
  53. /**
  54. * dwc3_gadget_get_link_state - gets current state of usb link
  55. * @dwc: pointer to our context structure
  56. *
  57. * Caller should take care of locking. This function will
  58. * return the link state on success (>= 0) or -ETIMEDOUT.
  59. */
  60. int dwc3_gadget_get_link_state(struct dwc3 *dwc)
  61. {
  62. u32 reg;
  63. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  64. return DWC3_DSTS_USBLNKST(reg);
  65. }
  66. /**
  67. * dwc3_gadget_set_link_state - sets usb link to a particular state
  68. * @dwc: pointer to our context structure
  69. * @state: the state to put link into
  70. *
  71. * Caller should take care of locking. This function will
  72. * return 0 on success or -ETIMEDOUT.
  73. */
  74. int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
  75. {
  76. int retries = 10000;
  77. u32 reg;
  78. /*
  79. * Wait until device controller is ready. Only applies to 1.94a and
  80. * later RTL.
  81. */
  82. if (dwc->revision >= DWC3_REVISION_194A) {
  83. while (--retries) {
  84. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  85. if (reg & DWC3_DSTS_DCNRD)
  86. udelay(5);
  87. else
  88. break;
  89. }
  90. if (retries <= 0)
  91. return -ETIMEDOUT;
  92. }
  93. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  94. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  95. /* set requested state */
  96. reg |= DWC3_DCTL_ULSTCHNGREQ(state);
  97. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  98. /*
  99. * The following code is racy when called from dwc3_gadget_wakeup,
  100. * and is not needed, at least on newer versions
  101. */
  102. if (dwc->revision >= DWC3_REVISION_194A)
  103. return 0;
  104. /* wait for a change in DSTS */
  105. retries = 10000;
  106. while (--retries) {
  107. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  108. if (DWC3_DSTS_USBLNKST(reg) == state)
  109. return 0;
  110. udelay(5);
  111. }
  112. return -ETIMEDOUT;
  113. }
  114. /**
  115. * dwc3_ep_inc_trb - increment a trb index.
  116. * @index: Pointer to the TRB index to increment.
  117. *
  118. * The index should never point to the link TRB. After incrementing,
  119. * if it is point to the link TRB, wrap around to the beginning. The
  120. * link TRB is always at the last TRB entry.
  121. */
  122. static void dwc3_ep_inc_trb(u8 *index)
  123. {
  124. (*index)++;
  125. if (*index == (DWC3_TRB_NUM - 1))
  126. *index = 0;
  127. }
  128. /**
  129. * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
  130. * @dep: The endpoint whose enqueue pointer we're incrementing
  131. */
  132. static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
  133. {
  134. dwc3_ep_inc_trb(&dep->trb_enqueue);
  135. }
  136. /**
  137. * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
  138. * @dep: The endpoint whose enqueue pointer we're incrementing
  139. */
  140. static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
  141. {
  142. dwc3_ep_inc_trb(&dep->trb_dequeue);
  143. }
  144. /**
  145. * dwc3_gadget_giveback - call struct usb_request's ->complete callback
  146. * @dep: The endpoint to whom the request belongs to
  147. * @req: The request we're giving back
  148. * @status: completion code for the request
  149. *
  150. * Must be called with controller's lock held and interrupts disabled. This
  151. * function will unmap @req and call its ->complete() callback to notify upper
  152. * layers that it has completed.
  153. */
  154. void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
  155. int status)
  156. {
  157. struct dwc3 *dwc = dep->dwc;
  158. req->started = false;
  159. list_del(&req->list);
  160. req->remaining = 0;
  161. if (req->request.status == -EINPROGRESS)
  162. req->request.status = status;
  163. if (req->trb)
  164. usb_gadget_unmap_request_by_dev(dwc->sysdev,
  165. &req->request, req->direction);
  166. req->trb = NULL;
  167. trace_dwc3_gadget_giveback(req);
  168. spin_unlock(&dwc->lock);
  169. usb_gadget_giveback_request(&dep->endpoint, &req->request);
  170. spin_lock(&dwc->lock);
  171. if (dep->number > 1)
  172. pm_runtime_put(dwc->dev);
  173. }
  174. /**
  175. * dwc3_send_gadget_generic_command - issue a generic command for the controller
  176. * @dwc: pointer to the controller context
  177. * @cmd: the command to be issued
  178. * @param: command parameter
  179. *
  180. * Caller should take care of locking. Issue @cmd with a given @param to @dwc
  181. * and wait for its completion.
  182. */
  183. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
  184. {
  185. u32 timeout = 500;
  186. int status = 0;
  187. int ret = 0;
  188. u32 reg;
  189. dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
  190. dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
  191. do {
  192. reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
  193. if (!(reg & DWC3_DGCMD_CMDACT)) {
  194. status = DWC3_DGCMD_STATUS(reg);
  195. if (status)
  196. ret = -EINVAL;
  197. break;
  198. }
  199. } while (--timeout);
  200. if (!timeout) {
  201. ret = -ETIMEDOUT;
  202. status = -ETIMEDOUT;
  203. }
  204. trace_dwc3_gadget_generic_cmd(cmd, param, status);
  205. return ret;
  206. }
  207. static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
  208. /**
  209. * dwc3_send_gadget_ep_cmd - issue an endpoint command
  210. * @dep: the endpoint to which the command is going to be issued
  211. * @cmd: the command to be issued
  212. * @params: parameters to the command
  213. *
  214. * Caller should handle locking. This function will issue @cmd with given
  215. * @params to @dep and wait for its completion.
  216. */
  217. int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
  218. struct dwc3_gadget_ep_cmd_params *params)
  219. {
  220. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  221. struct dwc3 *dwc = dep->dwc;
  222. u32 timeout = 1000;
  223. u32 reg;
  224. int cmd_status = 0;
  225. int susphy = false;
  226. int ret = -EINVAL;
  227. /*
  228. * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
  229. * we're issuing an endpoint command, we must check if
  230. * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
  231. *
  232. * We will also set SUSPHY bit to what it was before returning as stated
  233. * by the same section on Synopsys databook.
  234. */
  235. if (dwc->gadget.speed <= USB_SPEED_HIGH) {
  236. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  237. if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
  238. susphy = true;
  239. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  240. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  241. }
  242. }
  243. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
  244. int needs_wakeup;
  245. needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
  246. dwc->link_state == DWC3_LINK_STATE_U2 ||
  247. dwc->link_state == DWC3_LINK_STATE_U3);
  248. if (unlikely(needs_wakeup)) {
  249. ret = __dwc3_gadget_wakeup(dwc);
  250. dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
  251. ret);
  252. }
  253. }
  254. dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
  255. dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
  256. dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
  257. /*
  258. * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
  259. * not relying on XferNotReady, we can make use of a special "No
  260. * Response Update Transfer" command where we should clear both CmdAct
  261. * and CmdIOC bits.
  262. *
  263. * With this, we don't need to wait for command completion and can
  264. * straight away issue further commands to the endpoint.
  265. *
  266. * NOTICE: We're making an assumption that control endpoints will never
  267. * make use of Update Transfer command. This is a safe assumption
  268. * because we can never have more than one request at a time with
  269. * Control Endpoints. If anybody changes that assumption, this chunk
  270. * needs to be updated accordingly.
  271. */
  272. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
  273. !usb_endpoint_xfer_isoc(desc))
  274. cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
  275. else
  276. cmd |= DWC3_DEPCMD_CMDACT;
  277. dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
  278. do {
  279. reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
  280. if (!(reg & DWC3_DEPCMD_CMDACT)) {
  281. cmd_status = DWC3_DEPCMD_STATUS(reg);
  282. switch (cmd_status) {
  283. case 0:
  284. ret = 0;
  285. break;
  286. case DEPEVT_TRANSFER_NO_RESOURCE:
  287. ret = -EINVAL;
  288. break;
  289. case DEPEVT_TRANSFER_BUS_EXPIRY:
  290. /*
  291. * SW issues START TRANSFER command to
  292. * isochronous ep with future frame interval. If
  293. * future interval time has already passed when
  294. * core receives the command, it will respond
  295. * with an error status of 'Bus Expiry'.
  296. *
  297. * Instead of always returning -EINVAL, let's
  298. * give a hint to the gadget driver that this is
  299. * the case by returning -EAGAIN.
  300. */
  301. ret = -EAGAIN;
  302. break;
  303. default:
  304. dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
  305. }
  306. break;
  307. }
  308. } while (--timeout);
  309. if (timeout == 0) {
  310. ret = -ETIMEDOUT;
  311. cmd_status = -ETIMEDOUT;
  312. }
  313. trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
  314. if (ret == 0) {
  315. switch (DWC3_DEPCMD_CMD(cmd)) {
  316. case DWC3_DEPCMD_STARTTRANSFER:
  317. dep->flags |= DWC3_EP_TRANSFER_STARTED;
  318. break;
  319. case DWC3_DEPCMD_ENDTRANSFER:
  320. dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  321. break;
  322. default:
  323. /* nothing */
  324. break;
  325. }
  326. }
  327. if (unlikely(susphy)) {
  328. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  329. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  330. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  331. }
  332. return ret;
  333. }
  334. static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
  335. {
  336. struct dwc3 *dwc = dep->dwc;
  337. struct dwc3_gadget_ep_cmd_params params;
  338. u32 cmd = DWC3_DEPCMD_CLEARSTALL;
  339. /*
  340. * As of core revision 2.60a the recommended programming model
  341. * is to set the ClearPendIN bit when issuing a Clear Stall EP
  342. * command for IN endpoints. This is to prevent an issue where
  343. * some (non-compliant) hosts may not send ACK TPs for pending
  344. * IN transfers due to a mishandled error condition. Synopsys
  345. * STAR 9000614252.
  346. */
  347. if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
  348. (dwc->gadget.speed >= USB_SPEED_SUPER))
  349. cmd |= DWC3_DEPCMD_CLEARPENDIN;
  350. memset(&params, 0, sizeof(params));
  351. return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  352. }
  353. static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
  354. struct dwc3_trb *trb)
  355. {
  356. u32 offset = (char *) trb - (char *) dep->trb_pool;
  357. return dep->trb_pool_dma + offset;
  358. }
  359. static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
  360. {
  361. struct dwc3 *dwc = dep->dwc;
  362. if (dep->trb_pool)
  363. return 0;
  364. dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
  365. sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  366. &dep->trb_pool_dma, GFP_KERNEL);
  367. if (!dep->trb_pool) {
  368. dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
  369. dep->name);
  370. return -ENOMEM;
  371. }
  372. return 0;
  373. }
  374. static void dwc3_free_trb_pool(struct dwc3_ep *dep)
  375. {
  376. struct dwc3 *dwc = dep->dwc;
  377. dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  378. dep->trb_pool, dep->trb_pool_dma);
  379. dep->trb_pool = NULL;
  380. dep->trb_pool_dma = 0;
  381. }
  382. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
  383. /**
  384. * dwc3_gadget_start_config - configure ep resources
  385. * @dwc: pointer to our controller context structure
  386. * @dep: endpoint that is being enabled
  387. *
  388. * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
  389. * completion, it will set Transfer Resource for all available endpoints.
  390. *
  391. * The assignment of transfer resources cannot perfectly follow the data book
  392. * due to the fact that the controller driver does not have all knowledge of the
  393. * configuration in advance. It is given this information piecemeal by the
  394. * composite gadget framework after every SET_CONFIGURATION and
  395. * SET_INTERFACE. Trying to follow the databook programming model in this
  396. * scenario can cause errors. For two reasons:
  397. *
  398. * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
  399. * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
  400. * incorrect in the scenario of multiple interfaces.
  401. *
  402. * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
  403. * endpoint on alt setting (8.1.6).
  404. *
  405. * The following simplified method is used instead:
  406. *
  407. * All hardware endpoints can be assigned a transfer resource and this setting
  408. * will stay persistent until either a core reset or hibernation. So whenever we
  409. * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
  410. * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
  411. * guaranteed that there are as many transfer resources as endpoints.
  412. *
  413. * This function is called for each endpoint when it is being enabled but is
  414. * triggered only when called for EP0-out, which always happens first, and which
  415. * should only happen in one of the above conditions.
  416. */
  417. static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
  418. {
  419. struct dwc3_gadget_ep_cmd_params params;
  420. u32 cmd;
  421. int i;
  422. int ret;
  423. if (dep->number)
  424. return 0;
  425. memset(&params, 0x00, sizeof(params));
  426. cmd = DWC3_DEPCMD_DEPSTARTCFG;
  427. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  428. if (ret)
  429. return ret;
  430. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  431. struct dwc3_ep *dep = dwc->eps[i];
  432. if (!dep)
  433. continue;
  434. ret = dwc3_gadget_set_xfer_resource(dwc, dep);
  435. if (ret)
  436. return ret;
  437. }
  438. return 0;
  439. }
  440. static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
  441. bool modify, bool restore)
  442. {
  443. const struct usb_ss_ep_comp_descriptor *comp_desc;
  444. const struct usb_endpoint_descriptor *desc;
  445. struct dwc3_gadget_ep_cmd_params params;
  446. if (dev_WARN_ONCE(dwc->dev, modify && restore,
  447. "Can't modify and restore\n"))
  448. return -EINVAL;
  449. comp_desc = dep->endpoint.comp_desc;
  450. desc = dep->endpoint.desc;
  451. memset(&params, 0x00, sizeof(params));
  452. params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
  453. | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
  454. /* Burst size is only needed in SuperSpeed mode */
  455. if (dwc->gadget.speed >= USB_SPEED_SUPER) {
  456. u32 burst = dep->endpoint.maxburst;
  457. params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
  458. }
  459. if (modify) {
  460. params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
  461. } else if (restore) {
  462. params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
  463. params.param2 |= dep->saved_state;
  464. } else {
  465. params.param0 |= DWC3_DEPCFG_ACTION_INIT;
  466. }
  467. if (usb_endpoint_xfer_control(desc))
  468. params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
  469. if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
  470. params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
  471. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  472. params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
  473. | DWC3_DEPCFG_STREAM_EVENT_EN;
  474. dep->stream_capable = true;
  475. }
  476. if (!usb_endpoint_xfer_control(desc))
  477. params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
  478. /*
  479. * We are doing 1:1 mapping for endpoints, meaning
  480. * Physical Endpoints 2 maps to Logical Endpoint 2 and
  481. * so on. We consider the direction bit as part of the physical
  482. * endpoint number. So USB endpoint 0x81 is 0x03.
  483. */
  484. params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
  485. /*
  486. * We must use the lower 16 TX FIFOs even though
  487. * HW might have more
  488. */
  489. if (dep->direction)
  490. params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
  491. if (desc->bInterval) {
  492. params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
  493. dep->interval = 1 << (desc->bInterval - 1);
  494. }
  495. return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
  496. }
  497. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
  498. {
  499. struct dwc3_gadget_ep_cmd_params params;
  500. memset(&params, 0x00, sizeof(params));
  501. params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
  502. return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
  503. &params);
  504. }
  505. /**
  506. * __dwc3_gadget_ep_enable - initializes a hw endpoint
  507. * @dep: endpoint to be initialized
  508. * @modify: if true, modify existing endpoint configuration
  509. * @restore: if true, restore endpoint configuration from scratch buffer
  510. *
  511. * Caller should take care of locking. Execute all necessary commands to
  512. * initialize a HW endpoint so it can be used by a gadget driver.
  513. */
  514. static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
  515. bool modify, bool restore)
  516. {
  517. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  518. struct dwc3 *dwc = dep->dwc;
  519. u32 reg;
  520. int ret;
  521. if (!(dep->flags & DWC3_EP_ENABLED)) {
  522. ret = dwc3_gadget_start_config(dwc, dep);
  523. if (ret)
  524. return ret;
  525. }
  526. ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
  527. if (ret)
  528. return ret;
  529. if (!(dep->flags & DWC3_EP_ENABLED)) {
  530. struct dwc3_trb *trb_st_hw;
  531. struct dwc3_trb *trb_link;
  532. dep->type = usb_endpoint_type(desc);
  533. dep->flags |= DWC3_EP_ENABLED;
  534. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  535. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  536. reg |= DWC3_DALEPENA_EP(dep->number);
  537. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  538. init_waitqueue_head(&dep->wait_end_transfer);
  539. if (usb_endpoint_xfer_control(desc))
  540. goto out;
  541. /* Initialize the TRB ring */
  542. dep->trb_dequeue = 0;
  543. dep->trb_enqueue = 0;
  544. memset(dep->trb_pool, 0,
  545. sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
  546. /* Link TRB. The HWO bit is never reset */
  547. trb_st_hw = &dep->trb_pool[0];
  548. trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  549. trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  550. trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  551. trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
  552. trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
  553. }
  554. /*
  555. * Issue StartTransfer here with no-op TRB so we can always rely on No
  556. * Response Update Transfer command.
  557. */
  558. if (usb_endpoint_xfer_bulk(desc)) {
  559. struct dwc3_gadget_ep_cmd_params params;
  560. struct dwc3_trb *trb;
  561. dma_addr_t trb_dma;
  562. u32 cmd;
  563. memset(&params, 0, sizeof(params));
  564. trb = &dep->trb_pool[0];
  565. trb_dma = dwc3_trb_dma_offset(dep, trb);
  566. params.param0 = upper_32_bits(trb_dma);
  567. params.param1 = lower_32_bits(trb_dma);
  568. cmd = DWC3_DEPCMD_STARTTRANSFER;
  569. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  570. if (ret < 0)
  571. return ret;
  572. dep->flags |= DWC3_EP_BUSY;
  573. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
  574. WARN_ON_ONCE(!dep->resource_index);
  575. }
  576. out:
  577. trace_dwc3_gadget_ep_enable(dep);
  578. return 0;
  579. }
  580. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
  581. static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
  582. {
  583. struct dwc3_request *req;
  584. dwc3_stop_active_transfer(dwc, dep->number, true);
  585. /* - giveback all requests to gadget driver */
  586. while (!list_empty(&dep->started_list)) {
  587. req = next_request(&dep->started_list);
  588. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  589. }
  590. while (!list_empty(&dep->pending_list)) {
  591. req = next_request(&dep->pending_list);
  592. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  593. }
  594. }
  595. /**
  596. * __dwc3_gadget_ep_disable - disables a hw endpoint
  597. * @dep: the endpoint to disable
  598. *
  599. * This function undoes what __dwc3_gadget_ep_enable did and also removes
  600. * requests which are currently being processed by the hardware and those which
  601. * are not yet scheduled.
  602. *
  603. * Caller should take care of locking.
  604. */
  605. static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  606. {
  607. struct dwc3 *dwc = dep->dwc;
  608. u32 reg;
  609. trace_dwc3_gadget_ep_disable(dep);
  610. dwc3_remove_requests(dwc, dep);
  611. /* make sure HW endpoint isn't stalled */
  612. if (dep->flags & DWC3_EP_STALL)
  613. __dwc3_gadget_ep_set_halt(dep, 0, false);
  614. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  615. reg &= ~DWC3_DALEPENA_EP(dep->number);
  616. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  617. dep->stream_capable = false;
  618. dep->type = 0;
  619. dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
  620. /* Clear out the ep descriptors for non-ep0 */
  621. if (dep->number > 1) {
  622. dep->endpoint.comp_desc = NULL;
  623. dep->endpoint.desc = NULL;
  624. }
  625. return 0;
  626. }
  627. /* -------------------------------------------------------------------------- */
  628. static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
  629. const struct usb_endpoint_descriptor *desc)
  630. {
  631. return -EINVAL;
  632. }
  633. static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
  634. {
  635. return -EINVAL;
  636. }
  637. /* -------------------------------------------------------------------------- */
  638. static int dwc3_gadget_ep_enable(struct usb_ep *ep,
  639. const struct usb_endpoint_descriptor *desc)
  640. {
  641. struct dwc3_ep *dep;
  642. struct dwc3 *dwc;
  643. unsigned long flags;
  644. int ret;
  645. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  646. pr_debug("dwc3: invalid parameters\n");
  647. return -EINVAL;
  648. }
  649. if (!desc->wMaxPacketSize) {
  650. pr_debug("dwc3: missing wMaxPacketSize\n");
  651. return -EINVAL;
  652. }
  653. dep = to_dwc3_ep(ep);
  654. dwc = dep->dwc;
  655. if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
  656. "%s is already enabled\n",
  657. dep->name))
  658. return 0;
  659. spin_lock_irqsave(&dwc->lock, flags);
  660. ret = __dwc3_gadget_ep_enable(dep, false, false);
  661. spin_unlock_irqrestore(&dwc->lock, flags);
  662. return ret;
  663. }
  664. static int dwc3_gadget_ep_disable(struct usb_ep *ep)
  665. {
  666. struct dwc3_ep *dep;
  667. struct dwc3 *dwc;
  668. unsigned long flags;
  669. int ret;
  670. if (!ep) {
  671. pr_debug("dwc3: invalid parameters\n");
  672. return -EINVAL;
  673. }
  674. dep = to_dwc3_ep(ep);
  675. dwc = dep->dwc;
  676. if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
  677. "%s is already disabled\n",
  678. dep->name))
  679. return 0;
  680. spin_lock_irqsave(&dwc->lock, flags);
  681. ret = __dwc3_gadget_ep_disable(dep);
  682. spin_unlock_irqrestore(&dwc->lock, flags);
  683. return ret;
  684. }
  685. static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
  686. gfp_t gfp_flags)
  687. {
  688. struct dwc3_request *req;
  689. struct dwc3_ep *dep = to_dwc3_ep(ep);
  690. req = kzalloc(sizeof(*req), gfp_flags);
  691. if (!req)
  692. return NULL;
  693. req->epnum = dep->number;
  694. req->dep = dep;
  695. dep->allocated_requests++;
  696. trace_dwc3_alloc_request(req);
  697. return &req->request;
  698. }
  699. static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
  700. struct usb_request *request)
  701. {
  702. struct dwc3_request *req = to_dwc3_request(request);
  703. struct dwc3_ep *dep = to_dwc3_ep(ep);
  704. dep->allocated_requests--;
  705. trace_dwc3_free_request(req);
  706. kfree(req);
  707. }
  708. static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
  709. static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
  710. dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
  711. unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
  712. {
  713. struct dwc3 *dwc = dep->dwc;
  714. struct usb_gadget *gadget = &dwc->gadget;
  715. enum usb_device_speed speed = gadget->speed;
  716. dwc3_ep_inc_enq(dep);
  717. trb->size = DWC3_TRB_SIZE_LENGTH(length);
  718. trb->bpl = lower_32_bits(dma);
  719. trb->bph = upper_32_bits(dma);
  720. switch (usb_endpoint_type(dep->endpoint.desc)) {
  721. case USB_ENDPOINT_XFER_CONTROL:
  722. trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
  723. break;
  724. case USB_ENDPOINT_XFER_ISOC:
  725. if (!node) {
  726. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  727. /*
  728. * USB Specification 2.0 Section 5.9.2 states that: "If
  729. * there is only a single transaction in the microframe,
  730. * only a DATA0 data packet PID is used. If there are
  731. * two transactions per microframe, DATA1 is used for
  732. * the first transaction data packet and DATA0 is used
  733. * for the second transaction data packet. If there are
  734. * three transactions per microframe, DATA2 is used for
  735. * the first transaction data packet, DATA1 is used for
  736. * the second, and DATA0 is used for the third."
  737. *
  738. * IOW, we should satisfy the following cases:
  739. *
  740. * 1) length <= maxpacket
  741. * - DATA0
  742. *
  743. * 2) maxpacket < length <= (2 * maxpacket)
  744. * - DATA1, DATA0
  745. *
  746. * 3) (2 * maxpacket) < length <= (3 * maxpacket)
  747. * - DATA2, DATA1, DATA0
  748. */
  749. if (speed == USB_SPEED_HIGH) {
  750. struct usb_ep *ep = &dep->endpoint;
  751. unsigned int mult = 2;
  752. unsigned int maxp = usb_endpoint_maxp(ep->desc);
  753. if (length <= (2 * maxp))
  754. mult--;
  755. if (length <= maxp)
  756. mult--;
  757. trb->size |= DWC3_TRB_SIZE_PCM1(mult);
  758. }
  759. } else {
  760. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
  761. }
  762. /* always enable Interrupt on Missed ISOC */
  763. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  764. break;
  765. case USB_ENDPOINT_XFER_BULK:
  766. case USB_ENDPOINT_XFER_INT:
  767. trb->ctrl = DWC3_TRBCTL_NORMAL;
  768. break;
  769. default:
  770. /*
  771. * This is only possible with faulty memory because we
  772. * checked it already :)
  773. */
  774. dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
  775. usb_endpoint_type(dep->endpoint.desc));
  776. }
  777. /* always enable Continue on Short Packet */
  778. if (usb_endpoint_dir_out(dep->endpoint.desc)) {
  779. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  780. if (short_not_ok)
  781. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  782. }
  783. if ((!no_interrupt && !chain) ||
  784. (dwc3_calc_trbs_left(dep) == 0))
  785. trb->ctrl |= DWC3_TRB_CTRL_IOC;
  786. if (chain)
  787. trb->ctrl |= DWC3_TRB_CTRL_CHN;
  788. if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
  789. trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
  790. trb->ctrl |= DWC3_TRB_CTRL_HWO;
  791. trace_dwc3_prepare_trb(dep, trb);
  792. }
  793. /**
  794. * dwc3_prepare_one_trb - setup one TRB from one request
  795. * @dep: endpoint for which this request is prepared
  796. * @req: dwc3_request pointer
  797. * @chain: should this TRB be chained to the next?
  798. * @node: only for isochronous endpoints. First TRB needs different type.
  799. */
  800. static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
  801. struct dwc3_request *req, unsigned chain, unsigned node)
  802. {
  803. struct dwc3_trb *trb;
  804. unsigned length = req->request.length;
  805. unsigned stream_id = req->request.stream_id;
  806. unsigned short_not_ok = req->request.short_not_ok;
  807. unsigned no_interrupt = req->request.no_interrupt;
  808. dma_addr_t dma = req->request.dma;
  809. trb = &dep->trb_pool[dep->trb_enqueue];
  810. if (!req->trb) {
  811. dwc3_gadget_move_started_request(req);
  812. req->trb = trb;
  813. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  814. dep->queued_requests++;
  815. }
  816. __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
  817. stream_id, short_not_ok, no_interrupt);
  818. }
  819. /**
  820. * dwc3_ep_prev_trb - returns the previous TRB in the ring
  821. * @dep: The endpoint with the TRB ring
  822. * @index: The index of the current TRB in the ring
  823. *
  824. * Returns the TRB prior to the one pointed to by the index. If the
  825. * index is 0, we will wrap backwards, skip the link TRB, and return
  826. * the one just before that.
  827. */
  828. static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
  829. {
  830. u8 tmp = index;
  831. if (!tmp)
  832. tmp = DWC3_TRB_NUM - 1;
  833. return &dep->trb_pool[tmp - 1];
  834. }
  835. static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
  836. {
  837. struct dwc3_trb *tmp;
  838. u8 trbs_left;
  839. /*
  840. * If enqueue & dequeue are equal than it is either full or empty.
  841. *
  842. * One way to know for sure is if the TRB right before us has HWO bit
  843. * set or not. If it has, then we're definitely full and can't fit any
  844. * more transfers in our ring.
  845. */
  846. if (dep->trb_enqueue == dep->trb_dequeue) {
  847. tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  848. if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
  849. return 0;
  850. return DWC3_TRB_NUM - 1;
  851. }
  852. trbs_left = dep->trb_dequeue - dep->trb_enqueue;
  853. trbs_left &= (DWC3_TRB_NUM - 1);
  854. if (dep->trb_dequeue < dep->trb_enqueue)
  855. trbs_left--;
  856. return trbs_left;
  857. }
  858. static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
  859. struct dwc3_request *req)
  860. {
  861. struct scatterlist *sg = req->sg;
  862. struct scatterlist *s;
  863. int i;
  864. for_each_sg(sg, s, req->num_pending_sgs, i) {
  865. unsigned int length = req->request.length;
  866. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  867. unsigned int rem = length % maxp;
  868. unsigned chain = true;
  869. if (sg_is_last(s))
  870. chain = false;
  871. if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
  872. struct dwc3 *dwc = dep->dwc;
  873. struct dwc3_trb *trb;
  874. req->unaligned = true;
  875. /* prepare normal TRB */
  876. dwc3_prepare_one_trb(dep, req, true, i);
  877. /* Now prepare one extra TRB to align transfer size */
  878. trb = &dep->trb_pool[dep->trb_enqueue];
  879. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
  880. maxp - rem, false, 0,
  881. req->request.stream_id,
  882. req->request.short_not_ok,
  883. req->request.no_interrupt);
  884. } else {
  885. dwc3_prepare_one_trb(dep, req, chain, i);
  886. }
  887. if (!dwc3_calc_trbs_left(dep))
  888. break;
  889. }
  890. }
  891. static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
  892. struct dwc3_request *req)
  893. {
  894. unsigned int length = req->request.length;
  895. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  896. unsigned int rem = length % maxp;
  897. if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
  898. struct dwc3 *dwc = dep->dwc;
  899. struct dwc3_trb *trb;
  900. req->unaligned = true;
  901. /* prepare normal TRB */
  902. dwc3_prepare_one_trb(dep, req, true, 0);
  903. /* Now prepare one extra TRB to align transfer size */
  904. trb = &dep->trb_pool[dep->trb_enqueue];
  905. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
  906. false, 0, req->request.stream_id,
  907. req->request.short_not_ok,
  908. req->request.no_interrupt);
  909. } else if (req->request.zero && req->request.length &&
  910. (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
  911. struct dwc3 *dwc = dep->dwc;
  912. struct dwc3_trb *trb;
  913. req->zero = true;
  914. /* prepare normal TRB */
  915. dwc3_prepare_one_trb(dep, req, true, 0);
  916. /* Now prepare one extra TRB to handle ZLP */
  917. trb = &dep->trb_pool[dep->trb_enqueue];
  918. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
  919. false, 0, req->request.stream_id,
  920. req->request.short_not_ok,
  921. req->request.no_interrupt);
  922. } else {
  923. dwc3_prepare_one_trb(dep, req, false, 0);
  924. }
  925. }
  926. /*
  927. * dwc3_prepare_trbs - setup TRBs from requests
  928. * @dep: endpoint for which requests are being prepared
  929. *
  930. * The function goes through the requests list and sets up TRBs for the
  931. * transfers. The function returns once there are no more TRBs available or
  932. * it runs out of requests.
  933. */
  934. static void dwc3_prepare_trbs(struct dwc3_ep *dep)
  935. {
  936. struct dwc3_request *req, *n;
  937. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  938. /*
  939. * We can get in a situation where there's a request in the started list
  940. * but there weren't enough TRBs to fully kick it in the first time
  941. * around, so it has been waiting for more TRBs to be freed up.
  942. *
  943. * In that case, we should check if we have a request with pending_sgs
  944. * in the started list and prepare TRBs for that request first,
  945. * otherwise we will prepare TRBs completely out of order and that will
  946. * break things.
  947. */
  948. list_for_each_entry(req, &dep->started_list, list) {
  949. if (req->num_pending_sgs > 0)
  950. dwc3_prepare_one_trb_sg(dep, req);
  951. if (!dwc3_calc_trbs_left(dep))
  952. return;
  953. }
  954. list_for_each_entry_safe(req, n, &dep->pending_list, list) {
  955. struct dwc3 *dwc = dep->dwc;
  956. int ret;
  957. ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
  958. dep->direction);
  959. if (ret)
  960. return;
  961. req->sg = req->request.sg;
  962. req->num_pending_sgs = req->request.num_mapped_sgs;
  963. if (req->num_pending_sgs > 0)
  964. dwc3_prepare_one_trb_sg(dep, req);
  965. else
  966. dwc3_prepare_one_trb_linear(dep, req);
  967. if (!dwc3_calc_trbs_left(dep))
  968. return;
  969. }
  970. }
  971. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
  972. {
  973. struct dwc3_gadget_ep_cmd_params params;
  974. struct dwc3_request *req;
  975. int starting;
  976. int ret;
  977. u32 cmd;
  978. if (!dwc3_calc_trbs_left(dep))
  979. return 0;
  980. starting = !(dep->flags & DWC3_EP_BUSY);
  981. dwc3_prepare_trbs(dep);
  982. req = next_request(&dep->started_list);
  983. if (!req) {
  984. dep->flags |= DWC3_EP_PENDING_REQUEST;
  985. return 0;
  986. }
  987. memset(&params, 0, sizeof(params));
  988. if (starting) {
  989. params.param0 = upper_32_bits(req->trb_dma);
  990. params.param1 = lower_32_bits(req->trb_dma);
  991. cmd = DWC3_DEPCMD_STARTTRANSFER;
  992. if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
  993. cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
  994. } else {
  995. cmd = DWC3_DEPCMD_UPDATETRANSFER |
  996. DWC3_DEPCMD_PARAM(dep->resource_index);
  997. }
  998. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  999. if (ret < 0) {
  1000. /*
  1001. * FIXME we need to iterate over the list of requests
  1002. * here and stop, unmap, free and del each of the linked
  1003. * requests instead of what we do now.
  1004. */
  1005. if (req->trb)
  1006. memset(req->trb, 0, sizeof(struct dwc3_trb));
  1007. dep->queued_requests--;
  1008. dwc3_gadget_giveback(dep, req, ret);
  1009. return ret;
  1010. }
  1011. dep->flags |= DWC3_EP_BUSY;
  1012. if (starting) {
  1013. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
  1014. WARN_ON_ONCE(!dep->resource_index);
  1015. }
  1016. return 0;
  1017. }
  1018. static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
  1019. {
  1020. u32 reg;
  1021. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1022. return DWC3_DSTS_SOFFN(reg);
  1023. }
  1024. static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
  1025. struct dwc3_ep *dep, u32 cur_uf)
  1026. {
  1027. if (list_empty(&dep->pending_list)) {
  1028. dev_info(dwc->dev, "%s: ran out of requests\n",
  1029. dep->name);
  1030. dep->flags |= DWC3_EP_PENDING_REQUEST;
  1031. return;
  1032. }
  1033. /*
  1034. * Schedule the first trb for one interval in the future or at
  1035. * least 4 microframes.
  1036. */
  1037. dep->frame_number = cur_uf + max_t(u32, 4, dep->interval);
  1038. __dwc3_gadget_kick_transfer(dep);
  1039. }
  1040. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  1041. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  1042. {
  1043. u32 cur_uf, mask;
  1044. mask = ~(dep->interval - 1);
  1045. cur_uf = event->parameters & mask;
  1046. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  1047. }
  1048. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  1049. {
  1050. struct dwc3 *dwc = dep->dwc;
  1051. if (!dep->endpoint.desc) {
  1052. dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
  1053. dep->name);
  1054. return -ESHUTDOWN;
  1055. }
  1056. if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
  1057. &req->request, req->dep->name))
  1058. return -EINVAL;
  1059. pm_runtime_get(dwc->dev);
  1060. req->request.actual = 0;
  1061. req->request.status = -EINPROGRESS;
  1062. req->direction = dep->direction;
  1063. req->epnum = dep->number;
  1064. trace_dwc3_ep_queue(req);
  1065. list_add_tail(&req->list, &dep->pending_list);
  1066. /*
  1067. * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
  1068. * wait for a XferNotReady event so we will know what's the current
  1069. * (micro-)frame number.
  1070. *
  1071. * Without this trick, we are very, very likely gonna get Bus Expiry
  1072. * errors which will force us issue EndTransfer command.
  1073. */
  1074. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1075. if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
  1076. if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
  1077. dwc3_stop_active_transfer(dwc, dep->number, true);
  1078. dep->flags = DWC3_EP_ENABLED;
  1079. } else {
  1080. u32 cur_uf;
  1081. cur_uf = __dwc3_gadget_get_frame(dwc);
  1082. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  1083. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  1084. }
  1085. return 0;
  1086. }
  1087. if ((dep->flags & DWC3_EP_BUSY) &&
  1088. !(dep->flags & DWC3_EP_MISSED_ISOC))
  1089. goto out;
  1090. return 0;
  1091. }
  1092. out:
  1093. return __dwc3_gadget_kick_transfer(dep);
  1094. }
  1095. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  1096. gfp_t gfp_flags)
  1097. {
  1098. struct dwc3_request *req = to_dwc3_request(request);
  1099. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1100. struct dwc3 *dwc = dep->dwc;
  1101. unsigned long flags;
  1102. int ret;
  1103. spin_lock_irqsave(&dwc->lock, flags);
  1104. ret = __dwc3_gadget_ep_queue(dep, req);
  1105. spin_unlock_irqrestore(&dwc->lock, flags);
  1106. return ret;
  1107. }
  1108. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  1109. struct usb_request *request)
  1110. {
  1111. struct dwc3_request *req = to_dwc3_request(request);
  1112. struct dwc3_request *r = NULL;
  1113. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1114. struct dwc3 *dwc = dep->dwc;
  1115. unsigned long flags;
  1116. int ret = 0;
  1117. trace_dwc3_ep_dequeue(req);
  1118. spin_lock_irqsave(&dwc->lock, flags);
  1119. list_for_each_entry(r, &dep->pending_list, list) {
  1120. if (r == req)
  1121. break;
  1122. }
  1123. if (r != req) {
  1124. list_for_each_entry(r, &dep->started_list, list) {
  1125. if (r == req)
  1126. break;
  1127. }
  1128. if (r == req) {
  1129. /* wait until it is processed */
  1130. dwc3_stop_active_transfer(dwc, dep->number, true);
  1131. /*
  1132. * If request was already started, this means we had to
  1133. * stop the transfer. With that we also need to ignore
  1134. * all TRBs used by the request, however TRBs can only
  1135. * be modified after completion of END_TRANSFER
  1136. * command. So what we do here is that we wait for
  1137. * END_TRANSFER completion and only after that, we jump
  1138. * over TRBs by clearing HWO and incrementing dequeue
  1139. * pointer.
  1140. *
  1141. * Note that we have 2 possible types of transfers here:
  1142. *
  1143. * i) Linear buffer request
  1144. * ii) SG-list based request
  1145. *
  1146. * SG-list based requests will have r->num_pending_sgs
  1147. * set to a valid number (> 0). Linear requests,
  1148. * normally use a single TRB.
  1149. *
  1150. * For each of these two cases, if r->unaligned flag is
  1151. * set, one extra TRB has been used to align transfer
  1152. * size to wMaxPacketSize.
  1153. *
  1154. * All of these cases need to be taken into
  1155. * consideration so we don't mess up our TRB ring
  1156. * pointers.
  1157. */
  1158. wait_event_lock_irq(dep->wait_end_transfer,
  1159. !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
  1160. dwc->lock);
  1161. if (!r->trb)
  1162. goto out1;
  1163. if (r->num_pending_sgs) {
  1164. struct dwc3_trb *trb;
  1165. int i = 0;
  1166. for (i = 0; i < r->num_pending_sgs; i++) {
  1167. trb = r->trb + i;
  1168. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1169. dwc3_ep_inc_deq(dep);
  1170. }
  1171. if (r->unaligned || r->zero) {
  1172. trb = r->trb + r->num_pending_sgs + 1;
  1173. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1174. dwc3_ep_inc_deq(dep);
  1175. }
  1176. } else {
  1177. struct dwc3_trb *trb = r->trb;
  1178. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1179. dwc3_ep_inc_deq(dep);
  1180. if (r->unaligned || r->zero) {
  1181. trb = r->trb + 1;
  1182. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1183. dwc3_ep_inc_deq(dep);
  1184. }
  1185. }
  1186. goto out1;
  1187. }
  1188. dev_err(dwc->dev, "request %pK was not queued to %s\n",
  1189. request, ep->name);
  1190. ret = -EINVAL;
  1191. goto out0;
  1192. }
  1193. out1:
  1194. /* giveback the request */
  1195. dep->queued_requests--;
  1196. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  1197. out0:
  1198. spin_unlock_irqrestore(&dwc->lock, flags);
  1199. return ret;
  1200. }
  1201. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
  1202. {
  1203. struct dwc3_gadget_ep_cmd_params params;
  1204. struct dwc3 *dwc = dep->dwc;
  1205. int ret;
  1206. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1207. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1208. return -EINVAL;
  1209. }
  1210. memset(&params, 0x00, sizeof(params));
  1211. if (value) {
  1212. struct dwc3_trb *trb;
  1213. unsigned transfer_in_flight;
  1214. unsigned started;
  1215. if (dep->flags & DWC3_EP_STALL)
  1216. return 0;
  1217. if (dep->number > 1)
  1218. trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  1219. else
  1220. trb = &dwc->ep0_trb[dep->trb_enqueue];
  1221. transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
  1222. started = !list_empty(&dep->started_list);
  1223. if (!protocol && ((dep->direction && transfer_in_flight) ||
  1224. (!dep->direction && started))) {
  1225. return -EAGAIN;
  1226. }
  1227. ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
  1228. &params);
  1229. if (ret)
  1230. dev_err(dwc->dev, "failed to set STALL on %s\n",
  1231. dep->name);
  1232. else
  1233. dep->flags |= DWC3_EP_STALL;
  1234. } else {
  1235. if (!(dep->flags & DWC3_EP_STALL))
  1236. return 0;
  1237. ret = dwc3_send_clear_stall_ep_cmd(dep);
  1238. if (ret)
  1239. dev_err(dwc->dev, "failed to clear STALL on %s\n",
  1240. dep->name);
  1241. else
  1242. dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
  1243. }
  1244. return ret;
  1245. }
  1246. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1247. {
  1248. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1249. struct dwc3 *dwc = dep->dwc;
  1250. unsigned long flags;
  1251. int ret;
  1252. spin_lock_irqsave(&dwc->lock, flags);
  1253. ret = __dwc3_gadget_ep_set_halt(dep, value, false);
  1254. spin_unlock_irqrestore(&dwc->lock, flags);
  1255. return ret;
  1256. }
  1257. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1258. {
  1259. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1260. struct dwc3 *dwc = dep->dwc;
  1261. unsigned long flags;
  1262. int ret;
  1263. spin_lock_irqsave(&dwc->lock, flags);
  1264. dep->flags |= DWC3_EP_WEDGE;
  1265. if (dep->number == 0 || dep->number == 1)
  1266. ret = __dwc3_gadget_ep0_set_halt(ep, 1);
  1267. else
  1268. ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
  1269. spin_unlock_irqrestore(&dwc->lock, flags);
  1270. return ret;
  1271. }
  1272. /* -------------------------------------------------------------------------- */
  1273. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1274. .bLength = USB_DT_ENDPOINT_SIZE,
  1275. .bDescriptorType = USB_DT_ENDPOINT,
  1276. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1277. };
  1278. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1279. .enable = dwc3_gadget_ep0_enable,
  1280. .disable = dwc3_gadget_ep0_disable,
  1281. .alloc_request = dwc3_gadget_ep_alloc_request,
  1282. .free_request = dwc3_gadget_ep_free_request,
  1283. .queue = dwc3_gadget_ep0_queue,
  1284. .dequeue = dwc3_gadget_ep_dequeue,
  1285. .set_halt = dwc3_gadget_ep0_set_halt,
  1286. .set_wedge = dwc3_gadget_ep_set_wedge,
  1287. };
  1288. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1289. .enable = dwc3_gadget_ep_enable,
  1290. .disable = dwc3_gadget_ep_disable,
  1291. .alloc_request = dwc3_gadget_ep_alloc_request,
  1292. .free_request = dwc3_gadget_ep_free_request,
  1293. .queue = dwc3_gadget_ep_queue,
  1294. .dequeue = dwc3_gadget_ep_dequeue,
  1295. .set_halt = dwc3_gadget_ep_set_halt,
  1296. .set_wedge = dwc3_gadget_ep_set_wedge,
  1297. };
  1298. /* -------------------------------------------------------------------------- */
  1299. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1300. {
  1301. struct dwc3 *dwc = gadget_to_dwc(g);
  1302. return __dwc3_gadget_get_frame(dwc);
  1303. }
  1304. static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
  1305. {
  1306. int retries;
  1307. int ret;
  1308. u32 reg;
  1309. u8 link_state;
  1310. u8 speed;
  1311. /*
  1312. * According to the Databook Remote wakeup request should
  1313. * be issued only when the device is in early suspend state.
  1314. *
  1315. * We can check that via USB Link State bits in DSTS register.
  1316. */
  1317. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1318. speed = reg & DWC3_DSTS_CONNECTSPD;
  1319. if ((speed == DWC3_DSTS_SUPERSPEED) ||
  1320. (speed == DWC3_DSTS_SUPERSPEED_PLUS))
  1321. return 0;
  1322. link_state = DWC3_DSTS_USBLNKST(reg);
  1323. switch (link_state) {
  1324. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1325. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1326. break;
  1327. default:
  1328. return -EINVAL;
  1329. }
  1330. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1331. if (ret < 0) {
  1332. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1333. return ret;
  1334. }
  1335. /* Recent versions do this automatically */
  1336. if (dwc->revision < DWC3_REVISION_194A) {
  1337. /* write zeroes to Link Change Request */
  1338. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1339. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1340. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1341. }
  1342. /* poll until Link State changes to ON */
  1343. retries = 20000;
  1344. while (retries--) {
  1345. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1346. /* in HS, means ON */
  1347. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1348. break;
  1349. }
  1350. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1351. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1352. return -EINVAL;
  1353. }
  1354. return 0;
  1355. }
  1356. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1357. {
  1358. struct dwc3 *dwc = gadget_to_dwc(g);
  1359. unsigned long flags;
  1360. int ret;
  1361. spin_lock_irqsave(&dwc->lock, flags);
  1362. ret = __dwc3_gadget_wakeup(dwc);
  1363. spin_unlock_irqrestore(&dwc->lock, flags);
  1364. return ret;
  1365. }
  1366. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  1367. int is_selfpowered)
  1368. {
  1369. struct dwc3 *dwc = gadget_to_dwc(g);
  1370. unsigned long flags;
  1371. spin_lock_irqsave(&dwc->lock, flags);
  1372. g->is_selfpowered = !!is_selfpowered;
  1373. spin_unlock_irqrestore(&dwc->lock, flags);
  1374. return 0;
  1375. }
  1376. static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
  1377. {
  1378. u32 reg;
  1379. u32 timeout = 500;
  1380. if (pm_runtime_suspended(dwc->dev))
  1381. return 0;
  1382. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1383. if (is_on) {
  1384. if (dwc->revision <= DWC3_REVISION_187A) {
  1385. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  1386. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  1387. }
  1388. if (dwc->revision >= DWC3_REVISION_194A)
  1389. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1390. reg |= DWC3_DCTL_RUN_STOP;
  1391. if (dwc->has_hibernation)
  1392. reg |= DWC3_DCTL_KEEP_CONNECT;
  1393. dwc->pullups_connected = true;
  1394. } else {
  1395. reg &= ~DWC3_DCTL_RUN_STOP;
  1396. if (dwc->has_hibernation && !suspend)
  1397. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1398. dwc->pullups_connected = false;
  1399. }
  1400. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1401. do {
  1402. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1403. reg &= DWC3_DSTS_DEVCTRLHLT;
  1404. } while (--timeout && !(!is_on ^ !reg));
  1405. if (!timeout)
  1406. return -ETIMEDOUT;
  1407. return 0;
  1408. }
  1409. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  1410. {
  1411. struct dwc3 *dwc = gadget_to_dwc(g);
  1412. unsigned long flags;
  1413. int ret;
  1414. is_on = !!is_on;
  1415. /*
  1416. * Per databook, when we want to stop the gadget, if a control transfer
  1417. * is still in process, complete it and get the core into setup phase.
  1418. */
  1419. if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
  1420. reinit_completion(&dwc->ep0_in_setup);
  1421. ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
  1422. msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
  1423. if (ret == 0) {
  1424. dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
  1425. return -ETIMEDOUT;
  1426. }
  1427. }
  1428. spin_lock_irqsave(&dwc->lock, flags);
  1429. ret = dwc3_gadget_run_stop(dwc, is_on, false);
  1430. spin_unlock_irqrestore(&dwc->lock, flags);
  1431. return ret;
  1432. }
  1433. static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
  1434. {
  1435. u32 reg;
  1436. /* Enable all but Start and End of Frame IRQs */
  1437. reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
  1438. DWC3_DEVTEN_EVNTOVERFLOWEN |
  1439. DWC3_DEVTEN_CMDCMPLTEN |
  1440. DWC3_DEVTEN_ERRTICERREN |
  1441. DWC3_DEVTEN_WKUPEVTEN |
  1442. DWC3_DEVTEN_CONNECTDONEEN |
  1443. DWC3_DEVTEN_USBRSTEN |
  1444. DWC3_DEVTEN_DISCONNEVTEN);
  1445. if (dwc->revision < DWC3_REVISION_250A)
  1446. reg |= DWC3_DEVTEN_ULSTCNGEN;
  1447. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  1448. }
  1449. static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
  1450. {
  1451. /* mask all interrupts */
  1452. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  1453. }
  1454. static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
  1455. static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
  1456. /**
  1457. * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
  1458. * @dwc: pointer to our context structure
  1459. *
  1460. * The following looks like complex but it's actually very simple. In order to
  1461. * calculate the number of packets we can burst at once on OUT transfers, we're
  1462. * gonna use RxFIFO size.
  1463. *
  1464. * To calculate RxFIFO size we need two numbers:
  1465. * MDWIDTH = size, in bits, of the internal memory bus
  1466. * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
  1467. *
  1468. * Given these two numbers, the formula is simple:
  1469. *
  1470. * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
  1471. *
  1472. * 24 bytes is for 3x SETUP packets
  1473. * 16 bytes is a clock domain crossing tolerance
  1474. *
  1475. * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
  1476. */
  1477. static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
  1478. {
  1479. u32 ram2_depth;
  1480. u32 mdwidth;
  1481. u32 nump;
  1482. u32 reg;
  1483. ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
  1484. mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
  1485. nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
  1486. nump = min_t(u32, nump, 16);
  1487. /* update NumP */
  1488. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1489. reg &= ~DWC3_DCFG_NUMP_MASK;
  1490. reg |= nump << DWC3_DCFG_NUMP_SHIFT;
  1491. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1492. }
  1493. static int __dwc3_gadget_start(struct dwc3 *dwc)
  1494. {
  1495. struct dwc3_ep *dep;
  1496. int ret = 0;
  1497. u32 reg;
  1498. /*
  1499. * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
  1500. * the core supports IMOD, disable it.
  1501. */
  1502. if (dwc->imod_interval) {
  1503. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  1504. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  1505. } else if (dwc3_has_imod(dwc)) {
  1506. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
  1507. }
  1508. /*
  1509. * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
  1510. * field instead of letting dwc3 itself calculate that automatically.
  1511. *
  1512. * This way, we maximize the chances that we'll be able to get several
  1513. * bursts of data without going through any sort of endpoint throttling.
  1514. */
  1515. reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
  1516. reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
  1517. dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
  1518. dwc3_gadget_setup_nump(dwc);
  1519. /* Start with SuperSpeed Default */
  1520. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1521. dep = dwc->eps[0];
  1522. ret = __dwc3_gadget_ep_enable(dep, false, false);
  1523. if (ret) {
  1524. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1525. goto err0;
  1526. }
  1527. dep = dwc->eps[1];
  1528. ret = __dwc3_gadget_ep_enable(dep, false, false);
  1529. if (ret) {
  1530. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1531. goto err1;
  1532. }
  1533. /* begin to receive SETUP packets */
  1534. dwc->ep0state = EP0_SETUP_PHASE;
  1535. dwc3_ep0_out_start(dwc);
  1536. dwc3_gadget_enable_irq(dwc);
  1537. return 0;
  1538. err1:
  1539. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1540. err0:
  1541. return ret;
  1542. }
  1543. static int dwc3_gadget_start(struct usb_gadget *g,
  1544. struct usb_gadget_driver *driver)
  1545. {
  1546. struct dwc3 *dwc = gadget_to_dwc(g);
  1547. unsigned long flags;
  1548. int ret = 0;
  1549. int irq;
  1550. irq = dwc->irq_gadget;
  1551. ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
  1552. IRQF_SHARED, "dwc3", dwc->ev_buf);
  1553. if (ret) {
  1554. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  1555. irq, ret);
  1556. goto err0;
  1557. }
  1558. spin_lock_irqsave(&dwc->lock, flags);
  1559. if (dwc->gadget_driver) {
  1560. dev_err(dwc->dev, "%s is already bound to %s\n",
  1561. dwc->gadget.name,
  1562. dwc->gadget_driver->driver.name);
  1563. ret = -EBUSY;
  1564. goto err1;
  1565. }
  1566. dwc->gadget_driver = driver;
  1567. if (pm_runtime_active(dwc->dev))
  1568. __dwc3_gadget_start(dwc);
  1569. spin_unlock_irqrestore(&dwc->lock, flags);
  1570. return 0;
  1571. err1:
  1572. spin_unlock_irqrestore(&dwc->lock, flags);
  1573. free_irq(irq, dwc);
  1574. err0:
  1575. return ret;
  1576. }
  1577. static void __dwc3_gadget_stop(struct dwc3 *dwc)
  1578. {
  1579. dwc3_gadget_disable_irq(dwc);
  1580. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1581. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1582. }
  1583. static int dwc3_gadget_stop(struct usb_gadget *g)
  1584. {
  1585. struct dwc3 *dwc = gadget_to_dwc(g);
  1586. unsigned long flags;
  1587. int epnum;
  1588. spin_lock_irqsave(&dwc->lock, flags);
  1589. if (pm_runtime_suspended(dwc->dev))
  1590. goto out;
  1591. __dwc3_gadget_stop(dwc);
  1592. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1593. struct dwc3_ep *dep = dwc->eps[epnum];
  1594. if (!dep)
  1595. continue;
  1596. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  1597. continue;
  1598. wait_event_lock_irq(dep->wait_end_transfer,
  1599. !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
  1600. dwc->lock);
  1601. }
  1602. out:
  1603. dwc->gadget_driver = NULL;
  1604. spin_unlock_irqrestore(&dwc->lock, flags);
  1605. free_irq(dwc->irq_gadget, dwc->ev_buf);
  1606. return 0;
  1607. }
  1608. static void dwc3_gadget_set_speed(struct usb_gadget *g,
  1609. enum usb_device_speed speed)
  1610. {
  1611. struct dwc3 *dwc = gadget_to_dwc(g);
  1612. unsigned long flags;
  1613. u32 reg;
  1614. spin_lock_irqsave(&dwc->lock, flags);
  1615. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1616. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1617. /*
  1618. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1619. * which would cause metastability state on Run/Stop
  1620. * bit if we try to force the IP to USB2-only mode.
  1621. *
  1622. * Because of that, we cannot configure the IP to any
  1623. * speed other than the SuperSpeed
  1624. *
  1625. * Refers to:
  1626. *
  1627. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1628. * USB 2.0 Mode
  1629. */
  1630. if (dwc->revision < DWC3_REVISION_220A &&
  1631. !dwc->dis_metastability_quirk) {
  1632. reg |= DWC3_DCFG_SUPERSPEED;
  1633. } else {
  1634. switch (speed) {
  1635. case USB_SPEED_LOW:
  1636. reg |= DWC3_DCFG_LOWSPEED;
  1637. break;
  1638. case USB_SPEED_FULL:
  1639. reg |= DWC3_DCFG_FULLSPEED;
  1640. break;
  1641. case USB_SPEED_HIGH:
  1642. reg |= DWC3_DCFG_HIGHSPEED;
  1643. break;
  1644. case USB_SPEED_SUPER:
  1645. reg |= DWC3_DCFG_SUPERSPEED;
  1646. break;
  1647. case USB_SPEED_SUPER_PLUS:
  1648. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  1649. break;
  1650. default:
  1651. dev_err(dwc->dev, "invalid speed (%d)\n", speed);
  1652. if (dwc->revision & DWC3_REVISION_IS_DWC31)
  1653. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  1654. else
  1655. reg |= DWC3_DCFG_SUPERSPEED;
  1656. }
  1657. }
  1658. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1659. spin_unlock_irqrestore(&dwc->lock, flags);
  1660. }
  1661. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1662. .get_frame = dwc3_gadget_get_frame,
  1663. .wakeup = dwc3_gadget_wakeup,
  1664. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1665. .pullup = dwc3_gadget_pullup,
  1666. .udc_start = dwc3_gadget_start,
  1667. .udc_stop = dwc3_gadget_stop,
  1668. .udc_set_speed = dwc3_gadget_set_speed,
  1669. };
  1670. /* -------------------------------------------------------------------------- */
  1671. static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
  1672. {
  1673. struct dwc3_ep *dep;
  1674. u8 epnum;
  1675. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1676. for (epnum = 0; epnum < total; epnum++) {
  1677. bool direction = epnum & 1;
  1678. u8 num = epnum >> 1;
  1679. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1680. if (!dep)
  1681. return -ENOMEM;
  1682. dep->dwc = dwc;
  1683. dep->number = epnum;
  1684. dep->direction = direction;
  1685. dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
  1686. dwc->eps[epnum] = dep;
  1687. snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
  1688. direction ? "in" : "out");
  1689. dep->endpoint.name = dep->name;
  1690. if (!(dep->number > 1)) {
  1691. dep->endpoint.desc = &dwc3_gadget_ep0_desc;
  1692. dep->endpoint.comp_desc = NULL;
  1693. }
  1694. spin_lock_init(&dep->lock);
  1695. if (num == 0) {
  1696. usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
  1697. dep->endpoint.maxburst = 1;
  1698. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1699. if (!direction)
  1700. dwc->gadget.ep0 = &dep->endpoint;
  1701. } else if (direction) {
  1702. int mdwidth;
  1703. int kbytes;
  1704. int size;
  1705. int ret;
  1706. mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
  1707. /* MDWIDTH is represented in bits, we need it in bytes */
  1708. mdwidth /= 8;
  1709. size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
  1710. size = DWC3_GTXFIFOSIZ_TXFDEF(size);
  1711. /* FIFO Depth is in MDWDITH bytes. Multiply */
  1712. size *= mdwidth;
  1713. kbytes = size / 1024;
  1714. if (kbytes == 0)
  1715. kbytes = 1;
  1716. /*
  1717. * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
  1718. * internal overhead. We don't really know how these are used,
  1719. * but documentation say it exists.
  1720. */
  1721. size -= mdwidth * (kbytes + 1);
  1722. size /= kbytes;
  1723. usb_ep_set_maxpacket_limit(&dep->endpoint, size);
  1724. dep->endpoint.max_streams = 15;
  1725. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1726. list_add_tail(&dep->endpoint.ep_list,
  1727. &dwc->gadget.ep_list);
  1728. ret = dwc3_alloc_trb_pool(dep);
  1729. if (ret)
  1730. return ret;
  1731. } else {
  1732. int ret;
  1733. usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
  1734. dep->endpoint.max_streams = 15;
  1735. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1736. list_add_tail(&dep->endpoint.ep_list,
  1737. &dwc->gadget.ep_list);
  1738. ret = dwc3_alloc_trb_pool(dep);
  1739. if (ret)
  1740. return ret;
  1741. }
  1742. if (num == 0) {
  1743. dep->endpoint.caps.type_control = true;
  1744. } else {
  1745. dep->endpoint.caps.type_iso = true;
  1746. dep->endpoint.caps.type_bulk = true;
  1747. dep->endpoint.caps.type_int = true;
  1748. }
  1749. dep->endpoint.caps.dir_in = direction;
  1750. dep->endpoint.caps.dir_out = !direction;
  1751. INIT_LIST_HEAD(&dep->pending_list);
  1752. INIT_LIST_HEAD(&dep->started_list);
  1753. }
  1754. return 0;
  1755. }
  1756. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1757. {
  1758. struct dwc3_ep *dep;
  1759. u8 epnum;
  1760. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1761. dep = dwc->eps[epnum];
  1762. if (!dep)
  1763. continue;
  1764. /*
  1765. * Physical endpoints 0 and 1 are special; they form the
  1766. * bi-directional USB endpoint 0.
  1767. *
  1768. * For those two physical endpoints, we don't allocate a TRB
  1769. * pool nor do we add them the endpoints list. Due to that, we
  1770. * shouldn't do these two operations otherwise we would end up
  1771. * with all sorts of bugs when removing dwc3.ko.
  1772. */
  1773. if (epnum != 0 && epnum != 1) {
  1774. dwc3_free_trb_pool(dep);
  1775. list_del(&dep->endpoint.ep_list);
  1776. }
  1777. kfree(dep);
  1778. }
  1779. }
  1780. /* -------------------------------------------------------------------------- */
  1781. static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1782. struct dwc3_request *req, struct dwc3_trb *trb,
  1783. const struct dwc3_event_depevt *event, int status,
  1784. int chain)
  1785. {
  1786. unsigned int count;
  1787. unsigned int s_pkt = 0;
  1788. unsigned int trb_status;
  1789. dwc3_ep_inc_deq(dep);
  1790. if (req->trb == trb)
  1791. dep->queued_requests--;
  1792. trace_dwc3_complete_trb(dep, trb);
  1793. /*
  1794. * If we're in the middle of series of chained TRBs and we
  1795. * receive a short transfer along the way, DWC3 will skip
  1796. * through all TRBs including the last TRB in the chain (the
  1797. * where CHN bit is zero. DWC3 will also avoid clearing HWO
  1798. * bit and SW has to do it manually.
  1799. *
  1800. * We're going to do that here to avoid problems of HW trying
  1801. * to use bogus TRBs for transfers.
  1802. */
  1803. if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
  1804. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1805. /*
  1806. * If we're dealing with unaligned size OUT transfer, we will be left
  1807. * with one TRB pending in the ring. We need to manually clear HWO bit
  1808. * from that TRB.
  1809. */
  1810. if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
  1811. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1812. return 1;
  1813. }
  1814. count = trb->size & DWC3_TRB_SIZE_MASK;
  1815. req->remaining += count;
  1816. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1817. return 1;
  1818. if (dep->direction) {
  1819. if (count) {
  1820. trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  1821. if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
  1822. /*
  1823. * If missed isoc occurred and there is
  1824. * no request queued then issue END
  1825. * TRANSFER, so that core generates
  1826. * next xfernotready and we will issue
  1827. * a fresh START TRANSFER.
  1828. * If there are still queued request
  1829. * then wait, do not issue either END
  1830. * or UPDATE TRANSFER, just attach next
  1831. * request in pending_list during
  1832. * giveback.If any future queued request
  1833. * is successfully transferred then we
  1834. * will issue UPDATE TRANSFER for all
  1835. * request in the pending_list.
  1836. */
  1837. dep->flags |= DWC3_EP_MISSED_ISOC;
  1838. } else {
  1839. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1840. dep->name);
  1841. status = -ECONNRESET;
  1842. }
  1843. } else {
  1844. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  1845. }
  1846. } else {
  1847. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1848. s_pkt = 1;
  1849. }
  1850. if (s_pkt && !chain)
  1851. return 1;
  1852. if ((event->status & DEPEVT_STATUS_IOC) &&
  1853. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1854. return 1;
  1855. return 0;
  1856. }
  1857. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1858. const struct dwc3_event_depevt *event, int status)
  1859. {
  1860. struct dwc3_request *req, *n;
  1861. struct dwc3_trb *trb;
  1862. bool ioc = false;
  1863. int ret = 0;
  1864. list_for_each_entry_safe(req, n, &dep->started_list, list) {
  1865. unsigned length;
  1866. int chain;
  1867. length = req->request.length;
  1868. chain = req->num_pending_sgs > 0;
  1869. if (chain) {
  1870. struct scatterlist *sg = req->sg;
  1871. struct scatterlist *s;
  1872. unsigned int pending = req->num_pending_sgs;
  1873. unsigned int i;
  1874. for_each_sg(sg, s, pending, i) {
  1875. trb = &dep->trb_pool[dep->trb_dequeue];
  1876. if (trb->ctrl & DWC3_TRB_CTRL_HWO)
  1877. break;
  1878. req->sg = sg_next(s);
  1879. req->num_pending_sgs--;
  1880. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1881. event, status, chain);
  1882. if (ret)
  1883. break;
  1884. }
  1885. } else {
  1886. trb = &dep->trb_pool[dep->trb_dequeue];
  1887. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1888. event, status, chain);
  1889. }
  1890. if (req->unaligned || req->zero) {
  1891. trb = &dep->trb_pool[dep->trb_dequeue];
  1892. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1893. event, status, false);
  1894. req->unaligned = false;
  1895. req->zero = false;
  1896. }
  1897. req->request.actual = length - req->remaining;
  1898. if ((req->request.actual < length) && req->num_pending_sgs)
  1899. return __dwc3_gadget_kick_transfer(dep);
  1900. dwc3_gadget_giveback(dep, req, status);
  1901. if (ret) {
  1902. if ((event->status & DEPEVT_STATUS_IOC) &&
  1903. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1904. ioc = true;
  1905. break;
  1906. }
  1907. }
  1908. /*
  1909. * Our endpoint might get disabled by another thread during
  1910. * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
  1911. * early on so DWC3_EP_BUSY flag gets cleared
  1912. */
  1913. if (!dep->endpoint.desc)
  1914. return 1;
  1915. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  1916. list_empty(&dep->started_list)) {
  1917. if (list_empty(&dep->pending_list)) {
  1918. /*
  1919. * If there is no entry in request list then do
  1920. * not issue END TRANSFER now. Just set PENDING
  1921. * flag, so that END TRANSFER is issued when an
  1922. * entry is added into request list.
  1923. */
  1924. dep->flags = DWC3_EP_PENDING_REQUEST;
  1925. } else {
  1926. dwc3_stop_active_transfer(dwc, dep->number, true);
  1927. dep->flags = DWC3_EP_ENABLED;
  1928. }
  1929. return 1;
  1930. }
  1931. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
  1932. return 0;
  1933. return 1;
  1934. }
  1935. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1936. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  1937. {
  1938. unsigned status = 0;
  1939. int clean_busy;
  1940. u32 is_xfer_complete;
  1941. is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
  1942. if (event->status & DEPEVT_STATUS_BUSERR)
  1943. status = -ECONNRESET;
  1944. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  1945. if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
  1946. usb_endpoint_xfer_isoc(dep->endpoint.desc)))
  1947. dep->flags &= ~DWC3_EP_BUSY;
  1948. /*
  1949. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  1950. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  1951. */
  1952. if (dwc->revision < DWC3_REVISION_183A) {
  1953. u32 reg;
  1954. int i;
  1955. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  1956. dep = dwc->eps[i];
  1957. if (!(dep->flags & DWC3_EP_ENABLED))
  1958. continue;
  1959. if (!list_empty(&dep->started_list))
  1960. return;
  1961. }
  1962. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1963. reg |= dwc->u1u2;
  1964. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1965. dwc->u1u2 = 0;
  1966. }
  1967. /*
  1968. * Our endpoint might get disabled by another thread during
  1969. * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
  1970. * early on so DWC3_EP_BUSY flag gets cleared
  1971. */
  1972. if (!dep->endpoint.desc)
  1973. return;
  1974. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc))
  1975. __dwc3_gadget_kick_transfer(dep);
  1976. }
  1977. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  1978. const struct dwc3_event_depevt *event)
  1979. {
  1980. struct dwc3_ep *dep;
  1981. u8 epnum = event->endpoint_number;
  1982. u8 cmd;
  1983. dep = dwc->eps[epnum];
  1984. if (!(dep->flags & DWC3_EP_ENABLED)) {
  1985. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  1986. return;
  1987. /* Handle only EPCMDCMPLT when EP disabled */
  1988. if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
  1989. return;
  1990. }
  1991. if (epnum == 0 || epnum == 1) {
  1992. dwc3_ep0_interrupt(dwc, event);
  1993. return;
  1994. }
  1995. switch (event->endpoint_event) {
  1996. case DWC3_DEPEVT_XFERCOMPLETE:
  1997. dep->resource_index = 0;
  1998. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1999. dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
  2000. return;
  2001. }
  2002. dwc3_endpoint_transfer_complete(dwc, dep, event);
  2003. break;
  2004. case DWC3_DEPEVT_XFERINPROGRESS:
  2005. dwc3_endpoint_transfer_complete(dwc, dep, event);
  2006. break;
  2007. case DWC3_DEPEVT_XFERNOTREADY:
  2008. if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
  2009. dwc3_gadget_start_isoc(dwc, dep, event);
  2010. else
  2011. __dwc3_gadget_kick_transfer(dep);
  2012. break;
  2013. case DWC3_DEPEVT_STREAMEVT:
  2014. if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
  2015. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  2016. dep->name);
  2017. return;
  2018. }
  2019. break;
  2020. case DWC3_DEPEVT_EPCMDCMPLT:
  2021. cmd = DEPEVT_PARAMETER_CMD(event->parameters);
  2022. if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
  2023. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  2024. wake_up(&dep->wait_end_transfer);
  2025. }
  2026. break;
  2027. case DWC3_DEPEVT_RXTXFIFOEVT:
  2028. break;
  2029. }
  2030. }
  2031. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  2032. {
  2033. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  2034. spin_unlock(&dwc->lock);
  2035. dwc->gadget_driver->disconnect(&dwc->gadget);
  2036. spin_lock(&dwc->lock);
  2037. }
  2038. }
  2039. static void dwc3_suspend_gadget(struct dwc3 *dwc)
  2040. {
  2041. if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
  2042. spin_unlock(&dwc->lock);
  2043. dwc->gadget_driver->suspend(&dwc->gadget);
  2044. spin_lock(&dwc->lock);
  2045. }
  2046. }
  2047. static void dwc3_resume_gadget(struct dwc3 *dwc)
  2048. {
  2049. if (dwc->gadget_driver && dwc->gadget_driver->resume) {
  2050. spin_unlock(&dwc->lock);
  2051. dwc->gadget_driver->resume(&dwc->gadget);
  2052. spin_lock(&dwc->lock);
  2053. }
  2054. }
  2055. static void dwc3_reset_gadget(struct dwc3 *dwc)
  2056. {
  2057. if (!dwc->gadget_driver)
  2058. return;
  2059. if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
  2060. spin_unlock(&dwc->lock);
  2061. usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
  2062. spin_lock(&dwc->lock);
  2063. }
  2064. }
  2065. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
  2066. {
  2067. struct dwc3_ep *dep;
  2068. struct dwc3_gadget_ep_cmd_params params;
  2069. u32 cmd;
  2070. int ret;
  2071. dep = dwc->eps[epnum];
  2072. if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
  2073. !dep->resource_index)
  2074. return;
  2075. /*
  2076. * NOTICE: We are violating what the Databook says about the
  2077. * EndTransfer command. Ideally we would _always_ wait for the
  2078. * EndTransfer Command Completion IRQ, but that's causing too
  2079. * much trouble synchronizing between us and gadget driver.
  2080. *
  2081. * We have discussed this with the IP Provider and it was
  2082. * suggested to giveback all requests here, but give HW some
  2083. * extra time to synchronize with the interconnect. We're using
  2084. * an arbitrary 100us delay for that.
  2085. *
  2086. * Note also that a similar handling was tested by Synopsys
  2087. * (thanks a lot Paul) and nothing bad has come out of it.
  2088. * In short, what we're doing is:
  2089. *
  2090. * - Issue EndTransfer WITH CMDIOC bit set
  2091. * - Wait 100us
  2092. *
  2093. * As of IP version 3.10a of the DWC_usb3 IP, the controller
  2094. * supports a mode to work around the above limitation. The
  2095. * software can poll the CMDACT bit in the DEPCMD register
  2096. * after issuing a EndTransfer command. This mode is enabled
  2097. * by writing GUCTL2[14]. This polling is already done in the
  2098. * dwc3_send_gadget_ep_cmd() function so if the mode is
  2099. * enabled, the EndTransfer command will have completed upon
  2100. * returning from this function and we don't need to delay for
  2101. * 100us.
  2102. *
  2103. * This mode is NOT available on the DWC_usb31 IP.
  2104. */
  2105. cmd = DWC3_DEPCMD_ENDTRANSFER;
  2106. cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
  2107. cmd |= DWC3_DEPCMD_CMDIOC;
  2108. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  2109. memset(&params, 0, sizeof(params));
  2110. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  2111. WARN_ON_ONCE(ret);
  2112. dep->resource_index = 0;
  2113. dep->flags &= ~DWC3_EP_BUSY;
  2114. if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
  2115. dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
  2116. udelay(100);
  2117. }
  2118. }
  2119. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  2120. {
  2121. u32 epnum;
  2122. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  2123. struct dwc3_ep *dep;
  2124. int ret;
  2125. dep = dwc->eps[epnum];
  2126. if (!dep)
  2127. continue;
  2128. if (!(dep->flags & DWC3_EP_STALL))
  2129. continue;
  2130. dep->flags &= ~DWC3_EP_STALL;
  2131. ret = dwc3_send_clear_stall_ep_cmd(dep);
  2132. WARN_ON_ONCE(ret);
  2133. }
  2134. }
  2135. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  2136. {
  2137. int reg;
  2138. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2139. reg &= ~DWC3_DCTL_INITU1ENA;
  2140. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2141. reg &= ~DWC3_DCTL_INITU2ENA;
  2142. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2143. dwc3_disconnect_gadget(dwc);
  2144. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2145. dwc->setup_packet_pending = false;
  2146. usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
  2147. dwc->connected = false;
  2148. }
  2149. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  2150. {
  2151. u32 reg;
  2152. dwc->connected = true;
  2153. /*
  2154. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  2155. * would cause a missing Disconnect Event if there's a
  2156. * pending Setup Packet in the FIFO.
  2157. *
  2158. * There's no suggested workaround on the official Bug
  2159. * report, which states that "unless the driver/application
  2160. * is doing any special handling of a disconnect event,
  2161. * there is no functional issue".
  2162. *
  2163. * Unfortunately, it turns out that we _do_ some special
  2164. * handling of a disconnect event, namely complete all
  2165. * pending transfers, notify gadget driver of the
  2166. * disconnection, and so on.
  2167. *
  2168. * Our suggested workaround is to follow the Disconnect
  2169. * Event steps here, instead, based on a setup_packet_pending
  2170. * flag. Such flag gets set whenever we have a SETUP_PENDING
  2171. * status for EP0 TRBs and gets cleared on XferComplete for the
  2172. * same endpoint.
  2173. *
  2174. * Refers to:
  2175. *
  2176. * STAR#9000466709: RTL: Device : Disconnect event not
  2177. * generated if setup packet pending in FIFO
  2178. */
  2179. if (dwc->revision < DWC3_REVISION_188A) {
  2180. if (dwc->setup_packet_pending)
  2181. dwc3_gadget_disconnect_interrupt(dwc);
  2182. }
  2183. dwc3_reset_gadget(dwc);
  2184. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2185. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  2186. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2187. dwc->test_mode = false;
  2188. dwc3_clear_stall_all_ep(dwc);
  2189. /* Reset device address to zero */
  2190. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2191. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  2192. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2193. }
  2194. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  2195. {
  2196. struct dwc3_ep *dep;
  2197. int ret;
  2198. u32 reg;
  2199. u8 speed;
  2200. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  2201. speed = reg & DWC3_DSTS_CONNECTSPD;
  2202. dwc->speed = speed;
  2203. /*
  2204. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  2205. * each time on Connect Done.
  2206. *
  2207. * Currently we always use the reset value. If any platform
  2208. * wants to set this to a different value, we need to add a
  2209. * setting and update GCTL.RAMCLKSEL here.
  2210. */
  2211. switch (speed) {
  2212. case DWC3_DSTS_SUPERSPEED_PLUS:
  2213. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2214. dwc->gadget.ep0->maxpacket = 512;
  2215. dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
  2216. break;
  2217. case DWC3_DSTS_SUPERSPEED:
  2218. /*
  2219. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  2220. * would cause a missing USB3 Reset event.
  2221. *
  2222. * In such situations, we should force a USB3 Reset
  2223. * event by calling our dwc3_gadget_reset_interrupt()
  2224. * routine.
  2225. *
  2226. * Refers to:
  2227. *
  2228. * STAR#9000483510: RTL: SS : USB3 reset event may
  2229. * not be generated always when the link enters poll
  2230. */
  2231. if (dwc->revision < DWC3_REVISION_190A)
  2232. dwc3_gadget_reset_interrupt(dwc);
  2233. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2234. dwc->gadget.ep0->maxpacket = 512;
  2235. dwc->gadget.speed = USB_SPEED_SUPER;
  2236. break;
  2237. case DWC3_DSTS_HIGHSPEED:
  2238. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2239. dwc->gadget.ep0->maxpacket = 64;
  2240. dwc->gadget.speed = USB_SPEED_HIGH;
  2241. break;
  2242. case DWC3_DSTS_FULLSPEED:
  2243. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2244. dwc->gadget.ep0->maxpacket = 64;
  2245. dwc->gadget.speed = USB_SPEED_FULL;
  2246. break;
  2247. case DWC3_DSTS_LOWSPEED:
  2248. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  2249. dwc->gadget.ep0->maxpacket = 8;
  2250. dwc->gadget.speed = USB_SPEED_LOW;
  2251. break;
  2252. }
  2253. /* Enable USB2 LPM Capability */
  2254. if ((dwc->revision > DWC3_REVISION_194A) &&
  2255. (speed != DWC3_DSTS_SUPERSPEED) &&
  2256. (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
  2257. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2258. reg |= DWC3_DCFG_LPM_CAP;
  2259. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2260. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2261. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  2262. reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
  2263. /*
  2264. * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
  2265. * DCFG.LPMCap is set, core responses with an ACK and the
  2266. * BESL value in the LPM token is less than or equal to LPM
  2267. * NYET threshold.
  2268. */
  2269. WARN_ONCE(dwc->revision < DWC3_REVISION_240A
  2270. && dwc->has_lpm_erratum,
  2271. "LPM Erratum not available on dwc3 revisions < 2.40a\n");
  2272. if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
  2273. reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
  2274. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2275. } else {
  2276. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2277. reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
  2278. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2279. }
  2280. dep = dwc->eps[0];
  2281. ret = __dwc3_gadget_ep_enable(dep, true, false);
  2282. if (ret) {
  2283. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2284. return;
  2285. }
  2286. dep = dwc->eps[1];
  2287. ret = __dwc3_gadget_ep_enable(dep, true, false);
  2288. if (ret) {
  2289. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2290. return;
  2291. }
  2292. /*
  2293. * Configure PHY via GUSB3PIPECTLn if required.
  2294. *
  2295. * Update GTXFIFOSIZn
  2296. *
  2297. * In both cases reset values should be sufficient.
  2298. */
  2299. }
  2300. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  2301. {
  2302. /*
  2303. * TODO take core out of low power mode when that's
  2304. * implemented.
  2305. */
  2306. if (dwc->gadget_driver && dwc->gadget_driver->resume) {
  2307. spin_unlock(&dwc->lock);
  2308. dwc->gadget_driver->resume(&dwc->gadget);
  2309. spin_lock(&dwc->lock);
  2310. }
  2311. }
  2312. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  2313. unsigned int evtinfo)
  2314. {
  2315. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2316. unsigned int pwropt;
  2317. /*
  2318. * WORKAROUND: DWC3 < 2.50a have an issue when configured without
  2319. * Hibernation mode enabled which would show up when device detects
  2320. * host-initiated U3 exit.
  2321. *
  2322. * In that case, device will generate a Link State Change Interrupt
  2323. * from U3 to RESUME which is only necessary if Hibernation is
  2324. * configured in.
  2325. *
  2326. * There are no functional changes due to such spurious event and we
  2327. * just need to ignore it.
  2328. *
  2329. * Refers to:
  2330. *
  2331. * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
  2332. * operational mode
  2333. */
  2334. pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
  2335. if ((dwc->revision < DWC3_REVISION_250A) &&
  2336. (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
  2337. if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
  2338. (next == DWC3_LINK_STATE_RESUME)) {
  2339. return;
  2340. }
  2341. }
  2342. /*
  2343. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  2344. * on the link partner, the USB session might do multiple entry/exit
  2345. * of low power states before a transfer takes place.
  2346. *
  2347. * Due to this problem, we might experience lower throughput. The
  2348. * suggested workaround is to disable DCTL[12:9] bits if we're
  2349. * transitioning from U1/U2 to U0 and enable those bits again
  2350. * after a transfer completes and there are no pending transfers
  2351. * on any of the enabled endpoints.
  2352. *
  2353. * This is the first half of that workaround.
  2354. *
  2355. * Refers to:
  2356. *
  2357. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  2358. * core send LGO_Ux entering U0
  2359. */
  2360. if (dwc->revision < DWC3_REVISION_183A) {
  2361. if (next == DWC3_LINK_STATE_U0) {
  2362. u32 u1u2;
  2363. u32 reg;
  2364. switch (dwc->link_state) {
  2365. case DWC3_LINK_STATE_U1:
  2366. case DWC3_LINK_STATE_U2:
  2367. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2368. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  2369. | DWC3_DCTL_ACCEPTU2ENA
  2370. | DWC3_DCTL_INITU1ENA
  2371. | DWC3_DCTL_ACCEPTU1ENA);
  2372. if (!dwc->u1u2)
  2373. dwc->u1u2 = reg & u1u2;
  2374. reg &= ~u1u2;
  2375. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2376. break;
  2377. default:
  2378. /* do nothing */
  2379. break;
  2380. }
  2381. }
  2382. }
  2383. switch (next) {
  2384. case DWC3_LINK_STATE_U1:
  2385. if (dwc->speed == USB_SPEED_SUPER)
  2386. dwc3_suspend_gadget(dwc);
  2387. break;
  2388. case DWC3_LINK_STATE_U2:
  2389. case DWC3_LINK_STATE_U3:
  2390. dwc3_suspend_gadget(dwc);
  2391. break;
  2392. case DWC3_LINK_STATE_RESUME:
  2393. dwc3_resume_gadget(dwc);
  2394. break;
  2395. default:
  2396. /* do nothing */
  2397. break;
  2398. }
  2399. dwc->link_state = next;
  2400. }
  2401. static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
  2402. unsigned int evtinfo)
  2403. {
  2404. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2405. if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
  2406. dwc3_suspend_gadget(dwc);
  2407. dwc->link_state = next;
  2408. }
  2409. static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
  2410. unsigned int evtinfo)
  2411. {
  2412. unsigned int is_ss = evtinfo & BIT(4);
  2413. /*
  2414. * WORKAROUND: DWC3 revison 2.20a with hibernation support
  2415. * have a known issue which can cause USB CV TD.9.23 to fail
  2416. * randomly.
  2417. *
  2418. * Because of this issue, core could generate bogus hibernation
  2419. * events which SW needs to ignore.
  2420. *
  2421. * Refers to:
  2422. *
  2423. * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
  2424. * Device Fallback from SuperSpeed
  2425. */
  2426. if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
  2427. return;
  2428. /* enter hibernation here */
  2429. }
  2430. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  2431. const struct dwc3_event_devt *event)
  2432. {
  2433. switch (event->type) {
  2434. case DWC3_DEVICE_EVENT_DISCONNECT:
  2435. dwc3_gadget_disconnect_interrupt(dwc);
  2436. break;
  2437. case DWC3_DEVICE_EVENT_RESET:
  2438. dwc3_gadget_reset_interrupt(dwc);
  2439. break;
  2440. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  2441. dwc3_gadget_conndone_interrupt(dwc);
  2442. break;
  2443. case DWC3_DEVICE_EVENT_WAKEUP:
  2444. dwc3_gadget_wakeup_interrupt(dwc);
  2445. break;
  2446. case DWC3_DEVICE_EVENT_HIBER_REQ:
  2447. if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
  2448. "unexpected hibernation event\n"))
  2449. break;
  2450. dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
  2451. break;
  2452. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  2453. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  2454. break;
  2455. case DWC3_DEVICE_EVENT_EOPF:
  2456. /* It changed to be suspend event for version 2.30a and above */
  2457. if (dwc->revision >= DWC3_REVISION_230A) {
  2458. /*
  2459. * Ignore suspend event until the gadget enters into
  2460. * USB_STATE_CONFIGURED state.
  2461. */
  2462. if (dwc->gadget.state >= USB_STATE_CONFIGURED)
  2463. dwc3_gadget_suspend_interrupt(dwc,
  2464. event->event_info);
  2465. }
  2466. break;
  2467. case DWC3_DEVICE_EVENT_SOF:
  2468. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  2469. case DWC3_DEVICE_EVENT_CMD_CMPL:
  2470. case DWC3_DEVICE_EVENT_OVERFLOW:
  2471. break;
  2472. default:
  2473. dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  2474. }
  2475. }
  2476. static void dwc3_process_event_entry(struct dwc3 *dwc,
  2477. const union dwc3_event *event)
  2478. {
  2479. trace_dwc3_event(event->raw, dwc);
  2480. if (!event->type.is_devspec)
  2481. dwc3_endpoint_interrupt(dwc, &event->depevt);
  2482. else if (event->type.type == DWC3_EVENT_TYPE_DEV)
  2483. dwc3_gadget_interrupt(dwc, &event->devt);
  2484. else
  2485. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  2486. }
  2487. static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
  2488. {
  2489. struct dwc3 *dwc = evt->dwc;
  2490. irqreturn_t ret = IRQ_NONE;
  2491. int left;
  2492. u32 reg;
  2493. left = evt->count;
  2494. if (!(evt->flags & DWC3_EVENT_PENDING))
  2495. return IRQ_NONE;
  2496. while (left > 0) {
  2497. union dwc3_event event;
  2498. event.raw = *(u32 *) (evt->cache + evt->lpos);
  2499. dwc3_process_event_entry(dwc, &event);
  2500. /*
  2501. * FIXME we wrap around correctly to the next entry as
  2502. * almost all entries are 4 bytes in size. There is one
  2503. * entry which has 12 bytes which is a regular entry
  2504. * followed by 8 bytes data. ATM I don't know how
  2505. * things are organized if we get next to the a
  2506. * boundary so I worry about that once we try to handle
  2507. * that.
  2508. */
  2509. evt->lpos = (evt->lpos + 4) % evt->length;
  2510. left -= 4;
  2511. }
  2512. evt->count = 0;
  2513. evt->flags &= ~DWC3_EVENT_PENDING;
  2514. ret = IRQ_HANDLED;
  2515. /* Unmask interrupt */
  2516. reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
  2517. reg &= ~DWC3_GEVNTSIZ_INTMASK;
  2518. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
  2519. if (dwc->imod_interval) {
  2520. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  2521. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  2522. }
  2523. return ret;
  2524. }
  2525. static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
  2526. {
  2527. struct dwc3_event_buffer *evt = _evt;
  2528. struct dwc3 *dwc = evt->dwc;
  2529. unsigned long flags;
  2530. irqreturn_t ret = IRQ_NONE;
  2531. spin_lock_irqsave(&dwc->lock, flags);
  2532. ret = dwc3_process_event_buf(evt);
  2533. spin_unlock_irqrestore(&dwc->lock, flags);
  2534. return ret;
  2535. }
  2536. static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
  2537. {
  2538. struct dwc3 *dwc = evt->dwc;
  2539. u32 amount;
  2540. u32 count;
  2541. u32 reg;
  2542. if (pm_runtime_suspended(dwc->dev)) {
  2543. pm_runtime_get(dwc->dev);
  2544. disable_irq_nosync(dwc->irq_gadget);
  2545. dwc->pending_events = true;
  2546. return IRQ_HANDLED;
  2547. }
  2548. /*
  2549. * With PCIe legacy interrupt, test shows that top-half irq handler can
  2550. * be called again after HW interrupt deassertion. Check if bottom-half
  2551. * irq event handler completes before caching new event to prevent
  2552. * losing events.
  2553. */
  2554. if (evt->flags & DWC3_EVENT_PENDING)
  2555. return IRQ_HANDLED;
  2556. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
  2557. count &= DWC3_GEVNTCOUNT_MASK;
  2558. if (!count)
  2559. return IRQ_NONE;
  2560. evt->count = count;
  2561. evt->flags |= DWC3_EVENT_PENDING;
  2562. /* Mask interrupt */
  2563. reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
  2564. reg |= DWC3_GEVNTSIZ_INTMASK;
  2565. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
  2566. amount = min(count, evt->length - evt->lpos);
  2567. memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
  2568. if (amount < count)
  2569. memcpy(evt->cache, evt->buf, count - amount);
  2570. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
  2571. return IRQ_WAKE_THREAD;
  2572. }
  2573. static irqreturn_t dwc3_interrupt(int irq, void *_evt)
  2574. {
  2575. struct dwc3_event_buffer *evt = _evt;
  2576. return dwc3_check_event_buf(evt);
  2577. }
  2578. static int dwc3_gadget_get_irq(struct dwc3 *dwc)
  2579. {
  2580. struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
  2581. int irq;
  2582. irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
  2583. if (irq > 0)
  2584. goto out;
  2585. if (irq == -EPROBE_DEFER)
  2586. goto out;
  2587. irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
  2588. if (irq > 0)
  2589. goto out;
  2590. if (irq == -EPROBE_DEFER)
  2591. goto out;
  2592. irq = platform_get_irq(dwc3_pdev, 0);
  2593. if (irq > 0)
  2594. goto out;
  2595. if (irq != -EPROBE_DEFER)
  2596. dev_err(dwc->dev, "missing peripheral IRQ\n");
  2597. if (!irq)
  2598. irq = -EINVAL;
  2599. out:
  2600. return irq;
  2601. }
  2602. /**
  2603. * dwc3_gadget_init - initializes gadget related registers
  2604. * @dwc: pointer to our controller context structure
  2605. *
  2606. * Returns 0 on success otherwise negative errno.
  2607. */
  2608. int dwc3_gadget_init(struct dwc3 *dwc)
  2609. {
  2610. int ret;
  2611. int irq;
  2612. irq = dwc3_gadget_get_irq(dwc);
  2613. if (irq < 0) {
  2614. ret = irq;
  2615. goto err0;
  2616. }
  2617. dwc->irq_gadget = irq;
  2618. dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
  2619. sizeof(*dwc->ep0_trb) * 2,
  2620. &dwc->ep0_trb_addr, GFP_KERNEL);
  2621. if (!dwc->ep0_trb) {
  2622. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  2623. ret = -ENOMEM;
  2624. goto err0;
  2625. }
  2626. dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
  2627. if (!dwc->setup_buf) {
  2628. ret = -ENOMEM;
  2629. goto err1;
  2630. }
  2631. dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
  2632. &dwc->bounce_addr, GFP_KERNEL);
  2633. if (!dwc->bounce) {
  2634. ret = -ENOMEM;
  2635. goto err2;
  2636. }
  2637. init_completion(&dwc->ep0_in_setup);
  2638. dwc->gadget.ops = &dwc3_gadget_ops;
  2639. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2640. dwc->gadget.sg_supported = true;
  2641. dwc->gadget.name = "dwc3-gadget";
  2642. dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
  2643. /*
  2644. * FIXME We might be setting max_speed to <SUPER, however versions
  2645. * <2.20a of dwc3 have an issue with metastability (documented
  2646. * elsewhere in this driver) which tells us we can't set max speed to
  2647. * anything lower than SUPER.
  2648. *
  2649. * Because gadget.max_speed is only used by composite.c and function
  2650. * drivers (i.e. it won't go into dwc3's registers) we are allowing this
  2651. * to happen so we avoid sending SuperSpeed Capability descriptor
  2652. * together with our BOS descriptor as that could confuse host into
  2653. * thinking we can handle super speed.
  2654. *
  2655. * Note that, in fact, we won't even support GetBOS requests when speed
  2656. * is less than super speed because we don't have means, yet, to tell
  2657. * composite.c that we are USB 2.0 + LPM ECN.
  2658. */
  2659. if (dwc->revision < DWC3_REVISION_220A &&
  2660. !dwc->dis_metastability_quirk)
  2661. dev_info(dwc->dev, "changing max_speed on rev %08x\n",
  2662. dwc->revision);
  2663. dwc->gadget.max_speed = dwc->maximum_speed;
  2664. /*
  2665. * REVISIT: Here we should clear all pending IRQs to be
  2666. * sure we're starting from a well known location.
  2667. */
  2668. ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
  2669. if (ret)
  2670. goto err3;
  2671. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  2672. if (ret) {
  2673. dev_err(dwc->dev, "failed to register udc\n");
  2674. goto err4;
  2675. }
  2676. return 0;
  2677. err4:
  2678. dwc3_gadget_free_endpoints(dwc);
  2679. err3:
  2680. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  2681. dwc->bounce_addr);
  2682. err2:
  2683. kfree(dwc->setup_buf);
  2684. err1:
  2685. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  2686. dwc->ep0_trb, dwc->ep0_trb_addr);
  2687. err0:
  2688. return ret;
  2689. }
  2690. /* -------------------------------------------------------------------------- */
  2691. void dwc3_gadget_exit(struct dwc3 *dwc)
  2692. {
  2693. usb_del_gadget_udc(&dwc->gadget);
  2694. dwc3_gadget_free_endpoints(dwc);
  2695. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  2696. dwc->bounce_addr);
  2697. kfree(dwc->setup_buf);
  2698. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  2699. dwc->ep0_trb, dwc->ep0_trb_addr);
  2700. }
  2701. int dwc3_gadget_suspend(struct dwc3 *dwc)
  2702. {
  2703. if (!dwc->gadget_driver)
  2704. return 0;
  2705. dwc3_gadget_run_stop(dwc, false, false);
  2706. dwc3_disconnect_gadget(dwc);
  2707. __dwc3_gadget_stop(dwc);
  2708. return 0;
  2709. }
  2710. int dwc3_gadget_resume(struct dwc3 *dwc)
  2711. {
  2712. int ret;
  2713. if (!dwc->gadget_driver)
  2714. return 0;
  2715. ret = __dwc3_gadget_start(dwc);
  2716. if (ret < 0)
  2717. goto err0;
  2718. ret = dwc3_gadget_run_stop(dwc, true, false);
  2719. if (ret < 0)
  2720. goto err1;
  2721. return 0;
  2722. err1:
  2723. __dwc3_gadget_stop(dwc);
  2724. err0:
  2725. return ret;
  2726. }
  2727. void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
  2728. {
  2729. if (dwc->pending_events) {
  2730. dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
  2731. dwc->pending_events = false;
  2732. enable_irq(dwc->irq_gadget);
  2733. }
  2734. }