sequencer.c 106 KB

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