Support for the memory controller and PCIe interface of the Intel
[coreboot.git] / src / northbridge / intel / i3100 / raminit_ep80579.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2005 Eric W. Biederman and Tom Zimmerman
5  * Copyright (C) 2008 Arastra, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  */
21
22 #include <cpu/x86/mem.h>
23 #include <cpu/x86/mtrr.h>
24 #include <cpu/x86/cache.h>
25 #include "raminit_ep80579.h"
26 #include "ep80579.h"
27
28 #define BAR 0x90000000
29
30 static void sdram_set_registers(const struct mem_controller *ctrl)
31 {
32         static const u32 register_values[] = {
33                 PCI_ADDR(0, 0x00, 0, CKDIS), 0xffff0000, 0x0000ffff,
34                 PCI_ADDR(0, 0x00, 0, DEVPRES), 0x00000000, 0x07420801 | DEVPRES_CONFIG,
35                 PCI_ADDR(0, 0x00, 0, PAM-1), 0xcccccc7f, 0x33333000,
36                 PCI_ADDR(0, 0x00, 0, PAM+3), 0xcccccccc, 0x33333333,
37                 PCI_ADDR(0, 0x00, 0, DEVPRES1), 0xffffffff, 0x0040003a,
38                 PCI_ADDR(0, 0x00, 0, SMRBASE), 0x00000fff, BAR | 0,  
39         };
40         int i;
41         int max;
42
43         for (i = 0; i < ARRAY_SIZE(register_values); i += 3) {
44                 device_t dev;
45                 u32 where;
46                 u32 reg;
47                 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x00, 0) + ctrl->f0;
48                 where = register_values[i] & 0xff;
49                 reg = pci_read_config32(dev, where);
50                 reg &= register_values[i+1];
51                 reg |= register_values[i+2];
52                 pci_write_config32(dev, where, reg);
53         }
54 }
55
56 struct dimm_size {
57         u32 side1;
58         u32 side2;
59 };
60
61 static struct dimm_size spd_get_dimm_size(u16 device)
62 {
63         /* Calculate the log base 2 size of a DIMM in bits */
64         struct dimm_size sz;
65         int value, low, ddr2;
66         sz.side1 = 0;
67         sz.side2 = 0;
68
69         /* Note it might be easier to use byte 31 here, it has the DIMM size as
70          * a multiple of 4MB.  The way we do it now we can size both
71          * sides of an assymetric dimm.
72          */
73         value = spd_read_byte(device, SPD_NUM_ROWS);
74         if (value < 0) goto hw_err;
75         if ((value & 0xf) == 0) goto val_err;
76         sz.side1 += value & 0xf;
77
78         value = spd_read_byte(device, SPD_NUM_COLUMNS);
79         if (value < 0) goto hw_err;
80         if ((value & 0xf) == 0) goto val_err;
81         sz.side1 += value & 0xf;
82
83         value = spd_read_byte(device, SPD_NUM_BANKS_PER_SDRAM);
84         if (value < 0) goto hw_err;
85         if ((value & 0xff) == 0) goto val_err;
86         sz.side1 += log2(value & 0xff);
87
88         /* Get the module data width and convert it to a power of two */
89         value = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_MSB);
90         if (value < 0) goto hw_err;
91         value &= 0xff;
92         value <<= 8;
93         
94         low = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_LSB);
95         if (low < 0) goto hw_err;
96         value = value | (low & 0xff);
97         if ((value != 72) && (value != 64)) goto val_err;
98         sz.side1 += log2(value);
99
100         /* side 2 */
101         value = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
102
103         if (value < 0) goto hw_err;
104         value &= 7;
105         value++;
106         if (value == 1) goto out;
107         if (value != 2) goto val_err;
108
109         /* Start with the symmetrical case */
110         sz.side2 = sz.side1;
111
112         value = spd_read_byte(device, SPD_NUM_ROWS);
113         if (value < 0) goto hw_err;
114         if ((value & 0xf0) == 0) goto out;      /* If symmetrical we are done */
115         sz.side2 -= (value & 0x0f);             /* Subtract out rows on side 1 */
116         sz.side2 += ((value >> 4) & 0x0f);      /* Add in rows on side 2 */
117
118         value = spd_read_byte(device, SPD_NUM_COLUMNS);
119         if (value < 0) goto hw_err;
120         if ((value & 0xff) == 0) goto val_err;
121         sz.side2 -= (value & 0x0f);             /* Subtract out columns on side 1 */
122         sz.side2 += ((value >> 4) & 0x0f);      /* Add in columns on side 2 */
123         goto out;
124
125  val_err:
126         die("Bad SPD value\r\n");
127         /* If an hw_error occurs report that I have no memory */
128  hw_err:
129         sz.side1 = 0;
130         sz.side2 = 0;
131  out:
132         print_debug("dimm ");
133         print_debug_hex8(device);
134         print_debug(" size = ");
135         print_debug_hex8(sz.side1);
136         print_debug(".");
137         print_debug_hex8(sz.side2);
138         print_debug("\r\n");
139         return sz;
140
141 }
142
143 static long spd_set_ram_size(const struct mem_controller *ctrl, u8 dimm_mask)
144 {
145         int i;
146         int cum;
147         
148         for (i = cum = 0; i < DIMM_SOCKETS; i++) {
149                 struct dimm_size sz;
150                 if (dimm_mask & (1 << i)) {
151                         sz = spd_get_dimm_size(ctrl->channel0[i]);
152                         if (sz.side1 < 29) {
153                                 return -1; /* Report SPD error */
154                         }
155                         /* convert bits to multiples of 64MB */
156                         sz.side1 -= 29;
157                         cum += (1 << sz.side1);
158                         pci_write_config8(ctrl->f0, DRB + (i*2), cum);
159                         pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
160                         if (spd_read_byte(ctrl->channel0[i], SPD_NUM_DIMM_BANKS) & 0x1) {
161                                 cum <<= 1;
162                         }
163                 }
164                 else {
165                         pci_write_config8(ctrl->f0, DRB + (i*2), cum);
166                         pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
167                 }
168         }
169         print_debug("DRB = ");
170         print_debug_hex32(pci_read_config32(ctrl->f0, DRB));
171         print_debug("\r\n");
172
173         cum >>= 1;
174         /* set TOM top of memory */
175         pci_write_config16(ctrl->f0, TOM, cum);
176         print_debug("TOM = ");
177         print_debug_hex16(cum);
178         print_debug("\r\n");
179         /* set TOLM top of low memory */
180         if (cum > 0x18) {
181                 cum = 0x18;
182         }
183         cum <<= 11;
184         pci_write_config16(ctrl->f0, TOLM, cum);
185         print_debug("TOLM = ");
186         print_debug_hex16(cum);
187         print_debug("\r\n");
188         return 0;
189 }
190
191
192 static u8 spd_detect_dimms(const struct mem_controller *ctrl)
193 {
194         u8 dimm_mask = 0;
195         int i;
196         for (i = 0; i < DIMM_SOCKETS; i++) {
197                 int byte;
198                 u16 device;
199                 device = ctrl->channel0[i];
200                 if (device) {
201                         byte = spd_read_byte(device, SPD_MEMORY_TYPE);
202                         print_debug("spd ");
203                         print_debug_hex8(device);
204                         print_debug(" = ");
205                         print_debug_hex8(byte);
206                         print_debug("\r\n");
207                         if (byte == 8) {
208                                 dimm_mask |= (1 << i);
209                         }
210                 }
211         }
212         return dimm_mask;
213 }
214
215
216 static int spd_set_row_attributes(const struct mem_controller *ctrl, 
217                                   u8 dimm_mask)
218 {
219         int value;
220         int i;
221
222         for (i = 0; i < DIMM_SOCKETS; i++) {
223                 u32 dra = 0;
224                 int reg = 0;
225
226                 if (!(dimm_mask & (1 << i))) {
227                         continue;
228                 }
229
230                 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_ROWS);
231                 if (value < 0) die("Bad SPD data\r\n");
232                 if ((value & 0xf) == 0) die("Invalid # of rows\r\n");
233                 dra |= (((value-13) & 0x7) << 23);
234                 dra |= (((value-13) & 0x7) << 29);
235                 reg += value & 0xf;
236
237                 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_COLUMNS);
238                 if (value < 0) die("Bad SPD data\r\n");
239                 if ((value & 0xf) == 0) die("Invalid # of columns\r\n");
240                 dra |= (((value-10) & 0x7) << 20);
241                 dra |= (((value-10) & 0x7) << 26);
242                 reg += value & 0xf;
243
244                 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_BANKS_PER_SDRAM);
245                 if (value < 0) die("Bad SPD data\r\n");
246                 if ((value & 0xff) == 0) die("Invalid # of banks\r\n");
247                 reg += log2(value & 0xff);
248
249                 print_debug("dimm ");
250                 print_debug_hex8(i);
251                 print_debug(" reg = ");
252                 print_debug_hex8(reg);
253                 print_debug("\r\n");
254
255                 /* set device density */
256                 dra |= ((31-reg));
257                 dra |= ((31-reg) << 6);
258
259                 /* set device width (x8) */
260                 dra |= (1 << 4);
261                 dra |= (1 << 10);
262         
263                 /* set device type (registered) */
264                 dra |= (1 << 14);
265
266                 /* set number of ranks (0=single, 1=dual) */
267                 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_DIMM_BANKS);
268                 dra |= ((value & 0x1) << 17);
269
270                 print_debug("DRA");
271                 print_debug_hex8(i);
272                 print_debug(" = ");
273                 print_debug_hex32(dra);
274                 print_debug("\r\n");
275
276                 pci_write_config32(ctrl->f0, DRA + (i*4), dra);
277         }
278         return 0;
279 }
280
281
282 static u32 spd_set_drt_attributes(const struct mem_controller *ctrl, 
283                 u8 dimm_mask, u32 drc)
284 {
285         int i;
286         u32 val, val1;
287         u32 cl;
288         u32 trc = 0;
289         u32 trfc = 0;
290         u32 tras = 0;
291         u32 trtp = 0;
292         u32 twtr = 0;
293         int index = drc & 0x00000003;
294         int ci;
295         static const u8 latencies[] = { /* 533, 800, 400, 667 */
296                 0x10, 0x60, 0x10, 0x20 };
297         static const u32 drt0[] = { /* 533, 800, 400, 667 */
298                 0x24240002, 0x24360002, 0x24220002, 0x24360002 };
299         static const u32 drt1[] = { /* 533, 800, 400, 667 */
300                 0x00400000, 0x00900000, 0x00200000, 0x00700000 };
301         static const u32 magic[] = { /* 533, 800, 400, 667 */
302                 0x007b8221, 0x00b94331, 0x005ca1a1, 0x009a62b1 };
303         static const u32 mrs[] = { /* 533, 800, 400, 667 */
304                 0x07020000, 0x0b020000, 0x05020000, 0x09020000 };
305         static const int cycle[] = { /* 533, 800, 400, 667 */
306                 15, 10, 20, 12 }; /* cycle time in 1/4 ns units */
307         static const int byte40rem[] = {
308                 0, 1, 2, 2, 3, 3, 0, 0 }; /* byte 40 remainder in 1/4 ns units */
309
310         /* CAS latency in cycles */
311         val = latencies[index];
312         for (i = 0; i < DIMM_SOCKETS; i++) {
313                 if (!(dimm_mask & (1 << i)))
314                         continue;
315                 val &= spd_read_byte(ctrl->channel0[i], SPD_ACCEPTABLE_CAS_LATENCIES);
316         }
317         if (val & 0x10)
318                 cl = 4;
319         else if (val & 0x20)
320                 cl = 5;
321         else if (val & 0x40)
322                 cl = 6;
323         else
324                 die("CAS latency mismatch\r\n");
325         print_debug("cl = ");
326         print_debug_hex8(cl);
327         print_debug("\r\n");
328
329         ci = cycle[index];
330
331         /* Trc, Trfc in cycles */
332         for (i = 0; i < DIMM_SOCKETS; i++) {
333                 if (!(dimm_mask & (1 << i)))
334                         continue;
335                 val1 = spd_read_byte(ctrl->channel0[i], SPD_BYTE_41_42_EXTENSION);
336                 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_ACT_TO_ACT_AUTO_REFRESH);
337                 val <<= 2; /* convert to 1/4 ns */
338                 val += byte40rem[(val1 >> 4) & 0x7];
339                 val = (val + ci - 1) / ci + 1; /* convert to cycles */
340                 if (trc < val)
341                         trc = val;
342                 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_AUTO_REFRESH_TO_ACT);
343                 val <<= 2; /* convert to 1/4 ns */
344                 if (val1 & 0x01)
345                         val += 1024;
346                 val += byte40rem[(val1 >> 1) & 0x7];
347                 val = (val + ci - 1) / ci; /* convert to cycles */
348                 if (trfc < val)
349                         trfc = val;
350         }
351         print_debug("trc = ");
352         print_debug_hex8(trc);
353         print_debug("\r\n");
354         print_debug("trfc = ");
355         print_debug_hex8(trfc);
356         print_debug("\r\n");
357
358         /* Tras, Trtp, Twtr in cycles */
359         for (i = 0; i < DIMM_SOCKETS; i++) {
360                 if (!(dimm_mask & (1 << i)))
361                         continue;
362                 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_ACTIVE_TO_PRECHARGE_DELAY);
363                 val <<= 2; /* convert to 1/4 ns */
364                 val = (val + ci - 1) / ci; /* convert to cycles */
365                 if (tras < val)
366                         tras = val;
367                 val = spd_read_byte(ctrl->channel0[i], SPD_INT_READ_TO_PRECHARGE_DELAY);
368                 val = (val + ci - 1) / ci; /* convert to cycles */
369                 if (trtp < val)
370                         trtp = val;
371                 val = spd_read_byte(ctrl->channel0[i], SPD_INT_WRITE_TO_READ_DELAY);
372                 val = (val + ci - 1) / ci; /* convert to cycles */
373                 if (twtr < val)
374                         twtr = val;
375         }
376         print_debug("tras = ");
377         print_debug_hex8(tras);
378         print_debug("\r\n");
379         print_debug("trtp = ");
380         print_debug_hex8(trtp);
381         print_debug("\r\n");
382         print_debug("twtr = ");
383         print_debug_hex8(twtr);
384         print_debug("\r\n");
385
386         val = (drt0[index] | ((trc - 11) << 12) | ((cl - 3) << 9)
387                | ((cl - 3) << 6) | ((cl - 3) << 3));
388         print_debug("drt0 = ");
389         print_debug_hex32(val);
390         print_debug("\r\n");
391         pci_write_config32(ctrl->f0, DRT0, val);
392
393         val = (drt1[index] | ((tras - 8) << 28) | ((trtp - 2) << 25)
394                | (twtr << 15));
395         print_debug("drt1 = ");
396         print_debug_hex32(val);
397         print_debug("\r\n");
398         pci_write_config32(ctrl->f0, DRT1, val);
399
400         val = (magic[index]);
401         print_debug("magic = ");
402         print_debug_hex32(val);
403         print_debug("\r\n");
404         pci_write_config32(PCI_DEV(0, 0x08, 0), 0xcc, val);
405
406         val = (mrs[index] | (cl << 20));
407         print_debug("mrs = ");
408         print_debug_hex32(val);
409         print_debug("\r\n");
410         return val;
411 }
412
413 static int spd_set_dram_controller_mode(const struct mem_controller *ctrl, 
414                                         u8 dimm_mask)
415 {
416         int value;
417         int drc = 0;
418         int i;
419         msr_t msr;
420         u8 cycle = 0x25;
421
422         for (i = 0; i < DIMM_SOCKETS; i++) {
423                 if (!(dimm_mask & (1 << i)))
424                         continue;
425                 if ((spd_read_byte(ctrl->channel0[i], SPD_MODULE_DATA_WIDTH_LSB) & 0xf0) != 0x40)
426                         die("ERROR: Only 64-bit DIMMs supported\r\n");
427                 if (!(spd_read_byte(ctrl->channel0[i], SPD_DIMM_CONFIG_TYPE) & 0x02))
428                         die("ERROR: Only ECC DIMMs supported\r\n");
429                 if (spd_read_byte(ctrl->channel0[i], SPD_PRIMARY_SDRAM_WIDTH) != 0x08)
430                         die("ERROR: Only x8 DIMMs supported\r\n");
431
432                 value = spd_read_byte(ctrl->channel0[i], SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
433                 if (value > cycle)
434                         cycle = value;
435         }
436         print_debug("cycle = ");
437         print_debug_hex8(cycle);
438         print_debug("\r\n");
439
440         drc |= (1 << 20); /* enable ECC */
441         drc |= (3 << 30); /* enable CKE on each DIMM */
442         drc |= (1 << 4); /* enable CKE globally */
443
444         /* TODO check: */
445         /* set front side bus speed */
446         msr = rdmsr(0xcd); /* returns 0 on Pentium M 90nm */
447         print_debug("msr 0xcd = ");
448         print_debug_hex32(msr.hi);
449         print_debug_hex32(msr.lo);
450         print_debug("\r\n");
451
452         /* TODO check that this msr really indicates fsb speed! */
453         if (msr.lo & 0x07) {
454                 print_info("533 MHz FSB\r\n");
455                 if (cycle <= 0x25) {
456                         drc |= 0x5;
457                         print_info("400 MHz DDR\r\n");
458                 } else if (cycle <= 0x30) {
459                         drc |= 0x7;
460                         print_info("333 MHz DDR\r\n");
461                 } else if (cycle <= 0x3d) {
462                         drc |= 0x4;
463                         print_info("266 MHz DDR\r\n");
464                 } else {
465                         drc |= 0x2;
466                         print_info("200 MHz DDR\r\n");
467                 }
468         }
469         else {
470                 print_info("400 MHz FSB\r\n");
471                 if (cycle <= 0x30) {
472                         drc |= 0x7;
473                         print_info("333 MHz DDR\r\n");
474                 } else if (cycle <= 0x3d) {
475                         drc |= 0x0;
476                         print_info("266 MHz DDR\r\n");
477                 } else {
478                         drc |= 0x2;
479                         print_info("200 MHz DDR\r\n");
480                 }
481         }
482
483         print_debug("DRC = ");
484         print_debug_hex32(drc);
485         print_debug("\r\n");
486
487         return drc;
488 }
489
490 static void sdram_set_spd_registers(const struct mem_controller *ctrl) 
491 {
492         u8 dimm_mask;
493         int i;
494
495         /* Test if we can read the SPD */
496         dimm_mask = spd_detect_dimms(ctrl);
497         if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
498                 print_err("No memory for this cpu\r\n");
499                 return;
500         }
501         return;
502 }
503
504 static void set_on_dimm_termination_enable(const struct mem_controller *ctrl)
505 {
506         u8 c1,c2;
507         u32 dimm,i;
508         u32 data32;
509         u32 t4;
510  
511         /* Set up northbridge values */
512         /* ODT enable */
513         pci_write_config32(ctrl->f0, SDRC, 0xa0002c30);
514
515         c1 = pci_read_config8(ctrl->f0, DRB);
516         c2 = pci_read_config8(ctrl->f0, DRB+2);
517         if (c1 == c2) {
518                 /* 1 single-rank DIMM */
519                 data32 = 0x00000010;
520         }
521         else {
522                 /* 2 single-rank DIMMs or 1 double-rank DIMM */
523                 data32 = 0x00002010;
524         }
525
526         print_debug("ODT Value = ");
527         print_debug_hex32(data32);
528         print_debug("\r\n");
529
530         pci_write_config32(ctrl->f0, DDR2ODTC, data32);
531
532         for (i = 0; i < 2; i++) {
533                 print_debug("ODT CS");
534                 print_debug_hex8(i);
535                 print_debug("\r\n");
536
537                 write32(BAR+DCALADDR, 0x0b840001);
538                 write32(BAR+DCALCSR, 0x80000003 | ((i+1)<<21));
539                 data32 = read32(BAR+DCALCSR);
540                 while (data32 & 0x80000000)
541                         data32 = read32(BAR+DCALCSR);
542         }
543 }
544
545
546 static void dump_dcal_regs(void)
547 {
548         int i;
549         for (i = 0x0; i < 0x2a0; i += 4) {
550                 if ((i % 16) == 0) {
551                         print_debug("\r\n");
552                         print_debug_hex16(i);
553                         print_debug(": ");
554                 }
555                 print_debug_hex32(read32(BAR+i));
556                 print_debug(" ");
557         }
558         print_debug("\r\n");
559 }
560
561
562 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
563 {
564         int i;
565         int cs;
566         long mask;
567         u32 drc;
568         u32 data32;
569         u32 mode_reg;
570         msr_t msr;
571         u16 data16;
572
573         mask = spd_detect_dimms(ctrl);
574         print_debug("Starting SDRAM Enable\r\n");
575
576         /* Set DRAM type and Front Side Bus frequency */
577         drc = spd_set_dram_controller_mode(ctrl, mask);
578         if (drc == 0) {
579                 die("Error calculating DRC\r\n");
580         }
581         data32 = drc & ~(3 << 20);  /* clear ECC mode */
582         data32 = data32 | (3 << 5);  /* temp turn off ODT */
583         /* Set DRAM controller mode */
584         pci_write_config32(ctrl->f0, DRC, data32);
585         
586         /* Turn the clocks on */
587         pci_write_config16(ctrl->f0, CKDIS, 0x0000);
588         
589         /* Program row size */
590         spd_set_ram_size(ctrl, mask);
591         
592         /* Program row attributes */
593         spd_set_row_attributes(ctrl, mask);
594
595         /* Program timing values */     
596         mode_reg = spd_set_drt_attributes(ctrl, mask, drc);
597
598         dump_dcal_regs();
599
600         /* Apply NOP */
601         for (cs = 0; cs < 2; cs++) {
602                 print_debug("NOP CS");
603                 print_debug_hex8(cs);
604                 print_debug("\r\n");
605                 udelay(16);
606                 write32(BAR+DCALCSR, (0x00000000 | ((cs+1)<<21)));
607                 write32(BAR+DCALCSR, (0x80000000 | ((cs+1)<<21)));
608                 data32 = read32(BAR+DCALCSR);
609                 while (data32 & 0x80000000)
610                         data32 = read32(BAR+DCALCSR);
611         }
612         
613         /* Apply NOP */
614         udelay(16);
615         for (cs = 0; cs < 2; cs++) {
616                 print_debug("NOP CS");
617                 print_debug_hex8(cs);
618                 print_debug("\r\n");
619                 write32(BAR + DCALCSR, (0x80000000 | ((cs+1)<<21))); 
620                 data32 = read32(BAR+DCALCSR);
621                 while (data32 & 0x80000000)
622                         data32 = read32(BAR+DCALCSR);
623         }
624
625         /* Precharge all banks */
626         udelay(16);
627         for (cs = 0; cs < 2; cs++) {    
628                 print_debug("Precharge CS");
629                 print_debug_hex8(cs);
630                 print_debug("\r\n");
631                 write32(BAR+DCALADDR, 0x04000000);
632                 write32(BAR+DCALCSR, (0x80000002 | ((cs+1)<<21)));
633                 data32 = read32(BAR+DCALCSR);
634                 while (data32 & 0x80000000)
635                         data32 = read32(BAR+DCALCSR);
636         }
637                 
638         /* EMRS: Enable DLLs, set OCD calibration mode to default */
639         udelay(16);
640         for (cs = 0; cs < 2; cs++) {    
641                 print_debug("EMRS CS");
642                 print_debug_hex8(cs);
643                 print_debug("\r\n");
644                 write32(BAR+DCALADDR, 0x0b840001);
645                 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
646                 data32 = read32(BAR+DCALCSR);
647                 while (data32 & 0x80000000)
648                         data32 = read32(BAR+DCALCSR);
649         }
650         /* MRS: Reset DLLs */
651         udelay(16);
652         for (cs = 0; cs < 2; cs++) {    
653                 print_debug("MRS CS");
654                 print_debug_hex8(cs);
655                 print_debug("\r\n");
656                 write32(BAR+DCALADDR, mode_reg);
657                 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
658                 data32 = read32(BAR+DCALCSR);
659                 while (data32 & 0x80000000)
660                         data32 = read32(BAR+DCALCSR);
661         }
662
663         /* Precharge all banks */
664         udelay(48);
665         for (cs = 0; cs < 2; cs++) {    
666                 print_debug("Precharge CS");
667                 print_debug_hex8(cs);
668                 print_debug("\r\n");
669                 write32(BAR+DCALADDR, 0x04000000);
670                 write32(BAR+DCALCSR, (0x80000002 | ((cs+1)<<21)));
671                 data32 = read32(BAR+DCALCSR);
672                 while (data32 & 0x80000000)
673                         data32 = read32(BAR+DCALCSR);
674         }
675         
676         /* Do 2 refreshes */
677         for (i = 0; i < 2; i++) {
678                 udelay(16);
679                 for (cs = 0; cs < 2; cs++) {    
680                         print_debug("Refresh CS");
681                         print_debug_hex8(cs);
682                         print_debug("\r\n");
683                         write32(BAR+DCALCSR, (0x80000001 | ((cs+1)<<21)));
684                         data32 = read32(BAR+DCALCSR);
685                         while (data32 & 0x80000000)
686                                 data32 = read32(BAR+DCALCSR);
687                 }
688         }
689
690         /* MRS: Set DLLs to normal */
691         udelay(16);
692         for (cs = 0; cs < 2; cs++) {    
693                 print_debug("MRS CS");
694                 print_debug_hex8(cs);
695                 print_debug("\r\n");
696                 write32(BAR+DCALADDR, (mode_reg & ~(1<<24)));
697                 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
698                 data32 = read32(BAR+DCALCSR);
699                 while (data32 & 0x80000000)
700                         data32 = read32(BAR+DCALCSR);
701         }
702
703         /* EMRS: Enable DLLs */
704         udelay(16);
705         for (cs = 0; cs < 2; cs++) {
706                 print_debug("EMRS CS");
707                 print_debug_hex8(cs);
708                 print_debug("\r\n");
709                 write32(BAR+DCALADDR, 0x0b840001);
710                 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
711                 data32 = read32(BAR+DCALCSR);
712                 while (data32 & 0x80000000)
713                         data32 = read32(BAR+DCALCSR);
714         }
715
716         udelay(16);
717         /* No command */
718         write32(BAR+DCALCSR, 0x0000000f);
719
720         write32(BAR, 0x00100000);
721
722         /* Enable on-DIMM termination */
723         set_on_dimm_termination_enable(ctrl);
724
725         dump_dcal_regs();
726
727         /* Receive enable calibration */
728         udelay(16);
729         for (cs = 0; cs < 1; cs++) {
730                 print_debug("receive enable calibration CS");
731                 print_debug_hex8(cs);
732                 print_debug("\r\n");
733                 write32(BAR+DCALCSR, (0x8000000c | ((cs+1)<<21)));
734                 data32 = read32(BAR+DCALCSR);
735                 while (data32 & 0x80000000)
736                         data32 = read32(BAR+DCALCSR);
737         }
738         
739         dump_dcal_regs();
740
741         /* Adjust RCOMP */
742         data32 = read32(BAR+DDRIOMC2);
743         data32 &= ~(0xf << 16);
744         data32 |= (0xb << 16);
745         write32(BAR+DDRIOMC2, data32);
746
747         dump_dcal_regs();
748
749         data32 = drc & ~(3 << 20);  /* clear ECC mode */
750         pci_write_config32(ctrl->f0, DRC, data32);      
751         write32(BAR+DCALCSR, 0x0008000f);
752
753         /* Clear memory and init ECC */
754         for (cs = 0; cs < 2; cs++) {    
755                 if (!(mask & (1<<cs)))
756                         continue;
757                 print_debug("clear memory CS");
758                 print_debug_hex8(cs);
759                 print_debug("\r\n");
760                 write32(BAR+MBCSR, 0xa00000f0 | ((cs+1)<<20) | (0<<16));
761                 data32 = read32(BAR+MBCSR);
762                 while (data32 & 0x80000000)
763                         data32 = read32(BAR+MBCSR);
764                 if (data32 & 0x40000000)
765                         print_debug("failed!\r\n");
766         }
767
768         /* Clear read/write FIFO pointers */
769         print_debug("clear read/write fifo pointers\r\n");
770         write32(BAR+DDRIOMC2, read32(BAR+DDRIOMC2) | (1<<15));
771         udelay(16);
772         write32(BAR+DDRIOMC2, read32(BAR+DDRIOMC2) & ~(1<<15));
773         udelay(16);
774
775         dump_dcal_regs();
776
777         print_debug("Done\r\n");
778
779         /* Set initialization complete */
780         drc |= (1 << 29);
781         drc |= (3 << 30);
782         data32 = drc & ~(3 << 20);  /* clear ECC mode */
783         pci_write_config32(ctrl->f0, DRC, data32);      
784
785         /* Set the ECC mode */
786         pci_write_config32(ctrl->f0, DRC, drc); 
787
788         /* The memory is now set up--use it */
789         cache_lbmem(MTRR_TYPE_WRBACK);
790 }
791
792 static inline int memory_initialized(void)
793 {
794         return pci_read_config32(PCI_DEV(0, 0x00, 0), DRC) & (1 << 29);
795 }