sequencer.c 105 KB

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