gadget.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551
  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. if (dwc->ep0_bounced && dep->number == 0)
  228. dwc->ep0_bounced = false;
  229. else
  230. usb_gadget_unmap_request(&dwc->gadget, &req->request,
  231. req->direction);
  232. dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
  233. req, dep->name, req->request.actual,
  234. req->request.length, status);
  235. spin_unlock(&dwc->lock);
  236. req->request.complete(&dep->endpoint, &req->request);
  237. spin_lock(&dwc->lock);
  238. }
  239. static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
  240. {
  241. switch (cmd) {
  242. case DWC3_DEPCMD_DEPSTARTCFG:
  243. return "Start New Configuration";
  244. case DWC3_DEPCMD_ENDTRANSFER:
  245. return "End Transfer";
  246. case DWC3_DEPCMD_UPDATETRANSFER:
  247. return "Update Transfer";
  248. case DWC3_DEPCMD_STARTTRANSFER:
  249. return "Start Transfer";
  250. case DWC3_DEPCMD_CLEARSTALL:
  251. return "Clear Stall";
  252. case DWC3_DEPCMD_SETSTALL:
  253. return "Set Stall";
  254. case DWC3_DEPCMD_GETEPSTATE:
  255. return "Get Endpoint State";
  256. case DWC3_DEPCMD_SETTRANSFRESOURCE:
  257. return "Set Endpoint Transfer Resource";
  258. case DWC3_DEPCMD_SETEPCONFIG:
  259. return "Set Endpoint Configuration";
  260. default:
  261. return "UNKNOWN command";
  262. }
  263. }
  264. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
  265. {
  266. u32 timeout = 500;
  267. u32 reg;
  268. dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
  269. dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
  270. do {
  271. reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
  272. if (!(reg & DWC3_DGCMD_CMDACT)) {
  273. dev_vdbg(dwc->dev, "Command Complete --> %d\n",
  274. DWC3_DGCMD_STATUS(reg));
  275. return 0;
  276. }
  277. /*
  278. * We can't sleep here, because it's also called from
  279. * interrupt context.
  280. */
  281. timeout--;
  282. if (!timeout)
  283. return -ETIMEDOUT;
  284. udelay(1);
  285. } while (1);
  286. }
  287. int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
  288. unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
  289. {
  290. struct dwc3_ep *dep = dwc->eps[ep];
  291. u32 timeout = 500;
  292. u32 reg;
  293. dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
  294. dep->name,
  295. dwc3_gadget_ep_cmd_string(cmd), params->param0,
  296. params->param1, params->param2);
  297. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
  298. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
  299. dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
  300. dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
  301. do {
  302. reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
  303. if (!(reg & DWC3_DEPCMD_CMDACT)) {
  304. dev_vdbg(dwc->dev, "Command Complete --> %d\n",
  305. DWC3_DEPCMD_STATUS(reg));
  306. return 0;
  307. }
  308. /*
  309. * We can't sleep here, because it is also called from
  310. * interrupt context.
  311. */
  312. timeout--;
  313. if (!timeout)
  314. return -ETIMEDOUT;
  315. udelay(1);
  316. } while (1);
  317. }
  318. static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
  319. struct dwc3_trb *trb)
  320. {
  321. u32 offset = (char *) trb - (char *) dep->trb_pool;
  322. return dep->trb_pool_dma + offset;
  323. }
  324. static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
  325. {
  326. struct dwc3 *dwc = dep->dwc;
  327. if (dep->trb_pool)
  328. return 0;
  329. if (dep->number == 0 || dep->number == 1)
  330. return 0;
  331. dep->trb_pool = dma_alloc_coherent(dwc->dev,
  332. sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  333. &dep->trb_pool_dma, GFP_KERNEL);
  334. if (!dep->trb_pool) {
  335. dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
  336. dep->name);
  337. return -ENOMEM;
  338. }
  339. return 0;
  340. }
  341. static void dwc3_free_trb_pool(struct dwc3_ep *dep)
  342. {
  343. struct dwc3 *dwc = dep->dwc;
  344. dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  345. dep->trb_pool, dep->trb_pool_dma);
  346. dep->trb_pool = NULL;
  347. dep->trb_pool_dma = 0;
  348. }
  349. static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
  350. {
  351. struct dwc3_gadget_ep_cmd_params params;
  352. u32 cmd;
  353. memset(&params, 0x00, sizeof(params));
  354. if (dep->number != 1) {
  355. cmd = DWC3_DEPCMD_DEPSTARTCFG;
  356. /* XferRscIdx == 0 for ep0 and 2 for the remaining */
  357. if (dep->number > 1) {
  358. if (dwc->start_config_issued)
  359. return 0;
  360. dwc->start_config_issued = true;
  361. cmd |= DWC3_DEPCMD_PARAM(2);
  362. }
  363. return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
  364. }
  365. return 0;
  366. }
  367. static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
  368. const struct usb_endpoint_descriptor *desc,
  369. const struct usb_ss_ep_comp_descriptor *comp_desc,
  370. bool ignore)
  371. {
  372. struct dwc3_gadget_ep_cmd_params params;
  373. memset(&params, 0x00, sizeof(params));
  374. params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
  375. | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
  376. /* Burst size is only needed in SuperSpeed mode */
  377. if (dwc->gadget.speed == USB_SPEED_SUPER) {
  378. u32 burst = dep->endpoint.maxburst - 1;
  379. params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
  380. }
  381. if (ignore)
  382. params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
  383. params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
  384. | DWC3_DEPCFG_XFER_NOT_READY_EN;
  385. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  386. params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
  387. | DWC3_DEPCFG_STREAM_EVENT_EN;
  388. dep->stream_capable = true;
  389. }
  390. if (usb_endpoint_xfer_isoc(desc))
  391. params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
  392. /*
  393. * We are doing 1:1 mapping for endpoints, meaning
  394. * Physical Endpoints 2 maps to Logical Endpoint 2 and
  395. * so on. We consider the direction bit as part of the physical
  396. * endpoint number. So USB endpoint 0x81 is 0x03.
  397. */
  398. params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
  399. /*
  400. * We must use the lower 16 TX FIFOs even though
  401. * HW might have more
  402. */
  403. if (dep->direction)
  404. params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
  405. if (desc->bInterval) {
  406. params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
  407. dep->interval = 1 << (desc->bInterval - 1);
  408. }
  409. return dwc3_send_gadget_ep_cmd(dwc, dep->number,
  410. DWC3_DEPCMD_SETEPCONFIG, &params);
  411. }
  412. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
  413. {
  414. struct dwc3_gadget_ep_cmd_params params;
  415. memset(&params, 0x00, sizeof(params));
  416. params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
  417. return dwc3_send_gadget_ep_cmd(dwc, dep->number,
  418. DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
  419. }
  420. /**
  421. * __dwc3_gadget_ep_enable - Initializes a HW endpoint
  422. * @dep: endpoint to be initialized
  423. * @desc: USB Endpoint Descriptor
  424. *
  425. * Caller should take care of locking
  426. */
  427. static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
  428. const struct usb_endpoint_descriptor *desc,
  429. const struct usb_ss_ep_comp_descriptor *comp_desc,
  430. bool ignore)
  431. {
  432. struct dwc3 *dwc = dep->dwc;
  433. u32 reg;
  434. int ret = -ENOMEM;
  435. if (!(dep->flags & DWC3_EP_ENABLED)) {
  436. ret = dwc3_gadget_start_config(dwc, dep);
  437. if (ret)
  438. return ret;
  439. }
  440. ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
  441. if (ret)
  442. return ret;
  443. if (!(dep->flags & DWC3_EP_ENABLED)) {
  444. struct dwc3_trb *trb_st_hw;
  445. struct dwc3_trb *trb_link;
  446. ret = dwc3_gadget_set_xfer_resource(dwc, dep);
  447. if (ret)
  448. return ret;
  449. dep->endpoint.desc = desc;
  450. dep->comp_desc = comp_desc;
  451. dep->type = usb_endpoint_type(desc);
  452. dep->flags |= DWC3_EP_ENABLED;
  453. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  454. reg |= DWC3_DALEPENA_EP(dep->number);
  455. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  456. if (!usb_endpoint_xfer_isoc(desc))
  457. return 0;
  458. memset(&trb_link, 0, sizeof(trb_link));
  459. /* Link TRB for ISOC. The HWO bit is never reset */
  460. trb_st_hw = &dep->trb_pool[0];
  461. trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  462. trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  463. trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  464. trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
  465. trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
  466. }
  467. return 0;
  468. }
  469. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
  470. static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
  471. {
  472. struct dwc3_request *req;
  473. if (!list_empty(&dep->req_queued)) {
  474. dwc3_stop_active_transfer(dwc, dep->number);
  475. /* - giveback all requests to gadget driver */
  476. while (!list_empty(&dep->req_queued)) {
  477. req = next_request(&dep->req_queued);
  478. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  479. }
  480. }
  481. while (!list_empty(&dep->request_list)) {
  482. req = next_request(&dep->request_list);
  483. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  484. }
  485. }
  486. /**
  487. * __dwc3_gadget_ep_disable - Disables a HW endpoint
  488. * @dep: the endpoint to disable
  489. *
  490. * This function also removes requests which are currently processed ny the
  491. * hardware and those which are not yet scheduled.
  492. * Caller should take care of locking.
  493. */
  494. static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  495. {
  496. struct dwc3 *dwc = dep->dwc;
  497. u32 reg;
  498. dwc3_remove_requests(dwc, dep);
  499. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  500. reg &= ~DWC3_DALEPENA_EP(dep->number);
  501. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  502. dep->stream_capable = false;
  503. dep->endpoint.desc = NULL;
  504. dep->comp_desc = NULL;
  505. dep->type = 0;
  506. dep->flags = 0;
  507. return 0;
  508. }
  509. /* -------------------------------------------------------------------------- */
  510. static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
  511. const struct usb_endpoint_descriptor *desc)
  512. {
  513. return -EINVAL;
  514. }
  515. static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
  516. {
  517. return -EINVAL;
  518. }
  519. /* -------------------------------------------------------------------------- */
  520. static int dwc3_gadget_ep_enable(struct usb_ep *ep,
  521. const struct usb_endpoint_descriptor *desc)
  522. {
  523. struct dwc3_ep *dep;
  524. struct dwc3 *dwc;
  525. unsigned long flags;
  526. int ret;
  527. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  528. pr_debug("dwc3: invalid parameters\n");
  529. return -EINVAL;
  530. }
  531. if (!desc->wMaxPacketSize) {
  532. pr_debug("dwc3: missing wMaxPacketSize\n");
  533. return -EINVAL;
  534. }
  535. dep = to_dwc3_ep(ep);
  536. dwc = dep->dwc;
  537. if (dep->flags & DWC3_EP_ENABLED) {
  538. dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
  539. dep->name);
  540. return 0;
  541. }
  542. switch (usb_endpoint_type(desc)) {
  543. case USB_ENDPOINT_XFER_CONTROL:
  544. strlcat(dep->name, "-control", sizeof(dep->name));
  545. break;
  546. case USB_ENDPOINT_XFER_ISOC:
  547. strlcat(dep->name, "-isoc", sizeof(dep->name));
  548. break;
  549. case USB_ENDPOINT_XFER_BULK:
  550. strlcat(dep->name, "-bulk", sizeof(dep->name));
  551. break;
  552. case USB_ENDPOINT_XFER_INT:
  553. strlcat(dep->name, "-int", sizeof(dep->name));
  554. break;
  555. default:
  556. dev_err(dwc->dev, "invalid endpoint transfer type\n");
  557. }
  558. dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
  559. spin_lock_irqsave(&dwc->lock, flags);
  560. ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
  561. spin_unlock_irqrestore(&dwc->lock, flags);
  562. return ret;
  563. }
  564. static int dwc3_gadget_ep_disable(struct usb_ep *ep)
  565. {
  566. struct dwc3_ep *dep;
  567. struct dwc3 *dwc;
  568. unsigned long flags;
  569. int ret;
  570. if (!ep) {
  571. pr_debug("dwc3: invalid parameters\n");
  572. return -EINVAL;
  573. }
  574. dep = to_dwc3_ep(ep);
  575. dwc = dep->dwc;
  576. if (!(dep->flags & DWC3_EP_ENABLED)) {
  577. dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
  578. dep->name);
  579. return 0;
  580. }
  581. snprintf(dep->name, sizeof(dep->name), "ep%d%s",
  582. dep->number >> 1,
  583. (dep->number & 1) ? "in" : "out");
  584. spin_lock_irqsave(&dwc->lock, flags);
  585. ret = __dwc3_gadget_ep_disable(dep);
  586. spin_unlock_irqrestore(&dwc->lock, flags);
  587. return ret;
  588. }
  589. static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
  590. gfp_t gfp_flags)
  591. {
  592. struct dwc3_request *req;
  593. struct dwc3_ep *dep = to_dwc3_ep(ep);
  594. struct dwc3 *dwc = dep->dwc;
  595. req = kzalloc(sizeof(*req), gfp_flags);
  596. if (!req) {
  597. dev_err(dwc->dev, "not enough memory\n");
  598. return NULL;
  599. }
  600. req->epnum = dep->number;
  601. req->dep = dep;
  602. return &req->request;
  603. }
  604. static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
  605. struct usb_request *request)
  606. {
  607. struct dwc3_request *req = to_dwc3_request(request);
  608. kfree(req);
  609. }
  610. /**
  611. * dwc3_prepare_one_trb - setup one TRB from one request
  612. * @dep: endpoint for which this request is prepared
  613. * @req: dwc3_request pointer
  614. */
  615. static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
  616. struct dwc3_request *req, dma_addr_t dma,
  617. unsigned length, unsigned last, unsigned chain)
  618. {
  619. struct dwc3 *dwc = dep->dwc;
  620. struct dwc3_trb *trb;
  621. unsigned int cur_slot;
  622. dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
  623. dep->name, req, (unsigned long long) dma,
  624. length, last ? " last" : "",
  625. chain ? " chain" : "");
  626. trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
  627. cur_slot = dep->free_slot;
  628. dep->free_slot++;
  629. /* Skip the LINK-TRB on ISOC */
  630. if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
  631. usb_endpoint_xfer_isoc(dep->endpoint.desc))
  632. return;
  633. if (!req->trb) {
  634. dwc3_gadget_move_request_queued(req);
  635. req->trb = trb;
  636. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  637. }
  638. trb->size = DWC3_TRB_SIZE_LENGTH(length);
  639. trb->bpl = lower_32_bits(dma);
  640. trb->bph = upper_32_bits(dma);
  641. switch (usb_endpoint_type(dep->endpoint.desc)) {
  642. case USB_ENDPOINT_XFER_CONTROL:
  643. trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
  644. break;
  645. case USB_ENDPOINT_XFER_ISOC:
  646. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  647. if (!req->request.no_interrupt)
  648. trb->ctrl |= DWC3_TRB_CTRL_IOC;
  649. break;
  650. case USB_ENDPOINT_XFER_BULK:
  651. case USB_ENDPOINT_XFER_INT:
  652. trb->ctrl = DWC3_TRBCTL_NORMAL;
  653. break;
  654. default:
  655. /*
  656. * This is only possible with faulty memory because we
  657. * checked it already :)
  658. */
  659. BUG();
  660. }
  661. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  662. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  663. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  664. } else {
  665. if (chain)
  666. trb->ctrl |= DWC3_TRB_CTRL_CHN;
  667. if (last)
  668. trb->ctrl |= DWC3_TRB_CTRL_LST;
  669. }
  670. if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
  671. trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
  672. trb->ctrl |= DWC3_TRB_CTRL_HWO;
  673. }
  674. /*
  675. * dwc3_prepare_trbs - setup TRBs from requests
  676. * @dep: endpoint for which requests are being prepared
  677. * @starting: true if the endpoint is idle and no requests are queued.
  678. *
  679. * The function goes through the requests list and sets up TRBs for the
  680. * transfers. The function returns once there are no more TRBs available or
  681. * it runs out of requests.
  682. */
  683. static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
  684. {
  685. struct dwc3_request *req, *n;
  686. u32 trbs_left;
  687. u32 max;
  688. unsigned int last_one = 0;
  689. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  690. /* the first request must not be queued */
  691. trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
  692. /* Can't wrap around on a non-isoc EP since there's no link TRB */
  693. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  694. max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
  695. if (trbs_left > max)
  696. trbs_left = max;
  697. }
  698. /*
  699. * If busy & slot are equal than it is either full or empty. If we are
  700. * starting to process requests then we are empty. Otherwise we are
  701. * full and don't do anything
  702. */
  703. if (!trbs_left) {
  704. if (!starting)
  705. return;
  706. trbs_left = DWC3_TRB_NUM;
  707. /*
  708. * In case we start from scratch, we queue the ISOC requests
  709. * starting from slot 1. This is done because we use ring
  710. * buffer and have no LST bit to stop us. Instead, we place
  711. * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
  712. * after the first request so we start at slot 1 and have
  713. * 7 requests proceed before we hit the first IOC.
  714. * Other transfer types don't use the ring buffer and are
  715. * processed from the first TRB until the last one. Since we
  716. * don't wrap around we have to start at the beginning.
  717. */
  718. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  719. dep->busy_slot = 1;
  720. dep->free_slot = 1;
  721. } else {
  722. dep->busy_slot = 0;
  723. dep->free_slot = 0;
  724. }
  725. }
  726. /* The last TRB is a link TRB, not used for xfer */
  727. if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
  728. return;
  729. list_for_each_entry_safe(req, n, &dep->request_list, list) {
  730. unsigned length;
  731. dma_addr_t dma;
  732. if (req->request.num_mapped_sgs > 0) {
  733. struct usb_request *request = &req->request;
  734. struct scatterlist *sg = request->sg;
  735. struct scatterlist *s;
  736. int i;
  737. for_each_sg(sg, s, request->num_mapped_sgs, i) {
  738. unsigned chain = true;
  739. length = sg_dma_len(s);
  740. dma = sg_dma_address(s);
  741. if (i == (request->num_mapped_sgs - 1) ||
  742. sg_is_last(s)) {
  743. last_one = true;
  744. chain = false;
  745. }
  746. trbs_left--;
  747. if (!trbs_left)
  748. last_one = true;
  749. if (last_one)
  750. chain = false;
  751. dwc3_prepare_one_trb(dep, req, dma, length,
  752. last_one, chain);
  753. if (last_one)
  754. break;
  755. }
  756. } else {
  757. dma = req->request.dma;
  758. length = req->request.length;
  759. trbs_left--;
  760. if (!trbs_left)
  761. last_one = 1;
  762. /* Is this the last request? */
  763. if (list_is_last(&req->list, &dep->request_list))
  764. last_one = 1;
  765. dwc3_prepare_one_trb(dep, req, dma, length,
  766. last_one, false);
  767. if (last_one)
  768. break;
  769. }
  770. }
  771. }
  772. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
  773. int start_new)
  774. {
  775. struct dwc3_gadget_ep_cmd_params params;
  776. struct dwc3_request *req;
  777. struct dwc3 *dwc = dep->dwc;
  778. int ret;
  779. u32 cmd;
  780. if (start_new && (dep->flags & DWC3_EP_BUSY)) {
  781. dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
  782. return -EBUSY;
  783. }
  784. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  785. /*
  786. * If we are getting here after a short-out-packet we don't enqueue any
  787. * new requests as we try to set the IOC bit only on the last request.
  788. */
  789. if (start_new) {
  790. if (list_empty(&dep->req_queued))
  791. dwc3_prepare_trbs(dep, start_new);
  792. /* req points to the first request which will be sent */
  793. req = next_request(&dep->req_queued);
  794. } else {
  795. dwc3_prepare_trbs(dep, start_new);
  796. /*
  797. * req points to the first request where HWO changed from 0 to 1
  798. */
  799. req = next_request(&dep->req_queued);
  800. }
  801. if (!req) {
  802. dep->flags |= DWC3_EP_PENDING_REQUEST;
  803. return 0;
  804. }
  805. memset(&params, 0, sizeof(params));
  806. params.param0 = upper_32_bits(req->trb_dma);
  807. params.param1 = lower_32_bits(req->trb_dma);
  808. if (start_new)
  809. cmd = DWC3_DEPCMD_STARTTRANSFER;
  810. else
  811. cmd = DWC3_DEPCMD_UPDATETRANSFER;
  812. cmd |= DWC3_DEPCMD_PARAM(cmd_param);
  813. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  814. if (ret < 0) {
  815. dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
  816. /*
  817. * FIXME we need to iterate over the list of requests
  818. * here and stop, unmap, free and del each of the linked
  819. * requests instead of what we do now.
  820. */
  821. usb_gadget_unmap_request(&dwc->gadget, &req->request,
  822. req->direction);
  823. list_del(&req->list);
  824. return ret;
  825. }
  826. dep->flags |= DWC3_EP_BUSY;
  827. if (start_new) {
  828. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
  829. dep->number);
  830. WARN_ON_ONCE(!dep->resource_index);
  831. }
  832. return 0;
  833. }
  834. static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
  835. struct dwc3_ep *dep, u32 cur_uf)
  836. {
  837. u32 uf;
  838. if (list_empty(&dep->request_list)) {
  839. dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
  840. dep->name);
  841. dep->flags |= DWC3_EP_PENDING_REQUEST;
  842. return;
  843. }
  844. /* 4 micro frames in the future */
  845. uf = cur_uf + dep->interval * 4;
  846. __dwc3_gadget_kick_transfer(dep, uf, 1);
  847. }
  848. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  849. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  850. {
  851. u32 cur_uf, mask;
  852. mask = ~(dep->interval - 1);
  853. cur_uf = event->parameters & mask;
  854. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  855. }
  856. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  857. {
  858. struct dwc3 *dwc = dep->dwc;
  859. int ret;
  860. req->request.actual = 0;
  861. req->request.status = -EINPROGRESS;
  862. req->direction = dep->direction;
  863. req->epnum = dep->number;
  864. /*
  865. * We only add to our list of requests now and
  866. * start consuming the list once we get XferNotReady
  867. * IRQ.
  868. *
  869. * That way, we avoid doing anything that we don't need
  870. * to do now and defer it until the point we receive a
  871. * particular token from the Host side.
  872. *
  873. * This will also avoid Host cancelling URBs due to too
  874. * many NAKs.
  875. */
  876. ret = usb_gadget_map_request(&dwc->gadget, &req->request,
  877. dep->direction);
  878. if (ret)
  879. return ret;
  880. list_add_tail(&req->list, &dep->request_list);
  881. /*
  882. * There are a few special cases:
  883. *
  884. * 1. XferNotReady with empty list of requests. We need to kick the
  885. * transfer here in that situation, otherwise we will be NAKing
  886. * forever. If we get XferNotReady before gadget driver has a
  887. * chance to queue a request, we will ACK the IRQ but won't be
  888. * able to receive the data until the next request is queued.
  889. * The following code is handling exactly that.
  890. *
  891. */
  892. if (dep->flags & DWC3_EP_PENDING_REQUEST) {
  893. int ret;
  894. /*
  895. * If xfernotready is already elapsed and it is a case
  896. * of isoc transfer, then issue END TRANSFER, so that
  897. * you can receive xfernotready again and can have
  898. * notion of current microframe.
  899. */
  900. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  901. dwc3_stop_active_transfer(dwc, dep->number);
  902. return 0;
  903. }
  904. ret = __dwc3_gadget_kick_transfer(dep, 0, true);
  905. if (ret && ret != -EBUSY)
  906. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  907. dep->name);
  908. }
  909. /*
  910. * 2. XferInProgress on Isoc EP with an active transfer. We need to
  911. * kick the transfer here after queuing a request, otherwise the
  912. * core may not see the modified TRB(s).
  913. */
  914. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  915. (dep->flags & DWC3_EP_BUSY) &&
  916. !(dep->flags & DWC3_EP_MISSED_ISOC)) {
  917. WARN_ON_ONCE(!dep->resource_index);
  918. ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
  919. false);
  920. if (ret && ret != -EBUSY)
  921. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  922. dep->name);
  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, false);
  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, false);
  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 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. 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. /*
  1540. * NOTICE: We are violating what the Databook says about the
  1541. * EndTransfer command. Ideally we would _always_ wait for the
  1542. * EndTransfer Command Completion IRQ, but that's causing too
  1543. * much trouble synchronizing between us and gadget driver.
  1544. *
  1545. * We have discussed this with the IP Provider and it was
  1546. * suggested to giveback all requests here, but give HW some
  1547. * extra time to synchronize with the interconnect. We're using
  1548. * an arbitraty 100us delay for that.
  1549. *
  1550. * Note also that a similar handling was tested by Synopsys
  1551. * (thanks a lot Paul) and nothing bad has come out of it.
  1552. * In short, what we're doing is:
  1553. *
  1554. * - Issue EndTransfer WITH CMDIOC bit set
  1555. * - Wait 100us
  1556. */
  1557. cmd = DWC3_DEPCMD_ENDTRANSFER;
  1558. cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
  1559. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  1560. memset(&params, 0, sizeof(params));
  1561. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  1562. WARN_ON_ONCE(ret);
  1563. dep->resource_index = 0;
  1564. dep->flags &= ~DWC3_EP_BUSY;
  1565. udelay(100);
  1566. }
  1567. static void dwc3_stop_active_transfers(struct dwc3 *dwc)
  1568. {
  1569. u32 epnum;
  1570. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1571. struct dwc3_ep *dep;
  1572. dep = dwc->eps[epnum];
  1573. if (!(dep->flags & DWC3_EP_ENABLED))
  1574. continue;
  1575. dwc3_remove_requests(dwc, dep);
  1576. }
  1577. }
  1578. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  1579. {
  1580. u32 epnum;
  1581. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1582. struct dwc3_ep *dep;
  1583. struct dwc3_gadget_ep_cmd_params params;
  1584. int ret;
  1585. dep = dwc->eps[epnum];
  1586. if (!(dep->flags & DWC3_EP_STALL))
  1587. continue;
  1588. dep->flags &= ~DWC3_EP_STALL;
  1589. memset(&params, 0, sizeof(params));
  1590. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1591. DWC3_DEPCMD_CLEARSTALL, &params);
  1592. WARN_ON_ONCE(ret);
  1593. }
  1594. }
  1595. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  1596. {
  1597. int reg;
  1598. dev_vdbg(dwc->dev, "%s\n", __func__);
  1599. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1600. reg &= ~DWC3_DCTL_INITU1ENA;
  1601. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1602. reg &= ~DWC3_DCTL_INITU2ENA;
  1603. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1604. dwc3_disconnect_gadget(dwc);
  1605. dwc->start_config_issued = false;
  1606. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  1607. dwc->setup_packet_pending = false;
  1608. }
  1609. static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
  1610. {
  1611. u32 reg;
  1612. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  1613. if (suspend)
  1614. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  1615. else
  1616. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  1617. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  1618. }
  1619. static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
  1620. {
  1621. u32 reg;
  1622. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1623. if (suspend)
  1624. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  1625. else
  1626. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  1627. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1628. }
  1629. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  1630. {
  1631. u32 reg;
  1632. dev_vdbg(dwc->dev, "%s\n", __func__);
  1633. /*
  1634. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  1635. * would cause a missing Disconnect Event if there's a
  1636. * pending Setup Packet in the FIFO.
  1637. *
  1638. * There's no suggested workaround on the official Bug
  1639. * report, which states that "unless the driver/application
  1640. * is doing any special handling of a disconnect event,
  1641. * there is no functional issue".
  1642. *
  1643. * Unfortunately, it turns out that we _do_ some special
  1644. * handling of a disconnect event, namely complete all
  1645. * pending transfers, notify gadget driver of the
  1646. * disconnection, and so on.
  1647. *
  1648. * Our suggested workaround is to follow the Disconnect
  1649. * Event steps here, instead, based on a setup_packet_pending
  1650. * flag. Such flag gets set whenever we have a XferNotReady
  1651. * event on EP0 and gets cleared on XferComplete for the
  1652. * same endpoint.
  1653. *
  1654. * Refers to:
  1655. *
  1656. * STAR#9000466709: RTL: Device : Disconnect event not
  1657. * generated if setup packet pending in FIFO
  1658. */
  1659. if (dwc->revision < DWC3_REVISION_188A) {
  1660. if (dwc->setup_packet_pending)
  1661. dwc3_gadget_disconnect_interrupt(dwc);
  1662. }
  1663. /* after reset -> Default State */
  1664. dwc->dev_state = DWC3_DEFAULT_STATE;
  1665. /* Recent versions support automatic phy suspend and don't need this */
  1666. if (dwc->revision < DWC3_REVISION_194A) {
  1667. /* Resume PHYs */
  1668. dwc3_gadget_usb2_phy_suspend(dwc, false);
  1669. dwc3_gadget_usb3_phy_suspend(dwc, false);
  1670. }
  1671. if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
  1672. dwc3_disconnect_gadget(dwc);
  1673. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1674. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  1675. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1676. dwc->test_mode = false;
  1677. dwc3_stop_active_transfers(dwc);
  1678. dwc3_clear_stall_all_ep(dwc);
  1679. dwc->start_config_issued = false;
  1680. /* Reset device address to zero */
  1681. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1682. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  1683. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1684. }
  1685. static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
  1686. {
  1687. u32 reg;
  1688. u32 usb30_clock = DWC3_GCTL_CLK_BUS;
  1689. /*
  1690. * We change the clock only at SS but I dunno why I would want to do
  1691. * this. Maybe it becomes part of the power saving plan.
  1692. */
  1693. if (speed != DWC3_DSTS_SUPERSPEED)
  1694. return;
  1695. /*
  1696. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  1697. * each time on Connect Done.
  1698. */
  1699. if (!usb30_clock)
  1700. return;
  1701. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  1702. reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
  1703. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  1704. }
  1705. static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
  1706. {
  1707. switch (speed) {
  1708. case USB_SPEED_SUPER:
  1709. dwc3_gadget_usb2_phy_suspend(dwc, true);
  1710. break;
  1711. case USB_SPEED_HIGH:
  1712. case USB_SPEED_FULL:
  1713. case USB_SPEED_LOW:
  1714. dwc3_gadget_usb3_phy_suspend(dwc, true);
  1715. break;
  1716. }
  1717. }
  1718. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  1719. {
  1720. struct dwc3_gadget_ep_cmd_params params;
  1721. struct dwc3_ep *dep;
  1722. int ret;
  1723. u32 reg;
  1724. u8 speed;
  1725. dev_vdbg(dwc->dev, "%s\n", __func__);
  1726. memset(&params, 0x00, sizeof(params));
  1727. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1728. speed = reg & DWC3_DSTS_CONNECTSPD;
  1729. dwc->speed = speed;
  1730. dwc3_update_ram_clk_sel(dwc, speed);
  1731. switch (speed) {
  1732. case DWC3_DCFG_SUPERSPEED:
  1733. /*
  1734. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  1735. * would cause a missing USB3 Reset event.
  1736. *
  1737. * In such situations, we should force a USB3 Reset
  1738. * event by calling our dwc3_gadget_reset_interrupt()
  1739. * routine.
  1740. *
  1741. * Refers to:
  1742. *
  1743. * STAR#9000483510: RTL: SS : USB3 reset event may
  1744. * not be generated always when the link enters poll
  1745. */
  1746. if (dwc->revision < DWC3_REVISION_190A)
  1747. dwc3_gadget_reset_interrupt(dwc);
  1748. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1749. dwc->gadget.ep0->maxpacket = 512;
  1750. dwc->gadget.speed = USB_SPEED_SUPER;
  1751. break;
  1752. case DWC3_DCFG_HIGHSPEED:
  1753. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1754. dwc->gadget.ep0->maxpacket = 64;
  1755. dwc->gadget.speed = USB_SPEED_HIGH;
  1756. break;
  1757. case DWC3_DCFG_FULLSPEED2:
  1758. case DWC3_DCFG_FULLSPEED1:
  1759. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  1760. dwc->gadget.ep0->maxpacket = 64;
  1761. dwc->gadget.speed = USB_SPEED_FULL;
  1762. break;
  1763. case DWC3_DCFG_LOWSPEED:
  1764. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  1765. dwc->gadget.ep0->maxpacket = 8;
  1766. dwc->gadget.speed = USB_SPEED_LOW;
  1767. break;
  1768. }
  1769. /* Recent versions support automatic phy suspend and don't need this */
  1770. if (dwc->revision < DWC3_REVISION_194A) {
  1771. /* Suspend unneeded PHY */
  1772. dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
  1773. }
  1774. dep = dwc->eps[0];
  1775. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
  1776. if (ret) {
  1777. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1778. return;
  1779. }
  1780. dep = dwc->eps[1];
  1781. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
  1782. if (ret) {
  1783. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1784. return;
  1785. }
  1786. /*
  1787. * Configure PHY via GUSB3PIPECTLn if required.
  1788. *
  1789. * Update GTXFIFOSIZn
  1790. *
  1791. * In both cases reset values should be sufficient.
  1792. */
  1793. }
  1794. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  1795. {
  1796. dev_vdbg(dwc->dev, "%s\n", __func__);
  1797. /*
  1798. * TODO take core out of low power mode when that's
  1799. * implemented.
  1800. */
  1801. dwc->gadget_driver->resume(&dwc->gadget);
  1802. }
  1803. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  1804. unsigned int evtinfo)
  1805. {
  1806. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  1807. /*
  1808. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  1809. * on the link partner, the USB session might do multiple entry/exit
  1810. * of low power states before a transfer takes place.
  1811. *
  1812. * Due to this problem, we might experience lower throughput. The
  1813. * suggested workaround is to disable DCTL[12:9] bits if we're
  1814. * transitioning from U1/U2 to U0 and enable those bits again
  1815. * after a transfer completes and there are no pending transfers
  1816. * on any of the enabled endpoints.
  1817. *
  1818. * This is the first half of that workaround.
  1819. *
  1820. * Refers to:
  1821. *
  1822. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  1823. * core send LGO_Ux entering U0
  1824. */
  1825. if (dwc->revision < DWC3_REVISION_183A) {
  1826. if (next == DWC3_LINK_STATE_U0) {
  1827. u32 u1u2;
  1828. u32 reg;
  1829. switch (dwc->link_state) {
  1830. case DWC3_LINK_STATE_U1:
  1831. case DWC3_LINK_STATE_U2:
  1832. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1833. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  1834. | DWC3_DCTL_ACCEPTU2ENA
  1835. | DWC3_DCTL_INITU1ENA
  1836. | DWC3_DCTL_ACCEPTU1ENA);
  1837. if (!dwc->u1u2)
  1838. dwc->u1u2 = reg & u1u2;
  1839. reg &= ~u1u2;
  1840. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1841. break;
  1842. default:
  1843. /* do nothing */
  1844. break;
  1845. }
  1846. }
  1847. }
  1848. dwc->link_state = next;
  1849. dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
  1850. }
  1851. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  1852. const struct dwc3_event_devt *event)
  1853. {
  1854. switch (event->type) {
  1855. case DWC3_DEVICE_EVENT_DISCONNECT:
  1856. dwc3_gadget_disconnect_interrupt(dwc);
  1857. break;
  1858. case DWC3_DEVICE_EVENT_RESET:
  1859. dwc3_gadget_reset_interrupt(dwc);
  1860. break;
  1861. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  1862. dwc3_gadget_conndone_interrupt(dwc);
  1863. break;
  1864. case DWC3_DEVICE_EVENT_WAKEUP:
  1865. dwc3_gadget_wakeup_interrupt(dwc);
  1866. break;
  1867. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  1868. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  1869. break;
  1870. case DWC3_DEVICE_EVENT_EOPF:
  1871. dev_vdbg(dwc->dev, "End of Periodic Frame\n");
  1872. break;
  1873. case DWC3_DEVICE_EVENT_SOF:
  1874. dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
  1875. break;
  1876. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  1877. dev_vdbg(dwc->dev, "Erratic Error\n");
  1878. break;
  1879. case DWC3_DEVICE_EVENT_CMD_CMPL:
  1880. dev_vdbg(dwc->dev, "Command Complete\n");
  1881. break;
  1882. case DWC3_DEVICE_EVENT_OVERFLOW:
  1883. dev_vdbg(dwc->dev, "Overflow\n");
  1884. break;
  1885. default:
  1886. dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  1887. }
  1888. }
  1889. static void dwc3_process_event_entry(struct dwc3 *dwc,
  1890. const union dwc3_event *event)
  1891. {
  1892. /* Endpoint IRQ, handle it and return early */
  1893. if (event->type.is_devspec == 0) {
  1894. /* depevt */
  1895. return dwc3_endpoint_interrupt(dwc, &event->depevt);
  1896. }
  1897. switch (event->type.type) {
  1898. case DWC3_EVENT_TYPE_DEV:
  1899. dwc3_gadget_interrupt(dwc, &event->devt);
  1900. break;
  1901. /* REVISIT what to do with Carkit and I2C events ? */
  1902. default:
  1903. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  1904. }
  1905. }
  1906. static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
  1907. {
  1908. struct dwc3_event_buffer *evt;
  1909. int left;
  1910. u32 count;
  1911. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
  1912. count &= DWC3_GEVNTCOUNT_MASK;
  1913. if (!count)
  1914. return IRQ_NONE;
  1915. evt = dwc->ev_buffs[buf];
  1916. left = count;
  1917. while (left > 0) {
  1918. union dwc3_event event;
  1919. event.raw = *(u32 *) (evt->buf + evt->lpos);
  1920. dwc3_process_event_entry(dwc, &event);
  1921. /*
  1922. * XXX we wrap around correctly to the next entry as almost all
  1923. * entries are 4 bytes in size. There is one entry which has 12
  1924. * bytes which is a regular entry followed by 8 bytes data. ATM
  1925. * I don't know how things are organized if were get next to the
  1926. * a boundary so I worry about that once we try to handle that.
  1927. */
  1928. evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
  1929. left -= 4;
  1930. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
  1931. }
  1932. return IRQ_HANDLED;
  1933. }
  1934. static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
  1935. {
  1936. struct dwc3 *dwc = _dwc;
  1937. int i;
  1938. irqreturn_t ret = IRQ_NONE;
  1939. spin_lock(&dwc->lock);
  1940. for (i = 0; i < dwc->num_event_buffers; i++) {
  1941. irqreturn_t status;
  1942. status = dwc3_process_event_buf(dwc, i);
  1943. if (status == IRQ_HANDLED)
  1944. ret = status;
  1945. }
  1946. spin_unlock(&dwc->lock);
  1947. return ret;
  1948. }
  1949. /**
  1950. * dwc3_gadget_init - Initializes gadget related registers
  1951. * @dwc: pointer to our controller context structure
  1952. *
  1953. * Returns 0 on success otherwise negative errno.
  1954. */
  1955. int dwc3_gadget_init(struct dwc3 *dwc)
  1956. {
  1957. u32 reg;
  1958. int ret;
  1959. int irq;
  1960. dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  1961. &dwc->ctrl_req_addr, GFP_KERNEL);
  1962. if (!dwc->ctrl_req) {
  1963. dev_err(dwc->dev, "failed to allocate ctrl request\n");
  1964. ret = -ENOMEM;
  1965. goto err0;
  1966. }
  1967. dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  1968. &dwc->ep0_trb_addr, GFP_KERNEL);
  1969. if (!dwc->ep0_trb) {
  1970. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  1971. ret = -ENOMEM;
  1972. goto err1;
  1973. }
  1974. dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
  1975. if (!dwc->setup_buf) {
  1976. dev_err(dwc->dev, "failed to allocate setup buffer\n");
  1977. ret = -ENOMEM;
  1978. goto err2;
  1979. }
  1980. dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
  1981. DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
  1982. GFP_KERNEL);
  1983. if (!dwc->ep0_bounce) {
  1984. dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
  1985. ret = -ENOMEM;
  1986. goto err3;
  1987. }
  1988. dev_set_name(&dwc->gadget.dev, "gadget");
  1989. dwc->gadget.ops = &dwc3_gadget_ops;
  1990. dwc->gadget.max_speed = USB_SPEED_SUPER;
  1991. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  1992. dwc->gadget.dev.parent = dwc->dev;
  1993. dwc->gadget.sg_supported = true;
  1994. dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
  1995. dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
  1996. dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
  1997. dwc->gadget.dev.release = dwc3_gadget_release;
  1998. dwc->gadget.name = "dwc3-gadget";
  1999. /*
  2000. * REVISIT: Here we should clear all pending IRQs to be
  2001. * sure we're starting from a well known location.
  2002. */
  2003. ret = dwc3_gadget_init_endpoints(dwc);
  2004. if (ret)
  2005. goto err4;
  2006. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  2007. ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
  2008. "dwc3", dwc);
  2009. if (ret) {
  2010. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  2011. irq, ret);
  2012. goto err5;
  2013. }
  2014. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2015. reg |= DWC3_DCFG_LPM_CAP;
  2016. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2017. /* Enable all but Start and End of Frame IRQs */
  2018. reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
  2019. DWC3_DEVTEN_EVNTOVERFLOWEN |
  2020. DWC3_DEVTEN_CMDCMPLTEN |
  2021. DWC3_DEVTEN_ERRTICERREN |
  2022. DWC3_DEVTEN_WKUPEVTEN |
  2023. DWC3_DEVTEN_ULSTCNGEN |
  2024. DWC3_DEVTEN_CONNECTDONEEN |
  2025. DWC3_DEVTEN_USBRSTEN |
  2026. DWC3_DEVTEN_DISCONNEVTEN);
  2027. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  2028. /* Enable USB2 LPM and automatic phy suspend only on recent versions */
  2029. if (dwc->revision >= DWC3_REVISION_194A) {
  2030. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2031. reg |= DWC3_DCFG_LPM_CAP;
  2032. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2033. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2034. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  2035. /* TODO: This should be configurable */
  2036. reg |= DWC3_DCTL_HIRD_THRES(28);
  2037. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2038. dwc3_gadget_usb2_phy_suspend(dwc, false);
  2039. dwc3_gadget_usb3_phy_suspend(dwc, false);
  2040. }
  2041. ret = device_register(&dwc->gadget.dev);
  2042. if (ret) {
  2043. dev_err(dwc->dev, "failed to register gadget device\n");
  2044. put_device(&dwc->gadget.dev);
  2045. goto err6;
  2046. }
  2047. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  2048. if (ret) {
  2049. dev_err(dwc->dev, "failed to register udc\n");
  2050. goto err7;
  2051. }
  2052. return 0;
  2053. err7:
  2054. device_unregister(&dwc->gadget.dev);
  2055. err6:
  2056. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  2057. free_irq(irq, dwc);
  2058. err5:
  2059. dwc3_gadget_free_endpoints(dwc);
  2060. err4:
  2061. dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
  2062. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2063. err3:
  2064. kfree(dwc->setup_buf);
  2065. err2:
  2066. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2067. dwc->ep0_trb, dwc->ep0_trb_addr);
  2068. err1:
  2069. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2070. dwc->ctrl_req, dwc->ctrl_req_addr);
  2071. err0:
  2072. return ret;
  2073. }
  2074. void dwc3_gadget_exit(struct dwc3 *dwc)
  2075. {
  2076. int irq;
  2077. usb_del_gadget_udc(&dwc->gadget);
  2078. irq = platform_get_irq(to_platform_device(dwc->dev), 0);
  2079. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  2080. free_irq(irq, dwc);
  2081. dwc3_gadget_free_endpoints(dwc);
  2082. dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
  2083. dwc->ep0_bounce, dwc->ep0_bounce_addr);
  2084. kfree(dwc->setup_buf);
  2085. dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
  2086. dwc->ep0_trb, dwc->ep0_trb_addr);
  2087. dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
  2088. dwc->ctrl_req, dwc->ctrl_req_addr);
  2089. device_unregister(&dwc->gadget.dev);
  2090. }