pci.c 170 KB

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