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