blob: 8ed5007428a4b43339d513d3b133d646716131a9 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Martin Roth5474eb12018-05-26 19:22:33 -06003
Stefan Reinauerb15975b2011-10-21 12:57:59 -07004/* This was originally for the e7500, modified for e7501
5 * The primary differences are that 7501 apparently can
6 * support single channel RAM (i haven't tested),
7 * CAS1.5 is no longer supported, The ECC scrubber
8 * now supports a mode to zero RAM and init ECC in one step
9 * and the undocumented registers at 0x80 require new
10 * (undocumented) values determined by guesswork and
11 * comparison w/ OEM BIOS values.
12 * Steven James 02/06/2003
13 */
14
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +030015#include <stdint.h>
16#include <device/pci_def.h>
17#include <arch/io.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020018#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.h>
Alexandru Gagniucaf4bd592014-01-12 15:42:58 -060020#include <lib.h>
Kyösti Mälkki717b6e32018-05-17 14:16:03 +030021#include <commonlib/helpers.h>
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +030022#include <console/console.h>
Kyösti Mälkki97c064f2012-04-18 20:33:35 +030023#include <cpu/x86/mtrr.h>
24#include <cpu/x86/cache.h>
25#include <cpu/x86/msr.h>
Stefan Reinauerb15975b2011-10-21 12:57:59 -070026#include <assert.h>
27#include <spd.h>
28#include <sdram_mode.h>
Kyösti Mälkkiec558682019-01-09 13:33:39 +020029#include <timestamp.h>
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +020030#include <southbridge/intel/i82801dx/i82801dx.h>
Kyösti Mälkki93b4ed92012-04-18 21:13:33 +030031
32#include "raminit.h"
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +020033#include "e7505.h"
Stefan Reinauerb15975b2011-10-21 12:57:59 -070034
35/*-----------------------------------------------------------------------------
36Definitions:
37-----------------------------------------------------------------------------*/
38
Julius Wernercd49cce2019-03-05 16:53:33 -080039#if CONFIG(DEBUG_RAM_SETUP)
Stefan Reinauer65b72ab2015-01-05 12:59:54 -080040#define RAM_DEBUG_MESSAGE(x) printk(BIOS_DEBUG, x)
41#define RAM_DEBUG_HEX32(x) printk(BIOS_DEBUG, "%08x", x)
42#define RAM_DEBUG_HEX8(x) printk(BIOS_DEBUG, "%02x", x)
Stefan Reinauerb15975b2011-10-21 12:57:59 -070043#else
44#define RAM_DEBUG_MESSAGE(x)
45#define RAM_DEBUG_HEX32(x)
46#define RAM_DEBUG_HEX8(x)
Stefan Reinauerb15975b2011-10-21 12:57:59 -070047#endif
48
49#define E7501_SDRAM_MODE (SDRAM_BURST_INTERLEAVED | SDRAM_BURST_4)
50#define SPD_ERROR "Error reading SPD info\n"
51
Kyösti Mälkki4c0e2772018-05-17 14:16:03 +030052#define MCHDEV PCI_DEV(0, 0, 0)
53#define RASDEV PCI_DEV(0, 0, 1)
54#define AGPDEV PCI_DEV(0, 1, 0)
55#define D060DEV PCI_DEV(0, 6, 0)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +030056
Stefan Reinauerb15975b2011-10-21 12:57:59 -070057// NOTE: This used to be 0x100000.
58// That doesn't work on systems where A20M# is asserted, because
59// attempts to access 0x1000NN end up accessing 0x0000NN.
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080060#define RCOMP_MMIO ((u8 *)0x200000)
Stefan Reinauerb15975b2011-10-21 12:57:59 -070061
62struct dimm_size {
63 unsigned long side1;
64 unsigned long side2;
65};
66
67static const uint32_t refresh_frequency[] = {
68 /* Relative frequency (array value) of each E7501 Refresh Mode Select
69 * (RMS) value (array index)
70 * 0 == least frequent refresh (longest interval between refreshes)
71 * [0] disabled -> 0
72 * [1] 15.6 usec -> 2
73 * [2] 7.8 usec -> 3
74 * [3] 64 usec -> 1
75 * [4] reserved -> 0
76 * [5] reserved -> 0
77 * [6] reserved -> 0
78 * [7] 64 clocks -> 4
79 */
80 0, 2, 3, 1, 0, 0, 0, 4
81};
82
83static const uint32_t refresh_rate_map[] = {
84 /* Map the JEDEC spd refresh rates (array index) to E7501 Refresh Mode
85 * Select values (array value)
86 * These are all the rates defined by JESD21-C Appendix D, Rev. 1.0
87 * The E7501 supports only 15.6 us (1), 7.8 us (2), 64 us (3), and
88 * 64 clock (481 ns) (7) refresh.
89 * [0] == 15.625 us -> 15.6 us
90 * [1] == 3.9 us -> 481 ns
91 * [2] == 7.8 us -> 7.8 us
92 * [3] == 31.3 us -> 15.6 us
93 * [4] == 62.5 us -> 15.6 us
94 * [5] == 125 us -> 64 us
95 */
96 1, 7, 2, 1, 1, 3
97};
98
99#define MAX_SPD_REFRESH_RATE ((sizeof(refresh_rate_map) / sizeof(uint32_t)) - 1)
100
101// SPD parameters that must match for dual-channel operation
102static const uint8_t dual_channel_parameters[] = {
103 SPD_MEMORY_TYPE,
104 SPD_MODULE_VOLTAGE,
105 SPD_NUM_COLUMNS,
106 SPD_NUM_ROWS,
107 SPD_NUM_DIMM_BANKS,
108 SPD_PRIMARY_SDRAM_WIDTH,
109 SPD_NUM_BANKS_PER_SDRAM
110};
111
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300112 /* Comments here are remains of e7501 or even 855PM.
113 * They might be partially (in)correct for e7505.
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700114 */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700115
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300116 /* (DRAM Read Timing Control, if similar to 855PM?)
117 * 0x80 - 0x81 documented differently for e7505
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700118 * This register has something to do with CAS latencies,
Martin Roth128c1042016-11-18 09:29:03 -0700119 * possibly this is the real chipset control.
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700120 * At 0x00 CAS latency 1.5 works.
121 * At 0x06 CAS latency 2.5 works.
122 * At 0x01 CAS latency 2.0 works.
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300123 *
124 * This is still undocumented in e7501, but with different values
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700125 * 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
129 * work at all.
130 * Steven James 02/06/2003
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300131 *
132 * NOTE: values now configured in configure_e7501_cas_latency() based
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700133 * on SPD info and total number of DIMMs (per Intel)
134 */
135
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300136 /* FDHC - Fixed DRAM Hole Control ???
137 * 0x58 undocumented for e7505, memory hole in southbridge configuration?
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700138 * [7:7] Hole_Enable
139 * 0 == No memory Hole
140 * 1 == Memory Hole from 15MB to 16MB
141 * [6:0] Reserved
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700142 */
143
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700144 /* Another Intel undocumented register
145 * 0x88 - 0x8B
146 * [31:31] Purpose unknown
147 * [26:26] Master DLL Reset?
148 * 0 == Normal operation?
149 * 1 == Reset?
150 * [07:07] Periodic memory recalibration?
151 * 0 == Disabled?
152 * 1 == Enabled?
153 * [04:04] Receive FIFO RE-Sync?
154 * 0 == Normal operation?
155 * 1 == Reset?
156 */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700157
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300158/* DDR RECOMP tables */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700159// Slew table for 2x drive?
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300160static const uint32_t slew_2x[] = {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700161 0x00000000, 0x76543210, 0xffffeca8, 0xffffffff,
162 0x21000000, 0xa8765432, 0xffffffec, 0xffffffff,
163};
164
165// Pull Up / Pull Down offset table, if analogous to IXP2800?
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300166static const uint32_t pull_updown_offset_table[] = {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700167 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
168 0x88888888, 0x88888888, 0x88888888, 0x88888888,
169};
170
171/*-----------------------------------------------------------------------------
172Delay functions:
173-----------------------------------------------------------------------------*/
174
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300175/* Estimate that SLOW_DOWN_IO takes about 1 us */
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700176#define SLOW_DOWN_IO inb(0x80)
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300177static void local_udelay(int i)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700178{
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300179 while (i--) {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700180 SLOW_DOWN_IO;
181 }
182}
183
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300184/* delay for 200us */
185#define DO_DELAY local_udelay(200)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700186#define EXTRA_DELAY DO_DELAY
187
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200188/*-----------------------------------------------------------------------------
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300189Handle (undocumented) control bits MCHTST and PCI_DEV(0,6,0)
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200190-----------------------------------------------------------------------------*/
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300191typedef enum {
192 MCHTST_CMD_0,
193 D060_ENABLE,
194 D060_DISABLE,
195 RCOMP_BAR_ENABLE,
196 RCOMP_BAR_DISABLE,
197} mchtst_cc;
198
199typedef enum {
200 D060_CMD_0,
201 D060_CMD_1,
202} d060_cc;
203
204typedef enum {
205 RCOMP_HOLD,
206 RCOMP_RELEASE,
207 RCOMP_SMR_00,
208 RCOMP_SMR_01,
209} rcomp_smr_cc;
210
211/**
212 * MCHTST - 0xF4 - 0xF7 -- Based on similarity to 855PM
213 *
214 * [31:31] Purpose unknown
215 * [30:30] Purpose unknown
216 * [29:23] Unknown - not used?
217 * [22:22] System Memory MMR Enable
218 * 0 == Disable: mem space and BAR at 0x14 are not accessible
219 * 1 == Enable: mem space and BAR at 0x14 are accessible
220 * [21:20] Purpose unknown
221 * [19:02] Unknown - not used?
222 * [01:01] D6EN (Device #6 enable)
223 * 0 == Disable
224 * 1 == Enable
225 * [00:00] Unknown - not used?
226 */
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300227static void mchtest_control(mchtst_cc cmd)
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300228{
229 uint32_t dword = pci_read_config32(MCHDEV, MCHTST);
230 switch (cmd) {
231 case MCHTST_CMD_0:
232 dword &= ~(3 << 30);
233 break;
234 case RCOMP_BAR_ENABLE:
235 dword |= (1 << 22);
236 break;
237 case RCOMP_BAR_DISABLE:
238 dword &= ~(1 << 22);
239 break;
240 case D060_ENABLE:
241 dword |= (1 << 1);
242 break;
243 case D060_DISABLE:
244 dword &= ~(1 << 1);
245 break;
246 };
247 pci_write_config32(MCHDEV, MCHTST, dword);
248}
249
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200250
251/**
252 *
253 */
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300254static void d060_control(d060_cc cmd)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700255{
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300256 mchtest_control(D060_ENABLE);
257 uint32_t dword = pci_read_config32(D060DEV, 0xf0);
258 switch (cmd) {
259 case D060_CMD_0:
260 dword |= (1 << 2);
261 break;
262 case D060_CMD_1:
263 dword |= (3 << 27);
264 break;
265 }
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300266 pci_write_config32(D060DEV, 0xf0, dword);
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300267 mchtest_control(D060_DISABLE);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200268}
269
270/**
271 *
272 */
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300273static void rcomp_smr_control(rcomp_smr_cc cmd)
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200274{
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300275 uint32_t dword = read32(RCOMP_MMIO + SMRCTL);
276 switch (cmd) {
277 case RCOMP_HOLD:
278 dword |= (1 << 9);
279 break;
280 case RCOMP_RELEASE:
281 dword &= ~((1 << 9) | (3 << 0));
282 dword |= (1 << 10) | (1 << 0);
283 break;
284 case RCOMP_SMR_00:
285 dword &= ~(1 << 8);
286 break;
287 case RCOMP_SMR_01:
288 dword |= (1 << 10) | (1 << 8);
289 break;
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200290 }
Kyösti Mälkki26c7b862012-04-12 22:46:23 +0300291 write32(RCOMP_MMIO + SMRCTL, dword);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700292}
293
294/*-----------------------------------------------------------------------------
295Serial presence detect (SPD) functions:
296-----------------------------------------------------------------------------*/
297
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200298static void die_on_spd_error(int spd_return_value)
299{
300 if (spd_return_value < 0)
301 die("Error reading SPD info\n");
302}
303
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700304/**
305 * Calculate the page size for each physical bank of the DIMM:
306 * log2(page size) = (# columns) + log2(data width)
307 *
308 * NOTE: Page size is the total number of data bits in a row.
309 *
310 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
311 * @return log2(page size) for each side of the DIMM.
312 */
313static struct dimm_size sdram_spd_get_page_size(uint16_t dimm_socket_address)
314{
315 uint16_t module_data_width;
316 int value;
317 struct dimm_size pgsz;
318
319 pgsz.side1 = 0;
320 pgsz.side2 = 0;
321
322 // Side 1
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200323 value = smbus_read_byte(dimm_socket_address, SPD_NUM_COLUMNS);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700324 if (value < 0)
325 goto hw_err;
326 pgsz.side1 = value & 0xf; // # columns in bank 1
327
328 /* Get the module data width and convert it to a power of two */
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200329 value = smbus_read_byte(dimm_socket_address, SPD_MODULE_DATA_WIDTH_MSB);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700330 if (value < 0)
331 goto hw_err;
332 module_data_width = (value & 0xff) << 8;
333
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200334 value = smbus_read_byte(dimm_socket_address, SPD_MODULE_DATA_WIDTH_LSB);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700335 if (value < 0)
336 goto hw_err;
337 module_data_width |= (value & 0xff);
338
339 pgsz.side1 += log2(module_data_width);
340
341 /* side two */
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200342 value = smbus_read_byte(dimm_socket_address, SPD_NUM_DIMM_BANKS);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700343 if (value < 0)
344 goto hw_err;
345 if (value > 2)
346 die("Bad SPD value\n");
347 if (value == 2) {
348
349 pgsz.side2 = pgsz.side1; // Assume symmetric banks until we know differently
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200350 value = smbus_read_byte(dimm_socket_address, SPD_NUM_COLUMNS);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700351 if (value < 0)
352 goto hw_err;
353 if ((value & 0xf0) != 0) {
354 // Asymmetric banks
355 pgsz.side2 -= value & 0xf; /* Subtract out columns on side 1 */
356 pgsz.side2 += (value >> 4) & 0xf; /* Add in columns on side 2 */
357 }
358 }
359
360 return pgsz;
361
362 hw_err:
363 die(SPD_ERROR);
364 return pgsz; // Never reached
365}
366
367/**
368 * Read the width in bits of each DIMM side's DRAMs via SPD (i.e. 4, 8, 16).
369 *
370 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
371 * @return Width in bits of each DIMM side's DRAMs.
372 */
373static struct dimm_size sdram_spd_get_width(uint16_t dimm_socket_address)
374{
375 int value;
376 struct dimm_size width;
377
378 width.side1 = 0;
379 width.side2 = 0;
380
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200381 value = smbus_read_byte(dimm_socket_address, SPD_PRIMARY_SDRAM_WIDTH);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700382 die_on_spd_error(value);
383
384 width.side1 = value & 0x7f; // Mask off bank 2 flag
385
386 if (value & 0x80) {
387 width.side2 = width.side1 << 1; // Bank 2 exists and is double-width
388 } else {
389 // If bank 2 exists, it's the same width as bank 1
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200390 value = smbus_read_byte(dimm_socket_address, SPD_NUM_DIMM_BANKS);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700391 die_on_spd_error(value);
392
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700393 if (value == 2)
394 width.side2 = width.side1;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700395 }
396
397 return width;
398}
399
400/**
401 * Calculate the log base 2 size in bits of both DIMM sides.
402 *
403 * log2(# bits) = (# columns) + log2(data width) +
404 * (# rows) + log2(banks per SDRAM)
405 *
406 * Note that it might be easier to use SPD byte 31 here, it has the DIMM size
407 * as a multiple of 4MB. The way we do it now we can size both sides of an
408 * asymmetric DIMM.
409 *
410 * @param dimm_socket_address SMBus address of DIMM socket to interrogate.
411 * @return log2(number of bits) for each side of the DIMM.
412 */
Martin Roth468d02c2019-10-23 21:44:42 -0600413static struct dimm_size spd_get_dimm_size(unsigned int dimm_socket_address)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700414{
415 int value;
416
417 // Start with log2(page size)
418 struct dimm_size sz = sdram_spd_get_page_size(dimm_socket_address);
419
420 if (sz.side1 > 0) {
421
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200422 value = smbus_read_byte(dimm_socket_address, SPD_NUM_ROWS);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700423 die_on_spd_error(value);
424
425 sz.side1 += value & 0xf;
426
427 if (sz.side2 > 0) {
428
429 // Double-sided DIMM
430 if (value & 0xF0)
431 sz.side2 += value >> 4; // Asymmetric
432 else
433 sz.side2 += value; // Symmetric
434 }
435
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200436 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700437 SPD_NUM_BANKS_PER_SDRAM);
438 die_on_spd_error(value);
439
440 value = log2(value);
441 sz.side1 += value;
442 if (sz.side2 > 0)
443 sz.side2 += value;
444 }
445
446 return sz;
447}
448
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700449/**
450 * Determine whether two DIMMs have the same value for an SPD parameter.
451 *
452 * @param spd_byte_number The SPD byte number to compare in both DIMMs.
453 * @param dimm0_address SMBus address of the 1st DIMM socket to interrogate.
454 * @param dimm1_address SMBus address of the 2nd DIMM socket to interrogate.
455 * @return 1 if both DIMM sockets report the same value for the specified
456 * SPD parameter, 0 if the values differed or an error occurred.
457 */
458static uint8_t are_spd_values_equal(uint8_t spd_byte_number,
459 uint16_t dimm0_address,
460 uint16_t dimm1_address)
461{
462 uint8_t bEqual = 0;
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200463 int dimm0_value = smbus_read_byte(dimm0_address, spd_byte_number);
464 int dimm1_value = smbus_read_byte(dimm1_address, spd_byte_number);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700465
466 if ((dimm0_value >= 0) && (dimm1_value >= 0)
467 && (dimm0_value == dimm1_value))
468 bEqual = 1;
469
470 return bEqual;
471}
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700472
473/**
474 * Scan for compatible DIMMs.
475 *
476 * The code in this module only supports dual-channel operation, so we test
477 * that compatible DIMMs are paired.
478 *
479 * @param ctrl PCI addresses of memory controller functions, and SMBus
480 * addresses of DIMM slots on the mainboard.
481 * @return A bitmask indicating which of the possible sockets for each channel
482 * was found to contain a compatible DIMM.
483 * Bit 0 corresponds to the closest socket for channel 0
484 * Bit 1 to the next socket for channel 0
485 * ...
486 * Bit MAX_DIMM_SOCKETS_PER_CHANNEL-1 to the last socket for channel 0
487 * Bit MAX_DIMM_SOCKETS_PER_CHANNEL is the closest socket for channel 1
488 * ...
489 * Bit 2*MAX_DIMM_SOCKETS_PER_CHANNEL-1 is the last socket for channel 1
490 */
491static uint8_t spd_get_supported_dimms(const struct mem_controller *ctrl)
492{
493 int i;
494 uint8_t dimm_mask = 0;
495
496 // Have to increase size of dimm_mask if this assertion is violated
497 ASSERT(MAX_DIMM_SOCKETS_PER_CHANNEL <= 4);
498
499 // Find DIMMs we can support on channel 0.
500 // Then see if the corresponding channel 1 DIMM has the same parameters,
501 // since we only support dual-channel.
502
503 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
504
505 uint16_t channel0_dimm = ctrl->channel0[i];
506 uint16_t channel1_dimm = ctrl->channel1[i];
507 uint8_t bDualChannel = 1;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700508 struct dimm_size page_size;
509 struct dimm_size sdram_width;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700510 int spd_value;
511
512 if (channel0_dimm == 0)
513 continue; // No such socket on this mainboard
514
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200515 if (smbus_read_byte(channel0_dimm, SPD_MEMORY_TYPE) !=
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700516 SPD_MEMORY_TYPE_SDRAM_DDR)
517 continue;
518
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200519 if (smbus_read_byte(channel0_dimm, SPD_MODULE_VOLTAGE) !=
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700520 SPD_VOLTAGE_SSTL2)
521 continue; // Unsupported voltage
522
523 // E7501 does not support unregistered DIMMs
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200524 spd_value = smbus_read_byte(channel0_dimm, SPD_MODULE_ATTRIBUTES);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700525 if (!(spd_value & MODULE_REGISTERED) || (spd_value < 0))
526 continue;
527
528 // Must support burst = 4 for dual-channel operation on E7501
529 // NOTE: for single-channel, burst = 8 is required
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200530 spd_value = smbus_read_byte(channel0_dimm,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700531 SPD_SUPPORTED_BURST_LENGTHS);
532 if (!(spd_value & SPD_BURST_LENGTH_4) || (spd_value < 0))
533 continue;
534
535 page_size = sdram_spd_get_page_size(channel0_dimm);
536 sdram_width = sdram_spd_get_width(channel0_dimm);
537
538 // Validate DIMM page size
539 // The E7501 only supports page sizes of 4, 8, 16, or 32 KB per channel
540 // NOTE: 4 KB = 32 Kb = 2^15
541 // 32 KB = 262 Kb = 2^18
542
543 if ((page_size.side1 < 15) || (page_size.side1 > 18))
544 continue;
545
546 // If DIMM is double-sided, verify side2 page size
547 if (page_size.side2 != 0) {
548 if ((page_size.side2 < 15)
549 || (page_size.side2 > 18))
550 continue;
551 }
552 // Validate SDRAM width
553 // The E7501 only supports x4 and x8 devices
554
555 if ((sdram_width.side1 != 4) && (sdram_width.side1 != 8))
556 continue;
557
558 // If DIMM is double-sided, verify side2 width
559 if (sdram_width.side2 != 0) {
560 if ((sdram_width.side2 != 4)
561 && (sdram_width.side2 != 8))
562 continue;
563 }
Kyösti Mälkkib71fb522020-01-07 11:16:35 +0200564
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700565 // Channel 0 DIMM looks compatible.
566 // Now see if it is paired with the proper DIMM on channel 1.
567
568 ASSERT(channel1_dimm != 0); // No such socket on this mainboard??
569
570 // NOTE: unpopulated DIMMs cause read to fail
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200571 spd_value = smbus_read_byte(channel1_dimm, SPD_MODULE_ATTRIBUTES);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700572 if (!(spd_value & MODULE_REGISTERED) || (spd_value < 0)) {
573
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800574 printk(BIOS_DEBUG, "Skipping un-matched DIMMs - only dual-channel operation supported\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700575 continue;
576 }
Kyösti Mälkkib71fb522020-01-07 11:16:35 +0200577
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200578 spd_value = smbus_read_byte(channel1_dimm,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700579 SPD_SUPPORTED_BURST_LENGTHS);
580 if (!(spd_value & SPD_BURST_LENGTH_4) || (spd_value < 0))
581 continue;
582
583 int j;
584 for (j = 0; j < sizeof(dual_channel_parameters); ++j) {
585 if (!are_spd_values_equal
586 (dual_channel_parameters[j], channel0_dimm,
587 channel1_dimm)) {
588
589 bDualChannel = 0;
590 break;
591 }
592 }
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700593
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700594 if (bDualChannel) {
Kyösti Mälkki58d6ff12018-06-02 18:38:04 +0300595 // This DIMM pair is usable
596 dimm_mask |= 1 << i;
597 dimm_mask |= 1 << (MAX_DIMM_SOCKETS_PER_CHANNEL + i);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700598 } else
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800599 printk(BIOS_DEBUG, "Skipping un-matched DIMMs - only dual-channel operation supported\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700600 }
601
602 return dimm_mask;
603}
604
605/*-----------------------------------------------------------------------------
606SDRAM configuration functions:
607-----------------------------------------------------------------------------*/
608
609/**
610 * Send the specified command to all DIMMs.
611 *
612 * @param command Specifies the command to be sent to the DIMMs.
613 * @param jedec_mode_bits For the MRS & EMRS commands, bits 0-12 contain the
614 * register value in JEDEC format.
615 */
616static void do_ram_command(uint8_t command, uint16_t jedec_mode_bits)
617{
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200618 uint8_t dimm_start_64M_multiple;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800619 uintptr_t dimm_start_address;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700620 uint32_t dram_controller_mode;
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200621 uint8_t i;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700622
623 // Configure the RAM command
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300624 dram_controller_mode = pci_read_config32(MCHDEV, DRC);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700625 dram_controller_mode &= 0xFFFFFF8F;
626 dram_controller_mode |= command;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300627 pci_write_config32(MCHDEV, DRC, dram_controller_mode);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700628
629 // RAM_COMMAND_NORMAL is an exception.
630 // 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 +0300631 if (command == RAM_COMMAND_NORMAL) {
632 EXTRA_DELAY;
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200633 return;
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300634 }
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700635
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200636 // NOTE: for mode select commands, some of the location address bits are part of the command
637 // Map JEDEC mode bits to E7505
638 if (command == RAM_COMMAND_MRS) {
639 // Host address lines [25:18] map to DIMM address lines [7:0]
640 // Host address lines [17:16] map to DIMM address lines [9:8]
641 // Host address lines [15:4] map to DIMM address lines [11:0]
642 dimm_start_address = (jedec_mode_bits & 0x00ff) << 18;
643 dimm_start_address |= (jedec_mode_bits & 0x0300) << 8;
644 dimm_start_address |= (jedec_mode_bits & 0x0fff) << 4;
645 } else if (command == RAM_COMMAND_EMRS) {
646 // Host address lines [15:4] map to DIMM address lines [11:0]
647 dimm_start_address = (jedec_mode_bits << 4);
648 } else {
649 ASSERT(jedec_mode_bits == 0);
650 dimm_start_address = 0;
651 }
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700652
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200653 // Send the command to all DIMMs by accessing a memory location within each
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700654
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200655 dimm_start_64M_multiple = 0;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700656
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300657 /* FIXME: Only address the number of rows present in the system?
658 * Seems like rows 4-7 overlap with 0-3.
659 */
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200660 for (i = 0; i < (MAX_NUM_CHANNELS * MAX_DIMM_SOCKETS_PER_CHANNEL); ++i) {
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700661
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300662 uint8_t dimm_end_64M_multiple = pci_read_config8(MCHDEV, DRB_ROW_0 + i);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700663
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200664 if (dimm_end_64M_multiple > dimm_start_64M_multiple) {
665 dimm_start_address &= 0x3ffffff;
666 dimm_start_address |= dimm_start_64M_multiple << 26;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800667 read32((void *)dimm_start_address);
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +0300668 // Set the start of the next DIMM
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +0200669 dimm_start_64M_multiple = dimm_end_64M_multiple;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700670 }
671 }
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +0300672 EXTRA_DELAY;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700673}
674
675/**
676 * Set the mode register of all DIMMs.
677 *
678 * The proper CAS# latency setting is added to the mode bits specified
679 * by the caller.
680 *
681 * @param jedec_mode_bits For the MRS & EMRS commands, bits 0-12 contain the
682 * register value in JEDEC format.
683 */
684static void set_ram_mode(uint16_t jedec_mode_bits)
685{
686 ASSERT(!(jedec_mode_bits & SDRAM_CAS_MASK));
687
688 uint32_t dram_cas_latency =
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300689 pci_read_config32(MCHDEV, DRT) & DRT_CAS_MASK;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700690
691 switch (dram_cas_latency) {
692 case DRT_CAS_2_5:
693 jedec_mode_bits |= SDRAM_CAS_2_5;
694 break;
695
696 case DRT_CAS_2_0:
697 jedec_mode_bits |= SDRAM_CAS_2_0;
698 break;
699
700 default:
701 BUG();
702 break;
703 }
704
705 do_ram_command(RAM_COMMAND_MRS, jedec_mode_bits);
706}
707
708/*-----------------------------------------------------------------------------
Martin Roth128c1042016-11-18 09:29:03 -0700709DIMM-independent configuration functions:
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700710-----------------------------------------------------------------------------*/
711
712/**
713 * Configure the E7501's DRAM Row Boundary (DRB) registers for the memory
714 * present in the specified DIMM.
715 *
716 * @param dimm_log2_num_bits Specifies log2(number of bits) for each side of
717 * the DIMM.
718 * @param total_dram_64M_multiple Total DRAM in the system (as a multiple of
719 * 64 MB) for DIMMs < dimm_index.
720 * @param dimm_index Which DIMM pair is being processed
721 * (0..MAX_DIMM_SOCKETS_PER_CHANNEL).
722 * @return New multiple of 64 MB total DRAM in the system.
723 */
Martin Roth468d02c2019-10-23 21:44:42 -0600724static uint8_t configure_dimm_row_boundaries(struct dimm_size dimm_log2_num_bits, uint8_t total_dram_64M_multiple, unsigned int dimm_index)
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700725{
726 int i;
727
728 ASSERT(dimm_index < MAX_DIMM_SOCKETS_PER_CHANNEL);
729
730 // DIMM sides must be at least 32 MB
731 ASSERT(dimm_log2_num_bits.side1 >= 28);
732 ASSERT((dimm_log2_num_bits.side2 == 0)
733 || (dimm_log2_num_bits.side2 >= 28));
734
735 // In dual-channel mode, we are called only once for each pair of DIMMs.
736 // Each time we process twice the capacity of a single DIMM.
737
738 // Convert single DIMM capacity to paired DIMM capacity
739 // (multiply by two ==> add 1 to log2)
740 dimm_log2_num_bits.side1++;
741 if (dimm_log2_num_bits.side2 > 0)
742 dimm_log2_num_bits.side2++;
743
744 // Add the capacity of side 1 this DIMM pair (as a multiple of 64 MB)
745 // to the total capacity of the system
746 // NOTE: 64 MB == 512 Mb, and log2(512 Mb) == 29
747
748 total_dram_64M_multiple += (1 << (dimm_log2_num_bits.side1 - 29));
749
750 // Configure the boundary address for the row on side 1
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300751 pci_write_config8(MCHDEV, DRB_ROW_0 + (dimm_index << 1),
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700752 total_dram_64M_multiple);
753
754 // If the DIMMs are double-sided, add the capacity of side 2 this DIMM pair
755 // (as a multiple of 64 MB) to the total capacity of the system
756 if (dimm_log2_num_bits.side2 >= 29)
757 total_dram_64M_multiple +=
758 (1 << (dimm_log2_num_bits.side2 - 29));
759
760 // Configure the boundary address for the row (if any) on side 2
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300761 pci_write_config8(MCHDEV, DRB_ROW_1 + (dimm_index << 1),
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700762 total_dram_64M_multiple);
763
764 // Update boundaries for rows subsequent to these.
765 // These settings will be overridden by a subsequent call if a populated physical slot exists
766
767 for (i = dimm_index + 1; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300768 pci_write_config8(MCHDEV, DRB_ROW_0 + (i << 1),
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700769 total_dram_64M_multiple);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300770 pci_write_config8(MCHDEV, DRB_ROW_1 + (i << 1),
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700771 total_dram_64M_multiple);
772 }
773
774 return total_dram_64M_multiple;
775}
776
777/**
778 * Set the E7501's DRAM row boundary addresses & its Top Of Low Memory (TOLM).
779 *
780 * If necessary, set up a remap window so we don't waste DRAM that ordinarily
781 * would lie behind addresses reserved for memory-mapped I/O.
782 *
783 * @param ctrl PCI addresses of memory controller functions, and SMBus
784 * addresses of DIMM slots on the mainboard.
785 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
786 */
787static void configure_e7501_ram_addresses(const struct mem_controller
788 *ctrl, uint8_t dimm_mask)
789{
790 int i;
791 uint8_t total_dram_64M_multiple = 0;
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300792 uint64_t tolm, tom;
793 uint16_t reg;
794
Kyösti Mälkki4c0e2772018-05-17 14:16:03 +0300795 /* FIXME: Is there standard presence detect bit somewhere. */
796 const int agp_slot_disabled = 1;
797
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300798 /* Start with disabled remap range. */
799 uint16_t remapbase_r = 0x3ff;
800 uint16_t remaplimit_r = 0;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700801
802 // Configure the E7501's DRAM row boundaries
803 // Start by zeroing out the temporary initial configuration
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300804 pci_write_config32(MCHDEV, DRB_ROW_0, 0);
805 pci_write_config32(MCHDEV, DRB_ROW_4, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700806
807 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
808
809 uint16_t dimm_socket_address = ctrl->channel0[i];
810 struct dimm_size sz;
811
812 if (!(dimm_mask & (1 << i)))
813 continue; // This DIMM not present
814
815 sz = spd_get_dimm_size(dimm_socket_address);
816
817 RAM_DEBUG_MESSAGE("dimm size =");
818 RAM_DEBUG_HEX32((u32)sz.side1);
819 RAM_DEBUG_MESSAGE(" ");
820 RAM_DEBUG_HEX32((u32)sz.side2);
821 RAM_DEBUG_MESSAGE("\n");
822
823 if (sz.side1 == 0)
824 die("Bad SPD value\n");
825
826 total_dram_64M_multiple =
827 configure_dimm_row_boundaries(sz, total_dram_64M_multiple, i);
828 }
829
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300830 tom = total_dram_64M_multiple * 64ULL * MiB;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700831
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300832 /* Reserve MMIO space. */
Kyösti Mälkki4c0e2772018-05-17 14:16:03 +0300833 tolm = 4ULL * GiB - 512 * MiB;
834 if (agp_slot_disabled) {
835 /* Reduce apertures to 2 x 4 MiB. */
836 pci_write_config8(MCHDEV, APSIZE, 0x3F);
837 pci_write_config16(AGPDEV, APSIZE1, 0x3F);
838 } else {
839 /* Add MMIO reserve for 2 x 256 MiB apertures. */
840 tolm -= 512 * MiB;
841 }
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300842 tolm = MIN(tolm, tom);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700843
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300844 /* The PCI memory hole overlaps memory setup the remap window. */
845 if (tolm < tom) {
846 uint64_t remapbase = MAX(tom, 4ULL * GiB);
847 uint64_t remaplimit = remapbase + (4ULL * GiB - tolm);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700848
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300849 remapbase_r = remapbase / (64 * MiB);
850 remaplimit_r = remaplimit / (64 * MiB);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700851
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300852 /* Limit register is inclusive. */
853 remaplimit_r -= 1;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700854 }
Kyösti Mälkki717b6e32018-05-17 14:16:03 +0300855
856 /* Write the RAM configuration registers,
857 preserving the reserved bits. */
858 reg = pci_read_config16(MCHDEV, TOLM) & 0x7ff;
859 reg |= (tolm / (128 * MiB)) << 11;
860 pci_write_config16(MCHDEV, TOLM, reg);
861
862 reg = pci_read_config16(MCHDEV, REMAPBASE) & 0xfc00;
863 reg |= remapbase_r;
864 pci_write_config16(MCHDEV, REMAPBASE, reg);
865
866 reg = pci_read_config16(MCHDEV, REMAPLIMIT) & 0xfc00;
867 reg |= remaplimit_r;
868 pci_write_config16(MCHDEV, REMAPLIMIT, reg);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700869}
870
871/**
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700872 * Program the DRAM Timing register (DRT) of the E7501 (except for CAS#
873 * latency, which is assumed to have been programmed already), based on the
874 * parameters of the various installed DIMMs.
875 *
876 * @param ctrl PCI addresses of memory controller functions, and SMBus
877 * addresses of DIMM slots on the mainboard.
878 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
879 */
880static void configure_e7501_dram_timing(const struct mem_controller *ctrl,
881 uint8_t dimm_mask)
882{
883 int i;
884 uint32_t dram_timing;
885 int value;
886 uint8_t slowest_row_precharge = 0;
887 uint8_t slowest_ras_cas_delay = 0;
888 uint8_t slowest_active_to_precharge_delay = 0;
889 uint32_t current_cas_latency =
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300890 pci_read_config32(MCHDEV, DRT) & DRT_CAS_MASK;
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700891
892 // CAS# latency must be programmed beforehand
893 ASSERT((current_cas_latency == DRT_CAS_2_0)
894 || (current_cas_latency == DRT_CAS_2_5));
895
896 // Each timing parameter is determined by the slowest DIMM
897
898 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
899 uint16_t dimm_socket_address;
900
901 if (!(dimm_mask & (1 << i)))
902 continue; // This DIMM not present
903
904 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
905 dimm_socket_address = ctrl->channel0[i];
906 else
907 dimm_socket_address =
908 ctrl->channel1[i - MAX_DIMM_SOCKETS_PER_CHANNEL];
909
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200910 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700911 SPD_MIN_ROW_PRECHARGE_TIME);
912 if (value < 0)
913 goto hw_err;
914 if (value > slowest_row_precharge)
915 slowest_row_precharge = value;
916
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200917 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700918 SPD_MIN_RAS_TO_CAS_DELAY);
919 if (value < 0)
920 goto hw_err;
921 if (value > slowest_ras_cas_delay)
922 slowest_ras_cas_delay = value;
923
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +0200924 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700925 SPD_MIN_ACTIVE_TO_PRECHARGE_DELAY);
926 if (value < 0)
927 goto hw_err;
928 if (value > slowest_active_to_precharge_delay)
929 slowest_active_to_precharge_delay = value;
930 }
931
932 // NOTE for timing parameters:
933 // At 133 MHz, 1 clock == 7.52 ns
934
935 /* Read the initial state */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300936 dram_timing = pci_read_config32(MCHDEV, DRT);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700937
938 /* Trp */
939
940 // E7501 supports only 2 or 3 clocks for tRP
941 if (slowest_row_precharge > ((22 << 2) | (2 << 0)))
942 die("unsupported DIMM tRP"); // > 22.5 ns: 4 or more clocks
943 else if (slowest_row_precharge > (15 << 2))
944 dram_timing &= ~(1 << 0); // > 15.0 ns: 3 clocks
945 else
946 dram_timing |= (1 << 0); // <= 15.0 ns: 2 clocks
947
948 /* Trcd */
949
950 // E7501 supports only 2 or 3 clocks for tRCD
951 // Use the same value for both read & write
952 dram_timing &= ~((1 << 3) | (3 << 1));
953 if (slowest_ras_cas_delay > ((22 << 2) | (2 << 0)))
954 die("unsupported DIMM tRCD"); // > 22.5 ns: 4 or more clocks
955 else if (slowest_ras_cas_delay > (15 << 2))
956 dram_timing |= (2 << 1); // > 15.0 ns: 3 clocks
957 else
958 dram_timing |= ((1 << 3) | (3 << 1)); // <= 15.0 ns: 2 clocks
959
960 /* Tras */
961
962 // E7501 supports only 5, 6, or 7 clocks for tRAS
963 // 5 clocks ~= 37.6 ns, 6 clocks ~= 45.1 ns, 7 clocks ~= 52.6 ns
964 dram_timing &= ~(3 << 9);
965
966 if (slowest_active_to_precharge_delay > 52)
967 die("unsupported DIMM tRAS"); // > 52 ns: 8 or more clocks
968 else if (slowest_active_to_precharge_delay > 45)
969 dram_timing |= (0 << 9); // 46-52 ns: 7 clocks
970 else if (slowest_active_to_precharge_delay > 37)
971 dram_timing |= (1 << 9); // 38-45 ns: 6 clocks
972 else
973 dram_timing |= (2 << 9); // < 38 ns: 5 clocks
974
975 /* Trd */
976
Elyes HAOUAS0f92f632014-07-27 19:37:31 +0200977 /* Set to a 7 clock read delay. This is for 133MHz
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700978 * with a CAS latency of 2.5 if 2.0 a 6 clock
979 * delay is good */
980
981 dram_timing &= ~(7 << 24); // 7 clocks
982 if (current_cas_latency == DRT_CAS_2_0)
983 dram_timing |= (1 << 24); // 6 clocks
984
985 /*
986 * Back to Back Read-Write Turn Around
987 */
988 /* Set to a 5 clock back to back read to write turn around.
989 * 4 is a good delay if the CAS latency is 2.0 */
990
991 dram_timing &= ~(1 << 28); // 5 clocks
992 if (current_cas_latency == DRT_CAS_2_0)
993 dram_timing |= (1 << 28); // 4 clocks
994
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +0300995 pci_write_config32(MCHDEV, DRT, dram_timing);
Stefan Reinauerb15975b2011-10-21 12:57:59 -0700996
997 return;
998
999 hw_err:
1000 die(SPD_ERROR);
1001}
1002
1003/**
1004 * Determine the shortest CAS# latency that the E7501 and all DIMMs have in
1005 * common, and program the E7501 to use it.
1006 *
1007 * @param ctrl PCI addresses of memory controller functions, and SMBus
1008 * addresses of DIMM slots on the mainboard.
1009 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1010 */
1011static void configure_e7501_cas_latency(const struct mem_controller *ctrl,
1012 uint8_t dimm_mask)
1013{
1014 int i;
1015 int value;
1016 uint32_t dram_timing;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001017 uint16_t dram_read_timing;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001018 uint32_t dword;
1019
1020 // CAS# latency bitmasks in SPD_ACCEPTABLE_CAS_LATENCIES format
1021 // NOTE: E7501 supports only 2.0 and 2.5
1022 uint32_t system_compatible_cas_latencies =
1023 SPD_CAS_LATENCY_2_0 | SPD_CAS_LATENCY_2_5;
1024 uint32_t current_cas_latency;
1025 uint32_t dimm_compatible_cas_latencies;
1026
1027 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
1028
1029 uint16_t dimm_socket_address;
1030
1031 if (!(dimm_mask & (1 << i)))
1032 continue; // This DIMM not usable
1033
1034 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
1035 dimm_socket_address = ctrl->channel0[i];
1036 else
1037 dimm_socket_address =
1038 ctrl->channel1[i - MAX_DIMM_SOCKETS_PER_CHANNEL];
1039
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +02001040 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001041 SPD_ACCEPTABLE_CAS_LATENCIES);
1042 if (value < 0)
1043 goto hw_err;
1044
1045 dimm_compatible_cas_latencies = value & 0x7f; // Start with all supported by DIMM
1046 current_cas_latency = 1 << log2(dimm_compatible_cas_latencies); // Max supported by DIMM
1047
1048 // Can we support the highest CAS# latency?
1049
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +02001050 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001051 SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
1052 if (value < 0)
1053 goto hw_err;
1054
1055 // NOTE: At 133 MHz, 1 clock == 7.52 ns
1056 if (value > 0x75) {
1057 // Our bus is too fast for this CAS# latency
1058 // Remove it from the bitmask of those supported by the DIMM that are compatible
1059 dimm_compatible_cas_latencies &= ~current_cas_latency;
1060 }
1061 // Can we support the next-highest CAS# latency (max - 0.5)?
1062
1063 current_cas_latency >>= 1;
1064 if (current_cas_latency != 0) {
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +02001065 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001066 SPD_SDRAM_CYCLE_TIME_2ND);
1067 if (value < 0)
1068 goto hw_err;
1069 if (value > 0x75)
1070 dimm_compatible_cas_latencies &=
1071 ~current_cas_latency;
1072 }
1073 // Can we support the next-highest CAS# latency (max - 1.0)?
1074 current_cas_latency >>= 1;
1075 if (current_cas_latency != 0) {
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +02001076 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001077 SPD_SDRAM_CYCLE_TIME_3RD);
1078 if (value < 0)
1079 goto hw_err;
1080 if (value > 0x75)
1081 dimm_compatible_cas_latencies &=
1082 ~current_cas_latency;
1083 }
1084 // Restrict the system to CAS# latencies compatible with this DIMM
1085 system_compatible_cas_latencies &=
1086 dimm_compatible_cas_latencies;
1087
1088 /* go to the next DIMM */
1089 }
1090
1091 /* After all of the arduous calculation setup with the fastest
1092 * cas latency I can use.
1093 */
1094
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001095 dram_timing = pci_read_config32(MCHDEV, DRT);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001096 dram_timing &= ~(DRT_CAS_MASK);
1097
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001098 dram_read_timing =
1099 pci_read_config16(MCHDEV, DRDCTL);
1100 dram_read_timing &= 0xF000;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001101
1102 if (system_compatible_cas_latencies & SPD_CAS_LATENCY_2_0) {
1103 dram_timing |= DRT_CAS_2_0;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001104 dram_read_timing |= 0x0222;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001105 } else if (system_compatible_cas_latencies & SPD_CAS_LATENCY_2_5) {
1106
1107 uint32_t dram_row_attributes =
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001108 pci_read_config32(MCHDEV, DRA);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001109
1110 dram_timing |= DRT_CAS_2_5;
1111
1112 // At CAS# 2.5, DRAM Read Timing (if that's what it its) appears to need a slightly
1113 // different value if all DIMM slots are populated
1114
1115 if ((dram_row_attributes & 0xff)
1116 && (dram_row_attributes & 0xff00)
1117 && (dram_row_attributes & 0xff0000)
1118 && (dram_row_attributes & 0xff000000)) {
1119
1120 // All slots populated
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001121 dram_read_timing |= 0x0882;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001122 } else {
1123 // Some unpopulated slots
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001124 dram_read_timing |= 0x0662;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001125 }
1126 } else
1127 die("No CAS# latencies compatible with all DIMMs!!\n");
1128
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001129 pci_write_config32(MCHDEV, DRT, dram_timing);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001130
1131 /* set master DLL reset */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001132 dword = pci_read_config32(MCHDEV, 0x88);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001133 dword |= (1 << 26);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001134 pci_write_config32(MCHDEV, 0x88, dword);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001135 /* patch try register 88 is undocumented tnz */
1136 dword &= 0x0ca17fff;
1137 dword |= 0xd14a5000;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001138 pci_write_config32(MCHDEV, 0x88, dword);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001139
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001140 pci_write_config16(MCHDEV, DRDCTL,
1141 dram_read_timing);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001142
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001143 /* clear master DLL reset */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001144 dword = pci_read_config32(MCHDEV, 0x88);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001145 dword &= ~(1 << 26);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001146 pci_write_config32(MCHDEV, 0x88, dword);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001147
1148 return;
1149
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001150hw_err:
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001151 die(SPD_ERROR);
1152}
1153
1154/**
1155 * Configure the refresh interval so that we refresh no more often than
1156 * required by the "most needy" DIMM. Also disable ECC if any of the DIMMs
1157 * don't support it.
1158 *
1159 * @param ctrl PCI addresses of memory controller functions, and SMBus
1160 * addresses of DIMM slots on the mainboard.
1161 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1162 */
1163static void configure_e7501_dram_controller_mode(const struct
1164 mem_controller *ctrl,
1165 uint8_t dimm_mask)
1166{
1167 int i;
1168
1169 // Initial settings
1170 uint32_t controller_mode =
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001171 pci_read_config32(MCHDEV, DRC);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001172 uint32_t system_refresh_mode = (controller_mode >> 8) & 7;
1173
1174 // Code below assumes that most aggressive settings are in
1175 // force when we are called, either via E7501 reset defaults
1176 // or by sdram_set_registers():
1177 // - ECC enabled
1178 // - No refresh
1179
1180 ASSERT((controller_mode & (3 << 20)) == (2 << 20)); // ECC
1181 ASSERT(!(controller_mode & (7 << 8))); // Refresh
1182
1183 /* Walk through _all_ dimms and find the least-common denominator for:
1184 * - ECC support
1185 * - refresh rates
1186 */
1187
1188 for (i = 0; i < MAX_DIMM_SOCKETS; i++) {
1189
1190 uint32_t dimm_refresh_mode;
1191 int value;
1192 uint16_t dimm_socket_address;
1193
1194 if (!(dimm_mask & (1 << i))) {
1195 continue; // This DIMM not usable
1196 }
1197
1198 if (i < MAX_DIMM_SOCKETS_PER_CHANNEL)
1199 dimm_socket_address = ctrl->channel0[i];
1200 else
1201 dimm_socket_address =
1202 ctrl->channel1[i -
1203 MAX_DIMM_SOCKETS_PER_CHANNEL];
1204
1205 // Disable ECC mode if any one of the DIMMs does not support ECC
1206 // SJM: Should we just die here? E7501 datasheet says non-ECC DIMMs aren't supported.
1207
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +02001208 value = smbus_read_byte(dimm_socket_address,
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001209 SPD_DIMM_CONFIG_TYPE);
1210 die_on_spd_error(value);
1211 if (value != ERROR_SCHEME_ECC) {
1212 controller_mode &= ~(3 << 20);
1213 }
1214
Kyösti Mälkki9e581ec2020-01-07 11:16:35 +02001215 value = smbus_read_byte(dimm_socket_address, SPD_REFRESH);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001216 die_on_spd_error(value);
1217 value &= 0x7f; // Mask off self-refresh bit
1218 if (value > MAX_SPD_REFRESH_RATE) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -08001219 printk(BIOS_ERR, "unsupported refresh rate\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001220 continue;
1221 }
1222 // Get the appropriate E7501 refresh mode for this DIMM
1223 dimm_refresh_mode = refresh_rate_map[value];
1224 if (dimm_refresh_mode > 7) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -08001225 printk(BIOS_ERR, "unsupported refresh rate\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001226 continue;
1227 }
1228 // If this DIMM requires more frequent refresh than others,
1229 // update the system setting
1230 if (refresh_frequency[dimm_refresh_mode] >
1231 refresh_frequency[system_refresh_mode])
1232 system_refresh_mode = dimm_refresh_mode;
1233
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001234 /* go to the next DIMM */
1235 }
1236
1237 controller_mode |= (system_refresh_mode << 8);
1238
1239 // Configure the E7501
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001240 pci_write_config32(MCHDEV, DRC, controller_mode);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001241}
1242
1243/**
1244 * Configure the E7501's DRAM Row Attributes (DRA) registers based on DIMM
1245 * parameters read via SPD. This tells the controller the width of the SDRAM
1246 * chips on each DIMM side (x4 or x8) and the page size of each DIMM side
1247 * (4, 8, 16, or 32 KB).
1248 *
1249 * @param ctrl PCI addresses of memory controller functions, and SMBus
1250 * addresses of DIMM slots on the mainboard.
1251 * @param dimm_mask Bitmask of populated DIMMs, spd_get_supported_dimms().
1252 */
1253static void configure_e7501_row_attributes(const struct mem_controller
1254 *ctrl, uint8_t dimm_mask)
1255{
1256 int i;
1257 uint32_t row_attributes = 0;
1258
1259 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
1260
1261 uint16_t dimm_socket_address = ctrl->channel0[i];
1262 struct dimm_size page_size;
1263 struct dimm_size sdram_width;
1264
1265 if (!(dimm_mask & (1 << i)))
1266 continue; // This DIMM not usable
1267
1268 // Get the relevant parameters via SPD
1269 page_size = sdram_spd_get_page_size(dimm_socket_address);
1270 sdram_width = sdram_spd_get_width(dimm_socket_address);
1271
1272 // Update the DRAM Row Attributes.
1273 // Page size is encoded as log2(page size in bits) - log2(8 Kb)
1274 // NOTE: 8 Kb = 2^13
1275 row_attributes |= (page_size.side1 - 13) << (i << 3); // Side 1 of each DIMM is an EVEN row
1276
1277 if (sdram_width.side2 > 0)
1278 row_attributes |= (page_size.side2 - 13) << ((i << 3) + 4); // Side 2 is ODD
1279
1280 // Set x4 flags if appropriate
1281 if (sdram_width.side1 == 4) {
1282 row_attributes |= 0x08 << (i << 3);
1283 }
1284
1285 if (sdram_width.side2 == 4) {
1286 row_attributes |= 0x08 << ((i << 3) + 4);
1287 }
1288
1289 /* go to the next DIMM */
1290 }
1291
1292 /* Write the new row attributes register */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001293 pci_write_config32(MCHDEV, DRA, row_attributes);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001294}
1295
1296/*
1297 * Enable clock signals for populated DIMM sockets and disable them for
1298 * unpopulated sockets (to reduce EMI).
1299 *
1300 * @param dimm_mask Bitmask of populated DIMMs, see spd_get_supported_dimms().
1301 */
1302static void enable_e7501_clocks(uint8_t dimm_mask)
1303{
1304 int i;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001305 uint8_t clock_disable = pci_read_config8(MCHDEV, CKDIS);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001306
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001307 pci_write_config8(MCHDEV, 0x8e, 0xb0);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001308
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001309 for (i = 0; i < MAX_DIMM_SOCKETS_PER_CHANNEL; i++) {
1310
1311 uint8_t socket_mask = 1 << i;
1312
1313 if (dimm_mask & socket_mask)
1314 clock_disable &= ~socket_mask; // DIMM present, enable clock
1315 else
1316 clock_disable |= socket_mask; // DIMM absent, disable clock
1317 }
1318
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001319 pci_write_config8(MCHDEV, CKDIS, clock_disable);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001320}
1321
Martin Roth128c1042016-11-18 09:29:03 -07001322/* DIMM-dependent configuration functions */
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001323
1324/**
1325 * DDR Receive FIFO RE-Sync (?)
1326 */
1327static void RAM_RESET_DDR_PTR(void)
1328{
1329 uint8_t byte;
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001330 byte = pci_read_config8(MCHDEV, 0x88);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001331 byte |= (1 << 4);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001332 pci_write_config8(MCHDEV, 0x88, byte);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001333
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001334 byte = pci_read_config8(MCHDEV, 0x88);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001335 byte &= ~(1 << 4);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001336 pci_write_config8(MCHDEV, 0x88, byte);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001337}
1338
1339/**
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001340 * Copy 64 bytes from one location to another.
1341 *
1342 * @param src_addr TODO
1343 * @param dst_addr TODO
1344 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001345static void write_8dwords(const uint32_t *src_addr, u8 *dst_addr)
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001346{
1347 int i;
1348 for (i = 0; i < 8; i++) {
1349 write32(dst_addr, *src_addr);
1350 src_addr++;
1351 dst_addr += sizeof(uint32_t);
1352 }
1353}
1354
1355/**
1356 * Set the E7501's (undocumented) RCOMP registers.
1357 *
1358 * Per the 855PM datasheet and IXP2800 HW Initialization Reference Manual,
1359 * RCOMP registers appear to affect drive strength, pullup/pulldown offset,
1360 * and slew rate of various signal groups.
1361 *
1362 * Comments below are conjecture based on apparent similarity between the
1363 * E7501 and these two chips.
1364 */
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001365static void rcomp_copy_registers(void)
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001366{
1367 uint32_t dword;
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001368 uint8_t strength_control;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001369
1370 RAM_DEBUG_MESSAGE("Setting RCOMP registers.\n");
1371
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001372 /* Begin to write the RCOMP registers */
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001373 write8(RCOMP_MMIO + 0x2c, 0x0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001374
1375 // Set CMD and DQ/DQS strength to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001376 strength_control = read8(RCOMP_MMIO + DQCMDSTR) & 0x88;
1377 strength_control |= 0x40;
1378 write8(RCOMP_MMIO + DQCMDSTR, strength_control);
1379 write_8dwords(slew_2x, RCOMP_MMIO + 0x80);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001380 write16(RCOMP_MMIO + 0x42, 0);
1381
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001382 // Set CMD and DQ/DQS strength to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001383 strength_control = read8(RCOMP_MMIO + DQCMDSTR) & 0xF8;
1384 strength_control |= 0x04;
1385 write8(RCOMP_MMIO + DQCMDSTR, strength_control);
1386 write_8dwords(slew_2x, RCOMP_MMIO + 0x60);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001387 write16(RCOMP_MMIO + 0x40, 0);
1388
1389 // Set RCVEnOut# strength to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001390 strength_control = read8(RCOMP_MMIO + RCVENSTR) & 0xF8;
1391 strength_control |= 0x04;
1392 write8(RCOMP_MMIO + RCVENSTR, strength_control);
1393 write_8dwords(slew_2x, RCOMP_MMIO + 0x1c0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001394 write16(RCOMP_MMIO + 0x50, 0);
1395
1396 // Set CS# strength for x4 SDRAM to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001397 strength_control = read8(RCOMP_MMIO + CSBSTR) & 0x88;
1398 strength_control |= 0x04;
1399 write8(RCOMP_MMIO + CSBSTR, strength_control);
1400 write_8dwords(slew_2x, RCOMP_MMIO + 0x140);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001401 write16(RCOMP_MMIO + 0x48, 0);
1402
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001403 // Set CS# strength for x4 SDRAM to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001404 strength_control = read8(RCOMP_MMIO + CSBSTR) & 0x8F;
1405 strength_control |= 0x40;
1406 write8(RCOMP_MMIO + CSBSTR, strength_control);
1407 write_8dwords(slew_2x, RCOMP_MMIO + 0x160);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001408 write16(RCOMP_MMIO + 0x4a, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001409
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001410 // Set CKE strength for x4 SDRAM to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001411 strength_control = read8(RCOMP_MMIO + CKESTR) & 0x88;
1412 strength_control |= 0x04;
1413 write8(RCOMP_MMIO + CKESTR, strength_control);
1414 write_8dwords(slew_2x, RCOMP_MMIO + 0xa0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001415 write16(RCOMP_MMIO + 0x44, 0);
1416
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001417 // Set CKE strength for x4 SDRAM to 2x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001418 strength_control = read8(RCOMP_MMIO + CKESTR) & 0x8F;
1419 strength_control |= 0x40;
1420 write8(RCOMP_MMIO + CKESTR, strength_control);
1421 write_8dwords(slew_2x, RCOMP_MMIO + 0xc0);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001422 write16(RCOMP_MMIO + 0x46, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001423
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001424 // Set CK strength for x4 SDRAM to 1x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001425 strength_control = read8(RCOMP_MMIO + CKSTR) & 0x88;
1426 strength_control |= 0x01;
1427 write8(RCOMP_MMIO + CKSTR, strength_control);
1428 write_8dwords(pull_updown_offset_table, RCOMP_MMIO + 0x180);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001429 write16(RCOMP_MMIO + 0x4c, 0);
1430
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001431 // Set CK strength for x4 SDRAM to 1x (?)
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001432 strength_control = read8(RCOMP_MMIO + CKSTR) & 0x8F;
1433 strength_control |= 0x10;
1434 write8(RCOMP_MMIO + CKSTR, strength_control);
1435 write_8dwords(pull_updown_offset_table, RCOMP_MMIO + 0x1a0);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001436 write16(RCOMP_MMIO + 0x4e, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001437
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001438 dword = read32(RCOMP_MMIO + 0x400);
1439 dword &= 0x7f7fffff;
1440 write32(RCOMP_MMIO + 0x400, dword);
1441
1442 dword = read32(RCOMP_MMIO + 0x408);
1443 dword &= 0x7f7fffff;
1444 write32(RCOMP_MMIO + 0x408, dword);
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001445}
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001446
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001447static void ram_set_rcomp_regs(void)
1448{
1449 /* Set the RCOMP MMIO base address */
1450 mchtest_control(RCOMP_BAR_ENABLE);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001451 pci_write_config32(MCHDEV, SMRBASE, (uintptr_t)RCOMP_MMIO);
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001452
1453 /* Block RCOMP updates while we configure the registers */
1454 rcomp_smr_control(RCOMP_HOLD);
1455 rcomp_copy_registers();
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001456 d060_control(D060_CMD_0);
1457 mchtest_control(MCHTST_CMD_0);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001458
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001459 uint8_t revision = pci_read_config8(MCHDEV, 0x08);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001460 if (revision >= 3) {
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001461 rcomp_smr_control(RCOMP_SMR_00);
1462 rcomp_smr_control(RCOMP_SMR_01);
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001463 }
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001464 rcomp_smr_control(RCOMP_RELEASE);
1465
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001466 /* Wait 40 usec */
1467 SLOW_DOWN_IO;
1468
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001469 /* Clear the RCOMP MMIO base address */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001470 pci_write_config32(MCHDEV, SMRBASE, 0);
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001471 mchtest_control(RCOMP_BAR_DISABLE);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001472}
1473
1474/*-----------------------------------------------------------------------------
1475Public interface:
1476-----------------------------------------------------------------------------*/
1477
1478/**
1479 * Go through the JEDEC initialization sequence for all DIMMs, then enable
1480 * refresh and initialize ECC and memory to zero. Upon exit, SDRAM is up
1481 * and running.
1482 *
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001483 * @param ctrl PCI addresses of memory controller functions, and SMBus
1484 * addresses of DIMM slots on the mainboard.
1485 */
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001486static void sdram_enable(const struct mem_controller *ctrl)
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001487{
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001488 uint8_t dimm_mask = pci_read_config16(MCHDEV, SKPD);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001489 uint32_t dram_controller_mode;
1490
1491 if (dimm_mask == 0)
1492 return;
1493
1494 /* 1 & 2 Power up and start clocks */
1495 RAM_DEBUG_MESSAGE("Ram Enable 1\n");
1496 RAM_DEBUG_MESSAGE("Ram Enable 2\n");
1497
1498 /* A 200us delay is needed */
1499 DO_DELAY; EXTRA_DELAY;
1500
1501 /* 3. Apply NOP */
1502 RAM_DEBUG_MESSAGE("Ram Enable 3\n");
1503 do_ram_command(RAM_COMMAND_NOP, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001504
1505 /* 4 Precharge all */
1506 RAM_DEBUG_MESSAGE("Ram Enable 4\n");
1507 do_ram_command(RAM_COMMAND_PRECHARGE, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001508 /* wait until the all banks idle state... */
1509
1510 /* 5. Issue EMRS to enable DLL */
1511 RAM_DEBUG_MESSAGE("Ram Enable 5\n");
1512 do_ram_command(RAM_COMMAND_EMRS,
1513 SDRAM_EXTMODE_DLL_ENABLE |
1514 SDRAM_EXTMODE_DRIVE_NORMAL);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001515
1516 /* 6. Reset DLL */
1517 RAM_DEBUG_MESSAGE("Ram Enable 6\n");
1518 set_ram_mode(E7501_SDRAM_MODE | SDRAM_MODE_DLL_RESET);
1519 EXTRA_DELAY;
1520 /* Ensure a 200us delay between the DLL reset in step 6 and the final
1521 * mode register set in step 9.
1522 * Infineon needs this before any other command is sent to the ram.
1523 */
1524 DO_DELAY; EXTRA_DELAY;
1525
1526 /* 7 Precharge all */
1527 RAM_DEBUG_MESSAGE("Ram Enable 7\n");
1528 do_ram_command(RAM_COMMAND_PRECHARGE, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001529
1530 /* 8 Now we need 2 AUTO REFRESH / CBR cycles to be performed */
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001531 /* And for good luck 6 more CBRs */
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001532 RAM_DEBUG_MESSAGE("Ram Enable 8\n");
1533 int i;
Elyes HAOUAS93095522016-09-17 21:05:10 +02001534 for (i = 0; i < 8; i++)
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001535 do_ram_command(RAM_COMMAND_CBR, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001536
1537 /* 9 mode register set */
1538 RAM_DEBUG_MESSAGE("Ram Enable 9\n");
1539 set_ram_mode(E7501_SDRAM_MODE | SDRAM_MODE_NORMAL);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001540
1541 /* 10 DDR Receive FIFO RE-Sync */
1542 RAM_DEBUG_MESSAGE("Ram Enable 10\n");
1543 RAM_RESET_DDR_PTR();
1544 EXTRA_DELAY;
1545
1546 /* 11 normal operation */
1547 RAM_DEBUG_MESSAGE("Ram Enable 11\n");
1548 do_ram_command(RAM_COMMAND_NORMAL, 0);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001549
1550 // Reconfigure the row boundaries and Top of Low Memory
1551 // to match the true size of the DIMMs
1552 configure_e7501_ram_addresses(ctrl, dimm_mask);
1553
1554 /* Finally enable refresh */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001555 dram_controller_mode = pci_read_config32(MCHDEV, DRC);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001556 dram_controller_mode |= (1 << 29);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001557 pci_write_config32(MCHDEV, DRC, dram_controller_mode);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001558 EXTRA_DELAY;
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001559}
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001560
Kyösti Mälkki97c064f2012-04-18 20:33:35 +03001561/**
1562 * @param ctrl PCI addresses of memory controller functions, and SMBus
1563 * addresses of DIMM slots on the mainboard.
1564 */
1565static void sdram_post_ecc(const struct mem_controller *ctrl)
1566{
1567 /* Fast CS# Enable. */
1568 uint32_t dram_controller_mode = pci_read_config32(MCHDEV, DRC);
1569 dram_controller_mode = pci_read_config32(MCHDEV, DRC);
1570 dram_controller_mode |= (1 << 17);
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001571 pci_write_config32(MCHDEV, DRC, dram_controller_mode);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001572}
1573
1574/**
1575 * Configure SDRAM controller parameters that depend on characteristics of the
1576 * DIMMs installed in the system. These characteristics are read from the
1577 * DIMMs via the standard Serial Presence Detect (SPD) interface.
1578 *
1579 * @param ctrl PCI addresses of memory controller functions, and SMBus
1580 * addresses of DIMM slots on the mainboard.
1581 */
1582static void sdram_set_spd_registers(const struct mem_controller *ctrl)
1583{
1584 uint8_t dimm_mask;
1585
1586 RAM_DEBUG_MESSAGE("Reading SPD data...\n");
1587
1588 dimm_mask = spd_get_supported_dimms(ctrl);
1589
1590 if (dimm_mask == 0) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -08001591 printk(BIOS_DEBUG, "No usable memory for this controller\n");
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001592 } else {
1593 enable_e7501_clocks(dimm_mask);
1594
1595 RAM_DEBUG_MESSAGE("setting based on SPD data...\n");
1596
1597 configure_e7501_row_attributes(ctrl, dimm_mask);
1598 configure_e7501_dram_controller_mode(ctrl, dimm_mask);
1599 configure_e7501_cas_latency(ctrl, dimm_mask);
1600 RAM_RESET_DDR_PTR();
1601
1602 configure_e7501_dram_timing(ctrl, dimm_mask);
1603 DO_DELAY;
1604 RAM_DEBUG_MESSAGE("done\n");
1605 }
1606
1607 /* NOTE: configure_e7501_ram_addresses() is NOT called here.
1608 * We want to keep the default 64 MB/row mapping until sdram_enable() is called,
1609 * even though the default mapping is almost certainly incorrect.
1610 * The default mapping makes it easy to initialize all of the DIMMs
1611 * even if the total system memory is > 4 GB.
1612 *
1613 * Save the dimm_mask for when sdram_enable is called, so it can call
1614 * configure_e7501_ram_addresses() without having to regenerate the bitmask
1615 * of usable DIMMs.
1616 */
Kyösti Mälkki5bd271b2012-04-10 16:11:53 +03001617 pci_write_config16(MCHDEV, SKPD, dimm_mask);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001618}
1619
1620/**
1621 * Do basic RAM setup that does NOT depend on serial presence detect
1622 * information (i.e. independent of DIMM specifics).
1623 *
1624 * @param ctrl PCI addresses of memory controller functions, and SMBus
1625 * addresses of DIMM slots on the mainboard.
1626 */
1627static void sdram_set_registers(const struct mem_controller *ctrl)
1628{
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001629 uint32_t dword;
1630 uint16_t word;
1631 uint8_t byte;
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001632
1633 ram_set_rcomp_regs();
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001634
1635 /* Enable 0:0.1, 0:2.1 */
1636 word = pci_read_config16(MCHDEV, DVNP);
1637 word &= ~0x05;
1638 pci_write_config16(MCHDEV, DVNP, word);
1639
1640 /* Disable high-memory remap (power-on defaults, really) */
1641 pci_write_config16(MCHDEV, REMAPBASE, 0x03ff);
1642 pci_write_config16(MCHDEV, REMAPLIMIT, 0x0);
1643
1644 /* Disable legacy MMIO (0xC0000-0xEFFFF is DRAM) */
1645 int i;
1646 pci_write_config8(MCHDEV, PAM_0, 0x30);
Elyes HAOUAS93095522016-09-17 21:05:10 +02001647 for (i = 1; i <= 6; i++)
Kyösti Mälkki77e4f7d2012-04-18 19:47:56 +03001648 pci_write_config8(MCHDEV, PAM_0 + i, 0x33);
1649
1650 /* Conservatively say each row has 64MB of ram, we will fix this up later
1651 * Initial TOLM 8 rows 64MB each (1<<3 * 1<<26) >> 16 = 1<<13
1652 *
1653 * FIXME: Hard-coded limit to first four rows to prevent overlap!
1654 */
1655 pci_write_config32(MCHDEV, DRB_ROW_0, 0x04030201);
1656 pci_write_config32(MCHDEV, DRB_ROW_4, 0x04040404);
1657 //pci_write_config32(MCHDEV, DRB_ROW_4, 0x08070605);
1658 pci_write_config16(MCHDEV, TOLM, (1<<13));
1659
1660 /* DIMM clocks off */
1661 pci_write_config8(MCHDEV, CKDIS, 0xff);
1662
1663 /* reset row attributes */
1664 pci_write_config32(MCHDEV, DRA, 0x0);
1665
1666 // The only things we need to set here are DRAM idle timer, Back-to-Back Read Turnaround, and
1667 // Back-to-Back Write-Read Turnaround. All others are configured based on SPD.
1668 dword = pci_read_config32(MCHDEV, DRT);
1669 dword &= 0xC7F8FFFF;
1670 dword |= (0x28<<24)|(0x03<<16);
1671 pci_write_config32(MCHDEV, DRT, dword);
1672
1673 dword = pci_read_config32(MCHDEV, DRC);
1674 dword &= 0xffcef8f7;
1675 dword |= 0x00210008;
1676 pci_write_config32(MCHDEV, DRC, dword);
1677
1678 /* Undocumented */
1679 pci_write_config8(MCHDEV, 0x88, 0x80);
1680
1681 /* Undocumented. Set much later in vendor BIOS. */
1682 byte = pci_read_config8(MCHDEV, 0xd9);
1683 byte &= ~0x60;
1684 pci_write_config8(MCHDEV, 0xd9, byte);
1685
Kyösti Mälkki26c7b862012-04-12 22:46:23 +03001686 uint8_t revision = pci_read_config8(MCHDEV, 0x08);
1687 if (revision >= 3)
1688 d060_control(D060_CMD_1);
Stefan Reinauerb15975b2011-10-21 12:57:59 -07001689}
Kyösti Mälkki0a0d5e82011-10-31 14:18:33 +02001690
Kyösti Mälkkid1141ab2020-01-07 11:16:35 +02001691static int e7505_mch_is_ready(void)
Kyösti Mälkki5c1ff922012-04-10 19:55:19 +03001692{
1693 uint32_t dword = pci_read_config32(MCHDEV, DRC);
1694 return !!(dword & DRC_DONE);
1695}
Kyösti Mälkkid1141ab2020-01-07 11:16:35 +02001696
1697void sdram_initialize(void)
1698{
1699 static const struct mem_controller memctrl[] = {
1700 {
1701 .d0 = PCI_DEV(0, 0, 0),
1702 .d0f1 = PCI_DEV(0, 0, 1),
1703 .channel0 = { 0x50, 0x52, 0, 0 },
1704 .channel1 = { 0x51, 0x53, 0, 0 },
1705 },
1706 };
1707
1708 /* If this is a warm boot, some initialisation can be skipped */
1709 if (!e7505_mch_is_ready()) {
1710
1711 /* The real MCH initialisation. */
1712 timestamp_add_now(TS_BEFORE_INITRAM);
1713
1714 sdram_set_registers(memctrl);
1715 sdram_set_spd_registers(memctrl);
1716 sdram_enable(memctrl);
1717
1718 /* Hook for post ECC scrub settings and debug. */
1719 sdram_post_ecc(memctrl);
1720
1721 timestamp_add_now(TS_AFTER_INITRAM);
1722 }
1723
1724 printk(BIOS_DEBUG, "SDRAM is up.\n");
1725}