sequencer.c 105 KB

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