mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen
@ 2023-02-21  7:07 Ahmad Fatoum
  2023-02-21  7:07 ` [PATCH master 2/2] bootsource: use /chosen instead of /aliases/barebox,bootsource- Ahmad Fatoum
  2023-02-21  7:52 ` [PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-02-21  7:07 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Follow-up commit will also lookup the value of a chosen property by full
path or alias, so factor this out into a helper function.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 drivers/of/base.c | 52 +++++++++++++++++++++++++++++++++--------------
 include/of.h      |  8 ++++++++
 2 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 937847f44ab7..da621ad5d1a9 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2667,30 +2667,52 @@ void of_delete_node(struct device_node *node)
 	free(node);
 }
 
-struct device_node *of_get_stdoutpath(unsigned int *baudrate)
+/*
+ * of_find_node_by_chosen - Find a node given a chosen property pointing at it
+ * @propname:   the name of the property containing a path or alias
+ *              The function will lookup the first string in the property
+ *              value up to the first : character or till \0.
+ * @options     The Remainder (without : or \0 at the end) will be written
+ *              to *options if not NULL.
+ */
+struct device_node *of_find_node_by_chosen(const char *propname,
+					   const char **options)
 {
+	const char *value, *p;
+	char *buf = NULL;
 	struct device_node *dn;
-	const char *name;
-	const char *p;
-	char *q;
 
-	name = of_get_property(of_chosen, "stdout-path", NULL);
-	if (!name)
-		name = of_get_property(of_chosen, "linux,stdout-path", NULL);
+	value = of_get_property(of_chosen, propname, NULL);
+	if (!value)
+		return NULL;
 
-	if (!name)
-		return 0;
+	p = strchrnul(value, ':');
+	if (*p)
+		buf = xstrndup(value, p - value);
 
-	p = strchrnul(name, ':');
+	dn = of_find_node_by_path_or_alias(NULL, buf);
 
-	q = xstrndup(name, p - name);
+	free(buf);
 
-	dn = of_find_node_by_path_or_alias(NULL, q);
+	if (options && *p)
+		*options = p + 1;
 
-	free(q);
+	return dn;
+}
+
+struct device_node *of_get_stdoutpath(unsigned int *baudrate)
+{
+	const char *opts = NULL;
+	struct device_node *dn;
+
+	dn = of_find_node_by_chosen("stdout-path", &opts);
+	if (!dn)
+		dn = of_find_node_by_chosen("linux,stdout-path", &opts);
+	if (!dn)
+		return NULL;
 
-	if (baudrate && *p) {
-		unsigned rate = simple_strtoul(p + 1, NULL, 10);
+	if (baudrate && opts) {
+		unsigned rate = simple_strtoul(opts, NULL, 10);
 		if (rate)
 			*baudrate = rate;
 	}
diff --git a/include/of.h b/include/of.h
index 7ee1304b932b..6d0aca0102c6 100644
--- a/include/of.h
+++ b/include/of.h
@@ -314,6 +314,8 @@ struct cdev *of_parse_partition(struct cdev *cdev, struct device_node *node);
 int of_parse_partitions(struct cdev *cdev, struct device_node *node);
 int of_fixup_partitions(struct device_node *np, struct cdev *cdev);
 int of_partitions_register_fixup(struct cdev *cdev);
+struct device_node *of_find_node_by_chosen(const char *propname,
+					   const char **options);
 struct device_node *of_get_stdoutpath(unsigned int *);
 int of_device_is_stdout_path(struct device *dev, unsigned int *baudrate);
 const char *of_get_model(void);
@@ -369,6 +371,12 @@ static inline int of_partitions_register_fixup(struct cdev *cdev)
 	return -ENOSYS;
 }
 
+static inline struct device_node *of_find_node_by_chosen(const char *propname,
+							 const char **options)
+{
+	return NULL;
+}
+
 static inline struct device_node *of_get_stdoutpath(unsigned int *rate)
 {
 	return NULL;
-- 
2.38.3




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

* [PATCH master 2/2] bootsource: use /chosen instead of /aliases/barebox,bootsource-
  2023-02-21  7:07 [PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen Ahmad Fatoum
@ 2023-02-21  7:07 ` Ahmad Fatoum
  2023-02-21  7:52 ` [PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-02-21  7:07 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Aliases were a bad choice for this as barebox could end up calling MMC
devices /dev/barebox,bootsource-mmc0, which was not intended.

Move over to using chosen instead.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 .../devicetree/bindings/barebox/aliases.rst     | 17 +++++++++++------
 arch/arm/dts/rk356x.dtsi                        |  2 +-
 common/bootsource.c                             |  6 +++---
 include/bootsource.h                            |  2 +-
 4 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/Documentation/devicetree/bindings/barebox/aliases.rst b/Documentation/devicetree/bindings/barebox/aliases.rst
index e6face2c3314..8d226cfab6eb 100644
--- a/Documentation/devicetree/bindings/barebox/aliases.rst
+++ b/Documentation/devicetree/bindings/barebox/aliases.rst
@@ -20,16 +20,21 @@ probing the node at ``&iwdg`` will be named ``wdog0``.
 By default, barebox will assume the aliases in the DT to align with
 the bootsource communicated by the firmware. If this is not the case,
 a device tree override is possible via an
-``/aliases/barebox,bootsource-${bootsource}${bootsource_instance}``
+``/chosen/barebox,bootsource-${bootsource}${bootsource_instance}``
 property:
 
 .. code-block:: none
 
-  &{/aliases} {
-	mmc0 = &sdmmc0;
-	mmc1 = &sdhci;
-	barebox,bootsource-mmc0 = &sdhci;
-	barebox,bootsource-mmc1 = &sdmmc0;
+   / {
+   	aliases {
+		mmc0 = &sdmmc0;
+		mmc1 = &sdhci;
+	};
+
+   	chosen {
+		barebox,bootsource-mmc0 = &sdhci;
+		barebox,bootsource-mmc1 = &sdmmc0;
+	};
   };
 
 This will ensure that when booting from MMC, ``/dev/mmc${bootsource_instance}``
diff --git a/arch/arm/dts/rk356x.dtsi b/arch/arm/dts/rk356x.dtsi
index 254450d78fa8..6a9cd14d2d5a 100644
--- a/arch/arm/dts/rk356x.dtsi
+++ b/arch/arm/dts/rk356x.dtsi
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
 
 / {
-	aliases {
+	chosen {
 		barebox,bootsource-mmc0 = &sdhci;
 		barebox,bootsource-mmc1 = &sdmmc0;
 		barebox,bootsource-mmc2 = &sdmmc1;
diff --git a/common/bootsource.c b/common/bootsource.c
index 66bddf2dacf4..da528a5b9b19 100644
--- a/common/bootsource.c
+++ b/common/bootsource.c
@@ -137,7 +137,7 @@ void bootsource_set_raw_instance(int instance)
 
 int bootsource_of_alias_xlate(enum bootsource src, int instance)
 {
-	char alias[sizeof("barebox,bootsource-harddisk4294967295")];
+	char chosen[sizeof("barebox,bootsource-harddisk4294967295")];
 	const char *bootsource_stem;
 	struct device_node *np;
 	int alias_id;
@@ -153,10 +153,10 @@ int bootsource_of_alias_xlate(enum bootsource src, int instance)
 	if (!bootsource_stem)
 		return BOOTSOURCE_INSTANCE_UNKNOWN;
 
-	scnprintf(alias, sizeof(alias), "barebox,bootsource-%s%u",
+	scnprintf(chosen, sizeof(chosen), "barebox,bootsource-%s%u",
 		  bootsource_stem, instance);
 
-	np = of_find_node_by_alias(NULL, alias);
+	np = of_find_node_by_chosen(chosen, NULL);
 	if (!np)
 		return BOOTSOURCE_INSTANCE_UNKNOWN;
 
diff --git a/include/bootsource.h b/include/bootsource.h
index 381776a85a4c..05935b64a763 100644
--- a/include/bootsource.h
+++ b/include/bootsource.h
@@ -46,7 +46,7 @@ int bootsource_of_alias_xlate(enum bootsource bs, int instance);
  * bootsource_set() will instead consult
  * /aliases/barebox,bootsource-mmc1 which may point at a different
  * device than mmc1. In absence of appropriate barebox,bootsource-*
- * alias, instance is set without translation.
+ * chosen property, instance is set without translation.
  */
 int bootsource_set(enum bootsource bs, int instance);
 
-- 
2.38.3




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

* Re: [PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen
  2023-02-21  7:07 [PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen Ahmad Fatoum
  2023-02-21  7:07 ` [PATCH master 2/2] bootsource: use /chosen instead of /aliases/barebox,bootsource- Ahmad Fatoum
@ 2023-02-21  7:52 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2023-02-21  7:52 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Tue, Feb 21, 2023 at 08:07:34AM +0100, Ahmad Fatoum wrote:
> Follow-up commit will also lookup the value of a chosen property by full
> path or alias, so factor this out into a helper function.
> 
> Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
> ---
>  drivers/of/base.c | 52 +++++++++++++++++++++++++++++++++--------------
>  include/of.h      |  8 ++++++++
>  2 files changed, 45 insertions(+), 15 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index 937847f44ab7..da621ad5d1a9 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -2667,30 +2667,52 @@ void of_delete_node(struct device_node *node)
>  	free(node);
>  }
>  
> -struct device_node *of_get_stdoutpath(unsigned int *baudrate)
> +/*
> + * of_find_node_by_chosen - Find a node given a chosen property pointing at it
> + * @propname:   the name of the property containing a path or alias
> + *              The function will lookup the first string in the property
> + *              value up to the first : character or till \0.
> + * @options     The Remainder (without : or \0 at the end) will be written
> + *              to *options if not NULL.
> + */
> +struct device_node *of_find_node_by_chosen(const char *propname,
> +					   const char **options)
>  {
> +	const char *value, *p;
> +	char *buf = NULL;
>  	struct device_node *dn;
> -	const char *name;
> -	const char *p;
> -	char *q;
>  
> -	name = of_get_property(of_chosen, "stdout-path", NULL);
> -	if (!name)
> -		name = of_get_property(of_chosen, "linux,stdout-path", NULL);
> +	value = of_get_property(of_chosen, propname, NULL);
> +	if (!value)
> +		return NULL;
>  
> -	if (!name)
> -		return 0;
> +	p = strchrnul(value, ':');
> +	if (*p)
> +		buf = xstrndup(value, p - value);
>  
> -	p = strchrnul(name, ':');
> +	dn = of_find_node_by_path_or_alias(NULL, buf);
>  
> -	q = xstrndup(name, p - name);
> +	free(buf);
>  
> -	dn = of_find_node_by_path_or_alias(NULL, q);
> +	if (options && *p)
> +		*options = p + 1;
>  
> -	free(q);
> +	return dn;
> +}
> +
> +struct device_node *of_get_stdoutpath(unsigned int *baudrate)
> +{
> +	const char *opts = NULL;
> +	struct device_node *dn;
> +
> +	dn = of_find_node_by_chosen("stdout-path", &opts);
> +	if (!dn)
> +		dn = of_find_node_by_chosen("linux,stdout-path", &opts);
> +	if (!dn)
> +		return NULL;
>  
> -	if (baudrate && *p) {
> -		unsigned rate = simple_strtoul(p + 1, NULL, 10);
> +	if (baudrate && opts) {
> +		unsigned rate = simple_strtoul(opts, NULL, 10);
>  		if (rate)
>  			*baudrate = rate;
>  	}
> diff --git a/include/of.h b/include/of.h
> index 7ee1304b932b..6d0aca0102c6 100644
> --- a/include/of.h
> +++ b/include/of.h
> @@ -314,6 +314,8 @@ struct cdev *of_parse_partition(struct cdev *cdev, struct device_node *node);
>  int of_parse_partitions(struct cdev *cdev, struct device_node *node);
>  int of_fixup_partitions(struct device_node *np, struct cdev *cdev);
>  int of_partitions_register_fixup(struct cdev *cdev);
> +struct device_node *of_find_node_by_chosen(const char *propname,
> +					   const char **options);
>  struct device_node *of_get_stdoutpath(unsigned int *);
>  int of_device_is_stdout_path(struct device *dev, unsigned int *baudrate);
>  const char *of_get_model(void);
> @@ -369,6 +371,12 @@ static inline int of_partitions_register_fixup(struct cdev *cdev)
>  	return -ENOSYS;
>  }
>  
> +static inline struct device_node *of_find_node_by_chosen(const char *propname,
> +							 const char **options)
> +{
> +	return NULL;
> +}
> +
>  static inline struct device_node *of_get_stdoutpath(unsigned int *rate)
>  {
>  	return NULL;
> -- 
> 2.38.3
> 
> 
> 

-- 
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 |



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

end of thread, other threads:[~2023-02-21  7:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-21  7:07 [PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen Ahmad Fatoum
2023-02-21  7:07 ` [PATCH master 2/2] bootsource: use /chosen instead of /aliases/barebox,bootsource- Ahmad Fatoum
2023-02-21  7:52 ` [PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen Sascha Hauer

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