blob: 27d8b2c000d4bef11a6e161ff8e62a9ce2879762 [file] [log] [blame]
Yann Gautiercaf575b2018-07-24 17:18:19 +02001/*
Yann Gautier634591d2021-09-07 09:07:35 +02002 * Copyright (C) 2018-2022, 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 { \
27 .name = #x, \
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020028 .offset = offsetof(struct stm32mp_ddrctl, x), \
Yann Gautiercaf575b2018-07-24 17:18:19 +020029 .par_offset = offsetof(struct y, x) \
30 }
31
32#define DDRPHY_REG(x, y) \
33 { \
34 .name = #x, \
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020035 .offset = offsetof(struct stm32mp_ddrphy, x), \
Yann Gautiercaf575b2018-07-24 17:18:19 +020036 .par_offset = offsetof(struct y, x) \
37 }
38
Yann Gautier39d85f42019-02-25 13:44:27 +010039/*
40 * PARAMETERS: value get from device tree :
41 * size / order need to be aligned with binding
42 * modification NOT ALLOWED !!!
43 */
44#define DDRCTL_REG_REG_SIZE 25 /* st,ctl-reg */
45#define DDRCTL_REG_TIMING_SIZE 12 /* st,ctl-timing */
46#define DDRCTL_REG_MAP_SIZE 9 /* st,ctl-map */
Yann Gautier6d8c2442020-09-17 12:42:46 +020047#if STM32MP_DDR_DUAL_AXI_PORT
Yann Gautier39d85f42019-02-25 13:44:27 +010048#define DDRCTL_REG_PERF_SIZE 17 /* st,ctl-perf */
Yann Gautier6d8c2442020-09-17 12:42:46 +020049#else
50#define DDRCTL_REG_PERF_SIZE 11 /* st,ctl-perf */
51#endif
Yann Gautier39d85f42019-02-25 13:44:27 +010052
Yann Gautier6d8c2442020-09-17 12:42:46 +020053#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautier39d85f42019-02-25 13:44:27 +010054#define DDRPHY_REG_REG_SIZE 11 /* st,phy-reg */
Yann Gautier6d8c2442020-09-17 12:42:46 +020055#else
56#define DDRPHY_REG_REG_SIZE 9 /* st,phy-reg */
57#endif
Yann Gautier39d85f42019-02-25 13:44:27 +010058#define DDRPHY_REG_TIMING_SIZE 10 /* st,phy-timing */
59
Yann Gautiercaf575b2018-07-24 17:18:19 +020060#define DDRCTL_REG_REG(x) DDRCTL_REG(x, stm32mp1_ddrctrl_reg)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020061static const struct stm32mp_ddr_reg_desc ddr_reg[DDRCTL_REG_REG_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +020062 DDRCTL_REG_REG(mstr),
63 DDRCTL_REG_REG(mrctrl0),
64 DDRCTL_REG_REG(mrctrl1),
65 DDRCTL_REG_REG(derateen),
66 DDRCTL_REG_REG(derateint),
67 DDRCTL_REG_REG(pwrctl),
68 DDRCTL_REG_REG(pwrtmg),
69 DDRCTL_REG_REG(hwlpctl),
70 DDRCTL_REG_REG(rfshctl0),
71 DDRCTL_REG_REG(rfshctl3),
72 DDRCTL_REG_REG(crcparctl0),
73 DDRCTL_REG_REG(zqctl0),
74 DDRCTL_REG_REG(dfitmg0),
75 DDRCTL_REG_REG(dfitmg1),
76 DDRCTL_REG_REG(dfilpcfg0),
77 DDRCTL_REG_REG(dfiupd0),
78 DDRCTL_REG_REG(dfiupd1),
79 DDRCTL_REG_REG(dfiupd2),
80 DDRCTL_REG_REG(dfiphymstr),
81 DDRCTL_REG_REG(odtmap),
82 DDRCTL_REG_REG(dbg0),
83 DDRCTL_REG_REG(dbg1),
84 DDRCTL_REG_REG(dbgcmd),
85 DDRCTL_REG_REG(poisoncfg),
86 DDRCTL_REG_REG(pccfg),
87};
88
89#define DDRCTL_REG_TIMING(x) DDRCTL_REG(x, stm32mp1_ddrctrl_timing)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +020090static const struct stm32mp_ddr_reg_desc ddr_timing[DDRCTL_REG_TIMING_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +020091 DDRCTL_REG_TIMING(rfshtmg),
92 DDRCTL_REG_TIMING(dramtmg0),
93 DDRCTL_REG_TIMING(dramtmg1),
94 DDRCTL_REG_TIMING(dramtmg2),
95 DDRCTL_REG_TIMING(dramtmg3),
96 DDRCTL_REG_TIMING(dramtmg4),
97 DDRCTL_REG_TIMING(dramtmg5),
98 DDRCTL_REG_TIMING(dramtmg6),
99 DDRCTL_REG_TIMING(dramtmg7),
100 DDRCTL_REG_TIMING(dramtmg8),
101 DDRCTL_REG_TIMING(dramtmg14),
102 DDRCTL_REG_TIMING(odtcfg),
103};
104
105#define DDRCTL_REG_MAP(x) DDRCTL_REG(x, stm32mp1_ddrctrl_map)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200106static const struct stm32mp_ddr_reg_desc ddr_map[DDRCTL_REG_MAP_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200107 DDRCTL_REG_MAP(addrmap1),
108 DDRCTL_REG_MAP(addrmap2),
109 DDRCTL_REG_MAP(addrmap3),
110 DDRCTL_REG_MAP(addrmap4),
111 DDRCTL_REG_MAP(addrmap5),
112 DDRCTL_REG_MAP(addrmap6),
113 DDRCTL_REG_MAP(addrmap9),
114 DDRCTL_REG_MAP(addrmap10),
115 DDRCTL_REG_MAP(addrmap11),
116};
117
118#define DDRCTL_REG_PERF(x) DDRCTL_REG(x, stm32mp1_ddrctrl_perf)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200119static const struct stm32mp_ddr_reg_desc ddr_perf[DDRCTL_REG_PERF_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200120 DDRCTL_REG_PERF(sched),
121 DDRCTL_REG_PERF(sched1),
122 DDRCTL_REG_PERF(perfhpr1),
123 DDRCTL_REG_PERF(perflpr1),
124 DDRCTL_REG_PERF(perfwr1),
125 DDRCTL_REG_PERF(pcfgr_0),
126 DDRCTL_REG_PERF(pcfgw_0),
127 DDRCTL_REG_PERF(pcfgqos0_0),
128 DDRCTL_REG_PERF(pcfgqos1_0),
129 DDRCTL_REG_PERF(pcfgwqos0_0),
130 DDRCTL_REG_PERF(pcfgwqos1_0),
Yann Gautier6d8c2442020-09-17 12:42:46 +0200131#if STM32MP_DDR_DUAL_AXI_PORT
Yann Gautiercaf575b2018-07-24 17:18:19 +0200132 DDRCTL_REG_PERF(pcfgr_1),
133 DDRCTL_REG_PERF(pcfgw_1),
134 DDRCTL_REG_PERF(pcfgqos0_1),
135 DDRCTL_REG_PERF(pcfgqos1_1),
136 DDRCTL_REG_PERF(pcfgwqos0_1),
137 DDRCTL_REG_PERF(pcfgwqos1_1),
Yann Gautier6d8c2442020-09-17 12:42:46 +0200138#endif
Yann Gautiercaf575b2018-07-24 17:18:19 +0200139};
140
141#define DDRPHY_REG_REG(x) DDRPHY_REG(x, stm32mp1_ddrphy_reg)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200142static const struct stm32mp_ddr_reg_desc ddrphy_reg[DDRPHY_REG_REG_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200143 DDRPHY_REG_REG(pgcr),
144 DDRPHY_REG_REG(aciocr),
145 DDRPHY_REG_REG(dxccr),
146 DDRPHY_REG_REG(dsgcr),
147 DDRPHY_REG_REG(dcr),
148 DDRPHY_REG_REG(odtcr),
149 DDRPHY_REG_REG(zq0cr1),
150 DDRPHY_REG_REG(dx0gcr),
151 DDRPHY_REG_REG(dx1gcr),
Yann Gautier6d8c2442020-09-17 12:42:46 +0200152#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200153 DDRPHY_REG_REG(dx2gcr),
154 DDRPHY_REG_REG(dx3gcr),
Yann Gautier6d8c2442020-09-17 12:42:46 +0200155#endif
Yann Gautiercaf575b2018-07-24 17:18:19 +0200156};
157
158#define DDRPHY_REG_TIMING(x) DDRPHY_REG(x, stm32mp1_ddrphy_timing)
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200159static const struct stm32mp_ddr_reg_desc ddrphy_timing[DDRPHY_REG_TIMING_SIZE] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200160 DDRPHY_REG_TIMING(ptr0),
161 DDRPHY_REG_TIMING(ptr1),
162 DDRPHY_REG_TIMING(ptr2),
163 DDRPHY_REG_TIMING(dtpr0),
164 DDRPHY_REG_TIMING(dtpr1),
165 DDRPHY_REG_TIMING(dtpr2),
166 DDRPHY_REG_TIMING(mr0),
167 DDRPHY_REG_TIMING(mr1),
168 DDRPHY_REG_TIMING(mr2),
169 DDRPHY_REG_TIMING(mr3),
170};
171
Yann Gautier39d85f42019-02-25 13:44:27 +0100172/*
173 * REGISTERS ARRAY: used to parse device tree and interactive mode
174 */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200175static const struct stm32mp_ddr_reg_info ddr_registers[REG_TYPE_NB] = {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200176 [REG_REG] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100177 .name = "static",
178 .desc = ddr_reg,
Yann Gautier39d85f42019-02-25 13:44:27 +0100179 .size = DDRCTL_REG_REG_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100180 .base = DDR_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200181 },
182 [REG_TIMING] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100183 .name = "timing",
184 .desc = ddr_timing,
Yann Gautier39d85f42019-02-25 13:44:27 +0100185 .size = DDRCTL_REG_TIMING_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100186 .base = DDR_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200187 },
188 [REG_PERF] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100189 .name = "perf",
190 .desc = ddr_perf,
Yann Gautier39d85f42019-02-25 13:44:27 +0100191 .size = DDRCTL_REG_PERF_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100192 .base = DDR_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200193 },
194 [REG_MAP] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100195 .name = "map",
196 .desc = ddr_map,
Yann Gautier39d85f42019-02-25 13:44:27 +0100197 .size = DDRCTL_REG_MAP_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100198 .base = DDR_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200199 },
200 [REGPHY_REG] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100201 .name = "static",
202 .desc = ddrphy_reg,
Yann Gautier39d85f42019-02-25 13:44:27 +0100203 .size = DDRPHY_REG_REG_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100204 .base = DDRPHY_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200205 },
206 [REGPHY_TIMING] = {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100207 .name = "timing",
208 .desc = ddrphy_timing,
Yann Gautier39d85f42019-02-25 13:44:27 +0100209 .size = DDRPHY_REG_TIMING_SIZE,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100210 .base = DDRPHY_BASE
Yann Gautiercaf575b2018-07-24 17:18:19 +0200211 },
212};
213
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200214static void stm32mp1_ddrphy_idone_wait(struct stm32mp_ddrphy *phy)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200215{
216 uint32_t pgsr;
217 int error = 0;
Yann Gautier2299d572019-02-14 11:14:39 +0100218 uint64_t timeout = timeout_init_us(TIMEOUT_US_1S);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200219
220 do {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100221 pgsr = mmio_read_32((uintptr_t)&phy->pgsr);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200222
Yann Gautier2299d572019-02-14 11:14:39 +0100223 VERBOSE(" > [0x%lx] pgsr = 0x%x &\n",
224 (uintptr_t)&phy->pgsr, pgsr);
225
226 if (timeout_elapsed(timeout)) {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200227 panic();
228 }
Yann Gautier2299d572019-02-14 11:14:39 +0100229
Yann Gautiercaf575b2018-07-24 17:18:19 +0200230 if ((pgsr & DDRPHYC_PGSR_DTERR) != 0U) {
231 VERBOSE("DQS Gate Trainig Error\n");
232 error++;
233 }
Yann Gautier2299d572019-02-14 11:14:39 +0100234
Yann Gautiercaf575b2018-07-24 17:18:19 +0200235 if ((pgsr & DDRPHYC_PGSR_DTIERR) != 0U) {
236 VERBOSE("DQS Gate Trainig Intermittent Error\n");
237 error++;
238 }
Yann Gautier2299d572019-02-14 11:14:39 +0100239
Yann Gautiercaf575b2018-07-24 17:18:19 +0200240 if ((pgsr & DDRPHYC_PGSR_DFTERR) != 0U) {
241 VERBOSE("DQS Drift Error\n");
242 error++;
243 }
Yann Gautier2299d572019-02-14 11:14:39 +0100244
Yann Gautiercaf575b2018-07-24 17:18:19 +0200245 if ((pgsr & DDRPHYC_PGSR_RVERR) != 0U) {
246 VERBOSE("Read Valid Training Error\n");
247 error++;
248 }
Yann Gautier2299d572019-02-14 11:14:39 +0100249
Yann Gautiercaf575b2018-07-24 17:18:19 +0200250 if ((pgsr & DDRPHYC_PGSR_RVEIRR) != 0U) {
251 VERBOSE("Read Valid Training Intermittent Error\n");
252 error++;
253 }
Yann Gautier2299d572019-02-14 11:14:39 +0100254 } while (((pgsr & DDRPHYC_PGSR_IDONE) == 0U) && (error == 0));
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100255 VERBOSE("\n[0x%lx] pgsr = 0x%x\n",
256 (uintptr_t)&phy->pgsr, pgsr);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200257}
258
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200259static void stm32mp1_ddrphy_init(struct stm32mp_ddrphy *phy, uint32_t pir)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200260{
261 uint32_t pir_init = pir | DDRPHYC_PIR_INIT;
262
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100263 mmio_write_32((uintptr_t)&phy->pir, pir_init);
264 VERBOSE("[0x%lx] pir = 0x%x -> 0x%x\n",
265 (uintptr_t)&phy->pir, pir_init,
266 mmio_read_32((uintptr_t)&phy->pir));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200267
268 /* Need to wait 10 configuration clock before start polling */
269 udelay(10);
270
271 /* Wait DRAM initialization and Gate Training Evaluation complete */
272 stm32mp1_ddrphy_idone_wait(phy);
273}
274
Yann Gautiercaf575b2018-07-24 17:18:19 +0200275/* Wait quasi dynamic register update */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200276static void stm32mp1_wait_operating_mode(struct stm32mp_ddr_priv *priv, uint32_t mode)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200277{
Yann Gautier2299d572019-02-14 11:14:39 +0100278 uint64_t timeout;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200279 uint32_t stat;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200280 int break_loop = 0;
281
Yann Gautier2299d572019-02-14 11:14:39 +0100282 timeout = timeout_init_us(TIMEOUT_US_1S);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200283 for ( ; ; ) {
Yann Gautier2299d572019-02-14 11:14:39 +0100284 uint32_t operating_mode;
285 uint32_t selref_type;
286
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100287 stat = mmio_read_32((uintptr_t)&priv->ctl->stat);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200288 operating_mode = stat & DDRCTRL_STAT_OPERATING_MODE_MASK;
289 selref_type = stat & DDRCTRL_STAT_SELFREF_TYPE_MASK;
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100290 VERBOSE("[0x%lx] stat = 0x%x\n",
291 (uintptr_t)&priv->ctl->stat, stat);
Yann Gautier2299d572019-02-14 11:14:39 +0100292 if (timeout_elapsed(timeout)) {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200293 panic();
294 }
295
296 if (mode == DDRCTRL_STAT_OPERATING_MODE_SR) {
297 /*
298 * Self-refresh due to software
299 * => checking also STAT.selfref_type.
300 */
301 if ((operating_mode ==
302 DDRCTRL_STAT_OPERATING_MODE_SR) &&
303 (selref_type == DDRCTRL_STAT_SELFREF_TYPE_SR)) {
304 break_loop = 1;
305 }
306 } else if (operating_mode == mode) {
307 break_loop = 1;
308 } else if ((mode == DDRCTRL_STAT_OPERATING_MODE_NORMAL) &&
309 (operating_mode == DDRCTRL_STAT_OPERATING_MODE_SR) &&
310 (selref_type == DDRCTRL_STAT_SELFREF_TYPE_ASR)) {
311 /* Normal mode: handle also automatic self refresh */
312 break_loop = 1;
313 }
314
315 if (break_loop == 1) {
316 break;
317 }
318 }
319
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100320 VERBOSE("[0x%lx] stat = 0x%x\n",
321 (uintptr_t)&priv->ctl->stat, stat);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200322}
323
324/* Mode Register Writes (MRW or MRS) */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200325static void stm32mp1_mode_register_write(struct stm32mp_ddr_priv *priv, uint8_t addr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200326 uint32_t data)
327{
328 uint32_t mrctrl0;
329
330 VERBOSE("MRS: %d = %x\n", addr, data);
331
332 /*
333 * 1. Poll MRSTAT.mr_wr_busy until it is '0'.
334 * This checks that there is no outstanding MR transaction.
335 * No write should be performed to MRCTRL0 and MRCTRL1
336 * if MRSTAT.mr_wr_busy = 1.
337 */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100338 while ((mmio_read_32((uintptr_t)&priv->ctl->mrstat) &
Yann Gautiercaf575b2018-07-24 17:18:19 +0200339 DDRCTRL_MRSTAT_MR_WR_BUSY) != 0U) {
340 ;
341 }
342
343 /*
344 * 2. Write the MRCTRL0.mr_type, MRCTRL0.mr_addr, MRCTRL0.mr_rank
345 * and (for MRWs) MRCTRL1.mr_data to define the MR transaction.
346 */
347 mrctrl0 = DDRCTRL_MRCTRL0_MR_TYPE_WRITE |
348 DDRCTRL_MRCTRL0_MR_RANK_ALL |
349 (((uint32_t)addr << DDRCTRL_MRCTRL0_MR_ADDR_SHIFT) &
350 DDRCTRL_MRCTRL0_MR_ADDR_MASK);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100351 mmio_write_32((uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
352 VERBOSE("[0x%lx] mrctrl0 = 0x%x (0x%x)\n",
353 (uintptr_t)&priv->ctl->mrctrl0,
354 mmio_read_32((uintptr_t)&priv->ctl->mrctrl0), mrctrl0);
355 mmio_write_32((uintptr_t)&priv->ctl->mrctrl1, data);
356 VERBOSE("[0x%lx] mrctrl1 = 0x%x\n",
357 (uintptr_t)&priv->ctl->mrctrl1,
358 mmio_read_32((uintptr_t)&priv->ctl->mrctrl1));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200359
360 /*
361 * 3. In a separate APB transaction, write the MRCTRL0.mr_wr to 1. This
362 * bit is self-clearing, and triggers the MR transaction.
363 * The uMCTL2 then asserts the MRSTAT.mr_wr_busy while it performs
364 * the MR transaction to SDRAM, and no further access can be
365 * initiated until it is deasserted.
366 */
367 mrctrl0 |= DDRCTRL_MRCTRL0_MR_WR;
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100368 mmio_write_32((uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200369
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100370 while ((mmio_read_32((uintptr_t)&priv->ctl->mrstat) &
Yann Gautiercaf575b2018-07-24 17:18:19 +0200371 DDRCTRL_MRSTAT_MR_WR_BUSY) != 0U) {
372 ;
373 }
374
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100375 VERBOSE("[0x%lx] mrctrl0 = 0x%x\n",
376 (uintptr_t)&priv->ctl->mrctrl0, mrctrl0);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200377}
378
379/* Switch DDR3 from DLL-on to DLL-off */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200380static void stm32mp1_ddr3_dll_off(struct stm32mp_ddr_priv *priv)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200381{
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100382 uint32_t mr1 = mmio_read_32((uintptr_t)&priv->phy->mr1);
383 uint32_t mr2 = mmio_read_32((uintptr_t)&priv->phy->mr2);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200384 uint32_t dbgcam;
385
386 VERBOSE("mr1: 0x%x\n", mr1);
387 VERBOSE("mr2: 0x%x\n", mr2);
388
389 /*
390 * 1. Set the DBG1.dis_hif = 1.
391 * This prevents further reads/writes being received on the HIF.
392 */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100393 mmio_setbits_32((uintptr_t)&priv->ctl->dbg1, DDRCTRL_DBG1_DIS_HIF);
394 VERBOSE("[0x%lx] dbg1 = 0x%x\n",
395 (uintptr_t)&priv->ctl->dbg1,
396 mmio_read_32((uintptr_t)&priv->ctl->dbg1));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200397
398 /*
399 * 2. Ensure all commands have been flushed from the uMCTL2 by polling
400 * DBGCAM.wr_data_pipeline_empty = 1,
401 * DBGCAM.rd_data_pipeline_empty = 1,
402 * DBGCAM.dbg_wr_q_depth = 0 ,
403 * DBGCAM.dbg_lpr_q_depth = 0, and
404 * DBGCAM.dbg_hpr_q_depth = 0.
405 */
406 do {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100407 dbgcam = mmio_read_32((uintptr_t)&priv->ctl->dbgcam);
408 VERBOSE("[0x%lx] dbgcam = 0x%x\n",
409 (uintptr_t)&priv->ctl->dbgcam, dbgcam);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200410 } while ((((dbgcam & DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY) ==
411 DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY)) &&
412 ((dbgcam & DDRCTRL_DBGCAM_DBG_Q_DEPTH) == 0U));
413
414 /*
415 * 3. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
416 * to disable RTT_NOM:
417 * a. DDR3: Write to MR1[9], MR1[6] and MR1[2]
418 * b. DDR4: Write to MR1[10:8]
419 */
420 mr1 &= ~(BIT(9) | BIT(6) | BIT(2));
421 stm32mp1_mode_register_write(priv, 1, mr1);
422
423 /*
424 * 4. For DDR4 only: Perform an MRS command
425 * (using MRCTRL0 and MRCTRL1 registers) to write to MR5[8:6]
426 * to disable RTT_PARK
427 */
428
429 /*
430 * 5. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
431 * to write to MR2[10:9], to disable RTT_WR
432 * (and therefore disable dynamic ODT).
433 * This applies for both DDR3 and DDR4.
434 */
435 mr2 &= ~GENMASK(10, 9);
436 stm32mp1_mode_register_write(priv, 2, mr2);
437
438 /*
439 * 6. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers)
440 * to disable the DLL. The timing of this MRS is automatically
441 * handled by the uMCTL2.
442 * a. DDR3: Write to MR1[0]
443 * b. DDR4: Write to MR1[0]
444 */
445 mr1 |= BIT(0);
446 stm32mp1_mode_register_write(priv, 1, mr1);
447
448 /*
449 * 7. Put the SDRAM into self-refresh mode by setting
450 * PWRCTL.selfref_sw = 1, and polling STAT.operating_mode to ensure
451 * the DDRC has entered self-refresh.
452 */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100453 mmio_setbits_32((uintptr_t)&priv->ctl->pwrctl,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200454 DDRCTRL_PWRCTL_SELFREF_SW);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100455 VERBOSE("[0x%lx] pwrctl = 0x%x\n",
456 (uintptr_t)&priv->ctl->pwrctl,
457 mmio_read_32((uintptr_t)&priv->ctl->pwrctl));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200458
459 /*
460 * 8. Wait until STAT.operating_mode[1:0]==11 indicating that the
461 * DWC_ddr_umctl2 core is in self-refresh mode.
462 * Ensure transition to self-refresh was due to software
463 * by checking that STAT.selfref_type[1:0]=2.
464 */
465 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_SR);
466
467 /*
468 * 9. Set the MSTR.dll_off_mode = 1.
469 * warning: MSTR.dll_off_mode is a quasi-dynamic type 2 field
470 */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200471 stm32mp_ddr_start_sw_done(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200472
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100473 mmio_setbits_32((uintptr_t)&priv->ctl->mstr, DDRCTRL_MSTR_DLL_OFF_MODE);
474 VERBOSE("[0x%lx] mstr = 0x%x\n",
475 (uintptr_t)&priv->ctl->mstr,
476 mmio_read_32((uintptr_t)&priv->ctl->mstr));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200477
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200478 stm32mp_ddr_wait_sw_done_ack(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200479
480 /* 10. Change the clock frequency to the desired value. */
481
482 /*
483 * 11. Update any registers which may be required to change for the new
484 * frequency. This includes static and dynamic registers.
485 * This includes both uMCTL2 registers and PHY registers.
486 */
487
488 /* Change Bypass Mode Frequency Range */
Yann Gautiera205a5c2021-08-30 15:06:54 +0200489 if (clk_get_rate(DDRPHYC) < 100000000U) {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100490 mmio_clrbits_32((uintptr_t)&priv->phy->dllgcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200491 DDRPHYC_DLLGCR_BPS200);
492 } else {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100493 mmio_setbits_32((uintptr_t)&priv->phy->dllgcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200494 DDRPHYC_DLLGCR_BPS200);
495 }
496
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100497 mmio_setbits_32((uintptr_t)&priv->phy->acdllcr, DDRPHYC_ACDLLCR_DLLDIS);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200498
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100499 mmio_setbits_32((uintptr_t)&priv->phy->dx0dllcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200500 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100501 mmio_setbits_32((uintptr_t)&priv->phy->dx1dllcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200502 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier6d8c2442020-09-17 12:42:46 +0200503#if STM32MP_DDR_32BIT_INTERFACE
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100504 mmio_setbits_32((uintptr_t)&priv->phy->dx2dllcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200505 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100506 mmio_setbits_32((uintptr_t)&priv->phy->dx3dllcr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200507 DDRPHYC_DXNDLLCR_DLLDIS);
Yann Gautier6d8c2442020-09-17 12:42:46 +0200508#endif
Yann Gautiercaf575b2018-07-24 17:18:19 +0200509
510 /* 12. Exit the self-refresh state by setting PWRCTL.selfref_sw = 0. */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100511 mmio_clrbits_32((uintptr_t)&priv->ctl->pwrctl,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200512 DDRCTRL_PWRCTL_SELFREF_SW);
513 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL);
514
515 /*
516 * 13. If ZQCTL0.dis_srx_zqcl = 0, the uMCTL2 performs a ZQCL command
517 * at this point.
518 */
519
520 /*
521 * 14. Perform MRS commands as required to re-program timing registers
522 * in the SDRAM for the new frequency
523 * (in particular, CL, CWL and WR may need to be changed).
524 */
525
526 /* 15. Write DBG1.dis_hif = 0 to re-enable reads and writes. */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100527 mmio_clrbits_32((uintptr_t)&priv->ctl->dbg1, DDRCTRL_DBG1_DIS_HIF);
528 VERBOSE("[0x%lx] dbg1 = 0x%x\n",
529 (uintptr_t)&priv->ctl->dbg1,
530 mmio_read_32((uintptr_t)&priv->ctl->dbg1));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200531}
532
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200533static void stm32mp1_refresh_disable(struct stm32mp_ddrctl *ctl)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200534{
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200535 stm32mp_ddr_start_sw_done(ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200536 /* Quasi-dynamic register update*/
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100537 mmio_setbits_32((uintptr_t)&ctl->rfshctl3,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200538 DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100539 mmio_clrbits_32((uintptr_t)&ctl->pwrctl, DDRCTRL_PWRCTL_POWERDOWN_EN);
540 mmio_clrbits_32((uintptr_t)&ctl->dfimisc,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200541 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200542 stm32mp_ddr_wait_sw_done_ack(ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200543}
544
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200545static void stm32mp1_refresh_restore(struct stm32mp_ddrctl *ctl,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200546 uint32_t rfshctl3, uint32_t pwrctl)
547{
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200548 stm32mp_ddr_start_sw_done(ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200549 if ((rfshctl3 & DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH) == 0U) {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100550 mmio_clrbits_32((uintptr_t)&ctl->rfshctl3,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200551 DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH);
552 }
553 if ((pwrctl & DDRCTRL_PWRCTL_POWERDOWN_EN) != 0U) {
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100554 mmio_setbits_32((uintptr_t)&ctl->pwrctl,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200555 DDRCTRL_PWRCTL_POWERDOWN_EN);
556 }
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100557 mmio_setbits_32((uintptr_t)&ctl->dfimisc,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200558 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200559 stm32mp_ddr_wait_sw_done_ack(ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200560}
561
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200562void stm32mp1_ddr_init(struct stm32mp_ddr_priv *priv,
563 struct stm32mp_ddr_config *config)
Yann Gautiercaf575b2018-07-24 17:18:19 +0200564{
565 uint32_t pir;
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100566 int ret = -EINVAL;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200567
568 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) != 0U) {
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200569 ret = stm32mp_board_ddr_power_init(STM32MP_DDR3);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100570 } else if ((config->c_reg.mstr & DDRCTRL_MSTR_LPDDR2) != 0U) {
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200571 ret = stm32mp_board_ddr_power_init(STM32MP_LPDDR2);
Yann Gautier917a00c2019-04-16 16:20:58 +0200572 } else if ((config->c_reg.mstr & DDRCTRL_MSTR_LPDDR3) != 0U) {
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200573 ret = stm32mp_board_ddr_power_init(STM32MP_LPDDR3);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100574 } else {
575 ERROR("DDR type not supported\n");
Yann Gautiercaf575b2018-07-24 17:18:19 +0200576 }
577
578 if (ret != 0) {
579 panic();
580 }
581
582 VERBOSE("name = %s\n", config->info.name);
Yann Gautier634591d2021-09-07 09:07:35 +0200583 VERBOSE("speed = %u kHz\n", config->info.speed);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200584 VERBOSE("size = 0x%x\n", config->info.size);
585
586 /* DDR INIT SEQUENCE */
587
588 /*
589 * 1. Program the DWC_ddr_umctl2 registers
590 * nota: check DFIMISC.dfi_init_complete = 0
591 */
592
593 /* 1.1 RESETS: presetn, core_ddrc_rstn, aresetn */
594 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
595 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
596 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
597 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
598 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
599 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
600
601 /* 1.2. start CLOCK */
602 if (stm32mp1_ddr_clk_enable(priv, config->info.speed) != 0) {
603 panic();
604 }
605
606 /* 1.3. deassert reset */
607 /* De-assert PHY rstn and ctl_rstn via DPHYRST and DPHYCTLRST. */
608 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST);
609 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST);
610 /*
611 * De-assert presetn once the clocks are active
612 * and stable via DDRCAPBRST bit.
613 */
614 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST);
615
616 /* 1.4. wait 128 cycles to permit initialization of end logic */
617 udelay(2);
618 /* For PCLK = 133MHz => 1 us is enough, 2 to allow lower frequency */
619
620 /* 1.5. initialize registers ddr_umctl2 */
621 /* Stop uMCTL2 before PHY is ready */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100622 mmio_clrbits_32((uintptr_t)&priv->ctl->dfimisc,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200623 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100624 VERBOSE("[0x%lx] dfimisc = 0x%x\n",
625 (uintptr_t)&priv->ctl->dfimisc,
626 mmio_read_32((uintptr_t)&priv->ctl->dfimisc));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200627
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200628 stm32mp_ddr_set_reg(priv, REG_REG, &config->c_reg, ddr_registers);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200629
630 /* DDR3 = don't set DLLOFF for init mode */
631 if ((config->c_reg.mstr &
632 (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE))
633 == (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) {
634 VERBOSE("deactivate DLL OFF in mstr\n");
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100635 mmio_clrbits_32((uintptr_t)&priv->ctl->mstr,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200636 DDRCTRL_MSTR_DLL_OFF_MODE);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100637 VERBOSE("[0x%lx] mstr = 0x%x\n",
638 (uintptr_t)&priv->ctl->mstr,
639 mmio_read_32((uintptr_t)&priv->ctl->mstr));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200640 }
641
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200642 stm32mp_ddr_set_reg(priv, REG_TIMING, &config->c_timing, ddr_registers);
643 stm32mp_ddr_set_reg(priv, REG_MAP, &config->c_map, ddr_registers);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200644
645 /* Skip CTRL init, SDRAM init is done by PHY PUBL */
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100646 mmio_clrsetbits_32((uintptr_t)&priv->ctl->init0,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200647 DDRCTRL_INIT0_SKIP_DRAM_INIT_MASK,
648 DDRCTRL_INIT0_SKIP_DRAM_INIT_NORMAL);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100649 VERBOSE("[0x%lx] init0 = 0x%x\n",
650 (uintptr_t)&priv->ctl->init0,
651 mmio_read_32((uintptr_t)&priv->ctl->init0));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200652
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200653 stm32mp_ddr_set_reg(priv, REG_PERF, &config->c_perf, ddr_registers);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200654
655 /* 2. deassert reset signal core_ddrc_rstn, aresetn and presetn */
656 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST);
657 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST);
658 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST);
659
660 /*
661 * 3. start PHY init by accessing relevant PUBL registers
662 * (DXGCR, DCR, PTR*, MR*, DTPR*)
663 */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200664 stm32mp_ddr_set_reg(priv, REGPHY_REG, &config->p_reg, ddr_registers);
665 stm32mp_ddr_set_reg(priv, REGPHY_TIMING, &config->p_timing, ddr_registers);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200666
667 /* DDR3 = don't set DLLOFF for init mode */
668 if ((config->c_reg.mstr &
669 (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE))
670 == (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) {
671 VERBOSE("deactivate DLL OFF in mr1\n");
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100672 mmio_clrbits_32((uintptr_t)&priv->phy->mr1, BIT(0));
673 VERBOSE("[0x%lx] mr1 = 0x%x\n",
674 (uintptr_t)&priv->phy->mr1,
675 mmio_read_32((uintptr_t)&priv->phy->mr1));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200676 }
677
678 /*
679 * 4. Monitor PHY init status by polling PUBL register PGSR.IDONE
680 * Perform DDR PHY DRAM initialization and Gate Training Evaluation
681 */
682 stm32mp1_ddrphy_idone_wait(priv->phy);
683
684 /*
685 * 5. Indicate to PUBL that controller performs SDRAM initialization
686 * by setting PIR.INIT and PIR CTLDINIT and pool PGSR.IDONE
687 * DRAM init is done by PHY, init0.skip_dram.init = 1
688 */
689
690 pir = DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK | DDRPHYC_PIR_ZCAL |
691 DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_DRAMINIT | DDRPHYC_PIR_ICPC;
692
693 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) != 0U) {
694 pir |= DDRPHYC_PIR_DRAMRST; /* Only for DDR3 */
695 }
696
697 stm32mp1_ddrphy_init(priv->phy, pir);
698
699 /*
700 * 6. SET DFIMISC.dfi_init_complete_en to 1
701 * Enable quasi-dynamic register programming.
702 */
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200703 stm32mp_ddr_start_sw_done(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200704
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100705 mmio_setbits_32((uintptr_t)&priv->ctl->dfimisc,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200706 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100707 VERBOSE("[0x%lx] dfimisc = 0x%x\n",
708 (uintptr_t)&priv->ctl->dfimisc,
709 mmio_read_32((uintptr_t)&priv->ctl->dfimisc));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200710
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200711 stm32mp_ddr_wait_sw_done_ack(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200712
713 /*
714 * 7. Wait for DWC_ddr_umctl2 to move to normal operation mode
715 * by monitoring STAT.operating_mode signal
716 */
717
718 /* Wait uMCTL2 ready */
719 stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL);
720
721 /* Switch to DLL OFF mode */
722 if ((config->c_reg.mstr & DDRCTRL_MSTR_DLL_OFF_MODE) != 0U) {
723 stm32mp1_ddr3_dll_off(priv);
724 }
725
726 VERBOSE("DDR DQS training : ");
727
728 /*
729 * 8. Disable Auto refresh and power down by setting
730 * - RFSHCTL3.dis_au_refresh = 1
731 * - PWRCTL.powerdown_en = 0
732 * - DFIMISC.dfiinit_complete_en = 0
733 */
734 stm32mp1_refresh_disable(priv->ctl);
735
736 /*
737 * 9. Program PUBL PGCR to enable refresh during training
738 * and rank to train
739 * not done => keep the programed value in PGCR
740 */
741
742 /*
743 * 10. configure PUBL PIR register to specify which training step
744 * to run
Nicolas Le Bayone22424a2021-09-10 12:03:38 +0200745 * RVTRN is executed only on LPDDR2/LPDDR3
Yann Gautiercaf575b2018-07-24 17:18:19 +0200746 */
Nicolas Le Bayone22424a2021-09-10 12:03:38 +0200747 pir = DDRPHYC_PIR_QSTRN;
748 if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) == 0U) {
749 pir |= DDRPHYC_PIR_RVTRN;
750 }
751
752 stm32mp1_ddrphy_init(priv->phy, pir);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200753
754 /* 11. monitor PUB PGSR.IDONE to poll cpmpletion of training sequence */
755 stm32mp1_ddrphy_idone_wait(priv->phy);
756
757 /*
Elyes Haouas2be03c02023-02-13 09:14:48 +0100758 * 12. set back registers in step 8 to the original values if desidered
Yann Gautiercaf575b2018-07-24 17:18:19 +0200759 */
760 stm32mp1_refresh_restore(priv->ctl, config->c_reg.rfshctl3,
761 config->c_reg.pwrctl);
762
Nicolas Le Bayon8ce825f2021-05-18 10:01:30 +0200763 stm32mp_ddr_enable_axi_port(priv->ctl);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200764}