pci.c 170 KB

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