sequencer.c 106 KB

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