Skip to contentSkip to conversation

gpio: Get rid of gpio_hog_probe_all()

PATCH In review Marek Vasut 4 participants 9 messages Sep 19, 2022 – Sep 22, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: Get rid of gpio_hog_probe_all()

The gpio_hog_probe_all() functionality can be perfectly well replaced by DM_FLAG_PROBE_AFTER_BIND DM flag, which would trigger .probe() callback of each GPIO hog driver instance after .bind() and thus configure the hogged GPIO accordingly.

---
Cc: Heinrich Schuchardt <[email protected]>
Cc: Samuel Holland <[email protected]>
Cc: Simon Glass <[email protected]>

---
 common/board_r.c           |  3 ---
 common/spl/spl.c           |  3 ---
 doc/README.gpio            |  6 ++----
 drivers/gpio/gpio-uclass.c | 25 ++-----------------------
 include/asm-generic/gpio.h |  8 --------
 5 files changed, 4 insertions(+), 41 deletions(-)
common/board_r.c +0 -3
index 56eb60fa275..c556aa5a073 100644
@@ -750,9 +750,6 @@ static init_fnc_t init_sequence_r[] = {
 	initr_status_led,
 #endif
 	/* PPC has a udelay(20) here dating from 2002. Why? */
-#if defined(CONFIG_GPIO_HOG)
-	gpio_hog_probe_all,
-#endif
 #ifdef CONFIG_BOARD_LATE_INIT
 	board_late_init,
 #endif
common/spl/spl.c +0 -3
index 29e0898f03d..683e0dfc526 100644
@@ -770,9 +770,6 @@ void board_init_r(gd_t *dummy1, ulong dummy2)
 		}
 	}
 
-	if (CONFIG_IS_ENABLED(GPIO_HOG))
-		gpio_hog_probe_all();
-
 #if CONFIG_IS_ENABLED(BOARD_INIT)
 	spl_board_init();
 #endif
doc/README.gpio +2 -4
index 548ff37b8cc..d253f654fad 100644
@@ -2,10 +2,8 @@
 GPIO hog (CONFIG_GPIO_HOG)
 --------
 
-All the GPIO hog are initialized in gpio_hog_probe_all() function called in
-board_r.c just before board_late_init() but you can also acces directly to
-the gpio with gpio_hog_lookup_name().
-
+All the GPIO hog are initialized using DM_FLAG_PROBE_AFTER_BIND DM flag
+after bind().
 
 Example, for the device tree:
 
drivers/gpio/gpio-uclass.c +2 -23
index 0ed32b72170..32df0448a7b 100644
@@ -315,34 +315,11 @@ static int gpio_hog_probe(struct udevice *dev)
 	return 0;
 }
 
-int gpio_hog_probe_all(void)
-{
-	struct udevice *dev;
-	int ret;
-	int retval = 0;
-
-	for (uclass_first_device(UCLASS_NOP, &dev);
-	     dev;
-	     uclass_find_next_device(&dev)) {
-		if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
-			ret = device_probe(dev);
-			if (ret) {
-				printf("Failed to probe device %s err: %d\n",
-				       dev->name, ret);
-				retval = ret;
-			}
-		}
-	}
-
-	return retval;
-}
-
 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
 {
 	struct udevice *dev;
 
 	*desc = NULL;
-	gpio_hog_probe_all();
 	if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
 		struct gpio_hog_priv *priv = dev_get_priv(dev);
 
@@ -1503,6 +1480,8 @@ static int gpio_post_bind(struct udevice *dev)
 								 &child);
 				if (ret)
 					return ret;
+
+				dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND);
 			}
 		}
 	}
include/asm-generic/gpio.h +0 -8
index 81f63f06f15..e56d3777ae5 100644
@@ -460,14 +460,6 @@ int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc);
  */
 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc);
 
