boot_init_dram.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499
  1. /*
  2. * Copyright (c) 2015-2023, Renesas Electronics Corporation.
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <common/debug.h>
  11. #include <lib/mmio.h>
  12. #include "ddr_regdef.h"
  13. #include "init_dram_tbl_h3.h"
  14. #include "init_dram_tbl_m3.h"
  15. #include "init_dram_tbl_h3ver2.h"
  16. #include "init_dram_tbl_m3n.h"
  17. #include "boot_init_dram_regdef.h"
  18. #include "boot_init_dram.h"
  19. #include "dram_sub_func.h"
  20. #include "micro_delay.h"
  21. #include "rcar_def.h"
  22. #define DDR_BACKUPMODE
  23. #define FATAL_MSG(x) NOTICE(x)
  24. /* variables */
  25. #ifdef RCAR_DDR_FIXED_LSI_TYPE
  26. #ifndef RCAR_AUTO
  27. #define RCAR_AUTO 99
  28. #define RCAR_H3 0
  29. #define RCAR_M3 1
  30. #define RCAR_M3N 2
  31. #define RCAR_E3 3 /* NON */
  32. #define RCAR_H3N 4
  33. #define RZ_G2M 100U
  34. #define RZ_G2H 101U
  35. #define RZ_G2N 102U
  36. #define RCAR_CUT_10 0
  37. #define RCAR_CUT_11 1
  38. #define RCAR_CUT_20 10
  39. #define RCAR_CUT_30 20
  40. #endif
  41. #ifndef RCAR_LSI
  42. #define RCAR_LSI RCAR_AUTO
  43. #endif
  44. #if (RCAR_LSI == RCAR_AUTO)
  45. static uint32_t prr_product;
  46. static uint32_t prr_cut;
  47. #else
  48. #if (RCAR_LSI == RCAR_H3)
  49. static const uint32_t prr_product = PRR_PRODUCT_H3;
  50. #elif(RCAR_LSI == RCAR_M3 || RCAR_LSI == RZ_G2M)
  51. static const uint32_t prr_product = PRR_PRODUCT_M3;
  52. #elif(RCAR_LSI == RCAR_M3N || RCAR_LSI == RZ_G2N)
  53. static const uint32_t prr_product = PRR_PRODUCT_M3N;
  54. #elif(RCAR_LSI == RCAR_H3N || RCAR_LSI == RZ_G2H)
  55. static const uint32_t prr_product = PRR_PRODUCT_H3;
  56. #endif /* RCAR_LSI */
  57. #ifndef RCAR_LSI_CUT
  58. static uint32_t prr_cut;
  59. #else /* RCAR_LSI_CUT */
  60. #if (RCAR_LSI_CUT == RCAR_CUT_10)
  61. static const uint32_t prr_cut = PRR_PRODUCT_10;
  62. #elif(RCAR_LSI_CUT == RCAR_CUT_11)
  63. static const uint32_t prr_cut = PRR_PRODUCT_11;
  64. #elif(RCAR_LSI_CUT == RCAR_CUT_20)
  65. static const uint32_t prr_cut = PRR_PRODUCT_20;
  66. #elif(RCAR_LSI_CUT == RCAR_CUT_30)
  67. static const uint32_t prr_cut = PRR_PRODUCT_30;
  68. #endif /* RCAR_LSI_CUT */
  69. #endif /* RCAR_LSI_CUT */
  70. #endif /* RCAR_AUTO_NON */
  71. #else /* RCAR_DDR_FIXED_LSI_TYPE */
  72. static uint32_t prr_product;
  73. static uint32_t prr_cut;
  74. #endif /* RCAR_DDR_FIXED_LSI_TYPE */
  75. static const uint32_t *p_ddr_regdef_tbl;
  76. static uint32_t brd_clk;
  77. static uint32_t brd_clkdiv;
  78. static uint32_t brd_clkdiva;
  79. static uint32_t ddr_mbps;
  80. static uint32_t ddr_mbpsdiv;
  81. static uint32_t ddr_tccd;
  82. static uint32_t ddr_phycaslice;
  83. static const struct _boardcnf *board_cnf;
  84. static uint32_t ddr_phyvalid;
  85. static uint32_t ddr_density[DRAM_CH_CNT][CS_CNT];
  86. static uint32_t ch_have_this_cs[CS_CNT] __aligned(64);
  87. static uint32_t rdqdm_dly[DRAM_CH_CNT][CSAB_CNT][SLICE_CNT * 2][9];
  88. static uint32_t max_density;
  89. static uint32_t ddr0800_mul;
  90. static uint32_t ddr_mul;
  91. static uint32_t DDR_PHY_SLICE_REGSET_OFS;
  92. static uint32_t DDR_PHY_ADR_V_REGSET_OFS;
  93. static uint32_t DDR_PHY_ADR_I_REGSET_OFS;
  94. static uint32_t DDR_PHY_ADR_G_REGSET_OFS;
  95. static uint32_t DDR_PI_REGSET_OFS;
  96. static uint32_t DDR_PHY_SLICE_REGSET_SIZE;
  97. static uint32_t DDR_PHY_ADR_V_REGSET_SIZE;
  98. static uint32_t DDR_PHY_ADR_I_REGSET_SIZE;
  99. static uint32_t DDR_PHY_ADR_G_REGSET_SIZE;
  100. static uint32_t DDR_PI_REGSET_SIZE;
  101. static uint32_t DDR_PHY_SLICE_REGSET_NUM;
  102. static uint32_t DDR_PHY_ADR_V_REGSET_NUM;
  103. static uint32_t DDR_PHY_ADR_I_REGSET_NUM;
  104. static uint32_t DDR_PHY_ADR_G_REGSET_NUM;
  105. static uint32_t DDR_PI_REGSET_NUM;
  106. static uint32_t DDR_PHY_ADR_I_NUM;
  107. #define DDR_PHY_REGSET_MAX 128
  108. #define DDR_PI_REGSET_MAX 320
  109. static uint32_t _cnf_DDR_PHY_SLICE_REGSET[DDR_PHY_REGSET_MAX];
  110. static uint32_t _cnf_DDR_PHY_ADR_V_REGSET[DDR_PHY_REGSET_MAX];
  111. static uint32_t _cnf_DDR_PHY_ADR_I_REGSET[DDR_PHY_REGSET_MAX];
  112. static uint32_t _cnf_DDR_PHY_ADR_G_REGSET[DDR_PHY_REGSET_MAX];
  113. static uint32_t _cnf_DDR_PI_REGSET[DDR_PI_REGSET_MAX];
  114. static uint32_t pll3_mode;
  115. static uint32_t loop_max;
  116. #ifdef DDR_BACKUPMODE
  117. uint32_t ddr_backup;
  118. /* #define DDR_BACKUPMODE_HALF //for Half channel(ch0,1 only) */
  119. #endif
  120. #ifdef ddr_qos_init_setting /* only for non qos_init */
  121. #define OPERATING_FREQ (400U) /* Mhz */
  122. #define BASE_SUB_SLOT_NUM (0x6U)
  123. #define SUB_SLOT_CYCLE (0x7EU) /* 126 */
  124. #define QOSWT_WTSET0_CYCLE \
  125. ((SUB_SLOT_CYCLE * BASE_SUB_SLOT_NUM * 1000U) / \
  126. OPERATING_FREQ) /* unit:ns */
  127. uint32_t get_refperiod(void)
  128. {
  129. return QOSWT_WTSET0_CYCLE;
  130. }
  131. #else /* ddr_qos_init_setting // only for non qos_init */
  132. extern uint32_t get_refperiod(void);
  133. #endif /* ddr_qos_init_setting // only for non qos_init */
  134. #define _reg_PHY_RX_CAL_X_NUM 11
  135. static const uint32_t _reg_PHY_RX_CAL_X[_reg_PHY_RX_CAL_X_NUM] = {
  136. _reg_PHY_RX_CAL_DQ0,
  137. _reg_PHY_RX_CAL_DQ1,
  138. _reg_PHY_RX_CAL_DQ2,
  139. _reg_PHY_RX_CAL_DQ3,
  140. _reg_PHY_RX_CAL_DQ4,
  141. _reg_PHY_RX_CAL_DQ5,
  142. _reg_PHY_RX_CAL_DQ6,
  143. _reg_PHY_RX_CAL_DQ7,
  144. _reg_PHY_RX_CAL_DM,
  145. _reg_PHY_RX_CAL_DQS,
  146. _reg_PHY_RX_CAL_FDBK
  147. };
  148. #define _reg_PHY_CLK_WRX_SLAVE_DELAY_NUM 10
  149. static const uint32_t _reg_PHY_CLK_WRX_SLAVE_DELAY
  150. [_reg_PHY_CLK_WRX_SLAVE_DELAY_NUM] = {
  151. _reg_PHY_CLK_WRDQ0_SLAVE_DELAY,
  152. _reg_PHY_CLK_WRDQ1_SLAVE_DELAY,
  153. _reg_PHY_CLK_WRDQ2_SLAVE_DELAY,
  154. _reg_PHY_CLK_WRDQ3_SLAVE_DELAY,
  155. _reg_PHY_CLK_WRDQ4_SLAVE_DELAY,
  156. _reg_PHY_CLK_WRDQ5_SLAVE_DELAY,
  157. _reg_PHY_CLK_WRDQ6_SLAVE_DELAY,
  158. _reg_PHY_CLK_WRDQ7_SLAVE_DELAY,
  159. _reg_PHY_CLK_WRDM_SLAVE_DELAY,
  160. _reg_PHY_CLK_WRDQS_SLAVE_DELAY
  161. };
  162. #define _reg_PHY_RDDQS_X_FALL_SLAVE_DELAY_NUM 9
  163. static const uint32_t _reg_PHY_RDDQS_X_FALL_SLAVE_DELAY
  164. [_reg_PHY_RDDQS_X_FALL_SLAVE_DELAY_NUM] = {
  165. _reg_PHY_RDDQS_DQ0_FALL_SLAVE_DELAY,
  166. _reg_PHY_RDDQS_DQ1_FALL_SLAVE_DELAY,
  167. _reg_PHY_RDDQS_DQ2_FALL_SLAVE_DELAY,
  168. _reg_PHY_RDDQS_DQ3_FALL_SLAVE_DELAY,
  169. _reg_PHY_RDDQS_DQ4_FALL_SLAVE_DELAY,
  170. _reg_PHY_RDDQS_DQ5_FALL_SLAVE_DELAY,
  171. _reg_PHY_RDDQS_DQ6_FALL_SLAVE_DELAY,
  172. _reg_PHY_RDDQS_DQ7_FALL_SLAVE_DELAY,
  173. _reg_PHY_RDDQS_DM_FALL_SLAVE_DELAY
  174. };
  175. #define _reg_PHY_RDDQS_X_RISE_SLAVE_DELAY_NUM 9
  176. static const uint32_t _reg_PHY_RDDQS_X_RISE_SLAVE_DELAY
  177. [_reg_PHY_RDDQS_X_RISE_SLAVE_DELAY_NUM] = {
  178. _reg_PHY_RDDQS_DQ0_RISE_SLAVE_DELAY,
  179. _reg_PHY_RDDQS_DQ1_RISE_SLAVE_DELAY,
  180. _reg_PHY_RDDQS_DQ2_RISE_SLAVE_DELAY,
  181. _reg_PHY_RDDQS_DQ3_RISE_SLAVE_DELAY,
  182. _reg_PHY_RDDQS_DQ4_RISE_SLAVE_DELAY,
  183. _reg_PHY_RDDQS_DQ5_RISE_SLAVE_DELAY,
  184. _reg_PHY_RDDQS_DQ6_RISE_SLAVE_DELAY,
  185. _reg_PHY_RDDQS_DQ7_RISE_SLAVE_DELAY,
  186. _reg_PHY_RDDQS_DM_RISE_SLAVE_DELAY
  187. };
  188. #define _reg_PHY_PAD_TERM_X_NUM 8
  189. static const uint32_t _reg_PHY_PAD_TERM_X[_reg_PHY_PAD_TERM_X_NUM] = {
  190. _reg_PHY_PAD_FDBK_TERM,
  191. _reg_PHY_PAD_DATA_TERM,
  192. _reg_PHY_PAD_DQS_TERM,
  193. _reg_PHY_PAD_ADDR_TERM,
  194. _reg_PHY_PAD_CLK_TERM,
  195. _reg_PHY_PAD_CKE_TERM,
  196. _reg_PHY_PAD_RST_TERM,
  197. _reg_PHY_PAD_CS_TERM
  198. };
  199. #define _reg_PHY_CLK_CACS_SLAVE_DELAY_X_NUM 10
  200. static const uint32_t _reg_PHY_CLK_CACS_SLAVE_DELAY_X
  201. [_reg_PHY_CLK_CACS_SLAVE_DELAY_X_NUM] = {
  202. _reg_PHY_ADR0_CLK_WR_SLAVE_DELAY,
  203. _reg_PHY_ADR1_CLK_WR_SLAVE_DELAY,
  204. _reg_PHY_ADR2_CLK_WR_SLAVE_DELAY,
  205. _reg_PHY_ADR3_CLK_WR_SLAVE_DELAY,
  206. _reg_PHY_ADR4_CLK_WR_SLAVE_DELAY,
  207. _reg_PHY_ADR5_CLK_WR_SLAVE_DELAY,
  208. _reg_PHY_GRP_SLAVE_DELAY_0,
  209. _reg_PHY_GRP_SLAVE_DELAY_1,
  210. _reg_PHY_GRP_SLAVE_DELAY_2,
  211. _reg_PHY_GRP_SLAVE_DELAY_3
  212. };
  213. /* Prototypes */
  214. static inline uint32_t vch_nxt(uint32_t pos);
  215. static void cpg_write_32(uint32_t a, uint32_t v);
  216. static void pll3_control(uint32_t high);
  217. static inline void dsb_sev(void);
  218. static void wait_dbcmd(void);
  219. static void send_dbcmd(uint32_t cmd);
  220. static uint32_t reg_ddrphy_read(uint32_t phyno, uint32_t regadd);
  221. static void reg_ddrphy_write(uint32_t phyno, uint32_t regadd, uint32_t regdata);
  222. static void reg_ddrphy_write_a(uint32_t regadd, uint32_t regdata);
  223. static inline uint32_t ddr_regdef(uint32_t _regdef);
  224. static inline uint32_t ddr_regdef_adr(uint32_t _regdef);
  225. static inline uint32_t ddr_regdef_lsb(uint32_t _regdef);
  226. static void ddr_setval_s(uint32_t ch, uint32_t slice, uint32_t _regdef,
  227. uint32_t val);
  228. static uint32_t ddr_getval_s(uint32_t ch, uint32_t slice, uint32_t _regdef);
  229. static void ddr_setval(uint32_t ch, uint32_t regdef, uint32_t val);
  230. static void ddr_setval_ach_s(uint32_t slice, uint32_t regdef, uint32_t val);
  231. static void ddr_setval_ach(uint32_t regdef, uint32_t val);
  232. static void ddr_setval_ach_as(uint32_t regdef, uint32_t val);
  233. static uint32_t ddr_getval(uint32_t ch, uint32_t regdef);
  234. static uint32_t ddr_getval_ach(uint32_t regdef, uint32_t *p);
  235. static uint32_t ddr_getval_ach_as(uint32_t regdef, uint32_t *p);
  236. static void _tblcopy(uint32_t *to, const uint32_t *from, uint32_t size);
  237. static void ddrtbl_setval(uint32_t *tbl, uint32_t _regdef, uint32_t val);
  238. static uint32_t ddrtbl_getval(uint32_t *tbl, uint32_t _regdef);
  239. static uint32_t ddrphy_regif_chk(void);
  240. static inline void ddrphy_regif_idle(void);
  241. static uint16_t _f_scale(uint32_t _ddr_mbps, uint32_t _ddr_mbpsdiv, uint32_t ps,
  242. uint16_t cyc);
  243. static void _f_scale_js2(uint32_t _ddr_mbps, uint32_t _ddr_mbpsdiv,
  244. uint16_t *_js2);
  245. static int16_t _f_scale_adj(int16_t ps);
  246. static void ddrtbl_load(void);
  247. static void ddr_config_sub(void);
  248. static void get_ca_swizzle(uint32_t ch, uint32_t ddr_csn, uint32_t *p_swz);
  249. static void ddr_config_sub_h3v1x(void);
  250. static void ddr_config(void);
  251. static void dbsc_regset(void);
  252. static void dbsc_regset_post(void);
  253. static uint32_t dfi_init_start(void);
  254. static void change_lpddr4_en(uint32_t mode);
  255. static uint32_t set_term_code(void);
  256. static void ddr_register_set(void);
  257. static inline uint32_t wait_freqchgreq(uint32_t assert);
  258. static inline void set_freqchgack(uint32_t assert);
  259. static inline void set_dfifrequency(uint32_t freq);
  260. static uint32_t pll3_freq(uint32_t on);
  261. static void update_dly(void);
  262. static uint32_t pi_training_go(void);
  263. static uint32_t init_ddr(void);
  264. static uint32_t swlvl1(uint32_t ddr_csn, uint32_t reg_cs, uint32_t reg_kick);
  265. static uint32_t wdqdm_man1(void);
  266. static uint32_t wdqdm_man(void);
  267. static uint32_t rdqdm_man1(void);
  268. static uint32_t rdqdm_man(void);
  269. static int32_t _find_change(uint64_t val, uint32_t dir);
  270. static uint32_t _rx_offset_cal_updn(uint32_t code);
  271. static uint32_t rx_offset_cal(void);
  272. static uint32_t rx_offset_cal_hw(void);
  273. static void adjust_rddqs_latency(void);
  274. static void adjust_wpath_latency(void);
  275. struct ddrt_data {
  276. int32_t init_temp; /* Initial Temperature (do) */
  277. uint32_t init_cal[4]; /* Initial io-code (4 is for H3) */
  278. uint32_t tcomp_cal[4]; /* Temp. compensated io-code (4 is for H3) */
  279. };
  280. static struct ddrt_data tcal;
  281. static void pvtcode_update(void);
  282. static void pvtcode_update2(void);
  283. static void ddr_padcal_tcompensate_getinit(uint32_t override);
  284. /* load board configuration */
  285. #include "boot_init_dram_config.c"
  286. #ifndef DDR_FAST_INIT
  287. static uint32_t rdqdm_le[DRAM_CH_CNT][CS_CNT][SLICE_CNT * 2][9];
  288. static uint32_t rdqdm_te[DRAM_CH_CNT][CS_CNT][SLICE_CNT * 2][9];
  289. static uint32_t rdqdm_nw[DRAM_CH_CNT][CS_CNT][SLICE_CNT * 2][9];
  290. static uint32_t rdqdm_win[DRAM_CH_CNT][CS_CNT][SLICE_CNT];
  291. static uint32_t rdqdm_st[DRAM_CH_CNT][CS_CNT][SLICE_CNT * 2];
  292. static void rdqdm_clr1(uint32_t ch, uint32_t ddr_csn);
  293. static uint32_t rdqdm_ana1(uint32_t ch, uint32_t ddr_csn);
  294. static uint32_t wdqdm_le[DRAM_CH_CNT][CS_CNT][SLICE_CNT][9];
  295. static uint32_t wdqdm_te[DRAM_CH_CNT][CS_CNT][SLICE_CNT][9];
  296. static uint32_t wdqdm_dly[DRAM_CH_CNT][CS_CNT][SLICE_CNT][9];
  297. static uint32_t wdqdm_st[DRAM_CH_CNT][CS_CNT][SLICE_CNT];
  298. static uint32_t wdqdm_win[DRAM_CH_CNT][CS_CNT][SLICE_CNT];
  299. static void wdqdm_clr1(uint32_t ch, uint32_t ddr_csn);
  300. static uint32_t wdqdm_ana1(uint32_t ch, uint32_t ddr_csn);
  301. #endif/* DDR_FAST_INIT */
  302. /* macro for channel selection loop */
  303. static inline uint32_t vch_nxt(uint32_t pos)
  304. {
  305. uint32_t posn;
  306. for (posn = pos; posn < DRAM_CH_CNT; posn++) {
  307. if (ddr_phyvalid & (1U << posn))
  308. break;
  309. }
  310. return posn;
  311. }
  312. #define foreach_vch(ch) \
  313. for (ch = vch_nxt(0); ch < DRAM_CH_CNT; ch = vch_nxt(ch + 1))
  314. #define foreach_ech(ch) \
  315. for (ch = 0; ch < DRAM_CH_CNT; ch++)
  316. /* Printing functions */
  317. #define MSG_LF(...)
  318. /* clock settings, reset control */
  319. static void cpg_write_32(uint32_t a, uint32_t v)
  320. {
  321. mmio_write_32(CPG_CPGWPR, ~v);
  322. mmio_write_32(a, v);
  323. }
  324. static void pll3_control(uint32_t high)
  325. {
  326. uint32_t data_l, data_div, data_mul, tmp_div;
  327. if (high) {
  328. tmp_div = 3999 * brd_clkdiv * (brd_clkdiva + 1) /
  329. (brd_clk * ddr_mul) / 2;
  330. data_mul = ((ddr_mul * tmp_div) - 1) << 24;
  331. pll3_mode = 1;
  332. loop_max = 2;
  333. } else {
  334. tmp_div = 3999 * brd_clkdiv * (brd_clkdiva + 1) /
  335. (brd_clk * ddr0800_mul) / 2;
  336. data_mul = ((ddr0800_mul * tmp_div) - 1) << 24;
  337. pll3_mode = 0;
  338. loop_max = 8;
  339. }
  340. switch (tmp_div) {
  341. case 1:
  342. data_div = 0;
  343. break;
  344. case 2:
  345. case 3:
  346. case 4:
  347. data_div = tmp_div;
  348. break;
  349. default:
  350. data_div = 6;
  351. data_mul = (data_mul * tmp_div) / 3;
  352. break;
  353. }
  354. data_mul = data_mul | (brd_clkdiva << 7);
  355. /* PLL3 disable */
  356. data_l = mmio_read_32(CPG_PLLECR) & ~CPG_PLLECR_PLL3E_BIT;
  357. cpg_write_32(CPG_PLLECR, data_l);
  358. dsb_sev();
  359. if ((prr_product == PRR_PRODUCT_M3) ||
  360. ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_20))) {
  361. /* PLL3 DIV resetting(Lowest value:3) */
  362. data_l = 0x00030003 | (0xFF80FF80 & mmio_read_32(CPG_FRQCRD));
  363. cpg_write_32(CPG_FRQCRD, data_l);
  364. dsb_sev();
  365. /* zb3 clk stop */
  366. data_l = CPG_ZB3CKCR_ZB3ST_BIT | mmio_read_32(CPG_ZB3CKCR);
  367. cpg_write_32(CPG_ZB3CKCR, data_l);
  368. dsb_sev();
  369. /* PLL3 enable */
  370. data_l = CPG_PLLECR_PLL3E_BIT | mmio_read_32(CPG_PLLECR);
  371. cpg_write_32(CPG_PLLECR, data_l);
  372. dsb_sev();
  373. do {
  374. data_l = mmio_read_32(CPG_PLLECR);
  375. } while ((data_l & CPG_PLLECR_PLL3ST_BIT) == 0);
  376. dsb_sev();
  377. /* PLL3 DIV resetting (Highest value:0) */
  378. data_l = (0xFF80FF80 & mmio_read_32(CPG_FRQCRD));
  379. cpg_write_32(CPG_FRQCRD, data_l);
  380. dsb_sev();
  381. /* DIV SET KICK */
  382. data_l = CPG_FRQCRB_KICK_BIT | mmio_read_32(CPG_FRQCRB);
  383. cpg_write_32(CPG_FRQCRB, data_l);
  384. dsb_sev();
  385. /* PLL3 multiplie set */
  386. cpg_write_32(CPG_PLL3CR, data_mul);
  387. dsb_sev();
  388. do {
  389. data_l = mmio_read_32(CPG_PLLECR);
  390. } while ((data_l & CPG_PLLECR_PLL3ST_BIT) == 0);
  391. dsb_sev();
  392. /* PLL3 DIV resetting(Target value) */
  393. data_l = (data_div << 16) | data_div |
  394. (mmio_read_32(CPG_FRQCRD) & 0xFF80FF80);
  395. cpg_write_32(CPG_FRQCRD, data_l);
  396. dsb_sev();
  397. /* DIV SET KICK */
  398. data_l = CPG_FRQCRB_KICK_BIT | mmio_read_32(CPG_FRQCRB);
  399. cpg_write_32(CPG_FRQCRB, data_l);
  400. dsb_sev();
  401. do {
  402. data_l = mmio_read_32(CPG_PLLECR);
  403. } while ((data_l & CPG_PLLECR_PLL3ST_BIT) == 0);
  404. dsb_sev();
  405. /* zb3 clk start */
  406. data_l = (~CPG_ZB3CKCR_ZB3ST_BIT) & mmio_read_32(CPG_ZB3CKCR);
  407. cpg_write_32(CPG_ZB3CKCR, data_l);
  408. dsb_sev();
  409. } else { /* H3Ver.3.0/M3N/V3H */
  410. /* PLL3 multiplie set */
  411. cpg_write_32(CPG_PLL3CR, data_mul);
  412. dsb_sev();
  413. /* PLL3 DIV set(Target value) */
  414. data_l = (data_div << 16) | data_div |
  415. (mmio_read_32(CPG_FRQCRD) & 0xFF80FF80);
  416. cpg_write_32(CPG_FRQCRD, data_l);
  417. /* DIV SET KICK */
  418. data_l = CPG_FRQCRB_KICK_BIT | mmio_read_32(CPG_FRQCRB);
  419. cpg_write_32(CPG_FRQCRB, data_l);
  420. dsb_sev();
  421. /* PLL3 enable */
  422. data_l = CPG_PLLECR_PLL3E_BIT | mmio_read_32(CPG_PLLECR);
  423. cpg_write_32(CPG_PLLECR, data_l);
  424. dsb_sev();
  425. do {
  426. data_l = mmio_read_32(CPG_PLLECR);
  427. } while ((data_l & CPG_PLLECR_PLL3ST_BIT) == 0);
  428. dsb_sev();
  429. }
  430. }
  431. /* barrier */
  432. static inline void dsb_sev(void)
  433. {
  434. __asm__ __volatile__("dsb sy");
  435. }
  436. /* DDR memory register access */
  437. static void wait_dbcmd(void)
  438. {
  439. uint32_t data_l;
  440. /* dummy read */
  441. data_l = mmio_read_32(DBSC_DBCMD);
  442. dsb_sev();
  443. while (1) {
  444. /* wait DBCMD 1=busy, 0=ready */
  445. data_l = mmio_read_32(DBSC_DBWAIT);
  446. dsb_sev();
  447. if ((data_l & 0x00000001) == 0x00)
  448. break;
  449. }
  450. }
  451. static void send_dbcmd(uint32_t cmd)
  452. {
  453. /* dummy read */
  454. wait_dbcmd();
  455. mmio_write_32(DBSC_DBCMD, cmd);
  456. dsb_sev();
  457. }
  458. static void dbwait_loop(uint32_t wait_loop)
  459. {
  460. uint32_t i;
  461. for (i = 0; i < wait_loop; i++)
  462. wait_dbcmd();
  463. }
  464. /* DDRPHY register access (raw) */
  465. static uint32_t reg_ddrphy_read(uint32_t phyno, uint32_t regadd)
  466. {
  467. uint32_t val;
  468. uint32_t loop;
  469. val = 0;
  470. if ((prr_product != PRR_PRODUCT_M3N) &&
  471. (prr_product != PRR_PRODUCT_V3H)) {
  472. mmio_write_32(DBSC_DBPDRGA(phyno), regadd);
  473. dsb_sev();
  474. while (mmio_read_32(DBSC_DBPDRGA(phyno)) != regadd) {
  475. dsb_sev();
  476. }
  477. dsb_sev();
  478. for (loop = 0; loop < loop_max; loop++) {
  479. val = mmio_read_32(DBSC_DBPDRGD(phyno));
  480. dsb_sev();
  481. }
  482. (void)val;
  483. } else {
  484. mmio_write_32(DBSC_DBPDRGA(phyno), regadd | 0x00004000);
  485. dsb_sev();
  486. while (mmio_read_32(DBSC_DBPDRGA(phyno)) !=
  487. (regadd | 0x0000C000)) {
  488. dsb_sev();
  489. };
  490. val = mmio_read_32(DBSC_DBPDRGA(phyno));
  491. mmio_write_32(DBSC_DBPDRGA(phyno), regadd | 0x00008000);
  492. while (mmio_read_32(DBSC_DBPDRGA(phyno)) != regadd) {
  493. dsb_sev();
  494. };
  495. dsb_sev();
  496. mmio_write_32(DBSC_DBPDRGA(phyno), regadd | 0x00008000);
  497. while (mmio_read_32(DBSC_DBPDRGA(phyno)) != regadd) {
  498. dsb_sev();
  499. };
  500. dsb_sev();
  501. val = mmio_read_32(DBSC_DBPDRGD(phyno));
  502. dsb_sev();
  503. (void)val;
  504. }
  505. return val;
  506. }
  507. static void reg_ddrphy_write(uint32_t phyno, uint32_t regadd, uint32_t regdata)
  508. {
  509. uint32_t val;
  510. uint32_t loop;
  511. if ((prr_product != PRR_PRODUCT_M3N) &&
  512. (prr_product != PRR_PRODUCT_V3H)) {
  513. mmio_write_32(DBSC_DBPDRGA(phyno), regadd);
  514. dsb_sev();
  515. for (loop = 0; loop < loop_max; loop++) {
  516. val = mmio_read_32(DBSC_DBPDRGA(phyno));
  517. dsb_sev();
  518. }
  519. mmio_write_32(DBSC_DBPDRGD(phyno), regdata);
  520. dsb_sev();
  521. for (loop = 0; loop < loop_max; loop++) {
  522. val = mmio_read_32(DBSC_DBPDRGD(phyno));
  523. dsb_sev();
  524. }
  525. } else {
  526. mmio_write_32(DBSC_DBPDRGA(phyno), regadd);
  527. dsb_sev();
  528. while (mmio_read_32(DBSC_DBPDRGA(phyno)) != regadd) {
  529. dsb_sev();
  530. };
  531. dsb_sev();
  532. mmio_write_32(DBSC_DBPDRGD(phyno), regdata);
  533. dsb_sev();
  534. while (mmio_read_32(DBSC_DBPDRGA(phyno)) !=
  535. (regadd | 0x00008000)) {
  536. dsb_sev();
  537. };
  538. mmio_write_32(DBSC_DBPDRGA(phyno), regadd | 0x00008000);
  539. while (mmio_read_32(DBSC_DBPDRGA(phyno)) != regadd) {
  540. dsb_sev();
  541. };
  542. dsb_sev();
  543. mmio_write_32(DBSC_DBPDRGA(phyno), regadd);
  544. }
  545. (void)val;
  546. }
  547. static void reg_ddrphy_write_a(uint32_t regadd, uint32_t regdata)
  548. {
  549. uint32_t ch;
  550. uint32_t val;
  551. uint32_t loop;
  552. if ((prr_product != PRR_PRODUCT_M3N) &&
  553. (prr_product != PRR_PRODUCT_V3H)) {
  554. foreach_vch(ch) {
  555. mmio_write_32(DBSC_DBPDRGA(ch), regadd);
  556. dsb_sev();
  557. }
  558. foreach_vch(ch) {
  559. mmio_write_32(DBSC_DBPDRGD(ch), regdata);
  560. dsb_sev();
  561. }
  562. for (loop = 0; loop < loop_max; loop++) {
  563. val = mmio_read_32(DBSC_DBPDRGD(0));
  564. dsb_sev();
  565. }
  566. (void)val;
  567. } else {
  568. foreach_vch(ch) {
  569. reg_ddrphy_write(ch, regadd, regdata);
  570. dsb_sev();
  571. }
  572. }
  573. }
  574. static inline void ddrphy_regif_idle(void)
  575. {
  576. uint32_t val;
  577. val = reg_ddrphy_read(0, ddr_regdef_adr(_reg_PI_INT_STATUS));
  578. dsb_sev();
  579. (void)val;
  580. }
  581. /* DDRPHY register access (field modify) */
  582. static inline uint32_t ddr_regdef(uint32_t _regdef)
  583. {
  584. return p_ddr_regdef_tbl[_regdef];
  585. }
  586. static inline uint32_t ddr_regdef_adr(uint32_t _regdef)
  587. {
  588. return DDR_REGDEF_ADR(p_ddr_regdef_tbl[_regdef]);
  589. }
  590. static inline uint32_t ddr_regdef_lsb(uint32_t _regdef)
  591. {
  592. return DDR_REGDEF_LSB(p_ddr_regdef_tbl[_regdef]);
  593. }
  594. static void ddr_setval_s(uint32_t ch, uint32_t slice, uint32_t _regdef,
  595. uint32_t val)
  596. {
  597. uint32_t adr;
  598. uint32_t lsb;
  599. uint32_t len;
  600. uint32_t msk;
  601. uint32_t tmp;
  602. uint32_t regdef;
  603. regdef = ddr_regdef(_regdef);
  604. adr = DDR_REGDEF_ADR(regdef) + 0x80 * slice;
  605. len = DDR_REGDEF_LEN(regdef);
  606. lsb = DDR_REGDEF_LSB(regdef);
  607. if (len == 0x20)
  608. msk = 0xffffffff;
  609. else
  610. msk = ((1U << len) - 1) << lsb;
  611. tmp = reg_ddrphy_read(ch, adr);
  612. tmp = (tmp & (~msk)) | ((val << lsb) & msk);
  613. reg_ddrphy_write(ch, adr, tmp);
  614. }
  615. static uint32_t ddr_getval_s(uint32_t ch, uint32_t slice, uint32_t _regdef)
  616. {
  617. uint32_t adr;
  618. uint32_t lsb;
  619. uint32_t len;
  620. uint32_t msk;
  621. uint32_t tmp;
  622. uint32_t regdef;
  623. regdef = ddr_regdef(_regdef);
  624. adr = DDR_REGDEF_ADR(regdef) + 0x80 * slice;
  625. len = DDR_REGDEF_LEN(regdef);
  626. lsb = DDR_REGDEF_LSB(regdef);
  627. if (len == 0x20)
  628. msk = 0xffffffff;
  629. else
  630. msk = ((1U << len) - 1);
  631. tmp = reg_ddrphy_read(ch, adr);
  632. tmp = (tmp >> lsb) & msk;
  633. return tmp;
  634. }
  635. static void ddr_setval(uint32_t ch, uint32_t regdef, uint32_t val)
  636. {
  637. ddr_setval_s(ch, 0, regdef, val);
  638. }
  639. static void ddr_setval_ach_s(uint32_t slice, uint32_t regdef, uint32_t val)
  640. {
  641. uint32_t ch;
  642. foreach_vch(ch)
  643. ddr_setval_s(ch, slice, regdef, val);
  644. }
  645. static void ddr_setval_ach(uint32_t regdef, uint32_t val)
  646. {
  647. ddr_setval_ach_s(0, regdef, val);
  648. }
  649. static void ddr_setval_ach_as(uint32_t regdef, uint32_t val)
  650. {
  651. uint32_t slice;
  652. for (slice = 0; slice < SLICE_CNT; slice++)
  653. ddr_setval_ach_s(slice, regdef, val);
  654. }
  655. static uint32_t ddr_getval(uint32_t ch, uint32_t regdef)
  656. {
  657. return ddr_getval_s(ch, 0, regdef);
  658. }
  659. static uint32_t ddr_getval_ach(uint32_t regdef, uint32_t *p)
  660. {
  661. uint32_t ch;
  662. foreach_vch(ch)
  663. p[ch] = ddr_getval_s(ch, 0, regdef);
  664. return p[0];
  665. }
  666. static uint32_t ddr_getval_ach_as(uint32_t regdef, uint32_t *p)
  667. {
  668. uint32_t ch, slice;
  669. uint32_t *pp;
  670. pp = p;
  671. foreach_vch(ch)
  672. for (slice = 0; slice < SLICE_CNT; slice++)
  673. *pp++ = ddr_getval_s(ch, slice, regdef);
  674. return p[0];
  675. }
  676. /* handling functions for setteing ddrphy value table */
  677. static void _tblcopy(uint32_t *to, const uint32_t *from, uint32_t size)
  678. {
  679. uint32_t i;
  680. for (i = 0; i < size; i++) {
  681. to[i] = from[i];
  682. }
  683. }
  684. static void ddrtbl_setval(uint32_t *tbl, uint32_t _regdef, uint32_t val)
  685. {
  686. uint32_t adr;
  687. uint32_t lsb;
  688. uint32_t len;
  689. uint32_t msk;
  690. uint32_t tmp;
  691. uint32_t adrmsk;
  692. uint32_t regdef;
  693. regdef = ddr_regdef(_regdef);
  694. adr = DDR_REGDEF_ADR(regdef);
  695. len = DDR_REGDEF_LEN(regdef);
  696. lsb = DDR_REGDEF_LSB(regdef);
  697. if (len == 0x20)
  698. msk = 0xffffffff;
  699. else
  700. msk = ((1U << len) - 1) << lsb;
  701. if (adr < 0x400) {
  702. adrmsk = 0xff;
  703. } else {
  704. adrmsk = 0x7f;
  705. }
  706. tmp = tbl[adr & adrmsk];
  707. tmp = (tmp & (~msk)) | ((val << lsb) & msk);
  708. tbl[adr & adrmsk] = tmp;
  709. }
  710. static uint32_t ddrtbl_getval(uint32_t *tbl, uint32_t _regdef)
  711. {
  712. uint32_t adr;
  713. uint32_t lsb;
  714. uint32_t len;
  715. uint32_t msk;
  716. uint32_t tmp;
  717. uint32_t adrmsk;
  718. uint32_t regdef;
  719. regdef = ddr_regdef(_regdef);
  720. adr = DDR_REGDEF_ADR(regdef);
  721. len = DDR_REGDEF_LEN(regdef);
  722. lsb = DDR_REGDEF_LSB(regdef);
  723. if (len == 0x20)
  724. msk = 0xffffffff;
  725. else
  726. msk = ((1U << len) - 1);
  727. if (adr < 0x400) {
  728. adrmsk = 0xff;
  729. } else {
  730. adrmsk = 0x7f;
  731. }
  732. tmp = tbl[adr & adrmsk];
  733. tmp = (tmp >> lsb) & msk;
  734. return tmp;
  735. }
  736. /* DDRPHY register access handling */
  737. static uint32_t ddrphy_regif_chk(void)
  738. {
  739. uint32_t tmp_ach[DRAM_CH_CNT];
  740. uint32_t ch;
  741. uint32_t err;
  742. uint32_t PI_VERSION_CODE;
  743. if (((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) ||
  744. (prr_product == PRR_PRODUCT_M3)) {
  745. PI_VERSION_CODE = 0x2041; /* H3 Ver.1.x/M3-W */
  746. } else {
  747. PI_VERSION_CODE = 0x2040; /* H3 Ver.2.0 or later/M3-N/V3H */
  748. }
  749. ddr_getval_ach(_reg_PI_VERSION, (uint32_t *)tmp_ach);
  750. err = 0;
  751. foreach_vch(ch) {
  752. if (tmp_ach[ch] != PI_VERSION_CODE)
  753. err = 1;
  754. }
  755. return err;
  756. }
  757. /* functions and parameters for timing setting */
  758. struct _jedec_spec1 {
  759. uint16_t fx3;
  760. uint8_t rlwodbi;
  761. uint8_t rlwdbi;
  762. uint8_t WL;
  763. uint8_t nwr;
  764. uint8_t nrtp;
  765. uint8_t odtlon;
  766. uint8_t MR1;
  767. uint8_t MR2;
  768. };
  769. #define JS1_USABLEC_SPEC_LO 2
  770. #define JS1_USABLEC_SPEC_HI 5
  771. #define JS1_FREQ_TBL_NUM 8
  772. #define JS1_MR1(f) (0x04 | ((f) << 4))
  773. #define JS1_MR2(f) (0x00 | ((f) << 3) | (f))
  774. const struct _jedec_spec1 js1[JS1_FREQ_TBL_NUM] = {
  775. /* 533.333Mbps */
  776. { 800, 6, 6, 4, 6, 8, 0, JS1_MR1(0), JS1_MR2(0) | 0x40 },
  777. /* 1066.666Mbps */
  778. { 1600, 10, 12, 8, 10, 8, 0, JS1_MR1(1), JS1_MR2(1) | 0x40 },
  779. /* 1600.000Mbps */
  780. { 2400, 14, 16, 12, 16, 8, 6, JS1_MR1(2), JS1_MR2(2) | 0x40 },
  781. /* 2133.333Mbps */
  782. { 3200, 20, 22, 10, 20, 8, 4, JS1_MR1(3), JS1_MR2(3) },
  783. /* 2666.666Mbps */
  784. { 4000, 24, 28, 12, 24, 10, 4, JS1_MR1(4), JS1_MR2(4) },
  785. /* 3200.000Mbps */
  786. { 4800, 28, 32, 14, 30, 12, 6, JS1_MR1(5), JS1_MR2(5) },
  787. /* 3733.333Mbps */
  788. { 5600, 32, 36, 16, 34, 14, 6, JS1_MR1(6), JS1_MR2(6) },
  789. /* 4266.666Mbps */
  790. { 6400, 36, 40, 18, 40, 16, 8, JS1_MR1(7), JS1_MR2(7) }
  791. };
  792. struct _jedec_spec2 {
  793. uint16_t ps;
  794. uint16_t cyc;
  795. };
  796. #define js2_tsr 0
  797. #define js2_txp 1
  798. #define js2_trtp 2
  799. #define js2_trcd 3
  800. #define js2_trppb 4
  801. #define js2_trpab 5
  802. #define js2_tras 6
  803. #define js2_twr 7
  804. #define js2_twtr 8
  805. #define js2_trrd 9
  806. #define js2_tppd 10
  807. #define js2_tfaw 11
  808. #define js2_tdqsck 12
  809. #define js2_tckehcmd 13
  810. #define js2_tckelcmd 14
  811. #define js2_tckelpd 15
  812. #define js2_tmrr 16
  813. #define js2_tmrw 17
  814. #define js2_tmrd 18
  815. #define js2_tzqcalns 19
  816. #define js2_tzqlat 20
  817. #define js2_tiedly 21
  818. #define js2_tODTon_min 22
  819. #define JS2_TBLCNT 23
  820. #define js2_trcpb (JS2_TBLCNT)
  821. #define js2_trcab (JS2_TBLCNT + 1)
  822. #define js2_trfcab (JS2_TBLCNT + 2)
  823. #define JS2_CNT (JS2_TBLCNT + 3)
  824. #ifndef JS2_DERATE
  825. #define JS2_DERATE 0
  826. #endif
  827. const struct _jedec_spec2 jedec_spec2[2][JS2_TBLCNT] = {
  828. {
  829. /*tSR */ {15000, 3},
  830. /*tXP */ {7500, 3},
  831. /*tRTP */ {7500, 8},
  832. /*tRCD */ {18000, 4},
  833. /*tRPpb */ {18000, 3},
  834. /*tRPab */ {21000, 3},
  835. /*tRAS */ {42000, 3},
  836. /*tWR */ {18000, 4},
  837. /*tWTR */ {10000, 8},
  838. /*tRRD */ {10000, 4},
  839. /*tPPD */ {0, 0},
  840. /*tFAW */ {40000, 0},
  841. /*tDQSCK*/ {3500, 0},
  842. /*tCKEHCMD*/ {7500, 3},
  843. /*tCKELCMD*/ {7500, 3},
  844. /*tCKELPD*/ {7500, 3},
  845. /*tMRR*/ {0, 8},
  846. /*tMRW*/ {10000, 10},
  847. /*tMRD*/ {14000, 10},
  848. /*tZQCALns*/ {1000 * 10, 0},
  849. /*tZQLAT*/ {30000, 10},
  850. /*tIEdly*/ {12500, 0},
  851. /*tODTon_min*/ {1500, 0}
  852. }, {
  853. /*tSR */ {15000, 3},
  854. /*tXP */ {7500, 3},
  855. /*tRTP */ {7500, 8},
  856. /*tRCD */ {19875, 4},
  857. /*tRPpb */ {19875, 3},
  858. /*tRPab */ {22875, 3},
  859. /*tRAS */ {43875, 3},
  860. /*tWR */ {18000, 4},
  861. /*tWTR */ {10000, 8},
  862. /*tRRD */ {11875, 4},
  863. /*tPPD */ {0, 0},
  864. /*tFAW */ {40000, 0},
  865. /*tDQSCK*/ {3600, 0},
  866. /*tCKEHCMD*/ {7500, 3},
  867. /*tCKELCMD*/ {7500, 3},
  868. /*tCKELPD*/ {7500, 3},
  869. /*tMRR*/ {0, 8},
  870. /*tMRW*/ {10000, 10},
  871. /*tMRD*/ {14000, 10},
  872. /*tZQCALns*/ {1000 * 10, 0},
  873. /*tZQLAT*/ {30000, 10},
  874. /*tIEdly*/ {12500, 0},
  875. /*tODTon_min*/ {1500, 0}
  876. }
  877. };
  878. const uint16_t jedec_spec2_trfc_ab[7] = {
  879. /* 4Gb, 6Gb, 8Gb,12Gb, 16Gb, 24Gb(non), 32Gb(non) */
  880. 130, 180, 180, 280, 280, 560, 560
  881. };
  882. static uint32_t js1_ind;
  883. static uint16_t js2[JS2_CNT];
  884. static uint8_t RL;
  885. static uint8_t WL;
  886. static uint16_t _f_scale(uint32_t _ddr_mbps, uint32_t _ddr_mbpsdiv, uint32_t ps,
  887. uint16_t cyc)
  888. {
  889. uint32_t tmp;
  890. uint32_t div;
  891. tmp = (((uint32_t)(ps) + 9) / 10) * _ddr_mbps;
  892. div = tmp / (200000 * _ddr_mbpsdiv);
  893. if (tmp != (div * 200000 * _ddr_mbpsdiv))
  894. div = div + 1;
  895. if (div > cyc)
  896. return (uint16_t)div;
  897. return cyc;
  898. }
  899. static void _f_scale_js2(uint32_t _ddr_mbps, uint32_t _ddr_mbpsdiv,
  900. uint16_t *_js2)
  901. {
  902. int i;
  903. for (i = 0; i < JS2_TBLCNT; i++) {
  904. _js2[i] = _f_scale(_ddr_mbps, _ddr_mbpsdiv,
  905. 1UL * jedec_spec2[JS2_DERATE][i].ps,
  906. jedec_spec2[JS2_DERATE][i].cyc);
  907. }
  908. _js2[js2_trcpb] = _js2[js2_tras] + _js2[js2_trppb];
  909. _js2[js2_trcab] = _js2[js2_tras] + _js2[js2_trpab];
  910. }
  911. /* scaler for DELAY value */
  912. static int16_t _f_scale_adj(int16_t ps)
  913. {
  914. int32_t tmp;
  915. /*
  916. * tmp = (int32_t)512 * ps * ddr_mbps /2 / ddr_mbpsdiv / 1000 / 1000;
  917. * = ps * ddr_mbps /2 / ddr_mbpsdiv *512 / 8 / 8 / 125 / 125
  918. * = ps * ddr_mbps / ddr_mbpsdiv *4 / 125 / 125
  919. */
  920. tmp =
  921. (int32_t)4 * (int32_t)ps * (int32_t)ddr_mbps /
  922. (int32_t)ddr_mbpsdiv;
  923. tmp = (int32_t)tmp / (int32_t)15625;
  924. return (int16_t)tmp;
  925. }
  926. static const uint32_t reg_pi_mr1_data_fx_csx[2][CSAB_CNT] = {
  927. {
  928. _reg_PI_MR1_DATA_F0_0,
  929. _reg_PI_MR1_DATA_F0_1,
  930. _reg_PI_MR1_DATA_F0_2,
  931. _reg_PI_MR1_DATA_F0_3},
  932. {
  933. _reg_PI_MR1_DATA_F1_0,
  934. _reg_PI_MR1_DATA_F1_1,
  935. _reg_PI_MR1_DATA_F1_2,
  936. _reg_PI_MR1_DATA_F1_3}
  937. };
  938. static const uint32_t reg_pi_mr2_data_fx_csx[2][CSAB_CNT] = {
  939. {
  940. _reg_PI_MR2_DATA_F0_0,
  941. _reg_PI_MR2_DATA_F0_1,
  942. _reg_PI_MR2_DATA_F0_2,
  943. _reg_PI_MR2_DATA_F0_3},
  944. {
  945. _reg_PI_MR2_DATA_F1_0,
  946. _reg_PI_MR2_DATA_F1_1,
  947. _reg_PI_MR2_DATA_F1_2,
  948. _reg_PI_MR2_DATA_F1_3}
  949. };
  950. static const uint32_t reg_pi_mr3_data_fx_csx[2][CSAB_CNT] = {
  951. {
  952. _reg_PI_MR3_DATA_F0_0,
  953. _reg_PI_MR3_DATA_F0_1,
  954. _reg_PI_MR3_DATA_F0_2,
  955. _reg_PI_MR3_DATA_F0_3},
  956. {
  957. _reg_PI_MR3_DATA_F1_0,
  958. _reg_PI_MR3_DATA_F1_1,
  959. _reg_PI_MR3_DATA_F1_2,
  960. _reg_PI_MR3_DATA_F1_3}
  961. };
  962. const uint32_t reg_pi_mr11_data_fx_csx[2][CSAB_CNT] = {
  963. {
  964. _reg_PI_MR11_DATA_F0_0,
  965. _reg_PI_MR11_DATA_F0_1,
  966. _reg_PI_MR11_DATA_F0_2,
  967. _reg_PI_MR11_DATA_F0_3},
  968. {
  969. _reg_PI_MR11_DATA_F1_0,
  970. _reg_PI_MR11_DATA_F1_1,
  971. _reg_PI_MR11_DATA_F1_2,
  972. _reg_PI_MR11_DATA_F1_3}
  973. };
  974. const uint32_t reg_pi_mr12_data_fx_csx[2][CSAB_CNT] = {
  975. {
  976. _reg_PI_MR12_DATA_F0_0,
  977. _reg_PI_MR12_DATA_F0_1,
  978. _reg_PI_MR12_DATA_F0_2,
  979. _reg_PI_MR12_DATA_F0_3},
  980. {
  981. _reg_PI_MR12_DATA_F1_0,
  982. _reg_PI_MR12_DATA_F1_1,
  983. _reg_PI_MR12_DATA_F1_2,
  984. _reg_PI_MR12_DATA_F1_3}
  985. };
  986. const uint32_t reg_pi_mr14_data_fx_csx[2][CSAB_CNT] = {
  987. {
  988. _reg_PI_MR14_DATA_F0_0,
  989. _reg_PI_MR14_DATA_F0_1,
  990. _reg_PI_MR14_DATA_F0_2,
  991. _reg_PI_MR14_DATA_F0_3},
  992. {
  993. _reg_PI_MR14_DATA_F1_0,
  994. _reg_PI_MR14_DATA_F1_1,
  995. _reg_PI_MR14_DATA_F1_2,
  996. _reg_PI_MR14_DATA_F1_3}
  997. };
  998. /*
  999. * regif pll w/a ( REGIF H3 Ver.2.0 or later/M3-N/V3H WA )
  1000. */
  1001. static void regif_pll_wa(void)
  1002. {
  1003. uint32_t ch;
  1004. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  1005. // PLL setting for PHY : H3 Ver.1.x
  1006. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_WAIT),
  1007. (0x0064U <<
  1008. ddr_regdef_lsb(_reg_PHY_PLL_WAIT)));
  1009. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_CTRL),
  1010. ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET,
  1011. _reg_PHY_PLL_CTRL));
  1012. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_LP4_BOOT_PLL_CTRL),
  1013. ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET,
  1014. _reg_PHY_LP4_BOOT_PLL_CTRL));
  1015. } else {
  1016. /* PLL setting for PHY : M3-W/M3-N/V3H/H3 Ver.2.0 or later */
  1017. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_WAIT),
  1018. (0x5064U <<
  1019. ddr_regdef_lsb(_reg_PHY_PLL_WAIT)));
  1020. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_CTRL),
  1021. (ddrtbl_getval
  1022. (_cnf_DDR_PHY_ADR_G_REGSET,
  1023. _reg_PHY_PLL_CTRL_TOP) << 16) |
  1024. ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET,
  1025. _reg_PHY_PLL_CTRL));
  1026. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_CTRL_CA),
  1027. ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET,
  1028. _reg_PHY_PLL_CTRL_CA));
  1029. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_LP4_BOOT_PLL_CTRL),
  1030. (ddrtbl_getval
  1031. (_cnf_DDR_PHY_ADR_G_REGSET,
  1032. _reg_PHY_LP4_BOOT_PLL_CTRL_CA) << 16) |
  1033. ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET,
  1034. _reg_PHY_LP4_BOOT_PLL_CTRL));
  1035. reg_ddrphy_write_a(ddr_regdef_adr
  1036. (_reg_PHY_LP4_BOOT_TOP_PLL_CTRL),
  1037. ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET,
  1038. _reg_PHY_LP4_BOOT_TOP_PLL_CTRL
  1039. ));
  1040. if (ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET, _reg_PHY_LP4_BOOT_LOW_FREQ_SEL)) {
  1041. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_LP4_BOOT_LOW_FREQ_SEL),
  1042. _cnf_DDR_PHY_ADR_G_REGSET[0x7f & ddr_regdef_adr(
  1043. _reg_PHY_LP4_BOOT_LOW_FREQ_SEL)]);
  1044. }
  1045. }
  1046. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_LPDDR3_CS),
  1047. _cnf_DDR_PHY_ADR_G_REGSET
  1048. [ddr_regdef_adr(_reg_PHY_LPDDR3_CS) -
  1049. DDR_PHY_ADR_G_REGSET_OFS]);
  1050. /* protect register interface */
  1051. ddrphy_regif_idle();
  1052. pll3_control(0);
  1053. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  1054. /* non */
  1055. } else {
  1056. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_DLL_RST_EN),
  1057. (0x01U <<
  1058. ddr_regdef_lsb(_reg_PHY_DLL_RST_EN)));
  1059. ddrphy_regif_idle();
  1060. }
  1061. /* init start */
  1062. /* dbdficnt0:
  1063. * dfi_dram_clk_disable=1
  1064. * dfi_frequency = 0
  1065. * freq_ratio = 01 (2:1)
  1066. * init_start =0
  1067. */
  1068. foreach_vch(ch)
  1069. mmio_write_32(DBSC_DBDFICNT(ch), 0x00000F10);
  1070. dsb_sev();
  1071. /* dbdficnt0:
  1072. * dfi_dram_clk_disable=1
  1073. * dfi_frequency = 0
  1074. * freq_ratio = 01 (2:1)
  1075. * init_start =1
  1076. */
  1077. foreach_vch(ch)
  1078. mmio_write_32(DBSC_DBDFICNT(ch), 0x00000F11);
  1079. dsb_sev();
  1080. foreach_ech(ch)
  1081. if ((board_cnf->phyvalid) & BIT(ch))
  1082. while ((mmio_read_32(DBSC_PLL_LOCK(ch)) & 0x1f) != 0x1f)
  1083. ;
  1084. dsb_sev();
  1085. }
  1086. /* load table data into DDR registers */
  1087. static void ddrtbl_load(void)
  1088. {
  1089. uint32_t i;
  1090. uint32_t slice;
  1091. uint32_t csab;
  1092. uint32_t adr;
  1093. uint32_t data_l;
  1094. uint32_t tmp[3];
  1095. uint16_t dataS;
  1096. /* TIMING REGISTERS */
  1097. /* search jedec_spec1 index */
  1098. for (i = JS1_USABLEC_SPEC_LO; i < JS1_FREQ_TBL_NUM - 1; i++) {
  1099. if (js1[i].fx3 * 2U * ddr_mbpsdiv >= ddr_mbps * 3U)
  1100. break;
  1101. }
  1102. if (i > JS1_USABLEC_SPEC_HI)
  1103. js1_ind = JS1_USABLEC_SPEC_HI;
  1104. else
  1105. js1_ind = i;
  1106. if (board_cnf->dbi_en)
  1107. RL = js1[js1_ind].rlwdbi;
  1108. else
  1109. RL = js1[js1_ind].rlwodbi;
  1110. WL = js1[js1_ind].WL;
  1111. /* calculate jedec_spec2 */
  1112. _f_scale_js2(ddr_mbps, ddr_mbpsdiv, js2);
  1113. /* PREPARE TBL */
  1114. if (prr_product == PRR_PRODUCT_H3) {
  1115. if (prr_cut <= PRR_PRODUCT_11) {
  1116. /* H3 Ver.1.x */
  1117. _tblcopy(_cnf_DDR_PHY_SLICE_REGSET,
  1118. DDR_PHY_SLICE_REGSET_H3,
  1119. DDR_PHY_SLICE_REGSET_NUM_H3);
  1120. _tblcopy(_cnf_DDR_PHY_ADR_V_REGSET,
  1121. DDR_PHY_ADR_V_REGSET_H3,
  1122. DDR_PHY_ADR_V_REGSET_NUM_H3);
  1123. _tblcopy(_cnf_DDR_PHY_ADR_I_REGSET,
  1124. DDR_PHY_ADR_I_REGSET_H3,
  1125. DDR_PHY_ADR_I_REGSET_NUM_H3);
  1126. _tblcopy(_cnf_DDR_PHY_ADR_G_REGSET,
  1127. DDR_PHY_ADR_G_REGSET_H3,
  1128. DDR_PHY_ADR_G_REGSET_NUM_H3);
  1129. _tblcopy(_cnf_DDR_PI_REGSET, DDR_PI_REGSET_H3,
  1130. DDR_PI_REGSET_NUM_H3);
  1131. DDR_PHY_SLICE_REGSET_OFS = DDR_PHY_SLICE_REGSET_OFS_H3;
  1132. DDR_PHY_ADR_V_REGSET_OFS = DDR_PHY_ADR_V_REGSET_OFS_H3;
  1133. DDR_PHY_ADR_I_REGSET_OFS = DDR_PHY_ADR_I_REGSET_OFS_H3;
  1134. DDR_PHY_ADR_G_REGSET_OFS = DDR_PHY_ADR_G_REGSET_OFS_H3;
  1135. DDR_PI_REGSET_OFS = DDR_PI_REGSET_OFS_H3;
  1136. DDR_PHY_SLICE_REGSET_SIZE =
  1137. DDR_PHY_SLICE_REGSET_SIZE_H3;
  1138. DDR_PHY_ADR_V_REGSET_SIZE =
  1139. DDR_PHY_ADR_V_REGSET_SIZE_H3;
  1140. DDR_PHY_ADR_I_REGSET_SIZE =
  1141. DDR_PHY_ADR_I_REGSET_SIZE_H3;
  1142. DDR_PHY_ADR_G_REGSET_SIZE =
  1143. DDR_PHY_ADR_G_REGSET_SIZE_H3;
  1144. DDR_PI_REGSET_SIZE = DDR_PI_REGSET_SIZE_H3;
  1145. DDR_PHY_SLICE_REGSET_NUM = DDR_PHY_SLICE_REGSET_NUM_H3;
  1146. DDR_PHY_ADR_V_REGSET_NUM = DDR_PHY_ADR_V_REGSET_NUM_H3;
  1147. DDR_PHY_ADR_I_REGSET_NUM = DDR_PHY_ADR_I_REGSET_NUM_H3;
  1148. DDR_PHY_ADR_G_REGSET_NUM = DDR_PHY_ADR_G_REGSET_NUM_H3;
  1149. DDR_PI_REGSET_NUM = DDR_PI_REGSET_NUM_H3;
  1150. DDR_PHY_ADR_I_NUM = 1;
  1151. } else {
  1152. /* H3 Ver.2.0 or later */
  1153. _tblcopy(_cnf_DDR_PHY_SLICE_REGSET,
  1154. DDR_PHY_SLICE_REGSET_H3VER2,
  1155. DDR_PHY_SLICE_REGSET_NUM_H3VER2);
  1156. _tblcopy(_cnf_DDR_PHY_ADR_V_REGSET,
  1157. DDR_PHY_ADR_V_REGSET_H3VER2,
  1158. DDR_PHY_ADR_V_REGSET_NUM_H3VER2);
  1159. _tblcopy(_cnf_DDR_PHY_ADR_G_REGSET,
  1160. DDR_PHY_ADR_G_REGSET_H3VER2,
  1161. DDR_PHY_ADR_G_REGSET_NUM_H3VER2);
  1162. _tblcopy(_cnf_DDR_PI_REGSET, DDR_PI_REGSET_H3VER2,
  1163. DDR_PI_REGSET_NUM_H3VER2);
  1164. DDR_PHY_SLICE_REGSET_OFS =
  1165. DDR_PHY_SLICE_REGSET_OFS_H3VER2;
  1166. DDR_PHY_ADR_V_REGSET_OFS =
  1167. DDR_PHY_ADR_V_REGSET_OFS_H3VER2;
  1168. DDR_PHY_ADR_G_REGSET_OFS =
  1169. DDR_PHY_ADR_G_REGSET_OFS_H3VER2;
  1170. DDR_PI_REGSET_OFS = DDR_PI_REGSET_OFS_H3VER2;
  1171. DDR_PHY_SLICE_REGSET_SIZE =
  1172. DDR_PHY_SLICE_REGSET_SIZE_H3VER2;
  1173. DDR_PHY_ADR_V_REGSET_SIZE =
  1174. DDR_PHY_ADR_V_REGSET_SIZE_H3VER2;
  1175. DDR_PHY_ADR_G_REGSET_SIZE =
  1176. DDR_PHY_ADR_G_REGSET_SIZE_H3VER2;
  1177. DDR_PI_REGSET_SIZE = DDR_PI_REGSET_SIZE_H3VER2;
  1178. DDR_PHY_SLICE_REGSET_NUM =
  1179. DDR_PHY_SLICE_REGSET_NUM_H3VER2;
  1180. DDR_PHY_ADR_V_REGSET_NUM =
  1181. DDR_PHY_ADR_V_REGSET_NUM_H3VER2;
  1182. DDR_PHY_ADR_G_REGSET_NUM =
  1183. DDR_PHY_ADR_G_REGSET_NUM_H3VER2;
  1184. DDR_PI_REGSET_NUM = DDR_PI_REGSET_NUM_H3VER2;
  1185. DDR_PHY_ADR_I_NUM = 0;
  1186. }
  1187. } else if (prr_product == PRR_PRODUCT_M3) {
  1188. /* M3-W */
  1189. _tblcopy(_cnf_DDR_PHY_SLICE_REGSET,
  1190. DDR_PHY_SLICE_REGSET_M3, DDR_PHY_SLICE_REGSET_NUM_M3);
  1191. _tblcopy(_cnf_DDR_PHY_ADR_V_REGSET,
  1192. DDR_PHY_ADR_V_REGSET_M3, DDR_PHY_ADR_V_REGSET_NUM_M3);
  1193. _tblcopy(_cnf_DDR_PHY_ADR_I_REGSET,
  1194. DDR_PHY_ADR_I_REGSET_M3, DDR_PHY_ADR_I_REGSET_NUM_M3);
  1195. _tblcopy(_cnf_DDR_PHY_ADR_G_REGSET,
  1196. DDR_PHY_ADR_G_REGSET_M3, DDR_PHY_ADR_G_REGSET_NUM_M3);
  1197. _tblcopy(_cnf_DDR_PI_REGSET,
  1198. DDR_PI_REGSET_M3, DDR_PI_REGSET_NUM_M3);
  1199. DDR_PHY_SLICE_REGSET_OFS = DDR_PHY_SLICE_REGSET_OFS_M3;
  1200. DDR_PHY_ADR_V_REGSET_OFS = DDR_PHY_ADR_V_REGSET_OFS_M3;
  1201. DDR_PHY_ADR_I_REGSET_OFS = DDR_PHY_ADR_I_REGSET_OFS_M3;
  1202. DDR_PHY_ADR_G_REGSET_OFS = DDR_PHY_ADR_G_REGSET_OFS_M3;
  1203. DDR_PI_REGSET_OFS = DDR_PI_REGSET_OFS_M3;
  1204. DDR_PHY_SLICE_REGSET_SIZE = DDR_PHY_SLICE_REGSET_SIZE_M3;
  1205. DDR_PHY_ADR_V_REGSET_SIZE = DDR_PHY_ADR_V_REGSET_SIZE_M3;
  1206. DDR_PHY_ADR_I_REGSET_SIZE = DDR_PHY_ADR_I_REGSET_SIZE_M3;
  1207. DDR_PHY_ADR_G_REGSET_SIZE = DDR_PHY_ADR_G_REGSET_SIZE_M3;
  1208. DDR_PI_REGSET_SIZE = DDR_PI_REGSET_SIZE_M3;
  1209. DDR_PHY_SLICE_REGSET_NUM = DDR_PHY_SLICE_REGSET_NUM_M3;
  1210. DDR_PHY_ADR_V_REGSET_NUM = DDR_PHY_ADR_V_REGSET_NUM_M3;
  1211. DDR_PHY_ADR_I_REGSET_NUM = DDR_PHY_ADR_I_REGSET_NUM_M3;
  1212. DDR_PHY_ADR_G_REGSET_NUM = DDR_PHY_ADR_G_REGSET_NUM_M3;
  1213. DDR_PI_REGSET_NUM = DDR_PI_REGSET_NUM_M3;
  1214. DDR_PHY_ADR_I_NUM = 2;
  1215. } else {
  1216. /* M3-N/V3H */
  1217. _tblcopy(_cnf_DDR_PHY_SLICE_REGSET,
  1218. DDR_PHY_SLICE_REGSET_M3N,
  1219. DDR_PHY_SLICE_REGSET_NUM_M3N);
  1220. _tblcopy(_cnf_DDR_PHY_ADR_V_REGSET, DDR_PHY_ADR_V_REGSET_M3N,
  1221. DDR_PHY_ADR_V_REGSET_NUM_M3N);
  1222. _tblcopy(_cnf_DDR_PHY_ADR_I_REGSET, DDR_PHY_ADR_I_REGSET_M3N,
  1223. DDR_PHY_ADR_I_REGSET_NUM_M3N);
  1224. _tblcopy(_cnf_DDR_PHY_ADR_G_REGSET, DDR_PHY_ADR_G_REGSET_M3N,
  1225. DDR_PHY_ADR_G_REGSET_NUM_M3N);
  1226. _tblcopy(_cnf_DDR_PI_REGSET, DDR_PI_REGSET_M3N,
  1227. DDR_PI_REGSET_NUM_M3N);
  1228. DDR_PHY_SLICE_REGSET_OFS = DDR_PHY_SLICE_REGSET_OFS_M3N;
  1229. DDR_PHY_ADR_V_REGSET_OFS = DDR_PHY_ADR_V_REGSET_OFS_M3N;
  1230. DDR_PHY_ADR_I_REGSET_OFS = DDR_PHY_ADR_I_REGSET_OFS_M3N;
  1231. DDR_PHY_ADR_G_REGSET_OFS = DDR_PHY_ADR_G_REGSET_OFS_M3N;
  1232. DDR_PI_REGSET_OFS = DDR_PI_REGSET_OFS_M3N;
  1233. DDR_PHY_SLICE_REGSET_SIZE = DDR_PHY_SLICE_REGSET_SIZE_M3N;
  1234. DDR_PHY_ADR_V_REGSET_SIZE = DDR_PHY_ADR_V_REGSET_SIZE_M3N;
  1235. DDR_PHY_ADR_I_REGSET_SIZE = DDR_PHY_ADR_I_REGSET_SIZE_M3N;
  1236. DDR_PHY_ADR_G_REGSET_SIZE = DDR_PHY_ADR_G_REGSET_SIZE_M3N;
  1237. DDR_PI_REGSET_SIZE = DDR_PI_REGSET_SIZE_M3N;
  1238. DDR_PHY_SLICE_REGSET_NUM = DDR_PHY_SLICE_REGSET_NUM_M3N;
  1239. DDR_PHY_ADR_V_REGSET_NUM = DDR_PHY_ADR_V_REGSET_NUM_M3N;
  1240. DDR_PHY_ADR_I_REGSET_NUM = DDR_PHY_ADR_I_REGSET_NUM_M3N;
  1241. DDR_PHY_ADR_G_REGSET_NUM = DDR_PHY_ADR_G_REGSET_NUM_M3N;
  1242. DDR_PI_REGSET_NUM = DDR_PI_REGSET_NUM_M3N;
  1243. DDR_PHY_ADR_I_NUM = 2;
  1244. }
  1245. /* PLL CODE CHANGE */
  1246. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut == PRR_PRODUCT_11)) {
  1247. ddrtbl_setval(_cnf_DDR_PHY_ADR_G_REGSET, _reg_PHY_PLL_CTRL,
  1248. 0x1142);
  1249. ddrtbl_setval(_cnf_DDR_PHY_ADR_G_REGSET,
  1250. _reg_PHY_LP4_BOOT_PLL_CTRL, 0x1142);
  1251. }
  1252. /* on fly gate adjust */
  1253. if ((prr_product == PRR_PRODUCT_M3) && (prr_cut == PRR_PRODUCT_10)) {
  1254. ddrtbl_setval(_cnf_DDR_PHY_SLICE_REGSET,
  1255. _reg_ON_FLY_GATE_ADJUST_EN, 0x00);
  1256. }
  1257. /* Adjust PI parameters */
  1258. #ifdef _def_LPDDR4_ODT
  1259. for (i = 0; i < 2; i++) {
  1260. for (csab = 0; csab < CSAB_CNT; csab++) {
  1261. ddrtbl_setval(_cnf_DDR_PI_REGSET,
  1262. reg_pi_mr11_data_fx_csx[i][csab],
  1263. _def_LPDDR4_ODT);
  1264. }
  1265. }
  1266. #endif /* _def_LPDDR4_ODT */
  1267. #ifdef _def_LPDDR4_VREFCA
  1268. for (i = 0; i < 2; i++) {
  1269. for (csab = 0; csab < CSAB_CNT; csab++) {
  1270. ddrtbl_setval(_cnf_DDR_PI_REGSET,
  1271. reg_pi_mr12_data_fx_csx[i][csab],
  1272. _def_LPDDR4_VREFCA);
  1273. }
  1274. }
  1275. #endif /* _def_LPDDR4_VREFCA */
  1276. if ((prr_product == PRR_PRODUCT_M3N) ||
  1277. (prr_product == PRR_PRODUCT_V3H)) {
  1278. js2[js2_tiedly] = _f_scale(ddr_mbps, ddr_mbpsdiv, 7000, 0) + 7U;
  1279. if (js2[js2_tiedly] > (RL))
  1280. js2[js2_tiedly] = RL;
  1281. } else if ((prr_product == PRR_PRODUCT_H3) &&
  1282. (prr_cut > PRR_PRODUCT_11)) {
  1283. js2[js2_tiedly] = _f_scale(ddr_mbps, ddr_mbpsdiv, 9000, 0) + 4U;
  1284. } else if ((prr_product == PRR_PRODUCT_H3) &&
  1285. (prr_cut <= PRR_PRODUCT_11)) {
  1286. js2[js2_tiedly] = _f_scale(ddr_mbps, ddr_mbpsdiv, 10000, 0);
  1287. }
  1288. if (((prr_product == PRR_PRODUCT_H3) && (prr_cut > PRR_PRODUCT_11)) ||
  1289. (prr_product == PRR_PRODUCT_M3N) ||
  1290. (prr_product == PRR_PRODUCT_V3H)) {
  1291. if ((js2[js2_tiedly]) >= 0x1e)
  1292. dataS = 0x1e;
  1293. else
  1294. dataS = js2[js2_tiedly];
  1295. } else {
  1296. if ((js2[js2_tiedly]) >= 0x0e)
  1297. dataS = 0x0e;
  1298. else
  1299. dataS = js2[js2_tiedly];
  1300. }
  1301. ddrtbl_setval(_cnf_DDR_PHY_SLICE_REGSET, _reg_PHY_RDDATA_EN_DLY, dataS);
  1302. ddrtbl_setval(_cnf_DDR_PHY_SLICE_REGSET, _reg_PHY_RDDATA_EN_TSEL_DLY,
  1303. (dataS - 2));
  1304. if ((prr_product == PRR_PRODUCT_M3N) ||
  1305. (prr_product == PRR_PRODUCT_V3H)) {
  1306. ddrtbl_setval(_cnf_DDR_PHY_SLICE_REGSET,
  1307. _reg_PHY_RDDATA_EN_OE_DLY, dataS - 2);
  1308. }
  1309. ddrtbl_setval(_cnf_DDR_PI_REGSET, _reg_PI_RDLAT_ADJ_F1, RL - dataS);
  1310. if (ddrtbl_getval
  1311. (_cnf_DDR_PHY_SLICE_REGSET, _reg_PHY_WRITE_PATH_LAT_ADD)) {
  1312. data_l = WL - 1;
  1313. } else {
  1314. data_l = WL;
  1315. }
  1316. ddrtbl_setval(_cnf_DDR_PI_REGSET, _reg_PI_WRLAT_ADJ_F1, data_l - 2);
  1317. ddrtbl_setval(_cnf_DDR_PI_REGSET, _reg_PI_WRLAT_F1, data_l);
  1318. if (board_cnf->dbi_en) {
  1319. ddrtbl_setval(_cnf_DDR_PHY_SLICE_REGSET, _reg_PHY_DBI_MODE,
  1320. 0x01);
  1321. ddrtbl_setval(_cnf_DDR_PHY_SLICE_REGSET,
  1322. _reg_PHY_WDQLVL_DATADM_MASK, 0x000);
  1323. } else {
  1324. ddrtbl_setval(_cnf_DDR_PHY_SLICE_REGSET, _reg_PHY_DBI_MODE,
  1325. 0x00);
  1326. ddrtbl_setval(_cnf_DDR_PHY_SLICE_REGSET,
  1327. _reg_PHY_WDQLVL_DATADM_MASK, 0x100);
  1328. }
  1329. tmp[0] = js1[js1_ind].MR1;
  1330. tmp[1] = js1[js1_ind].MR2;
  1331. data_l = ddrtbl_getval(_cnf_DDR_PI_REGSET, _reg_PI_MR3_DATA_F1_0);
  1332. if (board_cnf->dbi_en)
  1333. tmp[2] = data_l | 0xc0;
  1334. else
  1335. tmp[2] = data_l & (~0xc0);
  1336. for (i = 0; i < 2; i++) {
  1337. for (csab = 0; csab < CSAB_CNT; csab++) {
  1338. ddrtbl_setval(_cnf_DDR_PI_REGSET,
  1339. reg_pi_mr1_data_fx_csx[i][csab], tmp[0]);
  1340. ddrtbl_setval(_cnf_DDR_PI_REGSET,
  1341. reg_pi_mr2_data_fx_csx[i][csab], tmp[1]);
  1342. ddrtbl_setval(_cnf_DDR_PI_REGSET,
  1343. reg_pi_mr3_data_fx_csx[i][csab], tmp[2]);
  1344. }
  1345. }
  1346. /* DDRPHY INT START */
  1347. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  1348. /* non */
  1349. } else {
  1350. regif_pll_wa();
  1351. dbwait_loop(5);
  1352. }
  1353. /* FREQ_SEL_MULTICAST & PER_CS_TRAINING_MULTICAST SET (for safety) */
  1354. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_FREQ_SEL_MULTICAST_EN),
  1355. BIT(ddr_regdef_lsb(_reg_PHY_FREQ_SEL_MULTICAST_EN)));
  1356. ddr_setval_ach_as(_reg_PHY_PER_CS_TRAINING_MULTICAST_EN, 0x01);
  1357. /* SET DATA SLICE TABLE */
  1358. for (slice = 0; slice < SLICE_CNT; slice++) {
  1359. adr =
  1360. DDR_PHY_SLICE_REGSET_OFS +
  1361. DDR_PHY_SLICE_REGSET_SIZE * slice;
  1362. for (i = 0; i < DDR_PHY_SLICE_REGSET_NUM; i++) {
  1363. reg_ddrphy_write_a(adr + i,
  1364. _cnf_DDR_PHY_SLICE_REGSET[i]);
  1365. }
  1366. }
  1367. /* SET ADR SLICE TABLE */
  1368. adr = DDR_PHY_ADR_V_REGSET_OFS;
  1369. for (i = 0; i < DDR_PHY_ADR_V_REGSET_NUM; i++) {
  1370. reg_ddrphy_write_a(adr + i, _cnf_DDR_PHY_ADR_V_REGSET[i]);
  1371. }
  1372. if (((prr_product == PRR_PRODUCT_M3) ||
  1373. (prr_product == PRR_PRODUCT_M3N)) &&
  1374. ((0x00ffffff & (uint32_t)((board_cnf->ch[0].ca_swap) >> 40))
  1375. != 0x00)) {
  1376. adr = DDR_PHY_ADR_I_REGSET_OFS + DDR_PHY_ADR_I_REGSET_SIZE;
  1377. for (i = 0; i < DDR_PHY_ADR_V_REGSET_NUM; i++) {
  1378. reg_ddrphy_write_a(adr + i,
  1379. _cnf_DDR_PHY_ADR_V_REGSET[i]);
  1380. }
  1381. ddrtbl_setval(_cnf_DDR_PHY_ADR_G_REGSET,
  1382. _reg_PHY_ADR_DISABLE, 0x02);
  1383. DDR_PHY_ADR_I_NUM -= 1;
  1384. ddr_phycaslice = 1;
  1385. #ifndef _def_LPDDR4_ODT
  1386. for (i = 0; i < 2; i++) {
  1387. for (csab = 0; csab < CSAB_CNT; csab++) {
  1388. ddrtbl_setval(_cnf_DDR_PI_REGSET,
  1389. reg_pi_mr11_data_fx_csx[i][csab],
  1390. 0x66);
  1391. }
  1392. }
  1393. #endif/* _def_LPDDR4_ODT */
  1394. } else {
  1395. ddr_phycaslice = 0;
  1396. }
  1397. if (DDR_PHY_ADR_I_NUM > 0) {
  1398. for (slice = 0; slice < DDR_PHY_ADR_I_NUM; slice++) {
  1399. adr =
  1400. DDR_PHY_ADR_I_REGSET_OFS +
  1401. DDR_PHY_ADR_I_REGSET_SIZE * slice;
  1402. for (i = 0; i < DDR_PHY_ADR_I_REGSET_NUM; i++) {
  1403. reg_ddrphy_write_a(adr + i,
  1404. _cnf_DDR_PHY_ADR_I_REGSET
  1405. [i]);
  1406. }
  1407. }
  1408. }
  1409. /* SET ADRCTRL SLICE TABLE */
  1410. adr = DDR_PHY_ADR_G_REGSET_OFS;
  1411. for (i = 0; i < DDR_PHY_ADR_G_REGSET_NUM; i++) {
  1412. reg_ddrphy_write_a(adr + i, _cnf_DDR_PHY_ADR_G_REGSET[i]);
  1413. }
  1414. /* SET PI REGISTERS */
  1415. adr = DDR_PI_REGSET_OFS;
  1416. for (i = 0; i < DDR_PI_REGSET_NUM; i++) {
  1417. reg_ddrphy_write_a(adr + i, _cnf_DDR_PI_REGSET[i]);
  1418. }
  1419. }
  1420. /* CONFIGURE DDR REGISTERS */
  1421. static void ddr_config_sub(void)
  1422. {
  1423. uint32_t i;
  1424. uint32_t ch, slice;
  1425. uint32_t data_l;
  1426. uint32_t tmp;
  1427. uint8_t high_byte[SLICE_CNT];
  1428. const uint32_t _par_CALVL_DEVICE_MAP = 1;
  1429. foreach_vch(ch) {
  1430. /* BOARD SETTINGS (DQ,DM,VREF_DRIVING) */
  1431. for (slice = 0; slice < SLICE_CNT; slice++) {
  1432. high_byte[slice] =
  1433. (board_cnf->ch[ch].dqs_swap >> (4 * slice)) % 2;
  1434. ddr_setval_s(ch, slice, _reg_PHY_DQ_DM_SWIZZLE0,
  1435. board_cnf->ch[ch].dq_swap[slice]);
  1436. ddr_setval_s(ch, slice, _reg_PHY_DQ_DM_SWIZZLE1,
  1437. board_cnf->ch[ch].dm_swap[slice]);
  1438. if (high_byte[slice]) {
  1439. /* HIGHER 16 BYTE */
  1440. ddr_setval_s(ch, slice,
  1441. _reg_PHY_CALVL_VREF_DRIVING_SLICE,
  1442. 0x00);
  1443. } else {
  1444. /* LOWER 16 BYTE */
  1445. ddr_setval_s(ch, slice,
  1446. _reg_PHY_CALVL_VREF_DRIVING_SLICE,
  1447. 0x01);
  1448. }
  1449. }
  1450. /* BOARD SETTINGS (CA,ADDR_SEL) */
  1451. data_l = (0x00ffffff & (uint32_t)(board_cnf->ch[ch].ca_swap)) |
  1452. 0x00888888;
  1453. /* --- ADR_CALVL_SWIZZLE --- */
  1454. if (prr_product == PRR_PRODUCT_M3) {
  1455. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE0_0, data_l);
  1456. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE1_0,
  1457. 0x00000000);
  1458. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE0_1, data_l);
  1459. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE1_1,
  1460. 0x00000000);
  1461. ddr_setval(ch, _reg_PHY_ADR_CALVL_DEVICE_MAP,
  1462. _par_CALVL_DEVICE_MAP);
  1463. } else {
  1464. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE0, data_l);
  1465. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE1, 0x00000000);
  1466. ddr_setval(ch, _reg_PHY_CALVL_DEVICE_MAP,
  1467. _par_CALVL_DEVICE_MAP);
  1468. }
  1469. /* --- ADR_ADDR_SEL --- */
  1470. if ((prr_product == PRR_PRODUCT_H3) &&
  1471. (prr_cut > PRR_PRODUCT_11)) {
  1472. data_l = 0x00FFFFFF & board_cnf->ch[ch].ca_swap;
  1473. } else {
  1474. data_l = 0;
  1475. tmp = board_cnf->ch[ch].ca_swap;
  1476. for (i = 0; i < 6; i++) {
  1477. data_l |= ((tmp & 0x0f) << (i * 5));
  1478. tmp = tmp >> 4;
  1479. }
  1480. }
  1481. ddr_setval(ch, _reg_PHY_ADR_ADDR_SEL, data_l);
  1482. if (ddr_phycaslice == 1) {
  1483. /* ----------- adr slice2 swap ----------- */
  1484. tmp = (uint32_t)((board_cnf->ch[ch].ca_swap) >> 40);
  1485. data_l = (tmp & 0x00ffffff) | 0x00888888;
  1486. /* --- ADR_CALVL_SWIZZLE --- */
  1487. if (prr_product == PRR_PRODUCT_M3) {
  1488. ddr_setval_s(ch, 2,
  1489. _reg_PHY_ADR_CALVL_SWIZZLE0_0,
  1490. data_l);
  1491. ddr_setval_s(ch, 2,
  1492. _reg_PHY_ADR_CALVL_SWIZZLE1_0,
  1493. 0x00000000);
  1494. ddr_setval_s(ch, 2,
  1495. _reg_PHY_ADR_CALVL_SWIZZLE0_1,
  1496. data_l);
  1497. ddr_setval_s(ch, 2,
  1498. _reg_PHY_ADR_CALVL_SWIZZLE1_1,
  1499. 0x00000000);
  1500. ddr_setval_s(ch, 2,
  1501. _reg_PHY_ADR_CALVL_DEVICE_MAP,
  1502. _par_CALVL_DEVICE_MAP);
  1503. } else {
  1504. ddr_setval_s(ch, 2,
  1505. _reg_PHY_ADR_CALVL_SWIZZLE0,
  1506. data_l);
  1507. ddr_setval_s(ch, 2,
  1508. _reg_PHY_ADR_CALVL_SWIZZLE1,
  1509. 0x00000000);
  1510. ddr_setval_s(ch, 2,
  1511. _reg_PHY_CALVL_DEVICE_MAP,
  1512. _par_CALVL_DEVICE_MAP);
  1513. }
  1514. /* --- ADR_ADDR_SEL --- */
  1515. data_l = 0;
  1516. for (i = 0; i < 6; i++) {
  1517. data_l |= ((tmp & 0x0f) << (i * 5));
  1518. tmp = tmp >> 4;
  1519. }
  1520. ddr_setval_s(ch, 2, _reg_PHY_ADR_ADDR_SEL, data_l);
  1521. }
  1522. /* BOARD SETTINGS (BYTE_ORDER_SEL) */
  1523. if (prr_product == PRR_PRODUCT_M3) {
  1524. /* --- DATA_BYTE_SWAP --- */
  1525. data_l = 0;
  1526. tmp = board_cnf->ch[ch].dqs_swap;
  1527. for (i = 0; i < 4; i++) {
  1528. data_l |= ((tmp & 0x03) << (i * 2));
  1529. tmp = tmp >> 4;
  1530. }
  1531. } else {
  1532. /* --- DATA_BYTE_SWAP --- */
  1533. data_l = board_cnf->ch[ch].dqs_swap;
  1534. ddr_setval(ch, _reg_PI_DATA_BYTE_SWAP_EN, 0x01);
  1535. ddr_setval(ch, _reg_PI_DATA_BYTE_SWAP_SLICE0,
  1536. (data_l) & 0x0f);
  1537. ddr_setval(ch, _reg_PI_DATA_BYTE_SWAP_SLICE1,
  1538. (data_l >> 4 * 1) & 0x0f);
  1539. ddr_setval(ch, _reg_PI_DATA_BYTE_SWAP_SLICE2,
  1540. (data_l >> 4 * 2) & 0x0f);
  1541. ddr_setval(ch, _reg_PI_DATA_BYTE_SWAP_SLICE3,
  1542. (data_l >> 4 * 3) & 0x0f);
  1543. ddr_setval(ch, _reg_PHY_DATA_BYTE_ORDER_SEL_HIGH, 0x00);
  1544. }
  1545. ddr_setval(ch, _reg_PHY_DATA_BYTE_ORDER_SEL, data_l);
  1546. }
  1547. }
  1548. static void get_ca_swizzle(uint32_t ch, uint32_t ddr_csn, uint32_t *p_swz)
  1549. {
  1550. uint32_t slice;
  1551. uint32_t tmp;
  1552. uint32_t tgt;
  1553. if (ddr_csn / 2) {
  1554. tgt = 3;
  1555. } else {
  1556. tgt = 1;
  1557. }
  1558. for (slice = 0; slice < SLICE_CNT; slice++) {
  1559. tmp = (board_cnf->ch[ch].dqs_swap >> (4 * slice)) & 0x0f;
  1560. if (tgt == tmp)
  1561. break;
  1562. }
  1563. tmp = 0x00FFFFFF & board_cnf->ch[ch].ca_swap;
  1564. if (slice % 2)
  1565. tmp |= 0x00888888;
  1566. *p_swz = tmp;
  1567. }
  1568. static void ddr_config_sub_h3v1x(void)
  1569. {
  1570. uint32_t ch, slice;
  1571. uint32_t data_l;
  1572. uint32_t tmp;
  1573. uint8_t high_byte[SLICE_CNT];
  1574. uint32_t ca_swizzle;
  1575. uint32_t ca;
  1576. uint32_t csmap;
  1577. uint32_t o_inv;
  1578. uint32_t inv;
  1579. uint32_t bit_soc;
  1580. uint32_t bit_mem;
  1581. uint32_t j;
  1582. const uint8_t o_mr15 = 0x55;
  1583. const uint8_t o_mr20 = 0x55;
  1584. const uint16_t o_mr32_mr40 = 0x5a3c;
  1585. foreach_vch(ch) {
  1586. /* BOARD SETTINGS (DQ,DM,VREF_DRIVING) */
  1587. csmap = 0;
  1588. for (slice = 0; slice < SLICE_CNT; slice++) {
  1589. tmp = (board_cnf->ch[ch].dqs_swap >> (4 * slice)) &
  1590. 0x0f;
  1591. high_byte[slice] = tmp % 2;
  1592. if (tmp == 1 && (slice >= 2))
  1593. csmap |= 0x05;
  1594. if (tmp == 3 && (slice >= 2))
  1595. csmap |= 0x50;
  1596. ddr_setval_s(ch, slice, _reg_PHY_DQ_SWIZZLING,
  1597. board_cnf->ch[ch].dq_swap[slice]);
  1598. if (high_byte[slice]) {
  1599. /* HIGHER 16 BYTE */
  1600. ddr_setval_s(ch, slice,
  1601. _reg_PHY_CALVL_VREF_DRIVING_SLICE,
  1602. 0x00);
  1603. } else {
  1604. /* LOWER 16 BYTE */
  1605. ddr_setval_s(ch, slice,
  1606. _reg_PHY_CALVL_VREF_DRIVING_SLICE,
  1607. 0x01);
  1608. }
  1609. }
  1610. /* BOARD SETTINGS (CA,ADDR_SEL) */
  1611. ca = 0x00FFFFFF & board_cnf->ch[ch].ca_swap;
  1612. ddr_setval(ch, _reg_PHY_ADR_ADDR_SEL, ca);
  1613. ddr_setval(ch, _reg_PHY_CALVL_CS_MAP, csmap);
  1614. get_ca_swizzle(ch, 0, &ca_swizzle);
  1615. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE0_0, ca_swizzle);
  1616. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE1_0, 0x00000000);
  1617. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE0_1, 0x00000000);
  1618. ddr_setval(ch, _reg_PHY_ADR_CALVL_SWIZZLE1_1, 0x00000000);
  1619. ddr_setval(ch, _reg_PHY_ADR_CALVL_DEVICE_MAP, 0x01);
  1620. for (slice = 0; slice < SLICE_CNT; slice++) {
  1621. ddr_setval_s(ch, slice, _reg_PI_RDLVL_PATTERN_NUM,
  1622. 0x01);
  1623. ddr_setval_s(ch, slice, _reg_PI_RDLVL_PATTERN_START,
  1624. 0x08);
  1625. if (high_byte[slice])
  1626. o_inv = o_mr20;
  1627. else
  1628. o_inv = o_mr15;
  1629. tmp = board_cnf->ch[ch].dq_swap[slice];
  1630. inv = 0;
  1631. j = 0;
  1632. for (bit_soc = 0; bit_soc < 8; bit_soc++) {
  1633. bit_mem = (tmp >> (4 * bit_soc)) & 0x0f;
  1634. j |= (1U << bit_mem);
  1635. if (o_inv & (1U << bit_mem))
  1636. inv |= (1U << bit_soc);
  1637. }
  1638. data_l = o_mr32_mr40;
  1639. if (!high_byte[slice])
  1640. data_l |= (inv << 24);
  1641. if (high_byte[slice])
  1642. data_l |= (inv << 16);
  1643. ddr_setval_s(ch, slice, _reg_PHY_LP4_RDLVL_PATT8,
  1644. data_l);
  1645. }
  1646. }
  1647. }
  1648. static void ddr_config(void)
  1649. {
  1650. int32_t i;
  1651. uint32_t ch, slice;
  1652. uint32_t data_l;
  1653. uint32_t tmp;
  1654. int8_t _adj;
  1655. int16_t adj;
  1656. uint32_t dq;
  1657. union {
  1658. uint32_t ui32[4];
  1659. uint8_t ui8[16];
  1660. } patt;
  1661. uint16_t patm;
  1662. /* configure ddrphy registers */
  1663. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  1664. ddr_config_sub_h3v1x();
  1665. } else { /* H3 Ver.2.0 or later/M3-N/V3H is same as M3-W */
  1666. ddr_config_sub();
  1667. }
  1668. /* WDQ_USER_PATT */
  1669. foreach_vch(ch) {
  1670. for (slice = 0; slice < SLICE_CNT; slice++) {
  1671. patm = 0;
  1672. for (i = 0; i < 16; i++) {
  1673. tmp = board_cnf->ch[ch].wdqlvl_patt[i];
  1674. patt.ui8[i] = tmp & 0xff;
  1675. if (tmp & 0x100)
  1676. patm |= (1U << i);
  1677. }
  1678. ddr_setval_s(ch, slice, _reg_PHY_USER_PATT0,
  1679. patt.ui32[0]);
  1680. ddr_setval_s(ch, slice, _reg_PHY_USER_PATT1,
  1681. patt.ui32[1]);
  1682. ddr_setval_s(ch, slice, _reg_PHY_USER_PATT2,
  1683. patt.ui32[2]);
  1684. ddr_setval_s(ch, slice, _reg_PHY_USER_PATT3,
  1685. patt.ui32[3]);
  1686. ddr_setval_s(ch, slice, _reg_PHY_USER_PATT4, patm);
  1687. }
  1688. }
  1689. /* CACS DLY */
  1690. data_l = board_cnf->cacs_dly + _f_scale_adj(board_cnf->cacs_dly_adj);
  1691. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_FREQ_SEL_MULTICAST_EN),
  1692. 0x00U);
  1693. foreach_vch(ch) {
  1694. for (i = 0; i < _reg_PHY_CLK_CACS_SLAVE_DELAY_X_NUM - 4; i++) {
  1695. adj = _f_scale_adj(board_cnf->ch[ch].cacs_adj[i]);
  1696. ddrtbl_setval(_cnf_DDR_PHY_ADR_V_REGSET,
  1697. _reg_PHY_CLK_CACS_SLAVE_DELAY_X[i],
  1698. data_l + adj);
  1699. reg_ddrphy_write(ch,
  1700. ddr_regdef_adr
  1701. (_reg_PHY_CLK_CACS_SLAVE_DELAY_X[i]),
  1702. _cnf_DDR_PHY_ADR_V_REGSET
  1703. [ddr_regdef_adr
  1704. (_reg_PHY_CLK_CACS_SLAVE_DELAY_X[i]) -
  1705. DDR_PHY_ADR_V_REGSET_OFS]);
  1706. }
  1707. for (i = (_reg_PHY_CLK_CACS_SLAVE_DELAY_X_NUM - 4);
  1708. i < _reg_PHY_CLK_CACS_SLAVE_DELAY_X_NUM; i++) {
  1709. adj = _f_scale_adj(board_cnf->ch[ch].cacs_adj[i]);
  1710. ddrtbl_setval(_cnf_DDR_PHY_ADR_G_REGSET,
  1711. _reg_PHY_CLK_CACS_SLAVE_DELAY_X[i],
  1712. data_l + adj);
  1713. reg_ddrphy_write(ch,
  1714. ddr_regdef_adr
  1715. (_reg_PHY_CLK_CACS_SLAVE_DELAY_X[i]),
  1716. _cnf_DDR_PHY_ADR_G_REGSET
  1717. [ddr_regdef_adr
  1718. (_reg_PHY_CLK_CACS_SLAVE_DELAY_X[i]) -
  1719. DDR_PHY_ADR_G_REGSET_OFS]);
  1720. }
  1721. if (ddr_phycaslice == 1) {
  1722. for (i = 0; i < 6; i++) {
  1723. adj = _f_scale_adj
  1724. (board_cnf->ch[ch].cacs_adj
  1725. [i +
  1726. _reg_PHY_CLK_CACS_SLAVE_DELAY_X_NUM]);
  1727. ddrtbl_setval(_cnf_DDR_PHY_ADR_V_REGSET,
  1728. _reg_PHY_CLK_CACS_SLAVE_DELAY_X
  1729. [i],
  1730. data_l + adj);
  1731. reg_ddrphy_write(ch,
  1732. ddr_regdef_adr
  1733. (_reg_PHY_CLK_CACS_SLAVE_DELAY_X[i]) +
  1734. 0x0100,
  1735. _cnf_DDR_PHY_ADR_V_REGSET
  1736. [ddr_regdef_adr
  1737. (_reg_PHY_CLK_CACS_SLAVE_DELAY_X[i]) -
  1738. DDR_PHY_ADR_V_REGSET_OFS]);
  1739. }
  1740. }
  1741. }
  1742. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_FREQ_SEL_MULTICAST_EN),
  1743. BIT(ddr_regdef_lsb(_reg_PHY_FREQ_SEL_MULTICAST_EN)));
  1744. /* WDQDM DLY */
  1745. data_l = board_cnf->dqdm_dly_w;
  1746. foreach_vch(ch) {
  1747. for (slice = 0; slice < SLICE_CNT; slice++) {
  1748. for (i = 0; i <= 8; i++) {
  1749. dq = slice * 8 + i;
  1750. if (i == 8)
  1751. _adj = board_cnf->ch[ch].dm_adj_w[slice];
  1752. else
  1753. _adj = board_cnf->ch[ch].dq_adj_w[dq];
  1754. adj = _f_scale_adj(_adj);
  1755. ddr_setval_s(ch, slice,
  1756. _reg_PHY_CLK_WRX_SLAVE_DELAY[i],
  1757. data_l + adj);
  1758. }
  1759. }
  1760. }
  1761. /* RDQDM DLY */
  1762. data_l = board_cnf->dqdm_dly_r;
  1763. foreach_vch(ch) {
  1764. for (slice = 0; slice < SLICE_CNT; slice++) {
  1765. for (i = 0; i <= 8; i++) {
  1766. dq = slice * 8 + i;
  1767. if (i == 8)
  1768. _adj = board_cnf->ch[ch].dm_adj_r[slice];
  1769. else
  1770. _adj = board_cnf->ch[ch].dq_adj_r[dq];
  1771. adj = _f_scale_adj(_adj);
  1772. ddr_setval_s(ch, slice,
  1773. _reg_PHY_RDDQS_X_FALL_SLAVE_DELAY
  1774. [i], data_l + adj);
  1775. ddr_setval_s(ch, slice,
  1776. _reg_PHY_RDDQS_X_RISE_SLAVE_DELAY
  1777. [i], data_l + adj);
  1778. }
  1779. }
  1780. }
  1781. }
  1782. /* DBSC register setting functions */
  1783. static void dbsc_regset_pre(void)
  1784. {
  1785. uint32_t ch, csab;
  1786. uint32_t data_l;
  1787. /* PRIMARY SETTINGS */
  1788. /* LPDDR4, BL=16, DFI interface */
  1789. mmio_write_32(DBSC_DBKIND, 0x0000000a);
  1790. mmio_write_32(DBSC_DBBL, 0x00000002);
  1791. mmio_write_32(DBSC_DBPHYCONF0, 0x00000001);
  1792. /* FREQRATIO=2 */
  1793. mmio_write_32(DBSC_DBSYSCONF1, 0x00000002);
  1794. /* Chanel map (H3 Ver.1.x) */
  1795. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11))
  1796. mmio_write_32(DBSC_DBSCHCNT1, 0x00001010);
  1797. /* DRAM SIZE REGISTER:
  1798. * set all ranks as density=0(4Gb) for PHY initialization
  1799. */
  1800. foreach_vch(ch) {
  1801. for (csab = 0; csab < 4; csab++) {
  1802. mmio_write_32(DBSC_DBMEMCONF(ch, csab),
  1803. DBMEMCONF_REGD(0));
  1804. }
  1805. }
  1806. if (prr_product == PRR_PRODUCT_M3) {
  1807. data_l = 0xe4e4e4e4;
  1808. foreach_ech(ch) {
  1809. if ((ddr_phyvalid & (1U << ch)))
  1810. data_l = (data_l & (~(0x000000FF << (ch * 8))))
  1811. | (((board_cnf->ch[ch].dqs_swap & 0x0003)
  1812. | ((board_cnf->ch[ch].dqs_swap & 0x0030)
  1813. >> 2)
  1814. | ((board_cnf->ch[ch].dqs_swap & 0x0300)
  1815. >> 4)
  1816. | ((board_cnf->ch[ch].dqs_swap & 0x3000)
  1817. >> 6)) << (ch * 8));
  1818. }
  1819. mmio_write_32(DBSC_DBBSWAP, data_l);
  1820. }
  1821. }
  1822. static void dbsc_regset(void)
  1823. {
  1824. int32_t i;
  1825. uint32_t ch;
  1826. uint32_t data_l;
  1827. uint32_t data_l2;
  1828. uint32_t tmp[4];
  1829. /* RFC */
  1830. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut == PRR_PRODUCT_20) &&
  1831. (max_density == 0)) {
  1832. js2[js2_trfcab] =
  1833. _f_scale(ddr_mbps, ddr_mbpsdiv,
  1834. 1UL * jedec_spec2_trfc_ab[1] * 1000, 0);
  1835. } else {
  1836. js2[js2_trfcab] =
  1837. _f_scale(ddr_mbps, ddr_mbpsdiv,
  1838. 1UL * jedec_spec2_trfc_ab[max_density] *
  1839. 1000, 0);
  1840. }
  1841. /* DBTR0.CL : RL */
  1842. mmio_write_32(DBSC_DBTR(0), RL);
  1843. /* DBTR1.CWL : WL */
  1844. mmio_write_32(DBSC_DBTR(1), WL);
  1845. /* DBTR2.AL : 0 */
  1846. mmio_write_32(DBSC_DBTR(2), 0);
  1847. /* DBTR3.TRCD: tRCD */
  1848. mmio_write_32(DBSC_DBTR(3), js2[js2_trcd]);
  1849. /* DBTR4.TRPA,TRP: tRPab,tRPpb */
  1850. mmio_write_32(DBSC_DBTR(4), (js2[js2_trpab] << 16) | js2[js2_trppb]);
  1851. /* DBTR5.TRC : use tRCpb */
  1852. mmio_write_32(DBSC_DBTR(5), js2[js2_trcpb]);
  1853. /* DBTR6.TRAS : tRAS */
  1854. mmio_write_32(DBSC_DBTR(6), js2[js2_tras]);
  1855. /* DBTR7.TRRD : tRRD */
  1856. mmio_write_32(DBSC_DBTR(7), (js2[js2_trrd] << 16) | js2[js2_trrd]);
  1857. /* DBTR8.TFAW : tFAW */
  1858. mmio_write_32(DBSC_DBTR(8), js2[js2_tfaw]);
  1859. /* DBTR9.TRDPR : tRTP */
  1860. mmio_write_32(DBSC_DBTR(9), js2[js2_trtp]);
  1861. /* DBTR10.TWR : nWR */
  1862. mmio_write_32(DBSC_DBTR(10), js1[js1_ind].nwr);
  1863. /*
  1864. * DBTR11.TRDWR : RL + BL / 2 + Rounddown(tRPST) + PHY_ODTLoff -
  1865. * odtlon + tDQSCK - tODTon,min +
  1866. * PCB delay (out+in) + tPHY_ODToff
  1867. */
  1868. mmio_write_32(DBSC_DBTR(11),
  1869. RL + (16 / 2) + 1 + 2 - js1[js1_ind].odtlon +
  1870. js2[js2_tdqsck] - js2[js2_tODTon_min] +
  1871. _f_scale(ddr_mbps, ddr_mbpsdiv, 1300, 0));
  1872. /* DBTR12.TWRRD : WL + 1 + BL/2 + tWTR */
  1873. data_l = WL + 1 + (16 / 2) + js2[js2_twtr];
  1874. mmio_write_32(DBSC_DBTR(12), (data_l << 16) | data_l);
  1875. /* DBTR13.TRFCAB : tRFCab */
  1876. mmio_write_32(DBSC_DBTR(13), (js2[js2_trfcab]));
  1877. /* DBTR14.TCKEHDLL,tCKEH : tCKEHCMD,tCKEHCMD */
  1878. mmio_write_32(DBSC_DBTR(14),
  1879. (js2[js2_tckehcmd] << 16) | (js2[js2_tckehcmd]));
  1880. /* DBTR15.TCKESR,TCKEL : tSR,tCKELPD */
  1881. mmio_write_32(DBSC_DBTR(15), (js2[js2_tsr] << 16) | (js2[js2_tckelpd]));
  1882. /* DBTR16 */
  1883. /* WDQL : tphy_wrlat + tphy_wrdata */
  1884. tmp[0] = ddrtbl_getval(_cnf_DDR_PI_REGSET, _reg_PI_WRLAT_F1);
  1885. /* DQENLTNCY : tphy_wrlat = WL-2 : PHY_WRITE_PATH_LAT_ADD == 0
  1886. * tphy_wrlat = WL-3 : PHY_WRITE_PATH_LAT_ADD != 0
  1887. */
  1888. tmp[1] = ddrtbl_getval(_cnf_DDR_PI_REGSET, _reg_PI_WRLAT_ADJ_F1);
  1889. /* DQL : tphy_rdlat + trdata_en */
  1890. /* it is not important for dbsc */
  1891. tmp[2] = RL + 16;
  1892. /* DQIENLTNCY : trdata_en */
  1893. tmp[3] = ddrtbl_getval(_cnf_DDR_PI_REGSET, _reg_PI_RDLAT_ADJ_F1) - 1;
  1894. mmio_write_32(DBSC_DBTR(16),
  1895. (tmp[3] << 24) | (tmp[2] << 16) | (tmp[1] << 8) | tmp[0]);
  1896. /* DBTR24 */
  1897. /* WRCSLAT = WRLAT -5 */
  1898. tmp[0] -= 5;
  1899. /* WRCSGAP = 5 */
  1900. tmp[1] = 5;
  1901. /* RDCSLAT = RDLAT_ADJ +2 */
  1902. if (prr_product == PRR_PRODUCT_M3) {
  1903. tmp[2] = tmp[3];
  1904. } else {
  1905. tmp[2] = tmp[3] + 2;
  1906. }
  1907. /* RDCSGAP = 6 */
  1908. if (prr_product == PRR_PRODUCT_M3) {
  1909. tmp[3] = 4;
  1910. } else {
  1911. tmp[3] = 6;
  1912. }
  1913. mmio_write_32(DBSC_DBTR(24),
  1914. (tmp[3] << 24) | (tmp[2] << 16) | (tmp[1] << 8) | tmp[0]);
  1915. /* DBTR17.TMODRD,TMOD,TRDMR: tMRR,tMRD,(0) */
  1916. mmio_write_32(DBSC_DBTR(17),
  1917. (js2[js2_tmrr] << 24) | (js2[js2_tmrd] << 16));
  1918. /* DBTR18.RODTL, RODTA, WODTL, WODTA : do not use in LPDDR4 */
  1919. mmio_write_32(DBSC_DBTR(18), 0);
  1920. /* DBTR19.TZQCL, TZQCS : do not use in LPDDR4 */
  1921. mmio_write_32(DBSC_DBTR(19), 0);
  1922. /* DBTR20.TXSDLL, TXS : tRFCab+tCKEHCMD */
  1923. data_l = js2[js2_trfcab] + js2[js2_tckehcmd];
  1924. mmio_write_32(DBSC_DBTR(20), (data_l << 16) | data_l);
  1925. /* DBTR21.TCCD */
  1926. /* DBTR23.TCCD */
  1927. /* H3 Ver.1.0 cannot use TBTR23 feature */
  1928. if (ddr_tccd == 8 &&
  1929. !((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_10))
  1930. ) {
  1931. data_l = 8;
  1932. mmio_write_32(DBSC_DBTR(21), (data_l << 16) | data_l);
  1933. mmio_write_32(DBSC_DBTR(23), 0x00000002);
  1934. } else if (ddr_tccd <= 11) {
  1935. data_l = 11;
  1936. mmio_write_32(DBSC_DBTR(21), (data_l << 16) | data_l);
  1937. mmio_write_32(DBSC_DBTR(23), 0x00000000);
  1938. } else {
  1939. data_l = ddr_tccd;
  1940. mmio_write_32(DBSC_DBTR(21), (data_l << 16) | data_l);
  1941. mmio_write_32(DBSC_DBTR(23), 0x00000000);
  1942. }
  1943. /* DBTR22.ZQLAT : */
  1944. data_l = js2[js2_tzqcalns] * 100; /* 1000 * 1000 ps */
  1945. data_l = (data_l << 16) | (js2[js2_tzqlat] + 24 + 20);
  1946. mmio_write_32(DBSC_DBTR(22), data_l);
  1947. /* DBTR25 : do not use in LPDDR4 */
  1948. mmio_write_32(DBSC_DBTR(25), 0);
  1949. /* DBRNK : */
  1950. /*
  1951. * DBSC_DBRNK2 rkrr
  1952. * DBSC_DBRNK3 rkrw
  1953. * DBSC_DBRNK4 rkwr
  1954. * DBSC_DBRNK5 rkww
  1955. */
  1956. #define _par_DBRNK_VAL (0x7007)
  1957. for (i = 0; i < 4; i++) {
  1958. data_l = (_par_DBRNK_VAL >> (i * 4)) & 0x0f;
  1959. if ((prr_product == PRR_PRODUCT_H3) &&
  1960. (prr_cut > PRR_PRODUCT_11) && (i == 0)) {
  1961. data_l += 1;
  1962. }
  1963. data_l2 = 0;
  1964. foreach_vch(ch) {
  1965. data_l2 = data_l2 | (data_l << (4 * ch));
  1966. }
  1967. mmio_write_32(DBSC_DBRNK(2 + i), data_l2);
  1968. }
  1969. mmio_write_32(DBSC_DBADJ0, 0x00000000);
  1970. /* timing registers for Scheduler */
  1971. /* SCFCTST0 */
  1972. /* SCFCTST0 ACT-ACT */
  1973. tmp[3] = 1UL * js2[js2_trcpb] * 800 * ddr_mbpsdiv / ddr_mbps;
  1974. /* SCFCTST0 RDA-ACT */
  1975. tmp[2] =
  1976. 1UL * ((16 / 2) + js2[js2_trtp] - 8 +
  1977. js2[js2_trppb]) * 800 * ddr_mbpsdiv / ddr_mbps;
  1978. /* SCFCTST0 WRA-ACT */
  1979. tmp[1] =
  1980. 1UL * (WL + 1 + (16 / 2) +
  1981. js1[js1_ind].nwr) * 800 * ddr_mbpsdiv / ddr_mbps;
  1982. /* SCFCTST0 PRE-ACT */
  1983. tmp[0] = 1UL * js2[js2_trppb];
  1984. mmio_write_32(DBSC_SCFCTST0,
  1985. (tmp[3] << 24) | (tmp[2] << 16) | (tmp[1] << 8) | tmp[0]);
  1986. /* SCFCTST1 */
  1987. /* SCFCTST1 RD-WR */
  1988. tmp[3] =
  1989. 1UL * (mmio_read_32(DBSC_DBTR(11)) & 0xff) * 800 * ddr_mbpsdiv /
  1990. ddr_mbps;
  1991. /* SCFCTST1 WR-RD */
  1992. tmp[2] =
  1993. 1UL * (mmio_read_32(DBSC_DBTR(12)) & 0xff) * 800 * ddr_mbpsdiv /
  1994. ddr_mbps;
  1995. /* SCFCTST1 ACT-RD/WR */
  1996. tmp[1] = 1UL * js2[js2_trcd] * 800 * ddr_mbpsdiv / ddr_mbps;
  1997. /* SCFCTST1 ASYNCOFS */
  1998. tmp[0] = 12;
  1999. mmio_write_32(DBSC_SCFCTST1,
  2000. (tmp[3] << 24) | (tmp[2] << 16) | (tmp[1] << 8) | tmp[0]);
  2001. /* DBSCHRW1 */
  2002. /* DBSCHRW1 SCTRFCAB */
  2003. tmp[0] = 1UL * js2[js2_trfcab] * 800 * ddr_mbpsdiv / ddr_mbps;
  2004. data_l = (((mmio_read_32(DBSC_DBTR(16)) & 0x00FF0000) >> 16)
  2005. + (mmio_read_32(DBSC_DBTR(22)) & 0x0000FFFF)
  2006. + (0x28 * 2)) * 400 * 2 * ddr_mbpsdiv / ddr_mbps + 7;
  2007. if (tmp[0] < data_l)
  2008. tmp[0] = data_l;
  2009. if ((prr_product == PRR_PRODUCT_M3) && (prr_cut < PRR_PRODUCT_30)) {
  2010. mmio_write_32(DBSC_DBSCHRW1, tmp[0]
  2011. + ((mmio_read_32(DBSC_DBTR(22)) & 0x0000FFFF)
  2012. * 400 * 2 * ddr_mbpsdiv + (ddr_mbps - 1)) /
  2013. ddr_mbps - 3);
  2014. } else {
  2015. mmio_write_32(DBSC_DBSCHRW1, tmp[0]
  2016. + ((mmio_read_32(DBSC_DBTR(22)) & 0x0000FFFF)
  2017. * 400 * 2 * ddr_mbpsdiv + (ddr_mbps - 1)) /
  2018. ddr_mbps);
  2019. }
  2020. /* QOS and CAM */
  2021. #ifdef ddr_qos_init_setting /* only for non qos_init */
  2022. /*wbkwait(0004), wbkmdhi(4,2),wbkmdlo(1,8) */
  2023. mmio_write_32(DBSC_DBCAM0CNF1, 0x00043218);
  2024. /*0(fillunit),8(dirtymax),4(dirtymin) */
  2025. mmio_write_32(DBSC_DBCAM0CNF2, 0x000000F4);
  2026. /*stop_tolerance */
  2027. mmio_write_32(DBSC_DBSCHRW0, 0x22421111);
  2028. /*rd-wr/wr-rd toggle priority */
  2029. mmio_write_32(DBSC_SCFCTST2, 0x012F1123);
  2030. mmio_write_32(DBSC_DBSCHSZ0, 0x00000001);
  2031. mmio_write_32(DBSC_DBSCHCNT0, 0x000F0037);
  2032. /* QoS Settings */
  2033. mmio_write_32(DBSC_DBSCHQOS00, 0x00000F00U);
  2034. mmio_write_32(DBSC_DBSCHQOS01, 0x00000B00U);
  2035. mmio_write_32(DBSC_DBSCHQOS02, 0x00000000U);
  2036. mmio_write_32(DBSC_DBSCHQOS03, 0x00000000U);
  2037. mmio_write_32(DBSC_DBSCHQOS40, 0x00000300U);
  2038. mmio_write_32(DBSC_DBSCHQOS41, 0x000002F0U);
  2039. mmio_write_32(DBSC_DBSCHQOS42, 0x00000200U);
  2040. mmio_write_32(DBSC_DBSCHQOS43, 0x00000100U);
  2041. mmio_write_32(DBSC_DBSCHQOS90, 0x00000100U);
  2042. mmio_write_32(DBSC_DBSCHQOS91, 0x000000F0U);
  2043. mmio_write_32(DBSC_DBSCHQOS92, 0x000000A0U);
  2044. mmio_write_32(DBSC_DBSCHQOS93, 0x00000040U);
  2045. mmio_write_32(DBSC_DBSCHQOS120, 0x00000040U);
  2046. mmio_write_32(DBSC_DBSCHQOS121, 0x00000030U);
  2047. mmio_write_32(DBSC_DBSCHQOS122, 0x00000020U);
  2048. mmio_write_32(DBSC_DBSCHQOS123, 0x00000010U);
  2049. mmio_write_32(DBSC_DBSCHQOS130, 0x00000100U);
  2050. mmio_write_32(DBSC_DBSCHQOS131, 0x000000F0U);
  2051. mmio_write_32(DBSC_DBSCHQOS132, 0x000000A0U);
  2052. mmio_write_32(DBSC_DBSCHQOS133, 0x00000040U);
  2053. mmio_write_32(DBSC_DBSCHQOS140, 0x000000C0U);
  2054. mmio_write_32(DBSC_DBSCHQOS141, 0x000000B0U);
  2055. mmio_write_32(DBSC_DBSCHQOS142, 0x00000080U);
  2056. mmio_write_32(DBSC_DBSCHQOS143, 0x00000040U);
  2057. mmio_write_32(DBSC_DBSCHQOS150, 0x00000040U);
  2058. mmio_write_32(DBSC_DBSCHQOS151, 0x00000030U);
  2059. mmio_write_32(DBSC_DBSCHQOS152, 0x00000020U);
  2060. mmio_write_32(DBSC_DBSCHQOS153, 0x00000010U);
  2061. mmio_write_32(QOSCTRL_RAEN, 0x00000001U);
  2062. #endif /* ddr_qos_init_setting */
  2063. /* H3 Ver.1.1 need to set monitor function */
  2064. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut == PRR_PRODUCT_11)) {
  2065. mmio_write_32(DBSC_DBMONCONF4, 0x00700000);
  2066. }
  2067. if (prr_product == PRR_PRODUCT_H3) {
  2068. if (prr_cut == PRR_PRODUCT_10) {
  2069. /* resrdis, simple mode, sc off */
  2070. mmio_write_32(DBSC_DBBCAMDIS, 0x00000007);
  2071. } else if (prr_cut == PRR_PRODUCT_11) {
  2072. /* resrdis, simple mode */
  2073. mmio_write_32(DBSC_DBBCAMDIS, 0x00000005);
  2074. } else if (prr_cut < PRR_PRODUCT_30) {
  2075. /* H3 Ver.2.0 */
  2076. /* resrdis */
  2077. mmio_write_32(DBSC_DBBCAMDIS, 0x00000001);
  2078. } else { /* H3 Ver.3.0(include H3N) */
  2079. /* exprespque */
  2080. mmio_write_32(DBSC_DBBCAMDIS, 0x00000010);
  2081. }
  2082. } else { /* M3-W/M3-N/V3H */
  2083. /* resrdis */
  2084. mmio_write_32(DBSC_DBBCAMDIS, 0x00000001);
  2085. }
  2086. }
  2087. static void dbsc_regset_post(void)
  2088. {
  2089. uint32_t ch, cs;
  2090. uint32_t data_l;
  2091. uint32_t slice, rdlat_max, rdlat_min;
  2092. rdlat_max = 0;
  2093. rdlat_min = 0xffff;
  2094. foreach_vch(ch) {
  2095. for (cs = 0; cs < CS_CNT; cs++) {
  2096. if ((ch_have_this_cs[cs] & (1U << ch)) != 0) {
  2097. for (slice = 0; slice < SLICE_CNT; slice++) {
  2098. ddr_setval_s(ch, slice,
  2099. _reg_PHY_PER_CS_TRAINING_INDEX,
  2100. cs);
  2101. data_l = ddr_getval_s(ch, slice,
  2102. _reg_PHY_RDDQS_LATENCY_ADJUST);
  2103. if (data_l > rdlat_max)
  2104. rdlat_max = data_l;
  2105. if (data_l < rdlat_min)
  2106. rdlat_min = data_l;
  2107. }
  2108. }
  2109. }
  2110. }
  2111. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut > PRR_PRODUCT_11)) {
  2112. #if RCAR_DRAM_SPLIT == 2
  2113. if (board_cnf->phyvalid == 0x05) {
  2114. mmio_write_32(DBSC_DBTR(24),
  2115. (rdlat_max << 24) + (rdlat_min << 16) +
  2116. mmio_read_32(DBSC_DBTR(24)));
  2117. } else {
  2118. mmio_write_32(DBSC_DBTR(24),
  2119. ((rdlat_max * 2 - rdlat_min + 4) << 24) +
  2120. ((rdlat_min + 2) << 16) +
  2121. mmio_read_32(DBSC_DBTR(24)));
  2122. }
  2123. #else /*RCAR_DRAM_SPLIT == 2 */
  2124. mmio_write_32(DBSC_DBTR(24),
  2125. ((rdlat_max * 2 - rdlat_min + 4) << 24) +
  2126. ((rdlat_min + 2) << 16) +
  2127. mmio_read_32(DBSC_DBTR(24)));
  2128. #endif /*RCAR_DRAM_SPLIT == 2 */
  2129. } else {
  2130. mmio_write_32(DBSC_DBTR(24),
  2131. ((rdlat_max + 2) << 24) +
  2132. ((rdlat_max + 2) << 16) +
  2133. mmio_read_32(DBSC_DBTR(24)));
  2134. }
  2135. /* set ddr density information */
  2136. foreach_ech(ch) {
  2137. for (cs = 0; cs < CS_CNT; cs++) {
  2138. if (ddr_density[ch][cs] == 0xff) {
  2139. mmio_write_32(DBSC_DBMEMCONF(ch, cs), 0x00);
  2140. } else {
  2141. mmio_write_32(DBSC_DBMEMCONF(ch, cs),
  2142. DBMEMCONF_REGD(ddr_density[ch]
  2143. [cs]));
  2144. }
  2145. }
  2146. mmio_write_32(DBSC_DBMEMCONF(ch, 2), 0x00000000);
  2147. mmio_write_32(DBSC_DBMEMCONF(ch, 3), 0x00000000);
  2148. }
  2149. mmio_write_32(DBSC_DBBUS0CNF1, 0x00000010);
  2150. /*set DBI */
  2151. if (board_cnf->dbi_en)
  2152. mmio_write_32(DBSC_DBDBICNT, 0x00000003);
  2153. /* H3 Ver.2.0 or later/M3-N/V3H DBI wa */
  2154. if ((((prr_product == PRR_PRODUCT_H3) &&
  2155. (prr_cut > PRR_PRODUCT_11)) ||
  2156. (prr_product == PRR_PRODUCT_M3N) ||
  2157. (prr_product == PRR_PRODUCT_V3H)) &&
  2158. board_cnf->dbi_en)
  2159. reg_ddrphy_write_a(0x00001010, 0x01000000);
  2160. /*set REFCYCLE */
  2161. data_l = (get_refperiod()) * ddr_mbps / 2000 / ddr_mbpsdiv;
  2162. mmio_write_32(DBSC_DBRFCNF1, 0x00080000 | (data_l & 0x0000ffff));
  2163. mmio_write_32(DBSC_DBRFCNF2, 0x00010000 | DBSC_REFINTS);
  2164. #if RCAR_REWT_TRAINING != 0
  2165. /* Periodic-WriteDQ Training seeting */
  2166. if (((prr_product == PRR_PRODUCT_H3) &&
  2167. (prr_cut <= PRR_PRODUCT_11)) ||
  2168. ((prr_product == PRR_PRODUCT_M3) &&
  2169. (prr_cut == PRR_PRODUCT_10))) {
  2170. /* non : H3 Ver.1.x/M3-W Ver.1.0 not support */
  2171. } else {
  2172. /* H3 Ver.2.0 or later/M3-W Ver.1.1 or later/M3-N/V3H */
  2173. mmio_write_32(DBSC_DBDFIPMSTRCNF, 0x00000000);
  2174. ddr_setval_ach_as(_reg_PHY_WDQLVL_PATT, 0x04);
  2175. ddr_setval_ach_as(_reg_PHY_WDQLVL_QTR_DLY_STEP, 0x0F);
  2176. ddr_setval_ach_as(_reg_PHY_WDQLVL_DLY_STEP, 0x50);
  2177. ddr_setval_ach_as(_reg_PHY_WDQLVL_DQDM_SLV_DLY_START, 0x0300);
  2178. ddr_setval_ach(_reg_PI_WDQLVL_CS_MAP,
  2179. ddrtbl_getval(_cnf_DDR_PI_REGSET,
  2180. _reg_PI_WDQLVL_CS_MAP));
  2181. ddr_setval_ach(_reg_PI_LONG_COUNT_MASK, 0x1f);
  2182. ddr_setval_ach(_reg_PI_WDQLVL_VREF_EN, 0x00);
  2183. ddr_setval_ach(_reg_PI_WDQLVL_ROTATE, 0x01);
  2184. ddr_setval_ach(_reg_PI_TREF_F0, 0x0000);
  2185. ddr_setval_ach(_reg_PI_TREF_F1, 0x0000);
  2186. ddr_setval_ach(_reg_PI_TREF_F2, 0x0000);
  2187. if (prr_product == PRR_PRODUCT_M3) {
  2188. ddr_setval_ach(_reg_PI_WDQLVL_EN, 0x02);
  2189. } else {
  2190. ddr_setval_ach(_reg_PI_WDQLVL_EN_F1, 0x02);
  2191. }
  2192. ddr_setval_ach(_reg_PI_WDQLVL_PERIODIC, 0x01);
  2193. /* DFI_PHYMSTR_ACK , WTmode setting */
  2194. /* DFI_PHYMSTR_ACK: WTmode =b'01 */
  2195. mmio_write_32(DBSC_DBDFIPMSTRCNF, 0x00000011);
  2196. }
  2197. #endif /* RCAR_REWT_TRAINING */
  2198. /* periodic dram zqcal enable */
  2199. mmio_write_32(DBSC_DBCALCNF, 0x01000010);
  2200. /* periodic phy ctrl update enable */
  2201. if (((prr_product == PRR_PRODUCT_H3) &&
  2202. (prr_cut <= PRR_PRODUCT_11)) ||
  2203. ((prr_product == PRR_PRODUCT_M3) &&
  2204. (prr_cut < PRR_PRODUCT_30))) {
  2205. /* non : H3 Ver.1.x/M3-W Ver.1.x not support */
  2206. } else {
  2207. #if RCAR_DRAM_SPLIT == 2
  2208. if ((prr_product == PRR_PRODUCT_H3) &&
  2209. (board_cnf->phyvalid == 0x05))
  2210. mmio_write_32(DBSC_DBDFICUPDCNF, 0x2a240001);
  2211. else
  2212. mmio_write_32(DBSC_DBDFICUPDCNF, 0x28240001);
  2213. #else /* RCAR_DRAM_SPLIT == 2 */
  2214. mmio_write_32(DBSC_DBDFICUPDCNF, 0x28240001);
  2215. #endif /* RCAR_DRAM_SPLIT == 2 */
  2216. }
  2217. #ifdef DDR_BACKUPMODE
  2218. /* SRX */
  2219. if (ddr_backup == DRAM_BOOT_STATUS_WARM) {
  2220. #ifdef DDR_BACKUPMODE_HALF /* for Half channel(ch0, 1 only) */
  2221. NOTICE("BL2: [DEBUG_MESS] DDR_BACKUPMODE_HALF\n");
  2222. send_dbcmd(0x0A040001);
  2223. if (Prr_Product == PRR_PRODUCT_H3)
  2224. send_dbcmd(0x0A140001);
  2225. #else /* DDR_BACKUPMODE_HALF */ /* for All channels */
  2226. send_dbcmd(0x0A840001);
  2227. #endif /* DDR_BACKUPMODE_HALF */
  2228. }
  2229. #endif /* DDR_BACKUPMODE */
  2230. /* set Auto Refresh */
  2231. mmio_write_32(DBSC_DBRFEN, 0x00000001);
  2232. #if RCAR_REWT_TRAINING != 0
  2233. /* Periodic WriteDQ Traning */
  2234. if (((prr_product == PRR_PRODUCT_H3) &&
  2235. (prr_cut <= PRR_PRODUCT_11)) ||
  2236. ((prr_product == PRR_PRODUCT_M3) &&
  2237. (prr_cut == PRR_PRODUCT_10))) {
  2238. /* non : H3 Ver.1.x/M3-W Ver.1.0 not support */
  2239. } else {
  2240. /* H3 Ver.2.0 or later/M3-W Ver.1.1 or later/M3-N/V3H */
  2241. ddr_setval_ach(_reg_PI_WDQLVL_INTERVAL, 0x0100);
  2242. }
  2243. #endif /* RCAR_REWT_TRAINING */
  2244. /* dram access enable */
  2245. mmio_write_32(DBSC_DBACEN, 0x00000001);
  2246. MSG_LF(__func__ "(done)");
  2247. }
  2248. /* DFI_INIT_START */
  2249. static uint32_t dfi_init_start(void)
  2250. {
  2251. uint32_t ch;
  2252. uint32_t phytrainingok;
  2253. uint32_t retry;
  2254. uint32_t data_l;
  2255. const uint32_t RETRY_MAX = 0x10000;
  2256. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  2257. /* PLL3 Disable */
  2258. /* protect register interface */
  2259. ddrphy_regif_idle();
  2260. pll3_control(0);
  2261. /* init start */
  2262. /* dbdficnt0:
  2263. * dfi_dram_clk_disable=1
  2264. * dfi_frequency = 0
  2265. * freq_ratio = 01 (2:1)
  2266. * init_start =0
  2267. */
  2268. foreach_vch(ch)
  2269. mmio_write_32(DBSC_DBDFICNT(ch), 0x00000F10);
  2270. dsb_sev();
  2271. /* dbdficnt0:
  2272. * dfi_dram_clk_disable=1
  2273. * dfi_frequency = 0
  2274. * freq_ratio = 01 (2:1)
  2275. * init_start =1
  2276. */
  2277. foreach_vch(ch)
  2278. mmio_write_32(DBSC_DBDFICNT(ch), 0x00000F11);
  2279. dsb_sev();
  2280. } else {
  2281. ddr_setval_ach_as(_reg_PHY_DLL_RST_EN, 0x02);
  2282. dsb_sev();
  2283. ddrphy_regif_idle();
  2284. }
  2285. /* dll_rst negate */
  2286. foreach_vch(ch)
  2287. mmio_write_32(DBSC_DBPDCNT3(ch), 0x0000CF01);
  2288. dsb_sev();
  2289. /* wait init_complete */
  2290. phytrainingok = 0;
  2291. retry = 0;
  2292. while (retry++ < RETRY_MAX) {
  2293. foreach_vch(ch) {
  2294. data_l = mmio_read_32(DBSC_DBDFISTAT(ch));
  2295. if (data_l & 0x00000001)
  2296. phytrainingok |= (1U << ch);
  2297. }
  2298. dsb_sev();
  2299. if (phytrainingok == ddr_phyvalid)
  2300. break;
  2301. if (retry % 256 == 0)
  2302. ddr_setval_ach_as(_reg_SC_PHY_RX_CAL_START, 0x01);
  2303. }
  2304. /* all ch ok? */
  2305. if ((phytrainingok & ddr_phyvalid) != ddr_phyvalid)
  2306. return 0xff;
  2307. /* dbdficnt0:
  2308. * dfi_dram_clk_disable=0
  2309. * dfi_frequency = 0
  2310. * freq_ratio = 01 (2:1)
  2311. * init_start =0
  2312. */
  2313. foreach_vch(ch)
  2314. mmio_write_32(DBSC_DBDFICNT(ch), 0x00000010);
  2315. dsb_sev();
  2316. return 0;
  2317. }
  2318. /* drivablity setting : CMOS MODE ON/OFF */
  2319. static void change_lpddr4_en(uint32_t mode)
  2320. {
  2321. uint32_t ch;
  2322. uint32_t i;
  2323. uint32_t data_l;
  2324. const uint32_t _reg_PHY_PAD_DRIVE_X[3] = {
  2325. _reg_PHY_PAD_ADDR_DRIVE,
  2326. _reg_PHY_PAD_CLK_DRIVE,
  2327. _reg_PHY_PAD_CS_DRIVE
  2328. };
  2329. foreach_vch(ch) {
  2330. for (i = 0; i < 3; i++) {
  2331. data_l = ddr_getval(ch, _reg_PHY_PAD_DRIVE_X[i]);
  2332. if (mode) {
  2333. data_l |= (1U << 14);
  2334. } else {
  2335. data_l &= ~(1U << 14);
  2336. }
  2337. ddr_setval(ch, _reg_PHY_PAD_DRIVE_X[i], data_l);
  2338. }
  2339. }
  2340. }
  2341. /* drivablity setting */
  2342. static uint32_t set_term_code(void)
  2343. {
  2344. int32_t i;
  2345. uint32_t ch, index;
  2346. uint32_t data_l;
  2347. uint32_t chip_id[2];
  2348. uint32_t term_code;
  2349. uint32_t override;
  2350. uint32_t pvtr;
  2351. uint32_t pvtp;
  2352. uint32_t pvtn;
  2353. term_code = ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET,
  2354. _reg_PHY_PAD_DATA_TERM);
  2355. override = 0;
  2356. for (i = 0; i < 2; i++)
  2357. chip_id[i] = mmio_read_32(LIFEC_CHIPID(i));
  2358. index = 0;
  2359. while (1) {
  2360. if (termcode_by_sample[index][0] == 0xffffffff) {
  2361. break;
  2362. }
  2363. if ((termcode_by_sample[index][0] == chip_id[0]) &&
  2364. (termcode_by_sample[index][1] == chip_id[1])) {
  2365. term_code = termcode_by_sample[index][2];
  2366. override = 1;
  2367. break;
  2368. }
  2369. index++;
  2370. }
  2371. if (override) {
  2372. for (index = 0; index < _reg_PHY_PAD_TERM_X_NUM; index++) {
  2373. data_l =
  2374. ddrtbl_getval(_cnf_DDR_PHY_ADR_G_REGSET,
  2375. _reg_PHY_PAD_TERM_X[index]);
  2376. data_l = (data_l & 0xfffe0000) | term_code;
  2377. ddr_setval_ach(_reg_PHY_PAD_TERM_X[index], data_l);
  2378. }
  2379. } else if ((prr_product == PRR_PRODUCT_M3) &&
  2380. (prr_cut == PRR_PRODUCT_10)) {
  2381. /* non */
  2382. } else {
  2383. ddr_setval_ach(_reg_PHY_PAD_TERM_X[0],
  2384. (ddrtbl_getval
  2385. (_cnf_DDR_PHY_ADR_G_REGSET,
  2386. _reg_PHY_PAD_TERM_X[0]) & 0xFFFE0000));
  2387. ddr_setval_ach(_reg_PHY_CAL_CLEAR_0, 0x01);
  2388. ddr_setval_ach(_reg_PHY_CAL_START_0, 0x01);
  2389. foreach_vch(ch) {
  2390. do {
  2391. data_l =
  2392. ddr_getval(ch, _reg_PHY_CAL_RESULT2_OBS_0);
  2393. } while (!(data_l & 0x00800000));
  2394. }
  2395. if ((prr_product == PRR_PRODUCT_H3) &&
  2396. (prr_cut <= PRR_PRODUCT_11)) {
  2397. foreach_vch(ch) {
  2398. data_l = ddr_getval(ch, _reg_PHY_PAD_TERM_X[0]);
  2399. pvtr = (data_l >> 12) & 0x1f;
  2400. pvtr += 8;
  2401. if (pvtr > 0x1f)
  2402. pvtr = 0x1f;
  2403. data_l =
  2404. ddr_getval(ch, _reg_PHY_CAL_RESULT2_OBS_0);
  2405. pvtn = (data_l >> 6) & 0x03f;
  2406. pvtp = (data_l >> 0) & 0x03f;
  2407. for (index = 0; index < _reg_PHY_PAD_TERM_X_NUM;
  2408. index++) {
  2409. data_l =
  2410. ddrtbl_getval
  2411. (_cnf_DDR_PHY_ADR_G_REGSET,
  2412. _reg_PHY_PAD_TERM_X[index]);
  2413. data_l = (data_l & 0xfffe0000)
  2414. | (pvtr << 12)
  2415. | (pvtn << 6)
  2416. | (pvtp);
  2417. ddr_setval(ch,
  2418. _reg_PHY_PAD_TERM_X[index],
  2419. data_l);
  2420. }
  2421. }
  2422. } else {
  2423. /* M3-W Ver.1.1 or later/H3 Ver.2.0 or later/M3-N/V3H */
  2424. foreach_vch(ch) {
  2425. for (index = 0; index < _reg_PHY_PAD_TERM_X_NUM;
  2426. index++) {
  2427. data_l =
  2428. ddr_getval(ch,
  2429. _reg_PHY_PAD_TERM_X
  2430. [index]);
  2431. ddr_setval(ch,
  2432. _reg_PHY_PAD_TERM_X[index],
  2433. (data_l & 0xFFFE0FFF) |
  2434. 0x00015000);
  2435. }
  2436. }
  2437. }
  2438. }
  2439. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  2440. /* non */
  2441. } else {
  2442. ddr_padcal_tcompensate_getinit(override);
  2443. }
  2444. return 0;
  2445. }
  2446. /* DDR mode register setting */
  2447. static void ddr_register_set(void)
  2448. {
  2449. int32_t fspwp;
  2450. uint32_t tmp;
  2451. for (fspwp = 1; fspwp >= 0; fspwp--) {
  2452. /*MR13, fspwp */
  2453. send_dbcmd(0x0e840d08 | ((2 - fspwp) << 6));
  2454. tmp =
  2455. ddrtbl_getval(_cnf_DDR_PI_REGSET,
  2456. reg_pi_mr1_data_fx_csx[fspwp][0]);
  2457. send_dbcmd(0x0e840100 | tmp);
  2458. tmp =
  2459. ddrtbl_getval(_cnf_DDR_PI_REGSET,
  2460. reg_pi_mr2_data_fx_csx[fspwp][0]);
  2461. send_dbcmd(0x0e840200 | tmp);
  2462. tmp =
  2463. ddrtbl_getval(_cnf_DDR_PI_REGSET,
  2464. reg_pi_mr3_data_fx_csx[fspwp][0]);
  2465. send_dbcmd(0x0e840300 | tmp);
  2466. tmp =
  2467. ddrtbl_getval(_cnf_DDR_PI_REGSET,
  2468. reg_pi_mr11_data_fx_csx[fspwp][0]);
  2469. send_dbcmd(0x0e840b00 | tmp);
  2470. tmp =
  2471. ddrtbl_getval(_cnf_DDR_PI_REGSET,
  2472. reg_pi_mr12_data_fx_csx[fspwp][0]);
  2473. send_dbcmd(0x0e840c00 | tmp);
  2474. tmp =
  2475. ddrtbl_getval(_cnf_DDR_PI_REGSET,
  2476. reg_pi_mr14_data_fx_csx[fspwp][0]);
  2477. send_dbcmd(0x0e840e00 | tmp);
  2478. /* MR22 */
  2479. send_dbcmd(0x0e841616);
  2480. /* ZQCAL start */
  2481. send_dbcmd(0x0d84004F);
  2482. /* ZQLAT */
  2483. send_dbcmd(0x0d840051);
  2484. }
  2485. /* MR13, fspwp */
  2486. send_dbcmd(0x0e840d08);
  2487. }
  2488. /* Training handshake functions */
  2489. static inline uint32_t wait_freqchgreq(uint32_t assert)
  2490. {
  2491. uint32_t data_l;
  2492. uint32_t count;
  2493. uint32_t ch;
  2494. count = 100000;
  2495. /* H3 Ver.1.x cannot see frqchg_req */
  2496. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  2497. return 0;
  2498. }
  2499. if (assert) {
  2500. do {
  2501. data_l = 1;
  2502. foreach_vch(ch) {
  2503. data_l &= mmio_read_32(DBSC_DBPDSTAT(ch));
  2504. }
  2505. count = count - 1;
  2506. } while (((data_l & 0x01) != 0x01) & (count != 0));
  2507. } else {
  2508. do {
  2509. data_l = 0;
  2510. foreach_vch(ch) {
  2511. data_l |= mmio_read_32(DBSC_DBPDSTAT(ch));
  2512. }
  2513. count = count - 1;
  2514. } while (((data_l & 0x01) != 0x00) & (count != 0));
  2515. }
  2516. return (count == 0);
  2517. }
  2518. static inline void set_freqchgack(uint32_t assert)
  2519. {
  2520. uint32_t ch;
  2521. uint32_t data_l;
  2522. if (assert)
  2523. data_l = 0x0CF20000;
  2524. else
  2525. data_l = 0x00000000;
  2526. foreach_vch(ch)
  2527. mmio_write_32(DBSC_DBPDCNT2(ch), data_l);
  2528. }
  2529. static inline void set_dfifrequency(uint32_t freq)
  2530. {
  2531. uint32_t ch;
  2532. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  2533. foreach_vch(ch)
  2534. mmio_clrsetbits_32(DBSC_DBPDCNT1(ch), 0x1fU, freq);
  2535. } else {
  2536. foreach_vch(ch) {
  2537. mmio_clrsetbits_32(DBSC_DBDFICNT(ch), 0x1fU << 24,
  2538. (freq << 24));
  2539. }
  2540. }
  2541. dsb_sev();
  2542. }
  2543. static uint32_t pll3_freq(uint32_t on)
  2544. {
  2545. uint32_t timeout;
  2546. timeout = wait_freqchgreq(1);
  2547. if ((!((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11))) && (on)) {
  2548. if (((1600U * ddr_mbpsdiv) < ddr_mbps) || (prr_product == PRR_PRODUCT_M3)) {
  2549. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_CTRL), 0x01421142U);
  2550. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_CTRL_CA), 0x00000142U);
  2551. } else {
  2552. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_CTRL), 0x03421342U);
  2553. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_PLL_CTRL_CA), 0x00000342U);
  2554. }
  2555. }
  2556. if (timeout) {
  2557. return 1;
  2558. }
  2559. pll3_control(on);
  2560. set_dfifrequency(on);
  2561. set_freqchgack(1);
  2562. timeout = wait_freqchgreq(0);
  2563. set_freqchgack(0);
  2564. if (timeout) {
  2565. FATAL_MSG("BL2: Time out[2]\n");
  2566. return 1;
  2567. }
  2568. return 0;
  2569. }
  2570. /* update dly */
  2571. static void update_dly(void)
  2572. {
  2573. ddr_setval_ach(_reg_SC_PHY_MANUAL_UPDATE, 0x01);
  2574. ddr_setval_ach(_reg_PHY_ADRCTL_MANUAL_UPDATE, 0x01);
  2575. }
  2576. /* training by pi */
  2577. static uint32_t pi_training_go(void)
  2578. {
  2579. uint32_t flag;
  2580. uint32_t data_l;
  2581. uint32_t retry;
  2582. const uint32_t RETRY_MAX = 4096 * 16;
  2583. uint32_t ch;
  2584. uint32_t mst_ch;
  2585. uint32_t cur_frq;
  2586. uint32_t complete;
  2587. uint32_t frqchg_req;
  2588. /* pi_start */
  2589. ddr_setval_ach(_reg_PI_START, 0x01);
  2590. foreach_vch(ch)
  2591. ddr_getval(ch, _reg_PI_INT_STATUS);
  2592. /* set dfi_phymstr_ack = 1 */
  2593. mmio_write_32(DBSC_DBDFIPMSTRCNF, 0x00000001);
  2594. dsb_sev();
  2595. /* wait pi_int_status[0] */
  2596. mst_ch = 0;
  2597. flag = 0;
  2598. complete = 0;
  2599. cur_frq = 0;
  2600. retry = RETRY_MAX;
  2601. do {
  2602. frqchg_req = mmio_read_32(DBSC_DBPDSTAT(mst_ch)) & 0x01;
  2603. /* H3 Ver.1.x cannot see frqchg_req */
  2604. if ((prr_product == PRR_PRODUCT_H3) &&
  2605. (prr_cut <= PRR_PRODUCT_11)) {
  2606. if ((retry % 4096) == 1) {
  2607. frqchg_req = 1;
  2608. } else {
  2609. frqchg_req = 0;
  2610. }
  2611. }
  2612. if (frqchg_req) {
  2613. if (cur_frq) {
  2614. /* Low frequency */
  2615. flag = pll3_freq(0);
  2616. cur_frq = 0;
  2617. } else {
  2618. /* High frequency */
  2619. flag = pll3_freq(1);
  2620. cur_frq = 1;
  2621. }
  2622. if (flag)
  2623. break;
  2624. } else {
  2625. if (cur_frq) {
  2626. foreach_vch(ch) {
  2627. if (complete & (1U << ch))
  2628. continue;
  2629. data_l =
  2630. ddr_getval(ch, _reg_PI_INT_STATUS);
  2631. if (data_l & 0x01) {
  2632. complete |= (1U << ch);
  2633. }
  2634. }
  2635. if (complete == ddr_phyvalid)
  2636. break;
  2637. }
  2638. }
  2639. } while (--retry);
  2640. foreach_vch(ch) {
  2641. /* dummy read */
  2642. data_l = ddr_getval_s(ch, 0, _reg_PHY_CAL_RESULT2_OBS_0);
  2643. data_l = ddr_getval(ch, _reg_PI_INT_STATUS);
  2644. ddr_setval(ch, _reg_PI_INT_ACK, data_l);
  2645. }
  2646. if (ddrphy_regif_chk()) {
  2647. return 0xfd;
  2648. }
  2649. return complete;
  2650. }
  2651. /* Initialize DDR */
  2652. static uint32_t init_ddr(void)
  2653. {
  2654. int32_t i;
  2655. uint32_t data_l;
  2656. uint32_t phytrainingok;
  2657. uint32_t ch, slice;
  2658. uint32_t err;
  2659. int16_t adj;
  2660. MSG_LF(__func__ ":0\n");
  2661. #ifdef DDR_BACKUPMODE
  2662. rcar_dram_get_boot_status(&ddr_backup);
  2663. #endif
  2664. /* unlock phy */
  2665. /* Unlock DDRPHY register(AGAIN) */
  2666. foreach_vch(ch)
  2667. mmio_write_32(DBSC_DBPDLK(ch), 0x0000A55A);
  2668. dsb_sev();
  2669. if ((((prr_product == PRR_PRODUCT_H3) &&
  2670. (prr_cut > PRR_PRODUCT_11)) ||
  2671. (prr_product == PRR_PRODUCT_M3N) ||
  2672. (prr_product == PRR_PRODUCT_V3H)) && board_cnf->dbi_en)
  2673. reg_ddrphy_write_a(0x00001010, 0x01000001);
  2674. else
  2675. reg_ddrphy_write_a(0x00001010, 0x00000001);
  2676. /* DBSC register pre-setting */
  2677. dbsc_regset_pre();
  2678. /* load ddrphy registers */
  2679. ddrtbl_load();
  2680. /* configure ddrphy registers */
  2681. ddr_config();
  2682. /* dfi_reset assert */
  2683. foreach_vch(ch)
  2684. mmio_write_32(DBSC_DBPDCNT0(ch), 0x01);
  2685. dsb_sev();
  2686. /* dbsc register set */
  2687. dbsc_regset();
  2688. MSG_LF(__func__ ":1\n");
  2689. /* dfi_reset negate */
  2690. foreach_vch(ch)
  2691. mmio_write_32(DBSC_DBPDCNT0(ch), 0x00);
  2692. dsb_sev();
  2693. /* dfi_init_start (start ddrphy) */
  2694. err = dfi_init_start();
  2695. if (err) {
  2696. return INITDRAM_ERR_I;
  2697. }
  2698. MSG_LF(__func__ ":2\n");
  2699. /* ddr backupmode end */
  2700. #ifdef DDR_BACKUPMODE
  2701. if (ddr_backup) {
  2702. NOTICE("BL2: [WARM_BOOT]\n");
  2703. } else {
  2704. NOTICE("BL2: [COLD_BOOT]\n");
  2705. }
  2706. err = rcar_dram_update_boot_status(ddr_backup);
  2707. if (err) {
  2708. NOTICE("BL2: [BOOT_STATUS_UPDATE_ERROR]\n");
  2709. return INITDRAM_ERR_I;
  2710. }
  2711. #endif
  2712. MSG_LF(__func__ ":3\n");
  2713. /* override term code after dfi_init_complete */
  2714. err = set_term_code();
  2715. if (err) {
  2716. return INITDRAM_ERR_I;
  2717. }
  2718. MSG_LF(__func__ ":4\n");
  2719. /* rx offset calibration */
  2720. if ((prr_cut > PRR_PRODUCT_11) || (prr_product == PRR_PRODUCT_M3N) ||
  2721. (prr_product == PRR_PRODUCT_V3H)) {
  2722. err = rx_offset_cal_hw();
  2723. } else {
  2724. err = rx_offset_cal();
  2725. }
  2726. if (err)
  2727. return INITDRAM_ERR_O;
  2728. MSG_LF(__func__ ":5\n");
  2729. /* Dummy PDE */
  2730. send_dbcmd(0x08840000);
  2731. /* PDX */
  2732. send_dbcmd(0x08840001);
  2733. /* check register i/f is alive */
  2734. err = ddrphy_regif_chk();
  2735. if (err) {
  2736. return INITDRAM_ERR_O;
  2737. }
  2738. MSG_LF(__func__ ":6\n");
  2739. /* phy initialize end */
  2740. /* setup DDR mode registers */
  2741. /* CMOS MODE */
  2742. change_lpddr4_en(0);
  2743. /* MRS */
  2744. ddr_register_set();
  2745. /* Thermal sensor setting */
  2746. /* THCTR Bit6: PONM=0 , Bit0: THSST=1 */
  2747. data_l = (mmio_read_32(THS1_THCTR) & 0xFFFFFFBF) | 0x00000001;
  2748. mmio_write_32(THS1_THCTR, data_l);
  2749. /* LPDDR4 MODE */
  2750. change_lpddr4_en(1);
  2751. MSG_LF(__func__ ":7\n");
  2752. /* mask CS_MAP if RANKx is not found */
  2753. foreach_vch(ch) {
  2754. data_l = ddr_getval(ch, _reg_PI_CS_MAP);
  2755. if (!(ch_have_this_cs[1] & (1U << ch)))
  2756. data_l = data_l & 0x05;
  2757. ddr_setval(ch, _reg_PI_CS_MAP, data_l);
  2758. }
  2759. /* exec pi_training */
  2760. reg_ddrphy_write_a(ddr_regdef_adr(_reg_PHY_FREQ_SEL_MULTICAST_EN),
  2761. BIT(ddr_regdef_lsb(_reg_PHY_FREQ_SEL_MULTICAST_EN)));
  2762. ddr_setval_ach_as(_reg_PHY_PER_CS_TRAINING_MULTICAST_EN, 0x00);
  2763. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  2764. ddr_setval_ach_as(_reg_PHY_PER_CS_TRAINING_EN, 0x01);
  2765. } else {
  2766. foreach_vch(ch) {
  2767. for (slice = 0; slice < SLICE_CNT; slice++) {
  2768. ddr_setval_s(ch, slice,
  2769. _reg_PHY_PER_CS_TRAINING_EN,
  2770. ((ch_have_this_cs[1]) >> ch)
  2771. & 0x01);
  2772. }
  2773. }
  2774. }
  2775. phytrainingok = pi_training_go();
  2776. if (ddr_phyvalid != (phytrainingok & ddr_phyvalid)) {
  2777. return INITDRAM_ERR_T | phytrainingok;
  2778. }
  2779. MSG_LF(__func__ ":8\n");
  2780. /* CACS DLY ADJUST */
  2781. data_l = board_cnf->cacs_dly + _f_scale_adj(board_cnf->cacs_dly_adj);
  2782. foreach_vch(ch) {
  2783. for (i = 0; i < _reg_PHY_CLK_CACS_SLAVE_DELAY_X_NUM; i++) {
  2784. adj = _f_scale_adj(board_cnf->ch[ch].cacs_adj[i]);
  2785. ddr_setval(ch, _reg_PHY_CLK_CACS_SLAVE_DELAY_X[i],
  2786. data_l + adj);
  2787. }
  2788. if (ddr_phycaslice == 1) {
  2789. for (i = 0; i < 6; i++) {
  2790. adj = _f_scale_adj(board_cnf->ch[ch].cacs_adj
  2791. [i +
  2792. _reg_PHY_CLK_CACS_SLAVE_DELAY_X_NUM]);
  2793. ddr_setval_s(ch, 2,
  2794. _reg_PHY_CLK_CACS_SLAVE_DELAY_X
  2795. [i],
  2796. data_l + adj
  2797. );
  2798. }
  2799. }
  2800. }
  2801. update_dly();
  2802. MSG_LF(__func__ ":9\n");
  2803. /* H3 fix rd latency to avoid bug in elasitic buffer */
  2804. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11))
  2805. adjust_rddqs_latency();
  2806. /* Adjust Write path latency */
  2807. if (ddrtbl_getval
  2808. (_cnf_DDR_PHY_SLICE_REGSET, _reg_PHY_WRITE_PATH_LAT_ADD))
  2809. adjust_wpath_latency();
  2810. /* RDQLVL Training */
  2811. if (!ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET, _reg_PHY_IE_MODE))
  2812. ddr_setval_ach_as(_reg_PHY_IE_MODE, 0x01);
  2813. err = rdqdm_man();
  2814. if (!ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET, _reg_PHY_IE_MODE))
  2815. ddr_setval_ach_as(_reg_PHY_IE_MODE, 0x00);
  2816. if (err) {
  2817. return INITDRAM_ERR_T;
  2818. }
  2819. update_dly();
  2820. MSG_LF(__func__ ":10\n");
  2821. /* WDQLVL Training */
  2822. err = wdqdm_man();
  2823. if (err) {
  2824. return INITDRAM_ERR_T;
  2825. }
  2826. update_dly();
  2827. MSG_LF(__func__ ":11\n");
  2828. /* training complete, setup DBSC */
  2829. if (((prr_product == PRR_PRODUCT_H3) && (prr_cut > PRR_PRODUCT_11)) ||
  2830. (prr_product == PRR_PRODUCT_M3N) ||
  2831. (prr_product == PRR_PRODUCT_V3H)) {
  2832. ddr_setval_ach_as(_reg_PHY_DFI40_POLARITY, 0x00);
  2833. ddr_setval_ach(_reg_PI_DFI40_POLARITY, 0x00);
  2834. }
  2835. dbsc_regset_post();
  2836. MSG_LF(__func__ ":12\n");
  2837. return phytrainingok;
  2838. }
  2839. /* SW LEVELING COMMON */
  2840. static uint32_t swlvl1(uint32_t ddr_csn, uint32_t reg_cs, uint32_t reg_kick)
  2841. {
  2842. uint32_t ch;
  2843. uint32_t data_l;
  2844. uint32_t retry;
  2845. uint32_t waiting;
  2846. uint32_t err;
  2847. const uint32_t RETRY_MAX = 0x1000;
  2848. err = 0;
  2849. /* set EXIT -> OP_DONE is cleared */
  2850. ddr_setval_ach(_reg_PI_SWLVL_EXIT, 0x01);
  2851. /* kick */
  2852. foreach_vch(ch) {
  2853. if (ch_have_this_cs[ddr_csn % 2] & (1U << ch)) {
  2854. ddr_setval(ch, reg_cs, ddr_csn);
  2855. ddr_setval(ch, reg_kick, 0x01);
  2856. }
  2857. }
  2858. foreach_vch(ch) {
  2859. /*PREPARE ADDR REGISTER (for SWLVL_OP_DONE) */
  2860. ddr_getval(ch, _reg_PI_SWLVL_OP_DONE);
  2861. }
  2862. waiting = ch_have_this_cs[ddr_csn % 2];
  2863. dsb_sev();
  2864. retry = RETRY_MAX;
  2865. do {
  2866. foreach_vch(ch) {
  2867. if (!(waiting & (1U << ch)))
  2868. continue;
  2869. data_l = ddr_getval(ch, _reg_PI_SWLVL_OP_DONE);
  2870. if (data_l & 0x01)
  2871. waiting &= ~(1U << ch);
  2872. }
  2873. retry--;
  2874. } while (waiting && (retry > 0));
  2875. if (retry == 0) {
  2876. err = 1;
  2877. }
  2878. dsb_sev();
  2879. /* set EXIT -> OP_DONE is cleared */
  2880. ddr_setval_ach(_reg_PI_SWLVL_EXIT, 0x01);
  2881. dsb_sev();
  2882. return err;
  2883. }
  2884. /* WDQ TRAINING */
  2885. #ifndef DDR_FAST_INIT
  2886. static void wdqdm_clr1(uint32_t ch, uint32_t ddr_csn)
  2887. {
  2888. int32_t i, k;
  2889. uint32_t cs, slice;
  2890. uint32_t data_l;
  2891. /* clr of training results buffer */
  2892. cs = ddr_csn % 2;
  2893. data_l = board_cnf->dqdm_dly_w;
  2894. for (slice = 0; slice < SLICE_CNT; slice++) {
  2895. k = (board_cnf->ch[ch].dqs_swap >> (4 * slice)) & 0x0f;
  2896. if (((k >= 2) && (ddr_csn < 2)) || ((k < 2) && (ddr_csn >= 2)))
  2897. continue;
  2898. for (i = 0; i <= 8; i++) {
  2899. if (ch_have_this_cs[CS_CNT - 1 - cs] & (1U << ch))
  2900. wdqdm_dly[ch][cs][slice][i] =
  2901. wdqdm_dly[ch][CS_CNT - 1 - cs][slice][i];
  2902. else
  2903. wdqdm_dly[ch][cs][slice][i] = data_l;
  2904. wdqdm_le[ch][cs][slice][i] = 0;
  2905. wdqdm_te[ch][cs][slice][i] = 0;
  2906. }
  2907. wdqdm_st[ch][cs][slice] = 0;
  2908. wdqdm_win[ch][cs][slice] = 0;
  2909. }
  2910. }
  2911. static uint32_t wdqdm_ana1(uint32_t ch, uint32_t ddr_csn)
  2912. {
  2913. int32_t i, k;
  2914. uint32_t cs, slice;
  2915. uint32_t data_l;
  2916. uint32_t err;
  2917. const uint32_t _par_WDQLVL_RETRY_THRES = 0x7c0;
  2918. int32_t min_win;
  2919. int32_t win;
  2920. int8_t _adj;
  2921. int16_t adj;
  2922. uint32_t dq;
  2923. /* analysis of training results */
  2924. err = 0;
  2925. for (slice = 0; slice < SLICE_CNT; slice += 1) {
  2926. k = (board_cnf->ch[ch].dqs_swap >> (4 * slice)) & 0x0f;
  2927. if (((k >= 2) && (ddr_csn < 2)) || ((k < 2) && (ddr_csn >= 2)))
  2928. continue;
  2929. cs = ddr_csn % 2;
  2930. ddr_setval_s(ch, slice, _reg_PHY_PER_CS_TRAINING_INDEX, cs);
  2931. for (i = 0; i < 9; i++) {
  2932. dq = slice * 8 + i;
  2933. if (i == 8)
  2934. _adj = board_cnf->ch[ch].dm_adj_w[slice];
  2935. else
  2936. _adj = board_cnf->ch[ch].dq_adj_w[dq];
  2937. adj = _f_scale_adj(_adj);
  2938. data_l =
  2939. ddr_getval_s(ch, slice,
  2940. _reg_PHY_CLK_WRX_SLAVE_DELAY[i]) + adj;
  2941. ddr_setval_s(ch, slice, _reg_PHY_CLK_WRX_SLAVE_DELAY[i],
  2942. data_l);
  2943. wdqdm_dly[ch][cs][slice][i] = data_l;
  2944. }
  2945. ddr_setval_s(ch, slice, _reg_PHY_PER_CS_TRAINING_EN, 0x00);
  2946. data_l = ddr_getval_s(ch, slice, _reg_PHY_WDQLVL_STATUS_OBS);
  2947. wdqdm_st[ch][cs][slice] = data_l;
  2948. min_win = INT_LEAST32_MAX;
  2949. for (i = 0; i <= 8; i++) {
  2950. ddr_setval_s(ch, slice, _reg_PHY_WDQLVL_DQDM_OBS_SELECT,
  2951. i);
  2952. data_l =
  2953. ddr_getval_s(ch, slice,
  2954. _reg_PHY_WDQLVL_DQDM_TE_DLY_OBS);
  2955. wdqdm_te[ch][cs][slice][i] = data_l;
  2956. data_l =
  2957. ddr_getval_s(ch, slice,
  2958. _reg_PHY_WDQLVL_DQDM_LE_DLY_OBS);
  2959. wdqdm_le[ch][cs][slice][i] = data_l;
  2960. win =
  2961. (int32_t)wdqdm_te[ch][cs][slice][i] -
  2962. wdqdm_le[ch][cs][slice][i];
  2963. if (min_win > win)
  2964. min_win = win;
  2965. if (data_l >= _par_WDQLVL_RETRY_THRES)
  2966. err = 2;
  2967. }
  2968. wdqdm_win[ch][cs][slice] = min_win;
  2969. if ((prr_product == PRR_PRODUCT_H3) &&
  2970. (prr_cut <= PRR_PRODUCT_11)) {
  2971. ddr_setval_s(ch, slice, _reg_PHY_PER_CS_TRAINING_EN,
  2972. 0x01);
  2973. } else {
  2974. ddr_setval_s(ch, slice, _reg_PHY_PER_CS_TRAINING_EN,
  2975. ((ch_have_this_cs[1]) >> ch) & 0x01);
  2976. }
  2977. }
  2978. return err;
  2979. }
  2980. #endif/* DDR_FAST_INIT */
  2981. static void wdqdm_cp(uint32_t ddr_csn, uint32_t restore)
  2982. {
  2983. uint32_t i;
  2984. uint32_t ch, slice;
  2985. uint32_t tgt_cs, src_cs;
  2986. uint32_t tmp_r;
  2987. /* copy of training results */
  2988. foreach_vch(ch) {
  2989. for (tgt_cs = 0; tgt_cs < CS_CNT; tgt_cs++) {
  2990. for (slice = 0; slice < SLICE_CNT; slice++) {
  2991. ddr_setval_s(ch, slice,
  2992. _reg_PHY_PER_CS_TRAINING_INDEX,
  2993. tgt_cs);
  2994. src_cs = ddr_csn % 2;
  2995. if (!(ch_have_this_cs[1] & (1U << ch)))
  2996. src_cs = 0;
  2997. for (i = 0; i <= 4; i += 4) {
  2998. if (restore)
  2999. tmp_r =
  3000. rdqdm_dly[ch][tgt_cs][slice]
  3001. [i];
  3002. else
  3003. tmp_r =
  3004. rdqdm_dly[ch][src_cs][slice]
  3005. [i];
  3006. ddr_setval_s(ch, slice,
  3007. _reg_PHY_RDDQS_X_RISE_SLAVE_DELAY
  3008. [i], tmp_r);
  3009. }
  3010. }
  3011. }
  3012. }
  3013. }
  3014. static uint32_t wdqdm_man1(void)
  3015. {
  3016. int32_t k;
  3017. uint32_t ch, cs, slice;
  3018. uint32_t ddr_csn;
  3019. uint32_t data_l;
  3020. uint32_t err;
  3021. uint32_t high_dq[DRAM_CH_CNT];
  3022. uint32_t mr14_csab0_bak[DRAM_CH_CNT];
  3023. #ifndef DDR_FAST_INIT
  3024. uint32_t err_flg;
  3025. #endif/* DDR_FAST_INIT */
  3026. /* manual execution of training */
  3027. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  3028. foreach_vch(ch) {
  3029. high_dq[ch] = 0;
  3030. for (slice = 0; slice < SLICE_CNT; slice++) {
  3031. k = (board_cnf->ch[ch].dqs_swap >>
  3032. (4 * slice)) & 0x0f;
  3033. if (k >= 2)
  3034. high_dq[ch] |= (1U << slice);
  3035. }
  3036. ddr_setval(ch, _reg_PI_16BIT_DRAM_CONNECT, 0x00);
  3037. }
  3038. }
  3039. err = 0;
  3040. /* CLEAR PREV RESULT */
  3041. for (cs = 0; cs < CS_CNT; cs++) {
  3042. ddr_setval_ach_as(_reg_PHY_PER_CS_TRAINING_INDEX, cs);
  3043. if (((prr_product == PRR_PRODUCT_H3) &&
  3044. (prr_cut > PRR_PRODUCT_11)) ||
  3045. (prr_product == PRR_PRODUCT_M3N) ||
  3046. (prr_product == PRR_PRODUCT_V3H)) {
  3047. ddr_setval_ach_as(_reg_SC_PHY_WDQLVL_CLR_PREV_RESULTS,
  3048. 0x01);
  3049. } else {
  3050. ddr_setval_ach_as(_reg_PHY_WDQLVL_CLR_PREV_RESULTS,
  3051. 0x01);
  3052. }
  3053. }
  3054. ddrphy_regif_idle();
  3055. #ifndef DDR_FAST_INIT
  3056. err_flg = 0;
  3057. #endif/* DDR_FAST_INIT */
  3058. for (ddr_csn = 0; ddr_csn < CSAB_CNT; ddr_csn++) {
  3059. if ((prr_product == PRR_PRODUCT_H3) &&
  3060. (prr_cut <= PRR_PRODUCT_11)) {
  3061. foreach_vch(ch) {
  3062. data_l = mmio_read_32(DBSC_DBDFICNT(ch));
  3063. data_l &= ~(0x00ffU << 16);
  3064. if (ddr_csn >= 2)
  3065. k = (high_dq[ch] ^ 0x0f);
  3066. else
  3067. k = high_dq[ch];
  3068. data_l |= (k << 16);
  3069. mmio_write_32(DBSC_DBDFICNT(ch), data_l);
  3070. ddr_setval(ch, _reg_PI_WDQLVL_RESP_MASK, k);
  3071. }
  3072. }
  3073. if (((prr_product == PRR_PRODUCT_H3) &&
  3074. (prr_cut <= PRR_PRODUCT_11)) ||
  3075. ((prr_product == PRR_PRODUCT_M3) &&
  3076. (prr_cut == PRR_PRODUCT_10))) {
  3077. wdqdm_cp(ddr_csn, 0);
  3078. }
  3079. foreach_vch(ch) {
  3080. data_l =
  3081. ddr_getval(ch,
  3082. reg_pi_mr14_data_fx_csx[1][ddr_csn]);
  3083. ddr_setval(ch, reg_pi_mr14_data_fx_csx[1][0], data_l);
  3084. }
  3085. /* KICK WDQLVL */
  3086. err = swlvl1(ddr_csn, _reg_PI_WDQLVL_CS, _reg_PI_WDQLVL_REQ);
  3087. if (err)
  3088. goto err_exit;
  3089. if (ddr_csn == 0)
  3090. foreach_vch(ch) {
  3091. mr14_csab0_bak[ch] =
  3092. ddr_getval(ch, reg_pi_mr14_data_fx_csx[1][0]);
  3093. } else
  3094. foreach_vch(ch) {
  3095. ddr_setval(ch, reg_pi_mr14_data_fx_csx[1][0],
  3096. mr14_csab0_bak[ch]);
  3097. }
  3098. #ifndef DDR_FAST_INIT
  3099. foreach_vch(ch) {
  3100. if (!(ch_have_this_cs[ddr_csn % 2] & (1U << ch))) {
  3101. wdqdm_clr1(ch, ddr_csn);
  3102. continue;
  3103. }
  3104. err = wdqdm_ana1(ch, ddr_csn);
  3105. if (err)
  3106. err_flg |= (1U << (ddr_csn * 4 + ch));
  3107. ddrphy_regif_idle();
  3108. }
  3109. #endif/* DDR_FAST_INIT */
  3110. }
  3111. err_exit:
  3112. #ifndef DDR_FAST_INIT
  3113. err |= err_flg;
  3114. #endif/* DDR_FAST_INIT */
  3115. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  3116. ddr_setval_ach(_reg_PI_16BIT_DRAM_CONNECT, 0x01);
  3117. foreach_vch(ch) {
  3118. data_l = mmio_read_32(DBSC_DBDFICNT(ch));
  3119. data_l &= ~(0x00ffU << 16);
  3120. mmio_write_32(DBSC_DBDFICNT(ch), data_l);
  3121. ddr_setval(ch, _reg_PI_WDQLVL_RESP_MASK, 0x00);
  3122. }
  3123. }
  3124. return err;
  3125. }
  3126. static uint32_t wdqdm_man(void)
  3127. {
  3128. uint32_t err, retry_cnt;
  3129. const uint32_t retry_max = 0x10;
  3130. uint32_t datal, ch, ddr_csn, mr14_bkup[4][4];
  3131. datal = RL + js2[js2_tdqsck] + (16 / 2) + 1 - WL + 2 + 2 + 19;
  3132. if ((mmio_read_32(DBSC_DBTR(11)) & 0xFF) > datal)
  3133. datal = mmio_read_32(DBSC_DBTR(11)) & 0xFF;
  3134. ddr_setval_ach(_reg_PI_TDFI_WDQLVL_RW, datal);
  3135. if (((prr_product == PRR_PRODUCT_H3) && (prr_cut > PRR_PRODUCT_11)) ||
  3136. (prr_product == PRR_PRODUCT_M3N) ||
  3137. (prr_product == PRR_PRODUCT_V3H)) {
  3138. ddr_setval_ach(_reg_PI_TDFI_WDQLVL_WR_F0,
  3139. (mmio_read_32(DBSC_DBTR(12)) & 0xFF) + 10);
  3140. ddr_setval_ach(_reg_PI_TDFI_WDQLVL_WR_F1,
  3141. (mmio_read_32(DBSC_DBTR(12)) & 0xFF) + 10);
  3142. } else {
  3143. ddr_setval_ach(_reg_PI_TDFI_WDQLVL_WR,
  3144. (mmio_read_32(DBSC_DBTR(12)) & 0xFF) + 10);
  3145. }
  3146. ddr_setval_ach(_reg_PI_TRFC_F0, mmio_read_32(DBSC_DBTR(13)) & 0x1FF);
  3147. ddr_setval_ach(_reg_PI_TRFC_F1, mmio_read_32(DBSC_DBTR(13)) & 0x1FF);
  3148. retry_cnt = 0;
  3149. err = 0;
  3150. do {
  3151. if ((prr_product == PRR_PRODUCT_H3) &&
  3152. (prr_cut <= PRR_PRODUCT_11)) {
  3153. err = wdqdm_man1();
  3154. } else {
  3155. ddr_setval_ach(_reg_PI_WDQLVL_VREF_EN, 0x01);
  3156. ddr_setval_ach(_reg_PI_WDQLVL_VREF_NORMAL_STEPSIZE,
  3157. 0x01);
  3158. if ((prr_product == PRR_PRODUCT_M3N) ||
  3159. (prr_product == PRR_PRODUCT_V3H)) {
  3160. ddr_setval_ach(_reg_PI_WDQLVL_VREF_DELTA_F1,
  3161. 0x0C);
  3162. } else {
  3163. ddr_setval_ach(_reg_PI_WDQLVL_VREF_DELTA, 0x0C);
  3164. }
  3165. dsb_sev();
  3166. err = wdqdm_man1();
  3167. foreach_vch(ch) {
  3168. for (ddr_csn = 0; ddr_csn < CSAB_CNT; ddr_csn++) {
  3169. mr14_bkup[ch][ddr_csn] =
  3170. ddr_getval(ch,
  3171. reg_pi_mr14_data_fx_csx
  3172. [1][ddr_csn]);
  3173. dsb_sev();
  3174. }
  3175. }
  3176. if ((prr_product == PRR_PRODUCT_M3N) ||
  3177. (prr_product == PRR_PRODUCT_V3H)) {
  3178. ddr_setval_ach(_reg_PI_WDQLVL_VREF_DELTA_F1,
  3179. 0x04);
  3180. } else {
  3181. ddr_setval_ach(_reg_PI_WDQLVL_VREF_DELTA, 0x04);
  3182. }
  3183. pvtcode_update();
  3184. err = wdqdm_man1();
  3185. foreach_vch(ch) {
  3186. for (ddr_csn = 0; ddr_csn < CSAB_CNT; ddr_csn++) {
  3187. mr14_bkup[ch][ddr_csn] =
  3188. (mr14_bkup[ch][ddr_csn] +
  3189. ddr_getval(ch,
  3190. reg_pi_mr14_data_fx_csx
  3191. [1][ddr_csn])) / 2;
  3192. ddr_setval(ch,
  3193. reg_pi_mr14_data_fx_csx[1]
  3194. [ddr_csn],
  3195. mr14_bkup[ch][ddr_csn]);
  3196. }
  3197. }
  3198. ddr_setval_ach(_reg_PI_WDQLVL_VREF_NORMAL_STEPSIZE,
  3199. 0x00);
  3200. if ((prr_product == PRR_PRODUCT_M3N) ||
  3201. (prr_product == PRR_PRODUCT_V3H)) {
  3202. ddr_setval_ach(_reg_PI_WDQLVL_VREF_DELTA_F1,
  3203. 0x00);
  3204. ddr_setval_ach
  3205. (_reg_PI_WDQLVL_VREF_INITIAL_START_POINT_F1,
  3206. 0x00);
  3207. ddr_setval_ach
  3208. (_reg_PI_WDQLVL_VREF_INITIAL_STOP_POINT_F1,
  3209. 0x00);
  3210. } else {
  3211. ddr_setval_ach(_reg_PI_WDQLVL_VREF_DELTA, 0x00);
  3212. ddr_setval_ach
  3213. (_reg_PI_WDQLVL_VREF_INITIAL_START_POINT,
  3214. 0x00);
  3215. ddr_setval_ach
  3216. (_reg_PI_WDQLVL_VREF_INITIAL_STOP_POINT,
  3217. 0x00);
  3218. }
  3219. ddr_setval_ach(_reg_PI_WDQLVL_VREF_INITIAL_STEPSIZE,
  3220. 0x00);
  3221. pvtcode_update2();
  3222. err = wdqdm_man1();
  3223. ddr_setval_ach(_reg_PI_WDQLVL_VREF_EN, 0x00);
  3224. }
  3225. } while (err && (++retry_cnt < retry_max));
  3226. if (((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) ||
  3227. ((prr_product == PRR_PRODUCT_M3) && (prr_cut <= PRR_PRODUCT_10))) {
  3228. wdqdm_cp(0, 1);
  3229. }
  3230. return (retry_cnt >= retry_max);
  3231. }
  3232. /* RDQ TRAINING */
  3233. #ifndef DDR_FAST_INIT
  3234. static void rdqdm_clr1(uint32_t ch, uint32_t ddr_csn)
  3235. {
  3236. int32_t i, k;
  3237. uint32_t cs, slice;
  3238. uint32_t data_l;
  3239. /* clr of training results buffer */
  3240. cs = ddr_csn % 2;
  3241. data_l = board_cnf->dqdm_dly_r;
  3242. for (slice = 0; slice < SLICE_CNT; slice++) {
  3243. k = (board_cnf->ch[ch].dqs_swap >> (4 * slice)) & 0x0f;
  3244. if (((k >= 2) && (ddr_csn < 2)) || ((k < 2) && (ddr_csn >= 2)))
  3245. continue;
  3246. for (i = 0; i <= 8; i++) {
  3247. if (ch_have_this_cs[CS_CNT - 1 - cs] & (1U << ch)) {
  3248. rdqdm_dly[ch][cs][slice][i] =
  3249. rdqdm_dly[ch][CS_CNT - 1 - cs][slice][i];
  3250. rdqdm_dly[ch][cs][slice + SLICE_CNT][i] =
  3251. rdqdm_dly[ch][CS_CNT - 1 - cs][slice +
  3252. SLICE_CNT]
  3253. [i];
  3254. } else {
  3255. rdqdm_dly[ch][cs][slice][i] = data_l;
  3256. rdqdm_dly[ch][cs][slice + SLICE_CNT][i] =
  3257. data_l;
  3258. }
  3259. rdqdm_le[ch][cs][slice][i] = 0;
  3260. rdqdm_le[ch][cs][slice + SLICE_CNT][i] = 0;
  3261. rdqdm_te[ch][cs][slice][i] = 0;
  3262. rdqdm_te[ch][cs][slice + SLICE_CNT][i] = 0;
  3263. rdqdm_nw[ch][cs][slice][i] = 0;
  3264. rdqdm_nw[ch][cs][slice + SLICE_CNT][i] = 0;
  3265. }
  3266. rdqdm_st[ch][cs][slice] = 0;
  3267. rdqdm_win[ch][cs][slice] = 0;
  3268. }
  3269. }
  3270. static uint32_t rdqdm_ana1(uint32_t ch, uint32_t ddr_csn)
  3271. {
  3272. int32_t i, k;
  3273. uint32_t cs, slice;
  3274. uint32_t data_l;
  3275. uint32_t err;
  3276. int8_t _adj;
  3277. int16_t adj;
  3278. uint32_t dq;
  3279. int32_t min_win;
  3280. int32_t win;
  3281. uint32_t rdq_status_obs_select;
  3282. /* analysis of training results */
  3283. err = 0;
  3284. for (slice = 0; slice < SLICE_CNT; slice++) {
  3285. k = (board_cnf->ch[ch].dqs_swap >> (4 * slice)) & 0x0f;
  3286. if (((k >= 2) && (ddr_csn < 2)) || ((k < 2) && (ddr_csn >= 2)))
  3287. continue;
  3288. cs = ddr_csn % 2;
  3289. ddr_setval_s(ch, slice, _reg_PHY_PER_CS_TRAINING_INDEX, cs);
  3290. ddrphy_regif_idle();
  3291. ddr_getval_s(ch, slice, _reg_PHY_PER_CS_TRAINING_INDEX);
  3292. ddrphy_regif_idle();
  3293. for (i = 0; i <= 8; i++) {
  3294. dq = slice * 8 + i;
  3295. if (i == 8)
  3296. _adj = board_cnf->ch[ch].dm_adj_r[slice];
  3297. else
  3298. _adj = board_cnf->ch[ch].dq_adj_r[dq];
  3299. adj = _f_scale_adj(_adj);
  3300. data_l =
  3301. ddr_getval_s(ch, slice,
  3302. _reg_PHY_RDDQS_X_RISE_SLAVE_DELAY[i]) +
  3303. adj;
  3304. ddr_setval_s(ch, slice,
  3305. _reg_PHY_RDDQS_X_RISE_SLAVE_DELAY[i],
  3306. data_l);
  3307. rdqdm_dly[ch][cs][slice][i] = data_l;
  3308. data_l =
  3309. ddr_getval_s(ch, slice,
  3310. _reg_PHY_RDDQS_X_FALL_SLAVE_DELAY[i]) +
  3311. adj;
  3312. ddr_setval_s(ch, slice,
  3313. _reg_PHY_RDDQS_X_FALL_SLAVE_DELAY[i],
  3314. data_l);
  3315. rdqdm_dly[ch][cs][slice + SLICE_CNT][i] = data_l;
  3316. }
  3317. min_win = INT_LEAST32_MAX;
  3318. for (i = 0; i <= 8; i++) {
  3319. data_l =
  3320. ddr_getval_s(ch, slice, _reg_PHY_RDLVL_STATUS_OBS);
  3321. rdqdm_st[ch][cs][slice] = data_l;
  3322. rdqdm_st[ch][cs][slice + SLICE_CNT] = data_l;
  3323. /* k : rise/fall */
  3324. for (k = 0; k < 2; k++) {
  3325. if (i == 8) {
  3326. rdq_status_obs_select = 16 + 8 * k;
  3327. } else {
  3328. rdq_status_obs_select = i + k * 8;
  3329. }
  3330. ddr_setval_s(ch, slice,
  3331. _reg_PHY_RDLVL_RDDQS_DQ_OBS_SELECT,
  3332. rdq_status_obs_select);
  3333. data_l =
  3334. ddr_getval_s(ch, slice,
  3335. _reg_PHY_RDLVL_RDDQS_DQ_LE_DLY_OBS);
  3336. rdqdm_le[ch][cs][slice + SLICE_CNT * k][i] =
  3337. data_l;
  3338. data_l =
  3339. ddr_getval_s(ch, slice,
  3340. _reg_PHY_RDLVL_RDDQS_DQ_TE_DLY_OBS);
  3341. rdqdm_te[ch][cs][slice + SLICE_CNT * k][i] =
  3342. data_l;
  3343. data_l =
  3344. ddr_getval_s(ch, slice,
  3345. _reg_PHY_RDLVL_RDDQS_DQ_NUM_WINDOWS_OBS);
  3346. rdqdm_nw[ch][cs][slice + SLICE_CNT * k][i] =
  3347. data_l;
  3348. win =
  3349. (int32_t)rdqdm_te[ch][cs][slice +
  3350. SLICE_CNT *
  3351. k][i] -
  3352. rdqdm_le[ch][cs][slice + SLICE_CNT * k][i];
  3353. if (i != 8) {
  3354. if (min_win > win)
  3355. min_win = win;
  3356. }
  3357. }
  3358. }
  3359. rdqdm_win[ch][cs][slice] = min_win;
  3360. if (min_win <= 0) {
  3361. err = 2;
  3362. }
  3363. }
  3364. return err;
  3365. }
  3366. #endif/* DDR_FAST_INIT */
  3367. static uint32_t rdqdm_man1(void)
  3368. {
  3369. uint32_t ch;
  3370. uint32_t ddr_csn;
  3371. #ifdef DDR_FAST_INIT
  3372. uint32_t slice;
  3373. uint32_t i, adj, data_l;
  3374. #endif/* DDR_FAST_INIT */
  3375. uint32_t err;
  3376. /* manual execution of training */
  3377. err = 0;
  3378. for (ddr_csn = 0; ddr_csn < CSAB_CNT; ddr_csn++) {
  3379. /* KICK RDQLVL */
  3380. err = swlvl1(ddr_csn, _reg_PI_RDLVL_CS, _reg_PI_RDLVL_REQ);
  3381. if (err)
  3382. goto err_exit;
  3383. #ifndef DDR_FAST_INIT
  3384. foreach_vch(ch) {
  3385. if (!(ch_have_this_cs[ddr_csn % 2] & (1U << ch))) {
  3386. rdqdm_clr1(ch, ddr_csn);
  3387. ddrphy_regif_idle();
  3388. continue;
  3389. }
  3390. err = rdqdm_ana1(ch, ddr_csn);
  3391. ddrphy_regif_idle();
  3392. if (err)
  3393. goto err_exit;
  3394. }
  3395. #else/* DDR_FAST_INIT */
  3396. foreach_vch(ch) {
  3397. if (ch_have_this_cs[ddr_csn] & (1U << ch)) {
  3398. for (slice = 0; slice < SLICE_CNT; slice++) {
  3399. if (ddr_getval_s(ch, slice,
  3400. _reg_PHY_RDLVL_STATUS_OBS) !=
  3401. 0x0D00FFFF) {
  3402. err = (1U << ch) |
  3403. (0x10U << slice);
  3404. goto err_exit;
  3405. }
  3406. }
  3407. }
  3408. if (((prr_product == PRR_PRODUCT_H3) &&
  3409. (prr_cut <= PRR_PRODUCT_11)) ||
  3410. ((prr_product == PRR_PRODUCT_M3) &&
  3411. (prr_cut <= PRR_PRODUCT_10))) {
  3412. for (slice = 0; slice < SLICE_CNT; slice++) {
  3413. for (i = 0; i <= 8; i++) {
  3414. if (i == 8)
  3415. adj = _f_scale_adj(board_cnf->ch[ch].dm_adj_r[slice]);
  3416. else
  3417. adj = _f_scale_adj(board_cnf->ch[ch].dq_adj_r[slice * 8 + i]);
  3418. ddr_setval_s(ch, slice, _reg_PHY_PER_CS_TRAINING_INDEX, ddr_csn);
  3419. data_l = ddr_getval_s(ch, slice, _reg_PHY_RDDQS_X_RISE_SLAVE_DELAY[i]) + adj;
  3420. ddr_setval_s(ch, slice, _reg_PHY_RDDQS_X_RISE_SLAVE_DELAY[i], data_l);
  3421. rdqdm_dly[ch][ddr_csn][slice][i] = data_l;
  3422. rdqdm_dly[ch][ddr_csn | 1][slice][i] = data_l;
  3423. data_l = ddr_getval_s(ch, slice, _reg_PHY_RDDQS_X_FALL_SLAVE_DELAY[i]) + adj;
  3424. ddr_setval_s(ch, slice, _reg_PHY_RDDQS_X_FALL_SLAVE_DELAY[i], data_l);
  3425. rdqdm_dly[ch][ddr_csn][slice + SLICE_CNT][i] = data_l;
  3426. rdqdm_dly[ch][ddr_csn | 1][slice + SLICE_CNT][i] = data_l;
  3427. }
  3428. }
  3429. }
  3430. }
  3431. ddrphy_regif_idle();
  3432. #endif/* DDR_FAST_INIT */
  3433. }
  3434. err_exit:
  3435. return err;
  3436. }
  3437. static uint32_t rdqdm_man(void)
  3438. {
  3439. uint32_t err, retry_cnt;
  3440. const uint32_t retry_max = 0x01;
  3441. ddr_setval_ach_as(_reg_PHY_DQ_TSEL_ENABLE,
  3442. 0x00000004 | ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET,
  3443. _reg_PHY_DQ_TSEL_ENABLE));
  3444. ddr_setval_ach_as(_reg_PHY_DQS_TSEL_ENABLE,
  3445. 0x00000004 | ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET,
  3446. _reg_PHY_DQS_TSEL_ENABLE));
  3447. ddr_setval_ach_as(_reg_PHY_DQ_TSEL_SELECT,
  3448. 0xFF0FFFFF & ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET,
  3449. _reg_PHY_DQ_TSEL_SELECT));
  3450. ddr_setval_ach_as(_reg_PHY_DQS_TSEL_SELECT,
  3451. 0xFF0FFFFF & ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET,
  3452. _reg_PHY_DQS_TSEL_SELECT));
  3453. retry_cnt = 0;
  3454. do {
  3455. err = rdqdm_man1();
  3456. ddrphy_regif_idle();
  3457. } while (err && (++retry_cnt < retry_max));
  3458. ddr_setval_ach_as(_reg_PHY_DQ_TSEL_ENABLE,
  3459. ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET,
  3460. _reg_PHY_DQ_TSEL_ENABLE));
  3461. ddr_setval_ach_as(_reg_PHY_DQS_TSEL_ENABLE,
  3462. ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET,
  3463. _reg_PHY_DQS_TSEL_ENABLE));
  3464. ddr_setval_ach_as(_reg_PHY_DQ_TSEL_SELECT,
  3465. ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET,
  3466. _reg_PHY_DQ_TSEL_SELECT));
  3467. ddr_setval_ach_as(_reg_PHY_DQS_TSEL_SELECT,
  3468. ddrtbl_getval(_cnf_DDR_PHY_SLICE_REGSET,
  3469. _reg_PHY_DQS_TSEL_SELECT));
  3470. return (retry_cnt >= retry_max);
  3471. }
  3472. /* rx offset calibration */
  3473. static int32_t _find_change(uint64_t val, uint32_t dir)
  3474. {
  3475. int32_t i;
  3476. uint32_t startval;
  3477. uint32_t curval;
  3478. const int32_t VAL_END = 0x3f;
  3479. if (dir == 0) {
  3480. startval = (val & 0x01);
  3481. for (i = 1; i <= VAL_END; i++) {
  3482. curval = (val >> i) & 0x01;
  3483. if (curval != startval)
  3484. return i;
  3485. }
  3486. return VAL_END;
  3487. }
  3488. startval = (val >> dir) & 0x01;
  3489. for (i = dir - 1; i >= 0; i--) {
  3490. curval = (val >> i) & 0x01;
  3491. if (curval != startval)
  3492. return i;
  3493. }
  3494. return 0;
  3495. }
  3496. static uint32_t _rx_offset_cal_updn(uint32_t code)
  3497. {
  3498. const uint32_t CODE_MAX = 0x40;
  3499. uint32_t tmp;
  3500. if ((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) {
  3501. if (code == 0)
  3502. tmp = (1U << 6) | (CODE_MAX - 1);
  3503. else if (code <= 0x20)
  3504. tmp =
  3505. ((CODE_MAX - 1 -
  3506. (0x20 - code) * 2) << 6) | (CODE_MAX - 1);
  3507. else
  3508. tmp =
  3509. ((CODE_MAX - 1) << 6) | (CODE_MAX - 1 -
  3510. (code - 0x20) * 2);
  3511. } else {
  3512. if (code == 0)
  3513. tmp = (1U << 6) | (CODE_MAX - 1);
  3514. else
  3515. tmp = (code << 6) | (CODE_MAX - code);
  3516. }
  3517. return tmp;
  3518. }
  3519. static uint32_t rx_offset_cal(void)
  3520. {
  3521. uint32_t index;
  3522. uint32_t code;
  3523. const uint32_t CODE_MAX = 0x40;
  3524. const uint32_t CODE_STEP = 2;
  3525. uint32_t ch, slice;
  3526. uint32_t tmp;
  3527. uint32_t tmp_ach_as[DRAM_CH_CNT][SLICE_CNT];
  3528. uint64_t val[DRAM_CH_CNT][SLICE_CNT][_reg_PHY_RX_CAL_X_NUM];
  3529. uint64_t tmpval;
  3530. int32_t lsb, msb;
  3531. ddr_setval_ach_as(_reg_PHY_RX_CAL_OVERRIDE, 0x01);
  3532. foreach_vch(ch) {
  3533. for (slice = 0; slice < SLICE_CNT; slice++) {
  3534. for (index = 0; index < _reg_PHY_RX_CAL_X_NUM; index++)
  3535. val[ch][slice][index] = 0;
  3536. }
  3537. }
  3538. for (code = 0; code < CODE_MAX / CODE_STEP; code++) {
  3539. tmp = _rx_offset_cal_updn(code * CODE_STEP);
  3540. for (index = 0; index < _reg_PHY_RX_CAL_X_NUM; index++) {
  3541. ddr_setval_ach_as(_reg_PHY_RX_CAL_X[index], tmp);
  3542. }
  3543. dsb_sev();
  3544. ddr_getval_ach_as(_reg_PHY_RX_CAL_OBS, (uint32_t *)tmp_ach_as);
  3545. foreach_vch(ch) {
  3546. for (slice = 0; slice < SLICE_CNT; slice++) {
  3547. tmp = tmp_ach_as[ch][slice];
  3548. for (index = 0; index < _reg_PHY_RX_CAL_X_NUM;
  3549. index++) {
  3550. if (tmp & (1U << index)) {
  3551. val[ch][slice][index] |=
  3552. (1ULL << code);
  3553. } else {
  3554. val[ch][slice][index] &=
  3555. ~(1ULL << code);
  3556. }
  3557. }
  3558. }
  3559. }
  3560. }
  3561. foreach_vch(ch) {
  3562. for (slice = 0; slice < SLICE_CNT; slice++) {
  3563. for (index = 0; index < _reg_PHY_RX_CAL_X_NUM;
  3564. index++) {
  3565. tmpval = val[ch][slice][index];
  3566. lsb = _find_change(tmpval, 0);
  3567. msb =
  3568. _find_change(tmpval,
  3569. (CODE_MAX / CODE_STEP) - 1);
  3570. tmp = (lsb + msb) >> 1;
  3571. tmp = _rx_offset_cal_updn(tmp * CODE_STEP);
  3572. ddr_setval_s(ch, slice,
  3573. _reg_PHY_RX_CAL_X[index], tmp);
  3574. }
  3575. }
  3576. }
  3577. ddr_setval_ach_as(_reg_PHY_RX_CAL_OVERRIDE, 0x00);
  3578. return 0;
  3579. }
  3580. static uint32_t rx_offset_cal_hw(void)
  3581. {
  3582. uint32_t ch, slice;
  3583. uint32_t retry;
  3584. uint32_t complete;
  3585. uint32_t tmp;
  3586. uint32_t tmp_ach_as[DRAM_CH_CNT][SLICE_CNT];
  3587. ddr_setval_ach_as(_reg_PHY_RX_CAL_X[9], 0x00);
  3588. ddr_setval_ach_as(_reg_PHY_RX_CAL_OVERRIDE, 0x00);
  3589. ddr_setval_ach_as(_reg_PHY_RX_CAL_SAMPLE_WAIT, 0x0f);
  3590. retry = 0;
  3591. while (retry < 4096) {
  3592. if ((retry & 0xff) == 0) {
  3593. ddr_setval_ach_as(_reg_SC_PHY_RX_CAL_START, 0x01);
  3594. }
  3595. foreach_vch(ch)
  3596. for (slice = 0; slice < SLICE_CNT; slice++)
  3597. tmp_ach_as[ch][slice] =
  3598. ddr_getval_s(ch, slice, _reg_PHY_RX_CAL_X[9]);
  3599. complete = 1;
  3600. foreach_vch(ch) {
  3601. for (slice = 0; slice < SLICE_CNT; slice++) {
  3602. tmp = tmp_ach_as[ch][slice];
  3603. tmp = (tmp & 0x3f) + ((tmp >> 6) & 0x3f);
  3604. if (((prr_product == PRR_PRODUCT_H3) &&
  3605. (prr_cut > PRR_PRODUCT_11)) ||
  3606. (prr_product == PRR_PRODUCT_M3N) ||
  3607. (prr_product == PRR_PRODUCT_V3H)) {
  3608. if (tmp != 0x3E)
  3609. complete = 0;
  3610. } else {
  3611. if (tmp != 0x40)
  3612. complete = 0;
  3613. }
  3614. }
  3615. }
  3616. if (complete)
  3617. break;
  3618. retry++;
  3619. }
  3620. return (complete == 0);
  3621. }
  3622. /* adjust rddqs latency */
  3623. static void adjust_rddqs_latency(void)
  3624. {
  3625. uint32_t ch, slice;
  3626. uint32_t dly;
  3627. uint32_t maxlatx2;
  3628. uint32_t tmp;
  3629. uint32_t rdlat_adjx2[SLICE_CNT];
  3630. foreach_vch(ch) {
  3631. maxlatx2 = 0;
  3632. for (slice = 0; slice < SLICE_CNT; slice++) {
  3633. ddr_setval_s(ch, slice, _reg_PHY_PER_CS_TRAINING_INDEX,
  3634. 0x00);
  3635. dly =
  3636. ddr_getval_s(ch, slice,
  3637. _reg_PHY_RDDQS_GATE_SLAVE_DELAY);
  3638. tmp =
  3639. ddr_getval_s(ch, slice,
  3640. _reg_PHY_RDDQS_LATENCY_ADJUST);
  3641. /* note gate_slave_delay[9] is always 0 */
  3642. tmp = (tmp << 1) + (dly >> 8);
  3643. rdlat_adjx2[slice] = tmp;
  3644. if (maxlatx2 < tmp)
  3645. maxlatx2 = tmp;
  3646. }
  3647. maxlatx2 = ((maxlatx2 + 1) >> 1) << 1;
  3648. for (slice = 0; slice < SLICE_CNT; slice++) {
  3649. tmp = maxlatx2 - rdlat_adjx2[slice];
  3650. tmp = (tmp >> 1);
  3651. if (tmp) {
  3652. ddr_setval_s(ch, slice, _reg_PHY_RPTR_UPDATE,
  3653. ddr_getval_s(ch, slice,
  3654. _reg_PHY_RPTR_UPDATE)
  3655. + 1);
  3656. }
  3657. }
  3658. }
  3659. }
  3660. /* adjust wpath latency */
  3661. static void adjust_wpath_latency(void)
  3662. {
  3663. uint32_t ch, cs, slice;
  3664. uint32_t dly;
  3665. uint32_t wpath_add;
  3666. const uint32_t _par_EARLY_THRESHOLD_VAL = 0x180;
  3667. foreach_vch(ch) {
  3668. for (slice = 0; slice < SLICE_CNT; slice += 1) {
  3669. for (cs = 0; cs < CS_CNT; cs++) {
  3670. ddr_setval_s(ch, slice,
  3671. _reg_PHY_PER_CS_TRAINING_INDEX,
  3672. cs);
  3673. ddr_getval_s(ch, slice,
  3674. _reg_PHY_PER_CS_TRAINING_INDEX);
  3675. dly =
  3676. ddr_getval_s(ch, slice,
  3677. _reg_PHY_CLK_WRDQS_SLAVE_DELAY);
  3678. if (dly <= _par_EARLY_THRESHOLD_VAL)
  3679. continue;
  3680. wpath_add =
  3681. ddr_getval_s(ch, slice,
  3682. _reg_PHY_WRITE_PATH_LAT_ADD);
  3683. ddr_setval_s(ch, slice,
  3684. _reg_PHY_WRITE_PATH_LAT_ADD,
  3685. wpath_add - 1);
  3686. }
  3687. }
  3688. }
  3689. }
  3690. /* DDR Initialize entry */
  3691. int32_t rcar_dram_init(void)
  3692. {
  3693. uint32_t ch, cs;
  3694. uint32_t data_l;
  3695. uint32_t bus_mbps, bus_mbpsdiv;
  3696. uint32_t tmp_tccd;
  3697. uint32_t failcount;
  3698. uint32_t cnf_boardtype;
  3699. /* Thermal sensor setting */
  3700. data_l = mmio_read_32(CPG_MSTPSR5);
  3701. if (data_l & BIT(22)) { /* case THS/TSC Standby */
  3702. data_l &= ~BIT(22);
  3703. cpg_write_32(CPG_SMSTPCR5, data_l);
  3704. while (mmio_read_32(CPG_MSTPSR5) & BIT(22))
  3705. ; /* wait bit=0 */
  3706. }
  3707. /* THCTR Bit6: PONM=0 , Bit0: THSST=0 */
  3708. data_l = mmio_read_32(THS1_THCTR);
  3709. if (data_l & 0x00000040U) {
  3710. data_l = data_l & 0xFFFFFFBEU;
  3711. } else {
  3712. data_l = data_l | BIT(1);
  3713. }
  3714. mmio_write_32(THS1_THCTR, data_l);
  3715. /* Judge product and cut */
  3716. #ifdef RCAR_DDR_FIXED_LSI_TYPE
  3717. #if (RCAR_LSI == RCAR_AUTO)
  3718. prr_product = mmio_read_32(PRR) & PRR_PRODUCT_MASK;
  3719. prr_cut = mmio_read_32(PRR) & PRR_CUT_MASK;
  3720. #else /* RCAR_LSI */
  3721. #ifndef RCAR_LSI_CUT
  3722. prr_cut = mmio_read_32(PRR) & PRR_CUT_MASK;
  3723. #endif /* RCAR_LSI_CUT */
  3724. #endif /* RCAR_LSI */
  3725. #else /* RCAR_DDR_FIXED_LSI_TYPE */
  3726. prr_product = mmio_read_32(PRR) & PRR_PRODUCT_MASK;
  3727. prr_cut = mmio_read_32(PRR) & PRR_CUT_MASK;
  3728. #endif /* RCAR_DDR_FIXED_LSI_TYPE */
  3729. if (prr_product == PRR_PRODUCT_H3) {
  3730. if (prr_cut <= PRR_PRODUCT_11) {
  3731. p_ddr_regdef_tbl =
  3732. (const uint32_t *)&DDR_REGDEF_TBL[0][0];
  3733. } else {
  3734. p_ddr_regdef_tbl =
  3735. (const uint32_t *)&DDR_REGDEF_TBL[2][0];
  3736. }
  3737. } else if (prr_product == PRR_PRODUCT_M3) {
  3738. p_ddr_regdef_tbl =
  3739. (const uint32_t *)&DDR_REGDEF_TBL[1][0];
  3740. } else if ((prr_product == PRR_PRODUCT_M3N) ||
  3741. (prr_product == PRR_PRODUCT_V3H)) {
  3742. p_ddr_regdef_tbl =
  3743. (const uint32_t *)&DDR_REGDEF_TBL[3][0];
  3744. } else {
  3745. FATAL_MSG("BL2: DDR:Unknown Product\n");
  3746. return 0xff;
  3747. }
  3748. if (((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) ||
  3749. ((prr_product == PRR_PRODUCT_M3) && (prr_cut < PRR_PRODUCT_30))) {
  3750. /* non : H3 Ver.1.x/M3-W Ver.1.x not support */
  3751. } else {
  3752. mmio_write_32(DBSC_DBSYSCNT0, 0x00001234);
  3753. }
  3754. /* Judge board type */
  3755. cnf_boardtype = boardcnf_get_brd_type();
  3756. if (cnf_boardtype >= BOARDNUM) {
  3757. FATAL_MSG("BL2: DDR:Unknown Board\n");
  3758. return 0xff;
  3759. }
  3760. board_cnf = (const struct _boardcnf *)&boardcnfs[cnf_boardtype];
  3761. /* RCAR_DRAM_SPLIT_2CH (2U) */
  3762. #if RCAR_DRAM_SPLIT == 2
  3763. /* H3(Test for future H3-N): Swap ch2 and ch1 for 2ch-split */
  3764. if ((prr_product == PRR_PRODUCT_H3) && (board_cnf->phyvalid == 0x05)) {
  3765. mmio_write_32(DBSC_DBMEMSWAPCONF0, 0x00000006);
  3766. ddr_phyvalid = 0x03;
  3767. } else {
  3768. ddr_phyvalid = board_cnf->phyvalid;
  3769. }
  3770. #else /* RCAR_DRAM_SPLIT_2CH */
  3771. ddr_phyvalid = board_cnf->phyvalid;
  3772. #endif /* RCAR_DRAM_SPLIT_2CH */
  3773. max_density = 0;
  3774. for (cs = 0; cs < CS_CNT; cs++) {
  3775. ch_have_this_cs[cs] = 0;
  3776. }
  3777. foreach_ech(ch)
  3778. for (cs = 0; cs < CS_CNT; cs++)
  3779. ddr_density[ch][cs] = 0xff;
  3780. foreach_vch(ch) {
  3781. for (cs = 0; cs < CS_CNT; cs++) {
  3782. data_l = board_cnf->ch[ch].ddr_density[cs];
  3783. ddr_density[ch][cs] = data_l;
  3784. if (data_l == 0xff)
  3785. continue;
  3786. if (data_l > max_density)
  3787. max_density = data_l;
  3788. if ((cs == 1) && (prr_product == PRR_PRODUCT_H3) &&
  3789. (prr_cut <= PRR_PRODUCT_11))
  3790. continue;
  3791. ch_have_this_cs[cs] |= (1U << ch);
  3792. }
  3793. }
  3794. /* Judge board clock frequency (in MHz) */
  3795. boardcnf_get_brd_clk(cnf_boardtype, &brd_clk, &brd_clkdiv);
  3796. if ((brd_clk / brd_clkdiv) > 25) {
  3797. brd_clkdiva = 1;
  3798. } else {
  3799. brd_clkdiva = 0;
  3800. }
  3801. /* Judge ddr operating frequency clock(in Mbps) */
  3802. boardcnf_get_ddr_mbps(cnf_boardtype, &ddr_mbps, &ddr_mbpsdiv);
  3803. ddr0800_mul = CLK_DIV(800, 2, brd_clk, brd_clkdiv * (brd_clkdiva + 1));
  3804. ddr_mul = CLK_DIV(ddr_mbps, ddr_mbpsdiv * 2, brd_clk,
  3805. brd_clkdiv * (brd_clkdiva + 1));
  3806. /* Adjust tccd */
  3807. data_l = (0x00006000 & mmio_read_32(RST_MODEMR)) >> 13;
  3808. bus_mbps = 0;
  3809. bus_mbpsdiv = 0;
  3810. switch (data_l) {
  3811. case 0:
  3812. bus_mbps = brd_clk * 0x60 * 2;
  3813. bus_mbpsdiv = brd_clkdiv * 1;
  3814. break;
  3815. case 1:
  3816. bus_mbps = brd_clk * 0x50 * 2;
  3817. bus_mbpsdiv = brd_clkdiv * 1;
  3818. break;
  3819. case 2:
  3820. bus_mbps = brd_clk * 0x40 * 2;
  3821. bus_mbpsdiv = brd_clkdiv * 1;
  3822. break;
  3823. case 3:
  3824. bus_mbps = brd_clk * 0x60 * 2;
  3825. bus_mbpsdiv = brd_clkdiv * 2;
  3826. break;
  3827. default:
  3828. bus_mbps = brd_clk * 0x60 * 2;
  3829. bus_mbpsdiv = brd_clkdiv * 2;
  3830. break;
  3831. }
  3832. tmp_tccd = CLK_DIV(ddr_mbps * 8, ddr_mbpsdiv, bus_mbps, bus_mbpsdiv);
  3833. if (8 * ddr_mbps * bus_mbpsdiv != tmp_tccd * bus_mbps * ddr_mbpsdiv)
  3834. tmp_tccd = tmp_tccd + 1;
  3835. if (tmp_tccd < 8)
  3836. ddr_tccd = 8;
  3837. else
  3838. ddr_tccd = tmp_tccd;
  3839. NOTICE("BL2: DDR%d(%s)\n", ddr_mbps / ddr_mbpsdiv, RCAR_DDR_VERSION);
  3840. MSG_LF("Start\n");
  3841. /* PLL Setting */
  3842. pll3_control(1);
  3843. /* initialize DDR */
  3844. data_l = init_ddr();
  3845. if (data_l == ddr_phyvalid) {
  3846. failcount = 0;
  3847. } else {
  3848. failcount = 1;
  3849. }
  3850. foreach_vch(ch)
  3851. mmio_write_32(DBSC_DBPDLK(ch), 0x00000000);
  3852. if (((prr_product == PRR_PRODUCT_H3) && (prr_cut <= PRR_PRODUCT_11)) ||
  3853. ((prr_product == PRR_PRODUCT_M3) && (prr_cut < PRR_PRODUCT_30))) {
  3854. /* non : H3 Ver.1.x/M3-W Ver.1.x not support */
  3855. } else {
  3856. mmio_write_32(DBSC_DBSYSCNT0, 0x00000000);
  3857. }
  3858. if (failcount == 0) {
  3859. return INITDRAM_OK;
  3860. } else {
  3861. return INITDRAM_NG;
  3862. }
  3863. }
  3864. void pvtcode_update(void)
  3865. {
  3866. uint32_t ch;
  3867. uint32_t data_l;
  3868. uint32_t pvtp[4], pvtn[4], pvtp_init, pvtn_init;
  3869. int32_t pvtp_tmp, pvtn_tmp;
  3870. foreach_vch(ch) {
  3871. pvtn_init = (tcal.tcomp_cal[ch] & 0xFC0) >> 6;
  3872. pvtp_init = (tcal.tcomp_cal[ch] & 0x03F) >> 0;
  3873. if (8912 * pvtp_init > 44230) {
  3874. pvtp_tmp = (5000 + 8912 * pvtp_init - 44230) / 10000;
  3875. } else {
  3876. pvtp_tmp =
  3877. -((-(5000 + 8912 * pvtp_init - 44230)) / 10000);
  3878. }
  3879. pvtn_tmp = (5000 + 5776 * pvtn_init + 30280) / 10000;
  3880. pvtn[ch] = pvtn_tmp + pvtn_init;
  3881. pvtp[ch] = pvtp_tmp + pvtp_init;
  3882. if (pvtn[ch] > 63) {
  3883. pvtn[ch] = 63;
  3884. pvtp[ch] =
  3885. (pvtp_tmp) * (63 - 6 * pvtn_tmp -
  3886. pvtn_init) / (pvtn_tmp) +
  3887. 6 * pvtp_tmp + pvtp_init;
  3888. }
  3889. if ((prr_product == PRR_PRODUCT_H3) &&
  3890. (prr_cut <= PRR_PRODUCT_11)) {
  3891. data_l = pvtp[ch] | (pvtn[ch] << 6) |
  3892. (tcal.tcomp_cal[ch] & 0xfffff000);
  3893. reg_ddrphy_write(ch,
  3894. ddr_regdef_adr(_reg_PHY_PAD_FDBK_TERM),
  3895. data_l | 0x00020000);
  3896. reg_ddrphy_write(ch,
  3897. ddr_regdef_adr(_reg_PHY_PAD_DATA_TERM),
  3898. data_l);
  3899. reg_ddrphy_write(ch,
  3900. ddr_regdef_adr(_reg_PHY_PAD_DQS_TERM),
  3901. data_l);
  3902. reg_ddrphy_write(ch,
  3903. ddr_regdef_adr(_reg_PHY_PAD_ADDR_TERM),
  3904. data_l);
  3905. reg_ddrphy_write(ch,
  3906. ddr_regdef_adr(_reg_PHY_PAD_CS_TERM),
  3907. data_l);
  3908. } else {
  3909. data_l = pvtp[ch] | (pvtn[ch] << 6) | 0x00015000;
  3910. reg_ddrphy_write(ch,
  3911. ddr_regdef_adr(_reg_PHY_PAD_FDBK_TERM),
  3912. data_l | 0x00020000);
  3913. reg_ddrphy_write(ch,
  3914. ddr_regdef_adr(_reg_PHY_PAD_DATA_TERM),
  3915. data_l);
  3916. reg_ddrphy_write(ch,
  3917. ddr_regdef_adr(_reg_PHY_PAD_DQS_TERM),
  3918. data_l);
  3919. reg_ddrphy_write(ch,
  3920. ddr_regdef_adr(_reg_PHY_PAD_ADDR_TERM),
  3921. data_l);
  3922. reg_ddrphy_write(ch,
  3923. ddr_regdef_adr(_reg_PHY_PAD_CS_TERM),
  3924. data_l);
  3925. }
  3926. }
  3927. }
  3928. void pvtcode_update2(void)
  3929. {
  3930. uint32_t ch;
  3931. foreach_vch(ch) {
  3932. reg_ddrphy_write(ch, ddr_regdef_adr(_reg_PHY_PAD_FDBK_TERM),
  3933. tcal.init_cal[ch] | 0x00020000);
  3934. reg_ddrphy_write(ch, ddr_regdef_adr(_reg_PHY_PAD_DATA_TERM),
  3935. tcal.init_cal[ch]);
  3936. reg_ddrphy_write(ch, ddr_regdef_adr(_reg_PHY_PAD_DQS_TERM),
  3937. tcal.init_cal[ch]);
  3938. reg_ddrphy_write(ch, ddr_regdef_adr(_reg_PHY_PAD_ADDR_TERM),
  3939. tcal.init_cal[ch]);
  3940. reg_ddrphy_write(ch, ddr_regdef_adr(_reg_PHY_PAD_CS_TERM),
  3941. tcal.init_cal[ch]);
  3942. }
  3943. }
  3944. void ddr_padcal_tcompensate_getinit(uint32_t override)
  3945. {
  3946. uint32_t ch;
  3947. uint32_t data_l;
  3948. uint32_t pvtp, pvtn;
  3949. tcal.init_temp = 0;
  3950. for (ch = 0; ch < 4; ch++) {
  3951. tcal.init_cal[ch] = 0;
  3952. tcal.tcomp_cal[ch] = 0;
  3953. }
  3954. foreach_vch(ch) {
  3955. tcal.init_cal[ch] = ddr_getval(ch, _reg_PHY_PAD_TERM_X[1]);
  3956. tcal.tcomp_cal[ch] = ddr_getval(ch, _reg_PHY_PAD_TERM_X[1]);
  3957. }
  3958. if (!override) {
  3959. data_l = mmio_read_32(THS1_TEMP);
  3960. if (data_l < 2800) {
  3961. tcal.init_temp =
  3962. (143 * (int32_t)data_l - 359000) / 1000;
  3963. } else {
  3964. tcal.init_temp =
  3965. (121 * (int32_t)data_l - 296300) / 1000;
  3966. }
  3967. foreach_vch(ch) {
  3968. pvtp = (tcal.init_cal[ch] >> 0) & 0x000003F;
  3969. pvtn = (tcal.init_cal[ch] >> 6) & 0x000003F;
  3970. if ((int32_t)pvtp >
  3971. ((tcal.init_temp * 29 - 3625) / 1000))
  3972. pvtp =
  3973. (int32_t)pvtp +
  3974. ((3625 - tcal.init_temp * 29) / 1000);
  3975. else
  3976. pvtp = 0;
  3977. if ((int32_t)pvtn >
  3978. ((tcal.init_temp * 54 - 6750) / 1000))
  3979. pvtn =
  3980. (int32_t)pvtn +
  3981. ((6750 - tcal.init_temp * 54) / 1000);
  3982. else
  3983. pvtn = 0;
  3984. if ((prr_product == PRR_PRODUCT_H3) &&
  3985. (prr_cut <= PRR_PRODUCT_11)) {
  3986. tcal.init_cal[ch] =
  3987. (tcal.init_cal[ch] & 0xfffff000) |
  3988. (pvtn << 6) |
  3989. pvtp;
  3990. } else {
  3991. tcal.init_cal[ch] =
  3992. 0x00015000 | (pvtn << 6) | pvtp;
  3993. }
  3994. }
  3995. tcal.init_temp = 125;
  3996. }
  3997. }
  3998. #ifndef ddr_qos_init_setting
  3999. /* For QoS init */
  4000. uint8_t get_boardcnf_phyvalid(void)
  4001. {
  4002. return ddr_phyvalid;
  4003. }
  4004. #endif /* ddr_qos_init_setting */