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