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