blob: 0b36d6c027dd27d3509dbb975cf80a9f3d064bdc [file] [log] [blame]
Ley Foon Tan0bc28b72018-05-24 00:17:30 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2016-2018 Intel Corporation <www.intel.com>
4 *
5 */
6
7#include <common.h>
Simon Glass9edefc22019-11-14 12:57:37 -07008#include <cpu_func.h>
Ley Foon Tan6bf238a2019-05-06 09:56:01 +08009#include <dm.h>
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080010#include <errno.h>
11#include <div64.h>
Ley Foon Tan6cd71342019-03-22 01:24:01 +080012#include <fdtdec.h>
Simon Glassdb41d652019-12-28 10:45:07 -070013#include <hang.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070014#include <init.h>
Ley Foon Tan6bf238a2019-05-06 09:56:01 +080015#include <ram.h>
16#include <reset.h>
17#include "sdram_s10.h"
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080018#include <wait_bit.h>
Ley Foon Tan8b7962a2019-11-27 15:55:15 +080019#include <asm/arch/firewall.h>
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080020#include <asm/arch/reset_manager.h>
Ley Foon Tan6bf238a2019-05-06 09:56:01 +080021#include <asm/io.h>
Ley Foon Tan6cd71342019-03-22 01:24:01 +080022#include <linux/sizes.h>
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080023
24DECLARE_GLOBAL_DATA_PTR;
25
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080026#define DDR_CONFIG(A, B, C, R) (((A) << 24) | ((B) << 16) | ((C) << 8) | (R))
27
28/* The followring are the supported configurations */
29u32 ddr_config[] = {
30 /* DDR_CONFIG(Address order,Bank,Column,Row) */
31 /* List for DDR3 or LPDDR3 (pinout order > chip, row, bank, column) */
32 DDR_CONFIG(0, 3, 10, 12),
33 DDR_CONFIG(0, 3, 9, 13),
34 DDR_CONFIG(0, 3, 10, 13),
35 DDR_CONFIG(0, 3, 9, 14),
36 DDR_CONFIG(0, 3, 10, 14),
37 DDR_CONFIG(0, 3, 10, 15),
38 DDR_CONFIG(0, 3, 11, 14),
39 DDR_CONFIG(0, 3, 11, 15),
40 DDR_CONFIG(0, 3, 10, 16),
41 DDR_CONFIG(0, 3, 11, 16),
42 DDR_CONFIG(0, 3, 12, 15), /* 0xa */
43 /* List for DDR4 only (pinout order > chip, bank, row, column) */
44 DDR_CONFIG(1, 3, 10, 14),
45 DDR_CONFIG(1, 4, 10, 14),
46 DDR_CONFIG(1, 3, 10, 15),
47 DDR_CONFIG(1, 4, 10, 15),
48 DDR_CONFIG(1, 3, 10, 16),
49 DDR_CONFIG(1, 4, 10, 16),
50 DDR_CONFIG(1, 3, 10, 17),
51 DDR_CONFIG(1, 4, 10, 17),
52};
53
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080054int match_ddr_conf(u32 ddr_conf)
55{
56 int i;
57
58 for (i = 0; i < ARRAY_SIZE(ddr_config); i++) {
59 if (ddr_conf == ddr_config[i])
60 return i;
61 }
62 return 0;
63}
64
Ley Foon Tan6bf238a2019-05-06 09:56:01 +080065/**
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080066 * sdram_mmr_init_full() - Function to initialize SDRAM MMR
67 *
68 * Initialize the SDRAM MMR.
69 */
Ley Foon Tan733cc6c2019-11-27 15:55:26 +080070int sdram_mmr_init_full(struct udevice *dev)
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080071{
Ley Foon Tan6bf238a2019-05-06 09:56:01 +080072 struct altera_sdram_platdata *plat = dev->platdata;
73 struct altera_sdram_priv *priv = dev_get_priv(dev);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080074 u32 update_value, io48_value, ddrioctl;
75 u32 i;
76 int ret;
Ley Foon Tan6cd71342019-03-22 01:24:01 +080077 phys_size_t hw_size;
78 bd_t bd = {0};
Ley Foon Tan0bc28b72018-05-24 00:17:30 +080079
80 /* Enable access to DDR from CPU master */
81 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_DDRREG),
82 CCU_ADBASE_DI_MASK);
83 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE0),
84 CCU_ADBASE_DI_MASK);
85 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1A),
86 CCU_ADBASE_DI_MASK);
87 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1B),
88 CCU_ADBASE_DI_MASK);
89 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1C),
90 CCU_ADBASE_DI_MASK);
91 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1D),
92 CCU_ADBASE_DI_MASK);
93 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1E),
94 CCU_ADBASE_DI_MASK);
95
96 /* Enable access to DDR from IO master */
97 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE0),
98 CCU_ADBASE_DI_MASK);
99 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1A),
100 CCU_ADBASE_DI_MASK);
101 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1B),
102 CCU_ADBASE_DI_MASK);
103 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1C),
104 CCU_ADBASE_DI_MASK);
105 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1D),
106 CCU_ADBASE_DI_MASK);
107 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1E),
108 CCU_ADBASE_DI_MASK);
109
Thor Thayer62079b22019-12-06 13:47:31 -0600110 /* Enable access to DDR from TCU */
111 clrbits_le32(CCU_REG_ADDR(CCU_TCU_MPRT_ADBASE_MEMSPACE0),
112 CCU_ADBASE_DI_MASK);
113 clrbits_le32(CCU_REG_ADDR(CCU_TCU_MPRT_ADBASE_MEMSPACE1A),
114 CCU_ADBASE_DI_MASK);
115 clrbits_le32(CCU_REG_ADDR(CCU_TCU_MPRT_ADBASE_MEMSPACE1B),
116 CCU_ADBASE_DI_MASK);
117 clrbits_le32(CCU_REG_ADDR(CCU_TCU_MPRT_ADBASE_MEMSPACE1C),
118 CCU_ADBASE_DI_MASK);
119 clrbits_le32(CCU_REG_ADDR(CCU_TCU_MPRT_ADBASE_MEMSPACE1D),
120 CCU_ADBASE_DI_MASK);
121 clrbits_le32(CCU_REG_ADDR(CCU_TCU_MPRT_ADBASE_MEMSPACE1E),
122 CCU_ADBASE_DI_MASK);
123
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800124 /* this enables nonsecure access to DDR */
125 /* mpuregion0addr_limit */
126 FW_MPU_DDR_SCR_WRITEL(0xFFFF0000, FW_MPU_DDR_SCR_MPUREGION0ADDR_LIMIT);
127 FW_MPU_DDR_SCR_WRITEL(0x1F, FW_MPU_DDR_SCR_MPUREGION0ADDR_LIMITEXT);
128
129 /* nonmpuregion0addr_limit */
130 FW_MPU_DDR_SCR_WRITEL(0xFFFF0000,
131 FW_MPU_DDR_SCR_NONMPUREGION0ADDR_LIMIT);
132 FW_MPU_DDR_SCR_WRITEL(0x1F, FW_MPU_DDR_SCR_NONMPUREGION0ADDR_LIMITEXT);
133
134 /* Enable mpuregion0enable and nonmpuregion0enable */
135 FW_MPU_DDR_SCR_WRITEL(MPUREGION0_ENABLE | NONMPUREGION0_ENABLE,
136 FW_MPU_DDR_SCR_EN_SET);
137
138 /* Ensure HMC clock is running */
139 if (poll_hmc_clock_status()) {
140 puts("DDR: Error as HMC clock not running\n");
141 return -1;
142 }
143
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800144 /* Try 3 times to do a calibration */
145 for (i = 0; i < 3; i++) {
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800146 ret = wait_for_bit_le32((const void *)(plat->hmc +
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800147 DDRCALSTAT),
148 DDR_HMC_DDRCALSTAT_CAL_MSK, true, 1000,
149 false);
150 if (!ret)
151 break;
152
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800153 emif_reset(plat);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800154 }
155
156 if (ret) {
157 puts("DDR: Error as SDRAM calibration failed\n");
158 return -1;
159 }
160 debug("DDR: Calibration success\n");
161
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800162 u32 ctrlcfg0 = hmc_readl(plat, CTRLCFG0);
163 u32 ctrlcfg1 = hmc_readl(plat, CTRLCFG1);
164 u32 dramaddrw = hmc_readl(plat, DRAMADDRW);
165 u32 dramtim0 = hmc_readl(plat, DRAMTIMING0);
166 u32 caltim0 = hmc_readl(plat, CALTIMING0);
167 u32 caltim1 = hmc_readl(plat, CALTIMING1);
168 u32 caltim2 = hmc_readl(plat, CALTIMING2);
169 u32 caltim3 = hmc_readl(plat, CALTIMING3);
170 u32 caltim4 = hmc_readl(plat, CALTIMING4);
171 u32 caltim9 = hmc_readl(plat, CALTIMING9);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800172
173 /*
174 * Configure the DDR IO size [0xFFCFB008]
175 * niosreserve0: Used to indicate DDR width &
176 * bit[7:0] = Number of data bits (bit[6:5] 0x01=32bit, 0x10=64bit)
177 * bit[8] = 1 if user-mode OCT is present
178 * bit[9] = 1 if warm reset compiled into EMIF Cal Code
179 * bit[10] = 1 if warm reset is on during generation in EMIF Cal
180 * niosreserve1: IP ADCDS version encoded as 16 bit value
181 * bit[2:0] = Variant (0=not special,1=FAE beta, 2=Customer beta,
182 * 3=EAP, 4-6 are reserved)
183 * bit[5:3] = Service Pack # (e.g. 1)
184 * bit[9:6] = Minor Release #
185 * bit[14:10] = Major Release #
186 */
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800187 update_value = hmc_readl(plat, NIOSRESERVED0);
188 hmc_ecc_writel(plat, ((update_value & 0xFF) >> 5), DDRIOCTRL);
189 ddrioctl = hmc_ecc_readl(plat, DDRIOCTRL);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800190
191 /* enable HPS interface to HMC */
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800192 hmc_ecc_writel(plat, DDR_HMC_HPSINTFCSEL_ENABLE_MASK, HPSINTFCSEL);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800193
194 /* Set the DDR Configuration */
195 io48_value = DDR_CONFIG(CTRLCFG1_CFG_ADDR_ORDER(ctrlcfg1),
196 (DRAMADDRW_CFG_BANK_ADDR_WIDTH(dramaddrw) +
197 DRAMADDRW_CFG_BANK_GRP_ADDR_WIDTH(dramaddrw)),
198 DRAMADDRW_CFG_COL_ADDR_WIDTH(dramaddrw),
199 DRAMADDRW_CFG_ROW_ADDR_WIDTH(dramaddrw));
200
201 update_value = match_ddr_conf(io48_value);
202 if (update_value)
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800203 ddr_sch_writel(plat, update_value, DDR_SCH_DDRCONF);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800204
205 /* Configure HMC dramaddrw */
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800206 hmc_ecc_writel(plat, hmc_readl(plat, DRAMADDRW), DRAMADDRWIDTH);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800207
208 /*
209 * Configure DDR timing
210 * RDTOMISS = tRTP + tRP + tRCD - BL/2
211 * WRTOMISS = WL + tWR + tRP + tRCD and
212 * WL = RL + BL/2 + 2 - rd-to-wr ; tWR = 15ns so...
213 * First part of equation is in memory clock units so divide by 2
214 * for HMC clock units. 1066MHz is close to 1ns so use 15 directly.
215 * WRTOMISS = ((RL + BL/2 + 2 + tWR) >> 1)- rd-to-wr + tRP + tRCD
216 */
217 u32 burst_len = CTRLCFG0_CFG_CTRL_BURST_LEN(ctrlcfg0);
218
219 update_value = CALTIMING2_CFG_RD_TO_WR_PCH(caltim2) +
220 CALTIMING4_CFG_PCH_TO_VALID(caltim4) +
221 CALTIMING0_CFG_ACT_TO_RDWR(caltim0) -
222 (burst_len >> 2);
223 io48_value = (((DRAMTIMING0_CFG_TCL(dramtim0) + 2 + DDR_TWR +
224 (burst_len >> 1)) >> 1) -
225 /* Up to here was in memory cycles so divide by 2 */
226 CALTIMING1_CFG_RD_TO_WR(caltim1) +
227 CALTIMING0_CFG_ACT_TO_RDWR(caltim0) +
228 CALTIMING4_CFG_PCH_TO_VALID(caltim4));
229
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800230 ddr_sch_writel(plat, ((CALTIMING0_CFG_ACT_TO_ACT(caltim0) <<
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800231 DDR_SCH_DDRTIMING_ACTTOACT_OFF) |
232 (update_value << DDR_SCH_DDRTIMING_RDTOMISS_OFF) |
233 (io48_value << DDR_SCH_DDRTIMING_WRTOMISS_OFF) |
234 ((burst_len >> 2) << DDR_SCH_DDRTIMING_BURSTLEN_OFF) |
235 (CALTIMING1_CFG_RD_TO_WR(caltim1) <<
236 DDR_SCH_DDRTIMING_RDTOWR_OFF) |
237 (CALTIMING3_CFG_WR_TO_RD(caltim3) <<
238 DDR_SCH_DDRTIMING_WRTORD_OFF) |
239 (((ddrioctl == 1) ? 1 : 0) <<
240 DDR_SCH_DDRTIMING_BWRATIO_OFF)),
241 DDR_SCH_DDRTIMING);
242
243 /* Configure DDR mode [precharge = 0] */
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800244 ddr_sch_writel(plat, ((ddrioctl ? 0 : 1) <<
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800245 DDR_SCH_DDRMOD_BWRATIOEXTENDED_OFF),
246 DDR_SCH_DDRMODE);
247
248 /* Configure the read latency */
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800249 ddr_sch_writel(plat, (DRAMTIMING0_CFG_TCL(dramtim0) >> 1) +
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800250 DDR_READ_LATENCY_DELAY,
251 DDR_SCH_READ_LATENCY);
252
253 /*
254 * Configuring timing values concerning activate commands
255 * [FAWBANK alway 1 because always 4 bank DDR]
256 */
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800257 ddr_sch_writel(plat, ((CALTIMING0_CFG_ACT_TO_ACT_DB(caltim0) <<
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800258 DDR_SCH_ACTIVATE_RRD_OFF) |
259 (CALTIMING9_CFG_4_ACT_TO_ACT(caltim9) <<
260 DDR_SCH_ACTIVATE_FAW_OFF) |
261 (DDR_ACTIVATE_FAWBANK <<
262 DDR_SCH_ACTIVATE_FAWBANK_OFF)),
263 DDR_SCH_ACTIVATE);
264
265 /*
266 * Configuring timing values concerning device to device data bus
267 * ownership change
268 */
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800269 ddr_sch_writel(plat, ((CALTIMING1_CFG_RD_TO_RD_DC(caltim1) <<
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800270 DDR_SCH_DEVTODEV_BUSRDTORD_OFF) |
271 (CALTIMING1_CFG_RD_TO_WR_DC(caltim1) <<
272 DDR_SCH_DEVTODEV_BUSRDTOWR_OFF) |
273 (CALTIMING3_CFG_WR_TO_RD_DC(caltim3) <<
274 DDR_SCH_DEVTODEV_BUSWRTORD_OFF)),
275 DDR_SCH_DEVTODEV);
276
277 /* assigning the SDRAM size */
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800278 unsigned long long size = sdram_calculate_size(plat);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800279 /* If the size is invalid, use default Config size */
280 if (size <= 0)
Ley Foon Tan6cd71342019-03-22 01:24:01 +0800281 hw_size = PHYS_SDRAM_1_SIZE;
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800282 else
Ley Foon Tan6cd71342019-03-22 01:24:01 +0800283 hw_size = size;
284
285 /* Get bank configuration from devicetree */
286 ret = fdtdec_decode_ram_size(gd->fdt_blob, NULL, 0, NULL,
287 (phys_size_t *)&gd->ram_size, &bd);
288 if (ret) {
289 puts("DDR: Failed to decode memory node\n");
290 return -1;
291 }
292
293 if (gd->ram_size != hw_size)
294 printf("DDR: Warning: DRAM size from device tree mismatch with hardware.\n");
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800295
Ley Foon Tanb6f7ee52019-03-22 01:24:00 +0800296 printf("DDR: %lld MiB\n", gd->ram_size >> 20);
297
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800298 /* Enable or disable the SDRAM ECC */
299 if (CTRLCFG1_CFG_CTRL_EN_ECC(ctrlcfg1)) {
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800300 setbits_le32(plat->hmc + ECCCTRL1,
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800301 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
302 DDR_HMC_ECCCTL_CNT_RST_SET_MSK |
303 DDR_HMC_ECCCTL_ECC_EN_SET_MSK));
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800304 clrbits_le32(plat->hmc + ECCCTRL1,
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800305 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
306 DDR_HMC_ECCCTL_CNT_RST_SET_MSK));
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800307 setbits_le32(plat->hmc + ECCCTRL2,
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800308 (DDR_HMC_ECCCTL2_RMW_EN_SET_MSK |
309 DDR_HMC_ECCCTL2_AWB_EN_SET_MSK));
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800310 hmc_ecc_writel(plat, DDR_HMC_ERRINTEN_INTMASK, ERRINTENS);
Ley Foon Tan456d4522019-03-22 01:24:05 +0800311
Ley Foon Tan456d4522019-03-22 01:24:05 +0800312 /* Initialize memory content if not from warm reset */
313 if (!cpu_has_been_warmreset())
314 sdram_init_ecc_bits(&bd);
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800315 } else {
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800316 clrbits_le32(plat->hmc + ECCCTRL1,
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800317 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
318 DDR_HMC_ECCCTL_CNT_RST_SET_MSK |
319 DDR_HMC_ECCCTL_ECC_EN_SET_MSK));
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800320 clrbits_le32(plat->hmc + ECCCTRL2,
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800321 (DDR_HMC_ECCCTL2_RMW_EN_SET_MSK |
322 DDR_HMC_ECCCTL2_AWB_EN_SET_MSK));
323 }
324
Thor Thayer8097aee2019-12-06 13:47:32 -0600325 /* Enable non-secure reads/writes to HMC Adapter for SDRAM ECC */
326 writel(FW_HMC_ADAPTOR_MPU_MASK, FW_HMC_ADAPTOR_REG_ADDR);
327
Ley Foon Tan6cd71342019-03-22 01:24:01 +0800328 sdram_size_check(&bd);
Ley Foon Tanb6f7ee52019-03-22 01:24:00 +0800329
Ley Foon Tan6bf238a2019-05-06 09:56:01 +0800330 priv->info.base = bd.bi_dram[0].start;
331 priv->info.size = gd->ram_size;
332
Ley Foon Tan0bc28b72018-05-24 00:17:30 +0800333 debug("DDR: HMC init success\n");
334 return 0;
335}
336