Remove various .c #includes from Intel i810/i82801ax/i82801bx boards.
[coreboot.git] / src / northbridge / intel / i82810 / raminit.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2009 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2007 Corey Osgood <corey.osgood@gmail.com>
6  * Copyright (C) 2008-2009 Elia Yehuda <z4ziggy@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include <spd.h>
24 #include <delay.h>
25 #include <stdint.h>
26 #include <arch/io.h>
27 #include <arch/romcc_io.h>
28 #include <device/pci_def.h>
29 #include <console/console.h>
30 #include "i82810.h"
31 #include "raminit.h"
32
33 /*-----------------------------------------------------------------------------
34 Macros and definitions.
35 -----------------------------------------------------------------------------*/
36
37 /* Debugging macros. */
38 #define HAVE_ENOUGH_REGISTERS   0 /* Don't have enough registers to compile all
39                                    * debugging code with ROMCC
40                                    */
41 #if CONFIG_DEBUG_RAM_SETUP
42 #define PRINT_DEBUG(x)          print_debug(x)
43 #define PRINT_DEBUG_HEX8(x)     print_debug_hex8(x)
44 #define PRINT_DEBUG_HEX16(x)    print_debug_hex16(x)
45 #define PRINT_DEBUG_HEX32(x)    print_debug_hex32(x)
46 // no dump_pci_device in src/northbridge/intel/i82810/
47 // #define DUMPNORTH()          dump_pci_device(PCI_DEV(0, 0, 0))
48 #define DUMPNORTH()
49 #else
50 #define PRINT_DEBUG(x)
51 #define PRINT_DEBUG_HEX8(x)
52 #define PRINT_DEBUG_HEX16(x)
53 #define PRINT_DEBUG_HEX32(x)
54 #define DUMPNORTH()
55 #endif
56
57 /* DRAMT[7:5] - SDRAM Mode Select (SMS). */
58 #define RAM_COMMAND_SELF_REFRESH 0x0 /* Disable refresh */
59 #define RAM_COMMAND_NORMAL       0x1 /* Refresh: 15.6/11.7us for 100/133MHz */
60 #define RAM_COMMAND_NORMAL_FR    0x2 /* Refresh: 7.8/5.85us for 100/133MHz */
61 #define RAM_COMMAND_NOP          0x4 /* NOP command */
62 #define RAM_COMMAND_PRECHARGE    0x5 /* All bank precharge */
63 #define RAM_COMMAND_MRS          0x6 /* Mode register set */
64 #define RAM_COMMAND_CBR          0x7 /* CBR */
65
66 /*
67  * This table is used to translate the value read from SPD Byte 31 to a value
68  * the northbridge can understand in DRP, aka Rx52[7:4], [3:0]. Where most
69  * northbridges have some sort of simple calculation that can be done for this,
70  * I haven't yet figured out one for this northbridge. Until someone does,
71  * this table is necessary.
72  */
73 static const u8 translate_spd_to_i82810[] = {
74         /* Note: 4MB sizes are not supported, so dual-sided DIMMs with a 4MB
75          * side can't be either, at least for now.
76          */
77         /* TODO: For above case, only use the other side if > 4MB, and get some
78          * of these DIMMs to test it with. Same for unsupported 128/x sizes.
79          */
80
81                 /*   SPD Byte 31        Memory Size [Side 1/2]  */
82         0xff,   /*      0x01            No memory       */
83         0xff,   /*      0x01             4/0            */
84         0x01,   /*      0x02             8/0            */
85         0xff,   /*      0x03             8/4            */
86         0x04,   /*      0x04            16/0 or 16      */
87         0xff,   /*      0x05            16/4            */
88         0x05,   /*      0x06            16/8            */
89         0xff,   /*      0x07            Invalid         */
90         0x07,   /*      0x08            32/0 or 32      */
91         0xff,   /*      0x09            32/4            */
92         0xff,   /*      0x0A            32/8            */
93         0xff,   /*      0x0B            Invalid         */
94         0x08,   /*      0x0C            32/16           */
95         0xff, 0xff, 0xff, /* 0x0D-0F    Invalid         */
96         0x0a,   /*      0x10            64/0 or 64      */
97         0xff,   /*      0x11            64/4            */
98         0xff,   /*      0x12            64/8            */
99         0xff,   /*      0x13            Invalid         */
100         0xff,   /*      0x14            64/16           */
101         0xff, 0xff, 0xff, /* 0x15-17    Invalid         */
102         0x0b,   /*      0x18            64/32           */
103         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x19-1f Invalid */
104         0x0d,   /*      0x20            128/0 or 128    */
105         /* These configurations are not supported by the i810 */
106         0xff,   /*      0x21            128/4           */
107         0xff,   /*      0x22            128/8           */
108         0xff,   /*      0x23            Invalid         */
109         0xff,   /*      0x24            128/16          */
110         0xff, 0xff, 0xff, /* 0x25-27    Invalid         */
111         0xff,   /*      0x28            128/32          */
112         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x29-2f Invalid */
113         0x0e,   /*      0x30            128/64          */
114         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
115         0xff, 0xff, 0xff, /* 0x31-3f    Invalid         */
116         0x0f,   /*      0x40            256/0 or 256    */
117         /* Anything larger is not supported by the 82810. */
118 };
119
120 /*
121  * Table which returns the RAM size in MB when fed the DRP[7:4] or [3:0] value.
122  * Note that 2 is a value which the DRP should never be programmed to.
123  * Some size values appear twice, due to single-sided vs dual-sided banks.
124  */
125 static const u16 translate_i82810_to_mb[] = {
126 /* DRP  0  1 (2) 3   4   5   6   7   8   9   A   B   C    D    E    F */
127 /* MB */0, 8, 0, 16, 16, 24, 32, 32, 48, 64, 64, 96, 128, 128, 192, 256,
128 };
129
130 /* Size of bank#0 for dual-sided DIMMs */
131 static const u8 translate_i82810_to_bank[] = {
132 /* DRP  0  1 (2) 3  4  5   6   7  8   9   A  B   C   D  E    F */
133 /* MB */0, 0, 0, 8, 0, 16, 16, 0, 32, 32, 0, 64, 64, 0, 128, 128,
134 };
135
136 struct dimm_info {
137         u8 ds;          /* dual-sided */
138         u8 ss;          /* single-sided */
139         u8 size;
140 };
141
142 /*-----------------------------------------------------------------------------
143 SDRAM configuration functions.
144 -----------------------------------------------------------------------------*/
145
146 static inline int spd_read_byte(unsigned device, unsigned address)
147 {
148         return smbus_read_byte(device, address);
149 }
150
151 /**
152  * Send the specified RAM command to all DIMMs.
153  *
154  * @param The RAM command to send to the DIMM(s).
155  */
156 static void do_ram_command(u8 command)
157 {
158         u32 addr, addr_offset;
159         u16 dimm_size, dimm_start, dimm_bank;
160         u8 reg8, drp;
161         int i, caslatency;
162
163         /* Configure the RAM command. */
164         reg8 = pci_read_config8(PCI_DEV(0, 0, 0), DRAMT);
165         reg8 &= 0x1f;           /* Clear bits 7-5. */
166         reg8 |= command << 5;
167         pci_write_config8(PCI_DEV(0, 0, 0), DRAMT, reg8);
168
169         /*
170          * RAM_COMMAND_NORMAL affects only the memory controller and
171          * doesn't need to be "sent" to the DIMMs.
172          */
173         if (command == RAM_COMMAND_NORMAL)
174                 return;
175
176         dimm_start = 0;
177         for (i = 0; i < DIMM_SOCKETS; i++) {
178                 /*
179                  * Calculate the address offset where we need to "send" the
180                  * DIMM command to. For most commands the offset is 0, only
181                  * RAM_COMMAND_MRS needs special values, see below.
182                  * The final address offset bits depend on three things:
183                  *
184                  *  (1) Some hardcoded values specified in the datasheet.
185                  *  (2) Which CAS latency we will use/set. This is the SMAA[4]
186                  *      bit, which is 1 for CL3, and 0 for CL2. The bitstring
187                  *      so far has the form '00000001X1010', X being SMAA[4].
188                  *  (3) The DIMM to which we want to send the command. For
189                  *      DIMM0 no special handling is needed, but for DIMM1 we
190                  *      must invert the four bits SMAA[7:4] (see datasheet).
191                  *
192                  * Finally, the bitstring has to be shifted 3 bits to the left.
193                  * See i810 datasheet pages 43, 85, and 86 for details.
194                  */
195                 addr_offset = 0;
196                 caslatency = 3; /* TODO: Dynamically get CAS latency later. */
197                 if (i == 0 && command == RAM_COMMAND_MRS && caslatency == 3)
198                         addr_offset = 0x1d0; /* DIMM0, CL3, 0000111010000 */
199                 if (i == 1 && command == RAM_COMMAND_MRS && caslatency == 3)
200                         addr_offset = 0x650; /* DIMM1, CL3, 0011001010000 */
201                 if (i == 0 && command == RAM_COMMAND_MRS && caslatency == 2)
202                         addr_offset = 0x150; /* DIMM0, CL2, 0000101010000 */
203                 if (i == 1 && command == RAM_COMMAND_MRS && caslatency == 2)
204                         addr_offset = 0x1a0; /* DIMM1, CL2, 0000110100000 */
205
206                 drp = pci_read_config8(PCI_DEV(0, 0, 0), DRP);
207                 drp = (drp >> (i * 4)) & 0x0f;
208
209                 dimm_size = translate_i82810_to_mb[drp];
210                 if (dimm_size) {
211                         addr = (dimm_start * 1024 * 1024) + addr_offset;
212 #if HAVE_ENOUGH_REGISTERS
213                         PRINT_DEBUG("    Sending RAM command 0x");
214                         PRINT_DEBUG_HEX8(reg8);
215                         PRINT_DEBUG(" to 0x");
216                         PRINT_DEBUG_HEX32(addr);
217                         PRINT_DEBUG("\n");
218 #endif
219
220                         read32(addr);
221                 }
222
223                 dimm_bank = translate_i82810_to_bank[drp];
224                 if (dimm_bank) {
225                         addr = ((dimm_start + dimm_bank) * 1024 * 1024) + addr_offset;
226 #if HAVE_ENOUGH_REGISTERS
227                         PRINT_DEBUG("    Sending RAM command 0x");
228                         PRINT_DEBUG_HEX8(reg8);
229                         PRINT_DEBUG(" to 0x");
230                         PRINT_DEBUG_HEX32(addr);
231                         PRINT_DEBUG("\n");
232 #endif
233                         read32(addr);
234                 }
235
236                 dimm_start += dimm_size;
237         }
238 }
239
240 /*-----------------------------------------------------------------------------
241 DIMM-independant configuration functions.
242 -----------------------------------------------------------------------------*/
243
244 /*
245  * Set DRP - DRAM Row Population Register (Device 0).
246  */
247 static void spd_set_dram_size(void)
248 {
249         /* The variables drp and dimm_size have to be ints since all the
250          * SMBus-related functions return ints, and its just easier this way.
251          */
252         int i, drp, dimm_size;
253
254         drp = 0x00;
255
256         for (i = 0; i < DIMM_SOCKETS; i++) {
257                 /* First check if a DIMM is actually present. */
258                 if (smbus_read_byte(DIMM_SPD_BASE + i, 2) == 4) {
259                         print_debug("Found DIMM in slot ");
260                         print_debug_hex8(i);
261                         print_debug("\n");
262
263                         dimm_size = smbus_read_byte(DIMM_SPD_BASE + i, 31);
264
265                         /* WISHLIST: would be nice to display it as decimal? */
266                         print_debug("DIMM is 0x");
267                         print_debug_hex8(dimm_size * 4);
268                         print_debug("MB\n");
269
270                         /* The i810 can't handle DIMMs larger than 128MB per
271                          * side. This will fail if the DIMM uses a
272                          * non-supported DRAM tech, and can't be used until
273                          * buffers are done dynamically.
274                          * Note: the factory BIOS just dies if it spots this :D
275                          */
276                         if (dimm_size > 32) {
277                                 print_err("DIMM row sizes larger than 128MB not"
278                                           "supported on i810\n");
279                                 print_err
280                                     ("Attempting to treat as 128MB DIMM\n");
281                                 dimm_size = 32;
282                         }
283
284                         /* This array is provided in raminit.h, because it got
285                          * extremely messy. The above way is cleaner, but
286                          * doesn't support any asymetrical/odd configurations.
287                          */
288                         dimm_size = translate_spd_to_i82810[dimm_size];
289
290                         print_debug("After translation, dimm_size is 0x");
291                         print_debug_hex8(dimm_size);
292                         print_debug("\n");
293
294                         /* If the DIMM is dual-sided, the DRP value is +2 */
295                         /* TODO: Figure out asymetrical configurations. */
296                         if ((smbus_read_byte(DIMM_SPD_BASE + i, 127) | 0xf) ==
297                             0xff) {
298                                 print_debug("DIMM is dual-sided\n");
299                                 dimm_size += 2;
300                         }
301                 } else {
302                         print_debug("No DIMM found in slot ");
303                         print_debug_hex8(i);
304                         print_debug("\n");
305
306                         /* If there's no DIMM in the slot, set value to 0. */
307                         dimm_size = 0x00;
308                 }
309
310                 /* Put in dimm_size to reflect the current DIMM. */
311                 drp |= dimm_size << (i * 4);
312         }
313
314         print_debug("DRP calculated to 0x");
315         print_debug_hex8(drp);
316         print_debug("\n");
317
318         pci_write_config8(PCI_DEV(0, 0, 0), DRP, drp);
319 }
320
321 static void set_dram_timing(void)
322 {
323         /* TODO, for now using default, hopefully safe values. */
324         // pci_write_config8(PCI_DEV(0, 0, 0), DRAMT, 0x00);
325 }
326
327 /*
328  * TODO: BUFF_SC needs to be set according to the DRAM tech (x8, x16,
329  * or x32), but the datasheet doesn't list all the details. Currently, it
330  * needs to be pulled from the output of 'lspci -xxx Rx92'.
331  *
332  * Common results (tested on actual hardware) are:
333  *
334  * (DRP: c = 128MB dual sided, d = 128MB single sided, f = 256MB dual sided)
335  *
336  * BUFF_SC  TOM     DRP    DIMM0                DIMM1
337  * ----------------------------------------------------------------------------
338  * 0x3356   128MB   0x0c   128MB dual-sided     -
339  * 0xcc56   128MB   0xc0   -                    128MB dual-sided
340  * 0x77da   128MB   0x0d   128MB single-sided   -
341  * 0xddda   128MB   0xd0   -                    128MB single-sided
342  * 0x0001   256MB   0xcc   128MB dual-sided     128MB dual-sided
343  * 0x55c6   256MB   0xdd   128MB single-sided   128MB single-sided
344  * 0x4445   256MB   0xcd   128MB single-sided   128MB dual-sided
345  * 0x1145   256MB   0xdc   128MB dual-sided     128MB single-sided
346  * 0x3356   256MB   0x0f   256MB dual-sided     -
347  * 0xcc56   256MB   0xf0   -                    256MB dual-sided
348  * 0x0001   384MB   0xcf   256MB dual-sided     128MB dual-sided
349  * 0x0001   384MB   0xfc   128MB dual-sided     256MB dual-sided
350  * 0x1145   384MB   0xdf   256MB dual-sided     128MB single-sided
351  * 0x4445   384MB   0xfd   128MB single-sided   256MB dual-sided
352  * 0x0001   512MB   0xff   256MB dual-sided     256MB dual-sided
353  *
354  * See also:
355  * http://www.coreboot.org/pipermail/coreboot/2009-May/047966.html
356  */
357 static void set_dram_buffer_strength(void)
358 {
359         struct dimm_info d0, d1;
360         u16 buff_sc;
361
362         /* Check first slot. */
363         d0.size = d0.ds = d0.ss = 0;
364         if (smbus_read_byte(DIMM_SPD_BASE, SPD_MEMORY_TYPE)
365             == SPD_MEMORY_TYPE_SDRAM) {
366                 d0.size = smbus_read_byte(DIMM_SPD_BASE, SPD_BANK_DENSITY);
367                 d0.ds = smbus_read_byte(DIMM_SPD_BASE, SPD_NUM_DIMM_BANKS) > 1;
368                 d0.ss = !d0.ds;
369         }
370
371         /* Check second slot. */
372         d1.size = d1.ds = d1.ss = 0;
373         if (smbus_read_byte(DIMM_SPD_BASE + 1, SPD_MEMORY_TYPE)
374             == SPD_MEMORY_TYPE_SDRAM) {
375                 d1.size = smbus_read_byte(DIMM_SPD_BASE + 1, SPD_BANK_DENSITY);
376                 d1.ds = smbus_read_byte(DIMM_SPD_BASE + 1,
377                                         SPD_NUM_DIMM_BANKS) > 1;
378                 d1.ss = !d1.ds;
379         }
380
381         buff_sc = 0;
382
383         /* Tame the beast... */
384         if ((d0.ds && d1.ds) || (d0.ds && d1.ss) || (d0.ss && d1.ds))
385                 buff_sc |= 1;
386         if ((d0.size && !d1.size) || (!d0.size && d1.size) || (d0.ss && d1.ss))
387                 buff_sc |= 1 << 1;
388         if ((d0.ds && !d1.size) || (!d0.size && d1.ds) || (d0.ss && d1.ss)
389            || (d0.ds && d1.ss) || (d0.ss && d1.ds))
390                 buff_sc |= 1 << 2;
391         if ((d0.ss && !d1.size) || (!d0.size && d1.ss))
392                 buff_sc |= 1 << 3;
393         if ((d0.size && !d1.size) || (!d0.size && d1.size))
394                 buff_sc |= 1 << 4;
395         if ((d0.ds && !d1.size) || (!d0.size && d1.ds) || (d0.ds && d1.ss)
396            || (d0.ss && d1.ds))
397                 buff_sc |= 1 << 6;
398         if ((d0.ss && !d1.size) || (!d0.size && d1.ss) || (d0.ss && d1.ss))
399                 buff_sc |= 3 << 6;
400         if ((!d0.size && d1.ss) || (d0.ds && d1.ss) || (d0.ss && d1.ss))
401                 buff_sc |= 1 << 8;
402         if (d0.size && !d1.size)
403                 buff_sc |= 3 << 8;
404         if ((d0.ss && !d1.size) || (d0.ss && d1.ss) || (d0.ss && d1.ds))
405                 buff_sc |= 1 << 10;
406         if (!d0.size && d1.size)
407                 buff_sc |= 3 << 10;
408         if ((d0.size && !d1.size) || (d0.ss && !d1.size) || (!d0.size && d1.ss)
409            || (d0.ss && d1.ss) || (d0.ds && d1.ss))
410                 buff_sc |= 1 << 12;
411         if (d0.size && !d1.size)
412                 buff_sc |= 1 << 13;
413         if ((!d0.size && d1.size) || (d0.ss && !d1.size) || (d0.ss && d1.ss)
414            || (d0.ss && d1.ds))
415                 buff_sc |= 1 << 14;
416         if (!d0.size && d1.size)
417                 buff_sc |= 1 << 15;
418
419         print_debug("BUFF_SC calculated to 0x");
420         print_debug_hex16(buff_sc);
421         print_debug("\n");
422
423         pci_write_config16(PCI_DEV(0, 0, 0), BUFF_SC, buff_sc);
424 }
425
426 /*-----------------------------------------------------------------------------
427 Public interface.
428 -----------------------------------------------------------------------------*/
429
430 void sdram_set_registers(void)
431 {
432         u8 reg8;
433         u16 did;
434
435         did = pci_read_config16(PCI_DEV(0, 0, 0), PCI_DEVICE_ID);
436
437         /* Ideally, this should be R/W for as many ranges as possible. */
438         pci_write_config8(PCI_DEV(0, 0, 0), PAMR, 0xff);
439
440         /* Set size for onboard-VGA framebuffer. */
441         reg8 = pci_read_config8(PCI_DEV(0, 0, 0), SMRAM);
442         reg8 &= 0x3f;                        /* Disable graphics (for now). */
443 #if CONFIG_VIDEO_MB
444         if (CONFIG_VIDEO_MB == 512)
445                 reg8 |= (1 << 7);            /* Enable graphics (512KB RAM). */
446         else if (CONFIG_VIDEO_MB == 1)
447                 reg8 |= (1 << 7) | (1 << 6); /* Enable graphics (1MB RAM). */
448 #endif
449         pci_write_config8(PCI_DEV(0, 0, 0), SMRAM, reg8);
450
451         /* MISSC2: Bits 1, 2, 6, 7 must be set for VGA (see datasheet). */
452         reg8 = pci_read_config8(PCI_DEV(0, 0, 0), MISSC2);
453         reg8 |= (1 << 1); /* Instruction Parser Unit-Level Clock Gating */
454         reg8 |= (1 << 2); /* Palette Load Select */
455         if (did == 0x7124) {
456                 /* Bits 6 and 7 are only available on 82810E (not 82810). */
457                 reg8 |= (1 << 6); /* Text Immediate Blit */
458                 reg8 |= (1 << 7); /* Must be 1 as per datasheet. */
459         }
460         pci_write_config8(PCI_DEV(0, 0, 0), MISSC2, reg8);
461 }
462
463 void sdram_set_spd_registers(void)
464 {
465         spd_set_dram_size();
466         set_dram_buffer_strength();
467         set_dram_timing();
468 }
469
470 /**
471  * Enable SDRAM.
472  */
473 void sdram_enable(void)
474 {
475         int i;
476
477         /* 1. Apply NOP. */
478         PRINT_DEBUG("RAM Enable 1: Apply NOP\n");
479         do_ram_command(RAM_COMMAND_NOP);
480         udelay(200);
481
482         /* 2. Precharge all. Wait tRP. */
483         PRINT_DEBUG("RAM Enable 2: Precharge all\n");
484         do_ram_command(RAM_COMMAND_PRECHARGE);
485         udelay(1);
486
487         /* 3. Perform 8 refresh cycles. Wait tRC each time. */
488         PRINT_DEBUG("RAM Enable 3: CBR\n");
489         for (i = 0; i < 8; i++) {
490                 do_ram_command(RAM_COMMAND_CBR);
491                 udelay(1);
492         }
493
494         /* 4. Mode register set. Wait two memory cycles. */
495         PRINT_DEBUG("RAM Enable 4: Mode register set\n");
496         do_ram_command(RAM_COMMAND_MRS);
497         udelay(2);
498
499         /* 5. Normal operation (enables refresh at 15.6usec). */
500         PRINT_DEBUG("RAM Enable 5: Normal operation\n");
501         do_ram_command(RAM_COMMAND_NORMAL);
502         udelay(1);
503
504         PRINT_DEBUG("Northbridge following SDRAM init:\n");
505         DUMPNORTH();
506 }