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> |
| 7 | #include <ip_checksum.h> |
| 8 | #include <soc/emi.h> |
| 9 | #include <symbols.h> |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 10 | #include <timer.h> |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 11 | |
| 12 | static int mt_mem_test(const struct dramc_data *dparam) |
| 13 | { |
| 14 | if (CONFIG(MEMORY_TEST)) { |
| 15 | u8 *addr = _dram; |
| 16 | const struct ddr_base_info *ddr_info = &dparam->ddr_info; |
| 17 | |
| 18 | for (u8 rank = RANK_0; rank < ddr_info->support_ranks; rank++) { |
Xi Chen | 3827f56 | 2020-10-20 17:55:14 +0800 | [diff] [blame] | 19 | int result = complex_mem_test(addr, 0x2000); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 20 | |
Xi Chen | 3827f56 | 2020-10-20 17:55:14 +0800 | [diff] [blame] | 21 | if (result != 0) { |
| 22 | printk(BIOS_ERR, |
| 23 | "[MEM] complex R/W mem test failed: %d\n", result); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 24 | return -1; |
Xi Chen | 3827f56 | 2020-10-20 17:55:14 +0800 | [diff] [blame] | 25 | } else { |
| 26 | printk(BIOS_DEBUG, "[MEM] complex R/W mem test passed\n"); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | addr += ddr_info->rank_size[rank]; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static u32 compute_checksum(const struct dramc_param *dparam) |
| 37 | { |
| 38 | return (u32)compute_ip_checksum(&dparam->dramc_datas, |
| 39 | sizeof(dparam->dramc_datas)); |
| 40 | } |
| 41 | |
| 42 | static int dram_run_fast_calibration(const struct dramc_param *dparam) |
| 43 | { |
| 44 | if (!is_valid_dramc_param(dparam)) { |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 45 | printk(BIOS_WARNING, "DRAM-K: Invalid DRAM calibration data from flash\n"); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 46 | dump_param_header((void *)dparam); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | const u32 checksum = compute_checksum(dparam); |
| 51 | if (dparam->header.checksum != checksum) { |
| 52 | printk(BIOS_ERR, |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 53 | "DRAM-K: Invalid DRAM calibration checksum from flash " |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 54 | "(expected: %#x, saved: %#x)\n", |
| 55 | checksum, dparam->header.checksum); |
| 56 | return DRAMC_ERR_INVALID_CHECKSUM; |
| 57 | } |
| 58 | |
| 59 | const u16 config = CONFIG(MT8192_DRAM_DVFS) ? DRAMC_ENABLE_DVFS : DRAMC_DISABLE_DVFS; |
| 60 | if (dparam->dramc_datas.ddr_info.config_dvfs != config) { |
| 61 | printk(BIOS_WARNING, |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 62 | "DRAM-K: Incompatible config for calibration data from flash " |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 63 | "(expected: %#x, saved: %#x)\n", |
| 64 | config, dparam->dramc_datas.ddr_info.config_dvfs); |
| 65 | return -1; |
| 66 | } |
| 67 | |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 68 | printk(BIOS_INFO, "DRAM-K: DRAM calibration data valid pass\n"); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 69 | mt_set_emi(&dparam->dramc_datas); |
| 70 | if (mt_mem_test(&dparam->dramc_datas) == 0) |
| 71 | return 0; |
| 72 | |
| 73 | return DRAMC_ERR_FAST_CALIBRATION; |
| 74 | } |
| 75 | |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 76 | static int dram_run_full_calibration(struct dramc_param *dparam) |
| 77 | { |
| 78 | /* Load and run the provided blob for full-calibration if available */ |
| 79 | struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram"); |
| 80 | |
| 81 | initialize_dramc_param(dparam); |
| 82 | |
| 83 | if (prog_locate(&dram)) { |
| 84 | printk(BIOS_ERR, "DRAM-K: Locate program failed\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | if (cbfs_prog_stage_load(&dram)) { |
| 89 | printk(BIOS_ERR, "DRAM-K: CBFS load program failed\n"); |
| 90 | return -2; |
| 91 | } |
| 92 | |
| 93 | dparam->do_putc = do_putchar; |
| 94 | |
| 95 | prog_set_entry(&dram, prog_entry(&dram), dparam); |
| 96 | prog_run(&dram); |
| 97 | if (dparam->header.status != DRAMC_SUCCESS) { |
| 98 | printk(BIOS_ERR, "DRAM-K: Full calibration failed: status = %d\n", |
| 99 | dparam->header.status); |
| 100 | return -3; |
| 101 | } |
| 102 | |
| 103 | if (!(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) { |
| 104 | printk(BIOS_ERR, |
| 105 | "DRAM-K: Full calibration executed without saving parameters. " |
| 106 | "Please ensure the blob is built properly.\n"); |
| 107 | return -4; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 113 | static void mem_init_set_default_config(struct dramc_param *dparam, |
| 114 | u32 ddr_geometry) |
| 115 | { |
| 116 | memset(dparam, 0, sizeof(*dparam)); |
| 117 | |
| 118 | if (CONFIG(MT8192_DRAM_EMCP)) |
| 119 | dparam->dramc_datas.ddr_info.ddr_type = DDR_TYPE_EMCP; |
| 120 | |
| 121 | if (CONFIG(MT8192_DRAM_DVFS)) |
| 122 | dparam->dramc_datas.ddr_info.config_dvfs = DRAMC_ENABLE_DVFS; |
| 123 | dparam->dramc_datas.ddr_info.ddr_geometry = ddr_geometry; |
| 124 | |
| 125 | printk(BIOS_INFO, "DRAM-K: ddr_type: %d, config_dvfs: %d, ddr_geometry: %d\n", |
| 126 | dparam->dramc_datas.ddr_info.ddr_type, |
| 127 | dparam->dramc_datas.ddr_info.config_dvfs, |
| 128 | dparam->dramc_datas.ddr_info.ddr_geometry); |
| 129 | } |
| 130 | |
| 131 | static void mt_mem_init_run(struct dramc_param_ops *dparam_ops, u32 ddr_geometry) |
| 132 | { |
| 133 | struct dramc_param *dparam = dparam_ops->param; |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 134 | struct stopwatch sw; |
| 135 | int ret; |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 136 | |
| 137 | /* Load calibration params from flash and run fast calibration */ |
| 138 | mem_init_set_default_config(dparam, ddr_geometry); |
| 139 | if (dparam_ops->read_from_flash(dparam)) { |
| 140 | printk(BIOS_INFO, "DRAM-K: Running fast calibration\n"); |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 141 | stopwatch_init(&sw); |
| 142 | |
| 143 | ret = dram_run_fast_calibration(dparam); |
| 144 | if (ret != 0) { |
| 145 | printk(BIOS_ERR, "DRAM-K: Failed to run fast calibration " |
| 146 | "in %ld msecs, error: %d\n", |
| 147 | stopwatch_duration_msecs(&sw), ret); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 148 | |
| 149 | /* Erase flash data after fast calibration failed */ |
| 150 | memset(dparam, 0xa5, sizeof(*dparam)); |
| 151 | dparam_ops->write_to_flash(dparam); |
| 152 | } else { |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 153 | printk(BIOS_INFO, "DRAM-K: Fast calibration passed in %ld msecs\n", |
| 154 | stopwatch_duration_msecs(&sw)); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | } else { |
Huayang Duan | 68e597d | 2020-06-22 19:59:40 +0800 | [diff] [blame^] | 158 | printk(BIOS_WARNING, "DRAM-K: Failed to read calibration data from flash\n"); |
| 159 | } |
| 160 | |
| 161 | /* Run full calibration */ |
| 162 | printk(BIOS_INFO, "DRAM-K: Running full calibration\n"); |
| 163 | mem_init_set_default_config(dparam, ddr_geometry); |
| 164 | |
| 165 | stopwatch_init(&sw); |
| 166 | int err = dram_run_full_calibration(dparam); |
| 167 | if (err == 0) { |
| 168 | printk(BIOS_INFO, "DRAM-K: Full calibration passed in %ld msecs\n", |
| 169 | stopwatch_duration_msecs(&sw)); |
| 170 | |
| 171 | dparam->header.checksum = compute_checksum(dparam); |
| 172 | dparam_ops->write_to_flash(dparam); |
| 173 | printk(BIOS_DEBUG, "DRAM-K: Calibration params saved to flash: " |
| 174 | "version=%#x, size=%#x\n", |
| 175 | dparam->header.version, dparam->header.size); |
| 176 | } else { |
| 177 | printk(BIOS_ERR, "DRAM-K: Full calibration failed in %ld msecs\n", |
| 178 | stopwatch_duration_msecs(&sw)); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | void mt_mem_init(struct dramc_param_ops *dparam_ops) |
| 183 | { |
| 184 | const struct sdram_info *sdram_param = get_sdram_config(); |
| 185 | |
| 186 | mt_mem_init_run(dparam_ops, sdram_param->ddr_geometry); |
| 187 | } |