public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Kory Maincent <kory.maincent@bootlin.com>
To: u-boot@lists.denx.de
Subject: [PATCH v5 07/10] arm: sunxi: add support for DIP detection to CHIP board
Date: Tue,  4 May 2021 19:31:27 +0200	[thread overview]
Message-ID: <20210504173130.22869-8-kory.maincent@bootlin.com> (raw)
In-Reply-To: <20210504173130.22869-1-kory.maincent@bootlin.com>

Add the extension_board_scan specific function to scan the information
of the EEPROM on one-wire and fill the extension struct.
Add the Kconfig symbol to enable the needs to detect DIPs.

Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
Reviewed-by: Maxime Ripard <maxime@cerno.tech>
---

Change since v1:
- Replace TARGET_CHIP options by CHIP_DIP_SCAN

Change since v2:
- Fix typo
- Place the Kconfig symbol in that patch

Change since v3:
- Replace imply CMD_EXTENSION by select option. Remove the build test on
  CMD_EXTENSION in the chip.c file.

 arch/arm/mach-sunxi/Kconfig |   9 ++++
 board/sunxi/Makefile        |   1 +
 board/sunxi/chip.c          | 100 ++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 board/sunxi/chip.c

diff --git a/arch/arm/mach-sunxi/Kconfig b/arch/arm/mach-sunxi/Kconfig
index 8e9012dbbf..bc8509b72a 100644
--- a/arch/arm/mach-sunxi/Kconfig
+++ b/arch/arm/mach-sunxi/Kconfig
@@ -1089,3 +1089,12 @@ config BLUETOOTH_DT_DEVICE_FIXUP
 	  flipped elsewise.
 
 endif
+
+config CHIP_DIP_SCAN
+	bool "Enable DIPs detection for CHIP board"
+	select SUPPORT_EXTENSION_SCAN
+	select W1
+	select W1_GPIO
+	select W1_EEPROM
+	select W1_EEPROM_DS24XXX
+	select CMD_EXTENSION
diff --git a/board/sunxi/Makefile b/board/sunxi/Makefile
index c4e13f8c38..d96b7897b6 100644
--- a/board/sunxi/Makefile
+++ b/board/sunxi/Makefile
@@ -11,3 +11,4 @@ obj-$(CONFIG_SUN7I_GMAC)	+= gmac.o
 obj-$(CONFIG_MACH_SUN4I)	+= dram_sun4i_auto.o
 obj-$(CONFIG_MACH_SUN5I)	+= dram_sun5i_auto.o
 obj-$(CONFIG_MACH_SUN7I)	+= dram_sun5i_auto.o
