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