pci.c 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467
  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. if (pci_dev->device == QCA6174_DEVICE_ID)
  1456. disable_irq(pci_dev->irq);
  1457. /* Notify bus related event. Now for all supported chips.
  1458. * Here PCIe LINK_DOWN notification taken care.
  1459. * uevent buffer can be extended later, to cover more bus info.
  1460. */
  1461. cnss_pci_update_link_event(pci_priv, BUS_EVENT_PCI_LINK_DOWN, NULL);
  1462. cnss_fatal_err("PCI link down, schedule recovery\n");
  1463. cnss_schedule_recovery(&pci_dev->dev, CNSS_REASON_LINK_DOWN);
  1464. }
  1465. int cnss_pci_link_down(struct device *dev)
  1466. {
  1467. struct pci_dev *pci_dev = to_pci_dev(dev);
  1468. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1469. struct cnss_plat_data *plat_priv = NULL;
  1470. int ret;
  1471. if (!pci_priv) {
  1472. cnss_pr_err("pci_priv is NULL\n");
  1473. return -EINVAL;
  1474. }
  1475. plat_priv = pci_priv->plat_priv;
  1476. if (!plat_priv) {
  1477. cnss_pr_err("plat_priv is NULL\n");
  1478. return -ENODEV;
  1479. }
  1480. if (pci_priv->pci_link_down_ind) {
  1481. cnss_pr_dbg("PCI link down recovery is already in progress\n");
  1482. return -EBUSY;
  1483. }
  1484. if (pci_priv->drv_connected_last &&
  1485. of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  1486. "cnss-enable-self-recovery"))
  1487. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  1488. cnss_pr_err("PCI link down is detected by drivers\n");
  1489. ret = cnss_pci_assert_perst(pci_priv);
  1490. if (ret)
  1491. cnss_pci_handle_linkdown(pci_priv);
  1492. return ret;
  1493. }
  1494. EXPORT_SYMBOL(cnss_pci_link_down);
  1495. int cnss_pcie_is_device_down(struct cnss_pci_data *pci_priv)
  1496. {
  1497. struct cnss_plat_data *plat_priv;
  1498. if (!pci_priv) {
  1499. cnss_pr_err("pci_priv is NULL\n");
  1500. return -ENODEV;
  1501. }
  1502. plat_priv = pci_priv->plat_priv;
  1503. if (!plat_priv) {
  1504. cnss_pr_err("plat_priv is NULL\n");
  1505. return -ENODEV;
  1506. }
  1507. return test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) |
  1508. pci_priv->pci_link_down_ind;
  1509. }
  1510. int cnss_pci_is_device_down(struct device *dev)
  1511. {
  1512. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  1513. return cnss_pcie_is_device_down(pci_priv);
  1514. }
  1515. EXPORT_SYMBOL(cnss_pci_is_device_down);
  1516. void cnss_pci_lock_reg_window(struct device *dev, unsigned long *flags)
  1517. {
  1518. spin_lock_bh(&pci_reg_window_lock);
  1519. }
  1520. EXPORT_SYMBOL(cnss_pci_lock_reg_window);
  1521. void cnss_pci_unlock_reg_window(struct device *dev, unsigned long *flags)
  1522. {
  1523. spin_unlock_bh(&pci_reg_window_lock);
  1524. }
  1525. EXPORT_SYMBOL(cnss_pci_unlock_reg_window);
  1526. int cnss_get_pci_slot(struct device *dev)
  1527. {
  1528. struct pci_dev *pci_dev = to_pci_dev(dev);
  1529. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1530. struct cnss_plat_data *plat_priv = NULL;
  1531. if (!pci_priv) {
  1532. cnss_pr_err("pci_priv is NULL\n");
  1533. return -EINVAL;
  1534. }
  1535. plat_priv = pci_priv->plat_priv;
  1536. if (!plat_priv) {
  1537. cnss_pr_err("plat_priv is NULL\n");
  1538. return -ENODEV;
  1539. }
  1540. return plat_priv->rc_num;
  1541. }
  1542. EXPORT_SYMBOL(cnss_get_pci_slot);
  1543. /**
  1544. * cnss_pci_dump_bl_sram_mem - Dump WLAN device bootloader debug log
  1545. * @pci_priv: driver PCI bus context pointer
  1546. *
  1547. * Dump primary and secondary bootloader debug log data. For SBL check the
  1548. * log struct address and size for validity.
  1549. *
  1550. * Return: None
  1551. */
  1552. static void cnss_pci_dump_bl_sram_mem(struct cnss_pci_data *pci_priv)
  1553. {
  1554. u32 mem_addr, val, pbl_log_max_size, sbl_log_max_size;
  1555. u32 pbl_log_sram_start;
  1556. u32 pbl_stage, sbl_log_start, sbl_log_size;
  1557. u32 pbl_wlan_boot_cfg, pbl_bootstrap_status;
  1558. u32 pbl_bootstrap_status_reg = PBL_BOOTSTRAP_STATUS;
  1559. u32 sbl_log_def_start = SRAM_START;
  1560. u32 sbl_log_def_end = SRAM_END;
  1561. int i;
  1562. switch (pci_priv->device_id) {
  1563. case QCA6390_DEVICE_ID:
  1564. pbl_log_sram_start = QCA6390_DEBUG_PBL_LOG_SRAM_START;
  1565. pbl_log_max_size = QCA6390_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1566. sbl_log_max_size = QCA6390_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1567. break;
  1568. case QCA6490_DEVICE_ID:
  1569. pbl_log_sram_start = QCA6490_DEBUG_PBL_LOG_SRAM_START;
  1570. pbl_log_max_size = QCA6490_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1571. sbl_log_max_size = QCA6490_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1572. break;
  1573. case KIWI_DEVICE_ID:
  1574. pbl_bootstrap_status_reg = KIWI_PBL_BOOTSTRAP_STATUS;
  1575. pbl_log_sram_start = KIWI_DEBUG_PBL_LOG_SRAM_START;
  1576. pbl_log_max_size = KIWI_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1577. sbl_log_max_size = KIWI_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1578. default:
  1579. return;
  1580. }
  1581. if (cnss_pci_check_link_status(pci_priv))
  1582. return;
  1583. cnss_pci_reg_read(pci_priv, TCSR_PBL_LOGGING_REG, &pbl_stage);
  1584. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG2_REG, &sbl_log_start);
  1585. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG3_REG, &sbl_log_size);
  1586. cnss_pci_reg_read(pci_priv, PBL_WLAN_BOOT_CFG, &pbl_wlan_boot_cfg);
  1587. cnss_pci_reg_read(pci_priv, pbl_bootstrap_status_reg,
  1588. &pbl_bootstrap_status);
  1589. cnss_pr_dbg("TCSR_PBL_LOGGING: 0x%08x PCIE_BHI_ERRDBG: Start: 0x%08x Size:0x%08x\n",
  1590. pbl_stage, sbl_log_start, sbl_log_size);
  1591. cnss_pr_dbg("PBL_WLAN_BOOT_CFG: 0x%08x PBL_BOOTSTRAP_STATUS: 0x%08x\n",
  1592. pbl_wlan_boot_cfg, pbl_bootstrap_status);
  1593. cnss_pr_dbg("Dumping PBL log data\n");
  1594. for (i = 0; i < pbl_log_max_size; i += sizeof(val)) {
  1595. mem_addr = pbl_log_sram_start + i;
  1596. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1597. break;
  1598. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1599. }
  1600. sbl_log_size = (sbl_log_size > sbl_log_max_size ?
  1601. sbl_log_max_size : sbl_log_size);
  1602. if (sbl_log_start < sbl_log_def_start ||
  1603. sbl_log_start > sbl_log_def_end ||
  1604. (sbl_log_start + sbl_log_size) > sbl_log_def_end) {
  1605. cnss_pr_err("Invalid SBL log data\n");
  1606. return;
  1607. }
  1608. cnss_pr_dbg("Dumping SBL log data\n");
  1609. for (i = 0; i < sbl_log_size; i += sizeof(val)) {
  1610. mem_addr = sbl_log_start + i;
  1611. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1612. break;
  1613. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1614. }
  1615. }
  1616. static int cnss_pci_handle_mhi_poweron_timeout(struct cnss_pci_data *pci_priv)
  1617. {
  1618. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1619. cnss_fatal_err("MHI power up returns timeout\n");
  1620. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE) ||
  1621. cnss_get_dev_sol_value(plat_priv) > 0) {
  1622. /* Wait for RDDM if RDDM cookie is set or device SOL GPIO is
  1623. * high. If RDDM times out, PBL/SBL error region may have been
  1624. * erased so no need to dump them either.
  1625. */
  1626. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  1627. !pci_priv->pci_link_down_ind) {
  1628. mod_timer(&pci_priv->dev_rddm_timer,
  1629. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1630. }
  1631. } else {
  1632. cnss_pr_dbg("RDDM cookie is not set and device SOL is low\n");
  1633. cnss_mhi_debug_reg_dump(pci_priv);
  1634. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1635. /* Dump PBL/SBL error log if RDDM cookie is not set */
  1636. cnss_pci_dump_bl_sram_mem(pci_priv);
  1637. return -ETIMEDOUT;
  1638. }
  1639. return 0;
  1640. }
  1641. static char *cnss_mhi_state_to_str(enum cnss_mhi_state mhi_state)
  1642. {
  1643. switch (mhi_state) {
  1644. case CNSS_MHI_INIT:
  1645. return "INIT";
  1646. case CNSS_MHI_DEINIT:
  1647. return "DEINIT";
  1648. case CNSS_MHI_POWER_ON:
  1649. return "POWER_ON";
  1650. case CNSS_MHI_POWERING_OFF:
  1651. return "POWERING_OFF";
  1652. case CNSS_MHI_POWER_OFF:
  1653. return "POWER_OFF";
  1654. case CNSS_MHI_FORCE_POWER_OFF:
  1655. return "FORCE_POWER_OFF";
  1656. case CNSS_MHI_SUSPEND:
  1657. return "SUSPEND";
  1658. case CNSS_MHI_RESUME:
  1659. return "RESUME";
  1660. case CNSS_MHI_TRIGGER_RDDM:
  1661. return "TRIGGER_RDDM";
  1662. case CNSS_MHI_RDDM_DONE:
  1663. return "RDDM_DONE";
  1664. default:
  1665. return "UNKNOWN";
  1666. }
  1667. };
  1668. static int cnss_pci_check_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1669. enum cnss_mhi_state mhi_state)
  1670. {
  1671. switch (mhi_state) {
  1672. case CNSS_MHI_INIT:
  1673. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state))
  1674. return 0;
  1675. break;
  1676. case CNSS_MHI_DEINIT:
  1677. case CNSS_MHI_POWER_ON:
  1678. if (test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state) &&
  1679. !test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1680. return 0;
  1681. break;
  1682. case CNSS_MHI_FORCE_POWER_OFF:
  1683. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1684. return 0;
  1685. break;
  1686. case CNSS_MHI_POWER_OFF:
  1687. case CNSS_MHI_SUSPEND:
  1688. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1689. !test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1690. return 0;
  1691. break;
  1692. case CNSS_MHI_RESUME:
  1693. if (test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1694. return 0;
  1695. break;
  1696. case CNSS_MHI_TRIGGER_RDDM:
  1697. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1698. !test_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state))
  1699. return 0;
  1700. break;
  1701. case CNSS_MHI_RDDM_DONE:
  1702. return 0;
  1703. default:
  1704. cnss_pr_err("Unhandled MHI state: %s(%d)\n",
  1705. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1706. }
  1707. cnss_pr_err("Cannot set MHI state %s(%d) in current MHI state (0x%lx)\n",
  1708. cnss_mhi_state_to_str(mhi_state), mhi_state,
  1709. pci_priv->mhi_state);
  1710. if (mhi_state != CNSS_MHI_TRIGGER_RDDM)
  1711. CNSS_ASSERT(0);
  1712. return -EINVAL;
  1713. }
  1714. static void cnss_pci_set_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1715. enum cnss_mhi_state mhi_state)
  1716. {
  1717. switch (mhi_state) {
  1718. case CNSS_MHI_INIT:
  1719. set_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1720. break;
  1721. case CNSS_MHI_DEINIT:
  1722. clear_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1723. break;
  1724. case CNSS_MHI_POWER_ON:
  1725. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1726. break;
  1727. case CNSS_MHI_POWERING_OFF:
  1728. set_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1729. break;
  1730. case CNSS_MHI_POWER_OFF:
  1731. case CNSS_MHI_FORCE_POWER_OFF:
  1732. clear_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1733. clear_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1734. clear_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1735. clear_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1736. break;
  1737. case CNSS_MHI_SUSPEND:
  1738. set_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1739. break;
  1740. case CNSS_MHI_RESUME:
  1741. clear_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1742. break;
  1743. case CNSS_MHI_TRIGGER_RDDM:
  1744. set_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1745. break;
  1746. case CNSS_MHI_RDDM_DONE:
  1747. set_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1748. break;
  1749. default:
  1750. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1751. }
  1752. }
  1753. static int cnss_pci_set_mhi_state(struct cnss_pci_data *pci_priv,
  1754. enum cnss_mhi_state mhi_state)
  1755. {
  1756. int ret = 0, retry = 0;
  1757. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  1758. return 0;
  1759. if (mhi_state < 0) {
  1760. cnss_pr_err("Invalid MHI state (%d)\n", mhi_state);
  1761. return -EINVAL;
  1762. }
  1763. ret = cnss_pci_check_mhi_state_bit(pci_priv, mhi_state);
  1764. if (ret)
  1765. goto out;
  1766. cnss_pr_vdbg("Setting MHI state: %s(%d)\n",
  1767. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1768. switch (mhi_state) {
  1769. case CNSS_MHI_INIT:
  1770. ret = mhi_prepare_for_power_up(pci_priv->mhi_ctrl);
  1771. break;
  1772. case CNSS_MHI_DEINIT:
  1773. mhi_unprepare_after_power_down(pci_priv->mhi_ctrl);
  1774. ret = 0;
  1775. break;
  1776. case CNSS_MHI_POWER_ON:
  1777. ret = mhi_sync_power_up(pci_priv->mhi_ctrl);
  1778. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  1779. /* Only set img_pre_alloc when power up succeeds */
  1780. if (!ret && !pci_priv->mhi_ctrl->img_pre_alloc) {
  1781. cnss_pr_dbg("Notify MHI to use already allocated images\n");
  1782. pci_priv->mhi_ctrl->img_pre_alloc = true;
  1783. }
  1784. #endif
  1785. break;
  1786. case CNSS_MHI_POWER_OFF:
  1787. mhi_power_down(pci_priv->mhi_ctrl, true);
  1788. ret = 0;
  1789. break;
  1790. case CNSS_MHI_FORCE_POWER_OFF:
  1791. mhi_power_down(pci_priv->mhi_ctrl, false);
  1792. ret = 0;
  1793. break;
  1794. case CNSS_MHI_SUSPEND:
  1795. retry_mhi_suspend:
  1796. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1797. if (pci_priv->drv_connected_last)
  1798. ret = cnss_mhi_pm_fast_suspend(pci_priv, true);
  1799. else
  1800. ret = mhi_pm_suspend(pci_priv->mhi_ctrl);
  1801. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1802. if (ret == -EBUSY && retry++ < MHI_SUSPEND_RETRY_MAX_TIMES) {
  1803. cnss_pr_dbg("Retry MHI suspend #%d\n", retry);
  1804. usleep_range(MHI_SUSPEND_RETRY_DELAY_US,
  1805. MHI_SUSPEND_RETRY_DELAY_US + 1000);
  1806. goto retry_mhi_suspend;
  1807. }
  1808. break;
  1809. case CNSS_MHI_RESUME:
  1810. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1811. if (pci_priv->drv_connected_last) {
  1812. ret = cnss_pci_prevent_l1(&pci_priv->pci_dev->dev);
  1813. if (ret) {
  1814. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1815. break;
  1816. }
  1817. ret = cnss_mhi_pm_fast_resume(pci_priv, true);
  1818. cnss_pci_allow_l1(&pci_priv->pci_dev->dev);
  1819. } else {
  1820. ret = mhi_pm_resume(pci_priv->mhi_ctrl);
  1821. }
  1822. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1823. break;
  1824. case CNSS_MHI_TRIGGER_RDDM:
  1825. ret = mhi_force_rddm_mode(pci_priv->mhi_ctrl);
  1826. if (ret) {
  1827. cnss_pr_err("Failed to trigger RDDM, err = %d\n", ret);
  1828. cnss_pr_dbg("Sending host reset req\n");
  1829. ret = cnss_mhi_force_reset(pci_priv);
  1830. }
  1831. break;
  1832. case CNSS_MHI_RDDM_DONE:
  1833. break;
  1834. default:
  1835. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1836. ret = -EINVAL;
  1837. }
  1838. if (ret)
  1839. goto out;
  1840. cnss_pci_set_mhi_state_bit(pci_priv, mhi_state);
  1841. return 0;
  1842. out:
  1843. cnss_pr_err("Failed to set MHI state: %s(%d), err = %d\n",
  1844. cnss_mhi_state_to_str(mhi_state), mhi_state, ret);
  1845. return ret;
  1846. }
  1847. #if IS_ENABLED(CONFIG_PCI_MSM)
  1848. /**
  1849. * cnss_wlan_adsp_pc_enable: Control ADSP power collapse setup
  1850. * @dev: Platform driver pci private data structure
  1851. * @control: Power collapse enable / disable
  1852. *
  1853. * This function controls ADSP power collapse (PC). It must be called
  1854. * based on wlan state. ADSP power collapse during wlan RTPM suspend state
  1855. * results in delay during periodic QMI stats PCI link up/down. This delay
  1856. * causes additional power consumption.
  1857. * Introduced in SM8350.
  1858. *
  1859. * Result: 0 Success. negative error codes.
  1860. */
  1861. static int cnss_wlan_adsp_pc_enable(struct cnss_pci_data *pci_priv,
  1862. bool control)
  1863. {
  1864. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1865. int ret = 0;
  1866. u32 pm_options = PM_OPTIONS_DEFAULT;
  1867. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1868. if (plat_priv->adsp_pc_enabled == control) {
  1869. cnss_pr_dbg("ADSP power collapse already %s\n",
  1870. control ? "Enabled" : "Disabled");
  1871. return 0;
  1872. }
  1873. if (control)
  1874. pm_options &= ~MSM_PCIE_CONFIG_NO_DRV_PC;
  1875. else
  1876. pm_options |= MSM_PCIE_CONFIG_NO_DRV_PC;
  1877. ret = msm_pcie_pm_control(MSM_PCIE_DRV_PC_CTRL, pci_dev->bus->number,
  1878. pci_dev, NULL, pm_options);
  1879. if (ret)
  1880. return ret;
  1881. cnss_pr_dbg("%s ADSP power collapse\n", control ? "Enable" : "Disable");
  1882. plat_priv->adsp_pc_enabled = control;
  1883. return 0;
  1884. }
  1885. #else
  1886. static int cnss_wlan_adsp_pc_enable(struct cnss_pci_data *pci_priv,
  1887. bool control)
  1888. {
  1889. return 0;
  1890. }
  1891. #endif
  1892. int cnss_pci_start_mhi(struct cnss_pci_data *pci_priv)
  1893. {
  1894. int ret = 0;
  1895. struct cnss_plat_data *plat_priv;
  1896. unsigned int timeout = 0;
  1897. if (!pci_priv) {
  1898. cnss_pr_err("pci_priv is NULL\n");
  1899. return -ENODEV;
  1900. }
  1901. plat_priv = pci_priv->plat_priv;
  1902. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1903. return 0;
  1904. if (MHI_TIMEOUT_OVERWRITE_MS)
  1905. pci_priv->mhi_ctrl->timeout_ms = MHI_TIMEOUT_OVERWRITE_MS;
  1906. cnss_mhi_set_m2_timeout_ms(pci_priv, MHI_M2_TIMEOUT_MS);
  1907. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_INIT);
  1908. if (ret)
  1909. return ret;
  1910. timeout = pci_priv->mhi_ctrl->timeout_ms;
  1911. /* For non-perf builds the timeout is 10 (default) * 6 seconds */
  1912. if (cnss_get_host_build_type() == QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  1913. pci_priv->mhi_ctrl->timeout_ms *= 6;
  1914. else /* For perf builds the timeout is 10 (default) * 3 seconds */
  1915. pci_priv->mhi_ctrl->timeout_ms *= 3;
  1916. /* Start the timer to dump MHI/PBL/SBL debug data periodically */
  1917. mod_timer(&pci_priv->boot_debug_timer,
  1918. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  1919. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_ON);
  1920. del_timer(&pci_priv->boot_debug_timer);
  1921. if (ret == 0)
  1922. cnss_wlan_adsp_pc_enable(pci_priv, false);
  1923. pci_priv->mhi_ctrl->timeout_ms = timeout;
  1924. if (ret == -ETIMEDOUT) {
  1925. /* This is a special case needs to be handled that if MHI
  1926. * power on returns -ETIMEDOUT, controller needs to take care
  1927. * the cleanup by calling MHI power down. Force to set the bit
  1928. * for driver internal MHI state to make sure it can be handled
  1929. * properly later.
  1930. */
  1931. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1932. ret = cnss_pci_handle_mhi_poweron_timeout(pci_priv);
  1933. }
  1934. return ret;
  1935. }
  1936. static void cnss_pci_power_off_mhi(struct cnss_pci_data *pci_priv)
  1937. {
  1938. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1939. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1940. return;
  1941. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state)) {
  1942. cnss_pr_dbg("MHI is already powered off\n");
  1943. return;
  1944. }
  1945. cnss_wlan_adsp_pc_enable(pci_priv, true);
  1946. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_RESUME);
  1947. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_POWERING_OFF);
  1948. if (!pci_priv->pci_link_down_ind)
  1949. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_OFF);
  1950. else
  1951. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_FORCE_POWER_OFF);
  1952. }
  1953. static void cnss_pci_deinit_mhi(struct cnss_pci_data *pci_priv)
  1954. {
  1955. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1956. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1957. return;
  1958. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state)) {
  1959. cnss_pr_dbg("MHI is already deinited\n");
  1960. return;
  1961. }
  1962. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_DEINIT);
  1963. }
  1964. static void cnss_pci_set_wlaon_pwr_ctrl(struct cnss_pci_data *pci_priv,
  1965. bool set_vddd4blow, bool set_shutdown,
  1966. bool do_force_wake)
  1967. {
  1968. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1969. int ret;
  1970. u32 val;
  1971. if (!plat_priv->set_wlaon_pwr_ctrl)
  1972. return;
  1973. if (pci_priv->pci_link_state == PCI_LINK_DOWN ||
  1974. pci_priv->pci_link_down_ind)
  1975. return;
  1976. if (do_force_wake)
  1977. if (cnss_pci_force_wake_get(pci_priv))
  1978. return;
  1979. ret = cnss_pci_reg_read(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, &val);
  1980. if (ret) {
  1981. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  1982. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1983. goto force_wake_put;
  1984. }
  1985. cnss_pr_dbg("Read register offset 0x%x, val = 0x%x\n",
  1986. WLAON_QFPROM_PWR_CTRL_REG, val);
  1987. if (set_vddd4blow)
  1988. val |= QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1989. else
  1990. val &= ~QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1991. if (set_shutdown)
  1992. val |= QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1993. else
  1994. val &= ~QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1995. ret = cnss_pci_reg_write(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, val);
  1996. if (ret) {
  1997. cnss_pr_err("Failed to write register offset 0x%x, err = %d\n",
  1998. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1999. goto force_wake_put;
  2000. }
  2001. cnss_pr_dbg("Write val 0x%x to register offset 0x%x\n", val,
  2002. WLAON_QFPROM_PWR_CTRL_REG);
  2003. if (set_shutdown)
  2004. usleep_range(WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US,
  2005. WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US);
  2006. force_wake_put:
  2007. if (do_force_wake)
  2008. cnss_pci_force_wake_put(pci_priv);
  2009. }
  2010. static int cnss_pci_get_device_timestamp(struct cnss_pci_data *pci_priv,
  2011. u64 *time_us)
  2012. {
  2013. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2014. u32 low, high;
  2015. u64 device_ticks;
  2016. if (!plat_priv->device_freq_hz) {
  2017. cnss_pr_err("Device time clock frequency is not valid\n");
  2018. return -EINVAL;
  2019. }
  2020. switch (pci_priv->device_id) {
  2021. case KIWI_DEVICE_ID:
  2022. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_LOW, &low);
  2023. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_HIGH, &high);
  2024. break;
  2025. default:
  2026. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL3, &low);
  2027. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL4, &high);
  2028. break;
  2029. }
  2030. device_ticks = (u64)high << 32 | low;
  2031. do_div(device_ticks, plat_priv->device_freq_hz / 100000);
  2032. *time_us = device_ticks * 10;
  2033. return 0;
  2034. }
  2035. static void cnss_pci_enable_time_sync_counter(struct cnss_pci_data *pci_priv)
  2036. {
  2037. switch (pci_priv->device_id) {
  2038. case KIWI_DEVICE_ID:
  2039. return;
  2040. default:
  2041. break;
  2042. }
  2043. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  2044. TIME_SYNC_ENABLE);
  2045. }
  2046. static void cnss_pci_clear_time_sync_counter(struct cnss_pci_data *pci_priv)
  2047. {
  2048. switch (pci_priv->device_id) {
  2049. case KIWI_DEVICE_ID:
  2050. return;
  2051. default:
  2052. break;
  2053. }
  2054. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  2055. TIME_SYNC_CLEAR);
  2056. }
  2057. static void cnss_pci_time_sync_reg_update(struct cnss_pci_data *pci_priv,
  2058. u32 low, u32 high)
  2059. {
  2060. u32 time_reg_low = PCIE_SHADOW_REG_VALUE_0;
  2061. u32 time_reg_high = PCIE_SHADOW_REG_VALUE_1;
  2062. switch (pci_priv->device_id) {
  2063. case KIWI_DEVICE_ID:
  2064. /* Forward compatibility */
  2065. break;
  2066. default:
  2067. time_reg_low = PCIE_SHADOW_REG_VALUE_34;
  2068. time_reg_high = PCIE_SHADOW_REG_VALUE_35;
  2069. break;
  2070. }
  2071. cnss_pci_reg_write(pci_priv, time_reg_low, low);
  2072. cnss_pci_reg_write(pci_priv, time_reg_high, high);
  2073. cnss_pci_reg_read(pci_priv, time_reg_low, &low);
  2074. cnss_pci_reg_read(pci_priv, time_reg_high, &high);
  2075. cnss_pr_dbg("Updated time sync regs [0x%x] = 0x%x, [0x%x] = 0x%x\n",
  2076. time_reg_low, low, time_reg_high, high);
  2077. }
  2078. static int cnss_pci_update_timestamp(struct cnss_pci_data *pci_priv)
  2079. {
  2080. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2081. struct device *dev = &pci_priv->pci_dev->dev;
  2082. unsigned long flags = 0;
  2083. u64 host_time_us, device_time_us, offset;
  2084. u32 low, high;
  2085. int ret;
  2086. ret = cnss_pci_prevent_l1(dev);
  2087. if (ret)
  2088. goto out;
  2089. ret = cnss_pci_force_wake_get(pci_priv);
  2090. if (ret)
  2091. goto allow_l1;
  2092. spin_lock_irqsave(&time_sync_lock, flags);
  2093. cnss_pci_clear_time_sync_counter(pci_priv);
  2094. cnss_pci_enable_time_sync_counter(pci_priv);
  2095. host_time_us = cnss_get_host_timestamp(plat_priv);
  2096. ret = cnss_pci_get_device_timestamp(pci_priv, &device_time_us);
  2097. cnss_pci_clear_time_sync_counter(pci_priv);
  2098. spin_unlock_irqrestore(&time_sync_lock, flags);
  2099. if (ret)
  2100. goto force_wake_put;
  2101. if (host_time_us < device_time_us) {
  2102. cnss_pr_err("Host time (%llu us) is smaller than device time (%llu us), stop\n",
  2103. host_time_us, device_time_us);
  2104. ret = -EINVAL;
  2105. goto force_wake_put;
  2106. }
  2107. offset = host_time_us - device_time_us;
  2108. cnss_pr_dbg("Host time = %llu us, device time = %llu us, offset = %llu us\n",
  2109. host_time_us, device_time_us, offset);
  2110. low = offset & 0xFFFFFFFF;
  2111. high = offset >> 32;
  2112. cnss_pci_time_sync_reg_update(pci_priv, low, high);
  2113. force_wake_put:
  2114. cnss_pci_force_wake_put(pci_priv);
  2115. allow_l1:
  2116. cnss_pci_allow_l1(dev);
  2117. out:
  2118. return ret;
  2119. }
  2120. static void cnss_pci_time_sync_work_hdlr(struct work_struct *work)
  2121. {
  2122. struct cnss_pci_data *pci_priv =
  2123. container_of(work, struct cnss_pci_data, time_sync_work.work);
  2124. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2125. unsigned int time_sync_period_ms =
  2126. plat_priv->ctrl_params.time_sync_period;
  2127. if (test_bit(DISABLE_TIME_SYNC, &plat_priv->ctrl_params.quirks)) {
  2128. cnss_pr_dbg("Time sync is disabled\n");
  2129. return;
  2130. }
  2131. if (!time_sync_period_ms) {
  2132. cnss_pr_dbg("Skip time sync as time period is 0\n");
  2133. return;
  2134. }
  2135. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  2136. return;
  2137. if (cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS) < 0)
  2138. goto runtime_pm_put;
  2139. mutex_lock(&pci_priv->bus_lock);
  2140. cnss_pci_update_timestamp(pci_priv);
  2141. mutex_unlock(&pci_priv->bus_lock);
  2142. schedule_delayed_work(&pci_priv->time_sync_work,
  2143. msecs_to_jiffies(time_sync_period_ms));
  2144. runtime_pm_put:
  2145. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  2146. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  2147. }
  2148. static int cnss_pci_start_time_sync_update(struct cnss_pci_data *pci_priv)
  2149. {
  2150. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2151. switch (pci_priv->device_id) {
  2152. case QCA6390_DEVICE_ID:
  2153. case QCA6490_DEVICE_ID:
  2154. case KIWI_DEVICE_ID:
  2155. break;
  2156. default:
  2157. return -EOPNOTSUPP;
  2158. }
  2159. if (!plat_priv->device_freq_hz) {
  2160. cnss_pr_dbg("Device time clock frequency is not valid, skip time sync\n");
  2161. return -EINVAL;
  2162. }
  2163. cnss_pci_time_sync_work_hdlr(&pci_priv->time_sync_work.work);
  2164. return 0;
  2165. }
  2166. static void cnss_pci_stop_time_sync_update(struct cnss_pci_data *pci_priv)
  2167. {
  2168. switch (pci_priv->device_id) {
  2169. case QCA6390_DEVICE_ID:
  2170. case QCA6490_DEVICE_ID:
  2171. case KIWI_DEVICE_ID:
  2172. break;
  2173. default:
  2174. return;
  2175. }
  2176. cancel_delayed_work_sync(&pci_priv->time_sync_work);
  2177. }
  2178. int cnss_pci_call_driver_probe(struct cnss_pci_data *pci_priv)
  2179. {
  2180. int ret = 0;
  2181. struct cnss_plat_data *plat_priv;
  2182. if (!pci_priv)
  2183. return -ENODEV;
  2184. plat_priv = pci_priv->plat_priv;
  2185. if (test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2186. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2187. cnss_pr_dbg("Skip driver probe\n");
  2188. goto out;
  2189. }
  2190. if (!pci_priv->driver_ops) {
  2191. cnss_pr_err("driver_ops is NULL\n");
  2192. ret = -EINVAL;
  2193. goto out;
  2194. }
  2195. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2196. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2197. ret = pci_priv->driver_ops->reinit(pci_priv->pci_dev,
  2198. pci_priv->pci_device_id);
  2199. if (ret) {
  2200. cnss_pr_err("Failed to reinit host driver, err = %d\n",
  2201. ret);
  2202. goto out;
  2203. }
  2204. complete(&plat_priv->recovery_complete);
  2205. } else if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state)) {
  2206. ret = pci_priv->driver_ops->probe(pci_priv->pci_dev,
  2207. pci_priv->pci_device_id);
  2208. if (ret) {
  2209. cnss_pr_err("Failed to probe host driver, err = %d\n",
  2210. ret);
  2211. goto out;
  2212. }
  2213. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2214. set_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2215. complete_all(&plat_priv->power_up_complete);
  2216. } else if (test_bit(CNSS_DRIVER_IDLE_RESTART,
  2217. &plat_priv->driver_state)) {
  2218. ret = pci_priv->driver_ops->idle_restart(pci_priv->pci_dev,
  2219. pci_priv->pci_device_id);
  2220. if (ret) {
  2221. cnss_pr_err("Failed to idle restart host driver, err = %d\n",
  2222. ret);
  2223. plat_priv->power_up_error = ret;
  2224. complete_all(&plat_priv->power_up_complete);
  2225. goto out;
  2226. }
  2227. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  2228. complete_all(&plat_priv->power_up_complete);
  2229. } else {
  2230. complete(&plat_priv->power_up_complete);
  2231. }
  2232. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  2233. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2234. __pm_relax(plat_priv->recovery_ws);
  2235. }
  2236. cnss_pci_start_time_sync_update(pci_priv);
  2237. return 0;
  2238. out:
  2239. return ret;
  2240. }
  2241. int cnss_pci_call_driver_remove(struct cnss_pci_data *pci_priv)
  2242. {
  2243. struct cnss_plat_data *plat_priv;
  2244. int ret;
  2245. if (!pci_priv)
  2246. return -ENODEV;
  2247. plat_priv = pci_priv->plat_priv;
  2248. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  2249. test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state) ||
  2250. test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2251. cnss_pr_dbg("Skip driver remove\n");
  2252. return 0;
  2253. }
  2254. if (!pci_priv->driver_ops) {
  2255. cnss_pr_err("driver_ops is NULL\n");
  2256. return -EINVAL;
  2257. }
  2258. cnss_pci_stop_time_sync_update(pci_priv);
  2259. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2260. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2261. pci_priv->driver_ops->shutdown(pci_priv->pci_dev);
  2262. } else if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state)) {
  2263. pci_priv->driver_ops->remove(pci_priv->pci_dev);
  2264. clear_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2265. } else if (test_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2266. &plat_priv->driver_state)) {
  2267. ret = pci_priv->driver_ops->idle_shutdown(pci_priv->pci_dev);
  2268. if (ret == -EAGAIN) {
  2269. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2270. &plat_priv->driver_state);
  2271. return ret;
  2272. }
  2273. }
  2274. plat_priv->get_info_cb_ctx = NULL;
  2275. plat_priv->get_info_cb = NULL;
  2276. return 0;
  2277. }
  2278. int cnss_pci_call_driver_modem_status(struct cnss_pci_data *pci_priv,
  2279. int modem_current_status)
  2280. {
  2281. struct cnss_wlan_driver *driver_ops;
  2282. if (!pci_priv)
  2283. return -ENODEV;
  2284. driver_ops = pci_priv->driver_ops;
  2285. if (!driver_ops || !driver_ops->modem_status)
  2286. return -EINVAL;
  2287. driver_ops->modem_status(pci_priv->pci_dev, modem_current_status);
  2288. return 0;
  2289. }
  2290. int cnss_pci_update_status(struct cnss_pci_data *pci_priv,
  2291. enum cnss_driver_status status)
  2292. {
  2293. struct cnss_wlan_driver *driver_ops;
  2294. if (!pci_priv)
  2295. return -ENODEV;
  2296. driver_ops = pci_priv->driver_ops;
  2297. if (!driver_ops || !driver_ops->update_status)
  2298. return -EINVAL;
  2299. cnss_pr_dbg("Update driver status: %d\n", status);
  2300. driver_ops->update_status(pci_priv->pci_dev, status);
  2301. return 0;
  2302. }
  2303. static void cnss_pci_misc_reg_dump(struct cnss_pci_data *pci_priv,
  2304. struct cnss_misc_reg *misc_reg,
  2305. u32 misc_reg_size,
  2306. char *reg_name)
  2307. {
  2308. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2309. bool do_force_wake_put = true;
  2310. int i;
  2311. if (!misc_reg)
  2312. return;
  2313. if (in_interrupt() || irqs_disabled())
  2314. return;
  2315. if (cnss_pci_check_link_status(pci_priv))
  2316. return;
  2317. if (cnss_pci_force_wake_get(pci_priv)) {
  2318. /* Continue to dump when device has entered RDDM already */
  2319. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2320. return;
  2321. do_force_wake_put = false;
  2322. }
  2323. cnss_pr_dbg("Start to dump %s registers\n", reg_name);
  2324. for (i = 0; i < misc_reg_size; i++) {
  2325. if (!test_bit(pci_priv->misc_reg_dev_mask,
  2326. &misc_reg[i].dev_mask))
  2327. continue;
  2328. if (misc_reg[i].wr) {
  2329. if (misc_reg[i].offset ==
  2330. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG &&
  2331. i >= 1)
  2332. misc_reg[i].val =
  2333. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG_MSK |
  2334. misc_reg[i - 1].val;
  2335. if (cnss_pci_reg_write(pci_priv,
  2336. misc_reg[i].offset,
  2337. misc_reg[i].val))
  2338. goto force_wake_put;
  2339. cnss_pr_vdbg("Write 0x%X to 0x%X\n",
  2340. misc_reg[i].val,
  2341. misc_reg[i].offset);
  2342. } else {
  2343. if (cnss_pci_reg_read(pci_priv,
  2344. misc_reg[i].offset,
  2345. &misc_reg[i].val))
  2346. goto force_wake_put;
  2347. }
  2348. }
  2349. force_wake_put:
  2350. if (do_force_wake_put)
  2351. cnss_pci_force_wake_put(pci_priv);
  2352. }
  2353. static void cnss_pci_dump_misc_reg(struct cnss_pci_data *pci_priv)
  2354. {
  2355. if (in_interrupt() || irqs_disabled())
  2356. return;
  2357. if (cnss_pci_check_link_status(pci_priv))
  2358. return;
  2359. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wcss_reg,
  2360. WCSS_REG_SIZE, "wcss");
  2361. cnss_pci_misc_reg_dump(pci_priv, pci_priv->pcie_reg,
  2362. PCIE_REG_SIZE, "pcie");
  2363. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wlaon_reg,
  2364. WLAON_REG_SIZE, "wlaon");
  2365. cnss_pci_misc_reg_dump(pci_priv, pci_priv->syspm_reg,
  2366. SYSPM_REG_SIZE, "syspm");
  2367. }
  2368. static void cnss_pci_dump_shadow_reg(struct cnss_pci_data *pci_priv)
  2369. {
  2370. int i, j = 0, array_size = SHADOW_REG_COUNT + SHADOW_REG_INTER_COUNT;
  2371. u32 reg_offset;
  2372. bool do_force_wake_put = true;
  2373. if (in_interrupt() || irqs_disabled())
  2374. return;
  2375. if (cnss_pci_check_link_status(pci_priv))
  2376. return;
  2377. if (!pci_priv->debug_reg) {
  2378. pci_priv->debug_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  2379. sizeof(*pci_priv->debug_reg)
  2380. * array_size, GFP_KERNEL);
  2381. if (!pci_priv->debug_reg)
  2382. return;
  2383. }
  2384. if (cnss_pci_force_wake_get(pci_priv))
  2385. do_force_wake_put = false;
  2386. cnss_pr_dbg("Start to dump shadow registers\n");
  2387. for (i = 0; i < SHADOW_REG_COUNT; i++, j++) {
  2388. reg_offset = PCIE_SHADOW_REG_VALUE_0 + i * 4;
  2389. pci_priv->debug_reg[j].offset = reg_offset;
  2390. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2391. &pci_priv->debug_reg[j].val))
  2392. goto force_wake_put;
  2393. }
  2394. for (i = 0; i < SHADOW_REG_INTER_COUNT; i++, j++) {
  2395. reg_offset = PCIE_SHADOW_REG_INTER_0 + i * 4;
  2396. pci_priv->debug_reg[j].offset = reg_offset;
  2397. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2398. &pci_priv->debug_reg[j].val))
  2399. goto force_wake_put;
  2400. }
  2401. force_wake_put:
  2402. if (do_force_wake_put)
  2403. cnss_pci_force_wake_put(pci_priv);
  2404. }
  2405. static int cnss_qca6174_powerup(struct cnss_pci_data *pci_priv)
  2406. {
  2407. int ret = 0;
  2408. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2409. ret = cnss_power_on_device(plat_priv);
  2410. if (ret) {
  2411. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2412. goto out;
  2413. }
  2414. ret = cnss_resume_pci_link(pci_priv);
  2415. if (ret) {
  2416. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2417. goto power_off;
  2418. }
  2419. ret = cnss_pci_call_driver_probe(pci_priv);
  2420. if (ret)
  2421. goto suspend_link;
  2422. return 0;
  2423. suspend_link:
  2424. cnss_suspend_pci_link(pci_priv);
  2425. power_off:
  2426. cnss_power_off_device(plat_priv);
  2427. out:
  2428. return ret;
  2429. }
  2430. static int cnss_qca6174_shutdown(struct cnss_pci_data *pci_priv)
  2431. {
  2432. int ret = 0;
  2433. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2434. cnss_pci_pm_runtime_resume(pci_priv);
  2435. ret = cnss_pci_call_driver_remove(pci_priv);
  2436. if (ret == -EAGAIN)
  2437. goto out;
  2438. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2439. CNSS_BUS_WIDTH_NONE);
  2440. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2441. cnss_pci_set_auto_suspended(pci_priv, 0);
  2442. ret = cnss_suspend_pci_link(pci_priv);
  2443. if (ret)
  2444. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2445. cnss_power_off_device(plat_priv);
  2446. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2447. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2448. out:
  2449. return ret;
  2450. }
  2451. static void cnss_qca6174_crash_shutdown(struct cnss_pci_data *pci_priv)
  2452. {
  2453. if (pci_priv->driver_ops && pci_priv->driver_ops->crash_shutdown)
  2454. pci_priv->driver_ops->crash_shutdown(pci_priv->pci_dev);
  2455. }
  2456. static int cnss_qca6174_ramdump(struct cnss_pci_data *pci_priv)
  2457. {
  2458. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2459. struct cnss_ramdump_info *ramdump_info;
  2460. ramdump_info = &plat_priv->ramdump_info;
  2461. if (!ramdump_info->ramdump_size)
  2462. return -EINVAL;
  2463. return cnss_do_ramdump(plat_priv);
  2464. }
  2465. static int cnss_qca6290_powerup(struct cnss_pci_data *pci_priv)
  2466. {
  2467. int ret = 0;
  2468. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2469. unsigned int timeout;
  2470. int retry = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  2471. if (plat_priv->ramdump_info_v2.dump_data_valid) {
  2472. cnss_pci_clear_dump_info(pci_priv);
  2473. cnss_pci_power_off_mhi(pci_priv);
  2474. cnss_suspend_pci_link(pci_priv);
  2475. cnss_pci_deinit_mhi(pci_priv);
  2476. cnss_power_off_device(plat_priv);
  2477. }
  2478. /* Clear QMI send usage count during every power up */
  2479. pci_priv->qmi_send_usage_count = 0;
  2480. plat_priv->power_up_error = 0;
  2481. retry:
  2482. ret = cnss_power_on_device(plat_priv);
  2483. if (ret) {
  2484. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2485. goto out;
  2486. }
  2487. ret = cnss_resume_pci_link(pci_priv);
  2488. if (ret) {
  2489. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2490. if (test_bit(IGNORE_PCI_LINK_FAILURE,
  2491. &plat_priv->ctrl_params.quirks)) {
  2492. cnss_pr_dbg("Ignore PCI link resume failure\n");
  2493. ret = 0;
  2494. goto out;
  2495. }
  2496. if (ret == -EAGAIN && retry++ < POWER_ON_RETRY_MAX_TIMES) {
  2497. cnss_power_off_device(plat_priv);
  2498. /* Force toggle BT_EN GPIO low */
  2499. if (retry == POWER_ON_RETRY_MAX_TIMES) {
  2500. cnss_pr_dbg("Retry #%d. Set BT_EN GPIO(%u) low\n",
  2501. retry, bt_en_gpio);
  2502. if (bt_en_gpio >= 0)
  2503. gpio_direction_output(bt_en_gpio, 0);
  2504. cnss_pr_dbg("BT_EN GPIO val: %d\n",
  2505. gpio_get_value(bt_en_gpio));
  2506. }
  2507. cnss_pr_dbg("Retry to resume PCI link #%d\n", retry);
  2508. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  2509. goto retry;
  2510. }
  2511. /* Assert when it reaches maximum retries */
  2512. CNSS_ASSERT(0);
  2513. goto power_off;
  2514. }
  2515. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  2516. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  2517. ret = cnss_pci_start_mhi(pci_priv);
  2518. if (ret) {
  2519. cnss_fatal_err("Failed to start MHI, err = %d\n", ret);
  2520. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  2521. !pci_priv->pci_link_down_ind && timeout) {
  2522. /* Start recovery directly for MHI start failures */
  2523. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  2524. CNSS_REASON_DEFAULT);
  2525. }
  2526. return 0;
  2527. }
  2528. if (test_bit(USE_CORE_ONLY_FW, &plat_priv->ctrl_params.quirks)) {
  2529. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  2530. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2531. return 0;
  2532. }
  2533. cnss_set_pin_connect_status(plat_priv);
  2534. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  2535. ret = cnss_pci_call_driver_probe(pci_priv);
  2536. if (ret)
  2537. goto stop_mhi;
  2538. } else if (timeout) {
  2539. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state))
  2540. timeout += WLAN_COLD_BOOT_CAL_TIMEOUT;
  2541. else
  2542. timeout += WLAN_MISSION_MODE_TIMEOUT;
  2543. mod_timer(&plat_priv->fw_boot_timer,
  2544. jiffies + msecs_to_jiffies(timeout));
  2545. }
  2546. return 0;
  2547. stop_mhi:
  2548. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, true);
  2549. cnss_pci_power_off_mhi(pci_priv);
  2550. cnss_suspend_pci_link(pci_priv);
  2551. cnss_pci_deinit_mhi(pci_priv);
  2552. power_off:
  2553. cnss_power_off_device(plat_priv);
  2554. out:
  2555. return ret;
  2556. }
  2557. static int cnss_qca6290_shutdown(struct cnss_pci_data *pci_priv)
  2558. {
  2559. int ret = 0;
  2560. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2561. int do_force_wake = true;
  2562. cnss_pci_pm_runtime_resume(pci_priv);
  2563. ret = cnss_pci_call_driver_remove(pci_priv);
  2564. if (ret == -EAGAIN)
  2565. goto out;
  2566. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2567. CNSS_BUS_WIDTH_NONE);
  2568. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2569. cnss_pci_set_auto_suspended(pci_priv, 0);
  2570. if ((test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  2571. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2572. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  2573. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state) ||
  2574. test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) &&
  2575. test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  2576. del_timer(&pci_priv->dev_rddm_timer);
  2577. cnss_pci_collect_dump_info(pci_priv, false);
  2578. CNSS_ASSERT(0);
  2579. }
  2580. if (!cnss_is_device_powered_on(plat_priv)) {
  2581. cnss_pr_dbg("Device is already powered off, ignore\n");
  2582. goto skip_power_off;
  2583. }
  2584. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2585. do_force_wake = false;
  2586. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, do_force_wake);
  2587. /* FBC image will be freed after powering off MHI, so skip
  2588. * if RAM dump data is still valid.
  2589. */
  2590. if (plat_priv->ramdump_info_v2.dump_data_valid)
  2591. goto skip_power_off;
  2592. cnss_pci_power_off_mhi(pci_priv);
  2593. ret = cnss_suspend_pci_link(pci_priv);
  2594. if (ret)
  2595. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2596. cnss_pci_deinit_mhi(pci_priv);
  2597. cnss_power_off_device(plat_priv);
  2598. skip_power_off:
  2599. pci_priv->remap_window = 0;
  2600. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  2601. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  2602. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2603. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  2604. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  2605. pci_priv->pci_link_down_ind = false;
  2606. }
  2607. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2608. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2609. out:
  2610. return ret;
  2611. }
  2612. static void cnss_qca6290_crash_shutdown(struct cnss_pci_data *pci_priv)
  2613. {
  2614. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2615. set_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2616. cnss_pr_dbg("Crash shutdown with driver_state 0x%lx\n",
  2617. plat_priv->driver_state);
  2618. cnss_pci_collect_dump_info(pci_priv, true);
  2619. clear_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2620. }
  2621. static int cnss_qca6290_ramdump(struct cnss_pci_data *pci_priv)
  2622. {
  2623. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2624. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2625. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2626. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2627. int ret = 0;
  2628. if (!info_v2->dump_data_valid || !dump_seg ||
  2629. dump_data->nentries == 0)
  2630. return 0;
  2631. ret = cnss_do_elf_ramdump(plat_priv);
  2632. cnss_pci_clear_dump_info(pci_priv);
  2633. cnss_pci_power_off_mhi(pci_priv);
  2634. cnss_suspend_pci_link(pci_priv);
  2635. cnss_pci_deinit_mhi(pci_priv);
  2636. cnss_power_off_device(plat_priv);
  2637. return ret;
  2638. }
  2639. int cnss_pci_dev_powerup(struct cnss_pci_data *pci_priv)
  2640. {
  2641. int ret = 0;
  2642. if (!pci_priv) {
  2643. cnss_pr_err("pci_priv is NULL\n");
  2644. return -ENODEV;
  2645. }
  2646. switch (pci_priv->device_id) {
  2647. case QCA6174_DEVICE_ID:
  2648. ret = cnss_qca6174_powerup(pci_priv);
  2649. break;
  2650. case QCA6290_DEVICE_ID:
  2651. case QCA6390_DEVICE_ID:
  2652. case QCA6490_DEVICE_ID:
  2653. case KIWI_DEVICE_ID:
  2654. ret = cnss_qca6290_powerup(pci_priv);
  2655. break;
  2656. default:
  2657. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2658. pci_priv->device_id);
  2659. ret = -ENODEV;
  2660. }
  2661. return ret;
  2662. }
  2663. int cnss_pci_dev_shutdown(struct cnss_pci_data *pci_priv)
  2664. {
  2665. int ret = 0;
  2666. if (!pci_priv) {
  2667. cnss_pr_err("pci_priv is NULL\n");
  2668. return -ENODEV;
  2669. }
  2670. switch (pci_priv->device_id) {
  2671. case QCA6174_DEVICE_ID:
  2672. ret = cnss_qca6174_shutdown(pci_priv);
  2673. break;
  2674. case QCA6290_DEVICE_ID:
  2675. case QCA6390_DEVICE_ID:
  2676. case QCA6490_DEVICE_ID:
  2677. case KIWI_DEVICE_ID:
  2678. ret = cnss_qca6290_shutdown(pci_priv);
  2679. break;
  2680. default:
  2681. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2682. pci_priv->device_id);
  2683. ret = -ENODEV;
  2684. }
  2685. return ret;
  2686. }
  2687. int cnss_pci_dev_crash_shutdown(struct cnss_pci_data *pci_priv)
  2688. {
  2689. int ret = 0;
  2690. if (!pci_priv) {
  2691. cnss_pr_err("pci_priv is NULL\n");
  2692. return -ENODEV;
  2693. }
  2694. switch (pci_priv->device_id) {
  2695. case QCA6174_DEVICE_ID:
  2696. cnss_qca6174_crash_shutdown(pci_priv);
  2697. break;
  2698. case QCA6290_DEVICE_ID:
  2699. case QCA6390_DEVICE_ID:
  2700. case QCA6490_DEVICE_ID:
  2701. case KIWI_DEVICE_ID:
  2702. cnss_qca6290_crash_shutdown(pci_priv);
  2703. break;
  2704. default:
  2705. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2706. pci_priv->device_id);
  2707. ret = -ENODEV;
  2708. }
  2709. return ret;
  2710. }
  2711. int cnss_pci_dev_ramdump(struct cnss_pci_data *pci_priv)
  2712. {
  2713. int ret = 0;
  2714. if (!pci_priv) {
  2715. cnss_pr_err("pci_priv is NULL\n");
  2716. return -ENODEV;
  2717. }
  2718. switch (pci_priv->device_id) {
  2719. case QCA6174_DEVICE_ID:
  2720. ret = cnss_qca6174_ramdump(pci_priv);
  2721. break;
  2722. case QCA6290_DEVICE_ID:
  2723. case QCA6390_DEVICE_ID:
  2724. case QCA6490_DEVICE_ID:
  2725. case KIWI_DEVICE_ID:
  2726. ret = cnss_qca6290_ramdump(pci_priv);
  2727. break;
  2728. default:
  2729. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2730. pci_priv->device_id);
  2731. ret = -ENODEV;
  2732. }
  2733. return ret;
  2734. }
  2735. int cnss_pci_is_drv_connected(struct device *dev)
  2736. {
  2737. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2738. if (!pci_priv)
  2739. return -ENODEV;
  2740. return pci_priv->drv_connected_last;
  2741. }
  2742. EXPORT_SYMBOL(cnss_pci_is_drv_connected);
  2743. static void cnss_wlan_reg_driver_work(struct work_struct *work)
  2744. {
  2745. struct cnss_plat_data *plat_priv =
  2746. container_of(work, struct cnss_plat_data, wlan_reg_driver_work.work);
  2747. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  2748. struct cnss_cal_info *cal_info;
  2749. unsigned int timeout;
  2750. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  2751. goto reg_driver;
  2752. } else {
  2753. if (plat_priv->charger_mode) {
  2754. cnss_pr_err("Ignore calibration timeout in charger mode\n");
  2755. return;
  2756. }
  2757. if (!test_bit(CNSS_IN_COLD_BOOT_CAL,
  2758. &plat_priv->driver_state)) {
  2759. timeout = cnss_get_timeout(plat_priv,
  2760. CNSS_TIMEOUT_CALIBRATION);
  2761. cnss_pr_dbg("File system not ready to start calibration. Wait for %ds..\n",
  2762. timeout / 1000);
  2763. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2764. msecs_to_jiffies(timeout));
  2765. return;
  2766. }
  2767. del_timer(&plat_priv->fw_boot_timer);
  2768. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) &&
  2769. !test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2770. cnss_pr_err("Timeout waiting for calibration to complete\n");
  2771. CNSS_ASSERT(0);
  2772. }
  2773. cal_info = kzalloc(sizeof(*cal_info), GFP_KERNEL);
  2774. if (!cal_info)
  2775. return;
  2776. cal_info->cal_status = CNSS_CAL_TIMEOUT;
  2777. cnss_driver_event_post(plat_priv,
  2778. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE,
  2779. 0, cal_info);
  2780. }
  2781. reg_driver:
  2782. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2783. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2784. return;
  2785. }
  2786. reinit_completion(&plat_priv->power_up_complete);
  2787. cnss_driver_event_post(plat_priv,
  2788. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2789. CNSS_EVENT_SYNC_UNKILLABLE,
  2790. pci_priv->driver_ops);
  2791. }
  2792. int cnss_wlan_register_driver(struct cnss_wlan_driver *driver_ops)
  2793. {
  2794. int ret = 0;
  2795. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2796. struct cnss_pci_data *pci_priv;
  2797. const struct pci_device_id *id_table = driver_ops->id_table;
  2798. unsigned int timeout;
  2799. if (!plat_priv) {
  2800. cnss_pr_info("plat_priv is not ready for register driver\n");
  2801. return -EAGAIN;
  2802. }
  2803. if (!test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state)) {
  2804. cnss_pr_info("pci probe not yet done for register driver\n");
  2805. return -EAGAIN;
  2806. }
  2807. pci_priv = plat_priv->bus_priv;
  2808. if (pci_priv->driver_ops) {
  2809. cnss_pr_err("Driver has already registered\n");
  2810. return -EEXIST;
  2811. }
  2812. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2813. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2814. return -EINVAL;
  2815. }
  2816. if (!id_table || !pci_dev_present(id_table)) {
  2817. /* id_table pointer will move from pci_dev_present(),
  2818. * so check again using local pointer.
  2819. */
  2820. id_table = driver_ops->id_table;
  2821. while (id_table && id_table->vendor) {
  2822. cnss_pr_info("Host driver is built for PCIe device ID 0x%x\n",
  2823. id_table->device);
  2824. id_table++;
  2825. }
  2826. cnss_pr_err("Enumerated PCIe device id is 0x%x, reject unsupported driver\n",
  2827. pci_priv->device_id);
  2828. return -ENODEV;
  2829. }
  2830. if (driver_ops->chip_version != CNSS_CHIP_VER_ANY &&
  2831. driver_ops->chip_version != plat_priv->device_version.major_version) {
  2832. cnss_pr_err("Driver built for chip ver 0x%x, enumerated ver 0x%x, reject unsupported driver\n",
  2833. driver_ops->chip_version,
  2834. plat_priv->device_version.major_version);
  2835. return -ENODEV;
  2836. }
  2837. set_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state);
  2838. if (!plat_priv->cbc_enabled ||
  2839. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  2840. goto register_driver;
  2841. pci_priv->driver_ops = driver_ops;
  2842. /* If Cold Boot Calibration is enabled, it is the 1st step in init
  2843. * sequence.CBC is done on file system_ready trigger. Qcacld will be
  2844. * loaded from vendor_modprobe.sh at early boot and must be deferred
  2845. * until CBC is complete
  2846. */
  2847. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_CALIBRATION);
  2848. INIT_DELAYED_WORK(&plat_priv->wlan_reg_driver_work,
  2849. cnss_wlan_reg_driver_work);
  2850. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2851. msecs_to_jiffies(timeout));
  2852. cnss_pr_info("WLAN register driver deferred for Calibration\n");
  2853. return 0;
  2854. register_driver:
  2855. reinit_completion(&plat_priv->power_up_complete);
  2856. ret = cnss_driver_event_post(plat_priv,
  2857. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2858. CNSS_EVENT_SYNC_UNKILLABLE,
  2859. driver_ops);
  2860. return ret;
  2861. }
  2862. EXPORT_SYMBOL(cnss_wlan_register_driver);
  2863. void cnss_wlan_unregister_driver(struct cnss_wlan_driver *driver_ops)
  2864. {
  2865. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2866. int ret = 0;
  2867. unsigned int timeout;
  2868. if (!plat_priv) {
  2869. cnss_pr_err("plat_priv is NULL\n");
  2870. return;
  2871. }
  2872. mutex_lock(&plat_priv->driver_ops_lock);
  2873. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  2874. goto skip_wait_power_up;
  2875. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_WLAN_WATCHDOG);
  2876. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  2877. msecs_to_jiffies(timeout));
  2878. if (!ret) {
  2879. cnss_pr_err("Timeout (%ums) waiting for driver power up to complete\n",
  2880. timeout);
  2881. CNSS_ASSERT(0);
  2882. }
  2883. skip_wait_power_up:
  2884. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2885. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2886. goto skip_wait_recovery;
  2887. reinit_completion(&plat_priv->recovery_complete);
  2888. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  2889. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  2890. msecs_to_jiffies(timeout));
  2891. if (!ret) {
  2892. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  2893. timeout);
  2894. CNSS_ASSERT(0);
  2895. }
  2896. skip_wait_recovery:
  2897. cnss_driver_event_post(plat_priv,
  2898. CNSS_DRIVER_EVENT_UNREGISTER_DRIVER,
  2899. CNSS_EVENT_SYNC_UNKILLABLE, NULL);
  2900. mutex_unlock(&plat_priv->driver_ops_lock);
  2901. }
  2902. EXPORT_SYMBOL(cnss_wlan_unregister_driver);
  2903. int cnss_pci_register_driver_hdlr(struct cnss_pci_data *pci_priv,
  2904. void *data)
  2905. {
  2906. int ret = 0;
  2907. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2908. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2909. cnss_pr_dbg("Reboot or shutdown is in progress, ignore register driver\n");
  2910. return -EINVAL;
  2911. }
  2912. set_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2913. pci_priv->driver_ops = data;
  2914. ret = cnss_pci_dev_powerup(pci_priv);
  2915. if (ret) {
  2916. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2917. pci_priv->driver_ops = NULL;
  2918. }
  2919. return ret;
  2920. }
  2921. int cnss_pci_unregister_driver_hdlr(struct cnss_pci_data *pci_priv)
  2922. {
  2923. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2924. set_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2925. cnss_pci_dev_shutdown(pci_priv);
  2926. pci_priv->driver_ops = NULL;
  2927. return 0;
  2928. }
  2929. #if IS_ENABLED(CONFIG_PCI_MSM)
  2930. static bool cnss_pci_is_drv_supported(struct cnss_pci_data *pci_priv)
  2931. {
  2932. struct pci_dev *root_port = pcie_find_root_port(pci_priv->pci_dev);
  2933. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2934. struct device_node *root_of_node;
  2935. bool drv_supported = false;
  2936. if (!root_port) {
  2937. cnss_pr_err("PCIe DRV is not supported as root port is null\n");
  2938. pci_priv->drv_supported = false;
  2939. return drv_supported;
  2940. }
  2941. root_of_node = root_port->dev.of_node;
  2942. if (root_of_node->parent) {
  2943. drv_supported = of_property_read_bool(root_of_node->parent,
  2944. "qcom,drv-supported") ||
  2945. of_property_read_bool(root_of_node->parent,
  2946. "qcom,drv-name");
  2947. }
  2948. cnss_pr_dbg("PCIe DRV is %s\n",
  2949. drv_supported ? "supported" : "not supported");
  2950. pci_priv->drv_supported = drv_supported;
  2951. if (drv_supported) {
  2952. plat_priv->cap.cap_flag |= CNSS_HAS_DRV_SUPPORT;
  2953. cnss_set_feature_list(plat_priv, CNSS_DRV_SUPPORT_V01);
  2954. }
  2955. return drv_supported;
  2956. }
  2957. static void cnss_pci_event_cb(struct msm_pcie_notify *notify)
  2958. {
  2959. struct pci_dev *pci_dev;
  2960. struct cnss_pci_data *pci_priv;
  2961. struct device *dev;
  2962. struct cnss_plat_data *plat_priv = NULL;
  2963. int ret = 0;
  2964. if (!notify)
  2965. return;
  2966. pci_dev = notify->user;
  2967. if (!pci_dev)
  2968. return;
  2969. pci_priv = cnss_get_pci_priv(pci_dev);
  2970. if (!pci_priv)
  2971. return;
  2972. dev = &pci_priv->pci_dev->dev;
  2973. switch (notify->event) {
  2974. case MSM_PCIE_EVENT_LINK_RECOVER:
  2975. cnss_pr_dbg("PCI link recover callback\n");
  2976. plat_priv = pci_priv->plat_priv;
  2977. if (!plat_priv) {
  2978. cnss_pr_err("plat_priv is NULL\n");
  2979. return;
  2980. }
  2981. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  2982. ret = msm_pcie_pm_control(MSM_PCIE_HANDLE_LINKDOWN,
  2983. pci_dev->bus->number, pci_dev, NULL,
  2984. PM_OPTIONS_DEFAULT);
  2985. if (ret)
  2986. cnss_pci_handle_linkdown(pci_priv);
  2987. break;
  2988. case MSM_PCIE_EVENT_LINKDOWN:
  2989. cnss_pr_dbg("PCI link down event callback\n");
  2990. cnss_pci_handle_linkdown(pci_priv);
  2991. break;
  2992. case MSM_PCIE_EVENT_WAKEUP:
  2993. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  2994. cnss_pci_get_auto_suspended(pci_priv)) ||
  2995. dev->power.runtime_status == RPM_SUSPENDING) {
  2996. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2997. cnss_pci_pm_request_resume(pci_priv);
  2998. }
  2999. break;
  3000. case MSM_PCIE_EVENT_DRV_CONNECT:
  3001. cnss_pr_dbg("DRV subsystem is connected\n");
  3002. cnss_pci_set_drv_connected(pci_priv, 1);
  3003. break;
  3004. case MSM_PCIE_EVENT_DRV_DISCONNECT:
  3005. cnss_pr_dbg("DRV subsystem is disconnected\n");
  3006. if (cnss_pci_get_auto_suspended(pci_priv))
  3007. cnss_pci_pm_request_resume(pci_priv);
  3008. cnss_pci_set_drv_connected(pci_priv, 0);
  3009. break;
  3010. default:
  3011. cnss_pr_err("Received invalid PCI event: %d\n", notify->event);
  3012. }
  3013. }
  3014. /**
  3015. * cnss_reg_pci_event() - Register for PCIe events
  3016. * @pci_priv: driver PCI bus context pointer
  3017. *
  3018. * This function shall call corresponding PCIe root complex driver APIs
  3019. * to register for PCIe events like link down or WAKE GPIO toggling etc.
  3020. * The events should be based on PCIe root complex driver's capability.
  3021. *
  3022. * Return: 0 for success, negative value for error
  3023. */
  3024. static int cnss_reg_pci_event(struct cnss_pci_data *pci_priv)
  3025. {
  3026. int ret = 0;
  3027. struct msm_pcie_register_event *pci_event;
  3028. pci_event = &pci_priv->msm_pci_event;
  3029. pci_event->events = MSM_PCIE_EVENT_LINK_RECOVER |
  3030. MSM_PCIE_EVENT_LINKDOWN |
  3031. MSM_PCIE_EVENT_WAKEUP;
  3032. if (cnss_pci_is_drv_supported(pci_priv))
  3033. pci_event->events = pci_event->events |
  3034. MSM_PCIE_EVENT_DRV_CONNECT |
  3035. MSM_PCIE_EVENT_DRV_DISCONNECT;
  3036. pci_event->user = pci_priv->pci_dev;
  3037. pci_event->mode = MSM_PCIE_TRIGGER_CALLBACK;
  3038. pci_event->callback = cnss_pci_event_cb;
  3039. pci_event->options = MSM_PCIE_CONFIG_NO_RECOVERY;
  3040. ret = msm_pcie_register_event(pci_event);
  3041. if (ret)
  3042. cnss_pr_err("Failed to register MSM PCI event, err = %d\n",
  3043. ret);
  3044. return ret;
  3045. }
  3046. static void cnss_dereg_pci_event(struct cnss_pci_data *pci_priv)
  3047. {
  3048. msm_pcie_deregister_event(&pci_priv->msm_pci_event);
  3049. }
  3050. #else
  3051. static int cnss_reg_pci_event(struct cnss_pci_data *pci_priv)
  3052. {
  3053. return 0;
  3054. }
  3055. static void cnss_dereg_pci_event(struct cnss_pci_data *pci_priv) {}
  3056. #endif
  3057. static int cnss_pci_suspend_driver(struct cnss_pci_data *pci_priv)
  3058. {
  3059. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3060. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3061. int ret = 0;
  3062. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  3063. if (driver_ops && driver_ops->suspend) {
  3064. ret = driver_ops->suspend(pci_dev, state);
  3065. if (ret) {
  3066. cnss_pr_err("Failed to suspend host driver, err = %d\n",
  3067. ret);
  3068. ret = -EAGAIN;
  3069. }
  3070. }
  3071. return ret;
  3072. }
  3073. static int cnss_pci_resume_driver(struct cnss_pci_data *pci_priv)
  3074. {
  3075. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3076. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3077. int ret = 0;
  3078. if (driver_ops && driver_ops->resume) {
  3079. ret = driver_ops->resume(pci_dev);
  3080. if (ret)
  3081. cnss_pr_err("Failed to resume host driver, err = %d\n",
  3082. ret);
  3083. }
  3084. return ret;
  3085. }
  3086. int cnss_pci_suspend_bus(struct cnss_pci_data *pci_priv)
  3087. {
  3088. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3089. int ret = 0;
  3090. if (pci_priv->pci_link_state == PCI_LINK_DOWN)
  3091. goto out;
  3092. if (cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_SUSPEND)) {
  3093. ret = -EAGAIN;
  3094. goto out;
  3095. }
  3096. if (pci_priv->drv_connected_last)
  3097. goto skip_disable_pci;
  3098. pci_clear_master(pci_dev);
  3099. cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  3100. pci_disable_device(pci_dev);
  3101. ret = pci_set_power_state(pci_dev, PCI_D3hot);
  3102. if (ret)
  3103. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  3104. skip_disable_pci:
  3105. if (cnss_set_pci_link(pci_priv, PCI_LINK_DOWN)) {
  3106. ret = -EAGAIN;
  3107. goto resume_mhi;
  3108. }
  3109. pci_priv->pci_link_state = PCI_LINK_DOWN;
  3110. return 0;
  3111. resume_mhi:
  3112. if (!pci_is_enabled(pci_dev))
  3113. if (pci_enable_device(pci_dev))
  3114. cnss_pr_err("Failed to enable PCI device\n");
  3115. if (pci_priv->saved_state)
  3116. cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  3117. pci_set_master(pci_dev);
  3118. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3119. out:
  3120. return ret;
  3121. }
  3122. int cnss_pci_resume_bus(struct cnss_pci_data *pci_priv)
  3123. {
  3124. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3125. int ret = 0;
  3126. if (pci_priv->pci_link_state == PCI_LINK_UP)
  3127. goto out;
  3128. if (cnss_set_pci_link(pci_priv, PCI_LINK_UP)) {
  3129. cnss_fatal_err("Failed to resume PCI link from suspend\n");
  3130. cnss_pci_link_down(&pci_dev->dev);
  3131. ret = -EAGAIN;
  3132. goto out;
  3133. }
  3134. pci_priv->pci_link_state = PCI_LINK_UP;
  3135. if (pci_priv->drv_connected_last)
  3136. goto skip_enable_pci;
  3137. ret = pci_enable_device(pci_dev);
  3138. if (ret) {
  3139. cnss_pr_err("Failed to enable PCI device, err = %d\n",
  3140. ret);
  3141. goto out;
  3142. }
  3143. if (pci_priv->saved_state)
  3144. cnss_set_pci_config_space(pci_priv,
  3145. RESTORE_PCI_CONFIG_SPACE);
  3146. pci_set_master(pci_dev);
  3147. skip_enable_pci:
  3148. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3149. out:
  3150. return ret;
  3151. }
  3152. static int cnss_pci_suspend(struct device *dev)
  3153. {
  3154. int ret = 0;
  3155. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3156. struct cnss_plat_data *plat_priv;
  3157. if (!pci_priv)
  3158. goto out;
  3159. plat_priv = pci_priv->plat_priv;
  3160. if (!plat_priv)
  3161. goto out;
  3162. if (!cnss_is_device_powered_on(plat_priv))
  3163. goto out;
  3164. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3165. pci_priv->drv_supported) {
  3166. pci_priv->drv_connected_last =
  3167. cnss_pci_get_drv_connected(pci_priv);
  3168. if (!pci_priv->drv_connected_last) {
  3169. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3170. ret = -EAGAIN;
  3171. goto out;
  3172. }
  3173. }
  3174. set_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3175. ret = cnss_pci_suspend_driver(pci_priv);
  3176. if (ret)
  3177. goto clear_flag;
  3178. if (!pci_priv->disable_pc) {
  3179. mutex_lock(&pci_priv->bus_lock);
  3180. ret = cnss_pci_suspend_bus(pci_priv);
  3181. mutex_unlock(&pci_priv->bus_lock);
  3182. if (ret)
  3183. goto resume_driver;
  3184. }
  3185. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  3186. return 0;
  3187. resume_driver:
  3188. cnss_pci_resume_driver(pci_priv);
  3189. clear_flag:
  3190. pci_priv->drv_connected_last = 0;
  3191. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3192. out:
  3193. return ret;
  3194. }
  3195. static int cnss_pci_resume(struct device *dev)
  3196. {
  3197. int ret = 0;
  3198. struct pci_dev *pci_dev = to_pci_dev(dev);
  3199. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3200. struct cnss_plat_data *plat_priv;
  3201. if (!pci_priv)
  3202. goto out;
  3203. plat_priv = pci_priv->plat_priv;
  3204. if (!plat_priv)
  3205. goto out;
  3206. if (pci_priv->pci_link_down_ind)
  3207. goto out;
  3208. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3209. goto out;
  3210. if (!pci_priv->disable_pc) {
  3211. ret = cnss_pci_resume_bus(pci_priv);
  3212. if (ret)
  3213. goto out;
  3214. }
  3215. ret = cnss_pci_resume_driver(pci_priv);
  3216. pci_priv->drv_connected_last = 0;
  3217. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3218. out:
  3219. return ret;
  3220. }
  3221. static int cnss_pci_suspend_noirq(struct device *dev)
  3222. {
  3223. int ret = 0;
  3224. struct pci_dev *pci_dev = to_pci_dev(dev);
  3225. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3226. struct cnss_wlan_driver *driver_ops;
  3227. if (!pci_priv)
  3228. goto out;
  3229. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3230. goto out;
  3231. driver_ops = pci_priv->driver_ops;
  3232. if (driver_ops && driver_ops->suspend_noirq)
  3233. ret = driver_ops->suspend_noirq(pci_dev);
  3234. if (pci_priv->disable_pc && !pci_dev->state_saved &&
  3235. !pci_priv->plat_priv->use_pm_domain)
  3236. pci_save_state(pci_dev);
  3237. out:
  3238. return ret;
  3239. }
  3240. static int cnss_pci_resume_noirq(struct device *dev)
  3241. {
  3242. int ret = 0;
  3243. struct pci_dev *pci_dev = to_pci_dev(dev);
  3244. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3245. struct cnss_wlan_driver *driver_ops;
  3246. if (!pci_priv)
  3247. goto out;
  3248. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3249. goto out;
  3250. driver_ops = pci_priv->driver_ops;
  3251. if (driver_ops && driver_ops->resume_noirq &&
  3252. !pci_priv->pci_link_down_ind)
  3253. ret = driver_ops->resume_noirq(pci_dev);
  3254. out:
  3255. return ret;
  3256. }
  3257. static int cnss_pci_runtime_suspend(struct device *dev)
  3258. {
  3259. int ret = 0;
  3260. struct pci_dev *pci_dev = to_pci_dev(dev);
  3261. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3262. struct cnss_plat_data *plat_priv;
  3263. struct cnss_wlan_driver *driver_ops;
  3264. if (!pci_priv)
  3265. return -EAGAIN;
  3266. plat_priv = pci_priv->plat_priv;
  3267. if (!plat_priv)
  3268. return -EAGAIN;
  3269. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3270. return -EAGAIN;
  3271. if (pci_priv->pci_link_down_ind) {
  3272. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3273. return -EAGAIN;
  3274. }
  3275. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3276. pci_priv->drv_supported) {
  3277. pci_priv->drv_connected_last =
  3278. cnss_pci_get_drv_connected(pci_priv);
  3279. if (!pci_priv->drv_connected_last) {
  3280. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3281. return -EAGAIN;
  3282. }
  3283. }
  3284. cnss_pr_vdbg("Runtime suspend start\n");
  3285. driver_ops = pci_priv->driver_ops;
  3286. if (driver_ops && driver_ops->runtime_ops &&
  3287. driver_ops->runtime_ops->runtime_suspend)
  3288. ret = driver_ops->runtime_ops->runtime_suspend(pci_dev);
  3289. else
  3290. ret = cnss_auto_suspend(dev);
  3291. if (ret)
  3292. pci_priv->drv_connected_last = 0;
  3293. cnss_pr_vdbg("Runtime suspend status: %d\n", ret);
  3294. return ret;
  3295. }
  3296. static int cnss_pci_runtime_resume(struct device *dev)
  3297. {
  3298. int ret = 0;
  3299. struct pci_dev *pci_dev = to_pci_dev(dev);
  3300. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3301. struct cnss_wlan_driver *driver_ops;
  3302. if (!pci_priv)
  3303. return -EAGAIN;
  3304. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3305. return -EAGAIN;
  3306. if (pci_priv->pci_link_down_ind) {
  3307. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3308. return -EAGAIN;
  3309. }
  3310. cnss_pr_vdbg("Runtime resume start\n");
  3311. driver_ops = pci_priv->driver_ops;
  3312. if (driver_ops && driver_ops->runtime_ops &&
  3313. driver_ops->runtime_ops->runtime_resume)
  3314. ret = driver_ops->runtime_ops->runtime_resume(pci_dev);
  3315. else
  3316. ret = cnss_auto_resume(dev);
  3317. if (!ret)
  3318. pci_priv->drv_connected_last = 0;
  3319. cnss_pr_vdbg("Runtime resume status: %d\n", ret);
  3320. return ret;
  3321. }
  3322. static int cnss_pci_runtime_idle(struct device *dev)
  3323. {
  3324. cnss_pr_vdbg("Runtime idle\n");
  3325. pm_request_autosuspend(dev);
  3326. return -EBUSY;
  3327. }
  3328. int cnss_wlan_pm_control(struct device *dev, bool vote)
  3329. {
  3330. struct pci_dev *pci_dev = to_pci_dev(dev);
  3331. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3332. int ret = 0;
  3333. if (!pci_priv)
  3334. return -ENODEV;
  3335. ret = cnss_pci_disable_pc(pci_priv, vote);
  3336. if (ret)
  3337. return ret;
  3338. pci_priv->disable_pc = vote;
  3339. cnss_pr_dbg("%s PCIe power collapse\n", vote ? "disable" : "enable");
  3340. return 0;
  3341. }
  3342. EXPORT_SYMBOL(cnss_wlan_pm_control);
  3343. static void cnss_pci_pm_runtime_get_record(struct cnss_pci_data *pci_priv,
  3344. enum cnss_rtpm_id id)
  3345. {
  3346. if (id >= RTPM_ID_MAX)
  3347. return;
  3348. atomic_inc(&pci_priv->pm_stats.runtime_get);
  3349. atomic_inc(&pci_priv->pm_stats.runtime_get_id[id]);
  3350. pci_priv->pm_stats.runtime_get_timestamp_id[id] =
  3351. cnss_get_host_timestamp(pci_priv->plat_priv);
  3352. }
  3353. static void cnss_pci_pm_runtime_put_record(struct cnss_pci_data *pci_priv,
  3354. enum cnss_rtpm_id id)
  3355. {
  3356. if (id >= RTPM_ID_MAX)
  3357. return;
  3358. atomic_inc(&pci_priv->pm_stats.runtime_put);
  3359. atomic_inc(&pci_priv->pm_stats.runtime_put_id[id]);
  3360. pci_priv->pm_stats.runtime_put_timestamp_id[id] =
  3361. cnss_get_host_timestamp(pci_priv->plat_priv);
  3362. }
  3363. void cnss_pci_pm_runtime_show_usage_count(struct cnss_pci_data *pci_priv)
  3364. {
  3365. struct device *dev;
  3366. if (!pci_priv)
  3367. return;
  3368. dev = &pci_priv->pci_dev->dev;
  3369. cnss_pr_dbg("Runtime PM usage count: %d\n",
  3370. atomic_read(&dev->power.usage_count));
  3371. }
  3372. int cnss_pci_pm_request_resume(struct cnss_pci_data *pci_priv)
  3373. {
  3374. struct device *dev;
  3375. enum rpm_status status;
  3376. if (!pci_priv)
  3377. return -ENODEV;
  3378. dev = &pci_priv->pci_dev->dev;
  3379. status = dev->power.runtime_status;
  3380. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3381. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3382. (void *)_RET_IP_);
  3383. return pm_request_resume(dev);
  3384. }
  3385. int cnss_pci_pm_runtime_resume(struct cnss_pci_data *pci_priv)
  3386. {
  3387. struct device *dev;
  3388. enum rpm_status status;
  3389. if (!pci_priv)
  3390. return -ENODEV;
  3391. dev = &pci_priv->pci_dev->dev;
  3392. status = dev->power.runtime_status;
  3393. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3394. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3395. (void *)_RET_IP_);
  3396. return pm_runtime_resume(dev);
  3397. }
  3398. int cnss_pci_pm_runtime_get(struct cnss_pci_data *pci_priv,
  3399. enum cnss_rtpm_id id)
  3400. {
  3401. struct device *dev;
  3402. enum rpm_status status;
  3403. if (!pci_priv)
  3404. return -ENODEV;
  3405. dev = &pci_priv->pci_dev->dev;
  3406. status = dev->power.runtime_status;
  3407. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3408. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3409. (void *)_RET_IP_);
  3410. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3411. return pm_runtime_get(dev);
  3412. }
  3413. int cnss_pci_pm_runtime_get_sync(struct cnss_pci_data *pci_priv,
  3414. enum cnss_rtpm_id id)
  3415. {
  3416. struct device *dev;
  3417. enum rpm_status status;
  3418. if (!pci_priv)
  3419. return -ENODEV;
  3420. dev = &pci_priv->pci_dev->dev;
  3421. status = dev->power.runtime_status;
  3422. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3423. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3424. (void *)_RET_IP_);
  3425. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3426. return pm_runtime_get_sync(dev);
  3427. }
  3428. void cnss_pci_pm_runtime_get_noresume(struct cnss_pci_data *pci_priv,
  3429. enum cnss_rtpm_id id)
  3430. {
  3431. if (!pci_priv)
  3432. return;
  3433. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3434. pm_runtime_get_noresume(&pci_priv->pci_dev->dev);
  3435. }
  3436. int cnss_pci_pm_runtime_put_autosuspend(struct cnss_pci_data *pci_priv,
  3437. enum cnss_rtpm_id id)
  3438. {
  3439. struct device *dev;
  3440. if (!pci_priv)
  3441. return -ENODEV;
  3442. dev = &pci_priv->pci_dev->dev;
  3443. if (atomic_read(&dev->power.usage_count) == 0) {
  3444. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3445. return -EINVAL;
  3446. }
  3447. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3448. return pm_runtime_put_autosuspend(&pci_priv->pci_dev->dev);
  3449. }
  3450. void cnss_pci_pm_runtime_put_noidle(struct cnss_pci_data *pci_priv,
  3451. enum cnss_rtpm_id id)
  3452. {
  3453. struct device *dev;
  3454. if (!pci_priv)
  3455. return;
  3456. dev = &pci_priv->pci_dev->dev;
  3457. if (atomic_read(&dev->power.usage_count) == 0) {
  3458. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3459. return;
  3460. }
  3461. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3462. pm_runtime_put_noidle(&pci_priv->pci_dev->dev);
  3463. }
  3464. void cnss_pci_pm_runtime_mark_last_busy(struct cnss_pci_data *pci_priv)
  3465. {
  3466. if (!pci_priv)
  3467. return;
  3468. pm_runtime_mark_last_busy(&pci_priv->pci_dev->dev);
  3469. }
  3470. int cnss_auto_suspend(struct device *dev)
  3471. {
  3472. int ret = 0;
  3473. struct pci_dev *pci_dev = to_pci_dev(dev);
  3474. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3475. struct cnss_plat_data *plat_priv;
  3476. if (!pci_priv)
  3477. return -ENODEV;
  3478. plat_priv = pci_priv->plat_priv;
  3479. if (!plat_priv)
  3480. return -ENODEV;
  3481. mutex_lock(&pci_priv->bus_lock);
  3482. if (!pci_priv->qmi_send_usage_count) {
  3483. ret = cnss_pci_suspend_bus(pci_priv);
  3484. if (ret) {
  3485. mutex_unlock(&pci_priv->bus_lock);
  3486. return ret;
  3487. }
  3488. }
  3489. cnss_pci_set_auto_suspended(pci_priv, 1);
  3490. mutex_unlock(&pci_priv->bus_lock);
  3491. cnss_pci_set_monitor_wake_intr(pci_priv, true);
  3492. /* For suspend temporarily set bandwidth vote to NONE and dont save in
  3493. * current_bw_vote as in resume path we should vote for last used
  3494. * bandwidth vote. Also ignore error if bw voting is not setup.
  3495. */
  3496. cnss_setup_bus_bandwidth(plat_priv, CNSS_BUS_WIDTH_NONE, false);
  3497. return 0;
  3498. }
  3499. EXPORT_SYMBOL(cnss_auto_suspend);
  3500. int cnss_auto_resume(struct device *dev)
  3501. {
  3502. int ret = 0;
  3503. struct pci_dev *pci_dev = to_pci_dev(dev);
  3504. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3505. struct cnss_plat_data *plat_priv;
  3506. if (!pci_priv)
  3507. return -ENODEV;
  3508. plat_priv = pci_priv->plat_priv;
  3509. if (!plat_priv)
  3510. return -ENODEV;
  3511. mutex_lock(&pci_priv->bus_lock);
  3512. ret = cnss_pci_resume_bus(pci_priv);
  3513. if (ret) {
  3514. mutex_unlock(&pci_priv->bus_lock);
  3515. return ret;
  3516. }
  3517. cnss_pci_set_auto_suspended(pci_priv, 0);
  3518. mutex_unlock(&pci_priv->bus_lock);
  3519. cnss_request_bus_bandwidth(dev, plat_priv->icc.current_bw_vote);
  3520. return 0;
  3521. }
  3522. EXPORT_SYMBOL(cnss_auto_resume);
  3523. int cnss_pci_force_wake_request_sync(struct device *dev, int timeout_us)
  3524. {
  3525. struct pci_dev *pci_dev = to_pci_dev(dev);
  3526. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3527. struct cnss_plat_data *plat_priv;
  3528. struct mhi_controller *mhi_ctrl;
  3529. if (!pci_priv)
  3530. return -ENODEV;
  3531. switch (pci_priv->device_id) {
  3532. case QCA6390_DEVICE_ID:
  3533. case QCA6490_DEVICE_ID:
  3534. case KIWI_DEVICE_ID:
  3535. break;
  3536. default:
  3537. return 0;
  3538. }
  3539. mhi_ctrl = pci_priv->mhi_ctrl;
  3540. if (!mhi_ctrl)
  3541. return -EINVAL;
  3542. plat_priv = pci_priv->plat_priv;
  3543. if (!plat_priv)
  3544. return -ENODEV;
  3545. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3546. return -EAGAIN;
  3547. if (timeout_us) {
  3548. /* Busy wait for timeout_us */
  3549. return cnss_mhi_device_get_sync_atomic(pci_priv,
  3550. timeout_us, false);
  3551. } else {
  3552. /* Sleep wait for mhi_ctrl->timeout_ms */
  3553. return mhi_device_get_sync(mhi_ctrl->mhi_dev);
  3554. }
  3555. }
  3556. EXPORT_SYMBOL(cnss_pci_force_wake_request_sync);
  3557. int cnss_pci_force_wake_request(struct device *dev)
  3558. {
  3559. struct pci_dev *pci_dev = to_pci_dev(dev);
  3560. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3561. struct cnss_plat_data *plat_priv;
  3562. struct mhi_controller *mhi_ctrl;
  3563. if (!pci_priv)
  3564. return -ENODEV;
  3565. switch (pci_priv->device_id) {
  3566. case QCA6390_DEVICE_ID:
  3567. case QCA6490_DEVICE_ID:
  3568. case KIWI_DEVICE_ID:
  3569. break;
  3570. default:
  3571. return 0;
  3572. }
  3573. mhi_ctrl = pci_priv->mhi_ctrl;
  3574. if (!mhi_ctrl)
  3575. return -EINVAL;
  3576. plat_priv = pci_priv->plat_priv;
  3577. if (!plat_priv)
  3578. return -ENODEV;
  3579. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3580. return -EAGAIN;
  3581. mhi_device_get(mhi_ctrl->mhi_dev);
  3582. return 0;
  3583. }
  3584. EXPORT_SYMBOL(cnss_pci_force_wake_request);
  3585. int cnss_pci_is_device_awake(struct device *dev)
  3586. {
  3587. struct pci_dev *pci_dev = to_pci_dev(dev);
  3588. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3589. struct mhi_controller *mhi_ctrl;
  3590. if (!pci_priv)
  3591. return -ENODEV;
  3592. switch (pci_priv->device_id) {
  3593. case QCA6390_DEVICE_ID:
  3594. case QCA6490_DEVICE_ID:
  3595. case KIWI_DEVICE_ID:
  3596. break;
  3597. default:
  3598. return 0;
  3599. }
  3600. mhi_ctrl = pci_priv->mhi_ctrl;
  3601. if (!mhi_ctrl)
  3602. return -EINVAL;
  3603. return (mhi_ctrl->dev_state == MHI_STATE_M0);
  3604. }
  3605. EXPORT_SYMBOL(cnss_pci_is_device_awake);
  3606. int cnss_pci_force_wake_release(struct device *dev)
  3607. {
  3608. struct pci_dev *pci_dev = to_pci_dev(dev);
  3609. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3610. struct cnss_plat_data *plat_priv;
  3611. struct mhi_controller *mhi_ctrl;
  3612. if (!pci_priv)
  3613. return -ENODEV;
  3614. switch (pci_priv->device_id) {
  3615. case QCA6390_DEVICE_ID:
  3616. case QCA6490_DEVICE_ID:
  3617. case KIWI_DEVICE_ID:
  3618. break;
  3619. default:
  3620. return 0;
  3621. }
  3622. mhi_ctrl = pci_priv->mhi_ctrl;
  3623. if (!mhi_ctrl)
  3624. return -EINVAL;
  3625. plat_priv = pci_priv->plat_priv;
  3626. if (!plat_priv)
  3627. return -ENODEV;
  3628. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3629. return -EAGAIN;
  3630. mhi_device_put(mhi_ctrl->mhi_dev);
  3631. return 0;
  3632. }
  3633. EXPORT_SYMBOL(cnss_pci_force_wake_release);
  3634. int cnss_pci_qmi_send_get(struct cnss_pci_data *pci_priv)
  3635. {
  3636. int ret = 0;
  3637. if (!pci_priv)
  3638. return -ENODEV;
  3639. mutex_lock(&pci_priv->bus_lock);
  3640. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3641. !pci_priv->qmi_send_usage_count)
  3642. ret = cnss_pci_resume_bus(pci_priv);
  3643. pci_priv->qmi_send_usage_count++;
  3644. cnss_pr_buf("Increased QMI send usage count to %d\n",
  3645. pci_priv->qmi_send_usage_count);
  3646. mutex_unlock(&pci_priv->bus_lock);
  3647. return ret;
  3648. }
  3649. int cnss_pci_qmi_send_put(struct cnss_pci_data *pci_priv)
  3650. {
  3651. int ret = 0;
  3652. if (!pci_priv)
  3653. return -ENODEV;
  3654. mutex_lock(&pci_priv->bus_lock);
  3655. if (pci_priv->qmi_send_usage_count)
  3656. pci_priv->qmi_send_usage_count--;
  3657. cnss_pr_buf("Decreased QMI send usage count to %d\n",
  3658. pci_priv->qmi_send_usage_count);
  3659. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3660. !pci_priv->qmi_send_usage_count &&
  3661. !cnss_pcie_is_device_down(pci_priv))
  3662. ret = cnss_pci_suspend_bus(pci_priv);
  3663. mutex_unlock(&pci_priv->bus_lock);
  3664. return ret;
  3665. }
  3666. int cnss_pci_alloc_fw_mem(struct cnss_pci_data *pci_priv)
  3667. {
  3668. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3669. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3670. struct device *dev = &pci_priv->pci_dev->dev;
  3671. int i;
  3672. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3673. if (!fw_mem[i].va && fw_mem[i].size) {
  3674. fw_mem[i].va =
  3675. dma_alloc_attrs(dev, fw_mem[i].size,
  3676. &fw_mem[i].pa, GFP_KERNEL,
  3677. fw_mem[i].attrs);
  3678. if (!fw_mem[i].va) {
  3679. cnss_pr_err("Failed to allocate memory for FW, size: 0x%zx, type: %u\n",
  3680. fw_mem[i].size, fw_mem[i].type);
  3681. return -ENOMEM;
  3682. }
  3683. }
  3684. }
  3685. return 0;
  3686. }
  3687. static void cnss_pci_free_fw_mem(struct cnss_pci_data *pci_priv)
  3688. {
  3689. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3690. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3691. struct device *dev = &pci_priv->pci_dev->dev;
  3692. int i;
  3693. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3694. if (fw_mem[i].va && fw_mem[i].size) {
  3695. cnss_pr_dbg("Freeing memory for FW, va: 0x%pK, pa: %pa, size: 0x%zx, type: %u\n",
  3696. fw_mem[i].va, &fw_mem[i].pa,
  3697. fw_mem[i].size, fw_mem[i].type);
  3698. dma_free_attrs(dev, fw_mem[i].size,
  3699. fw_mem[i].va, fw_mem[i].pa,
  3700. fw_mem[i].attrs);
  3701. fw_mem[i].va = NULL;
  3702. fw_mem[i].pa = 0;
  3703. fw_mem[i].size = 0;
  3704. fw_mem[i].type = 0;
  3705. }
  3706. }
  3707. plat_priv->fw_mem_seg_len = 0;
  3708. }
  3709. int cnss_pci_alloc_qdss_mem(struct cnss_pci_data *pci_priv)
  3710. {
  3711. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3712. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3713. int i, j;
  3714. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3715. if (!qdss_mem[i].va && qdss_mem[i].size) {
  3716. qdss_mem[i].va =
  3717. dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3718. qdss_mem[i].size,
  3719. &qdss_mem[i].pa,
  3720. GFP_KERNEL);
  3721. if (!qdss_mem[i].va) {
  3722. cnss_pr_err("Failed to allocate QDSS memory for FW, size: 0x%zx, type: %u, chuck-ID: %d\n",
  3723. qdss_mem[i].size,
  3724. qdss_mem[i].type, i);
  3725. break;
  3726. }
  3727. }
  3728. }
  3729. /* Best-effort allocation for QDSS trace */
  3730. if (i < plat_priv->qdss_mem_seg_len) {
  3731. for (j = i; j < plat_priv->qdss_mem_seg_len; j++) {
  3732. qdss_mem[j].type = 0;
  3733. qdss_mem[j].size = 0;
  3734. }
  3735. plat_priv->qdss_mem_seg_len = i;
  3736. }
  3737. return 0;
  3738. }
  3739. void cnss_pci_free_qdss_mem(struct cnss_pci_data *pci_priv)
  3740. {
  3741. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3742. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3743. int i;
  3744. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3745. if (qdss_mem[i].va && qdss_mem[i].size) {
  3746. cnss_pr_dbg("Freeing memory for QDSS: pa: %pa, size: 0x%zx, type: %u\n",
  3747. &qdss_mem[i].pa, qdss_mem[i].size,
  3748. qdss_mem[i].type);
  3749. dma_free_coherent(&pci_priv->pci_dev->dev,
  3750. qdss_mem[i].size, qdss_mem[i].va,
  3751. qdss_mem[i].pa);
  3752. qdss_mem[i].va = NULL;
  3753. qdss_mem[i].pa = 0;
  3754. qdss_mem[i].size = 0;
  3755. qdss_mem[i].type = 0;
  3756. }
  3757. }
  3758. plat_priv->qdss_mem_seg_len = 0;
  3759. }
  3760. int cnss_pci_load_m3(struct cnss_pci_data *pci_priv)
  3761. {
  3762. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3763. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3764. char filename[MAX_FIRMWARE_NAME_LEN];
  3765. char *phy_filename = DEFAULT_PHY_UCODE_FILE_NAME;
  3766. const struct firmware *fw_entry;
  3767. int ret = 0;
  3768. /* Use forward compatibility here since for any recent device
  3769. * it should use DEFAULT_PHY_UCODE_FILE_NAME.
  3770. */
  3771. switch (pci_priv->device_id) {
  3772. case QCA6174_DEVICE_ID:
  3773. cnss_pr_err("Invalid device ID (0x%x) to load phy image\n",
  3774. pci_priv->device_id);
  3775. return -EINVAL;
  3776. case QCA6290_DEVICE_ID:
  3777. case QCA6390_DEVICE_ID:
  3778. case QCA6490_DEVICE_ID:
  3779. phy_filename = DEFAULT_PHY_M3_FILE_NAME;
  3780. break;
  3781. case KIWI_DEVICE_ID:
  3782. switch (plat_priv->device_version.major_version) {
  3783. case FW_V2_NUMBER:
  3784. phy_filename = PHY_UCODE_V2_FILE_NAME;
  3785. break;
  3786. default:
  3787. break;
  3788. }
  3789. break;
  3790. default:
  3791. break;
  3792. }
  3793. if (!m3_mem->va && !m3_mem->size) {
  3794. cnss_pci_add_fw_prefix_name(pci_priv, filename,
  3795. phy_filename);
  3796. ret = firmware_request_nowarn(&fw_entry, filename,
  3797. &pci_priv->pci_dev->dev);
  3798. if (ret) {
  3799. cnss_pr_err("Failed to load M3 image: %s\n", filename);
  3800. return ret;
  3801. }
  3802. m3_mem->va = dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3803. fw_entry->size, &m3_mem->pa,
  3804. GFP_KERNEL);
  3805. if (!m3_mem->va) {
  3806. cnss_pr_err("Failed to allocate memory for M3, size: 0x%zx\n",
  3807. fw_entry->size);
  3808. release_firmware(fw_entry);
  3809. return -ENOMEM;
  3810. }
  3811. memcpy(m3_mem->va, fw_entry->data, fw_entry->size);
  3812. m3_mem->size = fw_entry->size;
  3813. release_firmware(fw_entry);
  3814. }
  3815. return 0;
  3816. }
  3817. static void cnss_pci_free_m3_mem(struct cnss_pci_data *pci_priv)
  3818. {
  3819. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3820. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3821. if (m3_mem->va && m3_mem->size) {
  3822. cnss_pr_dbg("Freeing memory for M3, va: 0x%pK, pa: %pa, size: 0x%zx\n",
  3823. m3_mem->va, &m3_mem->pa, m3_mem->size);
  3824. dma_free_coherent(&pci_priv->pci_dev->dev, m3_mem->size,
  3825. m3_mem->va, m3_mem->pa);
  3826. }
  3827. m3_mem->va = NULL;
  3828. m3_mem->pa = 0;
  3829. m3_mem->size = 0;
  3830. }
  3831. void cnss_pci_fw_boot_timeout_hdlr(struct cnss_pci_data *pci_priv)
  3832. {
  3833. struct cnss_plat_data *plat_priv;
  3834. if (!pci_priv)
  3835. return;
  3836. cnss_fatal_err("Timeout waiting for FW ready indication\n");
  3837. plat_priv = pci_priv->plat_priv;
  3838. if (!plat_priv)
  3839. return;
  3840. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  3841. cnss_pr_dbg("Ignore FW ready timeout for calibration mode\n");
  3842. return;
  3843. }
  3844. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  3845. CNSS_REASON_TIMEOUT);
  3846. }
  3847. static int cnss_pci_smmu_fault_handler(struct iommu_domain *domain,
  3848. struct device *dev, unsigned long iova,
  3849. int flags, void *handler_token)
  3850. {
  3851. struct cnss_pci_data *pci_priv = handler_token;
  3852. cnss_fatal_err("SMMU fault happened with IOVA 0x%lx\n", iova);
  3853. if (!pci_priv) {
  3854. cnss_pr_err("pci_priv is NULL\n");
  3855. return -ENODEV;
  3856. }
  3857. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  3858. cnss_force_fw_assert(&pci_priv->pci_dev->dev);
  3859. /* IOMMU driver requires -ENOSYS to print debug info. */
  3860. return -ENOSYS;
  3861. }
  3862. static int cnss_pci_init_smmu(struct cnss_pci_data *pci_priv)
  3863. {
  3864. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3865. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3866. struct device_node *of_node;
  3867. struct resource *res;
  3868. const char *iommu_dma_type;
  3869. u32 addr_win[2];
  3870. int ret = 0;
  3871. of_node = of_parse_phandle(pci_dev->dev.of_node, "qcom,iommu-group", 0);
  3872. if (!of_node)
  3873. return ret;
  3874. cnss_pr_dbg("Initializing SMMU\n");
  3875. pci_priv->iommu_domain = iommu_get_domain_for_dev(&pci_dev->dev);
  3876. ret = of_property_read_string(of_node, "qcom,iommu-dma",
  3877. &iommu_dma_type);
  3878. if (!ret && !strcmp("fastmap", iommu_dma_type)) {
  3879. cnss_pr_dbg("Enabling SMMU S1 stage\n");
  3880. pci_priv->smmu_s1_enable = true;
  3881. iommu_set_fault_handler(pci_priv->iommu_domain,
  3882. cnss_pci_smmu_fault_handler, pci_priv);
  3883. }
  3884. ret = of_property_read_u32_array(of_node, "qcom,iommu-dma-addr-pool",
  3885. addr_win, ARRAY_SIZE(addr_win));
  3886. if (ret) {
  3887. cnss_pr_err("Invalid SMMU size window, err = %d\n", ret);
  3888. of_node_put(of_node);
  3889. return ret;
  3890. }
  3891. pci_priv->smmu_iova_start = addr_win[0];
  3892. pci_priv->smmu_iova_len = addr_win[1];
  3893. cnss_pr_dbg("smmu_iova_start: %pa, smmu_iova_len: 0x%zx\n",
  3894. &pci_priv->smmu_iova_start,
  3895. pci_priv->smmu_iova_len);
  3896. res = platform_get_resource_byname(plat_priv->plat_dev, IORESOURCE_MEM,
  3897. "smmu_iova_ipa");
  3898. if (res) {
  3899. pci_priv->smmu_iova_ipa_start = res->start;
  3900. pci_priv->smmu_iova_ipa_current = res->start;
  3901. pci_priv->smmu_iova_ipa_len = resource_size(res);
  3902. cnss_pr_dbg("smmu_iova_ipa_start: %pa, smmu_iova_ipa_len: 0x%zx\n",
  3903. &pci_priv->smmu_iova_ipa_start,
  3904. pci_priv->smmu_iova_ipa_len);
  3905. }
  3906. pci_priv->iommu_geometry = of_property_read_bool(of_node,
  3907. "qcom,iommu-geometry");
  3908. cnss_pr_dbg("iommu_geometry: %d\n", pci_priv->iommu_geometry);
  3909. of_node_put(of_node);
  3910. return 0;
  3911. }
  3912. static void cnss_pci_deinit_smmu(struct cnss_pci_data *pci_priv)
  3913. {
  3914. pci_priv->iommu_domain = NULL;
  3915. }
  3916. int cnss_pci_get_iova(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3917. {
  3918. if (!pci_priv)
  3919. return -ENODEV;
  3920. if (!pci_priv->smmu_iova_len)
  3921. return -EINVAL;
  3922. *addr = pci_priv->smmu_iova_start;
  3923. *size = pci_priv->smmu_iova_len;
  3924. return 0;
  3925. }
  3926. int cnss_pci_get_iova_ipa(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3927. {
  3928. if (!pci_priv)
  3929. return -ENODEV;
  3930. if (!pci_priv->smmu_iova_ipa_len)
  3931. return -EINVAL;
  3932. *addr = pci_priv->smmu_iova_ipa_start;
  3933. *size = pci_priv->smmu_iova_ipa_len;
  3934. return 0;
  3935. }
  3936. struct iommu_domain *cnss_smmu_get_domain(struct device *dev)
  3937. {
  3938. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3939. if (!pci_priv)
  3940. return NULL;
  3941. return pci_priv->iommu_domain;
  3942. }
  3943. EXPORT_SYMBOL(cnss_smmu_get_domain);
  3944. int cnss_smmu_map(struct device *dev,
  3945. phys_addr_t paddr, uint32_t *iova_addr, size_t size)
  3946. {
  3947. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3948. struct cnss_plat_data *plat_priv;
  3949. unsigned long iova;
  3950. size_t len;
  3951. int ret = 0;
  3952. int flag = IOMMU_READ | IOMMU_WRITE;
  3953. struct pci_dev *root_port;
  3954. struct device_node *root_of_node;
  3955. bool dma_coherent = false;
  3956. if (!pci_priv)
  3957. return -ENODEV;
  3958. if (!iova_addr) {
  3959. cnss_pr_err("iova_addr is NULL, paddr %pa, size %zu\n",
  3960. &paddr, size);
  3961. return -EINVAL;
  3962. }
  3963. plat_priv = pci_priv->plat_priv;
  3964. len = roundup(size + paddr - rounddown(paddr, PAGE_SIZE), PAGE_SIZE);
  3965. iova = roundup(pci_priv->smmu_iova_ipa_current, PAGE_SIZE);
  3966. if (pci_priv->iommu_geometry &&
  3967. iova >= pci_priv->smmu_iova_ipa_start +
  3968. pci_priv->smmu_iova_ipa_len) {
  3969. cnss_pr_err("No IOVA space to map, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3970. iova,
  3971. &pci_priv->smmu_iova_ipa_start,
  3972. pci_priv->smmu_iova_ipa_len);
  3973. return -ENOMEM;
  3974. }
  3975. if (!test_bit(DISABLE_IO_COHERENCY,
  3976. &plat_priv->ctrl_params.quirks)) {
  3977. root_port = pcie_find_root_port(pci_priv->pci_dev);
  3978. if (!root_port) {
  3979. cnss_pr_err("Root port is null, so dma_coherent is disabled\n");
  3980. } else {
  3981. root_of_node = root_port->dev.of_node;
  3982. if (root_of_node && root_of_node->parent) {
  3983. dma_coherent =
  3984. of_property_read_bool(root_of_node->parent,
  3985. "dma-coherent");
  3986. cnss_pr_dbg("dma-coherent is %s\n",
  3987. dma_coherent ? "enabled" : "disabled");
  3988. if (dma_coherent)
  3989. flag |= IOMMU_CACHE;
  3990. }
  3991. }
  3992. }
  3993. cnss_pr_dbg("IOMMU map: iova %lx, len %zu\n", iova, len);
  3994. ret = iommu_map(pci_priv->iommu_domain, iova,
  3995. rounddown(paddr, PAGE_SIZE), len, flag);
  3996. if (ret) {
  3997. cnss_pr_err("PA to IOVA mapping failed, ret %d\n", ret);
  3998. return ret;
  3999. }
  4000. pci_priv->smmu_iova_ipa_current = iova + len;
  4001. *iova_addr = (uint32_t)(iova + paddr - rounddown(paddr, PAGE_SIZE));
  4002. cnss_pr_dbg("IOMMU map: iova_addr %lx\n", *iova_addr);
  4003. return 0;
  4004. }
  4005. EXPORT_SYMBOL(cnss_smmu_map);
  4006. int cnss_smmu_unmap(struct device *dev, uint32_t iova_addr, size_t size)
  4007. {
  4008. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4009. unsigned long iova;
  4010. size_t unmapped;
  4011. size_t len;
  4012. if (!pci_priv)
  4013. return -ENODEV;
  4014. iova = rounddown(iova_addr, PAGE_SIZE);
  4015. len = roundup(size + iova_addr - iova, PAGE_SIZE);
  4016. if (iova >= pci_priv->smmu_iova_ipa_start +
  4017. pci_priv->smmu_iova_ipa_len) {
  4018. cnss_pr_err("Out of IOVA space to unmap, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  4019. iova,
  4020. &pci_priv->smmu_iova_ipa_start,
  4021. pci_priv->smmu_iova_ipa_len);
  4022. return -ENOMEM;
  4023. }
  4024. cnss_pr_dbg("IOMMU unmap: iova %lx, len %zu\n", iova, len);
  4025. unmapped = iommu_unmap(pci_priv->iommu_domain, iova, len);
  4026. if (unmapped != len) {
  4027. cnss_pr_err("IOMMU unmap failed, unmapped = %zu, requested = %zu\n",
  4028. unmapped, len);
  4029. return -EINVAL;
  4030. }
  4031. pci_priv->smmu_iova_ipa_current = iova;
  4032. return 0;
  4033. }
  4034. EXPORT_SYMBOL(cnss_smmu_unmap);
  4035. int cnss_get_soc_info(struct device *dev, struct cnss_soc_info *info)
  4036. {
  4037. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4038. struct cnss_plat_data *plat_priv;
  4039. if (!pci_priv)
  4040. return -ENODEV;
  4041. plat_priv = pci_priv->plat_priv;
  4042. if (!plat_priv)
  4043. return -ENODEV;
  4044. info->va = pci_priv->bar;
  4045. info->pa = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4046. info->chip_id = plat_priv->chip_info.chip_id;
  4047. info->chip_family = plat_priv->chip_info.chip_family;
  4048. info->board_id = plat_priv->board_info.board_id;
  4049. info->soc_id = plat_priv->soc_info.soc_id;
  4050. info->fw_version = plat_priv->fw_version_info.fw_version;
  4051. strlcpy(info->fw_build_timestamp,
  4052. plat_priv->fw_version_info.fw_build_timestamp,
  4053. sizeof(info->fw_build_timestamp));
  4054. memcpy(&info->device_version, &plat_priv->device_version,
  4055. sizeof(info->device_version));
  4056. memcpy(&info->dev_mem_info, &plat_priv->dev_mem_info,
  4057. sizeof(info->dev_mem_info));
  4058. return 0;
  4059. }
  4060. EXPORT_SYMBOL(cnss_get_soc_info);
  4061. static struct cnss_msi_config msi_config = {
  4062. .total_vectors = 32,
  4063. .total_users = 4,
  4064. .users = (struct cnss_msi_user[]) {
  4065. { .name = "MHI", .num_vectors = 3, .base_vector = 0 },
  4066. { .name = "CE", .num_vectors = 10, .base_vector = 3 },
  4067. { .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
  4068. { .name = "DP", .num_vectors = 18, .base_vector = 14 },
  4069. },
  4070. };
  4071. static int cnss_pci_get_msi_assignment(struct cnss_pci_data *pci_priv)
  4072. {
  4073. pci_priv->msi_config = &msi_config;
  4074. return 0;
  4075. }
  4076. static int cnss_pci_enable_msi(struct cnss_pci_data *pci_priv)
  4077. {
  4078. int ret = 0;
  4079. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4080. int num_vectors;
  4081. struct cnss_msi_config *msi_config;
  4082. struct msi_desc *msi_desc;
  4083. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4084. return 0;
  4085. ret = cnss_pci_get_msi_assignment(pci_priv);
  4086. if (ret) {
  4087. cnss_pr_err("Failed to get MSI assignment, err = %d\n", ret);
  4088. goto out;
  4089. }
  4090. msi_config = pci_priv->msi_config;
  4091. if (!msi_config) {
  4092. cnss_pr_err("msi_config is NULL!\n");
  4093. ret = -EINVAL;
  4094. goto out;
  4095. }
  4096. num_vectors = pci_alloc_irq_vectors(pci_dev,
  4097. msi_config->total_vectors,
  4098. msi_config->total_vectors,
  4099. PCI_IRQ_MSI);
  4100. if (num_vectors != msi_config->total_vectors) {
  4101. cnss_pr_err("Failed to get enough MSI vectors (%d), available vectors = %d",
  4102. msi_config->total_vectors, num_vectors);
  4103. if (num_vectors >= 0)
  4104. ret = -EINVAL;
  4105. goto reset_msi_config;
  4106. }
  4107. msi_desc = irq_get_msi_desc(pci_dev->irq);
  4108. if (!msi_desc) {
  4109. cnss_pr_err("msi_desc is NULL!\n");
  4110. ret = -EINVAL;
  4111. goto free_msi_vector;
  4112. }
  4113. pci_priv->msi_ep_base_data = msi_desc->msg.data;
  4114. cnss_pr_dbg("MSI base data is %d\n", pci_priv->msi_ep_base_data);
  4115. return 0;
  4116. free_msi_vector:
  4117. pci_free_irq_vectors(pci_priv->pci_dev);
  4118. reset_msi_config:
  4119. pci_priv->msi_config = NULL;
  4120. out:
  4121. return ret;
  4122. }
  4123. static void cnss_pci_disable_msi(struct cnss_pci_data *pci_priv)
  4124. {
  4125. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4126. return;
  4127. pci_free_irq_vectors(pci_priv->pci_dev);
  4128. }
  4129. int cnss_get_user_msi_assignment(struct device *dev, char *user_name,
  4130. int *num_vectors, u32 *user_base_data,
  4131. u32 *base_vector)
  4132. {
  4133. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4134. struct cnss_msi_config *msi_config;
  4135. int idx;
  4136. if (!pci_priv)
  4137. return -ENODEV;
  4138. msi_config = pci_priv->msi_config;
  4139. if (!msi_config) {
  4140. cnss_pr_err("MSI is not supported.\n");
  4141. return -EINVAL;
  4142. }
  4143. for (idx = 0; idx < msi_config->total_users; idx++) {
  4144. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  4145. *num_vectors = msi_config->users[idx].num_vectors;
  4146. *user_base_data = msi_config->users[idx].base_vector
  4147. + pci_priv->msi_ep_base_data;
  4148. *base_vector = msi_config->users[idx].base_vector;
  4149. cnss_pr_dbg("Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  4150. user_name, *num_vectors, *user_base_data,
  4151. *base_vector);
  4152. return 0;
  4153. }
  4154. }
  4155. cnss_pr_err("Failed to find MSI assignment for %s!\n", user_name);
  4156. return -EINVAL;
  4157. }
  4158. EXPORT_SYMBOL(cnss_get_user_msi_assignment);
  4159. int cnss_get_msi_irq(struct device *dev, unsigned int vector)
  4160. {
  4161. struct pci_dev *pci_dev = to_pci_dev(dev);
  4162. int irq_num;
  4163. irq_num = pci_irq_vector(pci_dev, vector);
  4164. cnss_pr_dbg("Get IRQ number %d for vector index %d\n", irq_num, vector);
  4165. return irq_num;
  4166. }
  4167. EXPORT_SYMBOL(cnss_get_msi_irq);
  4168. void cnss_get_msi_address(struct device *dev, u32 *msi_addr_low,
  4169. u32 *msi_addr_high)
  4170. {
  4171. struct pci_dev *pci_dev = to_pci_dev(dev);
  4172. u16 control;
  4173. pci_read_config_word(pci_dev, pci_dev->msi_cap + PCI_MSI_FLAGS,
  4174. &control);
  4175. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  4176. msi_addr_low);
  4177. /* Return MSI high address only when device supports 64-bit MSI */
  4178. if (control & PCI_MSI_FLAGS_64BIT)
  4179. pci_read_config_dword(pci_dev,
  4180. pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  4181. msi_addr_high);
  4182. else
  4183. *msi_addr_high = 0;
  4184. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  4185. *msi_addr_low, *msi_addr_high);
  4186. }
  4187. EXPORT_SYMBOL(cnss_get_msi_address);
  4188. u32 cnss_pci_get_wake_msi(struct cnss_pci_data *pci_priv)
  4189. {
  4190. int ret, num_vectors;
  4191. u32 user_base_data, base_vector;
  4192. if (!pci_priv)
  4193. return -ENODEV;
  4194. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4195. WAKE_MSI_NAME, &num_vectors,
  4196. &user_base_data, &base_vector);
  4197. if (ret) {
  4198. cnss_pr_err("WAKE MSI is not valid\n");
  4199. return 0;
  4200. }
  4201. return user_base_data;
  4202. }
  4203. static int cnss_pci_enable_bus(struct cnss_pci_data *pci_priv)
  4204. {
  4205. int ret = 0;
  4206. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4207. u16 device_id;
  4208. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
  4209. if (device_id != pci_priv->pci_device_id->device) {
  4210. cnss_pr_err("PCI device ID mismatch, config ID: 0x%x, probe ID: 0x%x\n",
  4211. device_id, pci_priv->pci_device_id->device);
  4212. ret = -EIO;
  4213. goto out;
  4214. }
  4215. ret = pci_assign_resource(pci_dev, PCI_BAR_NUM);
  4216. if (ret) {
  4217. pr_err("Failed to assign PCI resource, err = %d\n", ret);
  4218. goto out;
  4219. }
  4220. ret = pci_enable_device(pci_dev);
  4221. if (ret) {
  4222. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  4223. goto out;
  4224. }
  4225. ret = pci_request_region(pci_dev, PCI_BAR_NUM, "cnss");
  4226. if (ret) {
  4227. cnss_pr_err("Failed to request PCI region, err = %d\n", ret);
  4228. goto disable_device;
  4229. }
  4230. switch (device_id) {
  4231. case QCA6174_DEVICE_ID:
  4232. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4233. break;
  4234. case QCA6390_DEVICE_ID:
  4235. case QCA6490_DEVICE_ID:
  4236. case KIWI_DEVICE_ID:
  4237. pci_priv->dma_bit_mask = PCI_DMA_MASK_36_BIT;
  4238. break;
  4239. default:
  4240. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4241. break;
  4242. }
  4243. cnss_pr_dbg("Set PCI DMA MASK (0x%llx)\n", pci_priv->dma_bit_mask);
  4244. ret = pci_set_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4245. if (ret) {
  4246. cnss_pr_err("Failed to set PCI DMA mask, err = %d\n", ret);
  4247. goto release_region;
  4248. }
  4249. ret = pci_set_consistent_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4250. if (ret) {
  4251. cnss_pr_err("Failed to set PCI consistent DMA mask, err = %d\n",
  4252. ret);
  4253. goto release_region;
  4254. }
  4255. pci_priv->bar = pci_iomap(pci_dev, PCI_BAR_NUM, 0);
  4256. if (!pci_priv->bar) {
  4257. cnss_pr_err("Failed to do PCI IO map!\n");
  4258. ret = -EIO;
  4259. goto release_region;
  4260. }
  4261. /* Save default config space without BME enabled */
  4262. pci_save_state(pci_dev);
  4263. pci_priv->default_state = pci_store_saved_state(pci_dev);
  4264. pci_set_master(pci_dev);
  4265. return 0;
  4266. release_region:
  4267. pci_release_region(pci_dev, PCI_BAR_NUM);
  4268. disable_device:
  4269. pci_disable_device(pci_dev);
  4270. out:
  4271. return ret;
  4272. }
  4273. static void cnss_pci_disable_bus(struct cnss_pci_data *pci_priv)
  4274. {
  4275. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4276. pci_clear_master(pci_dev);
  4277. pci_load_and_free_saved_state(pci_dev, &pci_priv->saved_state);
  4278. pci_load_and_free_saved_state(pci_dev, &pci_priv->default_state);
  4279. if (pci_priv->bar) {
  4280. pci_iounmap(pci_dev, pci_priv->bar);
  4281. pci_priv->bar = NULL;
  4282. }
  4283. pci_release_region(pci_dev, PCI_BAR_NUM);
  4284. if (pci_is_enabled(pci_dev))
  4285. pci_disable_device(pci_dev);
  4286. }
  4287. static void cnss_pci_dump_qdss_reg(struct cnss_pci_data *pci_priv)
  4288. {
  4289. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4290. int i, array_size = ARRAY_SIZE(qdss_csr) - 1;
  4291. gfp_t gfp = GFP_KERNEL;
  4292. u32 reg_offset;
  4293. if (in_interrupt() || irqs_disabled())
  4294. gfp = GFP_ATOMIC;
  4295. if (!plat_priv->qdss_reg) {
  4296. plat_priv->qdss_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  4297. sizeof(*plat_priv->qdss_reg)
  4298. * array_size, gfp);
  4299. if (!plat_priv->qdss_reg)
  4300. return;
  4301. }
  4302. cnss_pr_dbg("Start to dump qdss registers\n");
  4303. for (i = 0; qdss_csr[i].name; i++) {
  4304. reg_offset = QDSS_APB_DEC_CSR_BASE + qdss_csr[i].offset;
  4305. if (cnss_pci_reg_read(pci_priv, reg_offset,
  4306. &plat_priv->qdss_reg[i]))
  4307. return;
  4308. cnss_pr_dbg("%s[0x%x] = 0x%x\n", qdss_csr[i].name, reg_offset,
  4309. plat_priv->qdss_reg[i]);
  4310. }
  4311. }
  4312. static void cnss_pci_dump_ce_reg(struct cnss_pci_data *pci_priv,
  4313. enum cnss_ce_index ce)
  4314. {
  4315. int i;
  4316. u32 ce_base = ce * CE_REG_INTERVAL;
  4317. u32 reg_offset, src_ring_base, dst_ring_base, cmn_base, val;
  4318. switch (pci_priv->device_id) {
  4319. case QCA6390_DEVICE_ID:
  4320. src_ring_base = QCA6390_CE_SRC_RING_REG_BASE;
  4321. dst_ring_base = QCA6390_CE_DST_RING_REG_BASE;
  4322. cmn_base = QCA6390_CE_COMMON_REG_BASE;
  4323. break;
  4324. case QCA6490_DEVICE_ID:
  4325. src_ring_base = QCA6490_CE_SRC_RING_REG_BASE;
  4326. dst_ring_base = QCA6490_CE_DST_RING_REG_BASE;
  4327. cmn_base = QCA6490_CE_COMMON_REG_BASE;
  4328. break;
  4329. default:
  4330. return;
  4331. }
  4332. switch (ce) {
  4333. case CNSS_CE_09:
  4334. case CNSS_CE_10:
  4335. for (i = 0; ce_src[i].name; i++) {
  4336. reg_offset = src_ring_base + ce_base + ce_src[i].offset;
  4337. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4338. return;
  4339. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4340. ce, ce_src[i].name, reg_offset, val);
  4341. }
  4342. for (i = 0; ce_dst[i].name; i++) {
  4343. reg_offset = dst_ring_base + ce_base + ce_dst[i].offset;
  4344. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4345. return;
  4346. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4347. ce, ce_dst[i].name, reg_offset, val);
  4348. }
  4349. break;
  4350. case CNSS_CE_COMMON:
  4351. for (i = 0; ce_cmn[i].name; i++) {
  4352. reg_offset = cmn_base + ce_cmn[i].offset;
  4353. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4354. return;
  4355. cnss_pr_dbg("CE_COMMON_%s[0x%x] = 0x%x\n",
  4356. ce_cmn[i].name, reg_offset, val);
  4357. }
  4358. break;
  4359. default:
  4360. cnss_pr_err("Unsupported CE[%d] registers dump\n", ce);
  4361. }
  4362. }
  4363. static void cnss_pci_dump_debug_reg(struct cnss_pci_data *pci_priv)
  4364. {
  4365. if (cnss_pci_check_link_status(pci_priv))
  4366. return;
  4367. cnss_pr_dbg("Start to dump debug registers\n");
  4368. cnss_mhi_debug_reg_dump(pci_priv);
  4369. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4370. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_COMMON);
  4371. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_09);
  4372. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_10);
  4373. }
  4374. static int cnss_pci_assert_host_sol(struct cnss_pci_data *pci_priv)
  4375. {
  4376. if (cnss_get_host_sol_value(pci_priv->plat_priv))
  4377. return -EINVAL;
  4378. cnss_pr_dbg("Assert host SOL GPIO to retry RDDM, expecting link down\n");
  4379. cnss_set_host_sol_value(pci_priv->plat_priv, 1);
  4380. return 0;
  4381. }
  4382. int cnss_pci_force_fw_assert_hdlr(struct cnss_pci_data *pci_priv)
  4383. {
  4384. int ret;
  4385. struct cnss_plat_data *plat_priv;
  4386. if (!pci_priv)
  4387. return -ENODEV;
  4388. plat_priv = pci_priv->plat_priv;
  4389. if (!plat_priv)
  4390. return -ENODEV;
  4391. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4392. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state))
  4393. return -EINVAL;
  4394. cnss_auto_resume(&pci_priv->pci_dev->dev);
  4395. if (!cnss_pci_check_link_status(pci_priv))
  4396. cnss_mhi_debug_reg_dump(pci_priv);
  4397. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4398. cnss_pci_dump_misc_reg(pci_priv);
  4399. cnss_pci_dump_shadow_reg(pci_priv);
  4400. /* If link is still down here, directly trigger link down recovery */
  4401. ret = cnss_pci_check_link_status(pci_priv);
  4402. if (ret) {
  4403. cnss_pci_link_down(&pci_priv->pci_dev->dev);
  4404. return 0;
  4405. }
  4406. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_TRIGGER_RDDM);
  4407. if (ret) {
  4408. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4409. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state)) {
  4410. cnss_pr_dbg("MHI is not powered on, ignore RDDM failure\n");
  4411. return 0;
  4412. }
  4413. cnss_fatal_err("Failed to trigger RDDM, err = %d\n", ret);
  4414. if (!cnss_pci_assert_host_sol(pci_priv))
  4415. return 0;
  4416. cnss_pci_dump_debug_reg(pci_priv);
  4417. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4418. CNSS_REASON_DEFAULT);
  4419. return ret;
  4420. }
  4421. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  4422. mod_timer(&pci_priv->dev_rddm_timer,
  4423. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4424. }
  4425. return 0;
  4426. }
  4427. static void cnss_pci_add_dump_seg(struct cnss_pci_data *pci_priv,
  4428. struct cnss_dump_seg *dump_seg,
  4429. enum cnss_fw_dump_type type, int seg_no,
  4430. void *va, dma_addr_t dma, size_t size)
  4431. {
  4432. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4433. struct device *dev = &pci_priv->pci_dev->dev;
  4434. phys_addr_t pa;
  4435. dump_seg->address = dma;
  4436. dump_seg->v_address = va;
  4437. dump_seg->size = size;
  4438. dump_seg->type = type;
  4439. cnss_pr_dbg("Seg: %x, va: %pK, dma: %pa, size: 0x%zx\n",
  4440. seg_no, va, &dma, size);
  4441. if (cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS))
  4442. return;
  4443. cnss_minidump_add_region(plat_priv, type, seg_no, va, pa, size);
  4444. }
  4445. static void cnss_pci_remove_dump_seg(struct cnss_pci_data *pci_priv,
  4446. struct cnss_dump_seg *dump_seg,
  4447. enum cnss_fw_dump_type type, int seg_no,
  4448. void *va, dma_addr_t dma, size_t size)
  4449. {
  4450. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4451. struct device *dev = &pci_priv->pci_dev->dev;
  4452. phys_addr_t pa;
  4453. cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS);
  4454. cnss_minidump_remove_region(plat_priv, type, seg_no, va, pa, size);
  4455. }
  4456. int cnss_pci_call_driver_uevent(struct cnss_pci_data *pci_priv,
  4457. enum cnss_driver_status status, void *data)
  4458. {
  4459. struct cnss_uevent_data uevent_data;
  4460. struct cnss_wlan_driver *driver_ops;
  4461. driver_ops = pci_priv->driver_ops;
  4462. if (!driver_ops || !driver_ops->update_event) {
  4463. cnss_pr_dbg("Hang event driver ops is NULL\n");
  4464. return -EINVAL;
  4465. }
  4466. cnss_pr_dbg("Calling driver uevent: %d\n", status);
  4467. uevent_data.status = status;
  4468. uevent_data.data = data;
  4469. return driver_ops->update_event(pci_priv->pci_dev, &uevent_data);
  4470. }
  4471. static void cnss_pci_send_hang_event(struct cnss_pci_data *pci_priv)
  4472. {
  4473. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4474. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4475. struct cnss_hang_event hang_event;
  4476. void *hang_data_va = NULL;
  4477. u64 offset = 0;
  4478. int i = 0;
  4479. if (!fw_mem || !plat_priv->fw_mem_seg_len)
  4480. return;
  4481. memset(&hang_event, 0, sizeof(hang_event));
  4482. switch (pci_priv->device_id) {
  4483. case QCA6390_DEVICE_ID:
  4484. offset = HST_HANG_DATA_OFFSET;
  4485. break;
  4486. case QCA6490_DEVICE_ID:
  4487. offset = HSP_HANG_DATA_OFFSET;
  4488. break;
  4489. default:
  4490. cnss_pr_err("Skip Hang Event Data as unsupported Device ID received: %d\n",
  4491. pci_priv->device_id);
  4492. return;
  4493. }
  4494. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4495. if (fw_mem[i].type == QMI_WLFW_MEM_TYPE_DDR_V01 &&
  4496. fw_mem[i].va) {
  4497. hang_data_va = fw_mem[i].va + offset;
  4498. hang_event.hang_event_data = kmemdup(hang_data_va,
  4499. HANG_DATA_LENGTH,
  4500. GFP_ATOMIC);
  4501. if (!hang_event.hang_event_data) {
  4502. cnss_pr_dbg("Hang data memory alloc failed\n");
  4503. return;
  4504. }
  4505. hang_event.hang_event_data_len = HANG_DATA_LENGTH;
  4506. break;
  4507. }
  4508. }
  4509. cnss_pci_call_driver_uevent(pci_priv, CNSS_HANG_EVENT, &hang_event);
  4510. kfree(hang_event.hang_event_data);
  4511. hang_event.hang_event_data = NULL;
  4512. }
  4513. void cnss_pci_collect_dump_info(struct cnss_pci_data *pci_priv, bool in_panic)
  4514. {
  4515. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4516. struct cnss_dump_data *dump_data =
  4517. &plat_priv->ramdump_info_v2.dump_data;
  4518. struct cnss_dump_seg *dump_seg =
  4519. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4520. struct image_info *fw_image, *rddm_image;
  4521. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4522. int ret, i, j;
  4523. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  4524. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  4525. cnss_pci_send_hang_event(pci_priv);
  4526. if (test_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state)) {
  4527. cnss_pr_dbg("RAM dump is already collected, skip\n");
  4528. return;
  4529. }
  4530. if (!cnss_is_device_powered_on(plat_priv)) {
  4531. cnss_pr_dbg("Device is already powered off, skip\n");
  4532. return;
  4533. }
  4534. if (!in_panic) {
  4535. mutex_lock(&pci_priv->bus_lock);
  4536. ret = cnss_pci_check_link_status(pci_priv);
  4537. if (ret) {
  4538. if (ret != -EACCES) {
  4539. mutex_unlock(&pci_priv->bus_lock);
  4540. return;
  4541. }
  4542. if (cnss_pci_resume_bus(pci_priv)) {
  4543. mutex_unlock(&pci_priv->bus_lock);
  4544. return;
  4545. }
  4546. }
  4547. mutex_unlock(&pci_priv->bus_lock);
  4548. } else {
  4549. if (cnss_pci_check_link_status(pci_priv))
  4550. return;
  4551. /* Inside panic handler, reduce timeout for RDDM to avoid
  4552. * unnecessary hypervisor watchdog bite.
  4553. */
  4554. pci_priv->mhi_ctrl->timeout_ms /= 2;
  4555. }
  4556. cnss_mhi_debug_reg_dump(pci_priv);
  4557. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4558. cnss_pci_dump_misc_reg(pci_priv);
  4559. cnss_pci_dump_shadow_reg(pci_priv);
  4560. cnss_pci_dump_qdss_reg(pci_priv);
  4561. ret = mhi_download_rddm_image(pci_priv->mhi_ctrl, in_panic);
  4562. if (ret) {
  4563. cnss_fatal_err("Failed to download RDDM image, err = %d\n",
  4564. ret);
  4565. if (!cnss_pci_assert_host_sol(pci_priv))
  4566. return;
  4567. cnss_pci_dump_debug_reg(pci_priv);
  4568. return;
  4569. }
  4570. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4571. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4572. dump_data->nentries = 0;
  4573. cnss_mhi_dump_sfr(pci_priv);
  4574. if (!dump_seg) {
  4575. cnss_pr_warn("FW image dump collection not setup");
  4576. goto skip_dump;
  4577. }
  4578. cnss_pr_dbg("Collect FW image dump segment, nentries %d\n",
  4579. fw_image->entries);
  4580. for (i = 0; i < fw_image->entries; i++) {
  4581. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4582. fw_image->mhi_buf[i].buf,
  4583. fw_image->mhi_buf[i].dma_addr,
  4584. fw_image->mhi_buf[i].len);
  4585. dump_seg++;
  4586. }
  4587. dump_data->nentries += fw_image->entries;
  4588. cnss_pr_dbg("Collect RDDM image dump segment, nentries %d\n",
  4589. rddm_image->entries);
  4590. for (i = 0; i < rddm_image->entries; i++) {
  4591. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4592. rddm_image->mhi_buf[i].buf,
  4593. rddm_image->mhi_buf[i].dma_addr,
  4594. rddm_image->mhi_buf[i].len);
  4595. dump_seg++;
  4596. }
  4597. dump_data->nentries += rddm_image->entries;
  4598. cnss_pr_dbg("Collect remote heap dump segment\n");
  4599. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4600. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4601. cnss_pci_add_dump_seg(pci_priv, dump_seg,
  4602. CNSS_FW_REMOTE_HEAP, j,
  4603. fw_mem[i].va, fw_mem[i].pa,
  4604. fw_mem[i].size);
  4605. dump_seg++;
  4606. dump_data->nentries++;
  4607. j++;
  4608. }
  4609. }
  4610. if (dump_data->nentries > 0)
  4611. plat_priv->ramdump_info_v2.dump_data_valid = true;
  4612. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RDDM_DONE);
  4613. skip_dump:
  4614. complete(&plat_priv->rddm_complete);
  4615. }
  4616. void cnss_pci_clear_dump_info(struct cnss_pci_data *pci_priv)
  4617. {
  4618. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4619. struct cnss_dump_seg *dump_seg =
  4620. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4621. struct image_info *fw_image, *rddm_image;
  4622. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4623. int i, j;
  4624. if (!dump_seg)
  4625. return;
  4626. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4627. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4628. for (i = 0; i < fw_image->entries; i++) {
  4629. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4630. fw_image->mhi_buf[i].buf,
  4631. fw_image->mhi_buf[i].dma_addr,
  4632. fw_image->mhi_buf[i].len);
  4633. dump_seg++;
  4634. }
  4635. for (i = 0; i < rddm_image->entries; i++) {
  4636. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4637. rddm_image->mhi_buf[i].buf,
  4638. rddm_image->mhi_buf[i].dma_addr,
  4639. rddm_image->mhi_buf[i].len);
  4640. dump_seg++;
  4641. }
  4642. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4643. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4644. cnss_pci_remove_dump_seg(pci_priv, dump_seg,
  4645. CNSS_FW_REMOTE_HEAP, j,
  4646. fw_mem[i].va, fw_mem[i].pa,
  4647. fw_mem[i].size);
  4648. dump_seg++;
  4649. j++;
  4650. }
  4651. }
  4652. plat_priv->ramdump_info_v2.dump_data.nentries = 0;
  4653. plat_priv->ramdump_info_v2.dump_data_valid = false;
  4654. }
  4655. void cnss_pci_device_crashed(struct cnss_pci_data *pci_priv)
  4656. {
  4657. if (!pci_priv)
  4658. return;
  4659. cnss_device_crashed(&pci_priv->pci_dev->dev);
  4660. }
  4661. static int cnss_mhi_pm_runtime_get(struct mhi_controller *mhi_ctrl)
  4662. {
  4663. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4664. return cnss_pci_pm_runtime_get(pci_priv, RTPM_ID_MHI);
  4665. }
  4666. static void cnss_mhi_pm_runtime_put_noidle(struct mhi_controller *mhi_ctrl)
  4667. {
  4668. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4669. cnss_pci_pm_runtime_put_noidle(pci_priv, RTPM_ID_MHI);
  4670. }
  4671. void cnss_pci_add_fw_prefix_name(struct cnss_pci_data *pci_priv,
  4672. char *prefix_name, char *name)
  4673. {
  4674. struct cnss_plat_data *plat_priv;
  4675. if (!pci_priv)
  4676. return;
  4677. plat_priv = pci_priv->plat_priv;
  4678. if (!plat_priv->use_fw_path_with_prefix) {
  4679. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4680. return;
  4681. }
  4682. switch (pci_priv->device_id) {
  4683. case QCA6390_DEVICE_ID:
  4684. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4685. QCA6390_PATH_PREFIX "%s", name);
  4686. break;
  4687. case QCA6490_DEVICE_ID:
  4688. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4689. QCA6490_PATH_PREFIX "%s", name);
  4690. break;
  4691. case KIWI_DEVICE_ID:
  4692. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4693. KIWI_PATH_PREFIX "%s", name);
  4694. break;
  4695. default:
  4696. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4697. break;
  4698. }
  4699. cnss_pr_dbg("FW name added with prefix: %s\n", prefix_name);
  4700. }
  4701. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv)
  4702. {
  4703. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4704. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  4705. plat_priv->device_version.family_number = mhi_ctrl->family_number;
  4706. plat_priv->device_version.device_number = mhi_ctrl->device_number;
  4707. plat_priv->device_version.major_version = mhi_ctrl->major_version;
  4708. plat_priv->device_version.minor_version = mhi_ctrl->minor_version;
  4709. cnss_pr_dbg("Get device version info, family number: 0x%x, device number: 0x%x, major version: 0x%x, minor version: 0x%x\n",
  4710. plat_priv->device_version.family_number,
  4711. plat_priv->device_version.device_number,
  4712. plat_priv->device_version.major_version,
  4713. plat_priv->device_version.minor_version);
  4714. /* Only keep lower 4 bits as real device major version */
  4715. plat_priv->device_version.major_version &= DEVICE_MAJOR_VERSION_MASK;
  4716. switch (pci_priv->device_id) {
  4717. case QCA6390_DEVICE_ID:
  4718. if (plat_priv->device_version.major_version < FW_V2_NUMBER) {
  4719. cnss_pr_dbg("Device ID:version (0x%lx:%d) is not supported\n",
  4720. pci_priv->device_id,
  4721. plat_priv->device_version.major_version);
  4722. return -EINVAL;
  4723. }
  4724. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4725. FW_V2_FILE_NAME);
  4726. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4727. FW_V2_FILE_NAME);
  4728. break;
  4729. case QCA6490_DEVICE_ID:
  4730. case KIWI_DEVICE_ID:
  4731. switch (plat_priv->device_version.major_version) {
  4732. case FW_V2_NUMBER:
  4733. cnss_pci_add_fw_prefix_name(pci_priv,
  4734. plat_priv->firmware_name,
  4735. FW_V2_FILE_NAME);
  4736. snprintf(plat_priv->fw_fallback_name,
  4737. MAX_FIRMWARE_NAME_LEN,
  4738. FW_V2_FILE_NAME);
  4739. break;
  4740. default:
  4741. cnss_pci_add_fw_prefix_name(pci_priv,
  4742. plat_priv->firmware_name,
  4743. DEFAULT_FW_FILE_NAME);
  4744. snprintf(plat_priv->fw_fallback_name,
  4745. MAX_FIRMWARE_NAME_LEN,
  4746. DEFAULT_FW_FILE_NAME);
  4747. break;
  4748. }
  4749. break;
  4750. default:
  4751. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4752. DEFAULT_FW_FILE_NAME);
  4753. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4754. DEFAULT_FW_FILE_NAME);
  4755. break;
  4756. }
  4757. cnss_pr_dbg("FW name is %s, FW fallback name is %s\n",
  4758. plat_priv->firmware_name, plat_priv->fw_fallback_name);
  4759. return 0;
  4760. }
  4761. static char *cnss_mhi_notify_status_to_str(enum mhi_callback status)
  4762. {
  4763. switch (status) {
  4764. case MHI_CB_IDLE:
  4765. return "IDLE";
  4766. case MHI_CB_EE_RDDM:
  4767. return "RDDM";
  4768. case MHI_CB_SYS_ERROR:
  4769. return "SYS_ERROR";
  4770. case MHI_CB_FATAL_ERROR:
  4771. return "FATAL_ERROR";
  4772. case MHI_CB_EE_MISSION_MODE:
  4773. return "MISSION_MODE";
  4774. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4775. case MHI_CB_FALLBACK_IMG:
  4776. return "FW_FALLBACK";
  4777. #endif
  4778. default:
  4779. return "UNKNOWN";
  4780. }
  4781. };
  4782. static void cnss_dev_rddm_timeout_hdlr(struct timer_list *t)
  4783. {
  4784. struct cnss_pci_data *pci_priv =
  4785. from_timer(pci_priv, t, dev_rddm_timer);
  4786. enum mhi_ee_type mhi_ee;
  4787. if (!pci_priv)
  4788. return;
  4789. cnss_fatal_err("Timeout waiting for RDDM notification\n");
  4790. if (!cnss_pci_assert_host_sol(pci_priv))
  4791. return;
  4792. mhi_ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  4793. if (mhi_ee == MHI_EE_PBL)
  4794. cnss_pr_err("Unable to collect ramdumps due to abrupt reset\n");
  4795. if (mhi_ee == MHI_EE_RDDM) {
  4796. cnss_pr_info("Device MHI EE is RDDM, try to collect dump\n");
  4797. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4798. CNSS_REASON_RDDM);
  4799. } else {
  4800. cnss_mhi_debug_reg_dump(pci_priv);
  4801. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4802. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4803. CNSS_REASON_TIMEOUT);
  4804. }
  4805. }
  4806. static void cnss_boot_debug_timeout_hdlr(struct timer_list *t)
  4807. {
  4808. struct cnss_pci_data *pci_priv =
  4809. from_timer(pci_priv, t, boot_debug_timer);
  4810. if (!pci_priv)
  4811. return;
  4812. if (cnss_pci_check_link_status(pci_priv))
  4813. return;
  4814. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  4815. return;
  4816. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  4817. return;
  4818. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE))
  4819. return;
  4820. cnss_pr_dbg("Dump MHI/PBL/SBL debug data every %ds during MHI power on\n",
  4821. BOOT_DEBUG_TIMEOUT_MS / 1000);
  4822. cnss_mhi_debug_reg_dump(pci_priv);
  4823. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4824. cnss_pci_dump_bl_sram_mem(pci_priv);
  4825. mod_timer(&pci_priv->boot_debug_timer,
  4826. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  4827. }
  4828. static int cnss_pci_handle_mhi_sys_err(struct cnss_pci_data *pci_priv)
  4829. {
  4830. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4831. cnss_ignore_qmi_failure(true);
  4832. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4833. del_timer(&plat_priv->fw_boot_timer);
  4834. mod_timer(&pci_priv->dev_rddm_timer,
  4835. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4836. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4837. return 0;
  4838. }
  4839. int cnss_pci_handle_dev_sol_irq(struct cnss_pci_data *pci_priv)
  4840. {
  4841. return cnss_pci_handle_mhi_sys_err(pci_priv);
  4842. }
  4843. static void cnss_mhi_notify_status(struct mhi_controller *mhi_ctrl,
  4844. enum mhi_callback reason)
  4845. {
  4846. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4847. struct cnss_plat_data *plat_priv;
  4848. enum cnss_recovery_reason cnss_reason;
  4849. if (!pci_priv) {
  4850. cnss_pr_err("pci_priv is NULL");
  4851. return;
  4852. }
  4853. plat_priv = pci_priv->plat_priv;
  4854. if (reason != MHI_CB_IDLE)
  4855. cnss_pr_dbg("MHI status cb is called with reason %s(%d)\n",
  4856. cnss_mhi_notify_status_to_str(reason), reason);
  4857. switch (reason) {
  4858. case MHI_CB_IDLE:
  4859. case MHI_CB_EE_MISSION_MODE:
  4860. return;
  4861. case MHI_CB_FATAL_ERROR:
  4862. cnss_ignore_qmi_failure(true);
  4863. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4864. del_timer(&plat_priv->fw_boot_timer);
  4865. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4866. cnss_reason = CNSS_REASON_DEFAULT;
  4867. break;
  4868. case MHI_CB_SYS_ERROR:
  4869. cnss_pci_handle_mhi_sys_err(pci_priv);
  4870. return;
  4871. case MHI_CB_EE_RDDM:
  4872. cnss_ignore_qmi_failure(true);
  4873. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4874. del_timer(&plat_priv->fw_boot_timer);
  4875. del_timer(&pci_priv->dev_rddm_timer);
  4876. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4877. cnss_reason = CNSS_REASON_RDDM;
  4878. break;
  4879. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4880. case MHI_CB_FALLBACK_IMG:
  4881. plat_priv->use_fw_path_with_prefix = false;
  4882. cnss_pci_update_fw_name(pci_priv);
  4883. return;
  4884. #endif
  4885. default:
  4886. cnss_pr_err("Unsupported MHI status cb reason: %d\n", reason);
  4887. return;
  4888. }
  4889. cnss_schedule_recovery(&pci_priv->pci_dev->dev, cnss_reason);
  4890. }
  4891. static int cnss_pci_get_mhi_msi(struct cnss_pci_data *pci_priv)
  4892. {
  4893. int ret, num_vectors, i;
  4894. u32 user_base_data, base_vector;
  4895. int *irq;
  4896. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4897. MHI_MSI_NAME, &num_vectors,
  4898. &user_base_data, &base_vector);
  4899. if (ret)
  4900. return ret;
  4901. cnss_pr_dbg("Number of assigned MSI for MHI is %d, base vector is %d\n",
  4902. num_vectors, base_vector);
  4903. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  4904. if (!irq)
  4905. return -ENOMEM;
  4906. for (i = 0; i < num_vectors; i++)
  4907. irq[i] = cnss_get_msi_irq(&pci_priv->pci_dev->dev,
  4908. base_vector + i);
  4909. pci_priv->mhi_ctrl->irq = irq;
  4910. pci_priv->mhi_ctrl->nr_irqs = num_vectors;
  4911. return 0;
  4912. }
  4913. static int cnss_mhi_bw_scale(struct mhi_controller *mhi_ctrl,
  4914. struct mhi_link_info *link_info)
  4915. {
  4916. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4917. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4918. int ret = 0;
  4919. cnss_pr_dbg("Setting link speed:0x%x, width:0x%x\n",
  4920. link_info->target_link_speed,
  4921. link_info->target_link_width);
  4922. /* It has to set target link speed here before setting link bandwidth
  4923. * when device requests link speed change. This can avoid setting link
  4924. * bandwidth getting rejected if requested link speed is higher than
  4925. * current one.
  4926. */
  4927. ret = cnss_pci_set_max_link_speed(pci_priv, plat_priv->rc_num,
  4928. link_info->target_link_speed);
  4929. if (ret)
  4930. cnss_pr_err("Failed to set target link speed to 0x%x, err = %d\n",
  4931. link_info->target_link_speed, ret);
  4932. ret = cnss_pci_set_link_bandwidth(pci_priv,
  4933. link_info->target_link_speed,
  4934. link_info->target_link_width);
  4935. if (ret) {
  4936. cnss_pr_err("Failed to set link bandwidth, err = %d\n", ret);
  4937. return ret;
  4938. }
  4939. pci_priv->def_link_speed = link_info->target_link_speed;
  4940. pci_priv->def_link_width = link_info->target_link_width;
  4941. return 0;
  4942. }
  4943. static int cnss_mhi_read_reg(struct mhi_controller *mhi_ctrl,
  4944. void __iomem *addr, u32 *out)
  4945. {
  4946. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4947. u32 tmp = readl_relaxed(addr);
  4948. /* Unexpected value, query the link status */
  4949. if (PCI_INVALID_READ(tmp) &&
  4950. cnss_pci_check_link_status(pci_priv))
  4951. return -EIO;
  4952. *out = tmp;
  4953. return 0;
  4954. }
  4955. static void cnss_mhi_write_reg(struct mhi_controller *mhi_ctrl,
  4956. void __iomem *addr, u32 val)
  4957. {
  4958. writel_relaxed(val, addr);
  4959. }
  4960. static int cnss_pci_register_mhi(struct cnss_pci_data *pci_priv)
  4961. {
  4962. int ret = 0;
  4963. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4964. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4965. struct mhi_controller *mhi_ctrl;
  4966. phys_addr_t bar_start;
  4967. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4968. return 0;
  4969. mhi_ctrl = mhi_alloc_controller();
  4970. if (!mhi_ctrl) {
  4971. cnss_pr_err("Invalid MHI controller context\n");
  4972. return -EINVAL;
  4973. }
  4974. pci_priv->mhi_ctrl = mhi_ctrl;
  4975. mhi_ctrl->cntrl_dev = &pci_dev->dev;
  4976. mhi_ctrl->fw_image = plat_priv->firmware_name;
  4977. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4978. mhi_ctrl->fallback_fw_image = plat_priv->fw_fallback_name;
  4979. #endif
  4980. mhi_ctrl->regs = pci_priv->bar;
  4981. mhi_ctrl->reg_len = pci_resource_len(pci_priv->pci_dev, PCI_BAR_NUM);
  4982. bar_start = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4983. cnss_pr_dbg("BAR starts at %pa, length is %x\n",
  4984. &bar_start, mhi_ctrl->reg_len);
  4985. ret = cnss_pci_get_mhi_msi(pci_priv);
  4986. if (ret) {
  4987. cnss_pr_err("Failed to get MSI for MHI, err = %d\n", ret);
  4988. goto free_mhi_ctrl;
  4989. }
  4990. if (pci_priv->smmu_s1_enable) {
  4991. mhi_ctrl->iova_start = pci_priv->smmu_iova_start;
  4992. mhi_ctrl->iova_stop = pci_priv->smmu_iova_start +
  4993. pci_priv->smmu_iova_len;
  4994. } else {
  4995. mhi_ctrl->iova_start = 0;
  4996. mhi_ctrl->iova_stop = pci_priv->dma_bit_mask;
  4997. }
  4998. mhi_ctrl->status_cb = cnss_mhi_notify_status;
  4999. mhi_ctrl->runtime_get = cnss_mhi_pm_runtime_get;
  5000. mhi_ctrl->runtime_put = cnss_mhi_pm_runtime_put_noidle;
  5001. mhi_ctrl->read_reg = cnss_mhi_read_reg;
  5002. mhi_ctrl->write_reg = cnss_mhi_write_reg;
  5003. mhi_ctrl->rddm_size = pci_priv->plat_priv->ramdump_info_v2.ramdump_size;
  5004. if (!mhi_ctrl->rddm_size)
  5005. mhi_ctrl->rddm_size = RAMDUMP_SIZE_DEFAULT;
  5006. mhi_ctrl->sbl_size = SZ_512K;
  5007. mhi_ctrl->seg_len = SZ_512K;
  5008. mhi_ctrl->fbc_download = true;
  5009. ret = mhi_register_controller(mhi_ctrl, &cnss_mhi_config);
  5010. if (ret) {
  5011. cnss_pr_err("Failed to register to MHI bus, err = %d\n", ret);
  5012. goto free_mhi_irq;
  5013. }
  5014. /* MHI satellite driver only needs to connect when DRV is supported */
  5015. if (cnss_pci_is_drv_supported(pci_priv))
  5016. cnss_mhi_controller_set_base(pci_priv, bar_start);
  5017. /* BW scale CB needs to be set after registering MHI per requirement */
  5018. cnss_mhi_controller_set_bw_scale_cb(pci_priv, cnss_mhi_bw_scale);
  5019. ret = cnss_pci_update_fw_name(pci_priv);
  5020. if (ret)
  5021. goto unreg_mhi;
  5022. return 0;
  5023. unreg_mhi:
  5024. mhi_unregister_controller(mhi_ctrl);
  5025. free_mhi_irq:
  5026. kfree(mhi_ctrl->irq);
  5027. free_mhi_ctrl:
  5028. mhi_free_controller(mhi_ctrl);
  5029. return ret;
  5030. }
  5031. static void cnss_pci_unregister_mhi(struct cnss_pci_data *pci_priv)
  5032. {
  5033. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  5034. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  5035. return;
  5036. mhi_unregister_controller(mhi_ctrl);
  5037. kfree(mhi_ctrl->irq);
  5038. mhi_free_controller(mhi_ctrl);
  5039. }
  5040. static void cnss_pci_config_regs(struct cnss_pci_data *pci_priv)
  5041. {
  5042. switch (pci_priv->device_id) {
  5043. case QCA6390_DEVICE_ID:
  5044. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6390;
  5045. pci_priv->wcss_reg = wcss_reg_access_seq;
  5046. pci_priv->pcie_reg = pcie_reg_access_seq;
  5047. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5048. pci_priv->syspm_reg = syspm_reg_access_seq;
  5049. /* Configure WDOG register with specific value so that we can
  5050. * know if HW is in the process of WDOG reset recovery or not
  5051. * when reading the registers.
  5052. */
  5053. cnss_pci_reg_write
  5054. (pci_priv,
  5055. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG,
  5056. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG_VAL);
  5057. break;
  5058. case QCA6490_DEVICE_ID:
  5059. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6490;
  5060. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5061. break;
  5062. default:
  5063. return;
  5064. }
  5065. }
  5066. #if !IS_ENABLED(CONFIG_ARCH_QCOM)
  5067. static irqreturn_t cnss_pci_wake_handler(int irq, void *data)
  5068. {
  5069. struct cnss_pci_data *pci_priv = data;
  5070. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5071. enum rpm_status status;
  5072. struct device *dev;
  5073. pci_priv->wake_counter++;
  5074. cnss_pr_dbg("WLAN PCI wake IRQ (%u) is asserted #%u\n",
  5075. pci_priv->wake_irq, pci_priv->wake_counter);
  5076. /* Make sure abort current suspend */
  5077. cnss_pm_stay_awake(plat_priv);
  5078. cnss_pm_relax(plat_priv);
  5079. /* Above two pm* API calls will abort system suspend only when
  5080. * plat_dev->dev->ws is initiated by device_init_wakeup() API, and
  5081. * calling pm_system_wakeup() is just to guarantee system suspend
  5082. * can be aborted if it is not initiated in any case.
  5083. */
  5084. pm_system_wakeup();
  5085. dev = &pci_priv->pci_dev->dev;
  5086. status = dev->power.runtime_status;
  5087. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  5088. cnss_pci_get_auto_suspended(pci_priv)) ||
  5089. (status == RPM_SUSPENDING || status == RPM_SUSPENDED)) {
  5090. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  5091. cnss_pci_pm_request_resume(pci_priv);
  5092. }
  5093. return IRQ_HANDLED;
  5094. }
  5095. /**
  5096. * cnss_pci_wake_gpio_init() - Setup PCI wake GPIO for WLAN
  5097. * @pci_priv: driver PCI bus context pointer
  5098. *
  5099. * This function initializes WLAN PCI wake GPIO and corresponding
  5100. * interrupt. It should be used in non-MSM platforms whose PCIe
  5101. * root complex driver doesn't handle the GPIO.
  5102. *
  5103. * Return: 0 for success or skip, negative value for error
  5104. */
  5105. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  5106. {
  5107. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5108. struct device *dev = &plat_priv->plat_dev->dev;
  5109. int ret = 0;
  5110. pci_priv->wake_gpio = of_get_named_gpio(dev->of_node,
  5111. "wlan-pci-wake-gpio", 0);
  5112. if (pci_priv->wake_gpio < 0)
  5113. goto out;
  5114. cnss_pr_dbg("Get PCI wake GPIO (%d) from device node\n",
  5115. pci_priv->wake_gpio);
  5116. ret = gpio_request(pci_priv->wake_gpio, "wlan_pci_wake_gpio");
  5117. if (ret) {
  5118. cnss_pr_err("Failed to request PCI wake GPIO, err = %d\n",
  5119. ret);
  5120. goto out;
  5121. }
  5122. gpio_direction_input(pci_priv->wake_gpio);
  5123. pci_priv->wake_irq = gpio_to_irq(pci_priv->wake_gpio);
  5124. ret = request_irq(pci_priv->wake_irq, cnss_pci_wake_handler,
  5125. IRQF_TRIGGER_FALLING, "wlan_pci_wake_irq", pci_priv);
  5126. if (ret) {
  5127. cnss_pr_err("Failed to request PCI wake IRQ, err = %d\n", ret);
  5128. goto free_gpio;
  5129. }
  5130. ret = enable_irq_wake(pci_priv->wake_irq);
  5131. if (ret) {
  5132. cnss_pr_err("Failed to enable PCI wake IRQ, err = %d\n", ret);
  5133. goto free_irq;
  5134. }
  5135. return 0;
  5136. free_irq:
  5137. free_irq(pci_priv->wake_irq, pci_priv);
  5138. free_gpio:
  5139. gpio_free(pci_priv->wake_gpio);
  5140. out:
  5141. return ret;
  5142. }
  5143. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  5144. {
  5145. if (pci_priv->wake_gpio < 0)
  5146. return;
  5147. disable_irq_wake(pci_priv->wake_irq);
  5148. free_irq(pci_priv->wake_irq, pci_priv);
  5149. gpio_free(pci_priv->wake_gpio);
  5150. }
  5151. #else
  5152. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  5153. {
  5154. return 0;
  5155. }
  5156. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  5157. {
  5158. }
  5159. #endif
  5160. #if IS_ENABLED(CONFIG_ARCH_QCOM)
  5161. /**
  5162. * cnss_pci_of_reserved_mem_device_init() - Assign reserved memory region
  5163. * to given PCI device
  5164. * @pci_priv: driver PCI bus context pointer
  5165. *
  5166. * This function shall call corresponding of_reserved_mem_device* API to
  5167. * assign reserved memory region to PCI device based on where the memory is
  5168. * defined and attached to (platform device of_node or PCI device of_node)
  5169. * in device tree.
  5170. *
  5171. * Return: 0 for success, negative value for error
  5172. */
  5173. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  5174. {
  5175. struct device *dev_pci = &pci_priv->pci_dev->dev;
  5176. int ret;
  5177. /* Use of_reserved_mem_device_init_by_idx() if reserved memory is
  5178. * attached to platform device of_node.
  5179. */
  5180. ret = of_reserved_mem_device_init(dev_pci);
  5181. if (ret)
  5182. cnss_pr_err("Failed to init reserved mem device, err = %d\n",
  5183. ret);
  5184. if (dev_pci->cma_area)
  5185. cnss_pr_dbg("CMA area is %s\n",
  5186. cma_get_name(dev_pci->cma_area));
  5187. return ret;
  5188. }
  5189. #else
  5190. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  5191. {
  5192. return 0;
  5193. }
  5194. #endif
  5195. /* Setting to use this cnss_pm_domain ops will let PM framework override the
  5196. * ops from dev->bus->pm which is pci_dev_pm_ops from pci-driver.c. This ops
  5197. * has to take care everything device driver needed which is currently done
  5198. * from pci_dev_pm_ops.
  5199. */
  5200. static struct dev_pm_domain cnss_pm_domain = {
  5201. .ops = {
  5202. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5203. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5204. cnss_pci_resume_noirq)
  5205. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend,
  5206. cnss_pci_runtime_resume,
  5207. cnss_pci_runtime_idle)
  5208. }
  5209. };
  5210. static int cnss_pci_probe(struct pci_dev *pci_dev,
  5211. const struct pci_device_id *id)
  5212. {
  5213. int ret = 0;
  5214. struct cnss_pci_data *pci_priv;
  5215. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  5216. struct device *dev = &pci_dev->dev;
  5217. cnss_pr_dbg("PCI is probing, vendor ID: 0x%x, device ID: 0x%x\n",
  5218. id->vendor, pci_dev->device);
  5219. pci_priv = devm_kzalloc(dev, sizeof(*pci_priv), GFP_KERNEL);
  5220. if (!pci_priv) {
  5221. ret = -ENOMEM;
  5222. goto out;
  5223. }
  5224. pci_priv->pci_link_state = PCI_LINK_UP;
  5225. pci_priv->plat_priv = plat_priv;
  5226. pci_priv->pci_dev = pci_dev;
  5227. pci_priv->pci_device_id = id;
  5228. pci_priv->device_id = pci_dev->device;
  5229. cnss_set_pci_priv(pci_dev, pci_priv);
  5230. plat_priv->device_id = pci_dev->device;
  5231. plat_priv->bus_priv = pci_priv;
  5232. mutex_init(&pci_priv->bus_lock);
  5233. if (plat_priv->use_pm_domain)
  5234. dev->pm_domain = &cnss_pm_domain;
  5235. cnss_pci_of_reserved_mem_device_init(pci_priv);
  5236. ret = cnss_register_subsys(plat_priv);
  5237. if (ret)
  5238. goto reset_ctx;
  5239. ret = cnss_register_ramdump(plat_priv);
  5240. if (ret)
  5241. goto unregister_subsys;
  5242. ret = cnss_pci_init_smmu(pci_priv);
  5243. if (ret)
  5244. goto unregister_ramdump;
  5245. ret = cnss_reg_pci_event(pci_priv);
  5246. if (ret) {
  5247. cnss_pr_err("Failed to register PCI event, err = %d\n", ret);
  5248. goto deinit_smmu;
  5249. }
  5250. ret = cnss_pci_enable_bus(pci_priv);
  5251. if (ret)
  5252. goto dereg_pci_event;
  5253. ret = cnss_pci_enable_msi(pci_priv);
  5254. if (ret)
  5255. goto disable_bus;
  5256. ret = cnss_pci_register_mhi(pci_priv);
  5257. if (ret)
  5258. goto disable_msi;
  5259. switch (pci_dev->device) {
  5260. case QCA6174_DEVICE_ID:
  5261. pci_read_config_word(pci_dev, QCA6174_REV_ID_OFFSET,
  5262. &pci_priv->revision_id);
  5263. break;
  5264. case QCA6290_DEVICE_ID:
  5265. case QCA6390_DEVICE_ID:
  5266. case QCA6490_DEVICE_ID:
  5267. case KIWI_DEVICE_ID:
  5268. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  5269. timer_setup(&pci_priv->dev_rddm_timer,
  5270. cnss_dev_rddm_timeout_hdlr, 0);
  5271. timer_setup(&pci_priv->boot_debug_timer,
  5272. cnss_boot_debug_timeout_hdlr, 0);
  5273. INIT_DELAYED_WORK(&pci_priv->time_sync_work,
  5274. cnss_pci_time_sync_work_hdlr);
  5275. cnss_pci_get_link_status(pci_priv);
  5276. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, false);
  5277. cnss_pci_wake_gpio_init(pci_priv);
  5278. break;
  5279. default:
  5280. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  5281. pci_dev->device);
  5282. ret = -ENODEV;
  5283. goto unreg_mhi;
  5284. }
  5285. cnss_pci_config_regs(pci_priv);
  5286. if (EMULATION_HW)
  5287. goto out;
  5288. ret = cnss_suspend_pci_link(pci_priv);
  5289. if (ret)
  5290. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  5291. cnss_power_off_device(plat_priv);
  5292. set_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5293. return 0;
  5294. unreg_mhi:
  5295. cnss_pci_unregister_mhi(pci_priv);
  5296. disable_msi:
  5297. cnss_pci_disable_msi(pci_priv);
  5298. disable_bus:
  5299. cnss_pci_disable_bus(pci_priv);
  5300. dereg_pci_event:
  5301. cnss_dereg_pci_event(pci_priv);
  5302. deinit_smmu:
  5303. cnss_pci_deinit_smmu(pci_priv);
  5304. unregister_ramdump:
  5305. cnss_unregister_ramdump(plat_priv);
  5306. unregister_subsys:
  5307. cnss_unregister_subsys(plat_priv);
  5308. reset_ctx:
  5309. plat_priv->bus_priv = NULL;
  5310. out:
  5311. return ret;
  5312. }
  5313. static void cnss_pci_remove(struct pci_dev *pci_dev)
  5314. {
  5315. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5316. struct cnss_plat_data *plat_priv =
  5317. cnss_bus_dev_to_plat_priv(&pci_dev->dev);
  5318. clear_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5319. cnss_pci_free_m3_mem(pci_priv);
  5320. cnss_pci_free_fw_mem(pci_priv);
  5321. cnss_pci_free_qdss_mem(pci_priv);
  5322. switch (pci_dev->device) {
  5323. case QCA6290_DEVICE_ID:
  5324. case QCA6390_DEVICE_ID:
  5325. case QCA6490_DEVICE_ID:
  5326. case KIWI_DEVICE_ID:
  5327. cnss_pci_wake_gpio_deinit(pci_priv);
  5328. del_timer(&pci_priv->boot_debug_timer);
  5329. del_timer(&pci_priv->dev_rddm_timer);
  5330. break;
  5331. default:
  5332. break;
  5333. }
  5334. cnss_pci_unregister_mhi(pci_priv);
  5335. cnss_pci_disable_msi(pci_priv);
  5336. cnss_pci_disable_bus(pci_priv);
  5337. cnss_dereg_pci_event(pci_priv);
  5338. cnss_pci_deinit_smmu(pci_priv);
  5339. if (plat_priv) {
  5340. cnss_unregister_ramdump(plat_priv);
  5341. cnss_unregister_subsys(plat_priv);
  5342. plat_priv->bus_priv = NULL;
  5343. } else {
  5344. cnss_pr_err("Plat_priv is null, Unable to unregister ramdump,subsys\n");
  5345. }
  5346. }
  5347. static const struct pci_device_id cnss_pci_id_table[] = {
  5348. { QCA6174_VENDOR_ID, QCA6174_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5349. { QCA6290_VENDOR_ID, QCA6290_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5350. { QCA6390_VENDOR_ID, QCA6390_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5351. { QCA6490_VENDOR_ID, QCA6490_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5352. { KIWI_VENDOR_ID, KIWI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5353. { 0 }
  5354. };
  5355. MODULE_DEVICE_TABLE(pci, cnss_pci_id_table);
  5356. static const struct dev_pm_ops cnss_pm_ops = {
  5357. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5358. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5359. cnss_pci_resume_noirq)
  5360. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend, cnss_pci_runtime_resume,
  5361. cnss_pci_runtime_idle)
  5362. };
  5363. struct pci_driver cnss_pci_driver = {
  5364. .name = "cnss_pci",
  5365. .id_table = cnss_pci_id_table,
  5366. .probe = cnss_pci_probe,
  5367. .remove = cnss_pci_remove,
  5368. .driver = {
  5369. .pm = &cnss_pm_ops,
  5370. },
  5371. };
  5372. static int cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  5373. {
  5374. int ret, retry = 0;
  5375. /* Always set initial target PCIe link speed to Gen2 for QCA6490 device
  5376. * since there may be link issues if it boots up with Gen3 link speed.
  5377. * Device is able to change it later at any time. It will be rejected
  5378. * if requested speed is higher than the one specified in PCIe DT.
  5379. */
  5380. if (plat_priv->device_id == QCA6490_DEVICE_ID) {
  5381. ret = cnss_pci_set_max_link_speed(plat_priv->bus_priv, rc_num,
  5382. PCI_EXP_LNKSTA_CLS_5_0GB);
  5383. if (ret && ret != -EPROBE_DEFER)
  5384. cnss_pr_err("Failed to set max PCIe RC%x link speed to Gen2, err = %d\n",
  5385. rc_num, ret);
  5386. }
  5387. cnss_pr_dbg("Trying to enumerate with PCIe RC%x\n", rc_num);
  5388. retry:
  5389. ret = _cnss_pci_enumerate(plat_priv, rc_num);
  5390. if (ret) {
  5391. if (ret == -EPROBE_DEFER) {
  5392. cnss_pr_dbg("PCIe RC driver is not ready, defer probe\n");
  5393. goto out;
  5394. }
  5395. cnss_pr_err("Failed to enable PCIe RC%x, err = %d\n",
  5396. rc_num, ret);
  5397. if (retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  5398. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  5399. goto retry;
  5400. } else {
  5401. goto out;
  5402. }
  5403. }
  5404. plat_priv->rc_num = rc_num;
  5405. out:
  5406. return ret;
  5407. }
  5408. int cnss_pci_init(struct cnss_plat_data *plat_priv)
  5409. {
  5410. struct device *dev = &plat_priv->plat_dev->dev;
  5411. const __be32 *prop;
  5412. int ret = 0, prop_len = 0, rc_count, i;
  5413. prop = of_get_property(dev->of_node, "qcom,wlan-rc-num", &prop_len);
  5414. if (!prop || !prop_len) {
  5415. cnss_pr_err("Failed to get PCIe RC number from DT\n");
  5416. goto out;
  5417. }
  5418. rc_count = prop_len / sizeof(__be32);
  5419. for (i = 0; i < rc_count; i++) {
  5420. ret = cnss_pci_enumerate(plat_priv, be32_to_cpup(&prop[i]));
  5421. if (!ret)
  5422. break;
  5423. else if (ret == -EPROBE_DEFER || (ret && i == rc_count - 1))
  5424. goto out;
  5425. }
  5426. ret = pci_register_driver(&cnss_pci_driver);
  5427. if (ret) {
  5428. cnss_pr_err("Failed to register to PCI framework, err = %d\n",
  5429. ret);
  5430. goto out;
  5431. }
  5432. if (!plat_priv->bus_priv) {
  5433. cnss_pr_err("Failed to probe PCI driver\n");
  5434. ret = -ENODEV;
  5435. goto unreg_pci;
  5436. }
  5437. return 0;
  5438. unreg_pci:
  5439. pci_unregister_driver(&cnss_pci_driver);
  5440. out:
  5441. return ret;
  5442. }
  5443. void cnss_pci_deinit(struct cnss_plat_data *plat_priv)
  5444. {
  5445. pci_unregister_driver(&cnss_pci_driver);
  5446. }