e9827c43b99446a004855e002a4c0acddde85139
[coreboot.git] / src / northbridge / intel / e7520 / raminit.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2005 Eric W. Biederman and Tom Zimmerman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  */
20
21 #include <cpu/x86/mtrr.h>
22 #include <cpu/x86/cache.h>
23 #include <stdlib.h>
24 #include "raminit.h"
25 #include "e7520.h"
26 #include <pc80/mc146818rtc.h>
27 #if CONFIG_HAVE_OPTION_TABLE
28 #include "option_table.h"
29 #endif
30
31 #define BAR 0x40000000
32
33 static void sdram_set_registers(const struct mem_controller *ctrl)
34 {
35         static const unsigned int register_values[] = {
36
37                 /* CKDIS 0x8c disable clocks */
38         PCI_ADDR(0, 0x00, 0, CKDIS), 0xffff0000, 0x0000ffff,
39
40                 /* 0x9c Device present and extended RAM control
41                  * DEVPRES is very touchy, hard code the initialization
42                  * of PCI-E ports here.
43                  */
44         PCI_ADDR(0, 0x00, 0, DEVPRES), 0x00000000, 0x07020801 | DEVPRES_CONFIG,
45
46                 /* 0xc8 Remap RAM base and limit off */
47         PCI_ADDR(0, 0x00, 0, REMAPLIMIT), 0x00000000, 0x03df0000,
48
49                 /* ??? */
50         PCI_ADDR(0, 0x00, 0, 0xd8), 0x00000000, 0xb5930000,
51         PCI_ADDR(0, 0x00, 0, 0xe8), 0x00000000, 0x00004a2a,
52
53                 /* 0x50 scrub */
54         PCI_ADDR(0, 0x00, 0, MCHCFG0), 0xfce0ffff, 0x00006000, /* 6000 */
55
56                 /* 0x58 0x5c PAM */
57         PCI_ADDR(0, 0x00, 0, PAM-1), 0xcccccc7f, 0x33333000,
58         PCI_ADDR(0, 0x00, 0, PAM+3), 0xcccccccc, 0x33333333,
59
60                 /* 0xf4 */
61         PCI_ADDR(0, 0x00, 0, DEVPRES1), 0xffbffff, (1<<22)|(6<<2) | DEVPRES1_CONFIG,
62
63                 /* 0x14 */
64         PCI_ADDR(0, 0x00, 0, IURBASE), 0x00000fff, BAR |0,
65         };
66         int i;
67         int max;
68
69         max = ARRAY_SIZE(register_values);
70         for(i = 0; i < max; i += 3) {
71                 device_t dev;
72                 unsigned where;
73                 unsigned long reg;
74                 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x00, 0) + PCI_DEV(0, 0x00, 0);
75                 where = register_values[i] & 0xff;
76                 reg = pci_read_config32(dev, where);
77                 reg &= register_values[i+1];
78                 reg |= register_values[i+2];
79                 pci_write_config32(dev, where, reg);
80         }
81         print_spew("done.\n");
82 }
83
84
85
86 struct dimm_size {
87         unsigned long side1;
88         unsigned long side2;
89 };
90
91 static struct dimm_size spd_get_dimm_size(unsigned device)
92 {
93         /* Calculate the log base 2 size of a DIMM in bits */
94         struct dimm_size sz;
95         int value, low, ddr2;
96         sz.side1 = 0;
97         sz.side2 = 0;
98
99         /* test for ddr2 */
100         ddr2=0;
101         value = spd_read_byte(device, 2);       /* type */
102         if (value < 0) goto hw_err;
103         if (value == 8) ddr2 = 1;
104
105         /* Note it might be easier to use byte 31 here, it has the DIMM size as
106          * a multiple of 4MB.  The way we do it now we can size both
107          * sides of an assymetric dimm.
108          */
109         value = spd_read_byte(device, 3);       /* rows */
110         if (value < 0) goto hw_err;
111         if ((value & 0xf) == 0) goto val_err;
112         sz.side1 += value & 0xf;
113
114         value = spd_read_byte(device, 4);       /* columns */
115         if (value < 0) goto hw_err;
116         if ((value & 0xf) == 0) goto val_err;
117         sz.side1 += value & 0xf;
118
119         value = spd_read_byte(device, 17);      /* banks */
120         if (value < 0) goto hw_err;
121         if ((value & 0xff) == 0) goto val_err;
122         sz.side1 += log2(value & 0xff);
123
124         /* Get the module data width and convert it to a power of two */
125         value = spd_read_byte(device, 7);       /* (high byte) */
126         if (value < 0) goto hw_err;
127         value &= 0xff;
128         value <<= 8;
129
130         low = spd_read_byte(device, 6); /* (low byte) */
131         if (low < 0) goto hw_err;
132         value = value | (low & 0xff);
133         if ((value != 72) && (value != 64)) goto val_err;
134         sz.side1 += log2(value);
135
136         /* side 2 */
137         value = spd_read_byte(device, 5);       /* number of physical banks */
138
139         if (value < 0) goto hw_err;
140         value &= 7;
141         if(ddr2) value++;
142         if (value == 1) goto out;
143         if (value != 2) goto val_err;
144
145         /* Start with the symmetrical case */
146         sz.side2 = sz.side1;
147
148         value = spd_read_byte(device, 3);       /* rows */
149         if (value < 0) goto hw_err;
150         if ((value & 0xf0) == 0) goto out;      /* If symmetrical we are done */
151         sz.side2 -= (value & 0x0f);             /* Subtract out rows on side 1 */
152         sz.side2 += ((value >> 4) & 0x0f);      /* Add in rows on side 2 */
153
154         value = spd_read_byte(device, 4);       /* columns */
155         if (value < 0) goto hw_err;
156         if ((value & 0xff) == 0) goto val_err;
157         sz.side2 -= (value & 0x0f);             /* Subtract out columns on side 1 */
158         sz.side2 += ((value >> 4) & 0x0f);      /* Add in columsn on side 2 */
159         goto out;
160
161  val_err:
162         die("Bad SPD value\n");
163         /* If an hw_error occurs report that I have no memory */
164 hw_err:
165         sz.side1 = 0;
166         sz.side2 = 0;
167  out:
168         return sz;
169
170 }
171
172 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
173 {
174         int i;
175         int cum;
176
177         for(i = cum = 0; i < DIMM_SOCKETS; i++) {
178                 struct dimm_size sz;
179                 if (dimm_mask & (1 << i)) {
180                         sz = spd_get_dimm_size(ctrl->channel0[i]);
181                         if (sz.side1 < 29) {
182                                 return -1; /* Report SPD error */
183                         }
184                         /* convert bits to multiples of 64MB */
185                         sz.side1 -= 29;
186                         cum += (1 << sz.side1);
187                         /* DRB = 0x60 */
188                         pci_write_config8(PCI_DEV(0, 0x00, 0), DRB + (i*2), cum);
189                         if( sz.side2 > 28) {
190                                 sz.side2 -= 29;
191                                 cum += (1 << sz.side2);
192                         }
193                         pci_write_config8(PCI_DEV(0, 0x00, 0), DRB+1 + (i*2), cum);
194                 }
195                 else {
196                         pci_write_config8(PCI_DEV(0, 0x00, 0), DRB + (i*2), cum);
197                         pci_write_config8(PCI_DEV(0, 0x00, 0), DRB+1 + (i*2), cum);
198                 }
199         }
200         /* set TOM top of memory 0xcc */
201         pci_write_config16(PCI_DEV(0, 0x00, 0), TOM, cum);
202         /* set TOLM top of low memory */
203         if(cum > 0x18) {
204                 cum = 0x18;
205         }
206         cum <<= 11;
207         /* 0xc4 TOLM */
208         pci_write_config16(PCI_DEV(0, 0x00, 0), TOLM, cum);
209         return 0;
210 }
211
212
213 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
214 {
215         unsigned dimm_mask;
216         int i;
217         dimm_mask = 0;
218         for(i = 0; i < DIMM_SOCKETS; i++) {
219                 int byte;
220                 unsigned device;
221                 device = ctrl->channel0[i];
222                 if (device) {
223                         byte = spd_read_byte(device, 2);  /* Type */
224                         if ((byte == 7) || (byte == 8)) {
225                                 dimm_mask |= (1 << i);
226                         }
227                 }
228                 device = ctrl->channel1[i];
229                 if (device) {
230                         byte = spd_read_byte(device, 2);
231                         if ((byte == 7) || (byte == 8)) {
232                                 dimm_mask |= (1 << (i + DIMM_SOCKETS));
233                         }
234                 }
235         }
236         return dimm_mask;
237 }
238
239
240 static int spd_set_row_attributes(const struct mem_controller *ctrl,
241                 long dimm_mask)
242 {
243
244         int value;
245         int reg;
246         int dra;
247         int cnt;
248
249         dra = 0;
250         for(cnt=0; cnt < 4; cnt++) {
251                 if (!(dimm_mask & (1 << cnt))) {
252                         continue;
253                 }
254                 reg =0;
255                 value = spd_read_byte(ctrl->channel0[cnt], 3);  /* rows */
256                 if (value < 0) goto hw_err;
257                 if ((value & 0xf) == 0) goto val_err;
258                 reg += value & 0xf;
259
260                 value = spd_read_byte(ctrl->channel0[cnt], 4);  /* columns */
261                 if (value < 0) goto hw_err;
262                 if ((value & 0xf) == 0) goto val_err;
263                 reg += value & 0xf;
264
265                 value = spd_read_byte(ctrl->channel0[cnt], 17); /* banks */
266                 if (value < 0) goto hw_err;
267                 if ((value & 0xff) == 0) goto val_err;
268                 reg += log2(value & 0xff);
269
270                 /* Get the device width and convert it to a power of two */
271                 value = spd_read_byte(ctrl->channel0[cnt], 13);
272                 if (value < 0) goto hw_err;
273                 value = log2(value & 0xff);
274                 reg += value;
275                 if(reg < 27) goto hw_err;
276                 reg -= 27;
277                 reg += (value << 2);
278
279                 dra += reg << (cnt*8);
280                 value = spd_read_byte(ctrl->channel0[cnt], 5);
281                 if (value & 2)
282                         dra += reg << ((cnt*8)+4);
283         }
284
285         /* 0x70 DRA */
286         pci_write_config32(PCI_DEV(0, 0x00, 0), DRA, dra);
287         goto out;
288
289  val_err:
290         die("Bad SPD value\n");
291         /* If an hw_error occurs report that I have no memory */
292 hw_err:
293         dra = 0;
294  out:
295         return dra;
296
297 }
298
299
300 static int spd_set_drt_attributes(const struct mem_controller *ctrl,
301                 long dimm_mask, uint32_t drc)
302 {
303         int value;
304         int reg;
305         uint32_t drt;
306         int cnt;
307         int first_dimm;
308         int cas_latency=0;
309         int latency;
310         uint32_t index = 0;
311         uint32_t index2 = 0;
312         static const unsigned char cycle_time[3] = {0x75,0x60,0x50};
313         static const int latency_indicies[] = { 26, 23, 9 };
314
315         /* 0x78 DRT */
316         drt = pci_read_config32(PCI_DEV(0, 0x00, 0), DRT);
317         drt &= 3;  /* save bits 1:0 */
318
319         for(first_dimm = 0; first_dimm < 4; first_dimm++) {
320                 if (dimm_mask & (1 << first_dimm))
321                         break;
322         }
323
324         /* get dimm type */
325         value = spd_read_byte(ctrl->channel0[first_dimm], 2);
326         if(value == 8) {
327                 drt |= (3<<5); /* back to bark write turn around & cycle add */
328         }
329
330         drt |= (3<<18);  /* Trasmax */
331
332         for(cnt=0; cnt < 4; cnt++) {
333                 if (!(dimm_mask & (1 << cnt))) {
334                         continue;
335                 }
336                 reg = spd_read_byte(ctrl->channel0[cnt], 18); /* CAS Latency */
337                 /* Compute the lowest cas latency supported */
338                 latency = log2(reg) -2;
339
340                 /* Loop through and find a fast clock with a low latency */
341                 for(index = 0; index < 3; index++, latency++) {
342                         if ((latency < 2) || (latency > 4) ||
343                                 (!(reg & (1 << latency)))) {
344                                 continue;
345                         }
346                         value = spd_read_byte(ctrl->channel0[cnt],
347                                         latency_indicies[index]);
348
349                         if(value <= cycle_time[drc&3]) {
350                                 if( latency > cas_latency) {
351                                         cas_latency = latency;
352                                 }
353                                 break;
354                         }
355                 }
356         }
357         index = (cas_latency-2);
358         if((index)==0) cas_latency = 20;
359         else if((index)==1) cas_latency = 25;
360         else cas_latency = 30;
361
362         for(cnt=0;cnt<4;cnt++) {
363                 if (!(dimm_mask & (1 << cnt))) {
364                         continue;
365                 }
366                 reg = spd_read_byte(ctrl->channel0[cnt], 27)&0x0ff;
367                 if(((index>>8)&0x0ff)<reg) {
368                         index &= ~(0x0ff << 8);
369                         index |= (reg << 8);
370                 }
371                 reg = spd_read_byte(ctrl->channel0[cnt], 28)&0x0ff;
372                 if(((index>>16)&0x0ff)<reg) {
373                         index &= ~(0x0ff << 16);
374                         index |= (reg<<16);
375                 }
376                 reg = spd_read_byte(ctrl->channel0[cnt], 29)&0x0ff;
377                 if(((index2>>0)&0x0ff)<reg) {
378                         index2 &= ~(0x0ff << 0);
379                         index2 |= (reg<<0);
380                 }
381                 reg = spd_read_byte(ctrl->channel0[cnt], 41)&0x0ff;
382                 if(((index2>>8)&0x0ff)<reg) {
383                         index2 &= ~(0x0ff << 8);
384                         index2 |= (reg<<8);
385                 }
386                 reg = spd_read_byte(ctrl->channel0[cnt], 42)&0x0ff;
387                 if(((index2>>16)&0x0ff)<reg) {
388                         index2 &= ~(0x0ff << 16);
389                         index2 |= (reg<<16);
390                 }
391         }
392
393         /* get dimm speed */
394         value = cycle_time[drc&3];
395         if(value <= 0x50) {  /* 200 MHz */
396                 if((index&7) > 2) {
397                         drt |= (2<<2);  /* CAS latency 4 */
398                         cas_latency = 40;
399                 } else {
400                         drt |= (1<<2);  /* CAS latency 3 */
401                         cas_latency = 30;
402                 }
403                 if((index&0x0ff00)<=0x03c00) {
404                         drt |= (1<<8);  /* Trp RAS Precharg */
405                 } else {
406                         drt |= (2<<8);  /* Trp RAS Precharg */
407                 }
408
409                 /* Trcd RAS to CAS delay */
410                 if((index2&0x0ff)<=0x03c) {
411                         drt |= (0<<10);
412                 } else {
413                         drt |= (1<<10);
414                 }
415
416                 /* Tdal Write auto precharge recovery delay */
417                 drt |= (1<<12);
418
419                 /* Trc TRS min */
420                 if((index2&0x0ff00)<=0x03700)
421                         drt |= (0<<14);
422                 else if((index2&0xff00)<=0x03c00)
423                         drt |= (1<<14);
424                 else
425                         drt |= (2<<14); /* spd 41 */
426
427                 drt |= (2<<16);  /* Twr not defined for DDR docs say use 2 */
428
429                 /* Trrd Row Delay */
430                 if((index&0x0ff0000)<=0x0140000) {
431                         drt |= (0<<20);
432                 } else if((index&0x0ff0000)<=0x0280000) {
433                         drt |= (1<<20);
434                 } else if((index&0x0ff0000)<=0x03c0000) {
435                         drt |= (2<<20);
436                 } else {
437                         drt |= (3<<20);
438                 }
439
440                 /* Trfc Auto refresh cycle time */
441                 if((index2&0x0ff0000)<=0x04b0000) {
442                         drt |= (0<<22);
443                 } else if((index2&0x0ff0000)<=0x0690000) {
444                         drt |= (1<<22);
445                 } else {
446                         drt |= (2<<22);
447                 }
448                 /* Docs say use 55 for all 200Mhz */
449                 drt |= (0x055<<24);
450         }
451         else if(value <= 0x60) { /* 167 Mhz */
452                 /* according to new documentation CAS latency is 00
453                  * for bits 3:2 for all 167 Mhz
454                 drt |= ((index&3)<<2); */  /* set CAS latency */
455                 if((index&0x0ff00)<=0x03000) {
456                         drt |= (1<<8);  /* Trp RAS Precharg */
457                 } else {
458                         drt |= (2<<8);  /* Trp RAS Precharg */
459                 }
460
461                 /* Trcd RAS to CAS delay */
462                 if((index2&0x0ff)<=0x030) {
463                         drt |= (0<<10);
464                 } else {
465                         drt |= (1<<10);
466                 }
467
468                 /* Tdal Write auto precharge recovery delay */
469                 drt |= (2<<12);
470
471                 /* Trc TRS min */
472                 drt |= (2<<14); /* spd 41, but only one choice */
473
474                 drt |= (2<<16);  /* Twr not defined for DDR docs say 2 */
475
476                 /* Trrd Row Delay */
477                 if((index&0x0ff0000)<=0x0180000) {
478                         drt |= (0<<20);
479                 } else if((index&0x0ff0000)<=0x0300000) {
480                         drt |= (1<<20);
481                 } else {
482                         drt |= (2<<20);
483                 }
484
485                 /* Trfc Auto refresh cycle time */
486                 if((index2&0x0ff0000)<=0x0480000) {
487                         drt |= (0<<22);
488                 } else if((index2&0x0ff0000)<=0x0780000) {
489                         drt |= (2<<22);
490                 } else {
491                         drt |= (2<<22);
492                 }
493                 /* Docs state to use 99 for all 167 Mhz */
494                 drt |= (0x099<<24);
495         }
496         else if(value <= 0x75) { /* 133 Mhz */
497                 drt |= ((index&3)<<2);  /* set CAS latency */
498                 if((index&0x0ff00)<=0x03c00) {
499                         drt |= (1<<8);  /* Trp RAS Precharg */
500                 } else {
501                         drt |= (2<<8);  /* Trp RAS Precharg */
502                 }
503
504                 /* Trcd RAS to CAS delay */
505                 if((index2&0x0ff)<=0x03c) {
506                         drt |= (0<<10);
507                 } else {
508                         drt |= (1<<10);
509                 }
510
511                 /* Tdal Write auto precharge recovery delay */
512                 drt |= (1<<12);
513
514                 /* Trc TRS min */
515                 drt |= (2<<14); /* spd 41, but only one choice */
516
517                 drt |= (1<<16);  /* Twr not defined for DDR docs say 1 */
518
519                 /* Trrd Row Delay */
520                 if((index&0x0ff0000)<=0x01e0000) {
521                         drt |= (0<<20);
522                 } else if((index&0x0ff0000)<=0x03c0000) {
523                         drt |= (1<<20);
524                 } else {
525                         drt |= (2<<20);
526                 }
527
528                 /* Trfc Auto refresh cycle time */
529                 if((index2&0x0ff0000)<=0x04b0000) {
530                         drt |= (0<<22);
531                 } else if((index2&0x0ff0000)<=0x0780000) {
532                         drt |= (2<<22);
533                 } else {
534                         drt |= (2<<22);
535                 }
536
537                 /* Based on CAS latency */
538                 if(index&7)
539                         drt |= (0x099<<24);
540                 else
541                         drt |= (0x055<<24);
542
543         }
544         else {
545                 die("Invalid SPD 9 bus speed.\n");
546         }
547
548         /* 0x78 DRT */
549         pci_write_config32(PCI_DEV(0, 0x00, 0), DRT, drt);
550
551         return(cas_latency);
552 }
553
554 static int spd_set_dram_controller_mode(const struct mem_controller *ctrl,
555                 long dimm_mask)
556 {
557         int value;
558         int reg;
559         int drc;
560         int cnt;
561         msr_t msr;
562         unsigned char dram_type = 0xff;
563         unsigned char ecc = 0xff;
564         unsigned char rate = 62;
565         static const unsigned char spd_rates[6] = {15,3,7,7,62,62};
566         static const unsigned char drc_rates[5] = {0,15,7,62,3};
567         static const unsigned char fsb_conversion[4] = {3,1,3,2};
568
569         /* 0x7c DRC */
570         drc = pci_read_config32(PCI_DEV(0, 0x00, 0), DRC);
571         for(cnt=0; cnt < 4; cnt++) {
572                 if (!(dimm_mask & (1 << cnt))) {
573                         continue;
574                 }
575                 value = spd_read_byte(ctrl->channel0[cnt], 11); /* ECC */
576                 reg = spd_read_byte(ctrl->channel0[cnt], 2); /* Type */
577                 if (value == 2) {    /* RAM is ECC capable */
578                         if (reg == 8) {
579                                 if ( ecc == 0xff ) {
580                                         ecc = 2;
581                                 }
582                                 else if (ecc == 1) {
583                                         die("ERROR - Mixed DDR & DDR2 RAM\n");
584                                 }
585                         }
586                         else if ( reg == 7 ) {
587                                 if ( ecc == 0xff) {
588                                         ecc = 1;
589                                 }
590                                 else if ( ecc > 1 ) {
591                                         die("ERROR - Mixed DDR & DDR2 RAM\n");
592                                 }
593                         }
594                         else {
595                                 die("ERROR - RAM not DDR\n");
596                         }
597                 }
598                 else {
599                         die("ERROR - Non ECC memory dimm\n");
600                 }
601
602                 value = spd_read_byte(ctrl->channel0[cnt], 12); /*refresh rate*/
603                 value &= 0x0f;    /* clip self refresh bit */
604                 if (value > 5) goto hw_err;
605                 if (rate > spd_rates[value])
606                         rate = spd_rates[value];
607
608                 value = spd_read_byte(ctrl->channel0[cnt], 9);  /* cycle time */
609                 if (value > 0x75) goto hw_err;
610                 if (value <= 0x50) {
611                         if (dram_type >= 2) {
612                                 if (reg == 8) { /*speed is good, is this ddr2?*/
613                                         dram_type = 2;
614                                 } else { /* not ddr2 so use ddr333 */
615                                         dram_type = 1;
616                                 }
617                         }
618                 }
619                 else if (value <= 0x60) {
620                         if (dram_type >= 1)  dram_type = 1;
621                 }
622                 else dram_type = 0; /* ddr266 */
623
624         }
625         ecc = 2;
626 #if CONFIG_HAVE_OPTION_TABLE
627         if (read_option(CMOS_VSTART_ECC_memory, CMOS_VLEN_ECC_memory, 1) == 0) {
628                 ecc = 0;  /* ECC off in CMOS so disable it */
629                 print_debug("ECC off\n");
630         } else
631 #endif
632         {
633                 print_debug("ECC on\n");
634         }
635         drc &= ~(3 << 20); /* clear the ecc bits */
636         drc |= (ecc << 20);  /* or in the calculated ecc bits */
637         for ( cnt = 1; cnt < 5; cnt++)
638                 if (drc_rates[cnt] == rate)
639                         break;
640         if (cnt < 5) {
641                 drc &= ~(7 << 8);  /* clear the rate bits */
642                 drc |= (cnt << 8);
643         }
644
645         if (reg == 8) { /* independant clocks */
646                 drc |= (1 << 4);
647         }
648
649         drc |= (1 << 26); /* set the overlap bit - the factory BIOS does */
650         drc |= (1 << 27); /* set DED retry enable - the factory BIOS does */
651         /* front side bus */
652         msr = rdmsr(0x2c);
653         value = msr.lo >> 16;
654         value &= 0x03;
655         drc &= ~(3 << 2); /* set the front side bus */
656         drc |= (fsb_conversion[value] << 2);
657         drc &= ~(3 << 0); /* set the dram type */
658         drc |= (dram_type << 0);
659
660         goto out;
661
662  val_err:
663         die("Bad SPD value\n");
664         /* If an hw_error occurs report that I have no memory */
665 hw_err:
666         drc = 0;
667  out:
668         return drc;
669 }
670
671 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
672 {
673         long dimm_mask;
674
675         /* Test if we can read the spd and if ram is ddr or ddr2 */
676         dimm_mask = spd_detect_dimms(ctrl);
677         if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
678                 print_err("No memory for this cpu\n");
679                 return;
680         }
681         return;
682 }
683
684 static void do_delay(void)
685 {
686         int i;
687         unsigned char b;
688         for(i=0;i<16;i++)
689                 b=inb(0x80);
690 }
691
692 static void pll_setup(uint32_t drc)
693 {
694         unsigned pins;
695         if(drc&3) { /* DDR 333 or DDR 400 */
696                 if((drc&0x0c) == 0x0c) { /* FSB 200 */
697                         pins = 2 | 1;
698                 }
699                 else if((drc&0x0c) == 0x08) {   /* FSB 167 */
700                         pins = 0 | 1;
701                 }
702                 else if(drc&1){  /* FSB 133 DDR 333 */
703                         pins = 2 | 1;
704                 }
705                 else { /* FSB 133 DDR 400 */
706                         pins = 0 | 1;
707                 }
708         }
709         else { /* DDR 266 */
710                 if((drc&0x08) == 0x08) { /* FSB 200 or 167 */
711                         pins = 0 | 0;
712                 }
713                 else { /* FSB 133 */
714                         pins = 0 | 1;
715                 }
716         }
717         mainboard_set_e7520_pll(pins);
718         return;
719 }
720
721 #define TIMEOUT_LOOPS 300000
722
723 #define DCALCSR  0x100
724 #define DCALADDR 0x104
725 #define DCALDATA 0x108
726
727 static void set_on_dimm_termination_enable(const struct mem_controller *ctrl)
728 {
729         unsigned char c1,c2;
730         unsigned int dimm,i;
731         unsigned int data32;
732         unsigned int t4;
733
734         /* Set up northbridge values */
735         /* ODT enable */
736         pci_write_config32(PCI_DEV(0, 0x00, 0), 0x88, 0xf0000180);
737         /* Figure out which slots are Empty, Single, or Double sided */
738         for(i=0,t4=0,c2=0;i<8;i+=2) {
739                 c1 = pci_read_config8(PCI_DEV(0, 0x00, 0), DRB+i);
740                 if(c1 == c2) continue;
741                 c2 = pci_read_config8(PCI_DEV(0, 0x00, 0), DRB+1+i);
742                 if(c1 == c2)
743                         t4 |= (1 << (i*4));
744                 else
745                         t4 |= (2 << (i*4));
746         }
747         for(i=0;i<1;i++) {
748             if((t4&0x0f) == 1) {
749                 if( ((t4>>8)&0x0f) == 0 ) {
750                         data32 = 0x00000010; /* EEES */
751                         break;
752                 }
753                 if ( ((t4>>16)&0x0f) == 0 ) {
754                         data32 = 0x00003132; /* EESS */
755                         break;
756                 }
757                 if ( ((t4>>24)&0x0f)  == 0 ) {
758                         data32 = 0x00335566; /* ESSS */
759                         break;
760                 }
761                 data32 = 0x77bbddee; /* SSSS */
762                 break;
763             }
764             if((t4&0x0f) == 2) {
765                 if( ((t4>>8)&0x0f) == 0 ) {
766                         data32 = 0x00003132; /* EEED */
767                         break;
768                 }
769                 if ( ((t4>>8)&0x0f) == 2 ) {
770                         data32 = 0xb373ecdc; /* EEDD */
771                         break;
772                 }
773                 if ( ((t4>>16)&0x0f) == 0 ) {
774                         data32 = 0x00b3a898; /* EESD */
775                         break;
776                 }
777                 data32 = 0x777becdc; /* ESSD */
778                 break;
779             }
780             die("Error - First dimm slot empty\n");
781         }
782
783         print_debug("ODT Value = ");
784         print_debug_hex32(data32);
785         print_debug("\n");
786
787         pci_write_config32(PCI_DEV(0, 0x00, 0), 0xb0, data32);
788
789         for(dimm=0;dimm<8;dimm+=1) {
790
791                 write32(BAR+DCALADDR, 0x0b840001);
792                 write32(BAR+DCALCSR, 0x83000003 | (dimm << 20));
793
794                 for(i=0;i<1001;i++) {
795                         data32 = read32(BAR+DCALCSR);
796                         if(!(data32 & (1<<31)))
797                                 break;
798                 }
799         }
800 }
801 static void set_receive_enable(const struct mem_controller *ctrl)
802 {
803         unsigned int i;
804         unsigned int cnt;
805         uint32_t recena=0;
806         uint32_t recenb=0;
807
808         {
809         unsigned int dimm;
810         unsigned int edge;
811         int32_t data32;
812         uint32_t data32_dram;
813         uint32_t dcal_data32_0;
814         uint32_t dcal_data32_1;
815         uint32_t dcal_data32_2;
816         uint32_t dcal_data32_3;
817         uint32_t work32l;
818         uint32_t work32h;
819         uint32_t data32r;
820         int32_t recen;
821         for(dimm=0;dimm<8;dimm+=1) {
822
823                 if(!(dimm&1)) {
824                         write32(BAR+DCALDATA+(17*4), 0x04020000);
825                         write32(BAR+DCALCSR, 0x83800004 | (dimm << 20));
826
827                         for(i=0;i<1001;i++) {
828                                 data32 = read32(BAR+DCALCSR);
829                                 if(!(data32 & (1<<31)))
830                                         break;
831                         }
832                         if(i>=1000)
833                                 continue;
834
835                         dcal_data32_0 = read32(BAR+DCALDATA + 0);
836                         dcal_data32_1 = read32(BAR+DCALDATA + 4);
837                         dcal_data32_2 = read32(BAR+DCALDATA + 8);
838                         dcal_data32_3 = read32(BAR+DCALDATA + 12);
839                 }
840                 else {
841                         dcal_data32_0 = read32(BAR+DCALDATA + 16);
842                         dcal_data32_1 = read32(BAR+DCALDATA + 20);
843                         dcal_data32_2 = read32(BAR+DCALDATA + 24);
844                         dcal_data32_3 = read32(BAR+DCALDATA + 28);
845                 }
846
847                 /* check if bank is installed */
848                 if((dcal_data32_0 == 0) && (dcal_data32_2 == 0))
849                         continue;
850                 /* Calculate the timing value */
851                 {
852                 unsigned int bit;
853                 for(i=0,edge=0,bit=63,cnt=31,data32r=0,
854                         work32l=dcal_data32_1,work32h=dcal_data32_3;
855                                 (i<4) && bit; i++) {
856                         for(;;bit--,cnt--) {
857                                 if(work32l & (1<<cnt))
858                                         break;
859                                 if(!cnt) {
860                                         work32l = dcal_data32_0;
861                                         work32h = dcal_data32_2;
862                                         cnt = 32;
863                                 }
864                                 if(!bit) break;
865                         }
866                         for(;;bit--,cnt--) {
867                                 if(!(work32l & (1<<cnt)))
868                                         break;
869                                 if(!cnt) {
870                                         work32l = dcal_data32_0;
871                                         work32h = dcal_data32_2;
872                                         cnt = 32;
873                                 }
874                                 if(!bit) break;
875                         }
876                         if(!bit) {
877                                 break;
878                         }
879                         data32 = ((bit%8) << 1);
880                         if(work32h & (1<<cnt))
881                                 data32 += 1;
882                         if(data32 < 4) {
883                                 if(!edge) {
884                                         edge = 1;
885                                 }
886                                 else {
887                                         if(edge != 1) {
888                                                 data32 = 0x0f;
889                                         }
890                                 }
891                         }
892                         if(data32 > 12) {
893                                 if(!edge) {
894                                         edge = 2;
895                                 }
896                                 else {
897                                         if(edge != 2) {
898                                                 data32 = 0x00;
899                                         }
900                                 }
901                         }
902                         data32r += data32;
903                 }
904                 }
905                 work32l = dcal_data32_0;
906                 work32h = dcal_data32_2;
907                 recen = data32r;
908                 recen += 3;
909                 recen = recen>>2;
910                 for(cnt=5;cnt<24;) {
911                         for(;;cnt++)
912                                 if(!(work32l & (1<<cnt)))
913                                         break;
914                         for(;;cnt++) {
915                                 if(work32l & (1<<cnt))
916                                         break;
917                         }
918                         data32 = (((cnt-1)%8)<<1);
919                         if(work32h & (1<<(cnt-1))) {
920                                 data32++;
921                         }
922                         /* test for frame edge cross overs */
923                         if((edge == 1) && (data32 > 12) &&
924                             (((recen+16)-data32) < 3)) {
925                                 data32 = 0;
926                                 cnt += 2;
927                         }
928                         if((edge == 2) && (data32 < 4) &&
929                             ((recen - data32) > 12))  {
930                                 data32 = 0x0f;
931                                 cnt -= 2;
932                         }
933                         if(((recen+3) >= data32) && ((recen-3) <= data32))
934                                 break;
935                 }
936                 cnt--;
937                 cnt /= 8;
938                 cnt--;
939                 if(recen&1)
940                         recen+=2;
941                 recen >>= 1;
942                 recen += (cnt*8);
943                 recen+=2;      /* this is not in the spec, but matches
944                                  the factory output, and has less failure */
945                 recen <<= (dimm/2) * 8;
946                 if(!(dimm&1)) {
947                         recena |= recen;
948                 }
949                 else {
950                         recenb |= recen;
951                 }
952         }
953         }
954         /* Check for Eratta problem */
955         for(i=cnt=0;i<32;i+=8) {
956                 if (((recena>>i)&0x0f)>7) {
957                         cnt+= 0x101;
958                 }
959                 else {
960                         if((recena>>i)&0x0f) {
961                                 cnt++;
962                         }
963                 }
964         }
965         if(cnt&0x0f00) {
966                 cnt = (cnt&0x0f) - (cnt>>16);
967                 if(cnt>1) {
968                         for(i=0;i<32;i+=8) {
969                                 if(((recena>>i)&0x0f)>7) {
970                                         recena &= ~(0x0f<<i);
971                                         recena |= (7<<i);
972                                 }
973                         }
974                 }
975                 else {
976                         for(i=0;i<32;i+=8) {
977                                 if(((recena>>i)&0x0f)<8) {
978                                         recena &= ~(0x0f<<i);
979                                         recena |= (8<<i);
980                                 }
981                         }
982                 }
983         }
984         for(i=cnt=0;i<32;i+=8) {
985                 if (((recenb>>i)&0x0f)>7) {
986                         cnt+= 0x101;
987                 }
988                 else {
989                         if((recenb>>i)&0x0f) {
990                                 cnt++;
991                         }
992                 }
993         }
994         if(cnt & 0x0f00) {
995                 cnt = (cnt&0x0f) - (cnt>>16);
996                 if(cnt>1) {
997                         for(i=0;i<32;i+=8) {
998                                 if(((recenb>>i)&0x0f)>7) {
999                                         recenb &= ~(0x0f<<i);
1000                                         recenb |= (7<<i);
1001                                 }
1002                         }
1003                 }
1004                 else {
1005                         for(i=0;i<32;i+=8) {
1006                                 if(((recenb>>8)&0x0f)<8) {
1007                                         recenb &= ~(0x0f<<i);
1008                                         recenb |= (8<<i);
1009                                 }
1010                         }
1011                 }
1012         }
1013
1014         print_debug("Receive enable A = ");
1015         print_debug_hex32(recena);
1016         print_debug(",  Receive enable B = ");
1017         print_debug_hex32(recenb);
1018         print_debug("\n");
1019
1020         /* clear out the calibration area */
1021         write32(BAR+DCALDATA+(16*4), 0x00000000);
1022         write32(BAR+DCALDATA+(17*4), 0x00000000);
1023         write32(BAR+DCALDATA+(18*4), 0x00000000);
1024         write32(BAR+DCALDATA+(19*4), 0x00000000);
1025
1026         /* No command */
1027         write32(BAR+DCALCSR, 0x0000000f);
1028
1029         write32(BAR+0x150, recena);
1030         write32(BAR+0x154, recenb);
1031 }
1032
1033
1034 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
1035 {
1036         int i;
1037         int cs;
1038         int cnt;
1039         int cas_latency;
1040         long mask;
1041         uint32_t drc;
1042         uint32_t data32;
1043         uint32_t mode_reg;
1044         uint32_t *iptr;
1045         volatile unsigned long *iptrv;
1046         msr_t msr;
1047         uint32_t scratch;
1048         uint8_t byte;
1049         uint16_t data16;
1050         static const struct {
1051                 uint32_t clkgr[4];
1052         } gearing [] = {
1053                 /* FSB 133 DIMM 266 */
1054         {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1055                 /* FSB 133 DIMM 333 */
1056         {{ 0x00000000, 0x00000000, 0x00000000, 0x00000000}},
1057                 /* FSB 133 DIMM 400 */
1058         {{ 0x00000120, 0x00000000, 0x00000032, 0x00000010}},
1059                 /* FSB 167 DIMM 266 */
1060         {{ 0x00005432, 0x00001000, 0x00004325, 0x00000000}},
1061                 /* FSB 167 DIMM 333 */
1062         {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1063                 /* FSB 167 DIMM 400 */
1064         {{ 0x00154320, 0x00000000, 0x00065432, 0x00010000}},
1065                 /* FSB 200 DIMM 266 */
1066         {{ 0x00000032, 0x00000010, 0x00000120, 0x00000000}},
1067                 /* FSB 200 DIMM 333 */
1068         {{ 0x00065432, 0x00010000, 0x00154320, 0x00000000}},
1069                 /* FSB 200 DIMM 400 */
1070         {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1071         };
1072
1073         static const uint32_t dqs_data[] = {
1074                 0xffffffff, 0xffffffff, 0x000000ff,
1075                 0xffffffff, 0xffffffff, 0x000000ff,
1076                 0xffffffff, 0xffffffff, 0x000000ff,
1077                 0xffffffff, 0xffffffff, 0x000000ff,
1078                 0xffffffff, 0xffffffff, 0x000000ff,
1079                 0xffffffff, 0xffffffff, 0x000000ff,
1080                 0xffffffff, 0xffffffff, 0x000000ff,
1081                 0xffffffff, 0xffffffff, 0x000000ff};
1082
1083         mask = spd_detect_dimms(ctrl);
1084         print_debug("Starting SDRAM Enable\n");
1085
1086         /* 0x80 */
1087 #ifdef DIMM_MAP_LOGICAL
1088         pci_write_config32(PCI_DEV(0, 0x00, 0), DRM,
1089                 0x00210000 | DIMM_MAP_LOGICAL);
1090 #else
1091         pci_write_config32(PCI_DEV(0, 0x00, 0), DRM, 0x00211248);
1092 #endif
1093         /* set dram type and Front Side Bus freq. */
1094         drc = spd_set_dram_controller_mode(ctrl, mask);
1095         if( drc == 0) {
1096                 die("Error calculating DRC\n");
1097         }
1098         pll_setup(drc);
1099         data32 = drc & ~(3 << 20);  /* clear ECC mode */
1100         data32 = data32 & ~(7 << 8);  /* clear refresh rates */
1101         data32 = data32 | (1 << 5);  /* temp turn off of ODT */
1102         /* Set gearing, then dram controller mode */
1103         /* drc bits 1:0 = DIMM speed, bits 3:2 = FSB speed */
1104         for(iptr = gearing[(drc&3)+((((drc>>2)&3)-1)*3)].clkgr,cnt=0;
1105                         cnt<4;cnt++) {
1106                 pci_write_config32(PCI_DEV(0, 0x00, 0), 0xa0+(cnt*4), iptr[cnt]);
1107         }
1108         /* 0x7c DRC */
1109         pci_write_config32(PCI_DEV(0, 0x00, 0), DRC, data32);
1110
1111                 /* turn the clocks on */
1112         /* 0x8c CKDIS */
1113         pci_write_config16(PCI_DEV(0, 0x00, 0), CKDIS, 0x0000);
1114
1115                 /* 0x9a DDRCSR Take subsystem out of idle */
1116         data16 = pci_read_config16(PCI_DEV(0, 0x00, 0), DDRCSR);
1117         data16 &= ~(7 << 12);
1118         data16 |= (3 << 12);   /* use dual channel lock step */
1119         pci_write_config16(PCI_DEV(0, 0x00, 0), DDRCSR, data16);
1120
1121                 /* program row size DRB */
1122         spd_set_ram_size(ctrl, mask);
1123
1124                 /* program page size DRA */
1125         spd_set_row_attributes(ctrl, mask);
1126
1127                 /* program DRT timing values */
1128         cas_latency = spd_set_drt_attributes(ctrl, mask, drc);
1129
1130         for(i=0;i<8;i++) { /* loop throught each dimm to test for row */
1131                 print_debug("DIMM ");
1132                 print_debug_hex8(i);
1133                 print_debug("\n");
1134                 /* Apply NOP */
1135                 do_delay();
1136
1137                 write32(BAR + 0x100, (0x03000000 | (i<<20)));
1138
1139                 write32(BAR+0x100, (0x83000000 | (i<<20)));
1140
1141                 data32 = read32(BAR+DCALCSR);
1142                 while(data32 & 0x80000000)
1143                         data32 = read32(BAR+DCALCSR);
1144
1145         }
1146
1147         /* Apply NOP */
1148         do_delay();
1149
1150         for(cs=0;cs<8;cs++) {
1151                 write32(BAR + DCALCSR, (0x83000000 | (cs<<20)));
1152                 data32 = read32(BAR+DCALCSR);
1153                 while(data32 & 0x80000000)
1154                         data32 = read32(BAR+DCALCSR);
1155         }
1156
1157         /* Precharg all banks */
1158         do_delay();
1159         for(cs=0;cs<8;cs++) {
1160                 if ((drc & 3) == 2) /* DDR2  */
1161                         write32(BAR+DCALADDR, 0x04000000);
1162                 else   /* DDR1  */
1163                         write32(BAR+DCALADDR, 0x00000000);
1164                 write32(BAR+DCALCSR, (0x83000002 | (cs<<20)));
1165                 data32 = read32(BAR+DCALCSR);
1166                 while(data32 & 0x80000000)
1167                         data32 = read32(BAR+DCALCSR);
1168         }
1169
1170         /* EMRS dll's enabled */
1171         do_delay();
1172         for(cs=0;cs<8;cs++) {
1173                 if ((drc & 3) == 2) /* DDR2  */
1174                         /* fixme hard code AL additive latency */
1175                         write32(BAR+DCALADDR, 0x0b940001);
1176                 else   /* DDR1  */
1177                         write32(BAR+DCALADDR, 0x00000001);
1178                 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1179                 data32 = read32(BAR+DCALCSR);
1180                 while(data32 & 0x80000000)
1181                         data32 = read32(BAR+DCALCSR);
1182         }
1183         /* MRS reset dll's */
1184         do_delay();
1185         if ((drc & 3) == 2) {  /* DDR2  */
1186                 if(cas_latency == 30)
1187                         mode_reg = 0x053a0000;
1188                 else
1189                         mode_reg = 0x054a0000;
1190         }
1191         else {  /* DDR1  */
1192                 if(cas_latency == 20)
1193                         mode_reg = 0x012a0000;
1194                 else  /*  CAS Latency 2.5 */
1195                         mode_reg = 0x016a0000;
1196         }
1197         for(cs=0;cs<8;cs++) {
1198                 write32(BAR+DCALADDR, mode_reg);
1199                 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1200                 data32 = read32(BAR+DCALCSR);
1201                 while(data32 & 0x80000000)
1202                         data32 = read32(BAR+DCALCSR);
1203         }
1204
1205         /* Precharg all banks */
1206         do_delay();
1207         do_delay();
1208         do_delay();
1209         for(cs=0;cs<8;cs++) {
1210                 if ((drc & 3) == 2) /* DDR2  */
1211                         write32(BAR+DCALADDR, 0x04000000);
1212                 else   /* DDR1  */
1213                         write32(BAR+DCALADDR, 0x00000000);
1214                 write32(BAR+DCALCSR, (0x83000002 | (cs<<20)));
1215                 data32 = read32(BAR+DCALCSR);
1216                 while(data32 & 0x80000000)
1217                         data32 = read32(BAR+DCALCSR);
1218         }
1219
1220         /* Do 2 refreshes */
1221         do_delay();
1222         for(cs=0;cs<8;cs++) {
1223                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1224                 data32 = read32(BAR+DCALCSR);
1225                 while(data32 & 0x80000000)
1226                         data32 = read32(BAR+DCALCSR);
1227         }
1228         do_delay();
1229         for(cs=0;cs<8;cs++) {
1230                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1231                 data32 = read32(BAR+DCALCSR);
1232                 while(data32 & 0x80000000)
1233                         data32 = read32(BAR+DCALCSR);
1234         }
1235         do_delay();
1236         /* for good luck do 6 more */
1237         for(cs=0;cs<8;cs++) {
1238                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1239         }
1240         do_delay();
1241         for(cs=0;cs<8;cs++) {
1242                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1243         }
1244         do_delay();
1245         for(cs=0;cs<8;cs++) {
1246                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1247         }
1248         do_delay();
1249         for(cs=0;cs<8;cs++) {
1250                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1251         }
1252         do_delay();
1253         for(cs=0;cs<8;cs++) {
1254                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1255         }
1256         do_delay();
1257         for(cs=0;cs<8;cs++) {
1258                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1259         }
1260         do_delay();
1261         /* MRS reset dll's normal */
1262         do_delay();
1263         for(cs=0;cs<8;cs++) {
1264                 write32(BAR+DCALADDR, (mode_reg & ~(1<<24)));
1265                 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1266                 data32 = read32(BAR+DCALCSR);
1267                 while(data32 & 0x80000000)
1268                         data32 = read32(BAR+DCALCSR);
1269         }
1270
1271         /* Do only if DDR2  EMRS dll's enabled */
1272         if ((drc & 3) == 2) { /* DDR2  */
1273                 do_delay();
1274                 for(cs=0;cs<8;cs++) {
1275                         write32(BAR+DCALADDR, (0x0b940001));
1276                         write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1277                         data32 = read32(BAR+DCALCSR);
1278                         while(data32 & 0x80000000)
1279                                 data32 = read32(BAR+DCALCSR);
1280                 }
1281         }
1282
1283         do_delay();
1284         /* No command */
1285         write32(BAR+DCALCSR, 0x0000000f);
1286
1287         /* DDR1 This is test code to copy some codes in the factory setup */
1288
1289         write32(BAR, 0x00100000);
1290
1291         if ((drc & 3) == 2) { /* DDR2  */
1292         /* enable on dimm termination */
1293                 set_on_dimm_termination_enable(ctrl);
1294         }
1295         else { /* ddr */
1296                 pci_write_config32(PCI_DEV(0, 0x00, 0), 0x88, 0xa0000000 );
1297         }
1298
1299         /* receive enable calibration */
1300         set_receive_enable(ctrl);
1301
1302         /* DQS */
1303         pci_write_config32(PCI_DEV(0, 0x00, 0), 0x94, 0x3904a100 );
1304         for(i = 0, cnt = (BAR+0x200); i < 24; i++, cnt+=4) {
1305                 write32(cnt, dqs_data[i]);
1306         }
1307         pci_write_config32(PCI_DEV(0, 0x00, 0), 0x94, 0x3904a100 );
1308
1309         /* Enable refresh */
1310         /* 0x7c DRC */
1311         data32 = drc & ~(3 << 20);  /* clear ECC mode */
1312         pci_write_config32(PCI_DEV(0, 0x00, 0), DRC, data32);
1313         write32(BAR+DCALCSR, 0x0008000f);
1314
1315         /* clear memory and init ECC */
1316         print_debug("Clearing memory\n");
1317         for(i=0;i<64;i+=4) {
1318                 write32(BAR+DCALDATA+i, 0x00000000);
1319         }
1320
1321         for(cs=0;cs<8;cs++) {
1322                 write32(BAR+DCALCSR, (0x830831d8 | (cs<<20)));
1323                 data32 = read32(BAR+DCALCSR);
1324                 while(data32 & 0x80000000)
1325                         data32 = read32(BAR+DCALCSR);
1326         }
1327
1328         /* Bring memory subsystem on line */
1329         data32 = pci_read_config32(PCI_DEV(0, 0x00, 0), 0x98);
1330         data32 |= (1 << 31);
1331         pci_write_config32(PCI_DEV(0, 0x00, 0), 0x98, data32);
1332         /* wait for completion */
1333         print_debug("Waiting for mem complete\n");
1334         while(1) {
1335                 data32 = pci_read_config32(PCI_DEV(0, 0x00, 0), 0x98);
1336                 if( (data32 & (1<<31)) == 0)
1337                         break;
1338         }
1339         print_debug("Done\n");
1340
1341         /* Set initialization complete */
1342         /* 0x7c DRC */
1343         drc |= (1 << 29);
1344         data32 = drc & ~(3 << 20);  /* clear ECC mode */
1345         pci_write_config32(PCI_DEV(0, 0x00, 0), DRC, data32);
1346
1347         /* Set the ecc mode */
1348         pci_write_config32(PCI_DEV(0, 0x00, 0), DRC, drc);
1349
1350         /* Enable memory scrubbing */
1351         /* 0x52 MCHSCRB */
1352         data16 = pci_read_config16(PCI_DEV(0, 0x00, 0), MCHSCRB);
1353         data16 &= ~0x0f;
1354         data16 |= ((2 << 2) | (2 << 0));
1355         pci_write_config16(PCI_DEV(0, 0x00, 0), MCHSCRB, data16);
1356
1357         /* The memory is now setup, use it */
1358         cache_lbmem(MTRR_TYPE_WRBACK);
1359 }