mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data
@ 2021-04-03  7:02 Ahmad Fatoum
  2021-04-03  7:02 ` [PATCH 2/4] RISC-V: debug_ll: ns16550: split off debug_ll from generic parts Ahmad Fatoum
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2021-04-03  7:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Currently, the generic DT image can't properly have a PBL console,
because it's only known at runtime what system we are running on.

As we already parse the FDT in the PBL to get the memory regions, we
could extract the board compatible as well and determine which UART to
use. Add a helper to achieve this.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 include/pbl.h |  9 +++++++++
 pbl/fdt.c     | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/include/pbl.h b/include/pbl.h
index 194d5e750839..f58daec7351a 100644
--- a/include/pbl.h
+++ b/include/pbl.h
@@ -34,4 +34,13 @@ ssize_t pbl_fat_load(struct pbl_bio *, const char *filename, void *dest, size_t
 
 void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsize);
 
+struct fdt_device_id {
+	const char *compatible;
+	const void *data;
+};
+
+const void *
+fdt_device_get_match_data(const void *fdt, const char *nodepath,
+			  const struct fdt_device_id ids[]);
+
 #endif /* __PBL_H__ */
diff --git a/pbl/fdt.c b/pbl/fdt.c
index b4a40a514b8b..03260cb61971 100644
--- a/pbl/fdt.c
+++ b/pbl/fdt.c
@@ -68,3 +68,39 @@ err:
 	pr_err("No memory, cannot continue\n");
 	while (1);
 }
+
+const void *fdt_device_get_match_data(const void *fdt, const char *nodepath,
+				      const struct fdt_device_id ids[])
+{
+	int node, length;
+	const char *list, *end;
+	const struct fdt_device_id *id;
+
+	node = fdt_path_offset(fdt, nodepath);
+	if (node < 0)
+		return NULL;
+
+	list = fdt_getprop(fdt, node, "compatible", &length);
+	if (!list)
+		return NULL;
+
+	end = list + length;
+
+	while (list < end) {
+		length = strnlen(list, end - list) + 1;
+
+		/* Abort if the last string isn't properly NUL-terminated. */
+		if (list + length > end)
+			return NULL;
+
+		for (id = ids; id->compatible; id++) {
+			if (strlen(id->compatible) == length &&
+			    !memcmp(list, id->compatible, length))
+				return id->data;
+		}
+
+		list += length;
+	}
+
+	return NULL;
+}
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/4] RISC-V: debug_ll: ns16550: split off debug_ll from generic parts
  2021-04-03  7:02 [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
@ 2021-04-03  7:02 ` Ahmad Fatoum
  2021-04-03  7:02 ` [PATCH 3/4] RISC-V: board-dt-2nd: add PBL console support for virt Ahmad Fatoum
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2021-04-03  7:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The early NS16550 code is written for DEBUG_LL and can't be directly
used with pbl_set_putc if it's disabled. Split off the generic parts
into a new header that can be used by the virt board for PBL console.

DEBUG_LL functionality is unaffected.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 arch/riscv/include/asm/debug_ll_ns16550.h | 49 ++++++++---------------
 arch/riscv/include/asm/ns16550.h          | 48 ++++++++++++++++++++++
 2 files changed, 65 insertions(+), 32 deletions(-)
 create mode 100644 arch/riscv/include/asm/ns16550.h

diff --git a/arch/riscv/include/asm/debug_ll_ns16550.h b/arch/riscv/include/asm/debug_ll_ns16550.h
index 7d6d12df74fe..b09882ddad14 100644
--- a/arch/riscv/include/asm/debug_ll_ns16550.h
+++ b/arch/riscv/include/asm/debug_ll_ns16550.h
@@ -35,19 +35,6 @@
 
 #endif /* CONFIG_DEBUG_LL */
 
-#define UART_THR	(0x0 << DEBUG_LL_UART_SHIFT)
-#define UART_RBR	(0x0 << DEBUG_LL_UART_SHIFT)
-#define UART_DLL	(0x0 << DEBUG_LL_UART_SHIFT)
-#define UART_DLM	(0x1 << DEBUG_LL_UART_SHIFT)
-#define UART_LCR	(0x3 << DEBUG_LL_UART_SHIFT)
-#define UART_LSR	(0x5 << DEBUG_LL_UART_SHIFT)
-
-#define UART_LCR_W	0x07	/* Set UART to 8,N,2 & DLAB = 0 */
-#define UART_LCR_DLAB	0x87	/* Set UART to 8,N,2 & DLAB = 1 */
-
-#define UART_LSR_DR	0x01    /* UART received data present */
-#define UART_LSR_THRE	0x20	/* Xmit holding register empty */
-
 #if defined(DEBUG_LL_UART_IOSIZE32)
 #define UART_REG_L	lw
 #define UART_REG_S	sw
@@ -62,31 +49,29 @@
 #error "Please define DEBUG_LL_UART_IOSIZE{8,32}"
 #endif
 
+#include <asm/ns16550.h>
+
 #ifndef __ASSEMBLY__
 /*
  * C macros
  */
 
-#include <asm/io.h>
-
 static inline void PUTC_LL(char ch)
 {
 #ifdef CONFIG_DEBUG_LL
-	while (!(__uart_read((u8 *)DEBUG_LL_UART_ADDR + UART_LSR) & UART_LSR_THRE))
-		;
-	__uart_write(ch, (u8 *)DEBUG_LL_UART_ADDR + UART_THR);
-#endif /* CONFIG_DEBUG_LL */
+	early_ns16550_putc(ch, DEBUG_LL_UART_ADDR, DEBUG_LL_UART_SHIFT,
+			   __uart_read, __uart_write);
+#endif
 }
 
 static inline void debug_ll_ns16550_init(void)
 {
 #ifdef CONFIG_DEBUG_LL
-	__uart_write(UART_LCR_DLAB, (u8 *)DEBUG_LL_UART_ADDR + UART_LCR);
-	__uart_write(DEBUG_LL_UART_DIVISOR & 0xff, (u8 *)DEBUG_LL_UART_ADDR + UART_DLL);
-	__uart_write((DEBUG_LL_UART_DIVISOR >> 8) & 0xff, (u8 *)DEBUG_LL_UART_ADDR + UART_DLM);
-	__uart_write(UART_LCR_W, (u8 *)DEBUG_LL_UART_ADDR + UART_LCR);
-#endif /* CONFIG_DEBUG_LL */
+	early_ns16550_init(DEBUG_LL_UART_ADDR, DEBUG_LL_UART_DIVISOR,
+			   DEBUG_LL_UART_SHIFT, __uart_write);
+#endif
 }
+
 #else /* __ASSEMBLY__ */
 /*
  * Macros for use in assembly language code
@@ -97,15 +82,15 @@ static inline void debug_ll_ns16550_init(void)
 	li	t0, DEBUG_LL_UART_ADDR
 
 	li	t1, UART_LCR_DLAB		/* DLAB on */
-	UART_REG_S	t1, UART_LCR(t0)		/* Write it out */
+	UART_REG_S	t1, UART_LCR(DEBUG_LL_UART_SHIFT)(t0)	/* Write it out */
 
 	li	t1, \divisor
-	UART_REG_S	t1, UART_DLL(t0)		/* write low order byte */
+	UART_REG_S	t1, UART_DLL(DEBUG_LL_UART_SHIFT)(t0)	/* write low order byte */
 	srl	t1, t1, 8
-	UART_REG_S	t1, UART_DLM(t0)		/* write high order byte */
+	UART_REG_S	t1, UART_DLM(DEBUG_LL_UART_SHIFT)(t0)	/* write high order byte */
 
 	li	t1, UART_LCR_W			/* DLAB off */
-	UART_REG_S	t1, UART_LCR(t0)		/* Write it out */
+	UART_REG_S	t1, UART_LCR(DEBUG_LL_UART_SHIFT)(t0)	/* Write it out */
 #endif /* CONFIG_DEBUG_LL */
 .endm
 
@@ -118,11 +103,11 @@ static inline void debug_ll_ns16550_init(void)
 	li	t0, DEBUG_LL_UART_ADDR
 
 201:
-	UART_REG_L	t1, UART_LSR(t0)	/* get line status */
+	UART_REG_L	t1, UART_LSR(DEBUG_LL_UART_SHIFT)(t0)	/* get line status */
 	andi	t1, t1, UART_LSR_THRE	/* check for transmitter empty */
 	beqz	t1, 201b			/* try again */
 
-	UART_REG_S	a0, UART_THR(t0)	/* write the character */
+	UART_REG_S	a0, UART_THR(DEBUG_LL_UART_SHIFT)(t0)	/* write the character */
 
 #endif /* CONFIG_DEBUG_LL */
 .endm
@@ -158,7 +143,7 @@ static inline void debug_ll_ns16550_init(void)
 	li      t0, DEBUG_LL_UART_ADDR
 
 	/* get line status and check for data present */
-	UART_REG_L	s0, UART_LSR(t0)
+	UART_REG_L	s0, UART_LSR(DEBUG_LL_UART_SHIFT)(t0)
 	andi	s0, s0, UART_LSR_DR
 
 #endif /* CONFIG_DEBUG_LL */
@@ -177,7 +162,7 @@ static inline void debug_ll_ns16550_init(void)
 	beqz	s0, 204b
 
 	/* read a character */
-	UART_REG_L	s0, UART_RBR(t0)
+	UART_REG_L	s0, UART_RBR(DEBUG_LL_UART_SHIFT)(t0)
 
 #endif /* CONFIG_DEBUG_LL */
 .endm
diff --git a/arch/riscv/include/asm/ns16550.h b/arch/riscv/include/asm/ns16550.h
new file mode 100644
index 000000000000..7f56692b77ce
--- /dev/null
+++ b/arch/riscv/include/asm/ns16550.h
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2016, 2017 Antony Pavlov <antonynpavlov@gmail.com>
+ */
+
+/** @file
+ *  This file contains declaration for early output support
+ */
+#ifndef __ASM_NS16550_H__
+#define __ASM_NS16550_H__
+
+#include <linux/kconfig.h>
+
+#define UART_THR(shift)		(0x0 << shift)
+#define UART_RBR(shift)		(0x0 << shift)
+#define UART_DLL(shift)		(0x0 << shift)
+#define UART_DLM(shift)		(0x1 << shift)
+#define UART_LCR(shift)		(0x3 << shift)
+#define UART_LSR(shift)		(0x5 << shift)
+
+#define UART_LCR_W	0x07	/* Set UART to 8,N,2 & DLAB = 0 */
+#define UART_LCR_DLAB	0x87	/* Set UART to 8,N,2 & DLAB = 1 */
+
+#define UART_LSR_DR	0x01    /* UART received data present */
+#define UART_LSR_THRE	0x20	/* Xmit holding register empty */
+
+#ifndef __ASSEMBLY__
+
+#include <asm/io.h>
+
+#define early_ns16550_putc(ch, base, shift, readx, writex) \
+	do { \
+		while (!(readx((u8 *)base + UART_LSR(shift)) & UART_LSR_THRE)) \
+			; \
+		writex(ch, (u8 *)base + UART_THR(shift)); \
+	} while (0)
+
+#define early_ns16550_init(base, divisor, shift, writex) \
+	do { \
+		writex(UART_LCR_DLAB, (u8 *)base + UART_LCR(shift)); \
+		writex(divisor & 0xff, (u8 *)base + UART_DLL(shift)); \
+		writex((divisor >> 8) & 0xff, (u8 *)base + UART_DLM(shift)); \
+		writex(UART_LCR_W, (u8 *)base + UART_LCR(shift)); \
+	} while (0)
+
+#endif
+
+#endif
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/4] RISC-V: board-dt-2nd: add PBL console support for virt
  2021-04-03  7:02 [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
  2021-04-03  7:02 ` [PATCH 2/4] RISC-V: debug_ll: ns16550: split off debug_ll from generic parts Ahmad Fatoum
