pci.c 154 KB

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