blob: 191821ad5b9687348bce912b3d9ff15ef996348b [file] [log] [blame]
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 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
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050018 */
19
20#include <stddef.h>
21#include <arch/io.h>
Aaron Durbin6e328932013-11-06 12:04:50 -060022#include <bootmode.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050023#include <cbfs.h>
24#include <cbmem.h>
25#include <console/console.h>
26#include <device/pci_def.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010027#include <halt.h>
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060028#include <stage_cache.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070029#include <soc/gpio.h>
Duncan Lauried8c4f2b2014-04-22 10:46:06 -070030#include <soc/intel/common/mrc_cache.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070031#include <soc/iomap.h>
32#include <soc/iosf.h>
33#include <soc/pci_devs.h>
34#include <soc/reset.h>
35#include <soc/romstage.h>
Aaron Durbin107b71c2014-01-09 14:35:41 -060036#include <ec/google/chromeec/ec.h>
37#include <ec/google/chromeec/ec_commands.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050038
Aaron Durbin6e328932013-11-06 12:04:50 -060039static void reset_system(void)
40{
41 warm_reset();
Patrick Georgibd79c5e2014-11-28 22:35:36 +010042 halt();
Aaron Durbin6e328932013-11-06 12:04:50 -060043}
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050044
45static void enable_smbus(void)
46{
47 uint32_t reg;
48 const uint32_t smbus_dev = PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC);
49
50 /* SMBus I/O BAR */
51 reg = SMBUS_BASE_ADDRESS | 2;
52 pci_write_config32(smbus_dev, PCI_BASE_ADDRESS_4, reg);
53 /* Enable decode of I/O space. */
54 reg = pci_read_config16(smbus_dev, PCI_COMMAND);
55 reg |= 0x1;
56 pci_write_config16(smbus_dev, PCI_COMMAND, reg);
57 /* Enable Host Controller */
58 reg = pci_read_config8(smbus_dev, 0x40);
59 reg |= 1;
60 pci_write_config8(smbus_dev, 0x40, reg);
61
62 /* Configure pads to be used for SMBus */
63 score_select_func(PCU_SMB_CLK_PAD, 1);
64 score_select_func(PCU_SMB_DATA_PAD, 1);
65}
66
Aaron Durbin833ff352013-10-02 11:06:31 -050067static void ABI_X86 send_to_console(unsigned char b)
68{
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020069 do_putchar(b);
Aaron Durbin833ff352013-10-02 11:06:31 -050070}
71
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -050072static void print_dram_info(void)
73{
74 const int mrc_ver_reg = 0xf0;
75 const uint32_t soc_dev = PCI_DEV(0, SOC_DEV, SOC_FUNC);
76 uint32_t reg;
77 int num_channels;
78 int speed;
79 uint32_t ch0;
80 uint32_t ch1;
81
82 reg = pci_read_config32(soc_dev, mrc_ver_reg);
83
84 printk(BIOS_INFO, "MRC v%d.%02d\n", (reg >> 8) & 0xff, reg & 0xff);
85
86 /* Number of channels enabled and DDR3 type. Determine number of
87 * channels by keying of the rank enable bits [3:0]. * */
88 ch0 = iosf_dunit_ch0_read(DRP);
89 ch1 = iosf_dunit_ch1_read(DRP);
90 num_channels = 0;
91 if (ch0 & DRP_RANK_MASK)
92 num_channels++;
93 if (ch1 & DRP_RANK_MASK)
94 num_channels++;
95
96 printk(BIOS_INFO, "%d channels of %sDDR3 @ ", num_channels,
97 (reg & (1 << 22)) ? "LP" : "");
98
99 /* DRAM frequency -- all channels run at same frequency. */
100 reg = iosf_dunit_read(DTR0);
101 switch (reg & 0x3) {
102 case 0:
103 speed = 800; break;
104 case 1:
105 speed = 1066; break;
106 case 2:
107 speed = 1333; break;
108 case 3:
109 speed = 1600; break;
110 }
111 printk(BIOS_INFO, "%dMHz\n", speed);
112}
113
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500114void raminit(struct mrc_params *mp, int prev_sleep_state)
115{
116 int ret;
117 mrc_wrapper_entry_t mrc_entry;
118 const struct mrc_saved_data *cache;
119
120 /* Fill in default entries. */
121 mp->version = MRC_PARAMS_VER;
Aaron Durbin833ff352013-10-02 11:06:31 -0500122 mp->console_out = &send_to_console;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500123 mp->prev_sleep_state = prev_sleep_state;
Patrick Georgi5b33dc12014-05-07 20:20:10 +0200124 mp->rmt_enabled = IS_ENABLED(CONFIG_MRC_RMT);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800125
126 /* Default to 2GiB IO hole. */
127 if (!mp->io_hole_mb)
128 mp->io_hole_mb = 2048;
129
Aaron Durbin6e328932013-11-06 12:04:50 -0600130 if (recovery_mode_enabled()) {
131 printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n");
132 } else if (!mrc_cache_get_current(&cache)) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500133 mp->saved_data_size = cache->size;
134 mp->saved_data = &cache->data[0];
Aaron Durbin6e328932013-11-06 12:04:50 -0600135 } else if (prev_sleep_state == 3) {
136 /* If waking from S3 and no cache then. */
137 printk(BIOS_DEBUG, "No MRC cache found in S3 resume path.\n");
138 post_code(POST_RESUME_FAILURE);
139 reset_system();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500140 } else {
141 printk(BIOS_DEBUG, "No MRC cache found.\n");
Aaron Durbin107b71c2014-01-09 14:35:41 -0600142#if CONFIG_EC_GOOGLE_CHROMEEC
143 if (prev_sleep_state == 0) {
144 /* Ensure EC is running RO firmware. */
145 google_chromeec_check_ec_image(EC_IMAGE_RO);
146 }
147#endif
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500148 }
149
Aaron Durbin11318892014-04-02 20:46:13 -0500150 /* Determine if mrc.bin is in the cbfs. */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500151 if (cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL) == NULL) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500152 printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
153 return;
154 }
Aaron Durbin11318892014-04-02 20:46:13 -0500155
156 /*
157 * The entry point is currently the first instruction. Handle the
158 * case of an ELF file being put in the cbfs by setting the entry
159 * to the CONFIG_MRC_BIN_ADDRESS.
160 */
161 mrc_entry = (void *)(uintptr_t)CONFIG_MRC_BIN_ADDRESS;
162
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500163 if (mp->mainboard.dram_info_location == DRAM_INFO_SPD_SMBUS)
164 enable_smbus();
165
166 ret = mrc_entry(mp);
167
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -0500168 print_dram_info();
169
Aaron Durbin6e328932013-11-06 12:04:50 -0600170 if (prev_sleep_state != 3) {
171 cbmem_initialize_empty();
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600172 stage_cache_create_empty();
173 } else {
174 stage_cache_recover();
175 if (cbmem_initialize()) {
176 #if CONFIG_HAVE_ACPI_RESUME
177 printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n");
178 /* Failed S3 resume, reset to come up cleanly */
179 reset_system();
180 #endif
181 }
Aaron Durbin6e328932013-11-06 12:04:50 -0600182 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500183
184 printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret);
185 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save,
186 mp->data_to_save_size);
187
188 if (mp->data_to_save != NULL && mp->data_to_save_size > 0)
189 mrc_cache_stash_data(mp->data_to_save, mp->data_to_save_size);
190}