blob: b21c8949fe7ca7e3fd2b544aed348363a81c6e79 [file] [log] [blame]
Yann Gautiercaf575b2018-07-24 17:18:19 +02001/*
Yann Gautiera55169b2020-01-10 18:18:59 +01002 * Copyright (C) 2018-2020, 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 Gautiercaf575b2018-07-24 17:18:19 +02007#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Yann Gautiercaf575b2018-07-24 17:18:19 +02009#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
Yann Gautiercaf575b2018-07-24 17:18:19 +020011#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <arch_helpers.h>
14#include <common/debug.h>
Andre Przywaracc99f3f2020-03-26 12:51:21 +000015#include <common/fdt_wrappers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <drivers/st/stm32mp1_ddr.h>
17#include <drivers/st/stm32mp1_ddr_helpers.h>
18#include <drivers/st/stm32mp1_ram.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000019#include <lib/mmio.h>
20
Yann Gautiercaf575b2018-07-24 17:18:19 +020021#define DDR_PATTERN 0xAAAAAAAAU
22#define DDR_ANTIPATTERN 0x55555555U
23
24static struct ddr_info ddr_priv_data;
25
Yann Gautier7b7e4bf2019-01-17 19:16:03 +010026int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed)
Yann Gautiercaf575b2018-07-24 17:18:19 +020027{
28 unsigned long ddrphy_clk, ddr_clk, mem_speed_hz;
29
30 ddr_enable_clock();
31
Yann Gautiera2e2a302019-02-14 11:13:39 +010032 ddrphy_clk = stm32mp_clk_get_rate(DDRPHYC);
Yann Gautiercaf575b2018-07-24 17:18:19 +020033
Yann Gautier7b7e4bf2019-01-17 19:16:03 +010034 VERBOSE("DDR: mem_speed (%d kHz), RCC %ld kHz\n",
35 mem_speed, ddrphy_clk / 1000U);
Yann Gautiercaf575b2018-07-24 17:18:19 +020036
Yann Gautier7b7e4bf2019-01-17 19:16:03 +010037 mem_speed_hz = mem_speed * 1000U;
Yann Gautiercaf575b2018-07-24 17:18:19 +020038
39 /* Max 10% frequency delta */
40 if (ddrphy_clk > mem_speed_hz) {
41 ddr_clk = ddrphy_clk - mem_speed_hz;
42 } else {
43 ddr_clk = mem_speed_hz - ddrphy_clk;
44 }
Yann Gautier7b7e4bf2019-01-17 19:16:03 +010045 if (ddr_clk > (mem_speed_hz / 10)) {
46 ERROR("DDR expected freq %d kHz, current is %ld kHz\n",
47 mem_speed, ddrphy_clk / 1000U);
Yann Gautiercaf575b2018-07-24 17:18:19 +020048 return -1;
49 }
50 return 0;
51}
52
53/*******************************************************************************
54 * This function tests the DDR data bus wiring.
55 * This is inspired from the Data Bus Test algorithm written by Michael Barr
56 * in "Programming Embedded Systems in C and C++" book.
57 * resources.oreilly.com/examples/9781565923546/blob/master/Chapter6/
58 * File: memtest.c - This source code belongs to Public Domain.
59 * Returns 0 if success, and address value else.
60 ******************************************************************************/
61static uint32_t ddr_test_data_bus(void)
62{
63 uint32_t pattern;
64
65 for (pattern = 1U; pattern != 0U; pattern <<= 1) {
Yann Gautiera2e2a302019-02-14 11:13:39 +010066 mmio_write_32(STM32MP_DDR_BASE, pattern);
Yann Gautiercaf575b2018-07-24 17:18:19 +020067
Yann Gautiera2e2a302019-02-14 11:13:39 +010068 if (mmio_read_32(STM32MP_DDR_BASE) != pattern) {
69 return (uint32_t)STM32MP_DDR_BASE;
Yann Gautiercaf575b2018-07-24 17:18:19 +020070 }
71 }
72
73 return 0;
74}
75
76/*******************************************************************************
77 * This function tests the DDR address bus wiring.
78 * This is inspired from the Data Bus Test algorithm written by Michael Barr
79 * in "Programming Embedded Systems in C and C++" book.
80 * resources.oreilly.com/examples/9781565923546/blob/master/Chapter6/
81 * File: memtest.c - This source code belongs to Public Domain.
82 * Returns 0 if success, and address value else.
83 ******************************************************************************/
84static uint32_t ddr_test_addr_bus(void)
85{
86 uint64_t addressmask = (ddr_priv_data.info.size - 1U);
87 uint64_t offset;
88 uint64_t testoffset = 0;
89
90 /* Write the default pattern at each of the power-of-two offsets. */
91 for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
92 offset <<= 1) {
Yann Gautiera2e2a302019-02-14 11:13:39 +010093 mmio_write_32(STM32MP_DDR_BASE + (uint32_t)offset,
Yann Gautiercaf575b2018-07-24 17:18:19 +020094 DDR_PATTERN);
95 }
96
97 /* Check for address bits stuck high. */
Yann Gautiera2e2a302019-02-14 11:13:39 +010098 mmio_write_32(STM32MP_DDR_BASE + (uint32_t)testoffset,
Yann Gautiercaf575b2018-07-24 17:18:19 +020099 DDR_ANTIPATTERN);
100
101 for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
102 offset <<= 1) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100103 if (mmio_read_32(STM32MP_DDR_BASE + (uint32_t)offset) !=
Yann Gautiercaf575b2018-07-24 17:18:19 +0200104 DDR_PATTERN) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100105 return (uint32_t)(STM32MP_DDR_BASE + offset);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200106 }
107 }
108
Yann Gautiera2e2a302019-02-14 11:13:39 +0100109 mmio_write_32(STM32MP_DDR_BASE + (uint32_t)testoffset, DDR_PATTERN);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200110
111 /* Check for address bits stuck low or shorted. */
112 for (testoffset = sizeof(uint32_t); (testoffset & addressmask) != 0U;
113 testoffset <<= 1) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100114 mmio_write_32(STM32MP_DDR_BASE + (uint32_t)testoffset,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200115 DDR_ANTIPATTERN);
116
Yann Gautiera2e2a302019-02-14 11:13:39 +0100117 if (mmio_read_32(STM32MP_DDR_BASE) != DDR_PATTERN) {
118 return STM32MP_DDR_BASE;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200119 }
120
121 for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
122 offset <<= 1) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100123 if ((mmio_read_32(STM32MP_DDR_BASE +
Yann Gautiercaf575b2018-07-24 17:18:19 +0200124 (uint32_t)offset) != DDR_PATTERN) &&
125 (offset != testoffset)) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100126 return (uint32_t)(STM32MP_DDR_BASE + offset);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200127 }
128 }
129
Yann Gautiera2e2a302019-02-14 11:13:39 +0100130 mmio_write_32(STM32MP_DDR_BASE + (uint32_t)testoffset,
Yann Gautiercaf575b2018-07-24 17:18:19 +0200131 DDR_PATTERN);
132 }
133
134 return 0;
135}
136
137/*******************************************************************************
138 * This function checks the DDR size. It has to be run with Data Cache off.
139 * This test is run before data have been put in DDR, and is only done for
140 * cold boot. The DDR data can then be overwritten, and it is not useful to
141 * restore its content.
142 * Returns DDR computed size.
143 ******************************************************************************/
144static uint32_t ddr_check_size(void)
145{
146 uint32_t offset = sizeof(uint32_t);
147
Yann Gautiera2e2a302019-02-14 11:13:39 +0100148 mmio_write_32(STM32MP_DDR_BASE, DDR_PATTERN);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200149
Yann Gautiera2e2a302019-02-14 11:13:39 +0100150 while (offset < STM32MP_DDR_MAX_SIZE) {
151 mmio_write_32(STM32MP_DDR_BASE + offset, DDR_ANTIPATTERN);
Yann Gautiercaf575b2018-07-24 17:18:19 +0200152 dsb();
153
Yann Gautiera2e2a302019-02-14 11:13:39 +0100154 if (mmio_read_32(STM32MP_DDR_BASE) != DDR_PATTERN) {
Yann Gautiercaf575b2018-07-24 17:18:19 +0200155 break;
156 }
157
158 offset <<= 1;
159 }
160
161 INFO("Memory size = 0x%x (%d MB)\n", offset, offset / (1024U * 1024U));
162
163 return offset;
164}
165
166static int stm32mp1_ddr_setup(void)
167{
168 struct ddr_info *priv = &ddr_priv_data;
169 int ret;
170 struct stm32mp1_ddr_config config;
171 int node, len;
Yann Gautiere4a3c352019-02-14 10:53:33 +0100172 uint32_t uret, idx;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200173 void *fdt;
174
175#define PARAM(x, y) \
176 { \
177 .name = x, \
178 .offset = offsetof(struct stm32mp1_ddr_config, y), \
179 .size = sizeof(config.y) / sizeof(uint32_t) \
180 }
181
182#define CTL_PARAM(x) PARAM("st,ctl-"#x, c_##x)
183#define PHY_PARAM(x) PARAM("st,phy-"#x, p_##x)
184
185 const struct {
186 const char *name; /* Name in DT */
187 const uint32_t offset; /* Offset in config struct */
188 const uint32_t size; /* Size of parameters */
189 } param[] = {
190 CTL_PARAM(reg),
191 CTL_PARAM(timing),
192 CTL_PARAM(map),
193 CTL_PARAM(perf),
194 PHY_PARAM(reg),
195 PHY_PARAM(timing),
196 PHY_PARAM(cal)
197 };
198
199 if (fdt_get_address(&fdt) == 0) {
200 return -ENOENT;
201 }
202
203 node = fdt_node_offset_by_compatible(fdt, -1, DT_DDR_COMPAT);
204 if (node < 0) {
205 ERROR("%s: Cannot read DDR node in DT\n", __func__);
206 return -EINVAL;
207 }
208
Andre Przywara2d5690c2020-03-26 11:50:33 +0000209 ret = fdt_read_uint32(fdt, node, "st,mem-speed", &config.info.speed);
210 if (ret < 0) {
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100211 VERBOSE("%s: no st,mem-speed\n", __func__);
212 return -EINVAL;
213 }
Andre Przywara2d5690c2020-03-26 11:50:33 +0000214 ret = fdt_read_uint32(fdt, node, "st,mem-size", &config.info.size);
215 if (ret < 0) {
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100216 VERBOSE("%s: no st,mem-size\n", __func__);
217 return -EINVAL;
218 }
Yann Gautiercaf575b2018-07-24 17:18:19 +0200219 config.info.name = fdt_getprop(fdt, node, "st,mem-name", &len);
220 if (config.info.name == NULL) {
221 VERBOSE("%s: no st,mem-name\n", __func__);
222 return -EINVAL;
223 }
224 INFO("RAM: %s\n", config.info.name);
225
226 for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
Andre Przywaracc99f3f2020-03-26 12:51:21 +0000227 ret = fdt_read_uint32_array(fdt, node, param[idx].name,
228 param[idx].size,
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100229 (void *)((uintptr_t)&config +
Andre Przywaracc99f3f2020-03-26 12:51:21 +0000230 param[idx].offset));
Yann Gautiercaf575b2018-07-24 17:18:19 +0200231
232 VERBOSE("%s: %s[0x%x] = %d\n", __func__,
233 param[idx].name, param[idx].size, ret);
234 if (ret != 0) {
235 ERROR("%s: Cannot read %s\n",
236 __func__, param[idx].name);
237 return -EINVAL;
238 }
239 }
240
Yann Gautiercaf575b2018-07-24 17:18:19 +0200241 /* Disable axidcg clock gating during init */
242 mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN);
243
244 stm32mp1_ddr_init(priv, &config);
245
246 /* Enable axidcg clock gating */
247 mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN);
248
249 priv->info.size = config.info.size;
250
251 VERBOSE("%s : ram size(%x, %x)\n", __func__,
252 (uint32_t)priv->info.base, (uint32_t)priv->info.size);
253
Yann Gautiera55169b2020-01-10 18:18:59 +0100254 if (stm32mp_map_ddr_non_cacheable() != 0) {
255 panic();
256 }
Yann Gautiercaf575b2018-07-24 17:18:19 +0200257
258 uret = ddr_test_data_bus();
259 if (uret != 0U) {
260 ERROR("DDR data bus test: can't access memory @ 0x%x\n",
261 uret);
262 panic();
263 }
264
265 uret = ddr_test_addr_bus();
266 if (uret != 0U) {
267 ERROR("DDR addr bus test: can't access memory @ 0x%x\n",
268 uret);
269 panic();
270 }
271
272 uret = ddr_check_size();
273 if (uret < config.info.size) {
274 ERROR("DDR size: 0x%x does not match DT config: 0x%x\n",
275 uret, config.info.size);
276 panic();
277 }
278
Yann Gautiera55169b2020-01-10 18:18:59 +0100279 if (stm32mp_unmap_ddr() != 0) {
280 panic();
281 }
Yann Gautiercaf575b2018-07-24 17:18:19 +0200282
283 return 0;
284}
285
286int stm32mp1_ddr_probe(void)
287{
288 struct ddr_info *priv = &ddr_priv_data;
289
290 VERBOSE("STM32MP DDR probe\n");
291
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100292 priv->ctl = (struct stm32mp1_ddrctl *)stm32mp_ddrctrl_base();
293 priv->phy = (struct stm32mp1_ddrphy *)stm32mp_ddrphyc_base();
294 priv->pwr = stm32mp_pwr_base();
295 priv->rcc = stm32mp_rcc_base();
Yann Gautiercaf575b2018-07-24 17:18:19 +0200296
Yann Gautiera2e2a302019-02-14 11:13:39 +0100297 priv->info.base = STM32MP_DDR_BASE;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200298 priv->info.size = 0;
299
300 return stm32mp1_ddr_setup();
301}