Revision: linuxbios@linuxbios.org--devel/freebios--devel--2.0--patch-38
[coreboot.git] / src / northbridge / via / vt8601 / raminit.c
1 #include <cpu/x86/mtrr.h>
2 #include "raminit.h"
3
4 /*
5 This software and ancillary information (herein called SOFTWARE )
6 called LinuxBIOS          is made available under the terms described
7 here.  The SOFTWARE has been approved for release with associated
8 LA-CC Number 00-34   .  Unless otherwise indicated, this SOFTWARE has
9 been authored by an employee or employees of the University of
10 California, operator of the Los Alamos National Laboratory under
11 Contract No. W-7405-ENG-36 with the U.S. Department of Energy.  The
12 U.S. Government has rights to use, reproduce, and distribute this
13 SOFTWARE.  The public may copy, distribute, prepare derivative works
14 and publicly display this SOFTWARE without charge, provided that this
15 Notice and any statement of authorship are reproduced on all copies.
16 Neither the Government nor the University makes any warranty, express 
17 or implied, or assumes any liability or responsibility for the use of
18 this SOFTWARE.  If SOFTWARE is modified to produce derivative works,
19 such modified SOFTWARE should be clearly marked, so as not to confuse
20 it with the version available from LANL.
21  */
22 /* Copyright 2000, Ron Minnich, Advanced Computing Lab, LANL
23  * rminnich@lanl.gov
24  */
25 /*
26  * 11/26/02 - kevinh@ispiri.com - The existing comments implied that
27  * this didn't work yet.  Therefore, I've updated it so that it works
28  * correctly - at least on my VIA epia motherboard.  64MB DIMM in slot 0.
29  */
30
31 /* Added automatic detection of first equipped bank and its MA mapping type.
32  * (Rest of configuration is done in C)
33  * 5/19/03 by SONE Takeshi <ts1@tsn.or.jp>
34  */
35 /* converted to C 9/2003 Ron Minnich */
36
37 /* Set to 1 if your DIMMs are PC133 Note that I'm assuming CPU's FSB
38  * frequency is 133MHz. If your CPU runs at another bus speed, you
39  * might need to change some of register values.
40  */
41 #ifndef DIMM_PC133
42 #define DIMM_PC133 0
43 #endif
44
45 // Set to 1 if your DIMMs are CL=2
46 #ifndef DIMM_CL2
47 #define DIMM_CL2 0
48 #endif
49
50 void dimms_read(unsigned long x) 
51 {
52         uint8_t c;
53         unsigned long eax; 
54         volatile unsigned long y;
55         eax =  x;
56         for(c = 0; c < 6; c++) {
57                 y = * (volatile unsigned long *) eax;
58                 eax += 0x10000000;
59         }
60 }
61
62 void dimms_write(int x) 
63 {
64         uint8_t c;
65         unsigned long eax = x;
66         for(c = 0; c < 6; c++) {
67                 *(volatile unsigned long *) eax = 0;
68                 eax += 0x10000000;
69         }
70 }
71
72
73
74 #ifdef DEBUG_SETNORTHB
75 void setnorthb(device_t north, uint8_t reg, uint8_t val) 
76 {
77         print_debug("setnorth: reg ");
78         print_debug_hex8(reg);
79         print_debug(" to ");
80         print_debug_hex8(val);
81         print_debug("\r\n");
82         pci_write_config8(north, reg, val);
83 }
84 #else
85 #define setnorthb pci_write_config8
86 #endif
87
88 void
89 dumpnorth(device_t north) 
90 {
91         unsigned int r, c;
92         for(r = 0; ; r += 16) {
93                 print_debug_hex8(r);
94                 print_debug(":");
95                 for(c = 0; c < 16; c++) {
96                         print_debug_hex8(pci_read_config8(north, r+c));
97                         print_debug(" ");
98                 }
99                 print_debug("\r\n");
100                 if (r >= 240)
101                         break;
102   }
103 }
104
105 static void sdram_set_registers(const struct mem_controller *ctrl) 
106 {
107         device_t north = (device_t) 0;
108         uint8_t c, r;
109
110         print_err("vt8601 init starting\r\n");
111         north = pci_locate_device(PCI_ID(0x1106, 0x8601), 0);
112         north = 0;
113         print_debug_hex32(north);
114         print_debug(" is the north\n");
115         print_debug_hex16(pci_read_config16(north, 0));
116         print_debug(" ");
117         print_debug_hex16(pci_read_config16(north, 2));
118         print_debug("\r\n");
119         
120         /* All we are doing now is setting initial known-good values that will
121          * be revised later as we read SPD
122          */     
123         // memory clk enable. We are not using ECC
124         pci_write_config8(north,0x78, 0x01);
125         print_debug_hex8(pci_read_config8(north, 0x78));
126         // dram control, see the book. 
127 #if DIMM_PC133
128         pci_write_config8(north,0x68, 0x52);
129 #else
130         pci_write_config8(north,0x68, 0x42);
131 #endif
132         // dram control, see the book. 
133         pci_write_config8(north,0x6B, 0x0c);
134         // Initial setting, 256MB in each bank, will be rewritten later.
135         pci_write_config8(north,0x5A, 0x20);
136         print_debug_hex8(pci_read_config8(north, 0x5a));
137         pci_write_config8(north,0x5B, 0x40);
138         pci_write_config8(north,0x5C, 0x60);
139         pci_write_config8(north,0x5D, 0x80);
140         pci_write_config8(north,0x5E, 0xA0);
141         pci_write_config8(north,0x5F, 0xC0);
142         // It seems we have to take care of these 2 registers as if 
143         // they are bank 6 and 7.
144         pci_write_config8(north,0x56, 0xC0);
145         pci_write_config8(north,0x57, 0xC0);
146         
147         // SDRAM in all banks
148         pci_write_config8(north,0x60, 0x3F);
149         // DRAM timing. I'm suspicious of this
150         // This is for all banks, 64 is 0,1.  65 is 2,3. 66 is 4,5.
151         // ras precharge 4T, RAS pulse 5T
152         // cas2 is 0xd6, cas3 is 0xe6
153         // we're also backing off write pulse width to 2T, so result is 0xee
154 #if DIMM_CL2
155         pci_write_config8(north,0x64, 0xd4);
156         pci_write_config8(north,0x65, 0xd4);
157         pci_write_config8(north,0x66, 0xd4);
158 #else // CL=3
159         pci_write_config8(north,0x64, 0xe4);
160         pci_write_config8(north,0x65, 0xe4);
161         pci_write_config8(north,0x66, 0xe4);
162 #endif
163
164         // dram frequency select.
165         // enable 4K pages for 64M dram. 
166 #if DIMM_PC133
167         pci_write_config8(north,0x69, 0x3c);
168 #else
169         pci_write_config8(north,0x69, 0xac);
170 #endif
171
172         /* IMPORTANT -- disable refresh counter */
173         // refresh counter, disabled.
174         pci_write_config8(north,0x6A, 0x00);
175         
176
177         // clkenable configuration. kevinh FIXME - add precharge
178         pci_write_config8(north,0x6C, 0x00);
179         // dram read latch delay of 1 ns, MD drive 8 mA,
180         // high drive strength on MA[2: 13], we#, cas#, ras#
181         // As per Cindy Lee, set to 0x37, not 0x57
182         pci_write_config8(north,0x6D, 0x7f);
183 }
184
185 /* slot is the dram slot. Return size of side0 in lower 16-bit,
186  * side1 in upper 16-bit, in units of 8MB */
187 static unsigned long 
188 spd_module_size(unsigned char slot) 
189
190         /* for all the DRAMS, see if they are there and get the size of each
191          * module. This is just a very early first cut at sizing.
192          */
193         /* we may run out of registers ... */
194         unsigned int banks, rows, cols, reg;
195         unsigned int value = 0;
196         /* unsigned int module = ((0x50 + slot) << 1) + 1; */
197         unsigned int module = 0x50 + slot;
198         /* is the module there? if byte 2 is not 4, then we'll assume it 
199          * is useless. 
200          */
201         print_info("Slot "); 
202         print_info_hex8(slot); 
203         if (smbus_read_byte(module, 2) != 4) {
204                 print_info(" is empty\r\n");
205                 return 0;
206         }
207         print_info(" is SDRAM ");
208         
209         banks = smbus_read_byte(module, 17);
210         /* we're going to assume symmetric banks. Sorry. */
211         cols = smbus_read_byte(module, 4)  & 0xf;
212         rows = smbus_read_byte(module, 3)  & 0xf;
213         /* grand total. You have rows+cols addressing, * times of banks, times
214          * width of data in bytes */
215         /* Width is assumed to be 64 bits == 8 bytes */
216         value = (1 << (cols + rows)) * banks * 8;
217         print_info_hex32(value);
218         print_info(" bytes ");
219         /* Return in 8MB units */
220         value >>= 23;
221
222         /* We should have single or double side */
223         if (smbus_read_byte(module, 5) == 2) {
224                 print_info("x2");
225                 value = (value << 16) | value;
226         }
227         print_info("\r\n");
228         return value;
229
230 }
231
232 static int
233 spd_num_chips(unsigned char slot) 
234
235 /*      unsigned int module = ((0x50 + slot) << 1) + 1; */
236         unsigned int module = 0x50 + slot;
237         unsigned int width;
238
239         width = smbus_read_byte(module, 13);
240         if (width == 0)
241                 width = 8;
242         return 64 / width;
243 }
244
245 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
246 {
247 #define T133 7
248         unsigned char Trp = 1, Tras = 1, casl = 2, val;
249         unsigned char timing = 0xe4;
250         /* read Trp */
251         val = smbus_read_byte(0x50, 27);
252         if (val < 2*T133)
253                 Trp = 1;
254         val = smbus_read_byte(0x50, 30);
255         if (val < 5*T133)
256                 Tras = 0;
257         val = smbus_read_byte(0x50, 18);
258         if (val < 8)
259                 casl = 1;
260         if (val < 4)
261                 casl = 0;
262         
263         val = (Trp << 7) | (Tras << 6) | (casl << 4) | 4;
264         
265         print_debug_hex8(val); print_debug(" is the computed timing\n");
266         /* don't set it. Experience shows that this screwy chipset should just
267          * be run with the most conservative timing.
268          * pci_write_config8(0, 0x64, val);
269          */
270 }
271
272 static void set_ma_mapping(device_t north, int slot, int type)
273 {
274     unsigned char reg, val;
275     int shift;
276
277     reg = 0x58 + slot/2;
278     if (slot%2 >= 1)
279         shift = 0;
280     else
281         shift = 4;
282
283     val = pci_read_config8(north, reg);
284     val &= ~(0xf << shift);
285     val |= type << shift;
286     pci_write_config8(north, reg, val);
287 }
288
289
290 static void sdram_enable(int controllers, const struct mem_controller *ctrl) 
291 {
292         unsigned char i;
293         static const uint8_t ramregs[] = {
294                 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x56, 0x57
295         };
296         device_t north = 0;
297         uint32_t size, base, slot, ma;
298         /* begin to initialize*/
299         // I forget why we need this, but we do
300         dimms_write(0xa55a5aa5);
301         
302         /* set NOP*/
303         pci_write_config8(north,0x6C, 0x01);
304         print_debug("NOP\r\n");
305         /* wait 200us*/
306         // You need to do the memory reference. That causes the nop cycle. 
307         dimms_read(0);
308         udelay(400);
309         print_debug("PRECHARGE\r\n");
310         /* set precharge */
311         pci_write_config8(north,0x6C, 0x02);
312         print_debug("DUMMY READS\r\n");
313         /* dummy reads*/
314         dimms_read(0);
315         udelay(200);
316         print_debug("CBR\r\n");
317         /* set CBR*/
318         pci_write_config8(north,0x6C, 0x04);
319         
320         /* do 8 reads and wait >100us between each - from via*/
321         dimms_read(0);
322         udelay(200);
323         dimms_read(0);
324         udelay(200);
325         dimms_read(0);
326         udelay(200);
327         dimms_read(0);
328         udelay(200);
329         dimms_read(0);
330         udelay(200);
331         dimms_read(0);
332         udelay(200);
333         dimms_read(0);
334         udelay(200);
335         dimms_read(0);
336         udelay(200);
337         print_debug("MRS\r\n");
338         /* set MRS*/
339         pci_write_config8(north,0x6c, 0x03);
340 #if DIMM_CL2
341         dimms_read(0x150);
342 #else // CL=3
343         dimms_read(0x1d0);
344 #endif
345         udelay(200);
346         print_debug("NORMAL\r\n");
347         /* set to normal mode */
348         pci_write_config8(north,0x6C, 0x08);
349         
350         dimms_write(0x55aa55aa);
351         dimms_read(0);
352         udelay(200);
353         print_debug("set ref. rate\r\n");
354         // Set the refresh rate. 
355 #if DIMM_PC133
356         pci_write_config8(north,0x6A, 0x86);
357 #else
358         pci_write_config8(north,0x6A, 0x65);
359 #endif
360         print_debug("enable multi-page open\r\n");
361         // enable multi-page open
362         pci_write_config8(north,0x6B, 0x0d);
363         
364         base = 0;
365         for(slot = 0; slot < 4; slot++) {
366                 size = spd_module_size(slot);
367                 /* side 0 */
368                 base += size & 0xffff;
369                 pci_write_config8(north, ramregs[2*slot], base);
370                 /* side 1 */
371                 base += size >> 16;
372                 if (base > 0xff)
373                         base = 0xff;
374                 pci_write_config8(north, ramregs[2*slot + 1], base);
375
376                 if (!size)
377                         continue;
378
379                 /* Calculate the value of MA mapping type register,
380                  * based on size of SDRAM chips. */
381                 size = (size & 0xffff) << (3 + 3);
382                         /* convert module size to be in Mbits */
383                 size /= spd_num_chips(slot);
384                 print_debug_hex16(size);
385                 print_debug(" is the chip size\r\n");
386                 if (size < 64)
387                         ma = 0;
388                 if (size < 256)
389                         ma = 8;
390                 else
391                         ma = 0xe;
392                 print_debug_hex16(ma);
393                 print_debug(" is the MA type\r\n");
394                 set_ma_mapping(north, slot, ma);
395         }
396         print_err("vt8601 done\r\n");
397 }