hcd.c 150 KB

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