blob: 549dede00d13581ba649a547aa326f1e038fe984 [file] [log] [blame]
Huayang Duanc90a9e62020-06-22 19:52:45 +08001/* 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 Duan68e597d2020-06-22 19:59:40 +080010#include <timer.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080011
12static 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 Chen3827f562020-10-20 17:55:14 +080019 int result = complex_mem_test(addr, 0x2000);
Huayang Duanc90a9e62020-06-22 19:52:45 +080020
Xi Chen3827f562020-10-20 17:55:14 +080021 if (result != 0) {
22 printk(BIOS_ERR,
23 "[MEM] complex R/W mem test failed: %d\n", result);
Huayang Duanc90a9e62020-06-22 19:52:45 +080024 return -1;
Xi Chen3827f562020-10-20 17:55:14 +080025 } else {
26 printk(BIOS_DEBUG, "[MEM] complex R/W mem test passed\n");
Huayang Duanc90a9e62020-06-22 19:52:45 +080027 }
28
29 addr += ddr_info->rank_size[rank];
30 }
31 }
32
33 return 0;
34}
35
36static 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
42static int dram_run_fast_calibration(const struct dramc_param *dparam)
43{
44 if (!is_valid_dramc_param(dparam)) {
Huayang Duan68e597d2020-06-22 19:59:40 +080045 printk(BIOS_WARNING, "DRAM-K: Invalid DRAM calibration data from flash\n");
Huayang Duanc90a9e62020-06-22 19:52:45 +080046 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 Duan68e597d2020-06-22 19:59:40 +080053 "DRAM-K: Invalid DRAM calibration checksum from flash "
Huayang Duanc90a9e62020-06-22 19:52:45 +080054 "(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 Duan68e597d2020-06-22 19:59:40 +080062 "DRAM-K: Incompatible config for calibration data from flash "
Huayang Duanc90a9e62020-06-22 19:52:45 +080063 "(expected: %#x, saved: %#x)\n",
64 config, dparam->dramc_datas.ddr_info.config_dvfs);
65 return -1;
66 }
67
Huayang Duan68e597d2020-06-22 19:59:40 +080068 printk(BIOS_INFO, "DRAM-K: DRAM calibration data valid pass\n");
Huayang Duanc90a9e62020-06-22 19:52:45 +080069 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 Duan68e597d2020-06-22 19:59:40 +080076static 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 Duanc90a9e62020-06-22 19:52:45 +0800113static 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
131static void mt_mem_init_run(struct dramc_param_ops *dparam_ops, u32 ddr_geometry)
132{
133 struct dramc_param *dparam = dparam_ops->param;
Huayang Duan68e597d2020-06-22 19:59:40 +0800134 struct stopwatch sw;
135 int ret;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800136
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 Duan68e597d2020-06-22 19:59:40 +0800141 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 Duanc90a9e62020-06-22 19:52:45 +0800148
149 /* Erase flash data after fast calibration failed */
150 memset(dparam, 0xa5, sizeof(*dparam));
151 dparam_ops->write_to_flash(dparam);
152 } else {
Huayang Duan68e597d2020-06-22 19:59:40 +0800153 printk(BIOS_INFO, "DRAM-K: Fast calibration passed in %ld msecs\n",
154 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800155 return;
156 }
157 } else {
Huayang Duan68e597d2020-06-22 19:59:40 +0800158 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 Duanc90a9e62020-06-22 19:52:45 +0800179 }
180}
181
182void 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}