blob: 45848db6d8bf8b51a9658b00e28dca5cd2f2375c [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 +080023struct starfive_vf2_pro {
24 const char *path;
25 const char *name;
26 const char *value;
27};
28
Heinrich Schuchardtf8841732024-04-02 10:49:10 +020029static const struct starfive_vf2_pro milk_v_mars[] = {
30 {"/soc/ethernet@16030000", "starfive,tx-use-rgmii-clk", NULL},
31 {"/soc/ethernet@16040000", "starfive,tx-use-rgmii-clk", NULL},
32
33 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
34 "motorcomm,tx-clk-adj-enabled", NULL},
35 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
36 "motorcomm,tx-clk-100-inverted", NULL},
37 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
38 "motorcomm,tx-clk-1000-inverted", NULL},
39 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
40 "motorcomm,rx-clk-drv-microamp", "3970"},
41 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
42 "motorcomm,rx-data-drv-microamp", "2910"},
43 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
44 "rx-internal-delay-ps", "1900"},
45 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
46 "tx-internal-delay-ps", "1500"},
47};
48
Yanhong Wang4e321fa2023-06-15 17:36:52 +080049static const struct starfive_vf2_pro starfive_vera[] = {
50 {"/soc/ethernet@16030000/mdio/ethernet-phy@0", "rx-internal-delay-ps",
51 "1900"},
52 {"/soc/ethernet@16030000/mdio/ethernet-phy@0", "tx-internal-delay-ps",
53 "1350"}
54};
55
56static const struct starfive_vf2_pro starfive_verb[] = {
57 {"/soc/ethernet@16030000", "starfive,tx-use-rgmii-clk", NULL},
58 {"/soc/ethernet@16040000", "starfive,tx-use-rgmii-clk", NULL},
59
60 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
61 "motorcomm,tx-clk-adj-enabled", NULL},
62 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
63 "motorcomm,tx-clk-100-inverted", NULL},
64 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
65 "motorcomm,tx-clk-1000-inverted", NULL},
66 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
Lukasz Tekielia99605d2024-01-28 20:22:48 +010067 "motorcomm,rx-clk-drv-microamp", "3970"},
68 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
69 "motorcomm,rx-data-drv-microamp", "2910"},
70 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
Yanhong Wang4e321fa2023-06-15 17:36:52 +080071 "rx-internal-delay-ps", "1900"},
72 {"/soc/ethernet@16030000/mdio/ethernet-phy@0",
73 "tx-internal-delay-ps", "1500"},
74
75 {"/soc/ethernet@16040000/mdio/ethernet-phy@1",
76 "motorcomm,tx-clk-adj-enabled", NULL},
77 { "/soc/ethernet@16040000/mdio/ethernet-phy@1",
78 "motorcomm,tx-clk-100-inverted", NULL},
79 {"/soc/ethernet@16040000/mdio/ethernet-phy@1",
Lukasz Tekielia99605d2024-01-28 20:22:48 +010080 "motorcomm,rx-clk-drv-microamp", "3970"},
81 {"/soc/ethernet@16040000/mdio/ethernet-phy@1",
82 "motorcomm,rx-data-drv-microamp", "2910"},
83 {"/soc/ethernet@16040000/mdio/ethernet-phy@1",
Yanhong Wang4e321fa2023-06-15 17:36:52 +080084 "rx-internal-delay-ps", "0"},
85 {"/soc/ethernet@16040000/mdio/ethernet-phy@1",
86 "tx-internal-delay-ps", "0"},
87};
88
Heinrich Schuchardtf8841732024-04-02 10:49:10 +020089void spl_fdt_fixup_mars(void *fdt)
90{
91 static const char compat[] = "milkv,mars\0starfive,jh7110";
92 u32 phandle;
93 u8 i;
94 int offset;
95 int ret;
96
97 fdt_setprop(fdt, fdt_path_offset(fdt, "/"), "compatible", compat, sizeof(compat));
98 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "model",
99 "Milk-V Mars");
100
101 /* gmac0 */
102 offset = fdt_path_offset(fdt, "/soc/clock-controller@17000000");
103 phandle = fdt_get_phandle(fdt, offset);
104 offset = fdt_path_offset(fdt, "/soc/ethernet@16030000");
105
106 fdt_setprop_u32(fdt, offset, "assigned-clocks", phandle);
107 fdt_appendprop_u32(fdt, offset, "assigned-clocks", JH7110_AONCLK_GMAC0_TX);
108 fdt_setprop_u32(fdt, offset, "assigned-clock-parents", phandle);
109 fdt_appendprop_u32(fdt, offset, "assigned-clock-parents",
110 JH7110_AONCLK_GMAC0_RMII_RTX);
111
112 /* gmac1 */
113 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/soc/ethernet@16040000"),
114 "status", "disabled");
115
116 for (i = 0; i < ARRAY_SIZE(milk_v_mars); i++) {
117 offset = fdt_path_offset(fdt, milk_v_mars[i].path);
118
119 if (milk_v_mars[i].value)
120 ret = fdt_setprop_u32(fdt, offset, milk_v_mars[i].name,
121 dectoul(milk_v_mars[i].value, NULL));
122 else
123 ret = fdt_setprop_empty(fdt, offset, milk_v_mars[i].name);
124
125 if (ret) {
126 pr_err("%s set prop %s fail.\n", __func__, milk_v_mars[i].name);
127 break;
128 }
129 }
130}
131
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800132void spl_fdt_fixup_version_a(void *fdt)
133{
Aurelien Jarno5acb7c12024-01-10 21:17:44 +0100134 static const char compat[] = "starfive,visionfive-2-v1.2a\0starfive,jh7110";
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800135 u32 phandle;
136 u8 i;
137 int offset;
138 int ret;
139
Aurelien Jarno5acb7c12024-01-10 21:17:44 +0100140 fdt_setprop(fdt, fdt_path_offset(fdt, "/"), "compatible", compat, sizeof(compat));
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800141 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "model",
142 "StarFive VisionFive 2 v1.2A");
143
144 offset = fdt_path_offset(fdt, "/soc/clock-controller@13020000");
145 phandle = fdt_get_phandle(fdt, offset);
146 offset = fdt_path_offset(fdt, "/soc/ethernet@16040000");
147
148 fdt_setprop_u32(fdt, offset, "assigned-clocks", phandle);
149 fdt_appendprop_u32(fdt, offset, "assigned-clocks", JH7110_SYSCLK_GMAC1_TX);
150 fdt_appendprop_u32(fdt, offset, "assigned-clocks", phandle);
151 fdt_appendprop_u32(fdt, offset, "assigned-clocks", JH7110_SYSCLK_GMAC1_RX);
152
153 fdt_setprop_u32(fdt, offset, "assigned-clock-parents", phandle);
154 fdt_appendprop_u32(fdt, offset, "assigned-clock-parents",
155 JH7110_SYSCLK_GMAC1_RMII_RTX);
156 fdt_appendprop_u32(fdt, offset, "assigned-clock-parents", phandle);
157 fdt_appendprop_u32(fdt, offset, "assigned-clock-parents",
158 JH7110_SYSCLK_GMAC1_RMII_RTX);
159
160 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/soc/ethernet@16040000"),
161 "phy-mode", "rmii");
162
163 for (i = 0; i < ARRAY_SIZE(starfive_vera); i++) {
164 offset = fdt_path_offset(fdt, starfive_vera[i].path);
165
166 if (starfive_vera[i].value)
167 ret = fdt_setprop_u32(fdt, offset, starfive_vera[i].name,
168 dectoul(starfive_vera[i].value, NULL));
169 else
170 ret = fdt_setprop_empty(fdt, offset, starfive_vera[i].name);
171
172 if (ret) {
173 pr_err("%s set prop %s fail.\n", __func__, starfive_vera[i].name);
174 break;
175 }
176 }
177}
178
179void spl_fdt_fixup_version_b(void *fdt)
180{
Aurelien Jarno5acb7c12024-01-10 21:17:44 +0100181 static const char compat[] = "starfive,visionfive-2-v1.3b\0starfive,jh7110";
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800182 u32 phandle;
183 u8 i;
184 int offset;
185 int ret;
186
Aurelien Jarno5acb7c12024-01-10 21:17:44 +0100187 fdt_setprop(fdt, fdt_path_offset(fdt, "/"), "compatible", compat, sizeof(compat));
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800188 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "model",
189 "StarFive VisionFive 2 v1.3B");
190
191 /* gmac0 */
192 offset = fdt_path_offset(fdt, "/soc/clock-controller@17000000");
193 phandle = fdt_get_phandle(fdt, offset);
194 offset = fdt_path_offset(fdt, "/soc/ethernet@16030000");
195
196 fdt_setprop_u32(fdt, offset, "assigned-clocks", phandle);
197 fdt_appendprop_u32(fdt, offset, "assigned-clocks", JH7110_AONCLK_GMAC0_TX);
198 fdt_setprop_u32(fdt, offset, "assigned-clock-parents", phandle);
199 fdt_appendprop_u32(fdt, offset, "assigned-clock-parents",
200 JH7110_AONCLK_GMAC0_RMII_RTX);
201
202 /* gmac1 */
203 offset = fdt_path_offset(fdt, "/soc/clock-controller@13020000");
204 phandle = fdt_get_phandle(fdt, offset);
205 offset = fdt_path_offset(fdt, "/soc/ethernet@16040000");
206
207 fdt_setprop_u32(fdt, offset, "assigned-clocks", phandle);
208 fdt_appendprop_u32(fdt, offset, "assigned-clocks", JH7110_SYSCLK_GMAC1_TX);
209 fdt_setprop_u32(fdt, offset, "assigned-clock-parents", phandle);
210 fdt_appendprop_u32(fdt, offset, "assigned-clock-parents",
211 JH7110_SYSCLK_GMAC1_RMII_RTX);
212
213 for (i = 0; i < ARRAY_SIZE(starfive_verb); i++) {
214 offset = fdt_path_offset(fdt, starfive_verb[i].path);
215
216 if (starfive_verb[i].value)
217 ret = fdt_setprop_u32(fdt, offset, starfive_verb[i].name,
218 dectoul(starfive_verb[i].value, NULL));
219 else
220 ret = fdt_setprop_empty(fdt, offset, starfive_verb[i].name);
221
222 if (ret) {
223 pr_err("%s set prop %s fail.\n", __func__, starfive_verb[i].name);
224 break;
225 }
226 }
227}
228
229void spl_perform_fixups(struct spl_image_info *spl_image)
230{
231 u8 version;
Heinrich Schuchardtf8841732024-04-02 10:49:10 +0200232 const char *product_id;
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800233
Heinrich Schuchardtf8841732024-04-02 10:49:10 +0200234 product_id = get_product_id_from_eeprom();
235 if (!product_id) {
236 pr_err("Can't read EEPROM\n");
237 return;
238 }
239 if (!strncmp(product_id, "MARS", 4)) {
240 spl_fdt_fixup_mars(spl_image->fdt_addr);
241 } else if (!strncmp(product_id, "VF7110", 6)) {
242 version = get_pcb_revision_from_eeprom();
243 switch (version) {
244 case 'a':
245 case 'A':
246 spl_fdt_fixup_version_a(spl_image->fdt_addr);
247 break;
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800248
Heinrich Schuchardtf8841732024-04-02 10:49:10 +0200249 case 'b':
250 case 'B':
251 default:
252 spl_fdt_fixup_version_b(spl_image->fdt_addr);
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800253 break;
Heinrich Schuchardtf8841732024-04-02 10:49:10 +0200254 };
255 } else {
256 pr_err("Unknown product %s\n", product_id);
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800257 };
258
Heinrich Schuchardtf8841732024-04-02 10:49:10 +0200259 /* Update the memory size which read from eeprom or DT */
Yanhong Wang4e321fa2023-06-15 17:36:52 +0800260 fdt_fixup_memory(spl_image->fdt_addr, 0x40000000, gd->ram_size);
261}
Chanho Park9ca68c92023-10-31 17:56:00 +0900262
263static void jh7110_jtag_init(void)
264{
265 /* nTRST: GPIO36 */
266 SYS_IOMUX_DOEN(36, HIGH);
267 SYS_IOMUX_DIN(36, 4);
268 /* TDI: GPIO61 */
269 SYS_IOMUX_DOEN(61, HIGH);
270 SYS_IOMUX_DIN(61, 19);
271 /* TMS: GPIO63 */
272 SYS_IOMUX_DOEN(63, HIGH);
273 SYS_IOMUX_DIN(63, 20);
274 /* TCK: GPIO60 */
275 SYS_IOMUX_DOEN(60, HIGH);
276 SYS_IOMUX_DIN(60, 29);
277 /* TDO: GPIO44 */
278 SYS_IOMUX_DOEN(44, 8);
279 SYS_IOMUX_DOUT(44, 22);
280}
281
Yanhong Wang6a5a45d2023-03-29 11:42:17 +0800282int spl_board_init_f(void)
283{
284 int ret;
285
Chanho Park9ca68c92023-10-31 17:56:00 +0900286 jh7110_jtag_init();
287
Yanhong Wang6a5a45d2023-03-29 11:42:17 +0800288 ret = spl_soc_init();
289 if (ret) {
290 debug("JH7110 SPL init failed: %d\n", ret);
291 return ret;
292 }
293
294 return 0;
295}
296
297u32 spl_boot_device(void)
298{
299 u32 mode;
300
301 mode = in_le32(JH7110_BOOT_MODE_SELECT_REG)
302 & JH7110_BOOT_MODE_SELECT_MASK;
303 switch (mode) {
304 case 0:
305 return BOOT_DEVICE_SPI;
306
307 case 1:
308 return BOOT_DEVICE_MMC2;
309
310 case 2:
311 return BOOT_DEVICE_MMC1;
312
313 case 3:
314 return BOOT_DEVICE_UART;
315
316 default:
317 debug("Unsupported boot device 0x%x.\n", mode);
318 return BOOT_DEVICE_NONE;
319 }
320}
321
322void board_init_f(ulong dummy)
323{
324 int ret;
325
326 ret = spl_early_init();
327 if (ret)
328 panic("spl_early_init() failed: %d\n", ret);
329
Simon Glassb8357c12023-08-21 21:16:56 -0600330 riscv_cpu_setup();
Yanhong Wang6a5a45d2023-03-29 11:42:17 +0800331 preloader_console_init();
332
333 /* Set the parent clock of cpu_root clock to pll0,
334 * it must be initialized here
335 */
336 clrsetbits_le32(JH7110_SYS_CRG + JH7110_CLK_CPU_ROOT_OFFSET,
337 JH7110_CLK_CPU_ROOT_MASK,
338 BIT(JH7110_CLK_CPU_ROOT_SHIFT));
339
340 ret = spl_board_init_f();
341 if (ret) {
342 debug("spl_board_init_f init failed: %d\n", ret);
343 return;
344 }
345}
346
347#if CONFIG_IS_ENABLED(SPL_LOAD_FIT)
348int board_fit_config_name_match(const char *name)
349{
350 /* boot using first FIT config */
351 return 0;
352}
353#endif