gadget.c 64 KB

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