sequencer.c 105 KB

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