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