1 // 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 
7 #include <cpu_func.h>
8 #include <dm.h>
9 #include <fdt_support.h>
10 #include <env.h>
11 #include <log.h>
12 #include <asm/arch/eeprom.h>
13 #include <asm/io.h>
14 #include <asm/sections.h>
15 #include <linux/bitops.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 #define JH7110_L2_PREFETCHER_BASE_ADDR		0x2030000
19 #define JH7110_L2_PREFETCHER_HART_OFFSET	0x2000
20 
21 /* enable U74-mc hart1~hart4 prefetcher */
enable_prefetcher(void)22 static void enable_prefetcher(void)
23 {
24 	u8 hart;
25 	u32 *reg;
26 
27 	/* JH7110 use U74MC CORE IP, it include five cores(one S7 and four U7),
28 	 * but only U7 cores support prefetcher configuration
29 	 */
30 	for (hart = 1; hart < 5; hart++) {
31 		reg = (void *)(u64)(JH7110_L2_PREFETCHER_BASE_ADDR
32 					+ hart * JH7110_L2_PREFETCHER_HART_OFFSET);
33 
34 		mb(); /* memory barrier */
35 		setbits_le32(reg, 0x1);
36 		mb(); /* memory barrier */
37 	}
38 }
39 
40 /**
41  * set_fdtfile() - set the $fdtfile variable based on product data in EEPROM
42  */
set_fdtfile(void)43 static void set_fdtfile(void)
44 {
45 	const char *fdtfile;
46 
47 	fdtfile = env_get("fdtfile");
48 	if (fdtfile)
49 		return;
50 
51 	if (!get_product_id_from_eeprom()) {
52 		log_err("Can't read EEPROM\n");
53 		return;
54 	}
55 
56 	if (!strncmp(get_product_id_from_eeprom(), "FML13V01", 8)) {
57 		fdtfile = "starfive/jh7110-deepcomputing-fml13v01.dtb";
58 	} else if (!strncmp(get_product_id_from_eeprom(), "MARS", 4)) {
59 		fdtfile = "starfive/jh7110-milkv-mars.dtb";
60 	} else if (!strncmp(get_product_id_from_eeprom(), "STAR64", 6)) {
61 		fdtfile = "starfive/jh7110-pine64-star64.dtb";
62 	} else if (!strncmp(get_product_id_from_eeprom(), "VF7110", 6)) {
63 		switch (get_pcb_revision_from_eeprom()) {
64 		case 'a':
65 		case 'A':
66 			fdtfile = "starfive/jh7110-starfive-visionfive-2-v1.2a.dtb";
67 			break;
68 		case 'b':
69 		case 'B':
70 			fdtfile = "starfive/jh7110-starfive-visionfive-2-v1.3b.dtb";
71 			break;
72 		default:
73 			log_err("Unknown revision\n");
74 			return;
75 		}
76 	} else {
77 		log_err("Unknown product\n");
78 		return;
79 	}
80 
81 	env_set("fdtfile", fdtfile);
82 }
83 
board_init(void)84 int board_init(void)
85 {
86 	enable_caches();
87 	enable_prefetcher();
88 
89 	return 0;
90 }
91 
board_late_init(void)92 int board_late_init(void)
93 {
94 	if (CONFIG_IS_ENABLED(ID_EEPROM))
95 		set_fdtfile();
96 
97 	return 0;
98 }
99 
ft_board_setup(void * blob,struct bd_info * bd)100 int ft_board_setup(void *blob, struct bd_info *bd)
101 {
102 	return fdt_fixup_memory(blob, 0x40000000, gd->ram_size);
103 }
104