1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2022 Marek Vasut <marex@denx.de>
4  */
5 
6 #include <asm/arch/clock.h>
7 #include <asm/arch/imx8mp_pins.h>
8 #include <asm/io.h>
9 #include <dm.h>
10 #include <dm/device-internal.h>
11 #include <env.h>
12 #include <env_internal.h>
13 #include <linux/bitfield.h>
14 #include <malloc.h>
15 #include <net.h>
16 #include <spl.h>
17 
18 #include "../common/common.h"
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
dmo_setup_second_mac_address(void)22 static void dmo_setup_second_mac_address(void)
23 {
24 	u8 enetaddr[6];
25 	int ret;
26 
27 	/* In case 'eth1addr' is already set in environment, do nothing. */
28 	ret = eth_env_get_enetaddr_by_index("eth", 1, enetaddr);
29 	if (ret)	/* valid 'eth1addr' is already set */
30 		return;
31 
32 	/* Read 'ethaddr' from environment and validate. */
33 	ret = eth_env_get_enetaddr_by_index("eth", 0, enetaddr);
34 	if (!ret)	/* 'ethaddr' in environment is not valid, stop */
35 		return;
36 
37 	/* Set 'eth1addr' as 'ethaddr' + 1 */
38 	enetaddr[5]++;
39 
40 	eth_env_set_enetaddr_by_index("eth", 1, enetaddr);
41 }
42 
env_get_location(enum env_operation op,int prio)43 enum env_location env_get_location(enum env_operation op, int prio)
44 {
45 	/* Environment is always in eMMC boot partitions */
46 	return prio ? ENVL_UNKNOWN : ENVL_MMC;
47 }
48 
board_late_init(void)49 int board_late_init(void)
50 {
51 	struct udevice *dev;
52 	int ret;
53 
54 	dmo_setup_boot_device();
55 	dmo_setup_mac_address();
56 	dmo_setup_second_mac_address();
57 
58 	ret = uclass_get_device_by_name(UCLASS_MISC, "usb-hub@2c", &dev);
59 	if (ret)
60 		printf("Error bringing up USB hub (%d)\n", ret);
61 
62 	return 0;
63 }
64 
fdtdec_board_setup(const void * fdt_blob)65 int fdtdec_board_setup(const void *fdt_blob)
66 {
67 	const void __iomem *mux = (void __iomem *)IOMUXC_BASE_ADDR +
68 		FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_ENET_MDC__ENET_QOS_MDC);
69 	const char *phy_compat = "ethernet-phy-ieee802.3-c22";
70 	bool is_bcmphy;
71 	int phy_node;
72 	int ret;
73 
74 	/* Do nothing if not i.MX8MP eDM SBC */
75 	ret = fdt_node_check_compatible(fdt_blob, 0, "dmo,imx8mp-data-modul-edm-sbc");
76 	if (ret)
77 		return 0;
78 
79 	/*
80 	 * If GPIO1_16 RGMII_MDC is HIGH, then R390 is populated.
81 	 * R390 is populated only on boards with AR8031 PHY.
82 	 *
83 	 * If GPIO1_16 RGMII_MDC is LOW, then the in-SoM pull down
84 	 * is the dominant pull resistor. This is the case on boards
85 	 * with BCM54213PE PHY.
86 	 */
87 	setbits_le32(mux, IOMUX_CONFIG_SION);
88 	is_bcmphy = !(readl(GPIO1_BASE_ADDR) & BIT(16));
89 	clrbits_le32(mux, IOMUX_CONFIG_SION);
90 
91 	phy_node = fdt_node_offset_by_compatible(fdt_blob, -1, phy_compat);
92 	if (phy_node < 0)
93 		return 0;
94 
95 	/*
96 	 * Update PHY MDC address in control DT based on the populated
97 	 * PHY type. AR8031 is at address 0, BCM54213PE is at address 1.
98 	 */
99 	fdt_setprop_inplace_u32((void *)fdt_blob, phy_node,
100 				"reg", is_bcmphy ? 1 : 0);
101 
102 	/* Apply the same modification to EQoS PHY */
103 	phy_node = fdt_node_offset_by_compatible(fdt_blob, phy_node, phy_compat);
104 	if (phy_node < 0)
105 		return 0;
106 
107 	fdt_setprop_inplace_u32((void *)fdt_blob, phy_node,
108 				"reg", is_bcmphy ? 1 : 0);
109 
110 	return 0;
111 }
112