Move C labels to start-of-line
[coreboot.git] / src / northbridge / intel / e7525 / 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 "e7525.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) + ctrl->f0;
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(ctrl->f0, DRB + (i*2), cum);
189                         if( sz.side2 > 28) {
190                                 sz.side2 -= 29;
191                                 cum += (1 << sz.side2);
192                         }
193                         pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
194                 }
195                 else {
196                         pci_write_config8(ctrl->f0, DRB + (i*2), cum);
197                         pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
198                 }
199         }
200         /* set TOM top of memory 0xcc */
201         pci_write_config16(ctrl->f0, 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(ctrl->f0, 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(ctrl->f0, 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(ctrl->f0, 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(ctrl->f0, 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(ctrl->f0, 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(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 #define TIMEOUT_LOOPS 300000
693
694 #define DCALCSR  0x100
695 #define DCALADDR 0x104
696 #define DCALDATA 0x108
697
698 static void set_on_dimm_termination_enable(const struct mem_controller *ctrl)
699 {
700         unsigned char c1,c2;
701         unsigned int dimm,i;
702         unsigned int data32;
703         unsigned int t4;
704
705         /* Set up northbridge values */
706         /* ODT enable */
707         pci_write_config32(ctrl->f0, 0x88, 0xf0000180);
708         /* Figure out which slots are Empty, Single, or Double sided */
709         for(i=0,t4=0,c2=0;i<8;i+=2) {
710                 c1 = pci_read_config8(ctrl->f0, DRB+i);
711                 if(c1 == c2) continue;
712                 c2 = pci_read_config8(ctrl->f0, DRB+1+i);
713                 if(c1 == c2)
714                         t4 |= (1 << (i*4));
715                 else
716                         t4 |= (2 << (i*4));
717         }
718         for(i=0;i<1;i++) {
719             if((t4&0x0f) == 1) {
720                 if( ((t4>>8)&0x0f) == 0 ) {
721                         data32 = 0x00000010; /* EEES */
722                         break;
723                 }
724                 if ( ((t4>>16)&0x0f) == 0 ) {
725                         data32 = 0x00003132; /* EESS */
726                         break;
727                 }
728                 if ( ((t4>>24)&0x0f)  == 0 ) {
729                         data32 = 0x00335566; /* ESSS */
730                         break;
731                 }
732                 data32 = 0x77bbddee; /* SSSS */
733                 break;
734             }
735             if((t4&0x0f) == 2) {
736                 if( ((t4>>8)&0x0f) == 0 ) {
737                         data32 = 0x00003132; /* EEED */
738                         break;
739                 }
740                 if ( ((t4>>8)&0x0f) == 2 ) {
741                         data32 = 0xb373ecdc; /* EEDD */
742                         break;
743                 }
744                 if ( ((t4>>16)&0x0f) == 0 ) {
745                         data32 = 0x00b3a898; /* EESD */
746                         break;
747                 }
748                 data32 = 0x777becdc; /* ESSD */
749                 break;
750             }
751             die("Error - First dimm slot empty\n");
752         }
753
754         print_debug("ODT Value = ");
755         print_debug_hex32(data32);
756         print_debug("\n");
757
758         pci_write_config32(ctrl->f0, 0xb0, data32);
759
760         for(dimm=0;dimm<8;dimm+=1) {
761
762                 write32(BAR+DCALADDR, 0x0b840001);
763                 write32(BAR+DCALCSR, 0x83000003 | (dimm << 20));
764
765                 for(i=0;i<1001;i++) {
766                         data32 = read32(BAR+DCALCSR);
767                         if(!(data32 & (1<<31)))
768                                 break;
769                 }
770         }
771 }
772 static void set_receive_enable(const struct mem_controller *ctrl)
773 {
774         unsigned int i;
775         unsigned int cnt,bit;
776         uint32_t recena=0;
777         uint32_t recenb=0;
778
779         {
780         unsigned int dimm;
781         unsigned int edge;
782         int32_t data32;
783         uint32_t data32_dram;
784         uint32_t dcal_data32_0;
785         uint32_t dcal_data32_1;
786         uint32_t dcal_data32_2;
787         uint32_t dcal_data32_3;
788         uint32_t work32l;
789         uint32_t work32h;
790         uint32_t data32r;
791         int32_t recen;
792         for(dimm=0;dimm<8;dimm+=1) {
793
794                 if(!(dimm&1)) {
795                         write32(BAR+DCALDATA+(17*4), 0x04020000);
796                         write32(BAR+DCALCSR, 0x83800004 | (dimm << 20));
797
798                         for(i=0;i<1001;i++) {
799                                 data32 = read32(BAR+DCALCSR);
800                                 if(!(data32 & (1<<31)))
801                                         break;
802                         }
803                         if(i>=1000)
804                                 continue;
805
806                         dcal_data32_0 = read32(BAR+DCALDATA + 0);
807                         dcal_data32_1 = read32(BAR+DCALDATA + 4);
808                         dcal_data32_2 = read32(BAR+DCALDATA + 8);
809                         dcal_data32_3 = read32(BAR+DCALDATA + 12);
810                 }
811                 else {
812                         dcal_data32_0 = read32(BAR+DCALDATA + 16);
813                         dcal_data32_1 = read32(BAR+DCALDATA + 20);
814                         dcal_data32_2 = read32(BAR+DCALDATA + 24);
815                         dcal_data32_3 = read32(BAR+DCALDATA + 28);
816                 }
817
818                 /* check if bank is installed */
819                 if((dcal_data32_0 == 0) && (dcal_data32_2 == 0))
820                         continue;
821                 /* Calculate the timing value */
822                 for(i=0,edge=0,bit=63,cnt=31,data32r=0,
823                         work32l=dcal_data32_1,work32h=dcal_data32_3;
824                                 (i<4) && bit; i++) {
825                         for(;;bit--,cnt--) {
826                                 if(work32l & (1<<cnt))
827                                         break;
828                                 if(!cnt) {
829                                         work32l = dcal_data32_0;
830                                         work32h = dcal_data32_2;
831                                         cnt = 32;
832                                 }
833                                 if(!bit) break;
834                         }
835                         for(;;bit--,cnt--) {
836                                 if(!(work32l & (1<<cnt)))
837                                         break;
838                                 if(!cnt) {
839                                         work32l = dcal_data32_0;
840                                         work32h = dcal_data32_2;
841                                         cnt = 32;
842                                 }
843                                 if(!bit) break;
844                         }
845                         if(!bit) {
846                                 break;
847                         }
848                         data32 = ((bit%8) << 1);
849                         if(work32h & (1<<cnt))
850                                 data32 += 1;
851                         if(data32 < 4) {
852                                 if(!edge) {
853                                         edge = 1;
854                                 }
855                                 else {
856                                         if(edge != 1) {
857                                                 data32 = 0x0f;
858                                         }
859                                 }
860                         }
861                         if(data32 > 12) {
862                                 if(!edge) {
863                                         edge = 2;
864                                 }
865                                 else {
866                                         if(edge != 2) {
867                                                 data32 = 0x00;
868                                         }
869                                 }
870                         }
871                         data32r += data32;
872                 }
873
874                 work32l = dcal_data32_0;
875                 work32h = dcal_data32_2;
876                 recen = data32r;
877                 recen += 3;
878                 recen = recen>>2;
879                 for(cnt=5;cnt<24;) {
880                         for(;;cnt++)
881                                 if(!(work32l & (1<<cnt)))
882                                         break;
883                         for(;;cnt++) {
884                                 if(work32l & (1<<cnt))
885                                         break;
886                         }
887                         data32 = (((cnt-1)%8)<<1);
888                         if(work32h & (1<<(cnt-1))) {
889                                 data32++;
890                         }
891                         /* test for frame edge cross overs */
892                         if((edge == 1) && (data32 > 12) &&
893                             (((recen+16)-data32) < 3)) {
894                                 data32 = 0;
895                                 cnt += 2;
896                         }
897                         if((edge == 2) && (data32 < 4) &&
898                             ((recen - data32) > 12))  {
899                                 data32 = 0x0f;
900                                 cnt -= 2;
901                         }
902                         if(((recen+3) >= data32) && ((recen-3) <= data32))
903                                 break;
904                 }
905                 cnt--;
906                 cnt /= 8;
907                 cnt--;
908                 if(recen&1)
909                         recen+=2;
910                 recen >>= 1;
911                 recen += (cnt*8);
912         recen+=2;
913                 recen <<= (dimm/2) * 8;
914                 if(!(dimm&1)) {
915                         recena |= recen;
916                 }
917                 else {
918                         recenb |= recen;
919                 }
920         }
921         }
922         /* Check for Eratta problem */
923         for(i=cnt=bit=0;i<4;i++) {
924                 if (((recena>>(i*8))&0x0f)>7) {
925                         cnt++; bit++;
926                 }
927                 else {
928                         if((recena>>(i*8))&0x0f) {
929                                 cnt++;
930                         }
931                 }
932         }
933         if(bit) {
934                 cnt-=bit;
935                 if(cnt>1) {
936                         for(i=0;i<4;i++) {
937                                 if(((recena>>(i*8))&0x0f)>7) {
938                                         recena &= ~(0x0f<<(i*8));
939                                         recena |= (7<<(i*8));
940                                 }
941                         }
942                 }
943                 else {
944                         for(i=0;i<4;i++) {
945                                 if(((recena>>(i*8))&0x0f)<8) {
946                                         recena &= ~(0x0f<<(i*8));
947                                         recena |= (8<<(i*8));
948                                 }
949                         }
950                 }
951         }
952         for(i=cnt=bit=0;i<4;i++) {
953                 if (((recenb>>(i*8))&0x0f)>7) {
954                         cnt++; bit++;
955                 }
956                 else {
957                         if((recenb>>(i*8))&0x0f) {
958                                 cnt++;
959                         }
960                 }
961         }
962         if(bit) {
963                 cnt-=bit;
964                 if(cnt>1) {
965                         for(i=0;i<4;i++) {
966                                 if(((recenb>>(i*8))&0x0f)>7) {
967                                         recenb &= ~(0x0f<<(i*8));
968                                         recenb |= (7<<(i*8));
969                                 }
970                         }
971                 }
972                 else {
973                         for(i=0;i<4;i++) {
974                                 if(((recenb>>(i*8))&0x0f)<8) {
975                                         recenb &= ~(0x0f<<(i*8));
976                                         recenb |= (8<<(i*8));
977                                 }
978                         }
979                 }
980         }
981
982 //  recena = 0x0000090a;
983 //  recenb = 0x0000090a;
984
985         print_debug("Receive enable A = ");
986         print_debug_hex32(recena);
987         print_debug(",  Receive enable B = ");
988         print_debug_hex32(recenb);
989         print_debug("\n");
990
991         /* clear out the calibration area */
992         write32(BAR+DCALDATA+(16*4), 0x00000000);
993         write32(BAR+DCALDATA+(17*4), 0x00000000);
994         write32(BAR+DCALDATA+(18*4), 0x00000000);
995         write32(BAR+DCALDATA+(19*4), 0x00000000);
996
997         /* No command */
998         write32(BAR+DCALCSR, 0x0000000f);
999
1000         write32(BAR+0x150, recena);
1001         write32(BAR+0x154, recenb);
1002 }
1003
1004
1005 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
1006 {
1007         int i;
1008         int cs;
1009         int cnt;
1010         int cas_latency;
1011         long mask;
1012         uint32_t drc;
1013         uint32_t data32;
1014         uint32_t mode_reg;
1015         uint32_t *iptr;
1016         volatile unsigned long *iptrv;
1017         msr_t msr;
1018         uint32_t scratch;
1019         uint8_t byte;
1020         uint16_t data16;
1021         static const struct {
1022                 uint32_t clkgr[4];
1023         } gearing [] = {
1024                 /* FSB 133 DIMM 266 */
1025         {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1026                 /* FSB 133 DIMM 333 */
1027         {{ 0x00000000, 0x00000000, 0x00000000, 0x00000000}},
1028                 /* FSB 133 DIMM 400 */
1029         {{ 0x00000120, 0x00000000, 0x00000032, 0x00000010}},
1030                 /* FSB 167 DIMM 266 */
1031         {{ 0x00005432, 0x00001000, 0x00004325, 0x00000000}},
1032                 /* FSB 167 DIMM 333 */
1033         {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1034                 /* FSB 167 DIMM 400 */
1035         {{ 0x00154320, 0x00000000, 0x00065432, 0x00010000}},
1036                 /* FSB 200 DIMM 266 */
1037         {{ 0x00000032, 0x00000010, 0x00000120, 0x00000000}},
1038                 /* FSB 200 DIMM 333 */
1039         {{ 0x00065432, 0x00010000, 0x00054326, 0x00000000}},
1040                 /* FSB 200 DIMM 400 */
1041         {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1042         };
1043
1044         static const uint32_t dqs_data[] = {
1045                 0xffffffff, 0xffffffff, 0x000000ff,
1046                 0xffffffff, 0xffffffff, 0x000000ff,
1047                 0xffffffff, 0xffffffff, 0x000000ff,
1048                 0xffffffff, 0xffffffff, 0x000000ff,
1049                 0xffffffff, 0xffffffff, 0x000000ff,
1050                 0xffffffff, 0xffffffff, 0x000000ff,
1051                 0xffffffff, 0xffffffff, 0x000000ff,
1052                 0xffffffff, 0xffffffff, 0x000000ff};
1053
1054         mask = spd_detect_dimms(ctrl);
1055         print_debug("Starting SDRAM Enable\n");
1056
1057         /* 0x80 */
1058         pci_write_config32(ctrl->f0, DRM,
1059                 0x00210000 | CONFIG_DIMM_MAP_LOGICAL);
1060         /* set dram type and Front Side Bus freq. */
1061         drc = spd_set_dram_controller_mode(ctrl, mask);
1062         if( drc == 0) {
1063                 die("Error calculating DRC\n");
1064         }
1065         data32 = drc & ~(3 << 20);  /* clear ECC mode */
1066         data32 = data32 & ~(7 << 8);  /* clear refresh rates */
1067         data32 = data32 | (1 << 5);  /* temp turn off of ODT */
1068         /* Set gearing, then dram controller mode */
1069         /* drc bits 1:0 = DIMM speed, bits 3:2 = FSB speed */
1070         for(iptr = gearing[(drc&3)+((((drc>>2)&3)-1)*3)].clkgr,cnt=0;
1071                         cnt<4;cnt++) {
1072                 pci_write_config32(ctrl->f0, 0xa0+(cnt*4), iptr[cnt]);
1073         }
1074         /* 0x7c DRC */
1075         pci_write_config32(ctrl->f0, DRC, data32);
1076
1077                 /* turn the clocks on */
1078         /* 0x8c CKDIS */
1079         pci_write_config16(ctrl->f0, CKDIS, 0x0000);
1080
1081                 /* 0x9a DDRCSR Take subsystem out of idle */
1082         data16 = pci_read_config16(ctrl->f0, DDRCSR);
1083         data16 &= ~(7 << 12);
1084         data16 |= (3 << 12);   /* use dual channel lock step */
1085         pci_write_config16(ctrl->f0, DDRCSR, data16);
1086
1087                 /* program row size DRB */
1088         spd_set_ram_size(ctrl, mask);
1089
1090                 /* program page size DRA */
1091         spd_set_row_attributes(ctrl, mask);
1092
1093                 /* program DRT timing values */
1094         cas_latency = spd_set_drt_attributes(ctrl, mask, drc);
1095
1096         for(i=0;i<8;i++) { /* loop throught each dimm to test for row */
1097                 print_debug("DIMM ");
1098                 print_debug_hex8(i);
1099                 print_debug("\n");
1100                 /* Apply NOP */
1101                 do_delay();
1102
1103                 write32(BAR + 0x100, (0x03000000 | (i<<20)));
1104
1105                 write32(BAR+0x100, (0x83000000 | (i<<20)));
1106
1107                 do data32 = read32(BAR+DCALCSR);
1108                 while(data32 & 0x80000000);
1109
1110         }
1111
1112         /* Apply NOP */
1113         do_delay();
1114
1115         for(cs=0;cs<8;cs++) {
1116                 write32(BAR + DCALCSR, (0x83000000 | (cs<<20)));
1117                 do data32 = read32(BAR+DCALCSR);
1118                 while(data32 & 0x80000000);
1119         }
1120
1121         /* Precharg all banks */
1122         do_delay();
1123         for(cs=0;cs<8;cs++) {
1124                 if ((drc & 3) == 2) /* DDR2  */
1125                         write32(BAR+DCALADDR, 0x04000000);
1126                 else   /* DDR1  */
1127                         write32(BAR+DCALADDR, 0x00000000);
1128                 write32(BAR+DCALCSR, (0x83000002 | (cs<<20)));
1129                 do data32 = read32(BAR+DCALCSR);
1130                 while(data32 & 0x80000000);
1131         }
1132
1133         /* EMRS dll's enabled */
1134         do_delay();
1135         for(cs=0;cs<8;cs++) {
1136                 if ((drc & 3) == 2) /* DDR2  */
1137                         /* fixme hard code AL additive latency */
1138                         write32(BAR+DCALADDR, 0x0b940001);
1139                 else   /* DDR1  */
1140                         write32(BAR+DCALADDR, 0x00000001);
1141                 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1142                 do data32 = read32(BAR+DCALCSR);
1143                 while(data32 & 0x80000000);
1144         }
1145         /* MRS reset dll's */
1146         do_delay();
1147         if ((drc & 3) == 2) {  /* DDR2  */
1148                 if(cas_latency == 30)
1149                         mode_reg = 0x053a0000;
1150                 else
1151                         mode_reg = 0x054a0000;
1152         }
1153         else {  /* DDR1  */
1154                 if(cas_latency == 20)
1155                         mode_reg = 0x012a0000;
1156                 else  /*  CAS Latency 2.5 */
1157                         mode_reg = 0x016a0000;
1158         }
1159         for(cs=0;cs<8;cs++) {
1160                 write32(BAR+DCALADDR, mode_reg);
1161                 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1162                 do data32 = read32(BAR+DCALCSR);
1163                 while(data32 & 0x80000000);
1164         }
1165
1166         /* Precharg all banks */
1167         do_delay();
1168         do_delay();
1169         do_delay();
1170         for(cs=0;cs<8;cs++) {
1171                 if ((drc & 3) == 2) /* DDR2  */
1172                         write32(BAR+DCALADDR, 0x04000000);
1173                 else   /* DDR1  */
1174                         write32(BAR+DCALADDR, 0x00000000);
1175                 write32(BAR+DCALCSR, (0x83000002 | (cs<<20)));
1176                 do data32 = read32(BAR+DCALCSR);
1177                 while(data32 & 0x80000000);
1178         }
1179
1180         /* Do 2 refreshes */
1181         do_delay();
1182         for(cs=0;cs<8;cs++) {
1183                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1184                 do data32 = read32(BAR+DCALCSR);
1185                 while(data32 & 0x80000000);
1186         }
1187         do_delay();
1188         for(cs=0;cs<8;cs++) {
1189                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1190                 do data32 = read32(BAR+DCALCSR);
1191                 while(data32 & 0x80000000);
1192         }
1193         do_delay();
1194         /* for good luck do 6 more */
1195         for(cs=0;cs<8;cs++) {
1196                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1197         }
1198         do_delay();
1199         for(cs=0;cs<8;cs++) {
1200                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1201         }
1202         do_delay();
1203         for(cs=0;cs<8;cs++) {
1204                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1205         }
1206         do_delay();
1207         for(cs=0;cs<8;cs++) {
1208                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1209         }
1210         do_delay();
1211         for(cs=0;cs<8;cs++) {
1212                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1213         }
1214         do_delay();
1215         for(cs=0;cs<8;cs++) {
1216                 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1217         }
1218         do_delay();
1219         /* MRS reset dll's normal */
1220         do_delay();
1221         for(cs=0;cs<8;cs++) {
1222                 write32(BAR+DCALADDR, (mode_reg & ~(1<<24)));
1223                 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1224                 do data32 = read32(BAR+DCALCSR);
1225                 while(data32 & 0x80000000);
1226         }
1227
1228         /* Do only if DDR2  EMRS dll's enabled */
1229         if ((drc & 3) == 2) { /* DDR2  */
1230                 do_delay();
1231                 for(cs=0;cs<8;cs++) {
1232                         write32(BAR+DCALADDR, (0x0b940001));
1233                         write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1234                         do data32 = read32(BAR+DCALCSR);
1235                         while(data32 & 0x80000000);
1236                 }
1237         }
1238
1239         do_delay();
1240         /* No command */
1241         write32(BAR+DCALCSR, 0x0000000f);
1242
1243         /* DDR1 This is test code to copy some codes in the factory setup */
1244
1245         write32(BAR, 0x00100000);
1246
1247         if ((drc & 3) == 2) { /* DDR2  */
1248         /* enable on dimm termination */
1249                 set_on_dimm_termination_enable(ctrl);
1250         }
1251
1252         /* receive enable calibration */
1253         set_receive_enable(ctrl);
1254
1255         /* DQS */
1256         pci_write_config32(ctrl->f0, 0x94, 0x3904a100 );
1257         for(i = 0, cnt = (BAR+0x200); i < 24; i++, cnt+=4) {
1258                 write32(cnt, dqs_data[i]);
1259         }
1260         pci_write_config32(ctrl->f0, 0x94, 0x3904a100 );
1261
1262         /* Enable refresh */
1263         /* 0x7c DRC */
1264         data32 = drc & ~(3 << 20);  /* clear ECC mode */
1265         pci_write_config32(ctrl->f0, DRC, data32);
1266         write32(BAR+DCALCSR, 0x0008000f);
1267
1268         /* clear memory and init ECC */
1269         print_debug("Clearing memory\n");
1270         for(i=0;i<64;i+=4) {
1271                 write32(BAR+DCALDATA+i, 0x00000000);
1272         }
1273
1274         for(cs=0;cs<8;cs++) {
1275                 write32(BAR+DCALCSR, (0x830831d8 | (cs<<20)));
1276                 do data32 = read32(BAR+DCALCSR);
1277                 while(data32 & 0x80000000);
1278         }
1279
1280         /* Bring memory subsystem on line */
1281         data32 = pci_read_config32(ctrl->f0, 0x98);
1282         data32 |= (1 << 31);
1283         pci_write_config32(ctrl->f0, 0x98, data32);
1284         /* wait for completion */
1285         print_debug("Waiting for mem complete\n");
1286         while(1) {
1287                 data32 = pci_read_config32(ctrl->f0, 0x98);
1288                 if( (data32 & (1<<31)) == 0)
1289                         break;
1290         }
1291         print_debug("Done\n");
1292
1293         /* Set initialization complete */
1294         /* 0x7c DRC */
1295         drc |= (1 << 29);
1296         data32 = drc & ~(3 << 20);  /* clear ECC mode */
1297         pci_write_config32(ctrl->f0, DRC, data32);
1298
1299         /* Set the ecc mode */
1300         pci_write_config32(ctrl->f0, DRC, drc);
1301
1302         /* Enable memory scrubbing */
1303         /* 0x52 MCHSCRB */
1304         data16 = pci_read_config16(ctrl->f0, MCHSCRB);
1305         data16 &= ~0x0f;
1306         data16 |= ((2 << 2) | (2 << 0));
1307         pci_write_config16(ctrl->f0, MCHSCRB, data16);
1308
1309         /* The memory is now setup, use it */
1310         cache_lbmem(MTRR_TYPE_WRBACK);
1311 }