gadget.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274
  1. /**
  2. * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/list.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include "debug.h"
  31. #include "core.h"
  32. #include "gadget.h"
  33. #include "io.h"
  34. /**
  35. * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
  36. * @dwc: pointer to our context structure
  37. * @mode: the mode to set (J, K SE0 NAK, Force Enable)
  38. *
  39. * Caller should take care of locking. This function will
  40. * return 0 on success or -EINVAL if wrong Test Selector
  41. * is passed
  42. */
  43. int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
  44. {
  45. u32 reg;
  46. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  47. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  48. switch (mode) {
  49. case TEST_J:
  50. case TEST_K:
  51. case TEST_SE0_NAK:
  52. case TEST_PACKET:
  53. case TEST_FORCE_EN:
  54. reg |= mode << 1;
  55. break;
  56. default:
  57. return -EINVAL;
  58. }
  59. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  60. return 0;
  61. }
  62. /**
  63. * dwc3_gadget_get_link_state - Gets current state of USB Link
  64. * @dwc: pointer to our context structure
  65. *
  66. * Caller should take care of locking. This function will
  67. * return the link state on success (>= 0) or -ETIMEDOUT.
  68. */
  69. int dwc3_gadget_get_link_state(struct dwc3 *dwc)
  70. {
  71. u32 reg;
  72. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  73. return DWC3_DSTS_USBLNKST(reg);
  74. }
  75. /**
  76. * dwc3_gadget_set_link_state - Sets USB Link to a particular State
  77. * @dwc: pointer to our context structure
  78. * @state: the state to put link into
  79. *
  80. * Caller should take care of locking. This function will
  81. * return 0 on success or -ETIMEDOUT.
  82. */
  83. int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
  84. {
  85. int retries = 10000;
  86. u32 reg;
  87. /*
  88. * Wait until device controller is ready. Only applies to 1.94a and
  89. * later RTL.
  90. */
  91. if (dwc->revision >= DWC3_REVISION_194A) {
  92. while (--retries) {
  93. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  94. if (reg & DWC3_DSTS_DCNRD)
  95. udelay(5);
  96. else
  97. break;
  98. }
  99. if (retries <= 0)
  100. return -ETIMEDOUT;
  101. }
  102. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  103. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  104. /* set requested state */
  105. reg |= DWC3_DCTL_ULSTCHNGREQ(state);
  106. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  107. /*
  108. * The following code is racy when called from dwc3_gadget_wakeup,
  109. * and is not needed, at least on newer versions
  110. */
  111. if (dwc->revision >= DWC3_REVISION_194A)
  112. return 0;
  113. /* wait for a change in DSTS */
  114. retries = 10000;
  115. while (--retries) {
  116. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  117. if (DWC3_DSTS_USBLNKST(reg) == state)
  118. return 0;
  119. udelay(5);
  120. }
  121. return -ETIMEDOUT;
  122. }
  123. /**
  124. * dwc3_ep_inc_trb() - Increment a TRB index.
  125. * @index - Pointer to the TRB index to increment.
  126. *
  127. * The index should never point to the link TRB. After incrementing,
  128. * if it is point to the link TRB, wrap around to the beginning. The
  129. * link TRB is always at the last TRB entry.
  130. */
  131. static void dwc3_ep_inc_trb(u8 *index)
  132. {
  133. (*index)++;
  134. if (*index == (DWC3_TRB_NUM - 1))
  135. *index = 0;
  136. }
  137. static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
  138. {
  139. dwc3_ep_inc_trb(&dep->trb_enqueue);
  140. }
  141. static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
  142. {
  143. dwc3_ep_inc_trb(&dep->trb_dequeue);
  144. }
  145. void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
  146. int status)
  147. {
  148. struct dwc3 *dwc = dep->dwc;
  149. req->started = false;
  150. list_del(&req->list);
  151. req->trb = NULL;
  152. req->remaining = 0;
  153. if (req->request.status == -EINPROGRESS)
  154. req->request.status = status;
  155. usb_gadget_unmap_request_by_dev(dwc->sysdev,
  156. &req->request, req->direction);
  157. trace_dwc3_gadget_giveback(req);
  158. spin_unlock(&dwc->lock);
  159. usb_gadget_giveback_request(&dep->endpoint, &req->request);
  160. spin_lock(&dwc->lock);
  161. if (dep->number > 1)
  162. pm_runtime_put(dwc->dev);
  163. }
  164. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
  165. {
  166. u32 timeout = 500;
  167. int status = 0;
  168. int ret = 0;
  169. u32 reg;
  170. dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
  171. dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
  172. do {
  173. reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
  174. if (!(reg & DWC3_DGCMD_CMDACT)) {
  175. status = DWC3_DGCMD_STATUS(reg);
  176. if (status)
  177. ret = -EINVAL;
  178. break;
  179. }
  180. } while (--timeout);
  181. if (!timeout) {
  182. ret = -ETIMEDOUT;
  183. status = -ETIMEDOUT;
  184. }
  185. trace_dwc3_gadget_generic_cmd(cmd, param, status);
  186. return ret;
  187. }
  188. static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
  189. int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
  190. struct dwc3_gadget_ep_cmd_params *params)
  191. {
  192. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  193. struct dwc3 *dwc = dep->dwc;
  194. u32 timeout = 500;
  195. u32 reg;
  196. int cmd_status = 0;
  197. int susphy = false;
  198. int ret = -EINVAL;
  199. /*
  200. * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
  201. * we're issuing an endpoint command, we must check if
  202. * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
  203. *
  204. * We will also set SUSPHY bit to what it was before returning as stated
  205. * by the same section on Synopsys databook.
  206. */
  207. if (dwc->gadget.speed <= USB_SPEED_HIGH) {
  208. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  209. if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
  210. susphy = true;
  211. reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
  212. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  213. }
  214. }
  215. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
  216. int needs_wakeup;
  217. needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
  218. dwc->link_state == DWC3_LINK_STATE_U2 ||
  219. dwc->link_state == DWC3_LINK_STATE_U3);
  220. if (unlikely(needs_wakeup)) {
  221. ret = __dwc3_gadget_wakeup(dwc);
  222. dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
  223. ret);
  224. }
  225. }
  226. dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
  227. dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
  228. dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
  229. /*
  230. * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
  231. * not relying on XferNotReady, we can make use of a special "No
  232. * Response Update Transfer" command where we should clear both CmdAct
  233. * and CmdIOC bits.
  234. *
  235. * With this, we don't need to wait for command completion and can
  236. * straight away issue further commands to the endpoint.
  237. *
  238. * NOTICE: We're making an assumption that control endpoints will never
  239. * make use of Update Transfer command. This is a safe assumption
  240. * because we can never have more than one request at a time with
  241. * Control Endpoints. If anybody changes that assumption, this chunk
  242. * needs to be updated accordingly.
  243. */
  244. if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
  245. !usb_endpoint_xfer_isoc(desc))
  246. cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
  247. else
  248. cmd |= DWC3_DEPCMD_CMDACT;
  249. dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
  250. do {
  251. reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
  252. if (!(reg & DWC3_DEPCMD_CMDACT)) {
  253. cmd_status = DWC3_DEPCMD_STATUS(reg);
  254. switch (cmd_status) {
  255. case 0:
  256. ret = 0;
  257. break;
  258. case DEPEVT_TRANSFER_NO_RESOURCE:
  259. ret = -EINVAL;
  260. break;
  261. case DEPEVT_TRANSFER_BUS_EXPIRY:
  262. /*
  263. * SW issues START TRANSFER command to
  264. * isochronous ep with future frame interval. If
  265. * future interval time has already passed when
  266. * core receives the command, it will respond
  267. * with an error status of 'Bus Expiry'.
  268. *
  269. * Instead of always returning -EINVAL, let's
  270. * give a hint to the gadget driver that this is
  271. * the case by returning -EAGAIN.
  272. */
  273. ret = -EAGAIN;
  274. break;
  275. default:
  276. dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
  277. }
  278. break;
  279. }
  280. } while (--timeout);
  281. if (timeout == 0) {
  282. ret = -ETIMEDOUT;
  283. cmd_status = -ETIMEDOUT;
  284. }
  285. trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
  286. if (ret == 0) {
  287. switch (DWC3_DEPCMD_CMD(cmd)) {
  288. case DWC3_DEPCMD_STARTTRANSFER:
  289. dep->flags |= DWC3_EP_TRANSFER_STARTED;
  290. break;
  291. case DWC3_DEPCMD_ENDTRANSFER:
  292. dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  293. break;
  294. default:
  295. /* nothing */
  296. break;
  297. }
  298. }
  299. if (unlikely(susphy)) {
  300. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  301. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  302. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  303. }
  304. return ret;
  305. }
  306. static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
  307. {
  308. struct dwc3 *dwc = dep->dwc;
  309. struct dwc3_gadget_ep_cmd_params params;
  310. u32 cmd = DWC3_DEPCMD_CLEARSTALL;
  311. /*
  312. * As of core revision 2.60a the recommended programming model
  313. * is to set the ClearPendIN bit when issuing a Clear Stall EP
  314. * command for IN endpoints. This is to prevent an issue where
  315. * some (non-compliant) hosts may not send ACK TPs for pending
  316. * IN transfers due to a mishandled error condition. Synopsys
  317. * STAR 9000614252.
  318. */
  319. if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
  320. (dwc->gadget.speed >= USB_SPEED_SUPER))
  321. cmd |= DWC3_DEPCMD_CLEARPENDIN;
  322. memset(&params, 0, sizeof(params));
  323. return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  324. }
  325. static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
  326. struct dwc3_trb *trb)
  327. {
  328. u32 offset = (char *) trb - (char *) dep->trb_pool;
  329. return dep->trb_pool_dma + offset;
  330. }
  331. static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
  332. {
  333. struct dwc3 *dwc = dep->dwc;
  334. if (dep->trb_pool)
  335. return 0;
  336. dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
  337. sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  338. &dep->trb_pool_dma, GFP_KERNEL);
  339. if (!dep->trb_pool) {
  340. dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
  341. dep->name);
  342. return -ENOMEM;
  343. }
  344. return 0;
  345. }
  346. static void dwc3_free_trb_pool(struct dwc3_ep *dep)
  347. {
  348. struct dwc3 *dwc = dep->dwc;
  349. dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
  350. dep->trb_pool, dep->trb_pool_dma);
  351. dep->trb_pool = NULL;
  352. dep->trb_pool_dma = 0;
  353. }
  354. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
  355. /**
  356. * dwc3_gadget_start_config - Configure EP resources
  357. * @dwc: pointer to our controller context structure
  358. * @dep: endpoint that is being enabled
  359. *
  360. * The assignment of transfer resources cannot perfectly follow the
  361. * data book due to the fact that the controller driver does not have
  362. * all knowledge of the configuration in advance. It is given this
  363. * information piecemeal by the composite gadget framework after every
  364. * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
  365. * programming model in this scenario can cause errors. For two
  366. * reasons:
  367. *
  368. * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
  369. * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
  370. * multiple interfaces.
  371. *
  372. * 2) The databook does not mention doing more DEPXFERCFG for new
  373. * endpoint on alt setting (8.1.6).
  374. *
  375. * The following simplified method is used instead:
  376. *
  377. * All hardware endpoints can be assigned a transfer resource and this
  378. * setting will stay persistent until either a core reset or
  379. * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
  380. * do DEPXFERCFG for every hardware endpoint as well. We are
  381. * guaranteed that there are as many transfer resources as endpoints.
  382. *
  383. * This function is called for each endpoint when it is being enabled
  384. * but is triggered only when called for EP0-out, which always happens
  385. * first, and which should only happen in one of the above conditions.
  386. */
  387. static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
  388. {
  389. struct dwc3_gadget_ep_cmd_params params;
  390. u32 cmd;
  391. int i;
  392. int ret;
  393. if (dep->number)
  394. return 0;
  395. memset(&params, 0x00, sizeof(params));
  396. cmd = DWC3_DEPCMD_DEPSTARTCFG;
  397. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  398. if (ret)
  399. return ret;
  400. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  401. struct dwc3_ep *dep = dwc->eps[i];
  402. if (!dep)
  403. continue;
  404. ret = dwc3_gadget_set_xfer_resource(dwc, dep);
  405. if (ret)
  406. return ret;
  407. }
  408. return 0;
  409. }
  410. static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
  411. bool modify, bool restore)
  412. {
  413. const struct usb_ss_ep_comp_descriptor *comp_desc;
  414. const struct usb_endpoint_descriptor *desc;
  415. struct dwc3_gadget_ep_cmd_params params;
  416. if (dev_WARN_ONCE(dwc->dev, modify && restore,
  417. "Can't modify and restore\n"))
  418. return -EINVAL;
  419. comp_desc = dep->endpoint.comp_desc;
  420. desc = dep->endpoint.desc;
  421. memset(&params, 0x00, sizeof(params));
  422. params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
  423. | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
  424. /* Burst size is only needed in SuperSpeed mode */
  425. if (dwc->gadget.speed >= USB_SPEED_SUPER) {
  426. u32 burst = dep->endpoint.maxburst;
  427. params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
  428. }
  429. if (modify) {
  430. params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
  431. } else if (restore) {
  432. params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
  433. params.param2 |= dep->saved_state;
  434. } else {
  435. params.param0 |= DWC3_DEPCFG_ACTION_INIT;
  436. }
  437. if (usb_endpoint_xfer_control(desc))
  438. params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
  439. if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
  440. params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
  441. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  442. params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
  443. | DWC3_DEPCFG_STREAM_EVENT_EN;
  444. dep->stream_capable = true;
  445. }
  446. if (!usb_endpoint_xfer_control(desc))
  447. params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
  448. /*
  449. * We are doing 1:1 mapping for endpoints, meaning
  450. * Physical Endpoints 2 maps to Logical Endpoint 2 and
  451. * so on. We consider the direction bit as part of the physical
  452. * endpoint number. So USB endpoint 0x81 is 0x03.
  453. */
  454. params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
  455. /*
  456. * We must use the lower 16 TX FIFOs even though
  457. * HW might have more
  458. */
  459. if (dep->direction)
  460. params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
  461. if (desc->bInterval) {
  462. params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
  463. dep->interval = 1 << (desc->bInterval - 1);
  464. }
  465. return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
  466. }
  467. static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
  468. {
  469. struct dwc3_gadget_ep_cmd_params params;
  470. memset(&params, 0x00, sizeof(params));
  471. params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
  472. return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
  473. &params);
  474. }
  475. /**
  476. * __dwc3_gadget_ep_enable - Initializes a HW endpoint
  477. * @dep: endpoint to be initialized
  478. * @desc: USB Endpoint Descriptor
  479. *
  480. * Caller should take care of locking
  481. */
  482. static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
  483. bool modify, bool restore)
  484. {
  485. const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
  486. struct dwc3 *dwc = dep->dwc;
  487. u32 reg;
  488. int ret;
  489. if (!(dep->flags & DWC3_EP_ENABLED)) {
  490. ret = dwc3_gadget_start_config(dwc, dep);
  491. if (ret)
  492. return ret;
  493. }
  494. ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
  495. if (ret)
  496. return ret;
  497. if (!(dep->flags & DWC3_EP_ENABLED)) {
  498. struct dwc3_trb *trb_st_hw;
  499. struct dwc3_trb *trb_link;
  500. dep->type = usb_endpoint_type(desc);
  501. dep->flags |= DWC3_EP_ENABLED;
  502. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  503. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  504. reg |= DWC3_DALEPENA_EP(dep->number);
  505. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  506. init_waitqueue_head(&dep->wait_end_transfer);
  507. if (usb_endpoint_xfer_control(desc))
  508. goto out;
  509. /* Initialize the TRB ring */
  510. dep->trb_dequeue = 0;
  511. dep->trb_enqueue = 0;
  512. memset(dep->trb_pool, 0,
  513. sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
  514. /* Link TRB. The HWO bit is never reset */
  515. trb_st_hw = &dep->trb_pool[0];
  516. trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
  517. trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  518. trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
  519. trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
  520. trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
  521. }
  522. /*
  523. * Issue StartTransfer here with no-op TRB so we can always rely on No
  524. * Response Update Transfer command.
  525. */
  526. if (usb_endpoint_xfer_bulk(desc)) {
  527. struct dwc3_gadget_ep_cmd_params params;
  528. struct dwc3_trb *trb;
  529. dma_addr_t trb_dma;
  530. u32 cmd;
  531. memset(&params, 0, sizeof(params));
  532. trb = &dep->trb_pool[0];
  533. trb_dma = dwc3_trb_dma_offset(dep, trb);
  534. params.param0 = upper_32_bits(trb_dma);
  535. params.param1 = lower_32_bits(trb_dma);
  536. cmd = DWC3_DEPCMD_STARTTRANSFER;
  537. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  538. if (ret < 0)
  539. return ret;
  540. dep->flags |= DWC3_EP_BUSY;
  541. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
  542. WARN_ON_ONCE(!dep->resource_index);
  543. }
  544. out:
  545. trace_dwc3_gadget_ep_enable(dep);
  546. return 0;
  547. }
  548. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
  549. static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
  550. {
  551. struct dwc3_request *req;
  552. dwc3_stop_active_transfer(dwc, dep->number, true);
  553. /* - giveback all requests to gadget driver */
  554. while (!list_empty(&dep->started_list)) {
  555. req = next_request(&dep->started_list);
  556. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  557. }
  558. while (!list_empty(&dep->pending_list)) {
  559. req = next_request(&dep->pending_list);
  560. dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
  561. }
  562. }
  563. /**
  564. * __dwc3_gadget_ep_disable - Disables a HW endpoint
  565. * @dep: the endpoint to disable
  566. *
  567. * This function also removes requests which are currently processed ny the
  568. * hardware and those which are not yet scheduled.
  569. * Caller should take care of locking.
  570. */
  571. static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  572. {
  573. struct dwc3 *dwc = dep->dwc;
  574. u32 reg;
  575. trace_dwc3_gadget_ep_disable(dep);
  576. dwc3_remove_requests(dwc, dep);
  577. /* make sure HW endpoint isn't stalled */
  578. if (dep->flags & DWC3_EP_STALL)
  579. __dwc3_gadget_ep_set_halt(dep, 0, false);
  580. reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
  581. reg &= ~DWC3_DALEPENA_EP(dep->number);
  582. dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
  583. dep->stream_capable = false;
  584. dep->type = 0;
  585. dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
  586. /* Clear out the ep descriptors for non-ep0 */
  587. if (dep->number > 1) {
  588. dep->endpoint.comp_desc = NULL;
  589. dep->endpoint.desc = NULL;
  590. }
  591. return 0;
  592. }
  593. /* -------------------------------------------------------------------------- */
  594. static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
  595. const struct usb_endpoint_descriptor *desc)
  596. {
  597. return -EINVAL;
  598. }
  599. static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
  600. {
  601. return -EINVAL;
  602. }
  603. /* -------------------------------------------------------------------------- */
  604. static int dwc3_gadget_ep_enable(struct usb_ep *ep,
  605. const struct usb_endpoint_descriptor *desc)
  606. {
  607. struct dwc3_ep *dep;
  608. struct dwc3 *dwc;
  609. unsigned long flags;
  610. int ret;
  611. if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  612. pr_debug("dwc3: invalid parameters\n");
  613. return -EINVAL;
  614. }
  615. if (!desc->wMaxPacketSize) {
  616. pr_debug("dwc3: missing wMaxPacketSize\n");
  617. return -EINVAL;
  618. }
  619. dep = to_dwc3_ep(ep);
  620. dwc = dep->dwc;
  621. if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
  622. "%s is already enabled\n",
  623. dep->name))
  624. return 0;
  625. spin_lock_irqsave(&dwc->lock, flags);
  626. ret = __dwc3_gadget_ep_enable(dep, false, false);
  627. spin_unlock_irqrestore(&dwc->lock, flags);
  628. return ret;
  629. }
  630. static int dwc3_gadget_ep_disable(struct usb_ep *ep)
  631. {
  632. struct dwc3_ep *dep;
  633. struct dwc3 *dwc;
  634. unsigned long flags;
  635. int ret;
  636. if (!ep) {
  637. pr_debug("dwc3: invalid parameters\n");
  638. return -EINVAL;
  639. }
  640. dep = to_dwc3_ep(ep);
  641. dwc = dep->dwc;
  642. if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
  643. "%s is already disabled\n",
  644. dep->name))
  645. return 0;
  646. spin_lock_irqsave(&dwc->lock, flags);
  647. ret = __dwc3_gadget_ep_disable(dep);
  648. spin_unlock_irqrestore(&dwc->lock, flags);
  649. return ret;
  650. }
  651. static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
  652. gfp_t gfp_flags)
  653. {
  654. struct dwc3_request *req;
  655. struct dwc3_ep *dep = to_dwc3_ep(ep);
  656. req = kzalloc(sizeof(*req), gfp_flags);
  657. if (!req)
  658. return NULL;
  659. req->epnum = dep->number;
  660. req->dep = dep;
  661. dep->allocated_requests++;
  662. trace_dwc3_alloc_request(req);
  663. return &req->request;
  664. }
  665. static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
  666. struct usb_request *request)
  667. {
  668. struct dwc3_request *req = to_dwc3_request(request);
  669. struct dwc3_ep *dep = to_dwc3_ep(ep);
  670. dep->allocated_requests--;
  671. trace_dwc3_free_request(req);
  672. kfree(req);
  673. }
  674. static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
  675. static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
  676. dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
  677. unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
  678. {
  679. struct dwc3 *dwc = dep->dwc;
  680. struct usb_gadget *gadget = &dwc->gadget;
  681. enum usb_device_speed speed = gadget->speed;
  682. dwc3_ep_inc_enq(dep);
  683. trb->size = DWC3_TRB_SIZE_LENGTH(length);
  684. trb->bpl = lower_32_bits(dma);
  685. trb->bph = upper_32_bits(dma);
  686. switch (usb_endpoint_type(dep->endpoint.desc)) {
  687. case USB_ENDPOINT_XFER_CONTROL:
  688. trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
  689. break;
  690. case USB_ENDPOINT_XFER_ISOC:
  691. if (!node) {
  692. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
  693. if (speed == USB_SPEED_HIGH) {
  694. struct usb_ep *ep = &dep->endpoint;
  695. trb->size |= DWC3_TRB_SIZE_PCM1(ep->mult - 1);
  696. }
  697. } else {
  698. trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
  699. }
  700. /* always enable Interrupt on Missed ISOC */
  701. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  702. break;
  703. case USB_ENDPOINT_XFER_BULK:
  704. case USB_ENDPOINT_XFER_INT:
  705. trb->ctrl = DWC3_TRBCTL_NORMAL;
  706. break;
  707. default:
  708. /*
  709. * This is only possible with faulty memory because we
  710. * checked it already :)
  711. */
  712. dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
  713. usb_endpoint_type(dep->endpoint.desc));
  714. }
  715. /* always enable Continue on Short Packet */
  716. if (usb_endpoint_dir_out(dep->endpoint.desc)) {
  717. trb->ctrl |= DWC3_TRB_CTRL_CSP;
  718. if (short_not_ok)
  719. trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
  720. }
  721. if ((!no_interrupt && !chain) ||
  722. (dwc3_calc_trbs_left(dep) == 0))
  723. trb->ctrl |= DWC3_TRB_CTRL_IOC;
  724. if (chain)
  725. trb->ctrl |= DWC3_TRB_CTRL_CHN;
  726. if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
  727. trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
  728. trb->ctrl |= DWC3_TRB_CTRL_HWO;
  729. trace_dwc3_prepare_trb(dep, trb);
  730. }
  731. /**
  732. * dwc3_prepare_one_trb - setup one TRB from one request
  733. * @dep: endpoint for which this request is prepared
  734. * @req: dwc3_request pointer
  735. * @chain: should this TRB be chained to the next?
  736. * @node: only for isochronous endpoints. First TRB needs different type.
  737. */
  738. static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
  739. struct dwc3_request *req, unsigned chain, unsigned node)
  740. {
  741. struct dwc3_trb *trb;
  742. unsigned length = req->request.length;
  743. unsigned stream_id = req->request.stream_id;
  744. unsigned short_not_ok = req->request.short_not_ok;
  745. unsigned no_interrupt = req->request.no_interrupt;
  746. dma_addr_t dma = req->request.dma;
  747. trb = &dep->trb_pool[dep->trb_enqueue];
  748. if (!req->trb) {
  749. dwc3_gadget_move_started_request(req);
  750. req->trb = trb;
  751. req->trb_dma = dwc3_trb_dma_offset(dep, trb);
  752. dep->queued_requests++;
  753. }
  754. __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
  755. stream_id, short_not_ok, no_interrupt);
  756. }
  757. /**
  758. * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
  759. * @dep: The endpoint with the TRB ring
  760. * @index: The index of the current TRB in the ring
  761. *
  762. * Returns the TRB prior to the one pointed to by the index. If the
  763. * index is 0, we will wrap backwards, skip the link TRB, and return
  764. * the one just before that.
  765. */
  766. static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
  767. {
  768. u8 tmp = index;
  769. if (!tmp)
  770. tmp = DWC3_TRB_NUM - 1;
  771. return &dep->trb_pool[tmp - 1];
  772. }
  773. static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
  774. {
  775. struct dwc3_trb *tmp;
  776. struct dwc3 *dwc = dep->dwc;
  777. u8 trbs_left;
  778. /*
  779. * If enqueue & dequeue are equal than it is either full or empty.
  780. *
  781. * One way to know for sure is if the TRB right before us has HWO bit
  782. * set or not. If it has, then we're definitely full and can't fit any
  783. * more transfers in our ring.
  784. */
  785. if (dep->trb_enqueue == dep->trb_dequeue) {
  786. tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  787. if (dev_WARN_ONCE(dwc->dev, tmp->ctrl & DWC3_TRB_CTRL_HWO,
  788. "%s No TRBS left\n", dep->name))
  789. return 0;
  790. return DWC3_TRB_NUM - 1;
  791. }
  792. trbs_left = dep->trb_dequeue - dep->trb_enqueue;
  793. trbs_left &= (DWC3_TRB_NUM - 1);
  794. if (dep->trb_dequeue < dep->trb_enqueue)
  795. trbs_left--;
  796. return trbs_left;
  797. }
  798. static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
  799. struct dwc3_request *req)
  800. {
  801. struct scatterlist *sg = req->sg;
  802. struct scatterlist *s;
  803. int i;
  804. for_each_sg(sg, s, req->num_pending_sgs, i) {
  805. unsigned int length = req->request.length;
  806. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  807. unsigned int rem = length % maxp;
  808. unsigned chain = true;
  809. if (sg_is_last(s))
  810. chain = false;
  811. if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
  812. struct dwc3 *dwc = dep->dwc;
  813. struct dwc3_trb *trb;
  814. req->unaligned = true;
  815. /* prepare normal TRB */
  816. dwc3_prepare_one_trb(dep, req, true, i);
  817. /* Now prepare one extra TRB to align transfer size */
  818. trb = &dep->trb_pool[dep->trb_enqueue];
  819. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
  820. maxp - rem, false, 0,
  821. req->request.stream_id,
  822. req->request.short_not_ok,
  823. req->request.no_interrupt);
  824. } else {
  825. dwc3_prepare_one_trb(dep, req, chain, i);
  826. }
  827. if (!dwc3_calc_trbs_left(dep))
  828. break;
  829. }
  830. }
  831. static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
  832. struct dwc3_request *req)
  833. {
  834. unsigned int length = req->request.length;
  835. unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
  836. unsigned int rem = length % maxp;
  837. if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
  838. struct dwc3 *dwc = dep->dwc;
  839. struct dwc3_trb *trb;
  840. req->unaligned = true;
  841. /* prepare normal TRB */
  842. dwc3_prepare_one_trb(dep, req, true, 0);
  843. /* Now prepare one extra TRB to align transfer size */
  844. trb = &dep->trb_pool[dep->trb_enqueue];
  845. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
  846. false, 0, req->request.stream_id,
  847. req->request.short_not_ok,
  848. req->request.no_interrupt);
  849. } else if (req->request.zero && req->request.length &&
  850. (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
  851. struct dwc3 *dwc = dep->dwc;
  852. struct dwc3_trb *trb;
  853. req->zero = true;
  854. /* prepare normal TRB */
  855. dwc3_prepare_one_trb(dep, req, true, 0);
  856. /* Now prepare one extra TRB to handle ZLP */
  857. trb = &dep->trb_pool[dep->trb_enqueue];
  858. __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
  859. false, 0, req->request.stream_id,
  860. req->request.short_not_ok,
  861. req->request.no_interrupt);
  862. } else {
  863. dwc3_prepare_one_trb(dep, req, false, 0);
  864. }
  865. }
  866. /*
  867. * dwc3_prepare_trbs - setup TRBs from requests
  868. * @dep: endpoint for which requests are being prepared
  869. *
  870. * The function goes through the requests list and sets up TRBs for the
  871. * transfers. The function returns once there are no more TRBs available or
  872. * it runs out of requests.
  873. */
  874. static void dwc3_prepare_trbs(struct dwc3_ep *dep)
  875. {
  876. struct dwc3_request *req, *n;
  877. BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
  878. if (!dwc3_calc_trbs_left(dep))
  879. return;
  880. /*
  881. * We can get in a situation where there's a request in the started list
  882. * but there weren't enough TRBs to fully kick it in the first time
  883. * around, so it has been waiting for more TRBs to be freed up.
  884. *
  885. * In that case, we should check if we have a request with pending_sgs
  886. * in the started list and prepare TRBs for that request first,
  887. * otherwise we will prepare TRBs completely out of order and that will
  888. * break things.
  889. */
  890. list_for_each_entry(req, &dep->started_list, list) {
  891. if (req->num_pending_sgs > 0)
  892. dwc3_prepare_one_trb_sg(dep, req);
  893. if (!dwc3_calc_trbs_left(dep))
  894. return;
  895. }
  896. list_for_each_entry_safe(req, n, &dep->pending_list, list) {
  897. if (req->num_pending_sgs > 0)
  898. dwc3_prepare_one_trb_sg(dep, req);
  899. else
  900. dwc3_prepare_one_trb_linear(dep, req);
  901. if (!dwc3_calc_trbs_left(dep))
  902. return;
  903. }
  904. }
  905. static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
  906. {
  907. struct dwc3_gadget_ep_cmd_params params;
  908. struct dwc3_request *req;
  909. int starting;
  910. int ret;
  911. u32 cmd;
  912. starting = !(dep->flags & DWC3_EP_BUSY);
  913. dwc3_prepare_trbs(dep);
  914. req = next_request(&dep->started_list);
  915. if (!req) {
  916. dep->flags |= DWC3_EP_PENDING_REQUEST;
  917. return 0;
  918. }
  919. memset(&params, 0, sizeof(params));
  920. if (starting) {
  921. params.param0 = upper_32_bits(req->trb_dma);
  922. params.param1 = lower_32_bits(req->trb_dma);
  923. cmd = DWC3_DEPCMD_STARTTRANSFER |
  924. DWC3_DEPCMD_PARAM(cmd_param);
  925. } else {
  926. cmd = DWC3_DEPCMD_UPDATETRANSFER |
  927. DWC3_DEPCMD_PARAM(dep->resource_index);
  928. }
  929. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  930. if (ret < 0) {
  931. /*
  932. * FIXME we need to iterate over the list of requests
  933. * here and stop, unmap, free and del each of the linked
  934. * requests instead of what we do now.
  935. */
  936. if (req->trb)
  937. memset(req->trb, 0, sizeof(struct dwc3_trb));
  938. dep->queued_requests--;
  939. dwc3_gadget_giveback(dep, req, ret);
  940. return ret;
  941. }
  942. dep->flags |= DWC3_EP_BUSY;
  943. if (starting) {
  944. dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
  945. WARN_ON_ONCE(!dep->resource_index);
  946. }
  947. return 0;
  948. }
  949. static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
  950. {
  951. u32 reg;
  952. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  953. return DWC3_DSTS_SOFFN(reg);
  954. }
  955. static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
  956. struct dwc3_ep *dep, u32 cur_uf)
  957. {
  958. u32 uf;
  959. if (list_empty(&dep->pending_list)) {
  960. dev_info(dwc->dev, "%s: ran out of requests\n",
  961. dep->name);
  962. dep->flags |= DWC3_EP_PENDING_REQUEST;
  963. return;
  964. }
  965. /*
  966. * Schedule the first trb for one interval in the future or at
  967. * least 4 microframes.
  968. */
  969. uf = cur_uf + max_t(u32, 4, dep->interval);
  970. __dwc3_gadget_kick_transfer(dep, uf);
  971. }
  972. static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
  973. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  974. {
  975. u32 cur_uf, mask;
  976. mask = ~(dep->interval - 1);
  977. cur_uf = event->parameters & mask;
  978. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  979. }
  980. static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  981. {
  982. struct dwc3 *dwc = dep->dwc;
  983. int ret;
  984. if (!dep->endpoint.desc) {
  985. dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
  986. dep->name);
  987. return -ESHUTDOWN;
  988. }
  989. if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
  990. &req->request, req->dep->name)) {
  991. dev_err(dwc->dev, "%s: request %p belongs to '%s'\n",
  992. dep->name, &req->request, req->dep->name);
  993. return -EINVAL;
  994. }
  995. pm_runtime_get(dwc->dev);
  996. req->request.actual = 0;
  997. req->request.status = -EINPROGRESS;
  998. req->direction = dep->direction;
  999. req->epnum = dep->number;
  1000. trace_dwc3_ep_queue(req);
  1001. ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
  1002. dep->direction);
  1003. if (ret)
  1004. return ret;
  1005. req->sg = req->request.sg;
  1006. req->num_pending_sgs = req->request.num_mapped_sgs;
  1007. list_add_tail(&req->list, &dep->pending_list);
  1008. /*
  1009. * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
  1010. * wait for a XferNotReady event so we will know what's the current
  1011. * (micro-)frame number.
  1012. *
  1013. * Without this trick, we are very, very likely gonna get Bus Expiry
  1014. * errors which will force us issue EndTransfer command.
  1015. */
  1016. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1017. if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
  1018. if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
  1019. dwc3_stop_active_transfer(dwc, dep->number, true);
  1020. dep->flags = DWC3_EP_ENABLED;
  1021. } else {
  1022. u32 cur_uf;
  1023. cur_uf = __dwc3_gadget_get_frame(dwc);
  1024. __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
  1025. dep->flags &= ~DWC3_EP_PENDING_REQUEST;
  1026. }
  1027. return 0;
  1028. }
  1029. if ((dep->flags & DWC3_EP_BUSY) &&
  1030. !(dep->flags & DWC3_EP_MISSED_ISOC)) {
  1031. WARN_ON_ONCE(!dep->resource_index);
  1032. ret = __dwc3_gadget_kick_transfer(dep,
  1033. dep->resource_index);
  1034. }
  1035. goto out;
  1036. }
  1037. if (!dwc3_calc_trbs_left(dep))
  1038. return 0;
  1039. ret = __dwc3_gadget_kick_transfer(dep, 0);
  1040. out:
  1041. if (ret == -EBUSY)
  1042. ret = 0;
  1043. return ret;
  1044. }
  1045. static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  1046. gfp_t gfp_flags)
  1047. {
  1048. struct dwc3_request *req = to_dwc3_request(request);
  1049. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1050. struct dwc3 *dwc = dep->dwc;
  1051. unsigned long flags;
  1052. int ret;
  1053. spin_lock_irqsave(&dwc->lock, flags);
  1054. ret = __dwc3_gadget_ep_queue(dep, req);
  1055. spin_unlock_irqrestore(&dwc->lock, flags);
  1056. return ret;
  1057. }
  1058. static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
  1059. struct usb_request *request)
  1060. {
  1061. struct dwc3_request *req = to_dwc3_request(request);
  1062. struct dwc3_request *r = NULL;
  1063. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1064. struct dwc3 *dwc = dep->dwc;
  1065. unsigned long flags;
  1066. int ret = 0;
  1067. trace_dwc3_ep_dequeue(req);
  1068. spin_lock_irqsave(&dwc->lock, flags);
  1069. list_for_each_entry(r, &dep->pending_list, list) {
  1070. if (r == req)
  1071. break;
  1072. }
  1073. if (r != req) {
  1074. list_for_each_entry(r, &dep->started_list, list) {
  1075. if (r == req)
  1076. break;
  1077. }
  1078. if (r == req) {
  1079. /* wait until it is processed */
  1080. dwc3_stop_active_transfer(dwc, dep->number, true);
  1081. /*
  1082. * If request was already started, this means we had to
  1083. * stop the transfer. With that we also need to ignore
  1084. * all TRBs used by the request, however TRBs can only
  1085. * be modified after completion of END_TRANSFER
  1086. * command. So what we do here is that we wait for
  1087. * END_TRANSFER completion and only after that, we jump
  1088. * over TRBs by clearing HWO and incrementing dequeue
  1089. * pointer.
  1090. *
  1091. * Note that we have 2 possible types of transfers here:
  1092. *
  1093. * i) Linear buffer request
  1094. * ii) SG-list based request
  1095. *
  1096. * SG-list based requests will have r->num_pending_sgs
  1097. * set to a valid number (> 0). Linear requests,
  1098. * normally use a single TRB.
  1099. *
  1100. * For each of these two cases, if r->unaligned flag is
  1101. * set, one extra TRB has been used to align transfer
  1102. * size to wMaxPacketSize.
  1103. *
  1104. * All of these cases need to be taken into
  1105. * consideration so we don't mess up our TRB ring
  1106. * pointers.
  1107. */
  1108. wait_event_lock_irq(dep->wait_end_transfer,
  1109. !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
  1110. dwc->lock);
  1111. if (!r->trb)
  1112. goto out1;
  1113. if (r->num_pending_sgs) {
  1114. struct dwc3_trb *trb;
  1115. int i = 0;
  1116. for (i = 0; i < r->num_pending_sgs; i++) {
  1117. trb = r->trb + i;
  1118. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1119. dwc3_ep_inc_deq(dep);
  1120. }
  1121. if (r->unaligned || r->zero) {
  1122. trb = r->trb + r->num_pending_sgs + 1;
  1123. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1124. dwc3_ep_inc_deq(dep);
  1125. }
  1126. } else {
  1127. struct dwc3_trb *trb = r->trb;
  1128. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1129. dwc3_ep_inc_deq(dep);
  1130. if (r->unaligned || r->zero) {
  1131. trb = r->trb + 1;
  1132. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1133. dwc3_ep_inc_deq(dep);
  1134. }
  1135. }
  1136. goto out1;
  1137. }
  1138. dev_err(dwc->dev, "request %p was not queued to %s\n",
  1139. request, ep->name);
  1140. ret = -EINVAL;
  1141. goto out0;
  1142. }
  1143. out1:
  1144. /* giveback the request */
  1145. dep->queued_requests--;
  1146. dwc3_gadget_giveback(dep, req, -ECONNRESET);
  1147. out0:
  1148. spin_unlock_irqrestore(&dwc->lock, flags);
  1149. return ret;
  1150. }
  1151. int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
  1152. {
  1153. struct dwc3_gadget_ep_cmd_params params;
  1154. struct dwc3 *dwc = dep->dwc;
  1155. int ret;
  1156. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1157. dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
  1158. return -EINVAL;
  1159. }
  1160. memset(&params, 0x00, sizeof(params));
  1161. if (value) {
  1162. struct dwc3_trb *trb;
  1163. unsigned transfer_in_flight;
  1164. unsigned started;
  1165. if (dep->flags & DWC3_EP_STALL)
  1166. return 0;
  1167. if (dep->number > 1)
  1168. trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
  1169. else
  1170. trb = &dwc->ep0_trb[dep->trb_enqueue];
  1171. transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
  1172. started = !list_empty(&dep->started_list);
  1173. if (!protocol && ((dep->direction && transfer_in_flight) ||
  1174. (!dep->direction && started))) {
  1175. return -EAGAIN;
  1176. }
  1177. ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
  1178. &params);
  1179. if (ret)
  1180. dev_err(dwc->dev, "failed to set STALL on %s\n",
  1181. dep->name);
  1182. else
  1183. dep->flags |= DWC3_EP_STALL;
  1184. } else {
  1185. if (!(dep->flags & DWC3_EP_STALL))
  1186. return 0;
  1187. ret = dwc3_send_clear_stall_ep_cmd(dep);
  1188. if (ret)
  1189. dev_err(dwc->dev, "failed to clear STALL on %s\n",
  1190. dep->name);
  1191. else
  1192. dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
  1193. }
  1194. return ret;
  1195. }
  1196. static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  1197. {
  1198. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1199. struct dwc3 *dwc = dep->dwc;
  1200. unsigned long flags;
  1201. int ret;
  1202. spin_lock_irqsave(&dwc->lock, flags);
  1203. ret = __dwc3_gadget_ep_set_halt(dep, value, false);
  1204. spin_unlock_irqrestore(&dwc->lock, flags);
  1205. return ret;
  1206. }
  1207. static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
  1208. {
  1209. struct dwc3_ep *dep = to_dwc3_ep(ep);
  1210. struct dwc3 *dwc = dep->dwc;
  1211. unsigned long flags;
  1212. int ret;
  1213. spin_lock_irqsave(&dwc->lock, flags);
  1214. dep->flags |= DWC3_EP_WEDGE;
  1215. if (dep->number == 0 || dep->number == 1)
  1216. ret = __dwc3_gadget_ep0_set_halt(ep, 1);
  1217. else
  1218. ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
  1219. spin_unlock_irqrestore(&dwc->lock, flags);
  1220. return ret;
  1221. }
  1222. /* -------------------------------------------------------------------------- */
  1223. static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
  1224. .bLength = USB_DT_ENDPOINT_SIZE,
  1225. .bDescriptorType = USB_DT_ENDPOINT,
  1226. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  1227. };
  1228. static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
  1229. .enable = dwc3_gadget_ep0_enable,
  1230. .disable = dwc3_gadget_ep0_disable,
  1231. .alloc_request = dwc3_gadget_ep_alloc_request,
  1232. .free_request = dwc3_gadget_ep_free_request,
  1233. .queue = dwc3_gadget_ep0_queue,
  1234. .dequeue = dwc3_gadget_ep_dequeue,
  1235. .set_halt = dwc3_gadget_ep0_set_halt,
  1236. .set_wedge = dwc3_gadget_ep_set_wedge,
  1237. };
  1238. static const struct usb_ep_ops dwc3_gadget_ep_ops = {
  1239. .enable = dwc3_gadget_ep_enable,
  1240. .disable = dwc3_gadget_ep_disable,
  1241. .alloc_request = dwc3_gadget_ep_alloc_request,
  1242. .free_request = dwc3_gadget_ep_free_request,
  1243. .queue = dwc3_gadget_ep_queue,
  1244. .dequeue = dwc3_gadget_ep_dequeue,
  1245. .set_halt = dwc3_gadget_ep_set_halt,
  1246. .set_wedge = dwc3_gadget_ep_set_wedge,
  1247. };
  1248. /* -------------------------------------------------------------------------- */
  1249. static int dwc3_gadget_get_frame(struct usb_gadget *g)
  1250. {
  1251. struct dwc3 *dwc = gadget_to_dwc(g);
  1252. return __dwc3_gadget_get_frame(dwc);
  1253. }
  1254. static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
  1255. {
  1256. int retries;
  1257. int ret;
  1258. u32 reg;
  1259. u8 link_state;
  1260. u8 speed;
  1261. /*
  1262. * According to the Databook Remote wakeup request should
  1263. * be issued only when the device is in early suspend state.
  1264. *
  1265. * We can check that via USB Link State bits in DSTS register.
  1266. */
  1267. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1268. speed = reg & DWC3_DSTS_CONNECTSPD;
  1269. if ((speed == DWC3_DSTS_SUPERSPEED) ||
  1270. (speed == DWC3_DSTS_SUPERSPEED_PLUS))
  1271. return 0;
  1272. link_state = DWC3_DSTS_USBLNKST(reg);
  1273. switch (link_state) {
  1274. case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
  1275. case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
  1276. break;
  1277. default:
  1278. return -EINVAL;
  1279. }
  1280. ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
  1281. if (ret < 0) {
  1282. dev_err(dwc->dev, "failed to put link in Recovery\n");
  1283. return ret;
  1284. }
  1285. /* Recent versions do this automatically */
  1286. if (dwc->revision < DWC3_REVISION_194A) {
  1287. /* write zeroes to Link Change Request */
  1288. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1289. reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
  1290. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1291. }
  1292. /* poll until Link State changes to ON */
  1293. retries = 20000;
  1294. while (retries--) {
  1295. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1296. /* in HS, means ON */
  1297. if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
  1298. break;
  1299. }
  1300. if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
  1301. dev_err(dwc->dev, "failed to send remote wakeup\n");
  1302. return -EINVAL;
  1303. }
  1304. return 0;
  1305. }
  1306. static int dwc3_gadget_wakeup(struct usb_gadget *g)
  1307. {
  1308. struct dwc3 *dwc = gadget_to_dwc(g);
  1309. unsigned long flags;
  1310. int ret;
  1311. spin_lock_irqsave(&dwc->lock, flags);
  1312. ret = __dwc3_gadget_wakeup(dwc);
  1313. spin_unlock_irqrestore(&dwc->lock, flags);
  1314. return ret;
  1315. }
  1316. static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
  1317. int is_selfpowered)
  1318. {
  1319. struct dwc3 *dwc = gadget_to_dwc(g);
  1320. unsigned long flags;
  1321. spin_lock_irqsave(&dwc->lock, flags);
  1322. g->is_selfpowered = !!is_selfpowered;
  1323. spin_unlock_irqrestore(&dwc->lock, flags);
  1324. return 0;
  1325. }
  1326. static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
  1327. {
  1328. u32 reg;
  1329. u32 timeout = 500;
  1330. if (pm_runtime_suspended(dwc->dev))
  1331. return 0;
  1332. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1333. if (is_on) {
  1334. if (dwc->revision <= DWC3_REVISION_187A) {
  1335. reg &= ~DWC3_DCTL_TRGTULST_MASK;
  1336. reg |= DWC3_DCTL_TRGTULST_RX_DET;
  1337. }
  1338. if (dwc->revision >= DWC3_REVISION_194A)
  1339. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1340. reg |= DWC3_DCTL_RUN_STOP;
  1341. if (dwc->has_hibernation)
  1342. reg |= DWC3_DCTL_KEEP_CONNECT;
  1343. dwc->pullups_connected = true;
  1344. } else {
  1345. reg &= ~DWC3_DCTL_RUN_STOP;
  1346. if (dwc->has_hibernation && !suspend)
  1347. reg &= ~DWC3_DCTL_KEEP_CONNECT;
  1348. dwc->pullups_connected = false;
  1349. }
  1350. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1351. do {
  1352. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  1353. reg &= DWC3_DSTS_DEVCTRLHLT;
  1354. } while (--timeout && !(!is_on ^ !reg));
  1355. if (!timeout)
  1356. return -ETIMEDOUT;
  1357. return 0;
  1358. }
  1359. static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
  1360. {
  1361. struct dwc3 *dwc = gadget_to_dwc(g);
  1362. unsigned long flags;
  1363. int ret;
  1364. is_on = !!is_on;
  1365. /*
  1366. * Per databook, when we want to stop the gadget, if a control transfer
  1367. * is still in process, complete it and get the core into setup phase.
  1368. */
  1369. if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
  1370. reinit_completion(&dwc->ep0_in_setup);
  1371. ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
  1372. msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
  1373. if (ret == 0) {
  1374. dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
  1375. return -ETIMEDOUT;
  1376. }
  1377. }
  1378. spin_lock_irqsave(&dwc->lock, flags);
  1379. ret = dwc3_gadget_run_stop(dwc, is_on, false);
  1380. spin_unlock_irqrestore(&dwc->lock, flags);
  1381. return ret;
  1382. }
  1383. static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
  1384. {
  1385. u32 reg;
  1386. /* Enable all but Start and End of Frame IRQs */
  1387. reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
  1388. DWC3_DEVTEN_EVNTOVERFLOWEN |
  1389. DWC3_DEVTEN_CMDCMPLTEN |
  1390. DWC3_DEVTEN_ERRTICERREN |
  1391. DWC3_DEVTEN_WKUPEVTEN |
  1392. DWC3_DEVTEN_CONNECTDONEEN |
  1393. DWC3_DEVTEN_USBRSTEN |
  1394. DWC3_DEVTEN_DISCONNEVTEN);
  1395. if (dwc->revision < DWC3_REVISION_250A)
  1396. reg |= DWC3_DEVTEN_ULSTCNGEN;
  1397. dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
  1398. }
  1399. static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
  1400. {
  1401. /* mask all interrupts */
  1402. dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
  1403. }
  1404. static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
  1405. static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
  1406. /**
  1407. * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
  1408. * dwc: pointer to our context structure
  1409. *
  1410. * The following looks like complex but it's actually very simple. In order to
  1411. * calculate the number of packets we can burst at once on OUT transfers, we're
  1412. * gonna use RxFIFO size.
  1413. *
  1414. * To calculate RxFIFO size we need two numbers:
  1415. * MDWIDTH = size, in bits, of the internal memory bus
  1416. * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
  1417. *
  1418. * Given these two numbers, the formula is simple:
  1419. *
  1420. * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
  1421. *
  1422. * 24 bytes is for 3x SETUP packets
  1423. * 16 bytes is a clock domain crossing tolerance
  1424. *
  1425. * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
  1426. */
  1427. static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
  1428. {
  1429. u32 ram2_depth;
  1430. u32 mdwidth;
  1431. u32 nump;
  1432. u32 reg;
  1433. ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
  1434. mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
  1435. nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
  1436. nump = min_t(u32, nump, 16);
  1437. /* update NumP */
  1438. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1439. reg &= ~DWC3_DCFG_NUMP_MASK;
  1440. reg |= nump << DWC3_DCFG_NUMP_SHIFT;
  1441. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1442. }
  1443. static int __dwc3_gadget_start(struct dwc3 *dwc)
  1444. {
  1445. struct dwc3_ep *dep;
  1446. int ret = 0;
  1447. u32 reg;
  1448. /*
  1449. * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
  1450. * the core supports IMOD, disable it.
  1451. */
  1452. if (dwc->imod_interval) {
  1453. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  1454. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  1455. } else if (dwc3_has_imod(dwc)) {
  1456. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
  1457. }
  1458. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  1459. reg &= ~(DWC3_DCFG_SPEED_MASK);
  1460. /**
  1461. * WORKAROUND: DWC3 revision < 2.20a have an issue
  1462. * which would cause metastability state on Run/Stop
  1463. * bit if we try to force the IP to USB2-only mode.
  1464. *
  1465. * Because of that, we cannot configure the IP to any
  1466. * speed other than the SuperSpeed
  1467. *
  1468. * Refers to:
  1469. *
  1470. * STAR#9000525659: Clock Domain Crossing on DCTL in
  1471. * USB 2.0 Mode
  1472. */
  1473. if (dwc->revision < DWC3_REVISION_220A) {
  1474. reg |= DWC3_DCFG_SUPERSPEED;
  1475. } else {
  1476. switch (dwc->maximum_speed) {
  1477. case USB_SPEED_LOW:
  1478. reg |= DWC3_DCFG_LOWSPEED;
  1479. break;
  1480. case USB_SPEED_FULL:
  1481. reg |= DWC3_DCFG_FULLSPEED;
  1482. break;
  1483. case USB_SPEED_HIGH:
  1484. reg |= DWC3_DCFG_HIGHSPEED;
  1485. break;
  1486. case USB_SPEED_SUPER_PLUS:
  1487. reg |= DWC3_DCFG_SUPERSPEED_PLUS;
  1488. break;
  1489. default:
  1490. dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
  1491. dwc->maximum_speed);
  1492. /* fall through */
  1493. case USB_SPEED_SUPER:
  1494. reg |= DWC3_DCFG_SUPERSPEED;
  1495. break;
  1496. }
  1497. }
  1498. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  1499. /*
  1500. * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
  1501. * field instead of letting dwc3 itself calculate that automatically.
  1502. *
  1503. * This way, we maximize the chances that we'll be able to get several
  1504. * bursts of data without going through any sort of endpoint throttling.
  1505. */
  1506. reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
  1507. reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
  1508. dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
  1509. dwc3_gadget_setup_nump(dwc);
  1510. /* Start with SuperSpeed Default */
  1511. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  1512. dep = dwc->eps[0];
  1513. ret = __dwc3_gadget_ep_enable(dep, false, false);
  1514. if (ret) {
  1515. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1516. goto err0;
  1517. }
  1518. dep = dwc->eps[1];
  1519. ret = __dwc3_gadget_ep_enable(dep, false, false);
  1520. if (ret) {
  1521. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  1522. goto err1;
  1523. }
  1524. /* begin to receive SETUP packets */
  1525. dwc->ep0state = EP0_SETUP_PHASE;
  1526. dwc3_ep0_out_start(dwc);
  1527. dwc3_gadget_enable_irq(dwc);
  1528. return 0;
  1529. err1:
  1530. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1531. err0:
  1532. return ret;
  1533. }
  1534. static int dwc3_gadget_start(struct usb_gadget *g,
  1535. struct usb_gadget_driver *driver)
  1536. {
  1537. struct dwc3 *dwc = gadget_to_dwc(g);
  1538. unsigned long flags;
  1539. int ret = 0;
  1540. int irq;
  1541. irq = dwc->irq_gadget;
  1542. ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
  1543. IRQF_SHARED, "dwc3", dwc->ev_buf);
  1544. if (ret) {
  1545. dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
  1546. irq, ret);
  1547. goto err0;
  1548. }
  1549. spin_lock_irqsave(&dwc->lock, flags);
  1550. if (dwc->gadget_driver) {
  1551. dev_err(dwc->dev, "%s is already bound to %s\n",
  1552. dwc->gadget.name,
  1553. dwc->gadget_driver->driver.name);
  1554. ret = -EBUSY;
  1555. goto err1;
  1556. }
  1557. dwc->gadget_driver = driver;
  1558. if (pm_runtime_active(dwc->dev))
  1559. __dwc3_gadget_start(dwc);
  1560. spin_unlock_irqrestore(&dwc->lock, flags);
  1561. return 0;
  1562. err1:
  1563. spin_unlock_irqrestore(&dwc->lock, flags);
  1564. free_irq(irq, dwc);
  1565. err0:
  1566. return ret;
  1567. }
  1568. static void __dwc3_gadget_stop(struct dwc3 *dwc)
  1569. {
  1570. dwc3_gadget_disable_irq(dwc);
  1571. __dwc3_gadget_ep_disable(dwc->eps[0]);
  1572. __dwc3_gadget_ep_disable(dwc->eps[1]);
  1573. }
  1574. static int dwc3_gadget_stop(struct usb_gadget *g)
  1575. {
  1576. struct dwc3 *dwc = gadget_to_dwc(g);
  1577. unsigned long flags;
  1578. int epnum;
  1579. spin_lock_irqsave(&dwc->lock, flags);
  1580. if (pm_runtime_suspended(dwc->dev))
  1581. goto out;
  1582. __dwc3_gadget_stop(dwc);
  1583. for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1584. struct dwc3_ep *dep = dwc->eps[epnum];
  1585. if (!dep)
  1586. continue;
  1587. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  1588. continue;
  1589. wait_event_lock_irq(dep->wait_end_transfer,
  1590. !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
  1591. dwc->lock);
  1592. }
  1593. out:
  1594. dwc->gadget_driver = NULL;
  1595. spin_unlock_irqrestore(&dwc->lock, flags);
  1596. free_irq(dwc->irq_gadget, dwc->ev_buf);
  1597. return 0;
  1598. }
  1599. static const struct usb_gadget_ops dwc3_gadget_ops = {
  1600. .get_frame = dwc3_gadget_get_frame,
  1601. .wakeup = dwc3_gadget_wakeup,
  1602. .set_selfpowered = dwc3_gadget_set_selfpowered,
  1603. .pullup = dwc3_gadget_pullup,
  1604. .udc_start = dwc3_gadget_start,
  1605. .udc_stop = dwc3_gadget_stop,
  1606. };
  1607. /* -------------------------------------------------------------------------- */
  1608. static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 num)
  1609. {
  1610. struct dwc3_ep *dep;
  1611. u8 epnum;
  1612. INIT_LIST_HEAD(&dwc->gadget.ep_list);
  1613. for (epnum = 0; epnum < num; epnum++) {
  1614. bool direction = epnum & 1;
  1615. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  1616. if (!dep)
  1617. return -ENOMEM;
  1618. dep->dwc = dwc;
  1619. dep->number = epnum;
  1620. dep->direction = direction;
  1621. dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
  1622. dwc->eps[epnum] = dep;
  1623. snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
  1624. direction ? "in" : "out");
  1625. dep->endpoint.name = dep->name;
  1626. if (!(dep->number > 1)) {
  1627. dep->endpoint.desc = &dwc3_gadget_ep0_desc;
  1628. dep->endpoint.comp_desc = NULL;
  1629. }
  1630. spin_lock_init(&dep->lock);
  1631. if (epnum == 0 || epnum == 1) {
  1632. usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
  1633. dep->endpoint.maxburst = 1;
  1634. dep->endpoint.ops = &dwc3_gadget_ep0_ops;
  1635. if (!epnum)
  1636. dwc->gadget.ep0 = &dep->endpoint;
  1637. } else if (direction) {
  1638. int mdwidth;
  1639. int size;
  1640. int ret;
  1641. int num;
  1642. mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
  1643. /* MDWIDTH is represented in bits, we need it in bytes */
  1644. mdwidth /= 8;
  1645. size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(epnum >> 1));
  1646. size = DWC3_GTXFIFOSIZ_TXFDEF(size);
  1647. /* FIFO Depth is in MDWDITH bytes. Multiply */
  1648. size *= mdwidth;
  1649. num = size / 1024;
  1650. if (num == 0)
  1651. num = 1;
  1652. /*
  1653. * FIFO sizes account an extra MDWIDTH * (num + 1) bytes for
  1654. * internal overhead. We don't really know how these are used,
  1655. * but documentation say it exists.
  1656. */
  1657. size -= mdwidth * (num + 1);
  1658. size /= num;
  1659. usb_ep_set_maxpacket_limit(&dep->endpoint, size);
  1660. dep->endpoint.max_streams = 15;
  1661. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1662. list_add_tail(&dep->endpoint.ep_list,
  1663. &dwc->gadget.ep_list);
  1664. ret = dwc3_alloc_trb_pool(dep);
  1665. if (ret)
  1666. return ret;
  1667. } else {
  1668. int ret;
  1669. usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
  1670. dep->endpoint.max_streams = 15;
  1671. dep->endpoint.ops = &dwc3_gadget_ep_ops;
  1672. list_add_tail(&dep->endpoint.ep_list,
  1673. &dwc->gadget.ep_list);
  1674. ret = dwc3_alloc_trb_pool(dep);
  1675. if (ret)
  1676. return ret;
  1677. }
  1678. if (epnum == 0 || epnum == 1) {
  1679. dep->endpoint.caps.type_control = true;
  1680. } else {
  1681. dep->endpoint.caps.type_iso = true;
  1682. dep->endpoint.caps.type_bulk = true;
  1683. dep->endpoint.caps.type_int = true;
  1684. }
  1685. dep->endpoint.caps.dir_in = direction;
  1686. dep->endpoint.caps.dir_out = !direction;
  1687. INIT_LIST_HEAD(&dep->pending_list);
  1688. INIT_LIST_HEAD(&dep->started_list);
  1689. }
  1690. return 0;
  1691. }
  1692. static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
  1693. {
  1694. struct dwc3_ep *dep;
  1695. u8 epnum;
  1696. for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  1697. dep = dwc->eps[epnum];
  1698. if (!dep)
  1699. continue;
  1700. /*
  1701. * Physical endpoints 0 and 1 are special; they form the
  1702. * bi-directional USB endpoint 0.
  1703. *
  1704. * For those two physical endpoints, we don't allocate a TRB
  1705. * pool nor do we add them the endpoints list. Due to that, we
  1706. * shouldn't do these two operations otherwise we would end up
  1707. * with all sorts of bugs when removing dwc3.ko.
  1708. */
  1709. if (epnum != 0 && epnum != 1) {
  1710. dwc3_free_trb_pool(dep);
  1711. list_del(&dep->endpoint.ep_list);
  1712. }
  1713. kfree(dep);
  1714. }
  1715. }
  1716. /* -------------------------------------------------------------------------- */
  1717. static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1718. struct dwc3_request *req, struct dwc3_trb *trb,
  1719. const struct dwc3_event_depevt *event, int status,
  1720. int chain)
  1721. {
  1722. unsigned int count;
  1723. unsigned int s_pkt = 0;
  1724. unsigned int trb_status;
  1725. dwc3_ep_inc_deq(dep);
  1726. if (req->trb == trb)
  1727. dep->queued_requests--;
  1728. trace_dwc3_complete_trb(dep, trb);
  1729. /*
  1730. * If we're in the middle of series of chained TRBs and we
  1731. * receive a short transfer along the way, DWC3 will skip
  1732. * through all TRBs including the last TRB in the chain (the
  1733. * where CHN bit is zero. DWC3 will also avoid clearing HWO
  1734. * bit and SW has to do it manually.
  1735. *
  1736. * We're going to do that here to avoid problems of HW trying
  1737. * to use bogus TRBs for transfers.
  1738. */
  1739. if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
  1740. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1741. /*
  1742. * If we're dealing with unaligned size OUT transfer, we will be left
  1743. * with one TRB pending in the ring. We need to manually clear HWO bit
  1744. * from that TRB.
  1745. */
  1746. if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
  1747. trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
  1748. return 1;
  1749. }
  1750. count = trb->size & DWC3_TRB_SIZE_MASK;
  1751. req->remaining += count;
  1752. if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
  1753. return 1;
  1754. if (dep->direction) {
  1755. if (count) {
  1756. trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
  1757. if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
  1758. /*
  1759. * If missed isoc occurred and there is
  1760. * no request queued then issue END
  1761. * TRANSFER, so that core generates
  1762. * next xfernotready and we will issue
  1763. * a fresh START TRANSFER.
  1764. * If there are still queued request
  1765. * then wait, do not issue either END
  1766. * or UPDATE TRANSFER, just attach next
  1767. * request in pending_list during
  1768. * giveback.If any future queued request
  1769. * is successfully transferred then we
  1770. * will issue UPDATE TRANSFER for all
  1771. * request in the pending_list.
  1772. */
  1773. dep->flags |= DWC3_EP_MISSED_ISOC;
  1774. } else {
  1775. dev_err(dwc->dev, "incomplete IN transfer %s\n",
  1776. dep->name);
  1777. status = -ECONNRESET;
  1778. }
  1779. } else {
  1780. dep->flags &= ~DWC3_EP_MISSED_ISOC;
  1781. }
  1782. } else {
  1783. if (count && (event->status & DEPEVT_STATUS_SHORT))
  1784. s_pkt = 1;
  1785. }
  1786. if (s_pkt && !chain)
  1787. return 1;
  1788. if ((event->status & DEPEVT_STATUS_IOC) &&
  1789. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1790. return 1;
  1791. return 0;
  1792. }
  1793. static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
  1794. const struct dwc3_event_depevt *event, int status)
  1795. {
  1796. struct dwc3_request *req, *n;
  1797. struct dwc3_trb *trb;
  1798. bool ioc = false;
  1799. int ret = 0;
  1800. list_for_each_entry_safe(req, n, &dep->started_list, list) {
  1801. unsigned length;
  1802. int chain;
  1803. length = req->request.length;
  1804. chain = req->num_pending_sgs > 0;
  1805. if (chain) {
  1806. struct scatterlist *sg = req->sg;
  1807. struct scatterlist *s;
  1808. unsigned int pending = req->num_pending_sgs;
  1809. unsigned int i;
  1810. for_each_sg(sg, s, pending, i) {
  1811. trb = &dep->trb_pool[dep->trb_dequeue];
  1812. if (trb->ctrl & DWC3_TRB_CTRL_HWO)
  1813. break;
  1814. req->sg = sg_next(s);
  1815. req->num_pending_sgs--;
  1816. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1817. event, status, chain);
  1818. if (ret)
  1819. break;
  1820. }
  1821. } else {
  1822. trb = &dep->trb_pool[dep->trb_dequeue];
  1823. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1824. event, status, chain);
  1825. }
  1826. if (req->unaligned || req->zero) {
  1827. trb = &dep->trb_pool[dep->trb_dequeue];
  1828. ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
  1829. event, status, false);
  1830. req->unaligned = false;
  1831. req->zero = false;
  1832. }
  1833. req->request.actual = length - req->remaining;
  1834. if ((req->request.actual < length) && req->num_pending_sgs)
  1835. return __dwc3_gadget_kick_transfer(dep, 0);
  1836. dwc3_gadget_giveback(dep, req, status);
  1837. if (ret) {
  1838. if ((event->status & DEPEVT_STATUS_IOC) &&
  1839. (trb->ctrl & DWC3_TRB_CTRL_IOC))
  1840. ioc = true;
  1841. break;
  1842. }
  1843. }
  1844. /*
  1845. * Our endpoint might get disabled by another thread during
  1846. * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
  1847. * early on so DWC3_EP_BUSY flag gets cleared
  1848. */
  1849. if (!dep->endpoint.desc)
  1850. return 1;
  1851. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
  1852. list_empty(&dep->started_list)) {
  1853. if (list_empty(&dep->pending_list)) {
  1854. /*
  1855. * If there is no entry in request list then do
  1856. * not issue END TRANSFER now. Just set PENDING
  1857. * flag, so that END TRANSFER is issued when an
  1858. * entry is added into request list.
  1859. */
  1860. dep->flags = DWC3_EP_PENDING_REQUEST;
  1861. } else {
  1862. dwc3_stop_active_transfer(dwc, dep->number, true);
  1863. dep->flags = DWC3_EP_ENABLED;
  1864. }
  1865. return 1;
  1866. }
  1867. if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
  1868. return 0;
  1869. return 1;
  1870. }
  1871. static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
  1872. struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
  1873. {
  1874. unsigned status = 0;
  1875. int clean_busy;
  1876. u32 is_xfer_complete;
  1877. is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
  1878. if (event->status & DEPEVT_STATUS_BUSERR)
  1879. status = -ECONNRESET;
  1880. clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
  1881. if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
  1882. usb_endpoint_xfer_isoc(dep->endpoint.desc)))
  1883. dep->flags &= ~DWC3_EP_BUSY;
  1884. /*
  1885. * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
  1886. * See dwc3_gadget_linksts_change_interrupt() for 1st half.
  1887. */
  1888. if (dwc->revision < DWC3_REVISION_183A) {
  1889. u32 reg;
  1890. int i;
  1891. for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
  1892. dep = dwc->eps[i];
  1893. if (!(dep->flags & DWC3_EP_ENABLED))
  1894. continue;
  1895. if (!list_empty(&dep->started_list))
  1896. return;
  1897. }
  1898. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  1899. reg |= dwc->u1u2;
  1900. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  1901. dwc->u1u2 = 0;
  1902. }
  1903. /*
  1904. * Our endpoint might get disabled by another thread during
  1905. * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
  1906. * early on so DWC3_EP_BUSY flag gets cleared
  1907. */
  1908. if (!dep->endpoint.desc)
  1909. return;
  1910. if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1911. int ret;
  1912. ret = __dwc3_gadget_kick_transfer(dep, 0);
  1913. if (!ret || ret == -EBUSY)
  1914. return;
  1915. }
  1916. }
  1917. static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
  1918. const struct dwc3_event_depevt *event)
  1919. {
  1920. struct dwc3_ep *dep;
  1921. u8 epnum = event->endpoint_number;
  1922. u8 cmd;
  1923. dep = dwc->eps[epnum];
  1924. if (!(dep->flags & DWC3_EP_ENABLED)) {
  1925. if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
  1926. return;
  1927. /* Handle only EPCMDCMPLT when EP disabled */
  1928. if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
  1929. return;
  1930. }
  1931. if (epnum == 0 || epnum == 1) {
  1932. dwc3_ep0_interrupt(dwc, event);
  1933. return;
  1934. }
  1935. switch (event->endpoint_event) {
  1936. case DWC3_DEPEVT_XFERCOMPLETE:
  1937. dep->resource_index = 0;
  1938. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1939. dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
  1940. return;
  1941. }
  1942. dwc3_endpoint_transfer_complete(dwc, dep, event);
  1943. break;
  1944. case DWC3_DEPEVT_XFERINPROGRESS:
  1945. dwc3_endpoint_transfer_complete(dwc, dep, event);
  1946. break;
  1947. case DWC3_DEPEVT_XFERNOTREADY:
  1948. if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
  1949. dwc3_gadget_start_isoc(dwc, dep, event);
  1950. } else {
  1951. int ret;
  1952. ret = __dwc3_gadget_kick_transfer(dep, 0);
  1953. if (!ret || ret == -EBUSY)
  1954. return;
  1955. }
  1956. break;
  1957. case DWC3_DEPEVT_STREAMEVT:
  1958. if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
  1959. dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
  1960. dep->name);
  1961. return;
  1962. }
  1963. break;
  1964. case DWC3_DEPEVT_EPCMDCMPLT:
  1965. cmd = DEPEVT_PARAMETER_CMD(event->parameters);
  1966. if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
  1967. dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  1968. wake_up(&dep->wait_end_transfer);
  1969. }
  1970. break;
  1971. case DWC3_DEPEVT_RXTXFIFOEVT:
  1972. break;
  1973. }
  1974. }
  1975. static void dwc3_disconnect_gadget(struct dwc3 *dwc)
  1976. {
  1977. if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
  1978. spin_unlock(&dwc->lock);
  1979. dwc->gadget_driver->disconnect(&dwc->gadget);
  1980. spin_lock(&dwc->lock);
  1981. }
  1982. }
  1983. static void dwc3_suspend_gadget(struct dwc3 *dwc)
  1984. {
  1985. if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
  1986. spin_unlock(&dwc->lock);
  1987. dwc->gadget_driver->suspend(&dwc->gadget);
  1988. spin_lock(&dwc->lock);
  1989. }
  1990. }
  1991. static void dwc3_resume_gadget(struct dwc3 *dwc)
  1992. {
  1993. if (dwc->gadget_driver && dwc->gadget_driver->resume) {
  1994. spin_unlock(&dwc->lock);
  1995. dwc->gadget_driver->resume(&dwc->gadget);
  1996. spin_lock(&dwc->lock);
  1997. }
  1998. }
  1999. static void dwc3_reset_gadget(struct dwc3 *dwc)
  2000. {
  2001. if (!dwc->gadget_driver)
  2002. return;
  2003. if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
  2004. spin_unlock(&dwc->lock);
  2005. usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
  2006. spin_lock(&dwc->lock);
  2007. }
  2008. }
  2009. static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
  2010. {
  2011. struct dwc3_ep *dep;
  2012. struct dwc3_gadget_ep_cmd_params params;
  2013. u32 cmd;
  2014. int ret;
  2015. dep = dwc->eps[epnum];
  2016. if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
  2017. !dep->resource_index)
  2018. return;
  2019. /*
  2020. * NOTICE: We are violating what the Databook says about the
  2021. * EndTransfer command. Ideally we would _always_ wait for the
  2022. * EndTransfer Command Completion IRQ, but that's causing too
  2023. * much trouble synchronizing between us and gadget driver.
  2024. *
  2025. * We have discussed this with the IP Provider and it was
  2026. * suggested to giveback all requests here, but give HW some
  2027. * extra time to synchronize with the interconnect. We're using
  2028. * an arbitrary 100us delay for that.
  2029. *
  2030. * Note also that a similar handling was tested by Synopsys
  2031. * (thanks a lot Paul) and nothing bad has come out of it.
  2032. * In short, what we're doing is:
  2033. *
  2034. * - Issue EndTransfer WITH CMDIOC bit set
  2035. * - Wait 100us
  2036. *
  2037. * As of IP version 3.10a of the DWC_usb3 IP, the controller
  2038. * supports a mode to work around the above limitation. The
  2039. * software can poll the CMDACT bit in the DEPCMD register
  2040. * after issuing a EndTransfer command. This mode is enabled
  2041. * by writing GUCTL2[14]. This polling is already done in the
  2042. * dwc3_send_gadget_ep_cmd() function so if the mode is
  2043. * enabled, the EndTransfer command will have completed upon
  2044. * returning from this function and we don't need to delay for
  2045. * 100us.
  2046. *
  2047. * This mode is NOT available on the DWC_usb31 IP.
  2048. */
  2049. cmd = DWC3_DEPCMD_ENDTRANSFER;
  2050. cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
  2051. cmd |= DWC3_DEPCMD_CMDIOC;
  2052. cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
  2053. memset(&params, 0, sizeof(params));
  2054. ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
  2055. WARN_ON_ONCE(ret);
  2056. dep->resource_index = 0;
  2057. dep->flags &= ~DWC3_EP_BUSY;
  2058. if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
  2059. dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
  2060. udelay(100);
  2061. }
  2062. }
  2063. static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
  2064. {
  2065. u32 epnum;
  2066. for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  2067. struct dwc3_ep *dep;
  2068. int ret;
  2069. dep = dwc->eps[epnum];
  2070. if (!dep)
  2071. continue;
  2072. if (!(dep->flags & DWC3_EP_STALL))
  2073. continue;
  2074. dep->flags &= ~DWC3_EP_STALL;
  2075. ret = dwc3_send_clear_stall_ep_cmd(dep);
  2076. WARN_ON_ONCE(ret);
  2077. }
  2078. }
  2079. static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
  2080. {
  2081. int reg;
  2082. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2083. reg &= ~DWC3_DCTL_INITU1ENA;
  2084. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2085. reg &= ~DWC3_DCTL_INITU2ENA;
  2086. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2087. dwc3_disconnect_gadget(dwc);
  2088. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2089. dwc->setup_packet_pending = false;
  2090. usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
  2091. dwc->connected = false;
  2092. }
  2093. static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
  2094. {
  2095. u32 reg;
  2096. dwc->connected = true;
  2097. /*
  2098. * WORKAROUND: DWC3 revisions <1.88a have an issue which
  2099. * would cause a missing Disconnect Event if there's a
  2100. * pending Setup Packet in the FIFO.
  2101. *
  2102. * There's no suggested workaround on the official Bug
  2103. * report, which states that "unless the driver/application
  2104. * is doing any special handling of a disconnect event,
  2105. * there is no functional issue".
  2106. *
  2107. * Unfortunately, it turns out that we _do_ some special
  2108. * handling of a disconnect event, namely complete all
  2109. * pending transfers, notify gadget driver of the
  2110. * disconnection, and so on.
  2111. *
  2112. * Our suggested workaround is to follow the Disconnect
  2113. * Event steps here, instead, based on a setup_packet_pending
  2114. * flag. Such flag gets set whenever we have a SETUP_PENDING
  2115. * status for EP0 TRBs and gets cleared on XferComplete for the
  2116. * same endpoint.
  2117. *
  2118. * Refers to:
  2119. *
  2120. * STAR#9000466709: RTL: Device : Disconnect event not
  2121. * generated if setup packet pending in FIFO
  2122. */
  2123. if (dwc->revision < DWC3_REVISION_188A) {
  2124. if (dwc->setup_packet_pending)
  2125. dwc3_gadget_disconnect_interrupt(dwc);
  2126. }
  2127. dwc3_reset_gadget(dwc);
  2128. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2129. reg &= ~DWC3_DCTL_TSTCTRL_MASK;
  2130. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2131. dwc->test_mode = false;
  2132. dwc3_clear_stall_all_ep(dwc);
  2133. /* Reset device address to zero */
  2134. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2135. reg &= ~(DWC3_DCFG_DEVADDR_MASK);
  2136. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2137. }
  2138. static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
  2139. {
  2140. struct dwc3_ep *dep;
  2141. int ret;
  2142. u32 reg;
  2143. u8 speed;
  2144. reg = dwc3_readl(dwc->regs, DWC3_DSTS);
  2145. speed = reg & DWC3_DSTS_CONNECTSPD;
  2146. dwc->speed = speed;
  2147. /*
  2148. * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
  2149. * each time on Connect Done.
  2150. *
  2151. * Currently we always use the reset value. If any platform
  2152. * wants to set this to a different value, we need to add a
  2153. * setting and update GCTL.RAMCLKSEL here.
  2154. */
  2155. switch (speed) {
  2156. case DWC3_DSTS_SUPERSPEED_PLUS:
  2157. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2158. dwc->gadget.ep0->maxpacket = 512;
  2159. dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
  2160. break;
  2161. case DWC3_DSTS_SUPERSPEED:
  2162. /*
  2163. * WORKAROUND: DWC3 revisions <1.90a have an issue which
  2164. * would cause a missing USB3 Reset event.
  2165. *
  2166. * In such situations, we should force a USB3 Reset
  2167. * event by calling our dwc3_gadget_reset_interrupt()
  2168. * routine.
  2169. *
  2170. * Refers to:
  2171. *
  2172. * STAR#9000483510: RTL: SS : USB3 reset event may
  2173. * not be generated always when the link enters poll
  2174. */
  2175. if (dwc->revision < DWC3_REVISION_190A)
  2176. dwc3_gadget_reset_interrupt(dwc);
  2177. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
  2178. dwc->gadget.ep0->maxpacket = 512;
  2179. dwc->gadget.speed = USB_SPEED_SUPER;
  2180. break;
  2181. case DWC3_DSTS_HIGHSPEED:
  2182. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2183. dwc->gadget.ep0->maxpacket = 64;
  2184. dwc->gadget.speed = USB_SPEED_HIGH;
  2185. break;
  2186. case DWC3_DSTS_FULLSPEED:
  2187. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
  2188. dwc->gadget.ep0->maxpacket = 64;
  2189. dwc->gadget.speed = USB_SPEED_FULL;
  2190. break;
  2191. case DWC3_DSTS_LOWSPEED:
  2192. dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
  2193. dwc->gadget.ep0->maxpacket = 8;
  2194. dwc->gadget.speed = USB_SPEED_LOW;
  2195. break;
  2196. }
  2197. /* Enable USB2 LPM Capability */
  2198. if ((dwc->revision > DWC3_REVISION_194A) &&
  2199. (speed != DWC3_DSTS_SUPERSPEED) &&
  2200. (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
  2201. reg = dwc3_readl(dwc->regs, DWC3_DCFG);
  2202. reg |= DWC3_DCFG_LPM_CAP;
  2203. dwc3_writel(dwc->regs, DWC3_DCFG, reg);
  2204. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2205. reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
  2206. reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
  2207. /*
  2208. * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
  2209. * DCFG.LPMCap is set, core responses with an ACK and the
  2210. * BESL value in the LPM token is less than or equal to LPM
  2211. * NYET threshold.
  2212. */
  2213. WARN_ONCE(dwc->revision < DWC3_REVISION_240A
  2214. && dwc->has_lpm_erratum,
  2215. "LPM Erratum not available on dwc3 revisions < 2.40a\n");
  2216. if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
  2217. reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
  2218. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2219. } else {
  2220. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2221. reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
  2222. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2223. }
  2224. dep = dwc->eps[0];
  2225. ret = __dwc3_gadget_ep_enable(dep, true, false);
  2226. if (ret) {
  2227. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2228. return;
  2229. }
  2230. dep = dwc->eps[1];
  2231. ret = __dwc3_gadget_ep_enable(dep, true, false);
  2232. if (ret) {
  2233. dev_err(dwc->dev, "failed to enable %s\n", dep->name);
  2234. return;
  2235. }
  2236. /*
  2237. * Configure PHY via GUSB3PIPECTLn if required.
  2238. *
  2239. * Update GTXFIFOSIZn
  2240. *
  2241. * In both cases reset values should be sufficient.
  2242. */
  2243. }
  2244. static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
  2245. {
  2246. /*
  2247. * TODO take core out of low power mode when that's
  2248. * implemented.
  2249. */
  2250. if (dwc->gadget_driver && dwc->gadget_driver->resume) {
  2251. spin_unlock(&dwc->lock);
  2252. dwc->gadget_driver->resume(&dwc->gadget);
  2253. spin_lock(&dwc->lock);
  2254. }
  2255. }
  2256. static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
  2257. unsigned int evtinfo)
  2258. {
  2259. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2260. unsigned int pwropt;
  2261. /*
  2262. * WORKAROUND: DWC3 < 2.50a have an issue when configured without
  2263. * Hibernation mode enabled which would show up when device detects
  2264. * host-initiated U3 exit.
  2265. *
  2266. * In that case, device will generate a Link State Change Interrupt
  2267. * from U3 to RESUME which is only necessary if Hibernation is
  2268. * configured in.
  2269. *
  2270. * There are no functional changes due to such spurious event and we
  2271. * just need to ignore it.
  2272. *
  2273. * Refers to:
  2274. *
  2275. * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
  2276. * operational mode
  2277. */
  2278. pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
  2279. if ((dwc->revision < DWC3_REVISION_250A) &&
  2280. (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
  2281. if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
  2282. (next == DWC3_LINK_STATE_RESUME)) {
  2283. return;
  2284. }
  2285. }
  2286. /*
  2287. * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
  2288. * on the link partner, the USB session might do multiple entry/exit
  2289. * of low power states before a transfer takes place.
  2290. *
  2291. * Due to this problem, we might experience lower throughput. The
  2292. * suggested workaround is to disable DCTL[12:9] bits if we're
  2293. * transitioning from U1/U2 to U0 and enable those bits again
  2294. * after a transfer completes and there are no pending transfers
  2295. * on any of the enabled endpoints.
  2296. *
  2297. * This is the first half of that workaround.
  2298. *
  2299. * Refers to:
  2300. *
  2301. * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
  2302. * core send LGO_Ux entering U0
  2303. */
  2304. if (dwc->revision < DWC3_REVISION_183A) {
  2305. if (next == DWC3_LINK_STATE_U0) {
  2306. u32 u1u2;
  2307. u32 reg;
  2308. switch (dwc->link_state) {
  2309. case DWC3_LINK_STATE_U1:
  2310. case DWC3_LINK_STATE_U2:
  2311. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  2312. u1u2 = reg & (DWC3_DCTL_INITU2ENA
  2313. | DWC3_DCTL_ACCEPTU2ENA
  2314. | DWC3_DCTL_INITU1ENA
  2315. | DWC3_DCTL_ACCEPTU1ENA);
  2316. if (!dwc->u1u2)
  2317. dwc->u1u2 = reg & u1u2;
  2318. reg &= ~u1u2;
  2319. dwc3_writel(dwc->regs, DWC3_DCTL, reg);
  2320. break;
  2321. default:
  2322. /* do nothing */
  2323. break;
  2324. }
  2325. }
  2326. }
  2327. switch (next) {
  2328. case DWC3_LINK_STATE_U1:
  2329. if (dwc->speed == USB_SPEED_SUPER)
  2330. dwc3_suspend_gadget(dwc);
  2331. break;
  2332. case DWC3_LINK_STATE_U2:
  2333. case DWC3_LINK_STATE_U3:
  2334. dwc3_suspend_gadget(dwc);
  2335. break;
  2336. case DWC3_LINK_STATE_RESUME:
  2337. dwc3_resume_gadget(dwc);
  2338. break;
  2339. default:
  2340. /* do nothing */
  2341. break;
  2342. }
  2343. dwc->link_state = next;
  2344. }
  2345. static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
  2346. unsigned int evtinfo)
  2347. {
  2348. enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
  2349. if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
  2350. dwc3_suspend_gadget(dwc);
  2351. dwc->link_state = next;
  2352. }
  2353. static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
  2354. unsigned int evtinfo)
  2355. {
  2356. unsigned int is_ss = evtinfo & BIT(4);
  2357. /**
  2358. * WORKAROUND: DWC3 revison 2.20a with hibernation support
  2359. * have a known issue which can cause USB CV TD.9.23 to fail
  2360. * randomly.
  2361. *
  2362. * Because of this issue, core could generate bogus hibernation
  2363. * events which SW needs to ignore.
  2364. *
  2365. * Refers to:
  2366. *
  2367. * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
  2368. * Device Fallback from SuperSpeed
  2369. */
  2370. if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
  2371. return;
  2372. /* enter hibernation here */
  2373. }
  2374. static void dwc3_gadget_interrupt(struct dwc3 *dwc,
  2375. const struct dwc3_event_devt *event)
  2376. {
  2377. switch (event->type) {
  2378. case DWC3_DEVICE_EVENT_DISCONNECT:
  2379. dwc3_gadget_disconnect_interrupt(dwc);
  2380. break;
  2381. case DWC3_DEVICE_EVENT_RESET:
  2382. dwc3_gadget_reset_interrupt(dwc);
  2383. break;
  2384. case DWC3_DEVICE_EVENT_CONNECT_DONE:
  2385. dwc3_gadget_conndone_interrupt(dwc);
  2386. break;
  2387. case DWC3_DEVICE_EVENT_WAKEUP:
  2388. dwc3_gadget_wakeup_interrupt(dwc);
  2389. break;
  2390. case DWC3_DEVICE_EVENT_HIBER_REQ:
  2391. if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
  2392. "unexpected hibernation event\n"))
  2393. break;
  2394. dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
  2395. break;
  2396. case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
  2397. dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
  2398. break;
  2399. case DWC3_DEVICE_EVENT_EOPF:
  2400. /* It changed to be suspend event for version 2.30a and above */
  2401. if (dwc->revision >= DWC3_REVISION_230A) {
  2402. /*
  2403. * Ignore suspend event until the gadget enters into
  2404. * USB_STATE_CONFIGURED state.
  2405. */
  2406. if (dwc->gadget.state >= USB_STATE_CONFIGURED)
  2407. dwc3_gadget_suspend_interrupt(dwc,
  2408. event->event_info);
  2409. }
  2410. break;
  2411. case DWC3_DEVICE_EVENT_SOF:
  2412. case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
  2413. case DWC3_DEVICE_EVENT_CMD_CMPL:
  2414. case DWC3_DEVICE_EVENT_OVERFLOW:
  2415. break;
  2416. default:
  2417. dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
  2418. }
  2419. }
  2420. static void dwc3_process_event_entry(struct dwc3 *dwc,
  2421. const union dwc3_event *event)
  2422. {
  2423. trace_dwc3_event(event->raw, dwc);
  2424. /* Endpoint IRQ, handle it and return early */
  2425. if (event->type.is_devspec == 0) {
  2426. /* depevt */
  2427. return dwc3_endpoint_interrupt(dwc, &event->depevt);
  2428. }
  2429. switch (event->type.type) {
  2430. case DWC3_EVENT_TYPE_DEV:
  2431. dwc3_gadget_interrupt(dwc, &event->devt);
  2432. break;
  2433. /* REVISIT what to do with Carkit and I2C events ? */
  2434. default:
  2435. dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
  2436. }
  2437. }
  2438. static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
  2439. {
  2440. struct dwc3 *dwc = evt->dwc;
  2441. irqreturn_t ret = IRQ_NONE;
  2442. int left;
  2443. u32 reg;
  2444. left = evt->count;
  2445. if (!(evt->flags & DWC3_EVENT_PENDING))
  2446. return IRQ_NONE;
  2447. while (left > 0) {
  2448. union dwc3_event event;
  2449. event.raw = *(u32 *) (evt->cache + evt->lpos);
  2450. dwc3_process_event_entry(dwc, &event);
  2451. /*
  2452. * FIXME we wrap around correctly to the next entry as
  2453. * almost all entries are 4 bytes in size. There is one
  2454. * entry which has 12 bytes which is a regular entry
  2455. * followed by 8 bytes data. ATM I don't know how
  2456. * things are organized if we get next to the a
  2457. * boundary so I worry about that once we try to handle
  2458. * that.
  2459. */
  2460. evt->lpos = (evt->lpos + 4) % evt->length;
  2461. left -= 4;
  2462. }
  2463. evt->count = 0;
  2464. evt->flags &= ~DWC3_EVENT_PENDING;
  2465. ret = IRQ_HANDLED;
  2466. /* Unmask interrupt */
  2467. reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
  2468. reg &= ~DWC3_GEVNTSIZ_INTMASK;
  2469. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
  2470. if (dwc->imod_interval) {
  2471. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
  2472. dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
  2473. }
  2474. return ret;
  2475. }
  2476. static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
  2477. {
  2478. struct dwc3_event_buffer *evt = _evt;
  2479. struct dwc3 *dwc = evt->dwc;
  2480. unsigned long flags;
  2481. irqreturn_t ret = IRQ_NONE;
  2482. spin_lock_irqsave(&dwc->lock, flags);
  2483. ret = dwc3_process_event_buf(evt);
  2484. spin_unlock_irqrestore(&dwc->lock, flags);
  2485. return ret;
  2486. }
  2487. static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
  2488. {
  2489. struct dwc3 *dwc = evt->dwc;
  2490. u32 amount;
  2491. u32 count;
  2492. u32 reg;
  2493. if (pm_runtime_suspended(dwc->dev)) {
  2494. pm_runtime_get(dwc->dev);
  2495. disable_irq_nosync(dwc->irq_gadget);
  2496. dwc->pending_events = true;
  2497. return IRQ_HANDLED;
  2498. }
  2499. /*
  2500. * With PCIe legacy interrupt, test shows that top-half irq handler can
  2501. * be called again after HW interrupt deassertion. Check if bottom-half
  2502. * irq event handler completes before caching new event to prevent
  2503. * losing events.
  2504. */
  2505. if (evt->flags & DWC3_EVENT_PENDING)
  2506. return IRQ_HANDLED;
  2507. count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
  2508. count &= DWC3_GEVNTCOUNT_MASK;
  2509. if (!count)
  2510. return IRQ_NONE;
  2511. evt->count = count;
  2512. evt->flags |= DWC3_EVENT_PENDING;
  2513. /* Mask interrupt */
  2514. reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
  2515. reg |= DWC3_GEVNTSIZ_INTMASK;
  2516. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
  2517. amount = min(count, evt->length - evt->lpos);
  2518. memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
  2519. if (amount < count)
  2520. memcpy(evt->cache, evt->buf, count - amount);
  2521. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
  2522. return IRQ_WAKE_THREAD;
  2523. }
  2524. static irqreturn_t dwc3_interrupt(int irq, void *_evt)
  2525. {
  2526. struct dwc3_event_buffer *evt = _evt;
  2527. return dwc3_check_event_buf(evt);
  2528. }
  2529. static int dwc3_gadget_get_irq(struct dwc3 *dwc)
  2530. {
  2531. struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
  2532. int irq;
  2533. irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
  2534. if (irq > 0)
  2535. goto out;
  2536. if (irq == -EPROBE_DEFER)
  2537. goto out;
  2538. irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
  2539. if (irq > 0)
  2540. goto out;
  2541. if (irq == -EPROBE_DEFER)
  2542. goto out;
  2543. irq = platform_get_irq(dwc3_pdev, 0);
  2544. if (irq > 0)
  2545. goto out;
  2546. if (irq != -EPROBE_DEFER)
  2547. dev_err(dwc->dev, "missing peripheral IRQ\n");
  2548. if (!irq)
  2549. irq = -EINVAL;
  2550. out:
  2551. return irq;
  2552. }
  2553. /**
  2554. * dwc3_gadget_init - Initializes gadget related registers
  2555. * @dwc: pointer to our controller context structure
  2556. *
  2557. * Returns 0 on success otherwise negative errno.
  2558. */
  2559. int dwc3_gadget_init(struct dwc3 *dwc)
  2560. {
  2561. int ret;
  2562. int irq;
  2563. irq = dwc3_gadget_get_irq(dwc);
  2564. if (irq < 0) {
  2565. ret = irq;
  2566. goto err0;
  2567. }
  2568. dwc->irq_gadget = irq;
  2569. dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
  2570. sizeof(*dwc->ep0_trb) * 2,
  2571. &dwc->ep0_trb_addr, GFP_KERNEL);
  2572. if (!dwc->ep0_trb) {
  2573. dev_err(dwc->dev, "failed to allocate ep0 trb\n");
  2574. ret = -ENOMEM;
  2575. goto err0;
  2576. }
  2577. dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
  2578. if (!dwc->setup_buf) {
  2579. ret = -ENOMEM;
  2580. goto err1;
  2581. }
  2582. dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
  2583. &dwc->bounce_addr, GFP_KERNEL);
  2584. if (!dwc->bounce) {
  2585. ret = -ENOMEM;
  2586. goto err2;
  2587. }
  2588. init_completion(&dwc->ep0_in_setup);
  2589. dwc->gadget.ops = &dwc3_gadget_ops;
  2590. dwc->gadget.speed = USB_SPEED_UNKNOWN;
  2591. dwc->gadget.sg_supported = true;
  2592. dwc->gadget.name = "dwc3-gadget";
  2593. dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
  2594. /*
  2595. * FIXME We might be setting max_speed to <SUPER, however versions
  2596. * <2.20a of dwc3 have an issue with metastability (documented
  2597. * elsewhere in this driver) which tells us we can't set max speed to
  2598. * anything lower than SUPER.
  2599. *
  2600. * Because gadget.max_speed is only used by composite.c and function
  2601. * drivers (i.e. it won't go into dwc3's registers) we are allowing this
  2602. * to happen so we avoid sending SuperSpeed Capability descriptor
  2603. * together with our BOS descriptor as that could confuse host into
  2604. * thinking we can handle super speed.
  2605. *
  2606. * Note that, in fact, we won't even support GetBOS requests when speed
  2607. * is less than super speed because we don't have means, yet, to tell
  2608. * composite.c that we are USB 2.0 + LPM ECN.
  2609. */
  2610. if (dwc->revision < DWC3_REVISION_220A)
  2611. dev_info(dwc->dev, "changing max_speed on rev %08x\n",
  2612. dwc->revision);
  2613. dwc->gadget.max_speed = dwc->maximum_speed;
  2614. /*
  2615. * REVISIT: Here we should clear all pending IRQs to be
  2616. * sure we're starting from a well known location.
  2617. */
  2618. ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
  2619. if (ret)
  2620. goto err3;
  2621. ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
  2622. if (ret) {
  2623. dev_err(dwc->dev, "failed to register udc\n");
  2624. goto err4;
  2625. }
  2626. return 0;
  2627. err4:
  2628. dwc3_gadget_free_endpoints(dwc);
  2629. err3:
  2630. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  2631. dwc->bounce_addr);
  2632. err2:
  2633. kfree(dwc->setup_buf);
  2634. err1:
  2635. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  2636. dwc->ep0_trb, dwc->ep0_trb_addr);
  2637. err0:
  2638. return ret;
  2639. }
  2640. /* -------------------------------------------------------------------------- */
  2641. void dwc3_gadget_exit(struct dwc3 *dwc)
  2642. {
  2643. usb_del_gadget_udc(&dwc->gadget);
  2644. dwc3_gadget_free_endpoints(dwc);
  2645. dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
  2646. dwc->bounce_addr);
  2647. kfree(dwc->setup_buf);
  2648. dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
  2649. dwc->ep0_trb, dwc->ep0_trb_addr);
  2650. }
  2651. int dwc3_gadget_suspend(struct dwc3 *dwc)
  2652. {
  2653. if (!dwc->gadget_driver)
  2654. return 0;
  2655. dwc3_gadget_run_stop(dwc, false, false);
  2656. dwc3_disconnect_gadget(dwc);
  2657. __dwc3_gadget_stop(dwc);
  2658. return 0;
  2659. }
  2660. int dwc3_gadget_resume(struct dwc3 *dwc)
  2661. {
  2662. int ret;
  2663. if (!dwc->gadget_driver)
  2664. return 0;
  2665. ret = __dwc3_gadget_start(dwc);
  2666. if (ret < 0)
  2667. goto err0;
  2668. ret = dwc3_gadget_run_stop(dwc, true, false);
  2669. if (ret < 0)
  2670. goto err1;
  2671. return 0;
  2672. err1:
  2673. __dwc3_gadget_stop(dwc);
  2674. err0:
  2675. return ret;
  2676. }
  2677. void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
  2678. {
  2679. if (dwc->pending_events) {
  2680. dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
  2681. dwc->pending_events = false;
  2682. enable_irq(dwc->irq_gadget);
  2683. }
  2684. }