blob: d2ae4b039eb4ae6c8633f801ecd7a840d2431263 [file] [log] [blame]
Stefan Reinauer00636b02012-04-04 00:08:51 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer00636b02012-04-04 00:08:51 +020018 */
19
20#include <console/console.h>
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020021#include <console/usb.h>
Kyösti Mälkki5687fc92013-11-28 18:11:49 +020022#include <bootmode.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020023#include <string.h>
24#include <arch/hlt.h>
25#include <arch/io.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020026#include <cbmem.h>
27#include <arch/cbfs.h>
28#include <cbfs.h>
29#include <ip_checksum.h>
30#include <pc80/mc146818rtc.h>
Duncan Laurie7b508dd2012-04-09 12:30:43 -070031#include <device/pci_def.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020032#include "raminit.h"
33#include "pei_data.h"
34#include "sandybridge.h"
35
36/* Management Engine is in the southbridge */
37#include "southbridge/intel/bd82x6x/me.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020038
39/*
40 * MRC scrambler seed offsets should be reserved in
41 * mainboard cmos.layout and not covered by checksum.
42 */
43#if CONFIG_USE_OPTION_TABLE
44#include "option_table.h"
45#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
46#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
47#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
48#else
Duncan Lauriecf81b822012-08-08 13:43:55 -070049#define CMOS_OFFSET_MRC_SEED 152
50#define CMOS_OFFSET_MRC_SEED_S3 156
51#define CMOS_OFFSET_MRC_SEED_CHK 160
Stefan Reinauer00636b02012-04-04 00:08:51 +020052#endif
53
Kyösti Mälkki78938482014-01-04 11:02:45 +020054void save_mrc_data(struct pei_data *pei_data)
Stefan Reinauer00636b02012-04-04 00:08:51 +020055{
56 u16 c1, c2, checksum;
Stefan Reinauer00636b02012-04-04 00:08:51 +020057 struct mrc_data_container *mrcdata;
58 int output_len = ALIGN(pei_data->mrc_output_len, 16);
59
60 /* Save the MRC S3 restore data to cbmem */
Stefan Reinauer00636b02012-04-04 00:08:51 +020061 mrcdata = cbmem_add
62 (CBMEM_ID_MRCDATA,
63 output_len + sizeof(struct mrc_data_container));
64
Kyösti Mälkki743a2182014-06-15 15:59:44 +030065 if (mrcdata != NULL) {
66 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
67 pei_data->mrc_output, mrcdata, output_len);
Stefan Reinauer00636b02012-04-04 00:08:51 +020068
Kyösti Mälkki743a2182014-06-15 15:59:44 +030069 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
70 mrcdata->mrc_data_size = output_len;
71 mrcdata->reserved = 0;
72 memcpy(mrcdata->mrc_data, pei_data->mrc_output,
73 pei_data->mrc_output_len);
Stefan Reinauer00636b02012-04-04 00:08:51 +020074
Kyösti Mälkki743a2182014-06-15 15:59:44 +030075 /* Zero the unused space in aligned buffer. */
76 if (output_len > pei_data->mrc_output_len)
77 memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0,
78 output_len - pei_data->mrc_output_len);
Stefan Reinauer00636b02012-04-04 00:08:51 +020079
Kyösti Mälkki743a2182014-06-15 15:59:44 +030080 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
81 mrcdata->mrc_data_size);
82 }
Stefan Reinauer00636b02012-04-04 00:08:51 +020083
84 /* Save the MRC seed values to CMOS */
85 cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
86 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
87 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
88
89 cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
90 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
91 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
92
93 /* Save a simple checksum of the seed values */
94 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
95 sizeof(u32));
96 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
97 sizeof(u32));
98 checksum = add_ip_checksums(sizeof(u32), c1, c2);
99
100 cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
101 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1);
102}
103
Stefan Reinauer00636b02012-04-04 00:08:51 +0200104static void prepare_mrc_cache(struct pei_data *pei_data)
105{
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700106 struct mrc_data_container *mrc_cache;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200107 u16 c1, c2, checksum, seed_checksum;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200108
109 // preset just in case there is an error
110 pei_data->mrc_input = NULL;
111 pei_data->mrc_input_len = 0;
112
113 /* Read scrambler seeds from CMOS */
114 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
115 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
116 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
117
118 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
119 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
120 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
121
122 /* Compute seed checksum and compare */
123 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
124 sizeof(u32));
125 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
126 sizeof(u32));
127 checksum = add_ip_checksums(sizeof(u32), c1, c2);
128
129 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
130 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8;
131
132 if (checksum != seed_checksum) {
133 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
134 pei_data->scrambler_seed = 0;
135 pei_data->scrambler_seed_s3 = 0;
136 return;
137 }
138
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700139 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
140 /* error message printed in find_current_mrc_cache */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200141 return;
142 }
143
144 pei_data->mrc_input = mrc_cache->mrc_data;
145 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
146
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700147 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
148 __func__, pei_data->mrc_input,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200149 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200150}
Stefan Reinauer00636b02012-04-04 00:08:51 +0200151
152static const char* ecc_decoder[] = {
153 "inactive",
154 "active on IO",
155 "disabled on IO",
156 "active"
157};
158
159/*
160 * Dump in the log memory controller configuration as read from the memory
161 * controller registers.
162 */
163static void report_memory_config(void)
164{
165 u32 addr_decoder_common, addr_decode_ch[2];
166 int i;
167
168 addr_decoder_common = MCHBAR32(0x5000);
169 addr_decode_ch[0] = MCHBAR32(0x5004);
170 addr_decode_ch[1] = MCHBAR32(0x5008);
171
172 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
173 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
174 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
175 addr_decoder_common & 3,
176 (addr_decoder_common >> 2) & 3,
177 (addr_decoder_common >> 4) & 3);
178
179 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
180 u32 ch_conf = addr_decode_ch[i];
181 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
182 i, ch_conf);
183 printk(BIOS_DEBUG, " ECC %s\n",
184 ecc_decoder[(ch_conf >> 24) & 3]);
185 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
186 ((ch_conf >> 22) & 1) ? "on" : "off");
187 printk(BIOS_DEBUG, " rank interleave %s\n",
188 ((ch_conf >> 21) & 1) ? "on" : "off");
189 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
190 ((ch_conf >> 0) & 0xff) * 256,
191 ((ch_conf >> 19) & 1) ? 16 : 8,
192 ((ch_conf >> 17) & 1) ? "dual" : "single",
193 ((ch_conf >> 16) & 1) ? "" : ", selected");
194 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
195 ((ch_conf >> 8) & 0xff) * 256,
196 ((ch_conf >> 20) & 1) ? 16 : 8,
197 ((ch_conf >> 18) & 1) ? "dual" : "single",
198 ((ch_conf >> 16) & 1) ? ", selected" : "");
199 }
200}
201
Marc Jones313ec9d2012-11-02 14:26:44 -0600202static void post_system_agent_init(struct pei_data *pei_data)
203{
204 /* If PCIe init is skipped, set the PEG clock gating */
205 if (!pei_data->pcie_init)
206 MCHBAR32(0x7010) = MCHBAR32(0x7010) | 0x01;
207}
208
Stefan Reinauer00636b02012-04-04 00:08:51 +0200209/**
210 * Find PEI executable in coreboot filesystem and execute it.
211 *
212 * @param pei_data: configuration data for UEFI PEI reference code
213 */
214void sdram_initialize(struct pei_data *pei_data)
215{
216 struct sys_info sysinfo;
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100217 int (*entry) (struct pei_data *pei_data) __attribute__ ((regparm(1)));
Stefan Reinauer00636b02012-04-04 00:08:51 +0200218
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -0700219 report_platform_info();
220
Stefan Reinauer00636b02012-04-04 00:08:51 +0200221 /* Wait for ME to be ready */
222 intel_early_me_init();
223 intel_early_me_uma_size();
224
225 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
226
227 memset(&sysinfo, 0, sizeof(sysinfo));
228
229 sysinfo.boot_path = pei_data->boot_mode;
230
Stefan Reinauer00636b02012-04-04 00:08:51 +0200231 /*
232 * Do not pass MRC data in for recovery mode boot,
233 * Always pass it in for S3 resume.
234 */
235 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
236 prepare_mrc_cache(pei_data);
237
238 /* If MRC data is not found we cannot continue S3 resume. */
239 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700240 printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200241 outb(0x6, 0xcf9);
242 hlt();
243 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200244
Vadim Bendebury48a4a7f2012-06-07 18:47:13 -0700245 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200246 pei_data->tx_byte = do_putchar;
Vadim Bendebury48a4a7f2012-06-07 18:47:13 -0700247
Stefan Reinauer00636b02012-04-04 00:08:51 +0200248 /* Locate and call UEFI System Agent binary. */
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800249 /* TODO make MRC blob (0xab?) defined in cbfs_core.h. */
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100250 entry = cbfs_get_file_content(
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100251 CBFS_DEFAULT_MEDIA, "mrc.bin", 0xab, NULL);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200252 if (entry) {
253 int rv;
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100254 rv = entry (pei_data);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200255 if (rv) {
Stefan Reinauer696262b2012-06-18 14:38:40 -0700256 switch (rv) {
257 case -1:
Stefan Reinauerc6b9f922012-08-09 11:00:14 -0700258 printk(BIOS_ERR, "PEI version mismatch.\n");
Stefan Reinauer696262b2012-06-18 14:38:40 -0700259 break;
260 case -2:
261 printk(BIOS_ERR, "Invalid memory frequency.\n");
262 break;
263 default:
264 printk(BIOS_ERR, "MRC returned %x.\n", rv);
265 }
266 die("Nonzero MRC return value.\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200267 }
268 } else {
269 die("UEFI PEI System Agent not found.\n");
270 }
271
Kyösti Mälkki50ecb9c2013-12-06 21:50:55 +0200272#if CONFIG_USBDEBUG_IN_ROMSTAGE
Sven Schnelled4ee8082012-07-28 09:28:56 +0200273 /* mrc.bin reconfigures USB, so reinit it to have debug */
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300274 usbdebug_init();
Sven Schnelled4ee8082012-07-28 09:28:56 +0200275#endif
276
Stefan Reinauer00636b02012-04-04 00:08:51 +0200277 /* For reference print the System Agent version
278 * after executing the UEFI PEI stage.
279 */
280 u32 version = MCHBAR32(0x5034);
281 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
282 version >> 24 , (version >> 16) & 0xff,
283 (version >> 8) & 0xff, version & 0xff);
284
Duncan Laurie7b508dd2012-04-09 12:30:43 -0700285 /* Send ME init done for SandyBridge here. This is done
286 * inside the SystemAgent binary on IvyBridge. */
287 if (BASE_REV_SNB ==
288 (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
289 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
290 else
291 intel_early_me_status();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200292
Marc Jones313ec9d2012-11-02 14:26:44 -0600293 post_system_agent_init(pei_data);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200294 report_memory_config();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200295}