1 #include <cpu/k8/mtrr.h>
7 #define DRAM_CSBASE 0x40
8 #define DRAM_CSMASK 0x60
9 #define DRAM_BANK_ADDR_MAP 0x80
10 #define DRAM_TIMING_LOW 0x88
11 #define DTL_TCL_SHIFT 0
12 #define DTL_TCL_MASK 0x7
16 #define DTL_TRC_SHIFT 4
17 #define DTL_TRC_MASK 0xf
18 #define DTL_TRC_BASE 7
20 #define DTL_TRC_MAX 22
21 #define DTL_TRFC_SHIFT 8
22 #define DTL_TRFC_MASK 0xf
23 #define DTL_TRFC_BASE 9
24 #define DTL_TRFC_MIN 9
25 #define DTL_TRFC_MAX 24
26 #define DTL_TRCD_SHIFT 12
27 #define DTL_TRCD_MASK 0x7
28 #define DTL_TRCD_BASE 0
29 #define DTL_TRCD_MIN 2
30 #define DTL_TRCD_MAX 6
31 #define DTL_TRRD_SHIFT 16
32 #define DTL_TRRD_MASK 0x7
33 #define DTL_TRRD_BASE 0
34 #define DTL_TRRD_MIN 2
35 #define DTL_TRRD_MAX 4
36 #define DTL_TRAS_SHIFT 20
37 #define DTL_TRAS_MASK 0xf
38 #define DTL_TRAS_BASE 0
39 #define DTL_TRAS_MIN 5
40 #define DTL_TRAS_MAX 15
41 #define DTL_TRP_SHIFT 24
42 #define DTL_TRP_MASK 0x7
43 #define DTL_TRP_BASE 0
46 #define DTL_TWR_SHIFT 28
47 #define DTL_TWR_MASK 0x1
48 #define DTL_TWR_BASE 2
51 #define DRAM_TIMING_HIGH 0x8c
52 #define DTH_TWTR_SHIFT 0
53 #define DTH_TWTR_MASK 0x1
54 #define DTH_TWTR_BASE 1
55 #define DTH_TWTR_MIN 1
56 #define DTH_TWTR_MAX 2
57 #define DTH_TRWT_SHIFT 4
58 #define DTH_TRWT_MASK 0x7
59 #define DTH_TRWT_BASE 1
60 #define DTH_TRWT_MIN 1
61 #define DTH_TRWT_MAX 6
62 #define DTH_TREF_SHIFT 8
63 #define DTH_TREF_MASK 0x1f
64 #define DTH_TREF_100MHZ_4K 0x00
65 #define DTH_TREF_133MHZ_4K 0x01
66 #define DTH_TREF_166MHZ_4K 0x02
67 #define DTH_TREF_200MHZ_4K 0x03
68 #define DTH_TREF_100MHZ_8K 0x08
69 #define DTH_TREF_133MHZ_8K 0x09
70 #define DTH_TREF_166MHZ_8K 0x0A
71 #define DTH_TREF_200MHZ_8K 0x0B
72 #define DTH_TWCL_SHIFT 20
73 #define DTH_TWCL_MASK 0x7
74 #define DTH_TWCL_BASE 1
75 #define DTH_TWCL_MIN 1
76 #define DTH_TWCL_MAX 2
77 #define DRAM_CONFIG_LOW 0x90
78 #define DCL_DLL_Disable (1<<0)
79 #define DCL_D_DRV (1<<1)
80 #define DCL_QFC_EN (1<<2)
81 #define DCL_DisDqsHys (1<<3)
82 #define DCL_DramInit (1<<8)
83 #define DCL_DramEnable (1<<10)
84 #define DCL_MemClrStatus (1<<11)
85 #define DCL_ESR (1<<12)
86 #define DCL_SRS (1<<13)
87 #define DCL_128BitEn (1<<16)
88 #define DCL_DimmEccEn (1<<17)
89 #define DCL_UnBufDimm (1<<18)
90 #define DCL_32ByteEn (1<<19)
91 #define DCL_x4DIMM_SHIFT 20
92 #define DRAM_CONFIG_HIGH 0x94
93 #define DCH_ASYNC_LAT_SHIFT 0
94 #define DCH_ASYNC_LAT_MASK 0xf
95 #define DCH_ASYNC_LAT_BASE 0
96 #define DCH_ASYNC_LAT_MIN 0
97 #define DCH_ASYNC_LAT_MAX 15
98 #define DCH_RDPREAMBLE_SHIFT 8
99 #define DCH_RDPREAMBLE_MASK 0xf
100 #define DCH_RDPREAMBLE_BASE ((2<<1)+0) /* 2.0 ns */
101 #define DCH_RDPREAMBLE_MIN ((2<<1)+0) /* 2.0 ns */
102 #define DCH_RDPREAMBLE_MAX ((9<<1)+1) /* 9.5 ns */
103 #define DCH_IDLE_LIMIT_SHIFT 16
104 #define DCH_IDLE_LIMIT_MASK 0x7
105 #define DCH_IDLE_LIMIT_0 0
106 #define DCH_IDLE_LIMIT_4 1
107 #define DCH_IDLE_LIMIT_8 2
108 #define DCH_IDLE_LIMIT_16 3
109 #define DCH_IDLE_LIMIT_32 4
110 #define DCH_IDLE_LIMIT_64 5
111 #define DCH_IDLE_LIMIT_128 6
112 #define DCH_IDLE_LIMIT_256 7
113 #define DCH_DYN_IDLE_CTR_EN (1 << 19)
114 #define DCH_MEMCLK_SHIFT 20
115 #define DCH_MEMCLK_MASK 0x7
116 #define DCH_MEMCLK_100MHZ 0
117 #define DCH_MEMCLK_133MHZ 2
118 #define DCH_MEMCLK_166MHZ 5
119 #define DCH_MEMCLK_200MHZ 7
120 #define DCH_MEMCLK_VALID (1 << 25)
121 #define DCH_MEMCLK_EN0 (1 << 26)
122 #define DCH_MEMCLK_EN1 (1 << 27)
123 #define DCH_MEMCLK_EN2 (1 << 28)
124 #define DCH_MEMCLK_EN3 (1 << 29)
127 #define MCA_NB_CONFIG 0x44
128 #define MNC_ECC_EN (1 << 22)
129 #define MNC_CHIPKILL_EN (1 << 23)
130 #define SCRUB_CONTROL 0x58
134 #define SCRUB_160ns 3
135 #define SCRUB_320ns 4
136 #define SCRUB_640ns 5
137 #define SCRUB_1_28us 6
138 #define SCRUB_2_56us 7
139 #define SCRUB_5_12us 8
140 #define SCRUB_10_2us 9
141 #define SCRUB_20_5us 10
142 #define SCRUB_41_0us 11
143 #define SCRUB_81_9us 12
144 #define SCRUB_163_8us 13
145 #define SCRUB_327_7us 14
146 #define SCRUB_655_4us 15
147 #define SCRUB_1_31ms 16
148 #define SCRUB_2_62ms 17
149 #define SCRUB_5_24ms 18
150 #define SCRUB_10_49ms 19
151 #define SCRUB_20_97ms 20
152 #define SCRUB_42ms 21
153 #define SCRUB_84ms 22
154 #define SC_DRAM_SCRUB_RATE_SHFIT 0
155 #define SC_DRAM_SCRUB_RATE_MASK 0x1f
156 #define SC_L2_SCRUB_RATE_SHIFT 8
157 #define SC_L2_SCRUB_RATE_MASK 0x1f
158 #define SC_L1D_SCRUB_RATE_SHIFT 16
159 #define SC_L1D_SCRUB_RATE_MASK 0x1f
160 #define SCRUB_ADDR_LOW 0x5C
161 #define SCRUB_ADDR_HIGH 0x60
162 #define NORTHBRIDGE_CAP 0xE8
163 #define NBCAP_128Bit 0x0001
164 #define NBCAP_MP 0x0002
165 #define NBCAP_BIG_MP 0x0004
166 #define NBCAP_ECC 0x0004
167 #define NBCAP_CHIPKILL_ECC 0x0010
168 #define NBCAP_MEMCLK_SHIFT 5
169 #define NBCAP_MEMCLK_MASK 3
170 #define NBCAP_MEMCLK_100MHZ 3
171 #define NBCAP_MEMCLK_133MHZ 2
172 #define NBCAP_MEMCLK_166MHZ 1
173 #define NBCAP_MEMCLK_200MHZ 0
174 #define NBCAP_MEMCTRL 0x0100
177 static void setup_resource_map(const unsigned int *register_values, int max)
180 print_debug("setting up resource map....\r\n");
181 for(i = 0; i < max; i += 3) {
186 print_debug_hex32(register_values[i]);
188 print_debug_hex32(register_values[i+2]);
191 dev = register_values[i] & ~0xff;
192 where = register_values[i] & 0xff;
193 reg = pci_read_config32(dev, where);
194 reg &= register_values[i+1];
195 reg |= register_values[i+2];
196 pci_write_config32(dev, where, reg);
198 reg = pci_read_config32(register_values[i]);
199 reg &= register_values[i+1];
200 reg |= register_values[i+2] & ~register_values[i+1];
201 pci_write_config32(register_values[i], reg);
204 print_debug("done.\r\n");
207 static void setup_default_resource_map(void)
209 static const unsigned int register_values[] = {
210 /* Careful set limit registers before base registers which contain the enables */
211 /* DRAM Limit i Registers
220 * [ 2: 0] Destination Node ID
230 * [10: 8] Interleave select
231 * specifies the values of A[14:12] to use with interleave enable.
233 * [31:16] DRAM Limit Address i Bits 39-24
234 * This field defines the upper address bits of a 40 bit address
235 * that define the end of the DRAM region.
237 PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
238 PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
239 PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
240 PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
241 PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
242 PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
243 PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
244 PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
245 /* DRAM Base i Registers
254 * [ 0: 0] Read Enable
257 * [ 1: 1] Write Enable
258 * 0 = Writes Disabled
261 * [10: 8] Interleave Enable
262 * 000 = No interleave
263 * 001 = Interleave on A[12] (2 nodes)
265 * 011 = Interleave on A[12] and A[14] (4 nodes)
269 * 111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
271 * [13:16] DRAM Base Address i Bits 39-24
272 * This field defines the upper address bits of a 40-bit address
273 * that define the start of the DRAM region.
275 PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
276 PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
277 PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
278 PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
279 PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
280 PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
281 PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
282 PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
284 /* Memory-Mapped I/O Limit i Registers
293 * [ 2: 0] Destination Node ID
303 * [ 5: 4] Destination Link ID
310 * 0 = CPU writes may be posted
311 * 1 = CPU writes must be non-posted
312 * [31: 8] Memory-Mapped I/O Limit Address i (39-16)
313 * This field defines the upp adddress bits of a 40-bit address that
314 * defines the end of a memory-mapped I/O region n
316 PCI_ADDR(0, 0x18, 1, 0x84), 0x00000048, 0x00000000,
317 PCI_ADDR(0, 0x18, 1, 0x8C), 0x00000048, 0x00000000,
318 PCI_ADDR(0, 0x18, 1, 0x94), 0x00000048, 0x00000000,
319 PCI_ADDR(0, 0x18, 1, 0x9C), 0x00000048, 0x00000000,
320 PCI_ADDR(0, 0x18, 1, 0xA4), 0x00000048, 0x00000000,
321 PCI_ADDR(0, 0x18, 1, 0xAC), 0x00000048, 0x00000000,
322 PCI_ADDR(0, 0x18, 1, 0xB4), 0x00000048, 0x00000000,
323 PCI_ADDR(0, 0x18, 1, 0xBC), 0x00000048, 0x00ffff00,
325 /* Memory-Mapped I/O Base i Registers
334 * [ 0: 0] Read Enable
337 * [ 1: 1] Write Enable
338 * 0 = Writes disabled
340 * [ 2: 2] Cpu Disable
341 * 0 = Cpu can use this I/O range
342 * 1 = Cpu requests do not use this I/O range
344 * 0 = base/limit registers i are read/write
345 * 1 = base/limit registers i are read-only
347 * [31: 8] Memory-Mapped I/O Base Address i (39-16)
348 * This field defines the upper address bits of a 40bit address
349 * that defines the start of memory-mapped I/O region i
351 PCI_ADDR(0, 0x18, 1, 0x80), 0x000000f0, 0x00000000,
352 PCI_ADDR(0, 0x18, 1, 0x88), 0x000000f0, 0x00000000,
353 PCI_ADDR(0, 0x18, 1, 0x90), 0x000000f0, 0x00000000,
354 PCI_ADDR(0, 0x18, 1, 0x98), 0x000000f0, 0x00000000,
355 PCI_ADDR(0, 0x18, 1, 0xA0), 0x000000f0, 0x00000000,
356 PCI_ADDR(0, 0x18, 1, 0xA8), 0x000000f0, 0x00000000,
357 PCI_ADDR(0, 0x18, 1, 0xB0), 0x000000f0, 0x00000000,
358 PCI_ADDR(0, 0x18, 1, 0xB8), 0x000000f0, 0x00fc0003,
360 /* PCI I/O Limit i Registers
365 * [ 2: 0] Destination Node ID
375 * [ 5: 4] Destination Link ID
381 * [24:12] PCI I/O Limit Address i
382 * This field defines the end of PCI I/O region n
385 PCI_ADDR(0, 0x18, 1, 0xC4), 0xFE000FC8, 0x01fff000,
386 PCI_ADDR(0, 0x18, 1, 0xCC), 0xFE000FC8, 0x00000000,
387 PCI_ADDR(0, 0x18, 1, 0xD4), 0xFE000FC8, 0x00000000,
388 PCI_ADDR(0, 0x18, 1, 0xDC), 0xFE000FC8, 0x00000000,
390 /* PCI I/O Base i Registers
395 * [ 0: 0] Read Enable
398 * [ 1: 1] Write Enable
399 * 0 = Writes Disabled
403 * 0 = VGA matches Disabled
404 * 1 = matches all address < 64K and where A[9:0] is in the
405 * range 3B0-3BB or 3C0-3DF independen of the base & limit registers
407 * 0 = ISA matches Disabled
408 * 1 = Blocks address < 64K and in the last 768 bytes of eack 1K block
409 * from matching agains this base/limit pair
411 * [24:12] PCI I/O Base i
412 * This field defines the start of PCI I/O region n
415 PCI_ADDR(0, 0x18, 1, 0xC0), 0xFE000FCC, 0x00000003,
416 PCI_ADDR(0, 0x18, 1, 0xC8), 0xFE000FCC, 0x00000000,
417 PCI_ADDR(0, 0x18, 1, 0xD0), 0xFE000FCC, 0x00000000,
418 PCI_ADDR(0, 0x18, 1, 0xD8), 0xFE000FCC, 0x00000000,
420 /* Config Base and Limit i Registers
425 * [ 0: 0] Read Enable
428 * [ 1: 1] Write Enable
429 * 0 = Writes Disabled
431 * [ 2: 2] Device Number Compare Enable
432 * 0 = The ranges are based on bus number
433 * 1 = The ranges are ranges of devices on bus 0
435 * [ 6: 4] Destination Node
445 * [ 9: 8] Destination Link
451 * [23:16] Bus Number Base i
452 * This field defines the lowest bus number in configuration region i
453 * [31:24] Bus Number Limit i
454 * This field defines the highest bus number in configuration regin i
456 PCI_ADDR(0, 0x18, 1, 0xE0), 0x0000FC88, 0xff000003,
457 PCI_ADDR(0, 0x18, 1, 0xE4), 0x0000FC88, 0x00000000,
458 PCI_ADDR(0, 0x18, 1, 0xE8), 0x0000FC88, 0x00000000,
459 PCI_ADDR(0, 0x18, 1, 0xEC), 0x0000FC88, 0x00000000,
462 max = sizeof(register_values)/sizeof(register_values[0]);
463 setup_resource_map(register_values, max);
466 static void sdram_set_registers(const struct mem_controller *ctrl)
468 static const unsigned int register_values[] = {
470 /* Careful set limit registers before base registers which contain the enables */
471 /* DRAM Limit i Registers
480 * [ 2: 0] Destination Node ID
490 * [10: 8] Interleave select
491 * specifies the values of A[14:12] to use with interleave enable.
493 * [31:16] DRAM Limit Address i Bits 39-24
494 * This field defines the upper address bits of a 40 bit address
495 * that define the end of the DRAM region.
497 PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
498 PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
499 PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
500 PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
501 PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
502 PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
503 PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
504 PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
505 /* DRAM Base i Registers
514 * [ 0: 0] Read Enable
517 * [ 1: 1] Write Enable
518 * 0 = Writes Disabled
521 * [10: 8] Interleave Enable
522 * 000 = No interleave
523 * 001 = Interleave on A[12] (2 nodes)
525 * 011 = Interleave on A[12] and A[14] (4 nodes)
529 * 111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
531 * [13:16] DRAM Base Address i Bits 39-24
532 * This field defines the upper address bits of a 40-bit address
533 * that define the start of the DRAM region.
535 PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
536 PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
537 PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
538 PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
539 PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
540 PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
541 PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
542 PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
544 /* DRAM CS Base Address i Registers
553 * [ 0: 0] Chip-Select Bank Enable
557 * [15: 9] Base Address (19-13)
558 * An optimization used when all DIMM are the same size...
560 * [31:21] Base Address (35-25)
561 * This field defines the top 11 addresses bit of a 40-bit
562 * address that define the memory address space. These
563 * bits decode 32-MByte blocks of memory.
565 PCI_ADDR(0, 0x18, 2, 0x40), 0x001f01fe, 0x00000000,
566 PCI_ADDR(0, 0x18, 2, 0x44), 0x001f01fe, 0x00000000,
567 PCI_ADDR(0, 0x18, 2, 0x48), 0x001f01fe, 0x00000000,
568 PCI_ADDR(0, 0x18, 2, 0x4C), 0x001f01fe, 0x00000000,
569 PCI_ADDR(0, 0x18, 2, 0x50), 0x001f01fe, 0x00000000,
570 PCI_ADDR(0, 0x18, 2, 0x54), 0x001f01fe, 0x00000000,
571 PCI_ADDR(0, 0x18, 2, 0x58), 0x001f01fe, 0x00000000,
572 PCI_ADDR(0, 0x18, 2, 0x5C), 0x001f01fe, 0x00000000,
573 /* DRAM CS Mask Address i Registers
582 * Select bits to exclude from comparison with the DRAM Base address register.
584 * [15: 9] Address Mask (19-13)
585 * Address to be excluded from the optimized case
587 * [29:21] Address Mask (33-25)
588 * The bits with an address mask of 1 are excluded from address comparison
592 PCI_ADDR(0, 0x18, 2, 0x60), 0xC01f01ff, 0x00000000,
593 PCI_ADDR(0, 0x18, 2, 0x64), 0xC01f01ff, 0x00000000,
594 PCI_ADDR(0, 0x18, 2, 0x68), 0xC01f01ff, 0x00000000,
595 PCI_ADDR(0, 0x18, 2, 0x6C), 0xC01f01ff, 0x00000000,
596 PCI_ADDR(0, 0x18, 2, 0x70), 0xC01f01ff, 0x00000000,
597 PCI_ADDR(0, 0x18, 2, 0x74), 0xC01f01ff, 0x00000000,
598 PCI_ADDR(0, 0x18, 2, 0x78), 0xC01f01ff, 0x00000000,
599 PCI_ADDR(0, 0x18, 2, 0x7C), 0xC01f01ff, 0x00000000,
600 /* DRAM Bank Address Mapping Register
602 * Specify the memory module size
607 * 000 = 32Mbyte (Rows = 12 & Col = 8)
608 * 001 = 64Mbyte (Rows = 12 & Col = 9)
609 * 010 = 128Mbyte (Rows = 13 & Col = 9)|(Rows = 12 & Col = 10)
610 * 011 = 256Mbyte (Rows = 13 & Col = 10)|(Rows = 12 & Col = 11)
611 * 100 = 512Mbyte (Rows = 13 & Col = 11)|(Rows = 14 & Col = 10)
612 * 101 = 1Gbyte (Rows = 14 & Col = 11)|(Rows = 13 & Col = 12)
613 * 110 = 2Gbyte (Rows = 14 & Col = 12)
620 PCI_ADDR(0, 0x18, 2, 0x80), 0xffff8888, 0x00000000,
621 /* DRAM Timing Low Register
623 * [ 2: 0] Tcl (Cas# Latency, Cas# to read-data-valid)
633 * [ 7: 4] Trc (Row Cycle Time, Ras#-active to Ras#-active/bank auto refresh)
634 * 0000 = 7 bus clocks
635 * 0001 = 8 bus clocks
637 * 1110 = 21 bus clocks
638 * 1111 = 22 bus clocks
639 * [11: 8] Trfc (Row refresh Cycle time, Auto-refresh-active to RAS#-active or RAS#auto-refresh)
640 * 0000 = 9 bus clocks
641 * 0010 = 10 bus clocks
643 * 1110 = 23 bus clocks
644 * 1111 = 24 bus clocks
645 * [14:12] Trcd (Ras#-active to Case#-read/write Delay)
655 * [18:16] Trrd (Ras# to Ras# Delay)
665 * [23:20] Tras (Minmum Ras# Active Time)
666 * 0000 to 0100 = reserved
667 * 0101 = 5 bus clocks
669 * 1111 = 15 bus clocks
670 * [26:24] Trp (Row Precharge Time)
680 * [28:28] Twr (Write Recovery Time)
685 PCI_ADDR(0, 0x18, 2, 0x88), 0xe8088008, 0x02522001 /* 0x03623125 */ ,
686 /* DRAM Timing High Register
688 * [ 0: 0] Twtr (Write to Read Delay)
692 * [ 6: 4] Trwt (Read to Write Delay)
702 * [12: 8] Tref (Refresh Rate)
703 * 00000 = 100Mhz 4K rows
704 * 00001 = 133Mhz 4K rows
705 * 00010 = 166Mhz 4K rows
706 * 00011 = 200Mhz 4K rows
707 * 01000 = 100Mhz 8K/16K rows
708 * 01001 = 133Mhz 8K/16K rows
709 * 01010 = 166Mhz 8K/16K rows
710 * 01011 = 200Mhz 8K/16K rows
712 * [22:20] Twcl (Write CAS Latency)
713 * 000 = 1 Mem clock after CAS# (Unbuffered Dimms)
714 * 001 = 2 Mem clocks after CAS# (Registered Dimms)
717 PCI_ADDR(0, 0x18, 2, 0x8c), 0xff8fe08e, (0 << 20)|(0 << 8)|(0 << 4)|(0 << 0),
718 /* DRAM Config Low Register
720 * [ 0: 0] DLL Disable
729 * [ 3: 3] Disable DQS Hystersis (FIXME handle this one carefully)
730 * 0 = Enable DQS input filter
731 * 1 = Disable DQS input filtering
734 * 0 = Initialization done or not yet started.
735 * 1 = Initiate DRAM intialization sequence
736 * [ 9: 9] SO-Dimm Enable
738 * 1 = SO-Dimms present
740 * 0 = DRAM not enabled
741 * 1 = DRAM initialized and enabled
742 * [11:11] Memory Clear Status
743 * 0 = Memory Clear function has not completed
744 * 1 = Memory Clear function has completed
745 * [12:12] Exit Self-Refresh
746 * 0 = Exit from self-refresh done or not yet started
747 * 1 = DRAM exiting from self refresh
748 * [13:13] Self-Refresh Status
749 * 0 = Normal Operation
750 * 1 = Self-refresh mode active
751 * [15:14] Read/Write Queue Bypass Count
756 * [16:16] 128-bit/64-Bit
757 * 0 = 64bit Interface to DRAM
758 * 1 = 128bit Interface to DRAM
759 * [17:17] DIMM ECC Enable
760 * 0 = Some DIMMs do not have ECC
761 * 1 = ALL DIMMS have ECC bits
762 * [18:18] UnBuffered DIMMs
764 * 1 = Unbuffered DIMMS
765 * [19:19] Enable 32-Byte Granularity
766 * 0 = Optimize for 64byte bursts
767 * 1 = Optimize for 32byte bursts
768 * [20:20] DIMM 0 is x4
769 * [21:21] DIMM 1 is x4
770 * [22:22] DIMM 2 is x4
771 * [23:23] DIMM 3 is x4
773 * 1 = x4 DIMM present
774 * [24:24] Disable DRAM Receivers
775 * 0 = Receivers enabled
776 * 1 = Receivers disabled
778 * 000 = Arbiters chois is always respected
779 * 001 = Oldest entry in DCQ can be bypassed 1 time
780 * 010 = Oldest entry in DCQ can be bypassed 2 times
781 * 011 = Oldest entry in DCQ can be bypassed 3 times
782 * 100 = Oldest entry in DCQ can be bypassed 4 times
783 * 101 = Oldest entry in DCQ can be bypassed 5 times
784 * 110 = Oldest entry in DCQ can be bypassed 6 times
785 * 111 = Oldest entry in DCQ can be bypassed 7 times
788 PCI_ADDR(0, 0x18, 2, 0x90), 0xf0000000,
790 (0 << 23)|(0 << 22)|(0 << 21)|(0 << 20)|
791 (1 << 19)|(0 << 18)|(1 << 17)|(0 << 16)|
792 (2 << 14)|(0 << 13)|(0 << 12)|
793 (0 << 11)|(0 << 10)|(0 << 9)|(0 << 8)|
794 (0 << 3) |(0 << 1) |(0 << 0),
795 /* DRAM Config High Register
797 * [ 0: 3] Maximum Asynchronous Latency
802 * [11: 8] Read Preamble
820 * [18:16] Idle Cycle Limit
829 * [19:19] Dynamic Idle Cycle Center Enable
830 * 0 = Use Idle Cycle Limit
831 * 1 = Generate a dynamic Idle cycle limit
832 * [22:20] DRAM MEMCLK Frequency
842 * [25:25] Memory Clock Ratio Valid (FIXME carefully enable memclk)
843 * 0 = Disable MemClks
845 * [26:26] Memory Clock 0 Enable
848 * [27:27] Memory Clock 1 Enable
851 * [28:28] Memory Clock 2 Enable
854 * [29:29] Memory Clock 3 Enable
859 PCI_ADDR(0, 0x18, 2, 0x94), 0xc180f0f0,
860 (0 << 29)|(0 << 28)|(0 << 27)|(0 << 26)|(0 << 25)|
861 (0 << 20)|(0 << 19)|(DCH_IDLE_LIMIT_16 << 16)|(0 << 8)|(0 << 0),
862 /* DRAM Delay Line Register
864 * Adjust the skew of the input DQS strobe relative to DATA
866 * [23:16] Delay Line Adjust
867 * Adjusts the DLL derived PDL delay by one or more delay stages
868 * in either the faster or slower direction.
869 * [24:24} Adjust Slower
871 * 1 = Adj is used to increase the PDL delay
872 * [25:25] Adjust Faster
874 * 1 = Adj is used to decrease the PDL delay
877 PCI_ADDR(0, 0x18, 2, 0x98), 0xfc00ffff, 0x00000000,
878 /* DRAM Scrub Control Register
880 * [ 4: 0] DRAM Scrube Rate
882 * [12: 8] L2 Scrub Rate
884 * [20:16] Dcache Scrub
887 * 00000 = Do not scrub
909 * All Others = Reserved
911 PCI_ADDR(0, 0x18, 3, 0x58), 0xffe0e0e0, 0x00000000,
912 /* DRAM Scrub Address Low Register
914 * [ 0: 0] DRAM Scrubber Redirect Enable
916 * 1 = Scrubber Corrects errors found in normal operation
918 * [31: 6] DRAM Scrub Address 31-6
920 PCI_ADDR(0, 0x18, 3, 0x5C), 0x0000003e, 0x00000000,
921 /* DRAM Scrub Address High Register
923 * [ 7: 0] DRAM Scrubb Address 39-32
926 PCI_ADDR(0, 0x18, 3, 0x60), 0xffffff00, 0x00000000,
928 #if ENABLE_IOMMU != 0
929 /* BY LYH add IOMMU 64M APERTURE */
930 PCI_ADDR(0, 0x18, 3, 0x94), 0xffff8000, 0x00000f70,
931 PCI_ADDR(0, 0x18, 3, 0x90), 0xffffff80, 0x00000002,
932 PCI_ADDR(0, 0x18, 3, 0x98), 0x0000000f, 0x00068300,
937 print_debug("setting up CPU");
938 print_debug_hex8(ctrl->node_id);
939 print_debug(" northbridge registers\r\n");
940 max = sizeof(register_values)/sizeof(register_values[0]);
941 for(i = 0; i < max; i += 3) {
946 print_debug_hex32(register_values[i]);
948 print_debug_hex32(register_values[i+2]);
951 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x18, 0) + ctrl->f0;
952 where = register_values[i] & 0xff;
953 reg = pci_read_config32(dev, where);
954 reg &= register_values[i+1];
955 reg |= register_values[i+2];
956 pci_write_config32(dev, where, reg);
959 reg = pci_read_config32(register_values[i]);
960 reg &= register_values[i+1];
961 reg |= register_values[i+2];
962 pci_write_config32(register_values[i], reg);
965 print_debug("done.\r\n");
969 static int is_dual_channel(const struct mem_controller *ctrl)
972 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
973 return dcl & DCL_128BitEn;
976 static int is_opteron(const struct mem_controller *ctrl)
978 /* Test to see if I am an Opteron.
979 * FIXME Testing dual channel capability is correct for now
980 * but a beter test is probably required.
982 #warning "FIXME implement a better test for opterons"
984 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
985 return !!(nbcap & NBCAP_128Bit);
988 static int is_registered(const struct mem_controller *ctrl)
990 /* Test to see if we are dealing with registered SDRAM.
991 * If we are not registered we are unbuffered.
992 * This function must be called after spd_handle_unbuffered_dimms.
995 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
996 return !(dcl & DCL_UnBufDimm);
1000 unsigned long side1;
1001 unsigned long side2;
1004 static struct dimm_size spd_get_dimm_size(unsigned device)
1006 /* Calculate the log base 2 size of a DIMM in bits */
1007 struct dimm_size sz;
1012 /* Note it might be easier to use byte 31 here, it has the DIMM size as
1013 * a multiple of 4MB. The way we do it now we can size both
1014 * sides of an assymetric dimm.
1016 value = spd_read_byte(device, 3); /* rows */
1017 if (value < 0) goto out;
1018 sz.side1 += value & 0xf;
1020 value = spd_read_byte(device, 4); /* columns */
1021 if (value < 0) goto out;
1022 sz.side1 += value & 0xf;
1024 value = spd_read_byte(device, 17); /* banks */
1025 if (value < 0) goto out;
1026 sz.side1 += log2(value & 0xff);
1028 /* Get the module data width and convert it to a power of two */
1029 value = spd_read_byte(device, 7); /* (high byte) */
1030 if (value < 0) goto out;
1034 low = spd_read_byte(device, 6); /* (low byte) */
1035 if (low < 0) goto out;
1036 value = value | (low & 0xff);
1037 sz.side1 += log2(value);
1040 value = spd_read_byte(device, 5); /* number of physical banks */
1041 if (value <= 1) goto out;
1043 /* Start with the symmetrical case */
1044 sz.side2 = sz.side1;
1046 value = spd_read_byte(device, 3); /* rows */
1047 if (value < 0) goto out;
1048 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
1049 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
1050 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
1052 value = spd_read_byte(device, 4); /* columns */
1053 if (value < 0) goto out;
1054 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
1055 sz.side2 += ((value >> 4) & 0x0f); /* Add in columsn on side 2 */
1061 static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz, unsigned index)
1063 uint32_t base0, base1, map;
1067 print_debug("set_dimm_size: (");
1068 print_debug_hex32(sz.side1);
1069 print_debug_char(',');
1070 print_debug_hex32(sz.side2);
1071 print_debug_char(',');
1072 print_debug_hex32(index);
1073 print_debug(")\r\n");
1075 if (sz.side1 != sz.side2) {
1078 map = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
1079 map &= ~(0xf << (index + 4));
1081 /* For each base register.
1082 * Place the dimm size in 32 MB quantities in the bits 31 - 21.
1083 * The initialize dimm size is in bits.
1084 * Set the base enable bit0.
1089 /* Make certain side1 of the dimm is at least 32MB */
1090 if (sz.side1 >= (25 +3)) {
1091 map |= (sz.side1 - (25 + 3)) << (index *4);
1092 base0 = (1 << ((sz.side1 - (25 + 3)) + 21)) | 1;
1094 /* Make certain side2 of the dimm is at least 32MB */
1095 if (sz.side2 >= (25 + 3)) {
1096 base1 = (1 << ((sz.side2 - (25 + 3)) + 21)) | 1;
1099 /* Double the size if we are using dual channel memory */
1100 if (is_dual_channel(ctrl)) {
1101 base0 = (base0 << 1) | (base0 & 1);
1102 base1 = (base1 << 1) | (base1 & 1);
1105 /* Clear the reserved bits */
1106 base0 &= ~0x001ffffe;
1107 base1 &= ~0x001ffffe;
1109 /* Set the appropriate DIMM base address register */
1110 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
1111 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
1112 pci_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
1114 /* Enable the memory clocks for this DIMM */
1116 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1117 dch |= DCH_MEMCLK_EN0 << index;
1118 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
1122 static void spd_set_ram_size(const struct mem_controller *ctrl)
1126 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1127 struct dimm_size sz;
1128 sz = spd_get_dimm_size(ctrl->channel0[i]);
1129 set_dimm_size(ctrl, sz, i);
1133 static void route_dram_accesses(const struct mem_controller *ctrl,
1134 unsigned long base_k, unsigned long limit_k)
1136 /* Route the addresses to the controller node */
1141 unsigned limit_reg, base_reg;
1144 node_id = ctrl->node_id;
1145 index = (node_id << 3);
1146 limit = (limit_k << 2);
1147 limit &= 0xffff0000;
1148 limit -= 0x00010000;
1149 limit |= ( 0 << 8) | (node_id << 0);
1150 base = (base_k << 2);
1152 base |= (0 << 8) | (1<<1) | (1<<0);
1154 limit_reg = 0x44 + index;
1155 base_reg = 0x40 + index;
1156 for(device = PCI_DEV(0, 0x18, 1); device <= PCI_DEV(0, 0x1f, 1); device += PCI_DEV(0, 1, 0)) {
1157 pci_write_config32(device, limit_reg, limit);
1158 pci_write_config32(device, base_reg, base);
1162 static void set_top_mem(unsigned tom_k)
1164 /* Error if I don't have memory */
1167 print_debug("No memory - reset");
1169 pci_write_config8(PCI_DEV(0, 0x04, 3), 0x41, 0xf1);
1175 /* Report the amount of memory. */
1176 print_debug("RAM: 0x");
1177 print_debug_hex32(tom_k);
1178 print_debug(" KB\r\n");
1181 /* Now set top of memory */
1183 msr.lo = (tom_k & 0x003fffff) << 10;
1184 msr.hi = (tom_k & 0xffc00000) >> 22;
1185 wrmsr(TOP_MEM2, msr);
1187 /* Leave a 64M hole between TOP_MEM and TOP_MEM2
1188 * so I can see my rom chip and other I/O devices.
1190 if (tom_k >= 0x003f0000) {
1193 msr.lo = (tom_k & 0x003fffff) << 10;
1194 msr.hi = (tom_k & 0xffc00000) >> 22;
1195 wrmsr(TOP_MEM, msr);
1198 static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
1201 static const uint32_t csbase_low[] = {
1202 /* 32MB */ (1 << (13 - 4)),
1203 /* 64MB */ (1 << (14 - 4)),
1204 /* 128MB */ (1 << (14 - 4)),
1205 /* 256MB */ (1 << (15 - 4)),
1206 /* 512MB */ (1 << (15 - 4)),
1207 /* 1GB */ (1 << (16 - 4)),
1208 /* 2GB */ (1 << (16 - 4)),
1210 uint32_t csbase_inc;
1211 int chip_selects, index;
1214 unsigned common_size;
1215 uint32_t csbase, csmask;
1217 /* See if all of the memory chip selects are the same size
1218 * and if so count them.
1222 for(index = 0; index < 8; index++) {
1226 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1228 /* Is it enabled? */
1234 if (common_size == 0) {
1237 /* The size differed fail */
1238 if (common_size != size) {
1242 /* Chip selects can only be interleaved when there is
1243 * more than one and their is a power of two of them.
1245 bits = log2(chip_selects);
1246 if (((1 << bits) != chip_selects) || (bits < 1) || (bits > 3)) {
1250 /* Also we run out of address mask bits if we try and interleave 8 4GB dimms */
1251 if ((bits == 3) && (common_size == (1 << (32 - 3)))) {
1252 print_debug("8 4GB chip selects cannot be interleaved\r\n");
1255 /* Find the bits of csbase that we need to interleave on */
1256 if (is_dual_channel(ctrl)) {
1257 csbase_inc = csbase_low[log2(common_size) - 1] << 1;
1259 csbase_inc = csbase_low[log2(common_size)];
1261 /* Compute the initial values for csbase and csbask.
1262 * In csbase just set the enable bit and the base to zero.
1263 * In csmask set the mask bits for the size and page level interleave.
1266 csmask = (((common_size << bits) - 1) << 21);
1267 csmask |= 0xfe00 & ~((csbase_inc << bits) - csbase_inc);
1268 for(index = 0; index < 8; index++) {
1271 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1272 /* Is it enabled? */
1276 pci_write_config32(ctrl->f2, DRAM_CSBASE + (index << 2), csbase);
1277 pci_write_config32(ctrl->f2, DRAM_CSMASK + (index << 2), csmask);
1278 csbase += csbase_inc;
1282 print_debug("Interleaved\r\n");
1284 /* Return the memory size in K */
1285 return common_size << (15 + bits);
1288 static unsigned long order_chip_selects(const struct mem_controller *ctrl)
1292 /* Remember which registers we have used in the high 8 bits of tom */
1295 /* Find the largest remaining canidate */
1296 unsigned index, canidate;
1297 uint32_t csbase, csmask;
1301 for(index = 0; index < 8; index++) {
1303 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1305 /* Is it enabled? */
1310 /* Is it greater? */
1311 if (value <= csbase) {
1315 /* Has it already been selected */
1316 if (tom & (1 << (index + 24))) {
1319 /* I have a new canidate */
1323 /* See if I have found a new canidate */
1328 /* Remember the dimm size */
1329 size = csbase >> 21;
1331 /* Remember I have used this register */
1332 tom |= (1 << (canidate + 24));
1334 /* Recompute the cs base register value */
1335 csbase = (tom << 21) | 1;
1337 /* Increment the top of memory */
1340 /* Compute the memory mask */
1341 csmask = ((size -1) << 21);
1342 csmask |= 0xfe00; /* For now don't optimize */
1343 #warning "Don't forget to optimize the DIMM size"
1345 /* Write the new base register */
1346 pci_write_config32(ctrl->f2, DRAM_CSBASE + (canidate << 2), csbase);
1347 /* Write the new mask register */
1348 pci_write_config32(ctrl->f2, DRAM_CSMASK + (canidate << 2), csmask);
1351 /* Return the memory size in K */
1352 return (tom & ~0xff000000) << 15;
1355 static void order_dimms(const struct mem_controller *ctrl)
1357 unsigned long tom, tom_k, base_k;
1360 tom_k = interleave_chip_selects(ctrl);
1362 tom_k = order_chip_selects(ctrl);
1364 /* Compute the memory base address */
1366 for(node_id = 0; node_id < ctrl->node_id; node_id++) {
1367 uint32_t limit, base;
1369 index = node_id << 3;
1370 base = pci_read_config32(ctrl->f1, 0x40 + index);
1371 /* Only look at the limit if the base is enabled */
1372 if ((base & 3) == 3) {
1373 limit = pci_read_config32(ctrl->f1, 0x44 + index);
1374 base_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
1379 print_debug("base_k: ");
1380 print_debug_hex32(base_k);
1381 print_debug(" tom_k: ");
1382 print_debug_hex32(tom_k);
1383 print_debug("\r\n");
1385 route_dram_accesses(ctrl, base_k, tom_k);
1389 static void disable_dimm(const struct mem_controller *ctrl, unsigned index)
1391 print_debug("disabling dimm");
1392 print_debug_hex8(index);
1393 print_debug("\r\n");
1394 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
1395 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
1399 static void spd_handle_unbuffered_dimms(const struct mem_controller *ctrl)
1407 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1409 value = spd_read_byte(ctrl->channel0[i], 21);
1411 disable_dimm(ctrl, i);
1414 /* Registered dimm ? */
1415 if (value & (1 << 1)) {
1418 /* Otherwise it must be an unbuffered dimm */
1423 if (unbuffered && registered) {
1424 die("Mixed buffered and registered dimms not supported");
1426 if (unbuffered && is_opteron(ctrl)) {
1427 die("Unbuffered Dimms not supported on Opteron");
1430 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1431 dcl &= ~DCL_UnBufDimm;
1433 dcl |= DCL_UnBufDimm;
1435 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1437 if (is_registered(ctrl)) {
1438 print_debug("Registered\r\n");
1440 print_debug("Unbuffered\r\n");
1445 static void spd_enable_2channels(const struct mem_controller *ctrl)
1449 /* SPD addresses to verify are identical */
1450 #warning "FINISHME review and see if these are the bytes I need"
1451 /* FINISHME review and see if these are the bytes I need */
1452 static const unsigned addresses[] = {
1453 2, /* Type should be DDR SDRAM */
1454 3, /* *Row addresses */
1455 4, /* *Column addresses */
1456 5, /* *Physical Banks */
1457 6, /* *Module Data Width low */
1458 7, /* *Module Data Width high */
1459 9, /* *Cycle time at highest CAS Latency CL=X */
1460 11, /* *SDRAM Type */
1461 13, /* *SDRAM Width */
1462 17, /* *Logical Banks */
1463 18, /* *Supported CAS Latencies */
1464 21, /* *SDRAM Module Attributes */
1465 23, /* *Cycle time at CAS Latnecy (CLX - 0.5) */
1466 26, /* *Cycle time at CAS Latnecy (CLX - 1.0) */
1467 27, /* *tRP Row precharge time */
1468 28, /* *Minimum Row Active to Row Active Delay (tRRD) */
1469 29, /* *tRCD RAS to CAS */
1470 30, /* *tRAS Activate to Precharge */
1471 41, /* *Minimum Active to Active/Auto Refresh Time(Trc) */
1472 42, /* *Minimum Auto Refresh Command Time(Trfc) */
1474 nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1475 if (!(nbcap & NBCAP_128Bit)) {
1478 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1479 unsigned device0, device1;
1482 device0 = ctrl->channel0[i];
1483 device1 = ctrl->channel1[i];
1486 for(j = 0; j < sizeof(addresses)/sizeof(addresses[0]); j++) {
1488 addr = addresses[j];
1489 value0 = spd_read_byte(device0, addr);
1493 value1 = spd_read_byte(device1, addr);
1497 if (value0 != value1) {
1502 print_debug("Enabling dual channel memory\r\n");
1504 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1505 dcl &= ~DCL_32ByteEn;
1506 dcl |= DCL_128BitEn;
1507 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1512 uint8_t divisor; /* In 1/2 ns increments */
1515 uint32_t dch_memclk;
1516 uint16_t dch_tref4k, dch_tref8k;
1521 static const struct mem_param *get_mem_param(unsigned min_cycle_time)
1523 static const struct mem_param speed[] = {
1525 .name = "100Mhz\r\n",
1527 .divisor = (10 <<1),
1530 .dch_memclk = DCH_MEMCLK_100MHZ << DCH_MEMCLK_SHIFT,
1531 .dch_tref4k = DTH_TREF_100MHZ_4K,
1532 .dch_tref8k = DTH_TREF_100MHZ_8K,
1536 .name = "133Mhz\r\n",
1538 .divisor = (7<<1)+1,
1541 .dch_memclk = DCH_MEMCLK_133MHZ << DCH_MEMCLK_SHIFT,
1542 .dch_tref4k = DTH_TREF_133MHZ_4K,
1543 .dch_tref8k = DTH_TREF_133MHZ_8K,
1547 .name = "166Mhz\r\n",
1552 .dch_memclk = DCH_MEMCLK_166MHZ << DCH_MEMCLK_SHIFT,
1553 .dch_tref4k = DTH_TREF_166MHZ_4K,
1554 .dch_tref8k = DTH_TREF_166MHZ_8K,
1558 .name = "200Mhz\r\n",
1563 .dch_memclk = DCH_MEMCLK_200MHZ << DCH_MEMCLK_SHIFT,
1564 .dch_tref4k = DTH_TREF_200MHZ_4K,
1565 .dch_tref8k = DTH_TREF_200MHZ_8K,
1572 const struct mem_param *param;
1573 for(param = &speed[0]; param->cycle_time ; param++) {
1574 if (min_cycle_time > (param+1)->cycle_time) {
1578 if (!param->cycle_time) {
1579 die("min_cycle_time to low");
1582 print_debug(param->name);
1587 static const struct mem_param *spd_set_memclk(const struct mem_controller *ctrl)
1589 /* Compute the minimum cycle time for these dimms */
1590 const struct mem_param *param;
1591 unsigned min_cycle_time, min_latency;
1595 static const int latency_indicies[] = { 26, 23, 9 };
1596 static const unsigned char min_cycle_times[] = {
1597 [NBCAP_MEMCLK_200MHZ] = 0x50, /* 5ns */
1598 [NBCAP_MEMCLK_166MHZ] = 0x60, /* 6ns */
1599 [NBCAP_MEMCLK_133MHZ] = 0x75, /* 7.5ns */
1600 [NBCAP_MEMCLK_100MHZ] = 0xa0, /* 10ns */
1604 value = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1605 min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
1609 print_debug("min_cycle_time: ");
1610 print_debug_hex8(min_cycle_time);
1611 print_debug(" min_latency: ");
1612 print_debug_hex8(min_latency);
1613 print_debug("\r\n");
1616 /* Compute the least latency with the fastest clock supported
1617 * by both the memory controller and the dimms.
1619 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1620 int new_cycle_time, new_latency;
1625 /* First find the supported CAS latencies
1626 * Byte 18 for DDR SDRAM is interpreted:
1627 * bit 0 == CAS Latency = 1.0
1628 * bit 1 == CAS Latency = 1.5
1629 * bit 2 == CAS Latency = 2.0
1630 * bit 3 == CAS Latency = 2.5
1631 * bit 4 == CAS Latency = 3.0
1632 * bit 5 == CAS Latency = 3.5
1636 new_cycle_time = 0xa0;
1639 latencies = spd_read_byte(ctrl->channel0[i], 18);
1640 if (latencies <= 0) continue;
1642 /* Compute the lowest cas latency supported */
1643 latency = log2(latencies) -2;
1645 /* Loop through and find a fast clock with a low latency */
1646 for(index = 0; index < 3; index++, latency++) {
1648 if ((latency < 2) || (latency > 4) ||
1649 (!(latencies & (1 << latency)))) {
1652 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1657 /* Only increase the latency if we decreas the clock */
1658 if ((value >= min_cycle_time) && (value < new_cycle_time)) {
1659 new_cycle_time = value;
1660 new_latency = latency;
1663 if (new_latency > 4){
1666 /* Does min_latency need to be increased? */
1667 if (new_cycle_time > min_cycle_time) {
1668 min_cycle_time = new_cycle_time;
1670 /* Does min_cycle_time need to be increased? */
1671 if (new_latency > min_latency) {
1672 min_latency = new_latency;
1676 print_debug_hex8(i);
1677 print_debug(" min_cycle_time: ");
1678 print_debug_hex8(min_cycle_time);
1679 print_debug(" min_latency: ");
1680 print_debug_hex8(min_latency);
1681 print_debug("\r\n");
1684 /* Make a second pass through the dimms and disable
1685 * any that cannot support the selected memclk and cas latency.
1688 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1694 latencies = spd_read_byte(ctrl->channel0[i], 18);
1695 if (latencies <= 0) {
1699 /* Compute the lowest cas latency supported */
1700 latency = log2(latencies) -2;
1702 /* Walk through searching for the selected latency */
1703 for(index = 0; index < 3; index++, latency++) {
1704 if (!(latencies & (1 << latency))) {
1707 if (latency == min_latency)
1710 /* If I can't find the latency or my index is bad error */
1711 if ((latency != min_latency) || (index >= 3)) {
1715 /* Read the min_cycle_time for this latency */
1716 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1718 /* All is good if the selected clock speed
1719 * is what I need or slower.
1721 if (value <= min_cycle_time) {
1724 /* Otherwise I have an error, disable the dimm */
1726 disable_dimm(ctrl, i);
1729 print_debug("min_cycle_time: ");
1730 print_debug_hex8(min_cycle_time);
1731 print_debug(" min_latency: ");
1732 print_debug_hex8(min_latency);
1733 print_debug("\r\n");
1735 /* Now that I know the minimum cycle time lookup the memory parameters */
1736 param = get_mem_param(min_cycle_time);
1738 /* Update DRAM Config High with our selected memory speed */
1739 value = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1740 value &= ~(DCH_MEMCLK_MASK << DCH_MEMCLK_SHIFT);
1741 value |= param->dch_memclk;
1742 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
1744 static const unsigned latencies[] = { DTL_CL_2, DTL_CL_2_5, DTL_CL_3 };
1745 /* Update DRAM Timing Low with our selected cas latency */
1746 value = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1747 value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
1748 value |= latencies[min_latency - 2] << DTL_TCL_SHIFT;
1749 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
1755 static int update_dimm_Trc(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], 41);
1761 if (value < 0) return -1;
1762 if ((value == 0) || (value == 0xff)) {
1765 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1766 if (clocks < DTL_TRC_MIN) {
1767 clocks = DTL_TRC_MIN;
1769 if (clocks > DTL_TRC_MAX) {
1773 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1774 old_clocks = ((dtl >> DTL_TRC_SHIFT) & DTL_TRC_MASK) + DTL_TRC_BASE;
1775 if (old_clocks > clocks) {
1776 clocks = old_clocks;
1778 dtl &= ~(DTL_TRC_MASK << DTL_TRC_SHIFT);
1779 dtl |= ((clocks - DTL_TRC_BASE) << DTL_TRC_SHIFT);
1780 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1784 static int update_dimm_Trfc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1786 unsigned clocks, old_clocks;
1789 value = spd_read_byte(ctrl->channel0[i], 42);
1790 if (value < 0) return -1;
1791 if ((value == 0) || (value == 0xff)) {
1792 value = param->tRFC;
1794 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1795 if (clocks < DTL_TRFC_MIN) {
1796 clocks = DTL_TRFC_MIN;
1798 if (clocks > DTL_TRFC_MAX) {
1801 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1802 old_clocks = ((dtl >> DTL_TRFC_SHIFT) & DTL_TRFC_MASK) + DTL_TRFC_BASE;
1803 if (old_clocks > clocks) {
1804 clocks = old_clocks;
1806 dtl &= ~(DTL_TRFC_MASK << DTL_TRFC_SHIFT);
1807 dtl |= ((clocks - DTL_TRFC_BASE) << DTL_TRFC_SHIFT);
1808 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1813 static int update_dimm_Trcd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1815 unsigned clocks, old_clocks;
1818 value = spd_read_byte(ctrl->channel0[i], 29);
1819 if (value < 0) return -1;
1821 clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1823 clocks = (value + ((param->divisor & 0xff) << 1) -1)/((param->divisor & 0xff) << 1);
1825 if (clocks < DTL_TRCD_MIN) {
1826 clocks = DTL_TRCD_MIN;
1828 if (clocks > DTL_TRCD_MAX) {
1831 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1832 old_clocks = ((dtl >> DTL_TRCD_SHIFT) & DTL_TRCD_MASK) + DTL_TRCD_BASE;
1833 if (old_clocks > clocks) {
1834 clocks = old_clocks;
1836 dtl &= ~(DTL_TRCD_MASK << DTL_TRCD_SHIFT);
1837 dtl |= ((clocks - DTL_TRCD_BASE) << DTL_TRCD_SHIFT);
1838 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1842 static int update_dimm_Trrd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1844 unsigned clocks, old_clocks;
1847 value = spd_read_byte(ctrl->channel0[i], 28);
1848 if (value < 0) return -1;
1849 clocks = (value + ((param->divisor & 0xff) << 1) -1)/((param->divisor & 0xff) << 1);
1850 if (clocks < DTL_TRRD_MIN) {
1851 clocks = DTL_TRRD_MIN;
1853 if (clocks > DTL_TRRD_MAX) {
1856 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1857 old_clocks = ((dtl >> DTL_TRRD_SHIFT) & DTL_TRRD_MASK) + DTL_TRRD_BASE;
1858 if (old_clocks > clocks) {
1859 clocks = old_clocks;
1861 dtl &= ~(DTL_TRRD_MASK << DTL_TRRD_SHIFT);
1862 dtl |= ((clocks - DTL_TRRD_BASE) << DTL_TRRD_SHIFT);
1863 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1867 static int update_dimm_Tras(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1869 unsigned clocks, old_clocks;
1872 value = spd_read_byte(ctrl->channel0[i], 30);
1873 if (value < 0) return -1;
1874 clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1875 if (clocks < DTL_TRAS_MIN) {
1876 clocks = DTL_TRAS_MIN;
1878 if (clocks > DTL_TRAS_MAX) {
1881 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1882 old_clocks = ((dtl >> DTL_TRAS_SHIFT) & DTL_TRAS_MASK) + DTL_TRAS_BASE;
1883 if (old_clocks > clocks) {
1884 clocks = old_clocks;
1886 dtl &= ~(DTL_TRAS_MASK << DTL_TRAS_SHIFT);
1887 dtl |= ((clocks - DTL_TRAS_BASE) << DTL_TRAS_SHIFT);
1888 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1892 static int update_dimm_Trp(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1894 unsigned clocks, old_clocks;
1897 value = spd_read_byte(ctrl->channel0[i], 27);
1898 if (value < 0) return -1;
1900 clocks = (value + (param->divisor << 1) - 1)/(param->divisor << 1);
1902 clocks = (value + ((param->divisor & 0xff) << 1) - 1)/((param->divisor & 0xff) << 1);
1905 print_debug("Trp: ");
1906 print_debug_hex8(clocks);
1907 print_debug(" spd value: ");
1908 print_debug_hex8(value);
1909 print_debug(" divisor: ");
1910 print_debug_hex8(param->divisor);
1911 print_debug("\r\n");
1913 if (clocks < DTL_TRP_MIN) {
1914 clocks = DTL_TRP_MIN;
1916 if (clocks > DTL_TRP_MAX) {
1919 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1920 old_clocks = ((dtl >> DTL_TRP_SHIFT) & DTL_TRP_MASK) + DTL_TRP_BASE;
1921 if (old_clocks > clocks) {
1922 clocks = old_clocks;
1924 dtl &= ~(DTL_TRP_MASK << DTL_TRP_SHIFT);
1925 dtl |= ((clocks - DTL_TRP_BASE) << DTL_TRP_SHIFT);
1926 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1930 static void set_Twr(const struct mem_controller *ctrl, const struct mem_param *param)
1933 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1934 dtl &= ~(DTL_TWR_MASK << DTL_TWR_SHIFT);
1935 dtl |= (param->dtl_twr - DTL_TWR_BASE) << DTL_TWR_SHIFT;
1936 pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1940 static void init_Tref(const struct mem_controller *ctrl, const struct mem_param *param)
1943 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1944 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1945 dth |= (param->dch_tref4k << DTH_TREF_SHIFT);
1946 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1949 static int update_dimm_Tref(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1953 unsigned tref, old_tref;
1954 value = spd_read_byte(ctrl->channel0[i], 3);
1955 if (value < 0) return -1;
1958 tref = param->dch_tref8k;
1960 tref = param->dch_tref4k;
1963 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1964 old_tref = (dth >> DTH_TREF_SHIFT) & DTH_TREF_MASK;
1965 if ((value == 12) && (old_tref == param->dch_tref4k)) {
1966 tref = param->dch_tref4k;
1968 tref = param->dch_tref8k;
1970 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1971 dth |= (tref << DTH_TREF_SHIFT);
1972 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1977 static int update_dimm_x4(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1982 value = spd_read_byte(ctrl->channel0[i], 13);
1987 dimm += DCL_x4DIMM_SHIFT;
1988 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1989 dcl &= ~(1 << dimm);
1993 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1997 static int update_dimm_ecc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
2001 value = spd_read_byte(ctrl->channel0[i], 11);
2006 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
2007 dcl &= ~DCL_DimmEccEn;
2008 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2013 static int count_dimms(const struct mem_controller *ctrl)
2018 for(index = 0; index < 8; index += 2) {
2020 csbase = pci_read_config32(ctrl->f2, (DRAM_CSBASE + index << 2));
2028 static void set_Twtr(const struct mem_controller *ctrl, const struct mem_param *param)
2032 clocks = 1; /* AMD says hard code this */
2033 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
2034 dth &= ~(DTH_TWTR_MASK << DTH_TWTR_SHIFT);
2035 dth |= ((clocks - DTH_TWTR_BASE) << DTH_TWTR_SHIFT);
2036 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
2039 static void set_Trwt(const struct mem_controller *ctrl, const struct mem_param *param)
2047 dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
2048 latency = (dtl >> DTL_TCL_SHIFT) & DTL_TCL_MASK;
2049 divisor = param->divisor;
2051 if (is_opteron(ctrl)) {
2052 if (latency == DTL_CL_2) {
2053 if (divisor == ((6 << 0) + 0)) {
2057 else if (divisor > ((6 << 0)+0)) {
2058 /* 100Mhz && 133Mhz */
2062 else if (latency == DTL_CL_2_5) {
2065 else if (latency == DTL_CL_3) {
2066 if (divisor == ((6 << 0)+0)) {
2070 else if (divisor > ((6 << 0)+0)) {
2071 /* 100Mhz && 133Mhz */
2076 else /* Athlon64 */ {
2077 if (is_registered(ctrl)) {
2078 if (latency == DTL_CL_2) {
2081 else if (latency == DTL_CL_2_5) {
2084 else if (latency == DTL_CL_3) {
2088 else /* Unbuffered */{
2089 if (latency == DTL_CL_2) {
2092 else if (latency == DTL_CL_2_5) {
2095 else if (latency == DTL_CL_3) {
2100 if ((clocks < DTH_TRWT_MIN) || (clocks > DTH_TRWT_MAX)) {
2101 die("Unknown Trwt");
2104 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
2105 dth &= ~(DTH_TRWT_MASK << DTH_TRWT_SHIFT);
2106 dth |= ((clocks - DTH_TRWT_BASE) << DTH_TRWT_SHIFT);
2107 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
2111 static void set_Twcl(const struct mem_controller *ctrl, const struct mem_param *param)
2113 /* Memory Clocks after CAS# */
2116 if (is_registered(ctrl)) {
2121 dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
2122 dth &= ~(DTH_TWCL_MASK << DTH_TWCL_SHIFT);
2123 dth |= ((clocks - DTH_TWCL_BASE) << DTH_TWCL_SHIFT);
2124 pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
2128 static void set_read_preamble(const struct mem_controller *ctrl, const struct mem_param *param)
2132 unsigned rdpreamble;
2133 divisor = param->divisor;
2134 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2135 dch &= ~(DCH_RDPREAMBLE_MASK << DCH_RDPREAMBLE_SHIFT);
2137 if (is_registered(ctrl)) {
2138 if (divisor == ((10 << 1)+0)) {
2140 rdpreamble = ((9 << 1)+ 0);
2142 else if (divisor == ((7 << 1)+1)) {
2144 rdpreamble = ((8 << 1)+0);
2146 else if (divisor == ((6 << 1)+0)) {
2148 rdpreamble = ((7 << 1)+1);
2150 else if (divisor == ((5 << 1)+0)) {
2152 rdpreamble = ((7 << 1)+0);
2159 for(i = 0; i < 4; i++) {
2160 if (ctrl->channel0[i]) {
2164 if (divisor == ((10 << 1)+0)) {
2168 rdpreamble = ((9 << 1)+0);
2171 rdpreamble = ((14 << 1)+0);
2174 else if (divisor == ((7 << 1)+1)) {
2178 rdpreamble = ((7 << 1)+0);
2181 rdpreamble = ((11 << 1)+0);
2184 else if (divisor == ((6 << 1)+0)) {
2188 rdpreamble = ((7 << 1)+0);
2191 rdpreamble = ((9 << 1)+0);
2194 else if (divisor == ((5 << 1)+0)) {
2198 rdpreamble = ((5 << 1)+0);
2201 rdpreamble = ((7 << 1)+0);
2205 if ((rdpreamble < DCH_RDPREAMBLE_MIN) || (rdpreamble > DCH_RDPREAMBLE_MAX)) {
2206 die("Unknown rdpreamble");
2208 dch |= (rdpreamble - DCH_RDPREAMBLE_BASE) << DCH_RDPREAMBLE_SHIFT;
2209 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2212 static void set_max_async_latency(const struct mem_controller *ctrl, const struct mem_param *param)
2219 dimms = count_dimms(ctrl);
2221 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2222 dch &= ~(DCH_ASYNC_LAT_MASK << DCH_ASYNC_LAT_SHIFT);
2224 if (is_registered(ctrl)) {
2236 die("Too many unbuffered dimms");
2238 else if (dimms == 3) {
2247 dch |= ((async_lat - DCH_ASYNC_LAT_BASE) << DCH_ASYNC_LAT_SHIFT);
2248 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2251 static void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct mem_param *param)
2254 /* AMD says to Hardcode this */
2255 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2256 dch &= ~(DCH_IDLE_LIMIT_MASK << DCH_IDLE_LIMIT_SHIFT);
2257 dch |= DCH_IDLE_LIMIT_16 << DCH_IDLE_LIMIT_SHIFT;
2258 dch |= DCH_DYN_IDLE_CTR_EN;
2259 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2262 static void spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param)
2268 init_Tref(ctrl, param);
2269 for(i = 0; (i < 4) && ctrl->channel0[i]; i++) {
2271 /* DRAM Timing Low Register */
2272 if (update_dimm_Trc (ctrl, param, i) < 0) goto dimm_err;
2273 if (update_dimm_Trfc(ctrl, param, i) < 0) goto dimm_err;
2274 if (update_dimm_Trcd(ctrl, param, i) < 0) goto dimm_err;
2275 if (update_dimm_Trrd(ctrl, param, i) < 0) goto dimm_err;
2276 if (update_dimm_Tras(ctrl, param, i) < 0) goto dimm_err;
2277 if (update_dimm_Trp (ctrl, param, i) < 0) goto dimm_err;
2279 /* DRAM Timing High Register */
2280 if (update_dimm_Tref(ctrl, param, i) < 0) goto dimm_err;
2282 /* DRAM Config Low */
2283 if (update_dimm_x4 (ctrl, param, i) < 0) goto dimm_err;
2284 if (update_dimm_ecc(ctrl, param, i) < 0) goto dimm_err;
2287 disable_dimm(ctrl, i);
2290 /* DRAM Timing Low Register */
2291 set_Twr(ctrl, param);
2293 /* DRAM Timing High Register */
2294 set_Twtr(ctrl, param);
2295 set_Trwt(ctrl, param);
2296 set_Twcl(ctrl, param);
2298 /* DRAM Config High */
2299 set_read_preamble(ctrl, param);
2300 set_max_async_latency(ctrl, param);
2301 set_idle_cycle_limit(ctrl, param);
2304 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
2306 const struct mem_param *param;
2307 activate_spd_rom(ctrl);
2308 spd_enable_2channels(ctrl);
2309 spd_set_ram_size(ctrl);
2310 spd_handle_unbuffered_dimms(ctrl);
2311 param = spd_set_memclk(ctrl);
2312 spd_set_dram_timing(ctrl, param);
2316 #define TIMEOUT_LOOPS 300000
2317 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
2321 /* Before enabling memory start the memory clocks */
2322 for(i = 0; i < controllers; i++) {
2324 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2325 dch |= DCH_MEMCLK_VALID;
2326 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2329 /* And if necessary toggle the the reset on the dimms by hand */
2330 memreset(controllers, ctrl);
2332 for(i = 0; i < controllers; i++) {
2334 /* Toggle DisDqsHys to get it working */
2335 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2337 print_debug("dcl: ");
2338 print_debug_hex32(dcl);
2339 print_debug("\r\n");
2341 if (dcl & DCL_DimmEccEn) {
2343 print_debug("ECC enabled\r\n");
2344 mnc = pci_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
2346 if (dcl & DCL_128BitEn) {
2347 mnc |= MNC_CHIPKILL_EN;
2349 pci_write_config32(ctrl[i].f3, MCA_NB_CONFIG, mnc);
2351 dcl |= DCL_DisDqsHys;
2352 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2353 dcl &= ~DCL_DisDqsHys;
2354 dcl &= ~DCL_DLL_Disable;
2357 dcl |= DCL_DramInit;
2358 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2361 for(i = 0; i < controllers; i++) {
2363 print_debug("Initializing memory: ");
2366 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2368 if ((loops & 1023) == 0) {
2371 } while(((dcl & DCL_DramInit) != 0) && (loops < TIMEOUT_LOOPS));
2372 if (loops >= TIMEOUT_LOOPS) {
2373 print_debug(" failed\r\n");
2375 print_debug(" done\r\n");
2377 if (dcl & DCL_DimmEccEn) {
2378 print_debug("Clearing memory: ");
2379 if (!is_cpu_pre_c0()) {
2380 /* Wait until the automatic ram scrubber is finished */
2381 dcl &= ~(DCL_MemClrStatus | DCL_DramEnable);
2382 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2384 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2385 } while(((dcl & DCL_MemClrStatus) == 0) || ((dcl & DCL_DramEnable) == 0) );
2387 uint32_t base, last_scrub_k, scrub_k;
2388 uint32_t cnt,zstart,zend;
2391 /* First make certain the scrubber is disabled */
2392 pci_write_config32(ctrl[i].f3, SCRUB_CONTROL,
2393 (SCRUB_NONE << 16) | (SCRUB_NONE << 8) | (SCRUB_NONE << 0));
2395 /* load the start and end for the memory block to clear */
2396 msr_201 = rdmsr(0x201);
2397 zstart = pci_read_config32(ctrl[0].f1, 0x40 + (i*8));
2398 zend = pci_read_config32(ctrl[0].f1, 0x44 + (i*8));
2402 print_debug("addr ");
2403 print_debug_hex32(zstart);
2405 print_debug_hex32(zend);
2406 print_debug("\r\n");
2409 /* Disable fixed mtrrs */
2410 msr = rdmsr(MTRRdefType_MSR);
2412 wrmsr(MTRRdefType_MSR, msr);
2414 /* turn on the wrap 32 disable */
2415 msr = rdmsr(0xc0010015);
2417 wrmsr(0xc0010015,msr);
2419 for(;zstart<zend;zstart+=4) {
2421 /* test for the last 64 meg of 4 gig space */
2427 "movl %%cr0, %0\n\t"
2428 "orl $0x40000000, %0\n\t"
2429 "movl %0, %%cr0\n\t"
2433 /* Set the variable mtrrs to write combine */
2434 msr.lo = 1 + ((zstart&0x0ff)<<24);
2435 msr.hi = (zstart&0x0ff00)>>8;
2438 /* Set the limit to 64 meg of ram */
2439 msr.hi = 0x000000ff;
2440 msr.lo = 0xfc000800;
2445 "movl %%cr0, %0\n\t"
2446 "andl $0x9fffffff, %0\n\t"
2447 "movl %0, %%cr0\n\t"
2450 /* Set fs base address */
2451 msr.lo = (zstart&0xff) << 24;
2452 msr.hi = (zstart&0xff00) >> 8;
2453 wrmsr(0xc0000100,msr);
2455 print_debug_char((zstart > 0x0ff)?'+':'-');
2457 /* clear memory 64meg */
2460 "movl %0, %%fs:(%1)\n\t"
2465 : "a" (0), "D" (0), "c" (0x01000000)
2471 "movl %%cr0, %0\n\t"
2472 "orl $0x40000000, %0\n\t"
2473 "movl %0, %%cr0\n\t"
2477 /* restore msr registers */
2478 msr = rdmsr(MTRRdefType_MSR);
2480 wrmsr(MTRRdefType_MSR, msr);
2482 /* Restore the variable mtrrs */
2486 wrmsr(0x201,msr_201);
2488 /* Set fs base to 0 */
2491 wrmsr(0xc0000100,msr);
2495 "movl %%cr0, %0\n\t"
2496 "andl $0x9fffffff, %0\n\t"
2497 "movl %0, %%cr0\n\t"
2501 /* turn off the wrap 32 disable */
2502 msr = rdmsr(0xc0010015);
2504 wrmsr(0xc0010015,msr);
2506 /* Find the Srub base address for this cpu */
2507 base = pci_read_config32(ctrl[i].f1, 0x40 + (ctrl[i].node_id << 3));
2510 /* Set the scrub base address registers */
2511 pci_write_config32(ctrl[i].f3, SCRUB_ADDR_LOW, base << 8);
2512 pci_write_config32(ctrl[i].f3, SCRUB_ADDR_HIGH, base >> 24);
2514 /* Enable scrubbing at the lowest possible rate */
2515 pci_write_config32(ctrl[i].f3, SCRUB_CONTROL,
2516 (SCRUB_84ms << 16) | (SCRUB_84ms << 8) | (SCRUB_84ms << 0));
2518 print_debug("done\r\n");