regs.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /*
  2. * Copyright 2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <common/debug.h>
  13. #include <ddr.h>
  14. #include <lib/utils.h>
  15. static inline unsigned int cal_cwl(const unsigned long clk)
  16. {
  17. const unsigned int mclk_ps = get_memory_clk_ps(clk);
  18. return mclk_ps >= 1250U ? 9U :
  19. (mclk_ps >= 1070U ? 10U :
  20. (mclk_ps >= 935U ? 11U :
  21. (mclk_ps >= 833U ? 12U :
  22. (mclk_ps >= 750U ? 14U :
  23. (mclk_ps >= 625U ? 16U : 18U)))));
  24. }
  25. static void cal_csn_config(int i,
  26. struct ddr_cfg_regs *regs,
  27. const struct memctl_opt *popts,
  28. const struct dimm_params *pdimm)
  29. {
  30. unsigned int intlv_en = 0U;
  31. unsigned int intlv_ctl = 0U;
  32. const unsigned int cs_n_en = 1U;
  33. const unsigned int ap_n_en = popts->cs_odt[i].auto_precharge;
  34. const unsigned int odt_rd_cfg = popts->cs_odt[i].odt_rd_cfg;
  35. const unsigned int odt_wr_cfg = popts->cs_odt[i].odt_wr_cfg;
  36. const unsigned int ba_bits_cs_n = pdimm->bank_addr_bits;
  37. const unsigned int row_bits_cs_n = pdimm->n_row_addr - 12U;
  38. const unsigned int col_bits_cs_n = pdimm->n_col_addr - 8U;
  39. const unsigned int bg_bits_cs_n = pdimm->bank_group_bits;
  40. if (i == 0) {
  41. /* These fields only available in CS0_CONFIG */
  42. if (popts->ctlr_intlv != 0) {
  43. switch (popts->ctlr_intlv_mode) {
  44. case DDR_256B_INTLV:
  45. intlv_en = popts->ctlr_intlv;
  46. intlv_ctl = popts->ctlr_intlv_mode;
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. }
  53. regs->cs[i].config = ((cs_n_en & 0x1) << 31) |
  54. ((intlv_en & 0x3) << 29) |
  55. ((intlv_ctl & 0xf) << 24) |
  56. ((ap_n_en & 0x1) << 23) |
  57. ((odt_rd_cfg & 0x7) << 20) |
  58. ((odt_wr_cfg & 0x7) << 16) |
  59. ((ba_bits_cs_n & 0x3) << 14) |
  60. ((row_bits_cs_n & 0x7) << 8) |
  61. ((bg_bits_cs_n & 0x3) << 4) |
  62. ((col_bits_cs_n & 0x7) << 0);
  63. debug("cs%d\n", i);
  64. debug(" _config = 0x%x\n", regs->cs[i].config);
  65. }
  66. static inline int avoid_odt_overlap(const struct ddr_conf *conf,
  67. const struct dimm_params *pdimm)
  68. {
  69. if ((conf->cs_in_use == 0xf) != 0) {
  70. return 2;
  71. }
  72. #if DDRC_NUM_DIMM >= 2
  73. if (conf->dimm_in_use[0] != 0 && conf->dimm_in_use[1] != 0) {
  74. return 1;
  75. }
  76. #endif
  77. return 0;
  78. }
  79. /* Requires rcw2 set first */
  80. static void cal_timing_cfg(const unsigned long clk,
  81. struct ddr_cfg_regs *regs,
  82. const struct memctl_opt *popts,
  83. const struct dimm_params *pdimm,
  84. const struct ddr_conf *conf,
  85. unsigned int cas_latency,
  86. unsigned int additive_latency)
  87. {
  88. const unsigned int mclk_ps = get_memory_clk_ps(clk);
  89. /* tXP=max(4nCK, 6ns) */
  90. const int txp = max((int)mclk_ps * 4, 6000);
  91. /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
  92. static const int wrrec_table[] = {
  93. 10, 10, 10, 10, 10,
  94. 10, 10, 10, 10, 10,
  95. 12, 12, 14, 14, 16,
  96. 16, 18, 18, 20, 20,
  97. 24, 24, 24, 24,
  98. };
  99. int trwt_mclk = (clk / 1000000 > 1900) ? 3 : 2;
  100. int twrt_mclk;
  101. int trrt_mclk;
  102. int twwt_mclk;
  103. const int act_pd_exit_mclk = picos_to_mclk(clk, txp);
  104. const int pre_pd_exit_mclk = act_pd_exit_mclk;
  105. const int taxpd_mclk = 0;
  106. /*
  107. * MRS_CYC = max(tMRD, tMOD)
  108. * tMRD = 8nCK, tMOD = max(24nCK, 15ns)
  109. */
  110. const int tmrd_mclk = max(24U, picos_to_mclk(clk, 15000));
  111. const int pretoact_mclk = picos_to_mclk(clk, pdimm->trp_ps);
  112. const int acttopre_mclk = picos_to_mclk(clk, pdimm->tras_ps);
  113. const int acttorw_mclk = picos_to_mclk(clk, pdimm->trcd_ps);
  114. const int caslat_ctrl = (cas_latency - 1) << 1;
  115. const int trfc1_min = pdimm->die_density >= 0x3 ? 16000 :
  116. (pdimm->die_density == 0x4 ? 26000 :
  117. (pdimm->die_density == 0x5 ? 35000 :
  118. 55000));
  119. const int refrec_ctrl = picos_to_mclk(clk,
  120. pdimm->trfc1_ps) - 8;
  121. int wrrec_mclk = picos_to_mclk(clk, pdimm->twr_ps);
  122. const int acttoact_mclk = max(picos_to_mclk(clk,
  123. pdimm->trrds_ps),
  124. 4U);
  125. int wrtord_mclk = max(2U, picos_to_mclk(clk, 2500));
  126. const unsigned int cpo = 0U;
  127. const int wr_lat = cal_cwl(clk);
  128. int rd_to_pre = picos_to_mclk(clk, 7500);
  129. const int wr_data_delay = popts->wr_data_delay;
  130. const int cke_pls = max(3U, picos_to_mclk(clk, 5000));
  131. #ifdef ERRATA_DDR_A050450
  132. const unsigned short four_act = ((popts->twot_en == 0) &&
  133. (popts->threet_en == 0) &&
  134. (popts->tfaw_ps % 2 == 0)) ?
  135. (picos_to_mclk(clk, popts->tfaw_ps) + 1) :
  136. picos_to_mclk(clk, popts->tfaw_ps);
  137. #else
  138. const unsigned short four_act = picos_to_mclk(clk,
  139. popts->tfaw_ps);
  140. #endif
  141. const unsigned int cntl_adj = 0U;
  142. const unsigned int ext_pretoact = picos_to_mclk(clk,
  143. pdimm->trp_ps) >> 4U;
  144. const unsigned int ext_acttopre = picos_to_mclk(clk,
  145. pdimm->tras_ps) >> 4U;
  146. const unsigned int ext_acttorw = picos_to_mclk(clk,
  147. pdimm->trcd_ps) >> 4U;
  148. const unsigned int ext_caslat = (2U * cas_latency - 1U) >> 4U;
  149. const unsigned int ext_add_lat = additive_latency >> 4U;
  150. const unsigned int ext_refrec = (picos_to_mclk(clk,
  151. pdimm->trfc1_ps) - 8U) >> 4U;
  152. const unsigned int ext_wrrec = (picos_to_mclk(clk, pdimm->twr_ps) +
  153. (popts->otf_burst_chop_en ? 2U : 0U)) >> 4U;
  154. const unsigned int rwt_same_cs = 0U;
  155. const unsigned int wrt_same_cs = 0U;
  156. const unsigned int rrt_same_cs = popts->burst_length == DDR_BL8 ? 0U : 2U;
  157. const unsigned int wwt_same_cs = popts->burst_length == DDR_BL8 ? 0U : 2U;
  158. const unsigned int dll_lock = 2U;
  159. unsigned int rodt_on = 0U;
  160. const unsigned int rodt_off = 4U;
  161. const unsigned int wodt_on = 1U;
  162. const unsigned int wodt_off = 4U;
  163. const unsigned int hs_caslat = 0U;
  164. const unsigned int hs_wrlat = 0U;
  165. const unsigned int hs_wrrec = 0U;
  166. const unsigned int hs_clkadj = 0U;
  167. const unsigned int hs_wrlvl_start = 0U;
  168. const unsigned int txpr = max(5U,
  169. picos_to_mclk(clk,
  170. pdimm->trfc1_ps + 10000U));
  171. const unsigned int tcksre = max(5U, picos_to_mclk(clk, 10000U));
  172. const unsigned int tcksrx = max(5U, picos_to_mclk(clk, 10000U));
  173. const unsigned int cs_to_cmd = 0U;
  174. const unsigned int cke_rst = txpr <= 200U ? 0U :
  175. (txpr <= 256U ? 1U :
  176. (txpr <= 512U ? 2U : 3U));
  177. const unsigned int cksre = tcksre <= 19U ? tcksre - 5U : 15U;
  178. const unsigned int cksrx = tcksrx <= 19U ? tcksrx - 5U : 15U;
  179. unsigned int par_lat = 0U;
  180. const int tccdl = max(5U, picos_to_mclk(clk, pdimm->tccdl_ps));
  181. int rwt_bg = cas_latency + 2 + 4 - wr_lat;
  182. int wrt_bg = wr_lat + 4 + 1 - cas_latency;
  183. const int rrt_bg = popts->burst_length == DDR_BL8 ?
  184. tccdl - 4 : tccdl - 2;
  185. const int wwt_bg = popts->burst_length == DDR_BL8 ?
  186. tccdl - 4 : tccdl - 2;
  187. const unsigned int acttoact_bg = picos_to_mclk(clk, pdimm->trrdl_ps);
  188. const unsigned int wrtord_bg = max(4U, picos_to_mclk(clk, 7500)) +
  189. (popts->otf_burst_chop_en ? 2 : 0);
  190. const unsigned int pre_all_rec = 0;
  191. const unsigned int refrec_cid_mclk = pdimm->package_3ds ?
  192. picos_to_mclk(clk, pdimm->trfc_slr_ps) : 0;
  193. const unsigned int acttoact_cid_mclk = pdimm->package_3ds ? 4U : 0;
  194. /* for two dual-rank DIMMs to avoid ODT overlap */
  195. if (avoid_odt_overlap(conf, pdimm) == 2) {
  196. twrt_mclk = 2;
  197. twwt_mclk = 2;
  198. trrt_mclk = 2;
  199. } else {
  200. twrt_mclk = 1;
  201. twwt_mclk = 1;
  202. trrt_mclk = 0;
  203. }
  204. if (popts->trwt_override != 0) {
  205. trwt_mclk = popts->trwt;
  206. if (popts->twrt != 0) {
  207. twrt_mclk = popts->twrt;
  208. }
  209. if (popts->trrt != 0) {
  210. trrt_mclk = popts->trrt;
  211. }
  212. if (popts->twwt != 0) {
  213. twwt_mclk = popts->twwt;
  214. }
  215. }
  216. regs->timing_cfg[0] = (((trwt_mclk & 0x3) << 30) |
  217. ((twrt_mclk & 0x3) << 28) |
  218. ((trrt_mclk & 0x3) << 26) |
  219. ((twwt_mclk & 0x3) << 24) |
  220. ((act_pd_exit_mclk & 0xf) << 20) |
  221. ((pre_pd_exit_mclk & 0xF) << 16) |
  222. ((taxpd_mclk & 0xf) << 8) |
  223. ((tmrd_mclk & 0x1f) << 0));
  224. debug("timing_cfg[0] = 0x%x\n", regs->timing_cfg[0]);
  225. if ((wrrec_mclk < 1) || (wrrec_mclk > 24)) {
  226. ERROR("WRREC doesn't support clock %d\n", wrrec_mclk);
  227. } else {
  228. wrrec_mclk = wrrec_table[wrrec_mclk - 1];
  229. }
  230. if (popts->otf_burst_chop_en != 0) {
  231. wrrec_mclk += 2;
  232. wrtord_mclk += 2;
  233. }
  234. if (pdimm->trfc1_ps < trfc1_min) {
  235. ERROR("trfc1_ps (%d) < %d\n", pdimm->trfc1_ps, trfc1_min);
  236. }
  237. regs->timing_cfg[1] = (((pretoact_mclk & 0x0F) << 28) |
  238. ((acttopre_mclk & 0x0F) << 24) |
  239. ((acttorw_mclk & 0xF) << 20) |
  240. ((caslat_ctrl & 0xF) << 16) |
  241. ((refrec_ctrl & 0xF) << 12) |
  242. ((wrrec_mclk & 0x0F) << 8) |
  243. ((acttoact_mclk & 0x0F) << 4) |
  244. ((wrtord_mclk & 0x0F) << 0));
  245. debug("timing_cfg[1] = 0x%x\n", regs->timing_cfg[1]);
  246. if (rd_to_pre < 4) {
  247. rd_to_pre = 4;
  248. }
  249. if (popts->otf_burst_chop_en) {
  250. rd_to_pre += 2;
  251. }
  252. regs->timing_cfg[2] = (((additive_latency & 0xf) << 28) |
  253. ((cpo & 0x1f) << 23) |
  254. ((wr_lat & 0xf) << 19) |
  255. (((wr_lat & 0x10) >> 4) << 18) |
  256. ((rd_to_pre & 0xf) << 13) |
  257. ((wr_data_delay & 0xf) << 9) |
  258. ((cke_pls & 0x7) << 6) |
  259. ((four_act & 0x3f) << 0));
  260. debug("timing_cfg[2] = 0x%x\n", regs->timing_cfg[2]);
  261. regs->timing_cfg[3] = (((ext_pretoact & 0x1) << 28) |
  262. ((ext_acttopre & 0x3) << 24) |
  263. ((ext_acttorw & 0x1) << 22) |
  264. ((ext_refrec & 0x3F) << 16) |
  265. ((ext_caslat & 0x3) << 12) |
  266. ((ext_add_lat & 0x1) << 10) |
  267. ((ext_wrrec & 0x1) << 8) |
  268. ((cntl_adj & 0x7) << 0));
  269. debug("timing_cfg[3] = 0x%x\n", regs->timing_cfg[3]);
  270. regs->timing_cfg[4] = (((rwt_same_cs & 0xf) << 28) |
  271. ((wrt_same_cs & 0xf) << 24) |
  272. ((rrt_same_cs & 0xf) << 20) |
  273. ((wwt_same_cs & 0xf) << 16) |
  274. ((trwt_mclk & 0xc) << 12) |
  275. ((twrt_mclk & 0x4) << 10) |
  276. ((trrt_mclk & 0x4) << 8) |
  277. ((twwt_mclk & 0x4) << 6) |
  278. (dll_lock & 0x3));
  279. debug("timing_cfg[4] = 0x%x\n", regs->timing_cfg[4]);
  280. /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
  281. if (cas_latency >= wr_lat) {
  282. rodt_on = cas_latency - wr_lat + 1;
  283. }
  284. regs->timing_cfg[5] = (((rodt_on & 0x1f) << 24) |
  285. ((rodt_off & 0x7) << 20) |
  286. ((wodt_on & 0x1f) << 12) |
  287. (wodt_off & 0x7) << 8);
  288. debug("timing_cfg[5] = 0x%x\n", regs->timing_cfg[5]);
  289. regs->timing_cfg[6] = (((hs_caslat & 0x1f) << 24) |
  290. ((hs_wrlat & 0x1f) << 19) |
  291. ((hs_wrrec & 0x1f) << 12) |
  292. ((hs_clkadj & 0x1f) << 6) |
  293. ((hs_wrlvl_start & 0x1f) << 0));
  294. debug("timing_cfg[6] = 0x%x\n", regs->timing_cfg[6]);
  295. if (popts->ap_en != 0) {
  296. par_lat = (regs->sdram_rcw[1] & 0xf) + 1;
  297. debug("PAR_LAT = 0x%x\n", par_lat);
  298. }
  299. regs->timing_cfg[7] = (((cke_rst & 0x3) << 28) |
  300. ((cksre & 0xf) << 24) |
  301. ((cksrx & 0xf) << 20) |
  302. ((par_lat & 0xf) << 16) |
  303. ((cs_to_cmd & 0xf) << 4));
  304. debug("timing_cfg[7] = 0x%x\n", regs->timing_cfg[7]);
  305. if (rwt_bg < tccdl) {
  306. rwt_bg = tccdl - rwt_bg;
  307. } else {
  308. rwt_bg = 0;
  309. }
  310. if (wrt_bg < tccdl) {
  311. wrt_bg = tccdl - wrt_bg;
  312. } else {
  313. wrt_bg = 0;
  314. }
  315. regs->timing_cfg[8] = (((rwt_bg & 0xf) << 28) |
  316. ((wrt_bg & 0xf) << 24) |
  317. ((rrt_bg & 0xf) << 20) |
  318. ((wwt_bg & 0xf) << 16) |
  319. ((acttoact_bg & 0xf) << 12) |
  320. ((wrtord_bg & 0xf) << 8) |
  321. ((pre_all_rec & 0x1f) << 0));
  322. debug("timing_cfg[8] = 0x%x\n", regs->timing_cfg[8]);
  323. regs->timing_cfg[9] = (refrec_cid_mclk & 0x3ff) << 16 |
  324. (acttoact_cid_mclk & 0xf) << 8;
  325. debug("timing_cfg[9] = 0x%x\n", regs->timing_cfg[9]);
  326. }
  327. static void cal_ddr_sdram_rcw(const unsigned long clk,
  328. struct ddr_cfg_regs *regs,
  329. const struct memctl_opt *popts,
  330. const struct dimm_params *pdimm)
  331. {
  332. const unsigned int freq = clk / 1000000U;
  333. unsigned int rc0a, rc0f;
  334. if (pdimm->rdimm == 0) {
  335. return;
  336. }
  337. rc0a = freq > 3200U ? 7U :
  338. (freq > 2933U ? 6U :
  339. (freq > 2666U ? 5U :
  340. (freq > 2400U ? 4U :
  341. (freq > 2133U ? 3U :
  342. (freq > 1866U ? 2U :
  343. (freq > 1600U ? 1U : 0U))))));
  344. rc0f = freq > 3200U ? 3U :
  345. (freq > 2400U ? 2U :
  346. (freq > 2133U ? 1U : 0U));
  347. rc0f = (regs->sdram_cfg[1] & SDRAM_CFG2_AP_EN) ? rc0f : 4;
  348. regs->sdram_rcw[0] =
  349. pdimm->rcw[0] << 28 |
  350. pdimm->rcw[1] << 24 |
  351. pdimm->rcw[2] << 20 |
  352. pdimm->rcw[3] << 16 |
  353. pdimm->rcw[4] << 12 |
  354. pdimm->rcw[5] << 8 |
  355. pdimm->rcw[6] << 4 |
  356. pdimm->rcw[7];
  357. regs->sdram_rcw[1] =
  358. pdimm->rcw[8] << 28 |
  359. pdimm->rcw[9] << 24 |
  360. rc0a << 20 |
  361. pdimm->rcw[11] << 16 |
  362. pdimm->rcw[12] << 12 |
  363. pdimm->rcw[13] << 8 |
  364. pdimm->rcw[14] << 4 |
  365. rc0f;
  366. regs->sdram_rcw[2] =
  367. ((freq - 1260 + 19) / 20) << 8;
  368. debug("sdram_rcw[0] = 0x%x\n", regs->sdram_rcw[0]);
  369. debug("sdram_rcw[1] = 0x%x\n", regs->sdram_rcw[1]);
  370. debug("sdram_rcw[2] = 0x%x\n", regs->sdram_rcw[2]);
  371. }
  372. static void cal_ddr_sdram_cfg(const unsigned long clk,
  373. struct ddr_cfg_regs *regs,
  374. const struct memctl_opt *popts,
  375. const struct dimm_params *pdimm,
  376. const unsigned int ip_rev)
  377. {
  378. const unsigned int mem_en = 1U;
  379. const unsigned int sren = popts->self_refresh_in_sleep;
  380. const unsigned int ecc_en = popts->ecc_mode;
  381. const unsigned int rd_en = (pdimm->rdimm != 0U) ? 1U : 0U;
  382. const unsigned int dyn_pwr = popts->dynamic_power;
  383. const unsigned int dbw = popts->data_bus_used;
  384. const unsigned int eight_be = (dbw == 1U ||
  385. popts->burst_length == DDR_BL8) ? 1U : 0U;
  386. const unsigned int ncap = 0U;
  387. const unsigned int threet_en = popts->threet_en;
  388. const unsigned int twot_en = pdimm->rdimm ?
  389. 0U : popts->twot_en;
  390. const unsigned int ba_intlv = popts->ba_intlv;
  391. const unsigned int x32_en = 0U;
  392. const unsigned int pchb8 = 0U;
  393. const unsigned int hse = popts->half_strength_drive_en;
  394. const unsigned int acc_ecc_en = (dbw != 0U && ecc_en == 1U) ? 1U : 0U;
  395. const unsigned int mem_halt = 0U;
  396. #ifdef PHY_GEN2
  397. const unsigned int bi = 1U;
  398. #else
  399. const unsigned int bi = 0U;
  400. #endif
  401. const unsigned int sdram_type = SDRAM_TYPE_DDR4;
  402. unsigned int odt_cfg = 0U;
  403. const unsigned int frc_sr = 0U;
  404. const unsigned int sr_ie = popts->self_refresh_irq_en;
  405. const unsigned int num_pr = pdimm->package_3ds + 1U;
  406. const unsigned int slow = (clk < 1249000000U) ? 1U : 0U;
  407. const unsigned int x4_en = popts->x4_en;
  408. const unsigned int obc_cfg = popts->otf_burst_chop_en;
  409. const unsigned int ap_en = ip_rev == 0x50500U ? 0U : popts->ap_en;
  410. const unsigned int d_init = popts->ctlr_init_ecc;
  411. const unsigned int rcw_en = popts->rdimm;
  412. const unsigned int md_en = popts->mirrored_dimm;
  413. const unsigned int qd_en = popts->quad_rank_present;
  414. const unsigned int unq_mrs_en = ip_rev < 0x50500U ? 1U : 0U;
  415. const unsigned int rd_pre = popts->quad_rank_present;
  416. int i;
  417. regs->sdram_cfg[0] = ((mem_en & 0x1) << 31) |
  418. ((sren & 0x1) << 30) |
  419. ((ecc_en & 0x1) << 29) |
  420. ((rd_en & 0x1) << 28) |
  421. ((sdram_type & 0x7) << 24) |
  422. ((dyn_pwr & 0x1) << 21) |
  423. ((dbw & 0x3) << 19) |
  424. ((eight_be & 0x1) << 18) |
  425. ((ncap & 0x1) << 17) |
  426. ((threet_en & 0x1) << 16) |
  427. ((twot_en & 0x1) << 15) |
  428. ((ba_intlv & 0x7F) << 8) |
  429. ((x32_en & 0x1) << 5) |
  430. ((pchb8 & 0x1) << 4) |
  431. ((hse & 0x1) << 3) |
  432. ((acc_ecc_en & 0x1) << 2) |
  433. ((mem_halt & 0x1) << 1) |
  434. ((bi & 0x1) << 0);
  435. debug("sdram_cfg[0] = 0x%x\n", regs->sdram_cfg[0]);
  436. for (i = 0; i < DDRC_NUM_CS; i++) {
  437. if (popts->cs_odt[i].odt_rd_cfg != 0 ||
  438. popts->cs_odt[i].odt_wr_cfg != 0) {
  439. odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
  440. break;
  441. }
  442. }
  443. regs->sdram_cfg[1] = (0
  444. | ((frc_sr & 0x1) << 31)
  445. | ((sr_ie & 0x1) << 30)
  446. | ((odt_cfg & 0x3) << 21)
  447. | ((num_pr & 0xf) << 12)
  448. | ((slow & 1) << 11)
  449. | (x4_en << 10)
  450. | (qd_en << 9)
  451. | (unq_mrs_en << 8)
  452. | ((obc_cfg & 0x1) << 6)
  453. | ((ap_en & 0x1) << 5)
  454. | ((d_init & 0x1) << 4)
  455. | ((rcw_en & 0x1) << 2)
  456. | ((md_en & 0x1) << 0)
  457. );
  458. debug("sdram_cfg[1] = 0x%x\n", regs->sdram_cfg[1]);
  459. regs->sdram_cfg[2] = (rd_pre & 0x1) << 16 |
  460. (popts->rdimm ? 1 : 0);
  461. if (pdimm->package_3ds != 0) {
  462. if (((pdimm->package_3ds + 1) & 0x1) != 0) {
  463. WARN("Unsupported 3DS DIMM\n");
  464. } else {
  465. regs->sdram_cfg[2] |= ((pdimm->package_3ds + 1) >> 1)
  466. << 4;
  467. }
  468. }
  469. debug("sdram_cfg[2] = 0x%x\n", regs->sdram_cfg[2]);
  470. }
  471. static void cal_ddr_sdram_interval(const unsigned long clk,
  472. struct ddr_cfg_regs *regs,
  473. const struct memctl_opt *popts,
  474. const struct dimm_params *pdimm)
  475. {
  476. const unsigned int refint = picos_to_mclk(clk, pdimm->refresh_rate_ps);
  477. const unsigned int bstopre = popts->bstopre;
  478. regs->interval = ((refint & 0xFFFF) << 16) |
  479. ((bstopre & 0x3FFF) << 0);
  480. debug("interval = 0x%x\n", regs->interval);
  481. }
  482. /* Require cs and cfg first */
  483. static void cal_ddr_sdram_mode(const unsigned long clk,
  484. struct ddr_cfg_regs *regs,
  485. const struct memctl_opt *popts,
  486. const struct ddr_conf *conf,
  487. const struct dimm_params *pdimm,
  488. unsigned int cas_latency,
  489. unsigned int additive_latency,
  490. const unsigned int ip_rev)
  491. {
  492. int i;
  493. unsigned short esdmode; /* Extended SDRAM mode */
  494. unsigned short sdmode; /* SDRAM mode */
  495. /* Mode Register - MR1 */
  496. const unsigned int qoff = 0;
  497. const unsigned int tdqs_en = 0;
  498. unsigned int rtt;
  499. const unsigned int wrlvl_en = 0;
  500. unsigned int al = 0;
  501. unsigned int dic = 0;
  502. const unsigned int dll_en = 1;
  503. /* Mode Register - MR0 */
  504. unsigned int wr = 0;
  505. const unsigned int dll_rst = 0;
  506. const unsigned int mode = 0;
  507. unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
  508. /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
  509. const unsigned int bt = 0;
  510. const unsigned int bl = popts->burst_length == DDR_BL8 ? 0 :
  511. (popts->burst_length == DDR_BC4 ? 2 : 1);
  512. const unsigned int wr_mclk = picos_to_mclk(clk, pdimm->twr_ps);
  513. /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */
  514. static const int wr_table[] = {
  515. 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6
  516. };
  517. /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */
  518. static const int cas_latency_table[] = {
  519. 0, 1, 2, 3, 4, 5, 6, 7, 13, 8,
  520. 14, 9, 15, 10, 12, 11, 16, 17,
  521. 18, 19, 20, 21, 22, 23
  522. };
  523. const unsigned int unq_mrs_en = ip_rev < U(0x50500) ? 1U : 0U;
  524. unsigned short esdmode2 = 0U;
  525. unsigned short esdmode3 = 0U;
  526. const unsigned int wr_crc = 0U;
  527. unsigned int rtt_wr = 0U;
  528. const unsigned int srt = 0U;
  529. unsigned int cwl = cal_cwl(clk);
  530. const unsigned int mpr = 0U;
  531. const unsigned int mclk_ps = get_memory_clk_ps(clk);
  532. const unsigned int wc_lat = 0U;
  533. unsigned short esdmode4 = 0U;
  534. unsigned short esdmode5;
  535. int rtt_park_all = 0;
  536. unsigned int rtt_park;
  537. const bool four_cs = conf->cs_in_use == 0xf ? true : false;
  538. unsigned short esdmode6 = 0U; /* Extended SDRAM mode 6 */
  539. unsigned short esdmode7 = 0U; /* Extended SDRAM mode 7 */
  540. const unsigned int tccdl_min = max(5U,
  541. picos_to_mclk(clk, pdimm->tccdl_ps));
  542. if (popts->rtt_override != 0U) {
  543. rtt = popts->rtt_override_value;
  544. } else {
  545. rtt = popts->cs_odt[0].odt_rtt_norm;
  546. }
  547. if (additive_latency == (cas_latency - 1)) {
  548. al = 1;
  549. }
  550. if (additive_latency == (cas_latency - 2)) {
  551. al = 2;
  552. }
  553. if (popts->quad_rank_present != 0 || popts->output_driver_impedance != 0) {
  554. dic = 1; /* output driver impedance 240/7 ohm */
  555. }
  556. esdmode = (((qoff & 0x1) << 12) |
  557. ((tdqs_en & 0x1) << 11) |
  558. ((rtt & 0x7) << 8) |
  559. ((wrlvl_en & 0x1) << 7) |
  560. ((al & 0x3) << 3) |
  561. ((dic & 0x3) << 1) |
  562. ((dll_en & 0x1) << 0));
  563. if (wr_mclk >= 10 && wr_mclk <= 24) {
  564. wr = wr_table[wr_mclk - 10];
  565. } else {
  566. ERROR("unsupported wc_mclk = %d for mode register\n", wr_mclk);
  567. }
  568. /* look up table to get the cas latency bits */
  569. if (cas_latency >= 9 && cas_latency <= 32) {
  570. caslat = cas_latency_table[cas_latency - 9];
  571. } else {
  572. WARN("Error: unsupported cas latency for mode register\n");
  573. }
  574. sdmode = (((caslat & 0x10) << 8) |
  575. ((wr & 0x7) << 9) |
  576. ((dll_rst & 0x1) << 8) |
  577. ((mode & 0x1) << 7) |
  578. (((caslat >> 1) & 0x7) << 4) |
  579. ((bt & 0x1) << 3) |
  580. ((caslat & 1) << 2) |
  581. ((bl & 0x3) << 0));
  582. regs->sdram_mode[0] = (((esdmode & 0xFFFF) << 16) |
  583. ((sdmode & 0xFFFF) << 0));
  584. debug("sdram_mode[0] = 0x%x\n", regs->sdram_mode[0]);
  585. switch (cwl) {
  586. case 9:
  587. case 10:
  588. case 11:
  589. case 12:
  590. cwl -= 9;
  591. break;
  592. case 14:
  593. cwl -= 10;
  594. break;
  595. case 16:
  596. cwl -= 11;
  597. break;
  598. case 18:
  599. cwl -= 12;
  600. break;
  601. case 20:
  602. cwl -= 13;
  603. break;
  604. default:
  605. printf("Error CWL\n");
  606. break;
  607. }
  608. if (popts->rtt_override != 0) {
  609. rtt_wr = popts->rtt_wr_override_value;
  610. } else {
  611. rtt_wr = popts->cs_odt[0].odt_rtt_wr;
  612. }
  613. esdmode2 = ((wr_crc & 0x1) << 12) |
  614. ((rtt_wr & 0x7) << 9) |
  615. ((srt & 0x3) << 6) |
  616. ((cwl & 0x7) << 3);
  617. esdmode3 = ((mpr & 0x3) << 11) | ((wc_lat & 0x3) << 9);
  618. regs->sdram_mode[1] = ((esdmode2 & 0xFFFF) << 16) |
  619. ((esdmode3 & 0xFFFF) << 0);
  620. debug("sdram_mode[1] = 0x%x\n", regs->sdram_mode[1]);
  621. esdmode6 = ((tccdl_min - 4) & 0x7) << 10;
  622. if (popts->vref_dimm != 0) {
  623. esdmode6 |= popts->vref_dimm & 0x7f;
  624. } else if ((popts->ddr_cdr2 & DDR_CDR2_VREF_RANGE_2) != 0) {
  625. esdmode6 |= 1 << 6; /* Range 2 */
  626. }
  627. regs->sdram_mode[9] = ((esdmode6 & 0xffff) << 16) |
  628. ((esdmode7 & 0xffff) << 0);
  629. debug("sdram_mode[9] = 0x%x\n", regs->sdram_mode[9]);
  630. rtt_park = (popts->rtt_park != 0) ? popts->rtt_park : 240;
  631. switch (rtt_park) {
  632. case 240:
  633. rtt_park = 0x4;
  634. break;
  635. case 120:
  636. rtt_park = 0x2;
  637. break;
  638. case 80:
  639. rtt_park = 0x6;
  640. break;
  641. case 60:
  642. rtt_park = 0x1;
  643. break;
  644. case 48:
  645. rtt_park = 0x5;
  646. break;
  647. case 40:
  648. rtt_park = 0x3;
  649. break;
  650. case 34:
  651. rtt_park = 0x7;
  652. break;
  653. default:
  654. rtt_park = 0;
  655. break;
  656. }
  657. for (i = 0; i < DDRC_NUM_CS; i++) {
  658. if (i != 0 && unq_mrs_en == 0) {
  659. break;
  660. }
  661. if (popts->rtt_override != 0) {
  662. rtt = popts->rtt_override_value;
  663. rtt_wr = popts->rtt_wr_override_value;
  664. } else {
  665. rtt = popts->cs_odt[i].odt_rtt_norm;
  666. rtt_wr = popts->cs_odt[i].odt_rtt_wr;
  667. }
  668. esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */
  669. esdmode |= (rtt & 0x7) << 8;
  670. esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
  671. esdmode2 |= (rtt_wr & 0x3) << 9;
  672. esdmode5 = (popts->x4_en) ? 0 : 0x400; /* data mask */
  673. if (rtt_park_all == 0 &&
  674. ((regs->cs[i].config & SDRAM_CS_CONFIG_EN) != 0)) {
  675. esdmode5 |= rtt_park << 6;
  676. rtt_park_all = four_cs ? 0 : 1;
  677. }
  678. if (((regs->sdram_cfg[1] & SDRAM_CFG2_AP_EN) != 0) &&
  679. (popts->rdimm == 0)) {
  680. if (mclk_ps >= 935) {
  681. esdmode5 |= DDR_MR5_CA_PARITY_LAT_4_CLK;
  682. } else if (mclk_ps >= 833) {
  683. esdmode5 |= DDR_MR5_CA_PARITY_LAT_5_CLK;
  684. } else {
  685. esdmode5 |= DDR_MR5_CA_PARITY_LAT_5_CLK;
  686. WARN("mclk_ps not supported %d", mclk_ps);
  687. }
  688. }
  689. switch (i) {
  690. case 0:
  691. regs->sdram_mode[8] = ((esdmode4 & 0xffff) << 16) |
  692. ((esdmode5 & 0xffff) << 0);
  693. debug("sdram_mode[8] = 0x%x\n", regs->sdram_mode[8]);
  694. break;
  695. case 1:
  696. regs->sdram_mode[2] = (((esdmode & 0xFFFF) << 16) |
  697. ((sdmode & 0xFFFF) << 0));
  698. regs->sdram_mode[3] = ((esdmode2 & 0xFFFF) << 16) |
  699. ((esdmode3 & 0xFFFF) << 0);
  700. regs->sdram_mode[10] = ((esdmode4 & 0xFFFF) << 16) |
  701. ((esdmode5 & 0xFFFF) << 0);
  702. regs->sdram_mode[11] = ((esdmode6 & 0xFFFF) << 16) |
  703. ((esdmode7 & 0xFFFF) << 0);
  704. debug("sdram_mode[2] = 0x%x\n", regs->sdram_mode[2]);
  705. debug("sdram_mode[3] = 0x%x\n", regs->sdram_mode[3]);
  706. debug("sdram_mode[10] = 0x%x\n", regs->sdram_mode[10]);
  707. debug("sdram_mode[11] = 0x%x\n", regs->sdram_mode[11]);
  708. break;
  709. case 2:
  710. regs->sdram_mode[4] = (((esdmode & 0xFFFF) << 16) |
  711. ((sdmode & 0xFFFF) << 0));
  712. regs->sdram_mode[5] = ((esdmode2 & 0xFFFF) << 16) |
  713. ((esdmode3 & 0xFFFF) << 0);
  714. regs->sdram_mode[12] = ((esdmode4 & 0xFFFF) << 16) |
  715. ((esdmode5 & 0xFFFF) << 0);
  716. regs->sdram_mode[13] = ((esdmode6 & 0xFFFF) << 16) |
  717. ((esdmode7 & 0xFFFF) << 0);
  718. debug("sdram_mode[4] = 0x%x\n", regs->sdram_mode[4]);
  719. debug("sdram_mode[5] = 0x%x\n", regs->sdram_mode[5]);
  720. debug("sdram_mode[12] = 0x%x\n", regs->sdram_mode[12]);
  721. debug("sdram_mode[13] = 0x%x\n", regs->sdram_mode[13]);
  722. break;
  723. case 3:
  724. regs->sdram_mode[6] = (((esdmode & 0xFFFF) << 16) |
  725. ((sdmode & 0xFFFF) << 0));
  726. regs->sdram_mode[7] = ((esdmode2 & 0xFFFF) << 16) |
  727. ((esdmode3 & 0xFFFF) << 0);
  728. regs->sdram_mode[14] = ((esdmode4 & 0xFFFF) << 16) |
  729. ((esdmode5 & 0xFFFF) << 0);
  730. regs->sdram_mode[15] = ((esdmode6 & 0xFFFF) << 16) |
  731. ((esdmode7 & 0xFFFF) << 0);
  732. debug("sdram_mode[6] = 0x%x\n", regs->sdram_mode[6]);
  733. debug("sdram_mode[7] = 0x%x\n", regs->sdram_mode[7]);
  734. debug("sdram_mode[14] = 0x%x\n", regs->sdram_mode[14]);
  735. debug("sdram_mode[15] = 0x%x\n", regs->sdram_mode[15]);
  736. break;
  737. default:
  738. break;
  739. }
  740. }
  741. }
  742. #ifndef CONFIG_MEM_INIT_VALUE
  743. #define CONFIG_MEM_INIT_VALUE 0xDEADBEEF
  744. #endif
  745. static void cal_ddr_data_init(struct ddr_cfg_regs *regs)
  746. {
  747. regs->data_init = CONFIG_MEM_INIT_VALUE;
  748. }
  749. static void cal_ddr_dq_mapping(struct ddr_cfg_regs *regs,
  750. const struct dimm_params *pdimm)
  751. {
  752. const unsigned int acc_ecc_en = (regs->sdram_cfg[0] >> 2) & 0x1;
  753. /* FIXME: revert the dq mapping from DIMM */
  754. regs->dq_map[0] = ((pdimm->dq_mapping[0] & 0x3F) << 26) |
  755. ((pdimm->dq_mapping[1] & 0x3F) << 20) |
  756. ((pdimm->dq_mapping[2] & 0x3F) << 14) |
  757. ((pdimm->dq_mapping[3] & 0x3F) << 8) |
  758. ((pdimm->dq_mapping[4] & 0x3F) << 2);
  759. regs->dq_map[1] = ((pdimm->dq_mapping[5] & 0x3F) << 26) |
  760. ((pdimm->dq_mapping[6] & 0x3F) << 20) |
  761. ((pdimm->dq_mapping[7] & 0x3F) << 14) |
  762. ((pdimm->dq_mapping[10] & 0x3F) << 8) |
  763. ((pdimm->dq_mapping[11] & 0x3F) << 2);
  764. regs->dq_map[2] = ((pdimm->dq_mapping[12] & 0x3F) << 26) |
  765. ((pdimm->dq_mapping[13] & 0x3F) << 20) |
  766. ((pdimm->dq_mapping[14] & 0x3F) << 14) |
  767. ((pdimm->dq_mapping[15] & 0x3F) << 8) |
  768. ((pdimm->dq_mapping[16] & 0x3F) << 2);
  769. /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */
  770. regs->dq_map[3] = ((pdimm->dq_mapping[17] & 0x3F) << 26) |
  771. ((pdimm->dq_mapping[8] & 0x3F) << 20) |
  772. ((acc_ecc_en != 0) ? 0 :
  773. (pdimm->dq_mapping[9] & 0x3F) << 14) |
  774. pdimm->dq_mapping_ors;
  775. debug("dq_map[0] = 0x%x\n", regs->dq_map[0]);
  776. debug("dq_map[1] = 0x%x\n", regs->dq_map[1]);
  777. debug("dq_map[2] = 0x%x\n", regs->dq_map[2]);
  778. debug("dq_map[3] = 0x%x\n", regs->dq_map[3]);
  779. }
  780. static void cal_ddr_zq_cntl(struct ddr_cfg_regs *regs)
  781. {
  782. const unsigned int zqinit = 10U; /* 1024 clocks */
  783. const unsigned int zqoper = 9U; /* 512 clocks */
  784. const unsigned int zqcs = 7U; /* 128 clocks */
  785. const unsigned int zqcs_init = 5U; /* 1024 refresh seqences */
  786. const unsigned int zq_en = 1U; /* enabled */
  787. regs->zq_cntl = ((zq_en & 0x1) << 31) |
  788. ((zqinit & 0xF) << 24) |
  789. ((zqoper & 0xF) << 16) |
  790. ((zqcs & 0xF) << 8) |
  791. ((zqcs_init & 0xF) << 0);
  792. debug("zq_cntl = 0x%x\n", regs->zq_cntl);
  793. }
  794. static void cal_ddr_sr_cntr(struct ddr_cfg_regs *regs,
  795. const struct memctl_opt *popts)
  796. {
  797. const unsigned int sr_it = (popts->auto_self_refresh_en) ?
  798. popts->sr_it : 0;
  799. regs->ddr_sr_cntr = (sr_it & 0xF) << 16;
  800. debug("ddr_sr_cntr = 0x%x\n", regs->ddr_sr_cntr);
  801. }
  802. static void cal_ddr_eor(struct ddr_cfg_regs *regs,
  803. const struct memctl_opt *popts)
  804. {
  805. if (popts->addr_hash != 0) {
  806. regs->eor = 0x40000000; /* address hash enable */
  807. debug("eor = 0x%x\n", regs->eor);
  808. }
  809. }
  810. static void cal_ddr_csn_bnds(struct ddr_cfg_regs *regs,
  811. const struct memctl_opt *popts,
  812. const struct ddr_conf *conf,
  813. const struct dimm_params *pdimm)
  814. {
  815. int i;
  816. unsigned long long ea, sa;
  817. /* Chip Select Memory Bounds (CSn_BNDS) */
  818. for (i = 0;
  819. i < DDRC_NUM_CS && conf->cs_size[i];
  820. i++) {
  821. debug("cs_in_use = 0x%x\n", conf->cs_in_use);
  822. if (conf->cs_in_use != 0) {
  823. sa = conf->cs_base_addr[i];
  824. ea = sa + conf->cs_size[i] - 1;
  825. sa >>= 24;
  826. ea >>= 24;
  827. regs->cs[i].bnds = ((sa & 0xffff) << 16) |
  828. ((ea & 0xffff) << 0);
  829. cal_csn_config(i, regs, popts, pdimm);
  830. } else {
  831. /* setting bnds to 0xffffffff for inactive CS */
  832. regs->cs[i].bnds = 0xffffffff;
  833. }
  834. debug("cs[%d].bnds = 0x%x\n", i, regs->cs[i].bnds);
  835. }
  836. }
  837. static void cal_ddr_addr_dec(struct ddr_cfg_regs *regs)
  838. {
  839. #ifdef CONFIG_DDR_ADDR_DEC
  840. unsigned int ba_bits __unused;
  841. char p __unused;
  842. const unsigned int cs0_config = regs->cs[0].config;
  843. const int cacheline = PLATFORM_CACHE_LINE_SHIFT;
  844. unsigned int bg_bits;
  845. unsigned int row_bits;
  846. unsigned int col_bits;
  847. unsigned int cs;
  848. unsigned int map_row[18];
  849. unsigned int map_col[11];
  850. unsigned int map_ba[2];
  851. unsigned int map_cid[2] = {0x3F, 0x3F};
  852. unsigned int map_bg[2] = {0x3F, 0x3F};
  853. unsigned int map_cs[2] = {0x3F, 0x3F};
  854. unsigned int dbw;
  855. unsigned int ba_intlv;
  856. int placement;
  857. int intlv;
  858. int abort = 0;
  859. int i;
  860. int j;
  861. col_bits = (cs0_config >> 0) & 0x7;
  862. if (col_bits < 4) {
  863. col_bits += 8;
  864. } else if (col_bits < 7 || col_bits > 10) {
  865. ERROR("Error %s col_bits = %d\n", __func__, col_bits);
  866. }
  867. row_bits = ((cs0_config >> 8) & 0x7) + 12;
  868. ba_bits = ((cs0_config >> 14) & 0x3) + 2;
  869. bg_bits = ((cs0_config >> 4) & 0x3) + 0;
  870. intlv = (cs0_config >> 24) & 0xf;
  871. ba_intlv = (regs->sdram_cfg[0] >> 8) & 0x7f;
  872. switch (ba_intlv) {
  873. case DDR_BA_INTLV_CS01:
  874. cs = 1;
  875. break;
  876. case DDR_BA_INTLV_CS0123:
  877. cs = 2;
  878. break;
  879. case DDR_BA_NONE:
  880. cs = 0;
  881. break;
  882. default:
  883. ERROR("%s ba_intlv 0x%x\n", __func__, ba_intlv);
  884. return;
  885. }
  886. debug("col %d, row %d, ba %d, bg %d, intlv %d\n",
  887. col_bits, row_bits, ba_bits, bg_bits, intlv);
  888. /*
  889. * Example mapping of 15x2x2x10
  890. * ---- --rr rrrr rrrr rrrr rCBB Gccc cccI cGcc cbbb
  891. */
  892. dbw = (regs->sdram_cfg[0] >> 19) & 0x3;
  893. switch (dbw) {
  894. case 0: /* 64-bit */
  895. placement = 3;
  896. break;
  897. case 1: /* 32-bit */
  898. placement = 2;
  899. break;
  900. default:
  901. ERROR("%s dbw = %d\n", __func__, dbw);
  902. return;
  903. }
  904. debug("cacheline size %d\n", cacheline);
  905. for (i = 0; placement < cacheline; i++) {
  906. map_col[i] = placement++;
  907. }
  908. map_bg[0] = placement++;
  909. for ( ; i < col_bits; i++) {
  910. map_col[i] = placement++;
  911. if (placement == intlv) {
  912. placement++;
  913. }
  914. }
  915. for ( ; i < 11; i++) {
  916. map_col[i] = 0x3F; /* unused col bits */
  917. }
  918. if (bg_bits >= 2) {
  919. map_bg[1] = placement++;
  920. }
  921. map_ba[0] = placement++;
  922. map_ba[1] = placement++;
  923. if (cs != 0U) {
  924. map_cs[0] = placement++;
  925. if (cs == 2U) {
  926. map_cs[1] = placement++;
  927. }
  928. } else {
  929. map_cs[0] = U(0x3F);
  930. }
  931. for (i = 0; i < row_bits; i++) {
  932. map_row[i] = placement++;
  933. }
  934. for ( ; i < 18; i++) {
  935. map_row[i] = 0x3F; /* unused row bits */
  936. }
  937. for (i = 39; i >= 0 ; i--) {
  938. if (i == intlv) {
  939. placement = 8;
  940. p = 'I';
  941. } else if (i < 3) {
  942. p = 'b';
  943. placement = 0;
  944. } else {
  945. placement = 0;
  946. p = '-';
  947. }
  948. for (j = 0; j < 18; j++) {
  949. if (map_row[j] != i) {
  950. continue;
  951. }
  952. if (placement != 0) {
  953. abort = 1;
  954. ERROR("%s wrong address bit %d\n", __func__, i);
  955. }
  956. placement = i;
  957. p = 'r';
  958. }
  959. for (j = 0; j < 11; j++) {
  960. if (map_col[j] != i) {
  961. continue;
  962. }
  963. if (placement != 0) {
  964. abort = 1;
  965. ERROR("%s wrong address bit %d\n", __func__, i);
  966. }
  967. placement = i;
  968. p = 'c';
  969. }
  970. for (j = 0; j < 2; j++) {
  971. if (map_ba[j] != i) {
  972. continue;
  973. }
  974. if (placement != 0) {
  975. abort = 1;
  976. ERROR("%s wrong address bit %d\n", __func__, i);
  977. }
  978. placement = i;
  979. p = 'B';
  980. }
  981. for (j = 0; j < 2; j++) {
  982. if (map_bg[j] != i) {
  983. continue;
  984. }
  985. if (placement != 0) {
  986. abort = 1;
  987. ERROR("%s wrong address bit %d\n", __func__, i);
  988. }
  989. placement = i;
  990. p = 'G';
  991. }
  992. for (j = 0; j < 2; j++) {
  993. if (map_cs[j] != i) {
  994. continue;
  995. }
  996. if (placement != 0) {
  997. abort = 1;
  998. ERROR("%s wrong address bit %d\n", __func__, i);
  999. }
  1000. placement = i;
  1001. p = 'C';
  1002. }
  1003. #ifdef DDR_DEBUG
  1004. printf("%c", p);
  1005. if ((i % 4) == 0) {
  1006. printf(" ");
  1007. }
  1008. #endif
  1009. }
  1010. #ifdef DDR_DEBUG
  1011. puts("\n");
  1012. #endif
  1013. if (abort != 0) {
  1014. return;
  1015. }
  1016. regs->dec[0] = map_row[17] << 26 |
  1017. map_row[16] << 18 |
  1018. map_row[15] << 10 |
  1019. map_row[14] << 2;
  1020. regs->dec[1] = map_row[13] << 26 |
  1021. map_row[12] << 18 |
  1022. map_row[11] << 10 |
  1023. map_row[10] << 2;
  1024. regs->dec[2] = map_row[9] << 26 |
  1025. map_row[8] << 18 |
  1026. map_row[7] << 10 |
  1027. map_row[6] << 2;
  1028. regs->dec[3] = map_row[5] << 26 |
  1029. map_row[4] << 18 |
  1030. map_row[3] << 10 |
  1031. map_row[2] << 2;
  1032. regs->dec[4] = map_row[1] << 26 |
  1033. map_row[0] << 18 |
  1034. map_col[10] << 10 |
  1035. map_col[9] << 2;
  1036. regs->dec[5] = map_col[8] << 26 |
  1037. map_col[7] << 18 |
  1038. map_col[6] << 10 |
  1039. map_col[5] << 2;
  1040. regs->dec[6] = map_col[4] << 26 |
  1041. map_col[3] << 18 |
  1042. map_col[2] << 10 |
  1043. map_col[1] << 2;
  1044. regs->dec[7] = map_col[0] << 26 |
  1045. map_ba[1] << 18 |
  1046. map_ba[0] << 10 |
  1047. map_cid[1] << 2;
  1048. regs->dec[8] = map_cid[1] << 26 |
  1049. map_cs[1] << 18 |
  1050. map_cs[0] << 10 |
  1051. map_bg[1] << 2;
  1052. regs->dec[9] = map_bg[0] << 26 |
  1053. 1;
  1054. for (i = 0; i < 10; i++) {
  1055. debug("dec[%d] = 0x%x\n", i, regs->dec[i]);
  1056. }
  1057. #endif
  1058. }
  1059. static unsigned int skip_caslat(unsigned int tckmin_ps,
  1060. unsigned int taamin_ps,
  1061. unsigned int mclk_ps,
  1062. unsigned int package_3ds)
  1063. {
  1064. int i, j, k;
  1065. struct cas {
  1066. const unsigned int tckmin_ps;
  1067. const unsigned int caslat[4];
  1068. };
  1069. struct speed {
  1070. const struct cas *cl;
  1071. const unsigned int taamin_ps[4];
  1072. };
  1073. const struct cas cl_3200[] = {
  1074. {625, {0xa00000, 0xb00000, 0xf000000,} },
  1075. {750, { 0x20000, 0x60000, 0xe00000,} },
  1076. {833, { 0x8000, 0x18000, 0x38000,} },
  1077. {937, { 0x4000, 0x4000, 0xc000,} },
  1078. {1071, { 0x1000, 0x1000, 0x3000,} },
  1079. {1250, { 0x400, 0x400, 0xc00,} },
  1080. {1500, { 0, 0x600, 0x200,} },
  1081. };
  1082. const struct cas cl_2933[] = {
  1083. {682, { 0, 0x80000, 0x180000, 0x380000} },
  1084. {750, { 0x20000, 0x60000, 0x60000, 0xe0000} },
  1085. {833, { 0x8000, 0x18000, 0x18000, 0x38000} },
  1086. {937, { 0x4000, 0x4000, 0x4000, 0xc000} },
  1087. {1071, { 0x1000, 0x1000, 0x1000, 0x3000} },
  1088. {1250, { 0x400, 0x400, 0x400, 0xc00} },
  1089. {1500, { 0, 0x200, 0x200, 0x200} },
  1090. };
  1091. const struct cas cl_2666[] = {
  1092. {750, { 0, 0x20000, 0x60000, 0xe0000} },
  1093. {833, { 0x8000, 0x18000, 0x18000, 0x38000} },
  1094. {937, { 0x4000, 0x4000, 0x4000, 0xc000} },
  1095. {1071, { 0x1000, 0x1000, 0x1000, 0x3000} },
  1096. {1250, { 0x400, 0x400, 0x400, 0xc00} },
  1097. {1500, { 0, 0, 0x200, 0x200} },
  1098. };
  1099. const struct cas cl_2400[] = {
  1100. {833, { 0, 0x8000, 0x18000, 0x38000} },
  1101. {937, { 0xc000, 0x4000, 0x4000, 0xc000} },
  1102. {1071, { 0x3000, 0x1000, 0x1000, 0x3000} },
  1103. {1250, { 0xc00, 0x400, 0x400, 0xc00} },
  1104. {1500, { 0, 0x400, 0x200, 0x200} },
  1105. };
  1106. const struct cas cl_2133[] = {
  1107. {937, { 0, 0x4000, 0xc000,} },
  1108. {1071, { 0x2000, 0, 0x2000,} },
  1109. {1250, { 0x800, 0, 0x800,} },
  1110. {1500, { 0, 0x400, 0x200,} },
  1111. };
  1112. const struct cas cl_1866[] = {
  1113. {1071, { 0, 0x1000, 0x3000,} },
  1114. {1250, { 0xc00, 0x400, 0xc00,} },
  1115. {1500, { 0, 0x400, 0x200,} },
  1116. };
  1117. const struct cas cl_1600[] = {
  1118. {1250, { 0, 0x400, 0xc00,} },
  1119. {1500, { 0, 0x400, 0x200,} },
  1120. };
  1121. const struct speed bin_0[] = {
  1122. {cl_3200, {12500, 13750, 15000,} },
  1123. {cl_2933, {12960, 13640, 13750, 15000,} },
  1124. {cl_2666, {12750, 13500, 13750, 15000,} },
  1125. {cl_2400, {12500, 13320, 13750, 15000,} },
  1126. {cl_2133, {13130, 13500, 15000,} },
  1127. {cl_1866, {12850, 13500, 15000,} },
  1128. {cl_1600, {12500, 13500, 15000,} }
  1129. };
  1130. const struct cas cl_3200_3ds[] = {
  1131. {625, { 0xa000000, 0xb000000, 0xf000000,} },
  1132. {750, { 0xaa00000, 0xab00000, 0xef00000,} },
  1133. {833, { 0xaac0000, 0xaac0000, 0xebc0000,} },
  1134. {937, { 0xaab0000, 0xaab0000, 0xeaf0000,} },
  1135. {1071, { 0xaaa4000, 0xaaac000, 0xeaec000,} },
  1136. {1250, { 0xaaa0000, 0xaaa2000, 0xeaeb000,} },
  1137. };
  1138. const struct cas cl_2666_3ds[] = {
  1139. {750, { 0xa00000, 0xb00000, 0xf00000,} },
  1140. {833, { 0xac0000, 0xac0000, 0xbc0000,} },
  1141. {937, { 0xab0000, 0xab0000, 0xaf0000,} },
  1142. {1071, { 0xaa4000, 0xaac000, 0xaac000,} },
  1143. {1250, { 0xaa0000, 0xaaa000, 0xaaa000,} },
  1144. };
  1145. const struct cas cl_2400_3ds[] = {
  1146. {833, { 0xe00000, 0xe40000, 0xec0000, 0xb00000} },
  1147. {937, { 0xe00000, 0xe00000, 0xea0000, 0xae0000} },
  1148. {1071, { 0xe00000, 0xe04000, 0xeac000, 0xaec000} },
  1149. {1250, { 0xe00000, 0xe00000, 0xeaa000, 0xae2000} },
  1150. };
  1151. const struct cas cl_2133_3ds[] = {
  1152. {937, { 0x90000, 0xb0000, 0xf0000,} },
  1153. {1071, { 0x84000, 0xac000, 0xec000,} },
  1154. {1250, { 0x80000, 0xa2000, 0xe2000,} },
  1155. };
  1156. const struct cas cl_1866_3ds[] = {
  1157. {1071, { 0, 0x4000, 0xc000,} },
  1158. {1250, { 0, 0x1000, 0x3000,} },
  1159. };
  1160. const struct cas cl_1600_3ds[] = {
  1161. {1250, { 0, 0x1000, 0x3000,} },
  1162. };
  1163. const struct speed bin_3ds[] = {
  1164. {cl_3200_3ds, {15000, 16250, 17140,} },
  1165. {cl_2666_3ds, {15000, 16500, 17140,} },
  1166. {cl_2400_3ds, {15000, 15830, 16670, 17140} },
  1167. {cl_2133_3ds, {15950, 16880, 17140,} },
  1168. {cl_1866_3ds, {15000, 16070, 17140,} },
  1169. {cl_1600_3ds, {15000, 16250, 17500,} },
  1170. };
  1171. const struct speed *bin;
  1172. int size;
  1173. unsigned int taamin_max, tck_max;
  1174. if (taamin_ps > ((package_3ds != 0) ? 21500 : 18000)) {
  1175. ERROR("taamin_ps %u invalid\n", taamin_ps);
  1176. return 0;
  1177. }
  1178. if (package_3ds != 0) {
  1179. bin = bin_3ds;
  1180. size = ARRAY_SIZE(bin_3ds);
  1181. taamin_max = 1250;
  1182. tck_max = 1500;
  1183. } else {
  1184. bin = bin_0;
  1185. size = ARRAY_SIZE(bin_0);
  1186. taamin_max = 1500;
  1187. tck_max = 1600;
  1188. }
  1189. if (mclk_ps < 625 || mclk_ps > tck_max) {
  1190. ERROR("mclk %u invalid\n", mclk_ps);
  1191. return 0;
  1192. }
  1193. for (i = 0; i < size; i++) {
  1194. if (bin[i].cl[0].tckmin_ps >= tckmin_ps) {
  1195. break;
  1196. }
  1197. }
  1198. if (i >= size) {
  1199. ERROR("speed bin not found\n");
  1200. return 0;
  1201. }
  1202. if (bin[i].cl[0].tckmin_ps > tckmin_ps && i > 0) {
  1203. i--;
  1204. }
  1205. for (j = 0; j < 4; j++) {
  1206. if ((bin[i].taamin_ps[j] == 0) ||
  1207. bin[i].taamin_ps[j] >= taamin_ps) {
  1208. break;
  1209. }
  1210. }
  1211. if (j >= 4) {
  1212. ERROR("taamin_ps out of range.\n");
  1213. return 0;
  1214. }
  1215. if (((bin[i].taamin_ps[j] == 0) && j > 0) ||
  1216. (bin[i].taamin_ps[j] > taamin_ps && j > 0)) {
  1217. j--;
  1218. }
  1219. for (k = 0; bin[i].cl[k].tckmin_ps < mclk_ps &&
  1220. bin[i].cl[k].tckmin_ps < taamin_max; k++)
  1221. ;
  1222. if (bin[i].cl[k].tckmin_ps > mclk_ps && k > 0) {
  1223. k--;
  1224. }
  1225. debug("Skip CL mask for this speed 0x%x\n", bin[i].cl[k].caslat[j]);
  1226. return bin[i].cl[k].caslat[j];
  1227. }
  1228. int compute_ddrc(const unsigned long clk,
  1229. const struct memctl_opt *popts,
  1230. const struct ddr_conf *conf,
  1231. struct ddr_cfg_regs *regs,
  1232. const struct dimm_params *pdimm,
  1233. unsigned int ip_rev)
  1234. {
  1235. unsigned int cas_latency;
  1236. unsigned int caslat_skip;
  1237. unsigned int additive_latency;
  1238. const unsigned int mclk_ps = get_memory_clk_ps(clk);
  1239. int i;
  1240. zeromem(regs, sizeof(struct ddr_cfg_regs));
  1241. if (mclk_ps < pdimm->tckmin_x_ps) {
  1242. ERROR("DDR Clk: MCLK cycle is %u ps.\n", mclk_ps);
  1243. ERROR("DDR Clk is faster than DIMM can support.\n");
  1244. }
  1245. /* calculate cas latency, override first */
  1246. cas_latency = (popts->caslat_override != 0) ?
  1247. popts->caslat_override_value :
  1248. (pdimm->taa_ps + mclk_ps - 1) / mclk_ps;
  1249. /* skip unsupported caslat based on speed bin */
  1250. caslat_skip = skip_caslat(pdimm->tckmin_x_ps,
  1251. pdimm->taa_ps,
  1252. mclk_ps,
  1253. pdimm->package_3ds);
  1254. debug("Skip caslat 0x%x\n", caslat_skip);
  1255. /* Check if DIMM supports the cas latency */
  1256. i = 24;
  1257. while (((pdimm->caslat_x & ~caslat_skip & (1 << cas_latency)) == 0) &&
  1258. (i-- > 0)) {
  1259. cas_latency++;
  1260. }
  1261. if (i <= 0) {
  1262. ERROR("Failed to find a proper cas latency\n");
  1263. return -EINVAL;
  1264. }
  1265. /* Verify cas latency does not exceed 18ns for DDR4 */
  1266. if (cas_latency * mclk_ps > 18000) {
  1267. ERROR("cas latency is too large %d\n", cas_latency);
  1268. return -EINVAL;
  1269. }
  1270. additive_latency = (popts->addt_lat_override != 0) ?
  1271. popts->addt_lat_override_value : 0;
  1272. cal_ddr_csn_bnds(regs, popts, conf, pdimm);
  1273. cal_ddr_sdram_cfg(clk, regs, popts, pdimm, ip_rev);
  1274. cal_ddr_sdram_rcw(clk, regs, popts, pdimm);
  1275. cal_timing_cfg(clk, regs, popts, pdimm, conf, cas_latency,
  1276. additive_latency);
  1277. cal_ddr_dq_mapping(regs, pdimm);
  1278. if (ip_rev >= 0x50500) {
  1279. cal_ddr_addr_dec(regs);
  1280. }
  1281. cal_ddr_sdram_mode(clk, regs, popts, conf, pdimm, cas_latency,
  1282. additive_latency, ip_rev);
  1283. cal_ddr_eor(regs, popts);
  1284. cal_ddr_data_init(regs);
  1285. cal_ddr_sdram_interval(clk, regs, popts, pdimm);
  1286. cal_ddr_zq_cntl(regs);
  1287. cal_ddr_sr_cntr(regs, popts);
  1288. return 0;
  1289. }