pci.c 158 KB

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