blob: 92a453232b5797af5b6f3f00d84aeb951f4635b2 [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.
Aaron Durbin76c37002012-10-30 09:03:43 -050014 */
15
16#include <console/console.h>
Kyösti Mälkki5687fc92013-11-28 18:11:49 +020017#include <bootmode.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include <string.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include <arch/io.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050020#include <cbmem.h>
21#include <arch/cbfs.h>
22#include <cbfs.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010023#include <halt.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050024#include <ip_checksum.h>
25#include <pc80/mc146818rtc.h>
26#include <device/pci_def.h>
27#include "raminit.h"
28#include "pei_data.h"
29#include "haswell.h"
30
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060031void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050032{
Aaron Durbin76c37002012-10-30 09:03:43 -050033 struct mrc_data_container *mrcdata;
34 int output_len = ALIGN(pei_data->mrc_output_len, 16);
35
36 /* Save the MRC S3 restore data to cbmem */
Aaron Durbin76c37002012-10-30 09:03:43 -050037 mrcdata = cbmem_add
38 (CBMEM_ID_MRCDATA,
39 output_len + sizeof(struct mrc_data_container));
40
41 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
42 pei_data->mrc_output, mrcdata, output_len);
43
44 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
45 mrcdata->mrc_data_size = output_len;
46 mrcdata->reserved = 0;
47 memcpy(mrcdata->mrc_data, pei_data->mrc_output,
48 pei_data->mrc_output_len);
49
50 /* Zero the unused space in aligned buffer. */
51 if (output_len > pei_data->mrc_output_len)
52 memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0,
53 output_len - pei_data->mrc_output_len);
54
55 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
56 mrcdata->mrc_data_size);
Aaron Durbin76c37002012-10-30 09:03:43 -050057}
58
59static void prepare_mrc_cache(struct pei_data *pei_data)
60{
61 struct mrc_data_container *mrc_cache;
Aaron Durbin76c37002012-10-30 09:03:43 -050062
63 // preset just in case there is an error
64 pei_data->mrc_input = NULL;
65 pei_data->mrc_input_len = 0;
66
Aaron Durbin76c37002012-10-30 09:03:43 -050067 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
68 /* error message printed in find_current_mrc_cache */
69 return;
70 }
71
72 pei_data->mrc_input = mrc_cache->mrc_data;
73 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
74
75 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
76 __func__, pei_data->mrc_input,
77 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
78}
79
80static const char* ecc_decoder[] = {
81 "inactive",
82 "active on IO",
83 "disabled on IO",
84 "active"
85};
86
87/*
88 * Dump in the log memory controller configuration as read from the memory
89 * controller registers.
90 */
91static void report_memory_config(void)
92{
93 u32 addr_decoder_common, addr_decode_ch[2];
94 int i;
95
96 addr_decoder_common = MCHBAR32(0x5000);
97 addr_decode_ch[0] = MCHBAR32(0x5004);
98 addr_decode_ch[1] = MCHBAR32(0x5008);
99
100 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
101 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
102 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
103 addr_decoder_common & 3,
104 (addr_decoder_common >> 2) & 3,
105 (addr_decoder_common >> 4) & 3);
106
107 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
108 u32 ch_conf = addr_decode_ch[i];
109 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
110 i, ch_conf);
111 printk(BIOS_DEBUG, " ECC %s\n",
112 ecc_decoder[(ch_conf >> 24) & 3]);
113 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
114 ((ch_conf >> 22) & 1) ? "on" : "off");
115 printk(BIOS_DEBUG, " rank interleave %s\n",
116 ((ch_conf >> 21) & 1) ? "on" : "off");
Duncan Laurie8d774022013-10-22 16:32:49 -0700117 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500118 ((ch_conf >> 0) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -0700119 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500120 ((ch_conf >> 17) & 1) ? "dual" : "single",
121 ((ch_conf >> 16) & 1) ? "" : ", selected");
Duncan Laurie8d774022013-10-22 16:32:49 -0700122 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500123 ((ch_conf >> 8) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -0700124 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500125 ((ch_conf >> 18) & 1) ? "dual" : "single",
126 ((ch_conf >> 16) & 1) ? ", selected" : "");
127 }
128}
129
130/**
131 * Find PEI executable in coreboot filesystem and execute it.
132 *
133 * @param pei_data: configuration data for UEFI PEI reference code
134 */
135void sdram_initialize(struct pei_data *pei_data)
136{
Aaron Durbin76c37002012-10-30 09:03:43 -0500137 unsigned long entry;
138
Aaron Durbin76c37002012-10-30 09:03:43 -0500139 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
140
Aaron Durbin76c37002012-10-30 09:03:43 -0500141 /*
142 * Do not pass MRC data in for recovery mode boot,
143 * Always pass it in for S3 resume.
144 */
145 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
146 prepare_mrc_cache(pei_data);
147
148 /* If MRC data is not found we cannot continue S3 resume. */
149 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700150 post_code(POST_RESUME_FAILURE);
151 printk(BIOS_DEBUG, "Giving up in sdram_initialize: "
152 "No MRC data\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500153 outb(0x6, 0xcf9);
Patrick Georgibd79c5e2014-11-28 22:35:36 +0100154 halt();
Aaron Durbin76c37002012-10-30 09:03:43 -0500155 }
156
157 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200158 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500159
160 /* Locate and call UEFI System Agent binary. */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500161 entry = (unsigned long)cbfs_boot_map_with_leak("mrc.bin",
162 CBFS_TYPE_MRC, NULL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500163 if (entry) {
164 int rv;
165 asm volatile (
166 "call *%%ecx\n\t"
167 :"=a" (rv) : "c" (entry), "a" (pei_data));
168 if (rv) {
169 switch (rv) {
170 case -1:
171 printk(BIOS_ERR, "PEI version mismatch.\n");
172 break;
173 case -2:
174 printk(BIOS_ERR, "Invalid memory frequency.\n");
175 break;
176 default:
177 printk(BIOS_ERR, "MRC returned %x.\n", rv);
178 }
179 die("Nonzero MRC return value.\n");
180 }
181 } else {
182 die("UEFI PEI System Agent not found.\n");
183 }
184
185 /* For reference print the System Agent version
186 * after executing the UEFI PEI stage.
187 */
188 u32 version = MCHBAR32(0x5034);
189 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
190 version >> 24 , (version >> 16) & 0xff,
191 (version >> 8) & 0xff, version & 0xff);
192
Aaron Durbin76c37002012-10-30 09:03:43 -0500193 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500194}