Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <bootmode.h> |
| 5 | #include <cbfs.h> |
Xi Chen | 555c2ae | 2022-01-21 11:43:53 +0800 | [diff] [blame^] | 6 | #include <cbmem.h> |
| 7 | #include <commonlib/bsd/mem_chip_info.h> |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 8 | #include <console/console.h> |
Xi Chen | 5c7a923 | 2022-01-04 19:00:44 +0800 | [diff] [blame] | 9 | #include <soc/dramc_common.h> |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 10 | #include <ip_checksum.h> |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 11 | #include <mrc_cache.h> |
| 12 | #include <soc/dramc_param.h> |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 13 | #include <soc/emi.h> |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 14 | #include <soc/mmu_operations.h> |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 15 | #include <symbols.h> |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 16 | #include <timer.h> |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 17 | |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 18 | /* This must be defined in chromeos.fmd in same name and size. */ |
| 19 | #define CALIBRATION_REGION "RW_MRC_CACHE" |
| 20 | #define CALIBRATION_REGION_SIZE 0x2000 |
| 21 | |
| 22 | _Static_assert(sizeof(struct dramc_param) <= CALIBRATION_REGION_SIZE, |
| 23 | "sizeof(struct dramc_param) exceeds " CALIBRATION_REGION); |
| 24 | |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 25 | const char *get_dram_geometry_str(u32 ddr_geometry); |
| 26 | const char *get_dram_type_str(u32 ddr_type); |
| 27 | |
Xi Chen | 555c2ae | 2022-01-21 11:43:53 +0800 | [diff] [blame^] | 28 | static const struct ddr_base_info *curr_ddr_info; |
| 29 | |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 30 | static int mt_mem_test(const struct dramc_data *dparam) |
| 31 | { |
| 32 | if (CONFIG(MEMORY_TEST)) { |
| 33 | u8 *addr = _dram; |
| 34 | const struct ddr_base_info *ddr_info = &dparam->ddr_info; |
| 35 | |
| 36 | for (u8 rank = RANK_0; rank < ddr_info->support_ranks; rank++) { |
Xi Chen | 3827f56 | 2020-10-20 17:55:14 +0800 | [diff] [blame] | 37 | int result = complex_mem_test(addr, 0x2000); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 38 | |
Xi Chen | 3827f56 | 2020-10-20 17:55:14 +0800 | [diff] [blame] | 39 | if (result != 0) { |
| 40 | printk(BIOS_ERR, |
| 41 | "[MEM] complex R/W mem test failed: %d\n", result); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 42 | return -1; |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 43 | } |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 44 | printk(BIOS_DEBUG, "[MEM] rank %u complex R/W mem test passed\n", rank); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 45 | |
| 46 | addr += ddr_info->rank_size[rank]; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 53 | const char *get_dram_geometry_str(u32 ddr_geometry) |
| 54 | { |
| 55 | const char *s; |
| 56 | |
| 57 | switch (ddr_geometry) { |
| 58 | case DDR_TYPE_2CH_2RK_4GB_2_2: |
| 59 | s = "2CH_2RK_4GB_2_2"; |
| 60 | break; |
| 61 | case DDR_TYPE_2CH_2RK_6GB_3_3: |
| 62 | s = "2CH_2RK_6GB_3_3"; |
| 63 | break; |
| 64 | case DDR_TYPE_2CH_2RK_8GB_4_4: |
| 65 | s = "2CH_2RK_8GB_4_4"; |
| 66 | break; |
| 67 | case DDR_TYPE_2CH_2RK_8GB_4_4_BYTE: |
| 68 | s = "2CH_2RK_8GB_4_4_BYTE"; |
| 69 | break; |
| 70 | case DDR_TYPE_2CH_1RK_4GB_4_0: |
| 71 | s = "2CH_1RK_4GB_4_0"; |
| 72 | break; |
| 73 | case DDR_TYPE_2CH_2RK_6GB_2_4: |
| 74 | s = "2CH_2RK_6GB_2_4"; |
| 75 | break; |
| 76 | default: |
| 77 | s = ""; |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | return s; |
| 82 | } |
| 83 | |
| 84 | const char *get_dram_type_str(u32 ddr_type) |
| 85 | { |
| 86 | const char *s; |
| 87 | |
| 88 | switch (ddr_type) { |
| 89 | case DDR_TYPE_DISCRETE: |
| 90 | s = "DSC"; |
| 91 | break; |
| 92 | case DDR_TYPE_EMCP: |
| 93 | s = "EMCP"; |
| 94 | break; |
| 95 | default: |
| 96 | s = ""; |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | return s; |
| 101 | } |
| 102 | |
Xi Chen | 555c2ae | 2022-01-21 11:43:53 +0800 | [diff] [blame^] | 103 | size_t mtk_dram_size(void) |
| 104 | { |
| 105 | size_t size = 0; |
| 106 | |
| 107 | if (!curr_ddr_info) |
| 108 | return 0; |
| 109 | for (unsigned int i = 0; i < RANK_MAX; ++i) |
| 110 | size += curr_ddr_info->mrr_info.mr8_density[i]; |
| 111 | return size; |
| 112 | } |
| 113 | |
| 114 | static void fill_dram_info(struct mem_chip_info *mc, const struct ddr_base_info *ddr) |
| 115 | { |
| 116 | unsigned int i; |
| 117 | size_t size; |
| 118 | |
| 119 | mc->type = MEM_CHIP_LPDDR4X; |
| 120 | mc->num_channels = CHANNEL_MAX; |
| 121 | size = mtk_dram_size(); |
| 122 | assert(size); |
| 123 | |
| 124 | for (i = 0; i < mc->num_channels; ++i) { |
| 125 | mc->channel[i].density = size / mc->num_channels; |
| 126 | mc->channel[i].io_width = DQ_DATA_WIDTH_LP4; |
| 127 | mc->channel[i].manufacturer_id = ddr->mrr_info.mr5_vendor_id; |
| 128 | mc->channel[i].revision_id[0] = ddr->mrr_info.mr6_revision_id; |
| 129 | mc->channel[i].revision_id[1] = ddr->mrr_info.mr7_revision_id; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void add_mem_chip_info(int unused) |
| 134 | { |
| 135 | struct mem_chip_info *mc; |
| 136 | size_t size; |
| 137 | |
| 138 | size = sizeof(*mc) + sizeof(struct mem_chip_channel) * CHANNEL_MAX; |
| 139 | mc = cbmem_add(CBMEM_ID_MEM_CHIP_INFO, size); |
| 140 | assert(mc); |
| 141 | |
| 142 | fill_dram_info(mc, curr_ddr_info); |
| 143 | } |
| 144 | ROMSTAGE_CBMEM_INIT_HOOK(add_mem_chip_info); |
| 145 | |
Xi Chen | 5c7a923 | 2022-01-04 19:00:44 +0800 | [diff] [blame] | 146 | static int run_dram_blob(struct dramc_param *dparam) |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 147 | { |
| 148 | /* Load and run the provided blob for full-calibration if available */ |
| 149 | struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram"); |
| 150 | |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 151 | dump_param_header(dparam); |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 152 | |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 153 | if (cbfs_prog_stage_load(&dram)) { |
| 154 | printk(BIOS_ERR, "DRAM-K: CBFS load program failed\n"); |
| 155 | return -2; |
| 156 | } |
| 157 | |
| 158 | dparam->do_putc = do_putchar; |
| 159 | |
| 160 | prog_set_entry(&dram, prog_entry(&dram), dparam); |
| 161 | prog_run(&dram); |
| 162 | if (dparam->header.status != DRAMC_SUCCESS) { |
Xi Chen | 5c7a923 | 2022-01-04 19:00:44 +0800 | [diff] [blame] | 163 | printk(BIOS_ERR, "DRAM-K: calibration failed: status = %d\n", |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 164 | dparam->header.status); |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 165 | return -3; |
| 166 | } |
| 167 | |
Xi Chen | 5c7a923 | 2022-01-04 19:00:44 +0800 | [diff] [blame] | 168 | if (!(dparam->header.config & DRAMC_CONFIG_FAST_K) |
| 169 | && !(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) { |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 170 | printk(BIOS_ERR, |
| 171 | "DRAM-K: Full calibration executed without saving parameters. " |
| 172 | "Please ensure the blob is built properly.\n"); |
| 173 | return -4; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Xi Chen | 5c7a923 | 2022-01-04 19:00:44 +0800 | [diff] [blame] | 179 | static int dram_run_fast_calibration(struct dramc_param *dparam) |
| 180 | { |
| 181 | const u16 config = CONFIG(MEDIATEK_DRAM_DVFS) ? DRAMC_ENABLE_DVFS : DRAMC_DISABLE_DVFS; |
| 182 | |
| 183 | if (dparam->dramc_datas.ddr_info.config_dvfs != config) { |
| 184 | printk(BIOS_WARNING, |
| 185 | "DRAM-K: Incompatible config for calibration data from flash " |
| 186 | "(expected: %#x, saved: %#x)\n", |
| 187 | config, dparam->dramc_datas.ddr_info.config_dvfs); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | printk(BIOS_INFO, "DRAM-K: DRAM calibration data valid pass\n"); |
| 192 | |
| 193 | if (CONFIG(MEDIATEK_BLOB_FAST_INIT)) { |
| 194 | printk(BIOS_INFO, "DRAM-K: Run fast calibration run in blob mode\n"); |
| 195 | |
| 196 | /* |
| 197 | * The loaded config should not contain FAST_K (done in full calibration), |
| 198 | * so we have to set that now to indicate the blob taking the config instead |
| 199 | * of generating a new config. |
| 200 | */ |
| 201 | dparam->header.config |= DRAMC_CONFIG_FAST_K; |
| 202 | |
| 203 | if (run_dram_blob(dparam) < 0) |
| 204 | return -3; |
| 205 | } else { |
| 206 | init_dram_by_params(dparam); |
| 207 | } |
| 208 | |
| 209 | if (mt_mem_test(&dparam->dramc_datas) < 0) |
| 210 | return -4; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int dram_run_full_calibration(struct dramc_param *dparam) |
| 216 | { |
| 217 | initialize_dramc_param(dparam); |
| 218 | |
| 219 | return run_dram_blob(dparam); |
| 220 | } |
| 221 | |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 222 | static void mem_init_set_default_config(struct dramc_param *dparam, |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 223 | const struct sdram_info *dram_info) |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 224 | { |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 225 | u32 type, geometry; |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 226 | memset(dparam, 0, sizeof(*dparam)); |
| 227 | |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 228 | type = dram_info->ddr_type; |
| 229 | geometry = dram_info->ddr_geometry; |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 230 | |
Yu-Ping Wu | c074f61 | 2021-04-12 11:03:57 +0800 | [diff] [blame] | 231 | dparam->dramc_datas.ddr_info.sdram.ddr_type = type; |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 232 | |
| 233 | if (CONFIG(MEDIATEK_DRAM_DVFS)) |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 234 | dparam->dramc_datas.ddr_info.config_dvfs = DRAMC_ENABLE_DVFS; |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 235 | |
Yu-Ping Wu | c074f61 | 2021-04-12 11:03:57 +0800 | [diff] [blame] | 236 | dparam->dramc_datas.ddr_info.sdram.ddr_geometry = geometry; |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 237 | |
| 238 | printk(BIOS_INFO, "DRAM-K: ddr_type: %s, config_dvfs: %d, ddr_geometry: %s\n", |
| 239 | get_dram_type_str(type), |
| 240 | dparam->dramc_datas.ddr_info.config_dvfs, |
| 241 | get_dram_geometry_str(geometry)); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 244 | static void mt_mem_init_run(struct dramc_param *dparam, |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 245 | const struct sdram_info *dram_info) |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 246 | { |
Xi Chen | f4bb77b | 2022-01-21 17:18:45 +0800 | [diff] [blame] | 247 | const ssize_t mrc_cache_size = sizeof(*dparam); |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 248 | ssize_t data_size; |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 249 | struct stopwatch sw; |
| 250 | int ret; |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 251 | |
| 252 | /* Load calibration params from flash and run fast calibration */ |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 253 | data_size = mrc_cache_load_current(MRC_TRAINING_DATA, |
| 254 | DRAMC_PARAM_HEADER_VERSION, |
Xi Chen | f4bb77b | 2022-01-21 17:18:45 +0800 | [diff] [blame] | 255 | dparam, mrc_cache_size); |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 256 | if (data_size == mrc_cache_size) { |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 257 | printk(BIOS_INFO, "DRAM-K: Running fast calibration\n"); |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 258 | stopwatch_init(&sw); |
| 259 | |
| 260 | ret = dram_run_fast_calibration(dparam); |
| 261 | if (ret != 0) { |
| 262 | printk(BIOS_ERR, "DRAM-K: Failed to run fast calibration " |
| 263 | "in %ld msecs, error: %d\n", |
| 264 | stopwatch_duration_msecs(&sw), ret); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 265 | |
| 266 | /* Erase flash data after fast calibration failed */ |
Xi Chen | f4bb77b | 2022-01-21 17:18:45 +0800 | [diff] [blame] | 267 | memset(dparam, 0xa5, mrc_cache_size); |
Yu-Ping Wu | ba49444 | 2021-04-15 10:06:27 +0800 | [diff] [blame] | 268 | mrc_cache_stash_data(MRC_TRAINING_DATA, |
| 269 | DRAMC_PARAM_HEADER_VERSION, |
Xi Chen | f4bb77b | 2022-01-21 17:18:45 +0800 | [diff] [blame] | 270 | dparam, mrc_cache_size); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 271 | } else { |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 272 | printk(BIOS_INFO, "DRAM-K: Fast calibration passed in %ld msecs\n", |
| 273 | stopwatch_duration_msecs(&sw)); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | } else { |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 277 | printk(BIOS_WARNING, "DRAM-K: Invalid data in flash (size: %#zx, expected: %#zx)\n", |
| 278 | data_size, mrc_cache_size); |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* Run full calibration */ |
| 282 | printk(BIOS_INFO, "DRAM-K: Running full calibration\n"); |
Xi Chen | e8c681c | 2021-03-03 17:58:07 +0800 | [diff] [blame] | 283 | mem_init_set_default_config(dparam, dram_info); |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 284 | |
| 285 | stopwatch_init(&sw); |
| 286 | int err = dram_run_full_calibration(dparam); |
| 287 | if (err == 0) { |
| 288 | printk(BIOS_INFO, "DRAM-K: Full calibration passed in %ld msecs\n", |
| 289 | stopwatch_duration_msecs(&sw)); |
Yu-Ping Wu | ba49444 | 2021-04-15 10:06:27 +0800 | [diff] [blame] | 290 | mrc_cache_stash_data(MRC_TRAINING_DATA, |
| 291 | DRAMC_PARAM_HEADER_VERSION, |
Xi Chen | f4bb77b | 2022-01-21 17:18:45 +0800 | [diff] [blame] | 292 | dparam, mrc_cache_size); |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame] | 293 | } else { |
| 294 | printk(BIOS_ERR, "DRAM-K: Full calibration failed in %ld msecs\n", |
| 295 | stopwatch_duration_msecs(&sw)); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 299 | void mt_mem_init(struct dramc_param *dparam) |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 300 | { |
| 301 | const struct sdram_info *sdram_param = get_sdram_config(); |
| 302 | |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 303 | mt_mem_init_run(dparam, sdram_param); |
| 304 | } |
| 305 | |
| 306 | void mtk_dram_init(void) |
| 307 | { |
| 308 | /* dramc_param is too large to fit in stack. */ |
| 309 | static struct dramc_param dramc_parameter; |
| 310 | mt_mem_init(&dramc_parameter); |
Xi Chen | 555c2ae | 2022-01-21 11:43:53 +0800 | [diff] [blame^] | 311 | curr_ddr_info = &dramc_parameter.dramc_datas.ddr_info; |
Yu-Ping Wu | 71c5ca7 | 2021-01-13 10:29:18 +0800 | [diff] [blame] | 312 | mtk_mmu_after_dram(); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 313 | } |