Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2018 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 8 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 10 | #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 Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 15 | #include <asm/sections.h> |
| 16 | |
| 17 | DECLARE_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 */ |
| 28 | void 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 Fan | 4f992e5 | 2019-08-27 06:24:47 +0000 | [diff] [blame] | 35 | 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 Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 46 | |
| 47 | pr_from32 = imem_start; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 48 | pr_to32 = IMEM_OFFSET_ADDR; |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 49 | for (i = 0x0; i < IMEM_LEN; ) { |
| 50 | tmp32 = readl(pr_from32); |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 51 | 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 Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 56 | pr_from32 += 4; |
| 57 | i += 4; |
| 58 | } |
| 59 | |
| 60 | pr_from32 = dmem_start; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 61 | pr_to32 = DMEM_OFFSET_ADDR; |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 62 | for (i = 0x0; i < DMEM_LEN; ) { |
| 63 | tmp32 = readl(pr_from32); |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 64 | 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 Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 69 | pr_from32 += 4; |
| 70 | i += 4; |
| 71 | } |
| 72 | |
Jacky Bai | d62ddc1 | 2019-08-08 09:59:08 +0000 | [diff] [blame] | 73 | debug("check ddr_pmu_train_imem code\n"); |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 74 | pr_from32 = imem_start; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 75 | pr_to32 = IMEM_OFFSET_ADDR; |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 76 | for (i = 0x0; i < IMEM_LEN; ) { |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 77 | 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 Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 81 | |
| 82 | if (tmp32 != readl(pr_from32)) { |
| 83 | debug("%lx %lx\n", pr_from32, pr_to32); |
| 84 | error++; |
| 85 | } |
| 86 | pr_from32 += 4; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 87 | pr_to32 += 1; |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 88 | i += 4; |
| 89 | } |
| 90 | if (error) |
Jacky Bai | d62ddc1 | 2019-08-08 09:59:08 +0000 | [diff] [blame] | 91 | printf("check ddr_pmu_train_imem code fail=%d\n", error); |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 92 | else |
Jacky Bai | d62ddc1 | 2019-08-08 09:59:08 +0000 | [diff] [blame] | 93 | debug("check ddr_pmu_train_imem code pass\n"); |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 94 | |
| 95 | debug("check ddr4_pmu_train_dmem code\n"); |
| 96 | pr_from32 = dmem_start; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 97 | pr_to32 = DMEM_OFFSET_ADDR; |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 98 | for (i = 0x0; i < DMEM_LEN;) { |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 99 | 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 Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 103 | if (tmp32 != readl(pr_from32)) { |
| 104 | debug("%lx %lx\n", pr_from32, pr_to32); |
| 105 | error++; |
| 106 | } |
| 107 | pr_from32 += 4; |
Ye Li | b2cfc42 | 2022-07-26 16:41:07 +0800 | [diff] [blame^] | 108 | pr_to32 += 1; |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 109 | i += 4; |
| 110 | } |
| 111 | |
| 112 | if (error) |
Jacky Bai | d62ddc1 | 2019-08-08 09:59:08 +0000 | [diff] [blame] | 113 | printf("check ddr_pmu_train_dmem code fail=%d", error); |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 114 | else |
Jacky Bai | d62ddc1 | 2019-08-08 09:59:08 +0000 | [diff] [blame] | 115 | debug("check ddr_pmu_train_dmem code pass\n"); |
Peng Fan | 692f943 | 2018-11-20 10:19:57 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void 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 | |
| 135 | void 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 | } |