All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kory Maincent <kory.maincent@bootlin.com>
To: u-boot@lists.denx.de
Subject: [PATCH v5 04/10] ti/common: add support for extension_scan_board function
Date: Tue,  4 May 2021 19:31:24 +0200	[thread overview]
Message-ID: <20210504173130.22869-5-kory.maincent@bootlin.com> (raw)
In-Reply-To: <20210504173130.22869-1-kory.maincent@bootlin.com>

The BeagleBone platforms all use a common mechanism to discover and
identify extension boards (called "capes"): each extension board has an
I2C-connected EEPROM describing itself.

This patch implements a generic extension_scan_board() feature that can
be used by all BeagleBone platforms to read those I2C EEPROMs and fill
in the list of "extension" structures.

Following commits will enable this common logic on two BeagleBone
platforms.

Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
---

Change Since v1:
 - Use CAPE_EEPROM_BUS_NUM in Kconfig in place of the board header

 board/ti/common/Kconfig       |  6 +++
 board/ti/common/Makefile      |  1 +
 board/ti/common/cape_detect.c | 96 +++++++++++++++++++++++++++++++++++
 board/ti/common/cape_detect.h | 28 ++++++++++
 4 files changed, 131 insertions(+)
 create mode 100644 board/ti/common/cape_detect.c
 create mode 100644 board/ti/common/cape_detect.h

diff --git a/board/ti/common/Kconfig b/board/ti/common/Kconfig
index 9ead7ca038..49edd98014 100644
--- a/board/ti/common/Kconfig
+++ b/board/ti/common/Kconfig
@@ -16,6 +16,12 @@ config EEPROM_CHIP_ADDRESS
 	default 0x50
 	depends on TI_I2C_BOARD_DETECT
 
+config CAPE_EEPROM_BUS_NUM
+	int "Cape EEPROM's I2C bus address"
+	range 0 8
+	default 2
+	depends on CMD_EXTENSION
+
 config TI_COMMON_CMD_OPTIONS
 	bool "Enable cmd options on TI platforms"
 	imply CMD_ASKENV
diff --git a/board/ti/common/Makefile b/board/ti/common/Makefile
index cb97f226ae..3172d87b46 100644
--- a/board/ti/common/Makefile
+++ b/board/ti/common/Makefile
@@ -2,3 +2,4 @@
 # Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
 
 obj-${CONFIG_TI_I2C_BOARD_DETECT} += board_detect.o
+obj-${CONFIG_CMD_EXTENSION} += cape_detect.o
diff --git a/board/ti/common/cape_detect.c b/board/ti/common/cape_detect.c
new file mode 100644
index 0000000000..2e6105cfbf
--- /dev/null
+++ b/board/ti/common/cape_detect.c
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2021
+ * K?ry Maincent, Bootlin, <kory.maincent@bootlin.com>
+ */
+
+#include <common.h>
+#include <malloc.h>
+#include <i2c.h>
+#include <extension_board.h>
+
+#include "cape_detect.h"
+
+static void sanitize_field(char *text, size_t size)
+{
+	char *c = NULL;
+
+	for (c = text; c < text + (int)size; c++) {
+		if (*c == 0xFF)
+			*c = 0;
+	}
+}
+
+int extension_board_scan(struct list_head *extension_list)
+{
+	struct extension *cape;
+	struct am335x_cape_eeprom_id eeprom_header;
+
+	int num_capes = 0;
+	int ret, i;
+	struct udevice *dev;
+	unsigned char addr;
+
+	char process_cape_part_number[17] = {'0'};
+	char process_cape_version[5] = {'0'};
+	uint8_t cursor = 0;
+
+	for (addr = CAPE_EEPROM_FIRST_ADDR; addr <= CAPE_EEPROM_LAST_ADDR; addr++) {
+		ret = i2c_get_chip_for_busnum(CONFIG_CAPE_EEPROM_BUS_NUM, addr, 1, &dev);
+		if (ret)
+			continue;
+
+		/* Move the read cursor to the beginning of the EEPROM */
+		dm_i2c_write(dev, 0, &cursor, 1);
+		ret = dm_i2c_read(dev, 0, (uint8_t *)&eeprom_header,
+				  sizeof(struct am335x_cape_eeprom_id));
+		if (ret) {
+			printf("Cannot read i2c EEPROM\n");
+			continue;
+		}
+
+		if (eeprom_header.header != CAPE_MAGIC)
+			continue;
+
+		sanitize_field(eeprom_header.board_name, sizeof(eeprom_header.board_name));
+		sanitize_field(eeprom_header.version, sizeof(eeprom_header.version));
+		sanitize_field(eeprom_header.manufacturer, sizeof(eeprom_header.manufacturer));
+		sanitize_field(eeprom_header.part_number, sizeof(eeprom_header.part_number));
+
+		/* Process cape part_number */
+		memset(process_cape_part_number, 0, sizeof(process_cape_part_number));
+		strncpy(process_cape_part_number, eeprom_header.part_number, 16);
+		/* Some capes end with '.' */
+		for (i = 15; i >= 0; i--) {
+			if (process_cape_part_number[i] == '.')
+				process_cape_part_number[i] = '\0';
+			else
+				break;
+		}
+
+		/* Process cape version */
+		memset(process_cape_version, 0, sizeof(process_cape_version));
+		strncpy(process_cape_version, eeprom_header.version, 4);
+		for (i = 0; i < 4; i++) {
+			if (process_cape_version[i] == 0)
+				process_cape_version[i] = '0';
+		}
+
+		printf("BeagleBone Cape: %s (0x%x)\n", eeprom_header.board_name, addr);
+
+		cape = calloc(1, sizeof(struct extension));
+		if (!cape) {
+			printf("Error in memory allocation\n");
+			return num_capes;
+		}
+
+		snprintf(cape->overlay, sizeof(cape->overlay), "%s-%s.dtbo",
+			 process_cape_part_number, process_cape_version);
+		strncpy(cape->name, eeprom_header.board_name, 32);
+		strncpy(cape->version, process_cape_version, 4);
+		strncpy(cape->owner, eeprom_header.manufacturer, 16);
+		list_add_tail(&cape->list, extension_list);
+		num_capes++;
+	}
+	return num_capes;
+}
diff --git a/board/ti/common/cape_detect.h b/board/ti/common/cape_detect.h
new file mode 100644
index 0000000000..b0d5c9f18b
--- /dev/null
+++ b/board/ti/common/cape_detect.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * (C) Copyright 2021
+ * K?ry Maincent, Bootlin, <kory.maincent@bootlin.com>
+ */
+
+#ifndef __CAPE_DETECT_H
+#define __CAPE_DETECT_H
+
+struct am335x_cape_eeprom_id {
+	unsigned int header;
+	char eeprom_rev[2];
+	char board_name[32];
+	char version[4];
+	char manufacturer[16];
+	char part_number[16];
+};
+
+#define CAPE_EEPROM_FIRST_ADDR	0x54
+#define CAPE_EEPROM_LAST_ADDR	0x57
+
+#define CAPE_EEPROM_ADDR_LEN 0x10
+
+#define CAPE_MAGIC 0xEE3355AA
+
+int extension_board_scan(struct list_head *extension_list);
+
+#endif /* __CAPE_DETECT_H */
-- 
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 ` Kory Maincent [this message]
2021-05-13 19:06   ` [PATCH v5 04/10] ti/common: add support for extension_scan_board function 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 ` [PATCH v5 07/10] arm: sunxi: add support for DIP detection to CHIP board Kory Maincent
2021-05-13 11:01   ` 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-5-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.