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