blob: 4d470aae1914c104f117107a45fa70bb32add21c [file] [log] [blame]
Ralph Siemsen9446dfe2023-05-12 21:36:56 -04001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * RZ/N1 DDR Controller initialisation
4 *
5 * The DDR Controller register values for a specific DDR device, mode and
6 * frequency are generated using a Cadence tool.
7 *
8 * Copyright (C) 2015 Renesas Electronics Europe Ltd
9 */
Ralph Siemsen9446dfe2023-05-12 21:36:56 -040010#include <clk.h>
11#include <dm.h>
12#include <dm/device_compat.h>
13#include <ram.h>
14#include <regmap.h>
15#include <syscon.h>
16#include <asm/io.h>
17#include <linux/delay.h>
18#include <wait_bit.h>
19#include <renesas/ddr_ctrl.h>
20
21void clk_rzn1_reset_state(struct clk *clk, int on);
22
23DECLARE_GLOBAL_DATA_PTR;
24
25struct cadence_ddr_info {
26 struct udevice *dev;
27 void __iomem *ddrc;
28 void __iomem *phy;
29 struct clk clk_ddrc;
30 struct clk hclk_ddrc;
31 struct regmap *syscon;
32 bool enable_ecc;
33 bool enable_8bit;
34 u32 ddr_size;
35
36 /* These two used only during .probe */
37 u32 *reg0;
38 u32 *reg350;
39};
40
41static inline u32 cadence_readl(void __iomem *addr, unsigned int offset)
42{
43 return readl(addr + offset);
44}
45
46static inline void cadence_writel(void __iomem *addr, unsigned int offset,
47 u32 data)
48{
49 debug("%s: addr = 0x%p, value = 0x%08x\n", __func__, addr + offset, data);
50 writel(data, addr + offset);
51}
52
53#define ddrc_readl(off) cadence_readl(priv->ddrc, off)
54#define ddrc_writel(val, off) cadence_writel(priv->ddrc, off, val)
55
56#define phy_readl(off) cadence_readl(priv->phy, off)
57#define phy_writel(val, off) cadence_writel(priv->phy, off, val)
58
59#define RZN1_DDR3_SINGLE_BANK 3
60#define RZN1_DDR3_DUAL_BANK 32
61
62#define FUNCCTRL 0x00
63#define FUNCCTRL_MASKSDLOFS (0x18 << 16)
64#define FUNCCTRL_DVDDQ_1_5V BIT(8)
65#define FUNCCTRL_RESET_N BIT(0)
66#define DLLCTRL 0x04
67#define DLLCTRL_ASDLLOCK BIT(26)
68#define DLLCTRL_MFSL_500MHz (2 << 1)
69#define DLLCTRL_MDLLSTBY BIT(0)
70#define ZQCALCTRL 0x08
71#define ZQCALCTRL_ZQCALEND BIT(30)
72#define ZQCALCTRL_ZQCALRSTB BIT(0)
73#define ZQODTCTRL 0x0c
74#define RDCTRL 0x10
75#define RDTMG 0x14
76#define FIFOINIT 0x18
77#define FIFOINIT_RDPTINITEXE BIT(8)
78#define FIFOINIT_WRPTINITEXE BIT(0)
79#define OUTCTRL 0x1c
80#define OUTCTRL_ADCMDOE BIT(0)
81#define WLCTRL1 0x40
82#define WLCTRL1_WLSTR BIT(24)
83#define DQCALOFS1 0xe8
84
85/* DDR PHY setup */
86static void ddr_phy_init(struct cadence_ddr_info *priv, int ddr_type)
87{
88 u32 val;
89
90 /* Disable DDR Controller clock and FlexWAY connection */
91 clk_disable(&priv->hclk_ddrc);
92 clk_disable(&priv->clk_ddrc);
93
94 clk_rzn1_reset_state(&priv->hclk_ddrc, 0);
95 clk_rzn1_reset_state(&priv->clk_ddrc, 0);
96
97 /* Enable DDR Controller clock and FlexWAY connection */
98 clk_enable(&priv->clk_ddrc);
99 clk_enable(&priv->hclk_ddrc);
100
101 /* DDR PHY Soft reset assert */
102 ddrc_writel(FUNCCTRL_MASKSDLOFS | FUNCCTRL_DVDDQ_1_5V, FUNCCTRL);
103
104 clk_rzn1_reset_state(&priv->hclk_ddrc, 1);
105 clk_rzn1_reset_state(&priv->clk_ddrc, 1);
106
107 /* DDR PHY setup */
108 phy_writel(DLLCTRL_MFSL_500MHz | DLLCTRL_MDLLSTBY, DLLCTRL);
109 phy_writel(0x00000182, ZQCALCTRL);
110 if (ddr_type == RZN1_DDR3_DUAL_BANK)
111 phy_writel(0xAB330031, ZQODTCTRL);
112 else if (ddr_type == RZN1_DDR3_SINGLE_BANK)
113 phy_writel(0xAB320051, ZQODTCTRL);
114 else /* DDR2 */
115 phy_writel(0xAB330071, ZQODTCTRL);
116 phy_writel(0xB545B544, RDCTRL);
117 phy_writel(0x000000B0, RDTMG);
118 phy_writel(0x020A0806, OUTCTRL);
119 if (ddr_type == RZN1_DDR3_DUAL_BANK)
120 phy_writel(0x80005556, WLCTRL1);
121 else
122 phy_writel(0x80005C5D, WLCTRL1);
123 phy_writel(0x00000101, FIFOINIT);
124 phy_writel(0x00004545, DQCALOFS1);
125
126 /* Step 9 MDLL reset release */
127 val = phy_readl(DLLCTRL);
128 val &= ~DLLCTRL_MDLLSTBY;
129 phy_writel(val, DLLCTRL);
130
131 /* Step 12 Soft reset release */
132 val = phy_readl(FUNCCTRL);
133 val |= FUNCCTRL_RESET_N;
134 phy_writel(val, FUNCCTRL);
135
136 /* Step 13 FIFO pointer initialize */
137 phy_writel(FIFOINIT_RDPTINITEXE | FIFOINIT_WRPTINITEXE, FIFOINIT);
138
139 /* Step 14 Execute ZQ Calibration */
140 val = phy_readl(ZQCALCTRL);
141 val |= ZQCALCTRL_ZQCALRSTB;
142 phy_writel(val, ZQCALCTRL);
143
144 /* Step 15 Wait for 200us or more, or wait for DFIINITCOMPLETE to be "1" */
145 wait_for_bit_le32(priv->phy + DLLCTRL, DLLCTRL_ASDLLOCK, true, 1, false);
146 wait_for_bit_le32(priv->phy + ZQCALCTRL, ZQCALCTRL_ZQCALEND, true, 1, false);
147
148 /* Step 16 Enable Address and Command output */
149 val = phy_readl(OUTCTRL);
150 val |= OUTCTRL_ADCMDOE;
151 phy_writel(val, OUTCTRL);
152
153 /* Step 17 Wait for 200us or more(from MRESETB=0) */
154 udelay(200);
155}
156
157static void ddr_phy_enable_wl(struct cadence_ddr_info *priv)
158{
159 u32 val;
160
161 /* Step 26 (Set Write Leveling) */
162 val = phy_readl(WLCTRL1);
163 val |= WLCTRL1_WLSTR;
164 phy_writel(val, WLCTRL1);
165}
166
167#define RZN1_V_DDR_BASE 0x80000000 /* RZ/N1D only */
168
169static void rzn1_ddr3_single_bank(void *ddr_ctrl_base)
170{
171 /* CS0 */
172 cdns_ddr_set_mr1(ddr_ctrl_base, 0,
173 MR1_ODT_IMPEDANCE_60_OHMS,
174 MR1_DRIVE_STRENGTH_40_OHMS);
175 cdns_ddr_set_mr2(ddr_ctrl_base, 0,
176 MR2_DYNAMIC_ODT_OFF,
177 MR2_SELF_REFRESH_TEMP_EXT);
178
179 /* ODT_WR_MAP_CS0 = 1, ODT_RD_MAP_CS0 = 0 */
180 cdns_ddr_set_odt_map(ddr_ctrl_base, 0, 0x0100);
181}
182
183static int rzn1_dram_init(struct cadence_ddr_info *priv)
184{
185 u32 version;
186 u32 ddr_start_addr = 0;
187
188 ddr_phy_init(priv, RZN1_DDR3_SINGLE_BANK);
189
190 /*
191 * Override DDR PHY Interface (DFI) related settings
192 * DFI is the internal interface between the DDR controller and the DDR PHY.
193 * These settings are specific to the board and can't be known by the settings
194 * provided for each DDR model within the generated include.
195 */
196 priv->reg350[351 - 350] = 0x001e0000;
197 priv->reg350[352 - 350] = 0x1e680000;
198 priv->reg350[353 - 350] = 0x02000020;
199 priv->reg350[354 - 350] = 0x02000200;
200 priv->reg350[355 - 350] = 0x00000c30;
201 priv->reg350[356 - 350] = 0x00009808;
202 priv->reg350[357 - 350] = 0x020a0706;
203 priv->reg350[372 - 350] = 0x01000000;
204
205 /*
206 * On ES1.0 devices, the DDR start address that the DDR Controller sees
207 * is the physical address of the DDR. However, later devices changed it
208 * to be 0 in order to fix an issue with DDR out-of-range detection.
209 */
210#define RZN1_SYSCTRL_REG_VERSION 412
211 regmap_read(priv->syscon, RZN1_SYSCTRL_REG_VERSION, &version);
212 if (version == 0x10)
213 ddr_start_addr = RZN1_V_DDR_BASE;
214
215 if (priv->enable_ecc)
216 priv->ddr_size = priv->ddr_size / 2;
217
218 /* DDR Controller is always in ASYNC mode */
219 cdns_ddr_ctrl_init(priv->ddrc, 1,
220 priv->reg0, priv->reg350,
221 ddr_start_addr, priv->ddr_size,
222 priv->enable_ecc, priv->enable_8bit);
223
224 rzn1_ddr3_single_bank(priv->ddrc);
225 cdns_ddr_set_diff_cs_delays(priv->ddrc, 2, 7, 2, 2);
226 cdns_ddr_set_same_cs_delays(priv->ddrc, 0, 7, 0, 0);
227 cdns_ddr_set_odt_times(priv->ddrc, 5, 6, 6, 0, 4);
228 cdns_ddr_ctrl_start(priv->ddrc);
229
230 ddr_phy_enable_wl(priv);
231
232 if (priv->enable_ecc) {
233 /*
234 * Any read before a write will trigger an ECC un-correctable error,
235 * causing a data abort. However, this is also true for any read with a
236 * size less than the AXI bus width. So, the only sensible solution is
237 * to write to all of DDR now and take the hit...
238 */
239 memset((void *)RZN1_V_DDR_BASE, 0xff, priv->ddr_size);
240 }
241
242 return 0;
243}
244
245static int cadence_ddr_get_info(struct udevice *udev, struct ram_info *info)
246{
247 info->base = 0;
248 info->size = gd->ram_size;
249
250 return 0;
251}
252
253static struct ram_ops cadence_ddr_ops = {
254 .get_info = cadence_ddr_get_info,
255};
256
257static int cadence_ddr_test(long *base, long maxsize)
258{
259 volatile long *addr = base;
260 long cnt;
261
262 maxsize /= sizeof(long);
263
264 for (cnt = 1; cnt <= maxsize; cnt <<= 1) {
265 addr[cnt - 1] = ~cnt;
266 }
267
268 for (cnt = 1; cnt <= maxsize; cnt <<= 1) {
269 if (addr[cnt - 1] != ~cnt) {
270 return 0;
271 }
272 }
273
274 return 1;
275}
276
277static int cadence_ddr_probe(struct udevice *dev)
278{
279 struct cadence_ddr_info *priv = dev_get_priv(dev);
280 ofnode subnode;
281 int ret;
282
283 priv->dev = dev;
284
285 priv->ddrc = dev_remap_addr_name(dev, "ddrc");
286 if (!priv->ddrc) {
287 dev_err(dev, "No reg property for Cadence DDR CTRL\n");
288 return -EINVAL;
289 }
290
291 priv->phy = dev_remap_addr_name(dev, "phy");
292 if (!priv->phy) {
293 dev_err(dev, "No reg property for Cadence DDR PHY\n");
294 return -EINVAL;
295 }
296
297 ret = clk_get_by_name(dev, "clk_ddrc", &priv->clk_ddrc);
298 if (ret) {
299 dev_err(dev, "No clock for Cadence DDR\n");
300 return ret;
301 }
302
303 ret = clk_get_by_name(dev, "hclk_ddrc", &priv->hclk_ddrc);
304 if (ret) {
305 dev_err(dev, "No HCLK for Cadence DDR\n");
306 return ret;
307 }
308
309 priv->syscon = syscon_regmap_lookup_by_phandle(dev, "syscon");
310 if (IS_ERR(priv->syscon)) {
311 dev_err(dev, "No syscon node found\n");
312 return PTR_ERR(priv->syscon);
313 }
314
315 priv->enable_ecc = dev_read_bool(dev, "enable-ecc");
316 priv->enable_8bit = dev_read_bool(dev, "enable-8bit");
317
318 priv->reg0 = malloc(88 * sizeof(u32));
319 priv->reg350 = malloc(25 * sizeof(u32));
320 if (!priv->reg0 || !priv->reg350)
321 panic("malloc failure\n");
322
323 /* There may be multiple DDR configurations to try */
324 dev_for_each_subnode(subnode, dev) {
325 ret = ofnode_read_u32(subnode, "size", &priv->ddr_size);
326 if (ret) {
327 dev_err(dev, "No size for Cadence DDR\n");
328 continue;
329 }
330
331 ret = ofnode_read_u32_array(subnode, "cadence,ctl-000", priv->reg0, 88);
332 if (ret) {
333 dev_err(dev, "No cadence,ctl-000\n");
334 continue;
335 }
336
337 ret = ofnode_read_u32_array(subnode, "cadence,ctl-350", priv->reg350, 25);
338 if (ret) {
339 dev_err(dev, "No cadence,ctl-350\n");
340 continue;
341 }
342
343 if (rzn1_dram_init(priv))
344 continue;
345
346 if (cadence_ddr_test((long *)RZN1_V_DDR_BASE, priv->ddr_size)) {
347 gd->ram_base = RZN1_V_DDR_BASE;
348 gd->ram_size = priv->ddr_size;
349 break;
350 }
351 }
352
353 if (!priv->ddr_size)
354 panic("No valid DDR to start");
355
356 free(priv->reg350);
357 free(priv->reg0);
358
359 return 0;
360}
361
362static const struct udevice_id cadence_ddr_ids[] = {
363 { .compatible = "cadence,ddr-ctrl" },
364 { }
365};
366
367U_BOOT_DRIVER(cadence_ddr) = {
368 .name = "cadence_ddr",
369 .id = UCLASS_RAM,
370 .of_match = cadence_ddr_ids,
371 .ops = &cadence_ddr_ops,
372 .probe = cadence_ddr_probe,
373 .priv_auto = sizeof(struct cadence_ddr_info),
374 .flags = DM_FLAG_PRE_RELOC,
375};