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"
8 static void setup_resource_map(const unsigned int *register_values, int max)
11 print_debug("setting up resource map....");
15 for(i = 0; i < max; i += 3) {
20 print_debug_hex32(register_values[i]);
22 print_debug_hex32(register_values[i+2]);
25 dev = register_values[i] & ~0xff;
26 where = register_values[i] & 0xff;
27 reg = pci_read_config32(dev, where);
28 reg &= register_values[i+1];
29 reg |= register_values[i+2];
30 pci_write_config32(dev, where, reg);
32 reg = pci_read_config32(register_values[i]);
33 reg &= register_values[i+1];
34 reg |= register_values[i+2] & ~register_values[i+1];
35 pci_write_config32(register_values[i], reg);
38 print_debug("done.\r\n");
41 static void setup_default_resource_map(void)
43 static const unsigned int register_values[] = {
44 /* Careful set limit registers before base registers which contain the enables */
45 /* DRAM Limit i Registers
54 * [ 2: 0] Destination Node ID
64 * [10: 8] Interleave select
65 * specifies the values of A[14:12] to use with interleave enable.
67 * [31:16] DRAM Limit Address i Bits 39-24
68 * This field defines the upper address bits of a 40 bit address
69 * that define the end of the DRAM region.
71 PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
72 PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
73 PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
74 PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
75 PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
76 PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
77 PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
78 PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
79 /* DRAM Base i Registers
91 * [ 1: 1] Write Enable
95 * [10: 8] Interleave Enable
97 * 001 = Interleave on A[12] (2 nodes)
99 * 011 = Interleave on A[12] and A[14] (4 nodes)
103 * 111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
105 * [13:16] DRAM Base Address i Bits 39-24
106 * This field defines the upper address bits of a 40-bit address
107 * that define the start of the DRAM region.
109 PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
110 PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
111 PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
112 PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
113 PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
114 PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
115 PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
116 PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
118 /* Memory-Mapped I/O Limit i Registers
127 * [ 2: 0] Destination Node ID
137 * [ 5: 4] Destination Link ID
144 * 0 = CPU writes may be posted
145 * 1 = CPU writes must be non-posted
146 * [31: 8] Memory-Mapped I/O Limit Address i (39-16)
147 * This field defines the upp adddress bits of a 40-bit address that
148 * defines the end of a memory-mapped I/O region n
150 PCI_ADDR(0, 0x18, 1, 0x84), 0x00000048, 0x00000000,
151 PCI_ADDR(0, 0x18, 1, 0x8C), 0x00000048, 0x00000000,
152 PCI_ADDR(0, 0x18, 1, 0x94), 0x00000048, 0x00000000,
153 PCI_ADDR(0, 0x18, 1, 0x9C), 0x00000048, 0x00000000,
154 PCI_ADDR(0, 0x18, 1, 0xA4), 0x00000048, 0x00000000,
155 PCI_ADDR(0, 0x18, 1, 0xAC), 0x00000048, 0x00000000,
156 PCI_ADDR(0, 0x18, 1, 0xB4), 0x00000048, 0x00000000,
157 PCI_ADDR(0, 0x18, 1, 0xBC), 0x00000048, 0x00ffff00,
159 /* Memory-Mapped I/O Base i Registers
168 * [ 0: 0] Read Enable
171 * [ 1: 1] Write Enable
172 * 0 = Writes disabled
174 * [ 2: 2] Cpu Disable
175 * 0 = Cpu can use this I/O range
176 * 1 = Cpu requests do not use this I/O range
178 * 0 = base/limit registers i are read/write
179 * 1 = base/limit registers i are read-only
181 * [31: 8] Memory-Mapped I/O Base Address i (39-16)
182 * This field defines the upper address bits of a 40bit address
183 * that defines the start of memory-mapped I/O region i
185 PCI_ADDR(0, 0x18, 1, 0x80), 0x000000f0, 0x00000000,
186 PCI_ADDR(0, 0x18, 1, 0x88), 0x000000f0, 0x00000000,
187 PCI_ADDR(0, 0x18, 1, 0x90), 0x000000f0, 0x00000000,
188 PCI_ADDR(0, 0x18, 1, 0x98), 0x000000f0, 0x00000000,
189 PCI_ADDR(0, 0x18, 1, 0xA0), 0x000000f0, 0x00000000,
190 PCI_ADDR(0, 0x18, 1, 0xA8), 0x000000f0, 0x00000000,
191 PCI_ADDR(0, 0x18, 1, 0xB0), 0x000000f0, 0x00000000,
192 PCI_ADDR(0, 0x18, 1, 0xB8), 0x000000f0, 0x00fc0003,
194 /* PCI I/O Limit i Registers
199 * [ 2: 0] Destination Node ID
209 * [ 5: 4] Destination Link ID
215 * [24:12] PCI I/O Limit Address i
216 * This field defines the end of PCI I/O region n
219 PCI_ADDR(0, 0x18, 1, 0xC4), 0xFE000FC8, 0x01fff000,
220 PCI_ADDR(0, 0x18, 1, 0xCC), 0xFE000FC8, 0x00000000,
221 PCI_ADDR(0, 0x18, 1, 0xD4), 0xFE000FC8, 0x00000000,
222 PCI_ADDR(0, 0x18, 1, 0xDC), 0xFE000FC8, 0x00000000,
224 /* PCI I/O Base i Registers
229 * [ 0: 0] Read Enable
232 * [ 1: 1] Write Enable
233 * 0 = Writes Disabled
237 * 0 = VGA matches Disabled
238 * 1 = matches all address < 64K and where A[9:0] is in the
239 * range 3B0-3BB or 3C0-3DF independen of the base & limit registers
241 * 0 = ISA matches Disabled
242 * 1 = Blocks address < 64K and in the last 768 bytes of eack 1K block
243 * from matching agains this base/limit pair
245 * [24:12] PCI I/O Base i
246 * This field defines the start of PCI I/O region n
249 PCI_ADDR(0, 0x18, 1, 0xC0), 0xFE000FCC, 0x00000003,
250 PCI_ADDR(0, 0x18, 1, 0xC8), 0xFE000FCC, 0x00000000,
251 PCI_ADDR(0, 0x18, 1, 0xD0), 0xFE000FCC, 0x00000000,
252 PCI_ADDR(0, 0x18, 1, 0xD8), 0xFE000FCC, 0x00000000,
254 /* Config Base and Limit i Registers
259 * [ 0: 0] Read Enable
262 * [ 1: 1] Write Enable
263 * 0 = Writes Disabled
265 * [ 2: 2] Device Number Compare Enable
266 * 0 = The ranges are based on bus number
267 * 1 = The ranges are ranges of devices on bus 0
269 * [ 6: 4] Destination Node
279 * [ 9: 8] Destination Link
285 * [23:16] Bus Number Base i
286 * This field defines the lowest bus number in configuration region i
287 * [31:24] Bus Number Limit i
288 * This field defines the highest bus number in configuration regin i
290 PCI_ADDR(0, 0x18, 1, 0xE0), 0x0000FC88, 0xff000003,
291 PCI_ADDR(0, 0x18, 1, 0xE4), 0x0000FC88, 0x00000000,
292 PCI_ADDR(0, 0x18, 1, 0xE8), 0x0000FC88, 0x00000000,
293 PCI_ADDR(0, 0x18, 1, 0xEC), 0x0000FC88, 0x00000000,
296 max = sizeof(register_values)/sizeof(register_values[0]);
297 setup_resource_map(register_values, max);
300 static void sdram_set_registers(const struct mem_controller *ctrl)
302 static const unsigned int register_values[] = {
304 /* Careful set limit registers before base registers which contain the enables */
305 /* DRAM Limit i Registers
314 * [ 2: 0] Destination Node ID
324 * [10: 8] Interleave select
325 * specifies the values of A[14:12] to use with interleave enable.
327 * [31:16] DRAM Limit Address i Bits 39-24
328 * This field defines the upper address bits of a 40 bit address
329 * that define the end of the DRAM region.
331 PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
332 PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
333 PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
334 PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
335 PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
336 PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
337 PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
338 PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
339 /* DRAM Base i Registers
348 * [ 0: 0] Read Enable
351 * [ 1: 1] Write Enable
352 * 0 = Writes Disabled
355 * [10: 8] Interleave Enable
356 * 000 = No interleave
357 * 001 = Interleave on A[12] (2 nodes)
359 * 011 = Interleave on A[12] and A[14] (4 nodes)
363 * 111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
365 * [13:16] DRAM Base Address i Bits 39-24
366 * This field defines the upper address bits of a 40-bit address
367 * that define the start of the DRAM region.
369 PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
370 PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
371 PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
372 PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
373 PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
374 PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
375 PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
376 PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
378 /* DRAM CS Base Address i Registers
387 * [ 0: 0] Chip-Select Bank Enable
391 * [15: 9] Base Address (19-13)
392 * An optimization used when all DIMM are the same size...
394 * [31:21] Base Address (35-25)
395 * This field defines the top 11 addresses bit of a 40-bit
396 * address that define the memory address space. These
397 * bits decode 32-MByte blocks of memory.
399 PCI_ADDR(0, 0x18, 2, 0x40), 0x001f01fe, 0x00000000,
400 PCI_ADDR(0, 0x18, 2, 0x44), 0x001f01fe, 0x00000000,
401 PCI_ADDR(0, 0x18, 2, 0x48), 0x001f01fe, 0x00000000,
402 PCI_ADDR(0, 0x18, 2, 0x4C), 0x001f01fe, 0x00000000,
403 PCI_ADDR(0, 0x18, 2, 0x50), 0x001f01fe, 0x00000000,
404 PCI_ADDR(0, 0x18, 2, 0x54), 0x001f01fe, 0x00000000,
405 PCI_ADDR(0, 0x18, 2, 0x58), 0x001f01fe, 0x00000000,
406 PCI_ADDR(0, 0x18, 2, 0x5C), 0x001f01fe, 0x00000000,
407 /* DRAM CS Mask Address i Registers
416 * Select bits to exclude from comparison with the DRAM Base address register.
418 * [15: 9] Address Mask (19-13)
419 * Address to be excluded from the optimized case
421 * [29:21] Address Mask (33-25)
422 * The bits with an address mask of 1 are excluded from address comparison
426 PCI_ADDR(0, 0x18, 2, 0x60), 0xC01f01ff, 0x00000000,
427 PCI_ADDR(0, 0x18, 2, 0x64), 0xC01f01ff, 0x00000000,
428 PCI_ADDR(0, 0x18, 2, 0x68), 0xC01f01ff, 0x00000000,
429 PCI_ADDR(0, 0x18, 2, 0x6C), 0xC01f01ff, 0x00000000,
430 PCI_ADDR(0, 0x18, 2, 0x70), 0xC01f01ff, 0x00000000,
431 PCI_ADDR(0, 0x18, 2, 0x74), 0xC01f01ff, 0x00000000,
432 PCI_ADDR(0, 0x18, 2, 0x78), 0xC01f01ff, 0x00000000,
433 PCI_ADDR(0, 0x18, 2, 0x7C), 0xC01f01ff, 0x00000000,
434 /* DRAM Bank Address Mapping Register
436 * Specify the memory module size
441 * 000 = 32Mbyte (Rows = 12 & Col = 8)
442 * 001 = 64Mbyte (Rows = 12 & Col = 9)
443 * 010 = 128Mbyte (Rows = 13 & Col = 9)|(Rows = 12 & Col = 10)
444 * 011 = 256Mbyte (Rows = 13 & Col = 10)|(Rows = 12 & Col = 11)
445 * 100 = 512Mbyte (Rows = 13 & Col = 11)|(Rows = 14 & Col = 10)
446 * 101 = 1Gbyte (Rows = 14 & Col = 11)|(Rows = 13 & Col = 12)
447 * 110 = 2Gbyte (Rows = 14 & Col = 12)
454 PCI_ADDR(0, 0x18, 2, 0x80), 0xffff8888, 0x00000000,
455 /* DRAM Timing Low Register
457 * [ 2: 0] Tcl (Cas# Latency, Cas# to read-data-valid)
467 * [ 7: 4] Trc (Row Cycle Time, Ras#-active to Ras#-active/bank auto refresh)
468 * 0000 = 7 bus clocks
469 * 0001 = 8 bus clocks
471 * 1110 = 21 bus clocks
472 * 1111 = 22 bus clocks
473 * [11: 8] Trfc (Row refresh Cycle time, Auto-refresh-active to RAS#-active or RAS#auto-refresh)
474 * 0000 = 9 bus clocks
475 * 0010 = 10 bus clocks
477 * 1110 = 23 bus clocks
478 * 1111 = 24 bus clocks
479 * [14:12] Trcd (Ras#-active to Case#-read/write Delay)
489 * [18:16] Trrd (Ras# to Ras# Delay)
499 * [23:20] Tras (Minmum Ras# Active Time)
500 * 0000 to 0100 = reserved
501 * 0101 = 5 bus clocks
503 * 1111 = 15 bus clocks
504 * [26:24] Trp (Row Precharge Time)
514 * [28:28] Twr (Write Recovery Time)
519 PCI_ADDR(0, 0x18, 2, 0x88), 0xe8088008, 0x02522001 /* 0x03623125 */ ,
520 /* DRAM Timing High Register
522 * [ 0: 0] Twtr (Write to Read Delay)
526 * [ 6: 4] Trwt (Read to Write Delay)
536 * [12: 8] Tref (Refresh Rate)
537 * 00000 = 100Mhz 4K rows
538 * 00001 = 133Mhz 4K rows
539 * 00010 = 166Mhz 4K rows
540 * 00011 = 200Mhz 4K rows
541 * 01000 = 100Mhz 8K/16K rows
542 * 01001 = 133Mhz 8K/16K rows
543 * 01010 = 166Mhz 8K/16K rows
544 * 01011 = 200Mhz 8K/16K rows
546 * [22:20] Twcl (Write CAS Latency)
547 * 000 = 1 Mem clock after CAS# (Unbuffered Dimms)
548 * 001 = 2 Mem clocks after CAS# (Registered Dimms)
551 PCI_ADDR(0, 0x18, 2, 0x8c), 0xff8fe08e, (0 << 20)|(0 << 8)|(0 << 4)|(0 << 0),
552 /* DRAM Config Low Register
554 * [ 0: 0] DLL Disable
563 * [ 3: 3] Disable DQS Hystersis (FIXME handle this one carefully)
564 * 0 = Enable DQS input filter
565 * 1 = Disable DQS input filtering
568 * 0 = Initialization done or not yet started.
569 * 1 = Initiate DRAM intialization sequence
570 * [ 9: 9] SO-Dimm Enable
572 * 1 = SO-Dimms present
574 * 0 = DRAM not enabled
575 * 1 = DRAM initialized and enabled
576 * [11:11] Memory Clear Status
577 * 0 = Memory Clear function has not completed
578 * 1 = Memory Clear function has completed
579 * [12:12] Exit Self-Refresh
580 * 0 = Exit from self-refresh done or not yet started
581 * 1 = DRAM exiting from self refresh
582 * [13:13] Self-Refresh Status
583 * 0 = Normal Operation
584 * 1 = Self-refresh mode active
585 * [15:14] Read/Write Queue Bypass Count
590 * [16:16] 128-bit/64-Bit
591 * 0 = 64bit Interface to DRAM
592 * 1 = 128bit Interface to DRAM
593 * [17:17] DIMM ECC Enable
594 * 0 = Some DIMMs do not have ECC
595 * 1 = ALL DIMMS have ECC bits
596 * [18:18] UnBuffered DIMMs
598 * 1 = Unbuffered DIMMS
599 * [19:19] Enable 32-Byte Granularity
600 * 0 = Optimize for 64byte bursts
601 * 1 = Optimize for 32byte bursts
602 * [20:20] DIMM 0 is x4
603 * [21:21] DIMM 1 is x4
604 * [22:22] DIMM 2 is x4
605 * [23:23] DIMM 3 is x4
607 * 1 = x4 DIMM present
608 * [24:24] Disable DRAM Receivers
609 * 0 = Receivers enabled
610 * 1 = Receivers disabled
612 * 000 = Arbiters chois is always respected
613 * 001 = Oldest entry in DCQ can be bypassed 1 time
614 * 010 = Oldest entry in DCQ can be bypassed 2 times
615 * 011 = Oldest entry in DCQ can be bypassed 3 times
616 * 100 = Oldest entry in DCQ can be bypassed 4 times
617 * 101 = Oldest entry in DCQ can be bypassed 5 times
618 * 110 = Oldest entry in DCQ can be bypassed 6 times
619 * 111 = Oldest entry in DCQ can be bypassed 7 times
622 PCI_ADDR(0, 0x18, 2, 0x90), 0xf0000000,
624 (0 << 23)|(0 << 22)|(0 << 21)|(0 << 20)|
625 (1 << 19)|(0 << 18)|(1 << 17)|(0 << 16)|
626 (2 << 14)|(0 << 13)|(0 << 12)|
627 (0 << 11)|(0 << 10)|(0 << 9)|(0 << 8)|
628 (0 << 3) |(0 << 1) |(0 << 0),
629 /* DRAM Config High Register
631 * [ 0: 3] Maximum Asynchronous Latency
636 * [11: 8] Read Preamble
654 * [18:16] Idle Cycle Limit
663 * [19:19] Dynamic Idle Cycle Center Enable
664 * 0 = Use Idle Cycle Limit
665 * 1 = Generate a dynamic Idle cycle limit
666 * [22:20] DRAM MEMCLK Frequency
676 * [25:25] Memory Clock Ratio Valid (FIXME carefully enable memclk)
677 * 0 = Disable MemClks
679 * [26:26] Memory Clock 0 Enable
682 * [27:27] Memory Clock 1 Enable
685 * [28:28] Memory Clock 2 Enable
688 * [29:29] Memory Clock 3 Enable
693 PCI_ADDR(0, 0x18, 2, 0x94), 0xc180f0f0,
694 (0 << 29)|(0 << 28)|(0 << 27)|(0 << 26)|(0 << 25)|
695 (0 << 20)|(0 << 19)|(DCH_IDLE_LIMIT_16 << 16)|(0 << 8)|(0 << 0),
696 /* DRAM Delay Line Register
698 * Adjust the skew of the input DQS strobe relative to DATA
700 * [23:16] Delay Line Adjust
701 * Adjusts the DLL derived PDL delay by one or more delay stages
702 * in either the faster or slower direction.
703 * [24:24} Adjust Slower
705 * 1 = Adj is used to increase the PDL delay
706 * [25:25] Adjust Faster
708 * 1 = Adj is used to decrease the PDL delay
711 PCI_ADDR(0, 0x18, 2, 0x98), 0xfc00ffff, 0x00000000,
712 /* DRAM Scrub Control Register
714 * [ 4: 0] DRAM Scrube Rate
716 * [12: 8] L2 Scrub Rate
718 * [20:16] Dcache Scrub
721 * 00000 = Do not scrub
743 * All Others = Reserved
745 PCI_ADDR(0, 0x18, 3, 0x58), 0xffe0e0e0, 0x00000000,
746 /* DRAM Scrub Address Low Register
748 * [ 0: 0] DRAM Scrubber Redirect Enable
750 * 1 = Scrubber Corrects errors found in normal operation
752 * [31: 6] DRAM Scrub Address 31-6
754 PCI_ADDR(0, 0x18, 3, 0x5C), 0x0000003e, 0x00000000,
755 /* DRAM Scrub Address High Register
757 * [ 7: 0] DRAM Scrubb Address 39-32
760 PCI_ADDR(0, 0x18, 3, 0x60), 0xffffff00, 0x00000000,
764 print_spew("setting up CPU");
765 print_spew_hex8(ctrl->node_id);
766 print_spew(" northbridge registers\r\n");
767 max = sizeof(register_values)/sizeof(register_values[0]);
768 for(i = 0; i < max; i += 3) {
773 print_spew_hex32(register_values[i]);
775 print_spew_hex32(register_values[i+2]);
778 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x18, 0) + ctrl->f0;
779 where = register_values[i] & 0xff;
780 reg = pci_read_config32(dev, where);
781 reg &= register_values[i+1];
782 reg |= register_values[i+2];
783 pci_write_config32(dev, where, reg);
786 reg = pci_read_config32(register_values[i]);
787 reg &= register_values[i+1];
788 reg |= register_values[i+2];
789 pci_write_config32(register_values[i], reg);
792 print_spew("done.\r\n");
796 static void hw_enable_ecc(const struct mem_controller *ctrl)
799 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
800 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
801 dcl &= ~DCL_DimmEccEn;
802 if (nbcap & NBCAP_ECC) {
803 dcl |= DCL_DimmEccEn;
805 if (read_option(CMOS_VSTART_ECC_memory, CMOS_VLEN_ECC_memory, 1) == 0) {
806 dcl &= ~DCL_DimmEccEn;
808 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
812 static int is_dual_channel(const struct mem_controller *ctrl)
815 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
816 return dcl & DCL_128BitEn;
819 static int is_opteron(const struct mem_controller *ctrl)
821 /* Test to see if I am an Opteron.
822 * FIXME Testing dual channel capability is correct for now
823 * but a beter test is probably required.
825 #warning "FIXME implement a better test for opterons"
827 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
828 return !!(nbcap & NBCAP_128Bit);
831 static int is_registered(const struct mem_controller *ctrl)
833 /* Test to see if we are dealing with registered SDRAM.
834 * If we are not registered we are unbuffered.
835 * This function must be called after spd_handle_unbuffered_dimms.
838 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
839 return !(dcl & DCL_UnBufDimm);
847 static struct dimm_size spd_get_dimm_size(unsigned device)
849 /* Calculate the log base 2 size of a DIMM in bits */
855 /* Note it might be easier to use byte 31 here, it has the DIMM size as
856 * a multiple of 4MB. The way we do it now we can size both
857 * sides of an assymetric dimm.
859 value = spd_read_byte(device, 3); /* rows */
860 if (value < 0) goto hw_err;
861 if ((value & 0xf) == 0) goto val_err;
862 sz.side1 += value & 0xf;
864 value = spd_read_byte(device, 4); /* columns */
865 if (value < 0) goto hw_err;
866 if ((value & 0xf) == 0) goto val_err;
867 sz.side1 += value & 0xf;
869 value = spd_read_byte(device, 17); /* banks */
870 if (value < 0) goto hw_err;
871 if ((value & 0xff) == 0) goto val_err;
872 sz.side1 += log2(value & 0xff);
874 /* Get the module data width and convert it to a power of two */
875 value = spd_read_byte(device, 7); /* (high byte) */
876 if (value < 0) goto hw_err;
880 low = spd_read_byte(device, 6); /* (low byte) */
881 if (low < 0) goto hw_err;
882 value = value | (low & 0xff);
883 if ((value != 72) && (value &= 64)) goto val_err;
884 sz.side1 += log2(value);
887 value = spd_read_byte(device, 5); /* number of physical banks */
888 if (value < 0) goto hw_err;
889 if (value == 1) goto out;
890 if (value != 2) goto val_err;
892 /* Start with the symmetrical case */
895 value = spd_read_byte(device, 3); /* rows */
896 if (value < 0) goto hw_err;
897 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
898 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
899 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
901 value = spd_read_byte(device, 4); /* columns */
902 if (value < 0) goto hw_err;
903 if ((value & 0xff) == 0) goto val_err;
904 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
905 sz.side2 += ((value >> 4) & 0x0f); /* Add in columsn on side 2 */
909 die("Bad SPD value\r\n");
910 /* If an hw_error occurs report that I have no memory */
918 static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz, unsigned index)
920 uint32_t base0, base1, map;
923 if (sz.side1 != sz.side2) {
926 map = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
927 map &= ~(0xf << (index + 4));
929 /* For each base register.
930 * Place the dimm size in 32 MB quantities in the bits 31 - 21.
931 * The initialize dimm size is in bits.
932 * Set the base enable bit0.
937 /* Make certain side1 of the dimm is at least 32MB */
938 if (sz.side1 >= (25 +3)) {
939 map |= (sz.side1 - (25 + 3)) << (index *4);
940 base0 = (1 << ((sz.side1 - (25 + 3)) + 21)) | 1;
942 /* Make certain side2 of the dimm is at least 32MB */
943 if (sz.side2 >= (25 + 3)) {
944 base1 = (1 << ((sz.side2 - (25 + 3)) + 21)) | 1;
947 /* Double the size if we are using dual channel memory */
948 if (is_dual_channel(ctrl)) {
949 base0 = (base0 << 1) | (base0 & 1);
950 base1 = (base1 << 1) | (base1 & 1);
953 /* Clear the reserved bits */
954 base0 &= ~0x001ffffe;
955 base1 &= ~0x001ffffe;
957 /* Set the appropriate DIMM base address register */
958 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
959 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
960 pci_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
962 /* Enable the memory clocks for this DIMM */
964 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
965 dch |= DCH_MEMCLK_EN0 << index;
966 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
970 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
974 for(i = 0; i < DIMM_SOCKETS; i++) {
976 if (!(dimm_mask & (1 << i))) {
979 sz = spd_get_dimm_size(ctrl->channel0[i]);
981 return -1; /* Report SPD error */
983 set_dimm_size(ctrl, sz, i);
988 static void route_dram_accesses(const struct mem_controller *ctrl,
989 unsigned long base_k, unsigned long limit_k)
991 /* Route the addresses to the controller node */
996 unsigned limit_reg, base_reg;
999 node_id = ctrl->node_id;
1000 index = (node_id << 3);
1001 limit = (limit_k << 2);
1002 limit &= 0xffff0000;
1003 limit -= 0x00010000;
1004 limit |= ( 0 << 8) | (node_id << 0);
1005 base = (base_k << 2);
1007 base |= (0 << 8) | (1<<1) | (1<<0);
1009 limit_reg = 0x44 + index;
1010 base_reg = 0x40 + index;
1011 for(device = PCI_DEV(0, 0x18, 1); device <= PCI_DEV(0, 0x1f, 1); device += PCI_DEV(0, 1, 0)) {
1012 pci_write_config32(device, limit_reg, limit);
1013 pci_write_config32(device, base_reg, base);
1017 static void set_top_mem(unsigned tom_k)
1019 /* Error if I don't have memory */
1024 /* Report the amount of memory. */
1025 print_spew("RAM: 0x");
1026 print_spew_hex32(tom_k);
1027 print_spew(" KB\r\n");
1029 /* Now set top of memory */
1031 msr.lo = (tom_k & 0x003fffff) << 10;
1032 msr.hi = (tom_k & 0xffc00000) >> 22;
1033 wrmsr(TOP_MEM2, msr);
1035 /* Leave a 64M hole between TOP_MEM and TOP_MEM2
1036 * so I can see my rom chip and other I/O devices.
1038 if (tom_k >= 0x003f0000) {
1041 msr.lo = (tom_k & 0x003fffff) << 10;
1042 msr.hi = (tom_k & 0xffc00000) >> 22;
1043 wrmsr(TOP_MEM, msr);
1046 static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
1049 static const uint32_t csbase_low[] = {
1050 /* 32MB */ (1 << (13 - 4)),
1051 /* 64MB */ (1 << (14 - 4)),
1052 /* 128MB */ (1 << (14 - 4)),
1053 /* 256MB */ (1 << (15 - 4)),
1054 /* 512MB */ (1 << (15 - 4)),
1055 /* 1GB */ (1 << (16 - 4)),
1056 /* 2GB */ (1 << (16 - 4)),
1058 uint32_t csbase_inc;
1059 int chip_selects, index;
1061 unsigned common_size;
1062 uint32_t csbase, csmask;
1064 /* See if all of the memory chip selects are the same size
1065 * and if so count them.
1069 for(index = 0; index < 8; index++) {
1073 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1075 /* Is it enabled? */
1081 if (common_size == 0) {
1084 /* The size differed fail */
1085 if (common_size != size) {
1089 /* Chip selects can only be interleaved when there is
1090 * more than one and their is a power of two of them.
1092 bits = log2(chip_selects);
1093 if (((1 << bits) != chip_selects) || (bits < 1) || (bits > 3)) {
1097 /* Also we run out of address mask bits if we try and interleave 8 4GB dimms */
1098 if ((bits == 3) && (common_size == (1 << (32 - 3)))) {
1099 print_debug("8 4GB chip selects cannot be interleaved\r\n");
1102 /* Find the bits of csbase that we need to interleave on */
1103 if (is_dual_channel(ctrl)) {
1104 csbase_inc = csbase_low[log2(common_size) - 1] << 1;
1106 csbase_inc = csbase_low[log2(common_size)];
1108 /* Compute the initial values for csbase and csbask.
1109 * In csbase just set the enable bit and the base to zero.
1110 * In csmask set the mask bits for the size and page level interleave.
1113 csmask = (((common_size << bits) - 1) << 21);
1114 csmask |= 0xfe00 & ~((csbase_inc << bits) - csbase_inc);
1115 for(index = 0; index < 8; index++) {
1118 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1119 /* Is it enabled? */
1123 pci_write_config32(ctrl->f2, DRAM_CSBASE + (index << 2), csbase);
1124 pci_write_config32(ctrl->f2, DRAM_CSMASK + (index << 2), csmask);
1125 csbase += csbase_inc;
1128 print_spew("Interleaved\r\n");
1130 /* Return the memory size in K */
1131 return common_size << (15 + bits);
1134 static unsigned long order_chip_selects(const struct mem_controller *ctrl)
1138 /* Remember which registers we have used in the high 8 bits of tom */
1141 /* Find the largest remaining canidate */
1142 unsigned index, canidate;
1143 uint32_t csbase, csmask;
1147 for(index = 0; index < 8; index++) {
1149 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1151 /* Is it enabled? */
1156 /* Is it greater? */
1157 if (value <= csbase) {
1161 /* Has it already been selected */
1162 if (tom & (1 << (index + 24))) {
1165 /* I have a new canidate */
1169 /* See if I have found a new canidate */
1174 /* Remember the dimm size */
1175 size = csbase >> 21;
1177 /* Remember I have used this register */
1178 tom |= (1 << (canidate + 24));
1180 /* Recompute the cs base register value */
1181 csbase = (tom << 21) | 1;
1183 /* Increment the top of memory */
1186 /* Compute the memory mask */
1187 csmask = ((size -1) << 21);
1188 csmask |= 0xfe00; /* For now don't optimize */
1190 /* Write the new base register */
1191 pci_write_config32(ctrl->f2, DRAM_CSBASE + (canidate << 2), csbase);
1192 /* Write the new mask register */
1193 pci_write_config32(ctrl->f2, DRAM_CSMASK + (canidate << 2), csmask);
1196 /* Return the memory size in K */
1197 return (tom & ~0xff000000) << 15;
1200 unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
1204 /* Find the last memory address used */
1206 for(node_id = 0; node_id < max_node_id; node_id++) {
1207 uint32_t limit, base;
1209 index = node_id << 3;
1210 base = pci_read_config32(ctrl->f1, 0x40 + index);
1211 /* Only look at the limit if the base is enabled */
1212 if ((base & 3) == 3) {
1213 limit = pci_read_config32(ctrl->f1, 0x44 + index);
1214 end_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
1220 static void order_dimms(const struct mem_controller *ctrl)
1222 unsigned long tom_k, base_k;
1224 if (read_option(CMOS_VSTART_interleave_chip_selects, CMOS_VLEN_interleave_chip_selects, 1) != 0) {
1225 tom_k = interleave_chip_selects(ctrl);
1227 print_debug("Interleaving disabled\r\n");
1231 tom_k = order_chip_selects(ctrl);
1233 /* Compute the memory base address */
1234 base_k = memory_end_k(ctrl, ctrl->node_id);
1236 route_dram_accesses(ctrl, base_k, tom_k);
1240 static long disable_dimm(const struct mem_controller *ctrl, unsigned index, long dimm_mask)
1242 print_debug("disabling dimm");
1243 print_debug_hex8(index);
1244 print_debug("\r\n");
1245 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
1246 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
1247 dimm_mask &= ~(1 << index);
1251 static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long dimm_mask)
1259 for(i = 0; (i < DIMM_SOCKETS); i++) {
1261 if (!(dimm_mask & (1 << i))) {
1264 value = spd_read_byte(ctrl->channel0[i], 21);
1268 /* Registered dimm ? */
1269 if (value & (1 << 1)) {
1272 /* Otherwise it must be an unbuffered dimm */
1277 if (unbuffered && registered) {
1278 die("Mixed buffered and registered dimms not supported");
1280 if (unbuffered && is_opteron(ctrl)) {
1281 die("Unbuffered Dimms not supported on Opteron");
1284 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1285 dcl &= ~DCL_UnBufDimm;
1287 dcl |= DCL_UnBufDimm;
1289 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1291 if (is_registered(ctrl)) {
1292 print_debug("Registered\r\n");
1294 print_debug("Unbuffered\r\n");
1300 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
1305 for(i = 0; i < DIMM_SOCKETS; i++) {
1308 device = ctrl->channel0[i];
1310 byte = spd_read_byte(ctrl->channel0[i], 2); /* Type */
1312 dimm_mask |= (1 << i);
1315 device = ctrl->channel1[i];
1317 byte = spd_read_byte(ctrl->channel1[i], 2);
1319 dimm_mask |= (1 << (i + DIMM_SOCKETS));
1326 static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_mask)
1330 /* SPD addresses to verify are identical */
1331 static const unsigned addresses[] = {
1332 2, /* Type should be DDR SDRAM */
1333 3, /* *Row addresses */
1334 4, /* *Column addresses */
1335 5, /* *Physical Banks */
1336 6, /* *Module Data Width low */
1337 7, /* *Module Data Width high */
1338 9, /* *Cycle time at highest CAS Latency CL=X */
1339 11, /* *SDRAM Type */
1340 13, /* *SDRAM Width */
1341 17, /* *Logical Banks */
1342 18, /* *Supported CAS Latencies */
1343 21, /* *SDRAM Module Attributes */
1344 23, /* *Cycle time at CAS Latnecy (CLX - 0.5) */
1345 26, /* *Cycle time at CAS Latnecy (CLX - 1.0) */
1346 27, /* *tRP Row precharge time */
1347 28, /* *Minimum Row Active to Row Active Delay (tRRD) */
1348 29, /* *tRCD RAS to CAS */
1349 30, /* *tRAS Activate to Precharge */
1350 41, /* *Minimum Active to Active/Auto Refresh Time(Trc) */
1351 42, /* *Minimum Auto Refresh Command Time(Trfc) */
1353 /* If the dimms are not in pairs do not do dual channels */
1354 if ((dimm_mask & ((1 << DIMM_SOCKETS) - 1)) !=
1355 ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) {
1356 goto single_channel;
1358 /* If the cpu is not capable of doing dual channels don't do dual channels */
1359 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1360 if (!(nbcap & NBCAP_128Bit)) {
1361 goto single_channel;
1363 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1364 unsigned device0, device1;
1367 /* If I don't have a dimm skip this one */
1368 if (!(dimm_mask & (1 << i))) {
1371 device0 = ctrl->channel0[i];
1372 device1 = ctrl->channel1[i];
1373 for(j = 0; j < sizeof(addresses)/sizeof(addresses[0]); j++) {
1375 addr = addresses[j];
1376 value0 = spd_read_byte(device0, addr);
1380 value1 = spd_read_byte(device1, addr);
1384 if (value0 != value1) {
1385 goto single_channel;
1389 print_spew("Enabling dual channel memory\r\n");
1391 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1392 dcl &= ~DCL_32ByteEn;
1393 dcl |= DCL_128BitEn;
1394 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1397 dimm_mask &= ~((1 << (DIMM_SOCKETS *2)) - (1 << DIMM_SOCKETS));
1403 uint8_t divisor; /* In 1/2 ns increments */
1406 uint32_t dch_memclk;
1407 uint16_t dch_tref4k, dch_tref8k;
1412 static const struct mem_param *get_mem_param(unsigned min_cycle_time)
1414 static const struct mem_param speed[] = {
1416 .name = "100Mhz\r\n",
1418 .divisor = (10 <<1),
1421 .dch_memclk = DCH_MEMCLK_100MHZ << DCH_MEMCLK_SHIFT,
1422 .dch_tref4k = DTH_TREF_100MHZ_4K,
1423 .dch_tref8k = DTH_TREF_100MHZ_8K,
1427 .name = "133Mhz\r\n",
1429 .divisor = (7<<1)+1,
1432 .dch_memclk = DCH_MEMCLK_133MHZ << DCH_MEMCLK_SHIFT,
1433 .dch_tref4k = DTH_TREF_133MHZ_4K,
1434 .dch_tref8k = DTH_TREF_133MHZ_8K,
1438 .name = "166Mhz\r\n",
1443 .dch_memclk = DCH_MEMCLK_166MHZ << DCH_MEMCLK_SHIFT,
1444 .dch_tref4k = DTH_TREF_166MHZ_4K,
1445 .dch_tref8k = DTH_TREF_166MHZ_8K,
1449 .name = "200Mhz\r\n",
1454 .dch_memclk = DCH_MEMCLK_200MHZ << DCH_MEMCLK_SHIFT,
1455 .dch_tref4k = DTH_TREF_200MHZ_4K,
1456 .dch_tref8k = DTH_TREF_200MHZ_8K,
1463 const struct mem_param *param;
1464 for(param = &speed[0]; param->cycle_time ; param++) {
1465 if (min_cycle_time > (param+1)->cycle_time) {
1469 if (!param->cycle_time) {
1470 die("min_cycle_time to low");
1472 print_spew(param->name);
1473 #ifdef DRAM_MIN_CYCLE_TIME
1474 print_debug(param->name);
1479 struct spd_set_memclk_result {
1480 const struct mem_param *param;
1483 static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *ctrl, long dimm_mask)
1485 /* Compute the minimum cycle time for these dimms */
1486 struct spd_set_memclk_result result;
1487 unsigned min_cycle_time, min_latency, bios_cycle_time;
1491 static const int latency_indicies[] = { 26, 23, 9 };
1492 static const unsigned char min_cycle_times[] = {
1493 [NBCAP_MEMCLK_200MHZ] = 0x50, /* 5ns */
1494 [NBCAP_MEMCLK_166MHZ] = 0x60, /* 6ns */
1495 [NBCAP_MEMCLK_133MHZ] = 0x75, /* 7.5ns */
1496 [NBCAP_MEMCLK_100MHZ] = 0xa0, /* 10ns */
1500 value = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1501 min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
1502 bios_cycle_time = min_cycle_times[
1503 read_option(CMOS_VSTART_max_mem_clock, CMOS_VLEN_max_mem_clock, 0)];
1504 if (bios_cycle_time > min_cycle_time) {
1505 min_cycle_time = bios_cycle_time;
1509 /* Compute the least latency with the fastest clock supported
1510 * by both the memory controller and the dimms.
1512 for(i = 0; i < DIMM_SOCKETS; i++) {
1513 int new_cycle_time, new_latency;
1518 if (!(dimm_mask & (1 << i))) {
1522 /* First find the supported CAS latencies
1523 * Byte 18 for DDR SDRAM is interpreted:
1524 * bit 0 == CAS Latency = 1.0
1525 * bit 1 == CAS Latency = 1.5
1526 * bit 2 == CAS Latency = 2.0
1527 * bit 3 == CAS Latency = 2.5
1528 * bit 4 == CAS Latency = 3.0
1529 * bit 5 == CAS Latency = 3.5
1533 new_cycle_time = 0xa0;
1536 latencies = spd_read_byte(ctrl->channel0[i], 18);
1537 if (latencies <= 0) continue;
1539 /* Compute the lowest cas latency supported */
1540 latency = log2(latencies) -2;
1542 /* Loop through and find a fast clock with a low latency */
1543 for(index = 0; index < 3; index++, latency++) {
1545 if ((latency < 2) || (latency > 4) ||
1546 (!(latencies & (1 << latency)))) {
1549 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1554 /* Only increase the latency if we decreas the clock */
1555 if ((value >= min_cycle_time) && (value < new_cycle_time)) {
1556 new_cycle_time = value;
1557 new_latency = latency;
1560 if (new_latency > 4){
1563 /* Does min_latency need to be increased? */
1564 if (new_cycle_time > min_cycle_time) {
1565 min_cycle_time = new_cycle_time;
1567 /* Does min_cycle_time need to be increased? */
1568 if (new_latency > min_latency) {
1569 min_latency = new_latency;
1572 /* Make a second pass through the dimms and disable
1573 * any that cannot support the selected memclk and cas latency.
1576 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1581 if (!(dimm_mask & (1 << i))) {
1584 latencies = spd_read_byte(ctrl->channel0[i], 18);
1585 if (latencies < 0) goto hw_error;
1586 if (latencies == 0) {
1590 /* Compute the lowest cas latency supported */
1591 latency = log2(latencies) -2;
1593 /* Walk through searching for the selected latency */
1594 for(index = 0; index < 3; index++, latency++) {
1595 if (!(latencies & (1 << latency))) {
1598 if (latency == min_latency)
1601 /* If I can't find the latency or my index is bad error */
1602 if ((latency != min_latency) || (index >= 3)) {
1606 /* Read the min_cycle_time for this latency */
1607 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1608 if (value < 0) goto hw_error;
1610 /* All is good if the selected clock speed
1611 * is what I need or slower.
1613 if (value <= min_cycle_time) {
1616 /* Otherwise I have an error, disable the dimm */
1618 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
1620 /* Now that I know the minimum cycle time lookup the memory parameters */
1621 result.param = get_mem_param(min_cycle_time);
1623 /* Update DRAM Config High with our selected memory speed */
1624 value = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1625 value &= ~(DCH_MEMCLK_MASK << DCH_MEMCLK_SHIFT);
1626 value |= result.param->dch_memclk;
1627 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
1629 static const unsigned latencies[] = { DTL_CL_2, DTL_CL_2_5, DTL_CL_3 };
1630 /* Update DRAM Timing Low with our selected cas latency */
1631 value = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1632 value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
1633 value |= latencies[min_latency - 2] << DTL_TCL_SHIFT;
1634 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
1636 result.dimm_mask = dimm_mask;
1639 result.param = (const struct mem_param *)0;
1640 result.dimm_mask = -1;
1645 static int update_dimm_Trc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1647 unsigned clocks, old_clocks;
1650 value = spd_read_byte(ctrl->channel0[i], 41);
1651 if (value < 0) return -1;
1652 if ((value == 0) || (value == 0xff)) {
1655 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1656 if (clocks < DTL_TRC_MIN) {
1657 clocks = DTL_TRC_MIN;
1659 if (clocks > DTL_TRC_MAX) {
1663 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1664 old_clocks = ((dtl >> DTL_TRC_SHIFT) & DTL_TRC_MASK) + DTL_TRC_BASE;
1665 if (old_clocks > clocks) {
1666 clocks = old_clocks;
1668 dtl &= ~(DTL_TRC_MASK << DTL_TRC_SHIFT);
1669 dtl |= ((clocks - DTL_TRC_BASE) << DTL_TRC_SHIFT);
1670 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1674 static int update_dimm_Trfc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1676 unsigned clocks, old_clocks;
1679 value = spd_read_byte(ctrl->channel0[i], 42);
1680 if (value < 0) return -1;
1681 if ((value == 0) || (value == 0xff)) {
1682 value = param->tRFC;
1684 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1685 if (clocks < DTL_TRFC_MIN) {
1686 clocks = DTL_TRFC_MIN;
1688 if (clocks > DTL_TRFC_MAX) {
1691 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1692 old_clocks = ((dtl >> DTL_TRFC_SHIFT) & DTL_TRFC_MASK) + DTL_TRFC_BASE;
1693 if (old_clocks > clocks) {
1694 clocks = old_clocks;
1696 dtl &= ~(DTL_TRFC_MASK << DTL_TRFC_SHIFT);
1697 dtl |= ((clocks - DTL_TRFC_BASE) << DTL_TRFC_SHIFT);
1698 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1703 static int update_dimm_Trcd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1705 unsigned clocks, old_clocks;
1708 value = spd_read_byte(ctrl->channel0[i], 29);
1709 if (value < 0) return -1;
1710 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1711 if (clocks < DTL_TRCD_MIN) {
1712 clocks = DTL_TRCD_MIN;
1714 if (clocks > DTL_TRCD_MAX) {
1717 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1718 old_clocks = ((dtl >> DTL_TRCD_SHIFT) & DTL_TRCD_MASK) + DTL_TRCD_BASE;
1719 if (old_clocks > clocks) {
1720 clocks = old_clocks;
1722 dtl &= ~(DTL_TRCD_MASK << DTL_TRCD_SHIFT);
1723 dtl |= ((clocks - DTL_TRCD_BASE) << DTL_TRCD_SHIFT);
1724 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1728 static int update_dimm_Trrd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1730 unsigned clocks, old_clocks;
1733 value = spd_read_byte(ctrl->channel0[i], 28);
1734 if (value < 0) return -1;
1735 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1736 if (clocks < DTL_TRRD_MIN) {
1737 clocks = DTL_TRRD_MIN;
1739 if (clocks > DTL_TRRD_MAX) {
1742 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1743 old_clocks = ((dtl >> DTL_TRRD_SHIFT) & DTL_TRRD_MASK) + DTL_TRRD_BASE;
1744 if (old_clocks > clocks) {
1745 clocks = old_clocks;
1747 dtl &= ~(DTL_TRRD_MASK << DTL_TRRD_SHIFT);
1748 dtl |= ((clocks - DTL_TRRD_BASE) << DTL_TRRD_SHIFT);
1749 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1753 static int update_dimm_Tras(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1755 unsigned clocks, old_clocks;
1758 value = spd_read_byte(ctrl->channel0[i], 30);
1759 if (value < 0) return -1;
1760 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1761 if (clocks < DTL_TRAS_MIN) {
1762 clocks = DTL_TRAS_MIN;
1764 if (clocks > DTL_TRAS_MAX) {
1767 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1768 old_clocks = ((dtl >> DTL_TRAS_SHIFT) & DTL_TRAS_MASK) + DTL_TRAS_BASE;
1769 if (old_clocks > clocks) {
1770 clocks = old_clocks;
1772 dtl &= ~(DTL_TRAS_MASK << DTL_TRAS_SHIFT);
1773 dtl |= ((clocks - DTL_TRAS_BASE) << DTL_TRAS_SHIFT);
1774 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1778 static int update_dimm_Trp(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1780 unsigned clocks, old_clocks;
1783 value = spd_read_byte(ctrl->channel0[i], 27);
1784 if (value < 0) return -1;
1785 clocks = (value + (param->divisor << 1) - 1)/(param->divisor << 1);
1786 if (clocks < DTL_TRP_MIN) {
1787 clocks = DTL_TRP_MIN;
1789 if (clocks > DTL_TRP_MAX) {
1792 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1793 old_clocks = ((dtl >> DTL_TRP_SHIFT) & DTL_TRP_MASK) + DTL_TRP_BASE;
1794 if (old_clocks > clocks) {
1795 clocks = old_clocks;
1797 dtl &= ~(DTL_TRP_MASK << DTL_TRP_SHIFT);
1798 dtl |= ((clocks - DTL_TRP_BASE) << DTL_TRP_SHIFT);
1799 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1803 static void set_Twr(const struct mem_controller *ctrl, const struct mem_param *param)
1806 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1807 dtl &= ~(DTL_TWR_MASK << DTL_TWR_SHIFT);
1808 dtl |= (param->dtl_twr - DTL_TWR_BASE) << DTL_TWR_SHIFT;
1809 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1813 static void init_Tref(const struct mem_controller *ctrl, const struct mem_param *param)
1816 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1817 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1818 dth |= (param->dch_tref4k << DTH_TREF_SHIFT);
1819 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1822 static int update_dimm_Tref(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1826 unsigned tref, old_tref;
1827 value = spd_read_byte(ctrl->channel0[i], 3);
1828 if (value < 0) return -1;
1831 tref = param->dch_tref8k;
1833 tref = param->dch_tref4k;
1836 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1837 old_tref = (dth >> DTH_TREF_SHIFT) & DTH_TREF_MASK;
1838 if ((value == 12) && (old_tref == param->dch_tref4k)) {
1839 tref = param->dch_tref4k;
1841 tref = param->dch_tref8k;
1843 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1844 dth |= (tref << DTH_TREF_SHIFT);
1845 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1850 static int update_dimm_x4(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1855 value = spd_read_byte(ctrl->channel0[i], 13);
1860 dimm += DCL_x4DIMM_SHIFT;
1861 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1862 dcl &= ~(1 << dimm);
1866 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1870 static int update_dimm_ecc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1874 value = spd_read_byte(ctrl->channel0[i], 11);
1879 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1880 dcl &= ~DCL_DimmEccEn;
1881 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1886 static int count_dimms(const struct mem_controller *ctrl)
1891 for(index = 0; index < 8; index += 2) {
1893 csbase = pci_read_config32(ctrl->f2, (DRAM_CSBASE + (index << 2)));
1901 static void set_Twtr(const struct mem_controller *ctrl, const struct mem_param *param)
1905 clocks = 1; /* AMD says hard code this */
1906 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1907 dth &= ~(DTH_TWTR_MASK << DTH_TWTR_SHIFT);
1908 dth |= ((clocks - DTH_TWTR_BASE) << DTH_TWTR_SHIFT);
1909 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1912 static void set_Trwt(const struct mem_controller *ctrl, const struct mem_param *param)
1920 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1921 latency = (dtl >> DTL_TCL_SHIFT) & DTL_TCL_MASK;
1922 divisor = param->divisor;
1924 if (is_opteron(ctrl)) {
1925 if (latency == DTL_CL_2) {
1926 if (divisor == ((6 << 0) + 0)) {
1930 else if (divisor > ((6 << 0)+0)) {
1931 /* 100Mhz && 133Mhz */
1935 else if (latency == DTL_CL_2_5) {
1938 else if (latency == DTL_CL_3) {
1939 if (divisor == ((6 << 0)+0)) {
1943 else if (divisor > ((6 << 0)+0)) {
1944 /* 100Mhz && 133Mhz */
1949 else /* Athlon64 */ {
1950 if (is_registered(ctrl)) {
1951 if (latency == DTL_CL_2) {
1954 else if (latency == DTL_CL_2_5) {
1957 else if (latency == DTL_CL_3) {
1961 else /* Unbuffered */{
1962 if (latency == DTL_CL_2) {
1965 else if (latency == DTL_CL_2_5) {
1968 else if (latency == DTL_CL_3) {
1973 if ((clocks < DTH_TRWT_MIN) || (clocks > DTH_TRWT_MAX)) {
1974 die("Unknown Trwt\r\n");
1977 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1978 dth &= ~(DTH_TRWT_MASK << DTH_TRWT_SHIFT);
1979 dth |= ((clocks - DTH_TRWT_BASE) << DTH_TRWT_SHIFT);
1980 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1984 static void set_Twcl(const struct mem_controller *ctrl, const struct mem_param *param)
1986 /* Memory Clocks after CAS# */
1989 if (is_registered(ctrl)) {
1994 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1995 dth &= ~(DTH_TWCL_MASK << DTH_TWCL_SHIFT);
1996 dth |= ((clocks - DTH_TWCL_BASE) << DTH_TWCL_SHIFT);
1997 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
2001 static void set_read_preamble(const struct mem_controller *ctrl, const struct mem_param *param)
2005 unsigned rdpreamble;
2006 divisor = param->divisor;
2007 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2008 dch &= ~(DCH_RDPREAMBLE_MASK << DCH_RDPREAMBLE_SHIFT);
2010 if (is_registered(ctrl)) {
2011 if (divisor == ((10 << 1)+0)) {
2013 rdpreamble = ((9 << 1)+ 0);
2015 else if (divisor == ((7 << 1)+1)) {
2017 rdpreamble = ((8 << 1)+0);
2019 else if (divisor == ((6 << 1)+0)) {
2021 rdpreamble = ((7 << 1)+1);
2023 else if (divisor == ((5 << 1)+0)) {
2025 rdpreamble = ((7 << 1)+0);
2032 for(i = 0; i < 4; i++) {
2033 if (ctrl->channel0[i]) {
2037 if (divisor == ((10 << 1)+0)) {
2041 rdpreamble = ((9 << 1)+0);
2044 rdpreamble = ((14 << 1)+0);
2047 else if (divisor == ((7 << 1)+1)) {
2051 rdpreamble = ((7 << 1)+0);
2054 rdpreamble = ((11 << 1)+0);
2057 else if (divisor == ((6 << 1)+0)) {
2061 rdpreamble = ((7 << 1)+0);
2064 rdpreamble = ((9 << 1)+0);
2067 else if (divisor == ((5 << 1)+0)) {
2071 rdpreamble = ((5 << 1)+0);
2074 rdpreamble = ((7 << 1)+0);
2078 if ((rdpreamble < DCH_RDPREAMBLE_MIN) || (rdpreamble > DCH_RDPREAMBLE_MAX)) {
2079 die("Unknown rdpreamble");
2081 dch |= (rdpreamble - DCH_RDPREAMBLE_BASE) << DCH_RDPREAMBLE_SHIFT;
2082 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2085 static void set_max_async_latency(const struct mem_controller *ctrl, const struct mem_param *param)
2091 dimms = count_dimms(ctrl);
2093 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2094 dch &= ~(DCH_ASYNC_LAT_MASK << DCH_ASYNC_LAT_SHIFT);
2096 if (is_registered(ctrl)) {
2108 die("Too many unbuffered dimms");
2110 else if (dimms == 3) {
2119 dch |= ((async_lat - DCH_ASYNC_LAT_BASE) << DCH_ASYNC_LAT_SHIFT);
2120 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2123 static void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct mem_param *param)
2126 /* AMD says to Hardcode this */
2127 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2128 dch &= ~(DCH_IDLE_LIMIT_MASK << DCH_IDLE_LIMIT_SHIFT);
2129 dch |= DCH_IDLE_LIMIT_16 << DCH_IDLE_LIMIT_SHIFT;
2130 dch |= DCH_DYN_IDLE_CTR_EN;
2131 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2134 static long spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param, long dimm_mask)
2138 init_Tref(ctrl, param);
2139 for(i = 0; i < DIMM_SOCKETS; i++) {
2141 if (!(dimm_mask & (1 << i))) {
2144 /* DRAM Timing Low Register */
2145 if ((rc = update_dimm_Trc (ctrl, param, i)) <= 0) goto dimm_err;
2146 if ((rc = update_dimm_Trfc(ctrl, param, i)) <= 0) goto dimm_err;
2147 if ((rc = update_dimm_Trcd(ctrl, param, i)) <= 0) goto dimm_err;
2148 if ((rc = update_dimm_Trrd(ctrl, param, i)) <= 0) goto dimm_err;
2149 if ((rc = update_dimm_Tras(ctrl, param, i)) <= 0) goto dimm_err;
2150 if ((rc = update_dimm_Trp (ctrl, param, i)) <= 0) goto dimm_err;
2152 /* DRAM Timing High Register */
2153 if ((rc = update_dimm_Tref(ctrl, param, i)) <= 0) goto dimm_err;
2156 /* DRAM Config Low */
2157 if ((rc = update_dimm_x4 (ctrl, param, i)) <= 0) goto dimm_err;
2158 if ((rc = update_dimm_ecc(ctrl, param, i)) <= 0) goto dimm_err;
2164 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
2166 /* DRAM Timing Low Register */
2167 set_Twr(ctrl, param);
2169 /* DRAM Timing High Register */
2170 set_Twtr(ctrl, param);
2171 set_Trwt(ctrl, param);
2172 set_Twcl(ctrl, param);
2174 /* DRAM Config High */
2175 set_read_preamble(ctrl, param);
2176 set_max_async_latency(ctrl, param);
2177 set_idle_cycle_limit(ctrl, param);
2181 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
2183 struct spd_set_memclk_result result;
2184 const struct mem_param *param;
2186 hw_enable_ecc(ctrl);
2187 activate_spd_rom(ctrl);
2188 dimm_mask = spd_detect_dimms(ctrl);
2189 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
2190 print_debug("No memory for this cpu\r\n");
2193 dimm_mask = spd_enable_2channels(ctrl, dimm_mask);
2196 dimm_mask = spd_set_ram_size(ctrl , dimm_mask);
2199 dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask);
2202 result = spd_set_memclk(ctrl, dimm_mask);
2203 param = result.param;
2204 dimm_mask = result.dimm_mask;
2207 dimm_mask = spd_set_dram_timing(ctrl, param , dimm_mask);
2213 /* Unrecoverable error reading SPD data */
2214 print_err("SPD error - reset\r\n");
2219 #define TIMEOUT_LOOPS 300000
2220 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
2224 /* Error if I don't have memory */
2225 if (memory_end_k(ctrl, controllers) == 0) {
2226 die("No memory\r\n");
2229 /* Before enabling memory start the memory clocks */
2230 for(i = 0; i < controllers; i++) {
2232 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2233 if (dch & (DCH_MEMCLK_EN0|DCH_MEMCLK_EN1|DCH_MEMCLK_EN2|DCH_MEMCLK_EN3)) {
2234 dch |= DCH_MEMCLK_VALID;
2235 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2238 /* Disable dram receivers */
2240 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2241 dcl |= DCL_DisInRcvrs;
2242 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2246 /* And if necessary toggle the the reset on the dimms by hand */
2247 memreset(controllers, ctrl);
2249 for(i = 0; i < controllers; i++) {
2251 /* Skip everything if I don't have any memory on this controller */
2252 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2253 if (!(dch & DCH_MEMCLK_VALID)) {
2257 /* Toggle DisDqsHys to get it working */
2258 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2259 if (dcl & DCL_DimmEccEn) {
2261 print_spew("ECC enabled\r\n");
2262 mnc = pci_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
2264 if (dcl & DCL_128BitEn) {
2265 mnc |= MNC_CHIPKILL_EN;
2267 pci_write_config32(ctrl[i].f3, MCA_NB_CONFIG, mnc);
2269 dcl |= DCL_DisDqsHys;
2270 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2271 dcl &= ~DCL_DisDqsHys;
2272 dcl &= ~DCL_DLL_Disable;
2275 dcl |= DCL_DramInit;
2276 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2279 for(i = 0; i < controllers; i++) {
2281 /* Skip everything if I don't have any memory on this controller */
2282 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2283 if (!(dch & DCH_MEMCLK_VALID)) {
2287 print_debug("Initializing memory: ");
2290 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2292 if ((loops & 1023) == 0) {
2295 } while(((dcl & DCL_DramInit) != 0) && (loops < TIMEOUT_LOOPS));
2296 if (loops >= TIMEOUT_LOOPS) {
2297 print_debug(" failed\r\n");
2300 if (!is_cpu_pre_c0()) {
2301 /* Wait until it is safe to touch memory */
2302 dcl &= ~(DCL_MemClrStatus | DCL_DramEnable);
2303 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2305 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2306 } while(((dcl & DCL_MemClrStatus) == 0) || ((dcl & DCL_DramEnable) == 0) );
2308 print_debug(" done\r\n");
2311 /* Make certain the first 1M of memory is intialized */
2315 /* Save the value of msr_201 */
2316 msr_201 = rdmsr(0x201);
2318 print_debug("Clearing LinuxBIOS memory: ");
2322 "movl %%cr0, %0\n\t"
2323 "orl $0x40000000, %0\n\t"
2324 "movl %0, %%cr0\n\t"
2328 /* Disable fixed mtrrs */
2329 msr = rdmsr(MTRRdefType_MSR);
2331 wrmsr(MTRRdefType_MSR, msr);
2334 /* Set the variable mtrrs to write combine */
2336 msr.lo = 0 | MTRR_TYPE_WRCOMB;
2339 /* Set the limit to 1M of ram */
2340 msr.hi = 0x000000ff;
2341 msr.lo = (~((CONFIG_LB_MEM_TOPK << 10) - 1)) | 0x800;
2346 "movl %%cr0, %0\n\t"
2347 "andl $0x9fffffff, %0\n\t"
2348 "movl %0, %%cr0\n\t"
2352 /* clear memory 1meg */
2355 "movl %0, %%fs:(%1)\n\t"
2360 : "a" (0), "D" (0), "c" (1024*1024)
2365 "movl %%cr0, %0\n\t"
2366 "orl $0x40000000, %0\n\t"
2367 "movl %0, %%cr0\n\t"
2371 /* restore msr registers */
2372 msr = rdmsr(MTRRdefType_MSR);
2374 wrmsr(MTRRdefType_MSR, msr);
2377 /* Restore the variable mtrrs */
2379 msr.lo = MTRR_TYPE_WRBACK;
2381 wrmsr(0x201, msr_201);
2385 "movl %%cr0, %0\n\t"
2386 "andl $0x9fffffff, %0\n\t"
2387 "movl %0, %%cr0\n\t"
2391 print_debug(" done\r\n");