+obj-$(CONFIG_CHIP_DIP_SCAN)	+= chip.o
diff --git a/board/sunxi/chip.c b/board/sunxi/chip.c
new file mode 100644
index 0000000000..cde04bebe9
--- /dev/null
+++ b/board/sunxi/chip.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2021
+ * K?ry Maincent, Bootlin, <kory.maincent@bootlin.com>
+ * Based on initial code from Maxime Ripard
+ */
+
+#include <common.h>
+#include <malloc.h>
+#include <dm.h>
+#include <w1.h>
+#include <w1-eeprom.h>
+#include <dm/device-internal.h>
+
+#include <asm/arch/gpio.h>
+
+#include <extension_board.h>
+
+#define for_each_w1_device(b, d) \
+	for (device_find_first_child(b, d); *d; device_find_next_child(d))
+
+#define dip_convert(field)						\
+	(								\
+		(sizeof(field) == 1) ? field :				\
+		(sizeof(field) == 2) ? be16_to_cpu(field) :		\
+		(sizeof(field) == 4) ? be32_to_cpu(field) :		\
+		-1							\
+	)
+
+#define DIP_MAGIC	0x50494843	/* CHIP */
+
+struct dip_w1_header {
+	u32     magic;                  /* CHIP */
+	u8      version;                /* spec version */
+	u32     vendor_id;
+	u16     product_id;
+	u8      product_version;
+	char    vendor_name[32];
+	char    product_name[32];
+	u8      rsvd[36];               /* rsvd for future spec versions */
+	u8      data[16];               /* user data, per-dip specific */
+} __packed;
+
+int extension_board_scan(struct list_head *extension_list)
+{
+	struct extension *dip;
+	struct dip_w1_header w1_header;
+	struct udevice *bus, *dev;
+	u32 vid;
+	u16 pid;
+	int ret;
+
+	int num_dip = 0;
+
+	sunxi_gpio_set_pull(SUNXI_GPD(2), SUNXI_GPIO_PULL_UP);
+
+	ret = w1_get_bus(0, &bus);
+	if (ret) {
+		printf("one wire interface not found\n");
+		return 0;
+	}
+
+	for_each_w1_device(bus, &dev) {
+		if (w1_get_device_family(dev) != W1_FAMILY_DS2431)
+			continue;
+
+		ret = device_probe(dev);
+		if (ret) {
+			printf("Couldn't probe device %s: error %d",
+			       dev->name, ret);
+			continue;
+		}
+
+		w1_eeprom_read_buf(dev, 0, (u8 *)&w1_header, sizeof(w1_header));
+
+		if (w1_header.magic != DIP_MAGIC)
+			continue;
+
+		vid = dip_convert(w1_header.vendor_id);
+		pid = dip_convert(w1_header.product_id);
+
+		printf("DIP: %s (0x%x) from %s (0x%x)\n",
+		       w1_header.product_name, pid,
+		       w1_header.vendor_name, vid);
+
+		dip = calloc(1, sizeof(struct extension));
+		if (!dip) {
+			printf("Error in memory allocation\n");
+			return num_dip;
+		}
+
+		snprintf(dip->overlay, sizeof(dip->overlay), "dip-%x-%x.dtbo",
+			 vid, pid);
+		strncpy(dip->name, w1_header.product_name, 32);
+		strncpy(dip->owner, w1_header.vendor_name, 32);
+		list_add_tail(&dip->list, extension_list);
+		num_dip++;
+	}
+	return num_dip;
+}
-- 
2.17.1

  parent reply	other threads:[~2021-05-04 17:31 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-04 17:31 [PATCH v5 00/10] Add support for extension boards detection and DT overlays application Kory Maincent
2021-05-04 17:31 ` [PATCH v5 01/10] fdt_support: move fdt_valid from cmd_fdt.c to fdt_support.c Kory Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` [PATCH v5 02/10] cmd: add support for a new "extension" command Kory Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` [PATCH v5 03/10] pytest: add sandbox test for " Kory Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` [PATCH v5 04/10] ti/common: add support for extension_scan_board function Kory Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` [PATCH v5 05/10] am57xx: add support for cape detect functionality Kory Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` [PATCH v5 06/10] w1: replace dt detection by automatic detection Kory Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` Kory Maincent [this message]
2021-05-13 11:01   ` [PATCH v5 07/10] arm: sunxi: add support for DIP detection to CHIP board Andre Przywara
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` [PATCH v5 08/10] configs: CHIP: add support for DIP detect functionality Kory Maincent
2021-05-13 11:03   ` Andre Przywara
2021-05-17 10:19     ` Köry Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` [PATCH v5 09/10] arm: am335x: add support for i2c2 bus Kory Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-04 17:31 ` [PATCH v5 10/10] am335x: add support for cape detect functionality Kory Maincent
2021-05-13 19:06   ` Tom Rini
2021-05-12 20:37 ` [PATCH v5 00/10] Add support for extension boards detection and DT overlays application Thomas Petazzoni
2021-05-13 12:18   ` Tom Rini
2021-05-13 12:29     ` Thomas Petazzoni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210504173130.22869-8-kory.maincent@bootlin.com \
    --to=kory.maincent@bootlin.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox