gadget.c 80 KB

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