blob: b577a35ece89335819fc50dca9aceaef2d029f1f [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.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050014 */
15
16#include <stddef.h>
Aaron Durbinf5cfaa32016-07-13 23:20:07 -050017#include <arch/acpi.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -060018#include <assert.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050019#include <cbfs.h>
20#include <cbmem.h>
21#include <console/console.h>
22#include <device/pci_def.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010023#include <halt.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070024#include <soc/gpio.h>
Duncan Lauried8c4f2b2014-04-22 10:46:06 -070025#include <soc/intel/common/mrc_cache.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070026#include <soc/iomap.h>
27#include <soc/iosf.h>
28#include <soc/pci_devs.h>
29#include <soc/reset.h>
30#include <soc/romstage.h>
Aaron Durbin107b71c2014-01-09 14:35:41 -060031#include <ec/google/chromeec/ec.h>
32#include <ec/google/chromeec/ec_commands.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020033#include <security/vboot/vboot_common.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050034
Aaron Durbin6e328932013-11-06 12:04:50 -060035static void reset_system(void)
36{
37 warm_reset();
Patrick Georgibd79c5e2014-11-28 22:35:36 +010038 halt();
Aaron Durbin6e328932013-11-06 12:04:50 -060039}
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050040
41static void enable_smbus(void)
42{
43 uint32_t reg;
44 const uint32_t smbus_dev = PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC);
45
46 /* SMBus I/O BAR */
47 reg = SMBUS_BASE_ADDRESS | 2;
48 pci_write_config32(smbus_dev, PCI_BASE_ADDRESS_4, reg);
49 /* Enable decode of I/O space. */
50 reg = pci_read_config16(smbus_dev, PCI_COMMAND);
51 reg |= 0x1;
52 pci_write_config16(smbus_dev, PCI_COMMAND, reg);
53 /* Enable Host Controller */
54 reg = pci_read_config8(smbus_dev, 0x40);
55 reg |= 1;
56 pci_write_config8(smbus_dev, 0x40, reg);
57
58 /* Configure pads to be used for SMBus */
59 score_select_func(PCU_SMB_CLK_PAD, 1);
60 score_select_func(PCU_SMB_DATA_PAD, 1);
61}
62
Aaron Durbin833ff352013-10-02 11:06:31 -050063static void ABI_X86 send_to_console(unsigned char b)
64{
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020065 do_putchar(b);
Aaron Durbin833ff352013-10-02 11:06:31 -050066}
67
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -050068static void print_dram_info(void)
69{
70 const int mrc_ver_reg = 0xf0;
71 const uint32_t soc_dev = PCI_DEV(0, SOC_DEV, SOC_FUNC);
72 uint32_t reg;
73 int num_channels;
74 int speed;
75 uint32_t ch0;
76 uint32_t ch1;
77
78 reg = pci_read_config32(soc_dev, mrc_ver_reg);
79
80 printk(BIOS_INFO, "MRC v%d.%02d\n", (reg >> 8) & 0xff, reg & 0xff);
81
82 /* Number of channels enabled and DDR3 type. Determine number of
83 * channels by keying of the rank enable bits [3:0]. * */
84 ch0 = iosf_dunit_ch0_read(DRP);
85 ch1 = iosf_dunit_ch1_read(DRP);
86 num_channels = 0;
87 if (ch0 & DRP_RANK_MASK)
88 num_channels++;
89 if (ch1 & DRP_RANK_MASK)
90 num_channels++;
91
92 printk(BIOS_INFO, "%d channels of %sDDR3 @ ", num_channels,
93 (reg & (1 << 22)) ? "LP" : "");
94
95 /* DRAM frequency -- all channels run at same frequency. */
96 reg = iosf_dunit_read(DTR0);
97 switch (reg & 0x3) {
98 case 0:
99 speed = 800; break;
100 case 1:
101 speed = 1066; break;
102 case 2:
103 speed = 1333; break;
104 case 3:
105 speed = 1600; break;
106 }
107 printk(BIOS_INFO, "%dMHz\n", speed);
108}
109
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500110void raminit(struct mrc_params *mp, int prev_sleep_state)
111{
112 int ret;
113 mrc_wrapper_entry_t mrc_entry;
Aaron Durbin31be2c92016-12-03 22:08:20 -0600114 struct region_device rdev;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500115
116 /* Fill in default entries. */
117 mp->version = MRC_PARAMS_VER;
Aaron Durbin833ff352013-10-02 11:06:31 -0500118 mp->console_out = &send_to_console;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500119 mp->prev_sleep_state = prev_sleep_state;
Patrick Georgi5b33dc12014-05-07 20:20:10 +0200120 mp->rmt_enabled = IS_ENABLED(CONFIG_MRC_RMT);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800121
122 /* Default to 2GiB IO hole. */
123 if (!mp->io_hole_mb)
124 mp->io_hole_mb = 2048;
125
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700126 if (vboot_recovery_mode_enabled()) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600127 printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n");
Aaron Durbin31be2c92016-12-03 22:08:20 -0600128 } else if (!mrc_cache_get_current(MRC_TRAINING_DATA, 0, &rdev)) {
129 mp->saved_data_size = region_device_sz(&rdev);
130 mp->saved_data = rdev_mmap_full(&rdev);
131 /* Assume boot device is memory mapped. */
132 assert(IS_ENABLED(CONFIG_BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500133 } else if (prev_sleep_state == ACPI_S3) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600134 /* If waking from S3 and no cache then. */
135 printk(BIOS_DEBUG, "No MRC cache found in S3 resume path.\n");
136 post_code(POST_RESUME_FAILURE);
137 reset_system();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500138 } else {
139 printk(BIOS_DEBUG, "No MRC cache found.\n");
140 }
141
Aaron Durbin11318892014-04-02 20:46:13 -0500142 /* Determine if mrc.bin is in the cbfs. */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500143 if (cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL) == NULL) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500144 printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
145 return;
146 }
Aaron Durbin11318892014-04-02 20:46:13 -0500147
148 /*
149 * The entry point is currently the first instruction. Handle the
150 * case of an ELF file being put in the cbfs by setting the entry
151 * to the CONFIG_MRC_BIN_ADDRESS.
152 */
153 mrc_entry = (void *)(uintptr_t)CONFIG_MRC_BIN_ADDRESS;
154
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500155 if (mp->mainboard.dram_info_location == DRAM_INFO_SPD_SMBUS)
156 enable_smbus();
157
158 ret = mrc_entry(mp);
159
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -0500160 print_dram_info();
161
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500162 if (prev_sleep_state != ACPI_S3) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600163 cbmem_initialize_empty();
Aaron Durbin42e68562015-06-09 13:55:51 -0500164 } else if (cbmem_initialize()) {
Martin Rothe6ff1592017-06-24 21:34:29 -0600165 #if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)
Aaron Durbin42e68562015-06-09 13:55:51 -0500166 printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n");
167 /* Failed S3 resume, reset to come up cleanly */
168 reset_system();
169 #endif
Aaron Durbin6e328932013-11-06 12:04:50 -0600170 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500171
172 printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret);
173 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save,
174 mp->data_to_save_size);
175
176 if (mp->data_to_save != NULL && mp->data_to_save_size > 0)
Aaron Durbin31be2c92016-12-03 22:08:20 -0600177 mrc_cache_stash_data(MRC_TRAINING_DATA, 0, mp->data_to_save,
178 mp->data_to_save_size);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500179}