gadget.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398
  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. 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 int length;
  811. dma_addr_t dma;
  812. unsigned stream_id = req->request.stream_id;
  813. unsigned short_not_ok = req->request.short_not_ok;
  814. unsigned no_interrupt = req->request.no_interrupt;
  815. if (req->request.num_sgs > 0) {
  816. length = sg_dma_len(req->start_sg);
  817. dma = sg_dma_address(req->start_sg);
  818. } else {
  819. length = req->request.length;
  820. dma = req->request.dma;
  821. }
  822. trb = &dep->trb_pool[dep->trb_enqueue];
  823. if (!req->trb) {
  824. dwc3_gadget_move_started_request(req);
  825. req->trb = trb;
  826. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  827. dep->queued_requests++;
  828. }
  829. __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
  830. stream_id, short_not_ok, no_interrupt);
  831. }
  832. /**
  833. * dwc3_ep_prev_trb - returns the previous TRB in the ring
  834. * @dep: The endpoint with the TRB ring
  835. * @index: The index of the current TRB in the ring
  836. *
  837. * Returns the TRB prior to the one pointed to by the index. If the
  838. * index is 0, we will wrap backwards, skip the link TRB, and return
  839. * the one just before that.
  840. */
  841. static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
  842. {
  843. u8 tmp = index;
  844. if (!tmp)
  845. tmp = DWC3_TRB_NUM - 1;
  846. return &dep->trb_pool[tmp - 1];
  847. }
  848. static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
  849. {
  850. struct dwc3_trb *tmp;
  851. u8 trbs_left;
  852. /*
  853. * If enqueue & dequeue are equal than it is either full or empty.
  854. *
  855. * One way to know for sure is if the TRB right before us has HWO bit
  856. * set or not. If it has, then we're definitely full and can't fit any
  857. * more transfers in our ring.
  858. */
  859. if (dep->trb_enqueue == dep->trb_dequeue) {
  860. tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  861. if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
  862. return 0;
  863. return DWC3_TRB_NUM - 1;
  864. }
  865. trbs_left = dep->trb_dequeue - dep->trb_enqueue;
  866. trbs_left &= (DWC3_TRB_NUM - 1);
  867. if (dep->trb_dequeue < dep->trb_enqueue)
  868. trbs_left--;
  869. return trbs_left;
  870. }
  871. static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
  872. struct dwc3_request *req)
  873. {
  874. struct scatterlist *sg = req->start_sg;
  875. struct scatterlist *s;
  876. int i;
  877. unsigned int remaining = req->request.num_mapped_sgs
  878. - req->num_queued_sgs;
  879. for_each_sg(sg, s, remaining, i) {
  880. unsigned int length = req->request.length;
  881. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  882. unsigned int rem = length % maxp;
  883. unsigned chain = true;
  884. if (sg_is_last(s))
  885. chain = false;
  886. if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
  887. struct dwc3 *dwc = dep->dwc;
  888. struct dwc3_trb *trb;
  889. req->unaligned = true;
  890. /* prepare normal TRB */
  891. dwc3_prepare_one_trb(dep, req, true, i);
  892. /* Now prepare one extra TRB to align transfer size */
  893. trb = &dep->trb_pool[dep->trb_enqueue];
  894. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
  895. maxp - rem, false, 0,
  896. req->request.stream_id,
  897. req->request.short_not_ok,
  898. req->request.no_interrupt);
  899. } else {
  900. dwc3_prepare_one_trb(dep, req, chain, i);
  901. }
  902. /*
  903. * There can be a situation where all sgs in sglist are not
  904. * queued because of insufficient trb number. To handle this
  905. * case, update start_sg to next sg to be queued, so that
  906. * we have free trbs we can continue queuing from where we
  907. * previously stopped
  908. */
  909. if (chain)
  910. req->start_sg = sg_next(s);
  911. req->num_queued_sgs++;
  912. if (!dwc3_calc_trbs_left(dep))
  913. break;
  914. }
  915. }
  916. static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
  917. struct dwc3_request *req)
  918. {
  919. unsigned int length = req->request.length;
  920. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  921. unsigned int rem = length % maxp;
  922. if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
  923. struct dwc3 *dwc = dep->dwc;
  924. struct dwc3_trb *trb;
  925. req->unaligned = true;
  926. /* prepare normal TRB */
  927. dwc3_prepare_one_trb(dep, req, true, 0);
  928. /* Now prepare one extra TRB to align transfer size */
  929. trb = &dep->trb_pool[dep->trb_enqueue];
  930. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
  931. false, 0, req->request.stream_id,
  932. req->request.short_not_ok,
  933. req->request.no_interrupt);
  934. } else if (req->request.zero && req->request.length &&
  935. (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
  936. struct dwc3 *dwc = dep->dwc;
  937. struct dwc3_trb *trb;
  938. req->zero = true;
  939. /* prepare normal TRB */
  940. dwc3_prepare_one_trb(dep, req, true, 0);
  941. /* Now prepare one extra TRB to handle ZLP */
  942. trb = &dep->trb_pool[dep->trb_enqueue];
  943. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
  944. false, 0, req->request.stream_id,
  945. req->request.short_not_ok,
  946. req->request.no_interrupt);
  947. } else {
  948. dwc3_prepare_one_trb(dep, req, false, 0);
  949. }
  950. }
  951. /*
  952. * dwc3_prepare_trbs - setup TRBs from requests
  953. * @dep: endpoint for which requests are being prepared
  954. *
  955. * The function goes through the requests list and sets up TRBs for the
  956. * transfers. The function returns once there are no more TRBs available or
  957. * it runs out of requests.
  958. */
  959. static void dwc3_prepare_trbs(struct dwc3_ep *dep)
  960. {
  961. struct dwc3_request *req, *n;
  962. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  963. /*
  964. * We can get in a situation where there's a request in the started list
  965. * but there weren't enough TRBs to fully kick it in the first time
  966. * around, so it has been waiting for more TRBs to be freed up.
  967. *
  968. * In that case, we should check if we have a request with pending_sgs
  969. * in the started list and prepare TRBs for that request first,
  970. * otherwise we will prepare TRBs completely out of order and that will
  971. * break things.
  972. */
  973. list_for_each_entry(req, &dep->started_list, list) {
  974. if (req->num_pending_sgs > 0)
  975. dwc3_prepare_one_trb_sg(dep, req);
  976. if (!dwc3_calc_trbs_left(dep))
  977. return;
  978. }
  979. list_for_each_entry_safe(req, n, &dep->pending_list, list) {
  980. struct dwc3 *dwc = dep->dwc;
  981. int ret;
  982. ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
  983. dep->direction);
  984. if (ret)
  985. return;
  986. req->sg = req->request.sg;
  987. req->start_sg = req->sg;
  988. req->num_queued_sgs = 0;
  989. req->num_pending_sgs = req->request.num_mapped_sgs;
  990. if (req->num_pending_sgs > 0)
  991. dwc3_prepare_one_trb_sg(dep, req);
  992. else
  993. dwc3_prepare_one_trb_linear(dep, req);
  994. if (!dwc3_calc_trbs_left(dep))
  995. return;
  996. }
  997. }
  998. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
  999. {
  1000. struct dwc3_gadget_ep_cmd_params params;
  1001. struct dwc3_request *req;
  1002. int starting;
  1003. int ret;
  1004. u32 cmd;
  1005. if (!dwc3_calc_trbs_left(dep))
  1006. return 0;
  1007. starting = !(dep->flags & DWC3_EP_BUSY);
  1008. dwc3_prepare_trbs(dep);
  1009. req = next_request(&dep->started_list);
  1010. if (!req) {
  1011. dep->flags |= DWC3_EP_PENDING_REQUEST;
  1012. return 0;
  1013. }
  1014. memset(&params, 0, sizeof(params));
  1015. if (starting) {
  1016. params.param0 = upper_32_bits(req->trb_dma);
  1017. params.param1 = lower_32_bits(req->trb_dma);
  1018. cmd = DWC3_DEPCMD_STARTTRANSFER;
  1019. if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
  1020. cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
  1021. } else {
  1022. cmd = DWC3_DEPCMD_UPDATETRANSFER |
  1023. DWC3_DEPCMD_PARAM(dep->resource_index);
  1024. }
  1025. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  1026. if (ret < 0) {
  1027. /*
  1028. * FIXME we need to iterate over the list of requests
  1029. * here and stop, unmap, free and del each of the linked
  1030. * requests instead of what we do now.
  1031. */
  1032. if (req->trb)
  1033. memset(req->trb, 0, sizeof(struct dwc3_trb));
  1034. dep->queued_requests--;
  1035. dwc3_gadget_del_and_unmap_request(dep, req, ret);
  1036. return ret;
  1037. }
  1038. dep->flags |= DWC3_EP_BUSY;
  1039. if (starting) {
  1040. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
  1041. WARN_ON_ONCE(!dep->resource_index);
  1042. }
  1043. return 0;
  1044. }
  1045. static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
  1046. {
  1047. u32 reg;
  1048. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1049. return DWC3_DSTS_SOFFN(reg);
  1050. }
  1051. static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
  1052. struct dwc3_ep *dep, u32 cur_uf)
  1053. {
  1054. if (list_empty(&dep->pending_list)) {
  1055. dev_info(dwc->dev, "%s: ran out of requests\n",
  1056. dep->name);
  1057. dep->flags |= DWC3_EP_PENDING_REQUEST;
  1058. return;
  1059. }
  1060. /*
  1061. * Schedule the first trb for one interval in the future or at
  1062. * least 4 microframes.
  1063. */
  1064. dep->frame_number = cur_uf + max_t(u32, 4, dep->interval);
  1065. __dwc3_gadget_kick_transfer(dep);
  1066. }
  1067. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  1068. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  1069. {
  1070. u32 cur_uf, mask;
  1071. mask = ~(dep->interval - 1);
  1072. cur_uf = event->parameters & mask;
  1073. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  1074. }
  1075. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  1076. {
  1077. struct dwc3 *dwc = dep->dwc;
  1078. if (!dep->endpoint.desc) {
  1079. dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
  1080. dep->name);
  1081. return -ESHUTDOWN;
  1082. }
  1083. if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
  1084. &req->request, req->dep->name))
  1085. return -EINVAL;
  1086. pm_runtime_get(dwc->dev);
  1087. req->request.actual = 0;
  1088. req->request.status = -EINPROGRESS;
  1089. req->direction = dep->direction;
  1090. req->epnum = dep->number;
  1091. trace_dwc3_ep_queue(req);
  1092. list_add_tail(&req->list, &dep->pending_list);
  1093. /*
  1094. * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
  1095. * wait for a XferNotReady event so we will know what's the current
  1096. * (micro-)frame number.
  1097. *
  1098. * Without this trick, we are very, very likely gonna get Bus Expiry
  1099. * errors which will force us issue EndTransfer command.
  1100. */
  1101. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1102. if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
  1103. if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
  1104. dwc3_stop_active_transfer(dwc, dep->number, true);
  1105. dep->flags = DWC3_EP_ENABLED;
  1106. } else {
  1107. u32 cur_uf;
  1108. cur_uf = __dwc3_gadget_get_frame(dwc);
  1109. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  1110. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  1111. }
  1112. return 0;
  1113. }
  1114. if ((dep->flags & DWC3_EP_BUSY) &&
  1115. !(dep->flags & DWC3_EP_MISSED_ISOC))
  1116. goto out;
  1117. return 0;
  1118. }
  1119. out:
  1120. return __dwc3_gadget_kick_transfer(dep);
  1121. }
  1122. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  1123. gfp_t gfp_flags)
  1124. {
  1125. struct dwc3_request *req = to_dwc3_request(request);
  1126. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1127. struct dwc3 *dwc = dep->dwc;
  1128. unsigned long flags;
  1129. int ret;
  1130. spin_lock_irqsave(&dwc->lock, flags);
  1131. ret = __dwc3_gadget_ep_queue(dep, req);
  1132. spin_unlock_irqrestore(&dwc->lock, flags);
  1133. return ret;
  1134. }
  1135. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  1136. struct usb_request *request)
  1137. {
  1138. struct dwc3_request *req = to_dwc3_request(request);
  1139. struct dwc3_request *r = NULL;
  1140. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1141. struct dwc3 *dwc = dep->dwc;
  1142. unsigned long flags;
  1143. int ret = 0;
  1144. trace_dwc3_ep_dequeue(req);
  1145. spin_lock_irqsave(&dwc->lock, flags);
  1146. list_for_each_entry(r, &dep->pending_list, list) {
  1147. if (r == req)
  1148. break;
  1149. }
  1150. if (r != req) {
  1151. list_for_each_entry(r, &dep->started_list, list) {
  1152. if (r == req)
  1153. break;
  1154. }
  1155. if (r == req) {
  1156. /* wait until it is processed */
  1157. dwc3_stop_active_transfer(dwc, dep->number, true);
  1158. /*
  1159. * If request was already started, this means we had to
  1160. * stop the transfer. With that we also need to ignore
  1161. * all TRBs used by the request, however TRBs can only
  1162. * be modified after completion of END_TRANSFER
  1163. * command. So what we do here is that we wait for
  1164. * END_TRANSFER completion and only after that, we jump
  1165. * over TRBs by clearing HWO and incrementing dequeue
  1166. * pointer.
  1167. *
  1168. * Note that we have 2 possible types of transfers here:
  1169. *
  1170. * i) Linear buffer request
  1171. * ii) SG-list based request
  1172. *
  1173. * SG-list based requests will have r->num_pending_sgs
  1174. * set to a valid number (> 0). Linear requests,
  1175. * normally use a single TRB.
  1176. *
  1177. * For each of these two cases, if r->unaligned flag is
  1178. * set, one extra TRB has been used to align transfer
  1179. * size to wMaxPacketSize.
  1180. *
  1181. * All of these cases need to be taken into
  1182. * consideration so we don't mess up our TRB ring
  1183. * pointers.
  1184. */
  1185. wait_event_lock_irq(dep->wait_end_transfer,
  1186. !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
  1187. dwc->lock);
  1188. if (!r->trb)
  1189. goto out1;
  1190. if (r->num_pending_sgs) {
  1191. struct dwc3_trb *trb;
  1192. int i = 0;
  1193. for (i = 0; i < r->num_pending_sgs; i++) {
  1194. trb = r->trb + i;
  1195. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1196. dwc3_ep_inc_deq(dep);
  1197. }
  1198. if (r->unaligned || r->zero) {
  1199. trb = r->trb + r->num_pending_sgs + 1;
  1200. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1201. dwc3_ep_inc_deq(dep);
  1202. }
  1203. } else {
  1204. struct dwc3_trb *trb = r->trb;
  1205. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1206. dwc3_ep_inc_deq(dep);
  1207. if (r->unaligned || r->zero) {
  1208. trb = r->trb + 1;
  1209. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1210. dwc3_ep_inc_deq(dep);
  1211. }
  1212. }
  1213. goto out1;
  1214. }
  1215. dev_err(dwc->dev, "request %pK was not queued to %s\n",
  1216. request, ep->name);
  1217. ret = -EINVAL;
  1218. goto out0;
  1219. }
  1220. out1:
  1221. /* giveback the request */
  1222. dep->queued_requests--;
  1223. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  1224. out0:
  1225. spin_unlock_irqrestore(&dwc->lock, flags);
  1226. return ret;
  1227. }
  1228. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
  1229. {
  1230. struct dwc3_gadget_ep_cmd_params params;
  1231. struct dwc3 *dwc = dep->dwc;
  1232. int ret;
  1233. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1234. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1235. return -EINVAL;
  1236. }
  1237. memset(&params, 0x00, sizeof(params));
  1238. if (value) {
  1239. struct dwc3_trb *trb;
  1240. unsigned transfer_in_flight;
  1241. unsigned started;
  1242. if (dep->flags & DWC3_EP_STALL)
  1243. return 0;
  1244. if (dep->number > 1)
  1245. trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  1246. else
  1247. trb = &dwc->ep0_trb[dep->trb_enqueue];
  1248. transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
  1249. started = !list_empty(&dep->started_list);
  1250. if (!protocol && ((dep->direction && transfer_in_flight) ||
  1251. (!dep->direction && started))) {
  1252. return -EAGAIN;
  1253. }
  1254. ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
  1255. &params);
  1256. if (ret)
  1257. dev_err(dwc->dev, "failed to set STALL on %s\n",
  1258. dep->name);
  1259. else
  1260. dep->flags |= DWC3_EP_STALL;
  1261. } else {
  1262. if (!(dep->flags & DWC3_EP_STALL))
  1263. return 0;
  1264. ret = dwc3_send_clear_stall_ep_cmd(dep);
  1265. if (ret)
  1266. dev_err(dwc->dev, "failed to clear STALL on %s\n",
  1267. dep->name);
  1268. else
  1269. dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
  1270. }
  1271. return ret;
  1272. }
  1273. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1274. {
  1275. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1276. struct dwc3 *dwc = dep->dwc;
  1277. unsigned long flags;
  1278. int ret;
  1279. spin_lock_irqsave(&dwc->lock, flags);
  1280. ret = __dwc3_gadget_ep_set_halt(dep, value, false);
  1281. spin_unlock_irqrestore(&dwc->lock, flags);
  1282. return ret;
  1283. }
  1284. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1285. {
  1286. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1287. struct dwc3 *dwc = dep->dwc;
  1288. unsigned long flags;
  1289. int ret;
  1290. spin_lock_irqsave(&dwc->lock, flags);
  1291. dep->flags |= DWC3_EP_WEDGE;
  1292. if (dep->number == 0 || dep->number == 1)
  1293. ret = __dwc3_gadget_ep0_set_halt(ep, 1);
  1294. else
  1295. ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
  1296. spin_unlock_irqrestore(&dwc->lock, flags);
  1297. return ret;
  1298. }
  1299. /* -------------------------------------------------------------------------- */
  1300. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1301. .bLength = USB_DT_ENDPOINT_SIZE,
  1302. .bDescriptorType = USB_DT_ENDPOINT,
  1303. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1304. };
  1305. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1306. .enable = dwc3_gadget_ep0_enable,
  1307. .disable = dwc3_gadget_ep0_disable,
  1308. .alloc_request = dwc3_gadget_ep_alloc_request,
  1309. .free_request = dwc3_gadget_ep_free_request,
  1310. .queue = dwc3_gadget_ep0_queue,
  1311. .dequeue = dwc3_gadget_ep_dequeue,
  1312. .set_halt = dwc3_gadget_ep0_set_halt,
  1313. .set_wedge = dwc3_gadget_ep_set_wedge,
  1314. };
  1315. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1316. .enable = dwc3_gadget_ep_enable,
  1317. .disable = dwc3_gadget_ep_disable,
  1318. .alloc_request = dwc3_gadget_ep_alloc_request,
  1319. .free_request = dwc3_gadget_ep_free_request,
  1320. .queue = dwc3_gadget_ep_queue,
  1321. .dequeue = dwc3_gadget_ep_dequeue,
  1322. .set_halt = dwc3_gadget_ep_set_halt,
  1323. .set_wedge = dwc3_gadget_ep_set_wedge,
  1324. };
  1325. /* -------------------------------------------------------------------------- */
  1326. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1327. {
  1328. struct dwc3 *dwc = gadget_to_dwc(g);
  1329. return __dwc3_gadget_get_frame(dwc);
  1330. }
  1331. static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
  1332. {
  1333. int retries;
  1334. int ret;
  1335. u32 reg;
  1336. u8 link_state;
  1337. u8 speed;
  1338. /*
  1339. * According to the Databook Remote wakeup request should
  1340. * be issued only when the device is in early suspend state.
  1341. *
  1342. * We can check that via USB Link State bits in DSTS register.
  1343. */
  1344. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1345. speed = reg & DWC3_DSTS_CONNECTSPD;
  1346. if ((speed == DWC3_DSTS_SUPERSPEED) ||
  1347. (speed == DWC3_DSTS_SUPERSPEED_PLUS))
  1348. return 0;
  1349. link_state = DWC3_DSTS_USBLNKST(reg);
  1350. switch (link_state) {
  1351. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1352. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1353. break;
  1354. default:
  1355. return -EINVAL;
  1356. }
  1357. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1358. if (ret < 0) {
  1359. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1360. return ret;
  1361. }
  1362. /* Recent versions do this automatically */
  1363. if (dwc->revision < DWC3_REVISION_194A) {
  1364. /* write zeroes to Link Change Request */
  1365. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1366. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1367. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1368. }
  1369. /* poll until Link State changes to ON */
  1370. retries = 20000;
  1371. while (retries--) {
  1372. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1373. /* in HS, means ON */
  1374. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1375. break;
  1376. }
  1377. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1378. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1379. return -EINVAL;
  1380. }
  1381. return 0;
  1382. }
  1383. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1384. {
  1385. struct dwc3 *dwc = gadget_to_dwc(g);
  1386. unsigned long flags;
  1387. int ret;
  1388. spin_lock_irqsave(&dwc->lock, flags);
  1389. ret = __dwc3_gadget_wakeup(dwc);
  1390. spin_unlock_irqrestore(&dwc->lock, flags);
  1391. return ret;
  1392. }
  1393. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  1394. int is_selfpowered)
  1395. {
  1396. struct dwc3 *dwc = gadget_to_dwc(g);
  1397. unsigned long flags;
  1398. spin_lock_irqsave(&dwc->lock, flags);
  1399. g->is_selfpowered = !!is_selfpowered;
  1400. spin_unlock_irqrestore(&dwc->lock, flags);
  1401. return 0;
  1402. }
  1403. static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
  1404. {
  1405. u32 reg;
  1406. u32 timeout = 500;
  1407. if (pm_runtime_suspended(dwc->dev))
  1408. return 0;
  1409. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1410. if (is_on) {
  1411. if (dwc->revision <= DWC3_REVISION_187A) {
  1412. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  1413. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  1414. }
  1415. if (dwc->revision >= DWC3_REVISION_194A)
  1416. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1417. reg |= DWC3_DCTL_RUN_STOP;
  1418. if (dwc->has_hibernation)
  1419. reg |= DWC3_DCTL_KEEP_CONNECT;
  1420. dwc->pullups_connected = true;
  1421. } else {
  1422. reg &= ~DWC3_DCTL_RUN_STOP;
  1423. if (dwc->has_hibernation && !suspend)
  1424. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1425. dwc->pullups_connected = false;
  1426. }
  1427. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1428. do {
  1429. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1430. reg &= DWC3_DSTS_DEVCTRLHLT;
  1431. } while (--timeout && !(!is_on ^ !reg));
  1432. if (!timeout)
  1433. return -ETIMEDOUT;
  1434. return 0;
  1435. }
  1436. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  1437. {
  1438. struct dwc3 *dwc = gadget_to_dwc(g);
  1439. unsigned long flags;
  1440. int ret;
  1441. is_on = !!is_on;
  1442. /*
  1443. * Per databook, when we want to stop the gadget, if a control transfer
  1444. * is still in process, complete it and get the core into setup phase.
  1445. */
  1446. if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
  1447. reinit_completion(&dwc->ep0_in_setup);
  1448. ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
  1449. msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
  1450. if (ret == 0) {
  1451. dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
  1452. return -ETIMEDOUT;
  1453. }
  1454. }
  1455. spin_lock_irqsave(&dwc->lock, flags);
  1456. ret = dwc3_gadget_run_stop(dwc, is_on, false);
  1457. spin_unlock_irqrestore(&dwc->lock, flags);
  1458. return ret;
  1459. }
  1460. static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
  1461. {
  1462. u32 reg;
  1463. /* Enable all but Start and End of Frame IRQs */
  1464. reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
  1465. DWC3_DEVTEN_EVNTOVERFLOWEN |
  1466. DWC3_DEVTEN_CMDCMPLTEN |
  1467. DWC3_DEVTEN_ERRTICERREN |
  1468. DWC3_DEVTEN_WKUPEVTEN |
  1469. DWC3_DEVTEN_CONNECTDONEEN |
  1470. DWC3_DEVTEN_USBRSTEN |
  1471. DWC3_DEVTEN_DISCONNEVTEN);
  1472. if (dwc->revision < DWC3_REVISION_250A)
  1473. reg |= DWC3_DEVTEN_ULSTCNGEN;
  1474. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  1475. }
  1476. static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
  1477. {
  1478. /* mask all interrupts */
  1479. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  1480. }
  1481. static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
  1482. static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
  1483. /**
  1484. * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
  1485. * @dwc: pointer to our context structure
  1486. *
  1487. * The following looks like complex but it's actually very simple. In order to
  1488. * calculate the number of packets we can burst at once on OUT transfers, we're
  1489. * gonna use RxFIFO size.
  1490. *
  1491. * To calculate RxFIFO size we need two numbers:
  1492. * MDWIDTH = size, in bits, of the internal memory bus
  1493. * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
  1494. *
  1495. * Given these two numbers, the formula is simple:
  1496. *
  1497. * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
  1498. *
  1499. * 24 bytes is for 3x SETUP packets
  1500. * 16 bytes is a clock domain crossing tolerance
  1501. *
  1502. * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
  1503. */
  1504. static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
  1505. {
  1506. u32 ram2_depth;
  1507. u32 mdwidth;
  1508. u32 nump;
  1509. u32 reg;
  1510. ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
  1511. mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
  1512. nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
  1513. nump = min_t(u32, nump, 16);
  1514. /* update NumP */
  1515. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1516. reg &= ~DWC3_DCFG_NUMP_MASK;
  1517. reg |= nump << DWC3_DCFG_NUMP_SHIFT;
  1518. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1519. }
  1520. static int __dwc3_gadget_start(struct dwc3 *dwc)
  1521. {
  1522. struct dwc3_ep *dep;
  1523. int ret = 0;
  1524. u32 reg;
  1525. /*
  1526. * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
  1527. * the core supports IMOD, disable it.
  1528. */
  1529. if (dwc->imod_interval) {
  1530. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  1531. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  1532. } else if (dwc3_has_imod(dwc)) {
  1533. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
  1534. }
  1535. /*
  1536. * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
  1537. * field instead of letting dwc3 itself calculate that automatically.
  1538. *
  1539. * This way, we maximize the chances that we'll be able to get several
  1540. * bursts of data without going through any sort of endpoint throttling.
  1541. */
  1542. reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
  1543. if (dwc3_is_usb31(dwc))
  1544. reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
  1545. else
  1546. reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
  1547. dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
  1548. dwc3_gadget_setup_nump(dwc);
  1549. /* Start with SuperSpeed Default */
  1550. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1551. dep = dwc->eps[0];
  1552. ret = __dwc3_gadget_ep_enable(dep, false, false);
  1553. if (ret) {
  1554. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1555. goto err0;
  1556. }
  1557. dep = dwc->eps[1];
  1558. ret = __dwc3_gadget_ep_enable(dep, false, false);
  1559. if (ret) {
  1560. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1561. goto err1;
  1562. }
  1563. /* begin to receive SETUP packets */
  1564. dwc->ep0state = EP0_SETUP_PHASE;
  1565. dwc3_ep0_out_start(dwc);
  1566. dwc3_gadget_enable_irq(dwc);
  1567. return 0;
  1568. err1:
  1569. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1570. err0:
  1571. return ret;
  1572. }
  1573. static int dwc3_gadget_start(struct usb_gadget *g,
  1574. struct usb_gadget_driver *driver)
  1575. {
  1576. struct dwc3 *dwc = gadget_to_dwc(g);
  1577. unsigned long flags;
  1578. int ret = 0;
  1579. int irq;
  1580. irq = dwc->irq_gadget;
  1581. ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
  1582. IRQF_SHARED, "dwc3", dwc->ev_buf);
  1583. if (ret) {
  1584. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  1585. irq, ret);
  1586. goto err0;
  1587. }
  1588. spin_lock_irqsave(&dwc->lock, flags);
  1589. if (dwc->gadget_driver) {
  1590. dev_err(dwc->dev, "%s is already bound to %s\n",
  1591. dwc->gadget.name,
  1592. dwc->gadget_driver->driver.name);
  1593. ret = -EBUSY;
  1594. goto err1;
  1595. }
  1596. dwc->gadget_driver = driver;
  1597. if (pm_runtime_active(dwc->dev))
  1598. __dwc3_gadget_start(dwc);
  1599. spin_unlock_irqrestore(&dwc->lock, flags);
  1600. return 0;
  1601. err1:
  1602. spin_unlock_irqrestore(&dwc->lock, flags);
  1603. free_irq(irq, dwc);
  1604. err0:
  1605. return ret;
  1606. }
  1607. static void __dwc3_gadget_stop(struct dwc3 *dwc)
  1608. {
  1609. dwc3_gadget_disable_irq(dwc);
  1610. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1611. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1612. }
  1613. static int dwc3_gadget_stop(struct usb_gadget *g)
  1614. {
  1615. struct dwc3 *dwc = gadget_to_dwc(g);
  1616. unsigned long flags;
  1617. int epnum;
  1618. u32 tmo_eps = 0;
  1619. spin_lock_irqsave(&dwc->lock, flags);
  1620. if (pm_runtime_suspended(dwc->dev))
  1621. goto out;
  1622. __dwc3_gadget_stop(dwc);
  1623. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1624. struct dwc3_ep *dep = dwc->eps[epnum];
  1625. int ret;
  1626. if (!dep)
  1627. continue;
  1628. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  1629. continue;
  1630. ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
  1631. !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
  1632. dwc->lock, msecs_to_jiffies(5));
  1633. if (ret <= 0) {
  1634. /* Timed out or interrupted! There's nothing much
  1635. * we can do so we just log here and print which
  1636. * endpoints timed out at the end.
  1637. */
  1638. tmo_eps |= 1 << epnum;
  1639. dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
  1640. }
  1641. }
  1642. if (tmo_eps) {
  1643. dev_err(dwc->dev,
  1644. "end transfer timed out on endpoints 0x%x [bitmap]\n",
  1645. tmo_eps);
  1646. }
  1647. out:
  1648. dwc->gadget_driver = NULL;
  1649. spin_unlock_irqrestore(&dwc->lock, flags);
  1650. free_irq(dwc->irq_gadget, dwc->ev_buf);
  1651. return 0;
  1652. }
  1653. static void dwc3_gadget_set_speed(struct usb_gadget *g,
  1654. enum usb_device_speed speed)
  1655. {
  1656. struct dwc3 *dwc = gadget_to_dwc(g);
  1657. unsigned long flags;
  1658. u32 reg;
  1659. spin_lock_irqsave(&dwc->lock, flags);
  1660. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1661. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1662. /*
  1663. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1664. * which would cause metastability state on Run/Stop
  1665. * bit if we try to force the IP to USB2-only mode.
  1666. *
  1667. * Because of that, we cannot configure the IP to any
  1668. * speed other than the SuperSpeed
  1669. *
  1670. * Refers to:
  1671. *
  1672. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1673. * USB 2.0 Mode
  1674. */
  1675. if (dwc->revision < DWC3_REVISION_220A &&
  1676. !dwc->dis_metastability_quirk) {
  1677. reg |= DWC3_DCFG_SUPERSPEED;
  1678. } else {
  1679. switch (speed) {
  1680. case USB_SPEED_LOW:
  1681. reg |= DWC3_DCFG_LOWSPEED;
  1682. break;
  1683. case USB_SPEED_FULL:
  1684. reg |= DWC3_DCFG_FULLSPEED;
  1685. break;
  1686. case USB_SPEED_HIGH:
  1687. reg |= DWC3_DCFG_HIGHSPEED;
  1688. break;
  1689. case USB_SPEED_SUPER:
  1690. reg |= DWC3_DCFG_SUPERSPEED;
  1691. break;
  1692. case USB_SPEED_SUPER_PLUS:
  1693. if (dwc3_is_usb31(dwc))
  1694. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  1695. else
  1696. reg |= DWC3_DCFG_SUPERSPEED;
  1697. break;
  1698. default:
  1699. dev_err(dwc->dev, "invalid speed (%d)\n", speed);
  1700. if (dwc->revision & DWC3_REVISION_IS_DWC31)
  1701. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  1702. else
  1703. reg |= DWC3_DCFG_SUPERSPEED;
  1704. }
  1705. }
  1706. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1707. spin_unlock_irqrestore(&dwc->lock, flags);
  1708. }
  1709. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1710. .get_frame = dwc3_gadget_get_frame,
  1711. .wakeup = dwc3_gadget_wakeup,
  1712. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1713. .pullup = dwc3_gadget_pullup,
  1714. .udc_start = dwc3_gadget_start,
  1715. .udc_stop = dwc3_gadget_stop,
  1716. .udc_set_speed = dwc3_gadget_set_speed,
  1717. };
  1718. /* -------------------------------------------------------------------------- */
  1719. static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
  1720. {
  1721. struct dwc3_ep *dep;
  1722. u8 epnum;
  1723. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1724. for (epnum = 0; epnum < total; epnum++) {
  1725. bool direction = epnum & 1;
  1726. u8 num = epnum >> 1;
  1727. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1728. if (!dep)
  1729. return -ENOMEM;
  1730. dep->dwc = dwc;
  1731. dep->number = epnum;
  1732. dep->direction = direction;
  1733. dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
  1734. dwc->eps[epnum] = dep;
  1735. snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
  1736. direction ? "in" : "out");
  1737. dep->endpoint.name = dep->name;
  1738. if (!(dep->number > 1)) {
  1739. dep->endpoint.desc = &dwc3_gadget_ep0_desc;
  1740. dep->endpoint.comp_desc = NULL;
  1741. }
  1742. spin_lock_init(&dep->lock);
  1743. if (num == 0) {
  1744. usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
  1745. dep->endpoint.maxburst = 1;
  1746. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1747. if (!direction)
  1748. dwc->gadget.ep0 = &dep->endpoint;
  1749. } else if (direction) {
  1750. int mdwidth;
  1751. int kbytes;
  1752. int size;
  1753. int ret;
  1754. mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
  1755. /* MDWIDTH is represented in bits, we need it in bytes */
  1756. mdwidth /= 8;
  1757. size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
  1758. if (dwc3_is_usb31(dwc))
  1759. size = DWC31_GTXFIFOSIZ_TXFDEF(size);
  1760. else
  1761. size = DWC3_GTXFIFOSIZ_TXFDEF(size);
  1762. /* FIFO Depth is in MDWDITH bytes. Multiply */
  1763. size *= mdwidth;
  1764. kbytes = size / 1024;
  1765. if (kbytes == 0)
  1766. kbytes = 1;
  1767. /*
  1768. * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
  1769. * internal overhead. We don't really know how these are used,
  1770. * but documentation say it exists.
  1771. */
  1772. size -= mdwidth * (kbytes + 1);
  1773. size /= kbytes;
  1774. usb_ep_set_maxpacket_limit(&dep->endpoint, size);
  1775. dep->endpoint.max_streams = 15;
  1776. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1777. list_add_tail(&dep->endpoint.ep_list,
  1778. &dwc->gadget.ep_list);
  1779. ret = dwc3_alloc_trb_pool(dep);
  1780. if (ret)
  1781. return ret;
  1782. } else {
  1783. int ret;
  1784. usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
  1785. dep->endpoint.max_streams = 15;
  1786. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1787. list_add_tail(&dep->endpoint.ep_list,
  1788. &dwc->gadget.ep_list);
  1789. ret = dwc3_alloc_trb_pool(dep);
  1790. if (ret)
  1791. return ret;
  1792. }
  1793. if (num == 0) {
  1794. dep->endpoint.caps.type_control = true;
  1795. } else {
  1796. dep->endpoint.caps.type_iso = true;
  1797. dep->endpoint.caps.type_bulk = true;
  1798. dep->endpoint.caps.type_int = true;
  1799. }
  1800. dep->endpoint.caps.dir_in = direction;
  1801. dep->endpoint.caps.dir_out = !direction;
  1802. INIT_LIST_HEAD(&dep->pending_list);
  1803. INIT_LIST_HEAD(&dep->started_list);
  1804. }
  1805. return 0;
  1806. }
  1807. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1808. {
  1809. struct dwc3_ep *dep;
  1810. u8 epnum;
  1811. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1812. dep = dwc->eps[epnum];
  1813. if (!dep)
  1814. continue;
  1815. /*
  1816. * Physical endpoints 0 and 1 are special; they form the
  1817. * bi-directional USB endpoint 0.
  1818. *
  1819. * For those two physical endpoints, we don't allocate a TRB
  1820. * pool nor do we add them the endpoints list. Due to that, we
  1821. * shouldn't do these two operations otherwise we would end up
  1822. * with all sorts of bugs when removing dwc3.ko.
  1823. */
  1824. if (epnum != 0 && epnum != 1) {
  1825. dwc3_free_trb_pool(dep);
  1826. list_del(&dep->endpoint.ep_list);
  1827. }
  1828. kfree(dep);
  1829. }
  1830. }
  1831. /* -------------------------------------------------------------------------- */
  1832. static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1833. struct dwc3_request *req, struct dwc3_trb *trb,
  1834. const struct dwc3_event_depevt *event, int status,
  1835. int chain)
  1836. {
  1837. unsigned int count;
  1838. unsigned int s_pkt = 0;
  1839. unsigned int trb_status;
  1840. dwc3_ep_inc_deq(dep);
  1841. if (req->trb == trb)
  1842. dep->queued_requests--;
  1843. trace_dwc3_complete_trb(dep, trb);
  1844. /*
  1845. * If we're in the middle of series of chained TRBs and we
  1846. * receive a short transfer along the way, DWC3 will skip
  1847. * through all TRBs including the last TRB in the chain (the
  1848. * where CHN bit is zero. DWC3 will also avoid clearing HWO
  1849. * bit and SW has to do it manually.
  1850. *
  1851. * We're going to do that here to avoid problems of HW trying
  1852. * to use bogus TRBs for transfers.
  1853. */
  1854. if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
  1855. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1856. /*
  1857. * If we're dealing with unaligned size OUT transfer, we will be left
  1858. * with one TRB pending in the ring. We need to manually clear HWO bit
  1859. * from that TRB.
  1860. */
  1861. if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
  1862. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1863. return 1;
  1864. }
  1865. count = trb->size & DWC3_TRB_SIZE_MASK;
  1866. req->remaining += count;
  1867. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1868. return 1;
  1869. if (dep->direction) {
  1870. if (count) {
  1871. trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  1872. if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
  1873. /*
  1874. * If missed isoc occurred and there is
  1875. * no request queued then issue END
  1876. * TRANSFER, so that core generates
  1877. * next xfernotready and we will issue
  1878. * a fresh START TRANSFER.
  1879. * If there are still queued request
  1880. * then wait, do not issue either END
  1881. * or UPDATE TRANSFER, just attach next
  1882. * request in pending_list during
  1883. * giveback.If any future queued request
  1884. * is successfully transferred then we
  1885. * will issue UPDATE TRANSFER for all
  1886. * request in the pending_list.
  1887. */
  1888. dep->flags |= DWC3_EP_MISSED_ISOC;
  1889. } else {
  1890. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1891. dep->name);
  1892. status = -ECONNRESET;
  1893. }
  1894. } else {
  1895. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  1896. }
  1897. } else {
  1898. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1899. s_pkt = 1;
  1900. }
  1901. if (s_pkt && !chain)
  1902. return 1;
  1903. if ((event->status & DEPEVT_STATUS_IOC) &&
  1904. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1905. return 1;
  1906. return 0;
  1907. }
  1908. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1909. const struct dwc3_event_depevt *event, int status)
  1910. {
  1911. struct dwc3_request *req, *n;
  1912. struct dwc3_trb *trb;
  1913. bool ioc = false;
  1914. int ret = 0;
  1915. list_for_each_entry_safe(req, n, &dep->started_list, list) {
  1916. unsigned length;
  1917. int chain;
  1918. length = req->request.length;
  1919. chain = req->num_pending_sgs > 0;
  1920. if (chain) {
  1921. struct scatterlist *sg = req->sg;
  1922. struct scatterlist *s;
  1923. unsigned int pending = req->num_pending_sgs;
  1924. unsigned int i;
  1925. for_each_sg(sg, s, pending, i) {
  1926. trb = &dep->trb_pool[dep->trb_dequeue];
  1927. if (trb->ctrl & DWC3_TRB_CTRL_HWO)
  1928. break;
  1929. req->sg = sg_next(s);
  1930. req->num_pending_sgs--;
  1931. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1932. event, status, chain);
  1933. if (ret)
  1934. break;
  1935. }
  1936. } else {
  1937. trb = &dep->trb_pool[dep->trb_dequeue];
  1938. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1939. event, status, chain);
  1940. }
  1941. if (req->unaligned || req->zero) {
  1942. trb = &dep->trb_pool[dep->trb_dequeue];
  1943. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1944. event, status, false);
  1945. req->unaligned = false;
  1946. req->zero = false;
  1947. }
  1948. req->request.actual = length - req->remaining;
  1949. if (req->request.actual < length || req->num_pending_sgs) {
  1950. /*
  1951. * There could be a scenario where the whole req can't
  1952. * be mapped into available TRB's. In that case, we need
  1953. * to kick transfer again if (req->num_pending_sgs > 0)
  1954. */
  1955. if (req->num_pending_sgs) {
  1956. dev_WARN_ONCE(dwc->dev,
  1957. (req->request.actual == length),
  1958. "There are some pending sg's that needs to be queued again\n");
  1959. return __dwc3_gadget_kick_transfer(dep);
  1960. }
  1961. }
  1962. dwc3_gadget_giveback(dep, req, status);
  1963. if (ret) {
  1964. if ((event->status & DEPEVT_STATUS_IOC) &&
  1965. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1966. ioc = true;
  1967. break;
  1968. }
  1969. }
  1970. /*
  1971. * Our endpoint might get disabled by another thread during
  1972. * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
  1973. * early on so DWC3_EP_BUSY flag gets cleared
  1974. */
  1975. if (!dep->endpoint.desc)
  1976. return 1;
  1977. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  1978. list_empty(&dep->started_list)) {
  1979. if (list_empty(&dep->pending_list)) {
  1980. /*
  1981. * If there is no entry in request list then do
  1982. * not issue END TRANSFER now. Just set PENDING
  1983. * flag, so that END TRANSFER is issued when an
  1984. * entry is added into request list.
  1985. */
  1986. dep->flags = DWC3_EP_PENDING_REQUEST;
  1987. } else {
  1988. dwc3_stop_active_transfer(dwc, dep->number, true);
  1989. dep->flags = DWC3_EP_ENABLED;
  1990. }
  1991. return 1;
  1992. }
  1993. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
  1994. return 0;
  1995. return 1;
  1996. }
  1997. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1998. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  1999. {
  2000. unsigned status = 0;
  2001. int clean_busy;
  2002. u32 is_xfer_complete;
  2003. is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
  2004. if (event->status & DEPEVT_STATUS_BUSERR)
  2005. status = -ECONNRESET;
  2006. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  2007. if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
  2008. usb_endpoint_xfer_isoc(dep->endpoint.desc)))
  2009. dep->flags &= ~DWC3_EP_BUSY;
  2010. /*
  2011. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  2012. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  2013. */
  2014. if (dwc->revision < DWC3_REVISION_183A) {
  2015. u32 reg;
  2016. int i;
  2017. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  2018. dep = dwc->eps[i];
  2019. if (!(dep->flags & DWC3_EP_ENABLED))
  2020. continue;
  2021. if (!list_empty(&dep->started_list))
  2022. return;
  2023. }
  2024. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2025. reg |= dwc->u1u2;
  2026. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2027. dwc->u1u2 = 0;
  2028. }
  2029. /*
  2030. * Our endpoint might get disabled by another thread during
  2031. * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
  2032. * early on so DWC3_EP_BUSY flag gets cleared
  2033. */
  2034. if (!dep->endpoint.desc)
  2035. return;
  2036. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc))
  2037. __dwc3_gadget_kick_transfer(dep);
  2038. }
  2039. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  2040. const struct dwc3_event_depevt *event)
  2041. {
  2042. struct dwc3_ep *dep;
  2043. u8 epnum = event->endpoint_number;
  2044. u8 cmd;
  2045. dep = dwc->eps[epnum];
  2046. if (!(dep->flags & DWC3_EP_ENABLED)) {
  2047. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  2048. return;
  2049. /* Handle only EPCMDCMPLT when EP disabled */
  2050. if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
  2051. return;
  2052. }
  2053. if (epnum == 0 || epnum == 1) {
  2054. dwc3_ep0_interrupt(dwc, event);
  2055. return;
  2056. }
  2057. switch (event->endpoint_event) {
  2058. case DWC3_DEPEVT_XFERCOMPLETE:
  2059. dep->resource_index = 0;
  2060. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  2061. dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
  2062. return;
  2063. }
  2064. dwc3_endpoint_transfer_complete(dwc, dep, event);
  2065. break;
  2066. case DWC3_DEPEVT_XFERINPROGRESS:
  2067. dwc3_endpoint_transfer_complete(dwc, dep, event);
  2068. break;
  2069. case DWC3_DEPEVT_XFERNOTREADY:
  2070. if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
  2071. dwc3_gadget_start_isoc(dwc, dep, event);
  2072. else
  2073. __dwc3_gadget_kick_transfer(dep);
  2074. break;
  2075. case DWC3_DEPEVT_STREAMEVT:
  2076. if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
  2077. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  2078. dep->name);
  2079. return;
  2080. }
  2081. break;
  2082. case DWC3_DEPEVT_EPCMDCMPLT:
  2083. cmd = DEPEVT_PARAMETER_CMD(event->parameters);
  2084. if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
  2085. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  2086. wake_up(&dep->wait_end_transfer);
  2087. }
  2088. break;
  2089. case DWC3_DEPEVT_RXTXFIFOEVT:
  2090. break;
  2091. }
  2092. }
  2093. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  2094. {
  2095. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  2096. spin_unlock(&dwc->lock);
  2097. dwc->gadget_driver->disconnect(&dwc->gadget);
  2098. spin_lock(&dwc->lock);
  2099. }
  2100. }
  2101. static void dwc3_suspend_gadget(struct dwc3 *dwc)
  2102. {
  2103. if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
  2104. spin_unlock(&dwc->lock);
  2105. dwc->gadget_driver->suspend(&dwc->gadget);
  2106. spin_lock(&dwc->lock);
  2107. }
  2108. }
  2109. static void dwc3_resume_gadget(struct dwc3 *dwc)
  2110. {
  2111. if (dwc->gadget_driver && dwc->gadget_driver->resume) {
  2112. spin_unlock(&dwc->lock);
  2113. dwc->gadget_driver->resume(&dwc->gadget);
  2114. spin_lock(&dwc->lock);
  2115. }
  2116. }
  2117. static void dwc3_reset_gadget(struct dwc3 *dwc)
  2118. {
  2119. if (!dwc->gadget_driver)
  2120. return;
  2121. if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
  2122. spin_unlock(&dwc->lock);
  2123. usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
  2124. spin_lock(&dwc->lock);
  2125. }
  2126. }
  2127. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
  2128. {
  2129. struct dwc3_ep *dep;
  2130. struct dwc3_gadget_ep_cmd_params params;
  2131. u32 cmd;
  2132. int ret;
  2133. dep = dwc->eps[epnum];
  2134. if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
  2135. !dep->resource_index)
  2136. return;
  2137. /*
  2138. * NOTICE: We are violating what the Databook says about the
  2139. * EndTransfer command. Ideally we would _always_ wait for the
  2140. * EndTransfer Command Completion IRQ, but that's causing too
  2141. * much trouble synchronizing between us and gadget driver.
  2142. *
  2143. * We have discussed this with the IP Provider and it was
  2144. * suggested to giveback all requests here, but give HW some
  2145. * extra time to synchronize with the interconnect. We're using
  2146. * an arbitrary 100us delay for that.
  2147. *
  2148. * Note also that a similar handling was tested by Synopsys
  2149. * (thanks a lot Paul) and nothing bad has come out of it.
  2150. * In short, what we're doing is:
  2151. *
  2152. * - Issue EndTransfer WITH CMDIOC bit set
  2153. * - Wait 100us
  2154. *
  2155. * As of IP version 3.10a of the DWC_usb3 IP, the controller
  2156. * supports a mode to work around the above limitation. The
  2157. * software can poll the CMDACT bit in the DEPCMD register
  2158. * after issuing a EndTransfer command. This mode is enabled
  2159. * by writing GUCTL2[14]. This polling is already done in the
  2160. * dwc3_send_gadget_ep_cmd() function so if the mode is
  2161. * enabled, the EndTransfer command will have completed upon
  2162. * returning from this function and we don't need to delay for
  2163. * 100us.
  2164. *
  2165. * This mode is NOT available on the DWC_usb31 IP.
  2166. */
  2167. cmd = DWC3_DEPCMD_ENDTRANSFER;
  2168. cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
  2169. cmd |= DWC3_DEPCMD_CMDIOC;
  2170. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  2171. memset(&params, 0, sizeof(params));
  2172. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  2173. WARN_ON_ONCE(ret);
  2174. dep->resource_index = 0;
  2175. dep->flags &= ~DWC3_EP_BUSY;
  2176. if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
  2177. dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
  2178. udelay(100);
  2179. }
  2180. }
  2181. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  2182. {
  2183. u32 epnum;
  2184. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  2185. struct dwc3_ep *dep;
  2186. int ret;
  2187. dep = dwc->eps[epnum];
  2188. if (!dep)
  2189. continue;
  2190. if (!(dep->flags & DWC3_EP_STALL))
  2191. continue;
  2192. dep->flags &= ~DWC3_EP_STALL;
  2193. ret = dwc3_send_clear_stall_ep_cmd(dep);
  2194. WARN_ON_ONCE(ret);
  2195. }
  2196. }
  2197. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  2198. {
  2199. int reg;
  2200. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2201. reg &= ~DWC3_DCTL_INITU1ENA;
  2202. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2203. reg &= ~DWC3_DCTL_INITU2ENA;
  2204. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2205. dwc3_disconnect_gadget(dwc);
  2206. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2207. dwc->setup_packet_pending = false;
  2208. usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
  2209. dwc->connected = false;
  2210. }
  2211. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  2212. {
  2213. u32 reg;
  2214. dwc->connected = true;
  2215. /*
  2216. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  2217. * would cause a missing Disconnect Event if there's a
  2218. * pending Setup Packet in the FIFO.
  2219. *
  2220. * There's no suggested workaround on the official Bug
  2221. * report, which states that "unless the driver/application
  2222. * is doing any special handling of a disconnect event,
  2223. * there is no functional issue".
  2224. *
  2225. * Unfortunately, it turns out that we _do_ some special
  2226. * handling of a disconnect event, namely complete all
  2227. * pending transfers, notify gadget driver of the
  2228. * disconnection, and so on.
  2229. *
  2230. * Our suggested workaround is to follow the Disconnect
  2231. * Event steps here, instead, based on a setup_packet_pending
  2232. * flag. Such flag gets set whenever we have a SETUP_PENDING
  2233. * status for EP0 TRBs and gets cleared on XferComplete for the
  2234. * same endpoint.
  2235. *
  2236. * Refers to:
  2237. *
  2238. * STAR#9000466709: RTL: Device : Disconnect event not
  2239. * generated if setup packet pending in FIFO
  2240. */
  2241. if (dwc->revision < DWC3_REVISION_188A) {
  2242. if (dwc->setup_packet_pending)
  2243. dwc3_gadget_disconnect_interrupt(dwc);
  2244. }
  2245. dwc3_reset_gadget(dwc);
  2246. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2247. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  2248. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2249. dwc->test_mode = false;
  2250. dwc3_clear_stall_all_ep(dwc);
  2251. /* Reset device address to zero */
  2252. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2253. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  2254. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2255. }
  2256. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  2257. {
  2258. struct dwc3_ep *dep;
  2259. int ret;
  2260. u32 reg;
  2261. u8 speed;
  2262. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  2263. speed = reg & DWC3_DSTS_CONNECTSPD;
  2264. dwc->speed = speed;
  2265. /*
  2266. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  2267. * each time on Connect Done.
  2268. *
  2269. * Currently we always use the reset value. If any platform
  2270. * wants to set this to a different value, we need to add a
  2271. * setting and update GCTL.RAMCLKSEL here.
  2272. */
  2273. switch (speed) {
  2274. case DWC3_DSTS_SUPERSPEED_PLUS:
  2275. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2276. dwc->gadget.ep0->maxpacket = 512;
  2277. dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
  2278. break;
  2279. case DWC3_DSTS_SUPERSPEED:
  2280. /*
  2281. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  2282. * would cause a missing USB3 Reset event.
  2283. *
  2284. * In such situations, we should force a USB3 Reset
  2285. * event by calling our dwc3_gadget_reset_interrupt()
  2286. * routine.
  2287. *
  2288. * Refers to:
  2289. *
  2290. * STAR#9000483510: RTL: SS : USB3 reset event may
  2291. * not be generated always when the link enters poll
  2292. */
  2293. if (dwc->revision < DWC3_REVISION_190A)
  2294. dwc3_gadget_reset_interrupt(dwc);
  2295. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2296. dwc->gadget.ep0->maxpacket = 512;
  2297. dwc->gadget.speed = USB_SPEED_SUPER;
  2298. break;
  2299. case DWC3_DSTS_HIGHSPEED:
  2300. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2301. dwc->gadget.ep0->maxpacket = 64;
  2302. dwc->gadget.speed = USB_SPEED_HIGH;
  2303. break;
  2304. case DWC3_DSTS_FULLSPEED:
  2305. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2306. dwc->gadget.ep0->maxpacket = 64;
  2307. dwc->gadget.speed = USB_SPEED_FULL;
  2308. break;
  2309. case DWC3_DSTS_LOWSPEED:
  2310. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  2311. dwc->gadget.ep0->maxpacket = 8;
  2312. dwc->gadget.speed = USB_SPEED_LOW;
  2313. break;
  2314. }
  2315. dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
  2316. /* Enable USB2 LPM Capability */
  2317. if ((dwc->revision > DWC3_REVISION_194A) &&
  2318. (speed != DWC3_DSTS_SUPERSPEED) &&
  2319. (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
  2320. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2321. reg |= DWC3_DCFG_LPM_CAP;
  2322. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2323. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2324. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  2325. reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
  2326. /*
  2327. * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
  2328. * DCFG.LPMCap is set, core responses with an ACK and the
  2329. * BESL value in the LPM token is less than or equal to LPM
  2330. * NYET threshold.
  2331. */
  2332. WARN_ONCE(dwc->revision < DWC3_REVISION_240A
  2333. && dwc->has_lpm_erratum,
  2334. "LPM Erratum not available on dwc3 revisions < 2.40a\n");
  2335. if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
  2336. reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
  2337. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2338. } else {
  2339. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2340. reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
  2341. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2342. }
  2343. dep = dwc->eps[0];
  2344. ret = __dwc3_gadget_ep_enable(dep, true, false);
  2345. if (ret) {
  2346. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2347. return;
  2348. }
  2349. dep = dwc->eps[1];
  2350. ret = __dwc3_gadget_ep_enable(dep, true, false);
  2351. if (ret) {
  2352. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2353. return;
  2354. }
  2355. /*
  2356. * Configure PHY via GUSB3PIPECTLn if required.
  2357. *
  2358. * Update GTXFIFOSIZn
  2359. *
  2360. * In both cases reset values should be sufficient.
  2361. */
  2362. }
  2363. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  2364. {
  2365. /*
  2366. * TODO take core out of low power mode when that's
  2367. * implemented.
  2368. */
  2369. if (dwc->gadget_driver && dwc->gadget_driver->resume) {
  2370. spin_unlock(&dwc->lock);
  2371. dwc->gadget_driver->resume(&dwc->gadget);
  2372. spin_lock(&dwc->lock);
  2373. }
  2374. }
  2375. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  2376. unsigned int evtinfo)
  2377. {
  2378. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2379. unsigned int pwropt;
  2380. /*
  2381. * WORKAROUND: DWC3 < 2.50a have an issue when configured without
  2382. * Hibernation mode enabled which would show up when device detects
  2383. * host-initiated U3 exit.
  2384. *
  2385. * In that case, device will generate a Link State Change Interrupt
  2386. * from U3 to RESUME which is only necessary if Hibernation is
  2387. * configured in.
  2388. *
  2389. * There are no functional changes due to such spurious event and we
  2390. * just need to ignore it.
  2391. *
  2392. * Refers to:
  2393. *
  2394. * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
  2395. * operational mode
  2396. */
  2397. pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
  2398. if ((dwc->revision < DWC3_REVISION_250A) &&
  2399. (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
  2400. if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
  2401. (next == DWC3_LINK_STATE_RESUME)) {
  2402. return;
  2403. }
  2404. }
  2405. /*
  2406. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  2407. * on the link partner, the USB session might do multiple entry/exit
  2408. * of low power states before a transfer takes place.
  2409. *
  2410. * Due to this problem, we might experience lower throughput. The
  2411. * suggested workaround is to disable DCTL[12:9] bits if we're
  2412. * transitioning from U1/U2 to U0 and enable those bits again
  2413. * after a transfer completes and there are no pending transfers
  2414. * on any of the enabled endpoints.
  2415. *
  2416. * This is the first half of that workaround.
  2417. *
  2418. * Refers to:
  2419. *
  2420. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  2421. * core send LGO_Ux entering U0
  2422. */
  2423. if (dwc->revision < DWC3_REVISION_183A) {
  2424. if (next == DWC3_LINK_STATE_U0) {
  2425. u32 u1u2;
  2426. u32 reg;
  2427. switch (dwc->link_state) {
  2428. case DWC3_LINK_STATE_U1:
  2429. case DWC3_LINK_STATE_U2:
  2430. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2431. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  2432. | DWC3_DCTL_ACCEPTU2ENA
  2433. | DWC3_DCTL_INITU1ENA
  2434. | DWC3_DCTL_ACCEPTU1ENA);
  2435. if (!dwc->u1u2)
  2436. dwc->u1u2 = reg & u1u2;
  2437. reg &= ~u1u2;
  2438. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2439. break;
  2440. default:
  2441. /* do nothing */
  2442. break;
  2443. }
  2444. }
  2445. }
  2446. switch (next) {
  2447. case DWC3_LINK_STATE_U1:
  2448. if (dwc->speed == USB_SPEED_SUPER)
  2449. dwc3_suspend_gadget(dwc);
  2450. break;
  2451. case DWC3_LINK_STATE_U2:
  2452. case DWC3_LINK_STATE_U3:
  2453. dwc3_suspend_gadget(dwc);
  2454. break;
  2455. case DWC3_LINK_STATE_RESUME:
  2456. dwc3_resume_gadget(dwc);
  2457. break;
  2458. default:
  2459. /* do nothing */
  2460. break;
  2461. }
  2462. dwc->link_state = next;
  2463. }
  2464. static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
  2465. unsigned int evtinfo)
  2466. {
  2467. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2468. if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
  2469. dwc3_suspend_gadget(dwc);
  2470. dwc->link_state = next;
  2471. }
  2472. static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
  2473. unsigned int evtinfo)
  2474. {
  2475. unsigned int is_ss = evtinfo & BIT(4);
  2476. /*
  2477. * WORKAROUND: DWC3 revison 2.20a with hibernation support
  2478. * have a known issue which can cause USB CV TD.9.23 to fail
  2479. * randomly.
  2480. *
  2481. * Because of this issue, core could generate bogus hibernation
  2482. * events which SW needs to ignore.
  2483. *
  2484. * Refers to:
  2485. *
  2486. * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
  2487. * Device Fallback from SuperSpeed
  2488. */
  2489. if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
  2490. return;
  2491. /* enter hibernation here */
  2492. }
  2493. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  2494. const struct dwc3_event_devt *event)
  2495. {
  2496. switch (event->type) {
  2497. case DWC3_DEVICE_EVENT_DISCONNECT:
  2498. dwc3_gadget_disconnect_interrupt(dwc);
  2499. break;
  2500. case DWC3_DEVICE_EVENT_RESET:
  2501. dwc3_gadget_reset_interrupt(dwc);
  2502. break;
  2503. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  2504. dwc3_gadget_conndone_interrupt(dwc);
  2505. break;
  2506. case DWC3_DEVICE_EVENT_WAKEUP:
  2507. dwc3_gadget_wakeup_interrupt(dwc);
  2508. break;
  2509. case DWC3_DEVICE_EVENT_HIBER_REQ:
  2510. if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
  2511. "unexpected hibernation event\n"))
  2512. break;
  2513. dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
  2514. break;
  2515. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  2516. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  2517. break;
  2518. case DWC3_DEVICE_EVENT_EOPF:
  2519. /* It changed to be suspend event for version 2.30a and above */
  2520. if (dwc->revision >= DWC3_REVISION_230A) {
  2521. /*
  2522. * Ignore suspend event until the gadget enters into
  2523. * USB_STATE_CONFIGURED state.
  2524. */
  2525. if (dwc->gadget.state >= USB_STATE_CONFIGURED)
  2526. dwc3_gadget_suspend_interrupt(dwc,
  2527. event->event_info);
  2528. }
  2529. break;
  2530. case DWC3_DEVICE_EVENT_SOF:
  2531. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  2532. case DWC3_DEVICE_EVENT_CMD_CMPL:
  2533. case DWC3_DEVICE_EVENT_OVERFLOW:
  2534. break;
  2535. default:
  2536. dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  2537. }
  2538. }
  2539. static void dwc3_process_event_entry(struct dwc3 *dwc,
  2540. const union dwc3_event *event)
  2541. {
  2542. trace_dwc3_event(event->raw, dwc);
  2543. if (!event->type.is_devspec)
  2544. dwc3_endpoint_interrupt(dwc, &event->depevt);
  2545. else if (event->type.type == DWC3_EVENT_TYPE_DEV)
  2546. dwc3_gadget_interrupt(dwc, &event->devt);
  2547. else
  2548. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  2549. }
  2550. static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
  2551. {
  2552. struct dwc3 *dwc = evt->dwc;
  2553. irqreturn_t ret = IRQ_NONE;
  2554. int left;
  2555. u32 reg;
  2556. left = evt->count;
  2557. if (!(evt->flags & DWC3_EVENT_PENDING))
  2558. return IRQ_NONE;
  2559. while (left > 0) {
  2560. union dwc3_event event;
  2561. event.raw = *(u32 *) (evt->cache + evt->lpos);
  2562. dwc3_process_event_entry(dwc, &event);
  2563. /*
  2564. * FIXME we wrap around correctly to the next entry as
  2565. * almost all entries are 4 bytes in size. There is one
  2566. * entry which has 12 bytes which is a regular entry
  2567. * followed by 8 bytes data. ATM I don't know how
  2568. * things are organized if we get next to the a
  2569. * boundary so I worry about that once we try to handle
  2570. * that.
  2571. */
  2572. evt->lpos = (evt->lpos + 4) % evt->length;
  2573. left -= 4;
  2574. }
  2575. evt->count = 0;
  2576. evt->flags &= ~DWC3_EVENT_PENDING;
  2577. ret = IRQ_HANDLED;
  2578. /* Unmask interrupt */
  2579. reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
  2580. reg &= ~DWC3_GEVNTSIZ_INTMASK;
  2581. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
  2582. if (dwc->imod_interval) {
  2583. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  2584. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  2585. }
  2586. return ret;
  2587. }
  2588. static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
  2589. {
  2590. struct dwc3_event_buffer *evt = _evt;
  2591. struct dwc3 *dwc = evt->dwc;
  2592. unsigned long flags;
  2593. irqreturn_t ret = IRQ_NONE;
  2594. spin_lock_irqsave(&dwc->lock, flags);
  2595. ret = dwc3_process_event_buf(evt);
  2596. spin_unlock_irqrestore(&dwc->lock, flags);
  2597. return ret;
  2598. }
  2599. static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
  2600. {
  2601. struct dwc3 *dwc = evt->dwc;
  2602. u32 amount;
  2603. u32 count;
  2604. u32 reg;
  2605. if (pm_runtime_suspended(dwc->dev)) {
  2606. pm_runtime_get(dwc->dev);
  2607. disable_irq_nosync(dwc->irq_gadget);
  2608. dwc->pending_events = true;
  2609. return IRQ_HANDLED;
  2610. }
  2611. /*
  2612. * With PCIe legacy interrupt, test shows that top-half irq handler can
  2613. * be called again after HW interrupt deassertion. Check if bottom-half
  2614. * irq event handler completes before caching new event to prevent
  2615. * losing events.
  2616. */
  2617. if (evt->flags & DWC3_EVENT_PENDING)
  2618. return IRQ_HANDLED;
  2619. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
  2620. count &= DWC3_GEVNTCOUNT_MASK;
  2621. if (!count)
  2622. return IRQ_NONE;
  2623. evt->count = count;
  2624. evt->flags |= DWC3_EVENT_PENDING;
  2625. /* Mask interrupt */
  2626. reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
  2627. reg |= DWC3_GEVNTSIZ_INTMASK;
  2628. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
  2629. amount = min(count, evt->length - evt->lpos);
  2630. memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
  2631. if (amount < count)
  2632. memcpy(evt->cache, evt->buf, count - amount);
  2633. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
  2634. return IRQ_WAKE_THREAD;
  2635. }
  2636. static irqreturn_t dwc3_interrupt(int irq, void *_evt)
  2637. {
  2638. struct dwc3_event_buffer *evt = _evt;
  2639. return dwc3_check_event_buf(evt);
  2640. }
  2641. static int dwc3_gadget_get_irq(struct dwc3 *dwc)
  2642. {
  2643. struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
  2644. int irq;
  2645. irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
  2646. if (irq > 0)
  2647. goto out;
  2648. if (irq == -EPROBE_DEFER)
  2649. goto out;
  2650. irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
  2651. if (irq > 0)
  2652. goto out;
  2653. if (irq == -EPROBE_DEFER)
  2654. goto out;
  2655. irq = platform_get_irq(dwc3_pdev, 0);
  2656. if (irq > 0)
  2657. goto out;
  2658. if (irq != -EPROBE_DEFER)
  2659. dev_err(dwc->dev, "missing peripheral IRQ\n");
  2660. if (!irq)
  2661. irq = -EINVAL;
  2662. out:
  2663. return irq;
  2664. }
  2665. /**
  2666. * dwc3_gadget_init - initializes gadget related registers
  2667. * @dwc: pointer to our controller context structure
  2668. *
  2669. * Returns 0 on success otherwise negative errno.
  2670. */
  2671. int dwc3_gadget_init(struct dwc3 *dwc)
  2672. {
  2673. int ret;
  2674. int irq;
  2675. irq = dwc3_gadget_get_irq(dwc);
  2676. if (irq < 0) {
  2677. ret = irq;
  2678. goto err0;
  2679. }
  2680. dwc->irq_gadget = irq;
  2681. dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
  2682. sizeof(*dwc->ep0_trb) * 2,
  2683. &dwc->ep0_trb_addr, GFP_KERNEL);
  2684. if (!dwc->ep0_trb) {
  2685. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  2686. ret = -ENOMEM;
  2687. goto err0;
  2688. }
  2689. dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
  2690. if (!dwc->setup_buf) {
  2691. ret = -ENOMEM;
  2692. goto err1;
  2693. }
  2694. dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
  2695. &dwc->bounce_addr, GFP_KERNEL);
  2696. if (!dwc->bounce) {
  2697. ret = -ENOMEM;
  2698. goto err2;
  2699. }
  2700. init_completion(&dwc->ep0_in_setup);
  2701. dwc->gadget.ops = &dwc3_gadget_ops;
  2702. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2703. dwc->gadget.sg_supported = true;
  2704. dwc->gadget.name = "dwc3-gadget";
  2705. dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
  2706. /*
  2707. * FIXME We might be setting max_speed to <SUPER, however versions
  2708. * <2.20a of dwc3 have an issue with metastability (documented
  2709. * elsewhere in this driver) which tells us we can't set max speed to
  2710. * anything lower than SUPER.
  2711. *
  2712. * Because gadget.max_speed is only used by composite.c and function
  2713. * drivers (i.e. it won't go into dwc3's registers) we are allowing this
  2714. * to happen so we avoid sending SuperSpeed Capability descriptor
  2715. * together with our BOS descriptor as that could confuse host into
  2716. * thinking we can handle super speed.
  2717. *
  2718. * Note that, in fact, we won't even support GetBOS requests when speed
  2719. * is less than super speed because we don't have means, yet, to tell
  2720. * composite.c that we are USB 2.0 + LPM ECN.
  2721. */
  2722. if (dwc->revision < DWC3_REVISION_220A &&
  2723. !dwc->dis_metastability_quirk)
  2724. dev_info(dwc->dev, "changing max_speed on rev %08x\n",
  2725. dwc->revision);
  2726. dwc->gadget.max_speed = dwc->maximum_speed;
  2727. /*
  2728. * REVISIT: Here we should clear all pending IRQs to be
  2729. * sure we're starting from a well known location.
  2730. */
  2731. ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
  2732. if (ret)
  2733. goto err3;
  2734. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  2735. if (ret) {
  2736. dev_err(dwc->dev, "failed to register udc\n");
  2737. goto err4;
  2738. }
  2739. return 0;
  2740. err4:
  2741. dwc3_gadget_free_endpoints(dwc);
  2742. err3:
  2743. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  2744. dwc->bounce_addr);
  2745. err2:
  2746. kfree(dwc->setup_buf);
  2747. err1:
  2748. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  2749. dwc->ep0_trb, dwc->ep0_trb_addr);
  2750. err0:
  2751. return ret;
  2752. }
  2753. /* -------------------------------------------------------------------------- */
  2754. void dwc3_gadget_exit(struct dwc3 *dwc)
  2755. {
  2756. usb_del_gadget_udc(&dwc->gadget);
  2757. dwc3_gadget_free_endpoints(dwc);
  2758. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  2759. dwc->bounce_addr);
  2760. kfree(dwc->setup_buf);
  2761. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  2762. dwc->ep0_trb, dwc->ep0_trb_addr);
  2763. }
  2764. int dwc3_gadget_suspend(struct dwc3 *dwc)
  2765. {
  2766. if (!dwc->gadget_driver)
  2767. return 0;
  2768. dwc3_gadget_run_stop(dwc, false, false);
  2769. dwc3_disconnect_gadget(dwc);
  2770. __dwc3_gadget_stop(dwc);
  2771. return 0;
  2772. }
  2773. int dwc3_gadget_resume(struct dwc3 *dwc)
  2774. {
  2775. int ret;
  2776. if (!dwc->gadget_driver)
  2777. return 0;
  2778. ret = __dwc3_gadget_start(dwc);
  2779. if (ret < 0)
  2780. goto err0;
  2781. ret = dwc3_gadget_run_stop(dwc, true, false);
  2782. if (ret < 0)
  2783. goto err1;
  2784. return 0;
  2785. err1:
  2786. __dwc3_gadget_stop(dwc);
  2787. err0:
  2788. return ret;
  2789. }
  2790. void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
  2791. {
  2792. if (dwc->pending_events) {
  2793. dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
  2794. dwc->pending_events = false;
  2795. enable_irq(dwc->irq_gadget);
  2796. }
  2797. }