blob: b31f28042a5c4dfb3fd4f4853d847b039c017815 [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>
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020019#include <console/usb.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010020#include <commonlib/region.h>
Kyösti Mälkki5687fc92013-11-28 18:11:49 +020021#include <bootmode.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020022#include <string.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020023#include <arch/io.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020024#include <cbmem.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070025#include <halt.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010026#include <timestamp.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010027#include <mrc_cache.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010028#include <southbridge/intel/bd82x6x/me.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +020029#include <southbridge/intel/common/smbus.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010030#include <cpu/x86/msr.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070031#include <delay.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032#include <smbios.h>
33#include <memory_info.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070034#include <lib.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010035#include "raminit_native.h"
36#include "raminit_common.h"
37#include "sandybridge.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020038
Arthur Heymans7539b8c2017-12-24 10:42:57 +010039#define MRC_CACHE_VERSION 0
40
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070041/* FIXME: no ECC support. */
42/* FIXME: no support for 3-channel chipsets. */
Stefan Reinauer00636b02012-04-04 00:08:51 +020043
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070044static const char *ecc_decoder[] = {
Stefan Reinauer00636b02012-04-04 00:08:51 +020045 "inactive",
46 "active on IO",
47 "disabled on IO",
48 "active"
49};
50
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070051static void wait_txt_clear(void)
52{
53 struct cpuid_result cp;
54
55 cp = cpuid_ext(0x1, 0x0);
56 /* Check if TXT is supported? */
57 if (!(cp.ecx & 0x40))
58 return;
59 /* Some TXT public bit. */
60 if (!(read32((void *)0xfed30010) & 1))
61 return;
62 /* Wait for TXT clear. */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +020063 while (!(read8((void *)0xfed40000) & (1 << 7)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070064}
65
Stefan Reinauer00636b02012-04-04 00:08:51 +020066/*
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010067 * Disable a channel in ramctr_timing.
68 */
69static void disable_channel(ramctr_timing *ctrl, int channel) {
70 ctrl->rankmap[channel] = 0;
71 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
72 ctrl->channel_size_mb[channel] = 0;
73 ctrl->cmd_stretch[channel] = 0;
74 ctrl->mad_dimm[channel] = 0;
75 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
Patrick Rudolph74163d62016-11-17 20:02:43 +010076 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010077}
78
79/*
Patrick Rudolphb97009e2016-02-28 15:24:04 +010080 * Fill cbmem with information for SMBIOS type 17.
81 */
Patrick Rudolph735ecce2016-03-26 10:42:27 +010082static void fill_smbios17(ramctr_timing *ctrl)
Patrick Rudolphb97009e2016-02-28 15:24:04 +010083{
84 struct memory_info *mem_info;
85 int channel, slot;
86 struct dimm_info *dimm;
Patrick Rudolph735ecce2016-03-26 10:42:27 +010087 uint16_t ddr_freq;
88 dimm_info *info = &ctrl->info;
89
90 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +010091
92 /*
93 * Allocate CBMEM area for DIMM information used to populate SMBIOS
94 * table 17
95 */
96 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
97 printk(BIOS_DEBUG, "CBMEM entry for DIMM info: 0x%p\n", mem_info);
98 if (!mem_info)
99 return;
100
101 memset(mem_info, 0, sizeof(*mem_info));
102
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200103 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100104 dimm = &mem_info->dimm[mem_info->dimm_cnt];
105 if (info->dimm[channel][slot].size_mb) {
106 dimm->ddr_type = MEMORY_TYPE_DDR3;
107 dimm->ddr_frequency = ddr_freq;
108 dimm->dimm_size = info->dimm[channel][slot].size_mb;
109 dimm->channel_num = channel;
110 dimm->rank_per_dimm = info->dimm[channel][slot].ranks;
111 dimm->dimm_num = slot;
112 memcpy(dimm->module_part_number,
113 info->dimm[channel][slot].part_number, 16);
114 dimm->mod_id = info->dimm[channel][slot].manufacturer_id;
115 dimm->mod_type = info->dimm[channel][slot].dimm_type;
116 dimm->bus_width = info->dimm[channel][slot].width;
117 mem_info->dimm_cnt++;
118 }
119 }
120}
121
122/*
Stefan Reinauer00636b02012-04-04 00:08:51 +0200123 * Dump in the log memory controller configuration as read from the memory
124 * controller registers.
125 */
126static void report_memory_config(void)
127{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700128 u32 addr_decoder_common, addr_decode_ch[NUM_CHANNELS];
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200129 int i, refclk;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200130
131 addr_decoder_common = MCHBAR32(0x5000);
132 addr_decode_ch[0] = MCHBAR32(0x5004);
133 addr_decode_ch[1] = MCHBAR32(0x5008);
134
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200135 refclk = MCHBAR32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
136
137 printk(BIOS_DEBUG, "memcfg DDR3 ref clock %d MHz\n", refclk);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200138 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200139 (MCHBAR32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200140 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700141 addr_decoder_common & 3, (addr_decoder_common >> 2) & 3,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200142 (addr_decoder_common >> 4) & 3);
143
144 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
145 u32 ch_conf = addr_decode_ch[i];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700146 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i,
147 ch_conf);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200148 printk(BIOS_DEBUG, " ECC %s\n",
149 ecc_decoder[(ch_conf >> 24) & 3]);
150 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
151 ((ch_conf >> 22) & 1) ? "on" : "off");
152 printk(BIOS_DEBUG, " rank interleave %s\n",
153 ((ch_conf >> 21) & 1) ? "on" : "off");
154 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
155 ((ch_conf >> 0) & 0xff) * 256,
156 ((ch_conf >> 19) & 1) ? 16 : 8,
157 ((ch_conf >> 17) & 1) ? "dual" : "single",
158 ((ch_conf >> 16) & 1) ? "" : ", selected");
159 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
160 ((ch_conf >> 8) & 0xff) * 256,
161 ((ch_conf >> 20) & 1) ? 16 : 8,
162 ((ch_conf >> 18) & 1) ? "dual" : "single",
163 ((ch_conf >> 16) & 1) ? ", selected" : "");
164 }
165}
166
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100167/*
168 * Return CRC16 match for all SPDs.
169 */
170static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
171{
172 int channel, slot, spd_slot;
173 int match = 1;
174
175 FOR_ALL_CHANNELS {
176 for (slot = 0; slot < NUM_SLOTS; slot++) {
177 spd_slot = 2 * channel + slot;
178 match &= ctrl->spd_crc[channel][slot] ==
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200179 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100180 }
181 }
182 return match;
183}
184
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200185void read_spd(spd_raw_data * spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200186{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700187 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200188 if (id_only) {
189 for (j = 117; j < 128; j++)
190 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
191 } else {
192 for (j = 0; j < 256; j++)
193 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
194 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700195}
196
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100197static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700198{
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100199 int dimms = 0, dimms_on_channel;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700200 int channel, slot, spd_slot;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100201 dimm_info *dimm = &ctrl->info;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700202
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200203 memset (ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700204
205 ctrl->extended_temperature_range = 1;
206 ctrl->auto_self_refresh = 1;
207
208 FOR_ALL_CHANNELS {
209 ctrl->channel_size_mb[channel] = 0;
210
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100211 dimms_on_channel = 0;
212 /* count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700213 for (slot = 0; slot < NUM_SLOTS; slot++) {
214 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200215 printk(BIOS_DEBUG,
216 "SPD probe channel%d, slot%d\n", channel, slot);
217
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700218 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100219 if (dimm->dimm[channel][slot].dram_type == SPD_MEMORY_TYPE_SDRAM_DDR3)
220 dimms_on_channel++;
221 }
222
223 for (slot = 0; slot < NUM_SLOTS; slot++) {
224 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200225 printk(BIOS_DEBUG,
226 "SPD probe channel%d, slot%d\n", channel, slot);
227
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100228 /* search for XMP profile */
229 spd_xmp_decode_ddr3(&dimm->dimm[channel][slot],
230 spd[spd_slot],
231 DDR3_XMP_PROFILE_1);
232
233 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
234 printram("No valid XMP profile found.\n");
235 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
236 } else if (dimms_on_channel > dimm->dimm[channel][slot].dimms_per_channel) {
237 printram("XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
238 dimm->dimm[channel][slot].dimms_per_channel,
239 dimms_on_channel);
Vagiz Trakhanov771be482017-10-02 10:02:35 +0000240 if (IS_ENABLED(CONFIG_NATIVE_RAMINIT_IGNORE_XMP_MAX_DIMMS))
241 printk(BIOS_WARNING, "XMP maximum DIMMs will be ignored.\n");
242 else
243 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100244 } else if (dimm->dimm[channel][slot].voltage != 1500) {
245 /* TODO: support other DDR3 voltage than 1500mV */
246 printram("XMP profile's requested %u mV is unsupported.\n",
247 dimm->dimm[channel][slot].voltage);
248 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
249 }
250
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100251 /* fill in CRC16 for MRC cache */
252 ctrl->spd_crc[channel][slot] =
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200253 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100254
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700255 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
256 // set dimm invalid
257 dimm->dimm[channel][slot].ranks = 0;
258 dimm->dimm[channel][slot].size_mb = 0;
259 continue;
260 }
261
262 dram_print_spd_ddr3(&dimm->dimm[channel][slot]);
263 dimms++;
264 ctrl->rank_mirror[channel][slot * 2] = 0;
265 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->dimm[channel][slot].flags.pins_mirrored;
266 ctrl->channel_size_mb[channel] += dimm->dimm[channel][slot].size_mb;
267
268 ctrl->auto_self_refresh &= dimm->dimm[channel][slot].flags.asr;
269 ctrl->extended_temperature_range &= dimm->dimm[channel][slot].flags.ext_temp_refresh;
270
271 ctrl->rankmap[channel] |= ((1 << dimm->dimm[channel][slot].ranks) - 1) << (2 * slot);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100272 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n",
273 channel, ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700274 }
275 if ((ctrl->rankmap[channel] & 3) && (ctrl->rankmap[channel] & 0xc)
276 && dimm->dimm[channel][0].reference_card <= 5 && dimm->dimm[channel][1].reference_card <= 5) {
277 const int ref_card_offset_table[6][6] = {
278 { 0, 0, 0, 0, 2, 2, },
279 { 0, 0, 0, 0, 2, 2, },
280 { 0, 0, 0, 0, 2, 2, },
281 { 0, 0, 0, 0, 1, 1, },
282 { 2, 2, 2, 1, 0, 0, },
283 { 2, 2, 2, 1, 0, 0, },
284 };
285 ctrl->ref_card_offset[channel] = ref_card_offset_table[dimm->dimm[channel][0].reference_card]
286 [dimm->dimm[channel][1].reference_card];
287 } else
288 ctrl->ref_card_offset[channel] = 0;
289 }
290
291 if (!dimms)
292 die("No DIMMs were found");
293}
294
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200295static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700296{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700297 /* Save the MRC S3 restore data to cbmem */
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100298 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, ctrl,
299 sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700300}
301
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100302static int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot,
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200303 int s3_resume, int me_uma_size)
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100304{
Patrick Rudolph305035c2016-11-11 18:38:50 +0100305 if (ctrl->sandybridge)
306 return try_init_dram_ddr3_sandy(ctrl, fast_boot, s3_resume, me_uma_size);
307 else
308 return try_init_dram_ddr3_ivy(ctrl, fast_boot, s3_resume, me_uma_size);
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100309}
310
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200311static void init_dram_ddr3(int mobile, int min_tck, int s3resume)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700312{
313 int me_uma_size;
314 int cbmem_was_inited;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100315 ramctr_timing ctrl;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100316 int fast_boot;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200317 spd_raw_data spds[4];
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100318 struct region_device rdev;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100319 ramctr_timing *ctrl_cached;
Patrick Rudolph305035c2016-11-11 18:38:50 +0100320 struct cpuid_result cpures;
Patrick Rudolph31d19592016-03-26 12:22:34 +0100321 int err;
Patrick Rudolph305035c2016-11-11 18:38:50 +0100322 u32 cpu;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700323
324 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200325
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -0700326 report_platform_info();
327
Stefan Reinauer00636b02012-04-04 00:08:51 +0200328 /* Wait for ME to be ready */
329 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700330 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200331
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700332 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200333
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700334 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200335
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700336 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200337
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700338 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +0200339
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700340 reg_5d10 = read32(DEFAULT_MCHBAR + 0x5d10); // !!! = 0x00000000
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300341 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700342 && reg_5d10 && !s3resume) {
343 write32(DEFAULT_MCHBAR + 0x5d10, 0);
344 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200345 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700346
Patrick Georgi546953c2014-11-29 10:38:17 +0100347 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200348 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200349
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700350 early_pch_init_native();
351 early_thermal_init();
352
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100353 /* try to find timings in MRC cache */
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100354 int cache_not_found = mrc_cache_get_current(MRC_TRAINING_DATA,
355 MRC_CACHE_VERSION, &rdev);
356 if (cache_not_found || (region_device_sz(&rdev) < sizeof(ctrl))) {
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100357 if (s3resume) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700358 /* Failed S3 resume, reset to come up cleanly */
359 outb(0x6, 0xcf9);
360 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200361 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100362 ctrl_cached = NULL;
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100363 } else {
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100364 ctrl_cached = rdev_mmap_full(&rdev);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700365 }
366
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100367 /* verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200368 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200369 /* Load SPD unique information data. */
370 memset(spds, 0, sizeof(spds));
371 mainboard_get_spd(spds, 1);
372
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100373 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
374 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
375 if (!fast_boot)
376 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200377 } else {
378 fast_boot = s3resume;
379 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100380
381 if (fast_boot) {
382 printk(BIOS_DEBUG, "Trying stored timings.\n");
383 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
384
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200385 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100386 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200387 if (s3resume) {
388 /* Failed S3 resume, reset to come up cleanly */
389 outb(0x6, 0xcf9);
390 halt();
391 }
392 /* no need to erase bad mrc cache here, it gets overwritten on
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100393 * successful boot. */
394 printk(BIOS_ERR, "Stored timings are invalid !\n");
395 fast_boot = 0;
396 }
397 }
398 if (!fast_boot) {
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100399 /* Reset internal state */
400 memset(&ctrl, 0, sizeof(ctrl));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100401 ctrl.mobile = mobile;
402 ctrl.tCK = min_tck;
403
Patrick Rudolph305035c2016-11-11 18:38:50 +0100404 /* Get architecture */
405 cpures = cpuid(1);
406 cpu = cpures.eax;
407 ctrl.sandybridge = IS_SANDY_CPU(cpu);
408
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100409 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200410 memset(spds, 0, sizeof(spds));
411 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100412 dram_find_spds_ddr3(spds, &ctrl);
413
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200414 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100415 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100416
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100417 if (err) {
418 /* fallback: disable failing channel */
419 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
420 printram("Disable failing channel.\n");
421
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100422 /* Reset internal state */
423 memset(&ctrl, 0, sizeof(ctrl));
424 ctrl.mobile = mobile;
425 ctrl.tCK = min_tck;
426
Patrick Rudolph305035c2016-11-11 18:38:50 +0100427 /* Get architecture */
428 cpures = cpuid(1);
429 cpu = cpures.eax;
430 ctrl.sandybridge = IS_SANDY_CPU(cpu);
431
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100432 /* Reset DDR3 frequency */
433 dram_find_spds_ddr3(spds, &ctrl);
434
435 /* disable failing channel */
436 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
437
438 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
439 }
440
Patrick Rudolph31d19592016-03-26 12:22:34 +0100441 if (err)
442 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700443
444 /* FIXME: should be hardware revision-dependent. */
445 write32(DEFAULT_MCHBAR + 0x5024, 0x00a030ce);
446
447 set_scrambling_seed(&ctrl);
448
449 set_42a0(&ctrl);
450
451 final_registers(&ctrl);
452
453 /* Zone config */
454 dram_zones(&ctrl, 0);
455
Patrick Rudolph77db3e12016-11-26 10:11:14 +0100456 /* Non intrusive, fast ram check */
457 quick_ram_check();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700458
459 intel_early_me_status();
460 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
461 intel_early_me_status();
462
Stefan Reinauer00636b02012-04-04 00:08:51 +0200463 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700464
465 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100466 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700467 save_timings(&ctrl);
468 if (s3resume && !cbmem_was_inited) {
469 /* Failed S3 resume, reset to come up cleanly */
470 outb(0x6, 0xcf9);
471 halt();
472 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100473
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100474 fill_smbios17(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200475}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100476
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100477void perform_raminit(int s3resume)
478{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100479 post_code(0x3a);
480
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100481 timestamp_add_now(TS_BEFORE_INITRAM);
482
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200483 init_dram_ddr3(1, get_mem_min_tck(), s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100484}