pci.c 162 KB

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