pci.c 166 KB

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