blob: f3a2520dba1fa635e57061e48068378fd5300fad [file] [log] [blame]
Martin Roth5474eb12018-05-26 19:22:33 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * converted to C 6/2004 yhlu
5 *
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; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
Stefan Reinauerb15975b2011-10-21 12:57:59 -070016/* This was originally for the e7500, modified for e7501
17 * The primary differences are that 7501 apparently can
18 * support single channel RAM (i haven't tested),
19 * CAS1.5 is no longer supported, The ECC scrubber
20 * now supports a mode to zero RAM and init ECC in one step
21 * and the undocumented registers at 0x80 require new
22 * (undocumented) values determined by guesswork and
23 * comparison w/ OEM BIOS values.
24 * Steven James 02/06/2003
25 */
26
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +030027#include <stdint.h>
28#include <device/pci_def.h>
29#include <arch/io.h>
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +030030#include <arch/cpu.h>
Alexandru Gagniucaf4bd592014-01-12 15:42:58 -060031#include <lib.h>
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +030032#include <stdlib.h>
33#include <console/console.h>
34
Kyösti Mälkki97c064f2012-04-18 20:33:35 +030035#include <cpu/x86/mtrr.h>
36#include <cpu/x86/cache.h>
37#include <cpu/x86/msr.h>
Stefan Reinauerb15975b2011-10-21 12:57:59 -070038#include <assert.h>
39#include <spd.h>
40#include <sdram_mode.h>
Kyösti Mälkkidcb688e2013-09-04 01:11:16 +030041#include <cbmem.h>
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +030042
43#include "raminit.h"
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +020044#include "e7505.h"
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +030045#include "debug.h"
Stefan Reinauerb15975b2011-10-21 12:57:59 -070046
47/*-----------------------------------------------------------------------------
48Definitions:
49-----------------------------------------------------------------------------*/
50
51// Uncomment this to enable run-time checking of DIMM parameters
52// for dual-channel operation
53// Unfortunately the code seems to chew up several K of space.
54//#define VALIDATE_DIMM_COMPATIBILITY
55
Martin Roth33232602017-06-24 14:48:50 -060056#if IS_ENABLED(CONFIG_DEBUG_RAM_SETUP)
Stefan Reinauer65b72ab2015-01-05 12:59:54 -080057#define RAM_DEBUG_MESSAGE(x) printk(BIOS_DEBUG, x)
58#define RAM_DEBUG_HEX32(x) printk(BIOS_DEBUG, "%08x", x)
59#define RAM_DEBUG_HEX8(x) printk(BIOS_DEBUG, "%02x", x)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +030060#define DUMPNORTH() dump_pci_device(MCHDEV)
Stefan Reinauerb15975b2011-10-21 12:57:59 -070061#else
62#define RAM_DEBUG_MESSAGE(x)
63#define RAM_DEBUG_HEX32(x)
64#define RAM_DEBUG_HEX8(x)
65#define DUMPNORTH()
66#endif
67
68#define E7501_SDRAM_MODE (SDRAM_BURST_INTERLEAVED | SDRAM_BURST_4)
69#define SPD_ERROR "Error reading SPD info\n"
70
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +030071#define MCHDEV PCI_DEV(0,0,0)
72#define RASDEV PCI_DEV(0,0,1)
73#define D060DEV PCI_DEV(0,6,0)
74
Stefan Reinauerb15975b2011-10-21 12:57:59 -070075// NOTE: This used to be 0x100000.
76// That doesn't work on systems where A20M# is asserted, because
77// attempts to access 0x1000NN end up accessing 0x0000NN.
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080078#define RCOMP_MMIO ((u8 *)0x200000)
Stefan Reinauerb15975b2011-10-21 12:57:59 -070079
80struct dimm_size {
81 unsigned long side1;
82 unsigned long side2;
83};
84
85static const uint32_t refresh_frequency[] = {
86 /* Relative frequency (array value) of each E7501 Refresh Mode Select
87 * (RMS) value (array index)
88 * 0 == least frequent refresh (longest interval between refreshes)
89 * [0] disabled -> 0
90 * [1] 15.6 usec -> 2
91 * [2] 7.8 usec -> 3
92 * [3] 64 usec -> 1
93 * [4] reserved -> 0
94 * [5] reserved -> 0
95 * [6] reserved -> 0
96 * [7] 64 clocks -> 4
97 */
98 0, 2, 3, 1, 0, 0, 0, 4
99};
100
101static const uint32_t refresh_rate_map[] = {
102 /* Map the JEDEC spd refresh rates (array index) to E7501 Refresh Mode
103 * Select values (array value)
104 * These are all the rates defined by JESD21-C Appendix D, Rev. 1.0
105 * The E7501 supports only 15.6 us (1), 7.8 us (2), 64 us (3), and
106 * 64 clock (481 ns) (7) refresh.
107 * [0] == 15.625 us -> 15.6 us
108 * [1] == 3.9 us -> 481 ns
109 * [2] == 7.8 us -> 7.8 us
110 * [3] == 31.3 us -> 15.6 us
111 * [4] == 62.5 us -> 15.6 us
112 * [5] == 125 us -> 64 us
113 */
114 1, 7, 2, 1, 1, 3
115};
116
117#define MAX_SPD_REFRESH_RATE ((sizeof(refresh_rate_map) / sizeof(uint32_t)) - 1)
118
Edward O'Callaghan08280cb2014-05-25 06:33:29 +1000119#ifdef VALIDATE_DIMM_COMPATIBILITY
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700120// SPD parameters that must match for dual-channel operation
121static const uint8_t dual_channel_parameters[] = {
122 SPD_MEMORY_TYPE,
123 SPD_MODULE_VOLTAGE,
124 SPD_NUM_COLUMNS,
125 SPD_NUM_ROWS,
126 SPD_NUM_DIMM_BANKS,
127 SPD_PRIMARY_SDRAM_WIDTH,
128 SPD_NUM_BANKS_PER_SDRAM
129};
Edward O'Callaghan08280cb2014-05-25 06:33:29 +1000130#endif /* VALIDATE_DIMM_COMPATIBILITY */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700131
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300132 /* Comments here are remains of e7501 or even 855PM.
133 * They might be partially (in)correct for e7505.
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700134 */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700135
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300136 /* (DRAM Read Timing Control, if similar to 855PM?)
137 * 0x80 - 0x81 documented differently for e7505
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700138 * This register has something to do with CAS latencies,
Martin Roth128c1042016-11-18 09:29:03 -0700139 * possibly this is the real chipset control.
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700140 * At 0x00 CAS latency 1.5 works.
141 * At 0x06 CAS latency 2.5 works.
142 * At 0x01 CAS latency 2.0 works.
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300143 *
144 * This is still undocumented in e7501, but with different values
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700145 * CAS 2.0 values taken from Intel BIOS settings, others are a guess
146 * and may be terribly wrong. Old values preserved as comments until I
147 * figure this out for sure.
148 * e7501 docs claim that CAS1.5 is unsupported, so it may or may not
149 * work at all.
150 * Steven James 02/06/2003
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300151 *
152 * NOTE: values now configured in configure_e7501_cas_latency() based
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700153 * on SPD info and total number of DIMMs (per Intel)
154 */
155
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300156 /* FDHC - Fixed DRAM Hole Control ???
157 * 0x58 undocumented for e7505, memory hole in southbridge configuration?
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700158 * [7:7] Hole_Enable
159 * 0 == No memory Hole
160 * 1 == Memory Hole from 15MB to 16MB
161 * [6:0] Reserved
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700162 */
163
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700164 /* Another Intel undocumented register
165 * 0x88 - 0x8B
166 * [31:31] Purpose unknown
167 * [26:26] Master DLL Reset?
168 * 0 == Normal operation?
169 * 1 == Reset?
170 * [07:07] Periodic memory recalibration?
171 * 0 == Disabled?
172 * 1 == Enabled?
173 * [04:04] Receive FIFO RE-Sync?
174 * 0 == Normal operation?
175 * 1 == Reset?
176 */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700177
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300178/* DDR RECOMP tables */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700179// Slew table for 2x drive?
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300180static const uint32_t slew_2x[] = {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700181 0x00000000, 0x76543210, 0xffffeca8, 0xffffffff,
182 0x21000000, 0xa8765432, 0xffffffec, 0xffffffff,
183};
184
185// Pull Up / Pull Down offset table, if analogous to IXP2800?
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300186static const uint32_t pull_updown_offset_table[] = {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700187 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
188 0x88888888, 0x88888888, 0x88888888, 0x88888888,
189};
190
191/*-----------------------------------------------------------------------------
192Delay functions:
193-----------------------------------------------------------------------------*/
194
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300195/* Estimate that SLOW_DOWN_IO takes about 1 us */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700196#define SLOW_DOWN_IO inb(0x80)
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300197static void local_udelay(int i)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700198{
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300199 while (i--) {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700200 SLOW_DOWN_IO;
201 }
202}
203
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300204/* delay for 200us */
205#define DO_DELAY local_udelay(200)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700206#define EXTRA_DELAY DO_DELAY
207
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200208/*-----------------------------------------------------------------------------
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300209Handle (undocumented) control bits MCHTST and PCI_DEV(0,6,0)
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200210-----------------------------------------------------------------------------*/
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300211typedef enum {
212 MCHTST_CMD_0,
213 D060_ENABLE,
214 D060_DISABLE,
215 RCOMP_BAR_ENABLE,
216 RCOMP_BAR_DISABLE,
217} mchtst_cc;
218
219typedef enum {
220 D060_CMD_0,
221 D060_CMD_1,
222} d060_cc;
223
224typedef enum {
225 RCOMP_HOLD,
226 RCOMP_RELEASE,
227 RCOMP_SMR_00,
228 RCOMP_SMR_01,
229} rcomp_smr_cc;
230
231/**
232 * MCHTST - 0xF4 - 0xF7 -- Based on similarity to 855PM
233 *
234 * [31:31] Purpose unknown
235 * [30:30] Purpose unknown
236 * [29:23] Unknown - not used?
237 * [22:22] System Memory MMR Enable
238 * 0 == Disable: mem space and BAR at 0x14 are not accessible
239 * 1 == Enable: mem space and BAR at 0x14 are accessible
240 * [21:20] Purpose unknown
241 * [19:02] Unknown - not used?
242 * [01:01] D6EN (Device #6 enable)
243 * 0 == Disable
244 * 1 == Enable
245 * [00:00] Unknown - not used?
246 */
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300247static void mchtest_control(mchtst_cc cmd)
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300248{
249 uint32_t dword = pci_read_config32(MCHDEV, MCHTST);
250 switch (cmd) {
251 case MCHTST_CMD_0:
252 dword &= ~(3 << 30);
253 break;
254 case RCOMP_BAR_ENABLE:
255 dword |= (1 << 22);
256 break;
257 case RCOMP_BAR_DISABLE:
258 dword &= ~(1 << 22);
259 break;
260 case D060_ENABLE:
261 dword |= (1 << 1);
262 break;
263 case D060_DISABLE:
264 dword &= ~(1 << 1);
265 break;
266 };
267 pci_write_config32(MCHDEV, MCHTST, dword);
268}
269
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200270
271/**
272 *
273 */
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300274static void d060_control(d060_cc cmd)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700275{
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300276 mchtest_control(D060_ENABLE);
277 uint32_t dword = pci_read_config32(D060DEV, 0xf0);
278 switch (cmd) {
279 case D060_CMD_0:
280 dword |= (1 << 2);
281 break;
282 case D060_CMD_1:
283 dword |= (3 << 27);
284 break;
285 }
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300286 pci_write_config32(D060DEV, 0xf0, dword);
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300287 mchtest_control(D060_DISABLE);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200288}
289
290/**
291 *
292 */
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300293static void rcomp_smr_control(rcomp_smr_cc cmd)
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200294{
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300295 uint32_t dword = read32(RCOMP_MMIO + SMRCTL);
296 switch (cmd) {
297 case RCOMP_HOLD:
298 dword |= (1 << 9);
299 break;
300 case RCOMP_RELEASE:
301 dword &= ~((1 << 9) | (3 << 0));
302 dword |= (1 << 10) | (1 << 0);
303 break;
304 case RCOMP_SMR_00:
305 dword &= ~(1 << 8);
306 break;
307 case RCOMP_SMR_01:
308 dword |= (1 << 10) | (1 << 8);
309 break;
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200310 }
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300311 write32(RCOMP_MMIO + SMRCTL, dword);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700312}
313
314/*-----------------------------------------------------------------------------
315Serial presence detect (SPD) functions:
316-----------------------------------------------------------------------------*/
317
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200318static void die_on_spd_error(int spd_return_value)
319{
320 if (spd_return_value < 0)
321 die("Error reading SPD info\n");
322}
323
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700324/**
325 * Calculate the page size for each physical bank of the DIMM:
326 * log2(page size) = (# columns) + log2(data width)
327 *
328 * NOTE: Page size is the total number of data bits in a row.
329 *
330 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
331 * @return log2(page size) for each side of the DIMM.
332 */
333static struct dimm_size sdram_spd_get_page_size(uint16_t dimm_socket_address)
334{
335 uint16_t module_data_width;
336 int value;
337 struct dimm_size pgsz;
338
339 pgsz.side1 = 0;
340 pgsz.side2 = 0;
341
342 // Side 1
343 value = spd_read_byte(dimm_socket_address, SPD_NUM_COLUMNS);
344 if (value < 0)
345 goto hw_err;
346 pgsz.side1 = value & 0xf; // # columns in bank 1
347
348 /* Get the module data width and convert it to a power of two */
349 value =
350 spd_read_byte(dimm_socket_address, SPD_MODULE_DATA_WIDTH_MSB);
351 if (value < 0)
352 goto hw_err;
353 module_data_width = (value & 0xff) << 8;
354
355 value =
356 spd_read_byte(dimm_socket_address, SPD_MODULE_DATA_WIDTH_LSB);
357 if (value < 0)
358 goto hw_err;
359 module_data_width |= (value & 0xff);
360
361 pgsz.side1 += log2(module_data_width);
362
363 /* side two */
364 value = spd_read_byte(dimm_socket_address, SPD_NUM_DIMM_BANKS);
365 if (value < 0)
366 goto hw_err;
367 if (value > 2)
368 die("Bad SPD value\n");
369 if (value == 2) {
370
371 pgsz.side2 = pgsz.side1; // Assume symmetric banks until we know differently
372 value =
373 spd_read_byte(dimm_socket_address, SPD_NUM_COLUMNS);
374 if (value < 0)
375 goto hw_err;
376 if ((value & 0xf0) != 0) {
377 // Asymmetric banks
378 pgsz.side2 -= value & 0xf; /* Subtract out columns on side 1 */
379 pgsz.side2 += (value >> 4) & 0xf; /* Add in columns on side 2 */
380 }
381 }
382
383 return pgsz;
384
385 hw_err:
386 die(SPD_ERROR);
387 return pgsz; // Never reached
388}
389
390/**
391 * Read the width in bits of each DIMM side's DRAMs via SPD (i.e. 4, 8, 16).
392 *
393 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
394 * @return Width in bits of each DIMM side's DRAMs.
395 */
396static struct dimm_size sdram_spd_get_width(uint16_t dimm_socket_address)
397{
398 int value;
399 struct dimm_size width;
400
401 width.side1 = 0;
402 width.side2 = 0;
403
404 value =
405 spd_read_byte(dimm_socket_address, SPD_PRIMARY_SDRAM_WIDTH);
406 die_on_spd_error(value);
407
408 width.side1 = value & 0x7f; // Mask off bank 2 flag
409
410 if (value & 0x80) {
411 width.side2 = width.side1 << 1; // Bank 2 exists and is double-width
412 } else {
413 // If bank 2 exists, it's the same width as bank 1
414 value =
415 spd_read_byte(dimm_socket_address, SPD_NUM_DIMM_BANKS);
416 die_on_spd_error(value);
417
418#ifdef ROMCC_IF_BUG_FIXED
419 if (value == 2)
420 width.side2 = width.side1;
421#else
422 switch (value) {
423 case 2:
424 width.side2 = width.side1;
425 break;
426
427 default:
428 break;
429 }
430#endif
431 }
432
433 return width;
434}
435
436/**
437 * Calculate the log base 2 size in bits of both DIMM sides.
438 *
439 * log2(# bits) = (# columns) + log2(data width) +
440 * (# rows) + log2(banks per SDRAM)
441 *
442 * Note that it might be easier to use SPD byte 31 here, it has the DIMM size
443 * as a multiple of 4MB. The way we do it now we can size both sides of an
444 * asymmetric DIMM.
445 *
446 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
447 * @return log2(number of bits) for each side of the DIMM.
448 */
449static struct dimm_size spd_get_dimm_size(unsigned dimm_socket_address)
450{
451 int value;
452
453 // Start with log2(page size)
454 struct dimm_size sz = sdram_spd_get_page_size(dimm_socket_address);
455
456 if (sz.side1 > 0) {
457
458 value = spd_read_byte(dimm_socket_address, SPD_NUM_ROWS);
459 die_on_spd_error(value);
460
461 sz.side1 += value & 0xf;
462
463 if (sz.side2 > 0) {
464
465 // Double-sided DIMM
466 if (value & 0xF0)
467 sz.side2 += value >> 4; // Asymmetric
468 else
469 sz.side2 += value; // Symmetric
470 }
471
472 value =
473 spd_read_byte(dimm_socket_address,
474 SPD_NUM_BANKS_PER_SDRAM);
475 die_on_spd_error(value);
476
477 value = log2(value);
478 sz.side1 += value;
479 if (sz.side2 > 0)
480 sz.side2 += value;
481 }
482
483 return sz;
484}
485
486#ifdef VALIDATE_DIMM_COMPATIBILITY
487
488/**
489 * Determine whether two DIMMs have the same value for an SPD parameter.
490 *
491 * @param spd_byte_number The SPD byte number to compare in both DIMMs.
492 * @param dimm0_address SMBus address of the 1st DIMM socket to interrogate.
493 * @param dimm1_address SMBus address of the 2nd DIMM socket to interrogate.
494 * @return 1 if both DIMM sockets report the same value for the specified
495 * SPD parameter, 0 if the values differed or an error occurred.
496 */
497static uint8_t are_spd_values_equal(uint8_t spd_byte_number,
498 uint16_t dimm0_address,
499 uint16_t dimm1_address)
500{
501 uint8_t bEqual = 0;
502 int dimm0_value = spd_read_byte(dimm0_address, spd_byte_number);
503 int dimm1_value = spd_read_byte(dimm1_address, spd_byte_number);
504
505 if ((dimm0_value >= 0) && (dimm1_value >= 0)
506 && (dimm0_value == dimm1_value))
507 bEqual = 1;
508
509 return bEqual;
510}
511#endif
512
513/**
514 * Scan for compatible DIMMs.
515 *
516 * The code in this module only supports dual-channel operation, so we test
517 * that compatible DIMMs are paired.
518 *
519 * @param ctrl PCI addresses of memory controller functions, and SMBus
520 * addresses of DIMM slots on the mainboard.
521 * @return A bitmask indicating which of the possible sockets for each channel
522 * was found to contain a compatible DIMM.
523 * Bit 0 corresponds to the closest socket for channel 0
524 * Bit 1 to the next socket for channel 0
525 * ...
526 * Bit MAX_DIMM_SOCKETS_PER_CHANNEL-1 to the last socket for channel 0
527 * Bit MAX_DIMM_SOCKETS_PER_CHANNEL is the closest socket for channel 1
528 * ...
529 * Bit 2*MAX_DIMM_SOCKETS_PER_CHANNEL-1 is the last socket for channel 1
530 */
531static uint8_t spd_get_supported_dimms(const struct mem_controller *ctrl)
532{
533 int i;
534 uint8_t dimm_mask = 0;
535
536 // Have to increase size of dimm_mask if this assertion is violated
537 ASSERT(MAX_DIMM_SOCKETS_PER_CHANNEL <= 4);
538
539 // Find DIMMs we can support on channel 0.
540 // Then see if the corresponding channel 1 DIMM has the same parameters,
541 // since we only support dual-channel.
542
543 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
544
545 uint16_t channel0_dimm = ctrl->channel0[i];
546 uint16_t channel1_dimm = ctrl->channel1[i];
547 uint8_t bDualChannel = 1;
548#ifdef VALIDATE_DIMM_COMPATIBILITY
549 struct dimm_size page_size;
550 struct dimm_size sdram_width;
551#endif
552 int spd_value;
553
554 if (channel0_dimm == 0)
555 continue; // No such socket on this mainboard
556
557 if (spd_read_byte(channel0_dimm, SPD_MEMORY_TYPE) !=
558 SPD_MEMORY_TYPE_SDRAM_DDR)
559 continue;
560
561#ifdef VALIDATE_DIMM_COMPATIBILITY
562 if (spd_read_byte(channel0_dimm, SPD_MODULE_VOLTAGE) !=
563 SPD_VOLTAGE_SSTL2)
564 continue; // Unsupported voltage
565
566 // E7501 does not support unregistered DIMMs
567 spd_value =
568 spd_read_byte(channel0_dimm, SPD_MODULE_ATTRIBUTES);
569 if (!(spd_value & MODULE_REGISTERED) || (spd_value < 0))
570 continue;
571
572 // Must support burst = 4 for dual-channel operation on E7501
573 // NOTE: for single-channel, burst = 8 is required
574 spd_value =
575 spd_read_byte(channel0_dimm,
576 SPD_SUPPORTED_BURST_LENGTHS);
577 if (!(spd_value & SPD_BURST_LENGTH_4) || (spd_value < 0))
578 continue;
579
580 page_size = sdram_spd_get_page_size(channel0_dimm);
581 sdram_width = sdram_spd_get_width(channel0_dimm);
582
583 // Validate DIMM page size
584 // The E7501 only supports page sizes of 4, 8, 16, or 32 KB per channel
585 // NOTE: 4 KB = 32 Kb = 2^15
586 // 32 KB = 262 Kb = 2^18
587
588 if ((page_size.side1 < 15) || (page_size.side1 > 18))
589 continue;
590
591 // If DIMM is double-sided, verify side2 page size
592 if (page_size.side2 != 0) {
593 if ((page_size.side2 < 15)
594 || (page_size.side2 > 18))
595 continue;
596 }
597 // Validate SDRAM width
598 // The E7501 only supports x4 and x8 devices
599
600 if ((sdram_width.side1 != 4) && (sdram_width.side1 != 8))
601 continue;
602
603 // If DIMM is double-sided, verify side2 width
604 if (sdram_width.side2 != 0) {
605 if ((sdram_width.side2 != 4)
606 && (sdram_width.side2 != 8))
607 continue;
608 }
609#endif
610 // Channel 0 DIMM looks compatible.
611 // Now see if it is paired with the proper DIMM on channel 1.
612
613 ASSERT(channel1_dimm != 0); // No such socket on this mainboard??
614
615 // NOTE: unpopulated DIMMs cause read to fail
616 spd_value =
617 spd_read_byte(channel1_dimm, SPD_MODULE_ATTRIBUTES);
618 if (!(spd_value & MODULE_REGISTERED) || (spd_value < 0)) {
619
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800620 printk(BIOS_DEBUG, "Skipping un-matched DIMMs - only dual-channel operation supported\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700621 continue;
622 }
623#ifdef VALIDATE_DIMM_COMPATIBILITY
624 spd_value =
625 spd_read_byte(channel1_dimm,
626 SPD_SUPPORTED_BURST_LENGTHS);
627 if (!(spd_value & SPD_BURST_LENGTH_4) || (spd_value < 0))
628 continue;
629
630 int j;
631 for (j = 0; j < sizeof(dual_channel_parameters); ++j) {
632 if (!are_spd_values_equal
633 (dual_channel_parameters[j], channel0_dimm,
634 channel1_dimm)) {
635
636 bDualChannel = 0;
637 break;
638 }
639 }
Edward O'Callaghan08280cb2014-05-25 06:33:29 +1000640#endif /* VALIDATE_DIMM_COMPATIBILITY */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700641
642 // Code around ROMCC bug in optimization of "if" statements
643#ifdef ROMCC_IF_BUG_FIXED
644 if (bDualChannel) {
645 // Made it through all the checks, this DIMM pair is usable
646 dimm_mask |= ((1 << i) | (1 << (MAX_DIMM_SOCKETS_PER_CHANNEL + i)));
647 } else
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800648 printk(BIOS_DEBUG, "Skipping un-matched DIMMs - only dual-channel operation supported\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700649#else
650 switch (bDualChannel) {
651 case 0:
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800652 printk(BIOS_DEBUG, "Skipping un-matched DIMMs - only dual-channel operation supported\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700653 break;
654
655 default:
656 // Made it through all the checks, this DIMM pair is usable
657 dimm_mask |= (1 << i) | (1 << (MAX_DIMM_SOCKETS_PER_CHANNEL + i));
658 break;
659 }
660#endif
661 }
662
663 return dimm_mask;
664}
665
666/*-----------------------------------------------------------------------------
667SDRAM configuration functions:
668-----------------------------------------------------------------------------*/
669
670/**
671 * Send the specified command to all DIMMs.
672 *
673 * @param command Specifies the command to be sent to the DIMMs.
674 * @param jedec_mode_bits For the MRS & EMRS commands, bits 0-12 contain the
675 * register value in JEDEC format.
676 */
677static void do_ram_command(uint8_t command, uint16_t jedec_mode_bits)
678{
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200679 uint8_t dimm_start_64M_multiple;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800680 uintptr_t dimm_start_address;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700681 uint32_t dram_controller_mode;
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200682 uint8_t i;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700683
684 // Configure the RAM command
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300685 dram_controller_mode = pci_read_config32(MCHDEV, DRC);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700686 dram_controller_mode &= 0xFFFFFF8F;
687 dram_controller_mode |= command;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300688 pci_write_config32(MCHDEV, DRC, dram_controller_mode);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700689
690 // RAM_COMMAND_NORMAL is an exception.
691 // It affects only the memory controller and does not need to be "sent" to the DIMMs.
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300692 if (command == RAM_COMMAND_NORMAL) {
693 EXTRA_DELAY;
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200694 return;
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300695 }
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700696
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200697 // NOTE: for mode select commands, some of the location address bits are part of the command
698 // Map JEDEC mode bits to E7505
699 if (command == RAM_COMMAND_MRS) {
700 // Host address lines [25:18] map to DIMM address lines [7:0]
701 // Host address lines [17:16] map to DIMM address lines [9:8]
702 // Host address lines [15:4] map to DIMM address lines [11:0]
703 dimm_start_address = (jedec_mode_bits & 0x00ff) << 18;
704 dimm_start_address |= (jedec_mode_bits & 0x0300) << 8;
705 dimm_start_address |= (jedec_mode_bits & 0x0fff) << 4;
706 } else if (command == RAM_COMMAND_EMRS) {
707 // Host address lines [15:4] map to DIMM address lines [11:0]
708 dimm_start_address = (jedec_mode_bits << 4);
709 } else {
710 ASSERT(jedec_mode_bits == 0);
711 dimm_start_address = 0;
712 }
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700713
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200714 // Send the command to all DIMMs by accessing a memory location within each
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700715
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200716 dimm_start_64M_multiple = 0;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700717
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300718 /* FIXME: Only address the number of rows present in the system?
719 * Seems like rows 4-7 overlap with 0-3.
720 */
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200721 for (i = 0; i < (MAX_NUM_CHANNELS * MAX_DIMM_SOCKETS_PER_CHANNEL); ++i) {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700722
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300723 uint8_t dimm_end_64M_multiple = pci_read_config8(MCHDEV, DRB_ROW_0 + i);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700724
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200725 if (dimm_end_64M_multiple > dimm_start_64M_multiple) {
726 dimm_start_address &= 0x3ffffff;
727 dimm_start_address |= dimm_start_64M_multiple << 26;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800728 read32((void *)dimm_start_address);
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300729 // Set the start of the next DIMM
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200730 dimm_start_64M_multiple = dimm_end_64M_multiple;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700731 }
732 }
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300733 EXTRA_DELAY;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700734}
735
736/**
737 * Set the mode register of all DIMMs.
738 *
739 * The proper CAS# latency setting is added to the mode bits specified
740 * by the caller.
741 *
742 * @param jedec_mode_bits For the MRS & EMRS commands, bits 0-12 contain the
743 * register value in JEDEC format.
744 */
745static void set_ram_mode(uint16_t jedec_mode_bits)
746{
747 ASSERT(!(jedec_mode_bits & SDRAM_CAS_MASK));
748
749 uint32_t dram_cas_latency =
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300750 pci_read_config32(MCHDEV, DRT) & DRT_CAS_MASK;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700751
752 switch (dram_cas_latency) {
753 case DRT_CAS_2_5:
754 jedec_mode_bits |= SDRAM_CAS_2_5;
755 break;
756
757 case DRT_CAS_2_0:
758 jedec_mode_bits |= SDRAM_CAS_2_0;
759 break;
760
761 default:
762 BUG();
763 break;
764 }
765
766 do_ram_command(RAM_COMMAND_MRS, jedec_mode_bits);
767}
768
769/*-----------------------------------------------------------------------------
Martin Roth128c1042016-11-18 09:29:03 -0700770DIMM-independent configuration functions:
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700771-----------------------------------------------------------------------------*/
772
773/**
774 * Configure the E7501's DRAM Row Boundary (DRB) registers for the memory
775 * present in the specified DIMM.
776 *
777 * @param dimm_log2_num_bits Specifies log2(number of bits) for each side of
778 * the DIMM.
779 * @param total_dram_64M_multiple Total DRAM in the system (as a multiple of
780 * 64 MB) for DIMMs < dimm_index.
781 * @param dimm_index Which DIMM pair is being processed
782 * (0..MAX_DIMM_SOCKETS_PER_CHANNEL).
783 * @return New multiple of 64 MB total DRAM in the system.
784 */
785static uint8_t configure_dimm_row_boundaries(struct dimm_size dimm_log2_num_bits, uint8_t total_dram_64M_multiple, unsigned dimm_index)
786{
787 int i;
788
789 ASSERT(dimm_index < MAX_DIMM_SOCKETS_PER_CHANNEL);
790
791 // DIMM sides must be at least 32 MB
792 ASSERT(dimm_log2_num_bits.side1 >= 28);
793 ASSERT((dimm_log2_num_bits.side2 == 0)
794 || (dimm_log2_num_bits.side2 >= 28));
795
796 // In dual-channel mode, we are called only once for each pair of DIMMs.
797 // Each time we process twice the capacity of a single DIMM.
798
799 // Convert single DIMM capacity to paired DIMM capacity
800 // (multiply by two ==> add 1 to log2)
801 dimm_log2_num_bits.side1++;
802 if (dimm_log2_num_bits.side2 > 0)
803 dimm_log2_num_bits.side2++;
804
805 // Add the capacity of side 1 this DIMM pair (as a multiple of 64 MB)
806 // to the total capacity of the system
807 // NOTE: 64 MB == 512 Mb, and log2(512 Mb) == 29
808
809 total_dram_64M_multiple += (1 << (dimm_log2_num_bits.side1 - 29));
810
811 // Configure the boundary address for the row on side 1
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300812 pci_write_config8(MCHDEV, DRB_ROW_0 + (dimm_index << 1),
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700813 total_dram_64M_multiple);
814
815 // If the DIMMs are double-sided, add the capacity of side 2 this DIMM pair
816 // (as a multiple of 64 MB) to the total capacity of the system
817 if (dimm_log2_num_bits.side2 >= 29)
818 total_dram_64M_multiple +=
819 (1 << (dimm_log2_num_bits.side2 - 29));
820
821 // Configure the boundary address for the row (if any) on side 2
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300822 pci_write_config8(MCHDEV, DRB_ROW_1 + (dimm_index << 1),
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700823 total_dram_64M_multiple);
824
825 // Update boundaries for rows subsequent to these.
826 // These settings will be overridden by a subsequent call if a populated physical slot exists
827
828 for (i = dimm_index + 1; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300829 pci_write_config8(MCHDEV, DRB_ROW_0 + (i << 1),
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700830 total_dram_64M_multiple);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300831 pci_write_config8(MCHDEV, DRB_ROW_1 + (i << 1),
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700832 total_dram_64M_multiple);
833 }
834
835 return total_dram_64M_multiple;
836}
837
838/**
839 * Set the E7501's DRAM row boundary addresses & its Top Of Low Memory (TOLM).
840 *
841 * If necessary, set up a remap window so we don't waste DRAM that ordinarily
842 * would lie behind addresses reserved for memory-mapped I/O.
843 *
844 * @param ctrl PCI addresses of memory controller functions, and SMBus
845 * addresses of DIMM slots on the mainboard.
846 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
847 */
848static void configure_e7501_ram_addresses(const struct mem_controller
849 *ctrl, uint8_t dimm_mask)
850{
851 int i;
852 uint8_t total_dram_64M_multiple = 0;
853
854 // Configure the E7501's DRAM row boundaries
855 // Start by zeroing out the temporary initial configuration
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300856 pci_write_config32(MCHDEV, DRB_ROW_0, 0);
857 pci_write_config32(MCHDEV, DRB_ROW_4, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700858
859 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
860
861 uint16_t dimm_socket_address = ctrl->channel0[i];
862 struct dimm_size sz;
863
864 if (!(dimm_mask & (1 << i)))
865 continue; // This DIMM not present
866
867 sz = spd_get_dimm_size(dimm_socket_address);
868
869 RAM_DEBUG_MESSAGE("dimm size =");
870 RAM_DEBUG_HEX32((u32)sz.side1);
871 RAM_DEBUG_MESSAGE(" ");
872 RAM_DEBUG_HEX32((u32)sz.side2);
873 RAM_DEBUG_MESSAGE("\n");
874
875 if (sz.side1 == 0)
876 die("Bad SPD value\n");
877
878 total_dram_64M_multiple =
879 configure_dimm_row_boundaries(sz, total_dram_64M_multiple, i);
880 }
881
882 // Configure the Top Of Low Memory (TOLM) in the E7501
883 // This address must be a multiple of 128 MB that is less than 4 GB.
884 // NOTE: 16-bit wide TOLM register stores only the highest 5 bits of a 32-bit address
885 // in the highest 5 bits.
886
887 // We set TOLM to the smaller of 0xC0000000 (3 GB) or the total DRAM in the system.
888 // This reserves addresses from 0xC0000000 - 0xFFFFFFFF for non-DRAM purposes
889 // such as flash and memory-mapped I/O.
890
891 // If there is more than 3 GB of DRAM, we define a remap window which
892 // makes the DRAM "behind" the reserved region available above the top of physical
893 // memory.
894
895 // NOTE: 0xC0000000 / (64 MB) == 0x30
896
897 if (total_dram_64M_multiple <= 0x30) {
898
899 // <= 3 GB total RAM
900
901 /* I should really adjust all of this in C after I have resources
902 * to all of the pci devices.
903 */
904
905 // Round up to 128MB granularity
906 // SJM: Is "missing" 64 MB of memory a potential issue? Should this round down?
907
908 uint8_t total_dram_128M_multiple =
909 (total_dram_64M_multiple + 1) >> 1;
910
911 // Convert to high 16 bits of address
912 uint16_t top_of_low_memory =
913 total_dram_128M_multiple << 11;
914
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300915 pci_write_config16(MCHDEV, TOLM,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700916 top_of_low_memory);
917
918 } else {
919
920 // > 3 GB total RAM
921
922 // Set defaults for > 4 GB DRAM, i.e. remap a 1 GB (= 0x10 * 64 MB) range of memory
923 uint16_t remap_base = total_dram_64M_multiple; // A[25:0] == 0
924 uint16_t remap_limit = total_dram_64M_multiple + 0x10 - 1; // A[25:0] == 0xF
925
926 // Put TOLM at 3 GB
927
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300928 pci_write_config16(MCHDEV, TOLM, 0xc000);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700929
930 // Define a remap window to make the RAM that would appear from 3 GB - 4 GB
931 // visible just beyond 4 GB or the end of physical memory, whichever is larger
932 // NOTE: 16-bit wide REMAP registers store only the highest 10 bits of a 36-bit address,
933 // (i.e. a multiple of 64 MB) in the lowest 10 bits.
934 // NOTE: 0x100000000 / (64 MB) == 0x40
935
936 if (total_dram_64M_multiple < 0x40) {
937 remap_base = 0x40; // 0x100000000
938 remap_limit =
939 0x40 + (total_dram_64M_multiple - 0x30) - 1;
940 }
941
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300942 pci_write_config16(MCHDEV, REMAPBASE,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700943 remap_base);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300944 pci_write_config16(MCHDEV, REMAPLIMIT,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700945 remap_limit);
946 }
947}
948
949/**
Kyösti Mälkki97c064f2012-04-18 20:33:35 +0300950 * Execute ECC full-speed scrub once and leave scrubber disabled.
951 *
952 * NOTE: All cache and stack is lost during ECC scrub loop.
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700953 */
Stefan Reinauer1e0ddf62012-11-02 17:26:33 +0100954static inline void __attribute__((always_inline))
Kyösti Mälkki97c064f2012-04-18 20:33:35 +0300955 initialize_ecc(unsigned long ret_addr, unsigned long ret_addr2)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700956{
Kyösti Mälkki97c064f2012-04-18 20:33:35 +0300957 uint16_t scrubbed = pci_read_config16(MCHDEV, MCHCFGNS) & 0x08;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700958
Kyösti Mälkki97c064f2012-04-18 20:33:35 +0300959 if (!scrubbed) {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700960 RAM_DEBUG_MESSAGE("Initializing ECC state...\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700961
Kyösti Mälkki97c064f2012-04-18 20:33:35 +0300962 /* ECC scrub flushes cache-lines and stack, need to
963 * store return address from romstage.c:main().
964 */
965 asm volatile(
966 "movd %0, %%xmm0;"
967 "movd (%0), %%xmm1;"
968 "movd %1, %%xmm2;"
969 "movd (%1), %%xmm3;"
970 :: "r" (ret_addr), "r" (ret_addr2) :
971 );
972
973 /* NOTE: All cache is lost during this loop.
974 * Make sure PCI access does not use stack.
975 */
976
977 pci_write_config16(MCHDEV, MCHCFGNS, 0x01);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700978 do {
Kyösti Mälkki97c064f2012-04-18 20:33:35 +0300979 scrubbed = pci_read_config16(MCHDEV, MCHCFGNS);
980 } while (! (scrubbed & 0x08));
981 pci_write_config16(MCHDEV, MCHCFGNS, (scrubbed & ~0x07) | 0x04);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700982
Kyösti Mälkki97c064f2012-04-18 20:33:35 +0300983 /* Some problem remains with XIP cache from ROM, so for
984 * now, I disable XIP and also invalidate cache (again)
985 * before the remaining small portion of romstage.
986 *
987 * Adding NOPs here has unexpected results, making
988 * the first do_printk()/vtxprintf() after ECC scrub
989 * fail midway. Sometimes vtxprintf() dumps strings
990 * completely but with every 4th (fourth) character as "/".
991 *
992 * An inlined dump to console of the same string,
993 * before vtxprintf() call, is successful. So the
994 * source string should be completely in cache already.
995 *
996 * I need to review this again with CPU microcode
997 * update applied pre-CAR.
998 */
999
1000 /* Disable and invalidate all cache. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -07001001 msr_t xip_mtrr = rdmsr(MTRR_PHYS_MASK(1));
1002 xip_mtrr.lo &= ~MTRR_PHYS_MASK_VALID;
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001003 invd();
Alexandru Gagniuc86091f92015-09-30 20:23:09 -07001004 wrmsr(MTRR_PHYS_MASK(1), xip_mtrr);
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001005 invd();
1006
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001007 RAM_DEBUG_MESSAGE("ECC state initialized.\n");
1008
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001009 /* Recover IP for return from main. */
1010 asm volatile(
1011 "movd %%xmm0, %%edi;"
1012 "movd %%xmm1, (%%edi);"
1013 "movd %%xmm2, %%edi;"
1014 "movd %%xmm3, (%%edi);"
1015 ::: "edi"
1016 );
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001017
Martin Roth33232602017-06-24 14:48:50 -06001018#if IS_ENABLED(CONFIG_DEBUG_RAM_SETUP)
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001019 unsigned int a1, a2;
1020 asm volatile("movd %%xmm2, %%eax;" : "=a" (a1) ::);
1021 asm volatile("movd %%xmm3, %%eax;" : "=a" (a2) ::);
Martin Roth0cd338e2016-07-29 14:07:30 -06001022 printk(BIOS_DEBUG, "return EIP @ %x = %x\n", a1, a2);
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001023 asm volatile("movd %%xmm0, %%eax;" : "=a" (a1) ::);
1024 asm volatile("movd %%xmm1, %%eax;" : "=a" (a2) ::);
Martin Roth0cd338e2016-07-29 14:07:30 -06001025 printk(BIOS_DEBUG, "return EIP @ %x = %x\n", a1, a2);
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001026#endif
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001027 }
1028
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001029 /* Clear the ECC error bits. */
1030 pci_write_config8(RASDEV, DRAM_FERR, 0x03);
1031 pci_write_config8(RASDEV, DRAM_NERR, 0x03);
1032
1033 /* Clear DRAM Interface error bits. */
1034 pci_write_config32(RASDEV, FERR_GLOBAL, 1 << 18);
1035 pci_write_config32(RASDEV, NERR_GLOBAL, 1 << 18);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001036}
1037
1038/**
1039 * Program the DRAM Timing register (DRT) of the E7501 (except for CAS#
1040 * latency, which is assumed to have been programmed already), based on the
1041 * parameters of the various installed DIMMs.
1042 *
1043 * @param ctrl PCI addresses of memory controller functions, and SMBus
1044 * addresses of DIMM slots on the mainboard.
1045 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
1046 */
1047static void configure_e7501_dram_timing(const struct mem_controller *ctrl,
1048 uint8_t dimm_mask)
1049{
1050 int i;
1051 uint32_t dram_timing;
1052 int value;
1053 uint8_t slowest_row_precharge = 0;
1054 uint8_t slowest_ras_cas_delay = 0;
1055 uint8_t slowest_active_to_precharge_delay = 0;
1056 uint32_t current_cas_latency =
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001057 pci_read_config32(MCHDEV, DRT) & DRT_CAS_MASK;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001058
1059 // CAS# latency must be programmed beforehand
1060 ASSERT((current_cas_latency == DRT_CAS_2_0)
1061 || (current_cas_latency == DRT_CAS_2_5));
1062
1063 // Each timing parameter is determined by the slowest DIMM
1064
1065 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
1066 uint16_t dimm_socket_address;
1067
1068 if (!(dimm_mask & (1 << i)))
1069 continue; // This DIMM not present
1070
1071 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
1072 dimm_socket_address = ctrl->channel0[i];
1073 else
1074 dimm_socket_address =
1075 ctrl->channel1[i - MAX_DIMM_SOCKETS_PER_CHANNEL];
1076
1077 value =
1078 spd_read_byte(dimm_socket_address,
1079 SPD_MIN_ROW_PRECHARGE_TIME);
1080 if (value < 0)
1081 goto hw_err;
1082 if (value > slowest_row_precharge)
1083 slowest_row_precharge = value;
1084
1085 value =
1086 spd_read_byte(dimm_socket_address,
1087 SPD_MIN_RAS_TO_CAS_DELAY);
1088 if (value < 0)
1089 goto hw_err;
1090 if (value > slowest_ras_cas_delay)
1091 slowest_ras_cas_delay = value;
1092
1093 value =
1094 spd_read_byte(dimm_socket_address,
1095 SPD_MIN_ACTIVE_TO_PRECHARGE_DELAY);
1096 if (value < 0)
1097 goto hw_err;
1098 if (value > slowest_active_to_precharge_delay)
1099 slowest_active_to_precharge_delay = value;
1100 }
1101
1102 // NOTE for timing parameters:
1103 // At 133 MHz, 1 clock == 7.52 ns
1104
1105 /* Read the initial state */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001106 dram_timing = pci_read_config32(MCHDEV, DRT);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001107
1108 /* Trp */
1109
1110 // E7501 supports only 2 or 3 clocks for tRP
1111 if (slowest_row_precharge > ((22 << 2) | (2 << 0)))
1112 die("unsupported DIMM tRP"); // > 22.5 ns: 4 or more clocks
1113 else if (slowest_row_precharge > (15 << 2))
1114 dram_timing &= ~(1 << 0); // > 15.0 ns: 3 clocks
1115 else
1116 dram_timing |= (1 << 0); // <= 15.0 ns: 2 clocks
1117
1118 /* Trcd */
1119
1120 // E7501 supports only 2 or 3 clocks for tRCD
1121 // Use the same value for both read & write
1122 dram_timing &= ~((1 << 3) | (3 << 1));
1123 if (slowest_ras_cas_delay > ((22 << 2) | (2 << 0)))
1124 die("unsupported DIMM tRCD"); // > 22.5 ns: 4 or more clocks
1125 else if (slowest_ras_cas_delay > (15 << 2))
1126 dram_timing |= (2 << 1); // > 15.0 ns: 3 clocks
1127 else
1128 dram_timing |= ((1 << 3) | (3 << 1)); // <= 15.0 ns: 2 clocks
1129
1130 /* Tras */
1131
1132 // E7501 supports only 5, 6, or 7 clocks for tRAS
1133 // 5 clocks ~= 37.6 ns, 6 clocks ~= 45.1 ns, 7 clocks ~= 52.6 ns
1134 dram_timing &= ~(3 << 9);
1135
1136 if (slowest_active_to_precharge_delay > 52)
1137 die("unsupported DIMM tRAS"); // > 52 ns: 8 or more clocks
1138 else if (slowest_active_to_precharge_delay > 45)
1139 dram_timing |= (0 << 9); // 46-52 ns: 7 clocks
1140 else if (slowest_active_to_precharge_delay > 37)
1141 dram_timing |= (1 << 9); // 38-45 ns: 6 clocks
1142 else
1143 dram_timing |= (2 << 9); // < 38 ns: 5 clocks
1144
1145 /* Trd */
1146
Elyes HAOUAS0f92f632014-07-27 19:37:31 +02001147 /* Set to a 7 clock read delay. This is for 133MHz
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001148 * with a CAS latency of 2.5 if 2.0 a 6 clock
1149 * delay is good */
1150
1151 dram_timing &= ~(7 << 24); // 7 clocks
1152 if (current_cas_latency == DRT_CAS_2_0)
1153 dram_timing |= (1 << 24); // 6 clocks
1154
1155 /*
1156 * Back to Back Read-Write Turn Around
1157 */
1158 /* Set to a 5 clock back to back read to write turn around.
1159 * 4 is a good delay if the CAS latency is 2.0 */
1160
1161 dram_timing &= ~(1 << 28); // 5 clocks
1162 if (current_cas_latency == DRT_CAS_2_0)
1163 dram_timing |= (1 << 28); // 4 clocks
1164
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001165 pci_write_config32(MCHDEV, DRT, dram_timing);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001166
1167 return;
1168
1169 hw_err:
1170 die(SPD_ERROR);
1171}
1172
1173/**
1174 * Determine the shortest CAS# latency that the E7501 and all DIMMs have in
1175 * common, and program the E7501 to use it.
1176 *
1177 * @param ctrl PCI addresses of memory controller functions, and SMBus
1178 * addresses of DIMM slots on the mainboard.
1179 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1180 */
1181static void configure_e7501_cas_latency(const struct mem_controller *ctrl,
1182 uint8_t dimm_mask)
1183{
1184 int i;
1185 int value;
1186 uint32_t dram_timing;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001187 uint16_t dram_read_timing;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001188 uint32_t dword;
1189
1190 // CAS# latency bitmasks in SPD_ACCEPTABLE_CAS_LATENCIES format
1191 // NOTE: E7501 supports only 2.0 and 2.5
1192 uint32_t system_compatible_cas_latencies =
1193 SPD_CAS_LATENCY_2_0 | SPD_CAS_LATENCY_2_5;
1194 uint32_t current_cas_latency;
1195 uint32_t dimm_compatible_cas_latencies;
1196
1197 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
1198
1199 uint16_t dimm_socket_address;
1200
1201 if (!(dimm_mask & (1 << i)))
1202 continue; // This DIMM not usable
1203
1204 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
1205 dimm_socket_address = ctrl->channel0[i];
1206 else
1207 dimm_socket_address =
1208 ctrl->channel1[i - MAX_DIMM_SOCKETS_PER_CHANNEL];
1209
1210 value =
1211 spd_read_byte(dimm_socket_address,
1212 SPD_ACCEPTABLE_CAS_LATENCIES);
1213 if (value < 0)
1214 goto hw_err;
1215
1216 dimm_compatible_cas_latencies = value & 0x7f; // Start with all supported by DIMM
1217 current_cas_latency = 1 << log2(dimm_compatible_cas_latencies); // Max supported by DIMM
1218
1219 // Can we support the highest CAS# latency?
1220
1221 value =
1222 spd_read_byte(dimm_socket_address,
1223 SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
1224 if (value < 0)
1225 goto hw_err;
1226
1227 // NOTE: At 133 MHz, 1 clock == 7.52 ns
1228 if (value > 0x75) {
1229 // Our bus is too fast for this CAS# latency
1230 // Remove it from the bitmask of those supported by the DIMM that are compatible
1231 dimm_compatible_cas_latencies &= ~current_cas_latency;
1232 }
1233 // Can we support the next-highest CAS# latency (max - 0.5)?
1234
1235 current_cas_latency >>= 1;
1236 if (current_cas_latency != 0) {
1237 value =
1238 spd_read_byte(dimm_socket_address,
1239 SPD_SDRAM_CYCLE_TIME_2ND);
1240 if (value < 0)
1241 goto hw_err;
1242 if (value > 0x75)
1243 dimm_compatible_cas_latencies &=
1244 ~current_cas_latency;
1245 }
1246 // Can we support the next-highest CAS# latency (max - 1.0)?
1247 current_cas_latency >>= 1;
1248 if (current_cas_latency != 0) {
1249 value =
1250 spd_read_byte(dimm_socket_address,
1251 SPD_SDRAM_CYCLE_TIME_3RD);
1252 if (value < 0)
1253 goto hw_err;
1254 if (value > 0x75)
1255 dimm_compatible_cas_latencies &=
1256 ~current_cas_latency;
1257 }
1258 // Restrict the system to CAS# latencies compatible with this DIMM
1259 system_compatible_cas_latencies &=
1260 dimm_compatible_cas_latencies;
1261
1262 /* go to the next DIMM */
1263 }
1264
1265 /* After all of the arduous calculation setup with the fastest
1266 * cas latency I can use.
1267 */
1268
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001269 dram_timing = pci_read_config32(MCHDEV, DRT);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001270 dram_timing &= ~(DRT_CAS_MASK);
1271
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001272 dram_read_timing =
1273 pci_read_config16(MCHDEV, DRDCTL);
1274 dram_read_timing &= 0xF000;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001275
1276 if (system_compatible_cas_latencies & SPD_CAS_LATENCY_2_0) {
1277 dram_timing |= DRT_CAS_2_0;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001278 dram_read_timing |= 0x0222;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001279 } else if (system_compatible_cas_latencies & SPD_CAS_LATENCY_2_5) {
1280
1281 uint32_t dram_row_attributes =
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001282 pci_read_config32(MCHDEV, DRA);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001283
1284 dram_timing |= DRT_CAS_2_5;
1285
1286 // At CAS# 2.5, DRAM Read Timing (if that's what it its) appears to need a slightly
1287 // different value if all DIMM slots are populated
1288
1289 if ((dram_row_attributes & 0xff)
1290 && (dram_row_attributes & 0xff00)
1291 && (dram_row_attributes & 0xff0000)
1292 && (dram_row_attributes & 0xff000000)) {
1293
1294 // All slots populated
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001295 dram_read_timing |= 0x0882;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001296 } else {
1297 // Some unpopulated slots
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001298 dram_read_timing |= 0x0662;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001299 }
1300 } else
1301 die("No CAS# latencies compatible with all DIMMs!!\n");
1302
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001303 pci_write_config32(MCHDEV, DRT, dram_timing);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001304
1305 /* set master DLL reset */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001306 dword = pci_read_config32(MCHDEV, 0x88);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001307 dword |= (1 << 26);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001308 pci_write_config32(MCHDEV, 0x88, dword);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001309 /* patch try register 88 is undocumented tnz */
1310 dword &= 0x0ca17fff;
1311 dword |= 0xd14a5000;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001312 pci_write_config32(MCHDEV, 0x88, dword);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001313
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001314 pci_write_config16(MCHDEV, DRDCTL,
1315 dram_read_timing);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001316
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001317 /* clear master DLL reset */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001318 dword = pci_read_config32(MCHDEV, 0x88);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001319 dword &= ~(1 << 26);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001320 pci_write_config32(MCHDEV, 0x88, dword);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001321
1322 return;
1323
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001324hw_err:
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001325 die(SPD_ERROR);
1326}
1327
1328/**
1329 * Configure the refresh interval so that we refresh no more often than
1330 * required by the "most needy" DIMM. Also disable ECC if any of the DIMMs
1331 * don't support it.
1332 *
1333 * @param ctrl PCI addresses of memory controller functions, and SMBus
1334 * addresses of DIMM slots on the mainboard.
1335 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1336 */
1337static void configure_e7501_dram_controller_mode(const struct
1338 mem_controller *ctrl,
1339 uint8_t dimm_mask)
1340{
1341 int i;
1342
1343 // Initial settings
1344 uint32_t controller_mode =
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001345 pci_read_config32(MCHDEV, DRC);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001346 uint32_t system_refresh_mode = (controller_mode >> 8) & 7;
1347
1348 // Code below assumes that most aggressive settings are in
1349 // force when we are called, either via E7501 reset defaults
1350 // or by sdram_set_registers():
1351 // - ECC enabled
1352 // - No refresh
1353
1354 ASSERT((controller_mode & (3 << 20)) == (2 << 20)); // ECC
1355 ASSERT(!(controller_mode & (7 << 8))); // Refresh
1356
1357 /* Walk through _all_ dimms and find the least-common denominator for:
1358 * - ECC support
1359 * - refresh rates
1360 */
1361
1362 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
1363
1364 uint32_t dimm_refresh_mode;
1365 int value;
1366 uint16_t dimm_socket_address;
1367
1368 if (!(dimm_mask & (1 << i))) {
1369 continue; // This DIMM not usable
1370 }
1371
1372 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
1373 dimm_socket_address = ctrl->channel0[i];
1374 else
1375 dimm_socket_address =
1376 ctrl->channel1[i -
1377 MAX_DIMM_SOCKETS_PER_CHANNEL];
1378
1379 // Disable ECC mode if any one of the DIMMs does not support ECC
1380 // SJM: Should we just die here? E7501 datasheet says non-ECC DIMMs aren't supported.
1381
1382 value =
1383 spd_read_byte(dimm_socket_address,
1384 SPD_DIMM_CONFIG_TYPE);
1385 die_on_spd_error(value);
1386 if (value != ERROR_SCHEME_ECC) {
1387 controller_mode &= ~(3 << 20);
1388 }
1389
1390 value = spd_read_byte(dimm_socket_address, SPD_REFRESH);
1391 die_on_spd_error(value);
1392 value &= 0x7f; // Mask off self-refresh bit
1393 if (value > MAX_SPD_REFRESH_RATE) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -08001394 printk(BIOS_ERR, "unsupported refresh rate\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001395 continue;
1396 }
1397 // Get the appropriate E7501 refresh mode for this DIMM
1398 dimm_refresh_mode = refresh_rate_map[value];
1399 if (dimm_refresh_mode > 7) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -08001400 printk(BIOS_ERR, "unsupported refresh rate\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001401 continue;
1402 }
1403 // If this DIMM requires more frequent refresh than others,
1404 // update the system setting
1405 if (refresh_frequency[dimm_refresh_mode] >
1406 refresh_frequency[system_refresh_mode])
1407 system_refresh_mode = dimm_refresh_mode;
1408
1409#ifdef SUSPICIOUS_LOOKING_CODE
1410// SJM NOTE: This code doesn't look right. SPD values are an order of magnitude smaller
1411// than the clock period of the memory controller. Also, no other northbridge
1412// looks at SPD_CMD_SIGNAL_INPUT_HOLD_TIME.
1413
1414 // Switch to 2 clocks for address/command if required by any one of the DIMMs
1415 // NOTE: At 133 MHz, 1 clock == 7.52 ns
1416 value =
1417 spd_read_byte(dimm_socket_address,
1418 SPD_CMD_SIGNAL_INPUT_HOLD_TIME);
1419 die_on_spd_error(value);
1420 if (value >= 0xa0) { /* At 133MHz this constant should be 0x75 */
Martin Roth128c1042016-11-18 09:29:03 -07001421 controller_mode &= ~(1 << 16); /* Use two clock cycles instead of one */
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001422 }
1423#endif
1424
1425 /* go to the next DIMM */
1426 }
1427
1428 controller_mode |= (system_refresh_mode << 8);
1429
1430 // Configure the E7501
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001431 pci_write_config32(MCHDEV, DRC, controller_mode);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001432}
1433
1434/**
1435 * Configure the E7501's DRAM Row Attributes (DRA) registers based on DIMM
1436 * parameters read via SPD. This tells the controller the width of the SDRAM
1437 * chips on each DIMM side (x4 or x8) and the page size of each DIMM side
1438 * (4, 8, 16, or 32 KB).
1439 *
1440 * @param ctrl PCI addresses of memory controller functions, and SMBus
1441 * addresses of DIMM slots on the mainboard.
1442 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1443 */
1444static void configure_e7501_row_attributes(const struct mem_controller
1445 *ctrl, uint8_t dimm_mask)
1446{
1447 int i;
1448 uint32_t row_attributes = 0;
1449
1450 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
1451
1452 uint16_t dimm_socket_address = ctrl->channel0[i];
1453 struct dimm_size page_size;
1454 struct dimm_size sdram_width;
1455
1456 if (!(dimm_mask & (1 << i)))
1457 continue; // This DIMM not usable
1458
1459 // Get the relevant parameters via SPD
1460 page_size = sdram_spd_get_page_size(dimm_socket_address);
1461 sdram_width = sdram_spd_get_width(dimm_socket_address);
1462
1463 // Update the DRAM Row Attributes.
1464 // Page size is encoded as log2(page size in bits) - log2(8 Kb)
1465 // NOTE: 8 Kb = 2^13
1466 row_attributes |= (page_size.side1 - 13) << (i << 3); // Side 1 of each DIMM is an EVEN row
1467
1468 if (sdram_width.side2 > 0)
1469 row_attributes |= (page_size.side2 - 13) << ((i << 3) + 4); // Side 2 is ODD
1470
1471 // Set x4 flags if appropriate
1472 if (sdram_width.side1 == 4) {
1473 row_attributes |= 0x08 << (i << 3);
1474 }
1475
1476 if (sdram_width.side2 == 4) {
1477 row_attributes |= 0x08 << ((i << 3) + 4);
1478 }
1479
1480 /* go to the next DIMM */
1481 }
1482
1483 /* Write the new row attributes register */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001484 pci_write_config32(MCHDEV, DRA, row_attributes);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001485}
1486
1487/*
1488 * Enable clock signals for populated DIMM sockets and disable them for
1489 * unpopulated sockets (to reduce EMI).
1490 *
1491 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
1492 */
1493static void enable_e7501_clocks(uint8_t dimm_mask)
1494{
1495 int i;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001496 uint8_t clock_disable = pci_read_config8(MCHDEV, CKDIS);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001497
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001498 pci_write_config8(MCHDEV, 0x8e, 0xb0);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001499
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001500 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
1501
1502 uint8_t socket_mask = 1 << i;
1503
1504 if (dimm_mask & socket_mask)
1505 clock_disable &= ~socket_mask; // DIMM present, enable clock
1506 else
1507 clock_disable |= socket_mask; // DIMM absent, disable clock
1508 }
1509
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001510 pci_write_config8(MCHDEV, CKDIS, clock_disable);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001511}
1512
Martin Roth128c1042016-11-18 09:29:03 -07001513/* DIMM-dependent configuration functions */
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001514
1515/**
1516 * DDR Receive FIFO RE-Sync (?)
1517 */
1518static void RAM_RESET_DDR_PTR(void)
1519{
1520 uint8_t byte;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001521 byte = pci_read_config8(MCHDEV, 0x88);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001522 byte |= (1 << 4);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001523 pci_write_config8(MCHDEV, 0x88, byte);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001524
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001525 byte = pci_read_config8(MCHDEV, 0x88);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001526 byte &= ~(1 << 4);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001527 pci_write_config8(MCHDEV, 0x88, byte);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001528}
1529
1530/**
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001531 * Copy 64 bytes from one location to another.
1532 *
1533 * @param src_addr TODO
1534 * @param dst_addr TODO
1535 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001536static void write_8dwords(const uint32_t *src_addr, u8 *dst_addr)
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001537{
1538 int i;
1539 for (i = 0; i < 8; i++) {
1540 write32(dst_addr, *src_addr);
1541 src_addr++;
1542 dst_addr += sizeof(uint32_t);
1543 }
1544}
1545
1546/**
1547 * Set the E7501's (undocumented) RCOMP registers.
1548 *
1549 * Per the 855PM datasheet and IXP2800 HW Initialization Reference Manual,
1550 * RCOMP registers appear to affect drive strength, pullup/pulldown offset,
1551 * and slew rate of various signal groups.
1552 *
1553 * Comments below are conjecture based on apparent similarity between the
1554 * E7501 and these two chips.
1555 */
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001556static void rcomp_copy_registers(void)
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001557{
1558 uint32_t dword;
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001559 uint8_t strength_control;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001560
1561 RAM_DEBUG_MESSAGE("Setting RCOMP registers.\n");
1562
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001563 /* Begin to write the RCOMP registers */
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001564 write8(RCOMP_MMIO + 0x2c, 0x0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001565
1566 // Set CMD and DQ/DQS strength to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001567 strength_control = read8(RCOMP_MMIO + DQCMDSTR) & 0x88;
1568 strength_control |= 0x40;
1569 write8(RCOMP_MMIO + DQCMDSTR, strength_control);
1570 write_8dwords(slew_2x, RCOMP_MMIO + 0x80);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001571 write16(RCOMP_MMIO + 0x42, 0);
1572
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001573 // Set CMD and DQ/DQS strength to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001574 strength_control = read8(RCOMP_MMIO + DQCMDSTR) & 0xF8;
1575 strength_control |= 0x04;
1576 write8(RCOMP_MMIO + DQCMDSTR, strength_control);
1577 write_8dwords(slew_2x, RCOMP_MMIO + 0x60);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001578 write16(RCOMP_MMIO + 0x40, 0);
1579
1580 // Set RCVEnOut# strength to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001581 strength_control = read8(RCOMP_MMIO + RCVENSTR) & 0xF8;
1582 strength_control |= 0x04;
1583 write8(RCOMP_MMIO + RCVENSTR, strength_control);
1584 write_8dwords(slew_2x, RCOMP_MMIO + 0x1c0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001585 write16(RCOMP_MMIO + 0x50, 0);
1586
1587 // Set CS# strength for x4 SDRAM to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001588 strength_control = read8(RCOMP_MMIO + CSBSTR) & 0x88;
1589 strength_control |= 0x04;
1590 write8(RCOMP_MMIO + CSBSTR, strength_control);
1591 write_8dwords(slew_2x, RCOMP_MMIO + 0x140);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001592 write16(RCOMP_MMIO + 0x48, 0);
1593
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001594 // Set CS# strength for x4 SDRAM to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001595 strength_control = read8(RCOMP_MMIO + CSBSTR) & 0x8F;
1596 strength_control |= 0x40;
1597 write8(RCOMP_MMIO + CSBSTR, strength_control);
1598 write_8dwords(slew_2x, RCOMP_MMIO + 0x160);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001599 write16(RCOMP_MMIO + 0x4a, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001600
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001601 // Set CKE strength for x4 SDRAM to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001602 strength_control = read8(RCOMP_MMIO + CKESTR) & 0x88;
1603 strength_control |= 0x04;
1604 write8(RCOMP_MMIO + CKESTR, strength_control);
1605 write_8dwords(slew_2x, RCOMP_MMIO + 0xa0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001606 write16(RCOMP_MMIO + 0x44, 0);
1607
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001608 // Set CKE strength for x4 SDRAM to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001609 strength_control = read8(RCOMP_MMIO + CKESTR) & 0x8F;
1610 strength_control |= 0x40;
1611 write8(RCOMP_MMIO + CKESTR, strength_control);
1612 write_8dwords(slew_2x, RCOMP_MMIO + 0xc0);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001613 write16(RCOMP_MMIO + 0x46, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001614
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001615 // Set CK strength for x4 SDRAM to 1x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001616 strength_control = read8(RCOMP_MMIO + CKSTR) & 0x88;
1617 strength_control |= 0x01;
1618 write8(RCOMP_MMIO + CKSTR, strength_control);
1619 write_8dwords(pull_updown_offset_table, RCOMP_MMIO + 0x180);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001620 write16(RCOMP_MMIO + 0x4c, 0);
1621
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001622 // Set CK strength for x4 SDRAM to 1x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001623 strength_control = read8(RCOMP_MMIO + CKSTR) & 0x8F;
1624 strength_control |= 0x10;
1625 write8(RCOMP_MMIO + CKSTR, strength_control);
1626 write_8dwords(pull_updown_offset_table, RCOMP_MMIO + 0x1a0);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001627 write16(RCOMP_MMIO + 0x4e, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001628
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001629 dword = read32(RCOMP_MMIO + 0x400);
1630 dword &= 0x7f7fffff;
1631 write32(RCOMP_MMIO + 0x400, dword);
1632
1633 dword = read32(RCOMP_MMIO + 0x408);
1634 dword &= 0x7f7fffff;
1635 write32(RCOMP_MMIO + 0x408, dword);
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001636}
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001637
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001638static void ram_set_rcomp_regs(void)
1639{
1640 /* Set the RCOMP MMIO base address */
1641 mchtest_control(RCOMP_BAR_ENABLE);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001642 pci_write_config32(MCHDEV, SMRBASE, (uintptr_t)RCOMP_MMIO);
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001643
1644 /* Block RCOMP updates while we configure the registers */
1645 rcomp_smr_control(RCOMP_HOLD);
1646 rcomp_copy_registers();
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001647 d060_control(D060_CMD_0);
1648 mchtest_control(MCHTST_CMD_0);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001649
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001650 uint8_t revision = pci_read_config8(MCHDEV, 0x08);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001651 if (revision >= 3) {
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001652 rcomp_smr_control(RCOMP_SMR_00);
1653 rcomp_smr_control(RCOMP_SMR_01);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001654 }
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001655 rcomp_smr_control(RCOMP_RELEASE);
1656
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001657 /* Wait 40 usec */
1658 SLOW_DOWN_IO;
1659
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001660 /* Clear the RCOMP MMIO base address */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001661 pci_write_config32(MCHDEV, SMRBASE, 0);
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001662 mchtest_control(RCOMP_BAR_DISABLE);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001663}
1664
1665/*-----------------------------------------------------------------------------
1666Public interface:
1667-----------------------------------------------------------------------------*/
1668
1669/**
1670 * Go through the JEDEC initialization sequence for all DIMMs, then enable
1671 * refresh and initialize ECC and memory to zero. Upon exit, SDRAM is up
1672 * and running.
1673 *
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001674 * @param ctrl PCI addresses of memory controller functions, and SMBus
1675 * addresses of DIMM slots on the mainboard.
1676 */
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001677static void sdram_enable(const struct mem_controller *ctrl)
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001678{
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001679 uint8_t dimm_mask = pci_read_config16(MCHDEV, SKPD);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001680 uint32_t dram_controller_mode;
1681
1682 if (dimm_mask == 0)
1683 return;
1684
1685 /* 1 & 2 Power up and start clocks */
1686 RAM_DEBUG_MESSAGE("Ram Enable 1\n");
1687 RAM_DEBUG_MESSAGE("Ram Enable 2\n");
1688
1689 /* A 200us delay is needed */
1690 DO_DELAY; EXTRA_DELAY;
1691
1692 /* 3. Apply NOP */
1693 RAM_DEBUG_MESSAGE("Ram Enable 3\n");
1694 do_ram_command(RAM_COMMAND_NOP, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001695
1696 /* 4 Precharge all */
1697 RAM_DEBUG_MESSAGE("Ram Enable 4\n");
1698 do_ram_command(RAM_COMMAND_PRECHARGE, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001699 /* wait until the all banks idle state... */
1700
1701 /* 5. Issue EMRS to enable DLL */
1702 RAM_DEBUG_MESSAGE("Ram Enable 5\n");
1703 do_ram_command(RAM_COMMAND_EMRS,
1704 SDRAM_EXTMODE_DLL_ENABLE |
1705 SDRAM_EXTMODE_DRIVE_NORMAL);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001706
1707 /* 6. Reset DLL */
1708 RAM_DEBUG_MESSAGE("Ram Enable 6\n");
1709 set_ram_mode(E7501_SDRAM_MODE | SDRAM_MODE_DLL_RESET);
1710 EXTRA_DELAY;
1711 /* Ensure a 200us delay between the DLL reset in step 6 and the final
1712 * mode register set in step 9.
1713 * Infineon needs this before any other command is sent to the ram.
1714 */
1715 DO_DELAY; EXTRA_DELAY;
1716
1717 /* 7 Precharge all */
1718 RAM_DEBUG_MESSAGE("Ram Enable 7\n");
1719 do_ram_command(RAM_COMMAND_PRECHARGE, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001720
1721 /* 8 Now we need 2 AUTO REFRESH / CBR cycles to be performed */
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001722 /* And for good luck 6 more CBRs */
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001723 RAM_DEBUG_MESSAGE("Ram Enable 8\n");
1724 int i;
Elyes HAOUAS93095522016-09-17 21:05:10 +02001725 for (i = 0; i < 8; i++)
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001726 do_ram_command(RAM_COMMAND_CBR, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001727
1728 /* 9 mode register set */
1729 RAM_DEBUG_MESSAGE("Ram Enable 9\n");
1730 set_ram_mode(E7501_SDRAM_MODE | SDRAM_MODE_NORMAL);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001731
1732 /* 10 DDR Receive FIFO RE-Sync */
1733 RAM_DEBUG_MESSAGE("Ram Enable 10\n");
1734 RAM_RESET_DDR_PTR();
1735 EXTRA_DELAY;
1736
1737 /* 11 normal operation */
1738 RAM_DEBUG_MESSAGE("Ram Enable 11\n");
1739 do_ram_command(RAM_COMMAND_NORMAL, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001740
1741 // Reconfigure the row boundaries and Top of Low Memory
1742 // to match the true size of the DIMMs
1743 configure_e7501_ram_addresses(ctrl, dimm_mask);
1744
1745 /* Finally enable refresh */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001746 dram_controller_mode = pci_read_config32(MCHDEV, DRC);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001747 dram_controller_mode |= (1 << 29);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001748 pci_write_config32(MCHDEV, DRC, dram_controller_mode);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001749 EXTRA_DELAY;
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001750}
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001751
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001752/**
1753 * @param ctrl PCI addresses of memory controller functions, and SMBus
1754 * addresses of DIMM slots on the mainboard.
1755 */
1756static void sdram_post_ecc(const struct mem_controller *ctrl)
1757{
1758 /* Fast CS# Enable. */
1759 uint32_t dram_controller_mode = pci_read_config32(MCHDEV, DRC);
1760 dram_controller_mode = pci_read_config32(MCHDEV, DRC);
1761 dram_controller_mode |= (1 << 17);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001762 pci_write_config32(MCHDEV, DRC, dram_controller_mode);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001763}
1764
1765/**
1766 * Configure SDRAM controller parameters that depend on characteristics of the
1767 * DIMMs installed in the system. These characteristics are read from the
1768 * DIMMs via the standard Serial Presence Detect (SPD) interface.
1769 *
1770 * @param ctrl PCI addresses of memory controller functions, and SMBus
1771 * addresses of DIMM slots on the mainboard.
1772 */
1773static void sdram_set_spd_registers(const struct mem_controller *ctrl)
1774{
1775 uint8_t dimm_mask;
1776
1777 RAM_DEBUG_MESSAGE("Reading SPD data...\n");
1778
1779 dimm_mask = spd_get_supported_dimms(ctrl);
1780
1781 if (dimm_mask == 0) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -08001782 printk(BIOS_DEBUG, "No usable memory for this controller\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001783 } else {
1784 enable_e7501_clocks(dimm_mask);
1785
1786 RAM_DEBUG_MESSAGE("setting based on SPD data...\n");
1787
1788 configure_e7501_row_attributes(ctrl, dimm_mask);
1789 configure_e7501_dram_controller_mode(ctrl, dimm_mask);
1790 configure_e7501_cas_latency(ctrl, dimm_mask);
1791 RAM_RESET_DDR_PTR();
1792
1793 configure_e7501_dram_timing(ctrl, dimm_mask);
1794 DO_DELAY;
1795 RAM_DEBUG_MESSAGE("done\n");
1796 }
1797
1798 /* NOTE: configure_e7501_ram_addresses() is NOT called here.
1799 * We want to keep the default 64 MB/row mapping until sdram_enable() is called,
1800 * even though the default mapping is almost certainly incorrect.
1801 * The default mapping makes it easy to initialize all of the DIMMs
1802 * even if the total system memory is > 4 GB.
1803 *
1804 * Save the dimm_mask for when sdram_enable is called, so it can call
1805 * configure_e7501_ram_addresses() without having to regenerate the bitmask
1806 * of usable DIMMs.
1807 */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001808 pci_write_config16(MCHDEV, SKPD, dimm_mask);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001809}
1810
1811/**
1812 * Do basic RAM setup that does NOT depend on serial presence detect
1813 * information (i.e. independent of DIMM specifics).
1814 *
1815 * @param ctrl PCI addresses of memory controller functions, and SMBus
1816 * addresses of DIMM slots on the mainboard.
1817 */
1818static void sdram_set_registers(const struct mem_controller *ctrl)
1819{
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001820 uint32_t dword;
1821 uint16_t word;
1822 uint8_t byte;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001823
1824 ram_set_rcomp_regs();
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001825
1826 /* Enable 0:0.1, 0:2.1 */
1827 word = pci_read_config16(MCHDEV, DVNP);
1828 word &= ~0x05;
1829 pci_write_config16(MCHDEV, DVNP, word);
1830
1831 /* Disable high-memory remap (power-on defaults, really) */
1832 pci_write_config16(MCHDEV, REMAPBASE, 0x03ff);
1833 pci_write_config16(MCHDEV, REMAPLIMIT, 0x0);
1834
1835 /* Disable legacy MMIO (0xC0000-0xEFFFF is DRAM) */
1836 int i;
1837 pci_write_config8(MCHDEV, PAM_0, 0x30);
Elyes HAOUAS93095522016-09-17 21:05:10 +02001838 for (i = 1; i <= 6; i++)
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001839 pci_write_config8(MCHDEV, PAM_0 + i, 0x33);
1840
1841 /* Conservatively say each row has 64MB of ram, we will fix this up later
1842 * Initial TOLM 8 rows 64MB each (1<<3 * 1<<26) >> 16 = 1<<13
1843 *
1844 * FIXME: Hard-coded limit to first four rows to prevent overlap!
1845 */
1846 pci_write_config32(MCHDEV, DRB_ROW_0, 0x04030201);
1847 pci_write_config32(MCHDEV, DRB_ROW_4, 0x04040404);
1848 //pci_write_config32(MCHDEV, DRB_ROW_4, 0x08070605);
1849 pci_write_config16(MCHDEV, TOLM, (1<<13));
1850
1851 /* DIMM clocks off */
1852 pci_write_config8(MCHDEV, CKDIS, 0xff);
1853
1854 /* reset row attributes */
1855 pci_write_config32(MCHDEV, DRA, 0x0);
1856
1857 // The only things we need to set here are DRAM idle timer, Back-to-Back Read Turnaround, and
1858 // Back-to-Back Write-Read Turnaround. All others are configured based on SPD.
1859 dword = pci_read_config32(MCHDEV, DRT);
1860 dword &= 0xC7F8FFFF;
1861 dword |= (0x28<<24)|(0x03<<16);
1862 pci_write_config32(MCHDEV, DRT, dword);
1863
1864 dword = pci_read_config32(MCHDEV, DRC);
1865 dword &= 0xffcef8f7;
1866 dword |= 0x00210008;
1867 pci_write_config32(MCHDEV, DRC, dword);
1868
1869 /* Undocumented */
1870 pci_write_config8(MCHDEV, 0x88, 0x80);
1871
1872 /* Undocumented. Set much later in vendor BIOS. */
1873 byte = pci_read_config8(MCHDEV, 0xd9);
1874 byte &= ~0x60;
1875 pci_write_config8(MCHDEV, 0xd9, byte);
1876
1877#ifdef SUSPICIOUS_LOOKING_CODE
1878 /* This will access D2:F0:0x50, is this correct??
1879 * Vendor BIOS reads Device ID before this is set.
1880 * Undocumented in the p64h2 PCI-X bridge datasheet.
1881 */
1882 byte = pci_read_config8(PCI_DEV(0,2,0), 0x50);
1883 byte &= 0xcf;
1884 byte |= 0x30
1885 pci_write_config8(PCI_DEV(0,2,0), 0x50, byte);
1886#endif
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001887
1888 uint8_t revision = pci_read_config8(MCHDEV, 0x08);
1889 if (revision >= 3)
1890 d060_control(D060_CMD_1);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001891}
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001892
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001893/**
1894 *
1895 *
1896 */
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001897void e7505_mch_init(const struct mem_controller *memctrl)
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001898{
1899 RAM_DEBUG_MESSAGE("Northbridge prior to SDRAM init:\n");
1900 DUMPNORTH();
1901
1902 sdram_set_registers(memctrl);
1903 sdram_set_spd_registers(memctrl);
1904 sdram_enable(memctrl);
1905}
1906
Kyösti Mälkki70d92b92017-04-19 19:57:01 +03001907uintptr_t restore_top_of_low_cacheable(void)
Kyösti Mälkki41dd3db2012-07-03 11:36:44 +03001908{
1909 u32 tolm = (pci_read_config16(MCHDEV, TOLM) & ~0x7ff) << 16;
Kyösti Mälkki70d92b92017-04-19 19:57:01 +03001910 return tolm;
Kyösti Mälkki41dd3db2012-07-03 11:36:44 +03001911}
1912
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001913/**
1914 * Scrub and reset error counts for ECC dimms.
1915 *
1916 * NOTE: this will invalidate cache and disable XIP cache for the
1917 * short remaining part of romstage.
1918 */
1919void e7505_mch_scrub_ecc(unsigned long ret_addr)
1920{
1921 unsigned long ret_addr2 = (unsigned long)((unsigned long*)&ret_addr-1);
1922 if ((pci_read_config32(MCHDEV, DRC)>>20 & 3) == 2)
1923 initialize_ecc(ret_addr, ret_addr2);
1924}
1925
1926void e7505_mch_done(const struct mem_controller *memctrl)
1927{
1928 sdram_post_ecc(memctrl);
1929
1930 RAM_DEBUG_MESSAGE("Northbridge following SDRAM init:\n");
1931 DUMPNORTH();
1932}
1933
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +03001934int e7505_mch_is_ready(void)
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001935{
1936 uint32_t dword = pci_read_config32(MCHDEV, DRC);
1937 return !!(dword & DRC_DONE);
1938}