blob: fbf9f01cfa0268aea35de55049c2365a6fa7b8e6 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi2efc8802012-11-06 11:03:53 +01002
Elyes HAOUASba9b5042019-12-19 07:47:52 +01003#include <commonlib/helpers.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +01004#include <stdint.h>
5#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02006#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +01008#include <device/pci_def.h>
Patrick Rudolph266a1f72016-06-09 18:13:34 +02009#include <device/device.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +010010#include <spd.h>
11#include <console/console.h>
12#include <lib.h>
Arthur Heymans10141c32016-10-27 00:31:41 +020013#include <delay.h>
Arthur Heymans049347f2017-05-12 11:54:08 +020014#include <timestamp.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +010015#include "gm45.h"
Patrick Rudolph266a1f72016-06-09 18:13:34 +020016#include "chip.h"
Patrick Georgi2efc8802012-11-06 11:03:53 +010017
18static const gmch_gfx_t gmch_gfx_types[][5] = {
19/* MAX_667MHz MAX_533MHz MAX_400MHz MAX_333MHz MAX_800MHz */
20 { GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN },
21 { GMCH_GM47, GMCH_GM45, GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_GM49 },
22 { GMCH_GE45, GMCH_GE45, GMCH_GE45, GMCH_GE45, GMCH_GE45 },
23 { GMCH_UNKNOWN, GMCH_GL43, GMCH_GL40, GMCH_UNKNOWN, GMCH_UNKNOWN },
24 { GMCH_UNKNOWN, GMCH_GS45, GMCH_GS40, GMCH_UNKNOWN, GMCH_UNKNOWN },
25 { GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN },
26 { GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN, GMCH_UNKNOWN },
27 { GMCH_PM45, GMCH_PM45, GMCH_PM45, GMCH_PM45, GMCH_PM45 },
28};
29
30void get_gmch_info(sysinfo_t *sysinfo)
31{
32 sysinfo->stepping = pci_read_config8(PCI_DEV(0, 0, 0), PCI_CLASS_REVISION);
33 if ((sysinfo->stepping > STEPPING_B3) &&
34 (sysinfo->stepping != STEPPING_CONVERSION_A1))
35 die("Unknown stepping.\n");
36 if (sysinfo->stepping <= STEPPING_B3)
37 printk(BIOS_DEBUG, "Stepping %c%d\n", 'A' + sysinfo->stepping / 4, sysinfo->stepping % 4);
38 else
39 printk(BIOS_DEBUG, "Conversion stepping A1\n");
40
41 const u32 eax = cpuid_ext(0x04, 0).eax;
42 sysinfo->cores = ((eax >> 26) & 0x3f) + 1;
43 printk(BIOS_SPEW, "%d CPU cores\n", sysinfo->cores);
44
45 u32 capid = pci_read_config16(PCI_DEV(0, 0, 0), D0F0_CAPID0+8);
46 if (!(capid & (1<<(79-64)))) {
47 printk(BIOS_SPEW, "iTPM enabled\n");
48 }
49
50 capid = pci_read_config32(PCI_DEV(0, 0, 0), D0F0_CAPID0+4);
51 if (!(capid & (1<<(57-32)))) {
52 printk(BIOS_SPEW, "ME enabled\n");
53 }
54
55 if (!(capid & (1<<(56-32)))) {
56 printk(BIOS_SPEW, "AMT enabled\n");
57 }
58
59 sysinfo->max_ddr2_mhz = (capid & (1<<(53-32)))?667:800;
60 printk(BIOS_SPEW, "capable of DDR2 of %d MHz or lower\n", sysinfo->max_ddr2_mhz);
61
62 if (!(capid & (1<<(48-32)))) {
63 printk(BIOS_SPEW, "VT-d enabled\n");
64 }
65
66 const u32 gfx_variant = (capid>>(42-32)) & 0x7;
67 const u32 render_freq = ((capid>>(50-32) & 0x1) << 2) | ((capid>>(35-32)) & 0x3);
68 if (render_freq <= 4)
69 sysinfo->gfx_type = gmch_gfx_types[gfx_variant][render_freq];
70 else
71 sysinfo->gfx_type = GMCH_UNKNOWN;
Patrick Georgi2efc8802012-11-06 11:03:53 +010072 switch (sysinfo->gfx_type) {
73 case GMCH_GM45:
74 printk(BIOS_SPEW, "GMCH: GM45\n");
75 break;
76 case GMCH_GM47:
77 printk(BIOS_SPEW, "GMCH: GM47\n");
78 break;
79 case GMCH_GM49:
80 printk(BIOS_SPEW, "GMCH: GM49\n");
81 break;
82 case GMCH_GE45:
83 printk(BIOS_SPEW, "GMCH: GE45\n");
84 break;
85 case GMCH_GL40:
86 printk(BIOS_SPEW, "GMCH: GL40\n");
87 break;
88 case GMCH_GL43:
89 printk(BIOS_SPEW, "GMCH: GL43\n");
90 break;
91 case GMCH_GS40:
92 printk(BIOS_SPEW, "GMCH: GS40\n");
93 break;
94 case GMCH_GS45:
Nico Huber5aaeb272015-12-30 00:17:27 +010095 printk(BIOS_SPEW, "GMCH: GS45, using %s-power mode\n",
96 sysinfo->gs45_low_power_mode ? "low" : "high");
Patrick Georgi2efc8802012-11-06 11:03:53 +010097 break;
98 case GMCH_PM45:
99 printk(BIOS_SPEW, "GMCH: PM45\n");
100 break;
101 case GMCH_UNKNOWN:
102 printk(BIOS_SPEW, "unknown GMCH\n");
103 break;
104 }
105
106 sysinfo->txt_enabled = !(capid & (1 << (37-32)));
107 if (sysinfo->txt_enabled) {
108 printk(BIOS_SPEW, "TXT enabled\n");
109 }
110
111 switch (render_freq) {
112 case 4:
113 sysinfo->max_render_mhz = 800;
114 break;
115 case 0:
116 sysinfo->max_render_mhz = 667;
117 break;
118 case 1:
119 sysinfo->max_render_mhz = 533;
120 break;
121 case 2:
122 sysinfo->max_render_mhz = 400;
123 break;
124 case 3:
125 sysinfo->max_render_mhz = 333;
126 break;
127 default:
128 printk(BIOS_SPEW, "Unknown render frequency\n");
129 sysinfo->max_render_mhz = 0;
130 break;
131 }
132 if (sysinfo->max_render_mhz != 0) {
133 printk(BIOS_SPEW, "Render frequency: %d MHz\n", sysinfo->max_render_mhz);
134 }
135
136 if (!(capid & (1<<(33-32)))) {
137 printk(BIOS_SPEW, "IGD enabled\n");
138 }
139
140 if (!(capid & (1<<(32-32)))) {
141 printk(BIOS_SPEW, "PCIe-to-GMCH enabled\n");
142 }
143
144 capid = pci_read_config32(PCI_DEV(0, 0, 0), D0F0_CAPID0);
145
146 u32 ddr_cap = capid>>30 & 0x3;
147 switch (ddr_cap) {
148 case 0:
149 sysinfo->max_ddr3_mt = 1067;
150 break;
151 case 1:
152 sysinfo->max_ddr3_mt = 800;
153 break;
154 case 2:
155 case 3:
156 printk(BIOS_SPEW, "GMCH not DDR3 capable\n");
157 sysinfo->max_ddr3_mt = 0;
158 break;
159 }
160 if (sysinfo->max_ddr3_mt != 0) {
161 printk(BIOS_SPEW, "GMCH supports DDR3 with %d MT or less\n", sysinfo->max_ddr3_mt);
162 }
163
Martin Roth468d02c2019-10-23 21:44:42 -0600164 const unsigned int max_fsb = (capid >> 28) & 0x3;
Patrick Georgi2efc8802012-11-06 11:03:53 +0100165 switch (max_fsb) {
166 case 1:
167 sysinfo->max_fsb_mhz = 1067;
168 break;
169 case 2:
170 sysinfo->max_fsb_mhz = 800;
171 break;
172 case 3:
173 sysinfo->max_fsb_mhz = 667;
174 break;
175 default:
176 die("unknown FSB capability\n");
177 break;
178 }
179 if (sysinfo->max_fsb_mhz != 0) {
180 printk(BIOS_SPEW, "GMCH supports FSB with up to %d MHz\n", sysinfo->max_fsb_mhz);
181 }
182 sysinfo->max_fsb = max_fsb - 1;
183}
184
185/*
186 * Detect if the system went through an interrupted RAM init or is incon-
187 * sistent. If so, initiate a cold reboot. Otherwise mark the system to be
Martin Roth128c1042016-11-18 09:29:03 -0700188 * in RAM init, so this function would detect it on an erroneous reboot.
Patrick Georgi2efc8802012-11-06 11:03:53 +0100189 */
190void enter_raminit_or_reset(void)
191{
192 /* Interrupted RAM init or inconsistent system? */
193 u8 reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xa2);
194
195 if (reg8 & (1 << 2)) { /* S4-assertion-width violation */
196 /* Ignore S4-assertion-width violation like original BIOS. */
197 printk(BIOS_WARNING,
198 "WARNING: Ignoring S4-assertion-width violation.\n");
199 /* Bit2 is R/WC, so it will clear itself below. */
200 }
201
202 if (reg8 & (1 << 7)) { /* interrupted RAM init */
203 /* Don't enable S4-assertion stretch. Makes trouble on roda/rk9.
204 reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xa4);
205 pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xa4, reg8 | 0x08);
206 */
207
208 /* Clear bit7. */
209 pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xa2, reg8 & ~(1 << 7));
210
211 printk(BIOS_INFO, "Interrupted RAM init, reset required.\n");
212 gm45_early_reset();
213 }
214 /* Mark system to be in RAM init. */
215 pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xa2, reg8 | (1 << 7));
216}
217
218
219/* For a detected DIMM, test the value of an SPD byte to
220 match the expected value after masking some bits. */
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200221static int test_dimm(sysinfo_t *const sysinfo,
222 int dimm, int addr, int bitmask, int expected)
Patrick Georgi2efc8802012-11-06 11:03:53 +0100223{
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200224 return (smbus_read_byte(sysinfo->spd_map[dimm], addr) & bitmask) == expected;
Patrick Georgi2efc8802012-11-06 11:03:53 +0100225}
226
227/* This function dies if dimm is unsuitable for the chipset. */
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200228static void verify_ddr3_dimm(sysinfo_t *const sysinfo, int dimm)
Patrick Georgi2efc8802012-11-06 11:03:53 +0100229{
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200230 if (!test_dimm(sysinfo, dimm, 3, 15, 3))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100231 die("Chipset only supports SO-DIMM\n");
232
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200233 if (!test_dimm(sysinfo, dimm, 8, 0x18, 0))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100234 die("Chipset doesn't support ECC RAM\n");
235
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200236 if (!test_dimm(sysinfo, dimm, 7, 0x38, 0) &&
237 !test_dimm(sysinfo, dimm, 7, 0x38, 8))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100238 die("Chipset wants single or double sided DIMMs\n");
239
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200240 if (!test_dimm(sysinfo, dimm, 7, 7, 1) &&
241 !test_dimm(sysinfo, dimm, 7, 7, 2))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100242 die("Chipset requires x8 or x16 width\n");
243
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200244 if (!test_dimm(sysinfo, dimm, 4, 0x0f, 0) &&
245 !test_dimm(sysinfo, dimm, 4, 0x0f, 1) &&
246 !test_dimm(sysinfo, dimm, 4, 0x0f, 2) &&
247 !test_dimm(sysinfo, dimm, 4, 0x0f, 3))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100248 die("Chipset requires 256Mb, 512Mb, 1Gb or 2Gb chips.");
249
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200250 if (!test_dimm(sysinfo, dimm, 4, 0x70, 0))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100251 die("Chipset requires 8 banks on DDR3\n");
252
253 /* How to check if burst length is 8?
254 Other values are not supported, are they even possible? */
255
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200256 if (!test_dimm(sysinfo, dimm, 10, 0xff, 1))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100257 die("Code assumes 1/8ns MTB\n");
258
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200259 if (!test_dimm(sysinfo, dimm, 11, 0xff, 8))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100260 die("Code assumes 1/8ns MTB\n");
261
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200262 if (!test_dimm(sysinfo, dimm, 62, 0x9f, 0) &&
263 !test_dimm(sysinfo, dimm, 62, 0x9f, 1) &&
264 !test_dimm(sysinfo, dimm, 62, 0x9f, 2) &&
265 !test_dimm(sysinfo, dimm, 62, 0x9f, 3) &&
266 !test_dimm(sysinfo, dimm, 62, 0x9f, 5))
Patrick Georgi2efc8802012-11-06 11:03:53 +0100267 die("Only raw card types A, B, C, D and F are supported.\n");
268}
269
270/* For every detected DIMM, test if it's suitable for the chipset. */
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200271static void verify_ddr3(sysinfo_t *const sysinfo, int mask)
Patrick Georgi2efc8802012-11-06 11:03:53 +0100272{
273 int cur = 0;
274 while (mask) {
275 if (mask & 1) {
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200276 verify_ddr3_dimm(sysinfo, cur);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100277 }
278 mask >>= 1;
279 cur++;
280 }
281}
282
283
284typedef struct {
285 int dimm_mask;
286 struct {
287 unsigned int rows;
288 unsigned int cols;
289 unsigned int chip_capacity;
290 unsigned int banks;
291 unsigned int ranks;
292 unsigned int cas_latencies;
293 unsigned int tAAmin;
294 unsigned int tCKmin;
295 unsigned int width;
296 unsigned int tRAS;
297 unsigned int tRP;
298 unsigned int tRCD;
299 unsigned int tWR;
300 unsigned int page_size;
301 unsigned int raw_card;
302 } channel[2];
303} spdinfo_t;
304/*
305 * This function collects RAM characteristics from SPD, assuming that RAM
306 * is generally within chipset's requirements, since verify_ddr3() passed.
307 */
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200308static void collect_ddr3(sysinfo_t *const sysinfo, spdinfo_t *const config)
Patrick Georgi2efc8802012-11-06 11:03:53 +0100309{
310 int mask = config->dimm_mask;
311 int cur = 0;
312 while (mask != 0) {
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200313 /* FIXME: support several dimms on same channel. */
314 if ((mask & 1) && sysinfo->spd_map[2 * cur]) {
Patrick Georgi2efc8802012-11-06 11:03:53 +0100315 int tmp;
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200316 const int smb_addr = sysinfo->spd_map[2 * cur];
Patrick Georgi2efc8802012-11-06 11:03:53 +0100317
318 config->channel[cur].rows = ((smbus_read_byte(smb_addr, 5) >> 3) & 7) + 12;
319 config->channel[cur].cols = (smbus_read_byte(smb_addr, 5) & 7) + 9;
320
321 config->channel[cur].chip_capacity = smbus_read_byte(smb_addr, 4) & 0xf;
322
323 config->channel[cur].banks = 8; /* GM45 only accepts this for DDR3.
324 verify_ddr3() fails for other values. */
325 config->channel[cur].ranks = ((smbus_read_byte(smb_addr, 7) >> 3) & 7) + 1;
326
327 config->channel[cur].cas_latencies =
328 ((smbus_read_byte(smb_addr, 15) << 8) | smbus_read_byte(smb_addr, 14))
329 << 4; /* so bit x is CAS x */
330 config->channel[cur].tAAmin = smbus_read_byte(smb_addr, 16); /* in MTB */
331 config->channel[cur].tCKmin = smbus_read_byte(smb_addr, 12); /* in MTB */
332
333 config->channel[cur].width = smbus_read_byte(smb_addr, 7) & 7;
334 config->channel[cur].page_size = config->channel[cur].width *
335 (1 << config->channel[cur].cols); /* in Bytes */
336
337 tmp = smbus_read_byte(smb_addr, 21);
338 config->channel[cur].tRAS = smbus_read_byte(smb_addr, 22) | ((tmp & 0xf) << 8);
339 config->channel[cur].tRP = smbus_read_byte(smb_addr, 20);
340 config->channel[cur].tRCD = smbus_read_byte(smb_addr, 18);
341 config->channel[cur].tWR = smbus_read_byte(smb_addr, 17);
342
343 config->channel[cur].raw_card = smbus_read_byte(smb_addr, 62) & 0x1f;
344 }
345 cur++;
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200346 mask >>= 2;
Patrick Georgi2efc8802012-11-06 11:03:53 +0100347 }
348}
349
Patrick Georgi2efc8802012-11-06 11:03:53 +0100350static fsb_clock_t read_fsb_clock(void)
351{
352 switch (MCHBAR32(CLKCFG_MCHBAR) & CLKCFG_FSBCLK_MASK) {
353 case 6:
354 return FSB_CLOCK_1067MHz;
355 case 2:
356 return FSB_CLOCK_800MHz;
357 case 3:
358 return FSB_CLOCK_667MHz;
359 default:
360 die("Unsupported FSB clock.\n");
361 }
362}
363static mem_clock_t clock_index(const unsigned int clock)
364{
365 switch (clock) {
366 case 533: return MEM_CLOCK_533MHz;
367 case 400: return MEM_CLOCK_400MHz;
368 case 333: return MEM_CLOCK_333MHz;
369 default: die("Unknown clock value.\n");
370 }
371 return -1; /* Won't be reached. */
372}
373static void normalize_clock(unsigned int *const clock)
374{
375 if (*clock >= 533)
376 *clock = 533;
377 else if (*clock >= 400)
378 *clock = 400;
379 else if (*clock >= 333)
380 *clock = 333;
381 else
382 *clock = 0;
383}
384static void lower_clock(unsigned int *const clock)
385{
386 --*clock;
387 normalize_clock(clock);
388}
389static unsigned int find_common_clock_cas(sysinfo_t *const sysinfo,
390 const spdinfo_t *const spdinfo)
391{
392 /* various constraints must be fulfilled:
393 CAS * tCK < 20ns == 160MTB
394 tCK_max >= tCK >= tCK_min
395 CAS >= roundup(tAA_min/tCK)
396 CAS supported
397 Clock(MHz) = 1000 / tCK(ns)
398 Clock(MHz) = 8000 / tCK(MTB)
399 AND BTW: Clock(MT) = 2000 / tCK(ns) - intel uses MTs but calls them MHz
400 */
401 int i;
402
403 /* Calculate common cas_latencies mask, tCKmin and tAAmin. */
404 unsigned int cas_latencies = (unsigned int)-1;
405 unsigned int tCKmin = 0, tAAmin = 0;
406 FOR_EACH_POPULATED_CHANNEL(sysinfo->dimms, i) {
407 cas_latencies &= spdinfo->channel[i].cas_latencies;
408 if (spdinfo->channel[i].tCKmin > tCKmin)
409 tCKmin = spdinfo->channel[i].tCKmin;
410 if (spdinfo->channel[i].tAAmin > tAAmin)
411 tAAmin = spdinfo->channel[i].tAAmin;
412 }
413
414 /* Get actual value of fsb clock. */
415 sysinfo->selected_timings.fsb_clock = read_fsb_clock();
416 unsigned int fsb_mhz = 0;
417 switch (sysinfo->selected_timings.fsb_clock) {
418 case FSB_CLOCK_1067MHz: fsb_mhz = 1067; break;
419 case FSB_CLOCK_800MHz: fsb_mhz = 800; break;
420 case FSB_CLOCK_667MHz: fsb_mhz = 667; break;
421 }
422
423 unsigned int clock = 8000 / tCKmin;
424 if ((clock > sysinfo->max_ddr3_mt / 2) || (clock > fsb_mhz / 2)) {
Elyes HAOUASba9b5042019-12-19 07:47:52 +0100425 int new_clock = MIN(sysinfo->max_ddr3_mt / 2, fsb_mhz / 2);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100426 printk(BIOS_SPEW, "DIMMs support %d MHz, but chipset only runs at up to %d. Limiting...\n",
427 clock, new_clock);
428 clock = new_clock;
429 }
430 normalize_clock(&clock);
431
432 /* Find compatible clock / CAS pair. */
433 unsigned int tCKproposed;
434 unsigned int CAS;
435 while (1) {
436 if (!clock)
437 die("Couldn't find compatible clock / CAS settings.\n");
438 tCKproposed = 8000 / clock;
Elyes HAOUAS6df3b642018-11-26 22:53:49 +0100439 CAS = DIV_ROUND_UP(tAAmin, tCKproposed);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100440 printk(BIOS_SPEW, "Trying CAS %u, tCK %u.\n", CAS, tCKproposed);
441 for (; CAS <= DDR3_MAX_CAS; ++CAS)
442 if (cas_latencies & (1 << CAS))
443 break;
444 if ((CAS <= DDR3_MAX_CAS) && (CAS * tCKproposed < 160)) {
445 /* Found good CAS. */
446 printk(BIOS_SPEW, "Found compatible clock / CAS pair: %u / %u.\n", clock, CAS);
447 break;
448 }
449 lower_clock(&clock);
450 }
451 sysinfo->selected_timings.CAS = CAS;
452 sysinfo->selected_timings.mem_clock = clock_index(clock);
453
454 return tCKproposed;
455}
456
457static void calculate_derived_timings(sysinfo_t *const sysinfo,
458 const unsigned int tCLK,
459 const spdinfo_t *const spdinfo)
460{
461 int i;
462
463 /* Calculate common tRASmin, tRPmin, tRCDmin and tWRmin. */
464 unsigned int tRASmin = 0, tRPmin = 0, tRCDmin = 0, tWRmin = 0;
465 FOR_EACH_POPULATED_CHANNEL(sysinfo->dimms, i) {
466 if (spdinfo->channel[i].tRAS > tRASmin)
467 tRASmin = spdinfo->channel[i].tRAS;
468 if (spdinfo->channel[i].tRP > tRPmin)
469 tRPmin = spdinfo->channel[i].tRP;
470 if (spdinfo->channel[i].tRCD > tRCDmin)
471 tRCDmin = spdinfo->channel[i].tRCD;
472 if (spdinfo->channel[i].tWR > tWRmin)
473 tWRmin = spdinfo->channel[i].tWR;
474 }
Elyes HAOUAS6df3b642018-11-26 22:53:49 +0100475 tRASmin = DIV_ROUND_UP(tRASmin, tCLK);
476 tRPmin = DIV_ROUND_UP(tRPmin, tCLK);
477 tRCDmin = DIV_ROUND_UP(tRCDmin, tCLK);
478 tWRmin = DIV_ROUND_UP(tWRmin, tCLK);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100479
480 /* Lookup tRFC and calculate common tRFCmin. */
481 const unsigned int tRFC_from_clock_and_cap[][4] = {
482 /* CAP_256M CAP_512M CAP_1G CAP_2G */
483 /* 533MHz */ { 40, 56, 68, 104 },
484 /* 400MHz */ { 30, 42, 51, 78 },
485 /* 333MHz */ { 25, 35, 43, 65 },
486 };
487 unsigned int tRFCmin = 0;
488 FOR_EACH_POPULATED_CHANNEL(sysinfo->dimms, i) {
489 const unsigned int tRFC = tRFC_from_clock_and_cap
490 [sysinfo->selected_timings.mem_clock][spdinfo->channel[i].chip_capacity];
491 if (tRFC > tRFCmin)
492 tRFCmin = tRFC;
493 }
494
495 /* Calculate common tRD from CAS and FSB and DRAM clocks. */
496 unsigned int tRDmin = sysinfo->selected_timings.CAS;
497 switch (sysinfo->selected_timings.fsb_clock) {
498 case FSB_CLOCK_667MHz:
499 tRDmin += 1;
500 break;
501 case FSB_CLOCK_800MHz:
502 tRDmin += 2;
503 break;
504 case FSB_CLOCK_1067MHz:
505 tRDmin += 3;
506 if (sysinfo->selected_timings.mem_clock == MEM_CLOCK_1067MT)
507 tRDmin += 1;
508 break;
509 }
510
511 /* Calculate common tRRDmin. */
512 unsigned int tRRDmin = 0;
513 FOR_EACH_POPULATED_CHANNEL(sysinfo->dimms, i) {
514 unsigned int tRRD = 2 + (spdinfo->channel[i].page_size / 1024);
515 if (sysinfo->selected_timings.mem_clock == MEM_CLOCK_1067MT)
516 tRRD += (spdinfo->channel[i].page_size / 1024);
517 if (tRRD > tRRDmin)
518 tRRDmin = tRRD;
519 }
520
521 /* Lookup and calculate common tFAWmin. */
522 unsigned int tFAW_from_pagesize_and_clock[][3] = {
523 /* 533MHz 400MHz 333MHz */
524 /* 1K */ { 20, 15, 13 },
525 /* 2K */ { 27, 20, 17 },
526 };
527 unsigned int tFAWmin = 0;
528 FOR_EACH_POPULATED_CHANNEL(sysinfo->dimms, i) {
529 const unsigned int tFAW = tFAW_from_pagesize_and_clock
530 [spdinfo->channel[i].page_size / 1024 - 1]
531 [sysinfo->selected_timings.mem_clock];
532 if (tFAW > tFAWmin)
533 tFAWmin = tFAW;
534 }
535
536 /* Refresh rate is fixed. */
537 unsigned int tWL;
538 if (sysinfo->selected_timings.mem_clock == MEM_CLOCK_1067MT) {
539 tWL = 6;
540 } else {
541 tWL = 5;
542 }
543
544 printk(BIOS_SPEW, "Timing values:\n"
545 " tCLK: %3u\n"
546 " tRAS: %3u\n"
547 " tRP: %3u\n"
548 " tRCD: %3u\n"
549 " tRFC: %3u\n"
550 " tWR: %3u\n"
551 " tRD: %3u\n"
552 " tRRD: %3u\n"
553 " tFAW: %3u\n"
554 " tWL: %3u\n",
555 tCLK, tRASmin, tRPmin, tRCDmin, tRFCmin, tWRmin, tRDmin, tRRDmin, tFAWmin, tWL);
556
557 sysinfo->selected_timings.tRAS = tRASmin;
558 sysinfo->selected_timings.tRP = tRPmin;
559 sysinfo->selected_timings.tRCD = tRCDmin;
560 sysinfo->selected_timings.tRFC = tRFCmin;
561 sysinfo->selected_timings.tWR = tWRmin;
562 sysinfo->selected_timings.tRD = tRDmin;
563 sysinfo->selected_timings.tRRD = tRRDmin;
564 sysinfo->selected_timings.tFAW = tFAWmin;
565 sysinfo->selected_timings.tWL = tWL;
566}
567
568static void collect_dimm_config(sysinfo_t *const sysinfo)
569{
570 int i;
571 spdinfo_t spdinfo;
572
573 spdinfo.dimm_mask = 0;
574 sysinfo->spd_type = 0;
575
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200576 for (i = 0; i < 4; i++)
577 if (sysinfo->spd_map[i]) {
578 const u8 spd = smbus_read_byte(sysinfo->spd_map[i], 2);
579 printk (BIOS_DEBUG, "%x:%x:%x\n",
580 i, sysinfo->spd_map[i],
581 spd);
582 if ((spd == 7) || (spd == 8) || (spd == 0xb)) {
583 spdinfo.dimm_mask |= 1 << i;
584 if (sysinfo->spd_type && sysinfo->spd_type != spd) {
585 die("Multiple types of DIMM installed in the system, don't do that!\n");
586 }
587 sysinfo->spd_type = spd;
Patrick Georgi2efc8802012-11-06 11:03:53 +0100588 }
Patrick Georgi2efc8802012-11-06 11:03:53 +0100589 }
Patrick Georgi2efc8802012-11-06 11:03:53 +0100590 if (spdinfo.dimm_mask == 0) {
591 die("Could not find any DIMM.\n");
592 }
593
594 /* Normalize spd_type to 1, 2, 3. */
595 sysinfo->spd_type = (sysinfo->spd_type & 1) | ((sysinfo->spd_type & 8) >> 2);
596 printk(BIOS_SPEW, "DDR mask %x, DDR %d\n", spdinfo.dimm_mask, sysinfo->spd_type);
597
598 if (sysinfo->spd_type == DDR2) {
599 die("DDR2 not supported at this time.\n");
600 } else if (sysinfo->spd_type == DDR3) {
Vladimir Serbinenkoc4d89482014-06-05 09:14:48 +0200601 verify_ddr3(sysinfo, spdinfo.dimm_mask);
602 collect_ddr3(sysinfo, &spdinfo);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100603 } else {
604 die("Will never support DDR1.\n");
605 }
606
607 for (i = 0; i < 2; i++) {
608 if ((spdinfo.dimm_mask >> (i*2)) & 1) {
609 printk(BIOS_SPEW, "Bank %d populated:\n"
610 " Raw card type: %4c\n"
611 " Row addr bits: %4u\n"
612 " Col addr bits: %4u\n"
613 " byte width: %4u\n"
614 " page size: %4u\n"
615 " banks: %4u\n"
616 " ranks: %4u\n"
617 " tAAmin: %3u\n"
618 " tCKmin: %3u\n"
619 " Max clock: %3u MHz\n"
620 " CAS: 0x%04x\n",
621 i, spdinfo.channel[i].raw_card + 'A',
622 spdinfo.channel[i].rows, spdinfo.channel[i].cols,
623 spdinfo.channel[i].width, spdinfo.channel[i].page_size,
624 spdinfo.channel[i].banks, spdinfo.channel[i].ranks,
625 spdinfo.channel[i].tAAmin, spdinfo.channel[i].tCKmin,
626 8000 / spdinfo.channel[i].tCKmin, spdinfo.channel[i].cas_latencies);
627 }
628 }
629
630 FOR_EACH_CHANNEL(i) {
631 sysinfo->dimms[i].card_type =
632 (spdinfo.dimm_mask & (1 << (i * 2))) ? spdinfo.channel[i].raw_card + 0xa : 0;
633 }
634
635 /* Find common memory clock and CAS. */
636 const unsigned int tCLK = find_common_clock_cas(sysinfo, &spdinfo);
637
638 /* Calculate other timings from clock and CAS. */
639 calculate_derived_timings(sysinfo, tCLK, &spdinfo);
640
641 /* Initialize DIMM infos. */
642 /* Always prefer interleaved over async channel mode. */
643 FOR_EACH_CHANNEL(i) {
644 IF_CHANNEL_POPULATED(sysinfo->dimms, i) {
645 sysinfo->dimms[i].banks = spdinfo.channel[i].banks;
646 sysinfo->dimms[i].ranks = spdinfo.channel[i].ranks;
647
648 /* .width is 1 for x8 or 2 for x16, bus width is 8 bytes. */
649 const unsigned int chips_per_rank = 8 / spdinfo.channel[i].width;
650
651 sysinfo->dimms[i].chip_width = spdinfo.channel[i].width;
652 sysinfo->dimms[i].chip_capacity = spdinfo.channel[i].chip_capacity;
653 sysinfo->dimms[i].page_size = spdinfo.channel[i].page_size * chips_per_rank;
654 sysinfo->dimms[i].rank_capacity_mb =
655 /* offset of chip_capacity is 8 (256M), therefore, add 8
656 chip_capacity is in Mbit, we want MByte, therefore, subtract 3 */
657 (1 << (spdinfo.channel[i].chip_capacity + 8 - 3)) * chips_per_rank;
658 }
659 }
660 if (CHANNEL_IS_POPULATED(sysinfo->dimms, 0) &&
661 CHANNEL_IS_POPULATED(sysinfo->dimms, 1))
662 sysinfo->selected_timings.channel_mode = CHANNEL_MODE_DUAL_INTERLEAVED;
663 else
664 sysinfo->selected_timings.channel_mode = CHANNEL_MODE_SINGLE;
665}
666
667static void reset_on_bad_warmboot(void)
668{
669 /* Check self refresh channel status. */
670 const u32 reg = MCHBAR32(PMSTS_MCHBAR);
671 /* Clear status bits. R/WC */
672 MCHBAR32(PMSTS_MCHBAR) = reg;
673 if ((reg & PMSTS_WARM_RESET) && !(reg & PMSTS_BOTH_SELFREFRESH)) {
674 printk(BIOS_INFO, "DRAM was not in self refresh "
675 "during warm boot, reset required.\n");
676 gm45_early_reset();
677 }
678}
679
680static void set_system_memory_frequency(const timings_t *const timings)
681{
682 MCHBAR16(CLKCFG_MCHBAR + 0x60) &= ~(1 << 15);
683 MCHBAR16(CLKCFG_MCHBAR + 0x48) &= ~(1 << 15);
684
685 /* Calculate wanted frequency setting. */
686 const int want_freq = 6 - timings->mem_clock;
687
688 /* Read current memory frequency. */
689 const u32 clkcfg = MCHBAR32(CLKCFG_MCHBAR);
690 int cur_freq = (clkcfg & CLKCFG_MEMCLK_MASK) >> CLKCFG_MEMCLK_SHIFT;
691 if (0 == cur_freq) {
692 /* Try memory frequency from scratchpad. */
693 printk(BIOS_DEBUG, "Reading current memory frequency from scratchpad.\n");
694 cur_freq = (MCHBAR16(SSKPD_MCHBAR) & SSKPD_CLK_MASK) >> SSKPD_CLK_SHIFT;
695 }
696
697 if (cur_freq != want_freq) {
698 printk(BIOS_DEBUG, "Changing memory frequency: old %x, new %x.\n", cur_freq, want_freq);
699 /* When writing new frequency setting, reset, then set update bit. */
700 MCHBAR32(CLKCFG_MCHBAR) = (MCHBAR32(CLKCFG_MCHBAR) & ~(CLKCFG_UPDATE | CLKCFG_MEMCLK_MASK)) |
701 (want_freq << CLKCFG_MEMCLK_SHIFT);
702 MCHBAR32(CLKCFG_MCHBAR) = (MCHBAR32(CLKCFG_MCHBAR) & ~CLKCFG_MEMCLK_MASK) |
703 (want_freq << CLKCFG_MEMCLK_SHIFT) | CLKCFG_UPDATE;
704 /* Reset update bit. */
705 MCHBAR32(CLKCFG_MCHBAR) &= ~CLKCFG_UPDATE;
706 }
707
708 if ((timings->fsb_clock == FSB_CLOCK_1067MHz) && (timings->mem_clock == MEM_CLOCK_667MT)) {
709 MCHBAR32(CLKCFG_MCHBAR + 0x16) = 0x000030f0;
710 MCHBAR32(CLKCFG_MCHBAR + 0x64) = 0x000050c1;
711
712 MCHBAR32(CLKCFG_MCHBAR) = (MCHBAR32(CLKCFG_MCHBAR) & ~(1 << 12)) | (1 << 17);
713 MCHBAR32(CLKCFG_MCHBAR) |= (1 << 17) | (1 << 12);
714 MCHBAR32(CLKCFG_MCHBAR) &= ~(1 << 12);
715
716 MCHBAR32(CLKCFG_MCHBAR + 0x04) = 0x9bad1f1f;
717 MCHBAR8(CLKCFG_MCHBAR + 0x08) = 0xf4;
718 MCHBAR8(CLKCFG_MCHBAR + 0x0a) = 0x43;
719 MCHBAR8(CLKCFG_MCHBAR + 0x0c) = 0x10;
720 MCHBAR8(CLKCFG_MCHBAR + 0x0d) = 0x80;
721 MCHBAR32(CLKCFG_MCHBAR + 0x50) = 0x0b0e151b;
722 MCHBAR8(CLKCFG_MCHBAR + 0x54) = 0xb4;
723 MCHBAR8(CLKCFG_MCHBAR + 0x55) = 0x10;
724 MCHBAR8(CLKCFG_MCHBAR + 0x56) = 0x08;
725
726 MCHBAR32(CLKCFG_MCHBAR) |= (1 << 10);
727 MCHBAR32(CLKCFG_MCHBAR) |= (1 << 11);
728 MCHBAR32(CLKCFG_MCHBAR) &= ~(1 << 10);
729 MCHBAR32(CLKCFG_MCHBAR) &= ~(1 << 11);
730 }
731
732 MCHBAR32(CLKCFG_MCHBAR + 0x48) |= 0x3f << 24;
733}
734
735int raminit_read_vco_index(void)
736{
Nico Huberd85a71a2016-11-27 14:43:12 +0100737 switch (MCHBAR8(HPLLVCO_MCHBAR) & 0x7) {
Patrick Georgi2efc8802012-11-06 11:03:53 +0100738 case VCO_2666:
739 return 0;
740 case VCO_3200:
741 return 1;
742 case VCO_4000:
743 return 2;
744 case VCO_5333:
745 return 3;
746 default:
747 die("Unknown VCO frequency.\n");
748 return 0;
749 }
750}
751static void set_igd_memory_frequencies(const sysinfo_t *const sysinfo)
752{
753 const int gfx_idx = ((sysinfo->gfx_type == GMCH_GS45) &&
754 !sysinfo->gs45_low_power_mode)
755 ? (GMCH_GS45 + 1) : sysinfo->gfx_type;
756
757 /* Render and sampler frequency values seem to be some kind of factor. */
758 const u16 render_freq_from_vco_and_gfxtype[][10] = {
759 /* GM45 GM47 GM49 GE45 GL40 GL43 GS40 GS45 (perf) */
760 /* VCO 2666 */ { 0xd, 0xd, 0xe, 0xd, 0xb, 0xd, 0xb, 0xa, 0xd },
761 /* VCO 3200 */ { 0xd, 0xe, 0xf, 0xd, 0xb, 0xd, 0xb, 0x9, 0xd },
762 /* VCO 4000 */ { 0xc, 0xd, 0xf, 0xc, 0xa, 0xc, 0xa, 0x9, 0xc },
763 /* VCO 5333 */ { 0xb, 0xc, 0xe, 0xb, 0x9, 0xb, 0x9, 0x8, 0xb },
764 };
765 const u16 sampler_freq_from_vco_and_gfxtype[][10] = {
766 /* GM45 GM47 GM49 GE45 GL40 GL43 GS40 GS45 (perf) */
767 /* VCO 2666 */ { 0xc, 0xc, 0xd, 0xc, 0x9, 0xc, 0x9, 0x8, 0xc },
768 /* VCO 3200 */ { 0xc, 0xd, 0xe, 0xc, 0x9, 0xc, 0x9, 0x8, 0xc },
769 /* VCO 4000 */ { 0xa, 0xc, 0xd, 0xa, 0x8, 0xa, 0x8, 0x8, 0xa },
770 /* VCO 5333 */ { 0xa, 0xa, 0xc, 0xa, 0x7, 0xa, 0x7, 0x6, 0xa },
771 };
772 const u16 display_clock_select_from_gfxtype[] = {
773 /* GM45 GM47 GM49 GE45 GL40 GL43 GS40 GS45 (perf) */
774 1, 1, 1, 1, 1, 1, 1, 0, 1
775 };
776
777 if (pci_read_config16(GCFGC_PCIDEV, 0) != 0x8086) {
778 printk(BIOS_DEBUG, "Skipping IGD memory frequency setting.\n");
779 return;
780 }
781
782 MCHBAR16(0x119e) = 0xa800;
783 MCHBAR16(0x11c0) = (MCHBAR16(0x11c0) & ~0xff00) | (0x01 << 8);
784 MCHBAR16(0x119e) = 0xb800;
785 MCHBAR8(0x0f10) |= 1 << 7;
786
787 /* Read VCO. */
788 const int vco_idx = raminit_read_vco_index();
789 printk(BIOS_DEBUG, "Setting IGD memory frequencies for VCO #%d.\n", vco_idx);
790
791 const u32 freqcfg =
792 ((render_freq_from_vco_and_gfxtype[vco_idx][gfx_idx]
793 << GCFGC_CR_SHIFT) & GCFGC_CR_MASK) |
794 ((sampler_freq_from_vco_and_gfxtype[vco_idx][gfx_idx]
795 << GCFGC_CS_SHIFT) & GCFGC_CS_MASK);
796
797 /* Set frequencies, clear update bit. */
798 u32 gcfgc = pci_read_config16(GCFGC_PCIDEV, GCFGC_OFFSET);
799 gcfgc &= ~(GCFGC_CS_MASK | GCFGC_UPDATE | GCFGC_CR_MASK);
800 gcfgc |= freqcfg;
801 pci_write_config16(GCFGC_PCIDEV, GCFGC_OFFSET, gcfgc);
802
803 /* Set frequencies, set update bit. */
804 gcfgc = pci_read_config16(GCFGC_PCIDEV, GCFGC_OFFSET);
805 gcfgc &= ~(GCFGC_CS_MASK | GCFGC_CR_MASK);
806 gcfgc |= freqcfg | GCFGC_UPDATE;
807 pci_write_config16(GCFGC_PCIDEV, GCFGC_OFFSET, gcfgc);
808
809 /* Clear update bit. */
810 pci_write_config16(GCFGC_PCIDEV, GCFGC_OFFSET,
811 pci_read_config16(GCFGC_PCIDEV, GCFGC_OFFSET) & ~GCFGC_UPDATE);
812
813 /* Set display clock select bit. */
814 pci_write_config16(GCFGC_PCIDEV, GCFGC_OFFSET,
815 (pci_read_config16(GCFGC_PCIDEV, GCFGC_OFFSET) & ~GCFGC_CD_MASK) |
816 (display_clock_select_from_gfxtype[gfx_idx] << GCFGC_CD_SHIFT));
817}
818
819static void configure_dram_control_mode(const timings_t *const timings, const dimminfo_t *const dimms)
820{
821 int ch, r;
822
823 FOR_EACH_CHANNEL(ch) {
824 unsigned int mchbar = CxDRC0_MCHBAR(ch);
825 u32 cxdrc = MCHBAR32(mchbar);
826 cxdrc &= ~CxDRC0_RANKEN_MASK;
827 FOR_EACH_POPULATED_RANK_IN_CHANNEL(dimms, ch, r)
828 cxdrc |= CxDRC0_RANKEN(r);
829 cxdrc = (cxdrc & ~CxDRC0_RMS_MASK) |
830 /* Always 7.8us for DDR3: */
831 CxDRC0_RMS_78US;
832 MCHBAR32(mchbar) = cxdrc;
833
834 mchbar = CxDRC1_MCHBAR(ch);
835 cxdrc = MCHBAR32(mchbar);
836 cxdrc |= CxDRC1_NOTPOP_MASK;
837 FOR_EACH_POPULATED_RANK_IN_CHANNEL(dimms, ch, r)
838 cxdrc &= ~CxDRC1_NOTPOP(r);
839 cxdrc |= CxDRC1_MUSTWR;
840 MCHBAR32(mchbar) = cxdrc;
841
842 mchbar = CxDRC2_MCHBAR(ch);
843 cxdrc = MCHBAR32(mchbar);
844 cxdrc |= CxDRC2_NOTPOP_MASK;
845 FOR_EACH_POPULATED_RANK_IN_CHANNEL(dimms, ch, r)
846 cxdrc &= ~CxDRC2_NOTPOP(r);
847 cxdrc |= CxDRC2_MUSTWR;
848 if (timings->mem_clock == MEM_CLOCK_1067MT)
849 cxdrc |= CxDRC2_CLK1067MT;
850 MCHBAR32(mchbar) = cxdrc;
851 }
852}
853
854static void rcomp_initialization(const stepping_t stepping, const int sff)
855{
Elyes HAOUAS3d450002018-08-09 18:55:58 +0200856 /* Program RCOMP codes. */
Patrick Georgi2efc8802012-11-06 11:03:53 +0100857 if (sff)
858 die("SFF platform unsupported in RCOMP initialization.\n");
859 /* Values are for DDR3. */
860 MCHBAR8(0x6ac) &= ~0x0f;
861 MCHBAR8(0x6b0) = 0x55;
862 MCHBAR8(0x6ec) &= ~0x0f;
863 MCHBAR8(0x6f0) = 0x66;
864 MCHBAR8(0x72c) &= ~0x0f;
865 MCHBAR8(0x730) = 0x66;
866 MCHBAR8(0x76c) &= ~0x0f;
867 MCHBAR8(0x770) = 0x66;
868 MCHBAR8(0x7ac) &= ~0x0f;
869 MCHBAR8(0x7b0) = 0x66;
870 MCHBAR8(0x7ec) &= ~0x0f;
871 MCHBAR8(0x7f0) = 0x66;
872 MCHBAR8(0x86c) &= ~0x0f;
873 MCHBAR8(0x870) = 0x55;
874 MCHBAR8(0x8ac) &= ~0x0f;
875 MCHBAR8(0x8b0) = 0x66;
876 /* ODT multiplier bits. */
877 MCHBAR32(0x04d0) = (MCHBAR32(0x04d0) & ~((7 << 3) | (7 << 0))) | (2 << 3) | (2 << 0);
878
879 /* Perform RCOMP calibration for DDR3. */
880 raminit_rcomp_calibration(stepping);
881
882 /* Run initial RCOMP. */
883 MCHBAR32(0x418) |= 1 << 17;
884 MCHBAR32(0x40c) &= ~(1 << 23);
885 MCHBAR32(0x41c) &= ~((1 << 7) | (1 << 3));
886 MCHBAR32(0x400) |= 1;
887 while (MCHBAR32(0x400) & 1) {}
888
889 /* Run second RCOMP. */
890 MCHBAR32(0x40c) |= 1 << 19;
891 MCHBAR32(0x400) |= 1;
892 while (MCHBAR32(0x400) & 1) {}
893
894 /* Cleanup and start periodic RCOMP. */
895 MCHBAR32(0x40c) &= ~(1 << 19);
896 MCHBAR32(0x40c) |= 1 << 23;
897 MCHBAR32(0x418) &= ~(1 << 17);
898 MCHBAR32(0x41c) |= (1 << 7) | (1 << 3);
899 MCHBAR32(0x400) |= (1 << 1);
900}
901
902static void dram_powerup(const int resume)
903{
Arthur Heymans10141c32016-10-27 00:31:41 +0200904 udelay(200);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100905 MCHBAR32(CLKCFG_MCHBAR) = (MCHBAR32(CLKCFG_MCHBAR) & ~(1 << 3)) | (3 << 21);
906 if (!resume) {
907 MCHBAR32(0x1434) |= (1 << 10);
Arthur Heymans10141c32016-10-27 00:31:41 +0200908 udelay(1);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100909 }
910 MCHBAR32(0x1434) |= (1 << 6);
911 if (!resume) {
Arthur Heymans10141c32016-10-27 00:31:41 +0200912 udelay(1);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100913 MCHBAR32(0x1434) |= (1 << 9);
914 MCHBAR32(0x1434) &= ~(1 << 10);
915 udelay(500);
916 }
917}
918static void dram_program_timings(const timings_t *const timings)
919{
920 /* Values are for DDR3. */
921 const int burst_length = 8;
922 const int tWTR = 4, tRTP = 1;
923 int i;
924
925 FOR_EACH_CHANNEL(i) {
926 u32 reg = MCHBAR32(CxDRT0_MCHBAR(i));
927 const int btb_wtp = timings->tWL + burst_length/2 + timings->tWR;
928 const int btb_wtr = timings->tWL + burst_length/2 + tWTR;
929 reg = (reg & ~(CxDRT0_BtB_WtP_MASK | CxDRT0_BtB_WtR_MASK)) |
930 ((btb_wtp << CxDRT0_BtB_WtP_SHIFT) & CxDRT0_BtB_WtP_MASK) |
931 ((btb_wtr << CxDRT0_BtB_WtR_SHIFT) & CxDRT0_BtB_WtR_MASK);
932 if (timings->mem_clock != MEM_CLOCK_1067MT) {
933 reg = (reg & ~(0x7 << 15)) | ((9 - timings->CAS) << 15);
934 reg = (reg & ~(0xf << 10)) | ((timings->CAS - 3) << 10);
935 } else {
936 reg = (reg & ~(0x7 << 15)) | ((10 - timings->CAS) << 15);
937 reg = (reg & ~(0xf << 10)) | ((timings->CAS - 4) << 10);
938 }
939 reg = (reg & ~(0x7 << 5)) | (3 << 5);
940 reg = (reg & ~(0x7 << 0)) | (1 << 0);
941 MCHBAR32(CxDRT0_MCHBAR(i)) = reg;
942
943 reg = MCHBAR32(CxDRT1_MCHBAR(i));
944 reg = (reg & ~(0x03 << 28)) | ((tRTP & 0x03) << 28);
945 reg = (reg & ~(0x1f << 21)) | ((timings->tRAS & 0x1f) << 21);
946 reg = (reg & ~(0x07 << 10)) | (((timings->tRRD - 2) & 0x07) << 10);
947 reg = (reg & ~(0x07 << 5)) | (((timings->tRCD - 2) & 0x07) << 5);
948 reg = (reg & ~(0x07 << 0)) | (((timings->tRP - 2) & 0x07) << 0);
949 MCHBAR32(CxDRT1_MCHBAR(i)) = reg;
950
951 reg = MCHBAR32(CxDRT2_MCHBAR(i));
952 reg = (reg & ~(0x1f << 17)) | ((timings->tFAW & 0x1f) << 17);
953 if (timings->mem_clock != MEM_CLOCK_1067MT) {
954 reg = (reg & ~(0x7 << 12)) | (0x2 << 12);
955 reg = (reg & ~(0xf << 6)) | (0x9 << 6);
956 } else {
957 reg = (reg & ~(0x7 << 12)) | (0x3 << 12);
958 reg = (reg & ~(0xf << 6)) | (0xc << 6);
959 }
960 reg = (reg & ~(0x1f << 0)) | (0x13 << 0);
961 MCHBAR32(CxDRT2_MCHBAR(i)) = reg;
962
963 reg = MCHBAR32(CxDRT3_MCHBAR(i));
964 reg |= 0x3 << 28;
965 reg = (reg & ~(0x03 << 26));
966 reg = (reg & ~(0x07 << 23)) | (((timings->CAS - 3) & 0x07) << 23);
967 reg = (reg & ~(0xff << 13)) | ((timings->tRFC & 0xff) << 13);
968 reg = (reg & ~(0x07 << 0)) | (((timings->tWL - 2) & 0x07) << 0);
969 MCHBAR32(CxDRT3_MCHBAR(i)) = reg;
970
971 reg = MCHBAR32(CxDRT4_MCHBAR(i));
972 static const u8 timings_by_clock[4][3] = {
973 /* 333MHz 400MHz 533MHz
974 667MT 800MT 1067MT */
975 { 0x07, 0x0a, 0x0d },
976 { 0x3a, 0x46, 0x5d },
977 { 0x0c, 0x0e, 0x18 },
978 { 0x21, 0x28, 0x35 },
979 };
980 const int clk_idx = 2 - timings->mem_clock;
981 reg = (reg & ~(0x01f << 27)) | (timings_by_clock[0][clk_idx] << 27);
982 reg = (reg & ~(0x3ff << 17)) | (timings_by_clock[1][clk_idx] << 17);
983 reg = (reg & ~(0x03f << 10)) | (timings_by_clock[2][clk_idx] << 10);
984 reg = (reg & ~(0x1ff << 0)) | (timings_by_clock[3][clk_idx] << 0);
985 MCHBAR32(CxDRT4_MCHBAR(i)) = reg;
986
987 reg = MCHBAR32(CxDRT5_MCHBAR(i));
988 if (timings->mem_clock == MEM_CLOCK_1067MT)
989 reg = (reg & ~(0xf << 28)) | (0x8 << 28);
990 reg = (reg & ~(0x00f << 22)) | ((burst_length/2 + timings->CAS + 2) << 22);
991 reg = (reg & ~(0x3ff << 12)) | (0x190 << 12);
992 reg = (reg & ~(0x00f << 4)) | ((timings->CAS - 2) << 4);
993 reg = (reg & ~(0x003 << 2)) | (0x001 << 2);
994 reg = (reg & ~(0x003 << 0));
995 MCHBAR32(CxDRT5_MCHBAR(i)) = reg;
996
997 reg = MCHBAR32(CxDRT6_MCHBAR(i));
998 reg = (reg & ~(0xffff << 16)) | (0x066a << 16); /* always 7.8us refresh rate for DDR3 */
999 reg |= (1 << 2);
1000 MCHBAR32(CxDRT6_MCHBAR(i)) = reg;
1001 }
1002}
1003
1004static void dram_program_banks(const dimminfo_t *const dimms)
1005{
1006 int ch, r;
1007
1008 FOR_EACH_CHANNEL(ch) {
1009 const int tRPALL = dimms[ch].banks == 8;
1010
1011 u32 reg = MCHBAR32(CxDRT1_MCHBAR(ch)) & ~(0x01 << 15);
1012 IF_CHANNEL_POPULATED(dimms, ch)
1013 reg |= tRPALL << 15;
1014 MCHBAR32(CxDRT1_MCHBAR(ch)) = reg;
1015
1016 reg = MCHBAR32(CxDRA_MCHBAR(ch)) & ~CxDRA_BANKS_MASK;
1017 FOR_EACH_POPULATED_RANK_IN_CHANNEL(dimms, ch, r) {
1018 reg |= CxDRA_BANKS(r, dimms[ch].banks);
1019 }
1020 MCHBAR32(CxDRA_MCHBAR(ch)) = reg;
1021 }
1022}
1023
1024static void odt_setup(const timings_t *const timings, const int sff)
1025{
1026 /* Values are for DDR3. */
1027 int ch;
1028
1029 FOR_EACH_CHANNEL(ch) {
1030 u32 reg = MCHBAR32(CxODT_HIGH(ch));
1031 if (sff && (timings->mem_clock != MEM_CLOCK_1067MT))
1032 reg &= ~(0x3 << (61 - 32));
1033 else
1034 reg |= 0x3 << (61 - 32);
1035 reg = (reg & ~(0x3 << (52 - 32))) | (0x2 << (52 - 32));
1036 reg = (reg & ~(0x7 << (48 - 32))) | ((timings->CAS - 3) << (48 - 32));
1037 reg = (reg & ~(0xf << (44 - 32))) | (0x7 << (44 - 32));
1038 if (timings->mem_clock != MEM_CLOCK_1067MT) {
1039 reg = (reg & ~(0xf << (40 - 32))) | ((12 - timings->CAS) << (40 - 32));
1040 reg = (reg & ~(0xf << (36 - 32))) | (( 2 + timings->CAS) << (36 - 32));
1041 } else {
1042 reg = (reg & ~(0xf << (40 - 32))) | ((13 - timings->CAS) << (40 - 32));
1043 reg = (reg & ~(0xf << (36 - 32))) | (( 1 + timings->CAS) << (36 - 32));
1044 }
1045 reg = (reg & ~(0xf << (32 - 32))) | (0x7 << (32 - 32));
1046 MCHBAR32(CxODT_HIGH(ch)) = reg;
1047
1048 reg = MCHBAR32(CxODT_LOW(ch));
1049 reg = (reg & ~(0x7 << 28)) | (0x2 << 28);
1050 reg = (reg & ~(0x3 << 22)) | (0x2 << 22);
1051 reg = (reg & ~(0x7 << 12)) | (0x2 << 12);
1052 reg = (reg & ~(0x7 << 4)) | (0x2 << 4);
1053 switch (timings->mem_clock) {
1054 case MEM_CLOCK_667MT:
1055 reg = (reg & ~0x7);
1056 break;
1057 case MEM_CLOCK_800MT:
1058 reg = (reg & ~0x7) | 0x2;
1059 break;
1060 case MEM_CLOCK_1067MT:
1061 reg = (reg & ~0x7) | 0x5;
1062 break;
1063 }
1064 MCHBAR32(CxODT_LOW(ch)) = reg;
1065 }
1066}
1067
1068static void misc_settings(const timings_t *const timings,
1069 const stepping_t stepping)
1070{
1071 MCHBAR32(0x1260) = (MCHBAR32(0x1260) & ~((1 << 24) | 0x1f)) | timings->tRD;
1072 MCHBAR32(0x1360) = (MCHBAR32(0x1360) & ~((1 << 24) | 0x1f)) | timings->tRD;
1073
1074 MCHBAR8(0x1268) = (MCHBAR8(0x1268) & ~(0xf)) | timings->tWL;
1075 MCHBAR8(0x1368) = (MCHBAR8(0x1368) & ~(0xf)) | timings->tWL;
1076 MCHBAR8(0x12a0) = (MCHBAR8(0x12a0) & ~(0xf)) | 0xa;
1077 MCHBAR8(0x13a0) = (MCHBAR8(0x13a0) & ~(0xf)) | 0xa;
1078
1079 MCHBAR32(0x218) = (MCHBAR32(0x218) & ~((7 << 29) | (7 << 25) | (3 << 22) | (3 << 10))) |
1080 (4 << 29) | (3 << 25) | (0 << 22) | (1 << 10);
1081 MCHBAR32(0x220) = (MCHBAR32(0x220) & ~(7 << 16)) | (1 << 21) | (1 << 16);
1082 MCHBAR32(0x224) = (MCHBAR32(0x224) & ~(7 << 8)) | (3 << 8);
1083 if (stepping >= STEPPING_B1)
1084 MCHBAR8(0x234) |= (1 << 3);
1085}
1086
1087static void clock_crossing_setup(const fsb_clock_t fsb,
1088 const mem_clock_t ddr3clock,
1089 const dimminfo_t *const dimms)
1090{
1091 int ch;
1092
1093 static const u32 values_from_fsb_and_mem[][3][4] = {
1094 /* FSB 1067MHz */{
1095 /* DDR3-1067 */ { 0x00000000, 0x00000000, 0x00180006, 0x00810060 },
1096 /* DDR3-800 */ { 0x00000000, 0x00000000, 0x0000001c, 0x000300e0 },
1097 /* DDR3-667 */ { 0x00000000, 0x00001c00, 0x03c00038, 0x0007e000 },
1098 },
1099 /* FSB 800MHz */{
1100 /* DDR3-1067 */ { 0, 0, 0, 0 },
1101 /* DDR3-800 */ { 0x00000000, 0x00000000, 0x0030000c, 0x000300c0 },
1102 /* DDR3-667 */ { 0x00000000, 0x00000380, 0x0060001c, 0x00030c00 },
1103 },
1104 /* FSB 667MHz */{
1105 /* DDR3-1067 */ { 0, 0, 0, 0 },
1106 /* DDR3-800 */ { 0, 0, 0, 0 },
1107 /* DDR3-667 */ { 0x00000000, 0x00000000, 0x0030000c, 0x000300c0 },
1108 },
1109 };
1110
1111 const u32 *data = values_from_fsb_and_mem[fsb][ddr3clock];
1112 MCHBAR32(0x0208) = data[3];
1113 MCHBAR32(0x020c) = data[2];
1114 if (((fsb == FSB_CLOCK_1067MHz) || (fsb == FSB_CLOCK_800MHz)) && (ddr3clock == MEM_CLOCK_667MT))
1115 MCHBAR32(0x0210) = data[1];
1116
1117 static const u32 from_fsb_and_mem[][3] = {
1118 /* DDR3-1067 DDR3-800 DDR3-667 */
1119 /* FSB 1067MHz */{ 0x40100401, 0x10040220, 0x08040110, },
1120 /* FSB 800MHz */{ 0x00000000, 0x40100401, 0x00080201, },
1121 /* FSB 667MHz */{ 0x00000000, 0x00000000, 0x40100401, },
1122 };
1123 FOR_EACH_CHANNEL(ch) {
1124 const unsigned int mchbar = 0x1258 + (ch * 0x0100);
1125 if ((fsb == FSB_CLOCK_1067MHz) && (ddr3clock == MEM_CLOCK_800MT) && CHANNEL_IS_CARDF(dimms, ch))
1126 MCHBAR32(mchbar) = 0x08040120;
1127 else
1128 MCHBAR32(mchbar) = from_fsb_and_mem[fsb][ddr3clock];
1129 MCHBAR32(mchbar + 4) = 0x00000000;
1130 }
1131}
1132
1133/* Program egress VC1 timings. */
1134static void vc1_program_timings(const fsb_clock_t fsb)
1135{
1136 const u32 timings_by_fsb[][2] = {
1137 /* FSB 1067MHz */ { 0x1a, 0x01380138 },
1138 /* FSB 800MHz */ { 0x14, 0x00f000f0 },
1139 /* FSB 667MHz */ { 0x10, 0x00c000c0 },
1140 };
1141 EPBAR8(0x2c) = timings_by_fsb[fsb][0];
1142 EPBAR32(0x38) = timings_by_fsb[fsb][1];
1143 EPBAR32(0x3c) = timings_by_fsb[fsb][1];
1144}
1145
Patrick Rudolph266a1f72016-06-09 18:13:34 +02001146#define DEFAULT_PCI_MMIO_SIZE 2048
1147#define HOST_BRIDGE PCI_DEVFN(0, 0)
1148
1149static unsigned int get_mmio_size(void)
1150{
1151 const struct device *dev;
1152 const struct northbridge_intel_gm45_config *cfg = NULL;
1153
Kyösti Mälkkie7377552018-06-21 16:20:55 +03001154 dev = pcidev_path_on_root(HOST_BRIDGE);
Patrick Rudolph266a1f72016-06-09 18:13:34 +02001155 if (dev)
1156 cfg = dev->chip_info;
1157
1158 /* If this is zero, it just means devicetree.cb didn't set it */
1159 if (!cfg || cfg->pci_mmio_size == 0)
1160 return DEFAULT_PCI_MMIO_SIZE;
1161 else
1162 return cfg->pci_mmio_size;
1163}
1164
Patrick Georgi2efc8802012-11-06 11:03:53 +01001165/* @prejedec if not zero, set rank size to 128MB and page size to 4KB. */
Vladimir Serbinenko56ae8a02014-08-16 10:59:02 +02001166static void program_memory_map(const dimminfo_t *const dimms, const channel_mode_t mode, const int prejedec, u16 ggc)
Patrick Georgi2efc8802012-11-06 11:03:53 +01001167{
1168 int ch, r;
1169
1170 /* Program rank boundaries (CxDRBy). */
1171 unsigned int base = 0; /* start of next rank in MB */
1172 unsigned int total_mb[2] = { 0, 0 }; /* total memory per channel in MB */
1173 FOR_EACH_CHANNEL(ch) {
1174 if (mode == CHANNEL_MODE_DUAL_INTERLEAVED)
1175 /* In interleaved mode, start every channel from 0. */
1176 base = 0;
1177 for (r = 0; r < RANKS_PER_CHANNEL; r += 2) {
1178 /* Fixed capacity for pre-jedec config. */
1179 const unsigned int rank_capacity_mb =
1180 prejedec ? 128 : dimms[ch].rank_capacity_mb;
1181 u32 reg = 0;
1182
1183 /* Program bounds in CxDRBy. */
1184 IF_RANK_POPULATED(dimms, ch, r) {
1185 base += rank_capacity_mb;
1186 total_mb[ch] += rank_capacity_mb;
1187 }
1188 reg |= CxDRBy_BOUND_MB(r, base);
1189 IF_RANK_POPULATED(dimms, ch, r+1) {
1190 base += rank_capacity_mb;
1191 total_mb[ch] += rank_capacity_mb;
1192 }
1193 reg |= CxDRBy_BOUND_MB(r+1, base);
1194
1195 MCHBAR32(CxDRBy_MCHBAR(ch, r)) = reg;
1196 }
1197 }
1198
1199 /* Program page size (CxDRA). */
1200 FOR_EACH_CHANNEL(ch) {
1201 u32 reg = MCHBAR32(CxDRA_MCHBAR(ch)) & ~CxDRA_PAGESIZE_MASK;
1202 FOR_EACH_POPULATED_RANK_IN_CHANNEL(dimms, ch, r) {
1203 /* Fixed page size for pre-jedec config. */
1204 const unsigned int page_size = /* dimm page size in bytes */
1205 prejedec ? 4096 : dimms[ch].page_size;
1206 reg |= CxDRA_PAGESIZE(r, log2(page_size));
1207 /* deferred to f5_27: reg |= CxDRA_BANKS(r, dimms[ch].banks); */
1208 }
1209 MCHBAR32(CxDRA_MCHBAR(ch)) = reg;
1210 }
1211
1212 /* Calculate memory mapping, all values in MB. */
Vladimir Serbinenko56ae8a02014-08-16 10:59:02 +02001213
1214 u32 uma_sizem = 0;
1215 if (!prejedec) {
1216 if (!(ggc & 2)) {
1217 printk(BIOS_DEBUG, "IGD decoded, subtracting ");
1218
1219 /* Graphics memory */
1220 const u32 gms_sizek = decode_igd_memory_size((ggc >> 4) & 0xf);
1221 printk(BIOS_DEBUG, "%uM UMA", gms_sizek >> 10);
1222
1223 /* GTT Graphics Stolen Memory Size (GGMS) */
1224 const u32 gsm_sizek = decode_igd_gtt_size((ggc >> 8) & 0xf);
1225 printk(BIOS_DEBUG, " and %uM GTT\n", gsm_sizek >> 10);
1226
1227 uma_sizem = (gms_sizek + gsm_sizek) >> 10;
Vladimir Serbinenko56ae8a02014-08-16 10:59:02 +02001228 }
Arthur Heymansd522db02018-08-06 15:50:54 +02001229 /* TSEG 2M, This amount can easily be covered by SMRR MTRR's,
1230 which requires to have TSEG_BASE aligned to TSEG_SIZE. */
Arthur Heymans8b766052018-01-24 23:25:13 +01001231 u8 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC);
1232 reg8 &= ~0x7;
Arthur Heymansd522db02018-08-06 15:50:54 +02001233 reg8 |= (1 << 1) | (1 << 0); /* 2M and TSEG_Enable */
Arthur Heymans8b766052018-01-24 23:25:13 +01001234 pci_write_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC, reg8);
Arthur Heymansd522db02018-08-06 15:50:54 +02001235 uma_sizem += 2;
Vladimir Serbinenko56ae8a02014-08-16 10:59:02 +02001236 }
1237
Patrick Rudolph266a1f72016-06-09 18:13:34 +02001238 const unsigned int mmio_size = get_mmio_size();
1239 const unsigned int MMIOstart = 4096 - mmio_size + uma_sizem;
Vladimir Serbinenko9907be42014-08-12 21:51:28 +02001240 const int me_active = pci_read_config8(PCI_DEV(0, 3, 0), PCI_CLASS_REVISION) != 0xff;
1241 const unsigned int ME_SIZE = prejedec || !me_active ? 0 : 32;
Patrick Georgi2efc8802012-11-06 11:03:53 +01001242 const unsigned int usedMEsize = (total_mb[0] != total_mb[1]) ? ME_SIZE : 2 * ME_SIZE;
1243 const unsigned int claimCapable =
1244 !(pci_read_config32(PCI_DEV(0, 0, 0), D0F0_CAPID0 + 4) & (1 << (47 - 32)));
1245
1246 const unsigned int TOM = total_mb[0] + total_mb[1];
1247 unsigned int TOMminusME = TOM - usedMEsize;
1248 unsigned int TOLUD = (TOMminusME < MMIOstart) ? TOMminusME : MMIOstart;
1249 unsigned int TOUUD = TOMminusME;
1250 unsigned int REMAPbase = 0xffff, REMAPlimit = 0;
1251
1252 if (claimCapable && (TOMminusME >= (MMIOstart + 64))) {
1253 /* 64MB alignment: We'll lose some MBs here, if ME is on. */
1254 TOMminusME &= ~(64 - 1);
1255 /* 64MB alignment: Loss will be reclaimed. */
1256 TOLUD &= ~(64 - 1);
1257 if (TOMminusME > 4096) {
1258 REMAPbase = TOMminusME;
1259 REMAPlimit = REMAPbase + (4096 - TOLUD);
1260 } else {
1261 REMAPbase = 4096;
1262 REMAPlimit = REMAPbase + (TOMminusME - TOLUD);
1263 }
1264 TOUUD = REMAPlimit;
1265 /* REMAPlimit is an inclusive bound, all others exclusive. */
1266 REMAPlimit -= 64;
1267 }
1268
1269 pci_write_config16(PCI_DEV(0, 0, 0), D0F0_TOM, (TOM >> 7) & 0x1ff);
1270 pci_write_config16(PCI_DEV(0, 0, 0), D0F0_TOLUD, TOLUD << 4);
1271 pci_write_config16(PCI_DEV(0, 0, 0), D0F0_TOUUD, TOUUD);
1272 pci_write_config16(PCI_DEV(0, 0, 0), D0F0_REMAPBASE, (REMAPbase >> 6) & 0x03ff);
1273 pci_write_config16(PCI_DEV(0, 0, 0), D0F0_REMAPLIMIT, (REMAPlimit >> 6) & 0x03ff);
1274
1275 /* Program channel mode. */
1276 switch (mode) {
1277 case CHANNEL_MODE_SINGLE:
1278 printk(BIOS_DEBUG, "Memory configured in single-channel mode.\n");
1279 MCHBAR32(DCC_MCHBAR) &= ~DCC_INTERLEAVED;
1280 break;
1281 case CHANNEL_MODE_DUAL_ASYNC:
Elyes HAOUASc9a717d2020-02-16 09:52:09 +01001282 printk(BIOS_DEBUG, "Memory configured in dual-channel asymmetric mode.\n");
Patrick Georgi2efc8802012-11-06 11:03:53 +01001283 MCHBAR32(DCC_MCHBAR) &= ~DCC_INTERLEAVED;
1284 break;
1285 case CHANNEL_MODE_DUAL_INTERLEAVED:
1286 printk(BIOS_DEBUG, "Memory configured in dual-channel interleaved mode.\n");
1287 MCHBAR32(DCC_MCHBAR) &= ~(DCC_NO_CHANXOR | (1 << 9));
1288 MCHBAR32(DCC_MCHBAR) |= DCC_INTERLEAVED;
1289 break;
1290 }
1291
1292 printk(BIOS_SPEW, "Memory map:\n"
1293 "TOM = %5uMB\n"
1294 "TOLUD = %5uMB\n"
1295 "TOUUD = %5uMB\n"
1296 "REMAP:\t base = %5uMB\n"
Vladimir Serbinenko9907be42014-08-12 21:51:28 +02001297 "\t limit = %5uMB\n"
1298 "usedMEsize: %dMB\n",
1299 TOM, TOLUD, TOUUD, REMAPbase, REMAPlimit, usedMEsize);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001300}
1301static void prejedec_memory_map(const dimminfo_t *const dimms, channel_mode_t mode)
1302{
1303 /* Never use dual-interleaved mode in pre-jedec config. */
1304 if (CHANNEL_MODE_DUAL_INTERLEAVED == mode)
1305 mode = CHANNEL_MODE_DUAL_ASYNC;
1306
Vladimir Serbinenko56ae8a02014-08-16 10:59:02 +02001307 program_memory_map(dimms, mode, 1, 0);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001308 MCHBAR32(DCC_MCHBAR) |= DCC_NO_CHANXOR;
1309}
1310
1311static void ddr3_select_clock_mux(const mem_clock_t ddr3clock,
1312 const dimminfo_t *const dimms,
1313 const stepping_t stepping)
1314{
1315 const int clk1067 = (ddr3clock == MEM_CLOCK_1067MT);
1316 const int cardF[] = { CHANNEL_IS_CARDF(dimms, 0), CHANNEL_IS_CARDF(dimms, 1) };
1317
1318 int ch;
1319
1320 if (stepping < STEPPING_B1)
1321 die("Stepping <B1 unsupported in clock-multiplexer selection.\n");
1322
1323 FOR_EACH_POPULATED_CHANNEL(dimms, ch) {
1324 int mixed = 0;
1325 if ((1 == ch) && (!CHANNEL_IS_POPULATED(dimms, 0) || (cardF[0] != cardF[1])))
1326 mixed = 4 << 11;
1327 const unsigned int b = 0x14b0 + (ch * 0x0100);
1328 MCHBAR32(b+0x1c) = (MCHBAR32(b+0x1c) & ~(7 << 11)) |
1329 ((( cardF[ch])?1:0) << 11) | mixed;
1330 MCHBAR32(b+0x18) = (MCHBAR32(b+0x18) & ~(7 << 11)) | mixed;
1331 MCHBAR32(b+0x14) = (MCHBAR32(b+0x14) & ~(7 << 11)) |
1332 (((!clk1067 && !cardF[ch])?0:1) << 11) | mixed;
1333 MCHBAR32(b+0x10) = (MCHBAR32(b+0x10) & ~(7 << 11)) |
1334 ((( clk1067 && !cardF[ch])?1:0) << 11) | mixed;
1335 MCHBAR32(b+0x0c) = (MCHBAR32(b+0x0c) & ~(7 << 11)) |
1336 ((( cardF[ch])?3:2) << 11) | mixed;
1337 MCHBAR32(b+0x08) = (MCHBAR32(b+0x08) & ~(7 << 11)) |
1338 (2 << 11) | mixed;
1339 MCHBAR32(b+0x04) = (MCHBAR32(b+0x04) & ~(7 << 11)) |
1340 (((!clk1067 && !cardF[ch])?2:3) << 11) | mixed;
1341 MCHBAR32(b+0x00) = (MCHBAR32(b+0x00) & ~(7 << 11)) |
1342 ((( clk1067 && !cardF[ch])?3:2) << 11) | mixed;
1343 }
1344}
1345static void ddr3_write_io_init(const mem_clock_t ddr3clock,
1346 const dimminfo_t *const dimms,
1347 const stepping_t stepping,
1348 const int sff)
1349{
1350 const int a1step = stepping >= STEPPING_CONVERSION_A1;
1351 const int cardF[] = { CHANNEL_IS_CARDF(dimms, 0), CHANNEL_IS_CARDF(dimms, 1) };
1352
1353 int ch;
1354
1355 if (stepping < STEPPING_B1)
1356 die("Stepping <B1 unsupported in write i/o initialization.\n");
1357 if (sff)
1358 die("SFF platform unsupported in write i/o initialization.\n");
1359
1360 static const u32 ddr3_667_800_by_stepping_ddr3_and_card[][2][2][4] = {
1361 { /* Stepping B3 and below */
1362 { /* 667 MHz */
1363 { 0xa3255008, 0x26888209, 0x26288208, 0x6188040f },
1364 { 0x7524240b, 0xa5255608, 0x232b8508, 0x5528040f },
1365 },
1366 { /* 800 MHz */
1367 { 0xa6255308, 0x26888209, 0x212b7508, 0x6188040f },
1368 { 0x7524240b, 0xa6255708, 0x132b7508, 0x5528040f },
1369 },
1370 },
1371 { /* Conversion stepping A1 and above */
1372 { /* 667 MHz */
1373 { 0xc5257208, 0x26888209, 0x26288208, 0x6188040f },
1374 { 0x7524240b, 0xc5257608, 0x232b8508, 0x5528040f },
1375 },
1376 { /* 800 MHz */
1377 { 0xb6256308, 0x26888209, 0x212b7508, 0x6188040f },
1378 { 0x7524240b, 0xb6256708, 0x132b7508, 0x5528040f },
1379 }
1380 }};
1381
1382 static const u32 ddr3_1067_by_channel_and_card[][2][4] = {
1383 { /* Channel A */
1384 { 0xb2254708, 0x002b7408, 0x132b8008, 0x7228060f },
1385 { 0xb0255008, 0xa4254108, 0x4528b409, 0x9428230f },
1386 },
1387 { /* Channel B */
1388 { 0xa4254208, 0x022b6108, 0x132b8208, 0x9228210f },
1389 { 0x6024140b, 0x92244408, 0x252ba409, 0x9328360c },
1390 },
1391 };
1392
1393 FOR_EACH_POPULATED_CHANNEL(dimms, ch) {
1394 if ((1 == ch) && CHANNEL_IS_POPULATED(dimms, 0) && (cardF[0] == cardF[1]))
1395 /* Only write if second channel population differs. */
1396 continue;
1397 const u32 *const data = (ddr3clock != MEM_CLOCK_1067MT)
1398 ? ddr3_667_800_by_stepping_ddr3_and_card[a1step][2 - ddr3clock][cardF[ch]]
1399 : ddr3_1067_by_channel_and_card[ch][cardF[ch]];
1400 MCHBAR32(CxWRTy_MCHBAR(ch, 0)) = data[0];
1401 MCHBAR32(CxWRTy_MCHBAR(ch, 1)) = data[1];
1402 MCHBAR32(CxWRTy_MCHBAR(ch, 2)) = data[2];
1403 MCHBAR32(CxWRTy_MCHBAR(ch, 3)) = data[3];
1404 }
1405
1406 MCHBAR32(0x1490) = 0x00e70067;
1407 MCHBAR32(0x1494) = 0x000d8000;
1408 MCHBAR32(0x1590) = 0x00e70067;
1409 MCHBAR32(0x1594) = 0x000d8000;
1410}
1411static void ddr3_read_io_init(const mem_clock_t ddr3clock,
1412 const dimminfo_t *const dimms,
1413 const int sff)
1414{
1415 int ch;
1416
1417 FOR_EACH_POPULATED_CHANNEL(dimms, ch) {
1418 u32 addr, tmp;
1419 const unsigned int base = 0x14b0 + (ch * 0x0100);
1420 for (addr = base + 0x1c; addr >= base; addr -= 4) {
1421 tmp = MCHBAR32(addr);
1422 tmp &= ~((3 << 25) | (1 << 8) | (7 << 16) | (0xf << 20) | (1 << 27));
1423 tmp |= (1 << 27);
1424 switch (ddr3clock) {
1425 case MEM_CLOCK_667MT:
1426 tmp |= (1 << 16) | (4 << 20);
1427 break;
1428 case MEM_CLOCK_800MT:
1429 tmp |= (2 << 16) | (3 << 20);
1430 break;
1431 case MEM_CLOCK_1067MT:
1432 if (!sff)
1433 tmp |= (2 << 16) | (1 << 20);
1434 else
1435 tmp |= (2 << 16) | (2 << 20);
1436 break;
1437 default:
1438 die("Wrong clock");
1439 }
1440 MCHBAR32(addr) = tmp;
1441 }
1442 }
1443}
1444
1445static void memory_io_init(const mem_clock_t ddr3clock,
1446 const dimminfo_t *const dimms,
1447 const stepping_t stepping,
1448 const int sff)
1449{
1450 u32 tmp;
1451
1452 if (stepping < STEPPING_B1)
1453 die("Stepping <B1 unsupported in "
1454 "system-memory i/o initialization.\n");
1455
1456 tmp = MCHBAR32(0x1400);
1457 tmp &= ~(3<<13);
1458 tmp |= (1<<9) | (1<<13);
1459 MCHBAR32(0x1400) = tmp;
1460
1461 tmp = MCHBAR32(0x140c);
1462 tmp &= ~(0xff | (1<<11) | (1<<12) |
1463 (1<<16) | (1<<18) | (1<<27) | (0xf<<28));
1464 tmp |= (1<<7) | (1<<11) | (1<<16);
1465 switch (ddr3clock) {
1466 case MEM_CLOCK_667MT:
1467 tmp |= 9 << 28;
1468 break;
1469 case MEM_CLOCK_800MT:
1470 tmp |= 7 << 28;
1471 break;
1472 case MEM_CLOCK_1067MT:
1473 tmp |= 8 << 28;
1474 break;
1475 }
1476 MCHBAR32(0x140c) = tmp;
1477
1478 MCHBAR32(0x1440) &= ~1;
1479
1480 tmp = MCHBAR32(0x1414);
1481 tmp &= ~((1<<20) | (7<<11) | (0xf << 24) | (0xf << 16));
1482 tmp |= (3<<11);
1483 switch (ddr3clock) {
1484 case MEM_CLOCK_667MT:
1485 tmp |= (2 << 24) | (10 << 16);
1486 break;
1487 case MEM_CLOCK_800MT:
1488 tmp |= (3 << 24) | (7 << 16);
1489 break;
1490 case MEM_CLOCK_1067MT:
1491 tmp |= (4 << 24) | (4 << 16);
1492 break;
1493 }
1494 MCHBAR32(0x1414) = tmp;
1495
1496 MCHBAR32(0x1418) &= ~((1<<3) | (1<<11) | (1<<19) | (1<<27));
1497
1498 MCHBAR32(0x141c) &= ~((1<<3) | (1<<11) | (1<<19) | (1<<27));
1499
1500 MCHBAR32(0x1428) |= 1<<14;
1501
1502 tmp = MCHBAR32(0x142c);
1503 tmp &= ~((0xf << 8) | (0x7 << 20) | 0xf | (0xf << 24));
1504 tmp |= (0x3 << 20) | (5 << 24);
1505 switch (ddr3clock) {
1506 case MEM_CLOCK_667MT:
1507 tmp |= (2 << 8) | 0xc;
1508 break;
1509 case MEM_CLOCK_800MT:
1510 tmp |= (3 << 8) | 0xa;
1511 break;
1512 case MEM_CLOCK_1067MT:
1513 tmp |= (4 << 8) | 0x7;
1514 break;
1515 }
1516 MCHBAR32(0x142c) = tmp;
1517
1518 tmp = MCHBAR32(0x400);
1519 tmp &= ~((3 << 4) | (3 << 16) | (3 << 30));
1520 tmp |= (2 << 4) | (2 << 16);
1521 MCHBAR32(0x400) = tmp;
1522
1523 MCHBAR32(0x404) &= ~(0xf << 20);
1524
1525 MCHBAR32(0x40c) &= ~(1 << 6);
1526
1527 tmp = MCHBAR32(0x410);
1528 tmp &= ~(7 << 28);
1529 tmp |= 2 << 28;
1530 MCHBAR32(0x410) = tmp;
1531
1532 tmp = MCHBAR32(0x41c);
1533 tmp &= ~0x77;
1534 tmp |= 0x11;
1535 MCHBAR32(0x41c) = tmp;
1536
1537 ddr3_select_clock_mux(ddr3clock, dimms, stepping);
1538
1539 ddr3_write_io_init(ddr3clock, dimms, stepping, sff);
1540
1541 ddr3_read_io_init(ddr3clock, dimms, sff);
1542}
1543
1544static void jedec_init(const timings_t *const timings,
1545 const dimminfo_t *const dimms)
1546{
1547 if ((timings->tWR < 5) || (timings->tWR > 12))
1548 die("tWR value unsupported in Jedec initialization.\n");
1549
1550 /* Pre-jedec settings */
1551 MCHBAR32(0x40) |= (1 << 1);
1552 MCHBAR32(0x230) |= (3 << 1);
1553 MCHBAR32(0x238) |= (3 << 24);
1554 MCHBAR32(0x23c) |= (3 << 24);
1555
1556 /* Normal write pointer operation */
1557 MCHBAR32(0x14f0) |= (1 << 9);
1558 MCHBAR32(0x15f0) |= (1 << 9);
1559
1560 MCHBAR32(DCC_MCHBAR) = (MCHBAR32(DCC_MCHBAR) & ~DCC_CMD_MASK) | DCC_CMD_NOP;
1561
1562 u8 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0xf0);
1563 pci_write_config8(PCI_DEV(0, 0, 0), 0xf0, reg8 & ~(1 << 2));
1564 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0xf0);
1565 pci_write_config8(PCI_DEV(0, 0, 0), 0xf0, reg8 | (1 << 2));
1566 udelay(2);
1567
1568 /* 5 6 7 8 9 10 11 12 */
1569 static const u8 wr_lut[] = { 1, 2, 3, 4, 5, 5, 6, 6 };
1570
1571 const int WL = ((timings->tWL - 5) & 7) << 6;
1572 const int ODT_120OHMS = (1 << 9);
1573 const int ODS_34OHMS = (1 << 4);
1574 const int WR = (wr_lut[timings->tWR - 5] & 7) << 12;
1575 const int DLL1 = 1 << 11;
1576 const int CAS = ((timings->CAS - 4) & 7) << 7;
1577 const int INTERLEAVED = 1 << 6;/* This is READ Burst Type == interleaved. */
1578
1579 int ch, r;
1580 FOR_EACH_POPULATED_RANK(dimms, ch, r) {
1581 /* We won't do this in dual-interleaved mode,
Felix Held7f9f3d02019-06-07 14:47:28 +02001582 so don't care about the offset.
1583 Mirrored ranks aren't taken into account here. */
Patrick Georgi2efc8802012-11-06 11:03:53 +01001584 const u32 rankaddr = raminit_get_rank_addr(ch, r);
Nico Huber0624f922017-04-15 15:57:28 +02001585 printk(BIOS_DEBUG, "JEDEC init @0x%08x\n", rankaddr);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001586 MCHBAR32(DCC_MCHBAR) = (MCHBAR32(DCC_MCHBAR) & ~DCC_SET_EREG_MASK) | DCC_SET_EREGx(2);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001587 read32((u32 *)(rankaddr | WL));
Patrick Georgi2efc8802012-11-06 11:03:53 +01001588 MCHBAR32(DCC_MCHBAR) = (MCHBAR32(DCC_MCHBAR) & ~DCC_SET_EREG_MASK) | DCC_SET_EREGx(3);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001589 read32((u32 *)rankaddr);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001590 MCHBAR32(DCC_MCHBAR) = (MCHBAR32(DCC_MCHBAR) & ~DCC_SET_EREG_MASK) | DCC_SET_EREGx(1);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001591 read32((u32 *)(rankaddr | ODT_120OHMS | ODS_34OHMS));
Patrick Georgi2efc8802012-11-06 11:03:53 +01001592 MCHBAR32(DCC_MCHBAR) = (MCHBAR32(DCC_MCHBAR) & ~DCC_CMD_MASK) | DCC_SET_MREG;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001593 read32((u32 *)(rankaddr | WR | DLL1 | CAS | INTERLEAVED));
Patrick Georgi2efc8802012-11-06 11:03:53 +01001594 MCHBAR32(DCC_MCHBAR) = (MCHBAR32(DCC_MCHBAR) & ~DCC_CMD_MASK) | DCC_SET_MREG;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001595 read32((u32 *)(rankaddr | WR | CAS | INTERLEAVED));
Patrick Georgi2efc8802012-11-06 11:03:53 +01001596 }
1597}
1598
1599static void ddr3_calibrate_zq(void) {
1600 udelay(2);
1601
1602 u32 tmp = MCHBAR32(DCC_MCHBAR);
1603 tmp &= ~(7 << 16);
1604 tmp |= (5 << 16); /* ZQ calibration mode */
1605 MCHBAR32(DCC_MCHBAR) = tmp;
1606
1607 MCHBAR32(CxDRT6_MCHBAR(0)) |= (1 << 3);
1608 MCHBAR32(CxDRT6_MCHBAR(1)) |= (1 << 3);
1609
1610 udelay(1);
1611
1612 MCHBAR32(CxDRT6_MCHBAR(0)) &= ~(1 << 3);
1613 MCHBAR32(CxDRT6_MCHBAR(1)) &= ~(1 << 3);
1614
1615 MCHBAR32(DCC_MCHBAR) |= (7 << 16); /* Normal operation */
1616}
1617
1618static void post_jedec_sequence(const int cores) {
1619 const int quadcore = cores == 4;
1620
1621 MCHBAR32(0x0040) &= ~(1 << 1);
1622 MCHBAR32(0x0230) &= ~(3 << 1);
1623 MCHBAR32(0x0230) |= 1 << 15;
1624 MCHBAR32(0x0230) &= ~(1 << 19);
1625 MCHBAR32(0x1250) = 0x6c4;
1626 MCHBAR32(0x1350) = 0x6c4;
1627 MCHBAR32(0x1254) = 0x871a066d;
1628 MCHBAR32(0x1354) = 0x871a066d;
1629 MCHBAR32(0x0238) |= 1 << 26;
1630 MCHBAR32(0x0238) &= ~(3 << 24);
1631 MCHBAR32(0x0238) |= 1 << 23;
1632 MCHBAR32(0x0238) = (MCHBAR32(0x238) & ~(7 << 20)) | (3 << 20);
1633 MCHBAR32(0x0238) = (MCHBAR32(0x238) & ~(7 << 17)) | (6 << 17);
1634 MCHBAR32(0x0238) = (MCHBAR32(0x238) & ~(7 << 14)) | (6 << 14);
1635 MCHBAR32(0x0238) = (MCHBAR32(0x238) & ~(7 << 11)) | (6 << 11);
1636 MCHBAR32(0x0238) = (MCHBAR32(0x238) & ~(7 << 8)) | (6 << 8);
1637 MCHBAR32(0x023c) &= ~(3 << 24);
1638 MCHBAR32(0x023c) &= ~(1 << 23);
1639 MCHBAR32(0x023c) = (MCHBAR32(0x23c) & ~(7 << 20)) | (3 << 20);
1640 MCHBAR32(0x023c) = (MCHBAR32(0x23c) & ~(7 << 17)) | (6 << 17);
1641 MCHBAR32(0x023c) = (MCHBAR32(0x23c) & ~(7 << 14)) | (6 << 14);
1642 MCHBAR32(0x023c) = (MCHBAR32(0x23c) & ~(7 << 11)) | (6 << 11);
1643 MCHBAR32(0x023c) = (MCHBAR32(0x23c) & ~(7 << 8)) | (6 << 8);
1644
1645 if (quadcore) {
1646 MCHBAR32(0xb14) |= (0xbfbf << 16);
1647 }
1648}
1649
1650static void dram_optimizations(const timings_t *const timings,
1651 const dimminfo_t *const dimms)
1652{
1653 int ch;
1654
1655 FOR_EACH_POPULATED_CHANNEL(dimms, ch) {
1656 const unsigned int mchbar = CxDRC1_MCHBAR(ch);
1657 u32 cxdrc1 = MCHBAR32(mchbar);
1658 cxdrc1 &= ~CxDRC1_SSDS_MASK;
1659 if (dimms[ch].ranks == 1)
1660 cxdrc1 |= CxDRC1_SS;
1661 else
1662 cxdrc1 |= CxDRC1_DS;
1663 MCHBAR32(mchbar) = cxdrc1;
1664 }
1665}
1666
1667u32 raminit_get_rank_addr(unsigned int channel, unsigned int rank)
1668{
1669 if (!channel && !rank)
1670 return 0; /* Address of first rank */
1671
1672 /* Read the bound of the previous rank. */
1673 if (rank > 0) {
1674 rank--;
1675 } else {
1676 rank = 3; /* Highest rank per channel */
1677 channel--;
1678 }
1679 const u32 reg = MCHBAR32(CxDRBy_MCHBAR(channel, rank));
1680 /* Bound is in 32MB. */
1681 return ((reg & CxDRBy_BOUND_MASK(rank)) >> CxDRBy_BOUND_SHIFT(rank)) << 25;
1682}
1683
1684void raminit_reset_readwrite_pointers(void) {
1685 MCHBAR32(0x1234) |= (1 << 6);
1686 MCHBAR32(0x1234) &= ~(1 << 6);
1687 MCHBAR32(0x1334) |= (1 << 6);
1688 MCHBAR32(0x1334) &= ~(1 << 6);
1689 MCHBAR32(0x14f0) &= ~(1 << 9);
1690 MCHBAR32(0x14f0) |= (1 << 9);
1691 MCHBAR32(0x14f0) |= (1 << 10);
1692 MCHBAR32(0x15f0) &= ~(1 << 9);
1693 MCHBAR32(0x15f0) |= (1 << 9);
1694 MCHBAR32(0x15f0) |= (1 << 10);
1695}
1696
1697void raminit(sysinfo_t *const sysinfo, const int s3resume)
1698{
1699 const dimminfo_t *const dimms = sysinfo->dimms;
1700 const timings_t *const timings = &sysinfo->selected_timings;
Patrick Georgi2efc8802012-11-06 11:03:53 +01001701
1702 int ch;
1703 u8 reg8;
1704
Arthur Heymans049347f2017-05-12 11:54:08 +02001705 timestamp_add_now(TS_BEFORE_INITRAM);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001706
1707 /* Wait for some bit, maybe TXT clear. */
1708 if (sysinfo->txt_enabled) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -08001709 while (!(read8((u8 *)0xfed40000) & (1 << 7))) {}
Patrick Georgi2efc8802012-11-06 11:03:53 +01001710 }
1711
Patrick Georgi2efc8802012-11-06 11:03:53 +01001712 /* Collect information about DIMMs and find common settings. */
1713 collect_dimm_config(sysinfo);
1714
1715 /* Check for bad warm boot. */
1716 reset_on_bad_warmboot();
1717
1718
1719 /***** From now on, program according to collected infos: *****/
1720
1721 /* Program DRAM type. */
1722 switch (sysinfo->spd_type) {
1723 case DDR2:
1724 MCHBAR8(0x1434) |= (1 << 7);
1725 break;
1726 case DDR3:
1727 MCHBAR8(0x1434) |= (3 << 0);
1728 break;
1729 }
1730
1731 /* Program system memory frequency. */
1732 set_system_memory_frequency(timings);
1733 /* Program IGD memory frequency. */
1734 set_igd_memory_frequencies(sysinfo);
1735
1736 /* Configure DRAM control mode for populated channels. */
1737 configure_dram_control_mode(timings, dimms);
1738
1739 /* Initialize RCOMP. */
Nico Huber5aaeb272015-12-30 00:17:27 +01001740 rcomp_initialization(sysinfo->stepping, sysinfo->sff);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001741
1742 /* Power-up DRAM. */
1743 dram_powerup(s3resume);
1744 /* Program DRAM timings. */
1745 dram_program_timings(timings);
1746 /* Program number of banks. */
1747 dram_program_banks(dimms);
1748 /* Enable DRAM clock pairs for populated DIMMs. */
1749 FOR_EACH_POPULATED_CHANNEL(dimms, ch)
1750 MCHBAR32(CxDCLKDIS_MCHBAR(ch)) |= CxDCLKDIS_ENABLE;
1751
1752 /* Enable On-Die Termination. */
Nico Huber5aaeb272015-12-30 00:17:27 +01001753 odt_setup(timings, sysinfo->sff);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001754 /* Miscellaneous settings. */
1755 misc_settings(timings, sysinfo->stepping);
1756 /* Program clock crossing registers. */
1757 clock_crossing_setup(timings->fsb_clock, timings->mem_clock, dimms);
1758 /* Program egress VC1 timings. */
1759 vc1_program_timings(timings->fsb_clock);
1760 /* Perform system-memory i/o initialization. */
Nico Huber5aaeb272015-12-30 00:17:27 +01001761 memory_io_init(timings->mem_clock, dimms,
1762 sysinfo->stepping, sysinfo->sff);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001763
1764 /* Initialize memory map with dummy values of 128MB per rank with a
1765 page size of 4KB. This makes the JEDEC initialization code easier. */
1766 prejedec_memory_map(dimms, timings->channel_mode);
1767 if (!s3resume)
1768 /* Perform JEDEC initialization of DIMMS. */
1769 jedec_init(timings, dimms);
1770 /* Some programming steps after JEDEC initialization. */
1771 post_jedec_sequence(sysinfo->cores);
1772
1773 /* Announce normal operation, initialization completed. */
1774 MCHBAR32(DCC_MCHBAR) |= (0x7 << 16) | (0x1 << 19);
1775 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0xf0);
1776 pci_write_config8(PCI_DEV(0, 0, 0), 0xf0, reg8 | (1 << 2));
1777 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0xf0);
1778 pci_write_config8(PCI_DEV(0, 0, 0), 0xf0, reg8 & ~(1 << 2));
1779
1780
1781 /* Take a breath (the reader). */
1782
1783
1784 /* Perform ZQ calibration for DDR3. */
Nico Huber4a86b3b2019-08-11 16:28:05 +02001785 if (sysinfo->spd_type == DDR3)
1786 ddr3_calibrate_zq();
Patrick Georgi2efc8802012-11-06 11:03:53 +01001787
1788 /* Perform receive-enable calibration. */
1789 raminit_receive_enable_calibration(timings, dimms);
1790 /* Lend clock values from receive-enable calibration. */
Jonathan Neuschäfer2f828eb2018-02-12 12:00:44 +01001791 MCHBAR32(CxDRT5_MCHBAR(0)) =
1792 (MCHBAR32(CxDRT5_MCHBAR(0)) & ~(0xf0)) |
1793 ((((MCHBAR32(CxDRT3_MCHBAR(0)) >> 7) - 1) & 0xf) << 4);
1794 MCHBAR32(CxDRT5_MCHBAR(1)) =
1795 (MCHBAR32(CxDRT5_MCHBAR(1)) & ~(0xf0)) |
1796 ((((MCHBAR32(CxDRT3_MCHBAR(1)) >> 7) - 1) & 0xf) << 4);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001797
1798 /* Perform read/write training for high clock rate. */
1799 if (timings->mem_clock == MEM_CLOCK_1067MT) {
1800 raminit_read_training(dimms, s3resume);
1801 raminit_write_training(timings->mem_clock, dimms, s3resume);
1802 }
1803
Vladimir Serbinenko56ae8a02014-08-16 10:59:02 +02001804 igd_compute_ggc(sysinfo);
1805
Patrick Georgi2efc8802012-11-06 11:03:53 +01001806 /* Program final memory map (with real values). */
Vladimir Serbinenko56ae8a02014-08-16 10:59:02 +02001807 program_memory_map(dimms, timings->channel_mode, 0, sysinfo->ggc);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001808
1809 /* Some last optimizations. */
1810 dram_optimizations(timings, dimms);
1811
Elyes HAOUAS3d450002018-08-09 18:55:58 +02001812 /* Mark raminit being finished. :-) */
Patrick Georgi2efc8802012-11-06 11:03:53 +01001813 u8 tmp8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xa2) & ~(1 << 7);
1814 pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xa2, tmp8);
Vladimir Serbinenko56ae8a02014-08-16 10:59:02 +02001815
1816 raminit_thermal(sysinfo);
1817 init_igd(sysinfo);
Arthur Heymans049347f2017-05-12 11:54:08 +02001818
1819 timestamp_add_now(TS_AFTER_INITRAM);
Patrick Georgi2efc8802012-11-06 11:03:53 +01001820}