sequencer.c 105 KB

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