sequencer.c 105 KB

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