Applying YhLu's patch from issue 37.
[coreboot.git] / src / northbridge / amd / amdk8 / raminit.c
1 /*      This should be done by Eric
2         2004.11 yhlu add 4 rank DIMM support
3         2004.12 yhlu add D0 support
4         2005.02 yhlu add E0 memory hole support
5 */
6
7 #include <cpu/x86/mem.h>
8 #include <cpu/x86/cache.h>
9 #include <cpu/x86/mtrr.h>
10 #include "raminit.h"
11 #include "amdk8.h"
12
13 #if (CONFIG_LB_MEM_TOPK & (CONFIG_LB_MEM_TOPK -1)) != 0
14 # error "CONFIG_LB_MEM_TOPK must be a power of 2"
15 #endif
16
17 #ifndef K8_4RANK_DIMM_SUPPORT
18 #define K8_4RANK_DIMM_SUPPORT 0
19 #endif
20
21 #if 1
22 static void setup_resource_map(const unsigned int *register_values, int max)
23 {
24         int i;
25 //      print_debug("setting up resource map....");
26 #if 0
27         print_debug("\r\n");
28 #endif
29         for(i = 0; i < max; i += 3) {
30                 device_t dev;
31                 unsigned where;
32                 unsigned long reg;
33 #if 0
34         #if CONFIG_USE_INIT
35                 prink_debug("%08x <- %08x\r\n", register_values[i], register_values[i+2]);
36         #else
37                 print_debug_hex32(register_values[i]);
38                 print_debug(" <-");
39                 print_debug_hex32(register_values[i+2]);
40                 print_debug("\r\n");
41         #endif
42 #endif
43                 dev = register_values[i] & ~0xff;
44                 where = register_values[i] & 0xff;
45                 reg = pci_read_config32(dev, where);
46                 reg &= register_values[i+1];
47                 reg |= register_values[i+2];
48                 pci_write_config32(dev, where, reg);
49 #if 0
50                 reg = pci_read_config32(register_values[i]);
51                 reg &= register_values[i+1];
52                 reg |= register_values[i+2] & ~register_values[i+1];
53                 pci_write_config32(register_values[i], reg);
54 #endif
55         }
56 //      print_debug("done.\r\n");
57 }
58 #endif
59
60 static int controller_present(const struct mem_controller *ctrl)
61 {
62         return pci_read_config32(ctrl->f0, 0) == 0x11001022;
63 }
64
65 static void sdram_set_registers(const struct mem_controller *ctrl)
66 {
67         static const unsigned int register_values[] = {
68
69         /* Careful set limit registers before base registers which contain the enables */
70         /* DRAM Limit i Registers
71          * F1:0x44 i = 0
72          * F1:0x4C i = 1
73          * F1:0x54 i = 2
74          * F1:0x5C i = 3
75          * F1:0x64 i = 4
76          * F1:0x6C i = 5
77          * F1:0x74 i = 6
78          * F1:0x7C i = 7
79          * [ 2: 0] Destination Node ID
80          *         000 = Node 0
81          *         001 = Node 1
82          *         010 = Node 2
83          *         011 = Node 3
84          *         100 = Node 4
85          *         101 = Node 5
86          *         110 = Node 6
87          *         111 = Node 7
88          * [ 7: 3] Reserved
89          * [10: 8] Interleave select
90          *         specifies the values of A[14:12] to use with interleave enable.
91          * [15:11] Reserved
92          * [31:16] DRAM Limit Address i Bits 39-24
93          *         This field defines the upper address bits of a 40 bit  address
94          *         that define the end of the DRAM region.
95          */
96         PCI_ADDR(0, 0x18, 1, 0x44), 0x0000f8f8, 0x00000000,
97         PCI_ADDR(0, 0x18, 1, 0x4C), 0x0000f8f8, 0x00000001,
98         PCI_ADDR(0, 0x18, 1, 0x54), 0x0000f8f8, 0x00000002,
99         PCI_ADDR(0, 0x18, 1, 0x5C), 0x0000f8f8, 0x00000003,
100         PCI_ADDR(0, 0x18, 1, 0x64), 0x0000f8f8, 0x00000004,
101         PCI_ADDR(0, 0x18, 1, 0x6C), 0x0000f8f8, 0x00000005,
102         PCI_ADDR(0, 0x18, 1, 0x74), 0x0000f8f8, 0x00000006,
103         PCI_ADDR(0, 0x18, 1, 0x7C), 0x0000f8f8, 0x00000007,
104         /* DRAM Base i Registers
105          * F1:0x40 i = 0
106          * F1:0x48 i = 1
107          * F1:0x50 i = 2
108          * F1:0x58 i = 3
109          * F1:0x60 i = 4
110          * F1:0x68 i = 5
111          * F1:0x70 i = 6
112          * F1:0x78 i = 7
113          * [ 0: 0] Read Enable
114          *         0 = Reads Disabled
115          *         1 = Reads Enabled
116          * [ 1: 1] Write Enable
117          *         0 = Writes Disabled
118          *         1 = Writes Enabled
119          * [ 7: 2] Reserved
120          * [10: 8] Interleave Enable
121          *         000 = No interleave
122          *         001 = Interleave on A[12] (2 nodes)
123          *         010 = reserved
124          *         011 = Interleave on A[12] and A[14] (4 nodes)
125          *         100 = reserved
126          *         101 = reserved
127          *         110 = reserved
128          *         111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
129          * [15:11] Reserved
130          * [13:16] DRAM Base Address i Bits 39-24
131          *         This field defines the upper address bits of a 40-bit address
132          *         that define the start of the DRAM region.
133          */
134         PCI_ADDR(0, 0x18, 1, 0x40), 0x0000f8fc, 0x00000000,
135         PCI_ADDR(0, 0x18, 1, 0x48), 0x0000f8fc, 0x00000000,
136         PCI_ADDR(0, 0x18, 1, 0x50), 0x0000f8fc, 0x00000000,
137         PCI_ADDR(0, 0x18, 1, 0x58), 0x0000f8fc, 0x00000000,
138         PCI_ADDR(0, 0x18, 1, 0x60), 0x0000f8fc, 0x00000000,
139         PCI_ADDR(0, 0x18, 1, 0x68), 0x0000f8fc, 0x00000000,
140         PCI_ADDR(0, 0x18, 1, 0x70), 0x0000f8fc, 0x00000000,
141         PCI_ADDR(0, 0x18, 1, 0x78), 0x0000f8fc, 0x00000000,
142
143         /* DRAM CS Base Address i Registers
144          * F2:0x40 i = 0
145          * F2:0x44 i = 1
146          * F2:0x48 i = 2
147          * F2:0x4C i = 3
148          * F2:0x50 i = 4
149          * F2:0x54 i = 5
150          * F2:0x58 i = 6
151          * F2:0x5C i = 7
152          * [ 0: 0] Chip-Select Bank Enable
153          *         0 = Bank Disabled
154          *         1 = Bank Enabled
155          * [ 8: 1] Reserved
156          * [15: 9] Base Address (19-13)
157          *         An optimization used when all DIMM are the same size...
158          * [20:16] Reserved
159          * [31:21] Base Address (35-25)
160          *         This field defines the top 11 addresses bit of a 40-bit
161          *         address that define the memory address space.  These
162          *         bits decode 32-MByte blocks of memory.
163          */
164         PCI_ADDR(0, 0x18, 2, 0x40), 0x001f01fe, 0x00000000,
165         PCI_ADDR(0, 0x18, 2, 0x44), 0x001f01fe, 0x00000000,
166         PCI_ADDR(0, 0x18, 2, 0x48), 0x001f01fe, 0x00000000,
167         PCI_ADDR(0, 0x18, 2, 0x4C), 0x001f01fe, 0x00000000,
168         PCI_ADDR(0, 0x18, 2, 0x50), 0x001f01fe, 0x00000000,
169         PCI_ADDR(0, 0x18, 2, 0x54), 0x001f01fe, 0x00000000,
170         PCI_ADDR(0, 0x18, 2, 0x58), 0x001f01fe, 0x00000000,
171         PCI_ADDR(0, 0x18, 2, 0x5C), 0x001f01fe, 0x00000000,
172         /* DRAM CS Mask Address i Registers
173          * F2:0x60 i = 0
174          * F2:0x64 i = 1
175          * F2:0x68 i = 2
176          * F2:0x6C i = 3
177          * F2:0x70 i = 4
178          * F2:0x74 i = 5
179          * F2:0x78 i = 6
180          * F2:0x7C i = 7
181          * Select bits to exclude from comparison with the DRAM Base address register.
182          * [ 8: 0] Reserved
183          * [15: 9] Address Mask (19-13)
184          *         Address to be excluded from the optimized case
185          * [20:16] Reserved
186          * [29:21] Address Mask (33-25)
187          *         The bits with an address mask of 1 are excluded from address comparison
188          * [31:30] Reserved
189          * 
190          */
191         PCI_ADDR(0, 0x18, 2, 0x60), 0xC01f01ff, 0x00000000,
192         PCI_ADDR(0, 0x18, 2, 0x64), 0xC01f01ff, 0x00000000,
193         PCI_ADDR(0, 0x18, 2, 0x68), 0xC01f01ff, 0x00000000,
194         PCI_ADDR(0, 0x18, 2, 0x6C), 0xC01f01ff, 0x00000000,
195         PCI_ADDR(0, 0x18, 2, 0x70), 0xC01f01ff, 0x00000000,
196         PCI_ADDR(0, 0x18, 2, 0x74), 0xC01f01ff, 0x00000000,
197         PCI_ADDR(0, 0x18, 2, 0x78), 0xC01f01ff, 0x00000000,
198         PCI_ADDR(0, 0x18, 2, 0x7C), 0xC01f01ff, 0x00000000,
199         /* DRAM Bank Address Mapping Register
200          * F2:0x80
201          * Specify the memory module size
202          * [ 2: 0] CS1/0 
203          * [ 6: 4] CS3/2
204          * [10: 8] CS5/4
205          * [14:12] CS7/6
206          *         000 = 32Mbyte  (Rows = 12 & Col =  8)
207          *         001 = 64Mbyte  (Rows = 12 & Col =  9)
208          *         010 = 128Mbyte (Rows = 13 & Col =  9)|(Rows = 12 & Col = 10)
209          *         011 = 256Mbyte (Rows = 13 & Col = 10)|(Rows = 12 & Col = 11)
210          *         100 = 512Mbyte (Rows = 13 & Col = 11)|(Rows = 14 & Col = 10)
211          *         101 = 1Gbyte   (Rows = 14 & Col = 11)|(Rows = 13 & Col = 12)
212          *         110 = 2Gbyte   (Rows = 14 & Col = 12)
213          *         111 = reserved 
214          * [ 3: 3] Reserved
215          * [ 7: 7] Reserved
216          * [11:11] Reserved
217          * [31:15]
218          */
219         PCI_ADDR(0, 0x18, 2, 0x80), 0xffff8888, 0x00000000,
220         /* DRAM Timing Low Register
221          * F2:0x88
222          * [ 2: 0] Tcl (Cas# Latency, Cas# to read-data-valid)
223          *         000 = reserved
224          *         001 = CL 2
225          *         010 = CL 3
226          *         011 = reserved
227          *         100 = reserved
228          *         101 = CL 2.5
229          *         110 = reserved
230          *         111 = reserved
231          * [ 3: 3] Reserved
232          * [ 7: 4] Trc (Row Cycle Time, Ras#-active to Ras#-active/bank auto refresh)
233          *         0000 =  7 bus clocks
234          *         0001 =  8 bus clocks
235          *         ...
236          *         1110 = 21 bus clocks
237          *         1111 = 22 bus clocks
238          * [11: 8] Trfc (Row refresh Cycle time, Auto-refresh-active to RAS#-active or RAS#auto-refresh)
239          *         0000 = 9 bus clocks
240          *         0010 = 10 bus clocks
241          *         ....
242          *         1110 = 23 bus clocks
243          *         1111 = 24 bus clocks
244          * [14:12] Trcd (Ras#-active to Case#-read/write Delay)
245          *         000 = reserved
246          *         001 = reserved
247          *         010 = 2 bus clocks
248          *         011 = 3 bus clocks
249          *         100 = 4 bus clocks
250          *         101 = 5 bus clocks
251          *         110 = 6 bus clocks
252          *         111 = reserved
253          * [15:15] Reserved
254          * [18:16] Trrd (Ras# to Ras# Delay)
255          *         000 = reserved
256          *         001 = reserved
257          *         010 = 2 bus clocks
258          *         011 = 3 bus clocks
259          *         100 = 4 bus clocks
260          *         101 = reserved
261          *         110 = reserved
262          *         111 = reserved
263          * [19:19] Reserved
264          * [23:20] Tras (Minmum Ras# Active Time)
265          *         0000 to 0100 = reserved
266          *         0101 = 5 bus clocks
267          *         ...
268          *         1111 = 15 bus clocks
269          * [26:24] Trp (Row Precharge Time)
270          *         000 = reserved
271          *         001 = reserved
272          *         010 = 2 bus clocks
273          *         011 = 3 bus clocks
274          *         100 = 4 bus clocks
275          *         101 = 5 bus clocks
276          *         110 = 6 bus clocks
277          *         111 = reserved
278          * [27:27] Reserved
279          * [28:28] Twr (Write Recovery Time)
280          *         0 = 2 bus clocks
281          *         1 = 3 bus clocks
282          * [31:29] Reserved
283          */
284         PCI_ADDR(0, 0x18, 2, 0x88), 0xe8088008, 0x02522001 /* 0x03623125 */ ,
285         /* DRAM Timing High Register
286          * F2:0x8C
287          * [ 0: 0] Twtr (Write to Read Delay)
288          *         0 = 1 bus Clocks
289          *         1 = 2 bus Clocks
290          * [ 3: 1] Reserved
291          * [ 6: 4] Trwt (Read to Write Delay)
292          *         000 = 1 bus clocks
293          *         001 = 2 bus clocks
294          *         010 = 3 bus clocks
295          *         011 = 4 bus clocks
296          *         100 = 5 bus clocks
297          *         101 = 6 bus clocks
298          *         110 = reserved
299          *         111 = reserved
300          * [ 7: 7] Reserved
301          * [12: 8] Tref (Refresh Rate)
302          *         00000 = 100Mhz 4K rows
303          *         00001 = 133Mhz 4K rows
304          *         00010 = 166Mhz 4K rows
305          *         00011 = 200Mhz 4K rows
306          *         01000 = 100Mhz 8K/16K rows
307          *         01001 = 133Mhz 8K/16K rows
308          *         01010 = 166Mhz 8K/16K rows
309          *         01011 = 200Mhz 8K/16K rows
310          * [19:13] Reserved
311          * [22:20] Twcl (Write CAS Latency)
312          *         000 = 1 Mem clock after CAS# (Unbuffered Dimms)
313          *         001 = 2 Mem clocks after CAS# (Registered Dimms)
314          * [31:23] Reserved
315          */
316         PCI_ADDR(0, 0x18, 2, 0x8c), 0xff8fe08e, (0 << 20)|(0 << 8)|(0 << 4)|(0 << 0),
317         /* DRAM Config Low Register
318          * F2:0x90
319          * [ 0: 0] DLL Disable
320          *         0 = Enabled
321          *         1 = Disabled
322          * [ 1: 1] D_DRV
323          *         0 = Normal Drive
324          *         1 = Weak Drive
325          * [ 2: 2] QFC_EN
326          *         0 = Disabled
327          *         1 = Enabled
328          * [ 3: 3] Disable DQS Hystersis  (FIXME handle this one carefully)
329          *         0 = Enable DQS input filter 
330          *         1 = Disable DQS input filtering 
331          * [ 7: 4] Reserved
332          * [ 8: 8] DRAM_Init
333          *         0 = Initialization done or not yet started.
334          *         1 = Initiate DRAM intialization sequence
335          * [ 9: 9] SO-Dimm Enable
336          *         0 = Do nothing
337          *         1 = SO-Dimms present
338          * [10:10] DramEnable
339          *         0 = DRAM not enabled
340          *         1 = DRAM initialized and enabled
341          * [11:11] Memory Clear Status
342          *         0 = Memory Clear function has not completed
343          *         1 = Memory Clear function has completed
344          * [12:12] Exit Self-Refresh
345          *         0 = Exit from self-refresh done or not yet started
346          *         1 = DRAM exiting from self refresh
347          * [13:13] Self-Refresh Status
348          *         0 = Normal Operation
349          *         1 = Self-refresh mode active
350          * [15:14] Read/Write Queue Bypass Count
351          *         00 = 2
352          *         01 = 4
353          *         10 = 8
354          *         11 = 16
355          * [16:16] 128-bit/64-Bit
356          *         0 = 64bit Interface to DRAM
357          *         1 = 128bit Interface to DRAM
358          * [17:17] DIMM ECC Enable
359          *         0 = Some DIMMs do not have ECC
360          *         1 = ALL DIMMS have ECC bits
361          * [18:18] UnBuffered DIMMs
362          *         0 = Buffered DIMMS
363          *         1 = Unbuffered DIMMS
364          * [19:19] Enable 32-Byte Granularity
365          *         0 = Optimize for 64byte bursts
366          *         1 = Optimize for 32byte bursts
367          * [20:20] DIMM 0 is x4
368          * [21:21] DIMM 1 is x4
369          * [22:22] DIMM 2 is x4
370          * [23:23] DIMM 3 is x4
371          *         0 = DIMM is not x4
372          *         1 = x4 DIMM present
373          * [24:24] Disable DRAM Receivers
374          *         0 = Receivers enabled
375          *         1 = Receivers disabled
376          * [27:25] Bypass Max
377          *         000 = Arbiters chois is always respected
378          *         001 = Oldest entry in DCQ can be bypassed 1 time
379          *         010 = Oldest entry in DCQ can be bypassed 2 times
380          *         011 = Oldest entry in DCQ can be bypassed 3 times
381          *         100 = Oldest entry in DCQ can be bypassed 4 times
382          *         101 = Oldest entry in DCQ can be bypassed 5 times
383          *         110 = Oldest entry in DCQ can be bypassed 6 times
384          *         111 = Oldest entry in DCQ can be bypassed 7 times
385          * [31:28] Reserved
386          */
387         PCI_ADDR(0, 0x18, 2, 0x90), 0xf0000000, 
388         (4 << 25)|(0 << 24)| 
389         (0 << 23)|(0 << 22)|(0 << 21)|(0 << 20)| 
390         (1 << 19)|(0 << 18)|(1 << 17)|(0 << 16)| 
391         (2 << 14)|(0 << 13)|(0 << 12)| 
392         (0 << 11)|(0 << 10)|(0 << 9)|(0 << 8)| 
393         (0 << 3) |(0 << 1) |(0 << 0),
394         /* DRAM Config High Register
395          * F2:0x94
396          * [ 0: 3] Maximum Asynchronous Latency
397          *         0000 = 0 ns
398          *         ...
399          *         1111 = 15 ns
400          * [ 7: 4] Reserved
401          * [11: 8] Read Preamble
402          *         0000 = 2.0 ns
403          *         0001 = 2.5 ns
404          *         0010 = 3.0 ns
405          *         0011 = 3.5 ns
406          *         0100 = 4.0 ns
407          *         0101 = 4.5 ns
408          *         0110 = 5.0 ns
409          *         0111 = 5.5 ns
410          *         1000 = 6.0 ns
411          *         1001 = 6.5 ns
412          *         1010 = 7.0 ns
413          *         1011 = 7.5 ns
414          *         1100 = 8.0 ns
415          *         1101 = 8.5 ns
416          *         1110 = 9.0 ns
417          *         1111 = 9.5 ns
418          * [15:12] Reserved
419          * [18:16] Idle Cycle Limit
420          *         000 = 0 cycles
421          *         001 = 4 cycles
422          *         010 = 8 cycles
423          *         011 = 16 cycles
424          *         100 = 32 cycles
425          *         101 = 64 cycles
426          *         110 = 128 cycles
427          *         111 = 256 cycles
428          * [19:19] Dynamic Idle Cycle Center Enable
429          *         0 = Use Idle Cycle Limit
430          *         1 = Generate a dynamic Idle cycle limit
431          * [22:20] DRAM MEMCLK Frequency
432          *         000 = 100Mhz
433          *         001 = reserved
434          *         010 = 133Mhz
435          *         011 = reserved
436          *         100 = reserved
437          *         101 = 166Mhz
438          *         110 = reserved
439          *         111 = reserved
440          * [24:23] Reserved
441          * [25:25] Memory Clock Ratio Valid (FIXME carefully enable memclk)
442          *         0 = Disable MemClks
443          *         1 = Enable MemClks
444          * [26:26] Memory Clock 0 Enable
445          *         0 = Disabled
446          *         1 = Enabled
447          * [27:27] Memory Clock 1 Enable
448          *         0 = Disabled
449          *         1 = Enabled
450          * [28:28] Memory Clock 2 Enable
451          *         0 = Disabled
452          *         1 = Enabled
453          * [29:29] Memory Clock 3 Enable
454          *         0 = Disabled
455          *         1 = Enabled
456          * [31:30] Reserved
457          */
458         PCI_ADDR(0, 0x18, 2, 0x94), 0xc180f0f0,
459         (0 << 29)|(0 << 28)|(0 << 27)|(0 << 26)|(0 << 25)|
460         (0 << 20)|(0 << 19)|(DCH_IDLE_LIMIT_16 << 16)|(0 << 8)|(0 << 0),
461         /* DRAM Delay Line Register
462          * F2:0x98
463          * Adjust the skew of the input DQS strobe relative to DATA
464          * [15: 0] Reserved
465          * [23:16] Delay Line Adjust
466          *         Adjusts the DLL derived PDL delay by one or more delay stages
467          *         in either the faster or slower direction.
468          * [24:24} Adjust Slower
469          *         0 = Do Nothing
470          *         1 = Adj is used to increase the PDL delay
471          * [25:25] Adjust Faster
472          *         0 = Do Nothing
473          *         1 = Adj is used to decrease the PDL delay
474          * [31:26] Reserved
475          */
476         PCI_ADDR(0, 0x18, 2, 0x98), 0xfc00ffff, 0x00000000,
477         /* DRAM Scrub Control Register
478          * F3:0x58
479          * [ 4: 0] DRAM Scrube Rate
480          * [ 7: 5] reserved
481          * [12: 8] L2 Scrub Rate
482          * [15:13] reserved
483          * [20:16] Dcache Scrub
484          * [31:21] reserved
485          *         Scrub Rates
486          *         00000 = Do not scrub
487          *         00001 =  40.00 ns
488          *         00010 =  80.00 ns
489          *         00011 = 160.00 ns
490          *         00100 = 320.00 ns
491          *         00101 = 640.00 ns
492          *         00110 =   1.28 us
493          *         00111 =   2.56 us
494          *         01000 =   5.12 us
495          *         01001 =  10.20 us
496          *         01011 =  41.00 us
497          *         01100 =  81.90 us
498          *         01101 = 163.80 us
499          *         01110 = 327.70 us
500          *         01111 = 655.40 us
501          *         10000 =   1.31 ms
502          *         10001 =   2.62 ms
503          *         10010 =   5.24 ms
504          *         10011 =  10.49 ms
505          *         10100 =  20.97 ms
506          *         10101 =  42.00 ms
507          *         10110 =  84.00 ms
508          *         All Others = Reserved
509          */
510         PCI_ADDR(0, 0x18, 3, 0x58), 0xffe0e0e0, 0x00000000,
511         /* DRAM Scrub Address Low Register
512          * F3:0x5C
513          * [ 0: 0] DRAM Scrubber Redirect Enable
514          *         0 = Do nothing
515          *         1 = Scrubber Corrects errors found in normal operation
516          * [ 5: 1] Reserved
517          * [31: 6] DRAM Scrub Address 31-6
518          */
519         PCI_ADDR(0, 0x18, 3, 0x5C), 0x0000003e, 0x00000000,
520         /* DRAM Scrub Address High Register
521          * F3:0x60
522          * [ 7: 0] DRAM Scrubb Address 39-32
523          * [31: 8] Reserved
524          */
525         PCI_ADDR(0, 0x18, 3, 0x60), 0xffffff00, 0x00000000,
526         };
527         int i;
528         int max;
529
530 #if 1
531         if (!controller_present(ctrl)) {
532 //                print_debug("No memory controller present\r\n");
533                 return;
534         }
535 #endif
536         print_spew("setting up CPU");
537         print_spew_hex8(ctrl->node_id);
538         print_spew(" northbridge registers\r\n");
539         max = sizeof(register_values)/sizeof(register_values[0]);
540         for(i = 0; i < max; i += 3) {
541                 device_t dev;
542                 unsigned where;
543                 unsigned long reg;
544 #if 0
545         #if CONFIG_USE_INIT
546                 prink_debug("%08x <- %08x\r\n", register_values[i], register_values[i+2]);
547         #else
548                 print_spew_hex32(register_values[i]);
549                 print_spew(" <-");
550                 print_spew_hex32(register_values[i+2]);
551                 print_spew("\r\n");
552         #endif
553 #endif
554                 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x18, 0) + ctrl->f0;
555                 where = register_values[i] & 0xff;
556                 reg = pci_read_config32(dev, where);
557                 reg &= register_values[i+1];
558                 reg |= register_values[i+2];
559                 pci_write_config32(dev, where, reg);
560 #if 0
561
562                 reg = pci_read_config32(register_values[i]);
563                 reg &= register_values[i+1];
564                 reg |= register_values[i+2];
565                 pci_write_config32(register_values[i], reg);
566 #endif
567         }
568         print_spew("done.\r\n");
569 }
570
571
572 static void hw_enable_ecc(const struct mem_controller *ctrl)
573 {
574         uint32_t dcl, nbcap;
575         nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
576         dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
577         dcl &= ~DCL_DimmEccEn;
578         if (nbcap & NBCAP_ECC) {
579                 dcl |= DCL_DimmEccEn;
580         }
581         if (read_option(CMOS_VSTART_ECC_memory, CMOS_VLEN_ECC_memory, 1) == 0) {
582                 dcl &= ~DCL_DimmEccEn;
583         }
584         pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
585         
586 }
587
588 static int is_dual_channel(const struct mem_controller *ctrl)
589 {
590         uint32_t dcl;
591         dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
592         return dcl & DCL_128BitEn;
593 }
594
595 static int is_opteron(const struct mem_controller *ctrl)
596 {
597         /* Test to see if I am an Opteron.  
598          * FIXME Testing dual channel capability is correct for now
599          * but a beter test is probably required.
600          */
601 #warning "FIXME implement a better test for opterons"
602         uint32_t nbcap;
603         nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
604         return !!(nbcap & NBCAP_128Bit);
605 }
606
607 static int is_registered(const struct mem_controller *ctrl)
608 {
609         /* Test to see if we are dealing with registered SDRAM.
610          * If we are not registered we are unbuffered.
611          * This function must be called after spd_handle_unbuffered_dimms.
612          */
613         uint32_t dcl;
614         dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
615         return !(dcl & DCL_UnBufDimm);
616 }
617
618 struct dimm_size {
619         unsigned long side1;
620         unsigned long side2;
621         unsigned long rows;
622         unsigned long col;
623 #if K8_4RANK_DIMM_SUPPORT == 1
624         unsigned long rank;
625 #endif
626 };
627
628 static struct dimm_size spd_get_dimm_size(unsigned device)
629 {
630         /* Calculate the log base 2 size of a DIMM in bits */
631         struct dimm_size sz;
632         int value, low;
633         sz.side1 = 0;
634         sz.side2 = 0;
635         sz.rows = 0;
636         sz.col = 0;
637 #if K8_4RANK_DIMM_SUPPORT == 1
638         sz.rank = 0;
639 #endif
640
641         /* Note it might be easier to use byte 31 here, it has the DIMM size as
642          * a multiple of 4MB.  The way we do it now we can size both
643          * sides of an assymetric dimm.
644          */
645         value = spd_read_byte(device, 3);       /* rows */
646         if (value < 0) goto hw_err;
647         if ((value & 0xf) == 0) goto val_err;
648         sz.side1 += value & 0xf;
649         sz.rows = value & 0xf;
650
651         value = spd_read_byte(device, 4);       /* columns */
652         if (value < 0) goto hw_err;
653         if ((value & 0xf) == 0) goto val_err;
654         sz.side1 += value & 0xf;
655         sz.col = value & 0xf;
656
657         value = spd_read_byte(device, 17);      /* banks */
658         if (value < 0) goto hw_err;
659         if ((value & 0xff) == 0) goto val_err;
660         sz.side1 += log2(value & 0xff);
661
662         /* Get the module data width and convert it to a power of two */
663         value = spd_read_byte(device, 7);       /* (high byte) */
664         if (value < 0) goto hw_err;
665         value &= 0xff;
666         value <<= 8;
667         
668         low = spd_read_byte(device, 6); /* (low byte) */
669         if (low < 0) goto hw_err;
670         value = value | (low & 0xff);
671         if ((value != 72) && (value != 64)) goto val_err;
672         sz.side1 += log2(value);
673
674         /* side 2 */
675         value = spd_read_byte(device, 5);       /* number of physical banks */
676         if (value < 0) goto hw_err;
677         if (value == 1) goto out;
678         if ((value != 2) && (value != 4 )) {
679                 goto val_err;
680         }
681 #if K8_4RANK_DIMM_SUPPORT == 1
682         sz.rank = value;
683 #endif
684
685         /* Start with the symmetrical case */
686         sz.side2 = sz.side1;
687
688         value = spd_read_byte(device, 3);       /* rows */
689         if (value < 0) goto hw_err;
690         if ((value & 0xf0) == 0) goto out;      /* If symmetrical we are done */
691         sz.side2 -= (value & 0x0f);             /* Subtract out rows on side 1 */
692         sz.side2 += ((value >> 4) & 0x0f);      /* Add in rows on side 2 */
693
694         value = spd_read_byte(device, 4);       /* columns */
695         if (value < 0) goto hw_err;
696         if ((value & 0xff) == 0) goto val_err;
697         sz.side2 -= (value & 0x0f);             /* Subtract out columns on side 1 */
698         sz.side2 += ((value >> 4) & 0x0f);      /* Add in columsn on side 2 */
699
700         goto out;
701
702  val_err:
703         die("Bad SPD value\r\n");
704         /* If an hw_error occurs report that I have no memory */
705 hw_err:
706         sz.side1 = 0;
707         sz.side2 = 0;
708         sz.rows = 0;
709         sz.col = 0;
710 #if K8_4RANK_DIMM_SUPPORT == 1
711         sz.rank = 0;
712 #endif
713  out:
714         return sz;
715 }
716
717
718 static void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz, unsigned index)
719 {
720         uint32_t base0, base1;
721         uint32_t dch;
722
723         if (sz.side1 != sz.side2) {
724                 sz.side2 = 0;
725         }
726         
727         /* For each base register.
728          * Place the dimm size in 32 MB quantities in the bits 31 - 21.
729          * The initialize dimm size is in bits.
730          * Set the base enable bit0.
731          */
732         
733         base0 = base1 = 0;
734
735         /* Make certain side1 of the dimm is at least 32MB */
736         if (sz.side1 >= (25 +3)) {
737                 base0 = (1 << ((sz.side1 - (25 + 3)) + 21)) | 1;
738         }
739         
740         /* Make certain side2 of the dimm is at least 32MB */
741         if (sz.side2 >= (25 + 3)) {
742                 base1 = (1 << ((sz.side2 - (25 + 3)) + 21)) | 1;
743         }
744
745         /* Double the size if we are using dual channel memory */
746         if (is_dual_channel(ctrl)) {
747                 base0 = (base0 << 1) | (base0 & 1);
748                 base1 = (base1 << 1) | (base1 & 1);
749         }
750
751         /* Clear the reserved bits */
752         base0 &= ~0x001ffffe;
753         base1 &= ~0x001ffffe;
754
755         /* Set the appropriate DIMM base address register */
756         pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
757         pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
758 #if K8_4RANK_DIMM_SUPPORT == 1
759         if(sz.rank == 4) {
760                 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+4)<<2), base0);
761                 pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+5)<<2), base1);
762         }
763 #endif
764
765         /* Enable the memory clocks for this DIMM */
766         if (base0) {
767                 dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
768                 dch |= DCH_MEMCLK_EN0 << index;
769 #if K8_4RANK_DIMM_SUPPORT == 1
770                 if(sz.rank == 4) {
771                         dch |= DCH_MEMCLK_EN0 << (index + 2);
772                 }
773 #endif
774                 pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
775         }
776 }
777
778 static void set_dimm_map(const struct mem_controller *ctrl, struct dimm_size sz, unsigned index)
779 {
780         static const unsigned cs_map_aa[] = {
781                 /* (row=12, col=8)(14, 12) ---> (0, 0) (2, 4) */
782                 0, 1, 3, 6, 0,
783                 0, 2, 4, 7, 9,
784                 0, 0, 5, 8,10,
785         };
786
787         uint32_t map;
788         uint32_t dch;
789
790         map = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
791         map &= ~(0xf << (index * 4));
792 #if K8_4RANK_DIMM_SUPPORT == 1
793         if(sz.rank == 4) {
794                 map &= ~(0xf << ( (index + 2) * 4));
795         }
796 #endif
797
798
799         /* Make certain side1 of the dimm is at least 32MB */
800         if (sz.side1 >= (25 +3)) {
801                 if(is_cpu_pre_d0()) {
802                         map |= (sz.side1 - (25 + 3)) << (index *4);
803 #if K8_4RANK_DIMM_SUPPORT == 1
804                         if(sz.rank == 4) {
805                               map |= (sz.side1 - (25 + 3)) << ( (index + 2) * 4);
806                         }
807 #endif
808                 }
809                 else {
810                         map |= cs_map_aa[(sz.rows - 12) * 5 + (sz.col - 8) ] << (index*4);
811 #if K8_4RANK_DIMM_SUPPORT == 1
812                         if(sz.rank == 4) {
813                                map |=  cs_map_aa[(sz.rows - 12) * 5 + (sz.col - 8) ] << ( (index + 2) * 4);
814                         }
815 #endif
816                 }
817         }
818
819         pci_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
820         
821 }
822
823 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
824 {
825         int i;
826         
827         for(i = 0; i < DIMM_SOCKETS; i++) {
828                 struct dimm_size sz;
829                 if (!(dimm_mask & (1 << i))) {
830                         continue;
831                 }
832                 sz = spd_get_dimm_size(ctrl->channel0[i]);
833                 if (sz.side1 == 0) {
834                         return -1; /* Report SPD error */
835                 }
836                 set_dimm_size(ctrl, sz, i);
837                 set_dimm_map (ctrl, sz, i);
838         }
839         return dimm_mask;
840 }
841
842 static void route_dram_accesses(const struct mem_controller *ctrl,
843         unsigned long base_k, unsigned long limit_k)
844 {
845         /* Route the addresses to the controller node */
846         unsigned node_id;
847         unsigned limit;
848         unsigned base;
849         unsigned index;
850         unsigned limit_reg, base_reg;
851         device_t device;
852
853         node_id = ctrl->node_id;
854         index = (node_id << 3);
855         limit = (limit_k << 2);
856         limit &= 0xffff0000;
857         limit -= 0x00010000;
858         limit |= ( 0 << 8) | (node_id << 0);
859         base = (base_k << 2);
860         base &= 0xffff0000;
861         base |= (0 << 8) | (1<<1) | (1<<0);
862
863         limit_reg = 0x44 + index;
864         base_reg = 0x40 + index;
865         for(device = PCI_DEV(0, 0x18, 1); device <= PCI_DEV(0, 0x1f, 1); device += PCI_DEV(0, 1, 0)) {
866                 pci_write_config32(device, limit_reg, limit);
867                 pci_write_config32(device, base_reg, base);
868         }
869 }
870
871 static void set_top_mem(unsigned tom_k, unsigned hole_startk)
872 {
873         /* Error if I don't have memory */
874         if (!tom_k) {
875                 die("No memory?");
876         }
877
878         /* Report the amount of memory. */
879         print_spew("RAM: 0x");
880         print_spew_hex32(tom_k);
881         print_spew(" KB\r\n");
882
883         /* Now set top of memory */
884         msr_t msr;
885         msr.lo = (tom_k & 0x003fffff) << 10;
886         msr.hi = (tom_k & 0xffc00000) >> 22;
887         wrmsr(TOP_MEM2, msr);
888
889         /* Leave a 64M hole between TOP_MEM and TOP_MEM2
890          * so I can see my rom chip and other I/O devices.
891          */
892         if (tom_k >= 0x003f0000) {
893 #if K8_HW_MEM_HOLE_SIZEK != 0
894                 if(hole_startk != 0) {
895                         tom_k = hole_startk;
896                 } else
897 #endif
898                 tom_k = 0x3f0000;
899         }
900         msr.lo = (tom_k & 0x003fffff) << 10;
901         msr.hi = (tom_k & 0xffc00000) >> 22;
902         wrmsr(TOP_MEM, msr);
903 }
904
905 static unsigned long interleave_chip_selects(const struct mem_controller *ctrl)
906 {
907         /* 35 - 25 */
908         static const uint8_t csbase_low_shift[] = { 
909         /* 32MB */      (13 - 4),
910         /* 64MB */      (14 - 4),
911         /* 128MB */     (14 - 4), 
912         /* 256MB */     (15 - 4),
913         /* 512MB */     (15 - 4),
914         /* 1GB */       (16 - 4),
915         /* 2GB */       (16 - 4), 
916         };
917
918         static const uint8_t csbase_low_d0_shift[] = {
919         /* 32MB */      (13 - 4),
920         /* 64MB */      (14 - 4),
921         /* 128MB */     (14 - 4),
922         /* 128MB */     (15 - 4),
923         /* 256MB */     (15 - 4),
924         /* 512MB */     (15 - 4),
925         /* 256MB */     (16 - 4),
926         /* 512MB */     (16 - 4),
927         /* 1GB */       (16 - 4),
928         /* 1GB */       (17 - 4),
929         /* 2GB */       (17 - 4),
930         };
931
932         /* cs_base_high is not changed */
933
934         uint32_t csbase_inc;
935         int chip_selects, index;
936         int bits;
937         unsigned common_size;
938         unsigned common_cs_mode;
939         uint32_t csbase, csmask;
940
941         /* See if all of the memory chip selects are the same size
942          * and if so count them.
943          */
944         chip_selects = 0;
945         common_size = 0;
946         common_cs_mode = 0;
947         for(index = 0; index < 8; index++) {
948                 unsigned size;
949                 unsigned cs_mode;
950                 uint32_t value;
951                 
952                 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
953                 
954                 /* Is it enabled? */
955                 if (!(value & 1)) {
956                         continue;
957                 }
958                 chip_selects++;
959                 size = value >> 21;
960                 if (common_size == 0) {
961                         common_size = size;
962                 }
963                 /* The size differed fail */
964                 if (common_size != size) {
965                         return 0;
966                 }
967
968                 value = pci_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
969                 cs_mode =( value >> ((index>>1)*4)) & 0xf;
970                 if(cs_mode == 0 ) continue;
971                 if(common_cs_mode == 0) {
972                         common_cs_mode = cs_mode;
973                 }
974                 /* The size differed fail */
975                 if(common_cs_mode != cs_mode) {
976                         return 0;
977                 }
978         }
979
980         /* Chip selects can only be interleaved when there is
981          * more than one and their is a power of two of them.
982          */
983         bits = log2(chip_selects);
984         if (((1 << bits) != chip_selects) || (bits < 1) || (bits > 3)) {
985                 return 0;
986         }
987
988         /* Find the bits of csbase that we need to interleave on */
989         if(is_cpu_pre_d0()){
990                 csbase_inc = 1 << csbase_low_shift[common_cs_mode];
991                 if(is_dual_channel(ctrl)) {
992                 /* Also we run out of address mask bits if we try and interleave 8 4GB dimms */
993                         if ((bits == 3) && (common_size == (1 << (32 - 3)))) {
994 //                              print_debug("8 4GB chip selects cannot be interleaved\r\n");
995                                 return 0;
996                         }  
997                         csbase_inc <<=1;
998                 }
999         }
1000         else {
1001                 csbase_inc = 1 << csbase_low_d0_shift[common_cs_mode];
1002                 if(is_dual_channel(ctrl)) {
1003                         if( (bits==3) && (common_cs_mode > 8)) {
1004 //                              print_debug("8 cs_mode>8 chip selects cannot be interleaved\r\n");
1005                                 return 0;
1006                         }
1007                         csbase_inc <<=1;
1008                 }   
1009         }
1010
1011         /* Compute the initial values for csbase and csbask. 
1012          * In csbase just set the enable bit and the base to zero.
1013          * In csmask set the mask bits for the size and page level interleave.
1014          */
1015         csbase = 0 | 1;
1016         csmask = (((common_size  << bits) - 1) << 21);
1017         csmask |= 0xfe00 & ~((csbase_inc << bits) - csbase_inc);
1018         for(index = 0; index < 8; index++) {
1019                 uint32_t value;
1020
1021                 value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1022                 /* Is it enabled? */
1023                 if (!(value & 1)) {
1024                         continue;
1025                 }
1026                 pci_write_config32(ctrl->f2, DRAM_CSBASE + (index << 2), csbase);
1027                 pci_write_config32(ctrl->f2, DRAM_CSMASK + (index << 2), csmask);
1028                 csbase += csbase_inc;
1029         }
1030         
1031         print_spew("Interleaved\r\n");
1032
1033         /* Return the memory size in K */
1034         return common_size << (15 + bits);
1035 }
1036
1037 static unsigned long order_chip_selects(const struct mem_controller *ctrl)
1038 {
1039         unsigned long tom;
1040
1041         /* Remember which registers we have used in the high 8 bits of tom */
1042         tom = 0;
1043         for(;;) {
1044                 /* Find the largest remaining canidate */
1045                 unsigned index, canidate;
1046                 uint32_t csbase, csmask;
1047                 unsigned size;
1048                 csbase = 0;
1049                 canidate = 0;
1050                 for(index = 0; index < 8; index++) {
1051                         uint32_t value;
1052                         value = pci_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1053
1054                         /* Is it enabled? */
1055                         if (!(value & 1)) {
1056                                 continue;
1057                         }
1058                         
1059                         /* Is it greater? */
1060                         if (value <= csbase) {
1061                                 continue;
1062                         }
1063                         
1064                         /* Has it already been selected */
1065                         if (tom & (1 << (index + 24))) {
1066                                 continue;
1067                         }
1068                         /* I have a new canidate */
1069                         csbase = value;
1070                         canidate = index;
1071                 }
1072                 /* See if I have found a new canidate */
1073                 if (csbase == 0) {
1074                         break;
1075                 }
1076
1077                 /* Remember the dimm size */
1078                 size = csbase >> 21;
1079
1080                 /* Remember I have used this register */
1081                 tom |= (1 << (canidate + 24));
1082
1083                 /* Recompute the cs base register value */
1084                 csbase = (tom << 21) | 1;
1085
1086                 /* Increment the top of memory */
1087                 tom += size;
1088
1089                 /* Compute the memory mask */
1090                 csmask = ((size -1) << 21);
1091                 csmask |= 0xfe00;               /* For now don't optimize */
1092
1093                 /* Write the new base register */
1094                 pci_write_config32(ctrl->f2, DRAM_CSBASE + (canidate << 2), csbase);
1095                 /* Write the new mask register */
1096                 pci_write_config32(ctrl->f2, DRAM_CSMASK + (canidate << 2), csmask);
1097                 
1098         }
1099         /* Return the memory size in K */
1100         return (tom & ~0xff000000) << 15;
1101 }
1102
1103 unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
1104 {
1105         unsigned node_id;
1106         unsigned end_k;
1107         /* Find the last memory address used */
1108         end_k = 0;
1109         for(node_id = 0; node_id < max_node_id; node_id++) {
1110                 uint32_t limit, base;
1111                 unsigned index;
1112                 index = node_id << 3;
1113                 base = pci_read_config32(ctrl->f1, 0x40 + index);
1114                 /* Only look at the limit if the base is enabled */
1115                 if ((base & 3) == 3) {
1116                         limit = pci_read_config32(ctrl->f1, 0x44 + index);
1117                         end_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
1118                 }
1119         }
1120         return end_k;
1121 }
1122
1123 static void order_dimms(const struct mem_controller *ctrl)
1124 {
1125         unsigned long tom_k, base_k;
1126
1127         if (read_option(CMOS_VSTART_interleave_chip_selects, CMOS_VLEN_interleave_chip_selects, 1) != 0) {
1128                 tom_k = interleave_chip_selects(ctrl);
1129         } else {
1130                 print_debug("Interleaving disabled\r\n");
1131                 tom_k = 0;
1132         }
1133         if (!tom_k) {
1134                 tom_k = order_chip_selects(ctrl);
1135         }
1136         /* Compute the memory base address */
1137         base_k = memory_end_k(ctrl, ctrl->node_id);
1138         tom_k += base_k;
1139         route_dram_accesses(ctrl, base_k, tom_k);
1140         set_top_mem(tom_k, 0);
1141 }
1142
1143 static long disable_dimm(const struct mem_controller *ctrl, unsigned index, long dimm_mask)
1144 {
1145         print_debug("disabling dimm"); 
1146         print_debug_hex8(index); 
1147         print_debug("\r\n");
1148         pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
1149         pci_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
1150         dimm_mask &= ~(1 << index);
1151         return dimm_mask;
1152 }
1153
1154 static long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long dimm_mask)
1155 {
1156         int i;
1157         int registered;
1158         int unbuffered;
1159         uint32_t dcl;
1160         unbuffered = 0;
1161         registered = 0;
1162         for(i = 0; (i < DIMM_SOCKETS); i++) {
1163                 int value;
1164                 if (!(dimm_mask & (1 << i))) {
1165                         continue;
1166                 }
1167                 value = spd_read_byte(ctrl->channel0[i], 21);
1168                 if (value < 0) {
1169                         return -1;
1170                 }
1171                 /* Registered dimm ? */
1172                 if (value & (1 << 1)) {
1173                         registered = 1;
1174                 } 
1175                 /* Otherwise it must be an unbuffered dimm */
1176                 else {
1177                         unbuffered = 1;
1178                 }
1179         }
1180         if (unbuffered && registered) {
1181                 die("Mixed buffered and registered dimms not supported");
1182         }
1183 #if 1
1184         //By yhlu for debug Athlon64 939 can do dual channel, but it use unbuffer DIMM
1185         if (unbuffered && is_opteron(ctrl)) {
1186                 die("Unbuffered Dimms not supported on Opteron");
1187         }
1188 #endif
1189
1190         dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1191         dcl &= ~DCL_UnBufDimm;
1192         if (unbuffered) {
1193                 dcl |= DCL_UnBufDimm;
1194         }
1195         pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1196 #if 0
1197         if (is_registered(ctrl)) {
1198                 print_debug("Registered\r\n");
1199         } else {
1200                 print_debug("Unbuffered\r\n");
1201         }
1202 #endif
1203         return dimm_mask;
1204 }
1205
1206 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
1207 {
1208         unsigned dimm_mask;
1209         int i;
1210         dimm_mask = 0;
1211         for(i = 0; i < DIMM_SOCKETS; i++) {
1212                 int byte;
1213                 unsigned device;
1214                 device = ctrl->channel0[i];
1215                 if (device) {
1216                         byte = spd_read_byte(ctrl->channel0[i], 2);  /* Type */
1217                         if (byte == 7) {
1218                                 dimm_mask |= (1 << i);
1219                         }
1220                 }
1221                 device = ctrl->channel1[i];
1222                 if (device) {
1223                         byte = spd_read_byte(ctrl->channel1[i], 2);
1224                         if (byte == 7) {
1225                                 dimm_mask |= (1 << (i + DIMM_SOCKETS));
1226                         }
1227                 }
1228         }
1229         return dimm_mask;
1230 }
1231
1232 static long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_mask)
1233 {
1234         int i;
1235         uint32_t nbcap;
1236         /* SPD addresses to verify are identical */
1237         static const uint8_t addresses[] = {
1238                 2,      /* Type should be DDR SDRAM */
1239                 3,      /* *Row addresses */
1240                 4,      /* *Column addresses */
1241                 5,      /* *Physical Banks */
1242                 6,      /* *Module Data Width low */
1243                 7,      /* *Module Data Width high */
1244                 9,      /* *Cycle time at highest CAS Latency CL=X */
1245                 11,     /* *SDRAM Type */
1246                 13,     /* *SDRAM Width */
1247                 17,     /* *Logical Banks */
1248                 18,     /* *Supported CAS Latencies */
1249                 21,     /* *SDRAM Module Attributes */
1250                 23,     /* *Cycle time at CAS Latnecy (CLX - 0.5) */
1251                 26,     /* *Cycle time at CAS Latnecy (CLX - 1.0) */
1252                 27,     /* *tRP Row precharge time */
1253                 28,     /* *Minimum Row Active to Row Active Delay (tRRD) */
1254                 29,     /* *tRCD RAS to CAS */
1255                 30,     /* *tRAS Activate to Precharge */
1256                 41,     /* *Minimum Active to Active/Auto Refresh Time(Trc) */
1257                 42,     /* *Minimum Auto Refresh Command Time(Trfc) */
1258         };
1259         /* If the dimms are not in pairs do not do dual channels */
1260         if ((dimm_mask & ((1 << DIMM_SOCKETS) - 1)) !=
1261                 ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) { 
1262                 goto single_channel;
1263         }
1264         /* If the cpu is not capable of doing dual channels don't do dual channels */
1265         nbcap = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1266         if (!(nbcap & NBCAP_128Bit)) {
1267                 goto single_channel;
1268         }
1269         for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1270                 unsigned device0, device1;
1271                 int value0, value1;
1272                 int j;
1273                 /* If I don't have a dimm skip this one */
1274                 if (!(dimm_mask & (1 << i))) {
1275                         continue;
1276                 }
1277                 device0 = ctrl->channel0[i];
1278                 device1 = ctrl->channel1[i];
1279                 for(j = 0; j < sizeof(addresses)/sizeof(addresses[0]); j++) {
1280                         unsigned addr;
1281                         addr = addresses[j];
1282                         value0 = spd_read_byte(device0, addr);
1283                         if (value0 < 0) {
1284                                 return -1;
1285                         }
1286                         value1 = spd_read_byte(device1, addr);
1287                         if (value1 < 0) {
1288                                 return -1;
1289                         }
1290                         if (value0 != value1) {
1291                                 goto single_channel;
1292                         }
1293                 }
1294         }
1295         print_spew("Enabling dual channel memory\r\n");
1296         uint32_t dcl;
1297         dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1298         dcl &= ~DCL_32ByteEn;
1299         dcl |= DCL_128BitEn;
1300         pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1301         return dimm_mask;
1302  single_channel:
1303         dimm_mask &= ~((1 << (DIMM_SOCKETS *2)) - (1 << DIMM_SOCKETS));
1304         return dimm_mask;
1305 }
1306
1307 struct mem_param {
1308         uint8_t cycle_time;
1309         uint8_t divisor; /* In 1/2 ns increments */
1310         uint8_t tRC;
1311         uint8_t tRFC;
1312         uint32_t dch_memclk;
1313         uint16_t dch_tref4k, dch_tref8k;
1314         uint8_t  dtl_twr;
1315         char name[9];
1316 };
1317
1318 static const struct mem_param *get_mem_param(unsigned min_cycle_time)
1319 {
1320         static const struct mem_param speed[] = {
1321                 {
1322                         .name       = "100Mhz\r\n",
1323                         .cycle_time = 0xa0,
1324                         .divisor    = (10 <<1),
1325                         .tRC        = 0x46,
1326                         .tRFC       = 0x50,
1327                         .dch_memclk = DCH_MEMCLK_100MHZ << DCH_MEMCLK_SHIFT,
1328                         .dch_tref4k = DTH_TREF_100MHZ_4K,
1329                         .dch_tref8k = DTH_TREF_100MHZ_8K,
1330                         .dtl_twr    = 2,
1331                 },
1332                 {
1333                         .name       = "133Mhz\r\n",
1334                         .cycle_time = 0x75,
1335                         .divisor    = (7<<1)+1,
1336                         .tRC        = 0x41,
1337                         .tRFC       = 0x4B,
1338                         .dch_memclk = DCH_MEMCLK_133MHZ << DCH_MEMCLK_SHIFT,
1339                         .dch_tref4k = DTH_TREF_133MHZ_4K,
1340                         .dch_tref8k = DTH_TREF_133MHZ_8K,
1341                         .dtl_twr    = 2,
1342                 },
1343                 {
1344                         .name       = "166Mhz\r\n",
1345                         .cycle_time = 0x60,
1346                         .divisor    = (6<<1),
1347                         .tRC        = 0x3C,
1348                         .tRFC       = 0x48,
1349                         .dch_memclk = DCH_MEMCLK_166MHZ << DCH_MEMCLK_SHIFT,
1350                         .dch_tref4k = DTH_TREF_166MHZ_4K,
1351                         .dch_tref8k = DTH_TREF_166MHZ_8K,
1352                         .dtl_twr    = 3,
1353                 },
1354                 {
1355                         .name       = "200Mhz\r\n",
1356                         .cycle_time = 0x50,
1357                         .divisor    = (5<<1),
1358                         .tRC        = 0x37,
1359                         .tRFC       = 0x46,
1360                         .dch_memclk = DCH_MEMCLK_200MHZ << DCH_MEMCLK_SHIFT,
1361                         .dch_tref4k = DTH_TREF_200MHZ_4K,
1362                         .dch_tref8k = DTH_TREF_200MHZ_8K,
1363                         .dtl_twr    = 3,
1364                 },
1365                 {
1366                         .cycle_time = 0x00,
1367                 },
1368         };
1369         const struct mem_param *param;
1370         for(param = &speed[0]; param->cycle_time ; param++) {
1371                 if (min_cycle_time > (param+1)->cycle_time) {
1372                         break;
1373                 }
1374         }
1375         if (!param->cycle_time) {
1376                 die("min_cycle_time to low");
1377         }
1378         print_spew(param->name);
1379 #ifdef DRAM_MIN_CYCLE_TIME
1380         print_debug(param->name);
1381 #endif
1382         return param;
1383 }
1384
1385 struct spd_set_memclk_result {
1386         const struct mem_param *param;
1387         long dimm_mask;
1388 };
1389 static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *ctrl, long dimm_mask)
1390 {
1391         /* Compute the minimum cycle time for these dimms */
1392         struct spd_set_memclk_result result;
1393         unsigned min_cycle_time, min_latency, bios_cycle_time;
1394         int i;
1395         uint32_t value;
1396
1397         static const uint8_t latency_indicies[] = { 26, 23, 9 };
1398         static const unsigned char min_cycle_times[] = {
1399                 [NBCAP_MEMCLK_200MHZ] = 0x50, /* 5ns */
1400                 [NBCAP_MEMCLK_166MHZ] = 0x60, /* 6ns */
1401                 [NBCAP_MEMCLK_133MHZ] = 0x75, /* 7.5ns */
1402                 [NBCAP_MEMCLK_100MHZ] = 0xa0, /* 10ns */
1403         };
1404
1405
1406         value = pci_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1407         min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
1408         bios_cycle_time = min_cycle_times[
1409                 read_option(CMOS_VSTART_max_mem_clock, CMOS_VLEN_max_mem_clock, 0)];
1410         if (bios_cycle_time > min_cycle_time) {
1411                 min_cycle_time = bios_cycle_time;
1412         }
1413         min_latency = 2;
1414
1415         /* Compute the least latency with the fastest clock supported
1416          * by both the memory controller and the dimms.
1417          */
1418         for(i = 0; i < DIMM_SOCKETS; i++) {
1419                 int new_cycle_time, new_latency;
1420                 int index;
1421                 int latencies;
1422                 int latency;
1423
1424                 if (!(dimm_mask & (1 << i))) {
1425                         continue;
1426                 }
1427
1428                 /* First find the supported CAS latencies
1429                  * Byte 18 for DDR SDRAM is interpreted:
1430                  * bit 0 == CAS Latency = 1.0
1431                  * bit 1 == CAS Latency = 1.5
1432                  * bit 2 == CAS Latency = 2.0
1433                  * bit 3 == CAS Latency = 2.5
1434                  * bit 4 == CAS Latency = 3.0
1435                  * bit 5 == CAS Latency = 3.5
1436                  * bit 6 == TBD
1437                  * bit 7 == TBD
1438                  */
1439                 new_cycle_time = 0xa0;
1440                 new_latency = 5;
1441
1442                 latencies = spd_read_byte(ctrl->channel0[i], 18);
1443                 if (latencies <= 0) continue;
1444
1445                 /* Compute the lowest cas latency supported */
1446                 latency = log2(latencies) -2;
1447
1448                 /* Loop through and find a fast clock with a low latency */
1449                 for(index = 0; index < 3; index++, latency++) {
1450                         int value;
1451                         if ((latency < 2) || (latency > 4) ||
1452                                 (!(latencies & (1 << latency)))) {
1453                                 continue;
1454                         }
1455                         value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1456                         if (value < 0) {
1457                                 goto hw_error;
1458                         }
1459
1460                         /* Only increase the latency if we decreas the clock */
1461                         if ((value >= min_cycle_time) && (value < new_cycle_time)) {
1462                                 new_cycle_time = value;
1463                                 new_latency = latency;
1464                         }
1465                 }
1466                 if (new_latency > 4){
1467                         continue;
1468                 }
1469                 /* Does min_latency need to be increased? */
1470                 if (new_cycle_time > min_cycle_time) {
1471                         min_cycle_time = new_cycle_time;
1472                 }
1473                 /* Does min_cycle_time need to be increased? */
1474                 if (new_latency > min_latency) {
1475                         min_latency = new_latency;
1476                 }
1477         }
1478         /* Make a second pass through the dimms and disable
1479          * any that cannot support the selected memclk and cas latency.
1480          */
1481         
1482         for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1483                 int latencies;
1484                 int latency;
1485                 int index;
1486                 int value;
1487                 if (!(dimm_mask & (1 << i))) {
1488                         continue;
1489                 }
1490                 latencies = spd_read_byte(ctrl->channel0[i], 18);
1491                 if (latencies < 0) goto hw_error;
1492                 if (latencies == 0) {
1493                         goto dimm_err;
1494                 }
1495
1496                 /* Compute the lowest cas latency supported */
1497                 latency = log2(latencies) -2;
1498
1499                 /* Walk through searching for the selected latency */
1500                 for(index = 0; index < 3; index++, latency++) {
1501                         if (!(latencies & (1 << latency))) {
1502                                 continue;
1503                         }
1504                         if (latency == min_latency)
1505                                 break;
1506                 }
1507                 /* If I can't find the latency or my index is bad error */
1508                 if ((latency != min_latency) || (index >= 3)) {
1509                         goto dimm_err;
1510                 }
1511                 
1512                 /* Read the min_cycle_time for this latency */
1513                 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1514                 if (value < 0) goto hw_error;
1515                 
1516                 /* All is good if the selected clock speed 
1517                  * is what I need or slower.
1518                  */
1519                 if (value <= min_cycle_time) {
1520                         continue;
1521                 }
1522                 /* Otherwise I have an error, disable the dimm */
1523         dimm_err:
1524                 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
1525         }
1526 #if 0
1527 //down speed for full load 4 rank support
1528 #if K8_4RANK_DIMM_SUPPORT
1529         if(dimm_mask == (3|(3<<DIMM_SOCKETS)) ) {
1530                 int ranks = 4;
1531                 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1532                         int val;
1533                         if (!(dimm_mask & (1 << i))) {
1534                                 continue;
1535                         }
1536                         val = spd_read_byte(ctrl->channel0[i], 5);
1537                         if(val!=ranks) {
1538                                 ranks = val;
1539                                 break;
1540                         }
1541                 }
1542                 if(ranks==4) {
1543                         if(min_cycle_time <= 0x50 ) {
1544                                 min_cycle_time = 0x60;
1545                         }
1546                 }
1547                 
1548         }
1549 #endif
1550 #endif
1551         /* Now that I know the minimum cycle time lookup the memory parameters */
1552         result.param = get_mem_param(min_cycle_time);
1553
1554         /* Update DRAM Config High with our selected memory speed */
1555         value = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1556         value &= ~(DCH_MEMCLK_MASK << DCH_MEMCLK_SHIFT);
1557 #if 0
1558         /* Improves DQS centering by correcting for case when core speed multiplier and MEMCLK speed result in odd clock divisor, by selecting the next lowest memory speed, required only at DDR400 and higher speeds with certain DIMM loadings ---- cheating???*/
1559         if(!is_cpu_pre_e0()) {
1560                 if(min_cycle_time==0x50) {
1561                         value |= 1<<31;
1562                 }
1563         }
1564 #endif
1565
1566         value |= result.param->dch_memclk;
1567         pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
1568
1569         static const unsigned latencies[] = { DTL_CL_2, DTL_CL_2_5, DTL_CL_3 };
1570         /* Update DRAM Timing Low with our selected cas latency */
1571         value = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1572         value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
1573         value |= latencies[min_latency - 2] << DTL_TCL_SHIFT;
1574         pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
1575         
1576         result.dimm_mask = dimm_mask;
1577         return result;
1578  hw_error:
1579         result.param = (const struct mem_param *)0;
1580         result.dimm_mask = -1;
1581         return result;
1582 }
1583
1584
1585 static int update_dimm_Trc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1586 {
1587         unsigned clocks, old_clocks;
1588         uint32_t dtl;
1589         int value;
1590         value = spd_read_byte(ctrl->channel0[i], 41);
1591         if (value < 0) return -1;
1592         if ((value == 0) || (value == 0xff)) {
1593                 value = param->tRC;
1594         }
1595         clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1596         if (clocks < DTL_TRC_MIN) {
1597                 clocks = DTL_TRC_MIN;
1598         }
1599         if (clocks > DTL_TRC_MAX) {
1600                 return 0;
1601         }
1602
1603         dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1604         old_clocks = ((dtl >> DTL_TRC_SHIFT) & DTL_TRC_MASK) + DTL_TRC_BASE;
1605         if (old_clocks > clocks) {
1606                 clocks = old_clocks;
1607         }
1608         dtl &= ~(DTL_TRC_MASK << DTL_TRC_SHIFT);
1609         dtl |=  ((clocks - DTL_TRC_BASE) << DTL_TRC_SHIFT);
1610         pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1611         return 1;
1612 }
1613
1614 static int update_dimm_Trfc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1615 {
1616         unsigned clocks, old_clocks;
1617         uint32_t dtl;
1618         int value;
1619         value = spd_read_byte(ctrl->channel0[i], 42);
1620         if (value < 0) return -1;
1621         if ((value == 0) || (value == 0xff)) {
1622                 value = param->tRFC;
1623         }
1624         clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1625         if (clocks < DTL_TRFC_MIN) {
1626                 clocks = DTL_TRFC_MIN;
1627         }
1628         if (clocks > DTL_TRFC_MAX) {
1629                 return 0;
1630         }
1631         dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1632         old_clocks = ((dtl >> DTL_TRFC_SHIFT) & DTL_TRFC_MASK) + DTL_TRFC_BASE;
1633         if (old_clocks > clocks) {
1634                 clocks = old_clocks;
1635         }
1636         dtl &= ~(DTL_TRFC_MASK << DTL_TRFC_SHIFT);
1637         dtl |= ((clocks - DTL_TRFC_BASE) << DTL_TRFC_SHIFT);
1638         pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1639         return 1;
1640 }
1641
1642
1643 static int update_dimm_Trcd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1644 {
1645         unsigned clocks, old_clocks;
1646         uint32_t dtl;
1647         int value;
1648         value = spd_read_byte(ctrl->channel0[i], 29);
1649         if (value < 0) return -1;
1650         clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1651         if (clocks < DTL_TRCD_MIN) {
1652                 clocks = DTL_TRCD_MIN;
1653         }
1654         if (clocks > DTL_TRCD_MAX) {
1655                 return 0;
1656         }
1657         dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1658         old_clocks = ((dtl >> DTL_TRCD_SHIFT) & DTL_TRCD_MASK) + DTL_TRCD_BASE;
1659         if (old_clocks > clocks) {
1660                 clocks = old_clocks;
1661         }
1662         dtl &= ~(DTL_TRCD_MASK << DTL_TRCD_SHIFT);
1663         dtl |= ((clocks - DTL_TRCD_BASE) << DTL_TRCD_SHIFT);
1664         pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1665         return 1;
1666 }
1667
1668 static int update_dimm_Trrd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1669 {
1670         unsigned clocks, old_clocks;
1671         uint32_t dtl;
1672         int value;
1673         value = spd_read_byte(ctrl->channel0[i], 28);
1674         if (value < 0) return -1;
1675         clocks = (value + (param->divisor << 1) -1)/(param->divisor << 1);
1676         if (clocks < DTL_TRRD_MIN) {
1677                 clocks = DTL_TRRD_MIN;
1678         }
1679         if (clocks > DTL_TRRD_MAX) {
1680                 return 0;
1681         }
1682         dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1683         old_clocks = ((dtl >> DTL_TRRD_SHIFT) & DTL_TRRD_MASK) + DTL_TRRD_BASE;
1684         if (old_clocks > clocks) {
1685                 clocks = old_clocks;
1686         }
1687         dtl &= ~(DTL_TRRD_MASK << DTL_TRRD_SHIFT);
1688         dtl |= ((clocks - DTL_TRRD_BASE) << DTL_TRRD_SHIFT);
1689         pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1690         return 1;
1691 }
1692
1693 static int update_dimm_Tras(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1694 {
1695         unsigned clocks, old_clocks;
1696         uint32_t dtl;
1697         int value;
1698         value = spd_read_byte(ctrl->channel0[i], 30);
1699         if (value < 0) return -1;
1700         clocks = ((value << 1) + param->divisor - 1)/param->divisor;
1701         if (clocks < DTL_TRAS_MIN) {
1702                 clocks = DTL_TRAS_MIN;
1703         }
1704         if (clocks > DTL_TRAS_MAX) {
1705                 return 0;
1706         }
1707         dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1708         old_clocks = ((dtl >> DTL_TRAS_SHIFT) & DTL_TRAS_MASK) + DTL_TRAS_BASE;
1709         if (old_clocks > clocks) {
1710                 clocks = old_clocks;
1711         }
1712         dtl &= ~(DTL_TRAS_MASK << DTL_TRAS_SHIFT);
1713         dtl |= ((clocks - DTL_TRAS_BASE) << DTL_TRAS_SHIFT);
1714         pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1715         return 1;
1716 }
1717
1718 static int update_dimm_Trp(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1719 {
1720         unsigned clocks, old_clocks;
1721         uint32_t dtl;
1722         int value;
1723         value = spd_read_byte(ctrl->channel0[i], 27);
1724         if (value < 0) return -1;
1725         clocks = (value + (param->divisor << 1) - 1)/(param->divisor << 1);
1726         if (clocks < DTL_TRP_MIN) {
1727                 clocks = DTL_TRP_MIN;
1728         }
1729         if (clocks > DTL_TRP_MAX) {
1730                 return 0;
1731         }
1732         dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1733         old_clocks = ((dtl >> DTL_TRP_SHIFT) & DTL_TRP_MASK) + DTL_TRP_BASE;
1734         if (old_clocks > clocks) {
1735                 clocks = old_clocks;
1736         }
1737         dtl &= ~(DTL_TRP_MASK << DTL_TRP_SHIFT);
1738         dtl |= ((clocks - DTL_TRP_BASE) << DTL_TRP_SHIFT);
1739         pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1740         return 1;
1741 }
1742
1743 static void set_Twr(const struct mem_controller *ctrl, const struct mem_param *param)
1744 {
1745         uint32_t dtl;
1746         dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1747         dtl &= ~(DTL_TWR_MASK << DTL_TWR_SHIFT);
1748         dtl |= (param->dtl_twr - DTL_TWR_BASE) << DTL_TWR_SHIFT;
1749         pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1750 }
1751
1752
1753 static void init_Tref(const struct mem_controller *ctrl, const struct mem_param *param)
1754 {
1755         uint32_t dth;
1756         dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1757         dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1758         dth |= (param->dch_tref4k << DTH_TREF_SHIFT);
1759         pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1760 }
1761
1762 static int update_dimm_Tref(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1763 {
1764         uint32_t dth;
1765         int value;
1766         unsigned tref, old_tref;
1767         value = spd_read_byte(ctrl->channel0[i], 3);
1768         if (value < 0) return -1;
1769         value &= 0xf;
1770
1771         tref = param->dch_tref8k;
1772         if (value == 12) {
1773                 tref = param->dch_tref4k;
1774         }
1775
1776         dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1777         old_tref = (dth >> DTH_TREF_SHIFT) & DTH_TREF_MASK;
1778         if ((value == 12) && (old_tref == param->dch_tref4k)) {
1779                 tref = param->dch_tref4k;
1780         } else {
1781                 tref = param->dch_tref8k;
1782         }
1783         dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1784         dth |= (tref << DTH_TREF_SHIFT);
1785         pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1786         return 1;
1787 }
1788
1789
1790 static int update_dimm_x4(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1791 {
1792         uint32_t dcl;
1793         int value;
1794 #if K8_4RANK_DIMM_SUPPORT == 1
1795         int rank;
1796 #endif
1797         int dimm;
1798         value = spd_read_byte(ctrl->channel0[i], 13);
1799         if (value < 0) {
1800                 return -1;
1801         }
1802
1803 #if K8_4RANK_DIMM_SUPPORT == 1
1804         rank = spd_read_byte(ctrl->channel0[i], 5);       /* number of physical banks */
1805         if (rank < 0) {
1806                 return -1;      
1807         }
1808 #endif
1809
1810         dimm = 1<<(DCL_x4DIMM_SHIFT+i);
1811 #if K8_4RANK_DIMM_SUPPORT == 1
1812         if(rank==4) {
1813                 dimm |= 1<<(DCL_x4DIMM_SHIFT+i+2);
1814         }
1815 #endif
1816         dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1817         dcl &= ~dimm;
1818         if (value == 4) {
1819                 dcl |= dimm;
1820         }
1821         pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1822         return 1;
1823 }
1824
1825 static int update_dimm_ecc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1826 {
1827         uint32_t dcl;
1828         int value;
1829         value = spd_read_byte(ctrl->channel0[i], 11);
1830         if (value < 0) {
1831                 return -1;
1832         }
1833         if (value != 2) {
1834                 dcl = pci_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1835                 dcl &= ~DCL_DimmEccEn;
1836                 pci_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1837         }
1838         return 1;
1839 }
1840
1841 static int count_dimms(const struct mem_controller *ctrl)
1842 {
1843         int dimms;
1844         unsigned index;
1845         dimms = 0;
1846         for(index = 0; index < 8; index += 2) {
1847                 uint32_t csbase;
1848                 csbase = pci_read_config32(ctrl->f2, (DRAM_CSBASE + (index << 2)));
1849                 if (csbase & 1) {
1850                         dimms += 1;
1851                 }
1852         }
1853         return dimms;
1854 }
1855
1856 static void set_Twtr(const struct mem_controller *ctrl, const struct mem_param *param)
1857 {
1858         uint32_t dth;
1859         unsigned clocks;
1860         clocks = 1; /* AMD says hard code this */
1861         dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1862         dth &= ~(DTH_TWTR_MASK << DTH_TWTR_SHIFT);
1863         dth |= ((clocks - DTH_TWTR_BASE) << DTH_TWTR_SHIFT);
1864         pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1865 }
1866
1867 static void set_Trwt(const struct mem_controller *ctrl, const struct mem_param *param)
1868 {
1869         uint32_t dth, dtl;
1870         unsigned divisor;
1871         unsigned latency;
1872         unsigned clocks;
1873
1874         clocks = 0;
1875         dtl = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1876         latency = (dtl >> DTL_TCL_SHIFT) & DTL_TCL_MASK;
1877         divisor = param->divisor;
1878
1879         if (is_opteron(ctrl)) {
1880                 if (latency == DTL_CL_2) {
1881                         if (divisor == ((6 << 0) + 0)) {
1882                                 /* 166Mhz */
1883                                 clocks = 3;
1884                         }
1885                         else if (divisor > ((6 << 0)+0)) {
1886                                 /* 100Mhz && 133Mhz */
1887                                 clocks = 2;
1888                         }
1889                 }
1890                 else if (latency == DTL_CL_2_5) {
1891                         clocks = 3;
1892                 }
1893                 else if (latency == DTL_CL_3) {
1894                         if (divisor == ((6 << 0)+0)) {
1895                                 /* 166Mhz */
1896                                 clocks = 4;
1897                         }
1898                         else if (divisor > ((6 << 0)+0)) {
1899                                 /* 100Mhz && 133Mhz */
1900                                 clocks = 3;
1901                         }
1902                 }
1903         }
1904         else /* Athlon64 */ {
1905                 if (is_registered(ctrl)) {
1906                         if (latency == DTL_CL_2) {
1907                                 clocks = 2;
1908                         }
1909                         else if (latency == DTL_CL_2_5) {
1910                                 clocks = 3;
1911                         }
1912                         else if (latency == DTL_CL_3) {
1913                                 clocks = 3;
1914                         }
1915                 }
1916                 else /* Unbuffered */{
1917                         if (latency == DTL_CL_2) {
1918                                 clocks = 3;
1919                         }
1920                         else if (latency == DTL_CL_2_5) {
1921                                 clocks = 4;
1922                         }
1923                         else if (latency == DTL_CL_3) {
1924                                 clocks = 4;
1925                         }
1926                 }
1927         }
1928         if ((clocks < DTH_TRWT_MIN) || (clocks > DTH_TRWT_MAX)) {
1929                 die("Unknown Trwt\r\n");
1930         }
1931         
1932         dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1933         dth &= ~(DTH_TRWT_MASK << DTH_TRWT_SHIFT);
1934         dth |= ((clocks - DTH_TRWT_BASE) << DTH_TRWT_SHIFT);
1935         pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1936         return;
1937 }
1938
1939 static void set_Twcl(const struct mem_controller *ctrl, const struct mem_param *param)
1940 {
1941         /* Memory Clocks after CAS# */
1942         uint32_t dth;
1943         unsigned clocks;
1944         if (is_registered(ctrl)) {
1945                 clocks = 2;
1946         } else {
1947                 clocks = 1;
1948         }
1949         dth = pci_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1950         dth &= ~(DTH_TWCL_MASK << DTH_TWCL_SHIFT);
1951         dth |= ((clocks - DTH_TWCL_BASE) << DTH_TWCL_SHIFT);
1952         pci_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1953 }
1954
1955
1956 static void set_read_preamble(const struct mem_controller *ctrl, const struct mem_param *param)
1957 {
1958         uint32_t dch;
1959         unsigned divisor;
1960         unsigned rdpreamble;
1961         divisor = param->divisor;
1962         dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1963         dch &= ~(DCH_RDPREAMBLE_MASK << DCH_RDPREAMBLE_SHIFT);
1964         rdpreamble = 0;
1965         if (is_registered(ctrl)) {
1966                 if (divisor == ((10 << 1)+0)) {
1967                         /* 100Mhz, 9ns */
1968                         rdpreamble = ((9 << 1)+ 0);
1969                 }
1970                 else if (divisor == ((7 << 1)+1)) {
1971                         /* 133Mhz, 8ns */
1972                         rdpreamble = ((8 << 1)+0);
1973                 }
1974                 else if (divisor == ((6 << 1)+0)) {
1975                         /* 166Mhz, 7.5ns */
1976                         rdpreamble = ((7 << 1)+1);
1977                 }
1978                 else if (divisor == ((5 << 1)+0)) {
1979                         /* 200Mhz,  7ns */
1980                         rdpreamble = ((7 << 1)+0);
1981                 }
1982         }
1983         else {
1984                 int slots;
1985                 int i;
1986                 slots = 0;
1987                 for(i = 0; i < 4; i++) {
1988                         if (ctrl->channel0[i]) {
1989                                 slots += 1;
1990                         }
1991                 }
1992                 if (divisor == ((10 << 1)+0)) {
1993                         /* 100Mhz */
1994                         if (slots <= 2) {
1995                                 /* 9ns */
1996                                 rdpreamble = ((9 << 1)+0);
1997                         } else {
1998                                 /* 14ns */
1999                                 rdpreamble = ((14 << 1)+0);
2000                         }
2001                 }
2002                 else if (divisor == ((7 << 1)+1)) {
2003                         /* 133Mhz */
2004                         if (slots <= 2) {
2005                                 /* 7ns */
2006                                 rdpreamble = ((7 << 1)+0);
2007                         } else {
2008                                 /* 11 ns */
2009                                 rdpreamble = ((11 << 1)+0);
2010                         }
2011                 }
2012                 else if (divisor == ((6 << 1)+0)) {
2013                         /* 166Mhz */
2014                         if (slots <= 2) {
2015                                 /* 6ns */
2016                                 rdpreamble = ((7 << 1)+0);
2017                         } else {
2018                                 /* 9ns */
2019                                 rdpreamble = ((9 << 1)+0);
2020                         }
2021                 }
2022                 else if (divisor == ((5 << 1)+0)) {
2023                         /* 200Mhz */
2024                         if (slots <= 2) {
2025                                 /* 5ns */
2026                                 rdpreamble = ((5 << 1)+0);
2027                         } else {
2028                                 /* 7ns */
2029                                 rdpreamble = ((7 << 1)+0);
2030                         }
2031                 }
2032         }
2033         if ((rdpreamble < DCH_RDPREAMBLE_MIN) || (rdpreamble > DCH_RDPREAMBLE_MAX)) {
2034                 die("Unknown rdpreamble");
2035         }
2036         dch |= (rdpreamble - DCH_RDPREAMBLE_BASE) << DCH_RDPREAMBLE_SHIFT;
2037         pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2038 }
2039
2040 static void set_max_async_latency(const struct mem_controller *ctrl, const struct mem_param *param)
2041 {
2042         uint32_t dch;
2043         unsigned async_lat;
2044         int dimms;
2045
2046         dimms = count_dimms(ctrl);
2047
2048         dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2049         dch &= ~(DCH_ASYNC_LAT_MASK << DCH_ASYNC_LAT_SHIFT);
2050         async_lat = 0;
2051         if (is_registered(ctrl)) {
2052                 if (dimms == 4) {
2053                         /* 9ns */
2054                         async_lat = 9;
2055                 } 
2056                 else {
2057                         /* 8ns */
2058                         async_lat = 8;
2059                 }
2060         }
2061         else {
2062                 if (dimms > 3) {
2063                         die("Too many unbuffered dimms");
2064                 }
2065                 else if (dimms == 3) {
2066                         /* 7ns */
2067                         async_lat = 7;
2068                 }
2069                 else {
2070                         /* 6ns */
2071                         async_lat = 6;
2072                 }
2073         }
2074         dch |= ((async_lat - DCH_ASYNC_LAT_BASE) << DCH_ASYNC_LAT_SHIFT);
2075         pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2076 }
2077
2078 static void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct mem_param *param)
2079 {
2080         uint32_t dch;
2081         /* AMD says to Hardcode this */
2082         dch = pci_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2083         dch &= ~(DCH_IDLE_LIMIT_MASK << DCH_IDLE_LIMIT_SHIFT);
2084         dch |= DCH_IDLE_LIMIT_16 << DCH_IDLE_LIMIT_SHIFT;
2085         dch |= DCH_DYN_IDLE_CTR_EN;
2086         pci_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2087 }
2088
2089 static long spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param, long dimm_mask)
2090 {
2091         int i;
2092         
2093         init_Tref(ctrl, param);
2094         for(i = 0; i < DIMM_SOCKETS; i++) {
2095                 int rc;
2096                 if (!(dimm_mask & (1 << i))) {
2097                         continue;
2098                 }
2099                 /* DRAM Timing Low Register */
2100                 if ((rc = update_dimm_Trc (ctrl, param, i)) <= 0) goto dimm_err;
2101                 if ((rc = update_dimm_Trfc(ctrl, param, i)) <= 0) goto dimm_err;
2102                 if ((rc = update_dimm_Trcd(ctrl, param, i)) <= 0) goto dimm_err;
2103                 if ((rc = update_dimm_Trrd(ctrl, param, i)) <= 0) goto dimm_err;
2104                 if ((rc = update_dimm_Tras(ctrl, param, i)) <= 0) goto dimm_err;
2105                 if ((rc = update_dimm_Trp (ctrl, param, i)) <= 0) goto dimm_err;
2106
2107                 /* DRAM Timing High Register */
2108                 if ((rc = update_dimm_Tref(ctrl, param, i)) <= 0) goto dimm_err;
2109         
2110
2111                 /* DRAM Config Low */
2112                 if ((rc = update_dimm_x4 (ctrl, param, i)) <= 0) goto dimm_err;
2113                 if ((rc = update_dimm_ecc(ctrl, param, i)) <= 0) goto dimm_err;
2114                 continue;
2115         dimm_err:
2116                 if (rc < 0) {
2117                         return -1;
2118                 }
2119                 dimm_mask = disable_dimm(ctrl, i, dimm_mask);
2120         }
2121         /* DRAM Timing Low Register */
2122         set_Twr(ctrl, param);
2123
2124         /* DRAM Timing High Register */
2125         set_Twtr(ctrl, param);
2126         set_Trwt(ctrl, param);
2127         set_Twcl(ctrl, param);
2128
2129         /* DRAM Config High */
2130         set_read_preamble(ctrl, param);
2131         set_max_async_latency(ctrl, param);
2132         set_idle_cycle_limit(ctrl, param);
2133         return dimm_mask;
2134 }
2135
2136 static void sdram_set_spd_registers(const struct mem_controller *ctrl) 
2137 {
2138         struct spd_set_memclk_result result;
2139         const struct mem_param *param;
2140         long dimm_mask;
2141 #if 1
2142         if (!controller_present(ctrl)) {
2143 //              print_debug("No memory controller present\r\n");
2144                 return;
2145         }
2146 #endif
2147         hw_enable_ecc(ctrl);
2148         activate_spd_rom(ctrl);
2149         dimm_mask = spd_detect_dimms(ctrl);
2150         if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
2151                 print_debug("No memory for this cpu\r\n");
2152                 return;
2153         }
2154         dimm_mask = spd_enable_2channels(ctrl, dimm_mask);        
2155         if (dimm_mask < 0) 
2156                 goto hw_spd_err;
2157         dimm_mask = spd_set_ram_size(ctrl , dimm_mask);           
2158         if (dimm_mask < 0) 
2159                 goto hw_spd_err;
2160         dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask); 
2161         if (dimm_mask < 0) 
2162                 goto hw_spd_err;
2163         result = spd_set_memclk(ctrl, dimm_mask);
2164         param     = result.param;
2165         dimm_mask = result.dimm_mask;
2166         if (dimm_mask < 0) 
2167                 goto hw_spd_err;
2168         dimm_mask = spd_set_dram_timing(ctrl, param , dimm_mask);
2169         if (dimm_mask < 0)
2170                 goto hw_spd_err;
2171         order_dimms(ctrl);
2172         return;
2173  hw_spd_err:
2174         /* Unrecoverable error reading SPD data */
2175         print_err("SPD error - reset\r\n");
2176         hard_reset();
2177         return;
2178 }
2179
2180 #if K8_HW_MEM_HOLE_SIZEK != 0
2181 static uint32_t hoist_memory(int controllers, const struct mem_controller *ctrl,unsigned hole_startk, int i)
2182 {
2183         int ii;
2184         uint32_t carry_over;
2185         device_t dev;
2186         uint32_t base, limit;
2187         uint32_t basek;
2188         uint32_t hoist;
2189         int j;
2190
2191         carry_over = (4*1024*1024) - hole_startk;
2192
2193         for(ii=controllers - 1;ii>i;ii--) {
2194                 base  = pci_read_config32(ctrl[0].f1, 0x40 + (ii << 3));
2195                 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2196                         continue;
2197                 }
2198                 limit = pci_read_config32(ctrl[0].f1, 0x44 + (ii << 3));
2199                 for(j = 0; j < controllers; j++) {
2200                         pci_write_config32(ctrl[j].f1, 0x44 + (ii << 3), limit + (carry_over << 2));
2201                         pci_write_config32(ctrl[j].f1, 0x40 + (ii << 3), base + (carry_over << 2));
2202                 }
2203         }
2204         limit = pci_read_config32(ctrl[0].f1, 0x44 + (i << 3));
2205         for(j = 0; j < controllers; j++) {
2206                 pci_write_config32(ctrl[j].f1, 0x44 + (i << 3), limit + (carry_over << 2));
2207         }
2208         dev = ctrl[i].f1;
2209         base  = pci_read_config32(dev, 0x40 + (i << 3));
2210         basek  = (base & 0xffff0000) >> 2;
2211         if(basek == hole_startk) {
2212                 //don't need set memhole here, because hole off set will be 0, overflow
2213                 //so need to change base reg instead, new basek will be 4*1024*1024
2214                 base &= 0x0000ffff;
2215                 base |= (4*1024*1024)<<2;
2216                 for(j = 0; j < controllers; j++) {
2217                         pci_write_config32(ctrl[j].f1, 0x40 + (i<<3), base);
2218                 }
2219         }
2220         else {
2221                 hoist = /* hole start address */
2222                         ((hole_startk << 10) & 0xff000000) +
2223                         /* hole address to memory controller address */
2224                         (((basek + carry_over) >> 6) & 0x0000ff00) +
2225                         /* enable */
2226                         1;
2227                 pci_write_config32(dev, 0xf0, hoist);
2228         }
2229
2230         return carry_over;
2231 }
2232
2233 static void set_hw_mem_hole(int controllers, const struct mem_controller *ctrl)
2234 {
2235
2236         uint32_t hole_startk;
2237         int i;
2238
2239         hole_startk = 4*1024*1024 - K8_HW_MEM_HOLE_SIZEK;
2240
2241 #if K8_HW_MEM_HOLE_SIZE_AUTO_INC == 1 
2242         //We need to double check if the hole_startk is valid, if it is equal to basek, we need to decrease it some
2243         uint32_t basek_pri;
2244         for(i=0; i<controllers; i++) {
2245                         uint32_t base;
2246                         unsigned base_k;
2247                         base  = pci_read_config32(ctrl[0].f1, 0x40 + (i << 3));
2248                         if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2249                                 continue;
2250                         }
2251                         base_k = (base & 0xffff0000) >> 2;
2252                         if(base_k == hole_startk) {
2253                                 hole_startk -= (base_k - basek_pri)>>1; // decrease mem hole startk to make sure it is on middle of privous node
2254                                 break; //only one hole
2255                         }
2256                         basek_pri = base_k;
2257         }
2258
2259 #endif
2260         //find node index that need do set hole
2261         for(i=0; i<controllers; i++) {
2262                         uint32_t base, limit;
2263                         unsigned base_k, limit_k;
2264                         base  = pci_read_config32(ctrl[0].f1, 0x40 + (i << 3));
2265                         if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2266                                 continue;
2267                         }
2268                         limit = pci_read_config32(ctrl[0].f1, 0x44 + (i << 3));
2269                         base_k = (base & 0xffff0000) >> 2;
2270                         limit_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
2271                         if ((base_k <= hole_startk) && (limit_k > hole_startk)) {
2272                                 unsigned end_k;
2273                                 hoist_memory(controllers, ctrl, hole_startk, i);
2274                                 end_k = memory_end_k(ctrl, controllers);
2275                                 set_top_mem(end_k, hole_startk);
2276                                 break; //only one hole
2277                         }
2278         }
2279
2280 }
2281
2282 #endif
2283
2284 #define TIMEOUT_LOOPS 300000
2285 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
2286 {
2287         int i;
2288
2289         /* Error if I don't have memory */
2290         if (memory_end_k(ctrl, controllers) == 0) {
2291                 die("No memory\r\n");
2292         }
2293
2294         /* Before enabling memory start the memory clocks */
2295         for(i = 0; i < controllers; i++) {
2296                 uint32_t dch;
2297                 if (!controller_present(ctrl + i))
2298                         continue;
2299                 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2300                 if (dch & (DCH_MEMCLK_EN0|DCH_MEMCLK_EN1|DCH_MEMCLK_EN2|DCH_MEMCLK_EN3)) {
2301                         dch |= DCH_MEMCLK_VALID;
2302                         pci_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2303                 }
2304                 else {
2305                         /* Disable dram receivers */
2306                         uint32_t dcl;
2307                         dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2308                         dcl |= DCL_DisInRcvrs;
2309                         pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2310                 }
2311         }
2312
2313         /* And if necessary toggle the the reset on the dimms by hand */
2314         memreset(controllers, ctrl);
2315
2316         for(i = 0; i < controllers; i++) {
2317                 uint32_t dcl, dch;
2318                 if (!controller_present(ctrl + i))
2319                         continue;
2320                 /* Skip everything if I don't have any memory on this controller */
2321                 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2322                 if (!(dch & DCH_MEMCLK_VALID)) {
2323                         continue;
2324                 }
2325
2326                 /* Toggle DisDqsHys to get it working */
2327                 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2328                 if (dcl & DCL_DimmEccEn) {
2329                         uint32_t mnc;
2330                         print_spew("ECC enabled\r\n");
2331                         mnc = pci_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
2332                         mnc |= MNC_ECC_EN;
2333                         if (dcl & DCL_128BitEn) {
2334                                 mnc |= MNC_CHIPKILL_EN;
2335                         }
2336                         pci_write_config32(ctrl[i].f3, MCA_NB_CONFIG, mnc);
2337                 }
2338                 dcl |= DCL_DisDqsHys;
2339                 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2340                 dcl &= ~DCL_DisDqsHys;
2341                 dcl &= ~DCL_DLL_Disable;
2342                 dcl &= ~DCL_D_DRV;
2343                 dcl &= ~DCL_QFC_EN;
2344                 dcl |= DCL_DramInit;
2345                 pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2346
2347         }
2348         for(i = 0; i < controllers; i++) {
2349                 uint32_t dcl, dch;
2350                 if (!controller_present(ctrl + i))
2351                         continue;
2352                 /* Skip everything if I don't have any memory on this controller */
2353                 dch = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2354                 if (!(dch & DCH_MEMCLK_VALID)) {
2355                         continue;
2356                 }
2357
2358                 print_debug("Initializing memory: ");
2359
2360                 int loops = 0;
2361                 do {
2362                         dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2363                         loops += 1;
2364                         if ((loops & 1023) == 0) {
2365                                 print_debug(".");
2366                         }
2367                 } while(((dcl & DCL_DramInit) != 0) && (loops < TIMEOUT_LOOPS));
2368                 if (loops >= TIMEOUT_LOOPS) {
2369                         print_debug(" failed\r\n");
2370                         continue;
2371                 }
2372
2373                 if (!is_cpu_pre_c0()) {
2374                         /* Wait until it is safe to touch memory */
2375                         dcl &= ~(DCL_MemClrStatus | DCL_DramEnable);
2376                         pci_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2377                         do {
2378                                 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2379                         } while(((dcl & DCL_MemClrStatus) == 0) || ((dcl & DCL_DramEnable) == 0) );
2380                 }
2381
2382                 print_debug(" done\r\n");
2383         }
2384
2385 #if K8_HW_MEM_HOLE_SIZEK != 0
2386          // init hw mem hole here
2387         /* DramHoleValid bit only can be set after MemClrStatus is set by Hardware */
2388         if(!is_cpu_pre_e0())
2389                 set_hw_mem_hole(controllers, ctrl);
2390 #endif
2391
2392         //FIXME add enable node interleaving here -- yhlu
2393         /*needed?
2394                 1. check how many nodes we have , if not all has ram installed get out
2395                 2. check cs_base lo is 0, node 0 f2 0x40,,,,, if any one is not using lo is CS_BASE, get out
2396                 3. check if other node is the same as node 0 about f2 0x40,,,,, otherwise get out
2397                 4. if all ready enable node_interleaving in f1 0x40..... of every node
2398                 5. for node interleaving we need to set mem hole to every node ( need recalcute hole offset in f0 for every node)
2399         */
2400
2401 #if USE_DCACHE_RAM == 0
2402         /* Make certain the first 1M of memory is intialized */
2403         print_debug("Clearing initial memory region: ");
2404
2405         /* Use write combine caching while we setup the  first 1M */
2406         cache_lbmem(MTRR_TYPE_WRCOMB);
2407
2408         /* clear memory 1meg */
2409         clear_memory((void *)0, CONFIG_LB_MEM_TOPK << 10);
2410
2411         /* The first 1M is now setup, use it */
2412         cache_lbmem(MTRR_TYPE_WRBACK);
2413         
2414         print_debug(" done\r\n");
2415 #endif
2416 }
2417
2418 static int mem_inited(int controllers, const struct mem_controller *ctrl)
2419 {
2420         int i;
2421
2422         unsigned mask = 0;
2423         unsigned mask_inited = 0;
2424
2425         for(i = 0; i < controllers; i++) {
2426                 uint32_t dcl;
2427                 if (!controller_present(ctrl + i))
2428                         continue;
2429
2430                 mask |= (1<<i);
2431                 dcl = pci_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2432
2433                 if (!is_cpu_pre_c0()) { // B3
2434
2435                         if(  (dcl & DCL_MemClrStatus)  && (dcl & DCL_DramEnable) ) {
2436                                 mask_inited |= (1<<i);
2437                         }
2438                 }
2439         }
2440
2441         if(mask == mask_inited) return 1;
2442
2443         return 0;
2444
2445 }
2446 #if USE_DCACHE_RAM == 1
2447 static void fill_mem_ctrl(int controllers, struct mem_controller *ctrl_a, const uint16_t *spd_addr)
2448 {
2449         int i;
2450         int j;
2451         struct mem_controller *ctrl;
2452         for(i=0;i<controllers; i++) {
2453                 ctrl = &ctrl_a[i];
2454                 ctrl->node_id = i;
2455                 ctrl->f0 = PCI_DEV(0, 0x18+i, 0);
2456                 ctrl->f1 = PCI_DEV(0, 0x18+i, 1);
2457                 ctrl->f2 = PCI_DEV(0, 0x18+i, 2);
2458                 ctrl->f3 = PCI_DEV(0, 0x18+i, 3);
2459
2460                 if(spd_addr == (void *)0) continue;
2461
2462                 for(j=0;j<DIMM_SOCKETS;j++) {
2463                         ctrl->channel0[j] = spd_addr[(i*2+0)*DIMM_SOCKETS + j];
2464                         ctrl->channel1[j] = spd_addr[(i*2+1)*DIMM_SOCKETS + j];
2465                 }
2466         }
2467 }
2468 #endif