Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2018 MediaTek 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 | |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 16 | #include <assert.h> |
Joel Kitching | 9a29228 | 2020-03-06 13:44:50 +0800 | [diff] [blame^] | 17 | #include <bootmode.h> |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 18 | #include <cbfs.h> |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 19 | #include <console/console.h> |
Yu-Ping Wu | e67dce0 | 2019-10-14 16:56:50 +0800 | [diff] [blame] | 20 | #include <ip_checksum.h> |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 21 | #include <security/vboot/vboot_common.h> |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 22 | #include <soc/dramc_param.h> |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 23 | #include <soc/dramc_pi_api.h> |
Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 24 | #include <soc/emi.h> |
Huayang Duan | e6ac20b | 2019-12-24 16:35:13 +0800 | [diff] [blame] | 25 | #include <soc/mt6358.h> |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 26 | #include <symbols.h> |
Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 27 | |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame] | 28 | static int mt_mem_test(void) |
Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 29 | { |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 30 | u64 rank_size[RANK_MAX]; |
| 31 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 32 | if (CONFIG(MEMORY_TEST)) { |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 33 | size_t r; |
| 34 | u8 *addr = _dram; |
| 35 | |
| 36 | dramc_get_rank_size(rank_size); |
| 37 | |
| 38 | for (r = RANK_0; r < RANK_MAX; r++) { |
| 39 | int i; |
| 40 | |
| 41 | if (rank_size[r] == 0) |
| 42 | break; |
| 43 | |
| 44 | i = complex_mem_test(addr, 0x2000); |
| 45 | |
| 46 | printk(BIOS_DEBUG, "[MEM] complex R/W mem test %s : %d\n", |
| 47 | (i == 0) ? "pass" : "fail", i); |
| 48 | |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame] | 49 | if (i != 0) { |
Yu-Ping Wu | 31ec0c4 | 2019-10-09 16:11:47 +0800 | [diff] [blame] | 50 | printk(BIOS_ERR, "DRAM memory test failed\n"); |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame] | 51 | return -1; |
| 52 | } |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 53 | |
| 54 | addr += rank_size[r]; |
| 55 | } |
| 56 | } |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame] | 57 | |
| 58 | return 0; |
Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 59 | } |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 60 | |
| 61 | static void dump_param_header(const struct dramc_param *dparam) |
| 62 | { |
| 63 | const struct dramc_param_header *header = &dparam->header; |
| 64 | |
| 65 | printk(BIOS_DEBUG, "header.status = %#x\n", header->status); |
| 66 | printk(BIOS_DEBUG, "header.magic = %#x (expected: %#x)\n", |
| 67 | header->magic, DRAMC_PARAM_HEADER_MAGIC); |
| 68 | printk(BIOS_DEBUG, "header.version = %#x (expected: %#x)\n", |
| 69 | header->version, DRAMC_PARAM_HEADER_VERSION); |
| 70 | printk(BIOS_DEBUG, "header.size = %#x (expected: %#lx)\n", |
| 71 | header->size, sizeof(*dparam)); |
| 72 | printk(BIOS_DEBUG, "header.config = %#x\n", header->config); |
| 73 | printk(BIOS_DEBUG, "header.flags = %#x\n", header->flags); |
| 74 | printk(BIOS_DEBUG, "header.checksum = %#x\n", header->checksum); |
| 75 | } |
| 76 | |
Yu-Ping Wu | e67dce0 | 2019-10-14 16:56:50 +0800 | [diff] [blame] | 77 | static u32 compute_checksum(const struct dramc_param *dparam) |
| 78 | { |
| 79 | return (u32)compute_ip_checksum(dparam->freq_params, |
| 80 | sizeof(dparam->freq_params)); |
| 81 | } |
| 82 | |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 83 | static int dram_run_fast_calibration(const struct dramc_param *dparam, |
| 84 | u16 config) |
| 85 | { |
| 86 | if (!is_valid_dramc_param(dparam)) { |
| 87 | printk(BIOS_WARNING, |
| 88 | "Invalid DRAM calibration data from flash\n"); |
| 89 | dump_param_header(dparam); |
| 90 | return -1; |
| 91 | } |
| 92 | |
Yu-Ping Wu | 31ec0c4 | 2019-10-09 16:11:47 +0800 | [diff] [blame] | 93 | if (dparam->header.config != config) { |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 94 | printk(BIOS_WARNING, |
| 95 | "Incompatible config for calibration data from flash " |
| 96 | "(expected: %#x, saved: %#x)\n", |
| 97 | config, dparam->header.config); |
| 98 | return -1; |
| 99 | } |
| 100 | |
Yu-Ping Wu | e67dce0 | 2019-10-14 16:56:50 +0800 | [diff] [blame] | 101 | const u32 checksum = compute_checksum(dparam); |
| 102 | if (dparam->header.checksum != checksum) { |
| 103 | printk(BIOS_ERR, |
| 104 | "Invalid DRAM calibration checksum from flash " |
| 105 | "(expected: %#x, saved: %#x)\n", |
| 106 | checksum, dparam->header.checksum); |
| 107 | return -1; |
| 108 | } |
| 109 | |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int dram_run_full_calibration(struct dramc_param *dparam, u16 config) |
| 114 | { |
| 115 | initialize_dramc_param(dparam, config); |
| 116 | |
| 117 | /* Load and run the provided blob for full-calibration if available */ |
| 118 | struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram"); |
| 119 | |
| 120 | if (prog_locate(&dram)) |
| 121 | return -1; |
| 122 | |
| 123 | if (cbfs_prog_stage_load(&dram)) |
| 124 | return -2; |
| 125 | |
Hung-Te Lin | beeab4e | 2019-10-15 17:49:24 +0800 | [diff] [blame] | 126 | dparam->do_putc = do_putchar; |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 127 | prog_set_entry(&dram, prog_entry(&dram), dparam); |
| 128 | prog_run(&dram); |
| 129 | |
| 130 | if (dparam->header.status != DRAMC_SUCCESS) { |
| 131 | printk(BIOS_ERR, "Full calibration failed: status = %d\n", |
| 132 | dparam->header.status); |
| 133 | return -3; |
| 134 | } |
| 135 | |
| 136 | if (!(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) { |
| 137 | printk(BIOS_ERR, |
| 138 | "Full calibration executed without saving parameters. " |
| 139 | "Please ensure the blob is built properly.\n"); |
| 140 | return -4; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static void set_source_to_flash(struct sdram_params *freq_params) |
| 147 | { |
| 148 | for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX; |
| 149 | shuffle++) |
| 150 | freq_params[shuffle].source = DRAMC_PARAM_SOURCE_FLASH; |
| 151 | } |
| 152 | |
| 153 | static void init_sdram_params(struct sdram_params *dst, |
| 154 | const struct sdram_params *src) |
| 155 | { |
| 156 | for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX; |
| 157 | shuffle++) |
| 158 | memcpy(&dst[shuffle], src, sizeof(*dst)); |
| 159 | } |
| 160 | |
Huayang Duan | e6ac20b | 2019-12-24 16:35:13 +0800 | [diff] [blame] | 161 | static void mt_mem_init_run(struct dramc_param_ops *dparam_ops) |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 162 | { |
| 163 | struct dramc_param *dparam = dparam_ops->param; |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 164 | |
| 165 | u16 config = 0; |
| 166 | if (CONFIG(MT8183_DRAM_EMCP)) |
| 167 | config |= DRAMC_CONFIG_EMCP; |
| 168 | |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 169 | const bool recovery_mode = vboot_recovery_mode_enabled(); |
| 170 | |
Yu-Ping Wu | 02d9071 | 2019-10-29 16:20:35 +0800 | [diff] [blame] | 171 | /* DRAM DVFS is disabled in recovery mode */ |
| 172 | if (CONFIG(MT8183_DRAM_DVFS) && !recovery_mode) |
| 173 | config |= DRAMC_CONFIG_DVFS; |
| 174 | |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 175 | /* Load calibration params from flash and run fast calibration */ |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 176 | if (recovery_mode) { |
| 177 | printk(BIOS_WARNING, "Skip loading cached calibration data\n"); |
Joel Kitching | 9a29228 | 2020-03-06 13:44:50 +0800 | [diff] [blame^] | 178 | if (get_recovery_mode_retrain_switch() || |
Hung-Te Lin | 285975d | 2019-10-23 14:20:53 +0800 | [diff] [blame] | 179 | vboot_check_recovery_request() == VB2_RECOVERY_TRAIN_AND_REBOOT) { |
Yu-Ping Wu | 46009ea | 2019-10-17 13:38:32 +0800 | [diff] [blame] | 180 | printk(BIOS_WARNING, "Retrain memory in next boot\n"); |
| 181 | /* Use 0xFF as erased flash data. */ |
| 182 | memset(dparam, 0xff, sizeof(*dparam)); |
| 183 | dparam_ops->write_to_flash(dparam); |
| 184 | } |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 185 | } else if (dparam_ops->read_from_flash(dparam)) { |
| 186 | printk(BIOS_INFO, "DRAM-K: Fast Calibration\n"); |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 187 | if (dram_run_fast_calibration(dparam, config) == 0) { |
| 188 | printk(BIOS_INFO, |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 189 | "Calibration params loaded from flash\n"); |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame] | 190 | if (mt_set_emi(dparam) == 0 && mt_mem_test() == 0) |
| 191 | return; |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 192 | } else { |
| 193 | printk(BIOS_ERR, |
| 194 | "Failed to apply cached calibration data\n"); |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 195 | } |
| 196 | } else { |
| 197 | printk(BIOS_WARNING, |
| 198 | "Failed to read calibration data from flash\n"); |
| 199 | } |
| 200 | |
| 201 | /* Run full calibration */ |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 202 | printk(BIOS_INFO, "DRAM-K: Full Calibration\n"); |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 203 | int err = dram_run_full_calibration(dparam, config); |
| 204 | if (err == 0) { |
| 205 | printk(BIOS_INFO, "Successfully loaded DRAM blobs and " |
| 206 | "ran DRAM calibration\n"); |
Huayang Duan | e6ac20b | 2019-12-24 16:35:13 +0800 | [diff] [blame] | 207 | |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 208 | /* |
| 209 | * In recovery mode the system boots in RO but the flash params |
| 210 | * should be calibrated for RW so we can't mix them up. |
| 211 | */ |
| 212 | if (!recovery_mode) { |
| 213 | set_source_to_flash(dparam->freq_params); |
| 214 | dparam->header.checksum = compute_checksum(dparam); |
| 215 | dparam_ops->write_to_flash(dparam); |
| 216 | printk(BIOS_DEBUG, "Calibration params saved to flash: " |
| 217 | "version=%#x, size=%#x\n", |
| 218 | dparam->header.version, dparam->header.size); |
| 219 | } |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
| 223 | printk(BIOS_ERR, "Failed to do full calibration (%d), " |
| 224 | "falling back to load default sdram param\n", err); |
| 225 | |
| 226 | /* Init params from sdram configs and run partial calibration */ |
Yu-Ping Wu | 998a3cc | 2019-10-16 18:29:50 +0800 | [diff] [blame] | 227 | printk(BIOS_INFO, "DRAM-K: Partial Calibration\n"); |
Yu-Ping Wu | 0e5b196 | 2019-10-07 16:57:24 +0800 | [diff] [blame] | 228 | init_sdram_params(dparam->freq_params, get_sdram_config()); |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame] | 229 | if (mt_set_emi(dparam) != 0) |
| 230 | die("Set emi failed with params from sdram config\n"); |
| 231 | if (mt_mem_test() != 0) |
| 232 | die("Memory test failed with params from sdram config\n"); |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 233 | } |
Huayang Duan | e6ac20b | 2019-12-24 16:35:13 +0800 | [diff] [blame] | 234 | |
| 235 | void mt_mem_init(struct dramc_param_ops *dparam_ops) |
| 236 | { |
| 237 | mt_mem_init_run(dparam_ops); |
| 238 | |
| 239 | /* After DRAM calibration, restore vcore voltage to default setting */ |
| 240 | pmic_set_vcore_vol(800000); |
| 241 | } |