xref: /openbmc/u-boot/board/ti/ks2_evm/board.c (revision 8626cb80)
1e595107eSHao Zhang /*
2e595107eSHao Zhang  * Keystone : Board initialization
3e595107eSHao Zhang  *
4e595107eSHao Zhang  * (C) Copyright 2014
5e595107eSHao Zhang  *     Texas Instruments Incorporated, <www.ti.com>
6e595107eSHao Zhang  *
7e595107eSHao Zhang  * SPDX-License-Identifier:     GPL-2.0+
8e595107eSHao Zhang  */
9e595107eSHao Zhang 
10e595107eSHao Zhang #include "board.h"
11e595107eSHao Zhang #include <common.h>
125ec66b14SHao Zhang #include <spl.h>
13e595107eSHao Zhang #include <exports.h>
14e595107eSHao Zhang #include <fdt_support.h>
15e595107eSHao Zhang #include <asm/arch/ddr3.h>
16497e9e03SKhoronzhuk, Ivan #include <asm/arch/psc_defs.h>
17*8626cb80SLokesh Vutla #include <asm/arch/clock.h>
18e595107eSHao Zhang #include <asm/ti-common/ti-aemif.h>
190935cac6SKhoronzhuk, Ivan #include <asm/ti-common/keystone_net.h>
20e595107eSHao Zhang 
21e595107eSHao Zhang DECLARE_GLOBAL_DATA_PTR;
22e595107eSHao Zhang 
23e595107eSHao Zhang static struct aemif_config aemif_configs[] = {
24e595107eSHao Zhang 	{			/* CS0 */
25e595107eSHao Zhang 		.mode		= AEMIF_MODE_NAND,
26e595107eSHao Zhang 		.wr_setup	= 0xf,
27e595107eSHao Zhang 		.wr_strobe	= 0x3f,
28e595107eSHao Zhang 		.wr_hold	= 7,
29e595107eSHao Zhang 		.rd_setup	= 0xf,
30e595107eSHao Zhang 		.rd_strobe	= 0x3f,
31e595107eSHao Zhang 		.rd_hold	= 7,
32e595107eSHao Zhang 		.turn_around	= 3,
33e595107eSHao Zhang 		.width		= AEMIF_WIDTH_8,
34e595107eSHao Zhang 	},
35e595107eSHao Zhang };
36e595107eSHao Zhang 
37e595107eSHao Zhang int dram_init(void)
38e595107eSHao Zhang {
3966c98a0cSVitaly Andrianov 	u32 ddr3_size;
4066c98a0cSVitaly Andrianov 
4166c98a0cSVitaly Andrianov 	ddr3_size = ddr3_init();
42e595107eSHao Zhang 
43e595107eSHao Zhang 	gd->ram_size = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE,
44e595107eSHao Zhang 				    CONFIG_MAX_RAM_BANK_SIZE);
45e595107eSHao Zhang 	aemif_init(ARRAY_SIZE(aemif_configs), aemif_configs);
4666c98a0cSVitaly Andrianov 	ddr3_init_ecc(KS2_DDR3A_EMIF_CTRL_BASE, ddr3_size);
47e595107eSHao Zhang 	return 0;
48e595107eSHao Zhang }
49e595107eSHao Zhang 
50e595107eSHao Zhang int board_init(void)
51e595107eSHao Zhang {
5259d4cd22SNishanth Menon 	gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
53e595107eSHao Zhang 
54e595107eSHao Zhang 	return 0;
55e595107eSHao Zhang }
56e595107eSHao Zhang 
57e595107eSHao Zhang #ifdef CONFIG_DRIVER_TI_KEYSTONE_NET
58e595107eSHao Zhang int get_eth_env_param(char *env_name)
59e595107eSHao Zhang {
60e595107eSHao Zhang 	char *env;
61e595107eSHao Zhang 	int res = -1;
62e595107eSHao Zhang 
63e595107eSHao Zhang 	env = getenv(env_name);
64e595107eSHao Zhang 	if (env)
65e595107eSHao Zhang 		res = simple_strtol(env, NULL, 0);
66e595107eSHao Zhang 
67e595107eSHao Zhang 	return res;
68e595107eSHao Zhang }
69e595107eSHao Zhang 
70e595107eSHao Zhang int board_eth_init(bd_t *bis)
71e595107eSHao Zhang {
72e595107eSHao Zhang 	int j;
73e595107eSHao Zhang 	int res;
74e595107eSHao Zhang 	int port_num;
75e595107eSHao Zhang 	char link_type_name[32];
76e595107eSHao Zhang 
77497e9e03SKhoronzhuk, Ivan 	/* By default, select PA PLL clock as PA clock source */
78497e9e03SKhoronzhuk, Ivan 	if (psc_enable_module(KS2_LPSC_PA))
79497e9e03SKhoronzhuk, Ivan 		return -1;
80497e9e03SKhoronzhuk, Ivan 	if (psc_enable_module(KS2_LPSC_CPGMAC))
81497e9e03SKhoronzhuk, Ivan 		return -1;
82497e9e03SKhoronzhuk, Ivan 	if (psc_enable_module(KS2_LPSC_CRYPTO))
83497e9e03SKhoronzhuk, Ivan 		return -1;
84497e9e03SKhoronzhuk, Ivan 
85*8626cb80SLokesh Vutla 	if (cpu_is_k2e() || cpu_is_k2l())
86*8626cb80SLokesh Vutla 		pll_pa_clk_sel();
87*8626cb80SLokesh Vutla 
88e595107eSHao Zhang 	port_num = get_num_eth_ports();
89e595107eSHao Zhang 
90e595107eSHao Zhang 	for (j = 0; j < port_num; j++) {
91e595107eSHao Zhang 		sprintf(link_type_name, "sgmii%d_link_type", j);
92e595107eSHao Zhang 		res = get_eth_env_param(link_type_name);
93e595107eSHao Zhang 		if (res >= 0)
94e595107eSHao Zhang 			eth_priv_cfg[j].sgmii_link_type = res;
95e595107eSHao Zhang 
96e595107eSHao Zhang 		keystone2_emac_initialize(&eth_priv_cfg[j]);
97e595107eSHao Zhang 	}
98e595107eSHao Zhang 
99e595107eSHao Zhang 	return 0;
100e595107eSHao Zhang }
101e595107eSHao Zhang #endif
102e595107eSHao Zhang 
1035ec66b14SHao Zhang #ifdef CONFIG_SPL_BUILD
1045ec66b14SHao Zhang void spl_board_init(void)
1055ec66b14SHao Zhang {
1065ec66b14SHao Zhang 	spl_init_keystone_plls();
1075ec66b14SHao Zhang 	preloader_console_init();
1085ec66b14SHao Zhang }
1095ec66b14SHao Zhang 
1105ec66b14SHao Zhang u32 spl_boot_device(void)
1115ec66b14SHao Zhang {
1125ec66b14SHao Zhang #if defined(CONFIG_SPL_SPI_LOAD)
1135ec66b14SHao Zhang 	return BOOT_DEVICE_SPI;
1145ec66b14SHao Zhang #else
1155ec66b14SHao Zhang 	puts("Unknown boot device\n");
1165ec66b14SHao Zhang 	hang();
1175ec66b14SHao Zhang #endif
1185ec66b14SHao Zhang }
1195ec66b14SHao Zhang #endif
1205ec66b14SHao Zhang 
121e595107eSHao Zhang #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
122e895a4b0SSimon Glass int ft_board_setup(void *blob, bd_t *bd)
123e595107eSHao Zhang {
124e595107eSHao Zhang 	int lpae;
125e595107eSHao Zhang 	char *env;
126e595107eSHao Zhang 	char *endp;
127e595107eSHao Zhang 	int nbanks;
128e595107eSHao Zhang 	u64 size[2];
129e595107eSHao Zhang 	u64 start[2];
130e595107eSHao Zhang 	int nodeoffset;
131e595107eSHao Zhang 	u32 ddr3a_size;
132e595107eSHao Zhang 	int unitrd_fixup = 0;
133e595107eSHao Zhang 
134e595107eSHao Zhang 	env = getenv("mem_lpae");
135e595107eSHao Zhang 	lpae = env && simple_strtol(env, NULL, 0);
136e595107eSHao Zhang 	env = getenv("uinitrd_fixup");
137e595107eSHao Zhang 	unitrd_fixup = env && simple_strtol(env, NULL, 0);
138e595107eSHao Zhang 
139e595107eSHao Zhang 	ddr3a_size = 0;
140e595107eSHao Zhang 	if (lpae) {
141e595107eSHao Zhang 		env = getenv("ddr3a_size");
142e595107eSHao Zhang 		if (env)
143e595107eSHao Zhang 			ddr3a_size = simple_strtol(env, NULL, 10);
144e595107eSHao Zhang 		if ((ddr3a_size != 8) && (ddr3a_size != 4))
145e595107eSHao Zhang 			ddr3a_size = 0;
146e595107eSHao Zhang 	}
147e595107eSHao Zhang 
148e595107eSHao Zhang 	nbanks = 1;
149e595107eSHao Zhang 	start[0] = bd->bi_dram[0].start;
150e595107eSHao Zhang 	size[0]  = bd->bi_dram[0].size;
151e595107eSHao Zhang 
152e595107eSHao Zhang 	/* adjust memory start address for LPAE */
153e595107eSHao Zhang 	if (lpae) {
154e595107eSHao Zhang 		start[0] -= CONFIG_SYS_SDRAM_BASE;
155e595107eSHao Zhang 		start[0] += CONFIG_SYS_LPAE_SDRAM_BASE;
156e595107eSHao Zhang 	}
157e595107eSHao Zhang 
158e595107eSHao Zhang 	if ((size[0] == 0x80000000) && (ddr3a_size != 0)) {
159e595107eSHao Zhang 		size[1] = ((u64)ddr3a_size - 2) << 30;
160e595107eSHao Zhang 		start[1] = 0x880000000;
161e595107eSHao Zhang 		nbanks++;
162e595107eSHao Zhang 	}
163e595107eSHao Zhang 
164e595107eSHao Zhang 	/* reserve memory at start of bank */
16530491fc8SKhoronzhuk, Ivan 	env = getenv("mem_reserve_head");
166e595107eSHao Zhang 	if (env) {
167e595107eSHao Zhang 		start[0] += ustrtoul(env, &endp, 0);
168e595107eSHao Zhang 		size[0] -= ustrtoul(env, &endp, 0);
169e595107eSHao Zhang 	}
170e595107eSHao Zhang 
17130491fc8SKhoronzhuk, Ivan 	env = getenv("mem_reserve");
172e595107eSHao Zhang 	if (env)
173e595107eSHao Zhang 		size[0] -= ustrtoul(env, &endp, 0);
174e595107eSHao Zhang 
175e595107eSHao Zhang 	fdt_fixup_memory_banks(blob, start, size, nbanks);
176e595107eSHao Zhang 
177e595107eSHao Zhang 	/* Fix up the initrd */
178e595107eSHao Zhang 	if (lpae && unitrd_fixup) {
179e595107eSHao Zhang 		int err;
180e595107eSHao Zhang 		u32 *prop1, *prop2;
181e595107eSHao Zhang 		u64 initrd_start, initrd_end;
182e595107eSHao Zhang 
183e595107eSHao Zhang 		nodeoffset = fdt_path_offset(blob, "/chosen");
184e595107eSHao Zhang 		if (nodeoffset >= 0) {
185e595107eSHao Zhang 			prop1 = (u32 *)fdt_getprop(blob, nodeoffset,
186e595107eSHao Zhang 					    "linux,initrd-start", NULL);
187e595107eSHao Zhang 			prop2 = (u32 *)fdt_getprop(blob, nodeoffset,
188e595107eSHao Zhang 					    "linux,initrd-end", NULL);
189e595107eSHao Zhang 			if (prop1 && prop2) {
190e595107eSHao Zhang 				initrd_start = __be32_to_cpu(*prop1);
191e595107eSHao Zhang 				initrd_start -= CONFIG_SYS_SDRAM_BASE;
192e595107eSHao Zhang 				initrd_start += CONFIG_SYS_LPAE_SDRAM_BASE;
193e595107eSHao Zhang 				initrd_start = __cpu_to_be64(initrd_start);
194e595107eSHao Zhang 				initrd_end = __be32_to_cpu(*prop2);
195e595107eSHao Zhang 				initrd_end -= CONFIG_SYS_SDRAM_BASE;
196e595107eSHao Zhang 				initrd_end += CONFIG_SYS_LPAE_SDRAM_BASE;
197e595107eSHao Zhang 				initrd_end = __cpu_to_be64(initrd_end);
198e595107eSHao Zhang 
199e595107eSHao Zhang 				err = fdt_delprop(blob, nodeoffset,
200e595107eSHao Zhang 						  "linux,initrd-start");
201e595107eSHao Zhang 				if (err < 0)
202e595107eSHao Zhang 					puts("error deleting initrd-start\n");
203e595107eSHao Zhang 
204e595107eSHao Zhang 				err = fdt_delprop(blob, nodeoffset,
205e595107eSHao Zhang 						  "linux,initrd-end");
206e595107eSHao Zhang 				if (err < 0)
207e595107eSHao Zhang 					puts("error deleting initrd-end\n");
208e595107eSHao Zhang 
209e595107eSHao Zhang 				err = fdt_setprop(blob, nodeoffset,
210e595107eSHao Zhang 						  "linux,initrd-start",
211e595107eSHao Zhang 						  &initrd_start,
212e595107eSHao Zhang 						  sizeof(initrd_start));
213e595107eSHao Zhang 				if (err < 0)
214e595107eSHao Zhang 					puts("error adding initrd-start\n");
215e595107eSHao Zhang 
216e595107eSHao Zhang 				err = fdt_setprop(blob, nodeoffset,
217e595107eSHao Zhang 						  "linux,initrd-end",
218e595107eSHao Zhang 						  &initrd_end,
219e595107eSHao Zhang 						  sizeof(initrd_end));
220e595107eSHao Zhang 				if (err < 0)
221e595107eSHao Zhang 					puts("error adding linux,initrd-end\n");
222e595107eSHao Zhang 			}
223e595107eSHao Zhang 		}
224e595107eSHao Zhang 	}
225e895a4b0SSimon Glass 
226e895a4b0SSimon Glass 	return 0;
227e595107eSHao Zhang }
228e595107eSHao Zhang 
229e595107eSHao Zhang void ft_board_setup_ex(void *blob, bd_t *bd)
230e595107eSHao Zhang {
231e595107eSHao Zhang 	int lpae;
232e595107eSHao Zhang 	u64 size;
233e595107eSHao Zhang 	char *env;
234e595107eSHao Zhang 	u64 *reserve_start;
235e595107eSHao Zhang 
236e595107eSHao Zhang 	env = getenv("mem_lpae");
237e595107eSHao Zhang 	lpae = env && simple_strtol(env, NULL, 0);
238e595107eSHao Zhang 
239e595107eSHao Zhang 	if (lpae) {
240e595107eSHao Zhang 		/*
241e595107eSHao Zhang 		 * the initrd and other reserved memory areas are
242e595107eSHao Zhang 		 * embedded in in the DTB itslef. fix up these addresses
243e595107eSHao Zhang 		 * to 36 bit format
244e595107eSHao Zhang 		 */
245e595107eSHao Zhang 		reserve_start = (u64 *)((char *)blob +
246e595107eSHao Zhang 				       fdt_off_mem_rsvmap(blob));
247e595107eSHao Zhang 		while (1) {
248e595107eSHao Zhang 			*reserve_start = __cpu_to_be64(*reserve_start);
249e595107eSHao Zhang 			size = __cpu_to_be64(*(reserve_start + 1));
250e595107eSHao Zhang 			if (size) {
251e595107eSHao Zhang 				*reserve_start -= CONFIG_SYS_SDRAM_BASE;
252e595107eSHao Zhang 				*reserve_start +=
253e595107eSHao Zhang 					CONFIG_SYS_LPAE_SDRAM_BASE;
254e595107eSHao Zhang 				*reserve_start =
255e595107eSHao Zhang 					__cpu_to_be64(*reserve_start);
256e595107eSHao Zhang 			} else {
257e595107eSHao Zhang 				break;
258e595107eSHao Zhang 			}
259e595107eSHao Zhang 			reserve_start += 2;
260e595107eSHao Zhang 		}
261e595107eSHao Zhang 	}
26289f44bb0SVitaly Andrianov 
26389f44bb0SVitaly Andrianov 	ddr3_check_ecc_int(KS2_DDR3A_EMIF_CTRL_BASE);
264e595107eSHao Zhang }
265e595107eSHao Zhang #endif
266