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