sequencer.c 107 KB

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