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