sdio_autok.c 173 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136
  1. #include <linux/scatterlist.h>
  2. #include <linux/mmc/card.h>
  3. #include <linux/mmc/host.h>
  4. #include <linux/mmc/mmc.h>
  5. #include <linux/mmc/sdio_func.h>
  6. #include <linux/mmc/sdio.h>
  7. #include <linux/delay.h>
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #include <mt-plat/upmu_common.h>
  11. #include "mt_sd.h"
  12. #include "sdio_autok.h"
  13. /*****************************************************************************
  14. * Symbol/Type Definition *
  15. *****************************************************************************/
  16. #define AUTOK_VERSION_NO (0x62900010)
  17. #define SDIO_AUTOK_ID (1)
  18. #ifdef MT6582LTE
  19. #undef MT6582LTE
  20. #endif
  21. #ifdef MT6592LTE
  22. #undef MT6592LTE
  23. #endif
  24. #ifdef MT6595WIFI
  25. #undef MT6595WIFI
  26. #endif
  27. #ifdef MT6752WIFI
  28. #undef MT6752WIFI
  29. #endif
  30. #ifdef MT6735WIFI
  31. #undef MT6735WIFI
  32. #endif
  33. /* Host platform definition */
  34. /* #define MT6582LTE */
  35. /* #define MT6592LTE */
  36. /* #define MT6595WIFI */
  37. /* #define MT6752WIFI */
  38. #define MT6735WIFI
  39. #define AUTOK_CMD_TIMES (20)
  40. #define AUTOK_RDAT_TIMES (1)
  41. #define AUTOK_WDAT_TIMES (1)
  42. #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */
  43. /* AUTOK features definition */
  44. /* #define AUTOK_CYC_ALG_0 */
  45. /* #define AUTOK_CYC_ALG_1 */
  46. #define AUTOK_CYC_ALG_2
  47. /* #define AUTOK_WDAT_BOUND_DOUB_CHK */
  48. /* #define AUTOK_CMD_TUNE_LEGACY */
  49. #define AUTOK_RDAT_ACC
  50. /* AUTOK constant */
  51. #define AUTOK_TUNING_INACCURACY (2)
  52. #define AUTOK_CMD_EDGE_MATRIX_SCAN (1)
  53. #define AUTOK_CKGEN_ALLOW_MAX (2)
  54. #define AUTOK_RDAT_FBOUND_TH (autok_rdat_fbound_th)
  55. #define AUTOK_CMD_SCAN_RANGE_STG1 (pad_delay_period_cycle/4) /* (6) */
  56. #define AUTOK_CMD_SCAN_DE_RANGE_STG2 (8)
  57. #define AUTOK_SKIP_CMDTUNE_ON_STG2 (32)
  58. #define AUTOK_CMDPAT_CHK_SHIFT (6)
  59. #define TUNING_TEST_TIME (64)
  60. #define AUTOK_CMDMAT_CROSS_MAR (8)
  61. #define AUTOK_VCORE_SCAN_NUM 1 /* ccyeh */
  62. #define AUTOK_SS_IO_RING_TH (2407)
  63. #define AUTOK_SS_CORE_RING_TH (3162)
  64. #define AUTOK_TT_CORE_RING_TH (3307)
  65. #define AUTOK_TINY_MAR_TH_TOP (15)
  66. #define AUTOK_TINY_MAR_TH_BTM (8)
  67. #define AUTOK_TINY_MAR_R_MAR_MIN (3)
  68. #define AUTOK_TINY_MAR_R_SHIFT_MAX (2)
  69. #define AUTOK_TINY_MAR_PAT (0xAA55)
  70. /*CMD*/
  71. #define SCALE_CMD_RSP_DLY_SEL (32)
  72. #define SCALE_CKGEN_MSDC_DLY_SEL (32)
  73. #define SCALE_PAD_TUNE_CMDRDLY (32)
  74. #define SCALE_CMDMAT_RSP_DLY_SEL (SCALE_CMD_RSP_DLY_SEL/2)
  75. /*READ*/
  76. #define SCALE_DATA_DRIVING (8)
  77. #define SCALE_INT_DAT_LATCH_CK_SEL (8)
  78. #if 1
  79. #define SCALE_IOCON_RDSPL (2)
  80. #define SCALE_PAD_TUNE_DATRDDLY (32)
  81. #else
  82. #define SCALE_IOCON_RD0SPL (2)
  83. #define SCALE_IOCON_RD1SPL (2)
  84. #define SCALE_IOCON_RD2SPL (2)
  85. #define SCALE_IOCON_RD3SPL (2)
  86. #define SCALE_DAT_RDDLY0_D0 (32)
  87. #define SCALE_DAT_RDDLY0_D1 (32)
  88. #define SCALE_DAT_RDDLY0_D2 (32)
  89. #define SCALE_DAT_RDDLY0_D3 (32)
  90. #endif
  91. /*WRITE*/
  92. #define SCALE_WRDAT_CRCS_TA_CNTR (8)
  93. #define SCALE_IOCON_WD0SPL (2)
  94. #define SCALE_PAD_TUNE_DATWRDLY (32)
  95. /*Following definition is provided by spec 3.0*/
  96. #define MAX_DELAY_VARIATION_DUE_TO_TEMPERATURE_IN_PS (2600) /*-25degC~~+125degC*/
  97. #define F208M_CYCLE_IN_PS (4808)
  98. /*Following definition is provieded by designer PVT(Process/Voltage/Temperature) simulation result
  99. The data is used to calculating the auto-K stage1 range spec*/
  100. #define MIN_CLK_GEN_DELAY_IN_PS (9120)
  101. #define MAX_CLK_GEN_DELAY_IN_PS (24320) /* (18950) */
  102. #define MIN_PAD_DELAY_IN_PS (2400) /*bc_RCcbest : FF/LT/HV */
  103. #define MAX_PAD_DELAY_IN_PS (6400) /*wcl_RCcworst SS/LT/LV*/
  104. #define SCALE_OF_CLK_GEN_2_PAD_DELAY (MIN_CLK_GEN_DELAY_IN_PS/MIN_PAD_DELAY_IN_PS)
  105. /*****************************************************************************
  106. * Macro Function Definition *
  107. *****************************************************************************/
  108. #define DIV_CEIL_FUNC(_n, _d) ((_n)/(_d)+(((_n)%(_d) == 0)?0:1))
  109. #define ABS_DIFF(_a, _b) (((_a) >= (_b))?((_a)-(_b)):((_b)-(_a)))
  110. #define THRESHOLD_VAL(_v, _t) (((_v) >= (_t))?(_t):(_v))
  111. #define MAX_GET(_v, _t) (((_v) >= (_t))?(_v):(_t))
  112. #define MIN_GET(_v, _t) (((_v) >= (_t))?(_t):(_v))
  113. #define FREQ_MHZ_2_PERIOD_CYCLE_IN_PS(_Mhz) (1000000L/(_Mhz))
  114. #define MIN_SCORE_OF_CLK_GEN_IN_ONE_CYCLE(_periodCycle) \
  115. (DIV_CEIL_FUNC((SCALE_CKGEN_MSDC_DLY_SEL*(_periodCycle)), MAX_CLK_GEN_DELAY_IN_PS))
  116. #define MIN_SCORE_OF_PAD_DELAY_IN_ONE_CYCLE(_periodCycle) \
  117. (DIV_CEIL_FUNC((SCALE_PAD_TUNE_CMDRDLY*(_periodCycle)), MAX_PAD_DELAY_IN_PS))
  118. #define MAX_SCALE_OF_CLK_GEN_IN_ONE_CYCLE(_periodCycle) \
  119. (DIV_CEIL_FUNC((SCALE_CKGEN_MSDC_DLY_SEL*(_periodCycle)), MIN_CLK_GEN_DELAY_IN_PS))
  120. #define MAX_SCORE_OF_PAD_DELAY_AGAINST_TEMP_VAR \
  121. (DIV_CEIL_FUNC((SCALE_PAD_TUNE_CMDRDLY*MAX_DELAY_VARIATION_DUE_TO_TEMPERATURE_IN_PS), MIN_PAD_DELAY_IN_PS))
  122. #define MIN_SCORE_OF_PAD_DELAY_AGAINST_TEMP_VAR \
  123. (DIV_CEIL_FUNC((SCALE_PAD_TUNE_CMDRDLY*MAX_DELAY_VARIATION_DUE_TO_TEMPERATURE_IN_PS), MAX_PAD_DELAY_IN_PS))
  124. #define USER_DEF_MAX_SCORE_OF_PAD_DELAY_AGAINST_TEMP_VAR(_x, _y) \
  125. (DIV_CEIL_FUNC(((_x)*MAX_DELAY_VARIATION_DUE_TO_TEMPERATURE_IN_PS), (_y)))
  126. #define MIN_DATA_SCORE (MIN_SCORE_OF_PAD_DELAY_AGAINST_TEMP_VAR - AUTOK_TUNING_INACCURACY)
  127. #ifdef MT6290
  128. #define AUTOK_PRINT(_fmt, args...) printf("[AUTO_K]" _fmt, ## args)
  129. #else
  130. /* #define AUTOK_PRINT(_fmt, args...) printk(KERN_ERR "[AUTO_K]" _fmt, ## args) */
  131. #define AUTOK_PSIZE PAGE_SIZE
  132. char autok_single[128];
  133. char *log_info = NULL;
  134. int autok_size = 0;
  135. int total_msg_size = 0;
  136. #define AUTOK_PRINT(_fmt, args...) \
  137. do { \
  138. autok_size = snprintf(autok_single, 128, _fmt, ## args); \
  139. if (log_info != NULL && total_msg_size+autok_size < LOG_SIZE) { \
  140. memcpy(log_info+total_msg_size, autok_single, autok_size); \
  141. total_msg_size += autok_size; \
  142. } \
  143. pr_debug("[AUTO_K]" _fmt, ## args); \
  144. } while (0)
  145. #endif
  146. #ifdef AUTOK_DEBUG
  147. #define AUTOK_ERR() do {} while (1)
  148. #endif
  149. #define msdc_retry(expr, retry, cnt, id) \
  150. do { \
  151. int backup = cnt; \
  152. while (retry) { \
  153. if (!(expr)) \
  154. break; \
  155. if (cnt-- == 0) { \
  156. retry--; mdelay(1); cnt = backup; \
  157. } \
  158. } \
  159. WARN_ON(retry == 0); \
  160. } while (0)
  161. #define msdc_reset(id) \
  162. do { \
  163. int retry = 3, cnt = 1000; \
  164. sdr_set_bits(MSDC_CFG, MSDC_CFG_RST); \
  165. mb(); \
  166. msdc_retry(sdr_read32(MSDC_CFG) & MSDC_CFG_RST, retry, cnt, id); \
  167. } while (0)
  168. #define msdc_clr_int() \
  169. do { \
  170. volatile u32 val = sdr_read32(MSDC_INT); \
  171. sdr_write32(MSDC_INT, val); \
  172. } while (0)
  173. #define msdc_clr_fifo(id) \
  174. do { \
  175. int retry = 3, cnt = 1000; \
  176. sdr_set_bits(MSDC_FIFOCS, MSDC_FIFOCS_CLR); \
  177. msdc_retry(sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_CLR, retry, cnt, id); \
  178. } while (0)
  179. #define msdc_reset_hw(id) \
  180. do { \
  181. msdc_reset(id); \
  182. msdc_clr_fifo(id); \
  183. msdc_clr_int(); \
  184. } while (0)
  185. #define msdc_txfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16)
  186. #define msdc_rxfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) >> 0)
  187. /*****************************************************************************
  188. * Enum *
  189. *****************************************************************************/
  190. typedef enum {
  191. E_RESULT_PASS = 0,
  192. E_RESULT_CMD_CRC = 1,
  193. E_RESULT_W_CRC = 2,
  194. E_RESULT_R_CRC = 3,
  195. E_RESULT_ERR = 4,
  196. E_RESULT_START = 5,
  197. E_RESULT_PW_SMALL = 6,
  198. E_RESULT_KEEP_OLD = 7,
  199. E_RESULT_TO = 8,
  200. E_RESULT_CMP_ERR = 9,
  201. E_RESULT_MAX
  202. } E_RESULT_TYPE;
  203. typedef enum {
  204. AUTOK_CMD = 0,
  205. AUTOK_DATA,
  206. AUTOK_FAIL,
  207. AUTOK_DONE
  208. } E_AUTOK_STATE;
  209. typedef enum {
  210. ERR_NONE = 0,
  211. ERR_OCCURE,
  212. PASS_AFTER_ERR,
  213. ERR_MAX
  214. } E_AUTOK_ERR_STA;
  215. typedef enum {
  216. PERIOD_NONE = 0,
  217. PERIOD_F_FIRST_POS,
  218. PERIOD_F_FIRST_POS_DONE,
  219. PERIOD_F_SECOND_POS,
  220. PERIOD_L_FIRST_POS,
  221. PERIOD_L_FIRST_POS_DONE,
  222. PERIOD_L_SECOND_POS,
  223. PERIOD_DONE,
  224. PERIOD_DONE_2,
  225. PERIOD_MAX,
  226. } E_AUTOK_PERIOD_STA;
  227. typedef struct {
  228. unsigned int interDelaySel;
  229. unsigned int cmdScore;
  230. unsigned int cmdPadSel;
  231. unsigned int cmdEdgeSel;
  232. unsigned int readScore;
  233. unsigned int readPadSel;
  234. unsigned int writeScore;
  235. unsigned int writePadSel;
  236. } S_AUTOK_CKGEN_DATA;
  237. typedef enum {
  238. SEARCH_FIRST_PASS = 0,
  239. SEARCH_SECOND_PASS,
  240. SEARCH_PASS_REGION,
  241. PASS_REGION_GET,
  242. SEARCH_MAX
  243. } E_AUTOK_DATA_STA;
  244. typedef struct {
  245. unsigned int raw_data;
  246. unsigned int score;
  247. unsigned int numOfzero;
  248. unsigned int fstPosErr;
  249. unsigned int fstPosErrEnd;
  250. unsigned int period;
  251. } S_AUTOK_CMD_DLY;
  252. typedef enum {
  253. TUNING_STG1 = 0,
  254. TUNING_STG2,
  255. TUNING_STG_MAX
  256. } E_AUTOK_TUNING_STAGE;
  257. typedef enum {
  258. RD_SCAN_NONE,
  259. RD_SCAN_PAD_BOUND_S,
  260. RD_SCAN_PAD_BOUND_E,
  261. RD_SCAN_PAD_BOUND_S_2,
  262. RD_SCAN_PAD_BOUND_E_2,
  263. RD_SCAN_PAD_MARGIN,
  264. } AUTOK_RAWD_SCAN_STA_E;
  265. typedef enum {
  266. CMDPAT_NONE,
  267. CMDPAT_IDENTICAL,
  268. CMDPAT_HALF_IDENTICAL,
  269. } AUTOK_CMDPAT_COMP_E;
  270. typedef enum {
  271. CMDPAT_REG_1_L = 1,
  272. CMDPAT_REG_2_L,
  273. } AUTOK_CMDPAT_SCEN_E;
  274. typedef struct {
  275. unsigned int RawData;
  276. unsigned int BoundReg1_S;
  277. unsigned int BoundReg1_E;
  278. unsigned int Reg1Cnt;
  279. unsigned int BoundReg2_S;
  280. unsigned int BoundReg2_E;
  281. unsigned int Reg2Cnt;
  282. unsigned char fInvalidCKGEN;
  283. unsigned char CurCKGEN;
  284. } AUTOK_RAWD_SCAN_T, *P_AUTOK_RAWD_SCAN_T;
  285. typedef struct {
  286. unsigned int PadDlyPeriodLen;
  287. unsigned int CKGenPeriodLen;
  288. unsigned int fCMDIntDlyConf;
  289. unsigned int fFBound;
  290. unsigned int FBoundCK;
  291. unsigned int fLBound;
  292. unsigned int LBoundCK;
  293. unsigned int fHoleCK;
  294. unsigned int HoleCK;
  295. unsigned int FBoundMidRefCMD;
  296. unsigned int FBoundCKRefCMD;
  297. unsigned int FBoundCntRefCMD;
  298. unsigned int fFBoundRefCMD;
  299. } AUTOK_CYC_SCAN_RES_T, *P_AUTOK_CYC_SCAN_RES_T;
  300. typedef struct {
  301. unsigned int CmdPadDly;
  302. unsigned int CmdPadDlyStg1Bak;
  303. unsigned int CmdIntDly;
  304. unsigned int fTimingShiftLarge;
  305. unsigned int fRetOk;
  306. } AUTOK_CMD_TUNE_RES_T, *P_AUTOK_CMD_TUNE_RES_T;
  307. typedef struct {
  308. unsigned int fRetOk;
  309. unsigned int PadDlyRefRD;
  310. unsigned int CKGenSel;
  311. } AUTOK_RD_TUNE_RES_T, *P_AUTOK_RD_TUNE_RES_T;
  312. typedef struct {
  313. unsigned int ck_s_b4;
  314. unsigned int raw_s_b4;
  315. unsigned int ck_e_b4;
  316. unsigned int raw_e_b4;
  317. AUTOK_CMDPAT_SCEN_E scen;
  318. int diff_b4;
  319. unsigned int cmd_int_shift;
  320. unsigned int raw_s_sh;
  321. unsigned int raw_e_sh;
  322. } AUTOK_CMDPAT_DOUBLE_CHK_T, *P_AUTOK_CMDPAT_DOUBLE_CHK_T;
  323. typedef struct {
  324. int pad_trans_s1;
  325. int pad_trans_e1;
  326. int pad_trans_m1;
  327. int pad_trans_cnt1;
  328. int pad_trans_s2;
  329. int pad_trans_e2;
  330. int pad_trans_m2;
  331. int pad_trans_cnt2;
  332. } AUTOK_CMDMAT_CHAR_POS, *P_AUTOK_CMDMAT_CHAR_POS;
  333. /*****************************************************************************
  334. * Global/External Variable *
  335. *****************************************************************************/
  336. static const unsigned int tuning_data[] = { 0xAA55AA55, 0xAA558080, 0x807F8080,
  337. 0x807F7F7F, 0x807F7F7F, 0x404040BF,
  338. 0xBFBF40BF, 0xBFBF2020, 0x20DF2020,
  339. 0x20DFDFDF, 0x101010EF, 0xEFEF10EF,
  340. 0xEFEF0808, 0x08F70808, 0x08F7F7F7,
  341. 0x040404FB, 0xFBFB04FB, 0xFBFB0202,
  342. 0x02FD0202, 0x02FDFDFD, 0x010101FE,
  343. 0xFEFE01FE, 0xFEFE0000, 0x00FF0000,
  344. 0x00FFFFFF, 0x000000FF, 0xFFFF00FF,
  345. 0xFFFF0000, 0xFF0FFF00, 0xFFCCC3CC,
  346. 0xC33CCCFF, 0xFEFFFEEF, 0xFFDFFFDD,
  347. 0xFFFBFFFB, 0xBFFF7FFF, 0x77F7BDEF,
  348. 0xFFF0FFF0, 0x0FFCCC3C, 0xCC33CCCF,
  349. 0xFFEFFFEE, 0xFFFDFFFD, 0xDFFFBFFF,
  350. 0xBBFFF7FF, 0xF77F7BDE
  351. };
  352. #define TUNING_DATA_NO (sizeof(tuning_data)/sizeof(unsigned int))
  353. #if 1
  354. static const unsigned char tuning_cmd[] = { 0x55, 0xAA, 0x5A, 0xA5, /* 01010101, 10101010, 01011010, 10100101 */
  355. 0x55, 0xAA, 0x5A, 0xA5, /* 01010101, 10101010, 01011010, 10100101 */
  356. 0x55, 0xAA, 0x5A, 0xA5, /* 01010101, 10101010, 01011010, 10100101 */
  357. 0x55, 0xAA, 0x5A, 0xA5, /* 01010101, 10101010, 01011010, 10100101 */
  358. };
  359. #else
  360. static const unsigned char tuning_cmd[] = { 0x00, 0xFF, 0x0F, 0xF0, /* 00000000, 11111111, 00001111, 11110000 */
  361. 0x33, 0xCC, 0x3C, 0xC3, /* 00110011, 11001100, 00111100, 11000011 */
  362. 0x55, 0xAA, 0x5A, 0xA5, /* 01010101, 10101010, 01011010, 10100101 */
  363. 0x77, 0x88, 0x78, 0x87, /* 01110111, 10001000, 01111000, 10000111 */
  364. /*0x12, 0x34, 0x56, 0x78,
  365. 0x9A, 0xAB, 0xCD, 0xEF,
  366. 0x02, 0x3F, 0x95, 0xFD, */
  367. };
  368. #endif
  369. #define TUNING_CMD_NO (sizeof(tuning_cmd)/sizeof(unsigned char))
  370. static U_AUTOK_INTERFACE_DATA **g_pp_autok_data;
  371. /* micro-volt */
  372. unsigned int mt65x2_vcore_tbl[] = {
  373. 600000, 606250, 612500, 618750, 625000, 631250, 637500, 643750,
  374. 650000, 656250, 662500, 668750, 675000, 681250, 687500, 693750,
  375. 700000, 706250, 712500, 718750, 725000, 731250, 737500, 743750,
  376. 750000, 756250, 762500, 768750, 775000, 781250, 787500, 793750,
  377. 800000, 806250, 812500, 818750, 825000, 831250, 837500, 843750,
  378. 850000, 856250, 862500, 868750, 875000, 881250, 887500, 893750,
  379. 900000, 906250, 912500, 918750, 925000, 931250, 937500, 943750,
  380. 950000, 956250, 962500, 968750, 975000, 981250, 987500, 993750,
  381. 1000000, 1006250, 1012500, 1018750, 1025000, 1031250, 1037500, 1043750,
  382. 1050000, 1056250, 1062500, 1068750, 1075000, 1081250, 1087500, 1093750,
  383. 1100000, 1106250, 1112500, 1118750, 1125000, 1131250, 1137500, 1143750,
  384. 1150000, 1156250, 1162500, 1168750, 1175000, 1181250, 1187500, 1193750,
  385. 1200000, 1206250, 1212500, 1218750, 1225000, 1231250, 1237500, 1243750,
  386. 1250000, 1256250, 1262500, 1268750, 1275000, 1281250, 1287500, 1293750,
  387. 1300000, 1306250, 1312500, 1318750, 1325000, 1331250, 1337500, 1343750,
  388. 1350000, 1356250, 1362500, 1368750, 1375000, 1381250, 1387500, 1393750,
  389. 1400000
  390. };
  391. #ifdef CONFIG_ARCH_MT6753
  392. unsigned int g_autok_vcore_sel[AUTOK_VCORE_SCAN_NUM] = { 1250000 }; /* Denali-3 */
  393. #else
  394. unsigned int g_autok_vcore_sel[AUTOK_VCORE_SCAN_NUM] = { 1150000 }; /* Denali-1 */
  395. #endif
  396. static unsigned int g_test_write_pattern[TUNING_TEST_TIME * TUNING_DATA_NO];
  397. static unsigned int g_test_read_pattern[TUNING_TEST_TIME];
  398. S_AUTOK_CKGEN_DATA autok_ckg_data[SCALE_CKGEN_MSDC_DLY_SEL];
  399. AUTOK_RAWD_SCAN_T autok_rdata_scan[SCALE_CKGEN_MSDC_DLY_SEL];
  400. S_AUTOK_CMD_DLY autok_cmd_cmdrrdly[SCALE_CMD_RSP_DLY_SEL];
  401. S_AUTOK_CMD_DLY autok_cmd_ckgdly[SCALE_CKGEN_MSDC_DLY_SEL];
  402. #ifdef AUTOK_CYC_ALG_0
  403. S_AUTOK_CMD_DLY autok_cmd_ckgdly_cmdrrdly0[SCALE_CKGEN_MSDC_DLY_SEL];
  404. #endif
  405. #if defined(AUTOK_CYC_ALG_0) && defined(AUTOK_CMD_TUNE_LEGACY)
  406. unsigned int autok_cmddly_stop_bit[SCALE_CMD_RSP_DLY_SEL];
  407. #endif
  408. #ifdef AUTOK_CYC_ALG_2
  409. unsigned int autok_paddly_per_cyc_eval = 64;
  410. #endif
  411. static unsigned int autok_rdat_fbound_th = 4;
  412. static unsigned int autok_vcore_scan_num = AUTOK_VCORE_SCAN_NUM;
  413. static unsigned int freq_mhz = 200;
  414. #if defined(MT6582LTE)
  415. static unsigned int gfIOSS;
  416. static unsigned int gfCoreTT;
  417. #endif
  418. static unsigned int gfTinyMar;
  419. static unsigned int gfEqualVcore;
  420. static char g_tune_result_str[33];
  421. unsigned char autok_param_name[E_AUTOK_DLY_PARAM_MAX][25] = {
  422. {"PAD_CMD_RESP_RXDLY"},
  423. {"CMD_RSP_TA_CNTR"},
  424. {"R_SMPL"},
  425. {"CKGEN_MSDC_DLY_SEL"},
  426. {"PAD_CMD_RXDLY"},
  427. {"INT_DAT_LATCH_CK_SEL"},
  428. {"R_D_SMPL"},
  429. {"PAD_DATA_RD_RXDLY"},
  430. {"WRDAT_CRCS_TA_CNTR"},
  431. {"W_D_SMPL"},
  432. {"PAD_DATA_WR_RXDLY"}
  433. };
  434. /*****************************************************************************
  435. * Functions Implement *
  436. *****************************************************************************/
  437. int autok_start_rw(struct msdc_host *host, u8 *value, unsigned size, unsigned blocks, bool write)
  438. {
  439. int ret = 0;
  440. void __iomem *base = host->base;
  441. struct mmc_data *data = host->data;
  442. struct mmc_command *cmd = host->mrq->cmd;
  443. /* Code in host drivers/fwk assumes that "blocks" always is >=1 */
  444. data->blocks = blocks;
  445. data->error = 0;
  446. sg_init_one(data->sg, value, size);
  447. host->xfer_size = blocks * data->blksz;
  448. host->blksz = data->blksz;
  449. host->autocmd = 0;
  450. host->dma_xfer = 0;
  451. sdr_write32(SDC_BLK_NUM, blocks);
  452. /* check msdc is work ok. rule is RX/TX fifocnt must be zero after last request
  453. * if find abnormal, try to reset msdc first
  454. */
  455. if (msdc_txfifocnt() || msdc_rxfifocnt()) {
  456. pr_err("[%s][SD%d] register abnormal,please check!\n", __func__, host->id);
  457. msdc_reset_hw(host->id);
  458. }
  459. ret = msdc_do_command(host, cmd, 0, CMD_TIMEOUT);
  460. if (ret != 0)
  461. return ret;
  462. if (write == 0) {
  463. ret = msdc_pio_read(host, data);
  464. if (ret != 0)
  465. return ret;
  466. } else {
  467. ret = msdc_pio_write(host, data);
  468. if (ret != 0)
  469. return ret;
  470. }
  471. return 0;
  472. }
  473. int autok_io_rw_extended(struct msdc_host *host, unsigned int u4Addr, unsigned int u4Func,
  474. void *pBuffer, unsigned int u4Len, bool write)
  475. {
  476. int ret = 0;
  477. u8 *value = (u8 *) pBuffer;
  478. struct sdio_func *sdioFunc;
  479. struct mmc_request mrq = { NULL };
  480. struct mmc_command cmd = { 0 };
  481. struct mmc_data data = { 0 };
  482. struct scatterlist sg;
  483. unsigned remainder = u4Len;
  484. unsigned max_blocks;
  485. unsigned size;
  486. if ((pBuffer == NULL) || (host == NULL)) {
  487. pr_err("[%s] [ERR] pBuffer = %p, host = %p\n", __func__, pBuffer, host);
  488. return -1;
  489. }
  490. if (u4Len < 4) {
  491. pr_err("[%s] [ERR] u4Len = %d\n", __func__, u4Len);
  492. return -1;
  493. }
  494. /* Setup mrq */
  495. mrq.cmd = &cmd;
  496. mrq.data = &data;
  497. host->mrq = &mrq;
  498. /* Setup cmd */
  499. cmd.opcode = SD_IO_RW_EXTENDED;
  500. cmd.arg = 0;
  501. cmd.arg = write ? 0x80000000 : 0x00000000;
  502. cmd.arg |= u4Func << 28;
  503. cmd.arg |= u4Addr << 9;
  504. cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
  505. cmd.data = &data;
  506. /* Setup data */
  507. data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
  508. data.sg = &sg;
  509. data.sg_len = 1;
  510. host->data = &data;
  511. sdioFunc = host->mmc->card->sdio_func[u4Func - 1];
  512. max_blocks = min(sdioFunc->card->host->max_blk_count,
  513. sdioFunc->card->host->max_seg_size / sdioFunc->cur_blksize);
  514. max_blocks = min(max_blocks, 511u);
  515. while (remainder >= sdioFunc->cur_blksize) {
  516. unsigned blocks;
  517. blocks = remainder / sdioFunc->cur_blksize;
  518. if (blocks > max_blocks)
  519. blocks = max_blocks;
  520. size = blocks * sdioFunc->cur_blksize;
  521. cmd.arg |= 0x08000000 | blocks;
  522. data.blksz = sdioFunc->cur_blksize;
  523. ret = autok_start_rw(host, value, size, blocks, write);
  524. if (ret != 0)
  525. goto stop;
  526. remainder -= size;
  527. value += size;
  528. }
  529. /* Write the remainder using byte mode. */
  530. while (remainder > 0) {
  531. size = remainder;
  532. cmd.arg &= ~(0x08000000);
  533. cmd.arg |= size;
  534. data.blksz = size;
  535. ret = autok_start_rw(host, value, size, 1, write);
  536. if (ret != 0)
  537. goto stop;
  538. remainder -= size;
  539. value += size;
  540. }
  541. stop:
  542. host->mrq = NULL;
  543. host->data = NULL;
  544. host->dma_xfer = 0;
  545. host->blksz = 0;
  546. return ret;
  547. }
  548. int autok_io_rw_direct(struct msdc_host *host, unsigned int u4Addr, unsigned int u4Func,
  549. void *pBuffer, unsigned int u4Len, bool write)
  550. {
  551. int ret = 0;
  552. u8 *value = (u8 *) pBuffer;
  553. void __iomem *base = host->base;
  554. struct mmc_command cmd = { 0 };
  555. struct mmc_request mrq = { NULL };
  556. if ((pBuffer == NULL) || (host == NULL)) {
  557. pr_err("[%s] [ERR] pBuffer = %p, host = %p\n", __func__, pBuffer, host);
  558. return -1;
  559. }
  560. if (u4Len > 1) {
  561. pr_err("[%s] [ERR] u4Len = %d\n", __func__, u4Len);
  562. return -1;
  563. }
  564. /* Setup mrq */
  565. mrq.cmd = &cmd;
  566. host->mrq = &mrq;
  567. cmd.opcode = SD_IO_RW_DIRECT;
  568. cmd.arg = write ? 0x80000000 : 0x00000000;
  569. cmd.arg |= u4Func << 28;
  570. cmd.arg |= u4Addr << 9;
  571. cmd.arg |= *value;
  572. cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  573. memset(cmd.resp, 0, sizeof(cmd.resp));
  574. cmd.retries = 0;
  575. cmd.data = NULL;
  576. host->autocmd = 0;
  577. /* check msdc is work ok. rule is RX/TX fifocnt must be zero after last request
  578. * if find abnormal, try to reset msdc first
  579. */
  580. if (msdc_txfifocnt() || msdc_rxfifocnt()) {
  581. pr_err("[%s][SD%d] register abnormal,please check!\n", __func__, host->id);
  582. msdc_reset_hw(host->id);
  583. }
  584. ret = msdc_do_command(host, &cmd, 0, CMD_TIMEOUT);
  585. if (ret != 0)
  586. goto stop;
  587. if (write == 0)
  588. *value = cmd.resp[0] & 0xFF;
  589. stop:
  590. host->mrq = NULL;
  591. host->data = NULL;
  592. host->dma_xfer = 0;
  593. host->blksz = 0;
  594. return ret;
  595. }
  596. /*************************************************************************
  597. * FUNCTION
  598. * autok_get_current_vcore_offset
  599. *
  600. * DESCRIPTION
  601. * This function for auto-K, get current vcore information from pmic register
  602. *
  603. * PARAMETERS
  604. *
  605. * RETURN VALUES
  606. * current vcore
  607. *************************************************************************/
  608. unsigned int autok_get_current_vcore_offset(void)
  609. {
  610. unsigned int vcore_uv = 0;
  611. unsigned int idx, size;
  612. unsigned int vcore_sel = 0;
  613. unsigned int pmic_vcore = 0;
  614. unsigned int pmic_vio18 = 0;
  615. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  616. /* FIXME@CCJ vcore_uv = mt_vcore_dvfs_volt_get_by_sdio(); */
  617. vcore_uv = vcorefs_sdio_get_vcore_nml(); /* ccyeh */
  618. #endif
  619. AUTOK_PRINT("[%s] vcore_uv = %duV\r\n", __func__, vcore_uv);
  620. /* remove@CCJ vcore_uv=1150000; //ccyeh */
  621. pmic_read_interface(0x0616, &pmic_vcore, 0x7f, 0);
  622. AUTOK_PRINT("pmic_vcore: 0x%x\n", pmic_vcore);
  623. pmic_read_interface(0x0a80, &pmic_vio18, 0xf, 8);
  624. AUTOK_PRINT("pmic_vio18: 0x%x\n", pmic_vio18);
  625. /* removevcore_uv = 600000 + 6250 * pmic_vcore; */
  626. AUTOK_PRINT("vcore_uv@PMIC: %d uv\n", (600000 + 6250 * pmic_vcore));
  627. size = sizeof(mt65x2_vcore_tbl) / sizeof(mt65x2_vcore_tbl[0]);
  628. if (vcore_uv <= mt65x2_vcore_tbl[0]) {
  629. vcore_sel = 0;
  630. } else if (vcore_uv >= mt65x2_vcore_tbl[size - 1]) {
  631. vcore_sel = size - 1;
  632. } else {
  633. for (idx = 0; idx < size - 1; idx++) {
  634. if ((vcore_uv >= mt65x2_vcore_tbl[idx]) &&
  635. (vcore_uv < mt65x2_vcore_tbl[idx + 1])) {
  636. vcore_sel = idx;
  637. break;
  638. }
  639. }
  640. }
  641. /* Set vcore value to PMIC */
  642. AUTOK_PRINT("[%s] vcore_uv = %duV\r\n", __func__, vcore_uv);
  643. AUTOK_PRINT("[%s] Current Vcore = %duV(0x%x)\r\n", __func__, mt65x2_vcore_tbl[vcore_sel],
  644. vcore_sel);
  645. return vcore_sel;
  646. }
  647. /*************************************************************************
  648. * FUNCTION
  649. * msdc_autok_read
  650. *
  651. * DESCRIPTION
  652. * This function for auto-K, read from sdio device
  653. *
  654. * PARAMETERS
  655. * host: msdc host manipulator pointer
  656. * u4Addr: sdio device address
  657. * u4Func: sdio device function
  658. * pBuffer: content read from device
  659. * u4Len: read data length
  660. * u4Cmd: transferred cmd (cmd52/cmd53)
  661. *
  662. * RETURN VALUES
  663. * error code: refer to errno.h
  664. *************************************************************************/
  665. int msdc_autok_read(struct msdc_host *host, unsigned int u4Addr, unsigned int u4Func, void *pBuffer,
  666. unsigned int u4Len, unsigned int u4Cmd)
  667. {
  668. int ret = 0;
  669. if ((pBuffer == NULL) || (host == NULL)) {
  670. pr_err("[%s] pBuffer = %p, host = %p\n", __func__, pBuffer, host);
  671. return -1;
  672. }
  673. if (((u4Cmd == CMD_53) && (u4Len < 4)) || ((u4Cmd == CMD_52) && (u4Len > 1))) {
  674. pr_err("[%s] u4Cmd = %d, u4Len = %d\n", __func__, u4Cmd, u4Len);
  675. return -1;
  676. }
  677. if (u4Cmd == CMD_53)
  678. ret = autok_io_rw_extended(host, u4Addr, u4Func, pBuffer, u4Len, 0);
  679. else if (u4Cmd == CMD_52)
  680. ret = autok_io_rw_direct(host, u4Addr, u4Func, pBuffer, u4Len, 0);
  681. else {
  682. pr_err("[%s] Doesn't support u4Cmd = %d\n", __func__, u4Cmd);
  683. ret = -1;
  684. }
  685. return ret;
  686. }
  687. /*************************************************************************
  688. * FUNCTION
  689. * msdc_autok_write
  690. *
  691. * DESCRIPTION
  692. * This function for auto-K, write to sdio device
  693. *
  694. * PARAMETERS
  695. * host: msdc host manipulator pointer
  696. * u4Addr: sdio device address
  697. * u4Func: sdio device function
  698. * pBuffer: content write to device
  699. * u4Len: write data length
  700. * u4Cmd: transferred cmd (cmd52/cmd53)
  701. *
  702. * RETURN VALUES
  703. * error code: refer to errno.h
  704. *************************************************************************/
  705. int msdc_autok_write(struct msdc_host *host, unsigned int u4Addr, unsigned int u4Func,
  706. void *pBuffer, unsigned int u4Len, unsigned int u4Cmd)
  707. {
  708. int ret = 0;
  709. if ((pBuffer == NULL) || (host == NULL)) {
  710. pr_err("[%s] pBuffer = %p, host = %p\n", __func__, pBuffer, host);
  711. return -1;
  712. }
  713. if (((u4Cmd == CMD_53) && (u4Len < 4)) || ((u4Cmd == CMD_52) && (u4Len > 1))) {
  714. pr_err("[%s] u4Cmd = %d, u4Len = %d\n", __func__, u4Cmd, u4Len);
  715. return -1;
  716. }
  717. if (u4Cmd == CMD_53)
  718. ret = autok_io_rw_extended(host, u4Addr, u4Func, pBuffer, u4Len, 1);
  719. else if (u4Cmd == CMD_52)
  720. ret = autok_io_rw_direct(host, u4Addr, u4Func, pBuffer, u4Len, 1);
  721. else {
  722. pr_err("[%s] Doesn't support u4Cmd = %d\n", __func__, u4Cmd);
  723. ret = -1;
  724. }
  725. return ret;
  726. }
  727. /*************************************************************************
  728. * FUNCTION
  729. * msdc_autok_adjust_param
  730. *
  731. * DESCRIPTION
  732. * This function for auto-K, adjust msdc parameter
  733. *
  734. * PARAMETERS
  735. * host: msdc host manipulator pointer
  736. * param: enum of msdc parameter
  737. * value: value of msdc parameter
  738. * rw: AUTOK_READ/AUTOK_WRITE
  739. *
  740. * RETURN VALUES
  741. * error code: 0 success,
  742. * -1 parameter input error
  743. * -2 read/write fail
  744. * -3 else error
  745. *************************************************************************/
  746. int msdc_autok_adjust_param(struct msdc_host *host, enum AUTOK_PARAM param, u32 *value, int rw)
  747. {
  748. void __iomem *base = host->base;
  749. ulong reg = 0;
  750. u32 field = 0;
  751. switch (param) {
  752. case CMD_EDGE:
  753. if ((rw == AUTOK_WRITE) && (*value > 1)) {
  754. pr_err
  755. ("[%s] Input value(%d) for CMD_EDGE is out of range, it should be [0~1]\n",
  756. __func__, *value);
  757. return -1;
  758. }
  759. reg = (ulong) (MSDC_IOCON);
  760. field = (u32) (MSDC_IOCON_RSPL);
  761. break;
  762. case RDATA_EDGE:
  763. if ((rw == AUTOK_WRITE) && (*value > 1)) {
  764. pr_err
  765. ("[%s] Input value(%d) for RDATA_EDGE is out of range, it should be [0~1]\n",
  766. __func__, *value);
  767. return -1;
  768. }
  769. reg = (ulong) (MSDC_IOCON);
  770. field = (u32) (MSDC_IOCON_R_D_SMPL);
  771. break;
  772. case WDATA_EDGE:
  773. if ((rw == AUTOK_WRITE) && (*value > 1)) {
  774. pr_err
  775. ("[%s] Input value(%d) for WDATA_EDGE is out of range, it should be [0~1]\n",
  776. __func__, *value);
  777. return -1;
  778. }
  779. reg = (ulong) (MSDC_IOCON);
  780. field = (u32) (MSDC_IOCON_W_D_SMPL);
  781. break;
  782. #if 0
  783. case CLK_DRV:
  784. if ((rw == AUTOK_WRITE) && (*value > 7)) {
  785. pr_err
  786. ("[%s] Input value(%d) for CLK_DRV is out of range, it should be [0~7]\n",
  787. __func__, *value);
  788. return -1;
  789. }
  790. if (host->id != 2) {
  791. pr_err("[%s] MSDC%d doesn't support AUTO K\n", __func__, host->id);
  792. return -1;
  793. }
  794. reg = (ulong) (MSDC2_GPIO_CLK_BASE);
  795. field = (u32) (GPIO_MSDC1_MSDC2_DRVN);
  796. break;
  797. case CMD_DRV:
  798. if ((rw == AUTOK_WRITE) && (*value > 7)) {
  799. pr_err
  800. ("[%s] Input value(%d) for CMD_DRV is out of range, it should be [0~7]\n",
  801. __func__, *value);
  802. return -1;
  803. }
  804. if (host->id != 2) {
  805. pr_err("[%s] MSDC%d doesn't support on AUTO K\n", __func__, host->id);
  806. return -1;
  807. }
  808. reg = (ulong) (MSDC2_GPIO_CMD_BASE);
  809. field = (u32) (GPIO_MSDC1_MSDC2_DRVN);
  810. break;
  811. case DAT_DRV:
  812. if ((rw == AUTOK_WRITE) && (*value > 7)) {
  813. pr_err
  814. ("[%s] Input value(%d) for DAT_DRV is out of range, it should be [0~7]\n",
  815. __func__, *value);
  816. return -1;
  817. }
  818. if (host->id != 2) {
  819. pr_err("[%s] MSDC%d doesn't support on AUTO K\n", __func__, host->id);
  820. return -1;
  821. }
  822. reg = (ulong) (MSDC2_GPIO_DAT_BASE);
  823. field = (u32) (GPIO_MSDC1_MSDC2_DRVN);
  824. break;
  825. #endif
  826. case DAT0_RD_DLY:
  827. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  828. pr_err
  829. ("[%s] Input value(%d) for DAT0_RD_DLY is out of range, it should be [0~31]\n",
  830. __func__, *value);
  831. return -1;
  832. }
  833. reg = (ulong) (MSDC_DAT_RDDLY0);
  834. field = (u32) (MSDC_DAT_RDDLY0_D0);
  835. break;
  836. case DAT1_RD_DLY:
  837. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  838. pr_err
  839. ("[%s] Input value(%d) for DAT1_RD_DLY is out of range, it should be [0~31]\n",
  840. __func__, *value);
  841. return -1;
  842. }
  843. reg = (ulong) (MSDC_DAT_RDDLY0);
  844. field = (u32) (MSDC_DAT_RDDLY0_D1);
  845. break;
  846. case DAT2_RD_DLY:
  847. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  848. pr_err
  849. ("[%s] Input value(%d) for DAT2_RD_DLY is out of range, it should be [0~31]\n",
  850. __func__, *value);
  851. return -1;
  852. }
  853. reg = (ulong) (MSDC_DAT_RDDLY0);
  854. field = (u32) (MSDC_DAT_RDDLY0_D2);
  855. break;
  856. case DAT3_RD_DLY:
  857. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  858. pr_err
  859. ("[%s] Input value(%d) for DAT3_RD_DLY is out of range, it should be [0~31]\n",
  860. __func__, *value);
  861. return -1;
  862. }
  863. reg = (ulong) (MSDC_DAT_RDDLY0);
  864. field = (u32) (MSDC_DAT_RDDLY0_D3);
  865. break;
  866. case DAT_WRD_DLY:
  867. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  868. pr_err
  869. ("[%s] Input value(%d) for DAT_WRD_DLY is out of range, it should be [0~31]\n",
  870. __func__, *value);
  871. return -1;
  872. }
  873. reg = (ulong) (MSDC_PAD_TUNE0);
  874. field = (u32) (MSDC_PAD_TUNE0_DATWRDLY);
  875. break;
  876. case DAT_RD_DLY:
  877. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  878. pr_err
  879. ("[%s] Input value(%d) for DAT_RD_DLY is out of range, it should be [0~31]\n",
  880. __func__, *value);
  881. return -1;
  882. }
  883. reg = (ulong) (MSDC_PAD_TUNE0);
  884. field = (u32) (MSDC_PAD_TUNE0_DATRRDLY);
  885. break;
  886. case CMD_RESP_RD_DLY:
  887. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  888. pr_err
  889. ("[%s] Input value(%d) for CMD_RESP_RD_DLY is out of range, it should be [0~31]\n",
  890. __func__, *value);
  891. return -1;
  892. }
  893. reg = (ulong) (MSDC_PAD_TUNE0);
  894. field = (u32) (MSDC_PAD_TUNE0_CMDRRDLY);
  895. break;
  896. case CMD_RD_DLY:
  897. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  898. pr_err
  899. ("[%s] Input value(%d) for CMD_RD_DLY is out of range, it should be [0~31]\n",
  900. __func__, *value);
  901. return -1;
  902. }
  903. reg = (ulong) (MSDC_PAD_TUNE0);
  904. field = (u32) (MSDC_PAD_TUNE0_CMDRDLY);
  905. break;
  906. case DATA_DLYLINE_SEL:
  907. if ((rw == AUTOK_WRITE) && (*value > 1)) {
  908. pr_err
  909. ("[%s] Input value(%d) for DATA_DLYLINE_SEL is out of range, it should be [0~1]\n",
  910. __func__, *value);
  911. return -1;
  912. }
  913. reg = (ulong) (MSDC_IOCON);
  914. field = (u32) (MSDC_IOCON_DDLSEL);
  915. break;
  916. case READ_DATA_SMPL_SEL:
  917. if ((rw == AUTOK_WRITE) && (*value > 1)) {
  918. pr_err
  919. ("[%s] Input value(%d) for READ_DATA_SMPL_SEL is out of range, it should be [0~1]\n",
  920. __func__, *value);
  921. return -1;
  922. }
  923. reg = (ulong) (MSDC_IOCON);
  924. field = (u32) (MSDC_IOCON_R_D_SMPL_SEL);
  925. break;
  926. case WRITE_DATA_SMPL_SEL:
  927. if ((rw == AUTOK_WRITE) && (*value > 1)) {
  928. pr_err
  929. ("[%s] Input value(%d) for WRITE_DATA_SMPL_SEL is out of range, it should be [0~1]\n",
  930. __func__, *value);
  931. return -1;
  932. }
  933. reg = (ulong) (MSDC_IOCON);
  934. field = (u32) (MSDC_IOCON_W_D_SMPL_SEL);
  935. break;
  936. case INT_DAT_LATCH_CK:
  937. if ((rw == AUTOK_WRITE) && (*value > 7)) {
  938. pr_err
  939. ("[%s] Input value(%d) for INT_DAT_LATCH_CK is out of range, it should be [0~7]\n",
  940. __func__, *value);
  941. return -1;
  942. }
  943. reg = (ulong) (MSDC_PATCH_BIT0);
  944. field = (u32) (MSDC_PB0_INT_DAT_LATCH_CK_SEL);
  945. break;
  946. case CKGEN_MSDC_DLY_SEL:
  947. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  948. pr_err
  949. ("[%s] Input value(%d) for CKGEN_MSDC_DLY_SEL is out of range, it should be [0~31]\n",
  950. __func__, *value);
  951. return -1;
  952. }
  953. reg = (ulong) (MSDC_PATCH_BIT0);
  954. field = (u32) (MSDC_PB0_CKGEN_MSDC_DLY_SEL);
  955. break;
  956. case CMD_RSP_TA_CNTR:
  957. if ((rw == AUTOK_WRITE) && (*value > 7)) {
  958. pr_err
  959. ("[%s] Input value(%d) for CMD_RSP_TA_CNTR is out of range, it should be [0~7]\n",
  960. __func__, *value);
  961. return -1;
  962. }
  963. reg = (ulong) (MSDC_PATCH_BIT1);
  964. field = (u32) (MSDC_PB1_CMD_RSP_TA_CNTR);
  965. break;
  966. case WRDAT_CRCS_TA_CNTR:
  967. if ((rw == AUTOK_WRITE) && (*value > 7)) {
  968. pr_err
  969. ("[%s] Input value(%d) for WRDAT_CRCS_TA_CNTR is out of range, it should be [0~7]\n",
  970. __func__, *value);
  971. return -1;
  972. }
  973. reg = (ulong) (MSDC_PATCH_BIT1);
  974. field = (u32) (MSDC_PB1_WRDAT_CRCS_TA_CNTR);
  975. break;
  976. case PAD_CLK_TXDLY:
  977. if ((rw == AUTOK_WRITE) && (*value > 31)) {
  978. pr_err
  979. ("[%s] Input value(%d) for PAD_CLK_TXDLY is out of range, it should be [0~31]\n",
  980. __func__, *value);
  981. return -1;
  982. }
  983. reg = (ulong) (MSDC_PAD_TUNE0);
  984. field = (u32) (MSDC_PAD_TUNE0_CLKTXDLY);
  985. break;
  986. default:
  987. pr_err("[%s] Value of [enum AUTOK_PARAM param] is wrong\n", __func__);
  988. return -1;
  989. }
  990. if (rw == AUTOK_READ)
  991. sdr_get_field(reg, field, *value);
  992. else if (rw == AUTOK_WRITE) {
  993. sdr_set_field(reg, field, *value);
  994. if (param == CKGEN_MSDC_DLY_SEL)
  995. mdelay(1);
  996. /*
  997. else
  998. mdelay(1);
  999. */
  1000. } else {
  1001. pr_err("[%s] Value of [int rw] is wrong\n", __func__);
  1002. return -1;
  1003. }
  1004. return 0;
  1005. }
  1006. static E_RESULT_TYPE errMapping(struct msdc_host *host)
  1007. {
  1008. E_RESULT_TYPE res = E_RESULT_PASS;
  1009. switch (host->error) {
  1010. case REQ_CMD_EIO:
  1011. res = E_RESULT_CMD_CRC;
  1012. break;
  1013. case REQ_CMD_TMO:
  1014. res = E_RESULT_TO;
  1015. break;
  1016. default:
  1017. res = E_RESULT_ERR;
  1018. break;
  1019. }
  1020. return res;
  1021. }
  1022. static void containGen(void)
  1023. {
  1024. unsigned int i, j;
  1025. unsigned int *pData = g_test_write_pattern;
  1026. for (j = 0; j < TUNING_DATA_NO; j++) {
  1027. for (i = 0; i < TUNING_TEST_TIME; i++) {
  1028. *pData = tuning_data[j];
  1029. pData++;
  1030. }
  1031. }
  1032. }
  1033. static E_RESULT_TYPE autok_write_test(struct msdc_host *host)
  1034. {
  1035. int i;
  1036. E_RESULT_TYPE res = E_RESULT_PASS;
  1037. unsigned int reg;
  1038. unsigned char *data;
  1039. /*use test mode to test write */
  1040. for (i = 0; i < TUNING_DATA_NO; i++) {
  1041. data = (unsigned char *)&tuning_data[i];
  1042. if (msdc_autok_write
  1043. (host, SDIO_IP_WTMDPCR1, LTE_MODEM_FUNC, (void *)data, 1, CMD_52) != 0) {
  1044. res = E_RESULT_CMD_CRC;
  1045. goto end;
  1046. }
  1047. if (msdc_autok_write
  1048. (host, SDIO_IP_WTMDPCR1 + 1, LTE_MODEM_FUNC, (void *)(data + 1), 1,
  1049. CMD_52) != 0) {
  1050. res = E_RESULT_CMD_CRC;
  1051. goto end;
  1052. }
  1053. if (msdc_autok_write
  1054. (host, SDIO_IP_WTMDPCR1 + 2, LTE_MODEM_FUNC, (void *)(data + 2), 1,
  1055. CMD_52) != 0) {
  1056. res = E_RESULT_CMD_CRC;
  1057. goto end;
  1058. }
  1059. if (msdc_autok_write
  1060. (host, SDIO_IP_WTMDPCR1 + 3, LTE_MODEM_FUNC, (void *)(data + 3), 1,
  1061. CMD_52) != 0) {
  1062. res = E_RESULT_CMD_CRC;
  1063. goto end;
  1064. }
  1065. if (msdc_autok_write
  1066. (host, SDIO_IP_WTMDR, LTE_MODEM_FUNC,
  1067. (void *)&(g_test_write_pattern[i * TUNING_TEST_TIME]), (4 * TUNING_TEST_TIME),
  1068. CMD_53) != 0) {
  1069. res = errMapping(host);
  1070. goto end;
  1071. }
  1072. data = (unsigned char *)&reg;
  1073. if (msdc_autok_read(host, SDIO_IP_WTMCR, LTE_MODEM_FUNC, (void *)data, 1, CMD_52) !=
  1074. 0) {
  1075. res = E_RESULT_CMD_CRC;
  1076. goto end;
  1077. }
  1078. if (msdc_autok_read
  1079. (host, SDIO_IP_WTMCR + 1, LTE_MODEM_FUNC, (void *)(data + 1), 1, CMD_52) != 0) {
  1080. res = E_RESULT_CMD_CRC;
  1081. goto end;
  1082. }
  1083. if (msdc_autok_read
  1084. (host, SDIO_IP_WTMCR + 2, LTE_MODEM_FUNC, (void *)(data + 2), 1, CMD_52) != 0) {
  1085. res = E_RESULT_CMD_CRC;
  1086. goto end;
  1087. }
  1088. if (msdc_autok_read
  1089. (host, SDIO_IP_WTMCR + 3, LTE_MODEM_FUNC, (void *)(data + 3), 1, CMD_52) != 0) {
  1090. res = E_RESULT_CMD_CRC;
  1091. goto end;
  1092. }
  1093. if ((reg & TEST_MODE_STATUS) == TEST_MODE_STATUS) {
  1094. res = E_RESULT_ERR;
  1095. goto end;
  1096. }
  1097. }
  1098. end:
  1099. return res;
  1100. }
  1101. static E_RESULT_TYPE autok_read_test(struct msdc_host *host)
  1102. {
  1103. int i;
  1104. E_RESULT_TYPE res = E_RESULT_PASS;
  1105. unsigned char *data;
  1106. /*use test mode to test read */
  1107. for (i = 0; i < TUNING_DATA_NO; i++) {
  1108. data = (unsigned char *)&tuning_data[i];
  1109. if (msdc_autok_write
  1110. (host, SDIO_IP_WTMDPCR0, LTE_MODEM_FUNC, (void *)data, 1, CMD_52) != 0) {
  1111. res = E_RESULT_CMD_CRC;
  1112. goto end;
  1113. }
  1114. if (msdc_autok_write
  1115. (host, SDIO_IP_WTMDPCR0 + 1, LTE_MODEM_FUNC, (void *)(data + 1), 1,
  1116. CMD_52) != 0) {
  1117. res = E_RESULT_CMD_CRC;
  1118. goto end;
  1119. }
  1120. if (msdc_autok_write
  1121. (host, SDIO_IP_WTMDPCR0 + 2, LTE_MODEM_FUNC, (void *)(data + 2), 1,
  1122. CMD_52) != 0) {
  1123. res = E_RESULT_CMD_CRC;
  1124. goto end;
  1125. }
  1126. if (msdc_autok_write
  1127. (host, SDIO_IP_WTMDPCR0 + 3, LTE_MODEM_FUNC, (void *)(data + 3), 1,
  1128. CMD_52) != 0) {
  1129. res = E_RESULT_CMD_CRC;
  1130. goto end;
  1131. }
  1132. if (msdc_autok_read
  1133. (host, SDIO_IP_WTMDR, LTE_MODEM_FUNC, (void *)g_test_read_pattern,
  1134. (4 * TUNING_TEST_TIME), CMD_53) != 0) {
  1135. res = errMapping(host);
  1136. goto end;
  1137. }
  1138. if (memcmp
  1139. (g_test_read_pattern, &g_test_write_pattern[i * TUNING_TEST_TIME],
  1140. 4 * TUNING_TEST_TIME) != 0) {
  1141. res = E_RESULT_CMP_ERR;
  1142. pr_err("[%s] E_RESULT_CMP_ERR\n", __func__);
  1143. goto end;
  1144. }
  1145. }
  1146. end:
  1147. return res;
  1148. }
  1149. static E_RESULT_TYPE autok_cmd_test(struct msdc_host *host)
  1150. {
  1151. int i;
  1152. E_RESULT_TYPE res = E_RESULT_PASS;
  1153. unsigned char *data;
  1154. #if 0
  1155. /*use test mode to test read */
  1156. for (i = 0; i < TUNING_DATA_NO; i++) {
  1157. data = (unsigned char *)&tuning_data[i];
  1158. if (msdc_autok_write
  1159. (host, SDIO_IP_WTMDPCR0, LTE_MODEM_FUNC, (void *)data, 1, CMD_52) != 0) {
  1160. res = E_RESULT_CMD_CRC;
  1161. goto end;
  1162. }
  1163. if (msdc_autok_write
  1164. (host, SDIO_IP_WTMDPCR0 + 1, LTE_MODEM_FUNC, (void *)(data + 1), 1,
  1165. CMD_52) != 0) {
  1166. res = E_RESULT_CMD_CRC;
  1167. goto end;
  1168. }
  1169. if (msdc_autok_write
  1170. (host, SDIO_IP_WTMDPCR0 + 2, LTE_MODEM_FUNC, (void *)(data + 2), 1,
  1171. CMD_52) != 0) {
  1172. res = E_RESULT_CMD_CRC;
  1173. goto end;
  1174. }
  1175. if (msdc_autok_write
  1176. (host, SDIO_IP_WTMDPCR0 + 3, LTE_MODEM_FUNC, (void *)(data + 3), 1,
  1177. CMD_52) != 0) {
  1178. res = E_RESULT_CMD_CRC;
  1179. goto end;
  1180. }
  1181. data = (unsigned char *)g_test_read_pattern;
  1182. if (msdc_autok_read(host, SDIO_IP_WTMDPCR0, LTE_MODEM_FUNC, (void *)data, 1, CMD_52)
  1183. != 0) {
  1184. res = E_RESULT_CMD_CRC;
  1185. goto end;
  1186. }
  1187. if (msdc_autok_read
  1188. (host, SDIO_IP_WTMDPCR0 + 1, LTE_MODEM_FUNC, (void *)(data + 1), 1,
  1189. CMD_52) != 0) {
  1190. res = E_RESULT_CMD_CRC;
  1191. goto end;
  1192. }
  1193. if (msdc_autok_read
  1194. (host, SDIO_IP_WTMDPCR0 + 2, LTE_MODEM_FUNC, (void *)(data + 2), 1,
  1195. CMD_52) != 0) {
  1196. res = E_RESULT_CMD_CRC;
  1197. goto end;
  1198. }
  1199. if (msdc_autok_read
  1200. (host, SDIO_IP_WTMDPCR0 + 3, LTE_MODEM_FUNC, (void *)(data + 3), 1,
  1201. CMD_52) != 0) {
  1202. res = E_RESULT_CMD_CRC;
  1203. goto end;
  1204. }
  1205. if (g_test_read_pattern[0] != tuning_data[i]) {
  1206. #ifdef AUTOK_DEBUG
  1207. pr_debug("write: 0x%x read: 0x%x\r\n", tuning_data[i],
  1208. g_test_read_pattern[0]);
  1209. #endif
  1210. res = E_RESULT_CMP_ERR;
  1211. goto end;
  1212. }
  1213. }
  1214. #else
  1215. /*use test mode to test CMD */
  1216. for (i = 0; i < TUNING_CMD_NO; i += 4) {
  1217. data = (unsigned char *)&tuning_cmd[i];
  1218. if (msdc_autok_write
  1219. (host, SDIO_IP_WTMDPCR0, LTE_MODEM_FUNC, (void *)data, 1, CMD_52) != 0) {
  1220. res = E_RESULT_CMD_CRC;
  1221. goto end;
  1222. }
  1223. if (msdc_autok_write
  1224. (host, SDIO_IP_WTMDPCR0 + 1, LTE_MODEM_FUNC, (void *)(data + 1), 1,
  1225. CMD_52) != 0) {
  1226. res = E_RESULT_CMD_CRC;
  1227. goto end;
  1228. }
  1229. if (msdc_autok_write
  1230. (host, SDIO_IP_WTMDPCR0 + 2, LTE_MODEM_FUNC, (void *)(data + 2), 1,
  1231. CMD_52) != 0) {
  1232. res = E_RESULT_CMD_CRC;
  1233. goto end;
  1234. }
  1235. if (msdc_autok_write
  1236. (host, SDIO_IP_WTMDPCR0 + 3, LTE_MODEM_FUNC, (void *)(data + 3), 1,
  1237. CMD_52) != 0) {
  1238. res = E_RESULT_CMD_CRC;
  1239. goto end;
  1240. }
  1241. }
  1242. #endif
  1243. end:
  1244. return res;
  1245. }
  1246. static int autok_recovery(struct msdc_host *host)
  1247. {
  1248. /*TODO need to do some SW recovery for next test */
  1249. #if 0
  1250. MSDC_RESET();
  1251. MSDC_CLR_FIFO();
  1252. MSDC_WRITE32(MSDC_INT, MSDC_READ32(MSDC_INT));
  1253. #endif
  1254. return 0;
  1255. }
  1256. static void autok_select_range(unsigned int result, unsigned int *sel)
  1257. {
  1258. unsigned char start = 0;
  1259. unsigned char end = 0; /* we need ten 0. */
  1260. unsigned char bit = 0;
  1261. unsigned char max_start = 0;
  1262. unsigned char max_end = 0;
  1263. unsigned char max_score = 0;
  1264. /* maybe result is 0 */
  1265. if (result == 0) {
  1266. start = 0;
  1267. end = 31;
  1268. goto end;
  1269. }
  1270. find:
  1271. start = end = 0;
  1272. while (bit < 32) {
  1273. if (result & (1 << bit)) { /* failed */
  1274. bit++;
  1275. continue;
  1276. }
  1277. start = end = bit;
  1278. bit++;
  1279. break;
  1280. }
  1281. while (bit < 32) {
  1282. if (result & (1 << bit)) { /* failed */
  1283. bit++;
  1284. if ((end - start) > max_score) {
  1285. max_score = end - start;
  1286. max_start = start;
  1287. max_end = end;
  1288. }
  1289. goto find;
  1290. } else {
  1291. end = bit;
  1292. bit++;
  1293. }
  1294. }
  1295. end:
  1296. if ((end - start) > max_score) {
  1297. max_score = end - start;
  1298. max_start = start;
  1299. max_end = end;
  1300. }
  1301. AUTOK_PRINT("score<%d> choose bit<%d> from<0x%x>\r\n", (max_score + 1),
  1302. (max_end + max_start) / 2, result);
  1303. *sel = (max_end + max_start) / 2;
  1304. }
  1305. #if 0
  1306. static unsigned int autok_accum_score(unsigned int result)
  1307. {
  1308. unsigned int num = 0;
  1309. #if 0
  1310. unsigned int bit = 0;
  1311. /* maybe result is 0 */
  1312. if (0 == result)
  1313. return 32;
  1314. if (0xFFFFFFFF == result)
  1315. return 0;
  1316. /* calc continue zero number */
  1317. while (bit < 32) {
  1318. if (result & (1 << bit)) { /* failed */
  1319. bit++;
  1320. continue;
  1321. }
  1322. bit++;
  1323. num++;
  1324. }
  1325. #endif
  1326. return num;
  1327. }
  1328. #endif
  1329. static int autok_simple_score(unsigned int result)
  1330. {
  1331. unsigned int bit = 0;
  1332. unsigned int num = 0;
  1333. unsigned int old = 0;
  1334. /* maybe result is 0 */
  1335. if (0 == result) {
  1336. strcpy(g_tune_result_str, "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
  1337. return 32;
  1338. }
  1339. if (0xFFFFFFFF == result) {
  1340. strcpy(g_tune_result_str, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  1341. return 0;
  1342. }
  1343. /* calc continue zero number */
  1344. while (bit < 32) {
  1345. if (result & (1 << bit)) { /* failed */
  1346. g_tune_result_str[bit] = 'X';
  1347. bit++;
  1348. if (old < num)
  1349. old = num;
  1350. num = 0;
  1351. continue;
  1352. }
  1353. g_tune_result_str[bit] = 'O';
  1354. bit++;
  1355. num++;
  1356. }
  1357. if (num > old)
  1358. old = num;
  1359. return old;
  1360. }
  1361. #ifdef AUTOK_CYC_ALG_0
  1362. #ifdef AUTOK_CMD_TUNE_LEGACY
  1363. static int
  1364. autok_check_score(unsigned int result,
  1365. unsigned int *pNumOfzero,
  1366. unsigned int *pFrtPosErr, unsigned int *pPeriod, unsigned int minPadPerCycle)
  1367. {
  1368. unsigned int bit = 0;
  1369. unsigned int num = 0;
  1370. unsigned int old = 0;
  1371. unsigned int frstErrS = 0, sndErrS = 0, frstErrE = 0, sndErrE = 0;
  1372. E_AUTOK_PERIOD_STA sta = PERIOD_NONE;
  1373. *pNumOfzero = 0;
  1374. *pFrtPosErr = 0;
  1375. /* maybe result is 0 */
  1376. if (0 == result) {
  1377. strcpy(g_tune_result_str, "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
  1378. *pNumOfzero = 32;
  1379. *pFrtPosErr = 32;
  1380. return 32;
  1381. }
  1382. if (0xFFFFFFFF == result) {
  1383. strcpy(g_tune_result_str, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  1384. return 0;
  1385. }
  1386. /* calc continue zero number */
  1387. while (bit < 32) {
  1388. if (result & (1 << bit)) { /* failed */
  1389. g_tune_result_str[bit] = 'X';
  1390. switch (sta) {
  1391. case PERIOD_NONE:
  1392. sta = PERIOD_L_FIRST_POS;
  1393. *pFrtPosErr = bit;
  1394. break;
  1395. case PERIOD_L_FIRST_POS:
  1396. frstErrE = bit;
  1397. break;
  1398. case PERIOD_L_FIRST_POS_DONE:
  1399. sta = PERIOD_L_SECOND_POS;
  1400. sndErrS = bit;
  1401. break;
  1402. case PERIOD_L_SECOND_POS:
  1403. sndErrE = bit;
  1404. break;
  1405. case PERIOD_F_FIRST_POS:
  1406. sta = PERIOD_F_FIRST_POS_DONE;
  1407. frstErrS = bit;
  1408. *pFrtPosErr = bit;
  1409. break;
  1410. case PERIOD_F_SECOND_POS:
  1411. sta = PERIOD_DONE_2;
  1412. sndErrS = bit;
  1413. break;
  1414. default:
  1415. break;
  1416. }
  1417. bit++;
  1418. if (num > old)
  1419. old = num;
  1420. num = 0;
  1421. continue;
  1422. }
  1423. g_tune_result_str[bit] = 'O';
  1424. bit++;
  1425. num++;
  1426. *pNumOfzero = *pNumOfzero + 1;
  1427. switch (sta) {
  1428. case PERIOD_NONE:
  1429. sta = PERIOD_F_FIRST_POS;
  1430. break;
  1431. case PERIOD_F_FIRST_POS_DONE:
  1432. sta = PERIOD_F_SECOND_POS;
  1433. break;
  1434. case PERIOD_L_FIRST_POS:
  1435. sta = PERIOD_L_FIRST_POS_DONE;
  1436. frstErrE = bit - 1;
  1437. break;
  1438. case PERIOD_L_SECOND_POS:
  1439. sndErrE = bit - 1;
  1440. sta = PERIOD_DONE;
  1441. break;
  1442. default:
  1443. break;
  1444. }
  1445. }
  1446. if (num > old)
  1447. old = num;
  1448. if (sta == PERIOD_DONE)
  1449. *pPeriod = sndErrE - frstErrE;
  1450. else if (sta == PERIOD_DONE_2)
  1451. *pPeriod = sndErrS - frstErrS;
  1452. else
  1453. *pPeriod = 0;
  1454. if ((*pPeriod < minPadPerCycle) || (*pPeriod > 32))
  1455. *pPeriod = 0;
  1456. return old;
  1457. }
  1458. #endif
  1459. #endif
  1460. static void autok_check_rawd_style(P_AUTOK_RAWD_SCAN_T prAutok_raw_scan, unsigned char isRDat)
  1461. {
  1462. unsigned int bit;
  1463. unsigned char fInvalidCKGEN = 0;
  1464. unsigned int filter = 2;
  1465. AUTOK_RAWD_SCAN_STA_E RawScanSta = RD_SCAN_NONE;
  1466. for (bit = 0; bit < 32; bit++) {
  1467. if (prAutok_raw_scan->RawData & (1 << bit)) {
  1468. switch (RawScanSta) {
  1469. case RD_SCAN_NONE:
  1470. RawScanSta = RD_SCAN_PAD_BOUND_S;
  1471. prAutok_raw_scan->BoundReg1_S = 0;
  1472. prAutok_raw_scan->Reg1Cnt++;
  1473. break;
  1474. case RD_SCAN_PAD_MARGIN:
  1475. RawScanSta = RD_SCAN_PAD_BOUND_S;
  1476. prAutok_raw_scan->BoundReg1_S = bit;
  1477. prAutok_raw_scan->Reg1Cnt++;
  1478. break;
  1479. case RD_SCAN_PAD_BOUND_E:
  1480. if (filter) {
  1481. if (/*(prAutok_raw_scan->Reg1Cnt <= AUTOK_TUNING_INACCURACY) && */
  1482. ((bit - prAutok_raw_scan->BoundReg1_E) <=
  1483. AUTOK_TUNING_INACCURACY)) {
  1484. AUTOK_PRINT
  1485. ("[W] Try to filter the holes on raw data when CKGEN=%d\r\n",
  1486. prAutok_raw_scan->CurCKGEN);
  1487. RawScanSta = RD_SCAN_PAD_BOUND_S;
  1488. prAutok_raw_scan->Reg1Cnt += 2;
  1489. prAutok_raw_scan->BoundReg1_E = 0;
  1490. prAutok_raw_scan->BoundReg2_S = 0;
  1491. filter--;
  1492. } else {
  1493. RawScanSta = RD_SCAN_PAD_BOUND_S_2;
  1494. prAutok_raw_scan->BoundReg2_S = bit;
  1495. prAutok_raw_scan->Reg2Cnt++;
  1496. }
  1497. } else {
  1498. RawScanSta = RD_SCAN_PAD_BOUND_S_2;
  1499. prAutok_raw_scan->BoundReg2_S = bit;
  1500. prAutok_raw_scan->Reg2Cnt++;
  1501. }
  1502. break;
  1503. /* We do NOT hope to see the 3rd boundary region */
  1504. case RD_SCAN_PAD_BOUND_E_2:
  1505. /*
  1506. * Before we confirm the thing, just filter the fail
  1507. * point that might be cause by accident
  1508. */
  1509. if (filter) {
  1510. filter--;
  1511. RawScanSta = RD_SCAN_PAD_BOUND_S_2;
  1512. /* In case of the hole locates near region2 */
  1513. if ((bit - prAutok_raw_scan->BoundReg2_E) <= 2) {
  1514. if ((bit - prAutok_raw_scan->BoundReg2_E) >= 1)
  1515. prAutok_raw_scan->Reg2Cnt +=
  1516. (bit - prAutok_raw_scan->BoundReg2_E);
  1517. prAutok_raw_scan->BoundReg2_E = 0;
  1518. }
  1519. /* In case of the hole locates near region1 */
  1520. else if ((prAutok_raw_scan->BoundReg2_S -
  1521. prAutok_raw_scan->BoundReg1_E) <= 2) {
  1522. /* Update count of region1 */
  1523. if ((prAutok_raw_scan->BoundReg2_S -
  1524. prAutok_raw_scan->BoundReg1_E) >= 1)
  1525. prAutok_raw_scan->Reg1Cnt +=
  1526. (prAutok_raw_scan->BoundReg2_E -
  1527. prAutok_raw_scan->BoundReg1_E);
  1528. /* Update region1 */
  1529. prAutok_raw_scan->BoundReg1_E =
  1530. prAutok_raw_scan->BoundReg2_E;
  1531. /* Update region2 */
  1532. prAutok_raw_scan->BoundReg2_S = bit;
  1533. prAutok_raw_scan->BoundReg2_E = 0;
  1534. prAutok_raw_scan->Reg2Cnt = 1;
  1535. } else {
  1536. AUTOK_PRINT
  1537. ("[ERR] Find hole on raw data @ CKGEN=%d, but can NOT filter!\r\n",
  1538. prAutok_raw_scan->CurCKGEN);
  1539. fInvalidCKGEN = 1;
  1540. goto exit;
  1541. }
  1542. AUTOK_PRINT
  1543. ("[W] Try to filter the holes on raw data when CKGEN=%d\r\n",
  1544. prAutok_raw_scan->CurCKGEN);
  1545. } else {
  1546. AUTOK_PRINT
  1547. ("[W] Find too much fail regions when CKGEN=%d(Invalid)\r\n",
  1548. prAutok_raw_scan->CurCKGEN);
  1549. fInvalidCKGEN = 1;
  1550. goto exit;
  1551. }
  1552. break;
  1553. case RD_SCAN_PAD_BOUND_S:
  1554. prAutok_raw_scan->Reg1Cnt++;
  1555. break;
  1556. case RD_SCAN_PAD_BOUND_S_2:
  1557. prAutok_raw_scan->Reg2Cnt++;
  1558. break;
  1559. default:
  1560. break;
  1561. }
  1562. } else {
  1563. switch (RawScanSta) {
  1564. case RD_SCAN_NONE:
  1565. RawScanSta = RD_SCAN_PAD_MARGIN;
  1566. break;
  1567. case RD_SCAN_PAD_BOUND_S:
  1568. RawScanSta = RD_SCAN_PAD_BOUND_E;
  1569. prAutok_raw_scan->BoundReg1_E = bit - 1;
  1570. break;
  1571. case RD_SCAN_PAD_BOUND_S_2:
  1572. RawScanSta = RD_SCAN_PAD_BOUND_E_2;
  1573. prAutok_raw_scan->BoundReg2_E = bit - 1;
  1574. break;
  1575. case RD_SCAN_PAD_MARGIN:
  1576. case RD_SCAN_PAD_BOUND_E:
  1577. case RD_SCAN_PAD_BOUND_E_2:
  1578. default:
  1579. break;
  1580. }
  1581. }
  1582. }
  1583. /*
  1584. * Another abnormal case, found 2 regions,
  1585. * but they are too close, maybe it would NOT happen
  1586. */
  1587. if (isRDat) {
  1588. /*
  1589. if (prAutok_raw_scan->Reg1Cnt && prAutok_raw_scan->BoundReg2_E) {
  1590. if ((prAutok_raw_scan->BoundReg2_S - prAutok_raw_scan->BoundReg1_E) > 1) {
  1591. fInvalidCKGEN = 1;
  1592. goto exit;
  1593. }
  1594. else if ((prAutok_raw_scan->BoundReg2_E - prAutok_raw_scan->BoundReg1_S) < 15) {
  1595. prAutok_raw_scan->BoundReg2_S = 0;
  1596. prAutok_raw_scan->BoundReg1_E = prAutok_raw_scan->BoundReg2_E;
  1597. prAutok_raw_scan->BoundReg2_E = 0;
  1598. }
  1599. }
  1600. */
  1601. if (prAutok_raw_scan->Reg1Cnt == 32) {
  1602. fInvalidCKGEN = 1;
  1603. goto exit;
  1604. }
  1605. if ((32 - (prAutok_raw_scan->Reg1Cnt + prAutok_raw_scan->Reg2Cnt)) <=
  1606. (AUTOK_TUNING_INACCURACY + 1))
  1607. fInvalidCKGEN = 1;
  1608. }
  1609. exit:
  1610. if (fInvalidCKGEN)
  1611. prAutok_raw_scan->fInvalidCKGEN = 1;
  1612. }
  1613. #ifdef AUTOK_CYC_ALG_0
  1614. static AUTOK_CMDPAT_COMP_E
  1615. autok_comp_cmd_pat(P_AUTOK_CMDPAT_DOUBLE_CHK_T prCMDPatChk, unsigned int fDoubleChk,
  1616. int *diff, unsigned int fChkPos)
  1617. {
  1618. AUTOK_RAWD_SCAN_T CMDPadScan0, CMDPadScan1;
  1619. unsigned int FstMidErrPos0, FstMidErrPos1, SndMidErrPos0, SndMidErrPos1;
  1620. unsigned int ck0, ck1, raw0, raw1;
  1621. AUTOK_CMDPAT_COMP_E fIdent = CMDPAT_NONE;
  1622. memset(&CMDPadScan0, 0, sizeof(CMDPadScan0));
  1623. memset(&CMDPadScan1, 0, sizeof(CMDPadScan1));
  1624. ck0 = prCMDPatChk->ck_s_b4;
  1625. ck1 = prCMDPatChk->ck_e_b4;
  1626. if (fDoubleChk) {
  1627. raw0 = prCMDPatChk->raw_s_sh;
  1628. raw1 = prCMDPatChk->raw_e_sh;
  1629. } else {
  1630. raw0 = prCMDPatChk->raw_s_b4;
  1631. raw1 = prCMDPatChk->raw_e_b4;
  1632. }
  1633. CMDPadScan0.CurCKGEN = ck0;
  1634. CMDPadScan0.RawData = raw0;
  1635. autok_check_rawd_style(&CMDPadScan0, 0);
  1636. FstMidErrPos0 = CMDPadScan0.BoundReg1_S + CMDPadScan0.Reg1Cnt / 2;
  1637. CMDPadScan1.CurCKGEN = ck1;
  1638. CMDPadScan1.RawData = raw1;
  1639. autok_check_rawd_style(&CMDPadScan1, 0);
  1640. FstMidErrPos1 = CMDPadScan1.BoundReg1_S + CMDPadScan1.Reg1Cnt / 2;
  1641. /* Calculate the default differ */
  1642. *diff = (int)(FstMidErrPos1 - FstMidErrPos0);
  1643. CommonChk:
  1644. /* In case of first time check */
  1645. if (!fDoubleChk) {
  1646. /* Firstly, check if shift is identical */
  1647. if (fChkPos)
  1648. if (ABS_DIFF(FstMidErrPos0, FstMidErrPos1) > (AUTOK_TUNING_INACCURACY + 1))
  1649. goto exit;
  1650. /* Then, Check the pattern style */
  1651. /* In case of both have 2 fail regions */
  1652. if (CMDPadScan0.Reg1Cnt && CMDPadScan0.Reg2Cnt &&
  1653. CMDPadScan1.Reg1Cnt && CMDPadScan1.Reg2Cnt) {
  1654. SndMidErrPos0 = CMDPadScan0.BoundReg2_S + CMDPadScan0.Reg2Cnt / 2;
  1655. SndMidErrPos1 = CMDPadScan1.BoundReg2_S + CMDPadScan1.Reg2Cnt / 2;
  1656. AUTOK_PRINT
  1657. ("CKGEN[%d].MidErrPos=%d, CKGEN[%d].MidErrPos=%d, Reg_Gap=%d\r\n", ck0,
  1658. FstMidErrPos0, ck1, FstMidErrPos1,
  1659. ABS_DIFF((SndMidErrPos0 - FstMidErrPos0),
  1660. (SndMidErrPos1 - FstMidErrPos1)));
  1661. /* Do NOT judge the interval */
  1662. #if 0
  1663. if (3 <
  1664. ABS_DIFF((SndMidErrPos0 - FstMidErrPos0),
  1665. (SndMidErrPos1 - FstMidErrPos1)))
  1666. goto exit;
  1667. #endif
  1668. }
  1669. /* In case of only one have 2 fail region */
  1670. else {
  1671. if ((CMDPadScan1.Reg2Cnt && !CMDPadScan0.Reg2Cnt) ||
  1672. (!CMDPadScan1.Reg2Cnt && CMDPadScan0.Reg2Cnt)) {
  1673. /* Return half indentical if position check is passed */
  1674. if (fChkPos) {
  1675. if (CMDPadScan1.Reg2Cnt) {
  1676. /*
  1677. * For MT6582 specific consideration,
  1678. * the margin should remain more
  1679. */
  1680. #if 0
  1681. /*
  1682. if ((CMDPadScan1.BoundReg2_S -
  1683. CMDPadScan1.BoundReg1_E) <=
  1684. (AUTOK_TUNING_INACCURACY + 2))
  1685. fIdent = CMDPAT_HALF_IDENTICAL;
  1686. else
  1687. fIdent = CMDPAT_NONE;
  1688. */
  1689. #else
  1690. /* Only consider the fisrt fail region */
  1691. fIdent = CMDPAT_HALF_IDENTICAL;
  1692. #endif
  1693. }
  1694. if (CMDPadScan0.Reg2Cnt) {
  1695. /*
  1696. * For MT6582 specific consideration,
  1697. * the margin should remain more
  1698. */
  1699. #if 0
  1700. /*
  1701. if ((CMDPadScan0.BoundReg2_S -
  1702. CMDPadScan0.BoundReg1_E) <=
  1703. (AUTOK_TUNING_INACCURACY + 2))
  1704. fIdent = CMDPAT_HALF_IDENTICAL;
  1705. else
  1706. fIdent = CMDPAT_NONE;
  1707. */
  1708. #else
  1709. /* Only consider the fisrt fail region */
  1710. fIdent = CMDPAT_HALF_IDENTICAL;
  1711. #endif
  1712. }
  1713. }
  1714. goto exit;
  1715. }
  1716. }
  1717. }
  1718. /* In case of double check */
  1719. else {
  1720. switch (prCMDPatChk->scen) {
  1721. case CMDPAT_REG_1_L:
  1722. /*
  1723. * 1. Only Int. boundary, ck0 and ck1 all shift out, compare
  1724. * middle point only
  1725. * 2. Only Int. boundary, ck0 shift out, but ck1 NOT shift out
  1726. * compare middle point only
  1727. * 3. Only Int. bouddary, ck0 and ck1 all shift out, but ck0 roll
  1728. * back from right, can be double checked
  1729. * 4. Int. and Ext. boundary together, Ext. stay there, compare
  1730. * middle point only
  1731. */
  1732. if (ABS_DIFF(FstMidErrPos0, FstMidErrPos1) > (AUTOK_TUNING_INACCURACY + 1)) {
  1733. /* ck0 roll back from right */
  1734. if (ABS_DIFF(FstMidErrPos0, FstMidErrPos1) < 25)
  1735. goto exit;
  1736. else {
  1737. AUTOK_PRINT("ck0 roll back from right!\r\n");
  1738. /* Use the differ before */
  1739. *diff = (int)(prCMDPatChk->diff_b4);
  1740. }
  1741. }
  1742. break;
  1743. case CMDPAT_REG_2_L:
  1744. /*
  1745. * There must be one region stay there
  1746. * 1. Int. boundary near left, ck0 and ck1 NOT shift out,
  1747. * only compare middle point
  1748. * 2. Int. boundary near left, ck0 shift out, ck1 NOT, can
  1749. * be double checked
  1750. * 3. Ext. boundary near left, only compare middle point
  1751. * 4. Ext. boundary near left, but the shift scale is too small
  1752. */
  1753. if (ABS_DIFF(FstMidErrPos0, FstMidErrPos1) > (AUTOK_TUNING_INACCURACY + 1)) {
  1754. /* Only Int. boundary of ck0 shift out, ck1 NOT */
  1755. if (CMDPadScan0.Reg1Cnt && !CMDPadScan0.Reg2Cnt &&
  1756. CMDPadScan1.Reg1Cnt && CMDPadScan1.Reg2Cnt &&
  1757. (CMDPadScan0.BoundReg1_S > AUTOK_CMDPAT_CHK_SHIFT)) {
  1758. SndMidErrPos1 =
  1759. CMDPadScan1.BoundReg2_S + CMDPadScan1.Reg2Cnt / 2;
  1760. if (ABS_DIFF(FstMidErrPos0, SndMidErrPos1) >
  1761. (AUTOK_TUNING_INACCURACY + 1))
  1762. goto exit;
  1763. else {
  1764. AUTOK_PRINT
  1765. ("Int. boundary of ck0 shift out, ck1 stay here!\r\n");
  1766. *diff = (int)(SndMidErrPos1 - FstMidErrPos0);
  1767. }
  1768. }
  1769. /* Both 1 fail region, but NOT indentical */
  1770. else if (CMDPadScan0.Reg1Cnt && !CMDPadScan0.Reg2Cnt &&
  1771. CMDPadScan1.Reg1Cnt && !CMDPadScan1.Reg2Cnt)
  1772. goto exit;
  1773. }
  1774. break;
  1775. default:
  1776. fDoubleChk = 0;
  1777. goto CommonChk;
  1778. break;
  1779. }
  1780. }
  1781. fIdent = CMDPAT_IDENTICAL;
  1782. exit:
  1783. if (fIdent != CMDPAT_NONE) {
  1784. AUTOK_PRINT("CKGEN[%d].MidErrPos=%d, CKGEN[%d].MidErrPos=%d, Diff=%d\r\n",
  1785. ck0, FstMidErrPos0, ck1, FstMidErrPos1, *diff);
  1786. /* To determine the internal delay shift at the first check */
  1787. if (!fDoubleChk) {
  1788. /* Default value */
  1789. prCMDPatChk->cmd_int_shift = AUTOK_CMDPAT_CHK_SHIFT;
  1790. /* In case of only 1 fail region */
  1791. if (CMDPadScan0.Reg1Cnt && !CMDPadScan0.Reg2Cnt &&
  1792. CMDPadScan1.Reg1Cnt && !CMDPadScan1.Reg2Cnt) {
  1793. if (CMDPadScan0.BoundReg1_S < AUTOK_CMDPAT_CHK_SHIFT) {
  1794. /* Choose the larger one */
  1795. prCMDPatChk->cmd_int_shift = CMDPadScan1.BoundReg1_E + 1;
  1796. prCMDPatChk->scen = CMDPAT_REG_1_L;
  1797. AUTOK_PRINT("1 Fail Region, Near left boundary\r\n");
  1798. }
  1799. }
  1800. /* In case of 2 fail regions */
  1801. if (CMDPadScan0.Reg1Cnt && CMDPadScan0.Reg2Cnt) {
  1802. if (CMDPadScan0.BoundReg1_S < AUTOK_CMDPAT_CHK_SHIFT) {
  1803. AUTOK_PRINT("2 Fail Regions, Near left boundary\r\n");
  1804. prCMDPatChk->scen = CMDPAT_REG_2_L;
  1805. /* If the two region are nearby */
  1806. if ((CMDPadScan0.BoundReg2_S - CMDPadScan0.BoundReg1_E) <=
  1807. (AUTOK_CMDPAT_CHK_SHIFT + 1))
  1808. prCMDPatChk->cmd_int_shift =
  1809. CMDPadScan0.BoundReg2_E;
  1810. }
  1811. /* If the two region are nearby */
  1812. else if ((CMDPadScan0.BoundReg2_S - CMDPadScan0.BoundReg1_E) <=
  1813. (AUTOK_CMDPAT_CHK_SHIFT + 1)) {
  1814. AUTOK_PRINT("2 Fail Regions, nearby each other\r\n");
  1815. prCMDPatChk->cmd_int_shift = CMDPadScan0.BoundReg2_S -
  1816. CMDPadScan0.BoundReg1_S;
  1817. }
  1818. }
  1819. }
  1820. }
  1821. return fIdent;
  1822. }
  1823. #endif
  1824. #if !defined(AUTOK_CMD_TUNE_LEGACY)
  1825. static void
  1826. autok_check_cmd_matrix(unsigned int *pMatrixRaw,
  1827. unsigned int PadDlyNum,
  1828. unsigned int IntDlyNum, P_AUTOK_CMDMAT_CHAR_POS prMatChar)
  1829. {
  1830. unsigned int pad_idx, int_idx;
  1831. unsigned int PadDlyScore, raw;
  1832. AUTOK_RAWD_SCAN_T raw_scan;
  1833. raw = 0;
  1834. for (pad_idx = 0; pad_idx < PadDlyNum; pad_idx++) {
  1835. PadDlyScore = 1;
  1836. for (int_idx = 0; int_idx < IntDlyNum; int_idx++) {
  1837. PadDlyScore &= ((pMatrixRaw[int_idx] >> pad_idx) & 0x1);
  1838. if (!PadDlyScore)
  1839. break;
  1840. }
  1841. if (PadDlyScore)
  1842. raw |= 1 << pad_idx;
  1843. }
  1844. memset(&raw_scan, 0, sizeof(raw_scan));
  1845. raw_scan.RawData = raw;
  1846. autok_simple_score(raw_scan.RawData);
  1847. AUTOK_PRINT("CMD pad mapped from martrix: %s\r\n", g_tune_result_str);
  1848. autok_check_rawd_style(&raw_scan, 1);
  1849. if (raw_scan.Reg1Cnt) {
  1850. prMatChar->pad_trans_cnt1 = raw_scan.Reg1Cnt;
  1851. prMatChar->pad_trans_s1 = raw_scan.BoundReg1_S;
  1852. prMatChar->pad_trans_e1 = raw_scan.BoundReg1_E;
  1853. prMatChar->pad_trans_m1 = raw_scan.BoundReg1_S + raw_scan.Reg1Cnt / 2;
  1854. AUTOK_PRINT("Find 1st Pad transition boundary: [%d, %d], Mid=%d\r\n",
  1855. prMatChar->pad_trans_s1, prMatChar->pad_trans_e1,
  1856. prMatChar->pad_trans_m1);
  1857. }
  1858. if (raw_scan.Reg2Cnt) {
  1859. prMatChar->pad_trans_cnt2 = raw_scan.Reg2Cnt;
  1860. prMatChar->pad_trans_s2 = raw_scan.BoundReg2_S;
  1861. prMatChar->pad_trans_e2 = raw_scan.BoundReg2_E;
  1862. prMatChar->pad_trans_m2 = raw_scan.BoundReg2_S + raw_scan.Reg2Cnt / 2;
  1863. AUTOK_PRINT("Find 2nd Pad transition boundary: [%d, %d], Mid=%d\r\n",
  1864. prMatChar->pad_trans_s2, prMatChar->pad_trans_e2,
  1865. prMatChar->pad_trans_m2);
  1866. }
  1867. if (!raw_scan.Reg1Cnt && !raw_scan.Reg2Cnt)
  1868. AUTOK_PRINT("Can NOT find pad transition boundary!\r\n");
  1869. }
  1870. #endif
  1871. #if defined(AUTOK_CYC_ALG_2)
  1872. typedef enum {
  1873. AUTOK_CYC_SCAN_INIT = 0,
  1874. AUTOK_CYC_SCAN_CHNG_EDGE,
  1875. AUTOK_CYC_SCAN_CHNG_CKGEN,
  1876. AUTOK_CYC_SCAN_STA_MAX,
  1877. } AUTOK_CYC_SCAN_STA_E;
  1878. #if defined(MT6582LTE)
  1879. #define AUTOK_TRANS_BOUND_RISING_TH 6 /* 6/2 */
  1880. #define AUTOK_TRANS_BOUND_FALLING_TH 6 /* 6/2 */
  1881. #elif defined(MT6592LTE)
  1882. #define AUTOK_TRANS_BOUND_RISING_TH 2 /* 2/2 */
  1883. #define AUTOK_TRANS_BOUND_FALLING_TH 2 /* 2/2 */
  1884. #elif defined(MT6595WIFI)
  1885. #define AUTOK_TRANS_BOUND_RISING_TH 3 /* 3/2 */
  1886. #define AUTOK_TRANS_BOUND_FALLING_TH 3 /* 3/2 */
  1887. #elif defined(MT6752WIFI)
  1888. #define AUTOK_TRANS_BOUND_RISING_TH 3 /* 3/2 */
  1889. #define AUTOK_TRANS_BOUND_FALLING_TH 3 /* 3/2 */
  1890. #elif defined(MT6735WIFI)
  1891. #define AUTOK_TRANS_BOUND_RISING_TH 3 /* 3/2 */
  1892. #define AUTOK_TRANS_BOUND_FALLING_TH 3 /* 3/2 */
  1893. #endif
  1894. #define AUTOK_TRANS_BOUND_RISING autok_trans_bound_rising
  1895. #define AUTOK_TRANS_BOUND_FALLING autok_trans_bound_falling
  1896. static int autok_trans_bound_rising = AUTOK_TRANS_BOUND_RISING_TH;
  1897. static int autok_trans_bound_falling = AUTOK_TRANS_BOUND_FALLING_TH;
  1898. #define autok_calc_cycle(cyc, char_s, char_e, s_num, e_num, isTotalT, isSRising) \
  1899. { \
  1900. if (isTotalT) { \
  1901. /* Exact 1T */ \
  1902. if (char_s.pad_trans_s##s_num && char_e.pad_trans_e##e_num) \
  1903. cyc = ABS_DIFF(char_e.pad_trans_m##e_num, char_s.pad_trans_m##s_num); \
  1904. /* End full boundary, Start half boundary */ \
  1905. else if (!char_s.pad_trans_s##s_num && char_e.pad_trans_e##e_num) \
  1906. cyc = ABS_DIFF(char_e.pad_trans_e##e_num, char_s.pad_trans_e##s_num); \
  1907. /* Start full boundary, End half boundary */ \
  1908. else if (char_s.pad_trans_s##s_num && !char_e.pad_trans_e##e_num) \
  1909. cyc = ABS_DIFF(char_e.pad_trans_s##e_num, char_s.pad_trans_s##s_num); \
  1910. /* Estimated 1T */ \
  1911. else { \
  1912. if (isSRising) \
  1913. cyc = ABS_DIFF(char_s.pad_trans_e##s_num, \
  1914. (char_e.pad_trans_s##e_num + AUTOK_TRANS_BOUND_RISING - 1)); \
  1915. else \
  1916. cyc = ABS_DIFF(char_s.pad_trans_e##s_num, \
  1917. (char_e.pad_trans_s##e_num + AUTOK_TRANS_BOUND_FALLING - 1)); \
  1918. } \
  1919. } \
  1920. else { \
  1921. /* Exact T/2 */ \
  1922. if (char_s.pad_trans_s##s_num && char_e.pad_trans_e##e_num) \
  1923. cyc = ABS_DIFF(char_e.pad_trans_m##e_num, char_s.pad_trans_m##s_num); \
  1924. else if (!char_s.pad_trans_s##s_num && char_e.pad_trans_e##e_num) { \
  1925. /* End full boundary, Start half boundary */ \
  1926. if (isSRising) { \
  1927. if (char_e.pad_trans_cnt##e_num < AUTOK_TRANS_BOUND_FALLING_TH) \
  1928. cyc = ABS_DIFF(char_e.pad_trans_s##e_num, \
  1929. (char_s.pad_trans_e##s_num - AUTOK_TRANS_BOUND_RISING + 1)); \
  1930. else \
  1931. cyc = ABS_DIFF(char_e.pad_trans_e##e_num, \
  1932. char_s.pad_trans_e##s_num); \
  1933. } \
  1934. else { \
  1935. if (char_e.pad_trans_cnt##e_num < AUTOK_TRANS_BOUND_RISING_TH) \
  1936. cyc = ABS_DIFF(char_e.pad_trans_s##e_num, \
  1937. (char_s.pad_trans_e##s_num - AUTOK_TRANS_BOUND_FALLING + 1)); \
  1938. else \
  1939. cyc = ABS_DIFF(char_e.pad_trans_e##e_num, \
  1940. char_s.pad_trans_e##s_num); \
  1941. } \
  1942. } \
  1943. else if (char_s.pad_trans_s##s_num && !char_e.pad_trans_e##e_num) { \
  1944. /* Start full boundary, End half boundary */ \
  1945. if (isSRising) \
  1946. cyc = ABS_DIFF(char_e.pad_trans_s##e_num, \
  1947. char_s.pad_trans_s##s_num); \
  1948. else \
  1949. cyc = ABS_DIFF(char_e.pad_trans_s##e_num, \
  1950. char_s.pad_trans_s##s_num); \
  1951. } \
  1952. /* Estimated T/2 */ \
  1953. else { \
  1954. if (isSRising) \
  1955. cyc = ABS_DIFF(char_e.pad_trans_s##e_num, \
  1956. (char_s.pad_trans_e##s_num - AUTOK_TRANS_BOUND_RISING + 1)); \
  1957. else \
  1958. cyc = ABS_DIFF(char_e.pad_trans_s##e_num, \
  1959. (char_s.pad_trans_e##s_num - AUTOK_TRANS_BOUND_FALLING + 1)); \
  1960. } \
  1961. } \
  1962. }
  1963. static AUTOK_CYC_SCAN_RES_T
  1964. autok_cycle_scan(struct msdc_host *host, U_AUTOK_INTERFACE_DATA *pAutoKData)
  1965. {
  1966. AUTOK_CYC_SCAN_RES_T rPadDlyRes;
  1967. AUTOK_CMDMAT_CHAR_POS CMDMatChar[AUTOK_CYC_SCAN_STA_MAX];
  1968. AUTOK_CYC_SCAN_STA_E CycScanSta = AUTOK_CYC_SCAN_INIT;
  1969. AUTOK_RAWD_SCAN_T rRawScan;
  1970. E_RESULT_TYPE res;
  1971. S_AUTOK_CMD_DLY data;
  1972. int pad_delay_period_cycle = 0;
  1973. unsigned int k, x, m, n, cnt, ck_sel = 0, edge_sel, sel;
  1974. unsigned int fCMDEdgeDefault = 1;
  1975. unsigned int CMDMatRaw[SCALE_CMDMAT_RSP_DLY_SEL];
  1976. unsigned int RegCnt = 0, Start[2], End[2];
  1977. unsigned int pad_shift;
  1978. unsigned int fRdatPatFound = 0, fStopRDAT = 0, reTuneCmd, reTuneCmdCnt = 0;
  1979. /* Initialize parameters */
  1980. memset(&rPadDlyRes, 0, sizeof(rPadDlyRes));
  1981. memset(CMDMatChar, 0, sizeof(CMDMatChar));
  1982. memset(autok_rdata_scan, 0, sizeof(autok_rdata_scan));
  1983. memset(autok_cmd_cmdrrdly, 0, sizeof(autok_cmd_cmdrrdly));
  1984. memset(autok_ckg_data, 0, sizeof(autok_ckg_data));
  1985. /* Evaluate the pad delay per cycle first */
  1986. if (freq_mhz >= 200)
  1987. autok_paddly_per_cyc_eval = 64;
  1988. else if (freq_mhz >= 150)
  1989. autok_paddly_per_cyc_eval = 64;
  1990. else if (freq_mhz >= 100)
  1991. autok_paddly_per_cyc_eval = 90;
  1992. else if (freq_mhz >= 50)
  1993. autok_paddly_per_cyc_eval = 180;
  1994. AUTOK_PRINT("Estimated pad delay per cycle:%d, cur. freq:%dMHz\r\n",
  1995. autok_paddly_per_cyc_eval, freq_mhz);
  1996. ReTuneMatrix:
  1997. memset(CMDMatRaw, 0, sizeof(CMDMatRaw));
  1998. /* Select the default CKGEN and CMD edge */
  1999. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL, &ck_sel, MSDC_WRITE);
  2000. if (fCMDEdgeDefault)
  2001. edge_sel = AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  2002. else
  2003. edge_sel = ~AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  2004. msdc_autok_adjust_param(host, CMD_EDGE, &edge_sel, MSDC_WRITE);
  2005. AUTOK_PRINT("Start to scan CMD matrix(%s edge)...\r\n", edge_sel ? "falling" : "rising");
  2006. /* Matrix Scanning... */
  2007. AUTOK_PRINT("CKGEN_MSDC_DLY \t PAD_TUNE_CMDRRDLY \t PAD_TUNE_CMDRDLY \r\n");
  2008. for (x = 0; x < SCALE_CMDMAT_RSP_DLY_SEL; x++) {
  2009. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &x, MSDC_WRITE);
  2010. if (x == 0) {
  2011. for (m = 0; m < SCALE_PAD_TUNE_CMDRDLY; m++) {
  2012. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  2013. for (cnt = 0; cnt < AUTOK_CMD_TIMES; cnt++) {
  2014. if (autok_cmd_test(host) != E_RESULT_PASS) {
  2015. /* 0 means pass */
  2016. CMDMatRaw[x] |= (1 << m);
  2017. break;
  2018. }
  2019. }
  2020. }
  2021. /* Find the transition boundarys */
  2022. RegCnt = 0;
  2023. memset(&rRawScan, 0, sizeof(rRawScan));
  2024. rRawScan.RawData = CMDMatRaw[x];
  2025. autok_check_rawd_style(&rRawScan, 0);
  2026. if (rRawScan.Reg1Cnt && rRawScan.BoundReg1_S && !rRawScan.BoundReg1_E)
  2027. rRawScan.BoundReg1_E = 31;
  2028. else if (rRawScan.Reg1Cnt && rRawScan.Reg2Cnt && !rRawScan.BoundReg2_E)
  2029. rRawScan.BoundReg2_E = 31;
  2030. if (rRawScan.Reg1Cnt) {
  2031. Start[RegCnt] = rRawScan.BoundReg1_S ?
  2032. (rRawScan.BoundReg1_S - 1) : rRawScan.BoundReg1_S;
  2033. End[RegCnt] = (rRawScan.BoundReg1_E >= 31) ?
  2034. rRawScan.BoundReg1_E : (rRawScan.BoundReg1_E + 1);
  2035. RegCnt++;
  2036. }
  2037. if (rRawScan.Reg2Cnt) {
  2038. Start[RegCnt] = rRawScan.BoundReg2_S ?
  2039. (rRawScan.BoundReg2_S - 1) : rRawScan.BoundReg2_S;
  2040. if (rRawScan.fInvalidCKGEN)
  2041. End[RegCnt] = 31;
  2042. else
  2043. End[RegCnt] = (rRawScan.BoundReg2_E >= 31) ?
  2044. rRawScan.BoundReg2_E : (rRawScan.BoundReg2_E + 1);
  2045. RegCnt++;
  2046. }
  2047. AUTOK_PRINT
  2048. ("At INIT scan, RegCnt:%d, S1:%d, E1:%d, S2:%d, E2:%d, Invalid:%s\r\n",
  2049. RegCnt, Start[0], End[0], Start[1], End[1],
  2050. rRawScan.fInvalidCKGEN ? "YES" : "NO");
  2051. /* Quit the matrix scan directly */
  2052. if (!rRawScan.Reg1Cnt && !rRawScan.Reg2Cnt) {
  2053. AUTOK_PRINT
  2054. ("Can NOT find transition boundary at INIT, quit the loop!\r\n");
  2055. break;
  2056. }
  2057. } else {
  2058. for (n = 0; n < RegCnt; n++) {
  2059. for (m = Start[n]; m <= End[n]; m++) {
  2060. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  2061. for (cnt = 0; cnt < AUTOK_CMD_TIMES; cnt++) {
  2062. if (autok_cmd_test(host) != E_RESULT_PASS) {
  2063. /* 0 means pass */
  2064. CMDMatRaw[x] |= (1 << m);
  2065. break;
  2066. }
  2067. }
  2068. }
  2069. }
  2070. }
  2071. autok_simple_score(CMDMatRaw[x]);
  2072. AUTOK_PRINT("%02d \t %02d \t %s\r\n", ck_sel, x, g_tune_result_str);
  2073. }
  2074. /* Find the characteristic points */
  2075. autok_check_cmd_matrix(CMDMatRaw, SCALE_PAD_TUNE_CMDRDLY,
  2076. SCALE_CMDMAT_RSP_DLY_SEL, &CMDMatChar[CycScanSta]);
  2077. /* In case total cycle within pad delay */
  2078. if (CMDMatChar[CycScanSta].pad_trans_cnt1 && CMDMatChar[CycScanSta].pad_trans_cnt2) {
  2079. /* Calculate the cycle */
  2080. if (fCMDEdgeDefault) {
  2081. autok_calc_cycle(pad_delay_period_cycle, CMDMatChar[CycScanSta],
  2082. CMDMatChar[CycScanSta], 1, 2, 1,
  2083. AUTOK_CMD_EDGE_MATRIX_SCAN ? 0 : 1);
  2084. } else {
  2085. autok_calc_cycle(pad_delay_period_cycle, CMDMatChar[CycScanSta],
  2086. CMDMatChar[CycScanSta], 1, 2, 1,
  2087. AUTOK_CMD_EDGE_MATRIX_SCAN ? 1 : 0);
  2088. }
  2089. }
  2090. /*
  2091. * 1. Only 1 transition boundary found, or
  2092. * 2. NO transition boundary found
  2093. */
  2094. else {
  2095. switch (CycScanSta) {
  2096. case AUTOK_CYC_SCAN_INIT:
  2097. /* Just change to the other edge */
  2098. if (fCMDEdgeDefault)
  2099. fCMDEdgeDefault = 0;
  2100. /* Update the boundary length */
  2101. if (CMDMatChar[CycScanSta].pad_trans_cnt1 &&
  2102. CMDMatChar[CycScanSta].pad_trans_s1 &&
  2103. CMDMatChar[CycScanSta].pad_trans_e1) {
  2104. if (AUTOK_CMD_EDGE_MATRIX_SCAN)
  2105. autok_trans_bound_falling =
  2106. CMDMatChar[CycScanSta].pad_trans_cnt1;
  2107. else
  2108. autok_trans_bound_rising =
  2109. CMDMatChar[CycScanSta].pad_trans_cnt1;
  2110. AUTOK_PRINT("Update transition boundary len:%d(%s edge)\r\n",
  2111. (AUTOK_CMD_EDGE_MATRIX_SCAN ?
  2112. autok_trans_bound_falling : autok_trans_bound_rising),
  2113. (AUTOK_CMD_EDGE_MATRIX_SCAN ? "falling" : "rising"));
  2114. }
  2115. if (CMDMatChar[CycScanSta].pad_trans_cnt1) {
  2116. /* Get the reference for RDAT */
  2117. if (!AUTOK_CMD_EDGE_MATRIX_SCAN) {
  2118. rPadDlyRes.FBoundMidRefCMD =
  2119. CMDMatChar[CycScanSta].pad_trans_m1;
  2120. rPadDlyRes.FBoundCKRefCMD = ck_sel;
  2121. rPadDlyRes.fFBoundRefCMD = 1;
  2122. rPadDlyRes.FBoundCntRefCMD = autok_trans_bound_rising * 2;
  2123. }
  2124. }
  2125. CycScanSta = AUTOK_CYC_SCAN_CHNG_EDGE;
  2126. goto ReTuneMatrix;
  2127. case AUTOK_CYC_SCAN_CHNG_EDGE:
  2128. /* In case of only 1 transition boundary found */
  2129. if (CMDMatChar[CycScanSta].pad_trans_cnt1 &&
  2130. !CMDMatChar[CycScanSta].pad_trans_cnt2) {
  2131. /* Update the boundary length */
  2132. if (CMDMatChar[CycScanSta].pad_trans_cnt1 &&
  2133. CMDMatChar[CycScanSta].pad_trans_s1 &&
  2134. CMDMatChar[CycScanSta].pad_trans_e1) {
  2135. if (AUTOK_CMD_EDGE_MATRIX_SCAN)
  2136. autok_trans_bound_rising =
  2137. CMDMatChar[CycScanSta].pad_trans_cnt1;
  2138. else
  2139. autok_trans_bound_falling =
  2140. CMDMatChar[CycScanSta].pad_trans_cnt1;
  2141. AUTOK_PRINT
  2142. ("Update transition boundary len:%d(%s edge)\r\n",
  2143. (AUTOK_CMD_EDGE_MATRIX_SCAN ? autok_trans_bound_rising
  2144. : autok_trans_bound_falling),
  2145. (AUTOK_CMD_EDGE_MATRIX_SCAN ? "rising" : "falling"));
  2146. }
  2147. /* Get the reference for RDAT */
  2148. if (AUTOK_CMD_EDGE_MATRIX_SCAN) {
  2149. rPadDlyRes.FBoundMidRefCMD =
  2150. CMDMatChar[CycScanSta].pad_trans_m1;
  2151. rPadDlyRes.FBoundCKRefCMD = ck_sel;
  2152. rPadDlyRes.fFBoundRefCMD = 1;
  2153. rPadDlyRes.FBoundCntRefCMD = autok_trans_bound_rising * 2;
  2154. }
  2155. /* If state0 have found 1 transition boundary */
  2156. if (CMDMatChar[CycScanSta - 1].pad_trans_cnt1 &&
  2157. !CMDMatChar[CycScanSta - 1].pad_trans_cnt2) {
  2158. /* Calculate the cycle */
  2159. if (CMDMatChar[CycScanSta - 1].pad_trans_s1 >
  2160. CMDMatChar[CycScanSta].pad_trans_s1) {
  2161. autok_calc_cycle(pad_delay_period_cycle,
  2162. CMDMatChar[CycScanSta],
  2163. CMDMatChar[CycScanSta - 1], 1, 1,
  2164. 0,
  2165. AUTOK_CMD_EDGE_MATRIX_SCAN ? 1 :
  2166. 0);
  2167. } else {
  2168. autok_calc_cycle(pad_delay_period_cycle,
  2169. CMDMatChar[CycScanSta - 1],
  2170. CMDMatChar[CycScanSta], 1, 1, 0,
  2171. AUTOK_CMD_EDGE_MATRIX_SCAN ? 0 :
  2172. 1);
  2173. }
  2174. pad_delay_period_cycle *= 2;
  2175. }
  2176. /* If state0 have found NO transition boundary */
  2177. if (!CMDMatChar[CycScanSta - 1].pad_trans_cnt1 &&
  2178. !CMDMatChar[CycScanSta - 1].pad_trans_cnt2) {
  2179. /* In case of half boundary */
  2180. if (CMDMatChar[CycScanSta].pad_trans_s1 &&
  2181. !CMDMatChar[CycScanSta].pad_trans_e1)
  2182. CMDMatChar[CycScanSta].pad_trans_m1 =
  2183. CMDMatChar[CycScanSta].pad_trans_s1 +
  2184. (AUTOK_CMD_EDGE_MATRIX_SCAN ?
  2185. (AUTOK_TRANS_BOUND_RISING /
  2186. 2) : (AUTOK_TRANS_BOUND_FALLING / 2));
  2187. ck_sel = CMDMatChar[CycScanSta].pad_trans_m1 / 4;
  2188. CycScanSta = AUTOK_CYC_SCAN_CHNG_CKGEN;
  2189. /* Just estimate the cycle because it is too large */
  2190. if (!ck_sel)
  2191. pad_delay_period_cycle = autok_paddly_per_cyc_eval;
  2192. /* Use default edge and try again */
  2193. else {
  2194. if (!fCMDEdgeDefault)
  2195. fCMDEdgeDefault = 1;
  2196. goto ReTuneMatrix;
  2197. }
  2198. }
  2199. }
  2200. /* In case of NO transition boundary found */
  2201. else if (!CMDMatChar[CycScanSta].pad_trans_cnt1 &&
  2202. !CMDMatChar[CycScanSta].pad_trans_cnt2) {
  2203. /* If state0 have found 1 transition boundary */
  2204. if (CMDMatChar[CycScanSta - 1].pad_trans_cnt1 &&
  2205. !CMDMatChar[CycScanSta - 1].pad_trans_cnt2) {
  2206. /* In case of half boundary */
  2207. if (CMDMatChar[CycScanSta - 1].pad_trans_s1 &&
  2208. !CMDMatChar[CycScanSta - 1].pad_trans_e1)
  2209. CMDMatChar[CycScanSta - 1].pad_trans_m1 =
  2210. CMDMatChar[CycScanSta - 1].pad_trans_s1 +
  2211. (AUTOK_CMD_EDGE_MATRIX_SCAN ?
  2212. (AUTOK_TRANS_BOUND_FALLING /
  2213. 2) : (AUTOK_TRANS_BOUND_RISING / 2));
  2214. ck_sel = CMDMatChar[CycScanSta - 1].pad_trans_m1 / 4;
  2215. CycScanSta = AUTOK_CYC_SCAN_CHNG_CKGEN;
  2216. /* Just estimate the cycle because it is too large */
  2217. if (!ck_sel)
  2218. pad_delay_period_cycle = autok_paddly_per_cyc_eval;
  2219. /* Inverse the edge from default and try again */
  2220. else {
  2221. if (fCMDEdgeDefault)
  2222. fCMDEdgeDefault = 0;
  2223. goto ReTuneMatrix;
  2224. }
  2225. }
  2226. /* If state0 have found NO transition boundary */
  2227. if (!CMDMatChar[CycScanSta - 1].pad_trans_cnt1 &&
  2228. !CMDMatChar[CycScanSta - 1].pad_trans_cnt2) {
  2229. /* Just estimate the cycle because it is too large */
  2230. pad_delay_period_cycle = autok_paddly_per_cyc_eval;
  2231. }
  2232. }
  2233. break;
  2234. case AUTOK_CYC_SCAN_CHNG_CKGEN:
  2235. /* In case of only 1 transition boundary found */
  2236. if (CMDMatChar[CycScanSta].pad_trans_cnt1 &&
  2237. !CMDMatChar[CycScanSta].pad_trans_cnt2) {
  2238. /* Update the boundary length */
  2239. if (CMDMatChar[CycScanSta].pad_trans_cnt1 &&
  2240. CMDMatChar[CycScanSta].pad_trans_s1 &&
  2241. CMDMatChar[CycScanSta].pad_trans_e1) {
  2242. if (fCMDEdgeDefault) {
  2243. if (AUTOK_CMD_EDGE_MATRIX_SCAN)
  2244. autok_trans_bound_falling =
  2245. CMDMatChar[CycScanSta].pad_trans_cnt1;
  2246. else
  2247. autok_trans_bound_rising =
  2248. CMDMatChar[CycScanSta].pad_trans_cnt1;
  2249. AUTOK_PRINT
  2250. ("Update transition boundary len:%d(%s edge)\r\n",
  2251. (AUTOK_CMD_EDGE_MATRIX_SCAN ?
  2252. autok_trans_bound_falling :
  2253. autok_trans_bound_rising),
  2254. (AUTOK_CMD_EDGE_MATRIX_SCAN ? "falling" :
  2255. "rising"));
  2256. } else {
  2257. if (AUTOK_CMD_EDGE_MATRIX_SCAN)
  2258. autok_trans_bound_rising =
  2259. CMDMatChar[CycScanSta].pad_trans_cnt1;
  2260. else
  2261. autok_trans_bound_falling =
  2262. CMDMatChar[CycScanSta].pad_trans_cnt1;
  2263. AUTOK_PRINT
  2264. ("Update transition boundary len:%d(%s edge)\r\n",
  2265. (AUTOK_CMD_EDGE_MATRIX_SCAN ?
  2266. autok_trans_bound_rising :
  2267. autok_trans_bound_falling),
  2268. (AUTOK_CMD_EDGE_MATRIX_SCAN ? "rising" :
  2269. "falling"));
  2270. }
  2271. }
  2272. if (ck_sel > 2)
  2273. pad_shift =
  2274. ck_sel * MIN_CLK_GEN_DELAY_IN_PS / MIN_PAD_DELAY_IN_PS;
  2275. else
  2276. pad_shift = ck_sel * 4;
  2277. /* Base on inversed edge by default */
  2278. if (fCMDEdgeDefault) {
  2279. AUTOK_PRINT
  2280. ("Before shift, s1:%d, m1:%d, e1:%d, pad_shift:%d\r\n",
  2281. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_s1,
  2282. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_m1,
  2283. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_e1,
  2284. pad_shift);
  2285. /* Calculate to simulate the shift effect */
  2286. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_m1 -=
  2287. pad_shift;
  2288. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_e1 -=
  2289. pad_shift;
  2290. if (CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_e1 > 0) {
  2291. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_s1 =
  2292. CMDMatChar
  2293. [AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_e1 -
  2294. (AUTOK_CMD_EDGE_MATRIX_SCAN ?
  2295. AUTOK_TRANS_BOUND_RISING :
  2296. AUTOK_TRANS_BOUND_FALLING) + 1;
  2297. if (CMDMatChar
  2298. [AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_s1 < 0)
  2299. CMDMatChar
  2300. [AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_s1;
  2301. } else {
  2302. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_e1 =
  2303. 0;
  2304. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_s1 =
  2305. 0;
  2306. }
  2307. AUTOK_PRINT
  2308. ("Calc. the shift effect, S1:%d, E1:%d, BaseEdge:%s\r\n",
  2309. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_s1,
  2310. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE].pad_trans_e1,
  2311. AUTOK_CMD_EDGE_MATRIX_SCAN ? "Rising" : "Falling");
  2312. autok_calc_cycle(pad_delay_period_cycle,
  2313. CMDMatChar[AUTOK_CYC_SCAN_CHNG_EDGE],
  2314. CMDMatChar[CycScanSta], 1, 1, 0,
  2315. AUTOK_CMD_EDGE_MATRIX_SCAN ? 1 : 0);
  2316. }
  2317. /* Base on default edge */
  2318. else {
  2319. AUTOK_PRINT
  2320. ("Before shift, s1:%d, m1:%d, e1:%d, pad_shift:%d\r\n",
  2321. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_s1,
  2322. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_m1,
  2323. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_e1,
  2324. pad_shift);
  2325. /* Calculate to simulate the shift effect */
  2326. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_m1 -= pad_shift;
  2327. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_e1 -= pad_shift;
  2328. if (CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_e1 > 0) {
  2329. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_s1 =
  2330. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_e1 -
  2331. (AUTOK_CMD_EDGE_MATRIX_SCAN ?
  2332. AUTOK_TRANS_BOUND_FALLING :
  2333. AUTOK_TRANS_BOUND_RISING) + 1;
  2334. if (CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_s1 <
  2335. 0)
  2336. CMDMatChar
  2337. [AUTOK_CYC_SCAN_INIT].pad_trans_s1;
  2338. } else {
  2339. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_e1 = 0;
  2340. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_s1 = 0;
  2341. }
  2342. AUTOK_PRINT
  2343. ("Calc. the shift effect, S1:%d, E1:%d, BaseEdge:%s\r\n",
  2344. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_s1,
  2345. CMDMatChar[AUTOK_CYC_SCAN_INIT].pad_trans_e1,
  2346. AUTOK_CMD_EDGE_MATRIX_SCAN ? "Falling" : "Rising");
  2347. autok_calc_cycle(pad_delay_period_cycle,
  2348. CMDMatChar[AUTOK_CYC_SCAN_INIT],
  2349. CMDMatChar[CycScanSta], 1, 1, 0,
  2350. AUTOK_CMD_EDGE_MATRIX_SCAN ? 0 : 1);
  2351. }
  2352. pad_delay_period_cycle *= 2;
  2353. }
  2354. /* In case of NO transition boundary found */
  2355. else if (!CMDMatChar[CycScanSta].pad_trans_cnt1 &&
  2356. !CMDMatChar[CycScanSta].pad_trans_cnt2) {
  2357. /* Just estimate the cycle because it is too large */
  2358. pad_delay_period_cycle = autok_paddly_per_cyc_eval;
  2359. }
  2360. break;
  2361. default:
  2362. break;
  2363. }
  2364. }
  2365. if (pad_delay_period_cycle)
  2366. AUTOK_PRINT("Find 1T in pad delay: %d\r\n", pad_delay_period_cycle);
  2367. else
  2368. AUTOK_PRINT("Can NOT find pad delay cycle\r\n");
  2369. rPadDlyRes.CKGenPeriodLen = DIV_CEIL_FUNC((pad_delay_period_cycle * MIN_PAD_DELAY_IN_PS),
  2370. MIN_CLK_GEN_DELAY_IN_PS);
  2371. AUTOK_PRINT("Calculated 1T in CKGEN: %d\r\n", rPadDlyRes.CKGenPeriodLen);
  2372. if (rPadDlyRes.CKGenPeriodLen >= (SCALE_CKGEN_MSDC_DLY_SEL - 1))
  2373. rPadDlyRes.CKGenPeriodLen = SCALE_CKGEN_MSDC_DLY_SEL - 1;
  2374. /* Scan the cycle by CKGEN */
  2375. k = 0;
  2376. while (k <= rPadDlyRes.CKGenPeriodLen) {
  2377. if ((!fRdatPatFound || !rPadDlyRes.fHoleCK) && !fStopRDAT) {
  2378. /* Step1, tune CMD */
  2379. /* Set CKGEN and CMD edge */
  2380. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL, &k, MSDC_WRITE);
  2381. sel = 0;
  2382. msdc_autok_adjust_param(host, CMD_EDGE, &sel, MSDC_WRITE);
  2383. autok_cmd_cmdrrdly[0].raw_data = 0;
  2384. sel = 0;
  2385. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &sel, MSDC_WRITE);
  2386. for (m = 0; m < SCALE_PAD_TUNE_CMDRDLY; m++) {
  2387. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  2388. for (cnt = 0; cnt < (AUTOK_CMD_TIMES / 2); cnt++) {
  2389. if (autok_cmd_test(host) != E_RESULT_PASS) {
  2390. /* 0 means pass */
  2391. autok_cmd_cmdrrdly[0].raw_data |= (1 << m);
  2392. break;
  2393. }
  2394. }
  2395. }
  2396. autok_cmd_cmdrrdly[0].score =
  2397. autok_simple_score(autok_cmd_cmdrrdly[0].raw_data);
  2398. AUTOK_PRINT("CKGEN_MSDC_DLY \t PAD_TUNE_CMDRDLY \r\n");
  2399. AUTOK_PRINT("%02d \t %02d \t %s\r\n", k, autok_cmd_cmdrrdly[0].score,
  2400. g_tune_result_str);
  2401. autok_select_range(autok_cmd_cmdrrdly[0].raw_data, &sel);
  2402. msdc_autok_adjust_param(host, CMD_RD_DLY, &sel, MSDC_WRITE);
  2403. /* Step2, tune read data */
  2404. AUTOK_PRINT("Scanning Read data...\r\n");
  2405. data.raw_data = 0;
  2406. reTuneCmd = 0;
  2407. x = 0;
  2408. while (x < SCALE_PAD_TUNE_DATRDDLY && reTuneCmd == 0) {
  2409. msdc_autok_adjust_param(host, DAT_RD_DLY, &x, MSDC_WRITE);
  2410. for (m = 0; m < AUTOK_RDAT_TIMES; m++) {
  2411. res = autok_read_test(host);
  2412. if (res != E_RESULT_PASS) {
  2413. data.raw_data |= (1 << x);
  2414. if (autok_recovery(host)) {
  2415. AUTOK_PRINT
  2416. ("===tune read:error, fail to bring to tranfer status===\r\n");
  2417. goto exit;
  2418. }
  2419. if (res == E_RESULT_CMD_CRC) {
  2420. AUTOK_PRINT
  2421. ("[W]CMD CRC err@tuning read[%d %d],tune command again\r\n",
  2422. x, m);
  2423. reTuneCmd = 1;
  2424. }
  2425. break;
  2426. }
  2427. }
  2428. x++;
  2429. }
  2430. if ((reTuneCmd == 1) && (reTuneCmdCnt < 2)) {
  2431. AUTOK_PRINT("[ERR] Re-start to tune CMD again!!\r\n");
  2432. reTuneCmdCnt++;
  2433. k = 0;
  2434. continue;
  2435. } else if (reTuneCmdCnt >= 2)
  2436. #ifdef AUTOK_DEBUG
  2437. AUTOK_ERR();
  2438. #else
  2439. goto exit;
  2440. #endif
  2441. autok_rdata_scan[k].RawData = data.raw_data;
  2442. autok_rdata_scan[k].CurCKGEN = k;
  2443. autok_check_rawd_style(&autok_rdata_scan[k], 1);
  2444. #ifdef AUTOK_RDAT_ACC
  2445. /* Check if we have found the transition pattern */
  2446. if (!autok_rdata_scan[k].fInvalidCKGEN) {
  2447. /*In case of Full Boundary */
  2448. if (autok_rdata_scan[k].BoundReg1_S &&
  2449. autok_rdata_scan[k].BoundReg1_E && !rPadDlyRes.fFBound) {
  2450. /* Make sure the full boundary is large enough */
  2451. if (autok_rdata_scan[k].Reg1Cnt > AUTOK_RDAT_FBOUND_TH) {
  2452. /* Also check the former CKGEN */
  2453. if (k >= 1) {
  2454. if (!autok_rdata_scan[k - 1].fInvalidCKGEN
  2455. && autok_rdata_scan[k - 1].BoundReg1_S
  2456. &&
  2457. ((autok_rdata_scan[k].BoundReg1_S + 1) <
  2458. autok_rdata_scan[k - 1].BoundReg1_S)
  2459. &&
  2460. (ABS_DIFF
  2461. (autok_rdata_scan[k - 1].BoundReg1_S,
  2462. autok_rdata_scan[k].BoundReg1_S) <=
  2463. 5)) {
  2464. fRdatPatFound = 1;
  2465. rPadDlyRes.fFBound = 1;
  2466. rPadDlyRes.FBoundCK = k;
  2467. }
  2468. }
  2469. }
  2470. }
  2471. /* In case of Left Boundary */
  2472. else if (!autok_rdata_scan[k].BoundReg1_S
  2473. && autok_rdata_scan[k].Reg1Cnt) {
  2474. /* fRdatPatFound = 1; */
  2475. rPadDlyRes.fLBound = 1;
  2476. rPadDlyRes.LBoundCK = k;
  2477. }
  2478. /* Stop to find hole CKGEN */
  2479. if (!rPadDlyRes.fHoleCK &&
  2480. (k >= (AUTOK_CKGEN_ALLOW_MAX + 1)) && rPadDlyRes.fFBound)
  2481. fStopRDAT = 1;
  2482. }
  2483. /* Check if we have found the hole CKGEN */
  2484. else if (!rPadDlyRes.fHoleCK) {
  2485. rPadDlyRes.fHoleCK = 1;
  2486. rPadDlyRes.HoleCK = k;
  2487. }
  2488. #endif
  2489. k++;
  2490. } else
  2491. break;
  2492. }
  2493. /* Save the pad delay period subsequent operations */
  2494. rPadDlyRes.PadDlyPeriodLen = pad_delay_period_cycle;
  2495. pAutoKData[E_MSDC_PAD_DLY_PERIOD].data.sel = pad_delay_period_cycle;
  2496. exit:
  2497. return rPadDlyRes;
  2498. }
  2499. #elif defined(AUTOK_CYC_ALG_0)
  2500. static AUTOK_CYC_SCAN_RES_T
  2501. autok_cycle_scan(struct msdc_host *host, U_AUTOK_INTERFACE_DATA *pAutoKData)
  2502. {
  2503. unsigned int k, m, x, k_bak;
  2504. unsigned int sel;
  2505. #ifdef AUTOK_CMD_TUNE_LEGACY
  2506. unsigned int pass, stop, single_init, cross, FakeCnt;
  2507. unsigned char fCMDIntDlyFound = 0, fCMDMatrixStress = 0;
  2508. unsigned char CMDIntDly = 0, CMDIntDlyS, CMDIntDlyE;
  2509. E_AUTOK_ERR_STA err;
  2510. unsigned int max_score;
  2511. unsigned int max_numZero = 0;
  2512. #endif
  2513. unsigned int fRdatPatFound = 0, fStopRDAT = 0;
  2514. unsigned int pad_delay_period_cycle = 0;
  2515. unsigned int clk_gen_delay_period_cycle, ckgen_dly_period_max;
  2516. unsigned int periodCycle;
  2517. unsigned int minPadCycleScore;
  2518. unsigned int minClkGenCycleScore;
  2519. unsigned char bTryFindPadCycle = 1;
  2520. unsigned char bTryFindClkGenCycle = 1;
  2521. unsigned int range_min, range_max, autok_cmd_times;
  2522. int reTuneCmd, cnt, reTuneCmdCnt = 0;
  2523. unsigned int fCMDPatDoubleChk = 0, fReScanCycle = 1;
  2524. int PadDlyDiff;
  2525. AUTOK_CMDPAT_COMP_E fIdent;
  2526. AUTOK_CMDPAT_DOUBLE_CHK_T rCMDPatChk;
  2527. E_RESULT_TYPE res;
  2528. S_AUTOK_CMD_DLY data;
  2529. AUTOK_CYC_SCAN_RES_T rPadDlyRes;
  2530. ReScanCycle:
  2531. /* Initialize the parameters */
  2532. fRdatPatFound = 0;
  2533. pad_delay_period_cycle = 0;
  2534. bTryFindPadCycle = 1;
  2535. bTryFindClkGenCycle = 1;
  2536. fCMDPatDoubleChk = 0;
  2537. #ifdef AUTOK_CMD_TUNE_LEGACY
  2538. fCMDMatrixStress = 0;
  2539. fCMDIntDlyFound = 0;
  2540. CMDIntDly = 0;
  2541. max_numZero = 0;
  2542. #endif
  2543. memset(&rPadDlyRes, 0, sizeof(rPadDlyRes));
  2544. memset(&rCMDPatChk, 0, sizeof(rCMDPatChk));
  2545. memset(autok_cmd_cmdrrdly, 0, sizeof(S_AUTOK_CMD_DLY) * SCALE_CMD_RSP_DLY_SEL);
  2546. memset(autok_cmd_ckgdly, 0, sizeof(S_AUTOK_CMD_DLY) * SCALE_CKGEN_MSDC_DLY_SEL);
  2547. memset(autok_cmd_ckgdly_cmdrrdly0, 0, sizeof(S_AUTOK_CMD_DLY) * SCALE_CKGEN_MSDC_DLY_SEL);
  2548. memset(autok_ckg_data, 0, sizeof(S_AUTOK_CKGEN_DATA) * SCALE_CKGEN_MSDC_DLY_SEL);
  2549. memset(autok_rdata_scan, 0, sizeof(autok_rdata_scan));
  2550. memset(&data, 0, sizeof(data));
  2551. #ifdef MT6290
  2552. periodCycle = FREQ_MHZ_2_PERIOD_CYCLE_IN_PS(host->sclk / 1000000);
  2553. #else
  2554. periodCycle = FREQ_MHZ_2_PERIOD_CYCLE_IN_PS(host->mclk / 1000000);
  2555. #endif
  2556. clk_gen_delay_period_cycle = MAX_SCALE_OF_CLK_GEN_IN_ONE_CYCLE(periodCycle);
  2557. clk_gen_delay_period_cycle =
  2558. THRESHOLD_VAL(clk_gen_delay_period_cycle, SCALE_CKGEN_MSDC_DLY_SEL);
  2559. ckgen_dly_period_max = clk_gen_delay_period_cycle;
  2560. #ifdef MT6290
  2561. minPadCycleScore = MIN_SCORE_OF_PAD_DELAY_IN_ONE_CYCLE(periodCycle);
  2562. #else
  2563. minPadCycleScore = SCALE_PAD_TUNE_CMDRDLY;
  2564. #endif
  2565. if (minPadCycleScore >= SCALE_PAD_TUNE_CMDRDLY)
  2566. bTryFindPadCycle = 0;
  2567. minClkGenCycleScore = MIN_SCORE_OF_CLK_GEN_IN_ONE_CYCLE(periodCycle);
  2568. if (minClkGenCycleScore >= SCALE_CKGEN_MSDC_DLY_SEL)
  2569. bTryFindClkGenCycle = 0;
  2570. AUTOK_PRINT("period=%d MaxCkgen period=%d MinCkgen period=%d MinPad period=%d\r\n",
  2571. periodCycle, clk_gen_delay_period_cycle, minClkGenCycleScore, minPadCycleScore);
  2572. /* Initialize the loop parameters */
  2573. range_min = 0;
  2574. range_max = clk_gen_delay_period_cycle;
  2575. k = range_min;
  2576. range_max = THRESHOLD_VAL(range_max, SCALE_CKGEN_MSDC_DLY_SEL - 1);
  2577. AUTOK_PRINT("ckg scan range from %d to %d\r\n", k, range_max);
  2578. /*
  2579. * The aim of this loop, is to find the pad delay of 1T period.
  2580. * Stage1 only, NOT for Stage2.
  2581. */
  2582. while (k <= range_max) {
  2583. pAutoKData[E_MSDC_CKGEN_MSDC_DLY_SEL].data.sel = k;
  2584. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL, &k, MSDC_WRITE);
  2585. sel = 0;
  2586. pAutoKData[E_MSDC_IOCON_RSPL].data.sel = sel;
  2587. msdc_autok_adjust_param(host, CMD_EDGE, &sel, MSDC_WRITE);
  2588. /*Step1 find the simple cmd delay for clk_gen in internal delay=0 */
  2589. if (!fCMDPatDoubleChk) {
  2590. if (!k)
  2591. autok_cmd_times = AUTOK_CMD_TIMES;
  2592. else
  2593. autok_cmd_times = AUTOK_CMD_TIMES;
  2594. autok_cmd_cmdrrdly[0].raw_data = 0;
  2595. sel = 0;
  2596. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &sel, MSDC_WRITE);
  2597. for (m = 0; m < SCALE_PAD_TUNE_CMDRDLY; m++) {
  2598. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  2599. for (cnt = 0; cnt < autok_cmd_times; cnt++) {
  2600. if (autok_cmd_test(host) != E_RESULT_PASS) {
  2601. /* 0 means pass */
  2602. autok_cmd_cmdrrdly[0].raw_data |= (1 << m);
  2603. break;
  2604. }
  2605. }
  2606. }
  2607. autok_cmd_cmdrrdly[0].score =
  2608. autok_simple_score(autok_cmd_cmdrrdly[0].raw_data);
  2609. /*
  2610. * Record the detail information of each CKGEN,
  2611. * when internal delay is 0 at initial
  2612. */
  2613. autok_cmd_ckgdly_cmdrrdly0[k].raw_data = autok_cmd_cmdrrdly[0].raw_data;
  2614. }
  2615. /* For double CMD pattern double check, we need to scan by internal delay shifting */
  2616. else {
  2617. AUTOK_PRINT("Shift CMD internal delay and double check the cycle...\r\n");
  2618. AUTOK_PRINT("CK_S(%d),CK_E(%d),CMD_Int_Shift(%d),diff_b4(%d)\r\n",
  2619. rCMDPatChk.ck_s_b4, rCMDPatChk.ck_e_b4,
  2620. rCMDPatChk.cmd_int_shift, rCMDPatChk.diff_b4);
  2621. /* Reselect a internal delay for shifting */
  2622. sel = rCMDPatChk.cmd_int_shift;
  2623. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &sel, MSDC_WRITE);
  2624. autok_cmd_times = AUTOK_CMD_TIMES;
  2625. /* Select the CKGEN start point */
  2626. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL, &rCMDPatChk.ck_s_b4,
  2627. MSDC_WRITE);
  2628. autok_cmd_cmdrrdly[0].raw_data = 0;
  2629. for (m = 0; m < SCALE_PAD_TUNE_CMDRDLY; m++) {
  2630. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  2631. for (cnt = 0; cnt < autok_cmd_times; cnt++) {
  2632. if (autok_cmd_test(host) != E_RESULT_PASS) {
  2633. /* 0 means pass */
  2634. autok_cmd_cmdrrdly[0].raw_data |= (1 << m);
  2635. break;
  2636. }
  2637. }
  2638. }
  2639. rCMDPatChk.raw_s_sh = autok_cmd_cmdrrdly[0].raw_data;
  2640. autok_cmd_cmdrrdly[0].score =
  2641. autok_simple_score(autok_cmd_cmdrrdly[0].raw_data);
  2642. AUTOK_PRINT("CKGEN(%02d): %s, score=%d\r\n", rCMDPatChk.ck_s_b4,
  2643. g_tune_result_str, autok_cmd_cmdrrdly[0].score);
  2644. /* Select the CKGEN end point */
  2645. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL, &rCMDPatChk.ck_e_b4,
  2646. MSDC_WRITE);
  2647. autok_cmd_cmdrrdly[0].raw_data = 0;
  2648. for (m = 0; m < SCALE_PAD_TUNE_CMDRDLY; m++) {
  2649. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  2650. for (cnt = 0; cnt < autok_cmd_times; cnt++) {
  2651. if (autok_cmd_test(host) != E_RESULT_PASS) {
  2652. /* 0 means pass */
  2653. autok_cmd_cmdrrdly[0].raw_data |= (1 << m);
  2654. break;
  2655. }
  2656. }
  2657. }
  2658. rCMDPatChk.raw_e_sh = autok_cmd_cmdrrdly[0].raw_data;
  2659. autok_cmd_cmdrrdly[0].score =
  2660. autok_simple_score(autok_cmd_cmdrrdly[0].raw_data);
  2661. AUTOK_PRINT("CKGEN(%02d): %s, score=%d\r\n", rCMDPatChk.ck_e_b4,
  2662. g_tune_result_str, autok_cmd_cmdrrdly[0].score);
  2663. /* Compare the 2 CMD pattern again */
  2664. fIdent = autok_comp_cmd_pat(&rCMDPatChk, 1, &PadDlyDiff, 1);
  2665. /*
  2666. * If the cycle is correct, the pattern check
  2667. * could pass too, otherwise if NOT identical, continue
  2668. * to scan more CKGEN and find the right cycle
  2669. */
  2670. if ((fIdent == CMDPAT_IDENTICAL) &&
  2671. (ABS_DIFF(PadDlyDiff, rCMDPatChk.diff_b4) <=
  2672. (AUTOK_TUNING_INACCURACY + 1))) {
  2673. k = k_bak;
  2674. /* Update the cycle */
  2675. if (rCMDPatChk.scen == CMDPAT_REG_2_L) {
  2676. pad_delay_period_cycle = (MIN_CLK_GEN_DELAY_IN_PS *
  2677. (rCMDPatChk.ck_e_b4 -
  2678. rCMDPatChk.ck_s_b4)) /
  2679. MIN_PAD_DELAY_IN_PS;
  2680. pad_delay_period_cycle += PadDlyDiff;
  2681. AUTOK_PRINT
  2682. ("Update period change by ckg:pad cycle=%d ckg cycle=%d\r\n",
  2683. pad_delay_period_cycle,
  2684. rCMDPatChk.ck_e_b4 - rCMDPatChk.ck_s_b4);
  2685. }
  2686. goto CycDoubleChk;
  2687. } else {
  2688. /* Resume the CKGEN scan from the last one */
  2689. pad_delay_period_cycle = 0;
  2690. fCMDPatDoubleChk = 0;
  2691. minClkGenCycleScore = rCMDPatChk.ck_e_b4 - rCMDPatChk.ck_s_b4 + 1;
  2692. clk_gen_delay_period_cycle = ckgen_dly_period_max;
  2693. k = k_bak + 1;
  2694. AUTOK_PRINT("Resume to scan CKGEN from %d\r\n",
  2695. minClkGenCycleScore);
  2696. continue;
  2697. }
  2698. }
  2699. AUTOK_PRINT("CKGEN_MSDC_DLY \t PAD_TUNE_CMDRDLY \r\n");
  2700. AUTOK_PRINT("%02d \t %02d \t %s\r\n", k, autok_cmd_cmdrrdly[0].score,
  2701. g_tune_result_str);
  2702. if ((!fRdatPatFound || !rPadDlyRes.fHoleCK) && !fStopRDAT) {
  2703. autok_select_range(autok_cmd_cmdrrdly[0].raw_data,
  2704. &pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel);
  2705. msdc_autok_adjust_param(host, CMD_RD_DLY,
  2706. &pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel,
  2707. MSDC_WRITE);
  2708. /*Step2 tune_read data */
  2709. AUTOK_PRINT("Scanning Read data...\r\n");
  2710. data.raw_data = 0;
  2711. reTuneCmd = 0;
  2712. x = 0;
  2713. while (x < SCALE_PAD_TUNE_DATRDDLY && reTuneCmd == 0) {
  2714. msdc_autok_adjust_param(host, DAT_RD_DLY, &x, MSDC_WRITE);
  2715. for (m = 0; m < AUTOK_RDAT_TIMES; m++) {
  2716. res = autok_read_test(host);
  2717. if (res != E_RESULT_PASS) {
  2718. data.raw_data |= (1 << x);
  2719. if (autok_recovery(host)) {
  2720. AUTOK_PRINT
  2721. ("===tune read:err, fail bring to tranfer status===\r\n");
  2722. goto exit;
  2723. }
  2724. if (res == E_RESULT_CMD_CRC) {
  2725. AUTOK_PRINT
  2726. ("[W]CMD CRC err@tuning read[%d %d],tune CMD again!!\r\n",
  2727. x, m);
  2728. reTuneCmd = 1;
  2729. }
  2730. break;
  2731. }
  2732. }
  2733. x++;
  2734. }
  2735. if ((reTuneCmd == 1) && (reTuneCmdCnt < 2)) {
  2736. AUTOK_PRINT("[ERR] Re-start to tune CMD again!!\r\n");
  2737. reTuneCmdCnt++;
  2738. k = 0;
  2739. continue;
  2740. } else if (reTuneCmdCnt >= 2)
  2741. #ifdef AUTOK_DEBUG
  2742. AUTOK_ERR();
  2743. #else
  2744. goto exit;
  2745. #endif
  2746. autok_rdata_scan[k].RawData = data.raw_data;
  2747. autok_rdata_scan[k].CurCKGEN = k;
  2748. autok_check_rawd_style(&autok_rdata_scan[k], 1);
  2749. #ifdef AUTOK_RDAT_ACC
  2750. /* Check if we have found the transition pattern */
  2751. if (!autok_rdata_scan[k].fInvalidCKGEN) {
  2752. /*In case of Full Boundary */
  2753. if (autok_rdata_scan[k].BoundReg1_S &&
  2754. autok_rdata_scan[k].BoundReg1_E && !rPadDlyRes.fFBound) {
  2755. /* Make sure the full boundary is large enough */
  2756. if (autok_rdata_scan[k].Reg1Cnt > AUTOK_RDAT_FBOUND_TH) {
  2757. /* Also check the former CKGEN */
  2758. if (k >= 1) {
  2759. if (!autok_rdata_scan[k - 1].fInvalidCKGEN
  2760. && autok_rdata_scan[k - 1].BoundReg1_S
  2761. &&
  2762. ((autok_rdata_scan[k].BoundReg1_S + 1) <
  2763. autok_rdata_scan[k - 1].BoundReg1_S)
  2764. &&
  2765. (ABS_DIFF
  2766. (autok_rdata_scan[k - 1].BoundReg1_S,
  2767. autok_rdata_scan[k].BoundReg1_S) <=
  2768. 5)) {
  2769. fRdatPatFound = 1;
  2770. rPadDlyRes.fFBound = 1;
  2771. rPadDlyRes.FBoundCK = k;
  2772. }
  2773. }
  2774. }
  2775. }
  2776. /* In case of Left Boundary */
  2777. else if (!autok_rdata_scan[k].BoundReg1_S
  2778. && autok_rdata_scan[k].Reg1Cnt) {
  2779. /* fRdatPatFound = 1; */
  2780. rPadDlyRes.fLBound = 1;
  2781. rPadDlyRes.LBoundCK = k;
  2782. }
  2783. /* Stop to find hole CKGEN */
  2784. if (!rPadDlyRes.fHoleCK &&
  2785. (k >= (AUTOK_CKGEN_ALLOW_MAX + 1)) && rPadDlyRes.fFBound)
  2786. fStopRDAT = 1;
  2787. }
  2788. /* Check if we have found the hole CKGEN */
  2789. else if (!rPadDlyRes.fHoleCK) {
  2790. rPadDlyRes.fHoleCK = 1;
  2791. rPadDlyRes.HoleCK = k;
  2792. }
  2793. #endif
  2794. }
  2795. /*
  2796. * To confirm the CMD internal delay
  2797. */
  2798. #ifdef AUTOK_CMD_TUNE_LEGACY
  2799. AUTOK_PRINT("CKGEN_MSDC_DLY \t PAD_TUNE_CMDRRDLY \t PAD_TUNE_CMDRDLY \r\n");
  2800. pass = 0;
  2801. single_init = 0;
  2802. cross = 0;
  2803. FakeCnt = 0;
  2804. max_score = 0;
  2805. /* Select the CMD edge again */
  2806. sel = AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  2807. pAutoKData[E_MSDC_IOCON_RSPL].data.sel = sel;
  2808. msdc_autok_adjust_param(host, CMD_EDGE, &sel, MSDC_WRITE);
  2809. /* Determine if to scan 2 dimension matrix */
  2810. if (fCMDIntDlyFound) {
  2811. CMDIntDlyS = CMDIntDly;
  2812. #ifdef MT6290
  2813. CMDIntDlyE = CMDIntDly + 1;
  2814. /* For MT65x2, Just do NOT scan CMD matrix anymore */
  2815. #else
  2816. CMDIntDlyE = CMDIntDly;
  2817. max_score = 32;
  2818. #endif
  2819. } else {
  2820. CMDIntDlyS = 0;
  2821. CMDIntDlyE = SCALE_CMD_RSP_DLY_SEL;
  2822. }
  2823. /* Enhance CMD loop times */
  2824. autok_cmd_times = AUTOK_CMD_TIMES;
  2825. if (fCMDMatrixStress)
  2826. autok_cmd_times *= 2;
  2827. for (x = CMDIntDlyS; x < CMDIntDlyE; x++) {
  2828. autok_cmd_cmdrrdly[x].raw_data = 0;
  2829. autok_cmd_cmdrrdly[x].fstPosErrEnd = 0;
  2830. autok_cmddly_stop_bit[x] = 0;
  2831. stop = 0;
  2832. err = ERR_NONE;
  2833. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &x, MSDC_WRITE);
  2834. for (m = 0; m < SCALE_PAD_TUNE_CMDRDLY && stop == 0; m++) {
  2835. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  2836. for (cnt = 0; cnt < autok_cmd_times; cnt++) {
  2837. if (autok_cmd_test(host) != E_RESULT_PASS) {
  2838. /* 0 means pass */
  2839. autok_cmd_cmdrrdly[x].raw_data |= (1 << m);
  2840. if ((err == PASS_AFTER_ERR) && !fCMDIntDlyFound) {
  2841. /* Restart to find the possible cross point */
  2842. if ((cross > 1)
  2843. && (cross <
  2844. (AUTOK_CMDMAT_CROSS_MAR -
  2845. FakeCnt))) {
  2846. if (pass) {
  2847. pass = 0;
  2848. FakeCnt++;
  2849. if (FakeCnt >= AUTOK_CMDMAT_CROSS_MAR) {
  2850. AUTOK_PRINT
  2851. ("[ERR]Too many fake points!\r\n");
  2852. #ifdef AUTOK_DEBUG
  2853. AUTOK_ERR();
  2854. #else
  2855. goto exit;
  2856. #endif
  2857. }
  2858. }
  2859. }
  2860. /*
  2861. * Set the 'stop' flag
  2862. * once we find two fail regions
  2863. */
  2864. stop = 1;
  2865. autok_cmddly_stop_bit[x] = m;
  2866. } else if (err == ERR_NONE) {
  2867. err = ERR_OCCURE;
  2868. }
  2869. break;
  2870. }
  2871. }
  2872. if ((cnt == autok_cmd_times) && (err == ERR_OCCURE)) {
  2873. err = PASS_AFTER_ERR;
  2874. autok_cmd_cmdrrdly[x].fstPosErrEnd = m - 1;
  2875. }
  2876. }
  2877. /* If we do NOT find 2 region at the first internal delay */
  2878. if (!x && !fCMDIntDlyFound && !stop && !single_init)
  2879. single_init = 1;
  2880. else if (!x && !fCMDIntDlyFound && stop && !cross)
  2881. cross = 1;
  2882. /*only find one pass region */
  2883. if (pass == 1 && stop == 1) {
  2884. break;
  2885. } else if (pass == 0 && stop == 1) {
  2886. /* Restart to find the possible cross point */
  2887. if ((single_init && x && !cross) || FakeCnt) {
  2888. AUTOK_PRINT("[NOTICE]There are GAPs here.....(%d,%d)\r\n",
  2889. cross, FakeCnt);
  2890. /*
  2891. * To filter the fake result,
  2892. * but do NOT forget the max_score
  2893. */
  2894. if (!FakeCnt) {
  2895. /* Considering if the cross point is at the begainning */
  2896. if (single_init && (x >= 3)) {
  2897. /* In case of cross point at begainning */
  2898. if (ABS_DIFF
  2899. (autok_cmd_cmdrrdly[x - 1].fstPosErr,
  2900. autok_cmd_cmdrrdly[x - 3].fstPosErr) >=
  2901. 2) {
  2902. AUTOK_PRINT
  2903. ("[NOTICE]CrossP happenned!\r\n");
  2904. single_init = 0;
  2905. cross = x;
  2906. pass = 1;
  2907. }
  2908. /* In case of cross point from here */
  2909. else {
  2910. AUTOK_PRINT
  2911. ("[NOTICE]CrossP is to be happenned!\r\n");
  2912. max_score = 0;
  2913. cross = 1;
  2914. }
  2915. }
  2916. /* x < 3 */
  2917. else {
  2918. /* In case of cross point at begainning */
  2919. if ((ABS_DIFF
  2920. (autok_cmd_cmdrrdly[x - 1].fstPosErr,
  2921. autok_cmd_cmdrrdly[0].fstPosErr) >=
  2922. (x - 1))
  2923. &&
  2924. ((autok_cmddly_stop_bit[x] -
  2925. autok_cmd_cmdrrdly[x].fstPosErrEnd) <=
  2926. 2)) {
  2927. AUTOK_PRINT
  2928. ("[NOTICE]CrossP happenned!\r\n");
  2929. single_init = 0;
  2930. cross = x;
  2931. pass = 1;
  2932. }
  2933. /* In case of cross point from here */
  2934. else {
  2935. AUTOK_PRINT
  2936. ("[NOTICE]CrossP is to be happenned!\r\n");
  2937. max_score = 0;
  2938. cross = 1;
  2939. }
  2940. }
  2941. }
  2942. /* Restore the flag */
  2943. else
  2944. pass = 1;
  2945. }
  2946. continue;
  2947. }
  2948. if (cross) {
  2949. pass = 1;
  2950. /* Calculate the cross count */
  2951. cross++;
  2952. }
  2953. autok_cmd_cmdrrdly[x].score =
  2954. autok_check_score(autok_cmd_cmdrrdly[x].raw_data,
  2955. &autok_cmd_cmdrrdly[x].numOfzero,
  2956. &autok_cmd_cmdrrdly[x].fstPosErr,
  2957. &autok_cmd_cmdrrdly[x].period, minPadCycleScore);
  2958. AUTOK_PRINT("%02d \t %02d \t %02d \t %s\r\n", k, x,
  2959. autok_cmd_cmdrrdly[x].score, g_tune_result_str);
  2960. if (autok_cmd_cmdrrdly[x].score > max_score) {
  2961. max_score = autok_cmd_cmdrrdly[x].score;
  2962. max_numZero = autok_cmd_cmdrrdly[x].numOfzero;
  2963. sel = x;
  2964. } else if (autok_cmd_cmdrrdly[x].score == max_score) {
  2965. if (autok_cmd_cmdrrdly[x].numOfzero > max_numZero) {
  2966. max_numZero = autok_cmd_cmdrrdly[x].numOfzero;
  2967. sel = x;
  2968. }
  2969. }
  2970. }
  2971. /* To check the validation of current internal delay */
  2972. if (k && !fCMDIntDlyFound && !fCMDMatrixStress) {
  2973. if (ABS_DIFF(sel, autok_ckg_data[k - 1].interDelaySel) > 8) {
  2974. AUTOK_PRINT
  2975. ("[W]Restart to scan CKGEN due to invalid CMD matrix!IntDly Gap(%d)\r\n",
  2976. ABS_DIFF(sel, autok_ckg_data[k - 1].interDelaySel));
  2977. /* Restart CKGEN scan if the internal delay is invalid */
  2978. k = k - 1;
  2979. fCMDMatrixStress = 1;
  2980. continue;
  2981. }
  2982. }
  2983. /* To scan once again */
  2984. if (max_score == 0) {
  2985. AUTOK_PRINT
  2986. ("autok algorithm for tuning cmd internal delay need to scan more!!\r\n");
  2987. for (x = 0; x < SCALE_CMD_RSP_DLY_SEL; x++) {
  2988. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &x, MSDC_WRITE);
  2989. if (autok_cmddly_stop_bit[x] == (SCALE_PAD_TUNE_CMDRDLY - 1))
  2990. autok_cmddly_stop_bit[x] -= 1;
  2991. for (m = autok_cmddly_stop_bit[x] + 1; m < SCALE_PAD_TUNE_CMDRDLY;
  2992. m++) {
  2993. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  2994. for (cnt = 0; cnt < AUTOK_CMD_TIMES; cnt++) {
  2995. if (autok_cmd_test(host) != E_RESULT_PASS) {
  2996. /* 0 means pass */
  2997. autok_cmd_cmdrrdly[x].raw_data |= (1 << m);
  2998. break;
  2999. }
  3000. }
  3001. }
  3002. autok_cmd_cmdrrdly[x].score =
  3003. autok_check_score(autok_cmd_cmdrrdly[x].raw_data,
  3004. &autok_cmd_cmdrrdly[x].numOfzero,
  3005. &autok_cmd_cmdrrdly[x].fstPosErr,
  3006. &autok_cmd_cmdrrdly[x].period,
  3007. minPadCycleScore);
  3008. AUTOK_PRINT("%02d \t %02d \t %02d \t %s\r\n", k, x,
  3009. autok_cmd_cmdrrdly[x].score, g_tune_result_str);
  3010. if (autok_cmd_cmdrrdly[x].score > max_score) {
  3011. max_score = autok_cmd_cmdrrdly[x].score;
  3012. max_numZero = autok_cmd_cmdrrdly[x].numOfzero;
  3013. sel = x;
  3014. } else if (autok_cmd_cmdrrdly[x].score == max_score) {
  3015. if (autok_cmd_cmdrrdly[x].numOfzero > max_numZero) {
  3016. max_numZero = autok_cmd_cmdrrdly[x].numOfzero;
  3017. sel = x;
  3018. }
  3019. }
  3020. }
  3021. }
  3022. #ifdef MT6290
  3023. if (1) {
  3024. #else
  3025. if (!fCMDIntDlyFound) {
  3026. #endif
  3027. /* Apply CMD internal delay */
  3028. autok_ckg_data[k].interDelaySel = sel;
  3029. autok_cmd_ckgdly[k] = autok_cmd_cmdrrdly[sel];
  3030. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &sel, MSDC_WRITE);
  3031. AUTOK_PRINT("CMD internal delay %d score= %d numOfZero=%d fstPosErr=%d\r\n",
  3032. sel, max_score, max_numZero, autok_cmd_ckgdly[k].fstPosErr);
  3033. /*Step4 find sampling edge */
  3034. sel = ~AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3035. data.raw_data = 0;
  3036. msdc_autok_adjust_param(host, CMD_EDGE, &sel, MSDC_WRITE);
  3037. for (m = 0; m < SCALE_PAD_TUNE_CMDRDLY; m++) {
  3038. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  3039. for (cnt = 0; cnt < AUTOK_CMD_TIMES; cnt++) {
  3040. if (autok_cmd_test(host) != E_RESULT_PASS) {
  3041. /* 0 means pass */
  3042. data.raw_data |= (1 << m);
  3043. break;
  3044. }
  3045. }
  3046. }
  3047. data.score = autok_check_score(data.raw_data,
  3048. &data.numOfzero,
  3049. &data.fstPosErr,
  3050. &data.period, minPadCycleScore);
  3051. AUTOK_PRINT("%s edge %s score=%d fstPosErr=%d\r\n",
  3052. sel ? "Falling" : "Rising", g_tune_result_str, data.score,
  3053. data.fstPosErr);
  3054. sel = AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3055. #if 0
  3056. if (data.fstPosErr < autok_cmd_ckgdly[k].fstPosErr) {
  3057. sel = ~AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3058. autok_cmd_ckgdly[k] = data;
  3059. }
  3060. #else
  3061. if (data.score > max_score) {
  3062. sel = ~AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3063. autok_cmd_ckgdly[k] = data;
  3064. } else if (data.score == max_score) {
  3065. if (data.fstPosErr < autok_cmd_ckgdly[k].fstPosErr) {
  3066. sel = ~AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3067. autok_cmd_ckgdly[k] = data;
  3068. }
  3069. }
  3070. #endif
  3071. if (sel != (AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01))
  3072. msdc_autok_adjust_param(host, CMD_EDGE, &sel, MSDC_WRITE);
  3073. pAutoKData[E_MSDC_IOCON_RSPL].data.sel = sel;
  3074. AUTOK_PRINT("%s CMD edge is choosen!\r\n", sel ? "Falling" : "Rising");
  3075. autok_ckg_data[k].cmdEdgeSel = pAutoKData[E_MSDC_IOCON_RSPL].data.sel;
  3076. autok_ckg_data[k].cmdScore = autok_cmd_ckgdly[k].score;
  3077. /*
  3078. autok_select_range
  3079. (autok_cmd_ckgdly[k].raw_data, &pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel);
  3080. autok_ckg_data[k].cmdPadSel = pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel;
  3081. msdc_autok_adjust_param
  3082. (host, CMD_RD_DLY, &pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel, MSDC_WRITE);
  3083. */
  3084. }
  3085. /* For performance consideration, skip the after CKGEN for CMD matrix */
  3086. if (!fCMDIntDlyFound && (k >= AUTOK_CKGEN_ALLOW_MAX)) {
  3087. /* Record the internal delay for CMD */
  3088. fCMDIntDlyFound = 1;
  3089. CMDIntDly = sel;
  3090. pAutoKData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel = sel;
  3091. }
  3092. #endif /* AUTOK_CMD_TUNE_LEGACY */
  3093. /* Find cycle via pad delay */
  3094. if (bTryFindPadCycle == 1 && pad_delay_period_cycle == 0
  3095. && autok_cmd_ckgdly[k].period >= minPadCycleScore) {
  3096. /*get period form data the pass criteria may change */
  3097. pad_delay_period_cycle = autok_cmd_ckgdly[k].period;
  3098. clk_gen_delay_period_cycle = DIV_CEIL_FUNC(autok_cmd_ckgdly[k].period,
  3099. DIV_CEIL_FUNC
  3100. (MIN_CLK_GEN_DELAY_IN_PS,
  3101. MIN_PAD_DELAY_IN_PS));
  3102. range_max = clk_gen_delay_period_cycle;
  3103. /*
  3104. * If we can find 1T in full range of pad delay,
  3105. * the inernal delay can be confirmed in the former CMD matrix scan
  3106. */
  3107. rPadDlyRes.fCMDIntDlyConf = 1;
  3108. AUTOK_PRINT("period change by pad :pad cycle=%d ckg cycle=%d\r\n",
  3109. pad_delay_period_cycle, clk_gen_delay_period_cycle);
  3110. }
  3111. /* Find cycle via CKGEN */
  3112. if (bTryFindClkGenCycle == 1 && pad_delay_period_cycle == 0
  3113. && k >= minClkGenCycleScore) {
  3114. for (x = 0;
  3115. x <= 0 /*k-minClkGenCycleScore */ && pad_delay_period_cycle == 0;
  3116. x++) {
  3117. for (m = x + minClkGenCycleScore; m <= k; m++) {
  3118. memset(&rCMDPatChk, 0, sizeof(rCMDPatChk));
  3119. rCMDPatChk.ck_s_b4 = x;
  3120. rCMDPatChk.raw_s_b4 =
  3121. autok_cmd_ckgdly_cmdrrdly0[x].raw_data;
  3122. rCMDPatChk.ck_e_b4 = m;
  3123. rCMDPatChk.raw_e_b4 =
  3124. autok_cmd_ckgdly_cmdrrdly0[m].raw_data;
  3125. /* Check if the two rows are identical */
  3126. fIdent = autok_comp_cmd_pat(&rCMDPatChk, 0, &PadDlyDiff, 1);
  3127. rCMDPatChk.diff_b4 = PadDlyDiff;
  3128. if (fIdent == CMDPAT_NONE)
  3129. continue;
  3130. #if 0
  3131. if (fIdent == CMDPAT_HALF_IDENTICAL)
  3132. rCMDPatChk.cmd_int_shift = 12;
  3133. else if (fIdent == CMDPAT_IDENTICAL)
  3134. rCMDPatChk.cmd_int_shift = 6;
  3135. #endif
  3136. #if 0
  3137. /* To filter the CKGEN which may cause mistake judgement */
  3138. if (((fIdent == CMDPAT_HALF_IDENTICAL)
  3139. || (fIdent == CMDPAT_IDENTICAL)) && fCMDPatChkMore) {
  3140. fCMDPatChkMore = 0;
  3141. /* Skip the mistaken CKGEN and continue */
  3142. minClkGenCycleScore = m - x + 1;
  3143. AUTOK_PRINT
  3144. ("Find a fake CKGEN(%d) for a cycle, continue to scan...\r\n",
  3145. minClkGenCycleScore - 1);
  3146. goto CycDoubleChk;
  3147. }
  3148. /*
  3149. * If the interval of external and internal transition is too near
  3150. * we might found only 1 region after 1T, in this case, we just
  3151. * judge the position only
  3152. */
  3153. if ((fIdent == CMDPAT_HALF_IDENTICAL) && !fCMDPatNearAtInit)
  3154. continue;
  3155. #endif
  3156. clk_gen_delay_period_cycle = m - x;
  3157. /* Get the accurate value */
  3158. pad_delay_period_cycle =
  3159. (MIN_CLK_GEN_DELAY_IN_PS * (m - x)) /
  3160. MIN_PAD_DELAY_IN_PS;
  3161. pad_delay_period_cycle += PadDlyDiff;
  3162. AUTOK_PRINT
  3163. ("period change by ckg:pad cycle=%d ckg cycle=%d\r\n",
  3164. pad_delay_period_cycle, clk_gen_delay_period_cycle);
  3165. break;
  3166. }
  3167. }
  3168. if (!pad_delay_period_cycle) {
  3169. AUTOK_PRINT("[NOTICE]It is NOT the 1T whole cycle!\r\n");
  3170. /* Update the minimum CKGEN cycle */
  3171. minClkGenCycleScore += 1;
  3172. }
  3173. }
  3174. CycDoubleChk:
  3175. /*
  3176. * To double check the calculated cycle via to select another CKGEN,
  3177. * and the condition is we can compare the pattern which has 2 fail regions.
  3178. * if the 'k' exceeds the max_range, just quit the loop
  3179. */
  3180. if (!fCMDPatDoubleChk && !rPadDlyRes.fCMDIntDlyConf && pad_delay_period_cycle) {
  3181. fCMDPatDoubleChk = 1;
  3182. k_bak = k;
  3183. continue;
  3184. }
  3185. /*
  3186. * If the flag is NOT true, or the pattern to be compared has NOT found yet
  3187. * just use the cycle we just calculate
  3188. */
  3189. else if (fCMDPatDoubleChk && pad_delay_period_cycle)
  3190. range_max = clk_gen_delay_period_cycle;
  3191. /* Break the loop if 1 cycle is scanned by CKGEN */
  3192. if (k >= clk_gen_delay_period_cycle)
  3193. break;
  3194. k++;
  3195. }
  3196. /* Show the raw data by CMD cycle scan */
  3197. AUTOK_PRINT("Show the raw data by CMD cycle scan: \r\n");
  3198. for (k = range_min; k <= ckgen_dly_period_max; k++) {
  3199. autok_simple_score(autok_cmd_ckgdly_cmdrrdly0[k].raw_data);
  3200. AUTOK_PRINT("%2d \t %s\r\n", k, g_tune_result_str);
  3201. }
  3202. /* We must find the pad delay length for 1 cycle */
  3203. if (pad_delay_period_cycle == 0) {
  3204. AUTOK_PRINT("[W]Can NOT find pad delay cycle!\r\n");
  3205. /* Show current Vcore */
  3206. autok_get_current_vcore_offset();
  3207. /* Show the raw data of RDAT */
  3208. AUTOK_PRINT("Show the raw data of RDAT: \r\n");
  3209. for (k = range_min; k <= MAX_GET(rPadDlyRes.FBoundCK, rPadDlyRes.LBoundCK); k++) {
  3210. autok_simple_score(autok_rdata_scan[k].RawData);
  3211. AUTOK_PRINT("%2d \t %s\r\n", k, g_tune_result_str);
  3212. }
  3213. if (fReScanCycle) {
  3214. AUTOK_PRINT("Try to rescan the cycle...\r\n");
  3215. fReScanCycle--;
  3216. goto ReScanCycle;
  3217. } else {
  3218. AUTOK_PRINT("[ERR]Still can NOT find pad delay cycle anyway!\r\n");
  3219. #ifdef AUTOK_DEBUG
  3220. AUTOK_ERR();
  3221. #else
  3222. goto exit;
  3223. #endif
  3224. }
  3225. }
  3226. /* Save the pad delay period subsequent operations */
  3227. rPadDlyRes.CKGenPeriodLen = clk_gen_delay_period_cycle;
  3228. rPadDlyRes.PadDlyPeriodLen = pad_delay_period_cycle;
  3229. pAutoKData[E_MSDC_PAD_DLY_PERIOD].data.sel = pad_delay_period_cycle;
  3230. exit:
  3231. return rPadDlyRes;
  3232. }
  3233. #endif
  3234. static AUTOK_CMD_TUNE_RES_T
  3235. autok_tune_cmd(struct msdc_host *host,
  3236. E_AUTOK_TUNING_STAGE stg,
  3237. U_AUTOK_INTERFACE_DATA *pAutoKData,
  3238. P_AUTOK_CYC_SCAN_RES_T pPadDlyRes, P_AUTOK_RD_TUNE_RES_T pRDTuneRes)
  3239. {
  3240. unsigned int CMDIntPassWin;
  3241. unsigned char fCMDIntDlyScan = 0;
  3242. unsigned int CMDEdgeSel = 0, CMDDlySel = 0;
  3243. unsigned int sel = 0;
  3244. int cnt;
  3245. unsigned int pad_delay_period_cycle = 0;
  3246. #ifdef AUTOK_CMD_TUNE_LEGACY
  3247. unsigned int pad_delay_half_period = 0;
  3248. unsigned int PadDlyCMDRef = 0, CMDIntMargin;
  3249. unsigned int fCMDPadFullRange = 0, CMDPadRange;
  3250. unsigned int CMDScanRangeStg2 = 0, CMDPadScanEdge = 1;
  3251. unsigned int CMDIntDlyStg1, CMDIntDlyStg2;
  3252. unsigned char CMDDlyScanS = 0, CMDDlyScanE = 0, fCMDPadLatchMid = 1;
  3253. unsigned int m;
  3254. #else
  3255. unsigned int x, m;
  3256. unsigned int fCMDEdgeDefault = 1;
  3257. AUTOK_CMDMAT_CHAR_POS CMDMatChar;
  3258. unsigned int CMDMatRaw[SCALE_CMDMAT_RSP_DLY_SEL];
  3259. #endif
  3260. S_AUTOK_CMD_DLY data;
  3261. AUTOK_CMD_TUNE_RES_T rCmdTuneRes;
  3262. AUTOK_RAWD_SCAN_T CMDPadScan;
  3263. AUTOK_PRINT("=====autok_tune_cmd=====\r\n");
  3264. /* Initialize parameters */
  3265. memset(&rCmdTuneRes, 0, sizeof(rCmdTuneRes));
  3266. pad_delay_period_cycle = pPadDlyRes->PadDlyPeriodLen;
  3267. AUTOK_PRINT("CMD tune, Pad delays per 1 cycle: %d\r\n", pad_delay_period_cycle);
  3268. /*
  3269. * Step1 :
  3270. * Determine the CMD pad delay firstly,
  3271. */
  3272. #ifdef AUTOK_CMD_TUNE_LEGACY
  3273. /*
  3274. * Becuase the CMD internal delay is select at the largest margin,
  3275. * or at the same step with pad delay, so we can just scan the pad delay
  3276. * only, and is supposed that there should only have 1 fail region
  3277. */
  3278. ReTuneCMDPad:
  3279. /*
  3280. * Defaultly,
  3281. * Re-set sampling edge to falling edge
  3282. * Otherwise,
  3283. * Use rising edge as the CMD sampling edge
  3284. */
  3285. if (CMDPadScanEdge) {
  3286. sel = 1;
  3287. msdc_autok_adjust_param(host, CMD_EDGE, &sel, MSDC_WRITE);
  3288. } else {
  3289. sel = 0;
  3290. msdc_autok_adjust_param(host, CMD_EDGE, &sel, MSDC_WRITE);
  3291. }
  3292. if (stg == TUNING_STG1) {
  3293. /* Get the pad delay length by stage1 */
  3294. pad_delay_period_cycle = pPadDlyRes->PadDlyPeriodLen;
  3295. /* Apply internal delay, by read tune on stage1 */
  3296. pAutoKData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel =
  3297. autok_ckg_data[pRDTuneRes->CKGenSel].interDelaySel;
  3298. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY,
  3299. &pAutoKData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel, MSDC_WRITE);
  3300. /* Get CMD pad delay reference from read data */
  3301. PadDlyCMDRef = pRDTuneRes->PadDlyRefRD;
  3302. /* Use default range at first time */
  3303. if (!fCMDPadFullRange)
  3304. CMDPadRange = AUTOK_CMD_SCAN_RANGE_STG1;
  3305. /* Use full range if can NOT find fail region */
  3306. else
  3307. CMDPadRange = 32;
  3308. if (PadDlyCMDRef > CMDPadRange)
  3309. CMDDlyScanS = PadDlyCMDRef - CMDPadRange;
  3310. else
  3311. CMDDlyScanS = 0;
  3312. if ((PadDlyCMDRef + CMDPadRange) > 31)
  3313. CMDDlyScanE = 31;
  3314. else
  3315. CMDDlyScanE = PadDlyCMDRef + CMDPadRange;
  3316. } else if (stg == TUNING_STG2) {
  3317. /* Get CMD pad delay reference from stage1 */
  3318. PadDlyCMDRef = pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel;
  3319. pad_delay_period_cycle = pAutoKData[E_MSDC_PAD_DLY_PERIOD].data.sel;
  3320. CMDIntMargin = pAutoKData[E_MSDC_CMD_INT_MARGIN].data.sel;
  3321. rCmdTuneRes.CmdPadDlyStg1Bak = PadDlyCMDRef;
  3322. AUTOK_PRINT("CMD tune, Pad delays per 1 cycle: %d\r\n", pad_delay_period_cycle);
  3323. pad_delay_half_period = pad_delay_period_cycle / 2;
  3324. /* The internal delay is not stable enough, skip the CMD tune */
  3325. if (CMDIntMargin == AUTOK_SKIP_CMDTUNE_ON_STG2) {
  3326. AUTOK_PRINT
  3327. ("[W] The internal delay is not stable enough, skip the CMD tune!\r\n");
  3328. rCmdTuneRes.fTimingShiftLarge = 1;
  3329. goto exit;
  3330. }
  3331. /*
  3332. * The range should be near to but less than 1/2T,
  3333. * To avoid the scanning steps on internal boundary
  3334. */
  3335. if (CMDIntMargin) {
  3336. CMDScanRangeStg2 = CMDIntMargin - 5;
  3337. AUTOK_PRINT
  3338. ("[W] The internal delay may NOT be stable enough! Reduce the range.\r\n");
  3339. } else
  3340. CMDScanRangeStg2 = pad_delay_half_period / 2; /* AUTOK_CMD_SCAN_DE_RANGE_STG2; */
  3341. if (PadDlyCMDRef > CMDScanRangeStg2)
  3342. CMDDlyScanS = PadDlyCMDRef - CMDScanRangeStg2;
  3343. else
  3344. CMDDlyScanS = 0;
  3345. if ((PadDlyCMDRef + CMDScanRangeStg2) > 31)
  3346. CMDDlyScanE = 31;
  3347. else
  3348. CMDDlyScanE = PadDlyCMDRef + CMDScanRangeStg2;
  3349. /*
  3350. * Re-select CMD internal delay for
  3351. * pad delay scan at the falling sampling edge
  3352. */
  3353. CMDIntDlyStg1 = pAutoKData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel;
  3354. CMDIntDlyStg2 = CMDIntDlyStg1;
  3355. if (CMDPadScanEdge) {
  3356. if (pad_delay_half_period <= 16) {
  3357. if (CMDIntDlyStg1 >= pad_delay_half_period)
  3358. CMDIntDlyStg1 = CMDIntDlyStg1 - pad_delay_half_period;
  3359. else
  3360. CMDIntDlyStg1 = CMDIntDlyStg1 + pad_delay_half_period;
  3361. } else {
  3362. if (CMDIntDlyStg1 >= pad_delay_half_period)
  3363. CMDIntDlyStg1 = CMDIntDlyStg1 - pad_delay_half_period;
  3364. else if (CMDIntDlyStg1 < (32 - pad_delay_half_period))
  3365. CMDIntDlyStg1 = CMDIntDlyStg1 + pad_delay_half_period;
  3366. else {
  3367. if (CMDIntDlyStg1 >= 16)
  3368. CMDIntDlyStg1 = 0;
  3369. else
  3370. CMDIntDlyStg1 = 31;
  3371. }
  3372. }
  3373. }
  3374. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &CMDIntDlyStg1, MSDC_WRITE);
  3375. AUTOK_PRINT("Before scan CMD pad delay, re-select CMD internal delay to %d(%d)\r\n",
  3376. CMDIntDlyStg1, CMDIntDlyStg2);
  3377. }
  3378. /* Scan the pad delay */
  3379. AUTOK_PRINT("CMD pad delay scan range: [%d, %d]\r\n", CMDDlyScanS, CMDDlyScanE);
  3380. data.raw_data = 0;
  3381. for (m = CMDDlyScanS; m < CMDDlyScanE; m++) {
  3382. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  3383. for (cnt = 0; cnt < (AUTOK_CMD_TIMES * 5); cnt++) {
  3384. if (autok_cmd_test(host) != E_RESULT_PASS) {
  3385. /* 0 means pass */
  3386. data.raw_data |= (1 << m);
  3387. break;
  3388. }
  3389. }
  3390. }
  3391. memset(&CMDPadScan, 0, sizeof(CMDPadScan));
  3392. CMDPadScan.RawData = data.raw_data;
  3393. data.score = autok_simple_score(CMDPadScan.RawData);
  3394. AUTOK_PRINT("CMD delay scan result: \r\n");
  3395. AUTOK_PRINT("%s edge %s score=%d\r\n", CMDPadScanEdge ? "Falling" : "Rising",
  3396. g_tune_result_str, data.score);
  3397. autok_check_rawd_style(&CMDPadScan, 0);
  3398. /* Caculate and select the suitable gear */
  3399. if (stg == TUNING_STG1) {
  3400. /* Usually, It should be have 1 fail region only */
  3401. if (CMDPadScan.Reg1Cnt && (!CMDPadScan.BoundReg2_S) && !fCMDPadFullRange) {
  3402. CMDDlySel = CMDPadScan.BoundReg1_S +
  3403. (CMDPadScan.BoundReg1_E - CMDPadScan.BoundReg1_S) / 2;
  3404. }
  3405. /* After scanning full range, the pad transition should be firstly come out */
  3406. else if (CMDPadScan.Reg1Cnt && fCMDPadFullRange) {
  3407. CMDDlySel = CMDPadScan.BoundReg1_S +
  3408. (CMDPadScan.BoundReg1_E - CMDPadScan.BoundReg1_S) / 2;
  3409. }
  3410. /*
  3411. * No fail region found.
  3412. * It means the margin is not enough at the beginning already,
  3413. * Maybe the CMD and RDAT timing differ too much, check ETT now.
  3414. */
  3415. else if (!CMDPadScan.Reg1Cnt && !CMDPadScan.BoundReg2_S &&
  3416. (data.score == 32) && !fCMDPadFullRange) {
  3417. AUTOK_PRINT
  3418. ("[W] Can NOT find the best CMD pad delay, the timing between CMD & RDAT diff too much!\r\n");
  3419. /*
  3420. * We could NOT find fail region, just because
  3421. * the init gear is too small or too large resulting that the left
  3422. * range or right range is compressed
  3423. */
  3424. #if 0
  3425. if ((PadDlyCMDRef < (AUTOK_CMD_SCAN_RANGE_STG1 / 2)) ||
  3426. ((31 - PadDlyCMDRef) < (AUTOK_CMD_SCAN_RANGE_STG1 / 2))) {
  3427. CMDDlySel = PadDlyCMDRef;
  3428. fCMDPadLatchMid = 0;
  3429. } else {
  3430. AUTOK_PRINT("CMD pad tune should NOT goto here!\r\n");
  3431. #ifdef AUTOK_DEBUG
  3432. AUTOK_ERR();
  3433. #else
  3434. goto exit;
  3435. #endif
  3436. }
  3437. #else
  3438. /* Try again for full range */
  3439. fCMDPadFullRange = 1;
  3440. AUTOK_PRINT("[NOTICE]Try again for full range!\r\n");
  3441. goto ReTuneCMDPad;
  3442. #endif
  3443. }
  3444. /* If still can NOT find fail region after full range scan */
  3445. else if (!CMDPadScan.Reg1Cnt && !CMDPadScan.BoundReg2_S &&
  3446. (data.score == 32) && fCMDPadFullRange) {
  3447. AUTOK_PRINT
  3448. ("[W]Still can NOT find the best CMD pad delay! Just select the middle score!\r\n");
  3449. CMDEdgeSel = 1;
  3450. CMDDlySel = 15;
  3451. } else if (CMDPadScan.Reg2Cnt
  3452. && ((CMDPadScan.BoundReg2_E - CMDPadScan.BoundReg1_S) <= 6)) {
  3453. AUTOK_PRINT
  3454. ("[W] Find holes in fail region when scan CMD pad delay!\r\n");
  3455. CMDDlySel = CMDPadScan.BoundReg1_S +
  3456. (CMDPadScan.BoundReg2_E - CMDPadScan.BoundReg1_S) / 2;
  3457. } else {
  3458. AUTOK_PRINT("[ERR] Undefined scenario when scan CMD pad delay!!!\r\n");
  3459. #ifdef AUTOK_DEBUG
  3460. AUTOK_ERR();
  3461. #else
  3462. goto exit;
  3463. #endif
  3464. }
  3465. AUTOK_PRINT("CMD pad delay tune done: CMDDlySet=%d, CMDDlyRefRDAT=%d\r\n",
  3466. CMDDlySel, PadDlyCMDRef);
  3467. } else if (stg == TUNING_STG2) {
  3468. /* Usually, It should be have 1 fail region only */
  3469. if (CMDPadScan.Reg1Cnt && (!CMDPadScan.BoundReg2_S)) {
  3470. if (CMDPadScanEdge) {
  3471. CMDDlySel = CMDPadScan.BoundReg1_S +
  3472. (CMDPadScan.BoundReg1_E - CMDPadScan.BoundReg1_S) / 2;
  3473. } else {
  3474. CMDIntPassWin = pad_delay_period_cycle - CMDPadScan.Reg1Cnt;
  3475. CMDIntPassWin /= 2;
  3476. if (CMDPadScan.Reg1Cnt && !CMDPadScan.BoundReg1_E
  3477. && CMDPadScan.BoundReg1_S)
  3478. CMDPadScan.BoundReg1_E = 31;
  3479. /* Considering the lower frequency */
  3480. if (CMDIntPassWin >= 32) {
  3481. /* Try to select the best margin */
  3482. if ((31 - CMDPadScan.BoundReg1_E) == CMDPadScan.BoundReg1_S)
  3483. sel = 0;
  3484. else if ((31 - CMDPadScan.BoundReg1_E) >
  3485. CMDPadScan.BoundReg1_S) {
  3486. if ((31 - CMDPadScan.BoundReg1_E -
  3487. CMDPadScan.BoundReg1_S) >
  3488. AUTOK_TUNING_INACCURACY)
  3489. sel = 31;
  3490. else
  3491. sel = 0;
  3492. } else if ((31 - CMDPadScan.BoundReg1_E) <=
  3493. CMDPadScan.BoundReg1_S)
  3494. sel = 0;
  3495. } else if ((CMDIntPassWin > 16) && (CMDIntPassWin < 32)) {
  3496. /* We can find the best margin here */
  3497. if (CMDPadScan.BoundReg1_E < (32 - CMDIntPassWin)) {
  3498. CMDDlySel =
  3499. CMDPadScan.BoundReg1_E + CMDIntPassWin - 1;
  3500. } else if (CMDPadScan.BoundReg1_S > CMDIntPassWin) {
  3501. CMDDlySel = CMDPadScan.BoundReg1_S - CMDIntPassWin;
  3502. }
  3503. /* Can NOT find best margin, but try best */
  3504. else if ((CMDPadScan.BoundReg1_E >= (32 - CMDIntPassWin)) &&
  3505. (CMDPadScan.BoundReg1_S <= CMDIntPassWin)) {
  3506. /* Try to select the best margin */
  3507. if ((31 - CMDPadScan.BoundReg1_E) ==
  3508. CMDPadScan.BoundReg1_S)
  3509. CMDDlySel = 0;
  3510. else if ((31 - CMDPadScan.BoundReg1_E) >
  3511. CMDPadScan.BoundReg1_S) {
  3512. if ((31 - CMDPadScan.BoundReg1_E -
  3513. CMDPadScan.BoundReg1_S) >
  3514. AUTOK_TUNING_INACCURACY)
  3515. CMDDlySel = 31;
  3516. else
  3517. CMDDlySel = 0;
  3518. } else if ((31 - CMDPadScan.BoundReg1_E) <=
  3519. CMDPadScan.BoundReg1_S)
  3520. CMDDlySel = 0;
  3521. } else
  3522. AUTOK_PRINT
  3523. ("[ERR]Should NOT goto here in CMD pad scan!!!\r\n");
  3524. } else {
  3525. if (CMDPadScan.BoundReg1_E < CMDIntPassWin) {
  3526. CMDDlySel = CMDPadScan.BoundReg1_E + CMDIntPassWin;
  3527. } else if (CMDPadScan.BoundReg1_S > CMDIntPassWin) {
  3528. CMDDlySel = CMDPadScan.BoundReg1_S - CMDIntPassWin;
  3529. } else {
  3530. if ((CMDPadScan.BoundReg1_E + CMDIntPassWin) >= 32)
  3531. sel = 0;
  3532. else
  3533. sel =
  3534. CMDPadScan.BoundReg1_E + CMDIntPassWin;
  3535. }
  3536. }
  3537. }
  3538. }
  3539. /*
  3540. * No fail region found.
  3541. * It means the temperature shift on stage2 exceeds our expectation,
  3542. * So, we should select as rising edge, and re-tune again
  3543. */
  3544. else if ((!CMDPadScan.Reg1Cnt) && (!CMDPadScan.BoundReg2_S) &&
  3545. (data.score == 32) && CMDPadScanEdge) {
  3546. AUTOK_PRINT
  3547. ("[W] Temperature shift on stage2 exceeds our expectation! Scan rising edge again!\r\n");
  3548. CMDPadScanEdge = 0;
  3549. goto ReTuneCMDPad;
  3550. }
  3551. /* The scan can NOT cover the boundary */
  3552. else if ((data.score == 32) && (((PadDlyCMDRef - CMDDlyScanS) < CMDScanRangeStg2) ||
  3553. ((CMDDlyScanE - PadDlyCMDRef) <
  3554. CMDScanRangeStg2))) {
  3555. AUTOK_PRINT
  3556. ("[W] Can NOT find the suitable CMD pad delay! the temp shift is too much!!!\r\n");
  3557. CMDDlySel = PadDlyCMDRef;
  3558. rCmdTuneRes.fTimingShiftLarge = 1;
  3559. } else {
  3560. AUTOK_PRINT("[ERR] Undefined scenario when scan CMD pad delay!!!\r\n");
  3561. #ifdef AUTOK_DEBUG
  3562. AUTOK_ERR();
  3563. #else
  3564. goto exit;
  3565. #endif
  3566. }
  3567. AUTOK_PRINT("CMD pad delay tune done: CMDDlySet=%d, CMDDlySTG1=%d\r\n",
  3568. CMDDlySel, PadDlyCMDRef);
  3569. }
  3570. /* Set CMD pad delay */
  3571. rCmdTuneRes.CmdPadDly = CMDDlySel;
  3572. pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel = CMDDlySel;
  3573. msdc_autok_adjust_param(host, CMD_RD_DLY, &pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel,
  3574. MSDC_WRITE);
  3575. /* Set CMD edge, following read data edge */
  3576. pAutoKData[E_MSDC_IOCON_RSPL].data.sel = CMDEdgeSel;
  3577. msdc_autok_adjust_param(host, CMD_EDGE, &pAutoKData[E_MSDC_IOCON_RSPL].data.sel,
  3578. MSDC_WRITE);
  3579. #else /* AUTOK_CMD_TUNE_LEGACY */
  3580. reTuneCMDMatrix:
  3581. memset(CMDMatRaw, 0, sizeof(CMDMatRaw));
  3582. /* Select the CMD edge defaultly */
  3583. if (fCMDEdgeDefault)
  3584. sel = AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3585. else
  3586. sel = ~AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3587. pAutoKData[E_MSDC_IOCON_RSPL].data.sel = sel;
  3588. msdc_autok_adjust_param(host, CMD_EDGE, &sel, MSDC_WRITE);
  3589. AUTOK_PRINT("Start to scan CMD matrix(%s edge)...\r\n", sel ? "falling" : "rising");
  3590. /* Matrix Scanning... */
  3591. AUTOK_PRINT("CKGEN_MSDC_DLY \t PAD_TUNE_CMDRRDLY \t PAD_TUNE_CMDRDLY \r\n");
  3592. for (x = 0; x < SCALE_CMDMAT_RSP_DLY_SEL; x++) {
  3593. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &x, MSDC_WRITE);
  3594. for (m = 0; m < SCALE_PAD_TUNE_CMDRDLY; m++) {
  3595. msdc_autok_adjust_param(host, CMD_RD_DLY, &m, MSDC_WRITE);
  3596. for (cnt = 0; cnt < AUTOK_CMD_TIMES; cnt++) {
  3597. if (autok_cmd_test(host) != E_RESULT_PASS) {
  3598. /* 0 means pass */
  3599. CMDMatRaw[x] |= (1 << m);
  3600. break;
  3601. }
  3602. }
  3603. }
  3604. autok_simple_score(CMDMatRaw[x]);
  3605. AUTOK_PRINT("%02d \t %02d \t %s\r\n", pRDTuneRes->CKGenSel, x, g_tune_result_str);
  3606. }
  3607. /* Find the characteristic points */
  3608. memset(&CMDMatChar, 0, sizeof(CMDMatChar));
  3609. autok_check_cmd_matrix(CMDMatRaw, SCALE_PAD_TUNE_CMDRDLY,
  3610. SCALE_CMDMAT_RSP_DLY_SEL, &CMDMatChar);
  3611. /* In case of T/2 point found */
  3612. if (CMDMatChar.pad_trans_cnt1) {
  3613. /* select CMD pad delay */
  3614. CMDDlySel = CMDMatChar.pad_trans_s1 + CMDMatChar.pad_trans_cnt1 / 2;
  3615. rCmdTuneRes.CmdPadDly = CMDDlySel;
  3616. /* select CMD edge */
  3617. if (fCMDEdgeDefault)
  3618. CMDEdgeSel = ~AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3619. else
  3620. CMDEdgeSel = AUTOK_CMD_EDGE_MATRIX_SCAN & 0x01;
  3621. }
  3622. /* In case of NOT found */
  3623. else {
  3624. /* Swtich to another edge */
  3625. if (fCMDEdgeDefault) {
  3626. fCMDEdgeDefault = 0;
  3627. AUTOK_PRINT("Swtich to another edge...\r\n");
  3628. goto reTuneCMDMatrix;
  3629. }
  3630. /* Anyway, we can NOT find the transition boundary */
  3631. else {
  3632. AUTOK_PRINT("can NOT find the transition boundary\r\n");
  3633. CMDDlySel = 15;
  3634. msdc_autok_adjust_param(host, CMD_EDGE, &CMDEdgeSel, MSDC_READ);
  3635. }
  3636. }
  3637. /* Set CMD pad delay */
  3638. pAutoKData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel = CMDDlySel;
  3639. msdc_autok_adjust_param(host, CMD_RD_DLY, &CMDDlySel, MSDC_WRITE);
  3640. /* Set CMD edge */
  3641. pAutoKData[E_MSDC_IOCON_RSPL].data.sel = CMDEdgeSel;
  3642. msdc_autok_adjust_param(host, CMD_EDGE, &CMDEdgeSel, MSDC_WRITE);
  3643. AUTOK_PRINT("CMD pad delay tune done: CMDDlySet=%d, CMDEdgeSet=%s\r\n",
  3644. CMDDlySel, CMDEdgeSel ? "falling" : "rising");
  3645. #endif
  3646. /*
  3647. * Step2 :
  3648. * Tune the CMD internal delay, then
  3649. */
  3650. if (stg == TUNING_STG1) {
  3651. #ifdef AUTOK_CMD_TUNE_LEGACY
  3652. /*
  3653. * Prepare the internal delay for stage2,
  3654. * if we can not select the best margin for internal boundary,
  3655. * just retry to scan and find it
  3656. */
  3657. if (((pAutoKData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel == 0)
  3658. && pPadDlyRes->fCMDIntDlyConf) || !fCMDPadLatchMid
  3659. || !pPadDlyRes->fCMDIntDlyConf)
  3660. fCMDIntDlyScan = 1;
  3661. #else
  3662. fCMDIntDlyScan = 1;
  3663. #endif
  3664. } else if (stg == TUNING_STG2)
  3665. fCMDIntDlyScan = 1;
  3666. if (fCMDIntDlyScan) {
  3667. data.raw_data = 0;
  3668. /* Scan the pad delay */
  3669. for (m = 0; m < SCALE_CMD_RSP_DLY_SEL; m++) {
  3670. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &m, MSDC_WRITE);
  3671. for (cnt = 0; cnt < (AUTOK_CMD_TIMES * 5); cnt++) {
  3672. if (autok_cmd_test(host) != E_RESULT_PASS) {
  3673. /* 0 means pass */
  3674. data.raw_data |= (1 << m);
  3675. break;
  3676. }
  3677. }
  3678. }
  3679. memset(&CMDPadScan, 0, sizeof(CMDPadScan));
  3680. CMDPadScan.RawData = data.raw_data;
  3681. data.score = autok_simple_score(CMDPadScan.RawData);
  3682. AUTOK_PRINT("Int CMD scan %s, score=%d\r\n", g_tune_result_str, data.score);
  3683. autok_check_rawd_style(&CMDPadScan, 0);
  3684. if (data.score == 32) {
  3685. sel = 15;
  3686. pAutoKData[E_MSDC_CMD_INT_MARGIN].data.sel = AUTOK_SKIP_CMDTUNE_ON_STG2;
  3687. } else {
  3688. /* Calculate the pass window */
  3689. CMDIntPassWin = pad_delay_period_cycle - CMDPadScan.Reg1Cnt;
  3690. CMDIntPassWin /= 2;
  3691. if (CMDPadScan.Reg1Cnt && !CMDPadScan.BoundReg1_E && CMDPadScan.BoundReg1_S)
  3692. CMDPadScan.BoundReg1_E = 31;
  3693. /* Considering the lower frequency */
  3694. if (CMDIntPassWin >= 32) {
  3695. /* Try to select the best margin */
  3696. if ((31 - CMDPadScan.BoundReg1_E) == CMDPadScan.BoundReg1_S)
  3697. sel = 0;
  3698. else if ((31 - CMDPadScan.BoundReg1_E) > CMDPadScan.BoundReg1_S) {
  3699. if ((31 - CMDPadScan.BoundReg1_E - CMDPadScan.BoundReg1_S) >
  3700. AUTOK_TUNING_INACCURACY)
  3701. sel = 31;
  3702. else
  3703. sel = 0;
  3704. } else if ((31 - CMDPadScan.BoundReg1_E) <= CMDPadScan.BoundReg1_S)
  3705. sel = 0;
  3706. } else if ((CMDIntPassWin > 16) && (CMDIntPassWin < 32)) {
  3707. /* We can find the best margin here */
  3708. if (CMDPadScan.BoundReg1_E < (32 - CMDIntPassWin))
  3709. sel = CMDPadScan.BoundReg1_E + CMDIntPassWin - 1;
  3710. else if (CMDPadScan.BoundReg1_S > CMDIntPassWin)
  3711. sel = CMDPadScan.BoundReg1_S - CMDIntPassWin;
  3712. /* Can NOT find best margin, but try best */
  3713. else if ((CMDPadScan.BoundReg1_E >= (32 - CMDIntPassWin)) &&
  3714. (CMDPadScan.BoundReg1_S <= CMDIntPassWin)) {
  3715. AUTOK_PRINT
  3716. ("[NOTICE]It is NOT the best margin(CMD_INT) but try best\r\n");
  3717. /* Try to select the best margin */
  3718. if ((31 - CMDPadScan.BoundReg1_E) == CMDPadScan.BoundReg1_S)
  3719. sel = 0;
  3720. else if ((31 - CMDPadScan.BoundReg1_E) >
  3721. CMDPadScan.BoundReg1_S) {
  3722. if ((31 - CMDPadScan.BoundReg1_E -
  3723. CMDPadScan.BoundReg1_S) >
  3724. AUTOK_TUNING_INACCURACY)
  3725. sel = 31;
  3726. else
  3727. sel = 0;
  3728. } else if ((31 - CMDPadScan.BoundReg1_E) <=
  3729. CMDPadScan.BoundReg1_S)
  3730. sel = 0;
  3731. if (!sel)
  3732. pAutoKData[E_MSDC_CMD_INT_MARGIN].data.sel =
  3733. CMDPadScan.BoundReg1_S;
  3734. else
  3735. pAutoKData[E_MSDC_CMD_INT_MARGIN].data.sel =
  3736. 31 - CMDPadScan.BoundReg1_E;
  3737. } else {
  3738. AUTOK_PRINT
  3739. ("[ERR]Should NOT goto here in CMD internal scan!!!\r\n");
  3740. #ifdef AUTOK_DEBUG
  3741. AUTOK_ERR();
  3742. #else
  3743. goto exit;
  3744. #endif
  3745. }
  3746. } else {
  3747. if (CMDPadScan.BoundReg1_E < CMDIntPassWin) {
  3748. sel = CMDPadScan.BoundReg1_E + CMDIntPassWin;
  3749. } else if (CMDPadScan.BoundReg1_S > CMDIntPassWin) {
  3750. sel = CMDPadScan.BoundReg1_S - CMDIntPassWin;
  3751. } else {
  3752. if ((CMDPadScan.BoundReg1_E + CMDIntPassWin) >= 32)
  3753. sel = 0;
  3754. else
  3755. sel = CMDPadScan.BoundReg1_E + CMDIntPassWin;
  3756. pAutoKData[E_MSDC_CMD_INT_MARGIN].data.sel =
  3757. CMDPadScan.BoundReg1_S;
  3758. }
  3759. }
  3760. }
  3761. rCmdTuneRes.CmdIntDly = sel;
  3762. pAutoKData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel = sel;
  3763. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY,
  3764. &pAutoKData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel, MSDC_WRITE);
  3765. } else {
  3766. rCmdTuneRes.CmdIntDly = pAutoKData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel;
  3767. AUTOK_PRINT("CMD internal delay(%d) is determined by former scan\r\n",
  3768. rCmdTuneRes.CmdIntDly);
  3769. }
  3770. AUTOK_PRINT("CMD Internal delay tune done: %d\r\n", rCmdTuneRes.CmdIntDly);
  3771. rCmdTuneRes.fRetOk = 1;
  3772. #if defined(AUTOK_CMD_TUNE_LEGACY) || !defined(AUTOK_DEBUG)
  3773. exit:
  3774. #endif
  3775. return rCmdTuneRes;
  3776. }
  3777. static AUTOK_RD_TUNE_RES_T
  3778. autok_tune_rd(struct msdc_host *host,
  3779. E_AUTOK_TUNING_STAGE stg,
  3780. U_AUTOK_INTERFACE_DATA *pAutoKData,
  3781. P_AUTOK_CYC_SCAN_RES_T pPadDlyRes, P_AUTOK_CMD_TUNE_RES_T pCmdTuneRes)
  3782. {
  3783. int ck, CKLastLBound = 0, bound_diff;
  3784. unsigned int LeftBoundCnt = 0, CKGENLeftBound[4];
  3785. unsigned int FBoundFound = 0, CKGENFBound[2];
  3786. unsigned int LBoundPad = 0, LBoundCKGEN = 0;
  3787. unsigned int FBoundCnt = 0, FBoundCKGEN = 0, FBoundLMargin = 0, FBoundLDeMar = 0;
  3788. unsigned int IntBound = 0;
  3789. unsigned int DataPassWin, DataMargin;
  3790. unsigned int RealCKGEN, RealPadDelay;
  3791. unsigned int RBoundPad = 0;
  3792. unsigned int RDlyRefCMD, RDlyRefSTG1;
  3793. unsigned int baseCKGEN = 0;
  3794. unsigned int max_LBoundCnt = 0, LDeMarMin;
  3795. unsigned int fUnEnoughRightMar = 0;
  3796. #if defined(MT6582LTE)
  3797. unsigned int TinyMar = 0;
  3798. #endif
  3799. unsigned int m, x;
  3800. unsigned int sel = 0;
  3801. int reTuneCmd = 0;
  3802. unsigned int pad_delay_period_cycle = 0;
  3803. unsigned int range_max;
  3804. unsigned int CKGENMax = AUTOK_CKGEN_ALLOW_MAX;
  3805. S_AUTOK_CMD_DLY data;
  3806. E_RESULT_TYPE res;
  3807. AUTOK_RD_TUNE_RES_T rRdTuneRes;
  3808. AUTOK_PRINT("=====autok_tune_rd=====\r\n");
  3809. /* Initialize parameters */
  3810. memset(&rRdTuneRes, 0, sizeof(rRdTuneRes));
  3811. memset(CKGENFBound, 0, sizeof(CKGENFBound));
  3812. #ifdef AUTOK_RDAT_ACC
  3813. /* If do NOT find the hole CK, just equal to the cycle */
  3814. if (!pPadDlyRes->fHoleCK)
  3815. pPadDlyRes->HoleCK = pPadDlyRes->CKGenPeriodLen;
  3816. if (pPadDlyRes->fFBound) {
  3817. range_max = MAX_GET(pPadDlyRes->FBoundCK, pPadDlyRes->HoleCK);
  3818. FBoundFound = 1;
  3819. CKGENFBound[0] = pPadDlyRes->FBoundCK;
  3820. } else
  3821. range_max = pPadDlyRes->CKGenPeriodLen;
  3822. /* Determine the allowed maximum CKGEN */
  3823. pPadDlyRes->HoleCK--;
  3824. CKGENMax = MIN_GET(CKGENMax, pPadDlyRes->HoleCK);
  3825. pPadDlyRes->HoleCK++;
  3826. AUTOK_PRINT("MAX allow CKGEN:%d, Hole CKGEN:%d\r\n", CKGENMax, pPadDlyRes->HoleCK);
  3827. AUTOK_PRINT("RDAT pattern %s found(CKGEN=%d)\r\n",
  3828. pPadDlyRes->fFBound ? "Full Bound" : "Left Bound",
  3829. pPadDlyRes->fFBound ? pPadDlyRes->FBoundCK : range_max);
  3830. #else
  3831. range_max = pPadDlyRes->CKGenPeriodLen;
  3832. #endif
  3833. if (stg == TUNING_STG1) {
  3834. pad_delay_period_cycle = pPadDlyRes->PadDlyPeriodLen;
  3835. AUTOK_PRINT("Read Tune, Pad delays per 1 cycle: %d\r\n", pad_delay_period_cycle);
  3836. AUTOK_PRINT("CKGEN_MSDC_DLY \t PAD_TUNE_DATRDDLY \r\n");
  3837. /*
  3838. * Pad delay is confirmed now,
  3839. * And scan read raw data for 1T
  3840. */
  3841. for (ck = range_max; ck >= 0; ck--) {
  3842. autok_ckg_data[range_max - ck].readScore =
  3843. autok_simple_score(autok_rdata_scan[range_max - ck].RawData);
  3844. AUTOK_PRINT("%02d \t %02d \t %s %s\r\n", range_max - ck,
  3845. autok_ckg_data[range_max - ck].readScore, g_tune_result_str,
  3846. autok_rdata_scan[range_max -
  3847. ck].fInvalidCKGEN ? "(Invalid)" : "");
  3848. if (!autok_rdata_scan[ck].fInvalidCKGEN && !FBoundFound) {
  3849. /* Found the left pad boundary */
  3850. if ((autok_rdata_scan[ck].BoundReg1_S == 0)
  3851. && autok_rdata_scan[ck].Reg1Cnt) {
  3852. if (LeftBoundCnt <
  3853. (sizeof(CKGENLeftBound) / sizeof(unsigned int)))
  3854. CKGENLeftBound[LeftBoundCnt++] = ck;
  3855. /* Record the CKGEN of Max. left pad boundary */
  3856. if (autok_rdata_scan[ck].Reg1Cnt > max_LBoundCnt) {
  3857. max_LBoundCnt = autok_rdata_scan[ck].Reg1Cnt;
  3858. CKLastLBound = ck;
  3859. }
  3860. }
  3861. /* Found the full boundary */
  3862. if ((autok_rdata_scan[ck].BoundReg1_S > 0) &&
  3863. (autok_rdata_scan[ck].BoundReg1_E > 0) &&
  3864. (autok_rdata_scan[ck].Reg1Cnt > AUTOK_RDAT_FBOUND_TH) &&
  3865. (FBoundFound < (sizeof(CKGENFBound) / sizeof(unsigned int)))) {
  3866. if (LeftBoundCnt) {
  3867. if (ck < CKGENLeftBound[LeftBoundCnt - 1])
  3868. CKGENFBound[FBoundFound++] = ck;
  3869. } else
  3870. CKGENFBound[FBoundFound++] = ck;
  3871. }
  3872. }
  3873. }
  3874. if (LeftBoundCnt) {
  3875. x = 0;
  3876. findLBound:
  3877. /* Record the left boundary */
  3878. LBoundPad = autok_rdata_scan[CKGENLeftBound[x]].BoundReg1_E;
  3879. LBoundCKGEN = CKGENLeftBound[x];
  3880. if ((LeftBoundCnt >= 2) && (x < (LeftBoundCnt - 1))) {
  3881. /* Abnormal left boundary, abandon it */
  3882. if (autok_rdata_scan[CKGENLeftBound[x]].BoundReg1_E >
  3883. autok_rdata_scan[CKGENLeftBound[x + 1]].BoundReg1_E) {
  3884. x++;
  3885. goto findLBound;
  3886. }
  3887. bound_diff =
  3888. ABS_DIFF(autok_rdata_scan[CKGENLeftBound[x]].BoundReg1_E,
  3889. autok_rdata_scan[CKGENLeftBound[x + 1]].BoundReg1_E);
  3890. if (AUTOK_TUNING_INACCURACY < ABS_DIFF(bound_diff,
  3891. (ABS_DIFF
  3892. (CKGENLeftBound[x],
  3893. CKGENLeftBound[x +
  3894. 1]) *
  3895. DIV_CEIL_FUNC
  3896. (MIN_CLK_GEN_DELAY_IN_PS,
  3897. MIN_PAD_DELAY_IN_PS)))) {
  3898. AUTOK_PRINT
  3899. ("[W] The left Pad boundary NOT correct! Find again!\r\n");
  3900. x++;
  3901. goto findLBound;
  3902. }
  3903. } else {
  3904. AUTOK_PRINT("[W] The left Pad boundary may NOT be correct!\r\n");
  3905. }
  3906. /* Find internal boundary */
  3907. if ((autok_rdata_scan[CKGENLeftBound[LeftBoundCnt - 1]].BoundReg2_S > 0) &&
  3908. (autok_rdata_scan[CKGENLeftBound[LeftBoundCnt - 1]].BoundReg2_E == 0))
  3909. IntBound =
  3910. autok_rdata_scan[CKGENLeftBound[LeftBoundCnt - 1]].BoundReg2_S;
  3911. } else {
  3912. AUTOK_PRINT("[W] Can NOT find left pad boundary!!!\r\n");
  3913. }
  3914. /* Find the full pad boundary */
  3915. if (FBoundFound) {
  3916. FBoundCKGEN = CKGENFBound[0];
  3917. FBoundCnt = autok_rdata_scan[CKGENFBound[0]].BoundReg1_E -
  3918. autok_rdata_scan[CKGENFBound[0]].BoundReg1_S + 1;
  3919. FBoundLMargin = autok_rdata_scan[CKGENFBound[0]].BoundReg1_S;
  3920. if (FBoundFound == 2) {
  3921. bound_diff = autok_rdata_scan[CKGENFBound[1]].BoundReg1_E -
  3922. autok_rdata_scan[CKGENFBound[1]].BoundReg1_S + 1;
  3923. if (3 < ABS_DIFF(bound_diff, FBoundCnt))
  3924. AUTOK_PRINT
  3925. ("[W] The full Pad boundary may NOT correct!\r\n");
  3926. }
  3927. /* Update internal bound anyway */
  3928. /* if (!LeftBoundCnt && !IntBound) { */
  3929. if ((autok_rdata_scan[FBoundCKGEN].BoundReg2_S > 0) &&
  3930. (autok_rdata_scan[FBoundCKGEN].BoundReg2_E == 0))
  3931. IntBound = autok_rdata_scan[FBoundCKGEN].BoundReg2_S;
  3932. /* } */
  3933. } else if (LeftBoundCnt) {
  3934. AUTOK_PRINT("[W] Can NOT find full pad boundary directly!!!\r\n");
  3935. FBoundCnt = autok_rdata_scan[CKLastLBound].BoundReg1_E -
  3936. autok_rdata_scan[CKLastLBound].BoundReg1_S + 1;
  3937. FBoundCKGEN = SCALE_CKGEN_MSDC_DLY_SEL;
  3938. if (IntBound) {
  3939. m = IntBound;
  3940. m /= 4;
  3941. m += 2;
  3942. } else
  3943. /* m = 32; */
  3944. m = CKLastLBound;
  3945. if (m > CKLastLBound)
  3946. m = CKLastLBound;
  3947. AUTOK_PRINT("Cur. base full boundary count is %d\r\n", FBoundCnt);
  3948. AUTOK_PRINT("Find full boundary in CKGEN range [%d, %d]\r\n",
  3949. CKLastLBound - 1, CKLastLBound - m);
  3950. LDeMarMin = 5;
  3951. for (x = 1; x <= m; x++) {
  3952. if (!autok_rdata_scan[CKLastLBound - x].fInvalidCKGEN) {
  3953. if (autok_rdata_scan[CKLastLBound - x].BoundReg1_S) {
  3954. FBoundLDeMar =
  3955. ABS_DIFF(4 * x,
  3956. autok_rdata_scan[CKLastLBound -
  3957. x].BoundReg1_S);
  3958. if (FBoundLDeMar < LDeMarMin)
  3959. LDeMarMin = FBoundLDeMar;
  3960. }
  3961. }
  3962. }
  3963. FBoundLMargin = 0;
  3964. FBoundCnt += LDeMarMin;
  3965. FBoundLDeMar = LDeMarMin;
  3966. AUTOK_PRINT("[NOTICE] Full boundary is calculated by %d pad delay\r\n",
  3967. FBoundCnt);
  3968. }
  3969. AUTOK_PRINT
  3970. ("RData pattern found: LBoundCKGEN:%d, LBoundPad:%d, FBoundCKGEN:%d, FBoundCnt:%d, IntBound:%d\r\n",
  3971. LBoundCKGEN, LBoundPad, FBoundCKGEN, FBoundCnt, IntBound);
  3972. if (!FBoundCnt /* && !LBoundPad */) {
  3973. AUTOK_PRINT("[W] Can NOT find neither left and full pad boundary !\r\n");
  3974. /* Estimate the full boundary in case of low frequency */
  3975. if (freq_mhz <= 100) {
  3976. FBoundCnt = autok_rdat_fbound_th + 2;
  3977. FBoundCKGEN = pPadDlyRes->CKGenPeriodLen - 1;
  3978. FBoundLMargin = 31;
  3979. } else {
  3980. if (pPadDlyRes->fFBoundRefCMD) {
  3981. FBoundCnt = pPadDlyRes->FBoundCntRefCMD;
  3982. FBoundCKGEN = pPadDlyRes->FBoundCKRefCMD;
  3983. FBoundLMargin = pPadDlyRes->FBoundMidRefCMD - FBoundCnt / 2;
  3984. if (autok_rdata_scan[FBoundCKGEN].BoundReg1_S <=
  3985. FBoundLMargin)
  3986. IntBound =
  3987. autok_rdata_scan[FBoundCKGEN].BoundReg1_S;
  3988. AUTOK_PRINT
  3989. ("Ref@CMD: FBoundCKGEN:%d FBoundCnt:%d FBoundLMargin:%d IntBound:%d\r\n",
  3990. FBoundCKGEN, FBoundCnt, FBoundLMargin, IntBound);
  3991. } else
  3992. #ifdef AUTOK_DEBUG
  3993. AUTOK_ERR();
  3994. #else
  3995. goto exit;
  3996. #endif
  3997. }
  3998. }
  3999. /*
  4000. * Below is the approach of 'CKGEN' and 'RDAT pad delay' selection
  4001. *
  4002. * Step1 : Judge the initial timing, left boundary or right boundary?
  4003. * Step2 : Ignore CKGEN limitation firstly, calculated the ideal combination of
  4004. * CKGEN and RDAT pad delay
  4005. * Step3 : Consider the limitation of CKGEN, calculate the corresponding
  4006. * compensation to RDAT pad delay
  4007. * Step4 : Consider the internal boundary, fine tuning the RDAT pad delay to
  4008. * balance the margin between left and right
  4009. */
  4010. /* Calculate the pass window of data read */
  4011. DataPassWin = pad_delay_period_cycle - FBoundCnt;
  4012. /* AUTOK_PRINT("Read Data: Pass window= %d\r\n", DataPassWin);//Light Debug */
  4013. ChngBaseCK:
  4014. /* In case of Left boundary at CKGEN = 0 */
  4015. if (!autok_rdata_scan[baseCKGEN].BoundReg1_S &&
  4016. ((autok_rdata_scan[baseCKGEN].BoundReg1_E
  4017. && (autok_rdata_scan[baseCKGEN].Reg1Cnt > 1))
  4018. || (!autok_rdata_scan[baseCKGEN].BoundReg1_E
  4019. && (autok_rdata_scan[baseCKGEN].Reg1Cnt == 1)))) {
  4020. if (autok_rdata_scan[baseCKGEN].BoundReg2_S)
  4021. DataMargin = (autok_rdata_scan[baseCKGEN].BoundReg2_S -
  4022. autok_rdata_scan[baseCKGEN].BoundReg1_E) / 2;
  4023. else
  4024. DataMargin = (31 - autok_rdata_scan[baseCKGEN].BoundReg1_E) / 2;
  4025. AUTOK_PRINT
  4026. ("Left boundary found at initial timing(PWin:%d vs. Margin.Max:%d)\r\n",
  4027. DataMargin, DataPassWin / 2);
  4028. /*
  4029. * There are 2 scenarios here:
  4030. * 1. The internal boundary compress the margin
  4031. * 2. There is no internal boundary, but the pad delay is
  4032. * large by calculating
  4033. */
  4034. if (DataMargin < (DataPassWin / 2)) {
  4035. RealCKGEN =
  4036. baseCKGEN + DIV_CEIL_FUNC(((DataPassWin / 2) - DataMargin),
  4037. DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS,
  4038. MIN_PAD_DELAY_IN_PS));
  4039. /* Increase by 1 if the calculated increment is 0 */
  4040. if (RealCKGEN == baseCKGEN)
  4041. RealCKGEN = baseCKGEN + 1;
  4042. RealPadDelay =
  4043. autok_rdata_scan[baseCKGEN].BoundReg1_E + 1 +
  4044. (DataPassWin / 2) - (RealCKGEN -
  4045. baseCKGEN) *
  4046. DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS, MIN_PAD_DELAY_IN_PS);
  4047. }
  4048. /*
  4049. * The pass window margin is up to theoreical
  4050. * MAX. margin. It should happen seldomly
  4051. */
  4052. else {
  4053. RealCKGEN = baseCKGEN;
  4054. RealPadDelay =
  4055. autok_rdata_scan[baseCKGEN].BoundReg1_E + (DataPassWin / 2);
  4056. }
  4057. /* Exceeds the maximun CKGEN range */
  4058. if (RealCKGEN > CKGENMax) {
  4059. AUTOK_PRINT("[W] Exceeds the maximun CKGEN range[%d]\r\n",
  4060. CKGENMax);
  4061. AUTOK_PRINT("Maybe NOT the best point due to CKGEN limit!\r\n");
  4062. RealCKGEN = CKGENMax;
  4063. RealPadDelay =
  4064. autok_rdata_scan[baseCKGEN].BoundReg1_E + 1 +
  4065. (DataPassWin / 2) - (RealCKGEN -
  4066. baseCKGEN) *
  4067. DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS, MIN_PAD_DELAY_IN_PS);
  4068. }
  4069. /* Record the pad delay for CMD reference */
  4070. rRdTuneRes.PadDlyRefRD =
  4071. autok_rdata_scan[baseCKGEN].BoundReg1_E + (DataPassWin / 2) -
  4072. (RealCKGEN - baseCKGEN) * DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS,
  4073. MIN_PAD_DELAY_IN_PS);
  4074. }
  4075. /*
  4076. * Like Left boundary, but the timing
  4077. * is compressed by too large (output delay + internal boundary +
  4078. * transition boundary)
  4079. */
  4080. else if (!autok_rdata_scan[baseCKGEN].BoundReg1_S &&
  4081. !autok_rdata_scan[baseCKGEN].BoundReg1_E &&
  4082. (autok_rdata_scan[baseCKGEN].Reg1Cnt == 32)) {
  4083. AUTOK_PRINT
  4084. ("[W] Timing is compressed by too large output delay(CK=%d)\r\n",
  4085. baseCKGEN);
  4086. baseCKGEN++;
  4087. if (baseCKGEN <= CKGENMax)
  4088. goto ChngBaseCK;
  4089. else {
  4090. AUTOK_PRINT("[ERR] Exceeds the Max. CKGEN limitation!\r\n");
  4091. #ifdef AUTOK_DEBUG
  4092. AUTOK_ERR();
  4093. #else
  4094. goto exit;
  4095. #endif
  4096. }
  4097. }
  4098. /* In case of Right boundary at CKGEN = 0 */
  4099. else {
  4100. if (FBoundCnt && (FBoundCKGEN != SCALE_CKGEN_MSDC_DLY_SEL)) {
  4101. /* To make the result more accurate */
  4102. if (FBoundCKGEN > 2) {
  4103. if (FBoundLMargin)
  4104. RBoundPad =
  4105. FBoundCKGEN * MIN_CLK_GEN_DELAY_IN_PS /
  4106. MIN_PAD_DELAY_IN_PS + FBoundLMargin;
  4107. else
  4108. RBoundPad =
  4109. FBoundCKGEN * MIN_CLK_GEN_DELAY_IN_PS /
  4110. MIN_PAD_DELAY_IN_PS - FBoundLDeMar;
  4111. } else {
  4112. if (FBoundLMargin)
  4113. RBoundPad =
  4114. FBoundCKGEN *
  4115. DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS,
  4116. MIN_PAD_DELAY_IN_PS) +
  4117. FBoundLMargin;
  4118. else
  4119. RBoundPad =
  4120. FBoundCKGEN *
  4121. DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS,
  4122. MIN_PAD_DELAY_IN_PS) -
  4123. FBoundLDeMar;
  4124. }
  4125. } else if (LBoundPad)
  4126. RBoundPad =
  4127. LBoundCKGEN * DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS,
  4128. MIN_PAD_DELAY_IN_PS) + LBoundPad -
  4129. FBoundCnt;
  4130. AUTOK_PRINT
  4131. ("Right boundary found at initial timing(Rbound:%d vs. Margin.Max:%d)\r\n",
  4132. RBoundPad, DataPassWin / 2);
  4133. /*
  4134. * To make sure the pad boundary margin is enough firstly,
  4135. * then consider the internal boundary
  4136. */
  4137. if (RBoundPad >= (DataPassWin / 2)) {
  4138. RealCKGEN = (RBoundPad - (DataPassWin / 2)) /
  4139. DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS, MIN_PAD_DELAY_IN_PS);
  4140. RealPadDelay = RBoundPad - (DataPassWin / 2) -
  4141. RealCKGEN * DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS,
  4142. MIN_PAD_DELAY_IN_PS);
  4143. }
  4144. /* In case of un-enough margin */
  4145. else {
  4146. int LBound = 0, NewMargin = 0;
  4147. AUTOK_PRINT
  4148. ("[W] Un-enough margin to right boundary! Check layout!\r\n");
  4149. /*
  4150. * Considering a extreme case, the right boundary margin
  4151. * may be very little, at this situation, the CKGEN shifting
  4152. * should be considered
  4153. */
  4154. if (autok_rdata_scan[0].Reg1Cnt && autok_rdata_scan[0].Reg2Cnt) {
  4155. RBoundPad = autok_rdata_scan[0].BoundReg1_S;
  4156. LBound =
  4157. autok_rdata_scan[0].BoundReg2_S -
  4158. autok_rdata_scan[0].BoundReg1_E - 1;
  4159. } else if (autok_rdata_scan[0].Reg1Cnt
  4160. && !autok_rdata_scan[0].Reg2Cnt) {
  4161. RBoundPad = autok_rdata_scan[0].BoundReg1_S;
  4162. if (autok_rdata_scan[0].BoundReg1_E && !IntBound) {
  4163. LBound = 31 - autok_rdata_scan[0].BoundReg1_E;
  4164. } else if (!autok_rdata_scan[0].BoundReg1_E && IntBound) {
  4165. LBound =
  4166. (autok_rdata_scan[0].Reg1Cnt -
  4167. (32 - IntBound)) - FBoundCnt;
  4168. } else if (!autok_rdata_scan[0].BoundReg1_E && !IntBound) {
  4169. LBound = autok_rdata_scan[0].Reg1Cnt - FBoundCnt;
  4170. } else if (autok_rdata_scan[0].BoundReg1_E && IntBound) {
  4171. LBound =
  4172. IntBound - autok_rdata_scan[0].BoundReg1_E - 1;
  4173. }
  4174. } else
  4175. AUTOK_PRINT("[NOTICE] Might be the low freq case\r\n");
  4176. /* Calculate the new margin after CKGEN shifting */
  4177. NewMargin = LBound + CKGENMax *
  4178. DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS, MIN_PAD_DELAY_IN_PS);
  4179. AUTOK_PRINT("RBound:%d, LBound:%d, CKGEN shift Mar:%d\r\n",
  4180. RBoundPad, LBound, NewMargin);
  4181. if ((NewMargin / 2 > (int)0)
  4182. && (NewMargin / 2 >= (int)(RBoundPad + 2))) {
  4183. AUTOK_PRINT("Shifting CKGEN has more margin\r\n");
  4184. RealCKGEN = CKGENMax;
  4185. if (!IntBound)
  4186. IntBound = 32;
  4187. RealPadDelay = IntBound - NewMargin / 2 - 1;
  4188. } else {
  4189. RealCKGEN = 0;
  4190. RealPadDelay = 0;
  4191. fUnEnoughRightMar = 1;
  4192. }
  4193. }
  4194. if (RealCKGEN > CKGENMax) {
  4195. AUTOK_PRINT("[W] Exceeds the maximun CKGEN range[%d]\r\n",
  4196. CKGENMax);
  4197. AUTOK_PRINT("Maybe NOT the best point due to CKGEN limit!\r\n");
  4198. RealCKGEN = CKGENMax;
  4199. RealPadDelay = RBoundPad - (DataPassWin / 2) -
  4200. RealCKGEN * DIV_CEIL_FUNC(MIN_CLK_GEN_DELAY_IN_PS,
  4201. MIN_PAD_DELAY_IN_PS);
  4202. }
  4203. /* Record the pad delay for CMD reference */
  4204. rRdTuneRes.PadDlyRefRD = RealPadDelay;
  4205. }
  4206. if (!IntBound)
  4207. IntBound = 32;
  4208. /*
  4209. * In case of left boundary still,
  4210. * Select the middle score alternatively
  4211. */
  4212. if (autok_rdata_scan[RealCKGEN].Reg1Cnt && !autok_rdata_scan[RealCKGEN].BoundReg1_S) {
  4213. AUTOK_PRINT("Left boundary after shifting CKGEN\r\n");
  4214. AUTOK_PRINT("[W] Try to select the middle score!\r\n");
  4215. RealPadDelay = autok_rdata_scan[RealCKGEN].BoundReg1_E + 1 +
  4216. (IntBound - autok_rdata_scan[RealCKGEN].BoundReg1_E - 1) / 2;
  4217. #if defined(MT6582LTE)
  4218. TinyMar = (IntBound - autok_rdata_scan[RealCKGEN].BoundReg1_E - 1);
  4219. /*
  4220. * Tiny margin consideration,
  4221. * It should be 2 criteria :
  4222. * 1. Core ring < TT && IO ring < SS
  4223. * 2. Left bound && Internal bound co-exists
  4224. */
  4225. if (gfIOSS && gfCoreTT && (IntBound < 32)) {
  4226. gfTinyMar = 1;
  4227. pAutoKData[E_MSDC_F_TINY_MARGIN].data.sel = AUTOK_TINY_MAR_PAT;
  4228. /*
  4229. * In case of even margin number,
  4230. * select the middle by left shiftint 1 gear
  4231. */
  4232. if (!(TinyMar % 2))
  4233. RealPadDelay--;
  4234. if (TinyMar > AUTOK_TINY_MAR_TH_BTM) {
  4235. AUTOK_PRINT
  4236. ("[W] Tiny margin(%d), shift the pad delay(%d) to right\r\n",
  4237. TinyMar, RealPadDelay);
  4238. /*
  4239. * Shift to right but at least keeping
  4240. * 3 gears from the right boundary
  4241. */
  4242. if ((IntBound -
  4243. (RealPadDelay + AUTOK_TINY_MAR_R_SHIFT_MAX)) >=
  4244. (AUTOK_TINY_MAR_R_MAR_MIN + 1))
  4245. RealPadDelay += AUTOK_TINY_MAR_R_SHIFT_MAX;
  4246. else {
  4247. if ((IntBound - (AUTOK_TINY_MAR_R_MAR_MIN + 1)) >
  4248. RealPadDelay)
  4249. RealPadDelay =
  4250. IntBound - (AUTOK_TINY_MAR_R_MAR_MIN +
  4251. 1);
  4252. }
  4253. }
  4254. }
  4255. #endif
  4256. }
  4257. /* In case of right boundary by shifting CKGEN */
  4258. else if (!fUnEnoughRightMar) {
  4259. AUTOK_PRINT("Right boundary after shifting CKGEN\r\n");
  4260. /*
  4261. * If the right margin is compressed by too large
  4262. * internal boundary, scarify the left margin to make up.
  4263. * Otherwise, the right margin should be enough becuase
  4264. * the right boundary should be the transition boundary
  4265. * instead of internal boundary
  4266. */
  4267. if ((IntBound < 32)
  4268. && ((RealPadDelay >= autok_rdata_scan[RealCKGEN].BoundReg1_S)
  4269. || ((RealPadDelay < autok_rdata_scan[RealCKGEN].BoundReg1_S)
  4270. && ((autok_rdata_scan[RealCKGEN].BoundReg1_S - RealPadDelay) <=
  4271. (DataPassWin / 2 - AUTOK_TUNING_INACCURACY))))) {
  4272. AUTOK_PRINT
  4273. ("[W] The margin may NOT up to T/2 due to internal boundary\r\n");
  4274. if (IntBound > RealPadDelay) {
  4275. if ((IntBound - RealPadDelay + DataPassWin / 2) / 2 <
  4276. IntBound)
  4277. RealPadDelay =
  4278. IntBound - (IntBound - RealPadDelay +
  4279. DataPassWin / 2) / 2;
  4280. else
  4281. RealPadDelay = 0;
  4282. } else {
  4283. AUTOK_PRINT
  4284. ("[W] The internal boundary is too large!\r\n");
  4285. if ((DataPassWin / 2 + IntBound - RealPadDelay) / 2 <
  4286. IntBound)
  4287. RealPadDelay =
  4288. IntBound - (DataPassWin / 2 + IntBound -
  4289. RealPadDelay) / 2;
  4290. else
  4291. RealPadDelay = 0;
  4292. }
  4293. AUTOK_PRINT
  4294. ("Try to select the best margin(sacrifying the left margin)\r\n");
  4295. } else if (IntBound == 32) {
  4296. /* Transition boundary at the right */
  4297. if (autok_rdata_scan[RealCKGEN].Reg1Cnt &&
  4298. autok_rdata_scan[RealCKGEN].BoundReg1_S) {
  4299. if (RealPadDelay >=
  4300. (autok_rdata_scan[RealCKGEN].BoundReg1_S - 1) / 2)
  4301. RealPadDelay =
  4302. (autok_rdata_scan[RealCKGEN].BoundReg1_S -
  4303. 1) / 2;
  4304. }
  4305. /* No any boundary at all */
  4306. else {
  4307. if (RealPadDelay >= 16)
  4308. RealPadDelay = 16;
  4309. }
  4310. }
  4311. }
  4312. if (IntBound == 32)
  4313. IntBound = 0;
  4314. /* Double checks */
  4315. if (((RealPadDelay >= IntBound) && IntBound) || (RealPadDelay >= 32)) {
  4316. AUTOK_PRINT("[ERR] Invalid pad delay(%d): exceeds the range\r\n",
  4317. RealPadDelay);
  4318. #ifdef AUTOK_DEBUG
  4319. AUTOK_ERR();
  4320. #else
  4321. goto exit;
  4322. #endif
  4323. }
  4324. if (autok_rdata_scan[RealCKGEN].RawData & (unsigned int)(1 << RealPadDelay)) {
  4325. AUTOK_PRINT("[ERR] Invalid pad delay(%d): NOT at the pass window\r\n",
  4326. RealPadDelay);
  4327. #ifdef AUTOK_DEBUG
  4328. AUTOK_ERR();
  4329. #else
  4330. goto exit;
  4331. #endif
  4332. }
  4333. pAutoKData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel = RealPadDelay;
  4334. autok_ckg_data[RealCKGEN].readPadSel =
  4335. pAutoKData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel;
  4336. sel = RealCKGEN;
  4337. rRdTuneRes.CKGenSel = RealCKGEN;
  4338. AUTOK_PRINT("Read Data tune done: CKGEN=%d, Read pad=%d\r\n", sel,
  4339. autok_ckg_data[RealCKGEN].readPadSel);
  4340. goto APPLYSET;
  4341. } else if (stg == TUNING_STG2) {
  4342. data.raw_data = 0;
  4343. reTuneCmd = 0;
  4344. x = 0;
  4345. while (x < SCALE_PAD_TUNE_DATRDDLY && reTuneCmd == 0) {
  4346. msdc_autok_adjust_param(host, DAT_RD_DLY, &x, MSDC_WRITE);
  4347. for (m = 0; m < AUTOK_RDAT_TIMES; m++) {
  4348. res = autok_read_test(host);
  4349. if (res != E_RESULT_PASS) {
  4350. data.raw_data |= (1 << x);
  4351. if (autok_recovery(host)) {
  4352. AUTOK_PRINT
  4353. ("===tune read : error, fail to bring to tranfer status===\r\n");
  4354. goto exit;
  4355. }
  4356. if (res == E_RESULT_CMD_CRC) {
  4357. AUTOK_PRINT
  4358. ("[W]CMD CRC err @tuning read[%d %d],need to tune cmd again!!\r\n",
  4359. x, m);
  4360. reTuneCmd = 1;
  4361. }
  4362. break;
  4363. }
  4364. }
  4365. x++;
  4366. }
  4367. if (reTuneCmd == 1) {
  4368. AUTOK_PRINT("[ERR] CMD CRC error in tuning read!\r\n");
  4369. #ifdef AUTOK_DEBUG
  4370. AUTOK_ERR();
  4371. #else
  4372. goto exit;
  4373. #endif
  4374. }
  4375. memset(&autok_rdata_scan[0], 0, sizeof(AUTOK_RAWD_SCAN_T));
  4376. autok_rdata_scan[0].RawData = data.raw_data;
  4377. autok_select_range(data.raw_data, &sel);
  4378. data.score = autok_simple_score(data.raw_data);
  4379. AUTOK_PRINT("Read data scan %s, score=%d\r\n", g_tune_result_str, data.score);
  4380. autok_check_rawd_style(&autok_rdata_scan[0], 1);
  4381. /*
  4382. * In case CMD/RD have enough margin and timing varies NOT too much,
  4383. * Only in this scenario, CMD can be refered by RDAT
  4384. */
  4385. RDlyRefSTG1 = pAutoKData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel;
  4386. RDlyRefCMD = sel;
  4387. if (pCmdTuneRes->CmdPadDlyStg1Bak && !pCmdTuneRes->fTimingShiftLarge) {
  4388. if (pCmdTuneRes->CmdPadDly >= pCmdTuneRes->CmdPadDlyStg1Bak) {
  4389. RDlyRefCMD =
  4390. RDlyRefSTG1 + ABS_DIFF(pCmdTuneRes->CmdPadDly,
  4391. pCmdTuneRes->CmdPadDlyStg1Bak);
  4392. } else {
  4393. RDlyRefCMD =
  4394. RDlyRefSTG1 - ABS_DIFF(pCmdTuneRes->CmdPadDly,
  4395. pCmdTuneRes->CmdPadDlyStg1Bak);
  4396. }
  4397. AUTOK_PRINT("Read data pad delay Refered from CMD: %d\r\n", RDlyRefCMD);
  4398. if (!autok_rdata_scan[0].fInvalidCKGEN) {
  4399. if ((RDlyRefCMD >= sel) &&
  4400. ((autok_rdata_scan[0].Reg2Cnt
  4401. && !autok_rdata_scan[0].BoundReg2_E)
  4402. || (autok_rdata_scan[0].Reg1Cnt
  4403. && !autok_rdata_scan[0].BoundReg1_E)))
  4404. RDlyRefCMD = sel;
  4405. }
  4406. }
  4407. /* In case of margin is NOT enough */
  4408. else {
  4409. AUTOK_PRINT("[W]Read data margin is NOT enough! Check layout!!!\r\n");
  4410. if (!autok_rdata_scan[0].fInvalidCKGEN) {
  4411. if (!RDlyRefSTG1) {
  4412. if (autok_rdata_scan[0].BoundReg1_S * 2 >= data.score)
  4413. RDlyRefCMD = RDlyRefSTG1;
  4414. else
  4415. RDlyRefCMD = sel;
  4416. } else if (RDlyRefSTG1 < (autok_rdata_scan[0].BoundReg1_S / 2)) {
  4417. RDlyRefCMD = RDlyRefSTG1;
  4418. }
  4419. } else
  4420. RDlyRefCMD = sel;
  4421. }
  4422. AUTOK_PRINT("Read data pad delay tune done: %d\r\n", RDlyRefCMD);
  4423. pAutoKData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel = RDlyRefCMD;
  4424. }
  4425. APPLYSET:
  4426. if (stg == TUNING_STG1) {
  4427. /* Apply CKGEN */
  4428. pAutoKData[E_MSDC_CKGEN_MSDC_DLY_SEL].data.sel = sel;
  4429. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL,
  4430. &pAutoKData[E_MSDC_CKGEN_MSDC_DLY_SEL].data.sel,
  4431. MSDC_WRITE);
  4432. /* Apply RDAT pad delay */
  4433. pAutoKData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel = autok_ckg_data[sel].readPadSel;
  4434. msdc_autok_adjust_param(host, DAT_RD_DLY,
  4435. &pAutoKData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel, MSDC_WRITE);
  4436. } else if (stg == TUNING_STG2) {
  4437. msdc_autok_adjust_param(host, DAT_RD_DLY,
  4438. &pAutoKData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel, MSDC_WRITE);
  4439. }
  4440. rRdTuneRes.fRetOk = 1;
  4441. exit:
  4442. return rRdTuneRes;
  4443. }
  4444. static E_RESULT_TYPE autok_doub_chk_wdat_int_bound(struct msdc_host *host, unsigned int *pRaw)
  4445. {
  4446. #define AUTOK_WDAT_DOUBCHK_WIN (5)
  4447. unsigned int ckgen = 0, rdat_sel = 0;
  4448. unsigned int raw_data, chk_win = 0, chk_win_ex = 0, temp, idx;
  4449. unsigned int m, x;
  4450. int reTuneCmd = 0;
  4451. unsigned int w_data;
  4452. AUTOK_RAWD_SCAN_T rDatPattern;
  4453. E_RESULT_TYPE res = E_RESULT_ERR;
  4454. AUTOK_PRINT("Double check for WDAT internal boundary\r\n");
  4455. /* Defaultly, returns 0 */
  4456. *pRaw = 0;
  4457. /* Get initial parameters */
  4458. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL, &ckgen, MSDC_READ);
  4459. msdc_autok_adjust_param(host, DAT_RD_DLY, &rdat_sel, MSDC_READ);
  4460. raw_data = autok_rdata_scan[ckgen].RawData;
  4461. /* Gen the check window */
  4462. chk_win = 1 << rdat_sel;
  4463. temp = chk_win = chk_win << (AUTOK_WDAT_DOUBCHK_WIN / 2);
  4464. for (idx = 0; idx < AUTOK_WDAT_DOUBCHK_WIN; idx++)
  4465. chk_win |= (temp >> idx);
  4466. /* Make up the check window if NOT enough */
  4467. memset(&rDatPattern, 0, sizeof(rDatPattern));
  4468. rDatPattern.RawData = chk_win;
  4469. autok_check_rawd_style(&rDatPattern, 0);
  4470. if (rDatPattern.Reg1Cnt < AUTOK_WDAT_DOUBCHK_WIN) {
  4471. AUTOK_PRINT("Make up %d gears\r\n", AUTOK_WDAT_DOUBCHK_WIN - rDatPattern.Reg1Cnt);
  4472. if (!rDatPattern.BoundReg1_S)
  4473. chk_win |= (chk_win << (AUTOK_WDAT_DOUBCHK_WIN - rDatPattern.Reg1Cnt));
  4474. else if (rDatPattern.BoundReg1_S && !rDatPattern.BoundReg1_E)
  4475. chk_win |= (chk_win >> (AUTOK_WDAT_DOUBCHK_WIN - rDatPattern.Reg1Cnt));
  4476. }
  4477. /* Extend the check window by 1 gear, make sure the enough safe margin */
  4478. chk_win_ex = temp = chk_win;
  4479. chk_win_ex |= (temp << 1);
  4480. chk_win_ex |= (temp >> 1);
  4481. AUTOK_PRINT("CKGEN:%d, RDAT_SEL:%d, RAW:0x%X, CHK_WIN:0x%X, Ext.CHK_WIN:0x%X\r\n",
  4482. ckgen, rdat_sel, raw_data, chk_win, chk_win_ex);
  4483. /* Double check workable margin */
  4484. temp = raw_data & chk_win_ex;
  4485. if (temp) {
  4486. memset(&rDatPattern, 0, sizeof(rDatPattern));
  4487. rDatPattern.RawData = temp;
  4488. autok_check_rawd_style(&rDatPattern, 0);
  4489. /* The window compresses one side of boundary */
  4490. if (rDatPattern.Reg1Cnt && !rDatPattern.Reg2Cnt) {
  4491. /* The high bit is near from boundary */
  4492. if (rDatPattern.BoundReg1_S >= rdat_sel) {
  4493. temp |= (temp >> 1);
  4494. chk_win_ex ^= temp;
  4495. /* Check again */
  4496. chk_win = chk_win_ex;
  4497. chk_win_ex |= (chk_win_ex >> 1);
  4498. temp = raw_data & chk_win_ex;
  4499. if (temp)
  4500. chk_win &= (chk_win << 1);
  4501. }
  4502. /* The low bit is near from boundary */
  4503. else {
  4504. temp |= (temp << 1);
  4505. chk_win_ex ^= temp;
  4506. /* Check again */
  4507. chk_win = chk_win_ex;
  4508. chk_win_ex |= (chk_win_ex << 1);
  4509. temp = raw_data & chk_win_ex;
  4510. if (temp)
  4511. chk_win &= (chk_win >> 1);
  4512. }
  4513. }
  4514. /* The window compresses both sides of boundary */
  4515. else if (rDatPattern.Reg1Cnt && rDatPattern.Reg2Cnt) {
  4516. chk_win = chk_win_ex ^ temp;
  4517. temp = chk_win;
  4518. chk_win &= (temp >> 1);
  4519. chk_win &= (temp << 1);
  4520. }
  4521. }
  4522. memset(&rDatPattern, 0, sizeof(rDatPattern));
  4523. rDatPattern.RawData = chk_win;
  4524. autok_check_rawd_style(&rDatPattern, 0);
  4525. autok_simple_score(chk_win);
  4526. AUTOK_PRINT("Check window: %s [%d, %d]\r\n",
  4527. g_tune_result_str, rDatPattern.BoundReg1_S,
  4528. rDatPattern.BoundReg1_E ? rDatPattern.BoundReg1_E : 31);
  4529. /* Scan write data internal boundary */
  4530. for (idx = rDatPattern.BoundReg1_S;
  4531. idx <= (rDatPattern.BoundReg1_E ? rDatPattern.BoundReg1_E : 31); idx++) {
  4532. w_data = 0;
  4533. reTuneCmd = 0;
  4534. x = 0;
  4535. /* Set RDAT pad delay */
  4536. msdc_autok_adjust_param(host, DAT_RD_DLY, &idx, MSDC_WRITE);
  4537. while (x < SCALE_PAD_TUNE_DATWRDLY && reTuneCmd == 0) {
  4538. msdc_autok_adjust_param(host, DAT_WRD_DLY, &x, MSDC_WRITE);
  4539. for (m = 0; m < (AUTOK_WDAT_TIMES); m++) {
  4540. res = autok_write_test(host);
  4541. if (res != E_RESULT_PASS) {
  4542. w_data |= (1 << x);
  4543. if (autok_recovery(host)) {
  4544. AUTOK_PRINT
  4545. ("===tune write : error, fail to bring to tranfer status===\r\n");
  4546. goto exit;
  4547. }
  4548. if (res == E_RESULT_CMD_CRC) {
  4549. AUTOK_PRINT
  4550. ("[W]CMD CRC err@tuning write[%d %d],need to tune CMD again!!\r\n",
  4551. x, m);
  4552. reTuneCmd = 1;
  4553. }
  4554. break;
  4555. }
  4556. }
  4557. x++;
  4558. }
  4559. if (reTuneCmd == 1) {
  4560. AUTOK_PRINT("[Err] CMD CRC error in tuning write!!\r\n");
  4561. #ifdef AUTOK_DEBUG
  4562. AUTOK_ERR();
  4563. #else
  4564. goto exit;
  4565. #endif
  4566. }
  4567. if (autok_simple_score(w_data) < 32) {
  4568. if (idx < rdat_sel)
  4569. *pRaw = w_data >> ABS_DIFF(idx, rdat_sel);
  4570. else
  4571. *pRaw = w_data << ABS_DIFF(idx, rdat_sel);
  4572. AUTOK_PRINT("Internal transition boundary found! RDAT:%d\r\n", idx);
  4573. AUTOK_PRINT("%s\r\n", g_tune_result_str);
  4574. break;
  4575. }
  4576. }
  4577. res = E_RESULT_PASS;
  4578. exit:
  4579. /* Restore RDAT pad delay */
  4580. msdc_autok_adjust_param(host, DAT_RD_DLY, &rdat_sel, MSDC_WRITE);
  4581. return res;
  4582. }
  4583. static E_RESULT_TYPE autok_tune_wd(struct msdc_host *host, U_AUTOK_INTERFACE_DATA *pAutoKData)
  4584. {
  4585. unsigned int m, x;
  4586. unsigned int sel = 0;
  4587. unsigned int pad_delay_period_cycle = 0;
  4588. unsigned int WRIntPassWin;
  4589. unsigned int fDoubleChk = 0;
  4590. int reTuneCmd = 0;
  4591. S_AUTOK_CMD_DLY data;
  4592. AUTOK_RAWD_SCAN_T WRIntScan;
  4593. E_RESULT_TYPE res = E_RESULT_ERR;
  4594. AUTOK_PRINT("=====autok_tune_wd=====\r\n");
  4595. /* Initialize parameters */
  4596. pad_delay_period_cycle = pAutoKData[E_MSDC_PAD_DLY_PERIOD].data.sel;
  4597. AUTOK_PRINT("Write Tune, Pad delays per 1 cycle: %d\r\n", pad_delay_period_cycle);
  4598. data.raw_data = 0;
  4599. reTuneCmd = 0;
  4600. x = 0;
  4601. while (x < SCALE_PAD_TUNE_DATWRDLY && reTuneCmd == 0) {
  4602. msdc_autok_adjust_param(host, DAT_WRD_DLY, &x, MSDC_WRITE);
  4603. for (m = 0; m < (AUTOK_WDAT_TIMES * 5); m++) {
  4604. res = autok_write_test(host);
  4605. if (res != E_RESULT_PASS) {
  4606. data.raw_data |= (1 << x);
  4607. if (autok_recovery(host)) {
  4608. AUTOK_PRINT
  4609. ("===tune write : error, fail to bring to tranfer status===\r\n");
  4610. goto exit;
  4611. }
  4612. if (res == E_RESULT_CMD_CRC) {
  4613. AUTOK_PRINT
  4614. ("[W] CMD CRC error in tuning write[%d %d], need to tune command again!!\r\n",
  4615. x, m);
  4616. reTuneCmd = 1;
  4617. }
  4618. break;
  4619. }
  4620. }
  4621. x++;
  4622. }
  4623. if (reTuneCmd == 1) {
  4624. AUTOK_PRINT("[Err] CMD CRC error in tuning write!!\r\n");
  4625. #ifdef AUTOK_DEBUG
  4626. AUTOK_ERR();
  4627. #else
  4628. goto exit;
  4629. #endif
  4630. }
  4631. #if defined(MT6592LTE)
  4632. fDoubleChk = 1;
  4633. #endif
  4634. doubleChkWDAT:
  4635. memset(&WRIntScan, 0, sizeof(WRIntScan));
  4636. WRIntScan.RawData = data.raw_data;
  4637. data.score = autok_simple_score(WRIntScan.RawData);
  4638. autok_check_rawd_style(&WRIntScan, 0);
  4639. AUTOK_PRINT("CKGEN_MSDC_DLY \t PAD_TUNE_DATWDDLY \r\n");
  4640. AUTOK_PRINT("%d \t %d \t %s\r\n", pAutoKData[E_MSDC_CKGEN_MSDC_DLY_SEL].data.sel,
  4641. data.score, g_tune_result_str);
  4642. if (data.score == 32) {
  4643. sel = 15;
  4644. /* AUTOK_PRINT("Write Data: Large pass window since score==32\r\n");//Light Debug */
  4645. } else {
  4646. /* Calculate the pass window */
  4647. WRIntPassWin = pad_delay_period_cycle - WRIntScan.Reg1Cnt;
  4648. WRIntPassWin /= 2;
  4649. /* AUTOK_PRINT("Write Data: Pass window= %d\r\n", WRIntPassWin*2);//Light Debug */
  4650. if (WRIntScan.Reg1Cnt && !WRIntScan.BoundReg1_E && WRIntScan.BoundReg1_S)
  4651. WRIntScan.BoundReg1_E = 31;
  4652. /* Considering the lower frequency */
  4653. if (WRIntPassWin >= 32) {
  4654. /* Try to select the best margin */
  4655. if ((31 - WRIntScan.BoundReg1_E) == WRIntScan.BoundReg1_S)
  4656. sel = 0;
  4657. else if ((31 - WRIntScan.BoundReg1_E) > WRIntScan.BoundReg1_S) {
  4658. if ((31 - WRIntScan.BoundReg1_E - WRIntScan.BoundReg1_S) >
  4659. AUTOK_TUNING_INACCURACY)
  4660. sel = 31;
  4661. else
  4662. sel = 0;
  4663. } else if ((31 - WRIntScan.BoundReg1_E) <= WRIntScan.BoundReg1_S)
  4664. sel = 0;
  4665. } else if ((WRIntPassWin > 16) && (WRIntPassWin < 32)) {
  4666. /* We can find the best margin here */
  4667. if (WRIntScan.BoundReg1_E < (32 - WRIntPassWin))
  4668. sel = WRIntScan.BoundReg1_E + WRIntPassWin - 1;
  4669. else if (WRIntScan.BoundReg1_S > WRIntPassWin)
  4670. sel = WRIntScan.BoundReg1_S - WRIntPassWin;
  4671. /* Can NOT find best margin, but try best */
  4672. else if ((WRIntScan.BoundReg1_E >= (32 - WRIntPassWin)) &&
  4673. (WRIntScan.BoundReg1_S <= WRIntPassWin)) {
  4674. AUTOK_PRINT
  4675. ("[NOTICE]It is NOT the best margin(WR_INT) but try best\r\n");
  4676. /* Try to select the best margin */
  4677. if ((31 - WRIntScan.BoundReg1_E) == WRIntScan.BoundReg1_S)
  4678. sel = 0;
  4679. else if ((31 - WRIntScan.BoundReg1_E) > WRIntScan.BoundReg1_S) {
  4680. if ((31 - WRIntScan.BoundReg1_E - WRIntScan.BoundReg1_S) >
  4681. AUTOK_TUNING_INACCURACY)
  4682. sel = 31;
  4683. else
  4684. sel = 0;
  4685. } else if ((31 - WRIntScan.BoundReg1_E) <= WRIntScan.BoundReg1_S)
  4686. sel = 0;
  4687. } else {
  4688. AUTOK_PRINT("[ERR]Should NOT goto here in WR internal scan!!!\r\n");
  4689. #ifdef AUTOK_DEBUG
  4690. AUTOK_ERR();
  4691. #else
  4692. goto exit;
  4693. #endif
  4694. }
  4695. } else {
  4696. if (WRIntScan.BoundReg1_E < WRIntPassWin) {
  4697. sel = WRIntScan.BoundReg1_E + WRIntPassWin;
  4698. } else if (WRIntScan.BoundReg1_S > WRIntPassWin) {
  4699. sel = WRIntScan.BoundReg1_S - WRIntPassWin;
  4700. } else {
  4701. if ((WRIntScan.BoundReg1_E + WRIntPassWin) >= 32)
  4702. sel = 0;
  4703. else
  4704. sel = WRIntScan.BoundReg1_E + WRIntPassWin;
  4705. }
  4706. }
  4707. }
  4708. /* Double check for write internal transition boundary */
  4709. if ((data.score == 32) && fDoubleChk) {
  4710. fDoubleChk = 0;
  4711. if (E_RESULT_PASS == autok_doub_chk_wdat_int_bound(host, &data.raw_data))
  4712. goto doubleChkWDAT;
  4713. else
  4714. goto exit;
  4715. }
  4716. pAutoKData[E_MSDC_PAD_TUNE_DATWRDLY].data.sel = sel;
  4717. sel = pAutoKData[E_MSDC_CKGEN_MSDC_DLY_SEL].data.sel;
  4718. autok_ckg_data[sel].writePadSel = pAutoKData[E_MSDC_PAD_TUNE_DATWRDLY].data.sel;
  4719. msdc_autok_adjust_param(host, DAT_WRD_DLY, &pAutoKData[E_MSDC_PAD_TUNE_DATWRDLY].data.sel,
  4720. MSDC_WRITE);
  4721. AUTOK_PRINT("Write Internal delay tune done: %d\r\n",
  4722. pAutoKData[E_MSDC_PAD_TUNE_DATWRDLY].data.sel);
  4723. res = E_RESULT_PASS;
  4724. exit:
  4725. return res;
  4726. }
  4727. static E_RESULT_TYPE
  4728. autok_tune_algorithm(struct msdc_host *host,
  4729. E_AUTOK_TUNING_STAGE stg, U_AUTOK_INTERFACE_DATA *pAutoKData)
  4730. {
  4731. E_RESULT_TYPE ret = E_RESULT_ERR;
  4732. AUTOK_CYC_SCAN_RES_T rPadDlyRes;
  4733. AUTOK_RD_TUNE_RES_T rRdTuneRes;
  4734. AUTOK_CMD_TUNE_RES_T rCmdTuneRes;
  4735. /* title */
  4736. AUTOK_PRINT("=======autok_stg%d_tune=======\r\n", stg + 1);
  4737. /* Initialize parameters */
  4738. memset(&rPadDlyRes, 0, sizeof(rPadDlyRes));
  4739. memset(&rRdTuneRes, 0, sizeof(rRdTuneRes));
  4740. memset(&rCmdTuneRes, 0, sizeof(rCmdTuneRes));
  4741. /* Calculate the pad delay length, should be done on stage1 */
  4742. if (stg == TUNING_STG1) {
  4743. rPadDlyRes = autok_cycle_scan(host, pAutoKData);
  4744. if (!rPadDlyRes.PadDlyPeriodLen)
  4745. goto err;
  4746. }
  4747. /* Tune read data first on stage1 */
  4748. if (stg == TUNING_STG1) {
  4749. rRdTuneRes = autok_tune_rd(host, stg, pAutoKData, &rPadDlyRes, &rCmdTuneRes);
  4750. if (!rRdTuneRes.fRetOk)
  4751. goto err;
  4752. }
  4753. /* Tune CMD, including pad delay & internal delay */
  4754. rCmdTuneRes = autok_tune_cmd(host, stg, pAutoKData, &rPadDlyRes, &rRdTuneRes);
  4755. if (!rCmdTuneRes.fRetOk)
  4756. goto err;
  4757. /* Tune read data after CMD on stage2 */
  4758. if (stg == TUNING_STG2) {
  4759. rRdTuneRes = autok_tune_rd(host, stg, pAutoKData, &rPadDlyRes, &rCmdTuneRes);
  4760. if (!rRdTuneRes.fRetOk)
  4761. goto err;
  4762. }
  4763. /* Tune write data, only for internal delay */
  4764. ret = autok_tune_wd(host, pAutoKData);
  4765. err:
  4766. return ret;
  4767. }
  4768. static void autok_tuning_parameter_init(struct msdc_host *host, E_AUTOK_TUNING_STAGE stg,
  4769. U_AUTOK_INTERFACE_DATA *pAutokData)
  4770. {
  4771. unsigned int val = 0;
  4772. if (stg == TUNING_STG1)
  4773. containGen();
  4774. /* data delay using the one setting */
  4775. msdc_autok_adjust_param(host, DATA_DLYLINE_SEL, &val, MSDC_WRITE);
  4776. msdc_autok_adjust_param(host, DAT_RD_DLY, &val, MSDC_WRITE);
  4777. msdc_autok_adjust_param(host, DAT_WRD_DLY, &val, MSDC_WRITE);
  4778. /* data sampling use the one setting */
  4779. msdc_autok_adjust_param(host, READ_DATA_SMPL_SEL, &val, MSDC_WRITE);
  4780. msdc_autok_adjust_param(host, WRITE_DATA_SMPL_SEL, &val, MSDC_WRITE);
  4781. /* cmd response delay selection value */
  4782. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY, &val, MSDC_WRITE);
  4783. /* cmd line delay selection value */
  4784. msdc_autok_adjust_param(host, CMD_RD_DLY, &val, MSDC_WRITE);
  4785. /* ckbuf in ckgen delay selection for read tuning, 32 stages */
  4786. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL, &val, MSDC_WRITE);
  4787. if (stg == TUNING_STG1) {
  4788. /* cmd line with clock's rising or falling edge */
  4789. msdc_autok_adjust_param(host, CMD_EDGE, &val, MSDC_WRITE);
  4790. /* cmd response turn around period, just for UHS104 mode */
  4791. msdc_autok_adjust_param(host, CMD_RSP_TA_CNTR, &val, MSDC_READ);
  4792. #ifdef MT6290
  4793. pAutokData[E_MSDC_CMD_RSP_TA_CNTR].data.sel = val;
  4794. #else
  4795. #if 0 /* Temporary */
  4796. pAutokData[E_MSDC_CMD_RSP_TA_CNTR].data.sel = 3;
  4797. #else
  4798. pAutokData[E_MSDC_CMD_RSP_TA_CNTR].data.sel = val;
  4799. #endif
  4800. /* For low frequncy cnosideration */
  4801. if (freq_mhz <= 100)
  4802. pAutokData[E_MSDC_CMD_RSP_TA_CNTR].data.sel = 0;
  4803. msdc_autok_adjust_param(host, CMD_RSP_TA_CNTR,
  4804. &pAutokData[E_MSDC_CMD_RSP_TA_CNTR].data.sel, MSDC_WRITE);
  4805. #endif
  4806. /* read data latch clock selection */
  4807. msdc_autok_adjust_param(host, INT_DAT_LATCH_CK, &val, MSDC_READ);
  4808. #ifdef MT6290
  4809. pAutokData[E_MSDC_INT_DAT_LATCH_CK_SEL].data.sel = val;
  4810. #else
  4811. #if 0 /* Temporary */
  4812. pAutokData[E_MSDC_INT_DAT_LATCH_CK_SEL].data.sel = 0;
  4813. #else
  4814. pAutokData[E_MSDC_INT_DAT_LATCH_CK_SEL].data.sel = val;
  4815. #endif
  4816. /* For low frequncy cnosideration */
  4817. if (freq_mhz <= 100)
  4818. pAutokData[E_MSDC_INT_DAT_LATCH_CK_SEL].data.sel = 0;
  4819. msdc_autok_adjust_param(host, INT_DAT_LATCH_CK,
  4820. &pAutokData[E_MSDC_INT_DAT_LATCH_CK_SEL].data.sel,
  4821. MSDC_WRITE);
  4822. #endif
  4823. /* write CRC turn around period, just for UHS104 mode */
  4824. msdc_autok_adjust_param(host, WRDAT_CRCS_TA_CNTR, &val, MSDC_READ);
  4825. #ifdef MT6290
  4826. pAutokData[E_MSDC_WRDAT_CRCS_TA_CNTR].data.sel = val;
  4827. #else
  4828. #if 0 /* Temporary */
  4829. pAutokData[E_MSDC_WRDAT_CRCS_TA_CNTR].data.sel = 3;
  4830. #else
  4831. pAutokData[E_MSDC_WRDAT_CRCS_TA_CNTR].data.sel = val;
  4832. #endif
  4833. /* For low frequncy cnosideration */
  4834. if (freq_mhz <= 100)
  4835. pAutokData[E_MSDC_WRDAT_CRCS_TA_CNTR].data.sel = 0;
  4836. msdc_autok_adjust_param(host, WRDAT_CRCS_TA_CNTR,
  4837. &pAutokData[E_MSDC_WRDAT_CRCS_TA_CNTR].data.sel,
  4838. MSDC_WRITE);
  4839. #endif
  4840. /* read sampling edge */
  4841. pAutokData[E_MSDC_IOCON_RDSPL].data.sel = 0;
  4842. msdc_autok_adjust_param(host, RDATA_EDGE, &pAutokData[E_MSDC_IOCON_RDSPL].data.sel,
  4843. MSDC_WRITE);
  4844. /* write CRC sampling edge */
  4845. pAutokData[E_MSDC_IOCON_WDSPL].data.sel = 0;
  4846. msdc_autok_adjust_param(host, WDATA_EDGE, &pAutokData[E_MSDC_IOCON_WDSPL].data.sel,
  4847. MSDC_WRITE);
  4848. } else {
  4849. /* CKGEN */
  4850. msdc_autok_adjust_param(host, CKGEN_MSDC_DLY_SEL,
  4851. &pAutokData[E_MSDC_CKGEN_MSDC_DLY_SEL].data.sel,
  4852. MSDC_WRITE);
  4853. /* CMD internal delay */
  4854. msdc_autok_adjust_param(host, CMD_RESP_RD_DLY,
  4855. &pAutokData[E_MSDC_PAD_TUNE_CMDRRDLY].data.sel, MSDC_WRITE);
  4856. /* CMD pad delay */
  4857. msdc_autok_adjust_param(host, CMD_RD_DLY,
  4858. &pAutokData[E_MSDC_PAD_TUNE_CMDRDLY].data.sel, MSDC_WRITE);
  4859. /* Read data pad delay */
  4860. if (gfTinyMar && !gfEqualVcore) {
  4861. /*
  4862. * In case of tiny margin, apply the gear which is
  4863. * nearest and less than the current voltage, and
  4864. * shift the RDAT pad delay to right for 1 gear
  4865. */
  4866. val = pAutokData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel + 1;
  4867. msdc_autok_adjust_param(host, DAT_RD_DLY, &val, MSDC_WRITE);
  4868. } else
  4869. msdc_autok_adjust_param(host, DAT_RD_DLY,
  4870. &pAutokData[E_MSDC_PAD_TUNE_DATRRDLY].data.sel,
  4871. MSDC_WRITE);
  4872. /* Write data internal delay */
  4873. msdc_autok_adjust_param(host, DAT_WRD_DLY,
  4874. &pAutokData[E_MSDC_PAD_TUNE_DATWRDLY].data.sel, MSDC_WRITE);
  4875. /* cmd line with clock's rising or falling edge */
  4876. msdc_autok_adjust_param(host, CMD_EDGE, &pAutokData[E_MSDC_IOCON_RSPL].data.sel,
  4877. MSDC_WRITE);
  4878. /* cmd response turn around period, just for UHS104 mode */
  4879. msdc_autok_adjust_param(host, CMD_RSP_TA_CNTR,
  4880. &pAutokData[E_MSDC_CMD_RSP_TA_CNTR].data.sel, MSDC_WRITE);
  4881. /* read data latch clock selection */
  4882. msdc_autok_adjust_param(host, INT_DAT_LATCH_CK,
  4883. &pAutokData[E_MSDC_INT_DAT_LATCH_CK_SEL].data.sel,
  4884. MSDC_WRITE);
  4885. /* read sampling edge */
  4886. msdc_autok_adjust_param(host, RDATA_EDGE, &pAutokData[E_MSDC_IOCON_RDSPL].data.sel,
  4887. MSDC_WRITE);
  4888. /* write CRC turn around period, just for UHS104 mode */
  4889. msdc_autok_adjust_param(host, WRDAT_CRCS_TA_CNTR,
  4890. &pAutokData[E_MSDC_WRDAT_CRCS_TA_CNTR].data.sel,
  4891. MSDC_WRITE);
  4892. /* write CRC sampling edge */
  4893. msdc_autok_adjust_param(host, WDATA_EDGE, &pAutokData[E_MSDC_IOCON_WDSPL].data.sel,
  4894. MSDC_WRITE);
  4895. }
  4896. }
  4897. static void autok_vcore_set(unsigned int vcore_uv)
  4898. {
  4899. /* Denali do SDIO transfer at fixed vcore, so we don't need to do multiple vcore autok. */
  4900. /* Let it be a dummy function here. */
  4901. AUTOK_PRINT("autok_vcore_set(): %d\n", vcore_uv);
  4902. }
  4903. static void autok_get_reg_field(unsigned int param, unsigned int *field)
  4904. {
  4905. switch (param) {
  4906. case E_MSDC_PAD_TUNE_CMDRRDLY:
  4907. *field = CMD_RESP_RD_DLY;
  4908. break;
  4909. case E_MSDC_CMD_RSP_TA_CNTR:
  4910. *field = CMD_RSP_TA_CNTR;
  4911. break;
  4912. case E_MSDC_IOCON_RSPL:
  4913. *field = CMD_EDGE;
  4914. break;
  4915. case E_MSDC_CKGEN_MSDC_DLY_SEL:
  4916. *field = CKGEN_MSDC_DLY_SEL;
  4917. break;
  4918. case E_MSDC_PAD_TUNE_CMDRDLY:
  4919. *field = CMD_RD_DLY;
  4920. break;
  4921. case E_MSDC_INT_DAT_LATCH_CK_SEL:
  4922. *field = INT_DAT_LATCH_CK;
  4923. break;
  4924. case E_MSDC_IOCON_RDSPL:
  4925. *field = RDATA_EDGE;
  4926. break;
  4927. case E_MSDC_PAD_TUNE_DATRRDLY:
  4928. *field = DAT_RD_DLY;
  4929. break;
  4930. case E_MSDC_WRDAT_CRCS_TA_CNTR:
  4931. *field = WRDAT_CRCS_TA_CNTR;
  4932. break;
  4933. case E_MSDC_IOCON_WDSPL:
  4934. *field = WDATA_EDGE;
  4935. break;
  4936. case E_MSDC_PAD_TUNE_DATWRDLY:
  4937. *field = DAT_WRD_DLY;
  4938. break;
  4939. default:
  4940. AUTOK_PRINT("[ERR]Can NOT find the delay cell field!!!\r\n");
  4941. *field = 0;
  4942. break;
  4943. }
  4944. }
  4945. static void autok_show_parameters(struct msdc_host *host, void *pData)
  4946. {
  4947. unsigned int parm;
  4948. unsigned int val;
  4949. unsigned int field;
  4950. U_AUTOK_INTERFACE_DATA *pAutok;
  4951. pAutok = (U_AUTOK_INTERFACE_DATA *) pData;
  4952. AUTOK_PRINT("=====Delay Params Show:=====\r\n");
  4953. for (parm = 0; parm < E_AUTOK_DLY_PARAM_MAX; parm++) {
  4954. autok_get_reg_field(parm, &field);
  4955. msdc_autok_adjust_param(host, field, &val, MSDC_READ);
  4956. if (val != pAutok[parm].data.sel) {
  4957. AUTOK_PRINT("%s expect:%02d, real:%02d\r\n", autok_param_name[parm],
  4958. pAutok[parm].data.sel, val);
  4959. } else {
  4960. AUTOK_PRINT("%s value:%02d\r\n", autok_param_name[parm], val);
  4961. }
  4962. }
  4963. }
  4964. static void autok_setup_envir(struct msdc_host *host)
  4965. {
  4966. freq_mhz = host->mclk / 1000000;
  4967. #if defined(MT6582LTE)
  4968. {
  4969. unsigned int io_ring, core_ring;
  4970. if (freq_mhz >= 200) {
  4971. autok_vcore_scan_num = AUTOK_VCORE_SCAN_NUM;
  4972. autok_rdat_fbound_th = 6;
  4973. } else if (freq_mhz >= 150) {
  4974. autok_vcore_scan_num = AUTOK_VCORE_SCAN_NUM;
  4975. autok_rdat_fbound_th = 4;
  4976. } else if (freq_mhz >= 100) {
  4977. autok_vcore_scan_num = 1;
  4978. autok_rdat_fbound_th = 3;
  4979. } else if (freq_mhz >= 50) {
  4980. autok_vcore_scan_num = 1;
  4981. autok_rdat_fbound_th = 2;
  4982. }
  4983. sdio_get_rings(&io_ring, &core_ring);
  4984. AUTOK_PRINT("Get IO_RING(%d) and CORE_RING(%d)\r\n", io_ring, core_ring);
  4985. if (io_ring < AUTOK_SS_IO_RING_TH) {
  4986. AUTOK_PRINT("SS IO corner has been found!\r\n");
  4987. gfIOSS = 1;
  4988. } else
  4989. gfIOSS = 0;
  4990. if (core_ring < AUTOK_TT_CORE_RING_TH) {
  4991. AUTOK_PRINT("TT Core corner has been found!\r\n");
  4992. gfCoreTT = 1;
  4993. } else
  4994. gfCoreTT = 0;
  4995. }
  4996. #elif (defined(MT6592LTE) || defined(MT6595WIFI) || defined(MT6752WIFI)) || defined(MT6735WIFI)
  4997. if (freq_mhz >= 200) {
  4998. autok_vcore_scan_num = AUTOK_VCORE_SCAN_NUM;
  4999. autok_rdat_fbound_th = 2;
  5000. } else if (freq_mhz >= 150) {
  5001. autok_vcore_scan_num = AUTOK_VCORE_SCAN_NUM;
  5002. autok_rdat_fbound_th = 1;
  5003. } else if (freq_mhz >= 100) {
  5004. autok_vcore_scan_num = 1;
  5005. autok_rdat_fbound_th = 1;
  5006. } else if (freq_mhz >= 50) {
  5007. autok_vcore_scan_num = 1;
  5008. autok_rdat_fbound_th = 1;
  5009. }
  5010. #endif
  5011. AUTOK_PRINT("freq:%d, vcore_num:%d, rdat_th:%d\r\n", freq_mhz,
  5012. autok_vcore_scan_num, autok_rdat_fbound_th);
  5013. }
  5014. unsigned int msdc_autok_get_vcore(unsigned int vcore_uv, unsigned int *pfIdentical)
  5015. {
  5016. unsigned int idx, size, vcore_sel = 0;
  5017. unsigned int autok_vcore_sel[autok_vcore_scan_num * 2 - 1];
  5018. size = sizeof(autok_vcore_sel) / sizeof(autok_vcore_sel[0]);
  5019. /* AUTOK_PRINT("Vcore interpolation: "); */
  5020. if (!gfTinyMar) {
  5021. for (idx = 0; idx < size; idx++) {
  5022. if ((idx % 2) == 0)
  5023. autok_vcore_sel[idx] = g_autok_vcore_sel[idx / 2];
  5024. else
  5025. autok_vcore_sel[idx] = g_autok_vcore_sel[idx / 2] +
  5026. (g_autok_vcore_sel[idx / 2 + 1] -
  5027. g_autok_vcore_sel[idx / 2]) / 2;
  5028. /* AUTOK_PRINT("%duV ", autok_vcore_sel[idx]); */
  5029. }
  5030. /* AUTOK_PRINT("\r\n"); */
  5031. if (vcore_uv <= g_autok_vcore_sel[0])
  5032. /* vcore_uv = g_autok_vcore_sel[0]; */
  5033. vcore_sel = 0;
  5034. else if (vcore_uv >= g_autok_vcore_sel[autok_vcore_scan_num - 1])
  5035. /* vcore_uv = g_autok_vcore_sel[autok_vcore_scan_num-1]; */
  5036. vcore_sel = autok_vcore_scan_num - 1;
  5037. else {
  5038. for (idx = 0; idx < size; idx++) {
  5039. if ((vcore_uv > autok_vcore_sel[idx]) &&
  5040. (vcore_uv <= autok_vcore_sel[idx + 1])) {
  5041. if ((idx % 2) == 0)
  5042. vcore_sel = idx / 2;
  5043. else
  5044. vcore_sel = idx / 2 + 1;
  5045. break;
  5046. }
  5047. }
  5048. }
  5049. } else {
  5050. /* for (idx = 0; idx < autok_vcore_scan_num; idx++) */
  5051. /* AUTOK_PRINT("%duV ", g_autok_vcore_sel[idx]); */
  5052. /* AUTOK_PRINT("\r\n"); */
  5053. if (vcore_uv <= g_autok_vcore_sel[0])
  5054. /* vcore_uv = g_autok_vcore_sel[0]; */
  5055. vcore_sel = 0;
  5056. else if (vcore_uv >= g_autok_vcore_sel[autok_vcore_scan_num - 1])
  5057. /* vcore_uv = g_autok_vcore_sel[autok_vcore_scan_num-1]; */
  5058. vcore_sel = autok_vcore_scan_num - 1;
  5059. else {
  5060. for (idx = 0; idx < autok_vcore_scan_num; idx++) {
  5061. if ((vcore_uv >= g_autok_vcore_sel[idx]) &&
  5062. (vcore_uv < g_autok_vcore_sel[idx + 1])) {
  5063. vcore_sel = idx;
  5064. break;
  5065. }
  5066. }
  5067. }
  5068. if (g_autok_vcore_sel[vcore_sel] == vcore_uv)
  5069. gfEqualVcore = 1;
  5070. else
  5071. gfEqualVcore = 0;
  5072. }
  5073. AUTOK_PRINT("Cur Vcore:%duV, Sel Vcore:%duV\r\n", vcore_uv, g_autok_vcore_sel[vcore_sel]);
  5074. if ((vcore_uv == g_autok_vcore_sel[vcore_sel]) && pfIdentical)
  5075. *pfIdentical = 1;
  5076. else if ((vcore_uv != g_autok_vcore_sel[vcore_sel]) && pfIdentical)
  5077. *pfIdentical = 0;
  5078. return vcore_sel;
  5079. }
  5080. int msdc_autok_apply_param(struct msdc_host *host, unsigned int vcore_uv_off)
  5081. {
  5082. U_AUTOK_INTERFACE_DATA *pAutok;
  5083. unsigned int vcore_sel, vcore_uv, fIdent = 0;
  5084. /* Check parameters */
  5085. if (host == NULL)
  5086. return -1;
  5087. if (!g_pp_autok_data) {
  5088. AUTOK_PRINT("NULL autok param pointer on param apply!\r\n");
  5089. return -1;
  5090. }
  5091. /* Calclate the offset according to vcore_uv */
  5092. vcore_uv = mt65x2_vcore_tbl[vcore_uv_off];
  5093. vcore_sel = msdc_autok_get_vcore(vcore_uv, &fIdent);
  5094. pAutok = *(g_pp_autok_data + vcore_sel);
  5095. if ((pAutok + E_AUTOK_VERSION)->version != AUTOK_VERSION_NO) {
  5096. AUTOK_PRINT("autoK version wrong = %d\r\n", pAutok->version);
  5097. return -2;
  5098. }
  5099. /* Apply the parameters */
  5100. AUTOK_PRINT("msdc_autok_apply_param...%s\r\n", gfTinyMar ? "(TinyMargin)" : "");
  5101. autok_tuning_parameter_init(host, TUNING_STG2, pAutok);
  5102. autok_show_parameters(host, pAutok);
  5103. return 0;
  5104. }
  5105. /*************************************************************************
  5106. * FUNCTION
  5107. * msdc_autok_stg1_cal
  5108. *
  5109. * DESCRIPTION
  5110. * This function for auto-K at stage1
  5111. *
  5112. * PARAMETERS
  5113. * host: msdc host manipulator pointer
  5114. *
  5115. * RETURN VALUES
  5116. * error code: 0 success,
  5117. * -1 parameter input error
  5118. * -2 else error
  5119. *************************************************************************/
  5120. int msdc_autok_stg1_cal(struct msdc_host *host,
  5121. unsigned int offset_restore, struct autok_predata *p_single_autok)
  5122. {
  5123. E_RESULT_TYPE res = E_RESULT_ERR;
  5124. U_AUTOK_INTERFACE_DATA *pAutok;
  5125. /* Check parameters */
  5126. if ((p_single_autok == NULL) || (host == NULL)) {
  5127. AUTOK_PRINT("NULL autok param pointer on STG1!\r\n");
  5128. return -1;
  5129. }
  5130. if (!p_single_autok->vol_list || !p_single_autok->ai_data) {
  5131. AUTOK_PRINT("NULL autok param pointer on STG1!\r\n");
  5132. return -1;
  5133. }
  5134. /* Setup gloabal environment according to current SCLK */
  5135. autok_setup_envir(host);
  5136. AUTOK_PRINT("Stage1 statistic : start\r\n");
  5137. /* Set vcore for autoK scan */
  5138. autok_vcore_set(*p_single_autok->vol_list);
  5139. autok_get_current_vcore_offset();
  5140. pAutok = *p_single_autok->ai_data;
  5141. memset((void *)pAutok, 0, sizeof(U_AUTOK_INTERFACE_DATA) * MAX_AUTOK_DAT_NUM);
  5142. autok_tuning_parameter_init(host, TUNING_STG1, pAutok);
  5143. res = autok_tune_algorithm(host, TUNING_STG1, pAutok);
  5144. if (!res)
  5145. autok_show_parameters(host, pAutok);
  5146. else {
  5147. AUTOK_PRINT("[ERR]msdc_autok_stg1_cal returns %d\r\n", res);
  5148. goto exit;
  5149. }
  5150. pAutok[E_AUTOK_VERSION].version = AUTOK_VERSION_NO;
  5151. pAutok[E_AUTOK_FREQ].freq = host->mclk;
  5152. exit:
  5153. AUTOK_PRINT("Restore vcore to %duV\r\n", mt65x2_vcore_tbl[offset_restore]);
  5154. autok_vcore_set(mt65x2_vcore_tbl[offset_restore]);
  5155. AUTOK_PRINT("Stage1 statistic : end\r\n");
  5156. return -res;
  5157. }
  5158. /*************************************************************************
  5159. * FUNCTION
  5160. * msdc_autok_stg1_result_get
  5161. *
  5162. * DESCRIPTION
  5163. * This function for getting data from stage1 result
  5164. *
  5165. * PARAMETERS
  5166. * ppData: pointer to pointer for getting autoK data at stage1
  5167. * pLen: data length in number of byte
  5168. *
  5169. * RETURN VALUES
  5170. * error code: 0 success,
  5171. * -1 parameter input error
  5172. * -2 else error
  5173. *************************************************************************/
  5174. int msdc_autok_stg1_data_get(void **ppData, int *pLen)
  5175. {
  5176. if (ppData == NULL || pLen == NULL)
  5177. return -1;
  5178. *ppData = (void *)(*g_pp_autok_data);
  5179. *pLen = sizeof(U_AUTOK_INTERFACE_DATA) * (MAX_AUTOK_DAT_NUM * autok_vcore_scan_num);
  5180. return 0;
  5181. }
  5182. /*************************************************************************
  5183. * FUNCTION
  5184. * msdc_autok_stg2_cal
  5185. *
  5186. * DESCRIPTION
  5187. * This function for auto-K at stage2
  5188. *
  5189. * PARAMETERS
  5190. * host: msdc_host pointer
  5191. * pData: pointer for autoK data came from stage1
  5192. * len: number of byte data
  5193. *
  5194. * RETURN VALUES
  5195. * error code: 0 success,
  5196. * -1 parameter input error
  5197. * -2 else error
  5198. *************************************************************************/
  5199. int msdc_autok_stg2_cal(struct msdc_host *host,
  5200. struct autok_predata *p_autok_data, unsigned int vcore_uv_off)
  5201. {
  5202. unsigned int idx, vcore_sel, vcore_uv, fIdent = 0;
  5203. U_AUTOK_INTERFACE_DATA *pAutok;
  5204. E_RESULT_TYPE res = E_RESULT_ERR;
  5205. /* Setup gloabal environment according to current SCLK */
  5206. autok_setup_envir(host);
  5207. /* Check parameters */
  5208. if (p_autok_data == NULL || host == NULL) {
  5209. AUTOK_PRINT("NULL autok param pointer on STG2!\r\n");
  5210. return -1;
  5211. }
  5212. if (!p_autok_data->ai_data) {
  5213. AUTOK_PRINT("NULL autok param pointer on STG2!\r\n");
  5214. return -1;
  5215. }
  5216. g_pp_autok_data = p_autok_data->ai_data;
  5217. /* Update voltage number and voltage list */
  5218. autok_vcore_scan_num = p_autok_data->vol_count;
  5219. if (autok_vcore_scan_num > AUTOK_VCORE_SCAN_NUM) {
  5220. AUTOK_PRINT("Exceeds the Vcore scan number range\r\n");
  5221. return -1;
  5222. }
  5223. for (idx = 0; idx < autok_vcore_scan_num; idx++) {
  5224. g_autok_vcore_sel[idx] = *(p_autok_data->vol_list + idx);
  5225. /* Check if tiny margin case */
  5226. pAutok = *(g_pp_autok_data + idx);
  5227. if (pAutok[E_MSDC_F_TINY_MARGIN].data.sel == AUTOK_TINY_MAR_PAT) {
  5228. /* Set the flag for all voltage anyway */
  5229. gfTinyMar = 1;
  5230. AUTOK_PRINT("Tiny margin found\r\n");
  5231. }
  5232. }
  5233. /* Calclate the offset according to vcore_uv */
  5234. vcore_uv = mt65x2_vcore_tbl[vcore_uv_off];
  5235. vcore_sel = msdc_autok_get_vcore(vcore_uv, &fIdent);
  5236. pAutok = *(g_pp_autok_data + vcore_sel);
  5237. if ((pAutok + E_AUTOK_VERSION)->version != AUTOK_VERSION_NO) {
  5238. AUTOK_PRINT("autoK version wrong = %d\r\n", pAutok->version);
  5239. return -2;
  5240. }
  5241. if ((pAutok + E_AUTOK_FREQ)->freq != host->mclk) {
  5242. AUTOK_PRINT("Now operation freq(%d) not meet autok freq(%d)\r\n",
  5243. host->mclk, (pAutok + E_AUTOK_FREQ)->freq);
  5244. return -2;
  5245. }
  5246. autok_tuning_parameter_init(host, TUNING_STG2, pAutok);
  5247. autok_show_parameters(host, pAutok);
  5248. #if 0
  5249. res = autok_tune_algorithm(host, TUNING_STG2, pAutok);
  5250. #else
  5251. AUTOK_PRINT("=======autok_stg%d_tune=======\r\n", TUNING_STG2 + 1);
  5252. AUTOK_PRINT("stg%d is bypassed currently\r\n", TUNING_STG2 + 1);
  5253. res = E_RESULT_PASS;
  5254. #endif
  5255. if (!res)
  5256. autok_show_parameters(host, pAutok);
  5257. else
  5258. AUTOK_PRINT("[ERR]msdc_autok_stg2_cal returns %d\r\n", res);
  5259. return -res;
  5260. }
  5261. int msdc_autok_get_suggetst_vcore(unsigned int **suggest_vol_tbl)
  5262. {
  5263. unsigned int tbl_size = sizeof(unsigned int) * AUTOK_VCORE_SCAN_NUM;
  5264. *suggest_vol_tbl = kzalloc(tbl_size, GFP_KERNEL);
  5265. memcpy(*suggest_vol_tbl, g_autok_vcore_sel, tbl_size);
  5266. return AUTOK_VCORE_SCAN_NUM;
  5267. }