sequencer.c 105 KB

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