octeon3_lmc.c 326 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Marvell International Ltd.
  4. */
  5. #include <command.h>
  6. #include <dm.h>
  7. #include <hang.h>
  8. #include <i2c.h>
  9. #include <ram.h>
  10. #include <time.h>
  11. #include <linux/bitops.h>
  12. #include <linux/io.h>
  13. #include <mach/octeon_ddr.h>
  14. /* Random number generator stuff */
  15. #define CVMX_OCT_DID_RNG 8ULL
  16. static u64 cvmx_rng_get_random64(void)
  17. {
  18. return csr_rd(cvmx_build_io_address(CVMX_OCT_DID_RNG, 0));
  19. }
  20. static void cvmx_rng_enable(void)
  21. {
  22. u64 val;
  23. val = csr_rd(CVMX_RNM_CTL_STATUS);
  24. val |= BIT(0) | BIT(1);
  25. csr_wr(CVMX_RNM_CTL_STATUS, val);
  26. }
  27. #define RLEVEL_PRINTALL_DEFAULT 1
  28. #define WLEVEL_PRINTALL_DEFAULT 1
  29. /*
  30. * Define how many HW WL samples to take for majority voting.
  31. * MUST BE odd!!
  32. * Assume there should only be 2 possible values that will show up,
  33. * so treat ties as a problem!!!
  34. * NOTE: Do not change this without checking the code!!!
  35. */
  36. #define WLEVEL_LOOPS_DEFAULT 5
  37. #define ENABLE_COMPUTED_VREF_ADJUSTMENT 1
  38. #define SW_WLEVEL_HW_DEFAULT 1
  39. #define DEFAULT_BEST_RANK_SCORE 9999999
  40. #define MAX_RANK_SCORE_LIMIT 99
  41. /*
  42. * Define how many HW RL samples per rank to take multiple samples will
  43. * allow looking for the best sample score
  44. */
  45. #define RLEVEL_SAMPLES_DEFAULT 3
  46. #define ddr_seq_print(format, ...) do {} while (0)
  47. struct wlevel_bitcnt {
  48. int bitcnt[4];
  49. };
  50. static void display_dac_dbi_settings(int lmc, int dac_or_dbi,
  51. int ecc_ena, int *settings, char *title);
  52. static unsigned short load_dac_override(struct ddr_priv *priv, int if_num,
  53. int dac_value, int byte);
  54. /* "mode" arg */
  55. #define DBTRAIN_TEST 0
  56. #define DBTRAIN_DBI 1
  57. #define DBTRAIN_LFSR 2
  58. static int run_best_hw_patterns(struct ddr_priv *priv, int lmc, u64 phys_addr,
  59. int mode, u64 *xor_data);
  60. #define LMC_DDR3_RESET_ASSERT 0
  61. #define LMC_DDR3_RESET_DEASSERT 1
  62. static void cn7xxx_lmc_ddr3_reset(struct ddr_priv *priv, int if_num, int reset)
  63. {
  64. union cvmx_lmcx_reset_ctl reset_ctl;
  65. /*
  66. * 4. Deassert DDRn_RESET_L pin by writing
  67. * LMC(0..3)_RESET_CTL[DDR3RST] = 1
  68. * without modifying any other LMC(0..3)_RESET_CTL fields.
  69. * 5. Read LMC(0..3)_RESET_CTL and wait for the result.
  70. * 6. Wait a minimum of 500us. This guarantees the necessary T = 500us
  71. * delay between DDRn_RESET_L deassertion and DDRn_DIMM*_CKE*
  72. * assertion.
  73. */
  74. debug("LMC%d %s DDR_RESET_L\n", if_num,
  75. (reset ==
  76. LMC_DDR3_RESET_DEASSERT) ? "De-asserting" : "Asserting");
  77. reset_ctl.u64 = lmc_rd(priv, CVMX_LMCX_RESET_CTL(if_num));
  78. reset_ctl.cn78xx.ddr3rst = reset;
  79. lmc_wr(priv, CVMX_LMCX_RESET_CTL(if_num), reset_ctl.u64);
  80. lmc_rd(priv, CVMX_LMCX_RESET_CTL(if_num));
  81. udelay(500);
  82. }
  83. static void perform_lmc_reset(struct ddr_priv *priv, int node, int if_num)
  84. {
  85. /*
  86. * 5.9.6 LMC RESET Initialization
  87. *
  88. * The purpose of this step is to assert/deassert the RESET# pin at the
  89. * DDR3/DDR4 parts.
  90. *
  91. * This LMC RESET step is done for all enabled LMCs.
  92. *
  93. * It may be appropriate to skip this step if the DDR3/DDR4 DRAM parts
  94. * are in self refresh and are currently preserving their
  95. * contents. (Software can determine this via
  96. * LMC(0..3)_RESET_CTL[DDR3PSV] in some circumstances.) The remainder of
  97. * this section assumes that the DRAM contents need not be preserved.
  98. *
  99. * The remainder of this section assumes that the CN78XX DDRn_RESET_L
  100. * pin is attached to the RESET# pin of the attached DDR3/DDR4 parts,
  101. * as will be appropriate in many systems.
  102. *
  103. * (In other systems, such as ones that can preserve DDR3/DDR4 part
  104. * contents while CN78XX is powered down, it will not be appropriate to
  105. * directly attach the CN78XX DDRn_RESET_L pin to DRESET# of the
  106. * DDR3/DDR4 parts, and this section may not apply.)
  107. *
  108. * The remainder of this section describes the sequence for LMCn.
  109. *
  110. * Perform the following six substeps for LMC reset initialization:
  111. *
  112. * 1. If not done already, assert DDRn_RESET_L pin by writing
  113. * LMC(0..3)_RESET_ CTL[DDR3RST] = 0 without modifying any other
  114. * LMC(0..3)_RESET_CTL fields.
  115. */
  116. if (!ddr_memory_preserved(priv)) {
  117. /*
  118. * 2. Read LMC(0..3)_RESET_CTL and wait for the result.
  119. */
  120. lmc_rd(priv, CVMX_LMCX_RESET_CTL(if_num));
  121. /*
  122. * 3. Wait until RESET# assertion-time requirement from JEDEC
  123. * DDR3/DDR4 specification is satisfied (200 us during a
  124. * power-on ramp, 100ns when power is already stable).
  125. */
  126. udelay(200);
  127. /*
  128. * 4. Deassert DDRn_RESET_L pin by writing
  129. * LMC(0..3)_RESET_CTL[DDR3RST] = 1
  130. * without modifying any other LMC(0..3)_RESET_CTL fields.
  131. * 5. Read LMC(0..3)_RESET_CTL and wait for the result.
  132. * 6. Wait a minimum of 500us. This guarantees the necessary
  133. * T = 500us delay between DDRn_RESET_L deassertion and
  134. * DDRn_DIMM*_CKE* assertion.
  135. */
  136. cn7xxx_lmc_ddr3_reset(priv, if_num, LMC_DDR3_RESET_DEASSERT);
  137. /* Toggle Reset Again */
  138. /* That is, assert, then de-assert, one more time */
  139. cn7xxx_lmc_ddr3_reset(priv, if_num, LMC_DDR3_RESET_ASSERT);
  140. cn7xxx_lmc_ddr3_reset(priv, if_num, LMC_DDR3_RESET_DEASSERT);
  141. }
  142. }
  143. void oct3_ddr3_seq(struct ddr_priv *priv, int rank_mask, int if_num,
  144. int sequence)
  145. {
  146. /*
  147. * 3. Without changing any other fields in LMC(0)_CONFIG, write
  148. * LMC(0)_CONFIG[RANKMASK] then write both
  149. * LMC(0)_SEQ_CTL[SEQ_SEL,INIT_START] = 1 with a single CSR write
  150. * operation. LMC(0)_CONFIG[RANKMASK] bits should be set to indicate
  151. * the ranks that will participate in the sequence.
  152. *
  153. * The LMC(0)_SEQ_CTL[SEQ_SEL] value should select power-up/init or
  154. * selfrefresh exit, depending on whether the DRAM parts are in
  155. * self-refresh and whether their contents should be preserved. While
  156. * LMC performs these sequences, it will not perform any other DDR3
  157. * transactions. When the sequence is complete, hardware sets the
  158. * LMC(0)_CONFIG[INIT_STATUS] bits for the ranks that have been
  159. * initialized.
  160. *
  161. * If power-up/init is selected immediately following a DRESET
  162. * assertion, LMC executes the sequence described in the "Reset and
  163. * Initialization Procedure" section of the JEDEC DDR3
  164. * specification. This includes activating CKE, writing all four DDR3
  165. * mode registers on all selected ranks, and issuing the required
  166. * ZQCL
  167. * command. The LMC(0)_CONFIG[RANKMASK] value should select all ranks
  168. * with attached DRAM in this case. If LMC(0)_CONTROL[RDIMM_ENA] = 1,
  169. * LMC writes the JEDEC standard SSTE32882 control words selected by
  170. * LMC(0)_DIMM_CTL[DIMM*_WMASK] between DDR_CKE* signal assertion and
  171. * the first DDR3 mode register write operation.
  172. * LMC(0)_DIMM_CTL[DIMM*_WMASK] should be cleared to 0 if the
  173. * corresponding DIMM is not present.
  174. *
  175. * If self-refresh exit is selected, LMC executes the required SRX
  176. * command followed by a refresh and ZQ calibration. Section 4.5
  177. * describes behavior of a REF + ZQCS. LMC does not write the DDR3
  178. * mode registers as part of this sequence, and the mode register
  179. * parameters must match at self-refresh entry and exit times.
  180. *
  181. * 4. Read LMC(0)_SEQ_CTL and wait for LMC(0)_SEQ_CTL[SEQ_COMPLETE]
  182. * to be set.
  183. *
  184. * 5. Read LMC(0)_CONFIG[INIT_STATUS] and confirm that all ranks have
  185. * been initialized.
  186. */
  187. union cvmx_lmcx_seq_ctl seq_ctl;
  188. union cvmx_lmcx_config lmc_config;
  189. int timeout;
  190. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  191. lmc_config.s.rankmask = rank_mask;
  192. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), lmc_config.u64);
  193. seq_ctl.u64 = 0;
  194. seq_ctl.s.init_start = 1;
  195. seq_ctl.s.seq_sel = sequence;
  196. ddr_seq_print
  197. ("Performing LMC sequence: rank_mask=0x%02x, sequence=0x%x, %s\n",
  198. rank_mask, sequence, sequence_str[sequence]);
  199. if (seq_ctl.s.seq_sel == 3)
  200. debug("LMC%d: Exiting Self-refresh Rank_mask:%x\n", if_num,
  201. rank_mask);
  202. lmc_wr(priv, CVMX_LMCX_SEQ_CTL(if_num), seq_ctl.u64);
  203. lmc_rd(priv, CVMX_LMCX_SEQ_CTL(if_num));
  204. timeout = 100;
  205. do {
  206. udelay(100); /* Wait a while */
  207. seq_ctl.u64 = lmc_rd(priv, CVMX_LMCX_SEQ_CTL(if_num));
  208. if (--timeout == 0) {
  209. printf("Sequence %d timed out\n", sequence);
  210. break;
  211. }
  212. } while (seq_ctl.s.seq_complete != 1);
  213. ddr_seq_print(" LMC sequence=%x: Completed.\n", sequence);
  214. }
  215. #define bdk_numa_get_address(n, p) ((p) | ((u64)n) << CVMX_NODE_MEM_SHIFT)
  216. #define AREA_BASE_OFFSET BIT_ULL(26)
  217. static int test_dram_byte64(struct ddr_priv *priv, int lmc, u64 p,
  218. u64 bitmask, u64 *xor_data)
  219. {
  220. u64 p1, p2, d1, d2;
  221. u64 v, v1;
  222. u64 p2offset = (1ULL << 26); // offset to area 2
  223. u64 datamask;
  224. u64 xor;
  225. u64 i, j, k;
  226. u64 ii;
  227. int errors = 0;
  228. //u64 index;
  229. u64 pattern1 = cvmx_rng_get_random64();
  230. u64 pattern2 = 0;
  231. u64 bad_bits[2] = { 0, 0 };
  232. int kbitno = (octeon_is_cpuid(OCTEON_CN7XXX)) ? 20 : 18;
  233. union cvmx_l2c_ctl l2c_ctl;
  234. int burst;
  235. int saved_dissblkdty;
  236. int node = 0;
  237. // Force full cacheline write-backs to boost traffic
  238. l2c_ctl.u64 = l2c_rd(priv, CVMX_L2C_CTL_REL);
  239. saved_dissblkdty = l2c_ctl.cn78xx.dissblkdty;
  240. l2c_ctl.cn78xx.dissblkdty = 1;
  241. l2c_wr(priv, CVMX_L2C_CTL_REL, l2c_ctl.u64);
  242. if (octeon_is_cpuid(OCTEON_CN73XX) || octeon_is_cpuid(OCTEON_CNF75XX))
  243. kbitno = 18;
  244. // Byte lanes may be clear in the mask to indicate no testing on that
  245. //lane.
  246. datamask = bitmask;
  247. /*
  248. * Add offset to both test regions to not clobber boot stuff
  249. * when running from L2 for NAND boot.
  250. */
  251. p += AREA_BASE_OFFSET; // make sure base is out of the way of boot
  252. // final address must include LMC and node
  253. p |= (lmc << 7); /* Map address into proper interface */
  254. p = bdk_numa_get_address(node, p); /* Map to node */
  255. p |= 1ull << 63;
  256. #define II_INC BIT_ULL(22)
  257. #define II_MAX BIT_ULL(22)
  258. #define K_INC BIT_ULL(14)
  259. #define K_MAX BIT_ULL(kbitno)
  260. #define J_INC BIT_ULL(9)
  261. #define J_MAX BIT_ULL(12)
  262. #define I_INC BIT_ULL(3)
  263. #define I_MAX BIT_ULL(7)
  264. debug("N%d.LMC%d: %s: phys_addr=0x%llx/0x%llx (0x%llx)\n",
  265. node, lmc, __func__, p, p + p2offset, 1ULL << kbitno);
  266. // loops are ordered so that only a single 64-bit slot is written to
  267. // each cacheline at one time, then the cachelines are forced out;
  268. // this should maximize read/write traffic
  269. // FIXME? extend the range of memory tested!!
  270. for (ii = 0; ii < II_MAX; ii += II_INC) {
  271. for (i = 0; i < I_MAX; i += I_INC) {
  272. for (k = 0; k < K_MAX; k += K_INC) {
  273. for (j = 0; j < J_MAX; j += J_INC) {
  274. p1 = p + ii + k + j;
  275. p2 = p1 + p2offset;
  276. v = pattern1 * (p1 + i);
  277. // write the same thing to both areas
  278. v1 = v;
  279. cvmx_write64_uint64(p1 + i, v);
  280. cvmx_write64_uint64(p2 + i, v1);
  281. CVMX_CACHE_WBIL2(p1, 0);
  282. CVMX_CACHE_WBIL2(p2, 0);
  283. }
  284. }
  285. }
  286. }
  287. CVMX_DCACHE_INVALIDATE;
  288. debug("N%d.LMC%d: dram_tuning_mem_xor: done INIT loop\n", node, lmc);
  289. /* Make a series of passes over the memory areas. */
  290. for (burst = 0; burst < 1 /* was: dram_tune_use_bursts */ ; burst++) {
  291. u64 this_pattern = cvmx_rng_get_random64();
  292. pattern2 ^= this_pattern;
  293. /*
  294. * XOR the data with a random value, applying the change to both
  295. * memory areas.
  296. */
  297. // FIXME? extend the range of memory tested!!
  298. for (ii = 0; ii < II_MAX; ii += II_INC) {
  299. // FIXME: rearranged, did not make much difference?
  300. for (i = 0; i < I_MAX; i += I_INC) {
  301. for (k = 0; k < K_MAX; k += K_INC) {
  302. for (j = 0; j < J_MAX; j += J_INC) {
  303. p1 = p + ii + k + j;
  304. p2 = p1 + p2offset;
  305. v = cvmx_read64_uint64(p1 +
  306. i) ^
  307. this_pattern;
  308. v1 = cvmx_read64_uint64(p2 +
  309. i) ^
  310. this_pattern;
  311. cvmx_write64_uint64(p1 + i, v);
  312. cvmx_write64_uint64(p2 + i, v1);
  313. CVMX_CACHE_WBIL2(p1, 0);
  314. CVMX_CACHE_WBIL2(p2, 0);
  315. }
  316. }
  317. }
  318. }
  319. CVMX_DCACHE_INVALIDATE;
  320. debug("N%d.LMC%d: dram_tuning_mem_xor: done MODIFY loop\n",
  321. node, lmc);
  322. /*
  323. * Look for differences in the areas. If there is a mismatch,
  324. * reset both memory locations with the same pattern. Failing
  325. * to do so means that on all subsequent passes the pair of
  326. * locations remain out of sync giving spurious errors.
  327. */
  328. // FIXME: Change the loop order so that an entire cache line
  329. // is compared at one time. This is so that a read
  330. // error that occurs *anywhere* on the cacheline will
  331. // be caught, rather than comparing only 1 cacheline
  332. // slot at a time, where an error on a different
  333. // slot will be missed that time around
  334. // Does the above make sense?
  335. // FIXME? extend the range of memory tested!!
  336. for (ii = 0; ii < II_MAX; ii += II_INC) {
  337. for (k = 0; k < K_MAX; k += K_INC) {
  338. for (j = 0; j < J_MAX; j += J_INC) {
  339. p1 = p + ii + k + j;
  340. p2 = p1 + p2offset;
  341. // process entire cachelines in the
  342. //innermost loop
  343. for (i = 0; i < I_MAX; i += I_INC) {
  344. int bybit = 1;
  345. // start in byte lane 0
  346. u64 bymsk = 0xffULL;
  347. // FIXME: this should predict
  348. // what we find...???
  349. v = ((p1 + i) * pattern1) ^
  350. pattern2;
  351. d1 = cvmx_read64_uint64(p1 + i);
  352. d2 = cvmx_read64_uint64(p2 + i);
  353. // union of error bits only in
  354. // active byte lanes
  355. xor = ((d1 ^ v) | (d2 ^ v)) &
  356. datamask;
  357. if (!xor)
  358. continue;
  359. // accumulate bad bits
  360. bad_bits[0] |= xor;
  361. while (xor != 0) {
  362. debug("ERROR(%03d): [0x%016llX] [0x%016llX] expected 0x%016llX d1 %016llX d2 %016llX\n",
  363. burst, p1, p2, v,
  364. d1, d2);
  365. // error(s) in this lane
  366. if (xor & bymsk) {
  367. // set the byte
  368. // error bit
  369. errors |= bybit;
  370. // clear byte
  371. // lane in
  372. // error bits
  373. xor &= ~bymsk;
  374. // clear the
  375. // byte lane in
  376. // the mask
  377. datamask &= ~bymsk;
  378. #if EXIT_WHEN_ALL_LANES_HAVE_ERRORS
  379. // nothing
  380. // left to do
  381. if (datamask == 0) {
  382. return errors;
  383. }
  384. #endif /* EXIT_WHEN_ALL_LANES_HAVE_ERRORS */
  385. }
  386. // move mask into
  387. // next byte lane
  388. bymsk <<= 8;
  389. // move bit into next
  390. // byte position
  391. bybit <<= 1;
  392. }
  393. }
  394. CVMX_CACHE_WBIL2(p1, 0);
  395. CVMX_CACHE_WBIL2(p2, 0);
  396. }
  397. }
  398. }
  399. debug("N%d.LMC%d: dram_tuning_mem_xor: done TEST loop\n",
  400. node, lmc);
  401. }
  402. if (xor_data) { // send the bad bits back...
  403. xor_data[0] = bad_bits[0];
  404. xor_data[1] = bad_bits[1]; // let it be zeroed
  405. }
  406. // Restore original setting that could enable partial cacheline writes
  407. l2c_ctl.u64 = l2c_rd(priv, CVMX_L2C_CTL_REL);
  408. l2c_ctl.cn78xx.dissblkdty = saved_dissblkdty;
  409. l2c_wr(priv, CVMX_L2C_CTL_REL, l2c_ctl.u64);
  410. return errors;
  411. }
  412. static void ddr4_mrw(struct ddr_priv *priv, int if_num, int rank,
  413. int mr_wr_addr, int mr_wr_sel, int mr_wr_bg1)
  414. {
  415. union cvmx_lmcx_mr_mpr_ctl lmc_mr_mpr_ctl;
  416. lmc_mr_mpr_ctl.u64 = 0;
  417. lmc_mr_mpr_ctl.cn78xx.mr_wr_addr = (mr_wr_addr == -1) ? 0 : mr_wr_addr;
  418. lmc_mr_mpr_ctl.cn78xx.mr_wr_sel = mr_wr_sel;
  419. lmc_mr_mpr_ctl.cn78xx.mr_wr_rank = rank;
  420. lmc_mr_mpr_ctl.cn78xx.mr_wr_use_default_value =
  421. (mr_wr_addr == -1) ? 1 : 0;
  422. lmc_mr_mpr_ctl.cn78xx.mr_wr_bg1 = mr_wr_bg1;
  423. lmc_wr(priv, CVMX_LMCX_MR_MPR_CTL(if_num), lmc_mr_mpr_ctl.u64);
  424. /* Mode Register Write */
  425. oct3_ddr3_seq(priv, 1 << rank, if_num, 0x8);
  426. }
  427. #define INV_A0_17(x) ((x) ^ 0x22bf8)
  428. static void set_mpr_mode(struct ddr_priv *priv, int rank_mask,
  429. int if_num, int dimm_count, int mpr, int bg1)
  430. {
  431. int rankx;
  432. debug("All Ranks: Set mpr mode = %x %c-side\n",
  433. mpr, (bg1 == 0) ? 'A' : 'B');
  434. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  435. if (!(rank_mask & (1 << rankx)))
  436. continue;
  437. if (bg1 == 0) {
  438. /* MR3 A-side */
  439. ddr4_mrw(priv, if_num, rankx, mpr << 2, 3, bg1);
  440. } else {
  441. /* MR3 B-side */
  442. ddr4_mrw(priv, if_num, rankx, INV_A0_17(mpr << 2), ~3,
  443. bg1);
  444. }
  445. }
  446. }
  447. static void do_ddr4_mpr_read(struct ddr_priv *priv, int if_num,
  448. int rank, int page, int location)
  449. {
  450. union cvmx_lmcx_mr_mpr_ctl lmc_mr_mpr_ctl;
  451. lmc_mr_mpr_ctl.u64 = lmc_rd(priv, CVMX_LMCX_MR_MPR_CTL(if_num));
  452. lmc_mr_mpr_ctl.cn70xx.mr_wr_addr = 0;
  453. lmc_mr_mpr_ctl.cn70xx.mr_wr_sel = page; /* Page */
  454. lmc_mr_mpr_ctl.cn70xx.mr_wr_rank = rank;
  455. lmc_mr_mpr_ctl.cn70xx.mpr_loc = location;
  456. lmc_mr_mpr_ctl.cn70xx.mpr_wr = 0; /* Read=0, Write=1 */
  457. lmc_wr(priv, CVMX_LMCX_MR_MPR_CTL(if_num), lmc_mr_mpr_ctl.u64);
  458. /* MPR register access sequence */
  459. oct3_ddr3_seq(priv, 1 << rank, if_num, 0x9);
  460. debug("LMC_MR_MPR_CTL : 0x%016llx\n",
  461. lmc_mr_mpr_ctl.u64);
  462. debug("lmc_mr_mpr_ctl.cn70xx.mr_wr_addr: 0x%02x\n",
  463. lmc_mr_mpr_ctl.cn70xx.mr_wr_addr);
  464. debug("lmc_mr_mpr_ctl.cn70xx.mr_wr_sel : 0x%02x\n",
  465. lmc_mr_mpr_ctl.cn70xx.mr_wr_sel);
  466. debug("lmc_mr_mpr_ctl.cn70xx.mpr_loc : 0x%02x\n",
  467. lmc_mr_mpr_ctl.cn70xx.mpr_loc);
  468. debug("lmc_mr_mpr_ctl.cn70xx.mpr_wr : 0x%02x\n",
  469. lmc_mr_mpr_ctl.cn70xx.mpr_wr);
  470. }
  471. static int set_rdimm_mode(struct ddr_priv *priv, int if_num, int enable)
  472. {
  473. union cvmx_lmcx_control lmc_control;
  474. int save_rdimm_mode;
  475. lmc_control.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  476. save_rdimm_mode = lmc_control.s.rdimm_ena;
  477. lmc_control.s.rdimm_ena = enable;
  478. debug("Setting RDIMM_ENA = %x\n", enable);
  479. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), lmc_control.u64);
  480. return save_rdimm_mode;
  481. }
  482. static void ddr4_mpr_read(struct ddr_priv *priv, int if_num, int rank,
  483. int page, int location, u64 *mpr_data)
  484. {
  485. do_ddr4_mpr_read(priv, if_num, rank, page, location);
  486. mpr_data[0] = lmc_rd(priv, CVMX_LMCX_MPR_DATA0(if_num));
  487. }
  488. /* Display MPR values for Page */
  489. static void display_mpr_page(struct ddr_priv *priv, int rank_mask,
  490. int if_num, int page)
  491. {
  492. int rankx, location;
  493. u64 mpr_data[3];
  494. for (rankx = 0; rankx < 4; rankx++) {
  495. if (!(rank_mask & (1 << rankx)))
  496. continue;
  497. debug("N0.LMC%d.R%d: MPR Page %d loc [0:3]: ",
  498. if_num, rankx, page);
  499. for (location = 0; location < 4; location++) {
  500. ddr4_mpr_read(priv, if_num, rankx, page, location,
  501. mpr_data);
  502. debug("0x%02llx ", mpr_data[0] & 0xFF);
  503. }
  504. debug("\n");
  505. } /* for (rankx = 0; rankx < 4; rankx++) */
  506. }
  507. static void ddr4_mpr_write(struct ddr_priv *priv, int if_num, int rank,
  508. int page, int location, u8 mpr_data)
  509. {
  510. union cvmx_lmcx_mr_mpr_ctl lmc_mr_mpr_ctl;
  511. lmc_mr_mpr_ctl.u64 = 0;
  512. lmc_mr_mpr_ctl.cn70xx.mr_wr_addr = mpr_data;
  513. lmc_mr_mpr_ctl.cn70xx.mr_wr_sel = page; /* Page */
  514. lmc_mr_mpr_ctl.cn70xx.mr_wr_rank = rank;
  515. lmc_mr_mpr_ctl.cn70xx.mpr_loc = location;
  516. lmc_mr_mpr_ctl.cn70xx.mpr_wr = 1; /* Read=0, Write=1 */
  517. lmc_wr(priv, CVMX_LMCX_MR_MPR_CTL(if_num), lmc_mr_mpr_ctl.u64);
  518. /* MPR register access sequence */
  519. oct3_ddr3_seq(priv, 1 << rank, if_num, 0x9);
  520. debug("LMC_MR_MPR_CTL : 0x%016llx\n",
  521. lmc_mr_mpr_ctl.u64);
  522. debug("lmc_mr_mpr_ctl.cn70xx.mr_wr_addr: 0x%02x\n",
  523. lmc_mr_mpr_ctl.cn70xx.mr_wr_addr);
  524. debug("lmc_mr_mpr_ctl.cn70xx.mr_wr_sel : 0x%02x\n",
  525. lmc_mr_mpr_ctl.cn70xx.mr_wr_sel);
  526. debug("lmc_mr_mpr_ctl.cn70xx.mpr_loc : 0x%02x\n",
  527. lmc_mr_mpr_ctl.cn70xx.mpr_loc);
  528. debug("lmc_mr_mpr_ctl.cn70xx.mpr_wr : 0x%02x\n",
  529. lmc_mr_mpr_ctl.cn70xx.mpr_wr);
  530. }
  531. static void set_vref(struct ddr_priv *priv, int if_num, int rank,
  532. int range, int value)
  533. {
  534. union cvmx_lmcx_mr_mpr_ctl lmc_mr_mpr_ctl;
  535. union cvmx_lmcx_modereg_params3 lmc_modereg_params3;
  536. int mr_wr_addr = 0;
  537. lmc_mr_mpr_ctl.u64 = 0;
  538. lmc_modereg_params3.u64 = lmc_rd(priv,
  539. CVMX_LMCX_MODEREG_PARAMS3(if_num));
  540. /* A12:A10 tCCD_L */
  541. mr_wr_addr |= lmc_modereg_params3.s.tccd_l << 10;
  542. mr_wr_addr |= 1 << 7; /* A7 1 = Enable(Training Mode) */
  543. mr_wr_addr |= range << 6; /* A6 vrefDQ Training Range */
  544. mr_wr_addr |= value << 0; /* A5:A0 vrefDQ Training Value */
  545. lmc_mr_mpr_ctl.cn70xx.mr_wr_addr = mr_wr_addr;
  546. lmc_mr_mpr_ctl.cn70xx.mr_wr_sel = 6; /* Write MR6 */
  547. lmc_mr_mpr_ctl.cn70xx.mr_wr_rank = rank;
  548. lmc_wr(priv, CVMX_LMCX_MR_MPR_CTL(if_num), lmc_mr_mpr_ctl.u64);
  549. /* 0x8 = Mode Register Write */
  550. oct3_ddr3_seq(priv, 1 << rank, if_num, 0x8);
  551. /*
  552. * It is vendor specific whether vref_value is captured with A7=1.
  553. * A subsequent MRS might be necessary.
  554. */
  555. oct3_ddr3_seq(priv, 1 << rank, if_num, 0x8);
  556. mr_wr_addr &= ~(1 << 7); /* A7 0 = Disable(Training Mode) */
  557. lmc_mr_mpr_ctl.cn70xx.mr_wr_addr = mr_wr_addr;
  558. lmc_wr(priv, CVMX_LMCX_MR_MPR_CTL(if_num), lmc_mr_mpr_ctl.u64);
  559. }
  560. static void set_dram_output_inversion(struct ddr_priv *priv, int if_num,
  561. int dimm_count, int rank_mask,
  562. int inversion)
  563. {
  564. union cvmx_lmcx_ddr4_dimm_ctl lmc_ddr4_dimm_ctl;
  565. union cvmx_lmcx_dimmx_params lmc_dimmx_params;
  566. union cvmx_lmcx_dimm_ctl lmc_dimm_ctl;
  567. int dimm_no;
  568. /* Don't touch extenced register control words */
  569. lmc_ddr4_dimm_ctl.u64 = 0;
  570. lmc_wr(priv, CVMX_LMCX_DDR4_DIMM_CTL(if_num), lmc_ddr4_dimm_ctl.u64);
  571. debug("All DIMMs: Register Control Word RC0 : %x\n",
  572. (inversion & 1));
  573. for (dimm_no = 0; dimm_no < dimm_count; ++dimm_no) {
  574. lmc_dimmx_params.u64 =
  575. lmc_rd(priv, CVMX_LMCX_DIMMX_PARAMS(dimm_no, if_num));
  576. lmc_dimmx_params.s.rc0 =
  577. (lmc_dimmx_params.s.rc0 & ~1) | (inversion & 1);
  578. lmc_wr(priv,
  579. CVMX_LMCX_DIMMX_PARAMS(dimm_no, if_num),
  580. lmc_dimmx_params.u64);
  581. }
  582. /* LMC0_DIMM_CTL */
  583. lmc_dimm_ctl.u64 = lmc_rd(priv, CVMX_LMCX_DIMM_CTL(if_num));
  584. lmc_dimm_ctl.s.dimm0_wmask = 0x1;
  585. lmc_dimm_ctl.s.dimm1_wmask = (dimm_count > 1) ? 0x0001 : 0x0000;
  586. debug("LMC DIMM_CTL : 0x%016llx\n",
  587. lmc_dimm_ctl.u64);
  588. lmc_wr(priv, CVMX_LMCX_DIMM_CTL(if_num), lmc_dimm_ctl.u64);
  589. oct3_ddr3_seq(priv, rank_mask, if_num, 0x7); /* Init RCW */
  590. }
  591. static void write_mpr_page0_pattern(struct ddr_priv *priv, int rank_mask,
  592. int if_num, int dimm_count, int pattern,
  593. int location_mask)
  594. {
  595. int rankx;
  596. int location;
  597. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  598. if (!(rank_mask & (1 << rankx)))
  599. continue;
  600. for (location = 0; location < 4; ++location) {
  601. if (!(location_mask & (1 << location)))
  602. continue;
  603. ddr4_mpr_write(priv, if_num, rankx,
  604. /* page */ 0, /* location */ location,
  605. pattern);
  606. }
  607. }
  608. }
  609. static void change_rdimm_mpr_pattern(struct ddr_priv *priv, int rank_mask,
  610. int if_num, int dimm_count)
  611. {
  612. int save_ref_zqcs_int;
  613. union cvmx_lmcx_config lmc_config;
  614. /*
  615. * Okay, here is the latest sequence. This should work for all
  616. * chips and passes (78,88,73,etc). This sequence should be run
  617. * immediately after DRAM INIT. The basic idea is to write the
  618. * same pattern into each of the 4 MPR locations in the DRAM, so
  619. * that the same value is returned when doing MPR reads regardless
  620. * of the inversion state. My advice is to put this into a
  621. * function, change_rdimm_mpr_pattern or something like that, so
  622. * that it can be called multiple times, as I think David wants a
  623. * clock-like pattern for OFFSET training, but does not want a
  624. * clock pattern for Bit-Deskew. You should then be able to call
  625. * this at any point in the init sequence (after DRAM init) to
  626. * change the pattern to a new value.
  627. * Mike
  628. *
  629. * A correction: PHY doesn't need any pattern during offset
  630. * training, but needs clock like pattern for internal vref and
  631. * bit-dskew training. So for that reason, these steps below have
  632. * to be conducted before those trainings to pre-condition
  633. * the pattern. David
  634. *
  635. * Note: Step 3, 4, 8 and 9 have to be done through RDIMM
  636. * sequence. If you issue MRW sequence to do RCW write (in o78 pass
  637. * 1 at least), LMC will still do two commands because
  638. * CONTROL[RDIMM_ENA] is still set high. We don't want it to have
  639. * any unintentional mode register write so it's best to do what
  640. * Mike is doing here.
  641. * Andrew
  642. */
  643. /* 1) Disable refresh (REF_ZQCS_INT = 0) */
  644. debug("1) Disable refresh (REF_ZQCS_INT = 0)\n");
  645. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  646. save_ref_zqcs_int = lmc_config.cn78xx.ref_zqcs_int;
  647. lmc_config.cn78xx.ref_zqcs_int = 0;
  648. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), lmc_config.u64);
  649. /*
  650. * 2) Put all devices in MPR mode (Run MRW sequence (sequence=8)
  651. * with MODEREG_PARAMS0[MPRLOC]=0,
  652. * MODEREG_PARAMS0[MPR]=1, MR_MPR_CTL[MR_WR_SEL]=3, and
  653. * MR_MPR_CTL[MR_WR_USE_DEFAULT_VALUE]=1)
  654. */
  655. debug("2) Put all devices in MPR mode (Run MRW sequence (sequence=8)\n");
  656. /* A-side */
  657. set_mpr_mode(priv, rank_mask, if_num, dimm_count, 1, 0);
  658. /* B-side */
  659. set_mpr_mode(priv, rank_mask, if_num, dimm_count, 1, 1);
  660. /*
  661. * a. Or you can set MR_MPR_CTL[MR_WR_USE_DEFAULT_VALUE]=0 and set
  662. * the value you would like directly into
  663. * MR_MPR_CTL[MR_WR_ADDR]
  664. */
  665. /*
  666. * 3) Disable RCD Parity (if previously enabled) - parity does not
  667. * work if inversion disabled
  668. */
  669. debug("3) Disable RCD Parity\n");
  670. /*
  671. * 4) Disable Inversion in the RCD.
  672. * a. I did (3&4) via the RDIMM sequence (seq_sel=7), but it
  673. * may be easier to use the MRW sequence (seq_sel=8). Just set
  674. * MR_MPR_CTL[MR_WR_SEL]=7, MR_MPR_CTL[MR_WR_ADDR][3:0]=data,
  675. * MR_MPR_CTL[MR_WR_ADDR][7:4]=RCD reg
  676. */
  677. debug("4) Disable Inversion in the RCD.\n");
  678. set_dram_output_inversion(priv, if_num, dimm_count, rank_mask, 1);
  679. /*
  680. * 5) Disable CONTROL[RDIMM_ENA] so that MR sequence goes out
  681. * non-inverted.
  682. */
  683. debug("5) Disable CONTROL[RDIMM_ENA]\n");
  684. set_rdimm_mode(priv, if_num, 0);
  685. /*
  686. * 6) Write all 4 MPR registers with the desired pattern (have to
  687. * do this for all enabled ranks)
  688. * a. MR_MPR_CTL.MPR_WR=1, MR_MPR_CTL.MPR_LOC=0..3,
  689. * MR_MPR_CTL.MR_WR_SEL=0, MR_MPR_CTL.MR_WR_ADDR[7:0]=pattern
  690. */
  691. debug("6) Write all 4 MPR page 0 Training Patterns\n");
  692. write_mpr_page0_pattern(priv, rank_mask, if_num, dimm_count, 0x55, 0x8);
  693. /* 7) Re-enable RDIMM_ENA */
  694. debug("7) Re-enable RDIMM_ENA\n");
  695. set_rdimm_mode(priv, if_num, 1);
  696. /* 8) Re-enable RDIMM inversion */
  697. debug("8) Re-enable RDIMM inversion\n");
  698. set_dram_output_inversion(priv, if_num, dimm_count, rank_mask, 0);
  699. /* 9) Re-enable RDIMM parity (if desired) */
  700. debug("9) Re-enable RDIMM parity (if desired)\n");
  701. /*
  702. * 10)Take B-side devices out of MPR mode (Run MRW sequence
  703. * (sequence=8) with MODEREG_PARAMS0[MPRLOC]=0,
  704. * MODEREG_PARAMS0[MPR]=0, MR_MPR_CTL[MR_WR_SEL]=3, and
  705. * MR_MPR_CTL[MR_WR_USE_DEFAULT_VALUE]=1)
  706. */
  707. debug("10)Take B-side devices out of MPR mode\n");
  708. set_mpr_mode(priv, rank_mask, if_num, dimm_count,
  709. /* mpr */ 0, /* bg1 */ 1);
  710. /*
  711. * a. Or you can set MR_MPR_CTL[MR_WR_USE_DEFAULT_VALUE]=0 and
  712. * set the value you would like directly into MR_MPR_CTL[MR_WR_ADDR]
  713. */
  714. /* 11)Re-enable refresh (REF_ZQCS_INT=previous value) */
  715. debug("11)Re-enable refresh (REF_ZQCS_INT=previous value)\n");
  716. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  717. lmc_config.cn78xx.ref_zqcs_int = save_ref_zqcs_int;
  718. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), lmc_config.u64);
  719. }
  720. static int validate_hwl_seq(int *wl, int *seq)
  721. {
  722. // sequence index, step through the sequence array
  723. int seqx;
  724. int bitnum;
  725. seqx = 0;
  726. while (seq[seqx + 1] >= 0) { // stop on next seq entry == -1
  727. // but now, check current versus next
  728. bitnum = (wl[seq[seqx]] << 2) | wl[seq[seqx + 1]];
  729. // magic validity number (see matrix above)
  730. if (!((1 << bitnum) & 0xBDE7))
  731. return 1;
  732. seqx++;
  733. }
  734. return 0;
  735. }
  736. static int validate_hw_wl_settings(int if_num,
  737. union cvmx_lmcx_wlevel_rankx
  738. *lmc_wlevel_rank, int is_rdimm, int ecc_ena)
  739. {
  740. int wl[9], byte, errors;
  741. // arrange the sequences so
  742. // index 0 has byte 0, etc, ECC in middle
  743. int useq[] = { 0, 1, 2, 3, 8, 4, 5, 6, 7, -1 };
  744. // index 0 is ECC, then go down
  745. int rseq1[] = { 8, 3, 2, 1, 0, -1 };
  746. // index 0 has byte 4, then go up
  747. int rseq2[] = { 4, 5, 6, 7, -1 };
  748. // index 0 has byte 0, etc, no ECC
  749. int useqno[] = { 0, 1, 2, 3, 4, 5, 6, 7, -1 };
  750. // index 0 is byte 3, then go down, no ECC
  751. int rseq1no[] = { 3, 2, 1, 0, -1 };
  752. // in the CSR, bytes 0-7 are always data, byte 8 is ECC
  753. for (byte = 0; byte < (8 + ecc_ena); byte++) {
  754. // preprocess :-)
  755. wl[byte] = (get_wl_rank(lmc_wlevel_rank, byte) >>
  756. 1) & 3;
  757. }
  758. errors = 0;
  759. if (is_rdimm) { // RDIMM order
  760. errors = validate_hwl_seq(wl, (ecc_ena) ? rseq1 : rseq1no);
  761. errors += validate_hwl_seq(wl, rseq2);
  762. } else { // UDIMM order
  763. errors = validate_hwl_seq(wl, (ecc_ena) ? useq : useqno);
  764. }
  765. return errors;
  766. }
  767. static unsigned int extr_wr(u64 u, int x)
  768. {
  769. return (unsigned int)(((u >> (x * 12 + 5)) & 0x3ULL) |
  770. ((u >> (51 + x - 2)) & 0x4ULL));
  771. }
  772. static void insrt_wr(u64 *up, int x, int v)
  773. {
  774. u64 u = *up;
  775. u &= ~(((0x3ULL) << (x * 12 + 5)) | ((0x1ULL) << (51 + x)));
  776. *up = (u | ((v & 0x3ULL) << (x * 12 + 5)) |
  777. ((v & 0x4ULL) << (51 + x - 2)));
  778. }
  779. /* Read out Deskew Settings for DDR */
  780. struct deskew_bytes {
  781. u16 bits[8];
  782. };
  783. struct deskew_data {
  784. struct deskew_bytes bytes[9];
  785. };
  786. struct dac_data {
  787. int bytes[9];
  788. };
  789. // T88 pass 1, skip 4=DAC
  790. static const u8 dsk_bit_seq_p1[8] = { 0, 1, 2, 3, 5, 6, 7, 8 };
  791. // T88 Pass 2, skip 4=DAC and 5=DBI
  792. static const u8 dsk_bit_seq_p2[8] = { 0, 1, 2, 3, 6, 7, 8, 9 };
  793. static void get_deskew_settings(struct ddr_priv *priv, int if_num,
  794. struct deskew_data *dskdat)
  795. {
  796. union cvmx_lmcx_phy_ctl phy_ctl;
  797. union cvmx_lmcx_config lmc_config;
  798. int bit_index;
  799. int byte_lane, byte_limit;
  800. // NOTE: these are for pass 2.x
  801. int is_o78p2 = !octeon_is_cpuid(OCTEON_CN78XX_PASS1_X);
  802. const u8 *bit_seq = (is_o78p2) ? dsk_bit_seq_p2 : dsk_bit_seq_p1;
  803. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  804. byte_limit = ((!lmc_config.s.mode32b) ? 8 : 4) + lmc_config.s.ecc_ena;
  805. memset(dskdat, 0, sizeof(*dskdat));
  806. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  807. phy_ctl.s.dsk_dbg_clk_scaler = 3;
  808. for (byte_lane = 0; byte_lane < byte_limit; byte_lane++) {
  809. phy_ctl.s.dsk_dbg_byte_sel = byte_lane; // set byte lane
  810. for (bit_index = 0; bit_index < 8; ++bit_index) {
  811. // set bit number and start read sequence
  812. phy_ctl.s.dsk_dbg_bit_sel = bit_seq[bit_index];
  813. phy_ctl.s.dsk_dbg_rd_start = 1;
  814. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  815. // poll for read sequence to complete
  816. do {
  817. phy_ctl.u64 =
  818. lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  819. } while (phy_ctl.s.dsk_dbg_rd_complete != 1);
  820. // record the data
  821. dskdat->bytes[byte_lane].bits[bit_index] =
  822. phy_ctl.s.dsk_dbg_rd_data & 0x3ff;
  823. }
  824. }
  825. }
  826. static void display_deskew_settings(struct ddr_priv *priv, int if_num,
  827. struct deskew_data *dskdat,
  828. int print_enable)
  829. {
  830. int byte_lane;
  831. int bit_num;
  832. u16 flags, deskew;
  833. union cvmx_lmcx_config lmc_config;
  834. int byte_limit;
  835. const char *fc = " ?-=+*#&";
  836. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  837. byte_limit = ((lmc_config.s.mode32b) ? 4 : 8) + lmc_config.s.ecc_ena;
  838. if (print_enable) {
  839. debug("N0.LMC%d: Deskew Data: Bit => :",
  840. if_num);
  841. for (bit_num = 7; bit_num >= 0; --bit_num)
  842. debug(" %3d ", bit_num);
  843. debug("\n");
  844. }
  845. for (byte_lane = 0; byte_lane < byte_limit; byte_lane++) {
  846. if (print_enable)
  847. debug("N0.LMC%d: Bit Deskew Byte %d %s :",
  848. if_num, byte_lane,
  849. (print_enable >= 3) ? "FINAL" : " ");
  850. for (bit_num = 7; bit_num >= 0; --bit_num) {
  851. flags = dskdat->bytes[byte_lane].bits[bit_num] & 7;
  852. deskew = dskdat->bytes[byte_lane].bits[bit_num] >> 3;
  853. if (print_enable)
  854. debug(" %3d %c", deskew, fc[flags ^ 1]);
  855. } /* for (bit_num = 7; bit_num >= 0; --bit_num) */
  856. if (print_enable)
  857. debug("\n");
  858. }
  859. }
  860. static void override_deskew_settings(struct ddr_priv *priv, int if_num,
  861. struct deskew_data *dskdat)
  862. {
  863. union cvmx_lmcx_phy_ctl phy_ctl;
  864. union cvmx_lmcx_config lmc_config;
  865. int bit, byte_lane, byte_limit;
  866. u64 csr_data;
  867. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  868. byte_limit = ((lmc_config.s.mode32b) ? 4 : 8) + lmc_config.s.ecc_ena;
  869. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  870. phy_ctl.s.phy_reset = 0;
  871. phy_ctl.s.dsk_dbg_num_bits_sel = 1;
  872. phy_ctl.s.dsk_dbg_offset = 0;
  873. phy_ctl.s.dsk_dbg_clk_scaler = 3;
  874. phy_ctl.s.dsk_dbg_wr_mode = 1;
  875. phy_ctl.s.dsk_dbg_load_dis = 0;
  876. phy_ctl.s.dsk_dbg_overwrt_ena = 0;
  877. phy_ctl.s.phy_dsk_reset = 0;
  878. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  879. lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  880. for (byte_lane = 0; byte_lane < byte_limit; byte_lane++) {
  881. csr_data = 0;
  882. // FIXME: can we ignore DBI?
  883. for (bit = 0; bit < 8; ++bit) {
  884. // fetch input and adjust
  885. u64 bits = (dskdat->bytes[byte_lane].bits[bit] >> 3) &
  886. 0x7F;
  887. /*
  888. * lmc_general_purpose0.data[6:0] // DQ0
  889. * lmc_general_purpose0.data[13:7] // DQ1
  890. * lmc_general_purpose0.data[20:14] // DQ2
  891. * lmc_general_purpose0.data[27:21] // DQ3
  892. * lmc_general_purpose0.data[34:28] // DQ4
  893. * lmc_general_purpose0.data[41:35] // DQ5
  894. * lmc_general_purpose0.data[48:42] // DQ6
  895. * lmc_general_purpose0.data[55:49] // DQ7
  896. * lmc_general_purpose0.data[62:56] // DBI
  897. */
  898. csr_data |= (bits << (7 * bit));
  899. } /* for (bit = 0; bit < 8; ++bit) */
  900. // update GP0 with the bit data for this byte lane
  901. lmc_wr(priv, CVMX_LMCX_GENERAL_PURPOSE0(if_num), csr_data);
  902. lmc_rd(priv, CVMX_LMCX_GENERAL_PURPOSE0(if_num));
  903. // start the deskew load sequence
  904. phy_ctl.s.dsk_dbg_byte_sel = byte_lane;
  905. phy_ctl.s.dsk_dbg_rd_start = 1;
  906. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  907. // poll for read sequence to complete
  908. do {
  909. udelay(100);
  910. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  911. } while (phy_ctl.s.dsk_dbg_rd_complete != 1);
  912. }
  913. // tell phy to use the new settings
  914. phy_ctl.s.dsk_dbg_overwrt_ena = 1;
  915. phy_ctl.s.dsk_dbg_rd_start = 0;
  916. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  917. phy_ctl.s.dsk_dbg_wr_mode = 0;
  918. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  919. }
  920. static void process_by_rank_dac(struct ddr_priv *priv, int if_num,
  921. int rank_mask, struct dac_data *dacdat)
  922. {
  923. union cvmx_lmcx_config lmc_config;
  924. int rankx, byte_lane;
  925. int byte_limit;
  926. int rank_count;
  927. struct dac_data dacsum;
  928. int lane_probs;
  929. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  930. byte_limit = ((lmc_config.s.mode32b) ? 4 : 8) + lmc_config.s.ecc_ena;
  931. memset((void *)&dacsum, 0, sizeof(dacsum));
  932. rank_count = 0;
  933. lane_probs = 0;
  934. for (rankx = 0; rankx < 4; rankx++) {
  935. if (!(rank_mask & (1 << rankx)))
  936. continue;
  937. rank_count++;
  938. display_dac_dbi_settings(if_num, /*dac */ 1,
  939. lmc_config.s.ecc_ena,
  940. &dacdat[rankx].bytes[0],
  941. "By-Ranks VREF");
  942. // sum
  943. for (byte_lane = 0; byte_lane < byte_limit; byte_lane++) {
  944. if (rank_count == 2) {
  945. int ranks_diff =
  946. abs((dacsum.bytes[byte_lane] -
  947. dacdat[rankx].bytes[byte_lane]));
  948. // FIXME: is 19 a good number?
  949. if (ranks_diff > 19)
  950. lane_probs |= (1 << byte_lane);
  951. }
  952. dacsum.bytes[byte_lane] +=
  953. dacdat[rankx].bytes[byte_lane];
  954. }
  955. }
  956. // average
  957. for (byte_lane = 0; byte_lane < byte_limit; byte_lane++)
  958. dacsum.bytes[byte_lane] /= rank_count; // FIXME: nint?
  959. display_dac_dbi_settings(if_num, /*dac */ 1, lmc_config.s.ecc_ena,
  960. &dacsum.bytes[0], "All-Rank VREF");
  961. if (lane_probs) {
  962. debug("N0.LMC%d: All-Rank VREF DAC Problem Bytelane(s): 0x%03x\n",
  963. if_num, lane_probs);
  964. }
  965. // finally, write the averaged DAC values
  966. for (byte_lane = 0; byte_lane < byte_limit; byte_lane++) {
  967. load_dac_override(priv, if_num, dacsum.bytes[byte_lane],
  968. byte_lane);
  969. }
  970. }
  971. static void process_by_rank_dsk(struct ddr_priv *priv, int if_num,
  972. int rank_mask, struct deskew_data *dskdat)
  973. {
  974. union cvmx_lmcx_config lmc_config;
  975. int rankx, lane, bit;
  976. int byte_limit;
  977. struct deskew_data dsksum, dskcnt;
  978. u16 deskew;
  979. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  980. byte_limit = ((lmc_config.s.mode32b) ? 4 : 8) + lmc_config.s.ecc_ena;
  981. memset((void *)&dsksum, 0, sizeof(dsksum));
  982. memset((void *)&dskcnt, 0, sizeof(dskcnt));
  983. for (rankx = 0; rankx < 4; rankx++) {
  984. if (!(rank_mask & (1 << rankx)))
  985. continue;
  986. // sum ranks
  987. for (lane = 0; lane < byte_limit; lane++) {
  988. for (bit = 0; bit < 8; ++bit) {
  989. deskew = dskdat[rankx].bytes[lane].bits[bit];
  990. // if flags indicate sat hi or lo, skip it
  991. if (deskew & 6)
  992. continue;
  993. // clear flags
  994. dsksum.bytes[lane].bits[bit] +=
  995. deskew & ~7;
  996. // count entries
  997. dskcnt.bytes[lane].bits[bit] += 1;
  998. }
  999. }
  1000. }
  1001. // average ranks
  1002. for (lane = 0; lane < byte_limit; lane++) {
  1003. for (bit = 0; bit < 8; ++bit) {
  1004. int div = dskcnt.bytes[lane].bits[bit];
  1005. if (div > 0) {
  1006. dsksum.bytes[lane].bits[bit] /= div;
  1007. // clear flags
  1008. dsksum.bytes[lane].bits[bit] &= ~7;
  1009. // set LOCK
  1010. dsksum.bytes[lane].bits[bit] |= 1;
  1011. } else {
  1012. // FIXME? use reset value?
  1013. dsksum.bytes[lane].bits[bit] =
  1014. (64 << 3) | 1;
  1015. }
  1016. }
  1017. }
  1018. // TME for FINAL version
  1019. display_deskew_settings(priv, if_num, &dsksum, /*VBL_TME */ 3);
  1020. // finally, write the averaged DESKEW values
  1021. override_deskew_settings(priv, if_num, &dsksum);
  1022. }
  1023. struct deskew_counts {
  1024. int saturated; // number saturated
  1025. int unlocked; // number unlocked
  1026. int nibrng_errs; // nibble range errors
  1027. int nibunl_errs; // nibble unlocked errors
  1028. int bitval_errs; // bit value errors
  1029. };
  1030. #define MIN_BITVAL 17
  1031. #define MAX_BITVAL 110
  1032. static void validate_deskew_training(struct ddr_priv *priv, int rank_mask,
  1033. int if_num, struct deskew_counts *counts,
  1034. int print_flags)
  1035. {
  1036. int byte_lane, bit_index, nib_num;
  1037. int nibrng_errs, nibunl_errs, bitval_errs;
  1038. union cvmx_lmcx_config lmc_config;
  1039. s16 nib_min[2], nib_max[2], nib_unl[2];
  1040. int byte_limit;
  1041. int print_enable = print_flags & 1;
  1042. struct deskew_data dskdat;
  1043. s16 flags, deskew;
  1044. const char *fc = " ?-=+*#&";
  1045. int bit_last;
  1046. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  1047. byte_limit = ((!lmc_config.s.mode32b) ? 8 : 4) + lmc_config.s.ecc_ena;
  1048. memset(counts, 0, sizeof(struct deskew_counts));
  1049. get_deskew_settings(priv, if_num, &dskdat);
  1050. if (print_enable) {
  1051. debug("N0.LMC%d: Deskew Settings: Bit => :",
  1052. if_num);
  1053. for (bit_index = 7; bit_index >= 0; --bit_index)
  1054. debug(" %3d ", bit_index);
  1055. debug("\n");
  1056. }
  1057. for (byte_lane = 0; byte_lane < byte_limit; byte_lane++) {
  1058. if (print_enable)
  1059. debug("N0.LMC%d: Bit Deskew Byte %d %s :",
  1060. if_num, byte_lane,
  1061. (print_flags & 2) ? "FINAL" : " ");
  1062. nib_min[0] = 127;
  1063. nib_min[1] = 127;
  1064. nib_max[0] = 0;
  1065. nib_max[1] = 0;
  1066. nib_unl[0] = 0;
  1067. nib_unl[1] = 0;
  1068. if (lmc_config.s.mode32b == 1 && byte_lane == 4) {
  1069. bit_last = 3;
  1070. if (print_enable)
  1071. debug(" ");
  1072. } else {
  1073. bit_last = 7;
  1074. }
  1075. for (bit_index = bit_last; bit_index >= 0; --bit_index) {
  1076. nib_num = (bit_index > 3) ? 1 : 0;
  1077. flags = dskdat.bytes[byte_lane].bits[bit_index] & 7;
  1078. deskew = dskdat.bytes[byte_lane].bits[bit_index] >> 3;
  1079. counts->saturated += !!(flags & 6);
  1080. // Do range calc even when locked; it could happen
  1081. // that a bit is still unlocked after final retry,
  1082. // and we want to have an external retry if a RANGE
  1083. // error is present at exit...
  1084. nib_min[nib_num] = min(nib_min[nib_num], deskew);
  1085. nib_max[nib_num] = max(nib_max[nib_num], deskew);
  1086. if (!(flags & 1)) { // only when not locked
  1087. counts->unlocked += 1;
  1088. nib_unl[nib_num] += 1;
  1089. }
  1090. if (print_enable)
  1091. debug(" %3d %c", deskew, fc[flags ^ 1]);
  1092. }
  1093. /*
  1094. * Now look for nibble errors
  1095. *
  1096. * For bit 55, it looks like a bit deskew problem. When the
  1097. * upper nibble of byte 6 needs to go to saturation, bit 7
  1098. * of byte 6 locks prematurely at 64. For DIMMs with raw
  1099. * card A and B, can we reset the deskew training when we
  1100. * encounter this case? The reset criteria should be looking
  1101. * at one nibble at a time for raw card A and B; if the
  1102. * bit-deskew setting within a nibble is different by > 33,
  1103. * we'll issue a reset to the bit deskew training.
  1104. *
  1105. * LMC0 Bit Deskew Byte(6): 64 0 - 0 - 0 - 26 61 35 64
  1106. */
  1107. // upper nibble range, then lower nibble range
  1108. nibrng_errs = ((nib_max[1] - nib_min[1]) > 33) ? 1 : 0;
  1109. nibrng_errs |= ((nib_max[0] - nib_min[0]) > 33) ? 1 : 0;
  1110. // check for nibble all unlocked
  1111. nibunl_errs = ((nib_unl[0] == 4) || (nib_unl[1] == 4)) ? 1 : 0;
  1112. // check for bit value errors, ie < 17 or > 110
  1113. // FIXME? assume max always > MIN_BITVAL and min < MAX_BITVAL
  1114. bitval_errs = ((nib_max[1] > MAX_BITVAL) ||
  1115. (nib_max[0] > MAX_BITVAL)) ? 1 : 0;
  1116. bitval_errs |= ((nib_min[1] < MIN_BITVAL) ||
  1117. (nib_min[0] < MIN_BITVAL)) ? 1 : 0;
  1118. if ((nibrng_errs != 0 || nibunl_errs != 0 ||
  1119. bitval_errs != 0) && print_enable) {
  1120. debug(" %c%c%c",
  1121. (nibrng_errs) ? 'R' : ' ',
  1122. (nibunl_errs) ? 'U' : ' ',
  1123. (bitval_errs) ? 'V' : ' ');
  1124. }
  1125. if (print_enable)
  1126. debug("\n");
  1127. counts->nibrng_errs |= (nibrng_errs << byte_lane);
  1128. counts->nibunl_errs |= (nibunl_errs << byte_lane);
  1129. counts->bitval_errs |= (bitval_errs << byte_lane);
  1130. }
  1131. }
  1132. static unsigned short load_dac_override(struct ddr_priv *priv, int if_num,
  1133. int dac_value, int byte)
  1134. {
  1135. union cvmx_lmcx_dll_ctl3 ddr_dll_ctl3;
  1136. // single bytelanes incr by 1; A is for ALL
  1137. int bytex = (byte == 0x0A) ? byte : byte + 1;
  1138. ddr_dll_ctl3.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL3(if_num));
  1139. SET_DDR_DLL_CTL3(byte_sel, bytex);
  1140. SET_DDR_DLL_CTL3(offset, dac_value >> 1);
  1141. ddr_dll_ctl3.cn73xx.bit_select = 0x9; /* No-op */
  1142. lmc_wr(priv, CVMX_LMCX_DLL_CTL3(if_num), ddr_dll_ctl3.u64);
  1143. ddr_dll_ctl3.cn73xx.bit_select = 0xC; /* vref bypass setting load */
  1144. lmc_wr(priv, CVMX_LMCX_DLL_CTL3(if_num), ddr_dll_ctl3.u64);
  1145. ddr_dll_ctl3.cn73xx.bit_select = 0xD; /* vref bypass on. */
  1146. lmc_wr(priv, CVMX_LMCX_DLL_CTL3(if_num), ddr_dll_ctl3.u64);
  1147. ddr_dll_ctl3.cn73xx.bit_select = 0x9; /* No-op */
  1148. lmc_wr(priv, CVMX_LMCX_DLL_CTL3(if_num), ddr_dll_ctl3.u64);
  1149. lmc_rd(priv, CVMX_LMCX_DLL_CTL3(if_num)); // flush writes
  1150. return (unsigned short)GET_DDR_DLL_CTL3(offset);
  1151. }
  1152. // arg dac_or_dbi is 1 for DAC, 0 for DBI
  1153. // returns 9 entries (bytelanes 0 through 8) in settings[]
  1154. // returns 0 if OK, -1 if a problem
  1155. static int read_dac_dbi_settings(struct ddr_priv *priv, int if_num,
  1156. int dac_or_dbi, int *settings)
  1157. {
  1158. union cvmx_lmcx_phy_ctl phy_ctl;
  1159. int byte_lane, bit_num;
  1160. int deskew;
  1161. int dac_value;
  1162. int new_deskew_layout = 0;
  1163. new_deskew_layout = octeon_is_cpuid(OCTEON_CN73XX) ||
  1164. octeon_is_cpuid(OCTEON_CNF75XX);
  1165. new_deskew_layout |= (octeon_is_cpuid(OCTEON_CN78XX) &&
  1166. !octeon_is_cpuid(OCTEON_CN78XX_PASS1_X));
  1167. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  1168. phy_ctl.s.dsk_dbg_clk_scaler = 3;
  1169. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  1170. bit_num = (dac_or_dbi) ? 4 : 5;
  1171. // DBI not available
  1172. if (bit_num == 5 && !new_deskew_layout)
  1173. return -1;
  1174. // FIXME: always assume ECC is available
  1175. for (byte_lane = 8; byte_lane >= 0; --byte_lane) {
  1176. //set byte lane and bit to read
  1177. phy_ctl.s.dsk_dbg_bit_sel = bit_num;
  1178. phy_ctl.s.dsk_dbg_byte_sel = byte_lane;
  1179. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  1180. //start read sequence
  1181. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  1182. phy_ctl.s.dsk_dbg_rd_start = 1;
  1183. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  1184. //poll for read sequence to complete
  1185. do {
  1186. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  1187. } while (phy_ctl.s.dsk_dbg_rd_complete != 1);
  1188. // keep the flag bits where they are for DBI
  1189. deskew = phy_ctl.s.dsk_dbg_rd_data; /* >> 3 */
  1190. dac_value = phy_ctl.s.dsk_dbg_rd_data & 0xff;
  1191. settings[byte_lane] = (dac_or_dbi) ? dac_value : deskew;
  1192. }
  1193. return 0;
  1194. }
  1195. // print out the DBI settings array
  1196. // arg dac_or_dbi is 1 for DAC, 0 for DBI
  1197. static void display_dac_dbi_settings(int lmc, int dac_or_dbi,
  1198. int ecc_ena, int *settings, char *title)
  1199. {
  1200. int byte;
  1201. int flags;
  1202. int deskew;
  1203. const char *fc = " ?-=+*#&";
  1204. debug("N0.LMC%d: %s %s Settings %d:0 :",
  1205. lmc, title, (dac_or_dbi) ? "DAC" : "DBI", 7 + ecc_ena);
  1206. // FIXME: what about 32-bit mode?
  1207. for (byte = (7 + ecc_ena); byte >= 0; --byte) {
  1208. if (dac_or_dbi) { // DAC
  1209. flags = 1; // say its locked to get blank
  1210. deskew = settings[byte] & 0xff;
  1211. } else { // DBI
  1212. flags = settings[byte] & 7;
  1213. deskew = (settings[byte] >> 3) & 0x7f;
  1214. }
  1215. debug(" %3d %c", deskew, fc[flags ^ 1]);
  1216. }
  1217. debug("\n");
  1218. }
  1219. // Find a HWL majority
  1220. static int find_wl_majority(struct wlevel_bitcnt *bc, int *mx, int *mc,
  1221. int *xc, int *cc)
  1222. {
  1223. int ix, ic;
  1224. *mx = -1;
  1225. *mc = 0;
  1226. *xc = 0;
  1227. *cc = 0;
  1228. for (ix = 0; ix < 4; ix++) {
  1229. ic = bc->bitcnt[ix];
  1230. // make a bitmask of the ones with a count
  1231. if (ic > 0) {
  1232. *mc |= (1 << ix);
  1233. *cc += 1; // count how many had non-zero counts
  1234. }
  1235. // find the majority
  1236. if (ic > *xc) { // new max?
  1237. *xc = ic; // yes
  1238. *mx = ix; // set its index
  1239. }
  1240. }
  1241. return (*mx << 1);
  1242. }
  1243. // Evaluate the DAC settings array
  1244. static int evaluate_dac_settings(int if_64b, int ecc_ena, int *settings)
  1245. {
  1246. int byte, lane, dac, comp;
  1247. int last = (if_64b) ? 7 : 3;
  1248. // FIXME: change the check...???
  1249. // this looks only for sets of DAC values whose max/min differ by a lot
  1250. // let any EVEN go so long as it is within range...
  1251. for (byte = (last + ecc_ena); byte >= 0; --byte) {
  1252. dac = settings[byte] & 0xff;
  1253. for (lane = (last + ecc_ena); lane >= 0; --lane) {
  1254. comp = settings[lane] & 0xff;
  1255. if (abs((dac - comp)) > 25)
  1256. return 1;
  1257. }
  1258. }
  1259. return 0;
  1260. }
  1261. static void perform_offset_training(struct ddr_priv *priv, int rank_mask,
  1262. int if_num)
  1263. {
  1264. union cvmx_lmcx_phy_ctl lmc_phy_ctl;
  1265. u64 orig_phy_ctl;
  1266. const char *s;
  1267. /*
  1268. * 4.8.6 LMC Offset Training
  1269. *
  1270. * LMC requires input-receiver offset training.
  1271. *
  1272. * 1. Write LMC(0)_PHY_CTL[DAC_ON] = 1
  1273. */
  1274. lmc_phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  1275. orig_phy_ctl = lmc_phy_ctl.u64;
  1276. lmc_phy_ctl.s.dac_on = 1;
  1277. // allow full CSR override
  1278. s = lookup_env_ull(priv, "ddr_phy_ctl");
  1279. if (s)
  1280. lmc_phy_ctl.u64 = strtoull(s, NULL, 0);
  1281. // do not print or write if CSR does not change...
  1282. if (lmc_phy_ctl.u64 != orig_phy_ctl) {
  1283. debug("PHY_CTL : 0x%016llx\n",
  1284. lmc_phy_ctl.u64);
  1285. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), lmc_phy_ctl.u64);
  1286. }
  1287. /*
  1288. * 2. Write LMC(0)_SEQ_CTL[SEQ_SEL] = 0x0B and
  1289. * LMC(0)_SEQ_CTL[INIT_START] = 1.
  1290. *
  1291. * 3. Wait for LMC(0)_SEQ_CTL[SEQ_COMPLETE] to be set to 1.
  1292. */
  1293. /* Start Offset training sequence */
  1294. oct3_ddr3_seq(priv, rank_mask, if_num, 0x0B);
  1295. }
  1296. static void perform_internal_vref_training(struct ddr_priv *priv,
  1297. int rank_mask, int if_num)
  1298. {
  1299. union cvmx_lmcx_ext_config ext_config;
  1300. union cvmx_lmcx_dll_ctl3 ddr_dll_ctl3;
  1301. // First, make sure all byte-lanes are out of VREF bypass mode
  1302. ddr_dll_ctl3.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL3(if_num));
  1303. ddr_dll_ctl3.cn78xx.byte_sel = 0x0A; /* all byte-lanes */
  1304. ddr_dll_ctl3.cn78xx.bit_select = 0x09; /* No-op */
  1305. lmc_wr(priv, CVMX_LMCX_DLL_CTL3(if_num), ddr_dll_ctl3.u64);
  1306. ddr_dll_ctl3.cn78xx.bit_select = 0x0E; /* vref bypass off. */
  1307. lmc_wr(priv, CVMX_LMCX_DLL_CTL3(if_num), ddr_dll_ctl3.u64);
  1308. ddr_dll_ctl3.cn78xx.bit_select = 0x09; /* No-op */
  1309. lmc_wr(priv, CVMX_LMCX_DLL_CTL3(if_num), ddr_dll_ctl3.u64);
  1310. /*
  1311. * 4.8.7 LMC Internal vref Training
  1312. *
  1313. * LMC requires input-reference-voltage training.
  1314. *
  1315. * 1. Write LMC(0)_EXT_CONFIG[VREFINT_SEQ_DESKEW] = 0.
  1316. */
  1317. ext_config.u64 = lmc_rd(priv, CVMX_LMCX_EXT_CONFIG(if_num));
  1318. ext_config.s.vrefint_seq_deskew = 0;
  1319. ddr_seq_print("Performing LMC sequence: vrefint_seq_deskew = %d\n",
  1320. ext_config.s.vrefint_seq_deskew);
  1321. lmc_wr(priv, CVMX_LMCX_EXT_CONFIG(if_num), ext_config.u64);
  1322. /*
  1323. * 2. Write LMC(0)_SEQ_CTL[SEQ_SEL] = 0x0a and
  1324. * LMC(0)_SEQ_CTL[INIT_START] = 1.
  1325. *
  1326. * 3. Wait for LMC(0)_SEQ_CTL[SEQ_COMPLETE] to be set to 1.
  1327. */
  1328. /* Start LMC Internal vref Training */
  1329. oct3_ddr3_seq(priv, rank_mask, if_num, 0x0A);
  1330. }
  1331. #define dbg_avg(format, ...) // debug(format, ##__VA_ARGS__)
  1332. static int process_samples_average(s16 *bytes, int num_samples,
  1333. int lmc, int lane_no)
  1334. {
  1335. int i, sadj, sum = 0, ret, asum, trunc;
  1336. s16 smin = 32767, smax = -32768;
  1337. int nmin, nmax;
  1338. //int rng;
  1339. dbg_avg("DBG_AVG%d.%d: ", lmc, lane_no);
  1340. for (i = 0; i < num_samples; i++) {
  1341. sum += bytes[i];
  1342. if (bytes[i] < smin)
  1343. smin = bytes[i];
  1344. if (bytes[i] > smax)
  1345. smax = bytes[i];
  1346. dbg_avg(" %3d", bytes[i]);
  1347. }
  1348. nmin = 0;
  1349. nmax = 0;
  1350. for (i = 0; i < num_samples; i++) {
  1351. if (bytes[i] == smin)
  1352. nmin += 1;
  1353. if (bytes[i] == smax)
  1354. nmax += 1;
  1355. }
  1356. dbg_avg(" (min=%3d/%d, max=%3d/%d, range=%2d, samples=%2d)",
  1357. smin, nmin, smax, nmax, rng, num_samples);
  1358. asum = sum - smin - smax;
  1359. sadj = divide_nint(asum * 10, (num_samples - 2));
  1360. trunc = asum / (num_samples - 2);
  1361. dbg_avg(" [%3d.%d, %3d]", sadj / 10, sadj % 10, trunc);
  1362. sadj = divide_nint(sadj, 10);
  1363. if (trunc & 1)
  1364. ret = trunc;
  1365. else if (sadj & 1)
  1366. ret = sadj;
  1367. else
  1368. ret = trunc + 1;
  1369. dbg_avg(" -> %3d\n", ret);
  1370. return ret;
  1371. }
  1372. #define DEFAULT_SAT_RETRY_LIMIT 11 // 1 + 10 retries
  1373. #define default_lock_retry_limit 20 // 20 retries
  1374. #define deskew_validation_delay 10000 // 10 millisecs
  1375. static int perform_deskew_training(struct ddr_priv *priv, int rank_mask,
  1376. int if_num, int spd_rawcard_aorb)
  1377. {
  1378. int unsaturated, locked;
  1379. int sat_retries, sat_retries_limit;
  1380. int lock_retries, lock_retries_total, lock_retries_limit;
  1381. int print_first;
  1382. int print_them_all;
  1383. struct deskew_counts dsk_counts;
  1384. union cvmx_lmcx_phy_ctl phy_ctl;
  1385. char *s;
  1386. int has_no_sat = octeon_is_cpuid(OCTEON_CN78XX_PASS2_X) ||
  1387. octeon_is_cpuid(OCTEON_CNF75XX);
  1388. int disable_bitval_retries = 1; // default to disabled
  1389. debug("N0.LMC%d: Performing Deskew Training.\n", if_num);
  1390. sat_retries = 0;
  1391. sat_retries_limit = (has_no_sat) ? 5 : DEFAULT_SAT_RETRY_LIMIT;
  1392. lock_retries_total = 0;
  1393. unsaturated = 0;
  1394. print_first = 1; // print the first one
  1395. // set to true for printing all normal deskew attempts
  1396. print_them_all = 0;
  1397. // provide override for bitval_errs causing internal VREF retries
  1398. s = env_get("ddr_disable_bitval_retries");
  1399. if (s)
  1400. disable_bitval_retries = !!simple_strtoul(s, NULL, 0);
  1401. lock_retries_limit = default_lock_retry_limit;
  1402. if ((octeon_is_cpuid(OCTEON_CN78XX_PASS2_X)) ||
  1403. (octeon_is_cpuid(OCTEON_CN73XX)) ||
  1404. (octeon_is_cpuid(OCTEON_CNF75XX)))
  1405. lock_retries_limit *= 2; // give new chips twice as many
  1406. do { /* while (sat_retries < sat_retry_limit) */
  1407. /*
  1408. * 4.8.8 LMC Deskew Training
  1409. *
  1410. * LMC requires input-read-data deskew training.
  1411. *
  1412. * 1. Write LMC(0)_EXT_CONFIG[VREFINT_SEQ_DESKEW] = 1.
  1413. */
  1414. union cvmx_lmcx_ext_config ext_config;
  1415. ext_config.u64 = lmc_rd(priv, CVMX_LMCX_EXT_CONFIG(if_num));
  1416. ext_config.s.vrefint_seq_deskew = 1;
  1417. ddr_seq_print
  1418. ("Performing LMC sequence: vrefint_seq_deskew = %d\n",
  1419. ext_config.s.vrefint_seq_deskew);
  1420. lmc_wr(priv, CVMX_LMCX_EXT_CONFIG(if_num), ext_config.u64);
  1421. /*
  1422. * 2. Write LMC(0)_SEQ_CTL[SEQ_SEL] = 0x0A and
  1423. * LMC(0)_SEQ_CTL[INIT_START] = 1.
  1424. *
  1425. * 3. Wait for LMC(0)_SEQ_CTL[SEQ_COMPLETE] to be set to 1.
  1426. */
  1427. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  1428. phy_ctl.s.phy_dsk_reset = 1; /* RESET Deskew sequence */
  1429. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  1430. /* LMC Deskew Training */
  1431. oct3_ddr3_seq(priv, rank_mask, if_num, 0x0A);
  1432. lock_retries = 0;
  1433. perform_deskew_training:
  1434. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  1435. phy_ctl.s.phy_dsk_reset = 0; /* Normal Deskew sequence */
  1436. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  1437. /* LMC Deskew Training */
  1438. oct3_ddr3_seq(priv, rank_mask, if_num, 0x0A);
  1439. // Moved this from validate_deskew_training
  1440. /* Allow deskew results to stabilize before evaluating them. */
  1441. udelay(deskew_validation_delay);
  1442. // Now go look at lock and saturation status...
  1443. validate_deskew_training(priv, rank_mask, if_num, &dsk_counts,
  1444. print_first);
  1445. // after printing the first and not doing them all, no more
  1446. if (print_first && !print_them_all)
  1447. print_first = 0;
  1448. unsaturated = (dsk_counts.saturated == 0);
  1449. locked = (dsk_counts.unlocked == 0);
  1450. // only do locking retries if unsaturated or rawcard A or B,
  1451. // otherwise full SAT retry
  1452. if (unsaturated || (spd_rawcard_aorb && !has_no_sat)) {
  1453. if (!locked) { // and not locked
  1454. lock_retries++;
  1455. lock_retries_total++;
  1456. if (lock_retries <= lock_retries_limit) {
  1457. goto perform_deskew_training;
  1458. } else {
  1459. debug("N0.LMC%d: LOCK RETRIES failed after %d retries\n",
  1460. if_num, lock_retries_limit);
  1461. }
  1462. } else {
  1463. // only print if we did try
  1464. if (lock_retries_total > 0)
  1465. debug("N0.LMC%d: LOCK RETRIES successful after %d retries\n",
  1466. if_num, lock_retries);
  1467. }
  1468. } /* if (unsaturated || spd_rawcard_aorb) */
  1469. ++sat_retries;
  1470. /*
  1471. * At this point, check for a DDR4 RDIMM that will not
  1472. * benefit from SAT retries; if so, exit
  1473. */
  1474. if (spd_rawcard_aorb && !has_no_sat) {
  1475. debug("N0.LMC%d: Deskew Training Loop: Exiting for RAWCARD == A or B.\n",
  1476. if_num);
  1477. break; // no sat or lock retries
  1478. }
  1479. } while (!unsaturated && (sat_retries < sat_retries_limit));
  1480. debug("N0.LMC%d: Deskew Training %s. %d sat-retries, %d lock-retries\n",
  1481. if_num, (sat_retries >= DEFAULT_SAT_RETRY_LIMIT) ?
  1482. "Timed Out" : "Completed", sat_retries - 1, lock_retries_total);
  1483. // FIXME? add saturation to reasons for fault return - give it a
  1484. // chance via Internal VREF
  1485. // FIXME? add OPTIONAL bit value to reasons for fault return -
  1486. // give it a chance via Internal VREF
  1487. if (dsk_counts.nibrng_errs != 0 || dsk_counts.nibunl_errs != 0 ||
  1488. (dsk_counts.bitval_errs != 0 && !disable_bitval_retries) ||
  1489. !unsaturated) {
  1490. debug("N0.LMC%d: Nibble or Saturation Error(s) found, returning FAULT\n",
  1491. if_num);
  1492. // FIXME: do we want this output always for errors?
  1493. validate_deskew_training(priv, rank_mask, if_num,
  1494. &dsk_counts, 1);
  1495. return -1; // we did retry locally, they did not help
  1496. }
  1497. // NOTE: we (currently) always print one last training validation
  1498. // before starting Read Leveling...
  1499. return 0;
  1500. }
  1501. #define SCALING_FACTOR (1000)
  1502. // NOTE: this gets called for 1-rank and 2-rank DIMMs in single-slot config
  1503. static int compute_vref_1slot_2rank(int rtt_wr, int rtt_park, int dqx_ctl,
  1504. int rank_count, int dram_connection)
  1505. {
  1506. u64 reff_s;
  1507. u64 rser_s = (dram_connection) ? 0 : 15;
  1508. u64 vdd = 1200;
  1509. u64 vref;
  1510. // 99 == HiZ
  1511. u64 rtt_wr_s = (((rtt_wr == 0) || rtt_wr == 99) ?
  1512. 1 * 1024 * 1024 : rtt_wr);
  1513. u64 rtt_park_s = (((rtt_park == 0) || ((rank_count == 1) &&
  1514. (rtt_wr != 0))) ?
  1515. 1 * 1024 * 1024 : rtt_park);
  1516. u64 dqx_ctl_s = (dqx_ctl == 0 ? 1 * 1024 * 1024 : dqx_ctl);
  1517. int vref_value;
  1518. u64 rangepc = 6000; // range1 base
  1519. u64 vrefpc;
  1520. int vref_range = 0;
  1521. reff_s = divide_nint((rtt_wr_s * rtt_park_s), (rtt_wr_s + rtt_park_s));
  1522. vref = (((rser_s + dqx_ctl_s) * SCALING_FACTOR) /
  1523. (rser_s + dqx_ctl_s + reff_s)) + SCALING_FACTOR;
  1524. vref = (vref * vdd) / 2 / SCALING_FACTOR;
  1525. vrefpc = (vref * 100 * 100) / vdd;
  1526. if (vrefpc < rangepc) { // < range1 base, use range2
  1527. vref_range = 1 << 6; // set bit A6 for range2
  1528. rangepc = 4500; // range2 base is 45%
  1529. }
  1530. vref_value = divide_nint(vrefpc - rangepc, 65);
  1531. if (vref_value < 0)
  1532. vref_value = vref_range; // set to base of range
  1533. else
  1534. vref_value |= vref_range;
  1535. debug("rtt_wr: %d, rtt_park: %d, dqx_ctl: %d, rank_count: %d\n",
  1536. rtt_wr, rtt_park, dqx_ctl, rank_count);
  1537. debug("rtt_wr_s: %lld, rtt_park_s: %lld, dqx_ctl_s: %lld, vref_value: 0x%x, range: %d\n",
  1538. rtt_wr_s, rtt_park_s, dqx_ctl_s, vref_value ^ vref_range,
  1539. vref_range ? 2 : 1);
  1540. return vref_value;
  1541. }
  1542. // NOTE: this gets called for 1-rank and 2-rank DIMMs in two-slot configs
  1543. static int compute_vref_2slot_2rank(int rtt_wr, int rtt_park_00,
  1544. int rtt_park_01,
  1545. int dqx_ctl, int rtt_nom,
  1546. int dram_connection)
  1547. {
  1548. u64 rser = (dram_connection) ? 0 : 15;
  1549. u64 vdd = 1200;
  1550. u64 vl, vlp, vcm;
  1551. u64 rd0, rd1, rpullup;
  1552. // 99 == HiZ
  1553. u64 rtt_wr_s = (((rtt_wr == 0) || rtt_wr == 99) ?
  1554. 1 * 1024 * 1024 : rtt_wr);
  1555. u64 rtt_park_00_s = (rtt_park_00 == 0 ? 1 * 1024 * 1024 : rtt_park_00);
  1556. u64 rtt_park_01_s = (rtt_park_01 == 0 ? 1 * 1024 * 1024 : rtt_park_01);
  1557. u64 dqx_ctl_s = (dqx_ctl == 0 ? 1 * 1024 * 1024 : dqx_ctl);
  1558. u64 rtt_nom_s = (rtt_nom == 0 ? 1 * 1024 * 1024 : rtt_nom);
  1559. int vref_value;
  1560. u64 rangepc = 6000; // range1 base
  1561. u64 vrefpc;
  1562. int vref_range = 0;
  1563. // rd0 = (RTT_NOM (parallel) RTT_WR) + =
  1564. // ((RTT_NOM * RTT_WR) / (RTT_NOM + RTT_WR)) + RSER
  1565. rd0 = divide_nint((rtt_nom_s * rtt_wr_s),
  1566. (rtt_nom_s + rtt_wr_s)) + rser;
  1567. // rd1 = (RTT_PARK_00 (parallel) RTT_PARK_01) + RSER =
  1568. // ((RTT_PARK_00 * RTT_PARK_01) / (RTT_PARK_00 + RTT_PARK_01)) + RSER
  1569. rd1 = divide_nint((rtt_park_00_s * rtt_park_01_s),
  1570. (rtt_park_00_s + rtt_park_01_s)) + rser;
  1571. // rpullup = rd0 (parallel) rd1 = (rd0 * rd1) / (rd0 + rd1)
  1572. rpullup = divide_nint((rd0 * rd1), (rd0 + rd1));
  1573. // vl = (DQX_CTL / (DQX_CTL + rpullup)) * 1.2
  1574. vl = divide_nint((dqx_ctl_s * vdd), (dqx_ctl_s + rpullup));
  1575. // vlp = ((RSER / rd0) * (1.2 - vl)) + vl
  1576. vlp = divide_nint((rser * (vdd - vl)), rd0) + vl;
  1577. // vcm = (vlp + 1.2) / 2
  1578. vcm = divide_nint((vlp + vdd), 2);
  1579. // vrefpc = (vcm / 1.2) * 100
  1580. vrefpc = divide_nint((vcm * 100 * 100), vdd);
  1581. if (vrefpc < rangepc) { // < range1 base, use range2
  1582. vref_range = 1 << 6; // set bit A6 for range2
  1583. rangepc = 4500; // range2 base is 45%
  1584. }
  1585. vref_value = divide_nint(vrefpc - rangepc, 65);
  1586. if (vref_value < 0)
  1587. vref_value = vref_range; // set to base of range
  1588. else
  1589. vref_value |= vref_range;
  1590. debug("rtt_wr:%d, rtt_park_00:%d, rtt_park_01:%d, dqx_ctl:%d, rtt_nom:%d, vref_value:%d (0x%x)\n",
  1591. rtt_wr, rtt_park_00, rtt_park_01, dqx_ctl, rtt_nom, vref_value,
  1592. vref_value);
  1593. return vref_value;
  1594. }
  1595. // NOTE: only call this for DIMMs with 1 or 2 ranks, not 4.
  1596. static int compute_vref_val(struct ddr_priv *priv, int if_num, int rankx,
  1597. int dimm_count, int rank_count,
  1598. struct impedence_values *imp_values,
  1599. int is_stacked_die, int dram_connection)
  1600. {
  1601. int computed_final_vref_value = 0;
  1602. int enable_adjust = ENABLE_COMPUTED_VREF_ADJUSTMENT;
  1603. const char *s;
  1604. int rtt_wr, dqx_ctl, rtt_nom, index;
  1605. union cvmx_lmcx_modereg_params1 lmc_modereg_params1;
  1606. union cvmx_lmcx_modereg_params2 lmc_modereg_params2;
  1607. union cvmx_lmcx_comp_ctl2 comp_ctl2;
  1608. int rtt_park;
  1609. int rtt_park_00;
  1610. int rtt_park_01;
  1611. debug("N0.LMC%d.R%d: %s(...dram_connection = %d)\n",
  1612. if_num, rankx, __func__, dram_connection);
  1613. // allow some overrides...
  1614. s = env_get("ddr_adjust_computed_vref");
  1615. if (s) {
  1616. enable_adjust = !!simple_strtoul(s, NULL, 0);
  1617. if (!enable_adjust) {
  1618. debug("N0.LMC%d.R%d: DISABLE adjustment of computed VREF\n",
  1619. if_num, rankx);
  1620. }
  1621. }
  1622. s = env_get("ddr_set_computed_vref");
  1623. if (s) {
  1624. int new_vref = simple_strtoul(s, NULL, 0);
  1625. debug("N0.LMC%d.R%d: OVERRIDE computed VREF to 0x%x (%d)\n",
  1626. if_num, rankx, new_vref, new_vref);
  1627. return new_vref;
  1628. }
  1629. /*
  1630. * Calculate an alternative to the measured vref value
  1631. * but only for configurations we know how to...
  1632. */
  1633. // We have code for 2-rank DIMMs in both 1-slot or 2-slot configs,
  1634. // and can use the 2-rank 1-slot code for 1-rank DIMMs in 1-slot
  1635. // configs, and can use the 2-rank 2-slot code for 1-rank DIMMs
  1636. // in 2-slot configs.
  1637. lmc_modereg_params1.u64 =
  1638. lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS1(if_num));
  1639. lmc_modereg_params2.u64 =
  1640. lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS2(if_num));
  1641. comp_ctl2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  1642. dqx_ctl = imp_values->dqx_strength[comp_ctl2.s.dqx_ctl];
  1643. // WR always comes from the current rank
  1644. index = (lmc_modereg_params1.u64 >> (rankx * 12 + 5)) & 0x03;
  1645. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS1_X))
  1646. index |= lmc_modereg_params1.u64 >> (51 + rankx - 2) & 0x04;
  1647. rtt_wr = imp_values->rtt_wr_ohms[index];
  1648. // separate calculations for 1 vs 2 DIMMs per LMC
  1649. if (dimm_count == 1) {
  1650. // PARK comes from this rank if 1-rank, otherwise other rank
  1651. index =
  1652. (lmc_modereg_params2.u64 >>
  1653. ((rankx ^ (rank_count - 1)) * 10 + 0)) & 0x07;
  1654. rtt_park = imp_values->rtt_nom_ohms[index];
  1655. computed_final_vref_value =
  1656. compute_vref_1slot_2rank(rtt_wr, rtt_park, dqx_ctl,
  1657. rank_count, dram_connection);
  1658. } else {
  1659. // get both PARK values from the other DIMM
  1660. index =
  1661. (lmc_modereg_params2.u64 >> ((rankx ^ 0x02) * 10 + 0)) &
  1662. 0x07;
  1663. rtt_park_00 = imp_values->rtt_nom_ohms[index];
  1664. index =
  1665. (lmc_modereg_params2.u64 >> ((rankx ^ 0x03) * 10 + 0)) &
  1666. 0x07;
  1667. rtt_park_01 = imp_values->rtt_nom_ohms[index];
  1668. // NOM comes from this rank if 1-rank, otherwise other rank
  1669. index =
  1670. (lmc_modereg_params1.u64 >>
  1671. ((rankx ^ (rank_count - 1)) * 12 + 9)) & 0x07;
  1672. rtt_nom = imp_values->rtt_nom_ohms[index];
  1673. computed_final_vref_value =
  1674. compute_vref_2slot_2rank(rtt_wr, rtt_park_00, rtt_park_01,
  1675. dqx_ctl, rtt_nom, dram_connection);
  1676. }
  1677. if (enable_adjust) {
  1678. union cvmx_lmcx_config lmc_config;
  1679. union cvmx_lmcx_control lmc_control;
  1680. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  1681. lmc_control.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  1682. /*
  1683. * New computed vref = existing computed vref - X
  1684. *
  1685. * The value of X is depending on different conditions.
  1686. * Both #122 and #139 are 2Rx4 RDIMM, while #124 is stacked
  1687. * die 2Rx4, so I conclude the results into two conditions:
  1688. *
  1689. * 1. Stacked Die: 2Rx4
  1690. * 1-slot: offset = 7. i, e New computed vref = existing
  1691. * computed vref - 7
  1692. * 2-slot: offset = 6
  1693. *
  1694. * 2. Regular: 2Rx4
  1695. * 1-slot: offset = 3
  1696. * 2-slot: offset = 2
  1697. */
  1698. // we know we never get called unless DDR4, so test just
  1699. // the other conditions
  1700. if (lmc_control.s.rdimm_ena == 1 &&
  1701. rank_count == 2 && lmc_config.s.mode_x4dev) {
  1702. // it must first be RDIMM and 2-rank and x4
  1703. int adj;
  1704. // now do according to stacked die or not...
  1705. if (is_stacked_die)
  1706. adj = (dimm_count == 1) ? -7 : -6;
  1707. else
  1708. adj = (dimm_count == 1) ? -3 : -2;
  1709. // we must have adjusted it, so print it out if
  1710. // verbosity is right
  1711. debug("N0.LMC%d.R%d: adjusting computed vref from %2d (0x%02x) to %2d (0x%02x)\n",
  1712. if_num, rankx, computed_final_vref_value,
  1713. computed_final_vref_value,
  1714. computed_final_vref_value + adj,
  1715. computed_final_vref_value + adj);
  1716. computed_final_vref_value += adj;
  1717. }
  1718. }
  1719. return computed_final_vref_value;
  1720. }
  1721. static void unpack_rlevel_settings(int if_bytemask, int ecc_ena,
  1722. struct rlevel_byte_data *rlevel_byte,
  1723. union cvmx_lmcx_rlevel_rankx lmc_rlevel_rank)
  1724. {
  1725. if ((if_bytemask & 0xff) == 0xff) {
  1726. if (ecc_ena) {
  1727. rlevel_byte[8].delay = lmc_rlevel_rank.s.byte7;
  1728. rlevel_byte[7].delay = lmc_rlevel_rank.s.byte6;
  1729. rlevel_byte[6].delay = lmc_rlevel_rank.s.byte5;
  1730. rlevel_byte[5].delay = lmc_rlevel_rank.s.byte4;
  1731. /* ECC */
  1732. rlevel_byte[4].delay = lmc_rlevel_rank.s.byte8;
  1733. } else {
  1734. rlevel_byte[7].delay = lmc_rlevel_rank.s.byte7;
  1735. rlevel_byte[6].delay = lmc_rlevel_rank.s.byte6;
  1736. rlevel_byte[5].delay = lmc_rlevel_rank.s.byte5;
  1737. rlevel_byte[4].delay = lmc_rlevel_rank.s.byte4;
  1738. }
  1739. } else {
  1740. rlevel_byte[8].delay = lmc_rlevel_rank.s.byte8; /* unused */
  1741. rlevel_byte[7].delay = lmc_rlevel_rank.s.byte7; /* unused */
  1742. rlevel_byte[6].delay = lmc_rlevel_rank.s.byte6; /* unused */
  1743. rlevel_byte[5].delay = lmc_rlevel_rank.s.byte5; /* unused */
  1744. rlevel_byte[4].delay = lmc_rlevel_rank.s.byte4; /* ECC */
  1745. }
  1746. rlevel_byte[3].delay = lmc_rlevel_rank.s.byte3;
  1747. rlevel_byte[2].delay = lmc_rlevel_rank.s.byte2;
  1748. rlevel_byte[1].delay = lmc_rlevel_rank.s.byte1;
  1749. rlevel_byte[0].delay = lmc_rlevel_rank.s.byte0;
  1750. }
  1751. static void pack_rlevel_settings(int if_bytemask, int ecc_ena,
  1752. struct rlevel_byte_data *rlevel_byte,
  1753. union cvmx_lmcx_rlevel_rankx
  1754. *final_rlevel_rank)
  1755. {
  1756. union cvmx_lmcx_rlevel_rankx lmc_rlevel_rank = *final_rlevel_rank;
  1757. if ((if_bytemask & 0xff) == 0xff) {
  1758. if (ecc_ena) {
  1759. lmc_rlevel_rank.s.byte7 = rlevel_byte[8].delay;
  1760. lmc_rlevel_rank.s.byte6 = rlevel_byte[7].delay;
  1761. lmc_rlevel_rank.s.byte5 = rlevel_byte[6].delay;
  1762. lmc_rlevel_rank.s.byte4 = rlevel_byte[5].delay;
  1763. /* ECC */
  1764. lmc_rlevel_rank.s.byte8 = rlevel_byte[4].delay;
  1765. } else {
  1766. lmc_rlevel_rank.s.byte7 = rlevel_byte[7].delay;
  1767. lmc_rlevel_rank.s.byte6 = rlevel_byte[6].delay;
  1768. lmc_rlevel_rank.s.byte5 = rlevel_byte[5].delay;
  1769. lmc_rlevel_rank.s.byte4 = rlevel_byte[4].delay;
  1770. }
  1771. } else {
  1772. lmc_rlevel_rank.s.byte8 = rlevel_byte[8].delay;
  1773. lmc_rlevel_rank.s.byte7 = rlevel_byte[7].delay;
  1774. lmc_rlevel_rank.s.byte6 = rlevel_byte[6].delay;
  1775. lmc_rlevel_rank.s.byte5 = rlevel_byte[5].delay;
  1776. lmc_rlevel_rank.s.byte4 = rlevel_byte[4].delay;
  1777. }
  1778. lmc_rlevel_rank.s.byte3 = rlevel_byte[3].delay;
  1779. lmc_rlevel_rank.s.byte2 = rlevel_byte[2].delay;
  1780. lmc_rlevel_rank.s.byte1 = rlevel_byte[1].delay;
  1781. lmc_rlevel_rank.s.byte0 = rlevel_byte[0].delay;
  1782. *final_rlevel_rank = lmc_rlevel_rank;
  1783. }
  1784. /////////////////// These are the RLEVEL settings display routines
  1785. // flags
  1786. #define WITH_NOTHING 0
  1787. #define WITH_SCORE 1
  1788. #define WITH_AVERAGE 2
  1789. #define WITH_FINAL 4
  1790. #define WITH_COMPUTE 8
  1791. static void do_display_rl(int if_num,
  1792. union cvmx_lmcx_rlevel_rankx lmc_rlevel_rank,
  1793. int rank, int flags, int score)
  1794. {
  1795. char score_buf[16];
  1796. char *msg_buf;
  1797. char hex_buf[20];
  1798. if (flags & WITH_SCORE) {
  1799. snprintf(score_buf, sizeof(score_buf), "(%d)", score);
  1800. } else {
  1801. score_buf[0] = ' ';
  1802. score_buf[1] = 0;
  1803. }
  1804. if (flags & WITH_AVERAGE) {
  1805. msg_buf = " DELAY AVERAGES ";
  1806. } else if (flags & WITH_FINAL) {
  1807. msg_buf = " FINAL SETTINGS ";
  1808. } else if (flags & WITH_COMPUTE) {
  1809. msg_buf = " COMPUTED DELAYS ";
  1810. } else {
  1811. snprintf(hex_buf, sizeof(hex_buf), "0x%016llX",
  1812. (unsigned long long)lmc_rlevel_rank.u64);
  1813. msg_buf = hex_buf;
  1814. }
  1815. debug("N0.LMC%d.R%d: Rlevel Rank %#4x, %s : %5d %5d %5d %5d %5d %5d %5d %5d %5d %s\n",
  1816. if_num, rank, lmc_rlevel_rank.s.status, msg_buf,
  1817. lmc_rlevel_rank.s.byte8, lmc_rlevel_rank.s.byte7,
  1818. lmc_rlevel_rank.s.byte6, lmc_rlevel_rank.s.byte5,
  1819. lmc_rlevel_rank.s.byte4, lmc_rlevel_rank.s.byte3,
  1820. lmc_rlevel_rank.s.byte2, lmc_rlevel_rank.s.byte1,
  1821. lmc_rlevel_rank.s.byte0, score_buf);
  1822. }
  1823. static void display_rl(int if_num,
  1824. union cvmx_lmcx_rlevel_rankx lmc_rlevel_rank, int rank)
  1825. {
  1826. do_display_rl(if_num, lmc_rlevel_rank, rank, 0, 0);
  1827. }
  1828. static void display_rl_with_score(int if_num,
  1829. union cvmx_lmcx_rlevel_rankx lmc_rlevel_rank,
  1830. int rank, int score)
  1831. {
  1832. do_display_rl(if_num, lmc_rlevel_rank, rank, 1, score);
  1833. }
  1834. static void display_rl_with_final(int if_num,
  1835. union cvmx_lmcx_rlevel_rankx lmc_rlevel_rank,
  1836. int rank)
  1837. {
  1838. do_display_rl(if_num, lmc_rlevel_rank, rank, 4, 0);
  1839. }
  1840. static void display_rl_with_computed(int if_num,
  1841. union cvmx_lmcx_rlevel_rankx
  1842. lmc_rlevel_rank, int rank, int score)
  1843. {
  1844. do_display_rl(if_num, lmc_rlevel_rank, rank, 9, score);
  1845. }
  1846. // flag values
  1847. #define WITH_RODT_BLANK 0
  1848. #define WITH_RODT_SKIPPING 1
  1849. #define WITH_RODT_BESTROW 2
  1850. #define WITH_RODT_BESTSCORE 3
  1851. // control
  1852. #define SKIP_SKIPPING 1
  1853. static const char *with_rodt_canned_msgs[4] = {
  1854. " ", "SKIPPING ", "BEST ROW ", "BEST SCORE"
  1855. };
  1856. static void display_rl_with_rodt(int if_num,
  1857. union cvmx_lmcx_rlevel_rankx lmc_rlevel_rank,
  1858. int rank, int score,
  1859. int nom_ohms, int rodt_ohms, int flag)
  1860. {
  1861. const char *msg_buf;
  1862. char set_buf[20];
  1863. #if SKIP_SKIPPING
  1864. if (flag == WITH_RODT_SKIPPING)
  1865. return;
  1866. #endif
  1867. msg_buf = with_rodt_canned_msgs[flag];
  1868. if (nom_ohms < 0) {
  1869. snprintf(set_buf, sizeof(set_buf), " RODT %3d ",
  1870. rodt_ohms);
  1871. } else {
  1872. snprintf(set_buf, sizeof(set_buf), "NOM %3d RODT %3d", nom_ohms,
  1873. rodt_ohms);
  1874. }
  1875. debug("N0.LMC%d.R%d: Rlevel %s %s : %5d %5d %5d %5d %5d %5d %5d %5d %5d (%d)\n",
  1876. if_num, rank, set_buf, msg_buf, lmc_rlevel_rank.s.byte8,
  1877. lmc_rlevel_rank.s.byte7, lmc_rlevel_rank.s.byte6,
  1878. lmc_rlevel_rank.s.byte5, lmc_rlevel_rank.s.byte4,
  1879. lmc_rlevel_rank.s.byte3, lmc_rlevel_rank.s.byte2,
  1880. lmc_rlevel_rank.s.byte1, lmc_rlevel_rank.s.byte0, score);
  1881. }
  1882. static void do_display_wl(int if_num,
  1883. union cvmx_lmcx_wlevel_rankx lmc_wlevel_rank,
  1884. int rank, int flags)
  1885. {
  1886. char *msg_buf;
  1887. char hex_buf[20];
  1888. if (flags & WITH_FINAL) {
  1889. msg_buf = " FINAL SETTINGS ";
  1890. } else {
  1891. snprintf(hex_buf, sizeof(hex_buf), "0x%016llX",
  1892. (unsigned long long)lmc_wlevel_rank.u64);
  1893. msg_buf = hex_buf;
  1894. }
  1895. debug("N0.LMC%d.R%d: Wlevel Rank %#4x, %s : %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
  1896. if_num, rank, lmc_wlevel_rank.s.status, msg_buf,
  1897. lmc_wlevel_rank.s.byte8, lmc_wlevel_rank.s.byte7,
  1898. lmc_wlevel_rank.s.byte6, lmc_wlevel_rank.s.byte5,
  1899. lmc_wlevel_rank.s.byte4, lmc_wlevel_rank.s.byte3,
  1900. lmc_wlevel_rank.s.byte2, lmc_wlevel_rank.s.byte1,
  1901. lmc_wlevel_rank.s.byte0);
  1902. }
  1903. static void display_wl(int if_num,
  1904. union cvmx_lmcx_wlevel_rankx lmc_wlevel_rank, int rank)
  1905. {
  1906. do_display_wl(if_num, lmc_wlevel_rank, rank, WITH_NOTHING);
  1907. }
  1908. static void display_wl_with_final(int if_num,
  1909. union cvmx_lmcx_wlevel_rankx lmc_wlevel_rank,
  1910. int rank)
  1911. {
  1912. do_display_wl(if_num, lmc_wlevel_rank, rank, WITH_FINAL);
  1913. }
  1914. // pretty-print bitmask adjuster
  1915. static u64 ppbm(u64 bm)
  1916. {
  1917. if (bm != 0ul) {
  1918. while ((bm & 0x0fful) == 0ul)
  1919. bm >>= 4;
  1920. }
  1921. return bm;
  1922. }
  1923. // xlate PACKED index to UNPACKED index to use with rlevel_byte
  1924. #define XPU(i, e) (((i) < 4) ? (i) : (((i) < 8) ? (i) + (e) : 4))
  1925. // xlate UNPACKED index to PACKED index to use with rlevel_bitmask
  1926. #define XUP(i, e) (((i) < 4) ? (i) : (e) ? (((i) > 4) ? (i) - 1 : 8) : (i))
  1927. // flag values
  1928. #define WITH_WL_BITMASKS 0
  1929. #define WITH_RL_BITMASKS 1
  1930. #define WITH_RL_MASK_SCORES 2
  1931. #define WITH_RL_SEQ_SCORES 3
  1932. static void do_display_bm(int if_num, int rank, void *bm,
  1933. int flags, int ecc)
  1934. {
  1935. if (flags == WITH_WL_BITMASKS) {
  1936. // wlevel_bitmask array in PACKED index order, so just
  1937. // print them
  1938. int *bitmasks = (int *)bm;
  1939. debug("N0.LMC%d.R%d: Wlevel Debug Bitmasks : %05x %05x %05x %05x %05x %05x %05x %05x %05x\n",
  1940. if_num, rank, bitmasks[8], bitmasks[7], bitmasks[6],
  1941. bitmasks[5], bitmasks[4], bitmasks[3], bitmasks[2],
  1942. bitmasks[1], bitmasks[0]
  1943. );
  1944. } else if (flags == WITH_RL_BITMASKS) {
  1945. // rlevel_bitmask array in PACKED index order, so just
  1946. // print them
  1947. struct rlevel_bitmask *rlevel_bitmask =
  1948. (struct rlevel_bitmask *)bm;
  1949. debug("N0.LMC%d.R%d: Rlevel Debug Bitmasks 8:0 : %05llx %05llx %05llx %05llx %05llx %05llx %05llx %05llx %05llx\n",
  1950. if_num, rank, ppbm(rlevel_bitmask[8].bm),
  1951. ppbm(rlevel_bitmask[7].bm), ppbm(rlevel_bitmask[6].bm),
  1952. ppbm(rlevel_bitmask[5].bm), ppbm(rlevel_bitmask[4].bm),
  1953. ppbm(rlevel_bitmask[3].bm), ppbm(rlevel_bitmask[2].bm),
  1954. ppbm(rlevel_bitmask[1].bm), ppbm(rlevel_bitmask[0].bm)
  1955. );
  1956. } else if (flags == WITH_RL_MASK_SCORES) {
  1957. // rlevel_bitmask array in PACKED index order, so just
  1958. // print them
  1959. struct rlevel_bitmask *rlevel_bitmask =
  1960. (struct rlevel_bitmask *)bm;
  1961. debug("N0.LMC%d.R%d: Rlevel Debug Bitmask Scores 8:0 : %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
  1962. if_num, rank, rlevel_bitmask[8].errs,
  1963. rlevel_bitmask[7].errs, rlevel_bitmask[6].errs,
  1964. rlevel_bitmask[5].errs, rlevel_bitmask[4].errs,
  1965. rlevel_bitmask[3].errs, rlevel_bitmask[2].errs,
  1966. rlevel_bitmask[1].errs, rlevel_bitmask[0].errs);
  1967. } else if (flags == WITH_RL_SEQ_SCORES) {
  1968. // rlevel_byte array in UNPACKED index order, so xlate
  1969. // and print them
  1970. struct rlevel_byte_data *rlevel_byte =
  1971. (struct rlevel_byte_data *)bm;
  1972. debug("N0.LMC%d.R%d: Rlevel Debug Non-seq Scores 8:0 : %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
  1973. if_num, rank, rlevel_byte[XPU(8, ecc)].sqerrs,
  1974. rlevel_byte[XPU(7, ecc)].sqerrs,
  1975. rlevel_byte[XPU(6, ecc)].sqerrs,
  1976. rlevel_byte[XPU(5, ecc)].sqerrs,
  1977. rlevel_byte[XPU(4, ecc)].sqerrs,
  1978. rlevel_byte[XPU(3, ecc)].sqerrs,
  1979. rlevel_byte[XPU(2, ecc)].sqerrs,
  1980. rlevel_byte[XPU(1, ecc)].sqerrs,
  1981. rlevel_byte[XPU(0, ecc)].sqerrs);
  1982. }
  1983. }
  1984. static void display_wl_bm(int if_num, int rank, int *bitmasks)
  1985. {
  1986. do_display_bm(if_num, rank, (void *)bitmasks, WITH_WL_BITMASKS, 0);
  1987. }
  1988. static void display_rl_bm(int if_num, int rank,
  1989. struct rlevel_bitmask *bitmasks, int ecc_ena)
  1990. {
  1991. do_display_bm(if_num, rank, (void *)bitmasks, WITH_RL_BITMASKS,
  1992. ecc_ena);
  1993. }
  1994. static void display_rl_bm_scores(int if_num, int rank,
  1995. struct rlevel_bitmask *bitmasks, int ecc_ena)
  1996. {
  1997. do_display_bm(if_num, rank, (void *)bitmasks, WITH_RL_MASK_SCORES,
  1998. ecc_ena);
  1999. }
  2000. static void display_rl_seq_scores(int if_num, int rank,
  2001. struct rlevel_byte_data *bytes, int ecc_ena)
  2002. {
  2003. do_display_bm(if_num, rank, (void *)bytes, WITH_RL_SEQ_SCORES, ecc_ena);
  2004. }
  2005. #define RODT_OHMS_COUNT 8
  2006. #define RTT_NOM_OHMS_COUNT 8
  2007. #define RTT_NOM_TABLE_COUNT 8
  2008. #define RTT_WR_OHMS_COUNT 8
  2009. #define DIC_OHMS_COUNT 3
  2010. #define DRIVE_STRENGTH_COUNT 15
  2011. static unsigned char ddr4_rodt_ohms[RODT_OHMS_COUNT] = {
  2012. 0, 40, 60, 80, 120, 240, 34, 48 };
  2013. static unsigned char ddr4_rtt_nom_ohms[RTT_NOM_OHMS_COUNT] = {
  2014. 0, 60, 120, 40, 240, 48, 80, 34 };
  2015. static unsigned char ddr4_rtt_nom_table[RTT_NOM_TABLE_COUNT] = {
  2016. 0, 4, 2, 6, 1, 5, 3, 7 };
  2017. // setting HiZ ohms to 99 for computed vref
  2018. static unsigned char ddr4_rtt_wr_ohms[RTT_WR_OHMS_COUNT] = {
  2019. 0, 120, 240, 99, 80 };
  2020. static unsigned char ddr4_dic_ohms[DIC_OHMS_COUNT] = { 34, 48 };
  2021. static short ddr4_drive_strength[DRIVE_STRENGTH_COUNT] = {
  2022. 0, 0, 26, 30, 34, 40, 48, 68, 0, 0, 0, 0, 0, 0, 0 };
  2023. static short ddr4_dqx_strength[DRIVE_STRENGTH_COUNT] = {
  2024. 0, 24, 27, 30, 34, 40, 48, 60, 0, 0, 0, 0, 0, 0, 0 };
  2025. struct impedence_values ddr4_impedence_val = {
  2026. .rodt_ohms = ddr4_rodt_ohms,
  2027. .rtt_nom_ohms = ddr4_rtt_nom_ohms,
  2028. .rtt_nom_table = ddr4_rtt_nom_table,
  2029. .rtt_wr_ohms = ddr4_rtt_wr_ohms,
  2030. .dic_ohms = ddr4_dic_ohms,
  2031. .drive_strength = ddr4_drive_strength,
  2032. .dqx_strength = ddr4_dqx_strength,
  2033. };
  2034. static unsigned char ddr3_rodt_ohms[RODT_OHMS_COUNT] = {
  2035. 0, 20, 30, 40, 60, 120, 0, 0 };
  2036. static unsigned char ddr3_rtt_nom_ohms[RTT_NOM_OHMS_COUNT] = {
  2037. 0, 60, 120, 40, 20, 30, 0, 0 };
  2038. static unsigned char ddr3_rtt_nom_table[RTT_NOM_TABLE_COUNT] = {
  2039. 0, 2, 1, 3, 5, 4, 0, 0 };
  2040. static unsigned char ddr3_rtt_wr_ohms[RTT_WR_OHMS_COUNT] = { 0, 60, 120 };
  2041. static unsigned char ddr3_dic_ohms[DIC_OHMS_COUNT] = { 40, 34 };
  2042. static short ddr3_drive_strength[DRIVE_STRENGTH_COUNT] = {
  2043. 0, 24, 27, 30, 34, 40, 48, 60, 0, 0, 0, 0, 0, 0, 0 };
  2044. static struct impedence_values ddr3_impedence_val = {
  2045. .rodt_ohms = ddr3_rodt_ohms,
  2046. .rtt_nom_ohms = ddr3_rtt_nom_ohms,
  2047. .rtt_nom_table = ddr3_rtt_nom_table,
  2048. .rtt_wr_ohms = ddr3_rtt_wr_ohms,
  2049. .dic_ohms = ddr3_dic_ohms,
  2050. .drive_strength = ddr3_drive_strength,
  2051. .dqx_strength = ddr3_drive_strength,
  2052. };
  2053. static u64 hertz_to_psecs(u64 hertz)
  2054. {
  2055. /* Clock in psecs */
  2056. return divide_nint((u64)1000 * 1000 * 1000 * 1000, hertz);
  2057. }
  2058. #define DIVIDEND_SCALE 1000 /* Scale to avoid rounding error. */
  2059. static u64 psecs_to_mts(u64 psecs)
  2060. {
  2061. return divide_nint(divide_nint((u64)(2 * 1000000 * DIVIDEND_SCALE),
  2062. psecs), DIVIDEND_SCALE);
  2063. }
  2064. #define WITHIN(v, b, m) (((v) >= ((b) - (m))) && ((v) <= ((b) + (m))))
  2065. static unsigned long pretty_psecs_to_mts(u64 psecs)
  2066. {
  2067. u64 ret = 0; // default to error
  2068. if (WITHIN(psecs, 2500, 1))
  2069. ret = 800;
  2070. else if (WITHIN(psecs, 1875, 1))
  2071. ret = 1066;
  2072. else if (WITHIN(psecs, 1500, 1))
  2073. ret = 1333;
  2074. else if (WITHIN(psecs, 1250, 1))
  2075. ret = 1600;
  2076. else if (WITHIN(psecs, 1071, 1))
  2077. ret = 1866;
  2078. else if (WITHIN(psecs, 937, 1))
  2079. ret = 2133;
  2080. else if (WITHIN(psecs, 833, 1))
  2081. ret = 2400;
  2082. else if (WITHIN(psecs, 750, 1))
  2083. ret = 2666;
  2084. return ret;
  2085. }
  2086. static u64 mts_to_hertz(u64 mts)
  2087. {
  2088. return ((mts * 1000 * 1000) / 2);
  2089. }
  2090. static int compute_rc3x(int64_t tclk_psecs)
  2091. {
  2092. long speed;
  2093. long tclk_psecs_min, tclk_psecs_max;
  2094. long data_rate_mhz, data_rate_mhz_min, data_rate_mhz_max;
  2095. int rc3x;
  2096. #define ENCODING_BASE 1240
  2097. data_rate_mhz = psecs_to_mts(tclk_psecs);
  2098. /*
  2099. * 2400 MT/s is a special case. Using integer arithmetic it rounds
  2100. * from 833 psecs to 2401 MT/s. Force it to 2400 to pick the
  2101. * proper setting from the table.
  2102. */
  2103. if (tclk_psecs == 833)
  2104. data_rate_mhz = 2400;
  2105. for (speed = ENCODING_BASE; speed < 3200; speed += 20) {
  2106. int error = 0;
  2107. /* Clock in psecs */
  2108. tclk_psecs_min = hertz_to_psecs(mts_to_hertz(speed + 00));
  2109. /* Clock in psecs */
  2110. tclk_psecs_max = hertz_to_psecs(mts_to_hertz(speed + 18));
  2111. data_rate_mhz_min = psecs_to_mts(tclk_psecs_min);
  2112. data_rate_mhz_max = psecs_to_mts(tclk_psecs_max);
  2113. /* Force alingment to multiple to avound rounding errors. */
  2114. data_rate_mhz_min = ((data_rate_mhz_min + 18) / 20) * 20;
  2115. data_rate_mhz_max = ((data_rate_mhz_max + 18) / 20) * 20;
  2116. error += (speed + 00 != data_rate_mhz_min);
  2117. error += (speed + 20 != data_rate_mhz_max);
  2118. rc3x = (speed - ENCODING_BASE) / 20;
  2119. if (data_rate_mhz <= (speed + 20))
  2120. break;
  2121. }
  2122. return rc3x;
  2123. }
  2124. /*
  2125. * static global variables needed, so that functions (loops) can be
  2126. * restructured from the main huge function. Its not elegant, but the
  2127. * only way to break the original functions like init_octeon3_ddr3_interface()
  2128. * into separate logical smaller functions with less indentation levels.
  2129. */
  2130. static int if_num __section(".data");
  2131. static u32 if_mask __section(".data");
  2132. static int ddr_hertz __section(".data");
  2133. static struct ddr_conf *ddr_conf __section(".data");
  2134. static const struct dimm_odt_config *odt_1rank_config __section(".data");
  2135. static const struct dimm_odt_config *odt_2rank_config __section(".data");
  2136. static const struct dimm_odt_config *odt_4rank_config __section(".data");
  2137. static struct dimm_config *dimm_config_table __section(".data");
  2138. static const struct dimm_odt_config *odt_config __section(".data");
  2139. static const struct ddr3_custom_config *c_cfg __section(".data");
  2140. static int odt_idx __section(".data");
  2141. static ulong tclk_psecs __section(".data");
  2142. static ulong eclk_psecs __section(".data");
  2143. static int row_bits __section(".data");
  2144. static int col_bits __section(".data");
  2145. static int num_banks __section(".data");
  2146. static int num_ranks __section(".data");
  2147. static int dram_width __section(".data");
  2148. static int dimm_count __section(".data");
  2149. /* Accumulate and report all the errors before giving up */
  2150. static int fatal_error __section(".data");
  2151. /* Flag that indicates safe DDR settings should be used */
  2152. static int safe_ddr_flag __section(".data");
  2153. /* Octeon II Default: 64bit interface width */
  2154. static int if_64b __section(".data");
  2155. static int if_bytemask __section(".data");
  2156. static u32 mem_size_mbytes __section(".data");
  2157. static unsigned int didx __section(".data");
  2158. static int bank_bits __section(".data");
  2159. static int bunk_enable __section(".data");
  2160. static int rank_mask __section(".data");
  2161. static int column_bits_start __section(".data");
  2162. static int row_lsb __section(".data");
  2163. static int pbank_lsb __section(".data");
  2164. static int use_ecc __section(".data");
  2165. static int mtb_psec __section(".data");
  2166. static short ftb_dividend __section(".data");
  2167. static short ftb_divisor __section(".data");
  2168. static int taamin __section(".data");
  2169. static int tckmin __section(".data");
  2170. static int cl __section(".data");
  2171. static int min_cas_latency __section(".data");
  2172. static int max_cas_latency __section(".data");
  2173. static int override_cas_latency __section(".data");
  2174. static int ddr_rtt_nom_auto __section(".data");
  2175. static int ddr_rodt_ctl_auto __section(".data");
  2176. static int spd_addr __section(".data");
  2177. static int spd_org __section(".data");
  2178. static int spd_banks __section(".data");
  2179. static int spd_rdimm __section(".data");
  2180. static int spd_dimm_type __section(".data");
  2181. static int spd_ecc __section(".data");
  2182. static u32 spd_cas_latency __section(".data");
  2183. static int spd_mtb_dividend __section(".data");
  2184. static int spd_mtb_divisor __section(".data");
  2185. static int spd_tck_min __section(".data");
  2186. static int spd_taa_min __section(".data");
  2187. static int spd_twr __section(".data");
  2188. static int spd_trcd __section(".data");
  2189. static int spd_trrd __section(".data");
  2190. static int spd_trp __section(".data");
  2191. static int spd_tras __section(".data");
  2192. static int spd_trc __section(".data");
  2193. static int spd_trfc __section(".data");
  2194. static int spd_twtr __section(".data");
  2195. static int spd_trtp __section(".data");
  2196. static int spd_tfaw __section(".data");
  2197. static int spd_addr_mirror __section(".data");
  2198. static int spd_package __section(".data");
  2199. static int spd_rawcard __section(".data");
  2200. static int spd_rawcard_aorb __section(".data");
  2201. static int spd_rdimm_registers __section(".data");
  2202. static int spd_thermal_sensor __section(".data");
  2203. static int is_stacked_die __section(".data");
  2204. static int is_3ds_dimm __section(".data");
  2205. // 3DS: logical ranks per package rank
  2206. static int lranks_per_prank __section(".data");
  2207. // 3DS: logical ranks bits
  2208. static int lranks_bits __section(".data");
  2209. // in Mbits; only used for 3DS
  2210. static int die_capacity __section(".data");
  2211. static enum ddr_type ddr_type __section(".data");
  2212. static int twr __section(".data");
  2213. static int trcd __section(".data");
  2214. static int trrd __section(".data");
  2215. static int trp __section(".data");
  2216. static int tras __section(".data");
  2217. static int trc __section(".data");
  2218. static int trfc __section(".data");
  2219. static int twtr __section(".data");
  2220. static int trtp __section(".data");
  2221. static int tfaw __section(".data");
  2222. static int ddr4_tckavgmin __section(".data");
  2223. static int ddr4_tckavgmax __section(".data");
  2224. static int ddr4_trdcmin __section(".data");
  2225. static int ddr4_trpmin __section(".data");
  2226. static int ddr4_trasmin __section(".data");
  2227. static int ddr4_trcmin __section(".data");
  2228. static int ddr4_trfc1min __section(".data");
  2229. static int ddr4_trfc2min __section(".data");
  2230. static int ddr4_trfc4min __section(".data");
  2231. static int ddr4_tfawmin __section(".data");
  2232. static int ddr4_trrd_smin __section(".data");
  2233. static int ddr4_trrd_lmin __section(".data");
  2234. static int ddr4_tccd_lmin __section(".data");
  2235. static int wl_mask_err __section(".data");
  2236. static int wl_loops __section(".data");
  2237. static int default_rtt_nom[4] __section(".data");
  2238. static int dyn_rtt_nom_mask __section(".data");
  2239. static struct impedence_values *imp_val __section(".data");
  2240. static char default_rodt_ctl __section(".data");
  2241. // default to disabled (ie, try LMC restart, not chip reset)
  2242. static int ddr_disable_chip_reset __section(".data");
  2243. static const char *dimm_type_name __section(".data");
  2244. static int match_wl_rtt_nom __section(".data");
  2245. struct hwl_alt_by_rank {
  2246. u16 hwl_alt_mask; // mask of bytelanes with alternate
  2247. u16 hwl_alt_delay[9]; // bytelane alternate avail if mask=1
  2248. };
  2249. static struct hwl_alt_by_rank hwl_alts[4] __section(".data");
  2250. #define DEFAULT_INTERNAL_VREF_TRAINING_LIMIT 3 // was: 5
  2251. static int internal_retries __section(".data");
  2252. static int deskew_training_errors __section(".data");
  2253. static struct deskew_counts deskew_training_results __section(".data");
  2254. static int disable_deskew_training __section(".data");
  2255. static int restart_if_dsk_incomplete __section(".data");
  2256. static int dac_eval_retries __section(".data");
  2257. static int dac_settings[9] __section(".data");
  2258. static int num_samples __section(".data");
  2259. static int sample __section(".data");
  2260. static int lane __section(".data");
  2261. static int last_lane __section(".data");
  2262. static int total_dac_eval_retries __section(".data");
  2263. static int dac_eval_exhausted __section(".data");
  2264. #define DEFAULT_DAC_SAMPLES 7 // originally was 5
  2265. #define DAC_RETRIES_LIMIT 2
  2266. struct bytelane_sample {
  2267. s16 bytes[DEFAULT_DAC_SAMPLES];
  2268. };
  2269. static struct bytelane_sample lanes[9] __section(".data");
  2270. static char disable_sequential_delay_check __section(".data");
  2271. static int wl_print __section(".data");
  2272. static int enable_by_rank_init __section(".data");
  2273. static int saved_rank_mask __section(".data");
  2274. static int by_rank __section(".data");
  2275. static struct deskew_data rank_dsk[4] __section(".data");
  2276. static struct dac_data rank_dac[4] __section(".data");
  2277. // todo: perhaps remove node at some time completely?
  2278. static int node __section(".data");
  2279. static int base_cl __section(".data");
  2280. /* Parameters from DDR3 Specifications */
  2281. #define DDR3_TREFI 7800000 /* 7.8 us */
  2282. #define DDR3_ZQCS 80000ull /* 80 ns */
  2283. #define DDR3_ZQCS_INTERNAL 1280000000ull /* 128ms/100 */
  2284. #define DDR3_TCKE 5000 /* 5 ns */
  2285. #define DDR3_TMRD 4 /* 4 nCK */
  2286. #define DDR3_TDLLK 512 /* 512 nCK */
  2287. #define DDR3_TMPRR 1 /* 1 nCK */
  2288. #define DDR3_TWLMRD 40 /* 40 nCK */
  2289. #define DDR3_TWLDQSEN 25 /* 25 nCK */
  2290. /* Parameters from DDR4 Specifications */
  2291. #define DDR4_TMRD 8 /* 8 nCK */
  2292. #define DDR4_TDLLK 768 /* 768 nCK */
  2293. static void lmc_config(struct ddr_priv *priv)
  2294. {
  2295. union cvmx_lmcx_config cfg;
  2296. char *s;
  2297. cfg.u64 = 0;
  2298. cfg.cn78xx.ecc_ena = use_ecc;
  2299. cfg.cn78xx.row_lsb = encode_row_lsb_ddr3(row_lsb);
  2300. cfg.cn78xx.pbank_lsb = encode_pbank_lsb_ddr3(pbank_lsb);
  2301. cfg.cn78xx.idlepower = 0; /* Disabled */
  2302. s = lookup_env(priv, "ddr_idlepower");
  2303. if (s)
  2304. cfg.cn78xx.idlepower = simple_strtoul(s, NULL, 0);
  2305. cfg.cn78xx.forcewrite = 0; /* Disabled */
  2306. /* Include memory reference address in the ECC */
  2307. cfg.cn78xx.ecc_adr = 1;
  2308. s = lookup_env(priv, "ddr_ecc_adr");
  2309. if (s)
  2310. cfg.cn78xx.ecc_adr = simple_strtoul(s, NULL, 0);
  2311. cfg.cn78xx.reset = 0;
  2312. /*
  2313. * Program LMC0_CONFIG[24:18], ref_zqcs_int(6:0) to
  2314. * RND-DN(tREFI/clkPeriod/512) Program LMC0_CONFIG[36:25],
  2315. * ref_zqcs_int(18:7) to
  2316. * RND-DN(ZQCS_Interval/clkPeriod/(512*128)). Note that this
  2317. * value should always be greater than 32, to account for
  2318. * resistor calibration delays.
  2319. */
  2320. cfg.cn78xx.ref_zqcs_int = ((DDR3_TREFI / tclk_psecs / 512) & 0x7f);
  2321. cfg.cn78xx.ref_zqcs_int |=
  2322. ((max(33ull, (DDR3_ZQCS_INTERNAL / (tclk_psecs / 100) /
  2323. (512 * 128))) & 0xfff) << 7);
  2324. cfg.cn78xx.early_dqx = 1; /* Default to enabled */
  2325. s = lookup_env(priv, "ddr_early_dqx");
  2326. if (!s)
  2327. s = lookup_env(priv, "ddr%d_early_dqx", if_num);
  2328. if (s)
  2329. cfg.cn78xx.early_dqx = simple_strtoul(s, NULL, 0);
  2330. cfg.cn78xx.sref_with_dll = 0;
  2331. cfg.cn78xx.rank_ena = bunk_enable;
  2332. cfg.cn78xx.rankmask = rank_mask; /* Set later */
  2333. cfg.cn78xx.mirrmask = (spd_addr_mirror << 1 | spd_addr_mirror << 3) &
  2334. rank_mask;
  2335. /* Set once and don't change it. */
  2336. cfg.cn78xx.init_status = rank_mask;
  2337. cfg.cn78xx.early_unload_d0_r0 = 0;
  2338. cfg.cn78xx.early_unload_d0_r1 = 0;
  2339. cfg.cn78xx.early_unload_d1_r0 = 0;
  2340. cfg.cn78xx.early_unload_d1_r1 = 0;
  2341. cfg.cn78xx.scrz = 0;
  2342. if (octeon_is_cpuid(OCTEON_CN70XX))
  2343. cfg.cn78xx.mode32b = 1; /* Read-only. Always 1. */
  2344. cfg.cn78xx.mode_x4dev = (dram_width == 4) ? 1 : 0;
  2345. cfg.cn78xx.bg2_enable = ((ddr_type == DDR4_DRAM) &&
  2346. (dram_width == 16)) ? 0 : 1;
  2347. s = lookup_env_ull(priv, "ddr_config");
  2348. if (s)
  2349. cfg.u64 = simple_strtoull(s, NULL, 0);
  2350. debug("LMC_CONFIG : 0x%016llx\n",
  2351. cfg.u64);
  2352. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), cfg.u64);
  2353. }
  2354. static void lmc_control(struct ddr_priv *priv)
  2355. {
  2356. union cvmx_lmcx_control ctrl;
  2357. char *s;
  2358. ctrl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  2359. ctrl.s.rdimm_ena = spd_rdimm;
  2360. ctrl.s.bwcnt = 0; /* Clear counter later */
  2361. if (spd_rdimm)
  2362. ctrl.s.ddr2t = (safe_ddr_flag ? 1 : c_cfg->ddr2t_rdimm);
  2363. else
  2364. ctrl.s.ddr2t = (safe_ddr_flag ? 1 : c_cfg->ddr2t_udimm);
  2365. ctrl.s.pocas = 0;
  2366. ctrl.s.fprch2 = (safe_ddr_flag ? 2 : c_cfg->fprch2);
  2367. ctrl.s.throttle_rd = safe_ddr_flag ? 1 : 0;
  2368. ctrl.s.throttle_wr = safe_ddr_flag ? 1 : 0;
  2369. ctrl.s.inorder_rd = safe_ddr_flag ? 1 : 0;
  2370. ctrl.s.inorder_wr = safe_ddr_flag ? 1 : 0;
  2371. ctrl.s.elev_prio_dis = safe_ddr_flag ? 1 : 0;
  2372. /* discards writes to addresses that don't exist in the DRAM */
  2373. ctrl.s.nxm_write_en = 0;
  2374. ctrl.s.max_write_batch = 8;
  2375. ctrl.s.xor_bank = 1;
  2376. ctrl.s.auto_dclkdis = 1;
  2377. ctrl.s.int_zqcs_dis = 0;
  2378. ctrl.s.ext_zqcs_dis = 0;
  2379. ctrl.s.bprch = 1;
  2380. ctrl.s.wodt_bprch = 1;
  2381. ctrl.s.rodt_bprch = 1;
  2382. s = lookup_env(priv, "ddr_xor_bank");
  2383. if (s)
  2384. ctrl.s.xor_bank = simple_strtoul(s, NULL, 0);
  2385. s = lookup_env(priv, "ddr_2t");
  2386. if (s)
  2387. ctrl.s.ddr2t = simple_strtoul(s, NULL, 0);
  2388. s = lookup_env(priv, "ddr_fprch2");
  2389. if (s)
  2390. ctrl.s.fprch2 = simple_strtoul(s, NULL, 0);
  2391. s = lookup_env(priv, "ddr_bprch");
  2392. if (s)
  2393. ctrl.s.bprch = simple_strtoul(s, NULL, 0);
  2394. s = lookup_env(priv, "ddr_wodt_bprch");
  2395. if (s)
  2396. ctrl.s.wodt_bprch = simple_strtoul(s, NULL, 0);
  2397. s = lookup_env(priv, "ddr_rodt_bprch");
  2398. if (s)
  2399. ctrl.s.rodt_bprch = simple_strtoul(s, NULL, 0);
  2400. s = lookup_env(priv, "ddr_int_zqcs_dis");
  2401. if (s)
  2402. ctrl.s.int_zqcs_dis = simple_strtoul(s, NULL, 0);
  2403. s = lookup_env(priv, "ddr_ext_zqcs_dis");
  2404. if (s)
  2405. ctrl.s.ext_zqcs_dis = simple_strtoul(s, NULL, 0);
  2406. s = lookup_env_ull(priv, "ddr_control");
  2407. if (s)
  2408. ctrl.u64 = simple_strtoull(s, NULL, 0);
  2409. debug("LMC_CONTROL : 0x%016llx\n",
  2410. ctrl.u64);
  2411. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctrl.u64);
  2412. }
  2413. static void lmc_timing_params0(struct ddr_priv *priv)
  2414. {
  2415. union cvmx_lmcx_timing_params0 tp0;
  2416. unsigned int trp_value;
  2417. char *s;
  2418. tp0.u64 = lmc_rd(priv, CVMX_LMCX_TIMING_PARAMS0(if_num));
  2419. trp_value = divide_roundup(trp, tclk_psecs) - 1;
  2420. debug("TIMING_PARAMS0[TRP]: NEW 0x%x, OLD 0x%x\n", trp_value,
  2421. trp_value +
  2422. (unsigned int)(divide_roundup(max(4ull * tclk_psecs, 7500ull),
  2423. tclk_psecs)) - 4);
  2424. s = lookup_env_ull(priv, "ddr_use_old_trp");
  2425. if (s) {
  2426. if (!!simple_strtoull(s, NULL, 0)) {
  2427. trp_value +=
  2428. divide_roundup(max(4ull * tclk_psecs, 7500ull),
  2429. tclk_psecs) - 4;
  2430. debug("TIMING_PARAMS0[trp]: USING OLD 0x%x\n",
  2431. trp_value);
  2432. }
  2433. }
  2434. tp0.cn78xx.txpr =
  2435. divide_roundup(max(5ull * tclk_psecs, trfc + 10000ull),
  2436. 16 * tclk_psecs);
  2437. tp0.cn78xx.trp = trp_value & 0x1f;
  2438. tp0.cn78xx.tcksre =
  2439. divide_roundup(max(5ull * tclk_psecs, 10000ull), tclk_psecs) - 1;
  2440. if (ddr_type == DDR4_DRAM) {
  2441. int tzqinit = 4; // Default to 4, for all DDR4 speed bins
  2442. s = lookup_env(priv, "ddr_tzqinit");
  2443. if (s)
  2444. tzqinit = simple_strtoul(s, NULL, 0);
  2445. tp0.cn78xx.tzqinit = tzqinit;
  2446. /* Always 8. */
  2447. tp0.cn78xx.tzqcs = divide_roundup(128 * tclk_psecs,
  2448. (16 * tclk_psecs));
  2449. tp0.cn78xx.tcke =
  2450. divide_roundup(max(3 * tclk_psecs, (ulong)DDR3_TCKE),
  2451. tclk_psecs) - 1;
  2452. tp0.cn78xx.tmrd =
  2453. divide_roundup((DDR4_TMRD * tclk_psecs), tclk_psecs) - 1;
  2454. tp0.cn78xx.tmod = 25; /* 25 is the max allowed */
  2455. tp0.cn78xx.tdllk = divide_roundup(DDR4_TDLLK, 256);
  2456. } else {
  2457. tp0.cn78xx.tzqinit =
  2458. divide_roundup(max(512ull * tclk_psecs, 640000ull),
  2459. (256 * tclk_psecs));
  2460. tp0.cn78xx.tzqcs =
  2461. divide_roundup(max(64ull * tclk_psecs, DDR3_ZQCS),
  2462. (16 * tclk_psecs));
  2463. tp0.cn78xx.tcke = divide_roundup(DDR3_TCKE, tclk_psecs) - 1;
  2464. tp0.cn78xx.tmrd =
  2465. divide_roundup((DDR3_TMRD * tclk_psecs), tclk_psecs) - 1;
  2466. tp0.cn78xx.tmod =
  2467. divide_roundup(max(12ull * tclk_psecs, 15000ull),
  2468. tclk_psecs) - 1;
  2469. tp0.cn78xx.tdllk = divide_roundup(DDR3_TDLLK, 256);
  2470. }
  2471. s = lookup_env_ull(priv, "ddr_timing_params0");
  2472. if (s)
  2473. tp0.u64 = simple_strtoull(s, NULL, 0);
  2474. debug("TIMING_PARAMS0 : 0x%016llx\n",
  2475. tp0.u64);
  2476. lmc_wr(priv, CVMX_LMCX_TIMING_PARAMS0(if_num), tp0.u64);
  2477. }
  2478. static void lmc_timing_params1(struct ddr_priv *priv)
  2479. {
  2480. union cvmx_lmcx_timing_params1 tp1;
  2481. unsigned int txp, temp_trcd, trfc_dlr;
  2482. char *s;
  2483. tp1.u64 = lmc_rd(priv, CVMX_LMCX_TIMING_PARAMS1(if_num));
  2484. /* .cn70xx. */
  2485. tp1.s.tmprr = divide_roundup(DDR3_TMPRR * tclk_psecs, tclk_psecs) - 1;
  2486. tp1.cn78xx.tras = divide_roundup(tras, tclk_psecs) - 1;
  2487. temp_trcd = divide_roundup(trcd, tclk_psecs);
  2488. if (temp_trcd > 15) {
  2489. debug("TIMING_PARAMS1[trcd]: need extension bit for 0x%x\n",
  2490. temp_trcd);
  2491. }
  2492. if (octeon_is_cpuid(OCTEON_CN78XX_PASS1_X) && temp_trcd > 15) {
  2493. /*
  2494. * Let .trcd=0 serve as a flag that the field has
  2495. * overflowed. Must use Additive Latency mode as a
  2496. * workaround.
  2497. */
  2498. temp_trcd = 0;
  2499. }
  2500. tp1.cn78xx.trcd = (temp_trcd >> 0) & 0xf;
  2501. tp1.cn78xx.trcd_ext = (temp_trcd >> 4) & 0x1;
  2502. tp1.cn78xx.twtr = divide_roundup(twtr, tclk_psecs) - 1;
  2503. tp1.cn78xx.trfc = divide_roundup(trfc, 8 * tclk_psecs);
  2504. if (ddr_type == DDR4_DRAM) {
  2505. /* Workaround bug 24006. Use Trrd_l. */
  2506. tp1.cn78xx.trrd =
  2507. divide_roundup(ddr4_trrd_lmin, tclk_psecs) - 2;
  2508. } else {
  2509. tp1.cn78xx.trrd = divide_roundup(trrd, tclk_psecs) - 2;
  2510. }
  2511. /*
  2512. * tXP = max( 3nCK, 7.5 ns) DDR3-800 tCLK = 2500 psec
  2513. * tXP = max( 3nCK, 7.5 ns) DDR3-1066 tCLK = 1875 psec
  2514. * tXP = max( 3nCK, 6.0 ns) DDR3-1333 tCLK = 1500 psec
  2515. * tXP = max( 3nCK, 6.0 ns) DDR3-1600 tCLK = 1250 psec
  2516. * tXP = max( 3nCK, 6.0 ns) DDR3-1866 tCLK = 1071 psec
  2517. * tXP = max( 3nCK, 6.0 ns) DDR3-2133 tCLK = 937 psec
  2518. */
  2519. txp = (tclk_psecs < 1875) ? 6000 : 7500;
  2520. txp = divide_roundup(max((unsigned int)(3 * tclk_psecs), txp),
  2521. tclk_psecs) - 1;
  2522. if (txp > 7) {
  2523. debug("TIMING_PARAMS1[txp]: need extension bit for 0x%x\n",
  2524. txp);
  2525. }
  2526. if (octeon_is_cpuid(OCTEON_CN78XX_PASS1_X) && txp > 7)
  2527. txp = 7; // max it out
  2528. tp1.cn78xx.txp = (txp >> 0) & 7;
  2529. tp1.cn78xx.txp_ext = (txp >> 3) & 1;
  2530. tp1.cn78xx.twlmrd = divide_roundup(DDR3_TWLMRD * tclk_psecs,
  2531. 4 * tclk_psecs);
  2532. tp1.cn78xx.twldqsen = divide_roundup(DDR3_TWLDQSEN * tclk_psecs,
  2533. 4 * tclk_psecs);
  2534. tp1.cn78xx.tfaw = divide_roundup(tfaw, 4 * tclk_psecs);
  2535. tp1.cn78xx.txpdll = divide_roundup(max(10ull * tclk_psecs, 24000ull),
  2536. tclk_psecs) - 1;
  2537. if (ddr_type == DDR4_DRAM && is_3ds_dimm) {
  2538. /*
  2539. * 4 Gb: tRFC_DLR = 90 ns
  2540. * 8 Gb: tRFC_DLR = 120 ns
  2541. * 16 Gb: tRFC_DLR = 190 ns FIXME?
  2542. */
  2543. if (die_capacity == 0x1000) // 4 Gbit
  2544. trfc_dlr = 90;
  2545. else if (die_capacity == 0x2000) // 8 Gbit
  2546. trfc_dlr = 120;
  2547. else if (die_capacity == 0x4000) // 16 Gbit
  2548. trfc_dlr = 190;
  2549. else
  2550. trfc_dlr = 0;
  2551. if (trfc_dlr == 0) {
  2552. debug("N%d.LMC%d: ERROR: tRFC_DLR: die_capacity %u Mbit is illegal\n",
  2553. node, if_num, die_capacity);
  2554. } else {
  2555. tp1.cn78xx.trfc_dlr =
  2556. divide_roundup(trfc_dlr * 1000UL, 8 * tclk_psecs);
  2557. debug("N%d.LMC%d: TIMING_PARAMS1[trfc_dlr] set to %u\n",
  2558. node, if_num, tp1.cn78xx.trfc_dlr);
  2559. }
  2560. }
  2561. s = lookup_env_ull(priv, "ddr_timing_params1");
  2562. if (s)
  2563. tp1.u64 = simple_strtoull(s, NULL, 0);
  2564. debug("TIMING_PARAMS1 : 0x%016llx\n",
  2565. tp1.u64);
  2566. lmc_wr(priv, CVMX_LMCX_TIMING_PARAMS1(if_num), tp1.u64);
  2567. }
  2568. static void lmc_timing_params2(struct ddr_priv *priv)
  2569. {
  2570. if (ddr_type == DDR4_DRAM) {
  2571. union cvmx_lmcx_timing_params1 tp1;
  2572. union cvmx_lmcx_timing_params2 tp2;
  2573. int temp_trrd_l;
  2574. tp1.u64 = lmc_rd(priv, CVMX_LMCX_TIMING_PARAMS1(if_num));
  2575. tp2.u64 = lmc_rd(priv, CVMX_LMCX_TIMING_PARAMS2(if_num));
  2576. debug("TIMING_PARAMS2 : 0x%016llx\n",
  2577. tp2.u64);
  2578. temp_trrd_l = divide_roundup(ddr4_trrd_lmin, tclk_psecs) - 2;
  2579. if (temp_trrd_l > 7)
  2580. debug("TIMING_PARAMS2[trrd_l]: need extension bit for 0x%x\n",
  2581. temp_trrd_l);
  2582. if (octeon_is_cpuid(OCTEON_CN78XX_PASS1_X) && temp_trrd_l > 7)
  2583. temp_trrd_l = 7; // max it out
  2584. tp2.cn78xx.trrd_l = (temp_trrd_l >> 0) & 7;
  2585. tp2.cn78xx.trrd_l_ext = (temp_trrd_l >> 3) & 1;
  2586. // correct for 1600-2400
  2587. tp2.s.twtr_l = divide_nint(max(4ull * tclk_psecs, 7500ull),
  2588. tclk_psecs) - 1;
  2589. tp2.s.t_rw_op_max = 7;
  2590. tp2.s.trtp = divide_roundup(max(4ull * tclk_psecs, 7500ull),
  2591. tclk_psecs) - 1;
  2592. debug("TIMING_PARAMS2 : 0x%016llx\n",
  2593. tp2.u64);
  2594. lmc_wr(priv, CVMX_LMCX_TIMING_PARAMS2(if_num), tp2.u64);
  2595. /*
  2596. * Workaround Errata 25823 - LMC: Possible DDR4 tWTR_L not met
  2597. * for Write-to-Read operations to the same Bank Group
  2598. */
  2599. if (tp1.cn78xx.twtr < (tp2.s.twtr_l - 4)) {
  2600. tp1.cn78xx.twtr = tp2.s.twtr_l - 4;
  2601. debug("ERRATA 25823: NEW: TWTR: %d, TWTR_L: %d\n",
  2602. tp1.cn78xx.twtr, tp2.s.twtr_l);
  2603. debug("TIMING_PARAMS1 : 0x%016llx\n",
  2604. tp1.u64);
  2605. lmc_wr(priv, CVMX_LMCX_TIMING_PARAMS1(if_num), tp1.u64);
  2606. }
  2607. }
  2608. }
  2609. static void lmc_modereg_params0(struct ddr_priv *priv)
  2610. {
  2611. union cvmx_lmcx_modereg_params0 mp0;
  2612. int param;
  2613. char *s;
  2614. mp0.u64 = lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num));
  2615. if (ddr_type == DDR4_DRAM) {
  2616. mp0.s.cwl = 0; /* 1600 (1250ps) */
  2617. if (tclk_psecs < 1250)
  2618. mp0.s.cwl = 1; /* 1866 (1072ps) */
  2619. if (tclk_psecs < 1072)
  2620. mp0.s.cwl = 2; /* 2133 (938ps) */
  2621. if (tclk_psecs < 938)
  2622. mp0.s.cwl = 3; /* 2400 (833ps) */
  2623. if (tclk_psecs < 833)
  2624. mp0.s.cwl = 4; /* 2666 (750ps) */
  2625. if (tclk_psecs < 750)
  2626. mp0.s.cwl = 5; /* 3200 (625ps) */
  2627. } else {
  2628. /*
  2629. ** CSR CWL CAS write Latency
  2630. ** === === =================================
  2631. ** 0 5 ( tCK(avg) >= 2.5 ns)
  2632. ** 1 6 (2.5 ns > tCK(avg) >= 1.875 ns)
  2633. ** 2 7 (1.875 ns > tCK(avg) >= 1.5 ns)
  2634. ** 3 8 (1.5 ns > tCK(avg) >= 1.25 ns)
  2635. ** 4 9 (1.25 ns > tCK(avg) >= 1.07 ns)
  2636. ** 5 10 (1.07 ns > tCK(avg) >= 0.935 ns)
  2637. ** 6 11 (0.935 ns > tCK(avg) >= 0.833 ns)
  2638. ** 7 12 (0.833 ns > tCK(avg) >= 0.75 ns)
  2639. */
  2640. mp0.s.cwl = 0;
  2641. if (tclk_psecs < 2500)
  2642. mp0.s.cwl = 1;
  2643. if (tclk_psecs < 1875)
  2644. mp0.s.cwl = 2;
  2645. if (tclk_psecs < 1500)
  2646. mp0.s.cwl = 3;
  2647. if (tclk_psecs < 1250)
  2648. mp0.s.cwl = 4;
  2649. if (tclk_psecs < 1070)
  2650. mp0.s.cwl = 5;
  2651. if (tclk_psecs < 935)
  2652. mp0.s.cwl = 6;
  2653. if (tclk_psecs < 833)
  2654. mp0.s.cwl = 7;
  2655. }
  2656. s = lookup_env(priv, "ddr_cwl");
  2657. if (s)
  2658. mp0.s.cwl = simple_strtoul(s, NULL, 0) - 5;
  2659. if (ddr_type == DDR4_DRAM) {
  2660. debug("%-45s : %d, [0x%x]\n", "CAS Write Latency CWL, [CSR]",
  2661. mp0.s.cwl + 9
  2662. + ((mp0.s.cwl > 2) ? (mp0.s.cwl - 3) * 2 : 0), mp0.s.cwl);
  2663. } else {
  2664. debug("%-45s : %d, [0x%x]\n", "CAS Write Latency CWL, [CSR]",
  2665. mp0.s.cwl + 5, mp0.s.cwl);
  2666. }
  2667. mp0.s.mprloc = 0;
  2668. mp0.s.mpr = 0;
  2669. mp0.s.dll = (ddr_type == DDR4_DRAM); /* 0 for DDR3 and 1 for DDR4 */
  2670. mp0.s.al = 0;
  2671. mp0.s.wlev = 0; /* Read Only */
  2672. if (octeon_is_cpuid(OCTEON_CN70XX) || ddr_type == DDR4_DRAM)
  2673. mp0.s.tdqs = 0;
  2674. else
  2675. mp0.s.tdqs = 1;
  2676. mp0.s.qoff = 0;
  2677. s = lookup_env(priv, "ddr_cl");
  2678. if (s) {
  2679. cl = simple_strtoul(s, NULL, 0);
  2680. debug("CAS Latency : %6d\n",
  2681. cl);
  2682. }
  2683. if (ddr_type == DDR4_DRAM) {
  2684. mp0.s.cl = 0x0;
  2685. if (cl > 9)
  2686. mp0.s.cl = 0x1;
  2687. if (cl > 10)
  2688. mp0.s.cl = 0x2;
  2689. if (cl > 11)
  2690. mp0.s.cl = 0x3;
  2691. if (cl > 12)
  2692. mp0.s.cl = 0x4;
  2693. if (cl > 13)
  2694. mp0.s.cl = 0x5;
  2695. if (cl > 14)
  2696. mp0.s.cl = 0x6;
  2697. if (cl > 15)
  2698. mp0.s.cl = 0x7;
  2699. if (cl > 16)
  2700. mp0.s.cl = 0x8;
  2701. if (cl > 18)
  2702. mp0.s.cl = 0x9;
  2703. if (cl > 20)
  2704. mp0.s.cl = 0xA;
  2705. if (cl > 24)
  2706. mp0.s.cl = 0xB;
  2707. } else {
  2708. mp0.s.cl = 0x2;
  2709. if (cl > 5)
  2710. mp0.s.cl = 0x4;
  2711. if (cl > 6)
  2712. mp0.s.cl = 0x6;
  2713. if (cl > 7)
  2714. mp0.s.cl = 0x8;
  2715. if (cl > 8)
  2716. mp0.s.cl = 0xA;
  2717. if (cl > 9)
  2718. mp0.s.cl = 0xC;
  2719. if (cl > 10)
  2720. mp0.s.cl = 0xE;
  2721. if (cl > 11)
  2722. mp0.s.cl = 0x1;
  2723. if (cl > 12)
  2724. mp0.s.cl = 0x3;
  2725. if (cl > 13)
  2726. mp0.s.cl = 0x5;
  2727. if (cl > 14)
  2728. mp0.s.cl = 0x7;
  2729. if (cl > 15)
  2730. mp0.s.cl = 0x9;
  2731. }
  2732. mp0.s.rbt = 0; /* Read Only. */
  2733. mp0.s.tm = 0;
  2734. mp0.s.dllr = 0;
  2735. param = divide_roundup(twr, tclk_psecs);
  2736. if (ddr_type == DDR4_DRAM) { /* DDR4 */
  2737. mp0.s.wrp = 1;
  2738. if (param > 12)
  2739. mp0.s.wrp = 2;
  2740. if (param > 14)
  2741. mp0.s.wrp = 3;
  2742. if (param > 16)
  2743. mp0.s.wrp = 4;
  2744. if (param > 18)
  2745. mp0.s.wrp = 5;
  2746. if (param > 20)
  2747. mp0.s.wrp = 6;
  2748. if (param > 24) /* RESERVED in DDR4 spec */
  2749. mp0.s.wrp = 7;
  2750. } else { /* DDR3 */
  2751. mp0.s.wrp = 1;
  2752. if (param > 5)
  2753. mp0.s.wrp = 2;
  2754. if (param > 6)
  2755. mp0.s.wrp = 3;
  2756. if (param > 7)
  2757. mp0.s.wrp = 4;
  2758. if (param > 8)
  2759. mp0.s.wrp = 5;
  2760. if (param > 10)
  2761. mp0.s.wrp = 6;
  2762. if (param > 12)
  2763. mp0.s.wrp = 7;
  2764. }
  2765. mp0.s.ppd = 0;
  2766. s = lookup_env(priv, "ddr_wrp");
  2767. if (s)
  2768. mp0.s.wrp = simple_strtoul(s, NULL, 0);
  2769. debug("%-45s : %d, [0x%x]\n",
  2770. "Write recovery for auto precharge WRP, [CSR]", param, mp0.s.wrp);
  2771. s = lookup_env_ull(priv, "ddr_modereg_params0");
  2772. if (s)
  2773. mp0.u64 = simple_strtoull(s, NULL, 0);
  2774. debug("MODEREG_PARAMS0 : 0x%016llx\n",
  2775. mp0.u64);
  2776. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num), mp0.u64);
  2777. }
  2778. static void lmc_modereg_params1(struct ddr_priv *priv)
  2779. {
  2780. union cvmx_lmcx_modereg_params1 mp1;
  2781. char *s;
  2782. int i;
  2783. mp1.u64 = odt_config[odt_idx].modereg_params1.u64;
  2784. /*
  2785. * Special request: mismatched DIMM support. Slot 0: 2-Rank,
  2786. * Slot 1: 1-Rank
  2787. */
  2788. if (rank_mask == 0x7) { /* 2-Rank, 1-Rank */
  2789. mp1.s.rtt_nom_00 = 0;
  2790. mp1.s.rtt_nom_01 = 3; /* rttnom_40ohm */
  2791. mp1.s.rtt_nom_10 = 3; /* rttnom_40ohm */
  2792. mp1.s.rtt_nom_11 = 0;
  2793. dyn_rtt_nom_mask = 0x6;
  2794. }
  2795. s = lookup_env(priv, "ddr_rtt_nom_mask");
  2796. if (s)
  2797. dyn_rtt_nom_mask = simple_strtoul(s, NULL, 0);
  2798. /*
  2799. * Save the original rtt_nom settings before sweeping through
  2800. * settings.
  2801. */
  2802. default_rtt_nom[0] = mp1.s.rtt_nom_00;
  2803. default_rtt_nom[1] = mp1.s.rtt_nom_01;
  2804. default_rtt_nom[2] = mp1.s.rtt_nom_10;
  2805. default_rtt_nom[3] = mp1.s.rtt_nom_11;
  2806. ddr_rtt_nom_auto = c_cfg->ddr_rtt_nom_auto;
  2807. for (i = 0; i < 4; ++i) {
  2808. u64 value;
  2809. s = lookup_env(priv, "ddr_rtt_nom_%1d%1d", !!(i & 2),
  2810. !!(i & 1));
  2811. if (!s)
  2812. s = lookup_env(priv, "ddr%d_rtt_nom_%1d%1d", if_num,
  2813. !!(i & 2), !!(i & 1));
  2814. if (s) {
  2815. value = simple_strtoul(s, NULL, 0);
  2816. mp1.u64 &= ~((u64)0x7 << (i * 12 + 9));
  2817. mp1.u64 |= ((value & 0x7) << (i * 12 + 9));
  2818. default_rtt_nom[i] = value;
  2819. ddr_rtt_nom_auto = 0;
  2820. }
  2821. }
  2822. s = lookup_env(priv, "ddr_rtt_nom");
  2823. if (!s)
  2824. s = lookup_env(priv, "ddr%d_rtt_nom", if_num);
  2825. if (s) {
  2826. u64 value;
  2827. value = simple_strtoul(s, NULL, 0);
  2828. if (dyn_rtt_nom_mask & 1) {
  2829. default_rtt_nom[0] = value;
  2830. mp1.s.rtt_nom_00 = value;
  2831. }
  2832. if (dyn_rtt_nom_mask & 2) {
  2833. default_rtt_nom[1] = value;
  2834. mp1.s.rtt_nom_01 = value;
  2835. }
  2836. if (dyn_rtt_nom_mask & 4) {
  2837. default_rtt_nom[2] = value;
  2838. mp1.s.rtt_nom_10 = value;
  2839. }
  2840. if (dyn_rtt_nom_mask & 8) {
  2841. default_rtt_nom[3] = value;
  2842. mp1.s.rtt_nom_11 = value;
  2843. }
  2844. ddr_rtt_nom_auto = 0;
  2845. }
  2846. for (i = 0; i < 4; ++i) {
  2847. u64 value;
  2848. s = lookup_env(priv, "ddr_rtt_wr_%1d%1d", !!(i & 2), !!(i & 1));
  2849. if (!s)
  2850. s = lookup_env(priv, "ddr%d_rtt_wr_%1d%1d", if_num,
  2851. !!(i & 2), !!(i & 1));
  2852. if (s) {
  2853. value = simple_strtoul(s, NULL, 0);
  2854. insrt_wr(&mp1.u64, i, value);
  2855. }
  2856. }
  2857. // Make sure 78XX pass 1 has valid RTT_WR settings, because
  2858. // configuration files may be set-up for later chips, and
  2859. // 78XX pass 1 supports no RTT_WR extension bits
  2860. if (octeon_is_cpuid(OCTEON_CN78XX_PASS1_X)) {
  2861. for (i = 0; i < 4; ++i) {
  2862. // if 80 or undefined
  2863. if (extr_wr(mp1.u64, i) > 3) {
  2864. // FIXME? always insert 120
  2865. insrt_wr(&mp1.u64, i, 1);
  2866. debug("RTT_WR_%d%d set to 120 for CN78XX pass 1\n",
  2867. !!(i & 2), i & 1);
  2868. }
  2869. }
  2870. }
  2871. s = lookup_env(priv, "ddr_dic");
  2872. if (s) {
  2873. u64 value = simple_strtoul(s, NULL, 0);
  2874. for (i = 0; i < 4; ++i) {
  2875. mp1.u64 &= ~((u64)0x3 << (i * 12 + 7));
  2876. mp1.u64 |= ((value & 0x3) << (i * 12 + 7));
  2877. }
  2878. }
  2879. for (i = 0; i < 4; ++i) {
  2880. u64 value;
  2881. s = lookup_env(priv, "ddr_dic_%1d%1d", !!(i & 2), !!(i & 1));
  2882. if (s) {
  2883. value = simple_strtoul(s, NULL, 0);
  2884. mp1.u64 &= ~((u64)0x3 << (i * 12 + 7));
  2885. mp1.u64 |= ((value & 0x3) << (i * 12 + 7));
  2886. }
  2887. }
  2888. s = lookup_env_ull(priv, "ddr_modereg_params1");
  2889. if (s)
  2890. mp1.u64 = simple_strtoull(s, NULL, 0);
  2891. debug("RTT_NOM %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  2892. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_11],
  2893. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_10],
  2894. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_01],
  2895. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_00],
  2896. mp1.s.rtt_nom_11,
  2897. mp1.s.rtt_nom_10, mp1.s.rtt_nom_01, mp1.s.rtt_nom_00);
  2898. debug("RTT_WR %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  2899. imp_val->rtt_wr_ohms[extr_wr(mp1.u64, 3)],
  2900. imp_val->rtt_wr_ohms[extr_wr(mp1.u64, 2)],
  2901. imp_val->rtt_wr_ohms[extr_wr(mp1.u64, 1)],
  2902. imp_val->rtt_wr_ohms[extr_wr(mp1.u64, 0)],
  2903. extr_wr(mp1.u64, 3),
  2904. extr_wr(mp1.u64, 2), extr_wr(mp1.u64, 1), extr_wr(mp1.u64, 0));
  2905. debug("DIC %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  2906. imp_val->dic_ohms[mp1.s.dic_11],
  2907. imp_val->dic_ohms[mp1.s.dic_10],
  2908. imp_val->dic_ohms[mp1.s.dic_01],
  2909. imp_val->dic_ohms[mp1.s.dic_00],
  2910. mp1.s.dic_11, mp1.s.dic_10, mp1.s.dic_01, mp1.s.dic_00);
  2911. debug("MODEREG_PARAMS1 : 0x%016llx\n",
  2912. mp1.u64);
  2913. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS1(if_num), mp1.u64);
  2914. }
  2915. static void lmc_modereg_params2(struct ddr_priv *priv)
  2916. {
  2917. char *s;
  2918. int i;
  2919. if (ddr_type == DDR4_DRAM) {
  2920. union cvmx_lmcx_modereg_params2 mp2;
  2921. mp2.u64 = odt_config[odt_idx].modereg_params2.u64;
  2922. s = lookup_env(priv, "ddr_rtt_park");
  2923. if (s) {
  2924. u64 value = simple_strtoul(s, NULL, 0);
  2925. for (i = 0; i < 4; ++i) {
  2926. mp2.u64 &= ~((u64)0x7 << (i * 10 + 0));
  2927. mp2.u64 |= ((value & 0x7) << (i * 10 + 0));
  2928. }
  2929. }
  2930. for (i = 0; i < 4; ++i) {
  2931. u64 value;
  2932. s = lookup_env(priv, "ddr_rtt_park_%1d%1d", !!(i & 2),
  2933. !!(i & 1));
  2934. if (s) {
  2935. value = simple_strtoul(s, NULL, 0);
  2936. mp2.u64 &= ~((u64)0x7 << (i * 10 + 0));
  2937. mp2.u64 |= ((value & 0x7) << (i * 10 + 0));
  2938. }
  2939. }
  2940. s = lookup_env_ull(priv, "ddr_modereg_params2");
  2941. if (s)
  2942. mp2.u64 = simple_strtoull(s, NULL, 0);
  2943. debug("RTT_PARK %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  2944. imp_val->rtt_nom_ohms[mp2.s.rtt_park_11],
  2945. imp_val->rtt_nom_ohms[mp2.s.rtt_park_10],
  2946. imp_val->rtt_nom_ohms[mp2.s.rtt_park_01],
  2947. imp_val->rtt_nom_ohms[mp2.s.rtt_park_00],
  2948. mp2.s.rtt_park_11, mp2.s.rtt_park_10, mp2.s.rtt_park_01,
  2949. mp2.s.rtt_park_00);
  2950. debug("%-45s : 0x%x,0x%x,0x%x,0x%x\n", "VREF_RANGE",
  2951. mp2.s.vref_range_11,
  2952. mp2.s.vref_range_10,
  2953. mp2.s.vref_range_01, mp2.s.vref_range_00);
  2954. debug("%-45s : 0x%x,0x%x,0x%x,0x%x\n", "VREF_VALUE",
  2955. mp2.s.vref_value_11,
  2956. mp2.s.vref_value_10,
  2957. mp2.s.vref_value_01, mp2.s.vref_value_00);
  2958. debug("MODEREG_PARAMS2 : 0x%016llx\n",
  2959. mp2.u64);
  2960. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS2(if_num), mp2.u64);
  2961. }
  2962. }
  2963. static void lmc_modereg_params3(struct ddr_priv *priv)
  2964. {
  2965. char *s;
  2966. if (ddr_type == DDR4_DRAM) {
  2967. union cvmx_lmcx_modereg_params3 mp3;
  2968. mp3.u64 = lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS3(if_num));
  2969. /* Disable as workaround to Errata 20547 */
  2970. mp3.s.rd_dbi = 0;
  2971. mp3.s.tccd_l = max(divide_roundup(ddr4_tccd_lmin, tclk_psecs),
  2972. 5ull) - 4;
  2973. s = lookup_env(priv, "ddr_rd_preamble");
  2974. if (s)
  2975. mp3.s.rd_preamble = !!simple_strtoul(s, NULL, 0);
  2976. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS1_X)) {
  2977. int delay = 0;
  2978. if (lranks_per_prank == 4 && ddr_hertz >= 1000000000)
  2979. delay = 1;
  2980. mp3.s.xrank_add_tccd_l = delay;
  2981. mp3.s.xrank_add_tccd_s = delay;
  2982. }
  2983. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS3(if_num), mp3.u64);
  2984. debug("MODEREG_PARAMS3 : 0x%016llx\n",
  2985. mp3.u64);
  2986. }
  2987. }
  2988. static void lmc_nxm(struct ddr_priv *priv)
  2989. {
  2990. union cvmx_lmcx_nxm lmc_nxm;
  2991. int num_bits = row_lsb + row_bits + lranks_bits - 26;
  2992. char *s;
  2993. lmc_nxm.u64 = lmc_rd(priv, CVMX_LMCX_NXM(if_num));
  2994. /* .cn78xx. */
  2995. if (rank_mask & 0x1)
  2996. lmc_nxm.cn78xx.mem_msb_d0_r0 = num_bits;
  2997. if (rank_mask & 0x2)
  2998. lmc_nxm.cn78xx.mem_msb_d0_r1 = num_bits;
  2999. if (rank_mask & 0x4)
  3000. lmc_nxm.cn78xx.mem_msb_d1_r0 = num_bits;
  3001. if (rank_mask & 0x8)
  3002. lmc_nxm.cn78xx.mem_msb_d1_r1 = num_bits;
  3003. /* Set the mask for non-existent ranks. */
  3004. lmc_nxm.cn78xx.cs_mask = ~rank_mask & 0xff;
  3005. s = lookup_env_ull(priv, "ddr_nxm");
  3006. if (s)
  3007. lmc_nxm.u64 = simple_strtoull(s, NULL, 0);
  3008. debug("LMC_NXM : 0x%016llx\n",
  3009. lmc_nxm.u64);
  3010. lmc_wr(priv, CVMX_LMCX_NXM(if_num), lmc_nxm.u64);
  3011. }
  3012. static void lmc_wodt_mask(struct ddr_priv *priv)
  3013. {
  3014. union cvmx_lmcx_wodt_mask wodt_mask;
  3015. char *s;
  3016. wodt_mask.u64 = odt_config[odt_idx].odt_mask;
  3017. s = lookup_env_ull(priv, "ddr_wodt_mask");
  3018. if (s)
  3019. wodt_mask.u64 = simple_strtoull(s, NULL, 0);
  3020. debug("WODT_MASK : 0x%016llx\n",
  3021. wodt_mask.u64);
  3022. lmc_wr(priv, CVMX_LMCX_WODT_MASK(if_num), wodt_mask.u64);
  3023. }
  3024. static void lmc_rodt_mask(struct ddr_priv *priv)
  3025. {
  3026. union cvmx_lmcx_rodt_mask rodt_mask;
  3027. int rankx;
  3028. char *s;
  3029. rodt_mask.u64 = odt_config[odt_idx].rodt_ctl;
  3030. s = lookup_env_ull(priv, "ddr_rodt_mask");
  3031. if (s)
  3032. rodt_mask.u64 = simple_strtoull(s, NULL, 0);
  3033. debug("%-45s : 0x%016llx\n", "RODT_MASK", rodt_mask.u64);
  3034. lmc_wr(priv, CVMX_LMCX_RODT_MASK(if_num), rodt_mask.u64);
  3035. dyn_rtt_nom_mask = 0;
  3036. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  3037. if (!(rank_mask & (1 << rankx)))
  3038. continue;
  3039. dyn_rtt_nom_mask |= ((rodt_mask.u64 >> (8 * rankx)) & 0xff);
  3040. }
  3041. if (num_ranks == 4) {
  3042. /*
  3043. * Normally ODT1 is wired to rank 1. For quad-ranked DIMMs
  3044. * ODT1 is wired to the third rank (rank 2). The mask,
  3045. * dyn_rtt_nom_mask, is used to indicate for which ranks
  3046. * to sweep RTT_NOM during read-leveling. Shift the bit
  3047. * from the ODT1 position over to the "ODT2" position so
  3048. * that the read-leveling analysis comes out right.
  3049. */
  3050. int odt1_bit = dyn_rtt_nom_mask & 2;
  3051. dyn_rtt_nom_mask &= ~2;
  3052. dyn_rtt_nom_mask |= odt1_bit << 1;
  3053. }
  3054. debug("%-45s : 0x%02x\n", "DYN_RTT_NOM_MASK", dyn_rtt_nom_mask);
  3055. }
  3056. static void lmc_comp_ctl2(struct ddr_priv *priv)
  3057. {
  3058. union cvmx_lmcx_comp_ctl2 cc2;
  3059. char *s;
  3060. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  3061. cc2.cn78xx.dqx_ctl = odt_config[odt_idx].odt_ena;
  3062. /* Default 4=34.3 ohm */
  3063. cc2.cn78xx.ck_ctl = (c_cfg->ck_ctl == 0) ? 4 : c_cfg->ck_ctl;
  3064. /* Default 4=34.3 ohm */
  3065. cc2.cn78xx.cmd_ctl = (c_cfg->cmd_ctl == 0) ? 4 : c_cfg->cmd_ctl;
  3066. /* Default 4=34.3 ohm */
  3067. cc2.cn78xx.control_ctl = (c_cfg->ctl_ctl == 0) ? 4 : c_cfg->ctl_ctl;
  3068. ddr_rodt_ctl_auto = c_cfg->ddr_rodt_ctl_auto;
  3069. s = lookup_env(priv, "ddr_rodt_ctl_auto");
  3070. if (s)
  3071. ddr_rodt_ctl_auto = !!simple_strtoul(s, NULL, 0);
  3072. default_rodt_ctl = odt_config[odt_idx].qs_dic;
  3073. s = lookup_env(priv, "ddr_rodt_ctl");
  3074. if (!s)
  3075. s = lookup_env(priv, "ddr%d_rodt_ctl", if_num);
  3076. if (s) {
  3077. default_rodt_ctl = simple_strtoul(s, NULL, 0);
  3078. ddr_rodt_ctl_auto = 0;
  3079. }
  3080. cc2.cn70xx.rodt_ctl = default_rodt_ctl;
  3081. // if DDR4, force CK_CTL to 26 ohms if it is currently 34 ohms,
  3082. // and DCLK speed is 1 GHz or more...
  3083. if (ddr_type == DDR4_DRAM && cc2.s.ck_ctl == ddr4_driver_34_ohm &&
  3084. ddr_hertz >= 1000000000) {
  3085. // lowest for DDR4 is 26 ohms
  3086. cc2.s.ck_ctl = ddr4_driver_26_ohm;
  3087. debug("N%d.LMC%d: Forcing DDR4 COMP_CTL2[CK_CTL] to %d, %d ohms\n",
  3088. node, if_num, cc2.s.ck_ctl,
  3089. imp_val->drive_strength[cc2.s.ck_ctl]);
  3090. }
  3091. // if DDR4, 2DPC, UDIMM, force CONTROL_CTL and CMD_CTL to 26 ohms,
  3092. // if DCLK speed is 1 GHz or more...
  3093. if (ddr_type == DDR4_DRAM && dimm_count == 2 &&
  3094. (spd_dimm_type == 2 || spd_dimm_type == 6) &&
  3095. ddr_hertz >= 1000000000) {
  3096. // lowest for DDR4 is 26 ohms
  3097. cc2.cn78xx.control_ctl = ddr4_driver_26_ohm;
  3098. // lowest for DDR4 is 26 ohms
  3099. cc2.cn78xx.cmd_ctl = ddr4_driver_26_ohm;
  3100. debug("N%d.LMC%d: Forcing DDR4 COMP_CTL2[CONTROL_CTL,CMD_CTL] to %d, %d ohms\n",
  3101. node, if_num, ddr4_driver_26_ohm,
  3102. imp_val->drive_strength[ddr4_driver_26_ohm]);
  3103. }
  3104. s = lookup_env(priv, "ddr_ck_ctl");
  3105. if (s)
  3106. cc2.cn78xx.ck_ctl = simple_strtoul(s, NULL, 0);
  3107. s = lookup_env(priv, "ddr_cmd_ctl");
  3108. if (s)
  3109. cc2.cn78xx.cmd_ctl = simple_strtoul(s, NULL, 0);
  3110. s = lookup_env(priv, "ddr_control_ctl");
  3111. if (s)
  3112. cc2.cn70xx.control_ctl = simple_strtoul(s, NULL, 0);
  3113. s = lookup_env(priv, "ddr_dqx_ctl");
  3114. if (s)
  3115. cc2.cn78xx.dqx_ctl = simple_strtoul(s, NULL, 0);
  3116. debug("%-45s : %d, %d ohms\n", "DQX_CTL ", cc2.cn78xx.dqx_ctl,
  3117. imp_val->drive_strength[cc2.cn78xx.dqx_ctl]);
  3118. debug("%-45s : %d, %d ohms\n", "CK_CTL ", cc2.cn78xx.ck_ctl,
  3119. imp_val->drive_strength[cc2.cn78xx.ck_ctl]);
  3120. debug("%-45s : %d, %d ohms\n", "CMD_CTL ", cc2.cn78xx.cmd_ctl,
  3121. imp_val->drive_strength[cc2.cn78xx.cmd_ctl]);
  3122. debug("%-45s : %d, %d ohms\n", "CONTROL_CTL ",
  3123. cc2.cn78xx.control_ctl,
  3124. imp_val->drive_strength[cc2.cn78xx.control_ctl]);
  3125. debug("Read ODT_CTL : 0x%x (%d ohms)\n",
  3126. cc2.cn78xx.rodt_ctl, imp_val->rodt_ohms[cc2.cn78xx.rodt_ctl]);
  3127. debug("%-45s : 0x%016llx\n", "COMP_CTL2", cc2.u64);
  3128. lmc_wr(priv, CVMX_LMCX_COMP_CTL2(if_num), cc2.u64);
  3129. }
  3130. static void lmc_phy_ctl(struct ddr_priv *priv)
  3131. {
  3132. union cvmx_lmcx_phy_ctl phy_ctl;
  3133. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  3134. phy_ctl.s.ts_stagger = 0;
  3135. // FIXME: are there others TBD?
  3136. phy_ctl.s.dsk_dbg_overwrt_ena = 0;
  3137. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS1_X) && lranks_per_prank > 1) {
  3138. // C0 is TEN, C1 is A17
  3139. phy_ctl.s.c0_sel = 2;
  3140. phy_ctl.s.c1_sel = 2;
  3141. debug("N%d.LMC%d: 3DS: setting PHY_CTL[cx_csel] = %d\n",
  3142. node, if_num, phy_ctl.s.c1_sel);
  3143. }
  3144. debug("PHY_CTL : 0x%016llx\n",
  3145. phy_ctl.u64);
  3146. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  3147. }
  3148. static void lmc_ext_config(struct ddr_priv *priv)
  3149. {
  3150. union cvmx_lmcx_ext_config ext_cfg;
  3151. char *s;
  3152. ext_cfg.u64 = lmc_rd(priv, CVMX_LMCX_EXT_CONFIG(if_num));
  3153. ext_cfg.s.vrefint_seq_deskew = 0;
  3154. ext_cfg.s.read_ena_bprch = 1;
  3155. ext_cfg.s.read_ena_fprch = 1;
  3156. ext_cfg.s.drive_ena_fprch = 1;
  3157. ext_cfg.s.drive_ena_bprch = 1;
  3158. // make sure this is OFF for all current chips
  3159. ext_cfg.s.invert_data = 0;
  3160. s = lookup_env(priv, "ddr_read_fprch");
  3161. if (s)
  3162. ext_cfg.s.read_ena_fprch = strtoul(s, NULL, 0);
  3163. s = lookup_env(priv, "ddr_read_bprch");
  3164. if (s)
  3165. ext_cfg.s.read_ena_bprch = strtoul(s, NULL, 0);
  3166. s = lookup_env(priv, "ddr_drive_fprch");
  3167. if (s)
  3168. ext_cfg.s.drive_ena_fprch = strtoul(s, NULL, 0);
  3169. s = lookup_env(priv, "ddr_drive_bprch");
  3170. if (s)
  3171. ext_cfg.s.drive_ena_bprch = strtoul(s, NULL, 0);
  3172. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS1_X) && lranks_per_prank > 1) {
  3173. ext_cfg.s.dimm0_cid = lranks_bits;
  3174. ext_cfg.s.dimm1_cid = lranks_bits;
  3175. debug("N%d.LMC%d: 3DS: setting EXT_CONFIG[dimmx_cid] = %d\n",
  3176. node, if_num, ext_cfg.s.dimm0_cid);
  3177. }
  3178. lmc_wr(priv, CVMX_LMCX_EXT_CONFIG(if_num), ext_cfg.u64);
  3179. debug("%-45s : 0x%016llx\n", "EXT_CONFIG", ext_cfg.u64);
  3180. }
  3181. static void lmc_ext_config2(struct ddr_priv *priv)
  3182. {
  3183. char *s;
  3184. // NOTE: all chips have this register, but not necessarily the
  3185. // fields we modify...
  3186. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS1_X) &&
  3187. !octeon_is_cpuid(OCTEON_CN73XX)) {
  3188. union cvmx_lmcx_ext_config2 ext_cfg2;
  3189. int value = 1; // default to 1
  3190. ext_cfg2.u64 = lmc_rd(priv, CVMX_LMCX_EXT_CONFIG2(if_num));
  3191. s = lookup_env(priv, "ddr_ext2_delay_unload");
  3192. if (s)
  3193. value = !!simple_strtoul(s, NULL, 0);
  3194. ext_cfg2.s.delay_unload_r0 = value;
  3195. ext_cfg2.s.delay_unload_r1 = value;
  3196. ext_cfg2.s.delay_unload_r2 = value;
  3197. ext_cfg2.s.delay_unload_r3 = value;
  3198. lmc_wr(priv, CVMX_LMCX_EXT_CONFIG2(if_num), ext_cfg2.u64);
  3199. debug("%-45s : 0x%016llx\n", "EXT_CONFIG2", ext_cfg2.u64);
  3200. }
  3201. }
  3202. static void lmc_dimm01_params_loop(struct ddr_priv *priv)
  3203. {
  3204. union cvmx_lmcx_dimmx_params dimm_p;
  3205. int dimmx = didx;
  3206. char *s;
  3207. int rc;
  3208. int i;
  3209. dimm_p.u64 = lmc_rd(priv, CVMX_LMCX_DIMMX_PARAMS(dimmx, if_num));
  3210. if (ddr_type == DDR4_DRAM) {
  3211. union cvmx_lmcx_dimmx_ddr4_params0 ddr4_p0;
  3212. union cvmx_lmcx_dimmx_ddr4_params1 ddr4_p1;
  3213. union cvmx_lmcx_ddr4_dimm_ctl ddr4_ctl;
  3214. dimm_p.s.rc0 = 0;
  3215. dimm_p.s.rc1 = 0;
  3216. dimm_p.s.rc2 = 0;
  3217. rc = read_spd(&dimm_config_table[didx], 0,
  3218. DDR4_SPD_RDIMM_REGISTER_DRIVE_STRENGTH_CTL);
  3219. dimm_p.s.rc3 = (rc >> 4) & 0xf;
  3220. dimm_p.s.rc4 = ((rc >> 0) & 0x3) << 2;
  3221. dimm_p.s.rc4 |= ((rc >> 2) & 0x3) << 0;
  3222. rc = read_spd(&dimm_config_table[didx], 0,
  3223. DDR4_SPD_RDIMM_REGISTER_DRIVE_STRENGTH_CK);
  3224. dimm_p.s.rc5 = ((rc >> 0) & 0x3) << 2;
  3225. dimm_p.s.rc5 |= ((rc >> 2) & 0x3) << 0;
  3226. dimm_p.s.rc6 = 0;
  3227. dimm_p.s.rc7 = 0;
  3228. dimm_p.s.rc8 = 0;
  3229. dimm_p.s.rc9 = 0;
  3230. /*
  3231. * rc10 DDR4 RDIMM Operating Speed
  3232. * === ===================================================
  3233. * 0 tclk_psecs >= 1250 psec DDR4-1600 (1250 ps)
  3234. * 1 1250 psec > tclk_psecs >= 1071 psec DDR4-1866 (1071 ps)
  3235. * 2 1071 psec > tclk_psecs >= 938 psec DDR4-2133 ( 938 ps)
  3236. * 3 938 psec > tclk_psecs >= 833 psec DDR4-2400 ( 833 ps)
  3237. * 4 833 psec > tclk_psecs >= 750 psec DDR4-2666 ( 750 ps)
  3238. * 5 750 psec > tclk_psecs >= 625 psec DDR4-3200 ( 625 ps)
  3239. */
  3240. dimm_p.s.rc10 = 0;
  3241. if (tclk_psecs < 1250)
  3242. dimm_p.s.rc10 = 1;
  3243. if (tclk_psecs < 1071)
  3244. dimm_p.s.rc10 = 2;
  3245. if (tclk_psecs < 938)
  3246. dimm_p.s.rc10 = 3;
  3247. if (tclk_psecs < 833)
  3248. dimm_p.s.rc10 = 4;
  3249. if (tclk_psecs < 750)
  3250. dimm_p.s.rc10 = 5;
  3251. dimm_p.s.rc11 = 0;
  3252. dimm_p.s.rc12 = 0;
  3253. /* 0=LRDIMM, 1=RDIMM */
  3254. dimm_p.s.rc13 = (spd_dimm_type == 4) ? 0 : 4;
  3255. dimm_p.s.rc13 |= (ddr_type == DDR4_DRAM) ?
  3256. (spd_addr_mirror << 3) : 0;
  3257. dimm_p.s.rc14 = 0;
  3258. dimm_p.s.rc15 = 0; /* 1 nCK latency adder */
  3259. ddr4_p0.u64 = 0;
  3260. ddr4_p0.s.rc8x = 0;
  3261. ddr4_p0.s.rc7x = 0;
  3262. ddr4_p0.s.rc6x = 0;
  3263. ddr4_p0.s.rc5x = 0;
  3264. ddr4_p0.s.rc4x = 0;
  3265. ddr4_p0.s.rc3x = compute_rc3x(tclk_psecs);
  3266. ddr4_p0.s.rc2x = 0;
  3267. ddr4_p0.s.rc1x = 0;
  3268. ddr4_p1.u64 = 0;
  3269. ddr4_p1.s.rcbx = 0;
  3270. ddr4_p1.s.rcax = 0;
  3271. ddr4_p1.s.rc9x = 0;
  3272. ddr4_ctl.u64 = 0;
  3273. ddr4_ctl.cn70xx.ddr4_dimm0_wmask = 0x004;
  3274. ddr4_ctl.cn70xx.ddr4_dimm1_wmask =
  3275. (dimm_count > 1) ? 0x004 : 0x0000;
  3276. /*
  3277. * Handle any overrides from envvars here...
  3278. */
  3279. s = lookup_env(priv, "ddr_ddr4_params0");
  3280. if (s)
  3281. ddr4_p0.u64 = simple_strtoul(s, NULL, 0);
  3282. s = lookup_env(priv, "ddr_ddr4_params1");
  3283. if (s)
  3284. ddr4_p1.u64 = simple_strtoul(s, NULL, 0);
  3285. s = lookup_env(priv, "ddr_ddr4_dimm_ctl");
  3286. if (s)
  3287. ddr4_ctl.u64 = simple_strtoul(s, NULL, 0);
  3288. for (i = 0; i < 11; ++i) {
  3289. u64 value;
  3290. s = lookup_env(priv, "ddr_ddr4_rc%1xx", i + 1);
  3291. if (s) {
  3292. value = simple_strtoul(s, NULL, 0);
  3293. if (i < 8) {
  3294. ddr4_p0.u64 &= ~((u64)0xff << (i * 8));
  3295. ddr4_p0.u64 |= (value << (i * 8));
  3296. } else {
  3297. ddr4_p1.u64 &=
  3298. ~((u64)0xff << ((i - 8) * 8));
  3299. ddr4_p1.u64 |= (value << ((i - 8) * 8));
  3300. }
  3301. }
  3302. }
  3303. /*
  3304. * write the final CSR values
  3305. */
  3306. lmc_wr(priv, CVMX_LMCX_DIMMX_DDR4_PARAMS0(dimmx, if_num),
  3307. ddr4_p0.u64);
  3308. lmc_wr(priv, CVMX_LMCX_DDR4_DIMM_CTL(if_num), ddr4_ctl.u64);
  3309. lmc_wr(priv, CVMX_LMCX_DIMMX_DDR4_PARAMS1(dimmx, if_num),
  3310. ddr4_p1.u64);
  3311. debug("DIMM%d Register Control Words RCBx:RC1x : %x %x %x %x %x %x %x %x %x %x %x\n",
  3312. dimmx, ddr4_p1.s.rcbx, ddr4_p1.s.rcax,
  3313. ddr4_p1.s.rc9x, ddr4_p0.s.rc8x,
  3314. ddr4_p0.s.rc7x, ddr4_p0.s.rc6x,
  3315. ddr4_p0.s.rc5x, ddr4_p0.s.rc4x,
  3316. ddr4_p0.s.rc3x, ddr4_p0.s.rc2x, ddr4_p0.s.rc1x);
  3317. } else {
  3318. rc = read_spd(&dimm_config_table[didx], 0, 69);
  3319. dimm_p.s.rc0 = (rc >> 0) & 0xf;
  3320. dimm_p.s.rc1 = (rc >> 4) & 0xf;
  3321. rc = read_spd(&dimm_config_table[didx], 0, 70);
  3322. dimm_p.s.rc2 = (rc >> 0) & 0xf;
  3323. dimm_p.s.rc3 = (rc >> 4) & 0xf;
  3324. rc = read_spd(&dimm_config_table[didx], 0, 71);
  3325. dimm_p.s.rc4 = (rc >> 0) & 0xf;
  3326. dimm_p.s.rc5 = (rc >> 4) & 0xf;
  3327. rc = read_spd(&dimm_config_table[didx], 0, 72);
  3328. dimm_p.s.rc6 = (rc >> 0) & 0xf;
  3329. dimm_p.s.rc7 = (rc >> 4) & 0xf;
  3330. rc = read_spd(&dimm_config_table[didx], 0, 73);
  3331. dimm_p.s.rc8 = (rc >> 0) & 0xf;
  3332. dimm_p.s.rc9 = (rc >> 4) & 0xf;
  3333. rc = read_spd(&dimm_config_table[didx], 0, 74);
  3334. dimm_p.s.rc10 = (rc >> 0) & 0xf;
  3335. dimm_p.s.rc11 = (rc >> 4) & 0xf;
  3336. rc = read_spd(&dimm_config_table[didx], 0, 75);
  3337. dimm_p.s.rc12 = (rc >> 0) & 0xf;
  3338. dimm_p.s.rc13 = (rc >> 4) & 0xf;
  3339. rc = read_spd(&dimm_config_table[didx], 0, 76);
  3340. dimm_p.s.rc14 = (rc >> 0) & 0xf;
  3341. dimm_p.s.rc15 = (rc >> 4) & 0xf;
  3342. s = ddr_getenv_debug(priv, "ddr_clk_drive");
  3343. if (s) {
  3344. if (strcmp(s, "light") == 0)
  3345. dimm_p.s.rc5 = 0x0; /* Light Drive */
  3346. if (strcmp(s, "moderate") == 0)
  3347. dimm_p.s.rc5 = 0x5; /* Moderate Drive */
  3348. if (strcmp(s, "strong") == 0)
  3349. dimm_p.s.rc5 = 0xA; /* Strong Drive */
  3350. printf("Parameter found in environment. ddr_clk_drive = %s\n",
  3351. s);
  3352. }
  3353. s = ddr_getenv_debug(priv, "ddr_cmd_drive");
  3354. if (s) {
  3355. if (strcmp(s, "light") == 0)
  3356. dimm_p.s.rc3 = 0x0; /* Light Drive */
  3357. if (strcmp(s, "moderate") == 0)
  3358. dimm_p.s.rc3 = 0x5; /* Moderate Drive */
  3359. if (strcmp(s, "strong") == 0)
  3360. dimm_p.s.rc3 = 0xA; /* Strong Drive */
  3361. printf("Parameter found in environment. ddr_cmd_drive = %s\n",
  3362. s);
  3363. }
  3364. s = ddr_getenv_debug(priv, "ddr_ctl_drive");
  3365. if (s) {
  3366. if (strcmp(s, "light") == 0)
  3367. dimm_p.s.rc4 = 0x0; /* Light Drive */
  3368. if (strcmp(s, "moderate") == 0)
  3369. dimm_p.s.rc4 = 0x5; /* Moderate Drive */
  3370. printf("Parameter found in environment. ddr_ctl_drive = %s\n",
  3371. s);
  3372. }
  3373. /*
  3374. * rc10 DDR3 RDIMM Operating Speed
  3375. * == =====================================================
  3376. * 0 tclk_psecs >= 2500 psec DDR3/DDR3L-800 def
  3377. * 1 2500 psec > tclk_psecs >= 1875 psec DDR3/DDR3L-1066
  3378. * 2 1875 psec > tclk_psecs >= 1500 psec DDR3/DDR3L-1333
  3379. * 3 1500 psec > tclk_psecs >= 1250 psec DDR3/DDR3L-1600
  3380. * 4 1250 psec > tclk_psecs >= 1071 psec DDR3-1866
  3381. */
  3382. dimm_p.s.rc10 = 0;
  3383. if (tclk_psecs < 2500)
  3384. dimm_p.s.rc10 = 1;
  3385. if (tclk_psecs < 1875)
  3386. dimm_p.s.rc10 = 2;
  3387. if (tclk_psecs < 1500)
  3388. dimm_p.s.rc10 = 3;
  3389. if (tclk_psecs < 1250)
  3390. dimm_p.s.rc10 = 4;
  3391. }
  3392. s = lookup_env(priv, "ddr_dimmx_params", i);
  3393. if (s)
  3394. dimm_p.u64 = simple_strtoul(s, NULL, 0);
  3395. for (i = 0; i < 16; ++i) {
  3396. u64 value;
  3397. s = lookup_env(priv, "ddr_rc%d", i);
  3398. if (s) {
  3399. value = simple_strtoul(s, NULL, 0);
  3400. dimm_p.u64 &= ~((u64)0xf << (i * 4));
  3401. dimm_p.u64 |= (value << (i * 4));
  3402. }
  3403. }
  3404. lmc_wr(priv, CVMX_LMCX_DIMMX_PARAMS(dimmx, if_num), dimm_p.u64);
  3405. debug("DIMM%d Register Control Words RC15:RC0 : %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
  3406. dimmx, dimm_p.s.rc15, dimm_p.s.rc14, dimm_p.s.rc13,
  3407. dimm_p.s.rc12, dimm_p.s.rc11, dimm_p.s.rc10,
  3408. dimm_p.s.rc9, dimm_p.s.rc8, dimm_p.s.rc7,
  3409. dimm_p.s.rc6, dimm_p.s.rc5, dimm_p.s.rc4,
  3410. dimm_p.s.rc3, dimm_p.s.rc2, dimm_p.s.rc1, dimm_p.s.rc0);
  3411. // FIXME: recognize a DDR3 RDIMM with 4 ranks and 2 registers,
  3412. // and treat it specially
  3413. if (ddr_type == DDR3_DRAM && num_ranks == 4 &&
  3414. spd_rdimm_registers == 2 && dimmx == 0) {
  3415. debug("DDR3: Copying DIMM0_PARAMS to DIMM1_PARAMS for pseudo-DIMM #1...\n");
  3416. lmc_wr(priv, CVMX_LMCX_DIMMX_PARAMS(1, if_num), dimm_p.u64);
  3417. }
  3418. }
  3419. static void lmc_dimm01_params(struct ddr_priv *priv)
  3420. {
  3421. union cvmx_lmcx_dimm_ctl dimm_ctl;
  3422. char *s;
  3423. if (spd_rdimm) {
  3424. for (didx = 0; didx < (unsigned int)dimm_count; ++didx)
  3425. lmc_dimm01_params_loop(priv);
  3426. if (ddr_type == DDR4_DRAM) {
  3427. /* LMC0_DIMM_CTL */
  3428. dimm_ctl.u64 = lmc_rd(priv, CVMX_LMCX_DIMM_CTL(if_num));
  3429. dimm_ctl.s.dimm0_wmask = 0xdf3f;
  3430. dimm_ctl.s.dimm1_wmask =
  3431. (dimm_count > 1) ? 0xdf3f : 0x0000;
  3432. dimm_ctl.s.tcws = 0x4e0;
  3433. dimm_ctl.s.parity = c_cfg->parity;
  3434. s = lookup_env(priv, "ddr_dimm0_wmask");
  3435. if (s) {
  3436. dimm_ctl.s.dimm0_wmask =
  3437. simple_strtoul(s, NULL, 0);
  3438. }
  3439. s = lookup_env(priv, "ddr_dimm1_wmask");
  3440. if (s) {
  3441. dimm_ctl.s.dimm1_wmask =
  3442. simple_strtoul(s, NULL, 0);
  3443. }
  3444. s = lookup_env(priv, "ddr_dimm_ctl_parity");
  3445. if (s)
  3446. dimm_ctl.s.parity = simple_strtoul(s, NULL, 0);
  3447. s = lookup_env(priv, "ddr_dimm_ctl_tcws");
  3448. if (s)
  3449. dimm_ctl.s.tcws = simple_strtoul(s, NULL, 0);
  3450. debug("LMC DIMM_CTL : 0x%016llx\n",
  3451. dimm_ctl.u64);
  3452. lmc_wr(priv, CVMX_LMCX_DIMM_CTL(if_num), dimm_ctl.u64);
  3453. /* Init RCW */
  3454. oct3_ddr3_seq(priv, rank_mask, if_num, 0x7);
  3455. /* Write RC0D last */
  3456. dimm_ctl.s.dimm0_wmask = 0x2000;
  3457. dimm_ctl.s.dimm1_wmask = (dimm_count > 1) ?
  3458. 0x2000 : 0x0000;
  3459. debug("LMC DIMM_CTL : 0x%016llx\n",
  3460. dimm_ctl.u64);
  3461. lmc_wr(priv, CVMX_LMCX_DIMM_CTL(if_num), dimm_ctl.u64);
  3462. /*
  3463. * Don't write any extended registers the second time
  3464. */
  3465. lmc_wr(priv, CVMX_LMCX_DDR4_DIMM_CTL(if_num), 0);
  3466. /* Init RCW */
  3467. oct3_ddr3_seq(priv, rank_mask, if_num, 0x7);
  3468. } else {
  3469. /* LMC0_DIMM_CTL */
  3470. dimm_ctl.u64 = lmc_rd(priv, CVMX_LMCX_DIMM_CTL(if_num));
  3471. dimm_ctl.s.dimm0_wmask = 0xffff;
  3472. // FIXME: recognize a DDR3 RDIMM with 4 ranks and 2
  3473. // registers, and treat it specially
  3474. if (num_ranks == 4 && spd_rdimm_registers == 2) {
  3475. debug("DDR3: Activating DIMM_CTL[dimm1_mask] bits...\n");
  3476. dimm_ctl.s.dimm1_wmask = 0xffff;
  3477. } else {
  3478. dimm_ctl.s.dimm1_wmask =
  3479. (dimm_count > 1) ? 0xffff : 0x0000;
  3480. }
  3481. dimm_ctl.s.tcws = 0x4e0;
  3482. dimm_ctl.s.parity = c_cfg->parity;
  3483. s = lookup_env(priv, "ddr_dimm0_wmask");
  3484. if (s) {
  3485. dimm_ctl.s.dimm0_wmask =
  3486. simple_strtoul(s, NULL, 0);
  3487. }
  3488. s = lookup_env(priv, "ddr_dimm1_wmask");
  3489. if (s) {
  3490. dimm_ctl.s.dimm1_wmask =
  3491. simple_strtoul(s, NULL, 0);
  3492. }
  3493. s = lookup_env(priv, "ddr_dimm_ctl_parity");
  3494. if (s)
  3495. dimm_ctl.s.parity = simple_strtoul(s, NULL, 0);
  3496. s = lookup_env(priv, "ddr_dimm_ctl_tcws");
  3497. if (s)
  3498. dimm_ctl.s.tcws = simple_strtoul(s, NULL, 0);
  3499. debug("LMC DIMM_CTL : 0x%016llx\n",
  3500. dimm_ctl.u64);
  3501. lmc_wr(priv, CVMX_LMCX_DIMM_CTL(if_num), dimm_ctl.u64);
  3502. /* Init RCW */
  3503. oct3_ddr3_seq(priv, rank_mask, if_num, 0x7);
  3504. }
  3505. } else {
  3506. /* Disable register control writes for unbuffered */
  3507. union cvmx_lmcx_dimm_ctl dimm_ctl;
  3508. dimm_ctl.u64 = lmc_rd(priv, CVMX_LMCX_DIMM_CTL(if_num));
  3509. dimm_ctl.s.dimm0_wmask = 0;
  3510. dimm_ctl.s.dimm1_wmask = 0;
  3511. lmc_wr(priv, CVMX_LMCX_DIMM_CTL(if_num), dimm_ctl.u64);
  3512. }
  3513. }
  3514. static int lmc_rank_init(struct ddr_priv *priv)
  3515. {
  3516. char *s;
  3517. if (enable_by_rank_init) {
  3518. by_rank = 3;
  3519. saved_rank_mask = rank_mask;
  3520. }
  3521. start_by_rank_init:
  3522. if (enable_by_rank_init) {
  3523. rank_mask = (1 << by_rank);
  3524. if (!(rank_mask & saved_rank_mask))
  3525. goto end_by_rank_init;
  3526. if (by_rank == 0)
  3527. rank_mask = saved_rank_mask;
  3528. debug("\n>>>>> BY_RANK: starting rank %d with mask 0x%02x\n\n",
  3529. by_rank, rank_mask);
  3530. }
  3531. /*
  3532. * Comments (steps 3 through 5) continue in oct3_ddr3_seq()
  3533. */
  3534. union cvmx_lmcx_modereg_params0 mp0;
  3535. if (ddr_memory_preserved(priv)) {
  3536. /*
  3537. * Contents are being preserved. Take DRAM out of self-refresh
  3538. * first. Then init steps can procede normally
  3539. */
  3540. /* self-refresh exit */
  3541. oct3_ddr3_seq(priv, rank_mask, if_num, 3);
  3542. }
  3543. mp0.u64 = lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num));
  3544. mp0.s.dllr = 1; /* Set during first init sequence */
  3545. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num), mp0.u64);
  3546. ddr_init_seq(priv, rank_mask, if_num);
  3547. mp0.s.dllr = 0; /* Clear for normal operation */
  3548. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num), mp0.u64);
  3549. if (spd_rdimm && ddr_type == DDR4_DRAM &&
  3550. octeon_is_cpuid(OCTEON_CN7XXX)) {
  3551. debug("Running init sequence 1\n");
  3552. change_rdimm_mpr_pattern(priv, rank_mask, if_num, dimm_count);
  3553. }
  3554. memset(lanes, 0, sizeof(lanes));
  3555. for (lane = 0; lane < last_lane; lane++) {
  3556. // init all lanes to reset value
  3557. dac_settings[lane] = 127;
  3558. }
  3559. // FIXME: disable internal VREF if deskew is disabled?
  3560. if (disable_deskew_training) {
  3561. debug("N%d.LMC%d: internal VREF Training disabled, leaving them in RESET.\n",
  3562. node, if_num);
  3563. num_samples = 0;
  3564. } else if (ddr_type == DDR4_DRAM &&
  3565. !octeon_is_cpuid(OCTEON_CN78XX_PASS1_X)) {
  3566. num_samples = DEFAULT_DAC_SAMPLES;
  3567. } else {
  3568. // if DDR3 or no ability to write DAC values
  3569. num_samples = 1;
  3570. }
  3571. perform_internal_vref_training:
  3572. total_dac_eval_retries = 0;
  3573. dac_eval_exhausted = 0;
  3574. for (sample = 0; sample < num_samples; sample++) {
  3575. dac_eval_retries = 0;
  3576. // make offset and internal vref training repeatable
  3577. do {
  3578. /*
  3579. * 6.9.8 LMC Offset Training
  3580. * LMC requires input-receiver offset training.
  3581. */
  3582. perform_offset_training(priv, rank_mask, if_num);
  3583. /*
  3584. * 6.9.9 LMC Internal vref Training
  3585. * LMC requires input-reference-voltage training.
  3586. */
  3587. perform_internal_vref_training(priv, rank_mask, if_num);
  3588. // read and maybe display the DAC values for a sample
  3589. read_dac_dbi_settings(priv, if_num, /*DAC*/ 1,
  3590. dac_settings);
  3591. if (num_samples == 1 || ddr_verbose(priv)) {
  3592. display_dac_dbi_settings(if_num, /*DAC*/ 1,
  3593. use_ecc, dac_settings,
  3594. "Internal VREF");
  3595. }
  3596. // for DDR4, evaluate the DAC settings and retry
  3597. // if any issues
  3598. if (ddr_type == DDR4_DRAM) {
  3599. if (evaluate_dac_settings
  3600. (if_64b, use_ecc, dac_settings)) {
  3601. dac_eval_retries += 1;
  3602. if (dac_eval_retries >
  3603. DAC_RETRIES_LIMIT) {
  3604. debug("N%d.LMC%d: DDR4 internal VREF DAC settings: retries exhausted; continuing...\n",
  3605. node, if_num);
  3606. dac_eval_exhausted += 1;
  3607. } else {
  3608. debug("N%d.LMC%d: DDR4 internal VREF DAC settings inconsistent; retrying....\n",
  3609. node, if_num);
  3610. total_dac_eval_retries += 1;
  3611. // try another sample
  3612. continue;
  3613. }
  3614. }
  3615. // taking multiple samples, otherwise do nothing
  3616. if (num_samples > 1) {
  3617. // good sample or exhausted retries,
  3618. // record it
  3619. for (lane = 0; lane < last_lane;
  3620. lane++) {
  3621. lanes[lane].bytes[sample] =
  3622. dac_settings[lane];
  3623. }
  3624. }
  3625. }
  3626. // done if DDR3, or good sample, or exhausted retries
  3627. break;
  3628. } while (1);
  3629. }
  3630. if (ddr_type == DDR4_DRAM && dac_eval_exhausted > 0) {
  3631. debug("N%d.LMC%d: DDR internal VREF DAC settings: total retries %d, exhausted %d\n",
  3632. node, if_num, total_dac_eval_retries, dac_eval_exhausted);
  3633. }
  3634. if (num_samples > 1) {
  3635. debug("N%d.LMC%d: DDR4 internal VREF DAC settings: processing multiple samples...\n",
  3636. node, if_num);
  3637. for (lane = 0; lane < last_lane; lane++) {
  3638. dac_settings[lane] =
  3639. process_samples_average(&lanes[lane].bytes[0],
  3640. num_samples, if_num, lane);
  3641. }
  3642. display_dac_dbi_settings(if_num, /*DAC*/ 1, use_ecc,
  3643. dac_settings, "Averaged VREF");
  3644. // finally, write the final DAC values
  3645. for (lane = 0; lane < last_lane; lane++) {
  3646. load_dac_override(priv, if_num, dac_settings[lane],
  3647. lane);
  3648. }
  3649. }
  3650. // allow override of any byte-lane internal VREF
  3651. int overrode_vref_dac = 0;
  3652. for (lane = 0; lane < last_lane; lane++) {
  3653. s = lookup_env(priv, "ddr%d_vref_dac_byte%d", if_num, lane);
  3654. if (s) {
  3655. dac_settings[lane] = simple_strtoul(s, NULL, 0);
  3656. overrode_vref_dac = 1;
  3657. // finally, write the new DAC value
  3658. load_dac_override(priv, if_num, dac_settings[lane],
  3659. lane);
  3660. }
  3661. }
  3662. if (overrode_vref_dac) {
  3663. display_dac_dbi_settings(if_num, /*DAC*/ 1, use_ecc,
  3664. dac_settings, "Override VREF");
  3665. }
  3666. // as a second step, after internal VREF training, before starting
  3667. // deskew training:
  3668. // for DDR3 and OCTEON3 not O78 pass 1.x, override the DAC setting
  3669. // to 127
  3670. if (ddr_type == DDR3_DRAM && !octeon_is_cpuid(OCTEON_CN78XX_PASS1_X) &&
  3671. !disable_deskew_training) {
  3672. load_dac_override(priv, if_num, 127, /* all */ 0x0A);
  3673. debug("N%d.LMC%d: Overriding DDR3 internal VREF DAC settings to 127.\n",
  3674. node, if_num);
  3675. }
  3676. /*
  3677. * 4.8.8 LMC Deskew Training
  3678. *
  3679. * LMC requires input-read-data deskew training.
  3680. */
  3681. if (!disable_deskew_training) {
  3682. deskew_training_errors =
  3683. perform_deskew_training(priv, rank_mask, if_num,
  3684. spd_rawcard_aorb);
  3685. // All the Deskew lock and saturation retries (may) have
  3686. // been done, but we ended up with nibble errors; so,
  3687. // as a last ditch effort, try the Internal vref
  3688. // Training again...
  3689. if (deskew_training_errors) {
  3690. if (internal_retries <
  3691. DEFAULT_INTERNAL_VREF_TRAINING_LIMIT) {
  3692. internal_retries++;
  3693. debug("N%d.LMC%d: Deskew training results still unsettled - retrying internal vref training (%d)\n",
  3694. node, if_num, internal_retries);
  3695. goto perform_internal_vref_training;
  3696. } else {
  3697. if (restart_if_dsk_incomplete) {
  3698. debug("N%d.LMC%d: INFO: Deskew training incomplete - %d retries exhausted, Restarting LMC init...\n",
  3699. node, if_num, internal_retries);
  3700. return -EAGAIN;
  3701. }
  3702. debug("N%d.LMC%d: Deskew training incomplete - %d retries exhausted, but continuing...\n",
  3703. node, if_num, internal_retries);
  3704. }
  3705. } /* if (deskew_training_errors) */
  3706. // FIXME: treat this as the final DSK print from now on,
  3707. // and print if VBL_NORM or above also, save the results
  3708. // of the original training in case we want them later
  3709. validate_deskew_training(priv, rank_mask, if_num,
  3710. &deskew_training_results, 1);
  3711. } else { /* if (! disable_deskew_training) */
  3712. debug("N%d.LMC%d: Deskew Training disabled, printing settings before HWL.\n",
  3713. node, if_num);
  3714. validate_deskew_training(priv, rank_mask, if_num,
  3715. &deskew_training_results, 1);
  3716. } /* if (! disable_deskew_training) */
  3717. if (enable_by_rank_init) {
  3718. read_dac_dbi_settings(priv, if_num, /*dac */ 1,
  3719. &rank_dac[by_rank].bytes[0]);
  3720. get_deskew_settings(priv, if_num, &rank_dsk[by_rank]);
  3721. debug("\n>>>>> BY_RANK: ending rank %d\n\n", by_rank);
  3722. }
  3723. end_by_rank_init:
  3724. if (enable_by_rank_init) {
  3725. //debug("\n>>>>> BY_RANK: ending rank %d\n\n", by_rank);
  3726. by_rank--;
  3727. if (by_rank >= 0)
  3728. goto start_by_rank_init;
  3729. rank_mask = saved_rank_mask;
  3730. ddr_init_seq(priv, rank_mask, if_num);
  3731. process_by_rank_dac(priv, if_num, rank_mask, rank_dac);
  3732. process_by_rank_dsk(priv, if_num, rank_mask, rank_dsk);
  3733. // FIXME: set this to prevent later checking!!!
  3734. disable_deskew_training = 1;
  3735. debug("\n>>>>> BY_RANK: FINISHED!!\n\n");
  3736. }
  3737. return 0;
  3738. }
  3739. static void lmc_config_2(struct ddr_priv *priv)
  3740. {
  3741. union cvmx_lmcx_config lmc_config;
  3742. int save_ref_zqcs_int;
  3743. u64 temp_delay_usecs;
  3744. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  3745. /*
  3746. * Temporarily select the minimum ZQCS interval and wait
  3747. * long enough for a few ZQCS calibrations to occur. This
  3748. * should ensure that the calibration circuitry is
  3749. * stabilized before read/write leveling occurs.
  3750. */
  3751. if (octeon_is_cpuid(OCTEON_CN7XXX)) {
  3752. save_ref_zqcs_int = lmc_config.cn78xx.ref_zqcs_int;
  3753. /* set smallest interval */
  3754. lmc_config.cn78xx.ref_zqcs_int = 1 | (32 << 7);
  3755. } else {
  3756. save_ref_zqcs_int = lmc_config.cn63xx.ref_zqcs_int;
  3757. /* set smallest interval */
  3758. lmc_config.cn63xx.ref_zqcs_int = 1 | (32 << 7);
  3759. }
  3760. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), lmc_config.u64);
  3761. lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  3762. /*
  3763. * Compute an appropriate delay based on the current ZQCS
  3764. * interval. The delay should be long enough for the
  3765. * current ZQCS delay counter to expire plus ten of the
  3766. * minimum intarvals to ensure that some calibrations
  3767. * occur.
  3768. */
  3769. temp_delay_usecs = (((u64)save_ref_zqcs_int >> 7) * tclk_psecs *
  3770. 100 * 512 * 128) / (10000 * 10000) + 10 *
  3771. ((u64)32 * tclk_psecs * 100 * 512 * 128) / (10000 * 10000);
  3772. debug("Waiting %lld usecs for ZQCS calibrations to start\n",
  3773. temp_delay_usecs);
  3774. udelay(temp_delay_usecs);
  3775. if (octeon_is_cpuid(OCTEON_CN7XXX)) {
  3776. /* Restore computed interval */
  3777. lmc_config.cn78xx.ref_zqcs_int = save_ref_zqcs_int;
  3778. } else {
  3779. /* Restore computed interval */
  3780. lmc_config.cn63xx.ref_zqcs_int = save_ref_zqcs_int;
  3781. }
  3782. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), lmc_config.u64);
  3783. lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  3784. }
  3785. static union cvmx_lmcx_wlevel_ctl wl_ctl __section(".data");
  3786. static union cvmx_lmcx_wlevel_rankx wl_rank __section(".data");
  3787. static union cvmx_lmcx_modereg_params1 mp1 __section(".data");
  3788. static int wl_mask[9] __section(".data");
  3789. static int byte_idx __section(".data");
  3790. static int ecc_ena __section(".data");
  3791. static int wl_roundup __section(".data");
  3792. static int save_mode32b __section(".data");
  3793. static int disable_hwl_validity __section(".data");
  3794. static int default_wl_rtt_nom __section(".data");
  3795. static int wl_pbm_pump __section(".data");
  3796. static void lmc_write_leveling_loop(struct ddr_priv *priv, int rankx)
  3797. {
  3798. int wloop = 0;
  3799. // retries per sample for HW-related issues with bitmasks or values
  3800. int wloop_retries = 0;
  3801. int wloop_retries_total = 0;
  3802. int wloop_retries_exhausted = 0;
  3803. #define WLOOP_RETRIES_DEFAULT 5
  3804. int wl_val_err;
  3805. int wl_mask_err_rank = 0;
  3806. int wl_val_err_rank = 0;
  3807. // array to collect counts of byte-lane values
  3808. // assume low-order 3 bits and even, so really only 2-bit values
  3809. struct wlevel_bitcnt wl_bytes[9], wl_bytes_extra[9];
  3810. int extra_bumps, extra_mask;
  3811. int rank_nom = 0;
  3812. if (!(rank_mask & (1 << rankx)))
  3813. return;
  3814. if (match_wl_rtt_nom) {
  3815. if (rankx == 0)
  3816. rank_nom = mp1.s.rtt_nom_00;
  3817. if (rankx == 1)
  3818. rank_nom = mp1.s.rtt_nom_01;
  3819. if (rankx == 2)
  3820. rank_nom = mp1.s.rtt_nom_10;
  3821. if (rankx == 3)
  3822. rank_nom = mp1.s.rtt_nom_11;
  3823. debug("N%d.LMC%d.R%d: Setting WLEVEL_CTL[rtt_nom] to %d (%d)\n",
  3824. node, if_num, rankx, rank_nom,
  3825. imp_val->rtt_nom_ohms[rank_nom]);
  3826. }
  3827. memset(wl_bytes, 0, sizeof(wl_bytes));
  3828. memset(wl_bytes_extra, 0, sizeof(wl_bytes_extra));
  3829. // restructure the looping so we can keep trying until we get the
  3830. // samples we want
  3831. while (wloop < wl_loops) {
  3832. wl_ctl.u64 = lmc_rd(priv, CVMX_LMCX_WLEVEL_CTL(if_num));
  3833. wl_ctl.cn78xx.rtt_nom =
  3834. (default_wl_rtt_nom > 0) ? (default_wl_rtt_nom - 1) : 7;
  3835. if (match_wl_rtt_nom) {
  3836. wl_ctl.cn78xx.rtt_nom =
  3837. (rank_nom > 0) ? (rank_nom - 1) : 7;
  3838. }
  3839. /* Clear write-level delays */
  3840. lmc_wr(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num), 0);
  3841. wl_mask_err = 0; /* Reset error counters */
  3842. wl_val_err = 0;
  3843. for (byte_idx = 0; byte_idx < 9; ++byte_idx)
  3844. wl_mask[byte_idx] = 0; /* Reset bitmasks */
  3845. // do all the byte-lanes at the same time
  3846. wl_ctl.cn78xx.lanemask = 0x1ff;
  3847. lmc_wr(priv, CVMX_LMCX_WLEVEL_CTL(if_num), wl_ctl.u64);
  3848. /*
  3849. * Read and write values back in order to update the
  3850. * status field. This insures that we read the updated
  3851. * values after write-leveling has completed.
  3852. */
  3853. lmc_wr(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num),
  3854. lmc_rd(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num)));
  3855. /* write-leveling */
  3856. oct3_ddr3_seq(priv, 1 << rankx, if_num, 6);
  3857. do {
  3858. wl_rank.u64 = lmc_rd(priv,
  3859. CVMX_LMCX_WLEVEL_RANKX(rankx,
  3860. if_num));
  3861. } while (wl_rank.cn78xx.status != 3);
  3862. wl_rank.u64 = lmc_rd(priv, CVMX_LMCX_WLEVEL_RANKX(rankx,
  3863. if_num));
  3864. for (byte_idx = 0; byte_idx < (8 + ecc_ena); ++byte_idx) {
  3865. wl_mask[byte_idx] = lmc_ddr3_wl_dbg_read(priv,
  3866. if_num,
  3867. byte_idx);
  3868. if (wl_mask[byte_idx] == 0)
  3869. ++wl_mask_err;
  3870. }
  3871. // check validity only if no bitmask errors
  3872. if (wl_mask_err == 0) {
  3873. if ((spd_dimm_type == 1 || spd_dimm_type == 2) &&
  3874. dram_width != 16 && if_64b &&
  3875. !disable_hwl_validity) {
  3876. // bypass if [mini|SO]-[RU]DIMM or x16 or
  3877. // 32-bit
  3878. wl_val_err =
  3879. validate_hw_wl_settings(if_num,
  3880. &wl_rank,
  3881. spd_rdimm, ecc_ena);
  3882. wl_val_err_rank += (wl_val_err != 0);
  3883. }
  3884. } else {
  3885. wl_mask_err_rank++;
  3886. }
  3887. // before we print, if we had bitmask or validity errors,
  3888. // do a retry...
  3889. if (wl_mask_err != 0 || wl_val_err != 0) {
  3890. if (wloop_retries < WLOOP_RETRIES_DEFAULT) {
  3891. wloop_retries++;
  3892. wloop_retries_total++;
  3893. // this printout is per-retry: only when VBL
  3894. // is high enough (DEV?)
  3895. // FIXME: do we want to show the bad bitmaps
  3896. // or delays here also?
  3897. debug("N%d.LMC%d.R%d: H/W Write-Leveling had %s errors - retrying...\n",
  3898. node, if_num, rankx,
  3899. (wl_mask_err) ? "Bitmask" : "Validity");
  3900. // this takes us back to the top without
  3901. // counting a sample
  3902. return;
  3903. }
  3904. // retries exhausted, do not print at normal VBL
  3905. debug("N%d.LMC%d.R%d: H/W Write-Leveling issues: %s errors\n",
  3906. node, if_num, rankx,
  3907. (wl_mask_err) ? "Bitmask" : "Validity");
  3908. wloop_retries_exhausted++;
  3909. }
  3910. // no errors or exhausted retries, use this sample
  3911. wloop_retries = 0; //reset for next sample
  3912. // when only 1 sample or forced, print the bitmasks then
  3913. // current HW WL
  3914. if (wl_loops == 1 || wl_print) {
  3915. if (wl_print > 1)
  3916. display_wl_bm(if_num, rankx, wl_mask);
  3917. display_wl(if_num, wl_rank, rankx);
  3918. }
  3919. if (wl_roundup) { /* Round up odd bitmask delays */
  3920. for (byte_idx = 0; byte_idx < (8 + ecc_ena);
  3921. ++byte_idx) {
  3922. if (!(if_bytemask & (1 << byte_idx)))
  3923. return;
  3924. upd_wl_rank(&wl_rank, byte_idx,
  3925. roundup_ddr3_wlevel_bitmask
  3926. (wl_mask[byte_idx]));
  3927. }
  3928. lmc_wr(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num),
  3929. wl_rank.u64);
  3930. display_wl(if_num, wl_rank, rankx);
  3931. }
  3932. // OK, we have a decent sample, no bitmask or validity errors
  3933. extra_bumps = 0;
  3934. extra_mask = 0;
  3935. for (byte_idx = 0; byte_idx < (8 + ecc_ena); ++byte_idx) {
  3936. int ix;
  3937. if (!(if_bytemask & (1 << byte_idx)))
  3938. return;
  3939. // increment count of byte-lane value
  3940. // only 4 values
  3941. ix = (get_wl_rank(&wl_rank, byte_idx) >> 1) & 3;
  3942. wl_bytes[byte_idx].bitcnt[ix]++;
  3943. wl_bytes_extra[byte_idx].bitcnt[ix]++;
  3944. // if perfect...
  3945. if (__builtin_popcount(wl_mask[byte_idx]) == 4) {
  3946. wl_bytes_extra[byte_idx].bitcnt[ix] +=
  3947. wl_pbm_pump;
  3948. extra_bumps++;
  3949. extra_mask |= 1 << byte_idx;
  3950. }
  3951. }
  3952. if (extra_bumps) {
  3953. if (wl_print > 1) {
  3954. debug("N%d.LMC%d.R%d: HWL sample had %d bumps (0x%02x).\n",
  3955. node, if_num, rankx, extra_bumps,
  3956. extra_mask);
  3957. }
  3958. }
  3959. // if we get here, we have taken a decent sample
  3960. wloop++;
  3961. } /* while (wloop < wl_loops) */
  3962. // if we did sample more than once, try to pick a majority vote
  3963. if (wl_loops > 1) {
  3964. // look for the majority in each byte-lane
  3965. for (byte_idx = 0; byte_idx < (8 + ecc_ena); ++byte_idx) {
  3966. int mx, mc, xc, cc;
  3967. int ix, alts;
  3968. int maj, xmaj, xmx, xmc, xxc, xcc;
  3969. if (!(if_bytemask & (1 << byte_idx)))
  3970. return;
  3971. maj = find_wl_majority(&wl_bytes[byte_idx], &mx,
  3972. &mc, &xc, &cc);
  3973. xmaj = find_wl_majority(&wl_bytes_extra[byte_idx],
  3974. &xmx, &xmc, &xxc, &xcc);
  3975. if (maj != xmaj) {
  3976. if (wl_print) {
  3977. debug("N%d.LMC%d.R%d: Byte %d: HWL maj %d(%d), USING xmaj %d(%d)\n",
  3978. node, if_num, rankx,
  3979. byte_idx, maj, xc, xmaj, xxc);
  3980. }
  3981. mx = xmx;
  3982. mc = xmc;
  3983. xc = xxc;
  3984. cc = xcc;
  3985. }
  3986. // see if there was an alternate
  3987. // take out the majority choice
  3988. alts = (mc & ~(1 << mx));
  3989. if (alts != 0) {
  3990. for (ix = 0; ix < 4; ix++) {
  3991. // FIXME: could be done multiple times?
  3992. // bad if so
  3993. if (alts & (1 << ix)) {
  3994. // set the mask
  3995. hwl_alts[rankx].hwl_alt_mask |=
  3996. (1 << byte_idx);
  3997. // record the value
  3998. hwl_alts[rankx].hwl_alt_delay[byte_idx] =
  3999. ix << 1;
  4000. if (wl_print > 1) {
  4001. debug("N%d.LMC%d.R%d: SWL_TRY_HWL_ALT: Byte %d maj %d (%d) alt %d (%d).\n",
  4002. node,
  4003. if_num,
  4004. rankx,
  4005. byte_idx,
  4006. mx << 1,
  4007. xc,
  4008. ix << 1,
  4009. wl_bytes
  4010. [byte_idx].bitcnt
  4011. [ix]);
  4012. }
  4013. }
  4014. }
  4015. }
  4016. if (cc > 2) { // unlikely, but...
  4017. // assume: counts for 3 indices are all 1
  4018. // possiblities are: 0/2/4, 2/4/6, 0/4/6, 0/2/6
  4019. // and the desired?: 2 , 4 , 6, 0
  4020. // we choose the middle, assuming one of the
  4021. // outliers is bad
  4022. // NOTE: this is an ugly hack at the moment;
  4023. // there must be a better way
  4024. switch (mc) {
  4025. case 0x7:
  4026. mx = 1;
  4027. break; // was 0/2/4, choose 2
  4028. case 0xb:
  4029. mx = 0;
  4030. break; // was 0/2/6, choose 0
  4031. case 0xd:
  4032. mx = 3;
  4033. break; // was 0/4/6, choose 6
  4034. case 0xe:
  4035. mx = 2;
  4036. break; // was 2/4/6, choose 4
  4037. default:
  4038. case 0xf:
  4039. mx = 1;
  4040. break; // was 0/2/4/6, choose 2?
  4041. }
  4042. printf("N%d.LMC%d.R%d: HW WL MAJORITY: bad byte-lane %d (0x%x), using %d.\n",
  4043. node, if_num, rankx, byte_idx, mc,
  4044. mx << 1);
  4045. }
  4046. upd_wl_rank(&wl_rank, byte_idx, mx << 1);
  4047. }
  4048. lmc_wr(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num),
  4049. wl_rank.u64);
  4050. display_wl_with_final(if_num, wl_rank, rankx);
  4051. // FIXME: does this help make the output a little easier
  4052. // to focus?
  4053. if (wl_print > 0)
  4054. debug("-----------\n");
  4055. } /* if (wl_loops > 1) */
  4056. // maybe print an error summary for the rank
  4057. if (wl_mask_err_rank != 0 || wl_val_err_rank != 0) {
  4058. debug("N%d.LMC%d.R%d: H/W Write-Leveling errors - %d bitmask, %d validity, %d retries, %d exhausted\n",
  4059. node, if_num, rankx, wl_mask_err_rank,
  4060. wl_val_err_rank, wloop_retries_total,
  4061. wloop_retries_exhausted);
  4062. }
  4063. }
  4064. static void lmc_write_leveling(struct ddr_priv *priv)
  4065. {
  4066. union cvmx_lmcx_config cfg;
  4067. int rankx;
  4068. char *s;
  4069. /*
  4070. * 4.8.9 LMC Write Leveling
  4071. *
  4072. * LMC supports an automatic write leveling like that described in the
  4073. * JEDEC DDR3 specifications separately per byte-lane.
  4074. *
  4075. * All of DDR PLL, LMC CK, LMC DRESET, and early LMC initializations
  4076. * must be completed prior to starting this LMC write-leveling sequence.
  4077. *
  4078. * There are many possible procedures that will write-level all the
  4079. * attached DDR3 DRAM parts. One possibility is for software to simply
  4080. * write the desired values into LMC(0)_WLEVEL_RANK(0..3). This section
  4081. * describes one possible sequence that uses LMC's autowrite-leveling
  4082. * capabilities.
  4083. *
  4084. * 1. If the DQS/DQ delays on the board may be more than the ADD/CMD
  4085. * delays, then ensure that LMC(0)_CONFIG[EARLY_DQX] is set at this
  4086. * point.
  4087. *
  4088. * Do the remaining steps 2-7 separately for each rank i with attached
  4089. * DRAM.
  4090. *
  4091. * 2. Write LMC(0)_WLEVEL_RANKi = 0.
  4092. *
  4093. * 3. For x8 parts:
  4094. *
  4095. * Without changing any other fields in LMC(0)_WLEVEL_CTL, write
  4096. * LMC(0)_WLEVEL_CTL[LANEMASK] to select all byte lanes with attached
  4097. * DRAM.
  4098. *
  4099. * For x16 parts:
  4100. *
  4101. * Without changing any other fields in LMC(0)_WLEVEL_CTL, write
  4102. * LMC(0)_WLEVEL_CTL[LANEMASK] to select all even byte lanes with
  4103. * attached DRAM.
  4104. *
  4105. * 4. Without changing any other fields in LMC(0)_CONFIG,
  4106. *
  4107. * o write LMC(0)_SEQ_CTL[SEQ_SEL] to select write-leveling
  4108. *
  4109. * o write LMC(0)_CONFIG[RANKMASK] = (1 << i)
  4110. *
  4111. * o write LMC(0)_SEQ_CTL[INIT_START] = 1
  4112. *
  4113. * LMC will initiate write-leveling at this point. Assuming
  4114. * LMC(0)_WLEVEL_CTL [SSET] = 0, LMC first enables write-leveling on
  4115. * the selected DRAM rank via a DDR3 MR1 write, then sequences
  4116. * through
  4117. * and accumulates write-leveling results for eight different delay
  4118. * settings twice, starting at a delay of zero in this case since
  4119. * LMC(0)_WLEVEL_RANKi[BYTE*<4:3>] = 0, increasing by 1/8 CK each
  4120. * setting, covering a total distance of one CK, then disables the
  4121. * write-leveling via another DDR3 MR1 write.
  4122. *
  4123. * After the sequence through 16 delay settings is complete:
  4124. *
  4125. * o LMC sets LMC(0)_WLEVEL_RANKi[STATUS] = 3
  4126. *
  4127. * o LMC sets LMC(0)_WLEVEL_RANKi[BYTE*<2:0>] (for all ranks selected
  4128. * by LMC(0)_WLEVEL_CTL[LANEMASK]) to indicate the first write
  4129. * leveling result of 1 that followed result of 0 during the
  4130. * sequence, except that the LMC always writes
  4131. * LMC(0)_WLEVEL_RANKi[BYTE*<0>]=0.
  4132. *
  4133. * o Software can read the eight write-leveling results from the
  4134. * first pass through the delay settings by reading
  4135. * LMC(0)_WLEVEL_DBG[BITMASK] (after writing
  4136. * LMC(0)_WLEVEL_DBG[BYTE]). (LMC does not retain the writeleveling
  4137. * results from the second pass through the eight delay
  4138. * settings. They should often be identical to the
  4139. * LMC(0)_WLEVEL_DBG[BITMASK] results, though.)
  4140. *
  4141. * 5. Wait until LMC(0)_WLEVEL_RANKi[STATUS] != 2.
  4142. *
  4143. * LMC will have updated LMC(0)_WLEVEL_RANKi[BYTE*<2:0>] for all byte
  4144. * lanes selected by LMC(0)_WLEVEL_CTL[LANEMASK] at this point.
  4145. * LMC(0)_WLEVEL_RANKi[BYTE*<4:3>] will still be the value that
  4146. * software wrote in substep 2 above, which is 0.
  4147. *
  4148. * 6. For x16 parts:
  4149. *
  4150. * Without changing any other fields in LMC(0)_WLEVEL_CTL, write
  4151. * LMC(0)_WLEVEL_CTL[LANEMASK] to select all odd byte lanes with
  4152. * attached DRAM.
  4153. *
  4154. * Repeat substeps 4 and 5 with this new LMC(0)_WLEVEL_CTL[LANEMASK]
  4155. * setting. Skip to substep 7 if this has already been done.
  4156. *
  4157. * For x8 parts:
  4158. *
  4159. * Skip this substep. Go to substep 7.
  4160. *
  4161. * 7. Calculate LMC(0)_WLEVEL_RANKi[BYTE*<4:3>] settings for all byte
  4162. * lanes on all ranks with attached DRAM.
  4163. *
  4164. * At this point, all byte lanes on rank i with attached DRAM should
  4165. * have been write-leveled, and LMC(0)_WLEVEL_RANKi[BYTE*<2:0>] has
  4166. * the result for each byte lane.
  4167. *
  4168. * But note that the DDR3 write-leveling sequence will only determine
  4169. * the delay modulo the CK cycle time, and cannot determine how many
  4170. * additional CK cycles of delay are present. Software must calculate
  4171. * the number of CK cycles, or equivalently, the
  4172. * LMC(0)_WLEVEL_RANKi[BYTE*<4:3>] settings.
  4173. *
  4174. * This BYTE*<4:3> calculation is system/board specific.
  4175. *
  4176. * Many techniques can be used to calculate write-leveling BYTE*<4:3>
  4177. * values, including:
  4178. *
  4179. * o Known values for some byte lanes.
  4180. *
  4181. * o Relative values for some byte lanes relative to others.
  4182. *
  4183. * For example, suppose lane X is likely to require a larger
  4184. * write-leveling delay than lane Y. A BYTEX<2:0> value that is much
  4185. * smaller than the BYTEY<2:0> value may then indicate that the
  4186. * required lane X delay wrapped into the next CK, so BYTEX<4:3>
  4187. * should be set to BYTEY<4:3>+1.
  4188. *
  4189. * When ECC DRAM is not present (i.e. when DRAM is not attached to
  4190. * the DDR_CBS_0_* and DDR_CB<7:0> chip signals, or the
  4191. * DDR_DQS_<4>_* and DDR_DQ<35:32> chip signals), write
  4192. * LMC(0)_WLEVEL_RANK*[BYTE8] = LMC(0)_WLEVEL_RANK*[BYTE0],
  4193. * using the final calculated BYTE0 value.
  4194. * Write LMC(0)_WLEVEL_RANK*[BYTE4] = LMC(0)_WLEVEL_RANK*[BYTE0],
  4195. * using the final calculated BYTE0 value.
  4196. *
  4197. * 8. Initialize LMC(0)_WLEVEL_RANK* values for all unused ranks.
  4198. *
  4199. * Let rank i be a rank with attached DRAM.
  4200. *
  4201. * For all ranks j that do not have attached DRAM, set
  4202. * LMC(0)_WLEVEL_RANKj = LMC(0)_WLEVEL_RANKi.
  4203. */
  4204. rankx = 0;
  4205. wl_roundup = 0;
  4206. disable_hwl_validity = 0;
  4207. // wl_pbm_pump: weight for write-leveling PBMs...
  4208. // 0 causes original behavior
  4209. // 1 allows a minority of 2 pbms to outscore a majority of 3 non-pbms
  4210. // 4 would allow a minority of 1 pbm to outscore a majority of 4
  4211. // non-pbms
  4212. wl_pbm_pump = 4; // FIXME: is 4 too much?
  4213. if (wl_loops) {
  4214. debug("N%d.LMC%d: Performing Hardware Write-Leveling\n", node,
  4215. if_num);
  4216. } else {
  4217. /* Force software write-leveling to run */
  4218. wl_mask_err = 1;
  4219. debug("N%d.LMC%d: Forcing software Write-Leveling\n", node,
  4220. if_num);
  4221. }
  4222. default_wl_rtt_nom = (ddr_type == DDR3_DRAM) ?
  4223. rttnom_20ohm : ddr4_rttnom_40ohm;
  4224. cfg.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  4225. ecc_ena = cfg.s.ecc_ena;
  4226. save_mode32b = cfg.cn78xx.mode32b;
  4227. cfg.cn78xx.mode32b = (!if_64b);
  4228. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), cfg.u64);
  4229. debug("%-45s : %d\n", "MODE32B", cfg.cn78xx.mode32b);
  4230. s = lookup_env(priv, "ddr_wlevel_roundup");
  4231. if (s)
  4232. wl_roundup = simple_strtoul(s, NULL, 0);
  4233. s = lookup_env(priv, "ddr_wlevel_printall");
  4234. if (s)
  4235. wl_print = strtoul(s, NULL, 0);
  4236. s = lookup_env(priv, "ddr_wlevel_pbm_bump");
  4237. if (s)
  4238. wl_pbm_pump = strtoul(s, NULL, 0);
  4239. // default to disable when RL sequential delay check is disabled
  4240. disable_hwl_validity = disable_sequential_delay_check;
  4241. s = lookup_env(priv, "ddr_disable_hwl_validity");
  4242. if (s)
  4243. disable_hwl_validity = !!strtoul(s, NULL, 0);
  4244. s = lookup_env(priv, "ddr_wl_rtt_nom");
  4245. if (s)
  4246. default_wl_rtt_nom = simple_strtoul(s, NULL, 0);
  4247. s = lookup_env(priv, "ddr_match_wl_rtt_nom");
  4248. if (s)
  4249. match_wl_rtt_nom = !!simple_strtoul(s, NULL, 0);
  4250. if (match_wl_rtt_nom)
  4251. mp1.u64 = lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS1(if_num));
  4252. // For DDR3, we do not touch WLEVEL_CTL fields OR_DIS or BITMASK
  4253. // For DDR4, we touch WLEVEL_CTL fields OR_DIS or BITMASK here
  4254. if (ddr_type == DDR4_DRAM) {
  4255. int default_or_dis = 1;
  4256. int default_bitmask = 0xff;
  4257. // when x4, use only the lower nibble
  4258. if (dram_width == 4) {
  4259. default_bitmask = 0x0f;
  4260. if (wl_print) {
  4261. debug("N%d.LMC%d: WLEVEL_CTL: default bitmask is 0x%02x for DDR4 x4\n",
  4262. node, if_num, default_bitmask);
  4263. }
  4264. }
  4265. wl_ctl.u64 = lmc_rd(priv, CVMX_LMCX_WLEVEL_CTL(if_num));
  4266. wl_ctl.s.or_dis = default_or_dis;
  4267. wl_ctl.s.bitmask = default_bitmask;
  4268. // allow overrides
  4269. s = lookup_env(priv, "ddr_wlevel_ctl_or_dis");
  4270. if (s)
  4271. wl_ctl.s.or_dis = !!strtoul(s, NULL, 0);
  4272. s = lookup_env(priv, "ddr_wlevel_ctl_bitmask");
  4273. if (s)
  4274. wl_ctl.s.bitmask = simple_strtoul(s, NULL, 0);
  4275. // print only if not defaults
  4276. if (wl_ctl.s.or_dis != default_or_dis ||
  4277. wl_ctl.s.bitmask != default_bitmask) {
  4278. debug("N%d.LMC%d: WLEVEL_CTL: or_dis=%d, bitmask=0x%02x\n",
  4279. node, if_num, wl_ctl.s.or_dis, wl_ctl.s.bitmask);
  4280. }
  4281. // always write
  4282. lmc_wr(priv, CVMX_LMCX_WLEVEL_CTL(if_num), wl_ctl.u64);
  4283. }
  4284. // Start the hardware write-leveling loop per rank
  4285. for (rankx = 0; rankx < dimm_count * 4; rankx++)
  4286. lmc_write_leveling_loop(priv, rankx);
  4287. cfg.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  4288. cfg.cn78xx.mode32b = save_mode32b;
  4289. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), cfg.u64);
  4290. debug("%-45s : %d\n", "MODE32B", cfg.cn78xx.mode32b);
  4291. // At the end of HW Write Leveling, check on some DESKEW things...
  4292. if (!disable_deskew_training) {
  4293. struct deskew_counts dsk_counts;
  4294. int retry_count = 0;
  4295. debug("N%d.LMC%d: Check Deskew Settings before Read-Leveling.\n",
  4296. node, if_num);
  4297. do {
  4298. validate_deskew_training(priv, rank_mask, if_num,
  4299. &dsk_counts, 1);
  4300. // only RAWCARD A or B will not benefit from
  4301. // retraining if there's only saturation
  4302. // or any rawcard if there is a nibble error
  4303. if ((!spd_rawcard_aorb && dsk_counts.saturated > 0) ||
  4304. (dsk_counts.nibrng_errs != 0 ||
  4305. dsk_counts.nibunl_errs != 0)) {
  4306. retry_count++;
  4307. debug("N%d.LMC%d: Deskew Status indicates saturation or nibble errors - retry %d Training.\n",
  4308. node, if_num, retry_count);
  4309. perform_deskew_training(priv, rank_mask, if_num,
  4310. spd_rawcard_aorb);
  4311. } else {
  4312. break;
  4313. }
  4314. } while (retry_count < 5);
  4315. }
  4316. }
  4317. static void lmc_workaround(struct ddr_priv *priv)
  4318. {
  4319. /* Workaround Trcd overflow by using Additive latency. */
  4320. if (octeon_is_cpuid(OCTEON_CN78XX_PASS1_X)) {
  4321. union cvmx_lmcx_modereg_params0 mp0;
  4322. union cvmx_lmcx_timing_params1 tp1;
  4323. union cvmx_lmcx_control ctrl;
  4324. int rankx;
  4325. tp1.u64 = lmc_rd(priv, CVMX_LMCX_TIMING_PARAMS1(if_num));
  4326. mp0.u64 = lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num));
  4327. ctrl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  4328. if (tp1.cn78xx.trcd == 0) {
  4329. debug("Workaround Trcd overflow by using Additive latency.\n");
  4330. /* Hard code this to 12 and enable additive latency */
  4331. tp1.cn78xx.trcd = 12;
  4332. mp0.s.al = 2; /* CL-2 */
  4333. ctrl.s.pocas = 1;
  4334. debug("MODEREG_PARAMS0 : 0x%016llx\n",
  4335. mp0.u64);
  4336. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num),
  4337. mp0.u64);
  4338. debug("TIMING_PARAMS1 : 0x%016llx\n",
  4339. tp1.u64);
  4340. lmc_wr(priv, CVMX_LMCX_TIMING_PARAMS1(if_num), tp1.u64);
  4341. debug("LMC_CONTROL : 0x%016llx\n",
  4342. ctrl.u64);
  4343. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctrl.u64);
  4344. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  4345. if (!(rank_mask & (1 << rankx)))
  4346. continue;
  4347. /* MR1 */
  4348. ddr4_mrw(priv, if_num, rankx, -1, 1, 0);
  4349. }
  4350. }
  4351. }
  4352. // this is here just for output, to allow check of the Deskew
  4353. // settings one last time...
  4354. if (!disable_deskew_training) {
  4355. struct deskew_counts dsk_counts;
  4356. debug("N%d.LMC%d: Check Deskew Settings before software Write-Leveling.\n",
  4357. node, if_num);
  4358. validate_deskew_training(priv, rank_mask, if_num, &dsk_counts,
  4359. 3);
  4360. }
  4361. /*
  4362. * Workaround Errata 26304 (T88@2.0, O75@1.x, O78@2.x)
  4363. *
  4364. * When the CSRs LMCX_DLL_CTL3[WR_DESKEW_ENA] = 1 AND
  4365. * LMCX_PHY_CTL2[DQS[0..8]_DSK_ADJ] > 4, set
  4366. * LMCX_EXT_CONFIG[DRIVE_ENA_BPRCH] = 1.
  4367. */
  4368. if (octeon_is_cpuid(OCTEON_CN78XX_PASS2_X) ||
  4369. octeon_is_cpuid(OCTEON_CNF75XX_PASS1_X)) {
  4370. union cvmx_lmcx_dll_ctl3 dll_ctl3;
  4371. union cvmx_lmcx_phy_ctl2 phy_ctl2;
  4372. union cvmx_lmcx_ext_config ext_cfg;
  4373. int increased_dsk_adj = 0;
  4374. int byte;
  4375. phy_ctl2.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL2(if_num));
  4376. ext_cfg.u64 = lmc_rd(priv, CVMX_LMCX_EXT_CONFIG(if_num));
  4377. dll_ctl3.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL3(if_num));
  4378. for (byte = 0; byte < 8; ++byte) {
  4379. if (!(if_bytemask & (1 << byte)))
  4380. continue;
  4381. increased_dsk_adj |=
  4382. (((phy_ctl2.u64 >> (byte * 3)) & 0x7) > 4);
  4383. }
  4384. if (dll_ctl3.s.wr_deskew_ena == 1 && increased_dsk_adj) {
  4385. ext_cfg.s.drive_ena_bprch = 1;
  4386. lmc_wr(priv, CVMX_LMCX_EXT_CONFIG(if_num), ext_cfg.u64);
  4387. debug("LMC%d: Forcing DRIVE_ENA_BPRCH for Workaround Errata 26304.\n",
  4388. if_num);
  4389. }
  4390. }
  4391. }
  4392. // Software Write-Leveling block
  4393. #define VREF_RANGE1_LIMIT 0x33 // range1 is valid for 0x00 - 0x32
  4394. #define VREF_RANGE2_LIMIT 0x18 // range2 is valid for 0x00 - 0x17
  4395. // full window is valid for 0x00 to 0x4A
  4396. // let 0x00 - 0x17 be range2, 0x18 - 0x4a be range 1
  4397. #define VREF_LIMIT (VREF_RANGE1_LIMIT + VREF_RANGE2_LIMIT)
  4398. #define VREF_FINAL (VREF_LIMIT - 1)
  4399. enum sw_wl_status {
  4400. WL_ESTIMATED = 0, /* HW/SW wleveling failed. Reslt estimated */
  4401. WL_HARDWARE = 1, /* H/W wleveling succeeded */
  4402. WL_SOFTWARE = 2, /* S/W wleveling passed 2 contiguous setting */
  4403. WL_SOFTWARE1 = 3, /* S/W wleveling passed 1 marginal setting */
  4404. };
  4405. static u64 rank_addr __section(".data");
  4406. static int vref_val __section(".data");
  4407. static int final_vref_val __section(".data");
  4408. static int final_vref_range __section(".data");
  4409. static int start_vref_val __section(".data");
  4410. static int computed_final_vref_val __section(".data");
  4411. static char best_vref_val_count __section(".data");
  4412. static char vref_val_count __section(".data");
  4413. static char best_vref_val_start __section(".data");
  4414. static char vref_val_start __section(".data");
  4415. static int bytes_failed __section(".data");
  4416. static enum sw_wl_status byte_test_status[9] __section(".data");
  4417. static enum sw_wl_status sw_wl_rank_status __section(".data");
  4418. static int sw_wl_failed __section(".data");
  4419. static int sw_wl_hw __section(".data");
  4420. static int measured_vref_flag __section(".data");
  4421. static void ddr4_vref_loop(struct ddr_priv *priv, int rankx)
  4422. {
  4423. char *s;
  4424. if (vref_val < VREF_FINAL) {
  4425. int vrange, vvalue;
  4426. if (vref_val < VREF_RANGE2_LIMIT) {
  4427. vrange = 1;
  4428. vvalue = vref_val;
  4429. } else {
  4430. vrange = 0;
  4431. vvalue = vref_val - VREF_RANGE2_LIMIT;
  4432. }
  4433. set_vref(priv, if_num, rankx, vrange, vvalue);
  4434. } else { /* if (vref_val < VREF_FINAL) */
  4435. /* Print the final vref value first. */
  4436. /* Always print the computed first if its valid */
  4437. if (computed_final_vref_val >= 0) {
  4438. debug("N%d.LMC%d.R%d: vref Computed Summary : %2d (0x%02x)\n",
  4439. node, if_num, rankx,
  4440. computed_final_vref_val, computed_final_vref_val);
  4441. }
  4442. if (!measured_vref_flag) { // setup to use the computed
  4443. best_vref_val_count = 1;
  4444. final_vref_val = computed_final_vref_val;
  4445. } else { // setup to use the measured
  4446. if (best_vref_val_count > 0) {
  4447. best_vref_val_count =
  4448. max(best_vref_val_count, (char)2);
  4449. final_vref_val = best_vref_val_start +
  4450. divide_nint(best_vref_val_count - 1, 2);
  4451. if (final_vref_val < VREF_RANGE2_LIMIT) {
  4452. final_vref_range = 1;
  4453. } else {
  4454. final_vref_range = 0;
  4455. final_vref_val -= VREF_RANGE2_LIMIT;
  4456. }
  4457. int vvlo = best_vref_val_start;
  4458. int vrlo;
  4459. int vvhi = best_vref_val_start +
  4460. best_vref_val_count - 1;
  4461. int vrhi;
  4462. if (vvlo < VREF_RANGE2_LIMIT) {
  4463. vrlo = 2;
  4464. } else {
  4465. vrlo = 1;
  4466. vvlo -= VREF_RANGE2_LIMIT;
  4467. }
  4468. if (vvhi < VREF_RANGE2_LIMIT) {
  4469. vrhi = 2;
  4470. } else {
  4471. vrhi = 1;
  4472. vvhi -= VREF_RANGE2_LIMIT;
  4473. }
  4474. debug("N%d.LMC%d.R%d: vref Training Summary : 0x%02x/%1d <----- 0x%02x/%1d -----> 0x%02x/%1d, range: %2d\n",
  4475. node, if_num, rankx, vvlo, vrlo,
  4476. final_vref_val,
  4477. final_vref_range + 1, vvhi, vrhi,
  4478. best_vref_val_count - 1);
  4479. } else {
  4480. /*
  4481. * If nothing passed use the default vref
  4482. * value for this rank
  4483. */
  4484. union cvmx_lmcx_modereg_params2 mp2;
  4485. mp2.u64 =
  4486. lmc_rd(priv,
  4487. CVMX_LMCX_MODEREG_PARAMS2(if_num));
  4488. final_vref_val = (mp2.u64 >>
  4489. (rankx * 10 + 3)) & 0x3f;
  4490. final_vref_range = (mp2.u64 >>
  4491. (rankx * 10 + 9)) & 0x01;
  4492. debug("N%d.LMC%d.R%d: vref Using Default : %2d <----- %2d (0x%02x) -----> %2d, range%1d\n",
  4493. node, if_num, rankx, final_vref_val,
  4494. final_vref_val, final_vref_val,
  4495. final_vref_val, final_vref_range + 1);
  4496. }
  4497. }
  4498. // allow override
  4499. s = lookup_env(priv, "ddr%d_vref_val_%1d%1d",
  4500. if_num, !!(rankx & 2), !!(rankx & 1));
  4501. if (s)
  4502. final_vref_val = strtoul(s, NULL, 0);
  4503. set_vref(priv, if_num, rankx, final_vref_range, final_vref_val);
  4504. }
  4505. }
  4506. #define WL_MIN_NO_ERRORS_COUNT 3 // FIXME? three passes without errors
  4507. static int errors __section(".data");
  4508. static int byte_delay[9] __section(".data");
  4509. static u64 bytemask __section(".data");
  4510. static int bytes_todo __section(".data");
  4511. static int no_errors_count __section(".data");
  4512. static u64 bad_bits[2] __section(".data");
  4513. static u64 sum_dram_dclk __section(".data");
  4514. static u64 sum_dram_ops __section(".data");
  4515. static u64 start_dram_dclk __section(".data");
  4516. static u64 stop_dram_dclk __section(".data");
  4517. static u64 start_dram_ops __section(".data");
  4518. static u64 stop_dram_ops __section(".data");
  4519. static void lmc_sw_write_leveling_loop(struct ddr_priv *priv, int rankx)
  4520. {
  4521. int delay;
  4522. int b;
  4523. // write the current set of WL delays
  4524. lmc_wr(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num), wl_rank.u64);
  4525. wl_rank.u64 = lmc_rd(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num));
  4526. // do the test
  4527. if (sw_wl_hw) {
  4528. errors = run_best_hw_patterns(priv, if_num, rank_addr,
  4529. DBTRAIN_TEST, bad_bits);
  4530. errors &= bytes_todo; // keep only the ones we are still doing
  4531. } else {
  4532. start_dram_dclk = lmc_rd(priv, CVMX_LMCX_DCLK_CNT(if_num));
  4533. start_dram_ops = lmc_rd(priv, CVMX_LMCX_OPS_CNT(if_num));
  4534. errors = test_dram_byte64(priv, if_num, rank_addr, bytemask,
  4535. bad_bits);
  4536. stop_dram_dclk = lmc_rd(priv, CVMX_LMCX_DCLK_CNT(if_num));
  4537. stop_dram_ops = lmc_rd(priv, CVMX_LMCX_OPS_CNT(if_num));
  4538. sum_dram_dclk += stop_dram_dclk - start_dram_dclk;
  4539. sum_dram_ops += stop_dram_ops - start_dram_ops;
  4540. }
  4541. debug("WL pass1: test_dram_byte returned 0x%x\n", errors);
  4542. // remember, errors will not be returned for byte-lanes that have
  4543. // maxxed out...
  4544. if (errors == 0) {
  4545. no_errors_count++; // bump
  4546. // bypass check/update completely
  4547. if (no_errors_count > 1)
  4548. return; // to end of do-while
  4549. } else {
  4550. no_errors_count = 0; // reset
  4551. }
  4552. // check errors by byte
  4553. for (b = 0; b < 9; ++b) {
  4554. if (!(bytes_todo & (1 << b)))
  4555. continue;
  4556. delay = byte_delay[b];
  4557. // yes, an error in this byte lane
  4558. if (errors & (1 << b)) {
  4559. debug(" byte %d delay %2d Errors\n", b, delay);
  4560. // since this byte had an error, we move to the next
  4561. // delay value, unless done with it
  4562. delay += 8; // incr by 8 to do delay high-order bits
  4563. if (delay < 32) {
  4564. upd_wl_rank(&wl_rank, b, delay);
  4565. debug(" byte %d delay %2d New\n",
  4566. b, delay);
  4567. byte_delay[b] = delay;
  4568. } else {
  4569. // reached max delay, maybe really done with
  4570. // this byte
  4571. // consider an alt only for computed VREF and
  4572. if (!measured_vref_flag &&
  4573. (hwl_alts[rankx].hwl_alt_mask & (1 << b))) {
  4574. // if an alt exists...
  4575. // just orig low-3 bits
  4576. int bad_delay = delay & 0x6;
  4577. // yes, use it
  4578. delay = hwl_alts[rankx].hwl_alt_delay[b];
  4579. // clear that flag
  4580. hwl_alts[rankx].hwl_alt_mask &=
  4581. ~(1 << b);
  4582. upd_wl_rank(&wl_rank, b, delay);
  4583. byte_delay[b] = delay;
  4584. debug(" byte %d delay %2d ALTERNATE\n",
  4585. b, delay);
  4586. debug("N%d.LMC%d.R%d: SWL: Byte %d: %d FAIL, trying ALTERNATE %d\n",
  4587. node, if_num,
  4588. rankx, b, bad_delay, delay);
  4589. } else {
  4590. unsigned int bits_bad;
  4591. if (b < 8) {
  4592. // test no longer, remove from
  4593. // byte mask
  4594. bytemask &=
  4595. ~(0xffULL << (8 * b));
  4596. bits_bad = (unsigned int)
  4597. ((bad_bits[0] >>
  4598. (8 * b)) & 0xffUL);
  4599. } else {
  4600. bits_bad = (unsigned int)
  4601. (bad_bits[1] & 0xffUL);
  4602. }
  4603. // remove from bytes to do
  4604. bytes_todo &= ~(1 << b);
  4605. // make sure this is set for this case
  4606. byte_test_status[b] = WL_ESTIMATED;
  4607. debug(" byte %d delay %2d Exhausted\n",
  4608. b, delay);
  4609. if (!measured_vref_flag) {
  4610. // this is too noisy when doing
  4611. // measured VREF
  4612. debug("N%d.LMC%d.R%d: SWL: Byte %d (0x%02x): delay %d EXHAUSTED\n",
  4613. node, if_num, rankx,
  4614. b, bits_bad, delay);
  4615. }
  4616. }
  4617. }
  4618. } else {
  4619. // no error, stay with current delay, but keep testing
  4620. // it...
  4621. debug(" byte %d delay %2d Passed\n", b, delay);
  4622. byte_test_status[b] = WL_HARDWARE; // change status
  4623. }
  4624. } /* for (b = 0; b < 9; ++b) */
  4625. }
  4626. static void sw_write_lvl_use_ecc(struct ddr_priv *priv, int rankx)
  4627. {
  4628. int save_byte8 = wl_rank.s.byte8;
  4629. byte_test_status[8] = WL_HARDWARE; /* H/W delay value */
  4630. if (save_byte8 != wl_rank.s.byte3 &&
  4631. save_byte8 != wl_rank.s.byte4) {
  4632. int test_byte8 = save_byte8;
  4633. int test_byte8_error;
  4634. int byte8_error = 0x1f;
  4635. int adder;
  4636. int avg_bytes = divide_nint(wl_rank.s.byte3 + wl_rank.s.byte4,
  4637. 2);
  4638. for (adder = 0; adder <= 32; adder += 8) {
  4639. test_byte8_error = abs((adder + save_byte8) -
  4640. avg_bytes);
  4641. if (test_byte8_error < byte8_error) {
  4642. byte8_error = test_byte8_error;
  4643. test_byte8 = save_byte8 + adder;
  4644. }
  4645. }
  4646. // only do the check if we are not using measured VREF
  4647. if (!measured_vref_flag) {
  4648. /* Use only even settings, rounding down... */
  4649. test_byte8 &= ~1;
  4650. // do validity check on the calculated ECC delay value
  4651. // this depends on the DIMM type
  4652. if (spd_rdimm) { // RDIMM
  4653. // but not mini-RDIMM
  4654. if (spd_dimm_type != 5) {
  4655. // it can be > byte4, but should never
  4656. // be > byte3
  4657. if (test_byte8 > wl_rank.s.byte3) {
  4658. /* say it is still estimated */
  4659. byte_test_status[8] =
  4660. WL_ESTIMATED;
  4661. }
  4662. }
  4663. } else { // UDIMM
  4664. if (test_byte8 < wl_rank.s.byte3 ||
  4665. test_byte8 > wl_rank.s.byte4) {
  4666. // should never be outside the
  4667. // byte 3-4 range
  4668. /* say it is still estimated */
  4669. byte_test_status[8] = WL_ESTIMATED;
  4670. }
  4671. }
  4672. /*
  4673. * Report whenever the calculation appears bad.
  4674. * This happens if some of the original values were off,
  4675. * or unexpected geometry from DIMM type, or custom
  4676. * circuitry (NIC225E, I am looking at you!).
  4677. * We will trust the calculated value, and depend on
  4678. * later testing to catch any instances when that
  4679. * value is truly bad.
  4680. */
  4681. // ESTIMATED means there may be an issue
  4682. if (byte_test_status[8] == WL_ESTIMATED) {
  4683. debug("N%d.LMC%d.R%d: SWL: (%cDIMM): calculated ECC delay unexpected (%d/%d/%d)\n",
  4684. node, if_num, rankx,
  4685. (spd_rdimm ? 'R' : 'U'), wl_rank.s.byte4,
  4686. test_byte8, wl_rank.s.byte3);
  4687. byte_test_status[8] = WL_HARDWARE;
  4688. }
  4689. }
  4690. /* Use only even settings */
  4691. wl_rank.s.byte8 = test_byte8 & ~1;
  4692. }
  4693. if (wl_rank.s.byte8 != save_byte8) {
  4694. /* Change the status if s/w adjusted the delay */
  4695. byte_test_status[8] = WL_SOFTWARE; /* Estimated delay */
  4696. }
  4697. }
  4698. static __maybe_unused void parallel_wl_block_delay(struct ddr_priv *priv,
  4699. int rankx)
  4700. {
  4701. int errors;
  4702. int byte_delay[8];
  4703. int byte_passed[8];
  4704. u64 bytemask;
  4705. u64 bitmask;
  4706. int wl_offset;
  4707. int bytes_todo;
  4708. int sw_wl_offset = 1;
  4709. int delay;
  4710. int b;
  4711. for (b = 0; b < 8; ++b)
  4712. byte_passed[b] = 0;
  4713. bytes_todo = if_bytemask;
  4714. for (wl_offset = sw_wl_offset; wl_offset >= 0; --wl_offset) {
  4715. debug("Starting wl_offset for-loop: %d\n", wl_offset);
  4716. bytemask = 0;
  4717. for (b = 0; b < 8; ++b) {
  4718. byte_delay[b] = 0;
  4719. // this does not contain fully passed bytes
  4720. if (!(bytes_todo & (1 << b)))
  4721. continue;
  4722. // reset across passes if not fully passed
  4723. byte_passed[b] = 0;
  4724. upd_wl_rank(&wl_rank, b, 0); // all delays start at 0
  4725. bitmask = ((!if_64b) && (b == 4)) ? 0x0f : 0xff;
  4726. // set the bytes bits in the bytemask
  4727. bytemask |= bitmask << (8 * b);
  4728. } /* for (b = 0; b < 8; ++b) */
  4729. // start a pass if there is any byte lane to test
  4730. while (bytemask != 0) {
  4731. debug("Starting bytemask while-loop: 0x%llx\n",
  4732. bytemask);
  4733. // write this set of WL delays
  4734. lmc_wr(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num),
  4735. wl_rank.u64);
  4736. wl_rank.u64 = lmc_rd(priv,
  4737. CVMX_LMCX_WLEVEL_RANKX(rankx,
  4738. if_num));
  4739. // do the test
  4740. if (sw_wl_hw) {
  4741. errors = run_best_hw_patterns(priv, if_num,
  4742. rank_addr,
  4743. DBTRAIN_TEST,
  4744. NULL) & 0xff;
  4745. } else {
  4746. errors = test_dram_byte64(priv, if_num,
  4747. rank_addr, bytemask,
  4748. NULL);
  4749. }
  4750. debug("test_dram_byte returned 0x%x\n", errors);
  4751. // check errors by byte
  4752. for (b = 0; b < 8; ++b) {
  4753. if (!(bytes_todo & (1 << b)))
  4754. continue;
  4755. delay = byte_delay[b];
  4756. if (errors & (1 << b)) { // yes, an error
  4757. debug(" byte %d delay %2d Errors\n",
  4758. b, delay);
  4759. byte_passed[b] = 0;
  4760. } else { // no error
  4761. byte_passed[b] += 1;
  4762. // Look for consecutive working settings
  4763. if (byte_passed[b] == (1 + wl_offset)) {
  4764. debug(" byte %d delay %2d FULLY Passed\n",
  4765. b, delay);
  4766. if (wl_offset == 1) {
  4767. byte_test_status[b] =
  4768. WL_SOFTWARE;
  4769. } else if (wl_offset == 0) {
  4770. byte_test_status[b] =
  4771. WL_SOFTWARE1;
  4772. }
  4773. // test no longer, remove
  4774. // from byte mask this pass
  4775. bytemask &= ~(0xffULL <<
  4776. (8 * b));
  4777. // remove completely from
  4778. // concern
  4779. bytes_todo &= ~(1 << b);
  4780. // on to the next byte, bypass
  4781. // delay updating!!
  4782. continue;
  4783. } else {
  4784. debug(" byte %d delay %2d Passed\n",
  4785. b, delay);
  4786. }
  4787. }
  4788. // error or no, here we move to the next delay
  4789. // value for this byte, unless done all delays
  4790. // only a byte that has "fully passed" will
  4791. // bypass around this,
  4792. delay += 2;
  4793. if (delay < 32) {
  4794. upd_wl_rank(&wl_rank, b, delay);
  4795. debug(" byte %d delay %2d New\n",
  4796. b, delay);
  4797. byte_delay[b] = delay;
  4798. } else {
  4799. // reached max delay, done with this
  4800. // byte
  4801. debug(" byte %d delay %2d Exhausted\n",
  4802. b, delay);
  4803. // test no longer, remove from byte
  4804. // mask this pass
  4805. bytemask &= ~(0xffULL << (8 * b));
  4806. }
  4807. } /* for (b = 0; b < 8; ++b) */
  4808. debug("End of for-loop: bytemask 0x%llx\n", bytemask);
  4809. } /* while (bytemask != 0) */
  4810. }
  4811. for (b = 0; b < 8; ++b) {
  4812. // any bytes left in bytes_todo did not pass
  4813. if (bytes_todo & (1 << b)) {
  4814. union cvmx_lmcx_rlevel_rankx lmc_rlevel_rank;
  4815. /*
  4816. * Last resort. Use Rlevel settings to estimate
  4817. * Wlevel if software write-leveling fails
  4818. */
  4819. debug("Using RLEVEL as WLEVEL estimate for byte %d\n",
  4820. b);
  4821. lmc_rlevel_rank.u64 =
  4822. lmc_rd(priv, CVMX_LMCX_RLEVEL_RANKX(rankx,
  4823. if_num));
  4824. rlevel_to_wlevel(&lmc_rlevel_rank, &wl_rank, b);
  4825. }
  4826. } /* for (b = 0; b < 8; ++b) */
  4827. }
  4828. static int lmc_sw_write_leveling(struct ddr_priv *priv)
  4829. {
  4830. /* Try to determine/optimize write-level delays experimentally. */
  4831. union cvmx_lmcx_wlevel_rankx wl_rank_hw_res;
  4832. union cvmx_lmcx_config cfg;
  4833. int rankx;
  4834. int byte;
  4835. char *s;
  4836. int i;
  4837. int active_rank;
  4838. int sw_wl_enable = 1; /* FIX... Should be customizable. */
  4839. int interfaces;
  4840. static const char * const wl_status_strings[] = {
  4841. "(e)",
  4842. " ",
  4843. " ",
  4844. "(1)"
  4845. };
  4846. // FIXME: make HW-assist the default now?
  4847. int sw_wl_hw_default = SW_WLEVEL_HW_DEFAULT;
  4848. int dram_connection = c_cfg->dram_connection;
  4849. s = lookup_env(priv, "ddr_sw_wlevel_hw");
  4850. if (s)
  4851. sw_wl_hw_default = !!strtoul(s, NULL, 0);
  4852. if (!if_64b) // must use SW algo if 32-bit mode
  4853. sw_wl_hw_default = 0;
  4854. // can never use hw-assist
  4855. if (octeon_is_cpuid(OCTEON_CN78XX_PASS1_X))
  4856. sw_wl_hw_default = 0;
  4857. s = lookup_env(priv, "ddr_software_wlevel");
  4858. if (s)
  4859. sw_wl_enable = strtoul(s, NULL, 0);
  4860. s = lookup_env(priv, "ddr%d_dram_connection", if_num);
  4861. if (s)
  4862. dram_connection = !!strtoul(s, NULL, 0);
  4863. cvmx_rng_enable();
  4864. /*
  4865. * Get the measured_vref setting from the config, check for an
  4866. * override...
  4867. */
  4868. /* NOTE: measured_vref=1 (ON) means force use of MEASURED vref... */
  4869. // NOTE: measured VREF can only be done for DDR4
  4870. if (ddr_type == DDR4_DRAM) {
  4871. measured_vref_flag = c_cfg->measured_vref;
  4872. s = lookup_env(priv, "ddr_measured_vref");
  4873. if (s)
  4874. measured_vref_flag = !!strtoul(s, NULL, 0);
  4875. } else {
  4876. measured_vref_flag = 0; // OFF for DDR3
  4877. }
  4878. /*
  4879. * Ensure disabled ECC for DRAM tests using the SW algo, else leave
  4880. * it untouched
  4881. */
  4882. if (!sw_wl_hw_default) {
  4883. cfg.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  4884. cfg.cn78xx.ecc_ena = 0;
  4885. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), cfg.u64);
  4886. }
  4887. /*
  4888. * We need to track absolute rank number, as well as how many
  4889. * active ranks we have. Two single rank DIMMs show up as
  4890. * ranks 0 and 2, but only 2 ranks are active.
  4891. */
  4892. active_rank = 0;
  4893. interfaces = __builtin_popcount(if_mask);
  4894. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  4895. final_vref_range = 0;
  4896. start_vref_val = 0;
  4897. computed_final_vref_val = -1;
  4898. sw_wl_rank_status = WL_HARDWARE;
  4899. sw_wl_failed = 0;
  4900. sw_wl_hw = sw_wl_hw_default;
  4901. if (!sw_wl_enable)
  4902. break;
  4903. if (!(rank_mask & (1 << rankx)))
  4904. continue;
  4905. debug("N%d.LMC%d.R%d: Performing Software Write-Leveling %s\n",
  4906. node, if_num, rankx,
  4907. (sw_wl_hw) ? "with H/W assist" :
  4908. "with S/W algorithm");
  4909. if (ddr_type == DDR4_DRAM && num_ranks != 4) {
  4910. // always compute when we can...
  4911. computed_final_vref_val =
  4912. compute_vref_val(priv, if_num, rankx, dimm_count,
  4913. num_ranks, imp_val,
  4914. is_stacked_die, dram_connection);
  4915. // but only use it if allowed
  4916. if (!measured_vref_flag) {
  4917. // skip all the measured vref processing,
  4918. // just the final setting
  4919. start_vref_val = VREF_FINAL;
  4920. }
  4921. }
  4922. /* Save off the h/w wl results */
  4923. wl_rank_hw_res.u64 = lmc_rd(priv,
  4924. CVMX_LMCX_WLEVEL_RANKX(rankx,
  4925. if_num));
  4926. vref_val_count = 0;
  4927. vref_val_start = 0;
  4928. best_vref_val_count = 0;
  4929. best_vref_val_start = 0;
  4930. /* Loop one extra time using the Final vref value. */
  4931. for (vref_val = start_vref_val; vref_val < VREF_LIMIT;
  4932. ++vref_val) {
  4933. if (ddr_type == DDR4_DRAM)
  4934. ddr4_vref_loop(priv, rankx);
  4935. /* Restore the saved value */
  4936. wl_rank.u64 = wl_rank_hw_res.u64;
  4937. for (byte = 0; byte < 9; ++byte)
  4938. byte_test_status[byte] = WL_ESTIMATED;
  4939. if (wl_mask_err == 0) {
  4940. /*
  4941. * Determine address of DRAM to test for
  4942. * pass 1 of software write leveling.
  4943. */
  4944. rank_addr = active_rank *
  4945. (1ull << (pbank_lsb - bunk_enable +
  4946. (interfaces / 2)));
  4947. /*
  4948. * Adjust address for boot bus hole in memory
  4949. * map.
  4950. */
  4951. if (rank_addr > 0x10000000)
  4952. rank_addr += 0x10000000;
  4953. debug("N%d.LMC%d.R%d: Active Rank %d Address: 0x%llx\n",
  4954. node, if_num, rankx, active_rank,
  4955. rank_addr);
  4956. // start parallel write-leveling block for
  4957. // delay high-order bits
  4958. errors = 0;
  4959. no_errors_count = 0;
  4960. sum_dram_dclk = 0;
  4961. sum_dram_ops = 0;
  4962. if (if_64b) {
  4963. bytes_todo = (sw_wl_hw) ?
  4964. if_bytemask : 0xFF;
  4965. bytemask = ~0ULL;
  4966. } else {
  4967. // 32-bit, must be using SW algo,
  4968. // only data bytes
  4969. bytes_todo = 0x0f;
  4970. bytemask = 0x00000000ffffffffULL;
  4971. }
  4972. for (byte = 0; byte < 9; ++byte) {
  4973. if (!(bytes_todo & (1 << byte))) {
  4974. byte_delay[byte] = 0;
  4975. } else {
  4976. byte_delay[byte] =
  4977. get_wl_rank(&wl_rank, byte);
  4978. }
  4979. } /* for (byte = 0; byte < 9; ++byte) */
  4980. do {
  4981. lmc_sw_write_leveling_loop(priv, rankx);
  4982. } while (no_errors_count <
  4983. WL_MIN_NO_ERRORS_COUNT);
  4984. if (!sw_wl_hw) {
  4985. u64 percent_x10;
  4986. if (sum_dram_dclk == 0)
  4987. sum_dram_dclk = 1;
  4988. percent_x10 = sum_dram_ops * 1000 /
  4989. sum_dram_dclk;
  4990. debug("N%d.LMC%d.R%d: ops %llu, cycles %llu, used %llu.%llu%%\n",
  4991. node, if_num, rankx, sum_dram_ops,
  4992. sum_dram_dclk, percent_x10 / 10,
  4993. percent_x10 % 10);
  4994. }
  4995. if (errors) {
  4996. debug("End WLEV_64 while loop: vref_val %d(0x%x), errors 0x%02x\n",
  4997. vref_val, vref_val, errors);
  4998. }
  4999. // end parallel write-leveling block for
  5000. // delay high-order bits
  5001. // if we used HW-assist, we did the ECC byte
  5002. // when approp.
  5003. if (sw_wl_hw) {
  5004. if (wl_print) {
  5005. debug("N%d.LMC%d.R%d: HW-assisted SWL - ECC estimate not needed.\n",
  5006. node, if_num, rankx);
  5007. }
  5008. goto no_ecc_estimate;
  5009. }
  5010. if ((if_bytemask & 0xff) == 0xff) {
  5011. if (use_ecc) {
  5012. sw_write_lvl_use_ecc(priv,
  5013. rankx);
  5014. } else {
  5015. /* H/W delay value */
  5016. byte_test_status[8] =
  5017. WL_HARDWARE;
  5018. /* ECC is not used */
  5019. wl_rank.s.byte8 =
  5020. wl_rank.s.byte0;
  5021. }
  5022. } else {
  5023. if (use_ecc) {
  5024. /* Estimate the ECC byte dly */
  5025. // add hi-order to b4
  5026. wl_rank.s.byte4 |=
  5027. (wl_rank.s.byte3 &
  5028. 0x38);
  5029. if ((wl_rank.s.byte4 & 0x06) <
  5030. (wl_rank.s.byte3 & 0x06)) {
  5031. // must be next clock
  5032. wl_rank.s.byte4 += 8;
  5033. }
  5034. } else {
  5035. /* ECC is not used */
  5036. wl_rank.s.byte4 =
  5037. wl_rank.s.byte0;
  5038. }
  5039. /*
  5040. * Change the status if s/w adjusted
  5041. * the delay
  5042. */
  5043. /* Estimated delay */
  5044. byte_test_status[4] = WL_SOFTWARE;
  5045. } /* if ((if_bytemask & 0xff) == 0xff) */
  5046. } /* if (wl_mask_err == 0) */
  5047. no_ecc_estimate:
  5048. bytes_failed = 0;
  5049. for (byte = 0; byte < 9; ++byte) {
  5050. /* Don't accumulate errors for untested bytes */
  5051. if (!(if_bytemask & (1 << byte)))
  5052. continue;
  5053. bytes_failed +=
  5054. (byte_test_status[byte] == WL_ESTIMATED);
  5055. }
  5056. /* vref training loop is only used for DDR4 */
  5057. if (ddr_type != DDR4_DRAM)
  5058. break;
  5059. if (bytes_failed == 0) {
  5060. if (vref_val_count == 0)
  5061. vref_val_start = vref_val;
  5062. ++vref_val_count;
  5063. if (vref_val_count > best_vref_val_count) {
  5064. best_vref_val_count = vref_val_count;
  5065. best_vref_val_start = vref_val_start;
  5066. debug("N%d.LMC%d.R%d: vref Training (%2d) : 0x%02x <----- ???? -----> 0x%02x\n",
  5067. node, if_num, rankx, vref_val,
  5068. best_vref_val_start,
  5069. best_vref_val_start +
  5070. best_vref_val_count - 1);
  5071. }
  5072. } else {
  5073. vref_val_count = 0;
  5074. debug("N%d.LMC%d.R%d: vref Training (%2d) : failed\n",
  5075. node, if_num, rankx, vref_val);
  5076. }
  5077. }
  5078. /*
  5079. * Determine address of DRAM to test for software write
  5080. * leveling.
  5081. */
  5082. rank_addr = active_rank * (1ull << (pbank_lsb - bunk_enable +
  5083. (interfaces / 2)));
  5084. /* Adjust address for boot bus hole in memory map. */
  5085. if (rank_addr > 0x10000000)
  5086. rank_addr += 0x10000000;
  5087. debug("Rank Address: 0x%llx\n", rank_addr);
  5088. if (bytes_failed) {
  5089. // FIXME? the big hammer, did not even try SW WL pass2,
  5090. // assume only chip reset will help
  5091. debug("N%d.LMC%d.R%d: S/W write-leveling pass 1 failed\n",
  5092. node, if_num, rankx);
  5093. sw_wl_failed = 1;
  5094. } else { /* if (bytes_failed) */
  5095. // SW WL pass 1 was OK, write the settings
  5096. lmc_wr(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num),
  5097. wl_rank.u64);
  5098. wl_rank.u64 = lmc_rd(priv,
  5099. CVMX_LMCX_WLEVEL_RANKX(rankx,
  5100. if_num));
  5101. // do validity check on the delay values by running
  5102. // the test 1 more time...
  5103. // FIXME: we really need to check the ECC byte setting
  5104. // here as well, so we need to enable ECC for this test!
  5105. // if there are any errors, claim SW WL failure
  5106. u64 datamask = (if_64b) ? 0xffffffffffffffffULL :
  5107. 0x00000000ffffffffULL;
  5108. int errors;
  5109. // do the test
  5110. if (sw_wl_hw) {
  5111. errors = run_best_hw_patterns(priv, if_num,
  5112. rank_addr,
  5113. DBTRAIN_TEST,
  5114. NULL) & 0xff;
  5115. } else {
  5116. errors = test_dram_byte64(priv, if_num,
  5117. rank_addr, datamask,
  5118. NULL);
  5119. }
  5120. if (errors) {
  5121. debug("N%d.LMC%d.R%d: Wlevel Rank Final Test errors 0x%03x\n",
  5122. node, if_num, rankx, errors);
  5123. sw_wl_failed = 1;
  5124. }
  5125. } /* if (bytes_failed) */
  5126. // FIXME? dump the WL settings, so we get more of a clue
  5127. // as to what happened where
  5128. debug("N%d.LMC%d.R%d: Wlevel Rank %#4x, 0x%016llX : %2d%3s %2d%3s %2d%3s %2d%3s %2d%3s %2d%3s %2d%3s %2d%3s %2d%3s %s\n",
  5129. node, if_num, rankx, wl_rank.s.status, wl_rank.u64,
  5130. wl_rank.s.byte8, wl_status_strings[byte_test_status[8]],
  5131. wl_rank.s.byte7, wl_status_strings[byte_test_status[7]],
  5132. wl_rank.s.byte6, wl_status_strings[byte_test_status[6]],
  5133. wl_rank.s.byte5, wl_status_strings[byte_test_status[5]],
  5134. wl_rank.s.byte4, wl_status_strings[byte_test_status[4]],
  5135. wl_rank.s.byte3, wl_status_strings[byte_test_status[3]],
  5136. wl_rank.s.byte2, wl_status_strings[byte_test_status[2]],
  5137. wl_rank.s.byte1, wl_status_strings[byte_test_status[1]],
  5138. wl_rank.s.byte0, wl_status_strings[byte_test_status[0]],
  5139. (sw_wl_rank_status == WL_HARDWARE) ? "" : "(s)");
  5140. // finally, check for fatal conditions: either chip reset
  5141. // right here, or return error flag
  5142. if ((ddr_type == DDR4_DRAM && best_vref_val_count == 0) ||
  5143. sw_wl_failed) {
  5144. if (!ddr_disable_chip_reset) { // do chip RESET
  5145. printf("N%d.LMC%d.R%d: INFO: Short memory test indicates a retry is needed. Resetting node...\n",
  5146. node, if_num, rankx);
  5147. mdelay(500);
  5148. do_reset(NULL, 0, 0, NULL);
  5149. } else {
  5150. // return error flag so LMC init can be retried.
  5151. debug("N%d.LMC%d.R%d: INFO: Short memory test indicates a retry is needed. Restarting LMC init...\n",
  5152. node, if_num, rankx);
  5153. return -EAGAIN; // 0 indicates restart possible.
  5154. }
  5155. }
  5156. active_rank++;
  5157. }
  5158. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  5159. int parameter_set = 0;
  5160. u64 value;
  5161. if (!(rank_mask & (1 << rankx)))
  5162. continue;
  5163. wl_rank.u64 = lmc_rd(priv, CVMX_LMCX_WLEVEL_RANKX(rankx,
  5164. if_num));
  5165. for (i = 0; i < 9; ++i) {
  5166. s = lookup_env(priv, "ddr%d_wlevel_rank%d_byte%d",
  5167. if_num, rankx, i);
  5168. if (s) {
  5169. parameter_set |= 1;
  5170. value = strtoul(s, NULL, 0);
  5171. upd_wl_rank(&wl_rank, i, value);
  5172. }
  5173. }
  5174. s = lookup_env_ull(priv, "ddr%d_wlevel_rank%d", if_num, rankx);
  5175. if (s) {
  5176. parameter_set |= 1;
  5177. value = strtoull(s, NULL, 0);
  5178. wl_rank.u64 = value;
  5179. }
  5180. if (parameter_set) {
  5181. lmc_wr(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num),
  5182. wl_rank.u64);
  5183. wl_rank.u64 =
  5184. lmc_rd(priv, CVMX_LMCX_WLEVEL_RANKX(rankx, if_num));
  5185. display_wl(if_num, wl_rank, rankx);
  5186. }
  5187. // if there are unused entries to be filled
  5188. if ((rank_mask & 0x0F) != 0x0F) {
  5189. if (rankx < 3) {
  5190. debug("N%d.LMC%d.R%d: checking for WLEVEL_RANK unused entries.\n",
  5191. node, if_num, rankx);
  5192. // if rank 0, write ranks 1 and 2 here if empty
  5193. if (rankx == 0) {
  5194. // check that rank 1 is empty
  5195. if (!(rank_mask & (1 << 1))) {
  5196. debug("N%d.LMC%d.R%d: writing WLEVEL_RANK unused entry R%d.\n",
  5197. node, if_num, rankx, 1);
  5198. lmc_wr(priv,
  5199. CVMX_LMCX_WLEVEL_RANKX(1,
  5200. if_num),
  5201. wl_rank.u64);
  5202. }
  5203. // check that rank 2 is empty
  5204. if (!(rank_mask & (1 << 2))) {
  5205. debug("N%d.LMC%d.R%d: writing WLEVEL_RANK unused entry R%d.\n",
  5206. node, if_num, rankx, 2);
  5207. lmc_wr(priv,
  5208. CVMX_LMCX_WLEVEL_RANKX(2,
  5209. if_num),
  5210. wl_rank.u64);
  5211. }
  5212. }
  5213. // if rank 0, 1 or 2, write rank 3 here if empty
  5214. // check that rank 3 is empty
  5215. if (!(rank_mask & (1 << 3))) {
  5216. debug("N%d.LMC%d.R%d: writing WLEVEL_RANK unused entry R%d.\n",
  5217. node, if_num, rankx, 3);
  5218. lmc_wr(priv,
  5219. CVMX_LMCX_WLEVEL_RANKX(3,
  5220. if_num),
  5221. wl_rank.u64);
  5222. }
  5223. }
  5224. }
  5225. }
  5226. /* Enable 32-bit mode if required. */
  5227. cfg.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  5228. cfg.cn78xx.mode32b = (!if_64b);
  5229. debug("%-45s : %d\n", "MODE32B", cfg.cn78xx.mode32b);
  5230. /* Restore the ECC configuration */
  5231. if (!sw_wl_hw_default)
  5232. cfg.cn78xx.ecc_ena = use_ecc;
  5233. lmc_wr(priv, CVMX_LMCX_CONFIG(if_num), cfg.u64);
  5234. return 0;
  5235. }
  5236. static void lmc_dll(struct ddr_priv *priv)
  5237. {
  5238. union cvmx_lmcx_dll_ctl3 ddr_dll_ctl3;
  5239. int setting[9];
  5240. int i;
  5241. ddr_dll_ctl3.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL3(if_num));
  5242. for (i = 0; i < 9; ++i) {
  5243. SET_DDR_DLL_CTL3(dll90_byte_sel, ENCODE_DLL90_BYTE_SEL(i));
  5244. lmc_wr(priv, CVMX_LMCX_DLL_CTL3(if_num), ddr_dll_ctl3.u64);
  5245. lmc_rd(priv, CVMX_LMCX_DLL_CTL3(if_num));
  5246. ddr_dll_ctl3.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL3(if_num));
  5247. setting[i] = GET_DDR_DLL_CTL3(dll90_setting);
  5248. debug("%d. LMC%d_DLL_CTL3[%d] = %016llx %d\n", i, if_num,
  5249. GET_DDR_DLL_CTL3(dll90_byte_sel), ddr_dll_ctl3.u64,
  5250. setting[i]);
  5251. }
  5252. debug("N%d.LMC%d: %-36s : %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
  5253. node, if_num, "DLL90 Setting 8:0",
  5254. setting[8], setting[7], setting[6], setting[5], setting[4],
  5255. setting[3], setting[2], setting[1], setting[0]);
  5256. process_custom_dll_offsets(priv, if_num, "ddr_dll_write_offset",
  5257. c_cfg->dll_write_offset,
  5258. "ddr%d_dll_write_offset_byte%d", 1);
  5259. process_custom_dll_offsets(priv, if_num, "ddr_dll_read_offset",
  5260. c_cfg->dll_read_offset,
  5261. "ddr%d_dll_read_offset_byte%d", 2);
  5262. }
  5263. #define SLOT_CTL_INCR(csr, chip, field, incr) \
  5264. csr.chip.field = (csr.chip.field < (64 - incr)) ? \
  5265. (csr.chip.field + incr) : 63
  5266. #define INCR(csr, chip, field, incr) \
  5267. csr.chip.field = (csr.chip.field < (64 - incr)) ? \
  5268. (csr.chip.field + incr) : 63
  5269. static void lmc_workaround_2(struct ddr_priv *priv)
  5270. {
  5271. /* Workaround Errata 21063 */
  5272. if (octeon_is_cpuid(OCTEON_CN78XX) ||
  5273. octeon_is_cpuid(OCTEON_CN70XX_PASS1_X)) {
  5274. union cvmx_lmcx_slot_ctl0 slot_ctl0;
  5275. union cvmx_lmcx_slot_ctl1 slot_ctl1;
  5276. union cvmx_lmcx_slot_ctl2 slot_ctl2;
  5277. union cvmx_lmcx_ext_config ext_cfg;
  5278. slot_ctl0.u64 = lmc_rd(priv, CVMX_LMCX_SLOT_CTL0(if_num));
  5279. slot_ctl1.u64 = lmc_rd(priv, CVMX_LMCX_SLOT_CTL1(if_num));
  5280. slot_ctl2.u64 = lmc_rd(priv, CVMX_LMCX_SLOT_CTL2(if_num));
  5281. ext_cfg.u64 = lmc_rd(priv, CVMX_LMCX_EXT_CONFIG(if_num));
  5282. /* When ext_cfg.s.read_ena_bprch is set add 1 */
  5283. if (ext_cfg.s.read_ena_bprch) {
  5284. SLOT_CTL_INCR(slot_ctl0, cn78xx, r2w_init, 1);
  5285. SLOT_CTL_INCR(slot_ctl0, cn78xx, r2w_l_init, 1);
  5286. SLOT_CTL_INCR(slot_ctl1, cn78xx, r2w_xrank_init, 1);
  5287. SLOT_CTL_INCR(slot_ctl2, cn78xx, r2w_xdimm_init, 1);
  5288. }
  5289. /* Always add 2 */
  5290. SLOT_CTL_INCR(slot_ctl1, cn78xx, w2r_xrank_init, 2);
  5291. SLOT_CTL_INCR(slot_ctl2, cn78xx, w2r_xdimm_init, 2);
  5292. lmc_wr(priv, CVMX_LMCX_SLOT_CTL0(if_num), slot_ctl0.u64);
  5293. lmc_wr(priv, CVMX_LMCX_SLOT_CTL1(if_num), slot_ctl1.u64);
  5294. lmc_wr(priv, CVMX_LMCX_SLOT_CTL2(if_num), slot_ctl2.u64);
  5295. }
  5296. /* Workaround Errata 21216 */
  5297. if (octeon_is_cpuid(OCTEON_CN78XX_PASS1_X) ||
  5298. octeon_is_cpuid(OCTEON_CN70XX_PASS1_X)) {
  5299. union cvmx_lmcx_slot_ctl1 slot_ctl1;
  5300. union cvmx_lmcx_slot_ctl2 slot_ctl2;
  5301. slot_ctl1.u64 = lmc_rd(priv, CVMX_LMCX_SLOT_CTL1(if_num));
  5302. slot_ctl1.cn78xx.w2w_xrank_init =
  5303. max(10, (int)slot_ctl1.cn78xx.w2w_xrank_init);
  5304. lmc_wr(priv, CVMX_LMCX_SLOT_CTL1(if_num), slot_ctl1.u64);
  5305. slot_ctl2.u64 = lmc_rd(priv, CVMX_LMCX_SLOT_CTL2(if_num));
  5306. slot_ctl2.cn78xx.w2w_xdimm_init =
  5307. max(10, (int)slot_ctl2.cn78xx.w2w_xdimm_init);
  5308. lmc_wr(priv, CVMX_LMCX_SLOT_CTL2(if_num), slot_ctl2.u64);
  5309. }
  5310. }
  5311. static void lmc_final(struct ddr_priv *priv)
  5312. {
  5313. /*
  5314. * 4.8.11 Final LMC Initialization
  5315. *
  5316. * Early LMC initialization, LMC write-leveling, and LMC read-leveling
  5317. * must be completed prior to starting this final LMC initialization.
  5318. *
  5319. * LMC hardware updates the LMC(0)_SLOT_CTL0, LMC(0)_SLOT_CTL1,
  5320. * LMC(0)_SLOT_CTL2 CSRs with minimum values based on the selected
  5321. * readleveling and write-leveling settings. Software should not write
  5322. * the final LMC(0)_SLOT_CTL0, LMC(0)_SLOT_CTL1, and LMC(0)_SLOT_CTL2
  5323. * values until after the final read-leveling and write-leveling
  5324. * settings are written.
  5325. *
  5326. * Software must ensure the LMC(0)_SLOT_CTL0, LMC(0)_SLOT_CTL1, and
  5327. * LMC(0)_SLOT_CTL2 CSR values are appropriate for this step. These CSRs
  5328. * select the minimum gaps between read operations and write operations
  5329. * of various types.
  5330. *
  5331. * Software must not reduce the values in these CSR fields below the
  5332. * values previously selected by the LMC hardware (during write-leveling
  5333. * and read-leveling steps above).
  5334. *
  5335. * All sections in this chapter may be used to derive proper settings
  5336. * for these registers.
  5337. *
  5338. * For minimal read latency, L2C_CTL[EF_ENA,EF_CNT] should be programmed
  5339. * properly. This should be done prior to the first read.
  5340. */
  5341. /* Clear any residual ECC errors */
  5342. int num_tads = 1;
  5343. int tad;
  5344. int num_mcis = 1;
  5345. int mci;
  5346. if (octeon_is_cpuid(OCTEON_CN78XX)) {
  5347. num_tads = 8;
  5348. num_mcis = 4;
  5349. } else if (octeon_is_cpuid(OCTEON_CN70XX)) {
  5350. num_tads = 1;
  5351. num_mcis = 1;
  5352. } else if (octeon_is_cpuid(OCTEON_CN73XX) ||
  5353. octeon_is_cpuid(OCTEON_CNF75XX)) {
  5354. num_tads = 4;
  5355. num_mcis = 3;
  5356. }
  5357. lmc_wr(priv, CVMX_LMCX_INT(if_num), -1ULL);
  5358. lmc_rd(priv, CVMX_LMCX_INT(if_num));
  5359. for (tad = 0; tad < num_tads; tad++) {
  5360. l2c_wr(priv, CVMX_L2C_TADX_INT_REL(tad),
  5361. l2c_rd(priv, CVMX_L2C_TADX_INT_REL(tad)));
  5362. debug("%-45s : (%d) 0x%08llx\n", "CVMX_L2C_TAD_INT", tad,
  5363. l2c_rd(priv, CVMX_L2C_TADX_INT_REL(tad)));
  5364. }
  5365. for (mci = 0; mci < num_mcis; mci++) {
  5366. l2c_wr(priv, CVMX_L2C_MCIX_INT_REL(mci),
  5367. l2c_rd(priv, CVMX_L2C_MCIX_INT_REL(mci)));
  5368. debug("%-45s : (%d) 0x%08llx\n", "L2C_MCI_INT", mci,
  5369. l2c_rd(priv, CVMX_L2C_MCIX_INT_REL(mci)));
  5370. }
  5371. debug("%-45s : 0x%08llx\n", "LMC_INT",
  5372. lmc_rd(priv, CVMX_LMCX_INT(if_num)));
  5373. }
  5374. static void lmc_scrambling(struct ddr_priv *priv)
  5375. {
  5376. // Make sure scrambling is disabled during init...
  5377. union cvmx_lmcx_control ctrl;
  5378. union cvmx_lmcx_scramble_cfg0 lmc_scramble_cfg0;
  5379. union cvmx_lmcx_scramble_cfg1 lmc_scramble_cfg1;
  5380. union cvmx_lmcx_scramble_cfg2 lmc_scramble_cfg2;
  5381. union cvmx_lmcx_ns_ctl lmc_ns_ctl;
  5382. int use_scramble = 0; // default OFF
  5383. char *s;
  5384. ctrl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  5385. lmc_scramble_cfg0.u64 = lmc_rd(priv, CVMX_LMCX_SCRAMBLE_CFG0(if_num));
  5386. lmc_scramble_cfg1.u64 = lmc_rd(priv, CVMX_LMCX_SCRAMBLE_CFG1(if_num));
  5387. lmc_scramble_cfg2.u64 = 0; // quiet compiler
  5388. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS1_X)) {
  5389. lmc_scramble_cfg2.u64 =
  5390. lmc_rd(priv, CVMX_LMCX_SCRAMBLE_CFG2(if_num));
  5391. }
  5392. lmc_ns_ctl.u64 = lmc_rd(priv, CVMX_LMCX_NS_CTL(if_num));
  5393. s = lookup_env_ull(priv, "ddr_use_scramble");
  5394. if (s)
  5395. use_scramble = simple_strtoull(s, NULL, 0);
  5396. /* Generate random values if scrambling is needed */
  5397. if (use_scramble) {
  5398. lmc_scramble_cfg0.u64 = cvmx_rng_get_random64();
  5399. lmc_scramble_cfg1.u64 = cvmx_rng_get_random64();
  5400. lmc_scramble_cfg2.u64 = cvmx_rng_get_random64();
  5401. lmc_ns_ctl.s.ns_scramble_dis = 0;
  5402. lmc_ns_ctl.s.adr_offset = 0;
  5403. ctrl.s.scramble_ena = 1;
  5404. }
  5405. s = lookup_env_ull(priv, "ddr_scramble_cfg0");
  5406. if (s) {
  5407. lmc_scramble_cfg0.u64 = simple_strtoull(s, NULL, 0);
  5408. ctrl.s.scramble_ena = 1;
  5409. }
  5410. debug("%-45s : 0x%016llx\n", "LMC_SCRAMBLE_CFG0",
  5411. lmc_scramble_cfg0.u64);
  5412. lmc_wr(priv, CVMX_LMCX_SCRAMBLE_CFG0(if_num), lmc_scramble_cfg0.u64);
  5413. s = lookup_env_ull(priv, "ddr_scramble_cfg1");
  5414. if (s) {
  5415. lmc_scramble_cfg1.u64 = simple_strtoull(s, NULL, 0);
  5416. ctrl.s.scramble_ena = 1;
  5417. }
  5418. debug("%-45s : 0x%016llx\n", "LMC_SCRAMBLE_CFG1",
  5419. lmc_scramble_cfg1.u64);
  5420. lmc_wr(priv, CVMX_LMCX_SCRAMBLE_CFG1(if_num), lmc_scramble_cfg1.u64);
  5421. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS1_X)) {
  5422. s = lookup_env_ull(priv, "ddr_scramble_cfg2");
  5423. if (s) {
  5424. lmc_scramble_cfg2.u64 = simple_strtoull(s, NULL, 0);
  5425. ctrl.s.scramble_ena = 1;
  5426. }
  5427. debug("%-45s : 0x%016llx\n", "LMC_SCRAMBLE_CFG2",
  5428. lmc_scramble_cfg1.u64);
  5429. lmc_wr(priv, CVMX_LMCX_SCRAMBLE_CFG2(if_num),
  5430. lmc_scramble_cfg2.u64);
  5431. }
  5432. s = lookup_env_ull(priv, "ddr_ns_ctl");
  5433. if (s)
  5434. lmc_ns_ctl.u64 = simple_strtoull(s, NULL, 0);
  5435. debug("%-45s : 0x%016llx\n", "LMC_NS_CTL", lmc_ns_ctl.u64);
  5436. lmc_wr(priv, CVMX_LMCX_NS_CTL(if_num), lmc_ns_ctl.u64);
  5437. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctrl.u64);
  5438. }
  5439. struct rl_score {
  5440. u64 setting;
  5441. int score;
  5442. };
  5443. static union cvmx_lmcx_rlevel_rankx rl_rank __section(".data");
  5444. static union cvmx_lmcx_rlevel_ctl rl_ctl __section(".data");
  5445. static unsigned char rodt_ctl __section(".data");
  5446. static int rl_rodt_err __section(".data");
  5447. static unsigned char rtt_nom __section(".data");
  5448. static unsigned char rtt_idx __section(".data");
  5449. static char min_rtt_nom_idx __section(".data");
  5450. static char max_rtt_nom_idx __section(".data");
  5451. static char min_rodt_ctl __section(".data");
  5452. static char max_rodt_ctl __section(".data");
  5453. static int rl_dbg_loops __section(".data");
  5454. static unsigned char save_ddr2t __section(".data");
  5455. static int rl_samples __section(".data");
  5456. static char rl_compute __section(".data");
  5457. static char saved_ddr__ptune __section(".data");
  5458. static char saved_ddr__ntune __section(".data");
  5459. static char rl_comp_offs __section(".data");
  5460. static char saved_int_zqcs_dis __section(".data");
  5461. static int max_adj_rl_del_inc __section(".data");
  5462. static int print_nom_ohms __section(".data");
  5463. static int rl_print __section(".data");
  5464. #ifdef ENABLE_HARDCODED_RLEVEL
  5465. static char part_number[21] __section(".data");
  5466. #endif /* ENABLE_HARDCODED_RLEVEL */
  5467. struct perfect_counts {
  5468. u16 count[9][32]; // 8+ECC by 64 values
  5469. u32 mask[9]; // 8+ECC, bitmask of perfect delays
  5470. };
  5471. static struct perfect_counts rank_perf[4] __section(".data");
  5472. static struct perfect_counts rodt_perfect_counts __section(".data");
  5473. static int pbm_lowsum_limit __section(".data");
  5474. // FIXME: PBM skip for RODT 240 and 34
  5475. static u32 pbm_rodt_skip __section(".data");
  5476. // control rank majority processing
  5477. static int disable_rank_majority __section(".data");
  5478. // default to mask 11b ODDs for DDR4 (except 73xx), else DISABLE
  5479. // for DDR3
  5480. static int enable_rldelay_bump __section(".data");
  5481. static int rldelay_bump_incr __section(".data");
  5482. static int disable_rlv_bump_this_byte __section(".data");
  5483. static u64 value_mask __section(".data");
  5484. static struct rlevel_byte_data rl_byte[9] __section(".data");
  5485. static int sample_loops __section(".data");
  5486. static int max_samples __section(".data");
  5487. static int rl_rank_errors __section(".data");
  5488. static int rl_mask_err __section(".data");
  5489. static int rl_nonseq_err __section(".data");
  5490. static struct rlevel_bitmask rl_mask[9] __section(".data");
  5491. static int rl_best_rank_score __section(".data");
  5492. static int rodt_row_skip_mask __section(".data");
  5493. static void rodt_loop(struct ddr_priv *priv, int rankx, struct rl_score
  5494. rl_score[RTT_NOM_OHMS_COUNT][RODT_OHMS_COUNT][4])
  5495. {
  5496. union cvmx_lmcx_comp_ctl2 cc2;
  5497. const int rl_separate_ab = 1;
  5498. int i;
  5499. rl_best_rank_score = DEFAULT_BEST_RANK_SCORE;
  5500. rl_rodt_err = 0;
  5501. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  5502. cc2.cn78xx.rodt_ctl = rodt_ctl;
  5503. lmc_wr(priv, CVMX_LMCX_COMP_CTL2(if_num), cc2.u64);
  5504. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  5505. udelay(1); /* Give it a little time to take affect */
  5506. if (rl_print > 1) {
  5507. debug("Read ODT_CTL : 0x%x (%d ohms)\n",
  5508. cc2.cn78xx.rodt_ctl,
  5509. imp_val->rodt_ohms[cc2.cn78xx.rodt_ctl]);
  5510. }
  5511. memset(rl_byte, 0, sizeof(rl_byte));
  5512. memset(&rodt_perfect_counts, 0, sizeof(rodt_perfect_counts));
  5513. // when iter RODT is the target RODT, take more samples...
  5514. max_samples = rl_samples;
  5515. if (rodt_ctl == default_rodt_ctl)
  5516. max_samples += rl_samples + 1;
  5517. for (sample_loops = 0; sample_loops < max_samples; sample_loops++) {
  5518. int redoing_nonseq_errs = 0;
  5519. rl_mask_err = 0;
  5520. if (!(rl_separate_ab && spd_rdimm &&
  5521. ddr_type == DDR4_DRAM)) {
  5522. /* Clear read-level delays */
  5523. lmc_wr(priv, CVMX_LMCX_RLEVEL_RANKX(rankx, if_num), 0);
  5524. /* read-leveling */
  5525. oct3_ddr3_seq(priv, 1 << rankx, if_num, 1);
  5526. do {
  5527. rl_rank.u64 =
  5528. lmc_rd(priv,
  5529. CVMX_LMCX_RLEVEL_RANKX(rankx,
  5530. if_num));
  5531. } while (rl_rank.cn78xx.status != 3);
  5532. }
  5533. rl_rank.u64 =
  5534. lmc_rd(priv, CVMX_LMCX_RLEVEL_RANKX(rankx, if_num));
  5535. // start bitmask interpretation block
  5536. memset(rl_mask, 0, sizeof(rl_mask));
  5537. if (rl_separate_ab && spd_rdimm && ddr_type == DDR4_DRAM) {
  5538. union cvmx_lmcx_rlevel_rankx rl_rank_aside;
  5539. union cvmx_lmcx_modereg_params0 mp0;
  5540. /* A-side */
  5541. mp0.u64 =
  5542. lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num));
  5543. mp0.s.mprloc = 0; /* MPR Page 0 Location 0 */
  5544. lmc_wr(priv,
  5545. CVMX_LMCX_MODEREG_PARAMS0(if_num),
  5546. mp0.u64);
  5547. /* Clear read-level delays */
  5548. lmc_wr(priv, CVMX_LMCX_RLEVEL_RANKX(rankx, if_num), 0);
  5549. /* read-leveling */
  5550. oct3_ddr3_seq(priv, 1 << rankx, if_num, 1);
  5551. do {
  5552. rl_rank.u64 =
  5553. lmc_rd(priv,
  5554. CVMX_LMCX_RLEVEL_RANKX(rankx,
  5555. if_num));
  5556. } while (rl_rank.cn78xx.status != 3);
  5557. rl_rank.u64 =
  5558. lmc_rd(priv, CVMX_LMCX_RLEVEL_RANKX(rankx,
  5559. if_num));
  5560. rl_rank_aside.u64 = rl_rank.u64;
  5561. rl_mask[0].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 0);
  5562. rl_mask[1].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 1);
  5563. rl_mask[2].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 2);
  5564. rl_mask[3].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 3);
  5565. rl_mask[8].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 8);
  5566. /* A-side complete */
  5567. /* B-side */
  5568. mp0.u64 =
  5569. lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num));
  5570. mp0.s.mprloc = 3; /* MPR Page 0 Location 3 */
  5571. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num),
  5572. mp0.u64);
  5573. /* Clear read-level delays */
  5574. lmc_wr(priv, CVMX_LMCX_RLEVEL_RANKX(rankx, if_num), 0);
  5575. /* read-leveling */
  5576. oct3_ddr3_seq(priv, 1 << rankx, if_num, 1);
  5577. do {
  5578. rl_rank.u64 =
  5579. lmc_rd(priv,
  5580. CVMX_LMCX_RLEVEL_RANKX(rankx,
  5581. if_num));
  5582. } while (rl_rank.cn78xx.status != 3);
  5583. rl_rank.u64 =
  5584. lmc_rd(priv, CVMX_LMCX_RLEVEL_RANKX(rankx,
  5585. if_num));
  5586. rl_mask[4].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 4);
  5587. rl_mask[5].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 5);
  5588. rl_mask[6].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 6);
  5589. rl_mask[7].bm = lmc_ddr3_rl_dbg_read(priv, if_num, 7);
  5590. /* B-side complete */
  5591. upd_rl_rank(&rl_rank, 0, rl_rank_aside.s.byte0);
  5592. upd_rl_rank(&rl_rank, 1, rl_rank_aside.s.byte1);
  5593. upd_rl_rank(&rl_rank, 2, rl_rank_aside.s.byte2);
  5594. upd_rl_rank(&rl_rank, 3, rl_rank_aside.s.byte3);
  5595. /* ECC A-side */
  5596. upd_rl_rank(&rl_rank, 8, rl_rank_aside.s.byte8);
  5597. mp0.u64 =
  5598. lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num));
  5599. mp0.s.mprloc = 0; /* MPR Page 0 Location 0 */
  5600. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS0(if_num),
  5601. mp0.u64);
  5602. }
  5603. /*
  5604. * Evaluate the quality of the read-leveling delays from the
  5605. * bitmasks. Also save off a software computed read-leveling
  5606. * mask that may be used later to qualify the delay results
  5607. * from Octeon.
  5608. */
  5609. for (i = 0; i < (8 + ecc_ena); ++i) {
  5610. int bmerr;
  5611. if (!(if_bytemask & (1 << i)))
  5612. continue;
  5613. if (!(rl_separate_ab && spd_rdimm &&
  5614. ddr_type == DDR4_DRAM)) {
  5615. rl_mask[i].bm =
  5616. lmc_ddr3_rl_dbg_read(priv, if_num, i);
  5617. }
  5618. bmerr = validate_ddr3_rlevel_bitmask(&rl_mask[i],
  5619. ddr_type);
  5620. rl_mask[i].errs = bmerr;
  5621. rl_mask_err += bmerr;
  5622. // count only the "perfect" bitmasks
  5623. if (ddr_type == DDR4_DRAM && !bmerr) {
  5624. int delay;
  5625. // FIXME: for now, simple filtering:
  5626. // do NOT count PBMs for RODTs in skip mask
  5627. if ((1U << rodt_ctl) & pbm_rodt_skip)
  5628. continue;
  5629. // FIXME: could optimize this a bit?
  5630. delay = get_rl_rank(&rl_rank, i);
  5631. rank_perf[rankx].count[i][delay] += 1;
  5632. rank_perf[rankx].mask[i] |=
  5633. (1ULL << delay);
  5634. rodt_perfect_counts.count[i][delay] += 1;
  5635. rodt_perfect_counts.mask[i] |= (1ULL << delay);
  5636. }
  5637. }
  5638. /* Set delays for unused bytes to match byte 0. */
  5639. for (i = 0; i < 9; ++i) {
  5640. if (if_bytemask & (1 << i))
  5641. continue;
  5642. upd_rl_rank(&rl_rank, i, rl_rank.s.byte0);
  5643. }
  5644. /*
  5645. * Save a copy of the byte delays in physical
  5646. * order for sequential evaluation.
  5647. */
  5648. unpack_rlevel_settings(if_bytemask, ecc_ena, rl_byte, rl_rank);
  5649. redo_nonseq_errs:
  5650. rl_nonseq_err = 0;
  5651. if (!disable_sequential_delay_check) {
  5652. for (i = 0; i < 9; ++i)
  5653. rl_byte[i].sqerrs = 0;
  5654. if ((if_bytemask & 0xff) == 0xff) {
  5655. /*
  5656. * Evaluate delay sequence across the whole
  5657. * range of bytes for standard dimms.
  5658. */
  5659. /* 1=RDIMM, 5=Mini-RDIMM */
  5660. if (spd_dimm_type == 1 || spd_dimm_type == 5) {
  5661. int reg_adj_del = abs(rl_byte[4].delay -
  5662. rl_byte[5].delay);
  5663. /*
  5664. * Registered dimm topology routes
  5665. * from the center.
  5666. */
  5667. rl_nonseq_err +=
  5668. nonseq_del(rl_byte, 0,
  5669. 3 + ecc_ena,
  5670. max_adj_rl_del_inc);
  5671. rl_nonseq_err +=
  5672. nonseq_del(rl_byte, 5,
  5673. 7 + ecc_ena,
  5674. max_adj_rl_del_inc);
  5675. // byte 5 sqerrs never gets cleared
  5676. // for RDIMMs
  5677. rl_byte[5].sqerrs = 0;
  5678. if (reg_adj_del > 1) {
  5679. /*
  5680. * Assess proximity of bytes on
  5681. * opposite sides of register
  5682. */
  5683. rl_nonseq_err += (reg_adj_del -
  5684. 1) *
  5685. RLEVEL_ADJACENT_DELAY_ERROR;
  5686. // update byte 5 error
  5687. rl_byte[5].sqerrs +=
  5688. (reg_adj_del - 1) *
  5689. RLEVEL_ADJACENT_DELAY_ERROR;
  5690. }
  5691. }
  5692. /* 2=UDIMM, 6=Mini-UDIMM */
  5693. if (spd_dimm_type == 2 || spd_dimm_type == 6) {
  5694. /*
  5695. * Unbuffered dimm topology routes
  5696. * from end to end.
  5697. */
  5698. rl_nonseq_err += nonseq_del(rl_byte, 0,
  5699. 7 + ecc_ena,
  5700. max_adj_rl_del_inc);
  5701. }
  5702. } else {
  5703. rl_nonseq_err += nonseq_del(rl_byte, 0,
  5704. 3 + ecc_ena,
  5705. max_adj_rl_del_inc);
  5706. }
  5707. } /* if (! disable_sequential_delay_check) */
  5708. rl_rank_errors = rl_mask_err + rl_nonseq_err;
  5709. // print original sample here only if we are not really
  5710. // averaging or picking best
  5711. // also do not print if we were redoing the NONSEQ score
  5712. // for using COMPUTED
  5713. if (!redoing_nonseq_errs && rl_samples < 2) {
  5714. if (rl_print > 1) {
  5715. display_rl_bm(if_num, rankx, rl_mask, ecc_ena);
  5716. display_rl_bm_scores(if_num, rankx, rl_mask,
  5717. ecc_ena);
  5718. display_rl_seq_scores(if_num, rankx, rl_byte,
  5719. ecc_ena);
  5720. }
  5721. display_rl_with_score(if_num, rl_rank, rankx,
  5722. rl_rank_errors);
  5723. }
  5724. if (rl_compute) {
  5725. if (!redoing_nonseq_errs) {
  5726. /* Recompute the delays based on the bitmask */
  5727. for (i = 0; i < (8 + ecc_ena); ++i) {
  5728. if (!(if_bytemask & (1 << i)))
  5729. continue;
  5730. upd_rl_rank(&rl_rank, i,
  5731. compute_ddr3_rlevel_delay(
  5732. rl_mask[i].mstart,
  5733. rl_mask[i].width,
  5734. rl_ctl));
  5735. }
  5736. /*
  5737. * Override the copy of byte delays with the
  5738. * computed results.
  5739. */
  5740. unpack_rlevel_settings(if_bytemask, ecc_ena,
  5741. rl_byte, rl_rank);
  5742. redoing_nonseq_errs = 1;
  5743. goto redo_nonseq_errs;
  5744. } else {
  5745. /*
  5746. * now print this if already printed the
  5747. * original sample
  5748. */
  5749. if (rl_samples < 2 || rl_print) {
  5750. display_rl_with_computed(if_num,
  5751. rl_rank, rankx,
  5752. rl_rank_errors);
  5753. }
  5754. }
  5755. } /* if (rl_compute) */
  5756. // end bitmask interpretation block
  5757. // if it is a better (lower) score, then keep it
  5758. if (rl_rank_errors < rl_best_rank_score) {
  5759. rl_best_rank_score = rl_rank_errors;
  5760. // save the new best delays and best errors
  5761. for (i = 0; i < (8 + ecc_ena); ++i) {
  5762. rl_byte[i].best = rl_byte[i].delay;
  5763. rl_byte[i].bestsq = rl_byte[i].sqerrs;
  5764. // save bitmasks and their scores as well
  5765. // xlate UNPACKED index to PACKED index to
  5766. // get from rl_mask
  5767. rl_byte[i].bm = rl_mask[XUP(i, !!ecc_ena)].bm;
  5768. rl_byte[i].bmerrs =
  5769. rl_mask[XUP(i, !!ecc_ena)].errs;
  5770. }
  5771. }
  5772. rl_rodt_err += rl_rank_errors;
  5773. }
  5774. /* We recorded the best score across the averaging loops */
  5775. rl_score[rtt_nom][rodt_ctl][rankx].score = rl_best_rank_score;
  5776. /*
  5777. * Restore the delays from the best fields that go with the best
  5778. * score
  5779. */
  5780. for (i = 0; i < 9; ++i) {
  5781. rl_byte[i].delay = rl_byte[i].best;
  5782. rl_byte[i].sqerrs = rl_byte[i].bestsq;
  5783. }
  5784. rl_rank.u64 = lmc_rd(priv, CVMX_LMCX_RLEVEL_RANKX(rankx, if_num));
  5785. pack_rlevel_settings(if_bytemask, ecc_ena, rl_byte, &rl_rank);
  5786. if (rl_samples > 1) {
  5787. // restore the "best" bitmasks and their scores for printing
  5788. for (i = 0; i < 9; ++i) {
  5789. if ((if_bytemask & (1 << i)) == 0)
  5790. continue;
  5791. // xlate PACKED index to UNPACKED index to get from
  5792. // rl_byte
  5793. rl_mask[i].bm = rl_byte[XPU(i, !!ecc_ena)].bm;
  5794. rl_mask[i].errs = rl_byte[XPU(i, !!ecc_ena)].bmerrs;
  5795. }
  5796. // maybe print bitmasks/scores here
  5797. if (rl_print > 1) {
  5798. display_rl_bm(if_num, rankx, rl_mask, ecc_ena);
  5799. display_rl_bm_scores(if_num, rankx, rl_mask, ecc_ena);
  5800. display_rl_seq_scores(if_num, rankx, rl_byte, ecc_ena);
  5801. display_rl_with_rodt(if_num, rl_rank, rankx,
  5802. rl_score[rtt_nom][rodt_ctl][rankx].score,
  5803. print_nom_ohms,
  5804. imp_val->rodt_ohms[rodt_ctl],
  5805. WITH_RODT_BESTSCORE);
  5806. debug("-----------\n");
  5807. }
  5808. }
  5809. rl_score[rtt_nom][rodt_ctl][rankx].setting = rl_rank.u64;
  5810. // print out the PBMs for the current RODT
  5811. if (ddr_type == DDR4_DRAM && rl_print > 1) { // verbosity?
  5812. // FIXME: change verbosity level after debug complete...
  5813. for (i = 0; i < 9; i++) {
  5814. u64 temp_mask;
  5815. int num_values;
  5816. // FIXME: PBM skip for RODTs in mask
  5817. if ((1U << rodt_ctl) & pbm_rodt_skip)
  5818. continue;
  5819. temp_mask = rodt_perfect_counts.mask[i];
  5820. num_values = __builtin_popcountll(temp_mask);
  5821. i = __builtin_ffsll(temp_mask) - 1;
  5822. debug("N%d.LMC%d.R%d: PERFECT: RODT %3d: Byte %d: mask 0x%02llx (%d): ",
  5823. node, if_num, rankx,
  5824. imp_val->rodt_ohms[rodt_ctl],
  5825. i, temp_mask >> i, num_values);
  5826. while (temp_mask != 0) {
  5827. i = __builtin_ffsll(temp_mask) - 1;
  5828. debug("%2d(%2d) ", i,
  5829. rodt_perfect_counts.count[i][i]);
  5830. temp_mask &= ~(1UL << i);
  5831. } /* while (temp_mask != 0) */
  5832. debug("\n");
  5833. }
  5834. }
  5835. }
  5836. static void rank_major_loop(struct ddr_priv *priv, int rankx, struct rl_score
  5837. rl_score[RTT_NOM_OHMS_COUNT][RODT_OHMS_COUNT][4])
  5838. {
  5839. /* Start with an arbitrarily high score */
  5840. int best_rank_score = DEFAULT_BEST_RANK_SCORE;
  5841. int best_rank_rtt_nom = 0;
  5842. int best_rank_ctl = 0;
  5843. int best_rank_ohms = 0;
  5844. int best_rankx = 0;
  5845. int dimm_rank_mask;
  5846. int max_rank_score;
  5847. union cvmx_lmcx_rlevel_rankx saved_rl_rank;
  5848. int next_ohms;
  5849. int orankx;
  5850. int next_score = 0;
  5851. int best_byte, new_byte, temp_byte, orig_best_byte;
  5852. int rank_best_bytes[9];
  5853. int byte_sh;
  5854. int avg_byte;
  5855. int avg_diff;
  5856. int i;
  5857. if (!(rank_mask & (1 << rankx)))
  5858. return;
  5859. // some of the rank-related loops below need to operate only on
  5860. // the ranks of a single DIMM,
  5861. // so create a mask for their use here
  5862. if (num_ranks == 4) {
  5863. dimm_rank_mask = rank_mask; // should be 1111
  5864. } else {
  5865. dimm_rank_mask = rank_mask & 3; // should be 01 or 11
  5866. if (rankx >= 2) {
  5867. // doing a rank on the second DIMM, should be
  5868. // 0100 or 1100
  5869. dimm_rank_mask <<= 2;
  5870. }
  5871. }
  5872. debug("DIMM rank mask: 0x%x, rank mask: 0x%x, rankx: %d\n",
  5873. dimm_rank_mask, rank_mask, rankx);
  5874. // this is the start of the BEST ROW SCORE LOOP
  5875. for (rtt_idx = min_rtt_nom_idx; rtt_idx <= max_rtt_nom_idx; ++rtt_idx) {
  5876. rtt_nom = imp_val->rtt_nom_table[rtt_idx];
  5877. debug("N%d.LMC%d.R%d: starting RTT_NOM %d (%d)\n",
  5878. node, if_num, rankx, rtt_nom,
  5879. imp_val->rtt_nom_ohms[rtt_nom]);
  5880. for (rodt_ctl = max_rodt_ctl; rodt_ctl >= min_rodt_ctl;
  5881. --rodt_ctl) {
  5882. next_ohms = imp_val->rodt_ohms[rodt_ctl];
  5883. // skip RODT rows in mask, but *NOT* rows with too
  5884. // high a score;
  5885. // we will not use the skipped ones for printing or
  5886. // evaluating, but we need to allow all the
  5887. // non-skipped ones to be candidates for "best"
  5888. if (((1 << rodt_ctl) & rodt_row_skip_mask) != 0) {
  5889. debug("N%d.LMC%d.R%d: SKIPPING rodt:%d (%d) with rank_score:%d\n",
  5890. node, if_num, rankx, rodt_ctl,
  5891. next_ohms, next_score);
  5892. continue;
  5893. }
  5894. // this is ROFFIX-0528
  5895. for (orankx = 0; orankx < dimm_count * 4; orankx++) {
  5896. // stay on the same DIMM
  5897. if (!(dimm_rank_mask & (1 << orankx)))
  5898. continue;
  5899. next_score = rl_score[rtt_nom][rodt_ctl][orankx].score;
  5900. // always skip a higher score
  5901. if (next_score > best_rank_score)
  5902. continue;
  5903. // if scores are equal
  5904. if (next_score == best_rank_score) {
  5905. // always skip lower ohms
  5906. if (next_ohms < best_rank_ohms)
  5907. continue;
  5908. // if same ohms
  5909. if (next_ohms == best_rank_ohms) {
  5910. // always skip the other rank(s)
  5911. if (orankx != rankx)
  5912. continue;
  5913. }
  5914. // else next_ohms are greater,
  5915. // always choose it
  5916. }
  5917. // else next_score is less than current best,
  5918. // so always choose it
  5919. debug("N%d.LMC%d.R%d: new best score: rank %d, rodt %d(%3d), new best %d, previous best %d(%d)\n",
  5920. node, if_num, rankx, orankx, rodt_ctl, next_ohms, next_score,
  5921. best_rank_score, best_rank_ohms);
  5922. best_rank_score = next_score;
  5923. best_rank_rtt_nom = rtt_nom;
  5924. //best_rank_nom_ohms = rtt_nom_ohms;
  5925. best_rank_ctl = rodt_ctl;
  5926. best_rank_ohms = next_ohms;
  5927. best_rankx = orankx;
  5928. rl_rank.u64 =
  5929. rl_score[rtt_nom][rodt_ctl][orankx].setting;
  5930. }
  5931. }
  5932. }
  5933. // this is the end of the BEST ROW SCORE LOOP
  5934. // DANGER, Will Robinson!! Abort now if we did not find a best
  5935. // score at all...
  5936. if (best_rank_score == DEFAULT_BEST_RANK_SCORE) {
  5937. printf("N%d.LMC%d.R%d: WARNING: no best rank score found - resetting node...\n",
  5938. node, if_num, rankx);
  5939. mdelay(500);
  5940. do_reset(NULL, 0, 0, NULL);
  5941. }
  5942. // FIXME: relative now, but still arbitrary...
  5943. max_rank_score = best_rank_score;
  5944. if (ddr_type == DDR4_DRAM) {
  5945. // halve the range if 2 DIMMs unless they are single rank...
  5946. max_rank_score += (MAX_RANK_SCORE_LIMIT / ((num_ranks > 1) ?
  5947. dimm_count : 1));
  5948. } else {
  5949. // Since DDR3 typically has a wider score range,
  5950. // keep more of them always
  5951. max_rank_score += MAX_RANK_SCORE_LIMIT;
  5952. }
  5953. if (!ecc_ena) {
  5954. /* ECC is not used */
  5955. rl_rank.s.byte8 = rl_rank.s.byte0;
  5956. }
  5957. // at the end, write the best row settings to the current rank
  5958. lmc_wr(priv, CVMX_LMCX_RLEVEL_RANKX(rankx, if_num), rl_rank.u64);
  5959. rl_rank.u64 = lmc_rd(priv, CVMX_LMCX_RLEVEL_RANKX(rankx, if_num));
  5960. saved_rl_rank.u64 = rl_rank.u64;
  5961. // this is the start of the PRINT LOOP
  5962. int pass;
  5963. // for pass==0, print current rank, pass==1 print other rank(s)
  5964. // this is done because we want to show each ranks RODT values
  5965. // together, not interlaced
  5966. // keep separates for ranks - pass=0 target rank, pass=1 other
  5967. // rank on DIMM
  5968. int mask_skipped[2] = {0, 0};
  5969. int score_skipped[2] = {0, 0};
  5970. int selected_rows[2] = {0, 0};
  5971. int zero_scores[2] = {0, 0};
  5972. for (pass = 0; pass < 2; pass++) {
  5973. for (orankx = 0; orankx < dimm_count * 4; orankx++) {
  5974. // stay on the same DIMM
  5975. if (!(dimm_rank_mask & (1 << orankx)))
  5976. continue;
  5977. if ((pass == 0 && orankx != rankx) ||
  5978. (pass != 0 && orankx == rankx))
  5979. continue;
  5980. for (rtt_idx = min_rtt_nom_idx;
  5981. rtt_idx <= max_rtt_nom_idx; ++rtt_idx) {
  5982. rtt_nom = imp_val->rtt_nom_table[rtt_idx];
  5983. if (dyn_rtt_nom_mask == 0) {
  5984. print_nom_ohms = -1;
  5985. } else {
  5986. print_nom_ohms =
  5987. imp_val->rtt_nom_ohms[rtt_nom];
  5988. }
  5989. // cycle through all the RODT values...
  5990. for (rodt_ctl = max_rodt_ctl;
  5991. rodt_ctl >= min_rodt_ctl; --rodt_ctl) {
  5992. union cvmx_lmcx_rlevel_rankx
  5993. temp_rl_rank;
  5994. int temp_score =
  5995. rl_score[rtt_nom][rodt_ctl][orankx].score;
  5996. int skip_row;
  5997. temp_rl_rank.u64 =
  5998. rl_score[rtt_nom][rodt_ctl][orankx].setting;
  5999. // skip RODT rows in mask, or rows
  6000. // with too high a score;
  6001. // we will not use them for printing
  6002. // or evaluating...
  6003. if ((1 << rodt_ctl) &
  6004. rodt_row_skip_mask) {
  6005. skip_row = WITH_RODT_SKIPPING;
  6006. ++mask_skipped[pass];
  6007. } else if (temp_score >
  6008. max_rank_score) {
  6009. skip_row = WITH_RODT_SKIPPING;
  6010. ++score_skipped[pass];
  6011. } else {
  6012. skip_row = WITH_RODT_BLANK;
  6013. ++selected_rows[pass];
  6014. if (temp_score == 0)
  6015. ++zero_scores[pass];
  6016. }
  6017. // identify and print the BEST ROW
  6018. // when it comes up
  6019. if (skip_row == WITH_RODT_BLANK &&
  6020. best_rankx == orankx &&
  6021. best_rank_rtt_nom == rtt_nom &&
  6022. best_rank_ctl == rodt_ctl)
  6023. skip_row = WITH_RODT_BESTROW;
  6024. if (rl_print) {
  6025. display_rl_with_rodt(if_num,
  6026. temp_rl_rank, orankx, temp_score,
  6027. print_nom_ohms,
  6028. imp_val->rodt_ohms[rodt_ctl],
  6029. skip_row);
  6030. }
  6031. }
  6032. }
  6033. }
  6034. }
  6035. debug("N%d.LMC%d.R%d: RLROWS: selected %d+%d, zero_scores %d+%d, mask_skipped %d+%d, score_skipped %d+%d\n",
  6036. node, if_num, rankx, selected_rows[0], selected_rows[1],
  6037. zero_scores[0], zero_scores[1], mask_skipped[0], mask_skipped[1],
  6038. score_skipped[0], score_skipped[1]);
  6039. // this is the end of the PRINT LOOP
  6040. // now evaluate which bytes need adjusting
  6041. // collect the new byte values; first init with current best for
  6042. // neighbor use
  6043. for (i = 0, byte_sh = 0; i < 8 + ecc_ena; i++, byte_sh += 6) {
  6044. rank_best_bytes[i] = (int)(rl_rank.u64 >> byte_sh) &
  6045. RLEVEL_BYTE_MSK;
  6046. }
  6047. // this is the start of the BEST BYTE LOOP
  6048. for (i = 0, byte_sh = 0; i < 8 + ecc_ena; i++, byte_sh += 6) {
  6049. int sum = 0, count = 0;
  6050. int count_less = 0, count_same = 0, count_more = 0;
  6051. int count_byte; // save the value we counted around
  6052. // for rank majority use
  6053. int rank_less = 0, rank_same = 0, rank_more = 0;
  6054. int neighbor;
  6055. int neigh_byte;
  6056. best_byte = rank_best_bytes[i];
  6057. orig_best_byte = rank_best_bytes[i];
  6058. // this is the start of the BEST BYTE AVERAGING LOOP
  6059. // validate the initial "best" byte by looking at the
  6060. // average of the unskipped byte-column entries
  6061. // we want to do this before we go further, so we can
  6062. // try to start with a better initial value
  6063. // this is the so-called "BESTBUY" patch set
  6064. for (rtt_idx = min_rtt_nom_idx; rtt_idx <= max_rtt_nom_idx;
  6065. ++rtt_idx) {
  6066. rtt_nom = imp_val->rtt_nom_table[rtt_idx];
  6067. for (rodt_ctl = max_rodt_ctl; rodt_ctl >= min_rodt_ctl;
  6068. --rodt_ctl) {
  6069. union cvmx_lmcx_rlevel_rankx temp_rl_rank;
  6070. int temp_score;
  6071. // average over all the ranks
  6072. for (orankx = 0; orankx < dimm_count * 4;
  6073. orankx++) {
  6074. // stay on the same DIMM
  6075. if (!(dimm_rank_mask & (1 << orankx)))
  6076. continue;
  6077. temp_score =
  6078. rl_score[rtt_nom][rodt_ctl][orankx].score;
  6079. // skip RODT rows in mask, or rows with
  6080. // too high a score;
  6081. // we will not use them for printing or
  6082. // evaluating...
  6083. if (!((1 << rodt_ctl) &
  6084. rodt_row_skip_mask) &&
  6085. temp_score <= max_rank_score) {
  6086. temp_rl_rank.u64 =
  6087. rl_score[rtt_nom][rodt_ctl][orankx].setting;
  6088. temp_byte =
  6089. (int)(temp_rl_rank.u64 >> byte_sh) &
  6090. RLEVEL_BYTE_MSK;
  6091. sum += temp_byte;
  6092. count++;
  6093. }
  6094. }
  6095. }
  6096. }
  6097. // this is the end of the BEST BYTE AVERAGING LOOP
  6098. // FIXME: validate count and sum??
  6099. avg_byte = (int)divide_nint(sum, count);
  6100. avg_diff = best_byte - avg_byte;
  6101. new_byte = best_byte;
  6102. if (avg_diff != 0) {
  6103. // bump best up/dn by 1, not necessarily all the
  6104. // way to avg
  6105. new_byte = best_byte + ((avg_diff > 0) ? -1 : 1);
  6106. }
  6107. if (rl_print) {
  6108. debug("N%d.LMC%d.R%d: START: Byte %d: best %d is different by %d from average %d, using %d.\n",
  6109. node, if_num, rankx,
  6110. i, best_byte, avg_diff, avg_byte, new_byte);
  6111. }
  6112. best_byte = new_byte;
  6113. count_byte = new_byte; // save the value we will count around
  6114. // At this point best_byte is either:
  6115. // 1. the original byte-column value from the best scoring
  6116. // RODT row, OR
  6117. // 2. that value bumped toward the average of all the
  6118. // byte-column values
  6119. //
  6120. // best_byte will not change from here on...
  6121. // this is the start of the BEST BYTE COUNTING LOOP
  6122. // NOTE: we do this next loop separately from above, because
  6123. // we count relative to "best_byte"
  6124. // which may have been modified by the above averaging
  6125. // operation...
  6126. for (rtt_idx = min_rtt_nom_idx; rtt_idx <= max_rtt_nom_idx;
  6127. ++rtt_idx) {
  6128. rtt_nom = imp_val->rtt_nom_table[rtt_idx];
  6129. for (rodt_ctl = max_rodt_ctl; rodt_ctl >= min_rodt_ctl;
  6130. --rodt_ctl) {
  6131. union cvmx_lmcx_rlevel_rankx temp_rl_rank;
  6132. int temp_score;
  6133. for (orankx = 0; orankx < dimm_count * 4;
  6134. orankx++) { // count over all the ranks
  6135. // stay on the same DIMM
  6136. if (!(dimm_rank_mask & (1 << orankx)))
  6137. continue;
  6138. temp_score =
  6139. rl_score[rtt_nom][rodt_ctl][orankx].score;
  6140. // skip RODT rows in mask, or rows
  6141. // with too high a score;
  6142. // we will not use them for printing
  6143. // or evaluating...
  6144. if (((1 << rodt_ctl) &
  6145. rodt_row_skip_mask) ||
  6146. temp_score > max_rank_score)
  6147. continue;
  6148. temp_rl_rank.u64 =
  6149. rl_score[rtt_nom][rodt_ctl][orankx].setting;
  6150. temp_byte = (temp_rl_rank.u64 >>
  6151. byte_sh) & RLEVEL_BYTE_MSK;
  6152. if (temp_byte == 0)
  6153. ; // do not count it if illegal
  6154. else if (temp_byte == best_byte)
  6155. count_same++;
  6156. else if (temp_byte == best_byte - 1)
  6157. count_less++;
  6158. else if (temp_byte == best_byte + 1)
  6159. count_more++;
  6160. // else do not count anything more
  6161. // than 1 away from the best
  6162. // no rank counting if disabled
  6163. if (disable_rank_majority)
  6164. continue;
  6165. // FIXME? count is relative to
  6166. // best_byte; should it be rank-based?
  6167. // rank counts only on main rank
  6168. if (orankx != rankx)
  6169. continue;
  6170. else if (temp_byte == best_byte)
  6171. rank_same++;
  6172. else if (temp_byte == best_byte - 1)
  6173. rank_less++;
  6174. else if (temp_byte == best_byte + 1)
  6175. rank_more++;
  6176. }
  6177. }
  6178. }
  6179. if (rl_print) {
  6180. debug("N%d.LMC%d.R%d: COUNT: Byte %d: orig %d now %d, more %d same %d less %d (%d/%d/%d)\n",
  6181. node, if_num, rankx,
  6182. i, orig_best_byte, best_byte,
  6183. count_more, count_same, count_less,
  6184. rank_more, rank_same, rank_less);
  6185. }
  6186. // this is the end of the BEST BYTE COUNTING LOOP
  6187. // choose the new byte value
  6188. // we need to check that there is no gap greater than 2
  6189. // between adjacent bytes (adjacency depends on DIMM type)
  6190. // use the neighbor value to help decide
  6191. // initially, the rank_best_bytes[] will contain values from
  6192. // the chosen lowest score rank
  6193. new_byte = 0;
  6194. // neighbor is index-1 unless we are index 0 or index 8 (ECC)
  6195. neighbor = (i == 8) ? 3 : ((i == 0) ? 1 : i - 1);
  6196. neigh_byte = rank_best_bytes[neighbor];
  6197. // can go up or down or stay the same, so look at a numeric
  6198. // average to help
  6199. new_byte = (int)divide_nint(((count_more * (best_byte + 1)) +
  6200. (count_same * (best_byte + 0)) +
  6201. (count_less * (best_byte - 1))),
  6202. max(1, (count_more + count_same +
  6203. count_less)));
  6204. // use neighbor to help choose with average
  6205. if (i > 0 && (abs(neigh_byte - new_byte) > 2) &&
  6206. !disable_sequential_delay_check) {
  6207. // but not for byte 0
  6208. int avg_pick = new_byte;
  6209. if ((new_byte - best_byte) != 0) {
  6210. // back to best, average did not get better
  6211. new_byte = best_byte;
  6212. } else {
  6213. // avg was the same, still too far, now move
  6214. // it towards the neighbor
  6215. new_byte += (neigh_byte > new_byte) ? 1 : -1;
  6216. }
  6217. if (rl_print) {
  6218. debug("N%d.LMC%d.R%d: AVERAGE: Byte %d: neighbor %d too different %d from average %d, picking %d.\n",
  6219. node, if_num, rankx,
  6220. i, neighbor, neigh_byte, avg_pick,
  6221. new_byte);
  6222. }
  6223. } else {
  6224. // NOTE:
  6225. // For now, we let the neighbor processing above trump
  6226. // the new simple majority processing here.
  6227. // This is mostly because we have seen no smoking gun
  6228. // for a neighbor bad choice (yet?).
  6229. // Also note that we will ALWAYS be using byte 0
  6230. // majority, because of the if clause above.
  6231. // majority is dependent on the counts, which are
  6232. // relative to best_byte, so start there
  6233. int maj_byte = best_byte;
  6234. int rank_maj;
  6235. int rank_sum;
  6236. if (count_more > count_same &&
  6237. count_more > count_less) {
  6238. maj_byte++;
  6239. } else if (count_less > count_same &&
  6240. count_less > count_more) {
  6241. maj_byte--;
  6242. }
  6243. if (maj_byte != new_byte) {
  6244. // print only when majority choice is
  6245. // different from average
  6246. if (rl_print) {
  6247. debug("N%d.LMC%d.R%d: MAJORTY: Byte %d: picking majority of %d over average %d.\n",
  6248. node, if_num, rankx, i, maj_byte,
  6249. new_byte);
  6250. }
  6251. new_byte = maj_byte;
  6252. } else {
  6253. if (rl_print) {
  6254. debug("N%d.LMC%d.R%d: AVERAGE: Byte %d: picking average of %d.\n",
  6255. node, if_num, rankx, i, new_byte);
  6256. }
  6257. }
  6258. if (!disable_rank_majority) {
  6259. // rank majority is dependent on the rank
  6260. // counts, which are relative to best_byte,
  6261. // so start there, and adjust according to the
  6262. // rank counts majority
  6263. rank_maj = best_byte;
  6264. if (rank_more > rank_same &&
  6265. rank_more > rank_less) {
  6266. rank_maj++;
  6267. } else if (rank_less > rank_same &&
  6268. rank_less > rank_more) {
  6269. rank_maj--;
  6270. }
  6271. rank_sum = rank_more + rank_same + rank_less;
  6272. // now, let rank majority possibly rule over
  6273. // the current new_byte however we got it
  6274. if (rank_maj != new_byte) { // only if different
  6275. // Here is where we decide whether to
  6276. // completely apply RANK_MAJORITY or not
  6277. // ignore if less than
  6278. if (rank_maj < new_byte) {
  6279. if (rl_print) {
  6280. debug("N%d.LMC%d.R%d: RANKMAJ: Byte %d: LESS: NOT using %d over %d.\n",
  6281. node, if_num,
  6282. rankx, i,
  6283. rank_maj,
  6284. new_byte);
  6285. }
  6286. } else {
  6287. // For the moment, we do it
  6288. // ONLY when running 2-slot
  6289. // configs
  6290. // OR when rank_sum is big
  6291. // enough
  6292. if (dimm_count > 1 ||
  6293. rank_sum > 2) {
  6294. // print only when rank
  6295. // majority choice is
  6296. // selected
  6297. if (rl_print) {
  6298. debug("N%d.LMC%d.R%d: RANKMAJ: Byte %d: picking %d over %d.\n",
  6299. node,
  6300. if_num,
  6301. rankx,
  6302. i,
  6303. rank_maj,
  6304. new_byte);
  6305. }
  6306. new_byte = rank_maj;
  6307. } else {
  6308. // FIXME: print some
  6309. // info when we could
  6310. // have chosen RANKMAJ
  6311. // but did not
  6312. if (rl_print) {
  6313. debug("N%d.LMC%d.R%d: RANKMAJ: Byte %d: NOT using %d over %d (best=%d,sum=%d).\n",
  6314. node,
  6315. if_num,
  6316. rankx,
  6317. i,
  6318. rank_maj,
  6319. new_byte,
  6320. best_byte,
  6321. rank_sum);
  6322. }
  6323. }
  6324. }
  6325. }
  6326. } /* if (!disable_rank_majority) */
  6327. }
  6328. // one last check:
  6329. // if new_byte is still count_byte, BUT there was no count
  6330. // for that value, DO SOMETHING!!!
  6331. // FIXME: go back to original best byte from the best row
  6332. if (new_byte == count_byte && count_same == 0) {
  6333. new_byte = orig_best_byte;
  6334. if (rl_print) {
  6335. debug("N%d.LMC%d.R%d: FAILSAF: Byte %d: going back to original %d.\n",
  6336. node, if_num, rankx, i, new_byte);
  6337. }
  6338. }
  6339. // Look at counts for "perfect" bitmasks (PBMs) if we had
  6340. // any for this byte-lane.
  6341. // Remember, we only counted for DDR4, so zero means none
  6342. // or DDR3, and we bypass this...
  6343. value_mask = rank_perf[rankx].mask[i];
  6344. disable_rlv_bump_this_byte = 0;
  6345. if (value_mask != 0 && rl_ctl.cn78xx.offset == 1) {
  6346. int i, delay_count, delay_max = 0, del_val = 0;
  6347. int num_values = __builtin_popcountll(value_mask);
  6348. int sum_counts = 0;
  6349. u64 temp_mask = value_mask;
  6350. disable_rlv_bump_this_byte = 1;
  6351. i = __builtin_ffsll(temp_mask) - 1;
  6352. if (rl_print)
  6353. debug("N%d.LMC%d.R%d: PERFECT: Byte %d: OFF1: mask 0x%02llx (%d): ",
  6354. node, if_num, rankx, i, value_mask >> i,
  6355. num_values);
  6356. while (temp_mask != 0) {
  6357. i = __builtin_ffsll(temp_mask) - 1;
  6358. delay_count = rank_perf[rankx].count[i][i];
  6359. sum_counts += delay_count;
  6360. if (rl_print)
  6361. debug("%2d(%2d) ", i, delay_count);
  6362. if (delay_count >= delay_max) {
  6363. delay_max = delay_count;
  6364. del_val = i;
  6365. }
  6366. temp_mask &= ~(1UL << i);
  6367. } /* while (temp_mask != 0) */
  6368. // if sum_counts is small, just use NEW_BYTE
  6369. if (sum_counts < pbm_lowsum_limit) {
  6370. if (rl_print)
  6371. debug(": LOWSUM (%2d), choose ORIG ",
  6372. sum_counts);
  6373. del_val = new_byte;
  6374. delay_max = rank_perf[rankx].count[i][del_val];
  6375. }
  6376. // finish printing here...
  6377. if (rl_print) {
  6378. debug(": USING %2d (%2d) D%d\n", del_val,
  6379. delay_max, disable_rlv_bump_this_byte);
  6380. }
  6381. new_byte = del_val; // override with best PBM choice
  6382. } else if ((value_mask != 0) && (rl_ctl.cn78xx.offset == 2)) {
  6383. // if (value_mask != 0) {
  6384. int i, delay_count, del_val;
  6385. int num_values = __builtin_popcountll(value_mask);
  6386. int sum_counts = 0;
  6387. u64 temp_mask = value_mask;
  6388. i = __builtin_ffsll(temp_mask) - 1;
  6389. if (rl_print)
  6390. debug("N%d.LMC%d.R%d: PERFECT: Byte %d: mask 0x%02llx (%d): ",
  6391. node, if_num, rankx, i, value_mask >> i,
  6392. num_values);
  6393. while (temp_mask != 0) {
  6394. i = __builtin_ffsll(temp_mask) - 1;
  6395. delay_count = rank_perf[rankx].count[i][i];
  6396. sum_counts += delay_count;
  6397. if (rl_print)
  6398. debug("%2d(%2d) ", i, delay_count);
  6399. temp_mask &= ~(1UL << i);
  6400. } /* while (temp_mask != 0) */
  6401. del_val = __builtin_ffsll(value_mask) - 1;
  6402. delay_count =
  6403. rank_perf[rankx].count[i][del_val];
  6404. // overkill, normally only 1-4 bits
  6405. i = (value_mask >> del_val) & 0x1F;
  6406. // if sum_counts is small, treat as special and use
  6407. // NEW_BYTE
  6408. if (sum_counts < pbm_lowsum_limit) {
  6409. if (rl_print)
  6410. debug(": LOWSUM (%2d), choose ORIG",
  6411. sum_counts);
  6412. i = 99; // SPECIAL case...
  6413. }
  6414. switch (i) {
  6415. case 0x01 /* 00001b */:
  6416. // allow BUMP
  6417. break;
  6418. case 0x13 /* 10011b */:
  6419. case 0x0B /* 01011b */:
  6420. case 0x03 /* 00011b */:
  6421. del_val += 1; // take the second
  6422. disable_rlv_bump_this_byte = 1; // allow no BUMP
  6423. break;
  6424. case 0x0D /* 01101b */:
  6425. case 0x05 /* 00101b */:
  6426. // test count of lowest and all
  6427. if (delay_count >= 5 || sum_counts <= 5)
  6428. del_val += 1; // take the hole
  6429. else
  6430. del_val += 2; // take the next set
  6431. disable_rlv_bump_this_byte = 1; // allow no BUMP
  6432. break;
  6433. case 0x0F /* 01111b */:
  6434. case 0x17 /* 10111b */:
  6435. case 0x07 /* 00111b */:
  6436. del_val += 1; // take the second
  6437. if (delay_count < 5) { // lowest count is small
  6438. int second =
  6439. rank_perf[rankx].count[i][del_val];
  6440. int third =
  6441. rank_perf[rankx].count[i][del_val + 1];
  6442. // test if middle is more than 1 OR
  6443. // top is more than 1;
  6444. // this means if they are BOTH 1,
  6445. // then we keep the second...
  6446. if (second > 1 || third > 1) {
  6447. // if middle is small OR top
  6448. // is large
  6449. if (second < 5 ||
  6450. third > 1) {
  6451. // take the top
  6452. del_val += 1;
  6453. if (rl_print)
  6454. debug(": TOP7 ");
  6455. }
  6456. }
  6457. }
  6458. disable_rlv_bump_this_byte = 1; // allow no BUMP
  6459. break;
  6460. default: // all others...
  6461. if (rl_print)
  6462. debug(": ABNORMAL, choose ORIG");
  6463. case 99: // special
  6464. // FIXME: choose original choice?
  6465. del_val = new_byte;
  6466. disable_rlv_bump_this_byte = 1; // allow no BUMP
  6467. break;
  6468. }
  6469. delay_count =
  6470. rank_perf[rankx].count[i][del_val];
  6471. // finish printing here...
  6472. if (rl_print)
  6473. debug(": USING %2d (%2d) D%d\n", del_val,
  6474. delay_count, disable_rlv_bump_this_byte);
  6475. new_byte = del_val; // override with best PBM choice
  6476. } else {
  6477. if (ddr_type == DDR4_DRAM) { // only report when DDR4
  6478. // FIXME: remove or increase VBL for this
  6479. // output...
  6480. if (rl_print)
  6481. debug("N%d.LMC%d.R%d: PERFECT: Byte %d: ZERO PBMs, USING %d\n",
  6482. node, if_num, rankx, i,
  6483. new_byte);
  6484. // prevent ODD bump, rely on original
  6485. disable_rlv_bump_this_byte = 1;
  6486. }
  6487. } /* if (value_mask != 0) */
  6488. // optionally bump the delay value
  6489. if (enable_rldelay_bump && !disable_rlv_bump_this_byte) {
  6490. if ((new_byte & enable_rldelay_bump) ==
  6491. enable_rldelay_bump) {
  6492. int bump_value = new_byte + rldelay_bump_incr;
  6493. if (rl_print) {
  6494. debug("N%d.LMC%d.R%d: RLVBUMP: Byte %d: CHANGING %d to %d (%s)\n",
  6495. node, if_num, rankx, i,
  6496. new_byte, bump_value,
  6497. (value_mask &
  6498. (1 << bump_value)) ?
  6499. "PBM" : "NOPBM");
  6500. }
  6501. new_byte = bump_value;
  6502. }
  6503. }
  6504. // last checks for count-related purposes
  6505. if (new_byte == best_byte && count_more > 0 &&
  6506. count_less == 0) {
  6507. // we really should take best_byte + 1
  6508. if (rl_print) {
  6509. debug("N%d.LMC%d.R%d: CADJMOR: Byte %d: CHANGING %d to %d\n",
  6510. node, if_num, rankx, i,
  6511. new_byte, best_byte + 1);
  6512. new_byte = best_byte + 1;
  6513. }
  6514. } else if ((new_byte < best_byte) && (count_same > 0)) {
  6515. // we really should take best_byte
  6516. if (rl_print) {
  6517. debug("N%d.LMC%d.R%d: CADJSAM: Byte %d: CHANGING %d to %d\n",
  6518. node, if_num, rankx, i,
  6519. new_byte, best_byte);
  6520. new_byte = best_byte;
  6521. }
  6522. } else if (new_byte > best_byte) {
  6523. if ((new_byte == (best_byte + 1)) &&
  6524. count_more == 0 && count_less > 0) {
  6525. // we really should take best_byte
  6526. if (rl_print) {
  6527. debug("N%d.LMC%d.R%d: CADJLE1: Byte %d: CHANGING %d to %d\n",
  6528. node, if_num, rankx, i,
  6529. new_byte, best_byte);
  6530. new_byte = best_byte;
  6531. }
  6532. } else if ((new_byte >= (best_byte + 2)) &&
  6533. ((count_more > 0) || (count_same > 0))) {
  6534. if (rl_print) {
  6535. debug("N%d.LMC%d.R%d: CADJLE2: Byte %d: CHANGING %d to %d\n",
  6536. node, if_num, rankx, i,
  6537. new_byte, best_byte + 1);
  6538. new_byte = best_byte + 1;
  6539. }
  6540. }
  6541. }
  6542. if (rl_print) {
  6543. debug("N%d.LMC%d.R%d: SUMMARY: Byte %d: orig %d now %d, more %d same %d less %d, using %d\n",
  6544. node, if_num, rankx, i, orig_best_byte,
  6545. best_byte, count_more, count_same, count_less,
  6546. new_byte);
  6547. }
  6548. // update the byte with the new value (NOTE: orig value in
  6549. // the CSR may not be current "best")
  6550. upd_rl_rank(&rl_rank, i, new_byte);
  6551. // save new best for neighbor use
  6552. rank_best_bytes[i] = new_byte;
  6553. } /* for (i = 0; i < 8+ecc_ena; i++) */
  6554. ////////////////// this is the end of the BEST BYTE LOOP
  6555. if (saved_rl_rank.u64 != rl_rank.u64) {
  6556. lmc_wr(priv, CVMX_LMCX_RLEVEL_RANKX(rankx, if_num),
  6557. rl_rank.u64);
  6558. rl_rank.u64 = lmc_rd(priv,
  6559. CVMX_LMCX_RLEVEL_RANKX(rankx, if_num));
  6560. debug("Adjusting Read-Leveling per-RANK settings.\n");
  6561. } else {
  6562. debug("Not Adjusting Read-Leveling per-RANK settings.\n");
  6563. }
  6564. display_rl_with_final(if_num, rl_rank, rankx);
  6565. // FIXME: does this help make the output a little easier to focus?
  6566. if (rl_print > 0)
  6567. debug("-----------\n");
  6568. #define RLEVEL_RANKX_EXTRAS_INCR 0
  6569. // if there are unused entries to be filled
  6570. if ((rank_mask & 0x0f) != 0x0f) {
  6571. // copy the current rank
  6572. union cvmx_lmcx_rlevel_rankx temp_rl_rank = rl_rank;
  6573. if (rankx < 3) {
  6574. #if RLEVEL_RANKX_EXTRAS_INCR > 0
  6575. int byte, delay;
  6576. // modify the copy in prep for writing to empty slot(s)
  6577. for (byte = 0; byte < 9; byte++) {
  6578. delay = get_rl_rank(&temp_rl_rank, byte) +
  6579. RLEVEL_RANKX_EXTRAS_INCR;
  6580. if (delay > RLEVEL_BYTE_MSK)
  6581. delay = RLEVEL_BYTE_MSK;
  6582. upd_rl_rank(&temp_rl_rank, byte, delay);
  6583. }
  6584. #endif
  6585. // if rank 0, write rank 1 and rank 2 here if empty
  6586. if (rankx == 0) {
  6587. // check that rank 1 is empty
  6588. if (!(rank_mask & (1 << 1))) {
  6589. debug("N%d.LMC%d.R%d: writing RLEVEL_RANK unused entry R%d.\n",
  6590. node, if_num, rankx, 1);
  6591. lmc_wr(priv,
  6592. CVMX_LMCX_RLEVEL_RANKX(1,
  6593. if_num),
  6594. temp_rl_rank.u64);
  6595. }
  6596. // check that rank 2 is empty
  6597. if (!(rank_mask & (1 << 2))) {
  6598. debug("N%d.LMC%d.R%d: writing RLEVEL_RANK unused entry R%d.\n",
  6599. node, if_num, rankx, 2);
  6600. lmc_wr(priv,
  6601. CVMX_LMCX_RLEVEL_RANKX(2,
  6602. if_num),
  6603. temp_rl_rank.u64);
  6604. }
  6605. }
  6606. // if ranks 0, 1 or 2, write rank 3 here if empty
  6607. // check that rank 3 is empty
  6608. if (!(rank_mask & (1 << 3))) {
  6609. debug("N%d.LMC%d.R%d: writing RLEVEL_RANK unused entry R%d.\n",
  6610. node, if_num, rankx, 3);
  6611. lmc_wr(priv, CVMX_LMCX_RLEVEL_RANKX(3, if_num),
  6612. temp_rl_rank.u64);
  6613. }
  6614. }
  6615. }
  6616. }
  6617. static void lmc_read_leveling(struct ddr_priv *priv)
  6618. {
  6619. struct rl_score rl_score[RTT_NOM_OHMS_COUNT][RODT_OHMS_COUNT][4];
  6620. union cvmx_lmcx_control ctl;
  6621. union cvmx_lmcx_config cfg;
  6622. int rankx;
  6623. char *s;
  6624. int i;
  6625. /*
  6626. * 4.8.10 LMC Read Leveling
  6627. *
  6628. * LMC supports an automatic read-leveling separately per byte-lane
  6629. * using the DDR3 multipurpose register predefined pattern for system
  6630. * calibration defined in the JEDEC DDR3 specifications.
  6631. *
  6632. * All of DDR PLL, LMC CK, and LMC DRESET, and early LMC initializations
  6633. * must be completed prior to starting this LMC read-leveling sequence.
  6634. *
  6635. * Software could simply write the desired read-leveling values into
  6636. * LMC(0)_RLEVEL_RANK(0..3). This section describes a sequence that uses
  6637. * LMC's autoread-leveling capabilities.
  6638. *
  6639. * When LMC does the read-leveling sequence for a rank, it first enables
  6640. * the DDR3 multipurpose register predefined pattern for system
  6641. * calibration on the selected DRAM rank via a DDR3 MR3 write, then
  6642. * executes 64 RD operations at different internal delay settings, then
  6643. * disables the predefined pattern via another DDR3 MR3 write
  6644. * operation. LMC determines the pass or fail of each of the 64 settings
  6645. * independently for each byte lane, then writes appropriate
  6646. * LMC(0)_RLEVEL_RANK(0..3)[BYTE*] values for the rank.
  6647. *
  6648. * After read-leveling for a rank, software can read the 64 pass/fail
  6649. * indications for one byte lane via LMC(0)_RLEVEL_DBG[BITMASK].
  6650. * Software can observe all pass/fail results for all byte lanes in a
  6651. * rank via separate read-leveling sequences on the rank with different
  6652. * LMC(0)_RLEVEL_CTL[BYTE] values.
  6653. *
  6654. * The 64 pass/fail results will typically have failures for the low
  6655. * delays, followed by a run of some passing settings, followed by more
  6656. * failures in the remaining high delays. LMC sets
  6657. * LMC(0)_RLEVEL_RANK(0..3)[BYTE*] to one of the passing settings.
  6658. * First, LMC selects the longest run of successes in the 64 results.
  6659. * (In the unlikely event that there is more than one longest run, LMC
  6660. * selects the first one.) Then if LMC(0)_RLEVEL_CTL[OFFSET_EN] = 1 and
  6661. * the selected run has more than LMC(0)_RLEVEL_CTL[OFFSET] successes,
  6662. * LMC selects the last passing setting in the run minus
  6663. * LMC(0)_RLEVEL_CTL[OFFSET]. Otherwise LMC selects the middle setting
  6664. * in the run (rounding earlier when necessary). We expect the
  6665. * read-leveling sequence to produce good results with the reset values
  6666. * LMC(0)_RLEVEL_CTL [OFFSET_EN]=1, LMC(0)_RLEVEL_CTL[OFFSET] = 2.
  6667. *
  6668. * The read-leveling sequence has the following steps:
  6669. *
  6670. * 1. Select desired LMC(0)_RLEVEL_CTL[OFFSET_EN,OFFSET,BYTE] settings.
  6671. * Do the remaining substeps 2-4 separately for each rank i with
  6672. * attached DRAM.
  6673. *
  6674. * 2. Without changing any other fields in LMC(0)_CONFIG,
  6675. *
  6676. * o write LMC(0)_SEQ_CTL[SEQ_SEL] to select read-leveling
  6677. *
  6678. * o write LMC(0)_CONFIG[RANKMASK] = (1 << i)
  6679. *
  6680. * o write LMC(0)_SEQ_CTL[INIT_START] = 1
  6681. *
  6682. * This initiates the previously-described read-leveling.
  6683. *
  6684. * 3. Wait until LMC(0)_RLEVEL_RANKi[STATUS] != 2
  6685. *
  6686. * LMC will have updated LMC(0)_RLEVEL_RANKi[BYTE*] for all byte
  6687. * lanes at this point.
  6688. *
  6689. * If ECC DRAM is not present (i.e. when DRAM is not attached to the
  6690. * DDR_CBS_0_* and DDR_CB<7:0> chip signals, or the DDR_DQS_<4>_* and
  6691. * DDR_DQ<35:32> chip signals), write LMC(0)_RLEVEL_RANK*[BYTE8] =
  6692. * LMC(0)_RLEVEL_RANK*[BYTE0]. Write LMC(0)_RLEVEL_RANK*[BYTE4] =
  6693. * LMC(0)_RLEVEL_RANK*[BYTE0].
  6694. *
  6695. * 4. If desired, consult LMC(0)_RLEVEL_DBG[BITMASK] and compare to
  6696. * LMC(0)_RLEVEL_RANKi[BYTE*] for the lane selected by
  6697. * LMC(0)_RLEVEL_CTL[BYTE]. If desired, modify
  6698. * LMC(0)_RLEVEL_CTL[BYTE] to a new value and repeat so that all
  6699. * BITMASKs can be observed.
  6700. *
  6701. * 5. Initialize LMC(0)_RLEVEL_RANK* values for all unused ranks.
  6702. *
  6703. * Let rank i be a rank with attached DRAM.
  6704. *
  6705. * For all ranks j that do not have attached DRAM, set
  6706. * LMC(0)_RLEVEL_RANKj = LMC(0)_RLEVEL_RANKi.
  6707. *
  6708. * This read-leveling sequence can help select the proper CN70XX ODT
  6709. * resistance value (LMC(0)_COMP_CTL2[RODT_CTL]). A hardware-generated
  6710. * LMC(0)_RLEVEL_RANKi[BYTEj] value (for a used byte lane j) that is
  6711. * drastically different from a neighboring LMC(0)_RLEVEL_RANKi[BYTEk]
  6712. * (for a used byte lane k) can indicate that the CN70XX ODT value is
  6713. * bad. It is possible to simultaneously optimize both
  6714. * LMC(0)_COMP_CTL2[RODT_CTL] and LMC(0)_RLEVEL_RANKn[BYTE*] values by
  6715. * performing this read-leveling sequence for several
  6716. * LMC(0)_COMP_CTL2[RODT_CTL] values and selecting the one with the
  6717. * best LMC(0)_RLEVEL_RANKn[BYTE*] profile for the ranks.
  6718. */
  6719. rl_rodt_err = 0;
  6720. rl_dbg_loops = 1;
  6721. saved_int_zqcs_dis = 0;
  6722. max_adj_rl_del_inc = 0;
  6723. rl_print = RLEVEL_PRINTALL_DEFAULT;
  6724. #ifdef ENABLE_HARDCODED_RLEVEL
  6725. part_number[21] = {0};
  6726. #endif /* ENABLE_HARDCODED_RLEVEL */
  6727. pbm_lowsum_limit = 5; // FIXME: is this a good default?
  6728. // FIXME: PBM skip for RODT 240 and 34
  6729. pbm_rodt_skip = (1U << ddr4_rodt_ctl_240_ohm) |
  6730. (1U << ddr4_rodt_ctl_34_ohm);
  6731. disable_rank_majority = 0; // control rank majority processing
  6732. // default to mask 11b ODDs for DDR4 (except 73xx), else DISABLE
  6733. // for DDR3
  6734. rldelay_bump_incr = 0;
  6735. disable_rlv_bump_this_byte = 0;
  6736. enable_rldelay_bump = (ddr_type == DDR4_DRAM) ?
  6737. ((octeon_is_cpuid(OCTEON_CN73XX)) ? 1 : 3) : 0;
  6738. s = lookup_env(priv, "ddr_disable_rank_majority");
  6739. if (s)
  6740. disable_rank_majority = !!simple_strtoul(s, NULL, 0);
  6741. s = lookup_env(priv, "ddr_pbm_lowsum_limit");
  6742. if (s)
  6743. pbm_lowsum_limit = simple_strtoul(s, NULL, 0);
  6744. s = lookup_env(priv, "ddr_pbm_rodt_skip");
  6745. if (s)
  6746. pbm_rodt_skip = simple_strtoul(s, NULL, 0);
  6747. memset(rank_perf, 0, sizeof(rank_perf));
  6748. ctl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  6749. save_ddr2t = ctl.cn78xx.ddr2t;
  6750. cfg.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(if_num));
  6751. ecc_ena = cfg.cn78xx.ecc_ena;
  6752. s = lookup_env(priv, "ddr_rlevel_2t");
  6753. if (s)
  6754. ctl.cn78xx.ddr2t = simple_strtoul(s, NULL, 0);
  6755. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctl.u64);
  6756. debug("LMC%d: Performing Read-Leveling\n", if_num);
  6757. rl_ctl.u64 = lmc_rd(priv, CVMX_LMCX_RLEVEL_CTL(if_num));
  6758. rl_samples = c_cfg->rlevel_average_loops;
  6759. if (rl_samples == 0) {
  6760. rl_samples = RLEVEL_SAMPLES_DEFAULT;
  6761. // up the samples for these cases
  6762. if (dimm_count == 1 || num_ranks == 1)
  6763. rl_samples = rl_samples * 2 + 1;
  6764. }
  6765. rl_compute = c_cfg->rlevel_compute;
  6766. rl_ctl.cn78xx.offset_en = c_cfg->offset_en;
  6767. rl_ctl.cn78xx.offset = spd_rdimm
  6768. ? c_cfg->offset_rdimm
  6769. : c_cfg->offset_udimm;
  6770. int value = 1; // should ALWAYS be set
  6771. s = lookup_env(priv, "ddr_rlevel_delay_unload");
  6772. if (s)
  6773. value = !!simple_strtoul(s, NULL, 0);
  6774. rl_ctl.cn78xx.delay_unload_0 = value;
  6775. rl_ctl.cn78xx.delay_unload_1 = value;
  6776. rl_ctl.cn78xx.delay_unload_2 = value;
  6777. rl_ctl.cn78xx.delay_unload_3 = value;
  6778. // use OR_DIS=1 to try for better results
  6779. rl_ctl.cn78xx.or_dis = 1;
  6780. /*
  6781. * If we will be switching to 32bit mode level based on only
  6782. * four bits because there are only 4 ECC bits.
  6783. */
  6784. rl_ctl.cn78xx.bitmask = (if_64b) ? 0xFF : 0x0F;
  6785. // allow overrides
  6786. s = lookup_env(priv, "ddr_rlevel_ctl_or_dis");
  6787. if (s)
  6788. rl_ctl.cn78xx.or_dis = simple_strtoul(s, NULL, 0);
  6789. s = lookup_env(priv, "ddr_rlevel_ctl_bitmask");
  6790. if (s)
  6791. rl_ctl.cn78xx.bitmask = simple_strtoul(s, NULL, 0);
  6792. rl_comp_offs = spd_rdimm
  6793. ? c_cfg->rlevel_comp_offset_rdimm
  6794. : c_cfg->rlevel_comp_offset_udimm;
  6795. s = lookup_env(priv, "ddr_rlevel_comp_offset");
  6796. if (s)
  6797. rl_comp_offs = strtoul(s, NULL, 0);
  6798. s = lookup_env(priv, "ddr_rlevel_offset");
  6799. if (s)
  6800. rl_ctl.cn78xx.offset = simple_strtoul(s, NULL, 0);
  6801. s = lookup_env(priv, "ddr_rlevel_offset_en");
  6802. if (s)
  6803. rl_ctl.cn78xx.offset_en = simple_strtoul(s, NULL, 0);
  6804. s = lookup_env(priv, "ddr_rlevel_ctl");
  6805. if (s)
  6806. rl_ctl.u64 = simple_strtoul(s, NULL, 0);
  6807. lmc_wr(priv,
  6808. CVMX_LMCX_RLEVEL_CTL(if_num),
  6809. rl_ctl.u64);
  6810. // do this here so we can look at final RLEVEL_CTL[offset] setting...
  6811. s = lookup_env(priv, "ddr_enable_rldelay_bump");
  6812. if (s) {
  6813. // also use as mask bits
  6814. enable_rldelay_bump = strtoul(s, NULL, 0);
  6815. }
  6816. if (enable_rldelay_bump != 0)
  6817. rldelay_bump_incr = (rl_ctl.cn78xx.offset == 1) ? -1 : 1;
  6818. s = lookup_env(priv, "ddr%d_rlevel_debug_loops", if_num);
  6819. if (s)
  6820. rl_dbg_loops = simple_strtoul(s, NULL, 0);
  6821. s = lookup_env(priv, "ddr_rtt_nom_auto");
  6822. if (s)
  6823. ddr_rtt_nom_auto = !!simple_strtoul(s, NULL, 0);
  6824. s = lookup_env(priv, "ddr_rlevel_average");
  6825. if (s)
  6826. rl_samples = simple_strtoul(s, NULL, 0);
  6827. s = lookup_env(priv, "ddr_rlevel_compute");
  6828. if (s)
  6829. rl_compute = simple_strtoul(s, NULL, 0);
  6830. s = lookup_env(priv, "ddr_rlevel_printall");
  6831. if (s)
  6832. rl_print = simple_strtoul(s, NULL, 0);
  6833. debug("RLEVEL_CTL : 0x%016llx\n",
  6834. rl_ctl.u64);
  6835. debug("RLEVEL_OFFSET : %6d\n",
  6836. rl_ctl.cn78xx.offset);
  6837. debug("RLEVEL_OFFSET_EN : %6d\n",
  6838. rl_ctl.cn78xx.offset_en);
  6839. /*
  6840. * The purpose for the indexed table is to sort the settings
  6841. * by the ohm value to simplify the testing when incrementing
  6842. * through the settings. (index => ohms) 1=120, 2=60, 3=40,
  6843. * 4=30, 5=20
  6844. */
  6845. min_rtt_nom_idx = (c_cfg->min_rtt_nom_idx == 0) ?
  6846. 1 : c_cfg->min_rtt_nom_idx;
  6847. max_rtt_nom_idx = (c_cfg->max_rtt_nom_idx == 0) ?
  6848. 5 : c_cfg->max_rtt_nom_idx;
  6849. min_rodt_ctl = (c_cfg->min_rodt_ctl == 0) ? 1 : c_cfg->min_rodt_ctl;
  6850. max_rodt_ctl = (c_cfg->max_rodt_ctl == 0) ? 5 : c_cfg->max_rodt_ctl;
  6851. s = lookup_env(priv, "ddr_min_rodt_ctl");
  6852. if (s)
  6853. min_rodt_ctl = simple_strtoul(s, NULL, 0);
  6854. s = lookup_env(priv, "ddr_max_rodt_ctl");
  6855. if (s)
  6856. max_rodt_ctl = simple_strtoul(s, NULL, 0);
  6857. s = lookup_env(priv, "ddr_min_rtt_nom_idx");
  6858. if (s)
  6859. min_rtt_nom_idx = simple_strtoul(s, NULL, 0);
  6860. s = lookup_env(priv, "ddr_max_rtt_nom_idx");
  6861. if (s)
  6862. max_rtt_nom_idx = simple_strtoul(s, NULL, 0);
  6863. #ifdef ENABLE_HARDCODED_RLEVEL
  6864. if (c_cfg->rl_tbl) {
  6865. /* Check for hard-coded read-leveling settings */
  6866. get_dimm_part_number(part_number, &dimm_config_table[0],
  6867. 0, ddr_type);
  6868. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  6869. if (!(rank_mask & (1 << rankx)))
  6870. continue;
  6871. rl_rank.u64 = lmc_rd(priv,
  6872. CVMX_LMCX_RLEVEL_RANKX(rankx,
  6873. if_num));
  6874. i = 0;
  6875. while (c_cfg->rl_tbl[i].part) {
  6876. debug("DIMM part number:\"%s\", SPD: \"%s\"\n",
  6877. c_cfg->rl_tbl[i].part, part_number);
  6878. if ((strcmp(part_number,
  6879. c_cfg->rl_tbl[i].part) == 0) &&
  6880. (abs(c_cfg->rl_tbl[i].speed -
  6881. 2 * ddr_hertz / (1000 * 1000)) < 10)) {
  6882. debug("Using hard-coded read leveling for DIMM part number: \"%s\"\n",
  6883. part_number);
  6884. rl_rank.u64 =
  6885. c_cfg->rl_tbl[i].rl_rank[if_num][rankx];
  6886. lmc_wr(priv,
  6887. CVMX_LMCX_RLEVEL_RANKX(rankx,
  6888. if_num),
  6889. rl_rank.u64);
  6890. rl_rank.u64 =
  6891. lmc_rd(priv,
  6892. CVMX_LMCX_RLEVEL_RANKX(rankx,
  6893. if_num));
  6894. display_rl(if_num, rl_rank, rankx);
  6895. /* Disable h/w read-leveling */
  6896. rl_dbg_loops = 0;
  6897. break;
  6898. }
  6899. ++i;
  6900. }
  6901. }
  6902. }
  6903. #endif /* ENABLE_HARDCODED_RLEVEL */
  6904. max_adj_rl_del_inc = c_cfg->maximum_adjacent_rlevel_delay_increment;
  6905. s = lookup_env(priv, "ddr_maximum_adjacent_rlevel_delay_increment");
  6906. if (s)
  6907. max_adj_rl_del_inc = strtoul(s, NULL, 0);
  6908. while (rl_dbg_loops--) {
  6909. union cvmx_lmcx_modereg_params1 mp1;
  6910. union cvmx_lmcx_comp_ctl2 cc2;
  6911. /* Initialize the error scoreboard */
  6912. memset(rl_score, 0, sizeof(rl_score));
  6913. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  6914. saved_ddr__ptune = cc2.cn78xx.ddr__ptune;
  6915. saved_ddr__ntune = cc2.cn78xx.ddr__ntune;
  6916. /* Disable dynamic compensation settings */
  6917. if (rl_comp_offs != 0) {
  6918. cc2.cn78xx.ptune = saved_ddr__ptune;
  6919. cc2.cn78xx.ntune = saved_ddr__ntune;
  6920. /*
  6921. * Round up the ptune calculation to bias the odd
  6922. * cases toward ptune
  6923. */
  6924. cc2.cn78xx.ptune += divide_roundup(rl_comp_offs, 2);
  6925. cc2.cn78xx.ntune -= rl_comp_offs / 2;
  6926. ctl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  6927. saved_int_zqcs_dis = ctl.s.int_zqcs_dis;
  6928. /* Disable ZQCS while in bypass. */
  6929. ctl.s.int_zqcs_dis = 1;
  6930. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctl.u64);
  6931. cc2.cn78xx.byp = 1; /* Enable bypass mode */
  6932. lmc_wr(priv, CVMX_LMCX_COMP_CTL2(if_num), cc2.u64);
  6933. lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  6934. /* Read again */
  6935. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  6936. debug("DDR__PTUNE/DDR__NTUNE : %d/%d\n",
  6937. cc2.cn78xx.ddr__ptune, cc2.cn78xx.ddr__ntune);
  6938. }
  6939. mp1.u64 = lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS1(if_num));
  6940. for (rtt_idx = min_rtt_nom_idx; rtt_idx <= max_rtt_nom_idx;
  6941. ++rtt_idx) {
  6942. rtt_nom = imp_val->rtt_nom_table[rtt_idx];
  6943. /*
  6944. * When the read ODT mask is zero the dyn_rtt_nom_mask
  6945. * is zero than RTT_NOM will not be changing during
  6946. * read-leveling. Since the value is fixed we only need
  6947. * to test it once.
  6948. */
  6949. if (dyn_rtt_nom_mask == 0) {
  6950. // flag not to print NOM ohms
  6951. print_nom_ohms = -1;
  6952. } else {
  6953. if (dyn_rtt_nom_mask & 1)
  6954. mp1.s.rtt_nom_00 = rtt_nom;
  6955. if (dyn_rtt_nom_mask & 2)
  6956. mp1.s.rtt_nom_01 = rtt_nom;
  6957. if (dyn_rtt_nom_mask & 4)
  6958. mp1.s.rtt_nom_10 = rtt_nom;
  6959. if (dyn_rtt_nom_mask & 8)
  6960. mp1.s.rtt_nom_11 = rtt_nom;
  6961. // FIXME? rank 0 ohms always?
  6962. print_nom_ohms =
  6963. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_00];
  6964. }
  6965. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS1(if_num),
  6966. mp1.u64);
  6967. if (print_nom_ohms >= 0 && rl_print > 1) {
  6968. debug("\n");
  6969. debug("RTT_NOM %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  6970. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_11],
  6971. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_10],
  6972. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_01],
  6973. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_00],
  6974. mp1.s.rtt_nom_11,
  6975. mp1.s.rtt_nom_10,
  6976. mp1.s.rtt_nom_01,
  6977. mp1.s.rtt_nom_00);
  6978. }
  6979. ddr_init_seq(priv, rank_mask, if_num);
  6980. // Try RANK outside RODT to rearrange the output...
  6981. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  6982. if (!(rank_mask & (1 << rankx)))
  6983. continue;
  6984. for (rodt_ctl = max_rodt_ctl;
  6985. rodt_ctl >= min_rodt_ctl; --rodt_ctl)
  6986. rodt_loop(priv, rankx, rl_score);
  6987. }
  6988. }
  6989. /* Re-enable dynamic compensation settings. */
  6990. if (rl_comp_offs != 0) {
  6991. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  6992. cc2.cn78xx.ptune = 0;
  6993. cc2.cn78xx.ntune = 0;
  6994. cc2.cn78xx.byp = 0; /* Disable bypass mode */
  6995. lmc_wr(priv, CVMX_LMCX_COMP_CTL2(if_num), cc2.u64);
  6996. /* Read once */
  6997. lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  6998. /* Read again */
  6999. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  7000. debug("DDR__PTUNE/DDR__NTUNE : %d/%d\n",
  7001. cc2.cn78xx.ddr__ptune, cc2.cn78xx.ddr__ntune);
  7002. ctl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  7003. /* Restore original setting */
  7004. ctl.s.int_zqcs_dis = saved_int_zqcs_dis;
  7005. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctl.u64);
  7006. }
  7007. int override_compensation = 0;
  7008. s = lookup_env(priv, "ddr__ptune");
  7009. if (s)
  7010. saved_ddr__ptune = strtoul(s, NULL, 0);
  7011. s = lookup_env(priv, "ddr__ntune");
  7012. if (s) {
  7013. saved_ddr__ntune = strtoul(s, NULL, 0);
  7014. override_compensation = 1;
  7015. }
  7016. if (override_compensation) {
  7017. cc2.cn78xx.ptune = saved_ddr__ptune;
  7018. cc2.cn78xx.ntune = saved_ddr__ntune;
  7019. ctl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  7020. saved_int_zqcs_dis = ctl.s.int_zqcs_dis;
  7021. /* Disable ZQCS while in bypass. */
  7022. ctl.s.int_zqcs_dis = 1;
  7023. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctl.u64);
  7024. cc2.cn78xx.byp = 1; /* Enable bypass mode */
  7025. lmc_wr(priv, CVMX_LMCX_COMP_CTL2(if_num), cc2.u64);
  7026. /* Read again */
  7027. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  7028. debug("DDR__PTUNE/DDR__NTUNE : %d/%d\n",
  7029. cc2.cn78xx.ptune, cc2.cn78xx.ntune);
  7030. }
  7031. /* Evaluation block */
  7032. /* Still at initial value? */
  7033. int best_rodt_score = DEFAULT_BEST_RANK_SCORE;
  7034. int auto_rodt_ctl = 0;
  7035. int auto_rtt_nom = 0;
  7036. int rodt_score;
  7037. rodt_row_skip_mask = 0;
  7038. // just add specific RODT rows to the skip mask for DDR4
  7039. // at this time...
  7040. if (ddr_type == DDR4_DRAM) {
  7041. // skip RODT row 34 ohms for all DDR4 types
  7042. rodt_row_skip_mask |= (1 << ddr4_rodt_ctl_34_ohm);
  7043. // skip RODT row 40 ohms for all DDR4 types
  7044. rodt_row_skip_mask |= (1 << ddr4_rodt_ctl_40_ohm);
  7045. // For now, do not skip RODT row 40 or 48 ohm when
  7046. // ddr_hertz is above 1075 MHz
  7047. if (ddr_hertz > 1075000000) {
  7048. // noskip RODT row 40 ohms
  7049. rodt_row_skip_mask &=
  7050. ~(1 << ddr4_rodt_ctl_40_ohm);
  7051. // noskip RODT row 48 ohms
  7052. rodt_row_skip_mask &=
  7053. ~(1 << ddr4_rodt_ctl_48_ohm);
  7054. }
  7055. // For now, do not skip RODT row 48 ohm for 2Rx4
  7056. // stacked die DIMMs
  7057. if (is_stacked_die && num_ranks == 2 &&
  7058. dram_width == 4) {
  7059. // noskip RODT row 48 ohms
  7060. rodt_row_skip_mask &=
  7061. ~(1 << ddr4_rodt_ctl_48_ohm);
  7062. }
  7063. // for now, leave all rows eligible when we have
  7064. // mini-DIMMs...
  7065. if (spd_dimm_type == 5 || spd_dimm_type == 6)
  7066. rodt_row_skip_mask = 0;
  7067. // for now, leave all rows eligible when we have
  7068. // a 2-slot 1-rank config
  7069. if (dimm_count == 2 && num_ranks == 1)
  7070. rodt_row_skip_mask = 0;
  7071. debug("Evaluating Read-Leveling Scoreboard for AUTO settings.\n");
  7072. for (rtt_idx = min_rtt_nom_idx;
  7073. rtt_idx <= max_rtt_nom_idx; ++rtt_idx) {
  7074. rtt_nom = imp_val->rtt_nom_table[rtt_idx];
  7075. for (rodt_ctl = max_rodt_ctl;
  7076. rodt_ctl >= min_rodt_ctl; --rodt_ctl) {
  7077. rodt_score = 0;
  7078. for (rankx = 0; rankx < dimm_count * 4;
  7079. rankx++) {
  7080. if (!(rank_mask & (1 << rankx)))
  7081. continue;
  7082. debug("rl_score[rtt_nom=%d][rodt_ctl=%d][rankx=%d].score:%d\n",
  7083. rtt_nom, rodt_ctl, rankx,
  7084. rl_score[rtt_nom][rodt_ctl][rankx].score);
  7085. rodt_score +=
  7086. rl_score[rtt_nom][rodt_ctl][rankx].score;
  7087. }
  7088. // FIXME: do we need to skip RODT rows
  7089. // here, like we do below in the
  7090. // by-RANK settings?
  7091. /*
  7092. * When using automatic ODT settings use
  7093. * the ODT settings associated with the
  7094. * best score for all of the tested ODT
  7095. * combinations.
  7096. */
  7097. if (rodt_score < best_rodt_score ||
  7098. (rodt_score == best_rodt_score &&
  7099. (imp_val->rodt_ohms[rodt_ctl] >
  7100. imp_val->rodt_ohms[auto_rodt_ctl]))) {
  7101. debug("AUTO: new best score for rodt:%d (%d), new score:%d, previous score:%d\n",
  7102. rodt_ctl,
  7103. imp_val->rodt_ohms[rodt_ctl],
  7104. rodt_score,
  7105. best_rodt_score);
  7106. best_rodt_score = rodt_score;
  7107. auto_rodt_ctl = rodt_ctl;
  7108. auto_rtt_nom = rtt_nom;
  7109. }
  7110. }
  7111. }
  7112. mp1.u64 = lmc_rd(priv,
  7113. CVMX_LMCX_MODEREG_PARAMS1(if_num));
  7114. if (ddr_rtt_nom_auto) {
  7115. /* Store the automatically set RTT_NOM value */
  7116. if (dyn_rtt_nom_mask & 1)
  7117. mp1.s.rtt_nom_00 = auto_rtt_nom;
  7118. if (dyn_rtt_nom_mask & 2)
  7119. mp1.s.rtt_nom_01 = auto_rtt_nom;
  7120. if (dyn_rtt_nom_mask & 4)
  7121. mp1.s.rtt_nom_10 = auto_rtt_nom;
  7122. if (dyn_rtt_nom_mask & 8)
  7123. mp1.s.rtt_nom_11 = auto_rtt_nom;
  7124. } else {
  7125. /*
  7126. * restore the manual settings to the register
  7127. */
  7128. mp1.s.rtt_nom_00 = default_rtt_nom[0];
  7129. mp1.s.rtt_nom_01 = default_rtt_nom[1];
  7130. mp1.s.rtt_nom_10 = default_rtt_nom[2];
  7131. mp1.s.rtt_nom_11 = default_rtt_nom[3];
  7132. }
  7133. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS1(if_num),
  7134. mp1.u64);
  7135. debug("RTT_NOM %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  7136. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_11],
  7137. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_10],
  7138. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_01],
  7139. imp_val->rtt_nom_ohms[mp1.s.rtt_nom_00],
  7140. mp1.s.rtt_nom_11,
  7141. mp1.s.rtt_nom_10,
  7142. mp1.s.rtt_nom_01,
  7143. mp1.s.rtt_nom_00);
  7144. debug("RTT_WR %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  7145. imp_val->rtt_wr_ohms[extr_wr(mp1.u64, 3)],
  7146. imp_val->rtt_wr_ohms[extr_wr(mp1.u64, 2)],
  7147. imp_val->rtt_wr_ohms[extr_wr(mp1.u64, 1)],
  7148. imp_val->rtt_wr_ohms[extr_wr(mp1.u64, 0)],
  7149. extr_wr(mp1.u64, 3),
  7150. extr_wr(mp1.u64, 2),
  7151. extr_wr(mp1.u64, 1),
  7152. extr_wr(mp1.u64, 0));
  7153. debug("DIC %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  7154. imp_val->dic_ohms[mp1.s.dic_11],
  7155. imp_val->dic_ohms[mp1.s.dic_10],
  7156. imp_val->dic_ohms[mp1.s.dic_01],
  7157. imp_val->dic_ohms[mp1.s.dic_00],
  7158. mp1.s.dic_11,
  7159. mp1.s.dic_10,
  7160. mp1.s.dic_01,
  7161. mp1.s.dic_00);
  7162. if (ddr_type == DDR4_DRAM) {
  7163. union cvmx_lmcx_modereg_params2 mp2;
  7164. /*
  7165. * We must read the CSR, and not depend on
  7166. * odt_config[odt_idx].odt_mask2, since we could
  7167. * have overridden values with envvars.
  7168. * NOTE: this corrects the printout, since the
  7169. * CSR is not written with the old values...
  7170. */
  7171. mp2.u64 = lmc_rd(priv,
  7172. CVMX_LMCX_MODEREG_PARAMS2(if_num));
  7173. debug("RTT_PARK %3d, %3d, %3d, %3d ohms : %x,%x,%x,%x\n",
  7174. imp_val->rtt_nom_ohms[mp2.s.rtt_park_11],
  7175. imp_val->rtt_nom_ohms[mp2.s.rtt_park_10],
  7176. imp_val->rtt_nom_ohms[mp2.s.rtt_park_01],
  7177. imp_val->rtt_nom_ohms[mp2.s.rtt_park_00],
  7178. mp2.s.rtt_park_11,
  7179. mp2.s.rtt_park_10,
  7180. mp2.s.rtt_park_01,
  7181. mp2.s.rtt_park_00);
  7182. debug("%-45s : 0x%x,0x%x,0x%x,0x%x\n",
  7183. "VREF_RANGE",
  7184. mp2.s.vref_range_11,
  7185. mp2.s.vref_range_10,
  7186. mp2.s.vref_range_01,
  7187. mp2.s.vref_range_00);
  7188. debug("%-45s : 0x%x,0x%x,0x%x,0x%x\n",
  7189. "VREF_VALUE",
  7190. mp2.s.vref_value_11,
  7191. mp2.s.vref_value_10,
  7192. mp2.s.vref_value_01,
  7193. mp2.s.vref_value_00);
  7194. }
  7195. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  7196. if (ddr_rodt_ctl_auto) {
  7197. cc2.cn78xx.rodt_ctl = auto_rodt_ctl;
  7198. } else {
  7199. // back to the original setting
  7200. cc2.cn78xx.rodt_ctl = default_rodt_ctl;
  7201. }
  7202. lmc_wr(priv, CVMX_LMCX_COMP_CTL2(if_num), cc2.u64);
  7203. cc2.u64 = lmc_rd(priv, CVMX_LMCX_COMP_CTL2(if_num));
  7204. debug("Read ODT_CTL : 0x%x (%d ohms)\n",
  7205. cc2.cn78xx.rodt_ctl,
  7206. imp_val->rodt_ohms[cc2.cn78xx.rodt_ctl]);
  7207. /*
  7208. * Use the delays associated with the best score for
  7209. * each individual rank
  7210. */
  7211. debug("Evaluating Read-Leveling Scoreboard for per-RANK settings.\n");
  7212. // this is the the RANK MAJOR LOOP
  7213. for (rankx = 0; rankx < dimm_count * 4; rankx++)
  7214. rank_major_loop(priv, rankx, rl_score);
  7215. } /* Evaluation block */
  7216. } /* while(rl_dbg_loops--) */
  7217. ctl.cn78xx.ddr2t = save_ddr2t;
  7218. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctl.u64);
  7219. ctl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  7220. /* Display final 2T value */
  7221. debug("DDR2T : %6d\n",
  7222. ctl.cn78xx.ddr2t);
  7223. ddr_init_seq(priv, rank_mask, if_num);
  7224. for (rankx = 0; rankx < dimm_count * 4; rankx++) {
  7225. u64 value;
  7226. int parameter_set = 0;
  7227. if (!(rank_mask & (1 << rankx)))
  7228. continue;
  7229. rl_rank.u64 = lmc_rd(priv, CVMX_LMCX_RLEVEL_RANKX(rankx,
  7230. if_num));
  7231. for (i = 0; i < 9; ++i) {
  7232. s = lookup_env(priv, "ddr%d_rlevel_rank%d_byte%d",
  7233. if_num, rankx, i);
  7234. if (s) {
  7235. parameter_set |= 1;
  7236. value = simple_strtoul(s, NULL, 0);
  7237. upd_rl_rank(&rl_rank, i, value);
  7238. }
  7239. }
  7240. s = lookup_env_ull(priv, "ddr%d_rlevel_rank%d", if_num, rankx);
  7241. if (s) {
  7242. parameter_set |= 1;
  7243. value = simple_strtoull(s, NULL, 0);
  7244. rl_rank.u64 = value;
  7245. }
  7246. if (parameter_set) {
  7247. lmc_wr(priv,
  7248. CVMX_LMCX_RLEVEL_RANKX(rankx, if_num),
  7249. rl_rank.u64);
  7250. rl_rank.u64 = lmc_rd(priv,
  7251. CVMX_LMCX_RLEVEL_RANKX(rankx,
  7252. if_num));
  7253. display_rl(if_num, rl_rank, rankx);
  7254. }
  7255. }
  7256. }
  7257. int init_octeon3_ddr3_interface(struct ddr_priv *priv,
  7258. struct ddr_conf *_ddr_conf, u32 _ddr_hertz,
  7259. u32 cpu_hertz, u32 ddr_ref_hertz, int _if_num,
  7260. u32 _if_mask)
  7261. {
  7262. union cvmx_lmcx_control ctrl;
  7263. int ret;
  7264. char *s;
  7265. int i;
  7266. if_num = _if_num;
  7267. ddr_hertz = _ddr_hertz;
  7268. ddr_conf = _ddr_conf;
  7269. if_mask = _if_mask;
  7270. odt_1rank_config = ddr_conf->odt_1rank_config;
  7271. odt_2rank_config = ddr_conf->odt_2rank_config;
  7272. odt_4rank_config = ddr_conf->odt_4rank_config;
  7273. dimm_config_table = ddr_conf->dimm_config_table;
  7274. c_cfg = &ddr_conf->custom_lmc_config;
  7275. /*
  7276. * Compute clock rates to the nearest picosecond.
  7277. */
  7278. tclk_psecs = hertz_to_psecs(ddr_hertz); /* Clock in psecs */
  7279. eclk_psecs = hertz_to_psecs(cpu_hertz); /* Clock in psecs */
  7280. dimm_count = 0;
  7281. /* Accumulate and report all the errors before giving up */
  7282. fatal_error = 0;
  7283. /* Flag that indicates safe DDR settings should be used */
  7284. safe_ddr_flag = 0;
  7285. if_64b = 1; /* Octeon II Default: 64bit interface width */
  7286. mem_size_mbytes = 0;
  7287. bank_bits = 0;
  7288. column_bits_start = 1;
  7289. use_ecc = 1;
  7290. min_cas_latency = 0, max_cas_latency = 0, override_cas_latency = 0;
  7291. spd_package = 0;
  7292. spd_rawcard = 0;
  7293. spd_rawcard_aorb = 0;
  7294. spd_rdimm_registers = 0;
  7295. is_stacked_die = 0;
  7296. is_3ds_dimm = 0; // 3DS
  7297. lranks_per_prank = 1; // 3DS: logical ranks per package rank
  7298. lranks_bits = 0; // 3DS: logical ranks bits
  7299. die_capacity = 0; // in Mbits; only used for 3DS
  7300. wl_mask_err = 0;
  7301. dyn_rtt_nom_mask = 0;
  7302. ddr_disable_chip_reset = 1;
  7303. match_wl_rtt_nom = 0;
  7304. internal_retries = 0;
  7305. disable_deskew_training = 0;
  7306. restart_if_dsk_incomplete = 0;
  7307. last_lane = ((if_64b) ? 8 : 4) + use_ecc;
  7308. disable_sequential_delay_check = 0;
  7309. wl_print = WLEVEL_PRINTALL_DEFAULT;
  7310. enable_by_rank_init = 1; // FIXME: default by-rank ON
  7311. saved_rank_mask = 0;
  7312. node = 0;
  7313. memset(hwl_alts, 0, sizeof(hwl_alts));
  7314. /*
  7315. * Initialize these to shut up the compiler. They are configured
  7316. * and used only for DDR4
  7317. */
  7318. ddr4_trrd_lmin = 6000;
  7319. ddr4_tccd_lmin = 6000;
  7320. debug("\nInitializing node %d DDR interface %d, DDR Clock %d, DDR Reference Clock %d, CPUID 0x%08x\n",
  7321. node, if_num, ddr_hertz, ddr_ref_hertz, read_c0_prid());
  7322. if (dimm_config_table[0].spd_addrs[0] == 0 &&
  7323. !dimm_config_table[0].spd_ptrs[0]) {
  7324. printf("ERROR: No dimms specified in the dimm_config_table.\n");
  7325. return -1;
  7326. }
  7327. // allow some overrides to be done
  7328. // this one controls several things related to DIMM geometry: HWL and RL
  7329. disable_sequential_delay_check = c_cfg->disable_sequential_delay_check;
  7330. s = lookup_env(priv, "ddr_disable_sequential_delay_check");
  7331. if (s)
  7332. disable_sequential_delay_check = strtoul(s, NULL, 0);
  7333. // this one controls whether chip RESET is done, or LMC init restarted
  7334. // from step 6.9.6
  7335. s = lookup_env(priv, "ddr_disable_chip_reset");
  7336. if (s)
  7337. ddr_disable_chip_reset = !!strtoul(s, NULL, 0);
  7338. // this one controls whether Deskew Training is performed
  7339. s = lookup_env(priv, "ddr_disable_deskew_training");
  7340. if (s)
  7341. disable_deskew_training = !!strtoul(s, NULL, 0);
  7342. if (ddr_verbose(priv)) {
  7343. printf("DDR SPD Table:");
  7344. for (didx = 0; didx < DDR_CFG_T_MAX_DIMMS; ++didx) {
  7345. if (dimm_config_table[didx].spd_addrs[0] == 0)
  7346. break;
  7347. printf(" --ddr%dspd=0x%02x", if_num,
  7348. dimm_config_table[didx].spd_addrs[0]);
  7349. if (dimm_config_table[didx].spd_addrs[1] != 0)
  7350. printf(",0x%02x",
  7351. dimm_config_table[didx].spd_addrs[1]);
  7352. }
  7353. printf("\n");
  7354. }
  7355. /*
  7356. * Walk the DRAM Socket Configuration Table to see what is installed.
  7357. */
  7358. for (didx = 0; didx < DDR_CFG_T_MAX_DIMMS; ++didx) {
  7359. /* Check for lower DIMM socket populated */
  7360. if (validate_dimm(priv, &dimm_config_table[didx], 0)) {
  7361. if (ddr_verbose(priv))
  7362. report_dimm(&dimm_config_table[didx], 0,
  7363. dimm_count, if_num);
  7364. ++dimm_count;
  7365. } else {
  7366. break;
  7367. } /* Finished when there is no lower DIMM */
  7368. }
  7369. initialize_ddr_clock(priv, ddr_conf, cpu_hertz, ddr_hertz,
  7370. ddr_ref_hertz, if_num, if_mask);
  7371. if (!odt_1rank_config)
  7372. odt_1rank_config = disable_odt_config;
  7373. if (!odt_2rank_config)
  7374. odt_2rank_config = disable_odt_config;
  7375. if (!odt_4rank_config)
  7376. odt_4rank_config = disable_odt_config;
  7377. s = env_get("ddr_safe");
  7378. if (s) {
  7379. safe_ddr_flag = !!simple_strtoul(s, NULL, 0);
  7380. printf("Parameter found in environment. ddr_safe = %d\n",
  7381. safe_ddr_flag);
  7382. }
  7383. if (dimm_count == 0) {
  7384. printf("ERROR: DIMM 0 not detected.\n");
  7385. return (-1);
  7386. }
  7387. if (c_cfg->mode32b)
  7388. if_64b = 0;
  7389. s = lookup_env(priv, "if_64b");
  7390. if (s)
  7391. if_64b = !!simple_strtoul(s, NULL, 0);
  7392. if (if_64b == 1) {
  7393. if (octeon_is_cpuid(OCTEON_CN70XX)) {
  7394. printf("64-bit interface width is not supported for this Octeon model\n");
  7395. ++fatal_error;
  7396. }
  7397. }
  7398. /* ddr_type only indicates DDR4 or DDR3 */
  7399. ddr_type = (read_spd(&dimm_config_table[0], 0,
  7400. DDR4_SPD_KEY_BYTE_DEVICE_TYPE) == 0x0C) ? 4 : 3;
  7401. debug("DRAM Device Type: DDR%d\n", ddr_type);
  7402. if (ddr_type == DDR4_DRAM) {
  7403. int spd_module_type;
  7404. int asymmetric;
  7405. const char *signal_load[4] = { "", "MLS", "3DS", "RSV" };
  7406. imp_val = &ddr4_impedence_val;
  7407. spd_addr =
  7408. read_spd(&dimm_config_table[0], 0,
  7409. DDR4_SPD_ADDRESSING_ROW_COL_BITS);
  7410. spd_org =
  7411. read_spd(&dimm_config_table[0], 0,
  7412. DDR4_SPD_MODULE_ORGANIZATION);
  7413. spd_banks =
  7414. 0xFF & read_spd(&dimm_config_table[0], 0,
  7415. DDR4_SPD_DENSITY_BANKS);
  7416. bank_bits =
  7417. (2 + ((spd_banks >> 4) & 0x3)) + ((spd_banks >> 6) & 0x3);
  7418. /* Controller can only address 4 bits. */
  7419. bank_bits = min((int)bank_bits, 4);
  7420. spd_package =
  7421. 0XFF & read_spd(&dimm_config_table[0], 0,
  7422. DDR4_SPD_PACKAGE_TYPE);
  7423. if (spd_package & 0x80) { // non-monolithic device
  7424. is_stacked_die = ((spd_package & 0x73) == 0x11);
  7425. debug("DDR4: Package Type 0x%02x (%s), %d die\n",
  7426. spd_package, signal_load[(spd_package & 3)],
  7427. ((spd_package >> 4) & 7) + 1);
  7428. is_3ds_dimm = ((spd_package & 3) == 2); // is it 3DS?
  7429. if (is_3ds_dimm) { // is it 3DS?
  7430. lranks_per_prank = ((spd_package >> 4) & 7) + 1;
  7431. // FIXME: should make sure it is only 2H or 4H
  7432. // or 8H?
  7433. lranks_bits = lranks_per_prank >> 1;
  7434. if (lranks_bits == 4)
  7435. lranks_bits = 3;
  7436. }
  7437. } else if (spd_package != 0) {
  7438. // FIXME: print non-zero monolithic device definition
  7439. debug("DDR4: Package Type MONOLITHIC: %d die, signal load %d\n",
  7440. ((spd_package >> 4) & 7) + 1, (spd_package & 3));
  7441. }
  7442. asymmetric = (spd_org >> 6) & 1;
  7443. if (asymmetric) {
  7444. int spd_secondary_pkg =
  7445. read_spd(&dimm_config_table[0], 0,
  7446. DDR4_SPD_SECONDARY_PACKAGE_TYPE);
  7447. debug("DDR4: Module Organization: ASYMMETRICAL: Secondary Package Type 0x%02x\n",
  7448. spd_secondary_pkg);
  7449. } else {
  7450. u64 bus_width =
  7451. 8 << (0x07 &
  7452. read_spd(&dimm_config_table[0], 0,
  7453. DDR4_SPD_MODULE_MEMORY_BUS_WIDTH));
  7454. u64 ddr_width = 4 << ((spd_org >> 0) & 0x7);
  7455. u64 module_cap;
  7456. int shift = (spd_banks & 0x0F);
  7457. die_capacity = (shift < 8) ? (256UL << shift) :
  7458. ((12UL << (shift & 1)) << 10);
  7459. debug("DDR4: Module Organization: SYMMETRICAL: capacity per die %d %cbit\n",
  7460. (die_capacity > 512) ? (die_capacity >> 10) :
  7461. die_capacity, (die_capacity > 512) ? 'G' : 'M');
  7462. module_cap = ((u64)die_capacity << 20) / 8UL *
  7463. bus_width / ddr_width *
  7464. (1UL + ((spd_org >> 3) & 0x7));
  7465. // is it 3DS?
  7466. if (is_3ds_dimm) {
  7467. module_cap *= (u64)(((spd_package >> 4) & 7) +
  7468. 1);
  7469. }
  7470. debug("DDR4: Module Organization: SYMMETRICAL: capacity per module %lld GB\n",
  7471. module_cap >> 30);
  7472. }
  7473. spd_rawcard =
  7474. 0xFF & read_spd(&dimm_config_table[0], 0,
  7475. DDR4_SPD_REFERENCE_RAW_CARD);
  7476. debug("DDR4: Reference Raw Card 0x%02x\n", spd_rawcard);
  7477. spd_module_type =
  7478. read_spd(&dimm_config_table[0], 0,
  7479. DDR4_SPD_KEY_BYTE_MODULE_TYPE);
  7480. if (spd_module_type & 0x80) { // HYBRID module
  7481. debug("DDR4: HYBRID module, type %s\n",
  7482. ((spd_module_type & 0x70) ==
  7483. 0x10) ? "NVDIMM" : "UNKNOWN");
  7484. }
  7485. spd_thermal_sensor =
  7486. read_spd(&dimm_config_table[0], 0,
  7487. DDR4_SPD_MODULE_THERMAL_SENSOR);
  7488. spd_dimm_type = spd_module_type & 0x0F;
  7489. spd_rdimm = (spd_dimm_type == 1) || (spd_dimm_type == 5) ||
  7490. (spd_dimm_type == 8);
  7491. if (spd_rdimm) {
  7492. u16 spd_mfgr_id, spd_register_rev, spd_mod_attr;
  7493. static const u16 manu_ids[4] = {
  7494. 0xb380, 0x3286, 0x9780, 0xb304
  7495. };
  7496. static const char *manu_names[4] = {
  7497. "XXX", "XXXXXXX", "XX", "XXXXX"
  7498. };
  7499. int mc;
  7500. spd_mfgr_id =
  7501. (0xFFU &
  7502. read_spd(&dimm_config_table[0], 0,
  7503. DDR4_SPD_REGISTER_MANUFACTURER_ID_LSB)) |
  7504. ((0xFFU &
  7505. read_spd(&dimm_config_table[0], 0,
  7506. DDR4_SPD_REGISTER_MANUFACTURER_ID_MSB))
  7507. << 8);
  7508. spd_register_rev =
  7509. 0xFFU & read_spd(&dimm_config_table[0], 0,
  7510. DDR4_SPD_REGISTER_REVISION_NUMBER);
  7511. for (mc = 0; mc < 4; mc++)
  7512. if (manu_ids[mc] == spd_mfgr_id)
  7513. break;
  7514. debug("DDR4: RDIMM Register Manufacturer ID: %s, Revision: 0x%02x\n",
  7515. (mc >= 4) ? "UNKNOWN" : manu_names[mc],
  7516. spd_register_rev);
  7517. // RAWCARD A or B must be bit 7=0 and bits 4-0
  7518. // either 00000(A) or 00001(B)
  7519. spd_rawcard_aorb = ((spd_rawcard & 0x9fUL) <= 1);
  7520. // RDIMM Module Attributes
  7521. spd_mod_attr =
  7522. 0xFFU & read_spd(&dimm_config_table[0], 0,
  7523. DDR4_SPD_UDIMM_ADDR_MAPPING_FROM_EDGE);
  7524. spd_rdimm_registers = ((1 << (spd_mod_attr & 3)) >> 1);
  7525. debug("DDR4: RDIMM Module Attributes (0x%02x): Register Type DDR4RCD%02d, DRAM rows %d, Registers %d\n",
  7526. spd_mod_attr, (spd_mod_attr >> 4) + 1,
  7527. ((1 << ((spd_mod_attr >> 2) & 3)) >> 1),
  7528. spd_rdimm_registers);
  7529. }
  7530. dimm_type_name = ddr4_dimm_types[spd_dimm_type];
  7531. } else { /* if (ddr_type == DDR4_DRAM) */
  7532. const char *signal_load[4] = { "UNK", "MLS", "SLS", "RSV" };
  7533. imp_val = &ddr3_impedence_val;
  7534. spd_addr =
  7535. read_spd(&dimm_config_table[0], 0,
  7536. DDR3_SPD_ADDRESSING_ROW_COL_BITS);
  7537. spd_org =
  7538. read_spd(&dimm_config_table[0], 0,
  7539. DDR3_SPD_MODULE_ORGANIZATION);
  7540. spd_banks =
  7541. read_spd(&dimm_config_table[0], 0,
  7542. DDR3_SPD_DENSITY_BANKS) & 0xff;
  7543. bank_bits = 3 + ((spd_banks >> 4) & 0x7);
  7544. /* Controller can only address 3 bits. */
  7545. bank_bits = min((int)bank_bits, 3);
  7546. spd_dimm_type =
  7547. 0x0f & read_spd(&dimm_config_table[0], 0,
  7548. DDR3_SPD_KEY_BYTE_MODULE_TYPE);
  7549. spd_rdimm = (spd_dimm_type == 1) || (spd_dimm_type == 5) ||
  7550. (spd_dimm_type == 9);
  7551. spd_package =
  7552. 0xFF & read_spd(&dimm_config_table[0], 0,
  7553. DDR3_SPD_SDRAM_DEVICE_TYPE);
  7554. if (spd_package & 0x80) { // non-standard device
  7555. debug("DDR3: Device Type 0x%02x (%s), %d die\n",
  7556. spd_package, signal_load[(spd_package & 3)],
  7557. ((1 << ((spd_package >> 4) & 7)) >> 1));
  7558. } else if (spd_package != 0) {
  7559. // FIXME: print non-zero monolithic device definition
  7560. debug("DDR3: Device Type MONOLITHIC: %d die, signal load %d\n",
  7561. ((1 << (spd_package >> 4) & 7) >> 1),
  7562. (spd_package & 3));
  7563. }
  7564. spd_rawcard =
  7565. 0xFF & read_spd(&dimm_config_table[0], 0,
  7566. DDR3_SPD_REFERENCE_RAW_CARD);
  7567. debug("DDR3: Reference Raw Card 0x%02x\n", spd_rawcard);
  7568. spd_thermal_sensor =
  7569. read_spd(&dimm_config_table[0], 0,
  7570. DDR3_SPD_MODULE_THERMAL_SENSOR);
  7571. if (spd_rdimm) {
  7572. int spd_mfgr_id, spd_register_rev, spd_mod_attr;
  7573. spd_mfgr_id =
  7574. (0xFFU &
  7575. read_spd(&dimm_config_table[0], 0,
  7576. DDR3_SPD_REGISTER_MANUFACTURER_ID_LSB)) |
  7577. ((0xFFU &
  7578. read_spd(&dimm_config_table[0], 0,
  7579. DDR3_SPD_REGISTER_MANUFACTURER_ID_MSB))
  7580. << 8);
  7581. spd_register_rev =
  7582. 0xFFU & read_spd(&dimm_config_table[0], 0,
  7583. DDR3_SPD_REGISTER_REVISION_NUMBER);
  7584. debug("DDR3: RDIMM Register Manufacturer ID 0x%x Revision 0x%02x\n",
  7585. spd_mfgr_id, spd_register_rev);
  7586. // Module Attributes
  7587. spd_mod_attr =
  7588. 0xFFU & read_spd(&dimm_config_table[0], 0,
  7589. DDR3_SPD_ADDRESS_MAPPING);
  7590. spd_rdimm_registers = ((1 << (spd_mod_attr & 3)) >> 1);
  7591. debug("DDR3: RDIMM Module Attributes (0x%02x): DRAM rows %d, Registers %d\n",
  7592. spd_mod_attr,
  7593. ((1 << ((spd_mod_attr >> 2) & 3)) >> 1),
  7594. spd_rdimm_registers);
  7595. }
  7596. dimm_type_name = ddr3_dimm_types[spd_dimm_type];
  7597. }
  7598. if (spd_thermal_sensor & 0x80) {
  7599. debug("DDR%d: SPD: Thermal Sensor PRESENT\n",
  7600. (ddr_type == DDR4_DRAM) ? 4 : 3);
  7601. }
  7602. debug("spd_addr : %#06x\n", spd_addr);
  7603. debug("spd_org : %#06x\n", spd_org);
  7604. debug("spd_banks : %#06x\n", spd_banks);
  7605. row_bits = 12 + ((spd_addr >> 3) & 0x7);
  7606. col_bits = 9 + ((spd_addr >> 0) & 0x7);
  7607. num_ranks = 1 + ((spd_org >> 3) & 0x7);
  7608. dram_width = 4 << ((spd_org >> 0) & 0x7);
  7609. num_banks = 1 << bank_bits;
  7610. s = lookup_env(priv, "ddr_num_ranks");
  7611. if (s)
  7612. num_ranks = simple_strtoul(s, NULL, 0);
  7613. s = lookup_env(priv, "ddr_enable_by_rank_init");
  7614. if (s)
  7615. enable_by_rank_init = !!simple_strtoul(s, NULL, 0);
  7616. // FIXME: for now, we can only handle a DDR4 2rank-1slot config
  7617. // FIXME: also, by-rank init does not work correctly if 32-bit mode...
  7618. if (enable_by_rank_init && (ddr_type != DDR4_DRAM ||
  7619. dimm_count != 1 || if_64b != 1 ||
  7620. num_ranks != 2))
  7621. enable_by_rank_init = 0;
  7622. if (enable_by_rank_init) {
  7623. struct dimm_odt_config *odt_config;
  7624. union cvmx_lmcx_modereg_params1 mp1;
  7625. union cvmx_lmcx_modereg_params2 modereg_params2;
  7626. int by_rank_rodt, by_rank_wr, by_rank_park;
  7627. // Do ODT settings changes which work best for 2R-1S configs
  7628. debug("DDR4: 2R-1S special BY-RANK init ODT settings updated\n");
  7629. // setup for modifying config table values - 2 ranks and 1 DIMM
  7630. odt_config =
  7631. (struct dimm_odt_config *)&ddr_conf->odt_2rank_config[0];
  7632. // original was 80, first try was 60
  7633. by_rank_rodt = ddr4_rodt_ctl_48_ohm;
  7634. s = lookup_env(priv, "ddr_by_rank_rodt");
  7635. if (s)
  7636. by_rank_rodt = strtoul(s, NULL, 0);
  7637. odt_config->qs_dic = /*RODT_CTL */ by_rank_rodt;
  7638. // this is for MODEREG_PARAMS1 fields
  7639. // fetch the original settings
  7640. mp1.u64 = odt_config->modereg_params1.u64;
  7641. by_rank_wr = ddr4_rttwr_80ohm; // originals were 240
  7642. s = lookup_env(priv, "ddr_by_rank_wr");
  7643. if (s)
  7644. by_rank_wr = simple_strtoul(s, NULL, 0);
  7645. // change specific settings here...
  7646. insrt_wr(&mp1.u64, /*rank */ 00, by_rank_wr);
  7647. insrt_wr(&mp1.u64, /*rank */ 01, by_rank_wr);
  7648. // save final settings
  7649. odt_config->modereg_params1.u64 = mp1.u64;
  7650. // this is for MODEREG_PARAMS2 fields
  7651. // fetch the original settings
  7652. modereg_params2.u64 = odt_config->modereg_params2.u64;
  7653. by_rank_park = ddr4_rttpark_none; // originals were 120
  7654. s = lookup_env(priv, "ddr_by_rank_park");
  7655. if (s)
  7656. by_rank_park = simple_strtoul(s, NULL, 0);
  7657. // change specific settings here...
  7658. modereg_params2.s.rtt_park_00 = by_rank_park;
  7659. modereg_params2.s.rtt_park_01 = by_rank_park;
  7660. // save final settings
  7661. odt_config->modereg_params2.u64 = modereg_params2.u64;
  7662. }
  7663. /*
  7664. * FIX
  7665. * Check that values are within some theoretical limits.
  7666. * col_bits(min) = row_lsb(min) - bank_bits(max) - bus_bits(max) =
  7667. * 14 - 3 - 4 = 7
  7668. * col_bits(max) = row_lsb(max) - bank_bits(min) - bus_bits(min) =
  7669. * 18 - 2 - 3 = 13
  7670. */
  7671. if (col_bits > 13 || col_bits < 7) {
  7672. printf("Unsupported number of Col Bits: %d\n", col_bits);
  7673. ++fatal_error;
  7674. }
  7675. /*
  7676. * FIX
  7677. * Check that values are within some theoretical limits.
  7678. * row_bits(min) = pbank_lsb(min) - row_lsb(max) - rank_bits =
  7679. * 26 - 18 - 1 = 7
  7680. * row_bits(max) = pbank_lsb(max) - row_lsb(min) - rank_bits =
  7681. * 33 - 14 - 1 = 18
  7682. */
  7683. if (row_bits > 18 || row_bits < 7) {
  7684. printf("Unsupported number of Row Bits: %d\n", row_bits);
  7685. ++fatal_error;
  7686. }
  7687. s = lookup_env(priv, "ddr_rdimm_ena");
  7688. if (s)
  7689. spd_rdimm = !!simple_strtoul(s, NULL, 0);
  7690. wl_loops = WLEVEL_LOOPS_DEFAULT;
  7691. // accept generic or interface-specific override
  7692. s = lookup_env(priv, "ddr_wlevel_loops");
  7693. if (!s)
  7694. s = lookup_env(priv, "ddr%d_wlevel_loops", if_num);
  7695. if (s)
  7696. wl_loops = strtoul(s, NULL, 0);
  7697. s = lookup_env(priv, "ddr_ranks");
  7698. if (s)
  7699. num_ranks = simple_strtoul(s, NULL, 0);
  7700. bunk_enable = (num_ranks > 1);
  7701. if (octeon_is_cpuid(OCTEON_CN7XXX))
  7702. column_bits_start = 3;
  7703. else
  7704. printf("ERROR: Unsupported Octeon model: 0x%x\n",
  7705. read_c0_prid());
  7706. row_lsb = column_bits_start + col_bits + bank_bits - (!if_64b);
  7707. debug("row_lsb = column_bits_start + col_bits + bank_bits = %d\n",
  7708. row_lsb);
  7709. pbank_lsb = row_lsb + row_bits + bunk_enable;
  7710. debug("pbank_lsb = row_lsb + row_bits + bunk_enable = %d\n", pbank_lsb);
  7711. if (lranks_per_prank > 1) {
  7712. pbank_lsb = row_lsb + row_bits + lranks_bits + bunk_enable;
  7713. debug("DDR4: 3DS: pbank_lsb = (%d row_lsb) + (%d row_bits) + (%d lranks_bits) + (%d bunk_enable) = %d\n",
  7714. row_lsb, row_bits, lranks_bits, bunk_enable, pbank_lsb);
  7715. }
  7716. mem_size_mbytes = dimm_count * ((1ull << pbank_lsb) >> 20);
  7717. if (num_ranks == 4) {
  7718. /*
  7719. * Quad rank dimm capacity is equivalent to two dual-rank
  7720. * dimms.
  7721. */
  7722. mem_size_mbytes *= 2;
  7723. }
  7724. /*
  7725. * Mask with 1 bits set for for each active rank, allowing 2 bits
  7726. * per dimm. This makes later calculations simpler, as a variety
  7727. * of CSRs use this layout. This init needs to be updated for dual
  7728. * configs (ie non-identical DIMMs).
  7729. *
  7730. * Bit 0 = dimm0, rank 0
  7731. * Bit 1 = dimm0, rank 1
  7732. * Bit 2 = dimm1, rank 0
  7733. * Bit 3 = dimm1, rank 1
  7734. * ...
  7735. */
  7736. rank_mask = 0x1;
  7737. if (num_ranks > 1)
  7738. rank_mask = 0x3;
  7739. if (num_ranks > 2)
  7740. rank_mask = 0xf;
  7741. for (i = 1; i < dimm_count; i++)
  7742. rank_mask |= ((rank_mask & 0x3) << (2 * i));
  7743. /*
  7744. * If we are booting from RAM, the DRAM controller is
  7745. * already set up. Just return the memory size
  7746. */
  7747. if (priv->flags & FLAG_RAM_RESIDENT) {
  7748. debug("Ram Boot: Skipping LMC config\n");
  7749. return mem_size_mbytes;
  7750. }
  7751. if (ddr_type == DDR4_DRAM) {
  7752. spd_ecc =
  7753. !!(read_spd
  7754. (&dimm_config_table[0], 0,
  7755. DDR4_SPD_MODULE_MEMORY_BUS_WIDTH) & 8);
  7756. } else {
  7757. spd_ecc =
  7758. !!(read_spd
  7759. (&dimm_config_table[0], 0,
  7760. DDR3_SPD_MEMORY_BUS_WIDTH) & 8);
  7761. }
  7762. char rank_spec[8];
  7763. printable_rank_spec(rank_spec, num_ranks, dram_width, spd_package);
  7764. debug("Summary: %d %s%s %s %s, row bits=%d, col bits=%d, bank bits=%d\n",
  7765. dimm_count, dimm_type_name, (dimm_count > 1) ? "s" : "",
  7766. rank_spec,
  7767. (spd_ecc) ? "ECC" : "non-ECC", row_bits, col_bits, bank_bits);
  7768. if (ddr_type == DDR4_DRAM) {
  7769. spd_cas_latency =
  7770. ((0xff &
  7771. read_spd(&dimm_config_table[0], 0,
  7772. DDR4_SPD_CAS_LATENCIES_BYTE0)) << 0);
  7773. spd_cas_latency |=
  7774. ((0xff &
  7775. read_spd(&dimm_config_table[0], 0,
  7776. DDR4_SPD_CAS_LATENCIES_BYTE1)) << 8);
  7777. spd_cas_latency |=
  7778. ((0xff &
  7779. read_spd(&dimm_config_table[0], 0,
  7780. DDR4_SPD_CAS_LATENCIES_BYTE2)) << 16);
  7781. spd_cas_latency |=
  7782. ((0xff &
  7783. read_spd(&dimm_config_table[0], 0,
  7784. DDR4_SPD_CAS_LATENCIES_BYTE3)) << 24);
  7785. } else {
  7786. spd_cas_latency =
  7787. 0xff & read_spd(&dimm_config_table[0], 0,
  7788. DDR3_SPD_CAS_LATENCIES_LSB);
  7789. spd_cas_latency |=
  7790. ((0xff &
  7791. read_spd(&dimm_config_table[0], 0,
  7792. DDR3_SPD_CAS_LATENCIES_MSB)) << 8);
  7793. }
  7794. debug("spd_cas_latency : %#06x\n", spd_cas_latency);
  7795. if (ddr_type == DDR4_DRAM) {
  7796. /*
  7797. * No other values for DDR4 MTB and FTB are specified at the
  7798. * current time so don't bother reading them. Can't speculate
  7799. * how new values will be represented.
  7800. */
  7801. int spdmtb = 125;
  7802. int spdftb = 1;
  7803. taamin = spdmtb * read_spd(&dimm_config_table[0], 0,
  7804. DDR4_SPD_MIN_CAS_LATENCY_TAAMIN) +
  7805. spdftb * (signed char)read_spd(&dimm_config_table[0],
  7806. 0, DDR4_SPD_MIN_CAS_LATENCY_FINE_TAAMIN);
  7807. ddr4_tckavgmin = spdmtb * read_spd(&dimm_config_table[0], 0,
  7808. DDR4_SPD_MINIMUM_CYCLE_TIME_TCKAVGMIN) +
  7809. spdftb * (signed char)read_spd(&dimm_config_table[0], 0,
  7810. DDR4_SPD_MIN_CYCLE_TIME_FINE_TCKAVGMIN);
  7811. ddr4_tckavgmax = spdmtb * read_spd(&dimm_config_table[0], 0,
  7812. DDR4_SPD_MAXIMUM_CYCLE_TIME_TCKAVGMAX) +
  7813. spdftb * (signed char)read_spd(&dimm_config_table[0], 0,
  7814. DDR4_SPD_MAX_CYCLE_TIME_FINE_TCKAVGMAX);
  7815. ddr4_trdcmin = spdmtb * read_spd(&dimm_config_table[0], 0,
  7816. DDR4_SPD_MIN_RAS_CAS_DELAY_TRCDMIN) +
  7817. spdftb * (signed char)read_spd(&dimm_config_table[0], 0,
  7818. DDR4_SPD_MIN_RAS_TO_CAS_DELAY_FINE_TRCDMIN);
  7819. ddr4_trpmin = spdmtb * read_spd(&dimm_config_table[0], 0,
  7820. DDR4_SPD_MIN_ROW_PRECHARGE_DELAY_TRPMIN) +
  7821. spdftb * (signed char)read_spd(&dimm_config_table[0], 0,
  7822. DDR4_SPD_MIN_ROW_PRECHARGE_DELAY_FINE_TRPMIN);
  7823. ddr4_trasmin = spdmtb *
  7824. (((read_spd
  7825. (&dimm_config_table[0], 0,
  7826. DDR4_SPD_UPPER_NIBBLES_TRAS_TRC) & 0xf) << 8) +
  7827. (read_spd
  7828. (&dimm_config_table[0], 0,
  7829. DDR4_SPD_MIN_ACTIVE_PRECHARGE_LSB_TRASMIN) & 0xff));
  7830. ddr4_trcmin = spdmtb *
  7831. ((((read_spd
  7832. (&dimm_config_table[0], 0,
  7833. DDR4_SPD_UPPER_NIBBLES_TRAS_TRC) >> 4) & 0xf) <<
  7834. 8) + (read_spd
  7835. (&dimm_config_table[0], 0,
  7836. DDR4_SPD_MIN_ACTIVE_REFRESH_LSB_TRCMIN) &
  7837. 0xff))
  7838. + spdftb * (signed char)read_spd(&dimm_config_table[0],
  7839. 0,
  7840. DDR4_SPD_MIN_ACT_TO_ACT_REFRESH_DELAY_FINE_TRCMIN);
  7841. ddr4_trfc1min = spdmtb * (((read_spd(&dimm_config_table[0], 0,
  7842. DDR4_SPD_MIN_REFRESH_RECOVERY_MSB_TRFC1MIN) & 0xff) <<
  7843. 8) + (read_spd(&dimm_config_table[0], 0,
  7844. DDR4_SPD_MIN_REFRESH_RECOVERY_LSB_TRFC1MIN) & 0xff));
  7845. ddr4_trfc2min = spdmtb * (((read_spd(&dimm_config_table[0], 0,
  7846. DDR4_SPD_MIN_REFRESH_RECOVERY_MSB_TRFC2MIN) & 0xff) <<
  7847. 8) + (read_spd(&dimm_config_table[0], 0,
  7848. DDR4_SPD_MIN_REFRESH_RECOVERY_LSB_TRFC2MIN) & 0xff));
  7849. ddr4_trfc4min = spdmtb * (((read_spd(&dimm_config_table[0], 0,
  7850. DDR4_SPD_MIN_REFRESH_RECOVERY_MSB_TRFC4MIN) & 0xff) <<
  7851. 8) + (read_spd(&dimm_config_table[0], 0,
  7852. DDR4_SPD_MIN_REFRESH_RECOVERY_LSB_TRFC4MIN) & 0xff));
  7853. ddr4_tfawmin = spdmtb * (((read_spd(&dimm_config_table[0], 0,
  7854. DDR4_SPD_MIN_FOUR_ACTIVE_WINDOW_MSN_TFAWMIN) & 0xf) <<
  7855. 8) + (read_spd(&dimm_config_table[0], 0,
  7856. DDR4_SPD_MIN_FOUR_ACTIVE_WINDOW_LSB_TFAWMIN) & 0xff));
  7857. ddr4_trrd_smin = spdmtb * read_spd(&dimm_config_table[0], 0,
  7858. DDR4_SPD_MIN_ROW_ACTIVE_DELAY_SAME_TRRD_SMIN) +
  7859. spdftb * (signed char)read_spd(&dimm_config_table[0], 0,
  7860. DDR4_SPD_MIN_ACT_TO_ACT_DELAY_DIFF_FINE_TRRD_SMIN);
  7861. ddr4_trrd_lmin = spdmtb * read_spd(&dimm_config_table[0], 0,
  7862. DDR4_SPD_MIN_ROW_ACTIVE_DELAY_DIFF_TRRD_LMIN) +
  7863. spdftb * (signed char)read_spd(&dimm_config_table[0], 0,
  7864. DDR4_SPD_MIN_ACT_TO_ACT_DELAY_SAME_FINE_TRRD_LMIN);
  7865. ddr4_tccd_lmin = spdmtb * read_spd(&dimm_config_table[0], 0,
  7866. DDR4_SPD_MIN_CAS_TO_CAS_DELAY_TCCD_LMIN) +
  7867. spdftb * (signed char)read_spd(&dimm_config_table[0], 0,
  7868. DDR4_SPD_MIN_CAS_TO_CAS_DELAY_FINE_TCCD_LMIN);
  7869. debug("%-45s : %6d ps\n", "Medium Timebase (MTB)", spdmtb);
  7870. debug("%-45s : %6d ps\n", "Fine Timebase (FTB)", spdftb);
  7871. debug("%-45s : %6d ps (%ld MT/s)\n",
  7872. "SDRAM Minimum Cycle Time (tCKAVGmin)", ddr4_tckavgmin,
  7873. pretty_psecs_to_mts(ddr4_tckavgmin));
  7874. debug("%-45s : %6d ps\n",
  7875. "SDRAM Maximum Cycle Time (tCKAVGmax)", ddr4_tckavgmax);
  7876. debug("%-45s : %6d ps\n", "Minimum CAS Latency Time (taamin)",
  7877. taamin);
  7878. debug("%-45s : %6d ps\n",
  7879. "Minimum RAS to CAS Delay Time (tRCDmin)", ddr4_trdcmin);
  7880. debug("%-45s : %6d ps\n",
  7881. "Minimum Row Precharge Delay Time (tRPmin)", ddr4_trpmin);
  7882. debug("%-45s : %6d ps\n",
  7883. "Minimum Active to Precharge Delay (tRASmin)",
  7884. ddr4_trasmin);
  7885. debug("%-45s : %6d ps\n",
  7886. "Minimum Active to Active/Refr. Delay (tRCmin)",
  7887. ddr4_trcmin);
  7888. debug("%-45s : %6d ps\n",
  7889. "Minimum Refresh Recovery Delay (tRFC1min)",
  7890. ddr4_trfc1min);
  7891. debug("%-45s : %6d ps\n",
  7892. "Minimum Refresh Recovery Delay (tRFC2min)",
  7893. ddr4_trfc2min);
  7894. debug("%-45s : %6d ps\n",
  7895. "Minimum Refresh Recovery Delay (tRFC4min)",
  7896. ddr4_trfc4min);
  7897. debug("%-45s : %6d ps\n",
  7898. "Minimum Four Activate Window Time (tFAWmin)",
  7899. ddr4_tfawmin);
  7900. debug("%-45s : %6d ps\n",
  7901. "Minimum Act. to Act. Delay (tRRD_Smin)", ddr4_trrd_smin);
  7902. debug("%-45s : %6d ps\n",
  7903. "Minimum Act. to Act. Delay (tRRD_Lmin)", ddr4_trrd_lmin);
  7904. debug("%-45s : %6d ps\n",
  7905. "Minimum CAS to CAS Delay Time (tCCD_Lmin)",
  7906. ddr4_tccd_lmin);
  7907. #define DDR4_TWR 15000
  7908. #define DDR4_TWTR_S 2500
  7909. tckmin = ddr4_tckavgmin;
  7910. twr = DDR4_TWR;
  7911. trcd = ddr4_trdcmin;
  7912. trrd = ddr4_trrd_smin;
  7913. trp = ddr4_trpmin;
  7914. tras = ddr4_trasmin;
  7915. trc = ddr4_trcmin;
  7916. trfc = ddr4_trfc1min;
  7917. twtr = DDR4_TWTR_S;
  7918. tfaw = ddr4_tfawmin;
  7919. if (spd_rdimm) {
  7920. spd_addr_mirror = read_spd(&dimm_config_table[0], 0,
  7921. DDR4_SPD_RDIMM_ADDR_MAPPING_FROM_REGISTER_TO_DRAM) &
  7922. 0x1;
  7923. } else {
  7924. spd_addr_mirror = read_spd(&dimm_config_table[0], 0,
  7925. DDR4_SPD_UDIMM_ADDR_MAPPING_FROM_EDGE) & 0x1;
  7926. }
  7927. debug("spd_addr_mirror : %#06x\n", spd_addr_mirror);
  7928. } else {
  7929. spd_mtb_dividend =
  7930. 0xff & read_spd(&dimm_config_table[0], 0,
  7931. DDR3_SPD_MEDIUM_TIMEBASE_DIVIDEND);
  7932. spd_mtb_divisor =
  7933. 0xff & read_spd(&dimm_config_table[0], 0,
  7934. DDR3_SPD_MEDIUM_TIMEBASE_DIVISOR);
  7935. spd_tck_min =
  7936. 0xff & read_spd(&dimm_config_table[0], 0,
  7937. DDR3_SPD_MINIMUM_CYCLE_TIME_TCKMIN);
  7938. spd_taa_min =
  7939. 0xff & read_spd(&dimm_config_table[0], 0,
  7940. DDR3_SPD_MIN_CAS_LATENCY_TAAMIN);
  7941. spd_twr =
  7942. 0xff & read_spd(&dimm_config_table[0], 0,
  7943. DDR3_SPD_MIN_WRITE_RECOVERY_TWRMIN);
  7944. spd_trcd =
  7945. 0xff & read_spd(&dimm_config_table[0], 0,
  7946. DDR3_SPD_MIN_RAS_CAS_DELAY_TRCDMIN);
  7947. spd_trrd =
  7948. 0xff & read_spd(&dimm_config_table[0], 0,
  7949. DDR3_SPD_MIN_ROW_ACTIVE_DELAY_TRRDMIN);
  7950. spd_trp =
  7951. 0xff & read_spd(&dimm_config_table[0], 0,
  7952. DDR3_SPD_MIN_ROW_PRECHARGE_DELAY_TRPMIN);
  7953. spd_tras =
  7954. 0xff & read_spd(&dimm_config_table[0], 0,
  7955. DDR3_SPD_MIN_ACTIVE_PRECHARGE_LSB_TRASMIN);
  7956. spd_tras |=
  7957. ((0xff &
  7958. read_spd(&dimm_config_table[0], 0,
  7959. DDR3_SPD_UPPER_NIBBLES_TRAS_TRC) & 0xf) << 8);
  7960. spd_trc =
  7961. 0xff & read_spd(&dimm_config_table[0], 0,
  7962. DDR3_SPD_MIN_ACTIVE_REFRESH_LSB_TRCMIN);
  7963. spd_trc |=
  7964. ((0xff &
  7965. read_spd(&dimm_config_table[0], 0,
  7966. DDR3_SPD_UPPER_NIBBLES_TRAS_TRC) & 0xf0) << 4);
  7967. spd_trfc =
  7968. 0xff & read_spd(&dimm_config_table[0], 0,
  7969. DDR3_SPD_MIN_REFRESH_RECOVERY_LSB_TRFCMIN);
  7970. spd_trfc |=
  7971. ((0xff &
  7972. read_spd(&dimm_config_table[0], 0,
  7973. DDR3_SPD_MIN_REFRESH_RECOVERY_MSB_TRFCMIN)) <<
  7974. 8);
  7975. spd_twtr =
  7976. 0xff & read_spd(&dimm_config_table[0], 0,
  7977. DDR3_SPD_MIN_INTERNAL_WRITE_READ_CMD_TWTRMIN);
  7978. spd_trtp =
  7979. 0xff & read_spd(&dimm_config_table[0], 0,
  7980. DDR3_SPD_MIN_INTERNAL_READ_PRECHARGE_CMD_TRTPMIN);
  7981. spd_tfaw =
  7982. 0xff & read_spd(&dimm_config_table[0], 0,
  7983. DDR3_SPD_MIN_FOUR_ACTIVE_WINDOW_TFAWMIN);
  7984. spd_tfaw |=
  7985. ((0xff &
  7986. read_spd(&dimm_config_table[0], 0,
  7987. DDR3_SPD_UPPER_NIBBLE_TFAW) & 0xf) << 8);
  7988. spd_addr_mirror =
  7989. 0xff & read_spd(&dimm_config_table[0], 0,
  7990. DDR3_SPD_ADDRESS_MAPPING) & 0x1;
  7991. /* Only address mirror unbuffered dimms. */
  7992. spd_addr_mirror = spd_addr_mirror && !spd_rdimm;
  7993. ftb_dividend =
  7994. read_spd(&dimm_config_table[0], 0,
  7995. DDR3_SPD_FINE_TIMEBASE_DIVIDEND_DIVISOR) >> 4;
  7996. ftb_divisor =
  7997. read_spd(&dimm_config_table[0], 0,
  7998. DDR3_SPD_FINE_TIMEBASE_DIVIDEND_DIVISOR) & 0xf;
  7999. /* Make sure that it is not 0 */
  8000. ftb_divisor = (ftb_divisor == 0) ? 1 : ftb_divisor;
  8001. debug("spd_twr : %#06x\n", spd_twr);
  8002. debug("spd_trcd : %#06x\n", spd_trcd);
  8003. debug("spd_trrd : %#06x\n", spd_trrd);
  8004. debug("spd_trp : %#06x\n", spd_trp);
  8005. debug("spd_tras : %#06x\n", spd_tras);
  8006. debug("spd_trc : %#06x\n", spd_trc);
  8007. debug("spd_trfc : %#06x\n", spd_trfc);
  8008. debug("spd_twtr : %#06x\n", spd_twtr);
  8009. debug("spd_trtp : %#06x\n", spd_trtp);
  8010. debug("spd_tfaw : %#06x\n", spd_tfaw);
  8011. debug("spd_addr_mirror : %#06x\n", spd_addr_mirror);
  8012. mtb_psec = spd_mtb_dividend * 1000 / spd_mtb_divisor;
  8013. taamin = mtb_psec * spd_taa_min;
  8014. taamin += ftb_dividend *
  8015. (signed char)read_spd(&dimm_config_table[0],
  8016. 0, DDR3_SPD_MIN_CAS_LATENCY_FINE_TAAMIN) /
  8017. ftb_divisor;
  8018. tckmin = mtb_psec * spd_tck_min;
  8019. tckmin += ftb_dividend *
  8020. (signed char)read_spd(&dimm_config_table[0],
  8021. 0, DDR3_SPD_MINIMUM_CYCLE_TIME_FINE_TCKMIN) /
  8022. ftb_divisor;
  8023. twr = spd_twr * mtb_psec;
  8024. trcd = spd_trcd * mtb_psec;
  8025. trrd = spd_trrd * mtb_psec;
  8026. trp = spd_trp * mtb_psec;
  8027. tras = spd_tras * mtb_psec;
  8028. trc = spd_trc * mtb_psec;
  8029. trfc = spd_trfc * mtb_psec;
  8030. if (octeon_is_cpuid(OCTEON_CN78XX_PASS2_X) && trfc < 260000) {
  8031. // default to this - because it works...
  8032. int new_trfc = 260000;
  8033. s = env_get("ddr_trfc");
  8034. if (s) {
  8035. new_trfc = simple_strtoul(s, NULL, 0);
  8036. printf("Parameter found in environment. ddr_trfc = %d\n",
  8037. new_trfc);
  8038. if (new_trfc < 160000 || new_trfc > 260000) {
  8039. // back to default if out of range
  8040. new_trfc = 260000;
  8041. }
  8042. }
  8043. debug("N%d.LMC%d: Adjusting tRFC from %d to %d, for CN78XX Pass 2.x\n",
  8044. node, if_num, trfc, new_trfc);
  8045. trfc = new_trfc;
  8046. }
  8047. twtr = spd_twtr * mtb_psec;
  8048. trtp = spd_trtp * mtb_psec;
  8049. tfaw = spd_tfaw * mtb_psec;
  8050. debug("Medium Timebase (MTB) : %6d ps\n",
  8051. mtb_psec);
  8052. debug("Minimum Cycle Time (tckmin) : %6d ps (%ld MT/s)\n",
  8053. tckmin, pretty_psecs_to_mts(tckmin));
  8054. debug("Minimum CAS Latency Time (taamin) : %6d ps\n",
  8055. taamin);
  8056. debug("Write Recovery Time (tWR) : %6d ps\n",
  8057. twr);
  8058. debug("Minimum RAS to CAS delay (tRCD) : %6d ps\n",
  8059. trcd);
  8060. debug("Minimum Row Active to Row Active delay (tRRD) : %6d ps\n",
  8061. trrd);
  8062. debug("Minimum Row Precharge Delay (tRP) : %6d ps\n",
  8063. trp);
  8064. debug("Minimum Active to Precharge (tRAS) : %6d ps\n",
  8065. tras);
  8066. debug("Minimum Active to Active/Refresh Delay (tRC) : %6d ps\n",
  8067. trc);
  8068. debug("Minimum Refresh Recovery Delay (tRFC) : %6d ps\n",
  8069. trfc);
  8070. debug("Internal write to read command delay (tWTR) : %6d ps\n",
  8071. twtr);
  8072. debug("Min Internal Rd to Precharge Cmd Delay (tRTP) : %6d ps\n",
  8073. trtp);
  8074. debug("Minimum Four Activate Window Delay (tFAW) : %6d ps\n",
  8075. tfaw);
  8076. }
  8077. /*
  8078. * When the cycle time is within 1 psec of the minimum accept it
  8079. * as a slight rounding error and adjust it to exactly the minimum
  8080. * cycle time. This avoids an unnecessary warning.
  8081. */
  8082. if (abs(tclk_psecs - tckmin) < 2)
  8083. tclk_psecs = tckmin;
  8084. if (tclk_psecs < (u64)tckmin) {
  8085. printf("WARNING!!!!: DDR Clock Rate (tCLK: %ld) exceeds DIMM specifications (tckmin: %ld)!!!!\n",
  8086. tclk_psecs, (ulong)tckmin);
  8087. }
  8088. debug("DDR Clock Rate (tCLK) : %6ld ps\n",
  8089. tclk_psecs);
  8090. debug("Core Clock Rate (eCLK) : %6ld ps\n",
  8091. eclk_psecs);
  8092. s = env_get("ddr_use_ecc");
  8093. if (s) {
  8094. use_ecc = !!simple_strtoul(s, NULL, 0);
  8095. printf("Parameter found in environment. ddr_use_ecc = %d\n",
  8096. use_ecc);
  8097. }
  8098. use_ecc = use_ecc && spd_ecc;
  8099. if_bytemask = if_64b ? (use_ecc ? 0x1ff : 0xff)
  8100. : (use_ecc ? 0x01f : 0x0f);
  8101. debug("DRAM Interface width: %d bits %s bytemask 0x%03x\n",
  8102. if_64b ? 64 : 32, use_ecc ? "+ECC" : "", if_bytemask);
  8103. debug("\n------ Board Custom Configuration Settings ------\n");
  8104. debug("%-45s : %d\n", "MIN_RTT_NOM_IDX ", c_cfg->min_rtt_nom_idx);
  8105. debug("%-45s : %d\n", "MAX_RTT_NOM_IDX ", c_cfg->max_rtt_nom_idx);
  8106. debug("%-45s : %d\n", "MIN_RODT_CTL ", c_cfg->min_rodt_ctl);
  8107. debug("%-45s : %d\n", "MAX_RODT_CTL ", c_cfg->max_rodt_ctl);
  8108. debug("%-45s : %d\n", "MIN_CAS_LATENCY ", c_cfg->min_cas_latency);
  8109. debug("%-45s : %d\n", "OFFSET_EN ", c_cfg->offset_en);
  8110. debug("%-45s : %d\n", "OFFSET_UDIMM ", c_cfg->offset_udimm);
  8111. debug("%-45s : %d\n", "OFFSET_RDIMM ", c_cfg->offset_rdimm);
  8112. debug("%-45s : %d\n", "DDR_RTT_NOM_AUTO ", c_cfg->ddr_rtt_nom_auto);
  8113. debug("%-45s : %d\n", "DDR_RODT_CTL_AUTO ", c_cfg->ddr_rodt_ctl_auto);
  8114. if (spd_rdimm)
  8115. debug("%-45s : %d\n", "RLEVEL_COMP_OFFSET",
  8116. c_cfg->rlevel_comp_offset_rdimm);
  8117. else
  8118. debug("%-45s : %d\n", "RLEVEL_COMP_OFFSET",
  8119. c_cfg->rlevel_comp_offset_udimm);
  8120. debug("%-45s : %d\n", "RLEVEL_COMPUTE ", c_cfg->rlevel_compute);
  8121. debug("%-45s : %d\n", "DDR2T_UDIMM ", c_cfg->ddr2t_udimm);
  8122. debug("%-45s : %d\n", "DDR2T_RDIMM ", c_cfg->ddr2t_rdimm);
  8123. debug("%-45s : %d\n", "FPRCH2 ", c_cfg->fprch2);
  8124. debug("%-45s : %d\n", "PTUNE_OFFSET ", c_cfg->ptune_offset);
  8125. debug("%-45s : %d\n", "NTUNE_OFFSET ", c_cfg->ntune_offset);
  8126. debug("-------------------------------------------------\n");
  8127. cl = divide_roundup(taamin, tclk_psecs);
  8128. debug("Desired CAS Latency : %6d\n", cl);
  8129. min_cas_latency = c_cfg->min_cas_latency;
  8130. s = lookup_env(priv, "ddr_min_cas_latency");
  8131. if (s)
  8132. min_cas_latency = simple_strtoul(s, NULL, 0);
  8133. debug("CAS Latencies supported in DIMM :");
  8134. base_cl = (ddr_type == DDR4_DRAM) ? 7 : 4;
  8135. for (i = 0; i < 32; ++i) {
  8136. if ((spd_cas_latency >> i) & 1) {
  8137. debug(" %d", i + base_cl);
  8138. max_cas_latency = i + base_cl;
  8139. if (min_cas_latency == 0)
  8140. min_cas_latency = i + base_cl;
  8141. }
  8142. }
  8143. debug("\n");
  8144. /*
  8145. * Use relaxed timing when running slower than the minimum
  8146. * supported speed. Adjust timing to match the smallest supported
  8147. * CAS Latency.
  8148. */
  8149. if (min_cas_latency > cl) {
  8150. ulong adjusted_tclk = taamin / min_cas_latency;
  8151. cl = min_cas_latency;
  8152. debug("Slow clock speed. Adjusting timing: tClk = %ld, Adjusted tClk = %ld\n",
  8153. tclk_psecs, adjusted_tclk);
  8154. tclk_psecs = adjusted_tclk;
  8155. }
  8156. s = env_get("ddr_cas_latency");
  8157. if (s) {
  8158. override_cas_latency = simple_strtoul(s, NULL, 0);
  8159. printf("Parameter found in environment. ddr_cas_latency = %d\n",
  8160. override_cas_latency);
  8161. }
  8162. /* Make sure that the selected cas latency is legal */
  8163. for (i = (cl - base_cl); i < 32; ++i) {
  8164. if ((spd_cas_latency >> i) & 1) {
  8165. cl = i + base_cl;
  8166. break;
  8167. }
  8168. }
  8169. if (max_cas_latency < cl)
  8170. cl = max_cas_latency;
  8171. if (override_cas_latency != 0)
  8172. cl = override_cas_latency;
  8173. debug("CAS Latency : %6d\n", cl);
  8174. if ((cl * tckmin) > 20000) {
  8175. debug("(CLactual * tckmin) = %d exceeds 20 ns\n",
  8176. (cl * tckmin));
  8177. }
  8178. if (tclk_psecs < (ulong)tckmin) {
  8179. printf("WARNING!!!!!!: DDR3 Clock Rate (tCLK: %ld) exceeds DIMM specifications (tckmin:%ld)!!!!!!!!\n",
  8180. tclk_psecs, (ulong)tckmin);
  8181. }
  8182. if (num_banks != 4 && num_banks != 8 && num_banks != 16) {
  8183. printf("Unsupported number of banks %d. Must be 4 or 8.\n",
  8184. num_banks);
  8185. ++fatal_error;
  8186. }
  8187. if (num_ranks != 1 && num_ranks != 2 && num_ranks != 4) {
  8188. printf("Unsupported number of ranks: %d\n", num_ranks);
  8189. ++fatal_error;
  8190. }
  8191. if (octeon_is_cpuid(OCTEON_CN78XX) ||
  8192. octeon_is_cpuid(OCTEON_CN73XX) ||
  8193. octeon_is_cpuid(OCTEON_CNF75XX)) {
  8194. if (dram_width != 8 && dram_width != 16 && dram_width != 4) {
  8195. printf("Unsupported SDRAM Width, %d. Must be 4, 8 or 16.\n",
  8196. dram_width);
  8197. ++fatal_error;
  8198. }
  8199. } else if (dram_width != 8 && dram_width != 16) {
  8200. printf("Unsupported SDRAM Width, %d. Must be 8 or 16.\n",
  8201. dram_width);
  8202. ++fatal_error;
  8203. }
  8204. /*
  8205. ** Bail out here if things are not copasetic.
  8206. */
  8207. if (fatal_error)
  8208. return (-1);
  8209. /*
  8210. * 4.8.4 LMC RESET Initialization
  8211. *
  8212. * The purpose of this step is to assert/deassert the RESET# pin at the
  8213. * DDR3/DDR4 parts.
  8214. *
  8215. * This LMC RESET step is done for all enabled LMCs.
  8216. */
  8217. perform_lmc_reset(priv, node, if_num);
  8218. // Make sure scrambling is disabled during init...
  8219. ctrl.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(if_num));
  8220. ctrl.s.scramble_ena = 0;
  8221. lmc_wr(priv, CVMX_LMCX_CONTROL(if_num), ctrl.u64);
  8222. lmc_wr(priv, CVMX_LMCX_SCRAMBLE_CFG0(if_num), 0);
  8223. lmc_wr(priv, CVMX_LMCX_SCRAMBLE_CFG1(if_num), 0);
  8224. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS1_X))
  8225. lmc_wr(priv, CVMX_LMCX_SCRAMBLE_CFG2(if_num), 0);
  8226. odt_idx = min(dimm_count - 1, 3);
  8227. switch (num_ranks) {
  8228. case 1:
  8229. odt_config = odt_1rank_config;
  8230. break;
  8231. case 2:
  8232. odt_config = odt_2rank_config;
  8233. break;
  8234. case 4:
  8235. odt_config = odt_4rank_config;
  8236. break;
  8237. default:
  8238. odt_config = disable_odt_config;
  8239. printf("Unsupported number of ranks: %d\n", num_ranks);
  8240. ++fatal_error;
  8241. }
  8242. /*
  8243. * 4.8.5 Early LMC Initialization
  8244. *
  8245. * All of DDR PLL, LMC CK, and LMC DRESET initializations must be
  8246. * completed prior to starting this LMC initialization sequence.
  8247. *
  8248. * Perform the following five substeps for early LMC initialization:
  8249. *
  8250. * 1. Software must ensure there are no pending DRAM transactions.
  8251. *
  8252. * 2. Write LMC(0)_CONFIG, LMC(0)_CONTROL, LMC(0)_TIMING_PARAMS0,
  8253. * LMC(0)_TIMING_PARAMS1, LMC(0)_MODEREG_PARAMS0,
  8254. * LMC(0)_MODEREG_PARAMS1, LMC(0)_DUAL_MEMCFG, LMC(0)_NXM,
  8255. * LMC(0)_WODT_MASK, LMC(0)_RODT_MASK, LMC(0)_COMP_CTL2,
  8256. * LMC(0)_PHY_CTL, LMC(0)_DIMM0/1_PARAMS, and LMC(0)_DIMM_CTL with
  8257. * appropriate values. All sections in this chapter can be used to
  8258. * derive proper register settings.
  8259. */
  8260. /* LMC(0)_CONFIG */
  8261. lmc_config(priv);
  8262. /* LMC(0)_CONTROL */
  8263. lmc_control(priv);
  8264. /* LMC(0)_TIMING_PARAMS0 */
  8265. lmc_timing_params0(priv);
  8266. /* LMC(0)_TIMING_PARAMS1 */
  8267. lmc_timing_params1(priv);
  8268. /* LMC(0)_TIMING_PARAMS2 */
  8269. lmc_timing_params2(priv);
  8270. /* LMC(0)_MODEREG_PARAMS0 */
  8271. lmc_modereg_params0(priv);
  8272. /* LMC(0)_MODEREG_PARAMS1 */
  8273. lmc_modereg_params1(priv);
  8274. /* LMC(0)_MODEREG_PARAMS2 */
  8275. lmc_modereg_params2(priv);
  8276. /* LMC(0)_MODEREG_PARAMS3 */
  8277. lmc_modereg_params3(priv);
  8278. /* LMC(0)_NXM */
  8279. lmc_nxm(priv);
  8280. /* LMC(0)_WODT_MASK */
  8281. lmc_wodt_mask(priv);
  8282. /* LMC(0)_RODT_MASK */
  8283. lmc_rodt_mask(priv);
  8284. /* LMC(0)_COMP_CTL2 */
  8285. lmc_comp_ctl2(priv);
  8286. /* LMC(0)_PHY_CTL */
  8287. lmc_phy_ctl(priv);
  8288. /* LMC(0)_EXT_CONFIG */
  8289. lmc_ext_config(priv);
  8290. /* LMC(0)_EXT_CONFIG2 */
  8291. lmc_ext_config2(priv);
  8292. /* LMC(0)_DIMM0/1_PARAMS */
  8293. lmc_dimm01_params(priv);
  8294. ret = lmc_rank_init(priv);
  8295. if (ret < 0)
  8296. return 0; /* 0 indicates problem */
  8297. lmc_config_2(priv);
  8298. lmc_write_leveling(priv);
  8299. lmc_read_leveling(priv);
  8300. lmc_workaround(priv);
  8301. ret = lmc_sw_write_leveling(priv);
  8302. if (ret < 0)
  8303. return 0; /* 0 indicates problem */
  8304. // this sometimes causes stack overflow crashes..
  8305. // display only for DDR4 RDIMMs.
  8306. if (ddr_type == DDR4_DRAM && spd_rdimm) {
  8307. int i;
  8308. for (i = 0; i < 3; i += 2) // just pages 0 and 2 for now..
  8309. display_mpr_page(priv, rank_mask, if_num, i);
  8310. }
  8311. lmc_dll(priv);
  8312. lmc_workaround_2(priv);
  8313. lmc_final(priv);
  8314. lmc_scrambling(priv);
  8315. return mem_size_mbytes;
  8316. }
  8317. ///// HW-assist byte DLL offset tuning //////
  8318. static int cvmx_dram_get_num_lmc(struct ddr_priv *priv)
  8319. {
  8320. union cvmx_lmcx_dll_ctl2 lmcx_dll_ctl2;
  8321. if (octeon_is_cpuid(OCTEON_CN70XX))
  8322. return 1;
  8323. if (octeon_is_cpuid(OCTEON_CN73XX) || octeon_is_cpuid(OCTEON_CNF75XX)) {
  8324. // sample LMC1
  8325. lmcx_dll_ctl2.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL2(1));
  8326. if (lmcx_dll_ctl2.cn78xx.intf_en)
  8327. return 2;
  8328. else
  8329. return 1;
  8330. }
  8331. // for CN78XX, LMCs are always active in pairs, and always LMC0/1
  8332. // so, we sample LMC2 to see if 2 and 3 are active
  8333. lmcx_dll_ctl2.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL2(2));
  8334. if (lmcx_dll_ctl2.cn78xx.intf_en)
  8335. return 4;
  8336. else
  8337. return 2;
  8338. }
  8339. // got to do these here, even though already defined in BDK
  8340. // all DDR3, and DDR4 x16 today, use only 3 bank bits;
  8341. // DDR4 x4 and x8 always have 4 bank bits
  8342. // NOTE: this will change in the future, when DDR4 x16 devices can
  8343. // come with 16 banks!! FIXME!!
  8344. static int cvmx_dram_get_num_bank_bits(struct ddr_priv *priv, int lmc)
  8345. {
  8346. union cvmx_lmcx_dll_ctl2 lmcx_dll_ctl2;
  8347. union cvmx_lmcx_config lmcx_config;
  8348. union cvmx_lmcx_ddr_pll_ctl lmcx_ddr_pll_ctl;
  8349. int bank_width;
  8350. // can always read this
  8351. lmcx_dll_ctl2.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL2(lmc));
  8352. if (lmcx_dll_ctl2.cn78xx.dreset) // check LMCn
  8353. return 0;
  8354. lmcx_config.u64 = lmc_rd(priv, CVMX_LMCX_DLL_CTL2(lmc));
  8355. lmcx_ddr_pll_ctl.u64 = lmc_rd(priv, CVMX_LMCX_DDR_PLL_CTL(lmc));
  8356. bank_width = ((lmcx_ddr_pll_ctl.s.ddr4_mode != 0) &&
  8357. (lmcx_config.s.bg2_enable)) ? 4 : 3;
  8358. return bank_width;
  8359. }
  8360. #define EXTRACT(v, lsb, width) (((v) >> (lsb)) & ((1ull << (width)) - 1))
  8361. #define ADDRESS_HOLE 0x10000000ULL
  8362. static void cvmx_dram_address_extract_info(struct ddr_priv *priv, u64 address,
  8363. int *node, int *lmc, int *dimm,
  8364. int *prank, int *lrank, int *bank,
  8365. int *row, int *col)
  8366. {
  8367. int bank_lsb, xbits;
  8368. union cvmx_l2c_ctl l2c_ctl;
  8369. union cvmx_lmcx_config lmcx_config;
  8370. union cvmx_lmcx_control lmcx_control;
  8371. union cvmx_lmcx_ext_config ext_config;
  8372. int bitno = (octeon_is_cpuid(OCTEON_CN7XXX)) ? 20 : 18;
  8373. int bank_width;
  8374. int dimm_lsb;
  8375. int dimm_width;
  8376. int prank_lsb, lrank_lsb;
  8377. int prank_width, lrank_width;
  8378. int row_lsb;
  8379. int row_width;
  8380. int col_hi_lsb;
  8381. int col_hi_width;
  8382. int col_hi;
  8383. if (octeon_is_cpuid(OCTEON_CN73XX) || octeon_is_cpuid(OCTEON_CNF75XX))
  8384. bitno = 18;
  8385. *node = EXTRACT(address, 40, 2); /* Address bits [41:40] */
  8386. address &= (1ULL << 40) - 1; // lop off any node bits or above
  8387. if (address >= ADDRESS_HOLE) // adjust down if at HOLE or above
  8388. address -= ADDRESS_HOLE;
  8389. /* Determine the LMC controllers */
  8390. l2c_ctl.u64 = l2c_rd(priv, CVMX_L2C_CTL_REL);
  8391. /* xbits depends on number of LMCs */
  8392. xbits = cvmx_dram_get_num_lmc(priv) >> 1; // 4->2, 2->1, 1->0
  8393. bank_lsb = 7 + xbits;
  8394. /* LMC number is probably aliased */
  8395. if (l2c_ctl.s.disidxalias) {
  8396. *lmc = EXTRACT(address, 7, xbits);
  8397. } else {
  8398. *lmc = EXTRACT(address, 7, xbits) ^
  8399. EXTRACT(address, bitno, xbits) ^
  8400. EXTRACT(address, 12, xbits);
  8401. }
  8402. /* Figure out the bank field width */
  8403. lmcx_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(*lmc));
  8404. ext_config.u64 = lmc_rd(priv, CVMX_LMCX_EXT_CONFIG(*lmc));
  8405. bank_width = cvmx_dram_get_num_bank_bits(priv, *lmc);
  8406. /* Extract additional info from the LMC_CONFIG CSR */
  8407. dimm_lsb = 28 + lmcx_config.s.pbank_lsb + xbits;
  8408. dimm_width = 40 - dimm_lsb;
  8409. prank_lsb = dimm_lsb - lmcx_config.s.rank_ena;
  8410. prank_width = dimm_lsb - prank_lsb;
  8411. lrank_lsb = prank_lsb - ext_config.s.dimm0_cid;
  8412. lrank_width = prank_lsb - lrank_lsb;
  8413. row_lsb = 14 + lmcx_config.s.row_lsb + xbits;
  8414. row_width = lrank_lsb - row_lsb;
  8415. col_hi_lsb = bank_lsb + bank_width;
  8416. col_hi_width = row_lsb - col_hi_lsb;
  8417. /* Extract the parts of the address */
  8418. *dimm = EXTRACT(address, dimm_lsb, dimm_width);
  8419. *prank = EXTRACT(address, prank_lsb, prank_width);
  8420. *lrank = EXTRACT(address, lrank_lsb, lrank_width);
  8421. *row = EXTRACT(address, row_lsb, row_width);
  8422. /* bank calculation may be aliased... */
  8423. lmcx_control.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(*lmc));
  8424. if (lmcx_control.s.xor_bank) {
  8425. *bank = EXTRACT(address, bank_lsb, bank_width) ^
  8426. EXTRACT(address, 12 + xbits, bank_width);
  8427. } else {
  8428. *bank = EXTRACT(address, bank_lsb, bank_width);
  8429. }
  8430. /* LMC number already extracted */
  8431. col_hi = EXTRACT(address, col_hi_lsb, col_hi_width);
  8432. *col = EXTRACT(address, 3, 4) | (col_hi << 4);
  8433. /* Bus byte is address bits [2:0]. Unused here */
  8434. }
  8435. // end of added workarounds
  8436. // NOTE: "mode" argument:
  8437. // DBTRAIN_TEST: for testing using GP patterns, includes ECC
  8438. // DBTRAIN_DBI: for DBI deskew training behavior (uses GP patterns)
  8439. // DBTRAIN_LFSR: for testing using LFSR patterns, includes ECC
  8440. // NOTE: trust the caller to specify the correct/supported mode
  8441. //
  8442. static int test_dram_byte_hw(struct ddr_priv *priv, int if_num, u64 p,
  8443. int mode, u64 *xor_data)
  8444. {
  8445. u64 p1;
  8446. u64 k;
  8447. int errors = 0;
  8448. u64 mpr_data0, mpr_data1;
  8449. u64 bad_bits[2] = { 0, 0 };
  8450. int node_address, lmc, dimm;
  8451. int prank, lrank;
  8452. int bank, row, col;
  8453. int save_or_dis;
  8454. int byte;
  8455. int ba_loop, ba_bits;
  8456. union cvmx_lmcx_rlevel_ctl rlevel_ctl;
  8457. union cvmx_lmcx_dbtrain_ctl dbtrain_ctl;
  8458. union cvmx_lmcx_phy_ctl phy_ctl;
  8459. int biter_errs;
  8460. // FIXME: K iterations set to 4 for now.
  8461. // FIXME: decrement to increase interations.
  8462. // FIXME: must be no less than 22 to stay above an LMC hash field.
  8463. int kshift = 27;
  8464. const char *s;
  8465. int node = 0;
  8466. // allow override default setting for kshift
  8467. s = env_get("ddr_tune_set_kshift");
  8468. if (s) {
  8469. int temp = simple_strtoul(s, NULL, 0);
  8470. if (temp < 22 || temp > 28) {
  8471. debug("N%d.LMC%d: ILLEGAL override of kshift to %d, using default %d\n",
  8472. node, if_num, temp, kshift);
  8473. } else {
  8474. debug("N%d.LMC%d: overriding kshift (%d) to %d\n",
  8475. node, if_num, kshift, temp);
  8476. kshift = temp;
  8477. }
  8478. }
  8479. /*
  8480. * 1) Make sure that RLEVEL_CTL[OR_DIS] = 0.
  8481. */
  8482. rlevel_ctl.u64 = lmc_rd(priv, CVMX_LMCX_RLEVEL_CTL(if_num));
  8483. save_or_dis = rlevel_ctl.s.or_dis;
  8484. /* or_dis must be disabled for this sequence */
  8485. rlevel_ctl.s.or_dis = 0;
  8486. lmc_wr(priv, CVMX_LMCX_RLEVEL_CTL(if_num), rlevel_ctl.u64);
  8487. /*
  8488. * NOTE: this step done in the calling routine(s)...
  8489. * 3) Setup GENERAL_PURPOSE[0-2] registers with the data pattern
  8490. * of choice.
  8491. * a. GENERAL_PURPOSE0[DATA<63:0>] - sets the initial lower
  8492. * (rising edge) 64 bits of data.
  8493. * b. GENERAL_PURPOSE1[DATA<63:0>] - sets the initial upper
  8494. * (falling edge) 64 bits of data.
  8495. * c. GENERAL_PURPOSE2[DATA<15:0>] - sets the initial lower
  8496. * (rising edge <7:0>) and upper (falling edge <15:8>) ECC data.
  8497. */
  8498. // final address must include LMC and node
  8499. p |= (if_num << 7); /* Map address into proper interface */
  8500. p |= (u64)node << CVMX_NODE_MEM_SHIFT; // map to node
  8501. /*
  8502. * Add base offset to both test regions to not clobber u-boot stuff
  8503. * when running from L2 for NAND boot.
  8504. */
  8505. p += 0x20000000; // offset to 512MB, ie above THE HOLE!!!
  8506. p |= 1ull << 63; // needed for OCTEON
  8507. errors = 0;
  8508. cvmx_dram_address_extract_info(priv, p, &node_address, &lmc, &dimm,
  8509. &prank, &lrank, &bank, &row, &col);
  8510. debug("%s: START at A:0x%012llx, N%d L%d D%d/%d R%d B%1x Row:%05x Col:%05x\n",
  8511. __func__, p, node_address, lmc, dimm, prank, lrank, bank,
  8512. row, col);
  8513. // only check once per call, and ignore if no match...
  8514. if ((int)node != node_address) {
  8515. printf("ERROR: Node address mismatch\n");
  8516. return 0;
  8517. }
  8518. if (lmc != if_num) {
  8519. printf("ERROR: LMC address mismatch\n");
  8520. return 0;
  8521. }
  8522. /*
  8523. * 7) Set PHY_CTL[PHY_RESET] = 1 (LMC automatically clears this as
  8524. * it's a one-shot operation). This is to get into the habit of
  8525. * resetting PHY's SILO to the original 0 location.
  8526. */
  8527. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  8528. phy_ctl.s.phy_reset = 1;
  8529. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  8530. /*
  8531. * Walk through a range of addresses avoiding bits that alias
  8532. * interfaces on the CN88XX.
  8533. */
  8534. // FIXME: want to try to keep the K increment from affecting the
  8535. // LMC via hash, so keep it above bit 21 we also want to keep k
  8536. // less than the base offset of bit 29 (512MB)
  8537. for (k = 0; k < (1UL << 29); k += (1UL << kshift)) {
  8538. // FIXME: the sequence will interate over 1/2 cacheline
  8539. // FIXME: for each unit specified in "read_cmd_count",
  8540. // FIXME: so, we setup each sequence to do the max cachelines
  8541. // it can
  8542. p1 = p + k;
  8543. cvmx_dram_address_extract_info(priv, p1, &node_address, &lmc,
  8544. &dimm, &prank, &lrank, &bank,
  8545. &row, &col);
  8546. /*
  8547. * 2) Setup the fields of the CSR DBTRAIN_CTL as follows:
  8548. * a. COL, ROW, BA, BG, PRANK points to the starting point
  8549. * of the address.
  8550. * You can just set them to all 0.
  8551. * b. RW_TRAIN - set this to 1.
  8552. * c. TCCD_L - set this to 0.
  8553. * d. READ_CMD_COUNT - instruct the sequence to the how many
  8554. * writes/reads.
  8555. * It is 5 bits field, so set to 31 of maximum # of r/w.
  8556. */
  8557. dbtrain_ctl.u64 = lmc_rd(priv, CVMX_LMCX_DBTRAIN_CTL(if_num));
  8558. dbtrain_ctl.s.column_a = col;
  8559. dbtrain_ctl.s.row_a = row;
  8560. dbtrain_ctl.s.bg = (bank >> 2) & 3;
  8561. dbtrain_ctl.s.prank = (dimm * 2) + prank; // FIXME?
  8562. dbtrain_ctl.s.lrank = lrank; // FIXME?
  8563. dbtrain_ctl.s.activate = (mode == DBTRAIN_DBI);
  8564. dbtrain_ctl.s.write_ena = 1;
  8565. dbtrain_ctl.s.read_cmd_count = 31; // max count pass 1.x
  8566. if (octeon_is_cpuid(OCTEON_CN78XX_PASS2_X) ||
  8567. octeon_is_cpuid(OCTEON_CNF75XX)) {
  8568. // max count on chips that support it
  8569. dbtrain_ctl.s.cmd_count_ext = 3;
  8570. } else {
  8571. // max count pass 1.x
  8572. dbtrain_ctl.s.cmd_count_ext = 0;
  8573. }
  8574. dbtrain_ctl.s.rw_train = 1;
  8575. dbtrain_ctl.s.tccd_sel = (mode == DBTRAIN_DBI);
  8576. // LFSR should only be on when chip supports it...
  8577. dbtrain_ctl.s.lfsr_pattern_sel = (mode == DBTRAIN_LFSR) ? 1 : 0;
  8578. biter_errs = 0;
  8579. // for each address, iterate over the 4 "banks" in the BA
  8580. for (ba_loop = 0, ba_bits = bank & 3;
  8581. ba_loop < 4; ba_loop++, ba_bits = (ba_bits + 1) & 3) {
  8582. dbtrain_ctl.s.ba = ba_bits;
  8583. lmc_wr(priv, CVMX_LMCX_DBTRAIN_CTL(if_num),
  8584. dbtrain_ctl.u64);
  8585. /*
  8586. * We will use the RW_TRAINING sequence (14) for
  8587. * this task.
  8588. *
  8589. * 4) Kick off the sequence (SEQ_CTL[SEQ_SEL] = 14,
  8590. * SEQ_CTL[INIT_START] = 1).
  8591. * 5) Poll on SEQ_CTL[SEQ_COMPLETE] for completion.
  8592. */
  8593. oct3_ddr3_seq(priv, prank, if_num, 14);
  8594. /*
  8595. * 6) Read MPR_DATA0 and MPR_DATA1 for results.
  8596. * a. MPR_DATA0[MPR_DATA<63:0>] - comparison results
  8597. * for DQ63:DQ0. (1 means MATCH, 0 means FAIL).
  8598. * b. MPR_DATA1[MPR_DATA<7:0>] - comparison results
  8599. * for ECC bit7:0.
  8600. */
  8601. mpr_data0 = lmc_rd(priv, CVMX_LMCX_MPR_DATA0(if_num));
  8602. mpr_data1 = lmc_rd(priv, CVMX_LMCX_MPR_DATA1(if_num));
  8603. /*
  8604. * 7) Set PHY_CTL[PHY_RESET] = 1 (LMC automatically
  8605. * clears this as it's a one-shot operation).
  8606. * This is to get into the habit of resetting PHY's
  8607. * SILO to the original 0 location.
  8608. */
  8609. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(if_num));
  8610. phy_ctl.s.phy_reset = 1;
  8611. lmc_wr(priv, CVMX_LMCX_PHY_CTL(if_num), phy_ctl.u64);
  8612. // bypass any error checking or updating when DBI mode
  8613. if (mode == DBTRAIN_DBI)
  8614. continue;
  8615. // data bytes
  8616. if (~mpr_data0) {
  8617. for (byte = 0; byte < 8; byte++) {
  8618. if ((~mpr_data0 >> (8 * byte)) & 0xffUL)
  8619. biter_errs |= (1 << byte);
  8620. }
  8621. // accumulate bad bits
  8622. bad_bits[0] |= ~mpr_data0;
  8623. }
  8624. // include ECC byte errors
  8625. if (~mpr_data1 & 0xffUL) {
  8626. biter_errs |= (1 << 8);
  8627. bad_bits[1] |= ~mpr_data1 & 0xffUL;
  8628. }
  8629. }
  8630. errors |= biter_errs;
  8631. } /* end for (k=...) */
  8632. rlevel_ctl.s.or_dis = save_or_dis;
  8633. lmc_wr(priv, CVMX_LMCX_RLEVEL_CTL(if_num), rlevel_ctl.u64);
  8634. // send the bad bits back...
  8635. if (mode != DBTRAIN_DBI && xor_data) {
  8636. xor_data[0] = bad_bits[0];
  8637. xor_data[1] = bad_bits[1];
  8638. }
  8639. return errors;
  8640. }
  8641. // setup default for byte test pattern array
  8642. // take these from the HRM section 6.9.13
  8643. static const u64 byte_pattern_0[] = {
  8644. 0xFFAAFFFFFF55FFFFULL, // GP0
  8645. 0x55555555AAAAAAAAULL, // GP1
  8646. 0xAA55AAAAULL, // GP2
  8647. };
  8648. static const u64 byte_pattern_1[] = {
  8649. 0xFBF7EFDFBF7FFEFDULL, // GP0
  8650. 0x0F1E3C78F0E1C387ULL, // GP1
  8651. 0xF0E1BF7FULL, // GP2
  8652. };
  8653. // this is from Andrew via LFSR with PRBS=0xFFFFAAAA
  8654. static const u64 byte_pattern_2[] = {
  8655. 0xEE55AADDEE55AADDULL, // GP0
  8656. 0x55AADDEE55AADDEEULL, // GP1
  8657. 0x55EEULL, // GP2
  8658. };
  8659. // this is from Mike via LFSR with PRBS=0x4A519909
  8660. static const u64 byte_pattern_3[] = {
  8661. 0x0088CCEE0088CCEEULL, // GP0
  8662. 0xBB552211BB552211ULL, // GP1
  8663. 0xBB00ULL, // GP2
  8664. };
  8665. static const u64 *byte_patterns[4] = {
  8666. byte_pattern_0, byte_pattern_1, byte_pattern_2, byte_pattern_3
  8667. };
  8668. static const u32 lfsr_patterns[4] = {
  8669. 0xFFFFAAAAUL, 0x06000000UL, 0xAAAAFFFFUL, 0x4A519909UL
  8670. };
  8671. #define NUM_BYTE_PATTERNS 4
  8672. #define DEFAULT_BYTE_BURSTS 32 // compromise between time and rigor
  8673. static void setup_hw_pattern(struct ddr_priv *priv, int lmc,
  8674. const u64 *pattern_p)
  8675. {
  8676. /*
  8677. * 3) Setup GENERAL_PURPOSE[0-2] registers with the data pattern
  8678. * of choice.
  8679. * a. GENERAL_PURPOSE0[DATA<63:0>] - sets the initial lower
  8680. * (rising edge) 64 bits of data.
  8681. * b. GENERAL_PURPOSE1[DATA<63:0>] - sets the initial upper
  8682. * (falling edge) 64 bits of data.
  8683. * c. GENERAL_PURPOSE2[DATA<15:0>] - sets the initial lower
  8684. * (rising edge <7:0>) and upper
  8685. * (falling edge <15:8>) ECC data.
  8686. */
  8687. lmc_wr(priv, CVMX_LMCX_GENERAL_PURPOSE0(lmc), pattern_p[0]);
  8688. lmc_wr(priv, CVMX_LMCX_GENERAL_PURPOSE1(lmc), pattern_p[1]);
  8689. lmc_wr(priv, CVMX_LMCX_GENERAL_PURPOSE2(lmc), pattern_p[2]);
  8690. }
  8691. static void setup_lfsr_pattern(struct ddr_priv *priv, int lmc, u32 data)
  8692. {
  8693. union cvmx_lmcx_char_ctl char_ctl;
  8694. u32 prbs;
  8695. const char *s;
  8696. s = env_get("ddr_lfsr_prbs");
  8697. if (s)
  8698. prbs = simple_strtoul(s, NULL, 0);
  8699. else
  8700. prbs = data;
  8701. /*
  8702. * 2) DBTRAIN_CTL[LFSR_PATTERN_SEL] = 1
  8703. * here data comes from the LFSR generating a PRBS pattern
  8704. * CHAR_CTL.EN = 0
  8705. * CHAR_CTL.SEL = 0; // for PRBS
  8706. * CHAR_CTL.DR = 1;
  8707. * CHAR_CTL.PRBS = setup for whatever type of PRBS to send
  8708. * CHAR_CTL.SKEW_ON = 1;
  8709. */
  8710. char_ctl.u64 = lmc_rd(priv, CVMX_LMCX_CHAR_CTL(lmc));
  8711. char_ctl.s.en = 0;
  8712. char_ctl.s.sel = 0;
  8713. char_ctl.s.dr = 1;
  8714. char_ctl.s.prbs = prbs;
  8715. char_ctl.s.skew_on = 1;
  8716. lmc_wr(priv, CVMX_LMCX_CHAR_CTL(lmc), char_ctl.u64);
  8717. }
  8718. static int choose_best_hw_patterns(int lmc, int mode)
  8719. {
  8720. int new_mode = mode;
  8721. const char *s;
  8722. switch (mode) {
  8723. case DBTRAIN_TEST: // always choose LFSR if chip supports it
  8724. if (octeon_is_cpuid(OCTEON_CN78XX_PASS2_X)) {
  8725. int lfsr_enable = 1;
  8726. s = env_get("ddr_allow_lfsr");
  8727. if (s) {
  8728. // override?
  8729. lfsr_enable = !!strtoul(s, NULL, 0);
  8730. }
  8731. if (lfsr_enable)
  8732. new_mode = DBTRAIN_LFSR;
  8733. }
  8734. break;
  8735. case DBTRAIN_DBI: // possibly can allow LFSR use?
  8736. break;
  8737. case DBTRAIN_LFSR: // forced already
  8738. if (!octeon_is_cpuid(OCTEON_CN78XX_PASS2_X)) {
  8739. debug("ERROR: illegal HW assist mode %d\n", mode);
  8740. new_mode = DBTRAIN_TEST;
  8741. }
  8742. break;
  8743. default:
  8744. debug("ERROR: unknown HW assist mode %d\n", mode);
  8745. }
  8746. if (new_mode != mode)
  8747. debug("%s: changing mode %d to %d\n", __func__, mode, new_mode);
  8748. return new_mode;
  8749. }
  8750. int run_best_hw_patterns(struct ddr_priv *priv, int lmc, u64 phys_addr,
  8751. int mode, u64 *xor_data)
  8752. {
  8753. int pattern;
  8754. const u64 *pattern_p;
  8755. int errs, errors = 0;
  8756. // FIXME? always choose LFSR if chip supports it???
  8757. mode = choose_best_hw_patterns(lmc, mode);
  8758. for (pattern = 0; pattern < NUM_BYTE_PATTERNS; pattern++) {
  8759. if (mode == DBTRAIN_LFSR) {
  8760. setup_lfsr_pattern(priv, lmc, lfsr_patterns[pattern]);
  8761. } else {
  8762. pattern_p = byte_patterns[pattern];
  8763. setup_hw_pattern(priv, lmc, pattern_p);
  8764. }
  8765. errs = test_dram_byte_hw(priv, lmc, phys_addr, mode, xor_data);
  8766. debug("%s: PATTERN %d at A:0x%012llx errors 0x%x\n",
  8767. __func__, pattern, phys_addr, errs);
  8768. errors |= errs;
  8769. }
  8770. return errors;
  8771. }
  8772. static void hw_assist_test_dll_offset(struct ddr_priv *priv,
  8773. int dll_offset_mode, int lmc,
  8774. int bytelane,
  8775. int if_64b,
  8776. u64 dram_tune_rank_offset,
  8777. int dram_tune_byte_bursts)
  8778. {
  8779. int byte_offset, new_best_offset[9];
  8780. int rank_delay_start[4][9];
  8781. int rank_delay_count[4][9];
  8782. int rank_delay_best_start[4][9];
  8783. int rank_delay_best_count[4][9];
  8784. int errors[4], off_errors, tot_errors;
  8785. int rank_mask, rankx, active_ranks;
  8786. int pattern;
  8787. const u64 *pattern_p;
  8788. int byte;
  8789. char *mode_str = (dll_offset_mode == 2) ? "Read" : "Write";
  8790. int pat_best_offset[9];
  8791. u64 phys_addr;
  8792. int pat_beg, pat_end;
  8793. int rank_beg, rank_end;
  8794. int byte_lo, byte_hi;
  8795. union cvmx_lmcx_config lmcx_config;
  8796. u64 hw_rank_offset;
  8797. int num_lmcs = cvmx_dram_get_num_lmc(priv);
  8798. // FIXME? always choose LFSR if chip supports it???
  8799. int mode = choose_best_hw_patterns(lmc, DBTRAIN_TEST);
  8800. int node = 0;
  8801. if (bytelane == 0x0A) { // all bytelanes
  8802. byte_lo = 0;
  8803. byte_hi = 8;
  8804. } else { // just 1
  8805. byte_lo = bytelane;
  8806. byte_hi = bytelane;
  8807. }
  8808. lmcx_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(lmc));
  8809. rank_mask = lmcx_config.s.init_status;
  8810. // this should be correct for 1 or 2 ranks, 1 or 2 DIMMs
  8811. hw_rank_offset =
  8812. 1ull << (28 + lmcx_config.s.pbank_lsb - lmcx_config.s.rank_ena +
  8813. (num_lmcs / 2));
  8814. debug("N%d: %s: starting LMC%d with rank offset 0x%016llx\n",
  8815. node, __func__, lmc, (unsigned long long)hw_rank_offset);
  8816. // start of pattern loop
  8817. // we do the set of tests for each pattern supplied...
  8818. memset(new_best_offset, 0, sizeof(new_best_offset));
  8819. for (pattern = 0; pattern < NUM_BYTE_PATTERNS; pattern++) {
  8820. memset(pat_best_offset, 0, sizeof(pat_best_offset));
  8821. if (mode == DBTRAIN_TEST) {
  8822. pattern_p = byte_patterns[pattern];
  8823. setup_hw_pattern(priv, lmc, pattern_p);
  8824. } else {
  8825. setup_lfsr_pattern(priv, lmc, lfsr_patterns[pattern]);
  8826. }
  8827. // now loop through all legal values for the DLL byte offset...
  8828. #define BYTE_OFFSET_INCR 3 // FIXME: make this tunable?
  8829. tot_errors = 0;
  8830. memset(rank_delay_count, 0, sizeof(rank_delay_count));
  8831. memset(rank_delay_start, 0, sizeof(rank_delay_start));
  8832. memset(rank_delay_best_count, 0, sizeof(rank_delay_best_count));
  8833. memset(rank_delay_best_start, 0, sizeof(rank_delay_best_start));
  8834. for (byte_offset = -63; byte_offset < 64;
  8835. byte_offset += BYTE_OFFSET_INCR) {
  8836. // do the setup on the active LMC
  8837. // set the bytelanes DLL offsets
  8838. change_dll_offset_enable(priv, lmc, 0);
  8839. // FIXME? bytelane?
  8840. load_dll_offset(priv, lmc, dll_offset_mode,
  8841. byte_offset, bytelane);
  8842. change_dll_offset_enable(priv, lmc, 1);
  8843. //bdk_watchdog_poke();
  8844. // run the test on each rank
  8845. // only 1 call per rank should be enough, let the
  8846. // bursts, loops, etc, control the load...
  8847. // errors for this byte_offset, all ranks
  8848. off_errors = 0;
  8849. active_ranks = 0;
  8850. for (rankx = 0; rankx < 4; rankx++) {
  8851. if (!(rank_mask & (1 << rankx)))
  8852. continue;
  8853. phys_addr = hw_rank_offset * active_ranks;
  8854. // FIXME: now done by test_dram_byte_hw()
  8855. //phys_addr |= (lmc << 7);
  8856. //phys_addr |= (u64)node << CVMX_NODE_MEM_SHIFT;
  8857. active_ranks++;
  8858. // NOTE: return is a now a bitmask of the
  8859. // erroring bytelanes.
  8860. errors[rankx] =
  8861. test_dram_byte_hw(priv, lmc, phys_addr,
  8862. mode, NULL);
  8863. // process any errors in the bytelane(s) that
  8864. // are being tested
  8865. for (byte = byte_lo; byte <= byte_hi; byte++) {
  8866. // check errors
  8867. // yes, an error in the byte lane in
  8868. // this rank
  8869. if (errors[rankx] & (1 << byte)) {
  8870. off_errors |= (1 << byte);
  8871. debug("N%d.LMC%d.R%d: Bytelane %d DLL %s Offset Test %3d: Address 0x%012llx errors\n",
  8872. node, lmc, rankx, byte,
  8873. mode_str, byte_offset,
  8874. phys_addr);
  8875. // had started run
  8876. if (rank_delay_count
  8877. [rankx][byte] > 0) {
  8878. debug("N%d.LMC%d.R%d: Bytelane %d DLL %s Offset Test %3d: stopping a run here\n",
  8879. node, lmc, rankx,
  8880. byte, mode_str,
  8881. byte_offset);
  8882. // stop now
  8883. rank_delay_count
  8884. [rankx][byte] =
  8885. 0;
  8886. }
  8887. // FIXME: else had not started
  8888. // run - nothing else to do?
  8889. } else {
  8890. // no error in the byte lane
  8891. // first success, set run start
  8892. if (rank_delay_count[rankx]
  8893. [byte] == 0) {
  8894. debug("N%d.LMC%d.R%d: Bytelane %d DLL %s Offset Test %3d: starting a run here\n",
  8895. node, lmc, rankx,
  8896. byte, mode_str,
  8897. byte_offset);
  8898. rank_delay_start[rankx]
  8899. [byte] =
  8900. byte_offset;
  8901. }
  8902. // bump run length
  8903. rank_delay_count[rankx][byte]
  8904. += BYTE_OFFSET_INCR;
  8905. // is this now the biggest
  8906. // window?
  8907. if (rank_delay_count[rankx]
  8908. [byte] >
  8909. rank_delay_best_count[rankx]
  8910. [byte]) {
  8911. rank_delay_best_count
  8912. [rankx][byte] =
  8913. rank_delay_count
  8914. [rankx][byte];
  8915. rank_delay_best_start
  8916. [rankx][byte] =
  8917. rank_delay_start
  8918. [rankx][byte];
  8919. debug("N%d.LMC%d.R%d: Bytelane %d DLL %s Offset Test %3d: updating best to %d/%d\n",
  8920. node, lmc, rankx,
  8921. byte, mode_str,
  8922. byte_offset,
  8923. rank_delay_best_start
  8924. [rankx][byte],
  8925. rank_delay_best_count
  8926. [rankx][byte]);
  8927. }
  8928. }
  8929. }
  8930. } /* for (rankx = 0; rankx < 4; rankx++) */
  8931. tot_errors |= off_errors;
  8932. }
  8933. // set the bytelanes DLL offsets all back to 0
  8934. change_dll_offset_enable(priv, lmc, 0);
  8935. load_dll_offset(priv, lmc, dll_offset_mode, 0, bytelane);
  8936. change_dll_offset_enable(priv, lmc, 1);
  8937. // now choose the best byte_offsets for this pattern
  8938. // according to the best windows of the tested ranks
  8939. // calculate offset by constructing an average window
  8940. // from the rank windows
  8941. for (byte = byte_lo; byte <= byte_hi; byte++) {
  8942. pat_beg = -999;
  8943. pat_end = 999;
  8944. for (rankx = 0; rankx < 4; rankx++) {
  8945. if (!(rank_mask & (1 << rankx)))
  8946. continue;
  8947. rank_beg = rank_delay_best_start[rankx][byte];
  8948. pat_beg = max(pat_beg, rank_beg);
  8949. rank_end = rank_beg +
  8950. rank_delay_best_count[rankx][byte] -
  8951. BYTE_OFFSET_INCR;
  8952. pat_end = min(pat_end, rank_end);
  8953. debug("N%d.LMC%d.R%d: Bytelane %d DLL %s Offset Test: Rank Window %3d:%3d\n",
  8954. node, lmc, rankx, byte, mode_str,
  8955. rank_beg, rank_end);
  8956. } /* for (rankx = 0; rankx < 4; rankx++) */
  8957. pat_best_offset[byte] = (pat_end + pat_beg) / 2;
  8958. // sum the pattern averages
  8959. new_best_offset[byte] += pat_best_offset[byte];
  8960. }
  8961. // now print them on 1 line, descending order...
  8962. debug("N%d.LMC%d: HW DLL %s Offset Pattern %d :",
  8963. node, lmc, mode_str, pattern);
  8964. for (byte = byte_hi; byte >= byte_lo; --byte)
  8965. debug(" %4d", pat_best_offset[byte]);
  8966. debug("\n");
  8967. }
  8968. // end of pattern loop
  8969. debug("N%d.LMC%d: HW DLL %s Offset Average : ", node, lmc, mode_str);
  8970. // print in decending byte index order
  8971. for (byte = byte_hi; byte >= byte_lo; --byte) {
  8972. // create the new average NINT
  8973. new_best_offset[byte] = divide_nint(new_best_offset[byte],
  8974. NUM_BYTE_PATTERNS);
  8975. // print the best offsets from all patterns
  8976. // print just the offset of all the bytes
  8977. if (bytelane == 0x0A)
  8978. debug("%4d ", new_best_offset[byte]);
  8979. else // print the bytelanes also
  8980. debug("(byte %d) %4d ", byte, new_best_offset[byte]);
  8981. // done with testing, load up the best offsets we found...
  8982. // disable offsets while we load...
  8983. change_dll_offset_enable(priv, lmc, 0);
  8984. load_dll_offset(priv, lmc, dll_offset_mode,
  8985. new_best_offset[byte], byte);
  8986. // re-enable the offsets now that we are done loading
  8987. change_dll_offset_enable(priv, lmc, 1);
  8988. }
  8989. debug("\n");
  8990. }
  8991. /*
  8992. * Automatically adjust the DLL offset for the selected bytelane using
  8993. * hardware-assist
  8994. */
  8995. static int perform_HW_dll_offset_tuning(struct ddr_priv *priv,
  8996. int dll_offset_mode, int bytelane)
  8997. {
  8998. int if_64b;
  8999. int save_ecc_ena[4];
  9000. union cvmx_lmcx_config lmc_config;
  9001. int lmc, num_lmcs = cvmx_dram_get_num_lmc(priv);
  9002. const char *s;
  9003. int loops = 1, loop;
  9004. int by;
  9005. u64 dram_tune_rank_offset;
  9006. int dram_tune_byte_bursts = DEFAULT_BYTE_BURSTS;
  9007. int node = 0;
  9008. // see if we want to do the tuning more than once per LMC...
  9009. s = env_get("ddr_tune_ecc_loops");
  9010. if (s)
  9011. loops = strtoul(s, NULL, 0);
  9012. // allow override of the test repeats (bursts)
  9013. s = env_get("ddr_tune_byte_bursts");
  9014. if (s)
  9015. dram_tune_byte_bursts = strtoul(s, NULL, 10);
  9016. // print current working values
  9017. debug("N%d: H/W Tuning for bytelane %d will use %d loops, %d bursts, and %d patterns.\n",
  9018. node, bytelane, loops, dram_tune_byte_bursts, NUM_BYTE_PATTERNS);
  9019. // FIXME? get flag from LMC0 only
  9020. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(0));
  9021. if_64b = !lmc_config.s.mode32b;
  9022. // this should be correct for 1 or 2 ranks, 1 or 2 DIMMs
  9023. dram_tune_rank_offset =
  9024. 1ull << (28 + lmc_config.s.pbank_lsb - lmc_config.s.rank_ena +
  9025. (num_lmcs / 2));
  9026. // do once for each active LMC
  9027. for (lmc = 0; lmc < num_lmcs; lmc++) {
  9028. debug("N%d: H/W Tuning: starting LMC%d bytelane %d tune.\n",
  9029. node, lmc, bytelane);
  9030. /* Enable ECC for the HW tests */
  9031. // NOTE: we do enable ECC, but the HW tests used will not
  9032. // generate "visible" errors
  9033. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(lmc));
  9034. save_ecc_ena[lmc] = lmc_config.s.ecc_ena;
  9035. lmc_config.s.ecc_ena = 1;
  9036. lmc_wr(priv, CVMX_LMCX_CONFIG(lmc), lmc_config.u64);
  9037. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(lmc));
  9038. // testing is done on a single LMC at a time
  9039. // FIXME: for now, loop here to show what happens multiple times
  9040. for (loop = 0; loop < loops; loop++) {
  9041. /* Perform DLL offset tuning */
  9042. hw_assist_test_dll_offset(priv, 2 /* 2=read */, lmc,
  9043. bytelane,
  9044. if_64b, dram_tune_rank_offset,
  9045. dram_tune_byte_bursts);
  9046. }
  9047. // perform cleanup on active LMC
  9048. debug("N%d: H/W Tuning: finishing LMC%d bytelane %d tune.\n",
  9049. node, lmc, bytelane);
  9050. /* Restore ECC for DRAM tests */
  9051. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(lmc));
  9052. lmc_config.s.ecc_ena = save_ecc_ena[lmc];
  9053. lmc_wr(priv, CVMX_LMCX_CONFIG(lmc), lmc_config.u64);
  9054. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(lmc));
  9055. // finally, see if there are any read offset overrides
  9056. // after tuning
  9057. for (by = 0; by < 9; by++) {
  9058. s = lookup_env(priv, "ddr%d_tune_byte%d", lmc, by);
  9059. if (s) {
  9060. int dllro = strtoul(s, NULL, 10);
  9061. change_dll_offset_enable(priv, lmc, 0);
  9062. load_dll_offset(priv, lmc, 2, dllro, by);
  9063. change_dll_offset_enable(priv, lmc, 1);
  9064. }
  9065. }
  9066. } /* for (lmc = 0; lmc < num_lmcs; lmc++) */
  9067. // finish up...
  9068. return 0;
  9069. } /* perform_HW_dll_offset_tuning */
  9070. // this routine simply makes the calls to the tuning routine and returns
  9071. // any errors
  9072. static int cvmx_tune_node(struct ddr_priv *priv)
  9073. {
  9074. int errs, tot_errs;
  9075. int do_dllwo = 0; // default to NO
  9076. const char *str;
  9077. int node = 0;
  9078. // Automatically tune the data and ECC byte DLL read offsets
  9079. debug("N%d: Starting DLL Read Offset Tuning for LMCs\n", node);
  9080. errs = perform_HW_dll_offset_tuning(priv, 2, 0x0A /* all bytelanes */);
  9081. debug("N%d: Finished DLL Read Offset Tuning for LMCs, %d errors\n",
  9082. node, errs);
  9083. tot_errs = errs;
  9084. // disabled by default for now, does not seem to be needed?
  9085. // Automatically tune the data and ECC byte DLL write offsets
  9086. // allow override of default setting
  9087. str = env_get("ddr_tune_write_offsets");
  9088. if (str)
  9089. do_dllwo = !!strtoul(str, NULL, 0);
  9090. if (do_dllwo) {
  9091. debug("N%d: Starting DLL Write Offset Tuning for LMCs\n", node);
  9092. errs =
  9093. perform_HW_dll_offset_tuning(priv, 1,
  9094. 0x0A /* all bytelanes */);
  9095. debug("N%d: Finished DLL Write Offset Tuning for LMCs, %d errors\n",
  9096. node, errs);
  9097. tot_errs += errs;
  9098. }
  9099. return tot_errs;
  9100. }
  9101. // this routine makes the calls to the tuning routines when criteria are met
  9102. // intended to be called for automated tuning, to apply filtering...
  9103. #define IS_DDR4 1
  9104. #define IS_DDR3 0
  9105. #define IS_RDIMM 1
  9106. #define IS_UDIMM 0
  9107. #define IS_1SLOT 1
  9108. #define IS_2SLOT 0
  9109. // FIXME: DDR3 is not tuned
  9110. static const u32 ddr_speed_filter[2][2][2] = {
  9111. [IS_DDR4] = {
  9112. [IS_RDIMM] = {
  9113. [IS_1SLOT] = 940,
  9114. [IS_2SLOT] = 800},
  9115. [IS_UDIMM] = {
  9116. [IS_1SLOT] = 1050,
  9117. [IS_2SLOT] = 940},
  9118. },
  9119. [IS_DDR3] = {
  9120. [IS_RDIMM] = {
  9121. [IS_1SLOT] = 0, // disabled
  9122. [IS_2SLOT] = 0 // disabled
  9123. },
  9124. [IS_UDIMM] = {
  9125. [IS_1SLOT] = 0, // disabled
  9126. [IS_2SLOT] = 0 // disabled
  9127. }
  9128. }
  9129. };
  9130. void cvmx_maybe_tune_node(struct ddr_priv *priv, u32 ddr_speed)
  9131. {
  9132. const char *s;
  9133. union cvmx_lmcx_config lmc_config;
  9134. union cvmx_lmcx_control lmc_control;
  9135. union cvmx_lmcx_ddr_pll_ctl lmc_ddr_pll_ctl;
  9136. int is_ddr4;
  9137. int is_rdimm;
  9138. int is_1slot;
  9139. int do_tune = 0;
  9140. u32 ddr_min_speed;
  9141. int node = 0;
  9142. // scale it down from Hz to MHz
  9143. ddr_speed = divide_nint(ddr_speed, 1000000);
  9144. // FIXME: allow an override here so that all configs can be tuned
  9145. // or none
  9146. // If the envvar is defined, always either force it or avoid it
  9147. // accordingly
  9148. s = env_get("ddr_tune_all_configs");
  9149. if (s) {
  9150. do_tune = !!strtoul(s, NULL, 0);
  9151. printf("N%d: DRAM auto-tuning %s.\n", node,
  9152. (do_tune) ? "forced" : "disabled");
  9153. if (do_tune)
  9154. cvmx_tune_node(priv);
  9155. return;
  9156. }
  9157. // filter the tuning calls here...
  9158. // determine if we should/can run automatically for this configuration
  9159. //
  9160. // FIXME: tune only when the configuration indicates it will help:
  9161. // DDR type, RDIMM or UDIMM, 1-slot or 2-slot, and speed
  9162. //
  9163. lmc_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(0)); // sample LMC0
  9164. lmc_control.u64 = lmc_rd(priv, CVMX_LMCX_CONTROL(0)); // sample LMC0
  9165. // sample LMC0
  9166. lmc_ddr_pll_ctl.u64 = lmc_rd(priv, CVMX_LMCX_DDR_PLL_CTL(0));
  9167. is_ddr4 = (lmc_ddr_pll_ctl.s.ddr4_mode != 0);
  9168. is_rdimm = (lmc_control.s.rdimm_ena != 0);
  9169. // HACK, should do better
  9170. is_1slot = (lmc_config.s.init_status < 4);
  9171. ddr_min_speed = ddr_speed_filter[is_ddr4][is_rdimm][is_1slot];
  9172. do_tune = ((ddr_min_speed != 0) && (ddr_speed > ddr_min_speed));
  9173. debug("N%d: DDR%d %cDIMM %d-slot at %d MHz %s eligible for auto-tuning.\n",
  9174. node, (is_ddr4) ? 4 : 3, (is_rdimm) ? 'R' : 'U',
  9175. (is_1slot) ? 1 : 2, ddr_speed, (do_tune) ? "is" : "is not");
  9176. // call the tuning routine, filtering is done...
  9177. if (do_tune)
  9178. cvmx_tune_node(priv);
  9179. }
  9180. /*
  9181. * first pattern example:
  9182. * GENERAL_PURPOSE0.DATA == 64'h00ff00ff00ff00ff;
  9183. * GENERAL_PURPOSE1.DATA == 64'h00ff00ff00ff00ff;
  9184. * GENERAL_PURPOSE0.DATA == 16'h0000;
  9185. */
  9186. static const u64 dbi_pattern[3] = {
  9187. 0x00ff00ff00ff00ffULL, 0x00ff00ff00ff00ffULL, 0x0000ULL };
  9188. // Perform switchover to DBI
  9189. static void cvmx_dbi_switchover_interface(struct ddr_priv *priv, int lmc)
  9190. {
  9191. union cvmx_lmcx_modereg_params0 modereg_params0;
  9192. union cvmx_lmcx_modereg_params3 modereg_params3;
  9193. union cvmx_lmcx_phy_ctl phy_ctl;
  9194. union cvmx_lmcx_config lmcx_config;
  9195. union cvmx_lmcx_ddr_pll_ctl ddr_pll_ctl;
  9196. int rank_mask, rankx, active_ranks;
  9197. u64 phys_addr, rank_offset;
  9198. int num_lmcs, errors;
  9199. int dbi_settings[9], byte, unlocked, retries;
  9200. int ecc_ena;
  9201. int rank_max = 1; // FIXME: make this 4 to try all the ranks
  9202. int node = 0;
  9203. ddr_pll_ctl.u64 = lmc_rd(priv, CVMX_LMCX_DDR_PLL_CTL(0));
  9204. lmcx_config.u64 = lmc_rd(priv, CVMX_LMCX_CONFIG(lmc));
  9205. rank_mask = lmcx_config.s.init_status;
  9206. ecc_ena = lmcx_config.s.ecc_ena;
  9207. // FIXME: must filter out any non-supported configs
  9208. // ie, no DDR3, no x4 devices
  9209. if (ddr_pll_ctl.s.ddr4_mode == 0 || lmcx_config.s.mode_x4dev == 1) {
  9210. debug("N%d.LMC%d: DBI switchover: inappropriate device; EXITING...\n",
  9211. node, lmc);
  9212. return;
  9213. }
  9214. // this should be correct for 1 or 2 ranks, 1 or 2 DIMMs
  9215. num_lmcs = cvmx_dram_get_num_lmc(priv);
  9216. rank_offset = 1ull << (28 + lmcx_config.s.pbank_lsb -
  9217. lmcx_config.s.rank_ena + (num_lmcs / 2));
  9218. debug("N%d.LMC%d: DBI switchover: rank mask 0x%x, rank size 0x%016llx.\n",
  9219. node, lmc, rank_mask, (unsigned long long)rank_offset);
  9220. /*
  9221. * 1. conduct the current init sequence as usual all the way
  9222. * after software write leveling.
  9223. */
  9224. read_dac_dbi_settings(priv, lmc, /*DBI*/ 0, dbi_settings);
  9225. display_dac_dbi_settings(lmc, /*DBI*/ 0, ecc_ena, dbi_settings,
  9226. " INIT");
  9227. /*
  9228. * 2. set DBI related CSRs as below and issue MR write.
  9229. * MODEREG_PARAMS3.WR_DBI=1
  9230. * MODEREG_PARAMS3.RD_DBI=1
  9231. * PHY_CTL.DBI_MODE_ENA=1
  9232. */
  9233. modereg_params0.u64 = lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS0(lmc));
  9234. modereg_params3.u64 = lmc_rd(priv, CVMX_LMCX_MODEREG_PARAMS3(lmc));
  9235. modereg_params3.s.wr_dbi = 1;
  9236. modereg_params3.s.rd_dbi = 1;
  9237. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS3(lmc), modereg_params3.u64);
  9238. phy_ctl.u64 = lmc_rd(priv, CVMX_LMCX_PHY_CTL(lmc));
  9239. phy_ctl.s.dbi_mode_ena = 1;
  9240. lmc_wr(priv, CVMX_LMCX_PHY_CTL(lmc), phy_ctl.u64);
  9241. /*
  9242. * there are two options for data to send. Lets start with (1)
  9243. * and could move to (2) in the future:
  9244. *
  9245. * 1) DBTRAIN_CTL[LFSR_PATTERN_SEL] = 0 (or for older chips where
  9246. * this does not exist) set data directly in these reigsters.
  9247. * this will yield a clk/2 pattern:
  9248. * GENERAL_PURPOSE0.DATA == 64'h00ff00ff00ff00ff;
  9249. * GENERAL_PURPOSE1.DATA == 64'h00ff00ff00ff00ff;
  9250. * GENERAL_PURPOSE0.DATA == 16'h0000;
  9251. * 2) DBTRAIN_CTL[LFSR_PATTERN_SEL] = 1
  9252. * here data comes from the LFSR generating a PRBS pattern
  9253. * CHAR_CTL.EN = 0
  9254. * CHAR_CTL.SEL = 0; // for PRBS
  9255. * CHAR_CTL.DR = 1;
  9256. * CHAR_CTL.PRBS = setup for whatever type of PRBS to send
  9257. * CHAR_CTL.SKEW_ON = 1;
  9258. */
  9259. lmc_wr(priv, CVMX_LMCX_GENERAL_PURPOSE0(lmc), dbi_pattern[0]);
  9260. lmc_wr(priv, CVMX_LMCX_GENERAL_PURPOSE1(lmc), dbi_pattern[1]);
  9261. lmc_wr(priv, CVMX_LMCX_GENERAL_PURPOSE2(lmc), dbi_pattern[2]);
  9262. /*
  9263. * 3. adjust cas_latency (only necessary if RD_DBI is set).
  9264. * here is my code for doing this:
  9265. *
  9266. * if (csr_model.MODEREG_PARAMS3.RD_DBI.value == 1) begin
  9267. * case (csr_model.MODEREG_PARAMS0.CL.value)
  9268. * 0,1,2,3,4: csr_model.MODEREG_PARAMS0.CL.value += 2;
  9269. * // CL 9-13 -> 11-15
  9270. * 5: begin
  9271. * // CL=14, CWL=10,12 gets +2, CLW=11,14 gets +3
  9272. * if((csr_model.MODEREG_PARAMS0.CWL.value==1 ||
  9273. * csr_model.MODEREG_PARAMS0.CWL.value==3))
  9274. * csr_model.MODEREG_PARAMS0.CL.value = 7; // 14->16
  9275. * else
  9276. * csr_model.MODEREG_PARAMS0.CL.value = 13; // 14->17
  9277. * end
  9278. * 6: csr_model.MODEREG_PARAMS0.CL.value = 8; // 15->18
  9279. * 7: csr_model.MODEREG_PARAMS0.CL.value = 14; // 16->19
  9280. * 8: csr_model.MODEREG_PARAMS0.CL.value = 15; // 18->21
  9281. * default:
  9282. * `cn_fatal(("Error mem_cfg (%s) CL (%d) with RD_DBI=1,
  9283. * I am not sure what to do.",
  9284. * mem_cfg, csr_model.MODEREG_PARAMS3.RD_DBI.value))
  9285. * endcase
  9286. * end
  9287. */
  9288. if (modereg_params3.s.rd_dbi == 1) {
  9289. int old_cl, new_cl, old_cwl;
  9290. old_cl = modereg_params0.s.cl;
  9291. old_cwl = modereg_params0.s.cwl;
  9292. switch (old_cl) {
  9293. case 0:
  9294. case 1:
  9295. case 2:
  9296. case 3:
  9297. case 4:
  9298. new_cl = old_cl + 2;
  9299. break; // 9-13->11-15
  9300. // CL=14, CWL=10,12 gets +2, CLW=11,14 gets +3
  9301. case 5:
  9302. new_cl = ((old_cwl == 1) || (old_cwl == 3)) ? 7 : 13;
  9303. break;
  9304. case 6:
  9305. new_cl = 8;
  9306. break; // 15->18
  9307. case 7:
  9308. new_cl = 14;
  9309. break; // 16->19
  9310. case 8:
  9311. new_cl = 15;
  9312. break; // 18->21
  9313. default:
  9314. printf("ERROR: Bad CL value (%d) for DBI switchover.\n",
  9315. old_cl);
  9316. // FIXME: need to error exit here...
  9317. old_cl = -1;
  9318. new_cl = -1;
  9319. break;
  9320. }
  9321. debug("N%d.LMC%d: DBI switchover: CL ADJ: old_cl 0x%x, old_cwl 0x%x, new_cl 0x%x.\n",
  9322. node, lmc, old_cl, old_cwl, new_cl);
  9323. modereg_params0.s.cl = new_cl;
  9324. lmc_wr(priv, CVMX_LMCX_MODEREG_PARAMS0(lmc),
  9325. modereg_params0.u64);
  9326. }
  9327. /*
  9328. * 4. issue MRW to MR0 (CL) and MR5 (DBI), using LMC sequence
  9329. * SEQ_CTL[SEQ_SEL] = MRW.
  9330. */
  9331. // Use the default values, from the CSRs fields
  9332. // also, do B-sides for RDIMMs...
  9333. for (rankx = 0; rankx < 4; rankx++) {
  9334. if (!(rank_mask & (1 << rankx)))
  9335. continue;
  9336. // for RDIMMs, B-side writes should get done automatically
  9337. // when the A-side is written
  9338. ddr4_mrw(priv, lmc, rankx, -1 /* use_default */,
  9339. 0 /*MRreg */, 0 /*A-side */); /* MR0 */
  9340. ddr4_mrw(priv, lmc, rankx, -1 /* use_default */,
  9341. 5 /*MRreg */, 0 /*A-side */); /* MR5 */
  9342. }
  9343. /*
  9344. * 5. conduct DBI bit deskew training via the General Purpose
  9345. * R/W sequence (dbtrain). may need to run this over and over to get
  9346. * a lock (I need up to 5 in simulation):
  9347. * SEQ_CTL[SEQ_SEL] = RW_TRAINING (15)
  9348. * DBTRAIN_CTL.CMD_COUNT_EXT = all 1's
  9349. * DBTRAIN_CTL.READ_CMD_COUNT = all 1's
  9350. * DBTRAIN_CTL.TCCD_SEL = set according to MODEREG_PARAMS3[TCCD_L]
  9351. * DBTRAIN_CTL.RW_TRAIN = 1
  9352. * DBTRAIN_CTL.READ_DQ_COUNT = dont care
  9353. * DBTRAIN_CTL.WRITE_ENA = 1;
  9354. * DBTRAIN_CTL.ACTIVATE = 1;
  9355. * DBTRAIN_CTL LRANK, PRANK, ROW_A, BG, BA, COLUMN_A = set to a
  9356. * valid address
  9357. */
  9358. // NOW - do the training
  9359. debug("N%d.LMC%d: DBI switchover: TRAINING begins...\n", node, lmc);
  9360. active_ranks = 0;
  9361. for (rankx = 0; rankx < rank_max; rankx++) {
  9362. if (!(rank_mask & (1 << rankx)))
  9363. continue;
  9364. phys_addr = rank_offset * active_ranks;
  9365. // FIXME: now done by test_dram_byte_hw()
  9366. active_ranks++;
  9367. retries = 0;
  9368. restart_training:
  9369. // NOTE: return is a bitmask of the erroring bytelanes -
  9370. // we only print it
  9371. errors =
  9372. test_dram_byte_hw(priv, lmc, phys_addr, DBTRAIN_DBI, NULL);
  9373. debug("N%d.LMC%d: DBI switchover: TEST: rank %d, phys_addr 0x%llx, errors 0x%x.\n",
  9374. node, lmc, rankx, (unsigned long long)phys_addr, errors);
  9375. // NEXT - check for locking
  9376. unlocked = 0;
  9377. read_dac_dbi_settings(priv, lmc, /*DBI*/ 0, dbi_settings);
  9378. for (byte = 0; byte < (8 + ecc_ena); byte++)
  9379. unlocked += (dbi_settings[byte] & 1) ^ 1;
  9380. // FIXME: print out the DBI settings array after each rank?
  9381. if (rank_max > 1) // only when doing more than 1 rank
  9382. display_dac_dbi_settings(lmc, /*DBI*/ 0, ecc_ena,
  9383. dbi_settings, " RANK");
  9384. if (unlocked > 0) {
  9385. debug("N%d.LMC%d: DBI switchover: LOCK: %d still unlocked.\n",
  9386. node, lmc, unlocked);
  9387. retries++;
  9388. if (retries < 10) {
  9389. goto restart_training;
  9390. } else {
  9391. debug("N%d.LMC%d: DBI switchover: LOCK: %d retries exhausted.\n",
  9392. node, lmc, retries);
  9393. }
  9394. }
  9395. } /* for (rankx = 0; rankx < 4; rankx++) */
  9396. // print out the final DBI settings array
  9397. display_dac_dbi_settings(lmc, /*DBI*/ 0, ecc_ena, dbi_settings,
  9398. "FINAL");
  9399. }
  9400. void cvmx_dbi_switchover(struct ddr_priv *priv)
  9401. {
  9402. int lmc;
  9403. int num_lmcs = cvmx_dram_get_num_lmc(priv);
  9404. for (lmc = 0; lmc < num_lmcs; lmc++)
  9405. cvmx_dbi_switchover_interface(priv, lmc);
  9406. }