blob: 415d9e40f70c26b32dcabe4a4043d53e1008604d [file] [log] [blame]
Yann Gautiercaf575b2018-07-24 17:18:19 +02001/*
Yann Gautier2b855782024-05-29 15:34:45 +02002 * Copyright (C) 2018-2024, STMicroelectronics - All Rights Reserved
Yann Gautiercaf575b2018-07-24 17:18:19 +02003 *
4 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
5 */
6
Yann Gautier1a3fc9f2019-01-17 14:35:22 +01007#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <stddef.h>
9
Yann Gautiercaf575b2018-07-24 17:18:19 +020010#include <arch.h>
11#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/debug.h>
Yann Gautiera205a5c2021-08-30 15:06:54 +020013#include <drivers/clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <drivers/delay_timer.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <drivers/st/stm32mp1_ddr.h>
16#include <drivers/st/stm32mp1_ddr_regs.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <drivers/st/stm32mp1_pwr.h>
18#include <drivers/st/stm32mp1_ram.h>
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020019#include <drivers/st/stm32mp_ddr.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <lib/mmio.h>
21#include <plat/common/platform.h>
22
Yann Gautier634591d2021-09-07 09:07:35 +020023#include <platform_def.h>
24
Yann Gautiercaf575b2018-07-24 17:18:19 +020025#define DDRCTL_REG(x, y) \
26 { \
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020027 .offset = offsetof(struct stm32mp_ddrctl, x), \
Yann Gautiercaf575b2018-07-24 17:18:19 +020028 .par_offset = offsetof(struct y, x) \
29 }
30
31#define DDRPHY_REG(x, y) \
32 { \
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020033 .offset = offsetof(struct stm32mp_ddrphy, x), \
Yann Gautiercaf575b2018-07-24 17:18:19 +020034 .par_offset = offsetof(struct y, x) \
35 }
36
Yann Gautier39d85f42019-02-25 13:44:27 +010037/*
38 * PARAMETERS: value get from device tree :
39 * size / order need to be aligned with binding
40 * modification NOT ALLOWED !!!
41 */
42#define DDRCTL_REG_REG_SIZE 25 /* st,ctl-reg */
43#define DDRCTL_REG_TIMING_SIZE 12 /* st,ctl-timing */
44#define DDRCTL_REG_MAP_SIZE 9 /* st,ctl-map */
Yann Gautier6d8c2442020-09-17 12:42:46 +020045#if STM32MP_DDR_DUAL_AXI_PORT
Yann Gautier39d85f42019-02-25 13:44:27 +010046#define DDRCTL_REG_PERF_SIZE 17 /* st,ctl-perf */
Yann Gautier6d8c2442020-09-17 12:42:46 +020047#else
48#define DDRCTL_REG_PERF_SIZE 11 /* st,ctl-perf */
49#endif
Yann Gautier39d85f42019-02-25 13:44:27 +010050
Yann Gautier6d8c2442020-09-17 12:42:46 +020051#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautier39d85f42019-02-25 13:44:27 +010052#define DDRPHY_REG_REG_SIZE 11 /* st,phy-reg */
Yann Gautier6d8c2442020-09-17 12:42:46 +020053#else
54#define DDRPHY_REG_REG_SIZE 9 /* st,phy-reg */
55#endif
Yann Gautier39d85f42019-02-25 13:44:27 +010056#define DDRPHY_REG_TIMING_SIZE 10 /* st,phy-timing */
57
Yann Gautiercaf575b2018-07-24 17:18:19 +020058#define DDRCTL_REG_REG(x) DDRCTL_REG(x, stm32mp1_ddrctrl_reg)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020059static const struct stm32mp_ddr_reg_desc ddr_reg[DDRCTL_REG_REG_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +020060 DDRCTL_REG_REG(mstr),
61 DDRCTL_REG_REG(mrctrl0),
62 DDRCTL_REG_REG(mrctrl1),
63 DDRCTL_REG_REG(derateen),
64 DDRCTL_REG_REG(derateint),
65 DDRCTL_REG_REG(pwrctl),
66 DDRCTL_REG_REG(pwrtmg),
67 DDRCTL_REG_REG(hwlpctl),
68 DDRCTL_REG_REG(rfshctl0),
69 DDRCTL_REG_REG(rfshctl3),
70 DDRCTL_REG_REG(crcparctl0),
71 DDRCTL_REG_REG(zqctl0),
72 DDRCTL_REG_REG(dfitmg0),
73 DDRCTL_REG_REG(dfitmg1),
74 DDRCTL_REG_REG(dfilpcfg0),
75 DDRCTL_REG_REG(dfiupd0),
76 DDRCTL_REG_REG(dfiupd1),
77 DDRCTL_REG_REG(dfiupd2),
78 DDRCTL_REG_REG(dfiphymstr),
79 DDRCTL_REG_REG(odtmap),
80 DDRCTL_REG_REG(dbg0),
81 DDRCTL_REG_REG(dbg1),
82 DDRCTL_REG_REG(dbgcmd),
83 DDRCTL_REG_REG(poisoncfg),
84 DDRCTL_REG_REG(pccfg),
85};
86
87#define DDRCTL_REG_TIMING(x) DDRCTL_REG(x, stm32mp1_ddrctrl_timing)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020088static const struct stm32mp_ddr_reg_desc ddr_timing[DDRCTL_REG_TIMING_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +020089 DDRCTL_REG_TIMING(rfshtmg),
90 DDRCTL_REG_TIMING(dramtmg0),
91 DDRCTL_REG_TIMING(dramtmg1),
92 DDRCTL_REG_TIMING(dramtmg2),
93 DDRCTL_REG_TIMING(dramtmg3),
94 DDRCTL_REG_TIMING(dramtmg4),
95 DDRCTL_REG_TIMING(dramtmg5),
96 DDRCTL_REG_TIMING(dramtmg6),
97 DDRCTL_REG_TIMING(dramtmg7),
98 DDRCTL_REG_TIMING(dramtmg8),
99 DDRCTL_REG_TIMING(dramtmg14),
100 DDRCTL_REG_TIMING(odtcfg),
101};
102
103#define DDRCTL_REG_MAP(x) DDRCTL_REG(x, stm32mp1_ddrctrl_map)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200104static const struct stm32mp_ddr_reg_desc ddr_map[DDRCTL_REG_MAP_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200105 DDRCTL_REG_MAP(addrmap1),
106 DDRCTL_REG_MAP(addrmap2),
107 DDRCTL_REG_MAP(addrmap3),
108 DDRCTL_REG_MAP(addrmap4),
109 DDRCTL_REG_MAP(addrmap5),
110 DDRCTL_REG_MAP(addrmap6),
111 DDRCTL_REG_MAP(addrmap9),
112 DDRCTL_REG_MAP(addrmap10),
113 DDRCTL_REG_MAP(addrmap11),
114};
115
116#define DDRCTL_REG_PERF(x) DDRCTL_REG(x, stm32mp1_ddrctrl_perf)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200117static const struct stm32mp_ddr_reg_desc ddr_perf[DDRCTL_REG_PERF_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200118 DDRCTL_REG_PERF(sched),
119 DDRCTL_REG_PERF(sched1),
120 DDRCTL_REG_PERF(perfhpr1),
121 DDRCTL_REG_PERF(perflpr1),
122 DDRCTL_REG_PERF(perfwr1),
123 DDRCTL_REG_PERF(pcfgr_0),
124 DDRCTL_REG_PERF(pcfgw_0),
125 DDRCTL_REG_PERF(pcfgqos0_0),
126 DDRCTL_REG_PERF(pcfgqos1_0),
127 DDRCTL_REG_PERF(pcfgwqos0_0),
128 DDRCTL_REG_PERF(pcfgwqos1_0),
Yann Gautier6d8c2442020-09-17 12:42:46 +0200129#if STM32MP_DDR_DUAL_AXI_PORT
Yann Gautiercaf575b2018-07-24 17:18:19 +0200130 DDRCTL_REG_PERF(pcfgr_1),
131 DDRCTL_REG_PERF(pcfgw_1),
132 DDRCTL_REG_PERF(pcfgqos0_1),
133 DDRCTL_REG_PERF(pcfgqos1_1),
134 DDRCTL_REG_PERF(pcfgwqos0_1),
135 DDRCTL_REG_PERF(pcfgwqos1_1),
Yann Gautier6d8c2442020-09-17 12:42:46 +0200136#endif
Yann Gautiercaf575b2018-07-24 17:18:19 +0200137};
138
139#define DDRPHY_REG_REG(x) DDRPHY_REG(x, stm32mp1_ddrphy_reg)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200140static const struct stm32mp_ddr_reg_desc ddrphy_reg[DDRPHY_REG_REG_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200141 DDRPHY_REG_REG(pgcr),
142 DDRPHY_REG_REG(aciocr),
143 DDRPHY_REG_REG(dxccr),
144 DDRPHY_REG_REG(dsgcr),
145 DDRPHY_REG_REG(dcr),
146 DDRPHY_REG_REG(odtcr),
147 DDRPHY_REG_REG(zq0cr1),
148 DDRPHY_REG_REG(dx0gcr),
149 DDRPHY_REG_REG(dx1gcr),
Yann Gautier6d8c2442020-09-17 12:42:46 +0200150#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200151 DDRPHY_REG_REG(dx2gcr),
152 DDRPHY_REG_REG(dx3gcr),
Yann Gautier6d8c2442020-09-17 12:42:46 +0200153#endif
Yann Gautiercaf575b2018-07-24 17:18:19 +0200154};
155
156#define DDRPHY_REG_TIMING(x) DDRPHY_REG(x, stm32mp1_ddrphy_timing)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200157static const struct stm32mp_ddr_reg_desc ddrphy_timing[DDRPHY_REG_TIMING_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200158 DDRPHY_REG_TIMING(ptr0),
159 DDRPHY_REG_TIMING(ptr1),
160 DDRPHY_REG_TIMING(ptr2),
161 DDRPHY_REG_TIMING(dtpr0),
162 DDRPHY_REG_TIMING(dtpr1),
163 DDRPHY_REG_TIMING(dtpr2),
164 DDRPHY_REG_TIMING(mr0),
165 DDRPHY_REG_TIMING(mr1),
166 DDRPHY_REG_TIMING(mr2),
167 DDRPHY_REG_TIMING(mr3),
168};
169
Yann Gautier39d85f42019-02-25 13:44:27 +0100170/*
171 * REGISTERS ARRAY: used to parse device tree and interactive mode
172 */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200173static const struct stm32mp_ddr_reg_info ddr_registers[REG_TYPE_NB] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200174 [REG_REG] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100175 .name = "static",
176 .desc = ddr_reg,
Yann Gautier39d85f42019-02-25 13:44:27 +0100177 .size = DDRCTL_REG_REG_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100178 .base = DDR_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200179 },
180 [REG_TIMING] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100181 .name = "timing",
182 .desc = ddr_timing,
Yann Gautier39d85f42019-02-25 13:44:27 +0100183 .size = DDRCTL_REG_TIMING_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100184 .base = DDR_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200185 },
186 [REG_PERF] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100187 .name = "perf",
188 .desc = ddr_perf,
Yann Gautier39d85f42019-02-25 13:44:27 +0100189 .size = DDRCTL_REG_PERF_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100190 .base = DDR_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200191 },
192 [REG_MAP] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100193 .name = "map",
194 .desc = ddr_map,
Yann Gautier39d85f42019-02-25 13:44:27 +0100195 .size = DDRCTL_REG_MAP_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100196 .base = DDR_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200197 },
198 [REGPHY_REG] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100199 .name = "static",
200 .desc = ddrphy_reg,
Yann Gautier39d85f42019-02-25 13:44:27 +0100201 .size = DDRPHY_REG_REG_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100202 .base = DDRPHY_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200203 },
204 [REGPHY_TIMING] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100205 .name = "timing",
206 .desc = ddrphy_timing,
Yann Gautier39d85f42019-02-25 13:44:27 +0100207 .size = DDRPHY_REG_TIMING_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100208 .base = DDRPHY_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200209 },
210};
211
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200212static void stm32mp1_ddrphy_idone_wait(struct stm32mp_ddrphy *phy)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200213{
214 uint32_t pgsr;
215 int error = 0;
Yann Gautier2b855782024-05-29 15:34:45 +0200216 uint64_t timeout = timeout_init_us(DDR_TIMEOUT_US_1S);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200217
218 do {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100219 pgsr = mmio_read_32((uintptr_t)&phy->pgsr);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200220
Yann Gautier2299d572019-02-14 11:14:39 +0100221 VERBOSE(" > [0x%lx] pgsr = 0x%x &\n",
222 (uintptr_t)&phy->pgsr, pgsr);
223
224 if (timeout_elapsed(timeout)) {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200225 panic();
226 }
Yann Gautier2299d572019-02-14 11:14:39 +0100227
Yann Gautiercaf575b2018-07-24 17:18:19 +0200228 if ((pgsr & DDRPHYC_PGSR_DTERR) != 0U) {
229 VERBOSE("DQS Gate Trainig Error\n");
230 error++;
231 }
Yann Gautier2299d572019-02-14 11:14:39 +0100232
Yann Gautiercaf575b2018-07-24 17:18:19 +0200233 if ((pgsr & DDRPHYC_PGSR_DTIERR) != 0U) {
234 VERBOSE("DQS Gate Trainig Intermittent Error\n");
235 error++;
236 }
Yann Gautier2299d572019-02-14 11:14:39 +0100237
Yann Gautiercaf575b2018-07-24 17:18:19 +0200238 if ((pgsr & DDRPHYC_PGSR_DFTERR) != 0U) {
239 VERBOSE("DQS Drift Error\n");
240 error++;
241 }
Yann Gautier2299d572019-02-14 11:14:39 +0100242
Yann Gautiercaf575b2018-07-24 17:18:19 +0200243 if ((pgsr & DDRPHYC_PGSR_RVERR) != 0U) {
244 VERBOSE("Read Valid Training Error\n");
245 error++;
246 }
Yann Gautier2299d572019-02-14 11:14:39 +0100247
Yann Gautiercaf575b2018-07-24 17:18:19 +0200248 if ((pgsr & DDRPHYC_PGSR_RVEIRR) != 0U) {
249 VERBOSE("Read Valid Training Intermittent Error\n");
250 error++;
251 }
Yann Gautier2299d572019-02-14 11:14:39 +0100252 } while (((pgsr & DDRPHYC_PGSR_IDONE) == 0U) && (error == 0));
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100253 VERBOSE("\n[0x%lx] pgsr = 0x%x\n",
254 (uintptr_t)&phy->pgsr, pgsr);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200255}
256
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200257static void stm32mp1_ddrphy_init(struct stm32mp_ddrphy *phy, uint32_t pir)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200258{
259 uint32_t pir_init = pir | DDRPHYC_PIR_INIT;
260
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100261 mmio_write_32((uintptr_t)&phy->pir, pir_init);
262 VERBOSE("[0x%lx] pir = 0x%x -> 0x%x\n",
263 (uintptr_t)&phy->pir, pir_init,
264 mmio_read_32((uintptr_t)&phy->pir));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200265
266 /* Need to wait 10 configuration clock before start polling */
Yann Gautier2b855782024-05-29 15:34:45 +0200267 udelay(DDR_DELAY_10US);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200268
269 /* Wait DRAM initialization and Gate Training Evaluation complete */
270 stm32mp1_ddrphy_idone_wait(phy);
271}
272
Yann Gautiercaf575b2018-07-24 17:18:19 +0200273/* Wait quasi dynamic register update */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200274static void stm32mp1_wait_operating_mode(struct stm32mp_ddr_priv *priv, uint32_t mode)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200275{
Yann Gautier2299d572019-02-14 11:14:39 +0100276 uint64_t timeout;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200277 uint32_t stat;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200278 int break_loop = 0;
279
Yann Gautier2b855782024-05-29 15:34:45 +0200280 timeout = timeout_init_us(DDR_TIMEOUT_US_1S);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200281 for ( ; ; ) {
Yann Gautier2299d572019-02-14 11:14:39 +0100282 uint32_t operating_mode;
283 uint32_t selref_type;
284
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100285 stat = mmio_read_32((uintptr_t)&priv->ctl->stat);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200286 operating_mode = stat & DDRCTRL_STAT_OPERATING_MODE_MASK;
287 selref_type = stat & DDRCTRL_STAT_SELFREF_TYPE_MASK;
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100288 VERBOSE("[0x%lx] stat = 0x%x\n",
289 (uintptr_t)&priv->ctl->stat, stat);
Yann Gautier2299d572019-02-14 11:14:39 +0100290 if (timeout_elapsed(timeout)) {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200291 panic();
292 }
293
294 if (mode == DDRCTRL_STAT_OPERATING_MODE_SR) {
295 /*
296 * Self-refresh due to software
297 * => checking also STAT.selfref_type.
298 */
299 if ((operating_mode ==
300 DDRCTRL_STAT_OPERATING_MODE_SR) &&
301 (selref_type == DDRCTRL_STAT_SELFREF_TYPE_SR)) {
302 break_loop = 1;
303 }
304 } else if (operating_mode == mode) {
305 break_loop = 1;
306 } else if ((mode == DDRCTRL_STAT_OPERATING_MODE_NORMAL) &&
307 (operating_mode == DDRCTRL_STAT_OPERATING_MODE_SR) &&
308 (selref_type == DDRCTRL_STAT_SELFREF_TYPE_ASR)) {
309 /* Normal mode: handle also automatic self refresh */
310 break_loop = 1;
311 }
312
313 if (break_loop == 1) {
314 break;
315 }
316 }
317
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100318 VERBOSE("[0x%lx] stat = 0x%x\n",
319 (uintptr_t)&priv->ctl->stat, stat);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200320}
321
322/* Mode Register Writes (MRW or MRS) */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200323static void stm32mp1_mode_register_write(struct stm32mp_ddr_priv *priv, uint8_t addr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200324 uint32_t data)
325{
326 uint32_t mrctrl0;
327
328 VERBOSE("MRS: %d = %x\n", addr, data);
329
330 /*
331 * 1. Poll MRSTAT.mr_wr_busy until it is '0'.
332 * This checks that there is no outstanding MR transaction.
333 * No write should be performed to MRCTRL0 and MRCTRL1
334 * if MRSTAT.mr_wr_busy = 1.
335 */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100336 while ((mmio_read_32((uintptr_t)&priv->ctl->mrstat) &
Yann Gautiercaf575b2018-07-24 17:18:19 +0200337 DDRCTRL_MRSTAT_MR_WR_BUSY) != 0U) {
338 ;
339 }
340
341 /*
342 * 2. Write the MRCTRL0.mr_type, MRCTRL0.mr_addr, MRCTRL0.mr_rank
343 * and (for MRWs) MRCTRL1.mr_data to define the MR transaction.
344 */
345 mrctrl0 = DDRCTRL_MRCTRL0_MR_TYPE_WRITE |
346 DDRCTRL_MRCTRL0_MR_RANK_ALL |
347 (((uint32_t)addr << DDRCTRL_MRCTRL0_MR_ADDR_SHIFT) &
348 DDRCTRL_MRCTRL0_MR_ADDR_MASK);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100349 mmio_write_32((uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
350 VERBOSE("[0x%lx] mrctrl0 = 0x%x (0x%x)\n",
351 (uintptr_t)&priv->ctl->mrctrl0,
352 mmio_read_32((uintptr_t)&priv->ctl->mrctrl0), mrctrl0);
353 mmio_write_32((uintptr_t)&priv->ctl->mrctrl1, data);
354 VERBOSE("[0x%lx] mrctrl1 = 0x%x\n",
355 (uintptr_t)&priv->ctl->mrctrl1,
356 mmio_read_32((uintptr_t)&priv->ctl->mrctrl1));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200357
358 /*
359 * 3. In a separate APB transaction, write the MRCTRL0.mr_wr to 1. This
360 * bit is self-clearing, and triggers the MR transaction.
361 * The uMCTL2 then asserts the MRSTAT.mr_wr_busy while it performs
362 * the MR transaction to SDRAM, and no further access can be
363 * initiated until it is deasserted.
364 */
365 mrctrl0 |= DDRCTRL_MRCTRL0_MR_WR;
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100366 mmio_write_32((uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200367
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100368 while ((mmio_read_32((uintptr_t)&priv->ctl->mrstat) &
Yann Gautiercaf575b2018-07-24 17:18:19 +0200369 DDRCTRL_MRSTAT_MR_WR_BUSY) != 0U) {
370 ;
371 }
372
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100373 VERBOSE("[0x%lx] mrctrl0 = 0x%x\n",
374 (uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200375}
376
377/* Switch DDR3 from DLL-on to DLL-off */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200378static void stm32mp1_ddr3_dll_off(struct stm32mp_ddr_priv *priv)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200379{
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100380 uint32_t mr1 = mmio_read_32((uintptr_t)&priv->phy->mr1);
381 uint32_t mr2 = mmio_read_32((uintptr_t)&priv->phy->mr2);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200382 uint32_t dbgcam;
383
384 VERBOSE("mr1: 0x%x\n", mr1);
385 VERBOSE("mr2: 0x%x\n", mr2);
386
387 /*
388 * 1. Set the DBG1.dis_hif = 1.
389 * This prevents further reads/writes being received on the HIF.
390 */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100391 mmio_setbits_32((uintptr_t)&priv->ctl->dbg1, DDRCTRL_DBG1_DIS_HIF);
392 VERBOSE("[0x%lx] dbg1 = 0x%x\n",
393 (uintptr_t)&priv->ctl->dbg1,
394 mmio_read_32((uintptr_t)&priv->ctl->dbg1));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200395
396 /*
397 * 2. Ensure all commands have been flushed from the uMCTL2 by polling
398 * DBGCAM.wr_data_pipeline_empty = 1,
399 * DBGCAM.rd_data_pipeline_empty = 1,
400 * DBGCAM.dbg_wr_q_depth = 0 ,
401 * DBGCAM.dbg_lpr_q_depth = 0, and
402 * DBGCAM.dbg_hpr_q_depth = 0.
403 */
404 do {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100405 dbgcam = mmio_read_32((uintptr_t)&priv->ctl->dbgcam);
406 VERBOSE("[0x%lx] dbgcam = 0x%x\n",
407 (uintptr_t)&priv->ctl->dbgcam, dbgcam);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200408 } while ((((dbgcam & DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY) ==
409 DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY)) &&
410 ((dbgcam & DDRCTRL_DBGCAM_DBG_Q_DEPTH) == 0U));
411
412 /*
413 * 3. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
414 * to disable RTT_NOM:
415 * a. DDR3: Write to MR1[9], MR1[6] and MR1[2]
416 * b. DDR4: Write to MR1[10:8]
417 */
418 mr1 &= ~(BIT(9) | BIT(6) | BIT(2));
419 stm32mp1_mode_register_write(priv, 1, mr1);
420
421 /*
422 * 4. For DDR4 only: Perform an MRS command
423 * (using MRCTRL0 and MRCTRL1 registers) to write to MR5[8:6]
424 * to disable RTT_PARK
425 */
426
427 /*
428 * 5. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
429 * to write to MR2[10:9], to disable RTT_WR
430 * (and therefore disable dynamic ODT).
431 * This applies for both DDR3 and DDR4.
432 */
433 mr2 &= ~GENMASK(10, 9);
434 stm32mp1_mode_register_write(priv, 2, mr2);
435
436 /*
437 * 6. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
438 * to disable the DLL. The timing of this MRS is automatically
439 * handled by the uMCTL2.
440 * a. DDR3: Write to MR1[0]
441 * b. DDR4: Write to MR1[0]
442 */
443 mr1 |= BIT(0);
444 stm32mp1_mode_register_write(priv, 1, mr1);
445
446 /*
447 * 7. Put the SDRAM into self-refresh mode by setting
448 * PWRCTL.selfref_sw = 1, and polling STAT.operating_mode to ensure
449 * the DDRC has entered self-refresh.
450 */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100451 mmio_setbits_32((uintptr_t)&priv->ctl->pwrctl,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200452 DDRCTRL_PWRCTL_SELFREF_SW);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100453 VERBOSE("[0x%lx] pwrctl = 0x%x\n",
454 (uintptr_t)&priv->ctl->pwrctl,
455 mmio_read_32((uintptr_t)&priv->ctl->pwrctl));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200456
457 /*
458 * 8. Wait until STAT.operating_mode[1:0]==11 indicating that the
459 * DWC_ddr_umctl2 core is in self-refresh mode.
460 * Ensure transition to self-refresh was due to software
461 * by checking that STAT.selfref_type[1:0]=2.
462 */
463 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_SR);
464
465 /*
466 * 9. Set the MSTR.dll_off_mode = 1.
467 * warning: MSTR.dll_off_mode is a quasi-dynamic type 2 field
468 */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200469 stm32mp_ddr_start_sw_done(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200470
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100471 mmio_setbits_32((uintptr_t)&priv->ctl->mstr, DDRCTRL_MSTR_DLL_OFF_MODE);
472 VERBOSE("[0x%lx] mstr = 0x%x\n",
473 (uintptr_t)&priv->ctl->mstr,
474 mmio_read_32((uintptr_t)&priv->ctl->mstr));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200475
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200476 stm32mp_ddr_wait_sw_done_ack(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200477
478 /* 10. Change the clock frequency to the desired value. */
479
480 /*
481 * 11. Update any registers which may be required to change for the new
482 * frequency. This includes static and dynamic registers.
483 * This includes both uMCTL2 registers and PHY registers.
484 */
485
486 /* Change Bypass Mode Frequency Range */
Yann Gautiera205a5c2021-08-30 15:06:54 +0200487 if (clk_get_rate(DDRPHYC) < 100000000U) {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100488 mmio_clrbits_32((uintptr_t)&priv->phy->dllgcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200489 DDRPHYC_DLLGCR_BPS200);
490 } else {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100491 mmio_setbits_32((uintptr_t)&priv->phy->dllgcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200492 DDRPHYC_DLLGCR_BPS200);
493 }
494
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100495 mmio_setbits_32((uintptr_t)&priv->phy->acdllcr, DDRPHYC_ACDLLCR_DLLDIS);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200496
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100497 mmio_setbits_32((uintptr_t)&priv->phy->dx0dllcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200498 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100499 mmio_setbits_32((uintptr_t)&priv->phy->dx1dllcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200500 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier6d8c2442020-09-17 12:42:46 +0200501#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100502 mmio_setbits_32((uintptr_t)&priv->phy->dx2dllcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200503 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100504 mmio_setbits_32((uintptr_t)&priv->phy->dx3dllcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200505 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier6d8c2442020-09-17 12:42:46 +0200506#endif
Yann Gautiercaf575b2018-07-24 17:18:19 +0200507
508 /* 12. Exit the self-refresh state by setting PWRCTL.selfref_sw = 0. */
Nicolas Le Bayon93763f52022-03-08 17:09:59 +0100509 stm32mp_ddr_sw_selfref_exit(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200510 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL);
511
512 /*
513 * 13. If ZQCTL0.dis_srx_zqcl = 0, the uMCTL2 performs a ZQCL command
514 * at this point.
515 */
516
517 /*
518 * 14. Perform MRS commands as required to re-program timing registers
519 * in the SDRAM for the new frequency
520 * (in particular, CL, CWL and WR may need to be changed).
521 */
522
523 /* 15. Write DBG1.dis_hif = 0 to re-enable reads and writes. */
Nicolas Le Bayon93763f52022-03-08 17:09:59 +0100524 stm32mp_ddr_enable_host_interface(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200525}
526
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200527static void stm32mp1_refresh_disable(struct stm32mp_ddrctl *ctl)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200528{
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200529 stm32mp_ddr_start_sw_done(ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200530 /* Quasi-dynamic register update*/
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100531 mmio_setbits_32((uintptr_t)&ctl->rfshctl3,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200532 DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100533 mmio_clrbits_32((uintptr_t)&ctl->pwrctl, DDRCTRL_PWRCTL_POWERDOWN_EN);
534 mmio_clrbits_32((uintptr_t)&ctl->dfimisc,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200535 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200536 stm32mp_ddr_wait_sw_done_ack(ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200537}
538
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200539static void stm32mp1_refresh_restore(struct stm32mp_ddrctl *ctl,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200540 uint32_t rfshctl3, uint32_t pwrctl)
541{
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200542 stm32mp_ddr_start_sw_done(ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200543 if ((rfshctl3 & DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH) == 0U) {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100544 mmio_clrbits_32((uintptr_t)&ctl->rfshctl3,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200545 DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
546 }
547 if ((pwrctl & DDRCTRL_PWRCTL_POWERDOWN_EN) != 0U) {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100548 mmio_setbits_32((uintptr_t)&ctl->pwrctl,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200549 DDRCTRL_PWRCTL_POWERDOWN_EN);
550 }
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100551 mmio_setbits_32((uintptr_t)&ctl->dfimisc,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200552 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200553 stm32mp_ddr_wait_sw_done_ack(ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200554}
555
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200556void stm32mp1_ddr_init(struct stm32mp_ddr_priv *priv,
557 struct stm32mp_ddr_config *config)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200558{
559 uint32_t pir;
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100560 int ret = -EINVAL;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200561
562 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) != 0U) {
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200563 ret = stm32mp_board_ddr_power_init(STM32MP_DDR3);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100564 } else if ((config->c_reg.mstr & DDRCTRL_MSTR_LPDDR2) != 0U) {
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200565 ret = stm32mp_board_ddr_power_init(STM32MP_LPDDR2);
Yann Gautier917a00c2019-04-16 16:20:58 +0200566 } else if ((config->c_reg.mstr & DDRCTRL_MSTR_LPDDR3) != 0U) {
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200567 ret = stm32mp_board_ddr_power_init(STM32MP_LPDDR3);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100568 } else {
569 ERROR("DDR type not supported\n");
Yann Gautiercaf575b2018-07-24 17:18:19 +0200570 }
571
572 if (ret != 0) {
573 panic();
574 }
575
576 VERBOSE("name = %s\n", config->info.name);
Yann Gautier634591d2021-09-07 09:07:35 +0200577 VERBOSE("speed = %u kHz\n", config->info.speed);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200578 VERBOSE("size = 0x%x\n", config->info.size);
579
580 /* DDR INIT SEQUENCE */
581
582 /*
583 * 1. Program the DWC_ddr_umctl2 registers
584 * nota: check DFIMISC.dfi_init_complete = 0
585 */
586
587 /* 1.1 RESETS: presetn, core_ddrc_rstn, aresetn */
588 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
589 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
590 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
591 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
592 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
593 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
594
595 /* 1.2. start CLOCK */
596 if (stm32mp1_ddr_clk_enable(priv, config->info.speed) != 0) {
597 panic();
598 }
599
600 /* 1.3. deassert reset */
601 /* De-assert PHY rstn and ctl_rstn via DPHYRST and DPHYCTLRST. */
602 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
603 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
604 /*
605 * De-assert presetn once the clocks are active
606 * and stable via DDRCAPBRST bit.
607 */
608 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
609
610 /* 1.4. wait 128 cycles to permit initialization of end logic */
Yann Gautier2b855782024-05-29 15:34:45 +0200611 udelay(DDR_DELAY_2US);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200612 /* For PCLK = 133MHz => 1 us is enough, 2 to allow lower frequency */
613
614 /* 1.5. initialize registers ddr_umctl2 */
615 /* Stop uMCTL2 before PHY is ready */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100616 mmio_clrbits_32((uintptr_t)&priv->ctl->dfimisc,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200617 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100618 VERBOSE("[0x%lx] dfimisc = 0x%x\n",
619 (uintptr_t)&priv->ctl->dfimisc,
620 mmio_read_32((uintptr_t)&priv->ctl->dfimisc));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200621
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200622 stm32mp_ddr_set_reg(priv, REG_REG, &config->c_reg, ddr_registers);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200623
624 /* DDR3 = don't set DLLOFF for init mode */
625 if ((config->c_reg.mstr &
626 (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE))
627 == (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) {
628 VERBOSE("deactivate DLL OFF in mstr\n");
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100629 mmio_clrbits_32((uintptr_t)&priv->ctl->mstr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200630 DDRCTRL_MSTR_DLL_OFF_MODE);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100631 VERBOSE("[0x%lx] mstr = 0x%x\n",
632 (uintptr_t)&priv->ctl->mstr,
633 mmio_read_32((uintptr_t)&priv->ctl->mstr));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200634 }
635
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200636 stm32mp_ddr_set_reg(priv, REG_TIMING, &config->c_timing, ddr_registers);
637 stm32mp_ddr_set_reg(priv, REG_MAP, &config->c_map, ddr_registers);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200638
639 /* Skip CTRL init, SDRAM init is done by PHY PUBL */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100640 mmio_clrsetbits_32((uintptr_t)&priv->ctl->init0,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200641 DDRCTRL_INIT0_SKIP_DRAM_INIT_MASK,
642 DDRCTRL_INIT0_SKIP_DRAM_INIT_NORMAL);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100643 VERBOSE("[0x%lx] init0 = 0x%x\n",
644 (uintptr_t)&priv->ctl->init0,
645 mmio_read_32((uintptr_t)&priv->ctl->init0));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200646
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200647 stm32mp_ddr_set_reg(priv, REG_PERF, &config->c_perf, ddr_registers);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200648
649 /* 2. deassert reset signal core_ddrc_rstn, aresetn and presetn */
650 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
651 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
652 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
653
654 /*
655 * 3. start PHY init by accessing relevant PUBL registers
656 * (DXGCR, DCR, PTR*, MR*, DTPR*)
657 */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200658 stm32mp_ddr_set_reg(priv, REGPHY_REG, &config->p_reg, ddr_registers);
659 stm32mp_ddr_set_reg(priv, REGPHY_TIMING, &config->p_timing, ddr_registers);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200660
661 /* DDR3 = don't set DLLOFF for init mode */
662 if ((config->c_reg.mstr &
663 (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE))
664 == (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) {
665 VERBOSE("deactivate DLL OFF in mr1\n");
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100666 mmio_clrbits_32((uintptr_t)&priv->phy->mr1, BIT(0));
667 VERBOSE("[0x%lx] mr1 = 0x%x\n",
668 (uintptr_t)&priv->phy->mr1,
669 mmio_read_32((uintptr_t)&priv->phy->mr1));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200670 }
671
672 /*
673 * 4. Monitor PHY init status by polling PUBL register PGSR.IDONE
674 * Perform DDR PHY DRAM initialization and Gate Training Evaluation
675 */
676 stm32mp1_ddrphy_idone_wait(priv->phy);
677
678 /*
679 * 5. Indicate to PUBL that controller performs SDRAM initialization
680 * by setting PIR.INIT and PIR CTLDINIT and pool PGSR.IDONE
681 * DRAM init is done by PHY, init0.skip_dram.init = 1
682 */
683
684 pir = DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK | DDRPHYC_PIR_ZCAL |
685 DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_DRAMINIT | DDRPHYC_PIR_ICPC;
686
687 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) != 0U) {
688 pir |= DDRPHYC_PIR_DRAMRST; /* Only for DDR3 */
689 }
690
691 stm32mp1_ddrphy_init(priv->phy, pir);
692
693 /*
694 * 6. SET DFIMISC.dfi_init_complete_en to 1
695 * Enable quasi-dynamic register programming.
696 */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200697 stm32mp_ddr_start_sw_done(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200698
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100699 mmio_setbits_32((uintptr_t)&priv->ctl->dfimisc,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200700 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100701 VERBOSE("[0x%lx] dfimisc = 0x%x\n",
702 (uintptr_t)&priv->ctl->dfimisc,
703 mmio_read_32((uintptr_t)&priv->ctl->dfimisc));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200704
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200705 stm32mp_ddr_wait_sw_done_ack(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200706
707 /*
708 * 7. Wait for DWC_ddr_umctl2 to move to normal operation mode
709 * by monitoring STAT.operating_mode signal
710 */
711
712 /* Wait uMCTL2 ready */
713 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL);
714
715 /* Switch to DLL OFF mode */
716 if ((config->c_reg.mstr & DDRCTRL_MSTR_DLL_OFF_MODE) != 0U) {
717 stm32mp1_ddr3_dll_off(priv);
718 }
719
720 VERBOSE("DDR DQS training : ");
721
722 /*
723 * 8. Disable Auto refresh and power down by setting
724 * - RFSHCTL3.dis_au_refresh = 1
725 * - PWRCTL.powerdown_en = 0
726 * - DFIMISC.dfiinit_complete_en = 0
727 */
728 stm32mp1_refresh_disable(priv->ctl);
729
730 /*
731 * 9. Program PUBL PGCR to enable refresh during training
732 * and rank to train
733 * not done => keep the programed value in PGCR
734 */
735
736 /*
737 * 10. configure PUBL PIR register to specify which training step
738 * to run
Nicolas Le Bayone22424a2021-09-10 12:03:38 +0200739 * RVTRN is executed only on LPDDR2/LPDDR3
Yann Gautiercaf575b2018-07-24 17:18:19 +0200740 */
Nicolas Le Bayone22424a2021-09-10 12:03:38 +0200741 pir = DDRPHYC_PIR_QSTRN;
742 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) == 0U) {
743 pir |= DDRPHYC_PIR_RVTRN;
744 }
745
746 stm32mp1_ddrphy_init(priv->phy, pir);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200747
748 /* 11. monitor PUB PGSR.IDONE to poll cpmpletion of training sequence */
749 stm32mp1_ddrphy_idone_wait(priv->phy);
750
751 /*
Elyes Haouas2be03c02023-02-13 09:14:48 +0100752 * 12. set back registers in step 8 to the original values if desidered
Yann Gautiercaf575b2018-07-24 17:18:19 +0200753 */
754 stm32mp1_refresh_restore(priv->ctl, config->c_reg.rfshctl3,
755 config->c_reg.pwrctl);
756
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200757 stm32mp_ddr_enable_axi_port(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200758}