blob: 61bc002d979dc0feedb63ffbf3f63f85f77de189 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay939d5362018-03-12 10:46:11 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay939d5362018-03-12 10:46:11 +01004 */
5
Patrick Delaunay753f5362020-11-06 19:01:36 +01006#define LOG_CATEGORY UCLASS_RAM
7
Patrick Delaunay939d5362018-03-12 10:46:11 +01008#include <common.h>
9#include <clk.h>
10#include <dm.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070011#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Patrick Delaunay939d5362018-03-12 10:46:11 +010013#include <ram.h>
14#include <regmap.h>
15#include <syscon.h>
16#include <asm/io.h>
Patrick Delaunay753f5362020-11-06 19:01:36 +010017#include <dm/device_compat.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060018#include <linux/printk.h>
Patrick Delaunay939d5362018-03-12 10:46:11 +010019#include "stm32mp1_ddr.h"
Patrick Delaunaydf7fe212021-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 Delaunay939d5362018-03-12 10:46:11 +010026
Patrick Delaunay939d5362018-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 Delaunay29e1a942019-04-10 14:09:23 +020035int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed)
Patrick Delaunay939d5362018-03-12 10:46:11 +010036{
37 unsigned long ddrphy_clk;
38 unsigned long ddr_clk;
39 struct clk clk;
40 int ret;
Patrick Delaunay6abbd352019-06-21 15:26:51 +020041 unsigned int idx;
Patrick Delaunay939d5362018-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 Delaunay753f5362020-11-06 19:01:36 +010050 log_err("error for %s : %d\n", clkname[idx], ret);
Patrick Delaunay939d5362018-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 Delaunay753f5362020-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 Delaunay939d5362018-03-12 10:46:11 +010060 /* max 10% frequency delta */
Patrick Delaunay29e1a942019-04-10 14:09:23 +020061 ddr_clk = abs(ddrphy_clk - mem_speed * 1000);
62 if (ddr_clk > (mem_speed * 100)) {
Patrick Delaunay753f5362020-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 Delaunay939d5362018-03-12 10:46:11 +010065 return -EINVAL;
66 }
67
68 return 0;
69}
70
Marek Vasut697887a2020-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 Delaunay915bd1a2021-11-24 10:52:19 +010092static int stm32mp1_ddr_setup(struct udevice *dev)
Patrick Delaunay939d5362018-03-12 10:46:11 +010093{
94 struct ddr_info *priv = dev_get_priv(dev);
Patrick Delaunay6abbd352019-06-21 15:26:51 +020095 int ret;
96 unsigned int idx;
Patrick Delaunay939d5362018-03-12 10:46:11 +010097 struct clk axidcg;
98 struct stm32mp1_ddr_config config;
Marek Vasut697887a2020-04-22 13:18:12 +020099 ofnode node = stm32mp1_ddr_get_ofnode(dev);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100100
Patrick Delaunaya68e2d62020-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 Delaunaya68e2d62020-03-06 11:14:11 +0100105 }
Patrick Delaunay939d5362018-03-12 10:46:11 +0100106
Patrick Delaunaya68e2d62020-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 Delaunay939d5362018-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 Delaunay9e2dd662021-11-15 15:32:29 +0100120 PHY_PARAM(timing)
Patrick Delaunay939d5362018-03-12 10:46:11 +0100121 };
122
Marek Vasut697887a2020-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 Delaunay939d5362018-03-12 10:46:11 +0100126 if (!config.info.name) {
Patrick Delaunay753f5362020-11-06 19:01:36 +0100127 dev_dbg(dev, "no st,mem-name\n");
Patrick Delaunay939d5362018-03-12 10:46:11 +0100128 return -EINVAL;
129 }
130 printf("RAM: %s\n", config.info.name);
131
132 for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
Marek Vasut697887a2020-04-22 13:18:12 +0200133 ret = ofnode_read_u32_array(node, param[idx].name,
Patrick Delaunay939d5362018-03-12 10:46:11 +0100134 (void *)((u32)&config +
135 param[idx].offset),
136 param[idx].size);
Patrick Delaunay753f5362020-11-06 19:01:36 +0100137 dev_dbg(dev, "%s: %s[0x%x] = %d\n", __func__,
138 param[idx].name, param[idx].size, ret);
Patrick Delaunay9e2dd662021-11-15 15:32:29 +0100139 if (ret) {
Patrick Delaunay753f5362020-11-06 19:01:36 +0100140 dev_err(dev, "Cannot read %s, error=%d\n",
141 param[idx].name, ret);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100142 return -EINVAL;
143 }
144 }
145
146 ret = clk_get_by_name(dev, "axidcg", &axidcg);
147 if (ret) {
Patrick Delaunay753f5362020-11-06 19:01:36 +0100148 dev_dbg(dev, "%s: Cannot found axidcg\n", __func__);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100149 return -EINVAL;
150 }
151 clk_disable(&axidcg); /* disable clock gating during init */
152
153 stm32mp1_ddr_init(priv, &config);
154
155 clk_enable(&axidcg); /* enable clock gating */
156
157 /* check size */
Patrick Delaunay753f5362020-11-06 19:01:36 +0100158 dev_dbg(dev, "get_ram_size(%x, %x)\n",
159 (u32)priv->info.base, (u32)STM32_DDR_SIZE);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100160
161 priv->info.size = get_ram_size((long *)priv->info.base,
162 STM32_DDR_SIZE);
163
Patrick Delaunay753f5362020-11-06 19:01:36 +0100164 dev_dbg(dev, "info.size: %x\n", (u32)priv->info.size);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100165
166 /* check memory access for all memory */
167 if (config.info.size != priv->info.size) {
168 printf("DDR invalid size : 0x%x, expected 0x%x\n",
169 priv->info.size, config.info.size);
170 return -EINVAL;
171 }
172 return 0;
173}
174
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100175static u8 get_data_bus_width(struct stm32mp1_ddrctl *ctl)
176{
177 u32 reg = readl(&ctl->mstr) & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK;
178 u8 data_bus_width = reg >> DDRCTRL_MSTR_DATA_BUS_WIDTH_SHIFT;
179
180 return data_bus_width;
181}
182
183static u8 get_nb_bank(struct stm32mp1_ddrctl *ctl)
184{
185 /* Count bank address bits */
186 u8 bits = 0;
187 u32 reg, val;
188
189 reg = readl(&ctl->addrmap1);
190 /* addrmap1.addrmap_bank_b1 */
191 val = (reg & GENMASK(5, 0)) >> 0;
192 if (val <= 31)
193 bits++;
194 /* addrmap1.addrmap_bank_b2 */
195 val = (reg & GENMASK(13, 8)) >> 8;
196 if (val <= 31)
197 bits++;
198 /* addrmap1.addrmap_bank_b3 */
199 val = (reg & GENMASK(21, 16)) >> 16;
200 if (val <= 31)
201 bits++;
202
203 return bits;
204}
205
206static u8 get_nb_col(struct stm32mp1_ddrctl *ctl, u8 data_bus_width)
207{
208 u8 bits;
209 u32 reg, val;
210
211 /* Count column address bits, start at 2 for b0 and b1 (fixed) */
212 bits = 2;
213
214 reg = readl(&ctl->addrmap2);
215 /* addrmap2.addrmap_col_b2 */
216 val = (reg & GENMASK(3, 0)) >> 0;
217 if (val <= 7)
218 bits++;
219 /* addrmap2.addrmap_col_b3 */
220 val = (reg & GENMASK(11, 8)) >> 8;
221 if (val <= 7)
222 bits++;
223 /* addrmap2.addrmap_col_b4 */
224 val = (reg & GENMASK(19, 16)) >> 16;
225 if (val <= 7)
226 bits++;
227 /* addrmap2.addrmap_col_b5 */
228 val = (reg & GENMASK(27, 24)) >> 24;
229 if (val <= 7)
230 bits++;
231
232 reg = readl(&ctl->addrmap3);
233 /* addrmap3.addrmap_col_b6 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200234 val = (reg & GENMASK(4, 0)) >> 0;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100235 if (val <= 7)
236 bits++;
237 /* addrmap3.addrmap_col_b7 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200238 val = (reg & GENMASK(12, 8)) >> 8;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100239 if (val <= 7)
240 bits++;
241 /* addrmap3.addrmap_col_b8 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200242 val = (reg & GENMASK(20, 16)) >> 16;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100243 if (val <= 7)
244 bits++;
245 /* addrmap3.addrmap_col_b9 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200246 val = (reg & GENMASK(28, 24)) >> 24;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100247 if (val <= 7)
248 bits++;
249
250 reg = readl(&ctl->addrmap4);
251 /* addrmap4.addrmap_col_b10 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200252 val = (reg & GENMASK(4, 0)) >> 0;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100253 if (val <= 7)
254 bits++;
255 /* addrmap4.addrmap_col_b11 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200256 val = (reg & GENMASK(12, 8)) >> 8;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100257 if (val <= 7)
258 bits++;
259
260 /*
261 * column bits shift up:
262 * 1 when half the data bus is used (data_bus_width = 1)
263 * 2 when a quarter the data bus is used (data_bus_width = 2)
264 * nothing to do for full data bus (data_bus_width = 0)
265 */
266 bits += data_bus_width;
267
268 return bits;
269}
270
271static u8 get_nb_row(struct stm32mp1_ddrctl *ctl)
272{
273 /* Count row address bits */
274 u8 bits = 0;
275 u32 reg, val;
276
277 reg = readl(&ctl->addrmap5);
278 /* addrmap5.addrmap_row_b0 */
279 val = (reg & GENMASK(3, 0)) >> 0;
280 if (val <= 11)
281 bits++;
282 /* addrmap5.addrmap_row_b1 */
283 val = (reg & GENMASK(11, 8)) >> 8;
284 if (val <= 11)
285 bits++;
286 /* addrmap5.addrmap_row_b2_10 */
287 val = (reg & GENMASK(19, 16)) >> 16;
288 if (val <= 11)
289 bits += 9;
290 else
291 printf("warning: addrmap5.addrmap_row_b2_10 not supported\n");
292 /* addrmap5.addrmap_row_b11 */
293 val = (reg & GENMASK(27, 24)) >> 24;
294 if (val <= 11)
295 bits++;
296
297 reg = readl(&ctl->addrmap6);
298 /* addrmap6.addrmap_row_b12 */
299 val = (reg & GENMASK(3, 0)) >> 0;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200300 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100301 bits++;
302 /* addrmap6.addrmap_row_b13 */
303 val = (reg & GENMASK(11, 8)) >> 8;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200304 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100305 bits++;
306 /* addrmap6.addrmap_row_b14 */
307 val = (reg & GENMASK(19, 16)) >> 16;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200308 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100309 bits++;
310 /* addrmap6.addrmap_row_b15 */
311 val = (reg & GENMASK(27, 24)) >> 24;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200312 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100313 bits++;
314
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200315 if (reg & BIT(31))
316 printf("warning: LPDDR3_6GB_12GB is not supported\n");
317
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100318 return bits;
319}
320
321/*
322 * stm32mp1_ddr_size
323 *
324 * Get the current DRAM size from the DDR CTL registers
325 *
326 * @return: DRAM size
327 */
328u32 stm32mp1_ddr_size(struct udevice *dev)
329{
330 u8 nb_bit;
331 u32 ddr_size;
332 u8 data_bus_width;
333 struct ddr_info *priv = dev_get_priv(dev);
334 struct stm32mp1_ddrctl *ctl = priv->ctl;
335 struct stm32mp1_ddr_cfg *cfg = (struct stm32mp1_ddr_cfg *)dev_get_driver_data(dev);
336 const u8 nb_bytes = cfg->nb_bytes;
337
338 data_bus_width = get_data_bus_width(ctl);
339 nb_bit = get_nb_bank(ctl) + get_nb_col(ctl, data_bus_width) +
340 get_nb_row(ctl);
341 if (nb_bit > 32) {
342 nb_bit = 32;
343 debug("invalid DDR configuration: %d bits\n", nb_bit);
344 }
345
346 ddr_size = (nb_bytes >> data_bus_width) << nb_bit;
347 if (ddr_size > STM32_DDR_SIZE) {
348 ddr_size = STM32_DDR_SIZE;
349 debug("invalid DDR configuration: size = %x\n", ddr_size);
350 }
351
352 return ddr_size;
353}
354
Patrick Delaunay939d5362018-03-12 10:46:11 +0100355static int stm32mp1_ddr_probe(struct udevice *dev)
356{
357 struct ddr_info *priv = dev_get_priv(dev);
358 struct regmap *map;
359 int ret;
360
Patrick Delaunay939d5362018-03-12 10:46:11 +0100361 priv->dev = dev;
362
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900363 ret = regmap_init_mem(dev_ofnode(dev), &map);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100364 if (ret)
Patrick Delaunay753f5362020-11-06 19:01:36 +0100365 return log_ret(ret);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100366
367 priv->ctl = regmap_get_range(map, 0);
368 priv->phy = regmap_get_range(map, 1);
369
370 priv->rcc = STM32_RCC_BASE;
371
372 priv->info.base = STM32_DDR_BASE;
373
Patrick Delaunay72a57622021-10-11 09:52:50 +0200374 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
375 priv->info.size = 0;
376 ret = stm32mp1_ddr_setup(dev);
Patrick Delaunay753f5362020-11-06 19:01:36 +0100377
Patrick Delaunay72a57622021-10-11 09:52:50 +0200378 return log_ret(ret);
379 }
380
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100381 priv->info.size = stm32mp1_ddr_size(dev);
382
Patrick Delaunay939d5362018-03-12 10:46:11 +0100383 return 0;
Patrick Delaunay939d5362018-03-12 10:46:11 +0100384}
385
386static int stm32mp1_ddr_get_info(struct udevice *dev, struct ram_info *info)
387{
388 struct ddr_info *priv = dev_get_priv(dev);
389
390 *info = priv->info;
391
392 return 0;
393}
394
395static struct ram_ops stm32mp1_ddr_ops = {
396 .get_info = stm32mp1_ddr_get_info,
397};
398
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200399static const struct stm32mp1_ddr_cfg stm32mp13x_ddr_cfg = {
400 .nb_bytes = 2,
401};
402
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100403static const struct stm32mp1_ddr_cfg stm32mp15x_ddr_cfg = {
404 .nb_bytes = 4,
405};
406
Patrick Delaunay939d5362018-03-12 10:46:11 +0100407static const struct udevice_id stm32mp1_ddr_ids[] = {
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100408 { .compatible = "st,stm32mp1-ddr", .data = (ulong)&stm32mp15x_ddr_cfg},
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200409 { .compatible = "st,stm32mp13-ddr", .data = (ulong)&stm32mp13x_ddr_cfg},
Patrick Delaunay939d5362018-03-12 10:46:11 +0100410 { }
411};
412
413U_BOOT_DRIVER(ddr_stm32mp1) = {
414 .name = "stm32mp1_ddr",
415 .id = UCLASS_RAM,
416 .of_match = stm32mp1_ddr_ids,
417 .ops = &stm32mp1_ddr_ops,
418 .probe = stm32mp1_ddr_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700419 .priv_auto = sizeof(struct ddr_info),
Patrick Delaunay939d5362018-03-12 10:46:11 +0100420};