blob: df29de762ac8ca2ce44ce0d28ce9526fd4ed2990 [file] [log] [blame]
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050013 */
14
15#include <stddef.h>
Aaron Durbinf5cfaa32016-07-13 23:20:07 -050016#include <arch/acpi.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -060017#include <assert.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050018#include <cbfs.h>
19#include <cbmem.h>
Patrick Rudolph45022ae2018-10-01 19:17:11 +020020#include <cf9_reset.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050021#include <console/console.h>
22#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020023#include <device/pci_ops.h>
Kyösti Mälkkif555a582020-01-06 19:41:42 +020024#include <device/smbus_host.h>
Aaron Durbindecd0622017-12-15 12:26:40 -070025#include <mrc_cache.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070026#include <soc/gpio.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070027#include <soc/iomap.h>
28#include <soc/iosf.h>
29#include <soc/pci_devs.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070030#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
Kyösti Mälkkif555a582020-01-06 19:41:42 +020035uintptr_t smbus_base(void)
36{
37 return SMBUS_BASE_ADDRESS;
38}
39
40int smbus_enable_iobar(uintptr_t base)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050041{
42 uint32_t reg;
43 const uint32_t smbus_dev = PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC);
44
45 /* SMBus I/O BAR */
Kyösti Mälkkif555a582020-01-06 19:41:42 +020046 reg = base | 2;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050047 pci_write_config32(smbus_dev, PCI_BASE_ADDRESS_4, reg);
48 /* Enable decode of I/O space. */
49 reg = pci_read_config16(smbus_dev, PCI_COMMAND);
50 reg |= 0x1;
51 pci_write_config16(smbus_dev, PCI_COMMAND, reg);
52 /* Enable Host Controller */
53 reg = pci_read_config8(smbus_dev, 0x40);
54 reg |= 1;
55 pci_write_config8(smbus_dev, 0x40, reg);
56
57 /* Configure pads to be used for SMBus */
58 score_select_func(PCU_SMB_CLK_PAD, 1);
59 score_select_func(PCU_SMB_DATA_PAD, 1);
Kyösti Mälkkif555a582020-01-06 19:41:42 +020060
61 return 0;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050062}
63
Aaron Durbin833ff352013-10-02 11:06:31 -050064static void ABI_X86 send_to_console(unsigned char b)
65{
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020066 do_putchar(b);
Aaron Durbin833ff352013-10-02 11:06:31 -050067}
68
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -050069static void print_dram_info(void)
70{
71 const int mrc_ver_reg = 0xf0;
72 const uint32_t soc_dev = PCI_DEV(0, SOC_DEV, SOC_FUNC);
73 uint32_t reg;
74 int num_channels;
75 int speed;
76 uint32_t ch0;
77 uint32_t ch1;
78
79 reg = pci_read_config32(soc_dev, mrc_ver_reg);
80
81 printk(BIOS_INFO, "MRC v%d.%02d\n", (reg >> 8) & 0xff, reg & 0xff);
82
83 /* Number of channels enabled and DDR3 type. Determine number of
84 * channels by keying of the rank enable bits [3:0]. * */
85 ch0 = iosf_dunit_ch0_read(DRP);
86 ch1 = iosf_dunit_ch1_read(DRP);
87 num_channels = 0;
88 if (ch0 & DRP_RANK_MASK)
89 num_channels++;
90 if (ch1 & DRP_RANK_MASK)
91 num_channels++;
92
93 printk(BIOS_INFO, "%d channels of %sDDR3 @ ", num_channels,
94 (reg & (1 << 22)) ? "LP" : "");
95
96 /* DRAM frequency -- all channels run at same frequency. */
97 reg = iosf_dunit_read(DTR0);
98 switch (reg & 0x3) {
99 case 0:
100 speed = 800; break;
101 case 1:
102 speed = 1066; break;
103 case 2:
104 speed = 1333; break;
105 case 3:
106 speed = 1600; break;
107 }
108 printk(BIOS_INFO, "%dMHz\n", speed);
109}
110
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500111void raminit(struct mrc_params *mp, int prev_sleep_state)
112{
113 int ret;
114 mrc_wrapper_entry_t mrc_entry;
Aaron Durbin31be2c92016-12-03 22:08:20 -0600115 struct region_device rdev;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500116
117 /* Fill in default entries. */
118 mp->version = MRC_PARAMS_VER;
Aaron Durbin833ff352013-10-02 11:06:31 -0500119 mp->console_out = &send_to_console;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500120 mp->prev_sleep_state = prev_sleep_state;
Julius Wernercd49cce2019-03-05 16:53:33 -0800121 mp->rmt_enabled = CONFIG(MRC_RMT);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800122
123 /* Default to 2GiB IO hole. */
124 if (!mp->io_hole_mb)
125 mp->io_hole_mb = 2048;
126
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700127 if (vboot_recovery_mode_enabled()) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600128 printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n");
Aaron Durbin31be2c92016-12-03 22:08:20 -0600129 } else if (!mrc_cache_get_current(MRC_TRAINING_DATA, 0, &rdev)) {
130 mp->saved_data_size = region_device_sz(&rdev);
131 mp->saved_data = rdev_mmap_full(&rdev);
132 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800133 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500134 } else if (prev_sleep_state == ACPI_S3) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600135 /* If waking from S3 and no cache then. */
136 printk(BIOS_DEBUG, "No MRC cache found in S3 resume path.\n");
137 post_code(POST_RESUME_FAILURE);
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200138 system_reset();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500139 } else {
140 printk(BIOS_DEBUG, "No MRC cache found.\n");
141 }
142
Aaron Durbin11318892014-04-02 20:46:13 -0500143 /* Determine if mrc.bin is in the cbfs. */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500144 if (cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL) == NULL) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500145 printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
146 return;
147 }
Aaron Durbin11318892014-04-02 20:46:13 -0500148
149 /*
150 * The entry point is currently the first instruction. Handle the
151 * case of an ELF file being put in the cbfs by setting the entry
152 * to the CONFIG_MRC_BIN_ADDRESS.
153 */
154 mrc_entry = (void *)(uintptr_t)CONFIG_MRC_BIN_ADDRESS;
155
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500156 if (mp->mainboard.dram_info_location == DRAM_INFO_SPD_SMBUS)
157 enable_smbus();
158
159 ret = mrc_entry(mp);
160
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -0500161 print_dram_info();
162
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500163 if (prev_sleep_state != ACPI_S3) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600164 cbmem_initialize_empty();
Aaron Durbin42e68562015-06-09 13:55:51 -0500165 } else if (cbmem_initialize()) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800166 #if CONFIG(HAVE_ACPI_RESUME)
Aaron Durbin42e68562015-06-09 13:55:51 -0500167 printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n");
168 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200169 system_reset();
Aaron Durbin42e68562015-06-09 13:55:51 -0500170 #endif
Aaron Durbin6e328932013-11-06 12:04:50 -0600171 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500172
173 printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret);
174 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save,
175 mp->data_to_save_size);
176
177 if (mp->data_to_save != NULL && mp->data_to_save_size > 0)
Aaron Durbin31be2c92016-12-03 22:08:20 -0600178 mrc_cache_stash_data(MRC_TRAINING_DATA, 0, mp->data_to_save,
179 mp->data_to_save_size);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500180}