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> |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 17 | #include <cbfs.h> |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 18 | #include <console/console.h> |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 19 | #include <soc/dramc_param.h> |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 20 | #include <soc/dramc_pi_api.h> |
Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 21 | #include <soc/emi.h> |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 22 | #include <symbols.h> |
Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 23 | |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame^] | 24 | static int mt_mem_test(void) |
Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 25 | { |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 26 | u64 rank_size[RANK_MAX]; |
| 27 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 28 | if (CONFIG(MEMORY_TEST)) { |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 29 | size_t r; |
| 30 | u8 *addr = _dram; |
| 31 | |
| 32 | dramc_get_rank_size(rank_size); |
| 33 | |
| 34 | for (r = RANK_0; r < RANK_MAX; r++) { |
| 35 | int i; |
| 36 | |
| 37 | if (rank_size[r] == 0) |
| 38 | break; |
| 39 | |
| 40 | i = complex_mem_test(addr, 0x2000); |
| 41 | |
| 42 | printk(BIOS_DEBUG, "[MEM] complex R/W mem test %s : %d\n", |
| 43 | (i == 0) ? "pass" : "fail", i); |
| 44 | |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame^] | 45 | if (i != 0) { |
| 46 | dramc_show("DRAM memory test failed\n"); |
| 47 | return -1; |
| 48 | } |
Huayang Duan | 4d15d2f | 2018-09-26 21:23:53 +0800 | [diff] [blame] | 49 | |
| 50 | addr += rank_size[r]; |
| 51 | } |
| 52 | } |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame^] | 53 | |
| 54 | return 0; |
Huayang Duan | c2ef102 | 2018-09-26 14:24:02 +0800 | [diff] [blame] | 55 | } |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 56 | |
| 57 | static void dump_param_header(const struct dramc_param *dparam) |
| 58 | { |
| 59 | const struct dramc_param_header *header = &dparam->header; |
| 60 | |
| 61 | printk(BIOS_DEBUG, "header.status = %#x\n", header->status); |
| 62 | printk(BIOS_DEBUG, "header.magic = %#x (expected: %#x)\n", |
| 63 | header->magic, DRAMC_PARAM_HEADER_MAGIC); |
| 64 | printk(BIOS_DEBUG, "header.version = %#x (expected: %#x)\n", |
| 65 | header->version, DRAMC_PARAM_HEADER_VERSION); |
| 66 | printk(BIOS_DEBUG, "header.size = %#x (expected: %#lx)\n", |
| 67 | header->size, sizeof(*dparam)); |
| 68 | printk(BIOS_DEBUG, "header.config = %#x\n", header->config); |
| 69 | printk(BIOS_DEBUG, "header.flags = %#x\n", header->flags); |
| 70 | printk(BIOS_DEBUG, "header.checksum = %#x\n", header->checksum); |
| 71 | } |
| 72 | |
| 73 | static int dram_run_fast_calibration(const struct dramc_param *dparam, |
| 74 | u16 config) |
| 75 | { |
| 76 | if (!is_valid_dramc_param(dparam)) { |
| 77 | printk(BIOS_WARNING, |
| 78 | "Invalid DRAM calibration data from flash\n"); |
| 79 | dump_param_header(dparam); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | if (dparam->header.config != config) { |
| 84 | printk(BIOS_WARNING, |
| 85 | "Incompatible config for calibration data from flash " |
| 86 | "(expected: %#x, saved: %#x)\n", |
| 87 | config, dparam->header.config); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int dram_run_full_calibration(struct dramc_param *dparam, u16 config) |
| 95 | { |
| 96 | initialize_dramc_param(dparam, config); |
| 97 | |
| 98 | /* Load and run the provided blob for full-calibration if available */ |
| 99 | struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram"); |
| 100 | |
| 101 | if (prog_locate(&dram)) |
| 102 | return -1; |
| 103 | |
| 104 | if (cbfs_prog_stage_load(&dram)) |
| 105 | return -2; |
| 106 | |
| 107 | prog_set_entry(&dram, prog_entry(&dram), dparam); |
| 108 | prog_run(&dram); |
| 109 | |
| 110 | if (dparam->header.status != DRAMC_SUCCESS) { |
| 111 | printk(BIOS_ERR, "Full calibration failed: status = %d\n", |
| 112 | dparam->header.status); |
| 113 | return -3; |
| 114 | } |
| 115 | |
| 116 | if (!(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) { |
| 117 | printk(BIOS_ERR, |
| 118 | "Full calibration executed without saving parameters. " |
| 119 | "Please ensure the blob is built properly.\n"); |
| 120 | return -4; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static void set_source_to_flash(struct sdram_params *freq_params) |
| 127 | { |
| 128 | for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX; |
| 129 | shuffle++) |
| 130 | freq_params[shuffle].source = DRAMC_PARAM_SOURCE_FLASH; |
| 131 | } |
| 132 | |
| 133 | static void init_sdram_params(struct sdram_params *dst, |
| 134 | const struct sdram_params *src) |
| 135 | { |
| 136 | for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX; |
| 137 | shuffle++) |
| 138 | memcpy(&dst[shuffle], src, sizeof(*dst)); |
| 139 | } |
| 140 | |
| 141 | void mt_mem_init(struct dramc_param_ops *dparam_ops) |
| 142 | { |
| 143 | struct dramc_param *dparam = dparam_ops->param; |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 144 | |
| 145 | u16 config = 0; |
| 146 | if (CONFIG(MT8183_DRAM_EMCP)) |
| 147 | config |= DRAMC_CONFIG_EMCP; |
| 148 | |
| 149 | /* Load calibration params from flash and run fast calibration */ |
| 150 | if (dparam_ops->read_from_flash(dparam)) { |
| 151 | if (dram_run_fast_calibration(dparam, config) == 0) { |
| 152 | printk(BIOS_INFO, |
| 153 | "DRAM calibraion params loaded from flash\n"); |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame^] | 154 | if (mt_set_emi(dparam) == 0 && mt_mem_test() == 0) |
| 155 | return; |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 156 | } |
| 157 | } else { |
| 158 | printk(BIOS_WARNING, |
| 159 | "Failed to read calibration data from flash\n"); |
| 160 | } |
| 161 | |
| 162 | /* Run full calibration */ |
| 163 | int err = dram_run_full_calibration(dparam, config); |
| 164 | if (err == 0) { |
| 165 | printk(BIOS_INFO, "Successfully loaded DRAM blobs and " |
| 166 | "ran DRAM calibration\n"); |
| 167 | set_source_to_flash(dparam->freq_params); |
| 168 | dparam_ops->write_to_flash(dparam); |
| 169 | printk(BIOS_DEBUG, "Calibration params saved to flash: " |
| 170 | "version=%#x, size=#%x\n", |
| 171 | dparam->header.version, dparam->header.size); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | printk(BIOS_ERR, "Failed to do full calibration (%d), " |
| 176 | "falling back to load default sdram param\n", err); |
| 177 | |
| 178 | /* Init params from sdram configs and run partial calibration */ |
Yu-Ping Wu | 0e5b196 | 2019-10-07 16:57:24 +0800 | [diff] [blame] | 179 | init_sdram_params(dparam->freq_params, get_sdram_config()); |
Yu-Ping Wu | ffb5ea3 | 2019-10-07 15:55:57 +0800 | [diff] [blame^] | 180 | if (mt_set_emi(dparam) != 0) |
| 181 | die("Set emi failed with params from sdram config\n"); |
| 182 | if (mt_mem_test() != 0) |
| 183 | die("Memory test failed with params from sdram config\n"); |
Huayang Duan | 078332e | 2019-08-27 13:36:14 +0800 | [diff] [blame] | 184 | } |