cb13ad4e98003e433c1d61db8a88de4bd7a866d2
[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 static 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 static 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 #ifdef CONFIG_DEBUG_RAM_SETUP
73 static void dumpnorth(device_t north)
74 {
75         unsigned int r, c;
76         for (r = 0;; r += 16) {
77                 print_debug_hex8(r);
78                 print_debug(":");
79                 for (c = 0; c < 16; c++) {
80                         print_debug_hex8(pci_read_config8(north, r + c));
81                         print_debug(" ");
82                 }
83                 print_debug("\n");
84                 if (r >= 240)
85                         break;
86         }
87 }
88 #endif
89
90 static void sdram_set_registers(const struct mem_controller *ctrl)
91 {
92         device_t north = (device_t) PCI_DEV(0, 0, 0);
93
94         print_err("vt8601 init starting\n");
95         print_debug_hex32(north);
96         print_debug(" is the north\n");
97         print_debug_hex16(pci_read_config16(north, 0));
98         print_debug(" ");
99         print_debug_hex16(pci_read_config16(north, 2));
100         print_debug("\n");
101
102         /* All we are doing now is setting initial known-good values that will
103          * be revised later as we read SPD
104          */
105
106         // memory clk enable. We are not using ECC
107         pci_write_config8(north, 0x78, 0x01);
108         print_debug_hex8(pci_read_config8(north, 0x78));
109
110         // dram control, see the book. 
111 #if DIMM_PC133
112         pci_write_config8(north, 0x68, 0x52);
113 #else
114         pci_write_config8(north, 0x68, 0x42);
115 #endif
116
117         // dram control, see the book. 
118         pci_write_config8(north, 0x6B, 0x0c);
119
120         // Initial setting, 256MB in each bank, will be rewritten later.
121         pci_write_config8(north, 0x5A, 0x20);
122         print_debug_hex8(pci_read_config8(north, 0x5a));
123         pci_write_config8(north, 0x5B, 0x40);
124         pci_write_config8(north, 0x5C, 0x60);
125         pci_write_config8(north, 0x5D, 0x80);
126         pci_write_config8(north, 0x5E, 0xA0);
127         pci_write_config8(north, 0x5F, 0xC0);
128         // It seems we have to take care of these 2 registers as if 
129         // they are bank 6 and 7.
130         pci_write_config8(north, 0x56, 0xC0);
131         pci_write_config8(north, 0x57, 0xC0);
132
133         // SDRAM in all banks
134         pci_write_config8(north, 0x60, 0x3F);
135
136         // DRAM timing. I'm suspicious of this
137         // This is for all banks, 64 is 0,1.  65 is 2,3. 66 is 4,5.
138         // ras precharge 4T, RAS pulse 5T
139         // cas2 is 0xd6, cas3 is 0xe6
140         // we're also backing off write pulse width to 2T, so result is 0xee
141 #if DIMM_CL2
142         pci_write_config8(north, 0x64, 0xd4);
143         pci_write_config8(north, 0x65, 0xd4);
144         pci_write_config8(north, 0x66, 0xd4);
145 #else                           // CL=3
146         pci_write_config8(north, 0x64, 0xe4);
147         pci_write_config8(north, 0x65, 0xe4);
148         pci_write_config8(north, 0x66, 0xe4);
149 #endif
150
151         // dram frequency select.
152         // enable 4K pages for 64M dram. 
153 #if DIMM_PC133
154         pci_write_config8(north, 0x69, 0x3c);
155 #else
156         pci_write_config8(north, 0x69, 0xac);
157 #endif
158
159         /* IMPORTANT -- disable refresh counter */
160         // refresh counter, disabled.
161         pci_write_config8(north, 0x6A, 0x00);
162
163         // clkenable configuration. kevinh FIXME - add precharge
164         pci_write_config8(north, 0x6C, 0x00);
165         // dram read latch delay of 1 ns, MD drive 8 mA,
166         // high drive strength on MA[2: 13], we#, cas#, ras#
167         // As per Cindy Lee, set to 0x37, not 0x57
168         pci_write_config8(north, 0x6D, 0x7f);
169 }
170
171 /* slot is the dram slot. Return size of side0 in lower 16-bit,
172  * side1 in upper 16-bit, in units of 8MB */
173 static unsigned long spd_module_size(unsigned char slot)
174 {
175         /* for all the DRAMS, see if they are there and get the size of each
176          * module. This is just a very early first cut at sizing.
177          */
178         /* we may run out of registers ... */
179         unsigned int banks, rows, cols;
180         unsigned int value = 0;
181         /* unsigned int module = ((0x50 + slot) << 1) + 1; */
182         unsigned int module = 0x50 + slot;
183
184         /* is the module there? if byte 2 is not 4, then we'll assume it 
185          * is useless. 
186          */
187         print_info("Slot ");
188         print_info_hex8(slot);
189         if (smbus_read_byte(module, 2) != 4) {
190                 print_info(" is empty\n");
191                 return 0;
192         }
193         print_info(" is SDRAM ");
194
195         banks = smbus_read_byte(module, 17);
196
197         /* we're going to assume symmetric banks. Sorry. */
198         cols = smbus_read_byte(module, 4) & 0xf;
199         rows = smbus_read_byte(module, 3) & 0xf;
200
201         /* grand total. You have rows+cols addressing, * times of banks, times
202          * width of data in bytes */
203         /* Width is assumed to be 64 bits == 8 bytes */
204         value = (1 << (cols + rows));
205         value *= banks * 8;
206         print_info_hex32(value);
207         print_info(" bytes ");
208         /* Return in 8MB units */
209         value >>= 23;
210
211         /* We should have single or double side */
212         if (smbus_read_byte(module, 5) == 2) {
213                 print_info("x2");
214                 value = (value << 16) | value;
215         }
216         print_info("\n");
217         return value;
218 }
219
220 #if 0
221 static int spd_num_chips(unsigned char slot)
222 {
223         unsigned int module = 0x50 + slot;
224         unsigned int width;
225
226         width = smbus_read_byte(module, 13);
227         if (width == 0)
228                 width = 8;
229         return 64 / width;
230 }
231 #endif
232
233 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
234 {
235 #define T133 7
236         unsigned char Trp = 1, Tras = 1, casl = 2, val;
237         unsigned char timing = 0xe4;
238         /* read Trp */
239         val = smbus_read_byte(0x50, 27);
240         if (val < 2 * T133)
241                 Trp = 1;
242         val = smbus_read_byte(0x50, 30);
243         if (val < 5 * T133)
244                 Tras = 0;
245         val = smbus_read_byte(0x50, 18);
246         if (val < 8)
247                 casl = 1;
248         if (val < 4)
249                 casl = 0;
250
251         val = (Trp << 7) | (Tras << 6) | (casl << 4) | 4;
252
253         print_debug_hex8(val);
254         print_debug(" is the computed timing\n");
255         /* don't set it. Experience shows that this screwy chipset should just
256          * be run with the most conservative timing.
257          * pci_write_config8(0, 0x64, val);
258          */
259 }
260
261 static void set_ma_mapping(device_t north, int slot, int type)
262 {
263         unsigned char reg, val;
264         int shift;
265
266         reg = 0x58 + slot / 2;
267         if (slot % 2 >= 1)
268                 shift = 0;
269         else
270                 shift = 4;
271
272         val = pci_read_config8(north, reg);
273         val &= ~(0xf << shift);
274         val |= type << shift;
275         pci_write_config8(north, reg, val);
276 }
277
278
279 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
280 {
281         static const uint8_t ramregs[] = {
282                 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x56, 0x57
283         };
284         device_t north = 0;
285         uint32_t size, base, slot, ma;
286         /* begin to initialize */
287
288         // I forget why we need this, but we do
289         dimms_write(0xa55a5aa5);
290
291         /* set NOP */
292         pci_write_config8(north, 0x6C, 0x01);
293         print_debug("NOP\n");
294         /* wait 200us */
295         // You need to do the memory reference. That causes the nop cycle. 
296         dimms_read(0);
297         udelay(400);
298         print_debug("PRECHARGE\n");
299         /* set precharge */
300         pci_write_config8(north, 0x6C, 0x02);
301         print_debug("DUMMY READS\n");
302         /* dummy reads */
303         dimms_read(0);
304         udelay(200);
305         print_debug("CBR\n");
306         /* set CBR */
307         pci_write_config8(north, 0x6C, 0x04);
308
309         /* do 8 reads and wait >100us between each - from via */
310         dimms_read(0);
311         udelay(200);
312         dimms_read(0);
313         udelay(200);
314         dimms_read(0);
315         udelay(200);
316         dimms_read(0);
317         udelay(200);
318         dimms_read(0);
319         udelay(200);
320         dimms_read(0);
321         udelay(200);
322         dimms_read(0);
323         udelay(200);
324         dimms_read(0);
325         udelay(200);
326         print_debug("MRS\n");
327         /* set MRS */
328         pci_write_config8(north, 0x6c, 0x03);
329 #if DIMM_CL2
330         dimms_read(0x150);
331 #else                           // CL=3
332         dimms_read(0x1d0);
333 #endif
334         udelay(200);
335         print_debug("NORMAL\n");
336         /* set to normal mode */
337         pci_write_config8(north, 0x6C, 0x08);
338
339         dimms_write(0x55aa55aa);
340         dimms_read(0);
341         udelay(200);
342         print_debug("set ref. rate\n");
343         // Set the refresh rate. 
344 #if DIMM_PC133
345         pci_write_config8(north, 0x6A, 0x86);
346 #else
347         pci_write_config8(north, 0x6A, 0x65);
348 #endif
349         print_debug("enable multi-page open\n");
350         // enable multi-page open
351         pci_write_config8(north, 0x6B, 0x0d);
352
353         base = 0;
354         for (slot = 0; slot < 4; slot++) {
355                 size = spd_module_size(slot);
356                 /* side 0 */
357                 base += size & 0xffff;
358                 pci_write_config8(north, ramregs[2 * slot], base);
359                 /* side 1 */
360                 base += size >> 16;
361                 if (base > 0xff)
362                         base = 0xff;
363                 pci_write_config8(north, ramregs[2 * slot + 1], base);
364
365                 if (!size)
366                         continue;
367
368                 /* Read the row densities */
369                 size = smbus_read_byte(0x50+slot, 0x1f);
370
371                 /* Set the MA map type.
372                  *
373                  * 0xa should be another option, but when 
374                  * it would be used is unknown.
375                  */
376
377                 if (size < 16 ) /* less than 64 MB per side */
378                         ma = 0x0;
379                 else if (size < 32) /* less than 128MB per side */
380                         ma = 0x8;
381                 else if ( size < 64) /* less than 256MB per side */
382                         ma = 0xc;
383                 else /* 256MB or more per side */
384                         ma = 0xe;
385                 print_debug_hex16(ma);
386                 print_debug(" is the MA type\n");
387                 set_ma_mapping(north, slot, ma);
388         }
389         print_err("vt8601 done\n");
390 }