forked from u-boot/u-boot
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsdram_ast2500.c
More file actions
540 lines (446 loc) · 14.6 KB
/
sdram_ast2500.c
File metadata and controls
540 lines (446 loc) · 14.6 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2012-2020 ASPEED Technology Inc.
*
* Copyright 2016 Google, Inc
*/
#include <common.h>
#include <clk.h>
#include <dm.h>
#include <errno.h>
#include <ram.h>
#include <regmap.h>
#include <reset.h>
#include <asm/io.h>
#include <asm/arch/scu_ast2500.h>
#include <asm/arch/sdram_ast2500.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <dt-bindings/clock/ast2500-clock.h>
/* in order to speed up DRAM init time, write pre-defined values to registers
* directly */
#define AST2500_SDRAMMC_MANUAL_CLK
/* bit-field of m_pll_param */
#define SCU_MPLL_FREQ_MASK (SCU_MPLL_DENUM_MASK | SCU_MPLL_NUM_MASK | SCU_MPLL_POST_MASK)
#define SCU_MPLL_FREQ_400M 0x93002400
#define SCU_MPLL_FREQ_360M 0x930023A0
#define SCU_MPLL_FREQ_CFG SCU_MPLL_FREQ_360M
#define SCU_MPLL_TURN_OFF BIT(19)
#define SCU_MPLL_BYPASS BIT(20)
#define SCU_MPLL_RESET BIT(21)
/* These configuration parameters are taken from Aspeed SDK */
#define DDR4_MR46_MODE 0x08000000
#define DDR4_MR5_MODE 0x400
#define DDR4_MR13_MODE 0x101
#define DDR4_MR02_MODE 0x410
#define DDR4_TRFC 0x45457188
#define PHY_CFG_SIZE 15
static const u32 ddr4_ac_timing[3] = {0x63604e37, 0xe97afa99, 0x00019000};
static const struct {
u32 index[PHY_CFG_SIZE];
u32 value[PHY_CFG_SIZE];
} ddr4_phy_config = {
.index = {0, 1, 3, 4, 5, 56, 57, 58, 59, 60, 61, 62, 36, 49, 50},
.value = {
0x42492aae, 0x09002000, 0x55e00b0b, 0x20000000, 0x24,
0x03002900, 0x0e0000a0, 0x000e001c, 0x35b8c106, 0x08080607,
0x9b000900, 0x0e400a00, 0x00100008, 0x3c183c3c, 0x00631e0e,
},
};
/* supported SDRAM size */
#define SDRAM_SIZE_1KB (1024U)
#define SDRAM_SIZE_1MB (SDRAM_SIZE_1KB * SDRAM_SIZE_1KB)
#define SDRAM_MIN_SIZE (128 * SDRAM_SIZE_1MB)
#define SDRAM_MAX_SIZE (1024 * SDRAM_SIZE_1MB)
DECLARE_GLOBAL_DATA_PTR;
/*
* Bandwidth configuration parameters for different SDRAM requests.
* These are hardcoded settings taken from Aspeed SDK.
*/
static const u32 ddr_max_grant_params[4] = {
0x88448844, 0x24422288, 0x22222222, 0x22222222
};
/*
* These registers are not documented by Aspeed at all.
* All writes and reads are taken pretty much as is from SDK.
*/
struct ast2500_ddr_phy {
u32 phy[117];
};
struct dram_info {
struct ram_info info;
struct clk ddr_clk;
struct ast2500_sdrammc_regs *regs;
struct ast2500_scu *scu;
struct ast2500_ddr_phy *phy;
ulong clock_rate;
};
static int ast2500_sdrammc_init_phy(struct ast2500_ddr_phy *phy)
{
writel(0, &phy->phy[2]);
writel(0, &phy->phy[6]);
writel(0, &phy->phy[8]);
writel(0, &phy->phy[10]);
writel(0, &phy->phy[12]);
writel(0, &phy->phy[42]);
writel(0, &phy->phy[44]);
writel(0x86000000, &phy->phy[16]);
writel(0x00008600, &phy->phy[17]);
writel(0x80000000, &phy->phy[18]);
writel(0x80808080, &phy->phy[19]);
return 0;
}
static void ast2500_ddr_phy_init_process(struct dram_info *info)
{
struct ast2500_sdrammc_regs *regs = info->regs;
writel(0, ®s->phy_ctrl[0]);
writel(0x4040, &info->phy->phy[51]);
writel(SDRAM_PHYCTRL0_NRST | SDRAM_PHYCTRL0_INIT, ®s->phy_ctrl[0]);
while ((readl(®s->phy_ctrl[0]) & SDRAM_PHYCTRL0_INIT))
;
writel(SDRAM_PHYCTRL0_NRST | SDRAM_PHYCTRL0_AUTO_UPDATE,
®s->phy_ctrl[0]);
}
static void ast2500_sdrammc_set_vref(struct dram_info *info, u32 vref)
{
writel(0, &info->regs->phy_ctrl[0]);
writel((vref << 8) | 0x6, &info->phy->phy[48]);
ast2500_ddr_phy_init_process(info);
}
static int ast2500_ddr_cbr_test(struct dram_info *info)
{
struct ast2500_sdrammc_regs *regs = info->regs;
int i;
const u32 test_params = SDRAM_TEST_EN
| SDRAM_TEST_ERRSTOP
| SDRAM_TEST_TWO_MODES;
int ret = 0;
writel((1 << SDRAM_REFRESH_CYCLES_SHIFT) |
(0x5c << SDRAM_REFRESH_PERIOD_SHIFT), ®s->refresh_timing);
writel((0xfff << SDRAM_TEST_LEN_SHIFT), ®s->test_addr);
writel(0xff00ff00, ®s->test_init_val);
writel(SDRAM_TEST_EN | (SDRAM_TEST_MODE_RW << SDRAM_TEST_MODE_SHIFT) |
SDRAM_TEST_ERRSTOP, ®s->ecc_test_ctrl);
while (!(readl(®s->ecc_test_ctrl) & SDRAM_TEST_DONE))
;
if (readl(®s->ecc_test_ctrl) & SDRAM_TEST_FAIL) {
ret = -EIO;
} else {
for (i = 0; i <= SDRAM_TEST_GEN_MODE_MASK; ++i) {
writel((i << SDRAM_TEST_GEN_MODE_SHIFT) | test_params,
®s->ecc_test_ctrl);
while (!(readl(®s->ecc_test_ctrl) & SDRAM_TEST_DONE))
;
if (readl(®s->ecc_test_ctrl) & SDRAM_TEST_FAIL) {
ret = -EIO;
break;
}
}
}
writel(0, ®s->refresh_timing);
writel(0, ®s->ecc_test_ctrl);
return ret;
}
static int ast2500_sdrammc_ddr4_calibrate_vref(struct dram_info *info)
{
int i;
int vref_min = 0xff;
int vref_max = 0;
int range_size = 0;
for (i = 1; i < 0x40; ++i) {
int res;
ast2500_sdrammc_set_vref(info, i);
res = ast2500_ddr_cbr_test(info);
if (res < 0) {
if (range_size > 0)
break;
} else {
++range_size;
vref_min = min(vref_min, i);
vref_max = max(vref_max, i);
}
}
/* Pick average setting */
ast2500_sdrammc_set_vref(info, (vref_min + vref_max + 1) / 2);
return 0;
}
static size_t ast2500_sdrammc_get_vga_mem_size(struct dram_info *info)
{
size_t vga_mem_size_base = 8 * 1024 * 1024;
u32 vga_hwconf = (readl(&info->scu->hwstrap) & SCU_HWSTRAP_VGAMEM_MASK)
>> SCU_HWSTRAP_VGAMEM_SHIFT;
return vga_mem_size_base << vga_hwconf;
}
static void ast2500_sdrammc_update_size(struct dram_info *info)
{
struct ast2500_sdrammc_regs *regs = info->regs;
size_t hw_size;
size_t ram_size_tbl[4] = { 128 * SDRAM_SIZE_1MB,
256 * SDRAM_SIZE_1MB,
512 * SDRAM_SIZE_1MB,
1024 * SDRAM_SIZE_1MB };
size_t ram_size = SDRAM_MAX_SIZE;
u32 cap_param;
cap_param = (readl(&info->regs->config) & SDRAM_CONF_CAP_MASK) >> SDRAM_CONF_CAP_SHIFT;
ram_size = ram_size_tbl[cap_param];
info->info.base = CONFIG_SYS_SDRAM_BASE;
info->info.size = ram_size - ast2500_sdrammc_get_vga_mem_size(info);
if (0 == (readl(®s->config) & SDRAM_CONF_ECC_EN))
return;
hw_size = readl(®s->ecc_range_ctrl) & GENMASK(29, 20);
hw_size += (1 << 20);
info->info.size = hw_size;
}
/*
* Find out RAM size and save it in dram_info
*
* The procedure is taken from Aspeed SDK
*/
static void ast2500_sdrammc_calc_size(struct dram_info *info)
{
/* The controller supports 128/256/512/1024 MB ram */
size_t ram_size = SDRAM_MIN_SIZE;
const int write_test_offset = 0x100000;
u32 test_pattern = 0xdeadbeef;
u32 cap_param = SDRAM_CONF_CAP_1024M;
u32 refresh_timing_param = DDR4_TRFC;
const u32 write_addr_base = CONFIG_SYS_SDRAM_BASE + write_test_offset;
for (ram_size = SDRAM_MAX_SIZE; ram_size > SDRAM_MIN_SIZE;
ram_size >>= 1) {
writel(test_pattern, write_addr_base + (ram_size >> 1));
test_pattern = (test_pattern >> 4) | (test_pattern << 28);
}
/* One last write to overwrite all wrapped values */
writel(test_pattern, write_addr_base);
/* Reset the pattern and see which value was really written */
test_pattern = 0xdeadbeef;
for (ram_size = SDRAM_MAX_SIZE; ram_size > SDRAM_MIN_SIZE;
ram_size >>= 1) {
if (readl(write_addr_base + (ram_size >> 1)) == test_pattern)
break;
--cap_param;
refresh_timing_param >>= 8;
test_pattern = (test_pattern >> 4) | (test_pattern << 28);
}
clrsetbits_le32(&info->regs->ac_timing[1],
(SDRAM_AC_TRFC_MASK << SDRAM_AC_TRFC_SHIFT),
((refresh_timing_param & SDRAM_AC_TRFC_MASK)
<< SDRAM_AC_TRFC_SHIFT));
info->info.base = CONFIG_SYS_SDRAM_BASE;
info->info.size = ram_size - ast2500_sdrammc_get_vga_mem_size(info);
clrsetbits_le32(&info->regs->config,
(SDRAM_CONF_CAP_MASK << SDRAM_CONF_CAP_SHIFT),
((cap_param & SDRAM_CONF_CAP_MASK)
<< SDRAM_CONF_CAP_SHIFT));
}
static void ast2500_sdrammc_ecc_enable(struct dram_info *info, u32 conf_size_mb)
{
struct ast2500_sdrammc_regs *regs = info->regs;
size_t conf_size;
u32 reg;
conf_size = conf_size_mb * SDRAM_SIZE_1MB;
if (conf_size > info->info.size) {
printf("warning: ECC configured %dMB but actual size is %dMB\n",
conf_size_mb,
info->info.size / SDRAM_SIZE_1MB);
conf_size = info->info.size;
} else if (conf_size == 0) {
conf_size = info->info.size;
}
info->info.size = (((conf_size / 9) * 8) >> 20) << 20;
writel(((info->info.size >> 20) - 1) << 20, ®s->ecc_range_ctrl);
reg = readl(®s->config) |
(SDRAM_CONF_ECC_EN | SDRAM_CONF_CACHE_ADDR_CTRL);
writel(reg, ®s->config);
writel(0, ®s->test_init_val);
writel(0, ®s->test_addr);
writel(0x221, ®s->ecc_test_ctrl);
while (0 == (readl(®s->ecc_test_ctrl) & BIT(12)))
;
writel(0, ®s->ecc_test_ctrl);
writel(BIT(31), ®s->intr_ctrl);
writel(0, ®s->intr_ctrl);
writel(0x400, ®s->ecc_test_ctrl);
printf("ECC enable, ");
}
static int ast2500_sdrammc_init_ddr4(struct udevice *dev)
{
struct dram_info *info = dev_get_priv(dev);
int i;
const u32 power_control = SDRAM_PCR_CKE_EN
| (1 << SDRAM_PCR_CKE_DELAY_SHIFT)
| (2 << SDRAM_PCR_TCKE_PW_SHIFT)
| SDRAM_PCR_RESETN_DIS
| SDRAM_PCR_RGAP_CTRL_EN | SDRAM_PCR_ODT_EN | SDRAM_PCR_ODT_EXT_EN;
const u32 conf = (SDRAM_CONF_CAP_1024M << SDRAM_CONF_CAP_SHIFT)
#ifdef CONFIG_DUALX8_RAM
| SDRAM_CONF_DUALX8
#endif
| SDRAM_CONF_SCRAMBLE | SDRAM_CONF_SCRAMBLE_PAT2 | SDRAM_CONF_DDR4;
int ret;
writel(conf, &info->regs->config);
for (i = 0; i < ARRAY_SIZE(ddr4_ac_timing); ++i)
writel(ddr4_ac_timing[i], &info->regs->ac_timing[i]);
writel(DDR4_MR46_MODE, &info->regs->mr46_mode_setting);
writel(DDR4_MR5_MODE, &info->regs->mr5_mode_setting);
writel(DDR4_MR02_MODE, &info->regs->mr02_mode_setting);
writel(DDR4_MR13_MODE, &info->regs->mr13_mode_setting);
for (i = 0; i < PHY_CFG_SIZE; ++i) {
writel(ddr4_phy_config.value[i],
&info->phy->phy[ddr4_phy_config.index[i]]);
}
writel(power_control, &info->regs->power_control);
ast2500_ddr_phy_init_process(info);
ret = ast2500_sdrammc_ddr4_calibrate_vref(info);
if (ret < 0) {
debug("Vref calibration failed!\n");
return ret;
}
writel((1 << SDRAM_REFRESH_CYCLES_SHIFT)
| SDRAM_REFRESH_ZQCS_EN | (0x2f << SDRAM_REFRESH_PERIOD_SHIFT),
&info->regs->refresh_timing);
setbits_le32(&info->regs->power_control,
SDRAM_PCR_AUTOPWRDN_EN | SDRAM_PCR_ODT_AUTO_ON);
ast2500_sdrammc_calc_size(info);
setbits_le32(&info->regs->config, SDRAM_CONF_CACHE_INIT_EN);
while (!(readl(&info->regs->config) & SDRAM_CONF_CACHE_INIT_DONE))
;
setbits_le32(&info->regs->config, SDRAM_CONF_CACHE_EN);
writel(SDRAM_MISC_DDR4_TREFRESH, &info->regs->misc_control);
if (dev_read_bool(dev, "aspeed,ecc-enabled")) {
u32 ecc_size;
ecc_size = dev_read_u32_default(dev, "aspeed,ecc-size-mb", 0);
ast2500_sdrammc_ecc_enable(info, ecc_size);
}
/* Enable all requests except video & display */
writel(SDRAM_REQ_USB20_EHCI1
| SDRAM_REQ_USB20_EHCI2
| SDRAM_REQ_CPU
| SDRAM_REQ_AHB2
| SDRAM_REQ_AHB
| SDRAM_REQ_MAC0
| SDRAM_REQ_MAC1
| SDRAM_REQ_PCIE
| SDRAM_REQ_XDMA
| SDRAM_REQ_ENCRYPTION
| SDRAM_REQ_VIDEO_FLAG
| SDRAM_REQ_VIDEO_LOW_PRI_WRITE
| SDRAM_REQ_2D_RW
| SDRAM_REQ_MEMCHECK, &info->regs->req_limit_mask);
return 0;
}
static void ast2500_sdrammc_unlock(struct dram_info *info)
{
writel(SDRAM_UNLOCK_KEY, &info->regs->protection_key);
while (!readl(&info->regs->protection_key))
;
}
static void ast2500_sdrammc_lock(struct dram_info *info)
{
writel(~SDRAM_UNLOCK_KEY, &info->regs->protection_key);
while (readl(&info->regs->protection_key))
;
}
static int ast2500_sdrammc_probe(struct udevice *dev)
{
struct dram_info *priv = (struct dram_info *)dev_get_priv(dev);
struct ast2500_sdrammc_regs *regs = priv->regs;
struct udevice *clk_dev;
int i;
int ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
uint32_t reg;
if (ret) {
debug("DDR:No CLK\n");
return ret;
}
/* find the SCU base address from the aspeed clock device */
ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu),
&clk_dev);
if (ret) {
debug("clock device not defined\n");
return ret;
}
priv->scu = devfdt_get_addr_ptr(clk_dev);
if (IS_ERR(priv->scu)) {
debug("%s(): can't get SCU\n", __func__);
return PTR_ERR(priv->scu);
}
if (readl(&priv->scu->vga_handshake[0]) & (0x1 << 6)) {
ast2500_sdrammc_update_size(priv);
if (!(readl(&priv->regs->config) & SDRAM_CONF_CACHE_EN)) {
setbits_le32(&priv->regs->config,
SDRAM_CONF_CACHE_INIT_EN);
while (!(readl(&priv->regs->config) &
SDRAM_CONF_CACHE_INIT_DONE))
;
setbits_le32(&priv->regs->config, SDRAM_CONF_CACHE_EN);
}
writel(SDRAM_MISC_DDR4_TREFRESH, &priv->regs->misc_control);
return 0;
}
#ifdef AST2500_SDRAMMC_MANUAL_CLK
reg = readl(&priv->scu->m_pll_param);
reg |= (SCU_MPLL_RESET | SCU_MPLL_TURN_OFF);
writel(reg, &priv->scu->m_pll_param);
reg &= ~(SCU_MPLL_RESET | SCU_MPLL_TURN_OFF| SCU_MPLL_FREQ_MASK);
reg |= SCU_MPLL_FREQ_CFG;
writel(reg, &priv->scu->m_pll_param);
#else
clk_set_rate(&priv->ddr_clk, priv->clock_rate);
#endif
ast2500_sdrammc_unlock(priv);
writel(SDRAM_PCR_MREQI_DIS | SDRAM_PCR_RESETN_DIS,
®s->power_control);
writel(SDRAM_VIDEO_UNLOCK_KEY, ®s->gm_protection_key);
/* Mask all requests except CPU and AHB during PHY init */
writel(~(SDRAM_REQ_CPU | SDRAM_REQ_AHB), ®s->req_limit_mask);
for (i = 0; i < ARRAY_SIZE(ddr_max_grant_params); ++i)
writel(ddr_max_grant_params[i], ®s->max_grant_len[i]);
setbits_le32(®s->intr_ctrl, SDRAM_ICR_RESET_ALL);
ast2500_sdrammc_init_phy(priv->phy);
if (readl(&priv->scu->hwstrap) & SCU_HWSTRAP_DDR4) {
ast2500_sdrammc_init_ddr4(dev);
} else {
debug("Unsupported DRAM3\n");
return -EINVAL;
}
clrbits_le32(®s->intr_ctrl, SDRAM_ICR_RESET_ALL);
ast2500_sdrammc_lock(priv);
setbits_le32(&priv->scu->vga_handshake[0], BIT(6) | BIT(7));
return 0;
}
static int ast2500_sdrammc_ofdata_to_platdata(struct udevice *dev)
{
struct dram_info *priv = dev_get_priv(dev);
priv->regs = (void *)(uintptr_t)devfdt_get_addr_index(dev, 0);
priv->phy = (void *)(uintptr_t)devfdt_get_addr_index(dev, 1);
priv->clock_rate = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
"clock-frequency", 0);
if (!priv->clock_rate) {
debug("DDR Clock Rate not defined\n");
return -EINVAL;
}
return 0;
}
static int ast2500_sdrammc_get_info(struct udevice *dev, struct ram_info *info)
{
struct dram_info *priv = dev_get_priv(dev);
*info = priv->info;
return 0;
}
static struct ram_ops ast2500_sdrammc_ops = {
.get_info = ast2500_sdrammc_get_info,
};
static const struct udevice_id ast2500_sdrammc_ids[] = {
{ .compatible = "aspeed,ast2500-sdrammc" },
{ }
};
U_BOOT_DRIVER(sdrammc_ast2500) = {
.name = "aspeed_ast2500_sdrammc",
.id = UCLASS_RAM,
.of_match = ast2500_sdrammc_ids,
.ops = &ast2500_sdrammc_ops,
.ofdata_to_platdata = ast2500_sdrammc_ofdata_to_platdata,
.probe = ast2500_sdrammc_probe,
.priv_auto_alloc_size = sizeof(struct dram_info),
};