pci.c 158 KB

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