sequencer.c 105 KB

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