blob: ef0e2e8f54c5ca89cc0d101ca229a4b9a5df8b3f [file] [log] [blame]
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09001/*
Masahiro Yamadab4782cd2015-09-11 20:17:49 +09002 * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09003 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Masahiro Yamadab4782cd2015-09-11 20:17:49 +09008#include <libfdt.h>
Masahiro Yamada1f752802016-04-21 14:43:12 +09009#include <fdtdec.h>
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090010#include <linux/err.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
14static const void *get_memory_reg_prop(const void *fdt, int *lenp)
15{
16 int offset;
17
18 offset = fdt_path_offset(fdt, "/memory");
19 if (offset < 0)
20 return NULL;
21
22 return fdt_getprop(fdt, offset, "reg", lenp);
23}
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090024
25int dram_init(void)
26{
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090027 const void *fdt = gd->fdt_blob;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090028 const fdt32_t *val;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090029 int ac, sc, len;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090030
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090031 ac = fdt_address_cells(fdt, 0);
32 sc = fdt_size_cells(fdt, 0);
33 if (ac < 0 || sc < 1 || sc > 2) {
34 printf("invalid address/size cells\n");
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090035 return -EINVAL;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090036 }
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090037
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090038 val = get_memory_reg_prop(fdt, &len);
39 if (len / sizeof(*val) < ac + sc)
40 return -EINVAL;
41
42 val += ac;
43
Masahiro Yamada1f752802016-04-21 14:43:12 +090044 gd->ram_size = fdtdec_get_number(val, sc);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090045
Masahiro Yamada09350102016-02-26 18:59:45 +090046 debug("DRAM size = %08lx\n", (unsigned long)gd->ram_size);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090047
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090048 return 0;
49}
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090050
51void dram_init_banksize(void)
52{
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090053 const void *fdt = gd->fdt_blob;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090054 const fdt32_t *val;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090055 int ac, sc, cells, len, i;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090056
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090057 val = get_memory_reg_prop(fdt, &len);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090058 if (len < 0)
59 return;
60
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090061 ac = fdt_address_cells(fdt, 0);
62 sc = fdt_size_cells(fdt, 0);
63 if (ac < 1 || sc > 2 || sc < 1 || sc > 2) {
64 printf("invalid address/size cells\n");
65 return;
66 }
67
68 cells = ac + sc;
69
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090070 len /= sizeof(*val);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090071
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090072 for (i = 0; i < CONFIG_NR_DRAM_BANKS && len >= cells;
73 i++, len -= cells) {
Masahiro Yamada1f752802016-04-21 14:43:12 +090074 gd->bd->bi_dram[i].start = fdtdec_get_number(val, ac);
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090075 val += ac;
Masahiro Yamada1f752802016-04-21 14:43:12 +090076 gd->bd->bi_dram[i].size = fdtdec_get_number(val, sc);
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090077 val += sc;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090078
79 debug("DRAM bank %d: start = %08lx, size = %08lx\n",
Masahiro Yamada09350102016-02-26 18:59:45 +090080 i, (unsigned long)gd->bd->bi_dram[i].start,
81 (unsigned long)gd->bd->bi_dram[i].size);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090082 }
83}