2 * This file is part of the coreboot project.
4 * Copyright (C) 2005 Eric W. Biederman and Tom Zimmerman
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.
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.
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
21 #include <cpu/x86/mem.h>
22 #include <cpu/x86/mtrr.h>
23 #include <cpu/x86/cache.h>
28 #define BAR 0x40000000
30 static void sdram_set_registers(const struct mem_controller *ctrl)
32 static const unsigned int register_values[] = {
34 /* CKDIS 0x8c disable clocks */
35 PCI_ADDR(0, 0x00, 0, CKDIS), 0xffff0000, 0x0000ffff,
37 /* 0x9c Device present and extended RAM control
38 * DEVPRES is very touchy, hard code the initialization
39 * of PCI-E ports here.
41 PCI_ADDR(0, 0x00, 0, DEVPRES), 0x00000000, 0x07020801 | DEVPRES_CONFIG,
43 /* 0xc8 Remap RAM base and limit off */
44 PCI_ADDR(0, 0x00, 0, REMAPLIMIT), 0x00000000, 0x03df0000,
47 PCI_ADDR(0, 0x00, 0, 0xd8), 0x00000000, 0xb5930000,
48 PCI_ADDR(0, 0x00, 0, 0xe8), 0x00000000, 0x00004a2a,
51 PCI_ADDR(0, 0x00, 0, MCHCFG0), 0xfce0ffff, 0x00006000, /* 6000 */
54 PCI_ADDR(0, 0x00, 0, PAM-1), 0xcccccc7f, 0x33333000,
55 PCI_ADDR(0, 0x00, 0, PAM+3), 0xcccccccc, 0x33333333,
58 PCI_ADDR(0, 0x00, 0, DEVPRES1), 0xffbffff, (1<<22)|(6<<2) | DEVPRES1_CONFIG,
61 PCI_ADDR(0, 0x00, 0, IURBASE), 0x00000fff, BAR |0,
66 max = ARRAY_SIZE(register_values);
67 for(i = 0; i < max; i += 3) {
71 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x00, 0) + ctrl->f0;
72 where = register_values[i] & 0xff;
73 reg = pci_read_config32(dev, where);
74 reg &= register_values[i+1];
75 reg |= register_values[i+2];
76 pci_write_config32(dev, where, reg);
78 print_spew("done.\r\n");
88 static struct dimm_size spd_get_dimm_size(unsigned device)
90 /* Calculate the log base 2 size of a DIMM in bits */
98 value = spd_read_byte(device, 2); /* type */
99 if (value < 0) goto hw_err;
100 if (value == 8) ddr2 = 1;
102 /* Note it might be easier to use byte 31 here, it has the DIMM size as
103 * a multiple of 4MB. The way we do it now we can size both
104 * sides of an assymetric dimm.
106 value = spd_read_byte(device, 3); /* rows */
107 if (value < 0) goto hw_err;
108 if ((value & 0xf) == 0) goto val_err;
109 sz.side1 += value & 0xf;
111 value = spd_read_byte(device, 4); /* columns */
112 if (value < 0) goto hw_err;
113 if ((value & 0xf) == 0) goto val_err;
114 sz.side1 += value & 0xf;
116 value = spd_read_byte(device, 17); /* banks */
117 if (value < 0) goto hw_err;
118 if ((value & 0xff) == 0) goto val_err;
119 sz.side1 += log2(value & 0xff);
121 /* Get the module data width and convert it to a power of two */
122 value = spd_read_byte(device, 7); /* (high byte) */
123 if (value < 0) goto hw_err;
127 low = spd_read_byte(device, 6); /* (low byte) */
128 if (low < 0) goto hw_err;
129 value = value | (low & 0xff);
130 if ((value != 72) && (value != 64)) goto val_err;
131 sz.side1 += log2(value);
134 value = spd_read_byte(device, 5); /* number of physical banks */
136 if (value < 0) goto hw_err;
139 if (value == 1) goto out;
140 if (value != 2) goto val_err;
142 /* Start with the symmetrical case */
145 value = spd_read_byte(device, 3); /* rows */
146 if (value < 0) goto hw_err;
147 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
148 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
149 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
151 value = spd_read_byte(device, 4); /* columns */
152 if (value < 0) goto hw_err;
153 if ((value & 0xff) == 0) goto val_err;
154 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
155 sz.side2 += ((value >> 4) & 0x0f); /* Add in columsn on side 2 */
159 die("Bad SPD value\r\n");
160 /* If an hw_error occurs report that I have no memory */
169 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
174 for(i = cum = 0; i < DIMM_SOCKETS; i++) {
176 if (dimm_mask & (1 << i)) {
177 sz = spd_get_dimm_size(ctrl->channel0[i]);
179 return -1; /* Report SPD error */
181 /* convert bits to multiples of 64MB */
183 cum += (1 << sz.side1);
185 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
188 cum += (1 << sz.side2);
190 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
193 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
194 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
197 /* set TOM top of memory 0xcc */
198 pci_write_config16(ctrl->f0, TOM, cum);
199 /* set TOLM top of low memory */
205 pci_write_config16(ctrl->f0, TOLM, cum);
210 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
215 for(i = 0; i < DIMM_SOCKETS; i++) {
218 device = ctrl->channel0[i];
220 byte = spd_read_byte(device, 2); /* Type */
221 if ((byte == 7) || (byte == 8)) {
222 dimm_mask |= (1 << i);
225 device = ctrl->channel1[i];
227 byte = spd_read_byte(device, 2);
228 if ((byte == 7) || (byte == 8)) {
229 dimm_mask |= (1 << (i + DIMM_SOCKETS));
237 static int spd_set_row_attributes(const struct mem_controller *ctrl,
247 for(cnt=0; cnt < 4; cnt++) {
248 if (!(dimm_mask & (1 << cnt))) {
252 value = spd_read_byte(ctrl->channel0[cnt], 3); /* rows */
253 if (value < 0) goto hw_err;
254 if ((value & 0xf) == 0) goto val_err;
257 value = spd_read_byte(ctrl->channel0[cnt], 4); /* columns */
258 if (value < 0) goto hw_err;
259 if ((value & 0xf) == 0) goto val_err;
262 value = spd_read_byte(ctrl->channel0[cnt], 17); /* banks */
263 if (value < 0) goto hw_err;
264 if ((value & 0xff) == 0) goto val_err;
265 reg += log2(value & 0xff);
267 /* Get the device width and convert it to a power of two */
268 value = spd_read_byte(ctrl->channel0[cnt], 13);
269 if (value < 0) goto hw_err;
270 value = log2(value & 0xff);
272 if(reg < 27) goto hw_err;
276 dra += reg << (cnt*8);
277 value = spd_read_byte(ctrl->channel0[cnt], 5);
279 dra += reg << ((cnt*8)+4);
283 pci_write_config32(ctrl->f0, DRA, dra);
287 die("Bad SPD value\r\n");
288 /* If an hw_error occurs report that I have no memory */
297 static int spd_set_drt_attributes(const struct mem_controller *ctrl,
298 long dimm_mask, uint32_t drc)
309 static const unsigned char cycle_time[3] = {0x75,0x60,0x50};
310 static const int latency_indicies[] = { 26, 23, 9 };
313 drt = pci_read_config32(ctrl->f0, DRT);
314 drt &= 3; /* save bits 1:0 */
316 for(first_dimm = 0; first_dimm < 4; first_dimm++) {
317 if (dimm_mask & (1 << first_dimm))
322 value = spd_read_byte(ctrl->channel0[first_dimm], 2);
324 drt |= (3<<5); /* back to bark write turn around & cycle add */
327 drt |= (3<<18); /* Trasmax */
329 for(cnt=0; cnt < 4; cnt++) {
330 if (!(dimm_mask & (1 << cnt))) {
333 reg = spd_read_byte(ctrl->channel0[cnt], 18); /* CAS Latency */
334 /* Compute the lowest cas latency supported */
335 latency = log2(reg) -2;
337 /* Loop through and find a fast clock with a low latency */
338 for(index = 0; index < 3; index++, latency++) {
339 if ((latency < 2) || (latency > 4) ||
340 (!(reg & (1 << latency)))) {
343 value = spd_read_byte(ctrl->channel0[cnt],
344 latency_indicies[index]);
346 if(value <= cycle_time[drc&3]) {
347 if( latency > cas_latency) {
348 cas_latency = latency;
354 index = (cas_latency-2);
355 if((index)==0) cas_latency = 20;
356 else if((index)==1) cas_latency = 25;
357 else cas_latency = 30;
359 for(cnt=0;cnt<4;cnt++) {
360 if (!(dimm_mask & (1 << cnt))) {
363 reg = spd_read_byte(ctrl->channel0[cnt], 27)&0x0ff;
364 if(((index>>8)&0x0ff)<reg) {
365 index &= ~(0x0ff << 8);
368 reg = spd_read_byte(ctrl->channel0[cnt], 28)&0x0ff;
369 if(((index>>16)&0x0ff)<reg) {
370 index &= ~(0x0ff << 16);
373 reg = spd_read_byte(ctrl->channel0[cnt], 29)&0x0ff;
374 if(((index2>>0)&0x0ff)<reg) {
375 index2 &= ~(0x0ff << 0);
378 reg = spd_read_byte(ctrl->channel0[cnt], 41)&0x0ff;
379 if(((index2>>8)&0x0ff)<reg) {
380 index2 &= ~(0x0ff << 8);
383 reg = spd_read_byte(ctrl->channel0[cnt], 42)&0x0ff;
384 if(((index2>>16)&0x0ff)<reg) {
385 index2 &= ~(0x0ff << 16);
391 value = cycle_time[drc&3];
392 if(value <= 0x50) { /* 200 MHz */
394 drt |= (2<<2); /* CAS latency 4 */
397 drt |= (1<<2); /* CAS latency 3 */
400 if((index&0x0ff00)<=0x03c00) {
401 drt |= (1<<8); /* Trp RAS Precharg */
403 drt |= (2<<8); /* Trp RAS Precharg */
406 /* Trcd RAS to CAS delay */
407 if((index2&0x0ff)<=0x03c) {
413 /* Tdal Write auto precharge recovery delay */
417 if((index2&0x0ff00)<=0x03700)
419 else if((index2&0xff00)<=0x03c00)
422 drt |= (2<<14); /* spd 41 */
424 drt |= (2<<16); /* Twr not defined for DDR docs say use 2 */
427 if((index&0x0ff0000)<=0x0140000) {
429 } else if((index&0x0ff0000)<=0x0280000) {
431 } else if((index&0x0ff0000)<=0x03c0000) {
437 /* Trfc Auto refresh cycle time */
438 if((index2&0x0ff0000)<=0x04b0000) {
440 } else if((index2&0x0ff0000)<=0x0690000) {
445 /* Docs say use 55 for all 200Mhz */
448 else if(value <= 0x60) { /* 167 Mhz */
449 /* according to new documentation CAS latency is 00
450 * for bits 3:2 for all 167 Mhz
451 drt |= ((index&3)<<2); */ /* set CAS latency */
452 if((index&0x0ff00)<=0x03000) {
453 drt |= (1<<8); /* Trp RAS Precharg */
455 drt |= (2<<8); /* Trp RAS Precharg */
458 /* Trcd RAS to CAS delay */
459 if((index2&0x0ff)<=0x030) {
465 /* Tdal Write auto precharge recovery delay */
469 drt |= (2<<14); /* spd 41, but only one choice */
471 drt |= (2<<16); /* Twr not defined for DDR docs say 2 */
474 if((index&0x0ff0000)<=0x0180000) {
476 } else if((index&0x0ff0000)<=0x0300000) {
482 /* Trfc Auto refresh cycle time */
483 if((index2&0x0ff0000)<=0x0480000) {
485 } else if((index2&0x0ff0000)<=0x0780000) {
490 /* Docs state to use 99 for all 167 Mhz */
493 else if(value <= 0x75) { /* 133 Mhz */
494 drt |= ((index&3)<<2); /* set CAS latency */
495 if((index&0x0ff00)<=0x03c00) {
496 drt |= (1<<8); /* Trp RAS Precharg */
498 drt |= (2<<8); /* Trp RAS Precharg */
501 /* Trcd RAS to CAS delay */
502 if((index2&0x0ff)<=0x03c) {
508 /* Tdal Write auto precharge recovery delay */
512 drt |= (2<<14); /* spd 41, but only one choice */
514 drt |= (1<<16); /* Twr not defined for DDR docs say 1 */
517 if((index&0x0ff0000)<=0x01e0000) {
519 } else if((index&0x0ff0000)<=0x03c0000) {
525 /* Trfc Auto refresh cycle time */
526 if((index2&0x0ff0000)<=0x04b0000) {
528 } else if((index2&0x0ff0000)<=0x0780000) {
534 /* Based on CAS latency */
542 die("Invalid SPD 9 bus speed.\r\n");
546 pci_write_config32(ctrl->f0, DRT, drt);
551 static int spd_set_dram_controller_mode(const struct mem_controller *ctrl,
559 unsigned char dram_type = 0xff;
560 unsigned char ecc = 0xff;
561 unsigned char rate = 62;
562 static const unsigned char spd_rates[6] = {15,3,7,7,62,62};
563 static const unsigned char drc_rates[5] = {0,15,7,62,3};
564 static const unsigned char fsb_conversion[4] = {3,1,3,2};
567 drc = pci_read_config32(ctrl->f0, DRC);
568 for(cnt=0; cnt < 4; cnt++) {
569 if (!(dimm_mask & (1 << cnt))) {
572 value = spd_read_byte(ctrl->channel0[cnt], 11); /* ECC */
573 reg = spd_read_byte(ctrl->channel0[cnt], 2); /* Type */
574 if (value == 2) { /* RAM is ECC capable */
580 die("ERROR - Mixed DDR & DDR2 RAM\r\n");
583 else if ( reg == 7 ) {
587 else if ( ecc > 1 ) {
588 die("ERROR - Mixed DDR & DDR2 RAM\r\n");
592 die("ERROR - RAM not DDR\r\n");
596 die("ERROR - Non ECC memory dimm\r\n");
599 value = spd_read_byte(ctrl->channel0[cnt], 12); /*refresh rate*/
600 value &= 0x0f; /* clip self refresh bit */
601 if (value > 5) goto hw_err;
602 if (rate > spd_rates[value])
603 rate = spd_rates[value];
605 value = spd_read_byte(ctrl->channel0[cnt], 9); /* cycle time */
606 if (value > 0x75) goto hw_err;
608 if (dram_type >= 2) {
609 if (reg == 8) { /*speed is good, is this ddr2?*/
611 } else { /* not ddr2 so use ddr333 */
616 else if (value <= 0x60) {
617 if (dram_type >= 1) dram_type = 1;
619 else dram_type = 0; /* ddr266 */
623 if (read_option(CMOS_VSTART_ECC_memory,CMOS_VLEN_ECC_memory,1) == 0) {
624 ecc = 0; /* ECC off in CMOS so disable it */
625 print_debug("ECC off\r\n");
628 print_debug("ECC on\r\n");
630 drc &= ~(3 << 20); /* clear the ecc bits */
631 drc |= (ecc << 20); /* or in the calculated ecc bits */
632 for ( cnt = 1; cnt < 5; cnt++)
633 if (drc_rates[cnt] == rate)
636 drc &= ~(7 << 8); /* clear the rate bits */
640 if (reg == 8) { /* independant clocks */
644 drc |= (1 << 26); /* set the overlap bit - the factory BIOS does */
645 drc |= (1 << 27); /* set DED retry enable - the factory BIOS does */
648 value = msr.lo >> 16;
650 drc &= ~(3 << 2); /* set the front side bus */
651 drc |= (fsb_conversion[value] << 2);
652 drc &= ~(3 << 0); /* set the dram type */
653 drc |= (dram_type << 0);
658 die("Bad SPD value\r\n");
659 /* If an hw_error occurs report that I have no memory */
666 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
670 /* Test if we can read the spd and if ram is ddr or ddr2 */
671 dimm_mask = spd_detect_dimms(ctrl);
672 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
673 print_err("No memory for this cpu\r\n");
679 static void do_delay(void)
687 static void pll_setup(uint32_t drc)
690 if(drc&3) { /* DDR 333 or DDR 400 */
691 if((drc&0x0c) == 0x0c) { /* FSB 200 */
694 else if((drc&0x0c) == 0x08) { /* FSB 167 */
697 else if(drc&1){ /* FSB 133 DDR 333 */
700 else { /* FSB 133 DDR 400 */
705 if((drc&0x08) == 0x08) { /* FSB 200 or 167 */
712 mainboard_set_e7520_pll(pins);
716 #define TIMEOUT_LOOPS 300000
718 #define DCALCSR 0x100
719 #define DCALADDR 0x104
720 #define DCALDATA 0x108
722 static void set_on_dimm_termination_enable(const struct mem_controller *ctrl)
729 /* Set up northbridge values */
731 pci_write_config32(ctrl->f0, 0x88, 0xf0000180);
732 /* Figure out which slots are Empty, Single, or Double sided */
733 for(i=0,t4=0,c2=0;i<8;i+=2) {
734 c1 = pci_read_config8(ctrl->f0, DRB+i);
735 if(c1 == c2) continue;
736 c2 = pci_read_config8(ctrl->f0, DRB+1+i);
744 if( ((t4>>8)&0x0f) == 0 ) {
745 data32 = 0x00000010; /* EEES */
748 if ( ((t4>>16)&0x0f) == 0 ) {
749 data32 = 0x00003132; /* EESS */
752 if ( ((t4>>24)&0x0f) == 0 ) {
753 data32 = 0x00335566; /* ESSS */
756 data32 = 0x77bbddee; /* SSSS */
760 if( ((t4>>8)&0x0f) == 0 ) {
761 data32 = 0x00003132; /* EEED */
764 if ( ((t4>>8)&0x0f) == 2 ) {
765 data32 = 0xb373ecdc; /* EEDD */
768 if ( ((t4>>16)&0x0f) == 0 ) {
769 data32 = 0x00b3a898; /* EESD */
772 data32 = 0x777becdc; /* ESSD */
775 die("Error - First dimm slot empty\r\n");
778 print_debug("ODT Value = ");
779 print_debug_hex32(data32);
782 pci_write_config32(ctrl->f0, 0xb0, data32);
784 for(dimm=0;dimm<8;dimm+=1) {
786 write32(BAR+DCALADDR, 0x0b840001);
787 write32(BAR+DCALCSR, 0x83000003 | (dimm << 20));
789 for(i=0;i<1001;i++) {
790 data32 = read32(BAR+DCALCSR);
791 if(!(data32 & (1<<31)))
796 static void set_receive_enable(const struct mem_controller *ctrl)
807 uint32_t data32_dram;
808 uint32_t dcal_data32_0;
809 uint32_t dcal_data32_1;
810 uint32_t dcal_data32_2;
811 uint32_t dcal_data32_3;
816 for(dimm=0;dimm<8;dimm+=1) {
819 write32(BAR+DCALDATA+(17*4), 0x04020000);
820 write32(BAR+DCALCSR, 0x83800004 | (dimm << 20));
822 for(i=0;i<1001;i++) {
823 data32 = read32(BAR+DCALCSR);
824 if(!(data32 & (1<<31)))
830 dcal_data32_0 = read32(BAR+DCALDATA + 0);
831 dcal_data32_1 = read32(BAR+DCALDATA + 4);
832 dcal_data32_2 = read32(BAR+DCALDATA + 8);
833 dcal_data32_3 = read32(BAR+DCALDATA + 12);
836 dcal_data32_0 = read32(BAR+DCALDATA + 16);
837 dcal_data32_1 = read32(BAR+DCALDATA + 20);
838 dcal_data32_2 = read32(BAR+DCALDATA + 24);
839 dcal_data32_3 = read32(BAR+DCALDATA + 28);
842 /* check if bank is installed */
843 if((dcal_data32_0 == 0) && (dcal_data32_2 == 0))
845 /* Calculate the timing value */
848 for(i=0,edge=0,bit=63,cnt=31,data32r=0,
849 work32l=dcal_data32_1,work32h=dcal_data32_3;
852 if(work32l & (1<<cnt))
855 work32l = dcal_data32_0;
856 work32h = dcal_data32_2;
862 if(!(work32l & (1<<cnt)))
865 work32l = dcal_data32_0;
866 work32h = dcal_data32_2;
874 data32 = ((bit%8) << 1);
875 if(work32h & (1<<cnt))
900 work32l = dcal_data32_0;
901 work32h = dcal_data32_2;
907 if(!(work32l & (1<<cnt)))
910 if(work32l & (1<<cnt))
913 data32 = (((cnt-1)%8)<<1);
914 if(work32h & (1<<(cnt-1))) {
917 /* test for frame edge cross overs */
918 if((edge == 1) && (data32 > 12) &&
919 (((recen+16)-data32) < 3)) {
923 if((edge == 2) && (data32 < 4) &&
924 ((recen - data32) > 12)) {
928 if(((recen+3) >= data32) && ((recen-3) <= data32))
938 recen+=2; /* this is not in the spec, but matches
939 the factory output, and has less failure */
940 recen <<= (dimm/2) * 8;
949 /* Check for Eratta problem */
950 for(i=cnt=0;i<32;i+=8) {
951 if (((recena>>i)&0x0f)>7) {
955 if((recena>>i)&0x0f) {
961 cnt = (cnt&0x0f) - (cnt>>16);
964 if(((recena>>i)&0x0f)>7) {
965 recena &= ~(0x0f<<i);
972 if(((recena>>i)&0x0f)<8) {
973 recena &= ~(0x0f<<i);
979 for(i=cnt=0;i<32;i+=8) {
980 if (((recenb>>i)&0x0f)>7) {
984 if((recenb>>i)&0x0f) {
990 cnt = (cnt&0x0f) - (cnt>>16);
993 if(((recenb>>i)&0x0f)>7) {
994 recenb &= ~(0x0f<<i);
1000 for(i=0;i<32;i+=8) {
1001 if(((recenb>>8)&0x0f)<8) {
1002 recenb &= ~(0x0f<<i);
1009 print_debug("Receive enable A = ");
1010 print_debug_hex32(recena);
1011 print_debug(", Receive enable B = ");
1012 print_debug_hex32(recenb);
1013 print_debug("\r\n");
1015 /* clear out the calibration area */
1016 write32(BAR+DCALDATA+(16*4), 0x00000000);
1017 write32(BAR+DCALDATA+(17*4), 0x00000000);
1018 write32(BAR+DCALDATA+(18*4), 0x00000000);
1019 write32(BAR+DCALDATA+(19*4), 0x00000000);
1022 write32(BAR+DCALCSR, 0x0000000f);
1024 write32(BAR+0x150, recena);
1025 write32(BAR+0x154, recenb);
1029 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
1040 volatile unsigned long *iptrv;
1045 static const struct {
1048 /* FSB 133 DIMM 266 */
1049 {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1050 /* FSB 133 DIMM 333 */
1051 {{ 0x00000000, 0x00000000, 0x00000000, 0x00000000}},
1052 /* FSB 133 DIMM 400 */
1053 {{ 0x00000120, 0x00000000, 0x00000032, 0x00000010}},
1054 /* FSB 167 DIMM 266 */
1055 {{ 0x00005432, 0x00001000, 0x00004325, 0x00000000}},
1056 /* FSB 167 DIMM 333 */
1057 {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1058 /* FSB 167 DIMM 400 */
1059 {{ 0x00154320, 0x00000000, 0x00065432, 0x00010000}},
1060 /* FSB 200 DIMM 266 */
1061 {{ 0x00000032, 0x00000010, 0x00000120, 0x00000000}},
1062 /* FSB 200 DIMM 333 */
1063 {{ 0x00065432, 0x00010000, 0x00154320, 0x00000000}},
1064 /* FSB 200 DIMM 400 */
1065 {{ 0x00000001, 0x00000000, 0x00000001, 0x00000000}},
1068 static const uint32_t dqs_data[] = {
1069 0xffffffff, 0xffffffff, 0x000000ff,
1070 0xffffffff, 0xffffffff, 0x000000ff,
1071 0xffffffff, 0xffffffff, 0x000000ff,
1072 0xffffffff, 0xffffffff, 0x000000ff,
1073 0xffffffff, 0xffffffff, 0x000000ff,
1074 0xffffffff, 0xffffffff, 0x000000ff,
1075 0xffffffff, 0xffffffff, 0x000000ff,
1076 0xffffffff, 0xffffffff, 0x000000ff};
1078 mask = spd_detect_dimms(ctrl);
1079 print_debug("Starting SDRAM Enable\r\n");
1082 #ifdef DIMM_MAP_LOGICAL
1083 pci_write_config32(ctrl->f0, DRM,
1084 0x00210000 | DIMM_MAP_LOGICAL);
1086 pci_write_config32(ctrl->f0, DRM, 0x00211248);
1088 /* set dram type and Front Side Bus freq. */
1089 drc = spd_set_dram_controller_mode(ctrl, mask);
1091 die("Error calculating DRC\r\n");
1094 data32 = drc & ~(3 << 20); /* clear ECC mode */
1095 data32 = data32 & ~(7 << 8); /* clear refresh rates */
1096 data32 = data32 | (1 << 5); /* temp turn off of ODT */
1097 /* Set gearing, then dram controller mode */
1098 /* drc bits 1:0 = DIMM speed, bits 3:2 = FSB speed */
1099 for(iptr = gearing[(drc&3)+((((drc>>2)&3)-1)*3)].clkgr,cnt=0;
1101 pci_write_config32(ctrl->f0, 0xa0+(cnt*4), iptr[cnt]);
1104 pci_write_config32(ctrl->f0, DRC, data32);
1106 /* turn the clocks on */
1108 pci_write_config16(ctrl->f0, CKDIS, 0x0000);
1110 /* 0x9a DDRCSR Take subsystem out of idle */
1111 data16 = pci_read_config16(ctrl->f0, DDRCSR);
1112 data16 &= ~(7 << 12);
1113 data16 |= (3 << 12); /* use dual channel lock step */
1114 pci_write_config16(ctrl->f0, DDRCSR, data16);
1116 /* program row size DRB */
1117 spd_set_ram_size(ctrl, mask);
1119 /* program page size DRA */
1120 spd_set_row_attributes(ctrl, mask);
1122 /* program DRT timing values */
1123 cas_latency = spd_set_drt_attributes(ctrl, mask, drc);
1125 for(i=0;i<8;i++) { /* loop throught each dimm to test for row */
1126 print_debug("DIMM ");
1127 print_debug_hex8(i);
1128 print_debug("\r\n");
1132 write32(BAR + 0x100, (0x03000000 | (i<<20)));
1134 write32(BAR+0x100, (0x83000000 | (i<<20)));
1136 data32 = read32(BAR+DCALCSR);
1137 while(data32 & 0x80000000)
1138 data32 = read32(BAR+DCALCSR);
1145 for(cs=0;cs<8;cs++) {
1146 write32(BAR + DCALCSR, (0x83000000 | (cs<<20)));
1147 data32 = read32(BAR+DCALCSR);
1148 while(data32 & 0x80000000)
1149 data32 = read32(BAR+DCALCSR);
1152 /* Precharg all banks */
1154 for(cs=0;cs<8;cs++) {
1155 if ((drc & 3) == 2) /* DDR2 */
1156 write32(BAR+DCALADDR, 0x04000000);
1158 write32(BAR+DCALADDR, 0x00000000);
1159 write32(BAR+DCALCSR, (0x83000002 | (cs<<20)));
1160 data32 = read32(BAR+DCALCSR);
1161 while(data32 & 0x80000000)
1162 data32 = read32(BAR+DCALCSR);
1165 /* EMRS dll's enabled */
1167 for(cs=0;cs<8;cs++) {
1168 if ((drc & 3) == 2) /* DDR2 */
1169 /* fixme hard code AL additive latency */
1170 write32(BAR+DCALADDR, 0x0b940001);
1172 write32(BAR+DCALADDR, 0x00000001);
1173 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1174 data32 = read32(BAR+DCALCSR);
1175 while(data32 & 0x80000000)
1176 data32 = read32(BAR+DCALCSR);
1178 /* MRS reset dll's */
1180 if ((drc & 3) == 2) { /* DDR2 */
1181 if(cas_latency == 30)
1182 mode_reg = 0x053a0000;
1184 mode_reg = 0x054a0000;
1187 if(cas_latency == 20)
1188 mode_reg = 0x012a0000;
1189 else /* CAS Latency 2.5 */
1190 mode_reg = 0x016a0000;
1192 for(cs=0;cs<8;cs++) {
1193 write32(BAR+DCALADDR, mode_reg);
1194 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1195 data32 = read32(BAR+DCALCSR);
1196 while(data32 & 0x80000000)
1197 data32 = read32(BAR+DCALCSR);
1200 /* Precharg all banks */
1204 for(cs=0;cs<8;cs++) {
1205 if ((drc & 3) == 2) /* DDR2 */
1206 write32(BAR+DCALADDR, 0x04000000);
1208 write32(BAR+DCALADDR, 0x00000000);
1209 write32(BAR+DCALCSR, (0x83000002 | (cs<<20)));
1210 data32 = read32(BAR+DCALCSR);
1211 while(data32 & 0x80000000)
1212 data32 = read32(BAR+DCALCSR);
1215 /* Do 2 refreshes */
1217 for(cs=0;cs<8;cs++) {
1218 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1219 data32 = read32(BAR+DCALCSR);
1220 while(data32 & 0x80000000)
1221 data32 = read32(BAR+DCALCSR);
1224 for(cs=0;cs<8;cs++) {
1225 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1226 data32 = read32(BAR+DCALCSR);
1227 while(data32 & 0x80000000)
1228 data32 = read32(BAR+DCALCSR);
1231 /* for good luck do 6 more */
1232 for(cs=0;cs<8;cs++) {
1233 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1236 for(cs=0;cs<8;cs++) {
1237 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1240 for(cs=0;cs<8;cs++) {
1241 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1244 for(cs=0;cs<8;cs++) {
1245 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1248 for(cs=0;cs<8;cs++) {
1249 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1252 for(cs=0;cs<8;cs++) {
1253 write32(BAR+DCALCSR, (0x83000001 | (cs<<20)));
1256 /* MRS reset dll's normal */
1258 for(cs=0;cs<8;cs++) {
1259 write32(BAR+DCALADDR, (mode_reg & ~(1<<24)));
1260 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1261 data32 = read32(BAR+DCALCSR);
1262 while(data32 & 0x80000000)
1263 data32 = read32(BAR+DCALCSR);
1266 /* Do only if DDR2 EMRS dll's enabled */
1267 if ((drc & 3) == 2) { /* DDR2 */
1269 for(cs=0;cs<8;cs++) {
1270 write32(BAR+DCALADDR, (0x0b940001));
1271 write32(BAR+DCALCSR, (0x83000003 | (cs<<20)));
1272 data32 = read32(BAR+DCALCSR);
1273 while(data32 & 0x80000000)
1274 data32 = read32(BAR+DCALCSR);
1280 write32(BAR+DCALCSR, 0x0000000f);
1282 /* DDR1 This is test code to copy some codes in the factory setup */
1284 write32(BAR, 0x00100000);
1286 if ((drc & 3) == 2) { /* DDR2 */
1287 /* enable on dimm termination */
1288 set_on_dimm_termination_enable(ctrl);
1291 pci_write_config32(ctrl->f0, 0x88, 0xa0000000 );
1294 /* receive enable calibration */
1295 set_receive_enable(ctrl);
1298 pci_write_config32(ctrl->f0, 0x94, 0x3904a100 );
1299 for(i = 0, cnt = (BAR+0x200); i < 24; i++, cnt+=4) {
1300 write32(cnt, dqs_data[i]);
1302 pci_write_config32(ctrl->f0, 0x94, 0x3904a100 );
1304 /* Enable refresh */
1306 data32 = drc & ~(3 << 20); /* clear ECC mode */
1307 pci_write_config32(ctrl->f0, DRC, data32);
1308 write32(BAR+DCALCSR, 0x0008000f);
1310 /* clear memory and init ECC */
1311 print_debug("Clearing memory\r\n");
1312 for(i=0;i<64;i+=4) {
1313 write32(BAR+DCALDATA+i, 0x00000000);
1316 for(cs=0;cs<8;cs++) {
1317 write32(BAR+DCALCSR, (0x830831d8 | (cs<<20)));
1318 data32 = read32(BAR+DCALCSR);
1319 while(data32 & 0x80000000)
1320 data32 = read32(BAR+DCALCSR);
1323 /* Bring memory subsystem on line */
1324 data32 = pci_read_config32(ctrl->f0, 0x98);
1325 data32 |= (1 << 31);
1326 pci_write_config32(ctrl->f0, 0x98, data32);
1327 /* wait for completion */
1328 print_debug("Waiting for mem complete\r\n");
1330 data32 = pci_read_config32(ctrl->f0, 0x98);
1331 if( (data32 & (1<<31)) == 0)
1334 print_debug("Done\r\n");
1336 /* Set initialization complete */
1339 data32 = drc & ~(3 << 20); /* clear ECC mode */
1340 pci_write_config32(ctrl->f0, DRC, data32);
1342 /* Set the ecc mode */
1343 pci_write_config32(ctrl->f0, DRC, drc);
1345 /* Enable memory scrubbing */
1347 data16 = pci_read_config16(ctrl->f0, MCHSCRB);
1349 data16 |= ((2 << 2) | (2 << 0));
1350 pci_write_config16(ctrl->f0, MCHSCRB, data16);
1352 /* The memory is now setup, use it */
1353 cache_lbmem(MTRR_TYPE_WRBACK);