[PATCH v2 1/6] board: phytec: Add common PHYTEC SoM detection

2023-10-16 Thread sbabic
> Recent shipped PHYTEC SoMs come with an i2c  EEPROM containing
> information about the hardware such as board revision and variant.
> This can be used for RAM detection and loading device tree overlays
> during kernel start.
> Signed-off-by: Teresa Remmet 
> Reviewed-by: Yannic Moog 
> Tested-by: Yannic Moog 
> Reviewed-by: Fabio Estevam 
Applied to u-boot-imx, master, thanks !

Best regards,
Stefano Babic

-- 
=
DENX Software Engineering GmbH,Managing Director: Erika Unter  
HRB 165235 Munich,   Office: Kirchenstr.5, 82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sba...@denx.de
=


Re: [PATCH v2 1/6] board: phytec: Add common PHYTEC SoM detection

2023-10-07 Thread Fabio Estevam
On Mon, Aug 28, 2023 at 9:32 AM Yannic Moog  wrote:
>
> On Thu, 2023-08-17 at 10:57 +0200, Teresa Remmet wrote:
> > Recent shipped PHYTEC SoMs come with an i2c  EEPROM containing
> > information about the hardware such as board revision and variant.
> > This can be used for RAM detection and loading device tree overlays
> > during kernel start.
> >
> > Signed-off-by: Teresa Remmet 
>
> Reviewed-by: Yannic Moog 
> Tested-by: Yannic Moog 

For the series:

Reviewed-by: Fabio Estevam 


Re: [PATCH v2 1/6] board: phytec: Add common PHYTEC SoM detection

2023-08-28 Thread Yannic Moog
On Thu, 2023-08-17 at 10:57 +0200, Teresa Remmet wrote:
> Recent shipped PHYTEC SoMs come with an i2c  EEPROM containing
> information about the hardware such as board revision and variant.
> This can be used for RAM detection and loading device tree overlays
> during kernel start.
> 
> Signed-off-by: Teresa Remmet 

Reviewed-by: Yannic Moog 
Tested-by: Yannic Moog 

