gadget.c 63 KB

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