1 /*
2  * Copyright (C) 2016 Stefan Roese <sr@denx.de>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <fdtdec.h>
10 #include <libfdt.h>
11 #include <asm/io.h>
12 #include <asm/system.h>
13 #include <asm/arch/cpu.h>
14 #include <asm/arch/soc.h>
15 #include <asm/armv8/mmu.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 /*
20  * On ARMv8, MBus is not configured in U-Boot. To enable compilation
21  * of the already implemented drivers, lets add a dummy version of
22  * this function so that linking does not fail.
23  */
24 const struct mbus_dram_target_info *mvebu_mbus_dram_info(void)
25 {
26 	return NULL;
27 }
28 
29 /* DRAM init code ... */
30 
31 static const void *get_memory_reg_prop(const void *fdt, int *lenp)
32 {
33 	int offset;
34 
35 	offset = fdt_path_offset(fdt, "/memory");
36 	if (offset < 0)
37 		return NULL;
38 
39 	return fdt_getprop(fdt, offset, "reg", lenp);
40 }
41 
42 int dram_init(void)
43 {
44 	const void *fdt = gd->fdt_blob;
45 	const fdt32_t *val;
46 	int ac, sc, len;
47 
48 	ac = fdt_address_cells(fdt, 0);
49 	sc = fdt_size_cells(fdt, 0);
50 	if (ac < 0 || sc < 1 || sc > 2) {
51 		printf("invalid address/size cells\n");
52 		return -EINVAL;
53 	}
54 
55 	val = get_memory_reg_prop(fdt, &len);
56 	if (len / sizeof(*val) < ac + sc)
57 		return -EINVAL;
58 
59 	val += ac;
60 
61 	gd->ram_size = fdtdec_get_number(val, sc);
62 
63 	debug("DRAM size = %08lx\n", (unsigned long)gd->ram_size);
64 
65 	return 0;
66 }
67 
68 void dram_init_banksize(void)
69 {
70 	const void *fdt = gd->fdt_blob;
71 	const fdt32_t *val;
72 	int ac, sc, cells, len, i;
73 
74 	val = get_memory_reg_prop(fdt, &len);
75 	if (len < 0)
76 		return;
77 
78 	ac = fdt_address_cells(fdt, 0);
79 	sc = fdt_size_cells(fdt, 0);
80 	if (ac < 1 || sc > 2 || sc < 1 || sc > 2) {
81 		printf("invalid address/size cells\n");
82 		return;
83 	}
84 
85 	cells = ac + sc;
86 
87 	len /= sizeof(*val);
88 
89 	for (i = 0; i < CONFIG_NR_DRAM_BANKS && len >= cells;
90 	     i++, len -= cells) {
91 		gd->bd->bi_dram[i].start = fdtdec_get_number(val, ac);
92 		val += ac;
93 		gd->bd->bi_dram[i].size = fdtdec_get_number(val, sc);
94 		val += sc;
95 
96 		debug("DRAM bank %d: start = %08lx, size = %08lx\n",
97 		      i, (unsigned long)gd->bd->bi_dram[i].start,
98 		      (unsigned long)gd->bd->bi_dram[i].size);
99 	}
100 }
101 
102 int arch_cpu_init(void)
103 {
104 	/* Nothing to do (yet) */
105 	return 0;
106 }
107 
108 int arch_early_init_r(void)
109 {
110 	struct udevice *dev;
111 	int ret;
112 
113 	/* Call the comphy code via the MISC uclass driver */
114 	ret = uclass_get_device(UCLASS_MISC, 0, &dev);
115 	if (ret) {
116 		debug("COMPHY init failed: %d\n", ret);
117 		return -ENODEV;
118 	}
119 
120 	/* Cause the SATA device to do its early init */
121 	uclass_first_device(UCLASS_AHCI, &dev);
122 
123 	return 0;
124 }
125