-/**
- * gpio_hog_probe_all() - probe all gpio devices with
- * gpio-hog subnodes.
- *
- * @return:	Returns return value from device_probe()
- */
-int gpio_hog_probe_all(void);
-
 /**
  * gpio_lookup_name - Look up a GPIO name and return its details
  *
Signature · 1 line
2.35.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Marek Vasut

The gpio_hog_probe_all() functionality can be perfectly well replaced by DM_FLAG_PROBE_AFTER_BIND DM flag, which would trigger .probe() callback of each GPIO hog driver instance after .bind() and thus configure the hogged GPIO accordingly.

---
Cc: Heinrich Schuchardt <[email protected]>
Cc: Samuel Holland <[email protected]>
Cc: Simon Glass <[email protected]>

Quoted 7 lines
---
 common/board_r.c           |  3 ---
 common/spl/spl.c           |  3 ---
 doc/README.gpio            |  6 ++----
 drivers/gpio/gpio-uclass.c | 25 ++-----------------------
 include/asm-generic/gpio.h |  8 --------
 5 files changed, 4 insertions(+), 41 deletions(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Marek Vasut

Hi,

Quoted 10 lines · Marek Vasut

The gpio_hog_probe_all() functionality can be perfectly well replaced by DM_FLAG_PROBE_AFTER_BIND DM flag, which would trigger .probe() callback of each GPIO hog driver instance after .bind() and thus configure the hogged GPIO accordingly.

---
Cc: Heinrich Schuchardt <[email protected]>
Cc: Samuel Holland <[email protected]>
Cc: Simon Glass <[email protected]>

Quoted 8 lines
---
  common/board_r.c           |  3 ---
  common/spl/spl.c           |  3 ---
  doc/README.gpio            |  6 ++----
  drivers/gpio/gpio-uclass.c | 25 ++-----------------------
  include/asm-generic/gpio.h |  8 --------
  5 files changed, 4 insertions(+), 41 deletions(-)

Thanks
Patrick

#3 lore raw
In reply to Marek Vasut

Hi Marek,

Quoted 6 lines · Marek Vasut

The gpio_hog_probe_all() functionality can be perfectly well replaced by DM_FLAG_PROBE_AFTER_BIND DM flag, which would trigger .probe() callback of each GPIO hog driver instance after .bind() and thus configure the hogged GPIO accordingly.

This patch breaks the U-Boot proper loading fallback mechanism on Puma RK3399.

https://lore.kernel.org/u-boot/[email protected]/ is the base I used, on top of commit d6a03711fd with your patch applied on top.

I need the GPIO hogs to be probed before the SPL starts looking for storage media for U-Boot proper because the GPIO hog is necessary on the HW level for the eMMC and SPI-NOR flash to be usable.

Basically, we have a switch on the board disabling eMMC and SPI-NOR so that we can boot from SD card, but we want to be able to load U-Boot proper from eMMC/SPI-NOR. The GPIO hog overrides this HW switch. Use case is to recover from a corrupted SPL without touching U-Boot proper.

Cheers,
Quentin

#4 lore raw
Quoted 1 line · Quentin Schulz

Hi Marek,

Hi,

Quoted 6 lines · Marek Vasut
Quoted 5 lines

The gpio_hog_probe_all() functionality can be perfectly well replaced by DM_FLAG_PROBE_AFTER_BIND DM flag, which would trigger .probe() callback of each GPIO hog driver instance after .bind() and thus configure the hogged GPIO accordingly.

Quoted 7 lines
Quoted 1 line

This patch breaks the U-Boot proper loading fallback mechanism on Puma RK3399.

https://lore.kernel.org/u-boot/[email protected]/ is the base I used, on top of commit d6a03711fd with your patch applied on top.

I need the GPIO hogs to be probed before the SPL starts looking for storage media for U-Boot proper because the GPIO hog is necessary on the HW level for the eMMC and SPI-NOR flash to be usable.

That _should_ still work.

Look at common/spl/spl.c board_init_r() calls spl_init() calls spl_common_init() -> dm_init_and_scan() . That should bind and probe the GPIO hogs for you . The old gpio_hog_probe_all() happened AFTER all this.

Can you use e.g. dm_dump_all() call in or around spl_board_init() to check whether the hogs got probed in SPL ?

Quoted 1 line

Basically, we have a switch on the board disabling eMMC and SPI-NOR so that we can boot from SD card, but we want to be able to load U-Boot proper from eMMC/SPI-NOR. The GPIO hog overrides this HW switch. Use case is to recover from a corrupted SPL without touching U-Boot proper.

I don't quite get this -- isn't board_boot_order() or spl_boot_device() used for the purpose of selecting boot media , including fallback ?

[...]

#5 lore raw

Hi all,

Just so there's a written summary on the ML rather than only on IRC.

Quoted 17 lines · Marek Vasut
Quoted 1 line · Quentin Schulz

Hi Marek,

Hi,

Quoted 12 lines · Marek Vasut
Quoted 6 lines

The gpio_hog_probe_all() functionality can be perfectly well replaced by DM_FLAG_PROBE_AFTER_BIND DM flag, which would trigger .probe() callback of each GPIO hog driver instance after .bind() and thus configure the hogged GPIO accordingly.

This patch breaks the U-Boot proper loading fallback mechanism on Puma RK3399.

https://urldefense.proofpoint.com/v2/url?u=https-3A__lore.kernel.org_u-2Dboot_20220915091432.789294-2D1-2Dfoss-2Buboot-400leil.net_&d=DwICaQ&c=_sEr5x9kUWhuk4_nFwjJtA&r=LYjLexDn7rXIzVmkNPvw5ymA1XTSqHGq8yBP6m6qZZ4njZguQhZhkI_-172IIy1t&m=aMC9n5Cgsl79NAwWBVdx4n9MNHT7Jcm1g4sKww821Kd8JuvW1wjvVHLLPU2XsXVe&s=YbHd2jLJ2foRj_fTpj_PNmI5mXcUi5wGpzC3oL_rcVs&e=  is the base I used, on top of commit d6a03711fd with your patch applied on top.

Quoted 8 lines
Quoted 1 line

I need the GPIO hogs to be probed before the SPL starts looking for storage media for U-Boot proper because the GPIO hog is necessary on the HW level for the eMMC and SPI-NOR flash to be usable.

That _should_ still work.

Look at common/spl/spl.c board_init_r() calls spl_init() calls spl_common_init() -> dm_init_and_scan() . That should bind and probe the GPIO hogs for you . The old gpio_hog_probe_all() happened AFTER all this.

Can you use e.g. dm_dump_all() call in or around spl_board_init() to check whether the hogs got probed in SPL ?

After some debugging with Marek, we found out that DM_FLAG_PRE_RELOC flag is not set on the gpio hogs and thus, dm_probe_devices function in drivers/core/root.c will not call device_probe() function.

drivers/gpio/gpio-uclass.c +1 -1
index 32df0448a7..37a8706746 100644
@@ -1481,7 +1481,7 @@ static int gpio_post_bind(struct udevice *dev)
                                if (ret)
                                        return ret;
 
-                               dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND);
+                               dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND | DM_FLAG_PRE_RELOC);
                        }
                }
        }

made it work but it is not good enough (the flag needs to be added only if the DT property is there for example).

One way could be to add this flag in a post_bind callback of the gpio-hog driver.

But I believe a better way would have the DM code actually handle this on its own instead of relying on subsystems/drivers to do it.

Something like:

drivers/core/device.c +4 -0
index d9ce546c0c..103ec47b88 100644
@@ -174,6 +174,10 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv,
 		if (ret)
 			goto fail_child_post_bind;
 	}
+
+	if (ofnode_pre_reloc(node))
+		dev_or_flags(dev, DM_FLAG_PRE_RELOC);
+
 	if (uc->uc_drv->post_bind) {
 		ret = uc->uc_drv->post_bind(dev);
 		if (ret)

but with the knowledge of pre_reloc_only flag I guess? I have no experience in the DM code so maybe someone will have a better idea/quick implementation, so that this patch can actually be merged without breaking stuff :)

Quoted 5 lines
Quoted 1 line

Basically, we have a switch on the board disabling eMMC and SPI-NOR so that we can boot from SD card, but we want to be able to load U-Boot proper from eMMC/SPI-NOR. The GPIO hog overrides this HW switch. Use case is to recover from a corrupted SPL without touching U-Boot proper.

I don't quite get this -- isn't board_boot_order() or spl_boot_device() used for the purpose of selecting boot media , including fallback ?

[...]

In short, we have a switch on the devkit which allows to cut the power and clock lines to the eMMC and put the SPI-NOR flash in HOLD mode (reset basically).

We then have a GPIO we can control with SW that allows to electrically override this switch on the devkit so that at runtime we can enable eMMC and SPI-NOR flash.

The boot selection is handled by U-Boot (Rockchip mach directory deals with spl_boot_order), but the devices aren't physically usable until this GPIO is used to electrically override the switch on the devkit.

The use case being: I manually (and electrically) disable eMMC and SPI flash with the switch to boot from SD Card, then if U-Boot proper on the SD card is corrupted or not present, fallback to loading it from eMMC or SPI flash.

Cheers,
Quentin

#6 lore raw
Quoted 1 line · Quentin Schulz

Hi all,

Hi,

Quoted 1 line

Just so there's a written summary on the ML rather than only on IRC.

Thanks

[...]

Quoted 19 lines

Something like:

drivers/core/device.c +4 -0
index d9ce546c0c..103ec47b88 100644
@@ -174,6 +174,10 @@ static int device_bind_common(struct udevice 
 *parent, const struct driver *drv,
          if (ret)
              goto fail_child_post_bind;
      }
+
+    if (ofnode_pre_reloc(node))
+        dev_or_flags(dev, DM_FLAG_PRE_RELOC);
+
      if (uc->uc_drv->post_bind) {
          ret = uc->uc_drv->post_bind(dev);

         if (ret)

Quoted 1 line

but with the knowledge of pre_reloc_only flag I guess? I have no experience in the DM code so maybe someone will have a better idea/quick implementation, so that this patch can actually be merged without breaking stuff :)

Can you send the above as an actual proper patch and CC Simon and me please ? I think this is actually even a bugfix for DM core.

The rest is clear, thanks .

#7 lore raw
In reply to Marek Vasut

Hi Marek,

Quoted 103 lines · Marek Vasut

The gpio_hog_probe_all() functionality can be perfectly well replaced by DM_FLAG_PROBE_AFTER_BIND DM flag, which would trigger .probe() callback of each GPIO hog driver instance after .bind() and thus configure the hogged GPIO accordingly.

---
Cc: Heinrich Schuchardt <[email protected]>
Cc: Samuel Holland <[email protected]>
Cc: Simon Glass <[email protected]>

---
  common/board_r.c           |  3 ---
  common/spl/spl.c           |  3 ---
  doc/README.gpio            |  6 ++----
  drivers/gpio/gpio-uclass.c | 25 ++-----------------------
  include/asm-generic/gpio.h |  8 --------
  5 files changed, 4 insertions(+), 41 deletions(-)
common/board_r.c +0 -3
index 56eb60fa275..c556aa5a073 100644
@@ -750,9 +750,6 @@ static init_fnc_t init_sequence_r[] = {
  	initr_status_led,
  #endif
  	/* PPC has a udelay(20) here dating from 2002. Why? */
