blob: a6c19af9722001f3156c53289b31cdf979481e4d [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>
Patrick Delaunay939d5362018-03-12 10:46:11 +010018#include "stm32mp1_ddr.h"
Patrick Delaunaydf7fe212021-11-24 10:52:18 +010019#include "stm32mp1_ddr_regs.h"
20
21/* DDR subsystem configuration */
22struct stm32mp1_ddr_cfg {
23 u8 nb_bytes; /* MEMC_DRAM_DATA_WIDTH */
24};
Patrick Delaunay939d5362018-03-12 10:46:11 +010025
Patrick Delaunay939d5362018-03-12 10:46:11 +010026static const char *const clkname[] = {
27 "ddrc1",
28 "ddrc2",
29 "ddrcapb",
30 "ddrphycapb",
31 "ddrphyc" /* LAST clock => used for get_rate() */
32};
33
Patrick Delaunay29e1a942019-04-10 14:09:23 +020034int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed)
Patrick Delaunay939d5362018-03-12 10:46:11 +010035{
36 unsigned long ddrphy_clk;
37 unsigned long ddr_clk;
38 struct clk clk;
39 int ret;
Patrick Delaunay6abbd352019-06-21 15:26:51 +020040 unsigned int idx;
Patrick Delaunay939d5362018-03-12 10:46:11 +010041
42 for (idx = 0; idx < ARRAY_SIZE(clkname); idx++) {
43 ret = clk_get_by_name(priv->dev, clkname[idx], &clk);
44
45 if (!ret)
46 ret = clk_enable(&clk);
47
48 if (ret) {
Patrick Delaunay753f5362020-11-06 19:01:36 +010049 log_err("error for %s : %d\n", clkname[idx], ret);
Patrick Delaunay939d5362018-03-12 10:46:11 +010050 return ret;
51 }
52 }
53
54 priv->clk = clk;
55 ddrphy_clk = clk_get_rate(&priv->clk);
56
Patrick Delaunay753f5362020-11-06 19:01:36 +010057 log_debug("DDR: mem_speed (%d kHz), RCC %d kHz\n",
58 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunay939d5362018-03-12 10:46:11 +010059 /* max 10% frequency delta */
Patrick Delaunay29e1a942019-04-10 14:09:23 +020060 ddr_clk = abs(ddrphy_clk - mem_speed * 1000);
61 if (ddr_clk > (mem_speed * 100)) {
Patrick Delaunay753f5362020-11-06 19:01:36 +010062 log_err("DDR expected freq %d kHz, current is %d kHz\n",
63 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunay939d5362018-03-12 10:46:11 +010064 return -EINVAL;
65 }
66
67 return 0;
68}
69
Marek Vasut697887a2020-04-22 13:18:12 +020070__weak int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
71 const char *name)
72{
73 return 0; /* Always match */
74}
75
76static ofnode stm32mp1_ddr_get_ofnode(struct udevice *dev)
77{
78 const char *name;
79 ofnode node;
80
81 dev_for_each_subnode(node, dev) {
82 name = ofnode_get_property(node, "compatible", NULL);
83
84 if (!board_stm32mp1_ddr_config_name_match(dev, name))
85 return node;
86 }
87
88 return dev_ofnode(dev);
89}
90
Patrick Delaunay915bd1a2021-11-24 10:52:19 +010091static int stm32mp1_ddr_setup(struct udevice *dev)
Patrick Delaunay939d5362018-03-12 10:46:11 +010092{
93 struct ddr_info *priv = dev_get_priv(dev);
Patrick Delaunay6abbd352019-06-21 15:26:51 +020094 int ret;
95 unsigned int idx;
Patrick Delaunay939d5362018-03-12 10:46:11 +010096 struct clk axidcg;
97 struct stm32mp1_ddr_config config;
Marek Vasut697887a2020-04-22 13:18:12 +020098 ofnode node = stm32mp1_ddr_get_ofnode(dev);
Patrick Delaunay939d5362018-03-12 10:46:11 +010099
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100100#define PARAM(x, y, z) \
101 { .name = x, \
102 .offset = offsetof(struct stm32mp1_ddr_config, y), \
103 .size = sizeof(config.y) / sizeof(u32), \
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100104 }
Patrick Delaunay939d5362018-03-12 10:46:11 +0100105
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100106#define CTL_PARAM(x) PARAM("st,ctl-"#x, c_##x, NULL)
107#define PHY_PARAM(x) PARAM("st,phy-"#x, p_##x, NULL)
Patrick Delaunay939d5362018-03-12 10:46:11 +0100108
109 const struct {
110 const char *name; /* name in DT */
111 const u32 offset; /* offset in config struct */
112 const u32 size; /* size of parameters */
113 } param[] = {
114 CTL_PARAM(reg),
115 CTL_PARAM(timing),
116 CTL_PARAM(map),
117 CTL_PARAM(perf),
118 PHY_PARAM(reg),
Patrick Delaunay9e2dd662021-11-15 15:32:29 +0100119 PHY_PARAM(timing)
Patrick Delaunay939d5362018-03-12 10:46:11 +0100120 };
121
Marek Vasut697887a2020-04-22 13:18:12 +0200122 config.info.speed = ofnode_read_u32_default(node, "st,mem-speed", 0);
123 config.info.size = ofnode_read_u32_default(node, "st,mem-size", 0);
124 config.info.name = ofnode_read_string(node, "st,mem-name");
Patrick Delaunay939d5362018-03-12 10:46:11 +0100125 if (!config.info.name) {
Patrick Delaunay753f5362020-11-06 19:01:36 +0100126 dev_dbg(dev, "no st,mem-name\n");
Patrick Delaunay939d5362018-03-12 10:46:11 +0100127 return -EINVAL;
128 }
129 printf("RAM: %s\n", config.info.name);
130
131 for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
Marek Vasut697887a2020-04-22 13:18:12 +0200132 ret = ofnode_read_u32_array(node, param[idx].name,
Patrick Delaunay939d5362018-03-12 10:46:11 +0100133 (void *)((u32)&config +
134 param[idx].offset),
135 param[idx].size);
Patrick Delaunay753f5362020-11-06 19:01:36 +0100136 dev_dbg(dev, "%s: %s[0x%x] = %d\n", __func__,
137 param[idx].name, param[idx].size, ret);
Patrick Delaunay9e2dd662021-11-15 15:32:29 +0100138 if (ret) {
Patrick Delaunay753f5362020-11-06 19:01:36 +0100139 dev_err(dev, "Cannot read %s, error=%d\n",
140 param[idx].name, ret);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100141 return -EINVAL;
142 }
143 }
144
145 ret = clk_get_by_name(dev, "axidcg", &axidcg);
146 if (ret) {
Patrick Delaunay753f5362020-11-06 19:01:36 +0100147 dev_dbg(dev, "%s: Cannot found axidcg\n", __func__);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100148 return -EINVAL;
149 }
150 clk_disable(&axidcg); /* disable clock gating during init */
151
152 stm32mp1_ddr_init(priv, &config);
153
154 clk_enable(&axidcg); /* enable clock gating */
155
156 /* check size */
Patrick Delaunay753f5362020-11-06 19:01:36 +0100157 dev_dbg(dev, "get_ram_size(%x, %x)\n",
158 (u32)priv->info.base, (u32)STM32_DDR_SIZE);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100159
160 priv->info.size = get_ram_size((long *)priv->info.base,
161 STM32_DDR_SIZE);
162
Patrick Delaunay753f5362020-11-06 19:01:36 +0100163 dev_dbg(dev, "info.size: %x\n", (u32)priv->info.size);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100164
165 /* check memory access for all memory */
166 if (config.info.size != priv->info.size) {
167 printf("DDR invalid size : 0x%x, expected 0x%x\n",
168 priv->info.size, config.info.size);
169 return -EINVAL;
170 }
171 return 0;
172}
173
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100174static u8 get_data_bus_width(struct stm32mp1_ddrctl *ctl)
175{
176 u32 reg = readl(&ctl->mstr) & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK;
177 u8 data_bus_width = reg >> DDRCTRL_MSTR_DATA_BUS_WIDTH_SHIFT;
178
179 return data_bus_width;
180}
181
182static u8 get_nb_bank(struct stm32mp1_ddrctl *ctl)
183{
184 /* Count bank address bits */
185 u8 bits = 0;
186 u32 reg, val;
187
188 reg = readl(&ctl->addrmap1);
189 /* addrmap1.addrmap_bank_b1 */
190 val = (reg & GENMASK(5, 0)) >> 0;
191 if (val <= 31)
192 bits++;
193 /* addrmap1.addrmap_bank_b2 */
194 val = (reg & GENMASK(13, 8)) >> 8;
195 if (val <= 31)
196 bits++;
197 /* addrmap1.addrmap_bank_b3 */
198 val = (reg & GENMASK(21, 16)) >> 16;
199 if (val <= 31)
200 bits++;
201
202 return bits;
203}
204
205static u8 get_nb_col(struct stm32mp1_ddrctl *ctl, u8 data_bus_width)
206{
207 u8 bits;
208 u32 reg, val;
209
210 /* Count column address bits, start at 2 for b0 and b1 (fixed) */
211 bits = 2;
212
213 reg = readl(&ctl->addrmap2);
214 /* addrmap2.addrmap_col_b2 */
215 val = (reg & GENMASK(3, 0)) >> 0;
216 if (val <= 7)
217 bits++;
218 /* addrmap2.addrmap_col_b3 */
219 val = (reg & GENMASK(11, 8)) >> 8;
220 if (val <= 7)
221 bits++;
222 /* addrmap2.addrmap_col_b4 */
223 val = (reg & GENMASK(19, 16)) >> 16;
224 if (val <= 7)
225 bits++;
226 /* addrmap2.addrmap_col_b5 */
227 val = (reg & GENMASK(27, 24)) >> 24;
228 if (val <= 7)
229 bits++;
230
231 reg = readl(&ctl->addrmap3);
232 /* addrmap3.addrmap_col_b6 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200233 val = (reg & GENMASK(4, 0)) >> 0;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100234 if (val <= 7)
235 bits++;
236 /* addrmap3.addrmap_col_b7 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200237 val = (reg & GENMASK(12, 8)) >> 8;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100238 if (val <= 7)
239 bits++;
240 /* addrmap3.addrmap_col_b8 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200241 val = (reg & GENMASK(20, 16)) >> 16;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100242 if (val <= 7)
243 bits++;
244 /* addrmap3.addrmap_col_b9 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200245 val = (reg & GENMASK(28, 24)) >> 24;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100246 if (val <= 7)
247 bits++;
248
249 reg = readl(&ctl->addrmap4);
250 /* addrmap4.addrmap_col_b10 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200251 val = (reg & GENMASK(4, 0)) >> 0;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100252 if (val <= 7)
253 bits++;
254 /* addrmap4.addrmap_col_b11 */
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200255 val = (reg & GENMASK(12, 8)) >> 8;
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100256 if (val <= 7)
257 bits++;
258
259 /*
260 * column bits shift up:
261 * 1 when half the data bus is used (data_bus_width = 1)
262 * 2 when a quarter the data bus is used (data_bus_width = 2)
263 * nothing to do for full data bus (data_bus_width = 0)
264 */
265 bits += data_bus_width;
266
267 return bits;
268}
269
270static u8 get_nb_row(struct stm32mp1_ddrctl *ctl)
271{
272 /* Count row address bits */
273 u8 bits = 0;
274 u32 reg, val;
275
276 reg = readl(&ctl->addrmap5);
277 /* addrmap5.addrmap_row_b0 */
278 val = (reg & GENMASK(3, 0)) >> 0;
279 if (val <= 11)
280 bits++;
281 /* addrmap5.addrmap_row_b1 */
282 val = (reg & GENMASK(11, 8)) >> 8;
283 if (val <= 11)
284 bits++;
285 /* addrmap5.addrmap_row_b2_10 */
286 val = (reg & GENMASK(19, 16)) >> 16;
287 if (val <= 11)
288 bits += 9;
289 else
290 printf("warning: addrmap5.addrmap_row_b2_10 not supported\n");
291 /* addrmap5.addrmap_row_b11 */
292 val = (reg & GENMASK(27, 24)) >> 24;
293 if (val <= 11)
294 bits++;
295
296 reg = readl(&ctl->addrmap6);
297 /* addrmap6.addrmap_row_b12 */
298 val = (reg & GENMASK(3, 0)) >> 0;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200299 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100300 bits++;
301 /* addrmap6.addrmap_row_b13 */
302 val = (reg & GENMASK(11, 8)) >> 8;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200303 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100304 bits++;
305 /* addrmap6.addrmap_row_b14 */
306 val = (reg & GENMASK(19, 16)) >> 16;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200307 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100308 bits++;
309 /* addrmap6.addrmap_row_b15 */
310 val = (reg & GENMASK(27, 24)) >> 24;
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200311 if (val <= 11)
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100312 bits++;
313
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200314 if (reg & BIT(31))
315 printf("warning: LPDDR3_6GB_12GB is not supported\n");
316
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100317 return bits;
318}
319
320/*
321 * stm32mp1_ddr_size
322 *
323 * Get the current DRAM size from the DDR CTL registers
324 *
325 * @return: DRAM size
326 */
327u32 stm32mp1_ddr_size(struct udevice *dev)
328{
329 u8 nb_bit;
330 u32 ddr_size;
331 u8 data_bus_width;
332 struct ddr_info *priv = dev_get_priv(dev);
333 struct stm32mp1_ddrctl *ctl = priv->ctl;
334 struct stm32mp1_ddr_cfg *cfg = (struct stm32mp1_ddr_cfg *)dev_get_driver_data(dev);
335 const u8 nb_bytes = cfg->nb_bytes;
336
337 data_bus_width = get_data_bus_width(ctl);
338 nb_bit = get_nb_bank(ctl) + get_nb_col(ctl, data_bus_width) +
339 get_nb_row(ctl);
340 if (nb_bit > 32) {
341 nb_bit = 32;
342 debug("invalid DDR configuration: %d bits\n", nb_bit);
343 }
344
345 ddr_size = (nb_bytes >> data_bus_width) << nb_bit;
346 if (ddr_size > STM32_DDR_SIZE) {
347 ddr_size = STM32_DDR_SIZE;
348 debug("invalid DDR configuration: size = %x\n", ddr_size);
349 }
350
351 return ddr_size;
352}
353
Patrick Delaunay939d5362018-03-12 10:46:11 +0100354static int stm32mp1_ddr_probe(struct udevice *dev)
355{
356 struct ddr_info *priv = dev_get_priv(dev);
357 struct regmap *map;
358 int ret;
359
Patrick Delaunay939d5362018-03-12 10:46:11 +0100360 priv->dev = dev;
361
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900362 ret = regmap_init_mem(dev_ofnode(dev), &map);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100363 if (ret)
Patrick Delaunay753f5362020-11-06 19:01:36 +0100364 return log_ret(ret);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100365
366 priv->ctl = regmap_get_range(map, 0);
367 priv->phy = regmap_get_range(map, 1);
368
369 priv->rcc = STM32_RCC_BASE;
370
371 priv->info.base = STM32_DDR_BASE;
372
Patrick Delaunay72a57622021-10-11 09:52:50 +0200373 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
374 priv->info.size = 0;
375 ret = stm32mp1_ddr_setup(dev);
Patrick Delaunay753f5362020-11-06 19:01:36 +0100376
Patrick Delaunay72a57622021-10-11 09:52:50 +0200377 return log_ret(ret);
378 }
379
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100380 priv->info.size = stm32mp1_ddr_size(dev);
381
Patrick Delaunay939d5362018-03-12 10:46:11 +0100382 return 0;
Patrick Delaunay939d5362018-03-12 10:46:11 +0100383}
384
385static int stm32mp1_ddr_get_info(struct udevice *dev, struct ram_info *info)
386{
387 struct ddr_info *priv = dev_get_priv(dev);
388
389 *info = priv->info;
390
391 return 0;
392}
393
394static struct ram_ops stm32mp1_ddr_ops = {
395 .get_info = stm32mp1_ddr_get_info,
396};
397
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200398static const struct stm32mp1_ddr_cfg stm32mp13x_ddr_cfg = {
399 .nb_bytes = 2,
400};
401
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100402static const struct stm32mp1_ddr_cfg stm32mp15x_ddr_cfg = {
403 .nb_bytes = 4,
404};
405
Patrick Delaunay939d5362018-03-12 10:46:11 +0100406static const struct udevice_id stm32mp1_ddr_ids[] = {
Patrick Delaunaydf7fe212021-11-24 10:52:18 +0100407 { .compatible = "st,stm32mp1-ddr", .data = (ulong)&stm32mp15x_ddr_cfg},
Patrick Delaunayb5bb08d2022-05-20 18:24:50 +0200408 { .compatible = "st,stm32mp13-ddr", .data = (ulong)&stm32mp13x_ddr_cfg},
Patrick Delaunay939d5362018-03-12 10:46:11 +0100409 { }
410};
411
412U_BOOT_DRIVER(ddr_stm32mp1) = {
413 .name = "stm32mp1_ddr",
414 .id = UCLASS_RAM,
415 .of_match = stm32mp1_ddr_ids,
416 .ops = &stm32mp1_ddr_ops,
417 .probe = stm32mp1_ddr_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700418 .priv_auto = sizeof(struct ddr_info),
Patrick Delaunay939d5362018-03-12 10:46:11 +0100419};