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