blob: 4bd69a62be2b690c0e0db3f9f2b203ca76416e5a [file] [log] [blame]
Pragnesh Patelc514a942020-05-29 11:33:26 +05301// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
Green Wandab18c72021-05-27 06:52:09 -07003 * (C) Copyright 2020-2021 SiFive, Inc.
Pragnesh Patelc514a942020-05-29 11:33:26 +05304 *
5 * Authors:
6 * Pragnesh Patel <pragnesh.patel@sifive.com>
7 */
8
9#include <common.h>
10#include <dm.h>
Bin Mengecb70bd2020-07-19 23:06:35 -070011#include <fdtdec.h>
Pragnesh Patelc514a942020-05-29 11:33:26 +053012#include <init.h>
13#include <ram.h>
Pragnesh Patelc514a942020-05-29 11:33:26 +053014#include <syscon.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Pragnesh Patelc514a942020-05-29 11:33:26 +053016#include <asm/io.h>
17#include <clk.h>
18#include <wait_bit.h>
19#include <linux/bitops.h>
20
21#define DENALI_CTL_0 0
22#define DENALI_CTL_21 21
23#define DENALI_CTL_120 120
24#define DENALI_CTL_132 132
25#define DENALI_CTL_136 136
26#define DENALI_CTL_170 170
27#define DENALI_CTL_181 181
28#define DENALI_CTL_182 182
29#define DENALI_CTL_184 184
30#define DENALI_CTL_208 208
31#define DENALI_CTL_209 209
32#define DENALI_CTL_210 210
33#define DENALI_CTL_212 212
34#define DENALI_CTL_214 214
35#define DENALI_CTL_216 216
36#define DENALI_CTL_224 224
37#define DENALI_CTL_225 225
38#define DENALI_CTL_260 260
39
40#define DENALI_PHY_1152 1152
41#define DENALI_PHY_1214 1214
42
Pragnesh Patelc514a942020-05-29 11:33:26 +053043#define DRAM_CLASS_OFFSET 8
44#define DRAM_CLASS_DDR4 0xA
45#define OPTIMAL_RMODW_EN_OFFSET 0
46#define DISABLE_RD_INTERLEAVE_OFFSET 16
47#define OUT_OF_RANGE_OFFSET 1
48#define MULTIPLE_OUT_OF_RANGE_OFFSET 2
49#define PORT_COMMAND_CHANNEL_ERROR_OFFSET 7
50#define MC_INIT_COMPLETE_OFFSET 8
51#define LEVELING_OPERATION_COMPLETED_OFFSET 22
52#define DFI_PHY_WRLELV_MODE_OFFSET 24
53#define DFI_PHY_RDLVL_MODE_OFFSET 24
54#define DFI_PHY_RDLVL_GATE_MODE_OFFSET 0
55#define VREF_EN_OFFSET 24
56#define PORT_ADDR_PROTECTION_EN_OFFSET 0
57#define AXI0_ADDRESS_RANGE_ENABLE 8
58#define AXI0_RANGE_PROT_BITS_0_OFFSET 24
59#define RDLVL_EN_OFFSET 16
60#define RDLVL_GATE_EN_OFFSET 24
61#define WRLVL_EN_OFFSET 0
62
63#define PHY_RX_CAL_DQ0_0_OFFSET 0
64#define PHY_RX_CAL_DQ1_0_OFFSET 16
65
Bin Mengecb70bd2020-07-19 23:06:35 -070066DECLARE_GLOBAL_DATA_PTR;
67
Green Wandab18c72021-05-27 06:52:09 -070068struct sifive_ddrctl {
Pragnesh Patelc514a942020-05-29 11:33:26 +053069 volatile u32 denali_ctl[265];
70};
71
Green Wandab18c72021-05-27 06:52:09 -070072struct sifive_ddrphy {
Pragnesh Patelc514a942020-05-29 11:33:26 +053073 volatile u32 denali_phy[1215];
74};
75
76/**
Green Wandab18c72021-05-27 06:52:09 -070077 * struct sifive_ddr_info
Pragnesh Patelc514a942020-05-29 11:33:26 +053078 *
79 * @dev : pointer for the device
80 * @info : UCLASS RAM information
81 * @ctl : DDR controller base address
82 * @phy : DDR PHY base address
83 * @ctrl : DDR control base address
84 * @physical_filter_ctrl : DDR physical filter control base address
85 */
Green Wandab18c72021-05-27 06:52:09 -070086struct sifive_ddr_info {
Pragnesh Patelc514a942020-05-29 11:33:26 +053087 struct udevice *dev;
88 struct ram_info info;
Green Wandab18c72021-05-27 06:52:09 -070089 struct sifive_ddrctl *ctl;
90 struct sifive_ddrphy *phy;
Pragnesh Patelc514a942020-05-29 11:33:26 +053091 struct clk ddr_clk;
92 u32 *physical_filter_ctrl;
93};
94
95#if defined(CONFIG_SPL_BUILD)
Green Wandab18c72021-05-27 06:52:09 -070096struct sifive_ddr_params {
97 struct sifive_ddrctl pctl_regs;
98 struct sifive_ddrphy phy_regs;
Pragnesh Patelc514a942020-05-29 11:33:26 +053099};
100
101struct sifive_dmc_plat {
Green Wandab18c72021-05-27 06:52:09 -0700102 struct sifive_ddr_params ddr_params;
Pragnesh Patelc514a942020-05-29 11:33:26 +0530103};
104
105/*
106 * TODO : It can be possible to use common sdram_copy_to_reg() API
107 * n: Unit bytes
108 */
109static void sdram_copy_to_reg(volatile u32 *dest,
110 volatile u32 *src, u32 n)
111{
112 int i;
113
114 for (i = 0; i < n / sizeof(u32); i++) {
115 writel(*src, dest);
116 src++;
117 dest++;
118 }
119}
120
Green Wandab18c72021-05-27 06:52:09 -0700121static void sifive_ddr_setup_range_protection(volatile u32 *ctl, u64 end_addr)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530122{
123 u32 end_addr_16kblocks = ((end_addr >> 14) & 0x7FFFFF) - 1;
124
125 writel(0x0, DENALI_CTL_209 + ctl);
126 writel(end_addr_16kblocks, DENALI_CTL_210 + ctl);
127 writel(0x0, DENALI_CTL_212 + ctl);
128 writel(0x0, DENALI_CTL_214 + ctl);
129 writel(0x0, DENALI_CTL_216 + ctl);
130 setbits_le32(DENALI_CTL_224 + ctl,
131 0x3 << AXI0_RANGE_PROT_BITS_0_OFFSET);
132 writel(0xFFFFFFFF, DENALI_CTL_225 + ctl);
133 setbits_le32(DENALI_CTL_208 + ctl, 0x1 << AXI0_ADDRESS_RANGE_ENABLE);
134 setbits_le32(DENALI_CTL_208 + ctl,
135 0x1 << PORT_ADDR_PROTECTION_EN_OFFSET);
136}
137
Green Wandab18c72021-05-27 06:52:09 -0700138static void sifive_ddr_start(volatile u32 *ctl, u32 *physical_filter_ctrl,
139 u64 ddr_end)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530140{
141 volatile u64 *filterreg = (volatile u64 *)physical_filter_ctrl;
142
143 setbits_le32(DENALI_CTL_0 + ctl, 0x1);
144
145 wait_for_bit_le32((void *)ctl + DENALI_CTL_132,
146 BIT(MC_INIT_COMPLETE_OFFSET), false, 100, false);
147
148 /* Disable the BusBlocker in front of the controller AXI slave ports */
149 filterreg[0] = 0x0f00000000000000UL | (ddr_end >> 2);
150}
151
Green Wandab18c72021-05-27 06:52:09 -0700152static void sifive_ddr_check_errata(u32 regbase, u32 updownreg)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530153{
154 u64 fails = 0;
155 u32 dq = 0;
156 u32 down, up;
157 u8 failc0, failc1;
158 u32 phy_rx_cal_dqn_0_offset;
159
160 for (u32 bit = 0; bit < 2; bit++) {
161 if (bit == 0) {
162 phy_rx_cal_dqn_0_offset =
163 PHY_RX_CAL_DQ0_0_OFFSET;
164 } else {
165 phy_rx_cal_dqn_0_offset =
166 PHY_RX_CAL_DQ1_0_OFFSET;
167 }
168
169 down = (updownreg >>
170 phy_rx_cal_dqn_0_offset) & 0x3F;
171 up = (updownreg >>
172 (phy_rx_cal_dqn_0_offset + 6)) &
173 0x3F;
174
175 failc0 = ((down == 0) && (up == 0x3F));
176 failc1 = ((up == 0) && (down == 0x3F));
177
178 /* print error message on failure */
179 if (failc0 || failc1) {
180 if (fails == 0)
181 printf("DDR error in fixing up\n");
182
183 fails |= (1 << dq);
184
185 char slicelsc = '0';
186 char slicemsc = '0';
187
188 slicelsc += (dq % 10);
189 slicemsc += (dq / 10);
190 printf("S ");
191 printf("%c", slicemsc);
192 printf("%c", slicelsc);
193
194 if (failc0)
195 printf("U");
196 else
197 printf("D");
198
199 printf("\n");
200 }
201 dq++;
202 }
203}
204
Green Wandab18c72021-05-27 06:52:09 -0700205static u64 sifive_ddr_phy_fixup(volatile u32 *ddrphyreg)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530206{
207 u32 slicebase = 0;
208
209 /* check errata condition */
210 for (u32 slice = 0; slice < 8; slice++) {
211 u32 regbase = slicebase + 34;
212
213 for (u32 reg = 0; reg < 4; reg++) {
214 u32 updownreg = readl(regbase + reg + ddrphyreg);
215
Green Wandab18c72021-05-27 06:52:09 -0700216 sifive_ddr_check_errata(regbase, updownreg);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530217 }
218 slicebase += 128;
219 }
220
221 return(0);
222}
223
Green Wandab18c72021-05-27 06:52:09 -0700224static u32 sifive_ddr_get_dram_class(volatile u32 *ctl)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530225{
226 u32 reg = readl(DENALI_CTL_0 + ctl);
227
228 return ((reg >> DRAM_CLASS_OFFSET) & 0xF);
229}
230
Green Wandab18c72021-05-27 06:52:09 -0700231static int sifive_ddr_setup(struct udevice *dev)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530232{
Green Wandab18c72021-05-27 06:52:09 -0700233 struct sifive_ddr_info *priv = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700234 struct sifive_dmc_plat *plat = dev_get_plat(dev);
Green Wandab18c72021-05-27 06:52:09 -0700235 struct sifive_ddr_params *params = &plat->ddr_params;
Pragnesh Patelc514a942020-05-29 11:33:26 +0530236 volatile u32 *denali_ctl = priv->ctl->denali_ctl;
237 volatile u32 *denali_phy = priv->phy->denali_phy;
Bin Mengecb70bd2020-07-19 23:06:35 -0700238 const u64 ddr_size = priv->info.size;
239 const u64 ddr_end = priv->info.base + ddr_size;
Pragnesh Patelc514a942020-05-29 11:33:26 +0530240 int ret, i;
241 u32 physet;
242
243 ret = dev_read_u32_array(dev, "sifive,ddr-params",
244 (u32 *)&plat->ddr_params,
245 sizeof(plat->ddr_params) / sizeof(u32));
246 if (ret) {
247 printf("%s: Cannot read sifive,ddr-params %d\n",
248 __func__, ret);
249 return ret;
250 }
251
252 sdram_copy_to_reg(priv->ctl->denali_ctl,
253 params->pctl_regs.denali_ctl,
Green Wandab18c72021-05-27 06:52:09 -0700254 sizeof(struct sifive_ddrctl));
Pragnesh Patelc514a942020-05-29 11:33:26 +0530255
256 /* phy reset */
257 for (i = DENALI_PHY_1152; i <= DENALI_PHY_1214; i++) {
258 physet = params->phy_regs.denali_phy[i];
259 priv->phy->denali_phy[i] = physet;
260 }
261
262 for (i = 0; i < DENALI_PHY_1152; i++) {
263 physet = params->phy_regs.denali_phy[i];
264 priv->phy->denali_phy[i] = physet;
265 }
266
267 /* Disable read interleave DENALI_CTL_120 */
268 setbits_le32(DENALI_CTL_120 + denali_ctl,
269 1 << DISABLE_RD_INTERLEAVE_OFFSET);
270
271 /* Disable optimal read/modify/write logic DENALI_CTL_21 */
272 clrbits_le32(DENALI_CTL_21 + denali_ctl, 1 << OPTIMAL_RMODW_EN_OFFSET);
273
274 /* Enable write Leveling DENALI_CTL_170 */
275 setbits_le32(DENALI_CTL_170 + denali_ctl, (1 << WRLVL_EN_OFFSET)
276 | (1 << DFI_PHY_WRLELV_MODE_OFFSET));
277
278 /* Enable read leveling DENALI_CTL_181 and DENALI_CTL_260 */
279 setbits_le32(DENALI_CTL_181 + denali_ctl,
280 1 << DFI_PHY_RDLVL_MODE_OFFSET);
281 setbits_le32(DENALI_CTL_260 + denali_ctl, 1 << RDLVL_EN_OFFSET);
282
283 /* Enable read leveling gate DENALI_CTL_260 and DENALI_CTL_182 */
284 setbits_le32(DENALI_CTL_260 + denali_ctl, 1 << RDLVL_GATE_EN_OFFSET);
285 setbits_le32(DENALI_CTL_182 + denali_ctl,
286 1 << DFI_PHY_RDLVL_GATE_MODE_OFFSET);
287
Green Wandab18c72021-05-27 06:52:09 -0700288 if (sifive_ddr_get_dram_class(denali_ctl) == DRAM_CLASS_DDR4) {
Pragnesh Patelc514a942020-05-29 11:33:26 +0530289 /* Enable vref training DENALI_CTL_184 */
290 setbits_le32(DENALI_CTL_184 + denali_ctl, 1 << VREF_EN_OFFSET);
291 }
292
293 /* Mask off leveling completion interrupt DENALI_CTL_136 */
294 setbits_le32(DENALI_CTL_136 + denali_ctl,
295 1 << LEVELING_OPERATION_COMPLETED_OFFSET);
296
297 /* Mask off MC init complete interrupt DENALI_CTL_136 */
298 setbits_le32(DENALI_CTL_136 + denali_ctl, 1 << MC_INIT_COMPLETE_OFFSET);
299
300 /* Mask off out of range interrupts DENALI_CTL_136 */
301 setbits_le32(DENALI_CTL_136 + denali_ctl, (1 << OUT_OF_RANGE_OFFSET)
302 | (1 << MULTIPLE_OUT_OF_RANGE_OFFSET));
303
304 /* set up range protection */
Green Wandab18c72021-05-27 06:52:09 -0700305 sifive_ddr_setup_range_protection(denali_ctl, priv->info.size);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530306
307 /* Mask off port command error interrupt DENALI_CTL_136 */
308 setbits_le32(DENALI_CTL_136 + denali_ctl,
309 1 << PORT_COMMAND_CHANNEL_ERROR_OFFSET);
310
Green Wandab18c72021-05-27 06:52:09 -0700311 sifive_ddr_start(denali_ctl, priv->physical_filter_ctrl, ddr_end);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530312
Green Wandab18c72021-05-27 06:52:09 -0700313 sifive_ddr_phy_fixup(denali_phy);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530314
315 /* check size */
Bin Mengd36c9422021-09-12 11:15:15 +0800316 priv->info.size = get_ram_size((long *)(uintptr_t)priv->info.base,
Bin Mengecb70bd2020-07-19 23:06:35 -0700317 ddr_size);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530318
Bin Meng3ab26012020-08-18 01:09:21 -0700319 debug("%s : %lx\n", __func__, (uintptr_t)priv->info.size);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530320
321 /* check memory access for all memory */
Bin Mengecb70bd2020-07-19 23:06:35 -0700322 if (priv->info.size != ddr_size) {
Pragnesh Patelc514a942020-05-29 11:33:26 +0530323 printf("DDR invalid size : 0x%lx, expected 0x%lx\n",
Bin Meng3ab26012020-08-18 01:09:21 -0700324 (uintptr_t)priv->info.size, (uintptr_t)ddr_size);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530325 return -EINVAL;
326 }
327
328 return 0;
329}
330#endif
331
Green Wandab18c72021-05-27 06:52:09 -0700332static int sifive_ddr_probe(struct udevice *dev)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530333{
Green Wandab18c72021-05-27 06:52:09 -0700334 struct sifive_ddr_info *priv = dev_get_priv(dev);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530335
Bin Mengecb70bd2020-07-19 23:06:35 -0700336 /* Read memory base and size from DT */
337 fdtdec_setup_mem_size_base();
338 priv->info.base = gd->ram_base;
339 priv->info.size = gd->ram_size;
340
Pragnesh Patelc514a942020-05-29 11:33:26 +0530341#if defined(CONFIG_SPL_BUILD)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530342 int ret;
343 u32 clock = 0;
344
Green Wandab18c72021-05-27 06:52:09 -0700345 debug("sifive DDR probe\n");
Pragnesh Patelc514a942020-05-29 11:33:26 +0530346 priv->dev = dev;
347
Pragnesh Patelc514a942020-05-29 11:33:26 +0530348 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
349 if (ret) {
350 debug("clk get failed %d\n", ret);
351 return ret;
352 }
353
354 ret = dev_read_u32(dev, "clock-frequency", &clock);
355 if (ret) {
356 debug("clock-frequency not found in dt %d\n", ret);
357 return ret;
358 } else {
359 ret = clk_set_rate(&priv->ddr_clk, clock);
360 if (ret < 0) {
361 debug("Could not set DDR clock\n");
362 return ret;
363 }
364 }
365
366 ret = clk_enable(&priv->ddr_clk);
Bin Mengf8c96602020-09-15 16:05:06 +0800367 if (ret < 0) {
368 debug("Could not enable DDR clock\n");
369 return ret;
370 }
371
Bin Mengd36c9422021-09-12 11:15:15 +0800372 priv->ctl = (struct sifive_ddrctl *)dev_read_addr_index_ptr(dev, 0);
373 priv->phy = (struct sifive_ddrphy *)dev_read_addr_index_ptr(dev, 1);
374 priv->physical_filter_ctrl = (u32 *)dev_read_addr_index_ptr(dev, 2);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530375
Green Wandab18c72021-05-27 06:52:09 -0700376 return sifive_ddr_setup(dev);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530377#endif
Bin Mengecb70bd2020-07-19 23:06:35 -0700378
Pragnesh Patelc514a942020-05-29 11:33:26 +0530379 return 0;
380}
381
Green Wandab18c72021-05-27 06:52:09 -0700382static int sifive_ddr_get_info(struct udevice *dev, struct ram_info *info)
Pragnesh Patelc514a942020-05-29 11:33:26 +0530383{
Green Wandab18c72021-05-27 06:52:09 -0700384 struct sifive_ddr_info *priv = dev_get_priv(dev);
Pragnesh Patelc514a942020-05-29 11:33:26 +0530385
386 *info = priv->info;
387
388 return 0;
389}
390
Green Wandab18c72021-05-27 06:52:09 -0700391static struct ram_ops sifive_ddr_ops = {
392 .get_info = sifive_ddr_get_info,
Pragnesh Patelc514a942020-05-29 11:33:26 +0530393};
394
Green Wandab18c72021-05-27 06:52:09 -0700395static const struct udevice_id sifive_ddr_ids[] = {
Pragnesh Patelc514a942020-05-29 11:33:26 +0530396 { .compatible = "sifive,fu540-c000-ddr" },
Green Wandab18c72021-05-27 06:52:09 -0700397 { .compatible = "sifive,fu740-c000-ddr" },
Pragnesh Patelc514a942020-05-29 11:33:26 +0530398 { }
399};
400
Green Wandab18c72021-05-27 06:52:09 -0700401U_BOOT_DRIVER(sifive_ddr) = {
402 .name = "sifive_ddr",
Pragnesh Patelc514a942020-05-29 11:33:26 +0530403 .id = UCLASS_RAM,
Green Wandab18c72021-05-27 06:52:09 -0700404 .of_match = sifive_ddr_ids,
405 .ops = &sifive_ddr_ops,
406 .probe = sifive_ddr_probe,
407 .priv_auto = sizeof(struct sifive_ddr_info),
Pragnesh Patelc514a942020-05-29 11:33:26 +0530408#if defined(CONFIG_SPL_BUILD)
Green Wandab18c72021-05-27 06:52:09 -0700409 .plat_auto = sizeof(struct sifive_dmc_plat),
Pragnesh Patelc514a942020-05-29 11:33:26 +0530410#endif
411};