sequencer.c 105 KB

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