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