sequencer.c 105 KB

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