blob: f10ecdd2ef38bd6910c89ad0810b3e80fac3deea [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay939d5362018-03-12 10:46:11 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay939d5362018-03-12 10:46:11 +01004 */
5
6#include <common.h>
7#include <clk.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Patrick Delaunay939d5362018-03-12 10:46:11 +01009#include <ram.h>
10#include <reset.h>
11#include <timer.h>
12#include <asm/io.h>
13#include <asm/arch/ddr.h>
14#include <linux/iopoll.h>
15#include "stm32mp1_ddr.h"
16#include "stm32mp1_ddr_regs.h"
17
18#define RCC_DDRITFCR 0xD8
19
20#define RCC_DDRITFCR_DDRCAPBRST (BIT(14))
21#define RCC_DDRITFCR_DDRCAXIRST (BIT(15))
22#define RCC_DDRITFCR_DDRCORERST (BIT(16))
23#define RCC_DDRITFCR_DPHYAPBRST (BIT(17))
24#define RCC_DDRITFCR_DPHYRST (BIT(18))
25#define RCC_DDRITFCR_DPHYCTLRST (BIT(19))
26
27struct reg_desc {
28 const char *name;
29 u16 offset; /* offset for base address */
30 u8 par_offset; /* offset for parameter array */
31};
32
33#define INVALID_OFFSET 0xFF
34
35#define DDRCTL_REG(x, y) \
36 {#x,\
37 offsetof(struct stm32mp1_ddrctl, x),\
38 offsetof(struct y, x)}
39
40#define DDRPHY_REG(x, y) \
41 {#x,\
42 offsetof(struct stm32mp1_ddrphy, x),\
43 offsetof(struct y, x)}
44
Patrick Delaunaybbee2702019-04-10 14:09:27 +020045#define DDR_REG_DYN(x) \
46 {#x,\
47 offsetof(struct stm32mp1_ddrctl, x),\
48 INVALID_OFFSET}
49
50#define DDRPHY_REG_DYN(x) \
51 {#x,\
52 offsetof(struct stm32mp1_ddrphy, x),\
53 INVALID_OFFSET}
54
Patrick Delaunayd892d272019-04-10 14:09:25 +020055/***********************************************************
56 * PARAMETERS: value get from device tree :
57 * size / order need to be aligned with binding
58 * modification NOT ALLOWED !!!
59 ***********************************************************/
60#define DDRCTL_REG_REG_SIZE 25 /* st,ctl-reg */
61#define DDRCTL_REG_TIMING_SIZE 12 /* st,ctl-timing */
62#define DDRCTL_REG_MAP_SIZE 9 /* st,ctl-map */
63#define DDRCTL_REG_PERF_SIZE 17 /* st,ctl-perf */
64
65#define DDRPHY_REG_REG_SIZE 11 /* st,phy-reg */
66#define DDRPHY_REG_TIMING_SIZE 10 /* st,phy-timing */
67#define DDRPHY_REG_CAL_SIZE 12 /* st,phy-cal */
68
Patrick Delaunay939d5362018-03-12 10:46:11 +010069#define DDRCTL_REG_REG(x) DDRCTL_REG(x, stm32mp1_ddrctrl_reg)
Patrick Delaunayd892d272019-04-10 14:09:25 +020070static const struct reg_desc ddr_reg[DDRCTL_REG_REG_SIZE] = {
Patrick Delaunay939d5362018-03-12 10:46:11 +010071 DDRCTL_REG_REG(mstr),
72 DDRCTL_REG_REG(mrctrl0),
73 DDRCTL_REG_REG(mrctrl1),
74 DDRCTL_REG_REG(derateen),
75 DDRCTL_REG_REG(derateint),
76 DDRCTL_REG_REG(pwrctl),
77 DDRCTL_REG_REG(pwrtmg),
78 DDRCTL_REG_REG(hwlpctl),
79 DDRCTL_REG_REG(rfshctl0),
80 DDRCTL_REG_REG(rfshctl3),
81 DDRCTL_REG_REG(crcparctl0),
82 DDRCTL_REG_REG(zqctl0),
83 DDRCTL_REG_REG(dfitmg0),
84 DDRCTL_REG_REG(dfitmg1),
85 DDRCTL_REG_REG(dfilpcfg0),
86 DDRCTL_REG_REG(dfiupd0),
87 DDRCTL_REG_REG(dfiupd1),
88 DDRCTL_REG_REG(dfiupd2),
89 DDRCTL_REG_REG(dfiphymstr),
90 DDRCTL_REG_REG(odtmap),
91 DDRCTL_REG_REG(dbg0),
92 DDRCTL_REG_REG(dbg1),
93 DDRCTL_REG_REG(dbgcmd),
94 DDRCTL_REG_REG(poisoncfg),
95 DDRCTL_REG_REG(pccfg),
96};
97
98#define DDRCTL_REG_TIMING(x) DDRCTL_REG(x, stm32mp1_ddrctrl_timing)
Patrick Delaunayd892d272019-04-10 14:09:25 +020099static const struct reg_desc ddr_timing[DDRCTL_REG_TIMING_SIZE] = {
Patrick Delaunay939d5362018-03-12 10:46:11 +0100100 DDRCTL_REG_TIMING(rfshtmg),
101 DDRCTL_REG_TIMING(dramtmg0),
102 DDRCTL_REG_TIMING(dramtmg1),
103 DDRCTL_REG_TIMING(dramtmg2),
104 DDRCTL_REG_TIMING(dramtmg3),
105 DDRCTL_REG_TIMING(dramtmg4),
106 DDRCTL_REG_TIMING(dramtmg5),
107 DDRCTL_REG_TIMING(dramtmg6),
108 DDRCTL_REG_TIMING(dramtmg7),
109 DDRCTL_REG_TIMING(dramtmg8),
110 DDRCTL_REG_TIMING(dramtmg14),
111 DDRCTL_REG_TIMING(odtcfg),
112};
113
114#define DDRCTL_REG_MAP(x) DDRCTL_REG(x, stm32mp1_ddrctrl_map)
Patrick Delaunayd892d272019-04-10 14:09:25 +0200115static const struct reg_desc ddr_map[DDRCTL_REG_MAP_SIZE] = {
Patrick Delaunay939d5362018-03-12 10:46:11 +0100116 DDRCTL_REG_MAP(addrmap1),
117 DDRCTL_REG_MAP(addrmap2),
118 DDRCTL_REG_MAP(addrmap3),
119 DDRCTL_REG_MAP(addrmap4),
120 DDRCTL_REG_MAP(addrmap5),
121 DDRCTL_REG_MAP(addrmap6),
122 DDRCTL_REG_MAP(addrmap9),
123 DDRCTL_REG_MAP(addrmap10),
124 DDRCTL_REG_MAP(addrmap11),
125};
126
127#define DDRCTL_REG_PERF(x) DDRCTL_REG(x, stm32mp1_ddrctrl_perf)
Patrick Delaunayd892d272019-04-10 14:09:25 +0200128static const struct reg_desc ddr_perf[DDRCTL_REG_PERF_SIZE] = {
Patrick Delaunay939d5362018-03-12 10:46:11 +0100129 DDRCTL_REG_PERF(sched),
130 DDRCTL_REG_PERF(sched1),
131 DDRCTL_REG_PERF(perfhpr1),
132 DDRCTL_REG_PERF(perflpr1),
133 DDRCTL_REG_PERF(perfwr1),
134 DDRCTL_REG_PERF(pcfgr_0),
135 DDRCTL_REG_PERF(pcfgw_0),
136 DDRCTL_REG_PERF(pcfgqos0_0),
137 DDRCTL_REG_PERF(pcfgqos1_0),
138 DDRCTL_REG_PERF(pcfgwqos0_0),
139 DDRCTL_REG_PERF(pcfgwqos1_0),
140 DDRCTL_REG_PERF(pcfgr_1),
141 DDRCTL_REG_PERF(pcfgw_1),
142 DDRCTL_REG_PERF(pcfgqos0_1),
143 DDRCTL_REG_PERF(pcfgqos1_1),
144 DDRCTL_REG_PERF(pcfgwqos0_1),
145 DDRCTL_REG_PERF(pcfgwqos1_1),
146};
147
148#define DDRPHY_REG_REG(x) DDRPHY_REG(x, stm32mp1_ddrphy_reg)
Patrick Delaunayd892d272019-04-10 14:09:25 +0200149static const struct reg_desc ddrphy_reg[DDRPHY_REG_REG_SIZE] = {
Patrick Delaunay939d5362018-03-12 10:46:11 +0100150 DDRPHY_REG_REG(pgcr),
151 DDRPHY_REG_REG(aciocr),
152 DDRPHY_REG_REG(dxccr),
153 DDRPHY_REG_REG(dsgcr),
154 DDRPHY_REG_REG(dcr),
155 DDRPHY_REG_REG(odtcr),
156 DDRPHY_REG_REG(zq0cr1),
157 DDRPHY_REG_REG(dx0gcr),
158 DDRPHY_REG_REG(dx1gcr),
159 DDRPHY_REG_REG(dx2gcr),
160 DDRPHY_REG_REG(dx3gcr),
161};
162
163#define DDRPHY_REG_TIMING(x) DDRPHY_REG(x, stm32mp1_ddrphy_timing)
Patrick Delaunayd892d272019-04-10 14:09:25 +0200164static const struct reg_desc ddrphy_timing[DDRPHY_REG_TIMING_SIZE] = {
Patrick Delaunay939d5362018-03-12 10:46:11 +0100165 DDRPHY_REG_TIMING(ptr0),
166 DDRPHY_REG_TIMING(ptr1),
167 DDRPHY_REG_TIMING(ptr2),
168 DDRPHY_REG_TIMING(dtpr0),
169 DDRPHY_REG_TIMING(dtpr1),
170 DDRPHY_REG_TIMING(dtpr2),
171 DDRPHY_REG_TIMING(mr0),
172 DDRPHY_REG_TIMING(mr1),
173 DDRPHY_REG_TIMING(mr2),
174 DDRPHY_REG_TIMING(mr3),
175};
176
177#define DDRPHY_REG_CAL(x) DDRPHY_REG(x, stm32mp1_ddrphy_cal)
Patrick Delaunayd892d272019-04-10 14:09:25 +0200178static const struct reg_desc ddrphy_cal[DDRPHY_REG_CAL_SIZE] = {
Patrick Delaunay939d5362018-03-12 10:46:11 +0100179 DDRPHY_REG_CAL(dx0dllcr),
180 DDRPHY_REG_CAL(dx0dqtr),
181 DDRPHY_REG_CAL(dx0dqstr),
182 DDRPHY_REG_CAL(dx1dllcr),
183 DDRPHY_REG_CAL(dx1dqtr),
184 DDRPHY_REG_CAL(dx1dqstr),
185 DDRPHY_REG_CAL(dx2dllcr),
186 DDRPHY_REG_CAL(dx2dqtr),
187 DDRPHY_REG_CAL(dx2dqstr),
188 DDRPHY_REG_CAL(dx3dllcr),
189 DDRPHY_REG_CAL(dx3dqtr),
190 DDRPHY_REG_CAL(dx3dqstr),
191};
192
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200193/**************************************************************
194 * DYNAMIC REGISTERS: only used for debug purpose (read/modify)
195 **************************************************************/
196#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
197static const struct reg_desc ddr_dyn[] = {
198 DDR_REG_DYN(stat),
199 DDR_REG_DYN(init0),
200 DDR_REG_DYN(dfimisc),
201 DDR_REG_DYN(dfistat),
202 DDR_REG_DYN(swctl),
203 DDR_REG_DYN(swstat),
204 DDR_REG_DYN(pctrl_0),
205 DDR_REG_DYN(pctrl_1),
206};
207
208#define DDR_REG_DYN_SIZE ARRAY_SIZE(ddr_dyn)
209
210static const struct reg_desc ddrphy_dyn[] = {
211 DDRPHY_REG_DYN(pir),
212 DDRPHY_REG_DYN(pgsr),
213 DDRPHY_REG_DYN(zq0sr0),
214 DDRPHY_REG_DYN(zq0sr1),
215 DDRPHY_REG_DYN(dx0gsr0),
216 DDRPHY_REG_DYN(dx0gsr1),
217 DDRPHY_REG_DYN(dx1gsr0),
218 DDRPHY_REG_DYN(dx1gsr1),
219 DDRPHY_REG_DYN(dx2gsr0),
220 DDRPHY_REG_DYN(dx2gsr1),
221 DDRPHY_REG_DYN(dx3gsr0),
222 DDRPHY_REG_DYN(dx3gsr1),
223};
224
225#define DDRPHY_REG_DYN_SIZE ARRAY_SIZE(ddrphy_dyn)
226
227#endif
228
Patrick Delaunayd892d272019-04-10 14:09:25 +0200229/*****************************************************************
230 * REGISTERS ARRAY: used to parse device tree and interactive mode
231 *****************************************************************/
Patrick Delaunay939d5362018-03-12 10:46:11 +0100232enum reg_type {
233 REG_REG,
234 REG_TIMING,
235 REG_PERF,
236 REG_MAP,
237 REGPHY_REG,
238 REGPHY_TIMING,
239 REGPHY_CAL,
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200240#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
241/* dynamic registers => managed in driver or not changed,
242 * can be dumped in interactive mode
243 */
244 REG_DYN,
245 REGPHY_DYN,
246#endif
Patrick Delaunay939d5362018-03-12 10:46:11 +0100247 REG_TYPE_NB
248};
249
250enum base_type {
251 DDR_BASE,
252 DDRPHY_BASE,
253 NONE_BASE
254};
255
256struct ddr_reg_info {
257 const char *name;
258 const struct reg_desc *desc;
259 u8 size;
260 enum base_type base;
261};
262
263#define DDRPHY_REG_CAL(x) DDRPHY_REG(x, stm32mp1_ddrphy_cal)
264
265const struct ddr_reg_info ddr_registers[REG_TYPE_NB] = {
266[REG_REG] = {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200267 "static", ddr_reg, DDRCTL_REG_REG_SIZE, DDR_BASE},
Patrick Delaunay939d5362018-03-12 10:46:11 +0100268[REG_TIMING] = {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200269 "timing", ddr_timing, DDRCTL_REG_TIMING_SIZE, DDR_BASE},
Patrick Delaunay939d5362018-03-12 10:46:11 +0100270[REG_PERF] = {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200271 "perf", ddr_perf, DDRCTL_REG_PERF_SIZE, DDR_BASE},
Patrick Delaunay939d5362018-03-12 10:46:11 +0100272[REG_MAP] = {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200273 "map", ddr_map, DDRCTL_REG_MAP_SIZE, DDR_BASE},
Patrick Delaunay939d5362018-03-12 10:46:11 +0100274[REGPHY_REG] = {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200275 "static", ddrphy_reg, DDRPHY_REG_REG_SIZE, DDRPHY_BASE},
Patrick Delaunay939d5362018-03-12 10:46:11 +0100276[REGPHY_TIMING] = {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200277 "timing", ddrphy_timing, DDRPHY_REG_TIMING_SIZE, DDRPHY_BASE},
Patrick Delaunay939d5362018-03-12 10:46:11 +0100278[REGPHY_CAL] = {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200279 "cal", ddrphy_cal, DDRPHY_REG_CAL_SIZE, DDRPHY_BASE},
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200280#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
281[REG_DYN] = {
282 "dyn", ddr_dyn, DDR_REG_DYN_SIZE, DDR_BASE},
283[REGPHY_DYN] = {
284 "dyn", ddrphy_dyn, DDRPHY_REG_DYN_SIZE, DDRPHY_BASE},
285#endif
286
Patrick Delaunay939d5362018-03-12 10:46:11 +0100287};
288
289const char *base_name[] = {
290 [DDR_BASE] = "ctl",
291 [DDRPHY_BASE] = "phy",
292};
293
294static u32 get_base_addr(const struct ddr_info *priv, enum base_type base)
295{
296 if (base == DDRPHY_BASE)
297 return (u32)priv->phy;
298 else
299 return (u32)priv->ctl;
300}
301
302static void set_reg(const struct ddr_info *priv,
303 enum reg_type type,
304 const void *param)
305{
306 unsigned int i;
307 unsigned int *ptr, value;
308 enum base_type base = ddr_registers[type].base;
309 u32 base_addr = get_base_addr(priv, base);
310 const struct reg_desc *desc = ddr_registers[type].desc;
311
312 debug("init %s\n", ddr_registers[type].name);
313 for (i = 0; i < ddr_registers[type].size; i++) {
314 ptr = (unsigned int *)(base_addr + desc[i].offset);
315 if (desc[i].par_offset == INVALID_OFFSET) {
316 pr_err("invalid parameter offset for %s", desc[i].name);
317 } else {
318 value = *((u32 *)((u32)param +
319 desc[i].par_offset));
320 writel(value, ptr);
321 debug("[0x%x] %s= 0x%08x\n",
322 (u32)ptr, desc[i].name, value);
323 }
324 }
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200325}
326
327#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
328static void stm32mp1_dump_reg_desc(u32 base_addr, const struct reg_desc *desc)
329{
330 unsigned int *ptr;
331
332 ptr = (unsigned int *)(base_addr + desc->offset);
333 printf("%s= 0x%08x\n", desc->name, readl(ptr));
334}
335
336static void stm32mp1_dump_param_desc(u32 par_addr, const struct reg_desc *desc)
337{
338 unsigned int *ptr;
339
340 ptr = (unsigned int *)(par_addr + desc->par_offset);
341 printf("%s= 0x%08x\n", desc->name, readl(ptr));
342}
343
344static const struct reg_desc *found_reg(const char *name, enum reg_type *type)
345{
346 unsigned int i, j;
347 const struct reg_desc *desc;
348
349 for (i = 0; i < ARRAY_SIZE(ddr_registers); i++) {
350 desc = ddr_registers[i].desc;
351 for (j = 0; j < ddr_registers[i].size; j++) {
352 if (strcmp(name, desc[j].name) == 0) {
353 *type = i;
354 return &desc[j];
355 }
356 }
357 }
358 *type = REG_TYPE_NB;
359 return NULL;
360}
361
362int stm32mp1_dump_reg(const struct ddr_info *priv,
363 const char *name)
364{
365 unsigned int i, j;
366 const struct reg_desc *desc;
367 u32 base_addr;
368 enum base_type p_base;
369 enum reg_type type;
370 const char *p_name;
371 enum base_type filter = NONE_BASE;
372 int result = -1;
373
374 if (name) {
375 if (strcmp(name, base_name[DDR_BASE]) == 0)
376 filter = DDR_BASE;
377 else if (strcmp(name, base_name[DDRPHY_BASE]) == 0)
378 filter = DDRPHY_BASE;
379 }
380
381 for (i = 0; i < ARRAY_SIZE(ddr_registers); i++) {
382 p_base = ddr_registers[i].base;
383 p_name = ddr_registers[i].name;
384 if (!name || (filter == p_base || !strcmp(name, p_name))) {
385 result = 0;
386 desc = ddr_registers[i].desc;
387 base_addr = get_base_addr(priv, p_base);
388 printf("==%s.%s==\n", base_name[p_base], p_name);
389 for (j = 0; j < ddr_registers[i].size; j++)
390 stm32mp1_dump_reg_desc(base_addr, &desc[j]);
391 }
392 }
393 if (result) {
394 desc = found_reg(name, &type);
395 if (desc) {
396 p_base = ddr_registers[type].base;
397 base_addr = get_base_addr(priv, p_base);
398 stm32mp1_dump_reg_desc(base_addr, desc);
399 result = 0;
400 }
401 }
402 return result;
Patrick Delaunay939d5362018-03-12 10:46:11 +0100403}
404
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200405void stm32mp1_edit_reg(const struct ddr_info *priv,
406 char *name, char *string)
407{
408 unsigned long *ptr, value;
409 enum reg_type type;
410 enum base_type base;
411 const struct reg_desc *desc;
412 u32 base_addr;
413
414 desc = found_reg(name, &type);
415
416 if (!desc) {
417 printf("%s not found\n", name);
418 return;
419 }
420 if (strict_strtoul(string, 16, &value) < 0) {
421 printf("invalid value %s\n", string);
422 return;
423 }
424 base = ddr_registers[type].base;
425 base_addr = get_base_addr(priv, base);
426 ptr = (unsigned long *)(base_addr + desc->offset);
427 writel(value, ptr);
428 printf("%s= 0x%08x\n", desc->name, readl(ptr));
429}
430
431static u32 get_par_addr(const struct stm32mp1_ddr_config *config,
432 enum reg_type type)
433{
434 u32 par_addr = 0x0;
435
436 switch (type) {
437 case REG_REG:
438 par_addr = (u32)&config->c_reg;
439 break;
440 case REG_TIMING:
441 par_addr = (u32)&config->c_timing;
442 break;
443 case REG_PERF:
444 par_addr = (u32)&config->c_perf;
445 break;
446 case REG_MAP:
447 par_addr = (u32)&config->c_map;
448 break;
449 case REGPHY_REG:
450 par_addr = (u32)&config->p_reg;
451 break;
452 case REGPHY_TIMING:
453 par_addr = (u32)&config->p_timing;
454 break;
455 case REGPHY_CAL:
456 par_addr = (u32)&config->p_cal;
457 break;
458 case REG_DYN:
459 case REGPHY_DYN:
460 case REG_TYPE_NB:
461 par_addr = (u32)NULL;
462 break;
463 }
464
465 return par_addr;
466}
467
468int stm32mp1_dump_param(const struct stm32mp1_ddr_config *config,
469 const char *name)
470{
471 unsigned int i, j;
472 const struct reg_desc *desc;
473 u32 par_addr;
474 enum base_type p_base;
475 enum reg_type type;
476 const char *p_name;
477 enum base_type filter = NONE_BASE;
478 int result = -EINVAL;
479
480 if (name) {
481 if (strcmp(name, base_name[DDR_BASE]) == 0)
482 filter = DDR_BASE;
483 else if (strcmp(name, base_name[DDRPHY_BASE]) == 0)
484 filter = DDRPHY_BASE;
485 }
486
487 for (i = 0; i < ARRAY_SIZE(ddr_registers); i++) {
488 par_addr = get_par_addr(config, i);
489 if (!par_addr)
490 continue;
491 p_base = ddr_registers[i].base;
492 p_name = ddr_registers[i].name;
493 if (!name || (filter == p_base || !strcmp(name, p_name))) {
494 result = 0;
495 desc = ddr_registers[i].desc;
496 printf("==%s.%s==\n", base_name[p_base], p_name);
497 for (j = 0; j < ddr_registers[i].size; j++)
498 stm32mp1_dump_param_desc(par_addr, &desc[j]);
499 }
500 }
501 if (result) {
502 desc = found_reg(name, &type);
503 if (desc) {
504 par_addr = get_par_addr(config, type);
505 if (par_addr) {
506 stm32mp1_dump_param_desc(par_addr, desc);
507 result = 0;
508 }
509 }
510 }
511 return result;
512}
513
514void stm32mp1_edit_param(const struct stm32mp1_ddr_config *config,
515 char *name, char *string)
516{
517 unsigned long *ptr, value;
518 enum reg_type type;
519 const struct reg_desc *desc;
520 u32 par_addr;
521
522 desc = found_reg(name, &type);
523 if (!desc) {
524 printf("%s not found\n", name);
525 return;
526 }
527 if (strict_strtoul(string, 16, &value) < 0) {
528 printf("invalid value %s\n", string);
529 return;
530 }
531 par_addr = get_par_addr(config, type);
532 if (!par_addr) {
533 printf("no parameter %s\n", name);
534 return;
535 }
536 ptr = (unsigned long *)(par_addr + desc->par_offset);
537 writel(value, ptr);
538 printf("%s= 0x%08x\n", desc->name, readl(ptr));
539}
540#endif
541
542__weak bool stm32mp1_ddr_interactive(void *priv,
543 enum stm32mp1_ddr_interact_step step,
544 const struct stm32mp1_ddr_config *config)
545{
546 return false;
547}
548
549#define INTERACTIVE(step)\
550 stm32mp1_ddr_interactive(priv, step, config)
551
Patrick Delaunay939d5362018-03-12 10:46:11 +0100552static void ddrphy_idone_wait(struct stm32mp1_ddrphy *phy)
553{
554 u32 pgsr;
555 int ret;
556
557 ret = readl_poll_timeout(&phy->pgsr, pgsr,
558 pgsr & (DDRPHYC_PGSR_IDONE |
559 DDRPHYC_PGSR_DTERR |
560 DDRPHYC_PGSR_DTIERR |
561 DDRPHYC_PGSR_DFTERR |
562 DDRPHYC_PGSR_RVERR |
563 DDRPHYC_PGSR_RVEIRR),
564 1000000);
565 debug("\n[0x%08x] pgsr = 0x%08x ret=%d\n",
566 (u32)&phy->pgsr, pgsr, ret);
567}
568
569void stm32mp1_ddrphy_init(struct stm32mp1_ddrphy *phy, u32 pir)
570{
571 pir |= DDRPHYC_PIR_INIT;
572 writel(pir, &phy->pir);
573 debug("[0x%08x] pir = 0x%08x -> 0x%08x\n",
574 (u32)&phy->pir, pir, readl(&phy->pir));
575
576 /* need to wait 10 configuration clock before start polling */
577 udelay(10);
578
579 /* Wait DRAM initialization and Gate Training Evaluation complete */
580 ddrphy_idone_wait(phy);
581}
582
583/* start quasi dynamic register update */
584static void start_sw_done(struct stm32mp1_ddrctl *ctl)
585{
586 clrbits_le32(&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
587}
588
589/* wait quasi dynamic register update */
590static void wait_sw_done_ack(struct stm32mp1_ddrctl *ctl)
591{
592 int ret;
593 u32 swstat;
594
595 setbits_le32(&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
596
597 ret = readl_poll_timeout(&ctl->swstat, swstat,
598 swstat & DDRCTRL_SWSTAT_SW_DONE_ACK,
599 1000000);
600 if (ret)
601 panic("Timeout initialising DRAM : DDR->swstat = %x\n",
602 swstat);
603
604 debug("[0x%08x] swstat = 0x%08x\n", (u32)&ctl->swstat, swstat);
605}
606
607/* wait quasi dynamic register update */
608static void wait_operating_mode(struct ddr_info *priv, int mode)
609{
610 u32 stat, val, mask, val2 = 0, mask2 = 0;
611 int ret;
612
613 mask = DDRCTRL_STAT_OPERATING_MODE_MASK;
614 val = mode;
615 /* self-refresh due to software => check also STAT.selfref_type */
616 if (mode == DDRCTRL_STAT_OPERATING_MODE_SR) {
617 mask |= DDRCTRL_STAT_SELFREF_TYPE_MASK;
Patrick Delaunay250bf902019-04-10 14:09:21 +0200618 val |= DDRCTRL_STAT_SELFREF_TYPE_SR;
Patrick Delaunay939d5362018-03-12 10:46:11 +0100619 } else if (mode == DDRCTRL_STAT_OPERATING_MODE_NORMAL) {
620 /* normal mode: handle also automatic self refresh */
621 mask2 = DDRCTRL_STAT_OPERATING_MODE_MASK |
622 DDRCTRL_STAT_SELFREF_TYPE_MASK;
623 val2 = DDRCTRL_STAT_OPERATING_MODE_SR |
624 DDRCTRL_STAT_SELFREF_TYPE_ASR;
625 }
626
627 ret = readl_poll_timeout(&priv->ctl->stat, stat,
628 ((stat & mask) == val) ||
629 (mask2 && ((stat & mask2) == val2)),
630 1000000);
631
632 if (ret)
633 panic("Timeout DRAM : DDR->stat = %x\n", stat);
634
635 debug("[0x%08x] stat = 0x%08x\n", (u32)&priv->ctl->stat, stat);
636}
637
638void stm32mp1_refresh_disable(struct stm32mp1_ddrctl *ctl)
639{
640 start_sw_done(ctl);
641 /* quasi-dynamic register update*/
642 setbits_le32(&ctl->rfshctl3, DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
Patrick Delaunayeca9bc02020-03-06 11:14:09 +0100643 clrbits_le32(&ctl->pwrctl, DDRCTRL_PWRCTL_POWERDOWN_EN |
644 DDRCTRL_PWRCTL_SELFREF_EN);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100645 clrbits_le32(&ctl->dfimisc, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
646 wait_sw_done_ack(ctl);
647}
648
649void stm32mp1_refresh_restore(struct stm32mp1_ddrctl *ctl,
650 u32 rfshctl3, u32 pwrctl)
651{
652 start_sw_done(ctl);
653 if (!(rfshctl3 & DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH))
654 clrbits_le32(&ctl->rfshctl3, DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
655 if (pwrctl & DDRCTRL_PWRCTL_POWERDOWN_EN)
656 setbits_le32(&ctl->pwrctl, DDRCTRL_PWRCTL_POWERDOWN_EN);
Patrick Delaunayeca9bc02020-03-06 11:14:09 +0100657 if ((pwrctl & DDRCTRL_PWRCTL_SELFREF_EN))
658 setbits_le32(&ctl->pwrctl, DDRCTRL_PWRCTL_SELFREF_EN);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100659 setbits_le32(&ctl->dfimisc, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
660 wait_sw_done_ack(ctl);
661}
662
663/* board-specific DDR power initializations. */
Patrick Delaunay4175f452019-04-10 14:09:26 +0200664__weak int board_ddr_power_init(enum ddr_type ddr_type)
Patrick Delaunay939d5362018-03-12 10:46:11 +0100665{
666 return 0;
667}
668
669__maybe_unused
670void stm32mp1_ddr_init(struct ddr_info *priv,
671 const struct stm32mp1_ddr_config *config)
672{
673 u32 pir;
Patrick Delaunay4175f452019-04-10 14:09:26 +0200674 int ret = -EINVAL;
Patrick Delaunay2ebc2112020-03-06 11:14:03 +0100675 char bus_width;
676
677 switch (config->c_reg.mstr & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK) {
678 case DDRCTRL_MSTR_DATA_BUS_WIDTH_QUARTER:
679 bus_width = 8;
680 break;
681 case DDRCTRL_MSTR_DATA_BUS_WIDTH_HALF:
682 bus_width = 16;
683 break;
684 default:
685 bus_width = 32;
686 break;
687 }
688
Patrick Delaunay939d5362018-03-12 10:46:11 +0100689
Patrick Delaunay4175f452019-04-10 14:09:26 +0200690 if (config->c_reg.mstr & DDRCTRL_MSTR_DDR3)
691 ret = board_ddr_power_init(STM32MP_DDR3);
Patrick Delaunay2ebc2112020-03-06 11:14:03 +0100692 else if (config->c_reg.mstr & DDRCTRL_MSTR_LPDDR2) {
693 if (bus_width == 32)
694 ret = board_ddr_power_init(STM32MP_LPDDR2_32);
695 else
696 ret = board_ddr_power_init(STM32MP_LPDDR2_16);
697 } else if (config->c_reg.mstr & DDRCTRL_MSTR_LPDDR3) {
698 if (bus_width == 32)
699 ret = board_ddr_power_init(STM32MP_LPDDR3_32);
700 else
701 ret = board_ddr_power_init(STM32MP_LPDDR3_16);
702 }
Patrick Delaunay939d5362018-03-12 10:46:11 +0100703 if (ret)
704 panic("ddr power init failed\n");
705
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200706start:
Patrick Delaunay939d5362018-03-12 10:46:11 +0100707 debug("name = %s\n", config->info.name);
Patrick Delaunay29e1a942019-04-10 14:09:23 +0200708 debug("speed = %d kHz\n", config->info.speed);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100709 debug("size = 0x%x\n", config->info.size);
710/*
711 * 1. Program the DWC_ddr_umctl2 registers
712 * 1.1 RESETS: presetn, core_ddrc_rstn, aresetn
713 */
714 /* Assert All DDR part */
715 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
716 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
717 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
718 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
719 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
720 setbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
721
722/* 1.2. start CLOCK */
723 if (stm32mp1_ddr_clk_enable(priv, config->info.speed))
Patrick Delaunay29e1a942019-04-10 14:09:23 +0200724 panic("invalid DRAM clock : %d kHz\n",
Patrick Delaunay939d5362018-03-12 10:46:11 +0100725 config->info.speed);
726
727/* 1.3. deassert reset */
728 /* de-assert PHY rstn and ctl_rstn via DPHYRST and DPHYCTLRST */
729 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
730 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
731 /* De-assert presetn once the clocks are active
732 * and stable via DDRCAPBRST bit
733 */
734 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
735
Patrick Delaunay1f987042019-04-10 14:09:22 +0200736/* 1.4. wait 128 cycles to permit initialization of end logic */
737 udelay(2);
738 /* for PCLK = 133MHz => 1 us is enough, 2 to allow lower frequency */
Patrick Delaunay939d5362018-03-12 10:46:11 +0100739
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200740 if (INTERACTIVE(STEP_DDR_RESET))
741 goto start;
742
Patrick Delaunay939d5362018-03-12 10:46:11 +0100743/* 1.5. initialize registers ddr_umctl2 */
744 /* Stop uMCTL2 before PHY is ready */
745 clrbits_le32(&priv->ctl->dfimisc, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
746 debug("[0x%08x] dfimisc = 0x%08x\n",
747 (u32)&priv->ctl->dfimisc, readl(&priv->ctl->dfimisc));
748
749 set_reg(priv, REG_REG, &config->c_reg);
750 set_reg(priv, REG_TIMING, &config->c_timing);
751 set_reg(priv, REG_MAP, &config->c_map);
752
753 /* skip CTRL init, SDRAM init is done by PHY PUBL */
754 clrsetbits_le32(&priv->ctl->init0,
755 DDRCTRL_INIT0_SKIP_DRAM_INIT_MASK,
756 DDRCTRL_INIT0_SKIP_DRAM_INIT_NORMAL);
757
758 set_reg(priv, REG_PERF, &config->c_perf);
759
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200760 if (INTERACTIVE(STEP_CTL_INIT))
761 goto start;
762
Patrick Delaunay939d5362018-03-12 10:46:11 +0100763/* 2. deassert reset signal core_ddrc_rstn, aresetn and presetn */
764 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
765 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
766 clrbits_le32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
767
768/* 3. start PHY init by accessing relevant PUBL registers
769 * (DXGCR, DCR, PTR*, MR*, DTPR*)
770 */
771 set_reg(priv, REGPHY_REG, &config->p_reg);
772 set_reg(priv, REGPHY_TIMING, &config->p_timing);
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100773 if (config->p_cal_present)
774 set_reg(priv, REGPHY_CAL, &config->p_cal);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100775
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200776 if (INTERACTIVE(STEP_PHY_INIT))
777 goto start;
778
Patrick Delaunay939d5362018-03-12 10:46:11 +0100779/* 4. Monitor PHY init status by polling PUBL register PGSR.IDONE
780 * Perform DDR PHY DRAM initialization and Gate Training Evaluation
781 */
782 ddrphy_idone_wait(priv->phy);
783
784/* 5. Indicate to PUBL that controller performs SDRAM initialization
785 * by setting PIR.INIT and PIR CTLDINIT and pool PGSR.IDONE
786 * DRAM init is done by PHY, init0.skip_dram.init = 1
787 */
788 pir = DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK | DDRPHYC_PIR_ZCAL |
789 DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_DRAMINIT | DDRPHYC_PIR_ICPC;
790
791 if (config->c_reg.mstr & DDRCTRL_MSTR_DDR3)
792 pir |= DDRPHYC_PIR_DRAMRST; /* only for DDR3 */
793
794 stm32mp1_ddrphy_init(priv->phy, pir);
795
796/* 6. SET DFIMISC.dfi_init_complete_en to 1 */
797 /* Enable quasi-dynamic register programming*/
798 start_sw_done(priv->ctl);
799 setbits_le32(&priv->ctl->dfimisc, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
800 wait_sw_done_ack(priv->ctl);
801
802/* 7. Wait for DWC_ddr_umctl2 to move to normal operation mode
803 * by monitoring STAT.operating_mode signal
804 */
805 /* wait uMCTL2 ready */
806
807 wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL);
808
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100809 if (config->p_cal_present) {
810 debug("DDR DQS training skipped.\n");
811 } else {
812 debug("DDR DQS training : ");
Patrick Delaunay939d5362018-03-12 10:46:11 +0100813/* 8. Disable Auto refresh and power down by setting
814 * - RFSHCTL3.dis_au_refresh = 1
815 * - PWRCTL.powerdown_en = 0
816 * - DFIMISC.dfiinit_complete_en = 0
817 */
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100818 stm32mp1_refresh_disable(priv->ctl);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100819
820/* 9. Program PUBL PGCR to enable refresh during training and rank to train
821 * not done => keep the programed value in PGCR
822 */
823
824/* 10. configure PUBL PIR register to specify which training step to run */
825 /* warning : RVTRN is not supported by this PUBL */
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100826 stm32mp1_ddrphy_init(priv->phy, DDRPHYC_PIR_QSTRN);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100827
828/* 11. monitor PUB PGSR.IDONE to poll cpmpletion of training sequence */
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100829 ddrphy_idone_wait(priv->phy);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100830
831/* 12. set back registers in step 8 to the orginal values if desidered */
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100832 stm32mp1_refresh_restore(priv->ctl, config->c_reg.rfshctl3,
833 config->c_reg.pwrctl);
834 } /* if (config->p_cal_present) */
Patrick Delaunay939d5362018-03-12 10:46:11 +0100835
836 /* enable uMCTL2 AXI port 0 and 1 */
837 setbits_le32(&priv->ctl->pctrl_0, DDRCTRL_PCTRL_N_PORT_EN);
838 setbits_le32(&priv->ctl->pctrl_1, DDRCTRL_PCTRL_N_PORT_EN);
Patrick Delaunaybbee2702019-04-10 14:09:27 +0200839
840 if (INTERACTIVE(STEP_DDR_READY))
841 goto start;
Patrick Delaunay939d5362018-03-12 10:46:11 +0100842}