-#if defined(CONFIG_GPIO_HOG)
-	gpio_hog_probe_all,
-#endif
  #ifdef CONFIG_BOARD_LATE_INIT
  	board_late_init,
  #endif
common/spl/spl.c +0 -3
index 29e0898f03d..683e0dfc526 100644
@@ -770,9 +770,6 @@ void board_init_r(gd_t *dummy1, ulong dummy2)
  		}
  	}
 
-	if (CONFIG_IS_ENABLED(GPIO_HOG))
-		gpio_hog_probe_all();
-
  #if CONFIG_IS_ENABLED(BOARD_INIT)
  	spl_board_init();
  #endif
doc/README.gpio +2 -4
index 548ff37b8cc..d253f654fad 100644
@@ -2,10 +2,8 @@
  GPIO hog (CONFIG_GPIO_HOG)
  --------
 
-All the GPIO hog are initialized in gpio_hog_probe_all() function called in
-board_r.c just before board_late_init() but you can also acces directly to
-the gpio with gpio_hog_lookup_name().
-
+All the GPIO hog are initialized using DM_FLAG_PROBE_AFTER_BIND DM flag
+after bind().
 
  Example, for the device tree:
 
drivers/gpio/gpio-uclass.c +0 -23
index 0ed32b72170..32df0448a7b 100644
@@ -315,34 +315,11 @@ static int gpio_hog_probe(struct udevice *dev)
  	return 0;
  }
 
