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