sequencer.c 108 KB

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