gadget.c 83 KB

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