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