hcd.c 164 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * hcd.c - DesignWare HS OTG Controller host-mode routines
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. */
  7. /*
  8. * This file contains the core HCD code, and implements the Linux hc_driver
  9. * API
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/hcd.h>
  22. #include <linux/usb/ch11.h>
  23. #include <linux/usb/of.h>
  24. #include "core.h"
  25. #include "hcd.h"
  26. /*
  27. * =========================================================================
  28. * Host Core Layer Functions
  29. * =========================================================================
  30. */
  31. /**
  32. * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
  33. * used in both device and host modes
  34. *
  35. * @hsotg: Programming view of the DWC_otg controller
  36. */
  37. static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
  38. {
  39. u32 intmsk;
  40. /* Clear any pending OTG Interrupts */
  41. dwc2_writel(hsotg, 0xffffffff, GOTGINT);
  42. /* Clear any pending interrupts */
  43. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  44. /* Enable the interrupts in the GINTMSK */
  45. intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
  46. if (!hsotg->params.host_dma)
  47. intmsk |= GINTSTS_RXFLVL;
  48. if (!hsotg->params.external_id_pin_ctl)
  49. intmsk |= GINTSTS_CONIDSTSCHNG;
  50. intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
  51. GINTSTS_SESSREQINT;
  52. if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
  53. intmsk |= GINTSTS_LPMTRANRCVD;
  54. dwc2_writel(hsotg, intmsk, GINTMSK);
  55. }
  56. static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
  57. {
  58. u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  59. switch (hsotg->hw_params.arch) {
  60. case GHWCFG2_EXT_DMA_ARCH:
  61. dev_err(hsotg->dev, "External DMA Mode not supported\n");
  62. return -EINVAL;
  63. case GHWCFG2_INT_DMA_ARCH:
  64. dev_dbg(hsotg->dev, "Internal DMA Mode\n");
  65. if (hsotg->params.ahbcfg != -1) {
  66. ahbcfg &= GAHBCFG_CTRL_MASK;
  67. ahbcfg |= hsotg->params.ahbcfg &
  68. ~GAHBCFG_CTRL_MASK;
  69. }
  70. break;
  71. case GHWCFG2_SLAVE_ONLY_ARCH:
  72. default:
  73. dev_dbg(hsotg->dev, "Slave Only Mode\n");
  74. break;
  75. }
  76. if (hsotg->params.host_dma)
  77. ahbcfg |= GAHBCFG_DMA_EN;
  78. else
  79. hsotg->params.dma_desc_enable = false;
  80. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  81. return 0;
  82. }
  83. static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
  84. {
  85. u32 usbcfg;
  86. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  87. usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
  88. switch (hsotg->hw_params.op_mode) {
  89. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  90. if (hsotg->params.otg_caps.hnp_support &&
  91. hsotg->params.otg_caps.srp_support)
  92. usbcfg |= GUSBCFG_HNPCAP;
  93. fallthrough;
  94. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  95. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  96. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  97. if (hsotg->params.otg_caps.srp_support)
  98. usbcfg |= GUSBCFG_SRPCAP;
  99. break;
  100. case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
  101. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
  102. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
  103. default:
  104. break;
  105. }
  106. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  107. }
  108. static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
  109. {
  110. if (hsotg->vbus_supply)
  111. return regulator_enable(hsotg->vbus_supply);
  112. return 0;
  113. }
  114. static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
  115. {
  116. if (hsotg->vbus_supply)
  117. return regulator_disable(hsotg->vbus_supply);
  118. return 0;
  119. }
  120. /**
  121. * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
  122. *
  123. * @hsotg: Programming view of DWC_otg controller
  124. */
  125. static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
  126. {
  127. u32 intmsk;
  128. dev_dbg(hsotg->dev, "%s()\n", __func__);
  129. /* Disable all interrupts */
  130. dwc2_writel(hsotg, 0, GINTMSK);
  131. dwc2_writel(hsotg, 0, HAINTMSK);
  132. /* Enable the common interrupts */
  133. dwc2_enable_common_interrupts(hsotg);
  134. /* Enable host mode interrupts without disturbing common interrupts */
  135. intmsk = dwc2_readl(hsotg, GINTMSK);
  136. intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
  137. dwc2_writel(hsotg, intmsk, GINTMSK);
  138. }
  139. /**
  140. * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
  141. *
  142. * @hsotg: Programming view of DWC_otg controller
  143. */
  144. static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
  145. {
  146. u32 intmsk = dwc2_readl(hsotg, GINTMSK);
  147. /* Disable host mode interrupts without disturbing common interrupts */
  148. intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
  149. GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
  150. dwc2_writel(hsotg, intmsk, GINTMSK);
  151. }
  152. /*
  153. * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
  154. * For system that have a total fifo depth that is smaller than the default
  155. * RX + TX fifo size.
  156. *
  157. * @hsotg: Programming view of DWC_otg controller
  158. */
  159. static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
  160. {
  161. struct dwc2_core_params *params = &hsotg->params;
  162. struct dwc2_hw_params *hw = &hsotg->hw_params;
  163. u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
  164. total_fifo_size = hw->total_fifo_size;
  165. rxfsiz = params->host_rx_fifo_size;
  166. nptxfsiz = params->host_nperio_tx_fifo_size;
  167. ptxfsiz = params->host_perio_tx_fifo_size;
  168. /*
  169. * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
  170. * allocation with support for high bandwidth endpoints. Synopsys
  171. * defines MPS(Max Packet size) for a periodic EP=1024, and for
  172. * non-periodic as 512.
  173. */
  174. if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
  175. /*
  176. * For Buffer DMA mode/Scatter Gather DMA mode
  177. * 2 * ((Largest Packet size / 4) + 1 + 1) + n
  178. * with n = number of host channel.
  179. * 2 * ((1024/4) + 2) = 516
  180. */
  181. rxfsiz = 516 + hw->host_channels;
  182. /*
  183. * min non-periodic tx fifo depth
  184. * 2 * (largest non-periodic USB packet used / 4)
  185. * 2 * (512/4) = 256
  186. */
  187. nptxfsiz = 256;
  188. /*
  189. * min periodic tx fifo depth
  190. * (largest packet size*MC)/4
  191. * (1024 * 3)/4 = 768
  192. */
  193. ptxfsiz = 768;
  194. params->host_rx_fifo_size = rxfsiz;
  195. params->host_nperio_tx_fifo_size = nptxfsiz;
  196. params->host_perio_tx_fifo_size = ptxfsiz;
  197. }
  198. /*
  199. * If the summation of RX, NPTX and PTX fifo sizes is still
  200. * bigger than the total_fifo_size, then we have a problem.
  201. *
  202. * We won't be able to allocate as many endpoints. Right now,
  203. * we're just printing an error message, but ideally this FIFO
  204. * allocation algorithm would be improved in the future.
  205. *
  206. * FIXME improve this FIFO allocation algorithm.
  207. */
  208. if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
  209. dev_err(hsotg->dev, "invalid fifo sizes\n");
  210. }
  211. static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
  212. {
  213. struct dwc2_core_params *params = &hsotg->params;
  214. u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
  215. if (!params->enable_dynamic_fifo)
  216. return;
  217. dwc2_calculate_dynamic_fifo(hsotg);
  218. /* Rx FIFO */
  219. grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
  220. dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
  221. grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
  222. grxfsiz |= params->host_rx_fifo_size <<
  223. GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
  224. dwc2_writel(hsotg, grxfsiz, GRXFSIZ);
  225. dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
  226. dwc2_readl(hsotg, GRXFSIZ));
  227. /* Non-periodic Tx FIFO */
  228. dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
  229. dwc2_readl(hsotg, GNPTXFSIZ));
  230. nptxfsiz = params->host_nperio_tx_fifo_size <<
  231. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  232. nptxfsiz |= params->host_rx_fifo_size <<
  233. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  234. dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ);
  235. dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
  236. dwc2_readl(hsotg, GNPTXFSIZ));
  237. /* Periodic Tx FIFO */
  238. dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
  239. dwc2_readl(hsotg, HPTXFSIZ));
  240. hptxfsiz = params->host_perio_tx_fifo_size <<
  241. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  242. hptxfsiz |= (params->host_rx_fifo_size +
  243. params->host_nperio_tx_fifo_size) <<
  244. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  245. dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ);
  246. dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
  247. dwc2_readl(hsotg, HPTXFSIZ));
  248. if (hsotg->params.en_multiple_tx_fifo &&
  249. hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
  250. /*
  251. * This feature was implemented in 2.91a version
  252. * Global DFIFOCFG calculation for Host mode -
  253. * include RxFIFO, NPTXFIFO and HPTXFIFO
  254. */
  255. dfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
  256. dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
  257. dfifocfg |= (params->host_rx_fifo_size +
  258. params->host_nperio_tx_fifo_size +
  259. params->host_perio_tx_fifo_size) <<
  260. GDFIFOCFG_EPINFOBASE_SHIFT &
  261. GDFIFOCFG_EPINFOBASE_MASK;
  262. dwc2_writel(hsotg, dfifocfg, GDFIFOCFG);
  263. }
  264. }
  265. /**
  266. * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
  267. * the HFIR register according to PHY type and speed
  268. *
  269. * @hsotg: Programming view of DWC_otg controller
  270. *
  271. * NOTE: The caller can modify the value of the HFIR register only after the
  272. * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
  273. * has been set
  274. */
  275. u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
  276. {
  277. u32 usbcfg;
  278. u32 hprt0;
  279. int clock = 60; /* default value */
  280. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  281. hprt0 = dwc2_readl(hsotg, HPRT0);
  282. if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
  283. !(usbcfg & GUSBCFG_PHYIF16))
  284. clock = 60;
  285. if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
  286. GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
  287. clock = 48;
  288. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  289. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  290. clock = 30;
  291. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  292. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
  293. clock = 60;
  294. if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  295. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  296. clock = 48;
  297. if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
  298. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
  299. clock = 48;
  300. if ((usbcfg & GUSBCFG_PHYSEL) &&
  301. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  302. clock = 48;
  303. if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
  304. /* High speed case */
  305. return 125 * clock - 1;
  306. /* FS/LS case */
  307. return 1000 * clock - 1;
  308. }
  309. /**
  310. * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
  311. * buffer
  312. *
  313. * @hsotg: Programming view of DWC_otg controller
  314. * @dest: Destination buffer for the packet
  315. * @bytes: Number of bytes to copy to the destination
  316. */
  317. void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
  318. {
  319. u32 *data_buf = (u32 *)dest;
  320. int word_count = (bytes + 3) / 4;
  321. int i;
  322. /*
  323. * Todo: Account for the case where dest is not dword aligned. This
  324. * requires reading data from the FIFO into a u32 temp buffer, then
  325. * moving it into the data buffer.
  326. */
  327. dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
  328. for (i = 0; i < word_count; i++, data_buf++)
  329. *data_buf = dwc2_readl(hsotg, HCFIFO(0));
  330. }
  331. /**
  332. * dwc2_dump_channel_info() - Prints the state of a host channel
  333. *
  334. * @hsotg: Programming view of DWC_otg controller
  335. * @chan: Pointer to the channel to dump
  336. *
  337. * Must be called with interrupt disabled and spinlock held
  338. *
  339. * NOTE: This function will be removed once the peripheral controller code
  340. * is integrated and the driver is stable
  341. */
  342. static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
  343. struct dwc2_host_chan *chan)
  344. {
  345. #ifdef VERBOSE_DEBUG
  346. int num_channels = hsotg->params.host_channels;
  347. struct dwc2_qh *qh;
  348. u32 hcchar;
  349. u32 hcsplt;
  350. u32 hctsiz;
  351. u32 hc_dma;
  352. int i;
  353. if (!chan)
  354. return;
  355. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  356. hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
  357. hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num));
  358. hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num));
  359. dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
  360. dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
  361. hcchar, hcsplt);
  362. dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
  363. hctsiz, hc_dma);
  364. dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  365. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  366. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  367. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  368. dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
  369. dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
  370. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  371. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  372. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  373. (unsigned long)chan->xfer_dma);
  374. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  375. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  376. dev_dbg(hsotg->dev, " NP inactive sched:\n");
  377. list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
  378. qh_list_entry)
  379. dev_dbg(hsotg->dev, " %p\n", qh);
  380. dev_dbg(hsotg->dev, " NP waiting sched:\n");
  381. list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
  382. qh_list_entry)
  383. dev_dbg(hsotg->dev, " %p\n", qh);
  384. dev_dbg(hsotg->dev, " NP active sched:\n");
  385. list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
  386. qh_list_entry)
  387. dev_dbg(hsotg->dev, " %p\n", qh);
  388. dev_dbg(hsotg->dev, " Channels:\n");
  389. for (i = 0; i < num_channels; i++) {
  390. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  391. dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
  392. }
  393. #endif /* VERBOSE_DEBUG */
  394. }
  395. static int _dwc2_hcd_start(struct usb_hcd *hcd);
  396. static void dwc2_host_start(struct dwc2_hsotg *hsotg)
  397. {
  398. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  399. hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
  400. _dwc2_hcd_start(hcd);
  401. }
  402. static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
  403. {
  404. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  405. hcd->self.is_b_host = 0;
  406. }
  407. static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
  408. int *hub_addr, int *hub_port)
  409. {
  410. struct urb *urb = context;
  411. if (urb->dev->tt)
  412. *hub_addr = urb->dev->tt->hub->devnum;
  413. else
  414. *hub_addr = 0;
  415. *hub_port = urb->dev->ttport;
  416. }
  417. /*
  418. * =========================================================================
  419. * Low Level Host Channel Access Functions
  420. * =========================================================================
  421. */
  422. static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
  423. struct dwc2_host_chan *chan)
  424. {
  425. u32 hcintmsk = HCINTMSK_CHHLTD;
  426. switch (chan->ep_type) {
  427. case USB_ENDPOINT_XFER_CONTROL:
  428. case USB_ENDPOINT_XFER_BULK:
  429. dev_vdbg(hsotg->dev, "control/bulk\n");
  430. hcintmsk |= HCINTMSK_XFERCOMPL;
  431. hcintmsk |= HCINTMSK_STALL;
  432. hcintmsk |= HCINTMSK_XACTERR;
  433. hcintmsk |= HCINTMSK_DATATGLERR;
  434. if (chan->ep_is_in) {
  435. hcintmsk |= HCINTMSK_BBLERR;
  436. } else {
  437. hcintmsk |= HCINTMSK_NAK;
  438. hcintmsk |= HCINTMSK_NYET;
  439. if (chan->do_ping)
  440. hcintmsk |= HCINTMSK_ACK;
  441. }
  442. if (chan->do_split) {
  443. hcintmsk |= HCINTMSK_NAK;
  444. if (chan->complete_split)
  445. hcintmsk |= HCINTMSK_NYET;
  446. else
  447. hcintmsk |= HCINTMSK_ACK;
  448. }
  449. if (chan->error_state)
  450. hcintmsk |= HCINTMSK_ACK;
  451. break;
  452. case USB_ENDPOINT_XFER_INT:
  453. if (dbg_perio())
  454. dev_vdbg(hsotg->dev, "intr\n");
  455. hcintmsk |= HCINTMSK_XFERCOMPL;
  456. hcintmsk |= HCINTMSK_NAK;
  457. hcintmsk |= HCINTMSK_STALL;
  458. hcintmsk |= HCINTMSK_XACTERR;
  459. hcintmsk |= HCINTMSK_DATATGLERR;
  460. hcintmsk |= HCINTMSK_FRMOVRUN;
  461. if (chan->ep_is_in)
  462. hcintmsk |= HCINTMSK_BBLERR;
  463. if (chan->error_state)
  464. hcintmsk |= HCINTMSK_ACK;
  465. if (chan->do_split) {
  466. if (chan->complete_split)
  467. hcintmsk |= HCINTMSK_NYET;
  468. else
  469. hcintmsk |= HCINTMSK_ACK;
  470. }
  471. break;
  472. case USB_ENDPOINT_XFER_ISOC:
  473. if (dbg_perio())
  474. dev_vdbg(hsotg->dev, "isoc\n");
  475. hcintmsk |= HCINTMSK_XFERCOMPL;
  476. hcintmsk |= HCINTMSK_FRMOVRUN;
  477. hcintmsk |= HCINTMSK_ACK;
  478. if (chan->ep_is_in) {
  479. hcintmsk |= HCINTMSK_XACTERR;
  480. hcintmsk |= HCINTMSK_BBLERR;
  481. }
  482. break;
  483. default:
  484. dev_err(hsotg->dev, "## Unknown EP type ##\n");
  485. break;
  486. }
  487. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  488. if (dbg_hc(chan))
  489. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  490. }
  491. static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
  492. struct dwc2_host_chan *chan)
  493. {
  494. u32 hcintmsk = HCINTMSK_CHHLTD;
  495. /*
  496. * For Descriptor DMA mode core halts the channel on AHB error.
  497. * Interrupt is not required.
  498. */
  499. if (!hsotg->params.dma_desc_enable) {
  500. if (dbg_hc(chan))
  501. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  502. hcintmsk |= HCINTMSK_AHBERR;
  503. } else {
  504. if (dbg_hc(chan))
  505. dev_vdbg(hsotg->dev, "desc DMA enabled\n");
  506. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  507. hcintmsk |= HCINTMSK_XFERCOMPL;
  508. }
  509. if (chan->error_state && !chan->do_split &&
  510. chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
  511. if (dbg_hc(chan))
  512. dev_vdbg(hsotg->dev, "setting ACK\n");
  513. hcintmsk |= HCINTMSK_ACK;
  514. if (chan->ep_is_in) {
  515. hcintmsk |= HCINTMSK_DATATGLERR;
  516. if (chan->ep_type != USB_ENDPOINT_XFER_INT)
  517. hcintmsk |= HCINTMSK_NAK;
  518. }
  519. }
  520. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  521. if (dbg_hc(chan))
  522. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  523. }
  524. static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
  525. struct dwc2_host_chan *chan)
  526. {
  527. u32 intmsk;
  528. if (hsotg->params.host_dma) {
  529. if (dbg_hc(chan))
  530. dev_vdbg(hsotg->dev, "DMA enabled\n");
  531. dwc2_hc_enable_dma_ints(hsotg, chan);
  532. } else {
  533. if (dbg_hc(chan))
  534. dev_vdbg(hsotg->dev, "DMA disabled\n");
  535. dwc2_hc_enable_slave_ints(hsotg, chan);
  536. }
  537. /* Enable the top level host channel interrupt */
  538. intmsk = dwc2_readl(hsotg, HAINTMSK);
  539. intmsk |= 1 << chan->hc_num;
  540. dwc2_writel(hsotg, intmsk, HAINTMSK);
  541. if (dbg_hc(chan))
  542. dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
  543. /* Make sure host channel interrupts are enabled */
  544. intmsk = dwc2_readl(hsotg, GINTMSK);
  545. intmsk |= GINTSTS_HCHINT;
  546. dwc2_writel(hsotg, intmsk, GINTMSK);
  547. if (dbg_hc(chan))
  548. dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
  549. }
  550. /**
  551. * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
  552. * a specific endpoint
  553. *
  554. * @hsotg: Programming view of DWC_otg controller
  555. * @chan: Information needed to initialize the host channel
  556. *
  557. * The HCCHARn register is set up with the characteristics specified in chan.
  558. * Host channel interrupts that may need to be serviced while this transfer is
  559. * in progress are enabled.
  560. */
  561. static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  562. {
  563. u8 hc_num = chan->hc_num;
  564. u32 hcintmsk;
  565. u32 hcchar;
  566. u32 hcsplt = 0;
  567. if (dbg_hc(chan))
  568. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  569. /* Clear old interrupt conditions for this host channel */
  570. hcintmsk = 0xffffffff;
  571. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  572. dwc2_writel(hsotg, hcintmsk, HCINT(hc_num));
  573. /* Enable channel interrupts required for this transfer */
  574. dwc2_hc_enable_ints(hsotg, chan);
  575. /*
  576. * Program the HCCHARn register with the endpoint characteristics for
  577. * the current transfer
  578. */
  579. hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
  580. hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
  581. if (chan->ep_is_in)
  582. hcchar |= HCCHAR_EPDIR;
  583. if (chan->speed == USB_SPEED_LOW)
  584. hcchar |= HCCHAR_LSPDDEV;
  585. hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
  586. hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
  587. dwc2_writel(hsotg, hcchar, HCCHAR(hc_num));
  588. if (dbg_hc(chan)) {
  589. dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
  590. hc_num, hcchar);
  591. dev_vdbg(hsotg->dev, "%s: Channel %d\n",
  592. __func__, hc_num);
  593. dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
  594. chan->dev_addr);
  595. dev_vdbg(hsotg->dev, " Ep Num: %d\n",
  596. chan->ep_num);
  597. dev_vdbg(hsotg->dev, " Is In: %d\n",
  598. chan->ep_is_in);
  599. dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
  600. chan->speed == USB_SPEED_LOW);
  601. dev_vdbg(hsotg->dev, " Ep Type: %d\n",
  602. chan->ep_type);
  603. dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
  604. chan->max_packet);
  605. }
  606. /* Program the HCSPLT register for SPLITs */
  607. if (chan->do_split) {
  608. if (dbg_hc(chan))
  609. dev_vdbg(hsotg->dev,
  610. "Programming HC %d with split --> %s\n",
  611. hc_num,
  612. chan->complete_split ? "CSPLIT" : "SSPLIT");
  613. if (chan->complete_split)
  614. hcsplt |= HCSPLT_COMPSPLT;
  615. hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
  616. HCSPLT_XACTPOS_MASK;
  617. hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
  618. HCSPLT_HUBADDR_MASK;
  619. hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
  620. HCSPLT_PRTADDR_MASK;
  621. if (dbg_hc(chan)) {
  622. dev_vdbg(hsotg->dev, " comp split %d\n",
  623. chan->complete_split);
  624. dev_vdbg(hsotg->dev, " xact pos %d\n",
  625. chan->xact_pos);
  626. dev_vdbg(hsotg->dev, " hub addr %d\n",
  627. chan->hub_addr);
  628. dev_vdbg(hsotg->dev, " hub port %d\n",
  629. chan->hub_port);
  630. dev_vdbg(hsotg->dev, " is_in %d\n",
  631. chan->ep_is_in);
  632. dev_vdbg(hsotg->dev, " Max Pkt %d\n",
  633. chan->max_packet);
  634. dev_vdbg(hsotg->dev, " xferlen %d\n",
  635. chan->xfer_len);
  636. }
  637. }
  638. dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num));
  639. }
  640. /**
  641. * dwc2_hc_halt() - Attempts to halt a host channel
  642. *
  643. * @hsotg: Controller register interface
  644. * @chan: Host channel to halt
  645. * @halt_status: Reason for halting the channel
  646. *
  647. * This function should only be called in Slave mode or to abort a transfer in
  648. * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
  649. * controller halts the channel when the transfer is complete or a condition
  650. * occurs that requires application intervention.
  651. *
  652. * In slave mode, checks for a free request queue entry, then sets the Channel
  653. * Enable and Channel Disable bits of the Host Channel Characteristics
  654. * register of the specified channel to intiate the halt. If there is no free
  655. * request queue entry, sets only the Channel Disable bit of the HCCHARn
  656. * register to flush requests for this channel. In the latter case, sets a
  657. * flag to indicate that the host channel needs to be halted when a request
  658. * queue slot is open.
  659. *
  660. * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  661. * HCCHARn register. The controller ensures there is space in the request
  662. * queue before submitting the halt request.
  663. *
  664. * Some time may elapse before the core flushes any posted requests for this
  665. * host channel and halts. The Channel Halted interrupt handler completes the
  666. * deactivation of the host channel.
  667. */
  668. void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
  669. enum dwc2_halt_status halt_status)
  670. {
  671. u32 nptxsts, hptxsts, hcchar;
  672. if (dbg_hc(chan))
  673. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  674. /*
  675. * In buffer DMA or external DMA mode channel can't be halted
  676. * for non-split periodic channels. At the end of the next
  677. * uframe/frame (in the worst case), the core generates a channel
  678. * halted and disables the channel automatically.
  679. */
  680. if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
  681. hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
  682. if (!chan->do_split &&
  683. (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
  684. chan->ep_type == USB_ENDPOINT_XFER_INT)) {
  685. dev_err(hsotg->dev, "%s() Channel can't be halted\n",
  686. __func__);
  687. return;
  688. }
  689. }
  690. if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
  691. dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
  692. if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
  693. halt_status == DWC2_HC_XFER_AHB_ERR) {
  694. /*
  695. * Disable all channel interrupts except Ch Halted. The QTD
  696. * and QH state associated with this transfer has been cleared
  697. * (in the case of URB_DEQUEUE), so the channel needs to be
  698. * shut down carefully to prevent crashes.
  699. */
  700. u32 hcintmsk = HCINTMSK_CHHLTD;
  701. dev_vdbg(hsotg->dev, "dequeue/error\n");
  702. dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
  703. /*
  704. * Make sure no other interrupts besides halt are currently
  705. * pending. Handling another interrupt could cause a crash due
  706. * to the QTD and QH state.
  707. */
  708. dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num));
  709. /*
  710. * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  711. * even if the channel was already halted for some other
  712. * reason
  713. */
  714. chan->halt_status = halt_status;
  715. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  716. if (!(hcchar & HCCHAR_CHENA)) {
  717. /*
  718. * The channel is either already halted or it hasn't
  719. * started yet. In DMA mode, the transfer may halt if
  720. * it finishes normally or a condition occurs that
  721. * requires driver intervention. Don't want to halt
  722. * the channel again. In either Slave or DMA mode,
  723. * it's possible that the transfer has been assigned
  724. * to a channel, but not started yet when an URB is
  725. * dequeued. Don't want to halt a channel that hasn't
  726. * started yet.
  727. */
  728. return;
  729. }
  730. }
  731. if (chan->halt_pending) {
  732. /*
  733. * A halt has already been issued for this channel. This might
  734. * happen when a transfer is aborted by a higher level in
  735. * the stack.
  736. */
  737. dev_vdbg(hsotg->dev,
  738. "*** %s: Channel %d, chan->halt_pending already set ***\n",
  739. __func__, chan->hc_num);
  740. return;
  741. }
  742. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  743. /* No need to set the bit in DDMA for disabling the channel */
  744. /* TODO check it everywhere channel is disabled */
  745. if (!hsotg->params.dma_desc_enable) {
  746. if (dbg_hc(chan))
  747. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  748. hcchar |= HCCHAR_CHENA;
  749. } else {
  750. if (dbg_hc(chan))
  751. dev_dbg(hsotg->dev, "desc DMA enabled\n");
  752. }
  753. hcchar |= HCCHAR_CHDIS;
  754. if (!hsotg->params.host_dma) {
  755. if (dbg_hc(chan))
  756. dev_vdbg(hsotg->dev, "DMA not enabled\n");
  757. hcchar |= HCCHAR_CHENA;
  758. /* Check for space in the request queue to issue the halt */
  759. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  760. chan->ep_type == USB_ENDPOINT_XFER_BULK) {
  761. dev_vdbg(hsotg->dev, "control/bulk\n");
  762. nptxsts = dwc2_readl(hsotg, GNPTXSTS);
  763. if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
  764. dev_vdbg(hsotg->dev, "Disabling channel\n");
  765. hcchar &= ~HCCHAR_CHENA;
  766. }
  767. } else {
  768. if (dbg_perio())
  769. dev_vdbg(hsotg->dev, "isoc/intr\n");
  770. hptxsts = dwc2_readl(hsotg, HPTXSTS);
  771. if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
  772. hsotg->queuing_high_bandwidth) {
  773. if (dbg_perio())
  774. dev_vdbg(hsotg->dev, "Disabling channel\n");
  775. hcchar &= ~HCCHAR_CHENA;
  776. }
  777. }
  778. } else {
  779. if (dbg_hc(chan))
  780. dev_vdbg(hsotg->dev, "DMA enabled\n");
  781. }
  782. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  783. chan->halt_status = halt_status;
  784. if (hcchar & HCCHAR_CHENA) {
  785. if (dbg_hc(chan))
  786. dev_vdbg(hsotg->dev, "Channel enabled\n");
  787. chan->halt_pending = 1;
  788. chan->halt_on_queue = 0;
  789. } else {
  790. if (dbg_hc(chan))
  791. dev_vdbg(hsotg->dev, "Channel disabled\n");
  792. chan->halt_on_queue = 1;
  793. }
  794. if (dbg_hc(chan)) {
  795. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  796. chan->hc_num);
  797. dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
  798. hcchar);
  799. dev_vdbg(hsotg->dev, " halt_pending: %d\n",
  800. chan->halt_pending);
  801. dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
  802. chan->halt_on_queue);
  803. dev_vdbg(hsotg->dev, " halt_status: %d\n",
  804. chan->halt_status);
  805. }
  806. }
  807. /**
  808. * dwc2_hc_cleanup() - Clears the transfer state for a host channel
  809. *
  810. * @hsotg: Programming view of DWC_otg controller
  811. * @chan: Identifies the host channel to clean up
  812. *
  813. * This function is normally called after a transfer is done and the host
  814. * channel is being released
  815. */
  816. void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  817. {
  818. u32 hcintmsk;
  819. chan->xfer_started = 0;
  820. list_del_init(&chan->split_order_list_entry);
  821. /*
  822. * Clear channel interrupt enables and any unhandled channel interrupt
  823. * conditions
  824. */
  825. dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num));
  826. hcintmsk = 0xffffffff;
  827. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  828. dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num));
  829. }
  830. /**
  831. * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
  832. * which frame a periodic transfer should occur
  833. *
  834. * @hsotg: Programming view of DWC_otg controller
  835. * @chan: Identifies the host channel to set up and its properties
  836. * @hcchar: Current value of the HCCHAR register for the specified host channel
  837. *
  838. * This function has no effect on non-periodic transfers
  839. */
  840. static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
  841. struct dwc2_host_chan *chan, u32 *hcchar)
  842. {
  843. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  844. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  845. int host_speed;
  846. int xfer_ns;
  847. int xfer_us;
  848. int bytes_in_fifo;
  849. u16 fifo_space;
  850. u16 frame_number;
  851. u16 wire_frame;
  852. /*
  853. * Try to figure out if we're an even or odd frame. If we set
  854. * even and the current frame number is even the transfer
  855. * will happen immediately. Similar if both are odd. If one is
  856. * even and the other is odd then the transfer will happen when
  857. * the frame number ticks.
  858. *
  859. * There's a bit of a balancing act to get this right.
  860. * Sometimes we may want to send data in the current frame (AK
  861. * right away). We might want to do this if the frame number
  862. * _just_ ticked, but we might also want to do this in order
  863. * to continue a split transaction that happened late in a
  864. * microframe (so we didn't know to queue the next transfer
  865. * until the frame number had ticked). The problem is that we
  866. * need a lot of knowledge to know if there's actually still
  867. * time to send things or if it would be better to wait until
  868. * the next frame.
  869. *
  870. * We can look at how much time is left in the current frame
  871. * and make a guess about whether we'll have time to transfer.
  872. * We'll do that.
  873. */
  874. /* Get speed host is running at */
  875. host_speed = (chan->speed != USB_SPEED_HIGH &&
  876. !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
  877. /* See how many bytes are in the periodic FIFO right now */
  878. fifo_space = (dwc2_readl(hsotg, HPTXSTS) &
  879. TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
  880. bytes_in_fifo = sizeof(u32) *
  881. (hsotg->params.host_perio_tx_fifo_size -
  882. fifo_space);
  883. /*
  884. * Roughly estimate bus time for everything in the periodic
  885. * queue + our new transfer. This is "rough" because we're
  886. * using a function that makes takes into account IN/OUT
  887. * and INT/ISO and we're just slamming in one value for all
  888. * transfers. This should be an over-estimate and that should
  889. * be OK, but we can probably tighten it.
  890. */
  891. xfer_ns = usb_calc_bus_time(host_speed, false, false,
  892. chan->xfer_len + bytes_in_fifo);
  893. xfer_us = NS_TO_US(xfer_ns);
  894. /* See what frame number we'll be at by the time we finish */
  895. frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
  896. /* This is when we were scheduled to be on the wire */
  897. wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
  898. /*
  899. * If we'd finish _after_ the frame we're scheduled in then
  900. * it's hopeless. Just schedule right away and hope for the
  901. * best. Note that it _might_ be wise to call back into the
  902. * scheduler to pick a better frame, but this is better than
  903. * nothing.
  904. */
  905. if (dwc2_frame_num_gt(frame_number, wire_frame)) {
  906. dwc2_sch_vdbg(hsotg,
  907. "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
  908. chan->qh, wire_frame, frame_number,
  909. dwc2_frame_num_dec(frame_number,
  910. wire_frame));
  911. wire_frame = frame_number;
  912. /*
  913. * We picked a different frame number; communicate this
  914. * back to the scheduler so it doesn't try to schedule
  915. * another in the same frame.
  916. *
  917. * Remember that next_active_frame is 1 before the wire
  918. * frame.
  919. */
  920. chan->qh->next_active_frame =
  921. dwc2_frame_num_dec(frame_number, 1);
  922. }
  923. if (wire_frame & 1)
  924. *hcchar |= HCCHAR_ODDFRM;
  925. else
  926. *hcchar &= ~HCCHAR_ODDFRM;
  927. }
  928. }
  929. static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
  930. {
  931. /* Set up the initial PID for the transfer */
  932. if (chan->speed == USB_SPEED_HIGH) {
  933. if (chan->ep_is_in) {
  934. if (chan->multi_count == 1)
  935. chan->data_pid_start = DWC2_HC_PID_DATA0;
  936. else if (chan->multi_count == 2)
  937. chan->data_pid_start = DWC2_HC_PID_DATA1;
  938. else
  939. chan->data_pid_start = DWC2_HC_PID_DATA2;
  940. } else {
  941. if (chan->multi_count == 1)
  942. chan->data_pid_start = DWC2_HC_PID_DATA0;
  943. else
  944. chan->data_pid_start = DWC2_HC_PID_MDATA;
  945. }
  946. } else {
  947. chan->data_pid_start = DWC2_HC_PID_DATA0;
  948. }
  949. }
  950. /**
  951. * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
  952. * the Host Channel
  953. *
  954. * @hsotg: Programming view of DWC_otg controller
  955. * @chan: Information needed to initialize the host channel
  956. *
  957. * This function should only be called in Slave mode. For a channel associated
  958. * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
  959. * associated with a periodic EP, the periodic Tx FIFO is written.
  960. *
  961. * Upon return the xfer_buf and xfer_count fields in chan are incremented by
  962. * the number of bytes written to the Tx FIFO.
  963. */
  964. static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
  965. struct dwc2_host_chan *chan)
  966. {
  967. u32 i;
  968. u32 remaining_count;
  969. u32 byte_count;
  970. u32 dword_count;
  971. u32 *data_buf = (u32 *)chan->xfer_buf;
  972. if (dbg_hc(chan))
  973. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  974. remaining_count = chan->xfer_len - chan->xfer_count;
  975. if (remaining_count > chan->max_packet)
  976. byte_count = chan->max_packet;
  977. else
  978. byte_count = remaining_count;
  979. dword_count = (byte_count + 3) / 4;
  980. if (((unsigned long)data_buf & 0x3) == 0) {
  981. /* xfer_buf is DWORD aligned */
  982. for (i = 0; i < dword_count; i++, data_buf++)
  983. dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
  984. } else {
  985. /* xfer_buf is not DWORD aligned */
  986. for (i = 0; i < dword_count; i++, data_buf++) {
  987. u32 data = data_buf[0] | data_buf[1] << 8 |
  988. data_buf[2] << 16 | data_buf[3] << 24;
  989. dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
  990. }
  991. }
  992. chan->xfer_count += byte_count;
  993. chan->xfer_buf += byte_count;
  994. }
  995. /**
  996. * dwc2_hc_do_ping() - Starts a PING transfer
  997. *
  998. * @hsotg: Programming view of DWC_otg controller
  999. * @chan: Information needed to initialize the host channel
  1000. *
  1001. * This function should only be called in Slave mode. The Do Ping bit is set in
  1002. * the HCTSIZ register, then the channel is enabled.
  1003. */
  1004. static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
  1005. struct dwc2_host_chan *chan)
  1006. {
  1007. u32 hcchar;
  1008. u32 hctsiz;
  1009. if (dbg_hc(chan))
  1010. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1011. chan->hc_num);
  1012. hctsiz = TSIZ_DOPNG;
  1013. hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
  1014. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1015. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1016. hcchar |= HCCHAR_CHENA;
  1017. hcchar &= ~HCCHAR_CHDIS;
  1018. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1019. }
  1020. /**
  1021. * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
  1022. * channel and starts the transfer
  1023. *
  1024. * @hsotg: Programming view of DWC_otg controller
  1025. * @chan: Information needed to initialize the host channel. The xfer_len value
  1026. * may be reduced to accommodate the max widths of the XferSize and
  1027. * PktCnt fields in the HCTSIZn register. The multi_count value may be
  1028. * changed to reflect the final xfer_len value.
  1029. *
  1030. * This function may be called in either Slave mode or DMA mode. In Slave mode,
  1031. * the caller must ensure that there is sufficient space in the request queue
  1032. * and Tx Data FIFO.
  1033. *
  1034. * For an OUT transfer in Slave mode, it loads a data packet into the
  1035. * appropriate FIFO. If necessary, additional data packets are loaded in the
  1036. * Host ISR.
  1037. *
  1038. * For an IN transfer in Slave mode, a data packet is requested. The data
  1039. * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  1040. * additional data packets are requested in the Host ISR.
  1041. *
  1042. * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  1043. * register along with a packet count of 1 and the channel is enabled. This
  1044. * causes a single PING transaction to occur. Other fields in HCTSIZ are
  1045. * simply set to 0 since no data transfer occurs in this case.
  1046. *
  1047. * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  1048. * all the information required to perform the subsequent data transfer. In
  1049. * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  1050. * controller performs the entire PING protocol, then starts the data
  1051. * transfer.
  1052. */
  1053. static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
  1054. struct dwc2_host_chan *chan)
  1055. {
  1056. u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
  1057. u16 max_hc_pkt_count = hsotg->params.max_packet_count;
  1058. u32 hcchar;
  1059. u32 hctsiz = 0;
  1060. u16 num_packets;
  1061. u32 ec_mc;
  1062. if (dbg_hc(chan))
  1063. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1064. if (chan->do_ping) {
  1065. if (!hsotg->params.host_dma) {
  1066. if (dbg_hc(chan))
  1067. dev_vdbg(hsotg->dev, "ping, no DMA\n");
  1068. dwc2_hc_do_ping(hsotg, chan);
  1069. chan->xfer_started = 1;
  1070. return;
  1071. }
  1072. if (dbg_hc(chan))
  1073. dev_vdbg(hsotg->dev, "ping, DMA\n");
  1074. hctsiz |= TSIZ_DOPNG;
  1075. }
  1076. #if 1 //add by helen.
  1077. /*
  1078. * For high speed device, set the max xfer size 1024 when write.
  1079. * it's a dwc2 hardware bug in some version.
  1080. */
  1081. if ((chan->speed == USB_SPEED_HIGH) && !chan->ep_is_in) {
  1082. max_hc_pkt_count = 1;
  1083. max_hc_xfer_size = (max_hc_pkt_count + 1) * chan->max_packet - 1;
  1084. }
  1085. #endif
  1086. if (chan->do_split) {
  1087. if (dbg_hc(chan))
  1088. dev_vdbg(hsotg->dev, "split\n");
  1089. num_packets = 1;
  1090. if (chan->complete_split && !chan->ep_is_in)
  1091. /*
  1092. * For CSPLIT OUT Transfer, set the size to 0 so the
  1093. * core doesn't expect any data written to the FIFO
  1094. */
  1095. chan->xfer_len = 0;
  1096. else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
  1097. chan->xfer_len = chan->max_packet;
  1098. else if (!chan->ep_is_in && chan->xfer_len > 188)
  1099. chan->xfer_len = 188;
  1100. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1101. TSIZ_XFERSIZE_MASK;
  1102. /* For split set ec_mc for immediate retries */
  1103. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1104. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1105. ec_mc = 3;
  1106. else
  1107. ec_mc = 1;
  1108. } else {
  1109. if (dbg_hc(chan))
  1110. dev_vdbg(hsotg->dev, "no split\n");
  1111. /*
  1112. * Ensure that the transfer length and packet count will fit
  1113. * in the widths allocated for them in the HCTSIZn register
  1114. */
  1115. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1116. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1117. /*
  1118. * Make sure the transfer size is no larger than one
  1119. * (micro)frame's worth of data. (A check was done
  1120. * when the periodic transfer was accepted to ensure
  1121. * that a (micro)frame's worth of data can be
  1122. * programmed into a channel.)
  1123. */
  1124. u32 max_periodic_len =
  1125. chan->multi_count * chan->max_packet;
  1126. if (chan->xfer_len > max_periodic_len)
  1127. chan->xfer_len = max_periodic_len;
  1128. } else if (chan->xfer_len > max_hc_xfer_size) {
  1129. /*
  1130. * Make sure that xfer_len is a multiple of max packet
  1131. * size
  1132. */
  1133. chan->xfer_len =
  1134. max_hc_xfer_size - chan->max_packet + 1;
  1135. }
  1136. if (chan->xfer_len > 0) {
  1137. num_packets = (chan->xfer_len + chan->max_packet - 1) /
  1138. chan->max_packet;
  1139. if (num_packets > max_hc_pkt_count) {
  1140. num_packets = max_hc_pkt_count;
  1141. chan->xfer_len = num_packets * chan->max_packet;
  1142. } else if (chan->ep_is_in) {
  1143. /*
  1144. * Always program an integral # of max packets
  1145. * for IN transfers.
  1146. * Note: This assumes that the input buffer is
  1147. * aligned and sized accordingly.
  1148. */
  1149. chan->xfer_len = num_packets * chan->max_packet;
  1150. }
  1151. } else {
  1152. /* Need 1 packet for transfer length of 0 */
  1153. num_packets = 1;
  1154. }
  1155. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1156. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1157. /*
  1158. * Make sure that the multi_count field matches the
  1159. * actual transfer length
  1160. */
  1161. chan->multi_count = num_packets;
  1162. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1163. dwc2_set_pid_isoc(chan);
  1164. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1165. TSIZ_XFERSIZE_MASK;
  1166. /* The ec_mc gets the multi_count for non-split */
  1167. ec_mc = chan->multi_count;
  1168. }
  1169. chan->start_pkt_count = num_packets;
  1170. hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
  1171. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1172. TSIZ_SC_MC_PID_MASK;
  1173. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1174. if (dbg_hc(chan)) {
  1175. dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
  1176. hctsiz, chan->hc_num);
  1177. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1178. chan->hc_num);
  1179. dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
  1180. (hctsiz & TSIZ_XFERSIZE_MASK) >>
  1181. TSIZ_XFERSIZE_SHIFT);
  1182. dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
  1183. (hctsiz & TSIZ_PKTCNT_MASK) >>
  1184. TSIZ_PKTCNT_SHIFT);
  1185. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1186. (hctsiz & TSIZ_SC_MC_PID_MASK) >>
  1187. TSIZ_SC_MC_PID_SHIFT);
  1188. }
  1189. if (hsotg->params.host_dma) {
  1190. dma_addr_t dma_addr;
  1191. if (chan->align_buf) {
  1192. if (dbg_hc(chan))
  1193. dev_vdbg(hsotg->dev, "align_buf\n");
  1194. dma_addr = chan->align_buf;
  1195. } else {
  1196. dma_addr = chan->xfer_dma;
  1197. }
  1198. dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
  1199. if (dbg_hc(chan))
  1200. dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
  1201. (unsigned long)dma_addr, chan->hc_num);
  1202. }
  1203. /* Start the split */
  1204. if (chan->do_split) {
  1205. u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
  1206. hcsplt |= HCSPLT_SPLTENA;
  1207. dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
  1208. }
  1209. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1210. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1211. hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
  1212. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1213. if (hcchar & HCCHAR_CHDIS)
  1214. dev_warn(hsotg->dev,
  1215. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1216. __func__, chan->hc_num, hcchar);
  1217. /* Set host channel enable after all other setup is complete */
  1218. hcchar |= HCCHAR_CHENA;
  1219. hcchar &= ~HCCHAR_CHDIS;
  1220. if (dbg_hc(chan))
  1221. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1222. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1223. HCCHAR_MULTICNT_SHIFT);
  1224. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1225. if (dbg_hc(chan))
  1226. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1227. chan->hc_num);
  1228. chan->xfer_started = 1;
  1229. chan->requests++;
  1230. if (!hsotg->params.host_dma &&
  1231. !chan->ep_is_in && chan->xfer_len > 0)
  1232. /* Load OUT packet into the appropriate Tx FIFO */
  1233. dwc2_hc_write_packet(hsotg, chan);
  1234. }
  1235. /**
  1236. * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
  1237. * host channel and starts the transfer in Descriptor DMA mode
  1238. *
  1239. * @hsotg: Programming view of DWC_otg controller
  1240. * @chan: Information needed to initialize the host channel
  1241. *
  1242. * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  1243. * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
  1244. * with micro-frame bitmap.
  1245. *
  1246. * Initializes HCDMA register with descriptor list address and CTD value then
  1247. * starts the transfer via enabling the channel.
  1248. */
  1249. void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
  1250. struct dwc2_host_chan *chan)
  1251. {
  1252. u32 hcchar;
  1253. u32 hctsiz = 0;
  1254. if (chan->do_ping)
  1255. hctsiz |= TSIZ_DOPNG;
  1256. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1257. dwc2_set_pid_isoc(chan);
  1258. /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  1259. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1260. TSIZ_SC_MC_PID_MASK;
  1261. /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
  1262. hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
  1263. /* Non-zero only for high-speed interrupt endpoints */
  1264. hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
  1265. if (dbg_hc(chan)) {
  1266. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1267. chan->hc_num);
  1268. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1269. chan->data_pid_start);
  1270. dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
  1271. }
  1272. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1273. dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
  1274. chan->desc_list_sz, DMA_TO_DEVICE);
  1275. dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
  1276. if (dbg_hc(chan))
  1277. dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
  1278. &chan->desc_list_addr, chan->hc_num);
  1279. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1280. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1281. hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
  1282. HCCHAR_MULTICNT_MASK;
  1283. if (hcchar & HCCHAR_CHDIS)
  1284. dev_warn(hsotg->dev,
  1285. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1286. __func__, chan->hc_num, hcchar);
  1287. /* Set host channel enable after all other setup is complete */
  1288. hcchar |= HCCHAR_CHENA;
  1289. hcchar &= ~HCCHAR_CHDIS;
  1290. if (dbg_hc(chan))
  1291. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1292. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1293. HCCHAR_MULTICNT_SHIFT);
  1294. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1295. if (dbg_hc(chan))
  1296. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1297. chan->hc_num);
  1298. chan->xfer_started = 1;
  1299. chan->requests++;
  1300. }
  1301. /**
  1302. * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
  1303. * a previous call to dwc2_hc_start_transfer()
  1304. *
  1305. * @hsotg: Programming view of DWC_otg controller
  1306. * @chan: Information needed to initialize the host channel
  1307. *
  1308. * The caller must ensure there is sufficient space in the request queue and Tx
  1309. * Data FIFO. This function should only be called in Slave mode. In DMA mode,
  1310. * the controller acts autonomously to complete transfers programmed to a host
  1311. * channel.
  1312. *
  1313. * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  1314. * if there is any data remaining to be queued. For an IN transfer, another
  1315. * data packet is always requested. For the SETUP phase of a control transfer,
  1316. * this function does nothing.
  1317. *
  1318. * Return: 1 if a new request is queued, 0 if no more requests are required
  1319. * for this transfer
  1320. */
  1321. static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
  1322. struct dwc2_host_chan *chan)
  1323. {
  1324. if (dbg_hc(chan))
  1325. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1326. chan->hc_num);
  1327. if (chan->do_split)
  1328. /* SPLITs always queue just once per channel */
  1329. return 0;
  1330. if (chan->data_pid_start == DWC2_HC_PID_SETUP)
  1331. /* SETUPs are queued only once since they can't be NAK'd */
  1332. return 0;
  1333. if (chan->ep_is_in) {
  1334. /*
  1335. * Always queue another request for other IN transfers. If
  1336. * back-to-back INs are issued and NAKs are received for both,
  1337. * the driver may still be processing the first NAK when the
  1338. * second NAK is received. When the interrupt handler clears
  1339. * the NAK interrupt for the first NAK, the second NAK will
  1340. * not be seen. So we can't depend on the NAK interrupt
  1341. * handler to requeue a NAK'd request. Instead, IN requests
  1342. * are issued each time this function is called. When the
  1343. * transfer completes, the extra requests for the channel will
  1344. * be flushed.
  1345. */
  1346. u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1347. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1348. hcchar |= HCCHAR_CHENA;
  1349. hcchar &= ~HCCHAR_CHDIS;
  1350. if (dbg_hc(chan))
  1351. dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
  1352. hcchar);
  1353. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1354. chan->requests++;
  1355. return 1;
  1356. }
  1357. /* OUT transfers */
  1358. if (chan->xfer_count < chan->xfer_len) {
  1359. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1360. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1361. u32 hcchar = dwc2_readl(hsotg,
  1362. HCCHAR(chan->hc_num));
  1363. dwc2_hc_set_even_odd_frame(hsotg, chan,
  1364. &hcchar);
  1365. }
  1366. /* Load OUT packet into the appropriate Tx FIFO */
  1367. dwc2_hc_write_packet(hsotg, chan);
  1368. chan->requests++;
  1369. return 1;
  1370. }
  1371. return 0;
  1372. }
  1373. /*
  1374. * =========================================================================
  1375. * HCD
  1376. * =========================================================================
  1377. */
  1378. /*
  1379. * Processes all the URBs in a single list of QHs. Completes them with
  1380. * -ETIMEDOUT and frees the QTD.
  1381. *
  1382. * Must be called with interrupt disabled and spinlock held
  1383. */
  1384. static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
  1385. struct list_head *qh_list)
  1386. {
  1387. struct dwc2_qh *qh, *qh_tmp;
  1388. struct dwc2_qtd *qtd, *qtd_tmp;
  1389. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  1390. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  1391. qtd_list_entry) {
  1392. dwc2_host_complete(hsotg, qtd, -ECONNRESET);
  1393. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1394. }
  1395. }
  1396. }
  1397. static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
  1398. struct list_head *qh_list)
  1399. {
  1400. struct dwc2_qtd *qtd, *qtd_tmp;
  1401. struct dwc2_qh *qh, *qh_tmp;
  1402. unsigned long flags;
  1403. if (!qh_list->next)
  1404. /* The list hasn't been initialized yet */
  1405. return;
  1406. spin_lock_irqsave(&hsotg->lock, flags);
  1407. /* Ensure there are no QTDs or URBs left */
  1408. dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
  1409. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  1410. dwc2_hcd_qh_unlink(hsotg, qh);
  1411. /* Free each QTD in the QH's QTD list */
  1412. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  1413. qtd_list_entry)
  1414. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1415. if (qh->channel && qh->channel->qh == qh)
  1416. qh->channel->qh = NULL;
  1417. spin_unlock_irqrestore(&hsotg->lock, flags);
  1418. dwc2_hcd_qh_free(hsotg, qh);
  1419. spin_lock_irqsave(&hsotg->lock, flags);
  1420. }
  1421. spin_unlock_irqrestore(&hsotg->lock, flags);
  1422. }
  1423. /*
  1424. * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
  1425. * and periodic schedules. The QTD associated with each URB is removed from
  1426. * the schedule and freed. This function may be called when a disconnect is
  1427. * detected or when the HCD is being stopped.
  1428. *
  1429. * Must be called with interrupt disabled and spinlock held
  1430. */
  1431. static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
  1432. {
  1433. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
  1434. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
  1435. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
  1436. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
  1437. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
  1438. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
  1439. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
  1440. }
  1441. /**
  1442. * dwc2_hcd_start() - Starts the HCD when switching to Host mode
  1443. *
  1444. * @hsotg: Pointer to struct dwc2_hsotg
  1445. */
  1446. void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
  1447. {
  1448. u32 hprt0;
  1449. if (hsotg->op_state == OTG_STATE_B_HOST) {
  1450. /*
  1451. * Reset the port. During a HNP mode switch the reset
  1452. * needs to occur within 1ms and have a duration of at
  1453. * least 50ms.
  1454. */
  1455. hprt0 = dwc2_read_hprt0(hsotg);
  1456. hprt0 |= HPRT0_RST;
  1457. dwc2_writel(hsotg, hprt0, HPRT0);
  1458. }
  1459. queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
  1460. msecs_to_jiffies(50));
  1461. }
  1462. /* Must be called with interrupt disabled and spinlock held */
  1463. static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
  1464. {
  1465. int num_channels = hsotg->params.host_channels;
  1466. struct dwc2_host_chan *channel;
  1467. u32 hcchar;
  1468. int i;
  1469. if (!hsotg->params.host_dma) {
  1470. /* Flush out any channel requests in slave mode */
  1471. for (i = 0; i < num_channels; i++) {
  1472. channel = hsotg->hc_ptr_array[i];
  1473. if (!list_empty(&channel->hc_list_entry))
  1474. continue;
  1475. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1476. if (hcchar & HCCHAR_CHENA) {
  1477. hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
  1478. hcchar |= HCCHAR_CHDIS;
  1479. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1480. }
  1481. }
  1482. }
  1483. for (i = 0; i < num_channels; i++) {
  1484. channel = hsotg->hc_ptr_array[i];
  1485. if (!list_empty(&channel->hc_list_entry))
  1486. continue;
  1487. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1488. if (hcchar & HCCHAR_CHENA) {
  1489. /* Halt the channel */
  1490. hcchar |= HCCHAR_CHDIS;
  1491. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1492. }
  1493. dwc2_hc_cleanup(hsotg, channel);
  1494. list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
  1495. /*
  1496. * Added for Descriptor DMA to prevent channel double cleanup in
  1497. * release_channel_ddma(), which is called from ep_disable when
  1498. * device disconnects
  1499. */
  1500. channel->qh = NULL;
  1501. }
  1502. /* All channels have been freed, mark them available */
  1503. if (hsotg->params.uframe_sched) {
  1504. hsotg->available_host_channels =
  1505. hsotg->params.host_channels;
  1506. } else {
  1507. hsotg->non_periodic_channels = 0;
  1508. hsotg->periodic_channels = 0;
  1509. }
  1510. }
  1511. /**
  1512. * dwc2_hcd_connect() - Handles connect of the HCD
  1513. *
  1514. * @hsotg: Pointer to struct dwc2_hsotg
  1515. *
  1516. * Must be called with interrupt disabled and spinlock held
  1517. */
  1518. void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
  1519. {
  1520. if (hsotg->lx_state != DWC2_L0)
  1521. usb_hcd_resume_root_hub(hsotg->priv);
  1522. hsotg->flags.b.port_connect_status_change = 1;
  1523. hsotg->flags.b.port_connect_status = 1;
  1524. }
  1525. /**
  1526. * dwc2_hcd_disconnect() - Handles disconnect of the HCD
  1527. *
  1528. * @hsotg: Pointer to struct dwc2_hsotg
  1529. * @force: If true, we won't try to reconnect even if we see device connected.
  1530. *
  1531. * Must be called with interrupt disabled and spinlock held
  1532. */
  1533. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
  1534. {
  1535. u32 intr;
  1536. u32 hprt0;
  1537. /* Set status flags for the hub driver */
  1538. hsotg->flags.b.port_connect_status_change = 1;
  1539. hsotg->flags.b.port_connect_status = 0;
  1540. /*
  1541. * Shutdown any transfers in process by clearing the Tx FIFO Empty
  1542. * interrupt mask and status bits and disabling subsequent host
  1543. * channel interrupts.
  1544. */
  1545. intr = dwc2_readl(hsotg, GINTMSK);
  1546. intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
  1547. dwc2_writel(hsotg, intr, GINTMSK);
  1548. intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
  1549. dwc2_writel(hsotg, intr, GINTSTS);
  1550. /*
  1551. * Turn off the vbus power only if the core has transitioned to device
  1552. * mode. If still in host mode, need to keep power on to detect a
  1553. * reconnection.
  1554. */
  1555. if (dwc2_is_device_mode(hsotg)) {
  1556. if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
  1557. dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
  1558. dwc2_writel(hsotg, 0, HPRT0);
  1559. }
  1560. dwc2_disable_host_interrupts(hsotg);
  1561. }
  1562. /* Respond with an error status to all URBs in the schedule */
  1563. dwc2_kill_all_urbs(hsotg);
  1564. if (dwc2_is_host_mode(hsotg))
  1565. /* Clean up any host channels that were in use */
  1566. dwc2_hcd_cleanup_channels(hsotg);
  1567. dwc2_host_disconnect(hsotg);
  1568. /*
  1569. * Add an extra check here to see if we're actually connected but
  1570. * we don't have a detection interrupt pending. This can happen if:
  1571. * 1. hardware sees connect
  1572. * 2. hardware sees disconnect
  1573. * 3. hardware sees connect
  1574. * 4. dwc2_port_intr() - clears connect interrupt
  1575. * 5. dwc2_handle_common_intr() - calls here
  1576. *
  1577. * Without the extra check here we will end calling disconnect
  1578. * and won't get any future interrupts to handle the connect.
  1579. */
  1580. if (!force) {
  1581. hprt0 = dwc2_readl(hsotg, HPRT0);
  1582. if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
  1583. dwc2_hcd_connect(hsotg);
  1584. }
  1585. }
  1586. /**
  1587. * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
  1588. *
  1589. * @hsotg: Pointer to struct dwc2_hsotg
  1590. */
  1591. static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
  1592. {
  1593. if (hsotg->bus_suspended) {
  1594. hsotg->flags.b.port_suspend_change = 1;
  1595. usb_hcd_resume_root_hub(hsotg->priv);
  1596. }
  1597. if (hsotg->lx_state == DWC2_L1)
  1598. hsotg->flags.b.port_l1_change = 1;
  1599. }
  1600. /**
  1601. * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
  1602. *
  1603. * @hsotg: Pointer to struct dwc2_hsotg
  1604. *
  1605. * Must be called with interrupt disabled and spinlock held
  1606. */
  1607. void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
  1608. {
  1609. dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
  1610. /*
  1611. * The root hub should be disconnected before this function is called.
  1612. * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  1613. * and the QH lists (via ..._hcd_endpoint_disable).
  1614. */
  1615. /* Turn off all host-specific interrupts */
  1616. dwc2_disable_host_interrupts(hsotg);
  1617. /* Turn off the vbus power */
  1618. dev_dbg(hsotg->dev, "PortPower off\n");
  1619. dwc2_writel(hsotg, 0, HPRT0);
  1620. }
  1621. /* Caller must hold driver lock */
  1622. static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
  1623. struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
  1624. struct dwc2_qtd *qtd)
  1625. {
  1626. u32 intr_mask;
  1627. int retval;
  1628. int dev_speed;
  1629. if (!hsotg->flags.b.port_connect_status) {
  1630. /* No longer connected */
  1631. dev_err(hsotg->dev, "Not connected\n");
  1632. return -ENODEV;
  1633. }
  1634. dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  1635. /* Some configurations cannot support LS traffic on a FS root port */
  1636. if ((dev_speed == USB_SPEED_LOW) &&
  1637. (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
  1638. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
  1639. u32 hprt0 = dwc2_readl(hsotg, HPRT0);
  1640. u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  1641. if (prtspd == HPRT0_SPD_FULL_SPEED)
  1642. return -ENODEV;
  1643. }
  1644. if (!qtd)
  1645. return -EINVAL;
  1646. dwc2_hcd_qtd_init(qtd, urb);
  1647. retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
  1648. if (retval) {
  1649. dev_err(hsotg->dev,
  1650. "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
  1651. retval);
  1652. return retval;
  1653. }
  1654. intr_mask = dwc2_readl(hsotg, GINTMSK);
  1655. if (!(intr_mask & GINTSTS_SOF)) {
  1656. enum dwc2_transaction_type tr_type;
  1657. if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
  1658. !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  1659. /*
  1660. * Do not schedule SG transactions until qtd has
  1661. * URB_GIVEBACK_ASAP set
  1662. */
  1663. return 0;
  1664. tr_type = dwc2_hcd_select_transactions(hsotg);
  1665. if (tr_type != DWC2_TRANSACTION_NONE)
  1666. dwc2_hcd_queue_transactions(hsotg, tr_type);
  1667. }
  1668. return 0;
  1669. }
  1670. /* Must be called with interrupt disabled and spinlock held */
  1671. static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
  1672. struct dwc2_hcd_urb *urb)
  1673. {
  1674. struct dwc2_qh *qh;
  1675. struct dwc2_qtd *urb_qtd;
  1676. urb_qtd = urb->qtd;
  1677. if (!urb_qtd) {
  1678. dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
  1679. return -EINVAL;
  1680. }
  1681. qh = urb_qtd->qh;
  1682. if (!qh) {
  1683. dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
  1684. return -EINVAL;
  1685. }
  1686. urb->priv = NULL;
  1687. if (urb_qtd->in_process && qh->channel) {
  1688. dwc2_dump_channel_info(hsotg, qh->channel);
  1689. /* The QTD is in process (it has been assigned to a channel) */
  1690. if (hsotg->flags.b.port_connect_status)
  1691. /*
  1692. * If still connected (i.e. in host mode), halt the
  1693. * channel so it can be used for other transfers. If
  1694. * no longer connected, the host registers can't be
  1695. * written to halt the channel since the core is in
  1696. * device mode.
  1697. */
  1698. dwc2_hc_halt(hsotg, qh->channel,
  1699. DWC2_HC_XFER_URB_DEQUEUE);
  1700. }
  1701. /*
  1702. * Free the QTD and clean up the associated QH. Leave the QH in the
  1703. * schedule if it has any remaining QTDs.
  1704. */
  1705. if (!hsotg->params.dma_desc_enable) {
  1706. u8 in_process = urb_qtd->in_process;
  1707. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1708. if (in_process) {
  1709. dwc2_hcd_qh_deactivate(hsotg, qh, 0);
  1710. qh->channel = NULL;
  1711. } else if (list_empty(&qh->qtd_list)) {
  1712. dwc2_hcd_qh_unlink(hsotg, qh);
  1713. }
  1714. } else {
  1715. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1716. }
  1717. return 0;
  1718. }
  1719. /* Must NOT be called with interrupt disabled or spinlock held */
  1720. static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
  1721. struct usb_host_endpoint *ep, int retry)
  1722. {
  1723. struct dwc2_qtd *qtd, *qtd_tmp;
  1724. struct dwc2_qh *qh;
  1725. unsigned long flags;
  1726. int rc;
  1727. spin_lock_irqsave(&hsotg->lock, flags);
  1728. qh = ep->hcpriv;
  1729. if (!qh) {
  1730. rc = -EINVAL;
  1731. goto err;
  1732. }
  1733. while (!list_empty(&qh->qtd_list) && retry--) {
  1734. if (retry == 0) {
  1735. dev_err(hsotg->dev,
  1736. "## timeout in dwc2_hcd_endpoint_disable() ##\n");
  1737. rc = -EBUSY;
  1738. goto err;
  1739. }
  1740. spin_unlock_irqrestore(&hsotg->lock, flags);
  1741. msleep(20);
  1742. spin_lock_irqsave(&hsotg->lock, flags);
  1743. qh = ep->hcpriv;
  1744. if (!qh) {
  1745. rc = -EINVAL;
  1746. goto err;
  1747. }
  1748. }
  1749. dwc2_hcd_qh_unlink(hsotg, qh);
  1750. /* Free each QTD in the QH's QTD list */
  1751. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
  1752. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1753. ep->hcpriv = NULL;
  1754. if (qh->channel && qh->channel->qh == qh)
  1755. qh->channel->qh = NULL;
  1756. spin_unlock_irqrestore(&hsotg->lock, flags);
  1757. dwc2_hcd_qh_free(hsotg, qh);
  1758. return 0;
  1759. err:
  1760. ep->hcpriv = NULL;
  1761. spin_unlock_irqrestore(&hsotg->lock, flags);
  1762. return rc;
  1763. }
  1764. /* Must be called with interrupt disabled and spinlock held */
  1765. static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
  1766. struct usb_host_endpoint *ep)
  1767. {
  1768. struct dwc2_qh *qh = ep->hcpriv;
  1769. if (!qh)
  1770. return -EINVAL;
  1771. qh->data_toggle = DWC2_HC_PID_DATA0;
  1772. return 0;
  1773. }
  1774. /**
  1775. * dwc2_core_init() - Initializes the DWC_otg controller registers and
  1776. * prepares the core for device mode or host mode operation
  1777. *
  1778. * @hsotg: Programming view of the DWC_otg controller
  1779. * @initial_setup: If true then this is the first init for this instance.
  1780. */
  1781. int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
  1782. {
  1783. u32 usbcfg, otgctl;
  1784. int retval;
  1785. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  1786. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1787. /* Set ULPI External VBUS bit if needed */
  1788. usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
  1789. if (hsotg->params.phy_ulpi_ext_vbus)
  1790. usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
  1791. /* Set external TS Dline pulsing bit if needed */
  1792. usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
  1793. if (hsotg->params.ts_dline)
  1794. usbcfg |= GUSBCFG_TERMSELDLPULSE;
  1795. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1796. /*
  1797. * Reset the Controller
  1798. *
  1799. * We only need to reset the controller if this is a re-init.
  1800. * For the first init we know for sure that earlier code reset us (it
  1801. * needed to in order to properly detect various parameters).
  1802. */
  1803. if (!initial_setup) {
  1804. retval = dwc2_core_reset(hsotg, false);
  1805. if (retval) {
  1806. dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
  1807. __func__);
  1808. return retval;
  1809. }
  1810. }
  1811. /*
  1812. * This needs to happen in FS mode before any other programming occurs
  1813. */
  1814. retval = dwc2_phy_init(hsotg, initial_setup);
  1815. if (retval)
  1816. return retval;
  1817. /* Program the GAHBCFG Register */
  1818. retval = dwc2_gahbcfg_init(hsotg);
  1819. if (retval)
  1820. return retval;
  1821. /* Program the GUSBCFG register */
  1822. dwc2_gusbcfg_init(hsotg);
  1823. /* Program the GOTGCTL register */
  1824. otgctl = dwc2_readl(hsotg, GOTGCTL);
  1825. otgctl &= ~GOTGCTL_OTGVER;
  1826. dwc2_writel(hsotg, otgctl, GOTGCTL);
  1827. /* Clear the SRP success bit for FS-I2c */
  1828. hsotg->srp_success = 0;
  1829. /* Enable common interrupts */
  1830. dwc2_enable_common_interrupts(hsotg);
  1831. /*
  1832. * Do device or host initialization based on mode during PCD and
  1833. * HCD initialization
  1834. */
  1835. if (dwc2_is_host_mode(hsotg)) {
  1836. dev_dbg(hsotg->dev, "Host Mode\n");
  1837. hsotg->op_state = OTG_STATE_A_HOST;
  1838. } else {
  1839. dev_dbg(hsotg->dev, "Device Mode\n");
  1840. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  1841. }
  1842. return 0;
  1843. }
  1844. /**
  1845. * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
  1846. * Host mode
  1847. *
  1848. * @hsotg: Programming view of DWC_otg controller
  1849. *
  1850. * This function flushes the Tx and Rx FIFOs and flushes any entries in the
  1851. * request queues. Host channels are reset to ensure that they are ready for
  1852. * performing transfers.
  1853. */
  1854. static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
  1855. {
  1856. u32 hcfg, hfir, otgctl, usbcfg;
  1857. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  1858. /* Set HS/FS Timeout Calibration to 7 (max available value).
  1859. * The number of PHY clocks that the application programs in
  1860. * this field is added to the high/full speed interpacket timeout
  1861. * duration in the core to account for any additional delays
  1862. * introduced by the PHY. This can be required, because the delay
  1863. * introduced by the PHY in generating the linestate condition
  1864. * can vary from one PHY to another.
  1865. */
  1866. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1867. usbcfg |= GUSBCFG_TOUTCAL(7);
  1868. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1869. /* Restart the Phy Clock */
  1870. dwc2_writel(hsotg, 0, PCGCTL);
  1871. /* Initialize Host Configuration Register */
  1872. dwc2_init_fs_ls_pclk_sel(hsotg);
  1873. if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  1874. hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
  1875. hcfg = dwc2_readl(hsotg, HCFG);
  1876. hcfg |= HCFG_FSLSSUPP;
  1877. dwc2_writel(hsotg, hcfg, HCFG);
  1878. }
  1879. /*
  1880. * This bit allows dynamic reloading of the HFIR register during
  1881. * runtime. This bit needs to be programmed during initial configuration
  1882. * and its value must not be changed during runtime.
  1883. */
  1884. if (hsotg->params.reload_ctl) {
  1885. hfir = dwc2_readl(hsotg, HFIR);
  1886. hfir |= HFIR_RLDCTRL;
  1887. dwc2_writel(hsotg, hfir, HFIR);
  1888. }
  1889. if (hsotg->params.dma_desc_enable) {
  1890. u32 op_mode = hsotg->hw_params.op_mode;
  1891. if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
  1892. !hsotg->hw_params.dma_desc_enable ||
  1893. op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
  1894. op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
  1895. op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
  1896. dev_err(hsotg->dev,
  1897. "Hardware does not support descriptor DMA mode -\n");
  1898. dev_err(hsotg->dev,
  1899. "falling back to buffer DMA mode.\n");
  1900. hsotg->params.dma_desc_enable = false;
  1901. } else {
  1902. hcfg = dwc2_readl(hsotg, HCFG);
  1903. hcfg |= HCFG_DESCDMA;
  1904. dwc2_writel(hsotg, hcfg, HCFG);
  1905. }
  1906. }
  1907. /* Configure data FIFO sizes */
  1908. dwc2_config_fifos(hsotg);
  1909. /* TODO - check this */
  1910. /* Clear Host Set HNP Enable in the OTG Control Register */
  1911. otgctl = dwc2_readl(hsotg, GOTGCTL);
  1912. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  1913. dwc2_writel(hsotg, otgctl, GOTGCTL);
  1914. /* Make sure the FIFOs are flushed */
  1915. dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
  1916. dwc2_flush_rx_fifo(hsotg);
  1917. /* Clear Host Set HNP Enable in the OTG Control Register */
  1918. otgctl = dwc2_readl(hsotg, GOTGCTL);
  1919. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  1920. dwc2_writel(hsotg, otgctl, GOTGCTL);
  1921. if (!hsotg->params.dma_desc_enable) {
  1922. int num_channels, i;
  1923. u32 hcchar;
  1924. /* Flush out any leftover queued requests */
  1925. num_channels = hsotg->params.host_channels;
  1926. for (i = 0; i < num_channels; i++) {
  1927. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1928. if (hcchar & HCCHAR_CHENA) {
  1929. hcchar &= ~HCCHAR_CHENA;
  1930. hcchar |= HCCHAR_CHDIS;
  1931. hcchar &= ~HCCHAR_EPDIR;
  1932. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1933. }
  1934. }
  1935. /* Halt all channels to put them into a known state */
  1936. for (i = 0; i < num_channels; i++) {
  1937. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1938. if (hcchar & HCCHAR_CHENA) {
  1939. hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
  1940. hcchar &= ~HCCHAR_EPDIR;
  1941. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1942. dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
  1943. __func__, i);
  1944. if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
  1945. HCCHAR_CHENA,
  1946. 1000)) {
  1947. dev_warn(hsotg->dev,
  1948. "Unable to clear enable on channel %d\n",
  1949. i);
  1950. }
  1951. }
  1952. }
  1953. }
  1954. /* Enable ACG feature in host mode, if supported */
  1955. dwc2_enable_acg(hsotg);
  1956. /* Turn on the vbus power */
  1957. dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
  1958. if (hsotg->op_state == OTG_STATE_A_HOST) {
  1959. u32 hprt0 = dwc2_read_hprt0(hsotg);
  1960. dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
  1961. !!(hprt0 & HPRT0_PWR));
  1962. if (!(hprt0 & HPRT0_PWR)) {
  1963. hprt0 |= HPRT0_PWR;
  1964. dwc2_writel(hsotg, hprt0, HPRT0);
  1965. }
  1966. }
  1967. dwc2_enable_host_interrupts(hsotg);
  1968. }
  1969. /*
  1970. * Initializes dynamic portions of the DWC_otg HCD state
  1971. *
  1972. * Must be called with interrupt disabled and spinlock held
  1973. */
  1974. static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
  1975. {
  1976. struct dwc2_host_chan *chan, *chan_tmp;
  1977. int num_channels;
  1978. int i;
  1979. hsotg->flags.d32 = 0;
  1980. hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
  1981. if (hsotg->params.uframe_sched) {
  1982. hsotg->available_host_channels =
  1983. hsotg->params.host_channels;
  1984. } else {
  1985. hsotg->non_periodic_channels = 0;
  1986. hsotg->periodic_channels = 0;
  1987. }
  1988. /*
  1989. * Put all channels in the free channel list and clean up channel
  1990. * states
  1991. */
  1992. list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
  1993. hc_list_entry)
  1994. list_del_init(&chan->hc_list_entry);
  1995. num_channels = hsotg->params.host_channels;
  1996. for (i = 0; i < num_channels; i++) {
  1997. chan = hsotg->hc_ptr_array[i];
  1998. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  1999. dwc2_hc_cleanup(hsotg, chan);
  2000. }
  2001. /* Initialize the DWC core for host mode operation */
  2002. dwc2_core_host_init(hsotg);
  2003. }
  2004. static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
  2005. struct dwc2_host_chan *chan,
  2006. struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  2007. {
  2008. int hub_addr, hub_port;
  2009. chan->do_split = 1;
  2010. chan->xact_pos = qtd->isoc_split_pos;
  2011. chan->complete_split = qtd->complete_split;
  2012. dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
  2013. chan->hub_addr = (u8)hub_addr;
  2014. chan->hub_port = (u8)hub_port;
  2015. }
  2016. static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
  2017. struct dwc2_host_chan *chan,
  2018. struct dwc2_qtd *qtd)
  2019. {
  2020. struct dwc2_hcd_urb *urb = qtd->urb;
  2021. struct dwc2_hcd_iso_packet_desc *frame_desc;
  2022. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  2023. case USB_ENDPOINT_XFER_CONTROL:
  2024. chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
  2025. switch (qtd->control_phase) {
  2026. case DWC2_CONTROL_SETUP:
  2027. dev_vdbg(hsotg->dev, " Control setup transaction\n");
  2028. chan->do_ping = 0;
  2029. chan->ep_is_in = 0;
  2030. chan->data_pid_start = DWC2_HC_PID_SETUP;
  2031. if (hsotg->params.host_dma)
  2032. chan->xfer_dma = urb->setup_dma;
  2033. else
  2034. chan->xfer_buf = urb->setup_packet;
  2035. chan->xfer_len = 8;
  2036. break;
  2037. case DWC2_CONTROL_DATA:
  2038. dev_vdbg(hsotg->dev, " Control data transaction\n");
  2039. chan->data_pid_start = qtd->data_toggle;
  2040. break;
  2041. case DWC2_CONTROL_STATUS:
  2042. /*
  2043. * Direction is opposite of data direction or IN if no
  2044. * data
  2045. */
  2046. dev_vdbg(hsotg->dev, " Control status transaction\n");
  2047. if (urb->length == 0)
  2048. chan->ep_is_in = 1;
  2049. else
  2050. chan->ep_is_in =
  2051. dwc2_hcd_is_pipe_out(&urb->pipe_info);
  2052. if (chan->ep_is_in)
  2053. chan->do_ping = 0;
  2054. chan->data_pid_start = DWC2_HC_PID_DATA1;
  2055. chan->xfer_len = 0;
  2056. if (hsotg->params.host_dma)
  2057. chan->xfer_dma = hsotg->status_buf_dma;
  2058. else
  2059. chan->xfer_buf = hsotg->status_buf;
  2060. break;
  2061. }
  2062. break;
  2063. case USB_ENDPOINT_XFER_BULK:
  2064. chan->ep_type = USB_ENDPOINT_XFER_BULK;
  2065. break;
  2066. case USB_ENDPOINT_XFER_INT:
  2067. chan->ep_type = USB_ENDPOINT_XFER_INT;
  2068. break;
  2069. case USB_ENDPOINT_XFER_ISOC:
  2070. chan->ep_type = USB_ENDPOINT_XFER_ISOC;
  2071. if (hsotg->params.dma_desc_enable)
  2072. break;
  2073. frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  2074. frame_desc->status = 0;
  2075. if (hsotg->params.host_dma) {
  2076. chan->xfer_dma = urb->dma;
  2077. chan->xfer_dma += frame_desc->offset +
  2078. qtd->isoc_split_offset;
  2079. } else {
  2080. chan->xfer_buf = urb->buf;
  2081. chan->xfer_buf += frame_desc->offset +
  2082. qtd->isoc_split_offset;
  2083. }
  2084. chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
  2085. if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
  2086. if (chan->xfer_len <= 188)
  2087. chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
  2088. else
  2089. chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
  2090. }
  2091. break;
  2092. }
  2093. }
  2094. static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
  2095. struct dwc2_qh *qh,
  2096. struct dwc2_host_chan *chan)
  2097. {
  2098. if (!hsotg->unaligned_cache ||
  2099. chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
  2100. return -ENOMEM;
  2101. if (!qh->dw_align_buf) {
  2102. qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
  2103. GFP_ATOMIC | GFP_DMA);
  2104. if (!qh->dw_align_buf)
  2105. return -ENOMEM;
  2106. }
  2107. qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
  2108. DWC2_KMEM_UNALIGNED_BUF_SIZE,
  2109. DMA_FROM_DEVICE);
  2110. if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
  2111. dev_err(hsotg->dev, "can't map align_buf\n");
  2112. chan->align_buf = 0;
  2113. return -EINVAL;
  2114. }
  2115. chan->align_buf = qh->dw_align_buf_dma;
  2116. return 0;
  2117. }
  2118. #define DWC2_USB_DMA_ALIGN 4
  2119. static void dwc2_free_dma_aligned_buffer(struct urb *urb)
  2120. {
  2121. void *stored_xfer_buffer;
  2122. size_t length;
  2123. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  2124. return;
  2125. /* Restore urb->transfer_buffer from the end of the allocated area */
  2126. memcpy(&stored_xfer_buffer,
  2127. PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
  2128. dma_get_cache_alignment()),
  2129. sizeof(urb->transfer_buffer));
  2130. if (usb_urb_dir_in(urb)) {
  2131. if (usb_pipeisoc(urb->pipe))
  2132. length = urb->transfer_buffer_length;
  2133. else
  2134. length = urb->actual_length;
  2135. memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
  2136. }
  2137. kfree(urb->transfer_buffer);
  2138. urb->transfer_buffer = stored_xfer_buffer;
  2139. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  2140. }
  2141. static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  2142. {
  2143. void *kmalloc_ptr;
  2144. size_t kmalloc_size;
  2145. if (urb->num_sgs || urb->sg ||
  2146. urb->transfer_buffer_length == 0 ||
  2147. !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
  2148. return 0;
  2149. /*
  2150. * Allocate a buffer with enough padding for original transfer_buffer
  2151. * pointer. This allocation is guaranteed to be aligned properly for
  2152. * DMA
  2153. */
  2154. kmalloc_size = urb->transfer_buffer_length +
  2155. (dma_get_cache_alignment() - 1) +
  2156. sizeof(urb->transfer_buffer);
  2157. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  2158. if (!kmalloc_ptr)
  2159. return -ENOMEM;
  2160. /*
  2161. * Position value of original urb->transfer_buffer pointer to the end
  2162. * of allocation for later referencing
  2163. */
  2164. memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
  2165. dma_get_cache_alignment()),
  2166. &urb->transfer_buffer, sizeof(urb->transfer_buffer));
  2167. if (usb_urb_dir_out(urb))
  2168. memcpy(kmalloc_ptr, urb->transfer_buffer,
  2169. urb->transfer_buffer_length);
  2170. urb->transfer_buffer = kmalloc_ptr;
  2171. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  2172. return 0;
  2173. }
  2174. static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  2175. gfp_t mem_flags)
  2176. {
  2177. int ret;
  2178. /* We assume setup_dma is always aligned; warn if not */
  2179. WARN_ON_ONCE(urb->setup_dma &&
  2180. (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
  2181. ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
  2182. if (ret)
  2183. return ret;
  2184. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2185. if (ret)
  2186. dwc2_free_dma_aligned_buffer(urb);
  2187. return ret;
  2188. }
  2189. static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  2190. {
  2191. usb_hcd_unmap_urb_for_dma(hcd, urb);
  2192. dwc2_free_dma_aligned_buffer(urb);
  2193. }
  2194. /**
  2195. * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
  2196. * channel and initializes the host channel to perform the transactions. The
  2197. * host channel is removed from the free list.
  2198. *
  2199. * @hsotg: The HCD state structure
  2200. * @qh: Transactions from the first QTD for this QH are selected and assigned
  2201. * to a free host channel
  2202. */
  2203. static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  2204. {
  2205. struct dwc2_host_chan *chan;
  2206. struct dwc2_hcd_urb *urb;
  2207. struct dwc2_qtd *qtd;
  2208. if (dbg_qh(qh))
  2209. dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
  2210. if (list_empty(&qh->qtd_list)) {
  2211. dev_dbg(hsotg->dev, "No QTDs in QH list\n");
  2212. return -ENOMEM;
  2213. }
  2214. if (list_empty(&hsotg->free_hc_list)) {
  2215. dev_dbg(hsotg->dev, "No free channel to assign\n");
  2216. return -ENOMEM;
  2217. }
  2218. chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
  2219. hc_list_entry);
  2220. /* Remove host channel from free list */
  2221. list_del_init(&chan->hc_list_entry);
  2222. qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
  2223. urb = qtd->urb;
  2224. qh->channel = chan;
  2225. qtd->in_process = 1;
  2226. /*
  2227. * Use usb_pipedevice to determine device address. This address is
  2228. * 0 before the SET_ADDRESS command and the correct address afterward.
  2229. */
  2230. chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
  2231. chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
  2232. chan->speed = qh->dev_speed;
  2233. chan->max_packet = qh->maxp;
  2234. chan->xfer_started = 0;
  2235. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  2236. chan->error_state = (qtd->error_count > 0);
  2237. chan->halt_on_queue = 0;
  2238. chan->halt_pending = 0;
  2239. chan->requests = 0;
  2240. /*
  2241. * The following values may be modified in the transfer type section
  2242. * below. The xfer_len value may be reduced when the transfer is
  2243. * started to accommodate the max widths of the XferSize and PktCnt
  2244. * fields in the HCTSIZn register.
  2245. */
  2246. chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
  2247. if (chan->ep_is_in)
  2248. chan->do_ping = 0;
  2249. else
  2250. chan->do_ping = qh->ping_state;
  2251. chan->data_pid_start = qh->data_toggle;
  2252. chan->multi_count = 1;
  2253. if (urb->actual_length > urb->length &&
  2254. !dwc2_hcd_is_pipe_in(&urb->pipe_info))
  2255. urb->actual_length = urb->length;
  2256. if (hsotg->params.host_dma)
  2257. chan->xfer_dma = urb->dma + urb->actual_length;
  2258. else
  2259. chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
  2260. chan->xfer_len = urb->length - urb->actual_length;
  2261. chan->xfer_count = 0;
  2262. /* Set the split attributes if required */
  2263. if (qh->do_split)
  2264. dwc2_hc_init_split(hsotg, chan, qtd, urb);
  2265. else
  2266. chan->do_split = 0;
  2267. /* Set the transfer attributes */
  2268. dwc2_hc_init_xfer(hsotg, chan, qtd);
  2269. /* For non-dword aligned buffers */
  2270. if (hsotg->params.host_dma && qh->do_split &&
  2271. chan->ep_is_in && (chan->xfer_dma & 0x3)) {
  2272. dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
  2273. if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
  2274. dev_err(hsotg->dev,
  2275. "Failed to allocate memory to handle non-aligned buffer\n");
  2276. /* Add channel back to free list */
  2277. chan->align_buf = 0;
  2278. chan->multi_count = 0;
  2279. list_add_tail(&chan->hc_list_entry,
  2280. &hsotg->free_hc_list);
  2281. qtd->in_process = 0;
  2282. qh->channel = NULL;
  2283. return -ENOMEM;
  2284. }
  2285. } else {
  2286. /*
  2287. * We assume that DMA is always aligned in non-split
  2288. * case or split out case. Warn if not.
  2289. */
  2290. WARN_ON_ONCE(hsotg->params.host_dma &&
  2291. (chan->xfer_dma & 0x3));
  2292. chan->align_buf = 0;
  2293. }
  2294. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  2295. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  2296. /*
  2297. * This value may be modified when the transfer is started
  2298. * to reflect the actual transfer length
  2299. */
  2300. chan->multi_count = qh->maxp_mult;
  2301. if (hsotg->params.dma_desc_enable) {
  2302. chan->desc_list_addr = qh->desc_list_dma;
  2303. chan->desc_list_sz = qh->desc_list_sz;
  2304. }
  2305. dwc2_hc_init(hsotg, chan);
  2306. chan->qh = qh;
  2307. return 0;
  2308. }
  2309. /**
  2310. * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
  2311. * schedule and assigns them to available host channels. Called from the HCD
  2312. * interrupt handler functions.
  2313. *
  2314. * @hsotg: The HCD state structure
  2315. *
  2316. * Return: The types of new transactions that were assigned to host channels
  2317. */
  2318. enum dwc2_transaction_type dwc2_hcd_select_transactions(
  2319. struct dwc2_hsotg *hsotg)
  2320. {
  2321. enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
  2322. struct list_head *qh_ptr;
  2323. struct dwc2_qh *qh;
  2324. int num_channels;
  2325. #ifdef DWC2_DEBUG_SOF
  2326. dev_vdbg(hsotg->dev, " Select Transactions\n");
  2327. #endif
  2328. /* Process entries in the periodic ready list */
  2329. qh_ptr = hsotg->periodic_sched_ready.next;
  2330. while (qh_ptr != &hsotg->periodic_sched_ready) {
  2331. if (list_empty(&hsotg->free_hc_list))
  2332. break;
  2333. if (hsotg->params.uframe_sched) {
  2334. if (hsotg->available_host_channels <= 1)
  2335. break;
  2336. hsotg->available_host_channels--;
  2337. }
  2338. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2339. if (dwc2_assign_and_init_hc(hsotg, qh)) {
  2340. if (hsotg->params.uframe_sched)
  2341. hsotg->available_host_channels++;
  2342. break;
  2343. }
  2344. /*
  2345. * Move the QH from the periodic ready schedule to the
  2346. * periodic assigned schedule
  2347. */
  2348. qh_ptr = qh_ptr->next;
  2349. list_move_tail(&qh->qh_list_entry,
  2350. &hsotg->periodic_sched_assigned);
  2351. ret_val = DWC2_TRANSACTION_PERIODIC;
  2352. }
  2353. /*
  2354. * Process entries in the inactive portion of the non-periodic
  2355. * schedule. Some free host channels may not be used if they are
  2356. * reserved for periodic transfers.
  2357. */
  2358. num_channels = hsotg->params.host_channels;
  2359. qh_ptr = hsotg->non_periodic_sched_inactive.next;
  2360. while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
  2361. if (!hsotg->params.uframe_sched &&
  2362. hsotg->non_periodic_channels >= num_channels -
  2363. hsotg->periodic_channels)
  2364. break;
  2365. if (list_empty(&hsotg->free_hc_list))
  2366. break;
  2367. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2368. if (hsotg->params.uframe_sched) {
  2369. if (hsotg->available_host_channels < 1)
  2370. break;
  2371. hsotg->available_host_channels--;
  2372. }
  2373. if (dwc2_assign_and_init_hc(hsotg, qh)) {
  2374. if (hsotg->params.uframe_sched)
  2375. hsotg->available_host_channels++;
  2376. break;
  2377. }
  2378. /*
  2379. * Move the QH from the non-periodic inactive schedule to the
  2380. * non-periodic active schedule
  2381. */
  2382. qh_ptr = qh_ptr->next;
  2383. list_move_tail(&qh->qh_list_entry,
  2384. &hsotg->non_periodic_sched_active);
  2385. if (ret_val == DWC2_TRANSACTION_NONE)
  2386. ret_val = DWC2_TRANSACTION_NON_PERIODIC;
  2387. else
  2388. ret_val = DWC2_TRANSACTION_ALL;
  2389. if (!hsotg->params.uframe_sched)
  2390. hsotg->non_periodic_channels++;
  2391. }
  2392. return ret_val;
  2393. }
  2394. /**
  2395. * dwc2_queue_transaction() - Attempts to queue a single transaction request for
  2396. * a host channel associated with either a periodic or non-periodic transfer
  2397. *
  2398. * @hsotg: The HCD state structure
  2399. * @chan: Host channel descriptor associated with either a periodic or
  2400. * non-periodic transfer
  2401. * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
  2402. * for periodic transfers or the non-periodic Tx FIFO
  2403. * for non-periodic transfers
  2404. *
  2405. * Return: 1 if a request is queued and more requests may be needed to
  2406. * complete the transfer, 0 if no more requests are required for this
  2407. * transfer, -1 if there is insufficient space in the Tx FIFO
  2408. *
  2409. * This function assumes that there is space available in the appropriate
  2410. * request queue. For an OUT transfer or SETUP transaction in Slave mode,
  2411. * it checks whether space is available in the appropriate Tx FIFO.
  2412. *
  2413. * Must be called with interrupt disabled and spinlock held
  2414. */
  2415. static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
  2416. struct dwc2_host_chan *chan,
  2417. u16 fifo_dwords_avail)
  2418. {
  2419. int retval = 0;
  2420. if (chan->do_split)
  2421. /* Put ourselves on the list to keep order straight */
  2422. list_move_tail(&chan->split_order_list_entry,
  2423. &hsotg->split_order);
  2424. if (hsotg->params.host_dma && chan->qh) {
  2425. if (hsotg->params.dma_desc_enable) {
  2426. if (!chan->xfer_started ||
  2427. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  2428. dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
  2429. chan->qh->ping_state = 0;
  2430. }
  2431. } else if (!chan->xfer_started) {
  2432. dwc2_hc_start_transfer(hsotg, chan);
  2433. chan->qh->ping_state = 0;
  2434. }
  2435. } else if (chan->halt_pending) {
  2436. /* Don't queue a request if the channel has been halted */
  2437. } else if (chan->halt_on_queue) {
  2438. dwc2_hc_halt(hsotg, chan, chan->halt_status);
  2439. } else if (chan->do_ping) {
  2440. if (!chan->xfer_started)
  2441. dwc2_hc_start_transfer(hsotg, chan);
  2442. } else if (!chan->ep_is_in ||
  2443. chan->data_pid_start == DWC2_HC_PID_SETUP) {
  2444. if ((fifo_dwords_avail * 4) >= chan->max_packet) {
  2445. if (!chan->xfer_started) {
  2446. dwc2_hc_start_transfer(hsotg, chan);
  2447. retval = 1;
  2448. } else {
  2449. retval = dwc2_hc_continue_transfer(hsotg, chan);
  2450. }
  2451. } else {
  2452. retval = -1;
  2453. }
  2454. } else {
  2455. if (!chan->xfer_started) {
  2456. dwc2_hc_start_transfer(hsotg, chan);
  2457. retval = 1;
  2458. } else {
  2459. retval = dwc2_hc_continue_transfer(hsotg, chan);
  2460. }
  2461. }
  2462. return retval;
  2463. }
  2464. /*
  2465. * Processes periodic channels for the next frame and queues transactions for
  2466. * these channels to the DWC_otg controller. After queueing transactions, the
  2467. * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  2468. * to queue as Periodic Tx FIFO or request queue space becomes available.
  2469. * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  2470. *
  2471. * Must be called with interrupt disabled and spinlock held
  2472. */
  2473. static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
  2474. {
  2475. struct list_head *qh_ptr;
  2476. struct dwc2_qh *qh;
  2477. u32 tx_status;
  2478. u32 fspcavail;
  2479. u32 gintmsk;
  2480. int status;
  2481. bool no_queue_space = false;
  2482. bool no_fifo_space = false;
  2483. u32 qspcavail;
  2484. /* If empty list then just adjust interrupt enables */
  2485. if (list_empty(&hsotg->periodic_sched_assigned))
  2486. goto exit;
  2487. if (dbg_perio())
  2488. dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
  2489. tx_status = dwc2_readl(hsotg, HPTXSTS);
  2490. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2491. TXSTS_QSPCAVAIL_SHIFT;
  2492. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2493. TXSTS_FSPCAVAIL_SHIFT;
  2494. if (dbg_perio()) {
  2495. dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
  2496. qspcavail);
  2497. dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
  2498. fspcavail);
  2499. }
  2500. qh_ptr = hsotg->periodic_sched_assigned.next;
  2501. while (qh_ptr != &hsotg->periodic_sched_assigned) {
  2502. tx_status = dwc2_readl(hsotg, HPTXSTS);
  2503. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2504. TXSTS_QSPCAVAIL_SHIFT;
  2505. if (qspcavail == 0) {
  2506. no_queue_space = true;
  2507. break;
  2508. }
  2509. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2510. if (!qh->channel) {
  2511. qh_ptr = qh_ptr->next;
  2512. continue;
  2513. }
  2514. /* Make sure EP's TT buffer is clean before queueing qtds */
  2515. if (qh->tt_buffer_dirty) {
  2516. qh_ptr = qh_ptr->next;
  2517. continue;
  2518. }
  2519. /*
  2520. * Set a flag if we're queuing high-bandwidth in slave mode.
  2521. * The flag prevents any halts to get into the request queue in
  2522. * the middle of multiple high-bandwidth packets getting queued.
  2523. */
  2524. if (!hsotg->params.host_dma &&
  2525. qh->channel->multi_count > 1)
  2526. hsotg->queuing_high_bandwidth = 1;
  2527. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2528. TXSTS_FSPCAVAIL_SHIFT;
  2529. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  2530. if (status < 0) {
  2531. no_fifo_space = true;
  2532. break;
  2533. }
  2534. /*
  2535. * In Slave mode, stay on the current transfer until there is
  2536. * nothing more to do or the high-bandwidth request count is
  2537. * reached. In DMA mode, only need to queue one request. The
  2538. * controller automatically handles multiple packets for
  2539. * high-bandwidth transfers.
  2540. */
  2541. if (hsotg->params.host_dma || status == 0 ||
  2542. qh->channel->requests == qh->channel->multi_count) {
  2543. qh_ptr = qh_ptr->next;
  2544. /*
  2545. * Move the QH from the periodic assigned schedule to
  2546. * the periodic queued schedule
  2547. */
  2548. list_move_tail(&qh->qh_list_entry,
  2549. &hsotg->periodic_sched_queued);
  2550. /* done queuing high bandwidth */
  2551. hsotg->queuing_high_bandwidth = 0;
  2552. }
  2553. }
  2554. exit:
  2555. if (no_queue_space || no_fifo_space ||
  2556. (!hsotg->params.host_dma &&
  2557. !list_empty(&hsotg->periodic_sched_assigned))) {
  2558. /*
  2559. * May need to queue more transactions as the request
  2560. * queue or Tx FIFO empties. Enable the periodic Tx
  2561. * FIFO empty interrupt. (Always use the half-empty
  2562. * level to ensure that new requests are loaded as
  2563. * soon as possible.)
  2564. */
  2565. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2566. if (!(gintmsk & GINTSTS_PTXFEMP)) {
  2567. gintmsk |= GINTSTS_PTXFEMP;
  2568. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2569. }
  2570. } else {
  2571. /*
  2572. * Disable the Tx FIFO empty interrupt since there are
  2573. * no more transactions that need to be queued right
  2574. * now. This function is called from interrupt
  2575. * handlers to queue more transactions as transfer
  2576. * states change.
  2577. */
  2578. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2579. if (gintmsk & GINTSTS_PTXFEMP) {
  2580. gintmsk &= ~GINTSTS_PTXFEMP;
  2581. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2582. }
  2583. }
  2584. }
  2585. /*
  2586. * Processes active non-periodic channels and queues transactions for these
  2587. * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  2588. * FIFO Empty interrupt is enabled if there are more transactions to queue as
  2589. * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  2590. * FIFO Empty interrupt is disabled.
  2591. *
  2592. * Must be called with interrupt disabled and spinlock held
  2593. */
  2594. static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
  2595. {
  2596. struct list_head *orig_qh_ptr;
  2597. struct dwc2_qh *qh;
  2598. u32 tx_status;
  2599. u32 qspcavail;
  2600. u32 fspcavail;
  2601. u32 gintmsk;
  2602. int status;
  2603. int no_queue_space = 0;
  2604. int no_fifo_space = 0;
  2605. int more_to_do = 0;
  2606. dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
  2607. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2608. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2609. TXSTS_QSPCAVAIL_SHIFT;
  2610. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2611. TXSTS_FSPCAVAIL_SHIFT;
  2612. dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
  2613. qspcavail);
  2614. dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
  2615. fspcavail);
  2616. /*
  2617. * Keep track of the starting point. Skip over the start-of-list
  2618. * entry.
  2619. */
  2620. if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
  2621. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  2622. orig_qh_ptr = hsotg->non_periodic_qh_ptr;
  2623. /*
  2624. * Process once through the active list or until no more space is
  2625. * available in the request queue or the Tx FIFO
  2626. */
  2627. do {
  2628. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2629. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2630. TXSTS_QSPCAVAIL_SHIFT;
  2631. if (!hsotg->params.host_dma && qspcavail == 0) {
  2632. no_queue_space = 1;
  2633. break;
  2634. }
  2635. qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
  2636. qh_list_entry);
  2637. if (!qh->channel)
  2638. goto next;
  2639. /* Make sure EP's TT buffer is clean before queueing qtds */
  2640. if (qh->tt_buffer_dirty)
  2641. goto next;
  2642. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2643. TXSTS_FSPCAVAIL_SHIFT;
  2644. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  2645. if (status > 0) {
  2646. more_to_do = 1;
  2647. } else if (status < 0) {
  2648. no_fifo_space = 1;
  2649. break;
  2650. }
  2651. next:
  2652. /* Advance to next QH, skipping start-of-list entry */
  2653. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  2654. if (hsotg->non_periodic_qh_ptr ==
  2655. &hsotg->non_periodic_sched_active)
  2656. hsotg->non_periodic_qh_ptr =
  2657. hsotg->non_periodic_qh_ptr->next;
  2658. } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
  2659. if (!hsotg->params.host_dma) {
  2660. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2661. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2662. TXSTS_QSPCAVAIL_SHIFT;
  2663. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2664. TXSTS_FSPCAVAIL_SHIFT;
  2665. dev_vdbg(hsotg->dev,
  2666. " NP Tx Req Queue Space Avail (after queue): %d\n",
  2667. qspcavail);
  2668. dev_vdbg(hsotg->dev,
  2669. " NP Tx FIFO Space Avail (after queue): %d\n",
  2670. fspcavail);
  2671. if (more_to_do || no_queue_space || no_fifo_space) {
  2672. /*
  2673. * May need to queue more transactions as the request
  2674. * queue or Tx FIFO empties. Enable the non-periodic
  2675. * Tx FIFO empty interrupt. (Always use the half-empty
  2676. * level to ensure that new requests are loaded as
  2677. * soon as possible.)
  2678. */
  2679. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2680. gintmsk |= GINTSTS_NPTXFEMP;
  2681. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2682. } else {
  2683. /*
  2684. * Disable the Tx FIFO empty interrupt since there are
  2685. * no more transactions that need to be queued right
  2686. * now. This function is called from interrupt
  2687. * handlers to queue more transactions as transfer
  2688. * states change.
  2689. */
  2690. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2691. gintmsk &= ~GINTSTS_NPTXFEMP;
  2692. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2693. }
  2694. }
  2695. }
  2696. /**
  2697. * dwc2_hcd_queue_transactions() - Processes the currently active host channels
  2698. * and queues transactions for these channels to the DWC_otg controller. Called
  2699. * from the HCD interrupt handler functions.
  2700. *
  2701. * @hsotg: The HCD state structure
  2702. * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
  2703. * or both)
  2704. *
  2705. * Must be called with interrupt disabled and spinlock held
  2706. */
  2707. void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
  2708. enum dwc2_transaction_type tr_type)
  2709. {
  2710. #ifdef DWC2_DEBUG_SOF
  2711. dev_vdbg(hsotg->dev, "Queue Transactions\n");
  2712. #endif
  2713. /* Process host channels associated with periodic transfers */
  2714. if (tr_type == DWC2_TRANSACTION_PERIODIC ||
  2715. tr_type == DWC2_TRANSACTION_ALL)
  2716. dwc2_process_periodic_channels(hsotg);
  2717. /* Process host channels associated with non-periodic transfers */
  2718. if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
  2719. tr_type == DWC2_TRANSACTION_ALL) {
  2720. if (!list_empty(&hsotg->non_periodic_sched_active)) {
  2721. dwc2_process_non_periodic_channels(hsotg);
  2722. } else {
  2723. /*
  2724. * Ensure NP Tx FIFO empty interrupt is disabled when
  2725. * there are no non-periodic transfers to process
  2726. */
  2727. u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
  2728. gintmsk &= ~GINTSTS_NPTXFEMP;
  2729. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2730. }
  2731. }
  2732. }
  2733. static void dwc2_conn_id_status_change(struct work_struct *work)
  2734. {
  2735. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  2736. wf_otg);
  2737. u32 count = 0;
  2738. u32 gotgctl;
  2739. unsigned long flags;
  2740. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2741. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2742. dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
  2743. dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
  2744. !!(gotgctl & GOTGCTL_CONID_B));
  2745. /* B-Device connector (Device Mode) */
  2746. if (gotgctl & GOTGCTL_CONID_B) {
  2747. dwc2_vbus_supply_exit(hsotg);
  2748. /* Wait for switch to device mode */
  2749. dev_dbg(hsotg->dev, "connId B\n");
  2750. if (hsotg->bus_suspended) {
  2751. dev_info(hsotg->dev,
  2752. "Do port resume before switching to device mode\n");
  2753. dwc2_port_resume(hsotg);
  2754. }
  2755. while (!dwc2_is_device_mode(hsotg)) {
  2756. dev_info(hsotg->dev,
  2757. "Waiting for Peripheral Mode, Mode=%s\n",
  2758. dwc2_is_host_mode(hsotg) ? "Host" :
  2759. "Peripheral");
  2760. msleep(20);
  2761. /*
  2762. * Sometimes the initial GOTGCTRL read is wrong, so
  2763. * check it again and jump to host mode if that was
  2764. * the case.
  2765. */
  2766. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2767. if (!(gotgctl & GOTGCTL_CONID_B))
  2768. goto host;
  2769. if (++count > 250)
  2770. break;
  2771. }
  2772. if (count > 250)
  2773. dev_err(hsotg->dev,
  2774. "Connection id status change timed out\n");
  2775. /*
  2776. * Exit Partial Power Down without restoring registers.
  2777. * No need to check the return value as registers
  2778. * are not being restored.
  2779. */
  2780. if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2)
  2781. dwc2_exit_partial_power_down(hsotg, 0, false);
  2782. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  2783. dwc2_core_init(hsotg, false);
  2784. dwc2_enable_global_interrupts(hsotg);
  2785. spin_lock_irqsave(&hsotg->lock, flags);
  2786. dwc2_hsotg_core_init_disconnected(hsotg, false);
  2787. spin_unlock_irqrestore(&hsotg->lock, flags);
  2788. /* Enable ACG feature in device mode,if supported */
  2789. dwc2_enable_acg(hsotg);
  2790. dwc2_hsotg_core_connect(hsotg);
  2791. } else {
  2792. host:
  2793. /* A-Device connector (Host Mode) */
  2794. dev_dbg(hsotg->dev, "connId A\n");
  2795. while (!dwc2_is_host_mode(hsotg)) {
  2796. dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
  2797. dwc2_is_host_mode(hsotg) ?
  2798. "Host" : "Peripheral");
  2799. msleep(20);
  2800. if (++count > 250)
  2801. break;
  2802. }
  2803. if (count > 250)
  2804. dev_err(hsotg->dev,
  2805. "Connection id status change timed out\n");
  2806. spin_lock_irqsave(&hsotg->lock, flags);
  2807. dwc2_hsotg_disconnect(hsotg);
  2808. spin_unlock_irqrestore(&hsotg->lock, flags);
  2809. hsotg->op_state = OTG_STATE_A_HOST;
  2810. /* Initialize the Core for Host mode */
  2811. dwc2_core_init(hsotg, false);
  2812. dwc2_enable_global_interrupts(hsotg);
  2813. dwc2_hcd_start(hsotg);
  2814. }
  2815. }
  2816. static void dwc2_wakeup_detected(struct timer_list *t)
  2817. {
  2818. struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
  2819. u32 hprt0;
  2820. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2821. /*
  2822. * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  2823. * so that OPT tests pass with all PHYs.)
  2824. */
  2825. hprt0 = dwc2_read_hprt0(hsotg);
  2826. dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
  2827. hprt0 &= ~HPRT0_RES;
  2828. dwc2_writel(hsotg, hprt0, HPRT0);
  2829. dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
  2830. dwc2_readl(hsotg, HPRT0));
  2831. dwc2_hcd_rem_wakeup(hsotg);
  2832. hsotg->bus_suspended = false;
  2833. /* Change to L0 state */
  2834. hsotg->lx_state = DWC2_L0;
  2835. }
  2836. static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
  2837. {
  2838. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  2839. return hcd->self.b_hnp_enable;
  2840. }
  2841. /**
  2842. * dwc2_port_suspend() - Put controller in suspend mode for host.
  2843. *
  2844. * @hsotg: Programming view of the DWC_otg controller
  2845. * @windex: The control request wIndex field
  2846. *
  2847. * Return: non-zero if failed to enter suspend mode for host.
  2848. *
  2849. * This function is for entering Host mode suspend.
  2850. * Must NOT be called with interrupt disabled or spinlock held.
  2851. */
  2852. int dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
  2853. {
  2854. unsigned long flags;
  2855. u32 pcgctl;
  2856. u32 gotgctl;
  2857. int ret = 0;
  2858. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2859. spin_lock_irqsave(&hsotg->lock, flags);
  2860. if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
  2861. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2862. gotgctl |= GOTGCTL_HSTSETHNPEN;
  2863. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  2864. hsotg->op_state = OTG_STATE_A_SUSPEND;
  2865. }
  2866. switch (hsotg->params.power_down) {
  2867. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  2868. ret = dwc2_enter_partial_power_down(hsotg);
  2869. if (ret)
  2870. dev_err(hsotg->dev,
  2871. "enter partial_power_down failed.\n");
  2872. break;
  2873. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  2874. /*
  2875. * Perform spin unlock and lock because in
  2876. * "dwc2_host_enter_hibernation()" function there is a spinlock
  2877. * logic which prevents servicing of any IRQ during entering
  2878. * hibernation.
  2879. */
  2880. spin_unlock_irqrestore(&hsotg->lock, flags);
  2881. ret = dwc2_enter_hibernation(hsotg, 1);
  2882. if (ret)
  2883. dev_err(hsotg->dev, "enter hibernation failed.\n");
  2884. spin_lock_irqsave(&hsotg->lock, flags);
  2885. break;
  2886. case DWC2_POWER_DOWN_PARAM_NONE:
  2887. /*
  2888. * If not hibernation nor partial power down are supported,
  2889. * clock gating is used to save power.
  2890. */
  2891. if (!hsotg->params.no_clock_gating)
  2892. dwc2_host_enter_clock_gating(hsotg);
  2893. break;
  2894. }
  2895. /* For HNP the bus must be suspended for at least 200ms */
  2896. if (dwc2_host_is_b_hnp_enabled(hsotg)) {
  2897. pcgctl = dwc2_readl(hsotg, PCGCTL);
  2898. pcgctl &= ~PCGCTL_STOPPCLK;
  2899. dwc2_writel(hsotg, pcgctl, PCGCTL);
  2900. spin_unlock_irqrestore(&hsotg->lock, flags);
  2901. msleep(200);
  2902. } else {
  2903. spin_unlock_irqrestore(&hsotg->lock, flags);
  2904. }
  2905. return ret;
  2906. }
  2907. /**
  2908. * dwc2_port_resume() - Exit controller from suspend mode for host.
  2909. *
  2910. * @hsotg: Programming view of the DWC_otg controller
  2911. *
  2912. * Return: non-zero if failed to exit suspend mode for host.
  2913. *
  2914. * This function is for exiting Host mode suspend.
  2915. * Must NOT be called with interrupt disabled or spinlock held.
  2916. */
  2917. int dwc2_port_resume(struct dwc2_hsotg *hsotg)
  2918. {
  2919. unsigned long flags;
  2920. int ret = 0;
  2921. spin_lock_irqsave(&hsotg->lock, flags);
  2922. switch (hsotg->params.power_down) {
  2923. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  2924. ret = dwc2_exit_partial_power_down(hsotg, 0, true);
  2925. if (ret)
  2926. dev_err(hsotg->dev,
  2927. "exit partial_power_down failed.\n");
  2928. break;
  2929. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  2930. /* Exit host hibernation. */
  2931. ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
  2932. if (ret)
  2933. dev_err(hsotg->dev, "exit hibernation failed.\n");
  2934. break;
  2935. case DWC2_POWER_DOWN_PARAM_NONE:
  2936. /*
  2937. * If not hibernation nor partial power down are supported,
  2938. * port resume is done using the clock gating programming flow.
  2939. */
  2940. spin_unlock_irqrestore(&hsotg->lock, flags);
  2941. dwc2_host_exit_clock_gating(hsotg, 0);
  2942. spin_lock_irqsave(&hsotg->lock, flags);
  2943. break;
  2944. }
  2945. spin_unlock_irqrestore(&hsotg->lock, flags);
  2946. return ret;
  2947. }
  2948. /* Handles hub class-specific requests */
  2949. static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
  2950. u16 wvalue, u16 windex, char *buf, u16 wlength)
  2951. {
  2952. struct usb_hub_descriptor *hub_desc;
  2953. int retval = 0;
  2954. u32 hprt0;
  2955. u32 port_status;
  2956. u32 speed;
  2957. u32 pcgctl;
  2958. u32 pwr;
  2959. switch (typereq) {
  2960. case ClearHubFeature:
  2961. dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
  2962. switch (wvalue) {
  2963. case C_HUB_LOCAL_POWER:
  2964. case C_HUB_OVER_CURRENT:
  2965. /* Nothing required here */
  2966. break;
  2967. default:
  2968. retval = -EINVAL;
  2969. dev_err(hsotg->dev,
  2970. "ClearHubFeature request %1xh unknown\n",
  2971. wvalue);
  2972. }
  2973. break;
  2974. case ClearPortFeature:
  2975. if (wvalue != USB_PORT_FEAT_L1)
  2976. if (!windex || windex > 1)
  2977. goto error;
  2978. switch (wvalue) {
  2979. case USB_PORT_FEAT_ENABLE:
  2980. dev_dbg(hsotg->dev,
  2981. "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  2982. hprt0 = dwc2_read_hprt0(hsotg);
  2983. hprt0 |= HPRT0_ENA;
  2984. dwc2_writel(hsotg, hprt0, HPRT0);
  2985. break;
  2986. case USB_PORT_FEAT_SUSPEND:
  2987. dev_dbg(hsotg->dev,
  2988. "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  2989. if (hsotg->bus_suspended)
  2990. retval = dwc2_port_resume(hsotg);
  2991. break;
  2992. case USB_PORT_FEAT_POWER:
  2993. dev_dbg(hsotg->dev,
  2994. "ClearPortFeature USB_PORT_FEAT_POWER\n");
  2995. hprt0 = dwc2_read_hprt0(hsotg);
  2996. pwr = hprt0 & HPRT0_PWR;
  2997. hprt0 &= ~HPRT0_PWR;
  2998. dwc2_writel(hsotg, hprt0, HPRT0);
  2999. if (pwr)
  3000. dwc2_vbus_supply_exit(hsotg);
  3001. break;
  3002. case USB_PORT_FEAT_INDICATOR:
  3003. dev_dbg(hsotg->dev,
  3004. "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  3005. /* Port indicator not supported */
  3006. break;
  3007. case USB_PORT_FEAT_C_CONNECTION:
  3008. /*
  3009. * Clears driver's internal Connect Status Change flag
  3010. */
  3011. dev_dbg(hsotg->dev,
  3012. "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  3013. hsotg->flags.b.port_connect_status_change = 0;
  3014. break;
  3015. case USB_PORT_FEAT_C_RESET:
  3016. /* Clears driver's internal Port Reset Change flag */
  3017. dev_dbg(hsotg->dev,
  3018. "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  3019. hsotg->flags.b.port_reset_change = 0;
  3020. break;
  3021. case USB_PORT_FEAT_C_ENABLE:
  3022. /*
  3023. * Clears the driver's internal Port Enable/Disable
  3024. * Change flag
  3025. */
  3026. dev_dbg(hsotg->dev,
  3027. "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  3028. hsotg->flags.b.port_enable_change = 0;
  3029. break;
  3030. case USB_PORT_FEAT_C_SUSPEND:
  3031. /*
  3032. * Clears the driver's internal Port Suspend Change
  3033. * flag, which is set when resume signaling on the host
  3034. * port is complete
  3035. */
  3036. dev_dbg(hsotg->dev,
  3037. "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  3038. hsotg->flags.b.port_suspend_change = 0;
  3039. break;
  3040. case USB_PORT_FEAT_C_PORT_L1:
  3041. dev_dbg(hsotg->dev,
  3042. "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
  3043. hsotg->flags.b.port_l1_change = 0;
  3044. break;
  3045. case USB_PORT_FEAT_C_OVER_CURRENT:
  3046. dev_dbg(hsotg->dev,
  3047. "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  3048. hsotg->flags.b.port_over_current_change = 0;
  3049. break;
  3050. default:
  3051. retval = -EINVAL;
  3052. dev_err(hsotg->dev,
  3053. "ClearPortFeature request %1xh unknown or unsupported\n",
  3054. wvalue);
  3055. }
  3056. break;
  3057. case GetHubDescriptor:
  3058. dev_dbg(hsotg->dev, "GetHubDescriptor\n");
  3059. hub_desc = (struct usb_hub_descriptor *)buf;
  3060. hub_desc->bDescLength = 9;
  3061. hub_desc->bDescriptorType = USB_DT_HUB;
  3062. hub_desc->bNbrPorts = 1;
  3063. hub_desc->wHubCharacteristics =
  3064. cpu_to_le16(HUB_CHAR_COMMON_LPSM |
  3065. HUB_CHAR_INDV_PORT_OCPM);
  3066. hub_desc->bPwrOn2PwrGood = 1;
  3067. hub_desc->bHubContrCurrent = 0;
  3068. hub_desc->u.hs.DeviceRemovable[0] = 0;
  3069. hub_desc->u.hs.DeviceRemovable[1] = 0xff;
  3070. break;
  3071. case GetHubStatus:
  3072. dev_dbg(hsotg->dev, "GetHubStatus\n");
  3073. memset(buf, 0, 4);
  3074. break;
  3075. case GetPortStatus:
  3076. dev_vdbg(hsotg->dev,
  3077. "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
  3078. hsotg->flags.d32);
  3079. if (!windex || windex > 1)
  3080. goto error;
  3081. port_status = 0;
  3082. if (hsotg->flags.b.port_connect_status_change)
  3083. port_status |= USB_PORT_STAT_C_CONNECTION << 16;
  3084. if (hsotg->flags.b.port_enable_change)
  3085. port_status |= USB_PORT_STAT_C_ENABLE << 16;
  3086. if (hsotg->flags.b.port_suspend_change)
  3087. port_status |= USB_PORT_STAT_C_SUSPEND << 16;
  3088. if (hsotg->flags.b.port_l1_change)
  3089. port_status |= USB_PORT_STAT_C_L1 << 16;
  3090. if (hsotg->flags.b.port_reset_change)
  3091. port_status |= USB_PORT_STAT_C_RESET << 16;
  3092. if (hsotg->flags.b.port_over_current_change) {
  3093. dev_warn(hsotg->dev, "Overcurrent change detected\n");
  3094. port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  3095. }
  3096. if (dwc2_is_device_mode(hsotg)) {
  3097. /*
  3098. * Just return 0's for the remainder of the port status
  3099. * since the port register can't be read if the core
  3100. * is in device mode.
  3101. */
  3102. *(__le32 *)buf = cpu_to_le32(port_status);
  3103. break;
  3104. }
  3105. hprt0 = dwc2_readl(hsotg, HPRT0);
  3106. dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
  3107. if (hprt0 & HPRT0_CONNSTS)
  3108. port_status |= USB_PORT_STAT_CONNECTION;
  3109. if (hprt0 & HPRT0_ENA)
  3110. port_status |= USB_PORT_STAT_ENABLE;
  3111. if (hprt0 & HPRT0_SUSP)
  3112. port_status |= USB_PORT_STAT_SUSPEND;
  3113. if (hprt0 & HPRT0_OVRCURRACT)
  3114. port_status |= USB_PORT_STAT_OVERCURRENT;
  3115. if (hprt0 & HPRT0_RST)
  3116. port_status |= USB_PORT_STAT_RESET;
  3117. if (hprt0 & HPRT0_PWR)
  3118. port_status |= USB_PORT_STAT_POWER;
  3119. speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  3120. if (speed == HPRT0_SPD_HIGH_SPEED)
  3121. port_status |= USB_PORT_STAT_HIGH_SPEED;
  3122. else if (speed == HPRT0_SPD_LOW_SPEED)
  3123. port_status |= USB_PORT_STAT_LOW_SPEED;
  3124. if (hprt0 & HPRT0_TSTCTL_MASK)
  3125. port_status |= USB_PORT_STAT_TEST;
  3126. /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  3127. if (hsotg->params.dma_desc_fs_enable) {
  3128. /*
  3129. * Enable descriptor DMA only if a full speed
  3130. * device is connected.
  3131. */
  3132. if (hsotg->new_connection &&
  3133. ((port_status &
  3134. (USB_PORT_STAT_CONNECTION |
  3135. USB_PORT_STAT_HIGH_SPEED |
  3136. USB_PORT_STAT_LOW_SPEED)) ==
  3137. USB_PORT_STAT_CONNECTION)) {
  3138. u32 hcfg;
  3139. dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
  3140. hsotg->params.dma_desc_enable = true;
  3141. hcfg = dwc2_readl(hsotg, HCFG);
  3142. hcfg |= HCFG_DESCDMA;
  3143. dwc2_writel(hsotg, hcfg, HCFG);
  3144. hsotg->new_connection = false;
  3145. }
  3146. }
  3147. dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
  3148. *(__le32 *)buf = cpu_to_le32(port_status);
  3149. break;
  3150. case SetHubFeature:
  3151. dev_dbg(hsotg->dev, "SetHubFeature\n");
  3152. /* No HUB features supported */
  3153. break;
  3154. case SetPortFeature:
  3155. dev_dbg(hsotg->dev, "SetPortFeature\n");
  3156. if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
  3157. goto error;
  3158. if (dwc2_is_device_mode(hsotg)) {
  3159. /*
  3160. * Just return 0's for the remainder of the port status
  3161. * since the port register can't be read if the core
  3162. * is in device mode.
  3163. */
  3164. break;
  3165. }
  3166. switch (wvalue) {
  3167. case USB_PORT_FEAT_SUSPEND:
  3168. dev_dbg(hsotg->dev,
  3169. "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  3170. if (windex != hsotg->otg_port)
  3171. goto error;
  3172. if (!hsotg->bus_suspended)
  3173. retval = dwc2_port_suspend(hsotg, windex);
  3174. break;
  3175. case USB_PORT_FEAT_POWER:
  3176. dev_dbg(hsotg->dev,
  3177. "SetPortFeature - USB_PORT_FEAT_POWER\n");
  3178. hprt0 = dwc2_read_hprt0(hsotg);
  3179. pwr = hprt0 & HPRT0_PWR;
  3180. hprt0 |= HPRT0_PWR;
  3181. dwc2_writel(hsotg, hprt0, HPRT0);
  3182. if (!pwr)
  3183. dwc2_vbus_supply_init(hsotg);
  3184. break;
  3185. case USB_PORT_FEAT_RESET:
  3186. dev_dbg(hsotg->dev,
  3187. "SetPortFeature - USB_PORT_FEAT_RESET\n");
  3188. hprt0 = dwc2_read_hprt0(hsotg);
  3189. if (hsotg->hibernated) {
  3190. retval = dwc2_exit_hibernation(hsotg, 0, 1, 1);
  3191. if (retval)
  3192. dev_err(hsotg->dev,
  3193. "exit hibernation failed\n");
  3194. }
  3195. if (hsotg->in_ppd) {
  3196. retval = dwc2_exit_partial_power_down(hsotg, 1,
  3197. true);
  3198. if (retval)
  3199. dev_err(hsotg->dev,
  3200. "exit partial_power_down failed\n");
  3201. }
  3202. if (hsotg->params.power_down ==
  3203. DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended)
  3204. dwc2_host_exit_clock_gating(hsotg, 0);
  3205. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3206. pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
  3207. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3208. /* ??? Original driver does this */
  3209. dwc2_writel(hsotg, 0, PCGCTL);
  3210. hprt0 = dwc2_read_hprt0(hsotg);
  3211. pwr = hprt0 & HPRT0_PWR;
  3212. /* Clear suspend bit if resetting from suspend state */
  3213. hprt0 &= ~HPRT0_SUSP;
  3214. /*
  3215. * When B-Host the Port reset bit is set in the Start
  3216. * HCD Callback function, so that the reset is started
  3217. * within 1ms of the HNP success interrupt
  3218. */
  3219. if (!dwc2_hcd_is_b_host(hsotg)) {
  3220. hprt0 |= HPRT0_PWR | HPRT0_RST;
  3221. dev_dbg(hsotg->dev,
  3222. "In host mode, hprt0=%08x\n", hprt0);
  3223. dwc2_writel(hsotg, hprt0, HPRT0);
  3224. if (!pwr)
  3225. dwc2_vbus_supply_init(hsotg);
  3226. }
  3227. /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  3228. msleep(50);
  3229. hprt0 &= ~HPRT0_RST;
  3230. dwc2_writel(hsotg, hprt0, HPRT0);
  3231. hsotg->lx_state = DWC2_L0; /* Now back to On state */
  3232. break;
  3233. case USB_PORT_FEAT_INDICATOR:
  3234. dev_dbg(hsotg->dev,
  3235. "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  3236. /* Not supported */
  3237. break;
  3238. case USB_PORT_FEAT_TEST:
  3239. hprt0 = dwc2_read_hprt0(hsotg);
  3240. dev_dbg(hsotg->dev,
  3241. "SetPortFeature - USB_PORT_FEAT_TEST\n");
  3242. hprt0 &= ~HPRT0_TSTCTL_MASK;
  3243. hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
  3244. dwc2_writel(hsotg, hprt0, HPRT0);
  3245. break;
  3246. default:
  3247. retval = -EINVAL;
  3248. dev_err(hsotg->dev,
  3249. "SetPortFeature %1xh unknown or unsupported\n",
  3250. wvalue);
  3251. break;
  3252. }
  3253. break;
  3254. default:
  3255. error:
  3256. retval = -EINVAL;
  3257. dev_dbg(hsotg->dev,
  3258. "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
  3259. typereq, windex, wvalue);
  3260. break;
  3261. }
  3262. return retval;
  3263. }
  3264. static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
  3265. {
  3266. int retval;
  3267. if (port != 1)
  3268. return -EINVAL;
  3269. retval = (hsotg->flags.b.port_connect_status_change ||
  3270. hsotg->flags.b.port_reset_change ||
  3271. hsotg->flags.b.port_enable_change ||
  3272. hsotg->flags.b.port_suspend_change ||
  3273. hsotg->flags.b.port_over_current_change);
  3274. if (retval) {
  3275. dev_dbg(hsotg->dev,
  3276. "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
  3277. dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
  3278. hsotg->flags.b.port_connect_status_change);
  3279. dev_dbg(hsotg->dev, " port_reset_change: %d\n",
  3280. hsotg->flags.b.port_reset_change);
  3281. dev_dbg(hsotg->dev, " port_enable_change: %d\n",
  3282. hsotg->flags.b.port_enable_change);
  3283. dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
  3284. hsotg->flags.b.port_suspend_change);
  3285. dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
  3286. hsotg->flags.b.port_over_current_change);
  3287. }
  3288. return retval;
  3289. }
  3290. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  3291. {
  3292. u32 hfnum = dwc2_readl(hsotg, HFNUM);
  3293. #ifdef DWC2_DEBUG_SOF
  3294. dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
  3295. (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
  3296. #endif
  3297. return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  3298. }
  3299. int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
  3300. {
  3301. u32 hprt = dwc2_readl(hsotg, HPRT0);
  3302. u32 hfir = dwc2_readl(hsotg, HFIR);
  3303. u32 hfnum = dwc2_readl(hsotg, HFNUM);
  3304. unsigned int us_per_frame;
  3305. unsigned int frame_number;
  3306. unsigned int remaining;
  3307. unsigned int interval;
  3308. unsigned int phy_clks;
  3309. /* High speed has 125 us per (micro) frame; others are 1 ms per */
  3310. us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
  3311. /* Extract fields */
  3312. frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  3313. remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
  3314. interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
  3315. /*
  3316. * Number of phy clocks since the last tick of the frame number after
  3317. * "us" has passed.
  3318. */
  3319. phy_clks = (interval - remaining) +
  3320. DIV_ROUND_UP(interval * us, us_per_frame);
  3321. return dwc2_frame_num_inc(frame_number, phy_clks / interval);
  3322. }
  3323. int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
  3324. {
  3325. return hsotg->op_state == OTG_STATE_B_HOST;
  3326. }
  3327. static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
  3328. int iso_desc_count,
  3329. gfp_t mem_flags)
  3330. {
  3331. struct dwc2_hcd_urb *urb;
  3332. urb = kzalloc(struct_size(urb, iso_descs, iso_desc_count), mem_flags);
  3333. if (urb)
  3334. urb->packet_count = iso_desc_count;
  3335. return urb;
  3336. }
  3337. static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
  3338. struct dwc2_hcd_urb *urb, u8 dev_addr,
  3339. u8 ep_num, u8 ep_type, u8 ep_dir,
  3340. u16 maxp, u16 maxp_mult)
  3341. {
  3342. if (dbg_perio() ||
  3343. ep_type == USB_ENDPOINT_XFER_BULK ||
  3344. ep_type == USB_ENDPOINT_XFER_CONTROL)
  3345. dev_vdbg(hsotg->dev,
  3346. "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
  3347. dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
  3348. urb->pipe_info.dev_addr = dev_addr;
  3349. urb->pipe_info.ep_num = ep_num;
  3350. urb->pipe_info.pipe_type = ep_type;
  3351. urb->pipe_info.pipe_dir = ep_dir;
  3352. urb->pipe_info.maxp = maxp;
  3353. urb->pipe_info.maxp_mult = maxp_mult;
  3354. }
  3355. /*
  3356. * NOTE: This function will be removed once the peripheral controller code
  3357. * is integrated and the driver is stable
  3358. */
  3359. void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
  3360. {
  3361. #ifdef DEBUG
  3362. struct dwc2_host_chan *chan;
  3363. struct dwc2_hcd_urb *urb;
  3364. struct dwc2_qtd *qtd;
  3365. int num_channels;
  3366. u32 np_tx_status;
  3367. u32 p_tx_status;
  3368. int i;
  3369. num_channels = hsotg->params.host_channels;
  3370. dev_dbg(hsotg->dev, "\n");
  3371. dev_dbg(hsotg->dev,
  3372. "************************************************************\n");
  3373. dev_dbg(hsotg->dev, "HCD State:\n");
  3374. dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
  3375. for (i = 0; i < num_channels; i++) {
  3376. chan = hsotg->hc_ptr_array[i];
  3377. dev_dbg(hsotg->dev, " Channel %d:\n", i);
  3378. dev_dbg(hsotg->dev,
  3379. " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  3380. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  3381. dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
  3382. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  3383. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  3384. dev_dbg(hsotg->dev, " data_pid_start: %d\n",
  3385. chan->data_pid_start);
  3386. dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
  3387. dev_dbg(hsotg->dev, " xfer_started: %d\n",
  3388. chan->xfer_started);
  3389. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  3390. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  3391. (unsigned long)chan->xfer_dma);
  3392. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  3393. dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
  3394. dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
  3395. chan->halt_on_queue);
  3396. dev_dbg(hsotg->dev, " halt_pending: %d\n",
  3397. chan->halt_pending);
  3398. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  3399. dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
  3400. dev_dbg(hsotg->dev, " complete_split: %d\n",
  3401. chan->complete_split);
  3402. dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
  3403. dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
  3404. dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
  3405. dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
  3406. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  3407. if (chan->xfer_started) {
  3408. u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
  3409. hfnum = dwc2_readl(hsotg, HFNUM);
  3410. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  3411. hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
  3412. hcint = dwc2_readl(hsotg, HCINT(i));
  3413. hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
  3414. dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
  3415. dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
  3416. dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
  3417. dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
  3418. dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
  3419. }
  3420. if (!(chan->xfer_started && chan->qh))
  3421. continue;
  3422. list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
  3423. if (!qtd->in_process)
  3424. break;
  3425. urb = qtd->urb;
  3426. dev_dbg(hsotg->dev, " URB Info:\n");
  3427. dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
  3428. qtd, urb);
  3429. if (urb) {
  3430. dev_dbg(hsotg->dev,
  3431. " Dev: %d, EP: %d %s\n",
  3432. dwc2_hcd_get_dev_addr(&urb->pipe_info),
  3433. dwc2_hcd_get_ep_num(&urb->pipe_info),
  3434. dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
  3435. "IN" : "OUT");
  3436. dev_dbg(hsotg->dev,
  3437. " Max packet size: %d (%d mult)\n",
  3438. dwc2_hcd_get_maxp(&urb->pipe_info),
  3439. dwc2_hcd_get_maxp_mult(&urb->pipe_info));
  3440. dev_dbg(hsotg->dev,
  3441. " transfer_buffer: %p\n",
  3442. urb->buf);
  3443. dev_dbg(hsotg->dev,
  3444. " transfer_dma: %08lx\n",
  3445. (unsigned long)urb->dma);
  3446. dev_dbg(hsotg->dev,
  3447. " transfer_buffer_length: %d\n",
  3448. urb->length);
  3449. dev_dbg(hsotg->dev, " actual_length: %d\n",
  3450. urb->actual_length);
  3451. }
  3452. }
  3453. }
  3454. dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
  3455. hsotg->non_periodic_channels);
  3456. dev_dbg(hsotg->dev, " periodic_channels: %d\n",
  3457. hsotg->periodic_channels);
  3458. dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
  3459. np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
  3460. dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
  3461. (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  3462. dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
  3463. (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  3464. p_tx_status = dwc2_readl(hsotg, HPTXSTS);
  3465. dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
  3466. (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  3467. dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
  3468. (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  3469. dwc2_dump_global_registers(hsotg);
  3470. dwc2_dump_host_registers(hsotg);
  3471. dev_dbg(hsotg->dev,
  3472. "************************************************************\n");
  3473. dev_dbg(hsotg->dev, "\n");
  3474. #endif
  3475. }
  3476. struct wrapper_priv_data {
  3477. struct dwc2_hsotg *hsotg;
  3478. };
  3479. /* Gets the dwc2_hsotg from a usb_hcd */
  3480. static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
  3481. {
  3482. struct wrapper_priv_data *p;
  3483. p = (struct wrapper_priv_data *)&hcd->hcd_priv;
  3484. return p->hsotg;
  3485. }
  3486. /**
  3487. * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
  3488. *
  3489. * This will get the dwc2_tt structure (and ttport) associated with the given
  3490. * context (which is really just a struct urb pointer).
  3491. *
  3492. * The first time this is called for a given TT we allocate memory for our
  3493. * structure. When everyone is done and has called dwc2_host_put_tt_info()
  3494. * then the refcount for the structure will go to 0 and we'll free it.
  3495. *
  3496. * @hsotg: The HCD state structure for the DWC OTG controller.
  3497. * @context: The priv pointer from a struct dwc2_hcd_urb.
  3498. * @mem_flags: Flags for allocating memory.
  3499. * @ttport: We'll return this device's port number here. That's used to
  3500. * reference into the bitmap if we're on a multi_tt hub.
  3501. *
  3502. * Return: a pointer to a struct dwc2_tt. Don't forget to call
  3503. * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
  3504. */
  3505. struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
  3506. gfp_t mem_flags, int *ttport)
  3507. {
  3508. struct urb *urb = context;
  3509. struct dwc2_tt *dwc_tt = NULL;
  3510. if (urb->dev->tt) {
  3511. *ttport = urb->dev->ttport;
  3512. dwc_tt = urb->dev->tt->hcpriv;
  3513. if (!dwc_tt) {
  3514. size_t bitmap_size;
  3515. /*
  3516. * For single_tt we need one schedule. For multi_tt
  3517. * we need one per port.
  3518. */
  3519. bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
  3520. sizeof(dwc_tt->periodic_bitmaps[0]);
  3521. if (urb->dev->tt->multi)
  3522. bitmap_size *= urb->dev->tt->hub->maxchild;
  3523. dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
  3524. mem_flags);
  3525. if (!dwc_tt)
  3526. return NULL;
  3527. dwc_tt->usb_tt = urb->dev->tt;
  3528. dwc_tt->usb_tt->hcpriv = dwc_tt;
  3529. }
  3530. dwc_tt->refcount++;
  3531. }
  3532. return dwc_tt;
  3533. }
  3534. /**
  3535. * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
  3536. *
  3537. * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
  3538. * of the structure are done.
  3539. *
  3540. * It's OK to call this with NULL.
  3541. *
  3542. * @hsotg: The HCD state structure for the DWC OTG controller.
  3543. * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
  3544. */
  3545. void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
  3546. {
  3547. /* Model kfree and make put of NULL a no-op */
  3548. if (!dwc_tt)
  3549. return;
  3550. WARN_ON(dwc_tt->refcount < 1);
  3551. dwc_tt->refcount--;
  3552. if (!dwc_tt->refcount) {
  3553. dwc_tt->usb_tt->hcpriv = NULL;
  3554. kfree(dwc_tt);
  3555. }
  3556. }
  3557. int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
  3558. {
  3559. struct urb *urb = context;
  3560. return urb->dev->speed;
  3561. }
  3562. static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  3563. struct urb *urb)
  3564. {
  3565. struct usb_bus *bus = hcd_to_bus(hcd);
  3566. if (urb->interval)
  3567. bus->bandwidth_allocated += bw / urb->interval;
  3568. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  3569. bus->bandwidth_isoc_reqs++;
  3570. else
  3571. bus->bandwidth_int_reqs++;
  3572. }
  3573. static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  3574. struct urb *urb)
  3575. {
  3576. struct usb_bus *bus = hcd_to_bus(hcd);
  3577. if (urb->interval)
  3578. bus->bandwidth_allocated -= bw / urb->interval;
  3579. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  3580. bus->bandwidth_isoc_reqs--;
  3581. else
  3582. bus->bandwidth_int_reqs--;
  3583. }
  3584. /*
  3585. * Sets the final status of an URB and returns it to the upper layer. Any
  3586. * required cleanup of the URB is performed.
  3587. *
  3588. * Must be called with interrupt disabled and spinlock held
  3589. */
  3590. void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  3591. int status)
  3592. {
  3593. struct urb *urb;
  3594. int i;
  3595. if (!qtd) {
  3596. dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
  3597. return;
  3598. }
  3599. if (!qtd->urb) {
  3600. dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
  3601. return;
  3602. }
  3603. urb = qtd->urb->priv;
  3604. if (!urb) {
  3605. dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
  3606. return;
  3607. }
  3608. urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
  3609. if (dbg_urb(urb))
  3610. dev_vdbg(hsotg->dev,
  3611. "%s: urb %p device %d ep %d-%s status %d actual %d\n",
  3612. __func__, urb, usb_pipedevice(urb->pipe),
  3613. usb_pipeendpoint(urb->pipe),
  3614. usb_pipein(urb->pipe) ? "IN" : "OUT", status,
  3615. urb->actual_length);
  3616. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  3617. if (!hsotg->params.dma_desc_enable)
  3618. urb->start_frame = qtd->qh->start_active_frame;
  3619. urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
  3620. for (i = 0; i < urb->number_of_packets; ++i) {
  3621. urb->iso_frame_desc[i].actual_length =
  3622. dwc2_hcd_urb_get_iso_desc_actual_length(
  3623. qtd->urb, i);
  3624. urb->iso_frame_desc[i].status =
  3625. dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
  3626. }
  3627. }
  3628. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
  3629. for (i = 0; i < urb->number_of_packets; i++)
  3630. dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
  3631. i, urb->iso_frame_desc[i].status);
  3632. }
  3633. urb->status = status;
  3634. if (!status) {
  3635. if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  3636. urb->actual_length < urb->transfer_buffer_length)
  3637. urb->status = -EREMOTEIO;
  3638. }
  3639. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  3640. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  3641. struct usb_host_endpoint *ep = urb->ep;
  3642. if (ep)
  3643. dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
  3644. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  3645. urb);
  3646. }
  3647. usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
  3648. urb->hcpriv = NULL;
  3649. kfree(qtd->urb);
  3650. qtd->urb = NULL;
  3651. usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
  3652. }
  3653. /*
  3654. * Work queue function for starting the HCD when A-Cable is connected
  3655. */
  3656. static void dwc2_hcd_start_func(struct work_struct *work)
  3657. {
  3658. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3659. start_work.work);
  3660. dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
  3661. dwc2_host_start(hsotg);
  3662. }
  3663. /*
  3664. * Reset work queue function
  3665. */
  3666. static void dwc2_hcd_reset_func(struct work_struct *work)
  3667. {
  3668. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3669. reset_work.work);
  3670. unsigned long flags;
  3671. u32 hprt0;
  3672. dev_dbg(hsotg->dev, "USB RESET function called\n");
  3673. spin_lock_irqsave(&hsotg->lock, flags);
  3674. hprt0 = dwc2_read_hprt0(hsotg);
  3675. hprt0 &= ~HPRT0_RST;
  3676. dwc2_writel(hsotg, hprt0, HPRT0);
  3677. hsotg->flags.b.port_reset_change = 1;
  3678. spin_unlock_irqrestore(&hsotg->lock, flags);
  3679. }
  3680. static void dwc2_hcd_phy_reset_func(struct work_struct *work)
  3681. {
  3682. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3683. phy_reset_work);
  3684. int ret;
  3685. ret = phy_reset(hsotg->phy);
  3686. if (ret)
  3687. dev_warn(hsotg->dev, "PHY reset failed\n");
  3688. }
  3689. /*
  3690. * =========================================================================
  3691. * Linux HC Driver Functions
  3692. * =========================================================================
  3693. */
  3694. /*
  3695. * Initializes the DWC_otg controller and its root hub and prepares it for host
  3696. * mode operation. Activates the root port. Returns 0 on success and a negative
  3697. * error code on failure.
  3698. */
  3699. static int _dwc2_hcd_start(struct usb_hcd *hcd)
  3700. {
  3701. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3702. struct usb_bus *bus = hcd_to_bus(hcd);
  3703. unsigned long flags;
  3704. u32 hprt0;
  3705. int ret;
  3706. dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
  3707. spin_lock_irqsave(&hsotg->lock, flags);
  3708. hsotg->lx_state = DWC2_L0;
  3709. hcd->state = HC_STATE_RUNNING;
  3710. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3711. if (dwc2_is_device_mode(hsotg)) {
  3712. spin_unlock_irqrestore(&hsotg->lock, flags);
  3713. return 0; /* why 0 ?? */
  3714. }
  3715. dwc2_hcd_reinit(hsotg);
  3716. hprt0 = dwc2_read_hprt0(hsotg);
  3717. /* Has vbus power been turned on in dwc2_core_host_init ? */
  3718. if (hprt0 & HPRT0_PWR) {
  3719. /* Enable external vbus supply before resuming root hub */
  3720. spin_unlock_irqrestore(&hsotg->lock, flags);
  3721. ret = dwc2_vbus_supply_init(hsotg);
  3722. if (ret)
  3723. return ret;
  3724. spin_lock_irqsave(&hsotg->lock, flags);
  3725. }
  3726. /* Initialize and connect root hub if one is not already attached */
  3727. if (bus->root_hub) {
  3728. dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
  3729. /* Inform the HUB driver to resume */
  3730. usb_hcd_resume_root_hub(hcd);
  3731. }
  3732. spin_unlock_irqrestore(&hsotg->lock, flags);
  3733. return 0;
  3734. }
  3735. /*
  3736. * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  3737. * stopped.
  3738. */
  3739. static void _dwc2_hcd_stop(struct usb_hcd *hcd)
  3740. {
  3741. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3742. unsigned long flags;
  3743. u32 hprt0;
  3744. /* Turn off all host-specific interrupts */
  3745. dwc2_disable_host_interrupts(hsotg);
  3746. /* Wait for interrupt processing to finish */
  3747. synchronize_irq(hcd->irq);
  3748. spin_lock_irqsave(&hsotg->lock, flags);
  3749. hprt0 = dwc2_read_hprt0(hsotg);
  3750. /* Ensure hcd is disconnected */
  3751. dwc2_hcd_disconnect(hsotg, true);
  3752. dwc2_hcd_stop(hsotg);
  3753. hsotg->lx_state = DWC2_L3;
  3754. hcd->state = HC_STATE_HALT;
  3755. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3756. spin_unlock_irqrestore(&hsotg->lock, flags);
  3757. /* keep balanced supply init/exit by checking HPRT0_PWR */
  3758. if (hprt0 & HPRT0_PWR)
  3759. dwc2_vbus_supply_exit(hsotg);
  3760. usleep_range(1000, 3000);
  3761. }
  3762. static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
  3763. {
  3764. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3765. unsigned long flags;
  3766. int ret = 0;
  3767. spin_lock_irqsave(&hsotg->lock, flags);
  3768. if (dwc2_is_device_mode(hsotg))
  3769. goto unlock;
  3770. if (hsotg->lx_state != DWC2_L0)
  3771. goto unlock;
  3772. if (!HCD_HW_ACCESSIBLE(hcd))
  3773. goto unlock;
  3774. if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
  3775. goto unlock;
  3776. if (hsotg->bus_suspended)
  3777. goto skip_power_saving;
  3778. if (!(dwc2_read_hprt0(hsotg) & HPRT0_CONNSTS))
  3779. goto skip_power_saving;
  3780. switch (hsotg->params.power_down) {
  3781. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  3782. /* Enter partial_power_down */
  3783. ret = dwc2_enter_partial_power_down(hsotg);
  3784. if (ret)
  3785. dev_err(hsotg->dev,
  3786. "enter partial_power_down failed\n");
  3787. /* After entering suspend, hardware is not accessible */
  3788. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3789. break;
  3790. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  3791. /* Enter hibernation */
  3792. spin_unlock_irqrestore(&hsotg->lock, flags);
  3793. ret = dwc2_enter_hibernation(hsotg, 1);
  3794. if (ret)
  3795. dev_err(hsotg->dev, "enter hibernation failed\n");
  3796. spin_lock_irqsave(&hsotg->lock, flags);
  3797. /* After entering suspend, hardware is not accessible */
  3798. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3799. break;
  3800. case DWC2_POWER_DOWN_PARAM_NONE:
  3801. /*
  3802. * If not hibernation nor partial power down are supported,
  3803. * clock gating is used to save power.
  3804. */
  3805. if (!hsotg->params.no_clock_gating) {
  3806. dwc2_host_enter_clock_gating(hsotg);
  3807. /* After entering suspend, hardware is not accessible */
  3808. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3809. }
  3810. break;
  3811. default:
  3812. goto skip_power_saving;
  3813. }
  3814. spin_unlock_irqrestore(&hsotg->lock, flags);
  3815. dwc2_vbus_supply_exit(hsotg);
  3816. spin_lock_irqsave(&hsotg->lock, flags);
  3817. /* Ask phy to be suspended */
  3818. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  3819. spin_unlock_irqrestore(&hsotg->lock, flags);
  3820. usb_phy_set_suspend(hsotg->uphy, true);
  3821. spin_lock_irqsave(&hsotg->lock, flags);
  3822. }
  3823. skip_power_saving:
  3824. hsotg->lx_state = DWC2_L2;
  3825. unlock:
  3826. spin_unlock_irqrestore(&hsotg->lock, flags);
  3827. return ret;
  3828. }
  3829. static int _dwc2_hcd_resume(struct usb_hcd *hcd)
  3830. {
  3831. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3832. unsigned long flags;
  3833. u32 hprt0;
  3834. int ret = 0;
  3835. spin_lock_irqsave(&hsotg->lock, flags);
  3836. if (dwc2_is_device_mode(hsotg))
  3837. goto unlock;
  3838. if (hsotg->lx_state != DWC2_L2)
  3839. goto unlock;
  3840. hprt0 = dwc2_read_hprt0(hsotg);
  3841. /*
  3842. * Added port connection status checking which prevents exiting from
  3843. * Partial Power Down mode from _dwc2_hcd_resume() if not in Partial
  3844. * Power Down mode.
  3845. */
  3846. if (hprt0 & HPRT0_CONNSTS) {
  3847. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3848. hsotg->lx_state = DWC2_L0;
  3849. goto unlock;
  3850. }
  3851. switch (hsotg->params.power_down) {
  3852. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  3853. ret = dwc2_exit_partial_power_down(hsotg, 0, true);
  3854. if (ret)
  3855. dev_err(hsotg->dev,
  3856. "exit partial_power_down failed\n");
  3857. /*
  3858. * Set HW accessible bit before powering on the controller
  3859. * since an interrupt may rise.
  3860. */
  3861. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3862. break;
  3863. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  3864. ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
  3865. if (ret)
  3866. dev_err(hsotg->dev, "exit hibernation failed.\n");
  3867. /*
  3868. * Set HW accessible bit before powering on the controller
  3869. * since an interrupt may rise.
  3870. */
  3871. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3872. break;
  3873. case DWC2_POWER_DOWN_PARAM_NONE:
  3874. /*
  3875. * If not hibernation nor partial power down are supported,
  3876. * port resume is done using the clock gating programming flow.
  3877. */
  3878. spin_unlock_irqrestore(&hsotg->lock, flags);
  3879. dwc2_host_exit_clock_gating(hsotg, 0);
  3880. /*
  3881. * Initialize the Core for Host mode, as after system resume
  3882. * the global interrupts are disabled.
  3883. */
  3884. dwc2_core_init(hsotg, false);
  3885. dwc2_enable_global_interrupts(hsotg);
  3886. dwc2_hcd_reinit(hsotg);
  3887. spin_lock_irqsave(&hsotg->lock, flags);
  3888. /*
  3889. * Set HW accessible bit before powering on the controller
  3890. * since an interrupt may rise.
  3891. */
  3892. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3893. break;
  3894. default:
  3895. hsotg->lx_state = DWC2_L0;
  3896. goto unlock;
  3897. }
  3898. /* Change Root port status, as port status change occurred after resume.*/
  3899. hsotg->flags.b.port_suspend_change = 1;
  3900. /*
  3901. * Enable power if not already done.
  3902. * This must not be spinlocked since duration
  3903. * of this call is unknown.
  3904. */
  3905. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  3906. spin_unlock_irqrestore(&hsotg->lock, flags);
  3907. usb_phy_set_suspend(hsotg->uphy, false);
  3908. spin_lock_irqsave(&hsotg->lock, flags);
  3909. }
  3910. /* Enable external vbus supply after resuming the port. */
  3911. spin_unlock_irqrestore(&hsotg->lock, flags);
  3912. dwc2_vbus_supply_init(hsotg);
  3913. /* Wait for controller to correctly update D+/D- level */
  3914. usleep_range(3000, 5000);
  3915. spin_lock_irqsave(&hsotg->lock, flags);
  3916. /*
  3917. * Clear Port Enable and Port Status changes.
  3918. * Enable Port Power.
  3919. */
  3920. dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
  3921. HPRT0_ENACHG, HPRT0);
  3922. /* Wait for controller to detect Port Connect */
  3923. spin_unlock_irqrestore(&hsotg->lock, flags);
  3924. usleep_range(5000, 7000);
  3925. spin_lock_irqsave(&hsotg->lock, flags);
  3926. unlock:
  3927. spin_unlock_irqrestore(&hsotg->lock, flags);
  3928. return ret;
  3929. }
  3930. /* Returns the current frame number */
  3931. static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
  3932. {
  3933. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3934. return dwc2_hcd_get_frame_number(hsotg);
  3935. }
  3936. static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
  3937. char *fn_name)
  3938. {
  3939. #ifdef VERBOSE_DEBUG
  3940. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3941. char *pipetype = NULL;
  3942. char *speed = NULL;
  3943. dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
  3944. dev_vdbg(hsotg->dev, " Device address: %d\n",
  3945. usb_pipedevice(urb->pipe));
  3946. dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
  3947. usb_pipeendpoint(urb->pipe),
  3948. usb_pipein(urb->pipe) ? "IN" : "OUT");
  3949. switch (usb_pipetype(urb->pipe)) {
  3950. case PIPE_CONTROL:
  3951. pipetype = "CONTROL";
  3952. break;
  3953. case PIPE_BULK:
  3954. pipetype = "BULK";
  3955. break;
  3956. case PIPE_INTERRUPT:
  3957. pipetype = "INTERRUPT";
  3958. break;
  3959. case PIPE_ISOCHRONOUS:
  3960. pipetype = "ISOCHRONOUS";
  3961. break;
  3962. }
  3963. dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
  3964. usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
  3965. "IN" : "OUT");
  3966. switch (urb->dev->speed) {
  3967. case USB_SPEED_HIGH:
  3968. speed = "HIGH";
  3969. break;
  3970. case USB_SPEED_FULL:
  3971. speed = "FULL";
  3972. break;
  3973. case USB_SPEED_LOW:
  3974. speed = "LOW";
  3975. break;
  3976. default:
  3977. speed = "UNKNOWN";
  3978. break;
  3979. }
  3980. dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
  3981. dev_vdbg(hsotg->dev, " Max packet size: %d (%d mult)\n",
  3982. usb_endpoint_maxp(&urb->ep->desc),
  3983. usb_endpoint_maxp_mult(&urb->ep->desc));
  3984. dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
  3985. urb->transfer_buffer_length);
  3986. dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
  3987. urb->transfer_buffer, (unsigned long)urb->transfer_dma);
  3988. dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
  3989. urb->setup_packet, (unsigned long)urb->setup_dma);
  3990. dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
  3991. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  3992. int i;
  3993. for (i = 0; i < urb->number_of_packets; i++) {
  3994. dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
  3995. dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
  3996. urb->iso_frame_desc[i].offset,
  3997. urb->iso_frame_desc[i].length);
  3998. }
  3999. }
  4000. #endif
  4001. }
  4002. /*
  4003. * Starts processing a USB transfer request specified by a USB Request Block
  4004. * (URB). mem_flags indicates the type of memory allocation to use while
  4005. * processing this URB.
  4006. */
  4007. static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  4008. gfp_t mem_flags)
  4009. {
  4010. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4011. struct usb_host_endpoint *ep = urb->ep;
  4012. struct dwc2_hcd_urb *dwc2_urb;
  4013. int i;
  4014. int retval;
  4015. int alloc_bandwidth = 0;
  4016. u8 ep_type = 0;
  4017. u32 tflags = 0;
  4018. void *buf;
  4019. unsigned long flags;
  4020. struct dwc2_qh *qh;
  4021. bool qh_allocated = false;
  4022. struct dwc2_qtd *qtd;
  4023. struct dwc2_gregs_backup *gr;
  4024. gr = &hsotg->gr_backup;
  4025. if (dbg_urb(urb)) {
  4026. dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
  4027. dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
  4028. }
  4029. if (hsotg->hibernated) {
  4030. if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
  4031. retval = dwc2_exit_hibernation(hsotg, 0, 0, 1);
  4032. else
  4033. retval = dwc2_exit_hibernation(hsotg, 0, 0, 0);
  4034. if (retval)
  4035. dev_err(hsotg->dev,
  4036. "exit hibernation failed.\n");
  4037. }
  4038. if (hsotg->in_ppd) {
  4039. retval = dwc2_exit_partial_power_down(hsotg, 0, true);
  4040. if (retval)
  4041. dev_err(hsotg->dev,
  4042. "exit partial_power_down failed\n");
  4043. }
  4044. if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
  4045. hsotg->bus_suspended && !hsotg->params.no_clock_gating) {
  4046. if (dwc2_is_device_mode(hsotg))
  4047. dwc2_gadget_exit_clock_gating(hsotg, 0);
  4048. else
  4049. dwc2_host_exit_clock_gating(hsotg, 0);
  4050. }
  4051. if (!ep)
  4052. return -EINVAL;
  4053. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  4054. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  4055. spin_lock_irqsave(&hsotg->lock, flags);
  4056. if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
  4057. alloc_bandwidth = 1;
  4058. spin_unlock_irqrestore(&hsotg->lock, flags);
  4059. }
  4060. switch (usb_pipetype(urb->pipe)) {
  4061. case PIPE_CONTROL:
  4062. ep_type = USB_ENDPOINT_XFER_CONTROL;
  4063. break;
  4064. case PIPE_ISOCHRONOUS:
  4065. ep_type = USB_ENDPOINT_XFER_ISOC;
  4066. break;
  4067. case PIPE_BULK:
  4068. ep_type = USB_ENDPOINT_XFER_BULK;
  4069. break;
  4070. case PIPE_INTERRUPT:
  4071. ep_type = USB_ENDPOINT_XFER_INT;
  4072. break;
  4073. }
  4074. dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
  4075. mem_flags);
  4076. if (!dwc2_urb)
  4077. return -ENOMEM;
  4078. dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
  4079. usb_pipeendpoint(urb->pipe), ep_type,
  4080. usb_pipein(urb->pipe),
  4081. usb_endpoint_maxp(&ep->desc),
  4082. usb_endpoint_maxp_mult(&ep->desc));
  4083. buf = urb->transfer_buffer;
  4084. if (hcd_uses_dma(hcd)) {
  4085. if (!buf && (urb->transfer_dma & 3)) {
  4086. dev_err(hsotg->dev,
  4087. "%s: unaligned transfer with no transfer_buffer",
  4088. __func__);
  4089. retval = -EINVAL;
  4090. goto fail0;
  4091. }
  4092. }
  4093. if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  4094. tflags |= URB_GIVEBACK_ASAP;
  4095. if (urb->transfer_flags & URB_ZERO_PACKET)
  4096. tflags |= URB_SEND_ZERO_PACKET;
  4097. dwc2_urb->priv = urb;
  4098. dwc2_urb->buf = buf;
  4099. dwc2_urb->dma = urb->transfer_dma;
  4100. dwc2_urb->length = urb->transfer_buffer_length;
  4101. dwc2_urb->setup_packet = urb->setup_packet;
  4102. dwc2_urb->setup_dma = urb->setup_dma;
  4103. dwc2_urb->flags = tflags;
  4104. dwc2_urb->interval = urb->interval;
  4105. dwc2_urb->status = -EINPROGRESS;
  4106. for (i = 0; i < urb->number_of_packets; ++i)
  4107. dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
  4108. urb->iso_frame_desc[i].offset,
  4109. urb->iso_frame_desc[i].length);
  4110. urb->hcpriv = dwc2_urb;
  4111. qh = (struct dwc2_qh *)ep->hcpriv;
  4112. /* Create QH for the endpoint if it doesn't exist */
  4113. if (!qh) {
  4114. qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
  4115. if (!qh) {
  4116. retval = -ENOMEM;
  4117. goto fail0;
  4118. }
  4119. ep->hcpriv = qh;
  4120. qh_allocated = true;
  4121. }
  4122. qtd = kzalloc(sizeof(*qtd), mem_flags);
  4123. if (!qtd) {
  4124. retval = -ENOMEM;
  4125. goto fail1;
  4126. }
  4127. spin_lock_irqsave(&hsotg->lock, flags);
  4128. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  4129. if (retval)
  4130. goto fail2;
  4131. retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
  4132. if (retval)
  4133. goto fail3;
  4134. if (alloc_bandwidth) {
  4135. dwc2_allocate_bus_bandwidth(hcd,
  4136. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  4137. urb);
  4138. }
  4139. spin_unlock_irqrestore(&hsotg->lock, flags);
  4140. return 0;
  4141. fail3:
  4142. dwc2_urb->priv = NULL;
  4143. usb_hcd_unlink_urb_from_ep(hcd, urb);
  4144. if (qh_allocated && qh->channel && qh->channel->qh == qh)
  4145. qh->channel->qh = NULL;
  4146. fail2:
  4147. urb->hcpriv = NULL;
  4148. spin_unlock_irqrestore(&hsotg->lock, flags);
  4149. kfree(qtd);
  4150. fail1:
  4151. if (qh_allocated) {
  4152. struct dwc2_qtd *qtd2, *qtd2_tmp;
  4153. ep->hcpriv = NULL;
  4154. dwc2_hcd_qh_unlink(hsotg, qh);
  4155. /* Free each QTD in the QH's QTD list */
  4156. list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
  4157. qtd_list_entry)
  4158. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
  4159. dwc2_hcd_qh_free(hsotg, qh);
  4160. }
  4161. fail0:
  4162. kfree(dwc2_urb);
  4163. return retval;
  4164. }
  4165. /*
  4166. * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
  4167. */
  4168. static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  4169. int status)
  4170. {
  4171. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4172. int rc;
  4173. unsigned long flags;
  4174. dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
  4175. dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
  4176. spin_lock_irqsave(&hsotg->lock, flags);
  4177. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  4178. if (rc)
  4179. goto out;
  4180. if (!urb->hcpriv) {
  4181. dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
  4182. goto out;
  4183. }
  4184. rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
  4185. usb_hcd_unlink_urb_from_ep(hcd, urb);
  4186. kfree(urb->hcpriv);
  4187. urb->hcpriv = NULL;
  4188. /* Higher layer software sets URB status */
  4189. spin_unlock(&hsotg->lock);
  4190. usb_hcd_giveback_urb(hcd, urb, status);
  4191. spin_lock(&hsotg->lock);
  4192. dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
  4193. dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
  4194. out:
  4195. spin_unlock_irqrestore(&hsotg->lock, flags);
  4196. return rc;
  4197. }
  4198. /*
  4199. * Frees resources in the DWC_otg controller related to a given endpoint. Also
  4200. * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  4201. * must already be dequeued.
  4202. */
  4203. static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
  4204. struct usb_host_endpoint *ep)
  4205. {
  4206. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4207. dev_dbg(hsotg->dev,
  4208. "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
  4209. ep->desc.bEndpointAddress, ep->hcpriv);
  4210. dwc2_hcd_endpoint_disable(hsotg, ep, 250);
  4211. }
  4212. /*
  4213. * Resets endpoint specific parameter values, in current version used to reset
  4214. * the data toggle (as a WA). This function can be called from usb_clear_halt
  4215. * routine.
  4216. */
  4217. static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
  4218. struct usb_host_endpoint *ep)
  4219. {
  4220. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4221. unsigned long flags;
  4222. dev_dbg(hsotg->dev,
  4223. "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
  4224. ep->desc.bEndpointAddress);
  4225. spin_lock_irqsave(&hsotg->lock, flags);
  4226. dwc2_hcd_endpoint_reset(hsotg, ep);
  4227. spin_unlock_irqrestore(&hsotg->lock, flags);
  4228. }
  4229. /*
  4230. * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  4231. * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  4232. * interrupt.
  4233. *
  4234. * This function is called by the USB core when an interrupt occurs
  4235. */
  4236. static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
  4237. {
  4238. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4239. return dwc2_handle_hcd_intr(hsotg);
  4240. }
  4241. /*
  4242. * Creates Status Change bitmap for the root hub and root port. The bitmap is
  4243. * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  4244. * is the status change indicator for the single root port. Returns 1 if either
  4245. * change indicator is 1, otherwise returns 0.
  4246. */
  4247. static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  4248. {
  4249. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4250. buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
  4251. return buf[0] != 0;
  4252. }
  4253. /* Handles hub class-specific requests */
  4254. static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
  4255. u16 windex, char *buf, u16 wlength)
  4256. {
  4257. int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
  4258. wvalue, windex, buf, wlength);
  4259. return retval;
  4260. }
  4261. /* Handles hub TT buffer clear completions */
  4262. static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
  4263. struct usb_host_endpoint *ep)
  4264. {
  4265. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4266. struct dwc2_qh *qh;
  4267. unsigned long flags;
  4268. qh = ep->hcpriv;
  4269. if (!qh)
  4270. return;
  4271. spin_lock_irqsave(&hsotg->lock, flags);
  4272. qh->tt_buffer_dirty = 0;
  4273. if (hsotg->flags.b.port_connect_status)
  4274. dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
  4275. spin_unlock_irqrestore(&hsotg->lock, flags);
  4276. }
  4277. /*
  4278. * HPRT0_SPD_HIGH_SPEED: high speed
  4279. * HPRT0_SPD_FULL_SPEED: full speed
  4280. */
  4281. static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
  4282. {
  4283. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4284. if (hsotg->params.speed == speed)
  4285. return;
  4286. hsotg->params.speed = speed;
  4287. queue_work(hsotg->wq_otg, &hsotg->wf_otg);
  4288. }
  4289. static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  4290. {
  4291. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4292. if (!hsotg->params.change_speed_quirk)
  4293. return;
  4294. /*
  4295. * On removal, set speed to default high-speed.
  4296. */
  4297. if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
  4298. udev->parent->speed < USB_SPEED_HIGH) {
  4299. dev_info(hsotg->dev, "Set speed to default high-speed\n");
  4300. dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
  4301. }
  4302. }
  4303. static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  4304. {
  4305. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4306. if (!hsotg->params.change_speed_quirk)
  4307. return 0;
  4308. if (udev->speed == USB_SPEED_HIGH) {
  4309. dev_info(hsotg->dev, "Set speed to high-speed\n");
  4310. dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
  4311. } else if ((udev->speed == USB_SPEED_FULL ||
  4312. udev->speed == USB_SPEED_LOW)) {
  4313. /*
  4314. * Change speed setting to full-speed if there's
  4315. * a full-speed or low-speed device plugged in.
  4316. */
  4317. dev_info(hsotg->dev, "Set speed to full-speed\n");
  4318. dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
  4319. }
  4320. return 0;
  4321. }
  4322. static struct hc_driver dwc2_hc_driver = {
  4323. .description = "dwc2_hsotg",
  4324. .product_desc = "DWC OTG Controller",
  4325. .hcd_priv_size = sizeof(struct wrapper_priv_data),
  4326. .irq = _dwc2_hcd_irq,
  4327. .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
  4328. .start = _dwc2_hcd_start,
  4329. .stop = _dwc2_hcd_stop,
  4330. .urb_enqueue = _dwc2_hcd_urb_enqueue,
  4331. .urb_dequeue = _dwc2_hcd_urb_dequeue,
  4332. .endpoint_disable = _dwc2_hcd_endpoint_disable,
  4333. .endpoint_reset = _dwc2_hcd_endpoint_reset,
  4334. .get_frame_number = _dwc2_hcd_get_frame_number,
  4335. .hub_status_data = _dwc2_hcd_hub_status_data,
  4336. .hub_control = _dwc2_hcd_hub_control,
  4337. .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
  4338. .bus_suspend = _dwc2_hcd_suspend,
  4339. .bus_resume = _dwc2_hcd_resume,
  4340. .map_urb_for_dma = dwc2_map_urb_for_dma,
  4341. .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
  4342. };
  4343. /*
  4344. * Frees secondary storage associated with the dwc2_hsotg structure contained
  4345. * in the struct usb_hcd field
  4346. */
  4347. static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
  4348. {
  4349. u32 ahbcfg;
  4350. u32 dctl;
  4351. int i;
  4352. dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
  4353. /* Free memory for QH/QTD lists */
  4354. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
  4355. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
  4356. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
  4357. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
  4358. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
  4359. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
  4360. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
  4361. /* Free memory for the host channels */
  4362. for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  4363. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  4364. if (chan) {
  4365. dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
  4366. i, chan);
  4367. hsotg->hc_ptr_array[i] = NULL;
  4368. kfree(chan);
  4369. }
  4370. }
  4371. if (hsotg->params.host_dma) {
  4372. if (hsotg->status_buf) {
  4373. dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
  4374. hsotg->status_buf,
  4375. hsotg->status_buf_dma);
  4376. hsotg->status_buf = NULL;
  4377. }
  4378. } else {
  4379. kfree(hsotg->status_buf);
  4380. hsotg->status_buf = NULL;
  4381. }
  4382. ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  4383. /* Disable all interrupts */
  4384. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  4385. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  4386. dwc2_writel(hsotg, 0, GINTMSK);
  4387. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
  4388. dctl = dwc2_readl(hsotg, DCTL);
  4389. dctl |= DCTL_SFTDISCON;
  4390. dwc2_writel(hsotg, dctl, DCTL);
  4391. }
  4392. if (hsotg->wq_otg) {
  4393. if (!cancel_work_sync(&hsotg->wf_otg))
  4394. flush_workqueue(hsotg->wq_otg);
  4395. destroy_workqueue(hsotg->wq_otg);
  4396. }
  4397. cancel_work_sync(&hsotg->phy_reset_work);
  4398. del_timer(&hsotg->wkp_timer);
  4399. }
  4400. static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
  4401. {
  4402. /* Turn off all host-specific interrupts */
  4403. dwc2_disable_host_interrupts(hsotg);
  4404. dwc2_hcd_free(hsotg);
  4405. }
  4406. /*
  4407. * Initializes the HCD. This function allocates memory for and initializes the
  4408. * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
  4409. * USB bus with the core and calls the hc_driver->start() function. It returns
  4410. * a negative error on failure.
  4411. */
  4412. int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
  4413. {
  4414. struct platform_device *pdev = to_platform_device(hsotg->dev);
  4415. struct resource *res;
  4416. struct usb_hcd *hcd;
  4417. struct dwc2_host_chan *channel;
  4418. u32 hcfg;
  4419. int i, num_channels;
  4420. int retval;
  4421. if (usb_disabled())
  4422. return -ENODEV;
  4423. dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
  4424. retval = -ENOMEM;
  4425. hcfg = dwc2_readl(hsotg, HCFG);
  4426. dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
  4427. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4428. hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
  4429. sizeof(*hsotg->frame_num_array),
  4430. GFP_KERNEL);
  4431. if (!hsotg->frame_num_array)
  4432. goto error1;
  4433. hsotg->last_frame_num_array =
  4434. kcalloc(FRAME_NUM_ARRAY_SIZE,
  4435. sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
  4436. if (!hsotg->last_frame_num_array)
  4437. goto error1;
  4438. #endif
  4439. hsotg->last_frame_num = HFNUM_MAX_FRNUM;
  4440. /* Check if the bus driver or platform code has setup a dma_mask */
  4441. if (hsotg->params.host_dma &&
  4442. !hsotg->dev->dma_mask) {
  4443. dev_warn(hsotg->dev,
  4444. "dma_mask not set, disabling DMA\n");
  4445. hsotg->params.host_dma = false;
  4446. hsotg->params.dma_desc_enable = false;
  4447. }
  4448. /* Set device flags indicating whether the HCD supports DMA */
  4449. if (hsotg->params.host_dma) {
  4450. if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  4451. dev_warn(hsotg->dev, "can't set DMA mask\n");
  4452. if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  4453. dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
  4454. }
  4455. if (hsotg->params.change_speed_quirk) {
  4456. dwc2_hc_driver.free_dev = dwc2_free_dev;
  4457. dwc2_hc_driver.reset_device = dwc2_reset_device;
  4458. }
  4459. if (hsotg->params.host_dma)
  4460. dwc2_hc_driver.flags |= HCD_DMA;
  4461. hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
  4462. if (!hcd)
  4463. goto error1;
  4464. hcd->has_tt = 1;
  4465. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4466. if (!res) {
  4467. retval = -EINVAL;
  4468. goto error2;
  4469. }
  4470. hcd->rsrc_start = res->start;
  4471. hcd->rsrc_len = resource_size(res);
  4472. ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
  4473. hsotg->priv = hcd;
  4474. /*
  4475. * Disable the global interrupt until all the interrupt handlers are
  4476. * installed
  4477. */
  4478. dwc2_disable_global_interrupts(hsotg);
  4479. /* Initialize the DWC_otg core, and select the Phy type */
  4480. retval = dwc2_core_init(hsotg, true);
  4481. if (retval)
  4482. goto error2;
  4483. /* Create new workqueue and init work */
  4484. retval = -ENOMEM;
  4485. hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
  4486. if (!hsotg->wq_otg) {
  4487. dev_err(hsotg->dev, "Failed to create workqueue\n");
  4488. goto error2;
  4489. }
  4490. INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
  4491. timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
  4492. /* Initialize the non-periodic schedule */
  4493. INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
  4494. INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
  4495. INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
  4496. /* Initialize the periodic schedule */
  4497. INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
  4498. INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
  4499. INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
  4500. INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
  4501. INIT_LIST_HEAD(&hsotg->split_order);
  4502. /*
  4503. * Create a host channel descriptor for each host channel implemented
  4504. * in the controller. Initialize the channel descriptor array.
  4505. */
  4506. INIT_LIST_HEAD(&hsotg->free_hc_list);
  4507. num_channels = hsotg->params.host_channels;
  4508. memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
  4509. for (i = 0; i < num_channels; i++) {
  4510. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  4511. if (!channel)
  4512. goto error3;
  4513. channel->hc_num = i;
  4514. INIT_LIST_HEAD(&channel->split_order_list_entry);
  4515. hsotg->hc_ptr_array[i] = channel;
  4516. }
  4517. /* Initialize work */
  4518. INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
  4519. INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
  4520. INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func);
  4521. /*
  4522. * Allocate space for storing data on status transactions. Normally no
  4523. * data is sent, but this space acts as a bit bucket. This must be
  4524. * done after usb_add_hcd since that function allocates the DMA buffer
  4525. * pool.
  4526. */
  4527. if (hsotg->params.host_dma)
  4528. hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
  4529. DWC2_HCD_STATUS_BUF_SIZE,
  4530. &hsotg->status_buf_dma, GFP_KERNEL);
  4531. else
  4532. hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
  4533. GFP_KERNEL);
  4534. if (!hsotg->status_buf)
  4535. goto error3;
  4536. /*
  4537. * Create kmem caches to handle descriptor buffers in descriptor
  4538. * DMA mode.
  4539. * Alignment must be set to 512 bytes.
  4540. */
  4541. if (hsotg->params.dma_desc_enable ||
  4542. hsotg->params.dma_desc_fs_enable) {
  4543. hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
  4544. sizeof(struct dwc2_dma_desc) *
  4545. MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
  4546. NULL);
  4547. if (!hsotg->desc_gen_cache) {
  4548. dev_err(hsotg->dev,
  4549. "unable to create dwc2 generic desc cache\n");
  4550. /*
  4551. * Disable descriptor dma mode since it will not be
  4552. * usable.
  4553. */
  4554. hsotg->params.dma_desc_enable = false;
  4555. hsotg->params.dma_desc_fs_enable = false;
  4556. }
  4557. hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
  4558. sizeof(struct dwc2_dma_desc) *
  4559. MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
  4560. if (!hsotg->desc_hsisoc_cache) {
  4561. dev_err(hsotg->dev,
  4562. "unable to create dwc2 hs isoc desc cache\n");
  4563. kmem_cache_destroy(hsotg->desc_gen_cache);
  4564. /*
  4565. * Disable descriptor dma mode since it will not be
  4566. * usable.
  4567. */
  4568. hsotg->params.dma_desc_enable = false;
  4569. hsotg->params.dma_desc_fs_enable = false;
  4570. }
  4571. }
  4572. if (hsotg->params.host_dma) {
  4573. /*
  4574. * Create kmem caches to handle non-aligned buffer
  4575. * in Buffer DMA mode.
  4576. */
  4577. hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
  4578. DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
  4579. SLAB_CACHE_DMA, NULL);
  4580. if (!hsotg->unaligned_cache)
  4581. dev_err(hsotg->dev,
  4582. "unable to create dwc2 unaligned cache\n");
  4583. }
  4584. hsotg->otg_port = 1;
  4585. hsotg->frame_list = NULL;
  4586. hsotg->frame_list_dma = 0;
  4587. hsotg->periodic_qh_count = 0;
  4588. /* Initiate lx_state to L3 disconnected state */
  4589. hsotg->lx_state = DWC2_L3;
  4590. hcd->self.otg_port = hsotg->otg_port;
  4591. /* Don't support SG list at this point */
  4592. hcd->self.sg_tablesize = 0;
  4593. hcd->tpl_support = of_usb_host_tpl_support(hsotg->dev->of_node);
  4594. if (!IS_ERR_OR_NULL(hsotg->uphy))
  4595. otg_set_host(hsotg->uphy->otg, &hcd->self);
  4596. /*
  4597. * Finish generic HCD initialization and start the HCD. This function
  4598. * allocates the DMA buffer pool, registers the USB bus, requests the
  4599. * IRQ line, and calls hcd_start method.
  4600. */
  4601. retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
  4602. if (retval < 0)
  4603. goto error4;
  4604. device_wakeup_enable(hcd->self.controller);
  4605. dwc2_hcd_dump_state(hsotg);
  4606. dwc2_enable_global_interrupts(hsotg);
  4607. return 0;
  4608. error4:
  4609. kmem_cache_destroy(hsotg->unaligned_cache);
  4610. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  4611. kmem_cache_destroy(hsotg->desc_gen_cache);
  4612. error3:
  4613. dwc2_hcd_release(hsotg);
  4614. error2:
  4615. usb_put_hcd(hcd);
  4616. error1:
  4617. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4618. kfree(hsotg->last_frame_num_array);
  4619. kfree(hsotg->frame_num_array);
  4620. #endif
  4621. dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
  4622. return retval;
  4623. }
  4624. /*
  4625. * Removes the HCD.
  4626. * Frees memory and resources associated with the HCD and deregisters the bus.
  4627. */
  4628. void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
  4629. {
  4630. struct usb_hcd *hcd;
  4631. dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
  4632. hcd = dwc2_hsotg_to_hcd(hsotg);
  4633. dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
  4634. if (!hcd) {
  4635. dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
  4636. __func__);
  4637. return;
  4638. }
  4639. if (!IS_ERR_OR_NULL(hsotg->uphy))
  4640. otg_set_host(hsotg->uphy->otg, NULL);
  4641. usb_remove_hcd(hcd);
  4642. hsotg->priv = NULL;
  4643. kmem_cache_destroy(hsotg->unaligned_cache);
  4644. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  4645. kmem_cache_destroy(hsotg->desc_gen_cache);
  4646. dwc2_hcd_release(hsotg);
  4647. usb_put_hcd(hcd);
  4648. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4649. kfree(hsotg->last_frame_num_array);
  4650. kfree(hsotg->frame_num_array);
  4651. #endif
  4652. }
  4653. /**
  4654. * dwc2_backup_host_registers() - Backup controller host registers.
  4655. * When suspending usb bus, registers needs to be backuped
  4656. * if controller power is disabled once suspended.
  4657. *
  4658. * @hsotg: Programming view of the DWC_otg controller
  4659. */
  4660. int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
  4661. {
  4662. struct dwc2_hregs_backup *hr;
  4663. int i;
  4664. dev_dbg(hsotg->dev, "%s\n", __func__);
  4665. /* Backup Host regs */
  4666. hr = &hsotg->hr_backup;
  4667. hr->hcfg = dwc2_readl(hsotg, HCFG);
  4668. hr->hflbaddr = dwc2_readl(hsotg, HFLBADDR);
  4669. hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
  4670. for (i = 0; i < hsotg->params.host_channels; ++i) {
  4671. hr->hcchar[i] = dwc2_readl(hsotg, HCCHAR(i));
  4672. hr->hcsplt[i] = dwc2_readl(hsotg, HCSPLT(i));
  4673. hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
  4674. hr->hctsiz[i] = dwc2_readl(hsotg, HCTSIZ(i));
  4675. hr->hcidma[i] = dwc2_readl(hsotg, HCDMA(i));
  4676. hr->hcidmab[i] = dwc2_readl(hsotg, HCDMAB(i));
  4677. }
  4678. hr->hprt0 = dwc2_read_hprt0(hsotg);
  4679. hr->hfir = dwc2_readl(hsotg, HFIR);
  4680. hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
  4681. hr->valid = true;
  4682. return 0;
  4683. }
  4684. /**
  4685. * dwc2_restore_host_registers() - Restore controller host registers.
  4686. * When resuming usb bus, device registers needs to be restored
  4687. * if controller power were disabled.
  4688. *
  4689. * @hsotg: Programming view of the DWC_otg controller
  4690. */
  4691. int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
  4692. {
  4693. struct dwc2_hregs_backup *hr;
  4694. int i;
  4695. dev_dbg(hsotg->dev, "%s\n", __func__);
  4696. /* Restore host regs */
  4697. hr = &hsotg->hr_backup;
  4698. if (!hr->valid) {
  4699. dev_err(hsotg->dev, "%s: no host registers to restore\n",
  4700. __func__);
  4701. return -EINVAL;
  4702. }
  4703. hr->valid = false;
  4704. dwc2_writel(hsotg, hr->hcfg, HCFG);
  4705. dwc2_writel(hsotg, hr->hflbaddr, HFLBADDR);
  4706. dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
  4707. for (i = 0; i < hsotg->params.host_channels; ++i) {
  4708. dwc2_writel(hsotg, hr->hcchar[i], HCCHAR(i));
  4709. dwc2_writel(hsotg, hr->hcsplt[i], HCSPLT(i));
  4710. dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
  4711. dwc2_writel(hsotg, hr->hctsiz[i], HCTSIZ(i));
  4712. dwc2_writel(hsotg, hr->hcidma[i], HCDMA(i));
  4713. dwc2_writel(hsotg, hr->hcidmab[i], HCDMAB(i));
  4714. }
  4715. dwc2_writel(hsotg, hr->hprt0, HPRT0);
  4716. dwc2_writel(hsotg, hr->hfir, HFIR);
  4717. dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
  4718. hsotg->frame_number = 0;
  4719. return 0;
  4720. }
  4721. /**
  4722. * dwc2_host_enter_hibernation() - Put controller in Hibernation.
  4723. *
  4724. * @hsotg: Programming view of the DWC_otg controller
  4725. */
  4726. int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
  4727. {
  4728. unsigned long flags;
  4729. int ret = 0;
  4730. u32 hprt0;
  4731. u32 pcgcctl;
  4732. u32 gusbcfg;
  4733. u32 gpwrdn;
  4734. dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
  4735. ret = dwc2_backup_global_registers(hsotg);
  4736. if (ret) {
  4737. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4738. __func__);
  4739. return ret;
  4740. }
  4741. ret = dwc2_backup_host_registers(hsotg);
  4742. if (ret) {
  4743. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  4744. __func__);
  4745. return ret;
  4746. }
  4747. /* Enter USB Suspend Mode */
  4748. hprt0 = dwc2_readl(hsotg, HPRT0);
  4749. hprt0 |= HPRT0_SUSP;
  4750. hprt0 &= ~HPRT0_ENA;
  4751. dwc2_writel(hsotg, hprt0, HPRT0);
  4752. /* Wait for the HPRT0.PrtSusp register field to be set */
  4753. if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
  4754. dev_warn(hsotg->dev, "Suspend wasn't generated\n");
  4755. /*
  4756. * We need to disable interrupts to prevent servicing of any IRQ
  4757. * during going to hibernation
  4758. */
  4759. spin_lock_irqsave(&hsotg->lock, flags);
  4760. hsotg->lx_state = DWC2_L2;
  4761. gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  4762. if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
  4763. /* ULPI interface */
  4764. udelay(10);
  4765. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4766. gpwrdn |= GPWRDN_ULPI_LATCH_EN_DURING_HIB_ENTRY;
  4767. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4768. udelay(10);
  4769. /* Suspend the Phy Clock */
  4770. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4771. pcgcctl |= PCGCTL_STOPPCLK;
  4772. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4773. udelay(10);
  4774. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4775. gpwrdn |= GPWRDN_PMUACTV;
  4776. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4777. udelay(10);
  4778. } else {
  4779. /* UTMI+ Interface */
  4780. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4781. gpwrdn |= GPWRDN_PMUACTV;
  4782. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4783. udelay(10);
  4784. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4785. pcgcctl |= PCGCTL_STOPPCLK;
  4786. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4787. udelay(10);
  4788. }
  4789. /* Enable interrupts from wake up logic */
  4790. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4791. gpwrdn |= GPWRDN_PMUINTSEL;
  4792. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4793. udelay(10);
  4794. /* Unmask host mode interrupts in GPWRDN */
  4795. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4796. gpwrdn |= GPWRDN_DISCONN_DET_MSK;
  4797. gpwrdn |= GPWRDN_LNSTSCHG_MSK;
  4798. gpwrdn |= GPWRDN_STS_CHGINT_MSK;
  4799. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4800. udelay(10);
  4801. /* Enable Power Down Clamp */
  4802. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4803. gpwrdn |= GPWRDN_PWRDNCLMP;
  4804. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4805. udelay(10);
  4806. /* Switch off VDD */
  4807. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4808. gpwrdn |= GPWRDN_PWRDNSWTCH;
  4809. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4810. hsotg->hibernated = 1;
  4811. hsotg->bus_suspended = 1;
  4812. dev_dbg(hsotg->dev, "Host hibernation completed\n");
  4813. spin_unlock_irqrestore(&hsotg->lock, flags);
  4814. return ret;
  4815. }
  4816. /*
  4817. * dwc2_host_exit_hibernation()
  4818. *
  4819. * @hsotg: Programming view of the DWC_otg controller
  4820. * @rem_wakeup: indicates whether resume is initiated by Device or Host.
  4821. * @param reset: indicates whether resume is initiated by Reset.
  4822. *
  4823. * Return: non-zero if failed to enter to hibernation.
  4824. *
  4825. * This function is for exiting from Host mode hibernation by
  4826. * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  4827. */
  4828. int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  4829. int reset)
  4830. {
  4831. u32 gpwrdn;
  4832. u32 hprt0;
  4833. int ret = 0;
  4834. struct dwc2_gregs_backup *gr;
  4835. struct dwc2_hregs_backup *hr;
  4836. gr = &hsotg->gr_backup;
  4837. hr = &hsotg->hr_backup;
  4838. dev_dbg(hsotg->dev,
  4839. "%s: called with rem_wakeup = %d reset = %d\n",
  4840. __func__, rem_wakeup, reset);
  4841. dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
  4842. hsotg->hibernated = 0;
  4843. /*
  4844. * This step is not described in functional spec but if not wait for
  4845. * this delay, mismatch interrupts occurred because just after restore
  4846. * core is in Device mode(gintsts.curmode == 0)
  4847. */
  4848. mdelay(100);
  4849. /* Clear all pending interupts */
  4850. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4851. /* De-assert Restore */
  4852. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4853. gpwrdn &= ~GPWRDN_RESTORE;
  4854. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4855. udelay(10);
  4856. /* Restore GUSBCFG, HCFG */
  4857. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  4858. dwc2_writel(hsotg, hr->hcfg, HCFG);
  4859. /* Reset ULPI latch */
  4860. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4861. gpwrdn &= ~GPWRDN_ULPI_LATCH_EN_DURING_HIB_ENTRY;
  4862. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4863. /* De-assert Wakeup Logic */
  4864. if (!(rem_wakeup && hsotg->hw_params.snpsid >= DWC2_CORE_REV_4_30a)) {
  4865. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4866. gpwrdn &= ~GPWRDN_PMUACTV;
  4867. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4868. udelay(10);
  4869. }
  4870. hprt0 = hr->hprt0;
  4871. hprt0 |= HPRT0_PWR;
  4872. hprt0 &= ~HPRT0_ENA;
  4873. hprt0 &= ~HPRT0_SUSP;
  4874. dwc2_writel(hsotg, hprt0, HPRT0);
  4875. hprt0 = hr->hprt0;
  4876. hprt0 |= HPRT0_PWR;
  4877. hprt0 &= ~HPRT0_ENA;
  4878. hprt0 &= ~HPRT0_SUSP;
  4879. if (reset) {
  4880. hprt0 |= HPRT0_RST;
  4881. dwc2_writel(hsotg, hprt0, HPRT0);
  4882. /* Wait for Resume time and then program HPRT again */
  4883. mdelay(60);
  4884. hprt0 &= ~HPRT0_RST;
  4885. dwc2_writel(hsotg, hprt0, HPRT0);
  4886. } else {
  4887. hprt0 |= HPRT0_RES;
  4888. dwc2_writel(hsotg, hprt0, HPRT0);
  4889. /* De-assert Wakeup Logic */
  4890. if ((rem_wakeup && hsotg->hw_params.snpsid >= DWC2_CORE_REV_4_30a)) {
  4891. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4892. gpwrdn &= ~GPWRDN_PMUACTV;
  4893. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4894. udelay(10);
  4895. }
  4896. /* Wait for Resume time and then program HPRT again */
  4897. mdelay(100);
  4898. hprt0 &= ~HPRT0_RES;
  4899. dwc2_writel(hsotg, hprt0, HPRT0);
  4900. }
  4901. /* Clear all interrupt status */
  4902. hprt0 = dwc2_readl(hsotg, HPRT0);
  4903. hprt0 |= HPRT0_CONNDET;
  4904. hprt0 |= HPRT0_ENACHG;
  4905. hprt0 &= ~HPRT0_ENA;
  4906. dwc2_writel(hsotg, hprt0, HPRT0);
  4907. hprt0 = dwc2_readl(hsotg, HPRT0);
  4908. /* Clear all pending interupts */
  4909. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4910. /* Restore global registers */
  4911. ret = dwc2_restore_global_registers(hsotg);
  4912. if (ret) {
  4913. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  4914. __func__);
  4915. return ret;
  4916. }
  4917. /* Restore host registers */
  4918. ret = dwc2_restore_host_registers(hsotg);
  4919. if (ret) {
  4920. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  4921. __func__);
  4922. return ret;
  4923. }
  4924. if (rem_wakeup) {
  4925. dwc2_hcd_rem_wakeup(hsotg);
  4926. /*
  4927. * Change "port_connect_status_change" flag to re-enumerate,
  4928. * because after exit from hibernation port connection status
  4929. * is not detected.
  4930. */
  4931. hsotg->flags.b.port_connect_status_change = 1;
  4932. }
  4933. hsotg->hibernated = 0;
  4934. hsotg->bus_suspended = 0;
  4935. hsotg->lx_state = DWC2_L0;
  4936. dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
  4937. return ret;
  4938. }
  4939. bool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2)
  4940. {
  4941. struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub;
  4942. /* If the controller isn't allowed to wakeup then we can power off. */
  4943. if (!device_may_wakeup(dwc2->dev))
  4944. return true;
  4945. /*
  4946. * We don't want to power off the PHY if something under the
  4947. * root hub has wakeup enabled.
  4948. */
  4949. if (usb_wakeup_enabled_descendants(root_hub))
  4950. return false;
  4951. /* No reason to keep the PHY powered, so allow poweroff */
  4952. return true;
  4953. }
  4954. /**
  4955. * dwc2_host_enter_partial_power_down() - Put controller in partial
  4956. * power down.
  4957. *
  4958. * @hsotg: Programming view of the DWC_otg controller
  4959. *
  4960. * Return: non-zero if failed to enter host partial power down.
  4961. *
  4962. * This function is for entering Host mode partial power down.
  4963. */
  4964. int dwc2_host_enter_partial_power_down(struct dwc2_hsotg *hsotg)
  4965. {
  4966. u32 pcgcctl;
  4967. u32 hprt0;
  4968. int ret = 0;
  4969. dev_dbg(hsotg->dev, "Entering host partial power down started.\n");
  4970. /* Put this port in suspend mode. */
  4971. hprt0 = dwc2_read_hprt0(hsotg);
  4972. hprt0 |= HPRT0_SUSP;
  4973. dwc2_writel(hsotg, hprt0, HPRT0);
  4974. udelay(5);
  4975. /* Wait for the HPRT0.PrtSusp register field to be set */
  4976. if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
  4977. dev_warn(hsotg->dev, "Suspend wasn't generated\n");
  4978. /* Backup all registers */
  4979. ret = dwc2_backup_global_registers(hsotg);
  4980. if (ret) {
  4981. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4982. __func__);
  4983. return ret;
  4984. }
  4985. ret = dwc2_backup_host_registers(hsotg);
  4986. if (ret) {
  4987. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  4988. __func__);
  4989. return ret;
  4990. }
  4991. /*
  4992. * Clear any pending interrupts since dwc2 will not be able to
  4993. * clear them after entering partial_power_down.
  4994. */
  4995. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4996. /* Put the controller in low power state */
  4997. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4998. pcgcctl |= PCGCTL_PWRCLMP;
  4999. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  5000. udelay(5);
  5001. pcgcctl |= PCGCTL_RSTPDWNMODULE;
  5002. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  5003. udelay(5);
  5004. pcgcctl |= PCGCTL_STOPPCLK;
  5005. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  5006. /* Set in_ppd flag to 1 as here core enters suspend. */
  5007. hsotg->in_ppd = 1;
  5008. hsotg->lx_state = DWC2_L2;
  5009. hsotg->bus_suspended = true;
  5010. dev_dbg(hsotg->dev, "Entering host partial power down completed.\n");
  5011. return ret;
  5012. }
  5013. /*
  5014. * dwc2_host_exit_partial_power_down() - Exit controller from host partial
  5015. * power down.
  5016. *
  5017. * @hsotg: Programming view of the DWC_otg controller
  5018. * @rem_wakeup: indicates whether resume is initiated by Reset.
  5019. * @restore: indicates whether need to restore the registers or not.
  5020. *
  5021. * Return: non-zero if failed to exit host partial power down.
  5022. *
  5023. * This function is for exiting from Host mode partial power down.
  5024. */
  5025. int dwc2_host_exit_partial_power_down(struct dwc2_hsotg *hsotg,
  5026. int rem_wakeup, bool restore)
  5027. {
  5028. u32 pcgcctl;
  5029. int ret = 0;
  5030. u32 hprt0;
  5031. dev_dbg(hsotg->dev, "Exiting host partial power down started.\n");
  5032. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  5033. pcgcctl &= ~PCGCTL_STOPPCLK;
  5034. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  5035. udelay(5);
  5036. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  5037. pcgcctl &= ~PCGCTL_PWRCLMP;
  5038. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  5039. udelay(5);
  5040. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  5041. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  5042. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  5043. udelay(100);
  5044. if (restore) {
  5045. ret = dwc2_restore_global_registers(hsotg);
  5046. if (ret) {
  5047. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  5048. __func__);
  5049. return ret;
  5050. }
  5051. ret = dwc2_restore_host_registers(hsotg);
  5052. if (ret) {
  5053. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  5054. __func__);
  5055. return ret;
  5056. }
  5057. }
  5058. /* Drive resume signaling and exit suspend mode on the port. */
  5059. hprt0 = dwc2_read_hprt0(hsotg);
  5060. hprt0 |= HPRT0_RES;
  5061. hprt0 &= ~HPRT0_SUSP;
  5062. dwc2_writel(hsotg, hprt0, HPRT0);
  5063. udelay(5);
  5064. if (!rem_wakeup) {
  5065. /* Stop driveing resume signaling on the port. */
  5066. hprt0 = dwc2_read_hprt0(hsotg);
  5067. hprt0 &= ~HPRT0_RES;
  5068. dwc2_writel(hsotg, hprt0, HPRT0);
  5069. hsotg->bus_suspended = false;
  5070. } else {
  5071. /* Turn on the port power bit. */
  5072. hprt0 = dwc2_read_hprt0(hsotg);
  5073. hprt0 |= HPRT0_PWR;
  5074. dwc2_writel(hsotg, hprt0, HPRT0);
  5075. /* Connect hcd. */
  5076. dwc2_hcd_connect(hsotg);
  5077. mod_timer(&hsotg->wkp_timer,
  5078. jiffies + msecs_to_jiffies(71));
  5079. }
  5080. /* Set lx_state to and in_ppd to 0 as here core exits from suspend. */
  5081. hsotg->in_ppd = 0;
  5082. hsotg->lx_state = DWC2_L0;
  5083. dev_dbg(hsotg->dev, "Exiting host partial power down completed.\n");
  5084. return ret;
  5085. }
  5086. /**
  5087. * dwc2_host_enter_clock_gating() - Put controller in clock gating.
  5088. *
  5089. * @hsotg: Programming view of the DWC_otg controller
  5090. *
  5091. * This function is for entering Host mode clock gating.
  5092. */
  5093. void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg)
  5094. {
  5095. u32 hprt0;
  5096. u32 pcgctl;
  5097. dev_dbg(hsotg->dev, "Entering host clock gating.\n");
  5098. /* Put this port in suspend mode. */
  5099. hprt0 = dwc2_read_hprt0(hsotg);
  5100. hprt0 |= HPRT0_SUSP;
  5101. dwc2_writel(hsotg, hprt0, HPRT0);
  5102. /* Set the Phy Clock bit as suspend is received. */
  5103. pcgctl = dwc2_readl(hsotg, PCGCTL);
  5104. pcgctl |= PCGCTL_STOPPCLK;
  5105. dwc2_writel(hsotg, pcgctl, PCGCTL);
  5106. udelay(5);
  5107. /* Set the Gate hclk as suspend is received. */
  5108. pcgctl = dwc2_readl(hsotg, PCGCTL);
  5109. pcgctl |= PCGCTL_GATEHCLK;
  5110. dwc2_writel(hsotg, pcgctl, PCGCTL);
  5111. udelay(5);
  5112. hsotg->bus_suspended = true;
  5113. hsotg->lx_state = DWC2_L2;
  5114. }
  5115. /**
  5116. * dwc2_host_exit_clock_gating() - Exit controller from clock gating.
  5117. *
  5118. * @hsotg: Programming view of the DWC_otg controller
  5119. * @rem_wakeup: indicates whether resume is initiated by remote wakeup
  5120. *
  5121. * This function is for exiting Host mode clock gating.
  5122. */
  5123. void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
  5124. {
  5125. u32 hprt0;
  5126. u32 pcgctl;
  5127. dev_dbg(hsotg->dev, "Exiting host clock gating.\n");
  5128. /* Clear the Gate hclk. */
  5129. pcgctl = dwc2_readl(hsotg, PCGCTL);
  5130. pcgctl &= ~PCGCTL_GATEHCLK;
  5131. dwc2_writel(hsotg, pcgctl, PCGCTL);
  5132. udelay(5);
  5133. /* Phy Clock bit. */
  5134. pcgctl = dwc2_readl(hsotg, PCGCTL);
  5135. pcgctl &= ~PCGCTL_STOPPCLK;
  5136. dwc2_writel(hsotg, pcgctl, PCGCTL);
  5137. udelay(5);
  5138. /* Drive resume signaling and exit suspend mode on the port. */
  5139. hprt0 = dwc2_read_hprt0(hsotg);
  5140. hprt0 |= HPRT0_RES;
  5141. hprt0 &= ~HPRT0_SUSP;
  5142. dwc2_writel(hsotg, hprt0, HPRT0);
  5143. udelay(5);
  5144. if (!rem_wakeup) {
  5145. /* In case of port resume need to wait for 40 ms */
  5146. msleep(USB_RESUME_TIMEOUT);
  5147. /* Stop driveing resume signaling on the port. */
  5148. hprt0 = dwc2_read_hprt0(hsotg);
  5149. hprt0 &= ~HPRT0_RES;
  5150. dwc2_writel(hsotg, hprt0, HPRT0);
  5151. hsotg->bus_suspended = false;
  5152. hsotg->lx_state = DWC2_L0;
  5153. } else {
  5154. mod_timer(&hsotg->wkp_timer,
  5155. jiffies + msecs_to_jiffies(71));
  5156. }
  5157. }