]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
dm: core: support reading a single indexed u64 value
authorMichal Simek <michal.simek@amd.com>
Fri, 25 Aug 2023 09:37:46 +0000 (11:37 +0200)
committerMichal Simek <michal.simek@amd.com>
Thu, 21 Sep 2023 11:20:10 +0000 (13:20 +0200)
Add helper function to allow reading a single indexed u64 value from a
device-tree property containing multiple u64 values, that is an array of
u64's.

Co-developed-by: Ashok Reddy Soma <ashok.reddy.soma@amd.com>
Signed-off-by: Ashok Reddy Soma <ashok.reddy.soma@amd.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Michal Simek <michal.simek@amd.com>
Link: https://lore.kernel.org/r/08043c8d204d0068f04c27de86afe78c75c50b69.1692956263.git.michal.simek@amd.com
arch/sandbox/dts/test.dts
drivers/core/of_access.c
drivers/core/ofnode.c
include/dm/of_access.h
include/dm/ofnode.h
test/dm/ofnode.c

index 63fda15da76418a2266e3bd1a14516045856aca5..d4693e3c7a937b0593ea24956f3474daeb7bcc7e 100644 (file)
                uint-value = <(-1234)>;
                int64-value = /bits/ 64 <0x1111222233334444>;
                int-array = <5678 9123 4567>;
+               int64-array = /bits/ 64 <0x1111222233334444 0x4444333322221111>;
                str-value = "test string";
                interrupts-extended = <&irq 3 0>;
                acpi,name = "GHIJ";
index 57f10445b1221df353fc8f452ca45cba865c53d8..1bb4d8eab709a7af308e117f7141cddf35d26d7a 100644 (file)
@@ -570,26 +570,34 @@ int of_read_u32_index(const struct device_node *np, const char *propname,
        return 0;
 }
 
-int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
+int of_read_u64_index(const struct device_node *np, const char *propname,
+                     int index, u64 *outp)
 {
        const __be64 *val;
 
        debug("%s: %s: ", __func__, propname);
        if (!np)
                return -EINVAL;
-       val = of_find_property_value_of_size(np, propname, sizeof(*outp));
+
+       val = of_find_property_value_of_size(np, propname,
+                                            sizeof(*outp) * (index + 1));
        if (IS_ERR(val)) {
                debug("(not found)\n");
                return PTR_ERR(val);
        }
 
-       *outp = be64_to_cpup(val);
+       *outp = be64_to_cpup(val + index);
        debug("%#llx (%lld)\n", (unsigned long long)*outp,
-              (unsigned long long)*outp);
+             (unsigned long long)*outp);
 
        return 0;
 }
 
+int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
+{
+       return of_read_u64_index(np, propname, 0, outp);
+}
+
 int of_property_match_string(const struct device_node *np, const char *propname,
                             const char *string)
 {
index a4dc9bde085cce514bc56c0deb791fcc8ea7be3d..8311282abf69331964d0891a4f291a71db6092de 100644 (file)
@@ -344,6 +344,36 @@ int ofnode_read_u32_index(ofnode node, const char *propname, int index,
        return 0;
 }
 
+int ofnode_read_u64_index(ofnode node, const char *propname, int index,
+                         u64 *outp)
+{
+       const fdt64_t *cell;
+       int len;
+
+       assert(ofnode_valid(node));
+
+       if (ofnode_is_np(node))
+               return of_read_u64_index(ofnode_to_np(node), propname, index,
+                                        outp);
+
+       cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
+                          propname, &len);
+       if (!cell) {
+               debug("(not found)\n");
+               return -EINVAL;
+       }
+
+       if (len < (sizeof(u64) * (index + 1))) {
+               debug("(not large enough)\n");
+               return -EOVERFLOW;
+       }
+
+       *outp = fdt64_to_cpu(cell[index]);
+       debug("%#llx (%lld)\n", *outp, *outp);
+
+       return 0;
+}
+
 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
                                  u32 def)
 {
index c556a18f7d9c5aeb37540f4b64692d62d5864466..9361d0a87bfbd705fd0408fb477728a62c418b12 100644 (file)
@@ -333,6 +333,25 @@ int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
 int of_read_u32_index(const struct device_node *np, const char *propname,
                      int index, u32 *outp);
 
+/**
+ * of_read_u64_index() - Find and read a 64-bit value from a multi-value
+ *                       property
+ *
+ * @np:                device node from which the property value is to be read.
+ * @propname:  name of the property to be searched.
+ * @index:     index of the u32 in the list of values
+ * @outp:      pointer to return value, modified only if return value is 0.
+ *
+ * Search for a property in a device node and read a 64-bit value from
+ * it.
+ *
+ * Return:
+ *   0 on success, -EINVAL if the property does not exist, or -EOVERFLOW if the
+ *   property data isn't large enough.
+ */
+int of_read_u64_index(const struct device_node *np, const char *propname,
+                     int index, u64 *outp);
+
 /**
  * of_read_u64() - Find and read a 64-bit integer from a property
  *
index 0f38b3e736de415dcd206587340b0424afaff29d..0a85db31f3604ccbe608656768a6957eb346e5e1 100644 (file)
@@ -434,6 +434,18 @@ int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
                          u32 *outp);
 
+/**
+ * ofnode_read_u64_index() - Read a 64-bit integer from a multi-value property
+ *
+ * @node:      valid node reference to read property from
+ * @propname:  name of the property to read from
+ * @index:     index of the integer to return
+ * @outp:      place to put value (if found)
+ * Return: 0 if OK, -ve on error
+ */
+int ofnode_read_u64_index(ofnode node, const char *propname, int index,
+                         u64 *outp);
+
 /**
  * ofnode_read_s32() - Read a 32-bit integer from a property
  *
index 6fbebc7da08540bbbe6b9f9907bcd699c34c4dc6..b74f5c2ebcff3e91b5b3620bd01bf16a4a616e38 100644 (file)
@@ -967,6 +967,14 @@ static int dm_test_ofnode_u64(struct unit_test_state *uts)
        ut_asserteq_64(0x1111222233334444, val);
        ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
 
+       ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val));
+       ut_asserteq_64(0x1111222233334444, val);
+       ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val));
+       ut_asserteq_64(0x4444333322221111, val);
+       ut_asserteq(-EOVERFLOW,
+                   ofnode_read_u64_index(node, "int64-array", 2, &val));
+       ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val));
+
        return 0;
 }
 DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);