/src/barebox/common/ddr1_dimm_params.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | /* |
3 | | * Copyright 2008 Freescale Semiconductor, Inc. |
4 | | */ |
5 | | #include <common.h> |
6 | | #include <linux/log2.h> |
7 | | #include <ddr_dimms.h> |
8 | | |
9 | | /* |
10 | | * Calculate the Density of each Physical Rank. |
11 | | * Returned size is in bytes. |
12 | | * |
13 | | * Study these table from Byte 31 of JEDEC SPD Spec. |
14 | | * |
15 | | * DDR I DDR II |
16 | | * Bit Size Size |
17 | | * --- ----- ------ |
18 | | * 7 high 512MB 512MB |
19 | | * 6 256MB 256MB |
20 | | * 5 128MB 128MB |
21 | | * 4 64MB 16GB |
22 | | * 3 32MB 8GB |
23 | | * 2 16MB 4GB |
24 | | * 1 2GB 2GB |
25 | | * 0 low 1GB 1GB |
26 | | * |
27 | | * Reorder Table to be linear by stripping the bottom |
28 | | * 2 or 5 bits off and shifting them up to the top. |
29 | | */ |
30 | | |
31 | | static unsigned long long |
32 | | compute_ranksize(unsigned int mem_type, unsigned char row_dens) |
33 | 0 | { |
34 | 0 | unsigned long long bsize; |
35 | | |
36 | | /* Bottom 2 bits up to the top. */ |
37 | 0 | bsize = ((row_dens >> 2) | ((row_dens & 3) << 6)); |
38 | 0 | bsize <<= 24ULL; |
39 | 0 | debug("DDR: DDR I rank density = 0x%16llx\n", bsize); |
40 | |
|
41 | 0 | return bsize; |
42 | 0 | } |
43 | | |
44 | | /* |
45 | | * Convert a two-nibble BCD value into a cycle time. |
46 | | * While the spec calls for nano-seconds, picos are returned. |
47 | | * |
48 | | * This implements the tables for bytes 9, 23 and 25 for both |
49 | | * DDR I and II. No allowance for distinguishing the invalid |
50 | | * fields absent for DDR I yet present in DDR II is made. |
51 | | * (That is, cycle times of .25, .33, .66 and .75 ns are |
52 | | * allowed for both DDR II and I.) |
53 | | */ |
54 | | static unsigned int |
55 | | convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val) |
56 | 0 | { |
57 | | /* Table look up the lower nibble, allow DDR I & II. */ |
58 | 0 | unsigned int tenths_ps[16] = { |
59 | 0 | 0, |
60 | 0 | 100, |
61 | 0 | 200, |
62 | 0 | 300, |
63 | 0 | 400, |
64 | 0 | 500, |
65 | 0 | 600, |
66 | 0 | 700, |
67 | 0 | 800, |
68 | 0 | 900, |
69 | 0 | 250, /* This and the next 3 entries valid ... */ |
70 | 0 | 330, /* ... only for tCK calculations. */ |
71 | 0 | 660, |
72 | 0 | 750, |
73 | 0 | 0, /* undefined */ |
74 | 0 | 0 /* undefined */ |
75 | 0 | }; |
76 | |
|
77 | 0 | unsigned int whole_ns = (spd_val & 0xF0) >> 4; |
78 | 0 | unsigned int tenth_ns = spd_val & 0x0F; |
79 | 0 | unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns]; |
80 | |
|
81 | 0 | return ps; |
82 | 0 | } |
83 | | |
84 | | static unsigned int |
85 | | convert_bcd_hundredths_to_cycle_time_ps(unsigned int spd_val) |
86 | 0 | { |
87 | 0 | unsigned int tenth_ns = (spd_val & 0xF0) >> 4; |
88 | 0 | unsigned int hundredth_ns = spd_val & 0x0F; |
89 | 0 | unsigned int ps = tenth_ns * 100 + hundredth_ns * 10; |
90 | |
|
91 | 0 | return ps; |
92 | 0 | } |
93 | | |
94 | | static unsigned int byte40_table_ps[8] = { |
95 | | 0, |
96 | | 250, |
97 | | 330, |
98 | | 500, |
99 | | 660, |
100 | | 750, |
101 | | 0, /* supposed to be RFC, but not sure what that means */ |
102 | | 0 /* Undefined */ |
103 | | }; |
104 | | |
105 | | static unsigned int |
106 | | compute_trfc_ps_from_spd(unsigned char trctrfc_ext, unsigned char trfc) |
107 | 0 | { |
108 | 0 | return ((trctrfc_ext & 0x1) * 256 + trfc) * 1000 |
109 | 0 | + byte40_table_ps[(trctrfc_ext >> 1) & 0x7]; |
110 | 0 | } |
111 | | |
112 | | static unsigned int |
113 | | compute_trc_ps_from_spd(unsigned char trctrfc_ext, unsigned char trc) |
114 | 0 | { |
115 | 0 | return trc * 1000 + byte40_table_ps[(trctrfc_ext >> 4) & 0x7]; |
116 | 0 | } |
117 | | |
118 | | /* |
119 | | * tCKmax from DDR I SPD Byte 43 |
120 | | * |
121 | | * Bits 7:2 == whole ns |
122 | | * Bits 1:0 == quarter ns |
123 | | * 00 == 0.00 ns |
124 | | * 01 == 0.25 ns |
125 | | * 10 == 0.50 ns |
126 | | * 11 == 0.75 ns |
127 | | * |
128 | | * Returns picoseconds. |
129 | | */ |
130 | | static unsigned int |
131 | | compute_tckmax_from_spd_ps(unsigned int byte43) |
132 | 0 | { |
133 | 0 | return (byte43 >> 2) * 1000 + (byte43 & 0x3) * 250; |
134 | 0 | } |
135 | | |
136 | | /* |
137 | | * Determine Refresh Rate. Ignore self refresh bit on DDR I. |
138 | | * Table from SPD Spec, Byte 12, converted to picoseconds and |
139 | | * filled in with "default" normal values. |
140 | | */ |
141 | | static unsigned int |
142 | | determine_refresh_rate_ps(const unsigned int spd_refresh) |
143 | 0 | { |
144 | 0 | unsigned int refresh_time_ps[8] = { |
145 | 0 | 15625000, /* 0 Normal 1.00x */ |
146 | 0 | 3900000, /* 1 Reduced .25x */ |
147 | 0 | 7800000, /* 2 Extended .50x */ |
148 | 0 | 31300000, /* 3 Extended 2.00x */ |
149 | 0 | 62500000, /* 4 Extended 4.00x */ |
150 | 0 | 125000000, /* 5 Extended 8.00x */ |
151 | 0 | 15625000, /* 6 Normal 1.00x filler */ |
152 | 0 | 15625000, /* 7 Normal 1.00x filler */ |
153 | 0 | }; |
154 | |
|
155 | 0 | return refresh_time_ps[spd_refresh & 0x7]; |
156 | 0 | } |
157 | | |
158 | | /* |
159 | | * The purpose of this function is to compute a suitable |
160 | | * CAS latency given the DRAM clock period. The SPD only |
161 | | * defines at most 3 CAS latencies. Typically the slower in |
162 | | * frequency the DIMM runs at, the shorter its CAS latency can be. |
163 | | * If the DIMM is operating at a sufficiently low frequency, |
164 | | * it may be able to run at a CAS latency shorter than the |
165 | | * shortest SPD-defined CAS latency. |
166 | | * |
167 | | * If a CAS latency is not found, 0 is returned. |
168 | | * |
169 | | * Do this by finding in the standard speed bin table the longest |
170 | | * tCKmin that doesn't exceed the value of mclk_ps (tCK). |
171 | | * |
172 | | * An assumption made is that the SDRAM device allows the |
173 | | * CL to be programmed for a value that is lower than those |
174 | | * advertised by the SPD. This is not always the case, |
175 | | * as those modes not defined in the SPD are optional. |
176 | | * |
177 | | * CAS latency de-rating based upon values JEDEC Standard No. 79-E |
178 | | * Table 11. |
179 | | * |
180 | | * ordinal 2, ddr1_speed_bins[1] contains tCK for CL=2 |
181 | | */ |
182 | | /* CL2.0 CL2.5 CL3.0 */ |
183 | | unsigned short ddr1_speed_bins[] = {0, 7500, 6000, 5000 }; |
184 | | |
185 | | static unsigned int |
186 | | compute_derated_DDR1_CAS_latency(unsigned int mclk_ps) |
187 | 0 | { |
188 | 0 | const unsigned int num_speed_bins = ARRAY_SIZE(ddr1_speed_bins); |
189 | 0 | unsigned int lowest_tCKmin_found = 0; |
190 | 0 | unsigned int lowest_tCKmin_CL = 0; |
191 | 0 | unsigned int i; |
192 | |
|
193 | 0 | debug("mclk_ps = %u\n", mclk_ps); |
194 | |
|
195 | 0 | for (i = 0; i < num_speed_bins; i++) { |
196 | 0 | unsigned int x = ddr1_speed_bins[i]; |
197 | 0 | debug("i=%u, x = %u, lowest_tCKmin_found = %u\n", |
198 | 0 | i, x, lowest_tCKmin_found); |
199 | 0 | if (x && lowest_tCKmin_found <= x && x <= mclk_ps) { |
200 | 0 | lowest_tCKmin_found = x; |
201 | 0 | lowest_tCKmin_CL = i + 1; |
202 | 0 | } |
203 | 0 | } |
204 | |
|
205 | 0 | debug("lowest_tCKmin_CL = %u\n", lowest_tCKmin_CL); |
206 | |
|
207 | 0 | return lowest_tCKmin_CL; |
208 | 0 | } |
209 | | |
210 | | /* |
211 | | * ddr1_compute_dimm_parameters for DDR1 SPD |
212 | | * |
213 | | * Compute DIMM parameters based upon the SPD information in spd. |
214 | | * Writes the results to the struct dimm_params structure pointed by pdimm. |
215 | | * |
216 | | * FIXME: use #define for the retvals |
217 | | */ |
218 | | unsigned int ddr1_compute_dimm_parameters(unsigned int mclk_ps, |
219 | | const struct ddr1_spd_eeprom *spd, |
220 | | struct dimm_params *pdimm) |
221 | 0 | { |
222 | 0 | int ret; |
223 | |
|
224 | 0 | if (spd->mem_type != SPD_MEMTYPE_DDR) { |
225 | 0 | printf("DIMM: SPD data is not DDR1\n"); |
226 | 0 | return 3; |
227 | 0 | } |
228 | | |
229 | 0 | ret = ddr1_spd_check(spd); |
230 | 0 | if (ret) { |
231 | 0 | printf("DIMM: failed checksum\n"); |
232 | 0 | return 2; |
233 | 0 | } |
234 | | |
235 | | /* |
236 | | * The part name in ASCII in the SPD EEPROM is not null terminated. |
237 | | * Guarantee null termination here by presetting all bytes to 0 |
238 | | * and copying the part name in ASCII from the SPD onto it |
239 | | */ |
240 | 0 | memset(pdimm->mpart, 0, sizeof(pdimm->mpart)); |
241 | 0 | memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1); |
242 | | |
243 | | /* DIMM organization parameters */ |
244 | 0 | pdimm->n_ranks = spd->nrows; |
245 | 0 | pdimm->rank_density = compute_ranksize(spd->mem_type, spd->bank_dens); |
246 | 0 | pdimm->capacity = pdimm->n_ranks * pdimm->rank_density; |
247 | 0 | pdimm->data_width = spd->dataw_lsb; |
248 | 0 | pdimm->primary_sdram_width = spd->primw; |
249 | 0 | pdimm->ec_sdram_width = spd->ecw; |
250 | | |
251 | | /* |
252 | | * FIXME: Need to determine registered_dimm status. |
253 | | * 1 == register buffered |
254 | | * 0 == unbuffered |
255 | | */ |
256 | 0 | pdimm->registered_dimm = 0; /* unbuffered */ |
257 | | |
258 | | /* SDRAM device parameters */ |
259 | 0 | pdimm->n_row_addr = spd->nrow_addr; |
260 | 0 | pdimm->n_col_addr = spd->ncol_addr; |
261 | 0 | pdimm->n_banks_per_sdram_device = spd->nbanks; |
262 | 0 | pdimm->edc_config = spd->config; |
263 | 0 | pdimm->burst_lengths_bitmask = spd->burstl; |
264 | | |
265 | | /* |
266 | | * Calculate the Maximum Data Rate based on the Minimum Cycle time. |
267 | | * The SPD clk_cycle field (tCKmin) is measured in tenths of |
268 | | * nanoseconds and represented as BCD. |
269 | | */ |
270 | 0 | pdimm->tckmin_x_ps |
271 | 0 | = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle); |
272 | 0 | pdimm->tckmin_x_minus_1_ps |
273 | 0 | = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle2); |
274 | 0 | pdimm->tckmin_x_minus_2_ps |
275 | 0 | = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle3); |
276 | |
|
277 | 0 | pdimm->tckmax_ps = compute_tckmax_from_spd_ps(spd->tckmax); |
278 | | |
279 | | /* |
280 | | * Compute CAS latencies defined by SPD |
281 | | * The SPD caslat_x should have at least 1 and at most 3 bits set. |
282 | | * |
283 | | * If cas_lat after masking is 0, the __ilog2 function returns |
284 | | * 255 into the variable. This behavior is abused once. |
285 | | */ |
286 | 0 | pdimm->caslat_x = ilog2(spd->cas_lat); |
287 | 0 | pdimm->caslat_x_minus_1 = ilog2(spd->cas_lat |
288 | 0 | & ~(1 << pdimm->caslat_x)); |
289 | 0 | pdimm->caslat_x_minus_2 = ilog2(spd->cas_lat |
290 | 0 | & ~(1 << pdimm->caslat_x) |
291 | 0 | & ~(1 << pdimm->caslat_x_minus_1)); |
292 | | |
293 | | /* Compute CAS latencies below that defined by SPD */ |
294 | 0 | pdimm->caslat_lowest_derated = compute_derated_DDR1_CAS_latency( |
295 | 0 | mclk_ps); |
296 | | |
297 | | /* Compute timing parameters */ |
298 | 0 | pdimm->trcd_ps = spd->trcd * 250; |
299 | 0 | pdimm->trp_ps = spd->trp * 250; |
300 | 0 | pdimm->tras_ps = spd->tras * 1000; |
301 | |
|
302 | 0 | pdimm->twr_ps = mclk_ps * 3; |
303 | 0 | pdimm->twtr_ps = mclk_ps * 1; |
304 | 0 | pdimm->trfc_ps = compute_trfc_ps_from_spd(0, spd->trfc); |
305 | |
|
306 | 0 | pdimm->trrd_ps = spd->trrd * 250; |
307 | 0 | pdimm->trc_ps = compute_trc_ps_from_spd(0, spd->trc); |
308 | |
|
309 | 0 | pdimm->refresh_rate_ps = determine_refresh_rate_ps(spd->refresh); |
310 | |
|
311 | 0 | pdimm->tis_ps = convert_bcd_hundredths_to_cycle_time_ps(spd->ca_setup); |
312 | 0 | pdimm->tih_ps = convert_bcd_hundredths_to_cycle_time_ps(spd->ca_hold); |
313 | 0 | pdimm->tds_ps |
314 | 0 | = convert_bcd_hundredths_to_cycle_time_ps(spd->data_setup); |
315 | 0 | pdimm->tdh_ps |
316 | 0 | = convert_bcd_hundredths_to_cycle_time_ps(spd->data_hold); |
317 | |
|
318 | 0 | pdimm->trtp_ps = mclk_ps * 2; /* By the book. */ |
319 | 0 | pdimm->tdqsq_max_ps = spd->tdqsq * 10; |
320 | 0 | pdimm->tqhs_ps = spd->tqhs * 10; |
321 | |
|
322 | 0 | return 0; |
323 | 0 | } |