Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2022 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <errno.h> |
| 8 | #include <log.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <asm/arch/ddr.h> |
| 11 | #include <asm/arch/clock.h> |
| 12 | #include <asm/arch/sys_proto.h> |
| 13 | #include <linux/delay.h> |
| 14 | |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 15 | static unsigned int g_cdd_rr_max[4]; |
| 16 | static unsigned int g_cdd_rw_max[4]; |
| 17 | static unsigned int g_cdd_wr_max[4]; |
| 18 | static unsigned int g_cdd_ww_max[4]; |
| 19 | |
| 20 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 21 | |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 22 | void ddrphy_coldreset(void) |
| 23 | { |
| 24 | /* dramphy_apb_n default 1 , assert -> 0, de_assert -> 1 */ |
| 25 | /* dramphy_reset_n default 0 , assert -> 0, de_assert -> 1 */ |
| 26 | /* dramphy_PwrOKIn default 0 , assert -> 1, de_assert -> 0 */ |
| 27 | |
| 28 | /* src_gen_dphy_apb_sw_rst_de_assert */ |
| 29 | clrbits_le32(REG_SRC_DPHY_SW_CTRL, BIT(0)); |
| 30 | /* src_gen_dphy_sw_rst_de_assert */ |
| 31 | clrbits_le32(REG_SRC_DPHY_SINGLE_RESET_SW_CTRL, BIT(2)); |
| 32 | /* src_gen_dphy_PwrOKIn_sw_rst_de_assert() */ |
| 33 | setbits_le32(REG_SRC_DPHY_SINGLE_RESET_SW_CTRL, BIT(0)); |
| 34 | mdelay(10); |
| 35 | |
| 36 | /* src_gen_dphy_apb_sw_rst_assert */ |
| 37 | setbits_le32(REG_SRC_DPHY_SW_CTRL, BIT(0)); |
| 38 | /* src_gen_dphy_sw_rst_assert */ |
| 39 | setbits_le32(REG_SRC_DPHY_SINGLE_RESET_SW_CTRL, BIT(2)); |
| 40 | mdelay(10); |
| 41 | /* src_gen_dphy_PwrOKIn_sw_rst_assert */ |
| 42 | clrbits_le32(REG_SRC_DPHY_SINGLE_RESET_SW_CTRL, BIT(0)); |
| 43 | mdelay(10); |
| 44 | |
| 45 | /* src_gen_dphy_apb_sw_rst_de_assert */ |
| 46 | clrbits_le32(REG_SRC_DPHY_SW_CTRL, BIT(0)); |
| 47 | /* src_gen_dphy_sw_rst_de_assert() */ |
| 48 | clrbits_le32(REG_SRC_DPHY_SINGLE_RESET_SW_CTRL, BIT(2)); |
| 49 | } |
| 50 | |
| 51 | void check_ddrc_idle(void) |
| 52 | { |
| 53 | u32 regval; |
| 54 | |
| 55 | do { |
| 56 | regval = readl(REG_DDRDSR_2); |
| 57 | if (regval & BIT(31)) |
| 58 | break; |
| 59 | } while (1); |
| 60 | } |
| 61 | |
| 62 | void check_dfi_init_complete(void) |
| 63 | { |
| 64 | u32 regval; |
| 65 | |
| 66 | do { |
| 67 | regval = readl(REG_DDRDSR_2); |
| 68 | if (regval & BIT(2)) |
| 69 | break; |
| 70 | } while (1); |
| 71 | setbits_le32(REG_DDRDSR_2, BIT(2)); |
| 72 | } |
| 73 | |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 74 | void ddrc_config(struct dram_timing_info *dram_timing) |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 75 | { |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 76 | u32 num = dram_timing->ddrc_cfg_num; |
| 77 | struct dram_cfg_param *ddrc_config; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 78 | int i = 0; |
| 79 | |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 80 | ddrc_config = dram_timing->ddrc_cfg; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 81 | for (i = 0; i < num; i++) { |
| 82 | writel(ddrc_config->val, (ulong)ddrc_config->reg); |
| 83 | ddrc_config++; |
| 84 | } |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 85 | |
| 86 | if (dram_timing->fsp_cfg) { |
| 87 | ddrc_config = dram_timing->fsp_cfg[0].ddrc_cfg; |
| 88 | while (ddrc_config->reg != 0) { |
| 89 | writel(ddrc_config->val, (ulong)ddrc_config->reg); |
| 90 | ddrc_config++; |
| 91 | } |
| 92 | } |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static unsigned int look_for_max(unsigned int data[], unsigned int addr_start, |
| 96 | unsigned int addr_end) |
| 97 | { |
| 98 | unsigned int i, imax = 0; |
| 99 | |
| 100 | for (i = addr_start; i <= addr_end; i++) { |
| 101 | if (((data[i] >> 7) == 0) && data[i] > imax) |
| 102 | imax = data[i]; |
| 103 | } |
| 104 | |
| 105 | return imax; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void get_trained_CDD(u32 fsp) |
| 109 | { |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 110 | unsigned int i, tmp; |
| 111 | unsigned int cdd_cha[12], cdd_chb[12]; |
| 112 | unsigned int cdd_cha_rr_max, cdd_cha_rw_max, cdd_cha_wr_max, cdd_cha_ww_max; |
| 113 | unsigned int cdd_chb_rr_max, cdd_chb_rw_max, cdd_chb_wr_max, cdd_chb_ww_max; |
| 114 | |
| 115 | for (i = 0; i < 6; i++) { |
| 116 | tmp = dwc_ddrphy_apb_rd(0x54013 + i); |
| 117 | cdd_cha[i * 2] = tmp & 0xff; |
| 118 | cdd_cha[i * 2 + 1] = (tmp >> 8) & 0xff; |
| 119 | } |
| 120 | |
| 121 | for (i = 0; i < 7; i++) { |
| 122 | tmp = dwc_ddrphy_apb_rd(0x5402c + i); |
| 123 | |
| 124 | if (i == 0) { |
| 125 | cdd_chb[0] = (tmp >> 8) & 0xff; |
| 126 | } else if (i == 6) { |
| 127 | cdd_chb[11] = tmp & 0xff; |
| 128 | } else { |
| 129 | cdd_chb[i * 2 - 1] = tmp & 0xff; |
| 130 | cdd_chb[i * 2] = (tmp >> 8) & 0xff; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | cdd_cha_rr_max = look_for_max(cdd_cha, 0, 1); |
| 135 | cdd_cha_rw_max = look_for_max(cdd_cha, 2, 5); |
| 136 | cdd_cha_wr_max = look_for_max(cdd_cha, 6, 9); |
| 137 | cdd_cha_ww_max = look_for_max(cdd_cha, 10, 11); |
| 138 | cdd_chb_rr_max = look_for_max(cdd_chb, 0, 1); |
| 139 | cdd_chb_rw_max = look_for_max(cdd_chb, 2, 5); |
| 140 | cdd_chb_wr_max = look_for_max(cdd_chb, 6, 9); |
| 141 | cdd_chb_ww_max = look_for_max(cdd_chb, 10, 11); |
| 142 | g_cdd_rr_max[fsp] = cdd_cha_rr_max > cdd_chb_rr_max ? cdd_cha_rr_max : cdd_chb_rr_max; |
| 143 | g_cdd_rw_max[fsp] = cdd_cha_rw_max > cdd_chb_rw_max ? cdd_cha_rw_max : cdd_chb_rw_max; |
| 144 | g_cdd_wr_max[fsp] = cdd_cha_wr_max > cdd_chb_wr_max ? cdd_cha_wr_max : cdd_chb_wr_max; |
| 145 | g_cdd_ww_max[fsp] = cdd_cha_ww_max > cdd_chb_ww_max ? cdd_cha_ww_max : cdd_chb_ww_max; |
| 146 | } |
| 147 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 148 | static u32 ddrc_get_fsp_reg_setting(struct dram_cfg_param *ddrc_cfg, unsigned int cfg_num, u32 reg) |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 149 | { |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 150 | unsigned int i; |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 151 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 152 | for (i = 0; i < cfg_num; i++) { |
| 153 | if (reg == ddrc_cfg[i].reg) |
| 154 | return ddrc_cfg[i].val; |
| 155 | } |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 156 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 157 | return 0; |
| 158 | } |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 159 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 160 | static void ddrc_update_fsp_reg_setting(struct dram_cfg_param *ddrc_cfg, int cfg_num, |
| 161 | u32 reg, u32 val) |
| 162 | { |
| 163 | unsigned int i; |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 164 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 165 | for (i = 0; i < cfg_num; i++) { |
| 166 | if (reg == ddrc_cfg[i].reg) { |
| 167 | ddrc_cfg[i].val = val; |
| 168 | return; |
| 169 | } |
| 170 | } |
| 171 | } |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 172 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 173 | void update_umctl2_rank_space_setting(struct dram_timing_info *dram_timing, unsigned int pstat_num) |
| 174 | { |
| 175 | u32 tmp, tmp_t; |
| 176 | u32 wwt, rrt, wrt, rwt; |
| 177 | u32 ext_wwt, ext_rrt, ext_wrt, ext_rwt; |
| 178 | u32 max_wwt, max_rrt, max_wrt, max_rwt; |
| 179 | u32 i; |
| 180 | |
| 181 | for (i = 0; i < pstat_num; i++) { |
| 182 | /* read wwt, rrt, wrt, rwt fields from timing_cfg_0 */ |
| 183 | if (!dram_timing->fsp_cfg_num) { |
| 184 | tmp = ddrc_get_fsp_reg_setting(dram_timing->ddrc_cfg, |
| 185 | dram_timing->ddrc_cfg_num, |
| 186 | REG_DDR_TIMING_CFG_0); |
| 187 | } else { |
| 188 | tmp = ddrc_get_fsp_reg_setting(dram_timing->fsp_cfg[i].ddrc_cfg, |
| 189 | ARRAY_SIZE(dram_timing->fsp_cfg[i].ddrc_cfg), |
| 190 | REG_DDR_TIMING_CFG_0); |
| 191 | } |
| 192 | wwt = (tmp >> 24) & 0x3; |
| 193 | rrt = (tmp >> 26) & 0x3; |
| 194 | wrt = (tmp >> 28) & 0x3; |
| 195 | rwt = (tmp >> 30) & 0x3; |
| 196 | |
| 197 | /* read rxt_wwt, ext_rrt, ext_wrt, ext_rwt fields from timing_cfg_4 */ |
| 198 | if (!dram_timing->fsp_cfg_num) { |
| 199 | tmp_t = ddrc_get_fsp_reg_setting(dram_timing->ddrc_cfg, |
| 200 | dram_timing->ddrc_cfg_num, |
| 201 | REG_DDR_TIMING_CFG_4); |
| 202 | } else { |
| 203 | tmp_t = ddrc_get_fsp_reg_setting(dram_timing->fsp_cfg[i].ddrc_cfg, |
| 204 | ARRAY_SIZE(dram_timing->fsp_cfg[i].ddrc_cfg), |
| 205 | REG_DDR_TIMING_CFG_4); |
| 206 | } |
| 207 | ext_wwt = (tmp_t >> 8) & 0x3; |
| 208 | ext_rrt = (tmp_t >> 10) & 0x3; |
| 209 | ext_wrt = (tmp_t >> 12) & 0x3; |
| 210 | ext_rwt = (tmp_t >> 14) & 0x3; |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 211 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 212 | wwt = (ext_wwt << 2) | wwt; |
| 213 | rrt = (ext_rrt << 2) | rrt; |
| 214 | wrt = (ext_wrt << 2) | wrt; |
| 215 | rwt = (ext_rwt << 2) | rwt; |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 216 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 217 | max_wwt = MAX(g_cdd_ww_max[0], wwt); |
| 218 | max_rrt = MAX(g_cdd_rr_max[0], rrt); |
| 219 | max_wrt = MAX(g_cdd_wr_max[0], wrt); |
| 220 | max_rwt = MAX(g_cdd_rw_max[0], rwt); |
| 221 | /* verify values to see if are bigger then 15 (4 bits) */ |
| 222 | if (max_wwt > 15) |
| 223 | max_wwt = 15; |
| 224 | if (max_rrt > 15) |
| 225 | max_rrt = 15; |
| 226 | if (max_wrt > 15) |
| 227 | max_wrt = 15; |
| 228 | if (max_rwt > 15) |
| 229 | max_rwt = 15; |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 230 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 231 | /* recalculate timings for controller registers */ |
| 232 | wwt = max_wwt & 0x3; |
| 233 | rrt = max_rrt & 0x3; |
| 234 | wrt = max_wrt & 0x3; |
| 235 | rwt = max_rwt & 0x3; |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 236 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 237 | ext_wwt = (max_wwt & 0xC) >> 2; |
| 238 | ext_rrt = (max_rrt & 0xC) >> 2; |
| 239 | ext_wrt = (max_wrt & 0xC) >> 2; |
| 240 | ext_rwt = (max_rwt & 0xC) >> 2; |
Ye Li | a5163fa | 2023-04-28 12:08:39 +0800 | [diff] [blame] | 241 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 242 | /* update timing_cfg_0 and timing_cfg_4 */ |
| 243 | tmp = (tmp & 0x00ffffff) | (rwt << 30) | (wrt << 28) | |
| 244 | (rrt << 26) | (wwt << 24); |
| 245 | tmp_t = (tmp_t & 0xFFFF00FF) | (ext_rwt << 14) | |
| 246 | (ext_wrt << 12) | (ext_rrt << 10) | (ext_wwt << 8); |
| 247 | |
| 248 | if (!dram_timing->fsp_cfg_num) { |
| 249 | ddrc_update_fsp_reg_setting(dram_timing->ddrc_cfg, |
| 250 | dram_timing->ddrc_cfg_num, |
| 251 | REG_DDR_TIMING_CFG_0, tmp); |
| 252 | ddrc_update_fsp_reg_setting(dram_timing->ddrc_cfg, |
| 253 | dram_timing->ddrc_cfg_num, |
| 254 | REG_DDR_TIMING_CFG_4, tmp_t); |
| 255 | } else { |
| 256 | ddrc_update_fsp_reg_setting(dram_timing->fsp_cfg[i].ddrc_cfg, |
| 257 | ARRAY_SIZE(dram_timing->fsp_cfg[i].ddrc_cfg), |
| 258 | REG_DDR_TIMING_CFG_0, tmp); |
| 259 | ddrc_update_fsp_reg_setting(dram_timing->fsp_cfg[i].ddrc_cfg, |
| 260 | ARRAY_SIZE(dram_timing->fsp_cfg[i].ddrc_cfg), |
| 261 | REG_DDR_TIMING_CFG_4, tmp_t); |
| 262 | } |
| 263 | } |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 266 | u32 ddrc_mrr(u32 chip_select, u32 mode_reg_num, u32 *mode_reg_val) |
| 267 | { |
| 268 | u32 temp; |
| 269 | |
| 270 | writel(0x80000000, REG_DDR_SDRAM_MD_CNTL_2); |
| 271 | temp = 0x80000000 | (chip_select << 28) | (mode_reg_num << 0); |
| 272 | writel(temp, REG_DDR_SDRAM_MD_CNTL); |
| 273 | while ((readl(REG_DDR_SDRAM_MD_CNTL) & 0x80000000) == 0x80000000) |
| 274 | ; |
| 275 | while (!(readl(REG_DDR_SDRAM_MPR5))) |
| 276 | ; |
| 277 | *mode_reg_val = (readl(REG_DDR_SDRAM_MPR4) & 0xFF0000) >> 16; |
| 278 | writel(0x0, REG_DDR_SDRAM_MPR5); |
| 279 | while ((readl(REG_DDR_SDRAM_MPR5))) |
| 280 | ; |
| 281 | writel(0x0, REG_DDR_SDRAM_MPR4); |
| 282 | writel(0x0, REG_DDR_SDRAM_MD_CNTL_2); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | void ddrc_mrs(u32 cs_sel, u32 opcode, u32 mr) |
| 288 | { |
| 289 | u32 regval; |
| 290 | |
| 291 | regval = (cs_sel << 28) | (opcode << 6) | (mr); |
| 292 | writel(regval, REG_DDR_SDRAM_MD_CNTL); |
| 293 | setbits_le32(REG_DDR_SDRAM_MD_CNTL, BIT(31)); |
| 294 | check_ddrc_idle(); |
| 295 | } |
| 296 | |
| 297 | u32 lpddr4_mr_read(u32 mr_rank, u32 mr_addr) |
| 298 | { |
| 299 | u32 chip_select, regval; |
| 300 | |
| 301 | if (mr_rank == 1) |
| 302 | chip_select = 0; /* CS0 */ |
| 303 | else if (mr_rank == 2) |
| 304 | chip_select = 1; /* CS1 */ |
| 305 | else |
| 306 | chip_select = 4; /* CS0 & CS1 */ |
| 307 | |
| 308 | ddrc_mrr(chip_select, mr_addr, ®val); |
| 309 | |
| 310 | return regval; |
| 311 | } |
| 312 | |
| 313 | void update_mr_fsp_op0(struct dram_cfg_param *cfg, unsigned int num) |
| 314 | { |
| 315 | int i; |
| 316 | |
| 317 | ddrc_mrs(0x4, 0x88, 13); /* FSP-OP->1, FSP-WR->0, VRCG=1, DMD=0 */ |
| 318 | for (i = 0; i < num; i++) { |
| 319 | if (cfg[i].reg) |
| 320 | ddrc_mrs(0x4, cfg[i].val, cfg[i].reg); |
| 321 | } |
| 322 | ddrc_mrs(0x4, 0xc0, 13); /* FSP-OP->1, FSP-WR->1, VRCG=0, DMD=0 */ |
| 323 | } |
| 324 | |
| 325 | void save_trained_mr12_14(struct dram_cfg_param *cfg, u32 cfg_num, u32 mr12, u32 mr14) |
| 326 | { |
| 327 | int i; |
| 328 | |
| 329 | for (i = 0; i < cfg_num; i++) { |
| 330 | if (cfg->reg == 12) |
| 331 | cfg->val = mr12; |
| 332 | else if (cfg->reg == 14) |
| 333 | cfg->val = mr14; |
| 334 | cfg++; |
| 335 | } |
| 336 | } |
| 337 | |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 338 | int ddr_init(struct dram_timing_info *dram_timing) |
| 339 | { |
| 340 | unsigned int initial_drate; |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 341 | struct dram_timing_info *saved_timing; |
| 342 | void *fsp; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 343 | int ret; |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 344 | u32 mr12, mr14; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 345 | u32 regval; |
| 346 | |
| 347 | debug("DDRINFO: start DRAM init\n"); |
| 348 | |
| 349 | /* reset ddrphy */ |
| 350 | ddrphy_coldreset(); |
| 351 | |
| 352 | debug("DDRINFO: cfg clk\n"); |
| 353 | |
| 354 | initial_drate = dram_timing->fsp_msg[0].drate; |
| 355 | /* default to the frequency point 0 clock */ |
| 356 | ddrphy_init_set_dfi_clk(initial_drate); |
| 357 | |
| 358 | /* |
| 359 | * Start PHY initialization and training by |
| 360 | * accessing relevant PUB registers |
| 361 | */ |
| 362 | debug("DDRINFO:ddrphy config start\n"); |
| 363 | |
| 364 | ret = ddr_cfg_phy(dram_timing); |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | |
| 368 | debug("DDRINFO: ddrphy config done\n"); |
| 369 | |
Jacky Bai | eb3ff92 | 2023-04-28 12:08:44 +0800 | [diff] [blame] | 370 | update_umctl2_rank_space_setting(dram_timing, dram_timing->fsp_msg_num - 1); |
| 371 | |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 372 | /* rogram the ddrc registers */ |
| 373 | debug("DDRINFO: ddrc config start\n"); |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 374 | ddrc_config(dram_timing); |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 375 | debug("DDRINFO: ddrc config done\n"); |
| 376 | |
Ye Li | f188a4f | 2022-07-26 16:41:08 +0800 | [diff] [blame] | 377 | #ifdef CONFIG_IMX9_DRAM_PM_COUNTER |
| 378 | writel(0x200000, REG_DDR_DEBUG_19); |
| 379 | #endif |
| 380 | |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 381 | check_dfi_init_complete(); |
| 382 | |
| 383 | regval = readl(REG_DDR_SDRAM_CFG); |
| 384 | writel((regval | 0x80000000), REG_DDR_SDRAM_CFG); |
| 385 | |
| 386 | check_ddrc_idle(); |
| 387 | |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 388 | mr12 = lpddr4_mr_read(1, 12); |
| 389 | mr14 = lpddr4_mr_read(1, 14); |
| 390 | |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 391 | /* save the dram timing config into memory */ |
Jacky Bai | 9ded797 | 2023-04-28 12:08:43 +0800 | [diff] [blame] | 392 | fsp = dram_config_save(dram_timing, CONFIG_SAVED_DRAM_TIMING_BASE); |
| 393 | |
| 394 | saved_timing = (struct dram_timing_info *)CONFIG_SAVED_DRAM_TIMING_BASE; |
| 395 | saved_timing->fsp_cfg = fsp; |
| 396 | saved_timing->fsp_cfg_num = dram_timing->fsp_cfg_num; |
| 397 | if (saved_timing->fsp_cfg_num) { |
| 398 | memcpy(saved_timing->fsp_cfg, dram_timing->fsp_cfg, |
| 399 | dram_timing->fsp_cfg_num * sizeof(struct dram_fsp_cfg)); |
| 400 | |
| 401 | save_trained_mr12_14(saved_timing->fsp_cfg[0].mr_cfg, |
| 402 | ARRAY_SIZE(saved_timing->fsp_cfg[0].mr_cfg), mr12, mr14); |
| 403 | /* |
| 404 | * Configure mode registers in fsp1 to mode register 0 because DDRC |
| 405 | * doesn't automatically set. |
| 406 | */ |
| 407 | if (saved_timing->fsp_cfg_num > 1) |
| 408 | update_mr_fsp_op0(saved_timing->fsp_cfg[1].mr_cfg, |
| 409 | ARRAY_SIZE(saved_timing->fsp_cfg[1].mr_cfg)); |
| 410 | } |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame] | 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | ulong ddrphy_addr_remap(u32 paddr_apb_from_ctlr) |
| 416 | { |
| 417 | u32 paddr_apb_qual; |
| 418 | u32 paddr_apb_unqual_dec_22_13; |
| 419 | u32 paddr_apb_unqual_dec_19_13; |
| 420 | u32 paddr_apb_unqual_dec_12_1; |
| 421 | u32 paddr_apb_unqual; |
| 422 | u32 paddr_apb_phy; |
| 423 | |
| 424 | paddr_apb_qual = (paddr_apb_from_ctlr << 1); |
| 425 | paddr_apb_unqual_dec_22_13 = ((paddr_apb_qual & 0x7fe000) >> 13); |
| 426 | paddr_apb_unqual_dec_12_1 = ((paddr_apb_qual & 0x1ffe) >> 1); |
| 427 | |
| 428 | switch (paddr_apb_unqual_dec_22_13) { |
| 429 | case 0x000: |
| 430 | paddr_apb_unqual_dec_19_13 = 0x00; |
| 431 | break; |
| 432 | case 0x001: |
| 433 | paddr_apb_unqual_dec_19_13 = 0x01; |
| 434 | break; |
| 435 | case 0x002: |
| 436 | paddr_apb_unqual_dec_19_13 = 0x02; |
| 437 | break; |
| 438 | case 0x003: |
| 439 | paddr_apb_unqual_dec_19_13 = 0x03; |
| 440 | break; |
| 441 | case 0x004: |
| 442 | paddr_apb_unqual_dec_19_13 = 0x04; |
| 443 | break; |
| 444 | case 0x005: |
| 445 | paddr_apb_unqual_dec_19_13 = 0x05; |
| 446 | break; |
| 447 | case 0x006: |
| 448 | paddr_apb_unqual_dec_19_13 = 0x06; |
| 449 | break; |
| 450 | case 0x007: |
| 451 | paddr_apb_unqual_dec_19_13 = 0x07; |
| 452 | break; |
| 453 | case 0x008: |
| 454 | paddr_apb_unqual_dec_19_13 = 0x08; |
| 455 | break; |
| 456 | case 0x009: |
| 457 | paddr_apb_unqual_dec_19_13 = 0x09; |
| 458 | break; |
| 459 | case 0x00a: |
| 460 | paddr_apb_unqual_dec_19_13 = 0x0a; |
| 461 | break; |
| 462 | case 0x00b: |
| 463 | paddr_apb_unqual_dec_19_13 = 0x0b; |
| 464 | break; |
| 465 | case 0x100: |
| 466 | paddr_apb_unqual_dec_19_13 = 0x0c; |
| 467 | break; |
| 468 | case 0x101: |
| 469 | paddr_apb_unqual_dec_19_13 = 0x0d; |
| 470 | break; |
| 471 | case 0x102: |
| 472 | paddr_apb_unqual_dec_19_13 = 0x0e; |
| 473 | break; |
| 474 | case 0x103: |
| 475 | paddr_apb_unqual_dec_19_13 = 0x0f; |
| 476 | break; |
| 477 | case 0x104: |
| 478 | paddr_apb_unqual_dec_19_13 = 0x10; |
| 479 | break; |
| 480 | case 0x105: |
| 481 | paddr_apb_unqual_dec_19_13 = 0x11; |
| 482 | break; |
| 483 | case 0x106: |
| 484 | paddr_apb_unqual_dec_19_13 = 0x12; |
| 485 | break; |
| 486 | case 0x107: |
| 487 | paddr_apb_unqual_dec_19_13 = 0x13; |
| 488 | break; |
| 489 | case 0x108: |
| 490 | paddr_apb_unqual_dec_19_13 = 0x14; |
| 491 | break; |
| 492 | case 0x109: |
| 493 | paddr_apb_unqual_dec_19_13 = 0x15; |
| 494 | break; |
| 495 | case 0x10a: |
| 496 | paddr_apb_unqual_dec_19_13 = 0x16; |
| 497 | break; |
| 498 | case 0x10b: |
| 499 | paddr_apb_unqual_dec_19_13 = 0x17; |
| 500 | break; |
| 501 | case 0x200: |
| 502 | paddr_apb_unqual_dec_19_13 = 0x18; |
| 503 | break; |
| 504 | case 0x201: |
| 505 | paddr_apb_unqual_dec_19_13 = 0x19; |
| 506 | break; |
| 507 | case 0x202: |
| 508 | paddr_apb_unqual_dec_19_13 = 0x1a; |
| 509 | break; |
| 510 | case 0x203: |
| 511 | paddr_apb_unqual_dec_19_13 = 0x1b; |
| 512 | break; |
| 513 | case 0x204: |
| 514 | paddr_apb_unqual_dec_19_13 = 0x1c; |
| 515 | break; |
| 516 | case 0x205: |
| 517 | paddr_apb_unqual_dec_19_13 = 0x1d; |
| 518 | break; |
| 519 | case 0x206: |
| 520 | paddr_apb_unqual_dec_19_13 = 0x1e; |
| 521 | break; |
| 522 | case 0x207: |
| 523 | paddr_apb_unqual_dec_19_13 = 0x1f; |
| 524 | break; |
| 525 | case 0x208: |
| 526 | paddr_apb_unqual_dec_19_13 = 0x20; |
| 527 | break; |
| 528 | case 0x209: |
| 529 | paddr_apb_unqual_dec_19_13 = 0x21; |
| 530 | break; |
| 531 | case 0x20a: |
| 532 | paddr_apb_unqual_dec_19_13 = 0x22; |
| 533 | break; |
| 534 | case 0x20b: |
| 535 | paddr_apb_unqual_dec_19_13 = 0x23; |
| 536 | break; |
| 537 | case 0x300: |
| 538 | paddr_apb_unqual_dec_19_13 = 0x24; |
| 539 | break; |
| 540 | case 0x301: |
| 541 | paddr_apb_unqual_dec_19_13 = 0x25; |
| 542 | break; |
| 543 | case 0x302: |
| 544 | paddr_apb_unqual_dec_19_13 = 0x26; |
| 545 | break; |
| 546 | case 0x303: |
| 547 | paddr_apb_unqual_dec_19_13 = 0x27; |
| 548 | break; |
| 549 | case 0x304: |
| 550 | paddr_apb_unqual_dec_19_13 = 0x28; |
| 551 | break; |
| 552 | case 0x305: |
| 553 | paddr_apb_unqual_dec_19_13 = 0x29; |
| 554 | break; |
| 555 | case 0x306: |
| 556 | paddr_apb_unqual_dec_19_13 = 0x2a; |
| 557 | break; |
| 558 | case 0x307: |
| 559 | paddr_apb_unqual_dec_19_13 = 0x2b; |
| 560 | break; |
| 561 | case 0x308: |
| 562 | paddr_apb_unqual_dec_19_13 = 0x2c; |
| 563 | break; |
| 564 | case 0x309: |
| 565 | paddr_apb_unqual_dec_19_13 = 0x2d; |
| 566 | break; |
| 567 | case 0x30a: |
| 568 | paddr_apb_unqual_dec_19_13 = 0x2e; |
| 569 | break; |
| 570 | case 0x30b: |
| 571 | paddr_apb_unqual_dec_19_13 = 0x2f; |
| 572 | break; |
| 573 | case 0x010: |
| 574 | paddr_apb_unqual_dec_19_13 = 0x30; |
| 575 | break; |
| 576 | case 0x011: |
| 577 | paddr_apb_unqual_dec_19_13 = 0x31; |
| 578 | break; |
| 579 | case 0x012: |
| 580 | paddr_apb_unqual_dec_19_13 = 0x32; |
| 581 | break; |
| 582 | case 0x013: |
| 583 | paddr_apb_unqual_dec_19_13 = 0x33; |
| 584 | break; |
| 585 | case 0x014: |
| 586 | paddr_apb_unqual_dec_19_13 = 0x34; |
| 587 | break; |
| 588 | case 0x015: |
| 589 | paddr_apb_unqual_dec_19_13 = 0x35; |
| 590 | break; |
| 591 | case 0x016: |
| 592 | paddr_apb_unqual_dec_19_13 = 0x36; |
| 593 | break; |
| 594 | case 0x017: |
| 595 | paddr_apb_unqual_dec_19_13 = 0x37; |
| 596 | break; |
| 597 | case 0x018: |
| 598 | paddr_apb_unqual_dec_19_13 = 0x38; |
| 599 | break; |
| 600 | case 0x019: |
| 601 | paddr_apb_unqual_dec_19_13 = 0x39; |
| 602 | break; |
| 603 | case 0x110: |
| 604 | paddr_apb_unqual_dec_19_13 = 0x3a; |
| 605 | break; |
| 606 | case 0x111: |
| 607 | paddr_apb_unqual_dec_19_13 = 0x3b; |
| 608 | break; |
| 609 | case 0x112: |
| 610 | paddr_apb_unqual_dec_19_13 = 0x3c; |
| 611 | break; |
| 612 | case 0x113: |
| 613 | paddr_apb_unqual_dec_19_13 = 0x3d; |
| 614 | break; |
| 615 | case 0x114: |
| 616 | paddr_apb_unqual_dec_19_13 = 0x3e; |
| 617 | break; |
| 618 | case 0x115: |
| 619 | paddr_apb_unqual_dec_19_13 = 0x3f; |
| 620 | break; |
| 621 | case 0x116: |
| 622 | paddr_apb_unqual_dec_19_13 = 0x40; |
| 623 | break; |
| 624 | case 0x117: |
| 625 | paddr_apb_unqual_dec_19_13 = 0x41; |
| 626 | break; |
| 627 | case 0x118: |
| 628 | paddr_apb_unqual_dec_19_13 = 0x42; |
| 629 | break; |
| 630 | case 0x119: |
| 631 | paddr_apb_unqual_dec_19_13 = 0x43; |
| 632 | break; |
| 633 | case 0x210: |
| 634 | paddr_apb_unqual_dec_19_13 = 0x44; |
| 635 | break; |
| 636 | case 0x211: |
| 637 | paddr_apb_unqual_dec_19_13 = 0x45; |
| 638 | break; |
| 639 | case 0x212: |
| 640 | paddr_apb_unqual_dec_19_13 = 0x46; |
| 641 | break; |
| 642 | case 0x213: |
| 643 | paddr_apb_unqual_dec_19_13 = 0x47; |
| 644 | break; |
| 645 | case 0x214: |
| 646 | paddr_apb_unqual_dec_19_13 = 0x48; |
| 647 | break; |
| 648 | case 0x215: |
| 649 | paddr_apb_unqual_dec_19_13 = 0x49; |
| 650 | break; |
| 651 | case 0x216: |
| 652 | paddr_apb_unqual_dec_19_13 = 0x4a; |
| 653 | break; |
| 654 | case 0x217: |
| 655 | paddr_apb_unqual_dec_19_13 = 0x4b; |
| 656 | break; |
| 657 | case 0x218: |
| 658 | paddr_apb_unqual_dec_19_13 = 0x4c; |
| 659 | break; |
| 660 | case 0x219: |
| 661 | paddr_apb_unqual_dec_19_13 = 0x4d; |
| 662 | break; |
| 663 | case 0x310: |
| 664 | paddr_apb_unqual_dec_19_13 = 0x4e; |
| 665 | break; |
| 666 | case 0x311: |
| 667 | paddr_apb_unqual_dec_19_13 = 0x4f; |
| 668 | break; |
| 669 | case 0x312: |
| 670 | paddr_apb_unqual_dec_19_13 = 0x50; |
| 671 | break; |
| 672 | case 0x313: |
| 673 | paddr_apb_unqual_dec_19_13 = 0x51; |
| 674 | break; |
| 675 | case 0x314: |
| 676 | paddr_apb_unqual_dec_19_13 = 0x52; |
| 677 | break; |
| 678 | case 0x315: |
| 679 | paddr_apb_unqual_dec_19_13 = 0x53; |
| 680 | break; |
| 681 | case 0x316: |
| 682 | paddr_apb_unqual_dec_19_13 = 0x54; |
| 683 | break; |
| 684 | case 0x317: |
| 685 | paddr_apb_unqual_dec_19_13 = 0x55; |
| 686 | break; |
| 687 | case 0x318: |
| 688 | paddr_apb_unqual_dec_19_13 = 0x56; |
| 689 | break; |
| 690 | case 0x319: |
| 691 | paddr_apb_unqual_dec_19_13 = 0x57; |
| 692 | break; |
| 693 | case 0x020: |
| 694 | paddr_apb_unqual_dec_19_13 = 0x58; |
| 695 | break; |
| 696 | case 0x120: |
| 697 | paddr_apb_unqual_dec_19_13 = 0x59; |
| 698 | break; |
| 699 | case 0x220: |
| 700 | paddr_apb_unqual_dec_19_13 = 0x5a; |
| 701 | break; |
| 702 | case 0x320: |
| 703 | paddr_apb_unqual_dec_19_13 = 0x5b; |
| 704 | break; |
| 705 | case 0x040: |
| 706 | paddr_apb_unqual_dec_19_13 = 0x5c; |
| 707 | break; |
| 708 | case 0x140: |
| 709 | paddr_apb_unqual_dec_19_13 = 0x5d; |
| 710 | break; |
| 711 | case 0x240: |
| 712 | paddr_apb_unqual_dec_19_13 = 0x5e; |
| 713 | break; |
| 714 | case 0x340: |
| 715 | paddr_apb_unqual_dec_19_13 = 0x5f; |
| 716 | break; |
| 717 | case 0x050: |
| 718 | paddr_apb_unqual_dec_19_13 = 0x60; |
| 719 | break; |
| 720 | case 0x051: |
| 721 | paddr_apb_unqual_dec_19_13 = 0x61; |
| 722 | break; |
| 723 | case 0x052: |
| 724 | paddr_apb_unqual_dec_19_13 = 0x62; |
| 725 | break; |
| 726 | case 0x053: |
| 727 | paddr_apb_unqual_dec_19_13 = 0x63; |
| 728 | break; |
| 729 | case 0x054: |
| 730 | paddr_apb_unqual_dec_19_13 = 0x64; |
| 731 | break; |
| 732 | case 0x055: |
| 733 | paddr_apb_unqual_dec_19_13 = 0x65; |
| 734 | break; |
| 735 | case 0x056: |
| 736 | paddr_apb_unqual_dec_19_13 = 0x66; |
| 737 | break; |
| 738 | case 0x057: |
| 739 | paddr_apb_unqual_dec_19_13 = 0x67; |
| 740 | break; |
| 741 | case 0x070: |
| 742 | paddr_apb_unqual_dec_19_13 = 0x68; |
| 743 | break; |
| 744 | case 0x090: |
| 745 | paddr_apb_unqual_dec_19_13 = 0x69; |
| 746 | break; |
| 747 | case 0x190: |
| 748 | paddr_apb_unqual_dec_19_13 = 0x6a; |
| 749 | break; |
| 750 | case 0x290: |
| 751 | paddr_apb_unqual_dec_19_13 = 0x6b; |
| 752 | break; |
| 753 | case 0x390: |
| 754 | paddr_apb_unqual_dec_19_13 = 0x6c; |
| 755 | break; |
| 756 | case 0x0c0: |
| 757 | paddr_apb_unqual_dec_19_13 = 0x6d; |
| 758 | break; |
| 759 | case 0x0d0: |
| 760 | paddr_apb_unqual_dec_19_13 = 0x6e; |
| 761 | break; |
| 762 | default: |
| 763 | paddr_apb_unqual_dec_19_13 = 0x00; |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | paddr_apb_unqual = ((paddr_apb_unqual_dec_19_13 << 13) | (paddr_apb_unqual_dec_12_1 << 1)); |
| 768 | |
| 769 | paddr_apb_phy = (paddr_apb_unqual << 1); |
| 770 | |
| 771 | return paddr_apb_phy; |
| 772 | } |