pci.c 170 KB

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