> ---
> Changes in v2:
> - none
> ---
>  board/phytec/common/Kconfig    |   5 +
>  board/phytec/common/Makefile   |  10 ++
>  board/phytec/common/phytec_som_detection.c | 188
> +
>  board/phytec/common/phytec_som_detection.h | 104 
>  4 files changed, 307 insertions(+)
>  create mode 100644 board/phytec/common/Kconfig
>  create mode 100644 board/phytec/common/Makefile
>  create mode 100644 board/phytec/common/phytec_som_detection.c
>  create mode 100644 board/phytec/common/phytec_som_detection.h
> 
> diff --git a/board/phytec/common/Kconfig b/board/phytec/common/Kconfig
> new file mode 100644
> index ..d614d45b1d60
> --- /dev/null
> +++ b/board/phytec/common/Kconfig
> @@ -0,0 +1,5 @@
> +config PHYTEC_SOM_DETECTION
> +   bool "Support SoM detection for PHYTEC platforms"
> +   select SPL_CRC8 if SPL
> +   help
> +  Support of I2C EEPROM based SoM detection.
> diff --git a/board/phytec/common/Makefile
> b/board/phytec/common/Makefile
> new file mode 100644
> index ..5fe8725ef684
> --- /dev/null
> +++ b/board/phytec/common/Makefile
> @@ -0,0 +1,10 @@
> +# SPDX-License-Identifier: GPL-2.0+
> +# Copyright (C) 2023 PHYTEC Messtechnik GmbH
> +# Author: Teresa Remmet 
> +
> +ifdef CONFIG_SPL_BUILD
> +# necessary to create built-in.o
> +obj- := __dummy__.o
> +endif
> +
> +obj-$(CONFIG_PHYTEC_SOM_DETECTION) += phytec_som_detection.o
> diff --git a/board/phytec/common/phytec_som_detection.c
> b/board/phytec/common/phytec_som_detection.c
> new file mode 100644
> index ..366bdd4ace4b
> --- /dev/null
> +++ b/board/phytec/common/phytec_som_detection.c
> @@ -0,0 +1,188 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2023 PHYTEC Messtechnik GmbH
> + * Author: Teresa Remmet 
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include "phytec_som_detection.h"
> +
> +struct phytec_eeprom_data eeprom_data;
> +
> +int phytec_eeprom_data_setup_fallback(struct phytec_eeprom_data
> *data,
> + int bus_num, int addr, int
> addr_fallback)
> +{
> +   int ret;
> +
> +   ret = phytec_eeprom_data_init(data, bus_num, addr);
> +   if (ret) {
> +   pr_err("%s: init failed. Trying fall back address
> 0x%x\n",
> +  __func__, addr_fallback);
> +   ret = phytec_eeprom_data_init(data, bus_num,
> addr_fallback);
> +   }
> +
> +   if (ret)
> +   pr_err("%s: EEPROM data init failed\n", __func__);
> +
> +   return ret;
> +}
> +
> +int phytec_eeprom_data_setup(struct phytec_eeprom_data *data,
> +    int bus_num, int addr)
> +{
> +   int ret;
> +
> +   ret = phytec_eeprom_data_init(data, bus_num, addr);
> +   if (ret)
> +   pr_err("%s: EEPROM data init failed\n", __func__);
> +
> +   return ret;
> +}
> +
> +int phytec_eeprom_data_init(struct phytec_eeprom_data *data,
> +   int bus_num, int addr)
> +{
> +   int ret, i;
> +   unsigned int crc;
> +   int *ptr;
> +
> +   if (!data)
> +   data = _data;
> +
> +#if CONFIG_IS_ENABLED(DM_I2C)
> +   struct udevice *dev;
> +
> +   ret = i2c_get_chip_for_busnum(bus_num, addr, 2, );
> +   if (ret) {
> +   pr_err("%s: i2c EEPROM not found: %i.\n", __func__,
> ret);
> +   return ret;
> +   }
> +
> +   ret = dm_i2c_read(dev, 0, (uint8_t *)data,
> + sizeof(struct phytec_eeprom_data));
> +   if (ret) {
> +   pr_err("%s: Unable to read EEPROM data\n", __func__);
> +   return ret;
> +   }
> +#else
> +   i2c_set_bus_num(bus_num);
> +   ret = i2c_read(addr, 0, 2, (uint8_t *)data,
> +  sizeof(struct phytec_eeprom_data));
> +#endif
> +
> +   if (data->api_rev == 0xff) {
> +   pr_err("%s: EEPROM is not flashed. Prototype?\n",
> __func__);
> +   return -EINVAL;
> +   }
> +
> +   ptr = (int *)data;
> +   for (i = 0; i < sizeof(struct phytec_eeprom_data); i +=
> sizeof(ptr))
> +   if (*ptr != 0x0)
> +   break;
> +
> +   if (i == sizeof(struct phytec_eeprom_data)) {
> +   pr_err("%s: EEPROM data is all zero. Erased?\n",
> __func__);
> +   return -EINVAL;
> +   }
> +
> +   /* We are done here for early revisions */
> +   if (data->api_rev <= PHYTEC_API_REV1)
> +   return 0;
> +
> +   crc = crc8(0, (const 

[PATCH v2 1/6] board: phytec: Add common PHYTEC SoM detection

2023-08-17 Thread Teresa Remmet
Recent shipped PHYTEC SoMs come with an i2c  EEPROM containing
information about the hardware such as board revision and variant.
This can be used for RAM detection and loading device tree overlays
during kernel start.

Signed-off-by: Teresa Remmet 
---
Changes in v2:
- none
---
 board/phytec/common/Kconfig|   5 +
 board/phytec/common/Makefile   |  10 ++
 board/phytec/common/phytec_som_detection.c | 188 +
 board/phytec/common/phytec_som_detection.h | 104 
 4 files changed, 307 insertions(+)
 create mode 100644 board/phytec/common/Kconfig
 create mode 100644 board/phytec/common/Makefile
 create mode 100644 board/phytec/common/phytec_som_detection.c
 create mode 100644 board/phytec/common/phytec_som_detection.h

diff --git a/board/phytec/common/Kconfig b/board/phytec/common/Kconfig
new file mode 100644
index ..d614d45b1d60
--- /dev/null
+++ b/board/phytec/common/Kconfig
@@ -0,0 +1,5 @@
+config PHYTEC_SOM_DETECTION
+   bool "Support SoM detection for PHYTEC platforms"
+   select SPL_CRC8 if SPL
+   help
+  Support of I2C EEPROM based SoM detection.
diff --git a/board/phytec/common/Makefile b/board/phytec/common/Makefile
new file mode 100644
index ..5fe8725ef684
--- /dev/null
+++ b/board/phytec/common/Makefile
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright (C) 2023 PHYTEC Messtechnik GmbH
+# Author: Teresa Remmet 
+
+ifdef CONFIG_SPL_BUILD
+# necessary to create built-in.o
+obj- := __dummy__.o
+endif
+
+obj-$(CONFIG_PHYTEC_SOM_DETECTION) += phytec_som_detection.o
diff --git a/board/phytec/common/phytec_som_detection.c 
b/board/phytec/common/phytec_som_detection.c
new file mode 100644
index ..366bdd4ace4b
--- /dev/null
+++ b/board/phytec/common/phytec_som_detection.c
@@ -0,0 +1,188 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2023 PHYTEC Messtechnik GmbH
+ * Author: Teresa Remmet 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "phytec_som_detection.h"
+
+struct phytec_eeprom_data eeprom_data;
+
+int phytec_eeprom_data_setup_fallback(struct phytec_eeprom_data *data,
+ int bus_num, int addr, int addr_fallback)
+{
+   int ret;
+
+   ret = phytec_eeprom_data_init(data, bus_num, addr);
+   if (ret) {
+   pr_err("%s: init failed. Trying fall back address 0x%x\n",
+  __func__, addr_fallback);
+   ret = phytec_eeprom_data_init(data, bus_num, addr_fallback);
+   }
+
+   if (ret)
+   pr_err("%s: EEPROM data init failed\n", __func__);
+
+   return ret;
+}
+
+int phytec_eeprom_data_setup(struct phytec_eeprom_data *data,
+int bus_num, int addr)
+{
+   int ret;
+
+   ret = phytec_eeprom_data_init(data, bus_num, addr);
+   if (ret)
+   pr_err("%s: EEPROM data init failed\n", __func__);
+
+   return ret;
+}
+
+int phytec_eeprom_data_init(struct phytec_eeprom_data *data,
+   int bus_num, int addr)
+{
+   int ret, i;
+   unsigned int crc;
+   int *ptr;
+
+   if (!data)
+   data = _data;
+
+#if CONFIG_IS_ENABLED(DM_I2C)
+   struct udevice *dev;
+
+   ret = i2c_get_chip_for_busnum(bus_num, addr, 2, );
+   if (ret) {
+   pr_err("%s: i2c EEPROM not found: %i.\n", __func__, ret);
+   return ret;
+   }
+
+   ret = dm_i2c_read(dev, 0, (uint8_t *)data,
+ sizeof(struct phytec_eeprom_data));
+   if (ret) {
+   pr_err("%s: Unable to read EEPROM data\n", __func__);
+   return ret;
+   }
+#else
+   i2c_set_bus_num(bus_num);
+   ret = i2c_read(addr, 0, 2, (uint8_t *)data,
+  sizeof(struct phytec_eeprom_data));
+#endif
+
+   if (data->api_rev == 0xff) {
+   pr_err("%s: EEPROM is not flashed. Prototype?\n", __func__);
+   return -EINVAL;
+   }
+
+   ptr = (int *)data;
+   for (i = 0; i < sizeof(struct phytec_eeprom_data); i += sizeof(ptr))
+   if (*ptr != 0x0)
+   break;
+
+   if (i == sizeof(struct phytec_eeprom_data)) {
+   pr_err("%s: EEPROM data is all zero. Erased?\n", __func__);
+   return -EINVAL;
+   }
+
+   /* We are done here for early revisions */
+   if (data->api_rev <= PHYTEC_API_REV1)
+   return 0;
+
+   crc = crc8(0, (const unsigned char *)data,
+  sizeof(struct phytec_eeprom_data));
+   debug("%s: crc: %x\n", __func__, crc);
+
+   if (crc) {
+   pr_err("%s: CRC mismatch. EEPROM data is not usable\n",
+  __func__);
+   return -EINVAL;
+   }
+
+   return 0;
+}
+
+void __maybe_unused phytec_print_som_info(struct phytec_eeprom_data *data)
+{
+   struct phytec_api2_data