pci.c 170 KB

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