blob: 22afd76c6b911e4358fd7f7839cbdea66ba0a140 [file] [log] [blame]
Yanhong Wang6a5a45d2023-03-29 11:42:17 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2022 StarFive Technology Co., Ltd.
4 * Author: Yanhong Wang<yanhong.wang@starfivetech.com>
5 */
6
Yanhong Wang4e321fa2023-06-15 17:36:52 +08007#include <asm/arch/eeprom.h>
Chanho Park9ca68c92023-10-31 17:56:00 +09008#include <asm/arch/gpio.h>
Yanhong Wang6a5a45d2023-03-29 11:42:17 +08009#include <asm/arch/regs.h>
10#include <asm/arch/spl.h>
11#include <asm/io.h>
Yanhong Wang4e321fa2023-06-15 17:36:52 +080012#include <dt-bindings/clock/starfive,jh7110-crg.h>
13#include <fdt_support.h>
14#include <linux/libfdt.h>
Yanhong Wang6a5a45d2023-03-29 11:42:17 +080015#include <log.h>
16#include <spl.h>
17
Yanhong Wang4e321fa2023-06-15 17:36:52 +080018DECLARE_GLOBAL_DATA_PTR;
Yanhong Wang6a5a45d2023-03-29 11:42:17 +080019#define JH7110_CLK_CPU_ROOT_OFFSET 0x0U
20#define JH7110_CLK_CPU_ROOT_SHIFT 24
21#define JH7110_CLK_CPU_ROOT_MASK GENMASK(29, 24)
22
Yanhong Wang4e321fa2023-06-15 17:36:52 +080023void spl_perform_fixups(struct spl_image_info *spl_image)
24{
Heinrich Schuchardtf8841732024-04-02 10:49:10 +020025 /* Update the memory size which read from eeprom or DT */
Yanhong Wang4e321fa2023-06-15 17:36:52 +080026 fdt_fixup_memory(spl_image->fdt_addr, 0x40000000, gd->ram_size);
27}
Chanho Park9ca68c92023-10-31 17:56:00 +090028
29static void jh7110_jtag_init(void)
30{
31 /* nTRST: GPIO36 */
32 SYS_IOMUX_DOEN(36, HIGH);
33 SYS_IOMUX_DIN(36, 4);
34 /* TDI: GPIO61 */
35 SYS_IOMUX_DOEN(61, HIGH);
36 SYS_IOMUX_DIN(61, 19);
37 /* TMS: GPIO63 */
38 SYS_IOMUX_DOEN(63, HIGH);
39 SYS_IOMUX_DIN(63, 20);
40 /* TCK: GPIO60 */
41 SYS_IOMUX_DOEN(60, HIGH);
42 SYS_IOMUX_DIN(60, 29);
43 /* TDO: GPIO44 */
44 SYS_IOMUX_DOEN(44, 8);
45 SYS_IOMUX_DOUT(44, 22);
46}
47
Yanhong Wang6a5a45d2023-03-29 11:42:17 +080048int spl_board_init_f(void)
49{
50 int ret;
51
Chanho Park9ca68c92023-10-31 17:56:00 +090052 jh7110_jtag_init();
53
Lukas Funke2b62dd62024-04-24 09:43:39 +020054 ret = spl_dram_init();
Yanhong Wang6a5a45d2023-03-29 11:42:17 +080055 if (ret) {
Lukas Funke2b62dd62024-04-24 09:43:39 +020056 debug("JH7110 DRAM init failed: %d\n", ret);
Yanhong Wang6a5a45d2023-03-29 11:42:17 +080057 return ret;
58 }
59
60 return 0;
61}
62
63u32 spl_boot_device(void)
64{
65 u32 mode;
66
67 mode = in_le32(JH7110_BOOT_MODE_SELECT_REG)
68 & JH7110_BOOT_MODE_SELECT_MASK;
69 switch (mode) {
70 case 0:
71 return BOOT_DEVICE_SPI;
72
73 case 1:
74 return BOOT_DEVICE_MMC2;
75
76 case 2:
77 return BOOT_DEVICE_MMC1;
78
79 case 3:
80 return BOOT_DEVICE_UART;
81
82 default:
83 debug("Unsupported boot device 0x%x.\n", mode);
84 return BOOT_DEVICE_NONE;
85 }
86}
87
88void board_init_f(ulong dummy)
89{
90 int ret;
91
92 ret = spl_early_init();
93 if (ret)
94 panic("spl_early_init() failed: %d\n", ret);
95
Simon Glassb8357c12023-08-21 21:16:56 -060096 riscv_cpu_setup();
Yanhong Wang6a5a45d2023-03-29 11:42:17 +080097 preloader_console_init();
98
99 /* Set the parent clock of cpu_root clock to pll0,
100 * it must be initialized here
101 */
102 clrsetbits_le32(JH7110_SYS_CRG + JH7110_CLK_CPU_ROOT_OFFSET,
103 JH7110_CLK_CPU_ROOT_MASK,
104 BIT(JH7110_CLK_CPU_ROOT_SHIFT));
105
106 ret = spl_board_init_f();
107 if (ret) {
108 debug("spl_board_init_f init failed: %d\n", ret);
109 return;
110 }
111}
112
Hal Feng446e48f2024-12-08 17:19:40 +0800113#if CONFIG_IS_ENABLED(LOAD_FIT)
Yanhong Wang6a5a45d2023-03-29 11:42:17 +0800114int board_fit_config_name_match(const char *name)
115{
Hal Fengd0503502024-12-08 17:19:41 +0800116 const char *product_id;
117 u8 version;
118
119 product_id = get_product_id_from_eeprom();
120
121 if (!strncmp(product_id, "VF7110", 6)) {
122 version = get_pcb_revision_from_eeprom();
123 if ((version == 'b' || version == 'B') &&
124 !strcmp(name, "jh7110-starfive-visionfive-2-v1.3b"))
125 return 0;
126
127 if ((version == 'a' || version == 'A') &&
128 !strcmp(name, "jh7110-starfive-visionfive-2-v1.2a"))
129 return 0;
130 } else if (!strncmp(product_id, "MARS", 4) &&
131 !strcmp(name, "jh7110-milkv-mars")) {
132 return 0;
133 } else if (!strncmp(product_id, "MARC", 4)) {
134 if (!get_mmc_size_from_eeprom()) {
135 if (!strcmp(name, "jh7110-milkv-mars-cm-lite"))
136 return 0;
137 } else {
138 if (!strcmp(name, "jh7110-milkv-mars-cm"))
139 return 0;
140 }
141 } else if (!strncmp(product_id, "STAR64", 6) &&
142 !strcmp(name, "jh7110-pine64-star64")) {
143 return 0;
144 }
145
146 return -EINVAL;
Yanhong Wang6a5a45d2023-03-29 11:42:17 +0800147}
148#endif