sequencer.c 105 KB

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