2 * This file is part of the coreboot project.
4 * Copyright (C) 2007-2009 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2007 Corey Osgood <corey@slightlyhackish.com>
6 * Copyright (C) 2008-2009 Elia Yehuda <z4ziggy@gmail.com>
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.
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.
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
24 #include <sdram_mode.h>
28 /*-----------------------------------------------------------------------------
29 Macros and definitions.
30 -----------------------------------------------------------------------------*/
32 /* Uncomment this to enable debugging output. */
33 // #define DEBUG_RAM_SETUP 1
35 /* Debugging macros. */
36 #if defined(DEBUG_RAM_SETUP)
37 #define PRINT_DEBUG(x) print_debug(x)
38 #define PRINT_DEBUG_HEX8(x) print_debug_hex8(x)
39 #define PRINT_DEBUG_HEX16(x) print_debug_hex16(x)
40 #define PRINT_DEBUG_HEX32(x) print_debug_hex32(x)
41 #define DUMPNORTH() dump_pci_device(PCI_DEV(0, 0, 0))
43 #define PRINT_DEBUG(x)
44 #define PRINT_DEBUG_HEX8(x)
45 #define PRINT_DEBUG_HEX16(x)
46 #define PRINT_DEBUG_HEX32(x)
50 /* DRAMT[7:5] - SDRAM Mode Select (SMS). */
51 #define RAM_COMMAND_SELF_REFRESH 0x0 /* Disable refresh */
52 #define RAM_COMMAND_NORMAL 0x1 /* Refresh: 15.6/11.7us for 100/133MHz */
53 #define RAM_COMMAND_NORMAL_FR 0x2 /* Refresh: 7.8/5.85us for 100/133MHz */
54 #define RAM_COMMAND_NOP 0x4 /* NOP command */
55 #define RAM_COMMAND_PRECHARGE 0x5 /* All bank precharge */
56 #define RAM_COMMAND_MRS 0x6 /* Mode register set */
57 #define RAM_COMMAND_CBR 0x7 /* CBR */
60 * Table which returns the RAM size in MB when fed the DRP[7:4] or [3:0] value.
61 * Note that 2 is a value which the DRP should never be programmed to.
62 * Some size values appear twice, due to single-sided vs dual-sided banks.
64 static const u16 translate_i82810_to_mb[] = {
65 /* DRP 0 1 (2) 3 4 5 6 7 8 9 A B C D E F */
66 /* MB */0, 8, 0, 16, 16, 24, 32, 32, 48, 64, 64, 96, 128, 128, 192, 256,
69 /* Size of bank#0 for dual-sided DIMMs */
70 static const u8 translate_i82810_to_bank[] = {
71 /* DRP 0 1 (2) 3 4 5 6 7 8 9 A B C D E F */
72 /* MB */0, 0, 0, 8, 0, 16, 16, 0, 32, 32, 0, 64, 64, 0, 128, 128,
76 u8 ds; /* dual-sided */
77 u8 ss; /* single-sided */
81 /*-----------------------------------------------------------------------------
82 SDRAM configuration functions.
83 -----------------------------------------------------------------------------*/
86 * Send the specified RAM command to all DIMMs.
88 * @param The RAM command to send to the DIMM(s).
90 static void do_ram_command(u8 command)
92 u32 addr, addr_offset;
93 u16 dimm_size, dimm_start, dimm_bank;
97 /* Configure the RAM command. */
98 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), DRAMT);
99 reg8 &= 0x1f; /* Clear bits 7-5. */
100 reg8 |= command << 5;
101 pci_write_config8(PCI_DEV(0, 0, 0), DRAMT, reg8);
104 * RAM_COMMAND_NORMAL affects only the memory controller and
105 * doesn't need to be "sent" to the DIMMs.
107 if (command == RAM_COMMAND_NORMAL)
111 for (i = 0; i < DIMM_SOCKETS; i++) {
113 * Calculate the address offset where we need to "send" the
114 * DIMM command to. For most commands the offset is 0, only
115 * RAM_COMMAND_MRS needs special values, see below.
116 * The final address offset bits depend on three things:
118 * (1) Some hardcoded values specified in the datasheet.
119 * (2) Which CAS latency we will use/set. This is the SMAA[4]
120 * bit, which is 1 for CL3, and 0 for CL2. The bitstring
121 * so far has the form '00000001X1010', X being SMAA[4].
122 * (3) The DIMM to which we want to send the command. For
123 * DIMM0 no special handling is needed, but for DIMM1 we
124 * must invert the four bits SMAA[7:4] (see datasheet).
126 * Finally, the bitstring has to be shifted 3 bits to the left.
127 * See i810 datasheet pages 43, 85, and 86 for details.
130 caslatency = 3; /* TODO: Dynamically get CAS latency later. */
131 if (i == 0 && command == RAM_COMMAND_MRS && caslatency == 3)
132 addr_offset = 0x1d0; /* DIMM0, CL3, 0000111010000 */
133 if (i == 1 && command == RAM_COMMAND_MRS && caslatency == 3)
134 addr_offset = 0x650; /* DIMM1, CL3, 0011001010000 */
135 if (i == 0 && command == RAM_COMMAND_MRS && caslatency == 2)
136 addr_offset = 0x150; /* DIMM0, CL2, 0000101010000 */
137 if (i == 1 && command == RAM_COMMAND_MRS && caslatency == 2)
138 addr_offset = 0x1a0; /* DIMM1, CL2, 0000110100000 */
140 drp = pci_read_config8(PCI_DEV(0, 0, 0), DRP);
141 drp = (drp >> (i * 4)) & 0x0f;
143 dimm_size = translate_i82810_to_mb[drp];
144 addr = (dimm_start * 1024 * 1024) + addr_offset;
146 PRINT_DEBUG(" Sending RAM command 0x");
147 PRINT_DEBUG_HEX8(reg8);
148 PRINT_DEBUG(" to 0x");
149 PRINT_DEBUG_HEX32(addr);
155 dimm_bank = translate_i82810_to_bank[drp];
156 addr = ((dimm_start + dimm_bank) * 1024 * 1024) + addr_offset;
158 PRINT_DEBUG(" Sending RAM command 0x");
159 PRINT_DEBUG_HEX8(reg8);
160 PRINT_DEBUG(" to 0x");
161 PRINT_DEBUG_HEX32(addr);
167 dimm_start += dimm_size;
171 /*-----------------------------------------------------------------------------
172 DIMM-independant configuration functions.
173 -----------------------------------------------------------------------------*/
176 * Set DRP - DRAM Row Population Register (Device 0).
178 static void spd_set_dram_size(void)
180 /* The variables drp and dimm_size have to be ints since all the
181 * SMBus-related functions return ints, and its just easier this way.
183 int i, drp, dimm_size;
187 for (i = 0; i < DIMM_SOCKETS; i++) {
188 /* First check if a DIMM is actually present. */
189 if (smbus_read_byte(DIMM_SPD_BASE + i, 2) == 4) {
190 print_debug("Found DIMM in slot ");
194 dimm_size = smbus_read_byte(DIMM_SPD_BASE + i, 31);
196 /* WISHLIST: would be nice to display it as decimal? */
197 print_debug("DIMM is 0x");
198 print_debug_hex8(dimm_size * 4);
199 print_debug("MB\r\n");
201 /* The i810 can't handle DIMMs larger than 128MB per
202 * side. This will fail if the DIMM uses a
203 * non-supported DRAM tech, and can't be used until
204 * buffers are done dynamically.
205 * Note: the factory BIOS just dies if it spots this :D
207 if (dimm_size > 32) {
208 print_err("DIMM row sizes larger than 128MB not"
209 "supported on i810\r\n");
211 ("Attempting to treat as 128MB DIMM\r\n");
215 /* This array is provided in raminit.h, because it got
216 * extremely messy. The above way is cleaner, but
217 * doesn't support any asymetrical/odd configurations.
219 dimm_size = translate_spd_to_i82810[dimm_size];
221 print_debug("After translation, dimm_size is 0x");
222 print_debug_hex8(dimm_size);
225 /* If the DIMM is dual-sided, the DRP value is +2 */
226 /* TODO: Figure out asymetrical configurations. */
227 if ((smbus_read_byte(DIMM_SPD_BASE + i, 127) | 0xf) ==
229 print_debug("DIMM is dual-sided\r\n");
233 print_debug("No DIMM found in slot ");
237 /* If there's no DIMM in the slot, set value to 0. */
241 /* Put in dimm_size to reflect the current DIMM. */
242 drp |= dimm_size << (i * 4);
245 print_debug("DRP calculated to 0x");
246 print_debug_hex8(drp);
249 pci_write_config8(PCI_DEV(0, 0, 0), DRP, drp);
252 static void set_dram_timing(void)
254 /* TODO, for now using default, hopefully safe values. */
255 // pci_write_config8(PCI_DEV(0, 0, 0), DRAMT, 0x00);
259 * TODO: BUFF_SC needs to be set according to the DRAM tech (x8, x16,
260 * or x32), but the datasheet doesn't list all the details. Currently, it
261 * needs to be pulled from the output of 'lspci -xxx Rx92'.
263 * Common results (tested on actual hardware) are:
265 * (DRP: c = 128MB dual sided, d = 128MB single sided, f = 256MB dual sided)
267 * BUFF_SC TOM DRP DIMM0 DIMM1
268 * ----------------------------------------------------------------------------
269 * 0x3356 128MB 0x0c 128MB dual-sided -
270 * 0xcc56 128MB 0xc0 - 128MB dual-sided
271 * 0x77da 128MB 0x0d 128MB single-sided -
272 * 0xddda 128MB 0xd0 - 128MB single-sided
273 * 0x0001 256MB 0xcc 128MB dual-sided 128MB dual-sided
274 * 0x55c6 256MB 0xdd 128MB single-sided 128MB single-sided
275 * 0x4445 256MB 0xcd 128MB single-sided 128MB dual-sided
276 * 0x1145 256MB 0xdc 128MB dual-sided 128MB single-sided
277 * 0x3356 256MB 0x0f 256MB dual-sided -
278 * 0xcc56 256MB 0xf0 - 256MB dual-sided
279 * 0x0001 384MB 0xcf 256MB dual-sided 128MB dual-sided
280 * 0x0001 384MB 0xfc 128MB dual-sided 256MB dual-sided
281 * 0x1145 384MB 0xdf 256MB dual-sided 128MB single-sided
282 * 0x4445 384MB 0xfd 128MB single-sided 256MB dual-sided
283 * 0x0001 512MB 0xff 256MB dual-sided 256MB dual-sided
286 * http://www.coreboot.org/pipermail/coreboot/2009-May/047966.html
288 static void set_dram_buffer_strength(void)
290 struct dimm_info d0, d1;
293 /* Check first slot. */
294 d0.size = d0.ds = d0.ss = 0;
295 if (smbus_read_byte(DIMM_SPD_BASE, SPD_MEMORY_TYPE)
296 == SPD_MEMORY_TYPE_SDRAM) {
297 d0.size = smbus_read_byte(DIMM_SPD_BASE, SPD_BANK_DENSITY);
298 d0.ds = smbus_read_byte(DIMM_SPD_BASE, SPD_NUM_DIMM_BANKS) > 1;
302 /* Check second slot. */
303 d1.size = d1.ds = d1.ss = 0;
304 if (smbus_read_byte(DIMM_SPD_BASE + 1, SPD_MEMORY_TYPE)
305 == SPD_MEMORY_TYPE_SDRAM) {
306 d1.size = smbus_read_byte(DIMM_SPD_BASE + 1, SPD_BANK_DENSITY);
307 d1.ds = smbus_read_byte(DIMM_SPD_BASE + 1,
308 SPD_NUM_DIMM_BANKS) > 1;
314 /* Tame the beast... */
315 if ((d0.ds && d1.ds) || (d0.ds && d1.ss) || (d0.ss && d1.ds))
317 if ((d0.size && !d1.size) || (!d0.size && d1.size) || (d0.ss && d1.ss))
319 if ((d0.ds && !d1.size) || (!d0.size && d1.ds) || (d0.ss && d1.ss)
320 || (d0.ds && d1.ss) || (d0.ss && d1.ds))
322 if ((d0.ss && !d1.size) || (!d0.size && d1.ss))
324 if ((d0.size && !d1.size) || (!d0.size && d1.size))
326 if ((d0.ds && !d1.size) || (!d0.size && d1.ds) || (d0.ds && d1.ss)
329 if ((d0.ss && !d1.size) || (!d0.size && d1.ss) || (d0.ss && d1.ss))
331 if ((!d0.size && d1.ss) || (d0.ds && d1.ss) || (d0.ss && d1.ss))
333 if (d0.size && !d1.size)
335 if ((d0.ss && !d1.size) || (d0.ss && d1.ss) || (d0.ss && d1.ds))
337 if (!d0.size && d1.size)
339 if ((d0.size && !d1.size) || (d0.ss && !d1.size) || (!d0.size && d1.ss)
340 || (d0.ss && d1.ss) || (d0.ds && d1.ss))
342 if (d0.size && !d1.size)
344 if ((!d0.size && d1.size) || (d0.ss && !d1.size) || (d0.ss && d1.ss)
347 if (!d0.size && d1.size)
350 print_debug("BUFF_SC calculated to 0x");
351 print_debug_hex16(buff_sc);
354 pci_write_config16(PCI_DEV(0, 0, 0), BUFF_SC, buff_sc);
357 /*-----------------------------------------------------------------------------
359 -----------------------------------------------------------------------------*/
361 static void sdram_set_registers(void)
366 did = pci_read_config16(PCI_DEV(0, 0, 0), PCI_DEVICE_ID);
368 /* Ideally, this should be R/W for as many ranges as possible. */
369 pci_write_config8(PCI_DEV(0, 0, 0), PAMR, 0xff);
371 /* Set size for onboard-VGA framebuffer. */
372 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), SMRAM);
373 reg8 &= 0x3f; /* Disable graphics (for now). */
375 if (CONFIG_VIDEO_MB == 512)
376 reg8 |= (1 << 7); /* Enable graphics (512KB RAM). */
377 else if (CONFIG_VIDEO_MB == 1)
378 reg8 |= (1 << 7) | (1 << 6); /* Enable graphics (1MB RAM). */
380 pci_write_config8(PCI_DEV(0, 0, 0), SMRAM, reg8);
382 /* MISSC2: Bits 1, 2, 6, 7 must be set for VGA (see datasheet). */
383 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), MISSC2);
384 reg8 |= (1 << 1); /* Instruction Parser Unit-Level Clock Gating */
385 reg8 |= (1 << 2); /* Palette Load Select */
387 /* Bits 6 and 7 are only available on 82810E (not 82810). */
388 reg8 |= (1 << 6); /* Text Immediate Blit */
389 reg8 |= (1 << 7); /* Must be 1 as per datasheet. */
391 pci_write_config8(PCI_DEV(0, 0, 0), MISSC2, reg8);
394 static void sdram_set_spd_registers(void)
397 set_dram_buffer_strength();
404 static void sdram_enable(void)
409 PRINT_DEBUG("RAM Enable 1: Apply NOP\r\n");
410 do_ram_command(RAM_COMMAND_NOP);
413 /* 2. Precharge all. Wait tRP. */
414 PRINT_DEBUG("RAM Enable 2: Precharge all\r\n");
415 do_ram_command(RAM_COMMAND_PRECHARGE);
418 /* 3. Perform 8 refresh cycles. Wait tRC each time. */
419 PRINT_DEBUG("RAM Enable 3: CBR\r\n");
420 for (i = 0; i < 8; i++) {
421 do_ram_command(RAM_COMMAND_CBR);
425 /* 4. Mode register set. Wait two memory cycles. */
426 PRINT_DEBUG("RAM Enable 4: Mode register set\r\n");
427 do_ram_command(RAM_COMMAND_MRS);
430 /* 5. Normal operation (enables refresh at 15.6usec). */
431 PRINT_DEBUG("RAM Enable 5: Normal operation\r\n");
432 do_ram_command(RAM_COMMAND_NORMAL);
435 PRINT_DEBUG("Northbridge following SDRAM init:\r\n");