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