cdns3-gadget.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence USBSS DRD Driver - gadget side.
  4. *
  5. * Copyright (C) 2018-2019 Cadence Design Systems.
  6. * Copyright (C) 2017-2018 NXP
  7. *
  8. * Authors: Pawel Jez <pjez@cadence.com>,
  9. * Pawel Laszczak <pawell@cadence.com>
  10. * Peter Chen <peter.chen@nxp.com>
  11. */
  12. /*
  13. * Work around 1:
  14. * At some situations, the controller may get stale data address in TRB
  15. * at below sequences:
  16. * 1. Controller read TRB includes data address
  17. * 2. Software updates TRBs includes data address and Cycle bit
  18. * 3. Controller read TRB which includes Cycle bit
  19. * 4. DMA run with stale data address
  20. *
  21. * To fix this problem, driver needs to make the first TRB in TD as invalid.
  22. * After preparing all TRBs driver needs to check the position of DMA and
  23. * if the DMA point to the first just added TRB and doorbell is 1,
  24. * then driver must defer making this TRB as valid. This TRB will be make
  25. * as valid during adding next TRB only if DMA is stopped or at TRBERR
  26. * interrupt.
  27. *
  28. * Issue has been fixed in DEV_VER_V3 version of controller.
  29. *
  30. * Work around 2:
  31. * Controller for OUT endpoints has shared on-chip buffers for all incoming
  32. * packets, including ep0out. It's FIFO buffer, so packets must be handle by DMA
  33. * in correct order. If the first packet in the buffer will not be handled,
  34. * then the following packets directed for other endpoints and functions
  35. * will be blocked.
  36. * Additionally the packets directed to one endpoint can block entire on-chip
  37. * buffers. In this case transfer to other endpoints also will blocked.
  38. *
  39. * To resolve this issue after raising the descriptor missing interrupt
  40. * driver prepares internal usb_request object and use it to arm DMA transfer.
  41. *
  42. * The problematic situation was observed in case when endpoint has been enabled
  43. * but no usb_request were queued. Driver try detects such endpoints and will
  44. * use this workaround only for these endpoint.
  45. *
  46. * Driver use limited number of buffer. This number can be set by macro
  47. * CDNS3_WA2_NUM_BUFFERS.
  48. *
  49. * Such blocking situation was observed on ACM gadget. For this function
  50. * host send OUT data packet but ACM function is not prepared for this packet.
  51. * It's cause that buffer placed in on chip memory block transfer to other
  52. * endpoints.
  53. *
  54. * Issue has been fixed in DEV_VER_V2 version of controller.
  55. *
  56. */
  57. #include <linux/dma-mapping.h>
  58. #include <linux/usb/gadget.h>
  59. #include <linux/module.h>
  60. #include <linux/dmapool.h>
  61. #include <linux/iopoll.h>
  62. #include <linux/property.h>
  63. #include "core.h"
  64. #include "gadget-export.h"
  65. #include "cdns3-gadget.h"
  66. #include "cdns3-trace.h"
  67. #include "drd.h"
  68. static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
  69. struct usb_request *request,
  70. gfp_t gfp_flags);
  71. static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
  72. struct usb_request *request);
  73. static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
  74. struct usb_request *request);
  75. /**
  76. * cdns3_clear_register_bit - clear bit in given register.
  77. * @ptr: address of device controller register to be read and changed
  78. * @mask: bits requested to clar
  79. */
  80. static void cdns3_clear_register_bit(void __iomem *ptr, u32 mask)
  81. {
  82. mask = readl(ptr) & ~mask;
  83. writel(mask, ptr);
  84. }
  85. /**
  86. * cdns3_set_register_bit - set bit in given register.
  87. * @ptr: address of device controller register to be read and changed
  88. * @mask: bits requested to set
  89. */
  90. void cdns3_set_register_bit(void __iomem *ptr, u32 mask)
  91. {
  92. mask = readl(ptr) | mask;
  93. writel(mask, ptr);
  94. }
  95. /**
  96. * cdns3_ep_addr_to_index - Macro converts endpoint address to
  97. * index of endpoint object in cdns3_device.eps[] container
  98. * @ep_addr: endpoint address for which endpoint object is required
  99. *
  100. */
  101. u8 cdns3_ep_addr_to_index(u8 ep_addr)
  102. {
  103. return (((ep_addr & 0x7F)) + ((ep_addr & USB_DIR_IN) ? 16 : 0));
  104. }
  105. static int cdns3_get_dma_pos(struct cdns3_device *priv_dev,
  106. struct cdns3_endpoint *priv_ep)
  107. {
  108. int dma_index;
  109. dma_index = readl(&priv_dev->regs->ep_traddr) - priv_ep->trb_pool_dma;
  110. return dma_index / TRB_SIZE;
  111. }
  112. /**
  113. * cdns3_next_request - returns next request from list
  114. * @list: list containing requests
  115. *
  116. * Returns request or NULL if no requests in list
  117. */
  118. struct usb_request *cdns3_next_request(struct list_head *list)
  119. {
  120. return list_first_entry_or_null(list, struct usb_request, list);
  121. }
  122. /**
  123. * cdns3_next_align_buf - returns next buffer from list
  124. * @list: list containing buffers
  125. *
  126. * Returns buffer or NULL if no buffers in list
  127. */
  128. static struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list)
  129. {
  130. return list_first_entry_or_null(list, struct cdns3_aligned_buf, list);
  131. }
  132. /**
  133. * cdns3_next_priv_request - returns next request from list
  134. * @list: list containing requests
  135. *
  136. * Returns request or NULL if no requests in list
  137. */
  138. static struct cdns3_request *cdns3_next_priv_request(struct list_head *list)
  139. {
  140. return list_first_entry_or_null(list, struct cdns3_request, list);
  141. }
  142. /**
  143. * cdns3_select_ep - selects endpoint
  144. * @priv_dev: extended gadget object
  145. * @ep: endpoint address
  146. */
  147. void cdns3_select_ep(struct cdns3_device *priv_dev, u32 ep)
  148. {
  149. if (priv_dev->selected_ep == ep)
  150. return;
  151. priv_dev->selected_ep = ep;
  152. writel(ep, &priv_dev->regs->ep_sel);
  153. }
  154. /**
  155. * cdns3_get_tdl - gets current tdl for selected endpoint.
  156. * @priv_dev: extended gadget object
  157. *
  158. * Before calling this function the appropriate endpoint must
  159. * be selected by means of cdns3_select_ep function.
  160. */
  161. static int cdns3_get_tdl(struct cdns3_device *priv_dev)
  162. {
  163. if (priv_dev->dev_ver < DEV_VER_V3)
  164. return EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
  165. else
  166. return readl(&priv_dev->regs->ep_tdl);
  167. }
  168. dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep,
  169. struct cdns3_trb *trb)
  170. {
  171. u32 offset = (char *)trb - (char *)priv_ep->trb_pool;
  172. return priv_ep->trb_pool_dma + offset;
  173. }
  174. static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
  175. {
  176. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  177. if (priv_ep->trb_pool) {
  178. dma_pool_free(priv_dev->eps_dma_pool,
  179. priv_ep->trb_pool, priv_ep->trb_pool_dma);
  180. priv_ep->trb_pool = NULL;
  181. }
  182. }
  183. /**
  184. * cdns3_allocate_trb_pool - Allocates TRB's pool for selected endpoint
  185. * @priv_ep: endpoint object
  186. *
  187. * Function will return 0 on success or -ENOMEM on allocation error
  188. */
  189. int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
  190. {
  191. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  192. int ring_size = TRB_RING_SIZE;
  193. int num_trbs = ring_size / TRB_SIZE;
  194. struct cdns3_trb *link_trb;
  195. if (priv_ep->trb_pool && priv_ep->alloc_ring_size < ring_size)
  196. cdns3_free_trb_pool(priv_ep);
  197. if (!priv_ep->trb_pool) {
  198. priv_ep->trb_pool = dma_pool_alloc(priv_dev->eps_dma_pool,
  199. GFP_ATOMIC,
  200. &priv_ep->trb_pool_dma);
  201. if (!priv_ep->trb_pool)
  202. return -ENOMEM;
  203. priv_ep->alloc_ring_size = ring_size;
  204. }
  205. memset(priv_ep->trb_pool, 0, ring_size);
  206. priv_ep->num_trbs = num_trbs;
  207. if (!priv_ep->num)
  208. return 0;
  209. /* Initialize the last TRB as Link TRB */
  210. link_trb = (priv_ep->trb_pool + (priv_ep->num_trbs - 1));
  211. if (priv_ep->use_streams) {
  212. /*
  213. * For stream capable endpoints driver use single correct TRB.
  214. * The last trb has zeroed cycle bit
  215. */
  216. link_trb->control = 0;
  217. } else {
  218. link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma));
  219. link_trb->control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_LINK) | TRB_TOGGLE);
  220. }
  221. return 0;
  222. }
  223. /**
  224. * cdns3_ep_stall_flush - Stalls and flushes selected endpoint
  225. * @priv_ep: endpoint object
  226. *
  227. * Endpoint must be selected before call to this function
  228. */
  229. static void cdns3_ep_stall_flush(struct cdns3_endpoint *priv_ep)
  230. {
  231. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  232. int val;
  233. trace_cdns3_halt(priv_ep, 1, 1);
  234. writel(EP_CMD_DFLUSH | EP_CMD_ERDY | EP_CMD_SSTALL,
  235. &priv_dev->regs->ep_cmd);
  236. /* wait for DFLUSH cleared */
  237. readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  238. !(val & EP_CMD_DFLUSH), 1, 1000);
  239. priv_ep->flags |= EP_STALLED;
  240. priv_ep->flags &= ~EP_STALL_PENDING;
  241. }
  242. /**
  243. * cdns3_hw_reset_eps_config - reset endpoints configuration kept by controller.
  244. * @priv_dev: extended gadget object
  245. */
  246. void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev)
  247. {
  248. int i;
  249. writel(USB_CONF_CFGRST, &priv_dev->regs->usb_conf);
  250. cdns3_allow_enable_l1(priv_dev, 0);
  251. priv_dev->hw_configured_flag = 0;
  252. priv_dev->onchip_used_size = 0;
  253. priv_dev->out_mem_is_allocated = 0;
  254. priv_dev->wait_for_setup = 0;
  255. priv_dev->using_streams = 0;
  256. for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
  257. if (priv_dev->eps[i])
  258. priv_dev->eps[i]->flags &= ~EP_CONFIGURED;
  259. }
  260. /**
  261. * cdns3_ep_inc_trb - increment a trb index.
  262. * @index: Pointer to the TRB index to increment.
  263. * @cs: Cycle state
  264. * @trb_in_seg: number of TRBs in segment
  265. *
  266. * The index should never point to the link TRB. After incrementing,
  267. * if it is point to the link TRB, wrap around to the beginning and revert
  268. * cycle state bit The
  269. * link TRB is always at the last TRB entry.
  270. */
  271. static void cdns3_ep_inc_trb(int *index, u8 *cs, int trb_in_seg)
  272. {
  273. (*index)++;
  274. if (*index == (trb_in_seg - 1)) {
  275. *index = 0;
  276. *cs ^= 1;
  277. }
  278. }
  279. /**
  280. * cdns3_ep_inc_enq - increment endpoint's enqueue pointer
  281. * @priv_ep: The endpoint whose enqueue pointer we're incrementing
  282. */
  283. static void cdns3_ep_inc_enq(struct cdns3_endpoint *priv_ep)
  284. {
  285. priv_ep->free_trbs--;
  286. cdns3_ep_inc_trb(&priv_ep->enqueue, &priv_ep->pcs, priv_ep->num_trbs);
  287. }
  288. /**
  289. * cdns3_ep_inc_deq - increment endpoint's dequeue pointer
  290. * @priv_ep: The endpoint whose dequeue pointer we're incrementing
  291. */
  292. static void cdns3_ep_inc_deq(struct cdns3_endpoint *priv_ep)
  293. {
  294. priv_ep->free_trbs++;
  295. cdns3_ep_inc_trb(&priv_ep->dequeue, &priv_ep->ccs, priv_ep->num_trbs);
  296. }
  297. /**
  298. * cdns3_allow_enable_l1 - enable/disable permits to transition to L1.
  299. * @priv_dev: Extended gadget object
  300. * @enable: Enable/disable permit to transition to L1.
  301. *
  302. * If bit USB_CONF_L1EN is set and device receive Extended Token packet,
  303. * then controller answer with ACK handshake.
  304. * If bit USB_CONF_L1DS is set and device receive Extended Token packet,
  305. * then controller answer with NYET handshake.
  306. */
  307. void cdns3_allow_enable_l1(struct cdns3_device *priv_dev, int enable)
  308. {
  309. if (enable)
  310. writel(USB_CONF_L1EN, &priv_dev->regs->usb_conf);
  311. else
  312. writel(USB_CONF_L1DS, &priv_dev->regs->usb_conf);
  313. }
  314. enum usb_device_speed cdns3_get_speed(struct cdns3_device *priv_dev)
  315. {
  316. u32 reg;
  317. reg = readl(&priv_dev->regs->usb_sts);
  318. if (DEV_SUPERSPEED(reg))
  319. return USB_SPEED_SUPER;
  320. else if (DEV_HIGHSPEED(reg))
  321. return USB_SPEED_HIGH;
  322. else if (DEV_FULLSPEED(reg))
  323. return USB_SPEED_FULL;
  324. else if (DEV_LOWSPEED(reg))
  325. return USB_SPEED_LOW;
  326. return USB_SPEED_UNKNOWN;
  327. }
  328. /**
  329. * cdns3_start_all_request - add to ring all request not started
  330. * @priv_dev: Extended gadget object
  331. * @priv_ep: The endpoint for whom request will be started.
  332. *
  333. * Returns return ENOMEM if transfer ring i not enough TRBs to start
  334. * all requests.
  335. */
  336. static int cdns3_start_all_request(struct cdns3_device *priv_dev,
  337. struct cdns3_endpoint *priv_ep)
  338. {
  339. struct usb_request *request;
  340. int ret = 0;
  341. u8 pending_empty = list_empty(&priv_ep->pending_req_list);
  342. /*
  343. * If the last pending transfer is INTERNAL
  344. * OR streams are enabled for this endpoint
  345. * do NOT start new transfer till the last one is pending
  346. */
  347. if (!pending_empty) {
  348. struct cdns3_request *priv_req;
  349. request = cdns3_next_request(&priv_ep->pending_req_list);
  350. priv_req = to_cdns3_request(request);
  351. if ((priv_req->flags & REQUEST_INTERNAL) ||
  352. (priv_ep->flags & EP_TDLCHK_EN) ||
  353. priv_ep->use_streams) {
  354. dev_dbg(priv_dev->dev, "Blocking external request\n");
  355. return ret;
  356. }
  357. }
  358. while (!list_empty(&priv_ep->deferred_req_list)) {
  359. request = cdns3_next_request(&priv_ep->deferred_req_list);
  360. if (!priv_ep->use_streams) {
  361. ret = cdns3_ep_run_transfer(priv_ep, request);
  362. } else {
  363. priv_ep->stream_sg_idx = 0;
  364. ret = cdns3_ep_run_stream_transfer(priv_ep, request);
  365. }
  366. if (ret)
  367. return ret;
  368. list_move_tail(&request->list, &priv_ep->pending_req_list);
  369. if (request->stream_id != 0 || (priv_ep->flags & EP_TDLCHK_EN))
  370. break;
  371. }
  372. priv_ep->flags &= ~EP_RING_FULL;
  373. return ret;
  374. }
  375. /*
  376. * WA2: Set flag for all not ISOC OUT endpoints. If this flag is set
  377. * driver try to detect whether endpoint need additional internal
  378. * buffer for unblocking on-chip FIFO buffer. This flag will be cleared
  379. * if before first DESCMISS interrupt the DMA will be armed.
  380. */
  381. #define cdns3_wa2_enable_detection(priv_dev, priv_ep, reg) do { \
  382. if (!priv_ep->dir && priv_ep->type != USB_ENDPOINT_XFER_ISOC) { \
  383. priv_ep->flags |= EP_QUIRK_EXTRA_BUF_DET; \
  384. (reg) |= EP_STS_EN_DESCMISEN; \
  385. } } while (0)
  386. static void __cdns3_descmiss_copy_data(struct usb_request *request,
  387. struct usb_request *descmiss_req)
  388. {
  389. int length = request->actual + descmiss_req->actual;
  390. struct scatterlist *s = request->sg;
  391. if (!s) {
  392. if (length <= request->length) {
  393. memcpy(&((u8 *)request->buf)[request->actual],
  394. descmiss_req->buf,
  395. descmiss_req->actual);
  396. request->actual = length;
  397. } else {
  398. /* It should never occures */
  399. request->status = -ENOMEM;
  400. }
  401. } else {
  402. if (length <= sg_dma_len(s)) {
  403. void *p = phys_to_virt(sg_dma_address(s));
  404. memcpy(&((u8 *)p)[request->actual],
  405. descmiss_req->buf,
  406. descmiss_req->actual);
  407. request->actual = length;
  408. } else {
  409. request->status = -ENOMEM;
  410. }
  411. }
  412. }
  413. /**
  414. * cdns3_wa2_descmiss_copy_data - copy data from internal requests to
  415. * request queued by class driver.
  416. * @priv_ep: extended endpoint object
  417. * @request: request object
  418. */
  419. static void cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint *priv_ep,
  420. struct usb_request *request)
  421. {
  422. struct usb_request *descmiss_req;
  423. struct cdns3_request *descmiss_priv_req;
  424. while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
  425. int chunk_end;
  426. descmiss_priv_req =
  427. cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
  428. descmiss_req = &descmiss_priv_req->request;
  429. /* driver can't touch pending request */
  430. if (descmiss_priv_req->flags & REQUEST_PENDING)
  431. break;
  432. chunk_end = descmiss_priv_req->flags & REQUEST_INTERNAL_CH;
  433. request->status = descmiss_req->status;
  434. __cdns3_descmiss_copy_data(request, descmiss_req);
  435. list_del_init(&descmiss_priv_req->list);
  436. kfree(descmiss_req->buf);
  437. cdns3_gadget_ep_free_request(&priv_ep->endpoint, descmiss_req);
  438. --priv_ep->wa2_counter;
  439. if (!chunk_end)
  440. break;
  441. }
  442. }
  443. static struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev,
  444. struct cdns3_endpoint *priv_ep,
  445. struct cdns3_request *priv_req)
  446. {
  447. if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN &&
  448. priv_req->flags & REQUEST_INTERNAL) {
  449. struct usb_request *req;
  450. req = cdns3_next_request(&priv_ep->deferred_req_list);
  451. priv_ep->descmis_req = NULL;
  452. if (!req)
  453. return NULL;
  454. /* unmap the gadget request before copying data */
  455. usb_gadget_unmap_request_by_dev(priv_dev->sysdev, req,
  456. priv_ep->dir);
  457. cdns3_wa2_descmiss_copy_data(priv_ep, req);
  458. if (!(priv_ep->flags & EP_QUIRK_END_TRANSFER) &&
  459. req->length != req->actual) {
  460. /* wait for next part of transfer */
  461. /* re-map the gadget request buffer*/
  462. usb_gadget_map_request_by_dev(priv_dev->sysdev, req,
  463. usb_endpoint_dir_in(priv_ep->endpoint.desc));
  464. return NULL;
  465. }
  466. if (req->status == -EINPROGRESS)
  467. req->status = 0;
  468. list_del_init(&req->list);
  469. cdns3_start_all_request(priv_dev, priv_ep);
  470. return req;
  471. }
  472. return &priv_req->request;
  473. }
  474. static int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev,
  475. struct cdns3_endpoint *priv_ep,
  476. struct cdns3_request *priv_req)
  477. {
  478. int deferred = 0;
  479. /*
  480. * If transfer was queued before DESCMISS appear than we
  481. * can disable handling of DESCMISS interrupt. Driver assumes that it
  482. * can disable special treatment for this endpoint.
  483. */
  484. if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
  485. u32 reg;
  486. cdns3_select_ep(priv_dev, priv_ep->num | priv_ep->dir);
  487. priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
  488. reg = readl(&priv_dev->regs->ep_sts_en);
  489. reg &= ~EP_STS_EN_DESCMISEN;
  490. trace_cdns3_wa2(priv_ep, "workaround disabled\n");
  491. writel(reg, &priv_dev->regs->ep_sts_en);
  492. }
  493. if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
  494. u8 pending_empty = list_empty(&priv_ep->pending_req_list);
  495. u8 descmiss_empty = list_empty(&priv_ep->wa2_descmiss_req_list);
  496. /*
  497. * DESCMISS transfer has been finished, so data will be
  498. * directly copied from internal allocated usb_request
  499. * objects.
  500. */
  501. if (pending_empty && !descmiss_empty &&
  502. !(priv_req->flags & REQUEST_INTERNAL)) {
  503. cdns3_wa2_descmiss_copy_data(priv_ep,
  504. &priv_req->request);
  505. trace_cdns3_wa2(priv_ep, "get internal stored data");
  506. list_add_tail(&priv_req->request.list,
  507. &priv_ep->pending_req_list);
  508. cdns3_gadget_giveback(priv_ep, priv_req,
  509. priv_req->request.status);
  510. /*
  511. * Intentionally driver returns positive value as
  512. * correct value. It informs that transfer has
  513. * been finished.
  514. */
  515. return EINPROGRESS;
  516. }
  517. /*
  518. * Driver will wait for completion DESCMISS transfer,
  519. * before starts new, not DESCMISS transfer.
  520. */
  521. if (!pending_empty && !descmiss_empty) {
  522. trace_cdns3_wa2(priv_ep, "wait for pending transfer\n");
  523. deferred = 1;
  524. }
  525. if (priv_req->flags & REQUEST_INTERNAL)
  526. list_add_tail(&priv_req->list,
  527. &priv_ep->wa2_descmiss_req_list);
  528. }
  529. return deferred;
  530. }
  531. static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep)
  532. {
  533. struct cdns3_request *priv_req;
  534. while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
  535. u8 chain;
  536. priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
  537. chain = !!(priv_req->flags & REQUEST_INTERNAL_CH);
  538. trace_cdns3_wa2(priv_ep, "removes eldest request");
  539. kfree(priv_req->request.buf);
  540. list_del_init(&priv_req->list);
  541. cdns3_gadget_ep_free_request(&priv_ep->endpoint,
  542. &priv_req->request);
  543. --priv_ep->wa2_counter;
  544. if (!chain)
  545. break;
  546. }
  547. }
  548. /**
  549. * cdns3_wa2_descmissing_packet - handles descriptor missing event.
  550. * @priv_ep: extended gadget object
  551. *
  552. * This function is used only for WA2. For more information see Work around 2
  553. * description.
  554. */
  555. static void cdns3_wa2_descmissing_packet(struct cdns3_endpoint *priv_ep)
  556. {
  557. struct cdns3_request *priv_req;
  558. struct usb_request *request;
  559. u8 pending_empty = list_empty(&priv_ep->pending_req_list);
  560. /* check for pending transfer */
  561. if (!pending_empty) {
  562. trace_cdns3_wa2(priv_ep, "Ignoring Descriptor missing IRQ\n");
  563. return;
  564. }
  565. if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
  566. priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
  567. priv_ep->flags |= EP_QUIRK_EXTRA_BUF_EN;
  568. }
  569. trace_cdns3_wa2(priv_ep, "Description Missing detected\n");
  570. if (priv_ep->wa2_counter >= CDNS3_WA2_NUM_BUFFERS) {
  571. trace_cdns3_wa2(priv_ep, "WA2 overflow\n");
  572. cdns3_wa2_remove_old_request(priv_ep);
  573. }
  574. request = cdns3_gadget_ep_alloc_request(&priv_ep->endpoint,
  575. GFP_ATOMIC);
  576. if (!request)
  577. goto err;
  578. priv_req = to_cdns3_request(request);
  579. priv_req->flags |= REQUEST_INTERNAL;
  580. /* if this field is still assigned it indicate that transfer related
  581. * with this request has not been finished yet. Driver in this
  582. * case simply allocate next request and assign flag REQUEST_INTERNAL_CH
  583. * flag to previous one. It will indicate that current request is
  584. * part of the previous one.
  585. */
  586. if (priv_ep->descmis_req)
  587. priv_ep->descmis_req->flags |= REQUEST_INTERNAL_CH;
  588. priv_req->request.buf = kzalloc(CDNS3_DESCMIS_BUF_SIZE,
  589. GFP_ATOMIC);
  590. priv_ep->wa2_counter++;
  591. if (!priv_req->request.buf) {
  592. cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
  593. goto err;
  594. }
  595. priv_req->request.length = CDNS3_DESCMIS_BUF_SIZE;
  596. priv_ep->descmis_req = priv_req;
  597. __cdns3_gadget_ep_queue(&priv_ep->endpoint,
  598. &priv_ep->descmis_req->request,
  599. GFP_ATOMIC);
  600. return;
  601. err:
  602. dev_err(priv_ep->cdns3_dev->dev,
  603. "Failed: No sufficient memory for DESCMIS\n");
  604. }
  605. static void cdns3_wa2_reset_tdl(struct cdns3_device *priv_dev)
  606. {
  607. u16 tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
  608. if (tdl) {
  609. u16 reset_val = EP_CMD_TDL_MAX + 1 - tdl;
  610. writel(EP_CMD_TDL_SET(reset_val) | EP_CMD_STDL,
  611. &priv_dev->regs->ep_cmd);
  612. }
  613. }
  614. static void cdns3_wa2_check_outq_status(struct cdns3_device *priv_dev)
  615. {
  616. u32 ep_sts_reg;
  617. /* select EP0-out */
  618. cdns3_select_ep(priv_dev, 0);
  619. ep_sts_reg = readl(&priv_dev->regs->ep_sts);
  620. if (EP_STS_OUTQ_VAL(ep_sts_reg)) {
  621. u32 outq_ep_num = EP_STS_OUTQ_NO(ep_sts_reg);
  622. struct cdns3_endpoint *outq_ep = priv_dev->eps[outq_ep_num];
  623. if ((outq_ep->flags & EP_ENABLED) && !(outq_ep->use_streams) &&
  624. outq_ep->type != USB_ENDPOINT_XFER_ISOC && outq_ep_num) {
  625. u8 pending_empty = list_empty(&outq_ep->pending_req_list);
  626. if ((outq_ep->flags & EP_QUIRK_EXTRA_BUF_DET) ||
  627. (outq_ep->flags & EP_QUIRK_EXTRA_BUF_EN) ||
  628. !pending_empty) {
  629. } else {
  630. u32 ep_sts_en_reg;
  631. u32 ep_cmd_reg;
  632. cdns3_select_ep(priv_dev, outq_ep->num |
  633. outq_ep->dir);
  634. ep_sts_en_reg = readl(&priv_dev->regs->ep_sts_en);
  635. ep_cmd_reg = readl(&priv_dev->regs->ep_cmd);
  636. outq_ep->flags |= EP_TDLCHK_EN;
  637. cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
  638. EP_CFG_TDL_CHK);
  639. cdns3_wa2_enable_detection(priv_dev, outq_ep,
  640. ep_sts_en_reg);
  641. writel(ep_sts_en_reg,
  642. &priv_dev->regs->ep_sts_en);
  643. /* reset tdl value to zero */
  644. cdns3_wa2_reset_tdl(priv_dev);
  645. /*
  646. * Memory barrier - Reset tdl before ringing the
  647. * doorbell.
  648. */
  649. wmb();
  650. if (EP_CMD_DRDY & ep_cmd_reg) {
  651. trace_cdns3_wa2(outq_ep, "Enabling WA2 skipping doorbell\n");
  652. } else {
  653. trace_cdns3_wa2(outq_ep, "Enabling WA2 ringing doorbell\n");
  654. /*
  655. * ring doorbell to generate DESCMIS irq
  656. */
  657. writel(EP_CMD_DRDY,
  658. &priv_dev->regs->ep_cmd);
  659. }
  660. }
  661. }
  662. }
  663. }
  664. /**
  665. * cdns3_gadget_giveback - call struct usb_request's ->complete callback
  666. * @priv_ep: The endpoint to whom the request belongs to
  667. * @priv_req: The request we're giving back
  668. * @status: completion code for the request
  669. *
  670. * Must be called with controller's lock held and interrupts disabled. This
  671. * function will unmap @req and call its ->complete() callback to notify upper
  672. * layers that it has completed.
  673. */
  674. void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
  675. struct cdns3_request *priv_req,
  676. int status)
  677. {
  678. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  679. struct usb_request *request = &priv_req->request;
  680. list_del_init(&request->list);
  681. if (request->status == -EINPROGRESS)
  682. request->status = status;
  683. if (likely(!(priv_req->flags & REQUEST_UNALIGNED)))
  684. usb_gadget_unmap_request_by_dev(priv_dev->sysdev, request,
  685. priv_ep->dir);
  686. if ((priv_req->flags & REQUEST_UNALIGNED) &&
  687. priv_ep->dir == USB_DIR_OUT && !request->status) {
  688. /* Make DMA buffer CPU accessible */
  689. dma_sync_single_for_cpu(priv_dev->sysdev,
  690. priv_req->aligned_buf->dma,
  691. request->actual,
  692. priv_req->aligned_buf->dir);
  693. memcpy(request->buf, priv_req->aligned_buf->buf,
  694. request->actual);
  695. }
  696. priv_req->flags &= ~(REQUEST_PENDING | REQUEST_UNALIGNED);
  697. /* All TRBs have finished, clear the counter */
  698. priv_req->finished_trb = 0;
  699. trace_cdns3_gadget_giveback(priv_req);
  700. if (priv_dev->dev_ver < DEV_VER_V2) {
  701. request = cdns3_wa2_gadget_giveback(priv_dev, priv_ep,
  702. priv_req);
  703. if (!request)
  704. return;
  705. }
  706. /*
  707. * zlp request is appended by driver, needn't call usb_gadget_giveback_request() to notify
  708. * gadget composite driver.
  709. */
  710. if (request->complete && request->buf != priv_dev->zlp_buf) {
  711. spin_unlock(&priv_dev->lock);
  712. usb_gadget_giveback_request(&priv_ep->endpoint,
  713. request);
  714. spin_lock(&priv_dev->lock);
  715. }
  716. if (request->buf == priv_dev->zlp_buf)
  717. cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
  718. }
  719. static void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep)
  720. {
  721. /* Work around for stale data address in TRB*/
  722. if (priv_ep->wa1_set) {
  723. trace_cdns3_wa1(priv_ep, "restore cycle bit");
  724. priv_ep->wa1_set = 0;
  725. priv_ep->wa1_trb_index = 0xFFFF;
  726. if (priv_ep->wa1_cycle_bit) {
  727. priv_ep->wa1_trb->control =
  728. priv_ep->wa1_trb->control | cpu_to_le32(0x1);
  729. } else {
  730. priv_ep->wa1_trb->control =
  731. priv_ep->wa1_trb->control & cpu_to_le32(~0x1);
  732. }
  733. }
  734. }
  735. static void cdns3_free_aligned_request_buf(struct work_struct *work)
  736. {
  737. struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
  738. aligned_buf_wq);
  739. struct cdns3_aligned_buf *buf, *tmp;
  740. unsigned long flags;
  741. spin_lock_irqsave(&priv_dev->lock, flags);
  742. list_for_each_entry_safe(buf, tmp, &priv_dev->aligned_buf_list, list) {
  743. if (!buf->in_use) {
  744. list_del(&buf->list);
  745. /*
  746. * Re-enable interrupts to free DMA capable memory.
  747. * Driver can't free this memory with disabled
  748. * interrupts.
  749. */
  750. spin_unlock_irqrestore(&priv_dev->lock, flags);
  751. dma_free_noncoherent(priv_dev->sysdev, buf->size,
  752. buf->buf, buf->dma, buf->dir);
  753. kfree(buf);
  754. spin_lock_irqsave(&priv_dev->lock, flags);
  755. }
  756. }
  757. spin_unlock_irqrestore(&priv_dev->lock, flags);
  758. }
  759. static int cdns3_prepare_aligned_request_buf(struct cdns3_request *priv_req)
  760. {
  761. struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
  762. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  763. struct cdns3_aligned_buf *buf;
  764. /* check if buffer is aligned to 8. */
  765. if (!((uintptr_t)priv_req->request.buf & 0x7))
  766. return 0;
  767. buf = priv_req->aligned_buf;
  768. if (!buf || priv_req->request.length > buf->size) {
  769. buf = kzalloc_obj(*buf, GFP_ATOMIC);
  770. if (!buf)
  771. return -ENOMEM;
  772. buf->size = priv_req->request.length;
  773. buf->dir = usb_endpoint_dir_in(priv_ep->endpoint.desc) ?
  774. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  775. buf->buf = dma_alloc_noncoherent(priv_dev->sysdev,
  776. buf->size,
  777. &buf->dma,
  778. buf->dir,
  779. GFP_ATOMIC);
  780. if (!buf->buf) {
  781. kfree(buf);
  782. return -ENOMEM;
  783. }
  784. if (priv_req->aligned_buf) {
  785. trace_cdns3_free_aligned_request(priv_req);
  786. priv_req->aligned_buf->in_use = 0;
  787. queue_work(system_freezable_wq,
  788. &priv_dev->aligned_buf_wq);
  789. }
  790. buf->in_use = 1;
  791. priv_req->aligned_buf = buf;
  792. list_add_tail(&buf->list,
  793. &priv_dev->aligned_buf_list);
  794. }
  795. if (priv_ep->dir == USB_DIR_IN) {
  796. /* Make DMA buffer CPU accessible */
  797. dma_sync_single_for_cpu(priv_dev->sysdev,
  798. buf->dma, buf->size, buf->dir);
  799. memcpy(buf->buf, priv_req->request.buf,
  800. priv_req->request.length);
  801. }
  802. /* Transfer DMA buffer ownership back to device */
  803. dma_sync_single_for_device(priv_dev->sysdev,
  804. buf->dma, buf->size, buf->dir);
  805. priv_req->flags |= REQUEST_UNALIGNED;
  806. trace_cdns3_prepare_aligned_request(priv_req);
  807. return 0;
  808. }
  809. static int cdns3_wa1_update_guard(struct cdns3_endpoint *priv_ep,
  810. struct cdns3_trb *trb)
  811. {
  812. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  813. if (!priv_ep->wa1_set) {
  814. u32 doorbell;
  815. doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
  816. if (doorbell) {
  817. priv_ep->wa1_cycle_bit = priv_ep->pcs ? TRB_CYCLE : 0;
  818. priv_ep->wa1_set = 1;
  819. priv_ep->wa1_trb = trb;
  820. priv_ep->wa1_trb_index = priv_ep->enqueue;
  821. trace_cdns3_wa1(priv_ep, "set guard");
  822. return 0;
  823. }
  824. }
  825. return 1;
  826. }
  827. static void cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device *priv_dev,
  828. struct cdns3_endpoint *priv_ep)
  829. {
  830. int dma_index;
  831. u32 doorbell;
  832. doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
  833. dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
  834. if (!doorbell || dma_index != priv_ep->wa1_trb_index)
  835. cdns3_wa1_restore_cycle_bit(priv_ep);
  836. }
  837. static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
  838. struct usb_request *request)
  839. {
  840. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  841. struct cdns3_request *priv_req;
  842. struct cdns3_trb *trb;
  843. dma_addr_t trb_dma;
  844. int address;
  845. u32 control;
  846. u32 length;
  847. u32 tdl;
  848. unsigned int sg_idx = priv_ep->stream_sg_idx;
  849. priv_req = to_cdns3_request(request);
  850. address = priv_ep->endpoint.desc->bEndpointAddress;
  851. priv_ep->flags |= EP_PENDING_REQUEST;
  852. /* must allocate buffer aligned to 8 */
  853. if (priv_req->flags & REQUEST_UNALIGNED)
  854. trb_dma = priv_req->aligned_buf->dma;
  855. else
  856. trb_dma = request->dma;
  857. /* For stream capable endpoints driver use only single TD. */
  858. trb = priv_ep->trb_pool + priv_ep->enqueue;
  859. priv_req->start_trb = priv_ep->enqueue;
  860. priv_req->end_trb = priv_req->start_trb;
  861. priv_req->trb = trb;
  862. cdns3_select_ep(priv_ep->cdns3_dev, address);
  863. control = TRB_TYPE(TRB_NORMAL) | TRB_CYCLE |
  864. TRB_STREAM_ID(priv_req->request.stream_id) | TRB_ISP;
  865. if (!request->num_sgs) {
  866. trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
  867. length = request->length;
  868. } else {
  869. trb->buffer = cpu_to_le32(TRB_BUFFER(request->sg[sg_idx].dma_address));
  870. length = request->sg[sg_idx].length;
  871. }
  872. tdl = DIV_ROUND_UP(length, priv_ep->endpoint.maxpacket);
  873. trb->length = cpu_to_le32(TRB_BURST_LEN(16) | TRB_LEN(length));
  874. /*
  875. * For DEV_VER_V2 controller version we have enabled
  876. * USB_CONF2_EN_TDL_TRB in DMULT configuration.
  877. * This enables TDL calculation based on TRB, hence setting TDL in TRB.
  878. */
  879. if (priv_dev->dev_ver >= DEV_VER_V2) {
  880. if (priv_dev->gadget.speed == USB_SPEED_SUPER)
  881. trb->length |= cpu_to_le32(TRB_TDL_SS_SIZE(tdl));
  882. }
  883. priv_req->flags |= REQUEST_PENDING;
  884. trb->control = cpu_to_le32(control);
  885. trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
  886. /*
  887. * Memory barrier - Cycle Bit must be set before trb->length and
  888. * trb->buffer fields.
  889. */
  890. wmb();
  891. /* always first element */
  892. writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma),
  893. &priv_dev->regs->ep_traddr);
  894. if (!(priv_ep->flags & EP_STALLED)) {
  895. trace_cdns3_ring(priv_ep);
  896. /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
  897. writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
  898. priv_ep->prime_flag = false;
  899. /*
  900. * Controller version DEV_VER_V2 tdl calculation
  901. * is based on TRB
  902. */
  903. if (priv_dev->dev_ver < DEV_VER_V2)
  904. writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
  905. &priv_dev->regs->ep_cmd);
  906. else if (priv_dev->dev_ver > DEV_VER_V2)
  907. writel(tdl, &priv_dev->regs->ep_tdl);
  908. priv_ep->last_stream_id = priv_req->request.stream_id;
  909. writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
  910. writel(EP_CMD_ERDY_SID(priv_req->request.stream_id) |
  911. EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
  912. trace_cdns3_doorbell_epx(priv_ep->name,
  913. readl(&priv_dev->regs->ep_traddr));
  914. }
  915. /* WORKAROUND for transition to L0 */
  916. __cdns3_gadget_wakeup(priv_dev);
  917. return 0;
  918. }
  919. static void cdns3_rearm_drdy_if_needed(struct cdns3_endpoint *priv_ep)
  920. {
  921. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  922. if (priv_dev->dev_ver < DEV_VER_V3)
  923. return;
  924. if (readl(&priv_dev->regs->ep_sts) & EP_STS_TRBERR) {
  925. writel(EP_STS_TRBERR, &priv_dev->regs->ep_sts);
  926. writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
  927. }
  928. }
  929. /**
  930. * cdns3_ep_run_transfer - start transfer on no-default endpoint hardware
  931. * @priv_ep: endpoint object
  932. * @request: request object
  933. *
  934. * Returns zero on success or negative value on failure
  935. */
  936. static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
  937. struct usb_request *request)
  938. {
  939. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  940. struct cdns3_request *priv_req;
  941. struct cdns3_trb *trb;
  942. struct cdns3_trb *link_trb = NULL;
  943. dma_addr_t trb_dma;
  944. u32 togle_pcs = 1;
  945. int sg_iter = 0;
  946. int num_trb_req;
  947. int trb_burst;
  948. int num_trb;
  949. int address;
  950. u32 control;
  951. int pcs;
  952. u16 total_tdl = 0;
  953. struct scatterlist *s = NULL;
  954. bool sg_supported = !!(request->num_mapped_sgs);
  955. u32 ioc = request->no_interrupt ? 0 : TRB_IOC;
  956. num_trb_req = sg_supported ? request->num_mapped_sgs : 1;
  957. /* ISO transfer require each SOF have a TD, each TD include some TRBs */
  958. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
  959. num_trb = priv_ep->interval * num_trb_req;
  960. else
  961. num_trb = num_trb_req;
  962. priv_req = to_cdns3_request(request);
  963. address = priv_ep->endpoint.desc->bEndpointAddress;
  964. priv_ep->flags |= EP_PENDING_REQUEST;
  965. /* must allocate buffer aligned to 8 */
  966. if (priv_req->flags & REQUEST_UNALIGNED)
  967. trb_dma = priv_req->aligned_buf->dma;
  968. else
  969. trb_dma = request->dma;
  970. trb = priv_ep->trb_pool + priv_ep->enqueue;
  971. priv_req->start_trb = priv_ep->enqueue;
  972. priv_req->trb = trb;
  973. cdns3_select_ep(priv_ep->cdns3_dev, address);
  974. /* prepare ring */
  975. if ((priv_ep->enqueue + num_trb) >= (priv_ep->num_trbs - 1)) {
  976. int doorbell, dma_index;
  977. u32 ch_bit = 0;
  978. doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
  979. dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
  980. /* Driver can't update LINK TRB if it is current processed. */
  981. if (doorbell && dma_index == priv_ep->num_trbs - 1) {
  982. priv_ep->flags |= EP_DEFERRED_DRDY;
  983. return -ENOBUFS;
  984. }
  985. /*updating C bt in Link TRB before starting DMA*/
  986. link_trb = priv_ep->trb_pool + (priv_ep->num_trbs - 1);
  987. /*
  988. * For TRs size equal 2 enabling TRB_CHAIN for epXin causes
  989. * that DMA stuck at the LINK TRB.
  990. * On the other hand, removing TRB_CHAIN for longer TRs for
  991. * epXout cause that DMA stuck after handling LINK TRB.
  992. * To eliminate this strange behavioral driver set TRB_CHAIN
  993. * bit only for TR size > 2.
  994. */
  995. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC ||
  996. TRBS_PER_SEGMENT > 2)
  997. ch_bit = TRB_CHAIN;
  998. link_trb->control = cpu_to_le32(((priv_ep->pcs) ? TRB_CYCLE : 0) |
  999. TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit);
  1000. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
  1001. /*
  1002. * ISO require LINK TRB must be first one of TD.
  1003. * Fill LINK TRBs for left trb space to simply software process logic.
  1004. */
  1005. while (priv_ep->enqueue) {
  1006. *trb = *link_trb;
  1007. trace_cdns3_prepare_trb(priv_ep, trb);
  1008. cdns3_ep_inc_enq(priv_ep);
  1009. trb = priv_ep->trb_pool + priv_ep->enqueue;
  1010. priv_req->trb = trb;
  1011. }
  1012. }
  1013. }
  1014. if (num_trb > priv_ep->free_trbs) {
  1015. priv_ep->flags |= EP_RING_FULL;
  1016. return -ENOBUFS;
  1017. }
  1018. if (priv_dev->dev_ver <= DEV_VER_V2)
  1019. togle_pcs = cdns3_wa1_update_guard(priv_ep, trb);
  1020. /* set incorrect Cycle Bit for first trb*/
  1021. control = priv_ep->pcs ? 0 : TRB_CYCLE;
  1022. trb->length = 0;
  1023. if (priv_dev->dev_ver >= DEV_VER_V2) {
  1024. u16 td_size;
  1025. td_size = DIV_ROUND_UP(request->length,
  1026. priv_ep->endpoint.maxpacket);
  1027. if (priv_dev->gadget.speed == USB_SPEED_SUPER)
  1028. trb->length = cpu_to_le32(TRB_TDL_SS_SIZE(td_size));
  1029. else
  1030. control |= TRB_TDL_HS_SIZE(td_size);
  1031. }
  1032. do {
  1033. u32 length;
  1034. if (!(sg_iter % num_trb_req) && sg_supported)
  1035. s = request->sg;
  1036. /* fill TRB */
  1037. control |= TRB_TYPE(TRB_NORMAL);
  1038. if (sg_supported) {
  1039. trb->buffer = cpu_to_le32(TRB_BUFFER(sg_dma_address(s)));
  1040. length = sg_dma_len(s);
  1041. } else {
  1042. trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
  1043. length = request->length;
  1044. }
  1045. if (priv_ep->flags & EP_TDLCHK_EN)
  1046. total_tdl += DIV_ROUND_UP(length,
  1047. priv_ep->endpoint.maxpacket);
  1048. trb_burst = priv_ep->trb_burst_size;
  1049. /*
  1050. * Supposed DMA cross 4k bounder problem should be fixed at DEV_VER_V2, but still
  1051. * met problem when do ISO transfer if sg enabled.
  1052. *
  1053. * Data pattern likes below when sg enabled, package size is 1k and mult is 2
  1054. * [UVC Header(8B) ] [data(3k - 8)] ...
  1055. *
  1056. * The received data at offset 0xd000 will get 0xc000 data, len 0x70. Error happen
  1057. * as below pattern:
  1058. * 0xd000: wrong
  1059. * 0xe000: wrong
  1060. * 0xf000: correct
  1061. * 0x10000: wrong
  1062. * 0x11000: wrong
  1063. * 0x12000: correct
  1064. * ...
  1065. *
  1066. * But it is still unclear about why error have not happen below 0xd000, it should
  1067. * cross 4k bounder. But anyway, the below code can fix this problem.
  1068. *
  1069. * To avoid DMA cross 4k bounder at ISO transfer, reduce burst len according to 16.
  1070. */
  1071. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && priv_dev->dev_ver <= DEV_VER_V2)
  1072. if (ALIGN_DOWN(trb->buffer, SZ_4K) !=
  1073. ALIGN_DOWN(trb->buffer + length, SZ_4K))
  1074. trb_burst = 16;
  1075. trb->length |= cpu_to_le32(TRB_BURST_LEN(trb_burst) |
  1076. TRB_LEN(length));
  1077. pcs = priv_ep->pcs ? TRB_CYCLE : 0;
  1078. /*
  1079. * first trb should be prepared as last to avoid processing
  1080. * transfer to early
  1081. */
  1082. if (sg_iter != 0)
  1083. control |= pcs;
  1084. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
  1085. control |= ioc | TRB_ISP;
  1086. } else {
  1087. /* for last element in TD or in SG list */
  1088. if (sg_iter == (num_trb - 1) && sg_iter != 0)
  1089. control |= pcs | ioc | TRB_ISP;
  1090. }
  1091. if (sg_iter)
  1092. trb->control = cpu_to_le32(control);
  1093. else
  1094. priv_req->trb->control = cpu_to_le32(control);
  1095. if (sg_supported) {
  1096. trb->control |= cpu_to_le32(TRB_ISP);
  1097. /* Don't set chain bit for last TRB */
  1098. if ((sg_iter % num_trb_req) < num_trb_req - 1)
  1099. trb->control |= cpu_to_le32(TRB_CHAIN);
  1100. s = sg_next(s);
  1101. }
  1102. control = 0;
  1103. ++sg_iter;
  1104. priv_req->end_trb = priv_ep->enqueue;
  1105. cdns3_ep_inc_enq(priv_ep);
  1106. trb = priv_ep->trb_pool + priv_ep->enqueue;
  1107. trb->length = 0;
  1108. } while (sg_iter < num_trb);
  1109. trb = priv_req->trb;
  1110. priv_req->flags |= REQUEST_PENDING;
  1111. priv_req->num_of_trb = num_trb;
  1112. if (sg_iter == 1)
  1113. trb->control |= cpu_to_le32(ioc | TRB_ISP);
  1114. if (priv_dev->dev_ver < DEV_VER_V2 &&
  1115. (priv_ep->flags & EP_TDLCHK_EN)) {
  1116. u16 tdl = total_tdl;
  1117. u16 old_tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
  1118. if (tdl > EP_CMD_TDL_MAX) {
  1119. tdl = EP_CMD_TDL_MAX;
  1120. priv_ep->pending_tdl = total_tdl - EP_CMD_TDL_MAX;
  1121. }
  1122. if (old_tdl < tdl) {
  1123. tdl -= old_tdl;
  1124. writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
  1125. &priv_dev->regs->ep_cmd);
  1126. }
  1127. }
  1128. /*
  1129. * Memory barrier - cycle bit must be set before other filds in trb.
  1130. */
  1131. wmb();
  1132. /* give the TD to the consumer*/
  1133. if (togle_pcs)
  1134. trb->control = trb->control ^ cpu_to_le32(1);
  1135. if (priv_dev->dev_ver <= DEV_VER_V2)
  1136. cdns3_wa1_tray_restore_cycle_bit(priv_dev, priv_ep);
  1137. if (num_trb > 1) {
  1138. int i = 0;
  1139. while (i < num_trb) {
  1140. trace_cdns3_prepare_trb(priv_ep, trb + i);
  1141. if (trb + i == link_trb) {
  1142. trb = priv_ep->trb_pool;
  1143. num_trb = num_trb - i;
  1144. i = 0;
  1145. } else {
  1146. i++;
  1147. }
  1148. }
  1149. } else {
  1150. trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
  1151. }
  1152. /*
  1153. * Memory barrier - Cycle Bit must be set before trb->length and
  1154. * trb->buffer fields.
  1155. */
  1156. wmb();
  1157. /*
  1158. * For DMULT mode we can set address to transfer ring only once after
  1159. * enabling endpoint.
  1160. */
  1161. if (priv_ep->flags & EP_UPDATE_EP_TRBADDR) {
  1162. /*
  1163. * Until SW is not ready to handle the OUT transfer the ISO OUT
  1164. * Endpoint should be disabled (EP_CFG.ENABLE = 0).
  1165. * EP_CFG_ENABLE must be set before updating ep_traddr.
  1166. */
  1167. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir &&
  1168. !(priv_ep->flags & EP_QUIRK_ISO_OUT_EN)) {
  1169. priv_ep->flags |= EP_QUIRK_ISO_OUT_EN;
  1170. cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
  1171. EP_CFG_ENABLE);
  1172. }
  1173. writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma +
  1174. priv_req->start_trb * TRB_SIZE),
  1175. &priv_dev->regs->ep_traddr);
  1176. priv_ep->flags &= ~EP_UPDATE_EP_TRBADDR;
  1177. }
  1178. if (!priv_ep->wa1_set && !(priv_ep->flags & EP_STALLED)) {
  1179. trace_cdns3_ring(priv_ep);
  1180. /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
  1181. writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
  1182. writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
  1183. cdns3_rearm_drdy_if_needed(priv_ep);
  1184. trace_cdns3_doorbell_epx(priv_ep->name,
  1185. readl(&priv_dev->regs->ep_traddr));
  1186. }
  1187. /* WORKAROUND for transition to L0 */
  1188. __cdns3_gadget_wakeup(priv_dev);
  1189. return 0;
  1190. }
  1191. void cdns3_set_hw_configuration(struct cdns3_device *priv_dev)
  1192. {
  1193. struct cdns3_endpoint *priv_ep;
  1194. struct usb_ep *ep;
  1195. if (priv_dev->hw_configured_flag)
  1196. return;
  1197. writel(USB_CONF_CFGSET, &priv_dev->regs->usb_conf);
  1198. cdns3_set_register_bit(&priv_dev->regs->usb_conf,
  1199. USB_CONF_U1EN | USB_CONF_U2EN);
  1200. priv_dev->hw_configured_flag = 1;
  1201. list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
  1202. if (ep->enabled) {
  1203. priv_ep = ep_to_cdns3_ep(ep);
  1204. cdns3_start_all_request(priv_dev, priv_ep);
  1205. }
  1206. }
  1207. cdns3_allow_enable_l1(priv_dev, 1);
  1208. }
  1209. /**
  1210. * cdns3_trb_handled - check whether trb has been handled by DMA
  1211. *
  1212. * @priv_ep: extended endpoint object.
  1213. * @priv_req: request object for checking
  1214. *
  1215. * Endpoint must be selected before invoking this function.
  1216. *
  1217. * Returns false if request has not been handled by DMA, else returns true.
  1218. *
  1219. * SR - start ring
  1220. * ER - end ring
  1221. * DQ = priv_ep->dequeue - dequeue position
  1222. * EQ = priv_ep->enqueue - enqueue position
  1223. * ST = priv_req->start_trb - index of first TRB in transfer ring
  1224. * ET = priv_req->end_trb - index of last TRB in transfer ring
  1225. * CI = current_index - index of processed TRB by DMA.
  1226. *
  1227. * As first step, we check if the TRB between the ST and ET.
  1228. * Then, we check if cycle bit for index priv_ep->dequeue
  1229. * is correct.
  1230. *
  1231. * some rules:
  1232. * 1. priv_ep->dequeue never equals to current_index.
  1233. * 2 priv_ep->enqueue never exceed priv_ep->dequeue
  1234. * 3. exception: priv_ep->enqueue == priv_ep->dequeue
  1235. * and priv_ep->free_trbs is zero.
  1236. * This case indicate that TR is full.
  1237. *
  1238. * At below two cases, the request have been handled.
  1239. * Case 1 - priv_ep->dequeue < current_index
  1240. * SR ... EQ ... DQ ... CI ... ER
  1241. * SR ... DQ ... CI ... EQ ... ER
  1242. *
  1243. * Case 2 - priv_ep->dequeue > current_index
  1244. * This situation takes place when CI go through the LINK TRB at the end of
  1245. * transfer ring.
  1246. * SR ... CI ... EQ ... DQ ... ER
  1247. */
  1248. static bool cdns3_trb_handled(struct cdns3_endpoint *priv_ep,
  1249. struct cdns3_request *priv_req)
  1250. {
  1251. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  1252. struct cdns3_trb *trb;
  1253. int current_index = 0;
  1254. int handled = 0;
  1255. int doorbell;
  1256. current_index = cdns3_get_dma_pos(priv_dev, priv_ep);
  1257. doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
  1258. /* current trb doesn't belong to this request */
  1259. if (priv_req->start_trb < priv_req->end_trb) {
  1260. if (priv_ep->dequeue > priv_req->end_trb)
  1261. goto finish;
  1262. if (priv_ep->dequeue < priv_req->start_trb)
  1263. goto finish;
  1264. }
  1265. if ((priv_req->start_trb > priv_req->end_trb) &&
  1266. (priv_ep->dequeue > priv_req->end_trb) &&
  1267. (priv_ep->dequeue < priv_req->start_trb))
  1268. goto finish;
  1269. if ((priv_req->start_trb == priv_req->end_trb) &&
  1270. (priv_ep->dequeue != priv_req->end_trb))
  1271. goto finish;
  1272. trb = &priv_ep->trb_pool[priv_ep->dequeue];
  1273. if ((le32_to_cpu(trb->control) & TRB_CYCLE) != priv_ep->ccs)
  1274. goto finish;
  1275. if (doorbell == 1 && current_index == priv_ep->dequeue)
  1276. goto finish;
  1277. /* The corner case for TRBS_PER_SEGMENT equal 2). */
  1278. if (TRBS_PER_SEGMENT == 2 && priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
  1279. handled = 1;
  1280. goto finish;
  1281. }
  1282. if (priv_ep->enqueue == priv_ep->dequeue &&
  1283. priv_ep->free_trbs == 0) {
  1284. handled = 1;
  1285. } else if (priv_ep->dequeue < current_index) {
  1286. if ((current_index == (priv_ep->num_trbs - 1)) &&
  1287. !priv_ep->dequeue)
  1288. goto finish;
  1289. handled = 1;
  1290. } else if (priv_ep->dequeue > current_index) {
  1291. handled = 1;
  1292. }
  1293. finish:
  1294. trace_cdns3_request_handled(priv_req, current_index, handled);
  1295. return handled;
  1296. }
  1297. static void cdns3_transfer_completed(struct cdns3_device *priv_dev,
  1298. struct cdns3_endpoint *priv_ep)
  1299. {
  1300. struct cdns3_request *priv_req;
  1301. struct usb_request *request;
  1302. struct cdns3_trb *trb;
  1303. bool request_handled = false;
  1304. bool transfer_end = false;
  1305. while (!list_empty(&priv_ep->pending_req_list)) {
  1306. request = cdns3_next_request(&priv_ep->pending_req_list);
  1307. priv_req = to_cdns3_request(request);
  1308. trb = priv_ep->trb_pool + priv_ep->dequeue;
  1309. /* The TRB was changed as link TRB, and the request was handled at ep_dequeue */
  1310. while (TRB_FIELD_TO_TYPE(le32_to_cpu(trb->control)) == TRB_LINK) {
  1311. /* ISO ep_traddr may stop at LINK TRB */
  1312. if (priv_ep->dequeue == cdns3_get_dma_pos(priv_dev, priv_ep) &&
  1313. priv_ep->type == USB_ENDPOINT_XFER_ISOC)
  1314. break;
  1315. trace_cdns3_complete_trb(priv_ep, trb);
  1316. cdns3_ep_inc_deq(priv_ep);
  1317. trb = priv_ep->trb_pool + priv_ep->dequeue;
  1318. }
  1319. if (!request->stream_id) {
  1320. /* Re-select endpoint. It could be changed by other CPU
  1321. * during handling usb_gadget_giveback_request.
  1322. */
  1323. cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
  1324. while (cdns3_trb_handled(priv_ep, priv_req)) {
  1325. priv_req->finished_trb++;
  1326. if (priv_req->finished_trb >= priv_req->num_of_trb)
  1327. request_handled = true;
  1328. trb = priv_ep->trb_pool + priv_ep->dequeue;
  1329. trace_cdns3_complete_trb(priv_ep, trb);
  1330. if (!transfer_end)
  1331. request->actual +=
  1332. TRB_LEN(le32_to_cpu(trb->length));
  1333. if (priv_req->num_of_trb > 1 &&
  1334. le32_to_cpu(trb->control) & TRB_SMM &&
  1335. le32_to_cpu(trb->control) & TRB_CHAIN)
  1336. transfer_end = true;
  1337. cdns3_ep_inc_deq(priv_ep);
  1338. }
  1339. if (request_handled) {
  1340. /* TRBs are duplicated by priv_ep->interval time for ISO IN */
  1341. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && priv_ep->dir)
  1342. request->actual /= priv_ep->interval;
  1343. cdns3_gadget_giveback(priv_ep, priv_req, 0);
  1344. request_handled = false;
  1345. transfer_end = false;
  1346. } else {
  1347. goto prepare_next_td;
  1348. }
  1349. if (priv_ep->type != USB_ENDPOINT_XFER_ISOC &&
  1350. TRBS_PER_SEGMENT == 2)
  1351. break;
  1352. } else {
  1353. /* Re-select endpoint. It could be changed by other CPU
  1354. * during handling usb_gadget_giveback_request.
  1355. */
  1356. cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
  1357. trb = priv_ep->trb_pool;
  1358. trace_cdns3_complete_trb(priv_ep, trb);
  1359. if (trb != priv_req->trb)
  1360. dev_warn(priv_dev->dev,
  1361. "request_trb=0x%p, queue_trb=0x%p\n",
  1362. priv_req->trb, trb);
  1363. request->actual += TRB_LEN(le32_to_cpu(trb->length));
  1364. if (!request->num_sgs ||
  1365. (request->num_sgs == (priv_ep->stream_sg_idx + 1))) {
  1366. priv_ep->stream_sg_idx = 0;
  1367. cdns3_gadget_giveback(priv_ep, priv_req, 0);
  1368. } else {
  1369. priv_ep->stream_sg_idx++;
  1370. cdns3_ep_run_stream_transfer(priv_ep, request);
  1371. }
  1372. break;
  1373. }
  1374. }
  1375. priv_ep->flags &= ~EP_PENDING_REQUEST;
  1376. prepare_next_td:
  1377. if (!(priv_ep->flags & EP_STALLED) &&
  1378. !(priv_ep->flags & EP_STALL_PENDING))
  1379. cdns3_start_all_request(priv_dev, priv_ep);
  1380. }
  1381. void cdns3_rearm_transfer(struct cdns3_endpoint *priv_ep, u8 rearm)
  1382. {
  1383. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  1384. cdns3_wa1_restore_cycle_bit(priv_ep);
  1385. if (rearm) {
  1386. trace_cdns3_ring(priv_ep);
  1387. /* Cycle Bit must be updated before arming DMA. */
  1388. wmb();
  1389. writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
  1390. __cdns3_gadget_wakeup(priv_dev);
  1391. trace_cdns3_doorbell_epx(priv_ep->name,
  1392. readl(&priv_dev->regs->ep_traddr));
  1393. }
  1394. }
  1395. static void cdns3_reprogram_tdl(struct cdns3_endpoint *priv_ep)
  1396. {
  1397. u16 tdl = priv_ep->pending_tdl;
  1398. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  1399. if (tdl > EP_CMD_TDL_MAX) {
  1400. tdl = EP_CMD_TDL_MAX;
  1401. priv_ep->pending_tdl -= EP_CMD_TDL_MAX;
  1402. } else {
  1403. priv_ep->pending_tdl = 0;
  1404. }
  1405. writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, &priv_dev->regs->ep_cmd);
  1406. }
  1407. /**
  1408. * cdns3_check_ep_interrupt_proceed - Processes interrupt related to endpoint
  1409. * @priv_ep: endpoint object
  1410. *
  1411. * Returns 0
  1412. */
  1413. static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep)
  1414. {
  1415. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  1416. u32 ep_sts_reg;
  1417. struct usb_request *deferred_request;
  1418. struct usb_request *pending_request;
  1419. u32 tdl = 0;
  1420. cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
  1421. trace_cdns3_epx_irq(priv_dev, priv_ep);
  1422. ep_sts_reg = readl(&priv_dev->regs->ep_sts);
  1423. writel(ep_sts_reg, &priv_dev->regs->ep_sts);
  1424. if ((ep_sts_reg & EP_STS_PRIME) && priv_ep->use_streams) {
  1425. bool dbusy = !!(ep_sts_reg & EP_STS_DBUSY);
  1426. tdl = cdns3_get_tdl(priv_dev);
  1427. /*
  1428. * Continue the previous transfer:
  1429. * There is some racing between ERDY and PRIME. The device send
  1430. * ERDY and almost in the same time Host send PRIME. It cause
  1431. * that host ignore the ERDY packet and driver has to send it
  1432. * again.
  1433. */
  1434. if (tdl && (dbusy || !EP_STS_BUFFEMPTY(ep_sts_reg) ||
  1435. EP_STS_HOSTPP(ep_sts_reg))) {
  1436. writel(EP_CMD_ERDY |
  1437. EP_CMD_ERDY_SID(priv_ep->last_stream_id),
  1438. &priv_dev->regs->ep_cmd);
  1439. ep_sts_reg &= ~(EP_STS_MD_EXIT | EP_STS_IOC);
  1440. } else {
  1441. priv_ep->prime_flag = true;
  1442. pending_request = cdns3_next_request(&priv_ep->pending_req_list);
  1443. deferred_request = cdns3_next_request(&priv_ep->deferred_req_list);
  1444. if (deferred_request && !pending_request) {
  1445. cdns3_start_all_request(priv_dev, priv_ep);
  1446. }
  1447. }
  1448. }
  1449. if (ep_sts_reg & EP_STS_TRBERR) {
  1450. if (priv_ep->flags & EP_STALL_PENDING &&
  1451. !(ep_sts_reg & EP_STS_DESCMIS &&
  1452. priv_dev->dev_ver < DEV_VER_V2)) {
  1453. cdns3_ep_stall_flush(priv_ep);
  1454. }
  1455. /*
  1456. * For isochronous transfer driver completes request on
  1457. * IOC or on TRBERR. IOC appears only when device receive
  1458. * OUT data packet. If host disable stream or lost some packet
  1459. * then the only way to finish all queued transfer is to do it
  1460. * on TRBERR event.
  1461. */
  1462. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC &&
  1463. !priv_ep->wa1_set) {
  1464. if (!priv_ep->dir) {
  1465. u32 ep_cfg = readl(&priv_dev->regs->ep_cfg);
  1466. ep_cfg &= ~EP_CFG_ENABLE;
  1467. writel(ep_cfg, &priv_dev->regs->ep_cfg);
  1468. priv_ep->flags &= ~EP_QUIRK_ISO_OUT_EN;
  1469. priv_ep->flags |= EP_UPDATE_EP_TRBADDR;
  1470. }
  1471. cdns3_transfer_completed(priv_dev, priv_ep);
  1472. } else if (!(priv_ep->flags & EP_STALLED) &&
  1473. !(priv_ep->flags & EP_STALL_PENDING)) {
  1474. if (priv_ep->flags & EP_DEFERRED_DRDY) {
  1475. priv_ep->flags &= ~EP_DEFERRED_DRDY;
  1476. cdns3_start_all_request(priv_dev, priv_ep);
  1477. } else {
  1478. cdns3_rearm_transfer(priv_ep,
  1479. priv_ep->wa1_set);
  1480. }
  1481. }
  1482. }
  1483. if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP) ||
  1484. (ep_sts_reg & EP_STS_IOT)) {
  1485. if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
  1486. if (ep_sts_reg & EP_STS_ISP)
  1487. priv_ep->flags |= EP_QUIRK_END_TRANSFER;
  1488. else
  1489. priv_ep->flags &= ~EP_QUIRK_END_TRANSFER;
  1490. }
  1491. if (!priv_ep->use_streams) {
  1492. if ((ep_sts_reg & EP_STS_IOC) ||
  1493. (ep_sts_reg & EP_STS_ISP)) {
  1494. cdns3_transfer_completed(priv_dev, priv_ep);
  1495. } else if ((priv_ep->flags & EP_TDLCHK_EN) &
  1496. priv_ep->pending_tdl) {
  1497. /* handle IOT with pending tdl */
  1498. cdns3_reprogram_tdl(priv_ep);
  1499. }
  1500. } else if (priv_ep->dir == USB_DIR_OUT) {
  1501. priv_ep->ep_sts_pending |= ep_sts_reg;
  1502. } else if (ep_sts_reg & EP_STS_IOT) {
  1503. cdns3_transfer_completed(priv_dev, priv_ep);
  1504. }
  1505. }
  1506. /*
  1507. * MD_EXIT interrupt sets when stream capable endpoint exits
  1508. * from MOVE DATA state of Bulk IN/OUT stream protocol state machine
  1509. */
  1510. if (priv_ep->dir == USB_DIR_OUT && (ep_sts_reg & EP_STS_MD_EXIT) &&
  1511. (priv_ep->ep_sts_pending & EP_STS_IOT) && priv_ep->use_streams) {
  1512. priv_ep->ep_sts_pending = 0;
  1513. cdns3_transfer_completed(priv_dev, priv_ep);
  1514. }
  1515. /*
  1516. * WA2: this condition should only be meet when
  1517. * priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET or
  1518. * priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN.
  1519. * In other cases this interrupt will be disabled.
  1520. */
  1521. if (ep_sts_reg & EP_STS_DESCMIS && priv_dev->dev_ver < DEV_VER_V2 &&
  1522. !(priv_ep->flags & EP_STALLED))
  1523. cdns3_wa2_descmissing_packet(priv_ep);
  1524. return 0;
  1525. }
  1526. static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev)
  1527. {
  1528. if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect)
  1529. priv_dev->gadget_driver->disconnect(&priv_dev->gadget);
  1530. }
  1531. /**
  1532. * cdns3_check_usb_interrupt_proceed - Processes interrupt related to device
  1533. * @priv_dev: extended gadget object
  1534. * @usb_ists: bitmap representation of device's reported interrupts
  1535. * (usb_ists register value)
  1536. */
  1537. static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev,
  1538. u32 usb_ists)
  1539. __must_hold(&priv_dev->lock)
  1540. {
  1541. int speed = 0;
  1542. trace_cdns3_usb_irq(priv_dev, usb_ists);
  1543. if (usb_ists & USB_ISTS_L1ENTI) {
  1544. /*
  1545. * WORKAROUND: CDNS3 controller has issue with hardware resuming
  1546. * from L1. To fix it, if any DMA transfer is pending driver
  1547. * must starts driving resume signal immediately.
  1548. */
  1549. if (readl(&priv_dev->regs->drbl))
  1550. __cdns3_gadget_wakeup(priv_dev);
  1551. }
  1552. /* Connection detected */
  1553. if (usb_ists & (USB_ISTS_CON2I | USB_ISTS_CONI)) {
  1554. speed = cdns3_get_speed(priv_dev);
  1555. priv_dev->gadget.speed = speed;
  1556. usb_gadget_set_state(&priv_dev->gadget, USB_STATE_POWERED);
  1557. cdns3_ep0_config(priv_dev);
  1558. }
  1559. /* Disconnection detected */
  1560. if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) {
  1561. spin_unlock(&priv_dev->lock);
  1562. cdns3_disconnect_gadget(priv_dev);
  1563. spin_lock(&priv_dev->lock);
  1564. priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
  1565. usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
  1566. cdns3_hw_reset_eps_config(priv_dev);
  1567. }
  1568. if (usb_ists & (USB_ISTS_L2ENTI | USB_ISTS_U3ENTI)) {
  1569. if (priv_dev->gadget_driver &&
  1570. priv_dev->gadget_driver->suspend) {
  1571. spin_unlock(&priv_dev->lock);
  1572. priv_dev->gadget_driver->suspend(&priv_dev->gadget);
  1573. spin_lock(&priv_dev->lock);
  1574. }
  1575. }
  1576. if (usb_ists & (USB_ISTS_L2EXTI | USB_ISTS_U3EXTI)) {
  1577. if (priv_dev->gadget_driver &&
  1578. priv_dev->gadget_driver->resume) {
  1579. spin_unlock(&priv_dev->lock);
  1580. priv_dev->gadget_driver->resume(&priv_dev->gadget);
  1581. spin_lock(&priv_dev->lock);
  1582. }
  1583. }
  1584. /* reset*/
  1585. if (usb_ists & (USB_ISTS_UWRESI | USB_ISTS_UHRESI | USB_ISTS_U2RESI)) {
  1586. if (priv_dev->gadget_driver) {
  1587. spin_unlock(&priv_dev->lock);
  1588. usb_gadget_udc_reset(&priv_dev->gadget,
  1589. priv_dev->gadget_driver);
  1590. spin_lock(&priv_dev->lock);
  1591. /*read again to check the actual speed*/
  1592. speed = cdns3_get_speed(priv_dev);
  1593. priv_dev->gadget.speed = speed;
  1594. cdns3_hw_reset_eps_config(priv_dev);
  1595. cdns3_ep0_config(priv_dev);
  1596. }
  1597. }
  1598. }
  1599. /**
  1600. * cdns3_device_irq_handler - interrupt handler for device part of controller
  1601. *
  1602. * @irq: irq number for cdns3 core device
  1603. * @data: structure of cdns3
  1604. *
  1605. * Returns IRQ_HANDLED or IRQ_NONE
  1606. */
  1607. static irqreturn_t cdns3_device_irq_handler(int irq, void *data)
  1608. {
  1609. struct cdns3_device *priv_dev = data;
  1610. struct cdns *cdns = dev_get_drvdata(priv_dev->dev);
  1611. irqreturn_t ret = IRQ_NONE;
  1612. u32 reg;
  1613. if (cdns->in_lpm)
  1614. return ret;
  1615. /* check USB device interrupt */
  1616. reg = readl(&priv_dev->regs->usb_ists);
  1617. if (reg) {
  1618. /* After masking interrupts the new interrupts won't be
  1619. * reported in usb_ists/ep_ists. In order to not lose some
  1620. * of them driver disables only detected interrupts.
  1621. * They will be enabled ASAP after clearing source of
  1622. * interrupt. This an unusual behavior only applies to
  1623. * usb_ists register.
  1624. */
  1625. reg = ~reg & readl(&priv_dev->regs->usb_ien);
  1626. /* mask deferred interrupt. */
  1627. writel(reg, &priv_dev->regs->usb_ien);
  1628. ret = IRQ_WAKE_THREAD;
  1629. }
  1630. /* check endpoint interrupt */
  1631. reg = readl(&priv_dev->regs->ep_ists);
  1632. if (reg) {
  1633. writel(0, &priv_dev->regs->ep_ien);
  1634. ret = IRQ_WAKE_THREAD;
  1635. }
  1636. return ret;
  1637. }
  1638. /**
  1639. * cdns3_device_thread_irq_handler - interrupt handler for device part
  1640. * of controller
  1641. *
  1642. * @irq: irq number for cdns3 core device
  1643. * @data: structure of cdns3
  1644. *
  1645. * Returns IRQ_HANDLED or IRQ_NONE
  1646. */
  1647. static irqreturn_t cdns3_device_thread_irq_handler(int irq, void *data)
  1648. {
  1649. struct cdns3_device *priv_dev = data;
  1650. irqreturn_t ret = IRQ_NONE;
  1651. unsigned long flags;
  1652. unsigned int bit;
  1653. unsigned long reg;
  1654. local_bh_disable();
  1655. spin_lock_irqsave(&priv_dev->lock, flags);
  1656. reg = readl(&priv_dev->regs->usb_ists);
  1657. if (reg) {
  1658. writel(reg, &priv_dev->regs->usb_ists);
  1659. writel(USB_IEN_INIT, &priv_dev->regs->usb_ien);
  1660. cdns3_check_usb_interrupt_proceed(priv_dev, reg);
  1661. ret = IRQ_HANDLED;
  1662. }
  1663. reg = readl(&priv_dev->regs->ep_ists);
  1664. /* handle default endpoint OUT */
  1665. if (reg & EP_ISTS_EP_OUT0) {
  1666. cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT);
  1667. ret = IRQ_HANDLED;
  1668. }
  1669. /* handle default endpoint IN */
  1670. if (reg & EP_ISTS_EP_IN0) {
  1671. cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN);
  1672. ret = IRQ_HANDLED;
  1673. }
  1674. /* check if interrupt from non default endpoint, if no exit */
  1675. reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0);
  1676. if (!reg)
  1677. goto irqend;
  1678. for_each_set_bit(bit, &reg,
  1679. sizeof(u32) * BITS_PER_BYTE) {
  1680. cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]);
  1681. ret = IRQ_HANDLED;
  1682. }
  1683. if (priv_dev->dev_ver < DEV_VER_V2 && priv_dev->using_streams)
  1684. cdns3_wa2_check_outq_status(priv_dev);
  1685. irqend:
  1686. writel(~0, &priv_dev->regs->ep_ien);
  1687. spin_unlock_irqrestore(&priv_dev->lock, flags);
  1688. local_bh_enable();
  1689. return ret;
  1690. }
  1691. /**
  1692. * cdns3_ep_onchip_buffer_reserve - Try to reserve onchip buf for EP
  1693. *
  1694. * The real reservation will occur during write to EP_CFG register,
  1695. * this function is used to check if the 'size' reservation is allowed.
  1696. *
  1697. * @priv_dev: extended gadget object
  1698. * @size: the size (KB) for EP would like to allocate
  1699. * @is_in: endpoint direction
  1700. *
  1701. * Return 0 if the required size can met or negative value on failure
  1702. */
  1703. static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
  1704. int size, int is_in)
  1705. {
  1706. int remained;
  1707. /* 2KB are reserved for EP0*/
  1708. remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2;
  1709. if (is_in) {
  1710. if (remained < size)
  1711. return -EPERM;
  1712. priv_dev->onchip_used_size += size;
  1713. } else {
  1714. int required;
  1715. /**
  1716. * ALL OUT EPs are shared the same chunk onchip memory, so
  1717. * driver checks if it already has assigned enough buffers
  1718. */
  1719. if (priv_dev->out_mem_is_allocated >= size)
  1720. return 0;
  1721. required = size - priv_dev->out_mem_is_allocated;
  1722. if (required > remained)
  1723. return -EPERM;
  1724. priv_dev->out_mem_is_allocated += required;
  1725. priv_dev->onchip_used_size += required;
  1726. }
  1727. return 0;
  1728. }
  1729. static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
  1730. struct cdns3_endpoint *priv_ep)
  1731. {
  1732. struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
  1733. /* For dev_ver > DEV_VER_V2 DMULT is configured per endpoint */
  1734. if (priv_dev->dev_ver <= DEV_VER_V2)
  1735. writel(USB_CONF_DMULT, &regs->usb_conf);
  1736. if (priv_dev->dev_ver == DEV_VER_V2)
  1737. writel(USB_CONF2_EN_TDL_TRB, &regs->usb_conf2);
  1738. if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) {
  1739. u32 mask;
  1740. if (priv_ep->dir)
  1741. mask = BIT(priv_ep->num + 16);
  1742. else
  1743. mask = BIT(priv_ep->num);
  1744. if (priv_ep->type != USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
  1745. cdns3_set_register_bit(&regs->tdl_from_trb, mask);
  1746. cdns3_set_register_bit(&regs->tdl_beh, mask);
  1747. cdns3_set_register_bit(&regs->tdl_beh2, mask);
  1748. cdns3_set_register_bit(&regs->dma_adv_td, mask);
  1749. }
  1750. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
  1751. cdns3_set_register_bit(&regs->tdl_from_trb, mask);
  1752. cdns3_set_register_bit(&regs->dtrans, mask);
  1753. }
  1754. }
  1755. /**
  1756. * cdns3_ep_config - Configure hardware endpoint
  1757. * @priv_ep: extended endpoint object
  1758. * @enable: set EP_CFG_ENABLE bit in ep_cfg register.
  1759. */
  1760. int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable)
  1761. {
  1762. bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
  1763. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  1764. u32 bEndpointAddress = priv_ep->num | priv_ep->dir;
  1765. u32 max_packet_size = priv_ep->wMaxPacketSize;
  1766. u8 maxburst = priv_ep->bMaxBurst;
  1767. u32 ep_cfg = 0;
  1768. u8 buffering;
  1769. int ret;
  1770. buffering = priv_dev->ep_buf_size - 1;
  1771. cdns3_configure_dmult(priv_dev, priv_ep);
  1772. switch (priv_ep->type) {
  1773. case USB_ENDPOINT_XFER_INT:
  1774. ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT);
  1775. if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir)
  1776. ep_cfg |= EP_CFG_TDL_CHK;
  1777. break;
  1778. case USB_ENDPOINT_XFER_BULK:
  1779. ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK);
  1780. if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir)
  1781. ep_cfg |= EP_CFG_TDL_CHK;
  1782. break;
  1783. default:
  1784. ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC);
  1785. buffering = (priv_ep->bMaxBurst + 1) * (priv_ep->mult + 1) - 1;
  1786. }
  1787. switch (priv_dev->gadget.speed) {
  1788. case USB_SPEED_FULL:
  1789. max_packet_size = is_iso_ep ? 1023 : 64;
  1790. break;
  1791. case USB_SPEED_HIGH:
  1792. max_packet_size = is_iso_ep ? 1024 : 512;
  1793. break;
  1794. case USB_SPEED_SUPER:
  1795. if (priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
  1796. max_packet_size = 1024;
  1797. maxburst = priv_dev->ep_buf_size - 1;
  1798. }
  1799. break;
  1800. default:
  1801. /* all other speed are not supported */
  1802. return -EINVAL;
  1803. }
  1804. if (max_packet_size == 1024)
  1805. priv_ep->trb_burst_size = 128;
  1806. else if (max_packet_size >= 512)
  1807. priv_ep->trb_burst_size = 64;
  1808. else
  1809. priv_ep->trb_burst_size = 16;
  1810. /*
  1811. * In versions preceding DEV_VER_V2, for example, iMX8QM, there exit the bugs
  1812. * in the DMA. These bugs occur when the trb_burst_size exceeds 16 and the
  1813. * address is not aligned to 128 Bytes (which is a product of the 64-bit AXI
  1814. * and AXI maximum burst length of 16 or 0xF+1, dma_axi_ctrl0[3:0]). This
  1815. * results in data corruption when it crosses the 4K border. The corruption
  1816. * specifically occurs from the position (4K - (address & 0x7F)) to 4K.
  1817. *
  1818. * So force trb_burst_size to 16 at such platform.
  1819. */
  1820. if (priv_dev->dev_ver < DEV_VER_V2)
  1821. priv_ep->trb_burst_size = 16;
  1822. buffering = min_t(u8, buffering, EP_CFG_BUFFERING_MAX);
  1823. maxburst = min_t(u8, maxburst, EP_CFG_MAXBURST_MAX);
  1824. /* onchip buffer is only allocated before configuration */
  1825. if (!priv_dev->hw_configured_flag) {
  1826. ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
  1827. !!priv_ep->dir);
  1828. if (ret) {
  1829. dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
  1830. return ret;
  1831. }
  1832. }
  1833. if (enable)
  1834. ep_cfg |= EP_CFG_ENABLE;
  1835. if (priv_ep->use_streams && priv_dev->gadget.speed >= USB_SPEED_SUPER) {
  1836. if (priv_dev->dev_ver >= DEV_VER_V3) {
  1837. u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
  1838. /*
  1839. * Stream capable endpoints are handled by using ep_tdl
  1840. * register. Other endpoints use TDL from TRB feature.
  1841. */
  1842. cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb,
  1843. mask);
  1844. }
  1845. /* Enable Stream Bit TDL chk and SID chk */
  1846. ep_cfg |= EP_CFG_STREAM_EN | EP_CFG_TDL_CHK | EP_CFG_SID_CHK;
  1847. }
  1848. ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
  1849. EP_CFG_MULT(priv_ep->mult) | /* must match EP setting */
  1850. EP_CFG_BUFFERING(buffering) |
  1851. EP_CFG_MAXBURST(maxburst);
  1852. cdns3_select_ep(priv_dev, bEndpointAddress);
  1853. writel(ep_cfg, &priv_dev->regs->ep_cfg);
  1854. priv_ep->flags |= EP_CONFIGURED;
  1855. dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
  1856. priv_ep->name, ep_cfg);
  1857. return 0;
  1858. }
  1859. /* Find correct direction for HW endpoint according to description */
  1860. static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc,
  1861. struct cdns3_endpoint *priv_ep)
  1862. {
  1863. return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) ||
  1864. (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc));
  1865. }
  1866. static struct
  1867. cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev,
  1868. struct usb_endpoint_descriptor *desc)
  1869. {
  1870. struct usb_ep *ep;
  1871. struct cdns3_endpoint *priv_ep;
  1872. list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
  1873. unsigned long num;
  1874. int ret;
  1875. /* ep name pattern likes epXin or epXout */
  1876. char c[2] = {ep->name[2], '\0'};
  1877. ret = kstrtoul(c, 10, &num);
  1878. if (ret)
  1879. return ERR_PTR(ret);
  1880. priv_ep = ep_to_cdns3_ep(ep);
  1881. if (cdns3_ep_dir_is_correct(desc, priv_ep)) {
  1882. if (!(priv_ep->flags & EP_CLAIMED)) {
  1883. priv_ep->num = num;
  1884. return priv_ep;
  1885. }
  1886. }
  1887. }
  1888. return ERR_PTR(-ENOENT);
  1889. }
  1890. /*
  1891. * Cadence IP has one limitation that all endpoints must be configured
  1892. * (Type & MaxPacketSize) before setting configuration through hardware
  1893. * register, it means we can't change endpoints configuration after
  1894. * set_configuration.
  1895. *
  1896. * This function set EP_CLAIMED flag which is added when the gadget driver
  1897. * uses usb_ep_autoconfig to configure specific endpoint;
  1898. * When the udc driver receives set_configurion request,
  1899. * it goes through all claimed endpoints, and configure all endpoints
  1900. * accordingly.
  1901. *
  1902. * At usb_ep_ops.enable/disable, we only enable and disable endpoint through
  1903. * ep_cfg register which can be changed after set_configuration, and do
  1904. * some software operation accordingly.
  1905. */
  1906. static struct
  1907. usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget,
  1908. struct usb_endpoint_descriptor *desc,
  1909. struct usb_ss_ep_comp_descriptor *comp_desc)
  1910. {
  1911. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  1912. struct cdns3_endpoint *priv_ep;
  1913. unsigned long flags;
  1914. priv_ep = cdns3_find_available_ep(priv_dev, desc);
  1915. if (IS_ERR(priv_ep)) {
  1916. dev_err(priv_dev->dev, "no available ep\n");
  1917. return NULL;
  1918. }
  1919. dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name);
  1920. spin_lock_irqsave(&priv_dev->lock, flags);
  1921. priv_ep->endpoint.desc = desc;
  1922. priv_ep->dir = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT;
  1923. priv_ep->type = usb_endpoint_type(desc);
  1924. priv_ep->flags |= EP_CLAIMED;
  1925. priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
  1926. priv_ep->wMaxPacketSize = usb_endpoint_maxp(desc);
  1927. priv_ep->mult = USB_EP_MAXP_MULT(priv_ep->wMaxPacketSize);
  1928. priv_ep->wMaxPacketSize &= USB_ENDPOINT_MAXP_MASK;
  1929. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && comp_desc) {
  1930. priv_ep->mult = USB_SS_MULT(comp_desc->bmAttributes) - 1;
  1931. priv_ep->bMaxBurst = comp_desc->bMaxBurst;
  1932. }
  1933. spin_unlock_irqrestore(&priv_dev->lock, flags);
  1934. return &priv_ep->endpoint;
  1935. }
  1936. /**
  1937. * cdns3_gadget_ep_alloc_request - Allocates request
  1938. * @ep: endpoint object associated with request
  1939. * @gfp_flags: gfp flags
  1940. *
  1941. * Returns allocated request address, NULL on allocation error
  1942. */
  1943. struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep,
  1944. gfp_t gfp_flags)
  1945. {
  1946. struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
  1947. struct cdns3_request *priv_req;
  1948. priv_req = kzalloc_obj(*priv_req, gfp_flags);
  1949. if (!priv_req)
  1950. return NULL;
  1951. priv_req->priv_ep = priv_ep;
  1952. trace_cdns3_alloc_request(priv_req);
  1953. return &priv_req->request;
  1954. }
  1955. /**
  1956. * cdns3_gadget_ep_free_request - Free memory occupied by request
  1957. * @ep: endpoint object associated with request
  1958. * @request: request to free memory
  1959. */
  1960. void cdns3_gadget_ep_free_request(struct usb_ep *ep,
  1961. struct usb_request *request)
  1962. {
  1963. struct cdns3_request *priv_req = to_cdns3_request(request);
  1964. if (priv_req->aligned_buf)
  1965. priv_req->aligned_buf->in_use = 0;
  1966. trace_cdns3_free_request(priv_req);
  1967. kfree(priv_req);
  1968. }
  1969. /**
  1970. * cdns3_gadget_ep_enable - Enable endpoint
  1971. * @ep: endpoint object
  1972. * @desc: endpoint descriptor
  1973. *
  1974. * Returns 0 on success, error code elsewhere
  1975. */
  1976. static int cdns3_gadget_ep_enable(struct usb_ep *ep,
  1977. const struct usb_endpoint_descriptor *desc)
  1978. {
  1979. struct cdns3_endpoint *priv_ep;
  1980. struct cdns3_device *priv_dev;
  1981. const struct usb_ss_ep_comp_descriptor *comp_desc;
  1982. u32 reg = EP_STS_EN_TRBERREN;
  1983. u32 bEndpointAddress;
  1984. unsigned long flags;
  1985. int enable = 1;
  1986. int ret = 0;
  1987. int val;
  1988. if (!ep) {
  1989. pr_debug("usbss: ep not configured?\n");
  1990. return -EINVAL;
  1991. }
  1992. priv_ep = ep_to_cdns3_ep(ep);
  1993. priv_dev = priv_ep->cdns3_dev;
  1994. comp_desc = priv_ep->endpoint.comp_desc;
  1995. if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  1996. dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
  1997. return -EINVAL;
  1998. }
  1999. if (!desc->wMaxPacketSize) {
  2000. dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
  2001. return -EINVAL;
  2002. }
  2003. if (dev_WARN_ONCE(priv_dev->dev, priv_ep->flags & EP_ENABLED,
  2004. "%s is already enabled\n", priv_ep->name))
  2005. return 0;
  2006. spin_lock_irqsave(&priv_dev->lock, flags);
  2007. priv_ep->endpoint.desc = desc;
  2008. priv_ep->type = usb_endpoint_type(desc);
  2009. priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
  2010. if (priv_ep->interval > ISO_MAX_INTERVAL &&
  2011. priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
  2012. dev_err(priv_dev->dev, "Driver is limited to %d period\n",
  2013. ISO_MAX_INTERVAL);
  2014. ret = -EINVAL;
  2015. goto exit;
  2016. }
  2017. bEndpointAddress = priv_ep->num | priv_ep->dir;
  2018. cdns3_select_ep(priv_dev, bEndpointAddress);
  2019. /*
  2020. * For some versions of controller at some point during ISO OUT traffic
  2021. * DMA reads Transfer Ring for the EP which has never got doorbell.
  2022. * This issue was detected only on simulation, but to avoid this issue
  2023. * driver add protection against it. To fix it driver enable ISO OUT
  2024. * endpoint before setting DRBL. This special treatment of ISO OUT
  2025. * endpoints are recommended by controller specification.
  2026. */
  2027. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
  2028. enable = 0;
  2029. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  2030. /*
  2031. * Enable stream support (SS mode) related interrupts
  2032. * in EP_STS_EN Register
  2033. */
  2034. if (priv_dev->gadget.speed >= USB_SPEED_SUPER) {
  2035. reg |= EP_STS_EN_IOTEN | EP_STS_EN_PRIMEEEN |
  2036. EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN |
  2037. EP_STS_EN_STREAMREN;
  2038. priv_ep->use_streams = true;
  2039. ret = cdns3_ep_config(priv_ep, enable);
  2040. priv_dev->using_streams |= true;
  2041. }
  2042. } else {
  2043. ret = cdns3_ep_config(priv_ep, enable);
  2044. }
  2045. if (ret)
  2046. goto exit;
  2047. ret = cdns3_allocate_trb_pool(priv_ep);
  2048. if (ret)
  2049. goto exit;
  2050. bEndpointAddress = priv_ep->num | priv_ep->dir;
  2051. cdns3_select_ep(priv_dev, bEndpointAddress);
  2052. trace_cdns3_gadget_ep_enable(priv_ep);
  2053. writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
  2054. ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2055. !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
  2056. 1, 1000);
  2057. if (unlikely(ret)) {
  2058. cdns3_free_trb_pool(priv_ep);
  2059. ret = -EINVAL;
  2060. goto exit;
  2061. }
  2062. /* enable interrupt for selected endpoint */
  2063. cdns3_set_register_bit(&priv_dev->regs->ep_ien,
  2064. BIT(cdns3_ep_addr_to_index(bEndpointAddress)));
  2065. if (priv_dev->dev_ver < DEV_VER_V2)
  2066. cdns3_wa2_enable_detection(priv_dev, priv_ep, reg);
  2067. writel(reg, &priv_dev->regs->ep_sts_en);
  2068. ep->desc = desc;
  2069. priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING |
  2070. EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN);
  2071. priv_ep->flags |= EP_ENABLED | EP_UPDATE_EP_TRBADDR;
  2072. priv_ep->wa1_set = 0;
  2073. priv_ep->enqueue = 0;
  2074. priv_ep->dequeue = 0;
  2075. reg = readl(&priv_dev->regs->ep_sts);
  2076. priv_ep->pcs = !!EP_STS_CCS(reg);
  2077. priv_ep->ccs = !!EP_STS_CCS(reg);
  2078. /* one TRB is reserved for link TRB used in DMULT mode*/
  2079. priv_ep->free_trbs = priv_ep->num_trbs - 1;
  2080. exit:
  2081. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2082. return ret;
  2083. }
  2084. /**
  2085. * cdns3_gadget_ep_disable - Disable endpoint
  2086. * @ep: endpoint object
  2087. *
  2088. * Returns 0 on success, error code elsewhere
  2089. */
  2090. static int cdns3_gadget_ep_disable(struct usb_ep *ep)
  2091. {
  2092. struct cdns3_endpoint *priv_ep;
  2093. struct cdns3_request *priv_req;
  2094. struct cdns3_device *priv_dev;
  2095. struct usb_request *request;
  2096. unsigned long flags;
  2097. int ret = 0;
  2098. u32 ep_cfg;
  2099. int val;
  2100. if (!ep) {
  2101. pr_err("usbss: invalid parameters\n");
  2102. return -EINVAL;
  2103. }
  2104. priv_ep = ep_to_cdns3_ep(ep);
  2105. priv_dev = priv_ep->cdns3_dev;
  2106. if (dev_WARN_ONCE(priv_dev->dev, !(priv_ep->flags & EP_ENABLED),
  2107. "%s is already disabled\n", priv_ep->name))
  2108. return 0;
  2109. spin_lock_irqsave(&priv_dev->lock, flags);
  2110. trace_cdns3_gadget_ep_disable(priv_ep);
  2111. cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
  2112. ep_cfg = readl(&priv_dev->regs->ep_cfg);
  2113. ep_cfg &= ~EP_CFG_ENABLE;
  2114. writel(ep_cfg, &priv_dev->regs->ep_cfg);
  2115. /**
  2116. * Driver needs some time before resetting endpoint.
  2117. * It need waits for clearing DBUSY bit or for timeout expired.
  2118. * 10us is enough time for controller to stop transfer.
  2119. */
  2120. readl_poll_timeout_atomic(&priv_dev->regs->ep_sts, val,
  2121. !(val & EP_STS_DBUSY), 1, 10);
  2122. writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
  2123. readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2124. !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
  2125. 1, 1000);
  2126. if (unlikely(ret))
  2127. dev_err(priv_dev->dev, "Timeout: %s resetting failed.\n",
  2128. priv_ep->name);
  2129. while (!list_empty(&priv_ep->pending_req_list)) {
  2130. request = cdns3_next_request(&priv_ep->pending_req_list);
  2131. cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
  2132. -ESHUTDOWN);
  2133. }
  2134. while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
  2135. priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
  2136. list_del_init(&priv_req->list);
  2137. kfree(priv_req->request.buf);
  2138. cdns3_gadget_ep_free_request(&priv_ep->endpoint,
  2139. &priv_req->request);
  2140. --priv_ep->wa2_counter;
  2141. }
  2142. while (!list_empty(&priv_ep->deferred_req_list)) {
  2143. request = cdns3_next_request(&priv_ep->deferred_req_list);
  2144. cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
  2145. -ESHUTDOWN);
  2146. }
  2147. priv_ep->descmis_req = NULL;
  2148. ep->desc = NULL;
  2149. priv_ep->flags &= ~EP_ENABLED;
  2150. priv_ep->use_streams = false;
  2151. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2152. return ret;
  2153. }
  2154. /**
  2155. * __cdns3_gadget_ep_queue - Transfer data on endpoint
  2156. * @ep: endpoint object
  2157. * @request: request object
  2158. * @gfp_flags: gfp flags
  2159. *
  2160. * Returns 0 on success, error code elsewhere
  2161. */
  2162. static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
  2163. struct usb_request *request,
  2164. gfp_t gfp_flags)
  2165. {
  2166. struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
  2167. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  2168. struct cdns3_request *priv_req;
  2169. int ret = 0;
  2170. if (!ep->desc)
  2171. return -ESHUTDOWN;
  2172. request->actual = 0;
  2173. request->status = -EINPROGRESS;
  2174. priv_req = to_cdns3_request(request);
  2175. trace_cdns3_ep_queue(priv_req);
  2176. if (priv_dev->dev_ver < DEV_VER_V2) {
  2177. ret = cdns3_wa2_gadget_ep_queue(priv_dev, priv_ep,
  2178. priv_req);
  2179. if (ret == EINPROGRESS)
  2180. return 0;
  2181. }
  2182. ret = cdns3_prepare_aligned_request_buf(priv_req);
  2183. if (ret < 0)
  2184. return ret;
  2185. if (likely(!(priv_req->flags & REQUEST_UNALIGNED))) {
  2186. ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request,
  2187. usb_endpoint_dir_in(ep->desc));
  2188. if (ret)
  2189. return ret;
  2190. }
  2191. list_add_tail(&request->list, &priv_ep->deferred_req_list);
  2192. /*
  2193. * For stream capable endpoint if prime irq flag is set then only start
  2194. * request.
  2195. * If hardware endpoint configuration has not been set yet then
  2196. * just queue request in deferred list. Transfer will be started in
  2197. * cdns3_set_hw_configuration.
  2198. */
  2199. if (!request->stream_id) {
  2200. if (priv_dev->hw_configured_flag &&
  2201. !(priv_ep->flags & EP_STALLED) &&
  2202. !(priv_ep->flags & EP_STALL_PENDING))
  2203. cdns3_start_all_request(priv_dev, priv_ep);
  2204. } else {
  2205. if (priv_dev->hw_configured_flag && priv_ep->prime_flag)
  2206. cdns3_start_all_request(priv_dev, priv_ep);
  2207. }
  2208. return 0;
  2209. }
  2210. static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  2211. gfp_t gfp_flags)
  2212. {
  2213. struct usb_request *zlp_request;
  2214. struct cdns3_endpoint *priv_ep;
  2215. struct cdns3_device *priv_dev;
  2216. unsigned long flags;
  2217. int ret;
  2218. if (!request || !ep)
  2219. return -EINVAL;
  2220. priv_ep = ep_to_cdns3_ep(ep);
  2221. priv_dev = priv_ep->cdns3_dev;
  2222. spin_lock_irqsave(&priv_dev->lock, flags);
  2223. ret = __cdns3_gadget_ep_queue(ep, request, gfp_flags);
  2224. if (ret == 0 && request->zero && request->length &&
  2225. (request->length % ep->maxpacket == 0)) {
  2226. struct cdns3_request *priv_req;
  2227. zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
  2228. zlp_request->buf = priv_dev->zlp_buf;
  2229. zlp_request->length = 0;
  2230. priv_req = to_cdns3_request(zlp_request);
  2231. priv_req->flags |= REQUEST_ZLP;
  2232. dev_dbg(priv_dev->dev, "Queuing ZLP for endpoint: %s\n",
  2233. priv_ep->name);
  2234. ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags);
  2235. }
  2236. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2237. return ret;
  2238. }
  2239. /**
  2240. * cdns3_gadget_ep_dequeue - Remove request from transfer queue
  2241. * @ep: endpoint object associated with request
  2242. * @request: request object
  2243. *
  2244. * Returns 0 on success, error code elsewhere
  2245. */
  2246. int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
  2247. struct usb_request *request)
  2248. {
  2249. struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
  2250. struct cdns3_device *priv_dev;
  2251. struct usb_request *req, *req_temp;
  2252. struct cdns3_request *priv_req;
  2253. struct cdns3_trb *link_trb;
  2254. u8 req_on_hw_ring = 0;
  2255. unsigned long flags;
  2256. int ret = 0;
  2257. int val;
  2258. if (!ep || !request || !ep->desc)
  2259. return -EINVAL;
  2260. priv_dev = priv_ep->cdns3_dev;
  2261. spin_lock_irqsave(&priv_dev->lock, flags);
  2262. priv_req = to_cdns3_request(request);
  2263. trace_cdns3_ep_dequeue(priv_req);
  2264. cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
  2265. list_for_each_entry_safe(req, req_temp, &priv_ep->pending_req_list,
  2266. list) {
  2267. if (request == req) {
  2268. req_on_hw_ring = 1;
  2269. goto found;
  2270. }
  2271. }
  2272. list_for_each_entry_safe(req, req_temp, &priv_ep->deferred_req_list,
  2273. list) {
  2274. if (request == req)
  2275. goto found;
  2276. }
  2277. goto not_found;
  2278. found:
  2279. link_trb = priv_req->trb;
  2280. /* Update ring only if removed request is on pending_req_list list */
  2281. if (req_on_hw_ring && link_trb) {
  2282. /* Stop DMA */
  2283. writel(EP_CMD_DFLUSH, &priv_dev->regs->ep_cmd);
  2284. /* wait for DFLUSH cleared */
  2285. readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2286. !(val & EP_CMD_DFLUSH), 1, 1000);
  2287. link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma +
  2288. ((priv_req->end_trb + 1) * TRB_SIZE)));
  2289. link_trb->control = cpu_to_le32((le32_to_cpu(link_trb->control) & TRB_CYCLE) |
  2290. TRB_TYPE(TRB_LINK) | TRB_CHAIN);
  2291. if (priv_ep->wa1_trb == priv_req->trb)
  2292. cdns3_wa1_restore_cycle_bit(priv_ep);
  2293. }
  2294. cdns3_gadget_giveback(priv_ep, priv_req, -ECONNRESET);
  2295. req = cdns3_next_request(&priv_ep->pending_req_list);
  2296. if (req)
  2297. cdns3_rearm_transfer(priv_ep, 1);
  2298. not_found:
  2299. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2300. return ret;
  2301. }
  2302. /**
  2303. * __cdns3_gadget_ep_set_halt - Sets stall on selected endpoint
  2304. * Should be called after acquiring spin_lock and selecting ep
  2305. * @priv_ep: endpoint object to set stall on.
  2306. */
  2307. void __cdns3_gadget_ep_set_halt(struct cdns3_endpoint *priv_ep)
  2308. {
  2309. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  2310. trace_cdns3_halt(priv_ep, 1, 0);
  2311. if (!(priv_ep->flags & EP_STALLED)) {
  2312. u32 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
  2313. if (!(ep_sts_reg & EP_STS_DBUSY))
  2314. cdns3_ep_stall_flush(priv_ep);
  2315. else
  2316. priv_ep->flags |= EP_STALL_PENDING;
  2317. }
  2318. }
  2319. /**
  2320. * __cdns3_gadget_ep_clear_halt - Clears stall on selected endpoint
  2321. * Should be called after acquiring spin_lock and selecting ep
  2322. * @priv_ep: endpoint object to clear stall on
  2323. */
  2324. int __cdns3_gadget_ep_clear_halt(struct cdns3_endpoint *priv_ep)
  2325. {
  2326. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  2327. struct usb_request *request;
  2328. struct cdns3_request *priv_req;
  2329. struct cdns3_trb *trb = NULL;
  2330. struct cdns3_trb trb_tmp;
  2331. int ret;
  2332. int val;
  2333. trace_cdns3_halt(priv_ep, 0, 0);
  2334. request = cdns3_next_request(&priv_ep->pending_req_list);
  2335. if (request) {
  2336. priv_req = to_cdns3_request(request);
  2337. trb = priv_req->trb;
  2338. if (trb) {
  2339. trb_tmp = *trb;
  2340. trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE);
  2341. }
  2342. }
  2343. writel(EP_CMD_CSTALL | EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
  2344. /* wait for EPRST cleared */
  2345. ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2346. !(val & EP_CMD_EPRST), 1, 100);
  2347. if (ret)
  2348. return -EINVAL;
  2349. priv_ep->flags &= ~(EP_STALLED | EP_STALL_PENDING);
  2350. if (request) {
  2351. if (trb)
  2352. *trb = trb_tmp;
  2353. cdns3_rearm_transfer(priv_ep, 1);
  2354. }
  2355. cdns3_start_all_request(priv_dev, priv_ep);
  2356. return ret;
  2357. }
  2358. /**
  2359. * cdns3_gadget_ep_set_halt - Sets/clears stall on selected endpoint
  2360. * @ep: endpoint object to set/clear stall on
  2361. * @value: 1 for set stall, 0 for clear stall
  2362. *
  2363. * Returns 0 on success, error code elsewhere
  2364. */
  2365. int cdns3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  2366. {
  2367. struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
  2368. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  2369. unsigned long flags;
  2370. int ret = 0;
  2371. if (!(priv_ep->flags & EP_ENABLED))
  2372. return -EPERM;
  2373. spin_lock_irqsave(&priv_dev->lock, flags);
  2374. cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
  2375. if (!value) {
  2376. priv_ep->flags &= ~EP_WEDGE;
  2377. ret = __cdns3_gadget_ep_clear_halt(priv_ep);
  2378. } else {
  2379. __cdns3_gadget_ep_set_halt(priv_ep);
  2380. }
  2381. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2382. return ret;
  2383. }
  2384. extern const struct usb_ep_ops cdns3_gadget_ep0_ops;
  2385. static const struct usb_ep_ops cdns3_gadget_ep_ops = {
  2386. .enable = cdns3_gadget_ep_enable,
  2387. .disable = cdns3_gadget_ep_disable,
  2388. .alloc_request = cdns3_gadget_ep_alloc_request,
  2389. .free_request = cdns3_gadget_ep_free_request,
  2390. .queue = cdns3_gadget_ep_queue,
  2391. .dequeue = cdns3_gadget_ep_dequeue,
  2392. .set_halt = cdns3_gadget_ep_set_halt,
  2393. .set_wedge = cdns3_gadget_ep_set_wedge,
  2394. };
  2395. /**
  2396. * cdns3_gadget_get_frame - Returns number of actual ITP frame
  2397. * @gadget: gadget object
  2398. *
  2399. * Returns number of actual ITP frame
  2400. */
  2401. static int cdns3_gadget_get_frame(struct usb_gadget *gadget)
  2402. {
  2403. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2404. return readl(&priv_dev->regs->usb_itpn);
  2405. }
  2406. int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev)
  2407. {
  2408. enum usb_device_speed speed;
  2409. speed = cdns3_get_speed(priv_dev);
  2410. if (speed >= USB_SPEED_SUPER)
  2411. return 0;
  2412. /* Start driving resume signaling to indicate remote wakeup. */
  2413. writel(USB_CONF_LGO_L0, &priv_dev->regs->usb_conf);
  2414. return 0;
  2415. }
  2416. static int cdns3_gadget_wakeup(struct usb_gadget *gadget)
  2417. {
  2418. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2419. unsigned long flags;
  2420. int ret = 0;
  2421. spin_lock_irqsave(&priv_dev->lock, flags);
  2422. ret = __cdns3_gadget_wakeup(priv_dev);
  2423. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2424. return ret;
  2425. }
  2426. static int cdns3_gadget_set_selfpowered(struct usb_gadget *gadget,
  2427. int is_selfpowered)
  2428. {
  2429. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2430. unsigned long flags;
  2431. spin_lock_irqsave(&priv_dev->lock, flags);
  2432. priv_dev->is_selfpowered = !!is_selfpowered;
  2433. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2434. return 0;
  2435. }
  2436. static int cdns3_gadget_pullup(struct usb_gadget *gadget, int is_on)
  2437. {
  2438. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2439. if (is_on) {
  2440. writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
  2441. } else {
  2442. writel(~0, &priv_dev->regs->ep_ists);
  2443. writel(~0, &priv_dev->regs->usb_ists);
  2444. writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
  2445. }
  2446. return 0;
  2447. }
  2448. static void cdns3_gadget_config(struct cdns3_device *priv_dev)
  2449. {
  2450. struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
  2451. u32 reg;
  2452. cdns3_ep0_config(priv_dev);
  2453. /* enable interrupts for endpoint 0 (in and out) */
  2454. writel(EP_IEN_EP_OUT0 | EP_IEN_EP_IN0, &regs->ep_ien);
  2455. /*
  2456. * Driver needs to modify LFPS minimal U1 Exit time for DEV_VER_TI_V1
  2457. * revision of controller.
  2458. */
  2459. if (priv_dev->dev_ver == DEV_VER_TI_V1) {
  2460. reg = readl(&regs->dbg_link1);
  2461. reg &= ~DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_MASK;
  2462. reg |= DBG_LINK1_LFPS_MIN_GEN_U1_EXIT(0x55) |
  2463. DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_SET;
  2464. writel(reg, &regs->dbg_link1);
  2465. }
  2466. /*
  2467. * By default some platforms has set protected access to memory.
  2468. * This cause problem with cache, so driver restore non-secure
  2469. * access to memory.
  2470. */
  2471. reg = readl(&regs->dma_axi_ctrl);
  2472. reg |= DMA_AXI_CTRL_MARPROT(DMA_AXI_CTRL_NON_SECURE) |
  2473. DMA_AXI_CTRL_MAWPROT(DMA_AXI_CTRL_NON_SECURE);
  2474. writel(reg, &regs->dma_axi_ctrl);
  2475. /* enable generic interrupt*/
  2476. writel(USB_IEN_INIT, &regs->usb_ien);
  2477. writel(USB_CONF_CLK2OFFDS | USB_CONF_L1DS, &regs->usb_conf);
  2478. /* keep Fast Access bit */
  2479. writel(PUSB_PWR_FST_REG_ACCESS, &priv_dev->regs->usb_pwr);
  2480. cdns3_configure_dmult(priv_dev, NULL);
  2481. }
  2482. /**
  2483. * cdns3_gadget_udc_start - Gadget start
  2484. * @gadget: gadget object
  2485. * @driver: driver which operates on this gadget
  2486. *
  2487. * Returns 0 on success, error code elsewhere
  2488. */
  2489. static int cdns3_gadget_udc_start(struct usb_gadget *gadget,
  2490. struct usb_gadget_driver *driver)
  2491. {
  2492. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2493. unsigned long flags;
  2494. enum usb_device_speed max_speed = driver->max_speed;
  2495. spin_lock_irqsave(&priv_dev->lock, flags);
  2496. priv_dev->gadget_driver = driver;
  2497. /* limit speed if necessary */
  2498. max_speed = min(driver->max_speed, gadget->max_speed);
  2499. switch (max_speed) {
  2500. case USB_SPEED_FULL:
  2501. writel(USB_CONF_SFORCE_FS, &priv_dev->regs->usb_conf);
  2502. writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
  2503. break;
  2504. case USB_SPEED_HIGH:
  2505. writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
  2506. break;
  2507. case USB_SPEED_SUPER:
  2508. break;
  2509. default:
  2510. dev_err(priv_dev->dev,
  2511. "invalid maximum_speed parameter %d\n",
  2512. max_speed);
  2513. fallthrough;
  2514. case USB_SPEED_UNKNOWN:
  2515. /* default to superspeed */
  2516. max_speed = USB_SPEED_SUPER;
  2517. break;
  2518. }
  2519. cdns3_gadget_config(priv_dev);
  2520. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2521. return 0;
  2522. }
  2523. /**
  2524. * cdns3_gadget_udc_stop - Stops gadget
  2525. * @gadget: gadget object
  2526. *
  2527. * Returns 0
  2528. */
  2529. static int cdns3_gadget_udc_stop(struct usb_gadget *gadget)
  2530. {
  2531. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2532. struct cdns3_endpoint *priv_ep;
  2533. u32 bEndpointAddress;
  2534. struct usb_ep *ep;
  2535. int val;
  2536. priv_dev->gadget_driver = NULL;
  2537. priv_dev->onchip_used_size = 0;
  2538. priv_dev->out_mem_is_allocated = 0;
  2539. priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
  2540. list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
  2541. priv_ep = ep_to_cdns3_ep(ep);
  2542. bEndpointAddress = priv_ep->num | priv_ep->dir;
  2543. cdns3_select_ep(priv_dev, bEndpointAddress);
  2544. writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
  2545. readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2546. !(val & EP_CMD_EPRST), 1, 100);
  2547. priv_ep->flags &= ~EP_CLAIMED;
  2548. }
  2549. /* disable interrupt for device */
  2550. writel(0, &priv_dev->regs->usb_ien);
  2551. writel(0, &priv_dev->regs->usb_pwr);
  2552. writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
  2553. return 0;
  2554. }
  2555. /**
  2556. * cdns3_gadget_check_config - ensure cdns3 can support the USB configuration
  2557. * @gadget: pointer to the USB gadget
  2558. *
  2559. * Used to record the maximum number of endpoints being used in a USB composite
  2560. * device. (across all configurations) This is to be used in the calculation
  2561. * of the TXFIFO sizes when resizing internal memory for individual endpoints.
  2562. * It will help ensured that the resizing logic reserves enough space for at
  2563. * least one max packet.
  2564. */
  2565. static int cdns3_gadget_check_config(struct usb_gadget *gadget)
  2566. {
  2567. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2568. struct cdns3_endpoint *priv_ep;
  2569. struct usb_ep *ep;
  2570. int n_in = 0;
  2571. int iso = 0;
  2572. int out = 1;
  2573. int total;
  2574. int n;
  2575. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  2576. priv_ep = ep_to_cdns3_ep(ep);
  2577. if (!(priv_ep->flags & EP_CLAIMED))
  2578. continue;
  2579. n = (priv_ep->mult + 1) * (priv_ep->bMaxBurst + 1);
  2580. if (ep->address & USB_DIR_IN) {
  2581. /*
  2582. * ISO transfer: DMA start move data when get ISO, only transfer
  2583. * data as min(TD size, iso). No benefit for allocate bigger
  2584. * internal memory than 'iso'.
  2585. */
  2586. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
  2587. iso += n;
  2588. else
  2589. n_in++;
  2590. } else {
  2591. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
  2592. out = max_t(int, out, n);
  2593. }
  2594. }
  2595. /* 2KB are reserved for EP0, 1KB for out*/
  2596. total = 2 + n_in + out + iso;
  2597. if (total > priv_dev->onchip_buffers)
  2598. return -ENOMEM;
  2599. priv_dev->ep_buf_size = (priv_dev->onchip_buffers - 2 - iso) / (n_in + out);
  2600. return 0;
  2601. }
  2602. static const struct usb_gadget_ops cdns3_gadget_ops = {
  2603. .get_frame = cdns3_gadget_get_frame,
  2604. .wakeup = cdns3_gadget_wakeup,
  2605. .set_selfpowered = cdns3_gadget_set_selfpowered,
  2606. .pullup = cdns3_gadget_pullup,
  2607. .udc_start = cdns3_gadget_udc_start,
  2608. .udc_stop = cdns3_gadget_udc_stop,
  2609. .match_ep = cdns3_gadget_match_ep,
  2610. .check_config = cdns3_gadget_check_config,
  2611. };
  2612. static void cdns3_free_all_eps(struct cdns3_device *priv_dev)
  2613. {
  2614. int i;
  2615. /* ep0 OUT point to ep0 IN. */
  2616. priv_dev->eps[16] = NULL;
  2617. for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
  2618. if (priv_dev->eps[i]) {
  2619. cdns3_free_trb_pool(priv_dev->eps[i]);
  2620. devm_kfree(priv_dev->dev, priv_dev->eps[i]);
  2621. }
  2622. }
  2623. /**
  2624. * cdns3_init_eps - Initializes software endpoints of gadget
  2625. * @priv_dev: extended gadget object
  2626. *
  2627. * Returns 0 on success, error code elsewhere
  2628. */
  2629. static int cdns3_init_eps(struct cdns3_device *priv_dev)
  2630. {
  2631. u32 ep_enabled_reg, iso_ep_reg;
  2632. struct cdns3_endpoint *priv_ep;
  2633. int ep_dir, ep_number;
  2634. u32 ep_mask;
  2635. int ret = 0;
  2636. int i;
  2637. /* Read it from USB_CAP3 to USB_CAP5 */
  2638. ep_enabled_reg = readl(&priv_dev->regs->usb_cap3);
  2639. iso_ep_reg = readl(&priv_dev->regs->usb_cap4);
  2640. dev_dbg(priv_dev->dev, "Initializing non-zero endpoints\n");
  2641. for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
  2642. ep_dir = i >> 4; /* i div 16 */
  2643. ep_number = i & 0xF; /* i % 16 */
  2644. ep_mask = BIT(i);
  2645. if (!(ep_enabled_reg & ep_mask))
  2646. continue;
  2647. if (ep_dir && !ep_number) {
  2648. priv_dev->eps[i] = priv_dev->eps[0];
  2649. continue;
  2650. }
  2651. priv_ep = devm_kzalloc(priv_dev->dev, sizeof(*priv_ep),
  2652. GFP_KERNEL);
  2653. if (!priv_ep)
  2654. goto err;
  2655. /* set parent of endpoint object */
  2656. priv_ep->cdns3_dev = priv_dev;
  2657. priv_dev->eps[i] = priv_ep;
  2658. priv_ep->num = ep_number;
  2659. priv_ep->dir = ep_dir ? USB_DIR_IN : USB_DIR_OUT;
  2660. if (!ep_number) {
  2661. ret = cdns3_init_ep0(priv_dev, priv_ep);
  2662. if (ret) {
  2663. dev_err(priv_dev->dev, "Failed to init ep0\n");
  2664. goto err;
  2665. }
  2666. } else {
  2667. snprintf(priv_ep->name, sizeof(priv_ep->name), "ep%d%s",
  2668. ep_number, !!ep_dir ? "in" : "out");
  2669. priv_ep->endpoint.name = priv_ep->name;
  2670. usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
  2671. CDNS3_EP_MAX_PACKET_LIMIT);
  2672. priv_ep->endpoint.max_streams = CDNS3_EP_MAX_STREAMS;
  2673. priv_ep->endpoint.ops = &cdns3_gadget_ep_ops;
  2674. if (ep_dir)
  2675. priv_ep->endpoint.caps.dir_in = 1;
  2676. else
  2677. priv_ep->endpoint.caps.dir_out = 1;
  2678. if (iso_ep_reg & ep_mask)
  2679. priv_ep->endpoint.caps.type_iso = 1;
  2680. priv_ep->endpoint.caps.type_bulk = 1;
  2681. priv_ep->endpoint.caps.type_int = 1;
  2682. list_add_tail(&priv_ep->endpoint.ep_list,
  2683. &priv_dev->gadget.ep_list);
  2684. }
  2685. priv_ep->flags = 0;
  2686. dev_dbg(priv_dev->dev, "Initialized %s support: %s %s\n",
  2687. priv_ep->name,
  2688. priv_ep->endpoint.caps.type_bulk ? "BULK, INT" : "",
  2689. priv_ep->endpoint.caps.type_iso ? "ISO" : "");
  2690. INIT_LIST_HEAD(&priv_ep->pending_req_list);
  2691. INIT_LIST_HEAD(&priv_ep->deferred_req_list);
  2692. INIT_LIST_HEAD(&priv_ep->wa2_descmiss_req_list);
  2693. }
  2694. return 0;
  2695. err:
  2696. cdns3_free_all_eps(priv_dev);
  2697. return -ENOMEM;
  2698. }
  2699. static void cdns3_gadget_release(struct device *dev)
  2700. {
  2701. struct cdns3_device *priv_dev = container_of(dev,
  2702. struct cdns3_device, gadget.dev);
  2703. kfree(priv_dev);
  2704. }
  2705. static void cdns3_gadget_exit(struct cdns *cdns)
  2706. {
  2707. struct cdns3_device *priv_dev;
  2708. priv_dev = cdns->gadget_dev;
  2709. pm_runtime_put_autosuspend(cdns->dev);
  2710. usb_del_gadget(&priv_dev->gadget);
  2711. devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
  2712. cdns3_free_all_eps(priv_dev);
  2713. while (!list_empty(&priv_dev->aligned_buf_list)) {
  2714. struct cdns3_aligned_buf *buf;
  2715. buf = cdns3_next_align_buf(&priv_dev->aligned_buf_list);
  2716. dma_free_noncoherent(priv_dev->sysdev, buf->size,
  2717. buf->buf,
  2718. buf->dma,
  2719. buf->dir);
  2720. list_del(&buf->list);
  2721. kfree(buf);
  2722. }
  2723. dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
  2724. priv_dev->setup_dma);
  2725. dma_pool_destroy(priv_dev->eps_dma_pool);
  2726. kfree(priv_dev->zlp_buf);
  2727. usb_put_gadget(&priv_dev->gadget);
  2728. cdns->gadget_dev = NULL;
  2729. cdns_drd_gadget_off(cdns);
  2730. }
  2731. static int cdns3_gadget_start(struct cdns *cdns)
  2732. {
  2733. struct cdns3_device *priv_dev;
  2734. u32 max_speed;
  2735. int ret;
  2736. priv_dev = kzalloc_obj(*priv_dev);
  2737. if (!priv_dev)
  2738. return -ENOMEM;
  2739. usb_initialize_gadget(cdns->dev, &priv_dev->gadget,
  2740. cdns3_gadget_release);
  2741. cdns->gadget_dev = priv_dev;
  2742. priv_dev->sysdev = cdns->dev;
  2743. priv_dev->dev = cdns->dev;
  2744. priv_dev->regs = cdns->dev_regs;
  2745. device_property_read_u16(priv_dev->dev, "cdns,on-chip-buff-size",
  2746. &priv_dev->onchip_buffers);
  2747. if (priv_dev->onchip_buffers <= 0) {
  2748. u32 reg = readl(&priv_dev->regs->usb_cap2);
  2749. priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg);
  2750. }
  2751. if (!priv_dev->onchip_buffers)
  2752. priv_dev->onchip_buffers = 256;
  2753. max_speed = usb_get_maximum_speed(cdns->dev);
  2754. /* Check the maximum_speed parameter */
  2755. switch (max_speed) {
  2756. case USB_SPEED_FULL:
  2757. case USB_SPEED_HIGH:
  2758. case USB_SPEED_SUPER:
  2759. break;
  2760. default:
  2761. dev_err(cdns->dev, "invalid maximum_speed parameter %d\n",
  2762. max_speed);
  2763. fallthrough;
  2764. case USB_SPEED_UNKNOWN:
  2765. /* default to superspeed */
  2766. max_speed = USB_SPEED_SUPER;
  2767. break;
  2768. }
  2769. /* fill gadget fields */
  2770. priv_dev->gadget.max_speed = max_speed;
  2771. priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
  2772. priv_dev->gadget.ops = &cdns3_gadget_ops;
  2773. priv_dev->gadget.name = "usb-ss-gadget";
  2774. priv_dev->gadget.quirk_avoids_skb_reserve = 1;
  2775. priv_dev->gadget.irq = cdns->dev_irq;
  2776. spin_lock_init(&priv_dev->lock);
  2777. INIT_WORK(&priv_dev->pending_status_wq,
  2778. cdns3_pending_setup_status_handler);
  2779. INIT_WORK(&priv_dev->aligned_buf_wq,
  2780. cdns3_free_aligned_request_buf);
  2781. /* initialize endpoint container */
  2782. INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
  2783. INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
  2784. priv_dev->eps_dma_pool = dma_pool_create("cdns3_eps_dma_pool",
  2785. priv_dev->sysdev,
  2786. TRB_RING_SIZE, 8, 0);
  2787. if (!priv_dev->eps_dma_pool) {
  2788. dev_err(priv_dev->dev, "Failed to create TRB dma pool\n");
  2789. ret = -ENOMEM;
  2790. goto err1;
  2791. }
  2792. ret = cdns3_init_eps(priv_dev);
  2793. if (ret) {
  2794. dev_err(priv_dev->dev, "Failed to create endpoints\n");
  2795. goto err1;
  2796. }
  2797. /* allocate memory for setup packet buffer */
  2798. priv_dev->setup_buf = dma_alloc_coherent(priv_dev->sysdev, 8,
  2799. &priv_dev->setup_dma, GFP_DMA);
  2800. if (!priv_dev->setup_buf) {
  2801. ret = -ENOMEM;
  2802. goto err2;
  2803. }
  2804. priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6);
  2805. dev_dbg(priv_dev->dev, "Device Controller version: %08x\n",
  2806. readl(&priv_dev->regs->usb_cap6));
  2807. dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n",
  2808. readl(&priv_dev->regs->usb_cap1));
  2809. dev_dbg(priv_dev->dev, "On-Chip memory configuration: %08x\n",
  2810. readl(&priv_dev->regs->usb_cap2));
  2811. priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver);
  2812. if (priv_dev->dev_ver >= DEV_VER_V2)
  2813. priv_dev->gadget.sg_supported = 1;
  2814. priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL);
  2815. if (!priv_dev->zlp_buf) {
  2816. ret = -ENOMEM;
  2817. goto err3;
  2818. }
  2819. /* add USB gadget device */
  2820. ret = usb_add_gadget(&priv_dev->gadget);
  2821. if (ret < 0) {
  2822. dev_err(priv_dev->dev, "Failed to add gadget\n");
  2823. goto err4;
  2824. }
  2825. return 0;
  2826. err4:
  2827. kfree(priv_dev->zlp_buf);
  2828. err3:
  2829. dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
  2830. priv_dev->setup_dma);
  2831. err2:
  2832. cdns3_free_all_eps(priv_dev);
  2833. err1:
  2834. dma_pool_destroy(priv_dev->eps_dma_pool);
  2835. usb_put_gadget(&priv_dev->gadget);
  2836. cdns->gadget_dev = NULL;
  2837. return ret;
  2838. }
  2839. static int __cdns3_gadget_init(struct cdns *cdns)
  2840. {
  2841. int ret = 0;
  2842. /* Ensure 32-bit DMA Mask in case we switched back from Host mode */
  2843. ret = dma_set_mask_and_coherent(cdns->dev, DMA_BIT_MASK(32));
  2844. if (ret) {
  2845. dev_err(cdns->dev, "Failed to set dma mask: %d\n", ret);
  2846. return ret;
  2847. }
  2848. cdns_drd_gadget_on(cdns);
  2849. pm_runtime_get_sync(cdns->dev);
  2850. ret = cdns3_gadget_start(cdns);
  2851. if (ret) {
  2852. pm_runtime_put_sync(cdns->dev);
  2853. cdns_drd_gadget_off(cdns);
  2854. return ret;
  2855. }
  2856. /*
  2857. * Because interrupt line can be shared with other components in
  2858. * driver it can't use IRQF_ONESHOT flag here.
  2859. */
  2860. ret = devm_request_threaded_irq(cdns->dev, cdns->dev_irq,
  2861. cdns3_device_irq_handler,
  2862. cdns3_device_thread_irq_handler,
  2863. IRQF_SHARED, dev_name(cdns->dev),
  2864. cdns->gadget_dev);
  2865. if (ret)
  2866. goto err0;
  2867. return 0;
  2868. err0:
  2869. cdns3_gadget_exit(cdns);
  2870. return ret;
  2871. }
  2872. static int cdns3_gadget_suspend(struct cdns *cdns, bool do_wakeup)
  2873. __must_hold(&cdns->lock)
  2874. {
  2875. struct cdns3_device *priv_dev = cdns->gadget_dev;
  2876. spin_unlock(&cdns->lock);
  2877. cdns3_disconnect_gadget(priv_dev);
  2878. spin_lock(&cdns->lock);
  2879. priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
  2880. usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
  2881. cdns3_hw_reset_eps_config(priv_dev);
  2882. /* disable interrupt for device */
  2883. writel(0, &priv_dev->regs->usb_ien);
  2884. return 0;
  2885. }
  2886. static int cdns3_gadget_resume(struct cdns *cdns, bool lost_power)
  2887. {
  2888. struct cdns3_device *priv_dev = cdns->gadget_dev;
  2889. if (!priv_dev->gadget_driver)
  2890. return 0;
  2891. cdns3_gadget_config(priv_dev);
  2892. if (lost_power)
  2893. writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
  2894. return 0;
  2895. }
  2896. /**
  2897. * cdns3_gadget_init - initialize device structure
  2898. *
  2899. * @cdns: cdns instance
  2900. *
  2901. * This function initializes the gadget.
  2902. */
  2903. int cdns3_gadget_init(struct cdns *cdns)
  2904. {
  2905. struct cdns_role_driver *rdrv;
  2906. rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
  2907. if (!rdrv)
  2908. return -ENOMEM;
  2909. rdrv->start = __cdns3_gadget_init;
  2910. rdrv->stop = cdns3_gadget_exit;
  2911. rdrv->suspend = cdns3_gadget_suspend;
  2912. rdrv->resume = cdns3_gadget_resume;
  2913. rdrv->state = CDNS_ROLE_STATE_INACTIVE;
  2914. rdrv->name = "gadget";
  2915. cdns->roles[USB_ROLE_DEVICE] = rdrv;
  2916. return 0;
  2917. }