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