1 /* This should be done by Eric
2 2004.11 yhlu add 4 rank DIMM support
3 2004.12 yhlu add D0 support
4 2005.02 yhlu add E0 memory hole support
7 #include <cpu/x86/cache.h>
8 #include <cpu/x86/mtrr.h>
13 #if CONFIG_HAVE_OPTION_TABLE
14 #include "option_table.h"
17 #if (CONFIG_RAMTOP & (CONFIG_RAMTOP -1)) != 0
18 # error "CONFIG_RAMTOP must be a power of 2"
21 void setup_resource_map(const unsigned int *register_values, int max)
24 // printk(BIOS_DEBUG, "setting up resource map....");
25 for (i = 0; i < max; i += 3) {
29 dev = register_values[i] & ~0xfff;
30 where = register_values[i] & 0xfff;
31 reg = pci_read_config32(dev, where);
32 reg &= register_values[i+1];
33 reg |= register_values[i+2];
34 pci_write_config32(dev, where, reg);
36 // printk(BIOS_DEBUG, "done.\n");
39 static int controller_present(const struct mem_controller *ctrl)
41 return pci_read_config32(ctrl->f0, 0) == 0x11001022;
44 #if CONFIG_RAMINIT_SYSINFO
45 static void sdram_set_registers(const struct mem_controller *ctrl, struct sys_info *sysinfo)
47 static void sdram_set_registers(const struct mem_controller *ctrl)
50 static const unsigned int register_values[] = {
52 /* Careful set limit registers before base registers which
53 contain the enables */
54 /* DRAM Limit i Registers
63 * [ 2: 0] Destination Node ID
73 * [10: 8] Interleave select
74 * specifies the values of A[14:12] to use with interleave enable.
76 * [31:16] DRAM Limit Address i Bits 39-24
77 * This field defines the upper address bits of a 40 bit address
78 * that define the end of the DRAM region.
80 PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
81 PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
82 PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
83 PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
84 PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
85 PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
86 PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
87 PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
88 /* DRAM Base i Registers
100 * [ 1: 1] Write Enable
101 * 0 = Writes Disabled
104 * [10: 8] Interleave Enable
105 * 000 = No interleave
106 * 001 = Interleave on A[12] (2 nodes)
108 * 011 = Interleave on A[12] and A[14] (4 nodes)
112 * 111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
114 * [13:16] DRAM Base Address i Bits 39-24
115 * This field defines the upper address bits of a 40-bit address
116 * that define the start of the DRAM region.
118 PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
119 PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
120 PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
121 PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
122 PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
123 PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
124 PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
125 PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
127 /* DRAM CS Base Address i Registers
136 * [ 0: 0] Chip-Select Bank Enable
140 * [15: 9] Base Address (19-13)
141 * An optimization used when all DIMM are the same size...
143 * [31:21] Base Address (35-25)
144 * This field defines the top 11 addresses bit of a 40-bit
145 * address that define the memory address space. These
146 * bits decode 32-MByte blocks of memory.
148 PCI_ADDR(0, 0x18, 2, 0x40), 0x001f01fe, 0x00000000,
149 PCI_ADDR(0, 0x18, 2, 0x44), 0x001f01fe, 0x00000000,
150 PCI_ADDR(0, 0x18, 2, 0x48), 0x001f01fe, 0x00000000,
151 PCI_ADDR(0, 0x18, 2, 0x4C), 0x001f01fe, 0x00000000,
152 PCI_ADDR(0, 0x18, 2, 0x50), 0x001f01fe, 0x00000000,
153 PCI_ADDR(0, 0x18, 2, 0x54), 0x001f01fe, 0x00000000,
154 PCI_ADDR(0, 0x18, 2, 0x58), 0x001f01fe, 0x00000000,
155 PCI_ADDR(0, 0x18, 2, 0x5C), 0x001f01fe, 0x00000000,
156 /* DRAM CS Mask Address i Registers
165 * Select bits to exclude from comparison with the DRAM Base address register.
167 * [15: 9] Address Mask (19-13)
168 * Address to be excluded from the optimized case
170 * [29:21] Address Mask (33-25)
171 * The bits with an address mask of 1 are excluded from address comparison
175 PCI_ADDR(0, 0x18, 2, 0x60), 0xC01f01ff, 0x00000000,
176 PCI_ADDR(0, 0x18, 2, 0x64), 0xC01f01ff, 0x00000000,
177 PCI_ADDR(0, 0x18, 2, 0x68), 0xC01f01ff, 0x00000000,
178 PCI_ADDR(0, 0x18, 2, 0x6C), 0xC01f01ff, 0x00000000,
179 PCI_ADDR(0, 0x18, 2, 0x70), 0xC01f01ff, 0x00000000,
180 PCI_ADDR(0, 0x18, 2, 0x74), 0xC01f01ff, 0x00000000,
181 PCI_ADDR(0, 0x18, 2, 0x78), 0xC01f01ff, 0x00000000,
182 PCI_ADDR(0, 0x18, 2, 0x7C), 0xC01f01ff, 0x00000000,
183 /* DRAM Bank Address Mapping Register
185 * Specify the memory module size
190 * 000 = 32Mbyte (Rows = 12 & Col = 8)
191 * 001 = 64Mbyte (Rows = 12 & Col = 9)
192 * 010 = 128Mbyte (Rows = 13 & Col = 9)|(Rows = 12 & Col = 10)
193 * 011 = 256Mbyte (Rows = 13 & Col = 10)|(Rows = 12 & Col = 11)
194 * 100 = 512Mbyte (Rows = 13 & Col = 11)|(Rows = 14 & Col = 10)
195 * 101 = 1Gbyte (Rows = 14 & Col = 11)|(Rows = 13 & Col = 12)
196 * 110 = 2Gbyte (Rows = 14 & Col = 12)
203 PCI_ADDR(0, 0x18, 2, 0x80), 0xffff8888, 0x00000000,
204 /* DRAM Timing Low Register
206 * [ 2: 0] Tcl (Cas# Latency, Cas# to read-data-valid)
216 * [ 7: 4] Trc (Row Cycle Time, Ras#-active to Ras#-active/bank auto refresh)
217 * 0000 = 7 bus clocks
218 * 0001 = 8 bus clocks
220 * 1110 = 21 bus clocks
221 * 1111 = 22 bus clocks
222 * [11: 8] Trfc (Row refresh Cycle time, Auto-refresh-active to RAS#-active or RAS#auto-refresh)
223 * 0000 = 9 bus clocks
224 * 0010 = 10 bus clocks
226 * 1110 = 23 bus clocks
227 * 1111 = 24 bus clocks
228 * [14:12] Trcd (Ras#-active to Case#-read/write Delay)
238 * [18:16] Trrd (Ras# to Ras# Delay)
248 * [23:20] Tras (Minmum Ras# Active Time)
249 * 0000 to 0100 = reserved
250 * 0101 = 5 bus clocks
252 * 1111 = 15 bus clocks
253 * [26:24] Trp (Row Precharge Time)
263 * [28:28] Twr (Write Recovery Time)
268 PCI_ADDR(0, 0x18, 2, 0x88), 0xe8088008, 0x02522001 /* 0x03623125 */ ,
269 /* DRAM Timing High Register
271 * [ 0: 0] Twtr (Write to Read Delay)
275 * [ 6: 4] Trwt (Read to Write Delay)
285 * [12: 8] Tref (Refresh Rate)
286 * 00000 = 100Mhz 4K rows
287 * 00001 = 133Mhz 4K rows
288 * 00010 = 166Mhz 4K rows
289 * 00011 = 200Mhz 4K rows
290 * 01000 = 100Mhz 8K/16K rows
291 * 01001 = 133Mhz 8K/16K rows
292 * 01010 = 166Mhz 8K/16K rows
293 * 01011 = 200Mhz 8K/16K rows
295 * [22:20] Twcl (Write CAS Latency)
296 * 000 = 1 Mem clock after CAS# (Unbuffered Dimms)
297 * 001 = 2 Mem clocks after CAS# (Registered Dimms)
300 PCI_ADDR(0, 0x18, 2, 0x8c), 0xff8fe08e, (0 << 20)|(0 << 8)|(0 << 4)|(0 << 0),
301 /* DRAM Config Low Register
303 * [ 0: 0] DLL Disable
312 * [ 3: 3] Disable DQS Hystersis (FIXME handle this one carefully)
313 * 0 = Enable DQS input filter
314 * 1 = Disable DQS input filtering
317 * 0 = Initialization done or not yet started.
318 * 1 = Initiate DRAM intialization sequence
319 * [ 9: 9] SO-Dimm Enable
321 * 1 = SO-Dimms present
323 * 0 = DRAM not enabled
324 * 1 = DRAM initialized and enabled
325 * [11:11] Memory Clear Status
326 * 0 = Memory Clear function has not completed
327 * 1 = Memory Clear function has completed
328 * [12:12] Exit Self-Refresh
329 * 0 = Exit from self-refresh done or not yet started
330 * 1 = DRAM exiting from self refresh
331 * [13:13] Self-Refresh Status
332 * 0 = Normal Operation
333 * 1 = Self-refresh mode active
334 * [15:14] Read/Write Queue Bypass Count
339 * [16:16] 128-bit/64-Bit
340 * 0 = 64bit Interface to DRAM
341 * 1 = 128bit Interface to DRAM
342 * [17:17] DIMM ECC Enable
343 * 0 = Some DIMMs do not have ECC
344 * 1 = ALL DIMMS have ECC bits
345 * [18:18] UnBuffered DIMMs
347 * 1 = Unbuffered DIMMS
348 * [19:19] Enable 32-Byte Granularity
349 * 0 = Optimize for 64byte bursts
350 * 1 = Optimize for 32byte bursts
351 * [20:20] DIMM 0 is x4
352 * [21:21] DIMM 1 is x4
353 * [22:22] DIMM 2 is x4
354 * [23:23] DIMM 3 is x4
356 * 1 = x4 DIMM present
357 * [24:24] Disable DRAM Receivers
358 * 0 = Receivers enabled
359 * 1 = Receivers disabled
361 * 000 = Arbiters chois is always respected
362 * 001 = Oldest entry in DCQ can be bypassed 1 time
363 * 010 = Oldest entry in DCQ can be bypassed 2 times
364 * 011 = Oldest entry in DCQ can be bypassed 3 times
365 * 100 = Oldest entry in DCQ can be bypassed 4 times
366 * 101 = Oldest entry in DCQ can be bypassed 5 times
367 * 110 = Oldest entry in DCQ can be bypassed 6 times
368 * 111 = Oldest entry in DCQ can be bypassed 7 times
371 PCI_ADDR(0, 0x18, 2, 0x90), 0xf0000000,
373 (0 << 23)|(0 << 22)|(0 << 21)|(0 << 20)|
374 (1 << 19)|(0 << 18)|(1 << 17)|(0 << 16)|
375 (2 << 14)|(0 << 13)|(0 << 12)|
376 (0 << 11)|(0 << 10)|(0 << 9)|(0 << 8)|
377 (0 << 3) |(0 << 1) |(0 << 0),
378 /* DRAM Config High Register
380 * [ 0: 3] Maximum Asynchronous Latency
385 * [11: 8] Read Preamble
403 * [18:16] Idle Cycle Limit
412 * [19:19] Dynamic Idle Cycle Center Enable
413 * 0 = Use Idle Cycle Limit
414 * 1 = Generate a dynamic Idle cycle limit
415 * [22:20] DRAM MEMCLK Frequency
425 * [25:25] Memory Clock Ratio Valid (FIXME carefully enable memclk)
426 * 0 = Disable MemClks
428 * [26:26] Memory Clock 0 Enable
431 * [27:27] Memory Clock 1 Enable
434 * [28:28] Memory Clock 2 Enable
437 * [29:29] Memory Clock 3 Enable
442 PCI_ADDR(0, 0x18, 2, 0x94), 0xc180f0f0,
443 (0 << 29)|(0 << 28)|(0 << 27)|(0 << 26)|(0 << 25)|
444 (0 << 20)|(0 << 19)|(DCH_IDLE_LIMIT_16 << 16)|(0 << 8)|(0 << 0),
445 /* DRAM Delay Line Register
447 * Adjust the skew of the input DQS strobe relative to DATA
449 * [23:16] Delay Line Adjust
450 * Adjusts the DLL derived PDL delay by one or more delay stages
451 * in either the faster or slower direction.
452 * [24:24} Adjust Slower
454 * 1 = Adj is used to increase the PDL delay
455 * [25:25] Adjust Faster
457 * 1 = Adj is used to decrease the PDL delay
460 PCI_ADDR(0, 0x18, 2, 0x98), 0xfc00ffff, 0x00000000,
461 /* MCA NB Status Low reg */
462 PCI_ADDR(0, 0x18, 3, 0x48), 0x00f00000, 0x00000000,
463 /* MCA NB Status high reg */
464 PCI_ADDR(0, 0x18, 3, 0x4c), 0x01801e8c, 0x00000000,
465 /* MCA NB address Low reg */
466 PCI_ADDR(0, 0x18, 3, 0x50), 0x00000007, 0x00000000,
467 /* MCA NB address high reg */
468 PCI_ADDR(0, 0x18, 3, 0x54), 0xffffff00, 0x00000000,
469 /* DRAM Scrub Control Register
471 * [ 4: 0] DRAM Scrube Rate
473 * [12: 8] L2 Scrub Rate
475 * [20:16] Dcache Scrub
478 * 00000 = Do not scrub
500 * All Others = Reserved
502 PCI_ADDR(0, 0x18, 3, 0x58), 0xffe0e0e0, 0x00000000,
503 /* DRAM Scrub Address Low Register
505 * [ 0: 0] DRAM Scrubber Redirect Enable
507 * 1 = Scrubber Corrects errors found in normal operation
509 * [31: 6] DRAM Scrub Address 31-6
511 PCI_ADDR(0, 0x18, 3, 0x5C), 0x0000003e, 0x00000000,
512 /* DRAM Scrub Address High Register
514 * [ 7: 0] DRAM Scrubb Address 39-32
517 PCI_ADDR(0, 0x18, 3, 0x60), 0xffffff00, 0x00000000,
522 if (!controller_present(ctrl)) {
523 // printk(BIOS_DEBUG, "No memory controller present\n");
526 printk(BIOS_SPEW, "setting up CPU%02x northbridge registers\n", ctrl->node_id);
527 max = ARRAY_SIZE(register_values);
528 for (i = 0; i < max; i += 3) {
532 dev = (register_values[i] & ~0xfff) - PCI_DEV(0, 0x18, 0) + ctrl->f0;
533 where = register_values[i] & 0xfff;
534 reg = pci_read_config32(dev, where);
535 reg &= register_values[i+1];
536 reg |= register_values[i+2];
537 pci_write_config32(dev, where, reg);
539 printk(BIOS_SPEW, "done.\n");
542 static void hw_enable_ecc(const struct mem_controller *ctrl)
545 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
546 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
547 dcl &= ~DCL_DimmEccEn;
548 if (nbcap & NBCAP_ECC) {
549 dcl |= DCL_DimmEccEn;
551 if (read_option(CMOS_VSTART_ECC_memory, CMOS_VLEN_ECC_memory, 1) == 0) {
552 dcl &= ~DCL_DimmEccEn;
554 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
557 static int is_dual_channel(const struct mem_controller *ctrl)
560 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
561 return dcl & DCL_128BitEn;
564 static int is_opteron(const struct mem_controller *ctrl)
566 /* Test to see if I am an Opteron. Socket 939 based Athlon64
567 * have dual channel capability, too, so we need a better test
569 * However, all code uses is_opteron() to find out whether to
570 * use dual channel, so if we really check for opteron here, we
571 * need to fix up all code using this function, too.
574 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
575 return !!(nbcap & NBCAP_128Bit);
578 static int is_registered(const struct mem_controller *ctrl)
580 /* Test to see if we are dealing with registered SDRAM.
581 * If we are not registered we are unbuffered.
582 * This function must be called after spd_handle_unbuffered_dimms.
585 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
586 return !(dcl & DCL_UnBuffDimm);
594 #if CONFIG_QRANK_DIMM_SUPPORT
599 static struct dimm_size spd_get_dimm_size(unsigned device)
601 /* Calculate the log base 2 size of a DIMM in bits */
608 #if CONFIG_QRANK_DIMM_SUPPORT
612 /* Note it might be easier to use byte 31 here, it has the DIMM size as
613 * a multiple of 4MB. The way we do it now we can size both
614 * sides of an assymetric dimm.
616 value = spd_read_byte(device, 3); /* rows */
617 if (value < 0) goto hw_err;
618 if ((value & 0xf) == 0) goto val_err;
619 sz.side1 += value & 0xf;
620 sz.rows = value & 0xf;
622 value = spd_read_byte(device, 4); /* columns */
623 if (value < 0) goto hw_err;
624 if ((value & 0xf) == 0) goto val_err;
625 sz.side1 += value & 0xf;
626 sz.col = value & 0xf;
628 value = spd_read_byte(device, 17); /* banks */
629 if (value < 0) goto hw_err;
630 if ((value & 0xff) == 0) goto val_err;
631 sz.side1 += log2(value & 0xff);
633 /* Get the module data width and convert it to a power of two */
634 value = spd_read_byte(device, 7); /* (high byte) */
635 if (value < 0) goto hw_err;
639 low = spd_read_byte(device, 6); /* (low byte) */
640 if (low < 0) goto hw_err;
641 value = value | (low & 0xff);
642 if ((value != 72) && (value != 64)) goto val_err;
643 sz.side1 += log2(value);
646 value = spd_read_byte(device, 5); /* number of physical banks */
647 if (value < 0) goto hw_err;
648 if (value == 1) goto out;
649 if ((value != 2) && (value != 4 )) {
652 #if CONFIG_QRANK_DIMM_SUPPORT
656 /* Start with the symmetrical case */
659 value = spd_read_byte(device, 3); /* rows */
660 if (value < 0) goto hw_err;
661 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
662 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
663 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
665 value = spd_read_byte(device, 4); /* columns */
666 if (value < 0) goto hw_err;
667 if ((value & 0xff) == 0) goto val_err;
668 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
669 sz.side2 += ((value >> 4) & 0x0f); /* Add in columsn on side 2 */
674 die("Bad SPD value\n");
675 /* If an hw_error occurs report that I have no memory */
681 #if CONFIG_QRANK_DIMM_SUPPORT
689 static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz, unsigned index)
691 uint32_t base0, base1;
694 if (sz.side1 != sz.side2) {
698 /* For each base register.
699 * Place the dimm size in 32 MB quantities in the bits 31 - 21.
700 * The initialize dimm size is in bits.
701 * Set the base enable bit0.
706 /* Make certain side1 of the dimm is at least 32MB */
707 if (sz.side1 >= (25 +3)) {
708 base0 = (1 << ((sz.side1 - (25 + 3)) + 21)) | 1;
711 /* Make certain side2 of the dimm is at least 32MB */
712 if (sz.side2 >= (25 + 3)) {
713 base1 = (1 << ((sz.side2 - (25 + 3)) + 21)) | 1;
716 /* Double the size if we are using dual channel memory */
717 if (is_dual_channel(ctrl)) {
718 base0 = (base0 << 1) | (base0 & 1);
719 base1 = (base1 << 1) | (base1 & 1);
722 /* Clear the reserved bits */
723 base0 &= ~0x001ffffe;
724 base1 &= ~0x001ffffe;
726 /* Set the appropriate DIMM base address register */
727 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
728 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
729 #if CONFIG_QRANK_DIMM_SUPPORT
731 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+4)<<2), base0);
732 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+5)<<2), base1);
736 /* Enable the memory clocks for this DIMM */
738 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
739 dch |= DCH_MEMCLK_EN0 << index;
740 #if CONFIG_QRANK_DIMM_SUPPORT
742 dch |= DCH_MEMCLK_EN0 << (index + 2);
745 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
749 static void set_dimm_map(const struct mem_controller *ctrl, struct dimm_size sz, unsigned index)
751 static const unsigned cs_map_aa[] = {
752 /* (row=12, col=8)(14, 12) ---> (0, 0) (2, 4) */
760 map = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
761 map &= ~(0xf << (index * 4));
762 #if CONFIG_QRANK_DIMM_SUPPORT
764 map &= ~(0xf << ( (index + 2) * 4));
769 /* Make certain side1 of the dimm is at least 32MB */
770 if (sz.side1 >= (25 +3)) {
771 if (is_cpu_pre_d0()) {
772 map |= (sz.side1 - (25 + 3)) << (index *4);
773 #if CONFIG_QRANK_DIMM_SUPPORT
775 map |= (sz.side1 - (25 + 3)) << ( (index + 2) * 4);
780 map |= cs_map_aa[(sz.rows - 12) * 5 + (sz.col - 8) ] << (index*4);
781 #if CONFIG_QRANK_DIMM_SUPPORT
783 map |= cs_map_aa[(sz.rows - 12) * 5 + (sz.col - 8) ] << ( (index + 2) * 4);
789 pci_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
793 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
797 for (i = 0; i < DIMM_SOCKETS; i++) {
799 if (!(dimm_mask & (1 << i))) {
802 sz = spd_get_dimm_size(ctrl->channel0[i]);
804 return -1; /* Report SPD error */
806 set_dimm_size(ctrl, sz, i);
807 set_dimm_map (ctrl, sz, i);
812 static void route_dram_accesses(const struct mem_controller *ctrl,
813 unsigned long base_k, unsigned long limit_k)
815 /* Route the addresses to the controller node */
820 unsigned limit_reg, base_reg;
823 node_id = ctrl->node_id;
824 index = (node_id << 3);
825 limit = (limit_k << 2);
828 limit |= ( 0 << 8) | (node_id << 0);
829 base = (base_k << 2);
831 base |= (0 << 8) | (1<<1) | (1<<0);
833 limit_reg = 0x44 + index;
834 base_reg = 0x40 + index;
835 for (device = PCI_DEV(0, 0x18, 1); device <= PCI_DEV(0, 0x1f, 1); device += PCI_DEV(0, 1, 0)) {
836 pci_write_config32(device, limit_reg, limit);
837 pci_write_config32(device, base_reg, base);
841 static void set_top_mem(unsigned tom_k, unsigned hole_startk)
843 /* Error if I don't have memory */
848 /* Report the amount of memory. */
849 printk(BIOS_DEBUG, "RAM end at 0x%08x kB\n", tom_k);
851 /* Now set top of memory */
853 if (tom_k > (4*1024*1024)) {
854 printk(BIOS_SPEW, "Handling memory mapped above 4 GB\n");
855 printk(BIOS_SPEW, "Upper RAM end at 0x%08x kB\n", tom_k);
856 msr.lo = (tom_k & 0x003fffff) << 10;
857 msr.hi = (tom_k & 0xffc00000) >> 22;
858 wrmsr(TOP_MEM2, msr);
859 printk(BIOS_SPEW, "Correcting memory amount mapped below 4 GB\n");
862 /* Leave a 64M hole between TOP_MEM and TOP_MEM2
863 * so I can see my rom chip and other I/O devices.
865 if (tom_k >= 0x003f0000) {
866 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
867 if (hole_startk != 0) {
872 printk(BIOS_SPEW, "Adjusting lower RAM end\n");
874 printk(BIOS_SPEW, "Lower RAM end at 0x%08x kB\n", tom_k);
875 msr.lo = (tom_k & 0x003fffff) << 10;
876 msr.hi = (tom_k & 0xffc00000) >> 22;
880 static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
883 static const uint8_t csbase_low_shift[] = {
886 /* 128MB */ (14 - 4),
887 /* 256MB */ (15 - 4),
888 /* 512MB */ (15 - 4),
893 static const uint8_t csbase_low_d0_shift[] = {
896 /* 128MB */ (14 - 4),
897 /* 128MB */ (15 - 4),
898 /* 256MB */ (15 - 4),
899 /* 512MB */ (15 - 4),
900 /* 256MB */ (16 - 4),
901 /* 512MB */ (16 - 4),
907 /* cs_base_high is not changed */
910 int chip_selects, index;
912 unsigned common_size;
913 unsigned common_cs_mode;
914 uint32_t csbase, csmask;
916 /* See if all of the memory chip selects are the same size
917 * and if so count them.
922 for (index = 0; index < 8; index++) {
927 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
935 if (common_size == 0) {
938 /* The size differed fail */
939 if (common_size != size) {
943 value = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
944 cs_mode =( value >> ((index>>1)*4)) & 0xf;
945 if (cs_mode == 0 ) continue;
946 if (common_cs_mode == 0) {
947 common_cs_mode = cs_mode;
949 /* The cs_mode differed fail */
950 if (common_cs_mode != cs_mode) {
955 /* Chip selects can only be interleaved when there is
956 * more than one and their is a power of two of them.
958 bits = log2(chip_selects);
959 if (((1 << bits) != chip_selects) || (bits < 1) || (bits > 3)) {
963 /* Find the bits of csbase that we need to interleave on */
964 if (is_cpu_pre_d0()){
965 csbase_inc = 1 << csbase_low_shift[common_cs_mode];
966 if (is_dual_channel(ctrl)) {
967 /* Also we run out of address mask bits if we try and interleave 8 4GB dimms */
968 if ((bits == 3) && (common_size == (1 << (32 - 3)))) {
969 // printk(BIOS_DEBUG, "8 4GB chip selects cannot be interleaved\n");
976 csbase_inc = 1 << csbase_low_d0_shift[common_cs_mode];
977 if (is_dual_channel(ctrl)) {
978 if ( (bits==3) && (common_cs_mode > 8)) {
979 // printk(BIOS_DEBUG, "8 cs_mode>8 chip selects cannot be interleaved\n");
986 /* Compute the initial values for csbase and csbask.
987 * In csbase just set the enable bit and the base to zero.
988 * In csmask set the mask bits for the size and page level interleave.
991 csmask = (((common_size << bits) - 1) << 21);
992 csmask |= 0xfe00 & ~((csbase_inc << bits) - csbase_inc);
993 for (index = 0; index < 8; index++) {
996 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1001 pci_write_config32(ctrl->f2, DRAM_CSBASE + (index << 2), csbase);
1002 pci_write_config32(ctrl->f2, DRAM_CSMASK + (index << 2), csmask);
1003 csbase += csbase_inc;
1006 printk(BIOS_SPEW, "Interleaved\n");
1008 /* Return the memory size in K */
1009 return common_size << (15 + bits);
1012 static unsigned long order_chip_selects(const struct mem_controller *ctrl)
1016 /* Remember which registers we have used in the high 8 bits of tom */
1019 /* Find the largest remaining candidate */
1020 unsigned index, candidate;
1021 uint32_t csbase, csmask;
1025 for (index = 0; index < 8; index++) {
1027 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1029 /* Is it enabled? */
1034 /* Is it greater? */
1035 if (value <= csbase) {
1039 /* Has it already been selected */
1040 if (tom & (1 << (index + 24))) {
1043 /* I have a new candidate */
1048 /* See if I have found a new candidate */
1053 /* Remember the dimm size */
1054 size = csbase >> 21;
1056 /* Remember I have used this register */
1057 tom |= (1 << (candidate + 24));
1059 /* Recompute the cs base register value */
1060 csbase = (tom << 21) | 1;
1062 /* Increment the top of memory */
1065 /* Compute the memory mask */
1066 csmask = ((size -1) << 21);
1067 csmask |= 0xfe00; /* For now don't optimize */
1069 /* Write the new base register */
1070 pci_write_config32(ctrl->f2, DRAM_CSBASE + (candidate << 2), csbase);
1071 /* Write the new mask register */
1072 pci_write_config32(ctrl->f2, DRAM_CSMASK + (candidate << 2), csmask);
1075 /* Return the memory size in K */
1076 return (tom & ~0xff000000) << 15;
1079 static unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
1083 /* Find the last memory address used */
1085 for (node_id = 0; node_id < max_node_id; node_id++) {
1086 uint32_t limit, base;
1088 index = node_id << 3;
1089 base = pci_read_config32(ctrl->f1, 0x40 + index);
1090 /* Only look at the limit if the base is enabled */
1091 if ((base & 3) == 3) {
1092 limit = pci_read_config32(ctrl->f1, 0x44 + index);
1093 end_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
1099 static void order_dimms(const struct mem_controller *ctrl)
1101 unsigned long tom_k, base_k;
1103 if (read_option(CMOS_VSTART_interleave_chip_selects, CMOS_VLEN_interleave_chip_selects, 1) != 0) {
1104 tom_k = interleave_chip_selects(ctrl);
1106 printk(BIOS_DEBUG, "Interleaving disabled\n");
1111 tom_k = order_chip_selects(ctrl);
1114 /* Compute the memory base address */
1115 base_k = memory_end_k(ctrl, ctrl->node_id);
1117 route_dram_accesses(ctrl, base_k, tom_k);
1118 set_top_mem(tom_k, 0);
1121 static long disable_dimm(const struct mem_controller *ctrl, unsigned index, long dimm_mask)
1123 printk(BIOS_DEBUG, "disabling dimm %02x\n", index);
1124 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
1125 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
1126 dimm_mask &= ~(1 << index);
1130 static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl,
1136 int has_dualch = is_opteron(ctrl);
1140 for (i = 0; (i < DIMM_SOCKETS); i++) {
1142 if (!(dimm_mask & (1 << i))) {
1145 value = spd_read_byte(ctrl->channel0[i], 21);
1150 /* Registered dimm ? */
1151 if (value & (1 << 1)) {
1154 /* Otherwise it must be an unbuffered dimm */
1159 if (unbuffered && registered) {
1160 die("Mixed buffered and registered dimms not supported");
1163 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1164 dcl &= ~DCL_UnBuffDimm;
1166 if ((has_dualch) && (!is_cpu_pre_d0())) {
1167 dcl |= DCL_UnBuffDimm;
1168 #if CONFIG_CPU_AMD_SOCKET_939
1169 if ((cpuid_eax(1) & 0x30) == 0x30) {
1170 /* CS[7:4] is copy of CS[3:0], should be set for 939 socket */
1171 dcl |= DCL_UpperCSMap;
1175 dcl |= DCL_UnBuffDimm;
1178 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1180 if (is_registered(ctrl)) {
1181 printk(BIOS_SPEW, "Registered\n");
1183 printk(BIOS_SPEW, "Unbuffered\n");
1189 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
1194 for (i = 0; i < DIMM_SOCKETS; i++) {
1197 device = ctrl->channel0[i];
1199 byte = spd_read_byte(ctrl->channel0[i], 2); /* Type */
1201 dimm_mask |= (1 << i);
1204 device = ctrl->channel1[i];
1206 byte = spd_read_byte(ctrl->channel1[i], 2);
1208 dimm_mask |= (1 << (i + DIMM_SOCKETS));
1215 static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_mask)
1219 /* SPD addresses to verify are identical */
1220 static const uint8_t addresses[] = {
1221 2, /* Type should be DDR SDRAM */
1222 3, /* *Row addresses */
1223 4, /* *Column addresses */
1224 5, /* *Physical Banks */
1225 6, /* *Module Data Width low */
1226 7, /* *Module Data Width high */
1227 9, /* *Cycle time at highest CAS Latency CL=X */
1228 11, /* *SDRAM Type */
1229 13, /* *SDRAM Width */
1230 17, /* *Logical Banks */
1231 18, /* *Supported CAS Latencies */
1232 21, /* *SDRAM Module Attributes */
1233 23, /* *Cycle time at CAS Latnecy (CLX - 0.5) */
1234 26, /* *Cycle time at CAS Latnecy (CLX - 1.0) */
1235 27, /* *tRP Row precharge time */
1236 28, /* *Minimum Row Active to Row Active Delay (tRRD) */
1237 29, /* *tRCD RAS to CAS */
1238 30, /* *tRAS Activate to Precharge */
1239 41, /* *Minimum Active to Active/Auto Refresh Time(Trc) */
1240 42, /* *Minimum Auto Refresh Command Time(Trfc) */
1242 /* If the dimms are not in pairs do not do dual channels */
1243 if ((dimm_mask & ((1 << DIMM_SOCKETS) - 1)) !=
1244 ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) {
1245 goto single_channel;
1247 /* If the cpu is not capable of doing dual channels don't do dual channels */
1248 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1249 if (!(nbcap & NBCAP_128Bit)) {
1250 goto single_channel;
1252 for (i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1253 unsigned device0, device1;
1256 /* If I don't have a dimm skip this one */
1257 if (!(dimm_mask & (1 << i))) {
1260 device0 = ctrl->channel0[i];
1261 device1 = ctrl->channel1[i];
1262 for (j = 0; j < ARRAY_SIZE(addresses); j++) {
1264 addr = addresses[j];
1265 value0 = spd_read_byte(device0, addr);
1269 value1 = spd_read_byte(device1, addr);
1273 if (value0 != value1) {
1274 goto single_channel;
1278 printk(BIOS_SPEW, "Enabling dual channel memory\n");
1280 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1281 dcl &= ~DCL_32ByteEn;
1282 dcl |= DCL_128BitEn;
1283 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1286 dimm_mask &= ~((1 << (DIMM_SOCKETS *2)) - (1 << DIMM_SOCKETS));
1292 uint8_t divisor; /* In 1/2 ns increments */
1295 uint32_t dch_memclk;
1296 uint16_t dch_tref4k, dch_tref8k;
1299 uint8_t dtl_trwt[3][3]; /* first index is CAS_LAT 2/2.5/3 and 128/registered64/64 */
1300 uint8_t rdpreamble[4]; /* 0 is for registered, 1 for 1-2 DIMMS, 2 and 3 for 3 or 4 unreg dimm slots */
1304 static const struct mem_param *get_mem_param(unsigned min_cycle_time)
1306 static const struct mem_param speed[] = {
1310 .divisor = (10 <<1),
1313 .dch_memclk = DCH_MEMCLK_100MHZ << DCH_MEMCLK_SHIFT,
1314 .dch_tref4k = DTH_TREF_100MHZ_4K,
1315 .dch_tref8k = DTH_TREF_100MHZ_8K,
1318 .dtl_trwt = { { 2, 2, 3 }, { 3, 3, 4 }, { 3, 3, 4 }},
1319 .rdpreamble = { ((9 << 1) + 0), ((9 << 1) + 0), ((9 << 1) + 0), ((9 << 1) + 0) }
1324 .divisor = (7<<1)+1,
1327 .dch_memclk = DCH_MEMCLK_133MHZ << DCH_MEMCLK_SHIFT,
1328 .dch_tref4k = DTH_TREF_133MHZ_4K,
1329 .dch_tref8k = DTH_TREF_133MHZ_8K,
1332 .dtl_trwt = { { 2, 2, 3 }, { 3, 3, 4 }, { 3, 3, 4 }},
1333 .rdpreamble = { ((8 << 1) + 0), ((7 << 1) + 0), ((7 << 1) + 1), ((7 << 1) + 0) }
1341 .dch_memclk = DCH_MEMCLK_166MHZ << DCH_MEMCLK_SHIFT,
1342 .dch_tref4k = DTH_TREF_166MHZ_4K,
1343 .dch_tref8k = DTH_TREF_166MHZ_8K,
1346 .dtl_trwt = { { 3, 2, 3 }, { 3, 3, 4 }, { 4, 3, 4 }},
1347 .rdpreamble = { ((7 << 1) + 1), ((6 << 1) + 0), ((6 << 1) + 1), ((6 << 1) + 0) }
1355 .dch_memclk = DCH_MEMCLK_200MHZ << DCH_MEMCLK_SHIFT,
1356 .dch_tref4k = DTH_TREF_200MHZ_4K,
1357 .dch_tref8k = DTH_TREF_200MHZ_8K,
1360 .dtl_trwt = { { 0, 2, 3 }, { 3, 3, 4 }, { 3, 3, 4 }},
1361 .rdpreamble = { ((7 << 1) + 0), ((5 << 1) + 0), ((5 << 1) + 1), ((5 << 1) + 1) }
1367 const struct mem_param *param;
1368 for (param = &speed[0]; param->cycle_time ; param++) {
1369 if (min_cycle_time > (param+1)->cycle_time) {
1373 if (!param->cycle_time) {
1374 die("min_cycle_time to low");
1376 printk(BIOS_SPEW, "%s\n", param->name);
1380 struct spd_set_memclk_result {
1381 const struct mem_param *param;
1385 static const unsigned char min_cycle_times[] = {
1386 [NBCAP_MEMCLK_200MHZ] = 0x50, /* 5ns */
1387 [NBCAP_MEMCLK_166MHZ] = 0x60, /* 6ns */
1388 [NBCAP_MEMCLK_133MHZ] = 0x75, /* 7.5ns */
1389 [NBCAP_MEMCLK_100MHZ] = 0xa0, /* 10ns */
1392 #if CONFIG_CPU_AMD_SOCKET_939
1394 /* return the minimum cycle time and set 2T accordingly */
1395 static unsigned int spd_dimm_loading_socket939(const struct mem_controller *ctrl, long dimm_mask) {
1397 /* + 1 raise so we detect 0 as bad field */
1398 #define DDR200 (NBCAP_MEMCLK_100MHZ + 1)
1399 #define DDR333 (NBCAP_MEMCLK_166MHZ + 1)
1400 #define DDR400 (NBCAP_MEMCLK_200MHZ + 1)
1402 #define DDR_MASK 0x3
1404 #define DDR200_2T (DDR_2T | DDR200)
1405 #define DDR333_2T (DDR_2T | DDR333)
1406 #define DDR400_2T (DDR_2T | DDR400)
1409 Following table comes directly from BKDG (unbuffered DIMM support)
1410 [Y][X] Y = ch0_0, ch1_0, ch0_1, ch1_1 1=present 0=empty
1411 X uses same layout but 1 means double rank 0 is single rank/empty
1413 Following tables come from BKDG the ch{0_0,1_0,0_1,1_1} maps to
1414 MEMCS_{1L,1H,2L,2H} in i the PDF. PreE is table 45, and revE table 46.
1417 static const unsigned char dimm_loading_config_preE[16][16] = {
1418 [0x8] = {[0x0] = DDR400,[0x8] = DDR400},
1419 [0x2] = {[0x0] = DDR333,[0x2] = DDR400},
1420 [0xa] = {[0x0] = DDR400_2T,[0x2] = DDR400_2T,
1421 [0x8] = DDR400_2T,[0xa] = DDR333_2T},
1422 [0xc] = {[0x0] = DDR400,[0xc] = DDR400},
1423 [0x3] = {[0x0] = DDR333,[0x3] = DDR400},
1424 [0xf] = {[0x0] = DDR400_2T,[0x3] = DDR400_2T,
1425 [0xc] = DDR400_2T,[0xf] = DDR333_2T},
1428 static const unsigned char dimm_loading_config_revE[16][16] = {
1429 [0x8] = {[0x0] = DDR400, [0x8] = DDR400},
1430 [0x2] = {[0x0] = DDR333, [0x2] = DDR400},
1431 [0x4] = {[0x0] = DDR400, [0x4] = DDR400},
1432 [0x1] = {[0x0] = DDR333, [0x1] = DDR400},
1433 [0xa] = {[0x0] = DDR400_2T, [0x2] = DDR400_2T,
1434 [0x8] = DDR400_2T, [0xa] = DDR333_2T},
1435 [0x5] = {[0x0] = DDR400_2T, [0x1] = DDR400_2T,
1436 [0x4] = DDR400_2T, [0x5] = DDR333_2T},
1437 [0xc] = {[0x0] = DDR400, [0xc] = DDR400, [0x4] = DDR400, [0x8] = DDR400},
1438 [0x3] = {[0x0] = DDR333, [0x1] = DDR333, [0x2] = DDR333, [0x3] = DDR400},
1439 [0xe] = {[0x0] = DDR400_2T, [0x4] = DDR400_2T, [0x2] = DDR400_2T,
1440 [0x6] = DDR400_2T, [0x8] = DDR400_2T, [0xc] = DDR400_2T,
1441 [0xa] = DDR333_2T, [0xe] = DDR333_2T},
1442 [0xb] = {[0x0] = DDR333, [0x1] = DDR400_2T, [0x2] = DDR333_2T,
1443 [0x3] = DDR400_2T, [0x8] = DDR333_2T, [0x9] = DDR400_2T,
1444 [0xa] = DDR333_2T, [0xb] = DDR333_2T},
1445 [0xd] = {[0x0] = DDR400_2T, [0x8] = DDR400_2T, [0x1] = DDR400_2T,
1446 [0x9] = DDR333_2T, [0x4] = DDR400_2T, [0xc] = DDR400_2T,
1447 [0x5] = DDR333_2T, [0xd] = DDR333_2T},
1448 [0x7] = {[0x0] = DDR333, [0x2] = DDR400_2T, [0x1] = DDR333_2T,
1449 [0x3] = DDR400_2T, [0x4] = DDR333_2T, [0x6] = DDR400_2T,
1450 [0x5] = DDR333_2T, [0x7] = DDR333_2T},
1451 [0xf] = {[0x0] = DDR400_2T, [0x1] = DDR400_2T, [0x4] = DDR400_2T,
1452 [0x5] = DDR333_2T, [0x2] = DDR400_2T, [0x3] = DDR400_2T,
1453 [0x6] = DDR400_2T, [0x7] = DDR333_2T, [0x8] = DDR400_2T,
1454 [0x9] = DDR400_2T, [0xc] = DDR400_2T, [0xd] = DDR333_2T,
1455 [0xa] = DDR333_2T, [0xb] = DDR333_2T, [0xe] = DDR333_2T,
1458 /*The dpos matches channel positions defined in BKDG and above arrays
1459 The rpos is bitmask of dual rank dimms in same order as dpos */
1460 unsigned int dloading = 0, dloading_cycle_time, i, rpos = 0, dpos =0;
1461 const unsigned char (*dimm_loading_config)[16] = dimm_loading_config_revE;
1465 if (is_cpu_pre_e0()) {
1466 dimm_loading_config = dimm_loading_config_preE;
1469 /* only DIMMS two per channel */
1470 for (i = 0; i < 2; i++) {
1471 if ((dimm_mask & (1 << i))) {
1472 /* read rank channel 0 */
1473 rank = spd_read_byte(ctrl->channel0[i], 5);
1474 if (rank < 0) goto hw_error;
1475 rpos |= (rank == 2) ? (1 << (3 - (i * 2))) : 0;
1476 dpos |= (1 << (3 - (i * 2)));
1479 if ((dimm_mask & (1 << (i+DIMM_SOCKETS)))) {
1480 /* read rank channel 1*/
1481 rank = spd_read_byte(ctrl->channel1[i], 5);
1482 if (rank < 0) goto hw_error;
1483 rpos |= (rank == 2) ? (1 << (2 - (i * 2))) : 0;
1484 dpos |= (1 << (2 - (i * 2)));
1487 /* now the lookup, decode the max speed DDR400_2T etc */
1488 dloading = dimm_loading_config[dpos][rpos] & DDR_MASK;
1490 printk(BIOS_DEBUG, "XXX %x %x dload %x 2T %x\n", dpos,rpos, dloading, dimm_loading_config[dpos][rpos] & DDR_2T);
1493 if (dloading != 0) {
1494 /* map it back to cycle load times */
1495 dloading_cycle_time = min_cycle_times[dloading - 1];
1496 /* we have valid combination check the restrictions */
1497 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1498 dcl |= (dimm_loading_config[dpos][rpos] & DDR_2T) ? (DCL_En2T) : 0;
1499 /* Set DuallDimm is second channel is completely empty (revD+) */
1500 if (((cpuid_eax(1) & 0xfff0f) >= 0x10f00) && ((dpos & 0x5) == 0)) {
1501 printk(BIOS_DEBUG, "Setting DualDIMMen\n");
1502 dcl |= DCL_DualDIMMen;
1504 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1506 /* if we don't find it we se it to DDR400 */
1507 printk(BIOS_WARNING, "Detected strange DIMM configuration, may not work! (or bug)\n");
1508 dloading_cycle_time = min_cycle_times[NBCAP_MEMCLK_200MHZ];
1511 return dloading_cycle_time;
1514 #endif /* #if CONFIG_CPU_AMD_SOCKET_939 */
1516 static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *ctrl, long dimm_mask)
1518 /* Compute the minimum cycle time for these dimms */
1519 struct spd_set_memclk_result result;
1520 unsigned min_cycle_time, min_latency, bios_cycle_time;
1521 #if CONFIG_CPU_AMD_SOCKET_939
1522 unsigned dloading_cycle_time;
1527 static const uint8_t latency_indicies[] = { 26, 23, 9 };
1529 value = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1531 min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
1532 bios_cycle_time = min_cycle_times[
1533 read_option(CMOS_VSTART_max_mem_clock, CMOS_VLEN_max_mem_clock, 0)];
1534 if (bios_cycle_time > min_cycle_time) {
1535 min_cycle_time = bios_cycle_time;
1539 /* Compute the least latency with the fastest clock supported
1540 * by both the memory controller and the dimms.
1542 for (i = 0; i < DIMM_SOCKETS; i++) {
1543 int new_cycle_time, new_latency;
1548 if (!(dimm_mask & (1 << i))) {
1552 /* First find the supported CAS latencies
1553 * Byte 18 for DDR SDRAM is interpreted:
1554 * bit 0 == CAS Latency = 1.0
1555 * bit 1 == CAS Latency = 1.5
1556 * bit 2 == CAS Latency = 2.0
1557 * bit 3 == CAS Latency = 2.5
1558 * bit 4 == CAS Latency = 3.0
1559 * bit 5 == CAS Latency = 3.5
1563 new_cycle_time = 0xa0;
1566 latencies = spd_read_byte(ctrl->channel0[i], 18);
1567 if (latencies <= 0) continue;
1569 /* Compute the lowest cas latency supported */
1570 latency = log2(latencies) -2;
1572 /* Loop through and find a fast clock with a low latency */
1573 for (index = 0; index < 3; index++, latency++) {
1575 if ((latency < 2) || (latency > 4) ||
1576 (!(latencies & (1 << latency)))) {
1579 spd_value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1580 if (spd_value < 0) {
1584 /* Only increase the latency if we decreas the clock */
1585 if ((spd_value >= min_cycle_time) && (spd_value < new_cycle_time)) {
1586 new_cycle_time = spd_value;
1587 new_latency = latency;
1590 if (new_latency > 4){
1593 /* Does min_latency need to be increased? */
1594 if (new_cycle_time > min_cycle_time) {
1595 min_cycle_time = new_cycle_time;
1597 /* Does min_cycle_time need to be increased? */
1598 if (new_latency > min_latency) {
1599 min_latency = new_latency;
1602 /* Make a second pass through the dimms and disable
1603 * any that cannot support the selected memclk and cas latency.
1606 for (i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1611 if (!(dimm_mask & (1 << i))) {
1615 latencies = spd_read_byte(ctrl->channel0[i], 18);
1616 if (latencies < 0) goto hw_error;
1617 if (latencies == 0) {
1621 /* Compute the lowest cas latency supported */
1622 latency = log2(latencies) -2;
1624 /* Walk through searching for the selected latency */
1625 for (index = 0; index < 3; index++, latency++) {
1626 if (!(latencies & (1 << latency))) {
1629 if (latency == min_latency)
1632 /* If I can't find the latency or my index is bad error */
1633 if ((latency != min_latency) || (index >= 3)) {
1637 /* Read the min_cycle_time for this latency */
1638 spd_value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1639 if (spd_value < 0) goto hw_error;
1641 /* All is good if the selected clock speed
1642 * is what I need or slower.
1644 if (spd_value <= min_cycle_time) {
1647 /* Otherwise I have an error, disable the dimm */
1649 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
1652 //down speed for full load 4 rank support
1653 #if CONFIG_QRANK_DIMM_SUPPORT
1654 if (dimm_mask == (3|(3<<DIMM_SOCKETS)) ) {
1656 for (i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1658 if (!(dimm_mask & (1 << i))) {
1661 val = spd_read_byte(ctrl->channel0[i], 5);
1668 if (min_cycle_time <= 0x50 ) {
1669 min_cycle_time = 0x60;
1677 #if CONFIG_CPU_AMD_SOCKET_939
1678 dloading_cycle_time = spd_dimm_loading_socket939(ctrl, dimm_mask);
1679 if (dloading_cycle_time > min_cycle_time) {
1680 min_cycle_time = dloading_cycle_time;
1681 printk(BIOS_WARNING, "Memory speed reduced due to signal loading conditions\n");
1686 /* Now that I know the minimum cycle time lookup the memory parameters */
1687 result.param = get_mem_param(min_cycle_time);
1689 /* Update DRAM Config High with our selected memory speed */
1690 value = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1691 value &= ~(DCH_MEMCLK_MASK << DCH_MEMCLK_SHIFT);
1693 /* Improves DQS centering by correcting for case when core speed multiplier and MEMCLK speed result in odd clock divisor, by selecting the next lowest memory speed, required only at DDR400 and higher speeds with certain DIMM loadings ---- cheating???*/
1694 if (!is_cpu_pre_e0()) {
1695 if (min_cycle_time==0x50) {
1701 value |= result.param->dch_memclk;
1702 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
1704 static const unsigned latencies[] = { DTL_CL_2, DTL_CL_2_5, DTL_CL_3 };
1706 /* Update DRAM Timing Low with our selected cas latency */
1707 value = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1708 value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
1709 value |= latencies[min_latency - 2] << DTL_TCL_SHIFT;
1710 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
1712 result.dimm_mask = dimm_mask;
1715 result.param = (const struct mem_param *)0;
1716 result.dimm_mask = -1;
1721 static int update_dimm_Trc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1723 unsigned clocks, old_clocks;
1726 value = spd_read_byte(ctrl->channel0[i], 41);
1727 if (value < 0) return -1;
1728 if ((value == 0) || (value == 0xff)) {
1731 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1732 if (clocks < DTL_TRC_MIN) {
1733 clocks = DTL_TRC_MIN;
1735 if (clocks > DTL_TRC_MAX) {
1739 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1740 old_clocks = ((dtl >> DTL_TRC_SHIFT) & DTL_TRC_MASK) + DTL_TRC_BASE;
1741 if (old_clocks > clocks) {
1742 clocks = old_clocks;
1744 dtl &= ~(DTL_TRC_MASK << DTL_TRC_SHIFT);
1745 dtl |= ((clocks - DTL_TRC_BASE) << DTL_TRC_SHIFT);
1746 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1750 static int update_dimm_Trfc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1752 unsigned clocks, old_clocks;
1755 value = spd_read_byte(ctrl->channel0[i], 42);
1756 if (value < 0) return -1;
1757 if ((value == 0) || (value == 0xff)) {
1758 value = param->tRFC;
1760 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1761 if (clocks < DTL_TRFC_MIN) {
1762 clocks = DTL_TRFC_MIN;
1764 if (clocks > DTL_TRFC_MAX) {
1767 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1768 old_clocks = ((dtl >> DTL_TRFC_SHIFT) & DTL_TRFC_MASK) + DTL_TRFC_BASE;
1769 if (old_clocks > clocks) {
1770 clocks = old_clocks;
1772 dtl &= ~(DTL_TRFC_MASK << DTL_TRFC_SHIFT);
1773 dtl |= ((clocks - DTL_TRFC_BASE) << DTL_TRFC_SHIFT);
1774 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1779 static int update_dimm_Trcd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1781 unsigned clocks, old_clocks;
1784 value = spd_read_byte(ctrl->channel0[i], 29);
1785 if (value < 0) return -1;
1786 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1787 if (clocks < DTL_TRCD_MIN) {
1788 clocks = DTL_TRCD_MIN;
1790 if (clocks > DTL_TRCD_MAX) {
1793 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1794 old_clocks = ((dtl >> DTL_TRCD_SHIFT) & DTL_TRCD_MASK) + DTL_TRCD_BASE;
1795 if (old_clocks > clocks) {
1796 clocks = old_clocks;
1798 dtl &= ~(DTL_TRCD_MASK << DTL_TRCD_SHIFT);
1799 dtl |= ((clocks - DTL_TRCD_BASE) << DTL_TRCD_SHIFT);
1800 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1804 static int update_dimm_Trrd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1806 unsigned clocks, old_clocks;
1809 value = spd_read_byte(ctrl->channel0[i], 28);
1810 if (value < 0) return -1;
1811 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1812 if (clocks < DTL_TRRD_MIN) {
1813 clocks = DTL_TRRD_MIN;
1815 if (clocks > DTL_TRRD_MAX) {
1818 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1819 old_clocks = ((dtl >> DTL_TRRD_SHIFT) & DTL_TRRD_MASK) + DTL_TRRD_BASE;
1820 if (old_clocks > clocks) {
1821 clocks = old_clocks;
1823 dtl &= ~(DTL_TRRD_MASK << DTL_TRRD_SHIFT);
1824 dtl |= ((clocks - DTL_TRRD_BASE) << DTL_TRRD_SHIFT);
1825 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1829 static int update_dimm_Tras(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1831 unsigned clocks, old_clocks;
1834 value = spd_read_byte(ctrl->channel0[i], 30);
1835 if (value < 0) return -1;
1836 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1837 if (clocks < DTL_TRAS_MIN) {
1838 clocks = DTL_TRAS_MIN;
1840 if (clocks > DTL_TRAS_MAX) {
1843 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1844 old_clocks = ((dtl >> DTL_TRAS_SHIFT) & DTL_TRAS_MASK) + DTL_TRAS_BASE;
1845 if (old_clocks > clocks) {
1846 clocks = old_clocks;
1848 dtl &= ~(DTL_TRAS_MASK << DTL_TRAS_SHIFT);
1849 dtl |= ((clocks - DTL_TRAS_BASE) << DTL_TRAS_SHIFT);
1850 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1854 static int update_dimm_Trp(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1856 unsigned clocks, old_clocks;
1859 value = spd_read_byte(ctrl->channel0[i], 27);
1860 if (value < 0) return -1;
1861 clocks = (value + (param->divisor << 1) - 1)/(param->divisor << 1);
1862 if (clocks < DTL_TRP_MIN) {
1863 clocks = DTL_TRP_MIN;
1865 if (clocks > DTL_TRP_MAX) {
1868 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1869 old_clocks = ((dtl >> DTL_TRP_SHIFT) & DTL_TRP_MASK) + DTL_TRP_BASE;
1870 if (old_clocks > clocks) {
1871 clocks = old_clocks;
1873 dtl &= ~(DTL_TRP_MASK << DTL_TRP_SHIFT);
1874 dtl |= ((clocks - DTL_TRP_BASE) << DTL_TRP_SHIFT);
1875 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1879 static void set_Twr(const struct mem_controller *ctrl, const struct mem_param *param)
1882 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1883 dtl &= ~(DTL_TWR_MASK << DTL_TWR_SHIFT);
1884 dtl |= (param->dtl_twr - DTL_TWR_BASE) << DTL_TWR_SHIFT;
1885 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1889 static void init_Tref(const struct mem_controller *ctrl, const struct mem_param *param)
1892 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1893 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1894 dth |= (param->dch_tref4k << DTH_TREF_SHIFT);
1895 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1898 static int update_dimm_Tref(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1902 unsigned tref, old_tref;
1903 value = spd_read_byte(ctrl->channel0[i], 3);
1904 if (value < 0) return -1;
1907 tref = param->dch_tref8k;
1909 tref = param->dch_tref4k;
1912 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1913 old_tref = (dth >> DTH_TREF_SHIFT) & DTH_TREF_MASK;
1914 if ((value == 12) && (old_tref == param->dch_tref4k)) {
1915 tref = param->dch_tref4k;
1917 tref = param->dch_tref8k;
1919 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1920 dth |= (tref << DTH_TREF_SHIFT);
1921 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1926 static int update_dimm_x4(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1930 #if CONFIG_QRANK_DIMM_SUPPORT
1934 value = spd_read_byte(ctrl->channel0[i], 13);
1939 #if CONFIG_QRANK_DIMM_SUPPORT
1940 rank = spd_read_byte(ctrl->channel0[i], 5); /* number of physical banks */
1946 dimm = 1<<(DCL_x4DIMM_SHIFT+i);
1947 #if CONFIG_QRANK_DIMM_SUPPORT
1949 dimm |= 1<<(DCL_x4DIMM_SHIFT+i+2);
1952 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1957 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1961 static int update_dimm_ecc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1965 value = spd_read_byte(ctrl->channel0[i], 11);
1970 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1971 dcl &= ~DCL_DimmEccEn;
1972 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1977 static int count_dimms(const struct mem_controller *ctrl)
1982 for (index = 0; index < 8; index += 2) {
1984 csbase = pci_read_config32(ctrl->f2, (DRAM_CSBASE + (index << 2)));
1992 static void set_Twtr(const struct mem_controller *ctrl, const struct mem_param *param)
1996 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1997 dth &= ~(DTH_TWTR_MASK << DTH_TWTR_SHIFT);
1998 dth |= ((param->dtl_twtr - DTH_TWTR_BASE) << DTH_TWTR_SHIFT);
1999 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
2002 static void set_Trwt(const struct mem_controller *ctrl, const struct mem_param *param)
2010 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
2011 latency = (dtl >> DTL_TCL_SHIFT) & DTL_TCL_MASK;
2013 if (is_opteron(ctrl)) {
2014 mtype = 0; /* dual channel */
2015 } else if (is_registered(ctrl)) {
2016 mtype = 1; /* registered 64bit interface */
2018 mtype = 2; /* unbuffered 64bit interface */
2032 die("Unknown LAT for Trwt");
2035 clocks = param->dtl_trwt[lat][mtype];
2036 if ((clocks < DTH_TRWT_MIN) || (clocks > DTH_TRWT_MAX)) {
2037 die("Unknown Trwt\n");
2040 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
2041 dth &= ~(DTH_TRWT_MASK << DTH_TRWT_SHIFT);
2042 dth |= ((clocks - DTH_TRWT_BASE) << DTH_TRWT_SHIFT);
2043 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
2047 static void set_Twcl(const struct mem_controller *ctrl, const struct mem_param *param)
2049 /* Memory Clocks after CAS# */
2052 if (is_registered(ctrl)) {
2057 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
2058 dth &= ~(DTH_TWCL_MASK << DTH_TWCL_SHIFT);
2059 dth |= ((clocks - DTH_TWCL_BASE) << DTH_TWCL_SHIFT);
2060 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
2064 static void set_read_preamble(const struct mem_controller *ctrl, const struct mem_param *param)
2067 unsigned rdpreamble;
2072 for (i = 0; i < 4; i++) {
2073 if (ctrl->channel0[i]) {
2078 /* map to index to param.rdpreamble array */
2079 if (is_registered(ctrl)) {
2081 } else if (slots < 3) {
2083 } else if (slots == 3) {
2085 } else if (slots == 4) {
2088 die("Unknown rdpreamble for this nr of slots");
2091 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2092 dch &= ~(DCH_RDPREAMBLE_MASK << DCH_RDPREAMBLE_SHIFT);
2093 rdpreamble = param->rdpreamble[i];
2095 if ((rdpreamble < DCH_RDPREAMBLE_MIN) || (rdpreamble > DCH_RDPREAMBLE_MAX)) {
2096 die("Unknown rdpreamble");
2099 dch |= (rdpreamble - DCH_RDPREAMBLE_BASE) << DCH_RDPREAMBLE_SHIFT;
2100 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2103 static void set_max_async_latency(const struct mem_controller *ctrl, const struct mem_param *param)
2109 dimms = count_dimms(ctrl);
2111 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2112 dch &= ~(DCH_ASYNC_LAT_MASK << DCH_ASYNC_LAT_SHIFT);
2114 if (is_registered(ctrl)) {
2126 die("Too many unbuffered dimms");
2128 else if (dimms == 3) {
2137 dch |= ((async_lat - DCH_ASYNC_LAT_BASE) << DCH_ASYNC_LAT_SHIFT);
2138 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2141 static void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct mem_param *param)
2144 /* AMD says to Hardcode this */
2145 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2146 dch &= ~(DCH_IDLE_LIMIT_MASK << DCH_IDLE_LIMIT_SHIFT);
2147 dch |= DCH_IDLE_LIMIT_16 << DCH_IDLE_LIMIT_SHIFT;
2148 dch |= DCH_DYN_IDLE_CTR_EN;
2149 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2152 static long spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param, long dimm_mask)
2156 init_Tref(ctrl, param);
2157 for (i = 0; i < DIMM_SOCKETS; i++) {
2159 if (!(dimm_mask & (1 << i))) {
2162 /* DRAM Timing Low Register */
2163 if ((rc = update_dimm_Trc (ctrl, param, i)) <= 0) goto dimm_err;
2164 if ((rc = update_dimm_Trfc(ctrl, param, i)) <= 0) goto dimm_err;
2165 if ((rc = update_dimm_Trcd(ctrl, param, i)) <= 0) goto dimm_err;
2166 if ((rc = update_dimm_Trrd(ctrl, param, i)) <= 0) goto dimm_err;
2167 if ((rc = update_dimm_Tras(ctrl, param, i)) <= 0) goto dimm_err;
2168 if ((rc = update_dimm_Trp (ctrl, param, i)) <= 0) goto dimm_err;
2170 /* DRAM Timing High Register */
2171 if ((rc = update_dimm_Tref(ctrl, param, i)) <= 0) goto dimm_err;
2174 /* DRAM Config Low */
2175 if ((rc = update_dimm_x4 (ctrl, param, i)) <= 0) goto dimm_err;
2176 if ((rc = update_dimm_ecc(ctrl, param, i)) <= 0) goto dimm_err;
2182 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
2184 /* DRAM Timing Low Register */
2185 set_Twr(ctrl, param);
2187 /* DRAM Timing High Register */
2188 set_Twtr(ctrl, param);
2189 set_Trwt(ctrl, param);
2190 set_Twcl(ctrl, param);
2192 /* DRAM Config High */
2193 set_read_preamble(ctrl, param);
2194 set_max_async_latency(ctrl, param);
2195 set_idle_cycle_limit(ctrl, param);
2199 #if CONFIG_RAMINIT_SYSINFO
2200 static void sdram_set_spd_registers(const struct mem_controller *ctrl, struct sys_info *sysinfo)
2202 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
2205 struct spd_set_memclk_result result;
2206 const struct mem_param *param;
2209 if (!controller_present(ctrl)) {
2210 // printk(BIOS_DEBUG, "No memory controller present\n");
2214 hw_enable_ecc(ctrl);
2215 activate_spd_rom(ctrl);
2216 dimm_mask = spd_detect_dimms(ctrl);
2217 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
2218 printk(BIOS_DEBUG, "No memory for this cpu\n");
2221 dimm_mask = spd_enable_2channels(ctrl, dimm_mask);
2224 dimm_mask = spd_set_ram_size(ctrl , dimm_mask);
2227 dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask);
2230 result = spd_set_memclk(ctrl, dimm_mask);
2231 param = result.param;
2232 dimm_mask = result.dimm_mask;
2235 dimm_mask = spd_set_dram_timing(ctrl, param , dimm_mask);
2241 /* Unrecoverable error reading SPD data */
2242 printk(BIOS_ERR, "SPD error - reset\n");
2247 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
2248 static uint32_t hoist_memory(int controllers, const struct mem_controller *ctrl,unsigned hole_startk, int i)
2251 uint32_t carry_over;
2253 uint32_t base, limit;
2258 carry_over = (4*1024*1024) - hole_startk;
2260 for (ii=controllers - 1;ii>i;ii--) {
2261 base = pci_read_config32(ctrl[0].f1, 0x40 + (ii << 3));
2262 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2265 limit = pci_read_config32(ctrl[0].f1, 0x44 + (ii << 3));
2266 for (j = 0; j < controllers; j++) {
2267 pci_write_config32(ctrl[j].f1, 0x44 + (ii << 3), limit + (carry_over << 2));
2268 pci_write_config32(ctrl[j].f1, 0x40 + (ii << 3), base + (carry_over << 2));
2271 limit = pci_read_config32(ctrl[0].f1, 0x44 + (i << 3));
2272 for (j = 0; j < controllers; j++) {
2273 pci_write_config32(ctrl[j].f1, 0x44 + (i << 3), limit + (carry_over << 2));
2276 base = pci_read_config32(dev, 0x40 + (i << 3));
2277 basek = (base & 0xffff0000) >> 2;
2278 if (basek == hole_startk) {
2279 //don't need set memhole here, because hole off set will be 0, overflow
2280 //so need to change base reg instead, new basek will be 4*1024*1024
2282 base |= (4*1024*1024)<<2;
2283 for (j = 0; j < controllers; j++) {
2284 pci_write_config32(ctrl[j].f1, 0x40 + (i<<3), base);
2288 hoist = /* hole start address */
2289 ((hole_startk << 10) & 0xff000000) +
2290 /* hole address to memory controller address */
2291 (((basek + carry_over) >> 6) & 0x0000ff00) +
2294 pci_write_config32(dev, 0xf0, hoist);
2300 static void set_hw_mem_hole(int controllers, const struct mem_controller *ctrl)
2303 uint32_t hole_startk;
2306 hole_startk = 4*1024*1024 - CONFIG_HW_MEM_HOLE_SIZEK;
2308 printk(BIOS_SPEW, "Handling memory hole at 0x%08x (default)\n", hole_startk);
2309 #if CONFIG_HW_MEM_HOLE_SIZE_AUTO_INC == 1
2310 /* We need to double check if hole_startk is valid.
2311 * If it is equal to the dram base address in K (base_k),
2312 * we need to decrease it.
2315 for (i=0; i<controllers; i++) {
2318 base = pci_read_config32(ctrl[0].f1, 0x40 + (i << 3));
2319 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2322 base_k = (base & 0xffff0000) >> 2;
2323 if (base_k == hole_startk) {
2324 /* decrease memory hole startk to make sure it is
2325 * in the middle of the previous node
2327 hole_startk -= (base_k - basek_pri)>>1;
2328 break; /* only one hole */
2333 printk(BIOS_SPEW, "Handling memory hole at 0x%08x (adjusted)\n", hole_startk);
2335 /* Find node number that needs the memory hole configured */
2336 for (i=0; i<controllers; i++) {
2337 uint32_t base, limit;
2338 unsigned base_k, limit_k;
2339 base = pci_read_config32(ctrl[0].f1, 0x40 + (i << 3));
2340 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2343 limit = pci_read_config32(ctrl[0].f1, 0x44 + (i << 3));
2344 base_k = (base & 0xffff0000) >> 2;
2345 limit_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
2346 if ((base_k <= hole_startk) && (limit_k > hole_startk)) {
2348 hoist_memory(controllers, ctrl, hole_startk, i);
2349 end_k = memory_end_k(ctrl, controllers);
2350 set_top_mem(end_k, hole_startk);
2351 break; /* only one hole */
2359 #define TIMEOUT_LOOPS 300000
2360 #if CONFIG_RAMINIT_SYSINFO
2361 static void sdram_enable(int controllers, const struct mem_controller *ctrl, struct sys_info *sysinfo)
2363 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
2368 #if CONFIG_HAVE_ACPI_RESUME == 1
2369 int suspend = acpi_is_wakeup_early();
2374 /* Error if I don't have memory */
2375 if (memory_end_k(ctrl, controllers) == 0) {
2379 /* Before enabling memory start the memory clocks */
2380 for (i = 0; i < controllers; i++) {
2382 if (!controller_present(ctrl + i))
2384 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2385 if (dch & (DCH_MEMCLK_EN0|DCH_MEMCLK_EN1|DCH_MEMCLK_EN2|DCH_MEMCLK_EN3)) {
2386 dch |= DCH_MEMCLK_VALID;
2387 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2390 /* Disable dram receivers */
2392 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2393 dcl |= DCL_DisInRcvrs;
2394 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2398 /* We need to wait a minimum of 20 MEMCLKS to enable the InitDram */
2399 /* And if necessary toggle the the reset on the dimms by hand */
2400 memreset(controllers, ctrl);
2402 for (i = 0; i < controllers; i++) {
2404 if (!controller_present(ctrl + i))
2406 /* Skip everything if I don't have any memory on this controller */
2407 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2408 if (!(dch & DCH_MEMCLK_VALID)) {
2412 /* Toggle DisDqsHys to get it working */
2413 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2414 if (dcl & DCL_DimmEccEn) {
2416 printk(BIOS_SPEW, "ECC enabled\n");
2417 mnc = pci_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
2419 if (dcl & DCL_128BitEn) {
2420 mnc |= MNC_CHIPKILL_EN;
2422 pci_write_config32(ctrl[i].f3, MCA_NB_CONFIG, mnc);
2426 dcl |= DCL_DisDqsHys;
2427 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2429 dcl &= ~DCL_DisDqsHys;
2430 dcl &= ~DCL_DLL_Disable;
2437 dcl |= (DCL_ESR | DCL_SRS);
2438 /* Handle errata 85 Insufficient Delay Between MEMCLK Startup
2439 and CKE Assertion During Resume From S3 */
2440 udelay(10); /* for unregistered */
2441 if (is_registered(&ctrl[i])) {
2442 udelay(100); /* 110us for registered (we wait 10us already) */
2446 dcl |= DCL_DramInit;
2447 whatWait = DCL_DramInit;
2449 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2452 for (i = 0; i < controllers; i++) {
2454 if (!controller_present(ctrl + i))
2456 /* Skip everything if I don't have any memory on this controller */
2457 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2458 if (!(dch & DCH_MEMCLK_VALID)) {
2462 printk(BIOS_DEBUG, "Initializing memory: ");
2465 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2467 if ((loops & 1023) == 0) {
2468 printk(BIOS_DEBUG, ".");
2470 } while(((dcl & whatWait) != 0) && (loops < TIMEOUT_LOOPS));
2471 if (loops >= TIMEOUT_LOOPS) {
2472 printk(BIOS_DEBUG, " failed\n");
2476 if (!is_cpu_pre_c0()) {
2477 /* Wait until it is safe to touch memory */
2479 /* the registers are marked read-only but code zeros them */
2480 dcl &= ~(DCL_MemClrStatus | DCL_DramEnable);
2481 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2484 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2485 } while(((dcl & DCL_MemClrStatus) == 0) || ((dcl & DCL_DramEnable) == 0) ||
2489 printk(BIOS_DEBUG, " done\n");
2492 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
2493 // init hw mem hole here
2494 /* DramHoleValid bit only can be set after MemClrStatus is set by Hardware */
2495 if (!is_cpu_pre_e0())
2496 set_hw_mem_hole(controllers, ctrl);
2499 //FIXME add enable node interleaving here -- yhlu
2501 1. check how many nodes we have , if not all has ram installed get out
2502 2. check cs_base lo is 0, node 0 f2 0x40,,,,, if any one is not using lo is CS_BASE, get out
2503 3. check if other node is the same as node 0 about f2 0x40,,,,, otherwise get out
2504 4. if all ready enable node_interleaving in f1 0x40..... of every node
2505 5. for node interleaving we need to set mem hole to every node ( need recalcute hole offset in f0 for every node)
2510 static void set_sysinfo_in_ram(unsigned val)
2514 void fill_mem_ctrl(int controllers, struct mem_controller *ctrl_a,
2515 const uint16_t *spd_addr)
2519 struct mem_controller *ctrl;
2520 for (i=0;i<controllers; i++) {
2523 ctrl->f0 = PCI_DEV(0, 0x18+i, 0);
2524 ctrl->f1 = PCI_DEV(0, 0x18+i, 1);
2525 ctrl->f2 = PCI_DEV(0, 0x18+i, 2);
2526 ctrl->f3 = PCI_DEV(0, 0x18+i, 3);
2528 if (spd_addr == (void *)0) continue;
2530 for (j=0;j<DIMM_SOCKETS;j++) {
2531 ctrl->channel0[j] = spd_addr[(i*2+0)*DIMM_SOCKETS + j];
2532 ctrl->channel1[j] = spd_addr[(i*2+1)*DIMM_SOCKETS + j];