pci.c 153 KB

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