blob: e890c4d2e26090ff8ad5370045430b9350df9e39 [file] [log] [blame]
Yann Gautier10a511c2018-07-24 17:18:19 +02001/*
Yann Gautiera0781342021-09-07 09:07:35 +02002 * Copyright (C) 2018-2022, STMicroelectronics - All Rights Reserved
Yann Gautier10a511c2018-07-24 17:18:19 +02003 *
4 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
5 */
6
Yann Gautier4156d4d2019-01-17 14:35:22 +01007#include <errno.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00008#include <stddef.h>
9
Yann Gautier10a511c2018-07-24 17:18:19 +020010#include <arch.h>
11#include <arch_helpers.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000012#include <common/debug.h>
Yann Gautier33667d22021-08-30 15:06:54 +020013#include <drivers/clk.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000014#include <drivers/delay_timer.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000015#include <drivers/st/stm32mp1_ddr.h>
16#include <drivers/st/stm32mp1_ddr_regs.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000017#include <drivers/st/stm32mp1_pwr.h>
18#include <drivers/st/stm32mp1_ram.h>
Yann Gautiera0781342021-09-07 09:07:35 +020019#include <drivers/st/stm32mp_pmic.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000020#include <lib/mmio.h>
21#include <plat/common/platform.h>
22
Yann Gautiera0781342021-09-07 09:07:35 +020023#include <platform_def.h>
24
Yann Gautier10a511c2018-07-24 17:18:19 +020025struct reg_desc {
26 const char *name;
27 uint16_t offset; /* Offset for base address */
28 uint8_t par_offset; /* Offset for parameter array */
29};
30
31#define INVALID_OFFSET 0xFFU
32
Yann Gautierdfdb0572019-02-14 11:14:39 +010033#define TIMEOUT_US_1S 1000000U
Yann Gautier10a511c2018-07-24 17:18:19 +020034
35#define DDRCTL_REG(x, y) \
36 { \
37 .name = #x, \
38 .offset = offsetof(struct stm32mp1_ddrctl, x), \
39 .par_offset = offsetof(struct y, x) \
40 }
41
42#define DDRPHY_REG(x, y) \
43 { \
44 .name = #x, \
45 .offset = offsetof(struct stm32mp1_ddrphy, x), \
46 .par_offset = offsetof(struct y, x) \
47 }
48
Yann Gautierba7d2e22019-02-25 13:44:27 +010049/*
50 * PARAMETERS: value get from device tree :
51 * size / order need to be aligned with binding
52 * modification NOT ALLOWED !!!
53 */
54#define DDRCTL_REG_REG_SIZE 25 /* st,ctl-reg */
55#define DDRCTL_REG_TIMING_SIZE 12 /* st,ctl-timing */
56#define DDRCTL_REG_MAP_SIZE 9 /* st,ctl-map */
Yann Gautier88f4fb82020-09-17 12:42:46 +020057#if STM32MP_DDR_DUAL_AXI_PORT
Yann Gautierba7d2e22019-02-25 13:44:27 +010058#define DDRCTL_REG_PERF_SIZE 17 /* st,ctl-perf */
Yann Gautier88f4fb82020-09-17 12:42:46 +020059#else
60#define DDRCTL_REG_PERF_SIZE 11 /* st,ctl-perf */
61#endif
Yann Gautierba7d2e22019-02-25 13:44:27 +010062
Yann Gautier88f4fb82020-09-17 12:42:46 +020063#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautierba7d2e22019-02-25 13:44:27 +010064#define DDRPHY_REG_REG_SIZE 11 /* st,phy-reg */
Yann Gautier88f4fb82020-09-17 12:42:46 +020065#else
66#define DDRPHY_REG_REG_SIZE 9 /* st,phy-reg */
67#endif
Yann Gautierba7d2e22019-02-25 13:44:27 +010068#define DDRPHY_REG_TIMING_SIZE 10 /* st,phy-timing */
69
Yann Gautier10a511c2018-07-24 17:18:19 +020070#define DDRCTL_REG_REG(x) DDRCTL_REG(x, stm32mp1_ddrctrl_reg)
Yann Gautierba7d2e22019-02-25 13:44:27 +010071static const struct reg_desc ddr_reg[DDRCTL_REG_REG_SIZE] = {
Yann Gautier10a511c2018-07-24 17:18:19 +020072 DDRCTL_REG_REG(mstr),
73 DDRCTL_REG_REG(mrctrl0),
74 DDRCTL_REG_REG(mrctrl1),
75 DDRCTL_REG_REG(derateen),
76 DDRCTL_REG_REG(derateint),
77 DDRCTL_REG_REG(pwrctl),
78 DDRCTL_REG_REG(pwrtmg),
79 DDRCTL_REG_REG(hwlpctl),
80 DDRCTL_REG_REG(rfshctl0),
81 DDRCTL_REG_REG(rfshctl3),
82 DDRCTL_REG_REG(crcparctl0),
83 DDRCTL_REG_REG(zqctl0),
84 DDRCTL_REG_REG(dfitmg0),
85 DDRCTL_REG_REG(dfitmg1),
86 DDRCTL_REG_REG(dfilpcfg0),
87 DDRCTL_REG_REG(dfiupd0),
88 DDRCTL_REG_REG(dfiupd1),
89 DDRCTL_REG_REG(dfiupd2),
90 DDRCTL_REG_REG(dfiphymstr),
91 DDRCTL_REG_REG(odtmap),
92 DDRCTL_REG_REG(dbg0),
93 DDRCTL_REG_REG(dbg1),
94 DDRCTL_REG_REG(dbgcmd),
95 DDRCTL_REG_REG(poisoncfg),
96 DDRCTL_REG_REG(pccfg),
97};
98
99#define DDRCTL_REG_TIMING(x) DDRCTL_REG(x, stm32mp1_ddrctrl_timing)
Yann Gautierba7d2e22019-02-25 13:44:27 +0100100static const struct reg_desc ddr_timing[DDRCTL_REG_TIMING_SIZE] = {
Yann Gautier10a511c2018-07-24 17:18:19 +0200101 DDRCTL_REG_TIMING(rfshtmg),
102 DDRCTL_REG_TIMING(dramtmg0),
103 DDRCTL_REG_TIMING(dramtmg1),
104 DDRCTL_REG_TIMING(dramtmg2),
105 DDRCTL_REG_TIMING(dramtmg3),
106 DDRCTL_REG_TIMING(dramtmg4),
107 DDRCTL_REG_TIMING(dramtmg5),
108 DDRCTL_REG_TIMING(dramtmg6),
109 DDRCTL_REG_TIMING(dramtmg7),
110 DDRCTL_REG_TIMING(dramtmg8),
111 DDRCTL_REG_TIMING(dramtmg14),
112 DDRCTL_REG_TIMING(odtcfg),
113};
114
115#define DDRCTL_REG_MAP(x) DDRCTL_REG(x, stm32mp1_ddrctrl_map)
Yann Gautierba7d2e22019-02-25 13:44:27 +0100116static const struct reg_desc ddr_map[DDRCTL_REG_MAP_SIZE] = {
Yann Gautier10a511c2018-07-24 17:18:19 +0200117 DDRCTL_REG_MAP(addrmap1),
118 DDRCTL_REG_MAP(addrmap2),
119 DDRCTL_REG_MAP(addrmap3),
120 DDRCTL_REG_MAP(addrmap4),
121 DDRCTL_REG_MAP(addrmap5),
122 DDRCTL_REG_MAP(addrmap6),
123 DDRCTL_REG_MAP(addrmap9),
124 DDRCTL_REG_MAP(addrmap10),
125 DDRCTL_REG_MAP(addrmap11),
126};
127
128#define DDRCTL_REG_PERF(x) DDRCTL_REG(x, stm32mp1_ddrctrl_perf)
Yann Gautierba7d2e22019-02-25 13:44:27 +0100129static const struct reg_desc ddr_perf[DDRCTL_REG_PERF_SIZE] = {
Yann Gautier10a511c2018-07-24 17:18:19 +0200130 DDRCTL_REG_PERF(sched),
131 DDRCTL_REG_PERF(sched1),
132 DDRCTL_REG_PERF(perfhpr1),
133 DDRCTL_REG_PERF(perflpr1),
134 DDRCTL_REG_PERF(perfwr1),
135 DDRCTL_REG_PERF(pcfgr_0),
136 DDRCTL_REG_PERF(pcfgw_0),
137 DDRCTL_REG_PERF(pcfgqos0_0),
138 DDRCTL_REG_PERF(pcfgqos1_0),
139 DDRCTL_REG_PERF(pcfgwqos0_0),
140 DDRCTL_REG_PERF(pcfgwqos1_0),
Yann Gautier88f4fb82020-09-17 12:42:46 +0200141#if STM32MP_DDR_DUAL_AXI_PORT
Yann Gautier10a511c2018-07-24 17:18:19 +0200142 DDRCTL_REG_PERF(pcfgr_1),
143 DDRCTL_REG_PERF(pcfgw_1),
144 DDRCTL_REG_PERF(pcfgqos0_1),
145 DDRCTL_REG_PERF(pcfgqos1_1),
146 DDRCTL_REG_PERF(pcfgwqos0_1),
147 DDRCTL_REG_PERF(pcfgwqos1_1),
Yann Gautier88f4fb82020-09-17 12:42:46 +0200148#endif
Yann Gautier10a511c2018-07-24 17:18:19 +0200149};
150
151#define DDRPHY_REG_REG(x) DDRPHY_REG(x, stm32mp1_ddrphy_reg)
Yann Gautierba7d2e22019-02-25 13:44:27 +0100152static const struct reg_desc ddrphy_reg[DDRPHY_REG_REG_SIZE] = {
Yann Gautier10a511c2018-07-24 17:18:19 +0200153 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),
Yann Gautier88f4fb82020-09-17 12:42:46 +0200162#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautier10a511c2018-07-24 17:18:19 +0200163 DDRPHY_REG_REG(dx2gcr),
164 DDRPHY_REG_REG(dx3gcr),
Yann Gautier88f4fb82020-09-17 12:42:46 +0200165#endif
Yann Gautier10a511c2018-07-24 17:18:19 +0200166};
167
168#define DDRPHY_REG_TIMING(x) DDRPHY_REG(x, stm32mp1_ddrphy_timing)
Yann Gautierba7d2e22019-02-25 13:44:27 +0100169static const struct reg_desc ddrphy_timing[DDRPHY_REG_TIMING_SIZE] = {
Yann Gautier10a511c2018-07-24 17:18:19 +0200170 DDRPHY_REG_TIMING(ptr0),
171 DDRPHY_REG_TIMING(ptr1),
172 DDRPHY_REG_TIMING(ptr2),
173 DDRPHY_REG_TIMING(dtpr0),
174 DDRPHY_REG_TIMING(dtpr1),
175 DDRPHY_REG_TIMING(dtpr2),
176 DDRPHY_REG_TIMING(mr0),
177 DDRPHY_REG_TIMING(mr1),
178 DDRPHY_REG_TIMING(mr2),
179 DDRPHY_REG_TIMING(mr3),
180};
181
Yann Gautierba7d2e22019-02-25 13:44:27 +0100182/*
183 * REGISTERS ARRAY: used to parse device tree and interactive mode
184 */
Yann Gautier10a511c2018-07-24 17:18:19 +0200185enum reg_type {
186 REG_REG,
187 REG_TIMING,
188 REG_PERF,
189 REG_MAP,
190 REGPHY_REG,
191 REGPHY_TIMING,
Yann Gautier10a511c2018-07-24 17:18:19 +0200192 REG_TYPE_NB
193};
194
195enum base_type {
196 DDR_BASE,
197 DDRPHY_BASE,
198 NONE_BASE
199};
200
201struct ddr_reg_info {
202 const char *name;
203 const struct reg_desc *desc;
204 uint8_t size;
205 enum base_type base;
206};
207
208static const struct ddr_reg_info ddr_registers[REG_TYPE_NB] = {
209 [REG_REG] = {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100210 .name = "static",
211 .desc = ddr_reg,
Yann Gautierba7d2e22019-02-25 13:44:27 +0100212 .size = DDRCTL_REG_REG_SIZE,
Yann Gautier4156d4d2019-01-17 14:35:22 +0100213 .base = DDR_BASE
Yann Gautier10a511c2018-07-24 17:18:19 +0200214 },
215 [REG_TIMING] = {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100216 .name = "timing",
217 .desc = ddr_timing,
Yann Gautierba7d2e22019-02-25 13:44:27 +0100218 .size = DDRCTL_REG_TIMING_SIZE,
Yann Gautier4156d4d2019-01-17 14:35:22 +0100219 .base = DDR_BASE
Yann Gautier10a511c2018-07-24 17:18:19 +0200220 },
221 [REG_PERF] = {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100222 .name = "perf",
223 .desc = ddr_perf,
Yann Gautierba7d2e22019-02-25 13:44:27 +0100224 .size = DDRCTL_REG_PERF_SIZE,
Yann Gautier4156d4d2019-01-17 14:35:22 +0100225 .base = DDR_BASE
Yann Gautier10a511c2018-07-24 17:18:19 +0200226 },
227 [REG_MAP] = {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100228 .name = "map",
229 .desc = ddr_map,
Yann Gautierba7d2e22019-02-25 13:44:27 +0100230 .size = DDRCTL_REG_MAP_SIZE,
Yann Gautier4156d4d2019-01-17 14:35:22 +0100231 .base = DDR_BASE
Yann Gautier10a511c2018-07-24 17:18:19 +0200232 },
233 [REGPHY_REG] = {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100234 .name = "static",
235 .desc = ddrphy_reg,
Yann Gautierba7d2e22019-02-25 13:44:27 +0100236 .size = DDRPHY_REG_REG_SIZE,
Yann Gautier4156d4d2019-01-17 14:35:22 +0100237 .base = DDRPHY_BASE
Yann Gautier10a511c2018-07-24 17:18:19 +0200238 },
239 [REGPHY_TIMING] = {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100240 .name = "timing",
241 .desc = ddrphy_timing,
Yann Gautierba7d2e22019-02-25 13:44:27 +0100242 .size = DDRPHY_REG_TIMING_SIZE,
Yann Gautier4156d4d2019-01-17 14:35:22 +0100243 .base = DDRPHY_BASE
Yann Gautier10a511c2018-07-24 17:18:19 +0200244 },
245};
246
Yann Gautier4156d4d2019-01-17 14:35:22 +0100247static uintptr_t get_base_addr(const struct ddr_info *priv, enum base_type base)
Yann Gautier10a511c2018-07-24 17:18:19 +0200248{
249 if (base == DDRPHY_BASE) {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100250 return (uintptr_t)priv->phy;
Yann Gautier10a511c2018-07-24 17:18:19 +0200251 } else {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100252 return (uintptr_t)priv->ctl;
Yann Gautier10a511c2018-07-24 17:18:19 +0200253 }
254}
255
256static void set_reg(const struct ddr_info *priv,
257 enum reg_type type,
258 const void *param)
259{
260 unsigned int i;
Yann Gautier4156d4d2019-01-17 14:35:22 +0100261 unsigned int value;
Yann Gautier10a511c2018-07-24 17:18:19 +0200262 enum base_type base = ddr_registers[type].base;
Yann Gautier4156d4d2019-01-17 14:35:22 +0100263 uintptr_t base_addr = get_base_addr(priv, base);
Yann Gautier10a511c2018-07-24 17:18:19 +0200264 const struct reg_desc *desc = ddr_registers[type].desc;
265
266 VERBOSE("init %s\n", ddr_registers[type].name);
267 for (i = 0; i < ddr_registers[type].size; i++) {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100268 uintptr_t ptr = base_addr + desc[i].offset;
269
Yann Gautier10a511c2018-07-24 17:18:19 +0200270 if (desc[i].par_offset == INVALID_OFFSET) {
271 ERROR("invalid parameter offset for %s", desc[i].name);
272 panic();
273 } else {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100274 value = *((uint32_t *)((uintptr_t)param +
Yann Gautier10a511c2018-07-24 17:18:19 +0200275 desc[i].par_offset));
Yann Gautier4156d4d2019-01-17 14:35:22 +0100276 mmio_write_32(ptr, value);
Yann Gautier10a511c2018-07-24 17:18:19 +0200277 }
278 }
279}
280
281static void stm32mp1_ddrphy_idone_wait(struct stm32mp1_ddrphy *phy)
282{
283 uint32_t pgsr;
284 int error = 0;
Yann Gautierdfdb0572019-02-14 11:14:39 +0100285 uint64_t timeout = timeout_init_us(TIMEOUT_US_1S);
Yann Gautier10a511c2018-07-24 17:18:19 +0200286
287 do {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100288 pgsr = mmio_read_32((uintptr_t)&phy->pgsr);
Yann Gautier10a511c2018-07-24 17:18:19 +0200289
Yann Gautierdfdb0572019-02-14 11:14:39 +0100290 VERBOSE(" > [0x%lx] pgsr = 0x%x &\n",
291 (uintptr_t)&phy->pgsr, pgsr);
292
293 if (timeout_elapsed(timeout)) {
Yann Gautier10a511c2018-07-24 17:18:19 +0200294 panic();
295 }
Yann Gautierdfdb0572019-02-14 11:14:39 +0100296
Yann Gautier10a511c2018-07-24 17:18:19 +0200297 if ((pgsr & DDRPHYC_PGSR_DTERR) != 0U) {
298 VERBOSE("DQS Gate Trainig Error\n");
299 error++;
300 }
Yann Gautierdfdb0572019-02-14 11:14:39 +0100301
Yann Gautier10a511c2018-07-24 17:18:19 +0200302 if ((pgsr & DDRPHYC_PGSR_DTIERR) != 0U) {
303 VERBOSE("DQS Gate Trainig Intermittent Error\n");
304 error++;
305 }
Yann Gautierdfdb0572019-02-14 11:14:39 +0100306
Yann Gautier10a511c2018-07-24 17:18:19 +0200307 if ((pgsr & DDRPHYC_PGSR_DFTERR) != 0U) {
308 VERBOSE("DQS Drift Error\n");
309 error++;
310 }
Yann Gautierdfdb0572019-02-14 11:14:39 +0100311
Yann Gautier10a511c2018-07-24 17:18:19 +0200312 if ((pgsr & DDRPHYC_PGSR_RVERR) != 0U) {
313 VERBOSE("Read Valid Training Error\n");
314 error++;
315 }
Yann Gautierdfdb0572019-02-14 11:14:39 +0100316
Yann Gautier10a511c2018-07-24 17:18:19 +0200317 if ((pgsr & DDRPHYC_PGSR_RVEIRR) != 0U) {
318 VERBOSE("Read Valid Training Intermittent Error\n");
319 error++;
320 }
Yann Gautierdfdb0572019-02-14 11:14:39 +0100321 } while (((pgsr & DDRPHYC_PGSR_IDONE) == 0U) && (error == 0));
Yann Gautier4156d4d2019-01-17 14:35:22 +0100322 VERBOSE("\n[0x%lx] pgsr = 0x%x\n",
323 (uintptr_t)&phy->pgsr, pgsr);
Yann Gautier10a511c2018-07-24 17:18:19 +0200324}
325
326static void stm32mp1_ddrphy_init(struct stm32mp1_ddrphy *phy, uint32_t pir)
327{
328 uint32_t pir_init = pir | DDRPHYC_PIR_INIT;
329
Yann Gautier4156d4d2019-01-17 14:35:22 +0100330 mmio_write_32((uintptr_t)&phy->pir, pir_init);
331 VERBOSE("[0x%lx] pir = 0x%x -> 0x%x\n",
332 (uintptr_t)&phy->pir, pir_init,
333 mmio_read_32((uintptr_t)&phy->pir));
Yann Gautier10a511c2018-07-24 17:18:19 +0200334
335 /* Need to wait 10 configuration clock before start polling */
336 udelay(10);
337
338 /* Wait DRAM initialization and Gate Training Evaluation complete */
339 stm32mp1_ddrphy_idone_wait(phy);
340}
341
342/* Start quasi dynamic register update */
343static void stm32mp1_start_sw_done(struct stm32mp1_ddrctl *ctl)
344{
Yann Gautier4156d4d2019-01-17 14:35:22 +0100345 mmio_clrbits_32((uintptr_t)&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
346 VERBOSE("[0x%lx] swctl = 0x%x\n",
347 (uintptr_t)&ctl->swctl, mmio_read_32((uintptr_t)&ctl->swctl));
Yann Gautier10a511c2018-07-24 17:18:19 +0200348}
349
350/* Wait quasi dynamic register update */
351static void stm32mp1_wait_sw_done_ack(struct stm32mp1_ddrctl *ctl)
352{
Yann Gautierdfdb0572019-02-14 11:14:39 +0100353 uint64_t timeout;
Yann Gautier10a511c2018-07-24 17:18:19 +0200354 uint32_t swstat;
355
Yann Gautier4156d4d2019-01-17 14:35:22 +0100356 mmio_setbits_32((uintptr_t)&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
357 VERBOSE("[0x%lx] swctl = 0x%x\n",
358 (uintptr_t)&ctl->swctl, mmio_read_32((uintptr_t)&ctl->swctl));
Yann Gautier10a511c2018-07-24 17:18:19 +0200359
Yann Gautierdfdb0572019-02-14 11:14:39 +0100360 timeout = timeout_init_us(TIMEOUT_US_1S);
Yann Gautier10a511c2018-07-24 17:18:19 +0200361 do {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100362 swstat = mmio_read_32((uintptr_t)&ctl->swstat);
363 VERBOSE("[0x%lx] swstat = 0x%x ",
364 (uintptr_t)&ctl->swstat, swstat);
Yann Gautierdfdb0572019-02-14 11:14:39 +0100365 if (timeout_elapsed(timeout)) {
Yann Gautier10a511c2018-07-24 17:18:19 +0200366 panic();
367 }
368 } while ((swstat & DDRCTRL_SWSTAT_SW_DONE_ACK) == 0U);
369
Yann Gautier4156d4d2019-01-17 14:35:22 +0100370 VERBOSE("[0x%lx] swstat = 0x%x\n",
371 (uintptr_t)&ctl->swstat, swstat);
Yann Gautier10a511c2018-07-24 17:18:19 +0200372}
373
374/* Wait quasi dynamic register update */
375static void stm32mp1_wait_operating_mode(struct ddr_info *priv, uint32_t mode)
376{
Yann Gautierdfdb0572019-02-14 11:14:39 +0100377 uint64_t timeout;
Yann Gautier10a511c2018-07-24 17:18:19 +0200378 uint32_t stat;
Yann Gautier10a511c2018-07-24 17:18:19 +0200379 int break_loop = 0;
380
Yann Gautierdfdb0572019-02-14 11:14:39 +0100381 timeout = timeout_init_us(TIMEOUT_US_1S);
Yann Gautier10a511c2018-07-24 17:18:19 +0200382 for ( ; ; ) {
Yann Gautierdfdb0572019-02-14 11:14:39 +0100383 uint32_t operating_mode;
384 uint32_t selref_type;
385
Yann Gautier4156d4d2019-01-17 14:35:22 +0100386 stat = mmio_read_32((uintptr_t)&priv->ctl->stat);
Yann Gautier10a511c2018-07-24 17:18:19 +0200387 operating_mode = stat & DDRCTRL_STAT_OPERATING_MODE_MASK;
388 selref_type = stat & DDRCTRL_STAT_SELFREF_TYPE_MASK;
Yann Gautier4156d4d2019-01-17 14:35:22 +0100389 VERBOSE("[0x%lx] stat = 0x%x\n",
390 (uintptr_t)&priv->ctl->stat, stat);
Yann Gautierdfdb0572019-02-14 11:14:39 +0100391 if (timeout_elapsed(timeout)) {
Yann Gautier10a511c2018-07-24 17:18:19 +0200392 panic();
393 }
394
395 if (mode == DDRCTRL_STAT_OPERATING_MODE_SR) {
396 /*
397 * Self-refresh due to software
398 * => checking also STAT.selfref_type.
399 */
400 if ((operating_mode ==
401 DDRCTRL_STAT_OPERATING_MODE_SR) &&
402 (selref_type == DDRCTRL_STAT_SELFREF_TYPE_SR)) {
403 break_loop = 1;
404 }
405 } else if (operating_mode == mode) {
406 break_loop = 1;
407 } else if ((mode == DDRCTRL_STAT_OPERATING_MODE_NORMAL) &&
408 (operating_mode == DDRCTRL_STAT_OPERATING_MODE_SR) &&
409 (selref_type == DDRCTRL_STAT_SELFREF_TYPE_ASR)) {
410 /* Normal mode: handle also automatic self refresh */
411 break_loop = 1;
412 }
413
414 if (break_loop == 1) {
415 break;
416 }
417 }
418
Yann Gautier4156d4d2019-01-17 14:35:22 +0100419 VERBOSE("[0x%lx] stat = 0x%x\n",
420 (uintptr_t)&priv->ctl->stat, stat);
Yann Gautier10a511c2018-07-24 17:18:19 +0200421}
422
423/* Mode Register Writes (MRW or MRS) */
424static void stm32mp1_mode_register_write(struct ddr_info *priv, uint8_t addr,
425 uint32_t data)
426{
427 uint32_t mrctrl0;
428
429 VERBOSE("MRS: %d = %x\n", addr, data);
430
431 /*
432 * 1. Poll MRSTAT.mr_wr_busy until it is '0'.
433 * This checks that there is no outstanding MR transaction.
434 * No write should be performed to MRCTRL0 and MRCTRL1
435 * if MRSTAT.mr_wr_busy = 1.
436 */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100437 while ((mmio_read_32((uintptr_t)&priv->ctl->mrstat) &
Yann Gautier10a511c2018-07-24 17:18:19 +0200438 DDRCTRL_MRSTAT_MR_WR_BUSY) != 0U) {
439 ;
440 }
441
442 /*
443 * 2. Write the MRCTRL0.mr_type, MRCTRL0.mr_addr, MRCTRL0.mr_rank
444 * and (for MRWs) MRCTRL1.mr_data to define the MR transaction.
445 */
446 mrctrl0 = DDRCTRL_MRCTRL0_MR_TYPE_WRITE |
447 DDRCTRL_MRCTRL0_MR_RANK_ALL |
448 (((uint32_t)addr << DDRCTRL_MRCTRL0_MR_ADDR_SHIFT) &
449 DDRCTRL_MRCTRL0_MR_ADDR_MASK);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100450 mmio_write_32((uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
451 VERBOSE("[0x%lx] mrctrl0 = 0x%x (0x%x)\n",
452 (uintptr_t)&priv->ctl->mrctrl0,
453 mmio_read_32((uintptr_t)&priv->ctl->mrctrl0), mrctrl0);
454 mmio_write_32((uintptr_t)&priv->ctl->mrctrl1, data);
455 VERBOSE("[0x%lx] mrctrl1 = 0x%x\n",
456 (uintptr_t)&priv->ctl->mrctrl1,
457 mmio_read_32((uintptr_t)&priv->ctl->mrctrl1));
Yann Gautier10a511c2018-07-24 17:18:19 +0200458
459 /*
460 * 3. In a separate APB transaction, write the MRCTRL0.mr_wr to 1. This
461 * bit is self-clearing, and triggers the MR transaction.
462 * The uMCTL2 then asserts the MRSTAT.mr_wr_busy while it performs
463 * the MR transaction to SDRAM, and no further access can be
464 * initiated until it is deasserted.
465 */
466 mrctrl0 |= DDRCTRL_MRCTRL0_MR_WR;
Yann Gautier4156d4d2019-01-17 14:35:22 +0100467 mmio_write_32((uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
Yann Gautier10a511c2018-07-24 17:18:19 +0200468
Yann Gautier4156d4d2019-01-17 14:35:22 +0100469 while ((mmio_read_32((uintptr_t)&priv->ctl->mrstat) &
Yann Gautier10a511c2018-07-24 17:18:19 +0200470 DDRCTRL_MRSTAT_MR_WR_BUSY) != 0U) {
471 ;
472 }
473
Yann Gautier4156d4d2019-01-17 14:35:22 +0100474 VERBOSE("[0x%lx] mrctrl0 = 0x%x\n",
475 (uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
Yann Gautier10a511c2018-07-24 17:18:19 +0200476}
477
478/* Switch DDR3 from DLL-on to DLL-off */
479static void stm32mp1_ddr3_dll_off(struct ddr_info *priv)
480{
Yann Gautier4156d4d2019-01-17 14:35:22 +0100481 uint32_t mr1 = mmio_read_32((uintptr_t)&priv->phy->mr1);
482 uint32_t mr2 = mmio_read_32((uintptr_t)&priv->phy->mr2);
Yann Gautier10a511c2018-07-24 17:18:19 +0200483 uint32_t dbgcam;
484
485 VERBOSE("mr1: 0x%x\n", mr1);
486 VERBOSE("mr2: 0x%x\n", mr2);
487
488 /*
489 * 1. Set the DBG1.dis_hif = 1.
490 * This prevents further reads/writes being received on the HIF.
491 */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100492 mmio_setbits_32((uintptr_t)&priv->ctl->dbg1, DDRCTRL_DBG1_DIS_HIF);
493 VERBOSE("[0x%lx] dbg1 = 0x%x\n",
494 (uintptr_t)&priv->ctl->dbg1,
495 mmio_read_32((uintptr_t)&priv->ctl->dbg1));
Yann Gautier10a511c2018-07-24 17:18:19 +0200496
497 /*
498 * 2. Ensure all commands have been flushed from the uMCTL2 by polling
499 * DBGCAM.wr_data_pipeline_empty = 1,
500 * DBGCAM.rd_data_pipeline_empty = 1,
501 * DBGCAM.dbg_wr_q_depth = 0 ,
502 * DBGCAM.dbg_lpr_q_depth = 0, and
503 * DBGCAM.dbg_hpr_q_depth = 0.
504 */
505 do {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100506 dbgcam = mmio_read_32((uintptr_t)&priv->ctl->dbgcam);
507 VERBOSE("[0x%lx] dbgcam = 0x%x\n",
508 (uintptr_t)&priv->ctl->dbgcam, dbgcam);
Yann Gautier10a511c2018-07-24 17:18:19 +0200509 } while ((((dbgcam & DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY) ==
510 DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY)) &&
511 ((dbgcam & DDRCTRL_DBGCAM_DBG_Q_DEPTH) == 0U));
512
513 /*
514 * 3. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
515 * to disable RTT_NOM:
516 * a. DDR3: Write to MR1[9], MR1[6] and MR1[2]
517 * b. DDR4: Write to MR1[10:8]
518 */
519 mr1 &= ~(BIT(9) | BIT(6) | BIT(2));
520 stm32mp1_mode_register_write(priv, 1, mr1);
521
522 /*
523 * 4. For DDR4 only: Perform an MRS command
524 * (using MRCTRL0 and MRCTRL1 registers) to write to MR5[8:6]
525 * to disable RTT_PARK
526 */
527
528 /*
529 * 5. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
530 * to write to MR2[10:9], to disable RTT_WR
531 * (and therefore disable dynamic ODT).
532 * This applies for both DDR3 and DDR4.
533 */
534 mr2 &= ~GENMASK(10, 9);
535 stm32mp1_mode_register_write(priv, 2, mr2);
536
537 /*
538 * 6. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
539 * to disable the DLL. The timing of this MRS is automatically
540 * handled by the uMCTL2.
541 * a. DDR3: Write to MR1[0]
542 * b. DDR4: Write to MR1[0]
543 */
544 mr1 |= BIT(0);
545 stm32mp1_mode_register_write(priv, 1, mr1);
546
547 /*
548 * 7. Put the SDRAM into self-refresh mode by setting
549 * PWRCTL.selfref_sw = 1, and polling STAT.operating_mode to ensure
550 * the DDRC has entered self-refresh.
551 */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100552 mmio_setbits_32((uintptr_t)&priv->ctl->pwrctl,
Yann Gautier10a511c2018-07-24 17:18:19 +0200553 DDRCTRL_PWRCTL_SELFREF_SW);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100554 VERBOSE("[0x%lx] pwrctl = 0x%x\n",
555 (uintptr_t)&priv->ctl->pwrctl,
556 mmio_read_32((uintptr_t)&priv->ctl->pwrctl));
Yann Gautier10a511c2018-07-24 17:18:19 +0200557
558 /*
559 * 8. Wait until STAT.operating_mode[1:0]==11 indicating that the
560 * DWC_ddr_umctl2 core is in self-refresh mode.
561 * Ensure transition to self-refresh was due to software
562 * by checking that STAT.selfref_type[1:0]=2.
563 */
564 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_SR);
565
566 /*
567 * 9. Set the MSTR.dll_off_mode = 1.
568 * warning: MSTR.dll_off_mode is a quasi-dynamic type 2 field
569 */
570 stm32mp1_start_sw_done(priv->ctl);
571
Yann Gautier4156d4d2019-01-17 14:35:22 +0100572 mmio_setbits_32((uintptr_t)&priv->ctl->mstr, DDRCTRL_MSTR_DLL_OFF_MODE);
573 VERBOSE("[0x%lx] mstr = 0x%x\n",
574 (uintptr_t)&priv->ctl->mstr,
575 mmio_read_32((uintptr_t)&priv->ctl->mstr));
Yann Gautier10a511c2018-07-24 17:18:19 +0200576
577 stm32mp1_wait_sw_done_ack(priv->ctl);
578
579 /* 10. Change the clock frequency to the desired value. */
580
581 /*
582 * 11. Update any registers which may be required to change for the new
583 * frequency. This includes static and dynamic registers.
584 * This includes both uMCTL2 registers and PHY registers.
585 */
586
587 /* Change Bypass Mode Frequency Range */
Yann Gautier33667d22021-08-30 15:06:54 +0200588 if (clk_get_rate(DDRPHYC) < 100000000U) {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100589 mmio_clrbits_32((uintptr_t)&priv->phy->dllgcr,
Yann Gautier10a511c2018-07-24 17:18:19 +0200590 DDRPHYC_DLLGCR_BPS200);
591 } else {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100592 mmio_setbits_32((uintptr_t)&priv->phy->dllgcr,
Yann Gautier10a511c2018-07-24 17:18:19 +0200593 DDRPHYC_DLLGCR_BPS200);
594 }
595
Yann Gautier4156d4d2019-01-17 14:35:22 +0100596 mmio_setbits_32((uintptr_t)&priv->phy->acdllcr, DDRPHYC_ACDLLCR_DLLDIS);
Yann Gautier10a511c2018-07-24 17:18:19 +0200597
Yann Gautier4156d4d2019-01-17 14:35:22 +0100598 mmio_setbits_32((uintptr_t)&priv->phy->dx0dllcr,
Yann Gautier10a511c2018-07-24 17:18:19 +0200599 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100600 mmio_setbits_32((uintptr_t)&priv->phy->dx1dllcr,
Yann Gautier10a511c2018-07-24 17:18:19 +0200601 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier88f4fb82020-09-17 12:42:46 +0200602#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautier4156d4d2019-01-17 14:35:22 +0100603 mmio_setbits_32((uintptr_t)&priv->phy->dx2dllcr,
Yann Gautier10a511c2018-07-24 17:18:19 +0200604 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100605 mmio_setbits_32((uintptr_t)&priv->phy->dx3dllcr,
Yann Gautier10a511c2018-07-24 17:18:19 +0200606 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier88f4fb82020-09-17 12:42:46 +0200607#endif
Yann Gautier10a511c2018-07-24 17:18:19 +0200608
609 /* 12. Exit the self-refresh state by setting PWRCTL.selfref_sw = 0. */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100610 mmio_clrbits_32((uintptr_t)&priv->ctl->pwrctl,
Yann Gautier10a511c2018-07-24 17:18:19 +0200611 DDRCTRL_PWRCTL_SELFREF_SW);
612 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL);
613
614 /*
615 * 13. If ZQCTL0.dis_srx_zqcl = 0, the uMCTL2 performs a ZQCL command
616 * at this point.
617 */
618
619 /*
620 * 14. Perform MRS commands as required to re-program timing registers
621 * in the SDRAM for the new frequency
622 * (in particular, CL, CWL and WR may need to be changed).
623 */
624
625 /* 15. Write DBG1.dis_hif = 0 to re-enable reads and writes. */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100626 mmio_clrbits_32((uintptr_t)&priv->ctl->dbg1, DDRCTRL_DBG1_DIS_HIF);
627 VERBOSE("[0x%lx] dbg1 = 0x%x\n",
628 (uintptr_t)&priv->ctl->dbg1,
629 mmio_read_32((uintptr_t)&priv->ctl->dbg1));
Yann Gautier10a511c2018-07-24 17:18:19 +0200630}
631
632static void stm32mp1_refresh_disable(struct stm32mp1_ddrctl *ctl)
633{
634 stm32mp1_start_sw_done(ctl);
635 /* Quasi-dynamic register update*/
Yann Gautier4156d4d2019-01-17 14:35:22 +0100636 mmio_setbits_32((uintptr_t)&ctl->rfshctl3,
Yann Gautier10a511c2018-07-24 17:18:19 +0200637 DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100638 mmio_clrbits_32((uintptr_t)&ctl->pwrctl, DDRCTRL_PWRCTL_POWERDOWN_EN);
639 mmio_clrbits_32((uintptr_t)&ctl->dfimisc,
Yann Gautier10a511c2018-07-24 17:18:19 +0200640 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
641 stm32mp1_wait_sw_done_ack(ctl);
642}
643
644static void stm32mp1_refresh_restore(struct stm32mp1_ddrctl *ctl,
645 uint32_t rfshctl3, uint32_t pwrctl)
646{
647 stm32mp1_start_sw_done(ctl);
648 if ((rfshctl3 & DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH) == 0U) {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100649 mmio_clrbits_32((uintptr_t)&ctl->rfshctl3,
Yann Gautier10a511c2018-07-24 17:18:19 +0200650 DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
651 }
652 if ((pwrctl & DDRCTRL_PWRCTL_POWERDOWN_EN) != 0U) {
Yann Gautier4156d4d2019-01-17 14:35:22 +0100653 mmio_setbits_32((uintptr_t)&ctl->pwrctl,
Yann Gautier10a511c2018-07-24 17:18:19 +0200654 DDRCTRL_PWRCTL_POWERDOWN_EN);
655 }
Yann Gautier4156d4d2019-01-17 14:35:22 +0100656 mmio_setbits_32((uintptr_t)&ctl->dfimisc,
Yann Gautier10a511c2018-07-24 17:18:19 +0200657 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
658 stm32mp1_wait_sw_done_ack(ctl);
659}
660
661static int board_ddr_power_init(enum ddr_type ddr_type)
662{
Yann Gautierd82d4ff2019-02-14 11:15:03 +0100663 if (dt_pmic_status() > 0) {
Yann Gautier10a511c2018-07-24 17:18:19 +0200664 return pmic_ddr_power_init(ddr_type);
665 }
666
667 return 0;
668}
669
670void stm32mp1_ddr_init(struct ddr_info *priv,
671 struct stm32mp1_ddr_config *config)
672{
673 uint32_t pir;
Yann Gautier4156d4d2019-01-17 14:35:22 +0100674 int ret = -EINVAL;
Yann Gautier10a511c2018-07-24 17:18:19 +0200675
676 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) != 0U) {
677 ret = board_ddr_power_init(STM32MP_DDR3);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100678 } else if ((config->c_reg.mstr & DDRCTRL_MSTR_LPDDR2) != 0U) {
Yann Gautier10a511c2018-07-24 17:18:19 +0200679 ret = board_ddr_power_init(STM32MP_LPDDR2);
Yann Gautier4b549b22019-04-16 16:20:58 +0200680 } else if ((config->c_reg.mstr & DDRCTRL_MSTR_LPDDR3) != 0U) {
681 ret = board_ddr_power_init(STM32MP_LPDDR3);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100682 } else {
683 ERROR("DDR type not supported\n");
Yann Gautier10a511c2018-07-24 17:18:19 +0200684 }
685
686 if (ret != 0) {
687 panic();
688 }
689
690 VERBOSE("name = %s\n", config->info.name);
Yann Gautiera0781342021-09-07 09:07:35 +0200691 VERBOSE("speed = %u kHz\n", config->info.speed);
Yann Gautier10a511c2018-07-24 17:18:19 +0200692 VERBOSE("size = 0x%x\n", config->info.size);
693
694 /* DDR INIT SEQUENCE */
695
696 /*
697 * 1. Program the DWC_ddr_umctl2 registers
698 * nota: check DFIMISC.dfi_init_complete = 0
699 */
700
701 /* 1.1 RESETS: presetn, core_ddrc_rstn, aresetn */
702 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
703 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
704 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
705 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
706 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
707 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
708
709 /* 1.2. start CLOCK */
710 if (stm32mp1_ddr_clk_enable(priv, config->info.speed) != 0) {
711 panic();
712 }
713
714 /* 1.3. deassert reset */
715 /* De-assert PHY rstn and ctl_rstn via DPHYRST and DPHYCTLRST. */
716 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
717 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
718 /*
719 * De-assert presetn once the clocks are active
720 * and stable via DDRCAPBRST bit.
721 */
722 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
723
724 /* 1.4. wait 128 cycles to permit initialization of end logic */
725 udelay(2);
726 /* For PCLK = 133MHz => 1 us is enough, 2 to allow lower frequency */
727
728 /* 1.5. initialize registers ddr_umctl2 */
729 /* Stop uMCTL2 before PHY is ready */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100730 mmio_clrbits_32((uintptr_t)&priv->ctl->dfimisc,
Yann Gautier10a511c2018-07-24 17:18:19 +0200731 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100732 VERBOSE("[0x%lx] dfimisc = 0x%x\n",
733 (uintptr_t)&priv->ctl->dfimisc,
734 mmio_read_32((uintptr_t)&priv->ctl->dfimisc));
Yann Gautier10a511c2018-07-24 17:18:19 +0200735
736 set_reg(priv, REG_REG, &config->c_reg);
737
738 /* DDR3 = don't set DLLOFF for init mode */
739 if ((config->c_reg.mstr &
740 (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE))
741 == (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) {
742 VERBOSE("deactivate DLL OFF in mstr\n");
Yann Gautier4156d4d2019-01-17 14:35:22 +0100743 mmio_clrbits_32((uintptr_t)&priv->ctl->mstr,
Yann Gautier10a511c2018-07-24 17:18:19 +0200744 DDRCTRL_MSTR_DLL_OFF_MODE);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100745 VERBOSE("[0x%lx] mstr = 0x%x\n",
746 (uintptr_t)&priv->ctl->mstr,
747 mmio_read_32((uintptr_t)&priv->ctl->mstr));
Yann Gautier10a511c2018-07-24 17:18:19 +0200748 }
749
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 */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100754 mmio_clrsetbits_32((uintptr_t)&priv->ctl->init0,
Yann Gautier10a511c2018-07-24 17:18:19 +0200755 DDRCTRL_INIT0_SKIP_DRAM_INIT_MASK,
756 DDRCTRL_INIT0_SKIP_DRAM_INIT_NORMAL);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100757 VERBOSE("[0x%lx] init0 = 0x%x\n",
758 (uintptr_t)&priv->ctl->init0,
759 mmio_read_32((uintptr_t)&priv->ctl->init0));
Yann Gautier10a511c2018-07-24 17:18:19 +0200760
761 set_reg(priv, REG_PERF, &config->c_perf);
762
763 /* 2. deassert reset signal core_ddrc_rstn, aresetn and presetn */
764 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
765 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
766 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
767
768 /*
769 * 3. start PHY init by accessing relevant PUBL registers
770 * (DXGCR, DCR, PTR*, MR*, DTPR*)
771 */
772 set_reg(priv, REGPHY_REG, &config->p_reg);
773 set_reg(priv, REGPHY_TIMING, &config->p_timing);
Yann Gautier10a511c2018-07-24 17:18:19 +0200774
775 /* DDR3 = don't set DLLOFF for init mode */
776 if ((config->c_reg.mstr &
777 (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE))
778 == (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) {
779 VERBOSE("deactivate DLL OFF in mr1\n");
Yann Gautier4156d4d2019-01-17 14:35:22 +0100780 mmio_clrbits_32((uintptr_t)&priv->phy->mr1, BIT(0));
781 VERBOSE("[0x%lx] mr1 = 0x%x\n",
782 (uintptr_t)&priv->phy->mr1,
783 mmio_read_32((uintptr_t)&priv->phy->mr1));
Yann Gautier10a511c2018-07-24 17:18:19 +0200784 }
785
786 /*
787 * 4. Monitor PHY init status by polling PUBL register PGSR.IDONE
788 * Perform DDR PHY DRAM initialization and Gate Training Evaluation
789 */
790 stm32mp1_ddrphy_idone_wait(priv->phy);
791
792 /*
793 * 5. Indicate to PUBL that controller performs SDRAM initialization
794 * by setting PIR.INIT and PIR CTLDINIT and pool PGSR.IDONE
795 * DRAM init is done by PHY, init0.skip_dram.init = 1
796 */
797
798 pir = DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK | DDRPHYC_PIR_ZCAL |
799 DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_DRAMINIT | DDRPHYC_PIR_ICPC;
800
801 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) != 0U) {
802 pir |= DDRPHYC_PIR_DRAMRST; /* Only for DDR3 */
803 }
804
805 stm32mp1_ddrphy_init(priv->phy, pir);
806
807 /*
808 * 6. SET DFIMISC.dfi_init_complete_en to 1
809 * Enable quasi-dynamic register programming.
810 */
811 stm32mp1_start_sw_done(priv->ctl);
812
Yann Gautier4156d4d2019-01-17 14:35:22 +0100813 mmio_setbits_32((uintptr_t)&priv->ctl->dfimisc,
Yann Gautier10a511c2018-07-24 17:18:19 +0200814 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Yann Gautier4156d4d2019-01-17 14:35:22 +0100815 VERBOSE("[0x%lx] dfimisc = 0x%x\n",
816 (uintptr_t)&priv->ctl->dfimisc,
817 mmio_read_32((uintptr_t)&priv->ctl->dfimisc));
Yann Gautier10a511c2018-07-24 17:18:19 +0200818
819 stm32mp1_wait_sw_done_ack(priv->ctl);
820
821 /*
822 * 7. Wait for DWC_ddr_umctl2 to move to normal operation mode
823 * by monitoring STAT.operating_mode signal
824 */
825
826 /* Wait uMCTL2 ready */
827 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL);
828
829 /* Switch to DLL OFF mode */
830 if ((config->c_reg.mstr & DDRCTRL_MSTR_DLL_OFF_MODE) != 0U) {
831 stm32mp1_ddr3_dll_off(priv);
832 }
833
834 VERBOSE("DDR DQS training : ");
835
836 /*
837 * 8. Disable Auto refresh and power down by setting
838 * - RFSHCTL3.dis_au_refresh = 1
839 * - PWRCTL.powerdown_en = 0
840 * - DFIMISC.dfiinit_complete_en = 0
841 */
842 stm32mp1_refresh_disable(priv->ctl);
843
844 /*
845 * 9. Program PUBL PGCR to enable refresh during training
846 * and rank to train
847 * not done => keep the programed value in PGCR
848 */
849
850 /*
851 * 10. configure PUBL PIR register to specify which training step
852 * to run
Nicolas Le Bayon5def13e2021-09-10 12:03:38 +0200853 * RVTRN is executed only on LPDDR2/LPDDR3
Yann Gautier10a511c2018-07-24 17:18:19 +0200854 */
Nicolas Le Bayon5def13e2021-09-10 12:03:38 +0200855 pir = DDRPHYC_PIR_QSTRN;
856 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) == 0U) {
857 pir |= DDRPHYC_PIR_RVTRN;
858 }
859
860 stm32mp1_ddrphy_init(priv->phy, pir);
Yann Gautier10a511c2018-07-24 17:18:19 +0200861
862 /* 11. monitor PUB PGSR.IDONE to poll cpmpletion of training sequence */
863 stm32mp1_ddrphy_idone_wait(priv->phy);
864
865 /*
866 * 12. set back registers in step 8 to the orginal values if desidered
867 */
868 stm32mp1_refresh_restore(priv->ctl, config->c_reg.rfshctl3,
869 config->c_reg.pwrctl);
870
871 /* Enable uMCTL2 AXI port 0 */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100872 mmio_setbits_32((uintptr_t)&priv->ctl->pctrl_0,
873 DDRCTRL_PCTRL_N_PORT_EN);
874 VERBOSE("[0x%lx] pctrl_0 = 0x%x\n",
875 (uintptr_t)&priv->ctl->pctrl_0,
876 mmio_read_32((uintptr_t)&priv->ctl->pctrl_0));
Yann Gautier10a511c2018-07-24 17:18:19 +0200877
Yann Gautier88f4fb82020-09-17 12:42:46 +0200878#if STM32MP_DDR_DUAL_AXI_PORT
Yann Gautier10a511c2018-07-24 17:18:19 +0200879 /* Enable uMCTL2 AXI port 1 */
Yann Gautier4156d4d2019-01-17 14:35:22 +0100880 mmio_setbits_32((uintptr_t)&priv->ctl->pctrl_1,
881 DDRCTRL_PCTRL_N_PORT_EN);
882 VERBOSE("[0x%lx] pctrl_1 = 0x%x\n",
883 (uintptr_t)&priv->ctl->pctrl_1,
884 mmio_read_32((uintptr_t)&priv->ctl->pctrl_1));
Yann Gautier88f4fb82020-09-17 12:42:46 +0200885#endif
Yann Gautier10a511c2018-07-24 17:18:19 +0200886}