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