sequencer.c 105 KB

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