pci.c 169 KB

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