sequencer.c 106 KB

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