blob: e9e0294f87d356f1faae9ddd6c4bbeda86598cda [file] [log] [blame]
Peng Fan692f9432018-11-20 10:19:57 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 NXP
4 */
5
6#include <common.h>
Peng Fan9eba0822022-07-26 16:41:21 +08007#include <binman_sym.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Peng Fan692f9432018-11-20 10:19:57 +00009#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Peng Fan692f9432018-11-20 10:19:57 +000011#include <asm/io.h>
12#include <errno.h>
13#include <asm/io.h>
14#include <asm/arch/ddr.h>
15#include <asm/arch/ddr.h>
Peng Fan692f9432018-11-20 10:19:57 +000016#include <asm/sections.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20#define IMEM_LEN 32768 /* byte */
21#define DMEM_LEN 16384 /* byte */
22#define IMEM_2D_OFFSET 49152
23
24#define IMEM_OFFSET_ADDR 0x00050000
25#define DMEM_OFFSET_ADDR 0x00054000
26#define DDR_TRAIN_CODE_BASE_ADDR IP2APB_DDRPHY_IPS_BASE_ADDR(0)
27
Peng Fan9eba0822022-07-26 16:41:21 +080028binman_sym_declare(ulong, ddr_1d_imem_fw, image_pos);
29binman_sym_declare(ulong, ddr_1d_imem_fw, size);
30
31binman_sym_declare(ulong, ddr_1d_dmem_fw, image_pos);
32binman_sym_declare(ulong, ddr_1d_dmem_fw, size);
33
34#if !IS_ENABLED(CONFIG_IMX8M_DDR3L)
35binman_sym_declare(ulong, ddr_2d_imem_fw, image_pos);
36binman_sym_declare(ulong, ddr_2d_imem_fw, size);
37
38binman_sym_declare(ulong, ddr_2d_dmem_fw, image_pos);
39binman_sym_declare(ulong, ddr_2d_dmem_fw, size);
40#endif
41
Peng Fan692f9432018-11-20 10:19:57 +000042/* We need PHY iMEM PHY is 32KB padded */
43void ddr_load_train_firmware(enum fw_type type)
44{
45 u32 tmp32, i;
46 u32 error = 0;
47 unsigned long pr_to32, pr_from32;
Peng Fan9eba0822022-07-26 16:41:21 +080048 uint32_t fw_offset = type ? IMEM_2D_OFFSET : 0;
Peng Fan692f9432018-11-20 10:19:57 +000049 unsigned long imem_start = (unsigned long)&_end + fw_offset;
Peng Fan4f992e52019-08-27 06:24:47 +000050 unsigned long dmem_start;
Peng Fan9eba0822022-07-26 16:41:21 +080051 unsigned long imem_len = IMEM_LEN, dmem_len = DMEM_LEN;
Peng Fan4f992e52019-08-27 06:24:47 +000052
53#ifdef CONFIG_SPL_OF_CONTROL
54 if (gd->fdt_blob && !fdt_check_header(gd->fdt_blob)) {
55 imem_start = roundup((unsigned long)&_end +
56 fdt_totalsize(gd->fdt_blob), 4) +
57 fw_offset;
58 }
59#endif
60
Peng Fan9eba0822022-07-26 16:41:21 +080061 dmem_start = imem_start + imem_len;
62
63 if (BINMAN_SYMS_OK) {
64 switch (type) {
65 case FW_1D_IMAGE:
66 imem_start = binman_sym(ulong, ddr_1d_imem_fw, image_pos);
67 imem_len = binman_sym(ulong, ddr_1d_imem_fw, size);
68 dmem_start = binman_sym(ulong, ddr_1d_dmem_fw, image_pos);
69 dmem_len = binman_sym(ulong, ddr_1d_dmem_fw, size);
70 break;
71 case FW_2D_IMAGE:
72#if !IS_ENABLED(CONFIG_IMX8M_DDR3L)
73 imem_start = binman_sym(ulong, ddr_2d_imem_fw, image_pos);
74 imem_len = binman_sym(ulong, ddr_2d_imem_fw, size);
75 dmem_start = binman_sym(ulong, ddr_2d_dmem_fw, image_pos);
76 dmem_len = binman_sym(ulong, ddr_2d_dmem_fw, size);
77#endif
78 break;
79 }
80 }
Peng Fan692f9432018-11-20 10:19:57 +000081
82 pr_from32 = imem_start;
Ye Lib2cfc422022-07-26 16:41:07 +080083 pr_to32 = IMEM_OFFSET_ADDR;
Peng Fan9eba0822022-07-26 16:41:21 +080084 for (i = 0x0; i < imem_len; ) {
Peng Fan692f9432018-11-20 10:19:57 +000085 tmp32 = readl(pr_from32);
Ye Lib2cfc422022-07-26 16:41:07 +080086 writew(tmp32 & 0x0000ffff, DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
87 pr_to32 += 1;
88 writew((tmp32 >> 16) & 0x0000ffff,
89 DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
90 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +000091 pr_from32 += 4;
92 i += 4;
93 }
94
95 pr_from32 = dmem_start;
Ye Lib2cfc422022-07-26 16:41:07 +080096 pr_to32 = DMEM_OFFSET_ADDR;
Peng Fan9eba0822022-07-26 16:41:21 +080097 for (i = 0x0; i < dmem_len; ) {
Peng Fan692f9432018-11-20 10:19:57 +000098 tmp32 = readl(pr_from32);
Ye Lib2cfc422022-07-26 16:41:07 +080099 writew(tmp32 & 0x0000ffff, DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
100 pr_to32 += 1;
101 writew((tmp32 >> 16) & 0x0000ffff,
102 DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
103 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +0000104 pr_from32 += 4;
105 i += 4;
106 }
107
Jacky Baid62ddc12019-08-08 09:59:08 +0000108 debug("check ddr_pmu_train_imem code\n");
Peng Fan692f9432018-11-20 10:19:57 +0000109 pr_from32 = imem_start;
Ye Lib2cfc422022-07-26 16:41:07 +0800110 pr_to32 = IMEM_OFFSET_ADDR;
Peng Fan9eba0822022-07-26 16:41:21 +0800111 for (i = 0x0; i < imem_len; ) {
Ye Lib2cfc422022-07-26 16:41:07 +0800112 tmp32 = (readw(DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32)) & 0x0000ffff);
113 pr_to32 += 1;
114 tmp32 += ((readw(DDR_TRAIN_CODE_BASE_ADDR +
115 ddrphy_addr_remap(pr_to32)) & 0x0000ffff) << 16);
Peng Fan692f9432018-11-20 10:19:57 +0000116
117 if (tmp32 != readl(pr_from32)) {
118 debug("%lx %lx\n", pr_from32, pr_to32);
119 error++;
120 }
121 pr_from32 += 4;
Ye Lib2cfc422022-07-26 16:41:07 +0800122 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +0000123 i += 4;
124 }
125 if (error)
Jacky Baid62ddc12019-08-08 09:59:08 +0000126 printf("check ddr_pmu_train_imem code fail=%d\n", error);
Peng Fan692f9432018-11-20 10:19:57 +0000127 else
Jacky Baid62ddc12019-08-08 09:59:08 +0000128 debug("check ddr_pmu_train_imem code pass\n");
Peng Fan692f9432018-11-20 10:19:57 +0000129
130 debug("check ddr4_pmu_train_dmem code\n");
131 pr_from32 = dmem_start;
Ye Lib2cfc422022-07-26 16:41:07 +0800132 pr_to32 = DMEM_OFFSET_ADDR;
Peng Fan9eba0822022-07-26 16:41:21 +0800133 for (i = 0x0; i < dmem_len;) {
Ye Lib2cfc422022-07-26 16:41:07 +0800134 tmp32 = (readw(DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32)) & 0x0000ffff);
135 pr_to32 += 1;
136 tmp32 += ((readw(DDR_TRAIN_CODE_BASE_ADDR +
137 ddrphy_addr_remap(pr_to32)) & 0x0000ffff) << 16);
Peng Fan692f9432018-11-20 10:19:57 +0000138 if (tmp32 != readl(pr_from32)) {
139 debug("%lx %lx\n", pr_from32, pr_to32);
140 error++;
141 }
142 pr_from32 += 4;
Ye Lib2cfc422022-07-26 16:41:07 +0800143 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +0000144 i += 4;
145 }
146
147 if (error)
Jacky Baid62ddc12019-08-08 09:59:08 +0000148 printf("check ddr_pmu_train_dmem code fail=%d", error);
Peng Fan692f9432018-11-20 10:19:57 +0000149 else
Jacky Baid62ddc12019-08-08 09:59:08 +0000150 debug("check ddr_pmu_train_dmem code pass\n");
Peng Fan692f9432018-11-20 10:19:57 +0000151}
152
153void ddrphy_trained_csr_save(struct dram_cfg_param *ddrphy_csr,
154 unsigned int num)
155{
156 int i = 0;
157
158 /* enable the ddrphy apb */
159 dwc_ddrphy_apb_wr(0xd0000, 0x0);
160 dwc_ddrphy_apb_wr(0xc0080, 0x3);
161 for (i = 0; i < num; i++) {
162 ddrphy_csr->val = dwc_ddrphy_apb_rd(ddrphy_csr->reg);
163 ddrphy_csr++;
164 }
165 /* disable the ddrphy apb */
166 dwc_ddrphy_apb_wr(0xc0080, 0x2);
167 dwc_ddrphy_apb_wr(0xd0000, 0x1);
168}
169
170void dram_config_save(struct dram_timing_info *timing_info,
171 unsigned long saved_timing_base)
172{
173 int i = 0;
174 struct dram_timing_info *saved_timing = (struct dram_timing_info *)saved_timing_base;
175 struct dram_cfg_param *cfg;
176
177 saved_timing->ddrc_cfg_num = timing_info->ddrc_cfg_num;
178 saved_timing->ddrphy_cfg_num = timing_info->ddrphy_cfg_num;
179 saved_timing->ddrphy_trained_csr_num = ddrphy_trained_csr_num;
180 saved_timing->ddrphy_pie_num = timing_info->ddrphy_pie_num;
181
182 /* save the fsp table */
183 for (i = 0; i < 4; i++)
184 saved_timing->fsp_table[i] = timing_info->fsp_table[i];
185
186 cfg = (struct dram_cfg_param *)(saved_timing_base +
187 sizeof(*timing_info));
188
189 /* save ddrc config */
190 saved_timing->ddrc_cfg = cfg;
191 for (i = 0; i < timing_info->ddrc_cfg_num; i++) {
192 cfg->reg = timing_info->ddrc_cfg[i].reg;
193 cfg->val = timing_info->ddrc_cfg[i].val;
194 cfg++;
195 }
196
197 /* save ddrphy config */
198 saved_timing->ddrphy_cfg = cfg;
199 for (i = 0; i < timing_info->ddrphy_cfg_num; i++) {
200 cfg->reg = timing_info->ddrphy_cfg[i].reg;
201 cfg->val = timing_info->ddrphy_cfg[i].val;
202 cfg++;
203 }
204
205 /* save the ddrphy csr */
206 saved_timing->ddrphy_trained_csr = cfg;
207 for (i = 0; i < ddrphy_trained_csr_num; i++) {
208 cfg->reg = ddrphy_trained_csr[i].reg;
209 cfg->val = ddrphy_trained_csr[i].val;
210 cfg++;
211 }
212
213 /* save the ddrphy pie */
214 saved_timing->ddrphy_pie = cfg;
215 for (i = 0; i < timing_info->ddrphy_pie_num; i++) {
216 cfg->reg = timing_info->ddrphy_pie[i].reg;
217 cfg->val = timing_info->ddrphy_pie[i].val;
218 cfg++;
219 }
220}