pci.c 172 KB

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