sequencer.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852
  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. static void rw_mgr_incr_vfifo(uint32_t grp, uint32_t *v)
  1108. {
  1109. writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
  1110. (*v)++;
  1111. }
  1112. static void rw_mgr_decr_vfifo(uint32_t grp, uint32_t *v)
  1113. {
  1114. uint32_t i;
  1115. for (i = 0; i < VFIFO_SIZE-1; i++)
  1116. rw_mgr_incr_vfifo(grp, v);
  1117. }
  1118. static int find_vfifo_read(uint32_t grp, uint32_t *bit_chk)
  1119. {
  1120. uint32_t v;
  1121. uint32_t fail_cnt = 0;
  1122. uint32_t test_status;
  1123. for (v = 0; v < VFIFO_SIZE; ) {
  1124. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo %u\n",
  1125. __func__, __LINE__, v);
  1126. test_status = rw_mgr_mem_calibrate_read_test_all_ranks
  1127. (grp, 1, PASS_ONE_BIT, bit_chk, 0);
  1128. if (!test_status) {
  1129. fail_cnt++;
  1130. if (fail_cnt == 2)
  1131. break;
  1132. }
  1133. /* fiddle with FIFO */
  1134. rw_mgr_incr_vfifo(grp, &v);
  1135. }
  1136. if (v >= VFIFO_SIZE) {
  1137. /* no failing read found!! Something must have gone wrong */
  1138. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo failed\n",
  1139. __func__, __LINE__);
  1140. return 0;
  1141. } else {
  1142. return v;
  1143. }
  1144. }
  1145. static int find_working_phase(uint32_t *grp, uint32_t *bit_chk,
  1146. uint32_t dtaps_per_ptap, uint32_t *work_bgn,
  1147. uint32_t *v, uint32_t *d, uint32_t *p,
  1148. uint32_t *i, uint32_t *max_working_cnt)
  1149. {
  1150. uint32_t found_begin = 0;
  1151. uint32_t tmp_delay = 0;
  1152. uint32_t test_status;
  1153. for (*d = 0; *d <= dtaps_per_ptap; (*d)++, tmp_delay +=
  1154. IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
  1155. *work_bgn = tmp_delay;
  1156. scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
  1157. for (*i = 0; *i < VFIFO_SIZE; (*i)++) {
  1158. for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_bgn +=
  1159. IO_DELAY_PER_OPA_TAP) {
  1160. scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
  1161. test_status =
  1162. rw_mgr_mem_calibrate_read_test_all_ranks
  1163. (*grp, 1, PASS_ONE_BIT, bit_chk, 0);
  1164. if (test_status) {
  1165. *max_working_cnt = 1;
  1166. found_begin = 1;
  1167. break;
  1168. }
  1169. }
  1170. if (found_begin)
  1171. break;
  1172. if (*p > IO_DQS_EN_PHASE_MAX)
  1173. /* fiddle with FIFO */
  1174. rw_mgr_incr_vfifo(*grp, v);
  1175. }
  1176. if (found_begin)
  1177. break;
  1178. }
  1179. if (*i >= VFIFO_SIZE) {
  1180. /* cannot find working solution */
  1181. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/\
  1182. ptap/dtap\n", __func__, __LINE__);
  1183. return 0;
  1184. } else {
  1185. return 1;
  1186. }
  1187. }
  1188. static void sdr_backup_phase(uint32_t *grp, uint32_t *bit_chk,
  1189. uint32_t *work_bgn, uint32_t *v, uint32_t *d,
  1190. uint32_t *p, uint32_t *max_working_cnt)
  1191. {
  1192. uint32_t found_begin = 0;
  1193. uint32_t tmp_delay;
  1194. /* Special case code for backing up a phase */
  1195. if (*p == 0) {
  1196. *p = IO_DQS_EN_PHASE_MAX;
  1197. rw_mgr_decr_vfifo(*grp, v);
  1198. } else {
  1199. (*p)--;
  1200. }
  1201. tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
  1202. scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
  1203. for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn;
  1204. (*d)++, tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
  1205. scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
  1206. if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
  1207. PASS_ONE_BIT,
  1208. bit_chk, 0)) {
  1209. found_begin = 1;
  1210. *work_bgn = tmp_delay;
  1211. break;
  1212. }
  1213. }
  1214. /* We have found a working dtap before the ptap found above */
  1215. if (found_begin == 1)
  1216. (*max_working_cnt)++;
  1217. /*
  1218. * Restore VFIFO to old state before we decremented it
  1219. * (if needed).
  1220. */
  1221. (*p)++;
  1222. if (*p > IO_DQS_EN_PHASE_MAX) {
  1223. *p = 0;
  1224. rw_mgr_incr_vfifo(*grp, v);
  1225. }
  1226. scc_mgr_set_dqs_en_delay_all_ranks(*grp, 0);
  1227. }
  1228. static int sdr_nonworking_phase(uint32_t *grp, uint32_t *bit_chk,
  1229. uint32_t *work_bgn, uint32_t *v, uint32_t *d,
  1230. uint32_t *p, uint32_t *i, uint32_t *max_working_cnt,
  1231. uint32_t *work_end)
  1232. {
  1233. uint32_t found_end = 0;
  1234. (*p)++;
  1235. *work_end += IO_DELAY_PER_OPA_TAP;
  1236. if (*p > IO_DQS_EN_PHASE_MAX) {
  1237. /* fiddle with FIFO */
  1238. *p = 0;
  1239. rw_mgr_incr_vfifo(*grp, v);
  1240. }
  1241. for (; *i < VFIFO_SIZE + 1; (*i)++) {
  1242. for (; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_end
  1243. += IO_DELAY_PER_OPA_TAP) {
  1244. scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
  1245. if (!rw_mgr_mem_calibrate_read_test_all_ranks
  1246. (*grp, 1, PASS_ONE_BIT, bit_chk, 0)) {
  1247. found_end = 1;
  1248. break;
  1249. } else {
  1250. (*max_working_cnt)++;
  1251. }
  1252. }
  1253. if (found_end)
  1254. break;
  1255. if (*p > IO_DQS_EN_PHASE_MAX) {
  1256. /* fiddle with FIFO */
  1257. rw_mgr_incr_vfifo(*grp, v);
  1258. *p = 0;
  1259. }
  1260. }
  1261. if (*i >= VFIFO_SIZE + 1) {
  1262. /* cannot see edge of failing read */
  1263. debug_cond(DLEVEL == 2, "%s:%d sdr_nonworking_phase: end:\
  1264. failed\n", __func__, __LINE__);
  1265. return 0;
  1266. } else {
  1267. return 1;
  1268. }
  1269. }
  1270. static int sdr_find_window_centre(uint32_t *grp, uint32_t *bit_chk,
  1271. uint32_t *work_bgn, uint32_t *v, uint32_t *d,
  1272. uint32_t *p, uint32_t *work_mid,
  1273. uint32_t *work_end)
  1274. {
  1275. int i;
  1276. int tmp_delay = 0;
  1277. *work_mid = (*work_bgn + *work_end) / 2;
  1278. debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
  1279. *work_bgn, *work_end, *work_mid);
  1280. /* Get the middle delay to be less than a VFIFO delay */
  1281. for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX;
  1282. (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
  1283. ;
  1284. debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
  1285. while (*work_mid > tmp_delay)
  1286. *work_mid -= tmp_delay;
  1287. debug_cond(DLEVEL == 2, "new work_mid %d\n", *work_mid);
  1288. tmp_delay = 0;
  1289. for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX && tmp_delay < *work_mid;
  1290. (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
  1291. ;
  1292. tmp_delay -= IO_DELAY_PER_OPA_TAP;
  1293. debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", (*p) - 1, tmp_delay);
  1294. for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_mid; (*d)++,
  1295. tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP)
  1296. ;
  1297. debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", *d, tmp_delay);
  1298. scc_mgr_set_dqs_en_phase_all_ranks(*grp, (*p) - 1);
  1299. scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
  1300. /*
  1301. * push vfifo until we can successfully calibrate. We can do this
  1302. * because the largest possible margin in 1 VFIFO cycle.
  1303. */
  1304. for (i = 0; i < VFIFO_SIZE; i++) {
  1305. debug_cond(DLEVEL == 2, "find_dqs_en_phase: center: vfifo=%u\n",
  1306. *v);
  1307. if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
  1308. PASS_ONE_BIT,
  1309. bit_chk, 0)) {
  1310. break;
  1311. }
  1312. /* fiddle with FIFO */
  1313. rw_mgr_incr_vfifo(*grp, v);
  1314. }
  1315. if (i >= VFIFO_SIZE) {
  1316. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center: \
  1317. failed\n", __func__, __LINE__);
  1318. return 0;
  1319. } else {
  1320. return 1;
  1321. }
  1322. }
  1323. /* find a good dqs enable to use */
  1324. static uint32_t rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(uint32_t grp)
  1325. {
  1326. uint32_t v, d, p, i;
  1327. uint32_t max_working_cnt;
  1328. uint32_t bit_chk;
  1329. uint32_t dtaps_per_ptap;
  1330. uint32_t work_bgn, work_mid, work_end;
  1331. uint32_t found_passing_read, found_failing_read, initial_failing_dtap;
  1332. debug("%s:%d %u\n", __func__, __LINE__, grp);
  1333. reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
  1334. scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
  1335. scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
  1336. /* ************************************************************** */
  1337. /* * Step 0 : Determine number of delay taps for each phase tap * */
  1338. dtaps_per_ptap = IO_DELAY_PER_OPA_TAP/IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
  1339. /* ********************************************************* */
  1340. /* * Step 1 : First push vfifo until we get a failing read * */
  1341. v = find_vfifo_read(grp, &bit_chk);
  1342. max_working_cnt = 0;
  1343. /* ******************************************************** */
  1344. /* * step 2: find first working phase, increment in ptaps * */
  1345. work_bgn = 0;
  1346. if (find_working_phase(&grp, &bit_chk, dtaps_per_ptap, &work_bgn, &v, &d,
  1347. &p, &i, &max_working_cnt) == 0)
  1348. return 0;
  1349. work_end = work_bgn;
  1350. /*
  1351. * If d is 0 then the working window covers a phase tap and
  1352. * we can follow the old procedure otherwise, we've found the beginning,
  1353. * and we need to increment the dtaps until we find the end.
  1354. */
  1355. if (d == 0) {
  1356. /* ********************************************************* */
  1357. /* * step 3a: if we have room, back off by one and
  1358. increment in dtaps * */
  1359. sdr_backup_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
  1360. &max_working_cnt);
  1361. /* ********************************************************* */
  1362. /* * step 4a: go forward from working phase to non working
  1363. phase, increment in ptaps * */
  1364. if (sdr_nonworking_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
  1365. &i, &max_working_cnt, &work_end) == 0)
  1366. return 0;
  1367. /* ********************************************************* */
  1368. /* * step 5a: back off one from last, increment in dtaps * */
  1369. /* Special case code for backing up a phase */
  1370. if (p == 0) {
  1371. p = IO_DQS_EN_PHASE_MAX;
  1372. rw_mgr_decr_vfifo(grp, &v);
  1373. } else {
  1374. p = p - 1;
  1375. }
  1376. work_end -= IO_DELAY_PER_OPA_TAP;
  1377. scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
  1378. /* * The actual increment of dtaps is done outside of
  1379. the if/else loop to share code */
  1380. d = 0;
  1381. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p: \
  1382. vfifo=%u ptap=%u\n", __func__, __LINE__,
  1383. v, p);
  1384. } else {
  1385. /* ******************************************************* */
  1386. /* * step 3-5b: Find the right edge of the window using
  1387. delay taps * */
  1388. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase:vfifo=%u \
  1389. ptap=%u dtap=%u bgn=%u\n", __func__, __LINE__,
  1390. v, p, d, work_bgn);
  1391. work_end = work_bgn;
  1392. /* * The actual increment of dtaps is done outside of the
  1393. if/else loop to share code */
  1394. /* Only here to counterbalance a subtract later on which is
  1395. not needed if this branch of the algorithm is taken */
  1396. max_working_cnt++;
  1397. }
  1398. /* The dtap increment to find the failing edge is done here */
  1399. for (; d <= IO_DQS_EN_DELAY_MAX; d++, work_end +=
  1400. IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
  1401. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
  1402. end-2: dtap=%u\n", __func__, __LINE__, d);
  1403. scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
  1404. if (!rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
  1405. PASS_ONE_BIT,
  1406. &bit_chk, 0)) {
  1407. break;
  1408. }
  1409. }
  1410. /* Go back to working dtap */
  1411. if (d != 0)
  1412. work_end -= IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
  1413. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p/d: vfifo=%u \
  1414. ptap=%u dtap=%u end=%u\n", __func__, __LINE__,
  1415. v, p, d-1, work_end);
  1416. if (work_end < work_bgn) {
  1417. /* nil range */
  1418. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: end-2: \
  1419. failed\n", __func__, __LINE__);
  1420. return 0;
  1421. }
  1422. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: found range [%u,%u]\n",
  1423. __func__, __LINE__, work_bgn, work_end);
  1424. /* *************************************************************** */
  1425. /*
  1426. * * We need to calculate the number of dtaps that equal a ptap
  1427. * * To do that we'll back up a ptap and re-find the edge of the
  1428. * * window using dtaps
  1429. */
  1430. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: calculate dtaps_per_ptap \
  1431. for tracking\n", __func__, __LINE__);
  1432. /* Special case code for backing up a phase */
  1433. if (p == 0) {
  1434. p = IO_DQS_EN_PHASE_MAX;
  1435. rw_mgr_decr_vfifo(grp, &v);
  1436. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
  1437. cycle/phase: v=%u p=%u\n", __func__, __LINE__,
  1438. v, p);
  1439. } else {
  1440. p = p - 1;
  1441. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
  1442. phase only: v=%u p=%u", __func__, __LINE__,
  1443. v, p);
  1444. }
  1445. scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
  1446. /*
  1447. * Increase dtap until we first see a passing read (in case the
  1448. * window is smaller than a ptap),
  1449. * and then a failing read to mark the edge of the window again
  1450. */
  1451. /* Find a passing read */
  1452. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find passing read\n",
  1453. __func__, __LINE__);
  1454. found_passing_read = 0;
  1455. found_failing_read = 0;
  1456. initial_failing_dtap = d;
  1457. for (; d <= IO_DQS_EN_DELAY_MAX; d++) {
  1458. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: testing \
  1459. read d=%u\n", __func__, __LINE__, d);
  1460. scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
  1461. if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
  1462. PASS_ONE_BIT,
  1463. &bit_chk, 0)) {
  1464. found_passing_read = 1;
  1465. break;
  1466. }
  1467. }
  1468. if (found_passing_read) {
  1469. /* Find a failing read */
  1470. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find failing \
  1471. read\n", __func__, __LINE__);
  1472. for (d = d + 1; d <= IO_DQS_EN_DELAY_MAX; d++) {
  1473. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
  1474. testing read d=%u\n", __func__, __LINE__, d);
  1475. scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
  1476. if (!rw_mgr_mem_calibrate_read_test_all_ranks
  1477. (grp, 1, PASS_ONE_BIT, &bit_chk, 0)) {
  1478. found_failing_read = 1;
  1479. break;
  1480. }
  1481. }
  1482. } else {
  1483. debug_cond(DLEVEL == 1, "%s:%d find_dqs_en_phase: failed to \
  1484. calculate dtaps", __func__, __LINE__);
  1485. debug_cond(DLEVEL == 1, "per ptap. Fall back on static value\n");
  1486. }
  1487. /*
  1488. * The dynamically calculated dtaps_per_ptap is only valid if we
  1489. * found a passing/failing read. If we didn't, it means d hit the max
  1490. * (IO_DQS_EN_DELAY_MAX). Otherwise, dtaps_per_ptap retains its
  1491. * statically calculated value.
  1492. */
  1493. if (found_passing_read && found_failing_read)
  1494. dtaps_per_ptap = d - initial_failing_dtap;
  1495. writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
  1496. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: dtaps_per_ptap=%u \
  1497. - %u = %u", __func__, __LINE__, d,
  1498. initial_failing_dtap, dtaps_per_ptap);
  1499. /* ******************************************** */
  1500. /* * step 6: Find the centre of the window * */
  1501. if (sdr_find_window_centre(&grp, &bit_chk, &work_bgn, &v, &d, &p,
  1502. &work_mid, &work_end) == 0)
  1503. return 0;
  1504. debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center found: \
  1505. vfifo=%u ptap=%u dtap=%u\n", __func__, __LINE__,
  1506. v, p-1, d);
  1507. return 1;
  1508. }
  1509. /*
  1510. * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
  1511. * dq_in_delay values
  1512. */
  1513. static uint32_t
  1514. rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
  1515. (uint32_t write_group, uint32_t read_group, uint32_t test_bgn)
  1516. {
  1517. uint32_t found;
  1518. uint32_t i;
  1519. uint32_t p;
  1520. uint32_t d;
  1521. uint32_t r;
  1522. const uint32_t delay_step = IO_IO_IN_DELAY_MAX /
  1523. (RW_MGR_MEM_DQ_PER_READ_DQS-1);
  1524. /* we start at zero, so have one less dq to devide among */
  1525. debug("%s:%d (%u,%u,%u)", __func__, __LINE__, write_group, read_group,
  1526. test_bgn);
  1527. /* try different dq_in_delays since the dq path is shorter than dqs */
  1528. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  1529. r += NUM_RANKS_PER_SHADOW_REG) {
  1530. for (i = 0, p = test_bgn, d = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++, d += delay_step) {
  1531. debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_\
  1532. vfifo_find_dqs_", __func__, __LINE__);
  1533. debug_cond(DLEVEL == 1, "en_phase_sweep_dq_in_delay: g=%u/%u ",
  1534. write_group, read_group);
  1535. debug_cond(DLEVEL == 1, "r=%u, i=%u p=%u d=%u\n", r, i , p, d);
  1536. scc_mgr_set_dq_in_delay(p, d);
  1537. scc_mgr_load_dq(p);
  1538. }
  1539. writel(0, &sdr_scc_mgr->update);
  1540. }
  1541. found = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(read_group);
  1542. debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_vfifo_find_dqs_\
  1543. en_phase_sweep_dq", __func__, __LINE__);
  1544. debug_cond(DLEVEL == 1, "_in_delay: g=%u/%u found=%u; Reseting delay \
  1545. chain to zero\n", write_group, read_group, found);
  1546. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  1547. r += NUM_RANKS_PER_SHADOW_REG) {
  1548. for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS;
  1549. i++, p++) {
  1550. scc_mgr_set_dq_in_delay(p, 0);
  1551. scc_mgr_load_dq(p);
  1552. }
  1553. writel(0, &sdr_scc_mgr->update);
  1554. }
  1555. return found;
  1556. }
  1557. /* per-bit deskew DQ and center */
  1558. static uint32_t rw_mgr_mem_calibrate_vfifo_center(uint32_t rank_bgn,
  1559. uint32_t write_group, uint32_t read_group, uint32_t test_bgn,
  1560. uint32_t use_read_test, uint32_t update_fom)
  1561. {
  1562. uint32_t i, p, d, min_index;
  1563. /*
  1564. * Store these as signed since there are comparisons with
  1565. * signed numbers.
  1566. */
  1567. uint32_t bit_chk;
  1568. uint32_t sticky_bit_chk;
  1569. int32_t left_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
  1570. int32_t right_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
  1571. int32_t final_dq[RW_MGR_MEM_DQ_PER_READ_DQS];
  1572. int32_t mid;
  1573. int32_t orig_mid_min, mid_min;
  1574. int32_t new_dqs, start_dqs, start_dqs_en, shift_dq, final_dqs,
  1575. final_dqs_en;
  1576. int32_t dq_margin, dqs_margin;
  1577. uint32_t stop;
  1578. uint32_t temp_dq_in_delay1, temp_dq_in_delay2;
  1579. uint32_t addr;
  1580. debug("%s:%d: %u %u", __func__, __LINE__, read_group, test_bgn);
  1581. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_IN_DELAY_OFFSET;
  1582. start_dqs = readl(addr + (read_group << 2));
  1583. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
  1584. start_dqs_en = readl(addr + ((read_group << 2)
  1585. - IO_DQS_EN_DELAY_OFFSET));
  1586. /* set the left and right edge of each bit to an illegal value */
  1587. /* use (IO_IO_IN_DELAY_MAX + 1) as an illegal value */
  1588. sticky_bit_chk = 0;
  1589. for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1590. left_edge[i] = IO_IO_IN_DELAY_MAX + 1;
  1591. right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
  1592. }
  1593. /* Search for the left edge of the window for each bit */
  1594. for (d = 0; d <= IO_IO_IN_DELAY_MAX; d++) {
  1595. scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, d);
  1596. writel(0, &sdr_scc_mgr->update);
  1597. /*
  1598. * Stop searching when the read test doesn't pass AND when
  1599. * we've seen a passing read on every bit.
  1600. */
  1601. if (use_read_test) {
  1602. stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
  1603. read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
  1604. &bit_chk, 0, 0);
  1605. } else {
  1606. rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
  1607. 0, PASS_ONE_BIT,
  1608. &bit_chk, 0);
  1609. bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
  1610. (read_group - (write_group *
  1611. RW_MGR_MEM_IF_READ_DQS_WIDTH /
  1612. RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
  1613. stop = (bit_chk == 0);
  1614. }
  1615. sticky_bit_chk = sticky_bit_chk | bit_chk;
  1616. stop = stop && (sticky_bit_chk == param->read_correct_mask);
  1617. debug_cond(DLEVEL == 2, "%s:%d vfifo_center(left): dtap=%u => %u == %u \
  1618. && %u", __func__, __LINE__, d,
  1619. sticky_bit_chk,
  1620. param->read_correct_mask, stop);
  1621. if (stop == 1) {
  1622. break;
  1623. } else {
  1624. for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1625. if (bit_chk & 1) {
  1626. /* Remember a passing test as the
  1627. left_edge */
  1628. left_edge[i] = d;
  1629. } else {
  1630. /* If a left edge has not been seen yet,
  1631. then a future passing test will mark
  1632. this edge as the right edge */
  1633. if (left_edge[i] ==
  1634. IO_IO_IN_DELAY_MAX + 1) {
  1635. right_edge[i] = -(d + 1);
  1636. }
  1637. }
  1638. bit_chk = bit_chk >> 1;
  1639. }
  1640. }
  1641. }
  1642. /* Reset DQ delay chains to 0 */
  1643. scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
  1644. sticky_bit_chk = 0;
  1645. for (i = RW_MGR_MEM_DQ_PER_READ_DQS - 1;; i--) {
  1646. debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
  1647. %d right_edge[%u]: %d\n", __func__, __LINE__,
  1648. i, left_edge[i], i, right_edge[i]);
  1649. /*
  1650. * Check for cases where we haven't found the left edge,
  1651. * which makes our assignment of the the right edge invalid.
  1652. * Reset it to the illegal value.
  1653. */
  1654. if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) && (
  1655. right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
  1656. right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
  1657. debug_cond(DLEVEL == 2, "%s:%d vfifo_center: reset \
  1658. right_edge[%u]: %d\n", __func__, __LINE__,
  1659. i, right_edge[i]);
  1660. }
  1661. /*
  1662. * Reset sticky bit (except for bits where we have seen
  1663. * both the left and right edge).
  1664. */
  1665. sticky_bit_chk = sticky_bit_chk << 1;
  1666. if ((left_edge[i] != IO_IO_IN_DELAY_MAX + 1) &&
  1667. (right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
  1668. sticky_bit_chk = sticky_bit_chk | 1;
  1669. }
  1670. if (i == 0)
  1671. break;
  1672. }
  1673. /* Search for the right edge of the window for each bit */
  1674. for (d = 0; d <= IO_DQS_IN_DELAY_MAX - start_dqs; d++) {
  1675. scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
  1676. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
  1677. uint32_t delay = d + start_dqs_en;
  1678. if (delay > IO_DQS_EN_DELAY_MAX)
  1679. delay = IO_DQS_EN_DELAY_MAX;
  1680. scc_mgr_set_dqs_en_delay(read_group, delay);
  1681. }
  1682. scc_mgr_load_dqs(read_group);
  1683. writel(0, &sdr_scc_mgr->update);
  1684. /*
  1685. * Stop searching when the read test doesn't pass AND when
  1686. * we've seen a passing read on every bit.
  1687. */
  1688. if (use_read_test) {
  1689. stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
  1690. read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
  1691. &bit_chk, 0, 0);
  1692. } else {
  1693. rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
  1694. 0, PASS_ONE_BIT,
  1695. &bit_chk, 0);
  1696. bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
  1697. (read_group - (write_group *
  1698. RW_MGR_MEM_IF_READ_DQS_WIDTH /
  1699. RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
  1700. stop = (bit_chk == 0);
  1701. }
  1702. sticky_bit_chk = sticky_bit_chk | bit_chk;
  1703. stop = stop && (sticky_bit_chk == param->read_correct_mask);
  1704. debug_cond(DLEVEL == 2, "%s:%d vfifo_center(right): dtap=%u => %u == \
  1705. %u && %u", __func__, __LINE__, d,
  1706. sticky_bit_chk, param->read_correct_mask, stop);
  1707. if (stop == 1) {
  1708. break;
  1709. } else {
  1710. for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1711. if (bit_chk & 1) {
  1712. /* Remember a passing test as
  1713. the right_edge */
  1714. right_edge[i] = d;
  1715. } else {
  1716. if (d != 0) {
  1717. /* If a right edge has not been
  1718. seen yet, then a future passing
  1719. test will mark this edge as the
  1720. left edge */
  1721. if (right_edge[i] ==
  1722. IO_IO_IN_DELAY_MAX + 1) {
  1723. left_edge[i] = -(d + 1);
  1724. }
  1725. } else {
  1726. /* d = 0 failed, but it passed
  1727. when testing the left edge,
  1728. so it must be marginal,
  1729. set it to -1 */
  1730. if (right_edge[i] ==
  1731. IO_IO_IN_DELAY_MAX + 1 &&
  1732. left_edge[i] !=
  1733. IO_IO_IN_DELAY_MAX
  1734. + 1) {
  1735. right_edge[i] = -1;
  1736. }
  1737. /* If a right edge has not been
  1738. seen yet, then a future passing
  1739. test will mark this edge as the
  1740. left edge */
  1741. else if (right_edge[i] ==
  1742. IO_IO_IN_DELAY_MAX +
  1743. 1) {
  1744. left_edge[i] = -(d + 1);
  1745. }
  1746. }
  1747. }
  1748. debug_cond(DLEVEL == 2, "%s:%d vfifo_center[r,\
  1749. d=%u]: ", __func__, __LINE__, d);
  1750. debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d ",
  1751. (int)(bit_chk & 1), i, left_edge[i]);
  1752. debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
  1753. right_edge[i]);
  1754. bit_chk = bit_chk >> 1;
  1755. }
  1756. }
  1757. }
  1758. /* Check that all bits have a window */
  1759. for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1760. debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
  1761. %d right_edge[%u]: %d", __func__, __LINE__,
  1762. i, left_edge[i], i, right_edge[i]);
  1763. if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) || (right_edge[i]
  1764. == IO_IO_IN_DELAY_MAX + 1)) {
  1765. /*
  1766. * Restore delay chain settings before letting the loop
  1767. * in rw_mgr_mem_calibrate_vfifo to retry different
  1768. * dqs/ck relationships.
  1769. */
  1770. scc_mgr_set_dqs_bus_in_delay(read_group, start_dqs);
  1771. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
  1772. scc_mgr_set_dqs_en_delay(read_group,
  1773. start_dqs_en);
  1774. }
  1775. scc_mgr_load_dqs(read_group);
  1776. writel(0, &sdr_scc_mgr->update);
  1777. debug_cond(DLEVEL == 1, "%s:%d vfifo_center: failed to \
  1778. find edge [%u]: %d %d", __func__, __LINE__,
  1779. i, left_edge[i], right_edge[i]);
  1780. if (use_read_test) {
  1781. set_failing_group_stage(read_group *
  1782. RW_MGR_MEM_DQ_PER_READ_DQS + i,
  1783. CAL_STAGE_VFIFO,
  1784. CAL_SUBSTAGE_VFIFO_CENTER);
  1785. } else {
  1786. set_failing_group_stage(read_group *
  1787. RW_MGR_MEM_DQ_PER_READ_DQS + i,
  1788. CAL_STAGE_VFIFO_AFTER_WRITES,
  1789. CAL_SUBSTAGE_VFIFO_CENTER);
  1790. }
  1791. return 0;
  1792. }
  1793. }
  1794. /* Find middle of window for each DQ bit */
  1795. mid_min = left_edge[0] - right_edge[0];
  1796. min_index = 0;
  1797. for (i = 1; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
  1798. mid = left_edge[i] - right_edge[i];
  1799. if (mid < mid_min) {
  1800. mid_min = mid;
  1801. min_index = i;
  1802. }
  1803. }
  1804. /*
  1805. * -mid_min/2 represents the amount that we need to move DQS.
  1806. * If mid_min is odd and positive we'll need to add one to
  1807. * make sure the rounding in further calculations is correct
  1808. * (always bias to the right), so just add 1 for all positive values.
  1809. */
  1810. if (mid_min > 0)
  1811. mid_min++;
  1812. mid_min = mid_min / 2;
  1813. debug_cond(DLEVEL == 1, "%s:%d vfifo_center: mid_min=%d (index=%u)\n",
  1814. __func__, __LINE__, mid_min, min_index);
  1815. /* Determine the amount we can change DQS (which is -mid_min) */
  1816. orig_mid_min = mid_min;
  1817. new_dqs = start_dqs - mid_min;
  1818. if (new_dqs > IO_DQS_IN_DELAY_MAX)
  1819. new_dqs = IO_DQS_IN_DELAY_MAX;
  1820. else if (new_dqs < 0)
  1821. new_dqs = 0;
  1822. mid_min = start_dqs - new_dqs;
  1823. debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
  1824. mid_min, new_dqs);
  1825. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
  1826. if (start_dqs_en - mid_min > IO_DQS_EN_DELAY_MAX)
  1827. mid_min += start_dqs_en - mid_min - IO_DQS_EN_DELAY_MAX;
  1828. else if (start_dqs_en - mid_min < 0)
  1829. mid_min += start_dqs_en - mid_min;
  1830. }
  1831. new_dqs = start_dqs - mid_min;
  1832. debug_cond(DLEVEL == 1, "vfifo_center: start_dqs=%d start_dqs_en=%d \
  1833. new_dqs=%d mid_min=%d\n", start_dqs,
  1834. IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS ? start_dqs_en : -1,
  1835. new_dqs, mid_min);
  1836. /* Initialize data for export structures */
  1837. dqs_margin = IO_IO_IN_DELAY_MAX + 1;
  1838. dq_margin = IO_IO_IN_DELAY_MAX + 1;
  1839. /* add delay to bring centre of all DQ windows to the same "level" */
  1840. for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
  1841. /* Use values before divide by 2 to reduce round off error */
  1842. shift_dq = (left_edge[i] - right_edge[i] -
  1843. (left_edge[min_index] - right_edge[min_index]))/2 +
  1844. (orig_mid_min - mid_min);
  1845. debug_cond(DLEVEL == 2, "vfifo_center: before: \
  1846. shift_dq[%u]=%d\n", i, shift_dq);
  1847. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
  1848. temp_dq_in_delay1 = readl(addr + (p << 2));
  1849. temp_dq_in_delay2 = readl(addr + (i << 2));
  1850. if (shift_dq + (int32_t)temp_dq_in_delay1 >
  1851. (int32_t)IO_IO_IN_DELAY_MAX) {
  1852. shift_dq = (int32_t)IO_IO_IN_DELAY_MAX - temp_dq_in_delay2;
  1853. } else if (shift_dq + (int32_t)temp_dq_in_delay1 < 0) {
  1854. shift_dq = -(int32_t)temp_dq_in_delay1;
  1855. }
  1856. debug_cond(DLEVEL == 2, "vfifo_center: after: \
  1857. shift_dq[%u]=%d\n", i, shift_dq);
  1858. final_dq[i] = temp_dq_in_delay1 + shift_dq;
  1859. scc_mgr_set_dq_in_delay(p, final_dq[i]);
  1860. scc_mgr_load_dq(p);
  1861. debug_cond(DLEVEL == 2, "vfifo_center: margin[%u]=[%d,%d]\n", i,
  1862. left_edge[i] - shift_dq + (-mid_min),
  1863. right_edge[i] + shift_dq - (-mid_min));
  1864. /* To determine values for export structures */
  1865. if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
  1866. dq_margin = left_edge[i] - shift_dq + (-mid_min);
  1867. if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
  1868. dqs_margin = right_edge[i] + shift_dq - (-mid_min);
  1869. }
  1870. final_dqs = new_dqs;
  1871. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
  1872. final_dqs_en = start_dqs_en - mid_min;
  1873. /* Move DQS-en */
  1874. if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
  1875. scc_mgr_set_dqs_en_delay(read_group, final_dqs_en);
  1876. scc_mgr_load_dqs(read_group);
  1877. }
  1878. /* Move DQS */
  1879. scc_mgr_set_dqs_bus_in_delay(read_group, final_dqs);
  1880. scc_mgr_load_dqs(read_group);
  1881. debug_cond(DLEVEL == 2, "%s:%d vfifo_center: dq_margin=%d \
  1882. dqs_margin=%d", __func__, __LINE__,
  1883. dq_margin, dqs_margin);
  1884. /*
  1885. * Do not remove this line as it makes sure all of our decisions
  1886. * have been applied. Apply the update bit.
  1887. */
  1888. writel(0, &sdr_scc_mgr->update);
  1889. return (dq_margin >= 0) && (dqs_margin >= 0);
  1890. }
  1891. /**
  1892. * rw_mgr_mem_calibrate_guaranteed_write() - Perform guaranteed write into the device
  1893. * @rw_group: Read/Write Group
  1894. * @phase: DQ/DQS phase
  1895. *
  1896. * Because initially no communication ca be reliably performed with the memory
  1897. * device, the sequencer uses a guaranteed write mechanism to write data into
  1898. * the memory device.
  1899. */
  1900. static int rw_mgr_mem_calibrate_guaranteed_write(const u32 rw_group,
  1901. const u32 phase)
  1902. {
  1903. int ret;
  1904. /* Set a particular DQ/DQS phase. */
  1905. scc_mgr_set_dqdqs_output_phase_all_ranks(rw_group, phase);
  1906. debug_cond(DLEVEL == 1, "%s:%d guaranteed write: g=%u p=%u\n",
  1907. __func__, __LINE__, rw_group, phase);
  1908. /*
  1909. * Altera EMI_RM 2015.05.04 :: Figure 1-25
  1910. * Load up the patterns used by read calibration using the
  1911. * current DQDQS phase.
  1912. */
  1913. rw_mgr_mem_calibrate_read_load_patterns(0, 1);
  1914. if (gbl->phy_debug_mode_flags & PHY_DEBUG_DISABLE_GUARANTEED_READ)
  1915. return 0;
  1916. /*
  1917. * Altera EMI_RM 2015.05.04 :: Figure 1-26
  1918. * Back-to-Back reads of the patterns used for calibration.
  1919. */
  1920. ret = rw_mgr_mem_calibrate_read_test_patterns(0, rw_group, 1);
  1921. if (ret)
  1922. debug_cond(DLEVEL == 1,
  1923. "%s:%d Guaranteed read test failed: g=%u p=%u\n",
  1924. __func__, __LINE__, rw_group, phase);
  1925. return ret;
  1926. }
  1927. /**
  1928. * rw_mgr_mem_calibrate_dqs_enable_calibration() - DQS Enable Calibration
  1929. * @rw_group: Read/Write Group
  1930. * @test_bgn: Rank at which the test begins
  1931. *
  1932. * DQS enable calibration ensures reliable capture of the DQ signal without
  1933. * glitches on the DQS line.
  1934. */
  1935. static int rw_mgr_mem_calibrate_dqs_enable_calibration(const u32 rw_group,
  1936. const u32 test_bgn)
  1937. {
  1938. int ret;
  1939. /*
  1940. * Altera EMI_RM 2015.05.04 :: Figure 1-27
  1941. * DQS and DQS Eanble Signal Relationships.
  1942. */
  1943. ret = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay(
  1944. rw_group, rw_group, test_bgn);
  1945. if (!ret) /* FIXME: 0 means failure in this old code :-( */
  1946. return -EIO;
  1947. return 0;
  1948. }
  1949. /**
  1950. * rw_mgr_mem_calibrate_dq_dqs_centering() - Centering DQ/DQS
  1951. * @rw_group: Read/Write Group
  1952. * @test_bgn: Rank at which the test begins
  1953. * @use_read_test: Perform a read test
  1954. * @update_fom: Update FOM
  1955. *
  1956. * The centerin DQ/DQS stage attempts to align DQ and DQS signals on reads
  1957. * within a group.
  1958. */
  1959. static int
  1960. rw_mgr_mem_calibrate_dq_dqs_centering(const u32 rw_group, const u32 test_bgn,
  1961. const int use_read_test,
  1962. const int update_fom)
  1963. {
  1964. int ret, grp_calibrated;
  1965. u32 rank_bgn, sr;
  1966. /*
  1967. * Altera EMI_RM 2015.05.04 :: Figure 1-28
  1968. * Read per-bit deskew can be done on a per shadow register basis.
  1969. */
  1970. grp_calibrated = 1;
  1971. for (rank_bgn = 0, sr = 0;
  1972. rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
  1973. rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
  1974. /* Check if this set of ranks should be skipped entirely. */
  1975. if (param->skip_shadow_regs[sr])
  1976. continue;
  1977. ret = rw_mgr_mem_calibrate_vfifo_center(rank_bgn, rw_group,
  1978. rw_group, test_bgn,
  1979. use_read_test,
  1980. update_fom);
  1981. if (ret)
  1982. continue;
  1983. grp_calibrated = 0;
  1984. }
  1985. if (!grp_calibrated)
  1986. return -EIO;
  1987. return 0;
  1988. }
  1989. /**
  1990. * rw_mgr_mem_calibrate_vfifo() - Calibrate the read valid prediction FIFO
  1991. * @rw_group: Read/Write Group
  1992. * @test_bgn: Rank at which the test begins
  1993. *
  1994. * Stage 1: Calibrate the read valid prediction FIFO.
  1995. *
  1996. * This function implements UniPHY calibration Stage 1, as explained in
  1997. * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
  1998. *
  1999. * - read valid prediction will consist of finding:
  2000. * - DQS enable phase and DQS enable delay (DQS Enable Calibration)
  2001. * - DQS input phase and DQS input delay (DQ/DQS Centering)
  2002. * - we also do a per-bit deskew on the DQ lines.
  2003. */
  2004. static int rw_mgr_mem_calibrate_vfifo(const u32 rw_group, const u32 test_bgn)
  2005. {
  2006. uint32_t p, d;
  2007. uint32_t dtaps_per_ptap;
  2008. uint32_t failed_substage;
  2009. int ret;
  2010. debug("%s:%d: %u %u\n", __func__, __LINE__, rw_group, test_bgn);
  2011. /* Update info for sims */
  2012. reg_file_set_group(rw_group);
  2013. reg_file_set_stage(CAL_STAGE_VFIFO);
  2014. reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
  2015. failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
  2016. /* USER Determine number of delay taps for each phase tap. */
  2017. dtaps_per_ptap = DIV_ROUND_UP(IO_DELAY_PER_OPA_TAP,
  2018. IO_DELAY_PER_DQS_EN_DCHAIN_TAP) - 1;
  2019. for (d = 0; d <= dtaps_per_ptap; d += 2) {
  2020. /*
  2021. * In RLDRAMX we may be messing the delay of pins in
  2022. * the same write rw_group but outside of the current read
  2023. * the rw_group, but that's ok because we haven't calibrated
  2024. * output side yet.
  2025. */
  2026. if (d > 0) {
  2027. scc_mgr_apply_group_all_out_delay_add_all_ranks(
  2028. rw_group, d);
  2029. }
  2030. for (p = 0; p <= IO_DQDQS_OUT_PHASE_MAX; p++) {
  2031. /* 1) Guaranteed Write */
  2032. ret = rw_mgr_mem_calibrate_guaranteed_write(rw_group, p);
  2033. if (ret)
  2034. break;
  2035. /* 2) DQS Enable Calibration */
  2036. ret = rw_mgr_mem_calibrate_dqs_enable_calibration(rw_group,
  2037. test_bgn);
  2038. if (ret) {
  2039. failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
  2040. continue;
  2041. }
  2042. /* 3) Centering DQ/DQS */
  2043. /*
  2044. * If doing read after write calibration, do not update
  2045. * FOM now. Do it then.
  2046. */
  2047. ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group,
  2048. test_bgn, 1, 0);
  2049. if (ret) {
  2050. failed_substage = CAL_SUBSTAGE_VFIFO_CENTER;
  2051. continue;
  2052. }
  2053. /* All done. */
  2054. goto cal_done_ok;
  2055. }
  2056. }
  2057. /* Calibration Stage 1 failed. */
  2058. set_failing_group_stage(rw_group, CAL_STAGE_VFIFO, failed_substage);
  2059. return 0;
  2060. /* Calibration Stage 1 completed OK. */
  2061. cal_done_ok:
  2062. /*
  2063. * Reset the delay chains back to zero if they have moved > 1
  2064. * (check for > 1 because loop will increase d even when pass in
  2065. * first case).
  2066. */
  2067. if (d > 2)
  2068. scc_mgr_zero_group(rw_group, 1);
  2069. return 1;
  2070. }
  2071. /* VFIFO Calibration -- Read Deskew Calibration after write deskew */
  2072. static uint32_t rw_mgr_mem_calibrate_vfifo_end(uint32_t read_group,
  2073. uint32_t test_bgn)
  2074. {
  2075. uint32_t rank_bgn, sr;
  2076. uint32_t grp_calibrated;
  2077. uint32_t write_group;
  2078. debug("%s:%d %u %u", __func__, __LINE__, read_group, test_bgn);
  2079. /* update info for sims */
  2080. reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
  2081. reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
  2082. write_group = read_group;
  2083. /* update info for sims */
  2084. reg_file_set_group(read_group);
  2085. grp_calibrated = 1;
  2086. /* Read per-bit deskew can be done on a per shadow register basis */
  2087. for (rank_bgn = 0, sr = 0; rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
  2088. rank_bgn += NUM_RANKS_PER_SHADOW_REG, ++sr) {
  2089. /* Determine if this set of ranks should be skipped entirely */
  2090. if (!param->skip_shadow_regs[sr]) {
  2091. /* This is the last calibration round, update FOM here */
  2092. if (!rw_mgr_mem_calibrate_vfifo_center(rank_bgn,
  2093. write_group,
  2094. read_group,
  2095. test_bgn, 0,
  2096. 1)) {
  2097. grp_calibrated = 0;
  2098. }
  2099. }
  2100. }
  2101. if (grp_calibrated == 0) {
  2102. set_failing_group_stage(write_group,
  2103. CAL_STAGE_VFIFO_AFTER_WRITES,
  2104. CAL_SUBSTAGE_VFIFO_CENTER);
  2105. return 0;
  2106. }
  2107. return 1;
  2108. }
  2109. /* Calibrate LFIFO to find smallest read latency */
  2110. static uint32_t rw_mgr_mem_calibrate_lfifo(void)
  2111. {
  2112. uint32_t found_one;
  2113. uint32_t bit_chk;
  2114. debug("%s:%d\n", __func__, __LINE__);
  2115. /* update info for sims */
  2116. reg_file_set_stage(CAL_STAGE_LFIFO);
  2117. reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
  2118. /* Load up the patterns used by read calibration for all ranks */
  2119. rw_mgr_mem_calibrate_read_load_patterns(0, 1);
  2120. found_one = 0;
  2121. do {
  2122. writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
  2123. debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
  2124. __func__, __LINE__, gbl->curr_read_lat);
  2125. if (!rw_mgr_mem_calibrate_read_test_all_ranks(0,
  2126. NUM_READ_TESTS,
  2127. PASS_ALL_BITS,
  2128. &bit_chk, 1)) {
  2129. break;
  2130. }
  2131. found_one = 1;
  2132. /* reduce read latency and see if things are working */
  2133. /* correctly */
  2134. gbl->curr_read_lat--;
  2135. } while (gbl->curr_read_lat > 0);
  2136. /* reset the fifos to get pointers to known state */
  2137. writel(0, &phy_mgr_cmd->fifo_reset);
  2138. if (found_one) {
  2139. /* add a fudge factor to the read latency that was determined */
  2140. gbl->curr_read_lat += 2;
  2141. writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
  2142. debug_cond(DLEVEL == 2, "%s:%d lfifo: success: using \
  2143. read_lat=%u\n", __func__, __LINE__,
  2144. gbl->curr_read_lat);
  2145. return 1;
  2146. } else {
  2147. set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
  2148. CAL_SUBSTAGE_READ_LATENCY);
  2149. debug_cond(DLEVEL == 2, "%s:%d lfifo: failed at initial \
  2150. read_lat=%u\n", __func__, __LINE__,
  2151. gbl->curr_read_lat);
  2152. return 0;
  2153. }
  2154. }
  2155. /*
  2156. * issue write test command.
  2157. * two variants are provided. one that just tests a write pattern and
  2158. * another that tests datamask functionality.
  2159. */
  2160. static void rw_mgr_mem_calibrate_write_test_issue(uint32_t group,
  2161. uint32_t test_dm)
  2162. {
  2163. uint32_t mcc_instruction;
  2164. uint32_t quick_write_mode = (((STATIC_CALIB_STEPS) & CALIB_SKIP_WRITES) &&
  2165. ENABLE_SUPER_QUICK_CALIBRATION);
  2166. uint32_t rw_wl_nop_cycles;
  2167. uint32_t addr;
  2168. /*
  2169. * Set counter and jump addresses for the right
  2170. * number of NOP cycles.
  2171. * The number of supported NOP cycles can range from -1 to infinity
  2172. * Three different cases are handled:
  2173. *
  2174. * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
  2175. * mechanism will be used to insert the right number of NOPs
  2176. *
  2177. * 2. For a number of NOP cycles equals to 0, the micro-instruction
  2178. * issuing the write command will jump straight to the
  2179. * micro-instruction that turns on DQS (for DDRx), or outputs write
  2180. * data (for RLD), skipping
  2181. * the NOP micro-instruction all together
  2182. *
  2183. * 3. A number of NOP cycles equal to -1 indicates that DQS must be
  2184. * turned on in the same micro-instruction that issues the write
  2185. * command. Then we need
  2186. * to directly jump to the micro-instruction that sends out the data
  2187. *
  2188. * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
  2189. * (2 and 3). One jump-counter (0) is used to perform multiple
  2190. * write-read operations.
  2191. * one counter left to issue this command in "multiple-group" mode
  2192. */
  2193. rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
  2194. if (rw_wl_nop_cycles == -1) {
  2195. /*
  2196. * CNTR 2 - We want to execute the special write operation that
  2197. * turns on DQS right away and then skip directly to the
  2198. * instruction that sends out the data. We set the counter to a
  2199. * large 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_WL_1;
  2205. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DATA,
  2206. &sdr_rw_load_jump_mgr_regs->load_jump_add2);
  2207. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
  2208. &sdr_rw_load_jump_mgr_regs->load_jump_add3);
  2209. } else {
  2210. mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0_WL_1;
  2211. writel(RW_MGR_LFSR_WR_RD_BANK_0_DATA,
  2212. &sdr_rw_load_jump_mgr_regs->load_jump_add2);
  2213. writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
  2214. &sdr_rw_load_jump_mgr_regs->load_jump_add3);
  2215. }
  2216. } else if (rw_wl_nop_cycles == 0) {
  2217. /*
  2218. * CNTR 2 - We want to skip the NOP operation and go straight
  2219. * to the DQS enable instruction. We set the counter to a large
  2220. * number so that the jump is always taken.
  2221. */
  2222. writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
  2223. /* CNTR 3 - Not used */
  2224. if (test_dm) {
  2225. mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
  2226. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DQS,
  2227. &sdr_rw_load_jump_mgr_regs->load_jump_add2);
  2228. } else {
  2229. mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
  2230. writel(RW_MGR_LFSR_WR_RD_BANK_0_DQS,
  2231. &sdr_rw_load_jump_mgr_regs->load_jump_add2);
  2232. }
  2233. } else {
  2234. /*
  2235. * CNTR 2 - In this case we want to execute the next instruction
  2236. * and NOT take the jump. So we set the counter to 0. The jump
  2237. * address doesn't count.
  2238. */
  2239. writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
  2240. writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
  2241. /*
  2242. * CNTR 3 - Set the nop counter to the number of cycles we
  2243. * need to loop for, minus 1.
  2244. */
  2245. writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
  2246. if (test_dm) {
  2247. mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
  2248. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
  2249. &sdr_rw_load_jump_mgr_regs->load_jump_add3);
  2250. } else {
  2251. mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
  2252. writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
  2253. &sdr_rw_load_jump_mgr_regs->load_jump_add3);
  2254. }
  2255. }
  2256. writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
  2257. RW_MGR_RESET_READ_DATAPATH_OFFSET);
  2258. if (quick_write_mode)
  2259. writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
  2260. else
  2261. writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
  2262. writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
  2263. /*
  2264. * CNTR 1 - This is used to ensure enough time elapses
  2265. * for read data to come back.
  2266. */
  2267. writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
  2268. if (test_dm) {
  2269. writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_WAIT,
  2270. &sdr_rw_load_jump_mgr_regs->load_jump_add1);
  2271. } else {
  2272. writel(RW_MGR_LFSR_WR_RD_BANK_0_WAIT,
  2273. &sdr_rw_load_jump_mgr_regs->load_jump_add1);
  2274. }
  2275. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
  2276. writel(mcc_instruction, addr + (group << 2));
  2277. }
  2278. /* Test writes, can check for a single bit pass or multiple bit pass */
  2279. static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
  2280. uint32_t write_group, uint32_t use_dm, uint32_t all_correct,
  2281. uint32_t *bit_chk, uint32_t all_ranks)
  2282. {
  2283. uint32_t r;
  2284. uint32_t correct_mask_vg;
  2285. uint32_t tmp_bit_chk;
  2286. uint32_t vg;
  2287. uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
  2288. (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
  2289. uint32_t addr_rw_mgr;
  2290. uint32_t base_rw_mgr;
  2291. *bit_chk = param->write_correct_mask;
  2292. correct_mask_vg = param->write_correct_mask_vg;
  2293. for (r = rank_bgn; r < rank_end; r++) {
  2294. if (param->skip_ranks[r]) {
  2295. /* request to skip the rank */
  2296. continue;
  2297. }
  2298. /* set rank */
  2299. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
  2300. tmp_bit_chk = 0;
  2301. addr_rw_mgr = SDR_PHYGRP_RWMGRGRP_ADDRESS;
  2302. for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS-1; ; vg--) {
  2303. /* reset the fifos to get pointers to known state */
  2304. writel(0, &phy_mgr_cmd->fifo_reset);
  2305. tmp_bit_chk = tmp_bit_chk <<
  2306. (RW_MGR_MEM_DQ_PER_WRITE_DQS /
  2307. RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
  2308. rw_mgr_mem_calibrate_write_test_issue(write_group *
  2309. RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS+vg,
  2310. use_dm);
  2311. base_rw_mgr = readl(addr_rw_mgr);
  2312. tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
  2313. if (vg == 0)
  2314. break;
  2315. }
  2316. *bit_chk &= tmp_bit_chk;
  2317. }
  2318. if (all_correct) {
  2319. set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
  2320. debug_cond(DLEVEL == 2, "write_test(%u,%u,ALL) : %u == \
  2321. %u => %lu", write_group, use_dm,
  2322. *bit_chk, param->write_correct_mask,
  2323. (long unsigned int)(*bit_chk ==
  2324. param->write_correct_mask));
  2325. return *bit_chk == param->write_correct_mask;
  2326. } else {
  2327. set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
  2328. debug_cond(DLEVEL == 2, "write_test(%u,%u,ONE) : %u != ",
  2329. write_group, use_dm, *bit_chk);
  2330. debug_cond(DLEVEL == 2, "%lu" " => %lu", (long unsigned int)0,
  2331. (long unsigned int)(*bit_chk != 0));
  2332. return *bit_chk != 0x00;
  2333. }
  2334. }
  2335. /*
  2336. * center all windows. do per-bit-deskew to possibly increase size of
  2337. * certain windows.
  2338. */
  2339. static uint32_t rw_mgr_mem_calibrate_writes_center(uint32_t rank_bgn,
  2340. uint32_t write_group, uint32_t test_bgn)
  2341. {
  2342. uint32_t i, p, min_index;
  2343. int32_t d;
  2344. /*
  2345. * Store these as signed since there are comparisons with
  2346. * signed numbers.
  2347. */
  2348. uint32_t bit_chk;
  2349. uint32_t sticky_bit_chk;
  2350. int32_t left_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
  2351. int32_t right_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
  2352. int32_t mid;
  2353. int32_t mid_min, orig_mid_min;
  2354. int32_t new_dqs, start_dqs, shift_dq;
  2355. int32_t dq_margin, dqs_margin, dm_margin;
  2356. uint32_t stop;
  2357. uint32_t temp_dq_out1_delay;
  2358. uint32_t addr;
  2359. debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
  2360. dm_margin = 0;
  2361. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
  2362. start_dqs = readl(addr +
  2363. (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
  2364. /* per-bit deskew */
  2365. /*
  2366. * set the left and right edge of each bit to an illegal value
  2367. * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
  2368. */
  2369. sticky_bit_chk = 0;
  2370. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2371. left_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
  2372. right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
  2373. }
  2374. /* Search for the left edge of the window for each bit */
  2375. for (d = 0; d <= IO_IO_OUT1_DELAY_MAX; d++) {
  2376. scc_mgr_apply_group_dq_out1_delay(write_group, d);
  2377. writel(0, &sdr_scc_mgr->update);
  2378. /*
  2379. * Stop searching when the read test doesn't pass AND when
  2380. * we've seen a passing read on every bit.
  2381. */
  2382. stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
  2383. 0, PASS_ONE_BIT, &bit_chk, 0);
  2384. sticky_bit_chk = sticky_bit_chk | bit_chk;
  2385. stop = stop && (sticky_bit_chk == param->write_correct_mask);
  2386. debug_cond(DLEVEL == 2, "write_center(left): dtap=%d => %u \
  2387. == %u && %u [bit_chk= %u ]\n",
  2388. d, sticky_bit_chk, param->write_correct_mask,
  2389. stop, bit_chk);
  2390. if (stop == 1) {
  2391. break;
  2392. } else {
  2393. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2394. if (bit_chk & 1) {
  2395. /*
  2396. * Remember a passing test as the
  2397. * left_edge.
  2398. */
  2399. left_edge[i] = d;
  2400. } else {
  2401. /*
  2402. * If a left edge has not been seen
  2403. * yet, then a future passing test will
  2404. * mark this edge as the right edge.
  2405. */
  2406. if (left_edge[i] ==
  2407. IO_IO_OUT1_DELAY_MAX + 1) {
  2408. right_edge[i] = -(d + 1);
  2409. }
  2410. }
  2411. debug_cond(DLEVEL == 2, "write_center[l,d=%d):", d);
  2412. debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
  2413. (int)(bit_chk & 1), i, left_edge[i]);
  2414. debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
  2415. right_edge[i]);
  2416. bit_chk = bit_chk >> 1;
  2417. }
  2418. }
  2419. }
  2420. /* Reset DQ delay chains to 0 */
  2421. scc_mgr_apply_group_dq_out1_delay(0);
  2422. sticky_bit_chk = 0;
  2423. for (i = RW_MGR_MEM_DQ_PER_WRITE_DQS - 1;; i--) {
  2424. debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
  2425. %d right_edge[%u]: %d\n", __func__, __LINE__,
  2426. i, left_edge[i], i, right_edge[i]);
  2427. /*
  2428. * Check for cases where we haven't found the left edge,
  2429. * which makes our assignment of the the right edge invalid.
  2430. * Reset it to the illegal value.
  2431. */
  2432. if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) &&
  2433. (right_edge[i] != IO_IO_OUT1_DELAY_MAX + 1)) {
  2434. right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
  2435. debug_cond(DLEVEL == 2, "%s:%d write_center: reset \
  2436. right_edge[%u]: %d\n", __func__, __LINE__,
  2437. i, right_edge[i]);
  2438. }
  2439. /*
  2440. * Reset sticky bit (except for bits where we have
  2441. * seen the left edge).
  2442. */
  2443. sticky_bit_chk = sticky_bit_chk << 1;
  2444. if ((left_edge[i] != IO_IO_OUT1_DELAY_MAX + 1))
  2445. sticky_bit_chk = sticky_bit_chk | 1;
  2446. if (i == 0)
  2447. break;
  2448. }
  2449. /* Search for the right edge of the window for each bit */
  2450. for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - start_dqs; d++) {
  2451. scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
  2452. d + start_dqs);
  2453. writel(0, &sdr_scc_mgr->update);
  2454. /*
  2455. * Stop searching when the read test doesn't pass AND when
  2456. * we've seen a passing read on every bit.
  2457. */
  2458. stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
  2459. 0, PASS_ONE_BIT, &bit_chk, 0);
  2460. sticky_bit_chk = sticky_bit_chk | bit_chk;
  2461. stop = stop && (sticky_bit_chk == param->write_correct_mask);
  2462. debug_cond(DLEVEL == 2, "write_center (right): dtap=%u => %u == \
  2463. %u && %u\n", d, sticky_bit_chk,
  2464. param->write_correct_mask, stop);
  2465. if (stop == 1) {
  2466. if (d == 0) {
  2467. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS;
  2468. i++) {
  2469. /* d = 0 failed, but it passed when
  2470. testing the left edge, so it must be
  2471. marginal, set it to -1 */
  2472. if (right_edge[i] ==
  2473. IO_IO_OUT1_DELAY_MAX + 1 &&
  2474. left_edge[i] !=
  2475. IO_IO_OUT1_DELAY_MAX + 1) {
  2476. right_edge[i] = -1;
  2477. }
  2478. }
  2479. }
  2480. break;
  2481. } else {
  2482. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2483. if (bit_chk & 1) {
  2484. /*
  2485. * Remember a passing test as
  2486. * the right_edge.
  2487. */
  2488. right_edge[i] = d;
  2489. } else {
  2490. if (d != 0) {
  2491. /*
  2492. * If a right edge has not
  2493. * been seen yet, then a future
  2494. * passing test will mark this
  2495. * edge as the left edge.
  2496. */
  2497. if (right_edge[i] ==
  2498. IO_IO_OUT1_DELAY_MAX + 1)
  2499. left_edge[i] = -(d + 1);
  2500. } else {
  2501. /*
  2502. * d = 0 failed, but it passed
  2503. * when testing the left edge,
  2504. * so it must be marginal, set
  2505. * it to -1.
  2506. */
  2507. if (right_edge[i] ==
  2508. IO_IO_OUT1_DELAY_MAX + 1 &&
  2509. left_edge[i] !=
  2510. IO_IO_OUT1_DELAY_MAX + 1)
  2511. right_edge[i] = -1;
  2512. /*
  2513. * If a right edge has not been
  2514. * seen yet, then a future
  2515. * passing test will mark this
  2516. * edge as the left edge.
  2517. */
  2518. else if (right_edge[i] ==
  2519. IO_IO_OUT1_DELAY_MAX +
  2520. 1)
  2521. left_edge[i] = -(d + 1);
  2522. }
  2523. }
  2524. debug_cond(DLEVEL == 2, "write_center[r,d=%d):", d);
  2525. debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
  2526. (int)(bit_chk & 1), i, left_edge[i]);
  2527. debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
  2528. right_edge[i]);
  2529. bit_chk = bit_chk >> 1;
  2530. }
  2531. }
  2532. }
  2533. /* Check that all bits have a window */
  2534. for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2535. debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
  2536. %d right_edge[%u]: %d", __func__, __LINE__,
  2537. i, left_edge[i], i, right_edge[i]);
  2538. if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) ||
  2539. (right_edge[i] == IO_IO_OUT1_DELAY_MAX + 1)) {
  2540. set_failing_group_stage(test_bgn + i,
  2541. CAL_STAGE_WRITES,
  2542. CAL_SUBSTAGE_WRITES_CENTER);
  2543. return 0;
  2544. }
  2545. }
  2546. /* Find middle of window for each DQ bit */
  2547. mid_min = left_edge[0] - right_edge[0];
  2548. min_index = 0;
  2549. for (i = 1; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
  2550. mid = left_edge[i] - right_edge[i];
  2551. if (mid < mid_min) {
  2552. mid_min = mid;
  2553. min_index = i;
  2554. }
  2555. }
  2556. /*
  2557. * -mid_min/2 represents the amount that we need to move DQS.
  2558. * If mid_min is odd and positive we'll need to add one to
  2559. * make sure the rounding in further calculations is correct
  2560. * (always bias to the right), so just add 1 for all positive values.
  2561. */
  2562. if (mid_min > 0)
  2563. mid_min++;
  2564. mid_min = mid_min / 2;
  2565. debug_cond(DLEVEL == 1, "%s:%d write_center: mid_min=%d\n", __func__,
  2566. __LINE__, mid_min);
  2567. /* Determine the amount we can change DQS (which is -mid_min) */
  2568. orig_mid_min = mid_min;
  2569. new_dqs = start_dqs;
  2570. mid_min = 0;
  2571. debug_cond(DLEVEL == 1, "%s:%d write_center: start_dqs=%d new_dqs=%d \
  2572. mid_min=%d\n", __func__, __LINE__, start_dqs, new_dqs, mid_min);
  2573. /* Initialize data for export structures */
  2574. dqs_margin = IO_IO_OUT1_DELAY_MAX + 1;
  2575. dq_margin = IO_IO_OUT1_DELAY_MAX + 1;
  2576. /* add delay to bring centre of all DQ windows to the same "level" */
  2577. for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
  2578. /* Use values before divide by 2 to reduce round off error */
  2579. shift_dq = (left_edge[i] - right_edge[i] -
  2580. (left_edge[min_index] - right_edge[min_index]))/2 +
  2581. (orig_mid_min - mid_min);
  2582. debug_cond(DLEVEL == 2, "%s:%d write_center: before: shift_dq \
  2583. [%u]=%d\n", __func__, __LINE__, i, shift_dq);
  2584. addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
  2585. temp_dq_out1_delay = readl(addr + (i << 2));
  2586. if (shift_dq + (int32_t)temp_dq_out1_delay >
  2587. (int32_t)IO_IO_OUT1_DELAY_MAX) {
  2588. shift_dq = (int32_t)IO_IO_OUT1_DELAY_MAX - temp_dq_out1_delay;
  2589. } else if (shift_dq + (int32_t)temp_dq_out1_delay < 0) {
  2590. shift_dq = -(int32_t)temp_dq_out1_delay;
  2591. }
  2592. debug_cond(DLEVEL == 2, "write_center: after: shift_dq[%u]=%d\n",
  2593. i, shift_dq);
  2594. scc_mgr_set_dq_out1_delay(i, temp_dq_out1_delay + shift_dq);
  2595. scc_mgr_load_dq(i);
  2596. debug_cond(DLEVEL == 2, "write_center: margin[%u]=[%d,%d]\n", i,
  2597. left_edge[i] - shift_dq + (-mid_min),
  2598. right_edge[i] + shift_dq - (-mid_min));
  2599. /* To determine values for export structures */
  2600. if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
  2601. dq_margin = left_edge[i] - shift_dq + (-mid_min);
  2602. if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
  2603. dqs_margin = right_edge[i] + shift_dq - (-mid_min);
  2604. }
  2605. /* Move DQS */
  2606. scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
  2607. writel(0, &sdr_scc_mgr->update);
  2608. /* Centre DM */
  2609. debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
  2610. /*
  2611. * set the left and right edge of each bit to an illegal value,
  2612. * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value,
  2613. */
  2614. left_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
  2615. right_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
  2616. int32_t bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2617. int32_t end_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2618. int32_t bgn_best = IO_IO_OUT1_DELAY_MAX + 1;
  2619. int32_t end_best = IO_IO_OUT1_DELAY_MAX + 1;
  2620. int32_t win_best = 0;
  2621. /* Search for the/part of the window with DM shift */
  2622. for (d = IO_IO_OUT1_DELAY_MAX; d >= 0; d -= DELTA_D) {
  2623. scc_mgr_apply_group_dm_out1_delay(d);
  2624. writel(0, &sdr_scc_mgr->update);
  2625. if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
  2626. PASS_ALL_BITS, &bit_chk,
  2627. 0)) {
  2628. /* USE Set current end of the window */
  2629. end_curr = -d;
  2630. /*
  2631. * If a starting edge of our window has not been seen
  2632. * this is our current start of the DM window.
  2633. */
  2634. if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
  2635. bgn_curr = -d;
  2636. /*
  2637. * If current window is bigger than best seen.
  2638. * Set best seen to be current window.
  2639. */
  2640. if ((end_curr-bgn_curr+1) > win_best) {
  2641. win_best = end_curr-bgn_curr+1;
  2642. bgn_best = bgn_curr;
  2643. end_best = end_curr;
  2644. }
  2645. } else {
  2646. /* We just saw a failing test. Reset temp edge */
  2647. bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2648. end_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2649. }
  2650. }
  2651. /* Reset DM delay chains to 0 */
  2652. scc_mgr_apply_group_dm_out1_delay(0);
  2653. /*
  2654. * Check to see if the current window nudges up aganist 0 delay.
  2655. * If so we need to continue the search by shifting DQS otherwise DQS
  2656. * search begins as a new search. */
  2657. if (end_curr != 0) {
  2658. bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2659. end_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2660. }
  2661. /* Search for the/part of the window with DQS shifts */
  2662. for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - new_dqs; d += DELTA_D) {
  2663. /*
  2664. * Note: This only shifts DQS, so are we limiting ourselve to
  2665. * width of DQ unnecessarily.
  2666. */
  2667. scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
  2668. d + new_dqs);
  2669. writel(0, &sdr_scc_mgr->update);
  2670. if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
  2671. PASS_ALL_BITS, &bit_chk,
  2672. 0)) {
  2673. /* USE Set current end of the window */
  2674. end_curr = d;
  2675. /*
  2676. * If a beginning edge of our window has not been seen
  2677. * this is our current begin of the DM window.
  2678. */
  2679. if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
  2680. bgn_curr = d;
  2681. /*
  2682. * If current window is bigger than best seen. Set best
  2683. * seen to be current window.
  2684. */
  2685. if ((end_curr-bgn_curr+1) > win_best) {
  2686. win_best = end_curr-bgn_curr+1;
  2687. bgn_best = bgn_curr;
  2688. end_best = end_curr;
  2689. }
  2690. } else {
  2691. /* We just saw a failing test. Reset temp edge */
  2692. bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2693. end_curr = IO_IO_OUT1_DELAY_MAX + 1;
  2694. /* Early exit optimization: if ther remaining delay
  2695. chain space is less than already seen largest window
  2696. we can exit */
  2697. if ((win_best-1) >
  2698. (IO_IO_OUT1_DELAY_MAX - new_dqs - d)) {
  2699. break;
  2700. }
  2701. }
  2702. }
  2703. /* assign left and right edge for cal and reporting; */
  2704. left_edge[0] = -1*bgn_best;
  2705. right_edge[0] = end_best;
  2706. debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n", __func__,
  2707. __LINE__, left_edge[0], right_edge[0]);
  2708. /* Move DQS (back to orig) */
  2709. scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
  2710. /* Move DM */
  2711. /* Find middle of window for the DM bit */
  2712. mid = (left_edge[0] - right_edge[0]) / 2;
  2713. /* only move right, since we are not moving DQS/DQ */
  2714. if (mid < 0)
  2715. mid = 0;
  2716. /* dm_marign should fail if we never find a window */
  2717. if (win_best == 0)
  2718. dm_margin = -1;
  2719. else
  2720. dm_margin = left_edge[0] - mid;
  2721. scc_mgr_apply_group_dm_out1_delay(mid);
  2722. writel(0, &sdr_scc_mgr->update);
  2723. debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d mid=%d \
  2724. dm_margin=%d\n", __func__, __LINE__, left_edge[0],
  2725. right_edge[0], mid, dm_margin);
  2726. /* Export values */
  2727. gbl->fom_out += dq_margin + dqs_margin;
  2728. debug_cond(DLEVEL == 2, "%s:%d write_center: dq_margin=%d \
  2729. dqs_margin=%d dm_margin=%d\n", __func__, __LINE__,
  2730. dq_margin, dqs_margin, dm_margin);
  2731. /*
  2732. * Do not remove this line as it makes sure all of our
  2733. * decisions have been applied.
  2734. */
  2735. writel(0, &sdr_scc_mgr->update);
  2736. return (dq_margin >= 0) && (dqs_margin >= 0) && (dm_margin >= 0);
  2737. }
  2738. /* calibrate the write operations */
  2739. static uint32_t rw_mgr_mem_calibrate_writes(uint32_t rank_bgn, uint32_t g,
  2740. uint32_t test_bgn)
  2741. {
  2742. /* update info for sims */
  2743. debug("%s:%d %u %u\n", __func__, __LINE__, g, test_bgn);
  2744. reg_file_set_stage(CAL_STAGE_WRITES);
  2745. reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
  2746. reg_file_set_group(g);
  2747. if (!rw_mgr_mem_calibrate_writes_center(rank_bgn, g, test_bgn)) {
  2748. set_failing_group_stage(g, CAL_STAGE_WRITES,
  2749. CAL_SUBSTAGE_WRITES_CENTER);
  2750. return 0;
  2751. }
  2752. return 1;
  2753. }
  2754. /**
  2755. * mem_precharge_and_activate() - Precharge all banks and activate
  2756. *
  2757. * Precharge all banks and activate row 0 in bank "000..." and bank "111...".
  2758. */
  2759. static void mem_precharge_and_activate(void)
  2760. {
  2761. int r;
  2762. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
  2763. /* Test if the rank should be skipped. */
  2764. if (param->skip_ranks[r])
  2765. continue;
  2766. /* Set rank. */
  2767. set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
  2768. /* Precharge all banks. */
  2769. writel(RW_MGR_PRECHARGE_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
  2770. RW_MGR_RUN_SINGLE_GROUP_OFFSET);
  2771. writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
  2772. writel(RW_MGR_ACTIVATE_0_AND_1_WAIT1,
  2773. &sdr_rw_load_jump_mgr_regs->load_jump_add0);
  2774. writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
  2775. writel(RW_MGR_ACTIVATE_0_AND_1_WAIT2,
  2776. &sdr_rw_load_jump_mgr_regs->load_jump_add1);
  2777. /* Activate rows. */
  2778. writel(RW_MGR_ACTIVATE_0_AND_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
  2779. RW_MGR_RUN_SINGLE_GROUP_OFFSET);
  2780. }
  2781. }
  2782. /**
  2783. * mem_init_latency() - Configure memory RLAT and WLAT settings
  2784. *
  2785. * Configure memory RLAT and WLAT parameters.
  2786. */
  2787. static void mem_init_latency(void)
  2788. {
  2789. /*
  2790. * For AV/CV, LFIFO is hardened and always runs at full rate
  2791. * so max latency in AFI clocks, used here, is correspondingly
  2792. * smaller.
  2793. */
  2794. const u32 max_latency = (1 << MAX_LATENCY_COUNT_WIDTH) - 1;
  2795. u32 rlat, wlat;
  2796. debug("%s:%d\n", __func__, __LINE__);
  2797. /*
  2798. * Read in write latency.
  2799. * WL for Hard PHY does not include additive latency.
  2800. */
  2801. wlat = readl(&data_mgr->t_wl_add);
  2802. wlat += readl(&data_mgr->mem_t_add);
  2803. gbl->rw_wl_nop_cycles = wlat - 1;
  2804. /* Read in readl latency. */
  2805. rlat = readl(&data_mgr->t_rl_add);
  2806. /* Set a pretty high read latency initially. */
  2807. gbl->curr_read_lat = rlat + 16;
  2808. if (gbl->curr_read_lat > max_latency)
  2809. gbl->curr_read_lat = max_latency;
  2810. writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
  2811. /* Advertise write latency. */
  2812. writel(wlat, &phy_mgr_cfg->afi_wlat);
  2813. }
  2814. /**
  2815. * @mem_skip_calibrate() - Set VFIFO and LFIFO to instant-on settings
  2816. *
  2817. * Set VFIFO and LFIFO to instant-on settings in skip calibration mode.
  2818. */
  2819. static void mem_skip_calibrate(void)
  2820. {
  2821. uint32_t vfifo_offset;
  2822. uint32_t i, j, r;
  2823. debug("%s:%d\n", __func__, __LINE__);
  2824. /* Need to update every shadow register set used by the interface */
  2825. for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
  2826. r += NUM_RANKS_PER_SHADOW_REG) {
  2827. /*
  2828. * Set output phase alignment settings appropriate for
  2829. * skip calibration.
  2830. */
  2831. for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
  2832. scc_mgr_set_dqs_en_phase(i, 0);
  2833. #if IO_DLL_CHAIN_LENGTH == 6
  2834. scc_mgr_set_dqdqs_output_phase(i, 6);
  2835. #else
  2836. scc_mgr_set_dqdqs_output_phase(i, 7);
  2837. #endif
  2838. /*
  2839. * Case:33398
  2840. *
  2841. * Write data arrives to the I/O two cycles before write
  2842. * latency is reached (720 deg).
  2843. * -> due to bit-slip in a/c bus
  2844. * -> to allow board skew where dqs is longer than ck
  2845. * -> how often can this happen!?
  2846. * -> can claim back some ptaps for high freq
  2847. * support if we can relax this, but i digress...
  2848. *
  2849. * The write_clk leads mem_ck by 90 deg
  2850. * The minimum ptap of the OPA is 180 deg
  2851. * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
  2852. * The write_clk is always delayed by 2 ptaps
  2853. *
  2854. * Hence, to make DQS aligned to CK, we need to delay
  2855. * DQS by:
  2856. * (720 - 90 - 180 - 2 * (360 / IO_DLL_CHAIN_LENGTH))
  2857. *
  2858. * Dividing the above by (360 / IO_DLL_CHAIN_LENGTH)
  2859. * gives us the number of ptaps, which simplies to:
  2860. *
  2861. * (1.25 * IO_DLL_CHAIN_LENGTH - 2)
  2862. */
  2863. scc_mgr_set_dqdqs_output_phase(i,
  2864. 1.25 * IO_DLL_CHAIN_LENGTH - 2);
  2865. }
  2866. writel(0xff, &sdr_scc_mgr->dqs_ena);
  2867. writel(0xff, &sdr_scc_mgr->dqs_io_ena);
  2868. for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
  2869. writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
  2870. SCC_MGR_GROUP_COUNTER_OFFSET);
  2871. }
  2872. writel(0xff, &sdr_scc_mgr->dq_ena);
  2873. writel(0xff, &sdr_scc_mgr->dm_ena);
  2874. writel(0, &sdr_scc_mgr->update);
  2875. }
  2876. /* Compensate for simulation model behaviour */
  2877. for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
  2878. scc_mgr_set_dqs_bus_in_delay(i, 10);
  2879. scc_mgr_load_dqs(i);
  2880. }
  2881. writel(0, &sdr_scc_mgr->update);
  2882. /*
  2883. * ArriaV has hard FIFOs that can only be initialized by incrementing
  2884. * in sequencer.
  2885. */
  2886. vfifo_offset = CALIB_VFIFO_OFFSET;
  2887. for (j = 0; j < vfifo_offset; j++)
  2888. writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
  2889. writel(0, &phy_mgr_cmd->fifo_reset);
  2890. /*
  2891. * For Arria V and Cyclone V with hard LFIFO, we get the skip-cal
  2892. * setting from generation-time constant.
  2893. */
  2894. gbl->curr_read_lat = CALIB_LFIFO_OFFSET;
  2895. writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
  2896. }
  2897. /**
  2898. * mem_calibrate() - Memory calibration entry point.
  2899. *
  2900. * Perform memory calibration.
  2901. */
  2902. static uint32_t mem_calibrate(void)
  2903. {
  2904. uint32_t i;
  2905. uint32_t rank_bgn, sr;
  2906. uint32_t write_group, write_test_bgn;
  2907. uint32_t read_group, read_test_bgn;
  2908. uint32_t run_groups, current_run;
  2909. uint32_t failing_groups = 0;
  2910. uint32_t group_failed = 0;
  2911. const u32 rwdqs_ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
  2912. RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
  2913. debug("%s:%d\n", __func__, __LINE__);
  2914. /* Initialize the data settings */
  2915. gbl->error_substage = CAL_SUBSTAGE_NIL;
  2916. gbl->error_stage = CAL_STAGE_NIL;
  2917. gbl->error_group = 0xff;
  2918. gbl->fom_in = 0;
  2919. gbl->fom_out = 0;
  2920. /* Initialize WLAT and RLAT. */
  2921. mem_init_latency();
  2922. /* Initialize bit slips. */
  2923. mem_precharge_and_activate();
  2924. for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
  2925. writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
  2926. SCC_MGR_GROUP_COUNTER_OFFSET);
  2927. /* Only needed once to set all groups, pins, DQ, DQS, DM. */
  2928. if (i == 0)
  2929. scc_mgr_set_hhp_extras();
  2930. scc_set_bypass_mode(i);
  2931. }
  2932. /* Calibration is skipped. */
  2933. if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
  2934. /*
  2935. * Set VFIFO and LFIFO to instant-on settings in skip
  2936. * calibration mode.
  2937. */
  2938. mem_skip_calibrate();
  2939. /*
  2940. * Do not remove this line as it makes sure all of our
  2941. * decisions have been applied.
  2942. */
  2943. writel(0, &sdr_scc_mgr->update);
  2944. return 1;
  2945. }
  2946. /* Calibration is not skipped. */
  2947. for (i = 0; i < NUM_CALIB_REPEAT; i++) {
  2948. /*
  2949. * Zero all delay chain/phase settings for all
  2950. * groups and all shadow register sets.
  2951. */
  2952. scc_mgr_zero_all();
  2953. run_groups = ~param->skip_groups;
  2954. for (write_group = 0, write_test_bgn = 0; write_group
  2955. < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; write_group++,
  2956. write_test_bgn += RW_MGR_MEM_DQ_PER_WRITE_DQS) {
  2957. /* Initialize the group failure */
  2958. group_failed = 0;
  2959. current_run = run_groups & ((1 <<
  2960. RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
  2961. run_groups = run_groups >>
  2962. RW_MGR_NUM_DQS_PER_WRITE_GROUP;
  2963. if (current_run == 0)
  2964. continue;
  2965. writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
  2966. SCC_MGR_GROUP_COUNTER_OFFSET);
  2967. scc_mgr_zero_group(write_group, 0);
  2968. for (read_group = write_group * rwdqs_ratio,
  2969. read_test_bgn = 0;
  2970. read_group < (write_group + 1) * rwdqs_ratio;
  2971. read_group++,
  2972. read_test_bgn += RW_MGR_MEM_DQ_PER_READ_DQS) {
  2973. if (STATIC_CALIB_STEPS & CALIB_SKIP_VFIFO)
  2974. continue;
  2975. /* Calibrate the VFIFO */
  2976. if (rw_mgr_mem_calibrate_vfifo(read_group,
  2977. read_test_bgn))
  2978. continue;
  2979. if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
  2980. return 0;
  2981. /* The group failed, we're done. */
  2982. goto grp_failed;
  2983. }
  2984. /* Calibrate the output side */
  2985. for (rank_bgn = 0, sr = 0;
  2986. rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
  2987. rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
  2988. if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
  2989. continue;
  2990. /* Not needed in quick mode! */
  2991. if (STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS)
  2992. continue;
  2993. /*
  2994. * Determine if this set of ranks
  2995. * should be skipped entirely.
  2996. */
  2997. if (param->skip_shadow_regs[sr])
  2998. continue;
  2999. /* Calibrate WRITEs */
  3000. if (rw_mgr_mem_calibrate_writes(rank_bgn,
  3001. write_group, write_test_bgn))
  3002. continue;
  3003. group_failed = 1;
  3004. if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
  3005. return 0;
  3006. }
  3007. /* Some group failed, we're done. */
  3008. if (group_failed)
  3009. goto grp_failed;
  3010. for (read_group = write_group * rwdqs_ratio,
  3011. read_test_bgn = 0;
  3012. read_group < (write_group + 1) * rwdqs_ratio;
  3013. read_group++,
  3014. read_test_bgn += RW_MGR_MEM_DQ_PER_READ_DQS) {
  3015. if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
  3016. continue;
  3017. if (rw_mgr_mem_calibrate_vfifo_end(read_group,
  3018. read_test_bgn))
  3019. continue;
  3020. if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
  3021. return 0;
  3022. /* The group failed, we're done. */
  3023. goto grp_failed;
  3024. }
  3025. /* No group failed, continue as usual. */
  3026. continue;
  3027. grp_failed: /* A group failed, increment the counter. */
  3028. failing_groups++;
  3029. }
  3030. /*
  3031. * USER If there are any failing groups then report
  3032. * the failure.
  3033. */
  3034. if (failing_groups != 0)
  3035. return 0;
  3036. if (STATIC_CALIB_STEPS & CALIB_SKIP_LFIFO)
  3037. continue;
  3038. /*
  3039. * If we're skipping groups as part of debug,
  3040. * don't calibrate LFIFO.
  3041. */
  3042. if (param->skip_groups != 0)
  3043. continue;
  3044. /* Calibrate the LFIFO */
  3045. if (!rw_mgr_mem_calibrate_lfifo())
  3046. return 0;
  3047. }
  3048. /*
  3049. * Do not remove this line as it makes sure all of our decisions
  3050. * have been applied.
  3051. */
  3052. writel(0, &sdr_scc_mgr->update);
  3053. return 1;
  3054. }
  3055. /**
  3056. * run_mem_calibrate() - Perform memory calibration
  3057. *
  3058. * This function triggers the entire memory calibration procedure.
  3059. */
  3060. static int run_mem_calibrate(void)
  3061. {
  3062. int pass;
  3063. debug("%s:%d\n", __func__, __LINE__);
  3064. /* Reset pass/fail status shown on afi_cal_success/fail */
  3065. writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
  3066. /* Stop tracking manager. */
  3067. clrbits_le32(&sdr_ctrl->ctrl_cfg, 1 << 22);
  3068. phy_mgr_initialize();
  3069. rw_mgr_mem_initialize();
  3070. /* Perform the actual memory calibration. */
  3071. pass = mem_calibrate();
  3072. mem_precharge_and_activate();
  3073. writel(0, &phy_mgr_cmd->fifo_reset);
  3074. /* Handoff. */
  3075. rw_mgr_mem_handoff();
  3076. /*
  3077. * In Hard PHY this is a 2-bit control:
  3078. * 0: AFI Mux Select
  3079. * 1: DDIO Mux Select
  3080. */
  3081. writel(0x2, &phy_mgr_cfg->mux_sel);
  3082. /* Start tracking manager. */
  3083. setbits_le32(&sdr_ctrl->ctrl_cfg, 1 << 22);
  3084. return pass;
  3085. }
  3086. /**
  3087. * debug_mem_calibrate() - Report result of memory calibration
  3088. * @pass: Value indicating whether calibration passed or failed
  3089. *
  3090. * This function reports the results of the memory calibration
  3091. * and writes debug information into the register file.
  3092. */
  3093. static void debug_mem_calibrate(int pass)
  3094. {
  3095. uint32_t debug_info;
  3096. if (pass) {
  3097. printf("%s: CALIBRATION PASSED\n", __FILE__);
  3098. gbl->fom_in /= 2;
  3099. gbl->fom_out /= 2;
  3100. if (gbl->fom_in > 0xff)
  3101. gbl->fom_in = 0xff;
  3102. if (gbl->fom_out > 0xff)
  3103. gbl->fom_out = 0xff;
  3104. /* Update the FOM in the register file */
  3105. debug_info = gbl->fom_in;
  3106. debug_info |= gbl->fom_out << 8;
  3107. writel(debug_info, &sdr_reg_file->fom);
  3108. writel(debug_info, &phy_mgr_cfg->cal_debug_info);
  3109. writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
  3110. } else {
  3111. printf("%s: CALIBRATION FAILED\n", __FILE__);
  3112. debug_info = gbl->error_stage;
  3113. debug_info |= gbl->error_substage << 8;
  3114. debug_info |= gbl->error_group << 16;
  3115. writel(debug_info, &sdr_reg_file->failing_stage);
  3116. writel(debug_info, &phy_mgr_cfg->cal_debug_info);
  3117. writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
  3118. /* Update the failing group/stage in the register file */
  3119. debug_info = gbl->error_stage;
  3120. debug_info |= gbl->error_substage << 8;
  3121. debug_info |= gbl->error_group << 16;
  3122. writel(debug_info, &sdr_reg_file->failing_stage);
  3123. }
  3124. printf("%s: Calibration complete\n", __FILE__);
  3125. }
  3126. /**
  3127. * hc_initialize_rom_data() - Initialize ROM data
  3128. *
  3129. * Initialize ROM data.
  3130. */
  3131. static void hc_initialize_rom_data(void)
  3132. {
  3133. u32 i, addr;
  3134. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
  3135. for (i = 0; i < ARRAY_SIZE(inst_rom_init); i++)
  3136. writel(inst_rom_init[i], addr + (i << 2));
  3137. addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
  3138. for (i = 0; i < ARRAY_SIZE(ac_rom_init); i++)
  3139. writel(ac_rom_init[i], addr + (i << 2));
  3140. }
  3141. /**
  3142. * initialize_reg_file() - Initialize SDR register file
  3143. *
  3144. * Initialize SDR register file.
  3145. */
  3146. static void initialize_reg_file(void)
  3147. {
  3148. /* Initialize the register file with the correct data */
  3149. writel(REG_FILE_INIT_SEQ_SIGNATURE, &sdr_reg_file->signature);
  3150. writel(0, &sdr_reg_file->debug_data_addr);
  3151. writel(0, &sdr_reg_file->cur_stage);
  3152. writel(0, &sdr_reg_file->fom);
  3153. writel(0, &sdr_reg_file->failing_stage);
  3154. writel(0, &sdr_reg_file->debug1);
  3155. writel(0, &sdr_reg_file->debug2);
  3156. }
  3157. /**
  3158. * initialize_hps_phy() - Initialize HPS PHY
  3159. *
  3160. * Initialize HPS PHY.
  3161. */
  3162. static void initialize_hps_phy(void)
  3163. {
  3164. uint32_t reg;
  3165. /*
  3166. * Tracking also gets configured here because it's in the
  3167. * same register.
  3168. */
  3169. uint32_t trk_sample_count = 7500;
  3170. uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
  3171. /*
  3172. * Format is number of outer loops in the 16 MSB, sample
  3173. * count in 16 LSB.
  3174. */
  3175. reg = 0;
  3176. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
  3177. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
  3178. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
  3179. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
  3180. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
  3181. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
  3182. /*
  3183. * This field selects the intrinsic latency to RDATA_EN/FULL path.
  3184. * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
  3185. */
  3186. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
  3187. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
  3188. trk_sample_count);
  3189. writel(reg, &sdr_ctrl->phy_ctrl0);
  3190. reg = 0;
  3191. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
  3192. trk_sample_count >>
  3193. SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
  3194. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
  3195. trk_long_idle_sample_count);
  3196. writel(reg, &sdr_ctrl->phy_ctrl1);
  3197. reg = 0;
  3198. reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
  3199. trk_long_idle_sample_count >>
  3200. SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
  3201. writel(reg, &sdr_ctrl->phy_ctrl2);
  3202. }
  3203. /**
  3204. * initialize_tracking() - Initialize tracking
  3205. *
  3206. * Initialize the register file with usable initial data.
  3207. */
  3208. static void initialize_tracking(void)
  3209. {
  3210. /*
  3211. * Initialize the register file with the correct data.
  3212. * Compute usable version of value in case we skip full
  3213. * computation later.
  3214. */
  3215. writel(DIV_ROUND_UP(IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP) - 1,
  3216. &sdr_reg_file->dtaps_per_ptap);
  3217. /* trk_sample_count */
  3218. writel(7500, &sdr_reg_file->trk_sample_count);
  3219. /* longidle outer loop [15:0] */
  3220. writel((10 << 16) | (100 << 0), &sdr_reg_file->trk_longidle);
  3221. /*
  3222. * longidle sample count [31:24]
  3223. * trfc, worst case of 933Mhz 4Gb [23:16]
  3224. * trcd, worst case [15:8]
  3225. * vfifo wait [7:0]
  3226. */
  3227. writel((243 << 24) | (14 << 16) | (10 << 8) | (4 << 0),
  3228. &sdr_reg_file->delays);
  3229. /* mux delay */
  3230. writel((RW_MGR_IDLE << 24) | (RW_MGR_ACTIVATE_1 << 16) |
  3231. (RW_MGR_SGLE_READ << 8) | (RW_MGR_PRECHARGE_ALL << 0),
  3232. &sdr_reg_file->trk_rw_mgr_addr);
  3233. writel(RW_MGR_MEM_IF_READ_DQS_WIDTH,
  3234. &sdr_reg_file->trk_read_dqs_width);
  3235. /* trefi [7:0] */
  3236. writel((RW_MGR_REFRESH_ALL << 24) | (1000 << 0),
  3237. &sdr_reg_file->trk_rfsh);
  3238. }
  3239. int sdram_calibration_full(void)
  3240. {
  3241. struct param_type my_param;
  3242. struct gbl_type my_gbl;
  3243. uint32_t pass;
  3244. memset(&my_param, 0, sizeof(my_param));
  3245. memset(&my_gbl, 0, sizeof(my_gbl));
  3246. param = &my_param;
  3247. gbl = &my_gbl;
  3248. /* Set the calibration enabled by default */
  3249. gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
  3250. /*
  3251. * Only sweep all groups (regardless of fail state) by default
  3252. * Set enabled read test by default.
  3253. */
  3254. #if DISABLE_GUARANTEED_READ
  3255. gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
  3256. #endif
  3257. /* Initialize the register file */
  3258. initialize_reg_file();
  3259. /* Initialize any PHY CSR */
  3260. initialize_hps_phy();
  3261. scc_mgr_initialize();
  3262. initialize_tracking();
  3263. printf("%s: Preparing to start memory calibration\n", __FILE__);
  3264. debug("%s:%d\n", __func__, __LINE__);
  3265. debug_cond(DLEVEL == 1,
  3266. "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
  3267. RW_MGR_MEM_NUMBER_OF_RANKS, RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM,
  3268. RW_MGR_MEM_DQ_PER_READ_DQS, RW_MGR_MEM_DQ_PER_WRITE_DQS,
  3269. RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS,
  3270. RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
  3271. debug_cond(DLEVEL == 1,
  3272. "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
  3273. RW_MGR_MEM_IF_READ_DQS_WIDTH, RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
  3274. RW_MGR_MEM_DATA_WIDTH, RW_MGR_MEM_DATA_MASK_WIDTH,
  3275. IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP);
  3276. debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
  3277. IO_DELAY_PER_DQS_EN_DCHAIN_TAP, IO_DLL_CHAIN_LENGTH);
  3278. debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
  3279. IO_DQS_EN_PHASE_MAX, IO_DQDQS_OUT_PHASE_MAX,
  3280. IO_DQS_EN_DELAY_MAX, IO_DQS_IN_DELAY_MAX);
  3281. debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
  3282. IO_IO_IN_DELAY_MAX, IO_IO_OUT1_DELAY_MAX,
  3283. IO_IO_OUT2_DELAY_MAX);
  3284. debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
  3285. IO_DQS_IN_RESERVE, IO_DQS_OUT_RESERVE);
  3286. hc_initialize_rom_data();
  3287. /* update info for sims */
  3288. reg_file_set_stage(CAL_STAGE_NIL);
  3289. reg_file_set_group(0);
  3290. /*
  3291. * Load global needed for those actions that require
  3292. * some dynamic calibration support.
  3293. */
  3294. dyn_calib_steps = STATIC_CALIB_STEPS;
  3295. /*
  3296. * Load global to allow dynamic selection of delay loop settings
  3297. * based on calibration mode.
  3298. */
  3299. if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
  3300. skip_delay_mask = 0xff;
  3301. else
  3302. skip_delay_mask = 0x0;
  3303. pass = run_mem_calibrate();
  3304. debug_mem_calibrate(pass);
  3305. return pass;
  3306. }