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