Deduplicate dhelectronics board files
cover Deduplicate dhelectronics board files
This series unifies common mac address code for imx6, imx8 and stm32 based boards by DH. It is thought of as a starting point for more deduplication in the future.
Philip Oberfichtner (4):
board: dhelectronics: Implement common mac address functions
ARM: imx6: DH: Use common mac address functions
ARM: imx8: DH: Use common mac address functions
ARM: stm32: DH: Use common mac address functions
board/dhelectronics/common/Makefile | 10 ++ board/dhelectronics/common/dh_common.c | 64 +++++++++ board/dhelectronics/common/dh_common.h | 28 ++++ board/dhelectronics/common/dh_imx.c | 24 ++++ board/dhelectronics/common/dh_imx.h | 12 ++ board/dhelectronics/dh_imx6/dh_imx6.c | 47 ++----- .../dh_imx8mp/imx8mp_dhcom_pdk2.c | 121 +++++++----------- board/dhelectronics/dh_stm32mp1/board.c | 104 +++++++-------- 8 files changed, 247 insertions(+), 163 deletions(-) create mode 100644 board/dhelectronics/common/Makefile create mode 100644 board/dhelectronics/common/dh_common.c create mode 100644 board/dhelectronics/common/dh_common.h create mode 100644 board/dhelectronics/common/dh_imx.c create mode 100644 board/dhelectronics/common/dh_imx.h
Signature · 1 line
2.35.3
PATCH 1/4 board: dhelectronics: Implement common mac address functions
This is a starting point for unifying duplicate code in the DH board files. The functions for setting up the mac address are very similar for the i.MX6, i.MX8 and stm32mp1 based boards.
All pre-existing implementations follow the same logic:
(1) Check if ethaddr is already set in the environment
(2) If not, try to get it from a board specific location (e.g. fuse)
(3) If not, try to get it from eeprom
After this commit, (1) and (3) are implemented as common functions, ready to be used by board specific files.
Furthermore there is an implementation of (2) for imx based boards.
---
board/dhelectronics/common/Makefile | 10 ++++ board/dhelectronics/common/dh_common.c | 64 ++++++++++++++++++++++++++ board/dhelectronics/common/dh_common.h | 28 +++++++++++ board/dhelectronics/common/dh_imx.c | 24 ++++++++++ board/dhelectronics/common/dh_imx.h | 12 +++++ 5 files changed, 138 insertions(+) create mode 100644 board/dhelectronics/common/Makefile create mode 100644 board/dhelectronics/common/dh_common.c create mode 100644 board/dhelectronics/common/dh_common.h create mode 100644 board/dhelectronics/common/dh_imx.c create mode 100644 board/dhelectronics/common/dh_imx.h
board/dhelectronics/common/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner <[email protected]> +# + +obj-y += dh_common.o + +ifneq (,$(CONFIG_ARCH_MX6)$(CONFIG_ARCH_IMX8M)) +obj-y += dh_imx.o +endif
board/dhelectronics/common/dh_common.c new file +64 -0
64 lines not on this page · raw message
board/dhelectronics/common/dh_common.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0+ +* +* Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner <[email protected]> +*/ + +/* +* dh_mac_is_in_env - Check if MAC address is already set +* +* @env: name of environment variable +* Return: true if MAC is set, false otherwise +*/ +bool dh_mac_is_in_env (const char *env); + +/* +* dh_get_mac_from_eeprom - Get MAC address from eeprom and write it to enetaddr +* +* @enetaddr: buffer where address is to be stored +* @alias: alias for EEPROM device tree node +* Return: 0 if OK, other value on error +*/ +int dh_get_mac_from_eeprom (unsigned char *enetaddr,const char *alias); + +/* +* dh_setup_mac_address - Try to get MAC address from various locations and write it to env +* +* Return: 0 if OK, other value on error +*/ +int dh_setup_mac_address (void );
board/dhelectronics/common/dh_imx.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright 2022 Marek Vasut <[email protected]> +* Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner <[email protected]> +*/ + +#include <asm/arch/imx-regs.h> +#include <asm/arch/sys_proto.h> +#include <common.h> +#include <net.h> +#include "dh_imx.h" + +int dh_imx_get_mac_from_fuse (unsigned char *enetaddr) +{ +/* +* If IIM fuses contain valid MAC address, use it. +* The IIM MAC address fuses are NOT programmed by default. +*/ +imx_get_mac_from_fuse (0 , enetaddr); +if (!is_valid_ethaddr (enetaddr)) +return -EINVAL ; + +return 0 ; +}
board/dhelectronics/common/dh_imx.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0+ +* +* Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner <[email protected]> +*/ + +/* +* dh_imx_get_mac_from_fuse - Get MAC address from fuse and write it to env +* +* @enetaddr: buffer where address is to be stored +* Return: 0 if OK, other value on error +*/ +int dh_imx_get_mac_from_fuse (unsigned char *enetaddr);
Signature · 1 line
2.35.3
Re: [PATCH 1/4] board: dhelectronics: Implement common mac address functions
On 7/4/22 13:11, Philip Oberfichtner wrote:
[...]
Quoted 2 lines
+++
b/board/dhelectronics/common/dh_common.c(quoted diff) +0
-0@@ -0,0 +1,64 @@
[...]
board/dhelectronics/common/dh_common.c line 28
Quoted 8 lines
board/dhelectronics/common/dh_common.c new file +8
-0+int dh_get_mac_from_eeprom (unsigned char *enetaddr,const char *alias) +{ +struct udevice *dev; +int ret, offset; + + offset =fdt_path_offset (gd->fdt_blob, alias); +if (offset <0 ) { +printf ("%s: Path offset for %s not found! Error %d\n" , __func__, alias, offset);
Use "ret = %d" instead of "Error %d" consistently with the rest of this function.
board/dhelectronics/common/dh_common.c line 34
Quoted 6 lines
board/dhelectronics/common/dh_common.c new file +6
-0+return offset; + } + + ret =uclass_get_device_by_of_offset (UCLASS_I2C_EEPROM , offset, &dev); +if (ret) { +printf ("%s: Cannot find EEPROM! ret = %d\n" , __func__, ret);
Two spaces between "ret =" and "%d"
[...]
PATCH 2/4 ARM: imx6: DH: Use common mac address functions
In reply to Philip OberfichtnerTo reduce code duplication, let the imx6 based DH boards use the common code for setting up their mac addresses.
---
board/dhelectronics/dh_imx6/dh_imx6.c | 47 ++++++++------------------- 1 file changed, 14 insertions(+), 33 deletions(-)
board/dhelectronics/dh_imx6/dh_imx6.c +14 -33
@@ -36,6 +36,9 @@#include <linux/delay.h> #include <usb/ehci-ci.h> +#include "../common/dh_common.h" +#include "../common/dh_imx.h" +DECLARE_GLOBAL_DATA_PTR ;int dram_init (void )
@@ -82,46 +85,24 @@ int board_usb_phy_mode(int port) }#endif -static int setup_dhcom_mac_from_fuse (void ) +int dh_setup_mac_address (void ) { -struct udevice *dev; -ofnode eeprom;unsigned char enetaddr[6 ]; -int ret; - ret =eth_env_get_enetaddr ("ethaddr" , enetaddr); -if (ret)/* ethaddr is already set */ +if (dh_mac_is_in_env ("ethaddr" ))return 0 ; -imx_get_mac_from_fuse (0 , enetaddr); - -if (is_valid_ethaddr (enetaddr)) { -eth_env_set_enetaddr ("ethaddr" , enetaddr); -return 0 ; - } - - eeprom =ofnode_get_aliases_node ("eeprom0" ); -if (!ofnode_valid (eeprom)) { -printf ("Can't find eeprom0 alias!\n" ); -return -ENODEV ; - } +if (!dh_imx_get_mac_from_fuse (enetaddr)) + goto out; - ret =uclass_get_device_by_ofnode (UCLASS_I2C_EEPROM , eeprom, &dev); -if (ret) { -printf ("Cannot find EEPROM!\n" ); -return ret; - } +if (!dh_get_mac_from_eeprom (enetaddr,"eeprom0" )) + goto out; - ret =i2c_eeprom_read (dev,0xfa , enetaddr,0x6 ); -if (ret) { -printf ("Error reading configuration EEPROM!\n" ); -return ret; - } +printf ("%s: Unable to get mac address!\n" , __func__); +return -ENXIO ; -if (is_valid_ethaddr (enetaddr)) -eth_env_set_enetaddr ("ethaddr" , enetaddr); - -return 0 ; +out: +return eth_env_set_enetaddr ("ethaddr" , enetaddr); }int board_early_init_f (void )
@@ -188,7 +169,7 @@ int board_late_init(void)u32 hw_code;char buf[16 ]; -setup_dhcom_mac_from_fuse (); +dh_setup_mac_address (); hw_code =board_get_hwcode ();
Signature · 1 line
2.35.3
Re: [PATCH 2/4] ARM: imx6: DH: Use common mac address functions
Quoted 4 lines · Philip Oberfichtner
To reduce code duplication, let the imx6 based DH boards use the common code for setting up their mac addresses.
Signed-off-by Philip Oberfichtner <[email protected]>
PATCH 3/4 ARM: imx8: DH: Use common mac address functions
In reply to Philip OberfichtnerTo reduce code duplication, let the imx8 based DH boards use the common code for setting up their mac addresses.
---
.../dh_imx8mp/imx8mp_dhcom_pdk2.c | 121 +++++++----------- 1 file changed, 48 insertions(+), 73 deletions(-)
board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c +48 -73
@@ -16,6 +16,8 @@#include <miiphy.h> #include "lpddr4_timing.h" +#include "../common/dh_common.h" +#include "../common/dh_imx.h" DECLARE_GLOBAL_DATA_PTR ;
@@ -75,95 +77,68 @@ static void setup_fec(void)set_clk_enet (ENET_125MHZ ); } -static int setup_mac_address_from_eeprom (char *alias,char *env,bool odd) +static int dh_imx8_setup_ethaddr (void ) {unsigned char enetaddr[6 ]; -struct udevice *dev; -int ret, offset; - - offset =fdt_path_offset (gd->fdt_blob, alias); -if (offset <0 ) { -printf ("%s: No eeprom0 path offset\n" , __func__); -return offset; - } - - ret =uclass_get_device_by_of_offset (UCLASS_I2C_EEPROM , offset, &dev); -if (ret) { -printf ("Cannot find EEPROM!\n" ); -return ret; - } - - ret =i2c_eeprom_read (dev,0xfa , enetaddr,0x6 ); -if (ret) { -printf ("Error reading configuration EEPROM!\n" ); -return ret; - } + +if (dh_mac_is_in_env ("ethaddr" )) +return 0 ; + +if (!dh_imx_get_mac_from_fuse (enetaddr)) + goto out; + +if (!dh_get_mac_from_eeprom (enetaddr,"eeprom0" )) + goto out; + +return -ENXIO ; + +out: +return eth_env_set_enetaddr ("ethaddr" , enetaddr); +} + +static int dh_imx8_setup_eth1addr (void ) +{ +unsigned char enetaddr[6 ]; + +if (dh_mac_is_in_env ("eth1addr" )) +return 0 ; + +if (!dh_imx_get_mac_from_fuse (enetaddr)) + goto increment_out; + +if (!dh_get_mac_from_eeprom (enetaddr,"eeprom1" )) + goto out;/* * Populate second ethernet MAC from first ethernet EEPROM with MAC * address LSByte incremented by 1. This is only used on SoMs without * second ethernet EEPROM, i.e. early prototypes. */ -if (odd) - enetaddr[5 ]++; +if (!dh_get_mac_from_eeprom (enetaddr,"eeprom0" )) + goto increment_out; -eth_env_set_enetaddr (env, enetaddr); +return -ENXIO ; -return 0 ; +increment_out: + enetaddr[5 ]++; + +out: +return eth_env_set_enetaddr ("eth1addr" , enetaddr); } -static void setup_mac_address (void ) +int dh_setup_mac_address (void ) { -unsigned char enetaddr[6 ]; -bool skip_eth0 = false; -bool skip_eth1 = false;int ret; - ret =eth_env_get_enetaddr ("ethaddr" , enetaddr); -if (ret)/* ethaddr is already set */ - skip_eth0 = true; - - ret =eth_env_get_enetaddr ("eth1addr" , enetaddr); -if (ret)/* eth1addr is already set */ - skip_eth1 = true; + ret =dh_imx8_setup_ethaddr (); +if (ret) +printf ("%s: Unable to setup ethaddr! ret = %d\n" , __func__, ret); -/* Both MAC addresses are already set in U-Boot environment. */ -if (skip_eth0 && skip_eth1) -return ; + ret =dh_imx8_setup_eth1addr (); +if (ret) +printf ("%s: Unable to setup eth1addr! ret = %d\n" , __func__, ret); -/* -* If IIM fuses contain valid MAC address, use it. -* The IIM MAC address fuses are NOT programmed by default. -*/ -imx_get_mac_from_fuse (0 , enetaddr); -if (is_valid_ethaddr (enetaddr)) { -if (!skip_eth0) -eth_env_set_enetaddr ("ethaddr" , enetaddr); -/* -* The LSbit of MAC address in fuses is always 0, use the -* next consecutive MAC address for the second ethernet. -*/ - enetaddr[5 ]++; -if (!skip_eth1) -eth_env_set_enetaddr ("eth1addr" , enetaddr); -return ; - } - -/* Use on-SoM EEPROMs with pre-programmed MAC address. */ -if (!skip_eth0) { -/* We cannot do much more if this returns -ve . */ -setup_mac_address_from_eeprom ("eeprom0" ,"ethaddr" , false); - } - -if (!skip_eth1) { - ret =setup_mac_address_from_eeprom ("eeprom1" ,"eth1addr" , - false); -if (ret) {/* Second EEPROM might not be populated. */ -/* We cannot do much more if this returns -ve . */ -setup_mac_address_from_eeprom ("eeprom0" ,"eth1addr" , - true); - } - } +return ret; }int board_init (void )
@@ -176,7 +151,7 @@ int board_init(void)int board_late_init (void ) { -setup_mac_address (); +dh_setup_mac_address ();return 0 ; }
Signature · 1 line
2.35.3
Re: [PATCH 3/4] ARM: imx8: DH: Use common mac address functions
Quoted 4 lines · Philip Oberfichtner
To reduce code duplication, let the imx8 based DH boards use the common code for setting up their mac addresses.
Signed-off-by Philip Oberfichtner <[email protected]>
PATCH 4/4 ARM: stm32: DH: Use common mac address functions
In reply to Philip OberfichtnerTo reduce code duplication, let the stm32 based DH boards use the common code for setting up their mac addresses.
---
board/dhelectronics/dh_stm32mp1/board.c | 104 +++++++++++------------- 1 file changed, 47 insertions(+), 57 deletions(-)
board/dhelectronics/dh_stm32mp1/board.c +47 -57
@@ -43,6 +43,7 @@#include <usb/dwc2_udc.h> #include <watchdog.h> #include <dm/ofnode.h> +#include "../common/dh_common.h" #include "../../st/common/stpmic1.h" /* SYSCFG registers */
@@ -90,35 +91,18 @@ DECLARE_GLOBAL_DATA_PTR;#define KS_CIDER 0xC0#define CIDER_ID 0x8870 -int setup_mac_address (void ) -{ -unsigned char enetaddr[6 ]; -bool skip_eth0 = false; -bool skip_eth1 = false; -struct udevice *dev; -int off, ret; - ret =eth_env_get_enetaddr ("ethaddr" , enetaddr); -if (ret)/* ethaddr is already set */ - skip_eth0 = true; +static bool dh_stm32_mac_is_in_ks8851 (void ) +{ +int off; +u32 reg, cider, ccr; off =fdt_path_offset (gd->fdt_blob,"ethernet1" ); -if (off <0 ) { -/* ethernet1 is not present in the system */ - skip_eth1 = true; - goto out_set_ethaddr; - } +if (off <0 ) +return false; - ret =eth_env_get_enetaddr ("eth1addr" , enetaddr); -if (ret) { -/* eth1addr is already set */ - skip_eth1 = true; - goto out_set_ethaddr; - } - - ret =fdt_node_check_compatible (gd->fdt_blob, off,"micrel,ks8851-mll" ); -if (ret) - goto out_set_ethaddr; +if (fdt_node_check_compatible (gd->fdt_blob, off,"micrel,ks8851-mll" )) +return false;/* * KS8851 with EEPROM may use custom MAC from EEPROM, read
@@ -126,56 +110,62 @@ int setup_mac_address(void)* is present. If EEPROM is present, it must contain valid * MAC address. */ -u32 reg, cider, ccr; reg =fdt_get_base_address (gd->fdt_blob, off);if (!reg) - goto out_set_ethaddr; +return false;writew (KS_BE0 |KS_BE1 |KS_CIDER , reg +2 ); cider =readw (reg); -if ((cider &0xfff0 ) !=CIDER_ID ) { - skip_eth1 = true; - goto out_set_ethaddr; - } +if ((cider &0xfff0 ) !=CIDER_ID ) +return true;writew (KS_BE0 |KS_BE1 |KS_CCR , reg +2 ); ccr =readw (reg); -if (ccr &KS_CCR_EEPROM ) { - skip_eth1 = true; - goto out_set_ethaddr; - } +if (ccr &KS_CCR_EEPROM ) +return true; + +return false; +} + +static int dh_stm32_setup_ethaddr (void ) +{ +unsigned char enetaddr[6 ]; -out_set_ethaddr: -if (skip_eth0 && skip_eth1) +if (dh_mac_is_in_env ("ethaddr" ))return 0 ; - off =fdt_path_offset (gd->fdt_blob,"eeprom0" ); -if (off <0 ) { -printf ("%s: No eeprom0 path offset\n" , __func__); -return off; - } +if (!dh_get_mac_from_eeprom (enetaddr,"eeprom0" )) +return eth_env_set_enetaddr ("ethaddr" , enetaddr); - ret =uclass_get_device_by_of_offset (UCLASS_I2C_EEPROM , off, &dev); -if (ret) { -printf ("Cannot find EEPROM!\n" ); -return ret; - } +return -ENXIO ; +} - ret =i2c_eeprom_read (dev,0xfa , enetaddr,0x6 ); -if (ret) { -printf ("Error reading configuration EEPROM!\n" ); -return ret; - } +static int dh_stm32_setup_eth1addr (void ) +{ +unsigned char enetaddr[6 ]; -if (is_valid_ethaddr (enetaddr)) { -if (!skip_eth0) -eth_env_set_enetaddr ("ethaddr" , enetaddr); +if (dh_mac_is_in_env ("eth1addr" )) +return 0 ; + +if (dh_stm32_mac_is_in_ks8851 ()) +return 0 ; +if (!dh_get_mac_from_eeprom (enetaddr,"eeprom0" )) { enetaddr[5 ]++; -if (!skip_eth1) -eth_env_set_enetaddr ("eth1addr" , enetaddr); +return eth_env_set_enetaddr ("eth1addr" , enetaddr); } +return -ENXIO ; +} + +int setup_mac_address (void ) +{ +if (dh_stm32_setup_ethaddr ()) +printf ("%s: Unable to setup ethaddr!\n" , __func__); + +if (dh_stm32_setup_eth1addr ()) +printf ("%s: Unable to setup eth1addr!\n" , __func__); +return 0 ; }
Signature · 1 line
2.35.3
Re: [PATCH 4/4] ARM: stm32: DH: Use common mac address functions
Quoted 4 lines · Philip Oberfichtner
To reduce code duplication, let the stm32 based DH boards use the common code for setting up their mac addresses.
Signed-off-by Philip Oberfichtner <[email protected]>
"MAC address" is written in capitals , please fix the lowercase 'mac' all over the place and make it uppercase (except for function names, there is should stay lowercase).