blob: c1fc800f191b45292943fa8b93fbb4d8c6ff89d5 [file] [log] [blame]
Peng Fan692f9432018-11-20 10:19:57 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 NXP
4 */
5
Peng Fan9eba0822022-07-26 16:41:21 +08006#include <binman_sym.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Peng Fan692f9432018-11-20 10:19:57 +00008#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Peng Fan692f9432018-11-20 10:19:57 +000010#include <asm/io.h>
11#include <errno.h>
12#include <asm/io.h>
13#include <asm/arch/ddr.h>
14#include <asm/arch/ddr.h>
Peng Fan692f9432018-11-20 10:19:57 +000015#include <asm/sections.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19#define IMEM_LEN 32768 /* byte */
20#define DMEM_LEN 16384 /* byte */
21#define IMEM_2D_OFFSET 49152
22
23#define IMEM_OFFSET_ADDR 0x00050000
24#define DMEM_OFFSET_ADDR 0x00054000
25#define DDR_TRAIN_CODE_BASE_ADDR IP2APB_DDRPHY_IPS_BASE_ADDR(0)
26
Peng Fan9eba0822022-07-26 16:41:21 +080027binman_sym_declare(ulong, ddr_1d_imem_fw, image_pos);
28binman_sym_declare(ulong, ddr_1d_imem_fw, size);
29
30binman_sym_declare(ulong, ddr_1d_dmem_fw, image_pos);
31binman_sym_declare(ulong, ddr_1d_dmem_fw, size);
32
33#if !IS_ENABLED(CONFIG_IMX8M_DDR3L)
34binman_sym_declare(ulong, ddr_2d_imem_fw, image_pos);
35binman_sym_declare(ulong, ddr_2d_imem_fw, size);
36
37binman_sym_declare(ulong, ddr_2d_dmem_fw, image_pos);
38binman_sym_declare(ulong, ddr_2d_dmem_fw, size);
39#endif
40
Peng Fan692f9432018-11-20 10:19:57 +000041/* We need PHY iMEM PHY is 32KB padded */
42void ddr_load_train_firmware(enum fw_type type)
43{
44 u32 tmp32, i;
45 u32 error = 0;
46 unsigned long pr_to32, pr_from32;
Peng Fan9eba0822022-07-26 16:41:21 +080047 uint32_t fw_offset = type ? IMEM_2D_OFFSET : 0;
Shiji Yangeff11fa2023-08-03 09:47:17 +080048 unsigned long imem_start = (unsigned long)_end + fw_offset;
Peng Fan4f992e52019-08-27 06:24:47 +000049 unsigned long dmem_start;
Peng Fan9eba0822022-07-26 16:41:21 +080050 unsigned long imem_len = IMEM_LEN, dmem_len = DMEM_LEN;
Shawn Guo661f03e2023-01-02 20:20:34 +080051 static enum fw_type last_type = -1;
52
53 /* If FW doesn't change, we can save the loading. */
54 if (last_type == type)
55 return;
56
57 last_type = type;
Peng Fan4f992e52019-08-27 06:24:47 +000058
59#ifdef CONFIG_SPL_OF_CONTROL
60 if (gd->fdt_blob && !fdt_check_header(gd->fdt_blob)) {
Shiji Yangeff11fa2023-08-03 09:47:17 +080061 imem_start = roundup((unsigned long)_end +
Peng Fan4f992e52019-08-27 06:24:47 +000062 fdt_totalsize(gd->fdt_blob), 4) +
63 fw_offset;
64 }
65#endif
66
Peng Fan9eba0822022-07-26 16:41:21 +080067 dmem_start = imem_start + imem_len;
68
69 if (BINMAN_SYMS_OK) {
70 switch (type) {
71 case FW_1D_IMAGE:
72 imem_start = binman_sym(ulong, ddr_1d_imem_fw, image_pos);
73 imem_len = binman_sym(ulong, ddr_1d_imem_fw, size);
74 dmem_start = binman_sym(ulong, ddr_1d_dmem_fw, image_pos);
75 dmem_len = binman_sym(ulong, ddr_1d_dmem_fw, size);
76 break;
77 case FW_2D_IMAGE:
78#if !IS_ENABLED(CONFIG_IMX8M_DDR3L)
79 imem_start = binman_sym(ulong, ddr_2d_imem_fw, image_pos);
80 imem_len = binman_sym(ulong, ddr_2d_imem_fw, size);
81 dmem_start = binman_sym(ulong, ddr_2d_dmem_fw, image_pos);
82 dmem_len = binman_sym(ulong, ddr_2d_dmem_fw, size);
83#endif
84 break;
85 }
86 }
Peng Fan692f9432018-11-20 10:19:57 +000087
88 pr_from32 = imem_start;
Ye Lib2cfc422022-07-26 16:41:07 +080089 pr_to32 = IMEM_OFFSET_ADDR;
Peng Fan9eba0822022-07-26 16:41:21 +080090 for (i = 0x0; i < imem_len; ) {
Peng Fan692f9432018-11-20 10:19:57 +000091 tmp32 = readl(pr_from32);
Ye Lib2cfc422022-07-26 16:41:07 +080092 writew(tmp32 & 0x0000ffff, DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
93 pr_to32 += 1;
94 writew((tmp32 >> 16) & 0x0000ffff,
95 DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
96 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +000097 pr_from32 += 4;
98 i += 4;
99 }
100
101 pr_from32 = dmem_start;
Ye Lib2cfc422022-07-26 16:41:07 +0800102 pr_to32 = DMEM_OFFSET_ADDR;
Peng Fan9eba0822022-07-26 16:41:21 +0800103 for (i = 0x0; i < dmem_len; ) {
Peng Fan692f9432018-11-20 10:19:57 +0000104 tmp32 = readl(pr_from32);
Ye Lib2cfc422022-07-26 16:41:07 +0800105 writew(tmp32 & 0x0000ffff, DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
106 pr_to32 += 1;
107 writew((tmp32 >> 16) & 0x0000ffff,
108 DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
109 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +0000110 pr_from32 += 4;
111 i += 4;
112 }
113
Jacky Baid62ddc12019-08-08 09:59:08 +0000114 debug("check ddr_pmu_train_imem code\n");
Peng Fan692f9432018-11-20 10:19:57 +0000115 pr_from32 = imem_start;
Ye Lib2cfc422022-07-26 16:41:07 +0800116 pr_to32 = IMEM_OFFSET_ADDR;
Peng Fan9eba0822022-07-26 16:41:21 +0800117 for (i = 0x0; i < imem_len; ) {
Ye Lib2cfc422022-07-26 16:41:07 +0800118 tmp32 = (readw(DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32)) & 0x0000ffff);
119 pr_to32 += 1;
120 tmp32 += ((readw(DDR_TRAIN_CODE_BASE_ADDR +
121 ddrphy_addr_remap(pr_to32)) & 0x0000ffff) << 16);
Peng Fan692f9432018-11-20 10:19:57 +0000122
123 if (tmp32 != readl(pr_from32)) {
124 debug("%lx %lx\n", pr_from32, pr_to32);
125 error++;
126 }
127 pr_from32 += 4;
Ye Lib2cfc422022-07-26 16:41:07 +0800128 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +0000129 i += 4;
130 }
131 if (error)
Jacky Baid62ddc12019-08-08 09:59:08 +0000132 printf("check ddr_pmu_train_imem code fail=%d\n", error);
Peng Fan692f9432018-11-20 10:19:57 +0000133 else
Jacky Baid62ddc12019-08-08 09:59:08 +0000134 debug("check ddr_pmu_train_imem code pass\n");
Peng Fan692f9432018-11-20 10:19:57 +0000135
136 debug("check ddr4_pmu_train_dmem code\n");
137 pr_from32 = dmem_start;
Ye Lib2cfc422022-07-26 16:41:07 +0800138 pr_to32 = DMEM_OFFSET_ADDR;
Peng Fan9eba0822022-07-26 16:41:21 +0800139 for (i = 0x0; i < dmem_len;) {
Ye Lib2cfc422022-07-26 16:41:07 +0800140 tmp32 = (readw(DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32)) & 0x0000ffff);
141 pr_to32 += 1;
142 tmp32 += ((readw(DDR_TRAIN_CODE_BASE_ADDR +
143 ddrphy_addr_remap(pr_to32)) & 0x0000ffff) << 16);
Peng Fan692f9432018-11-20 10:19:57 +0000144 if (tmp32 != readl(pr_from32)) {
145 debug("%lx %lx\n", pr_from32, pr_to32);
146 error++;
147 }
148 pr_from32 += 4;
Ye Lib2cfc422022-07-26 16:41:07 +0800149 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +0000150 i += 4;
151 }
152
153 if (error)
Jacky Baid62ddc12019-08-08 09:59:08 +0000154 printf("check ddr_pmu_train_dmem code fail=%d", error);
Peng Fan692f9432018-11-20 10:19:57 +0000155 else
Jacky Baid62ddc12019-08-08 09:59:08 +0000156 debug("check ddr_pmu_train_dmem code pass\n");
Peng Fan692f9432018-11-20 10:19:57 +0000157}
158
159void ddrphy_trained_csr_save(struct dram_cfg_param *ddrphy_csr,
160 unsigned int num)
161{
162 int i = 0;
163
164 /* enable the ddrphy apb */
165 dwc_ddrphy_apb_wr(0xd0000, 0x0);
166 dwc_ddrphy_apb_wr(0xc0080, 0x3);
167 for (i = 0; i < num; i++) {
168 ddrphy_csr->val = dwc_ddrphy_apb_rd(ddrphy_csr->reg);
169 ddrphy_csr++;
170 }
171 /* disable the ddrphy apb */
172 dwc_ddrphy_apb_wr(0xc0080, 0x2);
173 dwc_ddrphy_apb_wr(0xd0000, 0x1);
174}
175
Jacky Bai9ded7972023-04-28 12:08:43 +0800176void *dram_config_save(struct dram_timing_info *timing_info, unsigned long saved_timing_base)
Peng Fan692f9432018-11-20 10:19:57 +0000177{
178 int i = 0;
179 struct dram_timing_info *saved_timing = (struct dram_timing_info *)saved_timing_base;
180 struct dram_cfg_param *cfg;
181
182 saved_timing->ddrc_cfg_num = timing_info->ddrc_cfg_num;
183 saved_timing->ddrphy_cfg_num = timing_info->ddrphy_cfg_num;
184 saved_timing->ddrphy_trained_csr_num = ddrphy_trained_csr_num;
185 saved_timing->ddrphy_pie_num = timing_info->ddrphy_pie_num;
186
187 /* save the fsp table */
188 for (i = 0; i < 4; i++)
189 saved_timing->fsp_table[i] = timing_info->fsp_table[i];
190
191 cfg = (struct dram_cfg_param *)(saved_timing_base +
192 sizeof(*timing_info));
193
194 /* save ddrc config */
195 saved_timing->ddrc_cfg = cfg;
196 for (i = 0; i < timing_info->ddrc_cfg_num; i++) {
197 cfg->reg = timing_info->ddrc_cfg[i].reg;
198 cfg->val = timing_info->ddrc_cfg[i].val;
199 cfg++;
200 }
201
202 /* save ddrphy config */
203 saved_timing->ddrphy_cfg = cfg;
204 for (i = 0; i < timing_info->ddrphy_cfg_num; i++) {
205 cfg->reg = timing_info->ddrphy_cfg[i].reg;
206 cfg->val = timing_info->ddrphy_cfg[i].val;
207 cfg++;
208 }
209
210 /* save the ddrphy csr */
211 saved_timing->ddrphy_trained_csr = cfg;
212 for (i = 0; i < ddrphy_trained_csr_num; i++) {
213 cfg->reg = ddrphy_trained_csr[i].reg;
214 cfg->val = ddrphy_trained_csr[i].val;
215 cfg++;
216 }
217
218 /* save the ddrphy pie */
219 saved_timing->ddrphy_pie = cfg;
220 for (i = 0; i < timing_info->ddrphy_pie_num; i++) {
221 cfg->reg = timing_info->ddrphy_pie[i].reg;
222 cfg->val = timing_info->ddrphy_pie[i].val;
223 cfg++;
224 }
Jacky Bai9ded7972023-04-28 12:08:43 +0800225
226 return (void *)cfg;
Peng Fan692f9432018-11-20 10:19:57 +0000227}