sequencer.c 109 KB

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