sequencer.c 108 KB

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