cdns3-gadget.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509
  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(sizeof(*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. spin_lock_irqsave(&priv_dev->lock, flags);
  1655. reg = readl(&priv_dev->regs->usb_ists);
  1656. if (reg) {
  1657. writel(reg, &priv_dev->regs->usb_ists);
  1658. writel(USB_IEN_INIT, &priv_dev->regs->usb_ien);
  1659. cdns3_check_usb_interrupt_proceed(priv_dev, reg);
  1660. ret = IRQ_HANDLED;
  1661. }
  1662. reg = readl(&priv_dev->regs->ep_ists);
  1663. /* handle default endpoint OUT */
  1664. if (reg & EP_ISTS_EP_OUT0) {
  1665. cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT);
  1666. ret = IRQ_HANDLED;
  1667. }
  1668. /* handle default endpoint IN */
  1669. if (reg & EP_ISTS_EP_IN0) {
  1670. cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN);
  1671. ret = IRQ_HANDLED;
  1672. }
  1673. /* check if interrupt from non default endpoint, if no exit */
  1674. reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0);
  1675. if (!reg)
  1676. goto irqend;
  1677. for_each_set_bit(bit, &reg,
  1678. sizeof(u32) * BITS_PER_BYTE) {
  1679. cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]);
  1680. ret = IRQ_HANDLED;
  1681. }
  1682. if (priv_dev->dev_ver < DEV_VER_V2 && priv_dev->using_streams)
  1683. cdns3_wa2_check_outq_status(priv_dev);
  1684. irqend:
  1685. writel(~0, &priv_dev->regs->ep_ien);
  1686. spin_unlock_irqrestore(&priv_dev->lock, flags);
  1687. return ret;
  1688. }
  1689. /**
  1690. * cdns3_ep_onchip_buffer_reserve - Try to reserve onchip buf for EP
  1691. *
  1692. * The real reservation will occur during write to EP_CFG register,
  1693. * this function is used to check if the 'size' reservation is allowed.
  1694. *
  1695. * @priv_dev: extended gadget object
  1696. * @size: the size (KB) for EP would like to allocate
  1697. * @is_in: endpoint direction
  1698. *
  1699. * Return 0 if the required size can met or negative value on failure
  1700. */
  1701. static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
  1702. int size, int is_in)
  1703. {
  1704. int remained;
  1705. /* 2KB are reserved for EP0*/
  1706. remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2;
  1707. if (is_in) {
  1708. if (remained < size)
  1709. return -EPERM;
  1710. priv_dev->onchip_used_size += size;
  1711. } else {
  1712. int required;
  1713. /**
  1714. * ALL OUT EPs are shared the same chunk onchip memory, so
  1715. * driver checks if it already has assigned enough buffers
  1716. */
  1717. if (priv_dev->out_mem_is_allocated >= size)
  1718. return 0;
  1719. required = size - priv_dev->out_mem_is_allocated;
  1720. if (required > remained)
  1721. return -EPERM;
  1722. priv_dev->out_mem_is_allocated += required;
  1723. priv_dev->onchip_used_size += required;
  1724. }
  1725. return 0;
  1726. }
  1727. static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
  1728. struct cdns3_endpoint *priv_ep)
  1729. {
  1730. struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
  1731. /* For dev_ver > DEV_VER_V2 DMULT is configured per endpoint */
  1732. if (priv_dev->dev_ver <= DEV_VER_V2)
  1733. writel(USB_CONF_DMULT, &regs->usb_conf);
  1734. if (priv_dev->dev_ver == DEV_VER_V2)
  1735. writel(USB_CONF2_EN_TDL_TRB, &regs->usb_conf2);
  1736. if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) {
  1737. u32 mask;
  1738. if (priv_ep->dir)
  1739. mask = BIT(priv_ep->num + 16);
  1740. else
  1741. mask = BIT(priv_ep->num);
  1742. if (priv_ep->type != USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
  1743. cdns3_set_register_bit(&regs->tdl_from_trb, mask);
  1744. cdns3_set_register_bit(&regs->tdl_beh, mask);
  1745. cdns3_set_register_bit(&regs->tdl_beh2, mask);
  1746. cdns3_set_register_bit(&regs->dma_adv_td, mask);
  1747. }
  1748. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
  1749. cdns3_set_register_bit(&regs->tdl_from_trb, mask);
  1750. cdns3_set_register_bit(&regs->dtrans, mask);
  1751. }
  1752. }
  1753. /**
  1754. * cdns3_ep_config - Configure hardware endpoint
  1755. * @priv_ep: extended endpoint object
  1756. * @enable: set EP_CFG_ENABLE bit in ep_cfg register.
  1757. */
  1758. int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable)
  1759. {
  1760. bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
  1761. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  1762. u32 bEndpointAddress = priv_ep->num | priv_ep->dir;
  1763. u32 max_packet_size = priv_ep->wMaxPacketSize;
  1764. u8 maxburst = priv_ep->bMaxBurst;
  1765. u32 ep_cfg = 0;
  1766. u8 buffering;
  1767. int ret;
  1768. buffering = priv_dev->ep_buf_size - 1;
  1769. cdns3_configure_dmult(priv_dev, priv_ep);
  1770. switch (priv_ep->type) {
  1771. case USB_ENDPOINT_XFER_INT:
  1772. ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT);
  1773. if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir)
  1774. ep_cfg |= EP_CFG_TDL_CHK;
  1775. break;
  1776. case USB_ENDPOINT_XFER_BULK:
  1777. ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK);
  1778. if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir)
  1779. ep_cfg |= EP_CFG_TDL_CHK;
  1780. break;
  1781. default:
  1782. ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC);
  1783. buffering = (priv_ep->bMaxBurst + 1) * (priv_ep->mult + 1) - 1;
  1784. }
  1785. switch (priv_dev->gadget.speed) {
  1786. case USB_SPEED_FULL:
  1787. max_packet_size = is_iso_ep ? 1023 : 64;
  1788. break;
  1789. case USB_SPEED_HIGH:
  1790. max_packet_size = is_iso_ep ? 1024 : 512;
  1791. break;
  1792. case USB_SPEED_SUPER:
  1793. if (priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
  1794. max_packet_size = 1024;
  1795. maxburst = priv_dev->ep_buf_size - 1;
  1796. }
  1797. break;
  1798. default:
  1799. /* all other speed are not supported */
  1800. return -EINVAL;
  1801. }
  1802. if (max_packet_size == 1024)
  1803. priv_ep->trb_burst_size = 128;
  1804. else if (max_packet_size >= 512)
  1805. priv_ep->trb_burst_size = 64;
  1806. else
  1807. priv_ep->trb_burst_size = 16;
  1808. /*
  1809. * In versions preceding DEV_VER_V2, for example, iMX8QM, there exit the bugs
  1810. * in the DMA. These bugs occur when the trb_burst_size exceeds 16 and the
  1811. * address is not aligned to 128 Bytes (which is a product of the 64-bit AXI
  1812. * and AXI maximum burst length of 16 or 0xF+1, dma_axi_ctrl0[3:0]). This
  1813. * results in data corruption when it crosses the 4K border. The corruption
  1814. * specifically occurs from the position (4K - (address & 0x7F)) to 4K.
  1815. *
  1816. * So force trb_burst_size to 16 at such platform.
  1817. */
  1818. if (priv_dev->dev_ver < DEV_VER_V2)
  1819. priv_ep->trb_burst_size = 16;
  1820. buffering = min_t(u8, buffering, EP_CFG_BUFFERING_MAX);
  1821. maxburst = min_t(u8, maxburst, EP_CFG_MAXBURST_MAX);
  1822. /* onchip buffer is only allocated before configuration */
  1823. if (!priv_dev->hw_configured_flag) {
  1824. ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
  1825. !!priv_ep->dir);
  1826. if (ret) {
  1827. dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
  1828. return ret;
  1829. }
  1830. }
  1831. if (enable)
  1832. ep_cfg |= EP_CFG_ENABLE;
  1833. if (priv_ep->use_streams && priv_dev->gadget.speed >= USB_SPEED_SUPER) {
  1834. if (priv_dev->dev_ver >= DEV_VER_V3) {
  1835. u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
  1836. /*
  1837. * Stream capable endpoints are handled by using ep_tdl
  1838. * register. Other endpoints use TDL from TRB feature.
  1839. */
  1840. cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb,
  1841. mask);
  1842. }
  1843. /* Enable Stream Bit TDL chk and SID chk */
  1844. ep_cfg |= EP_CFG_STREAM_EN | EP_CFG_TDL_CHK | EP_CFG_SID_CHK;
  1845. }
  1846. ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
  1847. EP_CFG_MULT(priv_ep->mult) | /* must match EP setting */
  1848. EP_CFG_BUFFERING(buffering) |
  1849. EP_CFG_MAXBURST(maxburst);
  1850. cdns3_select_ep(priv_dev, bEndpointAddress);
  1851. writel(ep_cfg, &priv_dev->regs->ep_cfg);
  1852. priv_ep->flags |= EP_CONFIGURED;
  1853. dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
  1854. priv_ep->name, ep_cfg);
  1855. return 0;
  1856. }
  1857. /* Find correct direction for HW endpoint according to description */
  1858. static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc,
  1859. struct cdns3_endpoint *priv_ep)
  1860. {
  1861. return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) ||
  1862. (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc));
  1863. }
  1864. static struct
  1865. cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev,
  1866. struct usb_endpoint_descriptor *desc)
  1867. {
  1868. struct usb_ep *ep;
  1869. struct cdns3_endpoint *priv_ep;
  1870. list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
  1871. unsigned long num;
  1872. int ret;
  1873. /* ep name pattern likes epXin or epXout */
  1874. char c[2] = {ep->name[2], '\0'};
  1875. ret = kstrtoul(c, 10, &num);
  1876. if (ret)
  1877. return ERR_PTR(ret);
  1878. priv_ep = ep_to_cdns3_ep(ep);
  1879. if (cdns3_ep_dir_is_correct(desc, priv_ep)) {
  1880. if (!(priv_ep->flags & EP_CLAIMED)) {
  1881. priv_ep->num = num;
  1882. return priv_ep;
  1883. }
  1884. }
  1885. }
  1886. return ERR_PTR(-ENOENT);
  1887. }
  1888. /*
  1889. * Cadence IP has one limitation that all endpoints must be configured
  1890. * (Type & MaxPacketSize) before setting configuration through hardware
  1891. * register, it means we can't change endpoints configuration after
  1892. * set_configuration.
  1893. *
  1894. * This function set EP_CLAIMED flag which is added when the gadget driver
  1895. * uses usb_ep_autoconfig to configure specific endpoint;
  1896. * When the udc driver receives set_configurion request,
  1897. * it goes through all claimed endpoints, and configure all endpoints
  1898. * accordingly.
  1899. *
  1900. * At usb_ep_ops.enable/disable, we only enable and disable endpoint through
  1901. * ep_cfg register which can be changed after set_configuration, and do
  1902. * some software operation accordingly.
  1903. */
  1904. static struct
  1905. usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget,
  1906. struct usb_endpoint_descriptor *desc,
  1907. struct usb_ss_ep_comp_descriptor *comp_desc)
  1908. {
  1909. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  1910. struct cdns3_endpoint *priv_ep;
  1911. unsigned long flags;
  1912. priv_ep = cdns3_find_available_ep(priv_dev, desc);
  1913. if (IS_ERR(priv_ep)) {
  1914. dev_err(priv_dev->dev, "no available ep\n");
  1915. return NULL;
  1916. }
  1917. dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name);
  1918. spin_lock_irqsave(&priv_dev->lock, flags);
  1919. priv_ep->endpoint.desc = desc;
  1920. priv_ep->dir = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT;
  1921. priv_ep->type = usb_endpoint_type(desc);
  1922. priv_ep->flags |= EP_CLAIMED;
  1923. priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
  1924. priv_ep->wMaxPacketSize = usb_endpoint_maxp(desc);
  1925. priv_ep->mult = USB_EP_MAXP_MULT(priv_ep->wMaxPacketSize);
  1926. priv_ep->wMaxPacketSize &= USB_ENDPOINT_MAXP_MASK;
  1927. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && comp_desc) {
  1928. priv_ep->mult = USB_SS_MULT(comp_desc->bmAttributes) - 1;
  1929. priv_ep->bMaxBurst = comp_desc->bMaxBurst;
  1930. }
  1931. spin_unlock_irqrestore(&priv_dev->lock, flags);
  1932. return &priv_ep->endpoint;
  1933. }
  1934. /**
  1935. * cdns3_gadget_ep_alloc_request - Allocates request
  1936. * @ep: endpoint object associated with request
  1937. * @gfp_flags: gfp flags
  1938. *
  1939. * Returns allocated request address, NULL on allocation error
  1940. */
  1941. struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep,
  1942. gfp_t gfp_flags)
  1943. {
  1944. struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
  1945. struct cdns3_request *priv_req;
  1946. priv_req = kzalloc(sizeof(*priv_req), gfp_flags);
  1947. if (!priv_req)
  1948. return NULL;
  1949. priv_req->priv_ep = priv_ep;
  1950. trace_cdns3_alloc_request(priv_req);
  1951. return &priv_req->request;
  1952. }
  1953. /**
  1954. * cdns3_gadget_ep_free_request - Free memory occupied by request
  1955. * @ep: endpoint object associated with request
  1956. * @request: request to free memory
  1957. */
  1958. void cdns3_gadget_ep_free_request(struct usb_ep *ep,
  1959. struct usb_request *request)
  1960. {
  1961. struct cdns3_request *priv_req = to_cdns3_request(request);
  1962. if (priv_req->aligned_buf)
  1963. priv_req->aligned_buf->in_use = 0;
  1964. trace_cdns3_free_request(priv_req);
  1965. kfree(priv_req);
  1966. }
  1967. /**
  1968. * cdns3_gadget_ep_enable - Enable endpoint
  1969. * @ep: endpoint object
  1970. * @desc: endpoint descriptor
  1971. *
  1972. * Returns 0 on success, error code elsewhere
  1973. */
  1974. static int cdns3_gadget_ep_enable(struct usb_ep *ep,
  1975. const struct usb_endpoint_descriptor *desc)
  1976. {
  1977. struct cdns3_endpoint *priv_ep;
  1978. struct cdns3_device *priv_dev;
  1979. const struct usb_ss_ep_comp_descriptor *comp_desc;
  1980. u32 reg = EP_STS_EN_TRBERREN;
  1981. u32 bEndpointAddress;
  1982. unsigned long flags;
  1983. int enable = 1;
  1984. int ret = 0;
  1985. int val;
  1986. if (!ep) {
  1987. pr_debug("usbss: ep not configured?\n");
  1988. return -EINVAL;
  1989. }
  1990. priv_ep = ep_to_cdns3_ep(ep);
  1991. priv_dev = priv_ep->cdns3_dev;
  1992. comp_desc = priv_ep->endpoint.comp_desc;
  1993. if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
  1994. dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
  1995. return -EINVAL;
  1996. }
  1997. if (!desc->wMaxPacketSize) {
  1998. dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
  1999. return -EINVAL;
  2000. }
  2001. if (dev_WARN_ONCE(priv_dev->dev, priv_ep->flags & EP_ENABLED,
  2002. "%s is already enabled\n", priv_ep->name))
  2003. return 0;
  2004. spin_lock_irqsave(&priv_dev->lock, flags);
  2005. priv_ep->endpoint.desc = desc;
  2006. priv_ep->type = usb_endpoint_type(desc);
  2007. priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
  2008. if (priv_ep->interval > ISO_MAX_INTERVAL &&
  2009. priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
  2010. dev_err(priv_dev->dev, "Driver is limited to %d period\n",
  2011. ISO_MAX_INTERVAL);
  2012. ret = -EINVAL;
  2013. goto exit;
  2014. }
  2015. bEndpointAddress = priv_ep->num | priv_ep->dir;
  2016. cdns3_select_ep(priv_dev, bEndpointAddress);
  2017. /*
  2018. * For some versions of controller at some point during ISO OUT traffic
  2019. * DMA reads Transfer Ring for the EP which has never got doorbell.
  2020. * This issue was detected only on simulation, but to avoid this issue
  2021. * driver add protection against it. To fix it driver enable ISO OUT
  2022. * endpoint before setting DRBL. This special treatment of ISO OUT
  2023. * endpoints are recommended by controller specification.
  2024. */
  2025. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
  2026. enable = 0;
  2027. if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
  2028. /*
  2029. * Enable stream support (SS mode) related interrupts
  2030. * in EP_STS_EN Register
  2031. */
  2032. if (priv_dev->gadget.speed >= USB_SPEED_SUPER) {
  2033. reg |= EP_STS_EN_IOTEN | EP_STS_EN_PRIMEEEN |
  2034. EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN |
  2035. EP_STS_EN_STREAMREN;
  2036. priv_ep->use_streams = true;
  2037. ret = cdns3_ep_config(priv_ep, enable);
  2038. priv_dev->using_streams |= true;
  2039. }
  2040. } else {
  2041. ret = cdns3_ep_config(priv_ep, enable);
  2042. }
  2043. if (ret)
  2044. goto exit;
  2045. ret = cdns3_allocate_trb_pool(priv_ep);
  2046. if (ret)
  2047. goto exit;
  2048. bEndpointAddress = priv_ep->num | priv_ep->dir;
  2049. cdns3_select_ep(priv_dev, bEndpointAddress);
  2050. trace_cdns3_gadget_ep_enable(priv_ep);
  2051. writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
  2052. ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2053. !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
  2054. 1, 1000);
  2055. if (unlikely(ret)) {
  2056. cdns3_free_trb_pool(priv_ep);
  2057. ret = -EINVAL;
  2058. goto exit;
  2059. }
  2060. /* enable interrupt for selected endpoint */
  2061. cdns3_set_register_bit(&priv_dev->regs->ep_ien,
  2062. BIT(cdns3_ep_addr_to_index(bEndpointAddress)));
  2063. if (priv_dev->dev_ver < DEV_VER_V2)
  2064. cdns3_wa2_enable_detection(priv_dev, priv_ep, reg);
  2065. writel(reg, &priv_dev->regs->ep_sts_en);
  2066. ep->desc = desc;
  2067. priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING |
  2068. EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN);
  2069. priv_ep->flags |= EP_ENABLED | EP_UPDATE_EP_TRBADDR;
  2070. priv_ep->wa1_set = 0;
  2071. priv_ep->enqueue = 0;
  2072. priv_ep->dequeue = 0;
  2073. reg = readl(&priv_dev->regs->ep_sts);
  2074. priv_ep->pcs = !!EP_STS_CCS(reg);
  2075. priv_ep->ccs = !!EP_STS_CCS(reg);
  2076. /* one TRB is reserved for link TRB used in DMULT mode*/
  2077. priv_ep->free_trbs = priv_ep->num_trbs - 1;
  2078. exit:
  2079. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2080. return ret;
  2081. }
  2082. /**
  2083. * cdns3_gadget_ep_disable - Disable endpoint
  2084. * @ep: endpoint object
  2085. *
  2086. * Returns 0 on success, error code elsewhere
  2087. */
  2088. static int cdns3_gadget_ep_disable(struct usb_ep *ep)
  2089. {
  2090. struct cdns3_endpoint *priv_ep;
  2091. struct cdns3_request *priv_req;
  2092. struct cdns3_device *priv_dev;
  2093. struct usb_request *request;
  2094. unsigned long flags;
  2095. int ret = 0;
  2096. u32 ep_cfg;
  2097. int val;
  2098. if (!ep) {
  2099. pr_err("usbss: invalid parameters\n");
  2100. return -EINVAL;
  2101. }
  2102. priv_ep = ep_to_cdns3_ep(ep);
  2103. priv_dev = priv_ep->cdns3_dev;
  2104. if (dev_WARN_ONCE(priv_dev->dev, !(priv_ep->flags & EP_ENABLED),
  2105. "%s is already disabled\n", priv_ep->name))
  2106. return 0;
  2107. spin_lock_irqsave(&priv_dev->lock, flags);
  2108. trace_cdns3_gadget_ep_disable(priv_ep);
  2109. cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
  2110. ep_cfg = readl(&priv_dev->regs->ep_cfg);
  2111. ep_cfg &= ~EP_CFG_ENABLE;
  2112. writel(ep_cfg, &priv_dev->regs->ep_cfg);
  2113. /**
  2114. * Driver needs some time before resetting endpoint.
  2115. * It need waits for clearing DBUSY bit or for timeout expired.
  2116. * 10us is enough time for controller to stop transfer.
  2117. */
  2118. readl_poll_timeout_atomic(&priv_dev->regs->ep_sts, val,
  2119. !(val & EP_STS_DBUSY), 1, 10);
  2120. writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
  2121. readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2122. !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
  2123. 1, 1000);
  2124. if (unlikely(ret))
  2125. dev_err(priv_dev->dev, "Timeout: %s resetting failed.\n",
  2126. priv_ep->name);
  2127. while (!list_empty(&priv_ep->pending_req_list)) {
  2128. request = cdns3_next_request(&priv_ep->pending_req_list);
  2129. cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
  2130. -ESHUTDOWN);
  2131. }
  2132. while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
  2133. priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
  2134. list_del_init(&priv_req->list);
  2135. kfree(priv_req->request.buf);
  2136. cdns3_gadget_ep_free_request(&priv_ep->endpoint,
  2137. &priv_req->request);
  2138. --priv_ep->wa2_counter;
  2139. }
  2140. while (!list_empty(&priv_ep->deferred_req_list)) {
  2141. request = cdns3_next_request(&priv_ep->deferred_req_list);
  2142. cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
  2143. -ESHUTDOWN);
  2144. }
  2145. priv_ep->descmis_req = NULL;
  2146. ep->desc = NULL;
  2147. priv_ep->flags &= ~EP_ENABLED;
  2148. priv_ep->use_streams = false;
  2149. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2150. return ret;
  2151. }
  2152. /**
  2153. * __cdns3_gadget_ep_queue - Transfer data on endpoint
  2154. * @ep: endpoint object
  2155. * @request: request object
  2156. * @gfp_flags: gfp flags
  2157. *
  2158. * Returns 0 on success, error code elsewhere
  2159. */
  2160. static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
  2161. struct usb_request *request,
  2162. gfp_t gfp_flags)
  2163. {
  2164. struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
  2165. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  2166. struct cdns3_request *priv_req;
  2167. int ret = 0;
  2168. request->actual = 0;
  2169. request->status = -EINPROGRESS;
  2170. priv_req = to_cdns3_request(request);
  2171. trace_cdns3_ep_queue(priv_req);
  2172. if (priv_dev->dev_ver < DEV_VER_V2) {
  2173. ret = cdns3_wa2_gadget_ep_queue(priv_dev, priv_ep,
  2174. priv_req);
  2175. if (ret == EINPROGRESS)
  2176. return 0;
  2177. }
  2178. ret = cdns3_prepare_aligned_request_buf(priv_req);
  2179. if (ret < 0)
  2180. return ret;
  2181. if (likely(!(priv_req->flags & REQUEST_UNALIGNED))) {
  2182. ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request,
  2183. usb_endpoint_dir_in(ep->desc));
  2184. if (ret)
  2185. return ret;
  2186. }
  2187. list_add_tail(&request->list, &priv_ep->deferred_req_list);
  2188. /*
  2189. * For stream capable endpoint if prime irq flag is set then only start
  2190. * request.
  2191. * If hardware endpoint configuration has not been set yet then
  2192. * just queue request in deferred list. Transfer will be started in
  2193. * cdns3_set_hw_configuration.
  2194. */
  2195. if (!request->stream_id) {
  2196. if (priv_dev->hw_configured_flag &&
  2197. !(priv_ep->flags & EP_STALLED) &&
  2198. !(priv_ep->flags & EP_STALL_PENDING))
  2199. cdns3_start_all_request(priv_dev, priv_ep);
  2200. } else {
  2201. if (priv_dev->hw_configured_flag && priv_ep->prime_flag)
  2202. cdns3_start_all_request(priv_dev, priv_ep);
  2203. }
  2204. return 0;
  2205. }
  2206. static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
  2207. gfp_t gfp_flags)
  2208. {
  2209. struct usb_request *zlp_request;
  2210. struct cdns3_endpoint *priv_ep;
  2211. struct cdns3_device *priv_dev;
  2212. unsigned long flags;
  2213. int ret;
  2214. if (!request || !ep)
  2215. return -EINVAL;
  2216. priv_ep = ep_to_cdns3_ep(ep);
  2217. priv_dev = priv_ep->cdns3_dev;
  2218. spin_lock_irqsave(&priv_dev->lock, flags);
  2219. ret = __cdns3_gadget_ep_queue(ep, request, gfp_flags);
  2220. if (ret == 0 && request->zero && request->length &&
  2221. (request->length % ep->maxpacket == 0)) {
  2222. struct cdns3_request *priv_req;
  2223. zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
  2224. zlp_request->buf = priv_dev->zlp_buf;
  2225. zlp_request->length = 0;
  2226. priv_req = to_cdns3_request(zlp_request);
  2227. priv_req->flags |= REQUEST_ZLP;
  2228. dev_dbg(priv_dev->dev, "Queuing ZLP for endpoint: %s\n",
  2229. priv_ep->name);
  2230. ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags);
  2231. }
  2232. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2233. return ret;
  2234. }
  2235. /**
  2236. * cdns3_gadget_ep_dequeue - Remove request from transfer queue
  2237. * @ep: endpoint object associated with request
  2238. * @request: request object
  2239. *
  2240. * Returns 0 on success, error code elsewhere
  2241. */
  2242. int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
  2243. struct usb_request *request)
  2244. {
  2245. struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
  2246. struct cdns3_device *priv_dev;
  2247. struct usb_request *req, *req_temp;
  2248. struct cdns3_request *priv_req;
  2249. struct cdns3_trb *link_trb;
  2250. u8 req_on_hw_ring = 0;
  2251. unsigned long flags;
  2252. int ret = 0;
  2253. int val;
  2254. if (!ep || !request || !ep->desc)
  2255. return -EINVAL;
  2256. priv_dev = priv_ep->cdns3_dev;
  2257. spin_lock_irqsave(&priv_dev->lock, flags);
  2258. priv_req = to_cdns3_request(request);
  2259. trace_cdns3_ep_dequeue(priv_req);
  2260. cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
  2261. list_for_each_entry_safe(req, req_temp, &priv_ep->pending_req_list,
  2262. list) {
  2263. if (request == req) {
  2264. req_on_hw_ring = 1;
  2265. goto found;
  2266. }
  2267. }
  2268. list_for_each_entry_safe(req, req_temp, &priv_ep->deferred_req_list,
  2269. list) {
  2270. if (request == req)
  2271. goto found;
  2272. }
  2273. goto not_found;
  2274. found:
  2275. link_trb = priv_req->trb;
  2276. /* Update ring only if removed request is on pending_req_list list */
  2277. if (req_on_hw_ring && link_trb) {
  2278. /* Stop DMA */
  2279. writel(EP_CMD_DFLUSH, &priv_dev->regs->ep_cmd);
  2280. /* wait for DFLUSH cleared */
  2281. readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2282. !(val & EP_CMD_DFLUSH), 1, 1000);
  2283. link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma +
  2284. ((priv_req->end_trb + 1) * TRB_SIZE)));
  2285. link_trb->control = cpu_to_le32((le32_to_cpu(link_trb->control) & TRB_CYCLE) |
  2286. TRB_TYPE(TRB_LINK) | TRB_CHAIN);
  2287. if (priv_ep->wa1_trb == priv_req->trb)
  2288. cdns3_wa1_restore_cycle_bit(priv_ep);
  2289. }
  2290. cdns3_gadget_giveback(priv_ep, priv_req, -ECONNRESET);
  2291. req = cdns3_next_request(&priv_ep->pending_req_list);
  2292. if (req)
  2293. cdns3_rearm_transfer(priv_ep, 1);
  2294. not_found:
  2295. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2296. return ret;
  2297. }
  2298. /**
  2299. * __cdns3_gadget_ep_set_halt - Sets stall on selected endpoint
  2300. * Should be called after acquiring spin_lock and selecting ep
  2301. * @priv_ep: endpoint object to set stall on.
  2302. */
  2303. void __cdns3_gadget_ep_set_halt(struct cdns3_endpoint *priv_ep)
  2304. {
  2305. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  2306. trace_cdns3_halt(priv_ep, 1, 0);
  2307. if (!(priv_ep->flags & EP_STALLED)) {
  2308. u32 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
  2309. if (!(ep_sts_reg & EP_STS_DBUSY))
  2310. cdns3_ep_stall_flush(priv_ep);
  2311. else
  2312. priv_ep->flags |= EP_STALL_PENDING;
  2313. }
  2314. }
  2315. /**
  2316. * __cdns3_gadget_ep_clear_halt - Clears stall on selected endpoint
  2317. * Should be called after acquiring spin_lock and selecting ep
  2318. * @priv_ep: endpoint object to clear stall on
  2319. */
  2320. int __cdns3_gadget_ep_clear_halt(struct cdns3_endpoint *priv_ep)
  2321. {
  2322. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  2323. struct usb_request *request;
  2324. struct cdns3_request *priv_req;
  2325. struct cdns3_trb *trb = NULL;
  2326. struct cdns3_trb trb_tmp;
  2327. int ret;
  2328. int val;
  2329. trace_cdns3_halt(priv_ep, 0, 0);
  2330. request = cdns3_next_request(&priv_ep->pending_req_list);
  2331. if (request) {
  2332. priv_req = to_cdns3_request(request);
  2333. trb = priv_req->trb;
  2334. if (trb) {
  2335. trb_tmp = *trb;
  2336. trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE);
  2337. }
  2338. }
  2339. writel(EP_CMD_CSTALL | EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
  2340. /* wait for EPRST cleared */
  2341. ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2342. !(val & EP_CMD_EPRST), 1, 100);
  2343. if (ret)
  2344. return -EINVAL;
  2345. priv_ep->flags &= ~(EP_STALLED | EP_STALL_PENDING);
  2346. if (request) {
  2347. if (trb)
  2348. *trb = trb_tmp;
  2349. cdns3_rearm_transfer(priv_ep, 1);
  2350. }
  2351. cdns3_start_all_request(priv_dev, priv_ep);
  2352. return ret;
  2353. }
  2354. /**
  2355. * cdns3_gadget_ep_set_halt - Sets/clears stall on selected endpoint
  2356. * @ep: endpoint object to set/clear stall on
  2357. * @value: 1 for set stall, 0 for clear stall
  2358. *
  2359. * Returns 0 on success, error code elsewhere
  2360. */
  2361. int cdns3_gadget_ep_set_halt(struct usb_ep *ep, int value)
  2362. {
  2363. struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
  2364. struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
  2365. unsigned long flags;
  2366. int ret = 0;
  2367. if (!(priv_ep->flags & EP_ENABLED))
  2368. return -EPERM;
  2369. spin_lock_irqsave(&priv_dev->lock, flags);
  2370. cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
  2371. if (!value) {
  2372. priv_ep->flags &= ~EP_WEDGE;
  2373. ret = __cdns3_gadget_ep_clear_halt(priv_ep);
  2374. } else {
  2375. __cdns3_gadget_ep_set_halt(priv_ep);
  2376. }
  2377. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2378. return ret;
  2379. }
  2380. extern const struct usb_ep_ops cdns3_gadget_ep0_ops;
  2381. static const struct usb_ep_ops cdns3_gadget_ep_ops = {
  2382. .enable = cdns3_gadget_ep_enable,
  2383. .disable = cdns3_gadget_ep_disable,
  2384. .alloc_request = cdns3_gadget_ep_alloc_request,
  2385. .free_request = cdns3_gadget_ep_free_request,
  2386. .queue = cdns3_gadget_ep_queue,
  2387. .dequeue = cdns3_gadget_ep_dequeue,
  2388. .set_halt = cdns3_gadget_ep_set_halt,
  2389. .set_wedge = cdns3_gadget_ep_set_wedge,
  2390. };
  2391. /**
  2392. * cdns3_gadget_get_frame - Returns number of actual ITP frame
  2393. * @gadget: gadget object
  2394. *
  2395. * Returns number of actual ITP frame
  2396. */
  2397. static int cdns3_gadget_get_frame(struct usb_gadget *gadget)
  2398. {
  2399. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2400. return readl(&priv_dev->regs->usb_itpn);
  2401. }
  2402. int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev)
  2403. {
  2404. enum usb_device_speed speed;
  2405. speed = cdns3_get_speed(priv_dev);
  2406. if (speed >= USB_SPEED_SUPER)
  2407. return 0;
  2408. /* Start driving resume signaling to indicate remote wakeup. */
  2409. writel(USB_CONF_LGO_L0, &priv_dev->regs->usb_conf);
  2410. return 0;
  2411. }
  2412. static int cdns3_gadget_wakeup(struct usb_gadget *gadget)
  2413. {
  2414. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2415. unsigned long flags;
  2416. int ret = 0;
  2417. spin_lock_irqsave(&priv_dev->lock, flags);
  2418. ret = __cdns3_gadget_wakeup(priv_dev);
  2419. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2420. return ret;
  2421. }
  2422. static int cdns3_gadget_set_selfpowered(struct usb_gadget *gadget,
  2423. int is_selfpowered)
  2424. {
  2425. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2426. unsigned long flags;
  2427. spin_lock_irqsave(&priv_dev->lock, flags);
  2428. priv_dev->is_selfpowered = !!is_selfpowered;
  2429. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2430. return 0;
  2431. }
  2432. static int cdns3_gadget_pullup(struct usb_gadget *gadget, int is_on)
  2433. {
  2434. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2435. if (is_on) {
  2436. writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
  2437. } else {
  2438. writel(~0, &priv_dev->regs->ep_ists);
  2439. writel(~0, &priv_dev->regs->usb_ists);
  2440. writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
  2441. }
  2442. return 0;
  2443. }
  2444. static void cdns3_gadget_config(struct cdns3_device *priv_dev)
  2445. {
  2446. struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
  2447. u32 reg;
  2448. cdns3_ep0_config(priv_dev);
  2449. /* enable interrupts for endpoint 0 (in and out) */
  2450. writel(EP_IEN_EP_OUT0 | EP_IEN_EP_IN0, &regs->ep_ien);
  2451. /*
  2452. * Driver needs to modify LFPS minimal U1 Exit time for DEV_VER_TI_V1
  2453. * revision of controller.
  2454. */
  2455. if (priv_dev->dev_ver == DEV_VER_TI_V1) {
  2456. reg = readl(&regs->dbg_link1);
  2457. reg &= ~DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_MASK;
  2458. reg |= DBG_LINK1_LFPS_MIN_GEN_U1_EXIT(0x55) |
  2459. DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_SET;
  2460. writel(reg, &regs->dbg_link1);
  2461. }
  2462. /*
  2463. * By default some platforms has set protected access to memory.
  2464. * This cause problem with cache, so driver restore non-secure
  2465. * access to memory.
  2466. */
  2467. reg = readl(&regs->dma_axi_ctrl);
  2468. reg |= DMA_AXI_CTRL_MARPROT(DMA_AXI_CTRL_NON_SECURE) |
  2469. DMA_AXI_CTRL_MAWPROT(DMA_AXI_CTRL_NON_SECURE);
  2470. writel(reg, &regs->dma_axi_ctrl);
  2471. /* enable generic interrupt*/
  2472. writel(USB_IEN_INIT, &regs->usb_ien);
  2473. writel(USB_CONF_CLK2OFFDS | USB_CONF_L1DS, &regs->usb_conf);
  2474. /* keep Fast Access bit */
  2475. writel(PUSB_PWR_FST_REG_ACCESS, &priv_dev->regs->usb_pwr);
  2476. cdns3_configure_dmult(priv_dev, NULL);
  2477. }
  2478. /**
  2479. * cdns3_gadget_udc_start - Gadget start
  2480. * @gadget: gadget object
  2481. * @driver: driver which operates on this gadget
  2482. *
  2483. * Returns 0 on success, error code elsewhere
  2484. */
  2485. static int cdns3_gadget_udc_start(struct usb_gadget *gadget,
  2486. struct usb_gadget_driver *driver)
  2487. {
  2488. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2489. unsigned long flags;
  2490. enum usb_device_speed max_speed = driver->max_speed;
  2491. spin_lock_irqsave(&priv_dev->lock, flags);
  2492. priv_dev->gadget_driver = driver;
  2493. /* limit speed if necessary */
  2494. max_speed = min(driver->max_speed, gadget->max_speed);
  2495. switch (max_speed) {
  2496. case USB_SPEED_FULL:
  2497. writel(USB_CONF_SFORCE_FS, &priv_dev->regs->usb_conf);
  2498. writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
  2499. break;
  2500. case USB_SPEED_HIGH:
  2501. writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
  2502. break;
  2503. case USB_SPEED_SUPER:
  2504. break;
  2505. default:
  2506. dev_err(priv_dev->dev,
  2507. "invalid maximum_speed parameter %d\n",
  2508. max_speed);
  2509. fallthrough;
  2510. case USB_SPEED_UNKNOWN:
  2511. /* default to superspeed */
  2512. max_speed = USB_SPEED_SUPER;
  2513. break;
  2514. }
  2515. cdns3_gadget_config(priv_dev);
  2516. spin_unlock_irqrestore(&priv_dev->lock, flags);
  2517. return 0;
  2518. }
  2519. /**
  2520. * cdns3_gadget_udc_stop - Stops gadget
  2521. * @gadget: gadget object
  2522. *
  2523. * Returns 0
  2524. */
  2525. static int cdns3_gadget_udc_stop(struct usb_gadget *gadget)
  2526. {
  2527. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2528. struct cdns3_endpoint *priv_ep;
  2529. u32 bEndpointAddress;
  2530. struct usb_ep *ep;
  2531. int val;
  2532. priv_dev->gadget_driver = NULL;
  2533. priv_dev->onchip_used_size = 0;
  2534. priv_dev->out_mem_is_allocated = 0;
  2535. priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
  2536. list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
  2537. priv_ep = ep_to_cdns3_ep(ep);
  2538. bEndpointAddress = priv_ep->num | priv_ep->dir;
  2539. cdns3_select_ep(priv_dev, bEndpointAddress);
  2540. writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
  2541. readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
  2542. !(val & EP_CMD_EPRST), 1, 100);
  2543. priv_ep->flags &= ~EP_CLAIMED;
  2544. }
  2545. /* disable interrupt for device */
  2546. writel(0, &priv_dev->regs->usb_ien);
  2547. writel(0, &priv_dev->regs->usb_pwr);
  2548. writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
  2549. return 0;
  2550. }
  2551. /**
  2552. * cdns3_gadget_check_config - ensure cdns3 can support the USB configuration
  2553. * @gadget: pointer to the USB gadget
  2554. *
  2555. * Used to record the maximum number of endpoints being used in a USB composite
  2556. * device. (across all configurations) This is to be used in the calculation
  2557. * of the TXFIFO sizes when resizing internal memory for individual endpoints.
  2558. * It will help ensured that the resizing logic reserves enough space for at
  2559. * least one max packet.
  2560. */
  2561. static int cdns3_gadget_check_config(struct usb_gadget *gadget)
  2562. {
  2563. struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
  2564. struct cdns3_endpoint *priv_ep;
  2565. struct usb_ep *ep;
  2566. int n_in = 0;
  2567. int iso = 0;
  2568. int out = 1;
  2569. int total;
  2570. int n;
  2571. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  2572. priv_ep = ep_to_cdns3_ep(ep);
  2573. if (!(priv_ep->flags & EP_CLAIMED))
  2574. continue;
  2575. n = (priv_ep->mult + 1) * (priv_ep->bMaxBurst + 1);
  2576. if (ep->address & USB_DIR_IN) {
  2577. /*
  2578. * ISO transfer: DMA start move data when get ISO, only transfer
  2579. * data as min(TD size, iso). No benefit for allocate bigger
  2580. * internal memory than 'iso'.
  2581. */
  2582. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
  2583. iso += n;
  2584. else
  2585. n_in++;
  2586. } else {
  2587. if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
  2588. out = max_t(int, out, n);
  2589. }
  2590. }
  2591. /* 2KB are reserved for EP0, 1KB for out*/
  2592. total = 2 + n_in + out + iso;
  2593. if (total > priv_dev->onchip_buffers)
  2594. return -ENOMEM;
  2595. priv_dev->ep_buf_size = (priv_dev->onchip_buffers - 2 - iso) / (n_in + out);
  2596. return 0;
  2597. }
  2598. static const struct usb_gadget_ops cdns3_gadget_ops = {
  2599. .get_frame = cdns3_gadget_get_frame,
  2600. .wakeup = cdns3_gadget_wakeup,
  2601. .set_selfpowered = cdns3_gadget_set_selfpowered,
  2602. .pullup = cdns3_gadget_pullup,
  2603. .udc_start = cdns3_gadget_udc_start,
  2604. .udc_stop = cdns3_gadget_udc_stop,
  2605. .match_ep = cdns3_gadget_match_ep,
  2606. .check_config = cdns3_gadget_check_config,
  2607. };
  2608. static void cdns3_free_all_eps(struct cdns3_device *priv_dev)
  2609. {
  2610. int i;
  2611. /* ep0 OUT point to ep0 IN. */
  2612. priv_dev->eps[16] = NULL;
  2613. for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
  2614. if (priv_dev->eps[i]) {
  2615. cdns3_free_trb_pool(priv_dev->eps[i]);
  2616. devm_kfree(priv_dev->dev, priv_dev->eps[i]);
  2617. }
  2618. }
  2619. /**
  2620. * cdns3_init_eps - Initializes software endpoints of gadget
  2621. * @priv_dev: extended gadget object
  2622. *
  2623. * Returns 0 on success, error code elsewhere
  2624. */
  2625. static int cdns3_init_eps(struct cdns3_device *priv_dev)
  2626. {
  2627. u32 ep_enabled_reg, iso_ep_reg;
  2628. struct cdns3_endpoint *priv_ep;
  2629. int ep_dir, ep_number;
  2630. u32 ep_mask;
  2631. int ret = 0;
  2632. int i;
  2633. /* Read it from USB_CAP3 to USB_CAP5 */
  2634. ep_enabled_reg = readl(&priv_dev->regs->usb_cap3);
  2635. iso_ep_reg = readl(&priv_dev->regs->usb_cap4);
  2636. dev_dbg(priv_dev->dev, "Initializing non-zero endpoints\n");
  2637. for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
  2638. ep_dir = i >> 4; /* i div 16 */
  2639. ep_number = i & 0xF; /* i % 16 */
  2640. ep_mask = BIT(i);
  2641. if (!(ep_enabled_reg & ep_mask))
  2642. continue;
  2643. if (ep_dir && !ep_number) {
  2644. priv_dev->eps[i] = priv_dev->eps[0];
  2645. continue;
  2646. }
  2647. priv_ep = devm_kzalloc(priv_dev->dev, sizeof(*priv_ep),
  2648. GFP_KERNEL);
  2649. if (!priv_ep)
  2650. goto err;
  2651. /* set parent of endpoint object */
  2652. priv_ep->cdns3_dev = priv_dev;
  2653. priv_dev->eps[i] = priv_ep;
  2654. priv_ep->num = ep_number;
  2655. priv_ep->dir = ep_dir ? USB_DIR_IN : USB_DIR_OUT;
  2656. if (!ep_number) {
  2657. ret = cdns3_init_ep0(priv_dev, priv_ep);
  2658. if (ret) {
  2659. dev_err(priv_dev->dev, "Failed to init ep0\n");
  2660. goto err;
  2661. }
  2662. } else {
  2663. snprintf(priv_ep->name, sizeof(priv_ep->name), "ep%d%s",
  2664. ep_number, !!ep_dir ? "in" : "out");
  2665. priv_ep->endpoint.name = priv_ep->name;
  2666. usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
  2667. CDNS3_EP_MAX_PACKET_LIMIT);
  2668. priv_ep->endpoint.max_streams = CDNS3_EP_MAX_STREAMS;
  2669. priv_ep->endpoint.ops = &cdns3_gadget_ep_ops;
  2670. if (ep_dir)
  2671. priv_ep->endpoint.caps.dir_in = 1;
  2672. else
  2673. priv_ep->endpoint.caps.dir_out = 1;
  2674. if (iso_ep_reg & ep_mask)
  2675. priv_ep->endpoint.caps.type_iso = 1;
  2676. priv_ep->endpoint.caps.type_bulk = 1;
  2677. priv_ep->endpoint.caps.type_int = 1;
  2678. list_add_tail(&priv_ep->endpoint.ep_list,
  2679. &priv_dev->gadget.ep_list);
  2680. }
  2681. priv_ep->flags = 0;
  2682. dev_dbg(priv_dev->dev, "Initialized %s support: %s %s\n",
  2683. priv_ep->name,
  2684. priv_ep->endpoint.caps.type_bulk ? "BULK, INT" : "",
  2685. priv_ep->endpoint.caps.type_iso ? "ISO" : "");
  2686. INIT_LIST_HEAD(&priv_ep->pending_req_list);
  2687. INIT_LIST_HEAD(&priv_ep->deferred_req_list);
  2688. INIT_LIST_HEAD(&priv_ep->wa2_descmiss_req_list);
  2689. }
  2690. return 0;
  2691. err:
  2692. cdns3_free_all_eps(priv_dev);
  2693. return -ENOMEM;
  2694. }
  2695. static void cdns3_gadget_release(struct device *dev)
  2696. {
  2697. struct cdns3_device *priv_dev = container_of(dev,
  2698. struct cdns3_device, gadget.dev);
  2699. kfree(priv_dev);
  2700. }
  2701. static void cdns3_gadget_exit(struct cdns *cdns)
  2702. {
  2703. struct cdns3_device *priv_dev;
  2704. priv_dev = cdns->gadget_dev;
  2705. pm_runtime_mark_last_busy(cdns->dev);
  2706. pm_runtime_put_autosuspend(cdns->dev);
  2707. usb_del_gadget(&priv_dev->gadget);
  2708. devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
  2709. cdns3_free_all_eps(priv_dev);
  2710. while (!list_empty(&priv_dev->aligned_buf_list)) {
  2711. struct cdns3_aligned_buf *buf;
  2712. buf = cdns3_next_align_buf(&priv_dev->aligned_buf_list);
  2713. dma_free_noncoherent(priv_dev->sysdev, buf->size,
  2714. buf->buf,
  2715. buf->dma,
  2716. buf->dir);
  2717. list_del(&buf->list);
  2718. kfree(buf);
  2719. }
  2720. dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
  2721. priv_dev->setup_dma);
  2722. dma_pool_destroy(priv_dev->eps_dma_pool);
  2723. kfree(priv_dev->zlp_buf);
  2724. usb_put_gadget(&priv_dev->gadget);
  2725. cdns->gadget_dev = NULL;
  2726. cdns_drd_gadget_off(cdns);
  2727. }
  2728. static int cdns3_gadget_start(struct cdns *cdns)
  2729. {
  2730. struct cdns3_device *priv_dev;
  2731. u32 max_speed;
  2732. int ret;
  2733. priv_dev = kzalloc(sizeof(*priv_dev), GFP_KERNEL);
  2734. if (!priv_dev)
  2735. return -ENOMEM;
  2736. usb_initialize_gadget(cdns->dev, &priv_dev->gadget,
  2737. cdns3_gadget_release);
  2738. cdns->gadget_dev = priv_dev;
  2739. priv_dev->sysdev = cdns->dev;
  2740. priv_dev->dev = cdns->dev;
  2741. priv_dev->regs = cdns->dev_regs;
  2742. device_property_read_u16(priv_dev->dev, "cdns,on-chip-buff-size",
  2743. &priv_dev->onchip_buffers);
  2744. if (priv_dev->onchip_buffers <= 0) {
  2745. u32 reg = readl(&priv_dev->regs->usb_cap2);
  2746. priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg);
  2747. }
  2748. if (!priv_dev->onchip_buffers)
  2749. priv_dev->onchip_buffers = 256;
  2750. max_speed = usb_get_maximum_speed(cdns->dev);
  2751. /* Check the maximum_speed parameter */
  2752. switch (max_speed) {
  2753. case USB_SPEED_FULL:
  2754. case USB_SPEED_HIGH:
  2755. case USB_SPEED_SUPER:
  2756. break;
  2757. default:
  2758. dev_err(cdns->dev, "invalid maximum_speed parameter %d\n",
  2759. max_speed);
  2760. fallthrough;
  2761. case USB_SPEED_UNKNOWN:
  2762. /* default to superspeed */
  2763. max_speed = USB_SPEED_SUPER;
  2764. break;
  2765. }
  2766. /* fill gadget fields */
  2767. priv_dev->gadget.max_speed = max_speed;
  2768. priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
  2769. priv_dev->gadget.ops = &cdns3_gadget_ops;
  2770. priv_dev->gadget.name = "usb-ss-gadget";
  2771. priv_dev->gadget.quirk_avoids_skb_reserve = 1;
  2772. priv_dev->gadget.irq = cdns->dev_irq;
  2773. spin_lock_init(&priv_dev->lock);
  2774. INIT_WORK(&priv_dev->pending_status_wq,
  2775. cdns3_pending_setup_status_handler);
  2776. INIT_WORK(&priv_dev->aligned_buf_wq,
  2777. cdns3_free_aligned_request_buf);
  2778. /* initialize endpoint container */
  2779. INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
  2780. INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
  2781. priv_dev->eps_dma_pool = dma_pool_create("cdns3_eps_dma_pool",
  2782. priv_dev->sysdev,
  2783. TRB_RING_SIZE, 8, 0);
  2784. if (!priv_dev->eps_dma_pool) {
  2785. dev_err(priv_dev->dev, "Failed to create TRB dma pool\n");
  2786. ret = -ENOMEM;
  2787. goto err1;
  2788. }
  2789. ret = cdns3_init_eps(priv_dev);
  2790. if (ret) {
  2791. dev_err(priv_dev->dev, "Failed to create endpoints\n");
  2792. goto err1;
  2793. }
  2794. /* allocate memory for setup packet buffer */
  2795. priv_dev->setup_buf = dma_alloc_coherent(priv_dev->sysdev, 8,
  2796. &priv_dev->setup_dma, GFP_DMA);
  2797. if (!priv_dev->setup_buf) {
  2798. ret = -ENOMEM;
  2799. goto err2;
  2800. }
  2801. priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6);
  2802. dev_dbg(priv_dev->dev, "Device Controller version: %08x\n",
  2803. readl(&priv_dev->regs->usb_cap6));
  2804. dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n",
  2805. readl(&priv_dev->regs->usb_cap1));
  2806. dev_dbg(priv_dev->dev, "On-Chip memory configuration: %08x\n",
  2807. readl(&priv_dev->regs->usb_cap2));
  2808. priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver);
  2809. if (priv_dev->dev_ver >= DEV_VER_V2)
  2810. priv_dev->gadget.sg_supported = 1;
  2811. priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL);
  2812. if (!priv_dev->zlp_buf) {
  2813. ret = -ENOMEM;
  2814. goto err3;
  2815. }
  2816. /* add USB gadget device */
  2817. ret = usb_add_gadget(&priv_dev->gadget);
  2818. if (ret < 0) {
  2819. dev_err(priv_dev->dev, "Failed to add gadget\n");
  2820. goto err4;
  2821. }
  2822. return 0;
  2823. err4:
  2824. kfree(priv_dev->zlp_buf);
  2825. err3:
  2826. dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
  2827. priv_dev->setup_dma);
  2828. err2:
  2829. cdns3_free_all_eps(priv_dev);
  2830. err1:
  2831. dma_pool_destroy(priv_dev->eps_dma_pool);
  2832. usb_put_gadget(&priv_dev->gadget);
  2833. cdns->gadget_dev = NULL;
  2834. return ret;
  2835. }
  2836. static int __cdns3_gadget_init(struct cdns *cdns)
  2837. {
  2838. int ret = 0;
  2839. /* Ensure 32-bit DMA Mask in case we switched back from Host mode */
  2840. ret = dma_set_mask_and_coherent(cdns->dev, DMA_BIT_MASK(32));
  2841. if (ret) {
  2842. dev_err(cdns->dev, "Failed to set dma mask: %d\n", ret);
  2843. return ret;
  2844. }
  2845. cdns_drd_gadget_on(cdns);
  2846. pm_runtime_get_sync(cdns->dev);
  2847. ret = cdns3_gadget_start(cdns);
  2848. if (ret) {
  2849. pm_runtime_put_sync(cdns->dev);
  2850. return ret;
  2851. }
  2852. /*
  2853. * Because interrupt line can be shared with other components in
  2854. * driver it can't use IRQF_ONESHOT flag here.
  2855. */
  2856. ret = devm_request_threaded_irq(cdns->dev, cdns->dev_irq,
  2857. cdns3_device_irq_handler,
  2858. cdns3_device_thread_irq_handler,
  2859. IRQF_SHARED, dev_name(cdns->dev),
  2860. cdns->gadget_dev);
  2861. if (ret)
  2862. goto err0;
  2863. return 0;
  2864. err0:
  2865. cdns3_gadget_exit(cdns);
  2866. return ret;
  2867. }
  2868. static int cdns3_gadget_suspend(struct cdns *cdns, bool do_wakeup)
  2869. __must_hold(&cdns->lock)
  2870. {
  2871. struct cdns3_device *priv_dev = cdns->gadget_dev;
  2872. spin_unlock(&cdns->lock);
  2873. cdns3_disconnect_gadget(priv_dev);
  2874. spin_lock(&cdns->lock);
  2875. priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
  2876. usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
  2877. cdns3_hw_reset_eps_config(priv_dev);
  2878. /* disable interrupt for device */
  2879. writel(0, &priv_dev->regs->usb_ien);
  2880. return 0;
  2881. }
  2882. static int cdns3_gadget_resume(struct cdns *cdns, bool hibernated)
  2883. {
  2884. struct cdns3_device *priv_dev = cdns->gadget_dev;
  2885. if (!priv_dev->gadget_driver)
  2886. return 0;
  2887. cdns3_gadget_config(priv_dev);
  2888. if (hibernated)
  2889. writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
  2890. return 0;
  2891. }
  2892. /**
  2893. * cdns3_gadget_init - initialize device structure
  2894. *
  2895. * @cdns: cdns instance
  2896. *
  2897. * This function initializes the gadget.
  2898. */
  2899. int cdns3_gadget_init(struct cdns *cdns)
  2900. {
  2901. struct cdns_role_driver *rdrv;
  2902. rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
  2903. if (!rdrv)
  2904. return -ENOMEM;
  2905. rdrv->start = __cdns3_gadget_init;
  2906. rdrv->stop = cdns3_gadget_exit;
  2907. rdrv->suspend = cdns3_gadget_suspend;
  2908. rdrv->resume = cdns3_gadget_resume;
  2909. rdrv->state = CDNS_ROLE_STATE_INACTIVE;
  2910. rdrv->name = "gadget";
  2911. cdns->roles[USB_ROLE_DEVICE] = rdrv;
  2912. return 0;
  2913. }