gadget.c 83 KB

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