sequencer.c 108 KB

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