blob: 9d086601a4a42103cca37418ac8ddf2d49737974 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01004 */
5
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +01006#define LOG_CATEGORY UCLASS_RAM
7
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01008#include <common.h>
9#include <clk.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010011#include <ram.h>
12#include <reset.h>
13#include <timer.h>
14#include <asm/io.h>
15#include <asm/arch/ddr.h>
Simon Glasscd93d622020-05-10 11:40:13 -060016#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060017#include <linux/delay.h>
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010018#include <linux/iopoll.h>
19#include "stm32mp1_ddr.h"
20#include "stm32mp1_ddr_regs.h"
21
22#define RCC_DDRITFCR 0xD8
23
24#define RCC_DDRITFCR_DDRCAPBRST (BIT(14))
25#define RCC_DDRITFCR_DDRCAXIRST (BIT(15))
26#define RCC_DDRITFCR_DDRCORERST (BIT(16))
27#define RCC_DDRITFCR_DPHYAPBRST (BIT(17))
28#define RCC_DDRITFCR_DPHYRST (BIT(18))
29#define RCC_DDRITFCR_DPHYCTLRST (BIT(19))
30
31struct reg_desc {
32 const char *name;
33 u16 offset; /* offset for base address */
34 u8 par_offset; /* offset for parameter array */
35};
36
37#define INVALID_OFFSET 0xFF
38
39#define DDRCTL_REG(x, y) \
40 {#x,\
41 offsetof(struct stm32mp1_ddrctl, x),\
42 offsetof(struct y, x)}
43
44#define DDRPHY_REG(x, y) \
45 {#x,\
46 offsetof(struct stm32mp1_ddrphy, x),\
47 offsetof(struct y, x)}
48
Patrick Delaunay01a75102019-04-10 14:09:27 +020049#define DDR_REG_DYN(x) \
50 {#x,\
51 offsetof(struct stm32mp1_ddrctl, x),\
52 INVALID_OFFSET}
53
54#define DDRPHY_REG_DYN(x) \
55 {#x,\
56 offsetof(struct stm32mp1_ddrphy, x),\
57 INVALID_OFFSET}
58
Patrick Delaunay53bb8312019-04-10 14:09:25 +020059/***********************************************************
60 * PARAMETERS: value get from device tree :
61 * size / order need to be aligned with binding
62 * modification NOT ALLOWED !!!
63 ***********************************************************/
64#define DDRCTL_REG_REG_SIZE 25 /* st,ctl-reg */
65#define DDRCTL_REG_TIMING_SIZE 12 /* st,ctl-timing */
66#define DDRCTL_REG_MAP_SIZE 9 /* st,ctl-map */
67#define DDRCTL_REG_PERF_SIZE 17 /* st,ctl-perf */
68
69#define DDRPHY_REG_REG_SIZE 11 /* st,phy-reg */
70#define DDRPHY_REG_TIMING_SIZE 10 /* st,phy-timing */
Patrick Delaunay53bb8312019-04-10 14:09:25 +020071
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010072#define DDRCTL_REG_REG(x) DDRCTL_REG(x, stm32mp1_ddrctrl_reg)
Patrick Delaunay53bb8312019-04-10 14:09:25 +020073static const struct reg_desc ddr_reg[DDRCTL_REG_REG_SIZE] = {
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010074 DDRCTL_REG_REG(mstr),
75 DDRCTL_REG_REG(mrctrl0),
76 DDRCTL_REG_REG(mrctrl1),
77 DDRCTL_REG_REG(derateen),
78 DDRCTL_REG_REG(derateint),
79 DDRCTL_REG_REG(pwrctl),
80 DDRCTL_REG_REG(pwrtmg),
81 DDRCTL_REG_REG(hwlpctl),
82 DDRCTL_REG_REG(rfshctl0),
83 DDRCTL_REG_REG(rfshctl3),
84 DDRCTL_REG_REG(crcparctl0),
85 DDRCTL_REG_REG(zqctl0),
86 DDRCTL_REG_REG(dfitmg0),
87 DDRCTL_REG_REG(dfitmg1),
88 DDRCTL_REG_REG(dfilpcfg0),
89 DDRCTL_REG_REG(dfiupd0),
90 DDRCTL_REG_REG(dfiupd1),
91 DDRCTL_REG_REG(dfiupd2),
92 DDRCTL_REG_REG(dfiphymstr),
93 DDRCTL_REG_REG(odtmap),
94 DDRCTL_REG_REG(dbg0),
95 DDRCTL_REG_REG(dbg1),
96 DDRCTL_REG_REG(dbgcmd),
97 DDRCTL_REG_REG(poisoncfg),
98 DDRCTL_REG_REG(pccfg),
99};
100
101#define DDRCTL_REG_TIMING(x) DDRCTL_REG(x, stm32mp1_ddrctrl_timing)
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200102static const struct reg_desc ddr_timing[DDRCTL_REG_TIMING_SIZE] = {
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100103 DDRCTL_REG_TIMING(rfshtmg),
104 DDRCTL_REG_TIMING(dramtmg0),
105 DDRCTL_REG_TIMING(dramtmg1),
106 DDRCTL_REG_TIMING(dramtmg2),
107 DDRCTL_REG_TIMING(dramtmg3),
108 DDRCTL_REG_TIMING(dramtmg4),
109 DDRCTL_REG_TIMING(dramtmg5),
110 DDRCTL_REG_TIMING(dramtmg6),
111 DDRCTL_REG_TIMING(dramtmg7),
112 DDRCTL_REG_TIMING(dramtmg8),
113 DDRCTL_REG_TIMING(dramtmg14),
114 DDRCTL_REG_TIMING(odtcfg),
115};
116
117#define DDRCTL_REG_MAP(x) DDRCTL_REG(x, stm32mp1_ddrctrl_map)
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200118static const struct reg_desc ddr_map[DDRCTL_REG_MAP_SIZE] = {
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100119 DDRCTL_REG_MAP(addrmap1),
120 DDRCTL_REG_MAP(addrmap2),
121 DDRCTL_REG_MAP(addrmap3),
122 DDRCTL_REG_MAP(addrmap4),
123 DDRCTL_REG_MAP(addrmap5),
124 DDRCTL_REG_MAP(addrmap6),
125 DDRCTL_REG_MAP(addrmap9),
126 DDRCTL_REG_MAP(addrmap10),
127 DDRCTL_REG_MAP(addrmap11),
128};
129
130#define DDRCTL_REG_PERF(x) DDRCTL_REG(x, stm32mp1_ddrctrl_perf)
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200131static const struct reg_desc ddr_perf[DDRCTL_REG_PERF_SIZE] = {
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100132 DDRCTL_REG_PERF(sched),
133 DDRCTL_REG_PERF(sched1),
134 DDRCTL_REG_PERF(perfhpr1),
135 DDRCTL_REG_PERF(perflpr1),
136 DDRCTL_REG_PERF(perfwr1),
137 DDRCTL_REG_PERF(pcfgr_0),
138 DDRCTL_REG_PERF(pcfgw_0),
139 DDRCTL_REG_PERF(pcfgqos0_0),
140 DDRCTL_REG_PERF(pcfgqos1_0),
141 DDRCTL_REG_PERF(pcfgwqos0_0),
142 DDRCTL_REG_PERF(pcfgwqos1_0),
143 DDRCTL_REG_PERF(pcfgr_1),
144 DDRCTL_REG_PERF(pcfgw_1),
145 DDRCTL_REG_PERF(pcfgqos0_1),
146 DDRCTL_REG_PERF(pcfgqos1_1),
147 DDRCTL_REG_PERF(pcfgwqos0_1),
148 DDRCTL_REG_PERF(pcfgwqos1_1),
149};
150
151#define DDRPHY_REG_REG(x) DDRPHY_REG(x, stm32mp1_ddrphy_reg)
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200152static const struct reg_desc ddrphy_reg[DDRPHY_REG_REG_SIZE] = {
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100153 DDRPHY_REG_REG(pgcr),
154 DDRPHY_REG_REG(aciocr),
155 DDRPHY_REG_REG(dxccr),
156 DDRPHY_REG_REG(dsgcr),
157 DDRPHY_REG_REG(dcr),
158 DDRPHY_REG_REG(odtcr),
159 DDRPHY_REG_REG(zq0cr1),
160 DDRPHY_REG_REG(dx0gcr),
161 DDRPHY_REG_REG(dx1gcr),
162 DDRPHY_REG_REG(dx2gcr),
163 DDRPHY_REG_REG(dx3gcr),
164};
165
166#define DDRPHY_REG_TIMING(x) DDRPHY_REG(x, stm32mp1_ddrphy_timing)
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200167static const struct reg_desc ddrphy_timing[DDRPHY_REG_TIMING_SIZE] = {
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100168 DDRPHY_REG_TIMING(ptr0),
169 DDRPHY_REG_TIMING(ptr1),
170 DDRPHY_REG_TIMING(ptr2),
171 DDRPHY_REG_TIMING(dtpr0),
172 DDRPHY_REG_TIMING(dtpr1),
173 DDRPHY_REG_TIMING(dtpr2),
174 DDRPHY_REG_TIMING(mr0),
175 DDRPHY_REG_TIMING(mr1),
176 DDRPHY_REG_TIMING(mr2),
177 DDRPHY_REG_TIMING(mr3),
178};
179
Patrick Delaunay01a75102019-04-10 14:09:27 +0200180/**************************************************************
181 * DYNAMIC REGISTERS: only used for debug purpose (read/modify)
182 **************************************************************/
183#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
184static const struct reg_desc ddr_dyn[] = {
185 DDR_REG_DYN(stat),
186 DDR_REG_DYN(init0),
187 DDR_REG_DYN(dfimisc),
188 DDR_REG_DYN(dfistat),
189 DDR_REG_DYN(swctl),
190 DDR_REG_DYN(swstat),
191 DDR_REG_DYN(pctrl_0),
192 DDR_REG_DYN(pctrl_1),
193};
194
195#define DDR_REG_DYN_SIZE ARRAY_SIZE(ddr_dyn)
196
197static const struct reg_desc ddrphy_dyn[] = {
198 DDRPHY_REG_DYN(pir),
199 DDRPHY_REG_DYN(pgsr),
200 DDRPHY_REG_DYN(zq0sr0),
201 DDRPHY_REG_DYN(zq0sr1),
202 DDRPHY_REG_DYN(dx0gsr0),
203 DDRPHY_REG_DYN(dx0gsr1),
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100204 DDRPHY_REG_DYN(dx0dllcr),
205 DDRPHY_REG_DYN(dx0dqtr),
206 DDRPHY_REG_DYN(dx0dqstr),
Patrick Delaunay01a75102019-04-10 14:09:27 +0200207 DDRPHY_REG_DYN(dx1gsr0),
208 DDRPHY_REG_DYN(dx1gsr1),
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100209 DDRPHY_REG_DYN(dx1dllcr),
210 DDRPHY_REG_DYN(dx1dqtr),
211 DDRPHY_REG_DYN(dx1dqstr),
Patrick Delaunay01a75102019-04-10 14:09:27 +0200212 DDRPHY_REG_DYN(dx2gsr0),
213 DDRPHY_REG_DYN(dx2gsr1),
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100214 DDRPHY_REG_DYN(dx2dllcr),
215 DDRPHY_REG_DYN(dx2dqtr),
216 DDRPHY_REG_DYN(dx2dqstr),
Patrick Delaunay01a75102019-04-10 14:09:27 +0200217 DDRPHY_REG_DYN(dx3gsr0),
218 DDRPHY_REG_DYN(dx3gsr1),
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100219 DDRPHY_REG_DYN(dx3dllcr),
220 DDRPHY_REG_DYN(dx3dqtr),
221 DDRPHY_REG_DYN(dx3dqstr),
Patrick Delaunay01a75102019-04-10 14:09:27 +0200222};
223
224#define DDRPHY_REG_DYN_SIZE ARRAY_SIZE(ddrphy_dyn)
225
226#endif
227
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200228/*****************************************************************
229 * REGISTERS ARRAY: used to parse device tree and interactive mode
230 *****************************************************************/
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100231enum reg_type {
232 REG_REG,
233 REG_TIMING,
234 REG_PERF,
235 REG_MAP,
236 REGPHY_REG,
237 REGPHY_TIMING,
Patrick Delaunay01a75102019-04-10 14:09:27 +0200238#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
239/* dynamic registers => managed in driver or not changed,
240 * can be dumped in interactive mode
241 */
242 REG_DYN,
243 REGPHY_DYN,
244#endif
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100245 REG_TYPE_NB
246};
247
248enum base_type {
249 DDR_BASE,
250 DDRPHY_BASE,
251 NONE_BASE
252};
253
254struct ddr_reg_info {
255 const char *name;
256 const struct reg_desc *desc;
257 u8 size;
258 enum base_type base;
259};
260
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100261const struct ddr_reg_info ddr_registers[REG_TYPE_NB] = {
262[REG_REG] = {
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200263 "static", ddr_reg, DDRCTL_REG_REG_SIZE, DDR_BASE},
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100264[REG_TIMING] = {
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200265 "timing", ddr_timing, DDRCTL_REG_TIMING_SIZE, DDR_BASE},
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100266[REG_PERF] = {
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200267 "perf", ddr_perf, DDRCTL_REG_PERF_SIZE, DDR_BASE},
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100268[REG_MAP] = {
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200269 "map", ddr_map, DDRCTL_REG_MAP_SIZE, DDR_BASE},
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100270[REGPHY_REG] = {
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200271 "static", ddrphy_reg, DDRPHY_REG_REG_SIZE, DDRPHY_BASE},
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100272[REGPHY_TIMING] = {
Patrick Delaunay53bb8312019-04-10 14:09:25 +0200273 "timing", ddrphy_timing, DDRPHY_REG_TIMING_SIZE, DDRPHY_BASE},
Patrick Delaunay01a75102019-04-10 14:09:27 +0200274#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
275[REG_DYN] = {
276 "dyn", ddr_dyn, DDR_REG_DYN_SIZE, DDR_BASE},
277[REGPHY_DYN] = {
278 "dyn", ddrphy_dyn, DDRPHY_REG_DYN_SIZE, DDRPHY_BASE},
279#endif
280
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100281};
282
283const char *base_name[] = {
284 [DDR_BASE] = "ctl",
285 [DDRPHY_BASE] = "phy",
286};
287
288static u32 get_base_addr(const struct ddr_info *priv, enum base_type base)
289{
290 if (base == DDRPHY_BASE)
291 return (u32)priv->phy;
292 else
293 return (u32)priv->ctl;
294}
295
296static void set_reg(const struct ddr_info *priv,
297 enum reg_type type,
298 const void *param)
299{
300 unsigned int i;
301 unsigned int *ptr, value;
302 enum base_type base = ddr_registers[type].base;
303 u32 base_addr = get_base_addr(priv, base);
304 const struct reg_desc *desc = ddr_registers[type].desc;
305
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100306 log_debug("init %s\n", ddr_registers[type].name);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100307 for (i = 0; i < ddr_registers[type].size; i++) {
308 ptr = (unsigned int *)(base_addr + desc[i].offset);
309 if (desc[i].par_offset == INVALID_OFFSET) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100310 log_err("invalid parameter offset for %s", desc[i].name);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100311 } else {
312 value = *((u32 *)((u32)param +
313 desc[i].par_offset));
314 writel(value, ptr);
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100315 log_debug("[0x%x] %s= 0x%08x\n",
316 (u32)ptr, desc[i].name, value);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100317 }
318 }
319}
320
Patrick Delaunay01a75102019-04-10 14:09:27 +0200321#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
322static void stm32mp1_dump_reg_desc(u32 base_addr, const struct reg_desc *desc)
323{
324 unsigned int *ptr;
325
326 ptr = (unsigned int *)(base_addr + desc->offset);
327 printf("%s= 0x%08x\n", desc->name, readl(ptr));
328}
329
330static void stm32mp1_dump_param_desc(u32 par_addr, const struct reg_desc *desc)
331{
332 unsigned int *ptr;
333
334 ptr = (unsigned int *)(par_addr + desc->par_offset);
335 printf("%s= 0x%08x\n", desc->name, readl(ptr));
336}
337
338static const struct reg_desc *found_reg(const char *name, enum reg_type *type)
339{
340 unsigned int i, j;
341 const struct reg_desc *desc;
342
343 for (i = 0; i < ARRAY_SIZE(ddr_registers); i++) {
344 desc = ddr_registers[i].desc;
345 for (j = 0; j < ddr_registers[i].size; j++) {
346 if (strcmp(name, desc[j].name) == 0) {
347 *type = i;
348 return &desc[j];
349 }
350 }
351 }
352 *type = REG_TYPE_NB;
353 return NULL;
354}
355
356int stm32mp1_dump_reg(const struct ddr_info *priv,
357 const char *name)
358{
359 unsigned int i, j;
360 const struct reg_desc *desc;
361 u32 base_addr;
362 enum base_type p_base;
363 enum reg_type type;
364 const char *p_name;
365 enum base_type filter = NONE_BASE;
366 int result = -1;
367
368 if (name) {
369 if (strcmp(name, base_name[DDR_BASE]) == 0)
370 filter = DDR_BASE;
371 else if (strcmp(name, base_name[DDRPHY_BASE]) == 0)
372 filter = DDRPHY_BASE;
373 }
374
375 for (i = 0; i < ARRAY_SIZE(ddr_registers); i++) {
376 p_base = ddr_registers[i].base;
377 p_name = ddr_registers[i].name;
378 if (!name || (filter == p_base || !strcmp(name, p_name))) {
379 result = 0;
380 desc = ddr_registers[i].desc;
381 base_addr = get_base_addr(priv, p_base);
382 printf("==%s.%s==\n", base_name[p_base], p_name);
383 for (j = 0; j < ddr_registers[i].size; j++)
384 stm32mp1_dump_reg_desc(base_addr, &desc[j]);
385 }
386 }
387 if (result) {
388 desc = found_reg(name, &type);
389 if (desc) {
390 p_base = ddr_registers[type].base;
391 base_addr = get_base_addr(priv, p_base);
392 stm32mp1_dump_reg_desc(base_addr, desc);
393 result = 0;
394 }
395 }
396 return result;
397}
398
399void stm32mp1_edit_reg(const struct ddr_info *priv,
400 char *name, char *string)
401{
402 unsigned long *ptr, value;
403 enum reg_type type;
404 enum base_type base;
405 const struct reg_desc *desc;
406 u32 base_addr;
407
408 desc = found_reg(name, &type);
409
410 if (!desc) {
411 printf("%s not found\n", name);
412 return;
413 }
414 if (strict_strtoul(string, 16, &value) < 0) {
415 printf("invalid value %s\n", string);
416 return;
417 }
418 base = ddr_registers[type].base;
419 base_addr = get_base_addr(priv, base);
420 ptr = (unsigned long *)(base_addr + desc->offset);
421 writel(value, ptr);
422 printf("%s= 0x%08x\n", desc->name, readl(ptr));
423}
424
425static u32 get_par_addr(const struct stm32mp1_ddr_config *config,
426 enum reg_type type)
427{
428 u32 par_addr = 0x0;
429
430 switch (type) {
431 case REG_REG:
432 par_addr = (u32)&config->c_reg;
433 break;
434 case REG_TIMING:
435 par_addr = (u32)&config->c_timing;
436 break;
437 case REG_PERF:
438 par_addr = (u32)&config->c_perf;
439 break;
440 case REG_MAP:
441 par_addr = (u32)&config->c_map;
442 break;
443 case REGPHY_REG:
444 par_addr = (u32)&config->p_reg;
445 break;
446 case REGPHY_TIMING:
447 par_addr = (u32)&config->p_timing;
448 break;
Patrick Delaunay01a75102019-04-10 14:09:27 +0200449 case REG_DYN:
450 case REGPHY_DYN:
451 case REG_TYPE_NB:
452 par_addr = (u32)NULL;
453 break;
454 }
455
456 return par_addr;
457}
458
459int stm32mp1_dump_param(const struct stm32mp1_ddr_config *config,
460 const char *name)
461{
462 unsigned int i, j;
463 const struct reg_desc *desc;
464 u32 par_addr;
465 enum base_type p_base;
466 enum reg_type type;
467 const char *p_name;
468 enum base_type filter = NONE_BASE;
469 int result = -EINVAL;
470
471 if (name) {
472 if (strcmp(name, base_name[DDR_BASE]) == 0)
473 filter = DDR_BASE;
474 else if (strcmp(name, base_name[DDRPHY_BASE]) == 0)
475 filter = DDRPHY_BASE;
476 }
477
478 for (i = 0; i < ARRAY_SIZE(ddr_registers); i++) {
479 par_addr = get_par_addr(config, i);
480 if (!par_addr)
481 continue;
482 p_base = ddr_registers[i].base;
483 p_name = ddr_registers[i].name;
484 if (!name || (filter == p_base || !strcmp(name, p_name))) {
485 result = 0;
486 desc = ddr_registers[i].desc;
487 printf("==%s.%s==\n", base_name[p_base], p_name);
488 for (j = 0; j < ddr_registers[i].size; j++)
489 stm32mp1_dump_param_desc(par_addr, &desc[j]);
490 }
491 }
492 if (result) {
493 desc = found_reg(name, &type);
494 if (desc) {
495 par_addr = get_par_addr(config, type);
496 if (par_addr) {
497 stm32mp1_dump_param_desc(par_addr, desc);
498 result = 0;
499 }
500 }
501 }
502 return result;
503}
504
505void stm32mp1_edit_param(const struct stm32mp1_ddr_config *config,
506 char *name, char *string)
507{
508 unsigned long *ptr, value;
509 enum reg_type type;
510 const struct reg_desc *desc;
511 u32 par_addr;
512
513 desc = found_reg(name, &type);
514 if (!desc) {
515 printf("%s not found\n", name);
516 return;
517 }
518 if (strict_strtoul(string, 16, &value) < 0) {
519 printf("invalid value %s\n", string);
520 return;
521 }
522 par_addr = get_par_addr(config, type);
523 if (!par_addr) {
524 printf("no parameter %s\n", name);
525 return;
526 }
527 ptr = (unsigned long *)(par_addr + desc->par_offset);
528 writel(value, ptr);
529 printf("%s= 0x%08x\n", desc->name, readl(ptr));
530}
531#endif
532
533__weak bool stm32mp1_ddr_interactive(void *priv,
534 enum stm32mp1_ddr_interact_step step,
535 const struct stm32mp1_ddr_config *config)
536{
537 return false;
538}
539
540#define INTERACTIVE(step)\
541 stm32mp1_ddr_interactive(priv, step, config)
542
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100543static void ddrphy_idone_wait(struct stm32mp1_ddrphy *phy)
544{
545 u32 pgsr;
546 int ret;
547
548 ret = readl_poll_timeout(&phy->pgsr, pgsr,
549 pgsr & (DDRPHYC_PGSR_IDONE |
550 DDRPHYC_PGSR_DTERR |
551 DDRPHYC_PGSR_DTIERR |
552 DDRPHYC_PGSR_DFTERR |
553 DDRPHYC_PGSR_RVERR |
554 DDRPHYC_PGSR_RVEIRR),
555 1000000);
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100556 log_debug("\n[0x%08x] pgsr = 0x%08x ret=%d\n",
557 (u32)&phy->pgsr, pgsr, ret);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100558}
559
560void stm32mp1_ddrphy_init(struct stm32mp1_ddrphy *phy, u32 pir)
561{
562 pir |= DDRPHYC_PIR_INIT;
563 writel(pir, &phy->pir);
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100564 log_debug("[0x%08x] pir = 0x%08x -> 0x%08x\n",
565 (u32)&phy->pir, pir, readl(&phy->pir));
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100566
567 /* need to wait 10 configuration clock before start polling */
568 udelay(10);
569
570 /* Wait DRAM initialization and Gate Training Evaluation complete */
571 ddrphy_idone_wait(phy);
572}
573
574/* start quasi dynamic register update */
575static void start_sw_done(struct stm32mp1_ddrctl *ctl)
576{
577 clrbits_le32(&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
578}
579
580/* wait quasi dynamic register update */
581static void wait_sw_done_ack(struct stm32mp1_ddrctl *ctl)
582{
583 int ret;
584 u32 swstat;
585
586 setbits_le32(&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
587
588 ret = readl_poll_timeout(&ctl->swstat, swstat,
589 swstat & DDRCTRL_SWSTAT_SW_DONE_ACK,
590 1000000);
591 if (ret)
592 panic("Timeout initialising DRAM : DDR->swstat = %x\n",
593 swstat);
594
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100595 log_debug("[0x%08x] swstat = 0x%08x\n", (u32)&ctl->swstat, swstat);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100596}
597
598/* wait quasi dynamic register update */
599static void wait_operating_mode(struct ddr_info *priv, int mode)
600{
601 u32 stat, val, mask, val2 = 0, mask2 = 0;
602 int ret;
603
604 mask = DDRCTRL_STAT_OPERATING_MODE_MASK;
605 val = mode;
606 /* self-refresh due to software => check also STAT.selfref_type */
607 if (mode == DDRCTRL_STAT_OPERATING_MODE_SR) {
608 mask |= DDRCTRL_STAT_SELFREF_TYPE_MASK;
Patrick Delaunayc3ec3702019-04-10 14:09:21 +0200609 val |= DDRCTRL_STAT_SELFREF_TYPE_SR;
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100610 } else if (mode == DDRCTRL_STAT_OPERATING_MODE_NORMAL) {
611 /* normal mode: handle also automatic self refresh */
612 mask2 = DDRCTRL_STAT_OPERATING_MODE_MASK |
613 DDRCTRL_STAT_SELFREF_TYPE_MASK;
614 val2 = DDRCTRL_STAT_OPERATING_MODE_SR |
615 DDRCTRL_STAT_SELFREF_TYPE_ASR;
616 }
617
618 ret = readl_poll_timeout(&priv->ctl->stat, stat,
619 ((stat & mask) == val) ||
620 (mask2 && ((stat & mask2) == val2)),
621 1000000);
622
623 if (ret)
624 panic("Timeout DRAM : DDR->stat = %x\n", stat);
625
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100626 log_debug("[0x%08x] stat = 0x%08x\n", (u32)&priv->ctl->stat, stat);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100627}
628
629void stm32mp1_refresh_disable(struct stm32mp1_ddrctl *ctl)
630{
631 start_sw_done(ctl);
632 /* quasi-dynamic register update*/
633 setbits_le32(&ctl->rfshctl3, DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
Patrick Delaunayb604a412020-03-06 11:14:09 +0100634 clrbits_le32(&ctl->pwrctl, DDRCTRL_PWRCTL_POWERDOWN_EN |
635 DDRCTRL_PWRCTL_SELFREF_EN);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100636 clrbits_le32(&ctl->dfimisc, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
637 wait_sw_done_ack(ctl);
638}
639
640void stm32mp1_refresh_restore(struct stm32mp1_ddrctl *ctl,
641 u32 rfshctl3, u32 pwrctl)
642{
643 start_sw_done(ctl);
644 if (!(rfshctl3 & DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH))
645 clrbits_le32(&ctl->rfshctl3, DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
646 if (pwrctl & DDRCTRL_PWRCTL_POWERDOWN_EN)
647 setbits_le32(&ctl->pwrctl, DDRCTRL_PWRCTL_POWERDOWN_EN);
Patrick Delaunayb604a412020-03-06 11:14:09 +0100648 if ((pwrctl & DDRCTRL_PWRCTL_SELFREF_EN))
649 setbits_le32(&ctl->pwrctl, DDRCTRL_PWRCTL_SELFREF_EN);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100650 setbits_le32(&ctl->dfimisc, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
651 wait_sw_done_ack(ctl);
652}
653
654/* board-specific DDR power initializations. */
Patrick Delaunay1767ac22019-04-10 14:09:26 +0200655__weak int board_ddr_power_init(enum ddr_type ddr_type)
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100656{
657 return 0;
658}
659
660__maybe_unused
661void stm32mp1_ddr_init(struct ddr_info *priv,
662 const struct stm32mp1_ddr_config *config)
663{
664 u32 pir;
Patrick Delaunay1767ac22019-04-10 14:09:26 +0200665 int ret = -EINVAL;
Patrick Delaunaye9a20f82020-03-06 11:14:03 +0100666 char bus_width;
667
668 switch (config->c_reg.mstr & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK) {
669 case DDRCTRL_MSTR_DATA_BUS_WIDTH_QUARTER:
670 bus_width = 8;
671 break;
672 case DDRCTRL_MSTR_DATA_BUS_WIDTH_HALF:
673 bus_width = 16;
674 break;
675 default:
676 bus_width = 32;
677 break;
678 }
679
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100680
Patrick Delaunay1767ac22019-04-10 14:09:26 +0200681 if (config->c_reg.mstr & DDRCTRL_MSTR_DDR3)
682 ret = board_ddr_power_init(STM32MP_DDR3);
Patrick Delaunaye9a20f82020-03-06 11:14:03 +0100683 else if (config->c_reg.mstr & DDRCTRL_MSTR_LPDDR2) {
684 if (bus_width == 32)
685 ret = board_ddr_power_init(STM32MP_LPDDR2_32);
686 else
687 ret = board_ddr_power_init(STM32MP_LPDDR2_16);
688 } else if (config->c_reg.mstr & DDRCTRL_MSTR_LPDDR3) {
689 if (bus_width == 32)
690 ret = board_ddr_power_init(STM32MP_LPDDR3_32);
691 else
692 ret = board_ddr_power_init(STM32MP_LPDDR3_16);
693 }
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100694 if (ret)
695 panic("ddr power init failed\n");
696
Patrick Delaunay01a75102019-04-10 14:09:27 +0200697start:
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100698 log_debug("name = %s\n", config->info.name);
699 log_debug("speed = %d kHz\n", config->info.speed);
700 log_debug("size = 0x%x\n", config->info.size);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100701/*
702 * 1. Program the DWC_ddr_umctl2 registers
703 * 1.1 RESETS: presetn, core_ddrc_rstn, aresetn
704 */
705 /* Assert All DDR part */
706 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
707 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
708 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
709 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
710 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
711 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
712
713/* 1.2. start CLOCK */
714 if (stm32mp1_ddr_clk_enable(priv, config->info.speed))
Patrick Delaunayc60fed12019-04-10 14:09:23 +0200715 panic("invalid DRAM clock : %d kHz\n",
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100716 config->info.speed);
717
718/* 1.3. deassert reset */
719 /* de-assert PHY rstn and ctl_rstn via DPHYRST and DPHYCTLRST */
720 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
721 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
722 /* De-assert presetn once the clocks are active
723 * and stable via DDRCAPBRST bit
724 */
725 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
726
Patrick Delaunay0cb1aa92019-04-10 14:09:22 +0200727/* 1.4. wait 128 cycles to permit initialization of end logic */
728 udelay(2);
729 /* for PCLK = 133MHz => 1 us is enough, 2 to allow lower frequency */
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100730
Patrick Delaunay01a75102019-04-10 14:09:27 +0200731 if (INTERACTIVE(STEP_DDR_RESET))
732 goto start;
733
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100734/* 1.5. initialize registers ddr_umctl2 */
735 /* Stop uMCTL2 before PHY is ready */
736 clrbits_le32(&priv->ctl->dfimisc, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100737 log_debug("[0x%08x] dfimisc = 0x%08x\n",
738 (u32)&priv->ctl->dfimisc, readl(&priv->ctl->dfimisc));
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100739
740 set_reg(priv, REG_REG, &config->c_reg);
741 set_reg(priv, REG_TIMING, &config->c_timing);
742 set_reg(priv, REG_MAP, &config->c_map);
743
744 /* skip CTRL init, SDRAM init is done by PHY PUBL */
745 clrsetbits_le32(&priv->ctl->init0,
746 DDRCTRL_INIT0_SKIP_DRAM_INIT_MASK,
747 DDRCTRL_INIT0_SKIP_DRAM_INIT_NORMAL);
748
749 set_reg(priv, REG_PERF, &config->c_perf);
750
Patrick Delaunay01a75102019-04-10 14:09:27 +0200751 if (INTERACTIVE(STEP_CTL_INIT))
752 goto start;
753
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100754/* 2. deassert reset signal core_ddrc_rstn, aresetn and presetn */
755 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
756 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
757 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
758
759/* 3. start PHY init by accessing relevant PUBL registers
760 * (DXGCR, DCR, PTR*, MR*, DTPR*)
761 */
762 set_reg(priv, REGPHY_REG, &config->p_reg);
763 set_reg(priv, REGPHY_TIMING, &config->p_timing);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100764
Patrick Delaunay01a75102019-04-10 14:09:27 +0200765 if (INTERACTIVE(STEP_PHY_INIT))
766 goto start;
767
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100768/* 4. Monitor PHY init status by polling PUBL register PGSR.IDONE
769 * Perform DDR PHY DRAM initialization and Gate Training Evaluation
770 */
771 ddrphy_idone_wait(priv->phy);
772
773/* 5. Indicate to PUBL that controller performs SDRAM initialization
774 * by setting PIR.INIT and PIR CTLDINIT and pool PGSR.IDONE
775 * DRAM init is done by PHY, init0.skip_dram.init = 1
776 */
777 pir = DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK | DDRPHYC_PIR_ZCAL |
778 DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_DRAMINIT | DDRPHYC_PIR_ICPC;
779
780 if (config->c_reg.mstr & DDRCTRL_MSTR_DDR3)
781 pir |= DDRPHYC_PIR_DRAMRST; /* only for DDR3 */
782
783 stm32mp1_ddrphy_init(priv->phy, pir);
784
785/* 6. SET DFIMISC.dfi_init_complete_en to 1 */
786 /* Enable quasi-dynamic register programming*/
787 start_sw_done(priv->ctl);
788 setbits_le32(&priv->ctl->dfimisc, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
789 wait_sw_done_ack(priv->ctl);
790
791/* 7. Wait for DWC_ddr_umctl2 to move to normal operation mode
792 * by monitoring STAT.operating_mode signal
793 */
794 /* wait uMCTL2 ready */
795
796 wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL);
797
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100798 log_debug("DDR DQS training : ");
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100799/* 8. Disable Auto refresh and power down by setting
800 * - RFSHCTL3.dis_au_refresh = 1
801 * - PWRCTL.powerdown_en = 0
802 * - DFIMISC.dfiinit_complete_en = 0
803 */
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100804 stm32mp1_refresh_disable(priv->ctl);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100805
806/* 9. Program PUBL PGCR to enable refresh during training and rank to train
807 * not done => keep the programed value in PGCR
808 */
809
810/* 10. configure PUBL PIR register to specify which training step to run */
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100811 /* RVTRN is excuted only on LPDDR2/LPDDR3 */
812 if (config->c_reg.mstr & DDRCTRL_MSTR_DDR3)
813 pir = DDRPHYC_PIR_QSTRN;
814 else
815 pir = DDRPHYC_PIR_QSTRN | DDRPHYC_PIR_RVTRN;
816 stm32mp1_ddrphy_init(priv->phy, pir);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100817
818/* 11. monitor PUB PGSR.IDONE to poll cpmpletion of training sequence */
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100819 ddrphy_idone_wait(priv->phy);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100820
821/* 12. set back registers in step 8 to the orginal values if desidered */
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100822 stm32mp1_refresh_restore(priv->ctl, config->c_reg.rfshctl3,
823 config->c_reg.pwrctl);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100824
825 /* enable uMCTL2 AXI port 0 and 1 */
826 setbits_le32(&priv->ctl->pctrl_0, DDRCTRL_PCTRL_N_PORT_EN);
827 setbits_le32(&priv->ctl->pctrl_1, DDRCTRL_PCTRL_N_PORT_EN);
Patrick Delaunay01a75102019-04-10 14:09:27 +0200828
829 if (INTERACTIVE(STEP_DDR_READY))
830 goto start;
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100831}