gadget.c 61 KB

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