hcd.c 159 KB

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