sequencer.c 106 KB

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