sequencer.c 105 KB

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