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