blob: bd2f438d7271be2a2eb98af8142a2d4f40b2ca94 [file] [log] [blame]
Pragnesh Patel4cefe722020-05-29 11:33:26 +05301// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
Green Wana5936a22021-05-27 06:52:09 -07003 * (C) Copyright 2020-2021 SiFive, Inc.
Pragnesh Patel4cefe722020-05-29 11:33:26 +05304 *
5 * Authors:
6 * Pragnesh Patel <pragnesh.patel@sifive.com>
7 */
8
Pragnesh Patel4cefe722020-05-29 11:33:26 +05309#include <dm.h>
Bin Mengca65e242020-07-19 23:06:35 -070010#include <fdtdec.h>
Pragnesh Patel4cefe722020-05-29 11:33:26 +053011#include <init.h>
12#include <ram.h>
Pragnesh Patel4cefe722020-05-29 11:33:26 +053013#include <syscon.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Pragnesh Patel4cefe722020-05-29 11:33:26 +053015#include <asm/io.h>
16#include <clk.h>
17#include <wait_bit.h>
18#include <linux/bitops.h>
19
20#define DENALI_CTL_0 0
21#define DENALI_CTL_21 21
22#define DENALI_CTL_120 120
23#define DENALI_CTL_132 132
24#define DENALI_CTL_136 136
25#define DENALI_CTL_170 170
26#define DENALI_CTL_181 181
27#define DENALI_CTL_182 182
28#define DENALI_CTL_184 184
29#define DENALI_CTL_208 208
30#define DENALI_CTL_209 209
31#define DENALI_CTL_210 210
32#define DENALI_CTL_212 212
33#define DENALI_CTL_214 214
34#define DENALI_CTL_216 216
35#define DENALI_CTL_224 224
36#define DENALI_CTL_225 225
37#define DENALI_CTL_260 260
38
39#define DENALI_PHY_1152 1152
40#define DENALI_PHY_1214 1214
41
Pragnesh Patel4cefe722020-05-29 11:33:26 +053042#define DRAM_CLASS_OFFSET 8
43#define DRAM_CLASS_DDR4 0xA
44#define OPTIMAL_RMODW_EN_OFFSET 0
45#define DISABLE_RD_INTERLEAVE_OFFSET 16
46#define OUT_OF_RANGE_OFFSET 1
47#define MULTIPLE_OUT_OF_RANGE_OFFSET 2
48#define PORT_COMMAND_CHANNEL_ERROR_OFFSET 7
49#define MC_INIT_COMPLETE_OFFSET 8
50#define LEVELING_OPERATION_COMPLETED_OFFSET 22
51#define DFI_PHY_WRLELV_MODE_OFFSET 24
52#define DFI_PHY_RDLVL_MODE_OFFSET 24
53#define DFI_PHY_RDLVL_GATE_MODE_OFFSET 0
54#define VREF_EN_OFFSET 24
55#define PORT_ADDR_PROTECTION_EN_OFFSET 0
56#define AXI0_ADDRESS_RANGE_ENABLE 8
57#define AXI0_RANGE_PROT_BITS_0_OFFSET 24
58#define RDLVL_EN_OFFSET 16
59#define RDLVL_GATE_EN_OFFSET 24
60#define WRLVL_EN_OFFSET 0
61
62#define PHY_RX_CAL_DQ0_0_OFFSET 0
63#define PHY_RX_CAL_DQ1_0_OFFSET 16
64
Bin Mengca65e242020-07-19 23:06:35 -070065DECLARE_GLOBAL_DATA_PTR;
66
Green Wana5936a22021-05-27 06:52:09 -070067struct sifive_ddrctl {
Pragnesh Patel4cefe722020-05-29 11:33:26 +053068 volatile u32 denali_ctl[265];
69};
70
Green Wana5936a22021-05-27 06:52:09 -070071struct sifive_ddrphy {
Pragnesh Patel4cefe722020-05-29 11:33:26 +053072 volatile u32 denali_phy[1215];
73};
74
75/**
Green Wana5936a22021-05-27 06:52:09 -070076 * struct sifive_ddr_info
Pragnesh Patel4cefe722020-05-29 11:33:26 +053077 *
78 * @dev : pointer for the device
79 * @info : UCLASS RAM information
80 * @ctl : DDR controller base address
81 * @phy : DDR PHY base address
82 * @ctrl : DDR control base address
83 * @physical_filter_ctrl : DDR physical filter control base address
84 */
Green Wana5936a22021-05-27 06:52:09 -070085struct sifive_ddr_info {
Pragnesh Patel4cefe722020-05-29 11:33:26 +053086 struct udevice *dev;
87 struct ram_info info;
Green Wana5936a22021-05-27 06:52:09 -070088 struct sifive_ddrctl *ctl;
89 struct sifive_ddrphy *phy;
Pragnesh Patel4cefe722020-05-29 11:33:26 +053090 struct clk ddr_clk;
91 u32 *physical_filter_ctrl;
92};
93
94#if defined(CONFIG_SPL_BUILD)
Green Wana5936a22021-05-27 06:52:09 -070095struct sifive_ddr_params {
96 struct sifive_ddrctl pctl_regs;
97 struct sifive_ddrphy phy_regs;
Pragnesh Patel4cefe722020-05-29 11:33:26 +053098};
99
100struct sifive_dmc_plat {
Green Wana5936a22021-05-27 06:52:09 -0700101 struct sifive_ddr_params ddr_params;
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530102};
103
104/*
105 * TODO : It can be possible to use common sdram_copy_to_reg() API
106 * n: Unit bytes
107 */
108static void sdram_copy_to_reg(volatile u32 *dest,
109 volatile u32 *src, u32 n)
110{
111 int i;
112
113 for (i = 0; i < n / sizeof(u32); i++) {
114 writel(*src, dest);
115 src++;
116 dest++;
117 }
118}
119
Green Wana5936a22021-05-27 06:52:09 -0700120static void sifive_ddr_setup_range_protection(volatile u32 *ctl, u64 end_addr)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530121{
122 u32 end_addr_16kblocks = ((end_addr >> 14) & 0x7FFFFF) - 1;
123
124 writel(0x0, DENALI_CTL_209 + ctl);
125 writel(end_addr_16kblocks, DENALI_CTL_210 + ctl);
126 writel(0x0, DENALI_CTL_212 + ctl);
127 writel(0x0, DENALI_CTL_214 + ctl);
128 writel(0x0, DENALI_CTL_216 + ctl);
129 setbits_le32(DENALI_CTL_224 + ctl,
130 0x3 << AXI0_RANGE_PROT_BITS_0_OFFSET);
131 writel(0xFFFFFFFF, DENALI_CTL_225 + ctl);
132 setbits_le32(DENALI_CTL_208 + ctl, 0x1 << AXI0_ADDRESS_RANGE_ENABLE);
133 setbits_le32(DENALI_CTL_208 + ctl,
134 0x1 << PORT_ADDR_PROTECTION_EN_OFFSET);
135}
136
Green Wana5936a22021-05-27 06:52:09 -0700137static void sifive_ddr_start(volatile u32 *ctl, u32 *physical_filter_ctrl,
138 u64 ddr_end)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530139{
140 volatile u64 *filterreg = (volatile u64 *)physical_filter_ctrl;
141
142 setbits_le32(DENALI_CTL_0 + ctl, 0x1);
143
144 wait_for_bit_le32((void *)ctl + DENALI_CTL_132,
145 BIT(MC_INIT_COMPLETE_OFFSET), false, 100, false);
146
147 /* Disable the BusBlocker in front of the controller AXI slave ports */
148 filterreg[0] = 0x0f00000000000000UL | (ddr_end >> 2);
149}
150
Green Wana5936a22021-05-27 06:52:09 -0700151static void sifive_ddr_check_errata(u32 regbase, u32 updownreg)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530152{
153 u64 fails = 0;
154 u32 dq = 0;
155 u32 down, up;
156 u8 failc0, failc1;
157 u32 phy_rx_cal_dqn_0_offset;
158
159 for (u32 bit = 0; bit < 2; bit++) {
160 if (bit == 0) {
161 phy_rx_cal_dqn_0_offset =
162 PHY_RX_CAL_DQ0_0_OFFSET;
163 } else {
164 phy_rx_cal_dqn_0_offset =
165 PHY_RX_CAL_DQ1_0_OFFSET;
166 }
167
168 down = (updownreg >>
169 phy_rx_cal_dqn_0_offset) & 0x3F;
170 up = (updownreg >>
171 (phy_rx_cal_dqn_0_offset + 6)) &
172 0x3F;
173
174 failc0 = ((down == 0) && (up == 0x3F));
175 failc1 = ((up == 0) && (down == 0x3F));
176
177 /* print error message on failure */
178 if (failc0 || failc1) {
179 if (fails == 0)
180 printf("DDR error in fixing up\n");
181
182 fails |= (1 << dq);
183
184 char slicelsc = '0';
185 char slicemsc = '0';
186
187 slicelsc += (dq % 10);
188 slicemsc += (dq / 10);
189 printf("S ");
190 printf("%c", slicemsc);
191 printf("%c", slicelsc);
192
193 if (failc0)
194 printf("U");
195 else
196 printf("D");
197
198 printf("\n");
199 }
200 dq++;
201 }
202}
203
Green Wana5936a22021-05-27 06:52:09 -0700204static u64 sifive_ddr_phy_fixup(volatile u32 *ddrphyreg)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530205{
206 u32 slicebase = 0;
207
208 /* check errata condition */
209 for (u32 slice = 0; slice < 8; slice++) {
210 u32 regbase = slicebase + 34;
211
212 for (u32 reg = 0; reg < 4; reg++) {
213 u32 updownreg = readl(regbase + reg + ddrphyreg);
214
Green Wana5936a22021-05-27 06:52:09 -0700215 sifive_ddr_check_errata(regbase, updownreg);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530216 }
217 slicebase += 128;
218 }
219
220 return(0);
221}
222
Green Wana5936a22021-05-27 06:52:09 -0700223static u32 sifive_ddr_get_dram_class(volatile u32 *ctl)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530224{
225 u32 reg = readl(DENALI_CTL_0 + ctl);
226
227 return ((reg >> DRAM_CLASS_OFFSET) & 0xF);
228}
229
Green Wana5936a22021-05-27 06:52:09 -0700230static int sifive_ddr_setup(struct udevice *dev)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530231{
Green Wana5936a22021-05-27 06:52:09 -0700232 struct sifive_ddr_info *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700233 struct sifive_dmc_plat *plat = dev_get_plat(dev);
Green Wana5936a22021-05-27 06:52:09 -0700234 struct sifive_ddr_params *params = &plat->ddr_params;
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530235 volatile u32 *denali_ctl = priv->ctl->denali_ctl;
236 volatile u32 *denali_phy = priv->phy->denali_phy;
Bin Mengca65e242020-07-19 23:06:35 -0700237 const u64 ddr_size = priv->info.size;
238 const u64 ddr_end = priv->info.base + ddr_size;
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530239 int ret, i;
240 u32 physet;
241
242 ret = dev_read_u32_array(dev, "sifive,ddr-params",
243 (u32 *)&plat->ddr_params,
244 sizeof(plat->ddr_params) / sizeof(u32));
245 if (ret) {
246 printf("%s: Cannot read sifive,ddr-params %d\n",
247 __func__, ret);
248 return ret;
249 }
250
251 sdram_copy_to_reg(priv->ctl->denali_ctl,
252 params->pctl_regs.denali_ctl,
Green Wana5936a22021-05-27 06:52:09 -0700253 sizeof(struct sifive_ddrctl));
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530254
255 /* phy reset */
256 for (i = DENALI_PHY_1152; i <= DENALI_PHY_1214; i++) {
257 physet = params->phy_regs.denali_phy[i];
258 priv->phy->denali_phy[i] = physet;
259 }
260
261 for (i = 0; i < DENALI_PHY_1152; i++) {
262 physet = params->phy_regs.denali_phy[i];
263 priv->phy->denali_phy[i] = physet;
264 }
265
266 /* Disable read interleave DENALI_CTL_120 */
267 setbits_le32(DENALI_CTL_120 + denali_ctl,
268 1 << DISABLE_RD_INTERLEAVE_OFFSET);
269
270 /* Disable optimal read/modify/write logic DENALI_CTL_21 */
271 clrbits_le32(DENALI_CTL_21 + denali_ctl, 1 << OPTIMAL_RMODW_EN_OFFSET);
272
273 /* Enable write Leveling DENALI_CTL_170 */
274 setbits_le32(DENALI_CTL_170 + denali_ctl, (1 << WRLVL_EN_OFFSET)
275 | (1 << DFI_PHY_WRLELV_MODE_OFFSET));
276
277 /* Enable read leveling DENALI_CTL_181 and DENALI_CTL_260 */
278 setbits_le32(DENALI_CTL_181 + denali_ctl,
279 1 << DFI_PHY_RDLVL_MODE_OFFSET);
280 setbits_le32(DENALI_CTL_260 + denali_ctl, 1 << RDLVL_EN_OFFSET);
281
282 /* Enable read leveling gate DENALI_CTL_260 and DENALI_CTL_182 */
283 setbits_le32(DENALI_CTL_260 + denali_ctl, 1 << RDLVL_GATE_EN_OFFSET);
284 setbits_le32(DENALI_CTL_182 + denali_ctl,
285 1 << DFI_PHY_RDLVL_GATE_MODE_OFFSET);
286
Green Wana5936a22021-05-27 06:52:09 -0700287 if (sifive_ddr_get_dram_class(denali_ctl) == DRAM_CLASS_DDR4) {
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530288 /* Enable vref training DENALI_CTL_184 */
289 setbits_le32(DENALI_CTL_184 + denali_ctl, 1 << VREF_EN_OFFSET);
290 }
291
292 /* Mask off leveling completion interrupt DENALI_CTL_136 */
293 setbits_le32(DENALI_CTL_136 + denali_ctl,
294 1 << LEVELING_OPERATION_COMPLETED_OFFSET);
295
296 /* Mask off MC init complete interrupt DENALI_CTL_136 */
297 setbits_le32(DENALI_CTL_136 + denali_ctl, 1 << MC_INIT_COMPLETE_OFFSET);
298
299 /* Mask off out of range interrupts DENALI_CTL_136 */
300 setbits_le32(DENALI_CTL_136 + denali_ctl, (1 << OUT_OF_RANGE_OFFSET)
301 | (1 << MULTIPLE_OUT_OF_RANGE_OFFSET));
302
303 /* set up range protection */
Green Wana5936a22021-05-27 06:52:09 -0700304 sifive_ddr_setup_range_protection(denali_ctl, priv->info.size);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530305
306 /* Mask off port command error interrupt DENALI_CTL_136 */
307 setbits_le32(DENALI_CTL_136 + denali_ctl,
308 1 << PORT_COMMAND_CHANNEL_ERROR_OFFSET);
309
Green Wana5936a22021-05-27 06:52:09 -0700310 sifive_ddr_start(denali_ctl, priv->physical_filter_ctrl, ddr_end);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530311
Green Wana5936a22021-05-27 06:52:09 -0700312 sifive_ddr_phy_fixup(denali_phy);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530313
314 /* check size */
Bin Mengd7ca28d2021-09-12 11:15:15 +0800315 priv->info.size = get_ram_size((long *)(uintptr_t)priv->info.base,
Bin Mengca65e242020-07-19 23:06:35 -0700316 ddr_size);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530317
Bin Meng93b2a832020-08-18 01:09:21 -0700318 debug("%s : %lx\n", __func__, (uintptr_t)priv->info.size);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530319
320 /* check memory access for all memory */
Bin Mengca65e242020-07-19 23:06:35 -0700321 if (priv->info.size != ddr_size) {
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530322 printf("DDR invalid size : 0x%lx, expected 0x%lx\n",
Bin Meng93b2a832020-08-18 01:09:21 -0700323 (uintptr_t)priv->info.size, (uintptr_t)ddr_size);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530324 return -EINVAL;
325 }
326
327 return 0;
328}
329#endif
330
Green Wana5936a22021-05-27 06:52:09 -0700331static int sifive_ddr_probe(struct udevice *dev)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530332{
Green Wana5936a22021-05-27 06:52:09 -0700333 struct sifive_ddr_info *priv = dev_get_priv(dev);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530334
Bin Mengca65e242020-07-19 23:06:35 -0700335 /* Read memory base and size from DT */
336 fdtdec_setup_mem_size_base();
337 priv->info.base = gd->ram_base;
338 priv->info.size = gd->ram_size;
339
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530340#if defined(CONFIG_SPL_BUILD)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530341 int ret;
342 u32 clock = 0;
343
Green Wana5936a22021-05-27 06:52:09 -0700344 debug("sifive DDR probe\n");
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530345 priv->dev = dev;
346
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530347 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
348 if (ret) {
349 debug("clk get failed %d\n", ret);
350 return ret;
351 }
352
353 ret = dev_read_u32(dev, "clock-frequency", &clock);
354 if (ret) {
355 debug("clock-frequency not found in dt %d\n", ret);
356 return ret;
357 } else {
358 ret = clk_set_rate(&priv->ddr_clk, clock);
359 if (ret < 0) {
360 debug("Could not set DDR clock\n");
361 return ret;
362 }
363 }
364
365 ret = clk_enable(&priv->ddr_clk);
Bin Mengfb51bec2020-09-15 16:05:06 +0800366 if (ret < 0) {
367 debug("Could not enable DDR clock\n");
368 return ret;
369 }
370
Bin Mengd7ca28d2021-09-12 11:15:15 +0800371 priv->ctl = (struct sifive_ddrctl *)dev_read_addr_index_ptr(dev, 0);
372 priv->phy = (struct sifive_ddrphy *)dev_read_addr_index_ptr(dev, 1);
373 priv->physical_filter_ctrl = (u32 *)dev_read_addr_index_ptr(dev, 2);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530374
Green Wana5936a22021-05-27 06:52:09 -0700375 return sifive_ddr_setup(dev);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530376#endif
Bin Mengca65e242020-07-19 23:06:35 -0700377
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530378 return 0;
379}
380
Green Wana5936a22021-05-27 06:52:09 -0700381static int sifive_ddr_get_info(struct udevice *dev, struct ram_info *info)
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530382{
Green Wana5936a22021-05-27 06:52:09 -0700383 struct sifive_ddr_info *priv = dev_get_priv(dev);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530384
385 *info = priv->info;
386
387 return 0;
388}
389
Green Wana5936a22021-05-27 06:52:09 -0700390static struct ram_ops sifive_ddr_ops = {
391 .get_info = sifive_ddr_get_info,
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530392};
393
Green Wana5936a22021-05-27 06:52:09 -0700394static const struct udevice_id sifive_ddr_ids[] = {
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530395 { .compatible = "sifive,fu540-c000-ddr" },
Green Wana5936a22021-05-27 06:52:09 -0700396 { .compatible = "sifive,fu740-c000-ddr" },
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530397 { }
398};
399
Green Wana5936a22021-05-27 06:52:09 -0700400U_BOOT_DRIVER(sifive_ddr) = {
401 .name = "sifive_ddr",
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530402 .id = UCLASS_RAM,
Green Wana5936a22021-05-27 06:52:09 -0700403 .of_match = sifive_ddr_ids,
404 .ops = &sifive_ddr_ops,
405 .probe = sifive_ddr_probe,
406 .priv_auto = sizeof(struct sifive_ddr_info),
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530407#if defined(CONFIG_SPL_BUILD)
Green Wana5936a22021-05-27 06:52:09 -0700408 .plat_auto = sizeof(struct sifive_dmc_plat),
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530409#endif
410};