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> |
| 10 | |
| 11 | static int mt_mem_test(const struct dramc_data *dparam) |
| 12 | { |
| 13 | if (CONFIG(MEMORY_TEST)) { |
| 14 | u8 *addr = _dram; |
| 15 | const struct ddr_base_info *ddr_info = &dparam->ddr_info; |
| 16 | |
| 17 | for (u8 rank = RANK_0; rank < ddr_info->support_ranks; rank++) { |
Xi Chen | 3827f56 | 2020-10-20 17:55:14 +0800 | [diff] [blame^] | 18 | int result = complex_mem_test(addr, 0x2000); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 19 | |
Xi Chen | 3827f56 | 2020-10-20 17:55:14 +0800 | [diff] [blame^] | 20 | if (result != 0) { |
| 21 | printk(BIOS_ERR, |
| 22 | "[MEM] complex R/W mem test failed: %d\n", result); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 23 | return -1; |
Xi Chen | 3827f56 | 2020-10-20 17:55:14 +0800 | [diff] [blame^] | 24 | } else { |
| 25 | printk(BIOS_DEBUG, "[MEM] complex R/W mem test passed\n"); |
Huayang Duan | c90a9e6 | 2020-06-22 19:52:45 +0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | addr += ddr_info->rank_size[rank]; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static u32 compute_checksum(const struct dramc_param *dparam) |
| 36 | { |
| 37 | return (u32)compute_ip_checksum(&dparam->dramc_datas, |
| 38 | sizeof(dparam->dramc_datas)); |
| 39 | } |
| 40 | |
| 41 | static int dram_run_fast_calibration(const struct dramc_param *dparam) |
| 42 | { |
| 43 | if (!is_valid_dramc_param(dparam)) { |
| 44 | printk(BIOS_WARNING, "Invalid DRAM calibration data from flash\n"); |
| 45 | dump_param_header((void *)dparam); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | const u32 checksum = compute_checksum(dparam); |
| 50 | if (dparam->header.checksum != checksum) { |
| 51 | printk(BIOS_ERR, |
| 52 | "Invalid DRAM calibration checksum from flash " |
| 53 | "(expected: %#x, saved: %#x)\n", |
| 54 | checksum, dparam->header.checksum); |
| 55 | return DRAMC_ERR_INVALID_CHECKSUM; |
| 56 | } |
| 57 | |
| 58 | const u16 config = CONFIG(MT8192_DRAM_DVFS) ? DRAMC_ENABLE_DVFS : DRAMC_DISABLE_DVFS; |
| 59 | if (dparam->dramc_datas.ddr_info.config_dvfs != config) { |
| 60 | printk(BIOS_WARNING, |
| 61 | "Incompatible config for calibration data from flash " |
| 62 | "(expected: %#x, saved: %#x)\n", |
| 63 | config, dparam->dramc_datas.ddr_info.config_dvfs); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | printk(BIOS_INFO, "DRAM calibration data valid pass\n"); |
| 68 | mt_set_emi(&dparam->dramc_datas); |
| 69 | if (mt_mem_test(&dparam->dramc_datas) == 0) |
| 70 | return 0; |
| 71 | |
| 72 | return DRAMC_ERR_FAST_CALIBRATION; |
| 73 | } |
| 74 | |
| 75 | static void mem_init_set_default_config(struct dramc_param *dparam, |
| 76 | u32 ddr_geometry) |
| 77 | { |
| 78 | memset(dparam, 0, sizeof(*dparam)); |
| 79 | |
| 80 | if (CONFIG(MT8192_DRAM_EMCP)) |
| 81 | dparam->dramc_datas.ddr_info.ddr_type = DDR_TYPE_EMCP; |
| 82 | |
| 83 | if (CONFIG(MT8192_DRAM_DVFS)) |
| 84 | dparam->dramc_datas.ddr_info.config_dvfs = DRAMC_ENABLE_DVFS; |
| 85 | dparam->dramc_datas.ddr_info.ddr_geometry = ddr_geometry; |
| 86 | |
| 87 | printk(BIOS_INFO, "DRAM-K: ddr_type: %d, config_dvfs: %d, ddr_geometry: %d\n", |
| 88 | dparam->dramc_datas.ddr_info.ddr_type, |
| 89 | dparam->dramc_datas.ddr_info.config_dvfs, |
| 90 | dparam->dramc_datas.ddr_info.ddr_geometry); |
| 91 | } |
| 92 | |
| 93 | static void mt_mem_init_run(struct dramc_param_ops *dparam_ops, u32 ddr_geometry) |
| 94 | { |
| 95 | struct dramc_param *dparam = dparam_ops->param; |
| 96 | |
| 97 | /* Load calibration params from flash and run fast calibration */ |
| 98 | mem_init_set_default_config(dparam, ddr_geometry); |
| 99 | if (dparam_ops->read_from_flash(dparam)) { |
| 100 | printk(BIOS_INFO, "DRAM-K: Running fast calibration\n"); |
| 101 | if (dram_run_fast_calibration(dparam) != 0) { |
| 102 | printk(BIOS_ERR, "Failed to run fast calibration\n"); |
| 103 | |
| 104 | /* Erase flash data after fast calibration failed */ |
| 105 | memset(dparam, 0xa5, sizeof(*dparam)); |
| 106 | dparam_ops->write_to_flash(dparam); |
| 107 | } else { |
| 108 | printk(BIOS_INFO, "Fast calibration passed\n"); |
| 109 | return; |
| 110 | } |
| 111 | } else { |
| 112 | printk(BIOS_WARNING, "Failed to read calibration data from flash\n"); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void mt_mem_init(struct dramc_param_ops *dparam_ops) |
| 117 | { |
| 118 | const struct sdram_info *sdram_param = get_sdram_config(); |
| 119 | |
| 120 | mt_mem_init_run(dparam_ops, sdram_param->ddr_geometry); |
| 121 | } |