pci.c 172 KB

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