gadget.c 64 KB

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