blob: b709758db7c40eee4edff3862d55460c3ec8a655 [file] [log] [blame]
Ley Foon Tanf9c7f792018-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 Glass1d91ba72019-11-14 12:57:37 -07008#include <cpu_func.h>
Ley Foon Tan3fdf4362019-05-06 09:56:01 +08009#include <dm.h>
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080010#include <errno.h>
11#include <div64.h>
Ley Foon Tan4bbed7b2019-03-22 01:24:01 +080012#include <fdtdec.h>
Ley Foon Tan3fdf4362019-05-06 09:56:01 +080013#include <ram.h>
14#include <reset.h>
15#include "sdram_s10.h"
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080016#include <wait_bit.h>
Ley Foon Tanf1c4bd52019-11-27 15:55:15 +080017#include <asm/arch/firewall.h>
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080018#include <asm/arch/reset_manager.h>
Ley Foon Tan3fdf4362019-05-06 09:56:01 +080019#include <asm/io.h>
Ley Foon Tan4bbed7b2019-03-22 01:24:01 +080020#include <linux/sizes.h>
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080021
22DECLARE_GLOBAL_DATA_PTR;
23
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080024#define DDR_CONFIG(A, B, C, R) (((A) << 24) | ((B) << 16) | ((C) << 8) | (R))
25
26/* The followring are the supported configurations */
27u32 ddr_config[] = {
28 /* DDR_CONFIG(Address order,Bank,Column,Row) */
29 /* List for DDR3 or LPDDR3 (pinout order > chip, row, bank, column) */
30 DDR_CONFIG(0, 3, 10, 12),
31 DDR_CONFIG(0, 3, 9, 13),
32 DDR_CONFIG(0, 3, 10, 13),
33 DDR_CONFIG(0, 3, 9, 14),
34 DDR_CONFIG(0, 3, 10, 14),
35 DDR_CONFIG(0, 3, 10, 15),
36 DDR_CONFIG(0, 3, 11, 14),
37 DDR_CONFIG(0, 3, 11, 15),
38 DDR_CONFIG(0, 3, 10, 16),
39 DDR_CONFIG(0, 3, 11, 16),
40 DDR_CONFIG(0, 3, 12, 15), /* 0xa */
41 /* List for DDR4 only (pinout order > chip, bank, row, column) */
42 DDR_CONFIG(1, 3, 10, 14),
43 DDR_CONFIG(1, 4, 10, 14),
44 DDR_CONFIG(1, 3, 10, 15),
45 DDR_CONFIG(1, 4, 10, 15),
46 DDR_CONFIG(1, 3, 10, 16),
47 DDR_CONFIG(1, 4, 10, 16),
48 DDR_CONFIG(1, 3, 10, 17),
49 DDR_CONFIG(1, 4, 10, 17),
50};
51
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080052int match_ddr_conf(u32 ddr_conf)
53{
54 int i;
55
56 for (i = 0; i < ARRAY_SIZE(ddr_config); i++) {
57 if (ddr_conf == ddr_config[i])
58 return i;
59 }
60 return 0;
61}
62
Ley Foon Tan3fdf4362019-05-06 09:56:01 +080063/**
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080064 * sdram_mmr_init_full() - Function to initialize SDRAM MMR
65 *
66 * Initialize the SDRAM MMR.
67 */
Ley Foon Tan25572cf2019-11-27 15:55:26 +080068int sdram_mmr_init_full(struct udevice *dev)
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080069{
Ley Foon Tan3fdf4362019-05-06 09:56:01 +080070 struct altera_sdram_platdata *plat = dev->platdata;
71 struct altera_sdram_priv *priv = dev_get_priv(dev);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080072 u32 update_value, io48_value, ddrioctl;
73 u32 i;
74 int ret;
Ley Foon Tan4bbed7b2019-03-22 01:24:01 +080075 phys_size_t hw_size;
76 bd_t bd = {0};
Ley Foon Tanf9c7f792018-05-24 00:17:30 +080077
78 /* Enable access to DDR from CPU master */
79 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_DDRREG),
80 CCU_ADBASE_DI_MASK);
81 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE0),
82 CCU_ADBASE_DI_MASK);
83 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1A),
84 CCU_ADBASE_DI_MASK);
85 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1B),
86 CCU_ADBASE_DI_MASK);
87 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1C),
88 CCU_ADBASE_DI_MASK);
89 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1D),
90 CCU_ADBASE_DI_MASK);
91 clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1E),
92 CCU_ADBASE_DI_MASK);
93
94 /* Enable access to DDR from IO master */
95 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE0),
96 CCU_ADBASE_DI_MASK);
97 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1A),
98 CCU_ADBASE_DI_MASK);
99 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1B),
100 CCU_ADBASE_DI_MASK);
101 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1C),
102 CCU_ADBASE_DI_MASK);
103 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1D),
104 CCU_ADBASE_DI_MASK);
105 clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1E),
106 CCU_ADBASE_DI_MASK);
107
108 /* this enables nonsecure access to DDR */
109 /* mpuregion0addr_limit */
110 FW_MPU_DDR_SCR_WRITEL(0xFFFF0000, FW_MPU_DDR_SCR_MPUREGION0ADDR_LIMIT);
111 FW_MPU_DDR_SCR_WRITEL(0x1F, FW_MPU_DDR_SCR_MPUREGION0ADDR_LIMITEXT);
112
113 /* nonmpuregion0addr_limit */
114 FW_MPU_DDR_SCR_WRITEL(0xFFFF0000,
115 FW_MPU_DDR_SCR_NONMPUREGION0ADDR_LIMIT);
116 FW_MPU_DDR_SCR_WRITEL(0x1F, FW_MPU_DDR_SCR_NONMPUREGION0ADDR_LIMITEXT);
117
118 /* Enable mpuregion0enable and nonmpuregion0enable */
119 FW_MPU_DDR_SCR_WRITEL(MPUREGION0_ENABLE | NONMPUREGION0_ENABLE,
120 FW_MPU_DDR_SCR_EN_SET);
121
122 /* Ensure HMC clock is running */
123 if (poll_hmc_clock_status()) {
124 puts("DDR: Error as HMC clock not running\n");
125 return -1;
126 }
127
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800128 /* Try 3 times to do a calibration */
129 for (i = 0; i < 3; i++) {
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800130 ret = wait_for_bit_le32((const void *)(plat->hmc +
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800131 DDRCALSTAT),
132 DDR_HMC_DDRCALSTAT_CAL_MSK, true, 1000,
133 false);
134 if (!ret)
135 break;
136
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800137 emif_reset(plat);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800138 }
139
140 if (ret) {
141 puts("DDR: Error as SDRAM calibration failed\n");
142 return -1;
143 }
144 debug("DDR: Calibration success\n");
145
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800146 u32 ctrlcfg0 = hmc_readl(plat, CTRLCFG0);
147 u32 ctrlcfg1 = hmc_readl(plat, CTRLCFG1);
148 u32 dramaddrw = hmc_readl(plat, DRAMADDRW);
149 u32 dramtim0 = hmc_readl(plat, DRAMTIMING0);
150 u32 caltim0 = hmc_readl(plat, CALTIMING0);
151 u32 caltim1 = hmc_readl(plat, CALTIMING1);
152 u32 caltim2 = hmc_readl(plat, CALTIMING2);
153 u32 caltim3 = hmc_readl(plat, CALTIMING3);
154 u32 caltim4 = hmc_readl(plat, CALTIMING4);
155 u32 caltim9 = hmc_readl(plat, CALTIMING9);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800156
157 /*
158 * Configure the DDR IO size [0xFFCFB008]
159 * niosreserve0: Used to indicate DDR width &
160 * bit[7:0] = Number of data bits (bit[6:5] 0x01=32bit, 0x10=64bit)
161 * bit[8] = 1 if user-mode OCT is present
162 * bit[9] = 1 if warm reset compiled into EMIF Cal Code
163 * bit[10] = 1 if warm reset is on during generation in EMIF Cal
164 * niosreserve1: IP ADCDS version encoded as 16 bit value
165 * bit[2:0] = Variant (0=not special,1=FAE beta, 2=Customer beta,
166 * 3=EAP, 4-6 are reserved)
167 * bit[5:3] = Service Pack # (e.g. 1)
168 * bit[9:6] = Minor Release #
169 * bit[14:10] = Major Release #
170 */
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800171 update_value = hmc_readl(plat, NIOSRESERVED0);
172 hmc_ecc_writel(plat, ((update_value & 0xFF) >> 5), DDRIOCTRL);
173 ddrioctl = hmc_ecc_readl(plat, DDRIOCTRL);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800174
175 /* enable HPS interface to HMC */
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800176 hmc_ecc_writel(plat, DDR_HMC_HPSINTFCSEL_ENABLE_MASK, HPSINTFCSEL);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800177
178 /* Set the DDR Configuration */
179 io48_value = DDR_CONFIG(CTRLCFG1_CFG_ADDR_ORDER(ctrlcfg1),
180 (DRAMADDRW_CFG_BANK_ADDR_WIDTH(dramaddrw) +
181 DRAMADDRW_CFG_BANK_GRP_ADDR_WIDTH(dramaddrw)),
182 DRAMADDRW_CFG_COL_ADDR_WIDTH(dramaddrw),
183 DRAMADDRW_CFG_ROW_ADDR_WIDTH(dramaddrw));
184
185 update_value = match_ddr_conf(io48_value);
186 if (update_value)
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800187 ddr_sch_writel(plat, update_value, DDR_SCH_DDRCONF);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800188
189 /* Configure HMC dramaddrw */
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800190 hmc_ecc_writel(plat, hmc_readl(plat, DRAMADDRW), DRAMADDRWIDTH);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800191
192 /*
193 * Configure DDR timing
194 * RDTOMISS = tRTP + tRP + tRCD - BL/2
195 * WRTOMISS = WL + tWR + tRP + tRCD and
196 * WL = RL + BL/2 + 2 - rd-to-wr ; tWR = 15ns so...
197 * First part of equation is in memory clock units so divide by 2
198 * for HMC clock units. 1066MHz is close to 1ns so use 15 directly.
199 * WRTOMISS = ((RL + BL/2 + 2 + tWR) >> 1)- rd-to-wr + tRP + tRCD
200 */
201 u32 burst_len = CTRLCFG0_CFG_CTRL_BURST_LEN(ctrlcfg0);
202
203 update_value = CALTIMING2_CFG_RD_TO_WR_PCH(caltim2) +
204 CALTIMING4_CFG_PCH_TO_VALID(caltim4) +
205 CALTIMING0_CFG_ACT_TO_RDWR(caltim0) -
206 (burst_len >> 2);
207 io48_value = (((DRAMTIMING0_CFG_TCL(dramtim0) + 2 + DDR_TWR +
208 (burst_len >> 1)) >> 1) -
209 /* Up to here was in memory cycles so divide by 2 */
210 CALTIMING1_CFG_RD_TO_WR(caltim1) +
211 CALTIMING0_CFG_ACT_TO_RDWR(caltim0) +
212 CALTIMING4_CFG_PCH_TO_VALID(caltim4));
213
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800214 ddr_sch_writel(plat, ((CALTIMING0_CFG_ACT_TO_ACT(caltim0) <<
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800215 DDR_SCH_DDRTIMING_ACTTOACT_OFF) |
216 (update_value << DDR_SCH_DDRTIMING_RDTOMISS_OFF) |
217 (io48_value << DDR_SCH_DDRTIMING_WRTOMISS_OFF) |
218 ((burst_len >> 2) << DDR_SCH_DDRTIMING_BURSTLEN_OFF) |
219 (CALTIMING1_CFG_RD_TO_WR(caltim1) <<
220 DDR_SCH_DDRTIMING_RDTOWR_OFF) |
221 (CALTIMING3_CFG_WR_TO_RD(caltim3) <<
222 DDR_SCH_DDRTIMING_WRTORD_OFF) |
223 (((ddrioctl == 1) ? 1 : 0) <<
224 DDR_SCH_DDRTIMING_BWRATIO_OFF)),
225 DDR_SCH_DDRTIMING);
226
227 /* Configure DDR mode [precharge = 0] */
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800228 ddr_sch_writel(plat, ((ddrioctl ? 0 : 1) <<
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800229 DDR_SCH_DDRMOD_BWRATIOEXTENDED_OFF),
230 DDR_SCH_DDRMODE);
231
232 /* Configure the read latency */
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800233 ddr_sch_writel(plat, (DRAMTIMING0_CFG_TCL(dramtim0) >> 1) +
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800234 DDR_READ_LATENCY_DELAY,
235 DDR_SCH_READ_LATENCY);
236
237 /*
238 * Configuring timing values concerning activate commands
239 * [FAWBANK alway 1 because always 4 bank DDR]
240 */
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800241 ddr_sch_writel(plat, ((CALTIMING0_CFG_ACT_TO_ACT_DB(caltim0) <<
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800242 DDR_SCH_ACTIVATE_RRD_OFF) |
243 (CALTIMING9_CFG_4_ACT_TO_ACT(caltim9) <<
244 DDR_SCH_ACTIVATE_FAW_OFF) |
245 (DDR_ACTIVATE_FAWBANK <<
246 DDR_SCH_ACTIVATE_FAWBANK_OFF)),
247 DDR_SCH_ACTIVATE);
248
249 /*
250 * Configuring timing values concerning device to device data bus
251 * ownership change
252 */
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800253 ddr_sch_writel(plat, ((CALTIMING1_CFG_RD_TO_RD_DC(caltim1) <<
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800254 DDR_SCH_DEVTODEV_BUSRDTORD_OFF) |
255 (CALTIMING1_CFG_RD_TO_WR_DC(caltim1) <<
256 DDR_SCH_DEVTODEV_BUSRDTOWR_OFF) |
257 (CALTIMING3_CFG_WR_TO_RD_DC(caltim3) <<
258 DDR_SCH_DEVTODEV_BUSWRTORD_OFF)),
259 DDR_SCH_DEVTODEV);
260
261 /* assigning the SDRAM size */
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800262 unsigned long long size = sdram_calculate_size(plat);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800263 /* If the size is invalid, use default Config size */
264 if (size <= 0)
Ley Foon Tan4bbed7b2019-03-22 01:24:01 +0800265 hw_size = PHYS_SDRAM_1_SIZE;
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800266 else
Ley Foon Tan4bbed7b2019-03-22 01:24:01 +0800267 hw_size = size;
268
269 /* Get bank configuration from devicetree */
270 ret = fdtdec_decode_ram_size(gd->fdt_blob, NULL, 0, NULL,
271 (phys_size_t *)&gd->ram_size, &bd);
272 if (ret) {
273 puts("DDR: Failed to decode memory node\n");
274 return -1;
275 }
276
277 if (gd->ram_size != hw_size)
278 printf("DDR: Warning: DRAM size from device tree mismatch with hardware.\n");
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800279
Ley Foon Tana9245d12019-03-22 01:24:00 +0800280 printf("DDR: %lld MiB\n", gd->ram_size >> 20);
281
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800282 /* Enable or disable the SDRAM ECC */
283 if (CTRLCFG1_CFG_CTRL_EN_ECC(ctrlcfg1)) {
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800284 setbits_le32(plat->hmc + ECCCTRL1,
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800285 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
286 DDR_HMC_ECCCTL_CNT_RST_SET_MSK |
287 DDR_HMC_ECCCTL_ECC_EN_SET_MSK));
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800288 clrbits_le32(plat->hmc + ECCCTRL1,
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800289 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
290 DDR_HMC_ECCCTL_CNT_RST_SET_MSK));
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800291 setbits_le32(plat->hmc + ECCCTRL2,
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800292 (DDR_HMC_ECCCTL2_RMW_EN_SET_MSK |
293 DDR_HMC_ECCCTL2_AWB_EN_SET_MSK));
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800294 hmc_ecc_writel(plat, DDR_HMC_ERRINTEN_INTMASK, ERRINTENS);
Ley Foon Tan9799a672019-03-22 01:24:05 +0800295
296 /* Enable non-secure writes to HMC Adapter for SDRAM ECC */
297 writel(FW_HMC_ADAPTOR_MPU_MASK, FW_HMC_ADAPTOR_REG_ADDR);
298
299 /* Initialize memory content if not from warm reset */
300 if (!cpu_has_been_warmreset())
301 sdram_init_ecc_bits(&bd);
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800302 } else {
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800303 clrbits_le32(plat->hmc + ECCCTRL1,
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800304 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
305 DDR_HMC_ECCCTL_CNT_RST_SET_MSK |
306 DDR_HMC_ECCCTL_ECC_EN_SET_MSK));
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800307 clrbits_le32(plat->hmc + ECCCTRL2,
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800308 (DDR_HMC_ECCCTL2_RMW_EN_SET_MSK |
309 DDR_HMC_ECCCTL2_AWB_EN_SET_MSK));
310 }
311
Ley Foon Tan4bbed7b2019-03-22 01:24:01 +0800312 sdram_size_check(&bd);
Ley Foon Tana9245d12019-03-22 01:24:00 +0800313
Ley Foon Tan3fdf4362019-05-06 09:56:01 +0800314 priv->info.base = bd.bi_dram[0].start;
315 priv->info.size = gd->ram_size;
316
Ley Foon Tanf9c7f792018-05-24 00:17:30 +0800317 debug("DDR: HMC init success\n");
318 return 0;
319}
320