sequencer.c 110 KB

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