sequencer.c 105 KB

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