gadget.c 67 KB

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