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