1 #include <cpu/x86/mem.h>
2 #include <cpu/x86/mtrr.h>
3 #include <cpu/x86/cache.h>
9 static void sdram_set_registers(const struct mem_controller *ctrl)
11 static const unsigned int register_values[] = {
13 /* CKDIS 0x8c disable clocks */
14 PCI_ADDR(0, 0x00, 0, CKDIS), 0xffff0000, 0x0000ffff,
16 /* 0x9c Device present and extended RAM control
17 * DEVPRES is very touchy, hard code the initialization
18 * of PCI-E ports here.
20 PCI_ADDR(0, 0x00, 0, DEVPRES), 0x00000000, 0x07020801 | DEVPRES_CONFIG,
22 /* 0xc8 Remap RAM base and limit off */
23 PCI_ADDR(0, 0x00, 0, REMAPLIMIT), 0x00000000, 0x03df0000,
26 PCI_ADDR(0, 0x00, 0, 0xd8), 0x00000000, 0xb5930000,
27 PCI_ADDR(0, 0x00, 0, 0xe8), 0x00000000, 0x00004a2a,
30 PCI_ADDR(0, 0x00, 0, MCHCFG0), 0xfce0ffff, 0x00006000, /* 6000 */
33 PCI_ADDR(0, 0x00, 0, PAM-1), 0xcccccc7f, 0x33333000,
34 PCI_ADDR(0, 0x00, 0, PAM+3), 0xcccccccc, 0x33333333,
37 PCI_ADDR(0, 0x00, 0, DEVPRES1), 0xffbffff, (1<<22)|(6<<2) | DEVPRES1_CONFIG,
40 PCI_ADDR(0, 0x00, 0, IURBASE), 0x00000fff, BAR |0,
45 max = sizeof(register_values)/sizeof(register_values[0]);
46 for(i = 0; i < max; i += 3) {
50 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x00, 0) + ctrl->f0;
51 where = register_values[i] & 0xff;
52 reg = pci_read_config32(dev, where);
53 reg &= register_values[i+1];
54 reg |= register_values[i+2];
55 pci_write_config32(dev, where, reg);
57 print_spew("done.\r\n");
67 static struct dimm_size spd_get_dimm_size(unsigned device)
69 /* Calculate the log base 2 size of a DIMM in bits */
77 value = spd_read_byte(device, 2); /* type */
78 if (value < 0) goto hw_err;
79 if (value == 8) ddr2 = 1;
81 /* Note it might be easier to use byte 31 here, it has the DIMM size as
82 * a multiple of 4MB. The way we do it now we can size both
83 * sides of an assymetric dimm.
85 value = spd_read_byte(device, 3); /* rows */
86 if (value < 0) goto hw_err;
87 if ((value & 0xf) == 0) goto val_err;
88 sz.side1 += value & 0xf;
90 value = spd_read_byte(device, 4); /* columns */
91 if (value < 0) goto hw_err;
92 if ((value & 0xf) == 0) goto val_err;
93 sz.side1 += value & 0xf;
95 value = spd_read_byte(device, 17); /* banks */
96 if (value < 0) goto hw_err;
97 if ((value & 0xff) == 0) goto val_err;
98 sz.side1 += log2(value & 0xff);
100 /* Get the module data width and convert it to a power of two */
101 value = spd_read_byte(device, 7); /* (high byte) */
102 if (value < 0) goto hw_err;
106 low = spd_read_byte(device, 6); /* (low byte) */
107 if (low < 0) goto hw_err;
108 value = value | (low & 0xff);
109 if ((value != 72) && (value != 64)) goto val_err;
110 sz.side1 += log2(value);
113 value = spd_read_byte(device, 5); /* number of physical banks */
115 if (value < 0) goto hw_err;
118 if (value == 1) goto out;
119 if (value != 2) goto val_err;
121 /* Start with the symmetrical case */
124 value = spd_read_byte(device, 3); /* rows */
125 if (value < 0) goto hw_err;
126 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
127 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
128 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
130 value = spd_read_byte(device, 4); /* columns */
131 if (value < 0) goto hw_err;
132 if ((value & 0xff) == 0) goto val_err;
133 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
134 sz.side2 += ((value >> 4) & 0x0f); /* Add in columsn on side 2 */
138 die("Bad SPD value\r\n");
139 /* If an hw_error occurs report that I have no memory */
148 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
153 for(i = cum = 0; i < DIMM_SOCKETS; i++) {
155 if (dimm_mask & (1 << i)) {
156 sz = spd_get_dimm_size(ctrl->channel0[i]);
158 return -1; /* Report SPD error */
160 /* convert bits to multiples of 64MB */
162 cum += (1 << sz.side1);
164 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
167 cum += (1 << sz.side2);
169 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
172 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
173 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
176 /* set TOM top of memory 0xcc */
177 pci_write_config16(ctrl->f0, TOM, cum);
178 /* set TOLM top of low memory */
184 pci_write_config16(ctrl->f0, TOLM, cum);
189 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
194 for(i = 0; i < DIMM_SOCKETS; i++) {
197 device = ctrl->channel0[i];
199 byte = spd_read_byte(device, 2); /* Type */
200 if ((byte == 7) || (byte == 8)) {
201 dimm_mask |= (1 << i);
204 device = ctrl->channel1[i];
206 byte = spd_read_byte(device, 2);
207 if ((byte == 7) || (byte == 8)) {
208 dimm_mask |= (1 << (i + DIMM_SOCKETS));
216 static int spd_set_row_attributes(const struct mem_controller *ctrl,
226 for(cnt=0; cnt < 4; cnt++) {
227 if (!(dimm_mask & (1 << cnt))) {
231 value = spd_read_byte(ctrl->channel0[cnt], 3); /* rows */
232 if (value < 0) goto hw_err;
233 if ((value & 0xf) == 0) goto val_err;
236 value = spd_read_byte(ctrl->channel0[cnt], 4); /* columns */
237 if (value < 0) goto hw_err;
238 if ((value & 0xf) == 0) goto val_err;
241 value = spd_read_byte(ctrl->channel0[cnt], 17); /* banks */
242 if (value < 0) goto hw_err;
243 if ((value & 0xff) == 0) goto val_err;
244 reg += log2(value & 0xff);
246 /* Get the device width and convert it to a power of two */
247 value = spd_read_byte(ctrl->channel0[cnt], 13);
248 if (value < 0) goto hw_err;
249 value = log2(value & 0xff);
251 if(reg < 27) goto hw_err;
255 dra += reg << (cnt*8);
256 value = spd_read_byte(ctrl->channel0[cnt], 5);
258 dra += reg << ((cnt*8)+4);
262 pci_write_config32(ctrl->f0, DRA, dra);
266 die("Bad SPD value\r\n");
267 /* If an hw_error occurs report that I have no memory */
276 static int spd_set_drt_attributes(const struct mem_controller *ctrl,
277 long dimm_mask, uint32_t drc)
288 static const unsigned char cycle_time[3] = {0x75,0x60,0x50};
289 static const int latency_indicies[] = { 26, 23, 9 };
292 drt = pci_read_config32(ctrl->f0, DRT);
293 drt &= 3; /* save bits 1:0 */
295 for(first_dimm = 0; first_dimm < 4; first_dimm++) {
296 if (dimm_mask & (1 << first_dimm))
301 value = spd_read_byte(ctrl->channel0[first_dimm], 2);
303 drt |= (3<<5); /* back to bark write turn around & cycle add */
306 drt |= (3<<18); /* Trasmax */
308 for(cnt=0; cnt < 4; cnt++) {
309 if (!(dimm_mask & (1 << cnt))) {
312 reg = spd_read_byte(ctrl->channel0[cnt], 18); /* CAS Latency */
313 /* Compute the lowest cas latency supported */
314 latency = log2(reg) -2;
316 /* Loop through and find a fast clock with a low latency */
317 for(index = 0; index < 3; index++, latency++) {
318 if ((latency < 2) || (latency > 4) ||
319 (!(reg & (1 << latency)))) {
322 value = spd_read_byte(ctrl->channel0[cnt],
323 latency_indicies[index]);
325 if(value <= cycle_time[drc&3]) {
326 if( latency > cas_latency) {
327 cas_latency = latency;
333 index = (cas_latency-2);
334 if((index)==0) cas_latency = 20;
335 else if((index)==1) cas_latency = 25;
336 else cas_latency = 30;
338 for(cnt=0;cnt<4;cnt++) {
339 if (!(dimm_mask & (1 << cnt))) {
342 reg = spd_read_byte(ctrl->channel0[cnt], 27)&0x0ff;
343 if(((index>>8)&0x0ff)<reg) {
344 index &= ~(0x0ff << 8);
347 reg = spd_read_byte(ctrl->channel0[cnt], 28)&0x0ff;
348 if(((index>>16)&0x0ff)<reg) {
349 index &= ~(0x0ff << 16);
352 reg = spd_read_byte(ctrl->channel0[cnt], 29)&0x0ff;
353 if(((index2>>0)&0x0ff)<reg) {
354 index2 &= ~(0x0ff << 0);
357 reg = spd_read_byte(ctrl->channel0[cnt], 41)&0x0ff;
358 if(((index2>>8)&0x0ff)<reg) {
359 index2 &= ~(0x0ff << 8);
362 reg = spd_read_byte(ctrl->channel0[cnt], 42)&0x0ff;
363 if(((index2>>16)&0x0ff)<reg) {
364 index2 &= ~(0x0ff << 16);
370 value = cycle_time[drc&3];
371 if(value <= 0x50) { /* 200 MHz */
373 drt |= (2<<2); /* CAS latency 4 */
376 drt |= (1<<2); /* CAS latency 3 */
379 if((index&0x0ff00)<=0x03c00) {
380 drt |= (1<<8); /* Trp RAS Precharg */
382 drt |= (2<<8); /* Trp RAS Precharg */
385 /* Trcd RAS to CAS delay */
386 if((index2&0x0ff)<=0x03c) {
392 /* Tdal Write auto precharge recovery delay */
396 if((index2&0x0ff00)<=0x03700)
398 else if((index2&0xff00)<=0x03c00)
401 drt |= (2<<14); /* spd 41 */
403 drt |= (2<<16); /* Twr not defined for DDR docs say use 2 */
406 if((index&0x0ff0000)<=0x0140000) {
408 } else if((index&0x0ff0000)<=0x0280000) {
410 } else if((index&0x0ff0000)<=0x03c0000) {
416 /* Trfc Auto refresh cycle time */
417 if((index2&0x0ff0000)<=0x04b0000) {
419 } else if((index2&0x0ff0000)<=0x0690000) {
424 /* Docs say use 55 for all 200Mhz */
427 else if(value <= 0x60) { /* 167 Mhz */
428 /* according to new documentation CAS latency is 00
429 * for bits 3:2 for all 167 Mhz
430 drt |= ((index&3)<<2); */ /* set CAS latency */
431 if((index&0x0ff00)<=0x03000) {
432 drt |= (1<<8); /* Trp RAS Precharg */
434 drt |= (2<<8); /* Trp RAS Precharg */
437 /* Trcd RAS to CAS delay */
438 if((index2&0x0ff)<=0x030) {
444 /* Tdal Write auto precharge recovery delay */
448 drt |= (2<<14); /* spd 41, but only one choice */
450 drt |= (2<<16); /* Twr not defined for DDR docs say 2 */
453 if((index&0x0ff0000)<=0x0180000) {
455 } else if((index&0x0ff0000)<=0x0300000) {
461 /* Trfc Auto refresh cycle time */
462 if((index2&0x0ff0000)<=0x0480000) {
464 } else if((index2&0x0ff0000)<=0x0780000) {
469 /* Docs state to use 99 for all 167 Mhz */
472 else if(value <= 0x75) { /* 133 Mhz */
473 drt |= ((index&3)<<2); /* set CAS latency */
474 if((index&0x0ff00)<=0x03c00) {
475 drt |= (1<<8); /* Trp RAS Precharg */
477 drt |= (2<<8); /* Trp RAS Precharg */
480 /* Trcd RAS to CAS delay */
481 if((index2&0x0ff)<=0x03c) {
487 /* Tdal Write auto precharge recovery delay */
491 drt |= (2<<14); /* spd 41, but only one choice */
493 drt |= (1<<16); /* Twr not defined for DDR docs say 1 */
496 if((index&0x0ff0000)<=0x01e0000) {
498 } else if((index&0x0ff0000)<=0x03c0000) {
504 /* Trfc Auto refresh cycle time */
505 if((index2&0x0ff0000)<=0x04b0000) {
507 } else if((index2&0x0ff0000)<=0x0780000) {
513 /* Based on CAS latency */
521 die("Invalid SPD 9 bus speed.\r\n");
525 pci_write_config32(ctrl->f0, DRT, drt);
530 static int spd_set_dram_controller_mode(const struct mem_controller *ctrl,
538 unsigned char dram_type = 0xff;
539 unsigned char ecc = 0xff;
540 unsigned char rate = 62;
541 static const unsigned char spd_rates[6] = {15,3,7,7,62,62};
542 static const unsigned char drc_rates[5] = {0,15,7,62,3};
543 static const unsigned char fsb_conversion[4] = {3,1,3,2};
546 drc = pci_read_config32(ctrl->f0, DRC);
547 for(cnt=0; cnt < 4; cnt++) {
548 if (!(dimm_mask & (1 << cnt))) {
551 value = spd_read_byte(ctrl->channel0[cnt], 11); /* ECC */
552 reg = spd_read_byte(ctrl->channel0[cnt], 2); /* Type */
553 if (value == 2) { /* RAM is ECC capable */
559 die("ERROR - Mixed DDR & DDR2 RAM\r\n");
562 else if ( reg == 7 ) {
566 else if ( ecc > 1 ) {
567 die("ERROR - Mixed DDR & DDR2 RAM\r\n");
571 die("ERROR - RAM not DDR\r\n");
575 die("ERROR - Non ECC memory dimm\r\n");
578 value = spd_read_byte(ctrl->channel0[cnt], 12); /*refresh rate*/
579 value &= 0x0f; /* clip self refresh bit */
580 if (value > 5) goto hw_err;
581 if (rate > spd_rates[value])
582 rate = spd_rates[value];
584 value = spd_read_byte(ctrl->channel0[cnt], 9); /* cycle time */
585 if (value > 0x75) goto hw_err;
587 if (dram_type >= 2) {
588 if (reg == 8) { /*speed is good, is this ddr2?*/
590 } else { /* not ddr2 so use ddr333 */
595 else if (value <= 0x60) {
596 if (dram_type >= 1) dram_type = 1;
598 else dram_type = 0; /* ddr266 */
602 if (read_option(CMOS_VSTART_ECC_memory,CMOS_VLEN_ECC_memory,1) == 0) {
603 ecc = 0; /* ECC off in CMOS so disable it */
604 print_debug("ECC off\r\n");
607 print_debug("ECC on\r\n");
609 drc &= ~(3 << 20); /* clear the ecc bits */
610 drc |= (ecc << 20); /* or in the calculated ecc bits */
611 for ( cnt = 1; cnt < 5; cnt++)
612 if (drc_rates[cnt] == rate)
615 drc &= ~(7 << 8); /* clear the rate bits */
619 if (reg == 8) { /* independant clocks */
623 drc |= (1 << 26); /* set the overlap bit - the factory BIOS does */
624 drc |= (1 << 27); /* set DED retry enable - the factory BIOS does */
627 value = msr.lo >> 16;
629 drc &= ~(3 << 2); /* set the front side bus */
630 drc |= (fsb_conversion[value] << 2);
631 drc &= ~(3 << 0); /* set the dram type */
632 drc |= (dram_type << 0);
637 die("Bad SPD value\r\n");
638 /* If an hw_error occurs report that I have no memory */
645 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
649 /* Test if we can read the spd and if ram is ddr or ddr2 */
650 dimm_mask = spd_detect_dimms(ctrl);
651 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
652 print_err("No memory for this cpu\r\n");
658 static void do_delay(void)
666 #define TIMEOUT_LOOPS 300000
668 #define DCALCSR 0x100
669 #define DCALADDR 0x104
670 #define DCALDATA 0x108
672 static void set_on_dimm_termination_enable(const struct mem_controller *ctrl)
679 /* Set up northbridge values */
681 pci_write_config32(ctrl->f0, 0x88, 0xf0000180);
682 /* Figure out which slots are Empty, Single, or Double sided */
683 for(i=0,t4=0,c2=0;i<8;i+=2) {
684 c1 = pci_read_config8(ctrl->f0, DRB+i);
685 if(c1 == c2) continue;
686 c2 = pci_read_config8(ctrl->f0, DRB+1+i);
694 if( ((t4>>8)&0x0f) == 0 ) {
695 data32 = 0x00000010; /* EEES */
698 if ( ((t4>>16)&0x0f) == 0 ) {
699 data32 = 0x00003132; /* EESS */
702 if ( ((t4>>24)&0x0f) == 0 ) {
703 data32 = 0x00335566; /* ESSS */
706 data32 = 0x77bbddee; /* SSSS */
710 if( ((t4>>8)&0x0f) == 0 ) {
711 data32 = 0x00003132; /* EEED */
714 if ( ((t4>>8)&0x0f) == 2 ) {
715 data32 = 0xb373ecdc; /* EEDD */
718 if ( ((t4>>16)&0x0f) == 0 ) {
719 data32 = 0x00b3a898; /* EESD */
722 data32 = 0x777becdc; /* ESSD */
725 die("Error - First dimm slot empty\r\n");
728 print_debug("ODT Value = ");
729 print_debug_hex32(data32);
732 pci_write_config32(ctrl->f0, 0xb0, data32);
734 for(dimm=0;dimm<8;dimm+=1) {
736 write32(BAR+DCALADDR, 0x0b840001);
737 write32(BAR+DCALCSR, 0x83000003 | (dimm << 20));
739 for(i=0;i<1001;i++) {
740 data32 = read32(BAR+DCALCSR);
741 if(!(data32 & (1<<31)))
746 static void set_receive_enable(const struct mem_controller *ctrl)
749 unsigned int cnt,bit;
757 uint32_t data32_dram;
758 uint32_t dcal_data32_0;
759 uint32_t dcal_data32_1;
760 uint32_t dcal_data32_2;
761 uint32_t dcal_data32_3;
766 for(dimm=0;dimm<8;dimm+=1) {
769 write32(BAR+DCALDATA+(17*4), 0x04020000);
770 write32(BAR+DCALCSR, 0x83800004 | (dimm << 20));
772 for(i=0;i<1001;i++) {
773 data32 = read32(BAR+DCALCSR);
774 if(!(data32 & (1<<31)))
780 dcal_data32_0 = read32(BAR+DCALDATA + 0);
781 dcal_data32_1 = read32(BAR+DCALDATA + 4);
782 dcal_data32_2 = read32(BAR+DCALDATA + 8);
783 dcal_data32_3 = read32(BAR+DCALDATA + 12);
786 dcal_data32_0 = read32(BAR+DCALDATA + 16);
787 dcal_data32_1 = read32(BAR+DCALDATA + 20);
788 dcal_data32_2 = read32(BAR+DCALDATA + 24);
789 dcal_data32_3 = read32(BAR+DCALDATA + 28);
792 /* check if bank is installed */
793 if((dcal_data32_0 == 0) && (dcal_data32_2 == 0))
795 /* Calculate the timing value */
796 for(i=0,edge=0,bit=63,cnt=31,data32r=0,
797 work32l=dcal_data32_1,work32h=dcal_data32_3;
800 if(work32l & (1<<cnt))
803 work32l = dcal_data32_0;
804 work32h = dcal_data32_2;
810 if(!(work32l & (1<<cnt)))
813 work32l = dcal_data32_0;
814 work32h = dcal_data32_2;
822 data32 = ((bit%8) << 1);
823 if(work32h & (1<<cnt))
848 work32l = dcal_data32_0;
849 work32h = dcal_data32_2;
855 if(!(work32l & (1<<cnt)))
858 if(work32l & (1<<cnt))
861 data32 = (((cnt-1)%8)<<1);
862 if(work32h & (1<<(cnt-1))) {
865 /* test for frame edge cross overs */
866 if((edge == 1) && (data32 > 12) &&
867 (((recen+16)-data32) < 3)) {
871 if((edge == 2) && (data32 < 4) &&
872 ((recen - data32) > 12)) {
876 if(((recen+3) >= data32) && ((recen-3) <= data32))
887 recen <<= (dimm/2) * 8;
896 /* Check for Eratta problem */
897 for(i=cnt=bit=0;i<4;i++) {
898 if (((recena>>(i*8))&0x0f)>7) {
902 if((recena>>(i*8))&0x0f) {
911 if(((recena>>(i*8))&0x0f)>7) {
912 recena &= ~(0x0f<<(i*8));
913 recena |= (7<<(i*8));
919 if(((recena>>(i*8))&0x0f)<8) {
920 recena &= ~(0x0f<<(i*8));
921 recena |= (8<<(i*8));
926 for(i=cnt=bit=0;i<4;i++) {
927 if (((recenb>>(i*8))&0x0f)>7) {
931 if((recenb>>(i*8))&0x0f) {
940 if(((recenb>>(i*8))&0x0f)>7) {
941 recenb &= ~(0x0f<<(i*8));
942 recenb |= (7<<(i*8));
948 if(((recenb>>(i*8))&0x0f)<8) {
949 recenb &= ~(0x0f<<(i*8));
950 recenb |= (8<<(i*8));
956 // recena = 0x0000090a;
957 // recenb = 0x0000090a;
959 print_debug("Receive enable A = ");
960 print_debug_hex32(recena);
961 print_debug(", Receive enable B = ");
962 print_debug_hex32(recenb);
965 /* clear out the calibration area */
966 write32(BAR+DCALDATA+(16*4), 0x00000000);
967 write32(BAR+DCALDATA+(17*4), 0x00000000);
968 write32(BAR+DCALDATA+(18*4), 0x00000000);
969 write32(BAR+DCALDATA+(19*4), 0x00000000);
972 write32(BAR+DCALCSR, 0x0000000f);
974 write32(BAR+0x150, recena);
975 write32(BAR+0x154, recenb);
979 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
990 volatile unsigned long *iptrv;
995 static const struct {
998 /* FSB 133 DIMM 266 */
999 {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1000 /* FSB 133 DIMM 333 */
1001 {{ 0x00000000, 0x00000000, 0x00000000, 0x00000000}},
1002 /* FSB 133 DIMM 400 */
1003 {{ 0x00000120, 0x00000000, 0x00000032, 0x00000010}},
1004 /* FSB 167 DIMM 266 */
1005 {{ 0x00005432, 0x00001000, 0x00004325, 0x00000000}},
1006 /* FSB 167 DIMM 333 */
1007 {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1008 /* FSB 167 DIMM 400 */
1009 {{ 0x00154320, 0x00000000, 0x00065432, 0x00010000}},
1010 /* FSB 200 DIMM 266 */
1011 {{ 0x00000032, 0x00000010, 0x00000120, 0x00000000}},
1012 /* FSB 200 DIMM 333 */
1013 {{ 0x00065432, 0x00010000, 0x00054326, 0x00000000}},
1014 /* FSB 200 DIMM 400 */
1015 {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1018 static const uint32_t dqs_data[] = {
1019 0xffffffff, 0xffffffff, 0x000000ff,
1020 0xffffffff, 0xffffffff, 0x000000ff,
1021 0xffffffff, 0xffffffff, 0x000000ff,
1022 0xffffffff, 0xffffffff, 0x000000ff,
1023 0xffffffff, 0xffffffff, 0x000000ff,
1024 0xffffffff, 0xffffffff, 0x000000ff,
1025 0xffffffff, 0xffffffff, 0x000000ff,
1026 0xffffffff, 0xffffffff, 0x000000ff};
1028 mask = spd_detect_dimms(ctrl);
1029 print_debug("Starting SDRAM Enable\r\n");
1032 #ifdef DIMM_MAP_LOGICAL
1033 pci_write_config32(ctrl->f0, DRM,
1034 0x00210000 | DIMM_MAP_LOGICAL);
1036 pci_write_config32(ctrl->f0, DRM, 0x00211248);
1038 /* set dram type and Front Side Bus freq. */
1039 drc = spd_set_dram_controller_mode(ctrl, mask);
1041 die("Error calculating DRC\r\n");
1043 data32 = drc & ~(3 << 20); /* clear ECC mode */
1044 data32 = data32 & ~(7 << 8); /* clear refresh rates */
1045 data32 = data32 | (1 << 5); /* temp turn off of ODT */
1046 /* Set gearing, then dram controller mode */
1047 /* drc bits 1:0 = DIMM speed, bits 3:2 = FSB speed */
1048 for(iptr = gearing[(drc&3)+((((drc>>2)&3)-1)*3)].clkgr,cnt=0;
1050 pci_write_config32(ctrl->f0, 0xa0+(cnt*4), iptr[cnt]);
1053 pci_write_config32(ctrl->f0, DRC, data32);
1055 /* turn the clocks on */
1057 pci_write_config16(ctrl->f0, CKDIS, 0x0000);
1059 /* 0x9a DDRCSR Take subsystem out of idle */
1060 data16 = pci_read_config16(ctrl->f0, DDRCSR);
1061 data16 &= ~(7 << 12);
1062 data16 |= (3 << 12); /* use dual channel lock step */
1063 pci_write_config16(ctrl->f0, DDRCSR, data16);
1065 /* program row size DRB */
1066 spd_set_ram_size(ctrl, mask);
1068 /* program page size DRA */
1069 spd_set_row_attributes(ctrl, mask);
1071 /* program DRT timing values */
1072 cas_latency = spd_set_drt_attributes(ctrl, mask, drc);
1074 for(i=0;i<8;i++) { /* loop throught each dimm to test for row */
1075 print_debug("DIMM ");
1076 print_debug_hex8(i);
1077 print_debug("\r\n");
1081 write32(BAR + 0x100, (0x03000000 | (i<<20)));
1083 write32(BAR+0x100, (0x83000000 | (i<<20)));
1085 data32 = read32(BAR+DCALCSR);
1086 while(data32 & 0x80000000)
1087 data32 = read32(BAR+DCALCSR);
1094 for(cs=0;cs<8;cs++) {
1095 write32(BAR + DCALCSR, (0x83000000 | (cs<<20)));
1096 data32 = read32(BAR+DCALCSR);
1097 while(data32 & 0x80000000)
1098 data32 = read32(BAR+DCALCSR);
1101 /* Precharg all banks */
1103 for(cs=0;cs<8;cs++) {
1104 if ((drc & 3) == 2) /* DDR2 */
1105 write32(BAR+DCALADDR, 0x04000000);
1107 write32(BAR+DCALADDR, 0x00000000);
1108 write32(BAR+DCALCSR, (0x83000002 | (cs<<20)));
1109 data32 = read32(BAR+DCALCSR);
1110 while(data32 & 0x80000000)
1111 data32 = read32(BAR+DCALCSR);
1114 /* EMRS dll's enabled */
1116 for(cs=0;cs<8;cs++) {
1117 if ((drc & 3) == 2) /* DDR2 */
1118 /* fixme hard code AL additive latency */
1119 write32(BAR+DCALADDR, 0x0b940001);
1121 write32(BAR+DCALADDR, 0x00000001);
1122 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1123 data32 = read32(BAR+DCALCSR);
1124 while(data32 & 0x80000000)
1125 data32 = read32(BAR+DCALCSR);
1127 /* MRS reset dll's */
1129 if ((drc & 3) == 2) { /* DDR2 */
1130 if(cas_latency == 30)
1131 mode_reg = 0x053a0000;
1133 mode_reg = 0x054a0000;
1136 if(cas_latency == 20)
1137 mode_reg = 0x012a0000;
1138 else /* CAS Latency 2.5 */
1139 mode_reg = 0x016a0000;
1141 for(cs=0;cs<8;cs++) {
1142 write32(BAR+DCALADDR, mode_reg);
1143 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1144 data32 = read32(BAR+DCALCSR);
1145 while(data32 & 0x80000000)
1146 data32 = read32(BAR+DCALCSR);
1149 /* Precharg all banks */
1153 for(cs=0;cs<8;cs++) {
1154 if ((drc & 3) == 2) /* DDR2 */
1155 write32(BAR+DCALADDR, 0x04000000);
1157 write32(BAR+DCALADDR, 0x00000000);
1158 write32(BAR+DCALCSR, (0x83000002 | (cs<<20)));
1159 data32 = read32(BAR+DCALCSR);
1160 while(data32 & 0x80000000)
1161 data32 = read32(BAR+DCALCSR);
1164 /* Do 2 refreshes */
1166 for(cs=0;cs<8;cs++) {
1167 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1168 data32 = read32(BAR+DCALCSR);
1169 while(data32 & 0x80000000)
1170 data32 = read32(BAR+DCALCSR);
1173 for(cs=0;cs<8;cs++) {
1174 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1175 data32 = read32(BAR+DCALCSR);
1176 while(data32 & 0x80000000)
1177 data32 = read32(BAR+DCALCSR);
1180 /* for good luck do 6 more */
1181 for(cs=0;cs<8;cs++) {
1182 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1185 for(cs=0;cs<8;cs++) {
1186 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1189 for(cs=0;cs<8;cs++) {
1190 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1193 for(cs=0;cs<8;cs++) {
1194 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1197 for(cs=0;cs<8;cs++) {
1198 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1201 for(cs=0;cs<8;cs++) {
1202 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1205 /* MRS reset dll's normal */
1207 for(cs=0;cs<8;cs++) {
1208 write32(BAR+DCALADDR, (mode_reg & ~(1<<24)));
1209 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1210 data32 = read32(BAR+DCALCSR);
1211 while(data32 & 0x80000000)
1212 data32 = read32(BAR+DCALCSR);
1215 /* Do only if DDR2 EMRS dll's enabled */
1216 if ((drc & 3) == 2) { /* DDR2 */
1218 for(cs=0;cs<8;cs++) {
1219 write32(BAR+DCALADDR, (0x0b940001));
1220 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1221 data32 = read32(BAR+DCALCSR);
1222 while(data32 & 0x80000000)
1223 data32 = read32(BAR+DCALCSR);
1229 write32(BAR+DCALCSR, 0x0000000f);
1231 /* DDR1 This is test code to copy some codes in the factory setup */
1233 write32(BAR, 0x00100000);
1235 if ((drc & 3) == 2) { /* DDR2 */
1236 /* enable on dimm termination */
1237 set_on_dimm_termination_enable(ctrl);
1240 /* receive enable calibration */
1241 set_receive_enable(ctrl);
1244 pci_write_config32(ctrl->f0, 0x94, 0x3904a100 );
1245 for(i = 0, cnt = (BAR+0x200); i < 24; i++, cnt+=4) {
1246 write32(cnt, dqs_data[i]);
1248 pci_write_config32(ctrl->f0, 0x94, 0x3904a100 );
1250 /* Enable refresh */
1252 data32 = drc & ~(3 << 20); /* clear ECC mode */
1253 pci_write_config32(ctrl->f0, DRC, data32);
1254 write32(BAR+DCALCSR, 0x0008000f);
1256 /* clear memory and init ECC */
1257 print_debug("Clearing memory\r\n");
1258 for(i=0;i<64;i+=4) {
1259 write32(BAR+DCALDATA+i, 0x00000000);
1262 for(cs=0;cs<8;cs++) {
1263 write32(BAR+DCALCSR, (0x830831d8 | (cs<<20)));
1264 data32 = read32(BAR+DCALCSR);
1265 while(data32 & 0x80000000)
1266 data32 = read32(BAR+DCALCSR);
1269 /* Bring memory subsystem on line */
1270 data32 = pci_read_config32(ctrl->f0, 0x98);
1271 data32 |= (1 << 31);
1272 pci_write_config32(ctrl->f0, 0x98, data32);
1273 /* wait for completion */
1274 print_debug("Waiting for mem complete\r\n");
1276 data32 = pci_read_config32(ctrl->f0, 0x98);
1277 if( (data32 & (1<<31)) == 0)
1280 print_debug("Done\r\n");
1282 /* Set initialization complete */
1285 data32 = drc & ~(3 << 20); /* clear ECC mode */
1286 pci_write_config32(ctrl->f0, DRC, data32);
1288 /* Set the ecc mode */
1289 pci_write_config32(ctrl->f0, DRC, drc);
1291 /* Enable memory scrubbing */
1293 data16 = pci_read_config16(ctrl->f0, MCHSCRB);
1295 data16 |= ((2 << 2) | (2 << 0));
1296 pci_write_config16(ctrl->f0, MCHSCRB, data16);
1298 /* The memory is now setup, use it */
1299 cache_lbmem(MTRR_TYPE_WRBACK);