Refactoring and cleanup of vc fdt parsing based on advice
from Ahmad Fatoum.

Signed-off-by: Daniel Brát <[email protected]>
---
I originally wanted to send these as part of v5 of
'ARM: rpi: parse useful data from vc fdt' patch, but you
were quicker with accepting the v4 :)
Hope that's not a problem.

 arch/arm/boards/raspberry-pi/rpi-common.c | 114 +++++++++++-----------
 1 file changed, 58 insertions(+), 56 deletions(-)

diff --git a/arch/arm/boards/raspberry-pi/rpi-common.c 
b/arch/arm/boards/raspberry-pi/rpi-common.c
index eaca4edef..77935e5c8 100644
--- a/arch/arm/boards/raspberry-pi/rpi-common.c
+++ b/arch/arm/boards/raspberry-pi/rpi-common.c
@@ -202,22 +202,52 @@ static int rpi_env_init(void)
 }
 
 /* Some string properties in fdt passed to us from vc may be
- * malformed by not being null terminated. In that case, create and
- * return a null terminated copy.
+ * malformed by not being null terminated, so just create and
+ * return a fixed copy.
  */
 static char *of_read_vc_string(struct device_node *node,
-                                    const char *prop_name,
-                                    bool *is_dyn)
+                              const char *prop_name)
 {
        int len;
-       char *str;
+       const char *str;
 
-       str = (char *)of_get_property(node, prop_name, &len);
-       if (!str || len <= 0)
+       str = of_get_property(node, prop_name, &len);
+       if (!str) {
+               pr_warn("no property '%s' found in vc fdt's '%s' node\n",
+                       prop_name, node->full_name);
                return NULL;
-       if (is_dyn)
-               *is_dyn = str[len - 1] != '\0';
-       return str[len - 1] == '\0' ? str : xstrndup(str, len);
+       }
+       return xstrndup(str, len);
+}
+
+static enum reset_src_type rpi_decode_pm_rsts(struct device_node *chosen,
+                                             struct device_node *bootloader)
+{
+       u32 pm_rsts;
+       int ret;
+
+       ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
+       if (ret && bootloader)
+                ret = of_property_read_u32(bootloader, "rsts", &pm_rsts);
+
+       if (ret) {
+               pr_warn("'pm_rsts' value not found in vc fdt\n");
+               return RESET_UKWN;
+       }
+       /*
+        * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
+        */
+
+       if (pm_rsts & PM_RSTS_HADPOR_SET)
+               return RESET_POR;
+       if (pm_rsts & PM_RSTS_HADDR_SET)
+               return RESET_JTAG;
+       if (pm_rsts & PM_RSTS_HADWR_SET)
+               return RESET_WDG;
+       if (pm_rsts & PM_RSTS_HADSR_SET)
+               return RESET_RST;
+
+       return RESET_UKWN;
 }
 
 static u32 rpi_boot_mode, rpi_boot_part;
@@ -229,31 +259,22 @@ static void rpi_vc_fdt_parse(void *fdt)
 {
        int ret;
        struct device_node *root, *chosen, *bootloader;
-       enum reset_src_type rst_src = RESET_UKWN;
        char *str;
-       bool is_dyn;
-       u32 pm_rsts;
 
        root = of_unflatten_dtb(fdt, INT_MAX);
        if (IS_ERR(root))
                return;
 
-       str = of_read_vc_string(root, "serial-number", &is_dyn);
+       str = of_read_vc_string(root, "serial-number");
        if (str) {
                barebox_set_serial_number(str);
-               if (is_dyn)
-                       free(str);
-       } else {
-               pr_warn("no 'serial-number' property found in vc fdt root\n");
+               free(str);
        }
 
-       str = of_read_vc_string(root, "model", &is_dyn);
+       str = of_read_vc_string(root, "model");
        if (str) {
                barebox_set_model(str);
-               if (is_dyn)
-                       free(str);
-       } else {
-               pr_warn("no 'model' property found in vc fdt root\n");
+               free(str);
        }
 
        chosen = of_find_node_by_path_from(root, "/chosen");
@@ -264,23 +285,23 @@ static void rpi_vc_fdt_parse(void *fdt)
 
        bootloader = of_find_node_by_name(chosen, "bootloader");
 
-       str = of_read_vc_string(chosen, "bootargs", NULL);
-       if (str)
+       str = of_read_vc_string(chosen, "bootargs");
+       if (str) {
                globalvar_add_simple("vc.bootargs", str);
-       else
-               pr_warn("no 'bootargs' property found in vc fdt '/chosen'\n");
+               free(str);
+       }
 
-       str = of_read_vc_string(chosen, "overlay_prefix", NULL);
-       if (str)
+       str = of_read_vc_string(chosen, "overlay_prefix");
+       if (str) {
                globalvar_add_simple("vc.overlay_prefix", str);
-       else
-               pr_warn("no 'overlay_prefix' property found in vc fdt 
'/chosen'\n");
+               free(str);
+       }
 
-       str = of_read_vc_string(chosen, "os_prefix", NULL);
-       if (str)
+       str = of_read_vc_string(chosen, "os_prefix");
+       if (str) {
                globalvar_add_simple("vc.os_prefix", str);
-       else
-               pr_warn("no 'os_prefix' property found in vc fdt '/chosen'\n");
+               free(str);
+       }
 
        ret = of_property_read_u32(chosen, "boot-mode", &rpi_boot_mode);
        if (ret && bootloader)
@@ -300,28 +321,9 @@ static void rpi_vc_fdt_parse(void *fdt)
        else
                globalvar_add_simple_int("vc.boot_partition", &rpi_boot_part, 
"%u");
 
-       if (IS_ENABLED(CONFIG_RESET_SOURCE)) {
-               ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
-               if (ret && bootloader)
-                        ret = of_property_read_u32(bootloader, "rsts", 
&pm_rsts);
+       if (IS_ENABLED(CONFIG_RESET_SOURCE))
+               reset_source_set(rpi_decode_pm_rsts(chosen, bootloader));
 
-               if (ret) {
-                       pr_warn("pm_rsts could not be get from vc fdt\n");
-               } else {
-/*
- * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
- */
-                       if (pm_rsts & PM_RSTS_HADPOR_SET)
-                               rst_src = RESET_POR;
-                       else if (pm_rsts & PM_RSTS_HADDR_SET)
-                               rst_src = RESET_JTAG;
-                       else if (pm_rsts & PM_RSTS_HADWR_SET)
-                               rst_src = RESET_WDG;
-                       else if (pm_rsts & PM_RSTS_HADSR_SET)
-                               rst_src = RESET_RST;
-                       reset_source_set(rst_src);
-               }
-       }
 out:
        if (root)
                of_delete_node(root);
-- 
2.17.1


Reply via email to