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