@ 2021-04-03  7:02 ` Ahmad Fatoum
  2021-04-03  7:02 ` [PATCH 4/4] RISC-V: delete unused mach-virt subdirectory Ahmad Fatoum
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2021-04-03  7:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The Virt machine has a ns16550a UART at address 0x10000000. As we reuse
the generic DT image for this platform, we can't use either DEBUG_LL or
pbl_console as we would need to hardcode information on what UART is
available where, which wouldn't be correct for other boards.

However, if we parse the board compatible, we could match it with the
appropriate PBL console implementation without sacrificing portability.
Do so.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 arch/riscv/boot/board-dt-2nd.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/boot/board-dt-2nd.c b/arch/riscv/boot/board-dt-2nd.c
index be28ea23cd6d..e9810f8add97 100644
--- a/arch/riscv/boot/board-dt-2nd.c
+++ b/arch/riscv/boot/board-dt-2nd.c
@@ -3,7 +3,7 @@
 #include <common.h>
 #include <asm/sections.h>
 #include <linux/sizes.h>
-#include <debug_ll.h>
+#include <asm/ns16550.h>
 #include <pbl.h>
 #include <fdt.h>
 
@@ -22,10 +22,29 @@
 
 #include <asm/barebox-riscv.h>
 
+static void virt_ns16550_putc(void *base, int ch)
+{
+	early_ns16550_putc(ch, base, 0, readb, writeb);
+}
+
+static void virt_ns16550_init(void)
+{
+	void __iomem *base = IOMEM(0x10000000);
+
+	early_ns16550_init(base, 3686400 / CONFIG_BAUDRATE, 0, writeb);
+	pbl_set_putc(virt_ns16550_putc, base);
+}
+
+static const struct fdt_device_id console_ids[] = {
+	{ .compatible = "riscv-virtio", .data = virt_ns16550_init },
+	{ /* sentinel */ }
+};
+
 ENTRY_FUNCTION(start_dt_2nd, a0, _fdt, a2)
 {
 	unsigned long membase, memsize, endmem, endfdt, uncompressed_len;
 	struct fdt_header *fdt = (void *)_fdt;
+	void (*pbl_uart_init)(void);
 
 	if (!fdt)
 		hang();
@@ -33,6 +52,12 @@ ENTRY_FUNCTION(start_dt_2nd, a0, _fdt, a2)
 	relocate_to_current_adr();
 	setup_c();
 
+	pbl_uart_init = fdt_device_get_match_data(fdt, "/", console_ids);
+	if (pbl_uart_init) {
+		pbl_uart_init();
+		putchar('>');
+	}
+
 	fdt_find_mem(fdt, &membase, &memsize);
 	endmem = membase + memsize;
 	endfdt = _fdt + be32_to_cpu(fdt->totalsize);
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 4/4] RISC-V: delete unused mach-virt subdirectory
  2021-04-03  7:02 [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
  2021-04-03  7:02 ` [PATCH 2/4] RISC-V: debug_ll: ns16550: split off debug_ll from generic parts Ahmad Fatoum
  2021-04-03  7:02 ` [PATCH 3/4] RISC-V: board-dt-2nd: add PBL console support for virt Ahmad Fatoum
@ 2021-04-03  7:02 ` Ahmad Fatoum
  2021-04-03  9:49 ` [PATCH] fixup! PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
  2021-04-03 12:00 ` [PATCH 1/4] " Jules Maselbas
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2021-04-03  7:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The code within was unused and was kept only to make patching the
machine for DEBUG_LL easier. Now that we have PBL console support, this
is no longer needed, so remove it.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 arch/riscv/Makefile                          |  1 -
 arch/riscv/mach-virt/Makefile                |  3 ---
 arch/riscv/mach-virt/include/mach/debug_ll.h | 25 --------------------
 3 files changed, 29 deletions(-)
 delete mode 100644 arch/riscv/mach-virt/Makefile
 delete mode 100644 arch/riscv/mach-virt/include/mach/debug_ll.h

diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index aba4526bba5a..1a41d15477b5 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -20,7 +20,6 @@ cflags-y += $(riscv-cflags-y)
 LDFLAGS_barebox += -nostdlib
 
 machine-$(CONFIG_MACH_ERIZO)	:= erizo
-machine-$(CONFIG_MACH_VIRT)	:= virt
 
 LDFLAGS_barebox += $(riscv-ldflags-y)
 
diff --git a/arch/riscv/mach-virt/Makefile b/arch/riscv/mach-virt/Makefile
deleted file mode 100644
index d9c51e74c379..000000000000
--- a/arch/riscv/mach-virt/Makefile
+++ /dev/null
@@ -1,3 +0,0 @@
-# just to build a built-in.o. Otherwise compilation fails when no o-files is
-# created.
-obj- += dummy.o
diff --git a/arch/riscv/mach-virt/include/mach/debug_ll.h b/arch/riscv/mach-virt/include/mach/debug_ll.h
deleted file mode 100644
index 056b7a330bdd..000000000000
--- a/arch/riscv/mach-virt/include/mach/debug_ll.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * Copyright (C) 2017 Antony Pavlov <antonynpavlov@gmail.com>
- */
-
-#ifndef __MACH_VIRT_DEBUG_LL__
-#define __MACH_VIRT_DEBUG_LL__
-
-/** @file
- *  This File contains declaration for early output support
- */
-
-#include <linux/kconfig.h>
-
-#define DEBUG_LL_UART_ADDR	0x10000000
-#define DEBUG_LL_UART_SHIFT	0
-#define DEBUG_LL_UART_IOSIZE8
-
-#define DEBUG_LL_UART_CLK       0x00384000
-#define DEBUG_LL_UART_BPS       CONFIG_BAUDRATE
-#define DEBUG_LL_UART_DIVISOR   (DEBUG_LL_UART_CLK / DEBUG_LL_UART_BPS)
-
-#include <asm/debug_ll_ns16550.h>
-
-#endif /* __MACH_VIRT_DEBUG_LL__ */
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] fixup! PBL: fdt: implement fdt_device_get_match_data
  2021-04-03  7:02 [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2021-04-03  7:02 ` [PATCH 4/4] RISC-V: delete unused mach-virt subdirectory Ahmad Fatoum
@ 2021-04-03  9:49 ` Ahmad Fatoum
  2021-04-03 12:00 ` [PATCH 1/4] " Jules Maselbas
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2021-04-03  9:49 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Forgot to squash

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 pbl/fdt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pbl/fdt.c b/pbl/fdt.c
index 03260cb61971..8ec128f35987 100644
--- a/pbl/fdt.c
+++ b/pbl/fdt.c
@@ -94,7 +94,7 @@ const void *fdt_device_get_match_data(const void *fdt, const char *nodepath,
 			return NULL;
 
 		for (id = ids; id->compatible; id++) {
-			if (strlen(id->compatible) == length &&
+			if (strlen(id->compatible) + 1 == length &&
 			    !memcmp(list, id->compatible, length))
 				return id->data;
 		}
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data
  2021-04-03  7:02 [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
                   ` (3 preceding siblings ...)
  2021-04-03  9:49 ` [PATCH] fixup! PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
@ 2021-04-03 12:00 ` Jules Maselbas
  2021-04-04 11:22   ` Ahmad Fatoum
  4 siblings, 1 reply; 7+ messages in thread
From: Jules Maselbas @ 2021-04-03 12:00 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

Hi Ahmad,

On Sat, Apr 03, 2021 at 09:02:34AM +0200, Ahmad Fatoum wrote:
> Currently, the generic DT image can't properly have a PBL console,
> because it's only known at runtime what system we are running on.
> 
> As we already parse the FDT in the PBL to get the memory regions, we
> could extract the board compatible as well and determine which UART to
> use. Add a helper to achieve this.
> 
> Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
> ---
>  include/pbl.h |  9 +++++++++
>  pbl/fdt.c     | 36 ++++++++++++++++++++++++++++++++++++
>  2 files changed, 45 insertions(+)
> 
> diff --git a/include/pbl.h b/include/pbl.h
> index 194d5e750839..f58daec7351a 100644
> --- a/include/pbl.h
> +++ b/include/pbl.h
> @@ -34,4 +34,13 @@ ssize_t pbl_fat_load(struct pbl_bio *, const char *filename, void *dest, size_t
>  
>  void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsize);
>  
> +struct fdt_device_id {
> +	const char *compatible;
> +	const void *data;
> +};
> +
> +const void *
> +fdt_device_get_match_data(const void *fdt, const char *nodepath,
> +			  const struct fdt_device_id ids[]);
> +
>  #endif /* __PBL_H__ */
> diff --git a/pbl/fdt.c b/pbl/fdt.c
> index b4a40a514b8b..03260cb61971 100644
> --- a/pbl/fdt.c
> +++ b/pbl/fdt.c
> @@ -68,3 +68,39 @@ err:
>  	pr_err("No memory, cannot continue\n");
>  	while (1);
>  }
> +
> +const void *fdt_device_get_match_data(const void *fdt, const char *nodepath,
> +				      const struct fdt_device_id ids[])
> +{
> +	int node, length;
> +	const char *list, *end;
> +	const struct fdt_device_id *id;
> +
> +	node = fdt_path_offset(fdt, nodepath);
> +	if (node < 0)
> +		return NULL;
> +
> +	list = fdt_getprop(fdt, node, "compatible", &length);
> +	if (!list)
> +		return NULL;
> +
> +	end = list + length;
> +
> +	while (list < end) {
> +		length = strnlen(list, end - list) + 1;
> +
> +		/* Abort if the last string isn't properly NUL-terminated. */
> +		if (list + length > end)
> +			return NULL;
> +
> +		for (id = ids; id->compatible; id++) {
> +			if (strlen(id->compatible) == length &&
> +			    !memcmp(list, id->compatible, length))
> +				return id->data;
Why not using strcmp, or even strcasecmp as done by of_compat_cmp ?
If both string doesn't have the same length, strcmp will report a diff.

> +		}
> +
> +		list += length;
> +	}
> +
> +	return NULL;
> +}
> -- 
> 2.30.0
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data
  2021-04-03 12:00 ` [PATCH 1/4] " Jules Maselbas
@ 2021-04-04 11:22   ` Ahmad Fatoum
  0 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2021-04-04 11:22 UTC (permalink / raw)
  To: Jules Maselbas, Ahmad Fatoum; +Cc: barebox

Hello Jules,

On 03.04.21 14:00, Jules Maselbas wrote:
>> +		for (id = ids; id->compatible; id++) {
>> +			if (strlen(id->compatible) == length &&
>> +			    !memcmp(list, id->compatible, length))
>> +				return id->data;
> Why not using strcmp, or even strcasecmp as done by of_compat_cmp ?
> If both string doesn't have the same length, strcmp will report a diff.

The code is based on fdt_stringlist_search. I assume it compared length
first, because it already computed it, so an early comparison
saves some cycles. Good point on case-insensitivity. I'll send out a v2.

Thanks,
Ahmad

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-04-04 11:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-03  7:02 [PATCH 1/4] PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
2021-04-03  7:02 ` [PATCH 2/4] RISC-V: debug_ll: ns16550: split off debug_ll from generic parts Ahmad Fatoum
2021-04-03  7:02 ` [PATCH 3/4] RISC-V: board-dt-2nd: add PBL console support for virt Ahmad Fatoum
2021-04-03  7:02 ` [PATCH 4/4] RISC-V: delete unused mach-virt subdirectory Ahmad Fatoum
2021-04-03  9:49 ` [PATCH] fixup! PBL: fdt: implement fdt_device_get_match_data Ahmad Fatoum
2021-04-03 12:00 ` [PATCH 1/4] " Jules Maselbas
2021-04-04 11:22   ` Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox