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>
14 * Calculate the Density of each Physical Rank.
15 * Returned size is in bytes.
17 * Study these table from Byte 31 of JEDEC SPD Spec.
31 * Reorder Table to be linear by stripping the bottom
32 * 2 or 5 bits off and shifting them up to the top.
36 compute_ranksize(unsigned int mem_type, unsigned char row_dens)
40 /* Bottom 5 bits up to the top. */
41 bsize = ((row_dens >> 5) | ((row_dens & 31) << 3));
43 debug("DDR: DDR II rank density = 0x%08x\n", bsize);
49 * Convert a two-nibble BCD value into a cycle time.
50 * While the spec calls for nano-seconds, picos are returned.
52 * This implements the tables for bytes 9, 23 and 25 for both
53 * DDR I and II. No allowance for distinguishing the invalid
54 * fields absent for DDR I yet present in DDR II is made.
55 * (That is, cycle times of .25, .33, .66 and .75 ns are
56 * allowed for both DDR II and I.)
59 convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
61 /* Table look up the lower nibble, allow DDR I & II. */
62 unsigned int tenths_ps[16] = {
73 250, /* This and the next 3 entries valid ... */
74 330, /* ... only for tCK calculations. */
81 unsigned int whole_ns = (spd_val & 0xF0) >> 4;
82 unsigned int tenth_ns = spd_val & 0x0F;
83 unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
89 convert_bcd_hundredths_to_cycle_time_ps(unsigned int spd_val)
91 unsigned int tenth_ns = (spd_val & 0xF0) >> 4;
92 unsigned int hundredth_ns = spd_val & 0x0F;
93 unsigned int ps = tenth_ns * 100 + hundredth_ns * 10;
98 static unsigned int byte40_table_ps[8] = {
105 0, /* supposed to be RFC, but not sure what that means */
110 compute_trfc_ps_from_spd(unsigned char trctrfc_ext, unsigned char trfc)
112 unsigned int trfc_ps;
114 trfc_ps = (((trctrfc_ext & 0x1) * 256) + trfc) * 1000
115 + byte40_table_ps[(trctrfc_ext >> 1) & 0x7];
121 compute_trc_ps_from_spd(unsigned char trctrfc_ext, unsigned char trc)
125 trc_ps = trc * 1000 + byte40_table_ps[(trctrfc_ext >> 4) & 0x7];
131 * Determine Refresh Rate. Ignore self refresh bit on DDR I.
132 * Table from SPD Spec, Byte 12, converted to picoseconds and
133 * filled in with "default" normal values.
136 determine_refresh_rate_ps(const unsigned int spd_refresh)
138 unsigned int refresh_time_ps[8] = {
139 15625000, /* 0 Normal 1.00x */
140 3900000, /* 1 Reduced .25x */
141 7800000, /* 2 Extended .50x */
142 31300000, /* 3 Extended 2.00x */
143 62500000, /* 4 Extended 4.00x */
144 125000000, /* 5 Extended 8.00x */
145 15625000, /* 6 Normal 1.00x filler */
146 15625000, /* 7 Normal 1.00x filler */
149 return refresh_time_ps[spd_refresh & 0x7];
153 * The purpose of this function is to compute a suitable
154 * CAS latency given the DRAM clock period. The SPD only
155 * defines at most 3 CAS latencies. Typically the slower in
156 * frequency the DIMM runs at, the shorter its CAS latency can.
157 * be. If the DIMM is operating at a sufficiently low frequency,
158 * it may be able to run at a CAS latency shorter than the
159 * shortest SPD-defined CAS latency.
161 * If a CAS latency is not found, 0 is returned.
163 * Do this by finding in the standard speed bin table the longest
164 * tCKmin that doesn't exceed the value of mclk_ps (tCK).
166 * An assumption made is that the SDRAM device allows the
167 * CL to be programmed for a value that is lower than those
168 * advertised by the SPD. This is not always the case,
169 * as those modes not defined in the SPD are optional.
171 * CAS latency de-rating based upon values JEDEC Standard No. 79-2C
172 * Table 40, "DDR2 SDRAM stanadard speed bins and tCK, tRCD, tRP, tRAS,
173 * and tRC for corresponding bin"
175 * ordinal 2, ddr2_speed_bins[1] contains tCK for CL=3
176 * Not certain if any good value exists for CL=2
178 /* CL2 CL3 CL4 CL5 CL6 */
179 unsigned short ddr2_speed_bins[] = { 0, 5000, 3750, 3000, 2500 };
182 compute_derated_DDR2_CAS_latency(unsigned int mclk_ps)
184 const unsigned int num_speed_bins = ARRAY_SIZE(ddr2_speed_bins);
185 unsigned int lowest_tCKmin_found = 0;
186 unsigned int lowest_tCKmin_CL = 0;
189 debug("mclk_ps = %u\n", mclk_ps);
191 for (i = 0; i < num_speed_bins; i++) {
192 unsigned int x = ddr2_speed_bins[i];
193 debug("i=%u, x = %u, lowest_tCKmin_found = %u\n",
194 i, x, lowest_tCKmin_found);
195 if (x && x <= mclk_ps && x >= lowest_tCKmin_found ) {
196 lowest_tCKmin_found = x;
197 lowest_tCKmin_CL = i + 2;
201 debug("lowest_tCKmin_CL = %u\n", lowest_tCKmin_CL);
203 return lowest_tCKmin_CL;
207 * ddr_compute_dimm_parameters for DDR2 SPD
209 * Compute DIMM parameters based upon the SPD information in spd.
210 * Writes the results to the dimm_params_t structure pointed by pdimm.
212 * FIXME: use #define for the retvals
215 ddr_compute_dimm_parameters(const ddr2_spd_eeprom_t *spd,
216 dimm_params_t *pdimm,
217 unsigned int dimm_number)
222 if (spd->mem_type != SPD_MEMTYPE_DDR2) {
223 printf("DIMM %u: is not a DDR2 SPD.\n", dimm_number);
227 memset(pdimm, 0, sizeof(dimm_params_t));
231 retval = ddr2_spd_check(spd);
233 printf("DIMM %u: failed checksum\n", dimm_number);
238 * The part name in ASCII in the SPD EEPROM is not null terminated.
239 * Guarantee null termination here by presetting all bytes to 0
240 * and copying the part name in ASCII from the SPD onto it
242 memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
243 memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
245 /* DIMM organization parameters */
246 pdimm->n_ranks = (spd->mod_ranks & 0x7) + 1;
247 pdimm->rank_density = compute_ranksize(spd->mem_type, spd->rank_dens);
248 pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
249 pdimm->data_width = spd->dataw;
250 pdimm->primary_sdram_width = spd->primw;
251 pdimm->ec_sdram_width = spd->ecw;
253 /* FIXME: what about registered SO-DIMM? */
254 switch (spd->dimm_type) {
255 case 0x01: /* RDIMM */
256 case 0x10: /* Mini-RDIMM */
257 pdimm->registered_dimm = 1; /* register buffered */
260 case 0x02: /* UDIMM */
261 case 0x04: /* SO-DIMM */
262 case 0x08: /* Micro-DIMM */
263 case 0x20: /* Mini-UDIMM */
264 pdimm->registered_dimm = 0; /* unbuffered */
268 printf("unknown dimm_type 0x%02X\n", spd->dimm_type);
273 /* SDRAM device parameters */
274 pdimm->n_row_addr = spd->nrow_addr;
275 pdimm->n_col_addr = spd->ncol_addr;
276 pdimm->n_banks_per_sdram_device = spd->nbanks;
277 pdimm->edc_config = spd->config;
278 pdimm->burst_lengths_bitmask = spd->burstl;
279 pdimm->row_density = spd->rank_dens;
282 * Calculate the Maximum Data Rate based on the Minimum Cycle time.
283 * The SPD clk_cycle field (tCKmin) is measured in tenths of
284 * nanoseconds and represented as BCD.
287 = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle);
288 pdimm->tCKmin_X_minus_1_ps
289 = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle2);
290 pdimm->tCKmin_X_minus_2_ps
291 = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle3);
293 pdimm->tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd->tckmax);
296 * Compute CAS latencies defined by SPD
297 * The SPD caslat_X should have at least 1 and at most 3 bits set.
299 * If cas_lat after masking is 0, the __ilog2 function returns
300 * 255 into the variable. This behavior is abused once.
302 pdimm->caslat_X = __ilog2(spd->cas_lat);
303 pdimm->caslat_X_minus_1 = __ilog2(spd->cas_lat
304 & ~(1 << pdimm->caslat_X));
305 pdimm->caslat_X_minus_2 = __ilog2(spd->cas_lat
306 & ~(1 << pdimm->caslat_X)
307 & ~(1 << pdimm->caslat_X_minus_1));
309 /* Compute CAS latencies below that defined by SPD */
310 pdimm->caslat_lowest_derated
311 = compute_derated_DDR2_CAS_latency(get_memory_clk_period_ps());
313 /* Compute timing parameters */
314 pdimm->tRCD_ps = spd->trcd * 250;
315 pdimm->tRP_ps = spd->trp * 250;
316 pdimm->tRAS_ps = spd->tras * 1000;
318 pdimm->tWR_ps = spd->twr * 250;
319 pdimm->tWTR_ps = spd->twtr * 250;
320 pdimm->tRFC_ps = compute_trfc_ps_from_spd(spd->trctrfc_ext, spd->trfc);
322 pdimm->tRRD_ps = spd->trrd * 250;
323 pdimm->tRC_ps = compute_trc_ps_from_spd(spd->trctrfc_ext, spd->trc);
325 pdimm->refresh_rate_ps = determine_refresh_rate_ps(spd->refresh);
327 pdimm->tIS_ps = convert_bcd_hundredths_to_cycle_time_ps(spd->ca_setup);
328 pdimm->tIH_ps = convert_bcd_hundredths_to_cycle_time_ps(spd->ca_hold);
330 = convert_bcd_hundredths_to_cycle_time_ps(spd->data_setup);
332 = convert_bcd_hundredths_to_cycle_time_ps(spd->data_hold);
334 pdimm->tRTP_ps = spd->trtp * 250;
335 pdimm->tDQSQ_max_ps = spd->tdqsq * 10;
336 pdimm->tQHS_ps = spd->tqhs * 10;