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