sequencer.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016
  1. /*
  2. * Copyright Altera Corporation (C) 2012-2015
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <asm/arch/sdram.h>
  9. #include "sequencer.h"
  10. #include "sequencer_auto.h"
  11. #include "sequencer_auto_ac_init.h"
  12. #include "sequencer_auto_inst_init.h"
  13. #include "sequencer_defines.h"
  14. static void scc_mgr_load_dqs_for_write_group(uint32_t write_group);
  15. static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
  16. (struct socfpga_sdr_rw_load_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
  17. static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
  18. (struct socfpga_sdr_rw_load_jump_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
  19. static struct socfpga_sdr_reg_file *sdr_reg_file =
  20. (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
  21. static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
  22. (struct socfpga_sdr_scc_mgr *)(SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
  23. static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
  24. (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
  25. static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
  26. (struct socfpga_phy_mgr_cfg *)(SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
  27. static struct socfpga_data_mgr *data_mgr =
  28. (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
  29. #define DELTA_D 1
  30. /*
  31. * In order to reduce ROM size, most of the selectable calibration steps are
  32. * decided at compile time based on the user's calibration mode selection,
  33. * as captured by the STATIC_CALIB_STEPS selection below.
  34. *
  35. * However, to support simulation-time selection of fast simulation mode, where
  36. * we skip everything except the bare minimum, we need a few of the steps to
  37. * be dynamic. In those cases, we either use the DYNAMIC_CALIB_STEPS for the
  38. * check, which is based on the rtl-supplied value, or we dynamically compute
  39. * the value to use based on the dynamically-chosen calibration mode
  40. */
  41. #define DLEVEL 0
  42. #define STATIC_IN_RTL_SIM 0
  43. #define STATIC_SKIP_DELAY_LOOPS 0
  44. #define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
  45. STATIC_SKIP_DELAY_LOOPS)
  46. /* calibration steps requested by the rtl */
  47. uint16_t dyn_calib_steps;
  48. /*
  49. * To make CALIB_SKIP_DELAY_LOOPS a dynamic conditional option
  50. * instead of static, we use boolean logic to select between
  51. * non-skip and skip values
  52. *
  53. * The mask is set to include all bits when not-skipping, but is
  54. * zero when skipping
  55. */
  56. uint16_t skip_delay_mask; /* mask off bits when skipping/not-skipping */
  57. #define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
  58. ((non_skip_value) & skip_delay_mask)
  59. struct gbl_type *gbl;
  60. struct param_type *param;
  61. uint32_t curr_shadow_reg;
  62. static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
  63. uint32_t write_group, uint32_t use_dm,
  64. uint32_t all_correct, uint32_t *bit_chk, uint32_t all_ranks);
  65. static void set_failing_group_stage(uint32_t group, uint32_t stage,
  66. uint32_t substage)
  67. {
  68. /*
  69. * Only set the global stage if there was not been any other
  70. * failing group
  71. */
  72. if (gbl->error_stage == CAL_STAGE_NIL) {
  73. gbl->error_substage = substage;
  74. gbl->error_stage = stage;
  75. gbl->error_group = group;
  76. }
  77. }
  78. static void reg_file_set_group(uint32_t set_group)
  79. {
  80. u32 addr = (u32)&sdr_reg_file->cur_stage;
  81. /* Read the current group and stage */
  82. uint32_t cur_stage_group = readl(SOCFPGA_SDR_ADDRESS + addr);
  83. /* Clear the group */
  84. cur_stage_group &= 0x0000FFFF;
  85. /* Set the group */
  86. cur_stage_group |= (set_group << 16);
  87. /* Write the data back */
  88. writel(cur_stage_group, SOCFPGA_SDR_ADDRESS + addr);
  89. }
  90. static void reg_file_set_stage(uint32_t set_stage)
  91. {
  92. u32 addr = (u32)&sdr_reg_file->cur_stage;
  93. /* Read the current group and stage */
  94. uint32_t cur_stage_group = readl(SOCFPGA_SDR_ADDRESS + addr);
  95. /* Clear the stage and substage */
  96. cur_stage_group &= 0xFFFF0000;
  97. /* Set the stage */
  98. cur_stage_group |= (set_stage & 0x000000FF);
  99. /* Write the data back */
  100. writel(cur_stage_group, SOCFPGA_SDR_ADDRESS + addr);
  101. }
  102. static void reg_file_set_sub_stage(uint32_t set_sub_stage)
  103. {
  104. u32 addr = (u32)&sdr_reg_file->cur_stage;
  105. /* Read the current group and stage */
  106. uint32_t cur_stage_group = readl(SOCFPGA_SDR_ADDRESS + addr);
  107. /* Clear the substage */
  108. cur_stage_group &= 0xFFFF00FF;
  109. /* Set the sub stage */
  110. cur_stage_group |= ((set_sub_stage << 8) & 0x0000FF00);
  111. /* Write the data back */
  112. writel(cur_stage_group, SOCFPGA_SDR_ADDRESS + addr);
  113. }
  114. static void initialize(void)
  115. {
  116. u32 addr = (u32)&phy_mgr_cfg->mux_sel;
  117. debug("%s:%d\n", __func__, __LINE__);
  118. /* USER calibration has control over path to memory */
  119. /*
  120. * In Hard PHY this is a 2-bit control:
  121. * 0: AFI Mux Select
  122. * 1: DDIO Mux Select
  123. */
  124. writel(0x3, SOCFPGA_SDR_ADDRESS + addr);
  125. /* USER memory clock is not stable we begin initialization */
  126. addr = (u32)&phy_mgr_cfg->reset_mem_stbl;
  127. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  128. /* USER calibration status all set to zero */
  129. addr = (u32)&phy_mgr_cfg->cal_status;
  130. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  131. addr = (u32)&phy_mgr_cfg->cal_debug_info;
  132. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  133. if ((dyn_calib_steps & CALIB_SKIP_ALL) != CALIB_SKIP_ALL) {
  134. param->read_correct_mask_vg = ((uint32_t)1 <<
  135. (RW_MGR_MEM_DQ_PER_READ_DQS /
  136. RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS)) - 1;
  137. param->write_correct_mask_vg = ((uint32_t)1 <<
  138. (RW_MGR_MEM_DQ_PER_READ_DQS /
  139. RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS)) - 1;
  140. param->read_correct_mask = ((uint32_t)1 <<
  141. RW_MGR_MEM_DQ_PER_READ_DQS) - 1;
  142. param->write_correct_mask = ((uint32_t)1 <<
  143. RW_MGR_MEM_DQ_PER_WRITE_DQS) - 1;
  144. param->dm_correct_mask = ((uint32_t)1 <<
  145. (RW_MGR_MEM_DATA_WIDTH / RW_MGR_MEM_DATA_MASK_WIDTH))
  146. - 1;
  147. }
  148. }
  149. static void set_rank_and_odt_mask(uint32_t rank, uint32_t odt_mode)
  150. {
  151. uint32_t odt_mask_0 = 0;
  152. uint32_t odt_mask_1 = 0;
  153. uint32_t cs_and_odt_mask;
  154. uint32_t addr;
  155. if (odt_mode == RW_MGR_ODT_MODE_READ_WRITE) {
  156. if (RW_MGR_MEM_NUMBER_OF_RANKS == 1) {
  157. /*
  158. * 1 Rank
  159. * Read: ODT = 0
  160. * Write: ODT = 1
  161. */
  162. odt_mask_0 = 0x0;
  163. odt_mask_1 = 0x1;
  164. } else if (RW_MGR_MEM_NUMBER_OF_RANKS == 2) {
  165. /* 2 Ranks */
  166. if (RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM == 1) {
  167. /* - Dual-Slot , Single-Rank
  168. * (1 chip-select per DIMM)
  169. * OR
  170. * - RDIMM, 4 total CS (2 CS per DIMM)
  171. * means 2 DIMM
  172. * Since MEM_NUMBER_OF_RANKS is 2 they are
  173. * both single rank
  174. * with 2 CS each (special for RDIMM)
  175. * Read: Turn on ODT on the opposite rank
  176. * Write: Turn on ODT on all ranks
  177. */
  178. odt_mask_0 = 0x3 & ~(1 << rank);
  179. odt_mask_1 = 0x3;
  180. } else {
  181. /*
  182. * USER - Single-Slot , Dual-rank DIMMs
  183. * (2 chip-selects per DIMM)
  184. * USER Read: Turn on ODT off on all ranks
  185. * USER Write: Turn on ODT on active rank
  186. */
  187. odt_mask_0 = 0x0;
  188. odt_mask_1 = 0x3 & (1 << rank);
  189. }
  190. } else {
  191. /* 4 Ranks
  192. * Read:
  193. * ----------+-----------------------+
  194. * | |
  195. * | ODT |
  196. * Read From +-----------------------+
  197. * Rank | 3 | 2 | 1 | 0 |
  198. * ----------+-----+-----+-----+-----+
  199. * 0 | 0 | 1 | 0 | 0 |
  200. * 1 | 1 | 0 | 0 | 0 |
  201. * 2 | 0 | 0 | 0 | 1 |
  202. * 3 | 0 | 0 | 1 | 0 |
  203. * ----------+-----+-----+-----+-----+
  204. *
  205. * Write:
  206. * ----------+-----------------------+
  207. * | |
  208. * | ODT |
  209. * Write To +-----------------------+
  210. * Rank | 3 | 2 | 1 | 0 |
  211. * ----------+-----+-----+-----+-----+
  212. * 0 | 0 | 1 | 0 | 1 |
  213. * 1 | 1 | 0 | 1 | 0 |
  214. * 2 | 0 | 1 | 0 | 1 |
  215. * 3 | 1 | 0 | 1 | 0 |
  216. * ----------+-----+-----+-----+-----+
  217. */
  218. switch (rank) {
  219. case 0:
  220. odt_mask_0 = 0x4;
  221. odt_mask_1 = 0x5;
  222. break;
  223. case 1:
  224. odt_mask_0 = 0x8;
  225. odt_mask_1 = 0xA;
  226. break;
  227. case 2:
  228. odt_mask_0 = 0x1;
  229. odt_mask_1 = 0x5;
  230. break;
  231. case 3:
  232. odt_mask_0 = 0x2;
  233. odt_mask_1 = 0xA;
  234. break;
  235. }
  236. }
  237. } else {
  238. odt_mask_0 = 0x0;
  239. odt_mask_1 = 0x0;
  240. }
  241. cs_and_odt_mask =
  242. (0xFF & ~(1 << rank)) |
  243. ((0xFF & odt_mask_0) << 8) |
  244. ((0xFF & odt_mask_1) << 16);
  245. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_SET_CS_AND_ODT_MASK_OFFSET;
  246. writel(cs_and_odt_mask, SOCFPGA_SDR_ADDRESS + addr);
  247. }
  248. static void scc_mgr_initialize(void)
  249. {
  250. u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_HHP_RFILE_OFFSET;
  251. /*
  252. * Clear register file for HPS
  253. * 16 (2^4) is the size of the full register file in the scc mgr:
  254. * RFILE_DEPTH = log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
  255. * MEM_IF_READ_DQS_WIDTH - 1) + 1;
  256. */
  257. uint32_t i;
  258. for (i = 0; i < 16; i++) {
  259. debug_cond(DLEVEL == 1, "%s:%d: Clearing SCC RFILE index %u\n",
  260. __func__, __LINE__, i);
  261. writel(0, SOCFPGA_SDR_ADDRESS + addr + (i << 2));
  262. }
  263. }
  264. static void scc_mgr_set_dqs_bus_in_delay(uint32_t read_group,
  265. uint32_t delay)
  266. {
  267. u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_IN_DELAY_OFFSET;
  268. /* Load the setting in the SCC manager */
  269. writel(delay, SOCFPGA_SDR_ADDRESS + addr + (read_group << 2));
  270. }
  271. static void scc_mgr_set_dqs_io_in_delay(uint32_t write_group,
  272. uint32_t delay)
  273. {
  274. u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
  275. writel(delay, SOCFPGA_SDR_ADDRESS + addr + (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
  276. }
  277. static void scc_mgr_set_dqs_en_phase(uint32_t read_group, uint32_t phase)
  278. {
  279. u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_EN_PHASE_OFFSET;
  280. /* Load the setting in the SCC manager */
  281. writel(phase, SOCFPGA_SDR_ADDRESS + addr + (read_group << 2));
  282. }
  283. static void scc_mgr_set_dqs_en_phase_all_ranks(uint32_t read_group,
  284. uint32_t phase)
  285. {
  286. uint32_t r;
  287. uint32_t update_scan_chains;
  288. uint32_t addr;
  289. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  290. r += NUM_RANKS_PER_SHADOW_REG) {
  291. /*
  292. * USER although the h/w doesn't support different phases per
  293. * shadow register, for simplicity our scc manager modeling
  294. * keeps different phase settings per shadow reg, and it's
  295. * important for us to keep them in sync to match h/w.
  296. * for efficiency, the scan chain update should occur only
  297. * once to sr0.
  298. */
  299. update_scan_chains = (r == 0) ? 1 : 0;
  300. scc_mgr_set_dqs_en_phase(read_group, phase);
  301. if (update_scan_chains) {
  302. addr = (u32)&sdr_scc_mgr->dqs_ena;
  303. writel(read_group, SOCFPGA_SDR_ADDRESS + addr);
  304. addr = (u32)&sdr_scc_mgr->update;
  305. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  306. }
  307. }
  308. }
  309. static void scc_mgr_set_dqdqs_output_phase(uint32_t write_group,
  310. uint32_t phase)
  311. {
  312. u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQDQS_OUT_PHASE_OFFSET;
  313. /* Load the setting in the SCC manager */
  314. writel(phase, SOCFPGA_SDR_ADDRESS + addr + (write_group << 2));
  315. }
  316. static void scc_mgr_set_dqdqs_output_phase_all_ranks(uint32_t write_group,
  317. uint32_t phase)
  318. {
  319. uint32_t r;
  320. uint32_t update_scan_chains;
  321. uint32_t addr;
  322. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  323. r += NUM_RANKS_PER_SHADOW_REG) {
  324. /*
  325. * USER although the h/w doesn't support different phases per
  326. * shadow register, for simplicity our scc manager modeling
  327. * keeps different phase settings per shadow reg, and it's
  328. * important for us to keep them in sync to match h/w.
  329. * for efficiency, the scan chain update should occur only
  330. * once to sr0.
  331. */
  332. update_scan_chains = (r == 0) ? 1 : 0;
  333. scc_mgr_set_dqdqs_output_phase(write_group, phase);
  334. if (update_scan_chains) {
  335. addr = (u32)&sdr_scc_mgr->dqs_ena;
  336. writel(write_group, SOCFPGA_SDR_ADDRESS + addr);
  337. addr = (u32)&sdr_scc_mgr->update;
  338. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  339. }
  340. }
  341. }
  342. static void scc_mgr_set_dqs_en_delay(uint32_t read_group, uint32_t delay)
  343. {
  344. uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_EN_DELAY_OFFSET;
  345. /* Load the setting in the SCC manager */
  346. writel(delay + IO_DQS_EN_DELAY_OFFSET, SOCFPGA_SDR_ADDRESS + addr +
  347. (read_group << 2));
  348. }
  349. static void scc_mgr_set_dqs_en_delay_all_ranks(uint32_t read_group,
  350. uint32_t delay)
  351. {
  352. uint32_t r;
  353. uint32_t addr;
  354. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  355. r += NUM_RANKS_PER_SHADOW_REG) {
  356. scc_mgr_set_dqs_en_delay(read_group, delay);
  357. addr = (u32)&sdr_scc_mgr->dqs_ena;
  358. writel(read_group, SOCFPGA_SDR_ADDRESS + addr);
  359. /*
  360. * In shadow register mode, the T11 settings are stored in
  361. * registers in the core, which are updated by the DQS_ENA
  362. * signals. Not issuing the SCC_MGR_UPD command allows us to
  363. * save lots of rank switching overhead, by calling
  364. * select_shadow_regs_for_update with update_scan_chains
  365. * set to 0.
  366. */
  367. addr = (u32)&sdr_scc_mgr->update;
  368. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  369. }
  370. /*
  371. * In shadow register mode, the T11 settings are stored in
  372. * registers in the core, which are updated by the DQS_ENA
  373. * signals. Not issuing the SCC_MGR_UPD command allows us to
  374. * save lots of rank switching overhead, by calling
  375. * select_shadow_regs_for_update with update_scan_chains
  376. * set to 0.
  377. */
  378. addr = (u32)&sdr_scc_mgr->update;
  379. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  380. }
  381. static void scc_mgr_set_oct_out1_delay(uint32_t write_group, uint32_t delay)
  382. {
  383. uint32_t read_group;
  384. uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_OCT_OUT1_DELAY_OFFSET;
  385. /*
  386. * Load the setting in the SCC manager
  387. * Although OCT affects only write data, the OCT delay is controlled
  388. * by the DQS logic block which is instantiated once per read group.
  389. * For protocols where a write group consists of multiple read groups,
  390. * the setting must be set multiple times.
  391. */
  392. for (read_group = write_group * RW_MGR_MEM_IF_READ_DQS_WIDTH /
  393. RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
  394. read_group < (write_group + 1) * RW_MGR_MEM_IF_READ_DQS_WIDTH /
  395. RW_MGR_MEM_IF_WRITE_DQS_WIDTH; ++read_group)
  396. writel(delay, SOCFPGA_SDR_ADDRESS + addr + (read_group << 2));
  397. }
  398. static void scc_mgr_set_dq_out1_delay(uint32_t write_group,
  399. uint32_t dq_in_group, uint32_t delay)
  400. {
  401. uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
  402. /* Load the setting in the SCC manager */
  403. writel(delay, SOCFPGA_SDR_ADDRESS + addr + (dq_in_group << 2));
  404. }
  405. static void scc_mgr_set_dq_in_delay(uint32_t write_group,
  406. uint32_t dq_in_group, uint32_t delay)
  407. {
  408. uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
  409. /* Load the setting in the SCC manager */
  410. writel(delay, SOCFPGA_SDR_ADDRESS + addr + (dq_in_group << 2));
  411. }
  412. static void scc_mgr_set_hhp_extras(void)
  413. {
  414. /*
  415. * Load the fixed setting in the SCC manager
  416. * bits: 0:0 = 1'b1 - dqs bypass
  417. * bits: 1:1 = 1'b1 - dq bypass
  418. * bits: 4:2 = 3'b001 - rfifo_mode
  419. * bits: 6:5 = 2'b01 - rfifo clock_select
  420. * bits: 7:7 = 1'b0 - separate gating from ungating setting
  421. * bits: 8:8 = 1'b0 - separate OE from Output delay setting
  422. */
  423. uint32_t value = (0<<8) | (0<<7) | (1<<5) | (1<<2) | (1<<1) | (1<<0);
  424. uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_HHP_GLOBALS_OFFSET;
  425. writel(value, SOCFPGA_SDR_ADDRESS + addr + SCC_MGR_HHP_EXTRAS_OFFSET);
  426. }
  427. static void scc_mgr_set_dqs_out1_delay(uint32_t write_group,
  428. uint32_t delay)
  429. {
  430. uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
  431. /* Load the setting in the SCC manager */
  432. writel(delay, SOCFPGA_SDR_ADDRESS + addr + (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
  433. }
  434. static void scc_mgr_set_dm_out1_delay(uint32_t write_group,
  435. uint32_t dm, uint32_t delay)
  436. {
  437. uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
  438. /* Load the setting in the SCC manager */
  439. writel(delay, SOCFPGA_SDR_ADDRESS + addr +
  440. ((RW_MGR_MEM_DQ_PER_WRITE_DQS + 1 + dm) << 2));
  441. }
  442. /*
  443. * USER Zero all DQS config
  444. * TODO: maybe rename to scc_mgr_zero_dqs_config (or something)
  445. */
  446. static void scc_mgr_zero_all(void)
  447. {
  448. uint32_t i, r;
  449. uint32_t addr;
  450. /*
  451. * USER Zero all DQS config settings, across all groups and all
  452. * shadow registers
  453. */
  454. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r +=
  455. NUM_RANKS_PER_SHADOW_REG) {
  456. for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
  457. /*
  458. * The phases actually don't exist on a per-rank basis,
  459. * but there's no harm updating them several times, so
  460. * let's keep the code simple.
  461. */
  462. scc_mgr_set_dqs_bus_in_delay(i, IO_DQS_IN_RESERVE);
  463. scc_mgr_set_dqs_en_phase(i, 0);
  464. scc_mgr_set_dqs_en_delay(i, 0);
  465. }
  466. for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
  467. scc_mgr_set_dqdqs_output_phase(i, 0);
  468. /* av/cv don't have out2 */
  469. scc_mgr_set_oct_out1_delay(i, IO_DQS_OUT_RESERVE);
  470. }
  471. }
  472. /* multicast to all DQS group enables */
  473. addr = (u32)&sdr_scc_mgr->dqs_ena;
  474. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  475. addr = (u32)&sdr_scc_mgr->update;
  476. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  477. }
  478. static void scc_set_bypass_mode(uint32_t write_group, uint32_t mode)
  479. {
  480. uint32_t addr;
  481. /* mode = 0 : Do NOT bypass - Half Rate Mode */
  482. /* mode = 1 : Bypass - Full Rate Mode */
  483. /* only need to set once for all groups, pins, dq, dqs, dm */
  484. if (write_group == 0) {
  485. debug_cond(DLEVEL == 1, "%s:%d Setting HHP Extras\n", __func__,
  486. __LINE__);
  487. scc_mgr_set_hhp_extras();
  488. debug_cond(DLEVEL == 1, "%s:%d Done Setting HHP Extras\n",
  489. __func__, __LINE__);
  490. }
  491. /* multicast to all DQ enables */
  492. addr = (u32)&sdr_scc_mgr->dq_ena;
  493. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  494. addr = (u32)&sdr_scc_mgr->dm_ena;
  495. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  496. /* update current DQS IO enable */
  497. addr = (u32)&sdr_scc_mgr->dqs_io_ena;
  498. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  499. /* update the DQS logic */
  500. addr = (u32)&sdr_scc_mgr->dqs_ena;
  501. writel(write_group, SOCFPGA_SDR_ADDRESS + addr);
  502. /* hit update */
  503. addr = (u32)&sdr_scc_mgr->update;
  504. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  505. }
  506. static void scc_mgr_zero_group(uint32_t write_group, uint32_t test_begin,
  507. int32_t out_only)
  508. {
  509. uint32_t i, r;
  510. uint32_t addr;
  511. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r +=
  512. NUM_RANKS_PER_SHADOW_REG) {
  513. /* Zero all DQ config settings */
  514. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  515. scc_mgr_set_dq_out1_delay(write_group, i, 0);
  516. if (!out_only)
  517. scc_mgr_set_dq_in_delay(write_group, i, 0);
  518. }
  519. /* multicast to all DQ enables */
  520. addr = (u32)&sdr_scc_mgr->dq_ena;
  521. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  522. /* Zero all DM config settings */
  523. for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
  524. scc_mgr_set_dm_out1_delay(write_group, i, 0);
  525. }
  526. /* multicast to all DM enables */
  527. addr = (u32)&sdr_scc_mgr->dm_ena;
  528. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  529. /* zero all DQS io settings */
  530. if (!out_only)
  531. scc_mgr_set_dqs_io_in_delay(write_group, 0);
  532. /* av/cv don't have out2 */
  533. scc_mgr_set_dqs_out1_delay(write_group, IO_DQS_OUT_RESERVE);
  534. scc_mgr_set_oct_out1_delay(write_group, IO_DQS_OUT_RESERVE);
  535. scc_mgr_load_dqs_for_write_group(write_group);
  536. /* multicast to all DQS IO enables (only 1) */
  537. addr = (u32)&sdr_scc_mgr->dqs_io_ena;
  538. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  539. /* hit update to zero everything */
  540. addr = (u32)&sdr_scc_mgr->update;
  541. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  542. }
  543. }
  544. /* load up dqs config settings */
  545. static void scc_mgr_load_dqs(uint32_t dqs)
  546. {
  547. uint32_t addr = (u32)&sdr_scc_mgr->dqs_ena;
  548. writel(dqs, SOCFPGA_SDR_ADDRESS + addr);
  549. }
  550. static void scc_mgr_load_dqs_for_write_group(uint32_t write_group)
  551. {
  552. uint32_t read_group;
  553. uint32_t addr = (u32)&sdr_scc_mgr->dqs_ena;
  554. /*
  555. * Although OCT affects only write data, the OCT delay is controlled
  556. * by the DQS logic block which is instantiated once per read group.
  557. * For protocols where a write group consists of multiple read groups,
  558. * the setting must be scanned multiple times.
  559. */
  560. for (read_group = write_group * RW_MGR_MEM_IF_READ_DQS_WIDTH /
  561. RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
  562. read_group < (write_group + 1) * RW_MGR_MEM_IF_READ_DQS_WIDTH /
  563. RW_MGR_MEM_IF_WRITE_DQS_WIDTH; ++read_group)
  564. writel(read_group, SOCFPGA_SDR_ADDRESS + addr);
  565. }
  566. /* load up dqs io config settings */
  567. static void scc_mgr_load_dqs_io(void)
  568. {
  569. uint32_t addr = (u32)&sdr_scc_mgr->dqs_io_ena;
  570. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  571. }
  572. /* load up dq config settings */
  573. static void scc_mgr_load_dq(uint32_t dq_in_group)
  574. {
  575. uint32_t addr = (u32)&sdr_scc_mgr->dq_ena;
  576. writel(dq_in_group, SOCFPGA_SDR_ADDRESS + addr);
  577. }
  578. /* load up dm config settings */
  579. static void scc_mgr_load_dm(uint32_t dm)
  580. {
  581. uint32_t addr = (u32)&sdr_scc_mgr->dm_ena;
  582. writel(dm, SOCFPGA_SDR_ADDRESS + addr);
  583. }
  584. /*
  585. * apply and load a particular input delay for the DQ pins in a group
  586. * group_bgn is the index of the first dq pin (in the write group)
  587. */
  588. static void scc_mgr_apply_group_dq_in_delay(uint32_t write_group,
  589. uint32_t group_bgn, uint32_t delay)
  590. {
  591. uint32_t i, p;
  592. for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
  593. scc_mgr_set_dq_in_delay(write_group, p, delay);
  594. scc_mgr_load_dq(p);
  595. }
  596. }
  597. /* apply and load a particular output delay for the DQ pins in a group */
  598. static void scc_mgr_apply_group_dq_out1_delay(uint32_t write_group,
  599. uint32_t group_bgn,
  600. uint32_t delay1)
  601. {
  602. uint32_t i, p;
  603. for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
  604. scc_mgr_set_dq_out1_delay(write_group, i, delay1);
  605. scc_mgr_load_dq(i);
  606. }
  607. }
  608. /* apply and load a particular output delay for the DM pins in a group */
  609. static void scc_mgr_apply_group_dm_out1_delay(uint32_t write_group,
  610. uint32_t delay1)
  611. {
  612. uint32_t i;
  613. for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
  614. scc_mgr_set_dm_out1_delay(write_group, i, delay1);
  615. scc_mgr_load_dm(i);
  616. }
  617. }
  618. /* apply and load delay on both DQS and OCT out1 */
  619. static void scc_mgr_apply_group_dqs_io_and_oct_out1(uint32_t write_group,
  620. uint32_t delay)
  621. {
  622. scc_mgr_set_dqs_out1_delay(write_group, delay);
  623. scc_mgr_load_dqs_io();
  624. scc_mgr_set_oct_out1_delay(write_group, delay);
  625. scc_mgr_load_dqs_for_write_group(write_group);
  626. }
  627. /* apply a delay to the entire output side: DQ, DM, DQS, OCT */
  628. static void scc_mgr_apply_group_all_out_delay_add(uint32_t write_group,
  629. uint32_t group_bgn,
  630. uint32_t delay)
  631. {
  632. uint32_t i, p, new_delay;
  633. /* dq shift */
  634. for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
  635. new_delay = READ_SCC_DQ_OUT2_DELAY;
  636. new_delay += delay;
  637. if (new_delay > IO_IO_OUT2_DELAY_MAX) {
  638. debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQ[%u,%u]:\
  639. %u > %lu => %lu", __func__, __LINE__,
  640. write_group, group_bgn, delay, i, p, new_delay,
  641. (long unsigned int)IO_IO_OUT2_DELAY_MAX,
  642. (long unsigned int)IO_IO_OUT2_DELAY_MAX);
  643. new_delay = IO_IO_OUT2_DELAY_MAX;
  644. }
  645. scc_mgr_load_dq(i);
  646. }
  647. /* dm shift */
  648. for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
  649. new_delay = READ_SCC_DM_IO_OUT2_DELAY;
  650. new_delay += delay;
  651. if (new_delay > IO_IO_OUT2_DELAY_MAX) {
  652. debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DM[%u]:\
  653. %u > %lu => %lu\n", __func__, __LINE__,
  654. write_group, group_bgn, delay, i, new_delay,
  655. (long unsigned int)IO_IO_OUT2_DELAY_MAX,
  656. (long unsigned int)IO_IO_OUT2_DELAY_MAX);
  657. new_delay = IO_IO_OUT2_DELAY_MAX;
  658. }
  659. scc_mgr_load_dm(i);
  660. }
  661. /* dqs shift */
  662. new_delay = READ_SCC_DQS_IO_OUT2_DELAY;
  663. new_delay += delay;
  664. if (new_delay > IO_IO_OUT2_DELAY_MAX) {
  665. debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQS: %u > %d => %d;"
  666. " adding %u to OUT1\n", __func__, __LINE__,
  667. write_group, group_bgn, delay, new_delay,
  668. IO_IO_OUT2_DELAY_MAX, IO_IO_OUT2_DELAY_MAX,
  669. new_delay - IO_IO_OUT2_DELAY_MAX);
  670. scc_mgr_set_dqs_out1_delay(write_group, new_delay -
  671. IO_IO_OUT2_DELAY_MAX);
  672. new_delay = IO_IO_OUT2_DELAY_MAX;
  673. }
  674. scc_mgr_load_dqs_io();
  675. /* oct shift */
  676. new_delay = READ_SCC_OCT_OUT2_DELAY;
  677. new_delay += delay;
  678. if (new_delay > IO_IO_OUT2_DELAY_MAX) {
  679. debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQS: %u > %d => %d;"
  680. " adding %u to OUT1\n", __func__, __LINE__,
  681. write_group, group_bgn, delay, new_delay,
  682. IO_IO_OUT2_DELAY_MAX, IO_IO_OUT2_DELAY_MAX,
  683. new_delay - IO_IO_OUT2_DELAY_MAX);
  684. scc_mgr_set_oct_out1_delay(write_group, new_delay -
  685. IO_IO_OUT2_DELAY_MAX);
  686. new_delay = IO_IO_OUT2_DELAY_MAX;
  687. }
  688. scc_mgr_load_dqs_for_write_group(write_group);
  689. }
  690. /*
  691. * USER apply a delay to the entire output side (DQ, DM, DQS, OCT)
  692. * and to all ranks
  693. */
  694. static void scc_mgr_apply_group_all_out_delay_add_all_ranks(
  695. uint32_t write_group, uint32_t group_bgn, uint32_t delay)
  696. {
  697. uint32_t r;
  698. uint32_t addr = (u32)&sdr_scc_mgr->update;
  699. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  700. r += NUM_RANKS_PER_SHADOW_REG) {
  701. scc_mgr_apply_group_all_out_delay_add(write_group,
  702. group_bgn, delay);
  703. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  704. }
  705. }
  706. /* optimization used to recover some slots in ddr3 inst_rom */
  707. /* could be applied to other protocols if we wanted to */
  708. static void set_jump_as_return(void)
  709. {
  710. uint32_t addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  711. /*
  712. * to save space, we replace return with jump to special shared
  713. * RETURN instruction so we set the counter to large value so that
  714. * we always jump
  715. */
  716. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  717. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  718. writel(RW_MGR_RETURN, SOCFPGA_SDR_ADDRESS + addr);
  719. }
  720. /*
  721. * should always use constants as argument to ensure all computations are
  722. * performed at compile time
  723. */
  724. static void delay_for_n_mem_clocks(const uint32_t clocks)
  725. {
  726. uint32_t afi_clocks;
  727. uint8_t inner = 0;
  728. uint8_t outer = 0;
  729. uint16_t c_loop = 0;
  730. uint32_t addr;
  731. debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
  732. afi_clocks = (clocks + AFI_RATE_RATIO-1) / AFI_RATE_RATIO;
  733. /* scale (rounding up) to get afi clocks */
  734. /*
  735. * Note, we don't bother accounting for being off a little bit
  736. * because of a few extra instructions in outer loops
  737. * Note, the loops have a test at the end, and do the test before
  738. * the decrement, and so always perform the loop
  739. * 1 time more than the counter value
  740. */
  741. if (afi_clocks == 0) {
  742. ;
  743. } else if (afi_clocks <= 0x100) {
  744. inner = afi_clocks-1;
  745. outer = 0;
  746. c_loop = 0;
  747. } else if (afi_clocks <= 0x10000) {
  748. inner = 0xff;
  749. outer = (afi_clocks-1) >> 8;
  750. c_loop = 0;
  751. } else {
  752. inner = 0xff;
  753. outer = 0xff;
  754. c_loop = (afi_clocks-1) >> 16;
  755. }
  756. /*
  757. * rom instructions are structured as follows:
  758. *
  759. * IDLE_LOOP2: jnz cntr0, TARGET_A
  760. * IDLE_LOOP1: jnz cntr1, TARGET_B
  761. * return
  762. *
  763. * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
  764. * TARGET_B is set to IDLE_LOOP2 as well
  765. *
  766. * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
  767. * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
  768. *
  769. * a little confusing, but it helps save precious space in the inst_rom
  770. * and sequencer rom and keeps the delays more accurate and reduces
  771. * overhead
  772. */
  773. if (afi_clocks <= 0x100) {
  774. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  775. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner), SOCFPGA_SDR_ADDRESS + addr);
  776. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  777. writel(RW_MGR_IDLE_LOOP1, SOCFPGA_SDR_ADDRESS + addr);
  778. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  779. writel(RW_MGR_IDLE_LOOP1, SOCFPGA_SDR_ADDRESS + addr);
  780. } else {
  781. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  782. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner), SOCFPGA_SDR_ADDRESS + addr);
  783. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  784. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer), SOCFPGA_SDR_ADDRESS + addr);
  785. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  786. writel(RW_MGR_IDLE_LOOP2, SOCFPGA_SDR_ADDRESS + addr);
  787. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  788. writel(RW_MGR_IDLE_LOOP2, SOCFPGA_SDR_ADDRESS + addr);
  789. /* hack to get around compiler not being smart enough */
  790. if (afi_clocks <= 0x10000) {
  791. /* only need to run once */
  792. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  793. writel(RW_MGR_IDLE_LOOP2, SOCFPGA_SDR_ADDRESS + addr);
  794. } else {
  795. do {
  796. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  797. writel(RW_MGR_IDLE_LOOP2, SOCFPGA_SDR_ADDRESS + addr);
  798. } while (c_loop-- != 0);
  799. }
  800. }
  801. debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
  802. }
  803. static void rw_mgr_mem_initialize(void)
  804. {
  805. uint32_t r;
  806. uint32_t addr;
  807. debug("%s:%d\n", __func__, __LINE__);
  808. /* The reset / cke part of initialization is broadcasted to all ranks */
  809. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_SET_CS_AND_ODT_MASK_OFFSET;
  810. writel(RW_MGR_RANK_ALL, SOCFPGA_SDR_ADDRESS + addr);
  811. /*
  812. * Here's how you load register for a loop
  813. * Counters are located @ 0x800
  814. * Jump address are located @ 0xC00
  815. * For both, registers 0 to 3 are selected using bits 3 and 2, like
  816. * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
  817. * I know this ain't pretty, but Avalon bus throws away the 2 least
  818. * significant bits
  819. */
  820. /* start with memory RESET activated */
  821. /* tINIT = 200us */
  822. /*
  823. * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
  824. * If a and b are the number of iteration in 2 nested loops
  825. * it takes the following number of cycles to complete the operation:
  826. * number_of_cycles = ((2 + n) * a + 2) * b
  827. * where n is the number of instruction in the inner loop
  828. * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
  829. * b = 6A
  830. */
  831. /* Load counters */
  832. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  833. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR0_VAL),
  834. SOCFPGA_SDR_ADDRESS + addr);
  835. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  836. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR1_VAL),
  837. SOCFPGA_SDR_ADDRESS + addr);
  838. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr2;
  839. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR2_VAL),
  840. SOCFPGA_SDR_ADDRESS + addr);
  841. /* Load jump address */
  842. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  843. writel(RW_MGR_INIT_RESET_0_CKE_0, SOCFPGA_SDR_ADDRESS + addr);
  844. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  845. writel(RW_MGR_INIT_RESET_0_CKE_0, SOCFPGA_SDR_ADDRESS + addr);
  846. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  847. writel(RW_MGR_INIT_RESET_0_CKE_0, SOCFPGA_SDR_ADDRESS + addr);
  848. /* Execute count instruction */
  849. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  850. writel(RW_MGR_INIT_RESET_0_CKE_0, SOCFPGA_SDR_ADDRESS + addr);
  851. /* indicate that memory is stable */
  852. addr = (u32)&phy_mgr_cfg->reset_mem_stbl;
  853. writel(1, SOCFPGA_SDR_ADDRESS + addr);
  854. /*
  855. * transition the RESET to high
  856. * Wait for 500us
  857. */
  858. /*
  859. * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
  860. * If a and b are the number of iteration in 2 nested loops
  861. * it takes the following number of cycles to complete the operation
  862. * number_of_cycles = ((2 + n) * a + 2) * b
  863. * where n is the number of instruction in the inner loop
  864. * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
  865. * b = FF
  866. */
  867. /* Load counters */
  868. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  869. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR0_VAL),
  870. SOCFPGA_SDR_ADDRESS + addr);
  871. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  872. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR1_VAL),
  873. SOCFPGA_SDR_ADDRESS + addr);
  874. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr2;
  875. writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR2_VAL),
  876. SOCFPGA_SDR_ADDRESS + addr);
  877. /* Load jump address */
  878. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  879. writel(RW_MGR_INIT_RESET_1_CKE_0, SOCFPGA_SDR_ADDRESS + addr);
  880. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  881. writel(RW_MGR_INIT_RESET_1_CKE_0, SOCFPGA_SDR_ADDRESS + addr);
  882. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  883. writel(RW_MGR_INIT_RESET_1_CKE_0, SOCFPGA_SDR_ADDRESS + addr);
  884. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  885. writel(RW_MGR_INIT_RESET_1_CKE_0, SOCFPGA_SDR_ADDRESS + addr);
  886. /* bring up clock enable */
  887. /* tXRP < 250 ck cycles */
  888. delay_for_n_mem_clocks(250);
  889. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
  890. if (param->skip_ranks[r]) {
  891. /* request to skip the rank */
  892. continue;
  893. }
  894. /* set rank */
  895. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
  896. /*
  897. * USER Use Mirror-ed commands for odd ranks if address
  898. * mirrorring is on
  899. */
  900. if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
  901. set_jump_as_return();
  902. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  903. writel(RW_MGR_MRS2_MIRR, SOCFPGA_SDR_ADDRESS + addr);
  904. delay_for_n_mem_clocks(4);
  905. set_jump_as_return();
  906. writel(RW_MGR_MRS3_MIRR, SOCFPGA_SDR_ADDRESS + addr);
  907. delay_for_n_mem_clocks(4);
  908. set_jump_as_return();
  909. writel(RW_MGR_MRS1_MIRR, SOCFPGA_SDR_ADDRESS + addr);
  910. delay_for_n_mem_clocks(4);
  911. set_jump_as_return();
  912. writel(RW_MGR_MRS0_DLL_RESET_MIRR, SOCFPGA_SDR_ADDRESS + addr);
  913. } else {
  914. set_jump_as_return();
  915. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  916. writel(RW_MGR_MRS2, SOCFPGA_SDR_ADDRESS + addr);
  917. delay_for_n_mem_clocks(4);
  918. set_jump_as_return();
  919. writel(RW_MGR_MRS3, SOCFPGA_SDR_ADDRESS + addr);
  920. delay_for_n_mem_clocks(4);
  921. set_jump_as_return();
  922. writel(RW_MGR_MRS1, SOCFPGA_SDR_ADDRESS + addr);
  923. set_jump_as_return();
  924. writel(RW_MGR_MRS0_DLL_RESET, SOCFPGA_SDR_ADDRESS + addr);
  925. }
  926. set_jump_as_return();
  927. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  928. writel(RW_MGR_ZQCL, SOCFPGA_SDR_ADDRESS + addr);
  929. /* tZQinit = tDLLK = 512 ck cycles */
  930. delay_for_n_mem_clocks(512);
  931. }
  932. }
  933. /*
  934. * At the end of calibration we have to program the user settings in, and
  935. * USER hand off the memory to the user.
  936. */
  937. static void rw_mgr_mem_handoff(void)
  938. {
  939. uint32_t r;
  940. uint32_t addr;
  941. debug("%s:%d\n", __func__, __LINE__);
  942. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
  943. if (param->skip_ranks[r])
  944. /* request to skip the rank */
  945. continue;
  946. /* set rank */
  947. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
  948. /* precharge all banks ... */
  949. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  950. writel(RW_MGR_PRECHARGE_ALL, SOCFPGA_SDR_ADDRESS + addr);
  951. /* load up MR settings specified by user */
  952. /*
  953. * Use Mirror-ed commands for odd ranks if address
  954. * mirrorring is on
  955. */
  956. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  957. if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
  958. set_jump_as_return();
  959. writel(RW_MGR_MRS2_MIRR, SOCFPGA_SDR_ADDRESS + addr);
  960. delay_for_n_mem_clocks(4);
  961. set_jump_as_return();
  962. writel(RW_MGR_MRS3_MIRR, SOCFPGA_SDR_ADDRESS + addr);
  963. delay_for_n_mem_clocks(4);
  964. set_jump_as_return();
  965. writel(RW_MGR_MRS1_MIRR, SOCFPGA_SDR_ADDRESS + addr);
  966. delay_for_n_mem_clocks(4);
  967. set_jump_as_return();
  968. writel(RW_MGR_MRS0_USER_MIRR, SOCFPGA_SDR_ADDRESS + addr);
  969. } else {
  970. set_jump_as_return();
  971. writel(RW_MGR_MRS2, SOCFPGA_SDR_ADDRESS + addr);
  972. delay_for_n_mem_clocks(4);
  973. set_jump_as_return();
  974. writel(RW_MGR_MRS3, SOCFPGA_SDR_ADDRESS + addr);
  975. delay_for_n_mem_clocks(4);
  976. set_jump_as_return();
  977. writel(RW_MGR_MRS1, SOCFPGA_SDR_ADDRESS + addr);
  978. delay_for_n_mem_clocks(4);
  979. set_jump_as_return();
  980. writel(RW_MGR_MRS0_USER, SOCFPGA_SDR_ADDRESS + addr);
  981. }
  982. /*
  983. * USER need to wait tMOD (12CK or 15ns) time before issuing
  984. * other commands, but we will have plenty of NIOS cycles before
  985. * actual handoff so its okay.
  986. */
  987. }
  988. }
  989. /*
  990. * performs a guaranteed read on the patterns we are going to use during a
  991. * read test to ensure memory works
  992. */
  993. static uint32_t rw_mgr_mem_calibrate_read_test_patterns(uint32_t rank_bgn,
  994. uint32_t group, uint32_t num_tries, uint32_t *bit_chk,
  995. uint32_t all_ranks)
  996. {
  997. uint32_t r, vg;
  998. uint32_t correct_mask_vg;
  999. uint32_t tmp_bit_chk;
  1000. uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
  1001. (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
  1002. uint32_t addr;
  1003. uint32_t base_rw_mgr;
  1004. *bit_chk = param->read_correct_mask;
  1005. correct_mask_vg = param->read_correct_mask_vg;
  1006. for (r = rank_bgn; r < rank_end; r++) {
  1007. if (param->skip_ranks[r])
  1008. /* request to skip the rank */
  1009. continue;
  1010. /* set rank */
  1011. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
  1012. /* Load up a constant bursts of read commands */
  1013. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  1014. writel(0x20, SOCFPGA_SDR_ADDRESS + addr);
  1015. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  1016. writel(RW_MGR_GUARANTEED_READ, SOCFPGA_SDR_ADDRESS + addr);
  1017. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  1018. writel(0x20, SOCFPGA_SDR_ADDRESS + addr);
  1019. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  1020. writel(RW_MGR_GUARANTEED_READ_CONT, SOCFPGA_SDR_ADDRESS + addr);
  1021. tmp_bit_chk = 0;
  1022. for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
  1023. /* reset the fifos to get pointers to known state */
  1024. addr = (u32)&phy_mgr_cmd->fifo_reset;
  1025. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1026. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RESET_READ_DATAPATH_OFFSET;
  1027. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1028. tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
  1029. / RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
  1030. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  1031. writel(RW_MGR_GUARANTEED_READ, SOCFPGA_SDR_ADDRESS + addr +
  1032. ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
  1033. vg) << 2));
  1034. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS;
  1035. base_rw_mgr = readl(SOCFPGA_SDR_ADDRESS + addr);
  1036. tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & (~base_rw_mgr));
  1037. if (vg == 0)
  1038. break;
  1039. }
  1040. *bit_chk &= tmp_bit_chk;
  1041. }
  1042. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  1043. writel(RW_MGR_CLEAR_DQS_ENABLE, SOCFPGA_SDR_ADDRESS + addr + (group << 2));
  1044. set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
  1045. debug_cond(DLEVEL == 1, "%s:%d test_load_patterns(%u,ALL) => (%u == %u) =>\
  1046. %lu\n", __func__, __LINE__, group, *bit_chk, param->read_correct_mask,
  1047. (long unsigned int)(*bit_chk == param->read_correct_mask));
  1048. return *bit_chk == param->read_correct_mask;
  1049. }
  1050. static uint32_t rw_mgr_mem_calibrate_read_test_patterns_all_ranks
  1051. (uint32_t group, uint32_t num_tries, uint32_t *bit_chk)
  1052. {
  1053. return rw_mgr_mem_calibrate_read_test_patterns(0, group,
  1054. num_tries, bit_chk, 1);
  1055. }
  1056. /* load up the patterns we are going to use during a read test */
  1057. static void rw_mgr_mem_calibrate_read_load_patterns(uint32_t rank_bgn,
  1058. uint32_t all_ranks)
  1059. {
  1060. uint32_t r;
  1061. uint32_t addr;
  1062. uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
  1063. (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
  1064. debug("%s:%d\n", __func__, __LINE__);
  1065. for (r = rank_bgn; r < rank_end; r++) {
  1066. if (param->skip_ranks[r])
  1067. /* request to skip the rank */
  1068. continue;
  1069. /* set rank */
  1070. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
  1071. /* Load up a constant bursts */
  1072. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  1073. writel(0x20, SOCFPGA_SDR_ADDRESS + addr);
  1074. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  1075. writel(RW_MGR_GUARANTEED_WRITE_WAIT0, SOCFPGA_SDR_ADDRESS + addr);
  1076. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  1077. writel(0x20, SOCFPGA_SDR_ADDRESS + addr);
  1078. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  1079. writel(RW_MGR_GUARANTEED_WRITE_WAIT1, SOCFPGA_SDR_ADDRESS + addr);
  1080. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr2;
  1081. writel(0x04, SOCFPGA_SDR_ADDRESS + addr);
  1082. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  1083. writel(RW_MGR_GUARANTEED_WRITE_WAIT2, SOCFPGA_SDR_ADDRESS + addr);
  1084. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr3;
  1085. writel(0x04, SOCFPGA_SDR_ADDRESS + addr);
  1086. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add3;
  1087. writel(RW_MGR_GUARANTEED_WRITE_WAIT3, SOCFPGA_SDR_ADDRESS + addr);
  1088. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  1089. writel(RW_MGR_GUARANTEED_WRITE, SOCFPGA_SDR_ADDRESS + addr);
  1090. }
  1091. set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
  1092. }
  1093. /*
  1094. * try a read and see if it returns correct data back. has dummy reads
  1095. * inserted into the mix used to align dqs enable. has more thorough checks
  1096. * than the regular read test.
  1097. */
  1098. static uint32_t rw_mgr_mem_calibrate_read_test(uint32_t rank_bgn, uint32_t group,
  1099. uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
  1100. uint32_t all_groups, uint32_t all_ranks)
  1101. {
  1102. uint32_t r, vg;
  1103. uint32_t correct_mask_vg;
  1104. uint32_t tmp_bit_chk;
  1105. uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
  1106. (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
  1107. uint32_t addr;
  1108. uint32_t base_rw_mgr;
  1109. *bit_chk = param->read_correct_mask;
  1110. correct_mask_vg = param->read_correct_mask_vg;
  1111. uint32_t quick_read_mode = (((STATIC_CALIB_STEPS) &
  1112. CALIB_SKIP_DELAY_SWEEPS) && ENABLE_SUPER_QUICK_CALIBRATION);
  1113. for (r = rank_bgn; r < rank_end; r++) {
  1114. if (param->skip_ranks[r])
  1115. /* request to skip the rank */
  1116. continue;
  1117. /* set rank */
  1118. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
  1119. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  1120. writel(0x10, SOCFPGA_SDR_ADDRESS + addr);
  1121. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  1122. writel(RW_MGR_READ_B2B_WAIT1, SOCFPGA_SDR_ADDRESS + addr);
  1123. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr2;
  1124. writel(0x10, SOCFPGA_SDR_ADDRESS + addr);
  1125. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  1126. writel(RW_MGR_READ_B2B_WAIT2, SOCFPGA_SDR_ADDRESS + addr);
  1127. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  1128. if (quick_read_mode)
  1129. writel(0x1, SOCFPGA_SDR_ADDRESS + addr);
  1130. /* need at least two (1+1) reads to capture failures */
  1131. else if (all_groups)
  1132. writel(0x06, SOCFPGA_SDR_ADDRESS + addr);
  1133. else
  1134. writel(0x32, SOCFPGA_SDR_ADDRESS + addr);
  1135. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  1136. writel(RW_MGR_READ_B2B, SOCFPGA_SDR_ADDRESS + addr);
  1137. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr3;
  1138. if (all_groups)
  1139. writel(RW_MGR_MEM_IF_READ_DQS_WIDTH *
  1140. RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1,
  1141. SOCFPGA_SDR_ADDRESS + addr);
  1142. else
  1143. writel(0x0, SOCFPGA_SDR_ADDRESS + addr);
  1144. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add3;
  1145. writel(RW_MGR_READ_B2B, SOCFPGA_SDR_ADDRESS + addr);
  1146. tmp_bit_chk = 0;
  1147. for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
  1148. /* reset the fifos to get pointers to known state */
  1149. addr = (u32)&phy_mgr_cmd->fifo_reset;
  1150. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1151. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RESET_READ_DATAPATH_OFFSET;
  1152. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1153. tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
  1154. / RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
  1155. if (all_groups)
  1156. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_ALL_GROUPS_OFFSET;
  1157. else
  1158. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  1159. writel(RW_MGR_READ_B2B, SOCFPGA_SDR_ADDRESS + addr +
  1160. ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
  1161. vg) << 2));
  1162. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS;
  1163. base_rw_mgr = readl(SOCFPGA_SDR_ADDRESS + addr);
  1164. tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
  1165. if (vg == 0)
  1166. break;
  1167. }
  1168. *bit_chk &= tmp_bit_chk;
  1169. }
  1170. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  1171. writel(RW_MGR_CLEAR_DQS_ENABLE, SOCFPGA_SDR_ADDRESS + addr + (group << 2));
  1172. if (all_correct) {
  1173. set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
  1174. debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ALL,%u) =>\
  1175. (%u == %u) => %lu", __func__, __LINE__, group,
  1176. all_groups, *bit_chk, param->read_correct_mask,
  1177. (long unsigned int)(*bit_chk ==
  1178. param->read_correct_mask));
  1179. return *bit_chk == param->read_correct_mask;
  1180. } else {
  1181. set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
  1182. debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ONE,%u) =>\
  1183. (%u != %lu) => %lu\n", __func__, __LINE__,
  1184. group, all_groups, *bit_chk, (long unsigned int)0,
  1185. (long unsigned int)(*bit_chk != 0x00));
  1186. return *bit_chk != 0x00;
  1187. }
  1188. }
  1189. static uint32_t rw_mgr_mem_calibrate_read_test_all_ranks(uint32_t group,
  1190. uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
  1191. uint32_t all_groups)
  1192. {
  1193. return rw_mgr_mem_calibrate_read_test(0, group, num_tries, all_correct,
  1194. bit_chk, all_groups, 1);
  1195. }
  1196. static void rw_mgr_incr_vfifo(uint32_t grp, uint32_t *v)
  1197. {
  1198. uint32_t addr = (u32)&phy_mgr_cmd->inc_vfifo_hard_phy;
  1199. writel(grp, SOCFPGA_SDR_ADDRESS + addr);
  1200. (*v)++;
  1201. }
  1202. static void rw_mgr_decr_vfifo(uint32_t grp, uint32_t *v)
  1203. {
  1204. uint32_t i;
  1205. for (i = 0; i < VFIFO_SIZE-1; i++)
  1206. rw_mgr_incr_vfifo(grp, v);
  1207. }
  1208. static int find_vfifo_read(uint32_t grp, uint32_t *bit_chk)
  1209. {
  1210. uint32_t v;
  1211. uint32_t fail_cnt = 0;
  1212. uint32_t test_status;
  1213. for (v = 0; v < VFIFO_SIZE; ) {
  1214. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo %u\n",
  1215. __func__, __LINE__, v);
  1216. test_status = rw_mgr_mem_calibrate_read_test_all_ranks
  1217. (grp, 1, PASS_ONE_BIT, bit_chk, 0);
  1218. if (!test_status) {
  1219. fail_cnt++;
  1220. if (fail_cnt == 2)
  1221. break;
  1222. }
  1223. /* fiddle with FIFO */
  1224. rw_mgr_incr_vfifo(grp, &v);
  1225. }
  1226. if (v >= VFIFO_SIZE) {
  1227. /* no failing read found!! Something must have gone wrong */
  1228. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo failed\n",
  1229. __func__, __LINE__);
  1230. return 0;
  1231. } else {
  1232. return v;
  1233. }
  1234. }
  1235. static int find_working_phase(uint32_t *grp, uint32_t *bit_chk,
  1236. uint32_t dtaps_per_ptap, uint32_t *work_bgn,
  1237. uint32_t *v, uint32_t *d, uint32_t *p,
  1238. uint32_t *i, uint32_t *max_working_cnt)
  1239. {
  1240. uint32_t found_begin = 0;
  1241. uint32_t tmp_delay = 0;
  1242. uint32_t test_status;
  1243. for (*d = 0; *d <= dtaps_per_ptap; (*d)++, tmp_delay +=
  1244. IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
  1245. *work_bgn = tmp_delay;
  1246. scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
  1247. for (*i = 0; *i < VFIFO_SIZE; (*i)++) {
  1248. for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_bgn +=
  1249. IO_DELAY_PER_OPA_TAP) {
  1250. scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
  1251. test_status =
  1252. rw_mgr_mem_calibrate_read_test_all_ranks
  1253. (*grp, 1, PASS_ONE_BIT, bit_chk, 0);
  1254. if (test_status) {
  1255. *max_working_cnt = 1;
  1256. found_begin = 1;
  1257. break;
  1258. }
  1259. }
  1260. if (found_begin)
  1261. break;
  1262. if (*p > IO_DQS_EN_PHASE_MAX)
  1263. /* fiddle with FIFO */
  1264. rw_mgr_incr_vfifo(*grp, v);
  1265. }
  1266. if (found_begin)
  1267. break;
  1268. }
  1269. if (*i >= VFIFO_SIZE) {
  1270. /* cannot find working solution */
  1271. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/\
  1272. ptap/dtap\n", __func__, __LINE__);
  1273. return 0;
  1274. } else {
  1275. return 1;
  1276. }
  1277. }
  1278. static void sdr_backup_phase(uint32_t *grp, uint32_t *bit_chk,
  1279. uint32_t *work_bgn, uint32_t *v, uint32_t *d,
  1280. uint32_t *p, uint32_t *max_working_cnt)
  1281. {
  1282. uint32_t found_begin = 0;
  1283. uint32_t tmp_delay;
  1284. /* Special case code for backing up a phase */
  1285. if (*p == 0) {
  1286. *p = IO_DQS_EN_PHASE_MAX;
  1287. rw_mgr_decr_vfifo(*grp, v);
  1288. } else {
  1289. (*p)--;
  1290. }
  1291. tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
  1292. scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
  1293. for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn;
  1294. (*d)++, tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
  1295. scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
  1296. if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
  1297. PASS_ONE_BIT,
  1298. bit_chk, 0)) {
  1299. found_begin = 1;
  1300. *work_bgn = tmp_delay;
  1301. break;
  1302. }
  1303. }
  1304. /* We have found a working dtap before the ptap found above */
  1305. if (found_begin == 1)
  1306. (*max_working_cnt)++;
  1307. /*
  1308. * Restore VFIFO to old state before we decremented it
  1309. * (if needed).
  1310. */
  1311. (*p)++;
  1312. if (*p > IO_DQS_EN_PHASE_MAX) {
  1313. *p = 0;
  1314. rw_mgr_incr_vfifo(*grp, v);
  1315. }
  1316. scc_mgr_set_dqs_en_delay_all_ranks(*grp, 0);
  1317. }
  1318. static int sdr_nonworking_phase(uint32_t *grp, uint32_t *bit_chk,
  1319. uint32_t *work_bgn, uint32_t *v, uint32_t *d,
  1320. uint32_t *p, uint32_t *i, uint32_t *max_working_cnt,
  1321. uint32_t *work_end)
  1322. {
  1323. uint32_t found_end = 0;
  1324. (*p)++;
  1325. *work_end += IO_DELAY_PER_OPA_TAP;
  1326. if (*p > IO_DQS_EN_PHASE_MAX) {
  1327. /* fiddle with FIFO */
  1328. *p = 0;
  1329. rw_mgr_incr_vfifo(*grp, v);
  1330. }
  1331. for (; *i < VFIFO_SIZE + 1; (*i)++) {
  1332. for (; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_end
  1333. += IO_DELAY_PER_OPA_TAP) {
  1334. scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
  1335. if (!rw_mgr_mem_calibrate_read_test_all_ranks
  1336. (*grp, 1, PASS_ONE_BIT, bit_chk, 0)) {
  1337. found_end = 1;
  1338. break;
  1339. } else {
  1340. (*max_working_cnt)++;
  1341. }
  1342. }
  1343. if (found_end)
  1344. break;
  1345. if (*p > IO_DQS_EN_PHASE_MAX) {
  1346. /* fiddle with FIFO */
  1347. rw_mgr_incr_vfifo(*grp, v);
  1348. *p = 0;
  1349. }
  1350. }
  1351. if (*i >= VFIFO_SIZE + 1) {
  1352. /* cannot see edge of failing read */
  1353. debug_cond(DLEVEL == 2, "%s:%d sdr_nonworking_phase: end:\
  1354. failed\n", __func__, __LINE__);
  1355. return 0;
  1356. } else {
  1357. return 1;
  1358. }
  1359. }
  1360. static int sdr_find_window_centre(uint32_t *grp, uint32_t *bit_chk,
  1361. uint32_t *work_bgn, uint32_t *v, uint32_t *d,
  1362. uint32_t *p, uint32_t *work_mid,
  1363. uint32_t *work_end)
  1364. {
  1365. int i;
  1366. int tmp_delay = 0;
  1367. *work_mid = (*work_bgn + *work_end) / 2;
  1368. debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
  1369. *work_bgn, *work_end, *work_mid);
  1370. /* Get the middle delay to be less than a VFIFO delay */
  1371. for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX;
  1372. (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
  1373. ;
  1374. debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
  1375. while (*work_mid > tmp_delay)
  1376. *work_mid -= tmp_delay;
  1377. debug_cond(DLEVEL == 2, "new work_mid %d\n", *work_mid);
  1378. tmp_delay = 0;
  1379. for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX && tmp_delay < *work_mid;
  1380. (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
  1381. ;
  1382. tmp_delay -= IO_DELAY_PER_OPA_TAP;
  1383. debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", (*p) - 1, tmp_delay);
  1384. for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_mid; (*d)++,
  1385. tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP)
  1386. ;
  1387. debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", *d, tmp_delay);
  1388. scc_mgr_set_dqs_en_phase_all_ranks(*grp, (*p) - 1);
  1389. scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
  1390. /*
  1391. * push vfifo until we can successfully calibrate. We can do this
  1392. * because the largest possible margin in 1 VFIFO cycle.
  1393. */
  1394. for (i = 0; i < VFIFO_SIZE; i++) {
  1395. debug_cond(DLEVEL == 2, "find_dqs_en_phase: center: vfifo=%u\n",
  1396. *v);
  1397. if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
  1398. PASS_ONE_BIT,
  1399. bit_chk, 0)) {
  1400. break;
  1401. }
  1402. /* fiddle with FIFO */
  1403. rw_mgr_incr_vfifo(*grp, v);
  1404. }
  1405. if (i >= VFIFO_SIZE) {
  1406. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center: \
  1407. failed\n", __func__, __LINE__);
  1408. return 0;
  1409. } else {
  1410. return 1;
  1411. }
  1412. }
  1413. /* find a good dqs enable to use */
  1414. static uint32_t rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(uint32_t grp)
  1415. {
  1416. uint32_t v, d, p, i;
  1417. uint32_t max_working_cnt;
  1418. uint32_t bit_chk;
  1419. uint32_t dtaps_per_ptap;
  1420. uint32_t work_bgn, work_mid, work_end;
  1421. uint32_t found_passing_read, found_failing_read, initial_failing_dtap;
  1422. uint32_t addr;
  1423. debug("%s:%d %u\n", __func__, __LINE__, grp);
  1424. reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
  1425. scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
  1426. scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
  1427. /* ************************************************************** */
  1428. /* * Step 0 : Determine number of delay taps for each phase tap * */
  1429. dtaps_per_ptap = IO_DELAY_PER_OPA_TAP/IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
  1430. /* ********************************************************* */
  1431. /* * Step 1 : First push vfifo until we get a failing read * */
  1432. v = find_vfifo_read(grp, &bit_chk);
  1433. max_working_cnt = 0;
  1434. /* ******************************************************** */
  1435. /* * step 2: find first working phase, increment in ptaps * */
  1436. work_bgn = 0;
  1437. if (find_working_phase(&grp, &bit_chk, dtaps_per_ptap, &work_bgn, &v, &d,
  1438. &p, &i, &max_working_cnt) == 0)
  1439. return 0;
  1440. work_end = work_bgn;
  1441. /*
  1442. * If d is 0 then the working window covers a phase tap and
  1443. * we can follow the old procedure otherwise, we've found the beginning,
  1444. * and we need to increment the dtaps until we find the end.
  1445. */
  1446. if (d == 0) {
  1447. /* ********************************************************* */
  1448. /* * step 3a: if we have room, back off by one and
  1449. increment in dtaps * */
  1450. sdr_backup_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
  1451. &max_working_cnt);
  1452. /* ********************************************************* */
  1453. /* * step 4a: go forward from working phase to non working
  1454. phase, increment in ptaps * */
  1455. if (sdr_nonworking_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
  1456. &i, &max_working_cnt, &work_end) == 0)
  1457. return 0;
  1458. /* ********************************************************* */
  1459. /* * step 5a: back off one from last, increment in dtaps * */
  1460. /* Special case code for backing up a phase */
  1461. if (p == 0) {
  1462. p = IO_DQS_EN_PHASE_MAX;
  1463. rw_mgr_decr_vfifo(grp, &v);
  1464. } else {
  1465. p = p - 1;
  1466. }
  1467. work_end -= IO_DELAY_PER_OPA_TAP;
  1468. scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
  1469. /* * The actual increment of dtaps is done outside of
  1470. the if/else loop to share code */
  1471. d = 0;
  1472. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p: \
  1473. vfifo=%u ptap=%u\n", __func__, __LINE__,
  1474. v, p);
  1475. } else {
  1476. /* ******************************************************* */
  1477. /* * step 3-5b: Find the right edge of the window using
  1478. delay taps * */
  1479. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase:vfifo=%u \
  1480. ptap=%u dtap=%u bgn=%u\n", __func__, __LINE__,
  1481. v, p, d, work_bgn);
  1482. work_end = work_bgn;
  1483. /* * The actual increment of dtaps is done outside of the
  1484. if/else loop to share code */
  1485. /* Only here to counterbalance a subtract later on which is
  1486. not needed if this branch of the algorithm is taken */
  1487. max_working_cnt++;
  1488. }
  1489. /* The dtap increment to find the failing edge is done here */
  1490. for (; d <= IO_DQS_EN_DELAY_MAX; d++, work_end +=
  1491. IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
  1492. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
  1493. end-2: dtap=%u\n", __func__, __LINE__, d);
  1494. scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
  1495. if (!rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
  1496. PASS_ONE_BIT,
  1497. &bit_chk, 0)) {
  1498. break;
  1499. }
  1500. }
  1501. /* Go back to working dtap */
  1502. if (d != 0)
  1503. work_end -= IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
  1504. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p/d: vfifo=%u \
  1505. ptap=%u dtap=%u end=%u\n", __func__, __LINE__,
  1506. v, p, d-1, work_end);
  1507. if (work_end < work_bgn) {
  1508. /* nil range */
  1509. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: end-2: \
  1510. failed\n", __func__, __LINE__);
  1511. return 0;
  1512. }
  1513. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: found range [%u,%u]\n",
  1514. __func__, __LINE__, work_bgn, work_end);
  1515. /* *************************************************************** */
  1516. /*
  1517. * * We need to calculate the number of dtaps that equal a ptap
  1518. * * To do that we'll back up a ptap and re-find the edge of the
  1519. * * window using dtaps
  1520. */
  1521. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: calculate dtaps_per_ptap \
  1522. for tracking\n", __func__, __LINE__);
  1523. /* Special case code for backing up a phase */
  1524. if (p == 0) {
  1525. p = IO_DQS_EN_PHASE_MAX;
  1526. rw_mgr_decr_vfifo(grp, &v);
  1527. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
  1528. cycle/phase: v=%u p=%u\n", __func__, __LINE__,
  1529. v, p);
  1530. } else {
  1531. p = p - 1;
  1532. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
  1533. phase only: v=%u p=%u", __func__, __LINE__,
  1534. v, p);
  1535. }
  1536. scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
  1537. /*
  1538. * Increase dtap until we first see a passing read (in case the
  1539. * window is smaller than a ptap),
  1540. * and then a failing read to mark the edge of the window again
  1541. */
  1542. /* Find a passing read */
  1543. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find passing read\n",
  1544. __func__, __LINE__);
  1545. found_passing_read = 0;
  1546. found_failing_read = 0;
  1547. initial_failing_dtap = d;
  1548. for (; d <= IO_DQS_EN_DELAY_MAX; d++) {
  1549. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: testing \
  1550. read d=%u\n", __func__, __LINE__, d);
  1551. scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
  1552. if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
  1553. PASS_ONE_BIT,
  1554. &bit_chk, 0)) {
  1555. found_passing_read = 1;
  1556. break;
  1557. }
  1558. }
  1559. if (found_passing_read) {
  1560. /* Find a failing read */
  1561. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find failing \
  1562. read\n", __func__, __LINE__);
  1563. for (d = d + 1; d <= IO_DQS_EN_DELAY_MAX; d++) {
  1564. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
  1565. testing read d=%u\n", __func__, __LINE__, d);
  1566. scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
  1567. if (!rw_mgr_mem_calibrate_read_test_all_ranks
  1568. (grp, 1, PASS_ONE_BIT, &bit_chk, 0)) {
  1569. found_failing_read = 1;
  1570. break;
  1571. }
  1572. }
  1573. } else {
  1574. debug_cond(DLEVEL == 1, "%s:%d find_dqs_en_phase: failed to \
  1575. calculate dtaps", __func__, __LINE__);
  1576. debug_cond(DLEVEL == 1, "per ptap. Fall back on static value\n");
  1577. }
  1578. /*
  1579. * The dynamically calculated dtaps_per_ptap is only valid if we
  1580. * found a passing/failing read. If we didn't, it means d hit the max
  1581. * (IO_DQS_EN_DELAY_MAX). Otherwise, dtaps_per_ptap retains its
  1582. * statically calculated value.
  1583. */
  1584. if (found_passing_read && found_failing_read)
  1585. dtaps_per_ptap = d - initial_failing_dtap;
  1586. addr = (u32)&sdr_reg_file->dtaps_per_ptap;
  1587. writel(dtaps_per_ptap, SOCFPGA_SDR_ADDRESS + addr);
  1588. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: dtaps_per_ptap=%u \
  1589. - %u = %u", __func__, __LINE__, d,
  1590. initial_failing_dtap, dtaps_per_ptap);
  1591. /* ******************************************** */
  1592. /* * step 6: Find the centre of the window * */
  1593. if (sdr_find_window_centre(&grp, &bit_chk, &work_bgn, &v, &d, &p,
  1594. &work_mid, &work_end) == 0)
  1595. return 0;
  1596. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center found: \
  1597. vfifo=%u ptap=%u dtap=%u\n", __func__, __LINE__,
  1598. v, p-1, d);
  1599. return 1;
  1600. }
  1601. /*
  1602. * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
  1603. * dq_in_delay values
  1604. */
  1605. static uint32_t
  1606. rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
  1607. (uint32_t write_group, uint32_t read_group, uint32_t test_bgn)
  1608. {
  1609. uint32_t found;
  1610. uint32_t i;
  1611. uint32_t p;
  1612. uint32_t d;
  1613. uint32_t r;
  1614. uint32_t addr;
  1615. const uint32_t delay_step = IO_IO_IN_DELAY_MAX /
  1616. (RW_MGR_MEM_DQ_PER_READ_DQS-1);
  1617. /* we start at zero, so have one less dq to devide among */
  1618. debug("%s:%d (%u,%u,%u)", __func__, __LINE__, write_group, read_group,
  1619. test_bgn);
  1620. /* try different dq_in_delays since the dq path is shorter than dqs */
  1621. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  1622. r += NUM_RANKS_PER_SHADOW_REG) {
  1623. for (i = 0, p = test_bgn, d = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS;
  1624. i++, p++, d += delay_step) {
  1625. debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_\
  1626. vfifo_find_dqs_", __func__, __LINE__);
  1627. debug_cond(DLEVEL == 1, "en_phase_sweep_dq_in_delay: g=%u/%u ",
  1628. write_group, read_group);
  1629. debug_cond(DLEVEL == 1, "r=%u, i=%u p=%u d=%u\n", r, i , p, d);
  1630. scc_mgr_set_dq_in_delay(write_group, p, d);
  1631. scc_mgr_load_dq(p);
  1632. }
  1633. addr = (u32)&sdr_scc_mgr->update;
  1634. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1635. }
  1636. found = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(read_group);
  1637. debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_vfifo_find_dqs_\
  1638. en_phase_sweep_dq", __func__, __LINE__);
  1639. debug_cond(DLEVEL == 1, "_in_delay: g=%u/%u found=%u; Reseting delay \
  1640. chain to zero\n", write_group, read_group, found);
  1641. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  1642. r += NUM_RANKS_PER_SHADOW_REG) {
  1643. for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS;
  1644. i++, p++) {
  1645. scc_mgr_set_dq_in_delay(write_group, p, 0);
  1646. scc_mgr_load_dq(p);
  1647. }
  1648. addr = (u32)&sdr_scc_mgr->update;
  1649. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1650. }
  1651. return found;
  1652. }
  1653. /* per-bit deskew DQ and center */
  1654. static uint32_t rw_mgr_mem_calibrate_vfifo_center(uint32_t rank_bgn,
  1655. uint32_t write_group, uint32_t read_group, uint32_t test_bgn,
  1656. uint32_t use_read_test, uint32_t update_fom)
  1657. {
  1658. uint32_t i, p, d, min_index;
  1659. /*
  1660. * Store these as signed since there are comparisons with
  1661. * signed numbers.
  1662. */
  1663. uint32_t bit_chk;
  1664. uint32_t sticky_bit_chk;
  1665. int32_t left_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
  1666. int32_t right_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
  1667. int32_t final_dq[RW_MGR_MEM_DQ_PER_READ_DQS];
  1668. int32_t mid;
  1669. int32_t orig_mid_min, mid_min;
  1670. int32_t new_dqs, start_dqs, start_dqs_en, shift_dq, final_dqs,
  1671. final_dqs_en;
  1672. int32_t dq_margin, dqs_margin;
  1673. uint32_t stop;
  1674. uint32_t temp_dq_in_delay1, temp_dq_in_delay2;
  1675. uint32_t addr;
  1676. debug("%s:%d: %u %u", __func__, __LINE__, read_group, test_bgn);
  1677. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_IN_DELAY_OFFSET;
  1678. start_dqs = readl(SOCFPGA_SDR_ADDRESS + addr + (read_group << 2));
  1679. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
  1680. start_dqs_en = readl(SOCFPGA_SDR_ADDRESS + addr + ((read_group << 2)
  1681. - IO_DQS_EN_DELAY_OFFSET));
  1682. /* set the left and right edge of each bit to an illegal value */
  1683. /* use (IO_IO_IN_DELAY_MAX + 1) as an illegal value */
  1684. sticky_bit_chk = 0;
  1685. for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1686. left_edge[i] = IO_IO_IN_DELAY_MAX + 1;
  1687. right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
  1688. }
  1689. addr = (u32)&sdr_scc_mgr->update;
  1690. /* Search for the left edge of the window for each bit */
  1691. for (d = 0; d <= IO_IO_IN_DELAY_MAX; d++) {
  1692. scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, d);
  1693. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1694. /*
  1695. * Stop searching when the read test doesn't pass AND when
  1696. * we've seen a passing read on every bit.
  1697. */
  1698. if (use_read_test) {
  1699. stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
  1700. read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
  1701. &bit_chk, 0, 0);
  1702. } else {
  1703. rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
  1704. 0, PASS_ONE_BIT,
  1705. &bit_chk, 0);
  1706. bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
  1707. (read_group - (write_group *
  1708. RW_MGR_MEM_IF_READ_DQS_WIDTH /
  1709. RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
  1710. stop = (bit_chk == 0);
  1711. }
  1712. sticky_bit_chk = sticky_bit_chk | bit_chk;
  1713. stop = stop && (sticky_bit_chk == param->read_correct_mask);
  1714. debug_cond(DLEVEL == 2, "%s:%d vfifo_center(left): dtap=%u => %u == %u \
  1715. && %u", __func__, __LINE__, d,
  1716. sticky_bit_chk,
  1717. param->read_correct_mask, stop);
  1718. if (stop == 1) {
  1719. break;
  1720. } else {
  1721. for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1722. if (bit_chk & 1) {
  1723. /* Remember a passing test as the
  1724. left_edge */
  1725. left_edge[i] = d;
  1726. } else {
  1727. /* If a left edge has not been seen yet,
  1728. then a future passing test will mark
  1729. this edge as the right edge */
  1730. if (left_edge[i] ==
  1731. IO_IO_IN_DELAY_MAX + 1) {
  1732. right_edge[i] = -(d + 1);
  1733. }
  1734. }
  1735. bit_chk = bit_chk >> 1;
  1736. }
  1737. }
  1738. }
  1739. /* Reset DQ delay chains to 0 */
  1740. scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, 0);
  1741. sticky_bit_chk = 0;
  1742. for (i = RW_MGR_MEM_DQ_PER_READ_DQS - 1;; i--) {
  1743. debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
  1744. %d right_edge[%u]: %d\n", __func__, __LINE__,
  1745. i, left_edge[i], i, right_edge[i]);
  1746. /*
  1747. * Check for cases where we haven't found the left edge,
  1748. * which makes our assignment of the the right edge invalid.
  1749. * Reset it to the illegal value.
  1750. */
  1751. if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) && (
  1752. right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
  1753. right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
  1754. debug_cond(DLEVEL == 2, "%s:%d vfifo_center: reset \
  1755. right_edge[%u]: %d\n", __func__, __LINE__,
  1756. i, right_edge[i]);
  1757. }
  1758. /*
  1759. * Reset sticky bit (except for bits where we have seen
  1760. * both the left and right edge).
  1761. */
  1762. sticky_bit_chk = sticky_bit_chk << 1;
  1763. if ((left_edge[i] != IO_IO_IN_DELAY_MAX + 1) &&
  1764. (right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
  1765. sticky_bit_chk = sticky_bit_chk | 1;
  1766. }
  1767. if (i == 0)
  1768. break;
  1769. }
  1770. addr = (u32)&sdr_scc_mgr->update;
  1771. /* Search for the right edge of the window for each bit */
  1772. for (d = 0; d <= IO_DQS_IN_DELAY_MAX - start_dqs; d++) {
  1773. scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
  1774. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
  1775. uint32_t delay = d + start_dqs_en;
  1776. if (delay > IO_DQS_EN_DELAY_MAX)
  1777. delay = IO_DQS_EN_DELAY_MAX;
  1778. scc_mgr_set_dqs_en_delay(read_group, delay);
  1779. }
  1780. scc_mgr_load_dqs(read_group);
  1781. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1782. /*
  1783. * Stop searching when the read test doesn't pass AND when
  1784. * we've seen a passing read on every bit.
  1785. */
  1786. if (use_read_test) {
  1787. stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
  1788. read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
  1789. &bit_chk, 0, 0);
  1790. } else {
  1791. rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
  1792. 0, PASS_ONE_BIT,
  1793. &bit_chk, 0);
  1794. bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
  1795. (read_group - (write_group *
  1796. RW_MGR_MEM_IF_READ_DQS_WIDTH /
  1797. RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
  1798. stop = (bit_chk == 0);
  1799. }
  1800. sticky_bit_chk = sticky_bit_chk | bit_chk;
  1801. stop = stop && (sticky_bit_chk == param->read_correct_mask);
  1802. debug_cond(DLEVEL == 2, "%s:%d vfifo_center(right): dtap=%u => %u == \
  1803. %u && %u", __func__, __LINE__, d,
  1804. sticky_bit_chk, param->read_correct_mask, stop);
  1805. if (stop == 1) {
  1806. break;
  1807. } else {
  1808. for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1809. if (bit_chk & 1) {
  1810. /* Remember a passing test as
  1811. the right_edge */
  1812. right_edge[i] = d;
  1813. } else {
  1814. if (d != 0) {
  1815. /* If a right edge has not been
  1816. seen yet, then a future passing
  1817. test will mark this edge as the
  1818. left edge */
  1819. if (right_edge[i] ==
  1820. IO_IO_IN_DELAY_MAX + 1) {
  1821. left_edge[i] = -(d + 1);
  1822. }
  1823. } else {
  1824. /* d = 0 failed, but it passed
  1825. when testing the left edge,
  1826. so it must be marginal,
  1827. set it to -1 */
  1828. if (right_edge[i] ==
  1829. IO_IO_IN_DELAY_MAX + 1 &&
  1830. left_edge[i] !=
  1831. IO_IO_IN_DELAY_MAX
  1832. + 1) {
  1833. right_edge[i] = -1;
  1834. }
  1835. /* If a right edge has not been
  1836. seen yet, then a future passing
  1837. test will mark this edge as the
  1838. left edge */
  1839. else if (right_edge[i] ==
  1840. IO_IO_IN_DELAY_MAX +
  1841. 1) {
  1842. left_edge[i] = -(d + 1);
  1843. }
  1844. }
  1845. }
  1846. debug_cond(DLEVEL == 2, "%s:%d vfifo_center[r,\
  1847. d=%u]: ", __func__, __LINE__, d);
  1848. debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d ",
  1849. (int)(bit_chk & 1), i, left_edge[i]);
  1850. debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
  1851. right_edge[i]);
  1852. bit_chk = bit_chk >> 1;
  1853. }
  1854. }
  1855. }
  1856. /* Check that all bits have a window */
  1857. addr = (u32)&sdr_scc_mgr->update;
  1858. for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1859. debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
  1860. %d right_edge[%u]: %d", __func__, __LINE__,
  1861. i, left_edge[i], i, right_edge[i]);
  1862. if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) || (right_edge[i]
  1863. == IO_IO_IN_DELAY_MAX + 1)) {
  1864. /*
  1865. * Restore delay chain settings before letting the loop
  1866. * in rw_mgr_mem_calibrate_vfifo to retry different
  1867. * dqs/ck relationships.
  1868. */
  1869. scc_mgr_set_dqs_bus_in_delay(read_group, start_dqs);
  1870. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
  1871. scc_mgr_set_dqs_en_delay(read_group,
  1872. start_dqs_en);
  1873. }
  1874. scc_mgr_load_dqs(read_group);
  1875. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1876. debug_cond(DLEVEL == 1, "%s:%d vfifo_center: failed to \
  1877. find edge [%u]: %d %d", __func__, __LINE__,
  1878. i, left_edge[i], right_edge[i]);
  1879. if (use_read_test) {
  1880. set_failing_group_stage(read_group *
  1881. RW_MGR_MEM_DQ_PER_READ_DQS + i,
  1882. CAL_STAGE_VFIFO,
  1883. CAL_SUBSTAGE_VFIFO_CENTER);
  1884. } else {
  1885. set_failing_group_stage(read_group *
  1886. RW_MGR_MEM_DQ_PER_READ_DQS + i,
  1887. CAL_STAGE_VFIFO_AFTER_WRITES,
  1888. CAL_SUBSTAGE_VFIFO_CENTER);
  1889. }
  1890. return 0;
  1891. }
  1892. }
  1893. /* Find middle of window for each DQ bit */
  1894. mid_min = left_edge[0] - right_edge[0];
  1895. min_index = 0;
  1896. for (i = 1; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1897. mid = left_edge[i] - right_edge[i];
  1898. if (mid < mid_min) {
  1899. mid_min = mid;
  1900. min_index = i;
  1901. }
  1902. }
  1903. /*
  1904. * -mid_min/2 represents the amount that we need to move DQS.
  1905. * If mid_min is odd and positive we'll need to add one to
  1906. * make sure the rounding in further calculations is correct
  1907. * (always bias to the right), so just add 1 for all positive values.
  1908. */
  1909. if (mid_min > 0)
  1910. mid_min++;
  1911. mid_min = mid_min / 2;
  1912. debug_cond(DLEVEL == 1, "%s:%d vfifo_center: mid_min=%d (index=%u)\n",
  1913. __func__, __LINE__, mid_min, min_index);
  1914. /* Determine the amount we can change DQS (which is -mid_min) */
  1915. orig_mid_min = mid_min;
  1916. new_dqs = start_dqs - mid_min;
  1917. if (new_dqs > IO_DQS_IN_DELAY_MAX)
  1918. new_dqs = IO_DQS_IN_DELAY_MAX;
  1919. else if (new_dqs < 0)
  1920. new_dqs = 0;
  1921. mid_min = start_dqs - new_dqs;
  1922. debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
  1923. mid_min, new_dqs);
  1924. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
  1925. if (start_dqs_en - mid_min > IO_DQS_EN_DELAY_MAX)
  1926. mid_min += start_dqs_en - mid_min - IO_DQS_EN_DELAY_MAX;
  1927. else if (start_dqs_en - mid_min < 0)
  1928. mid_min += start_dqs_en - mid_min;
  1929. }
  1930. new_dqs = start_dqs - mid_min;
  1931. debug_cond(DLEVEL == 1, "vfifo_center: start_dqs=%d start_dqs_en=%d \
  1932. new_dqs=%d mid_min=%d\n", start_dqs,
  1933. IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS ? start_dqs_en : -1,
  1934. new_dqs, mid_min);
  1935. /* Initialize data for export structures */
  1936. dqs_margin = IO_IO_IN_DELAY_MAX + 1;
  1937. dq_margin = IO_IO_IN_DELAY_MAX + 1;
  1938. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
  1939. /* add delay to bring centre of all DQ windows to the same "level" */
  1940. for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
  1941. /* Use values before divide by 2 to reduce round off error */
  1942. shift_dq = (left_edge[i] - right_edge[i] -
  1943. (left_edge[min_index] - right_edge[min_index]))/2 +
  1944. (orig_mid_min - mid_min);
  1945. debug_cond(DLEVEL == 2, "vfifo_center: before: \
  1946. shift_dq[%u]=%d\n", i, shift_dq);
  1947. temp_dq_in_delay1 = readl(SOCFPGA_SDR_ADDRESS + addr + (p << 2));
  1948. temp_dq_in_delay2 = readl(SOCFPGA_SDR_ADDRESS + addr + (i << 2));
  1949. if (shift_dq + (int32_t)temp_dq_in_delay1 >
  1950. (int32_t)IO_IO_IN_DELAY_MAX) {
  1951. shift_dq = (int32_t)IO_IO_IN_DELAY_MAX - temp_dq_in_delay2;
  1952. } else if (shift_dq + (int32_t)temp_dq_in_delay1 < 0) {
  1953. shift_dq = -(int32_t)temp_dq_in_delay1;
  1954. }
  1955. debug_cond(DLEVEL == 2, "vfifo_center: after: \
  1956. shift_dq[%u]=%d\n", i, shift_dq);
  1957. final_dq[i] = temp_dq_in_delay1 + shift_dq;
  1958. scc_mgr_set_dq_in_delay(write_group, p, final_dq[i]);
  1959. scc_mgr_load_dq(p);
  1960. debug_cond(DLEVEL == 2, "vfifo_center: margin[%u]=[%d,%d]\n", i,
  1961. left_edge[i] - shift_dq + (-mid_min),
  1962. right_edge[i] + shift_dq - (-mid_min));
  1963. /* To determine values for export structures */
  1964. if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
  1965. dq_margin = left_edge[i] - shift_dq + (-mid_min);
  1966. if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
  1967. dqs_margin = right_edge[i] + shift_dq - (-mid_min);
  1968. }
  1969. final_dqs = new_dqs;
  1970. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
  1971. final_dqs_en = start_dqs_en - mid_min;
  1972. /* Move DQS-en */
  1973. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
  1974. scc_mgr_set_dqs_en_delay(read_group, final_dqs_en);
  1975. scc_mgr_load_dqs(read_group);
  1976. }
  1977. /* Move DQS */
  1978. scc_mgr_set_dqs_bus_in_delay(read_group, final_dqs);
  1979. scc_mgr_load_dqs(read_group);
  1980. debug_cond(DLEVEL == 2, "%s:%d vfifo_center: dq_margin=%d \
  1981. dqs_margin=%d", __func__, __LINE__,
  1982. dq_margin, dqs_margin);
  1983. /*
  1984. * Do not remove this line as it makes sure all of our decisions
  1985. * have been applied. Apply the update bit.
  1986. */
  1987. addr = (u32)&sdr_scc_mgr->update;
  1988. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  1989. return (dq_margin >= 0) && (dqs_margin >= 0);
  1990. }
  1991. /*
  1992. * calibrate the read valid prediction FIFO.
  1993. *
  1994. * - read valid prediction will consist of finding a good DQS enable phase,
  1995. * DQS enable delay, DQS input phase, and DQS input delay.
  1996. * - we also do a per-bit deskew on the DQ lines.
  1997. */
  1998. static uint32_t rw_mgr_mem_calibrate_vfifo(uint32_t read_group,
  1999. uint32_t test_bgn)
  2000. {
  2001. uint32_t p, d, rank_bgn, sr;
  2002. uint32_t dtaps_per_ptap;
  2003. uint32_t tmp_delay;
  2004. uint32_t bit_chk;
  2005. uint32_t grp_calibrated;
  2006. uint32_t write_group, write_test_bgn;
  2007. uint32_t failed_substage;
  2008. debug("%s:%d: %u %u\n", __func__, __LINE__, read_group, test_bgn);
  2009. /* update info for sims */
  2010. reg_file_set_stage(CAL_STAGE_VFIFO);
  2011. write_group = read_group;
  2012. write_test_bgn = test_bgn;
  2013. /* USER Determine number of delay taps for each phase tap */
  2014. dtaps_per_ptap = 0;
  2015. tmp_delay = 0;
  2016. while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
  2017. dtaps_per_ptap++;
  2018. tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
  2019. }
  2020. dtaps_per_ptap--;
  2021. tmp_delay = 0;
  2022. /* update info for sims */
  2023. reg_file_set_group(read_group);
  2024. grp_calibrated = 0;
  2025. reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
  2026. failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
  2027. for (d = 0; d <= dtaps_per_ptap && grp_calibrated == 0; d += 2) {
  2028. /*
  2029. * In RLDRAMX we may be messing the delay of pins in
  2030. * the same write group but outside of the current read
  2031. * the group, but that's ok because we haven't
  2032. * calibrated output side yet.
  2033. */
  2034. if (d > 0) {
  2035. scc_mgr_apply_group_all_out_delay_add_all_ranks
  2036. (write_group, write_test_bgn, d);
  2037. }
  2038. for (p = 0; p <= IO_DQDQS_OUT_PHASE_MAX && grp_calibrated == 0;
  2039. p++) {
  2040. /* set a particular dqdqs phase */
  2041. scc_mgr_set_dqdqs_output_phase_all_ranks(read_group, p);
  2042. debug_cond(DLEVEL == 1, "%s:%d calibrate_vfifo: g=%u \
  2043. p=%u d=%u\n", __func__, __LINE__,
  2044. read_group, p, d);
  2045. /*
  2046. * Load up the patterns used by read calibration
  2047. * using current DQDQS phase.
  2048. */
  2049. rw_mgr_mem_calibrate_read_load_patterns(0, 1);
  2050. if (!(gbl->phy_debug_mode_flags &
  2051. PHY_DEBUG_DISABLE_GUARANTEED_READ)) {
  2052. if (!rw_mgr_mem_calibrate_read_test_patterns_all_ranks
  2053. (read_group, 1, &bit_chk)) {
  2054. debug_cond(DLEVEL == 1, "%s:%d Guaranteed read test failed:",
  2055. __func__, __LINE__);
  2056. debug_cond(DLEVEL == 1, " g=%u p=%u d=%u\n",
  2057. read_group, p, d);
  2058. break;
  2059. }
  2060. }
  2061. /* case:56390 */
  2062. grp_calibrated = 1;
  2063. if (rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
  2064. (write_group, read_group, test_bgn)) {
  2065. /*
  2066. * USER Read per-bit deskew can be done on a
  2067. * per shadow register basis.
  2068. */
  2069. for (rank_bgn = 0, sr = 0;
  2070. rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
  2071. rank_bgn += NUM_RANKS_PER_SHADOW_REG,
  2072. ++sr) {
  2073. /*
  2074. * Determine if this set of ranks
  2075. * should be skipped entirely.
  2076. */
  2077. if (!param->skip_shadow_regs[sr]) {
  2078. /*
  2079. * If doing read after write
  2080. * calibration, do not update
  2081. * FOM, now - do it then.
  2082. */
  2083. if (!rw_mgr_mem_calibrate_vfifo_center
  2084. (rank_bgn, write_group,
  2085. read_group, test_bgn, 1, 0)) {
  2086. grp_calibrated = 0;
  2087. failed_substage =
  2088. CAL_SUBSTAGE_VFIFO_CENTER;
  2089. }
  2090. }
  2091. }
  2092. } else {
  2093. grp_calibrated = 0;
  2094. failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
  2095. }
  2096. }
  2097. }
  2098. if (grp_calibrated == 0) {
  2099. set_failing_group_stage(write_group, CAL_STAGE_VFIFO,
  2100. failed_substage);
  2101. return 0;
  2102. }
  2103. /*
  2104. * Reset the delay chains back to zero if they have moved > 1
  2105. * (check for > 1 because loop will increase d even when pass in
  2106. * first case).
  2107. */
  2108. if (d > 2)
  2109. scc_mgr_zero_group(write_group, write_test_bgn, 1);
  2110. return 1;
  2111. }
  2112. /* VFIFO Calibration -- Read Deskew Calibration after write deskew */
  2113. static uint32_t rw_mgr_mem_calibrate_vfifo_end(uint32_t read_group,
  2114. uint32_t test_bgn)
  2115. {
  2116. uint32_t rank_bgn, sr;
  2117. uint32_t grp_calibrated;
  2118. uint32_t write_group;
  2119. debug("%s:%d %u %u", __func__, __LINE__, read_group, test_bgn);
  2120. /* update info for sims */
  2121. reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
  2122. reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
  2123. write_group = read_group;
  2124. /* update info for sims */
  2125. reg_file_set_group(read_group);
  2126. grp_calibrated = 1;
  2127. /* Read per-bit deskew can be done on a per shadow register basis */
  2128. for (rank_bgn = 0, sr = 0; rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
  2129. rank_bgn += NUM_RANKS_PER_SHADOW_REG, ++sr) {
  2130. /* Determine if this set of ranks should be skipped entirely */
  2131. if (!param->skip_shadow_regs[sr]) {
  2132. /* This is the last calibration round, update FOM here */
  2133. if (!rw_mgr_mem_calibrate_vfifo_center(rank_bgn,
  2134. write_group,
  2135. read_group,
  2136. test_bgn, 0,
  2137. 1)) {
  2138. grp_calibrated = 0;
  2139. }
  2140. }
  2141. }
  2142. if (grp_calibrated == 0) {
  2143. set_failing_group_stage(write_group,
  2144. CAL_STAGE_VFIFO_AFTER_WRITES,
  2145. CAL_SUBSTAGE_VFIFO_CENTER);
  2146. return 0;
  2147. }
  2148. return 1;
  2149. }
  2150. /* Calibrate LFIFO to find smallest read latency */
  2151. static uint32_t rw_mgr_mem_calibrate_lfifo(void)
  2152. {
  2153. uint32_t found_one;
  2154. uint32_t bit_chk;
  2155. uint32_t addr;
  2156. debug("%s:%d\n", __func__, __LINE__);
  2157. /* update info for sims */
  2158. reg_file_set_stage(CAL_STAGE_LFIFO);
  2159. reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
  2160. /* Load up the patterns used by read calibration for all ranks */
  2161. rw_mgr_mem_calibrate_read_load_patterns(0, 1);
  2162. found_one = 0;
  2163. addr = (u32)&phy_mgr_cfg->phy_rlat;
  2164. do {
  2165. writel(gbl->curr_read_lat, SOCFPGA_SDR_ADDRESS + addr);
  2166. debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
  2167. __func__, __LINE__, gbl->curr_read_lat);
  2168. if (!rw_mgr_mem_calibrate_read_test_all_ranks(0,
  2169. NUM_READ_TESTS,
  2170. PASS_ALL_BITS,
  2171. &bit_chk, 1)) {
  2172. break;
  2173. }
  2174. found_one = 1;
  2175. /* reduce read latency and see if things are working */
  2176. /* correctly */
  2177. gbl->curr_read_lat--;
  2178. } while (gbl->curr_read_lat > 0);
  2179. /* reset the fifos to get pointers to known state */
  2180. addr = (u32)&phy_mgr_cmd->fifo_reset;
  2181. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2182. if (found_one) {
  2183. /* add a fudge factor to the read latency that was determined */
  2184. gbl->curr_read_lat += 2;
  2185. addr = (u32)&phy_mgr_cfg->phy_rlat;
  2186. writel(gbl->curr_read_lat, SOCFPGA_SDR_ADDRESS + addr);
  2187. debug_cond(DLEVEL == 2, "%s:%d lfifo: success: using \
  2188. read_lat=%u\n", __func__, __LINE__,
  2189. gbl->curr_read_lat);
  2190. return 1;
  2191. } else {
  2192. set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
  2193. CAL_SUBSTAGE_READ_LATENCY);
  2194. debug_cond(DLEVEL == 2, "%s:%d lfifo: failed at initial \
  2195. read_lat=%u\n", __func__, __LINE__,
  2196. gbl->curr_read_lat);
  2197. return 0;
  2198. }
  2199. }
  2200. /*
  2201. * issue write test command.
  2202. * two variants are provided. one that just tests a write pattern and
  2203. * another that tests datamask functionality.
  2204. */
  2205. static void rw_mgr_mem_calibrate_write_test_issue(uint32_t group,
  2206. uint32_t test_dm)
  2207. {
  2208. uint32_t mcc_instruction;
  2209. uint32_t quick_write_mode = (((STATIC_CALIB_STEPS) & CALIB_SKIP_WRITES) &&
  2210. ENABLE_SUPER_QUICK_CALIBRATION);
  2211. uint32_t rw_wl_nop_cycles;
  2212. uint32_t addr;
  2213. /*
  2214. * Set counter and jump addresses for the right
  2215. * number of NOP cycles.
  2216. * The number of supported NOP cycles can range from -1 to infinity
  2217. * Three different cases are handled:
  2218. *
  2219. * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
  2220. * mechanism will be used to insert the right number of NOPs
  2221. *
  2222. * 2. For a number of NOP cycles equals to 0, the micro-instruction
  2223. * issuing the write command will jump straight to the
  2224. * micro-instruction that turns on DQS (for DDRx), or outputs write
  2225. * data (for RLD), skipping
  2226. * the NOP micro-instruction all together
  2227. *
  2228. * 3. A number of NOP cycles equal to -1 indicates that DQS must be
  2229. * turned on in the same micro-instruction that issues the write
  2230. * command. Then we need
  2231. * to directly jump to the micro-instruction that sends out the data
  2232. *
  2233. * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
  2234. * (2 and 3). One jump-counter (0) is used to perform multiple
  2235. * write-read operations.
  2236. * one counter left to issue this command in "multiple-group" mode
  2237. */
  2238. rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
  2239. if (rw_wl_nop_cycles == -1) {
  2240. /*
  2241. * CNTR 2 - We want to execute the special write operation that
  2242. * turns on DQS right away and then skip directly to the
  2243. * instruction that sends out the data. We set the counter to a
  2244. * large number so that the jump is always taken.
  2245. */
  2246. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr2;
  2247. writel(0xFF, SOCFPGA_SDR_ADDRESS + addr);
  2248. /* CNTR 3 - Not used */
  2249. if (test_dm) {
  2250. mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0_WL_1;
  2251. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  2252. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DATA,
  2253. SOCFPGA_SDR_ADDRESS + addr);
  2254. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add3;
  2255. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
  2256. SOCFPGA_SDR_ADDRESS + addr);
  2257. } else {
  2258. mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0_WL_1;
  2259. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  2260. writel(RW_MGR_LFSR_WR_RD_BANK_0_DATA, SOCFPGA_SDR_ADDRESS + addr);
  2261. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add3;
  2262. writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP, SOCFPGA_SDR_ADDRESS + addr);
  2263. }
  2264. } else if (rw_wl_nop_cycles == 0) {
  2265. /*
  2266. * CNTR 2 - We want to skip the NOP operation and go straight
  2267. * to the DQS enable instruction. We set the counter to a large
  2268. * number so that the jump is always taken.
  2269. */
  2270. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr2;
  2271. writel(0xFF, SOCFPGA_SDR_ADDRESS + addr);
  2272. /* CNTR 3 - Not used */
  2273. if (test_dm) {
  2274. mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
  2275. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  2276. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DQS,
  2277. SOCFPGA_SDR_ADDRESS + addr);
  2278. } else {
  2279. mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
  2280. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  2281. writel(RW_MGR_LFSR_WR_RD_BANK_0_DQS, SOCFPGA_SDR_ADDRESS + addr);
  2282. }
  2283. } else {
  2284. /*
  2285. * CNTR 2 - In this case we want to execute the next instruction
  2286. * and NOT take the jump. So we set the counter to 0. The jump
  2287. * address doesn't count.
  2288. */
  2289. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr2;
  2290. writel(0x0, SOCFPGA_SDR_ADDRESS + addr);
  2291. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add2;
  2292. writel(0x0, SOCFPGA_SDR_ADDRESS + addr);
  2293. /*
  2294. * CNTR 3 - Set the nop counter to the number of cycles we
  2295. * need to loop for, minus 1.
  2296. */
  2297. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr3;
  2298. writel(rw_wl_nop_cycles - 1, SOCFPGA_SDR_ADDRESS + addr);
  2299. if (test_dm) {
  2300. mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
  2301. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add3;
  2302. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP, SOCFPGA_SDR_ADDRESS + addr);
  2303. } else {
  2304. mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
  2305. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add3;
  2306. writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP, SOCFPGA_SDR_ADDRESS + addr);
  2307. }
  2308. }
  2309. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RESET_READ_DATAPATH_OFFSET;
  2310. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2311. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  2312. if (quick_write_mode)
  2313. writel(0x08, SOCFPGA_SDR_ADDRESS + addr);
  2314. else
  2315. writel(0x40, SOCFPGA_SDR_ADDRESS + addr);
  2316. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  2317. writel(mcc_instruction, SOCFPGA_SDR_ADDRESS + addr);
  2318. /*
  2319. * CNTR 1 - This is used to ensure enough time elapses
  2320. * for read data to come back.
  2321. */
  2322. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  2323. writel(0x30, SOCFPGA_SDR_ADDRESS + addr);
  2324. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  2325. if (test_dm) {
  2326. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_WAIT, SOCFPGA_SDR_ADDRESS + addr);
  2327. } else {
  2328. writel(RW_MGR_LFSR_WR_RD_BANK_0_WAIT, SOCFPGA_SDR_ADDRESS + addr);
  2329. }
  2330. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  2331. writel(mcc_instruction, SOCFPGA_SDR_ADDRESS + addr + (group << 2));
  2332. }
  2333. /* Test writes, can check for a single bit pass or multiple bit pass */
  2334. static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
  2335. uint32_t write_group, uint32_t use_dm, uint32_t all_correct,
  2336. uint32_t *bit_chk, uint32_t all_ranks)
  2337. {
  2338. uint32_t addr;
  2339. uint32_t r;
  2340. uint32_t correct_mask_vg;
  2341. uint32_t tmp_bit_chk;
  2342. uint32_t vg;
  2343. uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
  2344. (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
  2345. uint32_t addr_rw_mgr;
  2346. uint32_t base_rw_mgr;
  2347. *bit_chk = param->write_correct_mask;
  2348. correct_mask_vg = param->write_correct_mask_vg;
  2349. for (r = rank_bgn; r < rank_end; r++) {
  2350. if (param->skip_ranks[r]) {
  2351. /* request to skip the rank */
  2352. continue;
  2353. }
  2354. /* set rank */
  2355. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
  2356. tmp_bit_chk = 0;
  2357. addr = (u32)&phy_mgr_cmd->fifo_reset;
  2358. addr_rw_mgr = SDR_PHYGRP_RWMGRGRP_ADDRESS;
  2359. for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS-1; ; vg--) {
  2360. /* reset the fifos to get pointers to known state */
  2361. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2362. tmp_bit_chk = tmp_bit_chk <<
  2363. (RW_MGR_MEM_DQ_PER_WRITE_DQS /
  2364. RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
  2365. rw_mgr_mem_calibrate_write_test_issue(write_group *
  2366. RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS+vg,
  2367. use_dm);
  2368. base_rw_mgr = readl(SOCFPGA_SDR_ADDRESS + addr_rw_mgr);
  2369. tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
  2370. if (vg == 0)
  2371. break;
  2372. }
  2373. *bit_chk &= tmp_bit_chk;
  2374. }
  2375. if (all_correct) {
  2376. set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
  2377. debug_cond(DLEVEL == 2, "write_test(%u,%u,ALL) : %u == \
  2378. %u => %lu", write_group, use_dm,
  2379. *bit_chk, param->write_correct_mask,
  2380. (long unsigned int)(*bit_chk ==
  2381. param->write_correct_mask));
  2382. return *bit_chk == param->write_correct_mask;
  2383. } else {
  2384. set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
  2385. debug_cond(DLEVEL == 2, "write_test(%u,%u,ONE) : %u != ",
  2386. write_group, use_dm, *bit_chk);
  2387. debug_cond(DLEVEL == 2, "%lu" " => %lu", (long unsigned int)0,
  2388. (long unsigned int)(*bit_chk != 0));
  2389. return *bit_chk != 0x00;
  2390. }
  2391. }
  2392. /*
  2393. * center all windows. do per-bit-deskew to possibly increase size of
  2394. * certain windows.
  2395. */
  2396. static uint32_t rw_mgr_mem_calibrate_writes_center(uint32_t rank_bgn,
  2397. uint32_t write_group, uint32_t test_bgn)
  2398. {
  2399. uint32_t i, p, min_index;
  2400. int32_t d;
  2401. /*
  2402. * Store these as signed since there are comparisons with
  2403. * signed numbers.
  2404. */
  2405. uint32_t bit_chk;
  2406. uint32_t sticky_bit_chk;
  2407. int32_t left_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
  2408. int32_t right_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
  2409. int32_t mid;
  2410. int32_t mid_min, orig_mid_min;
  2411. int32_t new_dqs, start_dqs, shift_dq;
  2412. int32_t dq_margin, dqs_margin, dm_margin;
  2413. uint32_t stop;
  2414. uint32_t temp_dq_out1_delay;
  2415. uint32_t addr;
  2416. debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
  2417. dm_margin = 0;
  2418. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
  2419. start_dqs = readl(SOCFPGA_SDR_ADDRESS + addr +
  2420. (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
  2421. /* per-bit deskew */
  2422. /*
  2423. * set the left and right edge of each bit to an illegal value
  2424. * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
  2425. */
  2426. sticky_bit_chk = 0;
  2427. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2428. left_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
  2429. right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
  2430. }
  2431. /* Search for the left edge of the window for each bit */
  2432. addr = (u32)&sdr_scc_mgr->update;
  2433. for (d = 0; d <= IO_IO_OUT1_DELAY_MAX; d++) {
  2434. scc_mgr_apply_group_dq_out1_delay(write_group, test_bgn, d);
  2435. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2436. /*
  2437. * Stop searching when the read test doesn't pass AND when
  2438. * we've seen a passing read on every bit.
  2439. */
  2440. stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
  2441. 0, PASS_ONE_BIT, &bit_chk, 0);
  2442. sticky_bit_chk = sticky_bit_chk | bit_chk;
  2443. stop = stop && (sticky_bit_chk == param->write_correct_mask);
  2444. debug_cond(DLEVEL == 2, "write_center(left): dtap=%d => %u \
  2445. == %u && %u [bit_chk= %u ]\n",
  2446. d, sticky_bit_chk, param->write_correct_mask,
  2447. stop, bit_chk);
  2448. if (stop == 1) {
  2449. break;
  2450. } else {
  2451. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2452. if (bit_chk & 1) {
  2453. /*
  2454. * Remember a passing test as the
  2455. * left_edge.
  2456. */
  2457. left_edge[i] = d;
  2458. } else {
  2459. /*
  2460. * If a left edge has not been seen
  2461. * yet, then a future passing test will
  2462. * mark this edge as the right edge.
  2463. */
  2464. if (left_edge[i] ==
  2465. IO_IO_OUT1_DELAY_MAX + 1) {
  2466. right_edge[i] = -(d + 1);
  2467. }
  2468. }
  2469. debug_cond(DLEVEL == 2, "write_center[l,d=%d):", d);
  2470. debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
  2471. (int)(bit_chk & 1), i, left_edge[i]);
  2472. debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
  2473. right_edge[i]);
  2474. bit_chk = bit_chk >> 1;
  2475. }
  2476. }
  2477. }
  2478. /* Reset DQ delay chains to 0 */
  2479. scc_mgr_apply_group_dq_out1_delay(write_group, test_bgn, 0);
  2480. sticky_bit_chk = 0;
  2481. for (i = RW_MGR_MEM_DQ_PER_WRITE_DQS - 1;; i--) {
  2482. debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
  2483. %d right_edge[%u]: %d\n", __func__, __LINE__,
  2484. i, left_edge[i], i, right_edge[i]);
  2485. /*
  2486. * Check for cases where we haven't found the left edge,
  2487. * which makes our assignment of the the right edge invalid.
  2488. * Reset it to the illegal value.
  2489. */
  2490. if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) &&
  2491. (right_edge[i] != IO_IO_OUT1_DELAY_MAX + 1)) {
  2492. right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
  2493. debug_cond(DLEVEL == 2, "%s:%d write_center: reset \
  2494. right_edge[%u]: %d\n", __func__, __LINE__,
  2495. i, right_edge[i]);
  2496. }
  2497. /*
  2498. * Reset sticky bit (except for bits where we have
  2499. * seen the left edge).
  2500. */
  2501. sticky_bit_chk = sticky_bit_chk << 1;
  2502. if ((left_edge[i] != IO_IO_OUT1_DELAY_MAX + 1))
  2503. sticky_bit_chk = sticky_bit_chk | 1;
  2504. if (i == 0)
  2505. break;
  2506. }
  2507. /* Search for the right edge of the window for each bit */
  2508. addr = (u32)&sdr_scc_mgr->update;
  2509. for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - start_dqs; d++) {
  2510. scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
  2511. d + start_dqs);
  2512. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2513. /*
  2514. * Stop searching when the read test doesn't pass AND when
  2515. * we've seen a passing read on every bit.
  2516. */
  2517. stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
  2518. 0, PASS_ONE_BIT, &bit_chk, 0);
  2519. sticky_bit_chk = sticky_bit_chk | bit_chk;
  2520. stop = stop && (sticky_bit_chk == param->write_correct_mask);
  2521. debug_cond(DLEVEL == 2, "write_center (right): dtap=%u => %u == \
  2522. %u && %u\n", d, sticky_bit_chk,
  2523. param->write_correct_mask, stop);
  2524. if (stop == 1) {
  2525. if (d == 0) {
  2526. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS;
  2527. i++) {
  2528. /* d = 0 failed, but it passed when
  2529. testing the left edge, so it must be
  2530. marginal, set it to -1 */
  2531. if (right_edge[i] ==
  2532. IO_IO_OUT1_DELAY_MAX + 1 &&
  2533. left_edge[i] !=
  2534. IO_IO_OUT1_DELAY_MAX + 1) {
  2535. right_edge[i] = -1;
  2536. }
  2537. }
  2538. }
  2539. break;
  2540. } else {
  2541. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2542. if (bit_chk & 1) {
  2543. /*
  2544. * Remember a passing test as
  2545. * the right_edge.
  2546. */
  2547. right_edge[i] = d;
  2548. } else {
  2549. if (d != 0) {
  2550. /*
  2551. * If a right edge has not
  2552. * been seen yet, then a future
  2553. * passing test will mark this
  2554. * edge as the left edge.
  2555. */
  2556. if (right_edge[i] ==
  2557. IO_IO_OUT1_DELAY_MAX + 1)
  2558. left_edge[i] = -(d + 1);
  2559. } else {
  2560. /*
  2561. * d = 0 failed, but it passed
  2562. * when testing the left edge,
  2563. * so it must be marginal, set
  2564. * it to -1.
  2565. */
  2566. if (right_edge[i] ==
  2567. IO_IO_OUT1_DELAY_MAX + 1 &&
  2568. left_edge[i] !=
  2569. IO_IO_OUT1_DELAY_MAX + 1)
  2570. right_edge[i] = -1;
  2571. /*
  2572. * If a right edge has not been
  2573. * seen yet, then a future
  2574. * passing test will mark this
  2575. * edge as the left edge.
  2576. */
  2577. else if (right_edge[i] ==
  2578. IO_IO_OUT1_DELAY_MAX +
  2579. 1)
  2580. left_edge[i] = -(d + 1);
  2581. }
  2582. }
  2583. debug_cond(DLEVEL == 2, "write_center[r,d=%d):", d);
  2584. debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
  2585. (int)(bit_chk & 1), i, left_edge[i]);
  2586. debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
  2587. right_edge[i]);
  2588. bit_chk = bit_chk >> 1;
  2589. }
  2590. }
  2591. }
  2592. /* Check that all bits have a window */
  2593. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2594. debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
  2595. %d right_edge[%u]: %d", __func__, __LINE__,
  2596. i, left_edge[i], i, right_edge[i]);
  2597. if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) ||
  2598. (right_edge[i] == IO_IO_OUT1_DELAY_MAX + 1)) {
  2599. set_failing_group_stage(test_bgn + i,
  2600. CAL_STAGE_WRITES,
  2601. CAL_SUBSTAGE_WRITES_CENTER);
  2602. return 0;
  2603. }
  2604. }
  2605. /* Find middle of window for each DQ bit */
  2606. mid_min = left_edge[0] - right_edge[0];
  2607. min_index = 0;
  2608. for (i = 1; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2609. mid = left_edge[i] - right_edge[i];
  2610. if (mid < mid_min) {
  2611. mid_min = mid;
  2612. min_index = i;
  2613. }
  2614. }
  2615. /*
  2616. * -mid_min/2 represents the amount that we need to move DQS.
  2617. * If mid_min is odd and positive we'll need to add one to
  2618. * make sure the rounding in further calculations is correct
  2619. * (always bias to the right), so just add 1 for all positive values.
  2620. */
  2621. if (mid_min > 0)
  2622. mid_min++;
  2623. mid_min = mid_min / 2;
  2624. debug_cond(DLEVEL == 1, "%s:%d write_center: mid_min=%d\n", __func__,
  2625. __LINE__, mid_min);
  2626. /* Determine the amount we can change DQS (which is -mid_min) */
  2627. orig_mid_min = mid_min;
  2628. new_dqs = start_dqs;
  2629. mid_min = 0;
  2630. debug_cond(DLEVEL == 1, "%s:%d write_center: start_dqs=%d new_dqs=%d \
  2631. mid_min=%d\n", __func__, __LINE__, start_dqs, new_dqs, mid_min);
  2632. /* Initialize data for export structures */
  2633. dqs_margin = IO_IO_OUT1_DELAY_MAX + 1;
  2634. dq_margin = IO_IO_OUT1_DELAY_MAX + 1;
  2635. /* add delay to bring centre of all DQ windows to the same "level" */
  2636. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
  2637. for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
  2638. /* Use values before divide by 2 to reduce round off error */
  2639. shift_dq = (left_edge[i] - right_edge[i] -
  2640. (left_edge[min_index] - right_edge[min_index]))/2 +
  2641. (orig_mid_min - mid_min);
  2642. debug_cond(DLEVEL == 2, "%s:%d write_center: before: shift_dq \
  2643. [%u]=%d\n", __func__, __LINE__, i, shift_dq);
  2644. temp_dq_out1_delay = readl(SOCFPGA_SDR_ADDRESS + addr + (i << 2));
  2645. if (shift_dq + (int32_t)temp_dq_out1_delay >
  2646. (int32_t)IO_IO_OUT1_DELAY_MAX) {
  2647. shift_dq = (int32_t)IO_IO_OUT1_DELAY_MAX - temp_dq_out1_delay;
  2648. } else if (shift_dq + (int32_t)temp_dq_out1_delay < 0) {
  2649. shift_dq = -(int32_t)temp_dq_out1_delay;
  2650. }
  2651. debug_cond(DLEVEL == 2, "write_center: after: shift_dq[%u]=%d\n",
  2652. i, shift_dq);
  2653. scc_mgr_set_dq_out1_delay(write_group, i, temp_dq_out1_delay +
  2654. shift_dq);
  2655. scc_mgr_load_dq(i);
  2656. debug_cond(DLEVEL == 2, "write_center: margin[%u]=[%d,%d]\n", i,
  2657. left_edge[i] - shift_dq + (-mid_min),
  2658. right_edge[i] + shift_dq - (-mid_min));
  2659. /* To determine values for export structures */
  2660. if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
  2661. dq_margin = left_edge[i] - shift_dq + (-mid_min);
  2662. if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
  2663. dqs_margin = right_edge[i] + shift_dq - (-mid_min);
  2664. }
  2665. /* Move DQS */
  2666. scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
  2667. addr = (u32)&sdr_scc_mgr->update;
  2668. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2669. /* Centre DM */
  2670. debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
  2671. /*
  2672. * set the left and right edge of each bit to an illegal value,
  2673. * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value,
  2674. */
  2675. left_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
  2676. right_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
  2677. int32_t bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2678. int32_t end_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2679. int32_t bgn_best = IO_IO_OUT1_DELAY_MAX + 1;
  2680. int32_t end_best = IO_IO_OUT1_DELAY_MAX + 1;
  2681. int32_t win_best = 0;
  2682. /* Search for the/part of the window with DM shift */
  2683. addr = (u32)&sdr_scc_mgr->update;
  2684. for (d = IO_IO_OUT1_DELAY_MAX; d >= 0; d -= DELTA_D) {
  2685. scc_mgr_apply_group_dm_out1_delay(write_group, d);
  2686. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2687. if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
  2688. PASS_ALL_BITS, &bit_chk,
  2689. 0)) {
  2690. /* USE Set current end of the window */
  2691. end_curr = -d;
  2692. /*
  2693. * If a starting edge of our window has not been seen
  2694. * this is our current start of the DM window.
  2695. */
  2696. if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
  2697. bgn_curr = -d;
  2698. /*
  2699. * If current window is bigger than best seen.
  2700. * Set best seen to be current window.
  2701. */
  2702. if ((end_curr-bgn_curr+1) > win_best) {
  2703. win_best = end_curr-bgn_curr+1;
  2704. bgn_best = bgn_curr;
  2705. end_best = end_curr;
  2706. }
  2707. } else {
  2708. /* We just saw a failing test. Reset temp edge */
  2709. bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2710. end_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2711. }
  2712. }
  2713. /* Reset DM delay chains to 0 */
  2714. scc_mgr_apply_group_dm_out1_delay(write_group, 0);
  2715. /*
  2716. * Check to see if the current window nudges up aganist 0 delay.
  2717. * If so we need to continue the search by shifting DQS otherwise DQS
  2718. * search begins as a new search. */
  2719. if (end_curr != 0) {
  2720. bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2721. end_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2722. }
  2723. /* Search for the/part of the window with DQS shifts */
  2724. addr = (u32)&sdr_scc_mgr->update;
  2725. for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - new_dqs; d += DELTA_D) {
  2726. /*
  2727. * Note: This only shifts DQS, so are we limiting ourselve to
  2728. * width of DQ unnecessarily.
  2729. */
  2730. scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
  2731. d + new_dqs);
  2732. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2733. if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
  2734. PASS_ALL_BITS, &bit_chk,
  2735. 0)) {
  2736. /* USE Set current end of the window */
  2737. end_curr = d;
  2738. /*
  2739. * If a beginning edge of our window has not been seen
  2740. * this is our current begin of the DM window.
  2741. */
  2742. if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
  2743. bgn_curr = d;
  2744. /*
  2745. * If current window is bigger than best seen. Set best
  2746. * seen to be current window.
  2747. */
  2748. if ((end_curr-bgn_curr+1) > win_best) {
  2749. win_best = end_curr-bgn_curr+1;
  2750. bgn_best = bgn_curr;
  2751. end_best = end_curr;
  2752. }
  2753. } else {
  2754. /* We just saw a failing test. Reset temp edge */
  2755. bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2756. end_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2757. /* Early exit optimization: if ther remaining delay
  2758. chain space is less than already seen largest window
  2759. we can exit */
  2760. if ((win_best-1) >
  2761. (IO_IO_OUT1_DELAY_MAX - new_dqs - d)) {
  2762. break;
  2763. }
  2764. }
  2765. }
  2766. /* assign left and right edge for cal and reporting; */
  2767. left_edge[0] = -1*bgn_best;
  2768. right_edge[0] = end_best;
  2769. debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n", __func__,
  2770. __LINE__, left_edge[0], right_edge[0]);
  2771. /* Move DQS (back to orig) */
  2772. scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
  2773. /* Move DM */
  2774. /* Find middle of window for the DM bit */
  2775. mid = (left_edge[0] - right_edge[0]) / 2;
  2776. /* only move right, since we are not moving DQS/DQ */
  2777. if (mid < 0)
  2778. mid = 0;
  2779. /* dm_marign should fail if we never find a window */
  2780. if (win_best == 0)
  2781. dm_margin = -1;
  2782. else
  2783. dm_margin = left_edge[0] - mid;
  2784. scc_mgr_apply_group_dm_out1_delay(write_group, mid);
  2785. addr = (u32)&sdr_scc_mgr->update;
  2786. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2787. debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d mid=%d \
  2788. dm_margin=%d\n", __func__, __LINE__, left_edge[0],
  2789. right_edge[0], mid, dm_margin);
  2790. /* Export values */
  2791. gbl->fom_out += dq_margin + dqs_margin;
  2792. debug_cond(DLEVEL == 2, "%s:%d write_center: dq_margin=%d \
  2793. dqs_margin=%d dm_margin=%d\n", __func__, __LINE__,
  2794. dq_margin, dqs_margin, dm_margin);
  2795. /*
  2796. * Do not remove this line as it makes sure all of our
  2797. * decisions have been applied.
  2798. */
  2799. addr = (u32)&sdr_scc_mgr->update;
  2800. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2801. return (dq_margin >= 0) && (dqs_margin >= 0) && (dm_margin >= 0);
  2802. }
  2803. /* calibrate the write operations */
  2804. static uint32_t rw_mgr_mem_calibrate_writes(uint32_t rank_bgn, uint32_t g,
  2805. uint32_t test_bgn)
  2806. {
  2807. /* update info for sims */
  2808. debug("%s:%d %u %u\n", __func__, __LINE__, g, test_bgn);
  2809. reg_file_set_stage(CAL_STAGE_WRITES);
  2810. reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
  2811. reg_file_set_group(g);
  2812. if (!rw_mgr_mem_calibrate_writes_center(rank_bgn, g, test_bgn)) {
  2813. set_failing_group_stage(g, CAL_STAGE_WRITES,
  2814. CAL_SUBSTAGE_WRITES_CENTER);
  2815. return 0;
  2816. }
  2817. return 1;
  2818. }
  2819. /* precharge all banks and activate row 0 in bank "000..." and bank "111..." */
  2820. static void mem_precharge_and_activate(void)
  2821. {
  2822. uint32_t r;
  2823. uint32_t addr;
  2824. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
  2825. if (param->skip_ranks[r]) {
  2826. /* request to skip the rank */
  2827. continue;
  2828. }
  2829. /* set rank */
  2830. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
  2831. /* precharge all banks ... */
  2832. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  2833. writel(RW_MGR_PRECHARGE_ALL, SOCFPGA_SDR_ADDRESS + addr);
  2834. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr0;
  2835. writel(0x0F, SOCFPGA_SDR_ADDRESS + addr);
  2836. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add0;
  2837. writel(RW_MGR_ACTIVATE_0_AND_1_WAIT1, SOCFPGA_SDR_ADDRESS + addr);
  2838. addr = (u32)&sdr_rw_load_mgr_regs->load_cntr1;
  2839. writel(0x0F, SOCFPGA_SDR_ADDRESS + addr);
  2840. addr = (u32)&sdr_rw_load_jump_mgr_regs->load_jump_add1;
  2841. writel(RW_MGR_ACTIVATE_0_AND_1_WAIT2, SOCFPGA_SDR_ADDRESS + addr);
  2842. /* activate rows */
  2843. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  2844. writel(RW_MGR_ACTIVATE_0_AND_1, SOCFPGA_SDR_ADDRESS + addr);
  2845. }
  2846. }
  2847. /* Configure various memory related parameters. */
  2848. static void mem_config(void)
  2849. {
  2850. uint32_t rlat, wlat;
  2851. uint32_t rw_wl_nop_cycles;
  2852. uint32_t max_latency;
  2853. uint32_t addr;
  2854. debug("%s:%d\n", __func__, __LINE__);
  2855. /* read in write and read latency */
  2856. addr = (u32)&data_mgr->t_wl_add;
  2857. wlat = readl(SOCFPGA_SDR_ADDRESS + addr);
  2858. addr = (u32)&data_mgr->mem_t_add;
  2859. wlat += readl(SOCFPGA_SDR_ADDRESS + addr);
  2860. /* WL for hard phy does not include additive latency */
  2861. /*
  2862. * add addtional write latency to offset the address/command extra
  2863. * clock cycle. We change the AC mux setting causing AC to be delayed
  2864. * by one mem clock cycle. Only do this for DDR3
  2865. */
  2866. wlat = wlat + 1;
  2867. addr = (u32)&data_mgr->t_rl_add;
  2868. rlat = readl(SOCFPGA_SDR_ADDRESS + addr);
  2869. rw_wl_nop_cycles = wlat - 2;
  2870. gbl->rw_wl_nop_cycles = rw_wl_nop_cycles;
  2871. /*
  2872. * For AV/CV, lfifo is hardened and always runs at full rate so
  2873. * max latency in AFI clocks, used here, is correspondingly smaller.
  2874. */
  2875. max_latency = (1<<MAX_LATENCY_COUNT_WIDTH)/1 - 1;
  2876. /* configure for a burst length of 8 */
  2877. /* write latency */
  2878. /* Adjust Write Latency for Hard PHY */
  2879. wlat = wlat + 1;
  2880. /* set a pretty high read latency initially */
  2881. gbl->curr_read_lat = rlat + 16;
  2882. if (gbl->curr_read_lat > max_latency)
  2883. gbl->curr_read_lat = max_latency;
  2884. addr = (u32)&phy_mgr_cfg->phy_rlat;
  2885. writel(gbl->curr_read_lat, SOCFPGA_SDR_ADDRESS + addr);
  2886. /* advertise write latency */
  2887. gbl->curr_write_lat = wlat;
  2888. addr = (u32)&phy_mgr_cfg->afi_wlat;
  2889. writel(wlat - 2, SOCFPGA_SDR_ADDRESS + addr);
  2890. /* initialize bit slips */
  2891. mem_precharge_and_activate();
  2892. }
  2893. /* Set VFIFO and LFIFO to instant-on settings in skip calibration mode */
  2894. static void mem_skip_calibrate(void)
  2895. {
  2896. uint32_t vfifo_offset;
  2897. uint32_t i, j, r;
  2898. uint32_t addr;
  2899. debug("%s:%d\n", __func__, __LINE__);
  2900. /* Need to update every shadow register set used by the interface */
  2901. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  2902. r += NUM_RANKS_PER_SHADOW_REG) {
  2903. /*
  2904. * Set output phase alignment settings appropriate for
  2905. * skip calibration.
  2906. */
  2907. for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
  2908. scc_mgr_set_dqs_en_phase(i, 0);
  2909. #if IO_DLL_CHAIN_LENGTH == 6
  2910. scc_mgr_set_dqdqs_output_phase(i, 6);
  2911. #else
  2912. scc_mgr_set_dqdqs_output_phase(i, 7);
  2913. #endif
  2914. /*
  2915. * Case:33398
  2916. *
  2917. * Write data arrives to the I/O two cycles before write
  2918. * latency is reached (720 deg).
  2919. * -> due to bit-slip in a/c bus
  2920. * -> to allow board skew where dqs is longer than ck
  2921. * -> how often can this happen!?
  2922. * -> can claim back some ptaps for high freq
  2923. * support if we can relax this, but i digress...
  2924. *
  2925. * The write_clk leads mem_ck by 90 deg
  2926. * The minimum ptap of the OPA is 180 deg
  2927. * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
  2928. * The write_clk is always delayed by 2 ptaps
  2929. *
  2930. * Hence, to make DQS aligned to CK, we need to delay
  2931. * DQS by:
  2932. * (720 - 90 - 180 - 2 * (360 / IO_DLL_CHAIN_LENGTH))
  2933. *
  2934. * Dividing the above by (360 / IO_DLL_CHAIN_LENGTH)
  2935. * gives us the number of ptaps, which simplies to:
  2936. *
  2937. * (1.25 * IO_DLL_CHAIN_LENGTH - 2)
  2938. */
  2939. scc_mgr_set_dqdqs_output_phase(i, (1.25 *
  2940. IO_DLL_CHAIN_LENGTH - 2));
  2941. }
  2942. addr = (u32)&sdr_scc_mgr->dqs_ena;
  2943. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  2944. addr = (u32)&sdr_scc_mgr->dqs_io_ena;
  2945. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  2946. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_GROUP_COUNTER_OFFSET;
  2947. for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
  2948. writel(i, SOCFPGA_SDR_ADDRESS + addr);
  2949. }
  2950. addr = (u32)&sdr_scc_mgr->dq_ena;
  2951. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  2952. addr = (u32)&sdr_scc_mgr->dm_ena;
  2953. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  2954. addr = (u32)&sdr_scc_mgr->update;
  2955. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2956. }
  2957. /* Compensate for simulation model behaviour */
  2958. for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
  2959. scc_mgr_set_dqs_bus_in_delay(i, 10);
  2960. scc_mgr_load_dqs(i);
  2961. }
  2962. addr = (u32)&sdr_scc_mgr->update;
  2963. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2964. /*
  2965. * ArriaV has hard FIFOs that can only be initialized by incrementing
  2966. * in sequencer.
  2967. */
  2968. vfifo_offset = CALIB_VFIFO_OFFSET;
  2969. addr = (u32)&phy_mgr_cmd->inc_vfifo_hard_phy;
  2970. for (j = 0; j < vfifo_offset; j++) {
  2971. writel(0xff, SOCFPGA_SDR_ADDRESS + addr);
  2972. }
  2973. addr = (u32)&phy_mgr_cmd->fifo_reset;
  2974. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  2975. /*
  2976. * For ACV with hard lfifo, we get the skip-cal setting from
  2977. * generation-time constant.
  2978. */
  2979. gbl->curr_read_lat = CALIB_LFIFO_OFFSET;
  2980. addr = (u32)&phy_mgr_cfg->phy_rlat;
  2981. writel(gbl->curr_read_lat, SOCFPGA_SDR_ADDRESS + addr);
  2982. }
  2983. /* Memory calibration entry point */
  2984. static uint32_t mem_calibrate(void)
  2985. {
  2986. uint32_t i;
  2987. uint32_t rank_bgn, sr;
  2988. uint32_t write_group, write_test_bgn;
  2989. uint32_t read_group, read_test_bgn;
  2990. uint32_t run_groups, current_run;
  2991. uint32_t failing_groups = 0;
  2992. uint32_t group_failed = 0;
  2993. uint32_t sr_failed = 0;
  2994. uint32_t addr;
  2995. debug("%s:%d\n", __func__, __LINE__);
  2996. /* Initialize the data settings */
  2997. gbl->error_substage = CAL_SUBSTAGE_NIL;
  2998. gbl->error_stage = CAL_STAGE_NIL;
  2999. gbl->error_group = 0xff;
  3000. gbl->fom_in = 0;
  3001. gbl->fom_out = 0;
  3002. mem_config();
  3003. uint32_t bypass_mode = 0x1;
  3004. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_GROUP_COUNTER_OFFSET;
  3005. for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
  3006. writel(i, SOCFPGA_SDR_ADDRESS + addr);
  3007. scc_set_bypass_mode(i, bypass_mode);
  3008. }
  3009. if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
  3010. /*
  3011. * Set VFIFO and LFIFO to instant-on settings in skip
  3012. * calibration mode.
  3013. */
  3014. mem_skip_calibrate();
  3015. } else {
  3016. for (i = 0; i < NUM_CALIB_REPEAT; i++) {
  3017. /*
  3018. * Zero all delay chain/phase settings for all
  3019. * groups and all shadow register sets.
  3020. */
  3021. scc_mgr_zero_all();
  3022. run_groups = ~param->skip_groups;
  3023. for (write_group = 0, write_test_bgn = 0; write_group
  3024. < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; write_group++,
  3025. write_test_bgn += RW_MGR_MEM_DQ_PER_WRITE_DQS) {
  3026. /* Initialized the group failure */
  3027. group_failed = 0;
  3028. current_run = run_groups & ((1 <<
  3029. RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
  3030. run_groups = run_groups >>
  3031. RW_MGR_NUM_DQS_PER_WRITE_GROUP;
  3032. if (current_run == 0)
  3033. continue;
  3034. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_GROUP_COUNTER_OFFSET;
  3035. writel(write_group, SOCFPGA_SDR_ADDRESS + addr);
  3036. scc_mgr_zero_group(write_group, write_test_bgn,
  3037. 0);
  3038. for (read_group = write_group *
  3039. RW_MGR_MEM_IF_READ_DQS_WIDTH /
  3040. RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
  3041. read_test_bgn = 0;
  3042. read_group < (write_group + 1) *
  3043. RW_MGR_MEM_IF_READ_DQS_WIDTH /
  3044. RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
  3045. group_failed == 0;
  3046. read_group++, read_test_bgn +=
  3047. RW_MGR_MEM_DQ_PER_READ_DQS) {
  3048. /* Calibrate the VFIFO */
  3049. if (!((STATIC_CALIB_STEPS) &
  3050. CALIB_SKIP_VFIFO)) {
  3051. if (!rw_mgr_mem_calibrate_vfifo
  3052. (read_group,
  3053. read_test_bgn)) {
  3054. group_failed = 1;
  3055. if (!(gbl->
  3056. phy_debug_mode_flags &
  3057. PHY_DEBUG_SWEEP_ALL_GROUPS)) {
  3058. return 0;
  3059. }
  3060. }
  3061. }
  3062. }
  3063. /* Calibrate the output side */
  3064. if (group_failed == 0) {
  3065. for (rank_bgn = 0, sr = 0; rank_bgn
  3066. < RW_MGR_MEM_NUMBER_OF_RANKS;
  3067. rank_bgn +=
  3068. NUM_RANKS_PER_SHADOW_REG,
  3069. ++sr) {
  3070. sr_failed = 0;
  3071. if (!((STATIC_CALIB_STEPS) &
  3072. CALIB_SKIP_WRITES)) {
  3073. if ((STATIC_CALIB_STEPS)
  3074. & CALIB_SKIP_DELAY_SWEEPS) {
  3075. /* not needed in quick mode! */
  3076. } else {
  3077. /*
  3078. * Determine if this set of
  3079. * ranks should be skipped
  3080. * entirely.
  3081. */
  3082. if (!param->skip_shadow_regs[sr]) {
  3083. if (!rw_mgr_mem_calibrate_writes
  3084. (rank_bgn, write_group,
  3085. write_test_bgn)) {
  3086. sr_failed = 1;
  3087. if (!(gbl->
  3088. phy_debug_mode_flags &
  3089. PHY_DEBUG_SWEEP_ALL_GROUPS)) {
  3090. return 0;
  3091. }
  3092. }
  3093. }
  3094. }
  3095. }
  3096. if (sr_failed != 0)
  3097. group_failed = 1;
  3098. }
  3099. }
  3100. if (group_failed == 0) {
  3101. for (read_group = write_group *
  3102. RW_MGR_MEM_IF_READ_DQS_WIDTH /
  3103. RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
  3104. read_test_bgn = 0;
  3105. read_group < (write_group + 1)
  3106. * RW_MGR_MEM_IF_READ_DQS_WIDTH
  3107. / RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
  3108. group_failed == 0;
  3109. read_group++, read_test_bgn +=
  3110. RW_MGR_MEM_DQ_PER_READ_DQS) {
  3111. if (!((STATIC_CALIB_STEPS) &
  3112. CALIB_SKIP_WRITES)) {
  3113. if (!rw_mgr_mem_calibrate_vfifo_end
  3114. (read_group, read_test_bgn)) {
  3115. group_failed = 1;
  3116. if (!(gbl->phy_debug_mode_flags
  3117. & PHY_DEBUG_SWEEP_ALL_GROUPS)) {
  3118. return 0;
  3119. }
  3120. }
  3121. }
  3122. }
  3123. }
  3124. if (group_failed != 0)
  3125. failing_groups++;
  3126. }
  3127. /*
  3128. * USER If there are any failing groups then report
  3129. * the failure.
  3130. */
  3131. if (failing_groups != 0)
  3132. return 0;
  3133. /* Calibrate the LFIFO */
  3134. if (!((STATIC_CALIB_STEPS) & CALIB_SKIP_LFIFO)) {
  3135. /*
  3136. * If we're skipping groups as part of debug,
  3137. * don't calibrate LFIFO.
  3138. */
  3139. if (param->skip_groups == 0) {
  3140. if (!rw_mgr_mem_calibrate_lfifo())
  3141. return 0;
  3142. }
  3143. }
  3144. }
  3145. }
  3146. /*
  3147. * Do not remove this line as it makes sure all of our decisions
  3148. * have been applied.
  3149. */
  3150. addr = (u32)&sdr_scc_mgr->update;
  3151. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  3152. return 1;
  3153. }
  3154. static uint32_t run_mem_calibrate(void)
  3155. {
  3156. uint32_t pass;
  3157. uint32_t debug_info;
  3158. uint32_t addr;
  3159. debug("%s:%d\n", __func__, __LINE__);
  3160. /* Reset pass/fail status shown on afi_cal_success/fail */
  3161. addr = (u32)&phy_mgr_cfg->cal_status;
  3162. writel(PHY_MGR_CAL_RESET, SOCFPGA_SDR_ADDRESS + addr);
  3163. addr = SDR_CTRLGRP_ADDRESS;
  3164. /* stop tracking manger */
  3165. uint32_t ctrlcfg = readl(SOCFPGA_SDR_ADDRESS + addr);
  3166. addr = SDR_CTRLGRP_ADDRESS;
  3167. writel(ctrlcfg & 0xFFBFFFFF, SOCFPGA_SDR_ADDRESS + addr);
  3168. initialize();
  3169. rw_mgr_mem_initialize();
  3170. pass = mem_calibrate();
  3171. mem_precharge_and_activate();
  3172. addr = (u32)&phy_mgr_cmd->fifo_reset;
  3173. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  3174. /*
  3175. * Handoff:
  3176. * Don't return control of the PHY back to AFI when in debug mode.
  3177. */
  3178. if ((gbl->phy_debug_mode_flags & PHY_DEBUG_IN_DEBUG_MODE) == 0) {
  3179. rw_mgr_mem_handoff();
  3180. /*
  3181. * In Hard PHY this is a 2-bit control:
  3182. * 0: AFI Mux Select
  3183. * 1: DDIO Mux Select
  3184. */
  3185. addr = (u32)&phy_mgr_cfg->mux_sel;
  3186. writel(0x2, SOCFPGA_SDR_ADDRESS + addr);
  3187. }
  3188. addr = SDR_CTRLGRP_ADDRESS;
  3189. writel(ctrlcfg, SOCFPGA_SDR_ADDRESS + addr);
  3190. if (pass) {
  3191. printf("%s: CALIBRATION PASSED\n", __FILE__);
  3192. gbl->fom_in /= 2;
  3193. gbl->fom_out /= 2;
  3194. if (gbl->fom_in > 0xff)
  3195. gbl->fom_in = 0xff;
  3196. if (gbl->fom_out > 0xff)
  3197. gbl->fom_out = 0xff;
  3198. /* Update the FOM in the register file */
  3199. debug_info = gbl->fom_in;
  3200. debug_info |= gbl->fom_out << 8;
  3201. addr = (u32)&sdr_reg_file->fom;
  3202. writel(debug_info, SOCFPGA_SDR_ADDRESS + addr);
  3203. addr = (u32)&phy_mgr_cfg->cal_debug_info;
  3204. writel(debug_info, SOCFPGA_SDR_ADDRESS + addr);
  3205. addr = (u32)&phy_mgr_cfg->cal_status;
  3206. writel(PHY_MGR_CAL_SUCCESS, SOCFPGA_SDR_ADDRESS + addr);
  3207. } else {
  3208. printf("%s: CALIBRATION FAILED\n", __FILE__);
  3209. debug_info = gbl->error_stage;
  3210. debug_info |= gbl->error_substage << 8;
  3211. debug_info |= gbl->error_group << 16;
  3212. addr = (u32)&sdr_reg_file->failing_stage;
  3213. writel(debug_info, SOCFPGA_SDR_ADDRESS + addr);
  3214. addr = (u32)&phy_mgr_cfg->cal_debug_info;
  3215. writel(debug_info, SOCFPGA_SDR_ADDRESS + addr);
  3216. addr = (u32)&phy_mgr_cfg->cal_status;
  3217. writel(PHY_MGR_CAL_FAIL, SOCFPGA_SDR_ADDRESS + addr);
  3218. /* Update the failing group/stage in the register file */
  3219. debug_info = gbl->error_stage;
  3220. debug_info |= gbl->error_substage << 8;
  3221. debug_info |= gbl->error_group << 16;
  3222. addr = (u32)&sdr_reg_file->failing_stage;
  3223. writel(debug_info, SOCFPGA_SDR_ADDRESS + addr);
  3224. }
  3225. return pass;
  3226. }
  3227. static void hc_initialize_rom_data(void)
  3228. {
  3229. uint32_t i;
  3230. uint32_t addr;
  3231. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
  3232. for (i = 0; i < ARRAY_SIZE(inst_rom_init); i++) {
  3233. uint32_t data = inst_rom_init[i];
  3234. writel(data, SOCFPGA_SDR_ADDRESS + addr + (i << 2));
  3235. }
  3236. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
  3237. for (i = 0; i < ARRAY_SIZE(ac_rom_init); i++) {
  3238. uint32_t data = ac_rom_init[i];
  3239. writel(data, SOCFPGA_SDR_ADDRESS + addr + (i << 2));
  3240. }
  3241. }
  3242. static void initialize_reg_file(void)
  3243. {
  3244. uint32_t addr;
  3245. /* Initialize the register file with the correct data */
  3246. addr = (u32)&sdr_reg_file->signature;
  3247. writel(REG_FILE_INIT_SEQ_SIGNATURE, SOCFPGA_SDR_ADDRESS + addr);
  3248. addr = (u32)&sdr_reg_file->debug_data_addr;
  3249. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  3250. addr = (u32)&sdr_reg_file->cur_stage;
  3251. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  3252. addr = (u32)&sdr_reg_file->fom;
  3253. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  3254. addr = (u32)&sdr_reg_file->failing_stage;
  3255. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  3256. addr = (u32)&sdr_reg_file->debug1;
  3257. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  3258. addr = (u32)&sdr_reg_file->debug2;
  3259. writel(0, SOCFPGA_SDR_ADDRESS + addr);
  3260. }
  3261. static void initialize_hps_phy(void)
  3262. {
  3263. uint32_t reg;
  3264. uint32_t addr;
  3265. /*
  3266. * Tracking also gets configured here because it's in the
  3267. * same register.
  3268. */
  3269. uint32_t trk_sample_count = 7500;
  3270. uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
  3271. /*
  3272. * Format is number of outer loops in the 16 MSB, sample
  3273. * count in 16 LSB.
  3274. */
  3275. reg = 0;
  3276. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
  3277. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
  3278. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
  3279. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
  3280. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
  3281. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
  3282. /*
  3283. * This field selects the intrinsic latency to RDATA_EN/FULL path.
  3284. * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
  3285. */
  3286. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
  3287. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
  3288. trk_sample_count);
  3289. addr = SDR_CTRLGRP_ADDRESS;
  3290. writel(reg, SOCFPGA_SDR_ADDRESS + addr + SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_OFFSET);
  3291. reg = 0;
  3292. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
  3293. trk_sample_count >>
  3294. SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
  3295. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
  3296. trk_long_idle_sample_count);
  3297. writel(reg, SOCFPGA_SDR_ADDRESS + addr + SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_OFFSET);
  3298. reg = 0;
  3299. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
  3300. trk_long_idle_sample_count >>
  3301. SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
  3302. writel(reg, SOCFPGA_SDR_ADDRESS + addr + SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_OFFSET);
  3303. }
  3304. static void initialize_tracking(void)
  3305. {
  3306. uint32_t concatenated_longidle = 0x0;
  3307. uint32_t concatenated_delays = 0x0;
  3308. uint32_t concatenated_rw_addr = 0x0;
  3309. uint32_t concatenated_refresh = 0x0;
  3310. uint32_t trk_sample_count = 7500;
  3311. uint32_t dtaps_per_ptap;
  3312. uint32_t tmp_delay;
  3313. uint32_t addr;
  3314. /*
  3315. * compute usable version of value in case we skip full
  3316. * computation later
  3317. */
  3318. dtaps_per_ptap = 0;
  3319. tmp_delay = 0;
  3320. while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
  3321. dtaps_per_ptap++;
  3322. tmp_delay += IO_DELAY_PER_DCHAIN_TAP;
  3323. }
  3324. dtaps_per_ptap--;
  3325. concatenated_longidle = concatenated_longidle ^ 10;
  3326. /*longidle outer loop */
  3327. concatenated_longidle = concatenated_longidle << 16;
  3328. concatenated_longidle = concatenated_longidle ^ 100;
  3329. /*longidle sample count */
  3330. concatenated_delays = concatenated_delays ^ 243;
  3331. /* trfc, worst case of 933Mhz 4Gb */
  3332. concatenated_delays = concatenated_delays << 8;
  3333. concatenated_delays = concatenated_delays ^ 14;
  3334. /* trcd, worst case */
  3335. concatenated_delays = concatenated_delays << 8;
  3336. concatenated_delays = concatenated_delays ^ 10;
  3337. /* vfifo wait */
  3338. concatenated_delays = concatenated_delays << 8;
  3339. concatenated_delays = concatenated_delays ^ 4;
  3340. /* mux delay */
  3341. concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_IDLE;
  3342. concatenated_rw_addr = concatenated_rw_addr << 8;
  3343. concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_ACTIVATE_1;
  3344. concatenated_rw_addr = concatenated_rw_addr << 8;
  3345. concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_SGLE_READ;
  3346. concatenated_rw_addr = concatenated_rw_addr << 8;
  3347. concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_PRECHARGE_ALL;
  3348. concatenated_refresh = concatenated_refresh ^ RW_MGR_REFRESH_ALL;
  3349. concatenated_refresh = concatenated_refresh << 24;
  3350. concatenated_refresh = concatenated_refresh ^ 1000; /* trefi */
  3351. /* Initialize the register file with the correct data */
  3352. addr = (u32)&sdr_reg_file->dtaps_per_ptap;
  3353. writel(dtaps_per_ptap, SOCFPGA_SDR_ADDRESS + addr);
  3354. addr = (u32)&sdr_reg_file->trk_sample_count;
  3355. writel(trk_sample_count, SOCFPGA_SDR_ADDRESS + addr);
  3356. addr = (u32)&sdr_reg_file->trk_longidle;
  3357. writel(concatenated_longidle, SOCFPGA_SDR_ADDRESS + addr);
  3358. addr = (u32)&sdr_reg_file->delays;
  3359. writel(concatenated_delays, SOCFPGA_SDR_ADDRESS + addr);
  3360. addr = (u32)&sdr_reg_file->trk_rw_mgr_addr;
  3361. writel(concatenated_rw_addr, SOCFPGA_SDR_ADDRESS + addr);
  3362. addr = (u32)&sdr_reg_file->trk_read_dqs_width;
  3363. writel(RW_MGR_MEM_IF_READ_DQS_WIDTH, SOCFPGA_SDR_ADDRESS + addr);
  3364. addr = (u32)&sdr_reg_file->trk_rfsh;
  3365. writel(concatenated_refresh, SOCFPGA_SDR_ADDRESS + addr);
  3366. }
  3367. int sdram_calibration_full(void)
  3368. {
  3369. struct param_type my_param;
  3370. struct gbl_type my_gbl;
  3371. uint32_t pass;
  3372. uint32_t i;
  3373. param = &my_param;
  3374. gbl = &my_gbl;
  3375. /* Initialize the debug mode flags */
  3376. gbl->phy_debug_mode_flags = 0;
  3377. /* Set the calibration enabled by default */
  3378. gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
  3379. /*
  3380. * Only sweep all groups (regardless of fail state) by default
  3381. * Set enabled read test by default.
  3382. */
  3383. #if DISABLE_GUARANTEED_READ
  3384. gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
  3385. #endif
  3386. /* Initialize the register file */
  3387. initialize_reg_file();
  3388. /* Initialize any PHY CSR */
  3389. initialize_hps_phy();
  3390. scc_mgr_initialize();
  3391. initialize_tracking();
  3392. /* USER Enable all ranks, groups */
  3393. for (i = 0; i < RW_MGR_MEM_NUMBER_OF_RANKS; i++)
  3394. param->skip_ranks[i] = 0;
  3395. for (i = 0; i < NUM_SHADOW_REGS; ++i)
  3396. param->skip_shadow_regs[i] = 0;
  3397. param->skip_groups = 0;
  3398. printf("%s: Preparing to start memory calibration\n", __FILE__);
  3399. debug("%s:%d\n", __func__, __LINE__);
  3400. debug_cond(DLEVEL == 1,
  3401. "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
  3402. RW_MGR_MEM_NUMBER_OF_RANKS, RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM,
  3403. RW_MGR_MEM_DQ_PER_READ_DQS, RW_MGR_MEM_DQ_PER_WRITE_DQS,
  3404. RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS,
  3405. RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
  3406. debug_cond(DLEVEL == 1,
  3407. "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
  3408. RW_MGR_MEM_IF_READ_DQS_WIDTH, RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
  3409. RW_MGR_MEM_DATA_WIDTH, RW_MGR_MEM_DATA_MASK_WIDTH,
  3410. IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP);
  3411. debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
  3412. IO_DELAY_PER_DQS_EN_DCHAIN_TAP, IO_DLL_CHAIN_LENGTH);
  3413. debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
  3414. IO_DQS_EN_PHASE_MAX, IO_DQDQS_OUT_PHASE_MAX,
  3415. IO_DQS_EN_DELAY_MAX, IO_DQS_IN_DELAY_MAX);
  3416. debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
  3417. IO_IO_IN_DELAY_MAX, IO_IO_OUT1_DELAY_MAX,
  3418. IO_IO_OUT2_DELAY_MAX);
  3419. debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
  3420. IO_DQS_IN_RESERVE, IO_DQS_OUT_RESERVE);
  3421. hc_initialize_rom_data();
  3422. /* update info for sims */
  3423. reg_file_set_stage(CAL_STAGE_NIL);
  3424. reg_file_set_group(0);
  3425. /*
  3426. * Load global needed for those actions that require
  3427. * some dynamic calibration support.
  3428. */
  3429. dyn_calib_steps = STATIC_CALIB_STEPS;
  3430. /*
  3431. * Load global to allow dynamic selection of delay loop settings
  3432. * based on calibration mode.
  3433. */
  3434. if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
  3435. skip_delay_mask = 0xff;
  3436. else
  3437. skip_delay_mask = 0x0;
  3438. pass = run_mem_calibrate();
  3439. printf("%s: Calibration complete\n", __FILE__);
  3440. return pass;
  3441. }