Angel Pons | c3f58f6 | 2020-04-05 15:46:41 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 3 | |
| 4 | #include <stddef.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame^] | 5 | #include <acpi/acpi.h> |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 6 | #include <assert.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 7 | #include <cbfs.h> |
| 8 | #include <cbmem.h> |
Patrick Rudolph | 45022ae | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 9 | #include <cf9_reset.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 10 | #include <console/console.h> |
Matt DeVillier | 85360723 | 2020-04-23 00:46:56 -0500 | [diff] [blame] | 11 | #include <device/dram/ddr3.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 12 | #include <device/pci_def.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 13 | #include <device/pci_ops.h> |
Kyösti Mälkki | f555a58 | 2020-01-06 19:41:42 +0200 | [diff] [blame] | 14 | #include <device/smbus_host.h> |
Aaron Durbin | decd062 | 2017-12-15 12:26:40 -0700 | [diff] [blame] | 15 | #include <mrc_cache.h> |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 16 | #include <soc/gpio.h> |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 17 | #include <soc/iomap.h> |
| 18 | #include <soc/iosf.h> |
| 19 | #include <soc/pci_devs.h> |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 20 | #include <soc/romstage.h> |
Aaron Durbin | 107b71c | 2014-01-09 14:35:41 -0600 | [diff] [blame] | 21 | #include <ec/google/chromeec/ec.h> |
| 22 | #include <ec/google/chromeec/ec_commands.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 23 | #include <security/vboot/vboot_common.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 24 | |
Kyösti Mälkki | f555a58 | 2020-01-06 19:41:42 +0200 | [diff] [blame] | 25 | uintptr_t smbus_base(void) |
| 26 | { |
| 27 | return SMBUS_BASE_ADDRESS; |
| 28 | } |
| 29 | |
| 30 | int smbus_enable_iobar(uintptr_t base) |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 31 | { |
| 32 | uint32_t reg; |
| 33 | const uint32_t smbus_dev = PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC); |
| 34 | |
| 35 | /* SMBus I/O BAR */ |
Kyösti Mälkki | f555a58 | 2020-01-06 19:41:42 +0200 | [diff] [blame] | 36 | reg = base | 2; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 37 | pci_write_config32(smbus_dev, PCI_BASE_ADDRESS_4, reg); |
| 38 | /* Enable decode of I/O space. */ |
| 39 | reg = pci_read_config16(smbus_dev, PCI_COMMAND); |
| 40 | reg |= 0x1; |
| 41 | pci_write_config16(smbus_dev, PCI_COMMAND, reg); |
| 42 | /* Enable Host Controller */ |
| 43 | reg = pci_read_config8(smbus_dev, 0x40); |
| 44 | reg |= 1; |
| 45 | pci_write_config8(smbus_dev, 0x40, reg); |
| 46 | |
| 47 | /* Configure pads to be used for SMBus */ |
| 48 | score_select_func(PCU_SMB_CLK_PAD, 1); |
| 49 | score_select_func(PCU_SMB_DATA_PAD, 1); |
Kyösti Mälkki | f555a58 | 2020-01-06 19:41:42 +0200 | [diff] [blame] | 50 | |
| 51 | return 0; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Aaron Durbin | 833ff35 | 2013-10-02 11:06:31 -0500 | [diff] [blame] | 54 | static void ABI_X86 send_to_console(unsigned char b) |
| 55 | { |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 56 | do_putchar(b); |
Aaron Durbin | 833ff35 | 2013-10-02 11:06:31 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Matt DeVillier | 85360723 | 2020-04-23 00:46:56 -0500 | [diff] [blame] | 59 | static void populate_smbios_tables(void *dram_data, int speed, int num_channels) |
| 60 | { |
| 61 | dimm_attr dimm; |
| 62 | enum spd_status status; |
| 63 | |
| 64 | /* Decode into dimm_attr struct */ |
| 65 | status = spd_decode_ddr3(&dimm, *(spd_raw_data *)dram_data); |
| 66 | |
| 67 | /* Some SPDs have bad CRCs, nothing we can do about it */ |
| 68 | if (status == SPD_STATUS_OK || status == SPD_STATUS_CRC_ERROR) { |
| 69 | /* Add table 17 entry for each channel */ |
| 70 | for (int i = 0; i < num_channels; i++) |
| 71 | spd_add_smbios17(i, 0, speed, &dimm); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void print_dram_info(void *dram_data) |
Aaron Durbin | 3ccb3ce | 2013-10-11 00:26:04 -0500 | [diff] [blame] | 76 | { |
| 77 | const int mrc_ver_reg = 0xf0; |
| 78 | const uint32_t soc_dev = PCI_DEV(0, SOC_DEV, SOC_FUNC); |
| 79 | uint32_t reg; |
| 80 | int num_channels; |
| 81 | int speed; |
| 82 | uint32_t ch0; |
| 83 | uint32_t ch1; |
| 84 | |
| 85 | reg = pci_read_config32(soc_dev, mrc_ver_reg); |
| 86 | |
| 87 | printk(BIOS_INFO, "MRC v%d.%02d\n", (reg >> 8) & 0xff, reg & 0xff); |
| 88 | |
| 89 | /* Number of channels enabled and DDR3 type. Determine number of |
| 90 | * channels by keying of the rank enable bits [3:0]. * */ |
| 91 | ch0 = iosf_dunit_ch0_read(DRP); |
| 92 | ch1 = iosf_dunit_ch1_read(DRP); |
| 93 | num_channels = 0; |
| 94 | if (ch0 & DRP_RANK_MASK) |
| 95 | num_channels++; |
| 96 | if (ch1 & DRP_RANK_MASK) |
| 97 | num_channels++; |
| 98 | |
| 99 | printk(BIOS_INFO, "%d channels of %sDDR3 @ ", num_channels, |
| 100 | (reg & (1 << 22)) ? "LP" : ""); |
| 101 | |
| 102 | /* DRAM frequency -- all channels run at same frequency. */ |
| 103 | reg = iosf_dunit_read(DTR0); |
| 104 | switch (reg & 0x3) { |
| 105 | case 0: |
| 106 | speed = 800; break; |
| 107 | case 1: |
| 108 | speed = 1066; break; |
| 109 | case 2: |
| 110 | speed = 1333; break; |
| 111 | case 3: |
| 112 | speed = 1600; break; |
| 113 | } |
| 114 | printk(BIOS_INFO, "%dMHz\n", speed); |
Matt DeVillier | 85360723 | 2020-04-23 00:46:56 -0500 | [diff] [blame] | 115 | |
| 116 | populate_smbios_tables(dram_data, speed, num_channels); |
Aaron Durbin | 3ccb3ce | 2013-10-11 00:26:04 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 119 | void raminit(struct mrc_params *mp, int prev_sleep_state) |
| 120 | { |
| 121 | int ret; |
| 122 | mrc_wrapper_entry_t mrc_entry; |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 123 | struct region_device rdev; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 124 | |
| 125 | /* Fill in default entries. */ |
| 126 | mp->version = MRC_PARAMS_VER; |
Aaron Durbin | 833ff35 | 2013-10-02 11:06:31 -0500 | [diff] [blame] | 127 | mp->console_out = &send_to_console; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 128 | mp->prev_sleep_state = prev_sleep_state; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 129 | mp->rmt_enabled = CONFIG(MRC_RMT); |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 130 | |
| 131 | /* Default to 2GiB IO hole. */ |
| 132 | if (!mp->io_hole_mb) |
| 133 | mp->io_hole_mb = 2048; |
| 134 | |
Furquan Shaikh | 0325dc6 | 2016-07-25 13:02:36 -0700 | [diff] [blame] | 135 | if (vboot_recovery_mode_enabled()) { |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 136 | printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n"); |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 137 | } else if (!mrc_cache_get_current(MRC_TRAINING_DATA, 0, &rdev)) { |
| 138 | mp->saved_data_size = region_device_sz(&rdev); |
| 139 | mp->saved_data = rdev_mmap_full(&rdev); |
| 140 | /* Assume boot device is memory mapped. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 141 | assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED)); |
Aaron Durbin | f5cfaa3 | 2016-07-13 23:20:07 -0500 | [diff] [blame] | 142 | } else if (prev_sleep_state == ACPI_S3) { |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 143 | /* If waking from S3 and no cache then. */ |
| 144 | printk(BIOS_DEBUG, "No MRC cache found in S3 resume path.\n"); |
| 145 | post_code(POST_RESUME_FAILURE); |
Patrick Rudolph | 45022ae | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 146 | system_reset(); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 147 | } else { |
| 148 | printk(BIOS_DEBUG, "No MRC cache found.\n"); |
| 149 | } |
| 150 | |
Aaron Durbin | 1131889 | 2014-04-02 20:46:13 -0500 | [diff] [blame] | 151 | /* Determine if mrc.bin is in the cbfs. */ |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 152 | if (cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL) == NULL) { |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 153 | printk(BIOS_DEBUG, "Couldn't find mrc.bin\n"); |
| 154 | return; |
| 155 | } |
Aaron Durbin | 1131889 | 2014-04-02 20:46:13 -0500 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * The entry point is currently the first instruction. Handle the |
| 159 | * case of an ELF file being put in the cbfs by setting the entry |
| 160 | * to the CONFIG_MRC_BIN_ADDRESS. |
| 161 | */ |
| 162 | mrc_entry = (void *)(uintptr_t)CONFIG_MRC_BIN_ADDRESS; |
| 163 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 164 | if (mp->mainboard.dram_info_location == DRAM_INFO_SPD_SMBUS) |
| 165 | enable_smbus(); |
| 166 | |
| 167 | ret = mrc_entry(mp); |
| 168 | |
Aaron Durbin | f5cfaa3 | 2016-07-13 23:20:07 -0500 | [diff] [blame] | 169 | if (prev_sleep_state != ACPI_S3) { |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 170 | cbmem_initialize_empty(); |
Aaron Durbin | 42e6856 | 2015-06-09 13:55:51 -0500 | [diff] [blame] | 171 | } else if (cbmem_initialize()) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 172 | #if CONFIG(HAVE_ACPI_RESUME) |
Aaron Durbin | 42e6856 | 2015-06-09 13:55:51 -0500 | [diff] [blame] | 173 | printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n"); |
| 174 | /* Failed S3 resume, reset to come up cleanly */ |
Patrick Rudolph | 45022ae | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 175 | system_reset(); |
Aaron Durbin | 42e6856 | 2015-06-09 13:55:51 -0500 | [diff] [blame] | 176 | #endif |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 177 | } |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 178 | |
Matt DeVillier | 85360723 | 2020-04-23 00:46:56 -0500 | [diff] [blame] | 179 | print_dram_info(mp->mainboard.dram_data[0]); |
| 180 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 181 | printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret); |
| 182 | printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save, |
| 183 | mp->data_to_save_size); |
| 184 | |
| 185 | if (mp->data_to_save != NULL && mp->data_to_save_size > 0) |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 186 | mrc_cache_stash_data(MRC_TRAINING_DATA, 0, mp->data_to_save, |
| 187 | mp->data_to_save_size); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 188 | } |