1 /* This was originally for the e7500, modified for e7501
2 * The primary differences are that 7501 apparently can
3 * support single channel RAM (i haven't tested),
4 * CAS1.5 is no longer supported, The ECC scrubber
5 * now supports a mode to zero RAM and init ECC in one step
6 * and the undocumented registers at 0x80 require new
7 * (undocumented) values determined by guesswork and
8 * comparison w/ OEM BIOS values.
9 * Steven James 02/06/2003
12 /* converted to C 6/2004 yhlu */
16 #include <sdram_mode.h>
20 /*-----------------------------------------------------------------------------
22 -----------------------------------------------------------------------------*/
24 // Uncomment this to enable run-time checking of DIMM parameters
25 // for dual-channel operation
26 // Unfortunately the code seems to chew up several K of space.
27 //#define VALIDATE_DIMM_COMPATIBILITY
29 #if CONFIG_DEBUG_RAM_SETUP
30 #define RAM_DEBUG_MESSAGE(x) print_debug(x)
31 #define RAM_DEBUG_HEX32(x) print_debug_hex32(x)
32 #define RAM_DEBUG_HEX8(x) print_debug_hex8(x)
33 #define DUMPNORTH() dump_pci_device(PCI_DEV(0, 0, 0))
35 #define RAM_DEBUG_MESSAGE(x)
36 #define RAM_DEBUG_HEX32(x)
37 #define RAM_DEBUG_HEX8(x)
41 #define E7501_SDRAM_MODE (SDRAM_BURST_INTERLEAVED | SDRAM_BURST_4)
42 #define SPD_ERROR "Error reading SPD info\n"
44 // NOTE: This used to be 0x100000.
45 // That doesn't work on systems where A20M# is asserted, because
46 // attempts to access 0x1000NN end up accessing 0x0000NN.
47 #define RCOMP_MMIO 0x200000
54 static const uint32_t refresh_frequency[] = {
55 /* Relative frequency (array value) of each E7501 Refresh Mode Select
56 * (RMS) value (array index)
57 * 0 == least frequent refresh (longest interval between refreshes)
67 0, 2, 3, 1, 0, 0, 0, 4
70 static const uint32_t refresh_rate_map[] = {
71 /* Map the JEDEC spd refresh rates (array index) to E7501 Refresh Mode
72 * Select values (array value)
73 * These are all the rates defined by JESD21-C Appendix D, Rev. 1.0
74 * The E7501 supports only 15.6 us (1), 7.8 us (2), 64 us (3), and
75 * 64 clock (481 ns) (7) refresh.
76 * [0] == 15.625 us -> 15.6 us
77 * [1] == 3.9 us -> 481 ns
78 * [2] == 7.8 us -> 7.8 us
79 * [3] == 31.3 us -> 15.6 us
80 * [4] == 62.5 us -> 15.6 us
81 * [5] == 125 us -> 64 us
86 #define MAX_SPD_REFRESH_RATE ((sizeof(refresh_rate_map) / sizeof(uint32_t)) - 1)
88 // SPD parameters that must match for dual-channel operation
89 static const uint8_t dual_channel_parameters[] = {
95 SPD_PRIMARY_SDRAM_WIDTH,
96 SPD_NUM_BANKS_PER_SDRAM
100 * Table: constant_register_values
102 static const long constant_register_values[] = {
103 /* SVID - Subsystem Vendor Identification Register
105 * [15:00] Subsytem Vendor ID (Indicates system board vendor)
107 /* SID - Subsystem Identification Register
109 * [15:00] Subsystem ID
111 // Not everyone wants to be Super Micro Computer, Inc.
112 // The mainboard should set this if desired.
113 // 0x2c, 0, (0x15d9 << 0) | (0x3580 << 16),
116 * (DRAM Read Timing Control, if similar to 855PM?)
118 * This register has something to do with CAS latencies,
119 * possibily this is the real chipset control.
120 * At 0x00 CAS latency 1.5 works.
121 * At 0x06 CAS latency 2.5 works.
122 * At 0x01 CAS latency 2.0 works.
124 /* This is still undocumented in e7501, but with different values
125 * CAS 2.0 values taken from Intel BIOS settings, others are a guess
126 * and may be terribly wrong. Old values preserved as comments until I
127 * figure this out for sure.
128 * e7501 docs claim that CAS1.5 is unsupported, so it may or may not
130 * Steven James 02/06/2003
132 /* NOTE: values now configured in configure_e7501_cas_latency() based
133 * on SPD info and total number of DIMMs (per Intel)
136 /* FDHC - Fixed DRAM Hole Control
139 * 0 == No memory Hole
140 * 1 == Memory Hole from 15MB to 16MB
143 * PAM - Programmable Attribute Map
144 * 0x59 [1:0] Reserved
145 * 0x59 [5:4] 0xF0000 - 0xFFFFF
146 * 0x5A [1:0] 0xC0000 - 0xC3FFF
147 * 0x5A [5:4] 0xC4000 - 0xC7FFF
148 * 0x5B [1:0] 0xC8000 - 0xCBFFF
149 * 0x5B [5:4] 0xCC000 - 0xCFFFF
150 * 0x5C [1:0] 0xD0000 - 0xD3FFF
151 * 0x5C [5:4] 0xD4000 - 0xD7FFF
152 * 0x5D [1:0] 0xD8000 - 0xDBFFF
153 * 0x5D [5:4] 0xDC000 - 0xDFFFF
154 * 0x5E [1:0] 0xE0000 - 0xE3FFF
155 * 0x5E [5:4] 0xE4000 - 0xE7FFF
156 * 0x5F [1:0] 0xE8000 - 0xEBFFF
157 * 0x5F [5:4] 0xEC000 - 0xEFFFF
158 * 00 == DRAM Disabled (All Access go to memory mapped I/O space)
159 * 01 == Read Only (Reads to DRAM, Writes to memory mapped I/O space)
160 * 10 == Write Only (Writes to DRAM, Reads to memory mapped I/O space)
161 * 11 == Normal (All Access go to DRAM)
164 // Map all legacy ranges to DRAM
165 0x58, 0xcccccf7f, (0x00 << 0) | (0x30 << 8) | (0x33 << 16) | (0x33 << 24),
166 0x5C, 0xcccccccc, (0x33 << 0) | (0x33 << 8) | (0x33 << 16) | (0x33 << 24),
168 /* DRB - DRAM Row Boundary Registers
170 * An array of 8 byte registers, which hold the ending
171 * memory address assigned to each pair of DIMMS, in 64MB
174 // Conservatively say each row has 64MB of ram, we will fix this up later
175 // NOTE: These defaults allow us to prime all of the DIMMs on the board
176 // without jumping through 36-bit adddressing hoops, even if the
177 // total memory is > 4 GB. Changing these values may break do_ram_command()!
178 0x60, 0x00000000, (0x01 << 0) | (0x02 << 8) | (0x03 << 16) | (0x04 << 24),
179 // 0x64, 0x00000000, (0x05 << 0) | (0x06 << 8) | (0x07 << 16) | (0x08 << 24),
180 0x64, 0x00000000, (0x04 << 0) | (0x04 << 8) | (0x04 << 16) | (0x04 << 24),
182 /* DRA - DRAM Row Attribute Register
187 * [7:7] Device width for Odd numbered rows
188 * 0 == 8 bits wide x8
189 * 1 == 4 bits wide x4
190 * [6:4] Row Attributes for Odd numbered rows
191 * 010 == 8KB (for dual-channel)
192 * 011 == 16KB (for dual-channel)
193 * 100 == 32KB (for dual-channel)
194 * 101 == 64KB (for dual-channel)
196 * [3:3] Device width for Even numbered rows
197 * 0 == 8 bits wide x8
198 * 1 == 4 bits wide x4
199 * [2:0] Row Attributes for Even numbered rows
200 * 010 == 8KB (for dual-channel)
201 * 011 == 16KB (for dual-channel)
202 * 100 == 32KB (for dual-channel)
203 * 101 == 64KB (This page size appears broken)
206 // NOTE: overridden by configure_e7501_row_attributes(), later
209 /* DRT - DRAM Timing Register
212 * [29:29] Back to Back Write-Read Turn Around
213 * 0 == 3 clocks between WR-RD commands
214 * 1 == 2 clocks between WR-RD commands
215 * [28:28] Back to Back Read-Write Turn Around
216 * 0 == 5 clocks between RD-WR commands
217 * 1 == 4 clocks between RD-WR commands
218 * [27:27] Back to Back Read Turn Around
219 * 0 == 4 clocks between RD commands
220 * 1 == 3 clocks between RD commands
221 * [26:24] Read Delay (tRD)
227 * [18:16] DRAM idle timer
229 * 011 == 16 dram clocks
232 * [10:09] Active to Precharge (tRAS)
238 * [05:04] Cas Latency (tCL)
241 * 10 == Reserved (was 1.5 Clocks for E7500)
243 * [03:03] Write Ras# to Cas# Delay (tRCD)
246 * [02:01] Read RAS# to CAS# Delay (tRCD)
249 * 10 == 3 DRAM Clocks
250 * 11 == 2 DRAM Clocks
251 * [00:00] DRAM RAS# to Precharge (tRP)
256 // Some earlier settings:
257 /* Most aggressive settings possible */
258 // 0x78, 0xc0fff8c4, (1<<29)|(1<<28)|(1<<27)|(2<<24)|(2<<9)|CAS_LATENCY|(1<<3)|(1<<1)|(1<<0),
259 // 0x78, 0xc0f8f8c0, (1<<29)|(1<<28)|(1<<27)|(1<<24)|(1<<16)|(2<<9)|CAS_LATENCY|(1<<3)|(3<<1)|(1<<0),
260 // 0x78, 0xc0f8f9c0, (1<<29)|(1<<28)|(1<<27)|(1<<24)|(1<<16)|(2<<9)|CAS_LATENCY|(1<<3)|(3<<1)|(1<<0),
262 // The only things we need to set here are DRAM idle timer, Back-to-Back Read Turnaround, and
263 // Back-to-Back Write-Read Turnaround. All others are configured based on SPD.
264 // 0x78, 0xD7F8FFFF, (1 << 29) | (1 << 27) | (1 << 16),
265 0x78, 0xC7F8FFFF, (0x03<<16)|(0x28<<24),
267 /* FIXME why was I attempting to set a reserved bit? */
270 /* DRC - DRAM Contoller Mode Register
273 * [29:29] Initialization Complete
278 * 0 == Single channel
280 * [21:20] DRAM Data Integrity Mode
281 * 00 == Disabled, no ECC
283 * 10 == Error checking, using chip-kill, with correction
285 * [19:18] DRB Granularity (Read-Only)
286 * 00 == 32 MB quantities (single channel mode)
287 * 01 == 64 MB quantities (dual-channel mode)
290 * [17:17] (Intel Undocumented) should always be set to 1 (SJM: comment inconsistent with current setting, below)
291 * [16:16] Command Per Clock - Address/Control Assertion Rule (CPC)
295 * [10:08] Refresh mode select
296 * 000 == Refresh disabled
297 * 001 == Refresh interval 15.6 usec
298 * 010 == Refresh interval 7.8 usec
299 * 011 == Refresh interval 64 usec
300 * 111 == Refresh every 64 clocks (fast refresh)
302 * [06:04] Mode Select (SMS)
303 * 000 == Reserved (was Self Refresh Mode in E7500)
305 * 010 == All Banks Precharge
306 * 011 == Mode Register Set
307 * 100 == Extended Mode Register Set
310 * 111 == Normal Operation
313 // .long 0x7c, 0xffcefcff, (1<<22)|(2 << 20)|(1 << 16)| (0 << 8),
314 // .long 0x7c, 0xff8cfcff, (1<<22)|(2 << 20)|(1 << 17)|(1 << 16)| (0 << 8),
315 // .long 0x7c, 0xff80fcff, (1<<22)|(2 << 20)|(1 << 18)|(1 << 17)|(1 << 16)| (0 << 8),
317 // Default to dual-channel mode, ECC, 1-clock address/cmd hold
318 // NOTE: configure_e7501_dram_controller_mode() configures further
319 // 0x7c, 0xff8ef8ff, (1 << 22) | (2 << 20) | (1 << 16) | (0 << 8),
320 0x7c, 0xffcef8f7, 0x00210008,
322 /* Another Intel undocumented register
324 * [31:31] Purpose unknown
325 * [26:26] Master DLL Reset?
326 * 0 == Normal operation?
328 * [07:07] Periodic memory recalibration?
331 * [04:04] Receive FIFO RE-Sync?
332 * 0 == Normal operation?
335 // NOTE: Some factory BIOSs don't do this.
336 // Doesn't seem to matter either way.
337 0x88, 0xffffff00, 0x80,
339 /* CLOCK_DIS - CK/CK# Disable Register
341 * [7:7] DDR Frequency
342 * 0 == 100 MHz (200 MHz data rate)
343 * 1 == 133 MHz (266 MHz data rate)
358 // NOTE: Disable all clocks initially; turn ones we need back on
359 // in enable_e7501_clocks()
360 0x8C, 0xfffffff0, 0xf,
362 /* TOLM - Top of Low Memory Register
364 * [15:11] Top of low memory (TOLM)
365 * The address below 4GB that should be treated as RAM,
366 * on a 128MB granularity.
369 /* REMAPBASE - Remap Base Address Regsiter
372 * [09:00] Remap Base Address [35:26] 64M aligned
373 * Bits [25:0] are assumed to be 0.
376 // NOTE: TOLM overridden by configure_e7501_ram_addresses()
377 0xc4, 0xfc0007ff, (0x2000 << 0) | (0x3ff << 16),
379 /* REMAPLIMIT - Remap Limit Address Register
382 * [09:00] Remap Limit Address [35:26] 64M aligned
383 * When remaplimit < remapbase the remap window is disabled.
387 /* DVNP - Device Not Present Register
390 * [04:04] Device 4 Function 1 Present
393 * [03:03] Device 3 Function 1 Present
396 * [02:02] Device 2 Function 1 Present
400 * [00:00] Device 0 Function 1 Present
405 // Enable D0:D1, disable D2:F1, D3:F1, D4:F1
406 // 0xe0, 0xffffffe2, (1 << 4) | (1 << 3) | (1 << 2) | (0 << 0),
407 0xe0, 0xfffffffa, 0x0,
410 0xd8, 0xffff9fff, 0x00000000,
412 // Undocumented - this is pure conjecture based on similarity to 855PM
413 /* MCHTST - MCH Test Register
415 * [31:31] Purpose unknown
416 * [30:30] Purpose unknown
417 * [29:23] Unknown - not used?
418 * [22:22] System Memory MMR Enable
419 * 0 == Disable: mem space and BAR at 0x14 are not accessible
420 * 1 == Enable: mem space and BAR at 0x14 are accessible
421 * [21:20] Purpose unknown
422 * [19:02] Unknown - not used?
423 * [01:01] D6EN (Device #6 enable)
426 * [00:00] Unknown - not used?
429 // 0xf4, 0x3f8ffffd, 0x40300002,
431 #ifdef SUSPICIOUS_LOOKING_CODE
432 // SJM: Undocumented.
433 // This will access D2:F0:0x50, is this correct??
434 0x1050, 0xffffffcf, 0x00000030,
438 /* DDR RECOMP tables */
440 // Slew table for 1x drive?
441 static const uint32_t maybe_1x_slew_table[] = {
442 0x44332211, 0xc9776655, 0xffffffff, 0xffffffff,
443 0x22111111, 0x55444332, 0xfffca876, 0xffffffff,
447 // Slew table for 2x drive?
448 static const uint32_t maybe_2x_slew_table[] = {
449 0x00000000, 0x76543210, 0xffffeca8, 0xffffffff,
450 0x21000000, 0xa8765432, 0xffffffec, 0xffffffff,
453 // Pull Up / Pull Down offset table, if analogous to IXP2800?
454 static const uint32_t maybe_pull_updown_offset_table[] = {
455 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
456 0x88888888, 0x88888888, 0x88888888, 0x88888888,
459 /*-----------------------------------------------------------------------------
461 -----------------------------------------------------------------------------*/
463 #define SLOW_DOWN_IO inb(0x80)
464 //#define SLOW_DOWN_IO udelay(40);
466 /* Estimate that SLOW_DOWN_IO takes about 50&76us */
467 /* delay for 200us */
470 static void do_delay(void)
473 for (i = 0; i < 16; i++) {
478 #define DO_DELAY do_delay()
484 #define EXTRA_DELAY DO_DELAY
486 /*-----------------------------------------------------------------------------
487 Handle (undocumented) control bits in device 0:6.0
488 -----------------------------------------------------------------------------*/
493 static void ram_handle_d060_1(void)
497 dword = pci_read_config32(PCI_DEV(0,0,0), MAYBE_MCHTST);
499 pci_write_config32(PCI_DEV(0,0,0), MAYBE_MCHTST, dword);
501 dword = pci_read_config32(PCI_DEV(0,6,0), 0xf0);
503 pci_write_config32(PCI_DEV(0,6,0), 0xf0, dword);
505 dword = pci_read_config32(PCI_DEV(0,0,0), MAYBE_MCHTST);
507 pci_write_config32(PCI_DEV(0,0,0), MAYBE_MCHTST, dword);
513 static void ram_handle_d060_2(void)
518 revision = pci_read_config8(PCI_DEV(0,0,0), 0x08);
520 dword = pci_read_config32(PCI_DEV(0,0,0), MAYBE_MCHTST);
522 pci_write_config32(PCI_DEV(0,0,0), MAYBE_MCHTST, dword);
524 dword = pci_read_config32(PCI_DEV(0,6,0), 0xf0);
526 pci_write_config32(PCI_DEV(0,6,0), 0xf0, dword);
528 dword = pci_read_config32(PCI_DEV(0,0,0), MAYBE_MCHTST);
530 pci_write_config32(PCI_DEV(0,0,0), MAYBE_MCHTST, dword);
534 /*-----------------------------------------------------------------------------
535 Serial presence detect (SPD) functions:
536 -----------------------------------------------------------------------------*/
538 static void die_on_spd_error(int spd_return_value)
540 if (spd_return_value < 0)
541 die("Error reading SPD info\n");
545 * Calculate the page size for each physical bank of the DIMM:
546 * log2(page size) = (# columns) + log2(data width)
548 * NOTE: Page size is the total number of data bits in a row.
550 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
551 * @return log2(page size) for each side of the DIMM.
553 static struct dimm_size sdram_spd_get_page_size(uint16_t dimm_socket_address)
555 uint16_t module_data_width;
557 struct dimm_size pgsz;
563 value = spd_read_byte(dimm_socket_address, SPD_NUM_COLUMNS);
566 pgsz.side1 = value & 0xf; // # columns in bank 1
568 /* Get the module data width and convert it to a power of two */
570 spd_read_byte(dimm_socket_address, SPD_MODULE_DATA_WIDTH_MSB);
573 module_data_width = (value & 0xff) << 8;
576 spd_read_byte(dimm_socket_address, SPD_MODULE_DATA_WIDTH_LSB);
579 module_data_width |= (value & 0xff);
581 pgsz.side1 += log2(module_data_width);
584 value = spd_read_byte(dimm_socket_address, SPD_NUM_DIMM_BANKS);
588 die("Bad SPD value\n");
591 pgsz.side2 = pgsz.side1; // Assume symmetric banks until we know differently
593 spd_read_byte(dimm_socket_address, SPD_NUM_COLUMNS);
596 if ((value & 0xf0) != 0) {
598 pgsz.side2 -= value & 0xf; /* Subtract out columns on side 1 */
599 pgsz.side2 += (value >> 4) & 0xf; /* Add in columns on side 2 */
607 return pgsz; // Never reached
611 * Read the width in bits of each DIMM side's DRAMs via SPD (i.e. 4, 8, 16).
613 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
614 * @return Width in bits of each DIMM side's DRAMs.
616 static struct dimm_size sdram_spd_get_width(uint16_t dimm_socket_address)
619 struct dimm_size width;
625 spd_read_byte(dimm_socket_address, SPD_PRIMARY_SDRAM_WIDTH);
626 die_on_spd_error(value);
628 width.side1 = value & 0x7f; // Mask off bank 2 flag
631 width.side2 = width.side1 << 1; // Bank 2 exists and is double-width
633 // If bank 2 exists, it's the same width as bank 1
635 spd_read_byte(dimm_socket_address, SPD_NUM_DIMM_BANKS);
636 die_on_spd_error(value);
638 #ifdef ROMCC_IF_BUG_FIXED
640 width.side2 = width.side1;
644 width.side2 = width.side1;
657 * Calculate the log base 2 size in bits of both DIMM sides.
659 * log2(# bits) = (# columns) + log2(data width) +
660 * (# rows) + log2(banks per SDRAM)
662 * Note that it might be easier to use SPD byte 31 here, it has the DIMM size
663 * as a multiple of 4MB. The way we do it now we can size both sides of an
666 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
667 * @return log2(number of bits) for each side of the DIMM.
669 static struct dimm_size spd_get_dimm_size(unsigned dimm_socket_address)
673 // Start with log2(page size)
674 struct dimm_size sz = sdram_spd_get_page_size(dimm_socket_address);
678 value = spd_read_byte(dimm_socket_address, SPD_NUM_ROWS);
679 die_on_spd_error(value);
681 sz.side1 += value & 0xf;
687 sz.side2 += value >> 4; // Asymmetric
689 sz.side2 += value; // Symmetric
693 spd_read_byte(dimm_socket_address,
694 SPD_NUM_BANKS_PER_SDRAM);
695 die_on_spd_error(value);
706 #ifdef VALIDATE_DIMM_COMPATIBILITY
709 * Determine whether two DIMMs have the same value for an SPD parameter.
711 * @param spd_byte_number The SPD byte number to compare in both DIMMs.
712 * @param dimm0_address SMBus address of the 1st DIMM socket to interrogate.
713 * @param dimm1_address SMBus address of the 2nd DIMM socket to interrogate.
714 * @return 1 if both DIMM sockets report the same value for the specified
715 * SPD parameter, 0 if the values differed or an error occurred.
717 static uint8_t are_spd_values_equal(uint8_t spd_byte_number,
718 uint16_t dimm0_address,
719 uint16_t dimm1_address)
722 int dimm0_value = spd_read_byte(dimm0_address, spd_byte_number);
723 int dimm1_value = spd_read_byte(dimm1_address, spd_byte_number);
725 if ((dimm0_value >= 0) && (dimm1_value >= 0)
726 && (dimm0_value == dimm1_value))
734 * Scan for compatible DIMMs.
736 * The code in this module only supports dual-channel operation, so we test
737 * that compatible DIMMs are paired.
739 * @param ctrl PCI addresses of memory controller functions, and SMBus
740 * addresses of DIMM slots on the mainboard.
741 * @return A bitmask indicating which of the possible sockets for each channel
742 * was found to contain a compatible DIMM.
743 * Bit 0 corresponds to the closest socket for channel 0
744 * Bit 1 to the next socket for channel 0
746 * Bit MAX_DIMM_SOCKETS_PER_CHANNEL-1 to the last socket for channel 0
747 * Bit MAX_DIMM_SOCKETS_PER_CHANNEL is the closest socket for channel 1
749 * Bit 2*MAX_DIMM_SOCKETS_PER_CHANNEL-1 is the last socket for channel 1
751 static uint8_t spd_get_supported_dimms(const struct mem_controller *ctrl)
754 uint8_t dimm_mask = 0;
756 // Have to increase size of dimm_mask if this assertion is violated
757 ASSERT(MAX_DIMM_SOCKETS_PER_CHANNEL <= 4);
759 // Find DIMMs we can support on channel 0.
760 // Then see if the corresponding channel 1 DIMM has the same parameters,
761 // since we only support dual-channel.
763 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
765 uint16_t channel0_dimm = ctrl->channel0[i];
766 uint16_t channel1_dimm = ctrl->channel1[i];
767 uint8_t bDualChannel = 1;
768 #ifdef VALIDATE_DIMM_COMPATIBILITY
769 struct dimm_size page_size;
770 struct dimm_size sdram_width;
774 if (channel0_dimm == 0)
775 continue; // No such socket on this mainboard
777 if (spd_read_byte(channel0_dimm, SPD_MEMORY_TYPE) !=
778 SPD_MEMORY_TYPE_SDRAM_DDR)
781 #ifdef VALIDATE_DIMM_COMPATIBILITY
782 if (spd_read_byte(channel0_dimm, SPD_MODULE_VOLTAGE) !=
784 continue; // Unsupported voltage
786 // E7501 does not support unregistered DIMMs
788 spd_read_byte(channel0_dimm, SPD_MODULE_ATTRIBUTES);
789 if (!(spd_value & MODULE_REGISTERED) || (spd_value < 0))
792 // Must support burst = 4 for dual-channel operation on E7501
793 // NOTE: for single-channel, burst = 8 is required
795 spd_read_byte(channel0_dimm,
796 SPD_SUPPORTED_BURST_LENGTHS);
797 if (!(spd_value & SPD_BURST_LENGTH_4) || (spd_value < 0))
800 page_size = sdram_spd_get_page_size(channel0_dimm);
801 sdram_width = sdram_spd_get_width(channel0_dimm);
803 // Validate DIMM page size
804 // The E7501 only supports page sizes of 4, 8, 16, or 32 KB per channel
805 // NOTE: 4 KB = 32 Kb = 2^15
806 // 32 KB = 262 Kb = 2^18
808 if ((page_size.side1 < 15) || (page_size.side1 > 18))
811 // If DIMM is double-sided, verify side2 page size
812 if (page_size.side2 != 0) {
813 if ((page_size.side2 < 15)
814 || (page_size.side2 > 18))
817 // Validate SDRAM width
818 // The E7501 only supports x4 and x8 devices
820 if ((sdram_width.side1 != 4) && (sdram_width.side1 != 8))
823 // If DIMM is double-sided, verify side2 width
824 if (sdram_width.side2 != 0) {
825 if ((sdram_width.side2 != 4)
826 && (sdram_width.side2 != 8))
830 // Channel 0 DIMM looks compatible.
831 // Now see if it is paired with the proper DIMM on channel 1.
833 ASSERT(channel1_dimm != 0); // No such socket on this mainboard??
835 // NOTE: unpopulated DIMMs cause read to fail
837 spd_read_byte(channel1_dimm, SPD_MODULE_ATTRIBUTES);
838 if (!(spd_value & MODULE_REGISTERED) || (spd_value < 0)) {
840 print_debug("Skipping un-matched DIMMs - only dual-channel operation supported\n");
843 #ifdef VALIDATE_DIMM_COMPATIBILITY
845 spd_read_byte(channel1_dimm,
846 SPD_SUPPORTED_BURST_LENGTHS);
847 if (!(spd_value & SPD_BURST_LENGTH_4) || (spd_value < 0))
851 for (j = 0; j < sizeof(dual_channel_parameters); ++j) {
852 if (!are_spd_values_equal
853 (dual_channel_parameters[j], channel0_dimm,
862 // Code around ROMCC bug in optimization of "if" statements
863 #ifdef ROMCC_IF_BUG_FIXED
865 // Made it through all the checks, this DIMM pair is usable
866 dimm_mask |= ((1 << i) | (1 << (MAX_DIMM_SOCKETS_PER_CHANNEL + i)));
868 print_debug("Skipping un-matched DIMMs - only dual-channel operation supported\n");
870 switch (bDualChannel) {
872 print_debug("Skipping un-matched DIMMs - only dual-channel operation supported\n");
876 // Made it through all the checks, this DIMM pair is usable
877 dimm_mask |= (1 << i) | (1 << (MAX_DIMM_SOCKETS_PER_CHANNEL + i));
886 /*-----------------------------------------------------------------------------
887 SDRAM configuration functions:
888 -----------------------------------------------------------------------------*/
891 * Send the specified command to all DIMMs.
893 * @param command Specifies the command to be sent to the DIMMs.
894 * @param jedec_mode_bits For the MRS & EMRS commands, bits 0-12 contain the
895 * register value in JEDEC format.
897 static void do_ram_command(uint8_t command, uint16_t jedec_mode_bits)
899 uint8_t dimm_start_64M_multiple;
900 uint32_t dimm_start_address;
901 uint32_t dram_controller_mode;
904 // Configure the RAM command
905 dram_controller_mode = pci_read_config32(PCI_DEV(0, 0, 0), DRC);
906 dram_controller_mode &= 0xFFFFFF8F;
907 dram_controller_mode |= command;
908 pci_write_config32(PCI_DEV(0, 0, 0), DRC, dram_controller_mode);
910 // RAM_COMMAND_NORMAL is an exception.
911 // It affects only the memory controller and does not need to be "sent" to the DIMMs.
912 if (command == RAM_COMMAND_NORMAL)
915 // NOTE: for mode select commands, some of the location address bits are part of the command
916 // Map JEDEC mode bits to E7505
917 if (command == RAM_COMMAND_MRS) {
918 // Host address lines [25:18] map to DIMM address lines [7:0]
919 // Host address lines [17:16] map to DIMM address lines [9:8]
920 // Host address lines [15:4] map to DIMM address lines [11:0]
921 dimm_start_address = (jedec_mode_bits & 0x00ff) << 18;
922 dimm_start_address |= (jedec_mode_bits & 0x0300) << 8;
923 dimm_start_address |= (jedec_mode_bits & 0x0fff) << 4;
924 } else if (command == RAM_COMMAND_EMRS) {
925 // Host address lines [15:4] map to DIMM address lines [11:0]
926 dimm_start_address = (jedec_mode_bits << 4);
928 ASSERT(jedec_mode_bits == 0);
929 dimm_start_address = 0;
932 // Send the command to all DIMMs by accessing a memory location within each
934 dimm_start_64M_multiple = 0;
936 for (i = 0; i < (MAX_NUM_CHANNELS * MAX_DIMM_SOCKETS_PER_CHANNEL); ++i) {
938 uint8_t dimm_end_64M_multiple = pci_read_config8(PCI_DEV(0, 0, 0), DRB_ROW_0 + i);
940 if (dimm_end_64M_multiple > dimm_start_64M_multiple) {
941 dimm_start_address &= 0x3ffffff;
942 dimm_start_address |= dimm_start_64M_multiple << 26;
943 read32(dimm_start_address);
944 // Set the start of the next DIMM
945 dimm_start_64M_multiple = dimm_end_64M_multiple;
952 * Set the mode register of all DIMMs.
954 * The proper CAS# latency setting is added to the mode bits specified
957 * @param jedec_mode_bits For the MRS & EMRS commands, bits 0-12 contain the
958 * register value in JEDEC format.
960 static void set_ram_mode(uint16_t jedec_mode_bits)
962 ASSERT(!(jedec_mode_bits & SDRAM_CAS_MASK));
964 uint32_t dram_cas_latency =
965 pci_read_config32(PCI_DEV(0, 0, 0), DRT) & DRT_CAS_MASK;
967 switch (dram_cas_latency) {
969 jedec_mode_bits |= SDRAM_CAS_2_5;
973 jedec_mode_bits |= SDRAM_CAS_2_0;
981 do_ram_command(RAM_COMMAND_MRS, jedec_mode_bits);
984 /*-----------------------------------------------------------------------------
985 DIMM-independant configuration functions:
986 -----------------------------------------------------------------------------*/
989 * Configure the E7501's DRAM Row Boundary (DRB) registers for the memory
990 * present in the specified DIMM.
992 * @param dimm_log2_num_bits Specifies log2(number of bits) for each side of
994 * @param total_dram_64M_multiple Total DRAM in the system (as a multiple of
995 * 64 MB) for DIMMs < dimm_index.
996 * @param dimm_index Which DIMM pair is being processed
997 * (0..MAX_DIMM_SOCKETS_PER_CHANNEL).
998 * @return New multiple of 64 MB total DRAM in the system.
1000 static uint8_t configure_dimm_row_boundaries(struct dimm_size dimm_log2_num_bits, uint8_t total_dram_64M_multiple, unsigned dimm_index)
1004 ASSERT(dimm_index < MAX_DIMM_SOCKETS_PER_CHANNEL);
1006 // DIMM sides must be at least 32 MB
1007 ASSERT(dimm_log2_num_bits.side1 >= 28);
1008 ASSERT((dimm_log2_num_bits.side2 == 0)
1009 || (dimm_log2_num_bits.side2 >= 28));
1011 // In dual-channel mode, we are called only once for each pair of DIMMs.
1012 // Each time we process twice the capacity of a single DIMM.
1014 // Convert single DIMM capacity to paired DIMM capacity
1015 // (multiply by two ==> add 1 to log2)
1016 dimm_log2_num_bits.side1++;
1017 if (dimm_log2_num_bits.side2 > 0)
1018 dimm_log2_num_bits.side2++;
1020 // Add the capacity of side 1 this DIMM pair (as a multiple of 64 MB)
1021 // to the total capacity of the system
1022 // NOTE: 64 MB == 512 Mb, and log2(512 Mb) == 29
1024 total_dram_64M_multiple += (1 << (dimm_log2_num_bits.side1 - 29));
1026 // Configure the boundary address for the row on side 1
1027 pci_write_config8(PCI_DEV(0, 0, 0), DRB_ROW_0 + (dimm_index << 1),
1028 total_dram_64M_multiple);
1030 // If the DIMMs are double-sided, add the capacity of side 2 this DIMM pair
1031 // (as a multiple of 64 MB) to the total capacity of the system
1032 if (dimm_log2_num_bits.side2 >= 29)
1033 total_dram_64M_multiple +=
1034 (1 << (dimm_log2_num_bits.side2 - 29));
1036 // Configure the boundary address for the row (if any) on side 2
1037 pci_write_config8(PCI_DEV(0, 0, 0), DRB_ROW_1 + (dimm_index << 1),
1038 total_dram_64M_multiple);
1040 // Update boundaries for rows subsequent to these.
1041 // These settings will be overridden by a subsequent call if a populated physical slot exists
1043 for (i = dimm_index + 1; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
1044 pci_write_config8(PCI_DEV(0, 0, 0), DRB_ROW_0 + (i << 1),
1045 total_dram_64M_multiple);
1046 pci_write_config8(PCI_DEV(0, 0, 0), DRB_ROW_1 + (i << 1),
1047 total_dram_64M_multiple);
1050 return total_dram_64M_multiple;
1054 * Set the E7501's DRAM row boundary addresses & its Top Of Low Memory (TOLM).
1056 * If necessary, set up a remap window so we don't waste DRAM that ordinarily
1057 * would lie behind addresses reserved for memory-mapped I/O.
1059 * @param ctrl PCI addresses of memory controller functions, and SMBus
1060 * addresses of DIMM slots on the mainboard.
1061 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
1063 static void configure_e7501_ram_addresses(const struct mem_controller
1064 *ctrl, uint8_t dimm_mask)
1067 uint8_t total_dram_64M_multiple = 0;
1069 // Configure the E7501's DRAM row boundaries
1070 // Start by zeroing out the temporary initial configuration
1071 pci_write_config32(PCI_DEV(0, 0, 0), DRB_ROW_0, 0);
1072 pci_write_config32(PCI_DEV(0, 0, 0), DRB_ROW_4, 0);
1074 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
1076 uint16_t dimm_socket_address = ctrl->channel0[i];
1077 struct dimm_size sz;
1079 if (!(dimm_mask & (1 << i)))
1080 continue; // This DIMM not present
1082 sz = spd_get_dimm_size(dimm_socket_address);
1084 RAM_DEBUG_MESSAGE("dimm size =");
1085 RAM_DEBUG_HEX32((u32)sz.side1);
1086 RAM_DEBUG_MESSAGE(" ");
1087 RAM_DEBUG_HEX32((u32)sz.side2);
1088 RAM_DEBUG_MESSAGE("\n");
1091 die("Bad SPD value\n");
1093 total_dram_64M_multiple =
1094 configure_dimm_row_boundaries(sz, total_dram_64M_multiple, i);
1097 // Configure the Top Of Low Memory (TOLM) in the E7501
1098 // This address must be a multiple of 128 MB that is less than 4 GB.
1099 // NOTE: 16-bit wide TOLM register stores only the highest 5 bits of a 32-bit address
1100 // in the highest 5 bits.
1102 // We set TOLM to the smaller of 0xC0000000 (3 GB) or the total DRAM in the system.
1103 // This reserves addresses from 0xC0000000 - 0xFFFFFFFF for non-DRAM purposes
1104 // such as flash and memory-mapped I/O.
1106 // If there is more than 3 GB of DRAM, we define a remap window which
1107 // makes the DRAM "behind" the reserved region available above the top of physical
1110 // NOTE: 0xC0000000 / (64 MB) == 0x30
1112 if (total_dram_64M_multiple <= 0x30) {
1114 // <= 3 GB total RAM
1116 /* I should really adjust all of this in C after I have resources
1117 * to all of the pci devices.
1120 // Round up to 128MB granularity
1121 // SJM: Is "missing" 64 MB of memory a potential issue? Should this round down?
1123 uint8_t total_dram_128M_multiple =
1124 (total_dram_64M_multiple + 1) >> 1;
1126 // Convert to high 16 bits of address
1127 uint16_t top_of_low_memory =
1128 total_dram_128M_multiple << 11;
1130 pci_write_config16(PCI_DEV(0, 0, 0), TOLM,
1137 // Set defaults for > 4 GB DRAM, i.e. remap a 1 GB (= 0x10 * 64 MB) range of memory
1138 uint16_t remap_base = total_dram_64M_multiple; // A[25:0] == 0
1139 uint16_t remap_limit = total_dram_64M_multiple + 0x10 - 1; // A[25:0] == 0xF
1143 pci_write_config16(PCI_DEV(0, 0, 0), TOLM, 0xc000);
1145 // Define a remap window to make the RAM that would appear from 3 GB - 4 GB
1146 // visible just beyond 4 GB or the end of physical memory, whichever is larger
1147 // NOTE: 16-bit wide REMAP registers store only the highest 10 bits of a 36-bit address,
1148 // (i.e. a multiple of 64 MB) in the lowest 10 bits.
1149 // NOTE: 0x100000000 / (64 MB) == 0x40
1151 if (total_dram_64M_multiple < 0x40) {
1152 remap_base = 0x40; // 0x100000000
1154 0x40 + (total_dram_64M_multiple - 0x30) - 1;
1157 pci_write_config16(PCI_DEV(0, 0, 0), REMAPBASE,
1159 pci_write_config16(PCI_DEV(0, 0, 0), REMAPLIMIT,
1165 * If we're configured to use ECC, initialize the SDRAM and clear the E7501's
1168 static void initialize_ecc(void)
1170 uint32_t dram_controller_mode;
1172 /* Test to see if ECC support is enabled */
1173 dram_controller_mode = pci_read_config32(PCI_DEV(0, 0, 0), DRC);
1174 dram_controller_mode >>= 20;
1175 dram_controller_mode &= 3;
1176 if (dram_controller_mode == 2) {
1179 RAM_DEBUG_MESSAGE("Initializing ECC state...\n");
1180 pci_write_config8(PCI_DEV(0, 0, 0), MCHCFGNS, 0x01);
1182 // Wait for scrub cycle to complete
1185 pci_read_config8(PCI_DEV(0, 0, 0), MCHCFGNS);
1186 } while ((byte & 0x08) == 0);
1188 pci_write_config8(PCI_DEV(0, 0, 0), MCHCFGNS, (byte & 0xfc) | 0x04);
1189 RAM_DEBUG_MESSAGE("ECC state initialized.\n");
1191 /* Clear the ECC error bits */
1192 pci_write_config8(PCI_DEV(0, 0, 1), DRAM_FERR, 0x03);
1193 pci_write_config8(PCI_DEV(0, 0, 1), DRAM_NERR, 0x03);
1195 // Clear DRAM Interface error bits (write-one-clear)
1196 pci_write_config32(PCI_DEV(0, 0, 1), FERR_GLOBAL, 1 << 18);
1197 pci_write_config32(PCI_DEV(0, 0, 1), NERR_GLOBAL, 1 << 18);
1203 * Program the DRAM Timing register (DRT) of the E7501 (except for CAS#
1204 * latency, which is assumed to have been programmed already), based on the
1205 * parameters of the various installed DIMMs.
1207 * @param ctrl PCI addresses of memory controller functions, and SMBus
1208 * addresses of DIMM slots on the mainboard.
1209 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
1211 static void configure_e7501_dram_timing(const struct mem_controller *ctrl,
1215 uint32_t dram_timing;
1217 uint8_t slowest_row_precharge = 0;
1218 uint8_t slowest_ras_cas_delay = 0;
1219 uint8_t slowest_active_to_precharge_delay = 0;
1220 uint32_t current_cas_latency =
1221 pci_read_config32(PCI_DEV(0, 0, 0), DRT) & DRT_CAS_MASK;
1223 // CAS# latency must be programmed beforehand
1224 ASSERT((current_cas_latency == DRT_CAS_2_0)
1225 || (current_cas_latency == DRT_CAS_2_5));
1227 // Each timing parameter is determined by the slowest DIMM
1229 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
1230 uint16_t dimm_socket_address;
1232 if (!(dimm_mask & (1 << i)))
1233 continue; // This DIMM not present
1235 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
1236 dimm_socket_address = ctrl->channel0[i];
1238 dimm_socket_address =
1239 ctrl->channel1[i - MAX_DIMM_SOCKETS_PER_CHANNEL];
1242 spd_read_byte(dimm_socket_address,
1243 SPD_MIN_ROW_PRECHARGE_TIME);
1246 if (value > slowest_row_precharge)
1247 slowest_row_precharge = value;
1250 spd_read_byte(dimm_socket_address,
1251 SPD_MIN_RAS_TO_CAS_DELAY);
1254 if (value > slowest_ras_cas_delay)
1255 slowest_ras_cas_delay = value;
1258 spd_read_byte(dimm_socket_address,
1259 SPD_MIN_ACTIVE_TO_PRECHARGE_DELAY);
1262 if (value > slowest_active_to_precharge_delay)
1263 slowest_active_to_precharge_delay = value;
1266 // NOTE for timing parameters:
1267 // At 133 MHz, 1 clock == 7.52 ns
1269 /* Read the initial state */
1270 dram_timing = pci_read_config32(PCI_DEV(0, 0, 0), DRT);
1274 // E7501 supports only 2 or 3 clocks for tRP
1275 if (slowest_row_precharge > ((22 << 2) | (2 << 0)))
1276 die("unsupported DIMM tRP"); // > 22.5 ns: 4 or more clocks
1277 else if (slowest_row_precharge > (15 << 2))
1278 dram_timing &= ~(1 << 0); // > 15.0 ns: 3 clocks
1280 dram_timing |= (1 << 0); // <= 15.0 ns: 2 clocks
1284 // E7501 supports only 2 or 3 clocks for tRCD
1285 // Use the same value for both read & write
1286 dram_timing &= ~((1 << 3) | (3 << 1));
1287 if (slowest_ras_cas_delay > ((22 << 2) | (2 << 0)))
1288 die("unsupported DIMM tRCD"); // > 22.5 ns: 4 or more clocks
1289 else if (slowest_ras_cas_delay > (15 << 2))
1290 dram_timing |= (2 << 1); // > 15.0 ns: 3 clocks
1292 dram_timing |= ((1 << 3) | (3 << 1)); // <= 15.0 ns: 2 clocks
1296 // E7501 supports only 5, 6, or 7 clocks for tRAS
1297 // 5 clocks ~= 37.6 ns, 6 clocks ~= 45.1 ns, 7 clocks ~= 52.6 ns
1298 dram_timing &= ~(3 << 9);
1300 if (slowest_active_to_precharge_delay > 52)
1301 die("unsupported DIMM tRAS"); // > 52 ns: 8 or more clocks
1302 else if (slowest_active_to_precharge_delay > 45)
1303 dram_timing |= (0 << 9); // 46-52 ns: 7 clocks
1304 else if (slowest_active_to_precharge_delay > 37)
1305 dram_timing |= (1 << 9); // 38-45 ns: 6 clocks
1307 dram_timing |= (2 << 9); // < 38 ns: 5 clocks
1311 /* Set to a 7 clock read delay. This is for 133Mhz
1312 * with a CAS latency of 2.5 if 2.0 a 6 clock
1315 dram_timing &= ~(7 << 24); // 7 clocks
1316 if (current_cas_latency == DRT_CAS_2_0)
1317 dram_timing |= (1 << 24); // 6 clocks
1320 * Back to Back Read-Write Turn Around
1322 /* Set to a 5 clock back to back read to write turn around.
1323 * 4 is a good delay if the CAS latency is 2.0 */
1325 dram_timing &= ~(1 << 28); // 5 clocks
1326 if (current_cas_latency == DRT_CAS_2_0)
1327 dram_timing |= (1 << 28); // 4 clocks
1329 pci_write_config32(PCI_DEV(0, 0, 0), DRT, dram_timing);
1338 * Determine the shortest CAS# latency that the E7501 and all DIMMs have in
1339 * common, and program the E7501 to use it.
1341 * @param ctrl PCI addresses of memory controller functions, and SMBus
1342 * addresses of DIMM slots on the mainboard.
1343 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1345 static void configure_e7501_cas_latency(const struct mem_controller *ctrl,
1350 uint32_t dram_timing;
1351 uint16_t maybe_dram_read_timing;
1354 // CAS# latency bitmasks in SPD_ACCEPTABLE_CAS_LATENCIES format
1355 // NOTE: E7501 supports only 2.0 and 2.5
1356 uint32_t system_compatible_cas_latencies =
1357 SPD_CAS_LATENCY_2_0 | SPD_CAS_LATENCY_2_5;
1358 uint32_t current_cas_latency;
1359 uint32_t dimm_compatible_cas_latencies;
1361 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
1363 uint16_t dimm_socket_address;
1365 if (!(dimm_mask & (1 << i)))
1366 continue; // This DIMM not usable
1368 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
1369 dimm_socket_address = ctrl->channel0[i];
1371 dimm_socket_address =
1372 ctrl->channel1[i - MAX_DIMM_SOCKETS_PER_CHANNEL];
1375 spd_read_byte(dimm_socket_address,
1376 SPD_ACCEPTABLE_CAS_LATENCIES);
1380 dimm_compatible_cas_latencies = value & 0x7f; // Start with all supported by DIMM
1381 current_cas_latency = 1 << log2(dimm_compatible_cas_latencies); // Max supported by DIMM
1383 // Can we support the highest CAS# latency?
1386 spd_read_byte(dimm_socket_address,
1387 SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
1391 // NOTE: At 133 MHz, 1 clock == 7.52 ns
1393 // Our bus is too fast for this CAS# latency
1394 // Remove it from the bitmask of those supported by the DIMM that are compatible
1395 dimm_compatible_cas_latencies &= ~current_cas_latency;
1397 // Can we support the next-highest CAS# latency (max - 0.5)?
1399 current_cas_latency >>= 1;
1400 if (current_cas_latency != 0) {
1402 spd_read_byte(dimm_socket_address,
1403 SPD_SDRAM_CYCLE_TIME_2ND);
1407 dimm_compatible_cas_latencies &=
1408 ~current_cas_latency;
1410 // Can we support the next-highest CAS# latency (max - 1.0)?
1411 current_cas_latency >>= 1;
1412 if (current_cas_latency != 0) {
1414 spd_read_byte(dimm_socket_address,
1415 SPD_SDRAM_CYCLE_TIME_3RD);
1419 dimm_compatible_cas_latencies &=
1420 ~current_cas_latency;
1422 // Restrict the system to CAS# latencies compatible with this DIMM
1423 system_compatible_cas_latencies &=
1424 dimm_compatible_cas_latencies;
1426 /* go to the next DIMM */
1429 /* After all of the arduous calculation setup with the fastest
1430 * cas latency I can use.
1433 dram_timing = pci_read_config32(PCI_DEV(0, 0, 0), DRT);
1434 dram_timing &= ~(DRT_CAS_MASK);
1436 maybe_dram_read_timing =
1437 pci_read_config16(PCI_DEV(0, 0, 0), MAYBE_DRDCTL);
1438 maybe_dram_read_timing &= 0xF000;
1440 if (system_compatible_cas_latencies & SPD_CAS_LATENCY_2_0) {
1441 dram_timing |= DRT_CAS_2_0;
1442 maybe_dram_read_timing |= 0x0222;
1443 } else if (system_compatible_cas_latencies & SPD_CAS_LATENCY_2_5) {
1445 uint32_t dram_row_attributes =
1446 pci_read_config32(PCI_DEV(0, 0, 0), DRA);
1448 dram_timing |= DRT_CAS_2_5;
1450 // At CAS# 2.5, DRAM Read Timing (if that's what it its) appears to need a slightly
1451 // different value if all DIMM slots are populated
1453 if ((dram_row_attributes & 0xff)
1454 && (dram_row_attributes & 0xff00)
1455 && (dram_row_attributes & 0xff0000)
1456 && (dram_row_attributes & 0xff000000)) {
1458 // All slots populated
1459 maybe_dram_read_timing |= 0x0882;
1461 // Some unpopulated slots
1462 maybe_dram_read_timing |= 0x0662;
1465 die("No CAS# latencies compatible with all DIMMs!!\n");
1467 pci_write_config32(PCI_DEV(0, 0, 0), DRT, dram_timing);
1469 /* set master DLL reset */
1470 dword = pci_read_config32(PCI_DEV(0, 0, 0), 0x88);
1472 pci_write_config32(PCI_DEV(0, 0, 0), 0x88, dword);
1473 /* patch try register 88 is undocumented tnz */
1474 dword &= 0x0ca17fff;
1475 dword |= 0xd14a5000;
1476 pci_write_config32(PCI_DEV(0, 0, 0), 0x88, dword);
1478 pci_write_config16(PCI_DEV(0, 0, 0), MAYBE_DRDCTL,
1479 maybe_dram_read_timing);
1481 /* clear master DLL reset */
1482 dword = pci_read_config32(PCI_DEV(0, 0, 0), 0x88);
1483 dword &= ~(1 << 26);
1484 pci_write_config32(PCI_DEV(0, 0, 0), 0x88, dword);
1493 * Configure the refresh interval so that we refresh no more often than
1494 * required by the "most needy" DIMM. Also disable ECC if any of the DIMMs
1497 * @param ctrl PCI addresses of memory controller functions, and SMBus
1498 * addresses of DIMM slots on the mainboard.
1499 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1501 static void configure_e7501_dram_controller_mode(const struct
1502 mem_controller *ctrl,
1508 uint32_t controller_mode =
1509 pci_read_config32(PCI_DEV(0, 0, 0), DRC);
1510 uint32_t system_refresh_mode = (controller_mode >> 8) & 7;
1512 // Code below assumes that most aggressive settings are in
1513 // force when we are called, either via E7501 reset defaults
1514 // or by sdram_set_registers():
1518 ASSERT((controller_mode & (3 << 20)) == (2 << 20)); // ECC
1519 ASSERT(!(controller_mode & (7 << 8))); // Refresh
1521 /* Walk through _all_ dimms and find the least-common denominator for:
1526 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
1528 uint32_t dimm_refresh_mode;
1530 uint16_t dimm_socket_address;
1532 if (!(dimm_mask & (1 << i))) {
1533 continue; // This DIMM not usable
1536 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
1537 dimm_socket_address = ctrl->channel0[i];
1539 dimm_socket_address =
1541 MAX_DIMM_SOCKETS_PER_CHANNEL];
1543 // Disable ECC mode if any one of the DIMMs does not support ECC
1544 // SJM: Should we just die here? E7501 datasheet says non-ECC DIMMs aren't supported.
1547 spd_read_byte(dimm_socket_address,
1548 SPD_DIMM_CONFIG_TYPE);
1549 die_on_spd_error(value);
1550 if (value != ERROR_SCHEME_ECC) {
1551 controller_mode &= ~(3 << 20);
1554 value = spd_read_byte(dimm_socket_address, SPD_REFRESH);
1555 die_on_spd_error(value);
1556 value &= 0x7f; // Mask off self-refresh bit
1557 if (value > MAX_SPD_REFRESH_RATE) {
1558 print_err("unsupported refresh rate\n");
1561 // Get the appropriate E7501 refresh mode for this DIMM
1562 dimm_refresh_mode = refresh_rate_map[value];
1563 if (dimm_refresh_mode > 7) {
1564 print_err("unsupported refresh rate\n");
1567 // If this DIMM requires more frequent refresh than others,
1568 // update the system setting
1569 if (refresh_frequency[dimm_refresh_mode] >
1570 refresh_frequency[system_refresh_mode])
1571 system_refresh_mode = dimm_refresh_mode;
1573 #ifdef SUSPICIOUS_LOOKING_CODE
1574 // SJM NOTE: This code doesn't look right. SPD values are an order of magnitude smaller
1575 // than the clock period of the memory controller. Also, no other northbridge
1576 // looks at SPD_CMD_SIGNAL_INPUT_HOLD_TIME.
1578 // Switch to 2 clocks for address/command if required by any one of the DIMMs
1579 // NOTE: At 133 MHz, 1 clock == 7.52 ns
1581 spd_read_byte(dimm_socket_address,
1582 SPD_CMD_SIGNAL_INPUT_HOLD_TIME);
1583 die_on_spd_error(value);
1584 if (value >= 0xa0) { /* At 133MHz this constant should be 0x75 */
1585 controller_mode &= ~(1 << 16); /* Use two clock cyles instead of one */
1589 /* go to the next DIMM */
1592 controller_mode |= (system_refresh_mode << 8);
1594 // Configure the E7501
1595 pci_write_config32(PCI_DEV(0, 0, 0), DRC, controller_mode);
1599 * Configure the E7501's DRAM Row Attributes (DRA) registers based on DIMM
1600 * parameters read via SPD. This tells the controller the width of the SDRAM
1601 * chips on each DIMM side (x4 or x8) and the page size of each DIMM side
1602 * (4, 8, 16, or 32 KB).
1604 * @param ctrl PCI addresses of memory controller functions, and SMBus
1605 * addresses of DIMM slots on the mainboard.
1606 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1608 static void configure_e7501_row_attributes(const struct mem_controller
1609 *ctrl, uint8_t dimm_mask)
1612 uint32_t row_attributes = 0;
1614 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
1616 uint16_t dimm_socket_address = ctrl->channel0[i];
1617 struct dimm_size page_size;
1618 struct dimm_size sdram_width;
1620 if (!(dimm_mask & (1 << i)))
1621 continue; // This DIMM not usable
1623 // Get the relevant parameters via SPD
1624 page_size = sdram_spd_get_page_size(dimm_socket_address);
1625 sdram_width = sdram_spd_get_width(dimm_socket_address);
1627 // Update the DRAM Row Attributes.
1628 // Page size is encoded as log2(page size in bits) - log2(8 Kb)
1629 // NOTE: 8 Kb = 2^13
1630 row_attributes |= (page_size.side1 - 13) << (i << 3); // Side 1 of each DIMM is an EVEN row
1632 if (sdram_width.side2 > 0)
1633 row_attributes |= (page_size.side2 - 13) << ((i << 3) + 4); // Side 2 is ODD
1635 // Set x4 flags if appropriate
1636 if (sdram_width.side1 == 4) {
1637 row_attributes |= 0x08 << (i << 3);
1640 if (sdram_width.side2 == 4) {
1641 row_attributes |= 0x08 << ((i << 3) + 4);
1644 /* go to the next DIMM */
1647 /* Write the new row attributes register */
1648 pci_write_config32(PCI_DEV(0, 0, 0), DRA, row_attributes);
1652 * Enable clock signals for populated DIMM sockets and disable them for
1653 * unpopulated sockets (to reduce EMI).
1655 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
1657 static void enable_e7501_clocks(uint8_t dimm_mask)
1660 uint8_t clock_disable = pci_read_config8(PCI_DEV(0, 0, 0), CKDIS);
1662 pci_write_config8(PCI_DEV(0,0,0), 0x8e, 0xb0);
1664 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
1666 uint8_t socket_mask = 1 << i;
1668 if (dimm_mask & socket_mask)
1669 clock_disable &= ~socket_mask; // DIMM present, enable clock
1671 clock_disable |= socket_mask; // DIMM absent, disable clock
1674 pci_write_config8(PCI_DEV(0, 0, 0), CKDIS, clock_disable);
1677 /* DIMM-dedependent configuration functions */
1680 * DDR Receive FIFO RE-Sync (?)
1682 static void RAM_RESET_DDR_PTR(void)
1685 byte = pci_read_config8(PCI_DEV(0, 0, 0), 0x88);
1687 pci_write_config8(PCI_DEV(0, 0, 0), 0x88, byte);
1689 byte = pci_read_config8(PCI_DEV(0, 0, 0), 0x88);
1691 pci_write_config8(PCI_DEV(0, 0, 0), 0x88, byte);
1695 * Set E7501 registers that are either independent of DIMM specifics, or
1696 * establish default settings that will be overridden when we learn the
1699 * This sets PCI configuration registers to known good values based on the
1700 * table 'constant_register_values', which are a triple of configuration
1701 * register offset, mask, and bits to set.
1703 static void ram_set_d0f0_regs(void)
1706 int num_values = ARRAY_SIZE(constant_register_values);
1708 ASSERT((num_values % 3) == 0); // Bad table?
1710 for (i = 0; i < num_values; i += 3) {
1712 uint32_t register_offset = constant_register_values[i];
1713 uint32_t bits_to_mask = constant_register_values[i + 1];
1714 uint32_t bits_to_set = constant_register_values[i + 2];
1715 uint32_t register_value;
1717 // It's theoretically possible to set values for something other than D0:F0,
1718 // but it's not typically done here
1719 ASSERT(!(register_offset & 0xFFFFFF00));
1721 // bits_to_mask and bits_to_set should not reference the same bits
1722 // Again, not strictly an error, but flagged as a potential bug
1723 ASSERT((bits_to_mask & bits_to_set) == 0);
1726 pci_read_config32(PCI_DEV(0, 0, 0), register_offset);
1727 register_value &= bits_to_mask;
1728 register_value |= bits_to_set;
1730 pci_write_config32(PCI_DEV(0, 0, 0), register_offset,
1736 * Copy 64 bytes from one location to another.
1738 * @param src_addr TODO
1739 * @param dst_addr TODO
1741 static void write_8dwords(const uint32_t *src_addr, uint32_t dst_addr)
1744 for (i = 0; i < 8; i++) {
1745 write32(dst_addr, *src_addr);
1747 dst_addr += sizeof(uint32_t);
1752 * Set the E7501's (undocumented) RCOMP registers.
1754 * Per the 855PM datasheet and IXP2800 HW Initialization Reference Manual,
1755 * RCOMP registers appear to affect drive strength, pullup/pulldown offset,
1756 * and slew rate of various signal groups.
1758 * Comments below are conjecture based on apparent similarity between the
1759 * E7501 and these two chips.
1761 static void ram_set_rcomp_regs(void)
1764 uint8_t maybe_strength_control, revision;
1766 RAM_DEBUG_MESSAGE("Setting RCOMP registers.\n");
1768 /*enable access to the rcomp bar */
1769 dword = pci_read_config32(PCI_DEV(0, 0, 0), MAYBE_MCHTST);
1771 pci_write_config32(PCI_DEV(0, 0, 0), MAYBE_MCHTST, dword);
1773 // Set the RCOMP MMIO base address
1774 pci_write_config32(PCI_DEV(0, 0, 0), MAYBE_SMRBASE, RCOMP_MMIO);
1776 // Block RCOMP updates while we configure the registers
1777 dword = read32(RCOMP_MMIO + MAYBE_SMRCTL);
1779 write32(RCOMP_MMIO + MAYBE_SMRCTL, dword);
1781 /* Begin to write the RCOMP registers */
1782 write8(RCOMP_MMIO + 0x2c, 0x0);
1784 // Set CMD and DQ/DQS strength to 2x (?)
1785 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_DQCMDSTR) & 0x88;
1786 maybe_strength_control |= 0x40;
1787 write8(RCOMP_MMIO + MAYBE_DQCMDSTR, maybe_strength_control);
1788 write_8dwords(maybe_2x_slew_table, RCOMP_MMIO + 0x80);
1789 write16(RCOMP_MMIO + 0x42, 0);
1791 // Set CMD and DQ/DQS strength to 2x (?)
1792 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_DQCMDSTR) & 0xF8;
1793 maybe_strength_control |= 0x04;
1794 write8(RCOMP_MMIO + MAYBE_DQCMDSTR, maybe_strength_control);
1795 write_8dwords(maybe_2x_slew_table, RCOMP_MMIO + 0x60);
1796 write16(RCOMP_MMIO + 0x40, 0);
1798 // Set RCVEnOut# strength to 2x (?)
1799 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_RCVENSTR) & 0xF8;
1800 maybe_strength_control |= 0x04;
1801 write8(RCOMP_MMIO + MAYBE_RCVENSTR, maybe_strength_control);
1802 write_8dwords(maybe_2x_slew_table, RCOMP_MMIO + 0x1c0);
1803 write16(RCOMP_MMIO + 0x50, 0);
1805 // Set CS# strength for x4 SDRAM to 2x (?)
1806 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_CSBSTR) & 0x88;
1807 maybe_strength_control |= 0x04;
1808 write8(RCOMP_MMIO + MAYBE_CSBSTR, maybe_strength_control);
1809 write_8dwords(maybe_2x_slew_table, RCOMP_MMIO + 0x140);
1810 write16(RCOMP_MMIO + 0x48, 0);
1812 // Set CS# strength for x4 SDRAM to 2x (?)
1813 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_CSBSTR) & 0x8F;
1814 maybe_strength_control |= 0x40;
1815 write8(RCOMP_MMIO + MAYBE_CSBSTR, maybe_strength_control);
1816 write_8dwords(maybe_2x_slew_table, RCOMP_MMIO + 0x160);
1817 write16(RCOMP_MMIO + 0x4a, 0);
1819 // Set CKE strength for x4 SDRAM to 2x (?)
1820 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_CKESTR) & 0x88;
1821 maybe_strength_control |= 0x04;
1822 write8(RCOMP_MMIO + MAYBE_CKESTR, maybe_strength_control);
1823 write_8dwords(maybe_2x_slew_table, RCOMP_MMIO + 0xa0);
1824 write16(RCOMP_MMIO + 0x44, 0);
1826 // Set CKE strength for x4 SDRAM to 2x (?)
1827 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_CKESTR) & 0x8F;
1828 maybe_strength_control |= 0x40;
1829 write8(RCOMP_MMIO + MAYBE_CKESTR, maybe_strength_control);
1830 write_8dwords(maybe_2x_slew_table, RCOMP_MMIO + 0xc0);
1831 write16(RCOMP_MMIO + 0x46, 0);
1833 // Set CK strength for x4 SDRAM to 1x (?)
1834 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_CKSTR) & 0x88;
1835 maybe_strength_control |= 0x01;
1836 write8(RCOMP_MMIO + MAYBE_CKSTR, maybe_strength_control);
1837 write_8dwords(maybe_pull_updown_offset_table, RCOMP_MMIO + 0x180);
1838 write16(RCOMP_MMIO + 0x4c, 0);
1840 // Set CK strength for x4 SDRAM to 1x (?)
1841 maybe_strength_control = read8(RCOMP_MMIO + MAYBE_CKSTR) & 0x8F;
1842 maybe_strength_control |= 0x10;
1843 write8(RCOMP_MMIO + MAYBE_CKSTR, maybe_strength_control);
1844 write_8dwords(maybe_pull_updown_offset_table, RCOMP_MMIO + 0x1a0);
1845 write16(RCOMP_MMIO + 0x4e, 0);
1848 dword = read32(RCOMP_MMIO + 0x400);
1849 dword &= 0x7f7fffff;
1850 write32(RCOMP_MMIO + 0x400, dword);
1852 dword = read32(RCOMP_MMIO + 0x408);
1853 dword &= 0x7f7fffff;
1854 write32(RCOMP_MMIO + 0x408, dword);
1856 ram_handle_d060_1();
1858 dword = pci_read_config32(PCI_DEV(0,0,0), MAYBE_MCHTST);
1859 dword &= 0x3fffffff;
1860 pci_write_config32(PCI_DEV(0,0,0), MAYBE_MCHTST, dword);
1862 revision = pci_read_config8(PCI_DEV(0,0,0), 0x08);
1863 if (revision >= 3) {
1864 dword = read32(RCOMP_MMIO + MAYBE_SMRCTL);
1866 write32(RCOMP_MMIO + MAYBE_SMRCTL, dword);
1867 dword = read32(RCOMP_MMIO + MAYBE_SMRCTL);
1869 write32(RCOMP_MMIO + MAYBE_SMRCTL, dword);
1871 dword = read32(RCOMP_MMIO + MAYBE_SMRCTL);
1874 write32(RCOMP_MMIO + MAYBE_SMRCTL, dword);
1879 // Clear the RCOMP MMIO base address
1880 pci_write_config32(PCI_DEV(0, 0, 0), MAYBE_SMRBASE, 0);
1881 /*disable access to the rcomp bar */
1882 dword = pci_read_config32(PCI_DEV(0, 0, 0), MAYBE_MCHTST);
1883 dword &= ~(1 << 22);
1884 pci_write_config32(PCI_DEV(0, 0, 0), MAYBE_MCHTST, dword);
1888 /*-----------------------------------------------------------------------------
1890 -----------------------------------------------------------------------------*/
1893 * Go through the JEDEC initialization sequence for all DIMMs, then enable
1894 * refresh and initialize ECC and memory to zero. Upon exit, SDRAM is up
1897 * @param controllers Not used.
1898 * @param ctrl PCI addresses of memory controller functions, and SMBus
1899 * addresses of DIMM slots on the mainboard.
1901 static void sdram_enable(int controllers,
1902 const struct mem_controller *ctrl)
1904 uint8_t dimm_mask = pci_read_config16(PCI_DEV(0, 0, 0), SKPD);
1905 uint32_t dram_controller_mode;
1910 /* 1 & 2 Power up and start clocks */
1911 RAM_DEBUG_MESSAGE("Ram Enable 1\n");
1912 RAM_DEBUG_MESSAGE("Ram Enable 2\n");
1914 /* A 200us delay is needed */
1915 DO_DELAY; EXTRA_DELAY;
1918 RAM_DEBUG_MESSAGE("Ram Enable 3\n");
1919 do_ram_command(RAM_COMMAND_NOP, 0);
1922 /* 4 Precharge all */
1923 RAM_DEBUG_MESSAGE("Ram Enable 4\n");
1924 do_ram_command(RAM_COMMAND_PRECHARGE, 0);
1926 /* wait until the all banks idle state... */
1928 /* 5. Issue EMRS to enable DLL */
1929 RAM_DEBUG_MESSAGE("Ram Enable 5\n");
1930 do_ram_command(RAM_COMMAND_EMRS,
1931 SDRAM_EXTMODE_DLL_ENABLE |
1932 SDRAM_EXTMODE_DRIVE_NORMAL);
1936 RAM_DEBUG_MESSAGE("Ram Enable 6\n");
1937 set_ram_mode(E7501_SDRAM_MODE | SDRAM_MODE_DLL_RESET);
1939 /* Ensure a 200us delay between the DLL reset in step 6 and the final
1940 * mode register set in step 9.
1941 * Infineon needs this before any other command is sent to the ram.
1943 DO_DELAY; EXTRA_DELAY;
1945 /* 7 Precharge all */
1946 RAM_DEBUG_MESSAGE("Ram Enable 7\n");
1947 do_ram_command(RAM_COMMAND_PRECHARGE, 0);
1950 /* 8 Now we need 2 AUTO REFRESH / CBR cycles to be performed */
1951 RAM_DEBUG_MESSAGE("Ram Enable 8\n");
1952 do_ram_command(RAM_COMMAND_CBR, 0);
1954 do_ram_command(RAM_COMMAND_CBR, 0);
1957 /* And for good luck 6 more CBRs */
1958 do_ram_command(RAM_COMMAND_CBR, 0);
1960 do_ram_command(RAM_COMMAND_CBR, 0);
1962 do_ram_command(RAM_COMMAND_CBR, 0);
1964 do_ram_command(RAM_COMMAND_CBR, 0);
1966 do_ram_command(RAM_COMMAND_CBR, 0);
1968 do_ram_command(RAM_COMMAND_CBR, 0);
1971 /* 9 mode register set */
1972 RAM_DEBUG_MESSAGE("Ram Enable 9\n");
1973 set_ram_mode(E7501_SDRAM_MODE | SDRAM_MODE_NORMAL);
1976 /* 10 DDR Receive FIFO RE-Sync */
1977 RAM_DEBUG_MESSAGE("Ram Enable 10\n");
1978 RAM_RESET_DDR_PTR();
1981 /* 11 normal operation */
1982 RAM_DEBUG_MESSAGE("Ram Enable 11\n");
1983 do_ram_command(RAM_COMMAND_NORMAL, 0);
1986 // Reconfigure the row boundaries and Top of Low Memory
1987 // to match the true size of the DIMMs
1988 configure_e7501_ram_addresses(ctrl, dimm_mask);
1990 /* Finally enable refresh */
1991 dram_controller_mode = pci_read_config32(PCI_DEV(0, 0, 0), DRC);
1992 dram_controller_mode |= (1 << 29);
1993 pci_write_config32(PCI_DEV(0, 0, 0), DRC, dram_controller_mode);
1997 dram_controller_mode = pci_read_config32(PCI_DEV(0, 0, 0), DRC); /* FCS_EN */
1998 dram_controller_mode |= (1 << 17); // NOTE: undocumented reserved bit
1999 pci_write_config32(PCI_DEV(0, 0, 0), DRC, dram_controller_mode);
2001 RAM_DEBUG_MESSAGE("Northbridge following SDRAM init:\n");
2006 * Configure SDRAM controller parameters that depend on characteristics of the
2007 * DIMMs installed in the system. These characteristics are read from the
2008 * DIMMs via the standard Serial Presence Detect (SPD) interface.
2010 * @param ctrl PCI addresses of memory controller functions, and SMBus
2011 * addresses of DIMM slots on the mainboard.
2013 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
2017 RAM_DEBUG_MESSAGE("Reading SPD data...\n");
2019 dimm_mask = spd_get_supported_dimms(ctrl);
2021 if (dimm_mask == 0) {
2022 print_debug("No usable memory for this controller\n");
2024 enable_e7501_clocks(dimm_mask);
2026 RAM_DEBUG_MESSAGE("setting based on SPD data...\n");
2028 configure_e7501_row_attributes(ctrl, dimm_mask);
2029 configure_e7501_dram_controller_mode(ctrl, dimm_mask);
2030 configure_e7501_cas_latency(ctrl, dimm_mask);
2031 RAM_RESET_DDR_PTR();
2033 configure_e7501_dram_timing(ctrl, dimm_mask);
2035 RAM_DEBUG_MESSAGE("done\n");
2038 /* NOTE: configure_e7501_ram_addresses() is NOT called here.
2039 * We want to keep the default 64 MB/row mapping until sdram_enable() is called,
2040 * even though the default mapping is almost certainly incorrect.
2041 * The default mapping makes it easy to initialize all of the DIMMs
2042 * even if the total system memory is > 4 GB.
2044 * Save the dimm_mask for when sdram_enable is called, so it can call
2045 * configure_e7501_ram_addresses() without having to regenerate the bitmask
2048 pci_write_config16(PCI_DEV(0, 0, 0), SKPD, dimm_mask);
2052 * Do basic RAM setup that does NOT depend on serial presence detect
2053 * information (i.e. independent of DIMM specifics).
2055 * @param ctrl PCI addresses of memory controller functions, and SMBus
2056 * addresses of DIMM slots on the mainboard.
2058 static void sdram_set_registers(const struct mem_controller *ctrl)
2060 RAM_DEBUG_MESSAGE("Northbridge prior to SDRAM init:\n");
2063 ram_set_rcomp_regs();
2064 ram_set_d0f0_regs();
2065 ram_handle_d060_2();