blob: 60d650e3089f90561d290930a84c857d2f32ce28 [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>
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
27/* We need PHY iMEM PHY is 32KB padded */
28void ddr_load_train_firmware(enum fw_type type)
29{
30 u32 tmp32, i;
31 u32 error = 0;
32 unsigned long pr_to32, pr_from32;
33 unsigned long fw_offset = type ? IMEM_2D_OFFSET : 0;
34 unsigned long imem_start = (unsigned long)&_end + fw_offset;
Peng Fan4f992e52019-08-27 06:24:47 +000035 unsigned long dmem_start;
36
37#ifdef CONFIG_SPL_OF_CONTROL
38 if (gd->fdt_blob && !fdt_check_header(gd->fdt_blob)) {
39 imem_start = roundup((unsigned long)&_end +
40 fdt_totalsize(gd->fdt_blob), 4) +
41 fw_offset;
42 }
43#endif
44
45 dmem_start = imem_start + IMEM_LEN;
Peng Fan692f9432018-11-20 10:19:57 +000046
47 pr_from32 = imem_start;
Ye Lib2cfc422022-07-26 16:41:07 +080048 pr_to32 = IMEM_OFFSET_ADDR;
Peng Fan692f9432018-11-20 10:19:57 +000049 for (i = 0x0; i < IMEM_LEN; ) {
50 tmp32 = readl(pr_from32);
Ye Lib2cfc422022-07-26 16:41:07 +080051 writew(tmp32 & 0x0000ffff, DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
52 pr_to32 += 1;
53 writew((tmp32 >> 16) & 0x0000ffff,
54 DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
55 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +000056 pr_from32 += 4;
57 i += 4;
58 }
59
60 pr_from32 = dmem_start;
Ye Lib2cfc422022-07-26 16:41:07 +080061 pr_to32 = DMEM_OFFSET_ADDR;
Peng Fan692f9432018-11-20 10:19:57 +000062 for (i = 0x0; i < DMEM_LEN; ) {
63 tmp32 = readl(pr_from32);
Ye Lib2cfc422022-07-26 16:41:07 +080064 writew(tmp32 & 0x0000ffff, DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
65 pr_to32 += 1;
66 writew((tmp32 >> 16) & 0x0000ffff,
67 DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32));
68 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +000069 pr_from32 += 4;
70 i += 4;
71 }
72
Jacky Baid62ddc12019-08-08 09:59:08 +000073 debug("check ddr_pmu_train_imem code\n");
Peng Fan692f9432018-11-20 10:19:57 +000074 pr_from32 = imem_start;
Ye Lib2cfc422022-07-26 16:41:07 +080075 pr_to32 = IMEM_OFFSET_ADDR;
Peng Fan692f9432018-11-20 10:19:57 +000076 for (i = 0x0; i < IMEM_LEN; ) {
Ye Lib2cfc422022-07-26 16:41:07 +080077 tmp32 = (readw(DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32)) & 0x0000ffff);
78 pr_to32 += 1;
79 tmp32 += ((readw(DDR_TRAIN_CODE_BASE_ADDR +
80 ddrphy_addr_remap(pr_to32)) & 0x0000ffff) << 16);
Peng Fan692f9432018-11-20 10:19:57 +000081
82 if (tmp32 != readl(pr_from32)) {
83 debug("%lx %lx\n", pr_from32, pr_to32);
84 error++;
85 }
86 pr_from32 += 4;
Ye Lib2cfc422022-07-26 16:41:07 +080087 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +000088 i += 4;
89 }
90 if (error)
Jacky Baid62ddc12019-08-08 09:59:08 +000091 printf("check ddr_pmu_train_imem code fail=%d\n", error);
Peng Fan692f9432018-11-20 10:19:57 +000092 else
Jacky Baid62ddc12019-08-08 09:59:08 +000093 debug("check ddr_pmu_train_imem code pass\n");
Peng Fan692f9432018-11-20 10:19:57 +000094
95 debug("check ddr4_pmu_train_dmem code\n");
96 pr_from32 = dmem_start;
Ye Lib2cfc422022-07-26 16:41:07 +080097 pr_to32 = DMEM_OFFSET_ADDR;
Peng Fan692f9432018-11-20 10:19:57 +000098 for (i = 0x0; i < DMEM_LEN;) {
Ye Lib2cfc422022-07-26 16:41:07 +080099 tmp32 = (readw(DDR_TRAIN_CODE_BASE_ADDR + ddrphy_addr_remap(pr_to32)) & 0x0000ffff);
100 pr_to32 += 1;
101 tmp32 += ((readw(DDR_TRAIN_CODE_BASE_ADDR +
102 ddrphy_addr_remap(pr_to32)) & 0x0000ffff) << 16);
Peng Fan692f9432018-11-20 10:19:57 +0000103 if (tmp32 != readl(pr_from32)) {
104 debug("%lx %lx\n", pr_from32, pr_to32);
105 error++;
106 }
107 pr_from32 += 4;
Ye Lib2cfc422022-07-26 16:41:07 +0800108 pr_to32 += 1;
Peng Fan692f9432018-11-20 10:19:57 +0000109 i += 4;
110 }
111
112 if (error)
Jacky Baid62ddc12019-08-08 09:59:08 +0000113 printf("check ddr_pmu_train_dmem code fail=%d", error);
Peng Fan692f9432018-11-20 10:19:57 +0000114 else
Jacky Baid62ddc12019-08-08 09:59:08 +0000115 debug("check ddr_pmu_train_dmem code pass\n");
Peng Fan692f9432018-11-20 10:19:57 +0000116}
117
118void ddrphy_trained_csr_save(struct dram_cfg_param *ddrphy_csr,
119 unsigned int num)
120{
121 int i = 0;
122
123 /* enable the ddrphy apb */
124 dwc_ddrphy_apb_wr(0xd0000, 0x0);
125 dwc_ddrphy_apb_wr(0xc0080, 0x3);
126 for (i = 0; i < num; i++) {
127 ddrphy_csr->val = dwc_ddrphy_apb_rd(ddrphy_csr->reg);
128 ddrphy_csr++;
129 }
130 /* disable the ddrphy apb */
131 dwc_ddrphy_apb_wr(0xc0080, 0x2);
132 dwc_ddrphy_apb_wr(0xd0000, 0x1);
133}
134
135void dram_config_save(struct dram_timing_info *timing_info,
136 unsigned long saved_timing_base)
137{
138 int i = 0;
139 struct dram_timing_info *saved_timing = (struct dram_timing_info *)saved_timing_base;
140 struct dram_cfg_param *cfg;
141
142 saved_timing->ddrc_cfg_num = timing_info->ddrc_cfg_num;
143 saved_timing->ddrphy_cfg_num = timing_info->ddrphy_cfg_num;
144 saved_timing->ddrphy_trained_csr_num = ddrphy_trained_csr_num;
145 saved_timing->ddrphy_pie_num = timing_info->ddrphy_pie_num;
146
147 /* save the fsp table */
148 for (i = 0; i < 4; i++)
149 saved_timing->fsp_table[i] = timing_info->fsp_table[i];
150
151 cfg = (struct dram_cfg_param *)(saved_timing_base +
152 sizeof(*timing_info));
153
154 /* save ddrc config */
155 saved_timing->ddrc_cfg = cfg;
156 for (i = 0; i < timing_info->ddrc_cfg_num; i++) {
157 cfg->reg = timing_info->ddrc_cfg[i].reg;
158 cfg->val = timing_info->ddrc_cfg[i].val;
159 cfg++;
160 }
161
162 /* save ddrphy config */
163 saved_timing->ddrphy_cfg = cfg;
164 for (i = 0; i < timing_info->ddrphy_cfg_num; i++) {
165 cfg->reg = timing_info->ddrphy_cfg[i].reg;
166 cfg->val = timing_info->ddrphy_cfg[i].val;
167 cfg++;
168 }
169
170 /* save the ddrphy csr */
171 saved_timing->ddrphy_trained_csr = cfg;
172 for (i = 0; i < ddrphy_trained_csr_num; i++) {
173 cfg->reg = ddrphy_trained_csr[i].reg;
174 cfg->val = ddrphy_trained_csr[i].val;
175 cfg++;
176 }
177
178 /* save the ddrphy pie */
179 saved_timing->ddrphy_pie = cfg;
180 for (i = 0; i < timing_info->ddrphy_pie_num; i++) {
181 cfg->reg = timing_info->ddrphy_pie[i].reg;
182 cfg->val = timing_info->ddrphy_pie[i].val;
183 cfg++;
184 }
185}