-int gpio_hog_probe_all(void)
-{
-	struct udevice *dev;
-	int ret;
-	int retval = 0;
-
-	for (uclass_first_device(UCLASS_NOP, &dev);
-	     dev;
-	     uclass_find_next_device(&dev)) {
-		if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
-			ret = device_probe(dev);
-			if (ret) {
-				printf("Failed to probe device %s err: %d\n",
-				       dev->name, ret);
-				retval = ret;
-			}
-		}
-	}
-
-	return retval;
-}
-
  int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
  {
  	struct udevice *dev;
 
  	*desc = NULL;
-	gpio_hog_probe_all();
  	if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
  		struct gpio_hog_priv *priv = dev_get_priv(dev);
 

drivers/gpio/gpio-uclass.c static int gpio_post_bind(struct udevice *dev) · line 1484

Quoted 5 lines
drivers/gpio/gpio-uclass.c +2 -0
@@ -1503,6 +1480,8 @@ static int gpio_post_bind(struct udevice *dev)
  								 &child);
  				if (ret)
  					return ret;
+
+				dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND);

I replaced this line with:

+ /*

+                                * Make sure gpio-hogs are probed after bind
+                                * since hogs can be essential to the hardware
+                                * system.
+                                */
+                               dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND);
+
+                               /*
+                                * Since gpio-hog is a U_BOOT_DRIVER and not
+                                * a U_BOOT_CLASS, the DM core does not bind
+                                * it and therefore it's up to this driver to
+                                * set the DM_FLAG_PRE_RELOC appropriately.
+                                */

