pci.c 153 KB

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