blob: a82b1db7592df7fbd39ebd9f6c639a305f1982f6 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01004 */
5
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +01006#define LOG_CATEGORY UCLASS_RAM
7
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01008#include <common.h>
9#include <clk.h>
10#include <dm.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070011#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010013#include <ram.h>
14#include <regmap.h>
15#include <syscon.h>
16#include <asm/io.h>
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +010017#include <dm/device_compat.h>
Simon Glass1e94b462023-09-14 18:21:46 -060018#include <linux/printk.h>
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010019#include "stm32mp1_ddr.h"
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +010020#include "stm32mp1_ddr_regs.h"
21
22/* DDR subsystem configuration */
23struct stm32mp1_ddr_cfg {
24 u8 nb_bytes; /* MEMC_DRAM_DATA_WIDTH */
25};
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010026
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010027static const char *const clkname[] = {
28 "ddrc1",
29 "ddrc2",
30 "ddrcapb",
31 "ddrphycapb",
32 "ddrphyc" /* LAST clock => used for get_rate() */
33};
34
Patrick Delaunayc60fed12019-04-10 14:09:23 +020035int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed)
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010036{
37 unsigned long ddrphy_clk;
38 unsigned long ddr_clk;
39 struct clk clk;
40 int ret;
Patrick Delaunay58844852019-06-21 15:26:51 +020041 unsigned int idx;
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010042
43 for (idx = 0; idx < ARRAY_SIZE(clkname); idx++) {
44 ret = clk_get_by_name(priv->dev, clkname[idx], &clk);
45
46 if (!ret)
47 ret = clk_enable(&clk);
48
49 if (ret) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +010050 log_err("error for %s : %d\n", clkname[idx], ret);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010051 return ret;
52 }
53 }
54
55 priv->clk = clk;
56 ddrphy_clk = clk_get_rate(&priv->clk);
57
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +010058 log_debug("DDR: mem_speed (%d kHz), RCC %d kHz\n",
59 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010060 /* max 10% frequency delta */
Patrick Delaunayc60fed12019-04-10 14:09:23 +020061 ddr_clk = abs(ddrphy_clk - mem_speed * 1000);
62 if (ddr_clk > (mem_speed * 100)) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +010063 log_err("DDR expected freq %d kHz, current is %d kHz\n",
64 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010065 return -EINVAL;
66 }
67
68 return 0;
69}
70
Marek Vasut0c27c162020-04-22 13:18:12 +020071__weak int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
72 const char *name)
73{
74 return 0; /* Always match */
75}
76
77static ofnode stm32mp1_ddr_get_ofnode(struct udevice *dev)
78{
79 const char *name;
80 ofnode node;
81
82 dev_for_each_subnode(node, dev) {
83 name = ofnode_get_property(node, "compatible", NULL);
84
85 if (!board_stm32mp1_ddr_config_name_match(dev, name))
86 return node;
87 }
88
89 return dev_ofnode(dev);
90}
91
Patrick Delaunayb2ac9642021-11-24 10:52:19 +010092static int stm32mp1_ddr_setup(struct udevice *dev)
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010093{
94 struct ddr_info *priv = dev_get_priv(dev);
Patrick Delaunay58844852019-06-21 15:26:51 +020095 int ret;
96 unsigned int idx;
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010097 struct clk axidcg;
98 struct stm32mp1_ddr_config config;
Marek Vasut0c27c162020-04-22 13:18:12 +020099 ofnode node = stm32mp1_ddr_get_ofnode(dev);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100100
Patrick Delaunay9368bdf2020-03-06 11:14:11 +0100101#define PARAM(x, y, z) \
102 { .name = x, \
103 .offset = offsetof(struct stm32mp1_ddr_config, y), \
104 .size = sizeof(config.y) / sizeof(u32), \
Patrick Delaunay9368bdf2020-03-06 11:14:11 +0100105 }
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100106
Patrick Delaunay9368bdf2020-03-06 11:14:11 +0100107#define CTL_PARAM(x) PARAM("st,ctl-"#x, c_##x, NULL)
108#define PHY_PARAM(x) PARAM("st,phy-"#x, p_##x, NULL)
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100109
110 const struct {
111 const char *name; /* name in DT */
112 const u32 offset; /* offset in config struct */
113 const u32 size; /* size of parameters */
114 } param[] = {
115 CTL_PARAM(reg),
116 CTL_PARAM(timing),
117 CTL_PARAM(map),
118 CTL_PARAM(perf),
119 PHY_PARAM(reg),
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100120 PHY_PARAM(timing)
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100121 };
122
Marek Vasut0c27c162020-04-22 13:18:12 +0200123 config.info.speed = ofnode_read_u32_default(node, "st,mem-speed", 0);
124 config.info.size = ofnode_read_u32_default(node, "st,mem-size", 0);
125 config.info.name = ofnode_read_string(node, "st,mem-name");
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100126 if (!config.info.name) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100127 dev_dbg(dev, "no st,mem-name\n");
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100128 return -EINVAL;
129 }
Harald Seiler719a8752023-09-27 14:44:40 +0200130 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
131 printf("RAM: %s\n", config.info.name);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100132
133 for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
Marek Vasut0c27c162020-04-22 13:18:12 +0200134 ret = ofnode_read_u32_array(node, param[idx].name,
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100135 (void *)((u32)&config +
136 param[idx].offset),
137 param[idx].size);
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100138 dev_dbg(dev, "%s: %s[0x%x] = %d\n", __func__,
139 param[idx].name, param[idx].size, ret);
Patrick Delaunay9819fe32021-11-15 15:32:29 +0100140 if (ret) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100141 dev_err(dev, "Cannot read %s, error=%d\n",
142 param[idx].name, ret);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100143 return -EINVAL;
144 }
145 }
146
147 ret = clk_get_by_name(dev, "axidcg", &axidcg);
148 if (ret) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100149 dev_dbg(dev, "%s: Cannot found axidcg\n", __func__);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100150 return -EINVAL;
151 }
152 clk_disable(&axidcg); /* disable clock gating during init */
153
154 stm32mp1_ddr_init(priv, &config);
155
156 clk_enable(&axidcg); /* enable clock gating */
157
158 /* check size */
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100159 dev_dbg(dev, "get_ram_size(%x, %x)\n",
160 (u32)priv->info.base, (u32)STM32_DDR_SIZE);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100161
162 priv->info.size = get_ram_size((long *)priv->info.base,
163 STM32_DDR_SIZE);
164
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100165 dev_dbg(dev, "info.size: %x\n", (u32)priv->info.size);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100166
167 /* check memory access for all memory */
168 if (config.info.size != priv->info.size) {
169 printf("DDR invalid size : 0x%x, expected 0x%x\n",
170 priv->info.size, config.info.size);
171 return -EINVAL;
172 }
173 return 0;
174}
175
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100176static u8 get_data_bus_width(struct stm32mp1_ddrctl *ctl)
177{
178 u32 reg = readl(&ctl->mstr) & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK;
179 u8 data_bus_width = reg >> DDRCTRL_MSTR_DATA_BUS_WIDTH_SHIFT;
180
181 return data_bus_width;
182}
183
184static u8 get_nb_bank(struct stm32mp1_ddrctl *ctl)
185{
186 /* Count bank address bits */
187 u8 bits = 0;
188 u32 reg, val;
189
190 reg = readl(&ctl->addrmap1);
191 /* addrmap1.addrmap_bank_b1 */
192 val = (reg & GENMASK(5, 0)) >> 0;
193 if (val <= 31)
194 bits++;
195 /* addrmap1.addrmap_bank_b2 */
196 val = (reg & GENMASK(13, 8)) >> 8;
197 if (val <= 31)
198 bits++;
199 /* addrmap1.addrmap_bank_b3 */
200 val = (reg & GENMASK(21, 16)) >> 16;
201 if (val <= 31)
202 bits++;
203
204 return bits;
205}
206
207static u8 get_nb_col(struct stm32mp1_ddrctl *ctl, u8 data_bus_width)
208{
209 u8 bits;
210 u32 reg, val;
211
212 /* Count column address bits, start at 2 for b0 and b1 (fixed) */
213 bits = 2;
214
215 reg = readl(&ctl->addrmap2);
216 /* addrmap2.addrmap_col_b2 */
217 val = (reg & GENMASK(3, 0)) >> 0;
218 if (val <= 7)
219 bits++;
220 /* addrmap2.addrmap_col_b3 */
221 val = (reg & GENMASK(11, 8)) >> 8;
222 if (val <= 7)
223 bits++;
224 /* addrmap2.addrmap_col_b4 */
225 val = (reg & GENMASK(19, 16)) >> 16;
226 if (val <= 7)
227 bits++;
228 /* addrmap2.addrmap_col_b5 */
229 val = (reg & GENMASK(27, 24)) >> 24;
230 if (val <= 7)
231 bits++;
232
233 reg = readl(&ctl->addrmap3);
234 /* addrmap3.addrmap_col_b6 */
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200235 val = (reg & GENMASK(4, 0)) >> 0;
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100236 if (val <= 7)
237 bits++;
238 /* addrmap3.addrmap_col_b7 */
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200239 val = (reg & GENMASK(12, 8)) >> 8;
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100240 if (val <= 7)
241 bits++;
242 /* addrmap3.addrmap_col_b8 */
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200243 val = (reg & GENMASK(20, 16)) >> 16;
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100244 if (val <= 7)
245 bits++;
246 /* addrmap3.addrmap_col_b9 */
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200247 val = (reg & GENMASK(28, 24)) >> 24;
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100248 if (val <= 7)
249 bits++;
250
251 reg = readl(&ctl->addrmap4);
252 /* addrmap4.addrmap_col_b10 */
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200253 val = (reg & GENMASK(4, 0)) >> 0;
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100254 if (val <= 7)
255 bits++;
256 /* addrmap4.addrmap_col_b11 */
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200257 val = (reg & GENMASK(12, 8)) >> 8;
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100258 if (val <= 7)
259 bits++;
260
261 /*
262 * column bits shift up:
263 * 1 when half the data bus is used (data_bus_width = 1)
264 * 2 when a quarter the data bus is used (data_bus_width = 2)
265 * nothing to do for full data bus (data_bus_width = 0)
266 */
267 bits += data_bus_width;
268
269 return bits;
270}
271
272static u8 get_nb_row(struct stm32mp1_ddrctl *ctl)
273{
274 /* Count row address bits */
275 u8 bits = 0;
276 u32 reg, val;
277
278 reg = readl(&ctl->addrmap5);
279 /* addrmap5.addrmap_row_b0 */
280 val = (reg & GENMASK(3, 0)) >> 0;
281 if (val <= 11)
282 bits++;
283 /* addrmap5.addrmap_row_b1 */
284 val = (reg & GENMASK(11, 8)) >> 8;
285 if (val <= 11)
286 bits++;
287 /* addrmap5.addrmap_row_b2_10 */
288 val = (reg & GENMASK(19, 16)) >> 16;
289 if (val <= 11)
290 bits += 9;
291 else
292 printf("warning: addrmap5.addrmap_row_b2_10 not supported\n");
293 /* addrmap5.addrmap_row_b11 */
294 val = (reg & GENMASK(27, 24)) >> 24;
295 if (val <= 11)
296 bits++;
297
298 reg = readl(&ctl->addrmap6);
299 /* addrmap6.addrmap_row_b12 */
300 val = (reg & GENMASK(3, 0)) >> 0;
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200301 if (val <= 11)
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100302 bits++;
303 /* addrmap6.addrmap_row_b13 */
304 val = (reg & GENMASK(11, 8)) >> 8;
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200305 if (val <= 11)
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100306 bits++;
307 /* addrmap6.addrmap_row_b14 */
308 val = (reg & GENMASK(19, 16)) >> 16;
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200309 if (val <= 11)
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100310 bits++;
311 /* addrmap6.addrmap_row_b15 */
312 val = (reg & GENMASK(27, 24)) >> 24;
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200313 if (val <= 11)
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100314 bits++;
315
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200316 if (reg & BIT(31))
317 printf("warning: LPDDR3_6GB_12GB is not supported\n");
318
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100319 return bits;
320}
321
322/*
323 * stm32mp1_ddr_size
324 *
325 * Get the current DRAM size from the DDR CTL registers
326 *
327 * @return: DRAM size
328 */
329u32 stm32mp1_ddr_size(struct udevice *dev)
330{
331 u8 nb_bit;
332 u32 ddr_size;
333 u8 data_bus_width;
334 struct ddr_info *priv = dev_get_priv(dev);
335 struct stm32mp1_ddrctl *ctl = priv->ctl;
336 struct stm32mp1_ddr_cfg *cfg = (struct stm32mp1_ddr_cfg *)dev_get_driver_data(dev);
337 const u8 nb_bytes = cfg->nb_bytes;
338
339 data_bus_width = get_data_bus_width(ctl);
340 nb_bit = get_nb_bank(ctl) + get_nb_col(ctl, data_bus_width) +
341 get_nb_row(ctl);
342 if (nb_bit > 32) {
343 nb_bit = 32;
344 debug("invalid DDR configuration: %d bits\n", nb_bit);
345 }
346
347 ddr_size = (nb_bytes >> data_bus_width) << nb_bit;
348 if (ddr_size > STM32_DDR_SIZE) {
349 ddr_size = STM32_DDR_SIZE;
350 debug("invalid DDR configuration: size = %x\n", ddr_size);
351 }
352
353 return ddr_size;
354}
355
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100356static int stm32mp1_ddr_probe(struct udevice *dev)
357{
358 struct ddr_info *priv = dev_get_priv(dev);
359 struct regmap *map;
360 int ret;
361
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100362 priv->dev = dev;
363
Masahiro Yamadad3581232018-04-19 12:14:03 +0900364 ret = regmap_init_mem(dev_ofnode(dev), &map);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100365 if (ret)
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100366 return log_ret(ret);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100367
368 priv->ctl = regmap_get_range(map, 0);
369 priv->phy = regmap_get_range(map, 1);
370
371 priv->rcc = STM32_RCC_BASE;
372
373 priv->info.base = STM32_DDR_BASE;
374
Patrick Delaunayf42045b2021-10-11 09:52:50 +0200375 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
376 priv->info.size = 0;
377 ret = stm32mp1_ddr_setup(dev);
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100378
Patrick Delaunayf42045b2021-10-11 09:52:50 +0200379 return log_ret(ret);
380 }
381
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100382 priv->info.size = stm32mp1_ddr_size(dev);
383
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100384 return 0;
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100385}
386
387static int stm32mp1_ddr_get_info(struct udevice *dev, struct ram_info *info)
388{
389 struct ddr_info *priv = dev_get_priv(dev);
390
391 *info = priv->info;
392
393 return 0;
394}
395
396static struct ram_ops stm32mp1_ddr_ops = {
397 .get_info = stm32mp1_ddr_get_info,
398};
399
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200400static const struct stm32mp1_ddr_cfg stm32mp13x_ddr_cfg = {
401 .nb_bytes = 2,
402};
403
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100404static const struct stm32mp1_ddr_cfg stm32mp15x_ddr_cfg = {
405 .nb_bytes = 4,
406};
407
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100408static const struct udevice_id stm32mp1_ddr_ids[] = {
Patrick Delaunayd72e7bb2021-11-24 10:52:18 +0100409 { .compatible = "st,stm32mp1-ddr", .data = (ulong)&stm32mp15x_ddr_cfg},
Patrick Delaunaya46dce22022-05-20 18:24:50 +0200410 { .compatible = "st,stm32mp13-ddr", .data = (ulong)&stm32mp13x_ddr_cfg},
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100411 { }
412};
413
414U_BOOT_DRIVER(ddr_stm32mp1) = {
415 .name = "stm32mp1_ddr",
416 .id = UCLASS_RAM,
417 .of_match = stm32mp1_ddr_ids,
418 .ops = &stm32mp1_ddr_ops,
419 .probe = stm32mp1_ddr_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700420 .priv_auto = sizeof(struct ddr_info),
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100421};