blob: 0cbcd14056070de04efd57978f2a398526b11f97 [file] [log] [blame]
Ley Foon Tan4ddb9092019-11-27 15:55:27 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2019 Intel Corporation <www.intel.com>
4 *
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <div64.h>
11#include <fdtdec.h>
12#include <ram.h>
13#include <reset.h>
14#include "sdram_soc64.h"
15#include <wait_bit.h>
16#include <asm/arch/firewall.h>
17#include <asm/arch/reset_manager.h>
18#include <asm/arch/system_manager.h>
19#include <asm/io.h>
20#include <linux/sizes.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24int sdram_mmr_init_full(struct udevice *dev)
25{
26 struct altera_sdram_platdata *plat = dev->platdata;
27 struct altera_sdram_priv *priv = dev_get_priv(dev);
28 u32 i;
29 int ret;
30 phys_size_t hw_size;
31 bd_t bd = {0};
32
33 /* Ensure HMC clock is running */
34 if (poll_hmc_clock_status()) {
35 debug("DDR: Error as HMC clock was not running\n");
36 return -EPERM;
37 }
38
39 /* Trying 3 times to do a calibration */
40 for (i = 0; i < 3; i++) {
41 ret = wait_for_bit_le32((const void *)(plat->hmc +
42 DDRCALSTAT),
43 DDR_HMC_DDRCALSTAT_CAL_MSK, true, 1000,
44 false);
45 if (!ret)
46 break;
47
48 emif_reset(plat);
49 }
50
51 if (ret) {
52 puts("DDR: Error as SDRAM calibration failed\n");
53 return -EPERM;
54 }
55 debug("DDR: Calibration success\n");
56
57 /*
58 * Configure the DDR IO size
59 * niosreserve0: Used to indicate DDR width &
60 * bit[7:0] = Number of data bits (bit[6:5] 0x01=32bit, 0x10=64bit)
61 * bit[8] = 1 if user-mode OCT is present
62 * bit[9] = 1 if warm reset compiled into EMIF Cal Code
63 * bit[10] = 1 if warm reset is on during generation in EMIF Cal
64 * niosreserve1: IP ADCDS version encoded as 16 bit value
65 * bit[2:0] = Variant (0=not special,1=FAE beta, 2=Customer beta,
66 * 3=EAP, 4-6 are reserved)
67 * bit[5:3] = Service Pack # (e.g. 1)
68 * bit[9:6] = Minor Release #
69 * bit[14:10] = Major Release #
70 */
71 /* Configure DDR IO size x16, x32 and x64 mode */
72 u32 update_value;
73
74 update_value = hmc_readl(plat, NIOSRESERVED0);
75 update_value = (update_value & 0xFF) >> 5;
76
77 /* Configure DDR data rate 0-HAlf-rate 1-Quarter-rate */
78 update_value |= (hmc_readl(plat, CTRLCFG3) & 0x4);
79 hmc_ecc_writel(plat, update_value, DDRIOCTRL);
80
81 /* Copy values MMR IOHMC dramaddrw to HMC adp DRAMADDRWIDTH */
82 hmc_ecc_writel(plat, hmc_readl(plat, DRAMADDRW), DRAMADDRWIDTH);
83
84 /* assigning the SDRAM size */
85 phys_size_t size = sdram_calculate_size(plat);
86
87 if (size <= 0)
88 hw_size = PHYS_SDRAM_1_SIZE;
89 else
90 hw_size = size;
91
92 /* Get bank configuration from devicetree */
93 ret = fdtdec_decode_ram_size(gd->fdt_blob, NULL, 0, NULL,
94 (phys_size_t *)&gd->ram_size, &bd);
95 if (ret) {
96 puts("DDR: Failed to decode memory node\n");
97 return -ENXIO;
98 }
99
100 if (gd->ram_size != hw_size) {
101 printf("DDR: Warning: DRAM size from device tree (%lld MiB)\n",
102 gd->ram_size >> 20);
103 printf(" mismatch with hardware (%lld MiB).\n",
104 hw_size >> 20);
105 }
106
107 if (gd->ram_size > hw_size) {
108 printf("DDR: Error: DRAM size from device tree is greater\n");
109 printf(" than hardware size.\n");
110 hang();
111 }
112
113 printf("DDR: %lld MiB\n", gd->ram_size >> 20);
114
115 /* This enables nonsecure access to DDR */
116 /* mpuregion0addr_limit */
117 FW_MPU_DDR_SCR_WRITEL(gd->ram_size - 1,
118 FW_MPU_DDR_SCR_MPUREGION0ADDR_LIMIT);
119 FW_MPU_DDR_SCR_WRITEL(0x1F, FW_MPU_DDR_SCR_MPUREGION0ADDR_LIMITEXT);
120
121 /* nonmpuregion0addr_limit */
122 FW_MPU_DDR_SCR_WRITEL(gd->ram_size - 1,
123 FW_MPU_DDR_SCR_NONMPUREGION0ADDR_LIMIT);
124
125 /* Enable mpuregion0enable and nonmpuregion0enable */
126 FW_MPU_DDR_SCR_WRITEL(MPUREGION0_ENABLE | NONMPUREGION0_ENABLE,
127 FW_MPU_DDR_SCR_EN_SET);
128
129 u32 ctrlcfg1 = hmc_readl(plat, CTRLCFG1);
130
131 /* Enable or disable the DDR ECC */
132 if (CTRLCFG1_CFG_CTRL_EN_ECC(ctrlcfg1)) {
133 setbits_le32(plat->hmc + ECCCTRL1,
134 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
135 DDR_HMC_ECCCTL_CNT_RST_SET_MSK |
136 DDR_HMC_ECCCTL_ECC_EN_SET_MSK));
137 clrbits_le32(plat->hmc + ECCCTRL1,
138 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
139 DDR_HMC_ECCCTL_CNT_RST_SET_MSK));
140 setbits_le32(plat->hmc + ECCCTRL2,
141 (DDR_HMC_ECCCTL2_RMW_EN_SET_MSK |
142 DDR_HMC_ECCCTL2_AWB_EN_SET_MSK));
143 setbits_le32(plat->hmc + ERRINTEN,
144 DDR_HMC_ERRINTEN_DERRINTEN_EN_SET_MSK);
145
Ley Foon Tan4ddb9092019-11-27 15:55:27 +0800146 if (!cpu_has_been_warmreset())
147 sdram_init_ecc_bits(&bd);
148 } else {
149 clrbits_le32(plat->hmc + ECCCTRL1,
150 (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
151 DDR_HMC_ECCCTL_CNT_RST_SET_MSK |
152 DDR_HMC_ECCCTL_ECC_EN_SET_MSK));
153 clrbits_le32(plat->hmc + ECCCTRL2,
154 (DDR_HMC_ECCCTL2_RMW_EN_SET_MSK |
155 DDR_HMC_ECCCTL2_AWB_EN_SET_MSK));
156 }
157
Thor Thayer7ead4212019-12-06 13:47:32 -0600158 /* Enable non-secure reads/writes to HMC Adapter for SDRAM ECC */
159 writel(FW_HMC_ADAPTOR_MPU_MASK, FW_HMC_ADAPTOR_REG_ADDR);
160
Ley Foon Tan4ddb9092019-11-27 15:55:27 +0800161 sdram_size_check(&bd);
162
163 priv->info.base = bd.bi_dram[0].start;
164 priv->info.size = gd->ram_size;
165
166 debug("DDR: HMC init success\n");
167 return 0;
168}