sequencer.c 119 KB

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