dram_init.c 1.65 KB
Newer Older
1
/*
2
 * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3 4 5 6 7
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
8
#include <libfdt.h>
9
#include <fdtdec.h>
10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <linux/err.h>

DECLARE_GLOBAL_DATA_PTR;

static const void *get_memory_reg_prop(const void *fdt, int *lenp)
{
	int offset;

	offset = fdt_path_offset(fdt, "/memory");
	if (offset < 0)
		return NULL;

	return fdt_getprop(fdt, offset, "reg", lenp);
}
24 25 26

int dram_init(void)
{
27
	const void *fdt = gd->fdt_blob;
28
	const fdt32_t *val;
29
	int ac, sc, len;
30

31 32 33 34
	ac = fdt_address_cells(fdt, 0);
	sc = fdt_size_cells(fdt, 0);
	if (ac < 0 || sc < 1 || sc > 2) {
		printf("invalid address/size cells\n");
35
		return -EINVAL;
36 37 38 39 40 41 42
	}

	val = get_memory_reg_prop(fdt, &len);
	if (len / sizeof(*val) < ac + sc)
		return -EINVAL;

	val += ac;
43

44
	gd->ram_size = fdtdec_get_number(val, sc);
45

46
	debug("DRAM size = %08lx\n", (unsigned long)gd->ram_size);
47 48 49

	return 0;
}
50 51 52

void dram_init_banksize(void)
{
53
	const void *fdt = gd->fdt_blob;
54
	const fdt32_t *val;
55
	int ac, sc, cells, len, i;
56

57
	val = get_memory_reg_prop(fdt, &len);
58 59 60
	if (len < 0)
		return;

61 62 63 64 65 66 67 68 69
	ac = fdt_address_cells(fdt, 0);
	sc = fdt_size_cells(fdt, 0);
	if (ac < 1 || sc > 2 || sc < 1 || sc > 2) {
		printf("invalid address/size cells\n");
		return;
	}

	cells = ac + sc;

70 71
	len /= sizeof(*val);

72 73
	for (i = 0; i < CONFIG_NR_DRAM_BANKS && len >= cells;
	     i++, len -= cells) {
74
		gd->bd->bi_dram[i].start = fdtdec_get_number(val, ac);
75
		val += ac;
76
		gd->bd->bi_dram[i].size = fdtdec_get_number(val, sc);
77
		val += sc;
78 79

		debug("DRAM bank %d: start = %08lx, size = %08lx\n",
80 81
		      i, (unsigned long)gd->bd->bi_dram[i].start,
		      (unsigned long)gd->bd->bi_dram[i].size);
82 83
	}
}