gadget.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762
  1. /**
  2. * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2, as published by the Free
  24. * Software Foundation.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/delay.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/io.h>
  46. #include <linux/list.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/usb/ch9.h>
  49. #include <linux/usb/gadget.h>
  50. #include "core.h"
  51. #include "gadget.h"
  52. #include "io.h"
  53. /**
  54. * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
  55. * @dwc: pointer to our context structure
  56. * @mode: the mode to set (J, K SE0 NAK, Force Enable)
  57. *
  58. * Caller should take care of locking. This function will
  59. * return 0 on success or -EINVAL if wrong Test Selector
  60. * is passed
  61. */
  62. int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
  63. {
  64. u32 reg;
  65. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  66. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  67. switch (mode) {
  68. case TEST_J:
  69. case TEST_K:
  70. case TEST_SE0_NAK:
  71. case TEST_PACKET:
  72. case TEST_FORCE_EN:
  73. reg |= mode << 1;
  74. break;
  75. default:
  76. return -EINVAL;
  77. }
  78. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  79. return 0;
  80. }
  81. /**
  82. * dwc3_gadget_set_link_state - Sets USB Link to a particular State
  83. * @dwc: pointer to our context structure
  84. * @state: the state to put link into
  85. *
  86. * Caller should take care of locking. This function will
  87. * return 0 on success or -ETIMEDOUT.
  88. */
  89. int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
  90. {
  91. int retries = 10000;
  92. u32 reg;
  93. /*
  94. * Wait until device controller is ready. Only applies to 1.94a and
  95. * later RTL.
  96. */
  97. if (dwc->revision >= DWC3_REVISION_194A) {
  98. while (--retries) {
  99. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  100. if (reg & DWC3_DSTS_DCNRD)
  101. udelay(5);
  102. else
  103. break;
  104. }
  105. if (retries <= 0)
  106. return -ETIMEDOUT;
  107. }
  108. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  109. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  110. /* set requested state */
  111. reg |= DWC3_DCTL_ULSTCHNGREQ(state);
  112. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  113. /*
  114. * The following code is racy when called from dwc3_gadget_wakeup,
  115. * and is not needed, at least on newer versions
  116. */
  117. if (dwc->revision >= DWC3_REVISION_194A)
  118. return 0;
  119. /* wait for a change in DSTS */
  120. retries = 10000;
  121. while (--retries) {
  122. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  123. if (DWC3_DSTS_USBLNKST(reg) == state)
  124. return 0;
  125. udelay(5);
  126. }
  127. dev_vdbg(dwc->dev, "link state change request timed out\n");
  128. return -ETIMEDOUT;
  129. }
  130. /**
  131. * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
  132. * @dwc: pointer to our context structure
  133. *
  134. * This function will a best effort FIFO allocation in order
  135. * to improve FIFO usage and throughput, while still allowing
  136. * us to enable as many endpoints as possible.
  137. *
  138. * Keep in mind that this operation will be highly dependent
  139. * on the configured size for RAM1 - which contains TxFifo -,
  140. * the amount of endpoints enabled on coreConsultant tool, and
  141. * the width of the Master Bus.
  142. *
  143. * In the ideal world, we would always be able to satisfy the
  144. * following equation:
  145. *
  146. * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
  147. * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
  148. *
  149. * Unfortunately, due to many variables that's not always the case.
  150. */
  151. int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
  152. {
  153. int last_fifo_depth = 0;
  154. int ram1_depth;
  155. int fifo_size;
  156. int mdwidth;
  157. int num;
  158. if (!dwc->needs_fifo_resize)
  159. return 0;
  160. ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
  161. mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
  162. /* MDWIDTH is represented in bits, we need it in bytes */
  163. mdwidth >>= 3;
  164. /*
  165. * FIXME For now we will only allocate 1 wMaxPacketSize space
  166. * for each enabled endpoint, later patches will come to
  167. * improve this algorithm so that we better use the internal
  168. * FIFO space
  169. */
  170. for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
  171. struct dwc3_ep *dep = dwc->eps[num];
  172. int fifo_number = dep->number >> 1;
  173. int mult = 1;
  174. int tmp;
  175. if (!(dep->number & 1))
  176. continue;
  177. if (!(dep->flags & DWC3_EP_ENABLED))
  178. continue;
  179. if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
  180. || usb_endpoint_xfer_isoc(dep->endpoint.desc))
  181. mult = 3;
  182. /*
  183. * REVISIT: the following assumes we will always have enough
  184. * space available on the FIFO RAM for all possible use cases.
  185. * Make sure that's true somehow and change FIFO allocation
  186. * accordingly.
  187. *
  188. * If we have Bulk or Isochronous endpoints, we want
  189. * them to be able to be very, very fast. So we're giving
  190. * those endpoints a fifo_size which is enough for 3 full
  191. * packets
  192. */
  193. tmp = mult * (dep->endpoint.maxpacket + mdwidth);
  194. tmp += mdwidth;
  195. fifo_size = DIV_ROUND_UP(tmp, mdwidth);
  196. fifo_size |= (last_fifo_depth << 16);
  197. dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
  198. dep->name, last_fifo_depth, fifo_size & 0xffff);
  199. dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
  200. fifo_size);
  201. last_fifo_depth += (fifo_size & 0xffff);
  202. }
  203. return 0;
  204. }
  205. void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
  206. int status)
  207. {
  208. struct dwc3 *dwc = dep->dwc;
  209. int i;
  210. if (req->queued) {
  211. i = 0;
  212. do {
  213. dep->busy_slot++;
  214. /*
  215. * Skip LINK TRB. We can't use req->trb and check for
  216. * DWC3_TRBCTL_LINK_TRB because it points the TRB we
  217. * just completed (not the LINK TRB).
  218. */
  219. if (((dep->busy_slot & DWC3_TRB_MASK) ==
  220. DWC3_TRB_NUM- 1) &&
  221. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  222. dep->busy_slot++;
  223. } while(++i < req->request.num_mapped_sgs);
  224. req->queued = false;
  225. }
  226. list_del(&req->list);
  227. req->trb = NULL;
  228. if (req->request.status == -EINPROGRESS)
  229. req->request.status = status;
  230. if (dwc->ep0_bounced && dep->number == 0)
  231. dwc->ep0_bounced = false;
  232. else
  233. usb_gadget_unmap_request(&dwc->gadget, &req->request,
  234. req->direction);
  235. dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
  236. req, dep->name, req->request.actual,
  237. req->request.length, status);
  238. spin_unlock(&dwc->lock);
  239. req->request.complete(&dep->endpoint, &req->request);
  240. spin_lock(&dwc->lock);
  241. }
  242. static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
  243. {
  244. switch (cmd) {
  245. case DWC3_DEPCMD_DEPSTARTCFG:
  246. return "Start New Configuration";
  247. case DWC3_DEPCMD_ENDTRANSFER:
  248. return "End Transfer";
  249. case DWC3_DEPCMD_UPDATETRANSFER:
  250. return "Update Transfer";
  251. case DWC3_DEPCMD_STARTTRANSFER:
  252. return "Start Transfer";
  253. case DWC3_DEPCMD_CLEARSTALL:
  254. return "Clear Stall";
  255. case DWC3_DEPCMD_SETSTALL:
  256. return "Set Stall";
  257. case DWC3_DEPCMD_GETEPSTATE:
  258. return "Get Endpoint State";
  259. case DWC3_DEPCMD_SETTRANSFRESOURCE:
  260. return "Set Endpoint Transfer Resource";
  261. case DWC3_DEPCMD_SETEPCONFIG:
  262. return "Set Endpoint Configuration";
  263. default:
  264. return "UNKNOWN command";
  265. }
  266. }
  267. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
  268. {
  269. u32 timeout = 500;
  270. u32 reg;
  271. dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
  272. dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
  273. do {
  274. reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
  275. if (!(reg & DWC3_DGCMD_CMDACT)) {
  276. dev_vdbg(dwc->dev, "Command Complete --> %d\n",
  277. DWC3_DGCMD_STATUS(reg));
  278. return 0;
  279. }
  280. /*
  281. * We can't sleep here, because it's also called from
  282. * interrupt context.
  283. */
  284. timeout--;
  285. if (!timeout)
  286. return -ETIMEDOUT;
  287. udelay(1);
  288. } while (1);
  289. }
  290. int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
  291. unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
  292. {
  293. struct dwc3_ep *dep = dwc->eps[ep];
  294. u32 timeout = 500;
  295. u32 reg;
  296. dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
  297. dep->name,
  298. dwc3_gadget_ep_cmd_string(cmd), params->param0,
  299. params->param1, params->param2);
  300. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
  301. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
  302. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
  303. dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
  304. do {
  305. reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
  306. if (!(reg & DWC3_DEPCMD_CMDACT)) {
  307. dev_vdbg(dwc->dev, "Command Complete --> %d\n",
  308. DWC3_DEPCMD_STATUS(reg));
  309. return 0;
  310. }
  311. /*
  312. * We can't sleep here, because it is also called from
  313. * interrupt context.
  314. */
  315. timeout--;
  316. if (!timeout)
  317. return -ETIMEDOUT;
  318. udelay(1);
  319. } while (1);
  320. }
  321. static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
  322. struct dwc3_trb *trb)
  323. {
  324. u32 offset = (char *) trb - (char *) dep->trb_pool;
  325. return dep->trb_pool_dma + offset;
  326. }
  327. static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
  328. {
  329. struct dwc3 *dwc = dep->dwc;
  330. if (dep->trb_pool)
  331. return 0;
  332. if (dep->number == 0 || dep->number == 1)
  333. return 0;
  334. dep->trb_pool = dma_alloc_coherent(dwc->dev,
  335. sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  336. &dep->trb_pool_dma, GFP_KERNEL);
  337. if (!dep->trb_pool) {
  338. dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
  339. dep->name);
  340. return -ENOMEM;
  341. }
  342. return 0;
  343. }
  344. static void dwc3_free_trb_pool(struct dwc3_ep *dep)
  345. {
  346. struct dwc3 *dwc = dep->dwc;
  347. dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  348. dep->trb_pool, dep->trb_pool_dma);
  349. dep->trb_pool = NULL;
  350. dep->trb_pool_dma = 0;
  351. }
  352. static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
  353. {
  354. struct dwc3_gadget_ep_cmd_params params;
  355. u32 cmd;
  356. memset(&params, 0x00, sizeof(params));
  357. if (dep->number != 1) {
  358. cmd = DWC3_DEPCMD_DEPSTARTCFG;
  359. /* XferRscIdx == 0 for ep0 and 2 for the remaining */
  360. if (dep->number > 1) {
  361. if (dwc->start_config_issued)
  362. return 0;
  363. dwc->start_config_issued = true;
  364. cmd |= DWC3_DEPCMD_PARAM(2);
  365. }
  366. return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
  367. }
  368. return 0;
  369. }
  370. static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
  371. const struct usb_endpoint_descriptor *desc,
  372. const struct usb_ss_ep_comp_descriptor *comp_desc,
  373. bool ignore)
  374. {
  375. struct dwc3_gadget_ep_cmd_params params;
  376. memset(&params, 0x00, sizeof(params));
  377. params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
  378. | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
  379. /* Burst size is only needed in SuperSpeed mode */
  380. if (dwc->gadget.speed == USB_SPEED_SUPER) {
  381. u32 burst = dep->endpoint.maxburst - 1;
  382. params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
  383. }
  384. if (ignore)
  385. params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
  386. params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
  387. | DWC3_DEPCFG_XFER_NOT_READY_EN;
  388. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  389. params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
  390. | DWC3_DEPCFG_STREAM_EVENT_EN;
  391. dep->stream_capable = true;
  392. }
  393. if (usb_endpoint_xfer_isoc(desc))
  394. params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
  395. /*
  396. * We are doing 1:1 mapping for endpoints, meaning
  397. * Physical Endpoints 2 maps to Logical Endpoint 2 and
  398. * so on. We consider the direction bit as part of the physical
  399. * endpoint number. So USB endpoint 0x81 is 0x03.
  400. */
  401. params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
  402. /*
  403. * We must use the lower 16 TX FIFOs even though
  404. * HW might have more
  405. */
  406. if (dep->direction)
  407. params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
  408. if (desc->bInterval) {
  409. params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
  410. dep->interval = 1 << (desc->bInterval - 1);
  411. }
  412. return dwc3_send_gadget_ep_cmd(dwc, dep->number,
  413. DWC3_DEPCMD_SETEPCONFIG, &params);
  414. }
  415. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
  416. {
  417. struct dwc3_gadget_ep_cmd_params params;
  418. memset(&params, 0x00, sizeof(params));
  419. params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
  420. return dwc3_send_gadget_ep_cmd(dwc, dep->number,
  421. DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
  422. }
  423. /**
  424. * __dwc3_gadget_ep_enable - Initializes a HW endpoint
  425. * @dep: endpoint to be initialized
  426. * @desc: USB Endpoint Descriptor
  427. *
  428. * Caller should take care of locking
  429. */
  430. static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
  431. const struct usb_endpoint_descriptor *desc,
  432. const struct usb_ss_ep_comp_descriptor *comp_desc,
  433. bool ignore)
  434. {
  435. struct dwc3 *dwc = dep->dwc;
  436. u32 reg;
  437. int ret = -ENOMEM;
  438. if (!(dep->flags & DWC3_EP_ENABLED)) {
  439. ret = dwc3_gadget_start_config(dwc, dep);
  440. if (ret)
  441. return ret;
  442. }
  443. ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
  444. if (ret)
  445. return ret;
  446. if (!(dep->flags & DWC3_EP_ENABLED)) {
  447. struct dwc3_trb *trb_st_hw;
  448. struct dwc3_trb *trb_link;
  449. ret = dwc3_gadget_set_xfer_resource(dwc, dep);
  450. if (ret)
  451. return ret;
  452. dep->endpoint.desc = desc;
  453. dep->comp_desc = comp_desc;
  454. dep->type = usb_endpoint_type(desc);
  455. dep->flags |= DWC3_EP_ENABLED;
  456. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  457. reg |= DWC3_DALEPENA_EP(dep->number);
  458. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  459. if (!usb_endpoint_xfer_isoc(desc))
  460. return 0;
  461. memset(&trb_link, 0, sizeof(trb_link));
  462. /* Link TRB for ISOC. The HWO bit is never reset */
  463. trb_st_hw = &dep->trb_pool[0];
  464. trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  465. trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  466. trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  467. trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
  468. trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
  469. }
  470. return 0;
  471. }
  472. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
  473. static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
  474. {
  475. struct dwc3_request *req;
  476. if (!list_empty(&dep->req_queued)) {
  477. dwc3_stop_active_transfer(dwc, dep->number);
  478. /* - giveback all requests to gadget driver */
  479. while (!list_empty(&dep->req_queued)) {
  480. req = next_request(&dep->req_queued);
  481. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  482. }
  483. }
  484. while (!list_empty(&dep->request_list)) {
  485. req = next_request(&dep->request_list);
  486. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  487. }
  488. }
  489. /**
  490. * __dwc3_gadget_ep_disable - Disables a HW endpoint
  491. * @dep: the endpoint to disable
  492. *
  493. * This function also removes requests which are currently processed ny the
  494. * hardware and those which are not yet scheduled.
  495. * Caller should take care of locking.
  496. */
  497. static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  498. {
  499. struct dwc3 *dwc = dep->dwc;
  500. u32 reg;
  501. dwc3_remove_requests(dwc, dep);
  502. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  503. reg &= ~DWC3_DALEPENA_EP(dep->number);
  504. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  505. dep->stream_capable = false;
  506. dep->endpoint.desc = NULL;
  507. dep->comp_desc = NULL;
  508. dep->type = 0;
  509. dep->flags = 0;
  510. return 0;
  511. }
  512. /* -------------------------------------------------------------------------- */
  513. static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
  514. const struct usb_endpoint_descriptor *desc)
  515. {
  516. return -EINVAL;
  517. }
  518. static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
  519. {
  520. return -EINVAL;
  521. }
  522. /* -------------------------------------------------------------------------- */
  523. static int dwc3_gadget_ep_enable(struct usb_ep *ep,
  524. const struct usb_endpoint_descriptor *desc)
  525. {
  526. struct dwc3_ep *dep;
  527. struct dwc3 *dwc;
  528. unsigned long flags;
  529. int ret;
  530. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  531. pr_debug("dwc3: invalid parameters\n");
  532. return -EINVAL;
  533. }
  534. if (!desc->wMaxPacketSize) {
  535. pr_debug("dwc3: missing wMaxPacketSize\n");
  536. return -EINVAL;
  537. }
  538. dep = to_dwc3_ep(ep);
  539. dwc = dep->dwc;
  540. if (dep->flags & DWC3_EP_ENABLED) {
  541. dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
  542. dep->name);
  543. return 0;
  544. }
  545. switch (usb_endpoint_type(desc)) {
  546. case USB_ENDPOINT_XFER_CONTROL:
  547. strlcat(dep->name, "-control", sizeof(dep->name));
  548. break;
  549. case USB_ENDPOINT_XFER_ISOC:
  550. strlcat(dep->name, "-isoc", sizeof(dep->name));
  551. break;
  552. case USB_ENDPOINT_XFER_BULK:
  553. strlcat(dep->name, "-bulk", sizeof(dep->name));
  554. break;
  555. case USB_ENDPOINT_XFER_INT:
  556. strlcat(dep->name, "-int", sizeof(dep->name));
  557. break;
  558. default:
  559. dev_err(dwc->dev, "invalid endpoint transfer type\n");
  560. }
  561. dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
  562. spin_lock_irqsave(&dwc->lock, flags);
  563. ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
  564. spin_unlock_irqrestore(&dwc->lock, flags);
  565. return ret;
  566. }
  567. static int dwc3_gadget_ep_disable(struct usb_ep *ep)
  568. {
  569. struct dwc3_ep *dep;
  570. struct dwc3 *dwc;
  571. unsigned long flags;
  572. int ret;
  573. if (!ep) {
  574. pr_debug("dwc3: invalid parameters\n");
  575. return -EINVAL;
  576. }
  577. dep = to_dwc3_ep(ep);
  578. dwc = dep->dwc;
  579. if (!(dep->flags & DWC3_EP_ENABLED)) {
  580. dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
  581. dep->name);
  582. return 0;
  583. }
  584. snprintf(dep->name, sizeof(dep->name), "ep%d%s",
  585. dep->number >> 1,
  586. (dep->number & 1) ? "in" : "out");
  587. spin_lock_irqsave(&dwc->lock, flags);
  588. ret = __dwc3_gadget_ep_disable(dep);
  589. spin_unlock_irqrestore(&dwc->lock, flags);
  590. return ret;
  591. }
  592. static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
  593. gfp_t gfp_flags)
  594. {
  595. struct dwc3_request *req;
  596. struct dwc3_ep *dep = to_dwc3_ep(ep);
  597. struct dwc3 *dwc = dep->dwc;
  598. req = kzalloc(sizeof(*req), gfp_flags);
  599. if (!req) {
  600. dev_err(dwc->dev, "not enough memory\n");
  601. return NULL;
  602. }
  603. req->epnum = dep->number;
  604. req->dep = dep;
  605. return &req->request;
  606. }
  607. static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
  608. struct usb_request *request)
  609. {
  610. struct dwc3_request *req = to_dwc3_request(request);
  611. kfree(req);
  612. }
  613. /**
  614. * dwc3_prepare_one_trb - setup one TRB from one request
  615. * @dep: endpoint for which this request is prepared
  616. * @req: dwc3_request pointer
  617. */
  618. static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
  619. struct dwc3_request *req, dma_addr_t dma,
  620. unsigned length, unsigned last, unsigned chain, unsigned node)
  621. {
  622. struct dwc3 *dwc = dep->dwc;
  623. struct dwc3_trb *trb;
  624. dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
  625. dep->name, req, (unsigned long long) dma,
  626. length, last ? " last" : "",
  627. chain ? " chain" : "");
  628. /* Skip the LINK-TRB on ISOC */
  629. if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
  630. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  631. dep->free_slot++;
  632. trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
  633. if (!req->trb) {
  634. dwc3_gadget_move_request_queued(req);
  635. req->trb = trb;
  636. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  637. req->start_slot = dep->free_slot & DWC3_TRB_MASK;
  638. }
  639. dep->free_slot++;
  640. trb->size = DWC3_TRB_SIZE_LENGTH(length);
  641. trb->bpl = lower_32_bits(dma);
  642. trb->bph = upper_32_bits(dma);
  643. switch (usb_endpoint_type(dep->endpoint.desc)) {
  644. case USB_ENDPOINT_XFER_CONTROL:
  645. trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
  646. break;
  647. case USB_ENDPOINT_XFER_ISOC:
  648. if (!node)
  649. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  650. else
  651. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
  652. if (!req->request.no_interrupt && !chain)
  653. trb->ctrl |= DWC3_TRB_CTRL_IOC;
  654. break;
  655. case USB_ENDPOINT_XFER_BULK:
  656. case USB_ENDPOINT_XFER_INT:
  657. trb->ctrl = DWC3_TRBCTL_NORMAL;
  658. break;
  659. default:
  660. /*
  661. * This is only possible with faulty memory because we
  662. * checked it already :)
  663. */
  664. BUG();
  665. }
  666. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  667. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  668. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  669. } else if (last) {
  670. trb->ctrl |= DWC3_TRB_CTRL_LST;
  671. }
  672. if (chain)
  673. trb->ctrl |= DWC3_TRB_CTRL_CHN;
  674. if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
  675. trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
  676. trb->ctrl |= DWC3_TRB_CTRL_HWO;
  677. }
  678. /*
  679. * dwc3_prepare_trbs - setup TRBs from requests
  680. * @dep: endpoint for which requests are being prepared
  681. * @starting: true if the endpoint is idle and no requests are queued.
  682. *
  683. * The function goes through the requests list and sets up TRBs for the
  684. * transfers. The function returns once there are no more TRBs available or
  685. * it runs out of requests.
  686. */
  687. static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
  688. {
  689. struct dwc3_request *req, *n;
  690. u32 trbs_left;
  691. u32 max;
  692. unsigned int last_one = 0;
  693. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  694. /* the first request must not be queued */
  695. trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
  696. /* Can't wrap around on a non-isoc EP since there's no link TRB */
  697. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  698. max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
  699. if (trbs_left > max)
  700. trbs_left = max;
  701. }
  702. /*
  703. * If busy & slot are equal than it is either full or empty. If we are
  704. * starting to process requests then we are empty. Otherwise we are
  705. * full and don't do anything
  706. */
  707. if (!trbs_left) {
  708. if (!starting)
  709. return;
  710. trbs_left = DWC3_TRB_NUM;
  711. /*
  712. * In case we start from scratch, we queue the ISOC requests
  713. * starting from slot 1. This is done because we use ring
  714. * buffer and have no LST bit to stop us. Instead, we place
  715. * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
  716. * after the first request so we start at slot 1 and have
  717. * 7 requests proceed before we hit the first IOC.
  718. * Other transfer types don't use the ring buffer and are
  719. * processed from the first TRB until the last one. Since we
  720. * don't wrap around we have to start at the beginning.
  721. */
  722. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  723. dep->busy_slot = 1;
  724. dep->free_slot = 1;
  725. } else {
  726. dep->busy_slot = 0;
  727. dep->free_slot = 0;
  728. }
  729. }
  730. /* The last TRB is a link TRB, not used for xfer */
  731. if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
  732. return;
  733. list_for_each_entry_safe(req, n, &dep->request_list, list) {
  734. unsigned length;
  735. dma_addr_t dma;
  736. last_one = false;
  737. if (req->request.num_mapped_sgs > 0) {
  738. struct usb_request *request = &req->request;
  739. struct scatterlist *sg = request->sg;
  740. struct scatterlist *s;
  741. int i;
  742. for_each_sg(sg, s, request->num_mapped_sgs, i) {
  743. unsigned chain = true;
  744. length = sg_dma_len(s);
  745. dma = sg_dma_address(s);
  746. if (i == (request->num_mapped_sgs - 1) ||
  747. sg_is_last(s)) {
  748. if (list_is_last(&req->list,
  749. &dep->request_list))
  750. last_one = true;
  751. chain = false;
  752. }
  753. trbs_left--;
  754. if (!trbs_left)
  755. last_one = true;
  756. if (last_one)
  757. chain = false;
  758. dwc3_prepare_one_trb(dep, req, dma, length,
  759. last_one, chain, i);
  760. if (last_one)
  761. break;
  762. }
  763. } else {
  764. dma = req->request.dma;
  765. length = req->request.length;
  766. trbs_left--;
  767. if (!trbs_left)
  768. last_one = 1;
  769. /* Is this the last request? */
  770. if (list_is_last(&req->list, &dep->request_list))
  771. last_one = 1;
  772. dwc3_prepare_one_trb(dep, req, dma, length,
  773. last_one, false, 0);
  774. if (last_one)
  775. break;
  776. }
  777. }
  778. }
  779. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
  780. int start_new)
  781. {
  782. struct dwc3_gadget_ep_cmd_params params;
  783. struct dwc3_request *req;
  784. struct dwc3 *dwc = dep->dwc;
  785. int ret;
  786. u32 cmd;
  787. if (start_new && (dep->flags & DWC3_EP_BUSY)) {
  788. dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
  789. return -EBUSY;
  790. }
  791. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  792. /*
  793. * If we are getting here after a short-out-packet we don't enqueue any
  794. * new requests as we try to set the IOC bit only on the last request.
  795. */
  796. if (start_new) {
  797. if (list_empty(&dep->req_queued))
  798. dwc3_prepare_trbs(dep, start_new);
  799. /* req points to the first request which will be sent */
  800. req = next_request(&dep->req_queued);
  801. } else {
  802. dwc3_prepare_trbs(dep, start_new);
  803. /*
  804. * req points to the first request where HWO changed from 0 to 1
  805. */
  806. req = next_request(&dep->req_queued);
  807. }
  808. if (!req) {
  809. dep->flags |= DWC3_EP_PENDING_REQUEST;
  810. return 0;
  811. }
  812. memset(&params, 0, sizeof(params));
  813. if (start_new) {
  814. params.param0 = upper_32_bits(req->trb_dma);
  815. params.param1 = lower_32_bits(req->trb_dma);
  816. cmd = DWC3_DEPCMD_STARTTRANSFER;
  817. } else {
  818. cmd = DWC3_DEPCMD_UPDATETRANSFER;
  819. }
  820. cmd |= DWC3_DEPCMD_PARAM(cmd_param);
  821. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  822. if (ret < 0) {
  823. dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
  824. /*
  825. * FIXME we need to iterate over the list of requests
  826. * here and stop, unmap, free and del each of the linked
  827. * requests instead of what we do now.
  828. */
  829. usb_gadget_unmap_request(&dwc->gadget, &req->request,
  830. req->direction);
  831. list_del(&req->list);
  832. return ret;
  833. }
  834. dep->flags |= DWC3_EP_BUSY;
  835. if (start_new) {
  836. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
  837. dep->number);
  838. WARN_ON_ONCE(!dep->resource_index);
  839. }
  840. return 0;
  841. }
  842. static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
  843. struct dwc3_ep *dep, u32 cur_uf)
  844. {
  845. u32 uf;
  846. if (list_empty(&dep->request_list)) {
  847. dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
  848. dep->name);
  849. dep->flags |= DWC3_EP_PENDING_REQUEST;
  850. return;
  851. }
  852. /* 4 micro frames in the future */
  853. uf = cur_uf + dep->interval * 4;
  854. __dwc3_gadget_kick_transfer(dep, uf, 1);
  855. }
  856. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  857. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  858. {
  859. u32 cur_uf, mask;
  860. mask = ~(dep->interval - 1);
  861. cur_uf = event->parameters & mask;
  862. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  863. }
  864. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  865. {
  866. struct dwc3 *dwc = dep->dwc;
  867. int ret;
  868. req->request.actual = 0;
  869. req->request.status = -EINPROGRESS;
  870. req->direction = dep->direction;
  871. req->epnum = dep->number;
  872. /*
  873. * We only add to our list of requests now and
  874. * start consuming the list once we get XferNotReady
  875. * IRQ.
  876. *
  877. * That way, we avoid doing anything that we don't need
  878. * to do now and defer it until the point we receive a
  879. * particular token from the Host side.
  880. *
  881. * This will also avoid Host cancelling URBs due to too
  882. * many NAKs.
  883. */
  884. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  885. dep->direction);
  886. if (ret)
  887. return ret;
  888. list_add_tail(&req->list, &dep->request_list);
  889. /*
  890. * There are a few special cases:
  891. *
  892. * 1. XferNotReady with empty list of requests. We need to kick the
  893. * transfer here in that situation, otherwise we will be NAKing
  894. * forever. If we get XferNotReady before gadget driver has a
  895. * chance to queue a request, we will ACK the IRQ but won't be
  896. * able to receive the data until the next request is queued.
  897. * The following code is handling exactly that.
  898. *
  899. */
  900. if (dep->flags & DWC3_EP_PENDING_REQUEST) {
  901. /*
  902. * If xfernotready is already elapsed and it is a case
  903. * of isoc transfer, then issue END TRANSFER, so that
  904. * you can receive xfernotready again and can have
  905. * notion of current microframe.
  906. */
  907. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  908. if (list_empty(&dep->req_queued)) {
  909. dwc3_stop_active_transfer(dwc, dep->number);
  910. dep->flags = DWC3_EP_ENABLED;
  911. }
  912. return 0;
  913. }
  914. ret = __dwc3_gadget_kick_transfer(dep, 0, true);
  915. if (ret && ret != -EBUSY)
  916. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  917. dep->name);
  918. return ret;
  919. }
  920. /*
  921. * 2. XferInProgress on Isoc EP with an active transfer. We need to
  922. * kick the transfer here after queuing a request, otherwise the
  923. * core may not see the modified TRB(s).
  924. */
  925. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  926. (dep->flags & DWC3_EP_BUSY) &&
  927. !(dep->flags & DWC3_EP_MISSED_ISOC)) {
  928. WARN_ON_ONCE(!dep->resource_index);
  929. ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
  930. false);
  931. if (ret && ret != -EBUSY)
  932. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  933. dep->name);
  934. return ret;
  935. }
  936. return 0;
  937. }
  938. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  939. gfp_t gfp_flags)
  940. {
  941. struct dwc3_request *req = to_dwc3_request(request);
  942. struct dwc3_ep *dep = to_dwc3_ep(ep);
  943. struct dwc3 *dwc = dep->dwc;
  944. unsigned long flags;
  945. int ret;
  946. if (!dep->endpoint.desc) {
  947. dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
  948. request, ep->name);
  949. return -ESHUTDOWN;
  950. }
  951. dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
  952. request, ep->name, request->length);
  953. spin_lock_irqsave(&dwc->lock, flags);
  954. ret = __dwc3_gadget_ep_queue(dep, req);
  955. spin_unlock_irqrestore(&dwc->lock, flags);
  956. return ret;
  957. }
  958. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  959. struct usb_request *request)
  960. {
  961. struct dwc3_request *req = to_dwc3_request(request);
  962. struct dwc3_request *r = NULL;
  963. struct dwc3_ep *dep = to_dwc3_ep(ep);
  964. struct dwc3 *dwc = dep->dwc;
  965. unsigned long flags;
  966. int ret = 0;
  967. spin_lock_irqsave(&dwc->lock, flags);
  968. list_for_each_entry(r, &dep->request_list, list) {
  969. if (r == req)
  970. break;
  971. }
  972. if (r != req) {
  973. list_for_each_entry(r, &dep->req_queued, list) {
  974. if (r == req)
  975. break;
  976. }
  977. if (r == req) {
  978. /* wait until it is processed */
  979. dwc3_stop_active_transfer(dwc, dep->number);
  980. goto out1;
  981. }
  982. dev_err(dwc->dev, "request %p was not queued to %s\n",
  983. request, ep->name);
  984. ret = -EINVAL;
  985. goto out0;
  986. }
  987. out1:
  988. /* giveback the request */
  989. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  990. out0:
  991. spin_unlock_irqrestore(&dwc->lock, flags);
  992. return ret;
  993. }
  994. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
  995. {
  996. struct dwc3_gadget_ep_cmd_params params;
  997. struct dwc3 *dwc = dep->dwc;
  998. int ret;
  999. memset(&params, 0x00, sizeof(params));
  1000. if (value) {
  1001. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1002. DWC3_DEPCMD_SETSTALL, &params);
  1003. if (ret)
  1004. dev_err(dwc->dev, "failed to %s STALL on %s\n",
  1005. value ? "set" : "clear",
  1006. dep->name);
  1007. else
  1008. dep->flags |= DWC3_EP_STALL;
  1009. } else {
  1010. if (dep->flags & DWC3_EP_WEDGE)
  1011. return 0;
  1012. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1013. DWC3_DEPCMD_CLEARSTALL, &params);
  1014. if (ret)
  1015. dev_err(dwc->dev, "failed to %s STALL on %s\n",
  1016. value ? "set" : "clear",
  1017. dep->name);
  1018. else
  1019. dep->flags &= ~DWC3_EP_STALL;
  1020. }
  1021. return ret;
  1022. }
  1023. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1024. {
  1025. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1026. struct dwc3 *dwc = dep->dwc;
  1027. unsigned long flags;
  1028. int ret;
  1029. spin_lock_irqsave(&dwc->lock, flags);
  1030. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1031. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1032. ret = -EINVAL;
  1033. goto out;
  1034. }
  1035. ret = __dwc3_gadget_ep_set_halt(dep, value);
  1036. out:
  1037. spin_unlock_irqrestore(&dwc->lock, flags);
  1038. return ret;
  1039. }
  1040. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1041. {
  1042. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1043. struct dwc3 *dwc = dep->dwc;
  1044. unsigned long flags;
  1045. spin_lock_irqsave(&dwc->lock, flags);
  1046. dep->flags |= DWC3_EP_WEDGE;
  1047. spin_unlock_irqrestore(&dwc->lock, flags);
  1048. if (dep->number == 0 || dep->number == 1)
  1049. return dwc3_gadget_ep0_set_halt(ep, 1);
  1050. else
  1051. return dwc3_gadget_ep_set_halt(ep, 1);
  1052. }
  1053. /* -------------------------------------------------------------------------- */
  1054. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1055. .bLength = USB_DT_ENDPOINT_SIZE,
  1056. .bDescriptorType = USB_DT_ENDPOINT,
  1057. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1058. };
  1059. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1060. .enable = dwc3_gadget_ep0_enable,
  1061. .disable = dwc3_gadget_ep0_disable,
  1062. .alloc_request = dwc3_gadget_ep_alloc_request,
  1063. .free_request = dwc3_gadget_ep_free_request,
  1064. .queue = dwc3_gadget_ep0_queue,
  1065. .dequeue = dwc3_gadget_ep_dequeue,
  1066. .set_halt = dwc3_gadget_ep0_set_halt,
  1067. .set_wedge = dwc3_gadget_ep_set_wedge,
  1068. };
  1069. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1070. .enable = dwc3_gadget_ep_enable,
  1071. .disable = dwc3_gadget_ep_disable,
  1072. .alloc_request = dwc3_gadget_ep_alloc_request,
  1073. .free_request = dwc3_gadget_ep_free_request,
  1074. .queue = dwc3_gadget_ep_queue,
  1075. .dequeue = dwc3_gadget_ep_dequeue,
  1076. .set_halt = dwc3_gadget_ep_set_halt,
  1077. .set_wedge = dwc3_gadget_ep_set_wedge,
  1078. };
  1079. /* -------------------------------------------------------------------------- */
  1080. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1081. {
  1082. struct dwc3 *dwc = gadget_to_dwc(g);
  1083. u32 reg;
  1084. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1085. return DWC3_DSTS_SOFFN(reg);
  1086. }
  1087. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1088. {
  1089. struct dwc3 *dwc = gadget_to_dwc(g);
  1090. unsigned long timeout;
  1091. unsigned long flags;
  1092. u32 reg;
  1093. int ret = 0;
  1094. u8 link_state;
  1095. u8 speed;
  1096. spin_lock_irqsave(&dwc->lock, flags);
  1097. /*
  1098. * According to the Databook Remote wakeup request should
  1099. * be issued only when the device is in early suspend state.
  1100. *
  1101. * We can check that via USB Link State bits in DSTS register.
  1102. */
  1103. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1104. speed = reg & DWC3_DSTS_CONNECTSPD;
  1105. if (speed == DWC3_DSTS_SUPERSPEED) {
  1106. dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
  1107. ret = -EINVAL;
  1108. goto out;
  1109. }
  1110. link_state = DWC3_DSTS_USBLNKST(reg);
  1111. switch (link_state) {
  1112. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1113. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1114. break;
  1115. default:
  1116. dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
  1117. link_state);
  1118. ret = -EINVAL;
  1119. goto out;
  1120. }
  1121. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1122. if (ret < 0) {
  1123. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1124. goto out;
  1125. }
  1126. /* Recent versions do this automatically */
  1127. if (dwc->revision < DWC3_REVISION_194A) {
  1128. /* write zeroes to Link Change Request */
  1129. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1130. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1131. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1132. }
  1133. /* poll until Link State changes to ON */
  1134. timeout = jiffies + msecs_to_jiffies(100);
  1135. while (!time_after(jiffies, timeout)) {
  1136. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1137. /* in HS, means ON */
  1138. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1139. break;
  1140. }
  1141. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1142. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1143. ret = -EINVAL;
  1144. }
  1145. out:
  1146. spin_unlock_irqrestore(&dwc->lock, flags);
  1147. return ret;
  1148. }
  1149. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  1150. int is_selfpowered)
  1151. {
  1152. struct dwc3 *dwc = gadget_to_dwc(g);
  1153. unsigned long flags;
  1154. spin_lock_irqsave(&dwc->lock, flags);
  1155. dwc->is_selfpowered = !!is_selfpowered;
  1156. spin_unlock_irqrestore(&dwc->lock, flags);
  1157. return 0;
  1158. }
  1159. static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
  1160. {
  1161. u32 reg;
  1162. u32 timeout = 500;
  1163. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1164. if (is_on) {
  1165. if (dwc->revision <= DWC3_REVISION_187A) {
  1166. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  1167. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  1168. }
  1169. if (dwc->revision >= DWC3_REVISION_194A)
  1170. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1171. reg |= DWC3_DCTL_RUN_STOP;
  1172. dwc->pullups_connected = true;
  1173. } else {
  1174. reg &= ~DWC3_DCTL_RUN_STOP;
  1175. dwc->pullups_connected = false;
  1176. }
  1177. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1178. do {
  1179. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1180. if (is_on) {
  1181. if (!(reg & DWC3_DSTS_DEVCTRLHLT))
  1182. break;
  1183. } else {
  1184. if (reg & DWC3_DSTS_DEVCTRLHLT)
  1185. break;
  1186. }
  1187. timeout--;
  1188. if (!timeout)
  1189. return -ETIMEDOUT;
  1190. udelay(1);
  1191. } while (1);
  1192. dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
  1193. dwc->gadget_driver
  1194. ? dwc->gadget_driver->function : "no-function",
  1195. is_on ? "connect" : "disconnect");
  1196. return 0;
  1197. }
  1198. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  1199. {
  1200. struct dwc3 *dwc = gadget_to_dwc(g);
  1201. unsigned long flags;
  1202. int ret;
  1203. is_on = !!is_on;
  1204. spin_lock_irqsave(&dwc->lock, flags);
  1205. ret = dwc3_gadget_run_stop(dwc, is_on);
  1206. spin_unlock_irqrestore(&dwc->lock, flags);
  1207. return ret;
  1208. }
  1209. static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
  1210. {
  1211. u32 reg;
  1212. /* Enable all but Start and End of Frame IRQs */
  1213. reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
  1214. DWC3_DEVTEN_EVNTOVERFLOWEN |
  1215. DWC3_DEVTEN_CMDCMPLTEN |
  1216. DWC3_DEVTEN_ERRTICERREN |
  1217. DWC3_DEVTEN_WKUPEVTEN |
  1218. DWC3_DEVTEN_ULSTCNGEN |
  1219. DWC3_DEVTEN_CONNECTDONEEN |
  1220. DWC3_DEVTEN_USBRSTEN |
  1221. DWC3_DEVTEN_DISCONNEVTEN);
  1222. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  1223. }
  1224. static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
  1225. {
  1226. /* mask all interrupts */
  1227. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  1228. }
  1229. static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
  1230. static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
  1231. static int dwc3_gadget_start(struct usb_gadget *g,
  1232. struct usb_gadget_driver *driver)
  1233. {
  1234. struct dwc3 *dwc = gadget_to_dwc(g);
  1235. struct dwc3_ep *dep;
  1236. unsigned long flags;
  1237. int ret = 0;
  1238. int irq;
  1239. u32 reg;
  1240. spin_lock_irqsave(&dwc->lock, flags);
  1241. if (dwc->gadget_driver) {
  1242. dev_err(dwc->dev, "%s is already bound to %s\n",
  1243. dwc->gadget.name,
  1244. dwc->gadget_driver->driver.name);
  1245. ret = -EBUSY;
  1246. goto err0;
  1247. }
  1248. dwc->gadget_driver = driver;
  1249. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1250. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1251. /**
  1252. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1253. * which would cause metastability state on Run/Stop
  1254. * bit if we try to force the IP to USB2-only mode.
  1255. *
  1256. * Because of that, we cannot configure the IP to any
  1257. * speed other than the SuperSpeed
  1258. *
  1259. * Refers to:
  1260. *
  1261. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1262. * USB 2.0 Mode
  1263. */
  1264. if (dwc->revision < DWC3_REVISION_220A)
  1265. reg |= DWC3_DCFG_SUPERSPEED;
  1266. else
  1267. reg |= dwc->maximum_speed;
  1268. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1269. dwc->start_config_issued = false;
  1270. /* Start with SuperSpeed Default */
  1271. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1272. dep = dwc->eps[0];
  1273. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  1274. if (ret) {
  1275. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1276. goto err0;
  1277. }
  1278. dep = dwc->eps[1];
  1279. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  1280. if (ret) {
  1281. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1282. goto err1;
  1283. }
  1284. /* begin to receive SETUP packets */
  1285. dwc->ep0state = EP0_SETUP_PHASE;
  1286. dwc3_ep0_out_start(dwc);
  1287. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  1288. ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
  1289. IRQF_SHARED | IRQF_ONESHOT, "dwc3", dwc);
  1290. if (ret) {
  1291. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  1292. irq, ret);
  1293. goto err1;
  1294. }
  1295. dwc3_gadget_enable_irq(dwc);
  1296. spin_unlock_irqrestore(&dwc->lock, flags);
  1297. return 0;
  1298. err1:
  1299. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1300. err0:
  1301. spin_unlock_irqrestore(&dwc->lock, flags);
  1302. return ret;
  1303. }
  1304. static int dwc3_gadget_stop(struct usb_gadget *g,
  1305. struct usb_gadget_driver *driver)
  1306. {
  1307. struct dwc3 *dwc = gadget_to_dwc(g);
  1308. unsigned long flags;
  1309. int irq;
  1310. spin_lock_irqsave(&dwc->lock, flags);
  1311. dwc3_gadget_disable_irq(dwc);
  1312. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  1313. free_irq(irq, dwc);
  1314. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1315. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1316. dwc->gadget_driver = NULL;
  1317. spin_unlock_irqrestore(&dwc->lock, flags);
  1318. return 0;
  1319. }
  1320. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1321. .get_frame = dwc3_gadget_get_frame,
  1322. .wakeup = dwc3_gadget_wakeup,
  1323. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1324. .pullup = dwc3_gadget_pullup,
  1325. .udc_start = dwc3_gadget_start,
  1326. .udc_stop = dwc3_gadget_stop,
  1327. };
  1328. /* -------------------------------------------------------------------------- */
  1329. static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
  1330. u8 num, u32 direction)
  1331. {
  1332. struct dwc3_ep *dep;
  1333. u8 i;
  1334. for (i = 0; i < num; i++) {
  1335. u8 epnum = (i << 1) | (!!direction);
  1336. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1337. if (!dep) {
  1338. dev_err(dwc->dev, "can't allocate endpoint %d\n",
  1339. epnum);
  1340. return -ENOMEM;
  1341. }
  1342. dep->dwc = dwc;
  1343. dep->number = epnum;
  1344. dwc->eps[epnum] = dep;
  1345. snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
  1346. (epnum & 1) ? "in" : "out");
  1347. dep->endpoint.name = dep->name;
  1348. dep->direction = (epnum & 1);
  1349. if (epnum == 0 || epnum == 1) {
  1350. dep->endpoint.maxpacket = 512;
  1351. dep->endpoint.maxburst = 1;
  1352. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1353. if (!epnum)
  1354. dwc->gadget.ep0 = &dep->endpoint;
  1355. } else {
  1356. int ret;
  1357. dep->endpoint.maxpacket = 1024;
  1358. dep->endpoint.max_streams = 15;
  1359. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1360. list_add_tail(&dep->endpoint.ep_list,
  1361. &dwc->gadget.ep_list);
  1362. ret = dwc3_alloc_trb_pool(dep);
  1363. if (ret)
  1364. return ret;
  1365. }
  1366. INIT_LIST_HEAD(&dep->request_list);
  1367. INIT_LIST_HEAD(&dep->req_queued);
  1368. }
  1369. return 0;
  1370. }
  1371. static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
  1372. {
  1373. int ret;
  1374. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1375. ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
  1376. if (ret < 0) {
  1377. dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
  1378. return ret;
  1379. }
  1380. ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
  1381. if (ret < 0) {
  1382. dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
  1383. return ret;
  1384. }
  1385. return 0;
  1386. }
  1387. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1388. {
  1389. struct dwc3_ep *dep;
  1390. u8 epnum;
  1391. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1392. dep = dwc->eps[epnum];
  1393. if (!dep)
  1394. continue;
  1395. /*
  1396. * Physical endpoints 0 and 1 are special; they form the
  1397. * bi-directional USB endpoint 0.
  1398. *
  1399. * For those two physical endpoints, we don't allocate a TRB
  1400. * pool nor do we add them the endpoints list. Due to that, we
  1401. * shouldn't do these two operations otherwise we would end up
  1402. * with all sorts of bugs when removing dwc3.ko.
  1403. */
  1404. if (epnum != 0 && epnum != 1) {
  1405. dwc3_free_trb_pool(dep);
  1406. list_del(&dep->endpoint.ep_list);
  1407. }
  1408. kfree(dep);
  1409. }
  1410. }
  1411. /* -------------------------------------------------------------------------- */
  1412. static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1413. struct dwc3_request *req, struct dwc3_trb *trb,
  1414. const struct dwc3_event_depevt *event, int status)
  1415. {
  1416. unsigned int count;
  1417. unsigned int s_pkt = 0;
  1418. unsigned int trb_status;
  1419. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1420. /*
  1421. * We continue despite the error. There is not much we
  1422. * can do. If we don't clean it up we loop forever. If
  1423. * we skip the TRB then it gets overwritten after a
  1424. * while since we use them in a ring buffer. A BUG()
  1425. * would help. Lets hope that if this occurs, someone
  1426. * fixes the root cause instead of looking away :)
  1427. */
  1428. dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
  1429. dep->name, trb);
  1430. count = trb->size & DWC3_TRB_SIZE_MASK;
  1431. if (dep->direction) {
  1432. if (count) {
  1433. trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  1434. if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
  1435. dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
  1436. dep->name);
  1437. /*
  1438. * If missed isoc occurred and there is
  1439. * no request queued then issue END
  1440. * TRANSFER, so that core generates
  1441. * next xfernotready and we will issue
  1442. * a fresh START TRANSFER.
  1443. * If there are still queued request
  1444. * then wait, do not issue either END
  1445. * or UPDATE TRANSFER, just attach next
  1446. * request in request_list during
  1447. * giveback.If any future queued request
  1448. * is successfully transferred then we
  1449. * will issue UPDATE TRANSFER for all
  1450. * request in the request_list.
  1451. */
  1452. dep->flags |= DWC3_EP_MISSED_ISOC;
  1453. } else {
  1454. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1455. dep->name);
  1456. status = -ECONNRESET;
  1457. }
  1458. } else {
  1459. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  1460. }
  1461. } else {
  1462. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1463. s_pkt = 1;
  1464. }
  1465. /*
  1466. * We assume here we will always receive the entire data block
  1467. * which we should receive. Meaning, if we program RX to
  1468. * receive 4K but we receive only 2K, we assume that's all we
  1469. * should receive and we simply bounce the request back to the
  1470. * gadget driver for further processing.
  1471. */
  1472. req->request.actual += req->request.length - count;
  1473. if (s_pkt)
  1474. return 1;
  1475. if ((event->status & DEPEVT_STATUS_LST) &&
  1476. (trb->ctrl & (DWC3_TRB_CTRL_LST |
  1477. DWC3_TRB_CTRL_HWO)))
  1478. return 1;
  1479. if ((event->status & DEPEVT_STATUS_IOC) &&
  1480. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1481. return 1;
  1482. return 0;
  1483. }
  1484. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1485. const struct dwc3_event_depevt *event, int status)
  1486. {
  1487. struct dwc3_request *req;
  1488. struct dwc3_trb *trb;
  1489. unsigned int slot;
  1490. unsigned int i;
  1491. int ret;
  1492. do {
  1493. req = next_request(&dep->req_queued);
  1494. if (!req) {
  1495. WARN_ON_ONCE(1);
  1496. return 1;
  1497. }
  1498. i = 0;
  1499. do {
  1500. slot = req->start_slot + i;
  1501. if ((slot == DWC3_TRB_NUM - 1) &&
  1502. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  1503. slot++;
  1504. slot %= DWC3_TRB_NUM;
  1505. trb = &dep->trb_pool[slot];
  1506. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1507. event, status);
  1508. if (ret)
  1509. break;
  1510. }while (++i < req->request.num_mapped_sgs);
  1511. dwc3_gadget_giveback(dep, req, status);
  1512. if (ret)
  1513. break;
  1514. } while (1);
  1515. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  1516. list_empty(&dep->req_queued)) {
  1517. if (list_empty(&dep->request_list)) {
  1518. /*
  1519. * If there is no entry in request list then do
  1520. * not issue END TRANSFER now. Just set PENDING
  1521. * flag, so that END TRANSFER is issued when an
  1522. * entry is added into request list.
  1523. */
  1524. dep->flags = DWC3_EP_PENDING_REQUEST;
  1525. } else {
  1526. dwc3_stop_active_transfer(dwc, dep->number);
  1527. dep->flags = DWC3_EP_ENABLED;
  1528. }
  1529. return 1;
  1530. }
  1531. if ((event->status & DEPEVT_STATUS_IOC) &&
  1532. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1533. return 0;
  1534. return 1;
  1535. }
  1536. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1537. struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
  1538. int start_new)
  1539. {
  1540. unsigned status = 0;
  1541. int clean_busy;
  1542. if (event->status & DEPEVT_STATUS_BUSERR)
  1543. status = -ECONNRESET;
  1544. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  1545. if (clean_busy)
  1546. dep->flags &= ~DWC3_EP_BUSY;
  1547. /*
  1548. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  1549. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  1550. */
  1551. if (dwc->revision < DWC3_REVISION_183A) {
  1552. u32 reg;
  1553. int i;
  1554. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  1555. dep = dwc->eps[i];
  1556. if (!(dep->flags & DWC3_EP_ENABLED))
  1557. continue;
  1558. if (!list_empty(&dep->req_queued))
  1559. return;
  1560. }
  1561. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1562. reg |= dwc->u1u2;
  1563. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1564. dwc->u1u2 = 0;
  1565. }
  1566. }
  1567. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  1568. const struct dwc3_event_depevt *event)
  1569. {
  1570. struct dwc3_ep *dep;
  1571. u8 epnum = event->endpoint_number;
  1572. dep = dwc->eps[epnum];
  1573. if (!(dep->flags & DWC3_EP_ENABLED))
  1574. return;
  1575. dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
  1576. dwc3_ep_event_string(event->endpoint_event));
  1577. if (epnum == 0 || epnum == 1) {
  1578. dwc3_ep0_interrupt(dwc, event);
  1579. return;
  1580. }
  1581. switch (event->endpoint_event) {
  1582. case DWC3_DEPEVT_XFERCOMPLETE:
  1583. dep->resource_index = 0;
  1584. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1585. dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
  1586. dep->name);
  1587. return;
  1588. }
  1589. dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
  1590. break;
  1591. case DWC3_DEPEVT_XFERINPROGRESS:
  1592. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1593. dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
  1594. dep->name);
  1595. return;
  1596. }
  1597. dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
  1598. break;
  1599. case DWC3_DEPEVT_XFERNOTREADY:
  1600. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1601. dwc3_gadget_start_isoc(dwc, dep, event);
  1602. } else {
  1603. int ret;
  1604. dev_vdbg(dwc->dev, "%s: reason %s\n",
  1605. dep->name, event->status &
  1606. DEPEVT_STATUS_TRANSFER_ACTIVE
  1607. ? "Transfer Active"
  1608. : "Transfer Not Active");
  1609. ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
  1610. if (!ret || ret == -EBUSY)
  1611. return;
  1612. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  1613. dep->name);
  1614. }
  1615. break;
  1616. case DWC3_DEPEVT_STREAMEVT:
  1617. if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
  1618. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  1619. dep->name);
  1620. return;
  1621. }
  1622. switch (event->status) {
  1623. case DEPEVT_STREAMEVT_FOUND:
  1624. dev_vdbg(dwc->dev, "Stream %d found and started\n",
  1625. event->parameters);
  1626. break;
  1627. case DEPEVT_STREAMEVT_NOTFOUND:
  1628. /* FALLTHROUGH */
  1629. default:
  1630. dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
  1631. }
  1632. break;
  1633. case DWC3_DEPEVT_RXTXFIFOEVT:
  1634. dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
  1635. break;
  1636. case DWC3_DEPEVT_EPCMDCMPLT:
  1637. dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
  1638. break;
  1639. }
  1640. }
  1641. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  1642. {
  1643. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  1644. spin_unlock(&dwc->lock);
  1645. dwc->gadget_driver->disconnect(&dwc->gadget);
  1646. spin_lock(&dwc->lock);
  1647. }
  1648. }
  1649. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
  1650. {
  1651. struct dwc3_ep *dep;
  1652. struct dwc3_gadget_ep_cmd_params params;
  1653. u32 cmd;
  1654. int ret;
  1655. dep = dwc->eps[epnum];
  1656. if (!dep->resource_index)
  1657. return;
  1658. /*
  1659. * NOTICE: We are violating what the Databook says about the
  1660. * EndTransfer command. Ideally we would _always_ wait for the
  1661. * EndTransfer Command Completion IRQ, but that's causing too
  1662. * much trouble synchronizing between us and gadget driver.
  1663. *
  1664. * We have discussed this with the IP Provider and it was
  1665. * suggested to giveback all requests here, but give HW some
  1666. * extra time to synchronize with the interconnect. We're using
  1667. * an arbitraty 100us delay for that.
  1668. *
  1669. * Note also that a similar handling was tested by Synopsys
  1670. * (thanks a lot Paul) and nothing bad has come out of it.
  1671. * In short, what we're doing is:
  1672. *
  1673. * - Issue EndTransfer WITH CMDIOC bit set
  1674. * - Wait 100us
  1675. */
  1676. cmd = DWC3_DEPCMD_ENDTRANSFER;
  1677. cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
  1678. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  1679. memset(&params, 0, sizeof(params));
  1680. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  1681. WARN_ON_ONCE(ret);
  1682. dep->resource_index = 0;
  1683. dep->flags &= ~DWC3_EP_BUSY;
  1684. udelay(100);
  1685. }
  1686. static void dwc3_stop_active_transfers(struct dwc3 *dwc)
  1687. {
  1688. u32 epnum;
  1689. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1690. struct dwc3_ep *dep;
  1691. dep = dwc->eps[epnum];
  1692. if (!dep)
  1693. continue;
  1694. if (!(dep->flags & DWC3_EP_ENABLED))
  1695. continue;
  1696. dwc3_remove_requests(dwc, dep);
  1697. }
  1698. }
  1699. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  1700. {
  1701. u32 epnum;
  1702. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1703. struct dwc3_ep *dep;
  1704. struct dwc3_gadget_ep_cmd_params params;
  1705. int ret;
  1706. dep = dwc->eps[epnum];
  1707. if (!dep)
  1708. continue;
  1709. if (!(dep->flags & DWC3_EP_STALL))
  1710. continue;
  1711. dep->flags &= ~DWC3_EP_STALL;
  1712. memset(&params, 0, sizeof(params));
  1713. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1714. DWC3_DEPCMD_CLEARSTALL, &params);
  1715. WARN_ON_ONCE(ret);
  1716. }
  1717. }
  1718. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  1719. {
  1720. int reg;
  1721. dev_vdbg(dwc->dev, "%s\n", __func__);
  1722. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1723. reg &= ~DWC3_DCTL_INITU1ENA;
  1724. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1725. reg &= ~DWC3_DCTL_INITU2ENA;
  1726. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1727. dwc3_disconnect_gadget(dwc);
  1728. dwc->start_config_issued = false;
  1729. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  1730. dwc->setup_packet_pending = false;
  1731. }
  1732. static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
  1733. {
  1734. u32 reg;
  1735. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  1736. if (suspend)
  1737. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  1738. else
  1739. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  1740. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  1741. }
  1742. static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
  1743. {
  1744. u32 reg;
  1745. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1746. if (suspend)
  1747. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  1748. else
  1749. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  1750. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1751. }
  1752. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  1753. {
  1754. u32 reg;
  1755. dev_vdbg(dwc->dev, "%s\n", __func__);
  1756. /*
  1757. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  1758. * would cause a missing Disconnect Event if there's a
  1759. * pending Setup Packet in the FIFO.
  1760. *
  1761. * There's no suggested workaround on the official Bug
  1762. * report, which states that "unless the driver/application
  1763. * is doing any special handling of a disconnect event,
  1764. * there is no functional issue".
  1765. *
  1766. * Unfortunately, it turns out that we _do_ some special
  1767. * handling of a disconnect event, namely complete all
  1768. * pending transfers, notify gadget driver of the
  1769. * disconnection, and so on.
  1770. *
  1771. * Our suggested workaround is to follow the Disconnect
  1772. * Event steps here, instead, based on a setup_packet_pending
  1773. * flag. Such flag gets set whenever we have a XferNotReady
  1774. * event on EP0 and gets cleared on XferComplete for the
  1775. * same endpoint.
  1776. *
  1777. * Refers to:
  1778. *
  1779. * STAR#9000466709: RTL: Device : Disconnect event not
  1780. * generated if setup packet pending in FIFO
  1781. */
  1782. if (dwc->revision < DWC3_REVISION_188A) {
  1783. if (dwc->setup_packet_pending)
  1784. dwc3_gadget_disconnect_interrupt(dwc);
  1785. }
  1786. /* after reset -> Default State */
  1787. usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
  1788. /* Recent versions support automatic phy suspend and don't need this */
  1789. if (dwc->revision < DWC3_REVISION_194A) {
  1790. /* Resume PHYs */
  1791. dwc3_gadget_usb2_phy_suspend(dwc, false);
  1792. dwc3_gadget_usb3_phy_suspend(dwc, false);
  1793. }
  1794. if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
  1795. dwc3_disconnect_gadget(dwc);
  1796. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1797. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  1798. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1799. dwc->test_mode = false;
  1800. dwc3_stop_active_transfers(dwc);
  1801. dwc3_clear_stall_all_ep(dwc);
  1802. dwc->start_config_issued = false;
  1803. /* Reset device address to zero */
  1804. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1805. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  1806. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1807. }
  1808. static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
  1809. {
  1810. u32 reg;
  1811. u32 usb30_clock = DWC3_GCTL_CLK_BUS;
  1812. /*
  1813. * We change the clock only at SS but I dunno why I would want to do
  1814. * this. Maybe it becomes part of the power saving plan.
  1815. */
  1816. if (speed != DWC3_DSTS_SUPERSPEED)
  1817. return;
  1818. /*
  1819. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  1820. * each time on Connect Done.
  1821. */
  1822. if (!usb30_clock)
  1823. return;
  1824. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  1825. reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
  1826. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  1827. }
  1828. static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
  1829. {
  1830. switch (speed) {
  1831. case USB_SPEED_SUPER:
  1832. dwc3_gadget_usb2_phy_suspend(dwc, true);
  1833. break;
  1834. case USB_SPEED_HIGH:
  1835. case USB_SPEED_FULL:
  1836. case USB_SPEED_LOW:
  1837. dwc3_gadget_usb3_phy_suspend(dwc, true);
  1838. break;
  1839. }
  1840. }
  1841. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  1842. {
  1843. struct dwc3_ep *dep;
  1844. int ret;
  1845. u32 reg;
  1846. u8 speed;
  1847. dev_vdbg(dwc->dev, "%s\n", __func__);
  1848. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1849. speed = reg & DWC3_DSTS_CONNECTSPD;
  1850. dwc->speed = speed;
  1851. dwc3_update_ram_clk_sel(dwc, speed);
  1852. switch (speed) {
  1853. case DWC3_DCFG_SUPERSPEED:
  1854. /*
  1855. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  1856. * would cause a missing USB3 Reset event.
  1857. *
  1858. * In such situations, we should force a USB3 Reset
  1859. * event by calling our dwc3_gadget_reset_interrupt()
  1860. * routine.
  1861. *
  1862. * Refers to:
  1863. *
  1864. * STAR#9000483510: RTL: SS : USB3 reset event may
  1865. * not be generated always when the link enters poll
  1866. */
  1867. if (dwc->revision < DWC3_REVISION_190A)
  1868. dwc3_gadget_reset_interrupt(dwc);
  1869. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1870. dwc->gadget.ep0->maxpacket = 512;
  1871. dwc->gadget.speed = USB_SPEED_SUPER;
  1872. break;
  1873. case DWC3_DCFG_HIGHSPEED:
  1874. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1875. dwc->gadget.ep0->maxpacket = 64;
  1876. dwc->gadget.speed = USB_SPEED_HIGH;
  1877. break;
  1878. case DWC3_DCFG_FULLSPEED2:
  1879. case DWC3_DCFG_FULLSPEED1:
  1880. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1881. dwc->gadget.ep0->maxpacket = 64;
  1882. dwc->gadget.speed = USB_SPEED_FULL;
  1883. break;
  1884. case DWC3_DCFG_LOWSPEED:
  1885. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  1886. dwc->gadget.ep0->maxpacket = 8;
  1887. dwc->gadget.speed = USB_SPEED_LOW;
  1888. break;
  1889. }
  1890. /* Enable USB2 LPM Capability */
  1891. if ((dwc->revision > DWC3_REVISION_194A)
  1892. && (speed != DWC3_DCFG_SUPERSPEED)) {
  1893. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1894. reg |= DWC3_DCFG_LPM_CAP;
  1895. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1896. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1897. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  1898. /*
  1899. * TODO: This should be configurable. For now using
  1900. * maximum allowed HIRD threshold value of 0b1100
  1901. */
  1902. reg |= DWC3_DCTL_HIRD_THRES(12);
  1903. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1904. }
  1905. /* Recent versions support automatic phy suspend and don't need this */
  1906. if (dwc->revision < DWC3_REVISION_194A) {
  1907. /* Suspend unneeded PHY */
  1908. dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
  1909. }
  1910. dep = dwc->eps[0];
  1911. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
  1912. if (ret) {
  1913. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1914. return;
  1915. }
  1916. dep = dwc->eps[1];
  1917. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
  1918. if (ret) {
  1919. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1920. return;
  1921. }
  1922. /*
  1923. * Configure PHY via GUSB3PIPECTLn if required.
  1924. *
  1925. * Update GTXFIFOSIZn
  1926. *
  1927. * In both cases reset values should be sufficient.
  1928. */
  1929. }
  1930. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  1931. {
  1932. dev_vdbg(dwc->dev, "%s\n", __func__);
  1933. /*
  1934. * TODO take core out of low power mode when that's
  1935. * implemented.
  1936. */
  1937. dwc->gadget_driver->resume(&dwc->gadget);
  1938. }
  1939. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  1940. unsigned int evtinfo)
  1941. {
  1942. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  1943. unsigned int pwropt;
  1944. /*
  1945. * WORKAROUND: DWC3 < 2.50a have an issue when configured without
  1946. * Hibernation mode enabled which would show up when device detects
  1947. * host-initiated U3 exit.
  1948. *
  1949. * In that case, device will generate a Link State Change Interrupt
  1950. * from U3 to RESUME which is only necessary if Hibernation is
  1951. * configured in.
  1952. *
  1953. * There are no functional changes due to such spurious event and we
  1954. * just need to ignore it.
  1955. *
  1956. * Refers to:
  1957. *
  1958. * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
  1959. * operational mode
  1960. */
  1961. pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
  1962. if ((dwc->revision < DWC3_REVISION_250A) &&
  1963. (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
  1964. if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
  1965. (next == DWC3_LINK_STATE_RESUME)) {
  1966. dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n");
  1967. return;
  1968. }
  1969. }
  1970. /*
  1971. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  1972. * on the link partner, the USB session might do multiple entry/exit
  1973. * of low power states before a transfer takes place.
  1974. *
  1975. * Due to this problem, we might experience lower throughput. The
  1976. * suggested workaround is to disable DCTL[12:9] bits if we're
  1977. * transitioning from U1/U2 to U0 and enable those bits again
  1978. * after a transfer completes and there are no pending transfers
  1979. * on any of the enabled endpoints.
  1980. *
  1981. * This is the first half of that workaround.
  1982. *
  1983. * Refers to:
  1984. *
  1985. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  1986. * core send LGO_Ux entering U0
  1987. */
  1988. if (dwc->revision < DWC3_REVISION_183A) {
  1989. if (next == DWC3_LINK_STATE_U0) {
  1990. u32 u1u2;
  1991. u32 reg;
  1992. switch (dwc->link_state) {
  1993. case DWC3_LINK_STATE_U1:
  1994. case DWC3_LINK_STATE_U2:
  1995. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1996. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  1997. | DWC3_DCTL_ACCEPTU2ENA
  1998. | DWC3_DCTL_INITU1ENA
  1999. | DWC3_DCTL_ACCEPTU1ENA);
  2000. if (!dwc->u1u2)
  2001. dwc->u1u2 = reg & u1u2;
  2002. reg &= ~u1u2;
  2003. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2004. break;
  2005. default:
  2006. /* do nothing */
  2007. break;
  2008. }
  2009. }
  2010. }
  2011. dwc->link_state = next;
  2012. dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
  2013. }
  2014. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  2015. const struct dwc3_event_devt *event)
  2016. {
  2017. switch (event->type) {
  2018. case DWC3_DEVICE_EVENT_DISCONNECT:
  2019. dwc3_gadget_disconnect_interrupt(dwc);
  2020. break;
  2021. case DWC3_DEVICE_EVENT_RESET:
  2022. dwc3_gadget_reset_interrupt(dwc);
  2023. break;
  2024. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  2025. dwc3_gadget_conndone_interrupt(dwc);
  2026. break;
  2027. case DWC3_DEVICE_EVENT_WAKEUP:
  2028. dwc3_gadget_wakeup_interrupt(dwc);
  2029. break;
  2030. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  2031. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  2032. break;
  2033. case DWC3_DEVICE_EVENT_EOPF:
  2034. dev_vdbg(dwc->dev, "End of Periodic Frame\n");
  2035. break;
  2036. case DWC3_DEVICE_EVENT_SOF:
  2037. dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
  2038. break;
  2039. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  2040. dev_vdbg(dwc->dev, "Erratic Error\n");
  2041. break;
  2042. case DWC3_DEVICE_EVENT_CMD_CMPL:
  2043. dev_vdbg(dwc->dev, "Command Complete\n");
  2044. break;
  2045. case DWC3_DEVICE_EVENT_OVERFLOW:
  2046. dev_vdbg(dwc->dev, "Overflow\n");
  2047. break;
  2048. default:
  2049. dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  2050. }
  2051. }
  2052. static void dwc3_process_event_entry(struct dwc3 *dwc,
  2053. const union dwc3_event *event)
  2054. {
  2055. /* Endpoint IRQ, handle it and return early */
  2056. if (event->type.is_devspec == 0) {
  2057. /* depevt */
  2058. return dwc3_endpoint_interrupt(dwc, &event->depevt);
  2059. }
  2060. switch (event->type.type) {
  2061. case DWC3_EVENT_TYPE_DEV:
  2062. dwc3_gadget_interrupt(dwc, &event->devt);
  2063. break;
  2064. /* REVISIT what to do with Carkit and I2C events ? */
  2065. default:
  2066. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  2067. }
  2068. }
  2069. static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
  2070. {
  2071. struct dwc3 *dwc = _dwc;
  2072. unsigned long flags;
  2073. irqreturn_t ret = IRQ_NONE;
  2074. int i;
  2075. spin_lock_irqsave(&dwc->lock, flags);
  2076. for (i = 0; i < dwc->num_event_buffers; i++) {
  2077. struct dwc3_event_buffer *evt;
  2078. int left;
  2079. evt = dwc->ev_buffs[i];
  2080. left = evt->count;
  2081. if (!(evt->flags & DWC3_EVENT_PENDING))
  2082. continue;
  2083. while (left > 0) {
  2084. union dwc3_event event;
  2085. event.raw = *(u32 *) (evt->buf + evt->lpos);
  2086. dwc3_process_event_entry(dwc, &event);
  2087. /*
  2088. * FIXME we wrap around correctly to the next entry as
  2089. * almost all entries are 4 bytes in size. There is one
  2090. * entry which has 12 bytes which is a regular entry
  2091. * followed by 8 bytes data. ATM I don't know how
  2092. * things are organized if we get next to the a
  2093. * boundary so I worry about that once we try to handle
  2094. * that.
  2095. */
  2096. evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
  2097. left -= 4;
  2098. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(i), 4);
  2099. }
  2100. evt->count = 0;
  2101. evt->flags &= ~DWC3_EVENT_PENDING;
  2102. ret = IRQ_HANDLED;
  2103. }
  2104. spin_unlock_irqrestore(&dwc->lock, flags);
  2105. return ret;
  2106. }
  2107. static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
  2108. {
  2109. struct dwc3_event_buffer *evt;
  2110. u32 count;
  2111. evt = dwc->ev_buffs[buf];
  2112. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
  2113. count &= DWC3_GEVNTCOUNT_MASK;
  2114. if (!count)
  2115. return IRQ_NONE;
  2116. evt->count = count;
  2117. evt->flags |= DWC3_EVENT_PENDING;
  2118. return IRQ_WAKE_THREAD;
  2119. }
  2120. static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
  2121. {
  2122. struct dwc3 *dwc = _dwc;
  2123. int i;
  2124. irqreturn_t ret = IRQ_NONE;
  2125. spin_lock(&dwc->lock);
  2126. for (i = 0; i < dwc->num_event_buffers; i++) {
  2127. irqreturn_t status;
  2128. status = dwc3_process_event_buf(dwc, i);
  2129. if (status == IRQ_WAKE_THREAD)
  2130. ret = status;
  2131. }
  2132. spin_unlock(&dwc->lock);
  2133. return ret;
  2134. }
  2135. /**
  2136. * dwc3_gadget_init - Initializes gadget related registers
  2137. * @dwc: pointer to our controller context structure
  2138. *
  2139. * Returns 0 on success otherwise negative errno.
  2140. */
  2141. int dwc3_gadget_init(struct dwc3 *dwc)
  2142. {
  2143. u32 reg;
  2144. int ret;
  2145. dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2146. &dwc->ctrl_req_addr, GFP_KERNEL);
  2147. if (!dwc->ctrl_req) {
  2148. dev_err(dwc->dev, "failed to allocate ctrl request\n");
  2149. ret = -ENOMEM;
  2150. goto err0;
  2151. }
  2152. dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2153. &dwc->ep0_trb_addr, GFP_KERNEL);
  2154. if (!dwc->ep0_trb) {
  2155. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  2156. ret = -ENOMEM;
  2157. goto err1;
  2158. }
  2159. dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
  2160. if (!dwc->setup_buf) {
  2161. dev_err(dwc->dev, "failed to allocate setup buffer\n");
  2162. ret = -ENOMEM;
  2163. goto err2;
  2164. }
  2165. dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
  2166. DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
  2167. GFP_KERNEL);
  2168. if (!dwc->ep0_bounce) {
  2169. dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
  2170. ret = -ENOMEM;
  2171. goto err3;
  2172. }
  2173. dwc->gadget.ops = &dwc3_gadget_ops;
  2174. dwc->gadget.max_speed = USB_SPEED_SUPER;
  2175. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2176. dwc->gadget.sg_supported = true;
  2177. dwc->gadget.name = "dwc3-gadget";
  2178. /*
  2179. * REVISIT: Here we should clear all pending IRQs to be
  2180. * sure we're starting from a well known location.
  2181. */
  2182. ret = dwc3_gadget_init_endpoints(dwc);
  2183. if (ret)
  2184. goto err4;
  2185. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2186. reg |= DWC3_DCFG_LPM_CAP;
  2187. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2188. /* Enable USB2 LPM and automatic phy suspend only on recent versions */
  2189. if (dwc->revision >= DWC3_REVISION_194A) {
  2190. dwc3_gadget_usb2_phy_suspend(dwc, false);
  2191. dwc3_gadget_usb3_phy_suspend(dwc, false);
  2192. }
  2193. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  2194. if (ret) {
  2195. dev_err(dwc->dev, "failed to register udc\n");
  2196. goto err5;
  2197. }
  2198. return 0;
  2199. err5:
  2200. dwc3_gadget_free_endpoints(dwc);
  2201. err4:
  2202. dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
  2203. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2204. err3:
  2205. kfree(dwc->setup_buf);
  2206. err2:
  2207. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2208. dwc->ep0_trb, dwc->ep0_trb_addr);
  2209. err1:
  2210. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2211. dwc->ctrl_req, dwc->ctrl_req_addr);
  2212. err0:
  2213. return ret;
  2214. }
  2215. /* -------------------------------------------------------------------------- */
  2216. void dwc3_gadget_exit(struct dwc3 *dwc)
  2217. {
  2218. usb_del_gadget_udc(&dwc->gadget);
  2219. dwc3_gadget_free_endpoints(dwc);
  2220. dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
  2221. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2222. kfree(dwc->setup_buf);
  2223. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2224. dwc->ep0_trb, dwc->ep0_trb_addr);
  2225. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2226. dwc->ctrl_req, dwc->ctrl_req_addr);
  2227. }
  2228. int dwc3_gadget_prepare(struct dwc3 *dwc)
  2229. {
  2230. if (dwc->pullups_connected)
  2231. dwc3_gadget_disable_irq(dwc);
  2232. return 0;
  2233. }
  2234. void dwc3_gadget_complete(struct dwc3 *dwc)
  2235. {
  2236. if (dwc->pullups_connected) {
  2237. dwc3_gadget_enable_irq(dwc);
  2238. dwc3_gadget_run_stop(dwc, true);
  2239. }
  2240. }
  2241. int dwc3_gadget_suspend(struct dwc3 *dwc)
  2242. {
  2243. __dwc3_gadget_ep_disable(dwc->eps[0]);
  2244. __dwc3_gadget_ep_disable(dwc->eps[1]);
  2245. dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2246. return 0;
  2247. }
  2248. int dwc3_gadget_resume(struct dwc3 *dwc)
  2249. {
  2250. struct dwc3_ep *dep;
  2251. int ret;
  2252. /* Start with SuperSpeed Default */
  2253. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2254. dep = dwc->eps[0];
  2255. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  2256. if (ret)
  2257. goto err0;
  2258. dep = dwc->eps[1];
  2259. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
  2260. if (ret)
  2261. goto err1;
  2262. /* begin to receive SETUP packets */
  2263. dwc->ep0state = EP0_SETUP_PHASE;
  2264. dwc3_ep0_out_start(dwc);
  2265. dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
  2266. return 0;
  2267. err1:
  2268. __dwc3_gadget_ep_disable(dwc->eps[0]);
  2269. err0:
  2270. return ret;
  2271. }