blob: 60d4945f848c85976dccad8aea223d74aeb53ae6 [file] [log] [blame]
Pragnesh Patel4cefe722020-05-29 11:33:26 +05301// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * (C) Copyright 2020 SiFive, Inc.
4 *
5 * Authors:
6 * Pragnesh Patel <pragnesh.patel@sifive.com>
7 */
8
9#include <common.h>
10#include <dm.h>
Bin Mengca65e242020-07-19 23:06:35 -070011#include <fdtdec.h>
Pragnesh Patel4cefe722020-05-29 11:33:26 +053012#include <init.h>
13#include <ram.h>
Pragnesh Patel4cefe722020-05-29 11:33:26 +053014#include <syscon.h>
15#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
Pragnesh Patel4cefe722020-05-29 11:33:26 +053067struct fu540_ddrctl {
68 volatile u32 denali_ctl[265];
69};
70
71struct fu540_ddrphy {
72 volatile u32 denali_phy[1215];
73};
74
75/**
76 * struct fu540_ddr_info
77 *
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 */
85struct fu540_ddr_info {
86 struct udevice *dev;
87 struct ram_info info;
88 struct fu540_ddrctl *ctl;
89 struct fu540_ddrphy *phy;
90 struct clk ddr_clk;
91 u32 *physical_filter_ctrl;
92};
93
94#if defined(CONFIG_SPL_BUILD)
95struct fu540_ddr_params {
96 struct fu540_ddrctl pctl_regs;
97 struct fu540_ddrphy phy_regs;
98};
99
100struct sifive_dmc_plat {
101 struct fu540_ddr_params ddr_params;
102};
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
120static void fu540_ddr_setup_range_protection(volatile u32 *ctl, u64 end_addr)
121{
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
137static void fu540_ddr_start(volatile u32 *ctl, u32 *physical_filter_ctrl,
138 u64 ddr_end)
139{
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
151static void fu540_ddr_check_errata(u32 regbase, u32 updownreg)
152{
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
204static u64 fu540_ddr_phy_fixup(volatile u32 *ddrphyreg)
205{
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
215 fu540_ddr_check_errata(regbase, updownreg);
216 }
217 slicebase += 128;
218 }
219
220 return(0);
221}
222
223static u32 fu540_ddr_get_dram_class(volatile u32 *ctl)
224{
225 u32 reg = readl(DENALI_CTL_0 + ctl);
226
227 return ((reg >> DRAM_CLASS_OFFSET) & 0xF);
228}
229
230static int fu540_ddr_setup(struct udevice *dev)
231{
232 struct fu540_ddr_info *priv = dev_get_priv(dev);
233 struct sifive_dmc_plat *plat = dev_get_platdata(dev);
234 struct fu540_ddr_params *params = &plat->ddr_params;
235 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,
253 sizeof(struct fu540_ddrctl));
254
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
287 if (fu540_ddr_get_dram_class(denali_ctl) == DRAM_CLASS_DDR4) {
288 /* 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 */
Bin Mengca65e242020-07-19 23:06:35 -0700304 fu540_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
310 fu540_ddr_start(denali_ctl, priv->physical_filter_ctrl, ddr_end);
311
312 fu540_ddr_phy_fixup(denali_phy);
313
314 /* check size */
315 priv->info.size = get_ram_size((long *)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
331static int fu540_ddr_probe(struct udevice *dev)
332{
333 struct fu540_ddr_info *priv = dev_get_priv(dev);
334
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
344 debug("FU540 DDR probe\n");
345 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 Meng8ad7da62020-09-15 16:05:07 +0800371 priv->ctl = (struct fu540_ddrctl *)dev_read_addr_index(dev, 0);
372 priv->phy = (struct fu540_ddrphy *)dev_read_addr_index(dev, 1);
373 priv->physical_filter_ctrl = (u32 *)dev_read_addr_index(dev, 2);
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530374
Pragnesh Patel4cefe722020-05-29 11:33:26 +0530375 return fu540_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
381static int fu540_ddr_get_info(struct udevice *dev, struct ram_info *info)
382{
383 struct fu540_ddr_info *priv = dev_get_priv(dev);
384
385 *info = priv->info;
386
387 return 0;
388}
389
390static struct ram_ops fu540_ddr_ops = {
391 .get_info = fu540_ddr_get_info,
392};
393
394static const struct udevice_id fu540_ddr_ids[] = {
395 { .compatible = "sifive,fu540-c000-ddr" },
396 { }
397};
398
399U_BOOT_DRIVER(fu540_ddr) = {
400 .name = "fu540_ddr",
401 .id = UCLASS_RAM,
402 .of_match = fu540_ddr_ids,
403 .ops = &fu540_ddr_ops,
404 .probe = fu540_ddr_probe,
405 .priv_auto_alloc_size = sizeof(struct fu540_ddr_info),
406#if defined(CONFIG_SPL_BUILD)
407 .platdata_auto_alloc_size = sizeof(struct sifive_dmc_plat),
408#endif
409};