blob: 0f72d888132fbd680110748b8de0e7046f7b9973 [file] [log] [blame]
Stefan Reinauer00636b02012-04-04 00:08:51 +02001/*
2 * This file is part of the coreboot project.
3 *
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004 * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
5 * Copyright (C) 2014 Vladimir Serbinenko <phcoder@gmail.com>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01006 * Copyright (C) 2016 Patrick Rudolph <siro@das-labor.org>
Stefan Reinauer00636b02012-04-04 00:08:51 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Stefan Reinauer00636b02012-04-04 00:08:51 +020016 */
17
18#include <console/console.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010019#include <commonlib/region.h>
Kyösti Mälkki5687fc92013-11-28 18:11:49 +020020#include <bootmode.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020021#include <string.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020022#include <arch/io.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020023#include <cbmem.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070024#include <halt.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010025#include <timestamp.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010026#include <mrc_cache.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010027#include <southbridge/intel/bd82x6x/me.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +020028#include <southbridge/intel/common/smbus.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010029#include <cpu/x86/msr.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070030#include <delay.h>
31#include <lib.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032#include "raminit_native.h"
33#include "raminit_common.h"
34#include "sandybridge.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020035
Patrick Rudolph708cf4b2018-07-29 12:34:03 +020036#define MRC_CACHE_VERSION 1
Arthur Heymans7539b8c2017-12-24 10:42:57 +010037
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070038/* FIXME: no ECC support. */
39/* FIXME: no support for 3-channel chipsets. */
Stefan Reinauer00636b02012-04-04 00:08:51 +020040
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070041static const char *ecc_decoder[] = {
Stefan Reinauer00636b02012-04-04 00:08:51 +020042 "inactive",
43 "active on IO",
44 "disabled on IO",
45 "active"
46};
47
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070048static void wait_txt_clear(void)
49{
50 struct cpuid_result cp;
51
52 cp = cpuid_ext(0x1, 0x0);
53 /* Check if TXT is supported? */
54 if (!(cp.ecx & 0x40))
55 return;
56 /* Some TXT public bit. */
57 if (!(read32((void *)0xfed30010) & 1))
58 return;
59 /* Wait for TXT clear. */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +020060 while (!(read8((void *)0xfed40000) & (1 << 7)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070061}
62
Stefan Reinauer00636b02012-04-04 00:08:51 +020063/*
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010064 * Disable a channel in ramctr_timing.
65 */
66static void disable_channel(ramctr_timing *ctrl, int channel) {
67 ctrl->rankmap[channel] = 0;
68 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
69 ctrl->channel_size_mb[channel] = 0;
70 ctrl->cmd_stretch[channel] = 0;
71 ctrl->mad_dimm[channel] = 0;
72 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
Patrick Rudolph74163d62016-11-17 20:02:43 +010073 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010074}
75
76/*
Patrick Rudolphb97009e2016-02-28 15:24:04 +010077 * Fill cbmem with information for SMBIOS type 17.
78 */
Patrick Rudolph735ecce2016-03-26 10:42:27 +010079static void fill_smbios17(ramctr_timing *ctrl)
Patrick Rudolphb97009e2016-02-28 15:24:04 +010080{
Patrick Rudolphb97009e2016-02-28 15:24:04 +010081 int channel, slot;
Patrick Rudolph24efe732018-08-19 11:06:06 +020082 const u16 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +010083
Elyes HAOUAS12df9502016-08-23 21:29:48 +020084 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolph24efe732018-08-19 11:06:06 +020085 enum cb_err ret = spd_add_smbios17(channel, slot, ddr_freq,
86 &ctrl->info.dimm[channel][slot]);
87 if (ret != CB_SUCCESS)
88 printk(BIOS_ERR, "RAMINIT: Failed to add SMBIOS17\n");
Patrick Rudolphb97009e2016-02-28 15:24:04 +010089 }
90}
91
92/*
Stefan Reinauer00636b02012-04-04 00:08:51 +020093 * Dump in the log memory controller configuration as read from the memory
94 * controller registers.
95 */
96static void report_memory_config(void)
97{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070098 u32 addr_decoder_common, addr_decode_ch[NUM_CHANNELS];
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +020099 int i, refclk;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200100
101 addr_decoder_common = MCHBAR32(0x5000);
102 addr_decode_ch[0] = MCHBAR32(0x5004);
103 addr_decode_ch[1] = MCHBAR32(0x5008);
104
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200105 refclk = MCHBAR32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
106
107 printk(BIOS_DEBUG, "memcfg DDR3 ref clock %d MHz\n", refclk);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200108 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200109 (MCHBAR32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200110 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700111 addr_decoder_common & 3, (addr_decoder_common >> 2) & 3,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200112 (addr_decoder_common >> 4) & 3);
113
114 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
115 u32 ch_conf = addr_decode_ch[i];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700116 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i,
117 ch_conf);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200118 printk(BIOS_DEBUG, " ECC %s\n",
119 ecc_decoder[(ch_conf >> 24) & 3]);
120 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
121 ((ch_conf >> 22) & 1) ? "on" : "off");
122 printk(BIOS_DEBUG, " rank interleave %s\n",
123 ((ch_conf >> 21) & 1) ? "on" : "off");
124 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
125 ((ch_conf >> 0) & 0xff) * 256,
126 ((ch_conf >> 19) & 1) ? 16 : 8,
127 ((ch_conf >> 17) & 1) ? "dual" : "single",
128 ((ch_conf >> 16) & 1) ? "" : ", selected");
129 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
130 ((ch_conf >> 8) & 0xff) * 256,
131 ((ch_conf >> 20) & 1) ? 16 : 8,
132 ((ch_conf >> 18) & 1) ? "dual" : "single",
133 ((ch_conf >> 16) & 1) ? ", selected" : "");
134 }
135}
136
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100137/*
138 * Return CRC16 match for all SPDs.
139 */
140static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
141{
142 int channel, slot, spd_slot;
143 int match = 1;
144
145 FOR_ALL_CHANNELS {
146 for (slot = 0; slot < NUM_SLOTS; slot++) {
147 spd_slot = 2 * channel + slot;
148 match &= ctrl->spd_crc[channel][slot] ==
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200149 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100150 }
151 }
152 return match;
153}
154
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200155void read_spd(spd_raw_data * spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200156{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700157 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200158 if (id_only) {
159 for (j = 117; j < 128; j++)
160 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
161 } else {
162 for (j = 0; j < 256; j++)
163 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
164 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700165}
166
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100167static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700168{
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100169 int dimms = 0, dimms_on_channel;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700170 int channel, slot, spd_slot;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100171 dimm_info *dimm = &ctrl->info;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700172
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200173 memset (ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700174
175 ctrl->extended_temperature_range = 1;
176 ctrl->auto_self_refresh = 1;
177
178 FOR_ALL_CHANNELS {
179 ctrl->channel_size_mb[channel] = 0;
180
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100181 dimms_on_channel = 0;
182 /* count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700183 for (slot = 0; slot < NUM_SLOTS; slot++) {
184 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200185 printk(BIOS_DEBUG,
186 "SPD probe channel%d, slot%d\n", channel, slot);
187
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700188 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100189 if (dimm->dimm[channel][slot].dram_type == SPD_MEMORY_TYPE_SDRAM_DDR3)
190 dimms_on_channel++;
191 }
192
193 for (slot = 0; slot < NUM_SLOTS; slot++) {
194 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200195 printk(BIOS_DEBUG,
196 "SPD probe channel%d, slot%d\n", channel, slot);
197
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100198 /* search for XMP profile */
199 spd_xmp_decode_ddr3(&dimm->dimm[channel][slot],
200 spd[spd_slot],
201 DDR3_XMP_PROFILE_1);
202
203 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
204 printram("No valid XMP profile found.\n");
205 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
206 } else if (dimms_on_channel > dimm->dimm[channel][slot].dimms_per_channel) {
207 printram("XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
208 dimm->dimm[channel][slot].dimms_per_channel,
209 dimms_on_channel);
Vagiz Trakhanov771be482017-10-02 10:02:35 +0000210 if (IS_ENABLED(CONFIG_NATIVE_RAMINIT_IGNORE_XMP_MAX_DIMMS))
211 printk(BIOS_WARNING, "XMP maximum DIMMs will be ignored.\n");
212 else
213 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100214 } else if (dimm->dimm[channel][slot].voltage != 1500) {
215 /* TODO: support other DDR3 voltage than 1500mV */
216 printram("XMP profile's requested %u mV is unsupported.\n",
217 dimm->dimm[channel][slot].voltage);
218 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
219 }
220
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100221 /* fill in CRC16 for MRC cache */
222 ctrl->spd_crc[channel][slot] =
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200223 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100224
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700225 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
226 // set dimm invalid
227 dimm->dimm[channel][slot].ranks = 0;
228 dimm->dimm[channel][slot].size_mb = 0;
229 continue;
230 }
231
232 dram_print_spd_ddr3(&dimm->dimm[channel][slot]);
233 dimms++;
234 ctrl->rank_mirror[channel][slot * 2] = 0;
235 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->dimm[channel][slot].flags.pins_mirrored;
236 ctrl->channel_size_mb[channel] += dimm->dimm[channel][slot].size_mb;
237
238 ctrl->auto_self_refresh &= dimm->dimm[channel][slot].flags.asr;
239 ctrl->extended_temperature_range &= dimm->dimm[channel][slot].flags.ext_temp_refresh;
240
241 ctrl->rankmap[channel] |= ((1 << dimm->dimm[channel][slot].ranks) - 1) << (2 * slot);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100242 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n",
243 channel, ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700244 }
245 if ((ctrl->rankmap[channel] & 3) && (ctrl->rankmap[channel] & 0xc)
246 && dimm->dimm[channel][0].reference_card <= 5 && dimm->dimm[channel][1].reference_card <= 5) {
247 const int ref_card_offset_table[6][6] = {
248 { 0, 0, 0, 0, 2, 2, },
249 { 0, 0, 0, 0, 2, 2, },
250 { 0, 0, 0, 0, 2, 2, },
251 { 0, 0, 0, 0, 1, 1, },
252 { 2, 2, 2, 1, 0, 0, },
253 { 2, 2, 2, 1, 0, 0, },
254 };
255 ctrl->ref_card_offset[channel] = ref_card_offset_table[dimm->dimm[channel][0].reference_card]
256 [dimm->dimm[channel][1].reference_card];
257 } else
258 ctrl->ref_card_offset[channel] = 0;
259 }
260
261 if (!dimms)
262 die("No DIMMs were found");
263}
264
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200265static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700266{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700267 /* Save the MRC S3 restore data to cbmem */
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100268 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, ctrl,
269 sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700270}
271
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100272static int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot,
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200273 int s3_resume, int me_uma_size)
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100274{
Patrick Rudolph305035c2016-11-11 18:38:50 +0100275 if (ctrl->sandybridge)
276 return try_init_dram_ddr3_sandy(ctrl, fast_boot, s3_resume, me_uma_size);
277 else
278 return try_init_dram_ddr3_ivy(ctrl, fast_boot, s3_resume, me_uma_size);
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100279}
280
Patrick Rudolph74203de2017-11-20 11:57:01 +0100281static void init_dram_ddr3(int min_tck, int s3resume)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700282{
283 int me_uma_size;
284 int cbmem_was_inited;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100285 ramctr_timing ctrl;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100286 int fast_boot;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200287 spd_raw_data spds[4];
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100288 struct region_device rdev;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100289 ramctr_timing *ctrl_cached;
Patrick Rudolph305035c2016-11-11 18:38:50 +0100290 struct cpuid_result cpures;
Patrick Rudolph31d19592016-03-26 12:22:34 +0100291 int err;
Patrick Rudolph305035c2016-11-11 18:38:50 +0100292 u32 cpu;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700293
294 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200295
296 /* Wait for ME to be ready */
297 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700298 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200299
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700300 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200301
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700302 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200303
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700304 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200305
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700306 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +0200307
Felix Held55823c32018-07-28 00:41:57 +0200308 reg_5d10 = MCHBAR32(0x5d10); // !!! = 0x00000000
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300309 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700310 && reg_5d10 && !s3resume) {
Felix Held55823c32018-07-28 00:41:57 +0200311 MCHBAR32(0x5d10) = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700312 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200313 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700314
Patrick Georgi546953c2014-11-29 10:38:17 +0100315 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200316 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200317
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700318 early_pch_init_native();
319 early_thermal_init();
320
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100321 /* try to find timings in MRC cache */
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100322 int cache_not_found = mrc_cache_get_current(MRC_TRAINING_DATA,
323 MRC_CACHE_VERSION, &rdev);
324 if (cache_not_found || (region_device_sz(&rdev) < sizeof(ctrl))) {
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100325 if (s3resume) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700326 /* Failed S3 resume, reset to come up cleanly */
327 outb(0x6, 0xcf9);
328 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200329 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100330 ctrl_cached = NULL;
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100331 } else {
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100332 ctrl_cached = rdev_mmap_full(&rdev);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700333 }
334
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100335 /* verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200336 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200337 /* Load SPD unique information data. */
338 memset(spds, 0, sizeof(spds));
339 mainboard_get_spd(spds, 1);
340
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100341 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
342 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
343 if (!fast_boot)
344 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200345 } else {
346 fast_boot = s3resume;
347 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100348
349 if (fast_boot) {
350 printk(BIOS_DEBUG, "Trying stored timings.\n");
351 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
352
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200353 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100354 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200355 if (s3resume) {
356 /* Failed S3 resume, reset to come up cleanly */
357 outb(0x6, 0xcf9);
358 halt();
359 }
360 /* no need to erase bad mrc cache here, it gets overwritten on
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100361 * successful boot. */
362 printk(BIOS_ERR, "Stored timings are invalid !\n");
363 fast_boot = 0;
364 }
365 }
366 if (!fast_boot) {
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100367 /* Reset internal state */
368 memset(&ctrl, 0, sizeof(ctrl));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100369 ctrl.tCK = min_tck;
370
Patrick Rudolph305035c2016-11-11 18:38:50 +0100371 /* Get architecture */
372 cpures = cpuid(1);
373 cpu = cpures.eax;
374 ctrl.sandybridge = IS_SANDY_CPU(cpu);
375
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100376 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200377 memset(spds, 0, sizeof(spds));
378 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100379 dram_find_spds_ddr3(spds, &ctrl);
380
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200381 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100382 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100383
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100384 if (err) {
385 /* fallback: disable failing channel */
386 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
387 printram("Disable failing channel.\n");
388
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100389 /* Reset internal state */
390 memset(&ctrl, 0, sizeof(ctrl));
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100391 ctrl.tCK = min_tck;
392
Patrick Rudolph305035c2016-11-11 18:38:50 +0100393 /* Get architecture */
394 cpures = cpuid(1);
395 cpu = cpures.eax;
396 ctrl.sandybridge = IS_SANDY_CPU(cpu);
397
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100398 /* Reset DDR3 frequency */
399 dram_find_spds_ddr3(spds, &ctrl);
400
401 /* disable failing channel */
402 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
403
404 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
405 }
406
Patrick Rudolph31d19592016-03-26 12:22:34 +0100407 if (err)
408 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700409
410 /* FIXME: should be hardware revision-dependent. */
Felix Held55823c32018-07-28 00:41:57 +0200411 MCHBAR32(0x5024) = 0x00a030ce;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700412
413 set_scrambling_seed(&ctrl);
414
415 set_42a0(&ctrl);
416
417 final_registers(&ctrl);
418
419 /* Zone config */
420 dram_zones(&ctrl, 0);
421
Patrick Rudolph77db3e12016-11-26 10:11:14 +0100422 /* Non intrusive, fast ram check */
423 quick_ram_check();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700424
425 intel_early_me_status();
426 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
427 intel_early_me_status();
428
Stefan Reinauer00636b02012-04-04 00:08:51 +0200429 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700430
431 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100432 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700433 save_timings(&ctrl);
434 if (s3resume && !cbmem_was_inited) {
435 /* Failed S3 resume, reset to come up cleanly */
436 outb(0x6, 0xcf9);
437 halt();
438 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100439
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100440 fill_smbios17(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200441}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100442
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100443void perform_raminit(int s3resume)
444{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100445 post_code(0x3a);
446
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100447 timestamp_add_now(TS_BEFORE_INITRAM);
448
Patrick Rudolph74203de2017-11-20 11:57:01 +0100449 init_dram_ddr3(get_mem_min_tck(), s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100450}