gadget.c 82 KB

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