cdns3-gadget.c 91 KB

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