gadget.c 134 KB

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