1
2
3
4
5
6
7#include <common.h>
8#include <fsl_ddr_sdram.h>
9
10#include <fsl_ddr.h>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34static unsigned long long
35compute_ranksize(unsigned int mem_type, unsigned char row_dens)
36{
37 unsigned long long bsize;
38
39
40 bsize = ((row_dens >> 2) | ((row_dens & 3) << 6));
41 bsize <<= 24ULL;
42 debug("DDR: DDR I rank density = 0x%16llx\n", bsize);
43
44 return bsize;
45}
46
47
48
49
50
51
52
53
54
55
56
57static unsigned int
58convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
59{
60
61 unsigned int tenths_ps[16] = {
62 0,
63 100,
64 200,
65 300,
66 400,
67 500,
68 600,
69 700,
70 800,
71 900,
72 250,
73 330,
74 660,
75 750,
76 0,
77 0
78 };
79
80 unsigned int whole_ns = (spd_val & 0xF0) >> 4;
81 unsigned int tenth_ns = spd_val & 0x0F;
82 unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
83
84 return ps;
85}
86
87static unsigned int
88convert_bcd_hundredths_to_cycle_time_ps(unsigned int spd_val)
89{
90 unsigned int tenth_ns = (spd_val & 0xF0) >> 4;
91 unsigned int hundredth_ns = spd_val & 0x0F;
92 unsigned int ps = tenth_ns * 100 + hundredth_ns * 10;
93
94 return ps;
95}
96
97static unsigned int byte40_table_ps[8] = {
98 0,
99 250,
100 330,
101 500,
102 660,
103 750,
104 0,
105 0
106};
107
108static unsigned int
109compute_trfc_ps_from_spd(unsigned char trctrfc_ext, unsigned char trfc)
110{
111 return ((trctrfc_ext & 0x1) * 256 + trfc) * 1000
112 + byte40_table_ps[(trctrfc_ext >> 1) & 0x7];
113}
114
115static unsigned int
116compute_trc_ps_from_spd(unsigned char trctrfc_ext, unsigned char trc)
117{
118 return trc * 1000 + byte40_table_ps[(trctrfc_ext >> 4) & 0x7];
119}
120
121
122
123
124
125
126
127
128
129
130
131
132
133static unsigned int
134compute_tckmax_from_spd_ps(unsigned int byte43)
135{
136 return (byte43 >> 2) * 1000 + (byte43 & 0x3) * 250;
137}
138
139
140
141
142
143
144static unsigned int
145determine_refresh_rate_ps(const unsigned int spd_refresh)
146{
147 unsigned int refresh_time_ps[8] = {
148 15625000,
149 3900000,
150 7800000,
151 31300000,
152 62500000,
153 125000000,
154 15625000,
155 15625000,
156 };
157
158 return refresh_time_ps[spd_refresh & 0x7];
159}
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186unsigned short ddr1_speed_bins[] = {0, 7500, 6000, 5000 };
187
188unsigned int
189compute_derated_DDR1_CAS_latency(unsigned int mclk_ps)
190{
191 const unsigned int num_speed_bins = ARRAY_SIZE(ddr1_speed_bins);
192 unsigned int lowest_tCKmin_found = 0;
193 unsigned int lowest_tCKmin_CL = 0;
194 unsigned int i;
195
196 debug("mclk_ps = %u\n", mclk_ps);
197
198 for (i = 0; i < num_speed_bins; i++) {
199 unsigned int x = ddr1_speed_bins[i];
200 debug("i=%u, x = %u, lowest_tCKmin_found = %u\n",
201 i, x, lowest_tCKmin_found);
202 if (x && lowest_tCKmin_found <= x && x <= mclk_ps) {
203 lowest_tCKmin_found = x;
204 lowest_tCKmin_CL = i + 1;
205 }
206 }
207
208 debug("lowest_tCKmin_CL = %u\n", lowest_tCKmin_CL);
209
210 return lowest_tCKmin_CL;
211}
212
213
214
215
216
217
218
219
220
221unsigned int ddr_compute_dimm_parameters(const unsigned int ctrl_num,
222 const ddr1_spd_eeprom_t *spd,
223 dimm_params_t *pdimm,
224 unsigned int dimm_number)
225{
226 unsigned int retval;
227
228 if (spd->mem_type) {
229 if (spd->mem_type != SPD_MEMTYPE_DDR) {
230 printf("DIMM %u: is not a DDR1 SPD.\n", dimm_number);
231 return 1;
232 }
233 } else {
234 memset(pdimm, 0, sizeof(dimm_params_t));
235 return 1;
236 }
237
238 retval = ddr1_spd_check(spd);
239 if (retval) {
240 printf("DIMM %u: failed checksum\n", dimm_number);
241 return 2;
242 }
243
244
245
246
247
248
249 memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
250 memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
251
252
253 pdimm->n_ranks = spd->nrows;
254 pdimm->rank_density = compute_ranksize(spd->mem_type, spd->bank_dens);
255 pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
256 pdimm->data_width = spd->dataw_lsb;
257 pdimm->primary_sdram_width = spd->primw;
258 pdimm->ec_sdram_width = spd->ecw;
259
260
261
262
263
264
265 pdimm->registered_dimm = 0;
266
267
268 pdimm->n_row_addr = spd->nrow_addr;
269 pdimm->n_col_addr = spd->ncol_addr;
270 pdimm->n_banks_per_sdram_device = spd->nbanks;
271 pdimm->edc_config = spd->config;
272 pdimm->burst_lengths_bitmask = spd->burstl;
273 pdimm->row_density = spd->bank_dens;
274
275
276
277
278
279
280 pdimm->tckmin_x_ps
281 = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle);
282 pdimm->tckmin_x_minus_1_ps
283 = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle2);
284 pdimm->tckmin_x_minus_2_ps
285 = convert_bcd_tenths_to_cycle_time_ps(spd->clk_cycle3);
286
287 pdimm->tckmax_ps = compute_tckmax_from_spd_ps(spd->tckmax);
288
289
290
291
292
293
294
295
296 pdimm->caslat_x = __ilog2(spd->cas_lat);
297 pdimm->caslat_x_minus_1 = __ilog2(spd->cas_lat
298 & ~(1 << pdimm->caslat_x));
299 pdimm->caslat_x_minus_2 = __ilog2(spd->cas_lat
300 & ~(1 << pdimm->caslat_x)
301 & ~(1 << pdimm->caslat_x_minus_1));
302
303
304 pdimm->caslat_lowest_derated = compute_derated_DDR1_CAS_latency(
305 get_memory_clk_period_ps(ctrl_num));
306
307
308 pdimm->trcd_ps = spd->trcd * 250;
309 pdimm->trp_ps = spd->trp * 250;
310 pdimm->tras_ps = spd->tras * 1000;
311
312 pdimm->twr_ps = mclk_to_picos(ctrl_num, 3);
313 pdimm->twtr_ps = mclk_to_picos(ctrl_num, 1);
314 pdimm->trfc_ps = compute_trfc_ps_from_spd(0, spd->trfc);
315
316 pdimm->trrd_ps = spd->trrd * 250;
317 pdimm->trc_ps = compute_trc_ps_from_spd(0, spd->trc);
318
319 pdimm->refresh_rate_ps = determine_refresh_rate_ps(spd->refresh);
320
321 pdimm->tis_ps = convert_bcd_hundredths_to_cycle_time_ps(spd->ca_setup);
322 pdimm->tih_ps = convert_bcd_hundredths_to_cycle_time_ps(spd->ca_hold);
323 pdimm->tds_ps
324 = convert_bcd_hundredths_to_cycle_time_ps(spd->data_setup);
325 pdimm->tdh_ps
326 = convert_bcd_hundredths_to_cycle_time_ps(spd->data_hold);
327
328 pdimm->trtp_ps = mclk_to_picos(ctrl_num, 2);
329 pdimm->tdqsq_max_ps = spd->tdqsq * 10;
330 pdimm->tqhs_ps = spd->tqhs * 10;
331
332 return 0;
333}
334