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> 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 22e595107eSHao Zhang static struct aemif_config aemif_configs[] = { 23e595107eSHao Zhang { /* CS0 */ 24e595107eSHao Zhang .mode = AEMIF_MODE_NAND, 25e595107eSHao Zhang .wr_setup = 0xf, 26e595107eSHao Zhang .wr_strobe = 0x3f, 27e595107eSHao Zhang .wr_hold = 7, 28e595107eSHao Zhang .rd_setup = 0xf, 29e595107eSHao Zhang .rd_strobe = 0x3f, 30e595107eSHao Zhang .rd_hold = 7, 31e595107eSHao Zhang .turn_around = 3, 32e595107eSHao Zhang .width = AEMIF_WIDTH_8, 33e595107eSHao Zhang }, 34e595107eSHao Zhang }; 35e595107eSHao Zhang 36e595107eSHao Zhang int dram_init(void) 37e595107eSHao Zhang { 38e595107eSHao Zhang ddr3_init(); 39e595107eSHao Zhang 40e595107eSHao Zhang gd->ram_size = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 41e595107eSHao Zhang CONFIG_MAX_RAM_BANK_SIZE); 42e595107eSHao Zhang aemif_init(ARRAY_SIZE(aemif_configs), aemif_configs); 4389f44bb0SVitaly Andrianov ddr3_init_ecc(KS2_DDR3A_EMIF_CTRL_BASE); 44e595107eSHao Zhang return 0; 45e595107eSHao Zhang } 46e595107eSHao Zhang 47e595107eSHao Zhang int board_init(void) 48e595107eSHao Zhang { 49e595107eSHao Zhang gd->bd->bi_boot_params = CONFIG_LINUX_BOOT_PARAM_ADDR; 50e595107eSHao Zhang 51e595107eSHao Zhang return 0; 52e595107eSHao Zhang } 53e595107eSHao Zhang 54e595107eSHao Zhang #ifdef CONFIG_DRIVER_TI_KEYSTONE_NET 55e595107eSHao Zhang int get_eth_env_param(char *env_name) 56e595107eSHao Zhang { 57e595107eSHao Zhang char *env; 58e595107eSHao Zhang int res = -1; 59e595107eSHao Zhang 60e595107eSHao Zhang env = getenv(env_name); 61e595107eSHao Zhang if (env) 62e595107eSHao Zhang res = simple_strtol(env, NULL, 0); 63e595107eSHao Zhang 64e595107eSHao Zhang return res; 65e595107eSHao Zhang } 66e595107eSHao Zhang 67e595107eSHao Zhang int board_eth_init(bd_t *bis) 68e595107eSHao Zhang { 69e595107eSHao Zhang int j; 70e595107eSHao Zhang int res; 71e595107eSHao Zhang int port_num; 72e595107eSHao Zhang char link_type_name[32]; 73e595107eSHao Zhang 74497e9e03SKhoronzhuk, Ivan /* By default, select PA PLL clock as PA clock source */ 75497e9e03SKhoronzhuk, Ivan if (psc_enable_module(KS2_LPSC_PA)) 76497e9e03SKhoronzhuk, Ivan return -1; 77497e9e03SKhoronzhuk, Ivan if (psc_enable_module(KS2_LPSC_CPGMAC)) 78497e9e03SKhoronzhuk, Ivan return -1; 79497e9e03SKhoronzhuk, Ivan if (psc_enable_module(KS2_LPSC_CRYPTO)) 80497e9e03SKhoronzhuk, Ivan return -1; 8169a3b811SKhoronzhuk, Ivan pass_pll_pa_clk_enable(); 82497e9e03SKhoronzhuk, Ivan 83e595107eSHao Zhang port_num = get_num_eth_ports(); 84e595107eSHao Zhang 85e595107eSHao Zhang for (j = 0; j < port_num; j++) { 86e595107eSHao Zhang sprintf(link_type_name, "sgmii%d_link_type", j); 87e595107eSHao Zhang res = get_eth_env_param(link_type_name); 88e595107eSHao Zhang if (res >= 0) 89e595107eSHao Zhang eth_priv_cfg[j].sgmii_link_type = res; 90e595107eSHao Zhang 91e595107eSHao Zhang keystone2_emac_initialize(ð_priv_cfg[j]); 92e595107eSHao Zhang } 93e595107eSHao Zhang 94e595107eSHao Zhang return 0; 95e595107eSHao Zhang } 96e595107eSHao Zhang #endif 97e595107eSHao Zhang 985ec66b14SHao Zhang #ifdef CONFIG_SPL_BUILD 995ec66b14SHao Zhang void spl_board_init(void) 1005ec66b14SHao Zhang { 1015ec66b14SHao Zhang spl_init_keystone_plls(); 1025ec66b14SHao Zhang preloader_console_init(); 1035ec66b14SHao Zhang } 1045ec66b14SHao Zhang 1055ec66b14SHao Zhang u32 spl_boot_device(void) 1065ec66b14SHao Zhang { 1075ec66b14SHao Zhang #if defined(CONFIG_SPL_SPI_LOAD) 1085ec66b14SHao Zhang return BOOT_DEVICE_SPI; 1095ec66b14SHao Zhang #else 1105ec66b14SHao Zhang puts("Unknown boot device\n"); 1115ec66b14SHao Zhang hang(); 1125ec66b14SHao Zhang #endif 1135ec66b14SHao Zhang } 1145ec66b14SHao Zhang #endif 1155ec66b14SHao Zhang 116e595107eSHao Zhang #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP) 117*e895a4b0SSimon Glass int ft_board_setup(void *blob, bd_t *bd) 118e595107eSHao Zhang { 119e595107eSHao Zhang int lpae; 120e595107eSHao Zhang char *env; 121e595107eSHao Zhang char *endp; 122e595107eSHao Zhang int nbanks; 123e595107eSHao Zhang u64 size[2]; 124e595107eSHao Zhang u64 start[2]; 125e595107eSHao Zhang int nodeoffset; 126e595107eSHao Zhang u32 ddr3a_size; 127e595107eSHao Zhang int unitrd_fixup = 0; 128e595107eSHao Zhang 129e595107eSHao Zhang env = getenv("mem_lpae"); 130e595107eSHao Zhang lpae = env && simple_strtol(env, NULL, 0); 131e595107eSHao Zhang env = getenv("uinitrd_fixup"); 132e595107eSHao Zhang unitrd_fixup = env && simple_strtol(env, NULL, 0); 133e595107eSHao Zhang 134e595107eSHao Zhang ddr3a_size = 0; 135e595107eSHao Zhang if (lpae) { 136e595107eSHao Zhang env = getenv("ddr3a_size"); 137e595107eSHao Zhang if (env) 138e595107eSHao Zhang ddr3a_size = simple_strtol(env, NULL, 10); 139e595107eSHao Zhang if ((ddr3a_size != 8) && (ddr3a_size != 4)) 140e595107eSHao Zhang ddr3a_size = 0; 141e595107eSHao Zhang } 142e595107eSHao Zhang 143e595107eSHao Zhang nbanks = 1; 144e595107eSHao Zhang start[0] = bd->bi_dram[0].start; 145e595107eSHao Zhang size[0] = bd->bi_dram[0].size; 146e595107eSHao Zhang 147e595107eSHao Zhang /* adjust memory start address for LPAE */ 148e595107eSHao Zhang if (lpae) { 149e595107eSHao Zhang start[0] -= CONFIG_SYS_SDRAM_BASE; 150e595107eSHao Zhang start[0] += CONFIG_SYS_LPAE_SDRAM_BASE; 151e595107eSHao Zhang } 152e595107eSHao Zhang 153e595107eSHao Zhang if ((size[0] == 0x80000000) && (ddr3a_size != 0)) { 154e595107eSHao Zhang size[1] = ((u64)ddr3a_size - 2) << 30; 155e595107eSHao Zhang start[1] = 0x880000000; 156e595107eSHao Zhang nbanks++; 157e595107eSHao Zhang } 158e595107eSHao Zhang 159e595107eSHao Zhang /* reserve memory at start of bank */ 16030491fc8SKhoronzhuk, Ivan env = getenv("mem_reserve_head"); 161e595107eSHao Zhang if (env) { 162e595107eSHao Zhang start[0] += ustrtoul(env, &endp, 0); 163e595107eSHao Zhang size[0] -= ustrtoul(env, &endp, 0); 164e595107eSHao Zhang } 165e595107eSHao Zhang 16630491fc8SKhoronzhuk, Ivan env = getenv("mem_reserve"); 167e595107eSHao Zhang if (env) 168e595107eSHao Zhang size[0] -= ustrtoul(env, &endp, 0); 169e595107eSHao Zhang 170e595107eSHao Zhang fdt_fixup_memory_banks(blob, start, size, nbanks); 171e595107eSHao Zhang 172e595107eSHao Zhang /* Fix up the initrd */ 173e595107eSHao Zhang if (lpae && unitrd_fixup) { 174e595107eSHao Zhang int err; 175e595107eSHao Zhang u32 *prop1, *prop2; 176e595107eSHao Zhang u64 initrd_start, initrd_end; 177e595107eSHao Zhang 178e595107eSHao Zhang nodeoffset = fdt_path_offset(blob, "/chosen"); 179e595107eSHao Zhang if (nodeoffset >= 0) { 180e595107eSHao Zhang prop1 = (u32 *)fdt_getprop(blob, nodeoffset, 181e595107eSHao Zhang "linux,initrd-start", NULL); 182e595107eSHao Zhang prop2 = (u32 *)fdt_getprop(blob, nodeoffset, 183e595107eSHao Zhang "linux,initrd-end", NULL); 184e595107eSHao Zhang if (prop1 && prop2) { 185e595107eSHao Zhang initrd_start = __be32_to_cpu(*prop1); 186e595107eSHao Zhang initrd_start -= CONFIG_SYS_SDRAM_BASE; 187e595107eSHao Zhang initrd_start += CONFIG_SYS_LPAE_SDRAM_BASE; 188e595107eSHao Zhang initrd_start = __cpu_to_be64(initrd_start); 189e595107eSHao Zhang initrd_end = __be32_to_cpu(*prop2); 190e595107eSHao Zhang initrd_end -= CONFIG_SYS_SDRAM_BASE; 191e595107eSHao Zhang initrd_end += CONFIG_SYS_LPAE_SDRAM_BASE; 192e595107eSHao Zhang initrd_end = __cpu_to_be64(initrd_end); 193e595107eSHao Zhang 194e595107eSHao Zhang err = fdt_delprop(blob, nodeoffset, 195e595107eSHao Zhang "linux,initrd-start"); 196e595107eSHao Zhang if (err < 0) 197e595107eSHao Zhang puts("error deleting initrd-start\n"); 198e595107eSHao Zhang 199e595107eSHao Zhang err = fdt_delprop(blob, nodeoffset, 200e595107eSHao Zhang "linux,initrd-end"); 201e595107eSHao Zhang if (err < 0) 202e595107eSHao Zhang puts("error deleting initrd-end\n"); 203e595107eSHao Zhang 204e595107eSHao Zhang err = fdt_setprop(blob, nodeoffset, 205e595107eSHao Zhang "linux,initrd-start", 206e595107eSHao Zhang &initrd_start, 207e595107eSHao Zhang sizeof(initrd_start)); 208e595107eSHao Zhang if (err < 0) 209e595107eSHao Zhang puts("error adding initrd-start\n"); 210e595107eSHao Zhang 211e595107eSHao Zhang err = fdt_setprop(blob, nodeoffset, 212e595107eSHao Zhang "linux,initrd-end", 213e595107eSHao Zhang &initrd_end, 214e595107eSHao Zhang sizeof(initrd_end)); 215e595107eSHao Zhang if (err < 0) 216e595107eSHao Zhang puts("error adding linux,initrd-end\n"); 217e595107eSHao Zhang } 218e595107eSHao Zhang } 219e595107eSHao Zhang } 220*e895a4b0SSimon Glass 221*e895a4b0SSimon Glass return 0; 222e595107eSHao Zhang } 223e595107eSHao Zhang 224e595107eSHao Zhang void ft_board_setup_ex(void *blob, bd_t *bd) 225e595107eSHao Zhang { 226e595107eSHao Zhang int lpae; 227e595107eSHao Zhang u64 size; 228e595107eSHao Zhang char *env; 229e595107eSHao Zhang u64 *reserve_start; 230e595107eSHao Zhang 231e595107eSHao Zhang env = getenv("mem_lpae"); 232e595107eSHao Zhang lpae = env && simple_strtol(env, NULL, 0); 233e595107eSHao Zhang 234e595107eSHao Zhang if (lpae) { 235e595107eSHao Zhang /* 236e595107eSHao Zhang * the initrd and other reserved memory areas are 237e595107eSHao Zhang * embedded in in the DTB itslef. fix up these addresses 238e595107eSHao Zhang * to 36 bit format 239e595107eSHao Zhang */ 240e595107eSHao Zhang reserve_start = (u64 *)((char *)blob + 241e595107eSHao Zhang fdt_off_mem_rsvmap(blob)); 242e595107eSHao Zhang while (1) { 243e595107eSHao Zhang *reserve_start = __cpu_to_be64(*reserve_start); 244e595107eSHao Zhang size = __cpu_to_be64(*(reserve_start + 1)); 245e595107eSHao Zhang if (size) { 246e595107eSHao Zhang *reserve_start -= CONFIG_SYS_SDRAM_BASE; 247e595107eSHao Zhang *reserve_start += 248e595107eSHao Zhang CONFIG_SYS_LPAE_SDRAM_BASE; 249e595107eSHao Zhang *reserve_start = 250e595107eSHao Zhang __cpu_to_be64(*reserve_start); 251e595107eSHao Zhang } else { 252e595107eSHao Zhang break; 253e595107eSHao Zhang } 254e595107eSHao Zhang reserve_start += 2; 255e595107eSHao Zhang } 256e595107eSHao Zhang } 25789f44bb0SVitaly Andrianov 25889f44bb0SVitaly Andrianov ddr3_check_ecc_int(KS2_DDR3A_EMIF_CTRL_BASE); 259e595107eSHao Zhang } 260e595107eSHao Zhang #endif 261