Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 1 | /* |
| 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 Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame^] | 17 | * Foundation, Inc. |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | #include <arch/io.h> |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 22 | #include <bootmode.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 23 | #include <cbfs.h> |
| 24 | #include <cbmem.h> |
| 25 | #include <console/console.h> |
| 26 | #include <device/pci_def.h> |
Patrick Georgi | bd79c5e | 2014-11-28 22:35:36 +0100 | [diff] [blame] | 27 | #include <halt.h> |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 28 | #include <stage_cache.h> |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 29 | #include <soc/gpio.h> |
Duncan Laurie | d8c4f2b | 2014-04-22 10:46:06 -0700 | [diff] [blame] | 30 | #include <soc/intel/common/mrc_cache.h> |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 31 | #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 Durbin | 107b71c | 2014-01-09 14:35:41 -0600 | [diff] [blame] | 36 | #include <ec/google/chromeec/ec.h> |
| 37 | #include <ec/google/chromeec/ec_commands.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 38 | |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 39 | static void reset_system(void) |
| 40 | { |
| 41 | warm_reset(); |
Patrick Georgi | bd79c5e | 2014-11-28 22:35:36 +0100 | [diff] [blame] | 42 | halt(); |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 43 | } |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 44 | |
| 45 | static 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 Durbin | 833ff35 | 2013-10-02 11:06:31 -0500 | [diff] [blame] | 67 | static void ABI_X86 send_to_console(unsigned char b) |
| 68 | { |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 69 | do_putchar(b); |
Aaron Durbin | 833ff35 | 2013-10-02 11:06:31 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Aaron Durbin | 3ccb3ce | 2013-10-11 00:26:04 -0500 | [diff] [blame] | 72 | static 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 Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 114 | void 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 Durbin | 833ff35 | 2013-10-02 11:06:31 -0500 | [diff] [blame] | 122 | mp->console_out = &send_to_console; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 123 | mp->prev_sleep_state = prev_sleep_state; |
Patrick Georgi | 5b33dc1 | 2014-05-07 20:20:10 +0200 | [diff] [blame] | 124 | mp->rmt_enabled = IS_ENABLED(CONFIG_MRC_RMT); |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 125 | |
| 126 | /* Default to 2GiB IO hole. */ |
| 127 | if (!mp->io_hole_mb) |
| 128 | mp->io_hole_mb = 2048; |
| 129 | |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 130 | if (recovery_mode_enabled()) { |
| 131 | printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n"); |
| 132 | } else if (!mrc_cache_get_current(&cache)) { |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 133 | mp->saved_data_size = cache->size; |
| 134 | mp->saved_data = &cache->data[0]; |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 135 | } 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 Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 140 | } else { |
| 141 | printk(BIOS_DEBUG, "No MRC cache found.\n"); |
Aaron Durbin | 107b71c | 2014-01-09 14:35:41 -0600 | [diff] [blame] | 142 | #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 Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Aaron Durbin | 1131889 | 2014-04-02 20:46:13 -0500 | [diff] [blame] | 150 | /* Determine if mrc.bin is in the cbfs. */ |
Aaron Durbin | a30f7e6 | 2015-03-31 20:33:53 -0500 | [diff] [blame] | 151 | if (cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "mrc.bin", CBFS_TYPE_MRC, |
| 152 | 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 | 3ccb3ce | 2013-10-11 00:26:04 -0500 | [diff] [blame] | 169 | print_dram_info(); |
| 170 | |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 171 | if (prev_sleep_state != 3) { |
| 172 | cbmem_initialize_empty(); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 173 | stage_cache_create_empty(); |
| 174 | } else { |
| 175 | stage_cache_recover(); |
| 176 | if (cbmem_initialize()) { |
| 177 | #if CONFIG_HAVE_ACPI_RESUME |
| 178 | printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n"); |
| 179 | /* Failed S3 resume, reset to come up cleanly */ |
| 180 | reset_system(); |
| 181 | #endif |
| 182 | } |
Aaron Durbin | 6e32893 | 2013-11-06 12:04:50 -0600 | [diff] [blame] | 183 | } |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 184 | |
| 185 | printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret); |
| 186 | printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save, |
| 187 | mp->data_to_save_size); |
| 188 | |
| 189 | if (mp->data_to_save != NULL && mp->data_to_save_size > 0) |
| 190 | mrc_cache_stash_data(mp->data_to_save, mp->data_to_save_size); |
| 191 | } |