1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
/*
* Copyright (C) 2018, STMicroelectronics - All Rights Reserved
*/
#define LOG_CATEGORY UCLASS_RAM
#include <common.h>
#include <clk.h>
#include <dm.h>
#include <init.h>
#include <log.h>
#include <ram.h>
#include <regmap.h>
#include <syscon.h>
#include <asm/io.h>
#include <dm/device_compat.h>
#include "stm32mp1_ddr.h"
#include "stm32mp1_ddr_regs.h"
/* DDR subsystem configuration */
struct stm32mp1_ddr_cfg {
u8 nb_bytes; /* MEMC_DRAM_DATA_WIDTH */
};
static const char *const clkname[] = {
"ddrc1",
"ddrc2",
"ddrcapb",
"ddrphycapb",
"ddrphyc" /* LAST clock => used for get_rate() */
};
int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed)
{
unsigned long ddrphy_clk;
unsigned long ddr_clk;
struct clk clk;
int ret;
unsigned int idx;
for (idx = 0; idx < ARRAY_SIZE(clkname); idx++) {
ret = clk_get_by_name(priv->dev, clkname[idx], &clk);
if (!ret)
ret = clk_enable(&clk);
if (ret) {
log_err("error for %s : %d\n", clkname[idx], ret);
return ret;
}
}
priv->clk = clk;
ddrphy_clk = clk_get_rate(&priv->clk);
log_debug("DDR: mem_speed (%d kHz), RCC %d kHz\n",
mem_speed, (u32)(ddrphy_clk / 1000));
/* max 10% frequency delta */
ddr_clk = abs(ddrphy_clk - mem_speed * 1000);
if (ddr_clk > (mem_speed * 100)) {
log_err("DDR expected freq %d kHz, current is %d kHz\n",
mem_speed, (u32)(ddrphy_clk / 1000));
return -EINVAL;
}
return 0;
}
__weak int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
const char *name)
{
return 0; /* Always match */
}
static ofnode stm32mp1_ddr_get_ofnode(struct udevice *dev)
{
const char *name;
ofnode node;
dev_for_each_subnode(node, dev) {
name = ofnode_get_property(node, "compatible", NULL);
if (!board_stm32mp1_ddr_config_name_match(dev, name))
return node;
}
return dev_ofnode(dev);
}
static int stm32mp1_ddr_setup(struct udevice *dev)
{
struct ddr_info *priv = dev_get_priv(dev);
int ret;
unsigned int idx;
struct clk axidcg;
struct stm32mp1_ddr_config config;
ofnode node = stm32mp1_ddr_get_ofnode(dev);
#define PARAM(x, y, z) \
{ .name = x, \
.offset = offsetof(struct stm32mp1_ddr_config, y), \
.size = sizeof(config.y) / sizeof(u32), \
}
#define CTL_PARAM(x) PARAM("st,ctl-"#x, c_##x, NULL)
#define PHY_PARAM(x) PARAM("st,phy-"#x, p_##x, NULL)
const struct {
const char *name; /* name in DT */
const u32 offset; /* offset in config struct */
const u32 size; /* size of parameters */
} param[] = {
CTL_PARAM(reg),
CTL_PARAM(timing),
CTL_PARAM(map),
CTL_PARAM(perf),
PHY_PARAM(reg),
PHY_PARAM(timing)
};
config.info.speed = ofnode_read_u32_default(node, "st,mem-speed", 0);
config.info.size = ofnode_read_u32_default(node, "st,mem-size", 0);
config.info.name = ofnode_read_string(node, "st,mem-name");
if (!config.info.name) {
dev_dbg(dev, "no st,mem-name\n");
return -EINVAL;
}
printf("RAM: %s\n", config.info.name);
for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
ret = ofnode_read_u32_array(node, param[idx].name,
(void *)((u32)&config +
param[idx].offset),
param[idx].size);
dev_dbg(dev, "%s: %s[0x%x] = %d\n", __func__,
param[idx].name, param[idx].size, ret);
if (ret) {
dev_err(dev, "Cannot read %s, error=%d\n",
param[idx].name, ret);
return -EINVAL;
}
}
ret = clk_get_by_name(dev, "axidcg", &axidcg);
if (ret) {
dev_dbg(dev, "%s: Cannot found axidcg\n", __func__);
return -EINVAL;
}
clk_disable(&axidcg); /* disable clock gating during init */
stm32mp1_ddr_init(priv, &config);
clk_enable(&axidcg); /* enable clock gating */
/* check size */
dev_dbg(dev, "get_ram_size(%x, %x)\n",
(u32)priv->info.base, (u32)STM32_DDR_SIZE);
priv->info.size = get_ram_size((long *)priv->info.base,
STM32_DDR_SIZE);
dev_dbg(dev, "info.size: %x\n", (u32)priv->info.size);
/* check memory access for all memory */
if (config.info.size != priv->info.size) {
printf("DDR invalid size : 0x%x, expected 0x%x\n",
priv->info.size, config.info.size);
return -EINVAL;
}
return 0;
}
static u8 get_data_bus_width(struct stm32mp1_ddrctl *ctl)
{
u32 reg = readl(&ctl->mstr) & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK;
u8 data_bus_width = reg >> DDRCTRL_MSTR_DATA_BUS_WIDTH_SHIFT;
return data_bus_width;
}
static u8 get_nb_bank(struct stm32mp1_ddrctl *ctl)
{
/* Count bank address bits */
u8 bits = 0;
u32 reg, val;
reg = readl(&ctl->addrmap1);
/* addrmap1.addrmap_bank_b1 */
val = (reg & GENMASK(5, 0)) >> 0;
if (val <= 31)
bits++;
/* addrmap1.addrmap_bank_b2 */
val = (reg & GENMASK(13, 8)) >> 8;
if (val <= 31)
bits++;
/* addrmap1.addrmap_bank_b3 */
val = (reg & GENMASK(21, 16)) >> 16;
if (val <= 31)
bits++;
return bits;
}
static u8 get_nb_col(struct stm32mp1_ddrctl *ctl, u8 data_bus_width)
{
u8 bits;
u32 reg, val;
/* Count column address bits, start at 2 for b0 and b1 (fixed) */
bits = 2;
reg = readl(&ctl->addrmap2);
/* addrmap2.addrmap_col_b2 */
val = (reg & GENMASK(3, 0)) >> 0;
if (val <= 7)
bits++;
/* addrmap2.addrmap_col_b3 */
val = (reg & GENMASK(11, 8)) >> 8;
if (val <= 7)
bits++;
/* addrmap2.addrmap_col_b4 */
val = (reg & GENMASK(19, 16)) >> 16;
if (val <= 7)
bits++;
/* addrmap2.addrmap_col_b5 */
val = (reg & GENMASK(27, 24)) >> 24;
if (val <= 7)
bits++;
reg = readl(&ctl->addrmap3);
/* addrmap3.addrmap_col_b6 */
val = (reg & GENMASK(4, 0)) >> 0;
if (val <= 7)
bits++;
/* addrmap3.addrmap_col_b7 */
val = (reg & GENMASK(12, 8)) >> 8;
if (val <= 7)
bits++;
/* addrmap3.addrmap_col_b8 */
val = (reg & GENMASK(20, 16)) >> 16;
if (val <= 7)
bits++;
/* addrmap3.addrmap_col_b9 */
val = (reg & GENMASK(28, 24)) >> 24;
if (val <= 7)
bits++;
reg = readl(&ctl->addrmap4);
/* addrmap4.addrmap_col_b10 */
val = (reg & GENMASK(4, 0)) >> 0;
if (val <= 7)
bits++;
/* addrmap4.addrmap_col_b11 */
val = (reg & GENMASK(12, 8)) >> 8;
if (val <= 7)
bits++;
/*
* column bits shift up:
* 1 when half the data bus is used (data_bus_width = 1)
* 2 when a quarter the data bus is used (data_bus_width = 2)
* nothing to do for full data bus (data_bus_width = 0)
*/
bits += data_bus_width;
return bits;
}
static u8 get_nb_row(struct stm32mp1_ddrctl *ctl)
{
/* Count row address bits */
u8 bits = 0;
u32 reg, val;
reg = readl(&ctl->addrmap5);
/* addrmap5.addrmap_row_b0 */
val = (reg & GENMASK(3, 0)) >> 0;
if (val <= 11)
bits++;
/* addrmap5.addrmap_row_b1 */
val = (reg & GENMASK(11, 8)) >> 8;
if (val <= 11)
bits++;
/* addrmap5.addrmap_row_b2_10 */
val = (reg & GENMASK(19, 16)) >> 16;
if (val <= 11)
bits += 9;
else
printf("warning: addrmap5.addrmap_row_b2_10 not supported\n");
/* addrmap5.addrmap_row_b11 */
val = (reg & GENMASK(27, 24)) >> 24;
if (val <= 11)
bits++;
reg = readl(&ctl->addrmap6);
/* addrmap6.addrmap_row_b12 */
val = (reg & GENMASK(3, 0)) >> 0;
if (val <= 11)
bits++;
/* addrmap6.addrmap_row_b13 */
val = (reg & GENMASK(11, 8)) >> 8;
if (val <= 11)
bits++;
/* addrmap6.addrmap_row_b14 */
val = (reg & GENMASK(19, 16)) >> 16;
if (val <= 11)
bits++;
/* addrmap6.addrmap_row_b15 */
val = (reg & GENMASK(27, 24)) >> 24;
if (val <= 11)
bits++;
if (reg & BIT(31))
printf("warning: LPDDR3_6GB_12GB is not supported\n");
return bits;
}
/*
* stm32mp1_ddr_size
*
* Get the current DRAM size from the DDR CTL registers
*
* @return: DRAM size
*/
u32 stm32mp1_ddr_size(struct udevice *dev)
{
u8 nb_bit;
u32 ddr_size;
u8 data_bus_width;
struct ddr_info *priv = dev_get_priv(dev);
struct stm32mp1_ddrctl *ctl = priv->ctl;
struct stm32mp1_ddr_cfg *cfg = (struct stm32mp1_ddr_cfg *)dev_get_driver_data(dev);
const u8 nb_bytes = cfg->nb_bytes;
data_bus_width = get_data_bus_width(ctl);
nb_bit = get_nb_bank(ctl) + get_nb_col(ctl, data_bus_width) +
get_nb_row(ctl);
if (nb_bit > 32) {
nb_bit = 32;
debug("invalid DDR configuration: %d bits\n", nb_bit);
}
ddr_size = (nb_bytes >> data_bus_width) << nb_bit;
if (ddr_size > STM32_DDR_SIZE) {
ddr_size = STM32_DDR_SIZE;
debug("invalid DDR configuration: size = %x\n", ddr_size);
}
return ddr_size;
}
static int stm32mp1_ddr_probe(struct udevice *dev)
{
struct ddr_info *priv = dev_get_priv(dev);
struct regmap *map;
int ret;
priv->dev = dev;
ret = regmap_init_mem(dev_ofnode(dev), &map);
if (ret)
return log_ret(ret);
priv->ctl = regmap_get_range(map, 0);
priv->phy = regmap_get_range(map, 1);
priv->rcc = STM32_RCC_BASE;
priv->info.base = STM32_DDR_BASE;
if (IS_ENABLED(CONFIG_SPL_BUILD)) {
priv->info.size = 0;
ret = stm32mp1_ddr_setup(dev);
return log_ret(ret);
}
priv->info.size = stm32mp1_ddr_size(dev);
return 0;
}
static int stm32mp1_ddr_get_info(struct udevice *dev, struct ram_info *info)
{
struct ddr_info *priv = dev_get_priv(dev);
*info = priv->info;
return 0;
}
static struct ram_ops stm32mp1_ddr_ops = {
.get_info = stm32mp1_ddr_get_info,
};
static const struct stm32mp1_ddr_cfg stm32mp13x_ddr_cfg = {
.nb_bytes = 2,
};
static const struct stm32mp1_ddr_cfg stm32mp15x_ddr_cfg = {
.nb_bytes = 4,
};
static const struct udevice_id stm32mp1_ddr_ids[] = {
{ .compatible = "st,stm32mp1-ddr", .data = (ulong)&stm32mp15x_ddr_cfg},
{ .compatible = "st,stm32mp13-ddr", .data = (ulong)&stm32mp13x_ddr_cfg},
{ }
};
U_BOOT_DRIVER(ddr_stm32mp1) = {
.name = "stm32mp1_ddr",
.id = UCLASS_RAM,
.of_match = stm32mp1_ddr_ids,
.ops = &stm32mp1_ddr_ops,
.probe = stm32mp1_ddr_probe,
.priv_auto = sizeof(struct ddr_info),
};
|