xref: /openbmc/u-boot/board/ti/ks2_evm/board.c (revision 522e0354)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2e595107eSHao Zhang /*
3e595107eSHao Zhang  * Keystone : Board initialization
4e595107eSHao Zhang  *
5e595107eSHao Zhang  * (C) Copyright 2014
6e595107eSHao Zhang  *     Texas Instruments Incorporated, <www.ti.com>
7e595107eSHao Zhang  */
8e595107eSHao Zhang 
9e595107eSHao Zhang #include <common.h>
10b8dafa22SVitaly Andrianov #include "board.h"
115ec66b14SHao Zhang #include <spl.h>
12e595107eSHao Zhang #include <exports.h>
13e595107eSHao Zhang #include <fdt_support.h>
14e595107eSHao Zhang #include <asm/arch/ddr3.h>
15497e9e03SKhoronzhuk, Ivan #include <asm/arch/psc_defs.h>
168626cb80SLokesh Vutla #include <asm/arch/clock.h>
17e595107eSHao Zhang #include <asm/ti-common/ti-aemif.h>
180935cac6SKhoronzhuk, Ivan #include <asm/ti-common/keystone_net.h>
19e595107eSHao Zhang 
20e595107eSHao Zhang DECLARE_GLOBAL_DATA_PTR;
21e595107eSHao Zhang 
228f695232SLokesh Vutla #if defined(CONFIG_TI_AEMIF)
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 };
368f695232SLokesh Vutla #endif
37e595107eSHao Zhang 
dram_init(void)38e595107eSHao Zhang int dram_init(void)
39e595107eSHao Zhang {
4066c98a0cSVitaly Andrianov 	u32 ddr3_size;
4166c98a0cSVitaly Andrianov 
4266c98a0cSVitaly Andrianov 	ddr3_size = ddr3_init();
43e595107eSHao Zhang 
44e595107eSHao Zhang 	gd->ram_size = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE,
45e595107eSHao Zhang 				    CONFIG_MAX_RAM_BANK_SIZE);
468f695232SLokesh Vutla #if defined(CONFIG_TI_AEMIF)
47e66a5da3SCooper Jr., Franklin 	if (!board_is_k2g_ice())
48e595107eSHao Zhang 		aemif_init(ARRAY_SIZE(aemif_configs), aemif_configs);
498f695232SLokesh Vutla #endif
508f695232SLokesh Vutla 
51e66a5da3SCooper Jr., Franklin 	if (!board_is_k2g_ice()) {
52235dd6e8SVitaly Andrianov 		if (ddr3_size)
5366c98a0cSVitaly Andrianov 			ddr3_init_ecc(KS2_DDR3A_EMIF_CTRL_BASE, ddr3_size);
54e92a6b2eSLokesh Vutla 		else
55e66a5da3SCooper Jr., Franklin 			ddr3_init_ecc(KS2_DDR3A_EMIF_CTRL_BASE,
56e66a5da3SCooper Jr., Franklin 				      gd->ram_size >> 30);
57e66a5da3SCooper Jr., Franklin 	}
58e92a6b2eSLokesh Vutla 
59e595107eSHao Zhang 	return 0;
60e595107eSHao Zhang }
61e595107eSHao Zhang 
spl_get_load_buffer(ssize_t offset,size_t size)623b074fb2SKeerthy struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
633b074fb2SKeerthy {
643b074fb2SKeerthy 	return (struct image_header *)(CONFIG_SYS_TEXT_BASE);
653b074fb2SKeerthy }
663b074fb2SKeerthy 
board_init(void)67e595107eSHao Zhang int board_init(void)
68e595107eSHao Zhang {
69*c9d52206SJean-Jacques Hiblot #if CONFIG_IS_ENABLED(DM_USB)
70*c9d52206SJean-Jacques Hiblot 	int rc = psc_enable_module(KS2_LPSC_USB);
71*c9d52206SJean-Jacques Hiblot 
72*c9d52206SJean-Jacques Hiblot 	if (rc)
73*c9d52206SJean-Jacques Hiblot 		puts("Cannot enable USB0 module");
74*c9d52206SJean-Jacques Hiblot #ifdef KS2_LPSC_USB_1
75*c9d52206SJean-Jacques Hiblot 	rc = psc_enable_module(KS2_LPSC_USB_1);
76*c9d52206SJean-Jacques Hiblot 	if (rc)
77*c9d52206SJean-Jacques Hiblot 		puts("Cannot enable USB1 module");
78*c9d52206SJean-Jacques Hiblot #endif
79*c9d52206SJean-Jacques Hiblot #endif
80*c9d52206SJean-Jacques Hiblot 
8159d4cd22SNishanth Menon 	gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
82e595107eSHao Zhang 
83e595107eSHao Zhang 	return 0;
84e595107eSHao Zhang }
85e595107eSHao Zhang 
865ec66b14SHao Zhang #ifdef CONFIG_SPL_BUILD
spl_board_init(void)875ec66b14SHao Zhang void spl_board_init(void)
885ec66b14SHao Zhang {
895ec66b14SHao Zhang 	spl_init_keystone_plls();
905ec66b14SHao Zhang 	preloader_console_init();
915ec66b14SHao Zhang }
925ec66b14SHao Zhang 
spl_boot_device(void)935ec66b14SHao Zhang u32 spl_boot_device(void)
945ec66b14SHao Zhang {
955ec66b14SHao Zhang #if defined(CONFIG_SPL_SPI_LOAD)
965ec66b14SHao Zhang 	return BOOT_DEVICE_SPI;
975ec66b14SHao Zhang #else
985ec66b14SHao Zhang 	puts("Unknown boot device\n");
995ec66b14SHao Zhang 	hang();
1005ec66b14SHao Zhang #endif
1015ec66b14SHao Zhang }
1025ec66b14SHao Zhang #endif
1035ec66b14SHao Zhang 
1047ffe3cd6SRobert P. J. Day #ifdef CONFIG_OF_BOARD_SETUP
ft_board_setup(void * blob,bd_t * bd)105e895a4b0SSimon Glass int ft_board_setup(void *blob, bd_t *bd)
106e595107eSHao Zhang {
107e595107eSHao Zhang 	int lpae;
108e595107eSHao Zhang 	char *env;
109e595107eSHao Zhang 	char *endp;
110e595107eSHao Zhang 	int nbanks;
111e595107eSHao Zhang 	u64 size[2];
112e595107eSHao Zhang 	u64 start[2];
113e595107eSHao Zhang 	u32 ddr3a_size;
114e595107eSHao Zhang 
11500caae6dSSimon Glass 	env = env_get("mem_lpae");
116e595107eSHao Zhang 	lpae = env && simple_strtol(env, NULL, 0);
117e595107eSHao Zhang 
118e595107eSHao Zhang 	ddr3a_size = 0;
119e595107eSHao Zhang 	if (lpae) {
1208efc2437SVitaly Andrianov 		ddr3a_size = ddr3_get_size();
121e595107eSHao Zhang 		if ((ddr3a_size != 8) && (ddr3a_size != 4))
122e595107eSHao Zhang 			ddr3a_size = 0;
123e595107eSHao Zhang 	}
124e595107eSHao Zhang 
125e595107eSHao Zhang 	nbanks = 1;
126e595107eSHao Zhang 	start[0] = bd->bi_dram[0].start;
127e595107eSHao Zhang 	size[0]  = bd->bi_dram[0].size;
128e595107eSHao Zhang 
129e595107eSHao Zhang 	/* adjust memory start address for LPAE */
130e595107eSHao Zhang 	if (lpae) {
131e595107eSHao Zhang 		start[0] -= CONFIG_SYS_SDRAM_BASE;
132e595107eSHao Zhang 		start[0] += CONFIG_SYS_LPAE_SDRAM_BASE;
133e595107eSHao Zhang 	}
134e595107eSHao Zhang 
135e595107eSHao Zhang 	if ((size[0] == 0x80000000) && (ddr3a_size != 0)) {
136e595107eSHao Zhang 		size[1] = ((u64)ddr3a_size - 2) << 30;
137e595107eSHao Zhang 		start[1] = 0x880000000;
138e595107eSHao Zhang 		nbanks++;
139e595107eSHao Zhang 	}
140e595107eSHao Zhang 
141e595107eSHao Zhang 	/* reserve memory at start of bank */
14200caae6dSSimon Glass 	env = env_get("mem_reserve_head");
143e595107eSHao Zhang 	if (env) {
144e595107eSHao Zhang 		start[0] += ustrtoul(env, &endp, 0);
145e595107eSHao Zhang 		size[0] -= ustrtoul(env, &endp, 0);
146e595107eSHao Zhang 	}
147e595107eSHao Zhang 
14800caae6dSSimon Glass 	env = env_get("mem_reserve");
149e595107eSHao Zhang 	if (env)
150e595107eSHao Zhang 		size[0] -= ustrtoul(env, &endp, 0);
151e595107eSHao Zhang 
152e595107eSHao Zhang 	fdt_fixup_memory_banks(blob, start, size, nbanks);
153e595107eSHao Zhang 
154442faf61SNicholas Faustini 	return 0;
155442faf61SNicholas Faustini }
156442faf61SNicholas Faustini 
ft_board_setup_ex(void * blob,bd_t * bd)157442faf61SNicholas Faustini void ft_board_setup_ex(void *blob, bd_t *bd)
158442faf61SNicholas Faustini {
159442faf61SNicholas Faustini 	int lpae;
160442faf61SNicholas Faustini 	u64 size;
161442faf61SNicholas Faustini 	char *env;
162442faf61SNicholas Faustini 	u64 *reserve_start;
163442faf61SNicholas Faustini 	int unitrd_fixup = 0;
164442faf61SNicholas Faustini 
165442faf61SNicholas Faustini 	env = env_get("mem_lpae");
166442faf61SNicholas Faustini 	lpae = env && simple_strtol(env, NULL, 0);
167442faf61SNicholas Faustini 	env = env_get("uinitrd_fixup");
168442faf61SNicholas Faustini 	unitrd_fixup = env && simple_strtol(env, NULL, 0);
169442faf61SNicholas Faustini 
170e595107eSHao Zhang 	/* Fix up the initrd */
171e595107eSHao Zhang 	if (lpae && unitrd_fixup) {
172442faf61SNicholas Faustini 		int nodeoffset;
173e595107eSHao Zhang 		int err;
174442faf61SNicholas Faustini 		u64 *prop1, *prop2;
175e595107eSHao Zhang 		u64 initrd_start, initrd_end;
176e595107eSHao Zhang 
177e595107eSHao Zhang 		nodeoffset = fdt_path_offset(blob, "/chosen");
178e595107eSHao Zhang 		if (nodeoffset >= 0) {
179442faf61SNicholas Faustini 			prop1 = (u64 *)fdt_getprop(blob, nodeoffset,
180e595107eSHao Zhang 					    "linux,initrd-start", NULL);
181442faf61SNicholas Faustini 			prop2 = (u64 *)fdt_getprop(blob, nodeoffset,
182e595107eSHao Zhang 					    "linux,initrd-end", NULL);
183e595107eSHao Zhang 			if (prop1 && prop2) {
184442faf61SNicholas Faustini 				initrd_start = __be64_to_cpu(*prop1);
185e595107eSHao Zhang 				initrd_start -= CONFIG_SYS_SDRAM_BASE;
186e595107eSHao Zhang 				initrd_start += CONFIG_SYS_LPAE_SDRAM_BASE;
187e595107eSHao Zhang 				initrd_start = __cpu_to_be64(initrd_start);
188442faf61SNicholas Faustini 				initrd_end = __be64_to_cpu(*prop2);
189e595107eSHao Zhang 				initrd_end -= CONFIG_SYS_SDRAM_BASE;
190e595107eSHao Zhang 				initrd_end += CONFIG_SYS_LPAE_SDRAM_BASE;
191e595107eSHao Zhang 				initrd_end = __cpu_to_be64(initrd_end);
192e595107eSHao Zhang 
193e595107eSHao Zhang 				err = fdt_delprop(blob, nodeoffset,
194e595107eSHao Zhang 						  "linux,initrd-start");
195e595107eSHao Zhang 				if (err < 0)
196e595107eSHao Zhang 					puts("error deleting initrd-start\n");
197e595107eSHao Zhang 
198e595107eSHao Zhang 				err = fdt_delprop(blob, nodeoffset,
199e595107eSHao Zhang 						  "linux,initrd-end");
200e595107eSHao Zhang 				if (err < 0)
201e595107eSHao Zhang 					puts("error deleting initrd-end\n");
202e595107eSHao Zhang 
203e595107eSHao Zhang 				err = fdt_setprop(blob, nodeoffset,
204e595107eSHao Zhang 						  "linux,initrd-start",
205e595107eSHao Zhang 						  &initrd_start,
206e595107eSHao Zhang 						  sizeof(initrd_start));
207e595107eSHao Zhang 				if (err < 0)
208e595107eSHao Zhang 					puts("error adding initrd-start\n");
209e595107eSHao Zhang 
210e595107eSHao Zhang 				err = fdt_setprop(blob, nodeoffset,
211e595107eSHao Zhang 						  "linux,initrd-end",
212e595107eSHao Zhang 						  &initrd_end,
213e595107eSHao Zhang 						  sizeof(initrd_end));
214e595107eSHao Zhang 				if (err < 0)
215e595107eSHao Zhang 					puts("error adding linux,initrd-end\n");
216e595107eSHao Zhang 			}
217e595107eSHao Zhang 		}
218e595107eSHao Zhang 	}
219e895a4b0SSimon Glass 
220e595107eSHao Zhang 	if (lpae) {
221e595107eSHao Zhang 		/*
222e595107eSHao Zhang 		 * the initrd and other reserved memory areas are
223e595107eSHao Zhang 		 * embedded in in the DTB itslef. fix up these addresses
224e595107eSHao Zhang 		 * to 36 bit format
225e595107eSHao Zhang 		 */
226e595107eSHao Zhang 		reserve_start = (u64 *)((char *)blob +
227e595107eSHao Zhang 				       fdt_off_mem_rsvmap(blob));
228e595107eSHao Zhang 		while (1) {
229e595107eSHao Zhang 			*reserve_start = __cpu_to_be64(*reserve_start);
230e595107eSHao Zhang 			size = __cpu_to_be64(*(reserve_start + 1));
231e595107eSHao Zhang 			if (size) {
232e595107eSHao Zhang 				*reserve_start -= CONFIG_SYS_SDRAM_BASE;
233e595107eSHao Zhang 				*reserve_start +=
234e595107eSHao Zhang 					CONFIG_SYS_LPAE_SDRAM_BASE;
235e595107eSHao Zhang 				*reserve_start =
236e595107eSHao Zhang 					__cpu_to_be64(*reserve_start);
237e595107eSHao Zhang 			} else {
238e595107eSHao Zhang 				break;
239e595107eSHao Zhang 			}
240e595107eSHao Zhang 			reserve_start += 2;
241e595107eSHao Zhang 		}
242e595107eSHao Zhang 	}
24389f44bb0SVitaly Andrianov 
24489f44bb0SVitaly Andrianov 	ddr3_check_ecc_int(KS2_DDR3A_EMIF_CTRL_BASE);
245e595107eSHao Zhang }
2467ffe3cd6SRobert P. J. Day #endif /* CONFIG_OF_BOARD_SETUP */
2475f48da9aSCooper Jr., Franklin 
2485f48da9aSCooper Jr., Franklin #if defined(CONFIG_DTB_RESELECT)
embedded_dtb_select(void)2495f48da9aSCooper Jr., Franklin int __weak embedded_dtb_select(void)
2505f48da9aSCooper Jr., Franklin {
2515f48da9aSCooper Jr., Franklin 	return 0;
2525f48da9aSCooper Jr., Franklin }
2535f48da9aSCooper Jr., Franklin #endif
254