gadget.c 88 KB

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