xref: /openbmc/u-boot/board/ti/ks2_evm/board.c (revision 5ff10aa7)
1 /*
2  * Keystone : Board initialization
3  *
4  * (C) Copyright 2014
5  *     Texas Instruments Incorporated, <www.ti.com>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9 
10 #include "board.h"
11 #include <common.h>
12 #include <exports.h>
13 #include <fdt_support.h>
14 #include <asm/arch/ddr3.h>
15 #include <asm/arch/emac_defs.h>
16 #include <asm/ti-common/ti-aemif.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
20 static struct aemif_config aemif_configs[] = {
21 	{			/* CS0 */
22 		.mode		= AEMIF_MODE_NAND,
23 		.wr_setup	= 0xf,
24 		.wr_strobe	= 0x3f,
25 		.wr_hold	= 7,
26 		.rd_setup	= 0xf,
27 		.rd_strobe	= 0x3f,
28 		.rd_hold	= 7,
29 		.turn_around	= 3,
30 		.width		= AEMIF_WIDTH_8,
31 	},
32 };
33 
34 int dram_init(void)
35 {
36 	ddr3_init();
37 
38 	gd->ram_size = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE,
39 				    CONFIG_MAX_RAM_BANK_SIZE);
40 	aemif_init(ARRAY_SIZE(aemif_configs), aemif_configs);
41 	return 0;
42 }
43 
44 int board_init(void)
45 {
46 	gd->bd->bi_boot_params = CONFIG_LINUX_BOOT_PARAM_ADDR;
47 
48 	return 0;
49 }
50 
51 #ifdef CONFIG_DRIVER_TI_KEYSTONE_NET
52 int get_eth_env_param(char *env_name)
53 {
54 	char *env;
55 	int res = -1;
56 
57 	env = getenv(env_name);
58 	if (env)
59 		res = simple_strtol(env, NULL, 0);
60 
61 	return res;
62 }
63 
64 int board_eth_init(bd_t *bis)
65 {
66 	int j;
67 	int res;
68 	int port_num;
69 	char link_type_name[32];
70 
71 	port_num = get_num_eth_ports();
72 
73 	for (j = 0; j < port_num; j++) {
74 		sprintf(link_type_name, "sgmii%d_link_type", j);
75 		res = get_eth_env_param(link_type_name);
76 		if (res >= 0)
77 			eth_priv_cfg[j].sgmii_link_type = res;
78 
79 		keystone2_emac_initialize(&eth_priv_cfg[j]);
80 	}
81 
82 	return 0;
83 }
84 #endif
85 
86 #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
87 void ft_board_setup(void *blob, bd_t *bd)
88 {
89 	int lpae;
90 	char *env;
91 	char *endp;
92 	int nbanks;
93 	u64 size[2];
94 	u64 start[2];
95 	char name[32];
96 	int nodeoffset;
97 	u32 ddr3a_size;
98 	int unitrd_fixup = 0;
99 
100 	env = getenv("mem_lpae");
101 	lpae = env && simple_strtol(env, NULL, 0);
102 	env = getenv("uinitrd_fixup");
103 	unitrd_fixup = env && simple_strtol(env, NULL, 0);
104 
105 	ddr3a_size = 0;
106 	if (lpae) {
107 		env = getenv("ddr3a_size");
108 		if (env)
109 			ddr3a_size = simple_strtol(env, NULL, 10);
110 		if ((ddr3a_size != 8) && (ddr3a_size != 4))
111 			ddr3a_size = 0;
112 	}
113 
114 	nbanks = 1;
115 	start[0] = bd->bi_dram[0].start;
116 	size[0]  = bd->bi_dram[0].size;
117 
118 	/* adjust memory start address for LPAE */
119 	if (lpae) {
120 		start[0] -= CONFIG_SYS_SDRAM_BASE;
121 		start[0] += CONFIG_SYS_LPAE_SDRAM_BASE;
122 	}
123 
124 	if ((size[0] == 0x80000000) && (ddr3a_size != 0)) {
125 		size[1] = ((u64)ddr3a_size - 2) << 30;
126 		start[1] = 0x880000000;
127 		nbanks++;
128 	}
129 
130 	/* reserve memory at start of bank */
131 	sprintf(name, "mem_reserve_head");
132 	env = getenv(name);
133 	if (env) {
134 		start[0] += ustrtoul(env, &endp, 0);
135 		size[0] -= ustrtoul(env, &endp, 0);
136 	}
137 
138 	sprintf(name, "mem_reserve");
139 	env = getenv(name);
140 	if (env)
141 		size[0] -= ustrtoul(env, &endp, 0);
142 
143 	fdt_fixup_memory_banks(blob, start, size, nbanks);
144 
145 	/* Fix up the initrd */
146 	if (lpae && unitrd_fixup) {
147 		int err;
148 		u32 *prop1, *prop2;
149 		u64 initrd_start, initrd_end;
150 
151 		nodeoffset = fdt_path_offset(blob, "/chosen");
152 		if (nodeoffset >= 0) {
153 			prop1 = (u32 *)fdt_getprop(blob, nodeoffset,
154 					    "linux,initrd-start", NULL);
155 			prop2 = (u32 *)fdt_getprop(blob, nodeoffset,
156 					    "linux,initrd-end", NULL);
157 			if (prop1 && prop2) {
158 				initrd_start = __be32_to_cpu(*prop1);
159 				initrd_start -= CONFIG_SYS_SDRAM_BASE;
160 				initrd_start += CONFIG_SYS_LPAE_SDRAM_BASE;
161 				initrd_start = __cpu_to_be64(initrd_start);
162 				initrd_end = __be32_to_cpu(*prop2);
163 				initrd_end -= CONFIG_SYS_SDRAM_BASE;
164 				initrd_end += CONFIG_SYS_LPAE_SDRAM_BASE;
165 				initrd_end = __cpu_to_be64(initrd_end);
166 
167 				err = fdt_delprop(blob, nodeoffset,
168 						  "linux,initrd-start");
169 				if (err < 0)
170 					puts("error deleting initrd-start\n");
171 
172 				err = fdt_delprop(blob, nodeoffset,
173 						  "linux,initrd-end");
174 				if (err < 0)
175 					puts("error deleting initrd-end\n");
176 
177 				err = fdt_setprop(blob, nodeoffset,
178 						  "linux,initrd-start",
179 						  &initrd_start,
180 						  sizeof(initrd_start));
181 				if (err < 0)
182 					puts("error adding initrd-start\n");
183 
184 				err = fdt_setprop(blob, nodeoffset,
185 						  "linux,initrd-end",
186 						  &initrd_end,
187 						  sizeof(initrd_end));
188 				if (err < 0)
189 					puts("error adding linux,initrd-end\n");
190 			}
191 		}
192 	}
193 }
194 
195 void ft_board_setup_ex(void *blob, bd_t *bd)
196 {
197 	int lpae;
198 	u64 size;
199 	char *env;
200 	u64 *reserve_start;
201 
202 	env = getenv("mem_lpae");
203 	lpae = env && simple_strtol(env, NULL, 0);
204 
205 	if (lpae) {
206 		/*
207 		 * the initrd and other reserved memory areas are
208 		 * embedded in in the DTB itslef. fix up these addresses
209 		 * to 36 bit format
210 		 */
211 		reserve_start = (u64 *)((char *)blob +
212 				       fdt_off_mem_rsvmap(blob));
213 		while (1) {
214 			*reserve_start = __cpu_to_be64(*reserve_start);
215 			size = __cpu_to_be64(*(reserve_start + 1));
216 			if (size) {
217 				*reserve_start -= CONFIG_SYS_SDRAM_BASE;
218 				*reserve_start +=
219 					CONFIG_SYS_LPAE_SDRAM_BASE;
220 				*reserve_start =
221 					__cpu_to_be64(*reserve_start);
222 			} else {
223 				break;
224 			}
225 			reserve_start += 2;
226 		}
227 	}
228 }
229 #endif
230