blob: 1577e688265a257453e066e1d2a7d2f34819b93e [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <console/console.h>
Kyösti Mälkki5687fc92013-11-28 18:11:49 +020021#include <bootmode.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050022#include <string.h>
23#include <arch/hlt.h>
24#include <arch/io.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050025#include <cbmem.h>
26#include <arch/cbfs.h>
27#include <cbfs.h>
28#include <ip_checksum.h>
29#include <pc80/mc146818rtc.h>
30#include <device/pci_def.h>
31#include "raminit.h"
32#include "pei_data.h"
33#include "haswell.h"
34
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060035void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050036{
Aaron Durbin76c37002012-10-30 09:03:43 -050037 struct mrc_data_container *mrcdata;
38 int output_len = ALIGN(pei_data->mrc_output_len, 16);
39
40 /* Save the MRC S3 restore data to cbmem */
Aaron Durbin76c37002012-10-30 09:03:43 -050041 mrcdata = cbmem_add
42 (CBMEM_ID_MRCDATA,
43 output_len + sizeof(struct mrc_data_container));
44
45 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
46 pei_data->mrc_output, mrcdata, output_len);
47
48 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
49 mrcdata->mrc_data_size = output_len;
50 mrcdata->reserved = 0;
51 memcpy(mrcdata->mrc_data, pei_data->mrc_output,
52 pei_data->mrc_output_len);
53
54 /* Zero the unused space in aligned buffer. */
55 if (output_len > pei_data->mrc_output_len)
56 memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0,
57 output_len - pei_data->mrc_output_len);
58
59 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
60 mrcdata->mrc_data_size);
Aaron Durbin76c37002012-10-30 09:03:43 -050061}
62
63static void prepare_mrc_cache(struct pei_data *pei_data)
64{
65 struct mrc_data_container *mrc_cache;
Aaron Durbin76c37002012-10-30 09:03:43 -050066
67 // preset just in case there is an error
68 pei_data->mrc_input = NULL;
69 pei_data->mrc_input_len = 0;
70
Aaron Durbin76c37002012-10-30 09:03:43 -050071 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
72 /* error message printed in find_current_mrc_cache */
73 return;
74 }
75
76 pei_data->mrc_input = mrc_cache->mrc_data;
77 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
78
79 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
80 __func__, pei_data->mrc_input,
81 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
82}
83
84static const char* ecc_decoder[] = {
85 "inactive",
86 "active on IO",
87 "disabled on IO",
88 "active"
89};
90
91/*
92 * Dump in the log memory controller configuration as read from the memory
93 * controller registers.
94 */
95static void report_memory_config(void)
96{
97 u32 addr_decoder_common, addr_decode_ch[2];
98 int i;
99
100 addr_decoder_common = MCHBAR32(0x5000);
101 addr_decode_ch[0] = MCHBAR32(0x5004);
102 addr_decode_ch[1] = MCHBAR32(0x5008);
103
104 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
105 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
106 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
107 addr_decoder_common & 3,
108 (addr_decoder_common >> 2) & 3,
109 (addr_decoder_common >> 4) & 3);
110
111 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
112 u32 ch_conf = addr_decode_ch[i];
113 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
114 i, ch_conf);
115 printk(BIOS_DEBUG, " ECC %s\n",
116 ecc_decoder[(ch_conf >> 24) & 3]);
117 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
118 ((ch_conf >> 22) & 1) ? "on" : "off");
119 printk(BIOS_DEBUG, " rank interleave %s\n",
120 ((ch_conf >> 21) & 1) ? "on" : "off");
Duncan Laurie8d774022013-10-22 16:32:49 -0700121 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500122 ((ch_conf >> 0) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -0700123 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500124 ((ch_conf >> 17) & 1) ? "dual" : "single",
125 ((ch_conf >> 16) & 1) ? "" : ", selected");
Duncan Laurie8d774022013-10-22 16:32:49 -0700126 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500127 ((ch_conf >> 8) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -0700128 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500129 ((ch_conf >> 18) & 1) ? "dual" : "single",
130 ((ch_conf >> 16) & 1) ? ", selected" : "");
131 }
132}
133
134/**
135 * Find PEI executable in coreboot filesystem and execute it.
136 *
137 * @param pei_data: configuration data for UEFI PEI reference code
138 */
139void sdram_initialize(struct pei_data *pei_data)
140{
Aaron Durbin76c37002012-10-30 09:03:43 -0500141 unsigned long entry;
142
Aaron Durbin76c37002012-10-30 09:03:43 -0500143 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
144
Aaron Durbin76c37002012-10-30 09:03:43 -0500145 /*
146 * Do not pass MRC data in for recovery mode boot,
147 * Always pass it in for S3 resume.
148 */
149 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
150 prepare_mrc_cache(pei_data);
151
152 /* If MRC data is not found we cannot continue S3 resume. */
153 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700154 post_code(POST_RESUME_FAILURE);
155 printk(BIOS_DEBUG, "Giving up in sdram_initialize: "
156 "No MRC data\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500157 outb(0x6, 0xcf9);
158 while(1) {
159 hlt();
160 }
161 }
162
163 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200164 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500165
166 /* Locate and call UEFI System Agent binary. */
167 entry = (unsigned long)cbfs_get_file_content(
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100168 CBFS_DEFAULT_MEDIA, "mrc.bin", 0xab, NULL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500169 if (entry) {
170 int rv;
171 asm volatile (
172 "call *%%ecx\n\t"
173 :"=a" (rv) : "c" (entry), "a" (pei_data));
174 if (rv) {
175 switch (rv) {
176 case -1:
177 printk(BIOS_ERR, "PEI version mismatch.\n");
178 break;
179 case -2:
180 printk(BIOS_ERR, "Invalid memory frequency.\n");
181 break;
182 default:
183 printk(BIOS_ERR, "MRC returned %x.\n", rv);
184 }
185 die("Nonzero MRC return value.\n");
186 }
187 } else {
188 die("UEFI PEI System Agent not found.\n");
189 }
190
191 /* For reference print the System Agent version
192 * after executing the UEFI PEI stage.
193 */
194 u32 version = MCHBAR32(0x5034);
195 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
196 version >> 24 , (version >> 16) & 0xff,
197 (version >> 8) & 0xff, version & 0xff);
198
Aaron Durbin76c37002012-10-30 09:03:43 -0500199 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500200}