sequencer.c 106 KB

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