pci.c 169 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406
  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. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL3, &low);
  2021. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL4, &high);
  2022. device_ticks = (u64)high << 32 | low;
  2023. do_div(device_ticks, plat_priv->device_freq_hz / 100000);
  2024. *time_us = device_ticks * 10;
  2025. return 0;
  2026. }
  2027. static void cnss_pci_enable_time_sync_counter(struct cnss_pci_data *pci_priv)
  2028. {
  2029. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  2030. TIME_SYNC_ENABLE);
  2031. }
  2032. static void cnss_pci_clear_time_sync_counter(struct cnss_pci_data *pci_priv)
  2033. {
  2034. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  2035. TIME_SYNC_CLEAR);
  2036. }
  2037. static int cnss_pci_update_timestamp(struct cnss_pci_data *pci_priv)
  2038. {
  2039. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2040. struct device *dev = &pci_priv->pci_dev->dev;
  2041. unsigned long flags = 0;
  2042. u64 host_time_us, device_time_us, offset;
  2043. u32 low, high;
  2044. int ret;
  2045. ret = cnss_pci_prevent_l1(dev);
  2046. if (ret)
  2047. goto out;
  2048. ret = cnss_pci_force_wake_get(pci_priv);
  2049. if (ret)
  2050. goto allow_l1;
  2051. spin_lock_irqsave(&time_sync_lock, flags);
  2052. cnss_pci_clear_time_sync_counter(pci_priv);
  2053. cnss_pci_enable_time_sync_counter(pci_priv);
  2054. host_time_us = cnss_get_host_timestamp(plat_priv);
  2055. ret = cnss_pci_get_device_timestamp(pci_priv, &device_time_us);
  2056. cnss_pci_clear_time_sync_counter(pci_priv);
  2057. spin_unlock_irqrestore(&time_sync_lock, flags);
  2058. if (ret)
  2059. goto force_wake_put;
  2060. if (host_time_us < device_time_us) {
  2061. cnss_pr_err("Host time (%llu us) is smaller than device time (%llu us), stop\n",
  2062. host_time_us, device_time_us);
  2063. ret = -EINVAL;
  2064. goto force_wake_put;
  2065. }
  2066. offset = host_time_us - device_time_us;
  2067. cnss_pr_dbg("Host time = %llu us, device time = %llu us, offset = %llu us\n",
  2068. host_time_us, device_time_us, offset);
  2069. low = offset & 0xFFFFFFFF;
  2070. high = offset >> 32;
  2071. cnss_pci_reg_write(pci_priv, PCIE_SHADOW_REG_VALUE_34, low);
  2072. cnss_pci_reg_write(pci_priv, PCIE_SHADOW_REG_VALUE_35, high);
  2073. cnss_pci_reg_read(pci_priv, PCIE_SHADOW_REG_VALUE_34, &low);
  2074. cnss_pci_reg_read(pci_priv, PCIE_SHADOW_REG_VALUE_35, &high);
  2075. cnss_pr_dbg("Updated time sync regs [0x%x] = 0x%x, [0x%x] = 0x%x\n",
  2076. PCIE_SHADOW_REG_VALUE_34, low,
  2077. PCIE_SHADOW_REG_VALUE_35, high);
  2078. force_wake_put:
  2079. cnss_pci_force_wake_put(pci_priv);
  2080. allow_l1:
  2081. cnss_pci_allow_l1(dev);
  2082. out:
  2083. return ret;
  2084. }
  2085. static void cnss_pci_time_sync_work_hdlr(struct work_struct *work)
  2086. {
  2087. struct cnss_pci_data *pci_priv =
  2088. container_of(work, struct cnss_pci_data, time_sync_work.work);
  2089. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2090. unsigned int time_sync_period_ms =
  2091. plat_priv->ctrl_params.time_sync_period;
  2092. if (test_bit(DISABLE_TIME_SYNC, &plat_priv->ctrl_params.quirks)) {
  2093. cnss_pr_dbg("Time sync is disabled\n");
  2094. return;
  2095. }
  2096. if (!time_sync_period_ms) {
  2097. cnss_pr_dbg("Skip time sync as time period is 0\n");
  2098. return;
  2099. }
  2100. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  2101. return;
  2102. if (cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS) < 0)
  2103. goto runtime_pm_put;
  2104. mutex_lock(&pci_priv->bus_lock);
  2105. cnss_pci_update_timestamp(pci_priv);
  2106. mutex_unlock(&pci_priv->bus_lock);
  2107. schedule_delayed_work(&pci_priv->time_sync_work,
  2108. msecs_to_jiffies(time_sync_period_ms));
  2109. runtime_pm_put:
  2110. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  2111. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  2112. }
  2113. static int cnss_pci_start_time_sync_update(struct cnss_pci_data *pci_priv)
  2114. {
  2115. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2116. switch (pci_priv->device_id) {
  2117. case QCA6390_DEVICE_ID:
  2118. case QCA6490_DEVICE_ID:
  2119. break;
  2120. default:
  2121. return -EOPNOTSUPP;
  2122. }
  2123. if (!plat_priv->device_freq_hz) {
  2124. cnss_pr_dbg("Device time clock frequency is not valid, skip time sync\n");
  2125. return -EINVAL;
  2126. }
  2127. cnss_pci_time_sync_work_hdlr(&pci_priv->time_sync_work.work);
  2128. return 0;
  2129. }
  2130. static void cnss_pci_stop_time_sync_update(struct cnss_pci_data *pci_priv)
  2131. {
  2132. switch (pci_priv->device_id) {
  2133. case QCA6390_DEVICE_ID:
  2134. case QCA6490_DEVICE_ID:
  2135. break;
  2136. default:
  2137. return;
  2138. }
  2139. cancel_delayed_work_sync(&pci_priv->time_sync_work);
  2140. }
  2141. int cnss_pci_call_driver_probe(struct cnss_pci_data *pci_priv)
  2142. {
  2143. int ret = 0;
  2144. struct cnss_plat_data *plat_priv;
  2145. if (!pci_priv)
  2146. return -ENODEV;
  2147. plat_priv = pci_priv->plat_priv;
  2148. if (test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2149. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2150. cnss_pr_dbg("Skip driver probe\n");
  2151. goto out;
  2152. }
  2153. if (!pci_priv->driver_ops) {
  2154. cnss_pr_err("driver_ops is NULL\n");
  2155. ret = -EINVAL;
  2156. goto out;
  2157. }
  2158. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2159. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2160. ret = pci_priv->driver_ops->reinit(pci_priv->pci_dev,
  2161. pci_priv->pci_device_id);
  2162. if (ret) {
  2163. cnss_pr_err("Failed to reinit host driver, err = %d\n",
  2164. ret);
  2165. goto out;
  2166. }
  2167. complete(&plat_priv->recovery_complete);
  2168. } else if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state)) {
  2169. ret = pci_priv->driver_ops->probe(pci_priv->pci_dev,
  2170. pci_priv->pci_device_id);
  2171. if (ret) {
  2172. cnss_pr_err("Failed to probe host driver, err = %d\n",
  2173. ret);
  2174. goto out;
  2175. }
  2176. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2177. set_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2178. complete_all(&plat_priv->power_up_complete);
  2179. } else if (test_bit(CNSS_DRIVER_IDLE_RESTART,
  2180. &plat_priv->driver_state)) {
  2181. ret = pci_priv->driver_ops->idle_restart(pci_priv->pci_dev,
  2182. pci_priv->pci_device_id);
  2183. if (ret) {
  2184. cnss_pr_err("Failed to idle restart host driver, err = %d\n",
  2185. ret);
  2186. plat_priv->power_up_error = ret;
  2187. complete_all(&plat_priv->power_up_complete);
  2188. goto out;
  2189. }
  2190. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  2191. complete_all(&plat_priv->power_up_complete);
  2192. } else {
  2193. complete(&plat_priv->power_up_complete);
  2194. }
  2195. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  2196. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2197. __pm_relax(plat_priv->recovery_ws);
  2198. }
  2199. cnss_pci_start_time_sync_update(pci_priv);
  2200. return 0;
  2201. out:
  2202. return ret;
  2203. }
  2204. int cnss_pci_call_driver_remove(struct cnss_pci_data *pci_priv)
  2205. {
  2206. struct cnss_plat_data *plat_priv;
  2207. int ret;
  2208. if (!pci_priv)
  2209. return -ENODEV;
  2210. plat_priv = pci_priv->plat_priv;
  2211. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  2212. test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state) ||
  2213. test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2214. cnss_pr_dbg("Skip driver remove\n");
  2215. return 0;
  2216. }
  2217. if (!pci_priv->driver_ops) {
  2218. cnss_pr_err("driver_ops is NULL\n");
  2219. return -EINVAL;
  2220. }
  2221. cnss_pci_stop_time_sync_update(pci_priv);
  2222. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2223. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2224. pci_priv->driver_ops->shutdown(pci_priv->pci_dev);
  2225. } else if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state)) {
  2226. pci_priv->driver_ops->remove(pci_priv->pci_dev);
  2227. clear_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2228. } else if (test_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2229. &plat_priv->driver_state)) {
  2230. ret = pci_priv->driver_ops->idle_shutdown(pci_priv->pci_dev);
  2231. if (ret == -EAGAIN) {
  2232. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2233. &plat_priv->driver_state);
  2234. return ret;
  2235. }
  2236. }
  2237. plat_priv->get_info_cb_ctx = NULL;
  2238. plat_priv->get_info_cb = NULL;
  2239. return 0;
  2240. }
  2241. int cnss_pci_call_driver_modem_status(struct cnss_pci_data *pci_priv,
  2242. int modem_current_status)
  2243. {
  2244. struct cnss_wlan_driver *driver_ops;
  2245. if (!pci_priv)
  2246. return -ENODEV;
  2247. driver_ops = pci_priv->driver_ops;
  2248. if (!driver_ops || !driver_ops->modem_status)
  2249. return -EINVAL;
  2250. driver_ops->modem_status(pci_priv->pci_dev, modem_current_status);
  2251. return 0;
  2252. }
  2253. int cnss_pci_update_status(struct cnss_pci_data *pci_priv,
  2254. enum cnss_driver_status status)
  2255. {
  2256. struct cnss_wlan_driver *driver_ops;
  2257. if (!pci_priv)
  2258. return -ENODEV;
  2259. driver_ops = pci_priv->driver_ops;
  2260. if (!driver_ops || !driver_ops->update_status)
  2261. return -EINVAL;
  2262. cnss_pr_dbg("Update driver status: %d\n", status);
  2263. driver_ops->update_status(pci_priv->pci_dev, status);
  2264. return 0;
  2265. }
  2266. static void cnss_pci_misc_reg_dump(struct cnss_pci_data *pci_priv,
  2267. struct cnss_misc_reg *misc_reg,
  2268. u32 misc_reg_size,
  2269. char *reg_name)
  2270. {
  2271. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2272. bool do_force_wake_put = true;
  2273. int i;
  2274. if (!misc_reg)
  2275. return;
  2276. if (in_interrupt() || irqs_disabled())
  2277. return;
  2278. if (cnss_pci_check_link_status(pci_priv))
  2279. return;
  2280. if (cnss_pci_force_wake_get(pci_priv)) {
  2281. /* Continue to dump when device has entered RDDM already */
  2282. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2283. return;
  2284. do_force_wake_put = false;
  2285. }
  2286. cnss_pr_dbg("Start to dump %s registers\n", reg_name);
  2287. for (i = 0; i < misc_reg_size; i++) {
  2288. if (!test_bit(pci_priv->misc_reg_dev_mask,
  2289. &misc_reg[i].dev_mask))
  2290. continue;
  2291. if (misc_reg[i].wr) {
  2292. if (misc_reg[i].offset ==
  2293. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG &&
  2294. i >= 1)
  2295. misc_reg[i].val =
  2296. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG_MSK |
  2297. misc_reg[i - 1].val;
  2298. if (cnss_pci_reg_write(pci_priv,
  2299. misc_reg[i].offset,
  2300. misc_reg[i].val))
  2301. goto force_wake_put;
  2302. cnss_pr_vdbg("Write 0x%X to 0x%X\n",
  2303. misc_reg[i].val,
  2304. misc_reg[i].offset);
  2305. } else {
  2306. if (cnss_pci_reg_read(pci_priv,
  2307. misc_reg[i].offset,
  2308. &misc_reg[i].val))
  2309. goto force_wake_put;
  2310. }
  2311. }
  2312. force_wake_put:
  2313. if (do_force_wake_put)
  2314. cnss_pci_force_wake_put(pci_priv);
  2315. }
  2316. static void cnss_pci_dump_misc_reg(struct cnss_pci_data *pci_priv)
  2317. {
  2318. if (in_interrupt() || irqs_disabled())
  2319. return;
  2320. if (cnss_pci_check_link_status(pci_priv))
  2321. return;
  2322. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wcss_reg,
  2323. WCSS_REG_SIZE, "wcss");
  2324. cnss_pci_misc_reg_dump(pci_priv, pci_priv->pcie_reg,
  2325. PCIE_REG_SIZE, "pcie");
  2326. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wlaon_reg,
  2327. WLAON_REG_SIZE, "wlaon");
  2328. cnss_pci_misc_reg_dump(pci_priv, pci_priv->syspm_reg,
  2329. SYSPM_REG_SIZE, "syspm");
  2330. }
  2331. static void cnss_pci_dump_shadow_reg(struct cnss_pci_data *pci_priv)
  2332. {
  2333. int i, j = 0, array_size = SHADOW_REG_COUNT + SHADOW_REG_INTER_COUNT;
  2334. u32 reg_offset;
  2335. bool do_force_wake_put = true;
  2336. if (in_interrupt() || irqs_disabled())
  2337. return;
  2338. if (cnss_pci_check_link_status(pci_priv))
  2339. return;
  2340. if (!pci_priv->debug_reg) {
  2341. pci_priv->debug_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  2342. sizeof(*pci_priv->debug_reg)
  2343. * array_size, GFP_KERNEL);
  2344. if (!pci_priv->debug_reg)
  2345. return;
  2346. }
  2347. if (cnss_pci_force_wake_get(pci_priv))
  2348. do_force_wake_put = false;
  2349. cnss_pr_dbg("Start to dump shadow registers\n");
  2350. for (i = 0; i < SHADOW_REG_COUNT; i++, j++) {
  2351. reg_offset = PCIE_SHADOW_REG_VALUE_0 + i * 4;
  2352. pci_priv->debug_reg[j].offset = reg_offset;
  2353. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2354. &pci_priv->debug_reg[j].val))
  2355. goto force_wake_put;
  2356. }
  2357. for (i = 0; i < SHADOW_REG_INTER_COUNT; i++, j++) {
  2358. reg_offset = PCIE_SHADOW_REG_INTER_0 + i * 4;
  2359. pci_priv->debug_reg[j].offset = reg_offset;
  2360. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2361. &pci_priv->debug_reg[j].val))
  2362. goto force_wake_put;
  2363. }
  2364. force_wake_put:
  2365. if (do_force_wake_put)
  2366. cnss_pci_force_wake_put(pci_priv);
  2367. }
  2368. static int cnss_qca6174_powerup(struct cnss_pci_data *pci_priv)
  2369. {
  2370. int ret = 0;
  2371. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2372. ret = cnss_power_on_device(plat_priv);
  2373. if (ret) {
  2374. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2375. goto out;
  2376. }
  2377. ret = cnss_resume_pci_link(pci_priv);
  2378. if (ret) {
  2379. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2380. goto power_off;
  2381. }
  2382. ret = cnss_pci_call_driver_probe(pci_priv);
  2383. if (ret)
  2384. goto suspend_link;
  2385. return 0;
  2386. suspend_link:
  2387. cnss_suspend_pci_link(pci_priv);
  2388. power_off:
  2389. cnss_power_off_device(plat_priv);
  2390. out:
  2391. return ret;
  2392. }
  2393. static int cnss_qca6174_shutdown(struct cnss_pci_data *pci_priv)
  2394. {
  2395. int ret = 0;
  2396. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2397. cnss_pci_pm_runtime_resume(pci_priv);
  2398. ret = cnss_pci_call_driver_remove(pci_priv);
  2399. if (ret == -EAGAIN)
  2400. goto out;
  2401. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2402. CNSS_BUS_WIDTH_NONE);
  2403. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2404. cnss_pci_set_auto_suspended(pci_priv, 0);
  2405. ret = cnss_suspend_pci_link(pci_priv);
  2406. if (ret)
  2407. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2408. cnss_power_off_device(plat_priv);
  2409. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2410. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2411. out:
  2412. return ret;
  2413. }
  2414. static void cnss_qca6174_crash_shutdown(struct cnss_pci_data *pci_priv)
  2415. {
  2416. if (pci_priv->driver_ops && pci_priv->driver_ops->crash_shutdown)
  2417. pci_priv->driver_ops->crash_shutdown(pci_priv->pci_dev);
  2418. }
  2419. static int cnss_qca6174_ramdump(struct cnss_pci_data *pci_priv)
  2420. {
  2421. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2422. struct cnss_ramdump_info *ramdump_info;
  2423. ramdump_info = &plat_priv->ramdump_info;
  2424. if (!ramdump_info->ramdump_size)
  2425. return -EINVAL;
  2426. return cnss_do_ramdump(plat_priv);
  2427. }
  2428. static int cnss_qca6290_powerup(struct cnss_pci_data *pci_priv)
  2429. {
  2430. int ret = 0;
  2431. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2432. unsigned int timeout;
  2433. int retry = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  2434. if (plat_priv->ramdump_info_v2.dump_data_valid) {
  2435. cnss_pci_clear_dump_info(pci_priv);
  2436. cnss_pci_power_off_mhi(pci_priv);
  2437. cnss_suspend_pci_link(pci_priv);
  2438. cnss_pci_deinit_mhi(pci_priv);
  2439. cnss_power_off_device(plat_priv);
  2440. }
  2441. /* Clear QMI send usage count during every power up */
  2442. pci_priv->qmi_send_usage_count = 0;
  2443. plat_priv->power_up_error = 0;
  2444. retry:
  2445. ret = cnss_power_on_device(plat_priv);
  2446. if (ret) {
  2447. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2448. goto out;
  2449. }
  2450. ret = cnss_resume_pci_link(pci_priv);
  2451. if (ret) {
  2452. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2453. if (test_bit(IGNORE_PCI_LINK_FAILURE,
  2454. &plat_priv->ctrl_params.quirks)) {
  2455. cnss_pr_dbg("Ignore PCI link resume failure\n");
  2456. ret = 0;
  2457. goto out;
  2458. }
  2459. if (ret == -EAGAIN && retry++ < POWER_ON_RETRY_MAX_TIMES) {
  2460. cnss_power_off_device(plat_priv);
  2461. /* Force toggle BT_EN GPIO low */
  2462. if (retry == POWER_ON_RETRY_MAX_TIMES) {
  2463. cnss_pr_dbg("Retry #%d. Set BT_EN GPIO(%u) low\n",
  2464. retry, bt_en_gpio);
  2465. if (bt_en_gpio >= 0)
  2466. gpio_direction_output(bt_en_gpio, 0);
  2467. cnss_pr_dbg("BT_EN GPIO val: %d\n",
  2468. gpio_get_value(bt_en_gpio));
  2469. }
  2470. cnss_pr_dbg("Retry to resume PCI link #%d\n", retry);
  2471. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  2472. goto retry;
  2473. }
  2474. /* Assert when it reaches maximum retries */
  2475. CNSS_ASSERT(0);
  2476. goto power_off;
  2477. }
  2478. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  2479. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  2480. ret = cnss_pci_start_mhi(pci_priv);
  2481. if (ret) {
  2482. cnss_fatal_err("Failed to start MHI, err = %d\n", ret);
  2483. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  2484. !pci_priv->pci_link_down_ind && timeout) {
  2485. /* Start recovery directly for MHI start failures */
  2486. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  2487. CNSS_REASON_DEFAULT);
  2488. }
  2489. return 0;
  2490. }
  2491. if (test_bit(USE_CORE_ONLY_FW, &plat_priv->ctrl_params.quirks)) {
  2492. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  2493. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2494. return 0;
  2495. }
  2496. cnss_set_pin_connect_status(plat_priv);
  2497. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  2498. ret = cnss_pci_call_driver_probe(pci_priv);
  2499. if (ret)
  2500. goto stop_mhi;
  2501. } else if (timeout) {
  2502. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state))
  2503. timeout += WLAN_COLD_BOOT_CAL_TIMEOUT;
  2504. else
  2505. timeout += WLAN_MISSION_MODE_TIMEOUT;
  2506. mod_timer(&plat_priv->fw_boot_timer,
  2507. jiffies + msecs_to_jiffies(timeout));
  2508. }
  2509. return 0;
  2510. stop_mhi:
  2511. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, true);
  2512. cnss_pci_power_off_mhi(pci_priv);
  2513. cnss_suspend_pci_link(pci_priv);
  2514. cnss_pci_deinit_mhi(pci_priv);
  2515. power_off:
  2516. cnss_power_off_device(plat_priv);
  2517. out:
  2518. return ret;
  2519. }
  2520. static int cnss_qca6290_shutdown(struct cnss_pci_data *pci_priv)
  2521. {
  2522. int ret = 0;
  2523. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2524. int do_force_wake = true;
  2525. cnss_pci_pm_runtime_resume(pci_priv);
  2526. ret = cnss_pci_call_driver_remove(pci_priv);
  2527. if (ret == -EAGAIN)
  2528. goto out;
  2529. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2530. CNSS_BUS_WIDTH_NONE);
  2531. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2532. cnss_pci_set_auto_suspended(pci_priv, 0);
  2533. if ((test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  2534. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2535. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  2536. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state) ||
  2537. test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) &&
  2538. test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  2539. del_timer(&pci_priv->dev_rddm_timer);
  2540. cnss_pci_collect_dump_info(pci_priv, false);
  2541. CNSS_ASSERT(0);
  2542. }
  2543. if (!cnss_is_device_powered_on(plat_priv)) {
  2544. cnss_pr_dbg("Device is already powered off, ignore\n");
  2545. goto skip_power_off;
  2546. }
  2547. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2548. do_force_wake = false;
  2549. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, do_force_wake);
  2550. /* FBC image will be freed after powering off MHI, so skip
  2551. * if RAM dump data is still valid.
  2552. */
  2553. if (plat_priv->ramdump_info_v2.dump_data_valid)
  2554. goto skip_power_off;
  2555. cnss_pci_power_off_mhi(pci_priv);
  2556. ret = cnss_suspend_pci_link(pci_priv);
  2557. if (ret)
  2558. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2559. cnss_pci_deinit_mhi(pci_priv);
  2560. cnss_power_off_device(plat_priv);
  2561. skip_power_off:
  2562. pci_priv->remap_window = 0;
  2563. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  2564. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  2565. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2566. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  2567. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  2568. pci_priv->pci_link_down_ind = false;
  2569. }
  2570. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2571. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2572. out:
  2573. return ret;
  2574. }
  2575. static void cnss_qca6290_crash_shutdown(struct cnss_pci_data *pci_priv)
  2576. {
  2577. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2578. set_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2579. cnss_pr_dbg("Crash shutdown with driver_state 0x%lx\n",
  2580. plat_priv->driver_state);
  2581. cnss_pci_collect_dump_info(pci_priv, true);
  2582. clear_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2583. }
  2584. static int cnss_qca6290_ramdump(struct cnss_pci_data *pci_priv)
  2585. {
  2586. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2587. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2588. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2589. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2590. int ret = 0;
  2591. if (!info_v2->dump_data_valid || !dump_seg ||
  2592. dump_data->nentries == 0)
  2593. return 0;
  2594. ret = cnss_do_elf_ramdump(plat_priv);
  2595. cnss_pci_clear_dump_info(pci_priv);
  2596. cnss_pci_power_off_mhi(pci_priv);
  2597. cnss_suspend_pci_link(pci_priv);
  2598. cnss_pci_deinit_mhi(pci_priv);
  2599. cnss_power_off_device(plat_priv);
  2600. return ret;
  2601. }
  2602. int cnss_pci_dev_powerup(struct cnss_pci_data *pci_priv)
  2603. {
  2604. int ret = 0;
  2605. if (!pci_priv) {
  2606. cnss_pr_err("pci_priv is NULL\n");
  2607. return -ENODEV;
  2608. }
  2609. switch (pci_priv->device_id) {
  2610. case QCA6174_DEVICE_ID:
  2611. ret = cnss_qca6174_powerup(pci_priv);
  2612. break;
  2613. case QCA6290_DEVICE_ID:
  2614. case QCA6390_DEVICE_ID:
  2615. case QCA6490_DEVICE_ID:
  2616. case KIWI_DEVICE_ID:
  2617. ret = cnss_qca6290_powerup(pci_priv);
  2618. break;
  2619. default:
  2620. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2621. pci_priv->device_id);
  2622. ret = -ENODEV;
  2623. }
  2624. return ret;
  2625. }
  2626. int cnss_pci_dev_shutdown(struct cnss_pci_data *pci_priv)
  2627. {
  2628. int ret = 0;
  2629. if (!pci_priv) {
  2630. cnss_pr_err("pci_priv is NULL\n");
  2631. return -ENODEV;
  2632. }
  2633. switch (pci_priv->device_id) {
  2634. case QCA6174_DEVICE_ID:
  2635. ret = cnss_qca6174_shutdown(pci_priv);
  2636. break;
  2637. case QCA6290_DEVICE_ID:
  2638. case QCA6390_DEVICE_ID:
  2639. case QCA6490_DEVICE_ID:
  2640. case KIWI_DEVICE_ID:
  2641. ret = cnss_qca6290_shutdown(pci_priv);
  2642. break;
  2643. default:
  2644. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2645. pci_priv->device_id);
  2646. ret = -ENODEV;
  2647. }
  2648. return ret;
  2649. }
  2650. int cnss_pci_dev_crash_shutdown(struct cnss_pci_data *pci_priv)
  2651. {
  2652. int ret = 0;
  2653. if (!pci_priv) {
  2654. cnss_pr_err("pci_priv is NULL\n");
  2655. return -ENODEV;
  2656. }
  2657. switch (pci_priv->device_id) {
  2658. case QCA6174_DEVICE_ID:
  2659. cnss_qca6174_crash_shutdown(pci_priv);
  2660. break;
  2661. case QCA6290_DEVICE_ID:
  2662. case QCA6390_DEVICE_ID:
  2663. case QCA6490_DEVICE_ID:
  2664. case KIWI_DEVICE_ID:
  2665. cnss_qca6290_crash_shutdown(pci_priv);
  2666. break;
  2667. default:
  2668. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2669. pci_priv->device_id);
  2670. ret = -ENODEV;
  2671. }
  2672. return ret;
  2673. }
  2674. int cnss_pci_dev_ramdump(struct cnss_pci_data *pci_priv)
  2675. {
  2676. int ret = 0;
  2677. if (!pci_priv) {
  2678. cnss_pr_err("pci_priv is NULL\n");
  2679. return -ENODEV;
  2680. }
  2681. switch (pci_priv->device_id) {
  2682. case QCA6174_DEVICE_ID:
  2683. ret = cnss_qca6174_ramdump(pci_priv);
  2684. break;
  2685. case QCA6290_DEVICE_ID:
  2686. case QCA6390_DEVICE_ID:
  2687. case QCA6490_DEVICE_ID:
  2688. case KIWI_DEVICE_ID:
  2689. ret = cnss_qca6290_ramdump(pci_priv);
  2690. break;
  2691. default:
  2692. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2693. pci_priv->device_id);
  2694. ret = -ENODEV;
  2695. }
  2696. return ret;
  2697. }
  2698. int cnss_pci_is_drv_connected(struct device *dev)
  2699. {
  2700. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2701. if (!pci_priv)
  2702. return -ENODEV;
  2703. return pci_priv->drv_connected_last;
  2704. }
  2705. EXPORT_SYMBOL(cnss_pci_is_drv_connected);
  2706. static void cnss_wlan_reg_driver_work(struct work_struct *work)
  2707. {
  2708. struct cnss_plat_data *plat_priv =
  2709. container_of(work, struct cnss_plat_data, wlan_reg_driver_work.work);
  2710. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  2711. struct cnss_cal_info *cal_info;
  2712. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  2713. goto reg_driver;
  2714. } else {
  2715. cnss_pr_err("Timeout waiting for calibration to complete\n");
  2716. del_timer(&plat_priv->fw_boot_timer);
  2717. if (!test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state))
  2718. CNSS_ASSERT(0);
  2719. cal_info = kzalloc(sizeof(*cal_info), GFP_KERNEL);
  2720. if (!cal_info)
  2721. return;
  2722. cal_info->cal_status = CNSS_CAL_TIMEOUT;
  2723. cnss_driver_event_post(plat_priv,
  2724. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE,
  2725. 0, cal_info);
  2726. }
  2727. reg_driver:
  2728. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2729. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2730. return;
  2731. }
  2732. reinit_completion(&plat_priv->power_up_complete);
  2733. cnss_driver_event_post(plat_priv,
  2734. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2735. CNSS_EVENT_SYNC_UNKILLABLE,
  2736. pci_priv->driver_ops);
  2737. }
  2738. int cnss_wlan_register_driver(struct cnss_wlan_driver *driver_ops)
  2739. {
  2740. int ret = 0;
  2741. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2742. struct cnss_pci_data *pci_priv;
  2743. const struct pci_device_id *id_table = driver_ops->id_table;
  2744. unsigned int timeout;
  2745. if (!plat_priv) {
  2746. cnss_pr_info("plat_priv is not ready for register driver\n");
  2747. return -EAGAIN;
  2748. }
  2749. if (!test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state)) {
  2750. cnss_pr_info("pci probe not yet done for register driver\n");
  2751. return -EAGAIN;
  2752. }
  2753. pci_priv = plat_priv->bus_priv;
  2754. if (pci_priv->driver_ops) {
  2755. cnss_pr_err("Driver has already registered\n");
  2756. return -EEXIST;
  2757. }
  2758. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2759. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2760. return -EINVAL;
  2761. }
  2762. if (!id_table || !pci_dev_present(id_table)) {
  2763. /* id_table pointer will move from pci_dev_present(),
  2764. * so check again using local pointer.
  2765. */
  2766. id_table = driver_ops->id_table;
  2767. while (id_table && id_table->vendor) {
  2768. cnss_pr_info("Host driver is built for PCIe device ID 0x%x\n",
  2769. id_table->device);
  2770. id_table++;
  2771. }
  2772. cnss_pr_err("Enumerated PCIe device id is 0x%x, reject unsupported driver\n",
  2773. pci_priv->device_id);
  2774. return -ENODEV;
  2775. }
  2776. if (driver_ops->chip_version != CNSS_CHIP_VER_ANY &&
  2777. driver_ops->chip_version != plat_priv->device_version.major_version) {
  2778. cnss_pr_err("Driver built for chip ver 0x%x, enumerated ver 0x%x, reject unsupported driver\n",
  2779. driver_ops->chip_version,
  2780. plat_priv->device_version.major_version);
  2781. return -ENODEV;
  2782. }
  2783. if (!plat_priv->cbc_enabled ||
  2784. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  2785. goto register_driver;
  2786. pci_priv->driver_ops = driver_ops;
  2787. /* If Cold Boot Calibration is enabled, it is the 1st step in init
  2788. * sequence.CBC is done on file system_ready trigger. Qcacld will be
  2789. * loaded from vendor_modprobe.sh at early boot and must be deferred
  2790. * until CBC is complete
  2791. */
  2792. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_CALIBRATION);
  2793. INIT_DELAYED_WORK(&plat_priv->wlan_reg_driver_work,
  2794. cnss_wlan_reg_driver_work);
  2795. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2796. msecs_to_jiffies(timeout));
  2797. cnss_pr_info("WLAN register driver deferred for Calibration\n");
  2798. return 0;
  2799. register_driver:
  2800. reinit_completion(&plat_priv->power_up_complete);
  2801. ret = cnss_driver_event_post(plat_priv,
  2802. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2803. CNSS_EVENT_SYNC_UNKILLABLE,
  2804. driver_ops);
  2805. return ret;
  2806. }
  2807. EXPORT_SYMBOL(cnss_wlan_register_driver);
  2808. void cnss_wlan_unregister_driver(struct cnss_wlan_driver *driver_ops)
  2809. {
  2810. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2811. int ret = 0;
  2812. unsigned int timeout;
  2813. if (!plat_priv) {
  2814. cnss_pr_err("plat_priv is NULL\n");
  2815. return;
  2816. }
  2817. mutex_lock(&plat_priv->driver_ops_lock);
  2818. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  2819. goto skip_wait_power_up;
  2820. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_WLAN_WATCHDOG);
  2821. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  2822. msecs_to_jiffies(timeout));
  2823. if (!ret) {
  2824. cnss_pr_err("Timeout (%ums) waiting for driver power up to complete\n",
  2825. timeout);
  2826. CNSS_ASSERT(0);
  2827. }
  2828. skip_wait_power_up:
  2829. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2830. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2831. goto skip_wait_recovery;
  2832. reinit_completion(&plat_priv->recovery_complete);
  2833. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  2834. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  2835. msecs_to_jiffies(timeout));
  2836. if (!ret) {
  2837. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  2838. timeout);
  2839. CNSS_ASSERT(0);
  2840. }
  2841. skip_wait_recovery:
  2842. cnss_driver_event_post(plat_priv,
  2843. CNSS_DRIVER_EVENT_UNREGISTER_DRIVER,
  2844. CNSS_EVENT_SYNC_UNKILLABLE, NULL);
  2845. mutex_unlock(&plat_priv->driver_ops_lock);
  2846. }
  2847. EXPORT_SYMBOL(cnss_wlan_unregister_driver);
  2848. int cnss_pci_register_driver_hdlr(struct cnss_pci_data *pci_priv,
  2849. void *data)
  2850. {
  2851. int ret = 0;
  2852. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2853. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2854. cnss_pr_dbg("Reboot or shutdown is in progress, ignore register driver\n");
  2855. return -EINVAL;
  2856. }
  2857. set_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2858. pci_priv->driver_ops = data;
  2859. ret = cnss_pci_dev_powerup(pci_priv);
  2860. if (ret) {
  2861. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2862. pci_priv->driver_ops = NULL;
  2863. }
  2864. return ret;
  2865. }
  2866. int cnss_pci_unregister_driver_hdlr(struct cnss_pci_data *pci_priv)
  2867. {
  2868. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2869. set_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2870. cnss_pci_dev_shutdown(pci_priv);
  2871. pci_priv->driver_ops = NULL;
  2872. return 0;
  2873. }
  2874. #if IS_ENABLED(CONFIG_PCI_MSM)
  2875. static bool cnss_pci_is_drv_supported(struct cnss_pci_data *pci_priv)
  2876. {
  2877. struct pci_dev *root_port = pcie_find_root_port(pci_priv->pci_dev);
  2878. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2879. struct device_node *root_of_node;
  2880. bool drv_supported = false;
  2881. if (!root_port) {
  2882. cnss_pr_err("PCIe DRV is not supported as root port is null\n");
  2883. pci_priv->drv_supported = false;
  2884. return drv_supported;
  2885. }
  2886. root_of_node = root_port->dev.of_node;
  2887. if (root_of_node->parent) {
  2888. drv_supported = of_property_read_bool(root_of_node->parent,
  2889. "qcom,drv-supported") ||
  2890. of_property_read_bool(root_of_node->parent,
  2891. "qcom,drv-name");
  2892. }
  2893. cnss_pr_dbg("PCIe DRV is %s\n",
  2894. drv_supported ? "supported" : "not supported");
  2895. pci_priv->drv_supported = drv_supported;
  2896. if (drv_supported) {
  2897. plat_priv->cap.cap_flag |= CNSS_HAS_DRV_SUPPORT;
  2898. cnss_set_feature_list(plat_priv, CNSS_DRV_SUPPORT_V01);
  2899. }
  2900. return drv_supported;
  2901. }
  2902. static void cnss_pci_event_cb(struct msm_pcie_notify *notify)
  2903. {
  2904. struct pci_dev *pci_dev;
  2905. struct cnss_pci_data *pci_priv;
  2906. struct device *dev;
  2907. struct cnss_plat_data *plat_priv = NULL;
  2908. int ret = 0;
  2909. if (!notify)
  2910. return;
  2911. pci_dev = notify->user;
  2912. if (!pci_dev)
  2913. return;
  2914. pci_priv = cnss_get_pci_priv(pci_dev);
  2915. if (!pci_priv)
  2916. return;
  2917. dev = &pci_priv->pci_dev->dev;
  2918. switch (notify->event) {
  2919. case MSM_PCIE_EVENT_LINK_RECOVER:
  2920. cnss_pr_dbg("PCI link recover callback\n");
  2921. plat_priv = pci_priv->plat_priv;
  2922. if (!plat_priv) {
  2923. cnss_pr_err("plat_priv is NULL\n");
  2924. return;
  2925. }
  2926. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  2927. ret = msm_pcie_pm_control(MSM_PCIE_HANDLE_LINKDOWN,
  2928. pci_dev->bus->number, pci_dev, NULL,
  2929. PM_OPTIONS_DEFAULT);
  2930. if (ret)
  2931. cnss_pci_handle_linkdown(pci_priv);
  2932. break;
  2933. case MSM_PCIE_EVENT_LINKDOWN:
  2934. cnss_pr_dbg("PCI link down event callback\n");
  2935. cnss_pci_handle_linkdown(pci_priv);
  2936. break;
  2937. case MSM_PCIE_EVENT_WAKEUP:
  2938. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  2939. cnss_pci_get_auto_suspended(pci_priv)) ||
  2940. dev->power.runtime_status == RPM_SUSPENDING) {
  2941. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2942. cnss_pci_pm_request_resume(pci_priv);
  2943. }
  2944. break;
  2945. case MSM_PCIE_EVENT_DRV_CONNECT:
  2946. cnss_pr_dbg("DRV subsystem is connected\n");
  2947. cnss_pci_set_drv_connected(pci_priv, 1);
  2948. break;
  2949. case MSM_PCIE_EVENT_DRV_DISCONNECT:
  2950. cnss_pr_dbg("DRV subsystem is disconnected\n");
  2951. if (cnss_pci_get_auto_suspended(pci_priv))
  2952. cnss_pci_pm_request_resume(pci_priv);
  2953. cnss_pci_set_drv_connected(pci_priv, 0);
  2954. break;
  2955. default:
  2956. cnss_pr_err("Received invalid PCI event: %d\n", notify->event);
  2957. }
  2958. }
  2959. /**
  2960. * cnss_reg_pci_event() - Register for PCIe events
  2961. * @pci_priv: driver PCI bus context pointer
  2962. *
  2963. * This function shall call corresponding PCIe root complex driver APIs
  2964. * to register for PCIe events like link down or WAKE GPIO toggling etc.
  2965. * The events should be based on PCIe root complex driver's capability.
  2966. *
  2967. * Return: 0 for success, negative value for error
  2968. */
  2969. static int cnss_reg_pci_event(struct cnss_pci_data *pci_priv)
  2970. {
  2971. int ret = 0;
  2972. struct msm_pcie_register_event *pci_event;
  2973. pci_event = &pci_priv->msm_pci_event;
  2974. pci_event->events = MSM_PCIE_EVENT_LINK_RECOVER |
  2975. MSM_PCIE_EVENT_LINKDOWN |
  2976. MSM_PCIE_EVENT_WAKEUP;
  2977. if (cnss_pci_is_drv_supported(pci_priv))
  2978. pci_event->events = pci_event->events |
  2979. MSM_PCIE_EVENT_DRV_CONNECT |
  2980. MSM_PCIE_EVENT_DRV_DISCONNECT;
  2981. pci_event->user = pci_priv->pci_dev;
  2982. pci_event->mode = MSM_PCIE_TRIGGER_CALLBACK;
  2983. pci_event->callback = cnss_pci_event_cb;
  2984. pci_event->options = MSM_PCIE_CONFIG_NO_RECOVERY;
  2985. ret = msm_pcie_register_event(pci_event);
  2986. if (ret)
  2987. cnss_pr_err("Failed to register MSM PCI event, err = %d\n",
  2988. ret);
  2989. return ret;
  2990. }
  2991. static void cnss_dereg_pci_event(struct cnss_pci_data *pci_priv)
  2992. {
  2993. msm_pcie_deregister_event(&pci_priv->msm_pci_event);
  2994. }
  2995. #else
  2996. static int cnss_reg_pci_event(struct cnss_pci_data *pci_priv)
  2997. {
  2998. return 0;
  2999. }
  3000. static void cnss_dereg_pci_event(struct cnss_pci_data *pci_priv) {}
  3001. #endif
  3002. static int cnss_pci_suspend_driver(struct cnss_pci_data *pci_priv)
  3003. {
  3004. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3005. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3006. int ret = 0;
  3007. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  3008. if (driver_ops && driver_ops->suspend) {
  3009. ret = driver_ops->suspend(pci_dev, state);
  3010. if (ret) {
  3011. cnss_pr_err("Failed to suspend host driver, err = %d\n",
  3012. ret);
  3013. ret = -EAGAIN;
  3014. }
  3015. }
  3016. return ret;
  3017. }
  3018. static int cnss_pci_resume_driver(struct cnss_pci_data *pci_priv)
  3019. {
  3020. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3021. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3022. int ret = 0;
  3023. if (driver_ops && driver_ops->resume) {
  3024. ret = driver_ops->resume(pci_dev);
  3025. if (ret)
  3026. cnss_pr_err("Failed to resume host driver, err = %d\n",
  3027. ret);
  3028. }
  3029. return ret;
  3030. }
  3031. int cnss_pci_suspend_bus(struct cnss_pci_data *pci_priv)
  3032. {
  3033. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3034. int ret = 0;
  3035. if (pci_priv->pci_link_state == PCI_LINK_DOWN)
  3036. goto out;
  3037. if (cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_SUSPEND)) {
  3038. ret = -EAGAIN;
  3039. goto out;
  3040. }
  3041. if (pci_priv->drv_connected_last)
  3042. goto skip_disable_pci;
  3043. pci_clear_master(pci_dev);
  3044. cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  3045. pci_disable_device(pci_dev);
  3046. ret = pci_set_power_state(pci_dev, PCI_D3hot);
  3047. if (ret)
  3048. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  3049. skip_disable_pci:
  3050. if (cnss_set_pci_link(pci_priv, PCI_LINK_DOWN)) {
  3051. ret = -EAGAIN;
  3052. goto resume_mhi;
  3053. }
  3054. pci_priv->pci_link_state = PCI_LINK_DOWN;
  3055. return 0;
  3056. resume_mhi:
  3057. if (!pci_is_enabled(pci_dev))
  3058. if (pci_enable_device(pci_dev))
  3059. cnss_pr_err("Failed to enable PCI device\n");
  3060. if (pci_priv->saved_state)
  3061. cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  3062. pci_set_master(pci_dev);
  3063. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3064. out:
  3065. return ret;
  3066. }
  3067. int cnss_pci_resume_bus(struct cnss_pci_data *pci_priv)
  3068. {
  3069. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3070. int ret = 0;
  3071. if (pci_priv->pci_link_state == PCI_LINK_UP)
  3072. goto out;
  3073. if (cnss_set_pci_link(pci_priv, PCI_LINK_UP)) {
  3074. cnss_fatal_err("Failed to resume PCI link from suspend\n");
  3075. cnss_pci_link_down(&pci_dev->dev);
  3076. ret = -EAGAIN;
  3077. goto out;
  3078. }
  3079. pci_priv->pci_link_state = PCI_LINK_UP;
  3080. if (pci_priv->drv_connected_last)
  3081. goto skip_enable_pci;
  3082. ret = pci_enable_device(pci_dev);
  3083. if (ret) {
  3084. cnss_pr_err("Failed to enable PCI device, err = %d\n",
  3085. ret);
  3086. goto out;
  3087. }
  3088. if (pci_priv->saved_state)
  3089. cnss_set_pci_config_space(pci_priv,
  3090. RESTORE_PCI_CONFIG_SPACE);
  3091. pci_set_master(pci_dev);
  3092. skip_enable_pci:
  3093. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3094. out:
  3095. return ret;
  3096. }
  3097. static int cnss_pci_suspend(struct device *dev)
  3098. {
  3099. int ret = 0;
  3100. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3101. struct cnss_plat_data *plat_priv;
  3102. if (!pci_priv)
  3103. goto out;
  3104. plat_priv = pci_priv->plat_priv;
  3105. if (!plat_priv)
  3106. goto out;
  3107. if (!cnss_is_device_powered_on(plat_priv))
  3108. goto out;
  3109. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3110. pci_priv->drv_supported) {
  3111. pci_priv->drv_connected_last =
  3112. cnss_pci_get_drv_connected(pci_priv);
  3113. if (!pci_priv->drv_connected_last) {
  3114. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3115. ret = -EAGAIN;
  3116. goto out;
  3117. }
  3118. }
  3119. set_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3120. ret = cnss_pci_suspend_driver(pci_priv);
  3121. if (ret)
  3122. goto clear_flag;
  3123. if (!pci_priv->disable_pc) {
  3124. mutex_lock(&pci_priv->bus_lock);
  3125. ret = cnss_pci_suspend_bus(pci_priv);
  3126. mutex_unlock(&pci_priv->bus_lock);
  3127. if (ret)
  3128. goto resume_driver;
  3129. }
  3130. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  3131. return 0;
  3132. resume_driver:
  3133. cnss_pci_resume_driver(pci_priv);
  3134. clear_flag:
  3135. pci_priv->drv_connected_last = 0;
  3136. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3137. out:
  3138. return ret;
  3139. }
  3140. static int cnss_pci_resume(struct device *dev)
  3141. {
  3142. int ret = 0;
  3143. struct pci_dev *pci_dev = to_pci_dev(dev);
  3144. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3145. struct cnss_plat_data *plat_priv;
  3146. if (!pci_priv)
  3147. goto out;
  3148. plat_priv = pci_priv->plat_priv;
  3149. if (!plat_priv)
  3150. goto out;
  3151. if (pci_priv->pci_link_down_ind)
  3152. goto out;
  3153. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3154. goto out;
  3155. if (!pci_priv->disable_pc) {
  3156. ret = cnss_pci_resume_bus(pci_priv);
  3157. if (ret)
  3158. goto out;
  3159. }
  3160. ret = cnss_pci_resume_driver(pci_priv);
  3161. pci_priv->drv_connected_last = 0;
  3162. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3163. out:
  3164. return ret;
  3165. }
  3166. static int cnss_pci_suspend_noirq(struct device *dev)
  3167. {
  3168. int ret = 0;
  3169. struct pci_dev *pci_dev = to_pci_dev(dev);
  3170. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3171. struct cnss_wlan_driver *driver_ops;
  3172. if (!pci_priv)
  3173. goto out;
  3174. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3175. goto out;
  3176. driver_ops = pci_priv->driver_ops;
  3177. if (driver_ops && driver_ops->suspend_noirq)
  3178. ret = driver_ops->suspend_noirq(pci_dev);
  3179. if (pci_priv->disable_pc && !pci_dev->state_saved &&
  3180. !pci_priv->plat_priv->use_pm_domain)
  3181. pci_save_state(pci_dev);
  3182. out:
  3183. return ret;
  3184. }
  3185. static int cnss_pci_resume_noirq(struct device *dev)
  3186. {
  3187. int ret = 0;
  3188. struct pci_dev *pci_dev = to_pci_dev(dev);
  3189. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3190. struct cnss_wlan_driver *driver_ops;
  3191. if (!pci_priv)
  3192. goto out;
  3193. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3194. goto out;
  3195. driver_ops = pci_priv->driver_ops;
  3196. if (driver_ops && driver_ops->resume_noirq &&
  3197. !pci_priv->pci_link_down_ind)
  3198. ret = driver_ops->resume_noirq(pci_dev);
  3199. out:
  3200. return ret;
  3201. }
  3202. static int cnss_pci_runtime_suspend(struct device *dev)
  3203. {
  3204. int ret = 0;
  3205. struct pci_dev *pci_dev = to_pci_dev(dev);
  3206. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3207. struct cnss_plat_data *plat_priv;
  3208. struct cnss_wlan_driver *driver_ops;
  3209. if (!pci_priv)
  3210. return -EAGAIN;
  3211. plat_priv = pci_priv->plat_priv;
  3212. if (!plat_priv)
  3213. return -EAGAIN;
  3214. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3215. return -EAGAIN;
  3216. if (pci_priv->pci_link_down_ind) {
  3217. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3218. return -EAGAIN;
  3219. }
  3220. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3221. pci_priv->drv_supported) {
  3222. pci_priv->drv_connected_last =
  3223. cnss_pci_get_drv_connected(pci_priv);
  3224. if (!pci_priv->drv_connected_last) {
  3225. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3226. return -EAGAIN;
  3227. }
  3228. }
  3229. cnss_pr_vdbg("Runtime suspend start\n");
  3230. driver_ops = pci_priv->driver_ops;
  3231. if (driver_ops && driver_ops->runtime_ops &&
  3232. driver_ops->runtime_ops->runtime_suspend)
  3233. ret = driver_ops->runtime_ops->runtime_suspend(pci_dev);
  3234. else
  3235. ret = cnss_auto_suspend(dev);
  3236. if (ret)
  3237. pci_priv->drv_connected_last = 0;
  3238. cnss_pr_vdbg("Runtime suspend status: %d\n", ret);
  3239. return ret;
  3240. }
  3241. static int cnss_pci_runtime_resume(struct device *dev)
  3242. {
  3243. int ret = 0;
  3244. struct pci_dev *pci_dev = to_pci_dev(dev);
  3245. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3246. struct cnss_wlan_driver *driver_ops;
  3247. if (!pci_priv)
  3248. return -EAGAIN;
  3249. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3250. return -EAGAIN;
  3251. if (pci_priv->pci_link_down_ind) {
  3252. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3253. return -EAGAIN;
  3254. }
  3255. cnss_pr_vdbg("Runtime resume start\n");
  3256. driver_ops = pci_priv->driver_ops;
  3257. if (driver_ops && driver_ops->runtime_ops &&
  3258. driver_ops->runtime_ops->runtime_resume)
  3259. ret = driver_ops->runtime_ops->runtime_resume(pci_dev);
  3260. else
  3261. ret = cnss_auto_resume(dev);
  3262. if (!ret)
  3263. pci_priv->drv_connected_last = 0;
  3264. cnss_pr_vdbg("Runtime resume status: %d\n", ret);
  3265. return ret;
  3266. }
  3267. static int cnss_pci_runtime_idle(struct device *dev)
  3268. {
  3269. cnss_pr_vdbg("Runtime idle\n");
  3270. pm_request_autosuspend(dev);
  3271. return -EBUSY;
  3272. }
  3273. int cnss_wlan_pm_control(struct device *dev, bool vote)
  3274. {
  3275. struct pci_dev *pci_dev = to_pci_dev(dev);
  3276. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3277. int ret = 0;
  3278. if (!pci_priv)
  3279. return -ENODEV;
  3280. ret = cnss_pci_disable_pc(pci_priv, vote);
  3281. if (ret)
  3282. return ret;
  3283. pci_priv->disable_pc = vote;
  3284. cnss_pr_dbg("%s PCIe power collapse\n", vote ? "disable" : "enable");
  3285. return 0;
  3286. }
  3287. EXPORT_SYMBOL(cnss_wlan_pm_control);
  3288. static void cnss_pci_pm_runtime_get_record(struct cnss_pci_data *pci_priv,
  3289. enum cnss_rtpm_id id)
  3290. {
  3291. if (id >= RTPM_ID_MAX)
  3292. return;
  3293. atomic_inc(&pci_priv->pm_stats.runtime_get);
  3294. atomic_inc(&pci_priv->pm_stats.runtime_get_id[id]);
  3295. pci_priv->pm_stats.runtime_get_timestamp_id[id] =
  3296. cnss_get_host_timestamp(pci_priv->plat_priv);
  3297. }
  3298. static void cnss_pci_pm_runtime_put_record(struct cnss_pci_data *pci_priv,
  3299. enum cnss_rtpm_id id)
  3300. {
  3301. if (id >= RTPM_ID_MAX)
  3302. return;
  3303. atomic_inc(&pci_priv->pm_stats.runtime_put);
  3304. atomic_inc(&pci_priv->pm_stats.runtime_put_id[id]);
  3305. pci_priv->pm_stats.runtime_put_timestamp_id[id] =
  3306. cnss_get_host_timestamp(pci_priv->plat_priv);
  3307. }
  3308. void cnss_pci_pm_runtime_show_usage_count(struct cnss_pci_data *pci_priv)
  3309. {
  3310. struct device *dev;
  3311. if (!pci_priv)
  3312. return;
  3313. dev = &pci_priv->pci_dev->dev;
  3314. cnss_pr_dbg("Runtime PM usage count: %d\n",
  3315. atomic_read(&dev->power.usage_count));
  3316. }
  3317. int cnss_pci_pm_request_resume(struct cnss_pci_data *pci_priv)
  3318. {
  3319. struct device *dev;
  3320. enum rpm_status status;
  3321. if (!pci_priv)
  3322. return -ENODEV;
  3323. dev = &pci_priv->pci_dev->dev;
  3324. status = dev->power.runtime_status;
  3325. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3326. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3327. (void *)_RET_IP_);
  3328. return pm_request_resume(dev);
  3329. }
  3330. int cnss_pci_pm_runtime_resume(struct cnss_pci_data *pci_priv)
  3331. {
  3332. struct device *dev;
  3333. enum rpm_status status;
  3334. if (!pci_priv)
  3335. return -ENODEV;
  3336. dev = &pci_priv->pci_dev->dev;
  3337. status = dev->power.runtime_status;
  3338. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3339. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3340. (void *)_RET_IP_);
  3341. return pm_runtime_resume(dev);
  3342. }
  3343. int cnss_pci_pm_runtime_get(struct cnss_pci_data *pci_priv,
  3344. enum cnss_rtpm_id id)
  3345. {
  3346. struct device *dev;
  3347. enum rpm_status status;
  3348. if (!pci_priv)
  3349. return -ENODEV;
  3350. dev = &pci_priv->pci_dev->dev;
  3351. status = dev->power.runtime_status;
  3352. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3353. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3354. (void *)_RET_IP_);
  3355. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3356. return pm_runtime_get(dev);
  3357. }
  3358. int cnss_pci_pm_runtime_get_sync(struct cnss_pci_data *pci_priv,
  3359. enum cnss_rtpm_id id)
  3360. {
  3361. struct device *dev;
  3362. enum rpm_status status;
  3363. if (!pci_priv)
  3364. return -ENODEV;
  3365. dev = &pci_priv->pci_dev->dev;
  3366. status = dev->power.runtime_status;
  3367. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3368. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3369. (void *)_RET_IP_);
  3370. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3371. return pm_runtime_get_sync(dev);
  3372. }
  3373. void cnss_pci_pm_runtime_get_noresume(struct cnss_pci_data *pci_priv,
  3374. enum cnss_rtpm_id id)
  3375. {
  3376. if (!pci_priv)
  3377. return;
  3378. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3379. pm_runtime_get_noresume(&pci_priv->pci_dev->dev);
  3380. }
  3381. int cnss_pci_pm_runtime_put_autosuspend(struct cnss_pci_data *pci_priv,
  3382. enum cnss_rtpm_id id)
  3383. {
  3384. struct device *dev;
  3385. if (!pci_priv)
  3386. return -ENODEV;
  3387. dev = &pci_priv->pci_dev->dev;
  3388. if (atomic_read(&dev->power.usage_count) == 0) {
  3389. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3390. return -EINVAL;
  3391. }
  3392. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3393. return pm_runtime_put_autosuspend(&pci_priv->pci_dev->dev);
  3394. }
  3395. void cnss_pci_pm_runtime_put_noidle(struct cnss_pci_data *pci_priv,
  3396. enum cnss_rtpm_id id)
  3397. {
  3398. struct device *dev;
  3399. if (!pci_priv)
  3400. return;
  3401. dev = &pci_priv->pci_dev->dev;
  3402. if (atomic_read(&dev->power.usage_count) == 0) {
  3403. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3404. return;
  3405. }
  3406. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3407. pm_runtime_put_noidle(&pci_priv->pci_dev->dev);
  3408. }
  3409. void cnss_pci_pm_runtime_mark_last_busy(struct cnss_pci_data *pci_priv)
  3410. {
  3411. if (!pci_priv)
  3412. return;
  3413. pm_runtime_mark_last_busy(&pci_priv->pci_dev->dev);
  3414. }
  3415. int cnss_auto_suspend(struct device *dev)
  3416. {
  3417. int ret = 0;
  3418. struct pci_dev *pci_dev = to_pci_dev(dev);
  3419. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3420. struct cnss_plat_data *plat_priv;
  3421. if (!pci_priv)
  3422. return -ENODEV;
  3423. plat_priv = pci_priv->plat_priv;
  3424. if (!plat_priv)
  3425. return -ENODEV;
  3426. mutex_lock(&pci_priv->bus_lock);
  3427. if (!pci_priv->qmi_send_usage_count) {
  3428. ret = cnss_pci_suspend_bus(pci_priv);
  3429. if (ret) {
  3430. mutex_unlock(&pci_priv->bus_lock);
  3431. return ret;
  3432. }
  3433. }
  3434. cnss_pci_set_auto_suspended(pci_priv, 1);
  3435. mutex_unlock(&pci_priv->bus_lock);
  3436. cnss_pci_set_monitor_wake_intr(pci_priv, true);
  3437. /* For suspend temporarily set bandwidth vote to NONE and dont save in
  3438. * current_bw_vote as in resume path we should vote for last used
  3439. * bandwidth vote. Also ignore error if bw voting is not setup.
  3440. */
  3441. cnss_setup_bus_bandwidth(plat_priv, CNSS_BUS_WIDTH_NONE, false);
  3442. return 0;
  3443. }
  3444. EXPORT_SYMBOL(cnss_auto_suspend);
  3445. int cnss_auto_resume(struct device *dev)
  3446. {
  3447. int ret = 0;
  3448. struct pci_dev *pci_dev = to_pci_dev(dev);
  3449. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3450. struct cnss_plat_data *plat_priv;
  3451. if (!pci_priv)
  3452. return -ENODEV;
  3453. plat_priv = pci_priv->plat_priv;
  3454. if (!plat_priv)
  3455. return -ENODEV;
  3456. mutex_lock(&pci_priv->bus_lock);
  3457. ret = cnss_pci_resume_bus(pci_priv);
  3458. if (ret) {
  3459. mutex_unlock(&pci_priv->bus_lock);
  3460. return ret;
  3461. }
  3462. cnss_pci_set_auto_suspended(pci_priv, 0);
  3463. mutex_unlock(&pci_priv->bus_lock);
  3464. cnss_request_bus_bandwidth(dev, plat_priv->icc.current_bw_vote);
  3465. return 0;
  3466. }
  3467. EXPORT_SYMBOL(cnss_auto_resume);
  3468. int cnss_pci_force_wake_request_sync(struct device *dev, int timeout_us)
  3469. {
  3470. struct pci_dev *pci_dev = to_pci_dev(dev);
  3471. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3472. struct cnss_plat_data *plat_priv;
  3473. struct mhi_controller *mhi_ctrl;
  3474. if (!pci_priv)
  3475. return -ENODEV;
  3476. switch (pci_priv->device_id) {
  3477. case QCA6390_DEVICE_ID:
  3478. case QCA6490_DEVICE_ID:
  3479. case KIWI_DEVICE_ID:
  3480. break;
  3481. default:
  3482. return 0;
  3483. }
  3484. mhi_ctrl = pci_priv->mhi_ctrl;
  3485. if (!mhi_ctrl)
  3486. return -EINVAL;
  3487. plat_priv = pci_priv->plat_priv;
  3488. if (!plat_priv)
  3489. return -ENODEV;
  3490. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3491. return -EAGAIN;
  3492. if (timeout_us) {
  3493. /* Busy wait for timeout_us */
  3494. return cnss_mhi_device_get_sync_atomic(pci_priv,
  3495. timeout_us, false);
  3496. } else {
  3497. /* Sleep wait for mhi_ctrl->timeout_ms */
  3498. return mhi_device_get_sync(mhi_ctrl->mhi_dev);
  3499. }
  3500. }
  3501. EXPORT_SYMBOL(cnss_pci_force_wake_request_sync);
  3502. int cnss_pci_force_wake_request(struct device *dev)
  3503. {
  3504. struct pci_dev *pci_dev = to_pci_dev(dev);
  3505. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3506. struct cnss_plat_data *plat_priv;
  3507. struct mhi_controller *mhi_ctrl;
  3508. if (!pci_priv)
  3509. return -ENODEV;
  3510. switch (pci_priv->device_id) {
  3511. case QCA6390_DEVICE_ID:
  3512. case QCA6490_DEVICE_ID:
  3513. case KIWI_DEVICE_ID:
  3514. break;
  3515. default:
  3516. return 0;
  3517. }
  3518. mhi_ctrl = pci_priv->mhi_ctrl;
  3519. if (!mhi_ctrl)
  3520. return -EINVAL;
  3521. plat_priv = pci_priv->plat_priv;
  3522. if (!plat_priv)
  3523. return -ENODEV;
  3524. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3525. return -EAGAIN;
  3526. mhi_device_get(mhi_ctrl->mhi_dev);
  3527. return 0;
  3528. }
  3529. EXPORT_SYMBOL(cnss_pci_force_wake_request);
  3530. int cnss_pci_is_device_awake(struct device *dev)
  3531. {
  3532. struct pci_dev *pci_dev = to_pci_dev(dev);
  3533. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3534. struct mhi_controller *mhi_ctrl;
  3535. if (!pci_priv)
  3536. return -ENODEV;
  3537. switch (pci_priv->device_id) {
  3538. case QCA6390_DEVICE_ID:
  3539. case QCA6490_DEVICE_ID:
  3540. case KIWI_DEVICE_ID:
  3541. break;
  3542. default:
  3543. return 0;
  3544. }
  3545. mhi_ctrl = pci_priv->mhi_ctrl;
  3546. if (!mhi_ctrl)
  3547. return -EINVAL;
  3548. return (mhi_ctrl->dev_state == MHI_STATE_M0);
  3549. }
  3550. EXPORT_SYMBOL(cnss_pci_is_device_awake);
  3551. int cnss_pci_force_wake_release(struct device *dev)
  3552. {
  3553. struct pci_dev *pci_dev = to_pci_dev(dev);
  3554. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3555. struct cnss_plat_data *plat_priv;
  3556. struct mhi_controller *mhi_ctrl;
  3557. if (!pci_priv)
  3558. return -ENODEV;
  3559. switch (pci_priv->device_id) {
  3560. case QCA6390_DEVICE_ID:
  3561. case QCA6490_DEVICE_ID:
  3562. case KIWI_DEVICE_ID:
  3563. break;
  3564. default:
  3565. return 0;
  3566. }
  3567. mhi_ctrl = pci_priv->mhi_ctrl;
  3568. if (!mhi_ctrl)
  3569. return -EINVAL;
  3570. plat_priv = pci_priv->plat_priv;
  3571. if (!plat_priv)
  3572. return -ENODEV;
  3573. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3574. return -EAGAIN;
  3575. mhi_device_put(mhi_ctrl->mhi_dev);
  3576. return 0;
  3577. }
  3578. EXPORT_SYMBOL(cnss_pci_force_wake_release);
  3579. int cnss_pci_qmi_send_get(struct cnss_pci_data *pci_priv)
  3580. {
  3581. int ret = 0;
  3582. if (!pci_priv)
  3583. return -ENODEV;
  3584. mutex_lock(&pci_priv->bus_lock);
  3585. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3586. !pci_priv->qmi_send_usage_count)
  3587. ret = cnss_pci_resume_bus(pci_priv);
  3588. pci_priv->qmi_send_usage_count++;
  3589. cnss_pr_buf("Increased QMI send usage count to %d\n",
  3590. pci_priv->qmi_send_usage_count);
  3591. mutex_unlock(&pci_priv->bus_lock);
  3592. return ret;
  3593. }
  3594. int cnss_pci_qmi_send_put(struct cnss_pci_data *pci_priv)
  3595. {
  3596. int ret = 0;
  3597. if (!pci_priv)
  3598. return -ENODEV;
  3599. mutex_lock(&pci_priv->bus_lock);
  3600. if (pci_priv->qmi_send_usage_count)
  3601. pci_priv->qmi_send_usage_count--;
  3602. cnss_pr_buf("Decreased QMI send usage count to %d\n",
  3603. pci_priv->qmi_send_usage_count);
  3604. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3605. !pci_priv->qmi_send_usage_count &&
  3606. !cnss_pcie_is_device_down(pci_priv))
  3607. ret = cnss_pci_suspend_bus(pci_priv);
  3608. mutex_unlock(&pci_priv->bus_lock);
  3609. return ret;
  3610. }
  3611. int cnss_pci_alloc_fw_mem(struct cnss_pci_data *pci_priv)
  3612. {
  3613. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3614. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3615. struct device *dev = &pci_priv->pci_dev->dev;
  3616. int i;
  3617. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3618. if (!fw_mem[i].va && fw_mem[i].size) {
  3619. fw_mem[i].va =
  3620. dma_alloc_attrs(dev, fw_mem[i].size,
  3621. &fw_mem[i].pa, GFP_KERNEL,
  3622. fw_mem[i].attrs);
  3623. if (!fw_mem[i].va) {
  3624. cnss_pr_err("Failed to allocate memory for FW, size: 0x%zx, type: %u\n",
  3625. fw_mem[i].size, fw_mem[i].type);
  3626. return -ENOMEM;
  3627. }
  3628. }
  3629. }
  3630. return 0;
  3631. }
  3632. static void cnss_pci_free_fw_mem(struct cnss_pci_data *pci_priv)
  3633. {
  3634. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3635. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3636. struct device *dev = &pci_priv->pci_dev->dev;
  3637. int i;
  3638. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3639. if (fw_mem[i].va && fw_mem[i].size) {
  3640. cnss_pr_dbg("Freeing memory for FW, va: 0x%pK, pa: %pa, size: 0x%zx, type: %u\n",
  3641. fw_mem[i].va, &fw_mem[i].pa,
  3642. fw_mem[i].size, fw_mem[i].type);
  3643. dma_free_attrs(dev, fw_mem[i].size,
  3644. fw_mem[i].va, fw_mem[i].pa,
  3645. fw_mem[i].attrs);
  3646. fw_mem[i].va = NULL;
  3647. fw_mem[i].pa = 0;
  3648. fw_mem[i].size = 0;
  3649. fw_mem[i].type = 0;
  3650. }
  3651. }
  3652. plat_priv->fw_mem_seg_len = 0;
  3653. }
  3654. int cnss_pci_alloc_qdss_mem(struct cnss_pci_data *pci_priv)
  3655. {
  3656. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3657. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3658. int i, j;
  3659. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3660. if (!qdss_mem[i].va && qdss_mem[i].size) {
  3661. qdss_mem[i].va =
  3662. dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3663. qdss_mem[i].size,
  3664. &qdss_mem[i].pa,
  3665. GFP_KERNEL);
  3666. if (!qdss_mem[i].va) {
  3667. cnss_pr_err("Failed to allocate QDSS memory for FW, size: 0x%zx, type: %u, chuck-ID: %d\n",
  3668. qdss_mem[i].size,
  3669. qdss_mem[i].type, i);
  3670. break;
  3671. }
  3672. }
  3673. }
  3674. /* Best-effort allocation for QDSS trace */
  3675. if (i < plat_priv->qdss_mem_seg_len) {
  3676. for (j = i; j < plat_priv->qdss_mem_seg_len; j++) {
  3677. qdss_mem[j].type = 0;
  3678. qdss_mem[j].size = 0;
  3679. }
  3680. plat_priv->qdss_mem_seg_len = i;
  3681. }
  3682. return 0;
  3683. }
  3684. void cnss_pci_free_qdss_mem(struct cnss_pci_data *pci_priv)
  3685. {
  3686. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3687. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3688. int i;
  3689. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3690. if (qdss_mem[i].va && qdss_mem[i].size) {
  3691. cnss_pr_dbg("Freeing memory for QDSS: pa: %pa, size: 0x%zx, type: %u\n",
  3692. &qdss_mem[i].pa, qdss_mem[i].size,
  3693. qdss_mem[i].type);
  3694. dma_free_coherent(&pci_priv->pci_dev->dev,
  3695. qdss_mem[i].size, qdss_mem[i].va,
  3696. qdss_mem[i].pa);
  3697. qdss_mem[i].va = NULL;
  3698. qdss_mem[i].pa = 0;
  3699. qdss_mem[i].size = 0;
  3700. qdss_mem[i].type = 0;
  3701. }
  3702. }
  3703. plat_priv->qdss_mem_seg_len = 0;
  3704. }
  3705. int cnss_pci_load_m3(struct cnss_pci_data *pci_priv)
  3706. {
  3707. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3708. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3709. char filename[MAX_FIRMWARE_NAME_LEN];
  3710. char *phy_filename = DEFAULT_PHY_UCODE_FILE_NAME;
  3711. const struct firmware *fw_entry;
  3712. int ret = 0;
  3713. /* Use forward compatibility here since for any recent device
  3714. * it should use DEFAULT_PHY_UCODE_FILE_NAME.
  3715. */
  3716. switch (pci_priv->device_id) {
  3717. case QCA6174_DEVICE_ID:
  3718. cnss_pr_err("Invalid device ID (0x%x) to load phy image\n",
  3719. pci_priv->device_id);
  3720. return -EINVAL;
  3721. case QCA6290_DEVICE_ID:
  3722. case QCA6390_DEVICE_ID:
  3723. case QCA6490_DEVICE_ID:
  3724. phy_filename = DEFAULT_PHY_M3_FILE_NAME;
  3725. break;
  3726. case KIWI_DEVICE_ID:
  3727. switch (plat_priv->device_version.major_version) {
  3728. case FW_V2_NUMBER:
  3729. phy_filename = PHY_UCODE_V2_FILE_NAME;
  3730. break;
  3731. default:
  3732. break;
  3733. }
  3734. break;
  3735. default:
  3736. break;
  3737. }
  3738. if (!m3_mem->va && !m3_mem->size) {
  3739. cnss_pci_add_fw_prefix_name(pci_priv, filename,
  3740. phy_filename);
  3741. ret = firmware_request_nowarn(&fw_entry, filename,
  3742. &pci_priv->pci_dev->dev);
  3743. if (ret) {
  3744. cnss_pr_err("Failed to load M3 image: %s\n", filename);
  3745. return ret;
  3746. }
  3747. m3_mem->va = dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3748. fw_entry->size, &m3_mem->pa,
  3749. GFP_KERNEL);
  3750. if (!m3_mem->va) {
  3751. cnss_pr_err("Failed to allocate memory for M3, size: 0x%zx\n",
  3752. fw_entry->size);
  3753. release_firmware(fw_entry);
  3754. return -ENOMEM;
  3755. }
  3756. memcpy(m3_mem->va, fw_entry->data, fw_entry->size);
  3757. m3_mem->size = fw_entry->size;
  3758. release_firmware(fw_entry);
  3759. }
  3760. return 0;
  3761. }
  3762. static void cnss_pci_free_m3_mem(struct cnss_pci_data *pci_priv)
  3763. {
  3764. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3765. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3766. if (m3_mem->va && m3_mem->size) {
  3767. cnss_pr_dbg("Freeing memory for M3, va: 0x%pK, pa: %pa, size: 0x%zx\n",
  3768. m3_mem->va, &m3_mem->pa, m3_mem->size);
  3769. dma_free_coherent(&pci_priv->pci_dev->dev, m3_mem->size,
  3770. m3_mem->va, m3_mem->pa);
  3771. }
  3772. m3_mem->va = NULL;
  3773. m3_mem->pa = 0;
  3774. m3_mem->size = 0;
  3775. }
  3776. void cnss_pci_fw_boot_timeout_hdlr(struct cnss_pci_data *pci_priv)
  3777. {
  3778. struct cnss_plat_data *plat_priv;
  3779. if (!pci_priv)
  3780. return;
  3781. cnss_fatal_err("Timeout waiting for FW ready indication\n");
  3782. plat_priv = pci_priv->plat_priv;
  3783. if (!plat_priv)
  3784. return;
  3785. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  3786. cnss_pr_dbg("Ignore FW ready timeout for calibration mode\n");
  3787. return;
  3788. }
  3789. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  3790. CNSS_REASON_TIMEOUT);
  3791. }
  3792. static int cnss_pci_smmu_fault_handler(struct iommu_domain *domain,
  3793. struct device *dev, unsigned long iova,
  3794. int flags, void *handler_token)
  3795. {
  3796. struct cnss_pci_data *pci_priv = handler_token;
  3797. cnss_fatal_err("SMMU fault happened with IOVA 0x%lx\n", iova);
  3798. if (!pci_priv) {
  3799. cnss_pr_err("pci_priv is NULL\n");
  3800. return -ENODEV;
  3801. }
  3802. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  3803. cnss_force_fw_assert(&pci_priv->pci_dev->dev);
  3804. /* IOMMU driver requires -ENOSYS to print debug info. */
  3805. return -ENOSYS;
  3806. }
  3807. static int cnss_pci_init_smmu(struct cnss_pci_data *pci_priv)
  3808. {
  3809. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3810. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3811. struct device_node *of_node;
  3812. struct resource *res;
  3813. const char *iommu_dma_type;
  3814. u32 addr_win[2];
  3815. int ret = 0;
  3816. of_node = of_parse_phandle(pci_dev->dev.of_node, "qcom,iommu-group", 0);
  3817. if (!of_node)
  3818. return ret;
  3819. cnss_pr_dbg("Initializing SMMU\n");
  3820. pci_priv->iommu_domain = iommu_get_domain_for_dev(&pci_dev->dev);
  3821. ret = of_property_read_string(of_node, "qcom,iommu-dma",
  3822. &iommu_dma_type);
  3823. if (!ret && !strcmp("fastmap", iommu_dma_type)) {
  3824. cnss_pr_dbg("Enabling SMMU S1 stage\n");
  3825. pci_priv->smmu_s1_enable = true;
  3826. iommu_set_fault_handler(pci_priv->iommu_domain,
  3827. cnss_pci_smmu_fault_handler, pci_priv);
  3828. }
  3829. ret = of_property_read_u32_array(of_node, "qcom,iommu-dma-addr-pool",
  3830. addr_win, ARRAY_SIZE(addr_win));
  3831. if (ret) {
  3832. cnss_pr_err("Invalid SMMU size window, err = %d\n", ret);
  3833. of_node_put(of_node);
  3834. return ret;
  3835. }
  3836. pci_priv->smmu_iova_start = addr_win[0];
  3837. pci_priv->smmu_iova_len = addr_win[1];
  3838. cnss_pr_dbg("smmu_iova_start: %pa, smmu_iova_len: 0x%zx\n",
  3839. &pci_priv->smmu_iova_start,
  3840. pci_priv->smmu_iova_len);
  3841. res = platform_get_resource_byname(plat_priv->plat_dev, IORESOURCE_MEM,
  3842. "smmu_iova_ipa");
  3843. if (res) {
  3844. pci_priv->smmu_iova_ipa_start = res->start;
  3845. pci_priv->smmu_iova_ipa_current = res->start;
  3846. pci_priv->smmu_iova_ipa_len = resource_size(res);
  3847. cnss_pr_dbg("smmu_iova_ipa_start: %pa, smmu_iova_ipa_len: 0x%zx\n",
  3848. &pci_priv->smmu_iova_ipa_start,
  3849. pci_priv->smmu_iova_ipa_len);
  3850. }
  3851. pci_priv->iommu_geometry = of_property_read_bool(of_node,
  3852. "qcom,iommu-geometry");
  3853. cnss_pr_dbg("iommu_geometry: %d\n", pci_priv->iommu_geometry);
  3854. of_node_put(of_node);
  3855. return 0;
  3856. }
  3857. static void cnss_pci_deinit_smmu(struct cnss_pci_data *pci_priv)
  3858. {
  3859. pci_priv->iommu_domain = NULL;
  3860. }
  3861. int cnss_pci_get_iova(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3862. {
  3863. if (!pci_priv)
  3864. return -ENODEV;
  3865. if (!pci_priv->smmu_iova_len)
  3866. return -EINVAL;
  3867. *addr = pci_priv->smmu_iova_start;
  3868. *size = pci_priv->smmu_iova_len;
  3869. return 0;
  3870. }
  3871. int cnss_pci_get_iova_ipa(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3872. {
  3873. if (!pci_priv)
  3874. return -ENODEV;
  3875. if (!pci_priv->smmu_iova_ipa_len)
  3876. return -EINVAL;
  3877. *addr = pci_priv->smmu_iova_ipa_start;
  3878. *size = pci_priv->smmu_iova_ipa_len;
  3879. return 0;
  3880. }
  3881. struct iommu_domain *cnss_smmu_get_domain(struct device *dev)
  3882. {
  3883. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3884. if (!pci_priv)
  3885. return NULL;
  3886. return pci_priv->iommu_domain;
  3887. }
  3888. EXPORT_SYMBOL(cnss_smmu_get_domain);
  3889. int cnss_smmu_map(struct device *dev,
  3890. phys_addr_t paddr, uint32_t *iova_addr, size_t size)
  3891. {
  3892. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3893. struct cnss_plat_data *plat_priv;
  3894. unsigned long iova;
  3895. size_t len;
  3896. int ret = 0;
  3897. int flag = IOMMU_READ | IOMMU_WRITE;
  3898. struct pci_dev *root_port;
  3899. struct device_node *root_of_node;
  3900. bool dma_coherent = false;
  3901. if (!pci_priv)
  3902. return -ENODEV;
  3903. if (!iova_addr) {
  3904. cnss_pr_err("iova_addr is NULL, paddr %pa, size %zu\n",
  3905. &paddr, size);
  3906. return -EINVAL;
  3907. }
  3908. plat_priv = pci_priv->plat_priv;
  3909. len = roundup(size + paddr - rounddown(paddr, PAGE_SIZE), PAGE_SIZE);
  3910. iova = roundup(pci_priv->smmu_iova_ipa_current, PAGE_SIZE);
  3911. if (pci_priv->iommu_geometry &&
  3912. iova >= pci_priv->smmu_iova_ipa_start +
  3913. pci_priv->smmu_iova_ipa_len) {
  3914. cnss_pr_err("No IOVA space to map, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3915. iova,
  3916. &pci_priv->smmu_iova_ipa_start,
  3917. pci_priv->smmu_iova_ipa_len);
  3918. return -ENOMEM;
  3919. }
  3920. if (!test_bit(DISABLE_IO_COHERENCY,
  3921. &plat_priv->ctrl_params.quirks)) {
  3922. root_port = pcie_find_root_port(pci_priv->pci_dev);
  3923. if (!root_port) {
  3924. cnss_pr_err("Root port is null, so dma_coherent is disabled\n");
  3925. } else {
  3926. root_of_node = root_port->dev.of_node;
  3927. if (root_of_node && root_of_node->parent) {
  3928. dma_coherent =
  3929. of_property_read_bool(root_of_node->parent,
  3930. "dma-coherent");
  3931. cnss_pr_dbg("dma-coherent is %s\n",
  3932. dma_coherent ? "enabled" : "disabled");
  3933. if (dma_coherent)
  3934. flag |= IOMMU_CACHE;
  3935. }
  3936. }
  3937. }
  3938. cnss_pr_dbg("IOMMU map: iova %lx, len %zu\n", iova, len);
  3939. ret = iommu_map(pci_priv->iommu_domain, iova,
  3940. rounddown(paddr, PAGE_SIZE), len, flag);
  3941. if (ret) {
  3942. cnss_pr_err("PA to IOVA mapping failed, ret %d\n", ret);
  3943. return ret;
  3944. }
  3945. pci_priv->smmu_iova_ipa_current = iova + len;
  3946. *iova_addr = (uint32_t)(iova + paddr - rounddown(paddr, PAGE_SIZE));
  3947. cnss_pr_dbg("IOMMU map: iova_addr %lx\n", *iova_addr);
  3948. return 0;
  3949. }
  3950. EXPORT_SYMBOL(cnss_smmu_map);
  3951. int cnss_smmu_unmap(struct device *dev, uint32_t iova_addr, size_t size)
  3952. {
  3953. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3954. unsigned long iova;
  3955. size_t unmapped;
  3956. size_t len;
  3957. if (!pci_priv)
  3958. return -ENODEV;
  3959. iova = rounddown(iova_addr, PAGE_SIZE);
  3960. len = roundup(size + iova_addr - iova, PAGE_SIZE);
  3961. if (iova >= pci_priv->smmu_iova_ipa_start +
  3962. pci_priv->smmu_iova_ipa_len) {
  3963. cnss_pr_err("Out of IOVA space to unmap, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3964. iova,
  3965. &pci_priv->smmu_iova_ipa_start,
  3966. pci_priv->smmu_iova_ipa_len);
  3967. return -ENOMEM;
  3968. }
  3969. cnss_pr_dbg("IOMMU unmap: iova %lx, len %zu\n", iova, len);
  3970. unmapped = iommu_unmap(pci_priv->iommu_domain, iova, len);
  3971. if (unmapped != len) {
  3972. cnss_pr_err("IOMMU unmap failed, unmapped = %zu, requested = %zu\n",
  3973. unmapped, len);
  3974. return -EINVAL;
  3975. }
  3976. pci_priv->smmu_iova_ipa_current = iova;
  3977. return 0;
  3978. }
  3979. EXPORT_SYMBOL(cnss_smmu_unmap);
  3980. int cnss_get_soc_info(struct device *dev, struct cnss_soc_info *info)
  3981. {
  3982. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3983. struct cnss_plat_data *plat_priv;
  3984. if (!pci_priv)
  3985. return -ENODEV;
  3986. plat_priv = pci_priv->plat_priv;
  3987. if (!plat_priv)
  3988. return -ENODEV;
  3989. info->va = pci_priv->bar;
  3990. info->pa = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  3991. info->chip_id = plat_priv->chip_info.chip_id;
  3992. info->chip_family = plat_priv->chip_info.chip_family;
  3993. info->board_id = plat_priv->board_info.board_id;
  3994. info->soc_id = plat_priv->soc_info.soc_id;
  3995. info->fw_version = plat_priv->fw_version_info.fw_version;
  3996. strlcpy(info->fw_build_timestamp,
  3997. plat_priv->fw_version_info.fw_build_timestamp,
  3998. sizeof(info->fw_build_timestamp));
  3999. memcpy(&info->device_version, &plat_priv->device_version,
  4000. sizeof(info->device_version));
  4001. memcpy(&info->dev_mem_info, &plat_priv->dev_mem_info,
  4002. sizeof(info->dev_mem_info));
  4003. return 0;
  4004. }
  4005. EXPORT_SYMBOL(cnss_get_soc_info);
  4006. static struct cnss_msi_config msi_config = {
  4007. .total_vectors = 32,
  4008. .total_users = 4,
  4009. .users = (struct cnss_msi_user[]) {
  4010. { .name = "MHI", .num_vectors = 3, .base_vector = 0 },
  4011. { .name = "CE", .num_vectors = 10, .base_vector = 3 },
  4012. { .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
  4013. { .name = "DP", .num_vectors = 18, .base_vector = 14 },
  4014. },
  4015. };
  4016. static int cnss_pci_get_msi_assignment(struct cnss_pci_data *pci_priv)
  4017. {
  4018. pci_priv->msi_config = &msi_config;
  4019. return 0;
  4020. }
  4021. static int cnss_pci_enable_msi(struct cnss_pci_data *pci_priv)
  4022. {
  4023. int ret = 0;
  4024. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4025. int num_vectors;
  4026. struct cnss_msi_config *msi_config;
  4027. struct msi_desc *msi_desc;
  4028. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4029. return 0;
  4030. ret = cnss_pci_get_msi_assignment(pci_priv);
  4031. if (ret) {
  4032. cnss_pr_err("Failed to get MSI assignment, err = %d\n", ret);
  4033. goto out;
  4034. }
  4035. msi_config = pci_priv->msi_config;
  4036. if (!msi_config) {
  4037. cnss_pr_err("msi_config is NULL!\n");
  4038. ret = -EINVAL;
  4039. goto out;
  4040. }
  4041. num_vectors = pci_alloc_irq_vectors(pci_dev,
  4042. msi_config->total_vectors,
  4043. msi_config->total_vectors,
  4044. PCI_IRQ_MSI);
  4045. if (num_vectors != msi_config->total_vectors) {
  4046. cnss_pr_err("Failed to get enough MSI vectors (%d), available vectors = %d",
  4047. msi_config->total_vectors, num_vectors);
  4048. if (num_vectors >= 0)
  4049. ret = -EINVAL;
  4050. goto reset_msi_config;
  4051. }
  4052. msi_desc = irq_get_msi_desc(pci_dev->irq);
  4053. if (!msi_desc) {
  4054. cnss_pr_err("msi_desc is NULL!\n");
  4055. ret = -EINVAL;
  4056. goto free_msi_vector;
  4057. }
  4058. pci_priv->msi_ep_base_data = msi_desc->msg.data;
  4059. cnss_pr_dbg("MSI base data is %d\n", pci_priv->msi_ep_base_data);
  4060. return 0;
  4061. free_msi_vector:
  4062. pci_free_irq_vectors(pci_priv->pci_dev);
  4063. reset_msi_config:
  4064. pci_priv->msi_config = NULL;
  4065. out:
  4066. return ret;
  4067. }
  4068. static void cnss_pci_disable_msi(struct cnss_pci_data *pci_priv)
  4069. {
  4070. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4071. return;
  4072. pci_free_irq_vectors(pci_priv->pci_dev);
  4073. }
  4074. int cnss_get_user_msi_assignment(struct device *dev, char *user_name,
  4075. int *num_vectors, u32 *user_base_data,
  4076. u32 *base_vector)
  4077. {
  4078. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4079. struct cnss_msi_config *msi_config;
  4080. int idx;
  4081. if (!pci_priv)
  4082. return -ENODEV;
  4083. msi_config = pci_priv->msi_config;
  4084. if (!msi_config) {
  4085. cnss_pr_err("MSI is not supported.\n");
  4086. return -EINVAL;
  4087. }
  4088. for (idx = 0; idx < msi_config->total_users; idx++) {
  4089. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  4090. *num_vectors = msi_config->users[idx].num_vectors;
  4091. *user_base_data = msi_config->users[idx].base_vector
  4092. + pci_priv->msi_ep_base_data;
  4093. *base_vector = msi_config->users[idx].base_vector;
  4094. cnss_pr_dbg("Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  4095. user_name, *num_vectors, *user_base_data,
  4096. *base_vector);
  4097. return 0;
  4098. }
  4099. }
  4100. cnss_pr_err("Failed to find MSI assignment for %s!\n", user_name);
  4101. return -EINVAL;
  4102. }
  4103. EXPORT_SYMBOL(cnss_get_user_msi_assignment);
  4104. int cnss_get_msi_irq(struct device *dev, unsigned int vector)
  4105. {
  4106. struct pci_dev *pci_dev = to_pci_dev(dev);
  4107. int irq_num;
  4108. irq_num = pci_irq_vector(pci_dev, vector);
  4109. cnss_pr_dbg("Get IRQ number %d for vector index %d\n", irq_num, vector);
  4110. return irq_num;
  4111. }
  4112. EXPORT_SYMBOL(cnss_get_msi_irq);
  4113. void cnss_get_msi_address(struct device *dev, u32 *msi_addr_low,
  4114. u32 *msi_addr_high)
  4115. {
  4116. struct pci_dev *pci_dev = to_pci_dev(dev);
  4117. u16 control;
  4118. pci_read_config_word(pci_dev, pci_dev->msi_cap + PCI_MSI_FLAGS,
  4119. &control);
  4120. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  4121. msi_addr_low);
  4122. /* Return MSI high address only when device supports 64-bit MSI */
  4123. if (control & PCI_MSI_FLAGS_64BIT)
  4124. pci_read_config_dword(pci_dev,
  4125. pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  4126. msi_addr_high);
  4127. else
  4128. *msi_addr_high = 0;
  4129. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  4130. *msi_addr_low, *msi_addr_high);
  4131. }
  4132. EXPORT_SYMBOL(cnss_get_msi_address);
  4133. u32 cnss_pci_get_wake_msi(struct cnss_pci_data *pci_priv)
  4134. {
  4135. int ret, num_vectors;
  4136. u32 user_base_data, base_vector;
  4137. if (!pci_priv)
  4138. return -ENODEV;
  4139. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4140. WAKE_MSI_NAME, &num_vectors,
  4141. &user_base_data, &base_vector);
  4142. if (ret) {
  4143. cnss_pr_err("WAKE MSI is not valid\n");
  4144. return 0;
  4145. }
  4146. return user_base_data;
  4147. }
  4148. static int cnss_pci_enable_bus(struct cnss_pci_data *pci_priv)
  4149. {
  4150. int ret = 0;
  4151. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4152. u16 device_id;
  4153. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
  4154. if (device_id != pci_priv->pci_device_id->device) {
  4155. cnss_pr_err("PCI device ID mismatch, config ID: 0x%x, probe ID: 0x%x\n",
  4156. device_id, pci_priv->pci_device_id->device);
  4157. ret = -EIO;
  4158. goto out;
  4159. }
  4160. ret = pci_assign_resource(pci_dev, PCI_BAR_NUM);
  4161. if (ret) {
  4162. pr_err("Failed to assign PCI resource, err = %d\n", ret);
  4163. goto out;
  4164. }
  4165. ret = pci_enable_device(pci_dev);
  4166. if (ret) {
  4167. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  4168. goto out;
  4169. }
  4170. ret = pci_request_region(pci_dev, PCI_BAR_NUM, "cnss");
  4171. if (ret) {
  4172. cnss_pr_err("Failed to request PCI region, err = %d\n", ret);
  4173. goto disable_device;
  4174. }
  4175. switch (device_id) {
  4176. case QCA6174_DEVICE_ID:
  4177. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4178. break;
  4179. case QCA6390_DEVICE_ID:
  4180. case QCA6490_DEVICE_ID:
  4181. case KIWI_DEVICE_ID:
  4182. pci_priv->dma_bit_mask = PCI_DMA_MASK_36_BIT;
  4183. break;
  4184. default:
  4185. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4186. break;
  4187. }
  4188. cnss_pr_dbg("Set PCI DMA MASK (0x%llx)\n", pci_priv->dma_bit_mask);
  4189. ret = pci_set_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4190. if (ret) {
  4191. cnss_pr_err("Failed to set PCI DMA mask, err = %d\n", ret);
  4192. goto release_region;
  4193. }
  4194. ret = pci_set_consistent_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4195. if (ret) {
  4196. cnss_pr_err("Failed to set PCI consistent DMA mask, err = %d\n",
  4197. ret);
  4198. goto release_region;
  4199. }
  4200. pci_priv->bar = pci_iomap(pci_dev, PCI_BAR_NUM, 0);
  4201. if (!pci_priv->bar) {
  4202. cnss_pr_err("Failed to do PCI IO map!\n");
  4203. ret = -EIO;
  4204. goto release_region;
  4205. }
  4206. /* Save default config space without BME enabled */
  4207. pci_save_state(pci_dev);
  4208. pci_priv->default_state = pci_store_saved_state(pci_dev);
  4209. pci_set_master(pci_dev);
  4210. return 0;
  4211. release_region:
  4212. pci_release_region(pci_dev, PCI_BAR_NUM);
  4213. disable_device:
  4214. pci_disable_device(pci_dev);
  4215. out:
  4216. return ret;
  4217. }
  4218. static void cnss_pci_disable_bus(struct cnss_pci_data *pci_priv)
  4219. {
  4220. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4221. pci_clear_master(pci_dev);
  4222. pci_load_and_free_saved_state(pci_dev, &pci_priv->saved_state);
  4223. pci_load_and_free_saved_state(pci_dev, &pci_priv->default_state);
  4224. if (pci_priv->bar) {
  4225. pci_iounmap(pci_dev, pci_priv->bar);
  4226. pci_priv->bar = NULL;
  4227. }
  4228. pci_release_region(pci_dev, PCI_BAR_NUM);
  4229. if (pci_is_enabled(pci_dev))
  4230. pci_disable_device(pci_dev);
  4231. }
  4232. static void cnss_pci_dump_qdss_reg(struct cnss_pci_data *pci_priv)
  4233. {
  4234. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4235. int i, array_size = ARRAY_SIZE(qdss_csr) - 1;
  4236. gfp_t gfp = GFP_KERNEL;
  4237. u32 reg_offset;
  4238. if (in_interrupt() || irqs_disabled())
  4239. gfp = GFP_ATOMIC;
  4240. if (!plat_priv->qdss_reg) {
  4241. plat_priv->qdss_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  4242. sizeof(*plat_priv->qdss_reg)
  4243. * array_size, gfp);
  4244. if (!plat_priv->qdss_reg)
  4245. return;
  4246. }
  4247. cnss_pr_dbg("Start to dump qdss registers\n");
  4248. for (i = 0; qdss_csr[i].name; i++) {
  4249. reg_offset = QDSS_APB_DEC_CSR_BASE + qdss_csr[i].offset;
  4250. if (cnss_pci_reg_read(pci_priv, reg_offset,
  4251. &plat_priv->qdss_reg[i]))
  4252. return;
  4253. cnss_pr_dbg("%s[0x%x] = 0x%x\n", qdss_csr[i].name, reg_offset,
  4254. plat_priv->qdss_reg[i]);
  4255. }
  4256. }
  4257. static void cnss_pci_dump_ce_reg(struct cnss_pci_data *pci_priv,
  4258. enum cnss_ce_index ce)
  4259. {
  4260. int i;
  4261. u32 ce_base = ce * CE_REG_INTERVAL;
  4262. u32 reg_offset, src_ring_base, dst_ring_base, cmn_base, val;
  4263. switch (pci_priv->device_id) {
  4264. case QCA6390_DEVICE_ID:
  4265. src_ring_base = QCA6390_CE_SRC_RING_REG_BASE;
  4266. dst_ring_base = QCA6390_CE_DST_RING_REG_BASE;
  4267. cmn_base = QCA6390_CE_COMMON_REG_BASE;
  4268. break;
  4269. case QCA6490_DEVICE_ID:
  4270. src_ring_base = QCA6490_CE_SRC_RING_REG_BASE;
  4271. dst_ring_base = QCA6490_CE_DST_RING_REG_BASE;
  4272. cmn_base = QCA6490_CE_COMMON_REG_BASE;
  4273. break;
  4274. default:
  4275. return;
  4276. }
  4277. switch (ce) {
  4278. case CNSS_CE_09:
  4279. case CNSS_CE_10:
  4280. for (i = 0; ce_src[i].name; i++) {
  4281. reg_offset = src_ring_base + ce_base + ce_src[i].offset;
  4282. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4283. return;
  4284. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4285. ce, ce_src[i].name, reg_offset, val);
  4286. }
  4287. for (i = 0; ce_dst[i].name; i++) {
  4288. reg_offset = dst_ring_base + ce_base + ce_dst[i].offset;
  4289. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4290. return;
  4291. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4292. ce, ce_dst[i].name, reg_offset, val);
  4293. }
  4294. break;
  4295. case CNSS_CE_COMMON:
  4296. for (i = 0; ce_cmn[i].name; i++) {
  4297. reg_offset = cmn_base + ce_cmn[i].offset;
  4298. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4299. return;
  4300. cnss_pr_dbg("CE_COMMON_%s[0x%x] = 0x%x\n",
  4301. ce_cmn[i].name, reg_offset, val);
  4302. }
  4303. break;
  4304. default:
  4305. cnss_pr_err("Unsupported CE[%d] registers dump\n", ce);
  4306. }
  4307. }
  4308. static void cnss_pci_dump_debug_reg(struct cnss_pci_data *pci_priv)
  4309. {
  4310. if (cnss_pci_check_link_status(pci_priv))
  4311. return;
  4312. cnss_pr_dbg("Start to dump debug registers\n");
  4313. cnss_mhi_debug_reg_dump(pci_priv);
  4314. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4315. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_COMMON);
  4316. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_09);
  4317. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_10);
  4318. }
  4319. static int cnss_pci_assert_host_sol(struct cnss_pci_data *pci_priv)
  4320. {
  4321. if (cnss_get_host_sol_value(pci_priv->plat_priv))
  4322. return -EINVAL;
  4323. cnss_pr_dbg("Assert host SOL GPIO to retry RDDM, expecting link down\n");
  4324. cnss_set_host_sol_value(pci_priv->plat_priv, 1);
  4325. return 0;
  4326. }
  4327. int cnss_pci_force_fw_assert_hdlr(struct cnss_pci_data *pci_priv)
  4328. {
  4329. int ret;
  4330. struct cnss_plat_data *plat_priv;
  4331. if (!pci_priv)
  4332. return -ENODEV;
  4333. plat_priv = pci_priv->plat_priv;
  4334. if (!plat_priv)
  4335. return -ENODEV;
  4336. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4337. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state))
  4338. return -EINVAL;
  4339. cnss_auto_resume(&pci_priv->pci_dev->dev);
  4340. if (!cnss_pci_check_link_status(pci_priv))
  4341. cnss_mhi_debug_reg_dump(pci_priv);
  4342. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4343. cnss_pci_dump_misc_reg(pci_priv);
  4344. cnss_pci_dump_shadow_reg(pci_priv);
  4345. /* If link is still down here, directly trigger link down recovery */
  4346. ret = cnss_pci_check_link_status(pci_priv);
  4347. if (ret) {
  4348. cnss_pci_link_down(&pci_priv->pci_dev->dev);
  4349. return 0;
  4350. }
  4351. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_TRIGGER_RDDM);
  4352. if (ret) {
  4353. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4354. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state)) {
  4355. cnss_pr_dbg("MHI is not powered on, ignore RDDM failure\n");
  4356. return 0;
  4357. }
  4358. cnss_fatal_err("Failed to trigger RDDM, err = %d\n", ret);
  4359. if (!cnss_pci_assert_host_sol(pci_priv))
  4360. return 0;
  4361. cnss_pci_dump_debug_reg(pci_priv);
  4362. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4363. CNSS_REASON_DEFAULT);
  4364. return ret;
  4365. }
  4366. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  4367. mod_timer(&pci_priv->dev_rddm_timer,
  4368. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4369. }
  4370. return 0;
  4371. }
  4372. static void cnss_pci_add_dump_seg(struct cnss_pci_data *pci_priv,
  4373. struct cnss_dump_seg *dump_seg,
  4374. enum cnss_fw_dump_type type, int seg_no,
  4375. void *va, dma_addr_t dma, size_t size)
  4376. {
  4377. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4378. struct device *dev = &pci_priv->pci_dev->dev;
  4379. phys_addr_t pa;
  4380. dump_seg->address = dma;
  4381. dump_seg->v_address = va;
  4382. dump_seg->size = size;
  4383. dump_seg->type = type;
  4384. cnss_pr_dbg("Seg: %x, va: %pK, dma: %pa, size: 0x%zx\n",
  4385. seg_no, va, &dma, size);
  4386. if (cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS))
  4387. return;
  4388. cnss_minidump_add_region(plat_priv, type, seg_no, va, pa, size);
  4389. }
  4390. static void cnss_pci_remove_dump_seg(struct cnss_pci_data *pci_priv,
  4391. struct cnss_dump_seg *dump_seg,
  4392. enum cnss_fw_dump_type type, int seg_no,
  4393. void *va, dma_addr_t dma, size_t size)
  4394. {
  4395. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4396. struct device *dev = &pci_priv->pci_dev->dev;
  4397. phys_addr_t pa;
  4398. cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS);
  4399. cnss_minidump_remove_region(plat_priv, type, seg_no, va, pa, size);
  4400. }
  4401. int cnss_pci_call_driver_uevent(struct cnss_pci_data *pci_priv,
  4402. enum cnss_driver_status status, void *data)
  4403. {
  4404. struct cnss_uevent_data uevent_data;
  4405. struct cnss_wlan_driver *driver_ops;
  4406. driver_ops = pci_priv->driver_ops;
  4407. if (!driver_ops || !driver_ops->update_event) {
  4408. cnss_pr_dbg("Hang event driver ops is NULL\n");
  4409. return -EINVAL;
  4410. }
  4411. cnss_pr_dbg("Calling driver uevent: %d\n", status);
  4412. uevent_data.status = status;
  4413. uevent_data.data = data;
  4414. return driver_ops->update_event(pci_priv->pci_dev, &uevent_data);
  4415. }
  4416. static void cnss_pci_send_hang_event(struct cnss_pci_data *pci_priv)
  4417. {
  4418. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4419. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4420. struct cnss_hang_event hang_event;
  4421. void *hang_data_va = NULL;
  4422. u64 offset = 0;
  4423. int i = 0;
  4424. if (!fw_mem || !plat_priv->fw_mem_seg_len)
  4425. return;
  4426. memset(&hang_event, 0, sizeof(hang_event));
  4427. switch (pci_priv->device_id) {
  4428. case QCA6390_DEVICE_ID:
  4429. offset = HST_HANG_DATA_OFFSET;
  4430. break;
  4431. case QCA6490_DEVICE_ID:
  4432. offset = HSP_HANG_DATA_OFFSET;
  4433. break;
  4434. default:
  4435. cnss_pr_err("Skip Hang Event Data as unsupported Device ID received: %d\n",
  4436. pci_priv->device_id);
  4437. return;
  4438. }
  4439. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4440. if (fw_mem[i].type == QMI_WLFW_MEM_TYPE_DDR_V01 &&
  4441. fw_mem[i].va) {
  4442. hang_data_va = fw_mem[i].va + offset;
  4443. hang_event.hang_event_data = kmemdup(hang_data_va,
  4444. HANG_DATA_LENGTH,
  4445. GFP_ATOMIC);
  4446. if (!hang_event.hang_event_data) {
  4447. cnss_pr_dbg("Hang data memory alloc failed\n");
  4448. return;
  4449. }
  4450. hang_event.hang_event_data_len = HANG_DATA_LENGTH;
  4451. break;
  4452. }
  4453. }
  4454. cnss_pci_call_driver_uevent(pci_priv, CNSS_HANG_EVENT, &hang_event);
  4455. kfree(hang_event.hang_event_data);
  4456. hang_event.hang_event_data = NULL;
  4457. }
  4458. void cnss_pci_collect_dump_info(struct cnss_pci_data *pci_priv, bool in_panic)
  4459. {
  4460. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4461. struct cnss_dump_data *dump_data =
  4462. &plat_priv->ramdump_info_v2.dump_data;
  4463. struct cnss_dump_seg *dump_seg =
  4464. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4465. struct image_info *fw_image, *rddm_image;
  4466. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4467. int ret, i, j;
  4468. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  4469. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  4470. cnss_pci_send_hang_event(pci_priv);
  4471. if (test_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state)) {
  4472. cnss_pr_dbg("RAM dump is already collected, skip\n");
  4473. return;
  4474. }
  4475. if (!cnss_is_device_powered_on(plat_priv)) {
  4476. cnss_pr_dbg("Device is already powered off, skip\n");
  4477. return;
  4478. }
  4479. if (!in_panic) {
  4480. mutex_lock(&pci_priv->bus_lock);
  4481. ret = cnss_pci_check_link_status(pci_priv);
  4482. if (ret) {
  4483. if (ret != -EACCES) {
  4484. mutex_unlock(&pci_priv->bus_lock);
  4485. return;
  4486. }
  4487. if (cnss_pci_resume_bus(pci_priv)) {
  4488. mutex_unlock(&pci_priv->bus_lock);
  4489. return;
  4490. }
  4491. }
  4492. mutex_unlock(&pci_priv->bus_lock);
  4493. } else {
  4494. if (cnss_pci_check_link_status(pci_priv))
  4495. return;
  4496. /* Inside panic handler, reduce timeout for RDDM to avoid
  4497. * unnecessary hypervisor watchdog bite.
  4498. */
  4499. pci_priv->mhi_ctrl->timeout_ms /= 2;
  4500. }
  4501. cnss_mhi_debug_reg_dump(pci_priv);
  4502. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4503. cnss_pci_dump_misc_reg(pci_priv);
  4504. cnss_pci_dump_shadow_reg(pci_priv);
  4505. cnss_pci_dump_qdss_reg(pci_priv);
  4506. ret = mhi_download_rddm_image(pci_priv->mhi_ctrl, in_panic);
  4507. if (ret) {
  4508. cnss_fatal_err("Failed to download RDDM image, err = %d\n",
  4509. ret);
  4510. if (!cnss_pci_assert_host_sol(pci_priv))
  4511. return;
  4512. cnss_pci_dump_debug_reg(pci_priv);
  4513. return;
  4514. }
  4515. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4516. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4517. dump_data->nentries = 0;
  4518. cnss_mhi_dump_sfr(pci_priv);
  4519. if (!dump_seg) {
  4520. cnss_pr_warn("FW image dump collection not setup");
  4521. goto skip_dump;
  4522. }
  4523. cnss_pr_dbg("Collect FW image dump segment, nentries %d\n",
  4524. fw_image->entries);
  4525. for (i = 0; i < fw_image->entries; i++) {
  4526. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4527. fw_image->mhi_buf[i].buf,
  4528. fw_image->mhi_buf[i].dma_addr,
  4529. fw_image->mhi_buf[i].len);
  4530. dump_seg++;
  4531. }
  4532. dump_data->nentries += fw_image->entries;
  4533. cnss_pr_dbg("Collect RDDM image dump segment, nentries %d\n",
  4534. rddm_image->entries);
  4535. for (i = 0; i < rddm_image->entries; i++) {
  4536. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4537. rddm_image->mhi_buf[i].buf,
  4538. rddm_image->mhi_buf[i].dma_addr,
  4539. rddm_image->mhi_buf[i].len);
  4540. dump_seg++;
  4541. }
  4542. dump_data->nentries += rddm_image->entries;
  4543. cnss_pr_dbg("Collect remote heap dump segment\n");
  4544. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4545. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4546. cnss_pci_add_dump_seg(pci_priv, dump_seg,
  4547. CNSS_FW_REMOTE_HEAP, j,
  4548. fw_mem[i].va, fw_mem[i].pa,
  4549. fw_mem[i].size);
  4550. dump_seg++;
  4551. dump_data->nentries++;
  4552. j++;
  4553. }
  4554. }
  4555. if (dump_data->nentries > 0)
  4556. plat_priv->ramdump_info_v2.dump_data_valid = true;
  4557. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RDDM_DONE);
  4558. skip_dump:
  4559. complete(&plat_priv->rddm_complete);
  4560. }
  4561. void cnss_pci_clear_dump_info(struct cnss_pci_data *pci_priv)
  4562. {
  4563. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4564. struct cnss_dump_seg *dump_seg =
  4565. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4566. struct image_info *fw_image, *rddm_image;
  4567. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4568. int i, j;
  4569. if (!dump_seg)
  4570. return;
  4571. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4572. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4573. for (i = 0; i < fw_image->entries; i++) {
  4574. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4575. fw_image->mhi_buf[i].buf,
  4576. fw_image->mhi_buf[i].dma_addr,
  4577. fw_image->mhi_buf[i].len);
  4578. dump_seg++;
  4579. }
  4580. for (i = 0; i < rddm_image->entries; i++) {
  4581. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4582. rddm_image->mhi_buf[i].buf,
  4583. rddm_image->mhi_buf[i].dma_addr,
  4584. rddm_image->mhi_buf[i].len);
  4585. dump_seg++;
  4586. }
  4587. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4588. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4589. cnss_pci_remove_dump_seg(pci_priv, dump_seg,
  4590. CNSS_FW_REMOTE_HEAP, j,
  4591. fw_mem[i].va, fw_mem[i].pa,
  4592. fw_mem[i].size);
  4593. dump_seg++;
  4594. j++;
  4595. }
  4596. }
  4597. plat_priv->ramdump_info_v2.dump_data.nentries = 0;
  4598. plat_priv->ramdump_info_v2.dump_data_valid = false;
  4599. }
  4600. void cnss_pci_device_crashed(struct cnss_pci_data *pci_priv)
  4601. {
  4602. if (!pci_priv)
  4603. return;
  4604. cnss_device_crashed(&pci_priv->pci_dev->dev);
  4605. }
  4606. static int cnss_mhi_pm_runtime_get(struct mhi_controller *mhi_ctrl)
  4607. {
  4608. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4609. return cnss_pci_pm_runtime_get(pci_priv, RTPM_ID_MHI);
  4610. }
  4611. static void cnss_mhi_pm_runtime_put_noidle(struct mhi_controller *mhi_ctrl)
  4612. {
  4613. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4614. cnss_pci_pm_runtime_put_noidle(pci_priv, RTPM_ID_MHI);
  4615. }
  4616. void cnss_pci_add_fw_prefix_name(struct cnss_pci_data *pci_priv,
  4617. char *prefix_name, char *name)
  4618. {
  4619. struct cnss_plat_data *plat_priv;
  4620. if (!pci_priv)
  4621. return;
  4622. plat_priv = pci_priv->plat_priv;
  4623. if (!plat_priv->use_fw_path_with_prefix) {
  4624. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4625. return;
  4626. }
  4627. switch (pci_priv->device_id) {
  4628. case QCA6390_DEVICE_ID:
  4629. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4630. QCA6390_PATH_PREFIX "%s", name);
  4631. break;
  4632. case QCA6490_DEVICE_ID:
  4633. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4634. QCA6490_PATH_PREFIX "%s", name);
  4635. break;
  4636. case KIWI_DEVICE_ID:
  4637. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4638. KIWI_PATH_PREFIX "%s", name);
  4639. break;
  4640. default:
  4641. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4642. break;
  4643. }
  4644. cnss_pr_dbg("FW name added with prefix: %s\n", prefix_name);
  4645. }
  4646. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv)
  4647. {
  4648. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4649. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  4650. plat_priv->device_version.family_number = mhi_ctrl->family_number;
  4651. plat_priv->device_version.device_number = mhi_ctrl->device_number;
  4652. plat_priv->device_version.major_version = mhi_ctrl->major_version;
  4653. plat_priv->device_version.minor_version = mhi_ctrl->minor_version;
  4654. cnss_pr_dbg("Get device version info, family number: 0x%x, device number: 0x%x, major version: 0x%x, minor version: 0x%x\n",
  4655. plat_priv->device_version.family_number,
  4656. plat_priv->device_version.device_number,
  4657. plat_priv->device_version.major_version,
  4658. plat_priv->device_version.minor_version);
  4659. /* Only keep lower 4 bits as real device major version */
  4660. plat_priv->device_version.major_version &= DEVICE_MAJOR_VERSION_MASK;
  4661. switch (pci_priv->device_id) {
  4662. case QCA6390_DEVICE_ID:
  4663. if (plat_priv->device_version.major_version < FW_V2_NUMBER) {
  4664. cnss_pr_dbg("Device ID:version (0x%lx:%d) is not supported\n",
  4665. pci_priv->device_id,
  4666. plat_priv->device_version.major_version);
  4667. return -EINVAL;
  4668. }
  4669. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4670. FW_V2_FILE_NAME);
  4671. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4672. FW_V2_FILE_NAME);
  4673. break;
  4674. case QCA6490_DEVICE_ID:
  4675. case KIWI_DEVICE_ID:
  4676. switch (plat_priv->device_version.major_version) {
  4677. case FW_V2_NUMBER:
  4678. cnss_pci_add_fw_prefix_name(pci_priv,
  4679. plat_priv->firmware_name,
  4680. FW_V2_FILE_NAME);
  4681. snprintf(plat_priv->fw_fallback_name,
  4682. MAX_FIRMWARE_NAME_LEN,
  4683. FW_V2_FILE_NAME);
  4684. break;
  4685. default:
  4686. cnss_pci_add_fw_prefix_name(pci_priv,
  4687. plat_priv->firmware_name,
  4688. DEFAULT_FW_FILE_NAME);
  4689. snprintf(plat_priv->fw_fallback_name,
  4690. MAX_FIRMWARE_NAME_LEN,
  4691. DEFAULT_FW_FILE_NAME);
  4692. break;
  4693. }
  4694. break;
  4695. default:
  4696. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4697. DEFAULT_FW_FILE_NAME);
  4698. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4699. DEFAULT_FW_FILE_NAME);
  4700. break;
  4701. }
  4702. cnss_pr_dbg("FW name is %s, FW fallback name is %s\n",
  4703. plat_priv->firmware_name, plat_priv->fw_fallback_name);
  4704. return 0;
  4705. }
  4706. static char *cnss_mhi_notify_status_to_str(enum mhi_callback status)
  4707. {
  4708. switch (status) {
  4709. case MHI_CB_IDLE:
  4710. return "IDLE";
  4711. case MHI_CB_EE_RDDM:
  4712. return "RDDM";
  4713. case MHI_CB_SYS_ERROR:
  4714. return "SYS_ERROR";
  4715. case MHI_CB_FATAL_ERROR:
  4716. return "FATAL_ERROR";
  4717. case MHI_CB_EE_MISSION_MODE:
  4718. return "MISSION_MODE";
  4719. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4720. case MHI_CB_FALLBACK_IMG:
  4721. return "FW_FALLBACK";
  4722. #endif
  4723. default:
  4724. return "UNKNOWN";
  4725. }
  4726. };
  4727. static void cnss_dev_rddm_timeout_hdlr(struct timer_list *t)
  4728. {
  4729. struct cnss_pci_data *pci_priv =
  4730. from_timer(pci_priv, t, dev_rddm_timer);
  4731. enum mhi_ee_type mhi_ee;
  4732. if (!pci_priv)
  4733. return;
  4734. cnss_fatal_err("Timeout waiting for RDDM notification\n");
  4735. if (!cnss_pci_assert_host_sol(pci_priv))
  4736. return;
  4737. mhi_ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  4738. if (mhi_ee == MHI_EE_PBL)
  4739. cnss_pr_err("Unable to collect ramdumps due to abrupt reset\n");
  4740. if (mhi_ee == MHI_EE_RDDM) {
  4741. cnss_pr_info("Device MHI EE is RDDM, try to collect dump\n");
  4742. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4743. CNSS_REASON_RDDM);
  4744. } else {
  4745. cnss_mhi_debug_reg_dump(pci_priv);
  4746. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4747. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4748. CNSS_REASON_TIMEOUT);
  4749. }
  4750. }
  4751. static void cnss_boot_debug_timeout_hdlr(struct timer_list *t)
  4752. {
  4753. struct cnss_pci_data *pci_priv =
  4754. from_timer(pci_priv, t, boot_debug_timer);
  4755. if (!pci_priv)
  4756. return;
  4757. if (cnss_pci_check_link_status(pci_priv))
  4758. return;
  4759. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  4760. return;
  4761. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  4762. return;
  4763. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE))
  4764. return;
  4765. cnss_pr_dbg("Dump MHI/PBL/SBL debug data every %ds during MHI power on\n",
  4766. BOOT_DEBUG_TIMEOUT_MS / 1000);
  4767. cnss_mhi_debug_reg_dump(pci_priv);
  4768. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4769. cnss_pci_dump_bl_sram_mem(pci_priv);
  4770. mod_timer(&pci_priv->boot_debug_timer,
  4771. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  4772. }
  4773. static int cnss_pci_handle_mhi_sys_err(struct cnss_pci_data *pci_priv)
  4774. {
  4775. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4776. cnss_ignore_qmi_failure(true);
  4777. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4778. del_timer(&plat_priv->fw_boot_timer);
  4779. mod_timer(&pci_priv->dev_rddm_timer,
  4780. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4781. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4782. return 0;
  4783. }
  4784. int cnss_pci_handle_dev_sol_irq(struct cnss_pci_data *pci_priv)
  4785. {
  4786. return cnss_pci_handle_mhi_sys_err(pci_priv);
  4787. }
  4788. static void cnss_mhi_notify_status(struct mhi_controller *mhi_ctrl,
  4789. enum mhi_callback reason)
  4790. {
  4791. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4792. struct cnss_plat_data *plat_priv;
  4793. enum cnss_recovery_reason cnss_reason;
  4794. if (!pci_priv) {
  4795. cnss_pr_err("pci_priv is NULL");
  4796. return;
  4797. }
  4798. plat_priv = pci_priv->plat_priv;
  4799. if (reason != MHI_CB_IDLE)
  4800. cnss_pr_dbg("MHI status cb is called with reason %s(%d)\n",
  4801. cnss_mhi_notify_status_to_str(reason), reason);
  4802. switch (reason) {
  4803. case MHI_CB_IDLE:
  4804. case MHI_CB_EE_MISSION_MODE:
  4805. return;
  4806. case MHI_CB_FATAL_ERROR:
  4807. cnss_ignore_qmi_failure(true);
  4808. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4809. del_timer(&plat_priv->fw_boot_timer);
  4810. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4811. cnss_reason = CNSS_REASON_DEFAULT;
  4812. break;
  4813. case MHI_CB_SYS_ERROR:
  4814. cnss_pci_handle_mhi_sys_err(pci_priv);
  4815. return;
  4816. case MHI_CB_EE_RDDM:
  4817. cnss_ignore_qmi_failure(true);
  4818. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4819. del_timer(&plat_priv->fw_boot_timer);
  4820. del_timer(&pci_priv->dev_rddm_timer);
  4821. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4822. cnss_reason = CNSS_REASON_RDDM;
  4823. break;
  4824. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4825. case MHI_CB_FALLBACK_IMG:
  4826. plat_priv->use_fw_path_with_prefix = false;
  4827. cnss_pci_update_fw_name(pci_priv);
  4828. return;
  4829. #endif
  4830. default:
  4831. cnss_pr_err("Unsupported MHI status cb reason: %d\n", reason);
  4832. return;
  4833. }
  4834. cnss_schedule_recovery(&pci_priv->pci_dev->dev, cnss_reason);
  4835. }
  4836. static int cnss_pci_get_mhi_msi(struct cnss_pci_data *pci_priv)
  4837. {
  4838. int ret, num_vectors, i;
  4839. u32 user_base_data, base_vector;
  4840. int *irq;
  4841. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4842. MHI_MSI_NAME, &num_vectors,
  4843. &user_base_data, &base_vector);
  4844. if (ret)
  4845. return ret;
  4846. cnss_pr_dbg("Number of assigned MSI for MHI is %d, base vector is %d\n",
  4847. num_vectors, base_vector);
  4848. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  4849. if (!irq)
  4850. return -ENOMEM;
  4851. for (i = 0; i < num_vectors; i++)
  4852. irq[i] = cnss_get_msi_irq(&pci_priv->pci_dev->dev,
  4853. base_vector + i);
  4854. pci_priv->mhi_ctrl->irq = irq;
  4855. pci_priv->mhi_ctrl->nr_irqs = num_vectors;
  4856. return 0;
  4857. }
  4858. static int cnss_mhi_bw_scale(struct mhi_controller *mhi_ctrl,
  4859. struct mhi_link_info *link_info)
  4860. {
  4861. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4862. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4863. int ret = 0;
  4864. cnss_pr_dbg("Setting link speed:0x%x, width:0x%x\n",
  4865. link_info->target_link_speed,
  4866. link_info->target_link_width);
  4867. /* It has to set target link speed here before setting link bandwidth
  4868. * when device requests link speed change. This can avoid setting link
  4869. * bandwidth getting rejected if requested link speed is higher than
  4870. * current one.
  4871. */
  4872. ret = cnss_pci_set_max_link_speed(pci_priv, plat_priv->rc_num,
  4873. link_info->target_link_speed);
  4874. if (ret)
  4875. cnss_pr_err("Failed to set target link speed to 0x%x, err = %d\n",
  4876. link_info->target_link_speed, ret);
  4877. ret = cnss_pci_set_link_bandwidth(pci_priv,
  4878. link_info->target_link_speed,
  4879. link_info->target_link_width);
  4880. if (ret) {
  4881. cnss_pr_err("Failed to set link bandwidth, err = %d\n", ret);
  4882. return ret;
  4883. }
  4884. pci_priv->def_link_speed = link_info->target_link_speed;
  4885. pci_priv->def_link_width = link_info->target_link_width;
  4886. return 0;
  4887. }
  4888. static int cnss_mhi_read_reg(struct mhi_controller *mhi_ctrl,
  4889. void __iomem *addr, u32 *out)
  4890. {
  4891. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4892. u32 tmp = readl_relaxed(addr);
  4893. /* Unexpected value, query the link status */
  4894. if (PCI_INVALID_READ(tmp) &&
  4895. cnss_pci_check_link_status(pci_priv))
  4896. return -EIO;
  4897. *out = tmp;
  4898. return 0;
  4899. }
  4900. static void cnss_mhi_write_reg(struct mhi_controller *mhi_ctrl,
  4901. void __iomem *addr, u32 val)
  4902. {
  4903. writel_relaxed(val, addr);
  4904. }
  4905. static int cnss_pci_register_mhi(struct cnss_pci_data *pci_priv)
  4906. {
  4907. int ret = 0;
  4908. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4909. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4910. struct mhi_controller *mhi_ctrl;
  4911. phys_addr_t bar_start;
  4912. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4913. return 0;
  4914. mhi_ctrl = mhi_alloc_controller();
  4915. if (!mhi_ctrl) {
  4916. cnss_pr_err("Invalid MHI controller context\n");
  4917. return -EINVAL;
  4918. }
  4919. pci_priv->mhi_ctrl = mhi_ctrl;
  4920. mhi_ctrl->cntrl_dev = &pci_dev->dev;
  4921. mhi_ctrl->fw_image = plat_priv->firmware_name;
  4922. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4923. mhi_ctrl->fallback_fw_image = plat_priv->fw_fallback_name;
  4924. #endif
  4925. mhi_ctrl->regs = pci_priv->bar;
  4926. mhi_ctrl->reg_len = pci_resource_len(pci_priv->pci_dev, PCI_BAR_NUM);
  4927. bar_start = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4928. cnss_pr_dbg("BAR starts at %pa, length is %x\n",
  4929. &bar_start, mhi_ctrl->reg_len);
  4930. ret = cnss_pci_get_mhi_msi(pci_priv);
  4931. if (ret) {
  4932. cnss_pr_err("Failed to get MSI for MHI, err = %d\n", ret);
  4933. goto free_mhi_ctrl;
  4934. }
  4935. if (pci_priv->smmu_s1_enable) {
  4936. mhi_ctrl->iova_start = pci_priv->smmu_iova_start;
  4937. mhi_ctrl->iova_stop = pci_priv->smmu_iova_start +
  4938. pci_priv->smmu_iova_len;
  4939. } else {
  4940. mhi_ctrl->iova_start = 0;
  4941. mhi_ctrl->iova_stop = pci_priv->dma_bit_mask;
  4942. }
  4943. mhi_ctrl->status_cb = cnss_mhi_notify_status;
  4944. mhi_ctrl->runtime_get = cnss_mhi_pm_runtime_get;
  4945. mhi_ctrl->runtime_put = cnss_mhi_pm_runtime_put_noidle;
  4946. mhi_ctrl->read_reg = cnss_mhi_read_reg;
  4947. mhi_ctrl->write_reg = cnss_mhi_write_reg;
  4948. mhi_ctrl->rddm_size = pci_priv->plat_priv->ramdump_info_v2.ramdump_size;
  4949. if (!mhi_ctrl->rddm_size)
  4950. mhi_ctrl->rddm_size = RAMDUMP_SIZE_DEFAULT;
  4951. mhi_ctrl->sbl_size = SZ_512K;
  4952. mhi_ctrl->seg_len = SZ_512K;
  4953. mhi_ctrl->fbc_download = true;
  4954. ret = mhi_register_controller(mhi_ctrl, &cnss_mhi_config);
  4955. if (ret) {
  4956. cnss_pr_err("Failed to register to MHI bus, err = %d\n", ret);
  4957. goto free_mhi_irq;
  4958. }
  4959. /* MHI satellite driver only needs to connect when DRV is supported */
  4960. if (cnss_pci_is_drv_supported(pci_priv))
  4961. cnss_mhi_controller_set_base(pci_priv, bar_start);
  4962. /* BW scale CB needs to be set after registering MHI per requirement */
  4963. cnss_mhi_controller_set_bw_scale_cb(pci_priv, cnss_mhi_bw_scale);
  4964. ret = cnss_pci_update_fw_name(pci_priv);
  4965. if (ret)
  4966. goto unreg_mhi;
  4967. return 0;
  4968. unreg_mhi:
  4969. mhi_unregister_controller(mhi_ctrl);
  4970. free_mhi_irq:
  4971. kfree(mhi_ctrl->irq);
  4972. free_mhi_ctrl:
  4973. mhi_free_controller(mhi_ctrl);
  4974. return ret;
  4975. }
  4976. static void cnss_pci_unregister_mhi(struct cnss_pci_data *pci_priv)
  4977. {
  4978. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  4979. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4980. return;
  4981. mhi_unregister_controller(mhi_ctrl);
  4982. kfree(mhi_ctrl->irq);
  4983. mhi_free_controller(mhi_ctrl);
  4984. }
  4985. static void cnss_pci_config_regs(struct cnss_pci_data *pci_priv)
  4986. {
  4987. switch (pci_priv->device_id) {
  4988. case QCA6390_DEVICE_ID:
  4989. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6390;
  4990. pci_priv->wcss_reg = wcss_reg_access_seq;
  4991. pci_priv->pcie_reg = pcie_reg_access_seq;
  4992. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  4993. pci_priv->syspm_reg = syspm_reg_access_seq;
  4994. /* Configure WDOG register with specific value so that we can
  4995. * know if HW is in the process of WDOG reset recovery or not
  4996. * when reading the registers.
  4997. */
  4998. cnss_pci_reg_write
  4999. (pci_priv,
  5000. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG,
  5001. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG_VAL);
  5002. break;
  5003. case QCA6490_DEVICE_ID:
  5004. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6490;
  5005. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5006. break;
  5007. default:
  5008. return;
  5009. }
  5010. }
  5011. #if !IS_ENABLED(CONFIG_ARCH_QCOM)
  5012. static irqreturn_t cnss_pci_wake_handler(int irq, void *data)
  5013. {
  5014. struct cnss_pci_data *pci_priv = data;
  5015. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5016. enum rpm_status status;
  5017. struct device *dev;
  5018. pci_priv->wake_counter++;
  5019. cnss_pr_dbg("WLAN PCI wake IRQ (%u) is asserted #%u\n",
  5020. pci_priv->wake_irq, pci_priv->wake_counter);
  5021. /* Make sure abort current suspend */
  5022. cnss_pm_stay_awake(plat_priv);
  5023. cnss_pm_relax(plat_priv);
  5024. /* Above two pm* API calls will abort system suspend only when
  5025. * plat_dev->dev->ws is initiated by device_init_wakeup() API, and
  5026. * calling pm_system_wakeup() is just to guarantee system suspend
  5027. * can be aborted if it is not initiated in any case.
  5028. */
  5029. pm_system_wakeup();
  5030. dev = &pci_priv->pci_dev->dev;
  5031. status = dev->power.runtime_status;
  5032. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  5033. cnss_pci_get_auto_suspended(pci_priv)) ||
  5034. (status == RPM_SUSPENDING || status == RPM_SUSPENDED)) {
  5035. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  5036. cnss_pci_pm_request_resume(pci_priv);
  5037. }
  5038. return IRQ_HANDLED;
  5039. }
  5040. /**
  5041. * cnss_pci_wake_gpio_init() - Setup PCI wake GPIO for WLAN
  5042. * @pci_priv: driver PCI bus context pointer
  5043. *
  5044. * This function initializes WLAN PCI wake GPIO and corresponding
  5045. * interrupt. It should be used in non-MSM platforms whose PCIe
  5046. * root complex driver doesn't handle the GPIO.
  5047. *
  5048. * Return: 0 for success or skip, negative value for error
  5049. */
  5050. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  5051. {
  5052. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5053. struct device *dev = &plat_priv->plat_dev->dev;
  5054. int ret = 0;
  5055. pci_priv->wake_gpio = of_get_named_gpio(dev->of_node,
  5056. "wlan-pci-wake-gpio", 0);
  5057. if (pci_priv->wake_gpio < 0)
  5058. goto out;
  5059. cnss_pr_dbg("Get PCI wake GPIO (%d) from device node\n",
  5060. pci_priv->wake_gpio);
  5061. ret = gpio_request(pci_priv->wake_gpio, "wlan_pci_wake_gpio");
  5062. if (ret) {
  5063. cnss_pr_err("Failed to request PCI wake GPIO, err = %d\n",
  5064. ret);
  5065. goto out;
  5066. }
  5067. gpio_direction_input(pci_priv->wake_gpio);
  5068. pci_priv->wake_irq = gpio_to_irq(pci_priv->wake_gpio);
  5069. ret = request_irq(pci_priv->wake_irq, cnss_pci_wake_handler,
  5070. IRQF_TRIGGER_FALLING, "wlan_pci_wake_irq", pci_priv);
  5071. if (ret) {
  5072. cnss_pr_err("Failed to request PCI wake IRQ, err = %d\n", ret);
  5073. goto free_gpio;
  5074. }
  5075. ret = enable_irq_wake(pci_priv->wake_irq);
  5076. if (ret) {
  5077. cnss_pr_err("Failed to enable PCI wake IRQ, err = %d\n", ret);
  5078. goto free_irq;
  5079. }
  5080. return 0;
  5081. free_irq:
  5082. free_irq(pci_priv->wake_irq, pci_priv);
  5083. free_gpio:
  5084. gpio_free(pci_priv->wake_gpio);
  5085. out:
  5086. return ret;
  5087. }
  5088. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  5089. {
  5090. if (pci_priv->wake_gpio < 0)
  5091. return;
  5092. disable_irq_wake(pci_priv->wake_irq);
  5093. free_irq(pci_priv->wake_irq, pci_priv);
  5094. gpio_free(pci_priv->wake_gpio);
  5095. }
  5096. #else
  5097. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  5098. {
  5099. return 0;
  5100. }
  5101. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  5102. {
  5103. }
  5104. #endif
  5105. #if IS_ENABLED(CONFIG_ARCH_QCOM)
  5106. /**
  5107. * cnss_pci_of_reserved_mem_device_init() - Assign reserved memory region
  5108. * to given PCI device
  5109. * @pci_priv: driver PCI bus context pointer
  5110. *
  5111. * This function shall call corresponding of_reserved_mem_device* API to
  5112. * assign reserved memory region to PCI device based on where the memory is
  5113. * defined and attached to (platform device of_node or PCI device of_node)
  5114. * in device tree.
  5115. *
  5116. * Return: 0 for success, negative value for error
  5117. */
  5118. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  5119. {
  5120. struct device *dev_pci = &pci_priv->pci_dev->dev;
  5121. int ret;
  5122. /* Use of_reserved_mem_device_init_by_idx() if reserved memory is
  5123. * attached to platform device of_node.
  5124. */
  5125. ret = of_reserved_mem_device_init(dev_pci);
  5126. if (ret)
  5127. cnss_pr_err("Failed to init reserved mem device, err = %d\n",
  5128. ret);
  5129. if (dev_pci->cma_area)
  5130. cnss_pr_dbg("CMA area is %s\n",
  5131. cma_get_name(dev_pci->cma_area));
  5132. return ret;
  5133. }
  5134. #else
  5135. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  5136. {
  5137. return 0;
  5138. }
  5139. #endif
  5140. /* Setting to use this cnss_pm_domain ops will let PM framework override the
  5141. * ops from dev->bus->pm which is pci_dev_pm_ops from pci-driver.c. This ops
  5142. * has to take care everything device driver needed which is currently done
  5143. * from pci_dev_pm_ops.
  5144. */
  5145. static struct dev_pm_domain cnss_pm_domain = {
  5146. .ops = {
  5147. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5148. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5149. cnss_pci_resume_noirq)
  5150. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend,
  5151. cnss_pci_runtime_resume,
  5152. cnss_pci_runtime_idle)
  5153. }
  5154. };
  5155. static int cnss_pci_probe(struct pci_dev *pci_dev,
  5156. const struct pci_device_id *id)
  5157. {
  5158. int ret = 0;
  5159. struct cnss_pci_data *pci_priv;
  5160. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  5161. struct device *dev = &pci_dev->dev;
  5162. cnss_pr_dbg("PCI is probing, vendor ID: 0x%x, device ID: 0x%x\n",
  5163. id->vendor, pci_dev->device);
  5164. pci_priv = devm_kzalloc(dev, sizeof(*pci_priv), GFP_KERNEL);
  5165. if (!pci_priv) {
  5166. ret = -ENOMEM;
  5167. goto out;
  5168. }
  5169. pci_priv->pci_link_state = PCI_LINK_UP;
  5170. pci_priv->plat_priv = plat_priv;
  5171. pci_priv->pci_dev = pci_dev;
  5172. pci_priv->pci_device_id = id;
  5173. pci_priv->device_id = pci_dev->device;
  5174. cnss_set_pci_priv(pci_dev, pci_priv);
  5175. plat_priv->device_id = pci_dev->device;
  5176. plat_priv->bus_priv = pci_priv;
  5177. mutex_init(&pci_priv->bus_lock);
  5178. if (plat_priv->use_pm_domain)
  5179. dev->pm_domain = &cnss_pm_domain;
  5180. cnss_pci_of_reserved_mem_device_init(pci_priv);
  5181. ret = cnss_register_subsys(plat_priv);
  5182. if (ret)
  5183. goto reset_ctx;
  5184. ret = cnss_register_ramdump(plat_priv);
  5185. if (ret)
  5186. goto unregister_subsys;
  5187. ret = cnss_pci_init_smmu(pci_priv);
  5188. if (ret)
  5189. goto unregister_ramdump;
  5190. ret = cnss_reg_pci_event(pci_priv);
  5191. if (ret) {
  5192. cnss_pr_err("Failed to register PCI event, err = %d\n", ret);
  5193. goto deinit_smmu;
  5194. }
  5195. ret = cnss_pci_enable_bus(pci_priv);
  5196. if (ret)
  5197. goto dereg_pci_event;
  5198. ret = cnss_pci_enable_msi(pci_priv);
  5199. if (ret)
  5200. goto disable_bus;
  5201. ret = cnss_pci_register_mhi(pci_priv);
  5202. if (ret)
  5203. goto disable_msi;
  5204. switch (pci_dev->device) {
  5205. case QCA6174_DEVICE_ID:
  5206. pci_read_config_word(pci_dev, QCA6174_REV_ID_OFFSET,
  5207. &pci_priv->revision_id);
  5208. break;
  5209. case QCA6290_DEVICE_ID:
  5210. case QCA6390_DEVICE_ID:
  5211. case QCA6490_DEVICE_ID:
  5212. case KIWI_DEVICE_ID:
  5213. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  5214. timer_setup(&pci_priv->dev_rddm_timer,
  5215. cnss_dev_rddm_timeout_hdlr, 0);
  5216. timer_setup(&pci_priv->boot_debug_timer,
  5217. cnss_boot_debug_timeout_hdlr, 0);
  5218. INIT_DELAYED_WORK(&pci_priv->time_sync_work,
  5219. cnss_pci_time_sync_work_hdlr);
  5220. cnss_pci_get_link_status(pci_priv);
  5221. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, false);
  5222. cnss_pci_wake_gpio_init(pci_priv);
  5223. break;
  5224. default:
  5225. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  5226. pci_dev->device);
  5227. ret = -ENODEV;
  5228. goto unreg_mhi;
  5229. }
  5230. cnss_pci_config_regs(pci_priv);
  5231. if (EMULATION_HW)
  5232. goto out;
  5233. ret = cnss_suspend_pci_link(pci_priv);
  5234. if (ret)
  5235. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  5236. cnss_power_off_device(plat_priv);
  5237. set_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5238. return 0;
  5239. unreg_mhi:
  5240. cnss_pci_unregister_mhi(pci_priv);
  5241. disable_msi:
  5242. cnss_pci_disable_msi(pci_priv);
  5243. disable_bus:
  5244. cnss_pci_disable_bus(pci_priv);
  5245. dereg_pci_event:
  5246. cnss_dereg_pci_event(pci_priv);
  5247. deinit_smmu:
  5248. cnss_pci_deinit_smmu(pci_priv);
  5249. unregister_ramdump:
  5250. cnss_unregister_ramdump(plat_priv);
  5251. unregister_subsys:
  5252. cnss_unregister_subsys(plat_priv);
  5253. reset_ctx:
  5254. plat_priv->bus_priv = NULL;
  5255. out:
  5256. return ret;
  5257. }
  5258. static void cnss_pci_remove(struct pci_dev *pci_dev)
  5259. {
  5260. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5261. struct cnss_plat_data *plat_priv =
  5262. cnss_bus_dev_to_plat_priv(&pci_dev->dev);
  5263. clear_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5264. cnss_pci_free_m3_mem(pci_priv);
  5265. cnss_pci_free_fw_mem(pci_priv);
  5266. cnss_pci_free_qdss_mem(pci_priv);
  5267. switch (pci_dev->device) {
  5268. case QCA6290_DEVICE_ID:
  5269. case QCA6390_DEVICE_ID:
  5270. case QCA6490_DEVICE_ID:
  5271. case KIWI_DEVICE_ID:
  5272. cnss_pci_wake_gpio_deinit(pci_priv);
  5273. del_timer(&pci_priv->boot_debug_timer);
  5274. del_timer(&pci_priv->dev_rddm_timer);
  5275. break;
  5276. default:
  5277. break;
  5278. }
  5279. cnss_pci_unregister_mhi(pci_priv);
  5280. cnss_pci_disable_msi(pci_priv);
  5281. cnss_pci_disable_bus(pci_priv);
  5282. cnss_dereg_pci_event(pci_priv);
  5283. cnss_pci_deinit_smmu(pci_priv);
  5284. if (plat_priv) {
  5285. cnss_unregister_ramdump(plat_priv);
  5286. cnss_unregister_subsys(plat_priv);
  5287. plat_priv->bus_priv = NULL;
  5288. } else {
  5289. cnss_pr_err("Plat_priv is null, Unable to unregister ramdump,subsys\n");
  5290. }
  5291. }
  5292. static const struct pci_device_id cnss_pci_id_table[] = {
  5293. { QCA6174_VENDOR_ID, QCA6174_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5294. { QCA6290_VENDOR_ID, QCA6290_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5295. { QCA6390_VENDOR_ID, QCA6390_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5296. { QCA6490_VENDOR_ID, QCA6490_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5297. { KIWI_VENDOR_ID, KIWI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5298. { 0 }
  5299. };
  5300. MODULE_DEVICE_TABLE(pci, cnss_pci_id_table);
  5301. static const struct dev_pm_ops cnss_pm_ops = {
  5302. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5303. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5304. cnss_pci_resume_noirq)
  5305. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend, cnss_pci_runtime_resume,
  5306. cnss_pci_runtime_idle)
  5307. };
  5308. struct pci_driver cnss_pci_driver = {
  5309. .name = "cnss_pci",
  5310. .id_table = cnss_pci_id_table,
  5311. .probe = cnss_pci_probe,
  5312. .remove = cnss_pci_remove,
  5313. .driver = {
  5314. .pm = &cnss_pm_ops,
  5315. },
  5316. };
  5317. static int cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  5318. {
  5319. int ret, retry = 0;
  5320. /* Always set initial target PCIe link speed to Gen2 for QCA6490 device
  5321. * since there may be link issues if it boots up with Gen3 link speed.
  5322. * Device is able to change it later at any time. It will be rejected
  5323. * if requested speed is higher than the one specified in PCIe DT.
  5324. */
  5325. if (plat_priv->device_id == QCA6490_DEVICE_ID) {
  5326. ret = cnss_pci_set_max_link_speed(plat_priv->bus_priv, rc_num,
  5327. PCI_EXP_LNKSTA_CLS_5_0GB);
  5328. if (ret && ret != -EPROBE_DEFER)
  5329. cnss_pr_err("Failed to set max PCIe RC%x link speed to Gen2, err = %d\n",
  5330. rc_num, ret);
  5331. }
  5332. cnss_pr_dbg("Trying to enumerate with PCIe RC%x\n", rc_num);
  5333. retry:
  5334. ret = _cnss_pci_enumerate(plat_priv, rc_num);
  5335. if (ret) {
  5336. if (ret == -EPROBE_DEFER) {
  5337. cnss_pr_dbg("PCIe RC driver is not ready, defer probe\n");
  5338. goto out;
  5339. }
  5340. cnss_pr_err("Failed to enable PCIe RC%x, err = %d\n",
  5341. rc_num, ret);
  5342. if (retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  5343. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  5344. goto retry;
  5345. } else {
  5346. goto out;
  5347. }
  5348. }
  5349. plat_priv->rc_num = rc_num;
  5350. out:
  5351. return ret;
  5352. }
  5353. int cnss_pci_init(struct cnss_plat_data *plat_priv)
  5354. {
  5355. struct device *dev = &plat_priv->plat_dev->dev;
  5356. const __be32 *prop;
  5357. int ret = 0, prop_len = 0, rc_count, i;
  5358. prop = of_get_property(dev->of_node, "qcom,wlan-rc-num", &prop_len);
  5359. if (!prop || !prop_len) {
  5360. cnss_pr_err("Failed to get PCIe RC number from DT\n");
  5361. goto out;
  5362. }
  5363. rc_count = prop_len / sizeof(__be32);
  5364. for (i = 0; i < rc_count; i++) {
  5365. ret = cnss_pci_enumerate(plat_priv, be32_to_cpup(&prop[i]));
  5366. if (!ret)
  5367. break;
  5368. else if (ret == -EPROBE_DEFER || (ret && i == rc_count - 1))
  5369. goto out;
  5370. }
  5371. ret = pci_register_driver(&cnss_pci_driver);
  5372. if (ret) {
  5373. cnss_pr_err("Failed to register to PCI framework, err = %d\n",
  5374. ret);
  5375. goto out;
  5376. }
  5377. if (!plat_priv->bus_priv) {
  5378. cnss_pr_err("Failed to probe PCI driver\n");
  5379. ret = -ENODEV;
  5380. goto unreg_pci;
  5381. }
  5382. return 0;
  5383. unreg_pci:
  5384. pci_unregister_driver(&cnss_pci_driver);
  5385. out:
  5386. return ret;
  5387. }
  5388. void cnss_pci_deinit(struct cnss_plat_data *plat_priv)
  5389. {
  5390. pci_unregister_driver(&cnss_pci_driver);
  5391. }