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