blob: a82b1db7592df7fbd39ebd9f6c639a305f1982f6 [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 }
Harald Seilerf3e4b8b2023-09-27 14:44:40 +0200130 if (CONFIG_IS_ENABLED(DISPLAY_PRINT))
131 printf("RAM: %s\n", config.info.name);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100132
133 for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
Marek Vasut697887a2020-04-22 13:18:12 +0200134 ret = ofnode_read_u32_array(node, param[idx].name,
Patrick Delaunay939d5362018-03-12 10:46:11 +0100135 (void *)((u32)&config +
136 param[idx].offset),
137 param[idx].size);
Patrick Delaunay753f5362020-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 Delaunay9e2dd662021-11-15 15:32:29 +0100140 if (ret) {
Patrick Delaunay753f5362020-11-06 19:01:36 +0100141 dev_err(dev, "Cannot read %s, error=%d\n",
142 param[idx].name, ret);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100143 return -EINVAL;
144 }
145 }
146
147 ret = clk_get_by_name(dev, "axidcg", &axidcg);
148 if (ret) {
Patrick Delaunay753f5362020-11-06 19:01:36 +0100149 dev_dbg(dev, "%s: Cannot found axidcg\n", __func__);
Patrick Delaunay939d5362018-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 Delaunay753f5362020-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 Delaunay939d5362018-03-12 10:46:11 +0100161
162 priv->info.size = get_ram_size((long *)priv->info.base,
163 STM32_DDR_SIZE);
164
Patrick Delaunay753f5362020-11-06 19:01:36 +0100165 dev_dbg(dev, "info.size: %x\n", (u32)priv->info.size);
Patrick Delaunay939d5362018-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 Delaunaydf7fe212021-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 Delaunayb5bb08d2022-05-20 18:24:50 +0200235 val = (reg & GENMASK(4, 0)) >> 0;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100236 if (val <= 7)
237 bits++;
238 /* addrmap3.addrmap_col_b7 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200239 val = (reg & GENMASK(12, 8)) >> 8;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100240 if (val <= 7)
241 bits++;
242 /* addrmap3.addrmap_col_b8 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200243 val = (reg & GENMASK(20, 16)) >> 16;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100244 if (val <= 7)
245 bits++;
246 /* addrmap3.addrmap_col_b9 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200247 val = (reg & GENMASK(28, 24)) >> 24;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100248 if (val <= 7)
249 bits++;
250
251 reg = readl(&ctl->addrmap4);
252 /* addrmap4.addrmap_col_b10 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200253 val = (reg & GENMASK(4, 0)) >> 0;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100254 if (val <= 7)
255 bits++;
256 /* addrmap4.addrmap_col_b11 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200257 val = (reg & GENMASK(12, 8)) >> 8;
Patrick Delaunaydf7fe212021-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 Delaunayb5bb08d2022-05-20 18:24:50 +0200301 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100302 bits++;
303 /* addrmap6.addrmap_row_b13 */
304 val = (reg & GENMASK(11, 8)) >> 8;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200305 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100306 bits++;
307 /* addrmap6.addrmap_row_b14 */
308 val = (reg & GENMASK(19, 16)) >> 16;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200309 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100310 bits++;
311 /* addrmap6.addrmap_row_b15 */
312 val = (reg & GENMASK(27, 24)) >> 24;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200313 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100314 bits++;
315
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200316 if (reg & BIT(31))
317 printf("warning: LPDDR3_6GB_12GB is not supported\n");
318
Patrick Delaunaydf7fe212021-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 Delaunay939d5362018-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 Delaunay939d5362018-03-12 10:46:11 +0100362 priv->dev = dev;
363
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900364 ret = regmap_init_mem(dev_ofnode(dev), &map);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100365 if (ret)
Patrick Delaunay753f5362020-11-06 19:01:36 +0100366 return log_ret(ret);
Patrick Delaunay939d5362018-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 Delaunay72a57622021-10-11 09:52:50 +0200375 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
376 priv->info.size = 0;
377 ret = stm32mp1_ddr_setup(dev);
Patrick Delaunay753f5362020-11-06 19:01:36 +0100378
Patrick Delaunay72a57622021-10-11 09:52:50 +0200379 return log_ret(ret);
380 }
381
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100382 priv->info.size = stm32mp1_ddr_size(dev);
383
Patrick Delaunay939d5362018-03-12 10:46:11 +0100384 return 0;
Patrick Delaunay939d5362018-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 Delaunayb5bb08d2022-05-20 18:24:50 +0200400static const struct stm32mp1_ddr_cfg stm32mp13x_ddr_cfg = {
401 .nb_bytes = 2,
402};
403
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100404static const struct stm32mp1_ddr_cfg stm32mp15x_ddr_cfg = {
405 .nb_bytes = 4,
406};
407
Patrick Delaunay939d5362018-03-12 10:46:11 +0100408static const struct udevice_id stm32mp1_ddr_ids[] = {
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100409 { .compatible = "st,stm32mp1-ddr", .data = (ulong)&stm32mp15x_ddr_cfg},
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200410 { .compatible = "st,stm32mp13-ddr", .data = (ulong)&stm32mp13x_ddr_cfg},
Patrick Delaunay939d5362018-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 Glass8a2b47f2020-12-03 16:55:17 -0700420 .priv_auto = sizeof(struct ddr_info),
Patrick Delaunay939d5362018-03-12 10:46:11 +0100421};