stupid ron! need to start names with a letter.
[coreboot.git] / src / northbridge / intel / i855pm / raminit.c
1
2 /* This was originally for the e7500, modified for i855pm
3  */
4
5 /* converted to C 6/2004 yhlu */
6
7 #define DEBUG_RAM_CONFIG 1
8
9 #define dumpnorth() dump_pci_device(PCI_DEV(0, 0, 0)) 
10
11 /* DDR DIMM Mode register Definitions */
12
13 #define BURST_2           (1<<0)
14 #define BURST_4           (2<<0)
15 #define BURST_8           (3<<0)
16
17 #define BURST_SEQUENTIAL  (0<<3)
18 #define BURST_INTERLEAVED (1<<3)
19
20 #define CAS_2_0           (0x2<<4)
21 #define CAS_3_0           (0x3<<4)
22 #define CAS_1_5           (0x5<<4)
23 #define CAS_2_5           (0x6<<4)
24
25 #define MODE_NORM         (0 << 7)
26 #define MODE_DLL_RESET    (2 << 7)
27 #define MODE_TEST         (1 << 7)
28
29 #define BURST_LENGTH BURST_4
30 #define BURST_TYPE   BURST_INTERLEAVED
31 #define CAS_LATENCY  CAS_2_0
32 //#define CAS_LATENCY  CAS_2_5
33 //#define CAS_LATENCY  CAS_1_5
34
35 #define MRS_VALUE (MODE_NORM | CAS_LATENCY | BURST_TYPE | BURST_LENGTH)
36 #define EMRS_VALUE 0x000
37
38 #define MD_SHIFT 4
39
40 #define RAM_COMMAND_NONE        0x0
41 #define RAM_COMMAND_NOP         0x1
42 #define RAM_COMMAND_PRECHARGE   0x2
43 #define RAM_COMMAND_MRS         0x3
44 #define RAM_COMMAND_EMRS        0x4
45 #define RAM_COMMAND_CBR         0x6
46 #define RAM_COMMAND_NORMAL      0x7
47
48
49 static inline void do_ram_command (const struct mem_controller *ctrl, uint32_t value) {
50         uint32_t dword;
51         uint8_t byte;
52         int i;
53         uint32_t result;
54 #if DEBUG_RAM_CONFIG >= 2
55         print_debug("P:");
56         print_debug_hex8(value);
57         print_debug("\r\n");
58 #endif
59         /* %ecx - initial address to read from */
60         /* Compute the offset */
61         dword = value >> 16;
62         for(i=0;i<8;i++) {
63                 /* Set the ram command */
64                 byte = pci_read_config8(ctrl->d0, 0x7c);
65                 byte &= 0x8f;
66                 byte |= (uint8_t)(value & 0xff);
67                 pci_write_config8(ctrl->d0, 0x7c, byte);
68
69                 /* Assert the command to the memory */
70 #if DEBUG_RAM_CONFIG  >= 2
71                 print_debug("R:");
72                 print_debug_hex32(dword);
73                 print_debug("\r\n");
74 #endif
75
76                 result = read32(dword);
77
78                 /* Go to the next base address */
79                 dword += 0x04000000;
80
81         } 
82
83         /* The command has been sent to all dimms so get out */
84 }
85
86
87 static inline void RAM_CMD(const struct mem_controller *ctrl, uint32_t command, uint32_t offset)  {
88         uint32_t value =        ((offset) << (MD_SHIFT + 16))|((command << 4) & 0x70) ; 
89         do_ram_command(ctrl, value);
90 }
91         
92 #define RAM_NOP(ctrl)           RAM_CMD(ctrl, RAM_COMMAND_NOP, 0)
93 #define RAM_PRECHARGE(ctrl)             RAM_CMD(ctrl, RAM_COMMAND_PRECHARGE, 0)
94 #define RAM_CBR(ctrl)           RAM_CMD(ctrl, RAM_COMMAND_CBR, 0)
95 #define RAM_EMRS(ctrl)          RAM_CMD(ctrl, RAM_COMMAND_EMRS, EMRS_VALUE)
96
97 static const uint8_t ram_cas_latency[] = {
98         CAS_2_5, CAS_2_0, CAS_1_5, CAS_2_5
99         };
100
101 static inline void ram_mrs(const struct mem_controller *ctrl, uint32_t value){
102         /* Read the cas latency setting */
103         uint8_t byte;
104         uint32_t dword;
105         byte = pci_read_config8(ctrl->d0, 0x78); 
106         /* Transform it into the form expected by SDRAM */
107         dword = ram_cas_latency[(byte>>4) & 3];
108
109         value  |= (dword<<(16+MD_SHIFT));
110         
111         value |= (MODE_NORM | BURST_TYPE | BURST_LENGTH) << (16+MD_SHIFT);
112
113         do_ram_command(ctrl, value);
114 }
115
116 #define RAM_MRS(ctrl, dll_reset) ram_mrs( ctrl, (dll_reset << (8+MD_SHIFT+ 16)) | ((RAM_COMMAND_MRS <<4)& 0x70) )
117
118 static void RAM_NORMAL(const struct mem_controller *ctrl) {
119         uint8_t byte;
120         byte = pci_read_config8(ctrl->d0, 0x7c);
121         byte &=  0x8f;
122         byte |= (RAM_COMMAND_NORMAL << 4);
123         pci_write_config8(ctrl->d0, 0x7c, byte);
124 }
125
126 static void  RAM_RESET_DDR_PTR(const struct mem_controller *ctrl) {
127         uint8_t byte;
128         byte = pci_read_config8(ctrl->d0, 0x88);
129         byte |= (1 << 4 );
130         pci_write_config8(ctrl->d0, 0x88, byte);
131         byte = pci_read_config8(ctrl->d0, 0x88);
132         byte &= ~(1 << 4);
133         pci_write_config8(ctrl->d0, 0x88, byte);
134 }
135
136 static void ENABLE_REFRESH(const struct mem_controller *ctrl) 
137 {
138         uint32_t dword;
139         dword = pci_read_config32(ctrl->d0, 0x7c);
140         dword |= (1 << 29);
141         pci_write_config32(ctrl->d0, 0x7c, dword);
142 }
143
144         /*
145          * Table:       constant_register_values
146          */
147 static const long register_values[] = {
148         /* SVID - Subsystem Vendor Identification Register
149          * 0x2c - 0x2d
150          * [15:00] Subsytem Vendor ID (Indicates system board vendor)
151          */
152         /* SID - Subsystem Identification Register
153          * 0x2e - 0x2f
154          * [15:00] Subsystem ID
155          */
156          0x2c, 0, (0x15d9 << 0) | (0x3580 << 16),
157
158         /* Undocumented
159          * 0x80 - 0x80
160          * This register has something to do with CAS latencies,
161          * possibily this is the real chipset control.
162          * At 0x00 CAS latency 1.5 works.
163          * At 0x06 CAS latency 2.5 works.
164          * At 0x01 CAS latency 2.0 works.
165          */
166         /* This is still undocumented in e7501, but with different values
167          * CAS 2.0 values taken from Intel BIOS settings, others are a guess
168          * and may be terribly wrong. Old values preserved as comments until I
169          * figure this out for sure.
170          * e7501 docs claim that CAS1.5 is unsupported, so it may or may not 
171          * work at all.
172          * Steven James 02/06/2003
173          */
174 #if CAS_LATENCY == CAS_2_5
175 //      0x80, 0xfffffe00, 0x06 /* Intel E7500 recommended */
176         0x80, 0xfffff000, 0x0662, /* from Factory Bios */
177 #elif CAS_LATENCY == CAS_2_0
178 //      0x80, 0xfffffe00, 0x0d /* values for register 0x80 */
179         0x80, 0xfffff000, 0x0bb1, /* values for register 0x80 */
180 #endif
181
182         /* Enable periodic memory recalibration */
183         0x88, 0xffffff00, 0x80,
184
185         /* FDHC - Fixed DRAM Hole Control
186          * 0x58
187          * [7:7] Hole_Enable
188          *       0 == No memory Hole
189          *       1 == Memory Hole from 15MB to 16MB
190          * [6:0] Reserved
191          *
192          * PAM - Programmable Attribute Map
193          * 0x59 [1:0] Reserved
194          * 0x59 [5:4] 0xF0000 - 0xFFFFF
195          * 0x5A [1:0] 0xC0000 - 0xC3FFF
196          * 0x5A [5:4] 0xC4000 - 0xC7FFF
197          * 0x5B [1:0] 0xC8000 - 0xCBFFF
198          * 0x5B [5:4] 0xCC000 - 0xCFFFF
199          * 0x5C [1:0] 0xD0000 - 0xD3FFF
200          * 0x5C [5:4] 0xD4000 - 0xD7FFF
201          * 0x5D [1:0] 0xD8000 - 0xDBFFF
202          * 0x5D [5:4] 0xDC000 - 0xDFFFF
203          * 0x5E [1:0] 0xE0000 - 0xE3FFF
204          * 0x5E [5:4] 0xE4000 - 0xE7FFF
205          * 0x5F [1:0] 0xE8000 - 0xEBFFF
206          * 0x5F [5:4] 0xEC000 - 0xEFFFF
207          *       00 == DRAM Disabled (All Access go to memory mapped I/O space)
208          *       01 == Read Only (Reads to DRAM, Writes to memory mapped I/O space)
209          *       10 == Write Only (Writes to DRAM, Reads to memory mapped I/O space)
210          *       11 == Normal (All Access go to DRAM)
211          */
212         0x58, 0xcccccf7f, (0x00 << 0) | (0x30 << 8) | (0x33 << 16) | (0x33 << 24),
213         0x5C, 0xcccccccc, (0x33 << 0) | (0x33 << 8) | (0x33 << 16) | (0x33 << 24),
214
215         /* DRB - DRAM Row Boundary Registers
216          * 0x60 - 0x6F
217          *     An array of 8 byte registers, which hold the ending
218          *     memory address assigned  to each pair of DIMMS, in 64MB 
219          *     granularity.   
220          */
221         /* Conservatively say each row has 64MB of ram, we will fix this up later */
222         0x60, 0x00000000, (0x01 << 0) | (0x02 << 8) | (0x03 << 16) | (0x04 << 24),
223         0x64, 0x00000000, (0x05 << 0) | (0x06 << 8) | (0x07 << 16) | (0x08 << 24),
224         0x68, 0xffffffff, 0,
225         0x6C, 0xffffffff, 0,
226
227         /* DRA - DRAM Row Attribute Register 
228          * 0x70 Row 0,1
229          * 0x71 Row 2,3
230          * 0x72 Row 4,5
231          * 0x73 Row 6,7
232          * [7:7] Device width for Odd numbered rows
233          *       0 == 8 bits wide x8
234          *       1 == 4 bits wide x4
235          * [6:4] Row Attributes for Odd numbered rows
236          *       010 == 8KB
237          *       011 == 16KB
238          *       100 == 32KB
239          *       101 == 64KB
240          *       Others == Reserved
241          * [3:3] Device width for Even numbered rows
242          *       0 == 8 bits wide x8
243          *       1 == 4 bits wide x4
244          * [2:0] Row Attributes for Even numbered rows
245          *       010 == 8KB
246          *       011 == 16KB
247          *       100 == 32KB
248          *       101 == 64KB (This page size appears broken)
249          *       Others == Reserved
250          */
251         0x70, 0x00000000, 
252                 (((0<<3)|(0<<0))<< 0) | 
253                 (((0<<3)|(0<<0))<< 4) | 
254                 (((0<<3)|(0<<0))<< 8) | 
255                 (((0<<3)|(0<<0))<<12) | 
256                 (((0<<3)|(0<<0))<<16) | 
257                 (((0<<3)|(0<<0))<<20) | 
258                 (((0<<3)|(0<<0))<<24) | 
259                 (((0<<3)|(0<<0))<<28),
260         0x74, 0xffffffff, 0,
261
262         /* DRT - DRAM Time Register
263          * 0x78
264          * [31:30] Reserved
265          * [29:29] Back to Back Write-Read Turn Around
266          *         0 == 3 clocks between WR-RD commands
267          *         1 == 2 clocks between WR-RD commands
268          * [28:28] Back to Back Read-Write Turn Around
269          *         0 == 5 clocks between RD-WR commands
270          *         1 == 4 clocks between RD-WR commands
271          * [27:27] Back to Back Read Turn Around
272          *         0 == 4 clocks between RD commands
273          *         1 == 3 clocks between RD commands
274          * [26:24] Read Delay (tRD)
275          *         000 == 7 clocks
276          *         001 == 6 clocks
277          *         010 == 5 clocks
278          *         Others == Reserved
279          * [23:19] Reserved
280          * [18:16] DRAM idle timer
281          *      000 == infinite
282          *      011 == 16 dram clocks
283          *      001 == Datasheet says reserved, but Intel BIOS sets it
284          * [15:11] Reserved
285          * [10:09] Active to Precharge (tRAS)
286          *         00 == 7 clocks
287          *         01 == 6 clocks
288          *         10 == 5 clocks
289          *         11 == Reserved
290          * [08:06] Reserved
291          * [05:04] Cas Latency (tCL)
292          *         00 == 2.5 Clocks
293          *         01 == 2.0 Clocks
294          *         10 == 1.5 Clocks
295          *         11 == Reserved
296          * [03:03] Write Ras# to Cas# Delay (tRCD)
297          *         0 == 3 DRAM Clocks
298          *         1 == 2 DRAM Clocks
299          * [02:01] Read RAS# to CAS# Delay (tRCD)
300          *         00 == reserved
301          *         01 == reserved
302          *         10 == 3 DRAM Clocks
303          *         11 == 2 DRAM Clocks
304          * [00:00] DRAM RAS# to Precharge (tRP)
305          *         0 == 3 DRAM Clocks
306          *         1 == 2 DRAM Clocks
307          */
308 #define DRT_CAS_2_5 (0<<4)
309 #define DRT_CAS_2_0 (1<<4)   
310 #define DRT_CAS_1_5 (2<<4)
311 #define DRT_CAS_MASK (3<<4)
312
313 #if CAS_LATENCY == CAS_2_5
314 #define DRT_CL DRT_CAS_2_5
315 #elif CAS_LATENCY == CAS_2_0
316 #define DRT_CL DRT_CAS_2_0
317 #elif CAS_LATENCY == CAS_1_5
318 #define DRT_CL DRT_CAS_1_5
319 #endif
320
321         /* Most aggressive settings possible */
322 //      0x78, 0xc0fff8c4, (1<<29)|(1<<28)|(1<<27)|(2<<24)|(2<<9)|DRT_CL|(1<<3)|(1<<1)|(1<<0),
323 //      0x78, 0xc0f8f8c0, (1<<29)|(1<<28)|(1<<27)|(1<<24)|(1<<16)|(2<<9)|DRT_CL|(1<<3)|(3<<1)|(1<<0),
324         0x78, 0xc0f8f9c0, (1<<29)|(1<<28)|(1<<27)|(1<<24)|(1<<16)|(2<<9)|DRT_CL|(1<<3)|(3<<1)|(1<<0),
325
326         /* FIXME why was I attempting to set a reserved bit? */
327         /* 0x0100040f */
328
329         /* DRC - DRAM Contoller Mode Register
330          * 0x7c
331          * [31:30] Reserved
332          * [29:29] Initialization Complete
333          *         0 == Not Complete
334          *         1 == Complete
335          * [28:23] Reserved
336          * [22:22]         Channels
337          *              0 == Single channel
338          *              1 == Dual Channel
339          * [21:20] DRAM Data Integrity Mode
340          *         00 == Disabled, no ECC
341          *         01 == Reserved
342          *         10 == Error checking, using chip-kill, with correction
343          *         11 == Reserved
344          * [19:18] Reserved
345          *         Must equal 01
346          * [17:17] (Intel Undocumented) should always be set to 1
347          * [16:16] Command Per Clock - Address/Control Assertion Rule (CPC)
348          *         0 == 2n Rule
349          *         1 == 1n rule
350          * [15:11] Reserved
351          * [10:08] Refresh mode select
352          *         000 == Refresh disabled
353          *         001 == Refresh interval 15.6 usec
354          *         010 == Refresh interval 7.8 usec
355          *         011 == Refresh interval 64 usec
356          *         111 == Refresh every 64 clocks (fast refresh)
357          * [07:07] Reserved
358          * [06:04] Mode Select (SMS)
359          *         000 == Self Refresh Mode
360          *         001 == NOP Command
361          *         010 == All Banks Precharge
362          *         011 == Mode Register Set
363          *         100 == Extended Mode Register Set
364          *         101 == Reserved
365          *         110 == CBR Refresh
366          *         111 == Normal Operation
367          * [03:00] Reserved
368          */
369 //      .long 0x7c, 0xffcefcff, (1<<22)|(2 << 20)|(1 << 16)| (0 << 8),
370 //      .long 0x7c, 0xff8cfcff, (1<<22)|(2 << 20)|(1 << 17)|(1 << 16)| (0 << 8),
371 //      .long 0x7c, 0xff80fcff, (1<<22)|(2 << 20)|(1 << 18)|(1 << 17)|(1 << 16)| (0 << 8),
372         0x7c, 0xff82fcff, (1<<22)|(2 << 20)|(1 << 18)|(1 << 16)| (0 << 8),
373
374
375         /* Another Intel undocumented register */
376         0x88, 0x080007ff, (1<<31)|(1 << 30)|(1<<28)|(0 << 26)|(0x10 << 21)|(10 << 16)|(0x13 << 11),
377
378         /* CLOCK_DIS - CK/CK# Disable Register
379          * 0x8C
380          * [7:4] Reserved
381          * [3:3] CK3
382          *       0 == Enable
383          *       1 == Disable
384          * [2:2] CK2
385          *       0 == Enable
386          *       1 == Disable
387          * [1:1] CK1
388          *       0 == Enable
389          *       1 == Disable
390          * [0:0] CK0
391          *       0 == Enable
392          *       1 == Disable
393          */
394         0x8C, 0xfffffff0, 0xf,
395
396         /* TOLM - Top of Low Memory Register
397          * 0xC4 - 0xC5
398          * [15:11] Top of low memory (TOLM)
399          *         The address below 4GB that should be treated as RAM,
400          *         on a 128MB granularity.
401          * [10:00] Reserved
402          */
403         /* REMAPBASE - Remap Base Address Regsiter
404          * 0xC6 - 0xC7
405          * [15:10] Reserved
406          * [09:00] Remap Base Address [35:26] 64M aligned
407          *         Bits [25:0] are assumed to be 0.
408          */
409         0xc4, 0xfc0007ff, (0x2000 << 0) | (0x3ff << 16),
410         /* REMAPLIMIT - Remap Limit Address Register
411          * 0xC8 - 0xC9
412          * [15:10] Reserved
413          * [09:00] Remap Limit Address [35:26] 64M aligned
414          * When remaplimit < remapbase this register is disabled.
415          */
416         0xc8, 0xfffffc00, 0,
417
418         /* DVNP - Device Not Present Register
419          * 0xE0 - 0xE1
420          * [15:05] Reserved
421          * [04:04] Device 4 Function 1 Present
422          *         0 == Present
423          *         1 == Absent
424          * [03:03] Device 3 Function 1 Present
425          *         0 == Present
426          *         1 == Absent
427          * [02:02] Device 2 Function 1 Present
428          *         0 == Present
429          *         1 == Absent
430          * [01:01] Reserved
431          * [00:00] Device 0 Function 1 Present
432          *         0 == Present
433          *         1 == Absent
434          */
435         0xe0, 0xffffffe2, (1<<4)|(1<<3)|(1<<2)|(0<<0),
436         0xd8, 0xffff9fff, 0x00000000,
437         0xf4, 0x3f8ffffd, 0x40300002,
438         0x1050, 0xffffffcf, 0x00000030,
439 };
440
441
442         /*
443          * Routine:     ram_set_registers
444          * Arguments:   none
445          * Results:     none
446          * Trashed:     %eax, %ebx, %ecx, %edx, %esi, %eflags
447          * Effects:     Do basic ram setup that does not depend on serial
448          *              presence detect information.
449          *              This sets PCI configuration registers to known good
450          *              values based on the table: 
451          *                      constant_register_values
452          *              Which are a triple of configuration regiser, mask, and value.
453          *              
454          */
455 /* from 1M or 512K */
456 #define RCOMP_MMIO 0x100000
457
458         /* DDR RECOMP table */
459
460 static const long ddr_rcomp_1[] = {
461         0x44332211, 0xc9776655, 0xffffffff, 0xffffffff,
462         0x22111111, 0x55444332, 0xfffca876, 0xffffffff,
463 };
464 static const long ddr_rcomp_2[] = {
465         0x00000000, 0x76543210, 0xffffeca8, 0xffffffff,
466         0x21000000, 0xa8765432, 0xffffffec, 0xffffffff,
467 };
468 static const long ddr_rcomp_3[] = {
469         0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
470         0x88888888, 0x88888888, 0x88888888, 0x88888888,
471 };
472
473 #define rcomp_init_str "Setting RCOMP registers.\r\n"
474
475 static void write_8dwords(uint32_t src_addr, uint32_t dst_addr) {
476         int i;
477         uint32_t dword;
478         for(i=0;i<8;i++) {
479                 dword = read32(src_addr);
480                 write32(dst_addr, dword);
481                 src_addr+=4;
482                 dst_addr+=4;
483                 
484         }
485 }
486
487 //#define SLOW_DOWN_IO inb(0x80);
488 #define SLOW_DOWN_IO udelay(40);
489
490 static void ram_set_rcomp_regs(const struct mem_controller *ctrl) {
491         uint32_t dword;
492 #if DEBUG_RAM_CONFIG
493         print_debug(rcomp_init_str);
494 #endif
495
496         /*enable access to the rcomp bar */
497         dword = pci_read_config32(ctrl->d0, 0x0f4);
498         dword &= ~(1<<31);
499         dword |=((1<<30)|1<<22);
500         pci_write_config32(ctrl->d0, 0x0f4, dword);
501         
502
503         /* Set the MMIO address to 512K */
504         pci_write_config32(ctrl->d0, 0x14, RCOMP_MMIO);
505
506         dword = read32(RCOMP_MMIO + 0x20);
507         dword |= (1<<9);
508         write32(RCOMP_MMIO + 0x20, dword);
509         
510
511         /* Begin to write the RCOMP registers */
512         
513         write8(RCOMP_MMIO + 0x2c, 0xff);
514         write32(RCOMP_MMIO + 0x30, 0x01040444);
515         write8(RCOMP_MMIO + 0x34, 0x04);
516         write32(RCOMP_MMIO + 0x40, 0);
517         write16(RCOMP_MMIO + 0x44, 0);
518         write16(RCOMP_MMIO + 0x48, 0);
519         write16(RCOMP_MMIO + 0x50, 0);
520         write_8dwords((uint32_t)ddr_rcomp_1, RCOMP_MMIO + 0x60);
521         write_8dwords((uint32_t)ddr_rcomp_2, RCOMP_MMIO + 0x80);
522         write_8dwords((uint32_t)ddr_rcomp_2, RCOMP_MMIO + 0xa0);
523         write_8dwords((uint32_t)ddr_rcomp_2, RCOMP_MMIO + 0x140);
524         write_8dwords((uint32_t)ddr_rcomp_2, RCOMP_MMIO + 0x1c0);
525         write_8dwords((uint32_t)ddr_rcomp_3, RCOMP_MMIO + 0x180);
526
527 #if 0   /* Print the RCOMP registers */
528         movl    $RCOMP_MMIO, %ecx
529 1:      movl    %ecx, %eax
530         andb    $0x0f, %al
531         jnz     2f
532         CONSOLE_INFO_TX_CHAR($'\r')
533         CONSOLE_INFO_TX_CHAR($'\n')
534         CONSOLE_INFO_TX_HEX32(%ecx)
535         CONSOLE_INFO_TX_CHAR($' ')
536         CONSOLE_INFO_TX_CHAR($'-')
537         CONSOLE_INFO_TX_CHAR($' ')
538 2:      movl    (%ecx), %eax
539         CONSOLE_INFO_TX_HEX32(%eax)
540         CONSOLE_INFO_TX_CHAR($' ')
541         addl    $4, %ecx
542         cmpl    $(RCOMP_MMIO + 0x1e0), %ecx
543         jnz     1b
544         CONSOLE_INFO_TX_CHAR($'\r')
545         CONSOLE_INFO_TX_CHAR($'\n')
546 #endif
547
548         dword = read32(RCOMP_MMIO + 0x20);
549         dword &= ~(3);
550         dword |= 1;
551         write32(RCOMP_MMIO + 0x20, dword);
552
553         /* Wait 40 usec */
554         SLOW_DOWN_IO;
555         
556         /* unblock updates */
557         dword = read32(RCOMP_MMIO + 0x20);
558         dword &= ~(1<<9);
559         write32(RCOMP_MMIO+0x20, dword);
560         dword |= (1<<8);
561         write32(RCOMP_MMIO+0x20, dword);
562         dword &= ~(1<<8);
563         write32(RCOMP_MMIO+0x20, dword);
564         
565         /* Wait 40 usec */
566         SLOW_DOWN_IO;
567
568         /*disable access to the rcomp bar */
569         dword = pci_read_config32(ctrl->d0, 0x0f4);
570         dword &= ~(1<<22);
571         pci_write_config32(ctrl->d0, 0x0f4, dword);     
572
573 }
574
575 static void ram_set_d0f0_regs(const struct mem_controller *ctrl) {
576 #if DEBUG_RAM_CONFIG
577         dumpnorth();
578 #endif
579         int i;
580         int max;
581         max = sizeof(register_values)/sizeof(register_values[0]);
582         for(i = 0; i < max; i += 3) {
583                 uint32_t reg;
584 #if DEBUG_RAM_CONFIG
585                 print_debug_hex32(register_values[i]);
586                 print_debug(" <-");
587                 print_debug_hex32(register_values[i+2]);
588                 print_debug("\r\n");
589 #endif
590                 reg = pci_read_config32(ctrl->d0,register_values[i]);
591                 reg &= register_values[i+1];
592                 reg |= register_values[i+2] & ~(register_values[i+1]);
593                 pci_write_config32(ctrl->d0,register_values[i], reg);
594
595
596         }
597 #if DEBUG_RAM_CONFIG
598         dumpnorth();
599 #endif
600 }
601 static void sdram_set_registers(const struct mem_controller *ctrl){
602         ram_set_rcomp_regs(ctrl);
603         ram_set_d0f0_regs(ctrl);
604 }
605
606
607         /*
608          * Routine:     sdram_spd_get_page_size
609          * Arguments:   %bl SMBUS_MEM_DEVICE
610          * Results:     
611          *              %edi log base 2 page size of DIMM side 1 in bits
612          *              %esi log base 2 page size of DIMM side 2 in bits
613          *
614          * Preserved:   %ebx (except %bh), %ebp
615          *
616          * Trashed:     %eax, %bh, %ecx, %edx, %esp, %eflags
617          * Used:        %eax, %ebx, %ecx, %edx, %esi, %edi, %esp, %eflags
618          *
619          * Effects:     Uses serial presence detect to set %edi & %esi
620          *              to the page size of a dimm.
621          * Notes:
622          *              %bl SMBUS_MEM_DEVICE
623          *              %edi holds the page size for the first side of the DIMM.
624          *              %esi holds the page size for the second side of the DIMM.
625          *                   memory size is represent as a power of 2.
626          *
627          *              This routine may be worth moving into generic code somewhere.
628          */
629 struct dimm_page_size { 
630         unsigned long side1;
631         unsigned long side2;
632 };      
633   
634 static struct dimm_page_size sdram_spd_get_page_size(unsigned device) {
635
636         uint32_t ecx;
637         int value;
638         struct dimm_page_size pgsz;
639
640         pgsz.side1 = 0;
641         pgsz.side2 = 0; 
642                 
643         value  = spd_read_byte(device, 4); /* columns */
644         if(value < 0) goto hw_err;
645         pgsz.side1 = value & 0xf;
646         
647         /* Get the module data width and convert it to a power of two */
648         value = spd_read_byte(device,7);                /* (high byte) */
649         if(value < 0) goto hw_err;
650         ecx = value & 0xff;
651         ecx <<= 8;
652
653         value = spd_read_byte(device, 6);        /* (low byte) */
654         if(value < 0) goto hw_err;
655         ecx |= (value & 0xff);
656
657         pgsz.side1 += log2(ecx);         /* compute cheap log base 2 */ 
658
659         /* side two */
660         value = spd_read_byte(device, 5);       /* number of physical banks */
661         if(value < 0) goto hw_err;
662         if(value==1) goto out;
663         if(value!=2) goto val_err;
664
665         /* Start with the symmetrical case */
666         pgsz.side2 = pgsz.side1;
667         value = spd_read_byte(device,4);   /* columns */
668         if(value < 0) goto hw_err;
669         if((value & 0xf0)==0 ) goto out;
670         pgsz.side2 -=value & 0xf; /* Subtract out columns on side 1 */
671         pgsz.side2 +=(value>>4)& 0xf; /* Add in columns on side 2 */
672         goto out;
673
674  val_err:
675         die("Bad SPD value\r\n");
676         /* If an hw_error occurs report that I have no memory */
677 hw_err:
678         pgsz.side1 = 0;
679         pgsz.side2 = 0;
680 out:
681         return pgsz;    
682 }
683
684
685         /*
686          * Routine:     sdram_spd_get_width
687          * Arguments:   %bl SMBUS_MEM_DEVICE
688          * Results:     
689          *              %edi width of SDRAM chips on DIMM side 1 in bits
690          *              %esi width of SDRAM chips on DIMM side 2 in bits
691          *
692          * Preserved:   %ebx (except %bh), %ebp
693          *
694          * Trashed:     %eax, %bh, %ecx, %edx, %esp, %eflags
695          * Used:        %eax, %ebx, %ecx, %edx, %esi, %edi, %esp, %eflags
696          *
697          * Effects:     Uses serial presence detect to set %edi & %esi
698          *              to the width of a dimm.
699          * Notes:
700          *              %bl SMBUS_MEM_DEVICE
701          *              %edi holds the width for the first side of the DIMM.
702          *              %esi holds the width for the second side of the DIMM.
703          *                   memory size is represent as a power of 2.
704          *
705          *              This routine may be worth moving into generic code somewhere.
706          */
707 struct dimm_width {
708         unsigned side1;
709         unsigned side2;
710 };      
711   
712 static struct dimm_width sdram_spd_get_width(unsigned device) {
713         int value;
714         struct dimm_width wd;
715         uint32_t ecx;
716         
717         wd.side1 = 0;
718         wd.side2 = 0;
719
720         value = spd_read_byte(device, 13); /* sdram width */
721         if(value < 0 )  goto hw_err;
722         ecx = value;
723         
724         wd.side1 = value & 0x7f;        
725         
726         /* side two */
727         value = spd_read_byte(device, 5); /* number of physical banks */
728         if(value < 0 ) goto hw_err;     
729         if(value <=1 ) goto out;
730
731         /* Start with the symmetrical case */
732         wd.side2 = wd.side1;
733
734         if((ecx & 0x80)==0) goto out;
735         
736         wd.side2 <<=1;
737 hw_err:
738         wd.side1 = 0;
739         wd.side2 = 0;
740
741  out:
742         return wd;
743 }
744         
745         /*
746          * Routine:     sdram_spd_get_dimm_size
747          * Arguments:   %bl SMBUS_MEM_DEVICE
748          * Results:     
749          *              %edi log base 2 size of DIMM side 1 in bits
750          *              %esi log base 2 size of DIMM side 2 in bits
751          *
752          * Preserved:   %ebx (except %bh), %ebp
753          *
754          * Trashed:     %eax, %bh, %ecx, %edx, %esp, %eflags
755          * Used:        %eax, %ebx, %ecx, %edx, %esi, %edi, %esp, %eflags
756          *
757          * Effects:     Uses serial presence detect to set %edi & %esi
758          *              the size of a dimm.
759          * Notes:
760          *              %bl SMBUS_MEM_DEVICE
761          *              %edi holds the memory size for the first side of the DIMM.
762          *              %esi holds the memory size for the second side of the DIMM.
763          *                   memory size is represent as a power of 2.
764          *
765          *              This routine may be worth moving into generic code somewhere.
766          */
767
768 struct dimm_size {
769         unsigned long side1;
770         unsigned long side2;
771 };
772
773 static struct dimm_size spd_get_dimm_size(unsigned device)
774 {
775         /* Calculate the log base 2 size of a DIMM in bits */
776         struct dimm_size sz;
777         int value, low;
778         sz.side1 = 0;
779         sz.side2 = 0;
780
781         /* Note it might be easier to use byte 31 here, it has the DIMM size as
782          * a multiple of 4MB.  The way we do it now we can size both
783          * sides of an assymetric dimm.
784          */
785         value = spd_read_byte(device, 3);       /* rows */
786         if (value < 0) goto hw_err;
787 //        if ((value & 0xf) == 0) goto val_err;
788         sz.side1 += value & 0xf;
789
790         value = spd_read_byte(device, 4);       /* columns */
791         if (value < 0) goto hw_err;
792 //        if ((value & 0xf) == 0) goto val_err;
793         sz.side1 += value & 0xf;
794
795         value = spd_read_byte(device, 17);      /* banks */
796         if (value < 0) goto hw_err;
797 //        if ((value & 0xff) == 0) goto val_err;
798         value &=0xff;
799         sz.side1 += log2(value);
800
801         /* Get the module data width and convert it to a power of two */
802         value = spd_read_byte(device, 7);       /* (high byte) */
803         if (value < 0) goto hw_err;
804         value &= 0xff;
805         value <<= 8;
806         
807         low = spd_read_byte(device, 6); /* (low byte) */
808         if (low < 0) goto hw_err;
809         value |= (low & 0xff);
810 //        if ((value != 72) && (value != 64)) goto val_err;
811         sz.side1 += log2(value);
812         
813         /* side 2 */
814         value = spd_read_byte(device, 5);       /* number of physical banks */
815         if (value < 0) goto hw_err;
816         if (value == 1) goto out;
817 //        if (value != 2) goto val_err;
818
819         /* Start with the symmetrical case */
820         sz.side2 = sz.side1;
821
822         value = spd_read_byte(device, 3);       /* rows */
823         if (value < 0) goto hw_err;
824         if ((value & 0xf0) == 0) goto out;      /* If symmetrical we are done */
825         sz.side2 -= (value & 0x0f);             /* Subtract out rows on side 1 */
826         sz.side2 += ((value >> 4) & 0x0f);      /* Add in rows on side 2 */
827
828         value = spd_read_byte(device, 4);       /* columns */
829         if (value < 0) goto hw_err;
830 //        if ((value & 0xff) == 0) goto val_err;
831         sz.side2 -= (value & 0x0f);             /* Subtract out columns on side 1 */
832         sz.side2 += ((value >> 4) & 0x0f);      /* Add in columsn on side 2 */
833         goto out;
834
835  val_err:
836         die("Bad SPD value\r\n");
837         /* If an hw_error occurs report that I have no memory */
838 hw_err:
839         sz.side1 = 0;
840         sz.side2 = 0;
841  out:
842         return sz;
843 }
844
845
846
847         /*
848          * This is a place holder fill this out
849          * Routine:     spd_set_row_attributes 
850          * Arguments:   %bl SMBUS_MEM_DEVICE
851          * Results:     
852          *              %edi log base 2 size of DIMM side 1 in bits
853          *              %esi log base 2 size of DIMM side 2 in bits
854          *
855          * Preserved:   %ebx (except %bh), %ebp
856          *
857          * Trashed:     %eax, %bh, %ecx, %edx, %esp, %eflags
858          * Used:        %eax, %ebx, %ecx, %edx, %esi, %edi, %esp, %eflags
859          *
860          * Effects:     Uses serial presence detect to set %edi & %esi
861          *              the size of a dimm.
862          * Notes:
863          *              %bl SMBUS_MEM_DEVICE
864          *              %edi holds the memory size for the first side of the DIMM.
865          *              %esi holds the memory size for the second side of the DIMM.
866          *                   memory size is represent as a power of 2.
867          *
868          *              This routine may be worth moving into generic code somewhere.
869          */
870 static long spd_set_row_attributes(const struct mem_controller *ctrl, long dimm_mask) {
871         int i;  
872         uint32_t dword=0;
873         int value;
874         
875
876         /* Walk through all dimms and find the interesection of the support
877          * for ecc sdram and refresh rates
878          */        
879         
880  
881         for(i = 0; i < DIMM_SOCKETS; i++) {
882                 if (!(dimm_mask & (1 << i))) {
883                         continue;
884                 }
885                  /* Test to see if I have ecc sdram */
886                 struct dimm_page_size sz;
887                 sz = sdram_spd_get_page_size(ctrl->channel0[i]);  /* SDRAM type */
888 #if DEBUG_RAM_CONFIG
889                 print_debug("page size =");
890                 print_debug_hex32(sz.side1);
891                 print_debug(" ");
892                 print_debug_hex32(sz.side2);
893                 print_debug("\r\n");
894 #endif
895         
896                 /* Test to see if the dimm is present */
897                 if( sz.side1 !=0) {
898
899                         /* Test for a valid dimm width */
900                         if((sz.side1 <15) || (sz.side1>18) ) {
901                                 print_err("unsupported page size\r\n");
902                         }
903
904                         /* double because I have 2 channels */
905                         sz.side1++;
906
907                         /* Convert to the format needed for the DRA register */
908                         sz.side1-=14;   
909
910                         /* Place in the %ebp the dra place holder */ //i
911                         dword |= sz.side1<<(i<<3);
912                         
913                         /* Test to see if the second side is present */
914
915                         if( sz.side2 !=0) {
916         
917                                 /* Test for a valid dimm width */
918                                 if((sz.side2 <15) || (sz.side2>18) ) {
919                                         print_err("unsupported page size\r\n");
920                                 }
921
922                                 /* double because I have 2 channels */
923                                 sz.side2++;
924
925                                 /* Convert to the format needed for the DRA register */
926                                 sz.side2-=14;
927
928                                 /* Place in the %ebp the dra place holder */ //i
929                                 dword |= sz.side2<<((i<<3) + 4 );
930
931                         }
932                 }
933
934                 /* Now add the SDRAM chip width to the DRA */
935                 struct dimm_width wd;
936                 wd = sdram_spd_get_width(ctrl->channel0[i]);
937
938 #if DEBUG_RAM_CONFIG
939                 print_debug("width =");
940                 print_debug_hex32(wd.side1);
941                 print_debug(" ");
942                 print_debug_hex32(wd.side2);
943                 print_debug("\r\n");
944 #endif
945                 
946                 if(wd.side1 == 0) continue;
947                 if(wd.side1 == 4) {
948                         /* Enable an x4 device */
949                         dword |= 0x08 << (i<<3);
950                 }
951
952                 if(wd.side2 == 0) continue;
953                 if(wd.side2 == 4) {
954                         /* Enable an x4 device */
955                         dword |= 0x08 << ((i<<3 ) + 4);
956                 }
957                 
958         /* go to the next DIMM */
959         }
960
961         /* Write the new row attributes register */
962         pci_write_config32(ctrl->d0, 0x70, dword);
963
964         return dimm_mask;
965
966 }
967 #if 0
968         /*
969          * Routine:     sdram_read_paired_byte
970          * Arguments:   %esp return address
971          *              %bl device on the smbus to read from
972          *              %bh address on the smbus to read
973          * Results:     
974          *              zf clear
975          *              byte read in %al
976          *      On Error:
977          *              zf set
978          *              %eax trashed
979          *
980          * Preserved:   %ebx, %esi, %edi
981          *
982          * Trashed:     %eax, %ecx, %edx, %ebp, %esp, %eflags
983          * Used:        %eax, %ebx, %ecx, %edx, %esp, %eflags
984          *
985          * Effects:     Reads two spd bytes from both ram channesl
986          *              and errors if they are not equal.
987          *              It then returns the equal result.
988          */
989 static spd_read_paired_byte () {
990         movl    %esp, %ebp
991         CALLSP(smbus_read_byte)
992         setnz   %cl
993         movb    %al, %ch
994         addb    $(SMBUS_MEM_CHANNEL_OFF), %bl
995         CALLSP(smbus_read_byte)
996         movb    %ch, %ah
997         setnz   %ch
998         subb    $(SMBUS_MEM_CHANNEL_OFF), %bl
999
1000         /* See if dimms on both sides are equally present */    
1001         cmp     %cl, %ch
1002         jne     sdram_presence_mismatch
1003
1004         /* Leave if I have no data */
1005         testb   %cl, %cl
1006         jz      spd_verify_byte_out
1007
1008         /* Verify the data is identical */
1009         cmp     %ah, %al
1010         jne     sdram_value_mismatch
1011
1012         /* Clear the zero flag */
1013         testb   %cl, %cl
1014 spd_verify_byte_out:
1015         movl    %ebp, %esp
1016         RETSP
1017 }
1018
1019         /*
1020          * Routine:     spd_verify_dimms
1021          * Arguments:   none
1022          * Results:     none
1023          * Preserved:   none
1024          * Trashed:     %eax, %ebx, %ecx, %edx, %ebp, %esi, %edi, %esp, %eflags
1025          * Used:        %eax, %ebx, %ecx, %edx, %ebp, %esi, %edi, %esp, %eflags
1026          *
1027          * Effects:     
1028          *              - Verify all interesting spd information
1029          *                matches for both dimm channels.
1030          *              - Additional error checks that can be easily done
1031          *                here are computed as well, so I don't need to
1032          *                worry about them later.
1033          */
1034 static spd_verify_dimms() {
1035         movl    $(SMBUS_MEM_DEVICE_START), %ebx
1036 spd_verify_dimm:
1037         /* Verify this is DDR SDRAM */
1038         movb    $2, %bh
1039         CALLSP(spd_read_paired_byte)
1040         jz      spd_verify_next_dimm
1041         cmpb    $7, %al
1042         jne     invalid_dimm_type
1043
1044         /* Verify the row addresses */
1045         movb    $3, %bh
1046         CALLSP(spd_read_paired_byte)
1047         jz      spd_missing_data
1048         testb   $0x0f, %al
1049         jz      spd_invalid_data
1050         
1051         /* Column addresses */
1052         movb    $4, %bh
1053         CALLSP(spd_read_paired_byte)
1054         jz      spd_missing_data
1055         testb   $0xf, %al
1056         jz      spd_invalid_data
1057
1058         /* Physical Banks */
1059         movb    $5, %bh
1060         CALLSP(spd_read_paired_byte)
1061         jz      spd_missing_data
1062         cmp     $1, %al
1063         jb      spd_invalid_data
1064         cmp     $2, %al
1065         ja      spd_invalid_data
1066
1067         /* Module Data Width */
1068         movb    $7, %bh
1069         CALLSP(spd_read_paired_byte)    
1070         jz      spd_missing_data
1071         cmpb    $0, %al
1072         jne     spd_invalid_data
1073
1074         movb    $6, %bh
1075         CALLSP(spd_read_paired_byte)
1076         jz      spd_missing_data
1077         cmpb    $64, %al
1078         je      1f
1079         cmpb    $72, %al
1080         je      1f
1081         jmp     spd_unsupported_data
1082 1:
1083
1084         /* Cycle time at highest CAS latency CL=X */
1085         movb    $9, %bh
1086         CALLSP(spd_read_paired_byte)
1087         jz      spd_missing_data
1088
1089         /* SDRAM type */
1090         movb    $11, %bh
1091         CALLSP(spd_read_paired_byte)
1092         jz      spd_missing_data
1093
1094         /* Refresh Interval */
1095         movb    $12, %bh
1096         CALLSP(spd_read_paired_byte)
1097         jz      spd_missing_data
1098         
1099         /* SDRAM Width */
1100         movb    $13, %bh
1101         CALLSP(spd_read_paired_byte)
1102         jz      spd_missing_data
1103         andb    $0x7f, %al
1104         cmpb    $4, %al
1105         je      1f
1106         cmpb    $8, %al
1107         je      1f
1108         jmp     spd_unsupported_data
1109 1:
1110
1111         /* Back-to-Back Random Column Accesses */
1112         movb    $15, %bh
1113         CALLSP(spd_read_paired_byte)
1114         jz      spd_missing_data
1115         testb   %al, %al
1116         jz      spd_invalid_data
1117         cmpb    $4, %al
1118         ja      spd_unsupported_data
1119
1120         /* Burst Lengths */
1121         movb    $16, %bh
1122         CALLSP(spd_read_paired_byte)
1123         jz      spd_missing_data
1124         testb   $(1<<2), %al
1125         jz      spd_unsupported_data
1126
1127         /* Logical Banks */
1128         movb    $17, %bh
1129         CALLSP(spd_read_paired_byte)
1130         jz      spd_missing_data
1131         testb   %al, %al
1132         jz      spd_invalid_data
1133         
1134         /* Supported CAS Latencies */
1135         movb    $18, %bh
1136         CALLSP(spd_read_paired_byte)
1137         jz      spd_missing_data
1138         testb   $(1 << 1), %al /* CL 1.5 */
1139         jnz     1f
1140         testb   $(1 << 2), %al /* CL 2.0 */
1141         jnz     1f
1142         testb   $(1 << 3), %al /* CL 2.5 */
1143         jnz     1f
1144         jmp     spd_unsupported_data
1145 1:
1146         
1147         /* Cycle time at Cas Latency (CLX - 0.5) */
1148         movb    $23, %bh
1149         CALLSP(spd_read_paired_byte)
1150         jz      spd_missing_data
1151         
1152         /* Cycle time at Cas Latency (CLX - 1.0) */
1153         movb    $26, %bh
1154         CALLSP(spd_read_paired_byte)
1155         jz      spd_missing_data
1156         
1157         /* tRP Row precharge time */
1158         movb    $27, %bh
1159         CALLSP(spd_read_paired_byte)
1160         jz      spd_missing_data
1161         testb   $0xfc, %al
1162         jz      spd_invalid_data
1163         
1164
1165         /* tRCD RAS to CAS */
1166         movb    $29, %bh
1167         CALLSP(spd_read_paired_byte)
1168         jz      spd_missing_data
1169         testb   $0xfc, %al
1170         jz      spd_invalid_data
1171         
1172         /* tRAS Activate to Precharge */
1173         movb    $30, %bh
1174         CALLSP(spd_read_paired_byte)
1175         jz      spd_missing_data
1176         testb   %al, %al
1177         jz      spd_invalid_data
1178
1179         /* Module Bank Density */
1180         movb    $31, %bh
1181         CALLSP(spd_read_paired_byte)
1182         jz      spd_missing_data
1183         testb   $(1<<2), %al            /* 16MB */
1184         jnz     spd_unsupported_data
1185         testb   $(1<<3), %al
1186         jnz     spd_unsupported_data    /* 32MB */
1187         
1188         /* Address and Command Hold Time After Clock */
1189         movb    $33, %bh
1190         CALLSP(spd_read_paired_byte)
1191         jz      spd_missing_data
1192
1193 spd_verify_next_dimm:
1194         /* go to the next DIMM */
1195         addb    $(SMBUS_MEM_DEVICE_INC), %bl /* increment the smbus device */
1196         cmpb    $SMBUS_MEM_DEVICE_END, %bl
1197         jbe     spd_verify_dimm
1198 spd_verify_dimms_out:
1199         RET_LABEL(spd_verify_dimms)
1200 }
1201 #endif
1202 #define spd_pre_init  "Reading SPD data...\r\n"
1203 #define spd_pre_set "setting based on SPD data...\r\n"
1204 #define spd_post_init "done\r\n"
1205
1206
1207 static const uint32_t refresh_rate_rank[]= {
1208         /* Refresh rates ordered from most conservative (lowest)
1209          * to most agressive (highest)
1210          * disabled 0 -> rank 3 
1211          * 15.6usec 1 -> rank 1
1212          * 7.8 usec 2 -> rank 0
1213          * 64usec   3 -> rank 2
1214          */
1215         3, 1, 0, 2 };
1216 static const uint32_t refresh_rate_index[] = {
1217         /* Map the spd refresh rates to memory controller settings 
1218          * 15.625us -> 15.6us
1219          * 3.9us    -> err
1220          * 7.8us    -> 7.8us
1221          * 31.3s    -> 15.6us
1222          * 62.5us   -> 15.6us
1223          * 125us    -> 64us
1224          */
1225         1, 0xff, 2, 1, 1, 3
1226 };
1227 #define MAX_SPD_REFRESH_RATE 5
1228
1229 static long spd_set_dram_controller_mode (const struct mem_controller *ctrl, long dimm_mask) {
1230
1231         int i;  
1232         uint32_t dword;
1233         int value;
1234         uint32_t ecx;
1235         uint32_t edx;
1236         
1237         /* Read the inititial state */
1238         dword = pci_read_config32(ctrl->d0, 0x7c);
1239
1240 #if 0
1241         /* Test if ECC cmos option is enabled */
1242         movb    $RTC_BOOT_BYTE, %al
1243         outb    %al, $0x70
1244         inb     $0x71, %al
1245         testb   $(1<<2), %al
1246         jnz     1f
1247         /* Clear the ecc enable */
1248         andl    $~(3 << 20), %esi
1249 1:
1250 #endif
1251
1252
1253         /* Walk through all dimms and find the interesection of the support
1254          * for ecc sdram and refresh rates
1255          */        
1256
1257  
1258         for(i = 0; i < DIMM_SOCKETS; i++) {
1259                 if (!(dimm_mask & (1 << i))) {
1260                         continue;
1261                 }
1262                  /* Test to see if I have ecc sdram */
1263                 value = spd_read_byte(ctrl->channel0[i], 11);  /* SDRAM type */
1264                 if(value < 0) continue;
1265                 if(value !=2 ) {
1266                         /* Clear the ecc enable */
1267                         dword &= ~(3 << 20);
1268                 }
1269                 value = spd_read_byte(ctrl->channel0[i], 12);  /* SDRAM refresh rate */
1270                 if(value < 0 ) continue;
1271                 value &= 0x7f;
1272                 if(value > MAX_SPD_REFRESH_RATE) { print_err("unsupported refresh rate\r\n");}
1273 //              if(value == 0xff) { print_err("unsupported refresh rate\r\n");}
1274                 
1275                 ecx = refresh_rate_index[value];
1276
1277                 /* Isolate the old refresh rate setting */
1278                 /* Load the refresh rate ranks */
1279                 edx = refresh_rate_rank[(dword >> 8) & 3]<<8;
1280                 edx |= refresh_rate_rank[ecx] & 0xff; 
1281         
1282                 /* See if the new refresh rate is more conservative than the old
1283                 * refresh rate setting. (Lower ranks are more conservative)
1284                 */
1285                 if((edx & 0xff)< ((edx >> 8) & 0xff) ) {
1286                         /* Clear the old refresh rate */
1287                         dword &= ~(3<<8);
1288                         /* Move in the new refresh rate */
1289                         dword |= (ecx<<8);
1290                 }
1291                 
1292                 value = spd_read_byte(ctrl->channel0[i], 33); /* Address and command hold time after clock */
1293                 if(value < 0) continue;
1294                 if(value >= 0xa0) {             /* At 133Mhz this constant should be 0x75 */
1295                         dword &= ~(1<<16);      /* Use two clock cyles instead of one */
1296                 }
1297         
1298         /* go to the next DIMM */
1299         }
1300
1301         /* Now write the controller mode */
1302         pci_write_config32(ctrl->d0, 0x7c, dword);
1303         
1304         return dimm_mask;
1305
1306 }
1307 static long spd_enable_clocks(const struct mem_controller *ctrl, long dimm_mask)
1308 {
1309         int i;
1310         uint32_t dword;
1311         int value;
1312
1313         /* Read the inititial state */
1314         dword = pci_read_config32(ctrl->d0, 0x8c);
1315 #if 0
1316 # Intel clears top bit here, should we?
1317 # No the default is on and for normal timming it should be on.  Tom Z
1318         andl    $0x7f, %esi
1319 #endif
1320
1321  
1322         for(i = 0; i < DIMM_SOCKETS; i++) {
1323                 if (!(dimm_mask & (1 << i))) {
1324                         continue;
1325                 }
1326                 /* Read any spd byte to see if the dimm is present */
1327                 value = spd_read_byte(ctrl->channel0[i], 5); /* Physical Banks */
1328                 if(value < 0) continue;
1329         
1330                 dword &= ~(1<<i);
1331         }
1332         
1333         pci_write_config32(ctrl->d0, 0x8c, dword);
1334         
1335         return dimm_mask;
1336 }
1337
1338 static const uint16_t cas_latency_80[] = {
1339         /* For cas latency 2.0 0x01 works and until I see a large test sample
1340          * I am not prepared to change this value, to the intel recommended value
1341          * of 0x0d.  Eric Biederman
1342          */
1343         /* The E7501 requires b1 rather than 01 for CAS2 or memory will be hosed
1344          * CAS 1.5 is claimed to be unsupported, will try to test that
1345          * will need to determine correct values for other CAS values
1346          * (perhaps b5, b1, b6?)
1347          * Steven James 02/06/2003
1348          */
1349          
1350 //#     .byte 0x05, 0x01, 0x06 
1351 //#     .byte 0xb5, 0xb1, 0xb6 
1352         0x0, 0x0bb1, 0x0662   /* RCVEN */ 
1353 };
1354 static const uint16_t cas_latency_80_4dimms[] = {
1355         0x0, 0x0bb1, 0x0882
1356 };
1357
1358
1359 static const uint8_t cas_latency_78[] = {
1360         DRT_CAS_1_5, DRT_CAS_2_0, DRT_CAS_2_5
1361 };
1362
1363 static long spd_set_cas_latency(const struct mem_controller *ctrl, long dimm_mask) {
1364         /* Walk through all dimms and find the interesection of the
1365          * supported cas latencies.
1366          */
1367         int i;
1368         /* Initially allow cas latencies 2.5, 2.0
1369          * which the chipset supports.
1370          */
1371         uint32_t dword = (1<<3)| (1<<2);// esi
1372         uint32_t edi;
1373         uint32_t ecx;
1374         unsigned device;
1375         int value;
1376         uint8_t byte;
1377         uint16_t word;
1378
1379         
1380         for(i = 0; i < DIMM_SOCKETS; i++) {
1381                 if (!(dimm_mask & (1 << i))) {
1382                         continue;
1383                 }
1384                 value = spd_read_byte(ctrl->channel0[i], 18);
1385                 if(value < 0) continue;
1386                 /* Find the highest supported cas latency */
1387                 ecx = log2(value & 0xff); 
1388                 edi = (1<< ecx);
1389
1390                  /* Remember the supported cas latencies */
1391                 ecx = (value & 0xff);
1392
1393                 /* Verify each cas latency at 133Mhz */
1394                 /* Verify slowest/highest CAS latency */
1395                 value = spd_read_byte(ctrl->channel0[i], 9);
1396                 if(value < 0 ) continue;
1397                 if(value > 0x75 ) {     
1398                         /* The bus is too fast so we cannot support this case latency */
1399                         ecx &= ~edi;
1400                 }
1401
1402                 /* Verify the highest CAS latency - 0.5 clocks */
1403                 edi >>= 1;
1404                 if(edi != 0) {
1405                         value = spd_read_byte(ctrl->channel0[i], 23);
1406                         if(value < 0 ) continue;
1407                         if(value > 0x75) {
1408                                 /* The bus is too fast so we cannot support this cas latency */
1409                                 ecx &= ~edi;
1410                         }
1411                 }
1412
1413                 /* Verify the highest CAS latency - 1.0 clocks */
1414                 edi >>=1;
1415                 if(edi !=0) {
1416                         value = spd_read_byte(ctrl->channel0[i], 25);
1417                         if(value < 0 ) continue;
1418                         if(value > 0x75) {
1419                                 /* The bus is too fast so we cannot support this cas latency */
1420                                 ecx &= ~edi;
1421                         }
1422                 }
1423
1424                 /* Now find which cas latencies are supported for the bus */
1425                 dword &= ecx;
1426         /* go to the next DIMM */
1427         }
1428
1429         /* After all of the arduous calculation setup with the fastest
1430          * cas latency I can use.
1431          */
1432         value = __builtin_bsf(dword);  // bsrl = log2 how about bsfl?
1433         if(value ==0 ) return -1;
1434         ecx = value -1;
1435
1436         byte = pci_read_config8(ctrl->d0, 0x78);
1437         byte &= ~(DRT_CAS_MASK);
1438         byte |= cas_latency_78[ecx];
1439         pci_write_config8(ctrl->d0,0x78, byte);
1440
1441         /* set master DLL reset */
1442         dword = pci_read_config32(ctrl->d0, 0x88);
1443         dword |= (1<<26);
1444         
1445         /* the rest of the references are words */
1446 //      ecx<<=1;  // don't need shift left, because we already define that in u16 array
1447         pci_write_config32(ctrl->d0, 0x88, dword);
1448         
1449         
1450         dword &= 0x0c0000ff;    /* patch try register 88 is undocumented tnz */
1451         dword |= 0xd2109800;
1452
1453         pci_write_config32(ctrl->d0, 0x88, dword);
1454         
1455         word = pci_read_config16(ctrl->d0, 0x80);
1456         word &= ~(0x0fff);
1457         word |= cas_latency_80[ecx];
1458
1459         dword = pci_read_config32(ctrl->d0, 0x70);
1460
1461         if((dword & 0xff) !=0 ) {
1462                 dword >>=8;
1463                 if((dword & 0xff)!=0) {
1464                         dword >>=8;
1465                         if((dword & 0xff)!=0) {
1466                                 dword >>= 8;
1467                                 if( (dword & 0xff)!=0) {
1468                                         word &=~(0x0fff);  /* we have dimms in all 4 slots */ 
1469                                         word |=cas_latency_80_4dimms[ecx];
1470                                 }
1471                         }
1472                 }
1473         }
1474         
1475         pci_write_config16(ctrl->d0, 0x80, word);
1476         
1477         dword = pci_read_config32(ctrl->d0, 0x88);      /* reset master DLL reset */
1478         dword &= ~(1<<26);
1479         pci_write_config32(ctrl->d0, 0x88, dword);
1480         
1481         RAM_RESET_DDR_PTR(ctrl);
1482
1483         return dimm_mask;
1484
1485 }
1486
1487 static long spd_set_dram_timing(const struct mem_controller *ctrl, long dimm_mask) {
1488         /* Walk through all dimms and find the interesection of the
1489          * supported dram timings.
1490          */
1491
1492         int i;
1493         uint32_t dword;
1494         int value;
1495
1496         /* Read the inititial state */
1497         dword = pci_read_config32(ctrl->d0, 0x78);
1498 #if 0
1499 # Intel clears top bit here, should we?
1500 # No the default is on and for normal timming it should be on.  Tom Z
1501         andl    $0x7f, %esi
1502 #endif
1503         
1504         
1505         for(i = 0; i < DIMM_SOCKETS; i++) {
1506                 if (!(dimm_mask & (1 << i))) {
1507                         continue;
1508                 }
1509                 /* Trp */
1510                 value = spd_read_byte(ctrl->channel0[i], 27);
1511                 if(value < 0) continue;
1512                 if(value > (15<<2)) {
1513                         /* At 133Mhz if row precharge time is above than 15ns than we
1514                          * need 3 clocks not 2 clocks.
1515                         */
1516                         dword &= ~(1<<0); 
1517                 }
1518                 /*  Trcd */
1519                 value = spd_read_byte(ctrl->channel0[i],29);
1520                 if(value < 0 ) continue;
1521                 if(value > (15<<2)) {
1522                         /* At 133Mhz if the Minimum ras to cas delay is about 15ns we
1523                          * need 3 clocks not 2 clocks.
1524                         */
1525                         dword &= ~((1<<3)|(1<<1));
1526                 }
1527                 /* Tras */
1528                 value = spd_read_byte(ctrl->channel0[i],30);
1529                 if(value < 0 ) continue;
1530                         /* Convert tRAS from ns to 133Mhz clock cycles */
1531                 value <<=1;      /* mult by 2 to make 7.5 15 */
1532                 value  += 15;   /* Make certain we round up */
1533                 value --;
1534                 value &= 0xff;  /* Clear the upper bits of eax */
1535                 value /= 15;
1536                 
1537                 /* Don't even process small timings */
1538                 if(value >5) {
1539                         uint32_t tmp;
1540                         /* Die if the value is to large */
1541                         if(value>7) {
1542                                 die ("unsupported_rcd\r\n");
1543                         }
1544                         /* Convert to clocks - 5 */
1545                         value -=5;
1546                         /* Convert the existing value into clocks - 5 */
1547                         tmp = (~((dword>>9) & 3) - 1) & 3;
1548                         /* See if we need a slower timing */
1549                         if(value > tmp ) {
1550                                 /* O.k. put in our slower timing */
1551                                 dword &= ~(3<<9);
1552                                 dword |= ((~(value + 1)) & 3)<<9 ;
1553                         }
1554                 }
1555         
1556                 /* Trd */ 
1557                 /* Set to a 7 clock read delay. This is for 133Mhz
1558                 *  with a CAS latency of 2.5  if 2.0 a 6 clock
1559                 *  delay is good  */
1560                 if( (pci_read_config8(ctrl->d0, 0x78) & 0x30) ==0 ){
1561                         dword &= ~(7<<24); /* CAS latency is 2.5, make 7 clks */
1562                 }
1563
1564                 /*
1565                 * Back to Back Read Turn Around
1566                 */
1567                 /* Set to a 3 clock back to back read turn around.  This
1568                  *  is good for CAS latencys 2.5 and 2.0 */
1569                 dword |= (1<<27);
1570                 /*
1571                 * Back to Back Read-Write Turn Around
1572                 */
1573                 /* Set to a 5 clock back to back read to write turn around.
1574                 *  4 is a good delay if the CAS latency is 2.0 */
1575                 if( ( pci_read_config8(ctrl->d0, 0x78) & (1<<4)) == 0) {
1576                         dword &= ~(1<<28);
1577                 }
1578                 /*
1579                 * Back to Back Write-Read Turn Around
1580                 */
1581                 /* Set to a 2 clock back to back write to read turn around.
1582                 *  This is good for 2.5 and 2.0 CAS Latencies. */
1583                 dword |= (1<<29);
1584         }
1585         
1586         pci_write_config32(ctrl->d0, 0x78, dword);
1587         
1588         return dimm_mask;
1589
1590 }
1591 static unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
1592 {               
1593         unsigned dimm_mask;
1594         int i;  
1595         dimm_mask = 0;  
1596 #if DEBUG_RAM_CONFIG
1597         print_debug("spd_detect_dimms:\r\n");
1598 #endif
1599         for(i = 0; i < DIMM_SOCKETS; i++) {
1600                 int byte;
1601                 unsigned device;
1602 #if DEBUG_RAM_CONFIG
1603                 print_debug_hex32(i);
1604                 print_debug("\r\n");
1605 #endif
1606                 device = ctrl->channel0[i];
1607                 if (device) {
1608                         byte = spd_read_byte(ctrl->channel0[i], 2);  /* Type */
1609                         if (byte == 7) {
1610                                 dimm_mask |= (1 << i);
1611                         }
1612                 }
1613 #if 1
1614                 device = ctrl->channel1[i];
1615                 if (device) {
1616                         byte = spd_read_byte(ctrl->channel1[i], 2);
1617                         if (byte == 7) {
1618                                 dimm_mask |= (1 << (i + DIMM_SOCKETS));
1619                         }
1620                 }
1621 #endif
1622         }       
1623 #if 1
1624         i = (dimm_mask>>DIMM_SOCKETS);
1625         if(i != (dimm_mask & ( (1<<DIMM_SOCKETS) - 1) ) ) {
1626                 die("now we only support dual channel\r\n");
1627         }
1628
1629 #endif
1630
1631         return dimm_mask;
1632 }               
1633
1634 static uint32_t set_dimm_size(const struct mem_controller *ctrl, struct dimm_size sz, uint32_t memsz, unsigned index)
1635 {
1636         int i;
1637         uint32_t base0, base1;
1638         uint32_t dch;
1639         uint8_t byte;
1640
1641         /* Double the size if we are using dual channel memory */
1642 //        if (is_dual_channel(ctrl)) {
1643                 /* Since I have 2 identical channels double the sizes */
1644                 sz.side1++ ;
1645                 sz.side2++;
1646 //        }
1647               
1648         if (sz.side1 != sz.side2) {
1649                 sz.side2 = 0;
1650         } 
1651  
1652         /* Make certain side1 of the dimm is at least 64MB */
1653         if (sz.side1 >= (25 + 4)) {
1654                 memsz += (1 << (sz.side1 - (25 + 4)) ) ;
1655         }
1656          /* Write the size of side 1 of the dimm */
1657         byte = memsz;
1658         pci_write_config8(ctrl->d0, 0x60+(index<<1), byte);
1659
1660         /* Make certain side2 of the dimm is at least 64MB */
1661         if (sz.side2 >= (25 + 4)) {
1662                 memsz += (1 << (sz.side2 - (25 + 4)) ) ;
1663         }
1664         
1665          /* Write the size of side 2 of the dimm */
1666         byte = memsz;
1667         pci_write_config8(ctrl->d0, 0x61+(index<<1), byte);
1668         
1669         /* now, fill in DRBs where no physical slot exists */
1670         
1671         for(i=index+1;i<4;i++) {
1672                 pci_write_config8(ctrl->d0, 0x60+(i<<1),byte);
1673                 pci_write_config8(ctrl->d0, 0x61+(i<<1),byte);
1674         
1675         }
1676         
1677         return memsz;
1678
1679 }
1680 /* LAST_DRB_SLOT is a constant for any E7500 board */
1681 #define LAST_DRB_SLOT 0x67
1682
1683 static long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask)
1684 {
1685         int i;
1686         uint32_t memsz=0;
1687         uint16_t word;
1688
1689         for(i = 0; i < DIMM_SOCKETS; i++) {
1690                 struct dimm_size sz;
1691                 if (!(dimm_mask & (1 << i))) {
1692                         continue;
1693                 }
1694                 sz = spd_get_dimm_size(ctrl->channel0[i]);
1695 #if DEBUG_RAM_CONFIG
1696                 print_debug("dimm size =");
1697                 print_debug_hex32(sz.side1);
1698                 print_debug(" ");
1699                 print_debug_hex32(sz.side2);
1700                 print_debug("\r\n");
1701 #endif
1702
1703                 if (sz.side1 == 0) {
1704                         return -1; /* Report SPD error */
1705                 }
1706                 memsz = set_dimm_size(ctrl, sz, memsz, i);
1707         }
1708         /* For now hardset everything at 128MB boundaries */
1709         /* %ebp has the ram size in multiples of 64MB */
1710 //        cmpl    $0, %ebp        /* test if there is no mem - smbus went bad */
1711 //        jz      no_memory_bad_smbus
1712         if(memsz < 0x30)  {
1713                 /* I should really adjust all of this in C after I have resources
1714                 * to all of the pcie devices.
1715                 */
1716
1717                 /* Round up to 128M granularity */
1718                 memsz++;
1719                 memsz &= 0xfe;
1720                 memsz<<= 10;
1721                 word = memsz;
1722                 pci_write_config16(ctrl->d0, 0xc4, word);
1723         } else {
1724
1725                 /* FIXME will this work with 3.5G of ram? */
1726                 /* Put TOLM at 3G */
1727                 pci_write_config16(ctrl->d0, 0xc4, 0xc000);
1728                 /* Hard code a 1G remap window, right after the ram */
1729                 if(memsz< 0x40){
1730                         word = 0x40;  /* Ensure we are over 4G */
1731                 } else {
1732                         word = memsz;
1733                 }
1734                 pci_write_config16(ctrl->d0, 0xc6, word);
1735                 word += 0x10;
1736                 pci_write_config16(ctrl->d0, 0xc8, word);
1737
1738         }
1739
1740         return dimm_mask;
1741 }
1742                                                 
1743 static void sdram_set_spd_registers(const struct mem_controller *ctrl) {
1744         long dimm_mask;
1745 #if DEBUG_RAM_CONFIG
1746         print_debug(spd_pre_init);
1747 #endif
1748         //activate_spd_rom(ctrl);
1749         dimm_mask = spd_detect_dimms(ctrl);
1750         if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
1751                 print_debug("No memory for this controller\n");
1752                 return;
1753         }
1754         dimm_mask = spd_enable_clocks(ctrl, dimm_mask);
1755         if (dimm_mask < 0)
1756                 goto hw_spd_err;
1757         //spd_verify_dimms(ctrl);
1758 #if DEBUG_RAM_CONFIG
1759         print_debug(spd_pre_set);
1760 #endif
1761         dimm_mask = spd_set_row_attributes(ctrl,dimm_mask);
1762         if (dimm_mask < 0)
1763                 goto hw_spd_err;
1764         dimm_mask = spd_set_dram_controller_mode(ctrl,dimm_mask);
1765         if (dimm_mask < 0)
1766                 goto hw_spd_err;        
1767         dimm_mask = spd_set_cas_latency(ctrl,dimm_mask);
1768         if (dimm_mask < 0)
1769                 goto hw_spd_err;
1770         dimm_mask = spd_set_dram_timing(ctrl,dimm_mask);
1771         if (dimm_mask < 0)
1772                 goto hw_spd_err;
1773 #if DEBUG_RAM_CONFIG
1774         print_debug(spd_post_init);
1775 #endif
1776         //moved from dram_post_init
1777         spd_set_ram_size(ctrl, dimm_mask);
1778                 return;
1779  hw_spd_err:
1780         /* Unrecoverable error reading SPD data */
1781         print_err("SPD error - reset\r\n");
1782         hard_reset();
1783         return;
1784 }
1785
1786
1787         /* I have finally seen ram bad enough to cause LinuxBIOS
1788          * to die in mysterious ways, before booting up far
1789          * enough to run a memory tester.  This code attempts
1790          * to catch this blatantly bad ram, with a spot check.
1791          * For most cases you should boot all of the way up 
1792          * and run a memory tester.  
1793          */
1794         /* Ensure I read/write each stick of bank of memory &&
1795          * that I do more than 1000 bytes to avoid the northbridge cache.
1796          * Only 64M of each side of each DIMM is currently mapped,
1797          * so we can handle > 4GB of ram here.
1798          */
1799 #if 0
1800 #define bank_msg  "Bank "
1801 #define side_msg " Side "
1802 static void verify_ram() {
1803         xorl    %ecx, %ecx
1804         /* Check to see if the RAM is present,
1805          * in the specified bank and side.
1806          */
1807 1:      movl    %ecx, %ebx
1808         shrl    $1, %ebx
1809         addl    $((5<<8) | SMBUS_MEM_DEVICE_START), %ebx
1810         CALLSP(smbus_read_byte)
1811         jz      5f
1812         testl   $1, %ecx
1813         jz      2f
1814         cmpb    $2, %al
1815         jne     5f
1816
1817         /* Display the bank and side we are spot checking.
1818          */
1819 2:      CONSOLE_INFO_TX_STRING($bank_msg)
1820         movl    %ecx, %ebx
1821         shrl    $1, %ebx
1822         incl    %ebx
1823         CONSOLE_INFO_TX_HEX8(%bl)
1824         CONSOLE_INFO_TX_STRING($side_msg)
1825         movl    %ecx, %ebx
1826         andl    $1, %ebx
1827         CONSOLE_INFO_TX_HEX8(%bl)
1828
1829         /* Compute the memory address to spot check. */
1830         movl    %ecx, %ebx
1831         xorl    %eax, %eax
1832 3:      testl   %ebx, %ebx
1833         jz      4f
1834         addl    $0x04000000, %eax
1835         decl    %ebx
1836         jmp     3b
1837 4:
1838         /* Spot check 512K of RAM */
1839         movl    %eax, %ebx
1840         addl    $0x0007ffff, %ebx
1841         CALLSP(spot_check)
1842 5:      
1843         /* Now find the next bank and side to spot check */
1844         incl    %ecx
1845         cmpl    $((SMBUS_MEM_DEVICE_END - SMBUS_MEM_DEVICE_START)<<1), %ecx
1846         jb      1b
1847         RET_LABEL(verify_ram)
1848
1849 }
1850 #endif  
1851
1852 #if 0
1853 static void ram_postinit(const struct mem_controller *ctrl) {
1854 #if DEBUG_RAM_CONFIG 
1855         dumpnorth();
1856 #endif
1857         /* Include a test to verify that memory is more or less working o.k. 
1858          * This test is to catch programming errors and hardware that is out of
1859          * spec, not a test to see if the memory dimms are working 100%
1860          */
1861 //#     CALL_LABEL(verify_ram)
1862         spd_set_ram_size(ctrl);
1863 }
1864 #define FIRST_NORMAL_REFERENCE() CALL_LABEL(ram_postinit)
1865
1866 #define SPECIAL_FINISHUP()   CALL_LABEL(dram_finish)
1867
1868 #endif
1869
1870 #define ecc_pre_init    "Initializing ECC state...\r\n"
1871 #define ecc_post_init   "ECC state initialized.\r\n"
1872 static void dram_finish(const struct mem_controller *ctrl)
1873 {
1874         uint32_t dword;
1875         uint8_t byte;
1876         /* Test to see if ECC support is enabled */
1877         dword = pci_read_config32(ctrl->d0, 0x7c);
1878         dword >>=20;
1879         dword &=3;
1880         if(dword == 2)  {
1881                 
1882 #if DEBUG_RAM_CONFIG    
1883                 print_debug(ecc_pre_init);
1884 #endif
1885                 /* Initialize ECC bits , use ECC zero mode (new to 7501)*/
1886                 pci_write_config8(ctrl->d0, 0x52, 0x06);
1887                 pci_write_config8(ctrl->d0, 0x52, 0x07);
1888                 do {
1889                         byte = pci_read_config8(ctrl->d0, 0x52);
1890
1891                 } while ( (byte & 0x08 ) == 0);
1892
1893                 pci_write_config8(ctrl->d0, 0x52, byte & 0xfc);
1894 #if DEBUG_RAM_CONFIG            
1895                 print_debug(ecc_post_init);     
1896 #endif
1897
1898                 /* Clear the ECC error bits */
1899                 pci_write_config8(ctrl->d0f1, 0x80, 0x03); /* dev 0, function 1, offset 80 */
1900                 pci_write_config8(ctrl->d0f1, 0x82, 0x03); /* dev 0, function 1, offset 82 */
1901
1902                 pci_write_config32(ctrl->d0f1, 0x40, 1<<18); /* clear dev 0, function 1, offset 40; bit 18 by writing a 1 to it */
1903                 pci_write_config32(ctrl->d0f1, 0x44, 1<<18); /* clear dev 0, function 1, offset 44; bit 18 by writing a 1 to it */
1904
1905                 pci_write_config8(ctrl->d0, 0x52, 0x0d);
1906         }
1907         
1908         dword = pci_read_config32(ctrl->d0, 0x7c); /* FCS_EN */
1909         dword |= (1<<17);
1910         pci_write_config32(ctrl->d0, 0x7c, dword);
1911
1912
1913 #if DEBUG_RAM_CONFIG 
1914         dumpnorth();
1915 #endif
1916
1917 //      verify_ram();
1918 }
1919 #if 0
1920 #define ERRFUNC(x, str) mem_err(x, str)
1921
1922
1923 ERRFUNC(invalid_dimm_type,          "Invalid dimm type")
1924 ERRFUNC(spd_missing_data,           "Missing sdram spd data")
1925 ERRFUNC(spd_invalid_data,           "Invalid sdram spd data")
1926 ERRFUNC(spd_unsupported_data,       "Unsupported sdram spd value")
1927 ERRFUNC(unsupported_page_size,      "Unsupported page size")
1928 ERRFUNC(sdram_presence_mismatch,    "DIMM presence mismatch")
1929 ERRFUNC(sdram_value_mismatch,       "spd data does not match")
1930 ERRFUNC(unsupported_refresh_rate,   "Unsuported spd refresh rate")
1931 ERRFUNC(inconsistent_cas_latencies, "No cas latency supported by all dimms")
1932 ERRFUNC(unsupported_rcd,            "Unsupported ras to cas delay")
1933 #undef ERRFUNC
1934
1935 #define mem_err_err "ERROR: "
1936 #define mem_err_pair " on dimm pair "
1937 #define mem_err_byte " spd byte "
1938 static void mem_err {
1939         movl    %ebx, %edi
1940         CONSOLE_ERR_TX_STRING($mem_err_err)
1941         CONSOLE_ERR_TX_STRING(%esi)
1942         CONSOLE_ERR_TX_STRING($mem_err_pair)
1943         movl    %edi, %ebx
1944         subb    $(SMBUS_MEM_DEVICE_START), %bl
1945         CONSOLE_ERR_TX_HEX8(%bl)
1946         CONSOLE_ERR_TX_STRING($mem_err_byte)
1947         movl    %edi, %ebx
1948         CONSOLE_ERR_TX_HEX8(%bh)
1949         jmp     mem_stop
1950
1951 }
1952
1953 #endif
1954
1955
1956 #if ASM_CONSOLE_LOGLEVEL > BIOS_DEBUG
1957 #define ram_enable_1    "Ram Enable 1\r\n"
1958 #define ram_enable_2    "Ram Enable 2\r\n"
1959 #define ram_enable_3    "Ram Enable 3\r\n"
1960 #define ram_enable_4    "Ram Enable 4\r\n"
1961 #define ram_enable_5    "Ram Enable 5\r\n"
1962 #define ram_enable_6    "Ram Enable 6\r\n"
1963 #define ram_enable_7    "Ram Enable 7\r\n"
1964 #define ram_enable_8    "Ram Enable 8\r\n"
1965 #define ram_enable_9    "Ram Enable 9\r\n"
1966 #define ram_enable_10   "Ram Enable 10\r\n"
1967 #define ram_enable_11   "Ram Enable 11\r\n"
1968 #endif
1969
1970         /* Estimate that SLOW_DOWN_IO takes about 50&76us*/
1971         /* delay for 200us */
1972
1973 #define DO_DELAY \
1974         udelay(200);
1975 //      for(i=0; i<16;i++)  {   SLOW_DOWN_IO    }
1976                 
1977
1978 #define EXTRA_DELAY DO_DELAY
1979
1980 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
1981 {
1982         int i;
1983         /* 1 & 2 Power up and start clocks */
1984 #if DEBUG_RAM_CONFIG 
1985         print_debug(ram_enable_1);
1986         print_debug(ram_enable_2);
1987 #endif
1988
1989         /* A 200us delay is needed */
1990
1991         DO_DELAY
1992         EXTRA_DELAY
1993
1994         /* 3. Apply NOP */
1995 #if DEBUG_RAM_CONFIG 
1996         print_debug(ram_enable_3);
1997 #endif
1998         RAM_NOP(ctrl);
1999         EXTRA_DELAY
2000
2001         /* 4 Precharge all */
2002 #if DEBUG_RAM_CONFIG 
2003         print_debug(ram_enable_4);
2004 #endif
2005         RAM_PRECHARGE(ctrl);
2006         EXTRA_DELAY
2007         
2008         /* wait until the all banks idle state... */
2009         /* 5. Issue EMRS to enable DLL */
2010 #if DEBUG_RAM_CONFIG 
2011         print_debug(ram_enable_5);
2012 #endif
2013         RAM_EMRS(ctrl);
2014         EXTRA_DELAY
2015         
2016         /* 6. Reset DLL */
2017 #if DEBUG_RAM_CONFIG 
2018         print_debug(ram_enable_6);
2019 #endif
2020         RAM_MRS(ctrl,1);
2021         EXTRA_DELAY
2022
2023         /* Ensure a 200us delay between the DLL reset in step 6 and the final
2024          * mode register set in step 9.
2025          * Infineon needs this before any other command is sent to the ram.
2026          */
2027         DO_DELAY
2028         EXTRA_DELAY
2029         
2030         /* 7 Precharge all */
2031 #if DEBUG_RAM_CONFIG 
2032         print_debug(ram_enable_7);
2033 #endif
2034         RAM_PRECHARGE(ctrl);
2035         EXTRA_DELAY
2036         
2037         /* 8 Now we need 2 AUTO REFRESH / CBR cycles to be performed */
2038 #if DEBUG_RAM_CONFIG 
2039         print_debug(ram_enable_8);
2040 #endif
2041         RAM_CBR(ctrl);
2042         EXTRA_DELAY
2043         RAM_CBR(ctrl);
2044         EXTRA_DELAY
2045         /* And for good luck 6 more CBRs */
2046         RAM_CBR(ctrl);
2047         EXTRA_DELAY
2048         RAM_CBR(ctrl);
2049         EXTRA_DELAY
2050         RAM_CBR(ctrl);
2051         EXTRA_DELAY
2052         RAM_CBR(ctrl);
2053         EXTRA_DELAY
2054         RAM_CBR(ctrl);
2055         EXTRA_DELAY
2056         RAM_CBR(ctrl);
2057         EXTRA_DELAY
2058
2059         /* 9 mode register set */
2060 #if DEBUG_RAM_CONFIG 
2061         print_debug(ram_enable_9);
2062 #endif
2063         RAM_MRS(ctrl,0);
2064         EXTRA_DELAY
2065         
2066         /* 10 DDR Receive FIFO RE-Sync */
2067 #if DEBUG_RAM_CONFIG 
2068         print_debug(ram_enable_10);
2069 #endif
2070         RAM_RESET_DDR_PTR(ctrl);
2071         EXTRA_DELAY
2072         
2073         /* 11 normal operation */
2074 #if DEBUG_RAM_CONFIG 
2075         print_debug(ram_enable_11);
2076 #endif
2077         RAM_NORMAL(ctrl);
2078
2079
2080         // special from v1
2081         //FIRST_NORMAL_REFERENCE();
2082         //spd_set_ram_size(ctrl, 0x03);
2083
2084         /* Finally enable refresh */
2085         ENABLE_REFRESH(ctrl);
2086
2087         //SPECIAL_FINISHUP();
2088         dram_finish(ctrl);
2089
2090 }
2091