tree: drop last paragraph of GPL copyright header
[coreboot.git] / src / northbridge / intel / i855 / raminit.c
blob63ee98b46a9c6e1384fc3e7d17e05964a7f5647a
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2006 Jon Dufresne <jon.dufresne@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <assert.h>
18 #include <lib.h>
19 #include <spd.h>
20 #include <sdram_mode.h>
21 #include <stdlib.h>
22 #include <delay.h>
23 #include "i855.h"
25 /*-----------------------------------------------------------------------------
26 Macros and definitions:
27 -----------------------------------------------------------------------------*/
29 #define VALIDATE_DIMM_COMPATIBILITY
31 /* Debugging macros. */
32 #if CONFIG_DEBUG_RAM_SETUP
33 #define PRINTK_DEBUG(x...) printk(BIOS_DEBUG, x)
34 #define DUMPNORTH() dump_pci_device(NORTHBRIDGE_MMC)
35 #else
36 #define PRINTK_DEBUG(x...)
37 #define DUMPNORTH()
38 #endif
40 #define delay() udelay(200)
42 #define VG85X_MODE (SDRAM_BURST_4 | SDRAM_BURST_INTERLEAVED | SDRAM_CAS_2_5)
44 /* DRC[10:8] - Refresh Mode Select (RMS).
45 * 0x0 for Refresh Disabled (Self Refresh)
46 * 0x1 for Refresh interval 15.6 us for 133MHz
47 * 0x2 for Refresh interval 7.8 us for 133MHz
48 * 0x7 for Refresh interval 64 Clocks. (Fast Refresh Mode)
50 #define RAM_COMMAND_REFRESH 0x1
52 /* DRC[6:4] - SDRAM Mode Select (SMS). */
53 #define RAM_COMMAND_SELF_REFRESH 0x0
54 #define RAM_COMMAND_NOP 0x1
55 #define RAM_COMMAND_PRECHARGE 0x2
56 #define RAM_COMMAND_MRS 0x3
57 #define RAM_COMMAND_EMRS 0x4
58 #define RAM_COMMAND_CBR 0x6
59 #define RAM_COMMAND_NORMAL 0x7
61 /* DRC[29] - Initialization Complete (IC). */
62 #define RAM_COMMAND_IC 0x1
64 struct dimm_size {
65 unsigned int side1;
66 unsigned int side2;
69 static const uint32_t refresh_frequency[] = {
70 /* Relative frequency (array value) of each E7501 Refresh Mode Select
71 * (RMS) value (array index)
72 * 0 == least frequent refresh (longest interval between refreshes)
73 * [0] disabled -> 0
74 * [1] 15.6 usec -> 2
75 * [2] 7.8 usec -> 3
76 * [3] 64 usec -> 1
77 * [4] reserved -> 0
78 * [5] reserved -> 0
79 * [6] reserved -> 0
80 * [7] 64 clocks -> 4
82 0, 2, 3, 1, 0, 0, 0, 4
85 static const uint32_t refresh_rate_map[] = {
86 /* Map the JEDEC spd refresh rates (array index) to i855 Refresh Mode
87 * Select values (array value)
88 * These are all the rates defined by JESD21-C Appendix D, Rev. 1.0
89 * The i855 supports only 15.6 us (1), 7.8 us (2) and
90 * 64 clock (481 ns) (7) refresh.
91 * [0] == 15.625 us -> 15.6 us
92 * [1] == 3.9 us -> 481 ns
93 * [2] == 7.8 us -> 7.8 us
94 * [3] == 31.3 us -> 15.6 us
95 * [4] == 62.5 us -> 15.6 us
96 * [5] == 125 us -> 15.6 us
98 1, 7, 2, 1, 1, 1
101 #define MAX_SPD_REFRESH_RATE ((sizeof(refresh_rate_map) / sizeof(uint32_t)) - 1)
103 /*-----------------------------------------------------------------------------
104 SPD functions:
105 -----------------------------------------------------------------------------*/
107 static void die_on_spd_error(int spd_return_value)
109 if (spd_return_value < 0)
110 PRINTK_DEBUG("Error reading SPD info: got %d\n", spd_return_value);
112 if (spd_return_value < 0)
113 die("Error reading SPD info\n");
118 * Calculate the page size for each physical bank of the DIMM:
120 * log2(page size) = (# columns) + log2(data width)
122 * NOTE: Page size is the total number of data bits in a row.
124 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
125 * @return log2(page size) for each side of the DIMM.
127 static struct dimm_size sdram_spd_get_page_size(u8 dimm_socket_address)
129 uint16_t module_data_width;
130 int value;
131 struct dimm_size pgsz;
133 pgsz.side1 = 0;
134 pgsz.side2 = 0;
136 // Side 1
137 value = spd_read_byte(dimm_socket_address, SPD_NUM_COLUMNS);
138 die_on_spd_error(value);
140 pgsz.side1 = value & 0xf; // # columns in bank 1
142 /* Get the module data width and convert it to a power of two */
143 value = spd_read_byte(dimm_socket_address, SPD_MODULE_DATA_WIDTH_MSB);
144 die_on_spd_error(value);
146 module_data_width = (value & 0xff) << 8;
148 value = spd_read_byte(dimm_socket_address, SPD_MODULE_DATA_WIDTH_LSB);
149 die_on_spd_error(value);
151 module_data_width |= (value & 0xff);
153 pgsz.side1 += log2(module_data_width);
155 /* side two */
156 value = spd_read_byte(dimm_socket_address, SPD_NUM_DIMM_BANKS);
157 die_on_spd_error(value);
160 if (value > 2)
161 die("Bad SPD value\n");
163 if (value > 2)
164 PRINTK_DEBUG("Bad SPD value\n");
166 if (value == 2) {
167 pgsz.side2 = pgsz.side1; // Assume symmetric banks until we know differently
168 value = spd_read_byte(dimm_socket_address, SPD_NUM_COLUMNS);
169 die_on_spd_error(value);
171 if ((value & 0xf0) != 0) {
172 // Asymmetric banks
173 pgsz.side2 -= value & 0xf; /* Subtract out columns on side 1 */
174 pgsz.side2 += (value >> 4) & 0xf; /* Add in columns on side 2 */
178 return pgsz;
182 * Read the width in bits of each DIMM side's DRAMs via SPD (i.e. 4, 8, 16).
184 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
185 * @return Width in bits of each DIMM side's DRAMs.
187 static struct dimm_size sdram_spd_get_width(u8 dimm_socket_address)
189 int value;
190 struct dimm_size width;
192 width.side1 = 0;
193 width.side2 = 0;
195 value = spd_read_byte(dimm_socket_address, SPD_PRIMARY_SDRAM_WIDTH);
196 die_on_spd_error(value);
198 width.side1 = value & 0x7f; // Mask off bank 2 flag
200 if (value & 0x80) {
201 width.side2 = width.side1 << 1; // Bank 2 exists and is double-width
202 } else {
203 // If bank 2 exists, it's the same width as bank 1
204 value = spd_read_byte(dimm_socket_address, SPD_NUM_DIMM_BANKS);
205 die_on_spd_error(value);
207 #ifdef ROMCC_IF_BUG_FIXED
208 if (value == 2)
209 width.side2 = width.side1;
210 #else
211 switch (value) {
212 case 2:
213 width.side2 = width.side1;
214 break;
216 default:
217 break;
219 #endif
222 return width;
226 * Calculate the log base 2 size in bits of both DIMM sides.
228 * log2(# bits) = (# columns) + log2(data width) +
229 * (# rows) + log2(banks per SDRAM)
231 * Note that it might be easier to use SPD byte 31 here, it has the DIMM size
232 * as a multiple of 4MB. The way we do it now we can size both sides of an
233 * asymmetric DIMM.
235 * @param dimm SMBus address of DIMM socket to interrogate.
236 * @return log2(number of bits) for each side of the DIMM.
238 static struct dimm_size spd_get_dimm_size(unsigned dimm)
240 int value;
242 // Start with log2(page size)
243 struct dimm_size sz = sdram_spd_get_page_size(dimm);
245 if (sz.side1 > 0) {
246 value = spd_read_byte(dimm, SPD_NUM_ROWS);
247 die_on_spd_error(value);
249 sz.side1 += value & 0xf;
251 if (sz.side2 > 0) {
252 // Double-sided DIMM
253 if (value & 0xF0)
254 sz.side2 += value >> 4; // Asymmetric
255 else
256 sz.side2 += value; // Symmetric
259 value = spd_read_byte(dimm, SPD_NUM_BANKS_PER_SDRAM);
260 die_on_spd_error(value);
262 value = log2(value);
263 sz.side1 += value;
264 if (sz.side2 > 0)
265 sz.side2 += value;
268 return sz;
272 * Scan for compatible DIMMs.
274 * @return A bitmask indicating which sockets contain a compatible DIMM.
276 static uint8_t spd_get_supported_dimms(void)
278 int i;
279 uint8_t dimm_mask = 0;
281 for (i = 0; i < DIMM_SOCKETS; i++) {
282 u8 dimm = DIMM0 + i;
284 #ifdef VALIDATE_DIMM_COMPATIBILITY
285 struct dimm_size page_size;
286 struct dimm_size sdram_width;
287 #endif
288 int spd_value;
290 if (dimm == 0)
291 continue; // No such socket on this mainboard
293 if (spd_read_byte(dimm, SPD_MEMORY_TYPE) != SPD_MEMORY_TYPE_SDRAM_DDR)
294 continue;
296 #ifdef VALIDATE_DIMM_COMPATIBILITY
297 if ((spd_value = spd_read_byte(dimm, SPD_MODULE_VOLTAGE)) != SPD_VOLTAGE_SSTL2) {
298 PRINTK_DEBUG("Skipping DIMM with unsupported voltage: %02x\n", spd_value);
299 continue; // Unsupported voltage
303 // E7501 does not support unregistered DIMMs
304 spd_value = spd_read_byte(dimm, SPD_MODULE_ATTRIBUTES);
305 if (!(spd_value & MODULE_REGISTERED) || (spd_value < 0)) {
306 PRINTK_DEBUG("Skipping unregistered DIMM: %02x\n", spd_value);
307 continue;
311 page_size = sdram_spd_get_page_size(dimm);
312 sdram_width = sdram_spd_get_width(dimm);
314 // Validate DIMM page size
315 // The i855 only supports page sizes of 4, 8, 16 KB per channel
316 // NOTE: 4 KB = 32 Kb = 2^15
317 // 16 KB = 128 Kb = 2^17
319 if ((page_size.side1 < 15) || (page_size.side1 > 17)) {
320 PRINTK_DEBUG("Skipping DIMM with unsupported page size: %d\n", page_size.side1);
321 continue;
324 // If DIMM is double-sided, verify side2 page size
325 if (page_size.side2 != 0) {
326 if ((page_size.side2 < 15) || (page_size.side2 > 17)) {
327 PRINTK_DEBUG("Skipping DIMM with unsupported page size: %d\n", page_size.side2);
328 continue;
331 // Validate SDRAM width
332 // The i855 only supports x8 and x16 devices
333 if ((sdram_width.side1 != 8) && (sdram_width.side1 != 16)) {
334 PRINTK_DEBUG("Skipping DIMM with unsupported width: %d\n", sdram_width.side2);
335 continue;
338 // If DIMM is double-sided, verify side2 width
339 if (sdram_width.side2 != 0) {
340 if ((sdram_width.side2 != 8)
341 && (sdram_width.side2 != 16)) {
342 PRINTK_DEBUG("Skipping DIMM with unsupported width: %d\n", sdram_width.side2);
343 continue;
346 #endif
347 // Made it through all the checks, this DIMM is usable
348 dimm_mask |= (1 << i);
351 return dimm_mask;
354 /*-----------------------------------------------------------------------------
355 SDRAM configuration functions:
356 -----------------------------------------------------------------------------*/
358 static void do_ram_command(uint8_t command, uint16_t jedec_mode_bits)
360 int i;
361 u32 reg32;
362 uint8_t dimm_start_32M_multiple = 0;
363 uint16_t i855_mode_bits = jedec_mode_bits;
365 /* Configure the RAM command. */
366 reg32 = pci_read_config32(NORTHBRIDGE_MMC, DRC);
367 reg32 &= ~(7 << 4);
368 reg32 |= (command << 4);
369 PRINTK_DEBUG(" Sending RAM command 0x%08x\n", reg32);
370 pci_write_config32(NORTHBRIDGE_MMC, DRC, reg32);
372 // RAM_COMMAND_NORMAL is an exception.
373 // It affects only the memory controller and does not need to be "sent" to the DIMMs.
375 if (command != RAM_COMMAND_NORMAL) {
377 // Send the command to all DIMMs by accessing a memory location within each
378 // NOTE: for mode select commands, some of the location address bits
379 // are part of the command
381 // Map JEDEC mode bits to i855
382 if (command == RAM_COMMAND_MRS || command == RAM_COMMAND_EMRS) {
383 /* Host address lines [13:3] map to DIMM address lines [11, 9:0] */
384 i855_mode_bits = ((jedec_mode_bits & 0x800) << (13 - 11)) | ((jedec_mode_bits & 0x3ff) << (12 - 9));
387 for (i = 0; i < (DIMM_SOCKETS * 2); ++i) {
388 uint8_t dimm_end_32M_multiple = pci_read_config8(NORTHBRIDGE_MMC, DRB + i);
389 if (dimm_end_32M_multiple > dimm_start_32M_multiple) {
391 uint32_t dimm_start_address = dimm_start_32M_multiple << 25;
392 PRINTK_DEBUG(" Sending RAM command to 0x%08x\n", dimm_start_address + i855_mode_bits);
393 read32((void *)(dimm_start_address + i855_mode_bits));
395 // Set the start of the next DIMM
396 dimm_start_32M_multiple = dimm_end_32M_multiple;
402 static void set_initialize_complete(void)
404 uint32_t drc_reg;
406 drc_reg = pci_read_config32(NORTHBRIDGE_MMC, DRC);
407 drc_reg |= (1 << 29);
408 pci_write_config32(NORTHBRIDGE_MMC, DRC, drc_reg);
411 static void sdram_enable(void)
413 int i;
415 printk(BIOS_DEBUG, "Ram enable 1\n");
416 delay();
417 delay();
419 /* NOP command */
420 PRINTK_DEBUG(" NOP\n");
421 do_ram_command(RAM_COMMAND_NOP, 0);
422 delay();
423 delay();
424 delay();
426 /* Pre-charge all banks (at least 200 us after NOP) */
427 PRINTK_DEBUG(" Pre-charging all banks\n");
428 do_ram_command(RAM_COMMAND_PRECHARGE, 0);
429 delay();
430 delay();
431 delay();
433 printk(BIOS_DEBUG, "Ram enable 4\n");
434 do_ram_command(RAM_COMMAND_EMRS, SDRAM_EXTMODE_DLL_ENABLE);
435 delay();
436 delay();
437 delay();
439 printk(BIOS_DEBUG, "Ram enable 5\n");
440 do_ram_command(RAM_COMMAND_MRS, VG85X_MODE | SDRAM_MODE_DLL_RESET);
442 printk(BIOS_DEBUG, "Ram enable 6\n");
443 do_ram_command(RAM_COMMAND_PRECHARGE, 0);
444 delay();
445 delay();
446 delay();
448 /* 8 CBR refreshes (Auto Refresh) */
449 PRINTK_DEBUG(" 8 CBR refreshes\n");
450 for(i = 0; i < 8; i++) {
451 do_ram_command(RAM_COMMAND_CBR, 0);
452 delay();
453 delay();
454 delay();
457 printk(BIOS_DEBUG, "Ram enable 8\n");
458 do_ram_command(RAM_COMMAND_MRS, VG85X_MODE | SDRAM_MODE_NORMAL);
460 /* Set GME-M Mode Select bits back to NORMAL operation mode */
461 PRINTK_DEBUG(" Normal operation mode\n");
462 do_ram_command(RAM_COMMAND_NORMAL, 0);
463 delay();
464 delay();
465 delay();
467 printk(BIOS_DEBUG, "Ram enable 9\n");
468 set_initialize_complete();
470 delay();
471 delay();
472 delay();
473 delay();
474 delay();
476 printk(BIOS_DEBUG, "After configuration:\n");
477 /* dump_pci_devices(); */
480 printk(BIOS_DEBUG, "\n\n***** RAM TEST *****\n");
481 ram_check(0, 0xa0000);
482 ram_check(0x100000, 0x40000000);
486 /*-----------------------------------------------------------------------------
487 DIMM-independant configuration functions:
488 -----------------------------------------------------------------------------*/
491 * Set only what I need until it works, then make it figure things out on boot
492 * assumes only one DIMM is populated.
494 static void sdram_set_registers(void)
497 printk(BIOS_DEBUG, "Before configuration:\n");
498 dump_pci_devices();
502 static void spd_set_row_attributes(uint8_t dimm_mask)
504 int i;
505 uint16_t row_attributes = 0;
507 for (i = 0; i < DIMM_SOCKETS; i++) {
508 u8 dimm = DIMM0 + i;
509 struct dimm_size page_size;
510 struct dimm_size sdram_width;
512 if (!(dimm_mask & (1 << i))) {
513 row_attributes |= 0x77 << (i << 3);
514 continue; // This DIMM not usable
517 // Get the relevant parameters via SPD
518 page_size = sdram_spd_get_page_size(dimm);
519 sdram_width = sdram_spd_get_width(dimm);
521 // Update the DRAM Row Attributes.
522 // Page size is encoded as log2(page size in bits) - log2(2 KB) or 4 KB == 1, 8 KB == 3, 16KB == 3
523 // NOTE: 2 KB = 16 Kb = 2^14
524 row_attributes |= (page_size.side1 - 14) << (i << 3); // Side 1 of each DIMM is an EVEN row
526 if (sdram_width.side2 > 0)
527 row_attributes |= (page_size.side2 - 14) << ((i << 3) + 4); // Side 2 is ODD
528 else
529 row_attributes |= 7 << ((i << 3) + 4);
530 /* go to the next DIMM */
533 PRINTK_DEBUG("DRA: %04x\n", row_attributes);
535 /* Write the new row attributes register */
536 pci_write_config16(NORTHBRIDGE_MMC, DRA, row_attributes);
539 static void spd_set_dram_controller_mode(uint8_t dimm_mask)
541 int i;
543 // Initial settings
544 u32 controller_mode = pci_read_config32(NORTHBRIDGE_MMC, DRC);
545 u32 system_refresh_mode = (controller_mode >> 7) & 7;
547 controller_mode |= (1 << 20); // ECC
548 controller_mode |= (1 << 15); // RAS lockout
549 controller_mode |= (1 << 12); // Address Tri-state enable (ADRTRIEN), FIXME: how is this detected?????
550 controller_mode |= (2 << 10); // FIXME: Undocumented, really needed?????
552 for (i = 0; i < DIMM_SOCKETS; i++) {
553 u8 dimm = DIMM0 + i;
554 uint32_t dimm_refresh_mode;
555 int value;
556 u8 tRCD, tRP;
558 if (!(dimm_mask & (1 << i))) {
559 continue; // This DIMM not usable
562 // Disable ECC mode if any one of the DIMMs does not support ECC
563 value = spd_read_byte(dimm, SPD_DIMM_CONFIG_TYPE);
564 die_on_spd_error(value);
565 if (value != ERROR_SCHEME_ECC)
566 controller_mode &= ~(3 << 20);
568 value = spd_read_byte(dimm, SPD_REFRESH);
569 die_on_spd_error(value);
570 value &= 0x7f; // Mask off self-refresh bit
571 if (value > MAX_SPD_REFRESH_RATE) {
572 printk(BIOS_ERR, "unsupported refresh rate\n");
573 continue;
575 // Get the appropriate i855 refresh mode for this DIMM
576 dimm_refresh_mode = refresh_rate_map[value];
577 if (dimm_refresh_mode > 7) {
578 printk(BIOS_ERR, "unsupported refresh rate\n");
579 continue;
581 // If this DIMM requires more frequent refresh than others,
582 // update the system setting
583 if (refresh_frequency[dimm_refresh_mode] >
584 refresh_frequency[system_refresh_mode])
585 system_refresh_mode = dimm_refresh_mode;
587 /* FIXME: is this correct? */
588 tRCD = spd_read_byte(dimm, SPD_tRCD);
589 tRP = spd_read_byte(dimm, SPD_tRP);
590 if (tRCD != tRP) {
591 PRINTK_DEBUG(" Disabling RAS lockouk due to tRCD (%d) != tRP (%d)\n", tRCD, tRP);
592 controller_mode &= ~(1 << 15);
595 /* go to the next DIMM */
598 controller_mode &= ~(7 << 7);
599 controller_mode |= (system_refresh_mode << 7);
600 PRINTK_DEBUG("DRC: %08x\n", controller_mode);
602 pci_write_config32(NORTHBRIDGE_MMC, DRC, controller_mode);
605 static void spd_set_dram_timing(uint8_t dimm_mask)
607 int i;
608 u32 dram_timing;
610 // CAS# latency bitmasks in SPD_ACCEPTABLE_CAS_LATENCIES format
611 // NOTE: i82822 supports only 2.0 and 2.5
612 uint32_t system_compatible_cas_latencies = SPD_CAS_LATENCY_2_0 | SPD_CAS_LATENCY_2_5;
613 uint8_t slowest_row_precharge = 0;
614 uint8_t slowest_ras_cas_delay = 0;
615 uint8_t slowest_active_to_precharge_delay = 0;
617 for (i = 0; i < DIMM_SOCKETS; i++) {
618 u8 dimm = DIMM0 + i;
619 int value;
620 uint32_t current_cas_latency;
621 uint32_t dimm_compatible_cas_latencies;
622 if (!(dimm_mask & (1 << i)))
623 continue; // This DIMM not usable
625 value = spd_read_byte(dimm, SPD_ACCEPTABLE_CAS_LATENCIES);
626 PRINTK_DEBUG("SPD_ACCEPTABLE_CAS_LATENCIES: %d\n", value);
627 die_on_spd_error(value);
629 dimm_compatible_cas_latencies = value & 0x7f; // Start with all supported by DIMM
630 PRINTK_DEBUG("dimm_compatible_cas_latencies #1: %d\n", dimm_compatible_cas_latencies);
632 current_cas_latency = 1 << log2(dimm_compatible_cas_latencies); // Max supported by DIMM
633 PRINTK_DEBUG("current_cas_latency: %d\n", current_cas_latency);
635 // Can we support the highest CAS# latency?
636 value = spd_read_byte(dimm, SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
637 die_on_spd_error(value);
638 PRINTK_DEBUG("SPD_MIN_CYCLE_TIME_AT_CAS_MAX: %d.%d\n", value >> 4, value & 0xf);
640 // NOTE: At 133 MHz, 1 clock == 7.52 ns
641 if (value > 0x75) {
642 // Our bus is too fast for this CAS# latency
643 // Remove it from the bitmask of those supported by the DIMM that are compatible
644 dimm_compatible_cas_latencies &= ~current_cas_latency;
645 PRINTK_DEBUG("dimm_compatible_cas_latencies #2: %d\n", dimm_compatible_cas_latencies);
647 // Can we support the next-highest CAS# latency (max - 0.5)?
649 current_cas_latency >>= 1;
650 if (current_cas_latency != 0) {
651 value = spd_read_byte(dimm, SPD_SDRAM_CYCLE_TIME_2ND);
652 die_on_spd_error(value);
653 PRINTK_DEBUG("SPD_SDRAM_CYCLE_TIME_2ND: %d.%d\n", value >> 4, value & 0xf);
654 if (value > 0x75) {
655 dimm_compatible_cas_latencies &= ~current_cas_latency;
656 PRINTK_DEBUG("dimm_compatible_cas_latencies #2: %d\n", dimm_compatible_cas_latencies);
659 // Can we support the next-highest CAS# latency (max - 1.0)?
660 current_cas_latency >>= 1;
661 if (current_cas_latency != 0) {
662 value = spd_read_byte(dimm, SPD_SDRAM_CYCLE_TIME_3RD);
663 PRINTK_DEBUG("SPD_SDRAM_CYCLE_TIME_3RD: %d.%d\n", value >> 4, value & 0xf);
664 die_on_spd_error(value);
665 if (value > 0x75) {
666 dimm_compatible_cas_latencies &= ~current_cas_latency;
667 PRINTK_DEBUG("dimm_compatible_cas_latencies #2: %d\n", dimm_compatible_cas_latencies);
670 // Restrict the system to CAS# latencies compatible with this DIMM
671 system_compatible_cas_latencies &= dimm_compatible_cas_latencies;
673 value = spd_read_byte(dimm, SPD_MIN_ROW_PRECHARGE_TIME);
674 die_on_spd_error(value);
675 if (value > slowest_row_precharge)
676 slowest_row_precharge = value;
678 value = spd_read_byte(dimm, SPD_MIN_RAS_TO_CAS_DELAY);
679 die_on_spd_error(value);
680 if (value > slowest_ras_cas_delay)
681 slowest_ras_cas_delay = value;
683 value = spd_read_byte(dimm, SPD_MIN_ACTIVE_TO_PRECHARGE_DELAY);
684 die_on_spd_error(value);
685 if (value > slowest_active_to_precharge_delay)
686 slowest_active_to_precharge_delay = value;
688 /* go to the next DIMM */
690 PRINTK_DEBUG("CAS latency: %d\n", system_compatible_cas_latencies);
692 dram_timing = pci_read_config32(NORTHBRIDGE_MMC, DRT);
693 dram_timing &= ~(DRT_CAS_MASK | DRT_TRP_MASK | DRT_RCD_MASK);
694 PRINTK_DEBUG("DRT: %08x\n", dram_timing);
696 if (system_compatible_cas_latencies & SPD_CAS_LATENCY_2_0) {
697 dram_timing |= DRT_CAS_2_0;
698 } else if (system_compatible_cas_latencies & SPD_CAS_LATENCY_2_5) {
699 dram_timing |= DRT_CAS_2_5;
700 } else
701 die("No CAS# latencies compatible with all DIMMs!!\n");
703 uint32_t current_cas_latency = dram_timing & DRT_CAS_MASK;
705 /* tRP */
707 PRINTK_DEBUG("slowest_row_precharge: %d.%d\n", slowest_row_precharge >> 2, slowest_row_precharge & 0x3);
708 // i855 supports only 2, 3 or 4 clocks for tRP
709 if (slowest_row_precharge > ((30 << 2)))
710 die("unsupported DIMM tRP"); // > 30.0 ns: 5 or more clocks
711 else if (slowest_row_precharge > ((22 << 2) | (2 << 0)))
712 dram_timing |= DRT_TRP_4; // > 22.5 ns: 4 or more clocks
713 else if (slowest_row_precharge > (15 << 2))
714 dram_timing |= DRT_TRP_3; // > 15.0 ns: 3 clocks
715 else
716 dram_timing |= DRT_TRP_2; // <= 15.0 ns: 2 clocks
718 /* tRCD */
720 PRINTK_DEBUG("slowest_ras_cas_delay: %d.%d\n", slowest_ras_cas_delay >> 2, slowest_ras_cas_delay & 0x3);
721 // i855 supports only 2, 3 or 4 clocks for tRCD
722 if (slowest_ras_cas_delay > ((30 << 2)))
723 die("unsupported DIMM tRCD"); // > 30.0 ns: 5 or more clocks
724 else if (slowest_ras_cas_delay > ((22 << 2) | (2 << 0)))
725 dram_timing |= DRT_RCD_4; // > 22.5 ns: 4 or more clocks
726 else if (slowest_ras_cas_delay > (15 << 2))
727 dram_timing |= DRT_RCD_3; // > 15.0 ns: 3 clocks
728 else
729 dram_timing |= DRT_RCD_2; // <= 15.0 ns: 2 clocks
731 /* tRAS, min */
733 PRINTK_DEBUG("slowest_active_to_precharge_delay: %d\n", slowest_active_to_precharge_delay);
734 // i855 supports only 5, 6, 7 or 8 clocks for tRAS
735 // 5 clocks ~= 37.6 ns, 6 clocks ~= 45.1 ns, 7 clocks ~= 52.6 ns, 8 clocks ~= 60.1 ns
736 if (slowest_active_to_precharge_delay > 60)
737 die("unsupported DIMM tRAS"); // > 52 ns: 8 or more clocks
738 else if (slowest_active_to_precharge_delay > 52)
739 dram_timing |= DRT_TRAS_MIN_8; // 46-52 ns: 7 clocks
740 else if (slowest_active_to_precharge_delay > 45)
741 dram_timing |= DRT_TRAS_MIN_7; // 46-52 ns: 7 clocks
742 else if (slowest_active_to_precharge_delay > 37)
743 dram_timing |= DRT_TRAS_MIN_6; // 38-45 ns: 6 clocks
744 else
745 dram_timing |= DRT_TRAS_MIN_5; // < 38 ns: 5 clocks
747 /* FIXME: guess work starts here...
749 * Intel refers to DQ turn-arround values for back to calculate the values,
750 * but i have no idea what this means
754 * Back to Back Read-Write command spacing (DDR, different Rows/Bank)
756 /* Set to a 3 clock back to back read to write turn around.
757 * 2 is a good delay if the CAS latency is 2.0 */
758 dram_timing &= ~(3 << 28);
759 if (current_cas_latency == DRT_CAS_2_0)
760 dram_timing |= (2 << 28); // 2 clocks
761 else
762 dram_timing |= (1 << 28); // 3 clocks
765 * Back to Back Read-Write command spacing (DDR, same or different Rows/Bank)
767 dram_timing &= ~(3 << 26);
768 if (current_cas_latency == DRT_CAS_2_0)
769 dram_timing |= (2 << 26); // 5 clocks
770 else
771 dram_timing |= (1 << 26); // 6 clocks
774 * Back To Back Read-Read commands spacing (DDR, different Rows):
776 dram_timing &= ~(1 << 25);
777 dram_timing |= (1 << 25); // 3 clocks
779 PRINTK_DEBUG("DRT: %08x\n", dram_timing);
780 pci_write_config32(NORTHBRIDGE_MMC, DRT, dram_timing);
783 static void spd_set_dram_size(uint8_t dimm_mask)
785 int i;
786 int total_dram = 0;
787 uint32_t drb_reg = 0;
789 for (i = 0; i < DIMM_SOCKETS; i++) {
790 u8 dimm = DIMM0 + i;
791 struct dimm_size sz;
793 if (!(dimm_mask & (1 << i))) {
794 /* fill values even for not present DIMMs */
795 drb_reg |= (total_dram << (i * 16));
796 drb_reg |= (total_dram << ((i * 16) + 8));
798 continue; // This DIMM not usable
800 sz = spd_get_dimm_size(dimm);
802 total_dram += (1 << (sz.side1 - 28));
803 drb_reg |= (total_dram << (i * 16));
805 total_dram += (1 << (sz.side2 - 28));
806 drb_reg |= (total_dram << ((i * 16) + 8));
808 PRINTK_DEBUG("DRB: %08x\n", drb_reg);
809 pci_write_config32(NORTHBRIDGE_MMC, DRB, drb_reg);
813 static void spd_set_dram_pwr_management(void)
815 uint32_t pwrmg_reg;
817 pwrmg_reg = 0x10f10430;
818 pci_write_config32(NORTHBRIDGE_MMC, PWRMG, pwrmg_reg);
821 static void spd_set_dram_throttle_control(void)
823 uint32_t dtc_reg = 0;
825 /* DDR SDRAM Throttle Mode (TMODE):
826 * 0011 = Both Rank and GMCH Thermal Sensor based throttling is enabled. When the external SO-
827 * DIMM Thermal Sensor is Tripped DDR SDRAM Throttling begins based on the setting in RTT
829 dtc_reg |= (3 << 28);
831 /* Read Counter Based Power Throttle Control (RCTC):
832 * 0 = 85%
834 dtc_reg |= (0 << 24);
836 /* Write Counter Based Power Throttle Control (WCTC):
837 * 0 = 85%
839 dtc_reg |= (0 << 20);
841 /* Read Thermal Based Power Throttle Control (RTTC):
842 * 0xA = 20%
844 dtc_reg |= (0xA << 16);
846 /* Write Thermal Based Power Throttle Control (WTTC):
847 * 0xA = 20%
849 dtc_reg |= (0xA << 12);
851 /* Counter Based Throttle Lock (CTLOCK): */
852 dtc_reg |= (0 << 11);
854 /* Thermal Throttle Lock (TTLOCK): */
855 dtc_reg |= (0 << 10);
857 /* Thermal Power Throttle Control fields Enable: */
858 dtc_reg |= (1 << 9);
860 /* High Priority Stream Throttling Enable: */
861 dtc_reg |= (0 << 8);
863 /* Global DDR SDRAM Sampling Window (GDSW): */
864 dtc_reg |= 0xff;
865 PRINTK_DEBUG("DTC: %08x\n", dtc_reg);
866 pci_write_config32(NORTHBRIDGE_MMC, DTC, dtc_reg);
869 static void spd_update(u8 reg, u32 new_value)
871 #if CONFIG_DEBUG_RAM_SETUP
872 u32 value1 = pci_read_config32(NORTHBRIDGE_MMC, reg);
873 #endif
874 pci_write_config32(NORTHBRIDGE_MMC, reg, new_value);
875 #if CONFIG_DEBUG_RAM_SETUP
876 u32 value2 = pci_read_config32(NORTHBRIDGE_MMC, reg);
877 PRINTK_DEBUG("update reg %02x, old: %08x, new: %08x, read back: %08x\n", reg, value1, new_value, value2);
878 #endif
881 /* if ram still doesn't work do this function */
882 static void spd_set_undocumented_registers(void)
884 spd_update(0x74, 0x00000001);
885 spd_update(0x78, 0x001fe974);
886 spd_update(0x80, 0x00af0039);
887 spd_update(0x84, 0x0000033c);
888 spd_update(0x88, 0x00000010);
890 spd_update(0xc0, 0x00000003);
893 static void northbridge_set_registers(void)
895 u16 value;
896 int video_memory = 0;
898 printk(BIOS_DEBUG, "Setting initial Northbridge registers....\n");
900 /* Set the value for Fixed DRAM Hole Control Register */
901 pci_write_config8(NORTHBRIDGE, FDHC, 0x00);
903 /* Set the value for Programable Attribute Map Registers
904 * Ideally, this should be R/W for as many ranges as possible.
906 pci_write_config8(NORTHBRIDGE, PAM0, 0x30);
907 pci_write_config8(NORTHBRIDGE, PAM1, 0x33);
908 pci_write_config8(NORTHBRIDGE, PAM2, 0x33);
909 pci_write_config8(NORTHBRIDGE, PAM3, 0x33);
910 pci_write_config8(NORTHBRIDGE, PAM4, 0x33);
911 pci_write_config8(NORTHBRIDGE, PAM5, 0x33);
912 pci_write_config8(NORTHBRIDGE, PAM6, 0x33);
914 /* Set the value for System Management RAM Control Register */
915 pci_write_config8(NORTHBRIDGE, SMRAM, 0x02);
917 /* Set the value for GMCH Control Register #1 */
918 switch (CONFIG_VIDEO_MB) {
919 case 1: /* 1M of memory */
920 video_memory = 0x1;
921 break;
922 case 4: /* 4M of memory */
923 video_memory = 0x2;
924 break;
925 case 8: /* 8M of memory */
926 video_memory = 0x3;
927 break;
928 case 16: /* 16M of memory */
929 video_memory = 0x4;
930 break;
931 case 32: /* 32M of memory */
932 video_memory = 0x5;
933 break;
934 default: /* No memory */
935 pci_write_config16(NORTHBRIDGE, GMC, pci_read_config16(NORTHBRIDGE, GMC) | 1);
936 video_memory = 0x0;
939 value = pci_read_config16(NORTHBRIDGE, GGC);
940 value |= video_memory << 4;
941 if (video_memory == 0) {
942 value &= ~(1 < 1);
943 } else
944 value |= (1 < 1);
945 pci_write_config16(NORTHBRIDGE, GGC, value);
947 /* AGPCMD: disable AGP, Data-Rate: 1x */
948 pci_write_config32(NORTHBRIDGE, AGPCMD, 0x00000001);
950 pci_write_config8(NORTHBRIDGE, AMTT, 0x20);
951 pci_write_config8(NORTHBRIDGE, LPTT, 0x10);
953 printk(BIOS_DEBUG, "Initial Northbridge registers have been set.\n");
956 static void sdram_set_spd_registers(void)
958 uint8_t dimm_mask;
960 PRINTK_DEBUG("Reading SPD data...\n");
962 dimm_mask = spd_get_supported_dimms();
964 if (dimm_mask == 0) {
965 printk(BIOS_DEBUG, "No usable memory for this controller\n");
966 } else {
967 PRINTK_DEBUG("DIMM MASK: %02x\n", dimm_mask);
969 spd_set_row_attributes(dimm_mask);
970 spd_set_dram_controller_mode(dimm_mask);
971 spd_set_dram_timing(dimm_mask);
972 spd_set_dram_size(dimm_mask);
973 spd_set_dram_pwr_management();
974 spd_set_dram_throttle_control();
975 spd_set_undocumented_registers();
978 /* Setup Initial Northbridge Registers */
979 northbridge_set_registers();