gadget.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796
  1. /**
  2. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Copyright 2008 Openmoko, Inc.
  6. * Copyright 2008 Simtec Electronics
  7. * Ben Dooks <ben@simtec.co.uk>
  8. * http://armlinux.simtec.co.uk/
  9. *
  10. * S3C USB2.0 High-speed / OtG driver
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/mutex.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/slab.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/usb/gadget.h>
  30. #include <linux/usb/phy.h>
  31. #include "core.h"
  32. #include "hw.h"
  33. /* conversion functions */
  34. static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
  35. {
  36. return container_of(req, struct dwc2_hsotg_req, req);
  37. }
  38. static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
  39. {
  40. return container_of(ep, struct dwc2_hsotg_ep, ep);
  41. }
  42. static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
  43. {
  44. return container_of(gadget, struct dwc2_hsotg, gadget);
  45. }
  46. static inline void __orr32(void __iomem *ptr, u32 val)
  47. {
  48. dwc2_writel(dwc2_readl(ptr) | val, ptr);
  49. }
  50. static inline void __bic32(void __iomem *ptr, u32 val)
  51. {
  52. dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
  53. }
  54. static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
  55. u32 ep_index, u32 dir_in)
  56. {
  57. if (dir_in)
  58. return hsotg->eps_in[ep_index];
  59. else
  60. return hsotg->eps_out[ep_index];
  61. }
  62. /* forward declaration of functions */
  63. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
  64. /**
  65. * using_dma - return the DMA status of the driver.
  66. * @hsotg: The driver state.
  67. *
  68. * Return true if we're using DMA.
  69. *
  70. * Currently, we have the DMA support code worked into everywhere
  71. * that needs it, but the AMBA DMA implementation in the hardware can
  72. * only DMA from 32bit aligned addresses. This means that gadgets such
  73. * as the CDC Ethernet cannot work as they often pass packets which are
  74. * not 32bit aligned.
  75. *
  76. * Unfortunately the choice to use DMA or not is global to the controller
  77. * and seems to be only settable when the controller is being put through
  78. * a core reset. This means we either need to fix the gadgets to take
  79. * account of DMA alignment, or add bounce buffers (yuerk).
  80. *
  81. * g_using_dma is set depending on dts flag.
  82. */
  83. static inline bool using_dma(struct dwc2_hsotg *hsotg)
  84. {
  85. return hsotg->params.g_dma;
  86. }
  87. /*
  88. * using_desc_dma - return the descriptor DMA status of the driver.
  89. * @hsotg: The driver state.
  90. *
  91. * Return true if we're using descriptor DMA.
  92. */
  93. static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
  94. {
  95. return hsotg->params.g_dma_desc;
  96. }
  97. /**
  98. * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
  99. * @hs_ep: The endpoint
  100. * @increment: The value to increment by
  101. *
  102. * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
  103. * If an overrun occurs it will wrap the value and set the frame_overrun flag.
  104. */
  105. static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
  106. {
  107. hs_ep->target_frame += hs_ep->interval;
  108. if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
  109. hs_ep->frame_overrun = 1;
  110. hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
  111. } else {
  112. hs_ep->frame_overrun = 0;
  113. }
  114. }
  115. /**
  116. * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
  117. * @hsotg: The device state
  118. * @ints: A bitmask of the interrupts to enable
  119. */
  120. static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  121. {
  122. u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  123. u32 new_gsintmsk;
  124. new_gsintmsk = gsintmsk | ints;
  125. if (new_gsintmsk != gsintmsk) {
  126. dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
  127. dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
  128. }
  129. }
  130. /**
  131. * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
  132. * @hsotg: The device state
  133. * @ints: A bitmask of the interrupts to enable
  134. */
  135. static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
  136. {
  137. u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  138. u32 new_gsintmsk;
  139. new_gsintmsk = gsintmsk & ~ints;
  140. if (new_gsintmsk != gsintmsk)
  141. dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
  142. }
  143. /**
  144. * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
  145. * @hsotg: The device state
  146. * @ep: The endpoint index
  147. * @dir_in: True if direction is in.
  148. * @en: The enable value, true to enable
  149. *
  150. * Set or clear the mask for an individual endpoint's interrupt
  151. * request.
  152. */
  153. static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
  154. unsigned int ep, unsigned int dir_in,
  155. unsigned int en)
  156. {
  157. unsigned long flags;
  158. u32 bit = 1 << ep;
  159. u32 daint;
  160. if (!dir_in)
  161. bit <<= 16;
  162. local_irq_save(flags);
  163. daint = dwc2_readl(hsotg->regs + DAINTMSK);
  164. if (en)
  165. daint |= bit;
  166. else
  167. daint &= ~bit;
  168. dwc2_writel(daint, hsotg->regs + DAINTMSK);
  169. local_irq_restore(flags);
  170. }
  171. /**
  172. * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
  173. * @hsotg: The device instance.
  174. */
  175. static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
  176. {
  177. unsigned int ep;
  178. unsigned int addr;
  179. int timeout;
  180. u32 val;
  181. u32 *txfsz = hsotg->params.g_tx_fifo_size;
  182. /* Reset fifo map if not correctly cleared during previous session */
  183. WARN_ON(hsotg->fifo_map);
  184. hsotg->fifo_map = 0;
  185. /* set RX/NPTX FIFO sizes */
  186. dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
  187. dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
  188. (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
  189. hsotg->regs + GNPTXFSIZ);
  190. /*
  191. * arange all the rest of the TX FIFOs, as some versions of this
  192. * block have overlapping default addresses. This also ensures
  193. * that if the settings have been changed, then they are set to
  194. * known values.
  195. */
  196. /* start at the end of the GNPTXFSIZ, rounded up */
  197. addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
  198. /*
  199. * Configure fifos sizes from provided configuration and assign
  200. * them to endpoints dynamically according to maxpacket size value of
  201. * given endpoint.
  202. */
  203. for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
  204. if (!txfsz[ep])
  205. continue;
  206. val = addr;
  207. val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
  208. WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
  209. "insufficient fifo memory");
  210. addr += txfsz[ep];
  211. dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
  212. val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
  213. }
  214. /*
  215. * according to p428 of the design guide, we need to ensure that
  216. * all fifos are flushed before continuing
  217. */
  218. dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
  219. GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
  220. /* wait until the fifos are both flushed */
  221. timeout = 100;
  222. while (1) {
  223. val = dwc2_readl(hsotg->regs + GRSTCTL);
  224. if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
  225. break;
  226. if (--timeout == 0) {
  227. dev_err(hsotg->dev,
  228. "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
  229. __func__, val);
  230. break;
  231. }
  232. udelay(1);
  233. }
  234. dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
  235. }
  236. /**
  237. * @ep: USB endpoint to allocate request for.
  238. * @flags: Allocation flags
  239. *
  240. * Allocate a new USB request structure appropriate for the specified endpoint
  241. */
  242. static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
  243. gfp_t flags)
  244. {
  245. struct dwc2_hsotg_req *req;
  246. req = kzalloc(sizeof(struct dwc2_hsotg_req), flags);
  247. if (!req)
  248. return NULL;
  249. INIT_LIST_HEAD(&req->queue);
  250. return &req->req;
  251. }
  252. /**
  253. * is_ep_periodic - return true if the endpoint is in periodic mode.
  254. * @hs_ep: The endpoint to query.
  255. *
  256. * Returns true if the endpoint is in periodic mode, meaning it is being
  257. * used for an Interrupt or ISO transfer.
  258. */
  259. static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
  260. {
  261. return hs_ep->periodic;
  262. }
  263. /**
  264. * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
  265. * @hsotg: The device state.
  266. * @hs_ep: The endpoint for the request
  267. * @hs_req: The request being processed.
  268. *
  269. * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
  270. * of a request to ensure the buffer is ready for access by the caller.
  271. */
  272. static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
  273. struct dwc2_hsotg_ep *hs_ep,
  274. struct dwc2_hsotg_req *hs_req)
  275. {
  276. struct usb_request *req = &hs_req->req;
  277. usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
  278. }
  279. /*
  280. * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
  281. * for Control endpoint
  282. * @hsotg: The device state.
  283. *
  284. * This function will allocate 4 descriptor chains for EP 0: 2 for
  285. * Setup stage, per one for IN and OUT data/status transactions.
  286. */
  287. static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
  288. {
  289. hsotg->setup_desc[0] =
  290. dmam_alloc_coherent(hsotg->dev,
  291. sizeof(struct dwc2_dma_desc),
  292. &hsotg->setup_desc_dma[0],
  293. GFP_KERNEL);
  294. if (!hsotg->setup_desc[0])
  295. goto fail;
  296. hsotg->setup_desc[1] =
  297. dmam_alloc_coherent(hsotg->dev,
  298. sizeof(struct dwc2_dma_desc),
  299. &hsotg->setup_desc_dma[1],
  300. GFP_KERNEL);
  301. if (!hsotg->setup_desc[1])
  302. goto fail;
  303. hsotg->ctrl_in_desc =
  304. dmam_alloc_coherent(hsotg->dev,
  305. sizeof(struct dwc2_dma_desc),
  306. &hsotg->ctrl_in_desc_dma,
  307. GFP_KERNEL);
  308. if (!hsotg->ctrl_in_desc)
  309. goto fail;
  310. hsotg->ctrl_out_desc =
  311. dmam_alloc_coherent(hsotg->dev,
  312. sizeof(struct dwc2_dma_desc),
  313. &hsotg->ctrl_out_desc_dma,
  314. GFP_KERNEL);
  315. if (!hsotg->ctrl_out_desc)
  316. goto fail;
  317. return 0;
  318. fail:
  319. return -ENOMEM;
  320. }
  321. /**
  322. * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
  323. * @hsotg: The controller state.
  324. * @hs_ep: The endpoint we're going to write for.
  325. * @hs_req: The request to write data for.
  326. *
  327. * This is called when the TxFIFO has some space in it to hold a new
  328. * transmission and we have something to give it. The actual setup of
  329. * the data size is done elsewhere, so all we have to do is to actually
  330. * write the data.
  331. *
  332. * The return value is zero if there is more space (or nothing was done)
  333. * otherwise -ENOSPC is returned if the FIFO space was used up.
  334. *
  335. * This routine is only needed for PIO
  336. */
  337. static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
  338. struct dwc2_hsotg_ep *hs_ep,
  339. struct dwc2_hsotg_req *hs_req)
  340. {
  341. bool periodic = is_ep_periodic(hs_ep);
  342. u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
  343. int buf_pos = hs_req->req.actual;
  344. int to_write = hs_ep->size_loaded;
  345. void *data;
  346. int can_write;
  347. int pkt_round;
  348. int max_transfer;
  349. to_write -= (buf_pos - hs_ep->last_load);
  350. /* if there's nothing to write, get out early */
  351. if (to_write == 0)
  352. return 0;
  353. if (periodic && !hsotg->dedicated_fifos) {
  354. u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  355. int size_left;
  356. int size_done;
  357. /*
  358. * work out how much data was loaded so we can calculate
  359. * how much data is left in the fifo.
  360. */
  361. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  362. /*
  363. * if shared fifo, we cannot write anything until the
  364. * previous data has been completely sent.
  365. */
  366. if (hs_ep->fifo_load != 0) {
  367. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  368. return -ENOSPC;
  369. }
  370. dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
  371. __func__, size_left,
  372. hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
  373. /* how much of the data has moved */
  374. size_done = hs_ep->size_loaded - size_left;
  375. /* how much data is left in the fifo */
  376. can_write = hs_ep->fifo_load - size_done;
  377. dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
  378. __func__, can_write);
  379. can_write = hs_ep->fifo_size - can_write;
  380. dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
  381. __func__, can_write);
  382. if (can_write <= 0) {
  383. dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
  384. return -ENOSPC;
  385. }
  386. } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
  387. can_write = dwc2_readl(hsotg->regs +
  388. DTXFSTS(hs_ep->fifo_index));
  389. can_write &= 0xffff;
  390. can_write *= 4;
  391. } else {
  392. if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
  393. dev_dbg(hsotg->dev,
  394. "%s: no queue slots available (0x%08x)\n",
  395. __func__, gnptxsts);
  396. dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
  397. return -ENOSPC;
  398. }
  399. can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
  400. can_write *= 4; /* fifo size is in 32bit quantities. */
  401. }
  402. max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
  403. dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
  404. __func__, gnptxsts, can_write, to_write, max_transfer);
  405. /*
  406. * limit to 512 bytes of data, it seems at least on the non-periodic
  407. * FIFO, requests of >512 cause the endpoint to get stuck with a
  408. * fragment of the end of the transfer in it.
  409. */
  410. if (can_write > 512 && !periodic)
  411. can_write = 512;
  412. /*
  413. * limit the write to one max-packet size worth of data, but allow
  414. * the transfer to return that it did not run out of fifo space
  415. * doing it.
  416. */
  417. if (to_write > max_transfer) {
  418. to_write = max_transfer;
  419. /* it's needed only when we do not use dedicated fifos */
  420. if (!hsotg->dedicated_fifos)
  421. dwc2_hsotg_en_gsint(hsotg,
  422. periodic ? GINTSTS_PTXFEMP :
  423. GINTSTS_NPTXFEMP);
  424. }
  425. /* see if we can write data */
  426. if (to_write > can_write) {
  427. to_write = can_write;
  428. pkt_round = to_write % max_transfer;
  429. /*
  430. * Round the write down to an
  431. * exact number of packets.
  432. *
  433. * Note, we do not currently check to see if we can ever
  434. * write a full packet or not to the FIFO.
  435. */
  436. if (pkt_round)
  437. to_write -= pkt_round;
  438. /*
  439. * enable correct FIFO interrupt to alert us when there
  440. * is more room left.
  441. */
  442. /* it's needed only when we do not use dedicated fifos */
  443. if (!hsotg->dedicated_fifos)
  444. dwc2_hsotg_en_gsint(hsotg,
  445. periodic ? GINTSTS_PTXFEMP :
  446. GINTSTS_NPTXFEMP);
  447. }
  448. dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
  449. to_write, hs_req->req.length, can_write, buf_pos);
  450. if (to_write <= 0)
  451. return -ENOSPC;
  452. hs_req->req.actual = buf_pos + to_write;
  453. hs_ep->total_data += to_write;
  454. if (periodic)
  455. hs_ep->fifo_load += to_write;
  456. to_write = DIV_ROUND_UP(to_write, 4);
  457. data = hs_req->req.buf + buf_pos;
  458. iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
  459. return (to_write >= can_write) ? -ENOSPC : 0;
  460. }
  461. /**
  462. * get_ep_limit - get the maximum data legnth for this endpoint
  463. * @hs_ep: The endpoint
  464. *
  465. * Return the maximum data that can be queued in one go on a given endpoint
  466. * so that transfers that are too long can be split.
  467. */
  468. static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
  469. {
  470. int index = hs_ep->index;
  471. unsigned maxsize;
  472. unsigned maxpkt;
  473. if (index != 0) {
  474. maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
  475. maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
  476. } else {
  477. maxsize = 64+64;
  478. if (hs_ep->dir_in)
  479. maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
  480. else
  481. maxpkt = 2;
  482. }
  483. /* we made the constant loading easier above by using +1 */
  484. maxpkt--;
  485. maxsize--;
  486. /*
  487. * constrain by packet count if maxpkts*pktsize is greater
  488. * than the length register size.
  489. */
  490. if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
  491. maxsize = maxpkt * hs_ep->ep.maxpacket;
  492. return maxsize;
  493. }
  494. /**
  495. * dwc2_hsotg_read_frameno - read current frame number
  496. * @hsotg: The device instance
  497. *
  498. * Return the current frame number
  499. */
  500. static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
  501. {
  502. u32 dsts;
  503. dsts = dwc2_readl(hsotg->regs + DSTS);
  504. dsts &= DSTS_SOFFN_MASK;
  505. dsts >>= DSTS_SOFFN_SHIFT;
  506. return dsts;
  507. }
  508. /**
  509. * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
  510. * DMA descriptor chain prepared for specific endpoint
  511. * @hs_ep: The endpoint
  512. *
  513. * Return the maximum data that can be queued in one go on a given endpoint
  514. * depending on its descriptor chain capacity so that transfers that
  515. * are too long can be split.
  516. */
  517. static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
  518. {
  519. int is_isoc = hs_ep->isochronous;
  520. unsigned int maxsize;
  521. if (is_isoc)
  522. maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
  523. DEV_DMA_ISOC_RX_NBYTES_LIMIT;
  524. else
  525. maxsize = DEV_DMA_NBYTES_LIMIT;
  526. /* Above size of one descriptor was chosen, multiple it */
  527. maxsize *= MAX_DMA_DESC_NUM_GENERIC;
  528. return maxsize;
  529. }
  530. /*
  531. * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
  532. * @hs_ep: The endpoint
  533. * @mask: RX/TX bytes mask to be defined
  534. *
  535. * Returns maximum data payload for one descriptor after analyzing endpoint
  536. * characteristics.
  537. * DMA descriptor transfer bytes limit depends on EP type:
  538. * Control out - MPS,
  539. * Isochronous - descriptor rx/tx bytes bitfield limit,
  540. * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
  541. * have concatenations from various descriptors within one packet.
  542. *
  543. * Selects corresponding mask for RX/TX bytes as well.
  544. */
  545. static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
  546. {
  547. u32 mps = hs_ep->ep.maxpacket;
  548. int dir_in = hs_ep->dir_in;
  549. u32 desc_size = 0;
  550. if (!hs_ep->index && !dir_in) {
  551. desc_size = mps;
  552. *mask = DEV_DMA_NBYTES_MASK;
  553. } else if (hs_ep->isochronous) {
  554. if (dir_in) {
  555. desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
  556. *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
  557. } else {
  558. desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
  559. *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
  560. }
  561. } else {
  562. desc_size = DEV_DMA_NBYTES_LIMIT;
  563. *mask = DEV_DMA_NBYTES_MASK;
  564. /* Round down desc_size to be mps multiple */
  565. desc_size -= desc_size % mps;
  566. }
  567. return desc_size;
  568. }
  569. /*
  570. * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
  571. * @hs_ep: The endpoint
  572. * @dma_buff: DMA address to use
  573. * @len: Length of the transfer
  574. *
  575. * This function will iterate over descriptor chain and fill its entries
  576. * with corresponding information based on transfer data.
  577. */
  578. static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
  579. dma_addr_t dma_buff,
  580. unsigned int len)
  581. {
  582. struct dwc2_hsotg *hsotg = hs_ep->parent;
  583. int dir_in = hs_ep->dir_in;
  584. struct dwc2_dma_desc *desc = hs_ep->desc_list;
  585. u32 mps = hs_ep->ep.maxpacket;
  586. u32 maxsize = 0;
  587. u32 offset = 0;
  588. u32 mask = 0;
  589. int i;
  590. maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
  591. hs_ep->desc_count = (len / maxsize) +
  592. ((len % maxsize) ? 1 : 0);
  593. if (len == 0)
  594. hs_ep->desc_count = 1;
  595. for (i = 0; i < hs_ep->desc_count; ++i) {
  596. desc->status = 0;
  597. desc->status |= (DEV_DMA_BUFF_STS_HBUSY
  598. << DEV_DMA_BUFF_STS_SHIFT);
  599. if (len > maxsize) {
  600. if (!hs_ep->index && !dir_in)
  601. desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
  602. desc->status |= (maxsize <<
  603. DEV_DMA_NBYTES_SHIFT & mask);
  604. desc->buf = dma_buff + offset;
  605. len -= maxsize;
  606. offset += maxsize;
  607. } else {
  608. desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
  609. if (dir_in)
  610. desc->status |= (len % mps) ? DEV_DMA_SHORT :
  611. ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
  612. if (len > maxsize)
  613. dev_err(hsotg->dev, "wrong len %d\n", len);
  614. desc->status |=
  615. len << DEV_DMA_NBYTES_SHIFT & mask;
  616. desc->buf = dma_buff + offset;
  617. }
  618. desc->status &= ~DEV_DMA_BUFF_STS_MASK;
  619. desc->status |= (DEV_DMA_BUFF_STS_HREADY
  620. << DEV_DMA_BUFF_STS_SHIFT);
  621. desc++;
  622. }
  623. }
  624. /*
  625. * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
  626. * @hs_ep: The isochronous endpoint.
  627. * @dma_buff: usb requests dma buffer.
  628. * @len: usb request transfer length.
  629. *
  630. * Finds out index of first free entry either in the bottom or up half of
  631. * descriptor chain depend on which is under SW control and not processed
  632. * by HW. Then fills that descriptor with the data of the arrived usb request,
  633. * frame info, sets Last and IOC bits increments next_desc. If filled
  634. * descriptor is not the first one, removes L bit from the previous descriptor
  635. * status.
  636. */
  637. static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
  638. dma_addr_t dma_buff, unsigned int len)
  639. {
  640. struct dwc2_dma_desc *desc;
  641. struct dwc2_hsotg *hsotg = hs_ep->parent;
  642. u32 index;
  643. u32 maxsize = 0;
  644. u32 mask = 0;
  645. maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
  646. if (len > maxsize) {
  647. dev_err(hsotg->dev, "wrong len %d\n", len);
  648. return -EINVAL;
  649. }
  650. /*
  651. * If SW has already filled half of chain, then return and wait for
  652. * the other chain to be processed by HW.
  653. */
  654. if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2)
  655. return -EBUSY;
  656. /* Increment frame number by interval for IN */
  657. if (hs_ep->dir_in)
  658. dwc2_gadget_incr_frame_num(hs_ep);
  659. index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num +
  660. hs_ep->next_desc;
  661. /* Sanity check of calculated index */
  662. if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) ||
  663. (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) {
  664. dev_err(hsotg->dev, "wrong index %d for iso chain\n", index);
  665. return -EINVAL;
  666. }
  667. desc = &hs_ep->desc_list[index];
  668. /* Clear L bit of previous desc if more than one entries in the chain */
  669. if (hs_ep->next_desc)
  670. hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
  671. dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
  672. __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
  673. desc->status = 0;
  674. desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
  675. desc->buf = dma_buff;
  676. desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
  677. ((len << DEV_DMA_NBYTES_SHIFT) & mask));
  678. if (hs_ep->dir_in) {
  679. desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) &
  680. DEV_DMA_ISOC_PID_MASK) |
  681. ((len % hs_ep->ep.maxpacket) ?
  682. DEV_DMA_SHORT : 0) |
  683. ((hs_ep->target_frame <<
  684. DEV_DMA_ISOC_FRNUM_SHIFT) &
  685. DEV_DMA_ISOC_FRNUM_MASK);
  686. }
  687. desc->status &= ~DEV_DMA_BUFF_STS_MASK;
  688. desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
  689. /* Update index of last configured entry in the chain */
  690. hs_ep->next_desc++;
  691. return 0;
  692. }
  693. /*
  694. * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
  695. * @hs_ep: The isochronous endpoint.
  696. *
  697. * Prepare first descriptor chain for isochronous endpoints. Afterwards
  698. * write DMA address to HW and enable the endpoint.
  699. *
  700. * Switch between descriptor chains via isoc_chain_num to give SW opportunity
  701. * to prepare second descriptor chain while first one is being processed by HW.
  702. */
  703. static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
  704. {
  705. struct dwc2_hsotg *hsotg = hs_ep->parent;
  706. struct dwc2_hsotg_req *hs_req, *treq;
  707. int index = hs_ep->index;
  708. int ret;
  709. u32 dma_reg;
  710. u32 depctl;
  711. u32 ctrl;
  712. if (list_empty(&hs_ep->queue)) {
  713. dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
  714. return;
  715. }
  716. list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
  717. ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
  718. hs_req->req.length);
  719. if (ret) {
  720. dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
  721. break;
  722. }
  723. }
  724. depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  725. dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
  726. /* write descriptor chain address to control register */
  727. dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
  728. ctrl = dwc2_readl(hsotg->regs + depctl);
  729. ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
  730. dwc2_writel(ctrl, hsotg->regs + depctl);
  731. /* Switch ISOC descriptor chain number being processed by SW*/
  732. hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
  733. hs_ep->next_desc = 0;
  734. }
  735. /**
  736. * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
  737. * @hsotg: The controller state.
  738. * @hs_ep: The endpoint to process a request for
  739. * @hs_req: The request to start.
  740. * @continuing: True if we are doing more for the current request.
  741. *
  742. * Start the given request running by setting the endpoint registers
  743. * appropriately, and writing any data to the FIFOs.
  744. */
  745. static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
  746. struct dwc2_hsotg_ep *hs_ep,
  747. struct dwc2_hsotg_req *hs_req,
  748. bool continuing)
  749. {
  750. struct usb_request *ureq = &hs_req->req;
  751. int index = hs_ep->index;
  752. int dir_in = hs_ep->dir_in;
  753. u32 epctrl_reg;
  754. u32 epsize_reg;
  755. u32 epsize;
  756. u32 ctrl;
  757. unsigned length;
  758. unsigned packets;
  759. unsigned maxreq;
  760. unsigned int dma_reg;
  761. if (index != 0) {
  762. if (hs_ep->req && !continuing) {
  763. dev_err(hsotg->dev, "%s: active request\n", __func__);
  764. WARN_ON(1);
  765. return;
  766. } else if (hs_ep->req != hs_req && continuing) {
  767. dev_err(hsotg->dev,
  768. "%s: continue different req\n", __func__);
  769. WARN_ON(1);
  770. return;
  771. }
  772. }
  773. dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
  774. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  775. epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  776. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
  777. __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
  778. hs_ep->dir_in ? "in" : "out");
  779. /* If endpoint is stalled, we will restart request later */
  780. ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  781. if (index && ctrl & DXEPCTL_STALL) {
  782. dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
  783. return;
  784. }
  785. length = ureq->length - ureq->actual;
  786. dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
  787. ureq->length, ureq->actual);
  788. if (!using_desc_dma(hsotg))
  789. maxreq = get_ep_limit(hs_ep);
  790. else
  791. maxreq = dwc2_gadget_get_chain_limit(hs_ep);
  792. if (length > maxreq) {
  793. int round = maxreq % hs_ep->ep.maxpacket;
  794. dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
  795. __func__, length, maxreq, round);
  796. /* round down to multiple of packets */
  797. if (round)
  798. maxreq -= round;
  799. length = maxreq;
  800. }
  801. if (length)
  802. packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
  803. else
  804. packets = 1; /* send one packet if length is zero. */
  805. if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
  806. dev_err(hsotg->dev, "req length > maxpacket*mc\n");
  807. return;
  808. }
  809. if (dir_in && index != 0)
  810. if (hs_ep->isochronous)
  811. epsize = DXEPTSIZ_MC(packets);
  812. else
  813. epsize = DXEPTSIZ_MC(1);
  814. else
  815. epsize = 0;
  816. /*
  817. * zero length packet should be programmed on its own and should not
  818. * be counted in DIEPTSIZ.PktCnt with other packets.
  819. */
  820. if (dir_in && ureq->zero && !continuing) {
  821. /* Test if zlp is actually required. */
  822. if ((ureq->length >= hs_ep->ep.maxpacket) &&
  823. !(ureq->length % hs_ep->ep.maxpacket))
  824. hs_ep->send_zlp = 1;
  825. }
  826. epsize |= DXEPTSIZ_PKTCNT(packets);
  827. epsize |= DXEPTSIZ_XFERSIZE(length);
  828. dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
  829. __func__, packets, length, ureq->length, epsize, epsize_reg);
  830. /* store the request as the current one we're doing */
  831. hs_ep->req = hs_req;
  832. if (using_desc_dma(hsotg)) {
  833. u32 offset = 0;
  834. u32 mps = hs_ep->ep.maxpacket;
  835. /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
  836. if (!dir_in) {
  837. if (!index)
  838. length = mps;
  839. else if (length % mps)
  840. length += (mps - (length % mps));
  841. }
  842. /*
  843. * If more data to send, adjust DMA for EP0 out data stage.
  844. * ureq->dma stays unchanged, hence increment it by already
  845. * passed passed data count before starting new transaction.
  846. */
  847. if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
  848. continuing)
  849. offset = ureq->actual;
  850. /* Fill DDMA chain entries */
  851. dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
  852. length);
  853. /* write descriptor chain address to control register */
  854. dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
  855. dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
  856. __func__, (u32)hs_ep->desc_list_dma, dma_reg);
  857. } else {
  858. /* write size / packets */
  859. dwc2_writel(epsize, hsotg->regs + epsize_reg);
  860. if (using_dma(hsotg) && !continuing && (length != 0)) {
  861. /*
  862. * write DMA address to control register, buffer
  863. * already synced by dwc2_hsotg_ep_queue().
  864. */
  865. dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
  866. dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
  867. __func__, &ureq->dma, dma_reg);
  868. }
  869. }
  870. if (hs_ep->isochronous && hs_ep->interval == 1) {
  871. hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  872. dwc2_gadget_incr_frame_num(hs_ep);
  873. if (hs_ep->target_frame & 0x1)
  874. ctrl |= DXEPCTL_SETODDFR;
  875. else
  876. ctrl |= DXEPCTL_SETEVENFR;
  877. }
  878. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  879. dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
  880. /* For Setup request do not clear NAK */
  881. if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
  882. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  883. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  884. dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
  885. /*
  886. * set these, it seems that DMA support increments past the end
  887. * of the packet buffer so we need to calculate the length from
  888. * this information.
  889. */
  890. hs_ep->size_loaded = length;
  891. hs_ep->last_load = ureq->actual;
  892. if (dir_in && !using_dma(hsotg)) {
  893. /* set these anyway, we may need them for non-periodic in */
  894. hs_ep->fifo_load = 0;
  895. dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  896. }
  897. /*
  898. * Note, trying to clear the NAK here causes problems with transmit
  899. * on the S3C6400 ending up with the TXFIFO becoming full.
  900. */
  901. /* check ep is enabled */
  902. if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
  903. dev_dbg(hsotg->dev,
  904. "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
  905. index, dwc2_readl(hsotg->regs + epctrl_reg));
  906. dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
  907. __func__, dwc2_readl(hsotg->regs + epctrl_reg));
  908. /* enable ep interrupts */
  909. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
  910. }
  911. /**
  912. * dwc2_hsotg_map_dma - map the DMA memory being used for the request
  913. * @hsotg: The device state.
  914. * @hs_ep: The endpoint the request is on.
  915. * @req: The request being processed.
  916. *
  917. * We've been asked to queue a request, so ensure that the memory buffer
  918. * is correctly setup for DMA. If we've been passed an extant DMA address
  919. * then ensure the buffer has been synced to memory. If our buffer has no
  920. * DMA memory, then we map the memory and mark our request to allow us to
  921. * cleanup on completion.
  922. */
  923. static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
  924. struct dwc2_hsotg_ep *hs_ep,
  925. struct usb_request *req)
  926. {
  927. int ret;
  928. ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
  929. if (ret)
  930. goto dma_error;
  931. return 0;
  932. dma_error:
  933. dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
  934. __func__, req->buf, req->length);
  935. return -EIO;
  936. }
  937. static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
  938. struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
  939. {
  940. void *req_buf = hs_req->req.buf;
  941. /* If dma is not being used or buffer is aligned */
  942. if (!using_dma(hsotg) || !((long)req_buf & 3))
  943. return 0;
  944. WARN_ON(hs_req->saved_req_buf);
  945. dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
  946. hs_ep->ep.name, req_buf, hs_req->req.length);
  947. hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
  948. if (!hs_req->req.buf) {
  949. hs_req->req.buf = req_buf;
  950. dev_err(hsotg->dev,
  951. "%s: unable to allocate memory for bounce buffer\n",
  952. __func__);
  953. return -ENOMEM;
  954. }
  955. /* Save actual buffer */
  956. hs_req->saved_req_buf = req_buf;
  957. if (hs_ep->dir_in)
  958. memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
  959. return 0;
  960. }
  961. static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
  962. struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
  963. {
  964. /* If dma is not being used or buffer was aligned */
  965. if (!using_dma(hsotg) || !hs_req->saved_req_buf)
  966. return;
  967. dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
  968. hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
  969. /* Copy data from bounce buffer on successful out transfer */
  970. if (!hs_ep->dir_in && !hs_req->req.status)
  971. memcpy(hs_req->saved_req_buf, hs_req->req.buf,
  972. hs_req->req.actual);
  973. /* Free bounce buffer */
  974. kfree(hs_req->req.buf);
  975. hs_req->req.buf = hs_req->saved_req_buf;
  976. hs_req->saved_req_buf = NULL;
  977. }
  978. /**
  979. * dwc2_gadget_target_frame_elapsed - Checks target frame
  980. * @hs_ep: The driver endpoint to check
  981. *
  982. * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
  983. * corresponding transfer.
  984. */
  985. static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
  986. {
  987. struct dwc2_hsotg *hsotg = hs_ep->parent;
  988. u32 target_frame = hs_ep->target_frame;
  989. u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
  990. bool frame_overrun = hs_ep->frame_overrun;
  991. if (!frame_overrun && current_frame >= target_frame)
  992. return true;
  993. if (frame_overrun && current_frame >= target_frame &&
  994. ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
  995. return true;
  996. return false;
  997. }
  998. /*
  999. * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
  1000. * @hsotg: The driver state
  1001. * @hs_ep: the ep descriptor chain is for
  1002. *
  1003. * Called to update EP0 structure's pointers depend on stage of
  1004. * control transfer.
  1005. */
  1006. static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
  1007. struct dwc2_hsotg_ep *hs_ep)
  1008. {
  1009. switch (hsotg->ep0_state) {
  1010. case DWC2_EP0_SETUP:
  1011. case DWC2_EP0_STATUS_OUT:
  1012. hs_ep->desc_list = hsotg->setup_desc[0];
  1013. hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
  1014. break;
  1015. case DWC2_EP0_DATA_IN:
  1016. case DWC2_EP0_STATUS_IN:
  1017. hs_ep->desc_list = hsotg->ctrl_in_desc;
  1018. hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
  1019. break;
  1020. case DWC2_EP0_DATA_OUT:
  1021. hs_ep->desc_list = hsotg->ctrl_out_desc;
  1022. hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
  1023. break;
  1024. default:
  1025. dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
  1026. hsotg->ep0_state);
  1027. return -EINVAL;
  1028. }
  1029. return 0;
  1030. }
  1031. static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  1032. gfp_t gfp_flags)
  1033. {
  1034. struct dwc2_hsotg_req *hs_req = our_req(req);
  1035. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1036. struct dwc2_hsotg *hs = hs_ep->parent;
  1037. bool first;
  1038. int ret;
  1039. dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
  1040. ep->name, req, req->length, req->buf, req->no_interrupt,
  1041. req->zero, req->short_not_ok);
  1042. /* Prevent new request submission when controller is suspended */
  1043. if (hs->lx_state == DWC2_L2) {
  1044. dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
  1045. __func__);
  1046. return -EAGAIN;
  1047. }
  1048. /* initialise status of the request */
  1049. INIT_LIST_HEAD(&hs_req->queue);
  1050. req->actual = 0;
  1051. req->status = -EINPROGRESS;
  1052. ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
  1053. if (ret)
  1054. return ret;
  1055. /* if we're using DMA, sync the buffers as necessary */
  1056. if (using_dma(hs)) {
  1057. ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
  1058. if (ret)
  1059. return ret;
  1060. }
  1061. /* If using descriptor DMA configure EP0 descriptor chain pointers */
  1062. if (using_desc_dma(hs) && !hs_ep->index) {
  1063. ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
  1064. if (ret)
  1065. return ret;
  1066. }
  1067. first = list_empty(&hs_ep->queue);
  1068. list_add_tail(&hs_req->queue, &hs_ep->queue);
  1069. /*
  1070. * Handle DDMA isochronous transfers separately - just add new entry
  1071. * to the half of descriptor chain that is not processed by HW.
  1072. * Transfer will be started once SW gets either one of NAK or
  1073. * OutTknEpDis interrupts.
  1074. */
  1075. if (using_desc_dma(hs) && hs_ep->isochronous &&
  1076. hs_ep->target_frame != TARGET_FRAME_INITIAL) {
  1077. ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
  1078. hs_req->req.length);
  1079. if (ret)
  1080. dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__);
  1081. return 0;
  1082. }
  1083. if (first) {
  1084. if (!hs_ep->isochronous) {
  1085. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  1086. return 0;
  1087. }
  1088. while (dwc2_gadget_target_frame_elapsed(hs_ep))
  1089. dwc2_gadget_incr_frame_num(hs_ep);
  1090. if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
  1091. dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
  1092. }
  1093. return 0;
  1094. }
  1095. static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
  1096. gfp_t gfp_flags)
  1097. {
  1098. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1099. struct dwc2_hsotg *hs = hs_ep->parent;
  1100. unsigned long flags = 0;
  1101. int ret = 0;
  1102. spin_lock_irqsave(&hs->lock, flags);
  1103. ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
  1104. spin_unlock_irqrestore(&hs->lock, flags);
  1105. return ret;
  1106. }
  1107. static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
  1108. struct usb_request *req)
  1109. {
  1110. struct dwc2_hsotg_req *hs_req = our_req(req);
  1111. kfree(hs_req);
  1112. }
  1113. /**
  1114. * dwc2_hsotg_complete_oursetup - setup completion callback
  1115. * @ep: The endpoint the request was on.
  1116. * @req: The request completed.
  1117. *
  1118. * Called on completion of any requests the driver itself
  1119. * submitted that need cleaning up.
  1120. */
  1121. static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
  1122. struct usb_request *req)
  1123. {
  1124. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1125. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1126. dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
  1127. dwc2_hsotg_ep_free_request(ep, req);
  1128. }
  1129. /**
  1130. * ep_from_windex - convert control wIndex value to endpoint
  1131. * @hsotg: The driver state.
  1132. * @windex: The control request wIndex field (in host order).
  1133. *
  1134. * Convert the given wIndex into a pointer to an driver endpoint
  1135. * structure, or return NULL if it is not a valid endpoint.
  1136. */
  1137. static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
  1138. u32 windex)
  1139. {
  1140. struct dwc2_hsotg_ep *ep;
  1141. int dir = (windex & USB_DIR_IN) ? 1 : 0;
  1142. int idx = windex & 0x7F;
  1143. if (windex >= 0x100)
  1144. return NULL;
  1145. if (idx > hsotg->num_of_eps)
  1146. return NULL;
  1147. ep = index_to_ep(hsotg, idx, dir);
  1148. if (idx && ep->dir_in != dir)
  1149. return NULL;
  1150. return ep;
  1151. }
  1152. /**
  1153. * dwc2_hsotg_set_test_mode - Enable usb Test Modes
  1154. * @hsotg: The driver state.
  1155. * @testmode: requested usb test mode
  1156. * Enable usb Test Mode requested by the Host.
  1157. */
  1158. int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
  1159. {
  1160. int dctl = dwc2_readl(hsotg->regs + DCTL);
  1161. dctl &= ~DCTL_TSTCTL_MASK;
  1162. switch (testmode) {
  1163. case TEST_J:
  1164. case TEST_K:
  1165. case TEST_SE0_NAK:
  1166. case TEST_PACKET:
  1167. case TEST_FORCE_EN:
  1168. dctl |= testmode << DCTL_TSTCTL_SHIFT;
  1169. break;
  1170. default:
  1171. return -EINVAL;
  1172. }
  1173. dwc2_writel(dctl, hsotg->regs + DCTL);
  1174. return 0;
  1175. }
  1176. /**
  1177. * dwc2_hsotg_send_reply - send reply to control request
  1178. * @hsotg: The device state
  1179. * @ep: Endpoint 0
  1180. * @buff: Buffer for request
  1181. * @length: Length of reply.
  1182. *
  1183. * Create a request and queue it on the given endpoint. This is useful as
  1184. * an internal method of sending replies to certain control requests, etc.
  1185. */
  1186. static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
  1187. struct dwc2_hsotg_ep *ep,
  1188. void *buff,
  1189. int length)
  1190. {
  1191. struct usb_request *req;
  1192. int ret;
  1193. dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
  1194. req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
  1195. hsotg->ep0_reply = req;
  1196. if (!req) {
  1197. dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
  1198. return -ENOMEM;
  1199. }
  1200. req->buf = hsotg->ep0_buff;
  1201. req->length = length;
  1202. /*
  1203. * zero flag is for sending zlp in DATA IN stage. It has no impact on
  1204. * STATUS stage.
  1205. */
  1206. req->zero = 0;
  1207. req->complete = dwc2_hsotg_complete_oursetup;
  1208. if (length)
  1209. memcpy(req->buf, buff, length);
  1210. ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
  1211. if (ret) {
  1212. dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
  1213. return ret;
  1214. }
  1215. return 0;
  1216. }
  1217. /**
  1218. * dwc2_hsotg_process_req_status - process request GET_STATUS
  1219. * @hsotg: The device state
  1220. * @ctrl: USB control request
  1221. */
  1222. static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
  1223. struct usb_ctrlrequest *ctrl)
  1224. {
  1225. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1226. struct dwc2_hsotg_ep *ep;
  1227. __le16 reply;
  1228. int ret;
  1229. dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
  1230. if (!ep0->dir_in) {
  1231. dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
  1232. return -EINVAL;
  1233. }
  1234. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1235. case USB_RECIP_DEVICE:
  1236. reply = cpu_to_le16(0); /* bit 0 => self powered,
  1237. * bit 1 => remote wakeup */
  1238. break;
  1239. case USB_RECIP_INTERFACE:
  1240. /* currently, the data result should be zero */
  1241. reply = cpu_to_le16(0);
  1242. break;
  1243. case USB_RECIP_ENDPOINT:
  1244. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  1245. if (!ep)
  1246. return -ENOENT;
  1247. reply = cpu_to_le16(ep->halted ? 1 : 0);
  1248. break;
  1249. default:
  1250. return 0;
  1251. }
  1252. if (le16_to_cpu(ctrl->wLength) != 2)
  1253. return -EINVAL;
  1254. ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
  1255. if (ret) {
  1256. dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
  1257. return ret;
  1258. }
  1259. return 1;
  1260. }
  1261. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
  1262. /**
  1263. * get_ep_head - return the first request on the endpoint
  1264. * @hs_ep: The controller endpoint to get
  1265. *
  1266. * Get the first request on the endpoint.
  1267. */
  1268. static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
  1269. {
  1270. return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
  1271. queue);
  1272. }
  1273. /**
  1274. * dwc2_gadget_start_next_request - Starts next request from ep queue
  1275. * @hs_ep: Endpoint structure
  1276. *
  1277. * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
  1278. * in its handler. Hence we need to unmask it here to be able to do
  1279. * resynchronization.
  1280. */
  1281. static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
  1282. {
  1283. u32 mask;
  1284. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1285. int dir_in = hs_ep->dir_in;
  1286. struct dwc2_hsotg_req *hs_req;
  1287. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  1288. if (!list_empty(&hs_ep->queue)) {
  1289. hs_req = get_ep_head(hs_ep);
  1290. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
  1291. return;
  1292. }
  1293. if (!hs_ep->isochronous)
  1294. return;
  1295. if (dir_in) {
  1296. dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
  1297. __func__);
  1298. } else {
  1299. dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
  1300. __func__);
  1301. mask = dwc2_readl(hsotg->regs + epmsk_reg);
  1302. mask |= DOEPMSK_OUTTKNEPDISMSK;
  1303. dwc2_writel(mask, hsotg->regs + epmsk_reg);
  1304. }
  1305. }
  1306. /**
  1307. * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
  1308. * @hsotg: The device state
  1309. * @ctrl: USB control request
  1310. */
  1311. static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
  1312. struct usb_ctrlrequest *ctrl)
  1313. {
  1314. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1315. struct dwc2_hsotg_req *hs_req;
  1316. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  1317. struct dwc2_hsotg_ep *ep;
  1318. int ret;
  1319. bool halted;
  1320. u32 recip;
  1321. u32 wValue;
  1322. u32 wIndex;
  1323. dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
  1324. __func__, set ? "SET" : "CLEAR");
  1325. wValue = le16_to_cpu(ctrl->wValue);
  1326. wIndex = le16_to_cpu(ctrl->wIndex);
  1327. recip = ctrl->bRequestType & USB_RECIP_MASK;
  1328. switch (recip) {
  1329. case USB_RECIP_DEVICE:
  1330. switch (wValue) {
  1331. case USB_DEVICE_TEST_MODE:
  1332. if ((wIndex & 0xff) != 0)
  1333. return -EINVAL;
  1334. if (!set)
  1335. return -EINVAL;
  1336. hsotg->test_mode = wIndex >> 8;
  1337. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1338. if (ret) {
  1339. dev_err(hsotg->dev,
  1340. "%s: failed to send reply\n", __func__);
  1341. return ret;
  1342. }
  1343. break;
  1344. default:
  1345. return -ENOENT;
  1346. }
  1347. break;
  1348. case USB_RECIP_ENDPOINT:
  1349. ep = ep_from_windex(hsotg, wIndex);
  1350. if (!ep) {
  1351. dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
  1352. __func__, wIndex);
  1353. return -ENOENT;
  1354. }
  1355. switch (wValue) {
  1356. case USB_ENDPOINT_HALT:
  1357. halted = ep->halted;
  1358. dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
  1359. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1360. if (ret) {
  1361. dev_err(hsotg->dev,
  1362. "%s: failed to send reply\n", __func__);
  1363. return ret;
  1364. }
  1365. /*
  1366. * we have to complete all requests for ep if it was
  1367. * halted, and the halt was cleared by CLEAR_FEATURE
  1368. */
  1369. if (!set && halted) {
  1370. /*
  1371. * If we have request in progress,
  1372. * then complete it
  1373. */
  1374. if (ep->req) {
  1375. hs_req = ep->req;
  1376. ep->req = NULL;
  1377. list_del_init(&hs_req->queue);
  1378. if (hs_req->req.complete) {
  1379. spin_unlock(&hsotg->lock);
  1380. usb_gadget_giveback_request(
  1381. &ep->ep, &hs_req->req);
  1382. spin_lock(&hsotg->lock);
  1383. }
  1384. }
  1385. /* If we have pending request, then start it */
  1386. if (!ep->req) {
  1387. dwc2_gadget_start_next_request(ep);
  1388. }
  1389. }
  1390. break;
  1391. default:
  1392. return -ENOENT;
  1393. }
  1394. break;
  1395. default:
  1396. return -ENOENT;
  1397. }
  1398. return 1;
  1399. }
  1400. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
  1401. /**
  1402. * dwc2_hsotg_stall_ep0 - stall ep0
  1403. * @hsotg: The device state
  1404. *
  1405. * Set stall for ep0 as response for setup request.
  1406. */
  1407. static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
  1408. {
  1409. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1410. u32 reg;
  1411. u32 ctrl;
  1412. dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
  1413. reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
  1414. /*
  1415. * DxEPCTL_Stall will be cleared by EP once it has
  1416. * taken effect, so no need to clear later.
  1417. */
  1418. ctrl = dwc2_readl(hsotg->regs + reg);
  1419. ctrl |= DXEPCTL_STALL;
  1420. ctrl |= DXEPCTL_CNAK;
  1421. dwc2_writel(ctrl, hsotg->regs + reg);
  1422. dev_dbg(hsotg->dev,
  1423. "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
  1424. ctrl, reg, dwc2_readl(hsotg->regs + reg));
  1425. /*
  1426. * complete won't be called, so we enqueue
  1427. * setup request here
  1428. */
  1429. dwc2_hsotg_enqueue_setup(hsotg);
  1430. }
  1431. /**
  1432. * dwc2_hsotg_process_control - process a control request
  1433. * @hsotg: The device state
  1434. * @ctrl: The control request received
  1435. *
  1436. * The controller has received the SETUP phase of a control request, and
  1437. * needs to work out what to do next (and whether to pass it on to the
  1438. * gadget driver).
  1439. */
  1440. static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
  1441. struct usb_ctrlrequest *ctrl)
  1442. {
  1443. struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
  1444. int ret = 0;
  1445. u32 dcfg;
  1446. dev_dbg(hsotg->dev,
  1447. "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
  1448. ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
  1449. ctrl->wIndex, ctrl->wLength);
  1450. if (ctrl->wLength == 0) {
  1451. ep0->dir_in = 1;
  1452. hsotg->ep0_state = DWC2_EP0_STATUS_IN;
  1453. } else if (ctrl->bRequestType & USB_DIR_IN) {
  1454. ep0->dir_in = 1;
  1455. hsotg->ep0_state = DWC2_EP0_DATA_IN;
  1456. } else {
  1457. ep0->dir_in = 0;
  1458. hsotg->ep0_state = DWC2_EP0_DATA_OUT;
  1459. }
  1460. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1461. switch (ctrl->bRequest) {
  1462. case USB_REQ_SET_ADDRESS:
  1463. hsotg->connected = 1;
  1464. dcfg = dwc2_readl(hsotg->regs + DCFG);
  1465. dcfg &= ~DCFG_DEVADDR_MASK;
  1466. dcfg |= (le16_to_cpu(ctrl->wValue) <<
  1467. DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
  1468. dwc2_writel(dcfg, hsotg->regs + DCFG);
  1469. dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
  1470. ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1471. return;
  1472. case USB_REQ_GET_STATUS:
  1473. ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
  1474. break;
  1475. case USB_REQ_CLEAR_FEATURE:
  1476. case USB_REQ_SET_FEATURE:
  1477. ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
  1478. break;
  1479. }
  1480. }
  1481. /* as a fallback, try delivering it to the driver to deal with */
  1482. if (ret == 0 && hsotg->driver) {
  1483. spin_unlock(&hsotg->lock);
  1484. ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
  1485. spin_lock(&hsotg->lock);
  1486. if (ret < 0)
  1487. dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
  1488. }
  1489. /*
  1490. * the request is either unhandlable, or is not formatted correctly
  1491. * so respond with a STALL for the status stage to indicate failure.
  1492. */
  1493. if (ret < 0)
  1494. dwc2_hsotg_stall_ep0(hsotg);
  1495. }
  1496. /**
  1497. * dwc2_hsotg_complete_setup - completion of a setup transfer
  1498. * @ep: The endpoint the request was on.
  1499. * @req: The request completed.
  1500. *
  1501. * Called on completion of any requests the driver itself submitted for
  1502. * EP0 setup packets
  1503. */
  1504. static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
  1505. struct usb_request *req)
  1506. {
  1507. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  1508. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1509. if (req->status < 0) {
  1510. dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
  1511. return;
  1512. }
  1513. spin_lock(&hsotg->lock);
  1514. if (req->actual == 0)
  1515. dwc2_hsotg_enqueue_setup(hsotg);
  1516. else
  1517. dwc2_hsotg_process_control(hsotg, req->buf);
  1518. spin_unlock(&hsotg->lock);
  1519. }
  1520. /**
  1521. * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
  1522. * @hsotg: The device state.
  1523. *
  1524. * Enqueue a request on EP0 if necessary to received any SETUP packets
  1525. * received from the host.
  1526. */
  1527. static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
  1528. {
  1529. struct usb_request *req = hsotg->ctrl_req;
  1530. struct dwc2_hsotg_req *hs_req = our_req(req);
  1531. int ret;
  1532. dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
  1533. req->zero = 0;
  1534. req->length = 8;
  1535. req->buf = hsotg->ctrl_buff;
  1536. req->complete = dwc2_hsotg_complete_setup;
  1537. if (!list_empty(&hs_req->queue)) {
  1538. dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
  1539. return;
  1540. }
  1541. hsotg->eps_out[0]->dir_in = 0;
  1542. hsotg->eps_out[0]->send_zlp = 0;
  1543. hsotg->ep0_state = DWC2_EP0_SETUP;
  1544. ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
  1545. if (ret < 0) {
  1546. dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
  1547. /*
  1548. * Don't think there's much we can do other than watch the
  1549. * driver fail.
  1550. */
  1551. }
  1552. }
  1553. static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
  1554. struct dwc2_hsotg_ep *hs_ep)
  1555. {
  1556. u32 ctrl;
  1557. u8 index = hs_ep->index;
  1558. u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  1559. u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  1560. if (hs_ep->dir_in)
  1561. dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
  1562. index);
  1563. else
  1564. dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
  1565. index);
  1566. if (using_desc_dma(hsotg)) {
  1567. /* Not specific buffer needed for ep0 ZLP */
  1568. dma_addr_t dma = hs_ep->desc_list_dma;
  1569. dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
  1570. dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
  1571. } else {
  1572. dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  1573. DXEPTSIZ_XFERSIZE(0), hsotg->regs +
  1574. epsiz_reg);
  1575. }
  1576. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  1577. ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
  1578. ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
  1579. ctrl |= DXEPCTL_USBACTEP;
  1580. dwc2_writel(ctrl, hsotg->regs + epctl_reg);
  1581. }
  1582. /**
  1583. * dwc2_hsotg_complete_request - complete a request given to us
  1584. * @hsotg: The device state.
  1585. * @hs_ep: The endpoint the request was on.
  1586. * @hs_req: The request to complete.
  1587. * @result: The result code (0 => Ok, otherwise errno)
  1588. *
  1589. * The given request has finished, so call the necessary completion
  1590. * if it has one and then look to see if we can start a new request
  1591. * on the endpoint.
  1592. *
  1593. * Note, expects the ep to already be locked as appropriate.
  1594. */
  1595. static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
  1596. struct dwc2_hsotg_ep *hs_ep,
  1597. struct dwc2_hsotg_req *hs_req,
  1598. int result)
  1599. {
  1600. if (!hs_req) {
  1601. dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
  1602. return;
  1603. }
  1604. dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
  1605. hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
  1606. /*
  1607. * only replace the status if we've not already set an error
  1608. * from a previous transaction
  1609. */
  1610. if (hs_req->req.status == -EINPROGRESS)
  1611. hs_req->req.status = result;
  1612. if (using_dma(hsotg))
  1613. dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
  1614. dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
  1615. hs_ep->req = NULL;
  1616. list_del_init(&hs_req->queue);
  1617. /*
  1618. * call the complete request with the locks off, just in case the
  1619. * request tries to queue more work for this endpoint.
  1620. */
  1621. if (hs_req->req.complete) {
  1622. spin_unlock(&hsotg->lock);
  1623. usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
  1624. spin_lock(&hsotg->lock);
  1625. }
  1626. /* In DDMA don't need to proceed to starting of next ISOC request */
  1627. if (using_desc_dma(hsotg) && hs_ep->isochronous)
  1628. return;
  1629. /*
  1630. * Look to see if there is anything else to do. Note, the completion
  1631. * of the previous request may have caused a new request to be started
  1632. * so be careful when doing this.
  1633. */
  1634. if (!hs_ep->req && result >= 0) {
  1635. dwc2_gadget_start_next_request(hs_ep);
  1636. }
  1637. }
  1638. /*
  1639. * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
  1640. * @hs_ep: The endpoint the request was on.
  1641. *
  1642. * Get first request from the ep queue, determine descriptor on which complete
  1643. * happened. SW based on isoc_chain_num discovers which half of the descriptor
  1644. * chain is currently in use by HW, adjusts dma_address and calculates index
  1645. * of completed descriptor based on the value of DEPDMA register. Update actual
  1646. * length of request, giveback to gadget.
  1647. */
  1648. static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
  1649. {
  1650. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1651. struct dwc2_hsotg_req *hs_req;
  1652. struct usb_request *ureq;
  1653. int index;
  1654. dma_addr_t dma_addr;
  1655. u32 dma_reg;
  1656. u32 depdma;
  1657. u32 desc_sts;
  1658. u32 mask;
  1659. hs_req = get_ep_head(hs_ep);
  1660. if (!hs_req) {
  1661. dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
  1662. return;
  1663. }
  1664. ureq = &hs_req->req;
  1665. dma_addr = hs_ep->desc_list_dma;
  1666. /*
  1667. * If lower half of descriptor chain is currently use by SW,
  1668. * that means higher half is being processed by HW, so shift
  1669. * DMA address to higher half of descriptor chain.
  1670. */
  1671. if (!hs_ep->isoc_chain_num)
  1672. dma_addr += sizeof(struct dwc2_dma_desc) *
  1673. (MAX_DMA_DESC_NUM_GENERIC / 2);
  1674. dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index);
  1675. depdma = dwc2_readl(hsotg->regs + dma_reg);
  1676. index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1;
  1677. desc_sts = hs_ep->desc_list[index].status;
  1678. mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
  1679. DEV_DMA_ISOC_RX_NBYTES_MASK;
  1680. ureq->actual = ureq->length -
  1681. ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT);
  1682. /* Adjust actual length for ISOC Out if length is not align of 4 */
  1683. if (!hs_ep->dir_in && ureq->length & 0x3)
  1684. ureq->actual += 4 - (ureq->length & 0x3);
  1685. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1686. }
  1687. /*
  1688. * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any.
  1689. * @hs_ep: The isochronous endpoint to be re-enabled.
  1690. *
  1691. * If ep has been disabled due to last descriptor servicing (IN endpoint) or
  1692. * BNA (OUT endpoint) check the status of other half of descriptor chain that
  1693. * was under SW control till HW was busy and restart the endpoint if needed.
  1694. */
  1695. static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
  1696. {
  1697. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1698. u32 depctl;
  1699. u32 dma_reg;
  1700. u32 ctrl;
  1701. u32 dma_addr = hs_ep->desc_list_dma;
  1702. unsigned char index = hs_ep->index;
  1703. dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
  1704. depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
  1705. ctrl = dwc2_readl(hsotg->regs + depctl);
  1706. /*
  1707. * EP was disabled if HW has processed last descriptor or BNA was set.
  1708. * So restart ep if SW has prepared new descriptor chain in ep_queue
  1709. * routine while HW was busy.
  1710. */
  1711. if (!(ctrl & DXEPCTL_EPENA)) {
  1712. if (!hs_ep->next_desc) {
  1713. dev_dbg(hsotg->dev, "%s: No more ISOC requests\n",
  1714. __func__);
  1715. return;
  1716. }
  1717. dma_addr += sizeof(struct dwc2_dma_desc) *
  1718. (MAX_DMA_DESC_NUM_GENERIC / 2) *
  1719. hs_ep->isoc_chain_num;
  1720. dwc2_writel(dma_addr, hsotg->regs + dma_reg);
  1721. ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
  1722. dwc2_writel(ctrl, hsotg->regs + depctl);
  1723. /* Switch ISOC descriptor chain number being processed by SW*/
  1724. hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
  1725. hs_ep->next_desc = 0;
  1726. dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n",
  1727. __func__);
  1728. }
  1729. }
  1730. /**
  1731. * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
  1732. * @hsotg: The device state.
  1733. * @ep_idx: The endpoint index for the data
  1734. * @size: The size of data in the fifo, in bytes
  1735. *
  1736. * The FIFO status shows there is data to read from the FIFO for a given
  1737. * endpoint, so sort out whether we need to read the data into a request
  1738. * that has been made for that endpoint.
  1739. */
  1740. static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
  1741. {
  1742. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
  1743. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1744. void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
  1745. int to_read;
  1746. int max_req;
  1747. int read_ptr;
  1748. if (!hs_req) {
  1749. u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
  1750. int ptr;
  1751. dev_dbg(hsotg->dev,
  1752. "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
  1753. __func__, size, ep_idx, epctl);
  1754. /* dump the data from the FIFO, we've nothing we can do */
  1755. for (ptr = 0; ptr < size; ptr += 4)
  1756. (void)dwc2_readl(fifo);
  1757. return;
  1758. }
  1759. to_read = size;
  1760. read_ptr = hs_req->req.actual;
  1761. max_req = hs_req->req.length - read_ptr;
  1762. dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
  1763. __func__, to_read, max_req, read_ptr, hs_req->req.length);
  1764. if (to_read > max_req) {
  1765. /*
  1766. * more data appeared than we where willing
  1767. * to deal with in this request.
  1768. */
  1769. /* currently we don't deal this */
  1770. WARN_ON_ONCE(1);
  1771. }
  1772. hs_ep->total_data += to_read;
  1773. hs_req->req.actual += to_read;
  1774. to_read = DIV_ROUND_UP(to_read, 4);
  1775. /*
  1776. * note, we might over-write the buffer end by 3 bytes depending on
  1777. * alignment of the data.
  1778. */
  1779. ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
  1780. }
  1781. /**
  1782. * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
  1783. * @hsotg: The device instance
  1784. * @dir_in: If IN zlp
  1785. *
  1786. * Generate a zero-length IN packet request for terminating a SETUP
  1787. * transaction.
  1788. *
  1789. * Note, since we don't write any data to the TxFIFO, then it is
  1790. * currently believed that we do not need to wait for any space in
  1791. * the TxFIFO.
  1792. */
  1793. static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
  1794. {
  1795. /* eps_out[0] is used in both directions */
  1796. hsotg->eps_out[0]->dir_in = dir_in;
  1797. hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
  1798. dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
  1799. }
  1800. static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
  1801. u32 epctl_reg)
  1802. {
  1803. u32 ctrl;
  1804. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  1805. if (ctrl & DXEPCTL_EOFRNUM)
  1806. ctrl |= DXEPCTL_SETEVENFR;
  1807. else
  1808. ctrl |= DXEPCTL_SETODDFR;
  1809. dwc2_writel(ctrl, hsotg->regs + epctl_reg);
  1810. }
  1811. /*
  1812. * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
  1813. * @hs_ep - The endpoint on which transfer went
  1814. *
  1815. * Iterate over endpoints descriptor chain and get info on bytes remained
  1816. * in DMA descriptors after transfer has completed. Used for non isoc EPs.
  1817. */
  1818. static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
  1819. {
  1820. struct dwc2_hsotg *hsotg = hs_ep->parent;
  1821. unsigned int bytes_rem = 0;
  1822. struct dwc2_dma_desc *desc = hs_ep->desc_list;
  1823. int i;
  1824. u32 status;
  1825. if (!desc)
  1826. return -EINVAL;
  1827. for (i = 0; i < hs_ep->desc_count; ++i) {
  1828. status = desc->status;
  1829. bytes_rem += status & DEV_DMA_NBYTES_MASK;
  1830. if (status & DEV_DMA_STS_MASK)
  1831. dev_err(hsotg->dev, "descriptor %d closed with %x\n",
  1832. i, status & DEV_DMA_STS_MASK);
  1833. }
  1834. return bytes_rem;
  1835. }
  1836. /**
  1837. * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
  1838. * @hsotg: The device instance
  1839. * @epnum: The endpoint received from
  1840. *
  1841. * The RXFIFO has delivered an OutDone event, which means that the data
  1842. * transfer for an OUT endpoint has been completed, either by a short
  1843. * packet or by the finish of a transfer.
  1844. */
  1845. static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
  1846. {
  1847. u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
  1848. struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
  1849. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  1850. struct usb_request *req = &hs_req->req;
  1851. unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  1852. int result = 0;
  1853. if (!hs_req) {
  1854. dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
  1855. return;
  1856. }
  1857. if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
  1858. dev_dbg(hsotg->dev, "zlp packet received\n");
  1859. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1860. dwc2_hsotg_enqueue_setup(hsotg);
  1861. return;
  1862. }
  1863. if (using_desc_dma(hsotg))
  1864. size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
  1865. if (using_dma(hsotg)) {
  1866. unsigned size_done;
  1867. /*
  1868. * Calculate the size of the transfer by checking how much
  1869. * is left in the endpoint size register and then working it
  1870. * out from the amount we loaded for the transfer.
  1871. *
  1872. * We need to do this as DMA pointers are always 32bit aligned
  1873. * so may overshoot/undershoot the transfer.
  1874. */
  1875. size_done = hs_ep->size_loaded - size_left;
  1876. size_done += hs_ep->last_load;
  1877. req->actual = size_done;
  1878. }
  1879. /* if there is more request to do, schedule new transfer */
  1880. if (req->actual < req->length && size_left == 0) {
  1881. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  1882. return;
  1883. }
  1884. if (req->actual < req->length && req->short_not_ok) {
  1885. dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
  1886. __func__, req->actual, req->length);
  1887. /*
  1888. * todo - what should we return here? there's no one else
  1889. * even bothering to check the status.
  1890. */
  1891. }
  1892. /* DDMA IN status phase will start from StsPhseRcvd interrupt */
  1893. if (!using_desc_dma(hsotg) && epnum == 0 &&
  1894. hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
  1895. /* Move to STATUS IN */
  1896. dwc2_hsotg_ep0_zlp(hsotg, true);
  1897. return;
  1898. }
  1899. /*
  1900. * Slave mode OUT transfers do not go through XferComplete so
  1901. * adjust the ISOC parity here.
  1902. */
  1903. if (!using_dma(hsotg)) {
  1904. if (hs_ep->isochronous && hs_ep->interval == 1)
  1905. dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
  1906. else if (hs_ep->isochronous && hs_ep->interval > 1)
  1907. dwc2_gadget_incr_frame_num(hs_ep);
  1908. }
  1909. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
  1910. }
  1911. /**
  1912. * dwc2_hsotg_handle_rx - RX FIFO has data
  1913. * @hsotg: The device instance
  1914. *
  1915. * The IRQ handler has detected that the RX FIFO has some data in it
  1916. * that requires processing, so find out what is in there and do the
  1917. * appropriate read.
  1918. *
  1919. * The RXFIFO is a true FIFO, the packets coming out are still in packet
  1920. * chunks, so if you have x packets received on an endpoint you'll get x
  1921. * FIFO events delivered, each with a packet's worth of data in it.
  1922. *
  1923. * When using DMA, we should not be processing events from the RXFIFO
  1924. * as the actual data should be sent to the memory directly and we turn
  1925. * on the completion interrupts to get notifications of transfer completion.
  1926. */
  1927. static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
  1928. {
  1929. u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
  1930. u32 epnum, status, size;
  1931. WARN_ON(using_dma(hsotg));
  1932. epnum = grxstsr & GRXSTS_EPNUM_MASK;
  1933. status = grxstsr & GRXSTS_PKTSTS_MASK;
  1934. size = grxstsr & GRXSTS_BYTECNT_MASK;
  1935. size >>= GRXSTS_BYTECNT_SHIFT;
  1936. dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
  1937. __func__, grxstsr, size, epnum);
  1938. switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
  1939. case GRXSTS_PKTSTS_GLOBALOUTNAK:
  1940. dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
  1941. break;
  1942. case GRXSTS_PKTSTS_OUTDONE:
  1943. dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
  1944. dwc2_hsotg_read_frameno(hsotg));
  1945. if (!using_dma(hsotg))
  1946. dwc2_hsotg_handle_outdone(hsotg, epnum);
  1947. break;
  1948. case GRXSTS_PKTSTS_SETUPDONE:
  1949. dev_dbg(hsotg->dev,
  1950. "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  1951. dwc2_hsotg_read_frameno(hsotg),
  1952. dwc2_readl(hsotg->regs + DOEPCTL(0)));
  1953. /*
  1954. * Call dwc2_hsotg_handle_outdone here if it was not called from
  1955. * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
  1956. * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
  1957. */
  1958. if (hsotg->ep0_state == DWC2_EP0_SETUP)
  1959. dwc2_hsotg_handle_outdone(hsotg, epnum);
  1960. break;
  1961. case GRXSTS_PKTSTS_OUTRX:
  1962. dwc2_hsotg_rx_data(hsotg, epnum, size);
  1963. break;
  1964. case GRXSTS_PKTSTS_SETUPRX:
  1965. dev_dbg(hsotg->dev,
  1966. "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  1967. dwc2_hsotg_read_frameno(hsotg),
  1968. dwc2_readl(hsotg->regs + DOEPCTL(0)));
  1969. WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
  1970. dwc2_hsotg_rx_data(hsotg, epnum, size);
  1971. break;
  1972. default:
  1973. dev_warn(hsotg->dev, "%s: unknown status %08x\n",
  1974. __func__, grxstsr);
  1975. dwc2_hsotg_dump(hsotg);
  1976. break;
  1977. }
  1978. }
  1979. /**
  1980. * dwc2_hsotg_ep0_mps - turn max packet size into register setting
  1981. * @mps: The maximum packet size in bytes.
  1982. */
  1983. static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
  1984. {
  1985. switch (mps) {
  1986. case 64:
  1987. return D0EPCTL_MPS_64;
  1988. case 32:
  1989. return D0EPCTL_MPS_32;
  1990. case 16:
  1991. return D0EPCTL_MPS_16;
  1992. case 8:
  1993. return D0EPCTL_MPS_8;
  1994. }
  1995. /* bad max packet size, warn and return invalid result */
  1996. WARN_ON(1);
  1997. return (u32)-1;
  1998. }
  1999. /**
  2000. * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
  2001. * @hsotg: The driver state.
  2002. * @ep: The index number of the endpoint
  2003. * @mps: The maximum packet size in bytes
  2004. * @mc: The multicount value
  2005. *
  2006. * Configure the maximum packet size for the given endpoint, updating
  2007. * the hardware control registers to reflect this.
  2008. */
  2009. static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
  2010. unsigned int ep, unsigned int mps,
  2011. unsigned int mc, unsigned int dir_in)
  2012. {
  2013. struct dwc2_hsotg_ep *hs_ep;
  2014. void __iomem *regs = hsotg->regs;
  2015. u32 reg;
  2016. hs_ep = index_to_ep(hsotg, ep, dir_in);
  2017. if (!hs_ep)
  2018. return;
  2019. if (ep == 0) {
  2020. u32 mps_bytes = mps;
  2021. /* EP0 is a special case */
  2022. mps = dwc2_hsotg_ep0_mps(mps_bytes);
  2023. if (mps > 3)
  2024. goto bad_mps;
  2025. hs_ep->ep.maxpacket = mps_bytes;
  2026. hs_ep->mc = 1;
  2027. } else {
  2028. if (mps > 1024)
  2029. goto bad_mps;
  2030. hs_ep->mc = mc;
  2031. if (mc > 3)
  2032. goto bad_mps;
  2033. hs_ep->ep.maxpacket = mps;
  2034. }
  2035. if (dir_in) {
  2036. reg = dwc2_readl(regs + DIEPCTL(ep));
  2037. reg &= ~DXEPCTL_MPS_MASK;
  2038. reg |= mps;
  2039. dwc2_writel(reg, regs + DIEPCTL(ep));
  2040. } else {
  2041. reg = dwc2_readl(regs + DOEPCTL(ep));
  2042. reg &= ~DXEPCTL_MPS_MASK;
  2043. reg |= mps;
  2044. dwc2_writel(reg, regs + DOEPCTL(ep));
  2045. }
  2046. return;
  2047. bad_mps:
  2048. dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
  2049. }
  2050. /**
  2051. * dwc2_hsotg_txfifo_flush - flush Tx FIFO
  2052. * @hsotg: The driver state
  2053. * @idx: The index for the endpoint (0..15)
  2054. */
  2055. static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
  2056. {
  2057. int timeout;
  2058. int val;
  2059. dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
  2060. hsotg->regs + GRSTCTL);
  2061. /* wait until the fifo is flushed */
  2062. timeout = 100;
  2063. while (1) {
  2064. val = dwc2_readl(hsotg->regs + GRSTCTL);
  2065. if ((val & (GRSTCTL_TXFFLSH)) == 0)
  2066. break;
  2067. if (--timeout == 0) {
  2068. dev_err(hsotg->dev,
  2069. "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
  2070. __func__, val);
  2071. break;
  2072. }
  2073. udelay(1);
  2074. }
  2075. }
  2076. /**
  2077. * dwc2_hsotg_trytx - check to see if anything needs transmitting
  2078. * @hsotg: The driver state
  2079. * @hs_ep: The driver endpoint to check.
  2080. *
  2081. * Check to see if there is a request that has data to send, and if so
  2082. * make an attempt to write data into the FIFO.
  2083. */
  2084. static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
  2085. struct dwc2_hsotg_ep *hs_ep)
  2086. {
  2087. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2088. if (!hs_ep->dir_in || !hs_req) {
  2089. /**
  2090. * if request is not enqueued, we disable interrupts
  2091. * for endpoints, excepting ep0
  2092. */
  2093. if (hs_ep->index != 0)
  2094. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
  2095. hs_ep->dir_in, 0);
  2096. return 0;
  2097. }
  2098. if (hs_req->req.actual < hs_req->req.length) {
  2099. dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
  2100. hs_ep->index);
  2101. return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  2102. }
  2103. return 0;
  2104. }
  2105. /**
  2106. * dwc2_hsotg_complete_in - complete IN transfer
  2107. * @hsotg: The device state.
  2108. * @hs_ep: The endpoint that has just completed.
  2109. *
  2110. * An IN transfer has been completed, update the transfer's state and then
  2111. * call the relevant completion routines.
  2112. */
  2113. static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
  2114. struct dwc2_hsotg_ep *hs_ep)
  2115. {
  2116. struct dwc2_hsotg_req *hs_req = hs_ep->req;
  2117. u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  2118. int size_left, size_done;
  2119. if (!hs_req) {
  2120. dev_dbg(hsotg->dev, "XferCompl but no req\n");
  2121. return;
  2122. }
  2123. /* Finish ZLP handling for IN EP0 transactions */
  2124. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
  2125. dev_dbg(hsotg->dev, "zlp packet sent\n");
  2126. /*
  2127. * While send zlp for DWC2_EP0_STATUS_IN EP direction was
  2128. * changed to IN. Change back to complete OUT transfer request
  2129. */
  2130. hs_ep->dir_in = 0;
  2131. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2132. if (hsotg->test_mode) {
  2133. int ret;
  2134. ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
  2135. if (ret < 0) {
  2136. dev_dbg(hsotg->dev, "Invalid Test #%d\n",
  2137. hsotg->test_mode);
  2138. dwc2_hsotg_stall_ep0(hsotg);
  2139. return;
  2140. }
  2141. }
  2142. dwc2_hsotg_enqueue_setup(hsotg);
  2143. return;
  2144. }
  2145. /*
  2146. * Calculate the size of the transfer by checking how much is left
  2147. * in the endpoint size register and then working it out from
  2148. * the amount we loaded for the transfer.
  2149. *
  2150. * We do this even for DMA, as the transfer may have incremented
  2151. * past the end of the buffer (DMA transfers are always 32bit
  2152. * aligned).
  2153. */
  2154. if (using_desc_dma(hsotg)) {
  2155. size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
  2156. if (size_left < 0)
  2157. dev_err(hsotg->dev, "error parsing DDMA results %d\n",
  2158. size_left);
  2159. } else {
  2160. size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
  2161. }
  2162. size_done = hs_ep->size_loaded - size_left;
  2163. size_done += hs_ep->last_load;
  2164. if (hs_req->req.actual != size_done)
  2165. dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
  2166. __func__, hs_req->req.actual, size_done);
  2167. hs_req->req.actual = size_done;
  2168. dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
  2169. hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
  2170. if (!size_left && hs_req->req.actual < hs_req->req.length) {
  2171. dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
  2172. dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  2173. return;
  2174. }
  2175. /* Zlp for all endpoints, for ep0 only in DATA IN stage */
  2176. if (hs_ep->send_zlp) {
  2177. dwc2_hsotg_program_zlp(hsotg, hs_ep);
  2178. hs_ep->send_zlp = 0;
  2179. /* transfer will be completed on next complete interrupt */
  2180. return;
  2181. }
  2182. if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
  2183. /* Move to STATUS OUT */
  2184. dwc2_hsotg_ep0_zlp(hsotg, false);
  2185. return;
  2186. }
  2187. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  2188. }
  2189. /**
  2190. * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
  2191. * @hsotg: The device state.
  2192. * @idx: Index of ep.
  2193. * @dir_in: Endpoint direction 1-in 0-out.
  2194. *
  2195. * Reads for endpoint with given index and direction, by masking
  2196. * epint_reg with coresponding mask.
  2197. */
  2198. static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
  2199. unsigned int idx, int dir_in)
  2200. {
  2201. u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
  2202. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  2203. u32 ints;
  2204. u32 mask;
  2205. u32 diepempmsk;
  2206. mask = dwc2_readl(hsotg->regs + epmsk_reg);
  2207. diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
  2208. mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
  2209. mask |= DXEPINT_SETUP_RCVD;
  2210. ints = dwc2_readl(hsotg->regs + epint_reg);
  2211. ints &= mask;
  2212. return ints;
  2213. }
  2214. /**
  2215. * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
  2216. * @hs_ep: The endpoint on which interrupt is asserted.
  2217. *
  2218. * This interrupt indicates that the endpoint has been disabled per the
  2219. * application's request.
  2220. *
  2221. * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
  2222. * in case of ISOC completes current request.
  2223. *
  2224. * For ISOC-OUT endpoints completes expired requests. If there is remaining
  2225. * request starts it.
  2226. */
  2227. static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
  2228. {
  2229. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2230. struct dwc2_hsotg_req *hs_req;
  2231. unsigned char idx = hs_ep->index;
  2232. int dir_in = hs_ep->dir_in;
  2233. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  2234. int dctl = dwc2_readl(hsotg->regs + DCTL);
  2235. dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
  2236. if (dir_in) {
  2237. int epctl = dwc2_readl(hsotg->regs + epctl_reg);
  2238. dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
  2239. if (hs_ep->isochronous) {
  2240. dwc2_hsotg_complete_in(hsotg, hs_ep);
  2241. return;
  2242. }
  2243. if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
  2244. int dctl = dwc2_readl(hsotg->regs + DCTL);
  2245. dctl |= DCTL_CGNPINNAK;
  2246. dwc2_writel(dctl, hsotg->regs + DCTL);
  2247. }
  2248. return;
  2249. }
  2250. if (dctl & DCTL_GOUTNAKSTS) {
  2251. dctl |= DCTL_CGOUTNAK;
  2252. dwc2_writel(dctl, hsotg->regs + DCTL);
  2253. }
  2254. if (!hs_ep->isochronous)
  2255. return;
  2256. if (list_empty(&hs_ep->queue)) {
  2257. dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
  2258. __func__, hs_ep);
  2259. return;
  2260. }
  2261. do {
  2262. hs_req = get_ep_head(hs_ep);
  2263. if (hs_req)
  2264. dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
  2265. -ENODATA);
  2266. dwc2_gadget_incr_frame_num(hs_ep);
  2267. } while (dwc2_gadget_target_frame_elapsed(hs_ep));
  2268. dwc2_gadget_start_next_request(hs_ep);
  2269. }
  2270. /**
  2271. * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
  2272. * @hs_ep: The endpoint on which interrupt is asserted.
  2273. *
  2274. * This is starting point for ISOC-OUT transfer, synchronization done with
  2275. * first out token received from host while corresponding EP is disabled.
  2276. *
  2277. * Device does not know initial frame in which out token will come. For this
  2278. * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
  2279. * getting this interrupt SW starts calculation for next transfer frame.
  2280. */
  2281. static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
  2282. {
  2283. struct dwc2_hsotg *hsotg = ep->parent;
  2284. int dir_in = ep->dir_in;
  2285. u32 doepmsk;
  2286. u32 tmp;
  2287. if (dir_in || !ep->isochronous)
  2288. return;
  2289. /*
  2290. * Store frame in which irq was asserted here, as
  2291. * it can change while completing request below.
  2292. */
  2293. tmp = dwc2_hsotg_read_frameno(hsotg);
  2294. dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
  2295. if (using_desc_dma(hsotg)) {
  2296. if (ep->target_frame == TARGET_FRAME_INITIAL) {
  2297. /* Start first ISO Out */
  2298. ep->target_frame = tmp;
  2299. dwc2_gadget_start_isoc_ddma(ep);
  2300. }
  2301. return;
  2302. }
  2303. if (ep->interval > 1 &&
  2304. ep->target_frame == TARGET_FRAME_INITIAL) {
  2305. u32 dsts;
  2306. u32 ctrl;
  2307. dsts = dwc2_readl(hsotg->regs + DSTS);
  2308. ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  2309. dwc2_gadget_incr_frame_num(ep);
  2310. ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
  2311. if (ep->target_frame & 0x1)
  2312. ctrl |= DXEPCTL_SETODDFR;
  2313. else
  2314. ctrl |= DXEPCTL_SETEVENFR;
  2315. dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
  2316. }
  2317. dwc2_gadget_start_next_request(ep);
  2318. doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  2319. doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
  2320. dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
  2321. }
  2322. /**
  2323. * dwc2_gadget_handle_nak - handle NAK interrupt
  2324. * @hs_ep: The endpoint on which interrupt is asserted.
  2325. *
  2326. * This is starting point for ISOC-IN transfer, synchronization done with
  2327. * first IN token received from host while corresponding EP is disabled.
  2328. *
  2329. * Device does not know when first one token will arrive from host. On first
  2330. * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
  2331. * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
  2332. * sent in response to that as there was no data in FIFO. SW is basing on this
  2333. * interrupt to obtain frame in which token has come and then based on the
  2334. * interval calculates next frame for transfer.
  2335. */
  2336. static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
  2337. {
  2338. struct dwc2_hsotg *hsotg = hs_ep->parent;
  2339. int dir_in = hs_ep->dir_in;
  2340. if (!dir_in || !hs_ep->isochronous)
  2341. return;
  2342. if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
  2343. hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
  2344. if (using_desc_dma(hsotg)) {
  2345. dwc2_gadget_start_isoc_ddma(hs_ep);
  2346. return;
  2347. }
  2348. if (hs_ep->interval > 1) {
  2349. u32 ctrl = dwc2_readl(hsotg->regs +
  2350. DIEPCTL(hs_ep->index));
  2351. if (hs_ep->target_frame & 0x1)
  2352. ctrl |= DXEPCTL_SETODDFR;
  2353. else
  2354. ctrl |= DXEPCTL_SETEVENFR;
  2355. dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
  2356. }
  2357. dwc2_hsotg_complete_request(hsotg, hs_ep,
  2358. get_ep_head(hs_ep), 0);
  2359. }
  2360. dwc2_gadget_incr_frame_num(hs_ep);
  2361. }
  2362. /**
  2363. * dwc2_hsotg_epint - handle an in/out endpoint interrupt
  2364. * @hsotg: The driver state
  2365. * @idx: The index for the endpoint (0..15)
  2366. * @dir_in: Set if this is an IN endpoint
  2367. *
  2368. * Process and clear any interrupt pending for an individual endpoint
  2369. */
  2370. static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
  2371. int dir_in)
  2372. {
  2373. struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
  2374. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  2375. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  2376. u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
  2377. u32 ints;
  2378. u32 ctrl;
  2379. ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
  2380. ctrl = dwc2_readl(hsotg->regs + epctl_reg);
  2381. /* Clear endpoint interrupts */
  2382. dwc2_writel(ints, hsotg->regs + epint_reg);
  2383. if (!hs_ep) {
  2384. dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
  2385. __func__, idx, dir_in ? "in" : "out");
  2386. return;
  2387. }
  2388. dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
  2389. __func__, idx, dir_in ? "in" : "out", ints);
  2390. /* Don't process XferCompl interrupt if it is a setup packet */
  2391. if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
  2392. ints &= ~DXEPINT_XFERCOMPL;
  2393. /*
  2394. * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
  2395. * stage and xfercomplete was generated without SETUP phase done
  2396. * interrupt. SW should parse received setup packet only after host's
  2397. * exit from setup phase of control transfer.
  2398. */
  2399. if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
  2400. hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
  2401. ints &= ~DXEPINT_XFERCOMPL;
  2402. if (ints & DXEPINT_XFERCOMPL) {
  2403. dev_dbg(hsotg->dev,
  2404. "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
  2405. __func__, dwc2_readl(hsotg->regs + epctl_reg),
  2406. dwc2_readl(hsotg->regs + epsiz_reg));
  2407. /* In DDMA handle isochronous requests separately */
  2408. if (using_desc_dma(hsotg) && hs_ep->isochronous) {
  2409. dwc2_gadget_complete_isoc_request_ddma(hs_ep);
  2410. /* Try to start next isoc request */
  2411. dwc2_gadget_start_next_isoc_ddma(hs_ep);
  2412. } else if (dir_in) {
  2413. /*
  2414. * We get OutDone from the FIFO, so we only
  2415. * need to look at completing IN requests here
  2416. * if operating slave mode
  2417. */
  2418. if (hs_ep->isochronous && hs_ep->interval > 1)
  2419. dwc2_gadget_incr_frame_num(hs_ep);
  2420. dwc2_hsotg_complete_in(hsotg, hs_ep);
  2421. if (ints & DXEPINT_NAKINTRPT)
  2422. ints &= ~DXEPINT_NAKINTRPT;
  2423. if (idx == 0 && !hs_ep->req)
  2424. dwc2_hsotg_enqueue_setup(hsotg);
  2425. } else if (using_dma(hsotg)) {
  2426. /*
  2427. * We're using DMA, we need to fire an OutDone here
  2428. * as we ignore the RXFIFO.
  2429. */
  2430. if (hs_ep->isochronous && hs_ep->interval > 1)
  2431. dwc2_gadget_incr_frame_num(hs_ep);
  2432. dwc2_hsotg_handle_outdone(hsotg, idx);
  2433. }
  2434. }
  2435. if (ints & DXEPINT_EPDISBLD)
  2436. dwc2_gadget_handle_ep_disabled(hs_ep);
  2437. if (ints & DXEPINT_OUTTKNEPDIS)
  2438. dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
  2439. if (ints & DXEPINT_NAKINTRPT)
  2440. dwc2_gadget_handle_nak(hs_ep);
  2441. if (ints & DXEPINT_AHBERR)
  2442. dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
  2443. if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
  2444. dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
  2445. if (using_dma(hsotg) && idx == 0) {
  2446. /*
  2447. * this is the notification we've received a
  2448. * setup packet. In non-DMA mode we'd get this
  2449. * from the RXFIFO, instead we need to process
  2450. * the setup here.
  2451. */
  2452. if (dir_in)
  2453. WARN_ON_ONCE(1);
  2454. else
  2455. dwc2_hsotg_handle_outdone(hsotg, 0);
  2456. }
  2457. }
  2458. if (ints & DXEPINT_STSPHSERCVD) {
  2459. dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
  2460. /* Move to STATUS IN for DDMA */
  2461. if (using_desc_dma(hsotg))
  2462. dwc2_hsotg_ep0_zlp(hsotg, true);
  2463. }
  2464. if (ints & DXEPINT_BACK2BACKSETUP)
  2465. dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
  2466. if (ints & DXEPINT_BNAINTR) {
  2467. dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
  2468. /*
  2469. * Try to start next isoc request, if any.
  2470. * Sometimes the endpoint remains enabled after BNA interrupt
  2471. * assertion, which is not expected, hence we can enter here
  2472. * couple of times.
  2473. */
  2474. if (hs_ep->isochronous)
  2475. dwc2_gadget_start_next_isoc_ddma(hs_ep);
  2476. }
  2477. if (dir_in && !hs_ep->isochronous) {
  2478. /* not sure if this is important, but we'll clear it anyway */
  2479. if (ints & DXEPINT_INTKNTXFEMP) {
  2480. dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
  2481. __func__, idx);
  2482. }
  2483. /* this probably means something bad is happening */
  2484. if (ints & DXEPINT_INTKNEPMIS) {
  2485. dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
  2486. __func__, idx);
  2487. }
  2488. /* FIFO has space or is empty (see GAHBCFG) */
  2489. if (hsotg->dedicated_fifos &&
  2490. ints & DXEPINT_TXFEMP) {
  2491. dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
  2492. __func__, idx);
  2493. if (!using_dma(hsotg))
  2494. dwc2_hsotg_trytx(hsotg, hs_ep);
  2495. }
  2496. }
  2497. }
  2498. /**
  2499. * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
  2500. * @hsotg: The device state.
  2501. *
  2502. * Handle updating the device settings after the enumeration phase has
  2503. * been completed.
  2504. */
  2505. static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
  2506. {
  2507. u32 dsts = dwc2_readl(hsotg->regs + DSTS);
  2508. int ep0_mps = 0, ep_mps = 8;
  2509. /*
  2510. * This should signal the finish of the enumeration phase
  2511. * of the USB handshaking, so we should now know what rate
  2512. * we connected at.
  2513. */
  2514. dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
  2515. /*
  2516. * note, since we're limited by the size of transfer on EP0, and
  2517. * it seems IN transfers must be a even number of packets we do
  2518. * not advertise a 64byte MPS on EP0.
  2519. */
  2520. /* catch both EnumSpd_FS and EnumSpd_FS48 */
  2521. switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
  2522. case DSTS_ENUMSPD_FS:
  2523. case DSTS_ENUMSPD_FS48:
  2524. hsotg->gadget.speed = USB_SPEED_FULL;
  2525. ep0_mps = EP0_MPS_LIMIT;
  2526. ep_mps = 1023;
  2527. break;
  2528. case DSTS_ENUMSPD_HS:
  2529. hsotg->gadget.speed = USB_SPEED_HIGH;
  2530. ep0_mps = EP0_MPS_LIMIT;
  2531. ep_mps = 1024;
  2532. break;
  2533. case DSTS_ENUMSPD_LS:
  2534. hsotg->gadget.speed = USB_SPEED_LOW;
  2535. ep0_mps = 8;
  2536. ep_mps = 8;
  2537. /*
  2538. * note, we don't actually support LS in this driver at the
  2539. * moment, and the documentation seems to imply that it isn't
  2540. * supported by the PHYs on some of the devices.
  2541. */
  2542. break;
  2543. }
  2544. dev_info(hsotg->dev, "new device is %s\n",
  2545. usb_speed_string(hsotg->gadget.speed));
  2546. /*
  2547. * we should now know the maximum packet size for an
  2548. * endpoint, so set the endpoints to a default value.
  2549. */
  2550. if (ep0_mps) {
  2551. int i;
  2552. /* Initialize ep0 for both in and out directions */
  2553. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
  2554. dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
  2555. for (i = 1; i < hsotg->num_of_eps; i++) {
  2556. if (hsotg->eps_in[i])
  2557. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
  2558. 0, 1);
  2559. if (hsotg->eps_out[i])
  2560. dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
  2561. 0, 0);
  2562. }
  2563. }
  2564. /* ensure after enumeration our EP0 is active */
  2565. dwc2_hsotg_enqueue_setup(hsotg);
  2566. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2567. dwc2_readl(hsotg->regs + DIEPCTL0),
  2568. dwc2_readl(hsotg->regs + DOEPCTL0));
  2569. }
  2570. /**
  2571. * kill_all_requests - remove all requests from the endpoint's queue
  2572. * @hsotg: The device state.
  2573. * @ep: The endpoint the requests may be on.
  2574. * @result: The result code to use.
  2575. *
  2576. * Go through the requests on the given endpoint and mark them
  2577. * completed with the given result code.
  2578. */
  2579. static void kill_all_requests(struct dwc2_hsotg *hsotg,
  2580. struct dwc2_hsotg_ep *ep,
  2581. int result)
  2582. {
  2583. struct dwc2_hsotg_req *req, *treq;
  2584. unsigned size;
  2585. ep->req = NULL;
  2586. list_for_each_entry_safe(req, treq, &ep->queue, queue)
  2587. dwc2_hsotg_complete_request(hsotg, ep, req,
  2588. result);
  2589. if (!hsotg->dedicated_fifos)
  2590. return;
  2591. size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
  2592. if (size < ep->fifo_size)
  2593. dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
  2594. }
  2595. /**
  2596. * dwc2_hsotg_disconnect - disconnect service
  2597. * @hsotg: The device state.
  2598. *
  2599. * The device has been disconnected. Remove all current
  2600. * transactions and signal the gadget driver that this
  2601. * has happened.
  2602. */
  2603. void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
  2604. {
  2605. unsigned ep;
  2606. if (!hsotg->connected)
  2607. return;
  2608. hsotg->connected = 0;
  2609. hsotg->test_mode = 0;
  2610. for (ep = 0; ep < hsotg->num_of_eps; ep++) {
  2611. if (hsotg->eps_in[ep])
  2612. kill_all_requests(hsotg, hsotg->eps_in[ep],
  2613. -ESHUTDOWN);
  2614. if (hsotg->eps_out[ep])
  2615. kill_all_requests(hsotg, hsotg->eps_out[ep],
  2616. -ESHUTDOWN);
  2617. }
  2618. call_gadget(hsotg, disconnect);
  2619. hsotg->lx_state = DWC2_L3;
  2620. }
  2621. /**
  2622. * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
  2623. * @hsotg: The device state:
  2624. * @periodic: True if this is a periodic FIFO interrupt
  2625. */
  2626. static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
  2627. {
  2628. struct dwc2_hsotg_ep *ep;
  2629. int epno, ret;
  2630. /* look through for any more data to transmit */
  2631. for (epno = 0; epno < hsotg->num_of_eps; epno++) {
  2632. ep = index_to_ep(hsotg, epno, 1);
  2633. if (!ep)
  2634. continue;
  2635. if (!ep->dir_in)
  2636. continue;
  2637. if ((periodic && !ep->periodic) ||
  2638. (!periodic && ep->periodic))
  2639. continue;
  2640. ret = dwc2_hsotg_trytx(hsotg, ep);
  2641. if (ret < 0)
  2642. break;
  2643. }
  2644. }
  2645. /* IRQ flags which will trigger a retry around the IRQ loop */
  2646. #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
  2647. GINTSTS_PTXFEMP | \
  2648. GINTSTS_RXFLVL)
  2649. /**
  2650. * dwc2_hsotg_core_init - issue softreset to the core
  2651. * @hsotg: The device state
  2652. *
  2653. * Issue a soft reset to the core, and await the core finishing it.
  2654. */
  2655. void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
  2656. bool is_usb_reset)
  2657. {
  2658. u32 intmsk;
  2659. u32 val;
  2660. u32 usbcfg;
  2661. u32 dcfg = 0;
  2662. /* Kill any ep0 requests as controller will be reinitialized */
  2663. kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
  2664. if (!is_usb_reset)
  2665. if (dwc2_core_reset(hsotg))
  2666. return;
  2667. /*
  2668. * we must now enable ep0 ready for host detection and then
  2669. * set configuration.
  2670. */
  2671. /* keep other bits untouched (so e.g. forced modes are not lost) */
  2672. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  2673. usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
  2674. GUSBCFG_HNPCAP);
  2675. if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
  2676. (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  2677. hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
  2678. /* FS/LS Dedicated Transceiver Interface */
  2679. usbcfg |= GUSBCFG_PHYSEL;
  2680. } else {
  2681. /* set the PLL on, remove the HNP/SRP and set the PHY */
  2682. val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
  2683. usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  2684. (val << GUSBCFG_USBTRDTIM_SHIFT);
  2685. }
  2686. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  2687. dwc2_hsotg_init_fifo(hsotg);
  2688. if (!is_usb_reset)
  2689. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2690. dcfg |= DCFG_EPMISCNT(1);
  2691. switch (hsotg->params.speed) {
  2692. case DWC2_SPEED_PARAM_LOW:
  2693. dcfg |= DCFG_DEVSPD_LS;
  2694. break;
  2695. case DWC2_SPEED_PARAM_FULL:
  2696. if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
  2697. dcfg |= DCFG_DEVSPD_FS48;
  2698. else
  2699. dcfg |= DCFG_DEVSPD_FS;
  2700. break;
  2701. default:
  2702. dcfg |= DCFG_DEVSPD_HS;
  2703. }
  2704. dwc2_writel(dcfg, hsotg->regs + DCFG);
  2705. /* Clear any pending OTG interrupts */
  2706. dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
  2707. /* Clear any pending interrupts */
  2708. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  2709. intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
  2710. GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
  2711. GINTSTS_USBRST | GINTSTS_RESETDET |
  2712. GINTSTS_ENUMDONE | GINTSTS_OTGINT |
  2713. GINTSTS_USBSUSP | GINTSTS_WKUPINT;
  2714. if (!using_desc_dma(hsotg))
  2715. intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
  2716. if (hsotg->params.external_id_pin_ctl <= 0)
  2717. intmsk |= GINTSTS_CONIDSTSCHNG;
  2718. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  2719. if (using_dma(hsotg)) {
  2720. dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
  2721. (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
  2722. hsotg->regs + GAHBCFG);
  2723. /* Set DDMA mode support in the core if needed */
  2724. if (using_desc_dma(hsotg))
  2725. __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
  2726. } else {
  2727. dwc2_writel(((hsotg->dedicated_fifos) ?
  2728. (GAHBCFG_NP_TXF_EMP_LVL |
  2729. GAHBCFG_P_TXF_EMP_LVL) : 0) |
  2730. GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
  2731. }
  2732. /*
  2733. * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
  2734. * when we have no data to transfer. Otherwise we get being flooded by
  2735. * interrupts.
  2736. */
  2737. dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
  2738. DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
  2739. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
  2740. DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
  2741. hsotg->regs + DIEPMSK);
  2742. /*
  2743. * don't need XferCompl, we get that from RXFIFO in slave mode. In
  2744. * DMA mode we may need this and StsPhseRcvd.
  2745. */
  2746. dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
  2747. DOEPMSK_STSPHSERCVDMSK) : 0) |
  2748. DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
  2749. DOEPMSK_SETUPMSK,
  2750. hsotg->regs + DOEPMSK);
  2751. /* Enable BNA interrupt for DDMA */
  2752. if (using_desc_dma(hsotg))
  2753. __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
  2754. dwc2_writel(0, hsotg->regs + DAINTMSK);
  2755. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2756. dwc2_readl(hsotg->regs + DIEPCTL0),
  2757. dwc2_readl(hsotg->regs + DOEPCTL0));
  2758. /* enable in and out endpoint interrupts */
  2759. dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
  2760. /*
  2761. * Enable the RXFIFO when in slave mode, as this is how we collect
  2762. * the data. In DMA mode, we get events from the FIFO but also
  2763. * things we cannot process, so do not use it.
  2764. */
  2765. if (!using_dma(hsotg))
  2766. dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
  2767. /* Enable interrupts for EP0 in and out */
  2768. dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
  2769. dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
  2770. if (!is_usb_reset) {
  2771. __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  2772. udelay(10); /* see openiboot */
  2773. __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
  2774. }
  2775. dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
  2776. /*
  2777. * DxEPCTL_USBActEp says RO in manual, but seems to be set by
  2778. * writing to the EPCTL register..
  2779. */
  2780. /* set to read 1 8byte packet */
  2781. dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
  2782. DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
  2783. dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  2784. DXEPCTL_CNAK | DXEPCTL_EPENA |
  2785. DXEPCTL_USBACTEP,
  2786. hsotg->regs + DOEPCTL0);
  2787. /* enable, but don't activate EP0in */
  2788. dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
  2789. DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
  2790. dwc2_hsotg_enqueue_setup(hsotg);
  2791. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  2792. dwc2_readl(hsotg->regs + DIEPCTL0),
  2793. dwc2_readl(hsotg->regs + DOEPCTL0));
  2794. /* clear global NAKs */
  2795. val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
  2796. if (!is_usb_reset)
  2797. val |= DCTL_SFTDISCON;
  2798. __orr32(hsotg->regs + DCTL, val);
  2799. /* must be at-least 3ms to allow bus to see disconnect */
  2800. mdelay(3);
  2801. hsotg->lx_state = DWC2_L0;
  2802. }
  2803. static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
  2804. {
  2805. /* set the soft-disconnect bit */
  2806. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2807. }
  2808. void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
  2809. {
  2810. /* remove the soft-disconnect and let's go */
  2811. __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  2812. }
  2813. /**
  2814. * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
  2815. * @hsotg: The device state:
  2816. *
  2817. * This interrupt indicates one of the following conditions occurred while
  2818. * transmitting an ISOC transaction.
  2819. * - Corrupted IN Token for ISOC EP.
  2820. * - Packet not complete in FIFO.
  2821. *
  2822. * The following actions will be taken:
  2823. * - Determine the EP
  2824. * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
  2825. */
  2826. static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
  2827. {
  2828. struct dwc2_hsotg_ep *hs_ep;
  2829. u32 epctrl;
  2830. u32 idx;
  2831. dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
  2832. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  2833. hs_ep = hsotg->eps_in[idx];
  2834. epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
  2835. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
  2836. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  2837. epctrl |= DXEPCTL_SNAK;
  2838. epctrl |= DXEPCTL_EPDIS;
  2839. dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
  2840. }
  2841. }
  2842. /* Clear interrupt */
  2843. dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
  2844. }
  2845. /**
  2846. * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
  2847. * @hsotg: The device state:
  2848. *
  2849. * This interrupt indicates one of the following conditions occurred while
  2850. * transmitting an ISOC transaction.
  2851. * - Corrupted OUT Token for ISOC EP.
  2852. * - Packet not complete in FIFO.
  2853. *
  2854. * The following actions will be taken:
  2855. * - Determine the EP
  2856. * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
  2857. */
  2858. static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
  2859. {
  2860. u32 gintsts;
  2861. u32 gintmsk;
  2862. u32 epctrl;
  2863. struct dwc2_hsotg_ep *hs_ep;
  2864. int idx;
  2865. dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
  2866. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  2867. hs_ep = hsotg->eps_out[idx];
  2868. epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
  2869. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
  2870. dwc2_gadget_target_frame_elapsed(hs_ep)) {
  2871. /* Unmask GOUTNAKEFF interrupt */
  2872. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2873. gintmsk |= GINTSTS_GOUTNAKEFF;
  2874. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  2875. gintsts = dwc2_readl(hsotg->regs + GINTSTS);
  2876. if (!(gintsts & GINTSTS_GOUTNAKEFF))
  2877. __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  2878. }
  2879. }
  2880. /* Clear interrupt */
  2881. dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
  2882. }
  2883. /**
  2884. * dwc2_hsotg_irq - handle device interrupt
  2885. * @irq: The IRQ number triggered
  2886. * @pw: The pw value when registered the handler.
  2887. */
  2888. static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
  2889. {
  2890. struct dwc2_hsotg *hsotg = pw;
  2891. int retry_count = 8;
  2892. u32 gintsts;
  2893. u32 gintmsk;
  2894. if (!dwc2_is_device_mode(hsotg))
  2895. return IRQ_NONE;
  2896. spin_lock(&hsotg->lock);
  2897. irq_retry:
  2898. gintsts = dwc2_readl(hsotg->regs + GINTSTS);
  2899. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2900. dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
  2901. __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
  2902. gintsts &= gintmsk;
  2903. if (gintsts & GINTSTS_RESETDET) {
  2904. dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
  2905. dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
  2906. /* This event must be used only if controller is suspended */
  2907. if (hsotg->lx_state == DWC2_L2) {
  2908. dwc2_exit_hibernation(hsotg, true);
  2909. hsotg->lx_state = DWC2_L0;
  2910. }
  2911. }
  2912. if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
  2913. u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
  2914. u32 connected = hsotg->connected;
  2915. dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
  2916. dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
  2917. dwc2_readl(hsotg->regs + GNPTXSTS));
  2918. dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
  2919. /* Report disconnection if it is not already done. */
  2920. dwc2_hsotg_disconnect(hsotg);
  2921. if (usb_status & GOTGCTL_BSESVLD && connected)
  2922. dwc2_hsotg_core_init_disconnected(hsotg, true);
  2923. }
  2924. if (gintsts & GINTSTS_ENUMDONE) {
  2925. dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
  2926. dwc2_hsotg_irq_enumdone(hsotg);
  2927. }
  2928. if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
  2929. u32 daint = dwc2_readl(hsotg->regs + DAINT);
  2930. u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  2931. u32 daint_out, daint_in;
  2932. int ep;
  2933. daint &= daintmsk;
  2934. daint_out = daint >> DAINT_OUTEP_SHIFT;
  2935. daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
  2936. dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
  2937. for (ep = 0; ep < hsotg->num_of_eps && daint_out;
  2938. ep++, daint_out >>= 1) {
  2939. if (daint_out & 1)
  2940. dwc2_hsotg_epint(hsotg, ep, 0);
  2941. }
  2942. for (ep = 0; ep < hsotg->num_of_eps && daint_in;
  2943. ep++, daint_in >>= 1) {
  2944. if (daint_in & 1)
  2945. dwc2_hsotg_epint(hsotg, ep, 1);
  2946. }
  2947. }
  2948. /* check both FIFOs */
  2949. if (gintsts & GINTSTS_NPTXFEMP) {
  2950. dev_dbg(hsotg->dev, "NPTxFEmp\n");
  2951. /*
  2952. * Disable the interrupt to stop it happening again
  2953. * unless one of these endpoint routines decides that
  2954. * it needs re-enabling
  2955. */
  2956. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
  2957. dwc2_hsotg_irq_fifoempty(hsotg, false);
  2958. }
  2959. if (gintsts & GINTSTS_PTXFEMP) {
  2960. dev_dbg(hsotg->dev, "PTxFEmp\n");
  2961. /* See note in GINTSTS_NPTxFEmp */
  2962. dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
  2963. dwc2_hsotg_irq_fifoempty(hsotg, true);
  2964. }
  2965. if (gintsts & GINTSTS_RXFLVL) {
  2966. /*
  2967. * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
  2968. * we need to retry dwc2_hsotg_handle_rx if this is still
  2969. * set.
  2970. */
  2971. dwc2_hsotg_handle_rx(hsotg);
  2972. }
  2973. if (gintsts & GINTSTS_ERLYSUSP) {
  2974. dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
  2975. dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
  2976. }
  2977. /*
  2978. * these next two seem to crop-up occasionally causing the core
  2979. * to shutdown the USB transfer, so try clearing them and logging
  2980. * the occurrence.
  2981. */
  2982. if (gintsts & GINTSTS_GOUTNAKEFF) {
  2983. u8 idx;
  2984. u32 epctrl;
  2985. u32 gintmsk;
  2986. struct dwc2_hsotg_ep *hs_ep;
  2987. /* Mask this interrupt */
  2988. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2989. gintmsk &= ~GINTSTS_GOUTNAKEFF;
  2990. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  2991. dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
  2992. for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
  2993. hs_ep = hsotg->eps_out[idx];
  2994. epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
  2995. if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
  2996. epctrl |= DXEPCTL_SNAK;
  2997. epctrl |= DXEPCTL_EPDIS;
  2998. dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
  2999. }
  3000. }
  3001. /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
  3002. }
  3003. if (gintsts & GINTSTS_GINNAKEFF) {
  3004. dev_info(hsotg->dev, "GINNakEff triggered\n");
  3005. __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
  3006. dwc2_hsotg_dump(hsotg);
  3007. }
  3008. if (gintsts & GINTSTS_INCOMPL_SOIN)
  3009. dwc2_gadget_handle_incomplete_isoc_in(hsotg);
  3010. if (gintsts & GINTSTS_INCOMPL_SOOUT)
  3011. dwc2_gadget_handle_incomplete_isoc_out(hsotg);
  3012. /*
  3013. * if we've had fifo events, we should try and go around the
  3014. * loop again to see if there's any point in returning yet.
  3015. */
  3016. if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
  3017. goto irq_retry;
  3018. spin_unlock(&hsotg->lock);
  3019. return IRQ_HANDLED;
  3020. }
  3021. static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
  3022. u32 bit, u32 timeout)
  3023. {
  3024. u32 i;
  3025. for (i = 0; i < timeout; i++) {
  3026. if (dwc2_readl(hs_otg->regs + reg) & bit)
  3027. return 0;
  3028. udelay(1);
  3029. }
  3030. return -ETIMEDOUT;
  3031. }
  3032. static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
  3033. struct dwc2_hsotg_ep *hs_ep)
  3034. {
  3035. u32 epctrl_reg;
  3036. u32 epint_reg;
  3037. epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
  3038. DOEPCTL(hs_ep->index);
  3039. epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
  3040. DOEPINT(hs_ep->index);
  3041. dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
  3042. hs_ep->name);
  3043. if (hs_ep->dir_in) {
  3044. if (hsotg->dedicated_fifos || hs_ep->periodic) {
  3045. __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
  3046. /* Wait for Nak effect */
  3047. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
  3048. DXEPINT_INEPNAKEFF, 100))
  3049. dev_warn(hsotg->dev,
  3050. "%s: timeout DIEPINT.NAKEFF\n",
  3051. __func__);
  3052. } else {
  3053. __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
  3054. /* Wait for Nak effect */
  3055. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3056. GINTSTS_GINNAKEFF, 100))
  3057. dev_warn(hsotg->dev,
  3058. "%s: timeout GINTSTS.GINNAKEFF\n",
  3059. __func__);
  3060. }
  3061. } else {
  3062. if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
  3063. __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
  3064. /* Wait for global nak to take effect */
  3065. if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
  3066. GINTSTS_GOUTNAKEFF, 100))
  3067. dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
  3068. __func__);
  3069. }
  3070. /* Disable ep */
  3071. __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
  3072. /* Wait for ep to be disabled */
  3073. if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
  3074. dev_warn(hsotg->dev,
  3075. "%s: timeout DOEPCTL.EPDisable\n", __func__);
  3076. /* Clear EPDISBLD interrupt */
  3077. __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
  3078. if (hs_ep->dir_in) {
  3079. unsigned short fifo_index;
  3080. if (hsotg->dedicated_fifos || hs_ep->periodic)
  3081. fifo_index = hs_ep->fifo_index;
  3082. else
  3083. fifo_index = 0;
  3084. /* Flush TX FIFO */
  3085. dwc2_flush_tx_fifo(hsotg, fifo_index);
  3086. /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
  3087. if (!hsotg->dedicated_fifos && !hs_ep->periodic)
  3088. __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
  3089. } else {
  3090. /* Remove global NAKs */
  3091. __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
  3092. }
  3093. }
  3094. /**
  3095. * dwc2_hsotg_ep_enable - enable the given endpoint
  3096. * @ep: The USB endpint to configure
  3097. * @desc: The USB endpoint descriptor to configure with.
  3098. *
  3099. * This is called from the USB gadget code's usb_ep_enable().
  3100. */
  3101. static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
  3102. const struct usb_endpoint_descriptor *desc)
  3103. {
  3104. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3105. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3106. unsigned long flags;
  3107. unsigned int index = hs_ep->index;
  3108. u32 epctrl_reg;
  3109. u32 epctrl;
  3110. u32 mps;
  3111. u32 mc;
  3112. u32 mask;
  3113. unsigned int dir_in;
  3114. unsigned int i, val, size;
  3115. int ret = 0;
  3116. dev_dbg(hsotg->dev,
  3117. "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
  3118. __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
  3119. desc->wMaxPacketSize, desc->bInterval);
  3120. /* not to be called for EP0 */
  3121. if (index == 0) {
  3122. dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
  3123. return -EINVAL;
  3124. }
  3125. dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
  3126. if (dir_in != hs_ep->dir_in) {
  3127. dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
  3128. return -EINVAL;
  3129. }
  3130. mps = usb_endpoint_maxp(desc);
  3131. mc = usb_endpoint_maxp_mult(desc);
  3132. /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
  3133. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  3134. epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  3135. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
  3136. __func__, epctrl, epctrl_reg);
  3137. /* Allocate DMA descriptor chain for non-ctrl endpoints */
  3138. if (using_desc_dma(hsotg)) {
  3139. hs_ep->desc_list = dma_alloc_coherent(hsotg->dev,
  3140. MAX_DMA_DESC_NUM_GENERIC *
  3141. sizeof(struct dwc2_dma_desc),
  3142. &hs_ep->desc_list_dma, GFP_KERNEL);
  3143. if (!hs_ep->desc_list) {
  3144. ret = -ENOMEM;
  3145. goto error2;
  3146. }
  3147. }
  3148. spin_lock_irqsave(&hsotg->lock, flags);
  3149. epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
  3150. epctrl |= DXEPCTL_MPS(mps);
  3151. /*
  3152. * mark the endpoint as active, otherwise the core may ignore
  3153. * transactions entirely for this endpoint
  3154. */
  3155. epctrl |= DXEPCTL_USBACTEP;
  3156. /* update the endpoint state */
  3157. dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
  3158. /* default, set to non-periodic */
  3159. hs_ep->isochronous = 0;
  3160. hs_ep->periodic = 0;
  3161. hs_ep->halted = 0;
  3162. hs_ep->interval = desc->bInterval;
  3163. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  3164. case USB_ENDPOINT_XFER_ISOC:
  3165. epctrl |= DXEPCTL_EPTYPE_ISO;
  3166. epctrl |= DXEPCTL_SETEVENFR;
  3167. hs_ep->isochronous = 1;
  3168. hs_ep->interval = 1 << (desc->bInterval - 1);
  3169. hs_ep->target_frame = TARGET_FRAME_INITIAL;
  3170. hs_ep->isoc_chain_num = 0;
  3171. hs_ep->next_desc = 0;
  3172. if (dir_in) {
  3173. hs_ep->periodic = 1;
  3174. mask = dwc2_readl(hsotg->regs + DIEPMSK);
  3175. mask |= DIEPMSK_NAKMSK;
  3176. dwc2_writel(mask, hsotg->regs + DIEPMSK);
  3177. } else {
  3178. mask = dwc2_readl(hsotg->regs + DOEPMSK);
  3179. mask |= DOEPMSK_OUTTKNEPDISMSK;
  3180. dwc2_writel(mask, hsotg->regs + DOEPMSK);
  3181. }
  3182. break;
  3183. case USB_ENDPOINT_XFER_BULK:
  3184. epctrl |= DXEPCTL_EPTYPE_BULK;
  3185. break;
  3186. case USB_ENDPOINT_XFER_INT:
  3187. if (dir_in)
  3188. hs_ep->periodic = 1;
  3189. if (hsotg->gadget.speed == USB_SPEED_HIGH)
  3190. hs_ep->interval = 1 << (desc->bInterval - 1);
  3191. epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
  3192. break;
  3193. case USB_ENDPOINT_XFER_CONTROL:
  3194. epctrl |= DXEPCTL_EPTYPE_CONTROL;
  3195. break;
  3196. }
  3197. /*
  3198. * if the hardware has dedicated fifos, we must give each IN EP
  3199. * a unique tx-fifo even if it is non-periodic.
  3200. */
  3201. if (dir_in && hsotg->dedicated_fifos) {
  3202. u32 fifo_index = 0;
  3203. u32 fifo_size = UINT_MAX;
  3204. size = hs_ep->ep.maxpacket*hs_ep->mc;
  3205. for (i = 1; i < hsotg->num_of_eps; ++i) {
  3206. if (hsotg->fifo_map & (1<<i))
  3207. continue;
  3208. val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
  3209. val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
  3210. if (val < size)
  3211. continue;
  3212. /* Search for smallest acceptable fifo */
  3213. if (val < fifo_size) {
  3214. fifo_size = val;
  3215. fifo_index = i;
  3216. }
  3217. }
  3218. if (!fifo_index) {
  3219. dev_err(hsotg->dev,
  3220. "%s: No suitable fifo found\n", __func__);
  3221. ret = -ENOMEM;
  3222. goto error1;
  3223. }
  3224. hsotg->fifo_map |= 1 << fifo_index;
  3225. epctrl |= DXEPCTL_TXFNUM(fifo_index);
  3226. hs_ep->fifo_index = fifo_index;
  3227. hs_ep->fifo_size = fifo_size;
  3228. }
  3229. /* for non control endpoints, set PID to D0 */
  3230. if (index && !hs_ep->isochronous)
  3231. epctrl |= DXEPCTL_SETD0PID;
  3232. dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
  3233. __func__, epctrl);
  3234. dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
  3235. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
  3236. __func__, dwc2_readl(hsotg->regs + epctrl_reg));
  3237. /* enable the endpoint interrupt */
  3238. dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
  3239. error1:
  3240. spin_unlock_irqrestore(&hsotg->lock, flags);
  3241. error2:
  3242. if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
  3243. dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
  3244. sizeof(struct dwc2_dma_desc),
  3245. hs_ep->desc_list, hs_ep->desc_list_dma);
  3246. hs_ep->desc_list = NULL;
  3247. }
  3248. return ret;
  3249. }
  3250. /**
  3251. * dwc2_hsotg_ep_disable - disable given endpoint
  3252. * @ep: The endpoint to disable.
  3253. */
  3254. static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
  3255. {
  3256. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3257. struct dwc2_hsotg *hsotg = hs_ep->parent;
  3258. int dir_in = hs_ep->dir_in;
  3259. int index = hs_ep->index;
  3260. unsigned long flags;
  3261. u32 epctrl_reg;
  3262. u32 ctrl;
  3263. dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
  3264. if (ep == &hsotg->eps_out[0]->ep) {
  3265. dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
  3266. return -EINVAL;
  3267. }
  3268. /* Remove DMA memory allocated for non-control Endpoints */
  3269. if (using_desc_dma(hsotg)) {
  3270. dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
  3271. sizeof(struct dwc2_dma_desc),
  3272. hs_ep->desc_list, hs_ep->desc_list_dma);
  3273. hs_ep->desc_list = NULL;
  3274. }
  3275. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  3276. spin_lock_irqsave(&hsotg->lock, flags);
  3277. ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
  3278. if (ctrl & DXEPCTL_EPENA)
  3279. dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
  3280. ctrl &= ~DXEPCTL_EPENA;
  3281. ctrl &= ~DXEPCTL_USBACTEP;
  3282. ctrl |= DXEPCTL_SNAK;
  3283. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  3284. dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
  3285. /* disable endpoint interrupts */
  3286. dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
  3287. /* terminate all requests with shutdown */
  3288. kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
  3289. hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
  3290. hs_ep->fifo_index = 0;
  3291. hs_ep->fifo_size = 0;
  3292. spin_unlock_irqrestore(&hsotg->lock, flags);
  3293. return 0;
  3294. }
  3295. /**
  3296. * on_list - check request is on the given endpoint
  3297. * @ep: The endpoint to check.
  3298. * @test: The request to test if it is on the endpoint.
  3299. */
  3300. static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
  3301. {
  3302. struct dwc2_hsotg_req *req, *treq;
  3303. list_for_each_entry_safe(req, treq, &ep->queue, queue) {
  3304. if (req == test)
  3305. return true;
  3306. }
  3307. return false;
  3308. }
  3309. /**
  3310. * dwc2_hsotg_ep_dequeue - dequeue given endpoint
  3311. * @ep: The endpoint to dequeue.
  3312. * @req: The request to be removed from a queue.
  3313. */
  3314. static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  3315. {
  3316. struct dwc2_hsotg_req *hs_req = our_req(req);
  3317. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3318. struct dwc2_hsotg *hs = hs_ep->parent;
  3319. unsigned long flags;
  3320. dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
  3321. spin_lock_irqsave(&hs->lock, flags);
  3322. if (!on_list(hs_ep, hs_req)) {
  3323. spin_unlock_irqrestore(&hs->lock, flags);
  3324. return -EINVAL;
  3325. }
  3326. /* Dequeue already started request */
  3327. if (req == &hs_ep->req->req)
  3328. dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
  3329. dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
  3330. spin_unlock_irqrestore(&hs->lock, flags);
  3331. return 0;
  3332. }
  3333. /**
  3334. * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
  3335. * @ep: The endpoint to set halt.
  3336. * @value: Set or unset the halt.
  3337. * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
  3338. * the endpoint is busy processing requests.
  3339. *
  3340. * We need to stall the endpoint immediately if request comes from set_feature
  3341. * protocol command handler.
  3342. */
  3343. static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
  3344. {
  3345. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3346. struct dwc2_hsotg *hs = hs_ep->parent;
  3347. int index = hs_ep->index;
  3348. u32 epreg;
  3349. u32 epctl;
  3350. u32 xfertype;
  3351. dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
  3352. if (index == 0) {
  3353. if (value)
  3354. dwc2_hsotg_stall_ep0(hs);
  3355. else
  3356. dev_warn(hs->dev,
  3357. "%s: can't clear halt on ep0\n", __func__);
  3358. return 0;
  3359. }
  3360. if (hs_ep->isochronous) {
  3361. dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
  3362. return -EINVAL;
  3363. }
  3364. if (!now && value && !list_empty(&hs_ep->queue)) {
  3365. dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
  3366. ep->name);
  3367. return -EAGAIN;
  3368. }
  3369. if (hs_ep->dir_in) {
  3370. epreg = DIEPCTL(index);
  3371. epctl = dwc2_readl(hs->regs + epreg);
  3372. if (value) {
  3373. epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
  3374. if (epctl & DXEPCTL_EPENA)
  3375. epctl |= DXEPCTL_EPDIS;
  3376. } else {
  3377. epctl &= ~DXEPCTL_STALL;
  3378. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  3379. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  3380. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  3381. epctl |= DXEPCTL_SETD0PID;
  3382. }
  3383. dwc2_writel(epctl, hs->regs + epreg);
  3384. } else {
  3385. epreg = DOEPCTL(index);
  3386. epctl = dwc2_readl(hs->regs + epreg);
  3387. if (value)
  3388. epctl |= DXEPCTL_STALL;
  3389. else {
  3390. epctl &= ~DXEPCTL_STALL;
  3391. xfertype = epctl & DXEPCTL_EPTYPE_MASK;
  3392. if (xfertype == DXEPCTL_EPTYPE_BULK ||
  3393. xfertype == DXEPCTL_EPTYPE_INTERRUPT)
  3394. epctl |= DXEPCTL_SETD0PID;
  3395. }
  3396. dwc2_writel(epctl, hs->regs + epreg);
  3397. }
  3398. hs_ep->halted = value;
  3399. return 0;
  3400. }
  3401. /**
  3402. * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
  3403. * @ep: The endpoint to set halt.
  3404. * @value: Set or unset the halt.
  3405. */
  3406. static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
  3407. {
  3408. struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
  3409. struct dwc2_hsotg *hs = hs_ep->parent;
  3410. unsigned long flags = 0;
  3411. int ret = 0;
  3412. spin_lock_irqsave(&hs->lock, flags);
  3413. ret = dwc2_hsotg_ep_sethalt(ep, value, false);
  3414. spin_unlock_irqrestore(&hs->lock, flags);
  3415. return ret;
  3416. }
  3417. static struct usb_ep_ops dwc2_hsotg_ep_ops = {
  3418. .enable = dwc2_hsotg_ep_enable,
  3419. .disable = dwc2_hsotg_ep_disable,
  3420. .alloc_request = dwc2_hsotg_ep_alloc_request,
  3421. .free_request = dwc2_hsotg_ep_free_request,
  3422. .queue = dwc2_hsotg_ep_queue_lock,
  3423. .dequeue = dwc2_hsotg_ep_dequeue,
  3424. .set_halt = dwc2_hsotg_ep_sethalt_lock,
  3425. /* note, don't believe we have any call for the fifo routines */
  3426. };
  3427. /**
  3428. * dwc2_hsotg_init - initalize the usb core
  3429. * @hsotg: The driver state
  3430. */
  3431. static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
  3432. {
  3433. u32 trdtim;
  3434. u32 usbcfg;
  3435. /* unmask subset of endpoint interrupts */
  3436. dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
  3437. DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
  3438. hsotg->regs + DIEPMSK);
  3439. dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
  3440. DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
  3441. hsotg->regs + DOEPMSK);
  3442. dwc2_writel(0, hsotg->regs + DAINTMSK);
  3443. /* Be in disconnected state until gadget is registered */
  3444. __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
  3445. /* setup fifos */
  3446. dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  3447. dwc2_readl(hsotg->regs + GRXFSIZ),
  3448. dwc2_readl(hsotg->regs + GNPTXFSIZ));
  3449. dwc2_hsotg_init_fifo(hsotg);
  3450. /* keep other bits untouched (so e.g. forced modes are not lost) */
  3451. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  3452. usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
  3453. GUSBCFG_HNPCAP);
  3454. /* set the PLL on, remove the HNP/SRP and set the PHY */
  3455. trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
  3456. usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
  3457. (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
  3458. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  3459. if (using_dma(hsotg))
  3460. __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
  3461. }
  3462. /**
  3463. * dwc2_hsotg_udc_start - prepare the udc for work
  3464. * @gadget: The usb gadget state
  3465. * @driver: The usb gadget driver
  3466. *
  3467. * Perform initialization to prepare udc device and driver
  3468. * to work.
  3469. */
  3470. static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
  3471. struct usb_gadget_driver *driver)
  3472. {
  3473. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3474. unsigned long flags;
  3475. int ret;
  3476. if (!hsotg) {
  3477. pr_err("%s: called with no device\n", __func__);
  3478. return -ENODEV;
  3479. }
  3480. if (!driver) {
  3481. dev_err(hsotg->dev, "%s: no driver\n", __func__);
  3482. return -EINVAL;
  3483. }
  3484. if (driver->max_speed < USB_SPEED_FULL)
  3485. dev_err(hsotg->dev, "%s: bad speed\n", __func__);
  3486. if (!driver->setup) {
  3487. dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
  3488. return -EINVAL;
  3489. }
  3490. WARN_ON(hsotg->driver);
  3491. driver->driver.bus = NULL;
  3492. hsotg->driver = driver;
  3493. hsotg->gadget.dev.of_node = hsotg->dev->of_node;
  3494. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3495. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
  3496. ret = dwc2_lowlevel_hw_enable(hsotg);
  3497. if (ret)
  3498. goto err;
  3499. }
  3500. if (!IS_ERR_OR_NULL(hsotg->uphy))
  3501. otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
  3502. spin_lock_irqsave(&hsotg->lock, flags);
  3503. if (dwc2_hw_is_device(hsotg)) {
  3504. dwc2_hsotg_init(hsotg);
  3505. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3506. }
  3507. hsotg->enabled = 0;
  3508. spin_unlock_irqrestore(&hsotg->lock, flags);
  3509. dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
  3510. return 0;
  3511. err:
  3512. hsotg->driver = NULL;
  3513. return ret;
  3514. }
  3515. /**
  3516. * dwc2_hsotg_udc_stop - stop the udc
  3517. * @gadget: The usb gadget state
  3518. * @driver: The usb gadget driver
  3519. *
  3520. * Stop udc hw block and stay tunned for future transmissions
  3521. */
  3522. static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
  3523. {
  3524. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3525. unsigned long flags = 0;
  3526. int ep;
  3527. if (!hsotg)
  3528. return -ENODEV;
  3529. /* all endpoints should be shutdown */
  3530. for (ep = 1; ep < hsotg->num_of_eps; ep++) {
  3531. if (hsotg->eps_in[ep])
  3532. dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
  3533. if (hsotg->eps_out[ep])
  3534. dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
  3535. }
  3536. spin_lock_irqsave(&hsotg->lock, flags);
  3537. hsotg->driver = NULL;
  3538. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3539. hsotg->enabled = 0;
  3540. spin_unlock_irqrestore(&hsotg->lock, flags);
  3541. if (!IS_ERR_OR_NULL(hsotg->uphy))
  3542. otg_set_peripheral(hsotg->uphy->otg, NULL);
  3543. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  3544. dwc2_lowlevel_hw_disable(hsotg);
  3545. return 0;
  3546. }
  3547. /**
  3548. * dwc2_hsotg_gadget_getframe - read the frame number
  3549. * @gadget: The usb gadget state
  3550. *
  3551. * Read the {micro} frame number
  3552. */
  3553. static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
  3554. {
  3555. return dwc2_hsotg_read_frameno(to_hsotg(gadget));
  3556. }
  3557. /**
  3558. * dwc2_hsotg_pullup - connect/disconnect the USB PHY
  3559. * @gadget: The usb gadget state
  3560. * @is_on: Current state of the USB PHY
  3561. *
  3562. * Connect/Disconnect the USB PHY pullup
  3563. */
  3564. static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
  3565. {
  3566. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3567. unsigned long flags = 0;
  3568. dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
  3569. hsotg->op_state);
  3570. /* Don't modify pullup state while in host mode */
  3571. if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
  3572. hsotg->enabled = is_on;
  3573. return 0;
  3574. }
  3575. spin_lock_irqsave(&hsotg->lock, flags);
  3576. if (is_on) {
  3577. hsotg->enabled = 1;
  3578. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3579. dwc2_hsotg_core_connect(hsotg);
  3580. } else {
  3581. dwc2_hsotg_core_disconnect(hsotg);
  3582. dwc2_hsotg_disconnect(hsotg);
  3583. hsotg->enabled = 0;
  3584. }
  3585. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3586. spin_unlock_irqrestore(&hsotg->lock, flags);
  3587. return 0;
  3588. }
  3589. static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
  3590. {
  3591. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3592. unsigned long flags;
  3593. dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
  3594. spin_lock_irqsave(&hsotg->lock, flags);
  3595. /*
  3596. * If controller is hibernated, it must exit from hibernation
  3597. * before being initialized / de-initialized
  3598. */
  3599. if (hsotg->lx_state == DWC2_L2)
  3600. dwc2_exit_hibernation(hsotg, false);
  3601. if (is_active) {
  3602. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  3603. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3604. if (hsotg->enabled)
  3605. dwc2_hsotg_core_connect(hsotg);
  3606. } else {
  3607. dwc2_hsotg_core_disconnect(hsotg);
  3608. dwc2_hsotg_disconnect(hsotg);
  3609. }
  3610. spin_unlock_irqrestore(&hsotg->lock, flags);
  3611. return 0;
  3612. }
  3613. /**
  3614. * dwc2_hsotg_vbus_draw - report bMaxPower field
  3615. * @gadget: The usb gadget state
  3616. * @mA: Amount of current
  3617. *
  3618. * Report how much power the device may consume to the phy.
  3619. */
  3620. static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  3621. {
  3622. struct dwc2_hsotg *hsotg = to_hsotg(gadget);
  3623. if (IS_ERR_OR_NULL(hsotg->uphy))
  3624. return -ENOTSUPP;
  3625. return usb_phy_set_power(hsotg->uphy, mA);
  3626. }
  3627. static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
  3628. .get_frame = dwc2_hsotg_gadget_getframe,
  3629. .udc_start = dwc2_hsotg_udc_start,
  3630. .udc_stop = dwc2_hsotg_udc_stop,
  3631. .pullup = dwc2_hsotg_pullup,
  3632. .vbus_session = dwc2_hsotg_vbus_session,
  3633. .vbus_draw = dwc2_hsotg_vbus_draw,
  3634. };
  3635. /**
  3636. * dwc2_hsotg_initep - initialise a single endpoint
  3637. * @hsotg: The device state.
  3638. * @hs_ep: The endpoint to be initialised.
  3639. * @epnum: The endpoint number
  3640. *
  3641. * Initialise the given endpoint (as part of the probe and device state
  3642. * creation) to give to the gadget driver. Setup the endpoint name, any
  3643. * direction information and other state that may be required.
  3644. */
  3645. static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
  3646. struct dwc2_hsotg_ep *hs_ep,
  3647. int epnum,
  3648. bool dir_in)
  3649. {
  3650. char *dir;
  3651. if (epnum == 0)
  3652. dir = "";
  3653. else if (dir_in)
  3654. dir = "in";
  3655. else
  3656. dir = "out";
  3657. hs_ep->dir_in = dir_in;
  3658. hs_ep->index = epnum;
  3659. snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
  3660. INIT_LIST_HEAD(&hs_ep->queue);
  3661. INIT_LIST_HEAD(&hs_ep->ep.ep_list);
  3662. /* add to the list of endpoints known by the gadget driver */
  3663. if (epnum)
  3664. list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
  3665. hs_ep->parent = hsotg;
  3666. hs_ep->ep.name = hs_ep->name;
  3667. if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
  3668. usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
  3669. else
  3670. usb_ep_set_maxpacket_limit(&hs_ep->ep,
  3671. epnum ? 1024 : EP0_MPS_LIMIT);
  3672. hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
  3673. if (epnum == 0) {
  3674. hs_ep->ep.caps.type_control = true;
  3675. } else {
  3676. if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
  3677. hs_ep->ep.caps.type_iso = true;
  3678. hs_ep->ep.caps.type_bulk = true;
  3679. }
  3680. hs_ep->ep.caps.type_int = true;
  3681. }
  3682. if (dir_in)
  3683. hs_ep->ep.caps.dir_in = true;
  3684. else
  3685. hs_ep->ep.caps.dir_out = true;
  3686. /*
  3687. * if we're using dma, we need to set the next-endpoint pointer
  3688. * to be something valid.
  3689. */
  3690. if (using_dma(hsotg)) {
  3691. u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
  3692. if (dir_in)
  3693. dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
  3694. else
  3695. dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
  3696. }
  3697. }
  3698. /**
  3699. * dwc2_hsotg_hw_cfg - read HW configuration registers
  3700. * @param: The device state
  3701. *
  3702. * Read the USB core HW configuration registers
  3703. */
  3704. static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
  3705. {
  3706. u32 cfg;
  3707. u32 ep_type;
  3708. u32 i;
  3709. /* check hardware configuration */
  3710. hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
  3711. /* Add ep0 */
  3712. hsotg->num_of_eps++;
  3713. hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
  3714. GFP_KERNEL);
  3715. if (!hsotg->eps_in[0])
  3716. return -ENOMEM;
  3717. /* Same dwc2_hsotg_ep is used in both directions for ep0 */
  3718. hsotg->eps_out[0] = hsotg->eps_in[0];
  3719. cfg = hsotg->hw_params.dev_ep_dirs;
  3720. for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
  3721. ep_type = cfg & 3;
  3722. /* Direction in or both */
  3723. if (!(ep_type & 2)) {
  3724. hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
  3725. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  3726. if (!hsotg->eps_in[i])
  3727. return -ENOMEM;
  3728. }
  3729. /* Direction out or both */
  3730. if (!(ep_type & 1)) {
  3731. hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
  3732. sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
  3733. if (!hsotg->eps_out[i])
  3734. return -ENOMEM;
  3735. }
  3736. }
  3737. hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
  3738. hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
  3739. dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
  3740. hsotg->num_of_eps,
  3741. hsotg->dedicated_fifos ? "dedicated" : "shared",
  3742. hsotg->fifo_mem);
  3743. return 0;
  3744. }
  3745. /**
  3746. * dwc2_hsotg_dump - dump state of the udc
  3747. * @param: The device state
  3748. */
  3749. static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
  3750. {
  3751. #ifdef DEBUG
  3752. struct device *dev = hsotg->dev;
  3753. void __iomem *regs = hsotg->regs;
  3754. u32 val;
  3755. int idx;
  3756. dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
  3757. dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
  3758. dwc2_readl(regs + DIEPMSK));
  3759. dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
  3760. dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
  3761. dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  3762. dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
  3763. /* show periodic fifo settings */
  3764. for (idx = 1; idx < hsotg->num_of_eps; idx++) {
  3765. val = dwc2_readl(regs + DPTXFSIZN(idx));
  3766. dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
  3767. val >> FIFOSIZE_DEPTH_SHIFT,
  3768. val & FIFOSIZE_STARTADDR_MASK);
  3769. }
  3770. for (idx = 0; idx < hsotg->num_of_eps; idx++) {
  3771. dev_info(dev,
  3772. "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
  3773. dwc2_readl(regs + DIEPCTL(idx)),
  3774. dwc2_readl(regs + DIEPTSIZ(idx)),
  3775. dwc2_readl(regs + DIEPDMA(idx)));
  3776. val = dwc2_readl(regs + DOEPCTL(idx));
  3777. dev_info(dev,
  3778. "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
  3779. idx, dwc2_readl(regs + DOEPCTL(idx)),
  3780. dwc2_readl(regs + DOEPTSIZ(idx)),
  3781. dwc2_readl(regs + DOEPDMA(idx)));
  3782. }
  3783. dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
  3784. dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
  3785. #endif
  3786. }
  3787. /**
  3788. * dwc2_gadget_init - init function for gadget
  3789. * @dwc2: The data structure for the DWC2 driver.
  3790. * @irq: The IRQ number for the controller.
  3791. */
  3792. int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
  3793. {
  3794. struct device *dev = hsotg->dev;
  3795. int epnum;
  3796. int ret;
  3797. /* Dump fifo information */
  3798. dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
  3799. hsotg->params.g_np_tx_fifo_size);
  3800. dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
  3801. hsotg->gadget.max_speed = USB_SPEED_HIGH;
  3802. hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
  3803. hsotg->gadget.name = dev_name(dev);
  3804. if (hsotg->dr_mode == USB_DR_MODE_OTG)
  3805. hsotg->gadget.is_otg = 1;
  3806. else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  3807. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  3808. ret = dwc2_hsotg_hw_cfg(hsotg);
  3809. if (ret) {
  3810. dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
  3811. return ret;
  3812. }
  3813. hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
  3814. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  3815. if (!hsotg->ctrl_buff)
  3816. return -ENOMEM;
  3817. hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
  3818. DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
  3819. if (!hsotg->ep0_buff)
  3820. return -ENOMEM;
  3821. if (using_desc_dma(hsotg)) {
  3822. ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
  3823. if (ret < 0)
  3824. return ret;
  3825. }
  3826. ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
  3827. dev_name(hsotg->dev), hsotg);
  3828. if (ret < 0) {
  3829. dev_err(dev, "cannot claim IRQ for gadget\n");
  3830. return ret;
  3831. }
  3832. /* hsotg->num_of_eps holds number of EPs other than ep0 */
  3833. if (hsotg->num_of_eps == 0) {
  3834. dev_err(dev, "wrong number of EPs (zero)\n");
  3835. return -EINVAL;
  3836. }
  3837. /* setup endpoint information */
  3838. INIT_LIST_HEAD(&hsotg->gadget.ep_list);
  3839. hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
  3840. /* allocate EP0 request */
  3841. hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
  3842. GFP_KERNEL);
  3843. if (!hsotg->ctrl_req) {
  3844. dev_err(dev, "failed to allocate ctrl req\n");
  3845. return -ENOMEM;
  3846. }
  3847. /* initialise the endpoints now the core has been initialised */
  3848. for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
  3849. if (hsotg->eps_in[epnum])
  3850. dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
  3851. epnum, 1);
  3852. if (hsotg->eps_out[epnum])
  3853. dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
  3854. epnum, 0);
  3855. }
  3856. ret = usb_add_gadget_udc(dev, &hsotg->gadget);
  3857. if (ret)
  3858. return ret;
  3859. dwc2_hsotg_dump(hsotg);
  3860. return 0;
  3861. }
  3862. /**
  3863. * dwc2_hsotg_remove - remove function for hsotg driver
  3864. * @pdev: The platform information for the driver
  3865. */
  3866. int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
  3867. {
  3868. usb_del_gadget_udc(&hsotg->gadget);
  3869. return 0;
  3870. }
  3871. int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
  3872. {
  3873. unsigned long flags;
  3874. if (hsotg->lx_state != DWC2_L0)
  3875. return 0;
  3876. if (hsotg->driver) {
  3877. int ep;
  3878. dev_info(hsotg->dev, "suspending usb gadget %s\n",
  3879. hsotg->driver->driver.name);
  3880. spin_lock_irqsave(&hsotg->lock, flags);
  3881. if (hsotg->enabled)
  3882. dwc2_hsotg_core_disconnect(hsotg);
  3883. dwc2_hsotg_disconnect(hsotg);
  3884. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  3885. spin_unlock_irqrestore(&hsotg->lock, flags);
  3886. for (ep = 0; ep < hsotg->num_of_eps; ep++) {
  3887. if (hsotg->eps_in[ep])
  3888. dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
  3889. if (hsotg->eps_out[ep])
  3890. dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
  3891. }
  3892. }
  3893. return 0;
  3894. }
  3895. int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
  3896. {
  3897. unsigned long flags;
  3898. if (hsotg->lx_state == DWC2_L2)
  3899. return 0;
  3900. if (hsotg->driver) {
  3901. dev_info(hsotg->dev, "resuming usb gadget %s\n",
  3902. hsotg->driver->driver.name);
  3903. spin_lock_irqsave(&hsotg->lock, flags);
  3904. dwc2_hsotg_core_init_disconnected(hsotg, false);
  3905. if (hsotg->enabled)
  3906. dwc2_hsotg_core_connect(hsotg);
  3907. spin_unlock_irqrestore(&hsotg->lock, flags);
  3908. }
  3909. return 0;
  3910. }
  3911. /**
  3912. * dwc2_backup_device_registers() - Backup controller device registers.
  3913. * When suspending usb bus, registers needs to be backuped
  3914. * if controller power is disabled once suspended.
  3915. *
  3916. * @hsotg: Programming view of the DWC_otg controller
  3917. */
  3918. int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
  3919. {
  3920. struct dwc2_dregs_backup *dr;
  3921. int i;
  3922. dev_dbg(hsotg->dev, "%s\n", __func__);
  3923. /* Backup dev regs */
  3924. dr = &hsotg->dr_backup;
  3925. dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
  3926. dr->dctl = dwc2_readl(hsotg->regs + DCTL);
  3927. dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
  3928. dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
  3929. dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
  3930. for (i = 0; i < hsotg->num_of_eps; i++) {
  3931. /* Backup IN EPs */
  3932. dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
  3933. /* Ensure DATA PID is correctly configured */
  3934. if (dr->diepctl[i] & DXEPCTL_DPID)
  3935. dr->diepctl[i] |= DXEPCTL_SETD1PID;
  3936. else
  3937. dr->diepctl[i] |= DXEPCTL_SETD0PID;
  3938. dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
  3939. dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
  3940. /* Backup OUT EPs */
  3941. dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
  3942. /* Ensure DATA PID is correctly configured */
  3943. if (dr->doepctl[i] & DXEPCTL_DPID)
  3944. dr->doepctl[i] |= DXEPCTL_SETD1PID;
  3945. else
  3946. dr->doepctl[i] |= DXEPCTL_SETD0PID;
  3947. dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
  3948. dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
  3949. }
  3950. dr->valid = true;
  3951. return 0;
  3952. }
  3953. /**
  3954. * dwc2_restore_device_registers() - Restore controller device registers.
  3955. * When resuming usb bus, device registers needs to be restored
  3956. * if controller power were disabled.
  3957. *
  3958. * @hsotg: Programming view of the DWC_otg controller
  3959. */
  3960. int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
  3961. {
  3962. struct dwc2_dregs_backup *dr;
  3963. u32 dctl;
  3964. int i;
  3965. dev_dbg(hsotg->dev, "%s\n", __func__);
  3966. /* Restore dev regs */
  3967. dr = &hsotg->dr_backup;
  3968. if (!dr->valid) {
  3969. dev_err(hsotg->dev, "%s: no device registers to restore\n",
  3970. __func__);
  3971. return -EINVAL;
  3972. }
  3973. dr->valid = false;
  3974. dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
  3975. dwc2_writel(dr->dctl, hsotg->regs + DCTL);
  3976. dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
  3977. dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
  3978. dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
  3979. for (i = 0; i < hsotg->num_of_eps; i++) {
  3980. /* Restore IN EPs */
  3981. dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
  3982. dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
  3983. dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
  3984. /* Restore OUT EPs */
  3985. dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
  3986. dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
  3987. dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
  3988. }
  3989. /* Set the Power-On Programming done bit */
  3990. dctl = dwc2_readl(hsotg->regs + DCTL);
  3991. dctl |= DCTL_PWRONPRGDONE;
  3992. dwc2_writel(dctl, hsotg->regs + DCTL);
  3993. return 0;
  3994. }