1 #include <cpu/k8/mtrr.h>
5 #if (CONFIG_LB_MEM_TOPK & (CONFIG_LB_MEM_TOPK -1)) != 0
6 # error "CONFIG_LB_MEM_TOPK must be a power of 2"
9 static void setup_resource_map(const unsigned int *register_values, int max)
12 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 setup_default_resource_map(void)
45 static const unsigned int register_values[] = {
46 /* Careful set limit registers before base registers which contain the enables */
47 /* DRAM Limit i Registers
56 * [ 2: 0] Destination Node ID
66 * [10: 8] Interleave select
67 * specifies the values of A[14:12] to use with interleave enable.
69 * [31:16] DRAM Limit Address i Bits 39-24
70 * This field defines the upper address bits of a 40 bit address
71 * that define the end of the DRAM region.
73 PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
74 PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
75 PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
76 PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
77 PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
78 PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
79 PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
80 PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
81 /* DRAM Base i Registers
93 * [ 1: 1] Write Enable
97 * [10: 8] Interleave Enable
99 * 001 = Interleave on A[12] (2 nodes)
101 * 011 = Interleave on A[12] and A[14] (4 nodes)
105 * 111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
107 * [13:16] DRAM Base Address i Bits 39-24
108 * This field defines the upper address bits of a 40-bit address
109 * that define the start of the DRAM region.
111 PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
112 PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
113 PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
114 PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
115 PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
116 PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
117 PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
118 PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
120 /* Memory-Mapped I/O Limit i Registers
129 * [ 2: 0] Destination Node ID
139 * [ 5: 4] Destination Link ID
146 * 0 = CPU writes may be posted
147 * 1 = CPU writes must be non-posted
148 * [31: 8] Memory-Mapped I/O Limit Address i (39-16)
149 * This field defines the upp adddress bits of a 40-bit address that
150 * defines the end of a memory-mapped I/O region n
152 PCI_ADDR(0, 0x18, 1, 0x84), 0x00000048, 0x00000000,
153 PCI_ADDR(0, 0x18, 1, 0x8C), 0x00000048, 0x00000000,
154 PCI_ADDR(0, 0x18, 1, 0x94), 0x00000048, 0x00000000,
155 PCI_ADDR(0, 0x18, 1, 0x9C), 0x00000048, 0x00000000,
156 PCI_ADDR(0, 0x18, 1, 0xA4), 0x00000048, 0x00000000,
157 PCI_ADDR(0, 0x18, 1, 0xAC), 0x00000048, 0x00000000,
158 PCI_ADDR(0, 0x18, 1, 0xB4), 0x00000048, 0x00000000,
159 PCI_ADDR(0, 0x18, 1, 0xBC), 0x00000048, 0x00ffff00,
161 /* Memory-Mapped I/O Base i Registers
170 * [ 0: 0] Read Enable
173 * [ 1: 1] Write Enable
174 * 0 = Writes disabled
176 * [ 2: 2] Cpu Disable
177 * 0 = Cpu can use this I/O range
178 * 1 = Cpu requests do not use this I/O range
180 * 0 = base/limit registers i are read/write
181 * 1 = base/limit registers i are read-only
183 * [31: 8] Memory-Mapped I/O Base Address i (39-16)
184 * This field defines the upper address bits of a 40bit address
185 * that defines the start of memory-mapped I/O region i
187 PCI_ADDR(0, 0x18, 1, 0x80), 0x000000f0, 0x00000000,
188 PCI_ADDR(0, 0x18, 1, 0x88), 0x000000f0, 0x00000000,
189 PCI_ADDR(0, 0x18, 1, 0x90), 0x000000f0, 0x00000000,
190 PCI_ADDR(0, 0x18, 1, 0x98), 0x000000f0, 0x00000000,
191 PCI_ADDR(0, 0x18, 1, 0xA0), 0x000000f0, 0x00000000,
192 PCI_ADDR(0, 0x18, 1, 0xA8), 0x000000f0, 0x00000000,
193 PCI_ADDR(0, 0x18, 1, 0xB0), 0x000000f0, 0x00000000,
194 PCI_ADDR(0, 0x18, 1, 0xB8), 0x000000f0, 0x00fc0003,
196 /* PCI I/O Limit i Registers
201 * [ 2: 0] Destination Node ID
211 * [ 5: 4] Destination Link ID
217 * [24:12] PCI I/O Limit Address i
218 * This field defines the end of PCI I/O region n
221 PCI_ADDR(0, 0x18, 1, 0xC4), 0xFE000FC8, 0x01fff000,
222 PCI_ADDR(0, 0x18, 1, 0xCC), 0xFE000FC8, 0x00000000,
223 PCI_ADDR(0, 0x18, 1, 0xD4), 0xFE000FC8, 0x00000000,
224 PCI_ADDR(0, 0x18, 1, 0xDC), 0xFE000FC8, 0x00000000,
226 /* PCI I/O Base i Registers
231 * [ 0: 0] Read Enable
234 * [ 1: 1] Write Enable
235 * 0 = Writes Disabled
239 * 0 = VGA matches Disabled
240 * 1 = matches all address < 64K and where A[9:0] is in the
241 * range 3B0-3BB or 3C0-3DF independen of the base & limit registers
243 * 0 = ISA matches Disabled
244 * 1 = Blocks address < 64K and in the last 768 bytes of eack 1K block
245 * from matching agains this base/limit pair
247 * [24:12] PCI I/O Base i
248 * This field defines the start of PCI I/O region n
251 PCI_ADDR(0, 0x18, 1, 0xC0), 0xFE000FCC, 0x00000003,
252 PCI_ADDR(0, 0x18, 1, 0xC8), 0xFE000FCC, 0x00000000,
253 PCI_ADDR(0, 0x18, 1, 0xD0), 0xFE000FCC, 0x00000000,
254 PCI_ADDR(0, 0x18, 1, 0xD8), 0xFE000FCC, 0x00000000,
256 /* Config Base and Limit i Registers
261 * [ 0: 0] Read Enable
264 * [ 1: 1] Write Enable
265 * 0 = Writes Disabled
267 * [ 2: 2] Device Number Compare Enable
268 * 0 = The ranges are based on bus number
269 * 1 = The ranges are ranges of devices on bus 0
271 * [ 6: 4] Destination Node
281 * [ 9: 8] Destination Link
287 * [23:16] Bus Number Base i
288 * This field defines the lowest bus number in configuration region i
289 * [31:24] Bus Number Limit i
290 * This field defines the highest bus number in configuration regin i
292 PCI_ADDR(0, 0x18, 1, 0xE0), 0x0000FC88, 0xff000003,
293 PCI_ADDR(0, 0x18, 1, 0xE4), 0x0000FC88, 0x00000000,
294 PCI_ADDR(0, 0x18, 1, 0xE8), 0x0000FC88, 0x00000000,
295 PCI_ADDR(0, 0x18, 1, 0xEC), 0x0000FC88, 0x00000000,
298 max = sizeof(register_values)/sizeof(register_values[0]);
299 setup_resource_map(register_values, max);
302 static void sdram_set_registers(const struct mem_controller *ctrl)
304 static const unsigned int register_values[] = {
306 /* Careful set limit registers before base registers which contain the enables */
307 /* DRAM Limit i Registers
316 * [ 2: 0] Destination Node ID
326 * [10: 8] Interleave select
327 * specifies the values of A[14:12] to use with interleave enable.
329 * [31:16] DRAM Limit Address i Bits 39-24
330 * This field defines the upper address bits of a 40 bit address
331 * that define the end of the DRAM region.
333 PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
334 PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
335 PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
336 PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
337 PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
338 PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
339 PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
340 PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
341 /* DRAM Base i Registers
350 * [ 0: 0] Read Enable
353 * [ 1: 1] Write Enable
354 * 0 = Writes Disabled
357 * [10: 8] Interleave Enable
358 * 000 = No interleave
359 * 001 = Interleave on A[12] (2 nodes)
361 * 011 = Interleave on A[12] and A[14] (4 nodes)
365 * 111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
367 * [13:16] DRAM Base Address i Bits 39-24
368 * This field defines the upper address bits of a 40-bit address
369 * that define the start of the DRAM region.
371 PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
372 PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
373 PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
374 PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
375 PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
376 PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
377 PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
378 PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
380 /* DRAM CS Base Address i Registers
389 * [ 0: 0] Chip-Select Bank Enable
393 * [15: 9] Base Address (19-13)
394 * An optimization used when all DIMM are the same size...
396 * [31:21] Base Address (35-25)
397 * This field defines the top 11 addresses bit of a 40-bit
398 * address that define the memory address space. These
399 * bits decode 32-MByte blocks of memory.
401 PCI_ADDR(0, 0x18, 2, 0x40), 0x001f01fe, 0x00000000,
402 PCI_ADDR(0, 0x18, 2, 0x44), 0x001f01fe, 0x00000000,
403 PCI_ADDR(0, 0x18, 2, 0x48), 0x001f01fe, 0x00000000,
404 PCI_ADDR(0, 0x18, 2, 0x4C), 0x001f01fe, 0x00000000,
405 PCI_ADDR(0, 0x18, 2, 0x50), 0x001f01fe, 0x00000000,
406 PCI_ADDR(0, 0x18, 2, 0x54), 0x001f01fe, 0x00000000,
407 PCI_ADDR(0, 0x18, 2, 0x58), 0x001f01fe, 0x00000000,
408 PCI_ADDR(0, 0x18, 2, 0x5C), 0x001f01fe, 0x00000000,
409 /* DRAM CS Mask Address i Registers
418 * Select bits to exclude from comparison with the DRAM Base address register.
420 * [15: 9] Address Mask (19-13)
421 * Address to be excluded from the optimized case
423 * [29:21] Address Mask (33-25)
424 * The bits with an address mask of 1 are excluded from address comparison
428 PCI_ADDR(0, 0x18, 2, 0x60), 0xC01f01ff, 0x00000000,
429 PCI_ADDR(0, 0x18, 2, 0x64), 0xC01f01ff, 0x00000000,
430 PCI_ADDR(0, 0x18, 2, 0x68), 0xC01f01ff, 0x00000000,
431 PCI_ADDR(0, 0x18, 2, 0x6C), 0xC01f01ff, 0x00000000,
432 PCI_ADDR(0, 0x18, 2, 0x70), 0xC01f01ff, 0x00000000,
433 PCI_ADDR(0, 0x18, 2, 0x74), 0xC01f01ff, 0x00000000,
434 PCI_ADDR(0, 0x18, 2, 0x78), 0xC01f01ff, 0x00000000,
435 PCI_ADDR(0, 0x18, 2, 0x7C), 0xC01f01ff, 0x00000000,
436 /* DRAM Bank Address Mapping Register
438 * Specify the memory module size
443 * 000 = 32Mbyte (Rows = 12 & Col = 8)
444 * 001 = 64Mbyte (Rows = 12 & Col = 9)
445 * 010 = 128Mbyte (Rows = 13 & Col = 9)|(Rows = 12 & Col = 10)
446 * 011 = 256Mbyte (Rows = 13 & Col = 10)|(Rows = 12 & Col = 11)
447 * 100 = 512Mbyte (Rows = 13 & Col = 11)|(Rows = 14 & Col = 10)
448 * 101 = 1Gbyte (Rows = 14 & Col = 11)|(Rows = 13 & Col = 12)
449 * 110 = 2Gbyte (Rows = 14 & Col = 12)
456 PCI_ADDR(0, 0x18, 2, 0x80), 0xffff8888, 0x00000000,
457 /* DRAM Timing Low Register
459 * [ 2: 0] Tcl (Cas# Latency, Cas# to read-data-valid)
469 * [ 7: 4] Trc (Row Cycle Time, Ras#-active to Ras#-active/bank auto refresh)
470 * 0000 = 7 bus clocks
471 * 0001 = 8 bus clocks
473 * 1110 = 21 bus clocks
474 * 1111 = 22 bus clocks
475 * [11: 8] Trfc (Row refresh Cycle time, Auto-refresh-active to RAS#-active or RAS#auto-refresh)
476 * 0000 = 9 bus clocks
477 * 0010 = 10 bus clocks
479 * 1110 = 23 bus clocks
480 * 1111 = 24 bus clocks
481 * [14:12] Trcd (Ras#-active to Case#-read/write Delay)
491 * [18:16] Trrd (Ras# to Ras# Delay)
501 * [23:20] Tras (Minmum Ras# Active Time)
502 * 0000 to 0100 = reserved
503 * 0101 = 5 bus clocks
505 * 1111 = 15 bus clocks
506 * [26:24] Trp (Row Precharge Time)
516 * [28:28] Twr (Write Recovery Time)
521 PCI_ADDR(0, 0x18, 2, 0x88), 0xe8088008, 0x02522001 /* 0x03623125 */ ,
522 /* DRAM Timing High Register
524 * [ 0: 0] Twtr (Write to Read Delay)
528 * [ 6: 4] Trwt (Read to Write Delay)
538 * [12: 8] Tref (Refresh Rate)
539 * 00000 = 100Mhz 4K rows
540 * 00001 = 133Mhz 4K rows
541 * 00010 = 166Mhz 4K rows
542 * 00011 = 200Mhz 4K rows
543 * 01000 = 100Mhz 8K/16K rows
544 * 01001 = 133Mhz 8K/16K rows
545 * 01010 = 166Mhz 8K/16K rows
546 * 01011 = 200Mhz 8K/16K rows
548 * [22:20] Twcl (Write CAS Latency)
549 * 000 = 1 Mem clock after CAS# (Unbuffered Dimms)
550 * 001 = 2 Mem clocks after CAS# (Registered Dimms)
553 PCI_ADDR(0, 0x18, 2, 0x8c), 0xff8fe08e, (0 << 20)|(0 << 8)|(0 << 4)|(0 << 0),
554 /* DRAM Config Low Register
556 * [ 0: 0] DLL Disable
565 * [ 3: 3] Disable DQS Hystersis (FIXME handle this one carefully)
566 * 0 = Enable DQS input filter
567 * 1 = Disable DQS input filtering
570 * 0 = Initialization done or not yet started.
571 * 1 = Initiate DRAM intialization sequence
572 * [ 9: 9] SO-Dimm Enable
574 * 1 = SO-Dimms present
576 * 0 = DRAM not enabled
577 * 1 = DRAM initialized and enabled
578 * [11:11] Memory Clear Status
579 * 0 = Memory Clear function has not completed
580 * 1 = Memory Clear function has completed
581 * [12:12] Exit Self-Refresh
582 * 0 = Exit from self-refresh done or not yet started
583 * 1 = DRAM exiting from self refresh
584 * [13:13] Self-Refresh Status
585 * 0 = Normal Operation
586 * 1 = Self-refresh mode active
587 * [15:14] Read/Write Queue Bypass Count
592 * [16:16] 128-bit/64-Bit
593 * 0 = 64bit Interface to DRAM
594 * 1 = 128bit Interface to DRAM
595 * [17:17] DIMM ECC Enable
596 * 0 = Some DIMMs do not have ECC
597 * 1 = ALL DIMMS have ECC bits
598 * [18:18] UnBuffered DIMMs
600 * 1 = Unbuffered DIMMS
601 * [19:19] Enable 32-Byte Granularity
602 * 0 = Optimize for 64byte bursts
603 * 1 = Optimize for 32byte bursts
604 * [20:20] DIMM 0 is x4
605 * [21:21] DIMM 1 is x4
606 * [22:22] DIMM 2 is x4
607 * [23:23] DIMM 3 is x4
609 * 1 = x4 DIMM present
610 * [24:24] Disable DRAM Receivers
611 * 0 = Receivers enabled
612 * 1 = Receivers disabled
614 * 000 = Arbiters chois is always respected
615 * 001 = Oldest entry in DCQ can be bypassed 1 time
616 * 010 = Oldest entry in DCQ can be bypassed 2 times
617 * 011 = Oldest entry in DCQ can be bypassed 3 times
618 * 100 = Oldest entry in DCQ can be bypassed 4 times
619 * 101 = Oldest entry in DCQ can be bypassed 5 times
620 * 110 = Oldest entry in DCQ can be bypassed 6 times
621 * 111 = Oldest entry in DCQ can be bypassed 7 times
624 PCI_ADDR(0, 0x18, 2, 0x90), 0xf0000000,
626 (0 << 23)|(0 << 22)|(0 << 21)|(0 << 20)|
627 (1 << 19)|(0 << 18)|(1 << 17)|(0 << 16)|
628 (2 << 14)|(0 << 13)|(0 << 12)|
629 (0 << 11)|(0 << 10)|(0 << 9)|(0 << 8)|
630 (0 << 3) |(0 << 1) |(0 << 0),
631 /* DRAM Config High Register
633 * [ 0: 3] Maximum Asynchronous Latency
638 * [11: 8] Read Preamble
656 * [18:16] Idle Cycle Limit
665 * [19:19] Dynamic Idle Cycle Center Enable
666 * 0 = Use Idle Cycle Limit
667 * 1 = Generate a dynamic Idle cycle limit
668 * [22:20] DRAM MEMCLK Frequency
678 * [25:25] Memory Clock Ratio Valid (FIXME carefully enable memclk)
679 * 0 = Disable MemClks
681 * [26:26] Memory Clock 0 Enable
684 * [27:27] Memory Clock 1 Enable
687 * [28:28] Memory Clock 2 Enable
690 * [29:29] Memory Clock 3 Enable
695 PCI_ADDR(0, 0x18, 2, 0x94), 0xc180f0f0,
696 (0 << 29)|(0 << 28)|(0 << 27)|(0 << 26)|(0 << 25)|
697 (0 << 20)|(0 << 19)|(DCH_IDLE_LIMIT_16 << 16)|(0 << 8)|(0 << 0),
698 /* DRAM Delay Line Register
700 * Adjust the skew of the input DQS strobe relative to DATA
702 * [23:16] Delay Line Adjust
703 * Adjusts the DLL derived PDL delay by one or more delay stages
704 * in either the faster or slower direction.
705 * [24:24} Adjust Slower
707 * 1 = Adj is used to increase the PDL delay
708 * [25:25] Adjust Faster
710 * 1 = Adj is used to decrease the PDL delay
713 PCI_ADDR(0, 0x18, 2, 0x98), 0xfc00ffff, 0x00000000,
714 /* DRAM Scrub Control Register
716 * [ 4: 0] DRAM Scrube Rate
718 * [12: 8] L2 Scrub Rate
720 * [20:16] Dcache Scrub
723 * 00000 = Do not scrub
745 * All Others = Reserved
747 PCI_ADDR(0, 0x18, 3, 0x58), 0xffe0e0e0, 0x00000000,
748 /* DRAM Scrub Address Low Register
750 * [ 0: 0] DRAM Scrubber Redirect Enable
752 * 1 = Scrubber Corrects errors found in normal operation
754 * [31: 6] DRAM Scrub Address 31-6
756 PCI_ADDR(0, 0x18, 3, 0x5C), 0x0000003e, 0x00000000,
757 /* DRAM Scrub Address High Register
759 * [ 7: 0] DRAM Scrubb Address 39-32
762 PCI_ADDR(0, 0x18, 3, 0x60), 0xffffff00, 0x00000000,
766 print_spew("setting up CPU");
767 print_spew_hex8(ctrl->node_id);
768 print_spew(" northbridge registers\r\n");
769 max = sizeof(register_values)/sizeof(register_values[0]);
770 for(i = 0; i < max; i += 3) {
775 print_spew_hex32(register_values[i]);
777 print_spew_hex32(register_values[i+2]);
780 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x18, 0) + ctrl->f0;
781 where = register_values[i] & 0xff;
782 reg = pci_read_config32(dev, where);
783 reg &= register_values[i+1];
784 reg |= register_values[i+2];
785 pci_write_config32(dev, where, reg);
788 reg = pci_read_config32(register_values[i]);
789 reg &= register_values[i+1];
790 reg |= register_values[i+2];
791 pci_write_config32(register_values[i], reg);
794 print_spew("done.\r\n");
798 static void hw_enable_ecc(const struct mem_controller *ctrl)
801 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
802 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
803 dcl &= ~DCL_DimmEccEn;
804 if (nbcap & NBCAP_ECC) {
805 dcl |= DCL_DimmEccEn;
807 if (read_option(CMOS_VSTART_ECC_memory, CMOS_VLEN_ECC_memory, 1) == 0) {
808 dcl &= ~DCL_DimmEccEn;
810 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
814 static int is_dual_channel(const struct mem_controller *ctrl)
817 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
818 return dcl & DCL_128BitEn;
821 static int is_opteron(const struct mem_controller *ctrl)
823 /* Test to see if I am an Opteron.
824 * FIXME Testing dual channel capability is correct for now
825 * but a beter test is probably required.
827 #warning "FIXME implement a better test for opterons"
829 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
830 return !!(nbcap & NBCAP_128Bit);
833 static int is_registered(const struct mem_controller *ctrl)
835 /* Test to see if we are dealing with registered SDRAM.
836 * If we are not registered we are unbuffered.
837 * This function must be called after spd_handle_unbuffered_dimms.
840 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
841 return !(dcl & DCL_UnBufDimm);
849 static struct dimm_size spd_get_dimm_size(unsigned device)
851 /* Calculate the log base 2 size of a DIMM in bits */
857 /* Note it might be easier to use byte 31 here, it has the DIMM size as
858 * a multiple of 4MB. The way we do it now we can size both
859 * sides of an assymetric dimm.
861 value = spd_read_byte(device, 3); /* rows */
862 if (value < 0) goto hw_err;
863 if ((value & 0xf) == 0) goto val_err;
864 sz.side1 += value & 0xf;
866 value = spd_read_byte(device, 4); /* columns */
867 if (value < 0) goto hw_err;
868 if ((value & 0xf) == 0) goto val_err;
869 sz.side1 += value & 0xf;
871 value = spd_read_byte(device, 17); /* banks */
872 if (value < 0) goto hw_err;
873 if ((value & 0xff) == 0) goto val_err;
874 sz.side1 += log2(value & 0xff);
876 /* Get the module data width and convert it to a power of two */
877 value = spd_read_byte(device, 7); /* (high byte) */
878 if (value < 0) goto hw_err;
882 low = spd_read_byte(device, 6); /* (low byte) */
883 if (low < 0) goto hw_err;
884 value = value | (low & 0xff);
885 if ((value != 72) && (value != 64)) goto val_err;
886 sz.side1 += log2(value);
889 value = spd_read_byte(device, 5); /* number of physical banks */
890 if (value < 0) goto hw_err;
891 if (value == 1) goto out;
892 if (value != 2) goto val_err;
894 /* Start with the symmetrical case */
897 value = spd_read_byte(device, 3); /* rows */
898 if (value < 0) goto hw_err;
899 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
900 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
901 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
903 value = spd_read_byte(device, 4); /* columns */
904 if (value < 0) goto hw_err;
905 if ((value & 0xff) == 0) goto val_err;
906 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
907 sz.side2 += ((value >> 4) & 0x0f); /* Add in columsn on side 2 */
911 die("Bad SPD value\r\n");
912 /* If an hw_error occurs report that I have no memory */
920 static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz, unsigned index)
922 uint32_t base0, base1, map;
925 if (sz.side1 != sz.side2) {
928 map = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
929 map &= ~(0xf << (index * 4));
931 /* For each base register.
932 * Place the dimm size in 32 MB quantities in the bits 31 - 21.
933 * The initialize dimm size is in bits.
934 * Set the base enable bit0.
939 /* Make certain side1 of the dimm is at least 32MB */
940 if (sz.side1 >= (25 +3)) {
941 map |= (sz.side1 - (25 + 3)) << (index *4);
942 base0 = (1 << ((sz.side1 - (25 + 3)) + 21)) | 1;
944 /* Make certain side2 of the dimm is at least 32MB */
945 if (sz.side2 >= (25 + 3)) {
946 base1 = (1 << ((sz.side2 - (25 + 3)) + 21)) | 1;
949 /* Double the size if we are using dual channel memory */
950 if (is_dual_channel(ctrl)) {
951 base0 = (base0 << 1) | (base0 & 1);
952 base1 = (base1 << 1) | (base1 & 1);
955 /* Clear the reserved bits */
956 base0 &= ~0x001ffffe;
957 base1 &= ~0x001ffffe;
959 /* Set the appropriate DIMM base address register */
960 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
961 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
962 pci_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
964 /* Enable the memory clocks for this DIMM */
966 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
967 dch |= DCH_MEMCLK_EN0 << index;
968 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
972 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
976 for(i = 0; i < DIMM_SOCKETS; i++) {
978 if (!(dimm_mask & (1 << i))) {
981 sz = spd_get_dimm_size(ctrl->channel0[i]);
983 return -1; /* Report SPD error */
985 set_dimm_size(ctrl, sz, i);
990 static void route_dram_accesses(const struct mem_controller *ctrl,
991 unsigned long base_k, unsigned long limit_k)
993 /* Route the addresses to the controller node */
998 unsigned limit_reg, base_reg;
1001 node_id = ctrl->node_id;
1002 index = (node_id << 3);
1003 limit = (limit_k << 2);
1004 limit &= 0xffff0000;
1005 limit -= 0x00010000;
1006 limit |= ( 0 << 8) | (node_id << 0);
1007 base = (base_k << 2);
1009 base |= (0 << 8) | (1<<1) | (1<<0);
1011 limit_reg = 0x44 + index;
1012 base_reg = 0x40 + index;
1013 for(device = PCI_DEV(0, 0x18, 1); device <= PCI_DEV(0, 0x1f, 1); device += PCI_DEV(0, 1, 0)) {
1014 pci_write_config32(device, limit_reg, limit);
1015 pci_write_config32(device, base_reg, base);
1019 static void set_top_mem(unsigned tom_k)
1021 /* Error if I don't have memory */
1026 /* Report the amount of memory. */
1027 print_spew("RAM: 0x");
1028 print_spew_hex32(tom_k);
1029 print_spew(" KB\r\n");
1031 /* Now set top of memory */
1033 msr.lo = (tom_k & 0x003fffff) << 10;
1034 msr.hi = (tom_k & 0xffc00000) >> 22;
1035 wrmsr(TOP_MEM2, msr);
1037 /* Leave a 64M hole between TOP_MEM and TOP_MEM2
1038 * so I can see my rom chip and other I/O devices.
1040 if (tom_k >= 0x003f0000) {
1043 msr.lo = (tom_k & 0x003fffff) << 10;
1044 msr.hi = (tom_k & 0xffc00000) >> 22;
1045 wrmsr(TOP_MEM, msr);
1048 static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
1051 static const uint32_t csbase_low[] = {
1052 /* 32MB */ (1 << (13 - 4)),
1053 /* 64MB */ (1 << (14 - 4)),
1054 /* 128MB */ (1 << (14 - 4)),
1055 /* 256MB */ (1 << (15 - 4)),
1056 /* 512MB */ (1 << (15 - 4)),
1057 /* 1GB */ (1 << (16 - 4)),
1058 /* 2GB */ (1 << (16 - 4)),
1060 uint32_t csbase_inc;
1061 int chip_selects, index;
1063 unsigned common_size;
1064 uint32_t csbase, csmask;
1066 /* See if all of the memory chip selects are the same size
1067 * and if so count them.
1071 for(index = 0; index < 8; index++) {
1075 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1077 /* Is it enabled? */
1083 if (common_size == 0) {
1086 /* The size differed fail */
1087 if (common_size != size) {
1091 /* Chip selects can only be interleaved when there is
1092 * more than one and their is a power of two of them.
1094 bits = log2(chip_selects);
1095 if (((1 << bits) != chip_selects) || (bits < 1) || (bits > 3)) {
1099 /* Also we run out of address mask bits if we try and interleave 8 4GB dimms */
1100 if ((bits == 3) && (common_size == (1 << (32 - 3)))) {
1101 print_debug("8 4GB chip selects cannot be interleaved\r\n");
1104 /* Find the bits of csbase that we need to interleave on */
1105 if (is_dual_channel(ctrl)) {
1106 csbase_inc = csbase_low[log2(common_size) - 1] << 1;
1108 csbase_inc = csbase_low[log2(common_size)];
1110 /* Compute the initial values for csbase and csbask.
1111 * In csbase just set the enable bit and the base to zero.
1112 * In csmask set the mask bits for the size and page level interleave.
1115 csmask = (((common_size << bits) - 1) << 21);
1116 csmask |= 0xfe00 & ~((csbase_inc << bits) - csbase_inc);
1117 for(index = 0; index < 8; index++) {
1120 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1121 /* Is it enabled? */
1125 pci_write_config32(ctrl->f2, DRAM_CSBASE + (index << 2), csbase);
1126 pci_write_config32(ctrl->f2, DRAM_CSMASK + (index << 2), csmask);
1127 csbase += csbase_inc;
1130 print_spew("Interleaved\r\n");
1132 /* Return the memory size in K */
1133 return common_size << (15 + bits);
1136 static unsigned long order_chip_selects(const struct mem_controller *ctrl)
1140 /* Remember which registers we have used in the high 8 bits of tom */
1143 /* Find the largest remaining canidate */
1144 unsigned index, canidate;
1145 uint32_t csbase, csmask;
1149 for(index = 0; index < 8; index++) {
1151 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1153 /* Is it enabled? */
1158 /* Is it greater? */
1159 if (value <= csbase) {
1163 /* Has it already been selected */
1164 if (tom & (1 << (index + 24))) {
1167 /* I have a new canidate */
1171 /* See if I have found a new canidate */
1176 /* Remember the dimm size */
1177 size = csbase >> 21;
1179 /* Remember I have used this register */
1180 tom |= (1 << (canidate + 24));
1182 /* Recompute the cs base register value */
1183 csbase = (tom << 21) | 1;
1185 /* Increment the top of memory */
1188 /* Compute the memory mask */
1189 csmask = ((size -1) << 21);
1190 csmask |= 0xfe00; /* For now don't optimize */
1192 /* Write the new base register */
1193 pci_write_config32(ctrl->f2, DRAM_CSBASE + (canidate << 2), csbase);
1194 /* Write the new mask register */
1195 pci_write_config32(ctrl->f2, DRAM_CSMASK + (canidate << 2), csmask);
1198 /* Return the memory size in K */
1199 return (tom & ~0xff000000) << 15;
1202 unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
1206 /* Find the last memory address used */
1208 for(node_id = 0; node_id < max_node_id; node_id++) {
1209 uint32_t limit, base;
1211 index = node_id << 3;
1212 base = pci_read_config32(ctrl->f1, 0x40 + index);
1213 /* Only look at the limit if the base is enabled */
1214 if ((base & 3) == 3) {
1215 limit = pci_read_config32(ctrl->f1, 0x44 + index);
1216 end_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
1222 static void order_dimms(const struct mem_controller *ctrl)
1224 unsigned long tom_k, base_k;
1226 if (read_option(CMOS_VSTART_interleave_chip_selects, CMOS_VLEN_interleave_chip_selects, 1) != 0) {
1227 tom_k = interleave_chip_selects(ctrl);
1229 print_debug("Interleaving disabled\r\n");
1233 tom_k = order_chip_selects(ctrl);
1235 /* Compute the memory base address */
1236 base_k = memory_end_k(ctrl, ctrl->node_id);
1238 route_dram_accesses(ctrl, base_k, tom_k);
1242 static long disable_dimm(const struct mem_controller *ctrl, unsigned index, long dimm_mask)
1244 print_debug("disabling dimm");
1245 print_debug_hex8(index);
1246 print_debug("\r\n");
1247 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
1248 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
1249 dimm_mask &= ~(1 << index);
1253 static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long dimm_mask)
1261 for(i = 0; (i < DIMM_SOCKETS); i++) {
1263 if (!(dimm_mask & (1 << i))) {
1266 value = spd_read_byte(ctrl->channel0[i], 21);
1270 /* Registered dimm ? */
1271 if (value & (1 << 1)) {
1274 /* Otherwise it must be an unbuffered dimm */
1279 if (unbuffered && registered) {
1280 die("Mixed buffered and registered dimms not supported");
1282 if (unbuffered && is_opteron(ctrl)) {
1283 die("Unbuffered Dimms not supported on Opteron");
1286 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1287 dcl &= ~DCL_UnBufDimm;
1289 dcl |= DCL_UnBufDimm;
1291 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1293 if (is_registered(ctrl)) {
1294 print_debug("Registered\r\n");
1296 print_debug("Unbuffered\r\n");
1302 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
1307 for(i = 0; i < DIMM_SOCKETS; i++) {
1310 device = ctrl->channel0[i];
1312 byte = spd_read_byte(ctrl->channel0[i], 2); /* Type */
1314 dimm_mask |= (1 << i);
1317 device = ctrl->channel1[i];
1319 byte = spd_read_byte(ctrl->channel1[i], 2);
1321 dimm_mask |= (1 << (i + DIMM_SOCKETS));
1328 static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_mask)
1332 /* SPD addresses to verify are identical */
1333 static const unsigned addresses[] = {
1334 2, /* Type should be DDR SDRAM */
1335 3, /* *Row addresses */
1336 4, /* *Column addresses */
1337 5, /* *Physical Banks */
1338 6, /* *Module Data Width low */
1339 7, /* *Module Data Width high */
1340 9, /* *Cycle time at highest CAS Latency CL=X */
1341 11, /* *SDRAM Type */
1342 13, /* *SDRAM Width */
1343 17, /* *Logical Banks */
1344 18, /* *Supported CAS Latencies */
1345 21, /* *SDRAM Module Attributes */
1346 23, /* *Cycle time at CAS Latnecy (CLX - 0.5) */
1347 26, /* *Cycle time at CAS Latnecy (CLX - 1.0) */
1348 27, /* *tRP Row precharge time */
1349 28, /* *Minimum Row Active to Row Active Delay (tRRD) */
1350 29, /* *tRCD RAS to CAS */
1351 30, /* *tRAS Activate to Precharge */
1352 41, /* *Minimum Active to Active/Auto Refresh Time(Trc) */
1353 42, /* *Minimum Auto Refresh Command Time(Trfc) */
1355 /* If the dimms are not in pairs do not do dual channels */
1356 if ((dimm_mask & ((1 << DIMM_SOCKETS) - 1)) !=
1357 ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) {
1358 goto single_channel;
1360 /* If the cpu is not capable of doing dual channels don't do dual channels */
1361 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1362 if (!(nbcap & NBCAP_128Bit)) {
1363 goto single_channel;
1365 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1366 unsigned device0, device1;
1369 /* If I don't have a dimm skip this one */
1370 if (!(dimm_mask & (1 << i))) {
1373 device0 = ctrl->channel0[i];
1374 device1 = ctrl->channel1[i];
1375 for(j = 0; j < sizeof(addresses)/sizeof(addresses[0]); j++) {
1377 addr = addresses[j];
1378 value0 = spd_read_byte(device0, addr);
1382 value1 = spd_read_byte(device1, addr);
1386 if (value0 != value1) {
1387 goto single_channel;
1391 print_spew("Enabling dual channel memory\r\n");
1393 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1394 dcl &= ~DCL_32ByteEn;
1395 dcl |= DCL_128BitEn;
1396 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1399 dimm_mask &= ~((1 << (DIMM_SOCKETS *2)) - (1 << DIMM_SOCKETS));
1405 uint8_t divisor; /* In 1/2 ns increments */
1408 uint32_t dch_memclk;
1409 uint16_t dch_tref4k, dch_tref8k;
1414 static const struct mem_param *get_mem_param(unsigned min_cycle_time)
1416 static const struct mem_param speed[] = {
1418 .name = "100Mhz\r\n",
1420 .divisor = (10 <<1),
1423 .dch_memclk = DCH_MEMCLK_100MHZ << DCH_MEMCLK_SHIFT,
1424 .dch_tref4k = DTH_TREF_100MHZ_4K,
1425 .dch_tref8k = DTH_TREF_100MHZ_8K,
1429 .name = "133Mhz\r\n",
1431 .divisor = (7<<1)+1,
1434 .dch_memclk = DCH_MEMCLK_133MHZ << DCH_MEMCLK_SHIFT,
1435 .dch_tref4k = DTH_TREF_133MHZ_4K,
1436 .dch_tref8k = DTH_TREF_133MHZ_8K,
1440 .name = "166Mhz\r\n",
1445 .dch_memclk = DCH_MEMCLK_166MHZ << DCH_MEMCLK_SHIFT,
1446 .dch_tref4k = DTH_TREF_166MHZ_4K,
1447 .dch_tref8k = DTH_TREF_166MHZ_8K,
1451 .name = "200Mhz\r\n",
1456 .dch_memclk = DCH_MEMCLK_200MHZ << DCH_MEMCLK_SHIFT,
1457 .dch_tref4k = DTH_TREF_200MHZ_4K,
1458 .dch_tref8k = DTH_TREF_200MHZ_8K,
1465 const struct mem_param *param;
1466 for(param = &speed[0]; param->cycle_time ; param++) {
1467 if (min_cycle_time > (param+1)->cycle_time) {
1471 if (!param->cycle_time) {
1472 die("min_cycle_time to low");
1474 print_spew(param->name);
1475 #ifdef DRAM_MIN_CYCLE_TIME
1476 print_debug(param->name);
1481 struct spd_set_memclk_result {
1482 const struct mem_param *param;
1485 static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *ctrl, long dimm_mask)
1487 /* Compute the minimum cycle time for these dimms */
1488 struct spd_set_memclk_result result;
1489 unsigned min_cycle_time, min_latency, bios_cycle_time;
1493 static const int latency_indicies[] = { 26, 23, 9 };
1494 static const unsigned char min_cycle_times[] = {
1495 [NBCAP_MEMCLK_200MHZ] = 0x50, /* 5ns */
1496 [NBCAP_MEMCLK_166MHZ] = 0x60, /* 6ns */
1497 [NBCAP_MEMCLK_133MHZ] = 0x75, /* 7.5ns */
1498 [NBCAP_MEMCLK_100MHZ] = 0xa0, /* 10ns */
1502 value = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1503 min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
1504 bios_cycle_time = min_cycle_times[
1505 read_option(CMOS_VSTART_max_mem_clock, CMOS_VLEN_max_mem_clock, 0)];
1506 if (bios_cycle_time > min_cycle_time) {
1507 min_cycle_time = bios_cycle_time;
1511 /* Compute the least latency with the fastest clock supported
1512 * by both the memory controller and the dimms.
1514 for(i = 0; i < DIMM_SOCKETS; i++) {
1515 int new_cycle_time, new_latency;
1520 if (!(dimm_mask & (1 << i))) {
1524 /* First find the supported CAS latencies
1525 * Byte 18 for DDR SDRAM is interpreted:
1526 * bit 0 == CAS Latency = 1.0
1527 * bit 1 == CAS Latency = 1.5
1528 * bit 2 == CAS Latency = 2.0
1529 * bit 3 == CAS Latency = 2.5
1530 * bit 4 == CAS Latency = 3.0
1531 * bit 5 == CAS Latency = 3.5
1535 new_cycle_time = 0xa0;
1538 latencies = spd_read_byte(ctrl->channel0[i], 18);
1539 if (latencies <= 0) continue;
1541 /* Compute the lowest cas latency supported */
1542 latency = log2(latencies) -2;
1544 /* Loop through and find a fast clock with a low latency */
1545 for(index = 0; index < 3; index++, latency++) {
1547 if ((latency < 2) || (latency > 4) ||
1548 (!(latencies & (1 << latency)))) {
1551 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1556 /* Only increase the latency if we decreas the clock */
1557 if ((value >= min_cycle_time) && (value < new_cycle_time)) {
1558 new_cycle_time = value;
1559 new_latency = latency;
1562 if (new_latency > 4){
1565 /* Does min_latency need to be increased? */
1566 if (new_cycle_time > min_cycle_time) {
1567 min_cycle_time = new_cycle_time;
1569 /* Does min_cycle_time need to be increased? */
1570 if (new_latency > min_latency) {
1571 min_latency = new_latency;
1574 /* Make a second pass through the dimms and disable
1575 * any that cannot support the selected memclk and cas latency.
1578 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1583 if (!(dimm_mask & (1 << i))) {
1586 latencies = spd_read_byte(ctrl->channel0[i], 18);
1587 if (latencies < 0) goto hw_error;
1588 if (latencies == 0) {
1592 /* Compute the lowest cas latency supported */
1593 latency = log2(latencies) -2;
1595 /* Walk through searching for the selected latency */
1596 for(index = 0; index < 3; index++, latency++) {
1597 if (!(latencies & (1 << latency))) {
1600 if (latency == min_latency)
1603 /* If I can't find the latency or my index is bad error */
1604 if ((latency != min_latency) || (index >= 3)) {
1608 /* Read the min_cycle_time for this latency */
1609 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1610 if (value < 0) goto hw_error;
1612 /* All is good if the selected clock speed
1613 * is what I need or slower.
1615 if (value <= min_cycle_time) {
1618 /* Otherwise I have an error, disable the dimm */
1620 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
1622 /* Now that I know the minimum cycle time lookup the memory parameters */
1623 result.param = get_mem_param(min_cycle_time);
1625 /* Update DRAM Config High with our selected memory speed */
1626 value = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1627 value &= ~(DCH_MEMCLK_MASK << DCH_MEMCLK_SHIFT);
1628 value |= result.param->dch_memclk;
1629 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
1631 static const unsigned latencies[] = { DTL_CL_2, DTL_CL_2_5, DTL_CL_3 };
1632 /* Update DRAM Timing Low with our selected cas latency */
1633 value = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1634 value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
1635 value |= latencies[min_latency - 2] << DTL_TCL_SHIFT;
1636 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
1638 result.dimm_mask = dimm_mask;
1641 result.param = (const struct mem_param *)0;
1642 result.dimm_mask = -1;
1647 static int update_dimm_Trc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1649 unsigned clocks, old_clocks;
1652 value = spd_read_byte(ctrl->channel0[i], 41);
1653 if (value < 0) return -1;
1654 if ((value == 0) || (value == 0xff)) {
1657 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1658 if (clocks < DTL_TRC_MIN) {
1659 clocks = DTL_TRC_MIN;
1661 if (clocks > DTL_TRC_MAX) {
1665 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1666 old_clocks = ((dtl >> DTL_TRC_SHIFT) & DTL_TRC_MASK) + DTL_TRC_BASE;
1667 if (old_clocks > clocks) {
1668 clocks = old_clocks;
1670 dtl &= ~(DTL_TRC_MASK << DTL_TRC_SHIFT);
1671 dtl |= ((clocks - DTL_TRC_BASE) << DTL_TRC_SHIFT);
1672 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1676 static int update_dimm_Trfc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1678 unsigned clocks, old_clocks;
1681 value = spd_read_byte(ctrl->channel0[i], 42);
1682 if (value < 0) return -1;
1683 if ((value == 0) || (value == 0xff)) {
1684 value = param->tRFC;
1686 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1687 if (clocks < DTL_TRFC_MIN) {
1688 clocks = DTL_TRFC_MIN;
1690 if (clocks > DTL_TRFC_MAX) {
1693 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1694 old_clocks = ((dtl >> DTL_TRFC_SHIFT) & DTL_TRFC_MASK) + DTL_TRFC_BASE;
1695 if (old_clocks > clocks) {
1696 clocks = old_clocks;
1698 dtl &= ~(DTL_TRFC_MASK << DTL_TRFC_SHIFT);
1699 dtl |= ((clocks - DTL_TRFC_BASE) << DTL_TRFC_SHIFT);
1700 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1705 static int update_dimm_Trcd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1707 unsigned clocks, old_clocks;
1710 value = spd_read_byte(ctrl->channel0[i], 29);
1711 if (value < 0) return -1;
1712 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1713 if (clocks < DTL_TRCD_MIN) {
1714 clocks = DTL_TRCD_MIN;
1716 if (clocks > DTL_TRCD_MAX) {
1719 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1720 old_clocks = ((dtl >> DTL_TRCD_SHIFT) & DTL_TRCD_MASK) + DTL_TRCD_BASE;
1721 if (old_clocks > clocks) {
1722 clocks = old_clocks;
1724 dtl &= ~(DTL_TRCD_MASK << DTL_TRCD_SHIFT);
1725 dtl |= ((clocks - DTL_TRCD_BASE) << DTL_TRCD_SHIFT);
1726 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1730 static int update_dimm_Trrd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1732 unsigned clocks, old_clocks;
1735 value = spd_read_byte(ctrl->channel0[i], 28);
1736 if (value < 0) return -1;
1737 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1738 if (clocks < DTL_TRRD_MIN) {
1739 clocks = DTL_TRRD_MIN;
1741 if (clocks > DTL_TRRD_MAX) {
1744 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1745 old_clocks = ((dtl >> DTL_TRRD_SHIFT) & DTL_TRRD_MASK) + DTL_TRRD_BASE;
1746 if (old_clocks > clocks) {
1747 clocks = old_clocks;
1749 dtl &= ~(DTL_TRRD_MASK << DTL_TRRD_SHIFT);
1750 dtl |= ((clocks - DTL_TRRD_BASE) << DTL_TRRD_SHIFT);
1751 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1755 static int update_dimm_Tras(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1757 unsigned clocks, old_clocks;
1760 value = spd_read_byte(ctrl->channel0[i], 30);
1761 if (value < 0) return -1;
1762 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1763 if (clocks < DTL_TRAS_MIN) {
1764 clocks = DTL_TRAS_MIN;
1766 if (clocks > DTL_TRAS_MAX) {
1769 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1770 old_clocks = ((dtl >> DTL_TRAS_SHIFT) & DTL_TRAS_MASK) + DTL_TRAS_BASE;
1771 if (old_clocks > clocks) {
1772 clocks = old_clocks;
1774 dtl &= ~(DTL_TRAS_MASK << DTL_TRAS_SHIFT);
1775 dtl |= ((clocks - DTL_TRAS_BASE) << DTL_TRAS_SHIFT);
1776 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1780 static int update_dimm_Trp(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1782 unsigned clocks, old_clocks;
1785 value = spd_read_byte(ctrl->channel0[i], 27);
1786 if (value < 0) return -1;
1787 clocks = (value + (param->divisor << 1) - 1)/(param->divisor << 1);
1788 if (clocks < DTL_TRP_MIN) {
1789 clocks = DTL_TRP_MIN;
1791 if (clocks > DTL_TRP_MAX) {
1794 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1795 old_clocks = ((dtl >> DTL_TRP_SHIFT) & DTL_TRP_MASK) + DTL_TRP_BASE;
1796 if (old_clocks > clocks) {
1797 clocks = old_clocks;
1799 dtl &= ~(DTL_TRP_MASK << DTL_TRP_SHIFT);
1800 dtl |= ((clocks - DTL_TRP_BASE) << DTL_TRP_SHIFT);
1801 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1805 static void set_Twr(const struct mem_controller *ctrl, const struct mem_param *param)
1808 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1809 dtl &= ~(DTL_TWR_MASK << DTL_TWR_SHIFT);
1810 dtl |= (param->dtl_twr - DTL_TWR_BASE) << DTL_TWR_SHIFT;
1811 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1815 static void init_Tref(const struct mem_controller *ctrl, const struct mem_param *param)
1818 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1819 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1820 dth |= (param->dch_tref4k << DTH_TREF_SHIFT);
1821 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1824 static int update_dimm_Tref(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1828 unsigned tref, old_tref;
1829 value = spd_read_byte(ctrl->channel0[i], 3);
1830 if (value < 0) return -1;
1833 tref = param->dch_tref8k;
1835 tref = param->dch_tref4k;
1838 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1839 old_tref = (dth >> DTH_TREF_SHIFT) & DTH_TREF_MASK;
1840 if ((value == 12) && (old_tref == param->dch_tref4k)) {
1841 tref = param->dch_tref4k;
1843 tref = param->dch_tref8k;
1845 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1846 dth |= (tref << DTH_TREF_SHIFT);
1847 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1852 static int update_dimm_x4(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1857 value = spd_read_byte(ctrl->channel0[i], 13);
1862 dimm += DCL_x4DIMM_SHIFT;
1863 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1864 dcl &= ~(1 << dimm);
1868 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1872 static int update_dimm_ecc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1876 value = spd_read_byte(ctrl->channel0[i], 11);
1881 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1882 dcl &= ~DCL_DimmEccEn;
1883 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1888 static int count_dimms(const struct mem_controller *ctrl)
1893 for(index = 0; index < 8; index += 2) {
1895 csbase = pci_read_config32(ctrl->f2, (DRAM_CSBASE + (index << 2)));
1903 static void set_Twtr(const struct mem_controller *ctrl, const struct mem_param *param)
1907 clocks = 1; /* AMD says hard code this */
1908 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1909 dth &= ~(DTH_TWTR_MASK << DTH_TWTR_SHIFT);
1910 dth |= ((clocks - DTH_TWTR_BASE) << DTH_TWTR_SHIFT);
1911 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1914 static void set_Trwt(const struct mem_controller *ctrl, const struct mem_param *param)
1922 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1923 latency = (dtl >> DTL_TCL_SHIFT) & DTL_TCL_MASK;
1924 divisor = param->divisor;
1926 if (is_opteron(ctrl)) {
1927 if (latency == DTL_CL_2) {
1928 if (divisor == ((6 << 0) + 0)) {
1932 else if (divisor > ((6 << 0)+0)) {
1933 /* 100Mhz && 133Mhz */
1937 else if (latency == DTL_CL_2_5) {
1940 else if (latency == DTL_CL_3) {
1941 if (divisor == ((6 << 0)+0)) {
1945 else if (divisor > ((6 << 0)+0)) {
1946 /* 100Mhz && 133Mhz */
1951 else /* Athlon64 */ {
1952 if (is_registered(ctrl)) {
1953 if (latency == DTL_CL_2) {
1956 else if (latency == DTL_CL_2_5) {
1959 else if (latency == DTL_CL_3) {
1963 else /* Unbuffered */{
1964 if (latency == DTL_CL_2) {
1967 else if (latency == DTL_CL_2_5) {
1970 else if (latency == DTL_CL_3) {
1975 if ((clocks < DTH_TRWT_MIN) || (clocks > DTH_TRWT_MAX)) {
1976 die("Unknown Trwt\r\n");
1979 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1980 dth &= ~(DTH_TRWT_MASK << DTH_TRWT_SHIFT);
1981 dth |= ((clocks - DTH_TRWT_BASE) << DTH_TRWT_SHIFT);
1982 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1986 static void set_Twcl(const struct mem_controller *ctrl, const struct mem_param *param)
1988 /* Memory Clocks after CAS# */
1991 if (is_registered(ctrl)) {
1996 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1997 dth &= ~(DTH_TWCL_MASK << DTH_TWCL_SHIFT);
1998 dth |= ((clocks - DTH_TWCL_BASE) << DTH_TWCL_SHIFT);
1999 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
2003 static void set_read_preamble(const struct mem_controller *ctrl, const struct mem_param *param)
2007 unsigned rdpreamble;
2008 divisor = param->divisor;
2009 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2010 dch &= ~(DCH_RDPREAMBLE_MASK << DCH_RDPREAMBLE_SHIFT);
2012 if (is_registered(ctrl)) {
2013 if (divisor == ((10 << 1)+0)) {
2015 rdpreamble = ((9 << 1)+ 0);
2017 else if (divisor == ((7 << 1)+1)) {
2019 rdpreamble = ((8 << 1)+0);
2021 else if (divisor == ((6 << 1)+0)) {
2023 rdpreamble = ((7 << 1)+1);
2025 else if (divisor == ((5 << 1)+0)) {
2027 rdpreamble = ((7 << 1)+0);
2034 for(i = 0; i < 4; i++) {
2035 if (ctrl->channel0[i]) {
2039 if (divisor == ((10 << 1)+0)) {
2043 rdpreamble = ((9 << 1)+0);
2046 rdpreamble = ((14 << 1)+0);
2049 else if (divisor == ((7 << 1)+1)) {
2053 rdpreamble = ((7 << 1)+0);
2056 rdpreamble = ((11 << 1)+0);
2059 else if (divisor == ((6 << 1)+0)) {
2063 rdpreamble = ((7 << 1)+0);
2066 rdpreamble = ((9 << 1)+0);
2069 else if (divisor == ((5 << 1)+0)) {
2073 rdpreamble = ((5 << 1)+0);
2076 rdpreamble = ((7 << 1)+0);
2080 if ((rdpreamble < DCH_RDPREAMBLE_MIN) || (rdpreamble > DCH_RDPREAMBLE_MAX)) {
2081 die("Unknown rdpreamble");
2083 dch |= (rdpreamble - DCH_RDPREAMBLE_BASE) << DCH_RDPREAMBLE_SHIFT;
2084 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2087 static void set_max_async_latency(const struct mem_controller *ctrl, const struct mem_param *param)
2093 dimms = count_dimms(ctrl);
2095 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2096 dch &= ~(DCH_ASYNC_LAT_MASK << DCH_ASYNC_LAT_SHIFT);
2098 if (is_registered(ctrl)) {
2110 die("Too many unbuffered dimms");
2112 else if (dimms == 3) {
2121 dch |= ((async_lat - DCH_ASYNC_LAT_BASE) << DCH_ASYNC_LAT_SHIFT);
2122 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2125 static void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct mem_param *param)
2128 /* AMD says to Hardcode this */
2129 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2130 dch &= ~(DCH_IDLE_LIMIT_MASK << DCH_IDLE_LIMIT_SHIFT);
2131 dch |= DCH_IDLE_LIMIT_16 << DCH_IDLE_LIMIT_SHIFT;
2132 dch |= DCH_DYN_IDLE_CTR_EN;
2133 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2136 static long spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param, long dimm_mask)
2140 init_Tref(ctrl, param);
2141 for(i = 0; i < DIMM_SOCKETS; i++) {
2143 if (!(dimm_mask & (1 << i))) {
2146 /* DRAM Timing Low Register */
2147 if ((rc = update_dimm_Trc (ctrl, param, i)) <= 0) goto dimm_err;
2148 if ((rc = update_dimm_Trfc(ctrl, param, i)) <= 0) goto dimm_err;
2149 if ((rc = update_dimm_Trcd(ctrl, param, i)) <= 0) goto dimm_err;
2150 if ((rc = update_dimm_Trrd(ctrl, param, i)) <= 0) goto dimm_err;
2151 if ((rc = update_dimm_Tras(ctrl, param, i)) <= 0) goto dimm_err;
2152 if ((rc = update_dimm_Trp (ctrl, param, i)) <= 0) goto dimm_err;
2154 /* DRAM Timing High Register */
2155 if ((rc = update_dimm_Tref(ctrl, param, i)) <= 0) goto dimm_err;
2158 /* DRAM Config Low */
2159 if ((rc = update_dimm_x4 (ctrl, param, i)) <= 0) goto dimm_err;
2160 if ((rc = update_dimm_ecc(ctrl, param, i)) <= 0) goto dimm_err;
2166 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
2168 /* DRAM Timing Low Register */
2169 set_Twr(ctrl, param);
2171 /* DRAM Timing High Register */
2172 set_Twtr(ctrl, param);
2173 set_Trwt(ctrl, param);
2174 set_Twcl(ctrl, param);
2176 /* DRAM Config High */
2177 set_read_preamble(ctrl, param);
2178 set_max_async_latency(ctrl, param);
2179 set_idle_cycle_limit(ctrl, param);
2183 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
2185 struct spd_set_memclk_result result;
2186 const struct mem_param *param;
2188 hw_enable_ecc(ctrl);
2189 activate_spd_rom(ctrl);
2190 dimm_mask = spd_detect_dimms(ctrl);
2191 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
2192 print_debug("No memory for this cpu\r\n");
2195 dimm_mask = spd_enable_2channels(ctrl, dimm_mask);
2198 dimm_mask = spd_set_ram_size(ctrl , dimm_mask);
2201 dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask);
2204 result = spd_set_memclk(ctrl, dimm_mask);
2205 param = result.param;
2206 dimm_mask = result.dimm_mask;
2209 dimm_mask = spd_set_dram_timing(ctrl, param , dimm_mask);
2215 /* Unrecoverable error reading SPD data */
2216 print_err("SPD error - reset\r\n");
2221 #define TIMEOUT_LOOPS 300000
2222 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
2226 /* Error if I don't have memory */
2227 if (memory_end_k(ctrl, controllers) == 0) {
2228 die("No memory\r\n");
2231 /* Before enabling memory start the memory clocks */
2232 for(i = 0; i < controllers; i++) {
2234 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2235 if (dch & (DCH_MEMCLK_EN0|DCH_MEMCLK_EN1|DCH_MEMCLK_EN2|DCH_MEMCLK_EN3)) {
2236 dch |= DCH_MEMCLK_VALID;
2237 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2240 /* Disable dram receivers */
2242 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2243 dcl |= DCL_DisInRcvrs;
2244 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2248 /* And if necessary toggle the the reset on the dimms by hand */
2249 memreset(controllers, ctrl);
2251 for(i = 0; i < controllers; i++) {
2253 /* Skip everything if I don't have any memory on this controller */
2254 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2255 if (!(dch & DCH_MEMCLK_VALID)) {
2259 /* Toggle DisDqsHys to get it working */
2260 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2261 if (dcl & DCL_DimmEccEn) {
2263 print_spew("ECC enabled\r\n");
2264 mnc = pci_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
2266 if (dcl & DCL_128BitEn) {
2267 mnc |= MNC_CHIPKILL_EN;
2269 pci_write_config32(ctrl[i].f3, MCA_NB_CONFIG, mnc);
2271 dcl |= DCL_DisDqsHys;
2272 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2273 dcl &= ~DCL_DisDqsHys;
2274 dcl &= ~DCL_DLL_Disable;
2277 dcl |= DCL_DramInit;
2278 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2281 for(i = 0; i < controllers; i++) {
2283 /* Skip everything if I don't have any memory on this controller */
2284 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2285 if (!(dch & DCH_MEMCLK_VALID)) {
2289 print_debug("Initializing memory: ");
2292 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2294 if ((loops & 1023) == 0) {
2297 } while(((dcl & DCL_DramInit) != 0) && (loops < TIMEOUT_LOOPS));
2298 if (loops >= TIMEOUT_LOOPS) {
2299 print_debug(" failed\r\n");
2302 if (!is_cpu_pre_c0()) {
2303 /* Wait until it is safe to touch memory */
2304 dcl &= ~(DCL_MemClrStatus | DCL_DramEnable);
2305 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2307 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2308 } while(((dcl & DCL_MemClrStatus) == 0) || ((dcl & DCL_DramEnable) == 0) );
2310 print_debug(" done\r\n");
2313 /* Make certain the first 1M of memory is intialized */
2317 /* Save the value of msr_201 */
2318 msr_201 = rdmsr(0x201);
2320 print_debug("Clearing LinuxBIOS memory: ");
2324 "movl %%cr0, %0\n\t"
2325 "orl $0x40000000, %0\n\t"
2326 "movl %0, %%cr0\n\t"
2330 /* Disable fixed mtrrs */
2331 msr = rdmsr(MTRRdefType_MSR);
2333 wrmsr(MTRRdefType_MSR, msr);
2336 /* Set the variable mtrrs to write combine */
2338 msr.lo = 0 | MTRR_TYPE_WRCOMB;
2341 /* Set the limit to 1M of ram */
2342 msr.hi = 0x000000ff;
2343 msr.lo = (~((CONFIG_LB_MEM_TOPK << 10) - 1)) | 0x800;
2348 "movl %%cr0, %0\n\t"
2349 "andl $0x9fffffff, %0\n\t"
2350 "movl %0, %%cr0\n\t"
2354 /* clear memory 1meg */
2357 "movl %0, %%fs:(%1)\n\t"
2362 : "a" (0), "D" (0), "c" (1024*1024)
2367 "movl %%cr0, %0\n\t"
2368 "orl $0x40000000, %0\n\t"
2369 "movl %0, %%cr0\n\t"
2373 /* restore msr registers */
2374 msr = rdmsr(MTRRdefType_MSR);
2376 wrmsr(MTRRdefType_MSR, msr);
2379 /* Restore the variable mtrrs */
2381 msr.lo = MTRR_TYPE_WRBACK;
2383 wrmsr(0x201, msr_201);
2387 "movl %%cr0, %0\n\t"
2388 "andl $0x9fffffff, %0\n\t"
2389 "movl %0, %%cr0\n\t"
2393 print_debug(" done\r\n");