2 * Copyright 2008 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * Version 2 as published by the Free Software Foundation.
10 #include <asm/fsl_ddr_sdram.h>
15 * compute_lowest_common_dimm_parameters()
17 * Determine the worst-case DIMM timing parameters from the set of DIMMs
18 * whose parameters have been computed into the array pointed to
22 compute_lowest_common_dimm_parameters(const dimm_params_t *dimm_params,
23 common_timing_params_t *outpdimm,
24 unsigned int number_of_dimms)
28 unsigned int tCKmin_X_ps = 0;
29 unsigned int tCKmax_ps = 0xFFFFFFFF;
30 unsigned int tCKmax_max_ps = 0;
31 unsigned int tRCD_ps = 0;
32 unsigned int tRP_ps = 0;
33 unsigned int tRAS_ps = 0;
34 unsigned int tWR_ps = 0;
35 unsigned int tWTR_ps = 0;
36 unsigned int tRFC_ps = 0;
37 unsigned int tRRD_ps = 0;
38 unsigned int tRC_ps = 0;
39 unsigned int refresh_rate_ps = 0;
40 unsigned int tIS_ps = 0;
41 unsigned int tIH_ps = 0;
42 unsigned int tDS_ps = 0;
43 unsigned int tDH_ps = 0;
44 unsigned int tRTP_ps = 0;
45 unsigned int tDQSQ_max_ps = 0;
46 unsigned int tQHS_ps = 0;
48 unsigned int temp1, temp2;
49 unsigned int lowest_good_caslat;
50 unsigned int additive_latency = 0;
51 const unsigned int mclk_ps = get_memory_clk_period_ps();
54 debug("using mclk_ps = %u\n", mclk_ps);
57 for (i = 0; i < number_of_dimms; i++) {
59 * If there are no ranks on this DIMM,
60 * it probably doesn't exist, so skip it.
62 if (dimm_params[i].n_ranks == 0) {
68 * Find minimum tCKmax_ps to find fastest slow speed,
69 * i.e., this is the slowest the whole system can go.
71 tCKmax_ps = min(tCKmax_ps, dimm_params[i].tCKmax_ps);
73 /* Either find maximum value to determine slowest
74 * speed, delay, time, period, etc */
75 tCKmin_X_ps = max(tCKmin_X_ps, dimm_params[i].tCKmin_X_ps);
76 tCKmax_max_ps = max(tCKmax_max_ps, dimm_params[i].tCKmax_ps);
77 tRCD_ps = max(tRCD_ps, dimm_params[i].tRCD_ps);
78 tRP_ps = max(tRP_ps, dimm_params[i].tRP_ps);
79 tRAS_ps = max(tRAS_ps, dimm_params[i].tRAS_ps);
80 tWR_ps = max(tWR_ps, dimm_params[i].tWR_ps);
81 tWTR_ps = max(tWTR_ps, dimm_params[i].tWTR_ps);
82 tRFC_ps = max(tRFC_ps, dimm_params[i].tRFC_ps);
83 tRRD_ps = max(tRRD_ps, dimm_params[i].tRRD_ps);
84 tRC_ps = max(tRC_ps, dimm_params[i].tRC_ps);
85 tIS_ps = max(tIS_ps, dimm_params[i].tIS_ps);
86 tIH_ps = max(tIH_ps, dimm_params[i].tIH_ps);
87 tDS_ps = max(tDS_ps, dimm_params[i].tDS_ps);
88 tDH_ps = max(tDH_ps, dimm_params[i].tDH_ps);
89 tRTP_ps = max(tRTP_ps, dimm_params[i].tRTP_ps);
90 tQHS_ps = max(tQHS_ps, dimm_params[i].tQHS_ps);
91 refresh_rate_ps = max(refresh_rate_ps,
92 dimm_params[i].refresh_rate_ps);
95 * Find maximum tDQSQ_max_ps to find slowest.
97 * FIXME: is finding the slowest value the correct
98 * strategy for this parameter?
100 tDQSQ_max_ps = max(tDQSQ_max_ps, dimm_params[i].tDQSQ_max_ps);
103 outpdimm->ndimms_present = number_of_dimms - temp1;
105 if (temp1 == number_of_dimms) {
106 debug("no dimms this memory controller\n");
110 outpdimm->tCKmin_X_ps = tCKmin_X_ps;
111 outpdimm->tCKmax_ps = tCKmax_ps;
112 outpdimm->tCKmax_max_ps = tCKmax_max_ps;
113 outpdimm->tRCD_ps = tRCD_ps;
114 outpdimm->tRP_ps = tRP_ps;
115 outpdimm->tRAS_ps = tRAS_ps;
116 outpdimm->tWR_ps = tWR_ps;
117 outpdimm->tWTR_ps = tWTR_ps;
118 outpdimm->tRFC_ps = tRFC_ps;
119 outpdimm->tRRD_ps = tRRD_ps;
120 outpdimm->tRC_ps = tRC_ps;
121 outpdimm->refresh_rate_ps = refresh_rate_ps;
122 outpdimm->tIS_ps = tIS_ps;
123 outpdimm->tIH_ps = tIH_ps;
124 outpdimm->tDS_ps = tDS_ps;
125 outpdimm->tDH_ps = tDH_ps;
126 outpdimm->tRTP_ps = tRTP_ps;
127 outpdimm->tDQSQ_max_ps = tDQSQ_max_ps;
128 outpdimm->tQHS_ps = tQHS_ps;
130 /* Determine common burst length for all DIMMs. */
132 for (i = 0; i < number_of_dimms; i++) {
133 if (dimm_params[i].n_ranks) {
134 temp1 &= dimm_params[i].burst_lengths_bitmask;
137 outpdimm->all_DIMMs_burst_lengths_bitmask = temp1;
139 /* Determine if all DIMMs registered buffered. */
141 for (i = 0; i < number_of_dimms; i++) {
142 if (dimm_params[i].n_ranks) {
143 if (dimm_params[i].registered_dimm)
145 if (!dimm_params[i].registered_dimm)
150 outpdimm->all_DIMMs_registered = 0;
151 if (temp1 && !temp2) {
152 outpdimm->all_DIMMs_registered = 1;
155 outpdimm->all_DIMMs_unbuffered = 0;
156 if (!temp1 && temp2) {
157 outpdimm->all_DIMMs_unbuffered = 1;
161 if (!outpdimm->all_DIMMs_registered
162 && !outpdimm->all_DIMMs_unbuffered) {
163 printf("ERROR: Mix of registered buffered and unbuffered "
164 "DIMMs detected!\n");
168 * Compute a CAS latency suitable for all DIMMs
170 * Strategy for SPD-defined latencies: compute only
171 * CAS latency defined by all DIMMs.
175 * Step 1: find CAS latency common to all DIMMs using bitwise
179 for (i = 0; i < number_of_dimms; i++) {
180 if (dimm_params[i].n_ranks) {
182 temp2 |= 1 << dimm_params[i].caslat_X;
183 temp2 |= 1 << dimm_params[i].caslat_X_minus_1;
184 temp2 |= 1 << dimm_params[i].caslat_X_minus_2;
186 * FIXME: If there was no entry for X-2 (X-1) in
187 * the SPD, then caslat_X_minus_2
188 * (caslat_X_minus_1) contains either 255 or
189 * 0xFFFFFFFF because that's what the glorious
190 * __ilog2 function returns for an input of 0.
191 * On 32-bit PowerPC, left shift counts with bit
192 * 26 set (that the value of 255 or 0xFFFFFFFF
193 * will have), cause the destination register to
194 * be 0. That is why this works.
201 * Step 2: check each common CAS latency against tCK of each
204 lowest_good_caslat = 0;
208 temp2 = __ilog2(temp1);
209 debug("checking common caslat = %u\n", temp2);
211 /* Check if this CAS latency will work on all DIMMs at tCK. */
212 for (i = 0; i < number_of_dimms; i++) {
213 if (!dimm_params[i].n_ranks) {
216 if (dimm_params[i].caslat_X == temp2) {
217 if (mclk_ps >= dimm_params[i].tCKmin_X_ps) {
218 debug("CL = %u ok on DIMM %u at tCK=%u"
219 " ps with its tCKmin_X_ps of %u\n",
221 dimm_params[i].tCKmin_X_ps);
228 if (dimm_params[i].caslat_X_minus_1 == temp2) {
229 unsigned int tCKmin_X_minus_1_ps
230 = dimm_params[i].tCKmin_X_minus_1_ps;
231 if (mclk_ps >= tCKmin_X_minus_1_ps) {
232 debug("CL = %u ok on DIMM %u at "
233 "tCK=%u ps with its "
234 "tCKmin_X_minus_1_ps of %u\n",
236 tCKmin_X_minus_1_ps);
243 if (dimm_params[i].caslat_X_minus_2 == temp2) {
244 unsigned int tCKmin_X_minus_2_ps
245 = dimm_params[i].tCKmin_X_minus_2_ps;
246 if (mclk_ps >= tCKmin_X_minus_2_ps) {
247 debug("CL = %u ok on DIMM %u at "
248 "tCK=%u ps with its "
249 "tCKmin_X_minus_2_ps of %u\n",
251 tCKmin_X_minus_2_ps);
260 lowest_good_caslat = temp2;
263 temp1 &= ~(1 << temp2);
266 debug("lowest common SPD-defined CAS latency = %u\n",
268 outpdimm->lowest_common_SPD_caslat = lowest_good_caslat;
272 * Compute a common 'de-rated' CAS latency.
274 * The strategy here is to find the *highest* dereated cas latency
275 * with the assumption that all of the DIMMs will support a dereated
276 * CAS latency higher than or equal to their lowest dereated value.
279 for (i = 0; i < number_of_dimms; i++) {
280 temp1 = max(temp1, dimm_params[i].caslat_lowest_derated);
282 outpdimm->highest_common_derated_caslat = temp1;
283 debug("highest common dereated CAS latency = %u\n", temp1);
285 /* Determine if all DIMMs ECC capable. */
287 for (i = 0; i < number_of_dimms; i++) {
288 if (dimm_params[i].n_ranks && dimm_params[i].edc_config != 2) {
294 debug("all DIMMs ECC capable\n");
296 debug("Warning: not all DIMMs ECC capable, cant enable ECC\n");
298 outpdimm->all_DIMMs_ECC_capable = temp1;
301 /* FIXME: move to somewhere else to validate. */
302 if (mclk_ps > tCKmax_max_ps) {
303 printf("Warning: some of the installed DIMMs "
304 "can not operate this slowly.\n");
309 * Compute additive latency.
311 * For DDR1, additive latency should be 0.
313 * For DDR2, with ODT enabled, use "a value" less than ACTTORW,
314 * which comes from Trcd, and also note that:
315 * add_lat + caslat must be >= 4
317 * For DDR3, FIXME additive latency determination
319 * When to use additive latency for DDR2:
321 * I. Because you are using CL=3 and need to do ODT on writes and
322 * want functionality.
323 * 1. Are you going to use ODT? (Does your board not have
324 * additional termination circuitry for DQ, DQS, DQS_,
325 * DM, RDQS, RDQS_ for x4/x8 configs?)
326 * 2. If so, is your lowest supported CL going to be 3?
327 * 3. If so, then you must set AL=1 because
329 * WL >= 3 for ODT on writes
338 * RL >= 3 for ODT on reads
341 * Since CL aren't usually less than 2, AL=0 is a minimum,
342 * so the WL-derived AL should be the -- FIXME?
344 * II. Because you are using auto-precharge globally and want to
345 * use additive latency (posted CAS) to get more bandwidth.
346 * 1. Are you going to use auto-precharge mode globally?
348 * Use addtivie latency and compute AL to be 1 cycle less than
349 * tRCD, i.e. the READ or WRITE command is in the cycle
350 * immediately following the ACTIVATE command..
352 * III. Because you feel like it or want to do some sort of
353 * degraded-performance experiment.
354 * 1. Do you just want to use additive latency because you feel
357 * Validation: AL is less than tRCD, and within the other
358 * read-to-precharge constraints.
361 additive_latency = 0;
363 #if defined(CONFIG_FSL_DDR2)
364 if (lowest_good_caslat < 4) {
365 additive_latency = picos_to_mclk(tRCD_ps) - lowest_good_caslat;
366 if (mclk_to_picos(additive_latency) > tRCD_ps) {
367 additive_latency = picos_to_mclk(tRCD_ps);
368 debug("setting additive_latency to %u because it was "
369 " greater than tRCD_ps\n", additive_latency);
373 #elif defined(CONFIG_FSL_DDR3)
374 error "FIXME determine additive latency for DDR3"
378 * Validate additive latency
379 * FIXME: move to somewhere else to validate
383 if (mclk_to_picos(additive_latency) > tRCD_ps) {
384 printf("Error: invalid additive latency exceeds tRCD(min).\n");
389 * RL = CL + AL; RL >= 3 for ODT_RD_CFG to be enabled
390 * WL = RL - 1; WL >= 3 for ODT_WL_CFG to be enabled
391 * ADD_LAT (the register) must be set to a value less
392 * than ACTTORW if WL = 1, then AL must be set to 1
393 * RD_TO_PRE (the register) must be set to a minimum
394 * tRTP + AL if AL is nonzero
398 * Additive latency will be applied only if the memctl option to
401 outpdimm->additive_latency = additive_latency;