1 /*
2  * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <libfdt.h>
9 #include <linux/err.h>
10 
11 DECLARE_GLOBAL_DATA_PTR;
12 
13 static const void *get_memory_reg_prop(const void *fdt, int *lenp)
14 {
15 	int offset;
16 
17 	offset = fdt_path_offset(fdt, "/memory");
18 	if (offset < 0)
19 		return NULL;
20 
21 	return fdt_getprop(fdt, offset, "reg", lenp);
22 }
23 
24 int dram_init(void)
25 {
26 	const fdt32_t *val;
27 	int len;
28 
29 	val = get_memory_reg_prop(gd->fdt_blob, &len);
30 	if (len < sizeof(*val))
31 		return -EINVAL;
32 
33 	gd->ram_size = fdt32_to_cpu(*(val + 1));
34 
35 	debug("DRAM size = %08lx\n", gd->ram_size);
36 
37 	return 0;
38 }
39 
40 void dram_init_banksize(void)
41 {
42 	const fdt32_t *val;
43 	int len, i;
44 
45 	val = get_memory_reg_prop(gd->fdt_blob, &len);
46 	if (len < 0)
47 		return;
48 
49 	len /= sizeof(*val);
50 	len /= 2;
51 
52 	for (i = 0; i < len; i++) {
53 		gd->bd->bi_dram[i].start = fdt32_to_cpu(*val++);
54 		gd->bd->bi_dram[i].size = fdt32_to_cpu(*val++);
55 
56 		debug("DRAM bank %d: start = %08lx, size = %08lx\n",
57 		      i, gd->bd->bi_dram[i].start, gd->bd->bi_dram[i].size);
58 	}
59 }
60