blob: 4e53fee5d23afc8b6c5aee68ff466ebe68e84d80 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rick Chen36cb27c2017-12-26 13:55:53 +08002/*
3 * Copyright (C) 2017 Andes Technology Corporation
4 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
Rick Chen36cb27c2017-12-26 13:55:53 +08005 */
6
Tom Rinib6b99002023-10-12 19:03:59 -04007#include <config.h>
Yu Chien Peter Lin39689a92023-02-06 16:10:45 +08008#include <cpu_func.h>
Simon Glass8e201882020-05-10 11:39:54 -06009#include <flash.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060010#include <image.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070011#include <init.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <net.h>
Rick Chen36cb27c2017-12-26 13:55:53 +080013#if defined(CONFIG_FTMAC100) && !defined(CONFIG_DM_ETH)
14#include <netdev.h>
15#endif
Leo Yu-Chi Liangcec100f2023-12-26 14:54:27 +080016#include <asm/csr.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Leo Yu-Chi Liangcec100f2023-12-26 14:54:27 +080018#include <asm/sbi.h>
Rick Chen36cb27c2017-12-26 13:55:53 +080019#include <linux/io.h>
Rick Chencea16d02018-05-29 11:07:53 +080020#include <faraday/ftsmc020.h>
21#include <fdtdec.h>
Rick Chen9e017162019-08-28 18:46:07 +080022#include <dm.h>
Rick Chenc3027d02019-11-14 13:52:22 +080023#include <spl.h>
Randolphb4dc63f2023-10-12 14:35:09 +080024#include <mapmem.h>
25#include <hang.h>
Rick Chen36cb27c2017-12-26 13:55:53 +080026
27DECLARE_GLOBAL_DATA_PTR;
28
29/*
30 * Miscellaneous platform dependent initializations
31 */
Leo Yu-Chi Liangcec100f2023-12-26 14:54:27 +080032#if IS_ENABLED(CONFIG_MISC_INIT_R)
33int misc_init_r(void)
34{
35 long csr_marchid = 0;
36 const long mask_64 = 0x8000;
37 const long mask_cpu = 0xff;
38 char cpu_name[10] = {};
39
40#if CONFIG_IS_ENABLED(RISCV_SMODE)
41 sbi_get_marchid(&csr_marchid);
42#elif CONFIG_IS_ENABLED(RISCV_MMODE)
43 csr_marchid = csr_read(CSR_MARCHID);
44#endif
45 if (mask_64 & csr_marchid)
46 snprintf(cpu_name, sizeof(cpu_name), "ax%lx", (mask_cpu & csr_marchid));
47 else
48 snprintf(cpu_name, sizeof(cpu_name), "a%lx", (mask_cpu & csr_marchid));
49
50 return env_set("cpu", cpu_name);
51}
52#endif
Rick Chen36cb27c2017-12-26 13:55:53 +080053
Randolphb4dc63f2023-10-12 14:35:09 +080054#if CONFIG_IS_ENABLED(LOAD_FIT) || CONFIG_IS_ENABLED(LOAD_FIT_FULL)
55#define ANDES_SPL_FDT_ADDR (CONFIG_TEXT_BASE - 0x100000)
56void spl_perform_fixups(struct spl_image_info *spl_image)
57{
58 /*
59 * Originally, u-boot-spl will place DTB directly after the kernel,
60 * but the size of the kernel did not include the BSS section, which
61 * means u-boot-spl will place the DTB in the kernel BSS section
62 * causing the DTB to be cleared by kernel BSS initializtion.
63 * Moving DTB in front of the kernel can avoid the error.
64 */
65 if (ANDES_SPL_FDT_ADDR < 0) {
66 printf("%s: CONFIG_TEXT_BASE needs to be larger than 0x100000\n",
67 __func__);
68 hang();
69 }
70
71 memcpy((void *)ANDES_SPL_FDT_ADDR, spl_image->fdt_addr,
72 fdt_totalsize(spl_image->fdt_addr));
73 spl_image->fdt_addr = map_sysmem(ANDES_SPL_FDT_ADDR, 0);
74}
75#endif
76
Rick Chen36cb27c2017-12-26 13:55:53 +080077int board_init(void)
78{
Rick Chen36cb27c2017-12-26 13:55:53 +080079 gd->bd->bi_boot_params = PHYS_SDRAM_0 + 0x400;
80
81 return 0;
82}
83
84int dram_init(void)
85{
Rick Chen92038262019-11-14 13:52:23 +080086 return fdtdec_setup_mem_size_base();
Rick Chen36cb27c2017-12-26 13:55:53 +080087}
88
89int dram_init_banksize(void)
90{
Rick Chen92038262019-11-14 13:52:23 +080091 return fdtdec_setup_memory_banksize();
Rick Chen36cb27c2017-12-26 13:55:53 +080092}
93
94#if defined(CONFIG_FTMAC100) && !defined(CONFIG_DM_ETH)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090095int board_eth_init(struct bd_info *bd)
Rick Chen36cb27c2017-12-26 13:55:53 +080096{
97 return ftmac100_initialize(bd);
98}
99#endif
100
101ulong board_flash_get_legacy(ulong base, int banknum, flash_info_t *info)
102{
103 return 0;
104}
Rick Chen40a6fe72018-03-29 10:08:33 +0800105
Leo Yu-Chi Liang4150eec2022-06-01 10:01:49 +0800106#define ANDES_HW_DTB_ADDRESS 0xF2000000
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300107void *board_fdt_blob_setup(int *err)
Rick Chen40a6fe72018-03-29 10:08:33 +0800108{
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300109 *err = 0;
Leo Yu-Chi Liang4150eec2022-06-01 10:01:49 +0800110
111 if (IS_ENABLED(CONFIG_OF_SEPARATE) || IS_ENABLED(CONFIG_OF_BOARD)) {
Rick Chen206feaa2022-10-20 13:56:17 +0800112 if (fdt_magic((uintptr_t)gd->arch.firmware_fdt_addr) == FDT_MAGIC)
Leo Yu-Chi Liang4150eec2022-06-01 10:01:49 +0800113 return (void *)(ulong)gd->arch.firmware_fdt_addr;
114 }
115
116 if (fdt_magic(CONFIG_SYS_FDT_BASE) == FDT_MAGIC)
117 return (void *)CONFIG_SYS_FDT_BASE;
118 return (void *)ANDES_HW_DTB_ADDRESS;
119
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300120 *err = -EINVAL;
Ilias Apalodimasdc35df42021-10-12 00:00:13 +0300121 return NULL;
Rick Chen40a6fe72018-03-29 10:08:33 +0800122}
Rick Chencea16d02018-05-29 11:07:53 +0800123
Yu Chien Peter Lin39689a92023-02-06 16:10:45 +0800124#ifdef CONFIG_SPL_BOARD_INIT
125void spl_board_init()
126{
127 /* enable v5l2 cache */
Leo Yu-Chi Liang1eb9f912023-12-26 14:17:33 +0800128 if (!CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
129 enable_caches();
Yu Chien Peter Lin39689a92023-02-06 16:10:45 +0800130}
131#endif
132
Rick Chencea16d02018-05-29 11:07:53 +0800133int smc_init(void)
134{
135 int node = -1;
136 const char *compat = "andestech,atfsmc020";
137 void *blob = (void *)gd->fdt_blob;
138 fdt_addr_t addr;
139 struct ftsmc020_bank *regs;
140
141 node = fdt_node_offset_by_compatible(blob, -1, compat);
142 if (node < 0)
143 return -FDT_ERR_NOTFOUND;
144
Rick Chenca3e5e42020-07-17 16:24:44 +0800145 addr = fdtdec_get_addr_size_auto_noparent(blob, node,
146 "reg", 0, NULL, false);
Rick Chencea16d02018-05-29 11:07:53 +0800147
148 if (addr == FDT_ADDR_T_NONE)
149 return -EINVAL;
150
Bin Meng65d59952021-01-31 20:36:01 +0800151 regs = (struct ftsmc020_bank *)(uintptr_t)addr;
Rick Chencea16d02018-05-29 11:07:53 +0800152 regs->cr &= ~FTSMC020_BANK_WPROT;
153
154 return 0;
155}
156
157#ifdef CONFIG_BOARD_EARLY_INIT_F
158int board_early_init_f(void)
159{
160 smc_init();
161
162 return 0;
163}
164#endif
Rick Chenc3027d02019-11-14 13:52:22 +0800165
166#ifdef CONFIG_SPL
167void board_boot_order(u32 *spl_boot_list)
168{
169 u8 i;
170 u32 boot_devices[] = {
171#ifdef CONFIG_SPL_RAM_SUPPORT
172 BOOT_DEVICE_RAM,
173#endif
Simon Glassb58bfe02021-08-08 12:20:09 -0600174#ifdef CONFIG_SPL_MMC
Rick Chenc3027d02019-11-14 13:52:22 +0800175 BOOT_DEVICE_MMC1,
176#endif
177 };
178
179 for (i = 0; i < ARRAY_SIZE(boot_devices); i++)
180 spl_boot_list[i] = boot_devices[i];
181}
182#endif
183
184#ifdef CONFIG_SPL_LOAD_FIT
185int board_fit_config_name_match(const char *name)
186{
187 /* boot using first FIT config */
188 return 0;
189}
190#endif