1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#include <common.h>
46#include <asm/processor.h>
47#include <i2c.h>
48#include <asm/ppc4xx.h>
49
50#if defined(CONFIG_SPD_EEPROM) && !defined(CONFIG_440)
51
52
53
54
55#ifndef CONFIG_SYS_I2C_SPEED
56#define CONFIG_SYS_I2C_SPEED 50000
57#endif
58
59#define ONE_BILLION 1000000000
60
61#define SDRAM0_CFG_DCE 0x80000000
62#define SDRAM0_CFG_SRE 0x40000000
63#define SDRAM0_CFG_PME 0x20000000
64#define SDRAM0_CFG_MEMCHK 0x10000000
65#define SDRAM0_CFG_REGEN 0x08000000
66#define SDRAM0_CFG_ECCDD 0x00400000
67#define SDRAM0_CFG_EMDULR 0x00200000
68#define SDRAM0_CFG_DRW_SHIFT (31-6)
69#define SDRAM0_CFG_BRPF_SHIFT (31-8)
70
71#define SDRAM0_TR_CASL_SHIFT (31-8)
72#define SDRAM0_TR_PTA_SHIFT (31-13)
73#define SDRAM0_TR_CTP_SHIFT (31-15)
74#define SDRAM0_TR_LDF_SHIFT (31-17)
75#define SDRAM0_TR_RFTA_SHIFT (31-29)
76#define SDRAM0_TR_RCD_SHIFT (31-31)
77
78#define SDRAM0_RTR_SHIFT (31-15)
79#define SDRAM0_ECCCFG_SHIFT (31-11)
80
81
82#define SDRAM0_CFG_BRPF(x) ( ( x & 0x3)<< SDRAM0_CFG_BRPF_SHIFT )
83
84#define SDRAM0_BXCR_SZ_MASK 0x000e0000
85#define SDRAM0_BXCR_AM_MASK 0x0000e000
86
87#define SDRAM0_BXCR_SZ_SHIFT (31-14)
88#define SDRAM0_BXCR_AM_SHIFT (31-18)
89
90#define SDRAM0_BXCR_SZ(x) ( (( x << SDRAM0_BXCR_SZ_SHIFT) & SDRAM0_BXCR_SZ_MASK) )
91#define SDRAM0_BXCR_AM(x) ( (( x << SDRAM0_BXCR_AM_SHIFT) & SDRAM0_BXCR_AM_MASK) )
92
93#ifdef CONFIG_SPDDRAM_SILENT
94# define SPD_ERR(x) do { return 0; } while (0)
95#else
96# define SPD_ERR(x) do { printf(x); return(0); } while (0)
97#endif
98
99#define sdram_HZ_to_ns(hertz) (1000000000/(hertz))
100
101
102int spd_read(uint addr);
103
104
105
106
107
108
109
110
111
112
113
114
115long int spd_sdram(int(read_spd)(uint addr))
116{
117 int tmp,row,col;
118 int total_size,bank_size,bank_code;
119 int mode;
120 int bank_cnt;
121
122 int sdram0_pmit=0x07c00000;
123 int sdram0_b0cr;
124 int sdram0_b1cr = 0;
125#ifndef CONFIG_405EP
126 int sdram0_b2cr = 0;
127 int sdram0_b3cr = 0;
128 int sdram0_besr0 = -1;
129 int sdram0_besr1 = -1;
130 int sdram0_eccesr = -1;
131 int sdram0_ecccfg;
132 int ecc_on;
133#endif
134
135 int sdram0_rtr=0;
136 int sdram0_tr=0;
137
138 int sdram0_cfg=0;
139
140 int t_rp;
141 int t_rcd;
142 int t_ras;
143 int t_rc;
144 int min_cas;
145
146 PPC4xx_SYS_INFO sys_info;
147 unsigned long bus_period_x_10;
148
149
150
151
152 get_sys_info(&sys_info);
153 bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10);
154
155 if (read_spd == 0){
156 read_spd=spd_read;
157
158
159
160
161 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
162 }
163
164
165 if (read_spd(2) != 0x04) {
166 SPD_ERR("SDRAM - non SDRAM memory module found\n");
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 tmp = read_spd(127) & 0x6;
184 if (tmp == 0x02) {
185 min_cas = 2;
186
187
188 } else if (tmp == 0x04) {
189 min_cas = 3;
190
191
192 } else if (tmp == 0x06) {
193 min_cas = 2;
194
195
196 } else {
197 SPD_ERR("SDRAM - unsupported CAS latency \n");
198 }
199
200
201
202 t_rp = read_spd(27);
203 t_rcd = read_spd(29);
204 t_ras = read_spd(30);
205 t_rc = t_ras + t_rp;
206
207
208
209
210
211
212 sdram0_tr = (min_cas - 1) << SDRAM0_TR_CASL_SHIFT;
213
214 sdram0_tr |= ((((t_rp - 1) * 10)/bus_period_x_10) & 0x3) << SDRAM0_TR_PTA_SHIFT;
215
216 tmp = (((t_rc - t_rcd - t_rp -1) * 10) / bus_period_x_10) & 0x3;
217 if (tmp < 1)
218 tmp = 1;
219 sdram0_tr |= tmp << SDRAM0_TR_CTP_SHIFT;
220
221 sdram0_tr |= 1 << SDRAM0_TR_LDF_SHIFT;
222
223 tmp = (((t_rc - 1) * 10) / bus_period_x_10) - 3;
224 if (tmp < 0)
225 tmp = 0;
226 if (tmp > 6)
227 tmp = 6;
228 sdram0_tr |= tmp << SDRAM0_TR_RFTA_SHIFT;
229
230 sdram0_tr |= ((((t_rcd - 1) * 10) / bus_period_x_10) &0x3) << SDRAM0_TR_RCD_SHIFT ;
231
232
233
234
235
236 row = read_spd(3);
237 col = read_spd(4);
238 tmp = read_spd(12) & 0x7f ;
239 switch (tmp) {
240 case 0x00:
241 tmp = 15625;
242 break;
243 case 0x01:
244 tmp = 15625 / 4;
245 break;
246 case 0x02:
247 tmp = 15625 / 2;
248 break;
249 case 0x03:
250 tmp = 15625 * 2;
251 break;
252 case 0x04:
253 tmp = 15625 * 4;
254 break;
255 case 0x05:
256 tmp = 15625 * 8;
257 break;
258 default:
259 SPD_ERR("SDRAM - Bad refresh period \n");
260 }
261
262 tmp = (tmp * 10) / bus_period_x_10;
263 sdram0_rtr = (tmp & 0x3ff8) << SDRAM0_RTR_SHIFT;
264
265
266
267
268
269 if (read_spd(7) != 0)
270 SPD_ERR("SDRAM - unsupported module width\n");
271 tmp = read_spd(6);
272 if (tmp < 32)
273 SPD_ERR("SDRAM - unsupported module width\n");
274 else if (tmp < 64)
275 bank_cnt = 1;
276 else if (tmp < 73)
277 bank_cnt = 2;
278 else if (tmp < 161)
279 bank_cnt = 4;
280 else
281 SPD_ERR("SDRAM - unsupported module width\n");
282
283
284 tmp = read_spd(5);
285 if (tmp == 1)
286 ;
287 else if (tmp==2)
288 bank_cnt *= 2;
289 else if (tmp==4)
290 bank_cnt *= 4;
291 else
292 bank_cnt = 8;
293
294 if (bank_cnt > 4)
295 SPD_ERR("SDRAM - unsupported module rows for this width\n");
296
297#ifndef CONFIG_405EP
298
299
300
301 if ((read_spd(11)==2) && (read_spd(6)==40) && (read_spd(14)==8)) {
302 sdram0_ecccfg = 0xf << SDRAM0_ECCCFG_SHIFT;
303 ecc_on = 1;
304 } else {
305 sdram0_ecccfg = 0;
306 ecc_on = 0;
307 }
308#endif
309
310
311
312
313
314 tmp = read_spd(31);
315 total_size = 1 << 22;
316
317
318 while (((tmp & 0x0001) == 0) && (tmp != 0)) {
319 total_size = total_size << 1;
320 tmp = tmp >> 1;
321 }
322 total_size *= read_spd(5);
323
324
325
326
327
328 switch (row) {
329 case 11:
330 switch (col) {
331 case 8:
332 mode=4;
333 break;
334 case 9:
335 case 10:
336 mode=0;
337 break;
338 default:
339 SPD_ERR("SDRAM - unsupported mode\n");
340 }
341 break;
342 case 12:
343 switch (col) {
344 case 8:
345 mode=3;
346 break;
347 case 9:
348 case 10:
349 mode=1;
350 break;
351 default:
352 SPD_ERR("SDRAM - unsupported mode\n");
353 }
354 break;
355 case 13:
356 switch (col) {
357 case 8:
358 mode=5;
359 break;
360 case 9:
361 case 10:
362 if (read_spd(17) == 2)
363 mode = 6;
364 else
365 mode = 2;
366 break;
367 case 11:
368 mode = 2;
369 break;
370 default:
371 SPD_ERR("SDRAM - unsupported mode\n");
372 }
373 break;
374 default:
375 SPD_ERR("SDRAM - unsupported mode\n");
376 }
377
378
379
380
381
382
383
384 bank_size = total_size / bank_cnt;
385
386
387 tmp = bank_size;
388 bank_code = 0;
389 while (tmp > 1) {
390 bank_code++;
391 tmp = tmp >> 1;
392 }
393 bank_code -= 22;
394
395 tmp = SDRAM0_BXCR_SZ(bank_code) | SDRAM0_BXCR_AM(mode) | 1;
396 sdram0_b0cr = (bank_size * 0) | tmp;
397#ifndef CONFIG_405EP
398 if (bank_cnt > 1)
399 sdram0_b2cr = (bank_size * 1) | tmp;
400 if (bank_cnt > 2)
401 sdram0_b1cr = (bank_size * 2) | tmp;
402 if (bank_cnt > 3)
403 sdram0_b3cr = (bank_size * 3) | tmp;
404#else
405
406 if (bank_cnt > 1)
407 sdram0_b1cr = (bank_size * 1) | tmp;
408 if (bank_cnt > 2)
409 total_size = 2 * bank_size;
410#endif
411
412
413
414
415
416
417
418
419
420
421
422
423
424 mtsdram(SDRAM0_CFG, 0);
425
426#ifndef CONFIG_405EP
427 mtsdram(SDRAM0_BESR0, sdram0_besr0);
428 mtsdram(SDRAM0_BESR1, sdram0_besr1);
429 mtsdram(SDRAM0_ECCCFG, sdram0_ecccfg);
430 mtsdram(SDRAM0_ECCESR, sdram0_eccesr);
431#endif
432 mtsdram(SDRAM0_RTR, sdram0_rtr);
433 mtsdram(SDRAM0_PMIT, sdram0_pmit);
434 mtsdram(SDRAM0_B0CR, sdram0_b0cr);
435 mtsdram(SDRAM0_B1CR, sdram0_b1cr);
436#ifndef CONFIG_405EP
437 mtsdram(SDRAM0_B2CR, sdram0_b2cr);
438 mtsdram(SDRAM0_B3CR, sdram0_b3cr);
439#endif
440 mtsdram(SDRAM0_TR, sdram0_tr);
441
442
443 udelay(500);
444 sdram0_cfg = SDRAM0_CFG_DCE | SDRAM0_CFG_BRPF(1) | SDRAM0_CFG_ECCDD | SDRAM0_CFG_EMDULR;
445#ifndef CONFIG_405EP
446 if (ecc_on)
447 sdram0_cfg |= SDRAM0_CFG_MEMCHK;
448#endif
449 mtsdram(SDRAM0_CFG, sdram0_cfg);
450
451 return (total_size);
452}
453
454int spd_read(uint addr)
455{
456 uchar data[2];
457
458 if (i2c_read(SPD_EEPROM_ADDRESS, addr, 1, data, 1) == 0)
459 return (int)data[0];
460 else
461 return 0;
462}
463
464#endif
465