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