sequencer.c 104 KB

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