blob: c06663431239fcf9bd6eda343bbaaa3c1466f5cf [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>
Subrata Banik53b08c32018-12-10 14:11:35 +053022#include <arch/cpu.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020023#include <arch/io.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020024#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020025#include <device/pci_ops.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020026#include <cbmem.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070027#include <halt.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010028#include <timestamp.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010029#include <mrc_cache.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010030#include <southbridge/intel/bd82x6x/me.h>
Arthur Heymans16fe7902017-04-12 17:01:31 +020031#include <southbridge/intel/common/smbus.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032#include <cpu/x86/msr.h>
Elyes HAOUASbf0970e2019-03-21 11:10:03 +010033
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
Patrick Rudolph708cf4b2018-07-29 12:34:03 +020038#define MRC_CACHE_VERSION 1
Arthur Heymans7539b8c2017-12-24 10:42:57 +010039
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070040/* FIXME: no ECC support. */
41/* FIXME: no support for 3-channel chipsets. */
Stefan Reinauer00636b02012-04-04 00:08:51 +020042
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070043static const char *ecc_decoder[] = {
Stefan Reinauer00636b02012-04-04 00:08:51 +020044 "inactive",
45 "active on IO",
46 "disabled on IO",
47 "active"
48};
49
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070050static void wait_txt_clear(void)
51{
52 struct cpuid_result cp;
53
54 cp = cpuid_ext(0x1, 0x0);
55 /* Check if TXT is supported? */
56 if (!(cp.ecx & 0x40))
57 return;
58 /* Some TXT public bit. */
59 if (!(read32((void *)0xfed30010) & 1))
60 return;
61 /* Wait for TXT clear. */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +020062 while (!(read8((void *)0xfed40000) & (1 << 7)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070063}
64
Stefan Reinauer00636b02012-04-04 00:08:51 +020065/*
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010066 * Disable a channel in ramctr_timing.
67 */
68static void disable_channel(ramctr_timing *ctrl, int channel) {
69 ctrl->rankmap[channel] = 0;
70 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
71 ctrl->channel_size_mb[channel] = 0;
72 ctrl->cmd_stretch[channel] = 0;
73 ctrl->mad_dimm[channel] = 0;
74 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
Patrick Rudolph74163d62016-11-17 20:02:43 +010075 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010076}
77
78/*
Patrick Rudolphb97009e2016-02-28 15:24:04 +010079 * Fill cbmem with information for SMBIOS type 17.
80 */
Patrick Rudolph735ecce2016-03-26 10:42:27 +010081static void fill_smbios17(ramctr_timing *ctrl)
Patrick Rudolphb97009e2016-02-28 15:24:04 +010082{
Patrick Rudolphb97009e2016-02-28 15:24:04 +010083 int channel, slot;
Patrick Rudolph24efe732018-08-19 11:06:06 +020084 const u16 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +010085
Elyes HAOUAS12df9502016-08-23 21:29:48 +020086 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolph24efe732018-08-19 11:06:06 +020087 enum cb_err ret = spd_add_smbios17(channel, slot, ddr_freq,
88 &ctrl->info.dimm[channel][slot]);
89 if (ret != CB_SUCCESS)
90 printk(BIOS_ERR, "RAMINIT: Failed to add SMBIOS17\n");
Patrick Rudolphb97009e2016-02-28 15:24:04 +010091 }
92}
93
94/*
Stefan Reinauer00636b02012-04-04 00:08:51 +020095 * Dump in the log memory controller configuration as read from the memory
96 * controller registers.
97 */
98static void report_memory_config(void)
99{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700100 u32 addr_decoder_common, addr_decode_ch[NUM_CHANNELS];
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200101 int i, refclk;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200102
103 addr_decoder_common = MCHBAR32(0x5000);
104 addr_decode_ch[0] = MCHBAR32(0x5004);
105 addr_decode_ch[1] = MCHBAR32(0x5008);
106
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200107 refclk = MCHBAR32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
108
109 printk(BIOS_DEBUG, "memcfg DDR3 ref clock %d MHz\n", refclk);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200110 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Patrick Rudolph6ab7e5e2017-05-31 18:21:59 +0200111 (MCHBAR32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200112 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700113 addr_decoder_common & 3, (addr_decoder_common >> 2) & 3,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200114 (addr_decoder_common >> 4) & 3);
115
116 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
117 u32 ch_conf = addr_decode_ch[i];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700118 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i,
119 ch_conf);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200120 printk(BIOS_DEBUG, " ECC %s\n",
121 ecc_decoder[(ch_conf >> 24) & 3]);
122 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
123 ((ch_conf >> 22) & 1) ? "on" : "off");
124 printk(BIOS_DEBUG, " rank interleave %s\n",
125 ((ch_conf >> 21) & 1) ? "on" : "off");
126 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
127 ((ch_conf >> 0) & 0xff) * 256,
128 ((ch_conf >> 19) & 1) ? 16 : 8,
129 ((ch_conf >> 17) & 1) ? "dual" : "single",
130 ((ch_conf >> 16) & 1) ? "" : ", selected");
131 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
132 ((ch_conf >> 8) & 0xff) * 256,
133 ((ch_conf >> 20) & 1) ? 16 : 8,
134 ((ch_conf >> 18) & 1) ? "dual" : "single",
135 ((ch_conf >> 16) & 1) ? ", selected" : "");
136 }
137}
138
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100139/*
140 * Return CRC16 match for all SPDs.
141 */
142static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
143{
144 int channel, slot, spd_slot;
145 int match = 1;
146
147 FOR_ALL_CHANNELS {
148 for (slot = 0; slot < NUM_SLOTS; slot++) {
149 spd_slot = 2 * channel + slot;
150 match &= ctrl->spd_crc[channel][slot] ==
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200151 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100152 }
153 }
154 return match;
155}
156
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200157void read_spd(spd_raw_data * spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200158{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700159 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200160 if (id_only) {
161 for (j = 117; j < 128; j++)
162 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
163 } else {
164 for (j = 0; j < 256; j++)
165 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
166 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700167}
168
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100169static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700170{
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100171 int dimms = 0, dimms_on_channel;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700172 int channel, slot, spd_slot;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100173 dimm_info *dimm = &ctrl->info;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700174
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200175 memset (ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700176
177 ctrl->extended_temperature_range = 1;
178 ctrl->auto_self_refresh = 1;
179
180 FOR_ALL_CHANNELS {
181 ctrl->channel_size_mb[channel] = 0;
182
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100183 dimms_on_channel = 0;
184 /* count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700185 for (slot = 0; slot < NUM_SLOTS; slot++) {
186 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200187 printk(BIOS_DEBUG,
188 "SPD probe channel%d, slot%d\n", channel, slot);
189
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700190 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100191 if (dimm->dimm[channel][slot].dram_type == SPD_MEMORY_TYPE_SDRAM_DDR3)
192 dimms_on_channel++;
193 }
194
195 for (slot = 0; slot < NUM_SLOTS; slot++) {
196 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200197 printk(BIOS_DEBUG,
198 "SPD probe channel%d, slot%d\n", channel, slot);
199
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100200 /* search for XMP profile */
201 spd_xmp_decode_ddr3(&dimm->dimm[channel][slot],
202 spd[spd_slot],
203 DDR3_XMP_PROFILE_1);
204
205 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
206 printram("No valid XMP profile found.\n");
207 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
208 } else if (dimms_on_channel > dimm->dimm[channel][slot].dimms_per_channel) {
209 printram("XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
210 dimm->dimm[channel][slot].dimms_per_channel,
211 dimms_on_channel);
Julius Wernercd49cce2019-03-05 16:53:33 -0800212 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_MAX_DIMMS))
Vagiz Trakhanov771be482017-10-02 10:02:35 +0000213 printk(BIOS_WARNING, "XMP maximum DIMMs will be ignored.\n");
214 else
215 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100216 } else if (dimm->dimm[channel][slot].voltage != 1500) {
217 /* TODO: support other DDR3 voltage than 1500mV */
218 printram("XMP profile's requested %u mV is unsupported.\n",
219 dimm->dimm[channel][slot].voltage);
220 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
221 }
222
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100223 /* fill in CRC16 for MRC cache */
224 ctrl->spd_crc[channel][slot] =
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200225 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100226
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700227 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
228 // set dimm invalid
229 dimm->dimm[channel][slot].ranks = 0;
230 dimm->dimm[channel][slot].size_mb = 0;
231 continue;
232 }
233
234 dram_print_spd_ddr3(&dimm->dimm[channel][slot]);
235 dimms++;
236 ctrl->rank_mirror[channel][slot * 2] = 0;
237 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->dimm[channel][slot].flags.pins_mirrored;
238 ctrl->channel_size_mb[channel] += dimm->dimm[channel][slot].size_mb;
239
240 ctrl->auto_self_refresh &= dimm->dimm[channel][slot].flags.asr;
241 ctrl->extended_temperature_range &= dimm->dimm[channel][slot].flags.ext_temp_refresh;
242
243 ctrl->rankmap[channel] |= ((1 << dimm->dimm[channel][slot].ranks) - 1) << (2 * slot);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100244 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n",
245 channel, ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700246 }
247 if ((ctrl->rankmap[channel] & 3) && (ctrl->rankmap[channel] & 0xc)
248 && dimm->dimm[channel][0].reference_card <= 5 && dimm->dimm[channel][1].reference_card <= 5) {
249 const int ref_card_offset_table[6][6] = {
250 { 0, 0, 0, 0, 2, 2, },
251 { 0, 0, 0, 0, 2, 2, },
252 { 0, 0, 0, 0, 2, 2, },
253 { 0, 0, 0, 0, 1, 1, },
254 { 2, 2, 2, 1, 0, 0, },
255 { 2, 2, 2, 1, 0, 0, },
256 };
257 ctrl->ref_card_offset[channel] = ref_card_offset_table[dimm->dimm[channel][0].reference_card]
258 [dimm->dimm[channel][1].reference_card];
259 } else
260 ctrl->ref_card_offset[channel] = 0;
261 }
262
263 if (!dimms)
264 die("No DIMMs were found");
265}
266
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200267static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700268{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700269 /* Save the MRC S3 restore data to cbmem */
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100270 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, ctrl,
271 sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700272}
273
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100274static int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot,
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200275 int s3_resume, int me_uma_size)
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100276{
Patrick Rudolph305035c2016-11-11 18:38:50 +0100277 if (ctrl->sandybridge)
278 return try_init_dram_ddr3_sandy(ctrl, fast_boot, s3_resume, me_uma_size);
279 else
280 return try_init_dram_ddr3_ivy(ctrl, fast_boot, s3_resume, me_uma_size);
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100281}
282
Patrick Rudolph74203de2017-11-20 11:57:01 +0100283static void init_dram_ddr3(int min_tck, int s3resume)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700284{
285 int me_uma_size;
286 int cbmem_was_inited;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100287 ramctr_timing ctrl;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100288 int fast_boot;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200289 spd_raw_data spds[4];
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100290 struct region_device rdev;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100291 ramctr_timing *ctrl_cached;
Patrick Rudolph31d19592016-03-26 12:22:34 +0100292 int err;
Patrick Rudolph305035c2016-11-11 18:38:50 +0100293 u32 cpu;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700294
295 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200296
297 /* Wait for ME to be ready */
298 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700299 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200300
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700301 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200302
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700303 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200304
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700305 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200306
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700307 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +0200308
Felix Held55823c32018-07-28 00:41:57 +0200309 reg_5d10 = MCHBAR32(0x5d10); // !!! = 0x00000000
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300310 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700311 && reg_5d10 && !s3resume) {
Felix Held55823c32018-07-28 00:41:57 +0200312 MCHBAR32(0x5d10) = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700313 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200314 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700315
Patrick Georgi546953c2014-11-29 10:38:17 +0100316 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200317 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200318
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700319 early_pch_init_native();
320 early_thermal_init();
321
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100322 /* try to find timings in MRC cache */
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100323 int cache_not_found = mrc_cache_get_current(MRC_TRAINING_DATA,
324 MRC_CACHE_VERSION, &rdev);
325 if (cache_not_found || (region_device_sz(&rdev) < sizeof(ctrl))) {
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100326 if (s3resume) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700327 /* Failed S3 resume, reset to come up cleanly */
328 outb(0x6, 0xcf9);
329 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200330 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100331 ctrl_cached = NULL;
Patrick Rudolph27e085a2016-03-26 10:59:02 +0100332 } else {
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100333 ctrl_cached = rdev_mmap_full(&rdev);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700334 }
335
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100336 /* verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200337 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200338 /* Load SPD unique information data. */
339 memset(spds, 0, sizeof(spds));
340 mainboard_get_spd(spds, 1);
341
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100342 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
343 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
344 if (!fast_boot)
345 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200346 } else {
347 fast_boot = s3resume;
348 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100349
350 if (fast_boot) {
351 printk(BIOS_DEBUG, "Trying stored timings.\n");
352 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
353
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200354 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100355 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200356 if (s3resume) {
357 /* Failed S3 resume, reset to come up cleanly */
358 outb(0x6, 0xcf9);
359 halt();
360 }
361 /* no need to erase bad mrc cache here, it gets overwritten on
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100362 * successful boot. */
363 printk(BIOS_ERR, "Stored timings are invalid !\n");
364 fast_boot = 0;
365 }
366 }
367 if (!fast_boot) {
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100368 /* Reset internal state */
369 memset(&ctrl, 0, sizeof(ctrl));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100370 ctrl.tCK = min_tck;
371
Patrick Rudolph305035c2016-11-11 18:38:50 +0100372 /* Get architecture */
Subrata Banik53b08c32018-12-10 14:11:35 +0530373 cpu = cpu_get_cpuid();
Patrick Rudolph305035c2016-11-11 18:38:50 +0100374 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 */
Subrata Banik53b08c32018-12-10 14:11:35 +0530394 cpu = cpu_get_cpuid();
Patrick Rudolph305035c2016-11-11 18:38:50 +0100395 ctrl.sandybridge = IS_SANDY_CPU(cpu);
396
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100397 /* Reset DDR3 frequency */
398 dram_find_spds_ddr3(spds, &ctrl);
399
400 /* disable failing channel */
401 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
402
403 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
404 }
405
Patrick Rudolph31d19592016-03-26 12:22:34 +0100406 if (err)
407 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700408
409 /* FIXME: should be hardware revision-dependent. */
Felix Held55823c32018-07-28 00:41:57 +0200410 MCHBAR32(0x5024) = 0x00a030ce;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700411
412 set_scrambling_seed(&ctrl);
413
414 set_42a0(&ctrl);
415
416 final_registers(&ctrl);
417
418 /* Zone config */
419 dram_zones(&ctrl, 0);
420
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700421 intel_early_me_status();
422 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
423 intel_early_me_status();
424
Stefan Reinauer00636b02012-04-04 00:08:51 +0200425 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700426
427 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100428 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700429 save_timings(&ctrl);
430 if (s3resume && !cbmem_was_inited) {
431 /* Failed S3 resume, reset to come up cleanly */
432 outb(0x6, 0xcf9);
433 halt();
434 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100435
Nico Huber9ce59742018-09-13 10:52:44 +0200436 if (!s3resume)
437 fill_smbios17(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200438}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100439
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100440void perform_raminit(int s3resume)
441{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100442 post_code(0x3a);
443
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100444 timestamp_add_now(TS_BEFORE_INITRAM);
445
Patrick Rudolph74203de2017-11-20 11:57:01 +0100446 init_dram_ddr3(get_mem_min_tck(), s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100447}