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