+ if (ofnode_pre_reloc(node))
+ dev_or_flags(child, DM_FLAG_PRE_RELOC);

and it works fine for me now (no dependency on the core DM patch I sent just a few minutes ago; I assume it's because we revert-recursively probe the parents of such devices?)

Cheers,
Quentin

#8 lore raw
Quoted 1 line · Quentin Schulz

Hi Marek,

Hi,

[...]

Quoted 75 lines
Quoted 53 lines
doc/README.gpio +0 -0
index 548ff37b8cc..d253f654fad 100644
@@ -2,10 +2,8 @@
   GPIO hog (CONFIG_GPIO_HOG)

  --------
-All the GPIO hog are initialized in gpio_hog_probe_all() function called in
-board_r.c just before board_late_init() but you can also acces directly to

doc/README.gpio +2 -2
-the gpio with gpio_hog_lookup_name().
-
+All the GPIO hog are initialized using DM_FLAG_PROBE_AFTER_BIND DM flag
+after bind().

  Example, for the device tree:

drivers/gpio/gpio-uclass.c +0 -23
index 0ed32b72170..32df0448a7b 100644
@@ -315,34 +315,11 @@ static int gpio_hog_probe(struct udevice *dev)
       return 0;
   }
-int gpio_hog_probe_all(void)
-{
-    struct udevice *dev;
-    int ret;
-    int retval = 0;
-
-    for (uclass_first_device(UCLASS_NOP, &dev);
-         dev;
-         uclass_find_next_device(&dev)) {
-        if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
-            ret = device_probe(dev);
-            if (ret) {
-                printf("Failed to probe device %s err: %d\n",
-                       dev->name, ret);
-                retval = ret;
-            }
-        }
-    }
-
-    return retval;
-}
-
   int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
   {
       struct udevice *dev;
       *desc = NULL;
-    gpio_hog_probe_all();
       if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
           struct gpio_hog_priv *priv = dev_get_priv(dev);

drivers/gpio/gpio-uclass.c static int gpio_post_bind(struct udevice *dev) · line 1484

Quoted 5 lines
drivers/gpio/gpio-uclass.c +2 -0
@@ -1503,6 +1480,8 @@ static int gpio_post_bind(struct udevice *dev)
                                    &child);
                   if (ret)
                       return ret;
+
+                dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND);

I replaced this line with:

+                               /*
+                                * Make sure gpio-hogs are probed after bind
+                                * since hogs can be essential to the hardware
+                                * system.
+                                */
+                               dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND);
+
+                               /*
+                                * Since gpio-hog is a U_BOOT_DRIVER and not
+                                * a U_BOOT_CLASS, the DM core does not

Quoted 8 lines

bind
+                                * it and therefore it's up to this driver to
+                                * set the DM_FLAG_PRE_RELOC appropriately. +                                */
+                               if (ofnode_pre_reloc(node))
+                                       dev_or_flags(child, DM_FLAG_PRE_RELOC);

This second part should be handled by the DM, or you need dm-pre-reloc in your GPIO controller in DT. This would fail e.g. in case your GPIO controller has higher depth of hog subnodes, like:

gpio-controller {

  something {
    gpio-hog {
      u-boot,dm-pre-reloc;
    };

};
};

Should really be:

gpio-controller {
u-boot,dm-pre-reloc;

  something {
    u-boot,dm-pre-reloc;
    gpio-hog {
      u-boot,dm-pre-reloc;
    };

};
};

At some point, I had the idea to instead of littering the DT with u-boot,dm-pre-reloc , we could use phandles instead and do something like:

/ {
config {
u-boot,dm-pre-reloc = <&node1 &node2 ... &gpio_hog ...>;
};
}
...
gpio-controller {

  something {
    gpio_hog: gpio-hog {
    };

};
};