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