From: Enrico Leto Date: Wed, 24 Jan 2024 14:43:50 +0000 (+0100) Subject: siemens: eeprom: simplify setup & read X-Git-Url: http://git.dujemihanovic.xyz/?a=commitdiff_plain;h=e9ef9a137741275ba656607cb8d1a66766c739e5;p=u-boot.git siemens: eeprom: simplify setup & read Since we have boards using the driver model or not for i2c, use abstraction function to probe the i2c, check the EEPROM and read from EEPROM. Signed-off-by: Enrico Leto --- diff --git a/board/siemens/capricorn/Makefile b/board/siemens/capricorn/Makefile index d5846cc8e3..4dafac1082 100644 --- a/board/siemens/capricorn/Makefile +++ b/board/siemens/capricorn/Makefile @@ -4,6 +4,7 @@ # obj-y += board.o +obj-y += ../common/eeprom.o ifdef CONFIG_SPL_BUILD obj-y += spl.o diff --git a/board/siemens/common/board.c b/board/siemens/common/board.c index 420c496581..e412858c4a 100644 --- a/board/siemens/common/board.c +++ b/board/siemens/common/board.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -39,6 +38,7 @@ DECLARE_GLOBAL_DATA_PTR; + #ifdef CONFIG_SPL_BUILD void set_uart_mux_conf(void) { @@ -49,12 +49,13 @@ void set_mux_conf_regs(void) { /* Initalize the board header */ enable_i2c0_pin_mux(); - i2c_set_bus_num(0); /* enable early the console */ gd->baudrate = CONFIG_BAUDRATE; serial_init(); gd->have_console = 1; + + siemens_ee_setup(); if (read_eeprom() < 0) puts("Could not get board ID.\n"); @@ -79,8 +80,7 @@ int board_init(void) #if defined(CONFIG_HW_WATCHDOG) hw_watchdog_init(); #endif /* defined(CONFIG_HW_WATCHDOG) */ - i2c_set_bus_num(0); - if (read_eeprom() < 0) + if (siemens_ee_setup() < 0) puts("Could not get board ID.\n"); #ifdef CONFIG_MACH_TYPE gd->bd->bi_arch_number = CONFIG_MACH_TYPE; diff --git a/board/siemens/common/eeprom.c b/board/siemens/common/eeprom.c new file mode 100644 index 0000000000..fc93df91d5 --- /dev/null +++ b/board/siemens/common/eeprom.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * + * Read EEPROM data + * (C) Copyright 2024 Siemens AG + */ + +#include +#include +#include "eeprom.h" + +#if CONFIG_IS_ENABLED(DM_I2C) +static struct udevice *i2c_dev; +#endif + +/* Probe I2C and set-up EEPROM */ +int siemens_ee_setup(void) +{ +#if CONFIG_IS_ENABLED(DM_I2C) + struct udevice *bus; + int ret; + + ret = uclass_get_device_by_seq(UCLASS_I2C, SIEMENS_EE_I2C_BUS, &bus); + if (ret) + goto err; + + ret = dm_i2c_probe(bus, SIEMENS_EE_I2C_ADDR, 0, &i2c_dev); + if (ret) + goto err; + if (i2c_set_chip_offset_len(i2c_dev, 2)) + goto err; +#else + i2c_set_bus_num(SIEMENS_EE_I2C_BUS); + if (i2c_probe(SIEMENS_EE_I2C_ADDR)) + goto err; +#endif + return 0; + +err: + printf("Could not probe the EEPROM; something fundamentally wrong on the I2C bus.\n"); + return 1; +} + +/* Read data from EEPROM */ +int siemens_ee_read_data(uint address, uchar *buffer, int len) +{ +#if CONFIG_IS_ENABLED(DM_I2C) + return dm_i2c_read(i2c_dev, address, buffer, len); +#else + return i2c_read(SIEMENS_EE_I2C_ADDR, address, 2, buffer, len); +#endif +} diff --git a/board/siemens/common/eeprom.h b/board/siemens/common/eeprom.h index a21d42789a..a5ef5abbaf 100644 --- a/board/siemens/common/eeprom.h +++ b/board/siemens/common/eeprom.h @@ -18,4 +18,7 @@ #define SIEMENS_EE_ADDR_CHIP 0x120 #define SIEMENS_EE_ADDR_FACTORYSET 0x400 +int siemens_ee_setup(void); +int siemens_ee_read_data(uint address, uchar *buffer, int len); + #endif /* _COMMON_EEPROM_H_ */ diff --git a/board/siemens/common/factoryset.c b/board/siemens/common/factoryset.c index b5befab9f5..4a1277340a 100644 --- a/board/siemens/common/factoryset.c +++ b/board/siemens/common/factoryset.c @@ -148,39 +148,14 @@ int factoryset_read_eeprom(int i2c_addr) int i, pages = 0, size = 0; unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH]; unsigned char *cp, *cp1; -#if CONFIG_IS_ENABLED(DM_I2C) - struct udevice *bus, *dev; - int ret; -#endif #if defined(CONFIG_DFU_OVER_USB) factory_dat.usb_vendor_id = CONFIG_USB_GADGET_VENDOR_NUM; factory_dat.usb_product_id = CONFIG_USB_GADGET_PRODUCT_NUM; #endif -#if CONFIG_IS_ENABLED(DM_I2C) - ret = uclass_get_device_by_seq(UCLASS_I2C, SIEMENS_EE_I2C_BUS, &bus); - if (ret) - goto err; - - ret = dm_i2c_probe(bus, i2c_addr, 0, &dev); - if (ret) - goto err; - - ret = i2c_set_chip_offset_len(dev, 2); - if (ret) - goto err; - - ret = dm_i2c_read(dev, SIEMENS_EE_ADDR_FACTORYSET, hdr, sizeof(hdr)); - if (ret) - goto err; -#else - if (i2c_probe(i2c_addr)) - goto err; - - if (i2c_read(i2c_addr, SIEMENS_EE_ADDR_FACTORYSET, 2, hdr, sizeof(hdr))) + if (siemens_ee_read_data(SIEMENS_EE_ADDR_FACTORYSET, hdr, sizeof(hdr))) goto err; -#endif if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) { printf("FactorySet is not right in eeprom.\n"); @@ -201,33 +176,16 @@ int factoryset_read_eeprom(int i2c_addr) * data after every time we got a record from eeprom */ debug("Read eeprom page :\n"); - for (i = 0; i < pages; i++) { -#if CONFIG_IS_ENABLED(DM_I2C) - ret = dm_i2c_read(dev, (OFF_PG + i) * EEPR_PG_SZ, - eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ); - if (ret) + for (i = 0; i < pages; i++) + if (siemens_ee_read_data((OFF_PG + i) * EEPR_PG_SZ, + eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ)) goto err; -#else - if (i2c_read(i2c_addr, (OFF_PG + i) * EEPR_PG_SZ, 2, - eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ)) - goto err; -#endif - } - if (size % EEPR_PG_SZ) { -#if CONFIG_IS_ENABLED(DM_I2C) - ret = dm_i2c_read(dev, (OFF_PG + pages) * EEPR_PG_SZ, - eeprom_buf + (pages * EEPR_PG_SZ), - size % EEPR_PG_SZ); - if (ret) - goto err; -#else - if (i2c_read(i2c_addr, (OFF_PG + pages) * EEPR_PG_SZ, 2, - eeprom_buf + (pages * EEPR_PG_SZ), - (size % EEPR_PG_SZ))) + if (size % EEPR_PG_SZ) + if (siemens_ee_read_data((OFF_PG + pages) * EEPR_PG_SZ, + eeprom_buf + (pages * EEPR_PG_SZ), + size % EEPR_PG_SZ)) goto err; -#endif - } /* we do below just for eeprom align */ for (i = 0; i < size; i++) @@ -249,9 +207,8 @@ int factoryset_read_eeprom(int i2c_addr) #if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB) /* get mac address for WLAN */ - ret = get_factory_record_val(cp, size, (uchar *)"WLAN1", (uchar *)"mac", - buf, MAX_STRING_LENGTH); - if (ret > 0) { + if (get_factory_record_val(cp, size, (uchar *)"WLAN1", (uchar *)"mac", + buf, MAX_STRING_LENGTH) > 0) { cp1 = buf; for (i = 0; i < 6; i++) { factory_dat.mac_wlan[i] = hextoul((char *)cp1, NULL); diff --git a/board/siemens/draco/Makefile b/board/siemens/draco/Makefile index e94456ab1c..1d0cb82bcb 100644 --- a/board/siemens/draco/Makefile +++ b/board/siemens/draco/Makefile @@ -14,6 +14,7 @@ obj-y := mux.o endif obj-y += board.o +obj-y += ../common/eeprom.o ifndef CONFIG_SPL_BUILD obj-y += ../common/factoryset.o endif diff --git a/board/siemens/draco/board.c b/board/siemens/draco/board.c index 367a300b89..30ad9d7ab3 100644 --- a/board/siemens/draco/board.c +++ b/board/siemens/draco/board.c @@ -138,8 +138,8 @@ static int draco_read_nand_geometry(void) struct am335x_nand_geometry geo; /* Read NAND geometry */ - if (i2c_read(SIEMENS_EE_I2C_ADDR, SIEMENS_EE_ADDR_NAND_GEO, 2, - (uchar *)&geo, sizeof(struct am335x_nand_geometry))) { + if (siemens_ee_read_data(SIEMENS_EE_ADDR_NAND_GEO, (uchar *)&geo, + sizeof(struct am335x_nand_geometry))) { printf("Could not read the NAND geomtery; something fundamentally wrong on the I2C bus.\n"); return -EIO; } @@ -155,27 +155,21 @@ static int draco_read_nand_geometry(void) return 0; } +#ifdef CONFIG_SPL_BUILD /* * Read header information from EEPROM into global structure. */ static int read_eeprom(void) { - /* Check if baseboard eeprom is available */ - if (i2c_probe(SIEMENS_EE_I2C_ADDR)) { - printf("Could not probe the EEPROM; something fundamentally wrong on the I2C bus.\n"); - return 1; - } - -#ifdef CONFIG_SPL_BUILD /* Read Siemens eeprom data (DDR3) */ - if (i2c_read(SIEMENS_EE_I2C_ADDR, SIEMENS_EE_ADDR_DDR3, 2, - (uchar *)&settings.ddr3, sizeof(struct ddr3_data))) { + if (siemens_ee_read_data(SIEMENS_EE_ADDR_DDR3, (uchar *)&settings.ddr3, + sizeof(struct ddr3_data))) { printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\nUse default DDR3 timings\n"); set_default_ddr3_timings(); } /* Read Siemens eeprom data (CHIP) */ - if (i2c_read(SIEMENS_EE_I2C_ADDR, SIEMENS_EE_ADDR_CHIP, 2, - (uchar *)&settings.chip, sizeof(settings.chip))) + if (siemens_ee_read_data(SIEMENS_EE_ADDR_CHIP, (uchar *)&settings.chip, + sizeof(settings.chip))) printf("Could not read chip settings\n"); if (ddr3_default.magic == settings.ddr3.magic && @@ -199,11 +193,8 @@ static int read_eeprom(void) print_ddr3_timings(); return draco_read_nand_geometry(); -#endif - return 0; } -#ifdef CONFIG_SPL_BUILD static void board_init_ddr(void) { struct emif_regs draco_ddr3_emif_reg_data = { diff --git a/board/siemens/draco/mux.c b/board/siemens/draco/mux.c index 2632f05033..56d8f45c34 100644 --- a/board/siemens/draco/mux.c +++ b/board/siemens/draco/mux.c @@ -16,7 +16,7 @@ #include #include #include -#include "board.h" +#include "eeprom.h" static struct module_pin_mux uart0_pin_mux[] = { {OFFSET(uart0_rxd), (MODE(0) | PULLUP_EN | RXACTIVE)}, /* UART0_RXD */ diff --git a/board/siemens/pxm2/Makefile b/board/siemens/pxm2/Makefile index e94456ab1c..1d0cb82bcb 100644 --- a/board/siemens/pxm2/Makefile +++ b/board/siemens/pxm2/Makefile @@ -14,6 +14,7 @@ obj-y := mux.o endif obj-y += board.o +obj-y += ../common/eeprom.o ifndef CONFIG_SPL_BUILD obj-y += ../common/factoryset.o endif diff --git a/board/siemens/pxm2/board.c b/board/siemens/pxm2/board.c index 9e85d9b921..16b0d3df75 100644 --- a/board/siemens/pxm2/board.c +++ b/board/siemens/pxm2/board.c @@ -161,7 +161,6 @@ void spl_siemens_board_init(void) printf("voltage update failed\n"); } } -#endif /* if def CONFIG_SPL_BUILD */ int read_eeprom(void) { @@ -169,6 +168,7 @@ int read_eeprom(void) return 0; } +#endif /* if def CONFIG_SPL_BUILD */ #if (defined(CONFIG_DRIVER_TI_CPSW) && !defined(CONFIG_SPL_BUILD)) || \ (defined(CONFIG_SPL_ETH) && defined(CONFIG_SPL_BUILD)) diff --git a/board/siemens/pxm2/mux.c b/board/siemens/pxm2/mux.c index d21ef47771..07cf567592 100644 --- a/board/siemens/pxm2/mux.c +++ b/board/siemens/pxm2/mux.c @@ -17,7 +17,7 @@ #include #include #include -#include "board.h" +#include "eeprom.h" static struct module_pin_mux uart0_pin_mux[] = { {OFFSET(uart0_rxd), (MODE(0) | PULLUP_EN | RXACTIVE)}, /* UART0_RXD */ diff --git a/board/siemens/rut/Makefile b/board/siemens/rut/Makefile index e94456ab1c..1d0cb82bcb 100644 --- a/board/siemens/rut/Makefile +++ b/board/siemens/rut/Makefile @@ -14,6 +14,7 @@ obj-y := mux.o endif obj-y += board.o +obj-y += ../common/eeprom.o ifndef CONFIG_SPL_BUILD obj-y += ../common/factoryset.o endif diff --git a/board/siemens/rut/board.c b/board/siemens/rut/board.c index 49318b3203..d9e84db03e 100644 --- a/board/siemens/rut/board.c +++ b/board/siemens/rut/board.c @@ -39,6 +39,7 @@ #include "../common/eeprom.h" #include "../common/factoryset.h" +#ifdef CONFIG_SPL_BUILD /* * Read header information from EEPROM into global structure. */ @@ -47,7 +48,6 @@ static int read_eeprom(void) return 0; } -#ifdef CONFIG_SPL_BUILD static void board_init_ddr(void) { struct emif_regs rut_ddr3_emif_reg_data = {