gadget.c 65 KB

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