1 #include <cpu/x86/mem.h>
2 #include <cpu/x86/cache.h>
3 #include <cpu/x86/mtrr.h>
7 #if (CONFIG_LB_MEM_TOPK & (CONFIG_LB_MEM_TOPK -1)) != 0
8 # error "CONFIG_LB_MEM_TOPK must be a power of 2"
10 static void setup_resource_map(const unsigned int *register_values, int max)
13 print_debug("setting up resource map....");
17 for(i = 0; i < max; i += 3) {
22 print_debug_hex32(register_values[i]);
24 print_debug_hex32(register_values[i+2]);
27 dev = register_values[i] & ~0xff;
28 where = register_values[i] & 0xff;
29 reg = pci_read_config32(dev, where);
30 reg &= register_values[i+1];
31 reg |= register_values[i+2];
32 pci_write_config32(dev, where, reg);
34 reg = pci_read_config32(register_values[i]);
35 reg &= register_values[i+1];
36 reg |= register_values[i+2] & ~register_values[i+1];
37 pci_write_config32(register_values[i], reg);
40 print_debug("done.\r\n");
43 static void sdram_set_registers(const struct mem_controller *ctrl)
45 static const unsigned int register_values[] = {
47 /* Careful set limit registers before base registers which contain the enables */
48 /* DRAM Limit i Registers
57 * [ 2: 0] Destination Node ID
67 * [10: 8] Interleave select
68 * specifies the values of A[14:12] to use with interleave enable.
70 * [31:16] DRAM Limit Address i Bits 39-24
71 * This field defines the upper address bits of a 40 bit address
72 * that define the end of the DRAM region.
74 PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
75 PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
76 PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
77 PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
78 PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
79 PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
80 PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
81 PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
82 /* DRAM Base i Registers
94 * [ 1: 1] Write Enable
98 * [10: 8] Interleave Enable
100 * 001 = Interleave on A[12] (2 nodes)
102 * 011 = Interleave on A[12] and A[14] (4 nodes)
106 * 111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
108 * [13:16] DRAM Base Address i Bits 39-24
109 * This field defines the upper address bits of a 40-bit address
110 * that define the start of the DRAM region.
112 PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
113 PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
114 PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
115 PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
116 PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
117 PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
118 PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
119 PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
121 /* DRAM CS Base Address i Registers
130 * [ 0: 0] Chip-Select Bank Enable
134 * [15: 9] Base Address (19-13)
135 * An optimization used when all DIMM are the same size...
137 * [31:21] Base Address (35-25)
138 * This field defines the top 11 addresses bit of a 40-bit
139 * address that define the memory address space. These
140 * bits decode 32-MByte blocks of memory.
142 PCI_ADDR(0, 0x18, 2, 0x40), 0x001f01fe, 0x00000000,
143 PCI_ADDR(0, 0x18, 2, 0x44), 0x001f01fe, 0x00000000,
144 PCI_ADDR(0, 0x18, 2, 0x48), 0x001f01fe, 0x00000000,
145 PCI_ADDR(0, 0x18, 2, 0x4C), 0x001f01fe, 0x00000000,
146 PCI_ADDR(0, 0x18, 2, 0x50), 0x001f01fe, 0x00000000,
147 PCI_ADDR(0, 0x18, 2, 0x54), 0x001f01fe, 0x00000000,
148 PCI_ADDR(0, 0x18, 2, 0x58), 0x001f01fe, 0x00000000,
149 PCI_ADDR(0, 0x18, 2, 0x5C), 0x001f01fe, 0x00000000,
150 /* DRAM CS Mask Address i Registers
159 * Select bits to exclude from comparison with the DRAM Base address register.
161 * [15: 9] Address Mask (19-13)
162 * Address to be excluded from the optimized case
164 * [29:21] Address Mask (33-25)
165 * The bits with an address mask of 1 are excluded from address comparison
169 PCI_ADDR(0, 0x18, 2, 0x60), 0xC01f01ff, 0x00000000,
170 PCI_ADDR(0, 0x18, 2, 0x64), 0xC01f01ff, 0x00000000,
171 PCI_ADDR(0, 0x18, 2, 0x68), 0xC01f01ff, 0x00000000,
172 PCI_ADDR(0, 0x18, 2, 0x6C), 0xC01f01ff, 0x00000000,
173 PCI_ADDR(0, 0x18, 2, 0x70), 0xC01f01ff, 0x00000000,
174 PCI_ADDR(0, 0x18, 2, 0x74), 0xC01f01ff, 0x00000000,
175 PCI_ADDR(0, 0x18, 2, 0x78), 0xC01f01ff, 0x00000000,
176 PCI_ADDR(0, 0x18, 2, 0x7C), 0xC01f01ff, 0x00000000,
177 /* DRAM Bank Address Mapping Register
179 * Specify the memory module size
184 * 000 = 32Mbyte (Rows = 12 & Col = 8)
185 * 001 = 64Mbyte (Rows = 12 & Col = 9)
186 * 010 = 128Mbyte (Rows = 13 & Col = 9)|(Rows = 12 & Col = 10)
187 * 011 = 256Mbyte (Rows = 13 & Col = 10)|(Rows = 12 & Col = 11)
188 * 100 = 512Mbyte (Rows = 13 & Col = 11)|(Rows = 14 & Col = 10)
189 * 101 = 1Gbyte (Rows = 14 & Col = 11)|(Rows = 13 & Col = 12)
190 * 110 = 2Gbyte (Rows = 14 & Col = 12)
197 PCI_ADDR(0, 0x18, 2, 0x80), 0xffff8888, 0x00000000,
198 /* DRAM Timing Low Register
200 * [ 2: 0] Tcl (Cas# Latency, Cas# to read-data-valid)
210 * [ 7: 4] Trc (Row Cycle Time, Ras#-active to Ras#-active/bank auto refresh)
211 * 0000 = 7 bus clocks
212 * 0001 = 8 bus clocks
214 * 1110 = 21 bus clocks
215 * 1111 = 22 bus clocks
216 * [11: 8] Trfc (Row refresh Cycle time, Auto-refresh-active to RAS#-active or RAS#auto-refresh)
217 * 0000 = 9 bus clocks
218 * 0010 = 10 bus clocks
220 * 1110 = 23 bus clocks
221 * 1111 = 24 bus clocks
222 * [14:12] Trcd (Ras#-active to Case#-read/write Delay)
232 * [18:16] Trrd (Ras# to Ras# Delay)
242 * [23:20] Tras (Minmum Ras# Active Time)
243 * 0000 to 0100 = reserved
244 * 0101 = 5 bus clocks
246 * 1111 = 15 bus clocks
247 * [26:24] Trp (Row Precharge Time)
257 * [28:28] Twr (Write Recovery Time)
262 PCI_ADDR(0, 0x18, 2, 0x88), 0xe8088008, 0x02522001 /* 0x03623125 */ ,
263 /* DRAM Timing High Register
265 * [ 0: 0] Twtr (Write to Read Delay)
269 * [ 6: 4] Trwt (Read to Write Delay)
279 * [12: 8] Tref (Refresh Rate)
280 * 00000 = 100Mhz 4K rows
281 * 00001 = 133Mhz 4K rows
282 * 00010 = 166Mhz 4K rows
283 * 00011 = 200Mhz 4K rows
284 * 01000 = 100Mhz 8K/16K rows
285 * 01001 = 133Mhz 8K/16K rows
286 * 01010 = 166Mhz 8K/16K rows
287 * 01011 = 200Mhz 8K/16K rows
289 * [22:20] Twcl (Write CAS Latency)
290 * 000 = 1 Mem clock after CAS# (Unbuffered Dimms)
291 * 001 = 2 Mem clocks after CAS# (Registered Dimms)
294 PCI_ADDR(0, 0x18, 2, 0x8c), 0xff8fe08e, (0 << 20)|(0 << 8)|(0 << 4)|(0 << 0),
295 /* DRAM Config Low Register
297 * [ 0: 0] DLL Disable
306 * [ 3: 3] Disable DQS Hystersis (FIXME handle this one carefully)
307 * 0 = Enable DQS input filter
308 * 1 = Disable DQS input filtering
311 * 0 = Initialization done or not yet started.
312 * 1 = Initiate DRAM intialization sequence
313 * [ 9: 9] SO-Dimm Enable
315 * 1 = SO-Dimms present
317 * 0 = DRAM not enabled
318 * 1 = DRAM initialized and enabled
319 * [11:11] Memory Clear Status
320 * 0 = Memory Clear function has not completed
321 * 1 = Memory Clear function has completed
322 * [12:12] Exit Self-Refresh
323 * 0 = Exit from self-refresh done or not yet started
324 * 1 = DRAM exiting from self refresh
325 * [13:13] Self-Refresh Status
326 * 0 = Normal Operation
327 * 1 = Self-refresh mode active
328 * [15:14] Read/Write Queue Bypass Count
333 * [16:16] 128-bit/64-Bit
334 * 0 = 64bit Interface to DRAM
335 * 1 = 128bit Interface to DRAM
336 * [17:17] DIMM ECC Enable
337 * 0 = Some DIMMs do not have ECC
338 * 1 = ALL DIMMS have ECC bits
339 * [18:18] UnBuffered DIMMs
341 * 1 = Unbuffered DIMMS
342 * [19:19] Enable 32-Byte Granularity
343 * 0 = Optimize for 64byte bursts
344 * 1 = Optimize for 32byte bursts
345 * [20:20] DIMM 0 is x4
346 * [21:21] DIMM 1 is x4
347 * [22:22] DIMM 2 is x4
348 * [23:23] DIMM 3 is x4
350 * 1 = x4 DIMM present
351 * [24:24] Disable DRAM Receivers
352 * 0 = Receivers enabled
353 * 1 = Receivers disabled
355 * 000 = Arbiters chois is always respected
356 * 001 = Oldest entry in DCQ can be bypassed 1 time
357 * 010 = Oldest entry in DCQ can be bypassed 2 times
358 * 011 = Oldest entry in DCQ can be bypassed 3 times
359 * 100 = Oldest entry in DCQ can be bypassed 4 times
360 * 101 = Oldest entry in DCQ can be bypassed 5 times
361 * 110 = Oldest entry in DCQ can be bypassed 6 times
362 * 111 = Oldest entry in DCQ can be bypassed 7 times
365 PCI_ADDR(0, 0x18, 2, 0x90), 0xf0000000,
367 (0 << 23)|(0 << 22)|(0 << 21)|(0 << 20)|
368 (1 << 19)|(0 << 18)|(1 << 17)|(0 << 16)|
369 (2 << 14)|(0 << 13)|(0 << 12)|
370 (0 << 11)|(0 << 10)|(0 << 9)|(0 << 8)|
371 (0 << 3) |(0 << 1) |(0 << 0),
372 /* DRAM Config High Register
374 * [ 0: 3] Maximum Asynchronous Latency
379 * [11: 8] Read Preamble
397 * [18:16] Idle Cycle Limit
406 * [19:19] Dynamic Idle Cycle Center Enable
407 * 0 = Use Idle Cycle Limit
408 * 1 = Generate a dynamic Idle cycle limit
409 * [22:20] DRAM MEMCLK Frequency
419 * [25:25] Memory Clock Ratio Valid (FIXME carefully enable memclk)
420 * 0 = Disable MemClks
422 * [26:26] Memory Clock 0 Enable
425 * [27:27] Memory Clock 1 Enable
428 * [28:28] Memory Clock 2 Enable
431 * [29:29] Memory Clock 3 Enable
436 PCI_ADDR(0, 0x18, 2, 0x94), 0xc180f0f0,
437 (0 << 29)|(0 << 28)|(0 << 27)|(0 << 26)|(0 << 25)|
438 (0 << 20)|(0 << 19)|(DCH_IDLE_LIMIT_16 << 16)|(0 << 8)|(0 << 0),
439 /* DRAM Delay Line Register
441 * Adjust the skew of the input DQS strobe relative to DATA
443 * [23:16] Delay Line Adjust
444 * Adjusts the DLL derived PDL delay by one or more delay stages
445 * in either the faster or slower direction.
446 * [24:24} Adjust Slower
448 * 1 = Adj is used to increase the PDL delay
449 * [25:25] Adjust Faster
451 * 1 = Adj is used to decrease the PDL delay
454 PCI_ADDR(0, 0x18, 2, 0x98), 0xfc00ffff, 0x00000000,
455 /* DRAM Scrub Control Register
457 * [ 4: 0] DRAM Scrube Rate
459 * [12: 8] L2 Scrub Rate
461 * [20:16] Dcache Scrub
464 * 00000 = Do not scrub
486 * All Others = Reserved
488 PCI_ADDR(0, 0x18, 3, 0x58), 0xffe0e0e0, 0x00000000,
489 /* DRAM Scrub Address Low Register
491 * [ 0: 0] DRAM Scrubber Redirect Enable
493 * 1 = Scrubber Corrects errors found in normal operation
495 * [31: 6] DRAM Scrub Address 31-6
497 PCI_ADDR(0, 0x18, 3, 0x5C), 0x0000003e, 0x00000000,
498 /* DRAM Scrub Address High Register
500 * [ 7: 0] DRAM Scrubb Address 39-32
503 PCI_ADDR(0, 0x18, 3, 0x60), 0xffffff00, 0x00000000,
507 print_spew("setting up CPU");
508 print_spew_hex8(ctrl->node_id);
509 print_spew(" northbridge registers\r\n");
510 max = sizeof(register_values)/sizeof(register_values[0]);
511 for(i = 0; i < max; i += 3) {
516 print_spew_hex32(register_values[i]);
518 print_spew_hex32(register_values[i+2]);
521 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x18, 0) + ctrl->f0;
522 where = register_values[i] & 0xff;
523 reg = pci_read_config32(dev, where);
524 reg &= register_values[i+1];
525 reg |= register_values[i+2];
526 pci_write_config32(dev, where, reg);
529 reg = pci_read_config32(register_values[i]);
530 reg &= register_values[i+1];
531 reg |= register_values[i+2];
532 pci_write_config32(register_values[i], reg);
535 print_spew("done.\r\n");
539 static void hw_enable_ecc(const struct mem_controller *ctrl)
542 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
543 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
544 dcl &= ~DCL_DimmEccEn;
545 if (nbcap & NBCAP_ECC) {
546 dcl |= DCL_DimmEccEn;
548 if (read_option(CMOS_VSTART_ECC_memory, CMOS_VLEN_ECC_memory, 1) == 0) {
549 dcl &= ~DCL_DimmEccEn;
551 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
555 static int is_dual_channel(const struct mem_controller *ctrl)
558 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
559 return dcl & DCL_128BitEn;
562 static int is_opteron(const struct mem_controller *ctrl)
564 /* Test to see if I am an Opteron.
565 * FIXME Testing dual channel capability is correct for now
566 * but a beter test is probably required.
568 #warning "FIXME implement a better test for opterons"
570 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
571 return !!(nbcap & NBCAP_128Bit);
574 static int is_registered(const struct mem_controller *ctrl)
576 /* Test to see if we are dealing with registered SDRAM.
577 * If we are not registered we are unbuffered.
578 * This function must be called after spd_handle_unbuffered_dimms.
581 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
582 return !(dcl & DCL_UnBufDimm);
590 static struct dimm_size spd_get_dimm_size(unsigned device)
592 /* Calculate the log base 2 size of a DIMM in bits */
598 /* Note it might be easier to use byte 31 here, it has the DIMM size as
599 * a multiple of 4MB. The way we do it now we can size both
600 * sides of an assymetric dimm.
602 value = spd_read_byte(device, 3); /* rows */
603 if (value < 0) goto hw_err;
604 if ((value & 0xf) == 0) goto val_err;
605 sz.side1 += value & 0xf;
607 value = spd_read_byte(device, 4); /* columns */
608 if (value < 0) goto hw_err;
609 if ((value & 0xf) == 0) goto val_err;
610 sz.side1 += value & 0xf;
612 value = spd_read_byte(device, 17); /* banks */
613 if (value < 0) goto hw_err;
614 if ((value & 0xff) == 0) goto val_err;
615 sz.side1 += log2(value & 0xff);
617 /* Get the module data width and convert it to a power of two */
618 value = spd_read_byte(device, 7); /* (high byte) */
619 if (value < 0) goto hw_err;
623 low = spd_read_byte(device, 6); /* (low byte) */
624 if (low < 0) goto hw_err;
625 value = value | (low & 0xff);
626 if ((value != 72) && (value != 64)) goto val_err;
627 sz.side1 += log2(value);
630 value = spd_read_byte(device, 5); /* number of physical banks */
631 if (value < 0) goto hw_err;
632 if (value == 1) goto out;
633 if (value != 2) goto val_err;
635 /* Start with the symmetrical case */
638 value = spd_read_byte(device, 3); /* rows */
639 if (value < 0) goto hw_err;
640 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
641 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
642 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
644 value = spd_read_byte(device, 4); /* columns */
645 if (value < 0) goto hw_err;
646 if ((value & 0xff) == 0) goto val_err;
647 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
648 sz.side2 += ((value >> 4) & 0x0f); /* Add in columsn on side 2 */
652 die("Bad SPD value\r\n");
653 /* If an hw_error occurs report that I have no memory */
661 static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz, unsigned index)
663 uint32_t base0, base1, map;
666 if (sz.side1 != sz.side2) {
669 map = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
670 map &= ~(0xf << (index * 4));
672 /* For each base register.
673 * Place the dimm size in 32 MB quantities in the bits 31 - 21.
674 * The initialize dimm size is in bits.
675 * Set the base enable bit0.
680 /* Make certain side1 of the dimm is at least 32MB */
681 if (sz.side1 >= (25 +3)) {
682 map |= (sz.side1 - (25 + 3)) << (index *4);
683 base0 = (1 << ((sz.side1 - (25 + 3)) + 21)) | 1;
685 /* Make certain side2 of the dimm is at least 32MB */
686 if (sz.side2 >= (25 + 3)) {
687 base1 = (1 << ((sz.side2 - (25 + 3)) + 21)) | 1;
690 /* Double the size if we are using dual channel memory */
691 if (is_dual_channel(ctrl)) {
692 base0 = (base0 << 1) | (base0 & 1);
693 base1 = (base1 << 1) | (base1 & 1);
696 /* Clear the reserved bits */
697 base0 &= ~0x001ffffe;
698 base1 &= ~0x001ffffe;
700 /* Set the appropriate DIMM base address register */
701 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
702 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
703 pci_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
705 /* Enable the memory clocks for this DIMM */
707 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
708 dch |= DCH_MEMCLK_EN0 << index;
709 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
713 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
717 for(i = 0; i < DIMM_SOCKETS; i++) {
719 if (!(dimm_mask & (1 << i))) {
722 sz = spd_get_dimm_size(ctrl->channel0[i]);
724 return -1; /* Report SPD error */
726 set_dimm_size(ctrl, sz, i);
731 static void route_dram_accesses(const struct mem_controller *ctrl,
732 unsigned long base_k, unsigned long limit_k)
734 /* Route the addresses to the controller node */
739 unsigned limit_reg, base_reg;
742 node_id = ctrl->node_id;
743 index = (node_id << 3);
744 limit = (limit_k << 2);
747 limit |= ( 0 << 8) | (node_id << 0);
748 base = (base_k << 2);
750 base |= (0 << 8) | (1<<1) | (1<<0);
752 limit_reg = 0x44 + index;
753 base_reg = 0x40 + index;
754 for(device = PCI_DEV(0, 0x18, 1); device <= PCI_DEV(0, 0x1f, 1); device += PCI_DEV(0, 1, 0)) {
755 pci_write_config32(device, limit_reg, limit);
756 pci_write_config32(device, base_reg, base);
760 static void set_top_mem(unsigned tom_k)
762 /* Error if I don't have memory */
767 /* Report the amount of memory. */
768 print_spew("RAM: 0x");
769 print_spew_hex32(tom_k);
770 print_spew(" KB\r\n");
772 /* Now set top of memory */
774 msr.lo = (tom_k & 0x003fffff) << 10;
775 msr.hi = (tom_k & 0xffc00000) >> 22;
776 wrmsr(TOP_MEM2, msr);
778 /* Leave a 64M hole between TOP_MEM and TOP_MEM2
779 * so I can see my rom chip and other I/O devices.
781 if (tom_k >= 0x003f0000) {
784 msr.lo = (tom_k & 0x003fffff) << 10;
785 msr.hi = (tom_k & 0xffc00000) >> 22;
789 static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
792 static const uint32_t csbase_low[] = {
793 /* 32MB */ (1 << (13 - 4)),
794 /* 64MB */ (1 << (14 - 4)),
795 /* 128MB */ (1 << (14 - 4)),
796 /* 256MB */ (1 << (15 - 4)),
797 /* 512MB */ (1 << (15 - 4)),
798 /* 1GB */ (1 << (16 - 4)),
799 /* 2GB */ (1 << (16 - 4)),
802 int chip_selects, index;
804 unsigned common_size;
805 uint32_t csbase, csmask;
807 /* See if all of the memory chip selects are the same size
808 * and if so count them.
812 for(index = 0; index < 8; index++) {
816 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
824 if (common_size == 0) {
827 /* The size differed fail */
828 if (common_size != size) {
832 /* Chip selects can only be interleaved when there is
833 * more than one and their is a power of two of them.
835 bits = log2(chip_selects);
836 if (((1 << bits) != chip_selects) || (bits < 1) || (bits > 3)) {
840 /* Also we run out of address mask bits if we try and interleave 8 4GB dimms */
841 if ((bits == 3) && (common_size == (1 << (32 - 3)))) {
842 print_debug("8 4GB chip selects cannot be interleaved\r\n");
845 /* Find the bits of csbase that we need to interleave on */
846 if (is_dual_channel(ctrl)) {
847 csbase_inc = csbase_low[log2(common_size) - 1] << 1;
849 csbase_inc = csbase_low[log2(common_size)];
851 /* Compute the initial values for csbase and csbask.
852 * In csbase just set the enable bit and the base to zero.
853 * In csmask set the mask bits for the size and page level interleave.
856 csmask = (((common_size << bits) - 1) << 21);
857 csmask |= 0xfe00 & ~((csbase_inc << bits) - csbase_inc);
858 for(index = 0; index < 8; index++) {
861 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
866 pci_write_config32(ctrl->f2, DRAM_CSBASE + (index << 2), csbase);
867 pci_write_config32(ctrl->f2, DRAM_CSMASK + (index << 2), csmask);
868 csbase += csbase_inc;
871 print_spew("Interleaved\r\n");
873 /* Return the memory size in K */
874 return common_size << (15 + bits);
877 static unsigned long order_chip_selects(const struct mem_controller *ctrl)
881 /* Remember which registers we have used in the high 8 bits of tom */
884 /* Find the largest remaining canidate */
885 unsigned index, canidate;
886 uint32_t csbase, csmask;
890 for(index = 0; index < 8; index++) {
892 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
900 if (value <= csbase) {
904 /* Has it already been selected */
905 if (tom & (1 << (index + 24))) {
908 /* I have a new canidate */
912 /* See if I have found a new canidate */
917 /* Remember the dimm size */
920 /* Remember I have used this register */
921 tom |= (1 << (canidate + 24));
923 /* Recompute the cs base register value */
924 csbase = (tom << 21) | 1;
926 /* Increment the top of memory */
929 /* Compute the memory mask */
930 csmask = ((size -1) << 21);
931 csmask |= 0xfe00; /* For now don't optimize */
933 /* Write the new base register */
934 pci_write_config32(ctrl->f2, DRAM_CSBASE + (canidate << 2), csbase);
935 /* Write the new mask register */
936 pci_write_config32(ctrl->f2, DRAM_CSMASK + (canidate << 2), csmask);
939 /* Return the memory size in K */
940 return (tom & ~0xff000000) << 15;
943 unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
947 /* Find the last memory address used */
949 for(node_id = 0; node_id < max_node_id; node_id++) {
950 uint32_t limit, base;
952 index = node_id << 3;
953 base = pci_read_config32(ctrl->f1, 0x40 + index);
954 /* Only look at the limit if the base is enabled */
955 if ((base & 3) == 3) {
956 limit = pci_read_config32(ctrl->f1, 0x44 + index);
957 end_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
963 static void order_dimms(const struct mem_controller *ctrl)
965 unsigned long tom_k, base_k;
967 if (read_option(CMOS_VSTART_interleave_chip_selects, CMOS_VLEN_interleave_chip_selects, 1) != 0) {
968 tom_k = interleave_chip_selects(ctrl);
970 print_debug("Interleaving disabled\r\n");
974 tom_k = order_chip_selects(ctrl);
976 /* Compute the memory base address */
977 base_k = memory_end_k(ctrl, ctrl->node_id);
979 route_dram_accesses(ctrl, base_k, tom_k);
983 static long disable_dimm(const struct mem_controller *ctrl, unsigned index, long dimm_mask)
985 print_debug("disabling dimm");
986 print_debug_hex8(index);
988 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
989 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
990 dimm_mask &= ~(1 << index);
994 static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long dimm_mask)
1002 for(i = 0; (i < DIMM_SOCKETS); i++) {
1004 if (!(dimm_mask & (1 << i))) {
1007 value = spd_read_byte(ctrl->channel0[i], 21);
1011 /* Registered dimm ? */
1012 if (value & (1 << 1)) {
1015 /* Otherwise it must be an unbuffered dimm */
1020 if (unbuffered && registered) {
1021 die("Mixed buffered and registered dimms not supported");
1023 if (unbuffered && is_opteron(ctrl)) {
1024 die("Unbuffered Dimms not supported on Opteron");
1027 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1028 dcl &= ~DCL_UnBufDimm;
1030 dcl |= DCL_UnBufDimm;
1032 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1034 if (is_registered(ctrl)) {
1035 print_debug("Registered\r\n");
1037 print_debug("Unbuffered\r\n");
1043 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
1048 for(i = 0; i < DIMM_SOCKETS; i++) {
1051 device = ctrl->channel0[i];
1053 byte = spd_read_byte(ctrl->channel0[i], 2); /* Type */
1055 dimm_mask |= (1 << i);
1058 device = ctrl->channel1[i];
1060 byte = spd_read_byte(ctrl->channel1[i], 2);
1062 dimm_mask |= (1 << (i + DIMM_SOCKETS));
1069 static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_mask)
1073 /* SPD addresses to verify are identical */
1074 static const unsigned addresses[] = {
1075 2, /* Type should be DDR SDRAM */
1076 3, /* *Row addresses */
1077 4, /* *Column addresses */
1078 5, /* *Physical Banks */
1079 6, /* *Module Data Width low */
1080 7, /* *Module Data Width high */
1081 9, /* *Cycle time at highest CAS Latency CL=X */
1082 11, /* *SDRAM Type */
1083 13, /* *SDRAM Width */
1084 17, /* *Logical Banks */
1085 18, /* *Supported CAS Latencies */
1086 21, /* *SDRAM Module Attributes */
1087 23, /* *Cycle time at CAS Latnecy (CLX - 0.5) */
1088 26, /* *Cycle time at CAS Latnecy (CLX - 1.0) */
1089 27, /* *tRP Row precharge time */
1090 28, /* *Minimum Row Active to Row Active Delay (tRRD) */
1091 29, /* *tRCD RAS to CAS */
1092 30, /* *tRAS Activate to Precharge */
1093 41, /* *Minimum Active to Active/Auto Refresh Time(Trc) */
1094 42, /* *Minimum Auto Refresh Command Time(Trfc) */
1096 /* If the dimms are not in pairs do not do dual channels */
1097 if ((dimm_mask & ((1 << DIMM_SOCKETS) - 1)) !=
1098 ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) {
1099 goto single_channel;
1101 /* If the cpu is not capable of doing dual channels don't do dual channels */
1102 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1103 if (!(nbcap & NBCAP_128Bit)) {
1104 goto single_channel;
1106 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1107 unsigned device0, device1;
1110 /* If I don't have a dimm skip this one */
1111 if (!(dimm_mask & (1 << i))) {
1114 device0 = ctrl->channel0[i];
1115 device1 = ctrl->channel1[i];
1116 for(j = 0; j < sizeof(addresses)/sizeof(addresses[0]); j++) {
1118 addr = addresses[j];
1119 value0 = spd_read_byte(device0, addr);
1123 value1 = spd_read_byte(device1, addr);
1127 if (value0 != value1) {
1128 goto single_channel;
1132 print_spew("Enabling dual channel memory\r\n");
1134 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1135 dcl &= ~DCL_32ByteEn;
1136 dcl |= DCL_128BitEn;
1137 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1140 dimm_mask &= ~((1 << (DIMM_SOCKETS *2)) - (1 << DIMM_SOCKETS));
1146 uint8_t divisor; /* In 1/2 ns increments */
1149 uint32_t dch_memclk;
1150 uint16_t dch_tref4k, dch_tref8k;
1155 static const struct mem_param *get_mem_param(unsigned min_cycle_time)
1157 static const struct mem_param speed[] = {
1159 .name = "100Mhz\r\n",
1161 .divisor = (10 <<1),
1164 .dch_memclk = DCH_MEMCLK_100MHZ << DCH_MEMCLK_SHIFT,
1165 .dch_tref4k = DTH_TREF_100MHZ_4K,
1166 .dch_tref8k = DTH_TREF_100MHZ_8K,
1170 .name = "133Mhz\r\n",
1172 .divisor = (7<<1)+1,
1175 .dch_memclk = DCH_MEMCLK_133MHZ << DCH_MEMCLK_SHIFT,
1176 .dch_tref4k = DTH_TREF_133MHZ_4K,
1177 .dch_tref8k = DTH_TREF_133MHZ_8K,
1181 .name = "166Mhz\r\n",
1186 .dch_memclk = DCH_MEMCLK_166MHZ << DCH_MEMCLK_SHIFT,
1187 .dch_tref4k = DTH_TREF_166MHZ_4K,
1188 .dch_tref8k = DTH_TREF_166MHZ_8K,
1192 .name = "200Mhz\r\n",
1197 .dch_memclk = DCH_MEMCLK_200MHZ << DCH_MEMCLK_SHIFT,
1198 .dch_tref4k = DTH_TREF_200MHZ_4K,
1199 .dch_tref8k = DTH_TREF_200MHZ_8K,
1206 const struct mem_param *param;
1207 for(param = &speed[0]; param->cycle_time ; param++) {
1208 if (min_cycle_time > (param+1)->cycle_time) {
1212 if (!param->cycle_time) {
1213 die("min_cycle_time to low");
1215 print_spew(param->name);
1216 #ifdef DRAM_MIN_CYCLE_TIME
1217 print_debug(param->name);
1222 struct spd_set_memclk_result {
1223 const struct mem_param *param;
1226 static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *ctrl, long dimm_mask)
1228 /* Compute the minimum cycle time for these dimms */
1229 struct spd_set_memclk_result result;
1230 unsigned min_cycle_time, min_latency, bios_cycle_time;
1234 static const int latency_indicies[] = { 26, 23, 9 };
1235 static const unsigned char min_cycle_times[] = {
1236 [NBCAP_MEMCLK_200MHZ] = 0x50, /* 5ns */
1237 [NBCAP_MEMCLK_166MHZ] = 0x60, /* 6ns */
1238 [NBCAP_MEMCLK_133MHZ] = 0x75, /* 7.5ns */
1239 [NBCAP_MEMCLK_100MHZ] = 0xa0, /* 10ns */
1243 value = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1244 min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
1245 bios_cycle_time = min_cycle_times[
1246 read_option(CMOS_VSTART_max_mem_clock, CMOS_VLEN_max_mem_clock, 0)];
1247 if (bios_cycle_time > min_cycle_time) {
1248 min_cycle_time = bios_cycle_time;
1252 /* Compute the least latency with the fastest clock supported
1253 * by both the memory controller and the dimms.
1255 for(i = 0; i < DIMM_SOCKETS; i++) {
1256 int new_cycle_time, new_latency;
1261 if (!(dimm_mask & (1 << i))) {
1265 /* First find the supported CAS latencies
1266 * Byte 18 for DDR SDRAM is interpreted:
1267 * bit 0 == CAS Latency = 1.0
1268 * bit 1 == CAS Latency = 1.5
1269 * bit 2 == CAS Latency = 2.0
1270 * bit 3 == CAS Latency = 2.5
1271 * bit 4 == CAS Latency = 3.0
1272 * bit 5 == CAS Latency = 3.5
1276 new_cycle_time = 0xa0;
1279 latencies = spd_read_byte(ctrl->channel0[i], 18);
1280 if (latencies <= 0) continue;
1282 /* Compute the lowest cas latency supported */
1283 latency = log2(latencies) -2;
1285 /* Loop through and find a fast clock with a low latency */
1286 for(index = 0; index < 3; index++, latency++) {
1288 if ((latency < 2) || (latency > 4) ||
1289 (!(latencies & (1 << latency)))) {
1292 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1297 /* Only increase the latency if we decreas the clock */
1298 if ((value >= min_cycle_time) && (value < new_cycle_time)) {
1299 new_cycle_time = value;
1300 new_latency = latency;
1303 if (new_latency > 4){
1306 /* Does min_latency need to be increased? */
1307 if (new_cycle_time > min_cycle_time) {
1308 min_cycle_time = new_cycle_time;
1310 /* Does min_cycle_time need to be increased? */
1311 if (new_latency > min_latency) {
1312 min_latency = new_latency;
1315 /* Make a second pass through the dimms and disable
1316 * any that cannot support the selected memclk and cas latency.
1319 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1324 if (!(dimm_mask & (1 << i))) {
1327 latencies = spd_read_byte(ctrl->channel0[i], 18);
1328 if (latencies < 0) goto hw_error;
1329 if (latencies == 0) {
1333 /* Compute the lowest cas latency supported */
1334 latency = log2(latencies) -2;
1336 /* Walk through searching for the selected latency */
1337 for(index = 0; index < 3; index++, latency++) {
1338 if (!(latencies & (1 << latency))) {
1341 if (latency == min_latency)
1344 /* If I can't find the latency or my index is bad error */
1345 if ((latency != min_latency) || (index >= 3)) {
1349 /* Read the min_cycle_time for this latency */
1350 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1351 if (value < 0) goto hw_error;
1353 /* All is good if the selected clock speed
1354 * is what I need or slower.
1356 if (value <= min_cycle_time) {
1359 /* Otherwise I have an error, disable the dimm */
1361 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
1363 /* Now that I know the minimum cycle time lookup the memory parameters */
1364 result.param = get_mem_param(min_cycle_time);
1366 /* Update DRAM Config High with our selected memory speed */
1367 value = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1368 value &= ~(DCH_MEMCLK_MASK << DCH_MEMCLK_SHIFT);
1369 value |= result.param->dch_memclk;
1370 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
1372 static const unsigned latencies[] = { DTL_CL_2, DTL_CL_2_5, DTL_CL_3 };
1373 /* Update DRAM Timing Low with our selected cas latency */
1374 value = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1375 value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
1376 value |= latencies[min_latency - 2] << DTL_TCL_SHIFT;
1377 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
1379 result.dimm_mask = dimm_mask;
1382 result.param = (const struct mem_param *)0;
1383 result.dimm_mask = -1;
1388 static int update_dimm_Trc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1390 unsigned clocks, old_clocks;
1393 value = spd_read_byte(ctrl->channel0[i], 41);
1394 if (value < 0) return -1;
1395 if ((value == 0) || (value == 0xff)) {
1398 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1399 if (clocks < DTL_TRC_MIN) {
1400 clocks = DTL_TRC_MIN;
1402 if (clocks > DTL_TRC_MAX) {
1406 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1407 old_clocks = ((dtl >> DTL_TRC_SHIFT) & DTL_TRC_MASK) + DTL_TRC_BASE;
1408 if (old_clocks > clocks) {
1409 clocks = old_clocks;
1411 dtl &= ~(DTL_TRC_MASK << DTL_TRC_SHIFT);
1412 dtl |= ((clocks - DTL_TRC_BASE) << DTL_TRC_SHIFT);
1413 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1417 static int update_dimm_Trfc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1419 unsigned clocks, old_clocks;
1422 value = spd_read_byte(ctrl->channel0[i], 42);
1423 if (value < 0) return -1;
1424 if ((value == 0) || (value == 0xff)) {
1425 value = param->tRFC;
1427 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1428 if (clocks < DTL_TRFC_MIN) {
1429 clocks = DTL_TRFC_MIN;
1431 if (clocks > DTL_TRFC_MAX) {
1434 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1435 old_clocks = ((dtl >> DTL_TRFC_SHIFT) & DTL_TRFC_MASK) + DTL_TRFC_BASE;
1436 if (old_clocks > clocks) {
1437 clocks = old_clocks;
1439 dtl &= ~(DTL_TRFC_MASK << DTL_TRFC_SHIFT);
1440 dtl |= ((clocks - DTL_TRFC_BASE) << DTL_TRFC_SHIFT);
1441 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1446 static int update_dimm_Trcd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1448 unsigned clocks, old_clocks;
1451 value = spd_read_byte(ctrl->channel0[i], 29);
1452 if (value < 0) return -1;
1453 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1454 if (clocks < DTL_TRCD_MIN) {
1455 clocks = DTL_TRCD_MIN;
1457 if (clocks > DTL_TRCD_MAX) {
1460 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1461 old_clocks = ((dtl >> DTL_TRCD_SHIFT) & DTL_TRCD_MASK) + DTL_TRCD_BASE;
1462 if (old_clocks > clocks) {
1463 clocks = old_clocks;
1465 dtl &= ~(DTL_TRCD_MASK << DTL_TRCD_SHIFT);
1466 dtl |= ((clocks - DTL_TRCD_BASE) << DTL_TRCD_SHIFT);
1467 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1471 static int update_dimm_Trrd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1473 unsigned clocks, old_clocks;
1476 value = spd_read_byte(ctrl->channel0[i], 28);
1477 if (value < 0) return -1;
1478 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1479 if (clocks < DTL_TRRD_MIN) {
1480 clocks = DTL_TRRD_MIN;
1482 if (clocks > DTL_TRRD_MAX) {
1485 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1486 old_clocks = ((dtl >> DTL_TRRD_SHIFT) & DTL_TRRD_MASK) + DTL_TRRD_BASE;
1487 if (old_clocks > clocks) {
1488 clocks = old_clocks;
1490 dtl &= ~(DTL_TRRD_MASK << DTL_TRRD_SHIFT);
1491 dtl |= ((clocks - DTL_TRRD_BASE) << DTL_TRRD_SHIFT);
1492 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1496 static int update_dimm_Tras(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1498 unsigned clocks, old_clocks;
1501 value = spd_read_byte(ctrl->channel0[i], 30);
1502 if (value < 0) return -1;
1503 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1504 if (clocks < DTL_TRAS_MIN) {
1505 clocks = DTL_TRAS_MIN;
1507 if (clocks > DTL_TRAS_MAX) {
1510 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1511 old_clocks = ((dtl >> DTL_TRAS_SHIFT) & DTL_TRAS_MASK) + DTL_TRAS_BASE;
1512 if (old_clocks > clocks) {
1513 clocks = old_clocks;
1515 dtl &= ~(DTL_TRAS_MASK << DTL_TRAS_SHIFT);
1516 dtl |= ((clocks - DTL_TRAS_BASE) << DTL_TRAS_SHIFT);
1517 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1521 static int update_dimm_Trp(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1523 unsigned clocks, old_clocks;
1526 value = spd_read_byte(ctrl->channel0[i], 27);
1527 if (value < 0) return -1;
1528 clocks = (value + (param->divisor << 1) - 1)/(param->divisor << 1);
1529 if (clocks < DTL_TRP_MIN) {
1530 clocks = DTL_TRP_MIN;
1532 if (clocks > DTL_TRP_MAX) {
1535 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1536 old_clocks = ((dtl >> DTL_TRP_SHIFT) & DTL_TRP_MASK) + DTL_TRP_BASE;
1537 if (old_clocks > clocks) {
1538 clocks = old_clocks;
1540 dtl &= ~(DTL_TRP_MASK << DTL_TRP_SHIFT);
1541 dtl |= ((clocks - DTL_TRP_BASE) << DTL_TRP_SHIFT);
1542 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1546 static void set_Twr(const struct mem_controller *ctrl, const struct mem_param *param)
1549 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1550 dtl &= ~(DTL_TWR_MASK << DTL_TWR_SHIFT);
1551 dtl |= (param->dtl_twr - DTL_TWR_BASE) << DTL_TWR_SHIFT;
1552 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1556 static void init_Tref(const struct mem_controller *ctrl, const struct mem_param *param)
1559 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1560 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1561 dth |= (param->dch_tref4k << DTH_TREF_SHIFT);
1562 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1565 static int update_dimm_Tref(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1569 unsigned tref, old_tref;
1570 value = spd_read_byte(ctrl->channel0[i], 3);
1571 if (value < 0) return -1;
1574 tref = param->dch_tref8k;
1576 tref = param->dch_tref4k;
1579 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1580 old_tref = (dth >> DTH_TREF_SHIFT) & DTH_TREF_MASK;
1581 if ((value == 12) && (old_tref == param->dch_tref4k)) {
1582 tref = param->dch_tref4k;
1584 tref = param->dch_tref8k;
1586 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1587 dth |= (tref << DTH_TREF_SHIFT);
1588 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1593 static int update_dimm_x4(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1598 value = spd_read_byte(ctrl->channel0[i], 13);
1603 dimm += DCL_x4DIMM_SHIFT;
1604 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1605 dcl &= ~(1 << dimm);
1609 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1613 static int update_dimm_ecc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1617 value = spd_read_byte(ctrl->channel0[i], 11);
1622 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1623 dcl &= ~DCL_DimmEccEn;
1624 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1629 static int count_dimms(const struct mem_controller *ctrl)
1634 for(index = 0; index < 8; index += 2) {
1636 csbase = pci_read_config32(ctrl->f2, (DRAM_CSBASE + (index << 2)));
1644 static void set_Twtr(const struct mem_controller *ctrl, const struct mem_param *param)
1648 clocks = 1; /* AMD says hard code this */
1649 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1650 dth &= ~(DTH_TWTR_MASK << DTH_TWTR_SHIFT);
1651 dth |= ((clocks - DTH_TWTR_BASE) << DTH_TWTR_SHIFT);
1652 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1655 static void set_Trwt(const struct mem_controller *ctrl, const struct mem_param *param)
1663 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1664 latency = (dtl >> DTL_TCL_SHIFT) & DTL_TCL_MASK;
1665 divisor = param->divisor;
1667 if (is_opteron(ctrl)) {
1668 if (latency == DTL_CL_2) {
1669 if (divisor == ((6 << 0) + 0)) {
1673 else if (divisor > ((6 << 0)+0)) {
1674 /* 100Mhz && 133Mhz */
1678 else if (latency == DTL_CL_2_5) {
1681 else if (latency == DTL_CL_3) {
1682 if (divisor == ((6 << 0)+0)) {
1686 else if (divisor > ((6 << 0)+0)) {
1687 /* 100Mhz && 133Mhz */
1692 else /* Athlon64 */ {
1693 if (is_registered(ctrl)) {
1694 if (latency == DTL_CL_2) {
1697 else if (latency == DTL_CL_2_5) {
1700 else if (latency == DTL_CL_3) {
1704 else /* Unbuffered */{
1705 if (latency == DTL_CL_2) {
1708 else if (latency == DTL_CL_2_5) {
1711 else if (latency == DTL_CL_3) {
1716 if ((clocks < DTH_TRWT_MIN) || (clocks > DTH_TRWT_MAX)) {
1717 die("Unknown Trwt\r\n");
1720 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1721 dth &= ~(DTH_TRWT_MASK << DTH_TRWT_SHIFT);
1722 dth |= ((clocks - DTH_TRWT_BASE) << DTH_TRWT_SHIFT);
1723 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1727 static void set_Twcl(const struct mem_controller *ctrl, const struct mem_param *param)
1729 /* Memory Clocks after CAS# */
1732 if (is_registered(ctrl)) {
1737 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1738 dth &= ~(DTH_TWCL_MASK << DTH_TWCL_SHIFT);
1739 dth |= ((clocks - DTH_TWCL_BASE) << DTH_TWCL_SHIFT);
1740 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1744 static void set_read_preamble(const struct mem_controller *ctrl, const struct mem_param *param)
1748 unsigned rdpreamble;
1749 divisor = param->divisor;
1750 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1751 dch &= ~(DCH_RDPREAMBLE_MASK << DCH_RDPREAMBLE_SHIFT);
1753 if (is_registered(ctrl)) {
1754 if (divisor == ((10 << 1)+0)) {
1756 rdpreamble = ((9 << 1)+ 0);
1758 else if (divisor == ((7 << 1)+1)) {
1760 rdpreamble = ((8 << 1)+0);
1762 else if (divisor == ((6 << 1)+0)) {
1764 rdpreamble = ((7 << 1)+1);
1766 else if (divisor == ((5 << 1)+0)) {
1768 rdpreamble = ((7 << 1)+0);
1775 for(i = 0; i < 4; i++) {
1776 if (ctrl->channel0[i]) {
1780 if (divisor == ((10 << 1)+0)) {
1784 rdpreamble = ((9 << 1)+0);
1787 rdpreamble = ((14 << 1)+0);
1790 else if (divisor == ((7 << 1)+1)) {
1794 rdpreamble = ((7 << 1)+0);
1797 rdpreamble = ((11 << 1)+0);
1800 else if (divisor == ((6 << 1)+0)) {
1804 rdpreamble = ((7 << 1)+0);
1807 rdpreamble = ((9 << 1)+0);
1810 else if (divisor == ((5 << 1)+0)) {
1814 rdpreamble = ((5 << 1)+0);
1817 rdpreamble = ((7 << 1)+0);
1821 if ((rdpreamble < DCH_RDPREAMBLE_MIN) || (rdpreamble > DCH_RDPREAMBLE_MAX)) {
1822 die("Unknown rdpreamble");
1824 dch |= (rdpreamble - DCH_RDPREAMBLE_BASE) << DCH_RDPREAMBLE_SHIFT;
1825 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
1828 static void set_max_async_latency(const struct mem_controller *ctrl, const struct mem_param *param)
1834 dimms = count_dimms(ctrl);
1836 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1837 dch &= ~(DCH_ASYNC_LAT_MASK << DCH_ASYNC_LAT_SHIFT);
1839 if (is_registered(ctrl)) {
1851 die("Too many unbuffered dimms");
1853 else if (dimms == 3) {
1862 dch |= ((async_lat - DCH_ASYNC_LAT_BASE) << DCH_ASYNC_LAT_SHIFT);
1863 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
1866 static void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct mem_param *param)
1869 /* AMD says to Hardcode this */
1870 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1871 dch &= ~(DCH_IDLE_LIMIT_MASK << DCH_IDLE_LIMIT_SHIFT);
1872 dch |= DCH_IDLE_LIMIT_16 << DCH_IDLE_LIMIT_SHIFT;
1873 dch |= DCH_DYN_IDLE_CTR_EN;
1874 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
1877 static long spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param, long dimm_mask)
1881 init_Tref(ctrl, param);
1882 for(i = 0; i < DIMM_SOCKETS; i++) {
1884 if (!(dimm_mask & (1 << i))) {
1887 /* DRAM Timing Low Register */
1888 if ((rc = update_dimm_Trc (ctrl, param, i)) <= 0) goto dimm_err;
1889 if ((rc = update_dimm_Trfc(ctrl, param, i)) <= 0) goto dimm_err;
1890 if ((rc = update_dimm_Trcd(ctrl, param, i)) <= 0) goto dimm_err;
1891 if ((rc = update_dimm_Trrd(ctrl, param, i)) <= 0) goto dimm_err;
1892 if ((rc = update_dimm_Tras(ctrl, param, i)) <= 0) goto dimm_err;
1893 if ((rc = update_dimm_Trp (ctrl, param, i)) <= 0) goto dimm_err;
1895 /* DRAM Timing High Register */
1896 if ((rc = update_dimm_Tref(ctrl, param, i)) <= 0) goto dimm_err;
1899 /* DRAM Config Low */
1900 if ((rc = update_dimm_x4 (ctrl, param, i)) <= 0) goto dimm_err;
1901 if ((rc = update_dimm_ecc(ctrl, param, i)) <= 0) goto dimm_err;
1907 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
1909 /* DRAM Timing Low Register */
1910 set_Twr(ctrl, param);
1912 /* DRAM Timing High Register */
1913 set_Twtr(ctrl, param);
1914 set_Trwt(ctrl, param);
1915 set_Twcl(ctrl, param);
1917 /* DRAM Config High */
1918 set_read_preamble(ctrl, param);
1919 set_max_async_latency(ctrl, param);
1920 set_idle_cycle_limit(ctrl, param);
1924 static int controller_present(const struct mem_controller *ctrl)
1926 return pci_read_config32(ctrl->f0, 0) == 0x11001022;
1928 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
1930 struct spd_set_memclk_result result;
1931 const struct mem_param *param;
1934 if (!controller_present(ctrl)) {
1935 print_debug("No memory controller present\r\n");
1939 hw_enable_ecc(ctrl);
1940 activate_spd_rom(ctrl);
1941 dimm_mask = spd_detect_dimms(ctrl);
1942 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
1943 print_debug("No memory for this cpu\r\n");
1946 dimm_mask = spd_enable_2channels(ctrl, dimm_mask);
1949 dimm_mask = spd_set_ram_size(ctrl , dimm_mask);
1952 dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask);
1955 result = spd_set_memclk(ctrl, dimm_mask);
1956 param = result.param;
1957 dimm_mask = result.dimm_mask;
1960 dimm_mask = spd_set_dram_timing(ctrl, param , dimm_mask);
1966 /* Unrecoverable error reading SPD data */
1967 print_err("SPD error - reset\r\n");
1972 #define TIMEOUT_LOOPS 300000
1973 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
1977 /* Error if I don't have memory */
1978 if (memory_end_k(ctrl, controllers) == 0) {
1979 die("No memory\r\n");
1982 /* Before enabling memory start the memory clocks */
1983 for(i = 0; i < controllers; i++) {
1985 if (!controller_present(ctrl + i))
1987 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
1988 if (dch & (DCH_MEMCLK_EN0|DCH_MEMCLK_EN1|DCH_MEMCLK_EN2|DCH_MEMCLK_EN3)) {
1989 dch |= DCH_MEMCLK_VALID;
1990 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
1993 /* Disable dram receivers */
1995 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
1996 dcl |= DCL_DisInRcvrs;
1997 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2001 /* And if necessary toggle the the reset on the dimms by hand */
2002 memreset(controllers, ctrl);
2004 for(i = 0; i < controllers; i++) {
2006 if (!controller_present(ctrl + i))
2008 /* Skip everything if I don't have any memory on this controller */
2009 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2010 if (!(dch & DCH_MEMCLK_VALID)) {
2014 /* Toggle DisDqsHys to get it working */
2015 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2016 if (dcl & DCL_DimmEccEn) {
2018 print_spew("ECC enabled\r\n");
2019 mnc = pci_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
2021 if (dcl & DCL_128BitEn) {
2022 mnc |= MNC_CHIPKILL_EN;
2024 pci_write_config32(ctrl[i].f3, MCA_NB_CONFIG, mnc);
2026 dcl |= DCL_DisDqsHys;
2027 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2028 dcl &= ~DCL_DisDqsHys;
2029 dcl &= ~DCL_DLL_Disable;
2032 dcl |= DCL_DramInit;
2033 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2036 for(i = 0; i < controllers; i++) {
2038 if (!controller_present(ctrl + i))
2040 /* Skip everything if I don't have any memory on this controller */
2041 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2042 if (!(dch & DCH_MEMCLK_VALID)) {
2046 print_debug("Initializing memory: ");
2049 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2051 if ((loops & 1023) == 0) {
2054 } while(((dcl & DCL_DramInit) != 0) && (loops < TIMEOUT_LOOPS));
2055 if (loops >= TIMEOUT_LOOPS) {
2056 print_debug(" failed\r\n");
2059 if (!is_cpu_pre_c0()) {
2060 /* Wait until it is safe to touch memory */
2061 dcl &= ~(DCL_MemClrStatus | DCL_DramEnable);
2062 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2064 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2065 } while(((dcl & DCL_MemClrStatus) == 0) || ((dcl & DCL_DramEnable) == 0) );
2067 print_debug(" done\r\n");
2070 /* Make certain the first 1M of memory is intialized */
2074 /* Save the value of msr_201 */
2075 msr_201 = rdmsr(0x201);
2077 print_debug("Clearing initial memory region: ");
2079 /* Use write combine caching while we setup the first 1M */
2080 cache_lbmem(MTRR_TYPE_WRCOMB);
2082 /* clear memory 1meg */
2083 clear_memory((void *)0, CONFIG_LB_MEM_TOPK << 10);
2085 /* The first 1M is now setup, use it */
2086 cache_lbmem(MTRR_TYPE_WRBACK);
2088 print_debug(" done\r\n");