gadget.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527
  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);
  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->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
  837. dep->number);
  838. WARN_ON_ONCE(!dep->res_trans_idx);
  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. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  889. if (dep->flags & DWC3_EP_BUSY) {
  890. dep->flags |= DWC3_EP_PENDING_REQUEST;
  891. } else if (dep->flags & DWC3_EP_MISSED_ISOC) {
  892. __dwc3_gadget_start_isoc(dwc, dep, dep->current_uf);
  893. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  894. }
  895. }
  896. /*
  897. * There are two special cases:
  898. *
  899. * 1. XferNotReady with empty list of requests. We need to kick the
  900. * transfer here in that situation, otherwise we will be NAKing
  901. * forever. If we get XferNotReady before gadget driver has a
  902. * chance to queue a request, we will ACK the IRQ but won't be
  903. * able to receive the data until the next request is queued.
  904. * The following code is handling exactly that.
  905. *
  906. * 2. XferInProgress on Isoc EP with an active transfer. We need to
  907. * kick the transfer here after queuing a request, otherwise the
  908. * core may not see the modified TRB(s).
  909. */
  910. if (dep->flags & DWC3_EP_PENDING_REQUEST) {
  911. int ret;
  912. int start_trans = 1;
  913. u8 trans_idx = dep->res_trans_idx;
  914. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  915. (dep->flags & DWC3_EP_BUSY)) {
  916. start_trans = 0;
  917. WARN_ON_ONCE(!trans_idx);
  918. } else {
  919. trans_idx = 0;
  920. }
  921. ret = __dwc3_gadget_kick_transfer(dep, trans_idx, start_trans);
  922. if (ret && ret != -EBUSY) {
  923. struct dwc3 *dwc = dep->dwc;
  924. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  925. dep->name);
  926. }
  927. }
  928. return 0;
  929. }
  930. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  931. gfp_t gfp_flags)
  932. {
  933. struct dwc3_request *req = to_dwc3_request(request);
  934. struct dwc3_ep *dep = to_dwc3_ep(ep);
  935. struct dwc3 *dwc = dep->dwc;
  936. unsigned long flags;
  937. int ret;
  938. if (!dep->endpoint.desc) {
  939. dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
  940. request, ep->name);
  941. return -ESHUTDOWN;
  942. }
  943. dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
  944. request, ep->name, request->length);
  945. spin_lock_irqsave(&dwc->lock, flags);
  946. ret = __dwc3_gadget_ep_queue(dep, req);
  947. spin_unlock_irqrestore(&dwc->lock, flags);
  948. return ret;
  949. }
  950. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  951. struct usb_request *request)
  952. {
  953. struct dwc3_request *req = to_dwc3_request(request);
  954. struct dwc3_request *r = NULL;
  955. struct dwc3_ep *dep = to_dwc3_ep(ep);
  956. struct dwc3 *dwc = dep->dwc;
  957. unsigned long flags;
  958. int ret = 0;
  959. spin_lock_irqsave(&dwc->lock, flags);
  960. list_for_each_entry(r, &dep->request_list, list) {
  961. if (r == req)
  962. break;
  963. }
  964. if (r != req) {
  965. list_for_each_entry(r, &dep->req_queued, list) {
  966. if (r == req)
  967. break;
  968. }
  969. if (r == req) {
  970. /* wait until it is processed */
  971. dwc3_stop_active_transfer(dwc, dep->number);
  972. goto out0;
  973. }
  974. dev_err(dwc->dev, "request %p was not queued to %s\n",
  975. request, ep->name);
  976. ret = -EINVAL;
  977. goto out0;
  978. }
  979. /* giveback the request */
  980. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  981. out0:
  982. spin_unlock_irqrestore(&dwc->lock, flags);
  983. return ret;
  984. }
  985. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
  986. {
  987. struct dwc3_gadget_ep_cmd_params params;
  988. struct dwc3 *dwc = dep->dwc;
  989. int ret;
  990. memset(&params, 0x00, sizeof(params));
  991. if (value) {
  992. if (dep->number == 0 || dep->number == 1) {
  993. /*
  994. * Whenever EP0 is stalled, we will restart
  995. * the state machine, thus moving back to
  996. * Setup Phase
  997. */
  998. dwc->ep0state = EP0_SETUP_PHASE;
  999. }
  1000. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1001. DWC3_DEPCMD_SETSTALL, &params);
  1002. if (ret)
  1003. dev_err(dwc->dev, "failed to %s STALL on %s\n",
  1004. value ? "set" : "clear",
  1005. dep->name);
  1006. else
  1007. dep->flags |= DWC3_EP_STALL;
  1008. } else {
  1009. if (dep->flags & DWC3_EP_WEDGE)
  1010. return 0;
  1011. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1012. DWC3_DEPCMD_CLEARSTALL, &params);
  1013. if (ret)
  1014. dev_err(dwc->dev, "failed to %s STALL on %s\n",
  1015. value ? "set" : "clear",
  1016. dep->name);
  1017. else
  1018. dep->flags &= ~DWC3_EP_STALL;
  1019. }
  1020. return ret;
  1021. }
  1022. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1023. {
  1024. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1025. struct dwc3 *dwc = dep->dwc;
  1026. unsigned long flags;
  1027. int ret;
  1028. spin_lock_irqsave(&dwc->lock, flags);
  1029. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1030. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1031. ret = -EINVAL;
  1032. goto out;
  1033. }
  1034. ret = __dwc3_gadget_ep_set_halt(dep, value);
  1035. out:
  1036. spin_unlock_irqrestore(&dwc->lock, flags);
  1037. return ret;
  1038. }
  1039. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1040. {
  1041. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1042. struct dwc3 *dwc = dep->dwc;
  1043. unsigned long flags;
  1044. spin_lock_irqsave(&dwc->lock, flags);
  1045. dep->flags |= DWC3_EP_WEDGE;
  1046. spin_unlock_irqrestore(&dwc->lock, flags);
  1047. return dwc3_gadget_ep_set_halt(ep, 1);
  1048. }
  1049. /* -------------------------------------------------------------------------- */
  1050. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1051. .bLength = USB_DT_ENDPOINT_SIZE,
  1052. .bDescriptorType = USB_DT_ENDPOINT,
  1053. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1054. };
  1055. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1056. .enable = dwc3_gadget_ep0_enable,
  1057. .disable = dwc3_gadget_ep0_disable,
  1058. .alloc_request = dwc3_gadget_ep_alloc_request,
  1059. .free_request = dwc3_gadget_ep_free_request,
  1060. .queue = dwc3_gadget_ep0_queue,
  1061. .dequeue = dwc3_gadget_ep_dequeue,
  1062. .set_halt = dwc3_gadget_ep_set_halt,
  1063. .set_wedge = dwc3_gadget_ep_set_wedge,
  1064. };
  1065. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1066. .enable = dwc3_gadget_ep_enable,
  1067. .disable = dwc3_gadget_ep_disable,
  1068. .alloc_request = dwc3_gadget_ep_alloc_request,
  1069. .free_request = dwc3_gadget_ep_free_request,
  1070. .queue = dwc3_gadget_ep_queue,
  1071. .dequeue = dwc3_gadget_ep_dequeue,
  1072. .set_halt = dwc3_gadget_ep_set_halt,
  1073. .set_wedge = dwc3_gadget_ep_set_wedge,
  1074. };
  1075. /* -------------------------------------------------------------------------- */
  1076. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1077. {
  1078. struct dwc3 *dwc = gadget_to_dwc(g);
  1079. u32 reg;
  1080. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1081. return DWC3_DSTS_SOFFN(reg);
  1082. }
  1083. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1084. {
  1085. struct dwc3 *dwc = gadget_to_dwc(g);
  1086. unsigned long timeout;
  1087. unsigned long flags;
  1088. u32 reg;
  1089. int ret = 0;
  1090. u8 link_state;
  1091. u8 speed;
  1092. spin_lock_irqsave(&dwc->lock, flags);
  1093. /*
  1094. * According to the Databook Remote wakeup request should
  1095. * be issued only when the device is in early suspend state.
  1096. *
  1097. * We can check that via USB Link State bits in DSTS register.
  1098. */
  1099. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1100. speed = reg & DWC3_DSTS_CONNECTSPD;
  1101. if (speed == DWC3_DSTS_SUPERSPEED) {
  1102. dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
  1103. ret = -EINVAL;
  1104. goto out;
  1105. }
  1106. link_state = DWC3_DSTS_USBLNKST(reg);
  1107. switch (link_state) {
  1108. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1109. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1110. break;
  1111. default:
  1112. dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
  1113. link_state);
  1114. ret = -EINVAL;
  1115. goto out;
  1116. }
  1117. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1118. if (ret < 0) {
  1119. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1120. goto out;
  1121. }
  1122. /* Recent versions do this automatically */
  1123. if (dwc->revision < DWC3_REVISION_194A) {
  1124. /* write zeroes to Link Change Request */
  1125. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1126. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1127. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1128. }
  1129. /* poll until Link State changes to ON */
  1130. timeout = jiffies + msecs_to_jiffies(100);
  1131. while (!time_after(jiffies, timeout)) {
  1132. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1133. /* in HS, means ON */
  1134. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1135. break;
  1136. }
  1137. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1138. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1139. ret = -EINVAL;
  1140. }
  1141. out:
  1142. spin_unlock_irqrestore(&dwc->lock, flags);
  1143. return ret;
  1144. }
  1145. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  1146. int is_selfpowered)
  1147. {
  1148. struct dwc3 *dwc = gadget_to_dwc(g);
  1149. unsigned long flags;
  1150. spin_lock_irqsave(&dwc->lock, flags);
  1151. dwc->is_selfpowered = !!is_selfpowered;
  1152. spin_unlock_irqrestore(&dwc->lock, flags);
  1153. return 0;
  1154. }
  1155. static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
  1156. {
  1157. u32 reg;
  1158. u32 timeout = 500;
  1159. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1160. if (is_on) {
  1161. if (dwc->revision <= DWC3_REVISION_187A) {
  1162. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  1163. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  1164. }
  1165. if (dwc->revision >= DWC3_REVISION_194A)
  1166. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1167. reg |= DWC3_DCTL_RUN_STOP;
  1168. } else {
  1169. reg &= ~DWC3_DCTL_RUN_STOP;
  1170. }
  1171. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1172. do {
  1173. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1174. if (is_on) {
  1175. if (!(reg & DWC3_DSTS_DEVCTRLHLT))
  1176. break;
  1177. } else {
  1178. if (reg & DWC3_DSTS_DEVCTRLHLT)
  1179. break;
  1180. }
  1181. timeout--;
  1182. if (!timeout)
  1183. break;
  1184. udelay(1);
  1185. } while (1);
  1186. dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
  1187. dwc->gadget_driver
  1188. ? dwc->gadget_driver->function : "no-function",
  1189. is_on ? "connect" : "disconnect");
  1190. }
  1191. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  1192. {
  1193. struct dwc3 *dwc = gadget_to_dwc(g);
  1194. unsigned long flags;
  1195. is_on = !!is_on;
  1196. spin_lock_irqsave(&dwc->lock, flags);
  1197. dwc3_gadget_run_stop(dwc, is_on);
  1198. spin_unlock_irqrestore(&dwc->lock, flags);
  1199. return 0;
  1200. }
  1201. static int dwc3_gadget_start(struct usb_gadget *g,
  1202. struct usb_gadget_driver *driver)
  1203. {
  1204. struct dwc3 *dwc = gadget_to_dwc(g);
  1205. struct dwc3_ep *dep;
  1206. unsigned long flags;
  1207. int ret = 0;
  1208. u32 reg;
  1209. spin_lock_irqsave(&dwc->lock, flags);
  1210. if (dwc->gadget_driver) {
  1211. dev_err(dwc->dev, "%s is already bound to %s\n",
  1212. dwc->gadget.name,
  1213. dwc->gadget_driver->driver.name);
  1214. ret = -EBUSY;
  1215. goto err0;
  1216. }
  1217. dwc->gadget_driver = driver;
  1218. dwc->gadget.dev.driver = &driver->driver;
  1219. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1220. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1221. /**
  1222. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1223. * which would cause metastability state on Run/Stop
  1224. * bit if we try to force the IP to USB2-only mode.
  1225. *
  1226. * Because of that, we cannot configure the IP to any
  1227. * speed other than the SuperSpeed
  1228. *
  1229. * Refers to:
  1230. *
  1231. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1232. * USB 2.0 Mode
  1233. */
  1234. if (dwc->revision < DWC3_REVISION_220A)
  1235. reg |= DWC3_DCFG_SUPERSPEED;
  1236. else
  1237. reg |= dwc->maximum_speed;
  1238. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1239. dwc->start_config_issued = false;
  1240. /* Start with SuperSpeed Default */
  1241. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1242. dep = dwc->eps[0];
  1243. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
  1244. if (ret) {
  1245. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1246. goto err0;
  1247. }
  1248. dep = dwc->eps[1];
  1249. ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
  1250. if (ret) {
  1251. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1252. goto err1;
  1253. }
  1254. /* begin to receive SETUP packets */
  1255. dwc->ep0state = EP0_SETUP_PHASE;
  1256. dwc3_ep0_out_start(dwc);
  1257. spin_unlock_irqrestore(&dwc->lock, flags);
  1258. return 0;
  1259. err1:
  1260. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1261. err0:
  1262. spin_unlock_irqrestore(&dwc->lock, flags);
  1263. return ret;
  1264. }
  1265. static int dwc3_gadget_stop(struct usb_gadget *g,
  1266. struct usb_gadget_driver *driver)
  1267. {
  1268. struct dwc3 *dwc = gadget_to_dwc(g);
  1269. unsigned long flags;
  1270. spin_lock_irqsave(&dwc->lock, flags);
  1271. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1272. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1273. dwc->gadget_driver = NULL;
  1274. dwc->gadget.dev.driver = NULL;
  1275. spin_unlock_irqrestore(&dwc->lock, flags);
  1276. return 0;
  1277. }
  1278. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1279. .get_frame = dwc3_gadget_get_frame,
  1280. .wakeup = dwc3_gadget_wakeup,
  1281. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1282. .pullup = dwc3_gadget_pullup,
  1283. .udc_start = dwc3_gadget_start,
  1284. .udc_stop = dwc3_gadget_stop,
  1285. };
  1286. /* -------------------------------------------------------------------------- */
  1287. static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
  1288. {
  1289. struct dwc3_ep *dep;
  1290. u8 epnum;
  1291. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1292. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1293. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1294. if (!dep) {
  1295. dev_err(dwc->dev, "can't allocate endpoint %d\n",
  1296. epnum);
  1297. return -ENOMEM;
  1298. }
  1299. dep->dwc = dwc;
  1300. dep->number = epnum;
  1301. dwc->eps[epnum] = dep;
  1302. snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
  1303. (epnum & 1) ? "in" : "out");
  1304. dep->endpoint.name = dep->name;
  1305. dep->direction = (epnum & 1);
  1306. if (epnum == 0 || epnum == 1) {
  1307. dep->endpoint.maxpacket = 512;
  1308. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1309. if (!epnum)
  1310. dwc->gadget.ep0 = &dep->endpoint;
  1311. } else {
  1312. int ret;
  1313. dep->endpoint.maxpacket = 1024;
  1314. dep->endpoint.max_streams = 15;
  1315. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1316. list_add_tail(&dep->endpoint.ep_list,
  1317. &dwc->gadget.ep_list);
  1318. ret = dwc3_alloc_trb_pool(dep);
  1319. if (ret)
  1320. return ret;
  1321. }
  1322. INIT_LIST_HEAD(&dep->request_list);
  1323. INIT_LIST_HEAD(&dep->req_queued);
  1324. }
  1325. return 0;
  1326. }
  1327. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1328. {
  1329. struct dwc3_ep *dep;
  1330. u8 epnum;
  1331. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1332. dep = dwc->eps[epnum];
  1333. dwc3_free_trb_pool(dep);
  1334. if (epnum != 0 && epnum != 1)
  1335. list_del(&dep->endpoint.ep_list);
  1336. kfree(dep);
  1337. }
  1338. }
  1339. static void dwc3_gadget_release(struct device *dev)
  1340. {
  1341. dev_dbg(dev, "%s\n", __func__);
  1342. }
  1343. /* -------------------------------------------------------------------------- */
  1344. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1345. const struct dwc3_event_depevt *event, int status)
  1346. {
  1347. struct dwc3_request *req;
  1348. struct dwc3_trb *trb;
  1349. unsigned int count;
  1350. unsigned int s_pkt = 0;
  1351. unsigned int trb_status;
  1352. do {
  1353. req = next_request(&dep->req_queued);
  1354. if (!req) {
  1355. WARN_ON_ONCE(1);
  1356. return 1;
  1357. }
  1358. trb = req->trb;
  1359. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1360. /*
  1361. * We continue despite the error. There is not much we
  1362. * can do. If we don't clean it up we loop forever. If
  1363. * we skip the TRB then it gets overwritten after a
  1364. * while since we use them in a ring buffer. A BUG()
  1365. * would help. Lets hope that if this occurs, someone
  1366. * fixes the root cause instead of looking away :)
  1367. */
  1368. dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
  1369. dep->name, req->trb);
  1370. count = trb->size & DWC3_TRB_SIZE_MASK;
  1371. if (dep->direction) {
  1372. if (count) {
  1373. trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  1374. if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
  1375. dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
  1376. dep->name);
  1377. dep->current_uf = event->parameters &
  1378. ~(dep->interval - 1);
  1379. dep->flags |= DWC3_EP_MISSED_ISOC;
  1380. } else {
  1381. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1382. dep->name);
  1383. status = -ECONNRESET;
  1384. }
  1385. }
  1386. } else {
  1387. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1388. s_pkt = 1;
  1389. }
  1390. /*
  1391. * We assume here we will always receive the entire data block
  1392. * which we should receive. Meaning, if we program RX to
  1393. * receive 4K but we receive only 2K, we assume that's all we
  1394. * should receive and we simply bounce the request back to the
  1395. * gadget driver for further processing.
  1396. */
  1397. req->request.actual += req->request.length - count;
  1398. dwc3_gadget_giveback(dep, req, status);
  1399. if (s_pkt)
  1400. break;
  1401. if ((event->status & DEPEVT_STATUS_LST) &&
  1402. (trb->ctrl & (DWC3_TRB_CTRL_LST |
  1403. DWC3_TRB_CTRL_HWO)))
  1404. break;
  1405. if ((event->status & DEPEVT_STATUS_IOC) &&
  1406. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1407. break;
  1408. } while (1);
  1409. if ((event->status & DEPEVT_STATUS_IOC) &&
  1410. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1411. return 0;
  1412. return 1;
  1413. }
  1414. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1415. struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
  1416. int start_new)
  1417. {
  1418. unsigned status = 0;
  1419. int clean_busy;
  1420. if (event->status & DEPEVT_STATUS_BUSERR)
  1421. status = -ECONNRESET;
  1422. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  1423. if (clean_busy)
  1424. dep->flags &= ~DWC3_EP_BUSY;
  1425. /*
  1426. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  1427. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  1428. */
  1429. if (dwc->revision < DWC3_REVISION_183A) {
  1430. u32 reg;
  1431. int i;
  1432. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  1433. struct dwc3_ep *dep = dwc->eps[i];
  1434. if (!(dep->flags & DWC3_EP_ENABLED))
  1435. continue;
  1436. if (!list_empty(&dep->req_queued))
  1437. return;
  1438. }
  1439. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1440. reg |= dwc->u1u2;
  1441. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1442. dwc->u1u2 = 0;
  1443. }
  1444. }
  1445. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  1446. const struct dwc3_event_depevt *event)
  1447. {
  1448. struct dwc3_ep *dep;
  1449. u8 epnum = event->endpoint_number;
  1450. dep = dwc->eps[epnum];
  1451. if (!(dep->flags & DWC3_EP_ENABLED))
  1452. return;
  1453. dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
  1454. dwc3_ep_event_string(event->endpoint_event));
  1455. if (epnum == 0 || epnum == 1) {
  1456. dwc3_ep0_interrupt(dwc, event);
  1457. return;
  1458. }
  1459. switch (event->endpoint_event) {
  1460. case DWC3_DEPEVT_XFERCOMPLETE:
  1461. dep->res_trans_idx = 0;
  1462. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1463. dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
  1464. dep->name);
  1465. return;
  1466. }
  1467. dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
  1468. break;
  1469. case DWC3_DEPEVT_XFERINPROGRESS:
  1470. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1471. dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
  1472. dep->name);
  1473. return;
  1474. }
  1475. dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
  1476. break;
  1477. case DWC3_DEPEVT_XFERNOTREADY:
  1478. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1479. dwc3_gadget_start_isoc(dwc, dep, event);
  1480. } else {
  1481. int ret;
  1482. dev_vdbg(dwc->dev, "%s: reason %s\n",
  1483. dep->name, event->status &
  1484. DEPEVT_STATUS_TRANSFER_ACTIVE
  1485. ? "Transfer Active"
  1486. : "Transfer Not Active");
  1487. ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
  1488. if (!ret || ret == -EBUSY)
  1489. return;
  1490. dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
  1491. dep->name);
  1492. }
  1493. break;
  1494. case DWC3_DEPEVT_STREAMEVT:
  1495. if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
  1496. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  1497. dep->name);
  1498. return;
  1499. }
  1500. switch (event->status) {
  1501. case DEPEVT_STREAMEVT_FOUND:
  1502. dev_vdbg(dwc->dev, "Stream %d found and started\n",
  1503. event->parameters);
  1504. break;
  1505. case DEPEVT_STREAMEVT_NOTFOUND:
  1506. /* FALLTHROUGH */
  1507. default:
  1508. dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
  1509. }
  1510. break;
  1511. case DWC3_DEPEVT_RXTXFIFOEVT:
  1512. dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
  1513. break;
  1514. case DWC3_DEPEVT_EPCMDCMPLT:
  1515. dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
  1516. break;
  1517. }
  1518. }
  1519. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  1520. {
  1521. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  1522. spin_unlock(&dwc->lock);
  1523. dwc->gadget_driver->disconnect(&dwc->gadget);
  1524. spin_lock(&dwc->lock);
  1525. }
  1526. }
  1527. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
  1528. {
  1529. struct dwc3_ep *dep;
  1530. struct dwc3_gadget_ep_cmd_params params;
  1531. u32 cmd;
  1532. int ret;
  1533. dep = dwc->eps[epnum];
  1534. WARN_ON(!dep->res_trans_idx);
  1535. if (dep->res_trans_idx) {
  1536. cmd = DWC3_DEPCMD_ENDTRANSFER;
  1537. cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
  1538. cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
  1539. memset(&params, 0, sizeof(params));
  1540. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
  1541. WARN_ON_ONCE(ret);
  1542. dep->res_trans_idx = 0;
  1543. }
  1544. }
  1545. static void dwc3_stop_active_transfers(struct dwc3 *dwc)
  1546. {
  1547. u32 epnum;
  1548. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1549. struct dwc3_ep *dep;
  1550. dep = dwc->eps[epnum];
  1551. if (!(dep->flags & DWC3_EP_ENABLED))
  1552. continue;
  1553. dwc3_remove_requests(dwc, dep);
  1554. }
  1555. }
  1556. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  1557. {
  1558. u32 epnum;
  1559. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1560. struct dwc3_ep *dep;
  1561. struct dwc3_gadget_ep_cmd_params params;
  1562. int ret;
  1563. dep = dwc->eps[epnum];
  1564. if (!(dep->flags & DWC3_EP_STALL))
  1565. continue;
  1566. dep->flags &= ~DWC3_EP_STALL;
  1567. memset(&params, 0, sizeof(params));
  1568. ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
  1569. DWC3_DEPCMD_CLEARSTALL, &params);
  1570. WARN_ON_ONCE(ret);
  1571. }
  1572. }
  1573. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  1574. {
  1575. int reg;
  1576. dev_vdbg(dwc->dev, "%s\n", __func__);
  1577. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1578. reg &= ~DWC3_DCTL_INITU1ENA;
  1579. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1580. reg &= ~DWC3_DCTL_INITU2ENA;
  1581. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1582. dwc3_disconnect_gadget(dwc);
  1583. dwc->start_config_issued = false;
  1584. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  1585. dwc->setup_packet_pending = false;
  1586. }
  1587. static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
  1588. {
  1589. u32 reg;
  1590. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  1591. if (suspend)
  1592. reg |= DWC3_GUSB3PIPECTL_SUSPHY;
  1593. else
  1594. reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
  1595. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  1596. }
  1597. static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
  1598. {
  1599. u32 reg;
  1600. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  1601. if (suspend)
  1602. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  1603. else
  1604. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  1605. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  1606. }
  1607. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  1608. {
  1609. u32 reg;
  1610. dev_vdbg(dwc->dev, "%s\n", __func__);
  1611. /*
  1612. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  1613. * would cause a missing Disconnect Event if there's a
  1614. * pending Setup Packet in the FIFO.
  1615. *
  1616. * There's no suggested workaround on the official Bug
  1617. * report, which states that "unless the driver/application
  1618. * is doing any special handling of a disconnect event,
  1619. * there is no functional issue".
  1620. *
  1621. * Unfortunately, it turns out that we _do_ some special
  1622. * handling of a disconnect event, namely complete all
  1623. * pending transfers, notify gadget driver of the
  1624. * disconnection, and so on.
  1625. *
  1626. * Our suggested workaround is to follow the Disconnect
  1627. * Event steps here, instead, based on a setup_packet_pending
  1628. * flag. Such flag gets set whenever we have a XferNotReady
  1629. * event on EP0 and gets cleared on XferComplete for the
  1630. * same endpoint.
  1631. *
  1632. * Refers to:
  1633. *
  1634. * STAR#9000466709: RTL: Device : Disconnect event not
  1635. * generated if setup packet pending in FIFO
  1636. */
  1637. if (dwc->revision < DWC3_REVISION_188A) {
  1638. if (dwc->setup_packet_pending)
  1639. dwc3_gadget_disconnect_interrupt(dwc);
  1640. }
  1641. /* after reset -> Default State */
  1642. dwc->dev_state = DWC3_DEFAULT_STATE;
  1643. /* Recent versions support automatic phy suspend and don't need this */
  1644. if (dwc->revision < DWC3_REVISION_194A) {
  1645. /* Resume PHYs */
  1646. dwc3_gadget_usb2_phy_suspend(dwc, false);
  1647. dwc3_gadget_usb3_phy_suspend(dwc, false);
  1648. }
  1649. if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
  1650. dwc3_disconnect_gadget(dwc);
  1651. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1652. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  1653. reg &= ~(DWC3_DCTL_INITU1ENA | DWC3_DCTL_INITU2ENA);
  1654. reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
  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(31);
  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. }