blob: bdba7489c092e93637a64b62f2d9c84b85143cfb [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>
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>
Alexander Couzens81c5c762016-03-09 03:13:45 +010026#include <northbridge/intel/common/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>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010031#include <smbios.h>
32#include <memory_info.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070033#include <lib.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010034#include "raminit_native.h"
35#include "raminit_common.h"
36#include "sandybridge.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020037
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{
81 struct memory_info *mem_info;
82 int channel, slot;
83 struct dimm_info *dimm;
Patrick Rudolph735ecce2016-03-26 10:42:27 +010084 uint16_t ddr_freq;
85 dimm_info *info = &ctrl->info;
86
87 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +010088
89 /*
90 * Allocate CBMEM area for DIMM information used to populate SMBIOS
91 * table 17
92 */
93 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
94 printk(BIOS_DEBUG, "CBMEM entry for DIMM info: 0x%p\n", mem_info);
95 if (!mem_info)
96 return;
97
98 memset(mem_info, 0, sizeof(*mem_info));
99
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200100 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100101 dimm = &mem_info->dimm[mem_info->dimm_cnt];
102 if (info->dimm[channel][slot].size_mb) {
103 dimm->ddr_type = MEMORY_TYPE_DDR3;
104 dimm->ddr_frequency = ddr_freq;
105 dimm->dimm_size = info->dimm[channel][slot].size_mb;
106 dimm->channel_num = channel;
107 dimm->rank_per_dimm = info->dimm[channel][slot].ranks;
108 dimm->dimm_num = slot;
109 memcpy(dimm->module_part_number,
110 info->dimm[channel][slot].part_number, 16);
111 dimm->mod_id = info->dimm[channel][slot].manufacturer_id;
112 dimm->mod_type = info->dimm[channel][slot].dimm_type;
113 dimm->bus_width = info->dimm[channel][slot].width;
114 mem_info->dimm_cnt++;
115 }
116 }
117}
118
119/*
Stefan Reinauer00636b02012-04-04 00:08:51 +0200120 * Dump in the log memory controller configuration as read from the memory
121 * controller registers.
122 */
123static void report_memory_config(void)
124{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700125 u32 addr_decoder_common, addr_decode_ch[NUM_CHANNELS];
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200126 int i, refclk;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200127
128 addr_decoder_common = MCHBAR32(0x5000);
129 addr_decode_ch[0] = MCHBAR32(0x5004);
130 addr_decode_ch[1] = MCHBAR32(0x5008);
131
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200132 refclk = MCHBAR32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
133
134 printk(BIOS_DEBUG, "memcfg DDR3 ref clock %d MHz\n", refclk);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200135 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200136 (MCHBAR32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200137 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700138 addr_decoder_common & 3, (addr_decoder_common >> 2) & 3,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200139 (addr_decoder_common >> 4) & 3);
140
141 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
142 u32 ch_conf = addr_decode_ch[i];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700143 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i,
144 ch_conf);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200145 printk(BIOS_DEBUG, " ECC %s\n",
146 ecc_decoder[(ch_conf >> 24) & 3]);
147 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
148 ((ch_conf >> 22) & 1) ? "on" : "off");
149 printk(BIOS_DEBUG, " rank interleave %s\n",
150 ((ch_conf >> 21) & 1) ? "on" : "off");
151 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
152 ((ch_conf >> 0) & 0xff) * 256,
153 ((ch_conf >> 19) & 1) ? 16 : 8,
154 ((ch_conf >> 17) & 1) ? "dual" : "single",
155 ((ch_conf >> 16) & 1) ? "" : ", selected");
156 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
157 ((ch_conf >> 8) & 0xff) * 256,
158 ((ch_conf >> 20) & 1) ? 16 : 8,
159 ((ch_conf >> 18) & 1) ? "dual" : "single",
160 ((ch_conf >> 16) & 1) ? ", selected" : "");
161 }
162}
163
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100164/*
165 * Return CRC16 match for all SPDs.
166 */
167static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
168{
169 int channel, slot, spd_slot;
170 int match = 1;
171
172 FOR_ALL_CHANNELS {
173 for (slot = 0; slot < NUM_SLOTS; slot++) {
174 spd_slot = 2 * channel + slot;
175 match &= ctrl->spd_crc[channel][slot] ==
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200176 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100177 }
178 }
179 return match;
180}
181
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200182void read_spd(spd_raw_data * spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200183{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700184 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200185 if (id_only) {
186 for (j = 117; j < 128; j++)
187 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
188 } else {
189 for (j = 0; j < 256; j++)
190 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
191 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700192}
193
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100194static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700195{
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100196 int dimms = 0, dimms_on_channel;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700197 int channel, slot, spd_slot;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100198 dimm_info *dimm = &ctrl->info;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700199
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200200 memset (ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700201
202 ctrl->extended_temperature_range = 1;
203 ctrl->auto_self_refresh = 1;
204
205 FOR_ALL_CHANNELS {
206 ctrl->channel_size_mb[channel] = 0;
207
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100208 dimms_on_channel = 0;
209 /* count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700210 for (slot = 0; slot < NUM_SLOTS; slot++) {
211 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200212 printk(BIOS_DEBUG,
213 "SPD probe channel%d, slot%d\n", channel, slot);
214
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700215 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100216 if (dimm->dimm[channel][slot].dram_type == SPD_MEMORY_TYPE_SDRAM_DDR3)
217 dimms_on_channel++;
218 }
219
220 for (slot = 0; slot < NUM_SLOTS; slot++) {
221 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200222 printk(BIOS_DEBUG,
223 "SPD probe channel%d, slot%d\n", channel, slot);
224
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100225 /* search for XMP profile */
226 spd_xmp_decode_ddr3(&dimm->dimm[channel][slot],
227 spd[spd_slot],
228 DDR3_XMP_PROFILE_1);
229
230 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
231 printram("No valid XMP profile found.\n");
232 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
233 } else if (dimms_on_channel > dimm->dimm[channel][slot].dimms_per_channel) {
234 printram("XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
235 dimm->dimm[channel][slot].dimms_per_channel,
236 dimms_on_channel);
237 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
238 } else if (dimm->dimm[channel][slot].voltage != 1500) {
239 /* TODO: support other DDR3 voltage than 1500mV */
240 printram("XMP profile's requested %u mV is unsupported.\n",
241 dimm->dimm[channel][slot].voltage);
242 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
243 }
244
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100245 /* fill in CRC16 for MRC cache */
246 ctrl->spd_crc[channel][slot] =
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200247 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100248
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700249 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
250 // set dimm invalid
251 dimm->dimm[channel][slot].ranks = 0;
252 dimm->dimm[channel][slot].size_mb = 0;
253 continue;
254 }
255
256 dram_print_spd_ddr3(&dimm->dimm[channel][slot]);
257 dimms++;
258 ctrl->rank_mirror[channel][slot * 2] = 0;
259 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->dimm[channel][slot].flags.pins_mirrored;
260 ctrl->channel_size_mb[channel] += dimm->dimm[channel][slot].size_mb;
261
262 ctrl->auto_self_refresh &= dimm->dimm[channel][slot].flags.asr;
263 ctrl->extended_temperature_range &= dimm->dimm[channel][slot].flags.ext_temp_refresh;
264
265 ctrl->rankmap[channel] |= ((1 << dimm->dimm[channel][slot].ranks) - 1) << (2 * slot);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100266 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n",
267 channel, ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700268 }
269 if ((ctrl->rankmap[channel] & 3) && (ctrl->rankmap[channel] & 0xc)
270 && dimm->dimm[channel][0].reference_card <= 5 && dimm->dimm[channel][1].reference_card <= 5) {
271 const int ref_card_offset_table[6][6] = {
272 { 0, 0, 0, 0, 2, 2, },
273 { 0, 0, 0, 0, 2, 2, },
274 { 0, 0, 0, 0, 2, 2, },
275 { 0, 0, 0, 0, 1, 1, },
276 { 2, 2, 2, 1, 0, 0, },
277 { 2, 2, 2, 1, 0, 0, },
278 };
279 ctrl->ref_card_offset[channel] = ref_card_offset_table[dimm->dimm[channel][0].reference_card]
280 [dimm->dimm[channel][1].reference_card];
281 } else
282 ctrl->ref_card_offset[channel] = 0;
283 }
284
285 if (!dimms)
286 die("No DIMMs were found");
287}
288
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200289static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700290{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700291 /* Save the MRC S3 restore data to cbmem */
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200292 store_current_mrc_cache(ctrl, sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700293}
294
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100295static int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot,
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200296 int s3_resume, int me_uma_size)
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100297{
Patrick Rudolph305035c2016-11-11 18:38:50 +0100298 if (ctrl->sandybridge)
299 return try_init_dram_ddr3_sandy(ctrl, fast_boot, s3_resume, me_uma_size);
300 else
301 return try_init_dram_ddr3_ivy(ctrl, fast_boot, s3_resume, me_uma_size);
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100302}
303
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200304static void init_dram_ddr3(int mobile, int min_tck, int s3resume)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700305{
306 int me_uma_size;
307 int cbmem_was_inited;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100308 ramctr_timing ctrl;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100309 int fast_boot;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200310 spd_raw_data spds[4];
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100311 struct mrc_data_container *mrc_cache;
312 ramctr_timing *ctrl_cached;
Patrick Rudolph305035c2016-11-11 18:38:50 +0100313 struct cpuid_result cpures;
Patrick Rudolph31d19592016-03-26 12:22:34 +0100314 int err;
Patrick Rudolph305035c2016-11-11 18:38:50 +0100315 u32 cpu;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700316
317 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200318
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -0700319 report_platform_info();
320
Stefan Reinauer00636b02012-04-04 00:08:51 +0200321 /* Wait for ME to be ready */
322 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700323 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200324
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700325 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200326
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700327 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200328
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700329 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200330
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700331 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +0200332
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700333 reg_5d10 = read32(DEFAULT_MCHBAR + 0x5d10); // !!! = 0x00000000
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300334 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700335 && reg_5d10 && !s3resume) {
336 write32(DEFAULT_MCHBAR + 0x5d10, 0);
337 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200338 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700339
Patrick Georgi546953c2014-11-29 10:38:17 +0100340 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200341 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200342
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700343 early_pch_init_native();
344 early_thermal_init();
345
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100346 /* try to find timings in MRC cache */
347 mrc_cache = find_current_mrc_cache();
348 if (!mrc_cache || (mrc_cache->mrc_data_size < sizeof(ctrl))) {
349 if (s3resume) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700350 /* Failed S3 resume, reset to come up cleanly */
351 outb(0x6, 0xcf9);
352 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200353 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100354 ctrl_cached = NULL;
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100355 } else {
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100356 ctrl_cached = (ramctr_timing *)mrc_cache->mrc_data;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700357 }
358
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100359 /* verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200360 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200361 /* Load SPD unique information data. */
362 memset(spds, 0, sizeof(spds));
363 mainboard_get_spd(spds, 1);
364
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100365 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
366 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
367 if (!fast_boot)
368 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200369 } else {
370 fast_boot = s3resume;
371 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100372
373 if (fast_boot) {
374 printk(BIOS_DEBUG, "Trying stored timings.\n");
375 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
376
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200377 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100378 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200379 if (s3resume) {
380 /* Failed S3 resume, reset to come up cleanly */
381 outb(0x6, 0xcf9);
382 halt();
383 }
384 /* no need to erase bad mrc cache here, it gets overwritten on
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100385 * successful boot. */
386 printk(BIOS_ERR, "Stored timings are invalid !\n");
387 fast_boot = 0;
388 }
389 }
390 if (!fast_boot) {
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100391 /* Reset internal state */
392 memset(&ctrl, 0, sizeof(ctrl));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100393 ctrl.mobile = mobile;
394 ctrl.tCK = min_tck;
395
Patrick Rudolph305035c2016-11-11 18:38:50 +0100396 /* Get architecture */
397 cpures = cpuid(1);
398 cpu = cpures.eax;
399 ctrl.sandybridge = IS_SANDY_CPU(cpu);
400
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100401 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200402 memset(spds, 0, sizeof(spds));
403 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100404 dram_find_spds_ddr3(spds, &ctrl);
405
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200406 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100407 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100408
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100409 if (err) {
410 /* fallback: disable failing channel */
411 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
412 printram("Disable failing channel.\n");
413
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100414 /* Reset internal state */
415 memset(&ctrl, 0, sizeof(ctrl));
416 ctrl.mobile = mobile;
417 ctrl.tCK = min_tck;
418
Patrick Rudolph305035c2016-11-11 18:38:50 +0100419 /* Get architecture */
420 cpures = cpuid(1);
421 cpu = cpures.eax;
422 ctrl.sandybridge = IS_SANDY_CPU(cpu);
423
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100424 /* Reset DDR3 frequency */
425 dram_find_spds_ddr3(spds, &ctrl);
426
427 /* disable failing channel */
428 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
429
430 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
431 }
432
Patrick Rudolph31d19592016-03-26 12:22:34 +0100433 if (err)
434 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700435
436 /* FIXME: should be hardware revision-dependent. */
437 write32(DEFAULT_MCHBAR + 0x5024, 0x00a030ce);
438
439 set_scrambling_seed(&ctrl);
440
441 set_42a0(&ctrl);
442
443 final_registers(&ctrl);
444
445 /* Zone config */
446 dram_zones(&ctrl, 0);
447
Patrick Rudolph77db3e12016-11-26 10:11:14 +0100448 /* Non intrusive, fast ram check */
449 quick_ram_check();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700450
451 intel_early_me_status();
452 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
453 intel_early_me_status();
454
Stefan Reinauer00636b02012-04-04 00:08:51 +0200455 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700456
457 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100458 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700459 save_timings(&ctrl);
460 if (s3resume && !cbmem_was_inited) {
461 /* Failed S3 resume, reset to come up cleanly */
462 outb(0x6, 0xcf9);
463 halt();
464 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100465
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100466 fill_smbios17(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200467}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100468
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100469void perform_raminit(int s3resume)
470{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100471 post_code(0x3a);
472
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100473 timestamp_add_now(TS_BEFORE_INITRAM);
474
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200475 init_dram_ddr3(1, get_mem_min_tck(), s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100476}