gadget.c.orig 148 KB

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