sequencer.c 109 KB

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