1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2020-2021 Toradex
4  */
5 
6 #include <common.h>
7 #include <init.h>
8 #include <asm/arch/clock.h>
9 #include <asm/arch/sys_proto.h>
10 #include <asm/global_data.h>
11 #include <asm/io.h>
12 #include <hang.h>
13 #include <i2c.h>
14 #include <micrel.h>
15 #include <miiphy.h>
16 #include <netdev.h>
17 
18 #include "../common/tdx-cfg-block.h"
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
22 #define I2C_PMIC	0
23 
24 enum pcb_rev_t {
25 	PCB_VERSION_1_0,
26 	PCB_VERSION_1_1
27 };
28 
29 #if IS_ENABLED(CONFIG_FEC_MXC)
setup_fec(void)30 static int setup_fec(void)
31 {
32 	struct iomuxc_gpr_base_regs *gpr =
33 		(struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
34 
35 	/* Use 125M anatop REF_CLK1 for ENET1, not from external */
36 	clrsetbits_le32(&gpr->gpr[1], 0x2000, 0);
37 
38 	return 0;
39 }
40 #endif
41 
board_init(void)42 int board_init(void)
43 {
44 	if (IS_ENABLED(CONFIG_FEC_MXC))
45 		setup_fec();
46 
47 	return 0;
48 }
49 
board_mmc_get_env_dev(int devno)50 int board_mmc_get_env_dev(int devno)
51 {
52 	return devno;
53 }
54 
get_pcb_revision(void)55 static enum pcb_rev_t get_pcb_revision(void)
56 {
57 	struct udevice *bus;
58 	struct udevice *i2c_dev = NULL;
59 	int ret;
60 	u8 is_bd71837 = 0;
61 
62 	ret = uclass_get_device_by_seq(UCLASS_I2C, I2C_PMIC, &bus);
63 	if (!ret)
64 		ret = dm_i2c_probe(bus, 0x4b, 0, &i2c_dev);
65 	if (!ret)
66 		ret = dm_i2c_read(i2c_dev, 0x0, &is_bd71837, 1);
67 
68 	/* BD71837_REV, High Nibble is major version, fix 1010 */
69 	is_bd71837 = !ret && ((is_bd71837 & 0xf0) == 0xa0);
70 	return is_bd71837 ? PCB_VERSION_1_0 : PCB_VERSION_1_1;
71 }
72 
select_dt_from_module_version(void)73 static void select_dt_from_module_version(void)
74 {
75 	char variant[32];
76 	char *env_variant = env_get("variant");
77 	int is_wifi = 0;
78 
79 	if (IS_ENABLED(CONFIG_TDX_CFG_BLOCK)) {
80 		/*
81 		 * If we have a valid config block and it says we are a
82 		 * module with Wi-Fi/Bluetooth make sure we use the -wifi
83 		 * device tree.
84 		 */
85 		is_wifi = (tdx_hw_tag.prodid == VERDIN_IMX8MMQ_WIFI_BT_IT) ||
86 			  (tdx_hw_tag.prodid == VERDIN_IMX8MMDL_WIFI_BT_IT);
87 	}
88 
89 	switch (get_pcb_revision()) {
90 	case PCB_VERSION_1_0:
91 		printf("Detected a V1.0 module which is no longer supported in this BSP version\n");
92 		hang();
93 	default:
94 		if (is_wifi)
95 			strlcpy(&variant[0], "wifi", sizeof(variant));
96 		else
97 			strlcpy(&variant[0], "nonwifi", sizeof(variant));
98 		break;
99 	}
100 
101 	if (strcmp(variant, env_variant)) {
102 		printf("Setting variant to %s\n", variant);
103 		env_set("variant", variant);
104 
105 		if (IS_ENABLED(CONFIG_ENV_IS_NOWHERE))
106 			env_save();
107 	}
108 }
109 
board_late_init(void)110 int board_late_init(void)
111 {
112 	select_dt_from_module_version();
113 
114 	return 0;
115 }
116 
board_phys_sdram_size(phys_size_t * size)117 int board_phys_sdram_size(phys_size_t *size)
118 {
119 	if (!size)
120 		return -EINVAL;
121 
122 	*size = get_ram_size((void *)PHYS_SDRAM, PHYS_SDRAM_SIZE);
123 
124 	return 0;
125 }
126 
127 #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
ft_board_setup(void * blob,struct bd_info * bd)128 int ft_board_setup(void *blob, struct bd_info *bd)
129 {
130 	return 0;
131 }
132 #endif
133