gadget.c 127 KB

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