pci.c 170 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/cma.h>
  7. #include <linux/completion.h>
  8. #include <linux/io.h>
  9. #include <linux/irq.h>
  10. #include <linux/memblock.h>
  11. #include <linux/module.h>
  12. #include <linux/msi.h>
  13. #include <linux/of.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/of_reserved_mem.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/suspend.h>
  18. #include <linux/version.h>
  19. #include "main.h"
  20. #include "bus.h"
  21. #include "debug.h"
  22. #include "pci.h"
  23. #include "reg.h"
  24. #define PCI_LINK_UP 1
  25. #define PCI_LINK_DOWN 0
  26. #define SAVE_PCI_CONFIG_SPACE 1
  27. #define RESTORE_PCI_CONFIG_SPACE 0
  28. #define PM_OPTIONS_DEFAULT 0
  29. #define PCI_BAR_NUM 0
  30. #define PCI_INVALID_READ(val) ((val) == U32_MAX)
  31. #define PCI_DMA_MASK_32_BIT DMA_BIT_MASK(32)
  32. #define PCI_DMA_MASK_36_BIT DMA_BIT_MASK(36)
  33. #define PCI_DMA_MASK_64_BIT DMA_BIT_MASK(64)
  34. #define MHI_NODE_NAME "qcom,mhi"
  35. #define MHI_MSI_NAME "MHI"
  36. #define QCA6390_PATH_PREFIX "qca6390/"
  37. #define QCA6490_PATH_PREFIX "qca6490/"
  38. #define KIWI_PATH_PREFIX "kiwi/"
  39. #define DEFAULT_PHY_M3_FILE_NAME "m3.bin"
  40. #define DEFAULT_PHY_UCODE_FILE_NAME "phy_ucode.elf"
  41. #define PHY_UCODE_V2_FILE_NAME "phy_ucode20.elf"
  42. #define DEFAULT_FW_FILE_NAME "amss.bin"
  43. #define FW_V2_FILE_NAME "amss20.bin"
  44. #define DEVICE_MAJOR_VERSION_MASK 0xF
  45. #define WAKE_MSI_NAME "WAKE"
  46. #define DEV_RDDM_TIMEOUT 5000
  47. #define WAKE_EVENT_TIMEOUT 5000
  48. #ifdef CONFIG_CNSS_EMULATION
  49. #define EMULATION_HW 1
  50. #else
  51. #define EMULATION_HW 0
  52. #endif
  53. #define RAMDUMP_SIZE_DEFAULT 0x420000
  54. #define DEVICE_RDDM_COOKIE 0xCAFECACE
  55. static DEFINE_SPINLOCK(pci_link_down_lock);
  56. static DEFINE_SPINLOCK(pci_reg_window_lock);
  57. static DEFINE_SPINLOCK(time_sync_lock);
  58. #define MHI_TIMEOUT_OVERWRITE_MS (plat_priv->ctrl_params.mhi_timeout)
  59. #define MHI_M2_TIMEOUT_MS (plat_priv->ctrl_params.mhi_m2_timeout)
  60. #define WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US 1000
  61. #define WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US 2000
  62. #define FORCE_WAKE_DELAY_MIN_US 4000
  63. #define FORCE_WAKE_DELAY_MAX_US 6000
  64. #define FORCE_WAKE_DELAY_TIMEOUT_US 60000
  65. #define LINK_TRAINING_RETRY_MAX_TIMES 3
  66. #define LINK_TRAINING_RETRY_DELAY_MS 500
  67. #define MHI_SUSPEND_RETRY_MAX_TIMES 3
  68. #define MHI_SUSPEND_RETRY_DELAY_US 5000
  69. #define BOOT_DEBUG_TIMEOUT_MS 7000
  70. #define HANG_DATA_LENGTH 384
  71. #define HST_HANG_DATA_OFFSET ((3 * 1024 * 1024) - HANG_DATA_LENGTH)
  72. #define HSP_HANG_DATA_OFFSET ((2 * 1024 * 1024) - HANG_DATA_LENGTH)
  73. static const struct mhi_channel_config cnss_mhi_channels[] = {
  74. {
  75. .num = 0,
  76. .name = "LOOPBACK",
  77. .num_elements = 32,
  78. .event_ring = 1,
  79. .dir = DMA_TO_DEVICE,
  80. .ee_mask = 0x4,
  81. .pollcfg = 0,
  82. .doorbell = MHI_DB_BRST_DISABLE,
  83. .lpm_notify = false,
  84. .offload_channel = false,
  85. .doorbell_mode_switch = false,
  86. .auto_queue = false,
  87. },
  88. {
  89. .num = 1,
  90. .name = "LOOPBACK",
  91. .num_elements = 32,
  92. .event_ring = 1,
  93. .dir = DMA_FROM_DEVICE,
  94. .ee_mask = 0x4,
  95. .pollcfg = 0,
  96. .doorbell = MHI_DB_BRST_DISABLE,
  97. .lpm_notify = false,
  98. .offload_channel = false,
  99. .doorbell_mode_switch = false,
  100. .auto_queue = false,
  101. },
  102. {
  103. .num = 4,
  104. .name = "DIAG",
  105. .num_elements = 64,
  106. .event_ring = 1,
  107. .dir = DMA_TO_DEVICE,
  108. .ee_mask = 0x4,
  109. .pollcfg = 0,
  110. .doorbell = MHI_DB_BRST_DISABLE,
  111. .lpm_notify = false,
  112. .offload_channel = false,
  113. .doorbell_mode_switch = false,
  114. .auto_queue = false,
  115. },
  116. {
  117. .num = 5,
  118. .name = "DIAG",
  119. .num_elements = 64,
  120. .event_ring = 1,
  121. .dir = DMA_FROM_DEVICE,
  122. .ee_mask = 0x4,
  123. .pollcfg = 0,
  124. .doorbell = MHI_DB_BRST_DISABLE,
  125. .lpm_notify = false,
  126. .offload_channel = false,
  127. .doorbell_mode_switch = false,
  128. .auto_queue = false,
  129. },
  130. {
  131. .num = 20,
  132. .name = "IPCR",
  133. .num_elements = 64,
  134. .event_ring = 1,
  135. .dir = DMA_TO_DEVICE,
  136. .ee_mask = 0x4,
  137. .pollcfg = 0,
  138. .doorbell = MHI_DB_BRST_DISABLE,
  139. .lpm_notify = false,
  140. .offload_channel = false,
  141. .doorbell_mode_switch = false,
  142. .auto_queue = false,
  143. },
  144. {
  145. .num = 21,
  146. .name = "IPCR",
  147. .num_elements = 64,
  148. .event_ring = 1,
  149. .dir = DMA_FROM_DEVICE,
  150. .ee_mask = 0x4,
  151. .pollcfg = 0,
  152. .doorbell = MHI_DB_BRST_DISABLE,
  153. .lpm_notify = false,
  154. .offload_channel = false,
  155. .doorbell_mode_switch = false,
  156. .auto_queue = true,
  157. },
  158. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  159. {
  160. .num = 50,
  161. .name = "ADSP_0",
  162. .num_elements = 64,
  163. .event_ring = 3,
  164. .dir = DMA_BIDIRECTIONAL,
  165. .ee_mask = 0x4,
  166. .pollcfg = 0,
  167. .doorbell = MHI_DB_BRST_DISABLE,
  168. .lpm_notify = false,
  169. .offload_channel = true,
  170. .doorbell_mode_switch = false,
  171. .auto_queue = false,
  172. },
  173. {
  174. .num = 51,
  175. .name = "ADSP_1",
  176. .num_elements = 64,
  177. .event_ring = 3,
  178. .dir = DMA_BIDIRECTIONAL,
  179. .ee_mask = 0x4,
  180. .pollcfg = 0,
  181. .doorbell = MHI_DB_BRST_DISABLE,
  182. .lpm_notify = false,
  183. .offload_channel = true,
  184. .doorbell_mode_switch = false,
  185. .auto_queue = false,
  186. },
  187. {
  188. .num = 70,
  189. .name = "ADSP_2",
  190. .num_elements = 64,
  191. .event_ring = 3,
  192. .dir = DMA_BIDIRECTIONAL,
  193. .ee_mask = 0x4,
  194. .pollcfg = 0,
  195. .doorbell = MHI_DB_BRST_DISABLE,
  196. .lpm_notify = false,
  197. .offload_channel = true,
  198. .doorbell_mode_switch = false,
  199. .auto_queue = false,
  200. },
  201. {
  202. .num = 71,
  203. .name = "ADSP_3",
  204. .num_elements = 64,
  205. .event_ring = 3,
  206. .dir = DMA_BIDIRECTIONAL,
  207. .ee_mask = 0x4,
  208. .pollcfg = 0,
  209. .doorbell = MHI_DB_BRST_DISABLE,
  210. .lpm_notify = false,
  211. .offload_channel = true,
  212. .doorbell_mode_switch = false,
  213. .auto_queue = false,
  214. },
  215. #endif
  216. };
  217. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0))
  218. static struct mhi_event_config cnss_mhi_events[] = {
  219. #else
  220. static const struct mhi_event_config cnss_mhi_events[] = {
  221. #endif
  222. {
  223. .num_elements = 32,
  224. .irq_moderation_ms = 0,
  225. .irq = 1,
  226. .mode = MHI_DB_BRST_DISABLE,
  227. .data_type = MHI_ER_CTRL,
  228. .priority = 0,
  229. .hardware_event = false,
  230. .client_managed = false,
  231. .offload_channel = false,
  232. },
  233. {
  234. .num_elements = 256,
  235. .irq_moderation_ms = 0,
  236. .irq = 2,
  237. .mode = MHI_DB_BRST_DISABLE,
  238. .priority = 1,
  239. .hardware_event = false,
  240. .client_managed = false,
  241. .offload_channel = false,
  242. },
  243. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  244. {
  245. .num_elements = 32,
  246. .irq_moderation_ms = 0,
  247. .irq = 1,
  248. .mode = MHI_DB_BRST_DISABLE,
  249. .data_type = MHI_ER_BW_SCALE,
  250. .priority = 2,
  251. .hardware_event = false,
  252. .client_managed = false,
  253. .offload_channel = false,
  254. },
  255. #endif
  256. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  257. {
  258. .num_elements = 256,
  259. .irq_moderation_ms = 0,
  260. .irq = 2,
  261. .mode = MHI_DB_BRST_DISABLE,
  262. .data_type = MHI_ER_DATA,
  263. .priority = 1,
  264. .hardware_event = false,
  265. .client_managed = true,
  266. .offload_channel = true,
  267. },
  268. #endif
  269. };
  270. static const struct mhi_controller_config cnss_mhi_config = {
  271. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  272. .max_channels = 72,
  273. #else
  274. .max_channels = 32,
  275. #endif
  276. .timeout_ms = 10000,
  277. .use_bounce_buf = false,
  278. .buf_len = 0x8000,
  279. .num_channels = ARRAY_SIZE(cnss_mhi_channels),
  280. .ch_cfg = cnss_mhi_channels,
  281. .num_events = ARRAY_SIZE(cnss_mhi_events),
  282. .event_cfg = cnss_mhi_events,
  283. .m2_no_db = true,
  284. };
  285. static struct cnss_pci_reg ce_src[] = {
  286. { "SRC_RING_BASE_LSB", CE_SRC_RING_BASE_LSB_OFFSET },
  287. { "SRC_RING_BASE_MSB", CE_SRC_RING_BASE_MSB_OFFSET },
  288. { "SRC_RING_ID", CE_SRC_RING_ID_OFFSET },
  289. { "SRC_RING_MISC", CE_SRC_RING_MISC_OFFSET },
  290. { "SRC_CTRL", CE_SRC_CTRL_OFFSET },
  291. { "SRC_R0_CE_CH_SRC_IS", CE_SRC_R0_CE_CH_SRC_IS_OFFSET },
  292. { "SRC_RING_HP", CE_SRC_RING_HP_OFFSET },
  293. { "SRC_RING_TP", CE_SRC_RING_TP_OFFSET },
  294. { NULL },
  295. };
  296. static struct cnss_pci_reg ce_dst[] = {
  297. { "DEST_RING_BASE_LSB", CE_DEST_RING_BASE_LSB_OFFSET },
  298. { "DEST_RING_BASE_MSB", CE_DEST_RING_BASE_MSB_OFFSET },
  299. { "DEST_RING_ID", CE_DEST_RING_ID_OFFSET },
  300. { "DEST_RING_MISC", CE_DEST_RING_MISC_OFFSET },
  301. { "DEST_CTRL", CE_DEST_CTRL_OFFSET },
  302. { "CE_CH_DST_IS", CE_CH_DST_IS_OFFSET },
  303. { "CE_CH_DEST_CTRL2", CE_CH_DEST_CTRL2_OFFSET },
  304. { "DEST_RING_HP", CE_DEST_RING_HP_OFFSET },
  305. { "DEST_RING_TP", CE_DEST_RING_TP_OFFSET },
  306. { "STATUS_RING_BASE_LSB", CE_STATUS_RING_BASE_LSB_OFFSET },
  307. { "STATUS_RING_BASE_MSB", CE_STATUS_RING_BASE_MSB_OFFSET },
  308. { "STATUS_RING_ID", CE_STATUS_RING_ID_OFFSET },
  309. { "STATUS_RING_MISC", CE_STATUS_RING_MISC_OFFSET },
  310. { "STATUS_RING_HP", CE_STATUS_RING_HP_OFFSET },
  311. { "STATUS_RING_TP", CE_STATUS_RING_TP_OFFSET },
  312. { NULL },
  313. };
  314. static struct cnss_pci_reg ce_cmn[] = {
  315. { "GXI_ERR_INTS", CE_COMMON_GXI_ERR_INTS },
  316. { "GXI_ERR_STATS", CE_COMMON_GXI_ERR_STATS },
  317. { "GXI_WDOG_STATUS", CE_COMMON_GXI_WDOG_STATUS },
  318. { "TARGET_IE_0", CE_COMMON_TARGET_IE_0 },
  319. { "TARGET_IE_1", CE_COMMON_TARGET_IE_1 },
  320. { NULL },
  321. };
  322. static struct cnss_pci_reg qdss_csr[] = {
  323. { "QDSSCSR_ETRIRQCTRL", QDSS_APB_DEC_CSR_ETRIRQCTRL_OFFSET },
  324. { "QDSSCSR_PRESERVEETF", QDSS_APB_DEC_CSR_PRESERVEETF_OFFSET },
  325. { "QDSSCSR_PRESERVEETR0", QDSS_APB_DEC_CSR_PRESERVEETR0_OFFSET },
  326. { "QDSSCSR_PRESERVEETR1", QDSS_APB_DEC_CSR_PRESERVEETR1_OFFSET },
  327. { NULL },
  328. };
  329. static struct cnss_pci_reg pci_scratch[] = {
  330. { "PCIE_SCRATCH_0", PCIE_SCRATCH_0_SOC_PCIE_REG },
  331. { "PCIE_SCRATCH_1", PCIE_SCRATCH_1_SOC_PCIE_REG },
  332. { "PCIE_SCRATCH_2", PCIE_SCRATCH_2_SOC_PCIE_REG },
  333. { NULL },
  334. };
  335. /* First field of the structure is the device bit mask. Use
  336. * enum cnss_pci_reg_mask as reference for the value.
  337. */
  338. static struct cnss_misc_reg wcss_reg_access_seq[] = {
  339. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  340. {1, 1, QCA6390_GCC_DEBUG_CLK_CTL, 0x802},
  341. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  342. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_PLL_MODE, 0},
  343. {1, 1, QCA6390_GCC_DEBUG_CLK_CTL, 0x805},
  344. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  345. {1, 0, QCA6390_WCSS_WFSS_PMM_WFSS_PMM_R0_PMM_CTRL, 0},
  346. {1, 0, QCA6390_WCSS_PMM_TOP_PMU_CX_CSR, 0},
  347. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_RAW_STAT, 0},
  348. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_EN, 0},
  349. {1, 0, QCA6390_WCSS_PMM_TOP_PMU_TESTBUS_STS, 0},
  350. {1, 1, QCA6390_WCSS_PMM_TOP_PMU_TESTBUS_CTL, 0xD},
  351. {1, 0, QCA6390_WCSS_PMM_TOP_TESTBUS_STS, 0},
  352. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG, 0},
  353. {1, 1, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG, 0},
  354. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x8},
  355. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  356. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_STS, 0},
  357. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_CTL, 0},
  358. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_SLP_SEQ_ENTRY_0, 0},
  359. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_SLP_SEQ_ENTRY_9, 0},
  360. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS0, 0},
  361. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS1, 0},
  362. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS2, 0},
  363. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS3, 0},
  364. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS4, 0},
  365. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS5, 0},
  366. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS6, 0},
  367. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE0, 0},
  368. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE1, 0},
  369. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE2, 0},
  370. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE3, 0},
  371. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE4, 0},
  372. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE5, 0},
  373. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE6, 0},
  374. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING0, 0},
  375. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING1, 0},
  376. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING2, 0},
  377. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING3, 0},
  378. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING4, 0},
  379. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING5, 0},
  380. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING6, 0},
  381. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x30040},
  382. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  383. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  384. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  385. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  386. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x30105},
  387. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  388. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  389. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  390. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  391. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  392. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  393. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  394. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  395. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  396. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_NOC_CBCR, 0},
  397. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_AHB_CBCR, 0},
  398. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_GDSCR, 0},
  399. {1, 0, QCA6390_WCSS_CC_WCSS_WLAN1_GDSCR, 0},
  400. {1, 0, QCA6390_WCSS_CC_WCSS_WLAN2_GDSCR, 0},
  401. {1, 0, QCA6390_WCSS_PMM_TOP_PMM_INT_CLR, 0},
  402. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_STICKY_EN, 0},
  403. };
  404. static struct cnss_misc_reg pcie_reg_access_seq[] = {
  405. {1, 0, QCA6390_PCIE_PCIE_WCSS_STATUS_FOR_DEBUG_LOW_PCIE_LOCAL_REG, 0},
  406. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  407. {1, 1, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0x18},
  408. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  409. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  410. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_STATUS_SOC_PCIE_REG, 0},
  411. {1, 0, QCA6390_PCIE_SOC_COMMIT_REPLAY_SOC_PCIE_REG, 0},
  412. {1, 0, QCA6390_TLMM_GPIO_IN_OUT57, 0},
  413. {1, 0, QCA6390_TLMM_GPIO_INTR_CFG57, 0},
  414. {1, 0, QCA6390_TLMM_GPIO_INTR_STATUS57, 0},
  415. {1, 0, QCA6390_TLMM_GPIO_IN_OUT59, 0},
  416. {1, 0, QCA6390_TLMM_GPIO_INTR_CFG59, 0},
  417. {1, 0, QCA6390_TLMM_GPIO_INTR_STATUS59, 0},
  418. {1, 0, QCA6390_PCIE_PCIE_PARF_LTSSM, 0},
  419. {1, 0, QCA6390_PCIE_PCIE_PARF_PM_STTS, 0},
  420. {1, 0, QCA6390_PCIE_PCIE_PARF_PM_STTS_1, 0},
  421. {1, 0, QCA6390_PCIE_PCIE_PARF_INT_STATUS, 0},
  422. {1, 0, QCA6390_PCIE_PCIE_INT_ALL_STATUS, 0},
  423. {1, 0, QCA6390_PCIE_PCIE_INT_ALL_MASK, 0},
  424. {1, 0, QCA6390_PCIE_PCIE_PARF_BDF_TO_SID_CFG, 0},
  425. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_4, 0},
  426. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_3, 0},
  427. {1, 0, QCA6390_PCIE_PCIE_PARF_MHI_CLOCK_RESET_CTRL, 0},
  428. {1, 0, QCA6390_PCIE_PCIE_PARF_MHI_BASE_ADDR_LOWER, 0},
  429. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_MODE_HANDLER_STATUS, 0},
  430. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_MODE_HANDLER_CFG, 0},
  431. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2, 0},
  432. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L1SUB, 0},
  433. {1, 0, QCA6390_PCIE_PCIE_CORE_CONFIG, 0},
  434. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_4, 0},
  435. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L2, 0},
  436. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L1, 0},
  437. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1, 0},
  438. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2, 0},
  439. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSSAON_PCIE_SR_STATUS_HIGH, 0},
  440. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSSAON_PCIE_SR_STATUS_LOW, 0},
  441. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSS_STATUS_FOR_DEBUG_HIGH, 0},
  442. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSS_STATUS_FOR_DEBUG_LOW, 0},
  443. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN1_STATUS_REG2, 0},
  444. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN2_STATUS_REG2, 0},
  445. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_PMM_WLAN2_CFG_REG1, 0},
  446. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_PMM_WLAN1_CFG_REG1, 0},
  447. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN2_APS_STATUS_REG1, 0},
  448. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN1_APS_STATUS_REG1, 0},
  449. {1, 0, QCA6390_PCIE_PCIE_BHI_EXECENV_REG, 0},
  450. };
  451. static struct cnss_misc_reg wlaon_reg_access_seq[] = {
  452. {3, 0, WLAON_SOC_POWER_CTRL, 0},
  453. {3, 0, WLAON_SOC_PWR_WDG_BARK_THRSHD, 0},
  454. {3, 0, WLAON_SOC_PWR_WDG_BITE_THRSHD, 0},
  455. {3, 0, WLAON_SW_COLD_RESET, 0},
  456. {3, 0, WLAON_RFA_MEM_SLP_NRET_N_OVERRIDE, 0},
  457. {3, 0, WLAON_GDSC_DELAY_SETTING, 0},
  458. {3, 0, WLAON_GDSC_DELAY_SETTING2, 0},
  459. {3, 0, WLAON_WL_PWR_STATUS_REG, 0},
  460. {3, 0, WLAON_WL_AON_DBG_CFG_REG, 0},
  461. {2, 0, WLAON_WL_AON_DBG_ENABLE_GRP0_REG, 0},
  462. {2, 0, WLAON_WL_AON_DBG_ENABLE_GRP1_REG, 0},
  463. {2, 0, WLAON_WL_AON_APM_CFG_CTRL0, 0},
  464. {2, 0, WLAON_WL_AON_APM_CFG_CTRL1, 0},
  465. {2, 0, WLAON_WL_AON_APM_CFG_CTRL2, 0},
  466. {2, 0, WLAON_WL_AON_APM_CFG_CTRL3, 0},
  467. {2, 0, WLAON_WL_AON_APM_CFG_CTRL4, 0},
  468. {2, 0, WLAON_WL_AON_APM_CFG_CTRL5, 0},
  469. {2, 0, WLAON_WL_AON_APM_CFG_CTRL5_1, 0},
  470. {2, 0, WLAON_WL_AON_APM_CFG_CTRL6, 0},
  471. {2, 0, WLAON_WL_AON_APM_CFG_CTRL6_1, 0},
  472. {2, 0, WLAON_WL_AON_APM_CFG_CTRL7, 0},
  473. {2, 0, WLAON_WL_AON_APM_CFG_CTRL8, 0},
  474. {2, 0, WLAON_WL_AON_APM_CFG_CTRL8_1, 0},
  475. {2, 0, WLAON_WL_AON_APM_CFG_CTRL9, 0},
  476. {2, 0, WLAON_WL_AON_APM_CFG_CTRL9_1, 0},
  477. {2, 0, WLAON_WL_AON_APM_CFG_CTRL10, 0},
  478. {2, 0, WLAON_WL_AON_APM_CFG_CTRL11, 0},
  479. {2, 0, WLAON_WL_AON_APM_CFG_CTRL12, 0},
  480. {2, 0, WLAON_WL_AON_APM_OVERRIDE_REG, 0},
  481. {2, 0, WLAON_WL_AON_CXPC_REG, 0},
  482. {2, 0, WLAON_WL_AON_APM_STATUS0, 0},
  483. {2, 0, WLAON_WL_AON_APM_STATUS1, 0},
  484. {2, 0, WLAON_WL_AON_APM_STATUS2, 0},
  485. {2, 0, WLAON_WL_AON_APM_STATUS3, 0},
  486. {2, 0, WLAON_WL_AON_APM_STATUS4, 0},
  487. {2, 0, WLAON_WL_AON_APM_STATUS5, 0},
  488. {2, 0, WLAON_WL_AON_APM_STATUS6, 0},
  489. {3, 0, WLAON_GLOBAL_COUNTER_CTRL1, 0},
  490. {3, 0, WLAON_GLOBAL_COUNTER_CTRL6, 0},
  491. {3, 0, WLAON_GLOBAL_COUNTER_CTRL7, 0},
  492. {3, 0, WLAON_GLOBAL_COUNTER_CTRL3, 0},
  493. {3, 0, WLAON_GLOBAL_COUNTER_CTRL4, 0},
  494. {3, 0, WLAON_GLOBAL_COUNTER_CTRL5, 0},
  495. {3, 0, WLAON_GLOBAL_COUNTER_CTRL8, 0},
  496. {3, 0, WLAON_GLOBAL_COUNTER_CTRL2, 0},
  497. {3, 0, WLAON_GLOBAL_COUNTER_CTRL9, 0},
  498. {3, 0, WLAON_RTC_CLK_CAL_CTRL1, 0},
  499. {3, 0, WLAON_RTC_CLK_CAL_CTRL2, 0},
  500. {3, 0, WLAON_RTC_CLK_CAL_CTRL3, 0},
  501. {3, 0, WLAON_RTC_CLK_CAL_CTRL4, 0},
  502. {3, 0, WLAON_RTC_CLK_CAL_CTRL5, 0},
  503. {3, 0, WLAON_RTC_CLK_CAL_CTRL6, 0},
  504. {3, 0, WLAON_RTC_CLK_CAL_CTRL7, 0},
  505. {3, 0, WLAON_RTC_CLK_CAL_CTRL8, 0},
  506. {3, 0, WLAON_RTC_CLK_CAL_CTRL9, 0},
  507. {3, 0, WLAON_WCSSAON_CONFIG_REG, 0},
  508. {3, 0, WLAON_WLAN_OEM_DEBUG_REG, 0},
  509. {3, 0, WLAON_WLAN_RAM_DUMP_REG, 0},
  510. {3, 0, WLAON_QDSS_WCSS_REG, 0},
  511. {3, 0, WLAON_QDSS_WCSS_ACK, 0},
  512. {3, 0, WLAON_WL_CLK_CNTL_KDF_REG, 0},
  513. {3, 0, WLAON_WL_CLK_CNTL_PMU_HFRC_REG, 0},
  514. {3, 0, WLAON_QFPROM_PWR_CTRL_REG, 0},
  515. {3, 0, WLAON_DLY_CONFIG, 0},
  516. {3, 0, WLAON_WLAON_Q6_IRQ_REG, 0},
  517. {3, 0, WLAON_PCIE_INTF_SW_CFG_REG, 0},
  518. {3, 0, WLAON_PCIE_INTF_STICKY_SW_CFG_REG, 0},
  519. {3, 0, WLAON_PCIE_INTF_PHY_SW_CFG_REG, 0},
  520. {3, 0, WLAON_PCIE_INTF_PHY_NOCSR_SW_CFG_REG, 0},
  521. {3, 0, WLAON_Q6_COOKIE_BIT, 0},
  522. {3, 0, WLAON_WARM_SW_ENTRY, 0},
  523. {3, 0, WLAON_RESET_DBG_SW_ENTRY, 0},
  524. {3, 0, WLAON_WL_PMUNOC_CFG_REG, 0},
  525. {3, 0, WLAON_RESET_CAUSE_CFG_REG, 0},
  526. {3, 0, WLAON_SOC_WCSSAON_WAKEUP_IRQ_7_EN_REG, 0},
  527. {3, 0, WLAON_DEBUG, 0},
  528. {3, 0, WLAON_SOC_PARAMETERS, 0},
  529. {3, 0, WLAON_WLPM_SIGNAL, 0},
  530. {3, 0, WLAON_SOC_RESET_CAUSE_REG, 0},
  531. {3, 0, WLAON_WAKEUP_PCIE_SOC_REG, 0},
  532. {3, 0, WLAON_PBL_STACK_CANARY, 0},
  533. {3, 0, WLAON_MEM_TOT_NUM_GRP_REG, 0},
  534. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP0_REG, 0},
  535. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP1_REG, 0},
  536. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP2_REG, 0},
  537. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP3_REG, 0},
  538. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP0_REG, 0},
  539. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP1_REG, 0},
  540. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP2_REG, 0},
  541. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP3_REG, 0},
  542. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP0_REG, 0},
  543. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP1_REG, 0},
  544. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP2_REG, 0},
  545. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP3_REG, 0},
  546. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP0_REG, 0},
  547. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP1_REG, 0},
  548. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP2_REG, 0},
  549. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP3_REG, 0},
  550. {3, 0, WLAON_MEM_CNT_SEL_REG, 0},
  551. {3, 0, WLAON_MEM_NO_EXTBHS_REG, 0},
  552. {3, 0, WLAON_MEM_DEBUG_REG, 0},
  553. {3, 0, WLAON_MEM_DEBUG_BUS_REG, 0},
  554. {3, 0, WLAON_MEM_REDUN_CFG_REG, 0},
  555. {3, 0, WLAON_WL_AON_SPARE2, 0},
  556. {3, 0, WLAON_VSEL_CFG_FOR_WL_RET_DISABLE_REG, 0},
  557. {3, 0, WLAON_BTFM_WLAN_IPC_STATUS_REG, 0},
  558. {3, 0, WLAON_MPM_COUNTER_CHICKEN_BITS, 0},
  559. {3, 0, WLAON_WLPM_CHICKEN_BITS, 0},
  560. {3, 0, WLAON_PCIE_PHY_PWR_REG, 0},
  561. {3, 0, WLAON_WL_CLK_CNTL_PMU_LPO2M_REG, 0},
  562. {3, 0, WLAON_WL_SS_ROOT_CLK_SWITCH_REG, 0},
  563. {3, 0, WLAON_POWERCTRL_PMU_REG, 0},
  564. {3, 0, WLAON_POWERCTRL_MEM_REG, 0},
  565. {3, 0, WLAON_PCIE_PWR_CTRL_REG, 0},
  566. {3, 0, WLAON_SOC_PWR_PROFILE_REG, 0},
  567. {3, 0, WLAON_WCSSAON_PCIE_SR_STATUS_HI_REG, 0},
  568. {3, 0, WLAON_WCSSAON_PCIE_SR_STATUS_LO_REG, 0},
  569. {3, 0, WLAON_WCSS_TCSR_PMM_SR_STATUS_HI_REG, 0},
  570. {3, 0, WLAON_WCSS_TCSR_PMM_SR_STATUS_LO_REG, 0},
  571. {3, 0, WLAON_MEM_SVS_CFG_REG, 0},
  572. {3, 0, WLAON_CMN_AON_MISC_REG, 0},
  573. {3, 0, WLAON_INTR_STATUS, 0},
  574. {2, 0, WLAON_INTR_ENABLE, 0},
  575. {2, 0, WLAON_NOC_DBG_BUS_SEL_REG, 0},
  576. {2, 0, WLAON_NOC_DBG_BUS_REG, 0},
  577. {2, 0, WLAON_WL_CTRL_MISC_REG, 0},
  578. {2, 0, WLAON_DBG_STATUS0, 0},
  579. {2, 0, WLAON_DBG_STATUS1, 0},
  580. {2, 0, WLAON_TIMERSYNC_OFFSET_L, 0},
  581. {2, 0, WLAON_TIMERSYNC_OFFSET_H, 0},
  582. {2, 0, WLAON_PMU_LDO_SETTLE_REG, 0},
  583. };
  584. static struct cnss_misc_reg syspm_reg_access_seq[] = {
  585. {1, 0, QCA6390_SYSPM_SYSPM_PWR_STATUS, 0},
  586. {1, 0, QCA6390_SYSPM_DBG_BTFM_AON_REG, 0},
  587. {1, 0, QCA6390_SYSPM_DBG_BUS_SEL_REG, 0},
  588. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  589. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  590. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  591. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  592. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  593. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  594. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  595. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  596. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  597. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  598. };
  599. #define WCSS_REG_SIZE ARRAY_SIZE(wcss_reg_access_seq)
  600. #define PCIE_REG_SIZE ARRAY_SIZE(pcie_reg_access_seq)
  601. #define WLAON_REG_SIZE ARRAY_SIZE(wlaon_reg_access_seq)
  602. #define SYSPM_REG_SIZE ARRAY_SIZE(syspm_reg_access_seq)
  603. #if IS_ENABLED(CONFIG_PCI_MSM)
  604. /**
  605. * _cnss_pci_enumerate() - Enumerate PCIe endpoints
  606. * @plat_priv: driver platform context pointer
  607. * @rc_num: root complex index that an endpoint connects to
  608. *
  609. * This function shall call corresponding PCIe root complex driver APIs
  610. * to power on root complex and enumerate the endpoint connected to it.
  611. *
  612. * Return: 0 for success, negative value for error
  613. */
  614. static int _cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  615. {
  616. return msm_pcie_enumerate(rc_num);
  617. }
  618. /**
  619. * cnss_pci_assert_perst() - Assert PCIe PERST GPIO
  620. * @pci_priv: driver PCI bus context pointer
  621. *
  622. * This function shall call corresponding PCIe root complex driver APIs
  623. * to assert PCIe PERST GPIO.
  624. *
  625. * Return: 0 for success, negative value for error
  626. */
  627. static int cnss_pci_assert_perst(struct cnss_pci_data *pci_priv)
  628. {
  629. struct pci_dev *pci_dev = pci_priv->pci_dev;
  630. return msm_pcie_pm_control(MSM_PCIE_HANDLE_LINKDOWN,
  631. pci_dev->bus->number, pci_dev, NULL,
  632. PM_OPTIONS_DEFAULT);
  633. }
  634. /**
  635. * cnss_pci_disable_pc() - Disable PCIe link power collapse from RC driver
  636. * @pci_priv: driver PCI bus context pointer
  637. * @vote: value to indicate disable (true) or enable (false)
  638. *
  639. * This function shall call corresponding PCIe root complex driver APIs
  640. * to disable PCIe power collapse. The purpose of this API is to avoid
  641. * root complex driver still controlling PCIe link from callbacks of
  642. * system suspend/resume. Device driver itself should take full control
  643. * of the link in such cases.
  644. *
  645. * Return: 0 for success, negative value for error
  646. */
  647. static int cnss_pci_disable_pc(struct cnss_pci_data *pci_priv, bool vote)
  648. {
  649. struct pci_dev *pci_dev = pci_priv->pci_dev;
  650. return msm_pcie_pm_control(vote ? MSM_PCIE_DISABLE_PC :
  651. MSM_PCIE_ENABLE_PC,
  652. pci_dev->bus->number, pci_dev, NULL,
  653. PM_OPTIONS_DEFAULT);
  654. }
  655. /**
  656. * cnss_pci_set_link_bandwidth() - Update number of lanes and speed of
  657. * PCIe link
  658. * @pci_priv: driver PCI bus context pointer
  659. * @link_speed: PCIe link gen speed
  660. * @link_width: number of lanes for PCIe link
  661. *
  662. * This function shall call corresponding PCIe root complex driver APIs
  663. * to update number of lanes and speed of the link.
  664. *
  665. * Return: 0 for success, negative value for error
  666. */
  667. static int cnss_pci_set_link_bandwidth(struct cnss_pci_data *pci_priv,
  668. u16 link_speed, u16 link_width)
  669. {
  670. return msm_pcie_set_link_bandwidth(pci_priv->pci_dev,
  671. link_speed, link_width);
  672. }
  673. /**
  674. * cnss_pci_set_max_link_speed() - Set the maximum speed PCIe can link up with
  675. * @pci_priv: driver PCI bus context pointer
  676. * @rc_num: root complex index that an endpoint connects to
  677. * @link_speed: PCIe link gen speed
  678. *
  679. * This function shall call corresponding PCIe root complex driver APIs
  680. * to update the maximum speed that PCIe can link up with.
  681. *
  682. * Return: 0 for success, negative value for error
  683. */
  684. static int cnss_pci_set_max_link_speed(struct cnss_pci_data *pci_priv,
  685. u32 rc_num, u16 link_speed)
  686. {
  687. return msm_pcie_set_target_link_speed(rc_num, link_speed, false);
  688. }
  689. /**
  690. * _cnss_pci_prevent_l1() - Prevent PCIe L1 and L1 sub-states
  691. * @pci_priv: driver PCI bus context pointer
  692. *
  693. * This function shall call corresponding PCIe root complex driver APIs
  694. * to prevent PCIe link enter L1 and L1 sub-states. The APIs should also
  695. * bring link out of L1 or L1 sub-states if any and avoid synchronization
  696. * issues if any.
  697. *
  698. * Return: 0 for success, negative value for error
  699. */
  700. static int _cnss_pci_prevent_l1(struct cnss_pci_data *pci_priv)
  701. {
  702. return msm_pcie_prevent_l1(pci_priv->pci_dev);
  703. }
  704. /**
  705. * _cnss_pci_allow_l1() - Allow PCIe L1 and L1 sub-states
  706. * @pci_priv: driver PCI bus context pointer
  707. *
  708. * This function shall call corresponding PCIe root complex driver APIs
  709. * to allow PCIe link enter L1 and L1 sub-states. The APIs should avoid
  710. * synchronization issues if any.
  711. *
  712. * Return: 0 for success, negative value for error
  713. */
  714. static void _cnss_pci_allow_l1(struct cnss_pci_data *pci_priv)
  715. {
  716. msm_pcie_allow_l1(pci_priv->pci_dev);
  717. }
  718. /**
  719. * cnss_pci_set_link_up() - Power on or resume PCIe link
  720. * @pci_priv: driver PCI bus context pointer
  721. *
  722. * This function shall call corresponding PCIe root complex driver APIs
  723. * to Power on or resume PCIe link.
  724. *
  725. * Return: 0 for success, negative value for error
  726. */
  727. static int cnss_pci_set_link_up(struct cnss_pci_data *pci_priv)
  728. {
  729. struct pci_dev *pci_dev = pci_priv->pci_dev;
  730. enum msm_pcie_pm_opt pm_ops = MSM_PCIE_RESUME;
  731. u32 pm_options = PM_OPTIONS_DEFAULT;
  732. int ret;
  733. ret = msm_pcie_pm_control(pm_ops, pci_dev->bus->number, pci_dev,
  734. NULL, pm_options);
  735. if (ret)
  736. cnss_pr_err("Failed to resume PCI link with default option, err = %d\n",
  737. ret);
  738. return ret;
  739. }
  740. /**
  741. * cnss_pci_set_link_down() - Power off or suspend PCIe link
  742. * @pci_priv: driver PCI bus context pointer
  743. *
  744. * This function shall call corresponding PCIe root complex driver APIs
  745. * to power off or suspend PCIe link.
  746. *
  747. * Return: 0 for success, negative value for error
  748. */
  749. static int cnss_pci_set_link_down(struct cnss_pci_data *pci_priv)
  750. {
  751. struct pci_dev *pci_dev = pci_priv->pci_dev;
  752. enum msm_pcie_pm_opt pm_ops;
  753. u32 pm_options = PM_OPTIONS_DEFAULT;
  754. int ret;
  755. if (pci_priv->drv_connected_last) {
  756. cnss_pr_vdbg("Use PCIe DRV suspend\n");
  757. pm_ops = MSM_PCIE_DRV_SUSPEND;
  758. } else {
  759. pm_ops = MSM_PCIE_SUSPEND;
  760. }
  761. ret = msm_pcie_pm_control(pm_ops, pci_dev->bus->number, pci_dev,
  762. NULL, pm_options);
  763. if (ret)
  764. cnss_pr_err("Failed to suspend PCI link with default option, err = %d\n",
  765. ret);
  766. return ret;
  767. }
  768. #else
  769. static int _cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  770. {
  771. return -EOPNOTSUPP;
  772. }
  773. static int cnss_pci_assert_perst(struct cnss_pci_data *pci_priv)
  774. {
  775. return -EOPNOTSUPP;
  776. }
  777. static int cnss_pci_disable_pc(struct cnss_pci_data *pci_priv, bool vote)
  778. {
  779. return 0;
  780. }
  781. static int cnss_pci_set_link_bandwidth(struct cnss_pci_data *pci_priv,
  782. u16 link_speed, u16 link_width)
  783. {
  784. return 0;
  785. }
  786. static int cnss_pci_set_max_link_speed(struct cnss_pci_data *pci_priv,
  787. u32 rc_num, u16 link_speed)
  788. {
  789. return 0;
  790. }
  791. static int _cnss_pci_prevent_l1(struct cnss_pci_data *pci_priv)
  792. {
  793. return 0;
  794. }
  795. static void _cnss_pci_allow_l1(struct cnss_pci_data *pci_priv) {}
  796. static int cnss_pci_set_link_up(struct cnss_pci_data *pci_priv)
  797. {
  798. return 0;
  799. }
  800. static int cnss_pci_set_link_down(struct cnss_pci_data *pci_priv)
  801. {
  802. return 0;
  803. }
  804. #endif /* CONFIG_PCI_MSM */
  805. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  806. static void cnss_mhi_debug_reg_dump(struct cnss_pci_data *pci_priv)
  807. {
  808. mhi_debug_reg_dump(pci_priv->mhi_ctrl);
  809. }
  810. static void cnss_mhi_dump_sfr(struct cnss_pci_data *pci_priv)
  811. {
  812. mhi_dump_sfr(pci_priv->mhi_ctrl);
  813. }
  814. static bool cnss_mhi_scan_rddm_cookie(struct cnss_pci_data *pci_priv,
  815. u32 cookie)
  816. {
  817. return mhi_scan_rddm_cookie(pci_priv->mhi_ctrl, cookie);
  818. }
  819. static int cnss_mhi_pm_fast_suspend(struct cnss_pci_data *pci_priv,
  820. bool notify_clients)
  821. {
  822. return mhi_pm_fast_suspend(pci_priv->mhi_ctrl, notify_clients);
  823. }
  824. static int cnss_mhi_pm_fast_resume(struct cnss_pci_data *pci_priv,
  825. bool notify_clients)
  826. {
  827. return mhi_pm_fast_resume(pci_priv->mhi_ctrl, notify_clients);
  828. }
  829. static void cnss_mhi_set_m2_timeout_ms(struct cnss_pci_data *pci_priv,
  830. u32 timeout)
  831. {
  832. return mhi_set_m2_timeout_ms(pci_priv->mhi_ctrl, timeout);
  833. }
  834. static int cnss_mhi_device_get_sync_atomic(struct cnss_pci_data *pci_priv,
  835. int timeout_us, bool in_panic)
  836. {
  837. return mhi_device_get_sync_atomic(pci_priv->mhi_ctrl->mhi_dev,
  838. timeout_us, in_panic);
  839. }
  840. static void
  841. cnss_mhi_controller_set_bw_scale_cb(struct cnss_pci_data *pci_priv,
  842. int (*cb)(struct mhi_controller *mhi_ctrl,
  843. struct mhi_link_info *link_info))
  844. {
  845. mhi_controller_set_bw_scale_cb(pci_priv->mhi_ctrl, cb);
  846. }
  847. static int cnss_mhi_force_reset(struct cnss_pci_data *pci_priv)
  848. {
  849. return mhi_force_reset(pci_priv->mhi_ctrl);
  850. }
  851. static void cnss_mhi_controller_set_base(struct cnss_pci_data *pci_priv,
  852. phys_addr_t base)
  853. {
  854. return mhi_controller_set_base(pci_priv->mhi_ctrl, base);
  855. }
  856. #else
  857. static void cnss_mhi_debug_reg_dump(struct cnss_pci_data *pci_priv)
  858. {
  859. }
  860. static void cnss_mhi_dump_sfr(struct cnss_pci_data *pci_priv)
  861. {
  862. }
  863. static bool cnss_mhi_scan_rddm_cookie(struct cnss_pci_data *pci_priv,
  864. u32 cookie)
  865. {
  866. return false;
  867. }
  868. static int cnss_mhi_pm_fast_suspend(struct cnss_pci_data *pci_priv,
  869. bool notify_clients)
  870. {
  871. return -EOPNOTSUPP;
  872. }
  873. static int cnss_mhi_pm_fast_resume(struct cnss_pci_data *pci_priv,
  874. bool notify_clients)
  875. {
  876. return -EOPNOTSUPP;
  877. }
  878. static void cnss_mhi_set_m2_timeout_ms(struct cnss_pci_data *pci_priv,
  879. u32 timeout)
  880. {
  881. }
  882. static int cnss_mhi_device_get_sync_atomic(struct cnss_pci_data *pci_priv,
  883. int timeout_us, bool in_panic)
  884. {
  885. return -EOPNOTSUPP;
  886. }
  887. static void
  888. cnss_mhi_controller_set_bw_scale_cb(struct cnss_pci_data *pci_priv,
  889. int (*cb)(struct mhi_controller *mhi_ctrl,
  890. struct mhi_link_info *link_info))
  891. {
  892. }
  893. static int cnss_mhi_force_reset(struct cnss_pci_data *pci_priv)
  894. {
  895. return -EOPNOTSUPP;
  896. }
  897. static void cnss_mhi_controller_set_base(struct cnss_pci_data *pci_priv,
  898. phys_addr_t base)
  899. {
  900. }
  901. #endif /* CONFIG_MHI_BUS_MISC */
  902. int cnss_pci_check_link_status(struct cnss_pci_data *pci_priv)
  903. {
  904. u16 device_id;
  905. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  906. cnss_pr_dbg("%ps: PCIe link is in suspend state\n",
  907. (void *)_RET_IP_);
  908. return -EACCES;
  909. }
  910. if (pci_priv->pci_link_down_ind) {
  911. cnss_pr_err("%ps: PCIe link is down\n", (void *)_RET_IP_);
  912. return -EIO;
  913. }
  914. pci_read_config_word(pci_priv->pci_dev, PCI_DEVICE_ID, &device_id);
  915. if (device_id != pci_priv->device_id) {
  916. cnss_fatal_err("%ps: PCI device ID mismatch, link possibly down, current read ID: 0x%x, record ID: 0x%x\n",
  917. (void *)_RET_IP_, device_id,
  918. pci_priv->device_id);
  919. return -EIO;
  920. }
  921. return 0;
  922. }
  923. static void cnss_pci_select_window(struct cnss_pci_data *pci_priv, u32 offset)
  924. {
  925. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  926. u32 window = (offset >> WINDOW_SHIFT) & WINDOW_VALUE_MASK;
  927. u32 window_enable = WINDOW_ENABLE_BIT | window;
  928. u32 val;
  929. writel_relaxed(window_enable, pci_priv->bar +
  930. QCA6390_PCIE_REMAP_BAR_CTRL_OFFSET);
  931. if (window != pci_priv->remap_window) {
  932. pci_priv->remap_window = window;
  933. cnss_pr_dbg("Config PCIe remap window register to 0x%x\n",
  934. window_enable);
  935. }
  936. /* Read it back to make sure the write has taken effect */
  937. val = readl_relaxed(pci_priv->bar + QCA6390_PCIE_REMAP_BAR_CTRL_OFFSET);
  938. if (val != window_enable) {
  939. cnss_pr_err("Failed to config window register to 0x%x, current value: 0x%x\n",
  940. window_enable, val);
  941. if (!cnss_pci_check_link_status(pci_priv) &&
  942. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  943. CNSS_ASSERT(0);
  944. }
  945. }
  946. static int cnss_pci_reg_read(struct cnss_pci_data *pci_priv,
  947. u32 offset, u32 *val)
  948. {
  949. int ret;
  950. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  951. if (!in_interrupt() && !irqs_disabled()) {
  952. ret = cnss_pci_check_link_status(pci_priv);
  953. if (ret)
  954. return ret;
  955. }
  956. if (pci_priv->pci_dev->device == QCA6174_DEVICE_ID ||
  957. offset < MAX_UNWINDOWED_ADDRESS) {
  958. *val = readl_relaxed(pci_priv->bar + offset);
  959. return 0;
  960. }
  961. /* If in panic, assumption is kernel panic handler will hold all threads
  962. * and interrupts. Further pci_reg_window_lock could be held before
  963. * panic. So only lock during normal operation.
  964. */
  965. if (test_bit(CNSS_IN_PANIC, &plat_priv->driver_state)) {
  966. cnss_pci_select_window(pci_priv, offset);
  967. *val = readl_relaxed(pci_priv->bar + WINDOW_START +
  968. (offset & WINDOW_RANGE_MASK));
  969. } else {
  970. spin_lock_bh(&pci_reg_window_lock);
  971. cnss_pci_select_window(pci_priv, offset);
  972. *val = readl_relaxed(pci_priv->bar + WINDOW_START +
  973. (offset & WINDOW_RANGE_MASK));
  974. spin_unlock_bh(&pci_reg_window_lock);
  975. }
  976. return 0;
  977. }
  978. static int cnss_pci_reg_write(struct cnss_pci_data *pci_priv, u32 offset,
  979. u32 val)
  980. {
  981. int ret;
  982. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  983. if (!in_interrupt() && !irqs_disabled()) {
  984. ret = cnss_pci_check_link_status(pci_priv);
  985. if (ret)
  986. return ret;
  987. }
  988. if (pci_priv->pci_dev->device == QCA6174_DEVICE_ID ||
  989. offset < MAX_UNWINDOWED_ADDRESS) {
  990. writel_relaxed(val, pci_priv->bar + offset);
  991. return 0;
  992. }
  993. /* Same constraint as PCI register read in panic */
  994. if (test_bit(CNSS_IN_PANIC, &plat_priv->driver_state)) {
  995. cnss_pci_select_window(pci_priv, offset);
  996. writel_relaxed(val, pci_priv->bar + WINDOW_START +
  997. (offset & WINDOW_RANGE_MASK));
  998. } else {
  999. spin_lock_bh(&pci_reg_window_lock);
  1000. cnss_pci_select_window(pci_priv, offset);
  1001. writel_relaxed(val, pci_priv->bar + WINDOW_START +
  1002. (offset & WINDOW_RANGE_MASK));
  1003. spin_unlock_bh(&pci_reg_window_lock);
  1004. }
  1005. return 0;
  1006. }
  1007. static int cnss_pci_force_wake_get(struct cnss_pci_data *pci_priv)
  1008. {
  1009. struct device *dev = &pci_priv->pci_dev->dev;
  1010. int ret;
  1011. ret = cnss_pci_force_wake_request_sync(dev,
  1012. FORCE_WAKE_DELAY_TIMEOUT_US);
  1013. if (ret) {
  1014. if (ret != -EAGAIN)
  1015. cnss_pr_err("Failed to request force wake\n");
  1016. return ret;
  1017. }
  1018. /* If device's M1 state-change event races here, it can be ignored,
  1019. * as the device is expected to immediately move from M2 to M0
  1020. * without entering low power state.
  1021. */
  1022. if (cnss_pci_is_device_awake(dev) != true)
  1023. cnss_pr_warn("MHI not in M0, while reg still accessible\n");
  1024. return 0;
  1025. }
  1026. static int cnss_pci_force_wake_put(struct cnss_pci_data *pci_priv)
  1027. {
  1028. struct device *dev = &pci_priv->pci_dev->dev;
  1029. int ret;
  1030. ret = cnss_pci_force_wake_release(dev);
  1031. if (ret && ret != -EAGAIN)
  1032. cnss_pr_err("Failed to release force wake\n");
  1033. return ret;
  1034. }
  1035. #if IS_ENABLED(CONFIG_INTERCONNECT)
  1036. /**
  1037. * cnss_setup_bus_bandwidth() - Setup interconnect vote for given bandwidth
  1038. * @plat_priv: Platform private data struct
  1039. * @bw: bandwidth
  1040. * @save: toggle flag to save bandwidth to current_bw_vote
  1041. *
  1042. * Setup bandwidth votes for configured interconnect paths
  1043. *
  1044. * Return: 0 for success
  1045. */
  1046. static int cnss_setup_bus_bandwidth(struct cnss_plat_data *plat_priv,
  1047. u32 bw, bool save)
  1048. {
  1049. int ret = 0;
  1050. struct cnss_bus_bw_info *bus_bw_info;
  1051. if (!plat_priv->icc.path_count)
  1052. return -EOPNOTSUPP;
  1053. if (bw >= plat_priv->icc.bus_bw_cfg_count) {
  1054. cnss_pr_err("Invalid bus bandwidth Type: %d", bw);
  1055. return -EINVAL;
  1056. }
  1057. list_for_each_entry(bus_bw_info, &plat_priv->icc.list_head, list) {
  1058. ret = icc_set_bw(bus_bw_info->icc_path,
  1059. bus_bw_info->cfg_table[bw].avg_bw,
  1060. bus_bw_info->cfg_table[bw].peak_bw);
  1061. if (ret) {
  1062. cnss_pr_err("Could not set BW Cfg: %d, err = %d ICC Path: %s Val: %d %d\n",
  1063. bw, ret, bus_bw_info->icc_name,
  1064. bus_bw_info->cfg_table[bw].avg_bw,
  1065. bus_bw_info->cfg_table[bw].peak_bw);
  1066. break;
  1067. }
  1068. }
  1069. if (ret == 0 && save)
  1070. plat_priv->icc.current_bw_vote = bw;
  1071. return ret;
  1072. }
  1073. int cnss_request_bus_bandwidth(struct device *dev, int bandwidth)
  1074. {
  1075. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1076. if (!plat_priv)
  1077. return -ENODEV;
  1078. if (bandwidth < 0)
  1079. return -EINVAL;
  1080. return cnss_setup_bus_bandwidth(plat_priv, (u32)bandwidth, true);
  1081. }
  1082. #else
  1083. static int cnss_setup_bus_bandwidth(struct cnss_plat_data *plat_priv,
  1084. u32 bw, bool save)
  1085. {
  1086. return 0;
  1087. }
  1088. int cnss_request_bus_bandwidth(struct device *dev, int bandwidth)
  1089. {
  1090. return 0;
  1091. }
  1092. #endif
  1093. EXPORT_SYMBOL(cnss_request_bus_bandwidth);
  1094. int cnss_pci_debug_reg_read(struct cnss_pci_data *pci_priv, u32 offset,
  1095. u32 *val, bool raw_access)
  1096. {
  1097. int ret = 0;
  1098. bool do_force_wake_put = true;
  1099. if (raw_access) {
  1100. ret = cnss_pci_reg_read(pci_priv, offset, val);
  1101. goto out;
  1102. }
  1103. ret = cnss_pci_is_device_down(&pci_priv->pci_dev->dev);
  1104. if (ret)
  1105. goto out;
  1106. ret = cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS);
  1107. if (ret < 0)
  1108. goto runtime_pm_put;
  1109. ret = cnss_pci_force_wake_get(pci_priv);
  1110. if (ret)
  1111. do_force_wake_put = false;
  1112. ret = cnss_pci_reg_read(pci_priv, offset, val);
  1113. if (ret) {
  1114. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  1115. offset, ret);
  1116. goto force_wake_put;
  1117. }
  1118. force_wake_put:
  1119. if (do_force_wake_put)
  1120. cnss_pci_force_wake_put(pci_priv);
  1121. runtime_pm_put:
  1122. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  1123. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  1124. out:
  1125. return ret;
  1126. }
  1127. int cnss_pci_debug_reg_write(struct cnss_pci_data *pci_priv, u32 offset,
  1128. u32 val, bool raw_access)
  1129. {
  1130. int ret = 0;
  1131. bool do_force_wake_put = true;
  1132. if (raw_access) {
  1133. ret = cnss_pci_reg_write(pci_priv, offset, val);
  1134. goto out;
  1135. }
  1136. ret = cnss_pci_is_device_down(&pci_priv->pci_dev->dev);
  1137. if (ret)
  1138. goto out;
  1139. ret = cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS);
  1140. if (ret < 0)
  1141. goto runtime_pm_put;
  1142. ret = cnss_pci_force_wake_get(pci_priv);
  1143. if (ret)
  1144. do_force_wake_put = false;
  1145. ret = cnss_pci_reg_write(pci_priv, offset, val);
  1146. if (ret) {
  1147. cnss_pr_err("Failed to write 0x%x to register offset 0x%x, err = %d\n",
  1148. val, offset, ret);
  1149. goto force_wake_put;
  1150. }
  1151. force_wake_put:
  1152. if (do_force_wake_put)
  1153. cnss_pci_force_wake_put(pci_priv);
  1154. runtime_pm_put:
  1155. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  1156. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  1157. out:
  1158. return ret;
  1159. }
  1160. static int cnss_set_pci_config_space(struct cnss_pci_data *pci_priv, bool save)
  1161. {
  1162. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1163. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1164. bool link_down_or_recovery;
  1165. if (!plat_priv)
  1166. return -ENODEV;
  1167. link_down_or_recovery = pci_priv->pci_link_down_ind ||
  1168. (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state));
  1169. if (save) {
  1170. if (link_down_or_recovery) {
  1171. pci_priv->saved_state = NULL;
  1172. } else {
  1173. pci_save_state(pci_dev);
  1174. pci_priv->saved_state = pci_store_saved_state(pci_dev);
  1175. }
  1176. } else {
  1177. if (link_down_or_recovery) {
  1178. pci_load_saved_state(pci_dev, pci_priv->default_state);
  1179. pci_restore_state(pci_dev);
  1180. } else if (pci_priv->saved_state) {
  1181. pci_load_and_free_saved_state(pci_dev,
  1182. &pci_priv->saved_state);
  1183. pci_restore_state(pci_dev);
  1184. }
  1185. }
  1186. return 0;
  1187. }
  1188. static int cnss_pci_get_link_status(struct cnss_pci_data *pci_priv)
  1189. {
  1190. u16 link_status;
  1191. int ret;
  1192. ret = pcie_capability_read_word(pci_priv->pci_dev, PCI_EXP_LNKSTA,
  1193. &link_status);
  1194. if (ret)
  1195. return ret;
  1196. cnss_pr_dbg("Get PCI link status register: %u\n", link_status);
  1197. pci_priv->def_link_speed = link_status & PCI_EXP_LNKSTA_CLS;
  1198. pci_priv->def_link_width =
  1199. (link_status & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
  1200. pci_priv->cur_link_speed = pci_priv->def_link_speed;
  1201. cnss_pr_dbg("Default PCI link speed is 0x%x, link width is 0x%x\n",
  1202. pci_priv->def_link_speed, pci_priv->def_link_width);
  1203. return 0;
  1204. }
  1205. static int cnss_set_pci_link_status(struct cnss_pci_data *pci_priv,
  1206. enum pci_link_status status)
  1207. {
  1208. u16 link_speed, link_width = pci_priv->def_link_width;
  1209. u16 one_lane = PCI_EXP_LNKSTA_NLW_X1 >> PCI_EXP_LNKSTA_NLW_SHIFT;
  1210. int ret;
  1211. cnss_pr_vdbg("Set PCI link status to: %u\n", status);
  1212. switch (status) {
  1213. case PCI_GEN1:
  1214. link_speed = PCI_EXP_LNKSTA_CLS_2_5GB;
  1215. if (!link_width)
  1216. link_width = one_lane;
  1217. break;
  1218. case PCI_GEN2:
  1219. link_speed = PCI_EXP_LNKSTA_CLS_5_0GB;
  1220. if (!link_width)
  1221. link_width = one_lane;
  1222. break;
  1223. case PCI_DEF:
  1224. link_speed = pci_priv->def_link_speed;
  1225. if (!link_speed || !link_width) {
  1226. cnss_pr_err("PCI link speed or width is not valid\n");
  1227. return -EINVAL;
  1228. }
  1229. break;
  1230. default:
  1231. cnss_pr_err("Unknown PCI link status config: %u\n", status);
  1232. return -EINVAL;
  1233. }
  1234. ret = cnss_pci_set_link_bandwidth(pci_priv, link_speed, link_width);
  1235. if (!ret)
  1236. pci_priv->cur_link_speed = link_speed;
  1237. return ret;
  1238. }
  1239. static int cnss_set_pci_link(struct cnss_pci_data *pci_priv, bool link_up)
  1240. {
  1241. int ret = 0, retry = 0;
  1242. cnss_pr_vdbg("%s PCI link\n", link_up ? "Resuming" : "Suspending");
  1243. if (link_up) {
  1244. retry:
  1245. ret = cnss_pci_set_link_up(pci_priv);
  1246. if (ret && retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  1247. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  1248. if (pci_priv->pci_link_down_ind)
  1249. msleep(LINK_TRAINING_RETRY_DELAY_MS * retry);
  1250. goto retry;
  1251. }
  1252. } else {
  1253. /* Since DRV suspend cannot be done in Gen 3, set it to
  1254. * Gen 2 if current link speed is larger than Gen 2.
  1255. */
  1256. if (pci_priv->drv_connected_last &&
  1257. pci_priv->cur_link_speed > PCI_EXP_LNKSTA_CLS_5_0GB)
  1258. cnss_set_pci_link_status(pci_priv, PCI_GEN2);
  1259. ret = cnss_pci_set_link_down(pci_priv);
  1260. }
  1261. if (pci_priv->drv_connected_last) {
  1262. if ((link_up && !ret) || (!link_up && ret))
  1263. cnss_set_pci_link_status(pci_priv, PCI_DEF);
  1264. }
  1265. return ret;
  1266. }
  1267. static void cnss_pci_soc_scratch_reg_dump(struct cnss_pci_data *pci_priv)
  1268. {
  1269. u32 reg_offset, val;
  1270. int i;
  1271. switch (pci_priv->device_id) {
  1272. case QCA6390_DEVICE_ID:
  1273. case QCA6490_DEVICE_ID:
  1274. break;
  1275. default:
  1276. return;
  1277. }
  1278. if (in_interrupt() || irqs_disabled())
  1279. return;
  1280. if (cnss_pci_check_link_status(pci_priv))
  1281. return;
  1282. cnss_pr_dbg("Start to dump SOC Scratch registers\n");
  1283. for (i = 0; pci_scratch[i].name; i++) {
  1284. reg_offset = pci_scratch[i].offset;
  1285. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  1286. return;
  1287. cnss_pr_dbg("PCIE_SOC_REG_%s = 0x%x\n",
  1288. pci_scratch[i].name, val);
  1289. }
  1290. }
  1291. int cnss_suspend_pci_link(struct cnss_pci_data *pci_priv)
  1292. {
  1293. int ret = 0;
  1294. if (!pci_priv)
  1295. return -ENODEV;
  1296. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1297. cnss_pr_info("PCI link is already suspended\n");
  1298. goto out;
  1299. }
  1300. pci_clear_master(pci_priv->pci_dev);
  1301. ret = cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  1302. if (ret)
  1303. goto out;
  1304. pci_disable_device(pci_priv->pci_dev);
  1305. if (pci_priv->pci_dev->device != QCA6174_DEVICE_ID) {
  1306. if (pci_set_power_state(pci_priv->pci_dev, PCI_D3hot))
  1307. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  1308. }
  1309. /* Always do PCIe L2 suspend during power off/PCIe link recovery */
  1310. pci_priv->drv_connected_last = 0;
  1311. ret = cnss_set_pci_link(pci_priv, PCI_LINK_DOWN);
  1312. if (ret)
  1313. goto out;
  1314. pci_priv->pci_link_state = PCI_LINK_DOWN;
  1315. return 0;
  1316. out:
  1317. return ret;
  1318. }
  1319. int cnss_resume_pci_link(struct cnss_pci_data *pci_priv)
  1320. {
  1321. int ret = 0;
  1322. if (!pci_priv)
  1323. return -ENODEV;
  1324. if (pci_priv->pci_link_state == PCI_LINK_UP) {
  1325. cnss_pr_info("PCI link is already resumed\n");
  1326. goto out;
  1327. }
  1328. ret = cnss_set_pci_link(pci_priv, PCI_LINK_UP);
  1329. if (ret) {
  1330. ret = -EAGAIN;
  1331. goto out;
  1332. }
  1333. pci_priv->pci_link_state = PCI_LINK_UP;
  1334. if (pci_priv->pci_dev->device != QCA6174_DEVICE_ID) {
  1335. ret = pci_set_power_state(pci_priv->pci_dev, PCI_D0);
  1336. if (ret) {
  1337. cnss_pr_err("Failed to set D0, err = %d\n", ret);
  1338. goto out;
  1339. }
  1340. }
  1341. ret = pci_enable_device(pci_priv->pci_dev);
  1342. if (ret) {
  1343. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  1344. goto out;
  1345. }
  1346. ret = cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  1347. if (ret)
  1348. goto out;
  1349. pci_set_master(pci_priv->pci_dev);
  1350. if (pci_priv->pci_link_down_ind)
  1351. pci_priv->pci_link_down_ind = false;
  1352. return 0;
  1353. out:
  1354. return ret;
  1355. }
  1356. int cnss_pci_recover_link_down(struct cnss_pci_data *pci_priv)
  1357. {
  1358. int ret;
  1359. switch (pci_priv->device_id) {
  1360. case QCA6390_DEVICE_ID:
  1361. case QCA6490_DEVICE_ID:
  1362. case KIWI_DEVICE_ID:
  1363. break;
  1364. default:
  1365. return -EOPNOTSUPP;
  1366. }
  1367. /* Always wait here to avoid missing WAKE assert for RDDM
  1368. * before link recovery
  1369. */
  1370. msleep(WAKE_EVENT_TIMEOUT);
  1371. ret = cnss_suspend_pci_link(pci_priv);
  1372. if (ret)
  1373. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  1374. ret = cnss_resume_pci_link(pci_priv);
  1375. if (ret) {
  1376. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  1377. del_timer(&pci_priv->dev_rddm_timer);
  1378. return ret;
  1379. }
  1380. mod_timer(&pci_priv->dev_rddm_timer,
  1381. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1382. cnss_mhi_debug_reg_dump(pci_priv);
  1383. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1384. return 0;
  1385. }
  1386. int cnss_pci_prevent_l1(struct device *dev)
  1387. {
  1388. struct pci_dev *pci_dev = to_pci_dev(dev);
  1389. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1390. int ret;
  1391. if (!pci_priv) {
  1392. cnss_pr_err("pci_priv is NULL\n");
  1393. return -ENODEV;
  1394. }
  1395. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1396. cnss_pr_dbg("PCIe link is in suspend state\n");
  1397. return -EIO;
  1398. }
  1399. if (pci_priv->pci_link_down_ind) {
  1400. cnss_pr_err("PCIe link is down\n");
  1401. return -EIO;
  1402. }
  1403. ret = _cnss_pci_prevent_l1(pci_priv);
  1404. if (ret == -EIO) {
  1405. cnss_pr_err("Failed to prevent PCIe L1, considered as link down\n");
  1406. cnss_pci_link_down(dev);
  1407. }
  1408. return ret;
  1409. }
  1410. EXPORT_SYMBOL(cnss_pci_prevent_l1);
  1411. void cnss_pci_allow_l1(struct device *dev)
  1412. {
  1413. struct pci_dev *pci_dev = to_pci_dev(dev);
  1414. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1415. if (!pci_priv) {
  1416. cnss_pr_err("pci_priv is NULL\n");
  1417. return;
  1418. }
  1419. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1420. cnss_pr_dbg("PCIe link is in suspend state\n");
  1421. return;
  1422. }
  1423. if (pci_priv->pci_link_down_ind) {
  1424. cnss_pr_err("PCIe link is down\n");
  1425. return;
  1426. }
  1427. _cnss_pci_allow_l1(pci_priv);
  1428. }
  1429. EXPORT_SYMBOL(cnss_pci_allow_l1);
  1430. static void cnss_pci_update_link_event(struct cnss_pci_data *pci_priv,
  1431. enum cnss_bus_event_type type,
  1432. void *data)
  1433. {
  1434. struct cnss_bus_event bus_event;
  1435. bus_event.etype = type;
  1436. bus_event.event_data = data;
  1437. cnss_pci_call_driver_uevent(pci_priv, CNSS_BUS_EVENT, &bus_event);
  1438. }
  1439. static void cnss_pci_handle_linkdown(struct cnss_pci_data *pci_priv)
  1440. {
  1441. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1442. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1443. unsigned long flags;
  1444. if (test_bit(ENABLE_PCI_LINK_DOWN_PANIC,
  1445. &plat_priv->ctrl_params.quirks))
  1446. panic("cnss: PCI link is down\n");
  1447. spin_lock_irqsave(&pci_link_down_lock, flags);
  1448. if (pci_priv->pci_link_down_ind) {
  1449. cnss_pr_dbg("PCI link down recovery is in progress, ignore\n");
  1450. spin_unlock_irqrestore(&pci_link_down_lock, flags);
  1451. return;
  1452. }
  1453. pci_priv->pci_link_down_ind = true;
  1454. spin_unlock_irqrestore(&pci_link_down_lock, flags);
  1455. if (pci_dev->device == QCA6174_DEVICE_ID)
  1456. disable_irq(pci_dev->irq);
  1457. /* Notify bus related event. Now for all supported chips.
  1458. * Here PCIe LINK_DOWN notification taken care.
  1459. * uevent buffer can be extended later, to cover more bus info.
  1460. */
  1461. cnss_pci_update_link_event(pci_priv, BUS_EVENT_PCI_LINK_DOWN, NULL);
  1462. cnss_fatal_err("PCI link down, schedule recovery\n");
  1463. cnss_schedule_recovery(&pci_dev->dev, CNSS_REASON_LINK_DOWN);
  1464. }
  1465. int cnss_pci_link_down(struct device *dev)
  1466. {
  1467. struct pci_dev *pci_dev = to_pci_dev(dev);
  1468. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1469. struct cnss_plat_data *plat_priv = NULL;
  1470. int ret;
  1471. if (!pci_priv) {
  1472. cnss_pr_err("pci_priv is NULL\n");
  1473. return -EINVAL;
  1474. }
  1475. plat_priv = pci_priv->plat_priv;
  1476. if (!plat_priv) {
  1477. cnss_pr_err("plat_priv is NULL\n");
  1478. return -ENODEV;
  1479. }
  1480. if (pci_priv->pci_link_down_ind) {
  1481. cnss_pr_dbg("PCI link down recovery is already in progress\n");
  1482. return -EBUSY;
  1483. }
  1484. if (pci_priv->drv_connected_last &&
  1485. of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  1486. "cnss-enable-self-recovery"))
  1487. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  1488. cnss_pr_err("PCI link down is detected by drivers\n");
  1489. ret = cnss_pci_assert_perst(pci_priv);
  1490. if (ret)
  1491. cnss_pci_handle_linkdown(pci_priv);
  1492. return ret;
  1493. }
  1494. EXPORT_SYMBOL(cnss_pci_link_down);
  1495. int cnss_pcie_is_device_down(struct cnss_pci_data *pci_priv)
  1496. {
  1497. struct cnss_plat_data *plat_priv;
  1498. if (!pci_priv) {
  1499. cnss_pr_err("pci_priv is NULL\n");
  1500. return -ENODEV;
  1501. }
  1502. plat_priv = pci_priv->plat_priv;
  1503. if (!plat_priv) {
  1504. cnss_pr_err("plat_priv is NULL\n");
  1505. return -ENODEV;
  1506. }
  1507. return test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) |
  1508. pci_priv->pci_link_down_ind;
  1509. }
  1510. int cnss_pci_is_device_down(struct device *dev)
  1511. {
  1512. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  1513. return cnss_pcie_is_device_down(pci_priv);
  1514. }
  1515. EXPORT_SYMBOL(cnss_pci_is_device_down);
  1516. void cnss_pci_lock_reg_window(struct device *dev, unsigned long *flags)
  1517. {
  1518. spin_lock_bh(&pci_reg_window_lock);
  1519. }
  1520. EXPORT_SYMBOL(cnss_pci_lock_reg_window);
  1521. void cnss_pci_unlock_reg_window(struct device *dev, unsigned long *flags)
  1522. {
  1523. spin_unlock_bh(&pci_reg_window_lock);
  1524. }
  1525. EXPORT_SYMBOL(cnss_pci_unlock_reg_window);
  1526. int cnss_get_pci_slot(struct device *dev)
  1527. {
  1528. struct pci_dev *pci_dev = to_pci_dev(dev);
  1529. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1530. struct cnss_plat_data *plat_priv = NULL;
  1531. if (!pci_priv) {
  1532. cnss_pr_err("pci_priv is NULL\n");
  1533. return -EINVAL;
  1534. }
  1535. plat_priv = pci_priv->plat_priv;
  1536. if (!plat_priv) {
  1537. cnss_pr_err("plat_priv is NULL\n");
  1538. return -ENODEV;
  1539. }
  1540. return plat_priv->rc_num;
  1541. }
  1542. EXPORT_SYMBOL(cnss_get_pci_slot);
  1543. /**
  1544. * cnss_pci_dump_bl_sram_mem - Dump WLAN device bootloader debug log
  1545. * @pci_priv: driver PCI bus context pointer
  1546. *
  1547. * Dump primary and secondary bootloader debug log data. For SBL check the
  1548. * log struct address and size for validity.
  1549. *
  1550. * Return: None
  1551. */
  1552. static void cnss_pci_dump_bl_sram_mem(struct cnss_pci_data *pci_priv)
  1553. {
  1554. u32 mem_addr, val, pbl_log_max_size, sbl_log_max_size;
  1555. u32 pbl_log_sram_start;
  1556. u32 pbl_stage, sbl_log_start, sbl_log_size;
  1557. u32 pbl_wlan_boot_cfg, pbl_bootstrap_status;
  1558. u32 pbl_bootstrap_status_reg = PBL_BOOTSTRAP_STATUS;
  1559. u32 sbl_log_def_start = SRAM_START;
  1560. u32 sbl_log_def_end = SRAM_END;
  1561. int i;
  1562. switch (pci_priv->device_id) {
  1563. case QCA6390_DEVICE_ID:
  1564. pbl_log_sram_start = QCA6390_DEBUG_PBL_LOG_SRAM_START;
  1565. pbl_log_max_size = QCA6390_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1566. sbl_log_max_size = QCA6390_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1567. break;
  1568. case QCA6490_DEVICE_ID:
  1569. pbl_log_sram_start = QCA6490_DEBUG_PBL_LOG_SRAM_START;
  1570. pbl_log_max_size = QCA6490_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1571. sbl_log_max_size = QCA6490_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1572. break;
  1573. case KIWI_DEVICE_ID:
  1574. pbl_bootstrap_status_reg = KIWI_PBL_BOOTSTRAP_STATUS;
  1575. pbl_log_sram_start = KIWI_DEBUG_PBL_LOG_SRAM_START;
  1576. pbl_log_max_size = KIWI_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1577. sbl_log_max_size = KIWI_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1578. break;
  1579. default:
  1580. return;
  1581. }
  1582. if (cnss_pci_check_link_status(pci_priv))
  1583. return;
  1584. cnss_pci_reg_read(pci_priv, TCSR_PBL_LOGGING_REG, &pbl_stage);
  1585. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG2_REG, &sbl_log_start);
  1586. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG3_REG, &sbl_log_size);
  1587. cnss_pci_reg_read(pci_priv, PBL_WLAN_BOOT_CFG, &pbl_wlan_boot_cfg);
  1588. cnss_pci_reg_read(pci_priv, pbl_bootstrap_status_reg,
  1589. &pbl_bootstrap_status);
  1590. cnss_pr_dbg("TCSR_PBL_LOGGING: 0x%08x PCIE_BHI_ERRDBG: Start: 0x%08x Size:0x%08x\n",
  1591. pbl_stage, sbl_log_start, sbl_log_size);
  1592. cnss_pr_dbg("PBL_WLAN_BOOT_CFG: 0x%08x PBL_BOOTSTRAP_STATUS: 0x%08x\n",
  1593. pbl_wlan_boot_cfg, pbl_bootstrap_status);
  1594. cnss_pr_dbg("Dumping PBL log data\n");
  1595. for (i = 0; i < pbl_log_max_size; i += sizeof(val)) {
  1596. mem_addr = pbl_log_sram_start + i;
  1597. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1598. break;
  1599. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1600. }
  1601. sbl_log_size = (sbl_log_size > sbl_log_max_size ?
  1602. sbl_log_max_size : sbl_log_size);
  1603. if (sbl_log_start < sbl_log_def_start ||
  1604. sbl_log_start > sbl_log_def_end ||
  1605. (sbl_log_start + sbl_log_size) > sbl_log_def_end) {
  1606. cnss_pr_err("Invalid SBL log data\n");
  1607. return;
  1608. }
  1609. cnss_pr_dbg("Dumping SBL log data\n");
  1610. for (i = 0; i < sbl_log_size; i += sizeof(val)) {
  1611. mem_addr = sbl_log_start + i;
  1612. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1613. break;
  1614. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1615. }
  1616. }
  1617. static int cnss_pci_handle_mhi_poweron_timeout(struct cnss_pci_data *pci_priv)
  1618. {
  1619. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1620. cnss_fatal_err("MHI power up returns timeout\n");
  1621. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE) ||
  1622. cnss_get_dev_sol_value(plat_priv) > 0) {
  1623. /* Wait for RDDM if RDDM cookie is set or device SOL GPIO is
  1624. * high. If RDDM times out, PBL/SBL error region may have been
  1625. * erased so no need to dump them either.
  1626. */
  1627. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  1628. !pci_priv->pci_link_down_ind) {
  1629. mod_timer(&pci_priv->dev_rddm_timer,
  1630. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1631. }
  1632. } else {
  1633. cnss_pr_dbg("RDDM cookie is not set and device SOL is low\n");
  1634. cnss_mhi_debug_reg_dump(pci_priv);
  1635. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1636. /* Dump PBL/SBL error log if RDDM cookie is not set */
  1637. cnss_pci_dump_bl_sram_mem(pci_priv);
  1638. return -ETIMEDOUT;
  1639. }
  1640. return 0;
  1641. }
  1642. static char *cnss_mhi_state_to_str(enum cnss_mhi_state mhi_state)
  1643. {
  1644. switch (mhi_state) {
  1645. case CNSS_MHI_INIT:
  1646. return "INIT";
  1647. case CNSS_MHI_DEINIT:
  1648. return "DEINIT";
  1649. case CNSS_MHI_POWER_ON:
  1650. return "POWER_ON";
  1651. case CNSS_MHI_POWERING_OFF:
  1652. return "POWERING_OFF";
  1653. case CNSS_MHI_POWER_OFF:
  1654. return "POWER_OFF";
  1655. case CNSS_MHI_FORCE_POWER_OFF:
  1656. return "FORCE_POWER_OFF";
  1657. case CNSS_MHI_SUSPEND:
  1658. return "SUSPEND";
  1659. case CNSS_MHI_RESUME:
  1660. return "RESUME";
  1661. case CNSS_MHI_TRIGGER_RDDM:
  1662. return "TRIGGER_RDDM";
  1663. case CNSS_MHI_RDDM_DONE:
  1664. return "RDDM_DONE";
  1665. default:
  1666. return "UNKNOWN";
  1667. }
  1668. };
  1669. static int cnss_pci_check_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1670. enum cnss_mhi_state mhi_state)
  1671. {
  1672. switch (mhi_state) {
  1673. case CNSS_MHI_INIT:
  1674. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state))
  1675. return 0;
  1676. break;
  1677. case CNSS_MHI_DEINIT:
  1678. case CNSS_MHI_POWER_ON:
  1679. if (test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state) &&
  1680. !test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1681. return 0;
  1682. break;
  1683. case CNSS_MHI_FORCE_POWER_OFF:
  1684. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1685. return 0;
  1686. break;
  1687. case CNSS_MHI_POWER_OFF:
  1688. case CNSS_MHI_SUSPEND:
  1689. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1690. !test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1691. return 0;
  1692. break;
  1693. case CNSS_MHI_RESUME:
  1694. if (test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1695. return 0;
  1696. break;
  1697. case CNSS_MHI_TRIGGER_RDDM:
  1698. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1699. !test_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state))
  1700. return 0;
  1701. break;
  1702. case CNSS_MHI_RDDM_DONE:
  1703. return 0;
  1704. default:
  1705. cnss_pr_err("Unhandled MHI state: %s(%d)\n",
  1706. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1707. }
  1708. cnss_pr_err("Cannot set MHI state %s(%d) in current MHI state (0x%lx)\n",
  1709. cnss_mhi_state_to_str(mhi_state), mhi_state,
  1710. pci_priv->mhi_state);
  1711. if (mhi_state != CNSS_MHI_TRIGGER_RDDM)
  1712. CNSS_ASSERT(0);
  1713. return -EINVAL;
  1714. }
  1715. static void cnss_pci_set_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1716. enum cnss_mhi_state mhi_state)
  1717. {
  1718. switch (mhi_state) {
  1719. case CNSS_MHI_INIT:
  1720. set_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1721. break;
  1722. case CNSS_MHI_DEINIT:
  1723. clear_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1724. break;
  1725. case CNSS_MHI_POWER_ON:
  1726. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1727. break;
  1728. case CNSS_MHI_POWERING_OFF:
  1729. set_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1730. break;
  1731. case CNSS_MHI_POWER_OFF:
  1732. case CNSS_MHI_FORCE_POWER_OFF:
  1733. clear_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1734. clear_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1735. clear_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1736. clear_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1737. break;
  1738. case CNSS_MHI_SUSPEND:
  1739. set_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1740. break;
  1741. case CNSS_MHI_RESUME:
  1742. clear_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1743. break;
  1744. case CNSS_MHI_TRIGGER_RDDM:
  1745. set_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1746. break;
  1747. case CNSS_MHI_RDDM_DONE:
  1748. set_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1749. break;
  1750. default:
  1751. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1752. }
  1753. }
  1754. static int cnss_pci_set_mhi_state(struct cnss_pci_data *pci_priv,
  1755. enum cnss_mhi_state mhi_state)
  1756. {
  1757. int ret = 0, retry = 0;
  1758. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  1759. return 0;
  1760. if (mhi_state < 0) {
  1761. cnss_pr_err("Invalid MHI state (%d)\n", mhi_state);
  1762. return -EINVAL;
  1763. }
  1764. ret = cnss_pci_check_mhi_state_bit(pci_priv, mhi_state);
  1765. if (ret)
  1766. goto out;
  1767. cnss_pr_vdbg("Setting MHI state: %s(%d)\n",
  1768. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1769. switch (mhi_state) {
  1770. case CNSS_MHI_INIT:
  1771. ret = mhi_prepare_for_power_up(pci_priv->mhi_ctrl);
  1772. break;
  1773. case CNSS_MHI_DEINIT:
  1774. mhi_unprepare_after_power_down(pci_priv->mhi_ctrl);
  1775. ret = 0;
  1776. break;
  1777. case CNSS_MHI_POWER_ON:
  1778. ret = mhi_sync_power_up(pci_priv->mhi_ctrl);
  1779. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  1780. /* Only set img_pre_alloc when power up succeeds */
  1781. if (!ret && !pci_priv->mhi_ctrl->img_pre_alloc) {
  1782. cnss_pr_dbg("Notify MHI to use already allocated images\n");
  1783. pci_priv->mhi_ctrl->img_pre_alloc = true;
  1784. }
  1785. #endif
  1786. break;
  1787. case CNSS_MHI_POWER_OFF:
  1788. mhi_power_down(pci_priv->mhi_ctrl, true);
  1789. ret = 0;
  1790. break;
  1791. case CNSS_MHI_FORCE_POWER_OFF:
  1792. mhi_power_down(pci_priv->mhi_ctrl, false);
  1793. ret = 0;
  1794. break;
  1795. case CNSS_MHI_SUSPEND:
  1796. retry_mhi_suspend:
  1797. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1798. if (pci_priv->drv_connected_last)
  1799. ret = cnss_mhi_pm_fast_suspend(pci_priv, true);
  1800. else
  1801. ret = mhi_pm_suspend(pci_priv->mhi_ctrl);
  1802. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1803. if (ret == -EBUSY && retry++ < MHI_SUSPEND_RETRY_MAX_TIMES) {
  1804. cnss_pr_dbg("Retry MHI suspend #%d\n", retry);
  1805. usleep_range(MHI_SUSPEND_RETRY_DELAY_US,
  1806. MHI_SUSPEND_RETRY_DELAY_US + 1000);
  1807. goto retry_mhi_suspend;
  1808. }
  1809. break;
  1810. case CNSS_MHI_RESUME:
  1811. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1812. if (pci_priv->drv_connected_last) {
  1813. ret = cnss_pci_prevent_l1(&pci_priv->pci_dev->dev);
  1814. if (ret) {
  1815. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1816. break;
  1817. }
  1818. ret = cnss_mhi_pm_fast_resume(pci_priv, true);
  1819. cnss_pci_allow_l1(&pci_priv->pci_dev->dev);
  1820. } else {
  1821. ret = mhi_pm_resume(pci_priv->mhi_ctrl);
  1822. }
  1823. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1824. break;
  1825. case CNSS_MHI_TRIGGER_RDDM:
  1826. ret = mhi_force_rddm_mode(pci_priv->mhi_ctrl);
  1827. if (ret) {
  1828. cnss_pr_err("Failed to trigger RDDM, err = %d\n", ret);
  1829. cnss_pr_dbg("Sending host reset req\n");
  1830. ret = cnss_mhi_force_reset(pci_priv);
  1831. }
  1832. break;
  1833. case CNSS_MHI_RDDM_DONE:
  1834. break;
  1835. default:
  1836. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1837. ret = -EINVAL;
  1838. }
  1839. if (ret)
  1840. goto out;
  1841. cnss_pci_set_mhi_state_bit(pci_priv, mhi_state);
  1842. return 0;
  1843. out:
  1844. cnss_pr_err("Failed to set MHI state: %s(%d), err = %d\n",
  1845. cnss_mhi_state_to_str(mhi_state), mhi_state, ret);
  1846. return ret;
  1847. }
  1848. #if IS_ENABLED(CONFIG_PCI_MSM)
  1849. /**
  1850. * cnss_wlan_adsp_pc_enable: Control ADSP power collapse setup
  1851. * @dev: Platform driver pci private data structure
  1852. * @control: Power collapse enable / disable
  1853. *
  1854. * This function controls ADSP power collapse (PC). It must be called
  1855. * based on wlan state. ADSP power collapse during wlan RTPM suspend state
  1856. * results in delay during periodic QMI stats PCI link up/down. This delay
  1857. * causes additional power consumption.
  1858. * Introduced in SM8350.
  1859. *
  1860. * Result: 0 Success. negative error codes.
  1861. */
  1862. static int cnss_wlan_adsp_pc_enable(struct cnss_pci_data *pci_priv,
  1863. bool control)
  1864. {
  1865. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1866. int ret = 0;
  1867. u32 pm_options = PM_OPTIONS_DEFAULT;
  1868. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1869. if (plat_priv->adsp_pc_enabled == control) {
  1870. cnss_pr_dbg("ADSP power collapse already %s\n",
  1871. control ? "Enabled" : "Disabled");
  1872. return 0;
  1873. }
  1874. if (control)
  1875. pm_options &= ~MSM_PCIE_CONFIG_NO_DRV_PC;
  1876. else
  1877. pm_options |= MSM_PCIE_CONFIG_NO_DRV_PC;
  1878. ret = msm_pcie_pm_control(MSM_PCIE_DRV_PC_CTRL, pci_dev->bus->number,
  1879. pci_dev, NULL, pm_options);
  1880. if (ret)
  1881. return ret;
  1882. cnss_pr_dbg("%s ADSP power collapse\n", control ? "Enable" : "Disable");
  1883. plat_priv->adsp_pc_enabled = control;
  1884. return 0;
  1885. }
  1886. #else
  1887. static int cnss_wlan_adsp_pc_enable(struct cnss_pci_data *pci_priv,
  1888. bool control)
  1889. {
  1890. return 0;
  1891. }
  1892. #endif
  1893. int cnss_pci_start_mhi(struct cnss_pci_data *pci_priv)
  1894. {
  1895. int ret = 0;
  1896. struct cnss_plat_data *plat_priv;
  1897. unsigned int timeout = 0;
  1898. if (!pci_priv) {
  1899. cnss_pr_err("pci_priv is NULL\n");
  1900. return -ENODEV;
  1901. }
  1902. plat_priv = pci_priv->plat_priv;
  1903. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1904. return 0;
  1905. if (MHI_TIMEOUT_OVERWRITE_MS)
  1906. pci_priv->mhi_ctrl->timeout_ms = MHI_TIMEOUT_OVERWRITE_MS;
  1907. cnss_mhi_set_m2_timeout_ms(pci_priv, MHI_M2_TIMEOUT_MS);
  1908. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_INIT);
  1909. if (ret)
  1910. return ret;
  1911. timeout = pci_priv->mhi_ctrl->timeout_ms;
  1912. /* For non-perf builds the timeout is 10 (default) * 6 seconds */
  1913. if (cnss_get_host_build_type() == QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  1914. pci_priv->mhi_ctrl->timeout_ms *= 6;
  1915. else /* For perf builds the timeout is 10 (default) * 3 seconds */
  1916. pci_priv->mhi_ctrl->timeout_ms *= 3;
  1917. /* Start the timer to dump MHI/PBL/SBL debug data periodically */
  1918. mod_timer(&pci_priv->boot_debug_timer,
  1919. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  1920. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_ON);
  1921. del_timer(&pci_priv->boot_debug_timer);
  1922. if (ret == 0)
  1923. cnss_wlan_adsp_pc_enable(pci_priv, false);
  1924. pci_priv->mhi_ctrl->timeout_ms = timeout;
  1925. if (ret == -ETIMEDOUT) {
  1926. /* This is a special case needs to be handled that if MHI
  1927. * power on returns -ETIMEDOUT, controller needs to take care
  1928. * the cleanup by calling MHI power down. Force to set the bit
  1929. * for driver internal MHI state to make sure it can be handled
  1930. * properly later.
  1931. */
  1932. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1933. ret = cnss_pci_handle_mhi_poweron_timeout(pci_priv);
  1934. }
  1935. return ret;
  1936. }
  1937. static void cnss_pci_power_off_mhi(struct cnss_pci_data *pci_priv)
  1938. {
  1939. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1940. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1941. return;
  1942. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state)) {
  1943. cnss_pr_dbg("MHI is already powered off\n");
  1944. return;
  1945. }
  1946. cnss_wlan_adsp_pc_enable(pci_priv, true);
  1947. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_RESUME);
  1948. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_POWERING_OFF);
  1949. if (!pci_priv->pci_link_down_ind)
  1950. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_OFF);
  1951. else
  1952. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_FORCE_POWER_OFF);
  1953. }
  1954. static void cnss_pci_deinit_mhi(struct cnss_pci_data *pci_priv)
  1955. {
  1956. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1957. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1958. return;
  1959. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state)) {
  1960. cnss_pr_dbg("MHI is already deinited\n");
  1961. return;
  1962. }
  1963. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_DEINIT);
  1964. }
  1965. static void cnss_pci_set_wlaon_pwr_ctrl(struct cnss_pci_data *pci_priv,
  1966. bool set_vddd4blow, bool set_shutdown,
  1967. bool do_force_wake)
  1968. {
  1969. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1970. int ret;
  1971. u32 val;
  1972. if (!plat_priv->set_wlaon_pwr_ctrl)
  1973. return;
  1974. if (pci_priv->pci_link_state == PCI_LINK_DOWN ||
  1975. pci_priv->pci_link_down_ind)
  1976. return;
  1977. if (do_force_wake)
  1978. if (cnss_pci_force_wake_get(pci_priv))
  1979. return;
  1980. ret = cnss_pci_reg_read(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, &val);
  1981. if (ret) {
  1982. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  1983. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1984. goto force_wake_put;
  1985. }
  1986. cnss_pr_dbg("Read register offset 0x%x, val = 0x%x\n",
  1987. WLAON_QFPROM_PWR_CTRL_REG, val);
  1988. if (set_vddd4blow)
  1989. val |= QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1990. else
  1991. val &= ~QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1992. if (set_shutdown)
  1993. val |= QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1994. else
  1995. val &= ~QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1996. ret = cnss_pci_reg_write(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, val);
  1997. if (ret) {
  1998. cnss_pr_err("Failed to write register offset 0x%x, err = %d\n",
  1999. WLAON_QFPROM_PWR_CTRL_REG, ret);
  2000. goto force_wake_put;
  2001. }
  2002. cnss_pr_dbg("Write val 0x%x to register offset 0x%x\n", val,
  2003. WLAON_QFPROM_PWR_CTRL_REG);
  2004. if (set_shutdown)
  2005. usleep_range(WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US,
  2006. WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US);
  2007. force_wake_put:
  2008. if (do_force_wake)
  2009. cnss_pci_force_wake_put(pci_priv);
  2010. }
  2011. static int cnss_pci_get_device_timestamp(struct cnss_pci_data *pci_priv,
  2012. u64 *time_us)
  2013. {
  2014. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2015. u32 low, high;
  2016. u64 device_ticks;
  2017. if (!plat_priv->device_freq_hz) {
  2018. cnss_pr_err("Device time clock frequency is not valid\n");
  2019. return -EINVAL;
  2020. }
  2021. switch (pci_priv->device_id) {
  2022. case KIWI_DEVICE_ID:
  2023. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_LOW, &low);
  2024. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_HIGH, &high);
  2025. break;
  2026. default:
  2027. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL3, &low);
  2028. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL4, &high);
  2029. break;
  2030. }
  2031. device_ticks = (u64)high << 32 | low;
  2032. do_div(device_ticks, plat_priv->device_freq_hz / 100000);
  2033. *time_us = device_ticks * 10;
  2034. return 0;
  2035. }
  2036. static void cnss_pci_enable_time_sync_counter(struct cnss_pci_data *pci_priv)
  2037. {
  2038. switch (pci_priv->device_id) {
  2039. case KIWI_DEVICE_ID:
  2040. return;
  2041. default:
  2042. break;
  2043. }
  2044. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  2045. TIME_SYNC_ENABLE);
  2046. }
  2047. static void cnss_pci_clear_time_sync_counter(struct cnss_pci_data *pci_priv)
  2048. {
  2049. switch (pci_priv->device_id) {
  2050. case KIWI_DEVICE_ID:
  2051. return;
  2052. default:
  2053. break;
  2054. }
  2055. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  2056. TIME_SYNC_CLEAR);
  2057. }
  2058. static void cnss_pci_time_sync_reg_update(struct cnss_pci_data *pci_priv,
  2059. u32 low, u32 high)
  2060. {
  2061. u32 time_reg_low = PCIE_SHADOW_REG_VALUE_0;
  2062. u32 time_reg_high = PCIE_SHADOW_REG_VALUE_1;
  2063. switch (pci_priv->device_id) {
  2064. case KIWI_DEVICE_ID:
  2065. /* Forward compatibility */
  2066. break;
  2067. default:
  2068. time_reg_low = PCIE_SHADOW_REG_VALUE_34;
  2069. time_reg_high = PCIE_SHADOW_REG_VALUE_35;
  2070. break;
  2071. }
  2072. cnss_pci_reg_write(pci_priv, time_reg_low, low);
  2073. cnss_pci_reg_write(pci_priv, time_reg_high, high);
  2074. cnss_pci_reg_read(pci_priv, time_reg_low, &low);
  2075. cnss_pci_reg_read(pci_priv, time_reg_high, &high);
  2076. cnss_pr_dbg("Updated time sync regs [0x%x] = 0x%x, [0x%x] = 0x%x\n",
  2077. time_reg_low, low, time_reg_high, high);
  2078. }
  2079. static int cnss_pci_update_timestamp(struct cnss_pci_data *pci_priv)
  2080. {
  2081. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2082. struct device *dev = &pci_priv->pci_dev->dev;
  2083. unsigned long flags = 0;
  2084. u64 host_time_us, device_time_us, offset;
  2085. u32 low, high;
  2086. int ret;
  2087. ret = cnss_pci_prevent_l1(dev);
  2088. if (ret)
  2089. goto out;
  2090. ret = cnss_pci_force_wake_get(pci_priv);
  2091. if (ret)
  2092. goto allow_l1;
  2093. spin_lock_irqsave(&time_sync_lock, flags);
  2094. cnss_pci_clear_time_sync_counter(pci_priv);
  2095. cnss_pci_enable_time_sync_counter(pci_priv);
  2096. host_time_us = cnss_get_host_timestamp(plat_priv);
  2097. ret = cnss_pci_get_device_timestamp(pci_priv, &device_time_us);
  2098. cnss_pci_clear_time_sync_counter(pci_priv);
  2099. spin_unlock_irqrestore(&time_sync_lock, flags);
  2100. if (ret)
  2101. goto force_wake_put;
  2102. if (host_time_us < device_time_us) {
  2103. cnss_pr_err("Host time (%llu us) is smaller than device time (%llu us), stop\n",
  2104. host_time_us, device_time_us);
  2105. ret = -EINVAL;
  2106. goto force_wake_put;
  2107. }
  2108. offset = host_time_us - device_time_us;
  2109. cnss_pr_dbg("Host time = %llu us, device time = %llu us, offset = %llu us\n",
  2110. host_time_us, device_time_us, offset);
  2111. low = offset & 0xFFFFFFFF;
  2112. high = offset >> 32;
  2113. cnss_pci_time_sync_reg_update(pci_priv, low, high);
  2114. force_wake_put:
  2115. cnss_pci_force_wake_put(pci_priv);
  2116. allow_l1:
  2117. cnss_pci_allow_l1(dev);
  2118. out:
  2119. return ret;
  2120. }
  2121. static void cnss_pci_time_sync_work_hdlr(struct work_struct *work)
  2122. {
  2123. struct cnss_pci_data *pci_priv =
  2124. container_of(work, struct cnss_pci_data, time_sync_work.work);
  2125. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2126. unsigned int time_sync_period_ms =
  2127. plat_priv->ctrl_params.time_sync_period;
  2128. if (test_bit(DISABLE_TIME_SYNC, &plat_priv->ctrl_params.quirks)) {
  2129. cnss_pr_dbg("Time sync is disabled\n");
  2130. return;
  2131. }
  2132. if (!time_sync_period_ms) {
  2133. cnss_pr_dbg("Skip time sync as time period is 0\n");
  2134. return;
  2135. }
  2136. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  2137. return;
  2138. if (cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS) < 0)
  2139. goto runtime_pm_put;
  2140. mutex_lock(&pci_priv->bus_lock);
  2141. cnss_pci_update_timestamp(pci_priv);
  2142. mutex_unlock(&pci_priv->bus_lock);
  2143. schedule_delayed_work(&pci_priv->time_sync_work,
  2144. msecs_to_jiffies(time_sync_period_ms));
  2145. runtime_pm_put:
  2146. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  2147. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  2148. }
  2149. static int cnss_pci_start_time_sync_update(struct cnss_pci_data *pci_priv)
  2150. {
  2151. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2152. switch (pci_priv->device_id) {
  2153. case QCA6390_DEVICE_ID:
  2154. case QCA6490_DEVICE_ID:
  2155. case KIWI_DEVICE_ID:
  2156. break;
  2157. default:
  2158. return -EOPNOTSUPP;
  2159. }
  2160. if (!plat_priv->device_freq_hz) {
  2161. cnss_pr_dbg("Device time clock frequency is not valid, skip time sync\n");
  2162. return -EINVAL;
  2163. }
  2164. cnss_pci_time_sync_work_hdlr(&pci_priv->time_sync_work.work);
  2165. return 0;
  2166. }
  2167. static void cnss_pci_stop_time_sync_update(struct cnss_pci_data *pci_priv)
  2168. {
  2169. switch (pci_priv->device_id) {
  2170. case QCA6390_DEVICE_ID:
  2171. case QCA6490_DEVICE_ID:
  2172. case KIWI_DEVICE_ID:
  2173. break;
  2174. default:
  2175. return;
  2176. }
  2177. cancel_delayed_work_sync(&pci_priv->time_sync_work);
  2178. }
  2179. int cnss_pci_call_driver_probe(struct cnss_pci_data *pci_priv)
  2180. {
  2181. int ret = 0;
  2182. struct cnss_plat_data *plat_priv;
  2183. if (!pci_priv)
  2184. return -ENODEV;
  2185. plat_priv = pci_priv->plat_priv;
  2186. if (test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2187. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2188. cnss_pr_dbg("Skip driver probe\n");
  2189. goto out;
  2190. }
  2191. if (!pci_priv->driver_ops) {
  2192. cnss_pr_err("driver_ops is NULL\n");
  2193. ret = -EINVAL;
  2194. goto out;
  2195. }
  2196. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2197. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2198. ret = pci_priv->driver_ops->reinit(pci_priv->pci_dev,
  2199. pci_priv->pci_device_id);
  2200. if (ret) {
  2201. cnss_pr_err("Failed to reinit host driver, err = %d\n",
  2202. ret);
  2203. goto out;
  2204. }
  2205. complete(&plat_priv->recovery_complete);
  2206. } else if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state)) {
  2207. ret = pci_priv->driver_ops->probe(pci_priv->pci_dev,
  2208. pci_priv->pci_device_id);
  2209. if (ret) {
  2210. cnss_pr_err("Failed to probe host driver, err = %d\n",
  2211. ret);
  2212. goto out;
  2213. }
  2214. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2215. set_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2216. complete_all(&plat_priv->power_up_complete);
  2217. } else if (test_bit(CNSS_DRIVER_IDLE_RESTART,
  2218. &plat_priv->driver_state)) {
  2219. ret = pci_priv->driver_ops->idle_restart(pci_priv->pci_dev,
  2220. pci_priv->pci_device_id);
  2221. if (ret) {
  2222. cnss_pr_err("Failed to idle restart host driver, err = %d\n",
  2223. ret);
  2224. plat_priv->power_up_error = ret;
  2225. complete_all(&plat_priv->power_up_complete);
  2226. goto out;
  2227. }
  2228. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  2229. complete_all(&plat_priv->power_up_complete);
  2230. } else {
  2231. complete(&plat_priv->power_up_complete);
  2232. }
  2233. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  2234. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2235. __pm_relax(plat_priv->recovery_ws);
  2236. }
  2237. cnss_pci_start_time_sync_update(pci_priv);
  2238. return 0;
  2239. out:
  2240. return ret;
  2241. }
  2242. int cnss_pci_call_driver_remove(struct cnss_pci_data *pci_priv)
  2243. {
  2244. struct cnss_plat_data *plat_priv;
  2245. int ret;
  2246. if (!pci_priv)
  2247. return -ENODEV;
  2248. plat_priv = pci_priv->plat_priv;
  2249. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  2250. test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state) ||
  2251. test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2252. cnss_pr_dbg("Skip driver remove\n");
  2253. return 0;
  2254. }
  2255. if (!pci_priv->driver_ops) {
  2256. cnss_pr_err("driver_ops is NULL\n");
  2257. return -EINVAL;
  2258. }
  2259. cnss_pci_stop_time_sync_update(pci_priv);
  2260. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2261. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2262. pci_priv->driver_ops->shutdown(pci_priv->pci_dev);
  2263. } else if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state)) {
  2264. pci_priv->driver_ops->remove(pci_priv->pci_dev);
  2265. clear_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2266. } else if (test_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2267. &plat_priv->driver_state)) {
  2268. ret = pci_priv->driver_ops->idle_shutdown(pci_priv->pci_dev);
  2269. if (ret == -EAGAIN) {
  2270. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2271. &plat_priv->driver_state);
  2272. return ret;
  2273. }
  2274. }
  2275. plat_priv->get_info_cb_ctx = NULL;
  2276. plat_priv->get_info_cb = NULL;
  2277. return 0;
  2278. }
  2279. int cnss_pci_call_driver_modem_status(struct cnss_pci_data *pci_priv,
  2280. int modem_current_status)
  2281. {
  2282. struct cnss_wlan_driver *driver_ops;
  2283. if (!pci_priv)
  2284. return -ENODEV;
  2285. driver_ops = pci_priv->driver_ops;
  2286. if (!driver_ops || !driver_ops->modem_status)
  2287. return -EINVAL;
  2288. driver_ops->modem_status(pci_priv->pci_dev, modem_current_status);
  2289. return 0;
  2290. }
  2291. int cnss_pci_update_status(struct cnss_pci_data *pci_priv,
  2292. enum cnss_driver_status status)
  2293. {
  2294. struct cnss_wlan_driver *driver_ops;
  2295. if (!pci_priv)
  2296. return -ENODEV;
  2297. driver_ops = pci_priv->driver_ops;
  2298. if (!driver_ops || !driver_ops->update_status)
  2299. return -EINVAL;
  2300. cnss_pr_dbg("Update driver status: %d\n", status);
  2301. driver_ops->update_status(pci_priv->pci_dev, status);
  2302. return 0;
  2303. }
  2304. static void cnss_pci_misc_reg_dump(struct cnss_pci_data *pci_priv,
  2305. struct cnss_misc_reg *misc_reg,
  2306. u32 misc_reg_size,
  2307. char *reg_name)
  2308. {
  2309. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2310. bool do_force_wake_put = true;
  2311. int i;
  2312. if (!misc_reg)
  2313. return;
  2314. if (in_interrupt() || irqs_disabled())
  2315. return;
  2316. if (cnss_pci_check_link_status(pci_priv))
  2317. return;
  2318. if (cnss_pci_force_wake_get(pci_priv)) {
  2319. /* Continue to dump when device has entered RDDM already */
  2320. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2321. return;
  2322. do_force_wake_put = false;
  2323. }
  2324. cnss_pr_dbg("Start to dump %s registers\n", reg_name);
  2325. for (i = 0; i < misc_reg_size; i++) {
  2326. if (!test_bit(pci_priv->misc_reg_dev_mask,
  2327. &misc_reg[i].dev_mask))
  2328. continue;
  2329. if (misc_reg[i].wr) {
  2330. if (misc_reg[i].offset ==
  2331. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG &&
  2332. i >= 1)
  2333. misc_reg[i].val =
  2334. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG_MSK |
  2335. misc_reg[i - 1].val;
  2336. if (cnss_pci_reg_write(pci_priv,
  2337. misc_reg[i].offset,
  2338. misc_reg[i].val))
  2339. goto force_wake_put;
  2340. cnss_pr_vdbg("Write 0x%X to 0x%X\n",
  2341. misc_reg[i].val,
  2342. misc_reg[i].offset);
  2343. } else {
  2344. if (cnss_pci_reg_read(pci_priv,
  2345. misc_reg[i].offset,
  2346. &misc_reg[i].val))
  2347. goto force_wake_put;
  2348. }
  2349. }
  2350. force_wake_put:
  2351. if (do_force_wake_put)
  2352. cnss_pci_force_wake_put(pci_priv);
  2353. }
  2354. static void cnss_pci_dump_misc_reg(struct cnss_pci_data *pci_priv)
  2355. {
  2356. if (in_interrupt() || irqs_disabled())
  2357. return;
  2358. if (cnss_pci_check_link_status(pci_priv))
  2359. return;
  2360. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wcss_reg,
  2361. WCSS_REG_SIZE, "wcss");
  2362. cnss_pci_misc_reg_dump(pci_priv, pci_priv->pcie_reg,
  2363. PCIE_REG_SIZE, "pcie");
  2364. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wlaon_reg,
  2365. WLAON_REG_SIZE, "wlaon");
  2366. cnss_pci_misc_reg_dump(pci_priv, pci_priv->syspm_reg,
  2367. SYSPM_REG_SIZE, "syspm");
  2368. }
  2369. static void cnss_pci_dump_shadow_reg(struct cnss_pci_data *pci_priv)
  2370. {
  2371. int i, j = 0, array_size = SHADOW_REG_COUNT + SHADOW_REG_INTER_COUNT;
  2372. u32 reg_offset;
  2373. bool do_force_wake_put = true;
  2374. if (in_interrupt() || irqs_disabled())
  2375. return;
  2376. if (cnss_pci_check_link_status(pci_priv))
  2377. return;
  2378. if (!pci_priv->debug_reg) {
  2379. pci_priv->debug_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  2380. sizeof(*pci_priv->debug_reg)
  2381. * array_size, GFP_KERNEL);
  2382. if (!pci_priv->debug_reg)
  2383. return;
  2384. }
  2385. if (cnss_pci_force_wake_get(pci_priv))
  2386. do_force_wake_put = false;
  2387. cnss_pr_dbg("Start to dump shadow registers\n");
  2388. for (i = 0; i < SHADOW_REG_COUNT; i++, j++) {
  2389. reg_offset = PCIE_SHADOW_REG_VALUE_0 + i * 4;
  2390. pci_priv->debug_reg[j].offset = reg_offset;
  2391. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2392. &pci_priv->debug_reg[j].val))
  2393. goto force_wake_put;
  2394. }
  2395. for (i = 0; i < SHADOW_REG_INTER_COUNT; i++, j++) {
  2396. reg_offset = PCIE_SHADOW_REG_INTER_0 + i * 4;
  2397. pci_priv->debug_reg[j].offset = reg_offset;
  2398. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2399. &pci_priv->debug_reg[j].val))
  2400. goto force_wake_put;
  2401. }
  2402. force_wake_put:
  2403. if (do_force_wake_put)
  2404. cnss_pci_force_wake_put(pci_priv);
  2405. }
  2406. static int cnss_qca6174_powerup(struct cnss_pci_data *pci_priv)
  2407. {
  2408. int ret = 0;
  2409. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2410. ret = cnss_power_on_device(plat_priv);
  2411. if (ret) {
  2412. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2413. goto out;
  2414. }
  2415. ret = cnss_resume_pci_link(pci_priv);
  2416. if (ret) {
  2417. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2418. goto power_off;
  2419. }
  2420. ret = cnss_pci_call_driver_probe(pci_priv);
  2421. if (ret)
  2422. goto suspend_link;
  2423. return 0;
  2424. suspend_link:
  2425. cnss_suspend_pci_link(pci_priv);
  2426. power_off:
  2427. cnss_power_off_device(plat_priv);
  2428. out:
  2429. return ret;
  2430. }
  2431. static int cnss_qca6174_shutdown(struct cnss_pci_data *pci_priv)
  2432. {
  2433. int ret = 0;
  2434. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2435. cnss_pci_pm_runtime_resume(pci_priv);
  2436. ret = cnss_pci_call_driver_remove(pci_priv);
  2437. if (ret == -EAGAIN)
  2438. goto out;
  2439. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2440. CNSS_BUS_WIDTH_NONE);
  2441. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2442. cnss_pci_set_auto_suspended(pci_priv, 0);
  2443. ret = cnss_suspend_pci_link(pci_priv);
  2444. if (ret)
  2445. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2446. cnss_power_off_device(plat_priv);
  2447. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2448. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2449. out:
  2450. return ret;
  2451. }
  2452. static void cnss_qca6174_crash_shutdown(struct cnss_pci_data *pci_priv)
  2453. {
  2454. if (pci_priv->driver_ops && pci_priv->driver_ops->crash_shutdown)
  2455. pci_priv->driver_ops->crash_shutdown(pci_priv->pci_dev);
  2456. }
  2457. static int cnss_qca6174_ramdump(struct cnss_pci_data *pci_priv)
  2458. {
  2459. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2460. struct cnss_ramdump_info *ramdump_info;
  2461. ramdump_info = &plat_priv->ramdump_info;
  2462. if (!ramdump_info->ramdump_size)
  2463. return -EINVAL;
  2464. return cnss_do_ramdump(plat_priv);
  2465. }
  2466. static int cnss_qca6290_powerup(struct cnss_pci_data *pci_priv)
  2467. {
  2468. int ret = 0;
  2469. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2470. unsigned int timeout;
  2471. int retry = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  2472. if (plat_priv->ramdump_info_v2.dump_data_valid) {
  2473. cnss_pci_clear_dump_info(pci_priv);
  2474. cnss_pci_power_off_mhi(pci_priv);
  2475. cnss_suspend_pci_link(pci_priv);
  2476. cnss_pci_deinit_mhi(pci_priv);
  2477. cnss_power_off_device(plat_priv);
  2478. }
  2479. /* Clear QMI send usage count during every power up */
  2480. pci_priv->qmi_send_usage_count = 0;
  2481. plat_priv->power_up_error = 0;
  2482. retry:
  2483. ret = cnss_power_on_device(plat_priv);
  2484. if (ret) {
  2485. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2486. goto out;
  2487. }
  2488. ret = cnss_resume_pci_link(pci_priv);
  2489. if (ret) {
  2490. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2491. if (test_bit(IGNORE_PCI_LINK_FAILURE,
  2492. &plat_priv->ctrl_params.quirks)) {
  2493. cnss_pr_dbg("Ignore PCI link resume failure\n");
  2494. ret = 0;
  2495. goto out;
  2496. }
  2497. if (ret == -EAGAIN && retry++ < POWER_ON_RETRY_MAX_TIMES) {
  2498. cnss_power_off_device(plat_priv);
  2499. /* Force toggle BT_EN GPIO low */
  2500. if (retry == POWER_ON_RETRY_MAX_TIMES) {
  2501. cnss_pr_dbg("Retry #%d. Set BT_EN GPIO(%u) low\n",
  2502. retry, bt_en_gpio);
  2503. if (bt_en_gpio >= 0)
  2504. gpio_direction_output(bt_en_gpio, 0);
  2505. cnss_pr_dbg("BT_EN GPIO val: %d\n",
  2506. gpio_get_value(bt_en_gpio));
  2507. }
  2508. cnss_pr_dbg("Retry to resume PCI link #%d\n", retry);
  2509. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  2510. goto retry;
  2511. }
  2512. /* Assert when it reaches maximum retries */
  2513. CNSS_ASSERT(0);
  2514. goto power_off;
  2515. }
  2516. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  2517. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  2518. ret = cnss_pci_start_mhi(pci_priv);
  2519. if (ret) {
  2520. cnss_fatal_err("Failed to start MHI, err = %d\n", ret);
  2521. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  2522. !pci_priv->pci_link_down_ind && timeout) {
  2523. /* Start recovery directly for MHI start failures */
  2524. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  2525. CNSS_REASON_DEFAULT);
  2526. }
  2527. return 0;
  2528. }
  2529. if (test_bit(USE_CORE_ONLY_FW, &plat_priv->ctrl_params.quirks)) {
  2530. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  2531. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2532. return 0;
  2533. }
  2534. cnss_set_pin_connect_status(plat_priv);
  2535. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  2536. ret = cnss_pci_call_driver_probe(pci_priv);
  2537. if (ret)
  2538. goto stop_mhi;
  2539. } else if (timeout) {
  2540. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state))
  2541. timeout += WLAN_COLD_BOOT_CAL_TIMEOUT;
  2542. else
  2543. timeout += WLAN_MISSION_MODE_TIMEOUT;
  2544. mod_timer(&plat_priv->fw_boot_timer,
  2545. jiffies + msecs_to_jiffies(timeout));
  2546. }
  2547. return 0;
  2548. stop_mhi:
  2549. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, true);
  2550. cnss_pci_power_off_mhi(pci_priv);
  2551. cnss_suspend_pci_link(pci_priv);
  2552. cnss_pci_deinit_mhi(pci_priv);
  2553. power_off:
  2554. cnss_power_off_device(plat_priv);
  2555. out:
  2556. return ret;
  2557. }
  2558. static int cnss_qca6290_shutdown(struct cnss_pci_data *pci_priv)
  2559. {
  2560. int ret = 0;
  2561. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2562. int do_force_wake = true;
  2563. cnss_pci_pm_runtime_resume(pci_priv);
  2564. ret = cnss_pci_call_driver_remove(pci_priv);
  2565. if (ret == -EAGAIN)
  2566. goto out;
  2567. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2568. CNSS_BUS_WIDTH_NONE);
  2569. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2570. cnss_pci_set_auto_suspended(pci_priv, 0);
  2571. if ((test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  2572. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2573. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  2574. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state) ||
  2575. test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) &&
  2576. test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  2577. del_timer(&pci_priv->dev_rddm_timer);
  2578. cnss_pci_collect_dump_info(pci_priv, false);
  2579. CNSS_ASSERT(0);
  2580. }
  2581. if (!cnss_is_device_powered_on(plat_priv)) {
  2582. cnss_pr_dbg("Device is already powered off, ignore\n");
  2583. goto skip_power_off;
  2584. }
  2585. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2586. do_force_wake = false;
  2587. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, do_force_wake);
  2588. /* FBC image will be freed after powering off MHI, so skip
  2589. * if RAM dump data is still valid.
  2590. */
  2591. if (plat_priv->ramdump_info_v2.dump_data_valid)
  2592. goto skip_power_off;
  2593. cnss_pci_power_off_mhi(pci_priv);
  2594. ret = cnss_suspend_pci_link(pci_priv);
  2595. if (ret)
  2596. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2597. cnss_pci_deinit_mhi(pci_priv);
  2598. cnss_power_off_device(plat_priv);
  2599. skip_power_off:
  2600. pci_priv->remap_window = 0;
  2601. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  2602. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  2603. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2604. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  2605. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  2606. pci_priv->pci_link_down_ind = false;
  2607. }
  2608. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2609. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2610. out:
  2611. return ret;
  2612. }
  2613. static void cnss_qca6290_crash_shutdown(struct cnss_pci_data *pci_priv)
  2614. {
  2615. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2616. set_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2617. cnss_pr_dbg("Crash shutdown with driver_state 0x%lx\n",
  2618. plat_priv->driver_state);
  2619. cnss_pci_collect_dump_info(pci_priv, true);
  2620. clear_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2621. }
  2622. static int cnss_qca6290_ramdump(struct cnss_pci_data *pci_priv)
  2623. {
  2624. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2625. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2626. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2627. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2628. int ret = 0;
  2629. if (!info_v2->dump_data_valid || !dump_seg ||
  2630. dump_data->nentries == 0)
  2631. return 0;
  2632. ret = cnss_do_elf_ramdump(plat_priv);
  2633. cnss_pci_clear_dump_info(pci_priv);
  2634. cnss_pci_power_off_mhi(pci_priv);
  2635. cnss_suspend_pci_link(pci_priv);
  2636. cnss_pci_deinit_mhi(pci_priv);
  2637. cnss_power_off_device(plat_priv);
  2638. return ret;
  2639. }
  2640. int cnss_pci_dev_powerup(struct cnss_pci_data *pci_priv)
  2641. {
  2642. int ret = 0;
  2643. if (!pci_priv) {
  2644. cnss_pr_err("pci_priv is NULL\n");
  2645. return -ENODEV;
  2646. }
  2647. switch (pci_priv->device_id) {
  2648. case QCA6174_DEVICE_ID:
  2649. ret = cnss_qca6174_powerup(pci_priv);
  2650. break;
  2651. case QCA6290_DEVICE_ID:
  2652. case QCA6390_DEVICE_ID:
  2653. case QCA6490_DEVICE_ID:
  2654. case KIWI_DEVICE_ID:
  2655. ret = cnss_qca6290_powerup(pci_priv);
  2656. break;
  2657. default:
  2658. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2659. pci_priv->device_id);
  2660. ret = -ENODEV;
  2661. }
  2662. return ret;
  2663. }
  2664. int cnss_pci_dev_shutdown(struct cnss_pci_data *pci_priv)
  2665. {
  2666. int ret = 0;
  2667. if (!pci_priv) {
  2668. cnss_pr_err("pci_priv is NULL\n");
  2669. return -ENODEV;
  2670. }
  2671. switch (pci_priv->device_id) {
  2672. case QCA6174_DEVICE_ID:
  2673. ret = cnss_qca6174_shutdown(pci_priv);
  2674. break;
  2675. case QCA6290_DEVICE_ID:
  2676. case QCA6390_DEVICE_ID:
  2677. case QCA6490_DEVICE_ID:
  2678. case KIWI_DEVICE_ID:
  2679. ret = cnss_qca6290_shutdown(pci_priv);
  2680. break;
  2681. default:
  2682. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2683. pci_priv->device_id);
  2684. ret = -ENODEV;
  2685. }
  2686. return ret;
  2687. }
  2688. int cnss_pci_dev_crash_shutdown(struct cnss_pci_data *pci_priv)
  2689. {
  2690. int ret = 0;
  2691. if (!pci_priv) {
  2692. cnss_pr_err("pci_priv is NULL\n");
  2693. return -ENODEV;
  2694. }
  2695. switch (pci_priv->device_id) {
  2696. case QCA6174_DEVICE_ID:
  2697. cnss_qca6174_crash_shutdown(pci_priv);
  2698. break;
  2699. case QCA6290_DEVICE_ID:
  2700. case QCA6390_DEVICE_ID:
  2701. case QCA6490_DEVICE_ID:
  2702. case KIWI_DEVICE_ID:
  2703. cnss_qca6290_crash_shutdown(pci_priv);
  2704. break;
  2705. default:
  2706. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2707. pci_priv->device_id);
  2708. ret = -ENODEV;
  2709. }
  2710. return ret;
  2711. }
  2712. int cnss_pci_dev_ramdump(struct cnss_pci_data *pci_priv)
  2713. {
  2714. int ret = 0;
  2715. if (!pci_priv) {
  2716. cnss_pr_err("pci_priv is NULL\n");
  2717. return -ENODEV;
  2718. }
  2719. switch (pci_priv->device_id) {
  2720. case QCA6174_DEVICE_ID:
  2721. ret = cnss_qca6174_ramdump(pci_priv);
  2722. break;
  2723. case QCA6290_DEVICE_ID:
  2724. case QCA6390_DEVICE_ID:
  2725. case QCA6490_DEVICE_ID:
  2726. case KIWI_DEVICE_ID:
  2727. ret = cnss_qca6290_ramdump(pci_priv);
  2728. break;
  2729. default:
  2730. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2731. pci_priv->device_id);
  2732. ret = -ENODEV;
  2733. }
  2734. return ret;
  2735. }
  2736. int cnss_pci_is_drv_connected(struct device *dev)
  2737. {
  2738. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2739. if (!pci_priv)
  2740. return -ENODEV;
  2741. return pci_priv->drv_connected_last;
  2742. }
  2743. EXPORT_SYMBOL(cnss_pci_is_drv_connected);
  2744. static void cnss_wlan_reg_driver_work(struct work_struct *work)
  2745. {
  2746. struct cnss_plat_data *plat_priv =
  2747. container_of(work, struct cnss_plat_data, wlan_reg_driver_work.work);
  2748. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  2749. struct cnss_cal_info *cal_info;
  2750. unsigned int timeout;
  2751. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  2752. goto reg_driver;
  2753. } else {
  2754. if (plat_priv->charger_mode) {
  2755. cnss_pr_err("Ignore calibration timeout in charger mode\n");
  2756. return;
  2757. }
  2758. if (!test_bit(CNSS_IN_COLD_BOOT_CAL,
  2759. &plat_priv->driver_state)) {
  2760. timeout = cnss_get_timeout(plat_priv,
  2761. CNSS_TIMEOUT_CALIBRATION);
  2762. cnss_pr_dbg("File system not ready to start calibration. Wait for %ds..\n",
  2763. timeout / 1000);
  2764. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2765. msecs_to_jiffies(timeout));
  2766. return;
  2767. }
  2768. del_timer(&plat_priv->fw_boot_timer);
  2769. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) &&
  2770. !test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2771. cnss_pr_err("Timeout waiting for calibration to complete\n");
  2772. CNSS_ASSERT(0);
  2773. }
  2774. cal_info = kzalloc(sizeof(*cal_info), GFP_KERNEL);
  2775. if (!cal_info)
  2776. return;
  2777. cal_info->cal_status = CNSS_CAL_TIMEOUT;
  2778. cnss_driver_event_post(plat_priv,
  2779. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE,
  2780. 0, cal_info);
  2781. }
  2782. reg_driver:
  2783. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2784. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2785. return;
  2786. }
  2787. reinit_completion(&plat_priv->power_up_complete);
  2788. cnss_driver_event_post(plat_priv,
  2789. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2790. CNSS_EVENT_SYNC_UNKILLABLE,
  2791. pci_priv->driver_ops);
  2792. }
  2793. int cnss_wlan_register_driver(struct cnss_wlan_driver *driver_ops)
  2794. {
  2795. int ret = 0;
  2796. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2797. struct cnss_pci_data *pci_priv;
  2798. const struct pci_device_id *id_table = driver_ops->id_table;
  2799. unsigned int timeout;
  2800. if (!plat_priv) {
  2801. cnss_pr_info("plat_priv is not ready for register driver\n");
  2802. return -EAGAIN;
  2803. }
  2804. if (!test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state)) {
  2805. cnss_pr_info("pci probe not yet done for register driver\n");
  2806. return -EAGAIN;
  2807. }
  2808. pci_priv = plat_priv->bus_priv;
  2809. if (pci_priv->driver_ops) {
  2810. cnss_pr_err("Driver has already registered\n");
  2811. return -EEXIST;
  2812. }
  2813. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2814. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2815. return -EINVAL;
  2816. }
  2817. if (!id_table || !pci_dev_present(id_table)) {
  2818. /* id_table pointer will move from pci_dev_present(),
  2819. * so check again using local pointer.
  2820. */
  2821. id_table = driver_ops->id_table;
  2822. while (id_table && id_table->vendor) {
  2823. cnss_pr_info("Host driver is built for PCIe device ID 0x%x\n",
  2824. id_table->device);
  2825. id_table++;
  2826. }
  2827. cnss_pr_err("Enumerated PCIe device id is 0x%x, reject unsupported driver\n",
  2828. pci_priv->device_id);
  2829. return -ENODEV;
  2830. }
  2831. if (driver_ops->chip_version != CNSS_CHIP_VER_ANY &&
  2832. driver_ops->chip_version != plat_priv->device_version.major_version) {
  2833. cnss_pr_err("Driver built for chip ver 0x%x, enumerated ver 0x%x, reject unsupported driver\n",
  2834. driver_ops->chip_version,
  2835. plat_priv->device_version.major_version);
  2836. return -ENODEV;
  2837. }
  2838. set_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state);
  2839. if (!plat_priv->cbc_enabled ||
  2840. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  2841. goto register_driver;
  2842. pci_priv->driver_ops = driver_ops;
  2843. /* If Cold Boot Calibration is enabled, it is the 1st step in init
  2844. * sequence.CBC is done on file system_ready trigger. Qcacld will be
  2845. * loaded from vendor_modprobe.sh at early boot and must be deferred
  2846. * until CBC is complete
  2847. */
  2848. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_CALIBRATION);
  2849. INIT_DELAYED_WORK(&plat_priv->wlan_reg_driver_work,
  2850. cnss_wlan_reg_driver_work);
  2851. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2852. msecs_to_jiffies(timeout));
  2853. cnss_pr_info("WLAN register driver deferred for Calibration\n");
  2854. return 0;
  2855. register_driver:
  2856. reinit_completion(&plat_priv->power_up_complete);
  2857. ret = cnss_driver_event_post(plat_priv,
  2858. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2859. CNSS_EVENT_SYNC_UNKILLABLE,
  2860. driver_ops);
  2861. return ret;
  2862. }
  2863. EXPORT_SYMBOL(cnss_wlan_register_driver);
  2864. void cnss_wlan_unregister_driver(struct cnss_wlan_driver *driver_ops)
  2865. {
  2866. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2867. int ret = 0;
  2868. unsigned int timeout;
  2869. if (!plat_priv) {
  2870. cnss_pr_err("plat_priv is NULL\n");
  2871. return;
  2872. }
  2873. mutex_lock(&plat_priv->driver_ops_lock);
  2874. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  2875. goto skip_wait_power_up;
  2876. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_WLAN_WATCHDOG);
  2877. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  2878. msecs_to_jiffies(timeout));
  2879. if (!ret) {
  2880. cnss_pr_err("Timeout (%ums) waiting for driver power up to complete\n",
  2881. timeout);
  2882. CNSS_ASSERT(0);
  2883. }
  2884. skip_wait_power_up:
  2885. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2886. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2887. goto skip_wait_recovery;
  2888. reinit_completion(&plat_priv->recovery_complete);
  2889. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  2890. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  2891. msecs_to_jiffies(timeout));
  2892. if (!ret) {
  2893. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  2894. timeout);
  2895. CNSS_ASSERT(0);
  2896. }
  2897. skip_wait_recovery:
  2898. cnss_driver_event_post(plat_priv,
  2899. CNSS_DRIVER_EVENT_UNREGISTER_DRIVER,
  2900. CNSS_EVENT_SYNC_UNKILLABLE, NULL);
  2901. mutex_unlock(&plat_priv->driver_ops_lock);
  2902. }
  2903. EXPORT_SYMBOL(cnss_wlan_unregister_driver);
  2904. int cnss_pci_register_driver_hdlr(struct cnss_pci_data *pci_priv,
  2905. void *data)
  2906. {
  2907. int ret = 0;
  2908. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2909. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2910. cnss_pr_dbg("Reboot or shutdown is in progress, ignore register driver\n");
  2911. return -EINVAL;
  2912. }
  2913. set_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2914. pci_priv->driver_ops = data;
  2915. ret = cnss_pci_dev_powerup(pci_priv);
  2916. if (ret) {
  2917. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2918. pci_priv->driver_ops = NULL;
  2919. }
  2920. return ret;
  2921. }
  2922. int cnss_pci_unregister_driver_hdlr(struct cnss_pci_data *pci_priv)
  2923. {
  2924. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2925. set_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2926. cnss_pci_dev_shutdown(pci_priv);
  2927. pci_priv->driver_ops = NULL;
  2928. return 0;
  2929. }
  2930. #if IS_ENABLED(CONFIG_PCI_MSM)
  2931. static bool cnss_pci_is_drv_supported(struct cnss_pci_data *pci_priv)
  2932. {
  2933. struct pci_dev *root_port = pcie_find_root_port(pci_priv->pci_dev);
  2934. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2935. struct device_node *root_of_node;
  2936. bool drv_supported = false;
  2937. if (!root_port) {
  2938. cnss_pr_err("PCIe DRV is not supported as root port is null\n");
  2939. pci_priv->drv_supported = false;
  2940. return drv_supported;
  2941. }
  2942. root_of_node = root_port->dev.of_node;
  2943. if (root_of_node->parent) {
  2944. drv_supported = of_property_read_bool(root_of_node->parent,
  2945. "qcom,drv-supported") ||
  2946. of_property_read_bool(root_of_node->parent,
  2947. "qcom,drv-name");
  2948. }
  2949. cnss_pr_dbg("PCIe DRV is %s\n",
  2950. drv_supported ? "supported" : "not supported");
  2951. pci_priv->drv_supported = drv_supported;
  2952. if (drv_supported) {
  2953. plat_priv->cap.cap_flag |= CNSS_HAS_DRV_SUPPORT;
  2954. cnss_set_feature_list(plat_priv, CNSS_DRV_SUPPORT_V01);
  2955. }
  2956. return drv_supported;
  2957. }
  2958. static void cnss_pci_event_cb(struct msm_pcie_notify *notify)
  2959. {
  2960. struct pci_dev *pci_dev;
  2961. struct cnss_pci_data *pci_priv;
  2962. struct device *dev;
  2963. struct cnss_plat_data *plat_priv = NULL;
  2964. int ret = 0;
  2965. if (!notify)
  2966. return;
  2967. pci_dev = notify->user;
  2968. if (!pci_dev)
  2969. return;
  2970. pci_priv = cnss_get_pci_priv(pci_dev);
  2971. if (!pci_priv)
  2972. return;
  2973. dev = &pci_priv->pci_dev->dev;
  2974. switch (notify->event) {
  2975. case MSM_PCIE_EVENT_LINK_RECOVER:
  2976. cnss_pr_dbg("PCI link recover callback\n");
  2977. plat_priv = pci_priv->plat_priv;
  2978. if (!plat_priv) {
  2979. cnss_pr_err("plat_priv is NULL\n");
  2980. return;
  2981. }
  2982. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  2983. ret = msm_pcie_pm_control(MSM_PCIE_HANDLE_LINKDOWN,
  2984. pci_dev->bus->number, pci_dev, NULL,
  2985. PM_OPTIONS_DEFAULT);
  2986. if (ret)
  2987. cnss_pci_handle_linkdown(pci_priv);
  2988. break;
  2989. case MSM_PCIE_EVENT_LINKDOWN:
  2990. cnss_pr_dbg("PCI link down event callback\n");
  2991. cnss_pci_handle_linkdown(pci_priv);
  2992. break;
  2993. case MSM_PCIE_EVENT_WAKEUP:
  2994. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  2995. cnss_pci_get_auto_suspended(pci_priv)) ||
  2996. dev->power.runtime_status == RPM_SUSPENDING) {
  2997. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2998. cnss_pci_pm_request_resume(pci_priv);
  2999. }
  3000. break;
  3001. case MSM_PCIE_EVENT_DRV_CONNECT:
  3002. cnss_pr_dbg("DRV subsystem is connected\n");
  3003. cnss_pci_set_drv_connected(pci_priv, 1);
  3004. break;
  3005. case MSM_PCIE_EVENT_DRV_DISCONNECT:
  3006. cnss_pr_dbg("DRV subsystem is disconnected\n");
  3007. if (cnss_pci_get_auto_suspended(pci_priv))
  3008. cnss_pci_pm_request_resume(pci_priv);
  3009. cnss_pci_set_drv_connected(pci_priv, 0);
  3010. break;
  3011. default:
  3012. cnss_pr_err("Received invalid PCI event: %d\n", notify->event);
  3013. }
  3014. }
  3015. /**
  3016. * cnss_reg_pci_event() - Register for PCIe events
  3017. * @pci_priv: driver PCI bus context pointer
  3018. *
  3019. * This function shall call corresponding PCIe root complex driver APIs
  3020. * to register for PCIe events like link down or WAKE GPIO toggling etc.
  3021. * The events should be based on PCIe root complex driver's capability.
  3022. *
  3023. * Return: 0 for success, negative value for error
  3024. */
  3025. static int cnss_reg_pci_event(struct cnss_pci_data *pci_priv)
  3026. {
  3027. int ret = 0;
  3028. struct msm_pcie_register_event *pci_event;
  3029. pci_event = &pci_priv->msm_pci_event;
  3030. pci_event->events = MSM_PCIE_EVENT_LINK_RECOVER |
  3031. MSM_PCIE_EVENT_LINKDOWN |
  3032. MSM_PCIE_EVENT_WAKEUP;
  3033. if (cnss_pci_is_drv_supported(pci_priv))
  3034. pci_event->events = pci_event->events |
  3035. MSM_PCIE_EVENT_DRV_CONNECT |
  3036. MSM_PCIE_EVENT_DRV_DISCONNECT;
  3037. pci_event->user = pci_priv->pci_dev;
  3038. pci_event->mode = MSM_PCIE_TRIGGER_CALLBACK;
  3039. pci_event->callback = cnss_pci_event_cb;
  3040. pci_event->options = MSM_PCIE_CONFIG_NO_RECOVERY;
  3041. ret = msm_pcie_register_event(pci_event);
  3042. if (ret)
  3043. cnss_pr_err("Failed to register MSM PCI event, err = %d\n",
  3044. ret);
  3045. return ret;
  3046. }
  3047. static void cnss_dereg_pci_event(struct cnss_pci_data *pci_priv)
  3048. {
  3049. msm_pcie_deregister_event(&pci_priv->msm_pci_event);
  3050. }
  3051. #else
  3052. static int cnss_reg_pci_event(struct cnss_pci_data *pci_priv)
  3053. {
  3054. return 0;
  3055. }
  3056. static void cnss_dereg_pci_event(struct cnss_pci_data *pci_priv) {}
  3057. #endif
  3058. static int cnss_pci_suspend_driver(struct cnss_pci_data *pci_priv)
  3059. {
  3060. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3061. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3062. int ret = 0;
  3063. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  3064. if (driver_ops && driver_ops->suspend) {
  3065. ret = driver_ops->suspend(pci_dev, state);
  3066. if (ret) {
  3067. cnss_pr_err("Failed to suspend host driver, err = %d\n",
  3068. ret);
  3069. ret = -EAGAIN;
  3070. }
  3071. }
  3072. return ret;
  3073. }
  3074. static int cnss_pci_resume_driver(struct cnss_pci_data *pci_priv)
  3075. {
  3076. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3077. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3078. int ret = 0;
  3079. if (driver_ops && driver_ops->resume) {
  3080. ret = driver_ops->resume(pci_dev);
  3081. if (ret)
  3082. cnss_pr_err("Failed to resume host driver, err = %d\n",
  3083. ret);
  3084. }
  3085. return ret;
  3086. }
  3087. int cnss_pci_suspend_bus(struct cnss_pci_data *pci_priv)
  3088. {
  3089. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3090. int ret = 0;
  3091. if (pci_priv->pci_link_state == PCI_LINK_DOWN)
  3092. goto out;
  3093. if (cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_SUSPEND)) {
  3094. ret = -EAGAIN;
  3095. goto out;
  3096. }
  3097. if (pci_priv->drv_connected_last)
  3098. goto skip_disable_pci;
  3099. pci_clear_master(pci_dev);
  3100. cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  3101. pci_disable_device(pci_dev);
  3102. ret = pci_set_power_state(pci_dev, PCI_D3hot);
  3103. if (ret)
  3104. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  3105. skip_disable_pci:
  3106. if (cnss_set_pci_link(pci_priv, PCI_LINK_DOWN)) {
  3107. ret = -EAGAIN;
  3108. goto resume_mhi;
  3109. }
  3110. pci_priv->pci_link_state = PCI_LINK_DOWN;
  3111. return 0;
  3112. resume_mhi:
  3113. if (!pci_is_enabled(pci_dev))
  3114. if (pci_enable_device(pci_dev))
  3115. cnss_pr_err("Failed to enable PCI device\n");
  3116. if (pci_priv->saved_state)
  3117. cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  3118. pci_set_master(pci_dev);
  3119. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3120. out:
  3121. return ret;
  3122. }
  3123. int cnss_pci_resume_bus(struct cnss_pci_data *pci_priv)
  3124. {
  3125. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3126. int ret = 0;
  3127. if (pci_priv->pci_link_state == PCI_LINK_UP)
  3128. goto out;
  3129. if (cnss_set_pci_link(pci_priv, PCI_LINK_UP)) {
  3130. cnss_fatal_err("Failed to resume PCI link from suspend\n");
  3131. cnss_pci_link_down(&pci_dev->dev);
  3132. ret = -EAGAIN;
  3133. goto out;
  3134. }
  3135. pci_priv->pci_link_state = PCI_LINK_UP;
  3136. if (pci_priv->drv_connected_last)
  3137. goto skip_enable_pci;
  3138. ret = pci_enable_device(pci_dev);
  3139. if (ret) {
  3140. cnss_pr_err("Failed to enable PCI device, err = %d\n",
  3141. ret);
  3142. goto out;
  3143. }
  3144. if (pci_priv->saved_state)
  3145. cnss_set_pci_config_space(pci_priv,
  3146. RESTORE_PCI_CONFIG_SPACE);
  3147. pci_set_master(pci_dev);
  3148. skip_enable_pci:
  3149. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3150. out:
  3151. return ret;
  3152. }
  3153. static int cnss_pci_suspend(struct device *dev)
  3154. {
  3155. int ret = 0;
  3156. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3157. struct cnss_plat_data *plat_priv;
  3158. if (!pci_priv)
  3159. goto out;
  3160. plat_priv = pci_priv->plat_priv;
  3161. if (!plat_priv)
  3162. goto out;
  3163. if (!cnss_is_device_powered_on(plat_priv))
  3164. goto out;
  3165. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3166. pci_priv->drv_supported) {
  3167. pci_priv->drv_connected_last =
  3168. cnss_pci_get_drv_connected(pci_priv);
  3169. if (!pci_priv->drv_connected_last) {
  3170. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3171. ret = -EAGAIN;
  3172. goto out;
  3173. }
  3174. }
  3175. set_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3176. ret = cnss_pci_suspend_driver(pci_priv);
  3177. if (ret)
  3178. goto clear_flag;
  3179. if (!pci_priv->disable_pc) {
  3180. mutex_lock(&pci_priv->bus_lock);
  3181. ret = cnss_pci_suspend_bus(pci_priv);
  3182. mutex_unlock(&pci_priv->bus_lock);
  3183. if (ret)
  3184. goto resume_driver;
  3185. }
  3186. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  3187. return 0;
  3188. resume_driver:
  3189. cnss_pci_resume_driver(pci_priv);
  3190. clear_flag:
  3191. pci_priv->drv_connected_last = 0;
  3192. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3193. out:
  3194. return ret;
  3195. }
  3196. static int cnss_pci_resume(struct device *dev)
  3197. {
  3198. int ret = 0;
  3199. struct pci_dev *pci_dev = to_pci_dev(dev);
  3200. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3201. struct cnss_plat_data *plat_priv;
  3202. if (!pci_priv)
  3203. goto out;
  3204. plat_priv = pci_priv->plat_priv;
  3205. if (!plat_priv)
  3206. goto out;
  3207. if (pci_priv->pci_link_down_ind)
  3208. goto out;
  3209. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3210. goto out;
  3211. if (!pci_priv->disable_pc) {
  3212. ret = cnss_pci_resume_bus(pci_priv);
  3213. if (ret)
  3214. goto out;
  3215. }
  3216. ret = cnss_pci_resume_driver(pci_priv);
  3217. pci_priv->drv_connected_last = 0;
  3218. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3219. out:
  3220. return ret;
  3221. }
  3222. static int cnss_pci_suspend_noirq(struct device *dev)
  3223. {
  3224. int ret = 0;
  3225. struct pci_dev *pci_dev = to_pci_dev(dev);
  3226. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3227. struct cnss_wlan_driver *driver_ops;
  3228. if (!pci_priv)
  3229. goto out;
  3230. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3231. goto out;
  3232. driver_ops = pci_priv->driver_ops;
  3233. if (driver_ops && driver_ops->suspend_noirq)
  3234. ret = driver_ops->suspend_noirq(pci_dev);
  3235. if (pci_priv->disable_pc && !pci_dev->state_saved &&
  3236. !pci_priv->plat_priv->use_pm_domain)
  3237. pci_save_state(pci_dev);
  3238. out:
  3239. return ret;
  3240. }
  3241. static int cnss_pci_resume_noirq(struct device *dev)
  3242. {
  3243. int ret = 0;
  3244. struct pci_dev *pci_dev = to_pci_dev(dev);
  3245. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3246. struct cnss_wlan_driver *driver_ops;
  3247. if (!pci_priv)
  3248. goto out;
  3249. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3250. goto out;
  3251. driver_ops = pci_priv->driver_ops;
  3252. if (driver_ops && driver_ops->resume_noirq &&
  3253. !pci_priv->pci_link_down_ind)
  3254. ret = driver_ops->resume_noirq(pci_dev);
  3255. out:
  3256. return ret;
  3257. }
  3258. static int cnss_pci_runtime_suspend(struct device *dev)
  3259. {
  3260. int ret = 0;
  3261. struct pci_dev *pci_dev = to_pci_dev(dev);
  3262. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3263. struct cnss_plat_data *plat_priv;
  3264. struct cnss_wlan_driver *driver_ops;
  3265. if (!pci_priv)
  3266. return -EAGAIN;
  3267. plat_priv = pci_priv->plat_priv;
  3268. if (!plat_priv)
  3269. return -EAGAIN;
  3270. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3271. return -EAGAIN;
  3272. if (pci_priv->pci_link_down_ind) {
  3273. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3274. return -EAGAIN;
  3275. }
  3276. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3277. pci_priv->drv_supported) {
  3278. pci_priv->drv_connected_last =
  3279. cnss_pci_get_drv_connected(pci_priv);
  3280. if (!pci_priv->drv_connected_last) {
  3281. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3282. return -EAGAIN;
  3283. }
  3284. }
  3285. cnss_pr_vdbg("Runtime suspend start\n");
  3286. driver_ops = pci_priv->driver_ops;
  3287. if (driver_ops && driver_ops->runtime_ops &&
  3288. driver_ops->runtime_ops->runtime_suspend)
  3289. ret = driver_ops->runtime_ops->runtime_suspend(pci_dev);
  3290. else
  3291. ret = cnss_auto_suspend(dev);
  3292. if (ret)
  3293. pci_priv->drv_connected_last = 0;
  3294. cnss_pr_vdbg("Runtime suspend status: %d\n", ret);
  3295. return ret;
  3296. }
  3297. static int cnss_pci_runtime_resume(struct device *dev)
  3298. {
  3299. int ret = 0;
  3300. struct pci_dev *pci_dev = to_pci_dev(dev);
  3301. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3302. struct cnss_wlan_driver *driver_ops;
  3303. if (!pci_priv)
  3304. return -EAGAIN;
  3305. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3306. return -EAGAIN;
  3307. if (pci_priv->pci_link_down_ind) {
  3308. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3309. return -EAGAIN;
  3310. }
  3311. cnss_pr_vdbg("Runtime resume start\n");
  3312. driver_ops = pci_priv->driver_ops;
  3313. if (driver_ops && driver_ops->runtime_ops &&
  3314. driver_ops->runtime_ops->runtime_resume)
  3315. ret = driver_ops->runtime_ops->runtime_resume(pci_dev);
  3316. else
  3317. ret = cnss_auto_resume(dev);
  3318. if (!ret)
  3319. pci_priv->drv_connected_last = 0;
  3320. cnss_pr_vdbg("Runtime resume status: %d\n", ret);
  3321. return ret;
  3322. }
  3323. static int cnss_pci_runtime_idle(struct device *dev)
  3324. {
  3325. cnss_pr_vdbg("Runtime idle\n");
  3326. pm_request_autosuspend(dev);
  3327. return -EBUSY;
  3328. }
  3329. int cnss_wlan_pm_control(struct device *dev, bool vote)
  3330. {
  3331. struct pci_dev *pci_dev = to_pci_dev(dev);
  3332. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3333. int ret = 0;
  3334. if (!pci_priv)
  3335. return -ENODEV;
  3336. ret = cnss_pci_disable_pc(pci_priv, vote);
  3337. if (ret)
  3338. return ret;
  3339. pci_priv->disable_pc = vote;
  3340. cnss_pr_dbg("%s PCIe power collapse\n", vote ? "disable" : "enable");
  3341. return 0;
  3342. }
  3343. EXPORT_SYMBOL(cnss_wlan_pm_control);
  3344. static void cnss_pci_pm_runtime_get_record(struct cnss_pci_data *pci_priv,
  3345. enum cnss_rtpm_id id)
  3346. {
  3347. if (id >= RTPM_ID_MAX)
  3348. return;
  3349. atomic_inc(&pci_priv->pm_stats.runtime_get);
  3350. atomic_inc(&pci_priv->pm_stats.runtime_get_id[id]);
  3351. pci_priv->pm_stats.runtime_get_timestamp_id[id] =
  3352. cnss_get_host_timestamp(pci_priv->plat_priv);
  3353. }
  3354. static void cnss_pci_pm_runtime_put_record(struct cnss_pci_data *pci_priv,
  3355. enum cnss_rtpm_id id)
  3356. {
  3357. if (id >= RTPM_ID_MAX)
  3358. return;
  3359. atomic_inc(&pci_priv->pm_stats.runtime_put);
  3360. atomic_inc(&pci_priv->pm_stats.runtime_put_id[id]);
  3361. pci_priv->pm_stats.runtime_put_timestamp_id[id] =
  3362. cnss_get_host_timestamp(pci_priv->plat_priv);
  3363. }
  3364. void cnss_pci_pm_runtime_show_usage_count(struct cnss_pci_data *pci_priv)
  3365. {
  3366. struct device *dev;
  3367. if (!pci_priv)
  3368. return;
  3369. dev = &pci_priv->pci_dev->dev;
  3370. cnss_pr_dbg("Runtime PM usage count: %d\n",
  3371. atomic_read(&dev->power.usage_count));
  3372. }
  3373. int cnss_pci_pm_request_resume(struct cnss_pci_data *pci_priv)
  3374. {
  3375. struct device *dev;
  3376. enum rpm_status status;
  3377. if (!pci_priv)
  3378. return -ENODEV;
  3379. dev = &pci_priv->pci_dev->dev;
  3380. status = dev->power.runtime_status;
  3381. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3382. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3383. (void *)_RET_IP_);
  3384. return pm_request_resume(dev);
  3385. }
  3386. int cnss_pci_pm_runtime_resume(struct cnss_pci_data *pci_priv)
  3387. {
  3388. struct device *dev;
  3389. enum rpm_status status;
  3390. if (!pci_priv)
  3391. return -ENODEV;
  3392. dev = &pci_priv->pci_dev->dev;
  3393. status = dev->power.runtime_status;
  3394. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3395. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3396. (void *)_RET_IP_);
  3397. return pm_runtime_resume(dev);
  3398. }
  3399. int cnss_pci_pm_runtime_get(struct cnss_pci_data *pci_priv,
  3400. enum cnss_rtpm_id id)
  3401. {
  3402. struct device *dev;
  3403. enum rpm_status status;
  3404. if (!pci_priv)
  3405. return -ENODEV;
  3406. dev = &pci_priv->pci_dev->dev;
  3407. status = dev->power.runtime_status;
  3408. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3409. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3410. (void *)_RET_IP_);
  3411. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3412. return pm_runtime_get(dev);
  3413. }
  3414. int cnss_pci_pm_runtime_get_sync(struct cnss_pci_data *pci_priv,
  3415. enum cnss_rtpm_id id)
  3416. {
  3417. struct device *dev;
  3418. enum rpm_status status;
  3419. if (!pci_priv)
  3420. return -ENODEV;
  3421. dev = &pci_priv->pci_dev->dev;
  3422. status = dev->power.runtime_status;
  3423. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3424. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3425. (void *)_RET_IP_);
  3426. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3427. return pm_runtime_get_sync(dev);
  3428. }
  3429. void cnss_pci_pm_runtime_get_noresume(struct cnss_pci_data *pci_priv,
  3430. enum cnss_rtpm_id id)
  3431. {
  3432. if (!pci_priv)
  3433. return;
  3434. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3435. pm_runtime_get_noresume(&pci_priv->pci_dev->dev);
  3436. }
  3437. int cnss_pci_pm_runtime_put_autosuspend(struct cnss_pci_data *pci_priv,
  3438. enum cnss_rtpm_id id)
  3439. {
  3440. struct device *dev;
  3441. if (!pci_priv)
  3442. return -ENODEV;
  3443. dev = &pci_priv->pci_dev->dev;
  3444. if (atomic_read(&dev->power.usage_count) == 0) {
  3445. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3446. return -EINVAL;
  3447. }
  3448. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3449. return pm_runtime_put_autosuspend(&pci_priv->pci_dev->dev);
  3450. }
  3451. void cnss_pci_pm_runtime_put_noidle(struct cnss_pci_data *pci_priv,
  3452. enum cnss_rtpm_id id)
  3453. {
  3454. struct device *dev;
  3455. if (!pci_priv)
  3456. return;
  3457. dev = &pci_priv->pci_dev->dev;
  3458. if (atomic_read(&dev->power.usage_count) == 0) {
  3459. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3460. return;
  3461. }
  3462. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3463. pm_runtime_put_noidle(&pci_priv->pci_dev->dev);
  3464. }
  3465. void cnss_pci_pm_runtime_mark_last_busy(struct cnss_pci_data *pci_priv)
  3466. {
  3467. if (!pci_priv)
  3468. return;
  3469. pm_runtime_mark_last_busy(&pci_priv->pci_dev->dev);
  3470. }
  3471. int cnss_auto_suspend(struct device *dev)
  3472. {
  3473. int ret = 0;
  3474. struct pci_dev *pci_dev = to_pci_dev(dev);
  3475. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3476. struct cnss_plat_data *plat_priv;
  3477. if (!pci_priv)
  3478. return -ENODEV;
  3479. plat_priv = pci_priv->plat_priv;
  3480. if (!plat_priv)
  3481. return -ENODEV;
  3482. mutex_lock(&pci_priv->bus_lock);
  3483. if (!pci_priv->qmi_send_usage_count) {
  3484. ret = cnss_pci_suspend_bus(pci_priv);
  3485. if (ret) {
  3486. mutex_unlock(&pci_priv->bus_lock);
  3487. return ret;
  3488. }
  3489. }
  3490. cnss_pci_set_auto_suspended(pci_priv, 1);
  3491. mutex_unlock(&pci_priv->bus_lock);
  3492. cnss_pci_set_monitor_wake_intr(pci_priv, true);
  3493. /* For suspend temporarily set bandwidth vote to NONE and dont save in
  3494. * current_bw_vote as in resume path we should vote for last used
  3495. * bandwidth vote. Also ignore error if bw voting is not setup.
  3496. */
  3497. cnss_setup_bus_bandwidth(plat_priv, CNSS_BUS_WIDTH_NONE, false);
  3498. return 0;
  3499. }
  3500. EXPORT_SYMBOL(cnss_auto_suspend);
  3501. int cnss_auto_resume(struct device *dev)
  3502. {
  3503. int ret = 0;
  3504. struct pci_dev *pci_dev = to_pci_dev(dev);
  3505. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3506. struct cnss_plat_data *plat_priv;
  3507. if (!pci_priv)
  3508. return -ENODEV;
  3509. plat_priv = pci_priv->plat_priv;
  3510. if (!plat_priv)
  3511. return -ENODEV;
  3512. mutex_lock(&pci_priv->bus_lock);
  3513. ret = cnss_pci_resume_bus(pci_priv);
  3514. if (ret) {
  3515. mutex_unlock(&pci_priv->bus_lock);
  3516. return ret;
  3517. }
  3518. cnss_pci_set_auto_suspended(pci_priv, 0);
  3519. mutex_unlock(&pci_priv->bus_lock);
  3520. cnss_request_bus_bandwidth(dev, plat_priv->icc.current_bw_vote);
  3521. return 0;
  3522. }
  3523. EXPORT_SYMBOL(cnss_auto_resume);
  3524. int cnss_pci_force_wake_request_sync(struct device *dev, int timeout_us)
  3525. {
  3526. struct pci_dev *pci_dev = to_pci_dev(dev);
  3527. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3528. struct cnss_plat_data *plat_priv;
  3529. struct mhi_controller *mhi_ctrl;
  3530. if (!pci_priv)
  3531. return -ENODEV;
  3532. switch (pci_priv->device_id) {
  3533. case QCA6390_DEVICE_ID:
  3534. case QCA6490_DEVICE_ID:
  3535. case KIWI_DEVICE_ID:
  3536. break;
  3537. default:
  3538. return 0;
  3539. }
  3540. mhi_ctrl = pci_priv->mhi_ctrl;
  3541. if (!mhi_ctrl)
  3542. return -EINVAL;
  3543. plat_priv = pci_priv->plat_priv;
  3544. if (!plat_priv)
  3545. return -ENODEV;
  3546. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3547. return -EAGAIN;
  3548. if (timeout_us) {
  3549. /* Busy wait for timeout_us */
  3550. return cnss_mhi_device_get_sync_atomic(pci_priv,
  3551. timeout_us, false);
  3552. } else {
  3553. /* Sleep wait for mhi_ctrl->timeout_ms */
  3554. return mhi_device_get_sync(mhi_ctrl->mhi_dev);
  3555. }
  3556. }
  3557. EXPORT_SYMBOL(cnss_pci_force_wake_request_sync);
  3558. int cnss_pci_force_wake_request(struct device *dev)
  3559. {
  3560. struct pci_dev *pci_dev = to_pci_dev(dev);
  3561. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3562. struct cnss_plat_data *plat_priv;
  3563. struct mhi_controller *mhi_ctrl;
  3564. if (!pci_priv)
  3565. return -ENODEV;
  3566. switch (pci_priv->device_id) {
  3567. case QCA6390_DEVICE_ID:
  3568. case QCA6490_DEVICE_ID:
  3569. case KIWI_DEVICE_ID:
  3570. break;
  3571. default:
  3572. return 0;
  3573. }
  3574. mhi_ctrl = pci_priv->mhi_ctrl;
  3575. if (!mhi_ctrl)
  3576. return -EINVAL;
  3577. plat_priv = pci_priv->plat_priv;
  3578. if (!plat_priv)
  3579. return -ENODEV;
  3580. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3581. return -EAGAIN;
  3582. mhi_device_get(mhi_ctrl->mhi_dev);
  3583. return 0;
  3584. }
  3585. EXPORT_SYMBOL(cnss_pci_force_wake_request);
  3586. int cnss_pci_is_device_awake(struct device *dev)
  3587. {
  3588. struct pci_dev *pci_dev = to_pci_dev(dev);
  3589. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3590. struct mhi_controller *mhi_ctrl;
  3591. if (!pci_priv)
  3592. return -ENODEV;
  3593. switch (pci_priv->device_id) {
  3594. case QCA6390_DEVICE_ID:
  3595. case QCA6490_DEVICE_ID:
  3596. case KIWI_DEVICE_ID:
  3597. break;
  3598. default:
  3599. return 0;
  3600. }
  3601. mhi_ctrl = pci_priv->mhi_ctrl;
  3602. if (!mhi_ctrl)
  3603. return -EINVAL;
  3604. return (mhi_ctrl->dev_state == MHI_STATE_M0);
  3605. }
  3606. EXPORT_SYMBOL(cnss_pci_is_device_awake);
  3607. int cnss_pci_force_wake_release(struct device *dev)
  3608. {
  3609. struct pci_dev *pci_dev = to_pci_dev(dev);
  3610. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3611. struct cnss_plat_data *plat_priv;
  3612. struct mhi_controller *mhi_ctrl;
  3613. if (!pci_priv)
  3614. return -ENODEV;
  3615. switch (pci_priv->device_id) {
  3616. case QCA6390_DEVICE_ID:
  3617. case QCA6490_DEVICE_ID:
  3618. case KIWI_DEVICE_ID:
  3619. break;
  3620. default:
  3621. return 0;
  3622. }
  3623. mhi_ctrl = pci_priv->mhi_ctrl;
  3624. if (!mhi_ctrl)
  3625. return -EINVAL;
  3626. plat_priv = pci_priv->plat_priv;
  3627. if (!plat_priv)
  3628. return -ENODEV;
  3629. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3630. return -EAGAIN;
  3631. mhi_device_put(mhi_ctrl->mhi_dev);
  3632. return 0;
  3633. }
  3634. EXPORT_SYMBOL(cnss_pci_force_wake_release);
  3635. int cnss_pci_qmi_send_get(struct cnss_pci_data *pci_priv)
  3636. {
  3637. int ret = 0;
  3638. if (!pci_priv)
  3639. return -ENODEV;
  3640. mutex_lock(&pci_priv->bus_lock);
  3641. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3642. !pci_priv->qmi_send_usage_count)
  3643. ret = cnss_pci_resume_bus(pci_priv);
  3644. pci_priv->qmi_send_usage_count++;
  3645. cnss_pr_buf("Increased QMI send usage count to %d\n",
  3646. pci_priv->qmi_send_usage_count);
  3647. mutex_unlock(&pci_priv->bus_lock);
  3648. return ret;
  3649. }
  3650. int cnss_pci_qmi_send_put(struct cnss_pci_data *pci_priv)
  3651. {
  3652. int ret = 0;
  3653. if (!pci_priv)
  3654. return -ENODEV;
  3655. mutex_lock(&pci_priv->bus_lock);
  3656. if (pci_priv->qmi_send_usage_count)
  3657. pci_priv->qmi_send_usage_count--;
  3658. cnss_pr_buf("Decreased QMI send usage count to %d\n",
  3659. pci_priv->qmi_send_usage_count);
  3660. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3661. !pci_priv->qmi_send_usage_count &&
  3662. !cnss_pcie_is_device_down(pci_priv))
  3663. ret = cnss_pci_suspend_bus(pci_priv);
  3664. mutex_unlock(&pci_priv->bus_lock);
  3665. return ret;
  3666. }
  3667. int cnss_pci_alloc_fw_mem(struct cnss_pci_data *pci_priv)
  3668. {
  3669. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3670. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3671. struct device *dev = &pci_priv->pci_dev->dev;
  3672. int i;
  3673. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3674. if (!fw_mem[i].va && fw_mem[i].size) {
  3675. fw_mem[i].va =
  3676. dma_alloc_attrs(dev, fw_mem[i].size,
  3677. &fw_mem[i].pa, GFP_KERNEL,
  3678. fw_mem[i].attrs);
  3679. if (!fw_mem[i].va) {
  3680. cnss_pr_err("Failed to allocate memory for FW, size: 0x%zx, type: %u\n",
  3681. fw_mem[i].size, fw_mem[i].type);
  3682. return -ENOMEM;
  3683. }
  3684. }
  3685. }
  3686. return 0;
  3687. }
  3688. static void cnss_pci_free_fw_mem(struct cnss_pci_data *pci_priv)
  3689. {
  3690. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3691. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3692. struct device *dev = &pci_priv->pci_dev->dev;
  3693. int i;
  3694. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3695. if (fw_mem[i].va && fw_mem[i].size) {
  3696. cnss_pr_dbg("Freeing memory for FW, va: 0x%pK, pa: %pa, size: 0x%zx, type: %u\n",
  3697. fw_mem[i].va, &fw_mem[i].pa,
  3698. fw_mem[i].size, fw_mem[i].type);
  3699. dma_free_attrs(dev, fw_mem[i].size,
  3700. fw_mem[i].va, fw_mem[i].pa,
  3701. fw_mem[i].attrs);
  3702. fw_mem[i].va = NULL;
  3703. fw_mem[i].pa = 0;
  3704. fw_mem[i].size = 0;
  3705. fw_mem[i].type = 0;
  3706. }
  3707. }
  3708. plat_priv->fw_mem_seg_len = 0;
  3709. }
  3710. int cnss_pci_alloc_qdss_mem(struct cnss_pci_data *pci_priv)
  3711. {
  3712. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3713. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3714. int i, j;
  3715. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3716. if (!qdss_mem[i].va && qdss_mem[i].size) {
  3717. qdss_mem[i].va =
  3718. dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3719. qdss_mem[i].size,
  3720. &qdss_mem[i].pa,
  3721. GFP_KERNEL);
  3722. if (!qdss_mem[i].va) {
  3723. cnss_pr_err("Failed to allocate QDSS memory for FW, size: 0x%zx, type: %u, chuck-ID: %d\n",
  3724. qdss_mem[i].size,
  3725. qdss_mem[i].type, i);
  3726. break;
  3727. }
  3728. }
  3729. }
  3730. /* Best-effort allocation for QDSS trace */
  3731. if (i < plat_priv->qdss_mem_seg_len) {
  3732. for (j = i; j < plat_priv->qdss_mem_seg_len; j++) {
  3733. qdss_mem[j].type = 0;
  3734. qdss_mem[j].size = 0;
  3735. }
  3736. plat_priv->qdss_mem_seg_len = i;
  3737. }
  3738. return 0;
  3739. }
  3740. void cnss_pci_free_qdss_mem(struct cnss_pci_data *pci_priv)
  3741. {
  3742. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3743. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3744. int i;
  3745. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3746. if (qdss_mem[i].va && qdss_mem[i].size) {
  3747. cnss_pr_dbg("Freeing memory for QDSS: pa: %pa, size: 0x%zx, type: %u\n",
  3748. &qdss_mem[i].pa, qdss_mem[i].size,
  3749. qdss_mem[i].type);
  3750. dma_free_coherent(&pci_priv->pci_dev->dev,
  3751. qdss_mem[i].size, qdss_mem[i].va,
  3752. qdss_mem[i].pa);
  3753. qdss_mem[i].va = NULL;
  3754. qdss_mem[i].pa = 0;
  3755. qdss_mem[i].size = 0;
  3756. qdss_mem[i].type = 0;
  3757. }
  3758. }
  3759. plat_priv->qdss_mem_seg_len = 0;
  3760. }
  3761. int cnss_pci_load_m3(struct cnss_pci_data *pci_priv)
  3762. {
  3763. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3764. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3765. char filename[MAX_FIRMWARE_NAME_LEN];
  3766. char *phy_filename = DEFAULT_PHY_UCODE_FILE_NAME;
  3767. const struct firmware *fw_entry;
  3768. int ret = 0;
  3769. /* Use forward compatibility here since for any recent device
  3770. * it should use DEFAULT_PHY_UCODE_FILE_NAME.
  3771. */
  3772. switch (pci_priv->device_id) {
  3773. case QCA6174_DEVICE_ID:
  3774. cnss_pr_err("Invalid device ID (0x%x) to load phy image\n",
  3775. pci_priv->device_id);
  3776. return -EINVAL;
  3777. case QCA6290_DEVICE_ID:
  3778. case QCA6390_DEVICE_ID:
  3779. case QCA6490_DEVICE_ID:
  3780. phy_filename = DEFAULT_PHY_M3_FILE_NAME;
  3781. break;
  3782. case KIWI_DEVICE_ID:
  3783. switch (plat_priv->device_version.major_version) {
  3784. case FW_V2_NUMBER:
  3785. phy_filename = PHY_UCODE_V2_FILE_NAME;
  3786. break;
  3787. default:
  3788. break;
  3789. }
  3790. break;
  3791. default:
  3792. break;
  3793. }
  3794. if (!m3_mem->va && !m3_mem->size) {
  3795. cnss_pci_add_fw_prefix_name(pci_priv, filename,
  3796. phy_filename);
  3797. ret = firmware_request_nowarn(&fw_entry, filename,
  3798. &pci_priv->pci_dev->dev);
  3799. if (ret) {
  3800. cnss_pr_err("Failed to load M3 image: %s\n", filename);
  3801. return ret;
  3802. }
  3803. m3_mem->va = dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3804. fw_entry->size, &m3_mem->pa,
  3805. GFP_KERNEL);
  3806. if (!m3_mem->va) {
  3807. cnss_pr_err("Failed to allocate memory for M3, size: 0x%zx\n",
  3808. fw_entry->size);
  3809. release_firmware(fw_entry);
  3810. return -ENOMEM;
  3811. }
  3812. memcpy(m3_mem->va, fw_entry->data, fw_entry->size);
  3813. m3_mem->size = fw_entry->size;
  3814. release_firmware(fw_entry);
  3815. }
  3816. return 0;
  3817. }
  3818. static void cnss_pci_free_m3_mem(struct cnss_pci_data *pci_priv)
  3819. {
  3820. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3821. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3822. if (m3_mem->va && m3_mem->size) {
  3823. cnss_pr_dbg("Freeing memory for M3, va: 0x%pK, pa: %pa, size: 0x%zx\n",
  3824. m3_mem->va, &m3_mem->pa, m3_mem->size);
  3825. dma_free_coherent(&pci_priv->pci_dev->dev, m3_mem->size,
  3826. m3_mem->va, m3_mem->pa);
  3827. }
  3828. m3_mem->va = NULL;
  3829. m3_mem->pa = 0;
  3830. m3_mem->size = 0;
  3831. }
  3832. void cnss_pci_fw_boot_timeout_hdlr(struct cnss_pci_data *pci_priv)
  3833. {
  3834. struct cnss_plat_data *plat_priv;
  3835. if (!pci_priv)
  3836. return;
  3837. cnss_fatal_err("Timeout waiting for FW ready indication\n");
  3838. plat_priv = pci_priv->plat_priv;
  3839. if (!plat_priv)
  3840. return;
  3841. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  3842. cnss_pr_dbg("Ignore FW ready timeout for calibration mode\n");
  3843. return;
  3844. }
  3845. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  3846. CNSS_REASON_TIMEOUT);
  3847. }
  3848. static int cnss_pci_smmu_fault_handler(struct iommu_domain *domain,
  3849. struct device *dev, unsigned long iova,
  3850. int flags, void *handler_token)
  3851. {
  3852. struct cnss_pci_data *pci_priv = handler_token;
  3853. cnss_fatal_err("SMMU fault happened with IOVA 0x%lx\n", iova);
  3854. if (!pci_priv) {
  3855. cnss_pr_err("pci_priv is NULL\n");
  3856. return -ENODEV;
  3857. }
  3858. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  3859. cnss_force_fw_assert(&pci_priv->pci_dev->dev);
  3860. /* IOMMU driver requires -ENOSYS to print debug info. */
  3861. return -ENOSYS;
  3862. }
  3863. static int cnss_pci_init_smmu(struct cnss_pci_data *pci_priv)
  3864. {
  3865. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3866. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3867. struct device_node *of_node;
  3868. struct resource *res;
  3869. const char *iommu_dma_type;
  3870. u32 addr_win[2];
  3871. int ret = 0;
  3872. of_node = of_parse_phandle(pci_dev->dev.of_node, "qcom,iommu-group", 0);
  3873. if (!of_node)
  3874. return ret;
  3875. cnss_pr_dbg("Initializing SMMU\n");
  3876. pci_priv->iommu_domain = iommu_get_domain_for_dev(&pci_dev->dev);
  3877. ret = of_property_read_string(of_node, "qcom,iommu-dma",
  3878. &iommu_dma_type);
  3879. if (!ret && !strcmp("fastmap", iommu_dma_type)) {
  3880. cnss_pr_dbg("Enabling SMMU S1 stage\n");
  3881. pci_priv->smmu_s1_enable = true;
  3882. iommu_set_fault_handler(pci_priv->iommu_domain,
  3883. cnss_pci_smmu_fault_handler, pci_priv);
  3884. }
  3885. ret = of_property_read_u32_array(of_node, "qcom,iommu-dma-addr-pool",
  3886. addr_win, ARRAY_SIZE(addr_win));
  3887. if (ret) {
  3888. cnss_pr_err("Invalid SMMU size window, err = %d\n", ret);
  3889. of_node_put(of_node);
  3890. return ret;
  3891. }
  3892. pci_priv->smmu_iova_start = addr_win[0];
  3893. pci_priv->smmu_iova_len = addr_win[1];
  3894. cnss_pr_dbg("smmu_iova_start: %pa, smmu_iova_len: 0x%zx\n",
  3895. &pci_priv->smmu_iova_start,
  3896. pci_priv->smmu_iova_len);
  3897. res = platform_get_resource_byname(plat_priv->plat_dev, IORESOURCE_MEM,
  3898. "smmu_iova_ipa");
  3899. if (res) {
  3900. pci_priv->smmu_iova_ipa_start = res->start;
  3901. pci_priv->smmu_iova_ipa_current = res->start;
  3902. pci_priv->smmu_iova_ipa_len = resource_size(res);
  3903. cnss_pr_dbg("smmu_iova_ipa_start: %pa, smmu_iova_ipa_len: 0x%zx\n",
  3904. &pci_priv->smmu_iova_ipa_start,
  3905. pci_priv->smmu_iova_ipa_len);
  3906. }
  3907. pci_priv->iommu_geometry = of_property_read_bool(of_node,
  3908. "qcom,iommu-geometry");
  3909. cnss_pr_dbg("iommu_geometry: %d\n", pci_priv->iommu_geometry);
  3910. of_node_put(of_node);
  3911. return 0;
  3912. }
  3913. static void cnss_pci_deinit_smmu(struct cnss_pci_data *pci_priv)
  3914. {
  3915. pci_priv->iommu_domain = NULL;
  3916. }
  3917. int cnss_pci_get_iova(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3918. {
  3919. if (!pci_priv)
  3920. return -ENODEV;
  3921. if (!pci_priv->smmu_iova_len)
  3922. return -EINVAL;
  3923. *addr = pci_priv->smmu_iova_start;
  3924. *size = pci_priv->smmu_iova_len;
  3925. return 0;
  3926. }
  3927. int cnss_pci_get_iova_ipa(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3928. {
  3929. if (!pci_priv)
  3930. return -ENODEV;
  3931. if (!pci_priv->smmu_iova_ipa_len)
  3932. return -EINVAL;
  3933. *addr = pci_priv->smmu_iova_ipa_start;
  3934. *size = pci_priv->smmu_iova_ipa_len;
  3935. return 0;
  3936. }
  3937. struct iommu_domain *cnss_smmu_get_domain(struct device *dev)
  3938. {
  3939. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3940. if (!pci_priv)
  3941. return NULL;
  3942. return pci_priv->iommu_domain;
  3943. }
  3944. EXPORT_SYMBOL(cnss_smmu_get_domain);
  3945. int cnss_smmu_map(struct device *dev,
  3946. phys_addr_t paddr, uint32_t *iova_addr, size_t size)
  3947. {
  3948. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3949. struct cnss_plat_data *plat_priv;
  3950. unsigned long iova;
  3951. size_t len;
  3952. int ret = 0;
  3953. int flag = IOMMU_READ | IOMMU_WRITE;
  3954. struct pci_dev *root_port;
  3955. struct device_node *root_of_node;
  3956. bool dma_coherent = false;
  3957. if (!pci_priv)
  3958. return -ENODEV;
  3959. if (!iova_addr) {
  3960. cnss_pr_err("iova_addr is NULL, paddr %pa, size %zu\n",
  3961. &paddr, size);
  3962. return -EINVAL;
  3963. }
  3964. plat_priv = pci_priv->plat_priv;
  3965. len = roundup(size + paddr - rounddown(paddr, PAGE_SIZE), PAGE_SIZE);
  3966. iova = roundup(pci_priv->smmu_iova_ipa_current, PAGE_SIZE);
  3967. if (pci_priv->iommu_geometry &&
  3968. iova >= pci_priv->smmu_iova_ipa_start +
  3969. pci_priv->smmu_iova_ipa_len) {
  3970. cnss_pr_err("No IOVA space to map, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3971. iova,
  3972. &pci_priv->smmu_iova_ipa_start,
  3973. pci_priv->smmu_iova_ipa_len);
  3974. return -ENOMEM;
  3975. }
  3976. if (!test_bit(DISABLE_IO_COHERENCY,
  3977. &plat_priv->ctrl_params.quirks)) {
  3978. root_port = pcie_find_root_port(pci_priv->pci_dev);
  3979. if (!root_port) {
  3980. cnss_pr_err("Root port is null, so dma_coherent is disabled\n");
  3981. } else {
  3982. root_of_node = root_port->dev.of_node;
  3983. if (root_of_node && root_of_node->parent) {
  3984. dma_coherent =
  3985. of_property_read_bool(root_of_node->parent,
  3986. "dma-coherent");
  3987. cnss_pr_dbg("dma-coherent is %s\n",
  3988. dma_coherent ? "enabled" : "disabled");
  3989. if (dma_coherent)
  3990. flag |= IOMMU_CACHE;
  3991. }
  3992. }
  3993. }
  3994. cnss_pr_dbg("IOMMU map: iova %lx, len %zu\n", iova, len);
  3995. ret = iommu_map(pci_priv->iommu_domain, iova,
  3996. rounddown(paddr, PAGE_SIZE), len, flag);
  3997. if (ret) {
  3998. cnss_pr_err("PA to IOVA mapping failed, ret %d\n", ret);
  3999. return ret;
  4000. }
  4001. pci_priv->smmu_iova_ipa_current = iova + len;
  4002. *iova_addr = (uint32_t)(iova + paddr - rounddown(paddr, PAGE_SIZE));
  4003. cnss_pr_dbg("IOMMU map: iova_addr %lx\n", *iova_addr);
  4004. return 0;
  4005. }
  4006. EXPORT_SYMBOL(cnss_smmu_map);
  4007. int cnss_smmu_unmap(struct device *dev, uint32_t iova_addr, size_t size)
  4008. {
  4009. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4010. unsigned long iova;
  4011. size_t unmapped;
  4012. size_t len;
  4013. if (!pci_priv)
  4014. return -ENODEV;
  4015. iova = rounddown(iova_addr, PAGE_SIZE);
  4016. len = roundup(size + iova_addr - iova, PAGE_SIZE);
  4017. if (iova >= pci_priv->smmu_iova_ipa_start +
  4018. pci_priv->smmu_iova_ipa_len) {
  4019. cnss_pr_err("Out of IOVA space to unmap, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  4020. iova,
  4021. &pci_priv->smmu_iova_ipa_start,
  4022. pci_priv->smmu_iova_ipa_len);
  4023. return -ENOMEM;
  4024. }
  4025. cnss_pr_dbg("IOMMU unmap: iova %lx, len %zu\n", iova, len);
  4026. unmapped = iommu_unmap(pci_priv->iommu_domain, iova, len);
  4027. if (unmapped != len) {
  4028. cnss_pr_err("IOMMU unmap failed, unmapped = %zu, requested = %zu\n",
  4029. unmapped, len);
  4030. return -EINVAL;
  4031. }
  4032. pci_priv->smmu_iova_ipa_current = iova;
  4033. return 0;
  4034. }
  4035. EXPORT_SYMBOL(cnss_smmu_unmap);
  4036. int cnss_get_soc_info(struct device *dev, struct cnss_soc_info *info)
  4037. {
  4038. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4039. struct cnss_plat_data *plat_priv;
  4040. if (!pci_priv)
  4041. return -ENODEV;
  4042. plat_priv = pci_priv->plat_priv;
  4043. if (!plat_priv)
  4044. return -ENODEV;
  4045. info->va = pci_priv->bar;
  4046. info->pa = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4047. info->chip_id = plat_priv->chip_info.chip_id;
  4048. info->chip_family = plat_priv->chip_info.chip_family;
  4049. info->board_id = plat_priv->board_info.board_id;
  4050. info->soc_id = plat_priv->soc_info.soc_id;
  4051. info->fw_version = plat_priv->fw_version_info.fw_version;
  4052. strlcpy(info->fw_build_timestamp,
  4053. plat_priv->fw_version_info.fw_build_timestamp,
  4054. sizeof(info->fw_build_timestamp));
  4055. memcpy(&info->device_version, &plat_priv->device_version,
  4056. sizeof(info->device_version));
  4057. memcpy(&info->dev_mem_info, &plat_priv->dev_mem_info,
  4058. sizeof(info->dev_mem_info));
  4059. return 0;
  4060. }
  4061. EXPORT_SYMBOL(cnss_get_soc_info);
  4062. static struct cnss_msi_config msi_config = {
  4063. .total_vectors = 32,
  4064. .total_users = 4,
  4065. .users = (struct cnss_msi_user[]) {
  4066. { .name = "MHI", .num_vectors = 3, .base_vector = 0 },
  4067. { .name = "CE", .num_vectors = 10, .base_vector = 3 },
  4068. { .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
  4069. { .name = "DP", .num_vectors = 18, .base_vector = 14 },
  4070. },
  4071. };
  4072. static int cnss_pci_get_msi_assignment(struct cnss_pci_data *pci_priv)
  4073. {
  4074. pci_priv->msi_config = &msi_config;
  4075. return 0;
  4076. }
  4077. static int cnss_pci_enable_msi(struct cnss_pci_data *pci_priv)
  4078. {
  4079. int ret = 0;
  4080. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4081. int num_vectors;
  4082. struct cnss_msi_config *msi_config;
  4083. struct msi_desc *msi_desc;
  4084. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4085. return 0;
  4086. ret = cnss_pci_get_msi_assignment(pci_priv);
  4087. if (ret) {
  4088. cnss_pr_err("Failed to get MSI assignment, err = %d\n", ret);
  4089. goto out;
  4090. }
  4091. msi_config = pci_priv->msi_config;
  4092. if (!msi_config) {
  4093. cnss_pr_err("msi_config is NULL!\n");
  4094. ret = -EINVAL;
  4095. goto out;
  4096. }
  4097. num_vectors = pci_alloc_irq_vectors(pci_dev,
  4098. msi_config->total_vectors,
  4099. msi_config->total_vectors,
  4100. PCI_IRQ_MSI);
  4101. if (num_vectors != msi_config->total_vectors) {
  4102. cnss_pr_err("Failed to get enough MSI vectors (%d), available vectors = %d",
  4103. msi_config->total_vectors, num_vectors);
  4104. if (num_vectors >= 0)
  4105. ret = -EINVAL;
  4106. goto reset_msi_config;
  4107. }
  4108. msi_desc = irq_get_msi_desc(pci_dev->irq);
  4109. if (!msi_desc) {
  4110. cnss_pr_err("msi_desc is NULL!\n");
  4111. ret = -EINVAL;
  4112. goto free_msi_vector;
  4113. }
  4114. pci_priv->msi_ep_base_data = msi_desc->msg.data;
  4115. cnss_pr_dbg("MSI base data is %d\n", pci_priv->msi_ep_base_data);
  4116. return 0;
  4117. free_msi_vector:
  4118. pci_free_irq_vectors(pci_priv->pci_dev);
  4119. reset_msi_config:
  4120. pci_priv->msi_config = NULL;
  4121. out:
  4122. return ret;
  4123. }
  4124. static void cnss_pci_disable_msi(struct cnss_pci_data *pci_priv)
  4125. {
  4126. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4127. return;
  4128. pci_free_irq_vectors(pci_priv->pci_dev);
  4129. }
  4130. int cnss_get_user_msi_assignment(struct device *dev, char *user_name,
  4131. int *num_vectors, u32 *user_base_data,
  4132. u32 *base_vector)
  4133. {
  4134. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4135. struct cnss_msi_config *msi_config;
  4136. int idx;
  4137. if (!pci_priv)
  4138. return -ENODEV;
  4139. msi_config = pci_priv->msi_config;
  4140. if (!msi_config) {
  4141. cnss_pr_err("MSI is not supported.\n");
  4142. return -EINVAL;
  4143. }
  4144. for (idx = 0; idx < msi_config->total_users; idx++) {
  4145. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  4146. *num_vectors = msi_config->users[idx].num_vectors;
  4147. *user_base_data = msi_config->users[idx].base_vector
  4148. + pci_priv->msi_ep_base_data;
  4149. *base_vector = msi_config->users[idx].base_vector;
  4150. cnss_pr_dbg("Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  4151. user_name, *num_vectors, *user_base_data,
  4152. *base_vector);
  4153. return 0;
  4154. }
  4155. }
  4156. cnss_pr_err("Failed to find MSI assignment for %s!\n", user_name);
  4157. return -EINVAL;
  4158. }
  4159. EXPORT_SYMBOL(cnss_get_user_msi_assignment);
  4160. int cnss_get_msi_irq(struct device *dev, unsigned int vector)
  4161. {
  4162. struct pci_dev *pci_dev = to_pci_dev(dev);
  4163. int irq_num;
  4164. irq_num = pci_irq_vector(pci_dev, vector);
  4165. cnss_pr_dbg("Get IRQ number %d for vector index %d\n", irq_num, vector);
  4166. return irq_num;
  4167. }
  4168. EXPORT_SYMBOL(cnss_get_msi_irq);
  4169. void cnss_get_msi_address(struct device *dev, u32 *msi_addr_low,
  4170. u32 *msi_addr_high)
  4171. {
  4172. struct pci_dev *pci_dev = to_pci_dev(dev);
  4173. u16 control;
  4174. pci_read_config_word(pci_dev, pci_dev->msi_cap + PCI_MSI_FLAGS,
  4175. &control);
  4176. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  4177. msi_addr_low);
  4178. /* Return MSI high address only when device supports 64-bit MSI */
  4179. if (control & PCI_MSI_FLAGS_64BIT)
  4180. pci_read_config_dword(pci_dev,
  4181. pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  4182. msi_addr_high);
  4183. else
  4184. *msi_addr_high = 0;
  4185. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  4186. *msi_addr_low, *msi_addr_high);
  4187. }
  4188. EXPORT_SYMBOL(cnss_get_msi_address);
  4189. u32 cnss_pci_get_wake_msi(struct cnss_pci_data *pci_priv)
  4190. {
  4191. int ret, num_vectors;
  4192. u32 user_base_data, base_vector;
  4193. if (!pci_priv)
  4194. return -ENODEV;
  4195. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4196. WAKE_MSI_NAME, &num_vectors,
  4197. &user_base_data, &base_vector);
  4198. if (ret) {
  4199. cnss_pr_err("WAKE MSI is not valid\n");
  4200. return 0;
  4201. }
  4202. return user_base_data;
  4203. }
  4204. static int cnss_pci_enable_bus(struct cnss_pci_data *pci_priv)
  4205. {
  4206. int ret = 0;
  4207. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4208. u16 device_id;
  4209. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
  4210. if (device_id != pci_priv->pci_device_id->device) {
  4211. cnss_pr_err("PCI device ID mismatch, config ID: 0x%x, probe ID: 0x%x\n",
  4212. device_id, pci_priv->pci_device_id->device);
  4213. ret = -EIO;
  4214. goto out;
  4215. }
  4216. ret = pci_assign_resource(pci_dev, PCI_BAR_NUM);
  4217. if (ret) {
  4218. pr_err("Failed to assign PCI resource, err = %d\n", ret);
  4219. goto out;
  4220. }
  4221. ret = pci_enable_device(pci_dev);
  4222. if (ret) {
  4223. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  4224. goto out;
  4225. }
  4226. ret = pci_request_region(pci_dev, PCI_BAR_NUM, "cnss");
  4227. if (ret) {
  4228. cnss_pr_err("Failed to request PCI region, err = %d\n", ret);
  4229. goto disable_device;
  4230. }
  4231. switch (device_id) {
  4232. case QCA6174_DEVICE_ID:
  4233. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4234. break;
  4235. case QCA6390_DEVICE_ID:
  4236. case QCA6490_DEVICE_ID:
  4237. case KIWI_DEVICE_ID:
  4238. pci_priv->dma_bit_mask = PCI_DMA_MASK_36_BIT;
  4239. break;
  4240. default:
  4241. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4242. break;
  4243. }
  4244. cnss_pr_dbg("Set PCI DMA MASK (0x%llx)\n", pci_priv->dma_bit_mask);
  4245. ret = pci_set_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4246. if (ret) {
  4247. cnss_pr_err("Failed to set PCI DMA mask, err = %d\n", ret);
  4248. goto release_region;
  4249. }
  4250. ret = pci_set_consistent_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4251. if (ret) {
  4252. cnss_pr_err("Failed to set PCI consistent DMA mask, err = %d\n",
  4253. ret);
  4254. goto release_region;
  4255. }
  4256. pci_priv->bar = pci_iomap(pci_dev, PCI_BAR_NUM, 0);
  4257. if (!pci_priv->bar) {
  4258. cnss_pr_err("Failed to do PCI IO map!\n");
  4259. ret = -EIO;
  4260. goto release_region;
  4261. }
  4262. /* Save default config space without BME enabled */
  4263. pci_save_state(pci_dev);
  4264. pci_priv->default_state = pci_store_saved_state(pci_dev);
  4265. pci_set_master(pci_dev);
  4266. return 0;
  4267. release_region:
  4268. pci_release_region(pci_dev, PCI_BAR_NUM);
  4269. disable_device:
  4270. pci_disable_device(pci_dev);
  4271. out:
  4272. return ret;
  4273. }
  4274. static void cnss_pci_disable_bus(struct cnss_pci_data *pci_priv)
  4275. {
  4276. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4277. pci_clear_master(pci_dev);
  4278. pci_load_and_free_saved_state(pci_dev, &pci_priv->saved_state);
  4279. pci_load_and_free_saved_state(pci_dev, &pci_priv->default_state);
  4280. if (pci_priv->bar) {
  4281. pci_iounmap(pci_dev, pci_priv->bar);
  4282. pci_priv->bar = NULL;
  4283. }
  4284. pci_release_region(pci_dev, PCI_BAR_NUM);
  4285. if (pci_is_enabled(pci_dev))
  4286. pci_disable_device(pci_dev);
  4287. }
  4288. static void cnss_pci_dump_qdss_reg(struct cnss_pci_data *pci_priv)
  4289. {
  4290. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4291. int i, array_size = ARRAY_SIZE(qdss_csr) - 1;
  4292. gfp_t gfp = GFP_KERNEL;
  4293. u32 reg_offset;
  4294. if (in_interrupt() || irqs_disabled())
  4295. gfp = GFP_ATOMIC;
  4296. if (!plat_priv->qdss_reg) {
  4297. plat_priv->qdss_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  4298. sizeof(*plat_priv->qdss_reg)
  4299. * array_size, gfp);
  4300. if (!plat_priv->qdss_reg)
  4301. return;
  4302. }
  4303. cnss_pr_dbg("Start to dump qdss registers\n");
  4304. for (i = 0; qdss_csr[i].name; i++) {
  4305. reg_offset = QDSS_APB_DEC_CSR_BASE + qdss_csr[i].offset;
  4306. if (cnss_pci_reg_read(pci_priv, reg_offset,
  4307. &plat_priv->qdss_reg[i]))
  4308. return;
  4309. cnss_pr_dbg("%s[0x%x] = 0x%x\n", qdss_csr[i].name, reg_offset,
  4310. plat_priv->qdss_reg[i]);
  4311. }
  4312. }
  4313. static void cnss_pci_dump_ce_reg(struct cnss_pci_data *pci_priv,
  4314. enum cnss_ce_index ce)
  4315. {
  4316. int i;
  4317. u32 ce_base = ce * CE_REG_INTERVAL;
  4318. u32 reg_offset, src_ring_base, dst_ring_base, cmn_base, val;
  4319. switch (pci_priv->device_id) {
  4320. case QCA6390_DEVICE_ID:
  4321. src_ring_base = QCA6390_CE_SRC_RING_REG_BASE;
  4322. dst_ring_base = QCA6390_CE_DST_RING_REG_BASE;
  4323. cmn_base = QCA6390_CE_COMMON_REG_BASE;
  4324. break;
  4325. case QCA6490_DEVICE_ID:
  4326. src_ring_base = QCA6490_CE_SRC_RING_REG_BASE;
  4327. dst_ring_base = QCA6490_CE_DST_RING_REG_BASE;
  4328. cmn_base = QCA6490_CE_COMMON_REG_BASE;
  4329. break;
  4330. default:
  4331. return;
  4332. }
  4333. switch (ce) {
  4334. case CNSS_CE_09:
  4335. case CNSS_CE_10:
  4336. for (i = 0; ce_src[i].name; i++) {
  4337. reg_offset = src_ring_base + ce_base + ce_src[i].offset;
  4338. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4339. return;
  4340. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4341. ce, ce_src[i].name, reg_offset, val);
  4342. }
  4343. for (i = 0; ce_dst[i].name; i++) {
  4344. reg_offset = dst_ring_base + ce_base + ce_dst[i].offset;
  4345. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4346. return;
  4347. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4348. ce, ce_dst[i].name, reg_offset, val);
  4349. }
  4350. break;
  4351. case CNSS_CE_COMMON:
  4352. for (i = 0; ce_cmn[i].name; i++) {
  4353. reg_offset = cmn_base + ce_cmn[i].offset;
  4354. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4355. return;
  4356. cnss_pr_dbg("CE_COMMON_%s[0x%x] = 0x%x\n",
  4357. ce_cmn[i].name, reg_offset, val);
  4358. }
  4359. break;
  4360. default:
  4361. cnss_pr_err("Unsupported CE[%d] registers dump\n", ce);
  4362. }
  4363. }
  4364. static void cnss_pci_dump_debug_reg(struct cnss_pci_data *pci_priv)
  4365. {
  4366. if (cnss_pci_check_link_status(pci_priv))
  4367. return;
  4368. cnss_pr_dbg("Start to dump debug registers\n");
  4369. cnss_mhi_debug_reg_dump(pci_priv);
  4370. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4371. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_COMMON);
  4372. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_09);
  4373. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_10);
  4374. }
  4375. static int cnss_pci_assert_host_sol(struct cnss_pci_data *pci_priv)
  4376. {
  4377. if (cnss_get_host_sol_value(pci_priv->plat_priv))
  4378. return -EINVAL;
  4379. cnss_pr_dbg("Assert host SOL GPIO to retry RDDM, expecting link down\n");
  4380. cnss_set_host_sol_value(pci_priv->plat_priv, 1);
  4381. return 0;
  4382. }
  4383. int cnss_pci_force_fw_assert_hdlr(struct cnss_pci_data *pci_priv)
  4384. {
  4385. int ret;
  4386. struct cnss_plat_data *plat_priv;
  4387. if (!pci_priv)
  4388. return -ENODEV;
  4389. plat_priv = pci_priv->plat_priv;
  4390. if (!plat_priv)
  4391. return -ENODEV;
  4392. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4393. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state))
  4394. return -EINVAL;
  4395. cnss_auto_resume(&pci_priv->pci_dev->dev);
  4396. if (!cnss_pci_check_link_status(pci_priv))
  4397. cnss_mhi_debug_reg_dump(pci_priv);
  4398. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4399. cnss_pci_dump_misc_reg(pci_priv);
  4400. cnss_pci_dump_shadow_reg(pci_priv);
  4401. /* If link is still down here, directly trigger link down recovery */
  4402. ret = cnss_pci_check_link_status(pci_priv);
  4403. if (ret) {
  4404. cnss_pci_link_down(&pci_priv->pci_dev->dev);
  4405. return 0;
  4406. }
  4407. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_TRIGGER_RDDM);
  4408. if (ret) {
  4409. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4410. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state)) {
  4411. cnss_pr_dbg("MHI is not powered on, ignore RDDM failure\n");
  4412. return 0;
  4413. }
  4414. cnss_fatal_err("Failed to trigger RDDM, err = %d\n", ret);
  4415. if (!cnss_pci_assert_host_sol(pci_priv))
  4416. return 0;
  4417. cnss_pci_dump_debug_reg(pci_priv);
  4418. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4419. CNSS_REASON_DEFAULT);
  4420. return ret;
  4421. }
  4422. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  4423. mod_timer(&pci_priv->dev_rddm_timer,
  4424. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4425. }
  4426. return 0;
  4427. }
  4428. static void cnss_pci_add_dump_seg(struct cnss_pci_data *pci_priv,
  4429. struct cnss_dump_seg *dump_seg,
  4430. enum cnss_fw_dump_type type, int seg_no,
  4431. void *va, dma_addr_t dma, size_t size)
  4432. {
  4433. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4434. struct device *dev = &pci_priv->pci_dev->dev;
  4435. phys_addr_t pa;
  4436. dump_seg->address = dma;
  4437. dump_seg->v_address = va;
  4438. dump_seg->size = size;
  4439. dump_seg->type = type;
  4440. cnss_pr_dbg("Seg: %x, va: %pK, dma: %pa, size: 0x%zx\n",
  4441. seg_no, va, &dma, size);
  4442. if (cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS))
  4443. return;
  4444. cnss_minidump_add_region(plat_priv, type, seg_no, va, pa, size);
  4445. }
  4446. static void cnss_pci_remove_dump_seg(struct cnss_pci_data *pci_priv,
  4447. struct cnss_dump_seg *dump_seg,
  4448. enum cnss_fw_dump_type type, int seg_no,
  4449. void *va, dma_addr_t dma, size_t size)
  4450. {
  4451. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4452. struct device *dev = &pci_priv->pci_dev->dev;
  4453. phys_addr_t pa;
  4454. cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS);
  4455. cnss_minidump_remove_region(plat_priv, type, seg_no, va, pa, size);
  4456. }
  4457. int cnss_pci_call_driver_uevent(struct cnss_pci_data *pci_priv,
  4458. enum cnss_driver_status status, void *data)
  4459. {
  4460. struct cnss_uevent_data uevent_data;
  4461. struct cnss_wlan_driver *driver_ops;
  4462. driver_ops = pci_priv->driver_ops;
  4463. if (!driver_ops || !driver_ops->update_event) {
  4464. cnss_pr_dbg("Hang event driver ops is NULL\n");
  4465. return -EINVAL;
  4466. }
  4467. cnss_pr_dbg("Calling driver uevent: %d\n", status);
  4468. uevent_data.status = status;
  4469. uevent_data.data = data;
  4470. return driver_ops->update_event(pci_priv->pci_dev, &uevent_data);
  4471. }
  4472. static void cnss_pci_send_hang_event(struct cnss_pci_data *pci_priv)
  4473. {
  4474. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4475. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4476. struct cnss_hang_event hang_event;
  4477. void *hang_data_va = NULL;
  4478. u64 offset = 0;
  4479. int i = 0;
  4480. if (!fw_mem || !plat_priv->fw_mem_seg_len)
  4481. return;
  4482. memset(&hang_event, 0, sizeof(hang_event));
  4483. switch (pci_priv->device_id) {
  4484. case QCA6390_DEVICE_ID:
  4485. offset = HST_HANG_DATA_OFFSET;
  4486. break;
  4487. case QCA6490_DEVICE_ID:
  4488. offset = HSP_HANG_DATA_OFFSET;
  4489. break;
  4490. default:
  4491. cnss_pr_err("Skip Hang Event Data as unsupported Device ID received: %d\n",
  4492. pci_priv->device_id);
  4493. return;
  4494. }
  4495. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4496. if (fw_mem[i].type == QMI_WLFW_MEM_TYPE_DDR_V01 &&
  4497. fw_mem[i].va) {
  4498. hang_data_va = fw_mem[i].va + offset;
  4499. hang_event.hang_event_data = kmemdup(hang_data_va,
  4500. HANG_DATA_LENGTH,
  4501. GFP_ATOMIC);
  4502. if (!hang_event.hang_event_data) {
  4503. cnss_pr_dbg("Hang data memory alloc failed\n");
  4504. return;
  4505. }
  4506. hang_event.hang_event_data_len = HANG_DATA_LENGTH;
  4507. break;
  4508. }
  4509. }
  4510. cnss_pci_call_driver_uevent(pci_priv, CNSS_HANG_EVENT, &hang_event);
  4511. kfree(hang_event.hang_event_data);
  4512. hang_event.hang_event_data = NULL;
  4513. }
  4514. void cnss_pci_collect_dump_info(struct cnss_pci_data *pci_priv, bool in_panic)
  4515. {
  4516. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4517. struct cnss_dump_data *dump_data =
  4518. &plat_priv->ramdump_info_v2.dump_data;
  4519. struct cnss_dump_seg *dump_seg =
  4520. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4521. struct image_info *fw_image, *rddm_image;
  4522. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4523. int ret, i, j;
  4524. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  4525. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  4526. cnss_pci_send_hang_event(pci_priv);
  4527. if (test_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state)) {
  4528. cnss_pr_dbg("RAM dump is already collected, skip\n");
  4529. return;
  4530. }
  4531. if (!cnss_is_device_powered_on(plat_priv)) {
  4532. cnss_pr_dbg("Device is already powered off, skip\n");
  4533. return;
  4534. }
  4535. if (!in_panic) {
  4536. mutex_lock(&pci_priv->bus_lock);
  4537. ret = cnss_pci_check_link_status(pci_priv);
  4538. if (ret) {
  4539. if (ret != -EACCES) {
  4540. mutex_unlock(&pci_priv->bus_lock);
  4541. return;
  4542. }
  4543. if (cnss_pci_resume_bus(pci_priv)) {
  4544. mutex_unlock(&pci_priv->bus_lock);
  4545. return;
  4546. }
  4547. }
  4548. mutex_unlock(&pci_priv->bus_lock);
  4549. } else {
  4550. if (cnss_pci_check_link_status(pci_priv))
  4551. return;
  4552. /* Inside panic handler, reduce timeout for RDDM to avoid
  4553. * unnecessary hypervisor watchdog bite.
  4554. */
  4555. pci_priv->mhi_ctrl->timeout_ms /= 2;
  4556. }
  4557. cnss_mhi_debug_reg_dump(pci_priv);
  4558. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4559. cnss_pci_dump_misc_reg(pci_priv);
  4560. cnss_pci_dump_shadow_reg(pci_priv);
  4561. cnss_pci_dump_qdss_reg(pci_priv);
  4562. ret = mhi_download_rddm_image(pci_priv->mhi_ctrl, in_panic);
  4563. if (ret) {
  4564. cnss_fatal_err("Failed to download RDDM image, err = %d\n",
  4565. ret);
  4566. if (!cnss_pci_assert_host_sol(pci_priv))
  4567. return;
  4568. cnss_pci_dump_debug_reg(pci_priv);
  4569. return;
  4570. }
  4571. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4572. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4573. dump_data->nentries = 0;
  4574. cnss_mhi_dump_sfr(pci_priv);
  4575. if (!dump_seg) {
  4576. cnss_pr_warn("FW image dump collection not setup");
  4577. goto skip_dump;
  4578. }
  4579. cnss_pr_dbg("Collect FW image dump segment, nentries %d\n",
  4580. fw_image->entries);
  4581. for (i = 0; i < fw_image->entries; i++) {
  4582. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4583. fw_image->mhi_buf[i].buf,
  4584. fw_image->mhi_buf[i].dma_addr,
  4585. fw_image->mhi_buf[i].len);
  4586. dump_seg++;
  4587. }
  4588. dump_data->nentries += fw_image->entries;
  4589. cnss_pr_dbg("Collect RDDM image dump segment, nentries %d\n",
  4590. rddm_image->entries);
  4591. for (i = 0; i < rddm_image->entries; i++) {
  4592. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4593. rddm_image->mhi_buf[i].buf,
  4594. rddm_image->mhi_buf[i].dma_addr,
  4595. rddm_image->mhi_buf[i].len);
  4596. dump_seg++;
  4597. }
  4598. dump_data->nentries += rddm_image->entries;
  4599. cnss_pr_dbg("Collect remote heap dump segment\n");
  4600. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4601. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4602. cnss_pci_add_dump_seg(pci_priv, dump_seg,
  4603. CNSS_FW_REMOTE_HEAP, j,
  4604. fw_mem[i].va, fw_mem[i].pa,
  4605. fw_mem[i].size);
  4606. dump_seg++;
  4607. dump_data->nentries++;
  4608. j++;
  4609. }
  4610. }
  4611. if (dump_data->nentries > 0)
  4612. plat_priv->ramdump_info_v2.dump_data_valid = true;
  4613. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RDDM_DONE);
  4614. skip_dump:
  4615. complete(&plat_priv->rddm_complete);
  4616. }
  4617. void cnss_pci_clear_dump_info(struct cnss_pci_data *pci_priv)
  4618. {
  4619. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4620. struct cnss_dump_seg *dump_seg =
  4621. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4622. struct image_info *fw_image, *rddm_image;
  4623. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4624. int i, j;
  4625. if (!dump_seg)
  4626. return;
  4627. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4628. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4629. for (i = 0; i < fw_image->entries; i++) {
  4630. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4631. fw_image->mhi_buf[i].buf,
  4632. fw_image->mhi_buf[i].dma_addr,
  4633. fw_image->mhi_buf[i].len);
  4634. dump_seg++;
  4635. }
  4636. for (i = 0; i < rddm_image->entries; i++) {
  4637. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4638. rddm_image->mhi_buf[i].buf,
  4639. rddm_image->mhi_buf[i].dma_addr,
  4640. rddm_image->mhi_buf[i].len);
  4641. dump_seg++;
  4642. }
  4643. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4644. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4645. cnss_pci_remove_dump_seg(pci_priv, dump_seg,
  4646. CNSS_FW_REMOTE_HEAP, j,
  4647. fw_mem[i].va, fw_mem[i].pa,
  4648. fw_mem[i].size);
  4649. dump_seg++;
  4650. j++;
  4651. }
  4652. }
  4653. plat_priv->ramdump_info_v2.dump_data.nentries = 0;
  4654. plat_priv->ramdump_info_v2.dump_data_valid = false;
  4655. }
  4656. void cnss_pci_device_crashed(struct cnss_pci_data *pci_priv)
  4657. {
  4658. if (!pci_priv)
  4659. return;
  4660. cnss_device_crashed(&pci_priv->pci_dev->dev);
  4661. }
  4662. static int cnss_mhi_pm_runtime_get(struct mhi_controller *mhi_ctrl)
  4663. {
  4664. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4665. return cnss_pci_pm_runtime_get(pci_priv, RTPM_ID_MHI);
  4666. }
  4667. static void cnss_mhi_pm_runtime_put_noidle(struct mhi_controller *mhi_ctrl)
  4668. {
  4669. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4670. cnss_pci_pm_runtime_put_noidle(pci_priv, RTPM_ID_MHI);
  4671. }
  4672. void cnss_pci_add_fw_prefix_name(struct cnss_pci_data *pci_priv,
  4673. char *prefix_name, char *name)
  4674. {
  4675. struct cnss_plat_data *plat_priv;
  4676. if (!pci_priv)
  4677. return;
  4678. plat_priv = pci_priv->plat_priv;
  4679. if (!plat_priv->use_fw_path_with_prefix) {
  4680. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4681. return;
  4682. }
  4683. switch (pci_priv->device_id) {
  4684. case QCA6390_DEVICE_ID:
  4685. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4686. QCA6390_PATH_PREFIX "%s", name);
  4687. break;
  4688. case QCA6490_DEVICE_ID:
  4689. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4690. QCA6490_PATH_PREFIX "%s", name);
  4691. break;
  4692. case KIWI_DEVICE_ID:
  4693. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4694. KIWI_PATH_PREFIX "%s", name);
  4695. break;
  4696. default:
  4697. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4698. break;
  4699. }
  4700. cnss_pr_dbg("FW name added with prefix: %s\n", prefix_name);
  4701. }
  4702. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv)
  4703. {
  4704. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4705. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  4706. plat_priv->device_version.family_number = mhi_ctrl->family_number;
  4707. plat_priv->device_version.device_number = mhi_ctrl->device_number;
  4708. plat_priv->device_version.major_version = mhi_ctrl->major_version;
  4709. plat_priv->device_version.minor_version = mhi_ctrl->minor_version;
  4710. cnss_pr_dbg("Get device version info, family number: 0x%x, device number: 0x%x, major version: 0x%x, minor version: 0x%x\n",
  4711. plat_priv->device_version.family_number,
  4712. plat_priv->device_version.device_number,
  4713. plat_priv->device_version.major_version,
  4714. plat_priv->device_version.minor_version);
  4715. /* Only keep lower 4 bits as real device major version */
  4716. plat_priv->device_version.major_version &= DEVICE_MAJOR_VERSION_MASK;
  4717. switch (pci_priv->device_id) {
  4718. case QCA6390_DEVICE_ID:
  4719. if (plat_priv->device_version.major_version < FW_V2_NUMBER) {
  4720. cnss_pr_dbg("Device ID:version (0x%lx:%d) is not supported\n",
  4721. pci_priv->device_id,
  4722. plat_priv->device_version.major_version);
  4723. return -EINVAL;
  4724. }
  4725. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4726. FW_V2_FILE_NAME);
  4727. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4728. FW_V2_FILE_NAME);
  4729. break;
  4730. case QCA6490_DEVICE_ID:
  4731. case KIWI_DEVICE_ID:
  4732. switch (plat_priv->device_version.major_version) {
  4733. case FW_V2_NUMBER:
  4734. cnss_pci_add_fw_prefix_name(pci_priv,
  4735. plat_priv->firmware_name,
  4736. FW_V2_FILE_NAME);
  4737. snprintf(plat_priv->fw_fallback_name,
  4738. MAX_FIRMWARE_NAME_LEN,
  4739. FW_V2_FILE_NAME);
  4740. break;
  4741. default:
  4742. cnss_pci_add_fw_prefix_name(pci_priv,
  4743. plat_priv->firmware_name,
  4744. DEFAULT_FW_FILE_NAME);
  4745. snprintf(plat_priv->fw_fallback_name,
  4746. MAX_FIRMWARE_NAME_LEN,
  4747. DEFAULT_FW_FILE_NAME);
  4748. break;
  4749. }
  4750. break;
  4751. default:
  4752. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4753. DEFAULT_FW_FILE_NAME);
  4754. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4755. DEFAULT_FW_FILE_NAME);
  4756. break;
  4757. }
  4758. cnss_pr_dbg("FW name is %s, FW fallback name is %s\n",
  4759. plat_priv->firmware_name, plat_priv->fw_fallback_name);
  4760. return 0;
  4761. }
  4762. static char *cnss_mhi_notify_status_to_str(enum mhi_callback status)
  4763. {
  4764. switch (status) {
  4765. case MHI_CB_IDLE:
  4766. return "IDLE";
  4767. case MHI_CB_EE_RDDM:
  4768. return "RDDM";
  4769. case MHI_CB_SYS_ERROR:
  4770. return "SYS_ERROR";
  4771. case MHI_CB_FATAL_ERROR:
  4772. return "FATAL_ERROR";
  4773. case MHI_CB_EE_MISSION_MODE:
  4774. return "MISSION_MODE";
  4775. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4776. case MHI_CB_FALLBACK_IMG:
  4777. return "FW_FALLBACK";
  4778. #endif
  4779. default:
  4780. return "UNKNOWN";
  4781. }
  4782. };
  4783. static void cnss_dev_rddm_timeout_hdlr(struct timer_list *t)
  4784. {
  4785. struct cnss_pci_data *pci_priv =
  4786. from_timer(pci_priv, t, dev_rddm_timer);
  4787. enum mhi_ee_type mhi_ee;
  4788. if (!pci_priv)
  4789. return;
  4790. cnss_fatal_err("Timeout waiting for RDDM notification\n");
  4791. if (!cnss_pci_assert_host_sol(pci_priv))
  4792. return;
  4793. mhi_ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  4794. if (mhi_ee == MHI_EE_PBL)
  4795. cnss_pr_err("Unable to collect ramdumps due to abrupt reset\n");
  4796. if (mhi_ee == MHI_EE_RDDM) {
  4797. cnss_pr_info("Device MHI EE is RDDM, try to collect dump\n");
  4798. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4799. CNSS_REASON_RDDM);
  4800. } else {
  4801. cnss_mhi_debug_reg_dump(pci_priv);
  4802. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4803. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4804. CNSS_REASON_TIMEOUT);
  4805. }
  4806. }
  4807. static void cnss_boot_debug_timeout_hdlr(struct timer_list *t)
  4808. {
  4809. struct cnss_pci_data *pci_priv =
  4810. from_timer(pci_priv, t, boot_debug_timer);
  4811. if (!pci_priv)
  4812. return;
  4813. if (cnss_pci_check_link_status(pci_priv))
  4814. return;
  4815. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  4816. return;
  4817. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  4818. return;
  4819. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE))
  4820. return;
  4821. cnss_pr_dbg("Dump MHI/PBL/SBL debug data every %ds during MHI power on\n",
  4822. BOOT_DEBUG_TIMEOUT_MS / 1000);
  4823. cnss_mhi_debug_reg_dump(pci_priv);
  4824. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4825. cnss_pci_dump_bl_sram_mem(pci_priv);
  4826. mod_timer(&pci_priv->boot_debug_timer,
  4827. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  4828. }
  4829. static int cnss_pci_handle_mhi_sys_err(struct cnss_pci_data *pci_priv)
  4830. {
  4831. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4832. cnss_ignore_qmi_failure(true);
  4833. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4834. del_timer(&plat_priv->fw_boot_timer);
  4835. mod_timer(&pci_priv->dev_rddm_timer,
  4836. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4837. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4838. return 0;
  4839. }
  4840. int cnss_pci_handle_dev_sol_irq(struct cnss_pci_data *pci_priv)
  4841. {
  4842. return cnss_pci_handle_mhi_sys_err(pci_priv);
  4843. }
  4844. static void cnss_mhi_notify_status(struct mhi_controller *mhi_ctrl,
  4845. enum mhi_callback reason)
  4846. {
  4847. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4848. struct cnss_plat_data *plat_priv;
  4849. enum cnss_recovery_reason cnss_reason;
  4850. if (!pci_priv) {
  4851. cnss_pr_err("pci_priv is NULL");
  4852. return;
  4853. }
  4854. plat_priv = pci_priv->plat_priv;
  4855. if (reason != MHI_CB_IDLE)
  4856. cnss_pr_dbg("MHI status cb is called with reason %s(%d)\n",
  4857. cnss_mhi_notify_status_to_str(reason), reason);
  4858. switch (reason) {
  4859. case MHI_CB_IDLE:
  4860. case MHI_CB_EE_MISSION_MODE:
  4861. return;
  4862. case MHI_CB_FATAL_ERROR:
  4863. cnss_ignore_qmi_failure(true);
  4864. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4865. del_timer(&plat_priv->fw_boot_timer);
  4866. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4867. cnss_reason = CNSS_REASON_DEFAULT;
  4868. break;
  4869. case MHI_CB_SYS_ERROR:
  4870. cnss_pci_handle_mhi_sys_err(pci_priv);
  4871. return;
  4872. case MHI_CB_EE_RDDM:
  4873. cnss_ignore_qmi_failure(true);
  4874. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4875. del_timer(&plat_priv->fw_boot_timer);
  4876. del_timer(&pci_priv->dev_rddm_timer);
  4877. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4878. cnss_reason = CNSS_REASON_RDDM;
  4879. break;
  4880. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4881. case MHI_CB_FALLBACK_IMG:
  4882. plat_priv->use_fw_path_with_prefix = false;
  4883. cnss_pci_update_fw_name(pci_priv);
  4884. return;
  4885. #endif
  4886. default:
  4887. cnss_pr_err("Unsupported MHI status cb reason: %d\n", reason);
  4888. return;
  4889. }
  4890. cnss_schedule_recovery(&pci_priv->pci_dev->dev, cnss_reason);
  4891. }
  4892. static int cnss_pci_get_mhi_msi(struct cnss_pci_data *pci_priv)
  4893. {
  4894. int ret, num_vectors, i;
  4895. u32 user_base_data, base_vector;
  4896. int *irq;
  4897. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4898. MHI_MSI_NAME, &num_vectors,
  4899. &user_base_data, &base_vector);
  4900. if (ret)
  4901. return ret;
  4902. cnss_pr_dbg("Number of assigned MSI for MHI is %d, base vector is %d\n",
  4903. num_vectors, base_vector);
  4904. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  4905. if (!irq)
  4906. return -ENOMEM;
  4907. for (i = 0; i < num_vectors; i++)
  4908. irq[i] = cnss_get_msi_irq(&pci_priv->pci_dev->dev,
  4909. base_vector + i);
  4910. pci_priv->mhi_ctrl->irq = irq;
  4911. pci_priv->mhi_ctrl->nr_irqs = num_vectors;
  4912. return 0;
  4913. }
  4914. static int cnss_mhi_bw_scale(struct mhi_controller *mhi_ctrl,
  4915. struct mhi_link_info *link_info)
  4916. {
  4917. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4918. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4919. int ret = 0;
  4920. cnss_pr_dbg("Setting link speed:0x%x, width:0x%x\n",
  4921. link_info->target_link_speed,
  4922. link_info->target_link_width);
  4923. /* It has to set target link speed here before setting link bandwidth
  4924. * when device requests link speed change. This can avoid setting link
  4925. * bandwidth getting rejected if requested link speed is higher than
  4926. * current one.
  4927. */
  4928. ret = cnss_pci_set_max_link_speed(pci_priv, plat_priv->rc_num,
  4929. link_info->target_link_speed);
  4930. if (ret)
  4931. cnss_pr_err("Failed to set target link speed to 0x%x, err = %d\n",
  4932. link_info->target_link_speed, ret);
  4933. ret = cnss_pci_set_link_bandwidth(pci_priv,
  4934. link_info->target_link_speed,
  4935. link_info->target_link_width);
  4936. if (ret) {
  4937. cnss_pr_err("Failed to set link bandwidth, err = %d\n", ret);
  4938. return ret;
  4939. }
  4940. pci_priv->def_link_speed = link_info->target_link_speed;
  4941. pci_priv->def_link_width = link_info->target_link_width;
  4942. return 0;
  4943. }
  4944. static int cnss_mhi_read_reg(struct mhi_controller *mhi_ctrl,
  4945. void __iomem *addr, u32 *out)
  4946. {
  4947. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4948. u32 tmp = readl_relaxed(addr);
  4949. /* Unexpected value, query the link status */
  4950. if (PCI_INVALID_READ(tmp) &&
  4951. cnss_pci_check_link_status(pci_priv))
  4952. return -EIO;
  4953. *out = tmp;
  4954. return 0;
  4955. }
  4956. static void cnss_mhi_write_reg(struct mhi_controller *mhi_ctrl,
  4957. void __iomem *addr, u32 val)
  4958. {
  4959. writel_relaxed(val, addr);
  4960. }
  4961. static int cnss_pci_register_mhi(struct cnss_pci_data *pci_priv)
  4962. {
  4963. int ret = 0;
  4964. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4965. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4966. struct mhi_controller *mhi_ctrl;
  4967. phys_addr_t bar_start;
  4968. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4969. return 0;
  4970. mhi_ctrl = mhi_alloc_controller();
  4971. if (!mhi_ctrl) {
  4972. cnss_pr_err("Invalid MHI controller context\n");
  4973. return -EINVAL;
  4974. }
  4975. pci_priv->mhi_ctrl = mhi_ctrl;
  4976. mhi_ctrl->cntrl_dev = &pci_dev->dev;
  4977. mhi_ctrl->fw_image = plat_priv->firmware_name;
  4978. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4979. mhi_ctrl->fallback_fw_image = plat_priv->fw_fallback_name;
  4980. #endif
  4981. mhi_ctrl->regs = pci_priv->bar;
  4982. mhi_ctrl->reg_len = pci_resource_len(pci_priv->pci_dev, PCI_BAR_NUM);
  4983. bar_start = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4984. cnss_pr_dbg("BAR starts at %pa, length is %x\n",
  4985. &bar_start, mhi_ctrl->reg_len);
  4986. ret = cnss_pci_get_mhi_msi(pci_priv);
  4987. if (ret) {
  4988. cnss_pr_err("Failed to get MSI for MHI, err = %d\n", ret);
  4989. goto free_mhi_ctrl;
  4990. }
  4991. if (pci_priv->smmu_s1_enable) {
  4992. mhi_ctrl->iova_start = pci_priv->smmu_iova_start;
  4993. mhi_ctrl->iova_stop = pci_priv->smmu_iova_start +
  4994. pci_priv->smmu_iova_len;
  4995. } else {
  4996. mhi_ctrl->iova_start = 0;
  4997. mhi_ctrl->iova_stop = pci_priv->dma_bit_mask;
  4998. }
  4999. mhi_ctrl->status_cb = cnss_mhi_notify_status;
  5000. mhi_ctrl->runtime_get = cnss_mhi_pm_runtime_get;
  5001. mhi_ctrl->runtime_put = cnss_mhi_pm_runtime_put_noidle;
  5002. mhi_ctrl->read_reg = cnss_mhi_read_reg;
  5003. mhi_ctrl->write_reg = cnss_mhi_write_reg;
  5004. mhi_ctrl->rddm_size = pci_priv->plat_priv->ramdump_info_v2.ramdump_size;
  5005. if (!mhi_ctrl->rddm_size)
  5006. mhi_ctrl->rddm_size = RAMDUMP_SIZE_DEFAULT;
  5007. mhi_ctrl->sbl_size = SZ_512K;
  5008. mhi_ctrl->seg_len = SZ_512K;
  5009. mhi_ctrl->fbc_download = true;
  5010. ret = mhi_register_controller(mhi_ctrl, &cnss_mhi_config);
  5011. if (ret) {
  5012. cnss_pr_err("Failed to register to MHI bus, err = %d\n", ret);
  5013. goto free_mhi_irq;
  5014. }
  5015. /* MHI satellite driver only needs to connect when DRV is supported */
  5016. if (cnss_pci_is_drv_supported(pci_priv))
  5017. cnss_mhi_controller_set_base(pci_priv, bar_start);
  5018. /* BW scale CB needs to be set after registering MHI per requirement */
  5019. cnss_mhi_controller_set_bw_scale_cb(pci_priv, cnss_mhi_bw_scale);
  5020. ret = cnss_pci_update_fw_name(pci_priv);
  5021. if (ret)
  5022. goto unreg_mhi;
  5023. return 0;
  5024. unreg_mhi:
  5025. mhi_unregister_controller(mhi_ctrl);
  5026. free_mhi_irq:
  5027. kfree(mhi_ctrl->irq);
  5028. free_mhi_ctrl:
  5029. mhi_free_controller(mhi_ctrl);
  5030. return ret;
  5031. }
  5032. static void cnss_pci_unregister_mhi(struct cnss_pci_data *pci_priv)
  5033. {
  5034. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  5035. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  5036. return;
  5037. mhi_unregister_controller(mhi_ctrl);
  5038. kfree(mhi_ctrl->irq);
  5039. mhi_free_controller(mhi_ctrl);
  5040. }
  5041. static void cnss_pci_config_regs(struct cnss_pci_data *pci_priv)
  5042. {
  5043. switch (pci_priv->device_id) {
  5044. case QCA6390_DEVICE_ID:
  5045. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6390;
  5046. pci_priv->wcss_reg = wcss_reg_access_seq;
  5047. pci_priv->pcie_reg = pcie_reg_access_seq;
  5048. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5049. pci_priv->syspm_reg = syspm_reg_access_seq;
  5050. /* Configure WDOG register with specific value so that we can
  5051. * know if HW is in the process of WDOG reset recovery or not
  5052. * when reading the registers.
  5053. */
  5054. cnss_pci_reg_write
  5055. (pci_priv,
  5056. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG,
  5057. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG_VAL);
  5058. break;
  5059. case QCA6490_DEVICE_ID:
  5060. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6490;
  5061. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5062. break;
  5063. default:
  5064. return;
  5065. }
  5066. }
  5067. #if !IS_ENABLED(CONFIG_ARCH_QCOM)
  5068. static irqreturn_t cnss_pci_wake_handler(int irq, void *data)
  5069. {
  5070. struct cnss_pci_data *pci_priv = data;
  5071. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5072. enum rpm_status status;
  5073. struct device *dev;
  5074. pci_priv->wake_counter++;
  5075. cnss_pr_dbg("WLAN PCI wake IRQ (%u) is asserted #%u\n",
  5076. pci_priv->wake_irq, pci_priv->wake_counter);
  5077. /* Make sure abort current suspend */
  5078. cnss_pm_stay_awake(plat_priv);
  5079. cnss_pm_relax(plat_priv);
  5080. /* Above two pm* API calls will abort system suspend only when
  5081. * plat_dev->dev->ws is initiated by device_init_wakeup() API, and
  5082. * calling pm_system_wakeup() is just to guarantee system suspend
  5083. * can be aborted if it is not initiated in any case.
  5084. */
  5085. pm_system_wakeup();
  5086. dev = &pci_priv->pci_dev->dev;
  5087. status = dev->power.runtime_status;
  5088. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  5089. cnss_pci_get_auto_suspended(pci_priv)) ||
  5090. (status == RPM_SUSPENDING || status == RPM_SUSPENDED)) {
  5091. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  5092. cnss_pci_pm_request_resume(pci_priv);
  5093. }
  5094. return IRQ_HANDLED;
  5095. }
  5096. /**
  5097. * cnss_pci_wake_gpio_init() - Setup PCI wake GPIO for WLAN
  5098. * @pci_priv: driver PCI bus context pointer
  5099. *
  5100. * This function initializes WLAN PCI wake GPIO and corresponding
  5101. * interrupt. It should be used in non-MSM platforms whose PCIe
  5102. * root complex driver doesn't handle the GPIO.
  5103. *
  5104. * Return: 0 for success or skip, negative value for error
  5105. */
  5106. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  5107. {
  5108. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5109. struct device *dev = &plat_priv->plat_dev->dev;
  5110. int ret = 0;
  5111. pci_priv->wake_gpio = of_get_named_gpio(dev->of_node,
  5112. "wlan-pci-wake-gpio", 0);
  5113. if (pci_priv->wake_gpio < 0)
  5114. goto out;
  5115. cnss_pr_dbg("Get PCI wake GPIO (%d) from device node\n",
  5116. pci_priv->wake_gpio);
  5117. ret = gpio_request(pci_priv->wake_gpio, "wlan_pci_wake_gpio");
  5118. if (ret) {
  5119. cnss_pr_err("Failed to request PCI wake GPIO, err = %d\n",
  5120. ret);
  5121. goto out;
  5122. }
  5123. gpio_direction_input(pci_priv->wake_gpio);
  5124. pci_priv->wake_irq = gpio_to_irq(pci_priv->wake_gpio);
  5125. ret = request_irq(pci_priv->wake_irq, cnss_pci_wake_handler,
  5126. IRQF_TRIGGER_FALLING, "wlan_pci_wake_irq", pci_priv);
  5127. if (ret) {
  5128. cnss_pr_err("Failed to request PCI wake IRQ, err = %d\n", ret);
  5129. goto free_gpio;
  5130. }
  5131. ret = enable_irq_wake(pci_priv->wake_irq);
  5132. if (ret) {
  5133. cnss_pr_err("Failed to enable PCI wake IRQ, err = %d\n", ret);
  5134. goto free_irq;
  5135. }
  5136. return 0;
  5137. free_irq:
  5138. free_irq(pci_priv->wake_irq, pci_priv);
  5139. free_gpio:
  5140. gpio_free(pci_priv->wake_gpio);
  5141. out:
  5142. return ret;
  5143. }
  5144. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  5145. {
  5146. if (pci_priv->wake_gpio < 0)
  5147. return;
  5148. disable_irq_wake(pci_priv->wake_irq);
  5149. free_irq(pci_priv->wake_irq, pci_priv);
  5150. gpio_free(pci_priv->wake_gpio);
  5151. }
  5152. #else
  5153. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  5154. {
  5155. return 0;
  5156. }
  5157. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  5158. {
  5159. }
  5160. #endif
  5161. #if IS_ENABLED(CONFIG_ARCH_QCOM)
  5162. /**
  5163. * cnss_pci_of_reserved_mem_device_init() - Assign reserved memory region
  5164. * to given PCI device
  5165. * @pci_priv: driver PCI bus context pointer
  5166. *
  5167. * This function shall call corresponding of_reserved_mem_device* API to
  5168. * assign reserved memory region to PCI device based on where the memory is
  5169. * defined and attached to (platform device of_node or PCI device of_node)
  5170. * in device tree.
  5171. *
  5172. * Return: 0 for success, negative value for error
  5173. */
  5174. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  5175. {
  5176. struct device *dev_pci = &pci_priv->pci_dev->dev;
  5177. int ret;
  5178. /* Use of_reserved_mem_device_init_by_idx() if reserved memory is
  5179. * attached to platform device of_node.
  5180. */
  5181. ret = of_reserved_mem_device_init(dev_pci);
  5182. if (ret)
  5183. cnss_pr_err("Failed to init reserved mem device, err = %d\n",
  5184. ret);
  5185. if (dev_pci->cma_area)
  5186. cnss_pr_dbg("CMA area is %s\n",
  5187. cma_get_name(dev_pci->cma_area));
  5188. return ret;
  5189. }
  5190. #else
  5191. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  5192. {
  5193. return 0;
  5194. }
  5195. #endif
  5196. /* Setting to use this cnss_pm_domain ops will let PM framework override the
  5197. * ops from dev->bus->pm which is pci_dev_pm_ops from pci-driver.c. This ops
  5198. * has to take care everything device driver needed which is currently done
  5199. * from pci_dev_pm_ops.
  5200. */
  5201. static struct dev_pm_domain cnss_pm_domain = {
  5202. .ops = {
  5203. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5204. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5205. cnss_pci_resume_noirq)
  5206. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend,
  5207. cnss_pci_runtime_resume,
  5208. cnss_pci_runtime_idle)
  5209. }
  5210. };
  5211. static int cnss_pci_probe(struct pci_dev *pci_dev,
  5212. const struct pci_device_id *id)
  5213. {
  5214. int ret = 0;
  5215. struct cnss_pci_data *pci_priv;
  5216. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  5217. struct device *dev = &pci_dev->dev;
  5218. cnss_pr_dbg("PCI is probing, vendor ID: 0x%x, device ID: 0x%x\n",
  5219. id->vendor, pci_dev->device);
  5220. pci_priv = devm_kzalloc(dev, sizeof(*pci_priv), GFP_KERNEL);
  5221. if (!pci_priv) {
  5222. ret = -ENOMEM;
  5223. goto out;
  5224. }
  5225. pci_priv->pci_link_state = PCI_LINK_UP;
  5226. pci_priv->plat_priv = plat_priv;
  5227. pci_priv->pci_dev = pci_dev;
  5228. pci_priv->pci_device_id = id;
  5229. pci_priv->device_id = pci_dev->device;
  5230. cnss_set_pci_priv(pci_dev, pci_priv);
  5231. plat_priv->device_id = pci_dev->device;
  5232. plat_priv->bus_priv = pci_priv;
  5233. mutex_init(&pci_priv->bus_lock);
  5234. if (plat_priv->use_pm_domain)
  5235. dev->pm_domain = &cnss_pm_domain;
  5236. cnss_pci_of_reserved_mem_device_init(pci_priv);
  5237. ret = cnss_register_subsys(plat_priv);
  5238. if (ret)
  5239. goto reset_ctx;
  5240. ret = cnss_register_ramdump(plat_priv);
  5241. if (ret)
  5242. goto unregister_subsys;
  5243. ret = cnss_pci_init_smmu(pci_priv);
  5244. if (ret)
  5245. goto unregister_ramdump;
  5246. ret = cnss_reg_pci_event(pci_priv);
  5247. if (ret) {
  5248. cnss_pr_err("Failed to register PCI event, err = %d\n", ret);
  5249. goto deinit_smmu;
  5250. }
  5251. ret = cnss_pci_enable_bus(pci_priv);
  5252. if (ret)
  5253. goto dereg_pci_event;
  5254. ret = cnss_pci_enable_msi(pci_priv);
  5255. if (ret)
  5256. goto disable_bus;
  5257. ret = cnss_pci_register_mhi(pci_priv);
  5258. if (ret)
  5259. goto disable_msi;
  5260. switch (pci_dev->device) {
  5261. case QCA6174_DEVICE_ID:
  5262. pci_read_config_word(pci_dev, QCA6174_REV_ID_OFFSET,
  5263. &pci_priv->revision_id);
  5264. break;
  5265. case QCA6290_DEVICE_ID:
  5266. case QCA6390_DEVICE_ID:
  5267. case QCA6490_DEVICE_ID:
  5268. case KIWI_DEVICE_ID:
  5269. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  5270. timer_setup(&pci_priv->dev_rddm_timer,
  5271. cnss_dev_rddm_timeout_hdlr, 0);
  5272. timer_setup(&pci_priv->boot_debug_timer,
  5273. cnss_boot_debug_timeout_hdlr, 0);
  5274. INIT_DELAYED_WORK(&pci_priv->time_sync_work,
  5275. cnss_pci_time_sync_work_hdlr);
  5276. cnss_pci_get_link_status(pci_priv);
  5277. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, false);
  5278. cnss_pci_wake_gpio_init(pci_priv);
  5279. break;
  5280. default:
  5281. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  5282. pci_dev->device);
  5283. ret = -ENODEV;
  5284. goto unreg_mhi;
  5285. }
  5286. cnss_pci_config_regs(pci_priv);
  5287. if (EMULATION_HW)
  5288. goto out;
  5289. ret = cnss_suspend_pci_link(pci_priv);
  5290. if (ret)
  5291. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  5292. cnss_power_off_device(plat_priv);
  5293. set_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5294. return 0;
  5295. unreg_mhi:
  5296. cnss_pci_unregister_mhi(pci_priv);
  5297. disable_msi:
  5298. cnss_pci_disable_msi(pci_priv);
  5299. disable_bus:
  5300. cnss_pci_disable_bus(pci_priv);
  5301. dereg_pci_event:
  5302. cnss_dereg_pci_event(pci_priv);
  5303. deinit_smmu:
  5304. cnss_pci_deinit_smmu(pci_priv);
  5305. unregister_ramdump:
  5306. cnss_unregister_ramdump(plat_priv);
  5307. unregister_subsys:
  5308. cnss_unregister_subsys(plat_priv);
  5309. reset_ctx:
  5310. plat_priv->bus_priv = NULL;
  5311. out:
  5312. return ret;
  5313. }
  5314. static void cnss_pci_remove(struct pci_dev *pci_dev)
  5315. {
  5316. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5317. struct cnss_plat_data *plat_priv =
  5318. cnss_bus_dev_to_plat_priv(&pci_dev->dev);
  5319. clear_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5320. cnss_pci_free_m3_mem(pci_priv);
  5321. cnss_pci_free_fw_mem(pci_priv);
  5322. cnss_pci_free_qdss_mem(pci_priv);
  5323. switch (pci_dev->device) {
  5324. case QCA6290_DEVICE_ID:
  5325. case QCA6390_DEVICE_ID:
  5326. case QCA6490_DEVICE_ID:
  5327. case KIWI_DEVICE_ID:
  5328. cnss_pci_wake_gpio_deinit(pci_priv);
  5329. del_timer(&pci_priv->boot_debug_timer);
  5330. del_timer(&pci_priv->dev_rddm_timer);
  5331. break;
  5332. default:
  5333. break;
  5334. }
  5335. cnss_pci_unregister_mhi(pci_priv);
  5336. cnss_pci_disable_msi(pci_priv);
  5337. cnss_pci_disable_bus(pci_priv);
  5338. cnss_dereg_pci_event(pci_priv);
  5339. cnss_pci_deinit_smmu(pci_priv);
  5340. if (plat_priv) {
  5341. cnss_unregister_ramdump(plat_priv);
  5342. cnss_unregister_subsys(plat_priv);
  5343. plat_priv->bus_priv = NULL;
  5344. } else {
  5345. cnss_pr_err("Plat_priv is null, Unable to unregister ramdump,subsys\n");
  5346. }
  5347. }
  5348. static const struct pci_device_id cnss_pci_id_table[] = {
  5349. { QCA6174_VENDOR_ID, QCA6174_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5350. { QCA6290_VENDOR_ID, QCA6290_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5351. { QCA6390_VENDOR_ID, QCA6390_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5352. { QCA6490_VENDOR_ID, QCA6490_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5353. { KIWI_VENDOR_ID, KIWI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5354. { 0 }
  5355. };
  5356. MODULE_DEVICE_TABLE(pci, cnss_pci_id_table);
  5357. static const struct dev_pm_ops cnss_pm_ops = {
  5358. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5359. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5360. cnss_pci_resume_noirq)
  5361. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend, cnss_pci_runtime_resume,
  5362. cnss_pci_runtime_idle)
  5363. };
  5364. struct pci_driver cnss_pci_driver = {
  5365. .name = "cnss_pci",
  5366. .id_table = cnss_pci_id_table,
  5367. .probe = cnss_pci_probe,
  5368. .remove = cnss_pci_remove,
  5369. .driver = {
  5370. .pm = &cnss_pm_ops,
  5371. },
  5372. };
  5373. static int cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  5374. {
  5375. int ret, retry = 0;
  5376. /* Always set initial target PCIe link speed to Gen2 for QCA6490 device
  5377. * since there may be link issues if it boots up with Gen3 link speed.
  5378. * Device is able to change it later at any time. It will be rejected
  5379. * if requested speed is higher than the one specified in PCIe DT.
  5380. */
  5381. if (plat_priv->device_id == QCA6490_DEVICE_ID) {
  5382. ret = cnss_pci_set_max_link_speed(plat_priv->bus_priv, rc_num,
  5383. PCI_EXP_LNKSTA_CLS_5_0GB);
  5384. if (ret && ret != -EPROBE_DEFER)
  5385. cnss_pr_err("Failed to set max PCIe RC%x link speed to Gen2, err = %d\n",
  5386. rc_num, ret);
  5387. }
  5388. cnss_pr_dbg("Trying to enumerate with PCIe RC%x\n", rc_num);
  5389. retry:
  5390. ret = _cnss_pci_enumerate(plat_priv, rc_num);
  5391. if (ret) {
  5392. if (ret == -EPROBE_DEFER) {
  5393. cnss_pr_dbg("PCIe RC driver is not ready, defer probe\n");
  5394. goto out;
  5395. }
  5396. cnss_pr_err("Failed to enable PCIe RC%x, err = %d\n",
  5397. rc_num, ret);
  5398. if (retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  5399. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  5400. goto retry;
  5401. } else {
  5402. goto out;
  5403. }
  5404. }
  5405. plat_priv->rc_num = rc_num;
  5406. out:
  5407. return ret;
  5408. }
  5409. int cnss_pci_init(struct cnss_plat_data *plat_priv)
  5410. {
  5411. struct device *dev = &plat_priv->plat_dev->dev;
  5412. const __be32 *prop;
  5413. int ret = 0, prop_len = 0, rc_count, i;
  5414. prop = of_get_property(dev->of_node, "qcom,wlan-rc-num", &prop_len);
  5415. if (!prop || !prop_len) {
  5416. cnss_pr_err("Failed to get PCIe RC number from DT\n");
  5417. goto out;
  5418. }
  5419. rc_count = prop_len / sizeof(__be32);
  5420. for (i = 0; i < rc_count; i++) {
  5421. ret = cnss_pci_enumerate(plat_priv, be32_to_cpup(&prop[i]));
  5422. if (!ret)
  5423. break;
  5424. else if (ret == -EPROBE_DEFER || (ret && i == rc_count - 1))
  5425. goto out;
  5426. }
  5427. ret = pci_register_driver(&cnss_pci_driver);
  5428. if (ret) {
  5429. cnss_pr_err("Failed to register to PCI framework, err = %d\n",
  5430. ret);
  5431. goto out;
  5432. }
  5433. if (!plat_priv->bus_priv) {
  5434. cnss_pr_err("Failed to probe PCI driver\n");
  5435. ret = -ENODEV;
  5436. goto unreg_pci;
  5437. }
  5438. return 0;
  5439. unreg_pci:
  5440. pci_unregister_driver(&cnss_pci_driver);
  5441. out:
  5442. return ret;
  5443. }
  5444. void cnss_pci_deinit(struct cnss_plat_data *plat_priv)
  5445. {
  5446. pci_unregister_driver(&cnss_pci_driver);
  5447. }