gadget.c 64 KB

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