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