blob: cc128112e4fc752c90443dad9fb08aa64c80b266 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kumar Gala124b0822008-08-26 15:01:29 -05002/*
York Sun6db4fdd2018-01-29 09:44:35 -08003 * Copyright 2008-2016 Freescale Semiconductor, Inc.
Maninder Singhe8fee9a2021-10-10 09:12:16 -07004 * Copyright 2017-2021 NXP Semiconductor
Kumar Gala124b0822008-08-26 15:01:29 -05005 */
6
York Sunf0626592013-09-30 09:22:09 -07007#include <fsl_ddr_sdram.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -06009#include <asm/bitops.h>
Kumar Gala124b0822008-08-26 15:01:29 -050010
York Sunf0626592013-09-30 09:22:09 -070011#include <fsl_ddr.h>
Kumar Gala124b0822008-08-26 15:01:29 -050012
York Sun2896cb72014-03-27 17:54:47 -070013#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Kim Phillips82f576f2012-10-29 13:34:37 +000014static unsigned int
York Sun2c0b62d2015-01-06 13:18:50 -080015compute_cas_latency(const unsigned int ctrl_num,
16 const dimm_params_t *dimm_params,
York Sun2896cb72014-03-27 17:54:47 -070017 common_timing_params_t *outpdimm,
18 unsigned int number_of_dimms)
Dave Liu4be87b22009-03-14 12:48:30 +080019{
20 unsigned int i;
Dave Liu4be87b22009-03-14 12:48:30 +080021 unsigned int common_caslat;
22 unsigned int caslat_actual;
23 unsigned int retry = 16;
York Sunfc63b282015-03-19 09:30:27 -070024 unsigned int tmp = ~0;
Maninder Singhe8fee9a2021-10-10 09:12:16 -070025 unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sun2896cb72014-03-27 17:54:47 -070026#ifdef CONFIG_SYS_FSL_DDR3
27 const unsigned int taamax = 20000;
28#else
29 const unsigned int taamax = 18000;
30#endif
Dave Liu4be87b22009-03-14 12:48:30 +080031
32 /* compute the common CAS latency supported between slots */
York Sunfc63b282015-03-19 09:30:27 -070033 for (i = 0; i < number_of_dimms; i++) {
York Sunfa3ede52012-08-17 08:22:41 +000034 if (dimm_params[i].n_ranks)
Priyanka Jain4a717412013-09-25 10:41:19 +053035 tmp &= dimm_params[i].caslat_x;
York Sunfa3ede52012-08-17 08:22:41 +000036 }
Dave Liu4be87b22009-03-14 12:48:30 +080037 common_caslat = tmp;
38
Maninder Singhe8fee9a2021-10-10 09:12:16 -070039 if (!mclk_ps) {
40 printf("DDR clock (MCLK cycle was 0 ps), So setting it to slowest DIMM(s) (tCKmin %u ps).\n",
41 outpdimm->tckmin_x_ps);
42 mclk_ps = outpdimm->tckmin_x_ps;
43 }
44
Dave Liu4be87b22009-03-14 12:48:30 +080045 /* validate if the memory clk is in the range of dimms */
York Sun2896cb72014-03-27 17:54:47 -070046 if (mclk_ps < outpdimm->tckmin_x_ps) {
York Sunc04da042011-05-06 07:14:14 +080047 printf("DDR clock (MCLK cycle %u ps) is faster than "
48 "the slowest DIMM(s) (tCKmin %u ps) can support.\n",
York Sun2896cb72014-03-27 17:54:47 -070049 mclk_ps, outpdimm->tckmin_x_ps);
Dave Liu4be87b22009-03-14 12:48:30 +080050 }
York Sun2896cb72014-03-27 17:54:47 -070051#ifdef CONFIG_SYS_FSL_DDR4
52 if (mclk_ps > outpdimm->tckmax_ps) {
53 printf("DDR clock (MCLK cycle %u ps) is slower than DIMM(s) (tCKmax %u ps) can support.\n",
54 mclk_ps, outpdimm->tckmax_ps);
55 }
56#endif
Dave Liu4be87b22009-03-14 12:48:30 +080057 /* determine the acutal cas latency */
York Sun2896cb72014-03-27 17:54:47 -070058 caslat_actual = (outpdimm->taamin_ps + mclk_ps - 1) / mclk_ps;
Dave Liu4be87b22009-03-14 12:48:30 +080059 /* check if the dimms support the CAS latency */
60 while (!(common_caslat & (1 << caslat_actual)) && retry > 0) {
61 caslat_actual++;
62 retry--;
63 }
64 /* once the caculation of caslat_actual is completed
65 * we must verify that this CAS latency value does not
York Sun2896cb72014-03-27 17:54:47 -070066 * exceed tAAmax, which is 20 ns for all DDR3 speed grades,
67 * 18ns for all DDR4 speed grades.
Dave Liu4be87b22009-03-14 12:48:30 +080068 */
York Sun2896cb72014-03-27 17:54:47 -070069 if (caslat_actual * mclk_ps > taamax) {
Alexander Merkle0137e602016-03-17 15:44:47 +010070 printf("The chosen cas latency %d is too large\n",
71 caslat_actual);
Dave Liu4be87b22009-03-14 12:48:30 +080072 }
York Sun2896cb72014-03-27 17:54:47 -070073 outpdimm->lowest_common_spd_caslat = caslat_actual;
74 debug("lowest_common_spd_caslat is 0x%x\n", caslat_actual);
Dave Liu4be87b22009-03-14 12:48:30 +080075
76 return 0;
77}
York Sun2896cb72014-03-27 17:54:47 -070078#else /* for DDR1 and DDR2 */
79static unsigned int
York Sun2c0b62d2015-01-06 13:18:50 -080080compute_cas_latency(const unsigned int ctrl_num,
81 const dimm_params_t *dimm_params,
York Sun2896cb72014-03-27 17:54:47 -070082 common_timing_params_t *outpdimm,
83 unsigned int number_of_dimms)
84{
85 int i;
York Sun2c0b62d2015-01-06 13:18:50 -080086 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sun2896cb72014-03-27 17:54:47 -070087 unsigned int lowest_good_caslat;
88 unsigned int not_ok;
89 unsigned int temp1, temp2;
90
91 debug("using mclk_ps = %u\n", mclk_ps);
92 if (mclk_ps > outpdimm->tckmax_ps) {
93 printf("Warning: DDR clock (%u ps) is slower than DIMM(s) (tCKmax %u ps)\n",
94 mclk_ps, outpdimm->tckmax_ps);
95 }
96
97 /*
98 * Compute a CAS latency suitable for all DIMMs
99 *
100 * Strategy for SPD-defined latencies: compute only
101 * CAS latency defined by all DIMMs.
102 */
103
104 /*
105 * Step 1: find CAS latency common to all DIMMs using bitwise
106 * operation.
107 */
108 temp1 = 0xFF;
109 for (i = 0; i < number_of_dimms; i++) {
110 if (dimm_params[i].n_ranks) {
111 temp2 = 0;
112 temp2 |= 1 << dimm_params[i].caslat_x;
113 temp2 |= 1 << dimm_params[i].caslat_x_minus_1;
114 temp2 |= 1 << dimm_params[i].caslat_x_minus_2;
115 /*
116 * If there was no entry for X-2 (X-1) in
117 * the SPD, then caslat_x_minus_2
118 * (caslat_x_minus_1) contains either 255 or
119 * 0xFFFFFFFF because that's what the glorious
120 * __ilog2 function returns for an input of 0.
121 * On 32-bit PowerPC, left shift counts with bit
122 * 26 set (that the value of 255 or 0xFFFFFFFF
123 * will have), cause the destination register to
124 * be 0. That is why this works.
125 */
126 temp1 &= temp2;
127 }
128 }
129
130 /*
131 * Step 2: check each common CAS latency against tCK of each
132 * DIMM's SPD.
133 */
134 lowest_good_caslat = 0;
135 temp2 = 0;
136 while (temp1) {
137 not_ok = 0;
138 temp2 = __ilog2(temp1);
139 debug("checking common caslat = %u\n", temp2);
140
141 /* Check if this CAS latency will work on all DIMMs at tCK. */
142 for (i = 0; i < number_of_dimms; i++) {
143 if (!dimm_params[i].n_ranks)
144 continue;
145
146 if (dimm_params[i].caslat_x == temp2) {
147 if (mclk_ps >= dimm_params[i].tckmin_x_ps) {
148 debug("CL = %u ok on DIMM %u at tCK=%u ps with tCKmin_X_ps of %u\n",
149 temp2, i, mclk_ps,
150 dimm_params[i].tckmin_x_ps);
151 continue;
152 } else {
153 not_ok++;
154 }
155 }
156
157 if (dimm_params[i].caslat_x_minus_1 == temp2) {
158 unsigned int tckmin_x_minus_1_ps
159 = dimm_params[i].tckmin_x_minus_1_ps;
160 if (mclk_ps >= tckmin_x_minus_1_ps) {
161 debug("CL = %u ok on DIMM %u at tCK=%u ps with tckmin_x_minus_1_ps of %u\n",
162 temp2, i, mclk_ps,
163 tckmin_x_minus_1_ps);
164 continue;
165 } else {
166 not_ok++;
167 }
168 }
169
170 if (dimm_params[i].caslat_x_minus_2 == temp2) {
171 unsigned int tckmin_x_minus_2_ps
172 = dimm_params[i].tckmin_x_minus_2_ps;
173 if (mclk_ps >= tckmin_x_minus_2_ps) {
174 debug("CL = %u ok on DIMM %u at tCK=%u ps with tckmin_x_minus_2_ps of %u\n",
175 temp2, i, mclk_ps,
176 tckmin_x_minus_2_ps);
177 continue;
178 } else {
179 not_ok++;
180 }
181 }
182 }
183
184 if (!not_ok)
185 lowest_good_caslat = temp2;
186
187 temp1 &= ~(1 << temp2);
188 }
189
190 debug("lowest common SPD-defined CAS latency = %u\n",
191 lowest_good_caslat);
192 outpdimm->lowest_common_spd_caslat = lowest_good_caslat;
193
York Sun2896cb72014-03-27 17:54:47 -0700194 /*
195 * Compute a common 'de-rated' CAS latency.
196 *
197 * The strategy here is to find the *highest* dereated cas latency
198 * with the assumption that all of the DIMMs will support a dereated
199 * CAS latency higher than or equal to their lowest dereated value.
200 */
201 temp1 = 0;
202 for (i = 0; i < number_of_dimms; i++)
203 temp1 = max(temp1, dimm_params[i].caslat_lowest_derated);
204
205 outpdimm->highest_common_derated_caslat = temp1;
206 debug("highest common dereated CAS latency = %u\n", temp1);
207
208 return 0;
209}
Kim Phillips82f576f2012-10-29 13:34:37 +0000210#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800211
Kumar Gala124b0822008-08-26 15:01:29 -0500212/*
213 * compute_lowest_common_dimm_parameters()
214 *
215 * Determine the worst-case DIMM timing parameters from the set of DIMMs
216 * whose parameters have been computed into the array pointed to
217 * by dimm_params.
218 */
219unsigned int
York Sun2c0b62d2015-01-06 13:18:50 -0800220compute_lowest_common_dimm_parameters(const unsigned int ctrl_num,
221 const dimm_params_t *dimm_params,
Kumar Gala124b0822008-08-26 15:01:29 -0500222 common_timing_params_t *outpdimm,
York Sun98df4d12012-10-08 07:44:23 +0000223 const unsigned int number_of_dimms)
Kumar Gala124b0822008-08-26 15:01:29 -0500224{
yorkde879322010-07-02 22:25:55 +0000225 unsigned int i, j;
Kumar Gala124b0822008-08-26 15:01:29 -0500226
Priyanka Jain4a717412013-09-25 10:41:19 +0530227 unsigned int tckmin_x_ps = 0;
228 unsigned int tckmax_ps = 0xFFFFFFFF;
Priyanka Jain4a717412013-09-25 10:41:19 +0530229 unsigned int trcd_ps = 0;
230 unsigned int trp_ps = 0;
231 unsigned int tras_ps = 0;
York Sun2896cb72014-03-27 17:54:47 -0700232#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
233 unsigned int taamin_ps = 0;
234#endif
235#ifdef CONFIG_SYS_FSL_DDR4
236 unsigned int twr_ps = 15000;
237 unsigned int trfc1_ps = 0;
238 unsigned int trfc2_ps = 0;
239 unsigned int trfc4_ps = 0;
240 unsigned int trrds_ps = 0;
241 unsigned int trrdl_ps = 0;
242 unsigned int tccdl_ps = 0;
York Sun6db4fdd2018-01-29 09:44:35 -0800243 unsigned int trfc_slr_ps = 0;
York Sun2896cb72014-03-27 17:54:47 -0700244#else
Priyanka Jain4a717412013-09-25 10:41:19 +0530245 unsigned int twr_ps = 0;
246 unsigned int twtr_ps = 0;
247 unsigned int trfc_ps = 0;
248 unsigned int trrd_ps = 0;
York Sun2896cb72014-03-27 17:54:47 -0700249 unsigned int trtp_ps = 0;
250#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530251 unsigned int trc_ps = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500252 unsigned int refresh_rate_ps = 0;
Valentin Longchamp0b810932013-10-18 11:47:20 +0200253 unsigned int extended_op_srt = 1;
York Sun2896cb72014-03-27 17:54:47 -0700254#if defined(CONFIG_SYS_FSL_DDR1) || defined(CONFIG_SYS_FSL_DDR2)
Priyanka Jain4a717412013-09-25 10:41:19 +0530255 unsigned int tis_ps = 0;
256 unsigned int tih_ps = 0;
257 unsigned int tds_ps = 0;
258 unsigned int tdh_ps = 0;
Priyanka Jain4a717412013-09-25 10:41:19 +0530259 unsigned int tdqsq_max_ps = 0;
260 unsigned int tqhs_ps = 0;
York Sun2896cb72014-03-27 17:54:47 -0700261#endif
York Sund56624f2011-01-10 12:02:56 +0000262 unsigned int temp1, temp2;
Kumar Gala124b0822008-08-26 15:01:29 -0500263 unsigned int additive_latency = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500264
265 temp1 = 0;
266 for (i = 0; i < number_of_dimms; i++) {
267 /*
268 * If there are no ranks on this DIMM,
269 * it probably doesn't exist, so skip it.
270 */
271 if (dimm_params[i].n_ranks == 0) {
272 temp1++;
273 continue;
274 }
yorkf4f93c62010-07-02 22:25:53 +0000275 if (dimm_params[i].n_ranks == 4 && i != 0) {
276 printf("Found Quad-rank DIMM in wrong bank, ignored."
277 " Software may not run as expected.\n");
278 temp1++;
279 continue;
280 }
York Sun98df4d12012-10-08 07:44:23 +0000281
282 /*
283 * check if quad-rank DIMM is plugged if
284 * CONFIG_CHIP_SELECT_QUAD_CAPABLE is not defined
285 * Only the board with proper design is capable
286 */
287#ifndef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
yorkf4f93c62010-07-02 22:25:53 +0000288 if (dimm_params[i].n_ranks == 4 && \
289 CONFIG_CHIP_SELECTS_PER_CTRL/CONFIG_DIMM_SLOTS_PER_CTLR < 4) {
290 printf("Found Quad-rank DIMM, not able to support.");
291 temp1++;
292 continue;
293 }
York Sun98df4d12012-10-08 07:44:23 +0000294#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500295 /*
Priyanka Jain4a717412013-09-25 10:41:19 +0530296 * Find minimum tckmax_ps to find fastest slow speed,
Kumar Gala124b0822008-08-26 15:01:29 -0500297 * i.e., this is the slowest the whole system can go.
298 */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900299 tckmax_ps = min(tckmax_ps,
300 (unsigned int)dimm_params[i].tckmax_ps);
York Sun2896cb72014-03-27 17:54:47 -0700301#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900302 taamin_ps = max(taamin_ps,
303 (unsigned int)dimm_params[i].taa_ps);
York Sun2896cb72014-03-27 17:54:47 -0700304#endif
Masahiro Yamadadb204642014-11-07 03:03:31 +0900305 tckmin_x_ps = max(tckmin_x_ps,
306 (unsigned int)dimm_params[i].tckmin_x_ps);
307 trcd_ps = max(trcd_ps, (unsigned int)dimm_params[i].trcd_ps);
308 trp_ps = max(trp_ps, (unsigned int)dimm_params[i].trp_ps);
309 tras_ps = max(tras_ps, (unsigned int)dimm_params[i].tras_ps);
York Sun2896cb72014-03-27 17:54:47 -0700310#ifdef CONFIG_SYS_FSL_DDR4
Masahiro Yamadadb204642014-11-07 03:03:31 +0900311 trfc1_ps = max(trfc1_ps,
312 (unsigned int)dimm_params[i].trfc1_ps);
313 trfc2_ps = max(trfc2_ps,
314 (unsigned int)dimm_params[i].trfc2_ps);
315 trfc4_ps = max(trfc4_ps,
316 (unsigned int)dimm_params[i].trfc4_ps);
317 trrds_ps = max(trrds_ps,
318 (unsigned int)dimm_params[i].trrds_ps);
319 trrdl_ps = max(trrdl_ps,
320 (unsigned int)dimm_params[i].trrdl_ps);
321 tccdl_ps = max(tccdl_ps,
322 (unsigned int)dimm_params[i].tccdl_ps);
York Sun6db4fdd2018-01-29 09:44:35 -0800323 trfc_slr_ps = max(trfc_slr_ps,
324 (unsigned int)dimm_params[i].trfc_slr_ps);
York Sun2896cb72014-03-27 17:54:47 -0700325#else
Masahiro Yamadadb204642014-11-07 03:03:31 +0900326 twr_ps = max(twr_ps, (unsigned int)dimm_params[i].twr_ps);
327 twtr_ps = max(twtr_ps, (unsigned int)dimm_params[i].twtr_ps);
328 trfc_ps = max(trfc_ps, (unsigned int)dimm_params[i].trfc_ps);
329 trrd_ps = max(trrd_ps, (unsigned int)dimm_params[i].trrd_ps);
330 trtp_ps = max(trtp_ps, (unsigned int)dimm_params[i].trtp_ps);
York Sun2896cb72014-03-27 17:54:47 -0700331#endif
Masahiro Yamadadb204642014-11-07 03:03:31 +0900332 trc_ps = max(trc_ps, (unsigned int)dimm_params[i].trc_ps);
York Sun2896cb72014-03-27 17:54:47 -0700333#if defined(CONFIG_SYS_FSL_DDR1) || defined(CONFIG_SYS_FSL_DDR2)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900334 tis_ps = max(tis_ps, (unsigned int)dimm_params[i].tis_ps);
335 tih_ps = max(tih_ps, (unsigned int)dimm_params[i].tih_ps);
336 tds_ps = max(tds_ps, (unsigned int)dimm_params[i].tds_ps);
337 tdh_ps = max(tdh_ps, (unsigned int)dimm_params[i].tdh_ps);
338 tqhs_ps = max(tqhs_ps, (unsigned int)dimm_params[i].tqhs_ps);
Kumar Gala124b0822008-08-26 15:01:29 -0500339 /*
Priyanka Jain4a717412013-09-25 10:41:19 +0530340 * Find maximum tdqsq_max_ps to find slowest.
Kumar Gala124b0822008-08-26 15:01:29 -0500341 *
342 * FIXME: is finding the slowest value the correct
343 * strategy for this parameter?
344 */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900345 tdqsq_max_ps = max(tdqsq_max_ps,
346 (unsigned int)dimm_params[i].tdqsq_max_ps);
York Sun2896cb72014-03-27 17:54:47 -0700347#endif
348 refresh_rate_ps = max(refresh_rate_ps,
Masahiro Yamadadb204642014-11-07 03:03:31 +0900349 (unsigned int)dimm_params[i].refresh_rate_ps);
York Sun2896cb72014-03-27 17:54:47 -0700350 /* extended_op_srt is either 0 or 1, 0 having priority */
351 extended_op_srt = min(extended_op_srt,
Masahiro Yamadadb204642014-11-07 03:03:31 +0900352 (unsigned int)dimm_params[i].extended_op_srt);
Kumar Gala124b0822008-08-26 15:01:29 -0500353 }
354
355 outpdimm->ndimms_present = number_of_dimms - temp1;
356
357 if (temp1 == number_of_dimms) {
358 debug("no dimms this memory controller\n");
359 return 0;
360 }
361
Priyanka Jain4a717412013-09-25 10:41:19 +0530362 outpdimm->tckmin_x_ps = tckmin_x_ps;
363 outpdimm->tckmax_ps = tckmax_ps;
York Sun2896cb72014-03-27 17:54:47 -0700364#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
365 outpdimm->taamin_ps = taamin_ps;
366#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530367 outpdimm->trcd_ps = trcd_ps;
368 outpdimm->trp_ps = trp_ps;
369 outpdimm->tras_ps = tras_ps;
York Sun2896cb72014-03-27 17:54:47 -0700370#ifdef CONFIG_SYS_FSL_DDR4
371 outpdimm->trfc1_ps = trfc1_ps;
372 outpdimm->trfc2_ps = trfc2_ps;
373 outpdimm->trfc4_ps = trfc4_ps;
374 outpdimm->trrds_ps = trrds_ps;
375 outpdimm->trrdl_ps = trrdl_ps;
376 outpdimm->tccdl_ps = tccdl_ps;
York Sun6db4fdd2018-01-29 09:44:35 -0800377 outpdimm->trfc_slr_ps = trfc_slr_ps;
York Sun2896cb72014-03-27 17:54:47 -0700378#else
Priyanka Jain4a717412013-09-25 10:41:19 +0530379 outpdimm->twtr_ps = twtr_ps;
380 outpdimm->trfc_ps = trfc_ps;
381 outpdimm->trrd_ps = trrd_ps;
York Sun2896cb72014-03-27 17:54:47 -0700382 outpdimm->trtp_ps = trtp_ps;
383#endif
384 outpdimm->twr_ps = twr_ps;
Priyanka Jain4a717412013-09-25 10:41:19 +0530385 outpdimm->trc_ps = trc_ps;
Kumar Gala124b0822008-08-26 15:01:29 -0500386 outpdimm->refresh_rate_ps = refresh_rate_ps;
Valentin Longchamp0b810932013-10-18 11:47:20 +0200387 outpdimm->extended_op_srt = extended_op_srt;
York Sun2896cb72014-03-27 17:54:47 -0700388#if defined(CONFIG_SYS_FSL_DDR1) || defined(CONFIG_SYS_FSL_DDR2)
Priyanka Jain4a717412013-09-25 10:41:19 +0530389 outpdimm->tis_ps = tis_ps;
390 outpdimm->tih_ps = tih_ps;
391 outpdimm->tds_ps = tds_ps;
392 outpdimm->tdh_ps = tdh_ps;
Priyanka Jain4a717412013-09-25 10:41:19 +0530393 outpdimm->tdqsq_max_ps = tdqsq_max_ps;
394 outpdimm->tqhs_ps = tqhs_ps;
York Sun2896cb72014-03-27 17:54:47 -0700395#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500396
397 /* Determine common burst length for all DIMMs. */
398 temp1 = 0xff;
399 for (i = 0; i < number_of_dimms; i++) {
400 if (dimm_params[i].n_ranks) {
401 temp1 &= dimm_params[i].burst_lengths_bitmask;
402 }
403 }
Priyanka Jain4a717412013-09-25 10:41:19 +0530404 outpdimm->all_dimms_burst_lengths_bitmask = temp1;
Kumar Gala124b0822008-08-26 15:01:29 -0500405
406 /* Determine if all DIMMs registered buffered. */
407 temp1 = temp2 = 0;
408 for (i = 0; i < number_of_dimms; i++) {
409 if (dimm_params[i].n_ranks) {
York Sunb06fcb52011-02-04 13:58:00 -0800410 if (dimm_params[i].registered_dimm) {
Kumar Gala124b0822008-08-26 15:01:29 -0500411 temp1 = 1;
Ying Zhang9ff70262013-08-16 15:16:11 +0800412#ifndef CONFIG_SPL_BUILD
York Sunb06fcb52011-02-04 13:58:00 -0800413 printf("Detected RDIMM %s\n",
414 dimm_params[i].mpart);
Ying Zhang9ff70262013-08-16 15:16:11 +0800415#endif
York Sunb06fcb52011-02-04 13:58:00 -0800416 } else {
Kumar Gala124b0822008-08-26 15:01:29 -0500417 temp2 = 1;
Ying Zhang9ff70262013-08-16 15:16:11 +0800418#ifndef CONFIG_SPL_BUILD
York Sunb06fcb52011-02-04 13:58:00 -0800419 printf("Detected UDIMM %s\n",
420 dimm_params[i].mpart);
Ying Zhang9ff70262013-08-16 15:16:11 +0800421#endif
York Sunb06fcb52011-02-04 13:58:00 -0800422 }
Pali Roháre917a0b2022-09-09 17:32:45 +0200423#ifndef CONFIG_SPL_BUILD
424 puts(" ");
425#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500426 }
427 }
428
Priyanka Jain4a717412013-09-25 10:41:19 +0530429 outpdimm->all_dimms_registered = 0;
430 outpdimm->all_dimms_unbuffered = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500431 if (temp1 && !temp2) {
Priyanka Jain4a717412013-09-25 10:41:19 +0530432 outpdimm->all_dimms_registered = 1;
York Sund56624f2011-01-10 12:02:56 +0000433 } else if (!temp1 && temp2) {
Priyanka Jain4a717412013-09-25 10:41:19 +0530434 outpdimm->all_dimms_unbuffered = 1;
York Sund56624f2011-01-10 12:02:56 +0000435 } else {
Kumar Gala124b0822008-08-26 15:01:29 -0500436 printf("ERROR: Mix of registered buffered and unbuffered "
437 "DIMMs detected!\n");
438 }
439
yorkde879322010-07-02 22:25:55 +0000440 temp1 = 0;
Priyanka Jain4a717412013-09-25 10:41:19 +0530441 if (outpdimm->all_dimms_registered)
yorkde879322010-07-02 22:25:55 +0000442 for (j = 0; j < 16; j++) {
443 outpdimm->rcw[j] = dimm_params[0].rcw[j];
York Sun98df4d12012-10-08 07:44:23 +0000444 for (i = 1; i < number_of_dimms; i++) {
445 if (!dimm_params[i].n_ranks)
446 continue;
yorkde879322010-07-02 22:25:55 +0000447 if (dimm_params[i].rcw[j] != dimm_params[0].rcw[j]) {
York Sund56624f2011-01-10 12:02:56 +0000448 temp1 = 1;
yorkde879322010-07-02 22:25:55 +0000449 break;
450 }
York Sun98df4d12012-10-08 07:44:23 +0000451 }
yorkde879322010-07-02 22:25:55 +0000452 }
453
454 if (temp1 != 0)
455 printf("ERROR: Mix different RDIMM detected!\n");
456
York Sun2896cb72014-03-27 17:54:47 -0700457 /* calculate cas latency for all DDR types */
York Sun2c0b62d2015-01-06 13:18:50 -0800458 if (compute_cas_latency(ctrl_num, dimm_params,
459 outpdimm, number_of_dimms))
Dave Liu4be87b22009-03-14 12:48:30 +0800460 return 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500461
462 /* Determine if all DIMMs ECC capable. */
463 temp1 = 1;
464 for (i = 0; i < number_of_dimms; i++) {
York Sunfbe65952011-03-17 11:18:10 -0700465 if (dimm_params[i].n_ranks &&
466 !(dimm_params[i].edc_config & EDC_ECC)) {
Kumar Gala124b0822008-08-26 15:01:29 -0500467 temp1 = 0;
468 break;
469 }
470 }
471 if (temp1) {
472 debug("all DIMMs ECC capable\n");
473 } else {
474 debug("Warning: not all DIMMs ECC capable, cant enable ECC\n");
475 }
Priyanka Jain4a717412013-09-25 10:41:19 +0530476 outpdimm->all_dimms_ecc_capable = temp1;
Kumar Gala124b0822008-08-26 15:01:29 -0500477
Kumar Gala124b0822008-08-26 15:01:29 -0500478 /*
479 * Compute additive latency.
480 *
481 * For DDR1, additive latency should be 0.
482 *
483 * For DDR2, with ODT enabled, use "a value" less than ACTTORW,
484 * which comes from Trcd, and also note that:
485 * add_lat + caslat must be >= 4
486 *
Dave Liu4be87b22009-03-14 12:48:30 +0800487 * For DDR3, we use the AL=0
Kumar Gala124b0822008-08-26 15:01:29 -0500488 *
489 * When to use additive latency for DDR2:
490 *
491 * I. Because you are using CL=3 and need to do ODT on writes and
492 * want functionality.
493 * 1. Are you going to use ODT? (Does your board not have
494 * additional termination circuitry for DQ, DQS, DQS_,
495 * DM, RDQS, RDQS_ for x4/x8 configs?)
496 * 2. If so, is your lowest supported CL going to be 3?
497 * 3. If so, then you must set AL=1 because
498 *
499 * WL >= 3 for ODT on writes
500 * RL = AL + CL
501 * WL = RL - 1
502 * ->
503 * WL = AL + CL - 1
504 * AL + CL - 1 >= 3
505 * AL + CL >= 4
506 * QED
507 *
508 * RL >= 3 for ODT on reads
509 * RL = AL + CL
510 *
511 * Since CL aren't usually less than 2, AL=0 is a minimum,
512 * so the WL-derived AL should be the -- FIXME?
513 *
514 * II. Because you are using auto-precharge globally and want to
515 * use additive latency (posted CAS) to get more bandwidth.
516 * 1. Are you going to use auto-precharge mode globally?
517 *
518 * Use addtivie latency and compute AL to be 1 cycle less than
519 * tRCD, i.e. the READ or WRITE command is in the cycle
520 * immediately following the ACTIVATE command..
521 *
522 * III. Because you feel like it or want to do some sort of
523 * degraded-performance experiment.
524 * 1. Do you just want to use additive latency because you feel
525 * like it?
526 *
527 * Validation: AL is less than tRCD, and within the other
528 * read-to-precharge constraints.
529 */
530
531 additive_latency = 0;
532
York Sunf0626592013-09-30 09:22:09 -0700533#if defined(CONFIG_SYS_FSL_DDR2)
York Sun2896cb72014-03-27 17:54:47 -0700534 if ((outpdimm->lowest_common_spd_caslat < 4) &&
York Sun2c0b62d2015-01-06 13:18:50 -0800535 (picos_to_mclk(ctrl_num, trcd_ps) >
536 outpdimm->lowest_common_spd_caslat)) {
537 additive_latency = picos_to_mclk(ctrl_num, trcd_ps) -
York Sun2896cb72014-03-27 17:54:47 -0700538 outpdimm->lowest_common_spd_caslat;
York Sun2c0b62d2015-01-06 13:18:50 -0800539 if (mclk_to_picos(ctrl_num, additive_latency) > trcd_ps) {
540 additive_latency = picos_to_mclk(ctrl_num, trcd_ps);
Kumar Gala124b0822008-08-26 15:01:29 -0500541 debug("setting additive_latency to %u because it was "
542 " greater than tRCD_ps\n", additive_latency);
543 }
544 }
Kumar Gala124b0822008-08-26 15:01:29 -0500545#endif
546
547 /*
548 * Validate additive latency
Kumar Gala124b0822008-08-26 15:01:29 -0500549 *
550 * AL <= tRCD(min)
551 */
York Sun2c0b62d2015-01-06 13:18:50 -0800552 if (mclk_to_picos(ctrl_num, additive_latency) > trcd_ps) {
Kumar Gala124b0822008-08-26 15:01:29 -0500553 printf("Error: invalid additive latency exceeds tRCD(min).\n");
554 return 1;
555 }
556
557 /*
558 * RL = CL + AL; RL >= 3 for ODT_RD_CFG to be enabled
559 * WL = RL - 1; WL >= 3 for ODT_WL_CFG to be enabled
560 * ADD_LAT (the register) must be set to a value less
561 * than ACTTORW if WL = 1, then AL must be set to 1
562 * RD_TO_PRE (the register) must be set to a minimum
563 * tRTP + AL if AL is nonzero
564 */
565
566 /*
567 * Additive latency will be applied only if the memctl option to
568 * use it.
569 */
570 outpdimm->additive_latency = additive_latency;
571
Priyanka Jain4a717412013-09-25 10:41:19 +0530572 debug("tCKmin_ps = %u\n", outpdimm->tckmin_x_ps);
573 debug("trcd_ps = %u\n", outpdimm->trcd_ps);
574 debug("trp_ps = %u\n", outpdimm->trp_ps);
575 debug("tras_ps = %u\n", outpdimm->tras_ps);
York Sun2896cb72014-03-27 17:54:47 -0700576#ifdef CONFIG_SYS_FSL_DDR4
577 debug("trfc1_ps = %u\n", trfc1_ps);
578 debug("trfc2_ps = %u\n", trfc2_ps);
579 debug("trfc4_ps = %u\n", trfc4_ps);
580 debug("trrds_ps = %u\n", trrds_ps);
581 debug("trrdl_ps = %u\n", trrdl_ps);
582 debug("tccdl_ps = %u\n", tccdl_ps);
York Sun6db4fdd2018-01-29 09:44:35 -0800583 debug("trfc_slr_ps = %u\n", trfc_slr_ps);
York Sun2896cb72014-03-27 17:54:47 -0700584#else
Priyanka Jain4a717412013-09-25 10:41:19 +0530585 debug("twtr_ps = %u\n", outpdimm->twtr_ps);
586 debug("trfc_ps = %u\n", outpdimm->trfc_ps);
587 debug("trrd_ps = %u\n", outpdimm->trrd_ps);
York Sun2896cb72014-03-27 17:54:47 -0700588#endif
589 debug("twr_ps = %u\n", outpdimm->twr_ps);
Priyanka Jain4a717412013-09-25 10:41:19 +0530590 debug("trc_ps = %u\n", outpdimm->trc_ps);
York Suncd077cf2012-08-17 08:22:40 +0000591
Kumar Gala124b0822008-08-26 15:01:29 -0500592 return 0;
593}