hcd.c 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709
  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 *data_buf = (u32 *)chan->xfer_buf;
  1175. if (dbg_hc(chan))
  1176. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1177. remaining_count = chan->xfer_len - chan->xfer_count;
  1178. if (remaining_count > chan->max_packet)
  1179. byte_count = chan->max_packet;
  1180. else
  1181. byte_count = remaining_count;
  1182. dword_count = (byte_count + 3) / 4;
  1183. if (((unsigned long)data_buf & 0x3) == 0) {
  1184. /* xfer_buf is DWORD aligned */
  1185. for (i = 0; i < dword_count; i++, data_buf++)
  1186. dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
  1187. } else {
  1188. /* xfer_buf is not DWORD aligned */
  1189. for (i = 0; i < dword_count; i++, data_buf++) {
  1190. u32 data = data_buf[0] | data_buf[1] << 8 |
  1191. data_buf[2] << 16 | data_buf[3] << 24;
  1192. dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
  1193. }
  1194. }
  1195. chan->xfer_count += byte_count;
  1196. chan->xfer_buf += byte_count;
  1197. }
  1198. /**
  1199. * dwc2_hc_do_ping() - Starts a PING transfer
  1200. *
  1201. * @hsotg: Programming view of DWC_otg controller
  1202. * @chan: Information needed to initialize the host channel
  1203. *
  1204. * This function should only be called in Slave mode. The Do Ping bit is set in
  1205. * the HCTSIZ register, then the channel is enabled.
  1206. */
  1207. static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
  1208. struct dwc2_host_chan *chan)
  1209. {
  1210. u32 hcchar;
  1211. u32 hctsiz;
  1212. if (dbg_hc(chan))
  1213. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1214. chan->hc_num);
  1215. hctsiz = TSIZ_DOPNG;
  1216. hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
  1217. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1218. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1219. hcchar |= HCCHAR_CHENA;
  1220. hcchar &= ~HCCHAR_CHDIS;
  1221. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1222. }
  1223. /**
  1224. * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
  1225. * channel and starts the transfer
  1226. *
  1227. * @hsotg: Programming view of DWC_otg controller
  1228. * @chan: Information needed to initialize the host channel. The xfer_len value
  1229. * may be reduced to accommodate the max widths of the XferSize and
  1230. * PktCnt fields in the HCTSIZn register. The multi_count value may be
  1231. * changed to reflect the final xfer_len value.
  1232. *
  1233. * This function may be called in either Slave mode or DMA mode. In Slave mode,
  1234. * the caller must ensure that there is sufficient space in the request queue
  1235. * and Tx Data FIFO.
  1236. *
  1237. * For an OUT transfer in Slave mode, it loads a data packet into the
  1238. * appropriate FIFO. If necessary, additional data packets are loaded in the
  1239. * Host ISR.
  1240. *
  1241. * For an IN transfer in Slave mode, a data packet is requested. The data
  1242. * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  1243. * additional data packets are requested in the Host ISR.
  1244. *
  1245. * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  1246. * register along with a packet count of 1 and the channel is enabled. This
  1247. * causes a single PING transaction to occur. Other fields in HCTSIZ are
  1248. * simply set to 0 since no data transfer occurs in this case.
  1249. *
  1250. * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  1251. * all the information required to perform the subsequent data transfer. In
  1252. * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  1253. * controller performs the entire PING protocol, then starts the data
  1254. * transfer.
  1255. */
  1256. static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
  1257. struct dwc2_host_chan *chan)
  1258. {
  1259. u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
  1260. u16 max_hc_pkt_count = hsotg->params.max_packet_count;
  1261. u32 hcchar;
  1262. u32 hctsiz = 0;
  1263. u16 num_packets;
  1264. u32 ec_mc;
  1265. if (dbg_hc(chan))
  1266. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1267. if (chan->do_ping) {
  1268. if (!hsotg->params.host_dma) {
  1269. if (dbg_hc(chan))
  1270. dev_vdbg(hsotg->dev, "ping, no DMA\n");
  1271. dwc2_hc_do_ping(hsotg, chan);
  1272. chan->xfer_started = 1;
  1273. return;
  1274. }
  1275. if (dbg_hc(chan))
  1276. dev_vdbg(hsotg->dev, "ping, DMA\n");
  1277. hctsiz |= TSIZ_DOPNG;
  1278. }
  1279. if (chan->do_split) {
  1280. if (dbg_hc(chan))
  1281. dev_vdbg(hsotg->dev, "split\n");
  1282. num_packets = 1;
  1283. if (chan->complete_split && !chan->ep_is_in)
  1284. /*
  1285. * For CSPLIT OUT Transfer, set the size to 0 so the
  1286. * core doesn't expect any data written to the FIFO
  1287. */
  1288. chan->xfer_len = 0;
  1289. else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
  1290. chan->xfer_len = chan->max_packet;
  1291. else if (!chan->ep_is_in && chan->xfer_len > 188)
  1292. chan->xfer_len = 188;
  1293. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1294. TSIZ_XFERSIZE_MASK;
  1295. /* For split set ec_mc for immediate retries */
  1296. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1297. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1298. ec_mc = 3;
  1299. else
  1300. ec_mc = 1;
  1301. } else {
  1302. if (dbg_hc(chan))
  1303. dev_vdbg(hsotg->dev, "no split\n");
  1304. /*
  1305. * Ensure that the transfer length and packet count will fit
  1306. * in the widths allocated for them in the HCTSIZn register
  1307. */
  1308. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1309. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1310. /*
  1311. * Make sure the transfer size is no larger than one
  1312. * (micro)frame's worth of data. (A check was done
  1313. * when the periodic transfer was accepted to ensure
  1314. * that a (micro)frame's worth of data can be
  1315. * programmed into a channel.)
  1316. */
  1317. u32 max_periodic_len =
  1318. chan->multi_count * chan->max_packet;
  1319. if (chan->xfer_len > max_periodic_len)
  1320. chan->xfer_len = max_periodic_len;
  1321. } else if (chan->xfer_len > max_hc_xfer_size) {
  1322. /*
  1323. * Make sure that xfer_len is a multiple of max packet
  1324. * size
  1325. */
  1326. chan->xfer_len =
  1327. max_hc_xfer_size - chan->max_packet + 1;
  1328. }
  1329. if (chan->xfer_len > 0) {
  1330. num_packets = (chan->xfer_len + chan->max_packet - 1) /
  1331. chan->max_packet;
  1332. if (num_packets > max_hc_pkt_count) {
  1333. num_packets = max_hc_pkt_count;
  1334. chan->xfer_len = num_packets * chan->max_packet;
  1335. }
  1336. } else {
  1337. /* Need 1 packet for transfer length of 0 */
  1338. num_packets = 1;
  1339. }
  1340. if (chan->ep_is_in)
  1341. /*
  1342. * Always program an integral # of max packets for IN
  1343. * transfers
  1344. */
  1345. chan->xfer_len = num_packets * chan->max_packet;
  1346. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1347. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1348. /*
  1349. * Make sure that the multi_count field matches the
  1350. * actual transfer length
  1351. */
  1352. chan->multi_count = num_packets;
  1353. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1354. dwc2_set_pid_isoc(chan);
  1355. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1356. TSIZ_XFERSIZE_MASK;
  1357. /* The ec_mc gets the multi_count for non-split */
  1358. ec_mc = chan->multi_count;
  1359. }
  1360. chan->start_pkt_count = num_packets;
  1361. hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
  1362. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1363. TSIZ_SC_MC_PID_MASK;
  1364. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1365. if (dbg_hc(chan)) {
  1366. dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
  1367. hctsiz, chan->hc_num);
  1368. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1369. chan->hc_num);
  1370. dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
  1371. (hctsiz & TSIZ_XFERSIZE_MASK) >>
  1372. TSIZ_XFERSIZE_SHIFT);
  1373. dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
  1374. (hctsiz & TSIZ_PKTCNT_MASK) >>
  1375. TSIZ_PKTCNT_SHIFT);
  1376. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1377. (hctsiz & TSIZ_SC_MC_PID_MASK) >>
  1378. TSIZ_SC_MC_PID_SHIFT);
  1379. }
  1380. if (hsotg->params.host_dma) {
  1381. dma_addr_t dma_addr;
  1382. if (chan->align_buf) {
  1383. if (dbg_hc(chan))
  1384. dev_vdbg(hsotg->dev, "align_buf\n");
  1385. dma_addr = chan->align_buf;
  1386. } else {
  1387. dma_addr = chan->xfer_dma;
  1388. }
  1389. dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
  1390. if (dbg_hc(chan))
  1391. dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
  1392. (unsigned long)dma_addr, chan->hc_num);
  1393. }
  1394. /* Start the split */
  1395. if (chan->do_split) {
  1396. u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
  1397. hcsplt |= HCSPLT_SPLTENA;
  1398. dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
  1399. }
  1400. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1401. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1402. hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
  1403. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1404. if (hcchar & HCCHAR_CHDIS)
  1405. dev_warn(hsotg->dev,
  1406. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1407. __func__, chan->hc_num, hcchar);
  1408. /* Set host channel enable after all other setup is complete */
  1409. hcchar |= HCCHAR_CHENA;
  1410. hcchar &= ~HCCHAR_CHDIS;
  1411. if (dbg_hc(chan))
  1412. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1413. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1414. HCCHAR_MULTICNT_SHIFT);
  1415. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1416. if (dbg_hc(chan))
  1417. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1418. chan->hc_num);
  1419. chan->xfer_started = 1;
  1420. chan->requests++;
  1421. if (!hsotg->params.host_dma &&
  1422. !chan->ep_is_in && chan->xfer_len > 0)
  1423. /* Load OUT packet into the appropriate Tx FIFO */
  1424. dwc2_hc_write_packet(hsotg, chan);
  1425. }
  1426. /**
  1427. * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
  1428. * host channel and starts the transfer in Descriptor DMA mode
  1429. *
  1430. * @hsotg: Programming view of DWC_otg controller
  1431. * @chan: Information needed to initialize the host channel
  1432. *
  1433. * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  1434. * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
  1435. * with micro-frame bitmap.
  1436. *
  1437. * Initializes HCDMA register with descriptor list address and CTD value then
  1438. * starts the transfer via enabling the channel.
  1439. */
  1440. void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
  1441. struct dwc2_host_chan *chan)
  1442. {
  1443. u32 hcchar;
  1444. u32 hctsiz = 0;
  1445. if (chan->do_ping)
  1446. hctsiz |= TSIZ_DOPNG;
  1447. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1448. dwc2_set_pid_isoc(chan);
  1449. /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  1450. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1451. TSIZ_SC_MC_PID_MASK;
  1452. /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
  1453. hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
  1454. /* Non-zero only for high-speed interrupt endpoints */
  1455. hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
  1456. if (dbg_hc(chan)) {
  1457. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1458. chan->hc_num);
  1459. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1460. chan->data_pid_start);
  1461. dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
  1462. }
  1463. dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
  1464. dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
  1465. chan->desc_list_sz, DMA_TO_DEVICE);
  1466. dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
  1467. if (dbg_hc(chan))
  1468. dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
  1469. &chan->desc_list_addr, chan->hc_num);
  1470. hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1471. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1472. hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
  1473. HCCHAR_MULTICNT_MASK;
  1474. if (hcchar & HCCHAR_CHDIS)
  1475. dev_warn(hsotg->dev,
  1476. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1477. __func__, chan->hc_num, hcchar);
  1478. /* Set host channel enable after all other setup is complete */
  1479. hcchar |= HCCHAR_CHENA;
  1480. hcchar &= ~HCCHAR_CHDIS;
  1481. if (dbg_hc(chan))
  1482. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1483. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1484. HCCHAR_MULTICNT_SHIFT);
  1485. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1486. if (dbg_hc(chan))
  1487. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1488. chan->hc_num);
  1489. chan->xfer_started = 1;
  1490. chan->requests++;
  1491. }
  1492. /**
  1493. * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
  1494. * a previous call to dwc2_hc_start_transfer()
  1495. *
  1496. * @hsotg: Programming view of DWC_otg controller
  1497. * @chan: Information needed to initialize the host channel
  1498. *
  1499. * The caller must ensure there is sufficient space in the request queue and Tx
  1500. * Data FIFO. This function should only be called in Slave mode. In DMA mode,
  1501. * the controller acts autonomously to complete transfers programmed to a host
  1502. * channel.
  1503. *
  1504. * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  1505. * if there is any data remaining to be queued. For an IN transfer, another
  1506. * data packet is always requested. For the SETUP phase of a control transfer,
  1507. * this function does nothing.
  1508. *
  1509. * Return: 1 if a new request is queued, 0 if no more requests are required
  1510. * for this transfer
  1511. */
  1512. static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
  1513. struct dwc2_host_chan *chan)
  1514. {
  1515. if (dbg_hc(chan))
  1516. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1517. chan->hc_num);
  1518. if (chan->do_split)
  1519. /* SPLITs always queue just once per channel */
  1520. return 0;
  1521. if (chan->data_pid_start == DWC2_HC_PID_SETUP)
  1522. /* SETUPs are queued only once since they can't be NAK'd */
  1523. return 0;
  1524. if (chan->ep_is_in) {
  1525. /*
  1526. * Always queue another request for other IN transfers. If
  1527. * back-to-back INs are issued and NAKs are received for both,
  1528. * the driver may still be processing the first NAK when the
  1529. * second NAK is received. When the interrupt handler clears
  1530. * the NAK interrupt for the first NAK, the second NAK will
  1531. * not be seen. So we can't depend on the NAK interrupt
  1532. * handler to requeue a NAK'd request. Instead, IN requests
  1533. * are issued each time this function is called. When the
  1534. * transfer completes, the extra requests for the channel will
  1535. * be flushed.
  1536. */
  1537. u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
  1538. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1539. hcchar |= HCCHAR_CHENA;
  1540. hcchar &= ~HCCHAR_CHDIS;
  1541. if (dbg_hc(chan))
  1542. dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
  1543. hcchar);
  1544. dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
  1545. chan->requests++;
  1546. return 1;
  1547. }
  1548. /* OUT transfers */
  1549. if (chan->xfer_count < chan->xfer_len) {
  1550. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1551. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1552. u32 hcchar = dwc2_readl(hsotg,
  1553. HCCHAR(chan->hc_num));
  1554. dwc2_hc_set_even_odd_frame(hsotg, chan,
  1555. &hcchar);
  1556. }
  1557. /* Load OUT packet into the appropriate Tx FIFO */
  1558. dwc2_hc_write_packet(hsotg, chan);
  1559. chan->requests++;
  1560. return 1;
  1561. }
  1562. return 0;
  1563. }
  1564. /*
  1565. * =========================================================================
  1566. * HCD
  1567. * =========================================================================
  1568. */
  1569. /*
  1570. * Processes all the URBs in a single list of QHs. Completes them with
  1571. * -ETIMEDOUT and frees the QTD.
  1572. *
  1573. * Must be called with interrupt disabled and spinlock held
  1574. */
  1575. static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
  1576. struct list_head *qh_list)
  1577. {
  1578. struct dwc2_qh *qh, *qh_tmp;
  1579. struct dwc2_qtd *qtd, *qtd_tmp;
  1580. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  1581. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  1582. qtd_list_entry) {
  1583. dwc2_host_complete(hsotg, qtd, -ECONNRESET);
  1584. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1585. }
  1586. }
  1587. }
  1588. static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
  1589. struct list_head *qh_list)
  1590. {
  1591. struct dwc2_qtd *qtd, *qtd_tmp;
  1592. struct dwc2_qh *qh, *qh_tmp;
  1593. unsigned long flags;
  1594. if (!qh_list->next)
  1595. /* The list hasn't been initialized yet */
  1596. return;
  1597. spin_lock_irqsave(&hsotg->lock, flags);
  1598. /* Ensure there are no QTDs or URBs left */
  1599. dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
  1600. list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
  1601. dwc2_hcd_qh_unlink(hsotg, qh);
  1602. /* Free each QTD in the QH's QTD list */
  1603. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  1604. qtd_list_entry)
  1605. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1606. if (qh->channel && qh->channel->qh == qh)
  1607. qh->channel->qh = NULL;
  1608. spin_unlock_irqrestore(&hsotg->lock, flags);
  1609. dwc2_hcd_qh_free(hsotg, qh);
  1610. spin_lock_irqsave(&hsotg->lock, flags);
  1611. }
  1612. spin_unlock_irqrestore(&hsotg->lock, flags);
  1613. }
  1614. /*
  1615. * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
  1616. * and periodic schedules. The QTD associated with each URB is removed from
  1617. * the schedule and freed. This function may be called when a disconnect is
  1618. * detected or when the HCD is being stopped.
  1619. *
  1620. * Must be called with interrupt disabled and spinlock held
  1621. */
  1622. static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
  1623. {
  1624. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
  1625. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
  1626. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
  1627. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
  1628. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
  1629. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
  1630. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
  1631. }
  1632. /**
  1633. * dwc2_hcd_start() - Starts the HCD when switching to Host mode
  1634. *
  1635. * @hsotg: Pointer to struct dwc2_hsotg
  1636. */
  1637. void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
  1638. {
  1639. u32 hprt0;
  1640. if (hsotg->op_state == OTG_STATE_B_HOST) {
  1641. /*
  1642. * Reset the port. During a HNP mode switch the reset
  1643. * needs to occur within 1ms and have a duration of at
  1644. * least 50ms.
  1645. */
  1646. hprt0 = dwc2_read_hprt0(hsotg);
  1647. hprt0 |= HPRT0_RST;
  1648. dwc2_writel(hsotg, hprt0, HPRT0);
  1649. }
  1650. queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
  1651. msecs_to_jiffies(50));
  1652. }
  1653. /* Must be called with interrupt disabled and spinlock held */
  1654. static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
  1655. {
  1656. int num_channels = hsotg->params.host_channels;
  1657. struct dwc2_host_chan *channel;
  1658. u32 hcchar;
  1659. int i;
  1660. if (!hsotg->params.host_dma) {
  1661. /* Flush out any channel requests in slave mode */
  1662. for (i = 0; i < num_channels; i++) {
  1663. channel = hsotg->hc_ptr_array[i];
  1664. if (!list_empty(&channel->hc_list_entry))
  1665. continue;
  1666. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1667. if (hcchar & HCCHAR_CHENA) {
  1668. hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
  1669. hcchar |= HCCHAR_CHDIS;
  1670. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1671. }
  1672. }
  1673. }
  1674. for (i = 0; i < num_channels; i++) {
  1675. channel = hsotg->hc_ptr_array[i];
  1676. if (!list_empty(&channel->hc_list_entry))
  1677. continue;
  1678. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  1679. if (hcchar & HCCHAR_CHENA) {
  1680. /* Halt the channel */
  1681. hcchar |= HCCHAR_CHDIS;
  1682. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  1683. }
  1684. dwc2_hc_cleanup(hsotg, channel);
  1685. list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
  1686. /*
  1687. * Added for Descriptor DMA to prevent channel double cleanup in
  1688. * release_channel_ddma(), which is called from ep_disable when
  1689. * device disconnects
  1690. */
  1691. channel->qh = NULL;
  1692. }
  1693. /* All channels have been freed, mark them available */
  1694. if (hsotg->params.uframe_sched) {
  1695. hsotg->available_host_channels =
  1696. hsotg->params.host_channels;
  1697. } else {
  1698. hsotg->non_periodic_channels = 0;
  1699. hsotg->periodic_channels = 0;
  1700. }
  1701. }
  1702. /**
  1703. * dwc2_hcd_connect() - Handles connect of the HCD
  1704. *
  1705. * @hsotg: Pointer to struct dwc2_hsotg
  1706. *
  1707. * Must be called with interrupt disabled and spinlock held
  1708. */
  1709. void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
  1710. {
  1711. if (hsotg->lx_state != DWC2_L0)
  1712. usb_hcd_resume_root_hub(hsotg->priv);
  1713. hsotg->flags.b.port_connect_status_change = 1;
  1714. hsotg->flags.b.port_connect_status = 1;
  1715. }
  1716. /**
  1717. * dwc2_hcd_disconnect() - Handles disconnect of the HCD
  1718. *
  1719. * @hsotg: Pointer to struct dwc2_hsotg
  1720. * @force: If true, we won't try to reconnect even if we see device connected.
  1721. *
  1722. * Must be called with interrupt disabled and spinlock held
  1723. */
  1724. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
  1725. {
  1726. u32 intr;
  1727. u32 hprt0;
  1728. /* Set status flags for the hub driver */
  1729. hsotg->flags.b.port_connect_status_change = 1;
  1730. hsotg->flags.b.port_connect_status = 0;
  1731. /*
  1732. * Shutdown any transfers in process by clearing the Tx FIFO Empty
  1733. * interrupt mask and status bits and disabling subsequent host
  1734. * channel interrupts.
  1735. */
  1736. intr = dwc2_readl(hsotg, GINTMSK);
  1737. intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
  1738. dwc2_writel(hsotg, intr, GINTMSK);
  1739. intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
  1740. dwc2_writel(hsotg, intr, GINTSTS);
  1741. /*
  1742. * Turn off the vbus power only if the core has transitioned to device
  1743. * mode. If still in host mode, need to keep power on to detect a
  1744. * reconnection.
  1745. */
  1746. if (dwc2_is_device_mode(hsotg)) {
  1747. if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
  1748. dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
  1749. dwc2_writel(hsotg, 0, HPRT0);
  1750. }
  1751. dwc2_disable_host_interrupts(hsotg);
  1752. }
  1753. /* Respond with an error status to all URBs in the schedule */
  1754. dwc2_kill_all_urbs(hsotg);
  1755. if (dwc2_is_host_mode(hsotg))
  1756. /* Clean up any host channels that were in use */
  1757. dwc2_hcd_cleanup_channels(hsotg);
  1758. dwc2_host_disconnect(hsotg);
  1759. /*
  1760. * Add an extra check here to see if we're actually connected but
  1761. * we don't have a detection interrupt pending. This can happen if:
  1762. * 1. hardware sees connect
  1763. * 2. hardware sees disconnect
  1764. * 3. hardware sees connect
  1765. * 4. dwc2_port_intr() - clears connect interrupt
  1766. * 5. dwc2_handle_common_intr() - calls here
  1767. *
  1768. * Without the extra check here we will end calling disconnect
  1769. * and won't get any future interrupts to handle the connect.
  1770. */
  1771. if (!force) {
  1772. hprt0 = dwc2_readl(hsotg, HPRT0);
  1773. if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
  1774. dwc2_hcd_connect(hsotg);
  1775. }
  1776. }
  1777. /**
  1778. * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
  1779. *
  1780. * @hsotg: Pointer to struct dwc2_hsotg
  1781. */
  1782. static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
  1783. {
  1784. if (hsotg->bus_suspended) {
  1785. hsotg->flags.b.port_suspend_change = 1;
  1786. usb_hcd_resume_root_hub(hsotg->priv);
  1787. }
  1788. if (hsotg->lx_state == DWC2_L1)
  1789. hsotg->flags.b.port_l1_change = 1;
  1790. }
  1791. /**
  1792. * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
  1793. *
  1794. * @hsotg: Pointer to struct dwc2_hsotg
  1795. *
  1796. * Must be called with interrupt disabled and spinlock held
  1797. */
  1798. void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
  1799. {
  1800. dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
  1801. /*
  1802. * The root hub should be disconnected before this function is called.
  1803. * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  1804. * and the QH lists (via ..._hcd_endpoint_disable).
  1805. */
  1806. /* Turn off all host-specific interrupts */
  1807. dwc2_disable_host_interrupts(hsotg);
  1808. /* Turn off the vbus power */
  1809. dev_dbg(hsotg->dev, "PortPower off\n");
  1810. dwc2_writel(hsotg, 0, HPRT0);
  1811. }
  1812. /* Caller must hold driver lock */
  1813. static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
  1814. struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
  1815. struct dwc2_qtd *qtd)
  1816. {
  1817. u32 intr_mask;
  1818. int retval;
  1819. int dev_speed;
  1820. if (!hsotg->flags.b.port_connect_status) {
  1821. /* No longer connected */
  1822. dev_err(hsotg->dev, "Not connected\n");
  1823. return -ENODEV;
  1824. }
  1825. dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  1826. /* Some configurations cannot support LS traffic on a FS root port */
  1827. if ((dev_speed == USB_SPEED_LOW) &&
  1828. (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
  1829. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
  1830. u32 hprt0 = dwc2_readl(hsotg, HPRT0);
  1831. u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  1832. if (prtspd == HPRT0_SPD_FULL_SPEED)
  1833. return -ENODEV;
  1834. }
  1835. if (!qtd)
  1836. return -EINVAL;
  1837. dwc2_hcd_qtd_init(qtd, urb);
  1838. retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
  1839. if (retval) {
  1840. dev_err(hsotg->dev,
  1841. "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
  1842. retval);
  1843. return retval;
  1844. }
  1845. intr_mask = dwc2_readl(hsotg, GINTMSK);
  1846. if (!(intr_mask & GINTSTS_SOF)) {
  1847. enum dwc2_transaction_type tr_type;
  1848. if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
  1849. !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  1850. /*
  1851. * Do not schedule SG transactions until qtd has
  1852. * URB_GIVEBACK_ASAP set
  1853. */
  1854. return 0;
  1855. tr_type = dwc2_hcd_select_transactions(hsotg);
  1856. if (tr_type != DWC2_TRANSACTION_NONE)
  1857. dwc2_hcd_queue_transactions(hsotg, tr_type);
  1858. }
  1859. return 0;
  1860. }
  1861. /* Must be called with interrupt disabled and spinlock held */
  1862. static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
  1863. struct dwc2_hcd_urb *urb)
  1864. {
  1865. struct dwc2_qh *qh;
  1866. struct dwc2_qtd *urb_qtd;
  1867. urb_qtd = urb->qtd;
  1868. if (!urb_qtd) {
  1869. dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
  1870. return -EINVAL;
  1871. }
  1872. qh = urb_qtd->qh;
  1873. if (!qh) {
  1874. dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
  1875. return -EINVAL;
  1876. }
  1877. urb->priv = NULL;
  1878. if (urb_qtd->in_process && qh->channel) {
  1879. dwc2_dump_channel_info(hsotg, qh->channel);
  1880. /* The QTD is in process (it has been assigned to a channel) */
  1881. if (hsotg->flags.b.port_connect_status)
  1882. /*
  1883. * If still connected (i.e. in host mode), halt the
  1884. * channel so it can be used for other transfers. If
  1885. * no longer connected, the host registers can't be
  1886. * written to halt the channel since the core is in
  1887. * device mode.
  1888. */
  1889. dwc2_hc_halt(hsotg, qh->channel,
  1890. DWC2_HC_XFER_URB_DEQUEUE);
  1891. }
  1892. /*
  1893. * Free the QTD and clean up the associated QH. Leave the QH in the
  1894. * schedule if it has any remaining QTDs.
  1895. */
  1896. if (!hsotg->params.dma_desc_enable) {
  1897. u8 in_process = urb_qtd->in_process;
  1898. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1899. if (in_process) {
  1900. dwc2_hcd_qh_deactivate(hsotg, qh, 0);
  1901. qh->channel = NULL;
  1902. } else if (list_empty(&qh->qtd_list)) {
  1903. dwc2_hcd_qh_unlink(hsotg, qh);
  1904. }
  1905. } else {
  1906. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1907. }
  1908. return 0;
  1909. }
  1910. /* Must NOT be called with interrupt disabled or spinlock held */
  1911. static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
  1912. struct usb_host_endpoint *ep, int retry)
  1913. {
  1914. struct dwc2_qtd *qtd, *qtd_tmp;
  1915. struct dwc2_qh *qh;
  1916. unsigned long flags;
  1917. int rc;
  1918. spin_lock_irqsave(&hsotg->lock, flags);
  1919. qh = ep->hcpriv;
  1920. if (!qh) {
  1921. rc = -EINVAL;
  1922. goto err;
  1923. }
  1924. while (!list_empty(&qh->qtd_list) && retry--) {
  1925. if (retry == 0) {
  1926. dev_err(hsotg->dev,
  1927. "## timeout in dwc2_hcd_endpoint_disable() ##\n");
  1928. rc = -EBUSY;
  1929. goto err;
  1930. }
  1931. spin_unlock_irqrestore(&hsotg->lock, flags);
  1932. msleep(20);
  1933. spin_lock_irqsave(&hsotg->lock, flags);
  1934. qh = ep->hcpriv;
  1935. if (!qh) {
  1936. rc = -EINVAL;
  1937. goto err;
  1938. }
  1939. }
  1940. dwc2_hcd_qh_unlink(hsotg, qh);
  1941. /* Free each QTD in the QH's QTD list */
  1942. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
  1943. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1944. ep->hcpriv = NULL;
  1945. if (qh->channel && qh->channel->qh == qh)
  1946. qh->channel->qh = NULL;
  1947. spin_unlock_irqrestore(&hsotg->lock, flags);
  1948. dwc2_hcd_qh_free(hsotg, qh);
  1949. return 0;
  1950. err:
  1951. ep->hcpriv = NULL;
  1952. spin_unlock_irqrestore(&hsotg->lock, flags);
  1953. return rc;
  1954. }
  1955. /* Must be called with interrupt disabled and spinlock held */
  1956. static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
  1957. struct usb_host_endpoint *ep)
  1958. {
  1959. struct dwc2_qh *qh = ep->hcpriv;
  1960. if (!qh)
  1961. return -EINVAL;
  1962. qh->data_toggle = DWC2_HC_PID_DATA0;
  1963. return 0;
  1964. }
  1965. /**
  1966. * dwc2_core_init() - Initializes the DWC_otg controller registers and
  1967. * prepares the core for device mode or host mode operation
  1968. *
  1969. * @hsotg: Programming view of the DWC_otg controller
  1970. * @initial_setup: If true then this is the first init for this instance.
  1971. */
  1972. int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
  1973. {
  1974. u32 usbcfg, otgctl;
  1975. int retval;
  1976. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  1977. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  1978. /* Set ULPI External VBUS bit if needed */
  1979. usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
  1980. if (hsotg->params.phy_ulpi_ext_vbus)
  1981. usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
  1982. /* Set external TS Dline pulsing bit if needed */
  1983. usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
  1984. if (hsotg->params.ts_dline)
  1985. usbcfg |= GUSBCFG_TERMSELDLPULSE;
  1986. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  1987. /*
  1988. * Reset the Controller
  1989. *
  1990. * We only need to reset the controller if this is a re-init.
  1991. * For the first init we know for sure that earlier code reset us (it
  1992. * needed to in order to properly detect various parameters).
  1993. */
  1994. if (!initial_setup) {
  1995. retval = dwc2_core_reset(hsotg, false);
  1996. if (retval) {
  1997. dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
  1998. __func__);
  1999. return retval;
  2000. }
  2001. }
  2002. /*
  2003. * This needs to happen in FS mode before any other programming occurs
  2004. */
  2005. retval = dwc2_phy_init(hsotg, initial_setup);
  2006. if (retval)
  2007. return retval;
  2008. /* Program the GAHBCFG Register */
  2009. retval = dwc2_gahbcfg_init(hsotg);
  2010. if (retval)
  2011. return retval;
  2012. /* Program the GUSBCFG register */
  2013. dwc2_gusbcfg_init(hsotg);
  2014. /* Program the GOTGCTL register */
  2015. otgctl = dwc2_readl(hsotg, GOTGCTL);
  2016. otgctl &= ~GOTGCTL_OTGVER;
  2017. dwc2_writel(hsotg, otgctl, GOTGCTL);
  2018. /* Clear the SRP success bit for FS-I2c */
  2019. hsotg->srp_success = 0;
  2020. /* Enable common interrupts */
  2021. dwc2_enable_common_interrupts(hsotg);
  2022. /*
  2023. * Do device or host initialization based on mode during PCD and
  2024. * HCD initialization
  2025. */
  2026. if (dwc2_is_host_mode(hsotg)) {
  2027. dev_dbg(hsotg->dev, "Host Mode\n");
  2028. hsotg->op_state = OTG_STATE_A_HOST;
  2029. } else {
  2030. dev_dbg(hsotg->dev, "Device Mode\n");
  2031. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  2032. }
  2033. return 0;
  2034. }
  2035. /**
  2036. * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
  2037. * Host mode
  2038. *
  2039. * @hsotg: Programming view of DWC_otg controller
  2040. *
  2041. * This function flushes the Tx and Rx FIFOs and flushes any entries in the
  2042. * request queues. Host channels are reset to ensure that they are ready for
  2043. * performing transfers.
  2044. */
  2045. static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
  2046. {
  2047. u32 hcfg, hfir, otgctl, usbcfg;
  2048. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  2049. /* Set HS/FS Timeout Calibration to 7 (max available value).
  2050. * The number of PHY clocks that the application programs in
  2051. * this field is added to the high/full speed interpacket timeout
  2052. * duration in the core to account for any additional delays
  2053. * introduced by the PHY. This can be required, because the delay
  2054. * introduced by the PHY in generating the linestate condition
  2055. * can vary from one PHY to another.
  2056. */
  2057. usbcfg = dwc2_readl(hsotg, GUSBCFG);
  2058. usbcfg |= GUSBCFG_TOUTCAL(7);
  2059. dwc2_writel(hsotg, usbcfg, GUSBCFG);
  2060. /* Restart the Phy Clock */
  2061. dwc2_writel(hsotg, 0, PCGCTL);
  2062. /* Initialize Host Configuration Register */
  2063. dwc2_init_fs_ls_pclk_sel(hsotg);
  2064. if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  2065. hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
  2066. hcfg = dwc2_readl(hsotg, HCFG);
  2067. hcfg |= HCFG_FSLSSUPP;
  2068. dwc2_writel(hsotg, hcfg, HCFG);
  2069. }
  2070. /*
  2071. * This bit allows dynamic reloading of the HFIR register during
  2072. * runtime. This bit needs to be programmed during initial configuration
  2073. * and its value must not be changed during runtime.
  2074. */
  2075. if (hsotg->params.reload_ctl) {
  2076. hfir = dwc2_readl(hsotg, HFIR);
  2077. hfir |= HFIR_RLDCTRL;
  2078. dwc2_writel(hsotg, hfir, HFIR);
  2079. }
  2080. if (hsotg->params.dma_desc_enable) {
  2081. u32 op_mode = hsotg->hw_params.op_mode;
  2082. if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
  2083. !hsotg->hw_params.dma_desc_enable ||
  2084. op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
  2085. op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
  2086. op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
  2087. dev_err(hsotg->dev,
  2088. "Hardware does not support descriptor DMA mode -\n");
  2089. dev_err(hsotg->dev,
  2090. "falling back to buffer DMA mode.\n");
  2091. hsotg->params.dma_desc_enable = false;
  2092. } else {
  2093. hcfg = dwc2_readl(hsotg, HCFG);
  2094. hcfg |= HCFG_DESCDMA;
  2095. dwc2_writel(hsotg, hcfg, HCFG);
  2096. }
  2097. }
  2098. /* Configure data FIFO sizes */
  2099. dwc2_config_fifos(hsotg);
  2100. /* TODO - check this */
  2101. /* Clear Host Set HNP Enable in the OTG Control Register */
  2102. otgctl = dwc2_readl(hsotg, GOTGCTL);
  2103. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  2104. dwc2_writel(hsotg, otgctl, GOTGCTL);
  2105. /* Make sure the FIFOs are flushed */
  2106. dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
  2107. dwc2_flush_rx_fifo(hsotg);
  2108. /* Clear Host Set HNP Enable in the OTG Control Register */
  2109. otgctl = dwc2_readl(hsotg, GOTGCTL);
  2110. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  2111. dwc2_writel(hsotg, otgctl, GOTGCTL);
  2112. if (!hsotg->params.dma_desc_enable) {
  2113. int num_channels, i;
  2114. u32 hcchar;
  2115. /* Flush out any leftover queued requests */
  2116. num_channels = hsotg->params.host_channels;
  2117. for (i = 0; i < num_channels; i++) {
  2118. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  2119. hcchar &= ~HCCHAR_CHENA;
  2120. hcchar |= HCCHAR_CHDIS;
  2121. hcchar &= ~HCCHAR_EPDIR;
  2122. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  2123. }
  2124. /* Halt all channels to put them into a known state */
  2125. for (i = 0; i < num_channels; i++) {
  2126. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  2127. hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
  2128. hcchar &= ~HCCHAR_EPDIR;
  2129. dwc2_writel(hsotg, hcchar, HCCHAR(i));
  2130. dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
  2131. __func__, i);
  2132. if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
  2133. HCCHAR_CHENA, 1000)) {
  2134. dev_warn(hsotg->dev, "Unable to clear enable on channel %d\n",
  2135. i);
  2136. }
  2137. }
  2138. }
  2139. /* Enable ACG feature in host mode, if supported */
  2140. dwc2_enable_acg(hsotg);
  2141. /* Turn on the vbus power */
  2142. dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
  2143. if (hsotg->op_state == OTG_STATE_A_HOST) {
  2144. u32 hprt0 = dwc2_read_hprt0(hsotg);
  2145. dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
  2146. !!(hprt0 & HPRT0_PWR));
  2147. if (!(hprt0 & HPRT0_PWR)) {
  2148. hprt0 |= HPRT0_PWR;
  2149. dwc2_writel(hsotg, hprt0, HPRT0);
  2150. }
  2151. }
  2152. dwc2_enable_host_interrupts(hsotg);
  2153. }
  2154. /*
  2155. * Initializes dynamic portions of the DWC_otg HCD state
  2156. *
  2157. * Must be called with interrupt disabled and spinlock held
  2158. */
  2159. static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
  2160. {
  2161. struct dwc2_host_chan *chan, *chan_tmp;
  2162. int num_channels;
  2163. int i;
  2164. hsotg->flags.d32 = 0;
  2165. hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
  2166. if (hsotg->params.uframe_sched) {
  2167. hsotg->available_host_channels =
  2168. hsotg->params.host_channels;
  2169. } else {
  2170. hsotg->non_periodic_channels = 0;
  2171. hsotg->periodic_channels = 0;
  2172. }
  2173. /*
  2174. * Put all channels in the free channel list and clean up channel
  2175. * states
  2176. */
  2177. list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
  2178. hc_list_entry)
  2179. list_del_init(&chan->hc_list_entry);
  2180. num_channels = hsotg->params.host_channels;
  2181. for (i = 0; i < num_channels; i++) {
  2182. chan = hsotg->hc_ptr_array[i];
  2183. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  2184. dwc2_hc_cleanup(hsotg, chan);
  2185. }
  2186. /* Initialize the DWC core for host mode operation */
  2187. dwc2_core_host_init(hsotg);
  2188. }
  2189. static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
  2190. struct dwc2_host_chan *chan,
  2191. struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  2192. {
  2193. int hub_addr, hub_port;
  2194. chan->do_split = 1;
  2195. chan->xact_pos = qtd->isoc_split_pos;
  2196. chan->complete_split = qtd->complete_split;
  2197. dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
  2198. chan->hub_addr = (u8)hub_addr;
  2199. chan->hub_port = (u8)hub_port;
  2200. }
  2201. static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
  2202. struct dwc2_host_chan *chan,
  2203. struct dwc2_qtd *qtd)
  2204. {
  2205. struct dwc2_hcd_urb *urb = qtd->urb;
  2206. struct dwc2_hcd_iso_packet_desc *frame_desc;
  2207. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  2208. case USB_ENDPOINT_XFER_CONTROL:
  2209. chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
  2210. switch (qtd->control_phase) {
  2211. case DWC2_CONTROL_SETUP:
  2212. dev_vdbg(hsotg->dev, " Control setup transaction\n");
  2213. chan->do_ping = 0;
  2214. chan->ep_is_in = 0;
  2215. chan->data_pid_start = DWC2_HC_PID_SETUP;
  2216. if (hsotg->params.host_dma)
  2217. chan->xfer_dma = urb->setup_dma;
  2218. else
  2219. chan->xfer_buf = urb->setup_packet;
  2220. chan->xfer_len = 8;
  2221. break;
  2222. case DWC2_CONTROL_DATA:
  2223. dev_vdbg(hsotg->dev, " Control data transaction\n");
  2224. chan->data_pid_start = qtd->data_toggle;
  2225. break;
  2226. case DWC2_CONTROL_STATUS:
  2227. /*
  2228. * Direction is opposite of data direction or IN if no
  2229. * data
  2230. */
  2231. dev_vdbg(hsotg->dev, " Control status transaction\n");
  2232. if (urb->length == 0)
  2233. chan->ep_is_in = 1;
  2234. else
  2235. chan->ep_is_in =
  2236. dwc2_hcd_is_pipe_out(&urb->pipe_info);
  2237. if (chan->ep_is_in)
  2238. chan->do_ping = 0;
  2239. chan->data_pid_start = DWC2_HC_PID_DATA1;
  2240. chan->xfer_len = 0;
  2241. if (hsotg->params.host_dma)
  2242. chan->xfer_dma = hsotg->status_buf_dma;
  2243. else
  2244. chan->xfer_buf = hsotg->status_buf;
  2245. break;
  2246. }
  2247. break;
  2248. case USB_ENDPOINT_XFER_BULK:
  2249. chan->ep_type = USB_ENDPOINT_XFER_BULK;
  2250. break;
  2251. case USB_ENDPOINT_XFER_INT:
  2252. chan->ep_type = USB_ENDPOINT_XFER_INT;
  2253. break;
  2254. case USB_ENDPOINT_XFER_ISOC:
  2255. chan->ep_type = USB_ENDPOINT_XFER_ISOC;
  2256. if (hsotg->params.dma_desc_enable)
  2257. break;
  2258. frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  2259. frame_desc->status = 0;
  2260. if (hsotg->params.host_dma) {
  2261. chan->xfer_dma = urb->dma;
  2262. chan->xfer_dma += frame_desc->offset +
  2263. qtd->isoc_split_offset;
  2264. } else {
  2265. chan->xfer_buf = urb->buf;
  2266. chan->xfer_buf += frame_desc->offset +
  2267. qtd->isoc_split_offset;
  2268. }
  2269. chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
  2270. if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
  2271. if (chan->xfer_len <= 188)
  2272. chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
  2273. else
  2274. chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
  2275. }
  2276. break;
  2277. }
  2278. }
  2279. static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
  2280. struct dwc2_qh *qh,
  2281. struct dwc2_host_chan *chan)
  2282. {
  2283. if (!hsotg->unaligned_cache ||
  2284. chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
  2285. return -ENOMEM;
  2286. if (!qh->dw_align_buf) {
  2287. qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
  2288. GFP_ATOMIC | GFP_DMA);
  2289. if (!qh->dw_align_buf)
  2290. return -ENOMEM;
  2291. }
  2292. qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
  2293. DWC2_KMEM_UNALIGNED_BUF_SIZE,
  2294. DMA_FROM_DEVICE);
  2295. if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
  2296. dev_err(hsotg->dev, "can't map align_buf\n");
  2297. chan->align_buf = 0;
  2298. return -EINVAL;
  2299. }
  2300. chan->align_buf = qh->dw_align_buf_dma;
  2301. return 0;
  2302. }
  2303. #define DWC2_USB_DMA_ALIGN 4
  2304. static void dwc2_free_dma_aligned_buffer(struct urb *urb)
  2305. {
  2306. void *stored_xfer_buffer;
  2307. size_t length;
  2308. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  2309. return;
  2310. /* Restore urb->transfer_buffer from the end of the allocated area */
  2311. memcpy(&stored_xfer_buffer, urb->transfer_buffer +
  2312. urb->transfer_buffer_length, sizeof(urb->transfer_buffer));
  2313. if (usb_urb_dir_in(urb)) {
  2314. if (usb_pipeisoc(urb->pipe))
  2315. length = urb->transfer_buffer_length;
  2316. else
  2317. length = urb->actual_length;
  2318. memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
  2319. }
  2320. kfree(urb->transfer_buffer);
  2321. urb->transfer_buffer = stored_xfer_buffer;
  2322. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  2323. }
  2324. static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  2325. {
  2326. void *kmalloc_ptr;
  2327. size_t kmalloc_size;
  2328. if (urb->num_sgs || urb->sg ||
  2329. urb->transfer_buffer_length == 0 ||
  2330. !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
  2331. return 0;
  2332. /*
  2333. * Allocate a buffer with enough padding for original transfer_buffer
  2334. * pointer. This allocation is guaranteed to be aligned properly for
  2335. * DMA
  2336. */
  2337. kmalloc_size = urb->transfer_buffer_length +
  2338. sizeof(urb->transfer_buffer);
  2339. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  2340. if (!kmalloc_ptr)
  2341. return -ENOMEM;
  2342. /*
  2343. * Position value of original urb->transfer_buffer pointer to the end
  2344. * of allocation for later referencing
  2345. */
  2346. memcpy(kmalloc_ptr + urb->transfer_buffer_length,
  2347. &urb->transfer_buffer, sizeof(urb->transfer_buffer));
  2348. if (usb_urb_dir_out(urb))
  2349. memcpy(kmalloc_ptr, urb->transfer_buffer,
  2350. urb->transfer_buffer_length);
  2351. urb->transfer_buffer = kmalloc_ptr;
  2352. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  2353. return 0;
  2354. }
  2355. static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  2356. gfp_t mem_flags)
  2357. {
  2358. int ret;
  2359. /* We assume setup_dma is always aligned; warn if not */
  2360. WARN_ON_ONCE(urb->setup_dma &&
  2361. (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
  2362. ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
  2363. if (ret)
  2364. return ret;
  2365. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2366. if (ret)
  2367. dwc2_free_dma_aligned_buffer(urb);
  2368. return ret;
  2369. }
  2370. static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  2371. {
  2372. usb_hcd_unmap_urb_for_dma(hcd, urb);
  2373. dwc2_free_dma_aligned_buffer(urb);
  2374. }
  2375. /**
  2376. * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
  2377. * channel and initializes the host channel to perform the transactions. The
  2378. * host channel is removed from the free list.
  2379. *
  2380. * @hsotg: The HCD state structure
  2381. * @qh: Transactions from the first QTD for this QH are selected and assigned
  2382. * to a free host channel
  2383. */
  2384. static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  2385. {
  2386. struct dwc2_host_chan *chan;
  2387. struct dwc2_hcd_urb *urb;
  2388. struct dwc2_qtd *qtd;
  2389. if (dbg_qh(qh))
  2390. dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
  2391. if (list_empty(&qh->qtd_list)) {
  2392. dev_dbg(hsotg->dev, "No QTDs in QH list\n");
  2393. return -ENOMEM;
  2394. }
  2395. if (list_empty(&hsotg->free_hc_list)) {
  2396. dev_dbg(hsotg->dev, "No free channel to assign\n");
  2397. return -ENOMEM;
  2398. }
  2399. chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
  2400. hc_list_entry);
  2401. /* Remove host channel from free list */
  2402. list_del_init(&chan->hc_list_entry);
  2403. qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
  2404. urb = qtd->urb;
  2405. qh->channel = chan;
  2406. qtd->in_process = 1;
  2407. /*
  2408. * Use usb_pipedevice to determine device address. This address is
  2409. * 0 before the SET_ADDRESS command and the correct address afterward.
  2410. */
  2411. chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
  2412. chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
  2413. chan->speed = qh->dev_speed;
  2414. chan->max_packet = dwc2_max_packet(qh->maxp);
  2415. chan->xfer_started = 0;
  2416. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  2417. chan->error_state = (qtd->error_count > 0);
  2418. chan->halt_on_queue = 0;
  2419. chan->halt_pending = 0;
  2420. chan->requests = 0;
  2421. /*
  2422. * The following values may be modified in the transfer type section
  2423. * below. The xfer_len value may be reduced when the transfer is
  2424. * started to accommodate the max widths of the XferSize and PktCnt
  2425. * fields in the HCTSIZn register.
  2426. */
  2427. chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
  2428. if (chan->ep_is_in)
  2429. chan->do_ping = 0;
  2430. else
  2431. chan->do_ping = qh->ping_state;
  2432. chan->data_pid_start = qh->data_toggle;
  2433. chan->multi_count = 1;
  2434. if (urb->actual_length > urb->length &&
  2435. !dwc2_hcd_is_pipe_in(&urb->pipe_info))
  2436. urb->actual_length = urb->length;
  2437. if (hsotg->params.host_dma)
  2438. chan->xfer_dma = urb->dma + urb->actual_length;
  2439. else
  2440. chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
  2441. chan->xfer_len = urb->length - urb->actual_length;
  2442. chan->xfer_count = 0;
  2443. /* Set the split attributes if required */
  2444. if (qh->do_split)
  2445. dwc2_hc_init_split(hsotg, chan, qtd, urb);
  2446. else
  2447. chan->do_split = 0;
  2448. /* Set the transfer attributes */
  2449. dwc2_hc_init_xfer(hsotg, chan, qtd);
  2450. /* For non-dword aligned buffers */
  2451. if (hsotg->params.host_dma && qh->do_split &&
  2452. chan->ep_is_in && (chan->xfer_dma & 0x3)) {
  2453. dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
  2454. if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
  2455. dev_err(hsotg->dev,
  2456. "Failed to allocate memory to handle non-aligned buffer\n");
  2457. /* Add channel back to free list */
  2458. chan->align_buf = 0;
  2459. chan->multi_count = 0;
  2460. list_add_tail(&chan->hc_list_entry,
  2461. &hsotg->free_hc_list);
  2462. qtd->in_process = 0;
  2463. qh->channel = NULL;
  2464. return -ENOMEM;
  2465. }
  2466. } else {
  2467. /*
  2468. * We assume that DMA is always aligned in non-split
  2469. * case or split out case. Warn if not.
  2470. */
  2471. WARN_ON_ONCE(hsotg->params.host_dma &&
  2472. (chan->xfer_dma & 0x3));
  2473. chan->align_buf = 0;
  2474. }
  2475. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  2476. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  2477. /*
  2478. * This value may be modified when the transfer is started
  2479. * to reflect the actual transfer length
  2480. */
  2481. chan->multi_count = dwc2_hb_mult(qh->maxp);
  2482. if (hsotg->params.dma_desc_enable) {
  2483. chan->desc_list_addr = qh->desc_list_dma;
  2484. chan->desc_list_sz = qh->desc_list_sz;
  2485. }
  2486. dwc2_hc_init(hsotg, chan);
  2487. chan->qh = qh;
  2488. return 0;
  2489. }
  2490. /**
  2491. * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
  2492. * schedule and assigns them to available host channels. Called from the HCD
  2493. * interrupt handler functions.
  2494. *
  2495. * @hsotg: The HCD state structure
  2496. *
  2497. * Return: The types of new transactions that were assigned to host channels
  2498. */
  2499. enum dwc2_transaction_type dwc2_hcd_select_transactions(
  2500. struct dwc2_hsotg *hsotg)
  2501. {
  2502. enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
  2503. struct list_head *qh_ptr;
  2504. struct dwc2_qh *qh;
  2505. int num_channels;
  2506. #ifdef DWC2_DEBUG_SOF
  2507. dev_vdbg(hsotg->dev, " Select Transactions\n");
  2508. #endif
  2509. /* Process entries in the periodic ready list */
  2510. qh_ptr = hsotg->periodic_sched_ready.next;
  2511. while (qh_ptr != &hsotg->periodic_sched_ready) {
  2512. if (list_empty(&hsotg->free_hc_list))
  2513. break;
  2514. if (hsotg->params.uframe_sched) {
  2515. if (hsotg->available_host_channels <= 1)
  2516. break;
  2517. hsotg->available_host_channels--;
  2518. }
  2519. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2520. if (dwc2_assign_and_init_hc(hsotg, qh))
  2521. break;
  2522. /*
  2523. * Move the QH from the periodic ready schedule to the
  2524. * periodic assigned schedule
  2525. */
  2526. qh_ptr = qh_ptr->next;
  2527. list_move_tail(&qh->qh_list_entry,
  2528. &hsotg->periodic_sched_assigned);
  2529. ret_val = DWC2_TRANSACTION_PERIODIC;
  2530. }
  2531. /*
  2532. * Process entries in the inactive portion of the non-periodic
  2533. * schedule. Some free host channels may not be used if they are
  2534. * reserved for periodic transfers.
  2535. */
  2536. num_channels = hsotg->params.host_channels;
  2537. qh_ptr = hsotg->non_periodic_sched_inactive.next;
  2538. while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
  2539. if (!hsotg->params.uframe_sched &&
  2540. hsotg->non_periodic_channels >= num_channels -
  2541. hsotg->periodic_channels)
  2542. break;
  2543. if (list_empty(&hsotg->free_hc_list))
  2544. break;
  2545. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2546. if (hsotg->params.uframe_sched) {
  2547. if (hsotg->available_host_channels < 1)
  2548. break;
  2549. hsotg->available_host_channels--;
  2550. }
  2551. if (dwc2_assign_and_init_hc(hsotg, qh))
  2552. break;
  2553. /*
  2554. * Move the QH from the non-periodic inactive schedule to the
  2555. * non-periodic active schedule
  2556. */
  2557. qh_ptr = qh_ptr->next;
  2558. list_move_tail(&qh->qh_list_entry,
  2559. &hsotg->non_periodic_sched_active);
  2560. if (ret_val == DWC2_TRANSACTION_NONE)
  2561. ret_val = DWC2_TRANSACTION_NON_PERIODIC;
  2562. else
  2563. ret_val = DWC2_TRANSACTION_ALL;
  2564. if (!hsotg->params.uframe_sched)
  2565. hsotg->non_periodic_channels++;
  2566. }
  2567. return ret_val;
  2568. }
  2569. /**
  2570. * dwc2_queue_transaction() - Attempts to queue a single transaction request for
  2571. * a host channel associated with either a periodic or non-periodic transfer
  2572. *
  2573. * @hsotg: The HCD state structure
  2574. * @chan: Host channel descriptor associated with either a periodic or
  2575. * non-periodic transfer
  2576. * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
  2577. * for periodic transfers or the non-periodic Tx FIFO
  2578. * for non-periodic transfers
  2579. *
  2580. * Return: 1 if a request is queued and more requests may be needed to
  2581. * complete the transfer, 0 if no more requests are required for this
  2582. * transfer, -1 if there is insufficient space in the Tx FIFO
  2583. *
  2584. * This function assumes that there is space available in the appropriate
  2585. * request queue. For an OUT transfer or SETUP transaction in Slave mode,
  2586. * it checks whether space is available in the appropriate Tx FIFO.
  2587. *
  2588. * Must be called with interrupt disabled and spinlock held
  2589. */
  2590. static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
  2591. struct dwc2_host_chan *chan,
  2592. u16 fifo_dwords_avail)
  2593. {
  2594. int retval = 0;
  2595. if (chan->do_split)
  2596. /* Put ourselves on the list to keep order straight */
  2597. list_move_tail(&chan->split_order_list_entry,
  2598. &hsotg->split_order);
  2599. if (hsotg->params.host_dma) {
  2600. if (hsotg->params.dma_desc_enable) {
  2601. if (!chan->xfer_started ||
  2602. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  2603. dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
  2604. chan->qh->ping_state = 0;
  2605. }
  2606. } else if (!chan->xfer_started) {
  2607. dwc2_hc_start_transfer(hsotg, chan);
  2608. chan->qh->ping_state = 0;
  2609. }
  2610. } else if (chan->halt_pending) {
  2611. /* Don't queue a request if the channel has been halted */
  2612. } else if (chan->halt_on_queue) {
  2613. dwc2_hc_halt(hsotg, chan, chan->halt_status);
  2614. } else if (chan->do_ping) {
  2615. if (!chan->xfer_started)
  2616. dwc2_hc_start_transfer(hsotg, chan);
  2617. } else if (!chan->ep_is_in ||
  2618. chan->data_pid_start == DWC2_HC_PID_SETUP) {
  2619. if ((fifo_dwords_avail * 4) >= chan->max_packet) {
  2620. if (!chan->xfer_started) {
  2621. dwc2_hc_start_transfer(hsotg, chan);
  2622. retval = 1;
  2623. } else {
  2624. retval = dwc2_hc_continue_transfer(hsotg, chan);
  2625. }
  2626. } else {
  2627. retval = -1;
  2628. }
  2629. } else {
  2630. if (!chan->xfer_started) {
  2631. dwc2_hc_start_transfer(hsotg, chan);
  2632. retval = 1;
  2633. } else {
  2634. retval = dwc2_hc_continue_transfer(hsotg, chan);
  2635. }
  2636. }
  2637. return retval;
  2638. }
  2639. /*
  2640. * Processes periodic channels for the next frame and queues transactions for
  2641. * these channels to the DWC_otg controller. After queueing transactions, the
  2642. * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  2643. * to queue as Periodic Tx FIFO or request queue space becomes available.
  2644. * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  2645. *
  2646. * Must be called with interrupt disabled and spinlock held
  2647. */
  2648. static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
  2649. {
  2650. struct list_head *qh_ptr;
  2651. struct dwc2_qh *qh;
  2652. u32 tx_status;
  2653. u32 fspcavail;
  2654. u32 gintmsk;
  2655. int status;
  2656. bool no_queue_space = false;
  2657. bool no_fifo_space = false;
  2658. u32 qspcavail;
  2659. /* If empty list then just adjust interrupt enables */
  2660. if (list_empty(&hsotg->periodic_sched_assigned))
  2661. goto exit;
  2662. if (dbg_perio())
  2663. dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
  2664. tx_status = dwc2_readl(hsotg, HPTXSTS);
  2665. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2666. TXSTS_QSPCAVAIL_SHIFT;
  2667. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2668. TXSTS_FSPCAVAIL_SHIFT;
  2669. if (dbg_perio()) {
  2670. dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
  2671. qspcavail);
  2672. dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
  2673. fspcavail);
  2674. }
  2675. qh_ptr = hsotg->periodic_sched_assigned.next;
  2676. while (qh_ptr != &hsotg->periodic_sched_assigned) {
  2677. tx_status = dwc2_readl(hsotg, HPTXSTS);
  2678. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2679. TXSTS_QSPCAVAIL_SHIFT;
  2680. if (qspcavail == 0) {
  2681. no_queue_space = true;
  2682. break;
  2683. }
  2684. qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
  2685. if (!qh->channel) {
  2686. qh_ptr = qh_ptr->next;
  2687. continue;
  2688. }
  2689. /* Make sure EP's TT buffer is clean before queueing qtds */
  2690. if (qh->tt_buffer_dirty) {
  2691. qh_ptr = qh_ptr->next;
  2692. continue;
  2693. }
  2694. /*
  2695. * Set a flag if we're queuing high-bandwidth in slave mode.
  2696. * The flag prevents any halts to get into the request queue in
  2697. * the middle of multiple high-bandwidth packets getting queued.
  2698. */
  2699. if (!hsotg->params.host_dma &&
  2700. qh->channel->multi_count > 1)
  2701. hsotg->queuing_high_bandwidth = 1;
  2702. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2703. TXSTS_FSPCAVAIL_SHIFT;
  2704. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  2705. if (status < 0) {
  2706. no_fifo_space = true;
  2707. break;
  2708. }
  2709. /*
  2710. * In Slave mode, stay on the current transfer until there is
  2711. * nothing more to do or the high-bandwidth request count is
  2712. * reached. In DMA mode, only need to queue one request. The
  2713. * controller automatically handles multiple packets for
  2714. * high-bandwidth transfers.
  2715. */
  2716. if (hsotg->params.host_dma || status == 0 ||
  2717. qh->channel->requests == qh->channel->multi_count) {
  2718. qh_ptr = qh_ptr->next;
  2719. /*
  2720. * Move the QH from the periodic assigned schedule to
  2721. * the periodic queued schedule
  2722. */
  2723. list_move_tail(&qh->qh_list_entry,
  2724. &hsotg->periodic_sched_queued);
  2725. /* done queuing high bandwidth */
  2726. hsotg->queuing_high_bandwidth = 0;
  2727. }
  2728. }
  2729. exit:
  2730. if (no_queue_space || no_fifo_space ||
  2731. (!hsotg->params.host_dma &&
  2732. !list_empty(&hsotg->periodic_sched_assigned))) {
  2733. /*
  2734. * May need to queue more transactions as the request
  2735. * queue or Tx FIFO empties. Enable the periodic Tx
  2736. * FIFO empty interrupt. (Always use the half-empty
  2737. * level to ensure that new requests are loaded as
  2738. * soon as possible.)
  2739. */
  2740. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2741. if (!(gintmsk & GINTSTS_PTXFEMP)) {
  2742. gintmsk |= GINTSTS_PTXFEMP;
  2743. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2744. }
  2745. } else {
  2746. /*
  2747. * Disable the Tx FIFO empty interrupt since there are
  2748. * no more transactions that need to be queued right
  2749. * now. This function is called from interrupt
  2750. * handlers to queue more transactions as transfer
  2751. * states change.
  2752. */
  2753. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2754. if (gintmsk & GINTSTS_PTXFEMP) {
  2755. gintmsk &= ~GINTSTS_PTXFEMP;
  2756. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2757. }
  2758. }
  2759. }
  2760. /*
  2761. * Processes active non-periodic channels and queues transactions for these
  2762. * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  2763. * FIFO Empty interrupt is enabled if there are more transactions to queue as
  2764. * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  2765. * FIFO Empty interrupt is disabled.
  2766. *
  2767. * Must be called with interrupt disabled and spinlock held
  2768. */
  2769. static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
  2770. {
  2771. struct list_head *orig_qh_ptr;
  2772. struct dwc2_qh *qh;
  2773. u32 tx_status;
  2774. u32 qspcavail;
  2775. u32 fspcavail;
  2776. u32 gintmsk;
  2777. int status;
  2778. int no_queue_space = 0;
  2779. int no_fifo_space = 0;
  2780. int more_to_do = 0;
  2781. dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
  2782. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2783. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2784. TXSTS_QSPCAVAIL_SHIFT;
  2785. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2786. TXSTS_FSPCAVAIL_SHIFT;
  2787. dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
  2788. qspcavail);
  2789. dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
  2790. fspcavail);
  2791. /*
  2792. * Keep track of the starting point. Skip over the start-of-list
  2793. * entry.
  2794. */
  2795. if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
  2796. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  2797. orig_qh_ptr = hsotg->non_periodic_qh_ptr;
  2798. /*
  2799. * Process once through the active list or until no more space is
  2800. * available in the request queue or the Tx FIFO
  2801. */
  2802. do {
  2803. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2804. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2805. TXSTS_QSPCAVAIL_SHIFT;
  2806. if (!hsotg->params.host_dma && qspcavail == 0) {
  2807. no_queue_space = 1;
  2808. break;
  2809. }
  2810. qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
  2811. qh_list_entry);
  2812. if (!qh->channel)
  2813. goto next;
  2814. /* Make sure EP's TT buffer is clean before queueing qtds */
  2815. if (qh->tt_buffer_dirty)
  2816. goto next;
  2817. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2818. TXSTS_FSPCAVAIL_SHIFT;
  2819. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  2820. if (status > 0) {
  2821. more_to_do = 1;
  2822. } else if (status < 0) {
  2823. no_fifo_space = 1;
  2824. break;
  2825. }
  2826. next:
  2827. /* Advance to next QH, skipping start-of-list entry */
  2828. hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
  2829. if (hsotg->non_periodic_qh_ptr ==
  2830. &hsotg->non_periodic_sched_active)
  2831. hsotg->non_periodic_qh_ptr =
  2832. hsotg->non_periodic_qh_ptr->next;
  2833. } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
  2834. if (!hsotg->params.host_dma) {
  2835. tx_status = dwc2_readl(hsotg, GNPTXSTS);
  2836. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2837. TXSTS_QSPCAVAIL_SHIFT;
  2838. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2839. TXSTS_FSPCAVAIL_SHIFT;
  2840. dev_vdbg(hsotg->dev,
  2841. " NP Tx Req Queue Space Avail (after queue): %d\n",
  2842. qspcavail);
  2843. dev_vdbg(hsotg->dev,
  2844. " NP Tx FIFO Space Avail (after queue): %d\n",
  2845. fspcavail);
  2846. if (more_to_do || no_queue_space || no_fifo_space) {
  2847. /*
  2848. * May need to queue more transactions as the request
  2849. * queue or Tx FIFO empties. Enable the non-periodic
  2850. * Tx FIFO empty interrupt. (Always use the half-empty
  2851. * level to ensure that new requests are loaded as
  2852. * soon as possible.)
  2853. */
  2854. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2855. gintmsk |= GINTSTS_NPTXFEMP;
  2856. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2857. } else {
  2858. /*
  2859. * Disable the Tx FIFO empty interrupt since there are
  2860. * no more transactions that need to be queued right
  2861. * now. This function is called from interrupt
  2862. * handlers to queue more transactions as transfer
  2863. * states change.
  2864. */
  2865. gintmsk = dwc2_readl(hsotg, GINTMSK);
  2866. gintmsk &= ~GINTSTS_NPTXFEMP;
  2867. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2868. }
  2869. }
  2870. }
  2871. /**
  2872. * dwc2_hcd_queue_transactions() - Processes the currently active host channels
  2873. * and queues transactions for these channels to the DWC_otg controller. Called
  2874. * from the HCD interrupt handler functions.
  2875. *
  2876. * @hsotg: The HCD state structure
  2877. * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
  2878. * or both)
  2879. *
  2880. * Must be called with interrupt disabled and spinlock held
  2881. */
  2882. void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
  2883. enum dwc2_transaction_type tr_type)
  2884. {
  2885. #ifdef DWC2_DEBUG_SOF
  2886. dev_vdbg(hsotg->dev, "Queue Transactions\n");
  2887. #endif
  2888. /* Process host channels associated with periodic transfers */
  2889. if (tr_type == DWC2_TRANSACTION_PERIODIC ||
  2890. tr_type == DWC2_TRANSACTION_ALL)
  2891. dwc2_process_periodic_channels(hsotg);
  2892. /* Process host channels associated with non-periodic transfers */
  2893. if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
  2894. tr_type == DWC2_TRANSACTION_ALL) {
  2895. if (!list_empty(&hsotg->non_periodic_sched_active)) {
  2896. dwc2_process_non_periodic_channels(hsotg);
  2897. } else {
  2898. /*
  2899. * Ensure NP Tx FIFO empty interrupt is disabled when
  2900. * there are no non-periodic transfers to process
  2901. */
  2902. u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
  2903. gintmsk &= ~GINTSTS_NPTXFEMP;
  2904. dwc2_writel(hsotg, gintmsk, GINTMSK);
  2905. }
  2906. }
  2907. }
  2908. static void dwc2_conn_id_status_change(struct work_struct *work)
  2909. {
  2910. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  2911. wf_otg);
  2912. u32 count = 0;
  2913. u32 gotgctl;
  2914. unsigned long flags;
  2915. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2916. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2917. dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
  2918. dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
  2919. !!(gotgctl & GOTGCTL_CONID_B));
  2920. /* B-Device connector (Device Mode) */
  2921. if (gotgctl & GOTGCTL_CONID_B) {
  2922. dwc2_vbus_supply_exit(hsotg);
  2923. /* Wait for switch to device mode */
  2924. dev_dbg(hsotg->dev, "connId B\n");
  2925. if (hsotg->bus_suspended) {
  2926. dev_info(hsotg->dev,
  2927. "Do port resume before switching to device mode\n");
  2928. dwc2_port_resume(hsotg);
  2929. }
  2930. while (!dwc2_is_device_mode(hsotg)) {
  2931. dev_info(hsotg->dev,
  2932. "Waiting for Peripheral Mode, Mode=%s\n",
  2933. dwc2_is_host_mode(hsotg) ? "Host" :
  2934. "Peripheral");
  2935. msleep(20);
  2936. /*
  2937. * Sometimes the initial GOTGCTRL read is wrong, so
  2938. * check it again and jump to host mode if that was
  2939. * the case.
  2940. */
  2941. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  2942. if (!(gotgctl & GOTGCTL_CONID_B))
  2943. goto host;
  2944. if (++count > 250)
  2945. break;
  2946. }
  2947. if (count > 250)
  2948. dev_err(hsotg->dev,
  2949. "Connection id status change timed out\n");
  2950. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  2951. dwc2_core_init(hsotg, false);
  2952. dwc2_enable_global_interrupts(hsotg);
  2953. spin_lock_irqsave(&hsotg->lock, flags);
  2954. dwc2_hsotg_core_init_disconnected(hsotg, false);
  2955. spin_unlock_irqrestore(&hsotg->lock, flags);
  2956. /* Enable ACG feature in device mode,if supported */
  2957. dwc2_enable_acg(hsotg);
  2958. dwc2_hsotg_core_connect(hsotg);
  2959. } else {
  2960. host:
  2961. /* A-Device connector (Host Mode) */
  2962. dev_dbg(hsotg->dev, "connId A\n");
  2963. while (!dwc2_is_host_mode(hsotg)) {
  2964. dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
  2965. dwc2_is_host_mode(hsotg) ?
  2966. "Host" : "Peripheral");
  2967. msleep(20);
  2968. if (++count > 250)
  2969. break;
  2970. }
  2971. if (count > 250)
  2972. dev_err(hsotg->dev,
  2973. "Connection id status change timed out\n");
  2974. spin_lock_irqsave(&hsotg->lock, flags);
  2975. dwc2_hsotg_disconnect(hsotg);
  2976. spin_unlock_irqrestore(&hsotg->lock, flags);
  2977. hsotg->op_state = OTG_STATE_A_HOST;
  2978. /* Initialize the Core for Host mode */
  2979. dwc2_core_init(hsotg, false);
  2980. dwc2_enable_global_interrupts(hsotg);
  2981. dwc2_hcd_start(hsotg);
  2982. }
  2983. }
  2984. static void dwc2_wakeup_detected(struct timer_list *t)
  2985. {
  2986. struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
  2987. u32 hprt0;
  2988. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2989. /*
  2990. * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  2991. * so that OPT tests pass with all PHYs.)
  2992. */
  2993. hprt0 = dwc2_read_hprt0(hsotg);
  2994. dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
  2995. hprt0 &= ~HPRT0_RES;
  2996. dwc2_writel(hsotg, hprt0, HPRT0);
  2997. dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
  2998. dwc2_readl(hsotg, HPRT0));
  2999. dwc2_hcd_rem_wakeup(hsotg);
  3000. hsotg->bus_suspended = false;
  3001. /* Change to L0 state */
  3002. hsotg->lx_state = DWC2_L0;
  3003. }
  3004. static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
  3005. {
  3006. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  3007. return hcd->self.b_hnp_enable;
  3008. }
  3009. /* Must NOT be called with interrupt disabled or spinlock held */
  3010. static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
  3011. {
  3012. unsigned long flags;
  3013. u32 hprt0;
  3014. u32 pcgctl;
  3015. u32 gotgctl;
  3016. dev_dbg(hsotg->dev, "%s()\n", __func__);
  3017. spin_lock_irqsave(&hsotg->lock, flags);
  3018. if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
  3019. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  3020. gotgctl |= GOTGCTL_HSTSETHNPEN;
  3021. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  3022. hsotg->op_state = OTG_STATE_A_SUSPEND;
  3023. }
  3024. hprt0 = dwc2_read_hprt0(hsotg);
  3025. hprt0 |= HPRT0_SUSP;
  3026. dwc2_writel(hsotg, hprt0, HPRT0);
  3027. hsotg->bus_suspended = true;
  3028. /*
  3029. * If power_down is supported, Phy clock will be suspended
  3030. * after registers are backuped.
  3031. */
  3032. if (!hsotg->params.power_down) {
  3033. /* Suspend the Phy Clock */
  3034. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3035. pcgctl |= PCGCTL_STOPPCLK;
  3036. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3037. udelay(10);
  3038. }
  3039. /* For HNP the bus must be suspended for at least 200ms */
  3040. if (dwc2_host_is_b_hnp_enabled(hsotg)) {
  3041. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3042. pcgctl &= ~PCGCTL_STOPPCLK;
  3043. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3044. spin_unlock_irqrestore(&hsotg->lock, flags);
  3045. msleep(200);
  3046. } else {
  3047. spin_unlock_irqrestore(&hsotg->lock, flags);
  3048. }
  3049. }
  3050. /* Must NOT be called with interrupt disabled or spinlock held */
  3051. static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
  3052. {
  3053. unsigned long flags;
  3054. u32 hprt0;
  3055. u32 pcgctl;
  3056. spin_lock_irqsave(&hsotg->lock, flags);
  3057. /*
  3058. * If power_down is supported, Phy clock is already resumed
  3059. * after registers restore.
  3060. */
  3061. if (!hsotg->params.power_down) {
  3062. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3063. pcgctl &= ~PCGCTL_STOPPCLK;
  3064. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3065. spin_unlock_irqrestore(&hsotg->lock, flags);
  3066. msleep(20);
  3067. spin_lock_irqsave(&hsotg->lock, flags);
  3068. }
  3069. hprt0 = dwc2_read_hprt0(hsotg);
  3070. hprt0 |= HPRT0_RES;
  3071. hprt0 &= ~HPRT0_SUSP;
  3072. dwc2_writel(hsotg, hprt0, HPRT0);
  3073. spin_unlock_irqrestore(&hsotg->lock, flags);
  3074. msleep(USB_RESUME_TIMEOUT);
  3075. spin_lock_irqsave(&hsotg->lock, flags);
  3076. hprt0 = dwc2_read_hprt0(hsotg);
  3077. hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
  3078. dwc2_writel(hsotg, hprt0, HPRT0);
  3079. hsotg->bus_suspended = false;
  3080. spin_unlock_irqrestore(&hsotg->lock, flags);
  3081. }
  3082. /* Handles hub class-specific requests */
  3083. static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
  3084. u16 wvalue, u16 windex, char *buf, u16 wlength)
  3085. {
  3086. struct usb_hub_descriptor *hub_desc;
  3087. int retval = 0;
  3088. u32 hprt0;
  3089. u32 port_status;
  3090. u32 speed;
  3091. u32 pcgctl;
  3092. switch (typereq) {
  3093. case ClearHubFeature:
  3094. dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
  3095. switch (wvalue) {
  3096. case C_HUB_LOCAL_POWER:
  3097. case C_HUB_OVER_CURRENT:
  3098. /* Nothing required here */
  3099. break;
  3100. default:
  3101. retval = -EINVAL;
  3102. dev_err(hsotg->dev,
  3103. "ClearHubFeature request %1xh unknown\n",
  3104. wvalue);
  3105. }
  3106. break;
  3107. case ClearPortFeature:
  3108. if (wvalue != USB_PORT_FEAT_L1)
  3109. if (!windex || windex > 1)
  3110. goto error;
  3111. switch (wvalue) {
  3112. case USB_PORT_FEAT_ENABLE:
  3113. dev_dbg(hsotg->dev,
  3114. "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  3115. hprt0 = dwc2_read_hprt0(hsotg);
  3116. hprt0 |= HPRT0_ENA;
  3117. dwc2_writel(hsotg, hprt0, HPRT0);
  3118. break;
  3119. case USB_PORT_FEAT_SUSPEND:
  3120. dev_dbg(hsotg->dev,
  3121. "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  3122. if (hsotg->bus_suspended) {
  3123. if (hsotg->hibernated)
  3124. dwc2_exit_hibernation(hsotg, 0, 0, 1);
  3125. else
  3126. dwc2_port_resume(hsotg);
  3127. }
  3128. break;
  3129. case USB_PORT_FEAT_POWER:
  3130. dev_dbg(hsotg->dev,
  3131. "ClearPortFeature USB_PORT_FEAT_POWER\n");
  3132. hprt0 = dwc2_read_hprt0(hsotg);
  3133. hprt0 &= ~HPRT0_PWR;
  3134. dwc2_writel(hsotg, hprt0, HPRT0);
  3135. break;
  3136. case USB_PORT_FEAT_INDICATOR:
  3137. dev_dbg(hsotg->dev,
  3138. "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  3139. /* Port indicator not supported */
  3140. break;
  3141. case USB_PORT_FEAT_C_CONNECTION:
  3142. /*
  3143. * Clears driver's internal Connect Status Change flag
  3144. */
  3145. dev_dbg(hsotg->dev,
  3146. "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  3147. hsotg->flags.b.port_connect_status_change = 0;
  3148. break;
  3149. case USB_PORT_FEAT_C_RESET:
  3150. /* Clears driver's internal Port Reset Change flag */
  3151. dev_dbg(hsotg->dev,
  3152. "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  3153. hsotg->flags.b.port_reset_change = 0;
  3154. break;
  3155. case USB_PORT_FEAT_C_ENABLE:
  3156. /*
  3157. * Clears the driver's internal Port Enable/Disable
  3158. * Change flag
  3159. */
  3160. dev_dbg(hsotg->dev,
  3161. "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  3162. hsotg->flags.b.port_enable_change = 0;
  3163. break;
  3164. case USB_PORT_FEAT_C_SUSPEND:
  3165. /*
  3166. * Clears the driver's internal Port Suspend Change
  3167. * flag, which is set when resume signaling on the host
  3168. * port is complete
  3169. */
  3170. dev_dbg(hsotg->dev,
  3171. "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  3172. hsotg->flags.b.port_suspend_change = 0;
  3173. break;
  3174. case USB_PORT_FEAT_C_PORT_L1:
  3175. dev_dbg(hsotg->dev,
  3176. "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
  3177. hsotg->flags.b.port_l1_change = 0;
  3178. break;
  3179. case USB_PORT_FEAT_C_OVER_CURRENT:
  3180. dev_dbg(hsotg->dev,
  3181. "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  3182. hsotg->flags.b.port_over_current_change = 0;
  3183. break;
  3184. default:
  3185. retval = -EINVAL;
  3186. dev_err(hsotg->dev,
  3187. "ClearPortFeature request %1xh unknown or unsupported\n",
  3188. wvalue);
  3189. }
  3190. break;
  3191. case GetHubDescriptor:
  3192. dev_dbg(hsotg->dev, "GetHubDescriptor\n");
  3193. hub_desc = (struct usb_hub_descriptor *)buf;
  3194. hub_desc->bDescLength = 9;
  3195. hub_desc->bDescriptorType = USB_DT_HUB;
  3196. hub_desc->bNbrPorts = 1;
  3197. hub_desc->wHubCharacteristics =
  3198. cpu_to_le16(HUB_CHAR_COMMON_LPSM |
  3199. HUB_CHAR_INDV_PORT_OCPM);
  3200. hub_desc->bPwrOn2PwrGood = 1;
  3201. hub_desc->bHubContrCurrent = 0;
  3202. hub_desc->u.hs.DeviceRemovable[0] = 0;
  3203. hub_desc->u.hs.DeviceRemovable[1] = 0xff;
  3204. break;
  3205. case GetHubStatus:
  3206. dev_dbg(hsotg->dev, "GetHubStatus\n");
  3207. memset(buf, 0, 4);
  3208. break;
  3209. case GetPortStatus:
  3210. dev_vdbg(hsotg->dev,
  3211. "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
  3212. hsotg->flags.d32);
  3213. if (!windex || windex > 1)
  3214. goto error;
  3215. port_status = 0;
  3216. if (hsotg->flags.b.port_connect_status_change)
  3217. port_status |= USB_PORT_STAT_C_CONNECTION << 16;
  3218. if (hsotg->flags.b.port_enable_change)
  3219. port_status |= USB_PORT_STAT_C_ENABLE << 16;
  3220. if (hsotg->flags.b.port_suspend_change)
  3221. port_status |= USB_PORT_STAT_C_SUSPEND << 16;
  3222. if (hsotg->flags.b.port_l1_change)
  3223. port_status |= USB_PORT_STAT_C_L1 << 16;
  3224. if (hsotg->flags.b.port_reset_change)
  3225. port_status |= USB_PORT_STAT_C_RESET << 16;
  3226. if (hsotg->flags.b.port_over_current_change) {
  3227. dev_warn(hsotg->dev, "Overcurrent change detected\n");
  3228. port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  3229. }
  3230. if (!hsotg->flags.b.port_connect_status) {
  3231. /*
  3232. * The port is disconnected, which means the core is
  3233. * either in device mode or it soon will be. Just
  3234. * return 0's for the remainder of the port status
  3235. * since the port register can't be read if the core
  3236. * is in device mode.
  3237. */
  3238. *(__le32 *)buf = cpu_to_le32(port_status);
  3239. break;
  3240. }
  3241. hprt0 = dwc2_readl(hsotg, HPRT0);
  3242. dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
  3243. if (hprt0 & HPRT0_CONNSTS)
  3244. port_status |= USB_PORT_STAT_CONNECTION;
  3245. if (hprt0 & HPRT0_ENA)
  3246. port_status |= USB_PORT_STAT_ENABLE;
  3247. if (hprt0 & HPRT0_SUSP)
  3248. port_status |= USB_PORT_STAT_SUSPEND;
  3249. if (hprt0 & HPRT0_OVRCURRACT)
  3250. port_status |= USB_PORT_STAT_OVERCURRENT;
  3251. if (hprt0 & HPRT0_RST)
  3252. port_status |= USB_PORT_STAT_RESET;
  3253. if (hprt0 & HPRT0_PWR)
  3254. port_status |= USB_PORT_STAT_POWER;
  3255. speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  3256. if (speed == HPRT0_SPD_HIGH_SPEED)
  3257. port_status |= USB_PORT_STAT_HIGH_SPEED;
  3258. else if (speed == HPRT0_SPD_LOW_SPEED)
  3259. port_status |= USB_PORT_STAT_LOW_SPEED;
  3260. if (hprt0 & HPRT0_TSTCTL_MASK)
  3261. port_status |= USB_PORT_STAT_TEST;
  3262. /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  3263. if (hsotg->params.dma_desc_fs_enable) {
  3264. /*
  3265. * Enable descriptor DMA only if a full speed
  3266. * device is connected.
  3267. */
  3268. if (hsotg->new_connection &&
  3269. ((port_status &
  3270. (USB_PORT_STAT_CONNECTION |
  3271. USB_PORT_STAT_HIGH_SPEED |
  3272. USB_PORT_STAT_LOW_SPEED)) ==
  3273. USB_PORT_STAT_CONNECTION)) {
  3274. u32 hcfg;
  3275. dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
  3276. hsotg->params.dma_desc_enable = true;
  3277. hcfg = dwc2_readl(hsotg, HCFG);
  3278. hcfg |= HCFG_DESCDMA;
  3279. dwc2_writel(hsotg, hcfg, HCFG);
  3280. hsotg->new_connection = false;
  3281. }
  3282. }
  3283. dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
  3284. *(__le32 *)buf = cpu_to_le32(port_status);
  3285. break;
  3286. case SetHubFeature:
  3287. dev_dbg(hsotg->dev, "SetHubFeature\n");
  3288. /* No HUB features supported */
  3289. break;
  3290. case SetPortFeature:
  3291. dev_dbg(hsotg->dev, "SetPortFeature\n");
  3292. if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
  3293. goto error;
  3294. if (!hsotg->flags.b.port_connect_status) {
  3295. /*
  3296. * The port is disconnected, which means the core is
  3297. * either in device mode or it soon will be. Just
  3298. * return without doing anything since the port
  3299. * register can't be written if the core is in device
  3300. * mode.
  3301. */
  3302. break;
  3303. }
  3304. switch (wvalue) {
  3305. case USB_PORT_FEAT_SUSPEND:
  3306. dev_dbg(hsotg->dev,
  3307. "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  3308. if (windex != hsotg->otg_port)
  3309. goto error;
  3310. if (hsotg->params.power_down == 2)
  3311. dwc2_enter_hibernation(hsotg, 1);
  3312. else
  3313. dwc2_port_suspend(hsotg, windex);
  3314. break;
  3315. case USB_PORT_FEAT_POWER:
  3316. dev_dbg(hsotg->dev,
  3317. "SetPortFeature - USB_PORT_FEAT_POWER\n");
  3318. hprt0 = dwc2_read_hprt0(hsotg);
  3319. hprt0 |= HPRT0_PWR;
  3320. dwc2_writel(hsotg, hprt0, HPRT0);
  3321. break;
  3322. case USB_PORT_FEAT_RESET:
  3323. if (hsotg->params.power_down == 2 &&
  3324. hsotg->hibernated)
  3325. dwc2_exit_hibernation(hsotg, 0, 1, 1);
  3326. hprt0 = dwc2_read_hprt0(hsotg);
  3327. dev_dbg(hsotg->dev,
  3328. "SetPortFeature - USB_PORT_FEAT_RESET\n");
  3329. pcgctl = dwc2_readl(hsotg, PCGCTL);
  3330. pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
  3331. dwc2_writel(hsotg, pcgctl, PCGCTL);
  3332. /* ??? Original driver does this */
  3333. dwc2_writel(hsotg, 0, PCGCTL);
  3334. hprt0 = dwc2_read_hprt0(hsotg);
  3335. /* Clear suspend bit if resetting from suspend state */
  3336. hprt0 &= ~HPRT0_SUSP;
  3337. /*
  3338. * When B-Host the Port reset bit is set in the Start
  3339. * HCD Callback function, so that the reset is started
  3340. * within 1ms of the HNP success interrupt
  3341. */
  3342. if (!dwc2_hcd_is_b_host(hsotg)) {
  3343. hprt0 |= HPRT0_PWR | HPRT0_RST;
  3344. dev_dbg(hsotg->dev,
  3345. "In host mode, hprt0=%08x\n", hprt0);
  3346. dwc2_writel(hsotg, hprt0, HPRT0);
  3347. }
  3348. /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  3349. msleep(50);
  3350. hprt0 &= ~HPRT0_RST;
  3351. dwc2_writel(hsotg, hprt0, HPRT0);
  3352. hsotg->lx_state = DWC2_L0; /* Now back to On state */
  3353. break;
  3354. case USB_PORT_FEAT_INDICATOR:
  3355. dev_dbg(hsotg->dev,
  3356. "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  3357. /* Not supported */
  3358. break;
  3359. case USB_PORT_FEAT_TEST:
  3360. hprt0 = dwc2_read_hprt0(hsotg);
  3361. dev_dbg(hsotg->dev,
  3362. "SetPortFeature - USB_PORT_FEAT_TEST\n");
  3363. hprt0 &= ~HPRT0_TSTCTL_MASK;
  3364. hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
  3365. dwc2_writel(hsotg, hprt0, HPRT0);
  3366. break;
  3367. default:
  3368. retval = -EINVAL;
  3369. dev_err(hsotg->dev,
  3370. "SetPortFeature %1xh unknown or unsupported\n",
  3371. wvalue);
  3372. break;
  3373. }
  3374. break;
  3375. default:
  3376. error:
  3377. retval = -EINVAL;
  3378. dev_dbg(hsotg->dev,
  3379. "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
  3380. typereq, windex, wvalue);
  3381. break;
  3382. }
  3383. return retval;
  3384. }
  3385. static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
  3386. {
  3387. int retval;
  3388. if (port != 1)
  3389. return -EINVAL;
  3390. retval = (hsotg->flags.b.port_connect_status_change ||
  3391. hsotg->flags.b.port_reset_change ||
  3392. hsotg->flags.b.port_enable_change ||
  3393. hsotg->flags.b.port_suspend_change ||
  3394. hsotg->flags.b.port_over_current_change);
  3395. if (retval) {
  3396. dev_dbg(hsotg->dev,
  3397. "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
  3398. dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
  3399. hsotg->flags.b.port_connect_status_change);
  3400. dev_dbg(hsotg->dev, " port_reset_change: %d\n",
  3401. hsotg->flags.b.port_reset_change);
  3402. dev_dbg(hsotg->dev, " port_enable_change: %d\n",
  3403. hsotg->flags.b.port_enable_change);
  3404. dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
  3405. hsotg->flags.b.port_suspend_change);
  3406. dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
  3407. hsotg->flags.b.port_over_current_change);
  3408. }
  3409. return retval;
  3410. }
  3411. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  3412. {
  3413. u32 hfnum = dwc2_readl(hsotg, HFNUM);
  3414. #ifdef DWC2_DEBUG_SOF
  3415. dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
  3416. (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
  3417. #endif
  3418. return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  3419. }
  3420. int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
  3421. {
  3422. u32 hprt = dwc2_readl(hsotg, HPRT0);
  3423. u32 hfir = dwc2_readl(hsotg, HFIR);
  3424. u32 hfnum = dwc2_readl(hsotg, HFNUM);
  3425. unsigned int us_per_frame;
  3426. unsigned int frame_number;
  3427. unsigned int remaining;
  3428. unsigned int interval;
  3429. unsigned int phy_clks;
  3430. /* High speed has 125 us per (micro) frame; others are 1 ms per */
  3431. us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
  3432. /* Extract fields */
  3433. frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  3434. remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
  3435. interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
  3436. /*
  3437. * Number of phy clocks since the last tick of the frame number after
  3438. * "us" has passed.
  3439. */
  3440. phy_clks = (interval - remaining) +
  3441. DIV_ROUND_UP(interval * us, us_per_frame);
  3442. return dwc2_frame_num_inc(frame_number, phy_clks / interval);
  3443. }
  3444. int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
  3445. {
  3446. return hsotg->op_state == OTG_STATE_B_HOST;
  3447. }
  3448. static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
  3449. int iso_desc_count,
  3450. gfp_t mem_flags)
  3451. {
  3452. struct dwc2_hcd_urb *urb;
  3453. u32 size = sizeof(*urb) + iso_desc_count *
  3454. sizeof(struct dwc2_hcd_iso_packet_desc);
  3455. urb = kzalloc(size, mem_flags);
  3456. if (urb)
  3457. urb->packet_count = iso_desc_count;
  3458. return urb;
  3459. }
  3460. static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
  3461. struct dwc2_hcd_urb *urb, u8 dev_addr,
  3462. u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
  3463. {
  3464. if (dbg_perio() ||
  3465. ep_type == USB_ENDPOINT_XFER_BULK ||
  3466. ep_type == USB_ENDPOINT_XFER_CONTROL)
  3467. dev_vdbg(hsotg->dev,
  3468. "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
  3469. dev_addr, ep_num, ep_dir, ep_type, mps);
  3470. urb->pipe_info.dev_addr = dev_addr;
  3471. urb->pipe_info.ep_num = ep_num;
  3472. urb->pipe_info.pipe_type = ep_type;
  3473. urb->pipe_info.pipe_dir = ep_dir;
  3474. urb->pipe_info.mps = mps;
  3475. }
  3476. /*
  3477. * NOTE: This function will be removed once the peripheral controller code
  3478. * is integrated and the driver is stable
  3479. */
  3480. void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
  3481. {
  3482. #ifdef DEBUG
  3483. struct dwc2_host_chan *chan;
  3484. struct dwc2_hcd_urb *urb;
  3485. struct dwc2_qtd *qtd;
  3486. int num_channels;
  3487. u32 np_tx_status;
  3488. u32 p_tx_status;
  3489. int i;
  3490. num_channels = hsotg->params.host_channels;
  3491. dev_dbg(hsotg->dev, "\n");
  3492. dev_dbg(hsotg->dev,
  3493. "************************************************************\n");
  3494. dev_dbg(hsotg->dev, "HCD State:\n");
  3495. dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
  3496. for (i = 0; i < num_channels; i++) {
  3497. chan = hsotg->hc_ptr_array[i];
  3498. dev_dbg(hsotg->dev, " Channel %d:\n", i);
  3499. dev_dbg(hsotg->dev,
  3500. " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  3501. chan->dev_addr, chan->ep_num, chan->ep_is_in);
  3502. dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
  3503. dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
  3504. dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
  3505. dev_dbg(hsotg->dev, " data_pid_start: %d\n",
  3506. chan->data_pid_start);
  3507. dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
  3508. dev_dbg(hsotg->dev, " xfer_started: %d\n",
  3509. chan->xfer_started);
  3510. dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
  3511. dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
  3512. (unsigned long)chan->xfer_dma);
  3513. dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
  3514. dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
  3515. dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
  3516. chan->halt_on_queue);
  3517. dev_dbg(hsotg->dev, " halt_pending: %d\n",
  3518. chan->halt_pending);
  3519. dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
  3520. dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
  3521. dev_dbg(hsotg->dev, " complete_split: %d\n",
  3522. chan->complete_split);
  3523. dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
  3524. dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
  3525. dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
  3526. dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
  3527. dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
  3528. if (chan->xfer_started) {
  3529. u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
  3530. hfnum = dwc2_readl(hsotg, HFNUM);
  3531. hcchar = dwc2_readl(hsotg, HCCHAR(i));
  3532. hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
  3533. hcint = dwc2_readl(hsotg, HCINT(i));
  3534. hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
  3535. dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
  3536. dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
  3537. dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
  3538. dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
  3539. dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
  3540. }
  3541. if (!(chan->xfer_started && chan->qh))
  3542. continue;
  3543. list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
  3544. if (!qtd->in_process)
  3545. break;
  3546. urb = qtd->urb;
  3547. dev_dbg(hsotg->dev, " URB Info:\n");
  3548. dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
  3549. qtd, urb);
  3550. if (urb) {
  3551. dev_dbg(hsotg->dev,
  3552. " Dev: %d, EP: %d %s\n",
  3553. dwc2_hcd_get_dev_addr(&urb->pipe_info),
  3554. dwc2_hcd_get_ep_num(&urb->pipe_info),
  3555. dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
  3556. "IN" : "OUT");
  3557. dev_dbg(hsotg->dev,
  3558. " Max packet size: %d\n",
  3559. dwc2_hcd_get_mps(&urb->pipe_info));
  3560. dev_dbg(hsotg->dev,
  3561. " transfer_buffer: %p\n",
  3562. urb->buf);
  3563. dev_dbg(hsotg->dev,
  3564. " transfer_dma: %08lx\n",
  3565. (unsigned long)urb->dma);
  3566. dev_dbg(hsotg->dev,
  3567. " transfer_buffer_length: %d\n",
  3568. urb->length);
  3569. dev_dbg(hsotg->dev, " actual_length: %d\n",
  3570. urb->actual_length);
  3571. }
  3572. }
  3573. }
  3574. dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
  3575. hsotg->non_periodic_channels);
  3576. dev_dbg(hsotg->dev, " periodic_channels: %d\n",
  3577. hsotg->periodic_channels);
  3578. dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
  3579. np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
  3580. dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
  3581. (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  3582. dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
  3583. (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  3584. p_tx_status = dwc2_readl(hsotg, HPTXSTS);
  3585. dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
  3586. (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
  3587. dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
  3588. (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
  3589. dwc2_dump_global_registers(hsotg);
  3590. dwc2_dump_host_registers(hsotg);
  3591. dev_dbg(hsotg->dev,
  3592. "************************************************************\n");
  3593. dev_dbg(hsotg->dev, "\n");
  3594. #endif
  3595. }
  3596. struct wrapper_priv_data {
  3597. struct dwc2_hsotg *hsotg;
  3598. };
  3599. /* Gets the dwc2_hsotg from a usb_hcd */
  3600. static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
  3601. {
  3602. struct wrapper_priv_data *p;
  3603. p = (struct wrapper_priv_data *)&hcd->hcd_priv;
  3604. return p->hsotg;
  3605. }
  3606. /**
  3607. * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
  3608. *
  3609. * This will get the dwc2_tt structure (and ttport) associated with the given
  3610. * context (which is really just a struct urb pointer).
  3611. *
  3612. * The first time this is called for a given TT we allocate memory for our
  3613. * structure. When everyone is done and has called dwc2_host_put_tt_info()
  3614. * then the refcount for the structure will go to 0 and we'll free it.
  3615. *
  3616. * @hsotg: The HCD state structure for the DWC OTG controller.
  3617. * @context: The priv pointer from a struct dwc2_hcd_urb.
  3618. * @mem_flags: Flags for allocating memory.
  3619. * @ttport: We'll return this device's port number here. That's used to
  3620. * reference into the bitmap if we're on a multi_tt hub.
  3621. *
  3622. * Return: a pointer to a struct dwc2_tt. Don't forget to call
  3623. * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
  3624. */
  3625. struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
  3626. gfp_t mem_flags, int *ttport)
  3627. {
  3628. struct urb *urb = context;
  3629. struct dwc2_tt *dwc_tt = NULL;
  3630. if (urb->dev->tt) {
  3631. *ttport = urb->dev->ttport;
  3632. dwc_tt = urb->dev->tt->hcpriv;
  3633. if (!dwc_tt) {
  3634. size_t bitmap_size;
  3635. /*
  3636. * For single_tt we need one schedule. For multi_tt
  3637. * we need one per port.
  3638. */
  3639. bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
  3640. sizeof(dwc_tt->periodic_bitmaps[0]);
  3641. if (urb->dev->tt->multi)
  3642. bitmap_size *= urb->dev->tt->hub->maxchild;
  3643. dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
  3644. mem_flags);
  3645. if (!dwc_tt)
  3646. return NULL;
  3647. dwc_tt->usb_tt = urb->dev->tt;
  3648. dwc_tt->usb_tt->hcpriv = dwc_tt;
  3649. }
  3650. dwc_tt->refcount++;
  3651. }
  3652. return dwc_tt;
  3653. }
  3654. /**
  3655. * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
  3656. *
  3657. * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
  3658. * of the structure are done.
  3659. *
  3660. * It's OK to call this with NULL.
  3661. *
  3662. * @hsotg: The HCD state structure for the DWC OTG controller.
  3663. * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
  3664. */
  3665. void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
  3666. {
  3667. /* Model kfree and make put of NULL a no-op */
  3668. if (!dwc_tt)
  3669. return;
  3670. WARN_ON(dwc_tt->refcount < 1);
  3671. dwc_tt->refcount--;
  3672. if (!dwc_tt->refcount) {
  3673. dwc_tt->usb_tt->hcpriv = NULL;
  3674. kfree(dwc_tt);
  3675. }
  3676. }
  3677. int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
  3678. {
  3679. struct urb *urb = context;
  3680. return urb->dev->speed;
  3681. }
  3682. static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  3683. struct urb *urb)
  3684. {
  3685. struct usb_bus *bus = hcd_to_bus(hcd);
  3686. if (urb->interval)
  3687. bus->bandwidth_allocated += bw / urb->interval;
  3688. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  3689. bus->bandwidth_isoc_reqs++;
  3690. else
  3691. bus->bandwidth_int_reqs++;
  3692. }
  3693. static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  3694. struct urb *urb)
  3695. {
  3696. struct usb_bus *bus = hcd_to_bus(hcd);
  3697. if (urb->interval)
  3698. bus->bandwidth_allocated -= bw / urb->interval;
  3699. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  3700. bus->bandwidth_isoc_reqs--;
  3701. else
  3702. bus->bandwidth_int_reqs--;
  3703. }
  3704. /*
  3705. * Sets the final status of an URB and returns it to the upper layer. Any
  3706. * required cleanup of the URB is performed.
  3707. *
  3708. * Must be called with interrupt disabled and spinlock held
  3709. */
  3710. void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  3711. int status)
  3712. {
  3713. struct urb *urb;
  3714. int i;
  3715. if (!qtd) {
  3716. dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
  3717. return;
  3718. }
  3719. if (!qtd->urb) {
  3720. dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
  3721. return;
  3722. }
  3723. urb = qtd->urb->priv;
  3724. if (!urb) {
  3725. dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
  3726. return;
  3727. }
  3728. urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
  3729. if (dbg_urb(urb))
  3730. dev_vdbg(hsotg->dev,
  3731. "%s: urb %p device %d ep %d-%s status %d actual %d\n",
  3732. __func__, urb, usb_pipedevice(urb->pipe),
  3733. usb_pipeendpoint(urb->pipe),
  3734. usb_pipein(urb->pipe) ? "IN" : "OUT", status,
  3735. urb->actual_length);
  3736. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  3737. urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
  3738. for (i = 0; i < urb->number_of_packets; ++i) {
  3739. urb->iso_frame_desc[i].actual_length =
  3740. dwc2_hcd_urb_get_iso_desc_actual_length(
  3741. qtd->urb, i);
  3742. urb->iso_frame_desc[i].status =
  3743. dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
  3744. }
  3745. }
  3746. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
  3747. for (i = 0; i < urb->number_of_packets; i++)
  3748. dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
  3749. i, urb->iso_frame_desc[i].status);
  3750. }
  3751. urb->status = status;
  3752. if (!status) {
  3753. if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  3754. urb->actual_length < urb->transfer_buffer_length)
  3755. urb->status = -EREMOTEIO;
  3756. }
  3757. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  3758. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  3759. struct usb_host_endpoint *ep = urb->ep;
  3760. if (ep)
  3761. dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
  3762. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  3763. urb);
  3764. }
  3765. usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
  3766. urb->hcpriv = NULL;
  3767. kfree(qtd->urb);
  3768. qtd->urb = NULL;
  3769. usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
  3770. }
  3771. /*
  3772. * Work queue function for starting the HCD when A-Cable is connected
  3773. */
  3774. static void dwc2_hcd_start_func(struct work_struct *work)
  3775. {
  3776. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3777. start_work.work);
  3778. dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
  3779. dwc2_host_start(hsotg);
  3780. }
  3781. /*
  3782. * Reset work queue function
  3783. */
  3784. static void dwc2_hcd_reset_func(struct work_struct *work)
  3785. {
  3786. struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
  3787. reset_work.work);
  3788. unsigned long flags;
  3789. u32 hprt0;
  3790. dev_dbg(hsotg->dev, "USB RESET function called\n");
  3791. spin_lock_irqsave(&hsotg->lock, flags);
  3792. hprt0 = dwc2_read_hprt0(hsotg);
  3793. hprt0 &= ~HPRT0_RST;
  3794. dwc2_writel(hsotg, hprt0, HPRT0);
  3795. hsotg->flags.b.port_reset_change = 1;
  3796. spin_unlock_irqrestore(&hsotg->lock, flags);
  3797. }
  3798. /*
  3799. * =========================================================================
  3800. * Linux HC Driver Functions
  3801. * =========================================================================
  3802. */
  3803. /*
  3804. * Initializes the DWC_otg controller and its root hub and prepares it for host
  3805. * mode operation. Activates the root port. Returns 0 on success and a negative
  3806. * error code on failure.
  3807. */
  3808. static int _dwc2_hcd_start(struct usb_hcd *hcd)
  3809. {
  3810. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3811. struct usb_bus *bus = hcd_to_bus(hcd);
  3812. unsigned long flags;
  3813. dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
  3814. spin_lock_irqsave(&hsotg->lock, flags);
  3815. hsotg->lx_state = DWC2_L0;
  3816. hcd->state = HC_STATE_RUNNING;
  3817. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3818. if (dwc2_is_device_mode(hsotg)) {
  3819. spin_unlock_irqrestore(&hsotg->lock, flags);
  3820. return 0; /* why 0 ?? */
  3821. }
  3822. dwc2_hcd_reinit(hsotg);
  3823. /* Initialize and connect root hub if one is not already attached */
  3824. if (bus->root_hub) {
  3825. dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
  3826. /* Inform the HUB driver to resume */
  3827. usb_hcd_resume_root_hub(hcd);
  3828. }
  3829. spin_unlock_irqrestore(&hsotg->lock, flags);
  3830. return dwc2_vbus_supply_init(hsotg);
  3831. }
  3832. /*
  3833. * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  3834. * stopped.
  3835. */
  3836. static void _dwc2_hcd_stop(struct usb_hcd *hcd)
  3837. {
  3838. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3839. unsigned long flags;
  3840. /* Turn off all host-specific interrupts */
  3841. dwc2_disable_host_interrupts(hsotg);
  3842. /* Wait for interrupt processing to finish */
  3843. synchronize_irq(hcd->irq);
  3844. spin_lock_irqsave(&hsotg->lock, flags);
  3845. /* Ensure hcd is disconnected */
  3846. dwc2_hcd_disconnect(hsotg, true);
  3847. dwc2_hcd_stop(hsotg);
  3848. hsotg->lx_state = DWC2_L3;
  3849. hcd->state = HC_STATE_HALT;
  3850. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3851. spin_unlock_irqrestore(&hsotg->lock, flags);
  3852. dwc2_vbus_supply_exit(hsotg);
  3853. usleep_range(1000, 3000);
  3854. }
  3855. static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
  3856. {
  3857. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3858. unsigned long flags;
  3859. int ret = 0;
  3860. u32 hprt0;
  3861. spin_lock_irqsave(&hsotg->lock, flags);
  3862. if (dwc2_is_device_mode(hsotg))
  3863. goto unlock;
  3864. if (hsotg->lx_state != DWC2_L0)
  3865. goto unlock;
  3866. if (!HCD_HW_ACCESSIBLE(hcd))
  3867. goto unlock;
  3868. if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
  3869. goto unlock;
  3870. if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL)
  3871. goto skip_power_saving;
  3872. /*
  3873. * Drive USB suspend and disable port Power
  3874. * if usb bus is not suspended.
  3875. */
  3876. if (!hsotg->bus_suspended) {
  3877. hprt0 = dwc2_read_hprt0(hsotg);
  3878. hprt0 |= HPRT0_SUSP;
  3879. hprt0 &= ~HPRT0_PWR;
  3880. dwc2_writel(hsotg, hprt0, HPRT0);
  3881. dwc2_vbus_supply_exit(hsotg);
  3882. }
  3883. /* Enter partial_power_down */
  3884. ret = dwc2_enter_partial_power_down(hsotg);
  3885. if (ret) {
  3886. if (ret != -ENOTSUPP)
  3887. dev_err(hsotg->dev,
  3888. "enter partial_power_down failed\n");
  3889. goto skip_power_saving;
  3890. }
  3891. /* Ask phy to be suspended */
  3892. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  3893. spin_unlock_irqrestore(&hsotg->lock, flags);
  3894. usb_phy_set_suspend(hsotg->uphy, true);
  3895. spin_lock_irqsave(&hsotg->lock, flags);
  3896. }
  3897. /* After entering partial_power_down, hardware is no more accessible */
  3898. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3899. skip_power_saving:
  3900. hsotg->lx_state = DWC2_L2;
  3901. unlock:
  3902. spin_unlock_irqrestore(&hsotg->lock, flags);
  3903. return ret;
  3904. }
  3905. static int _dwc2_hcd_resume(struct usb_hcd *hcd)
  3906. {
  3907. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3908. unsigned long flags;
  3909. int ret = 0;
  3910. spin_lock_irqsave(&hsotg->lock, flags);
  3911. if (dwc2_is_device_mode(hsotg))
  3912. goto unlock;
  3913. if (hsotg->lx_state != DWC2_L2)
  3914. goto unlock;
  3915. if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL) {
  3916. hsotg->lx_state = DWC2_L0;
  3917. goto unlock;
  3918. }
  3919. /*
  3920. * Set HW accessible bit before powering on the controller
  3921. * since an interrupt may rise.
  3922. */
  3923. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  3924. /*
  3925. * Enable power if not already done.
  3926. * This must not be spinlocked since duration
  3927. * of this call is unknown.
  3928. */
  3929. if (!IS_ERR_OR_NULL(hsotg->uphy)) {
  3930. spin_unlock_irqrestore(&hsotg->lock, flags);
  3931. usb_phy_set_suspend(hsotg->uphy, false);
  3932. spin_lock_irqsave(&hsotg->lock, flags);
  3933. }
  3934. /* Exit partial_power_down */
  3935. ret = dwc2_exit_partial_power_down(hsotg, true);
  3936. if (ret && (ret != -ENOTSUPP))
  3937. dev_err(hsotg->dev, "exit partial_power_down failed\n");
  3938. hsotg->lx_state = DWC2_L0;
  3939. spin_unlock_irqrestore(&hsotg->lock, flags);
  3940. if (hsotg->bus_suspended) {
  3941. spin_lock_irqsave(&hsotg->lock, flags);
  3942. hsotg->flags.b.port_suspend_change = 1;
  3943. spin_unlock_irqrestore(&hsotg->lock, flags);
  3944. dwc2_port_resume(hsotg);
  3945. } else {
  3946. dwc2_vbus_supply_init(hsotg);
  3947. /* Wait for controller to correctly update D+/D- level */
  3948. usleep_range(3000, 5000);
  3949. /*
  3950. * Clear Port Enable and Port Status changes.
  3951. * Enable Port Power.
  3952. */
  3953. dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
  3954. HPRT0_ENACHG, HPRT0);
  3955. /* Wait for controller to detect Port Connect */
  3956. usleep_range(5000, 7000);
  3957. }
  3958. return ret;
  3959. unlock:
  3960. spin_unlock_irqrestore(&hsotg->lock, flags);
  3961. return ret;
  3962. }
  3963. /* Returns the current frame number */
  3964. static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
  3965. {
  3966. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3967. return dwc2_hcd_get_frame_number(hsotg);
  3968. }
  3969. static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
  3970. char *fn_name)
  3971. {
  3972. #ifdef VERBOSE_DEBUG
  3973. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  3974. char *pipetype = NULL;
  3975. char *speed = NULL;
  3976. dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
  3977. dev_vdbg(hsotg->dev, " Device address: %d\n",
  3978. usb_pipedevice(urb->pipe));
  3979. dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
  3980. usb_pipeendpoint(urb->pipe),
  3981. usb_pipein(urb->pipe) ? "IN" : "OUT");
  3982. switch (usb_pipetype(urb->pipe)) {
  3983. case PIPE_CONTROL:
  3984. pipetype = "CONTROL";
  3985. break;
  3986. case PIPE_BULK:
  3987. pipetype = "BULK";
  3988. break;
  3989. case PIPE_INTERRUPT:
  3990. pipetype = "INTERRUPT";
  3991. break;
  3992. case PIPE_ISOCHRONOUS:
  3993. pipetype = "ISOCHRONOUS";
  3994. break;
  3995. }
  3996. dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
  3997. usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
  3998. "IN" : "OUT");
  3999. switch (urb->dev->speed) {
  4000. case USB_SPEED_HIGH:
  4001. speed = "HIGH";
  4002. break;
  4003. case USB_SPEED_FULL:
  4004. speed = "FULL";
  4005. break;
  4006. case USB_SPEED_LOW:
  4007. speed = "LOW";
  4008. break;
  4009. default:
  4010. speed = "UNKNOWN";
  4011. break;
  4012. }
  4013. dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
  4014. dev_vdbg(hsotg->dev, " Max packet size: %d\n",
  4015. usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  4016. dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
  4017. urb->transfer_buffer_length);
  4018. dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
  4019. urb->transfer_buffer, (unsigned long)urb->transfer_dma);
  4020. dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
  4021. urb->setup_packet, (unsigned long)urb->setup_dma);
  4022. dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
  4023. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  4024. int i;
  4025. for (i = 0; i < urb->number_of_packets; i++) {
  4026. dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
  4027. dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
  4028. urb->iso_frame_desc[i].offset,
  4029. urb->iso_frame_desc[i].length);
  4030. }
  4031. }
  4032. #endif
  4033. }
  4034. /*
  4035. * Starts processing a USB transfer request specified by a USB Request Block
  4036. * (URB). mem_flags indicates the type of memory allocation to use while
  4037. * processing this URB.
  4038. */
  4039. static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  4040. gfp_t mem_flags)
  4041. {
  4042. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4043. struct usb_host_endpoint *ep = urb->ep;
  4044. struct dwc2_hcd_urb *dwc2_urb;
  4045. int i;
  4046. int retval;
  4047. int alloc_bandwidth = 0;
  4048. u8 ep_type = 0;
  4049. u32 tflags = 0;
  4050. void *buf;
  4051. unsigned long flags;
  4052. struct dwc2_qh *qh;
  4053. bool qh_allocated = false;
  4054. struct dwc2_qtd *qtd;
  4055. if (dbg_urb(urb)) {
  4056. dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
  4057. dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
  4058. }
  4059. if (!ep)
  4060. return -EINVAL;
  4061. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
  4062. usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  4063. spin_lock_irqsave(&hsotg->lock, flags);
  4064. if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
  4065. alloc_bandwidth = 1;
  4066. spin_unlock_irqrestore(&hsotg->lock, flags);
  4067. }
  4068. switch (usb_pipetype(urb->pipe)) {
  4069. case PIPE_CONTROL:
  4070. ep_type = USB_ENDPOINT_XFER_CONTROL;
  4071. break;
  4072. case PIPE_ISOCHRONOUS:
  4073. ep_type = USB_ENDPOINT_XFER_ISOC;
  4074. break;
  4075. case PIPE_BULK:
  4076. ep_type = USB_ENDPOINT_XFER_BULK;
  4077. break;
  4078. case PIPE_INTERRUPT:
  4079. ep_type = USB_ENDPOINT_XFER_INT;
  4080. break;
  4081. }
  4082. dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
  4083. mem_flags);
  4084. if (!dwc2_urb)
  4085. return -ENOMEM;
  4086. dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
  4087. usb_pipeendpoint(urb->pipe), ep_type,
  4088. usb_pipein(urb->pipe),
  4089. usb_maxpacket(urb->dev, urb->pipe,
  4090. !(usb_pipein(urb->pipe))));
  4091. buf = urb->transfer_buffer;
  4092. if (hcd->self.uses_dma) {
  4093. if (!buf && (urb->transfer_dma & 3)) {
  4094. dev_err(hsotg->dev,
  4095. "%s: unaligned transfer with no transfer_buffer",
  4096. __func__);
  4097. retval = -EINVAL;
  4098. goto fail0;
  4099. }
  4100. }
  4101. if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  4102. tflags |= URB_GIVEBACK_ASAP;
  4103. if (urb->transfer_flags & URB_ZERO_PACKET)
  4104. tflags |= URB_SEND_ZERO_PACKET;
  4105. dwc2_urb->priv = urb;
  4106. dwc2_urb->buf = buf;
  4107. dwc2_urb->dma = urb->transfer_dma;
  4108. dwc2_urb->length = urb->transfer_buffer_length;
  4109. dwc2_urb->setup_packet = urb->setup_packet;
  4110. dwc2_urb->setup_dma = urb->setup_dma;
  4111. dwc2_urb->flags = tflags;
  4112. dwc2_urb->interval = urb->interval;
  4113. dwc2_urb->status = -EINPROGRESS;
  4114. for (i = 0; i < urb->number_of_packets; ++i)
  4115. dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
  4116. urb->iso_frame_desc[i].offset,
  4117. urb->iso_frame_desc[i].length);
  4118. urb->hcpriv = dwc2_urb;
  4119. qh = (struct dwc2_qh *)ep->hcpriv;
  4120. /* Create QH for the endpoint if it doesn't exist */
  4121. if (!qh) {
  4122. qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
  4123. if (!qh) {
  4124. retval = -ENOMEM;
  4125. goto fail0;
  4126. }
  4127. ep->hcpriv = qh;
  4128. qh_allocated = true;
  4129. }
  4130. qtd = kzalloc(sizeof(*qtd), mem_flags);
  4131. if (!qtd) {
  4132. retval = -ENOMEM;
  4133. goto fail1;
  4134. }
  4135. spin_lock_irqsave(&hsotg->lock, flags);
  4136. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  4137. if (retval)
  4138. goto fail2;
  4139. retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
  4140. if (retval)
  4141. goto fail3;
  4142. if (alloc_bandwidth) {
  4143. dwc2_allocate_bus_bandwidth(hcd,
  4144. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  4145. urb);
  4146. }
  4147. spin_unlock_irqrestore(&hsotg->lock, flags);
  4148. return 0;
  4149. fail3:
  4150. dwc2_urb->priv = NULL;
  4151. usb_hcd_unlink_urb_from_ep(hcd, urb);
  4152. if (qh_allocated && qh->channel && qh->channel->qh == qh)
  4153. qh->channel->qh = NULL;
  4154. fail2:
  4155. spin_unlock_irqrestore(&hsotg->lock, flags);
  4156. urb->hcpriv = NULL;
  4157. kfree(qtd);
  4158. qtd = NULL;
  4159. fail1:
  4160. if (qh_allocated) {
  4161. struct dwc2_qtd *qtd2, *qtd2_tmp;
  4162. ep->hcpriv = NULL;
  4163. dwc2_hcd_qh_unlink(hsotg, qh);
  4164. /* Free each QTD in the QH's QTD list */
  4165. list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
  4166. qtd_list_entry)
  4167. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
  4168. dwc2_hcd_qh_free(hsotg, qh);
  4169. }
  4170. fail0:
  4171. kfree(dwc2_urb);
  4172. return retval;
  4173. }
  4174. /*
  4175. * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
  4176. */
  4177. static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  4178. int status)
  4179. {
  4180. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4181. int rc;
  4182. unsigned long flags;
  4183. dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
  4184. dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
  4185. spin_lock_irqsave(&hsotg->lock, flags);
  4186. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  4187. if (rc)
  4188. goto out;
  4189. if (!urb->hcpriv) {
  4190. dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
  4191. goto out;
  4192. }
  4193. rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
  4194. usb_hcd_unlink_urb_from_ep(hcd, urb);
  4195. kfree(urb->hcpriv);
  4196. urb->hcpriv = NULL;
  4197. /* Higher layer software sets URB status */
  4198. spin_unlock(&hsotg->lock);
  4199. usb_hcd_giveback_urb(hcd, urb, status);
  4200. spin_lock(&hsotg->lock);
  4201. dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
  4202. dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
  4203. out:
  4204. spin_unlock_irqrestore(&hsotg->lock, flags);
  4205. return rc;
  4206. }
  4207. /*
  4208. * Frees resources in the DWC_otg controller related to a given endpoint. Also
  4209. * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  4210. * must already be dequeued.
  4211. */
  4212. static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
  4213. struct usb_host_endpoint *ep)
  4214. {
  4215. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4216. dev_dbg(hsotg->dev,
  4217. "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
  4218. ep->desc.bEndpointAddress, ep->hcpriv);
  4219. dwc2_hcd_endpoint_disable(hsotg, ep, 250);
  4220. }
  4221. /*
  4222. * Resets endpoint specific parameter values, in current version used to reset
  4223. * the data toggle (as a WA). This function can be called from usb_clear_halt
  4224. * routine.
  4225. */
  4226. static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
  4227. struct usb_host_endpoint *ep)
  4228. {
  4229. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4230. unsigned long flags;
  4231. dev_dbg(hsotg->dev,
  4232. "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
  4233. ep->desc.bEndpointAddress);
  4234. spin_lock_irqsave(&hsotg->lock, flags);
  4235. dwc2_hcd_endpoint_reset(hsotg, ep);
  4236. spin_unlock_irqrestore(&hsotg->lock, flags);
  4237. }
  4238. /*
  4239. * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  4240. * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  4241. * interrupt.
  4242. *
  4243. * This function is called by the USB core when an interrupt occurs
  4244. */
  4245. static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
  4246. {
  4247. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4248. return dwc2_handle_hcd_intr(hsotg);
  4249. }
  4250. /*
  4251. * Creates Status Change bitmap for the root hub and root port. The bitmap is
  4252. * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  4253. * is the status change indicator for the single root port. Returns 1 if either
  4254. * change indicator is 1, otherwise returns 0.
  4255. */
  4256. static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  4257. {
  4258. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4259. buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
  4260. return buf[0] != 0;
  4261. }
  4262. /* Handles hub class-specific requests */
  4263. static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
  4264. u16 windex, char *buf, u16 wlength)
  4265. {
  4266. int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
  4267. wvalue, windex, buf, wlength);
  4268. return retval;
  4269. }
  4270. /* Handles hub TT buffer clear completions */
  4271. static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
  4272. struct usb_host_endpoint *ep)
  4273. {
  4274. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4275. struct dwc2_qh *qh;
  4276. unsigned long flags;
  4277. qh = ep->hcpriv;
  4278. if (!qh)
  4279. return;
  4280. spin_lock_irqsave(&hsotg->lock, flags);
  4281. qh->tt_buffer_dirty = 0;
  4282. if (hsotg->flags.b.port_connect_status)
  4283. dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
  4284. spin_unlock_irqrestore(&hsotg->lock, flags);
  4285. }
  4286. /*
  4287. * HPRT0_SPD_HIGH_SPEED: high speed
  4288. * HPRT0_SPD_FULL_SPEED: full speed
  4289. */
  4290. static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
  4291. {
  4292. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4293. if (hsotg->params.speed == speed)
  4294. return;
  4295. hsotg->params.speed = speed;
  4296. queue_work(hsotg->wq_otg, &hsotg->wf_otg);
  4297. }
  4298. static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  4299. {
  4300. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4301. if (!hsotg->params.change_speed_quirk)
  4302. return;
  4303. /*
  4304. * On removal, set speed to default high-speed.
  4305. */
  4306. if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
  4307. udev->parent->speed < USB_SPEED_HIGH) {
  4308. dev_info(hsotg->dev, "Set speed to default high-speed\n");
  4309. dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
  4310. }
  4311. }
  4312. static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  4313. {
  4314. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  4315. if (!hsotg->params.change_speed_quirk)
  4316. return 0;
  4317. if (udev->speed == USB_SPEED_HIGH) {
  4318. dev_info(hsotg->dev, "Set speed to high-speed\n");
  4319. dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
  4320. } else if ((udev->speed == USB_SPEED_FULL ||
  4321. udev->speed == USB_SPEED_LOW)) {
  4322. /*
  4323. * Change speed setting to full-speed if there's
  4324. * a full-speed or low-speed device plugged in.
  4325. */
  4326. dev_info(hsotg->dev, "Set speed to full-speed\n");
  4327. dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
  4328. }
  4329. return 0;
  4330. }
  4331. static struct hc_driver dwc2_hc_driver = {
  4332. .description = "dwc2_hsotg",
  4333. .product_desc = "DWC OTG Controller",
  4334. .hcd_priv_size = sizeof(struct wrapper_priv_data),
  4335. .irq = _dwc2_hcd_irq,
  4336. .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
  4337. .start = _dwc2_hcd_start,
  4338. .stop = _dwc2_hcd_stop,
  4339. .urb_enqueue = _dwc2_hcd_urb_enqueue,
  4340. .urb_dequeue = _dwc2_hcd_urb_dequeue,
  4341. .endpoint_disable = _dwc2_hcd_endpoint_disable,
  4342. .endpoint_reset = _dwc2_hcd_endpoint_reset,
  4343. .get_frame_number = _dwc2_hcd_get_frame_number,
  4344. .hub_status_data = _dwc2_hcd_hub_status_data,
  4345. .hub_control = _dwc2_hcd_hub_control,
  4346. .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
  4347. .bus_suspend = _dwc2_hcd_suspend,
  4348. .bus_resume = _dwc2_hcd_resume,
  4349. .map_urb_for_dma = dwc2_map_urb_for_dma,
  4350. .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
  4351. };
  4352. /*
  4353. * Frees secondary storage associated with the dwc2_hsotg structure contained
  4354. * in the struct usb_hcd field
  4355. */
  4356. static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
  4357. {
  4358. u32 ahbcfg;
  4359. u32 dctl;
  4360. int i;
  4361. dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
  4362. /* Free memory for QH/QTD lists */
  4363. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
  4364. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
  4365. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
  4366. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
  4367. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
  4368. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
  4369. dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
  4370. /* Free memory for the host channels */
  4371. for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  4372. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  4373. if (chan) {
  4374. dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
  4375. i, chan);
  4376. hsotg->hc_ptr_array[i] = NULL;
  4377. kfree(chan);
  4378. }
  4379. }
  4380. if (hsotg->params.host_dma) {
  4381. if (hsotg->status_buf) {
  4382. dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
  4383. hsotg->status_buf,
  4384. hsotg->status_buf_dma);
  4385. hsotg->status_buf = NULL;
  4386. }
  4387. } else {
  4388. kfree(hsotg->status_buf);
  4389. hsotg->status_buf = NULL;
  4390. }
  4391. ahbcfg = dwc2_readl(hsotg, GAHBCFG);
  4392. /* Disable all interrupts */
  4393. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  4394. dwc2_writel(hsotg, ahbcfg, GAHBCFG);
  4395. dwc2_writel(hsotg, 0, GINTMSK);
  4396. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
  4397. dctl = dwc2_readl(hsotg, DCTL);
  4398. dctl |= DCTL_SFTDISCON;
  4399. dwc2_writel(hsotg, dctl, DCTL);
  4400. }
  4401. if (hsotg->wq_otg) {
  4402. if (!cancel_work_sync(&hsotg->wf_otg))
  4403. flush_workqueue(hsotg->wq_otg);
  4404. destroy_workqueue(hsotg->wq_otg);
  4405. }
  4406. del_timer(&hsotg->wkp_timer);
  4407. }
  4408. static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
  4409. {
  4410. /* Turn off all host-specific interrupts */
  4411. dwc2_disable_host_interrupts(hsotg);
  4412. dwc2_hcd_free(hsotg);
  4413. }
  4414. /*
  4415. * Initializes the HCD. This function allocates memory for and initializes the
  4416. * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
  4417. * USB bus with the core and calls the hc_driver->start() function. It returns
  4418. * a negative error on failure.
  4419. */
  4420. int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
  4421. {
  4422. struct platform_device *pdev = to_platform_device(hsotg->dev);
  4423. struct resource *res;
  4424. struct usb_hcd *hcd;
  4425. struct dwc2_host_chan *channel;
  4426. u32 hcfg;
  4427. int i, num_channels;
  4428. int retval;
  4429. if (usb_disabled())
  4430. return -ENODEV;
  4431. dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
  4432. retval = -ENOMEM;
  4433. hcfg = dwc2_readl(hsotg, HCFG);
  4434. dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
  4435. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4436. hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
  4437. sizeof(*hsotg->frame_num_array),
  4438. GFP_KERNEL);
  4439. if (!hsotg->frame_num_array)
  4440. goto error1;
  4441. hsotg->last_frame_num_array =
  4442. kcalloc(FRAME_NUM_ARRAY_SIZE,
  4443. sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
  4444. if (!hsotg->last_frame_num_array)
  4445. goto error1;
  4446. #endif
  4447. hsotg->last_frame_num = HFNUM_MAX_FRNUM;
  4448. /* Check if the bus driver or platform code has setup a dma_mask */
  4449. if (hsotg->params.host_dma &&
  4450. !hsotg->dev->dma_mask) {
  4451. dev_warn(hsotg->dev,
  4452. "dma_mask not set, disabling DMA\n");
  4453. hsotg->params.host_dma = false;
  4454. hsotg->params.dma_desc_enable = false;
  4455. }
  4456. /* Set device flags indicating whether the HCD supports DMA */
  4457. if (hsotg->params.host_dma) {
  4458. if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  4459. dev_warn(hsotg->dev, "can't set DMA mask\n");
  4460. if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
  4461. dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
  4462. }
  4463. if (hsotg->params.change_speed_quirk) {
  4464. dwc2_hc_driver.free_dev = dwc2_free_dev;
  4465. dwc2_hc_driver.reset_device = dwc2_reset_device;
  4466. }
  4467. hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
  4468. if (!hcd)
  4469. goto error1;
  4470. if (!hsotg->params.host_dma)
  4471. hcd->self.uses_dma = 0;
  4472. hcd->has_tt = 1;
  4473. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4474. hcd->rsrc_start = res->start;
  4475. hcd->rsrc_len = resource_size(res);
  4476. ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
  4477. hsotg->priv = hcd;
  4478. /*
  4479. * Disable the global interrupt until all the interrupt handlers are
  4480. * installed
  4481. */
  4482. dwc2_disable_global_interrupts(hsotg);
  4483. /* Initialize the DWC_otg core, and select the Phy type */
  4484. retval = dwc2_core_init(hsotg, true);
  4485. if (retval)
  4486. goto error2;
  4487. /* Create new workqueue and init work */
  4488. retval = -ENOMEM;
  4489. hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
  4490. if (!hsotg->wq_otg) {
  4491. dev_err(hsotg->dev, "Failed to create workqueue\n");
  4492. goto error2;
  4493. }
  4494. INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
  4495. timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
  4496. /* Initialize the non-periodic schedule */
  4497. INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
  4498. INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
  4499. INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
  4500. /* Initialize the periodic schedule */
  4501. INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
  4502. INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
  4503. INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
  4504. INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
  4505. INIT_LIST_HEAD(&hsotg->split_order);
  4506. /*
  4507. * Create a host channel descriptor for each host channel implemented
  4508. * in the controller. Initialize the channel descriptor array.
  4509. */
  4510. INIT_LIST_HEAD(&hsotg->free_hc_list);
  4511. num_channels = hsotg->params.host_channels;
  4512. memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
  4513. for (i = 0; i < num_channels; i++) {
  4514. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  4515. if (!channel)
  4516. goto error3;
  4517. channel->hc_num = i;
  4518. INIT_LIST_HEAD(&channel->split_order_list_entry);
  4519. hsotg->hc_ptr_array[i] = channel;
  4520. }
  4521. /* Initialize hsotg start work */
  4522. INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
  4523. /* Initialize port reset work */
  4524. INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
  4525. /*
  4526. * Allocate space for storing data on status transactions. Normally no
  4527. * data is sent, but this space acts as a bit bucket. This must be
  4528. * done after usb_add_hcd since that function allocates the DMA buffer
  4529. * pool.
  4530. */
  4531. if (hsotg->params.host_dma)
  4532. hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
  4533. DWC2_HCD_STATUS_BUF_SIZE,
  4534. &hsotg->status_buf_dma, GFP_KERNEL);
  4535. else
  4536. hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
  4537. GFP_KERNEL);
  4538. if (!hsotg->status_buf)
  4539. goto error3;
  4540. /*
  4541. * Create kmem caches to handle descriptor buffers in descriptor
  4542. * DMA mode.
  4543. * Alignment must be set to 512 bytes.
  4544. */
  4545. if (hsotg->params.dma_desc_enable ||
  4546. hsotg->params.dma_desc_fs_enable) {
  4547. hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
  4548. sizeof(struct dwc2_dma_desc) *
  4549. MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
  4550. NULL);
  4551. if (!hsotg->desc_gen_cache) {
  4552. dev_err(hsotg->dev,
  4553. "unable to create dwc2 generic desc cache\n");
  4554. /*
  4555. * Disable descriptor dma mode since it will not be
  4556. * usable.
  4557. */
  4558. hsotg->params.dma_desc_enable = false;
  4559. hsotg->params.dma_desc_fs_enable = false;
  4560. }
  4561. hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
  4562. sizeof(struct dwc2_dma_desc) *
  4563. MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
  4564. if (!hsotg->desc_hsisoc_cache) {
  4565. dev_err(hsotg->dev,
  4566. "unable to create dwc2 hs isoc desc cache\n");
  4567. kmem_cache_destroy(hsotg->desc_gen_cache);
  4568. /*
  4569. * Disable descriptor dma mode since it will not be
  4570. * usable.
  4571. */
  4572. hsotg->params.dma_desc_enable = false;
  4573. hsotg->params.dma_desc_fs_enable = false;
  4574. }
  4575. }
  4576. if (hsotg->params.host_dma) {
  4577. /*
  4578. * Create kmem caches to handle non-aligned buffer
  4579. * in Buffer DMA mode.
  4580. */
  4581. hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
  4582. DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
  4583. SLAB_CACHE_DMA, NULL);
  4584. if (!hsotg->unaligned_cache)
  4585. dev_err(hsotg->dev,
  4586. "unable to create dwc2 unaligned cache\n");
  4587. }
  4588. hsotg->otg_port = 1;
  4589. hsotg->frame_list = NULL;
  4590. hsotg->frame_list_dma = 0;
  4591. hsotg->periodic_qh_count = 0;
  4592. /* Initiate lx_state to L3 disconnected state */
  4593. hsotg->lx_state = DWC2_L3;
  4594. hcd->self.otg_port = hsotg->otg_port;
  4595. /* Don't support SG list at this point */
  4596. hcd->self.sg_tablesize = 0;
  4597. if (!IS_ERR_OR_NULL(hsotg->uphy))
  4598. otg_set_host(hsotg->uphy->otg, &hcd->self);
  4599. /*
  4600. * Finish generic HCD initialization and start the HCD. This function
  4601. * allocates the DMA buffer pool, registers the USB bus, requests the
  4602. * IRQ line, and calls hcd_start method.
  4603. */
  4604. retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
  4605. if (retval < 0)
  4606. goto error4;
  4607. device_wakeup_enable(hcd->self.controller);
  4608. dwc2_hcd_dump_state(hsotg);
  4609. dwc2_enable_global_interrupts(hsotg);
  4610. return 0;
  4611. error4:
  4612. kmem_cache_destroy(hsotg->unaligned_cache);
  4613. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  4614. kmem_cache_destroy(hsotg->desc_gen_cache);
  4615. error3:
  4616. dwc2_hcd_release(hsotg);
  4617. error2:
  4618. usb_put_hcd(hcd);
  4619. error1:
  4620. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4621. kfree(hsotg->last_frame_num_array);
  4622. kfree(hsotg->frame_num_array);
  4623. #endif
  4624. dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
  4625. return retval;
  4626. }
  4627. /*
  4628. * Removes the HCD.
  4629. * Frees memory and resources associated with the HCD and deregisters the bus.
  4630. */
  4631. void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
  4632. {
  4633. struct usb_hcd *hcd;
  4634. dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
  4635. hcd = dwc2_hsotg_to_hcd(hsotg);
  4636. dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
  4637. if (!hcd) {
  4638. dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
  4639. __func__);
  4640. return;
  4641. }
  4642. if (!IS_ERR_OR_NULL(hsotg->uphy))
  4643. otg_set_host(hsotg->uphy->otg, NULL);
  4644. usb_remove_hcd(hcd);
  4645. hsotg->priv = NULL;
  4646. kmem_cache_destroy(hsotg->unaligned_cache);
  4647. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  4648. kmem_cache_destroy(hsotg->desc_gen_cache);
  4649. dwc2_hcd_release(hsotg);
  4650. usb_put_hcd(hcd);
  4651. #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
  4652. kfree(hsotg->last_frame_num_array);
  4653. kfree(hsotg->frame_num_array);
  4654. #endif
  4655. }
  4656. /**
  4657. * dwc2_backup_host_registers() - Backup controller host registers.
  4658. * When suspending usb bus, registers needs to be backuped
  4659. * if controller power is disabled once suspended.
  4660. *
  4661. * @hsotg: Programming view of the DWC_otg controller
  4662. */
  4663. int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
  4664. {
  4665. struct dwc2_hregs_backup *hr;
  4666. int i;
  4667. dev_dbg(hsotg->dev, "%s\n", __func__);
  4668. /* Backup Host regs */
  4669. hr = &hsotg->hr_backup;
  4670. hr->hcfg = dwc2_readl(hsotg, HCFG);
  4671. hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
  4672. for (i = 0; i < hsotg->params.host_channels; ++i)
  4673. hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
  4674. hr->hprt0 = dwc2_read_hprt0(hsotg);
  4675. hr->hfir = dwc2_readl(hsotg, HFIR);
  4676. hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
  4677. hr->valid = true;
  4678. return 0;
  4679. }
  4680. /**
  4681. * dwc2_restore_host_registers() - Restore controller host registers.
  4682. * When resuming usb bus, device registers needs to be restored
  4683. * if controller power were disabled.
  4684. *
  4685. * @hsotg: Programming view of the DWC_otg controller
  4686. */
  4687. int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
  4688. {
  4689. struct dwc2_hregs_backup *hr;
  4690. int i;
  4691. dev_dbg(hsotg->dev, "%s\n", __func__);
  4692. /* Restore host regs */
  4693. hr = &hsotg->hr_backup;
  4694. if (!hr->valid) {
  4695. dev_err(hsotg->dev, "%s: no host registers to restore\n",
  4696. __func__);
  4697. return -EINVAL;
  4698. }
  4699. hr->valid = false;
  4700. dwc2_writel(hsotg, hr->hcfg, HCFG);
  4701. dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
  4702. for (i = 0; i < hsotg->params.host_channels; ++i)
  4703. dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
  4704. dwc2_writel(hsotg, hr->hprt0, HPRT0);
  4705. dwc2_writel(hsotg, hr->hfir, HFIR);
  4706. dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
  4707. hsotg->frame_number = 0;
  4708. return 0;
  4709. }
  4710. /**
  4711. * dwc2_host_enter_hibernation() - Put controller in Hibernation.
  4712. *
  4713. * @hsotg: Programming view of the DWC_otg controller
  4714. */
  4715. int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
  4716. {
  4717. unsigned long flags;
  4718. int ret = 0;
  4719. u32 hprt0;
  4720. u32 pcgcctl;
  4721. u32 gusbcfg;
  4722. u32 gpwrdn;
  4723. dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
  4724. ret = dwc2_backup_global_registers(hsotg);
  4725. if (ret) {
  4726. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  4727. __func__);
  4728. return ret;
  4729. }
  4730. ret = dwc2_backup_host_registers(hsotg);
  4731. if (ret) {
  4732. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  4733. __func__);
  4734. return ret;
  4735. }
  4736. /* Enter USB Suspend Mode */
  4737. hprt0 = dwc2_readl(hsotg, HPRT0);
  4738. hprt0 |= HPRT0_SUSP;
  4739. hprt0 &= ~HPRT0_ENA;
  4740. dwc2_writel(hsotg, hprt0, HPRT0);
  4741. /* Wait for the HPRT0.PrtSusp register field to be set */
  4742. if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
  4743. dev_warn(hsotg->dev, "Suspend wasn't generated\n");
  4744. /*
  4745. * We need to disable interrupts to prevent servicing of any IRQ
  4746. * during going to hibernation
  4747. */
  4748. spin_lock_irqsave(&hsotg->lock, flags);
  4749. hsotg->lx_state = DWC2_L2;
  4750. gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  4751. if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
  4752. /* ULPI interface */
  4753. /* Suspend the Phy Clock */
  4754. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4755. pcgcctl |= PCGCTL_STOPPCLK;
  4756. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4757. udelay(10);
  4758. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4759. gpwrdn |= GPWRDN_PMUACTV;
  4760. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4761. udelay(10);
  4762. } else {
  4763. /* UTMI+ Interface */
  4764. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4765. gpwrdn |= GPWRDN_PMUACTV;
  4766. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4767. udelay(10);
  4768. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  4769. pcgcctl |= PCGCTL_STOPPCLK;
  4770. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  4771. udelay(10);
  4772. }
  4773. /* Enable interrupts from wake up logic */
  4774. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4775. gpwrdn |= GPWRDN_PMUINTSEL;
  4776. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4777. udelay(10);
  4778. /* Unmask host mode interrupts in GPWRDN */
  4779. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4780. gpwrdn |= GPWRDN_DISCONN_DET_MSK;
  4781. gpwrdn |= GPWRDN_LNSTSCHG_MSK;
  4782. gpwrdn |= GPWRDN_STS_CHGINT_MSK;
  4783. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4784. udelay(10);
  4785. /* Enable Power Down Clamp */
  4786. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4787. gpwrdn |= GPWRDN_PWRDNCLMP;
  4788. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4789. udelay(10);
  4790. /* Switch off VDD */
  4791. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4792. gpwrdn |= GPWRDN_PWRDNSWTCH;
  4793. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4794. hsotg->hibernated = 1;
  4795. hsotg->bus_suspended = 1;
  4796. dev_dbg(hsotg->dev, "Host hibernation completed\n");
  4797. spin_unlock_irqrestore(&hsotg->lock, flags);
  4798. return ret;
  4799. }
  4800. /*
  4801. * dwc2_host_exit_hibernation()
  4802. *
  4803. * @hsotg: Programming view of the DWC_otg controller
  4804. * @rem_wakeup: indicates whether resume is initiated by Device or Host.
  4805. * @param reset: indicates whether resume is initiated by Reset.
  4806. *
  4807. * Return: non-zero if failed to enter to hibernation.
  4808. *
  4809. * This function is for exiting from Host mode hibernation by
  4810. * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  4811. */
  4812. int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
  4813. int reset)
  4814. {
  4815. u32 gpwrdn;
  4816. u32 hprt0;
  4817. int ret = 0;
  4818. struct dwc2_gregs_backup *gr;
  4819. struct dwc2_hregs_backup *hr;
  4820. gr = &hsotg->gr_backup;
  4821. hr = &hsotg->hr_backup;
  4822. dev_dbg(hsotg->dev,
  4823. "%s: called with rem_wakeup = %d reset = %d\n",
  4824. __func__, rem_wakeup, reset);
  4825. dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
  4826. hsotg->hibernated = 0;
  4827. /*
  4828. * This step is not described in functional spec but if not wait for
  4829. * this delay, mismatch interrupts occurred because just after restore
  4830. * core is in Device mode(gintsts.curmode == 0)
  4831. */
  4832. mdelay(100);
  4833. /* Clear all pending interupts */
  4834. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4835. /* De-assert Restore */
  4836. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4837. gpwrdn &= ~GPWRDN_RESTORE;
  4838. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4839. udelay(10);
  4840. /* Restore GUSBCFG, HCFG */
  4841. dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  4842. dwc2_writel(hsotg, hr->hcfg, HCFG);
  4843. /* De-assert Wakeup Logic */
  4844. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  4845. gpwrdn &= ~GPWRDN_PMUACTV;
  4846. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  4847. udelay(10);
  4848. hprt0 = hr->hprt0;
  4849. hprt0 |= HPRT0_PWR;
  4850. hprt0 &= ~HPRT0_ENA;
  4851. hprt0 &= ~HPRT0_SUSP;
  4852. dwc2_writel(hsotg, hprt0, HPRT0);
  4853. hprt0 = hr->hprt0;
  4854. hprt0 |= HPRT0_PWR;
  4855. hprt0 &= ~HPRT0_ENA;
  4856. hprt0 &= ~HPRT0_SUSP;
  4857. if (reset) {
  4858. hprt0 |= HPRT0_RST;
  4859. dwc2_writel(hsotg, hprt0, HPRT0);
  4860. /* Wait for Resume time and then program HPRT again */
  4861. mdelay(60);
  4862. hprt0 &= ~HPRT0_RST;
  4863. dwc2_writel(hsotg, hprt0, HPRT0);
  4864. } else {
  4865. hprt0 |= HPRT0_RES;
  4866. dwc2_writel(hsotg, hprt0, HPRT0);
  4867. /* Wait for Resume time and then program HPRT again */
  4868. mdelay(100);
  4869. hprt0 &= ~HPRT0_RES;
  4870. dwc2_writel(hsotg, hprt0, HPRT0);
  4871. }
  4872. /* Clear all interrupt status */
  4873. hprt0 = dwc2_readl(hsotg, HPRT0);
  4874. hprt0 |= HPRT0_CONNDET;
  4875. hprt0 |= HPRT0_ENACHG;
  4876. hprt0 &= ~HPRT0_ENA;
  4877. dwc2_writel(hsotg, hprt0, HPRT0);
  4878. hprt0 = dwc2_readl(hsotg, HPRT0);
  4879. /* Clear all pending interupts */
  4880. dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  4881. /* Restore global registers */
  4882. ret = dwc2_restore_global_registers(hsotg);
  4883. if (ret) {
  4884. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  4885. __func__);
  4886. return ret;
  4887. }
  4888. /* Restore host registers */
  4889. ret = dwc2_restore_host_registers(hsotg);
  4890. if (ret) {
  4891. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  4892. __func__);
  4893. return ret;
  4894. }
  4895. dwc2_hcd_rem_wakeup(hsotg);
  4896. hsotg->hibernated = 0;
  4897. hsotg->bus_suspended = 0;
  4898. hsotg->lx_state = DWC2_L0;
  4899. dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
  4900. return ret;
  4901. }