dm: core: support reading a single indexed u32 value
authorDario Binacchi <dariobin@libero.it>
Sun, 29 Mar 2020 16:04:41 +0000 (18:04 +0200)
committerSimon Glass <sjg@chromium.org>
Thu, 16 Apr 2020 14:07:58 +0000 (08:07 -0600)
The patch adds helper functions to allow reading a single indexed u32
value from a device-tree property containing multiple u32 values, that
is an array of integers.

Signed-off-by: Dario Binacchi <dariobin@libero.it>
Reviewed-by: Simon Glass <sjg@chromium.org>
arch/sandbox/dts/test.dts
drivers/core/of_access.c
drivers/core/ofnode.c
drivers/core/read.c
include/dm/of_access.h
include/dm/ofnode.h
include/dm/read.h
test/dm/test-fdt.c

index 8c6a48d195ed00ae7e5e2d1af866547f91928913..1973623a992bb7cc301b3da3a6d6695a9bbf8d6f 100644 (file)
@@ -94,6 +94,7 @@
                int-value = <1234>;
                uint-value = <(-1234)>;
                int64-value = /bits/ 64 <0x1111222233334444>;
+               int-array = <5678 9123 4567>;
                interrupts-extended = <&irq 3 0>;
        };
 
index 29e705e0b660c4ee7fd3a59852b3fde2af51c0d2..8b2ce7a0c21b5272548007af8a67087e78fac080 100644 (file)
@@ -485,6 +485,28 @@ int of_read_u32_array(const struct device_node *np, const char *propname,
        return 0;
 }
 
+int of_read_u32_index(const struct device_node *np, const char *propname,
+                     int index, u32 *outp)
+{
+       const __be32 *val;
+
+       debug("%s: %s: ", __func__, propname);
+       if (!np)
+               return -EINVAL;
+
+       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 = be32_to_cpup(val + index);
+       debug("%#x (%d)\n", *outp, *outp);
+
+       return 0;
+}
+
 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
 {
        const __be64 *val;
index 96a5dd20bd155db3bcaecfa7f967135b254c7b3e..5bc3b02996ebf972d2575804830b5829f9a8af61 100644 (file)
@@ -48,6 +48,46 @@ u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
        return def;
 }
 
+int ofnode_read_u32_index(ofnode node, const char *propname, int index,
+                         u32 *outp)
+{
+       const fdt32_t *cell;
+       int len;
+
+       assert(ofnode_valid(node));
+       debug("%s: %s: ", __func__, propname);
+
+       if (ofnode_is_np(node))
+               return of_read_u32_index(ofnode_to_np(node), propname, index,
+                                        outp);
+
+       cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
+                          &len);
+       if (!cell) {
+               debug("(not found)\n");
+               return -EINVAL;
+       }
+
+       if (len < (sizeof(int) * (index + 1))) {
+               debug("(not large enough)\n");
+               return -EOVERFLOW;
+       }
+
+       *outp = fdt32_to_cpu(cell[index]);
+       debug("%#x (%d)\n", *outp, *outp);
+
+       return 0;
+}
+
+u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
+                                 u32 def)
+{
+       assert(ofnode_valid(node));
+       ofnode_read_u32_index(node, propname, index, &def);
+
+       return def;
+}
+
 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
 {
        assert(ofnode_valid(node));
index 1f999b1b316c01fabb643c5ccfa631a50549a130..ce78f09d286f47a5c8e5112bf0ded892e6cef8a1 100644 (file)
@@ -22,6 +22,19 @@ int dev_read_u32_default(const struct udevice *dev, const char *propname,
        return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
 }
 
+int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
+                      u32 *outp)
+{
+       return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
+}
+
+u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
+                              int index, u32 def)
+{
+       return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
+                                            def);
+}
+
 int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
 {
        return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
index 13fedb7cf5e60e6ff11e49ac6a39d3dc5baeecb4..92876b3ecb698221fde654821ef13f46f326c2cc 100644 (file)
@@ -234,6 +234,25 @@ struct device_node *of_find_node_by_phandle(phandle handle);
  */
 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
 
+/**
+ * of_read_u32_index() - Find and read a 32-bit value from a multi-value
+ *                       property
+ *
+ * Search for a property in a device node and read a 32-bit value from
+ * it.
+ *
+ * @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.
+ *
+ * @return 0 on success, -EINVAL if the property does not exist,
+ * -ENODATA if property does not have a value, and -EOVERFLOW if the
+ * property data isn't large enough.
+ */
+int of_read_u32_index(const struct device_node *np, const char *propname,
+                     int index, u32 *outp);
+
 /**
  * of_read_u64() - Find and read a 64-bit integer from a property
  *
index b5a50e88499a04d15b7e0c3cee29367a7ed31454..ce5e366c06273f643c45a0926752cea08cf56997 100644 (file)
@@ -202,6 +202,18 @@ static inline ofnode ofnode_null(void)
  */
 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
 
+/**
+ * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
+ *
+ * @ref:       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_u32_index(ofnode node, const char *propname, int index,
+                         u32 *outp);
+
 /**
  * ofnode_read_s32() - Read a 32-bit integer from a property
  *
@@ -226,6 +238,19 @@ static inline int ofnode_read_s32(ofnode node, const char *propname,
  */
 u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
 
+/**
+ * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
+ *                                   property
+ *
+ * @ref:       valid node reference to read property from
+ * @propname:  name of the property to read from
+ * @index:     index of the integer to return
+ * @def:       default value to return if the property has no value
+ * @return property value, or @def if not found
+ */
+u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
+                                 u32 def);
+
 /**
  * ofnode_read_s32_default() - Read a 32-bit integer from a property
  *
index da8c7f25e7ca08421fe4f4d13cc368b051ff31f1..77d3bc8db5beeee637b4dbac574cf3d647c83699 100644 (file)
@@ -66,6 +66,32 @@ int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
 int dev_read_u32_default(const struct udevice *dev, const char *propname,
                         int def);
 
+/**
+ * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
+ *                        property
+ *
+ * @dev:       device to read DT 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 dev_read_u32_index(struct udevice *dev, const char *propname, int index,
+                      u32 *outp);
+
+/**
+ * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
+ *                                DT property
+ *
+ * @dev:       device to read DT property from
+ * @propname:  name of the property to read from
+ * @index:     index of the integer to return
+ * @def:       default value to return if the property has no value
+ * @return property value, or @def if not found
+ */
+u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
+                              int index, u32 def);
+
 /**
  * dev_read_s32() - read a signed 32-bit integer from a device's DT property
  *
@@ -621,6 +647,20 @@ static inline int dev_read_u32_default(const struct udevice *dev,
        return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
 }
 
+static inline int dev_read_u32_index(struct udevice *dev,
+                                    const char *propname, int index, u32 *outp)
+{
+       return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
+}
+
+static inline u32 dev_read_u32_index_default(struct udevice *dev,
+                                            const char *propname, int index,
+                                            u32 def)
+{
+       return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
+                                            def);
+}
+
 static inline int dev_read_s32(const struct udevice *dev,
                               const char *propname, s32 *outp)
 {
index b39777f082c224bfb8a30b0aabc8057f066c030c..7c9472a3588c903d39929ab33fbe676bc8068f6e 100644 (file)
@@ -905,6 +905,35 @@ static int dm_test_read_int(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_read_int, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
 
+static int dm_test_read_int_index(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+       u32 val32;
+
+       ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
+       ut_asserteq_str("a-test", dev->name);
+
+       ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
+       ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
+
+       ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
+       ut_asserteq(5678, val32);
+       ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
+       ut_asserteq(9123, val32);
+       ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
+       ut_asserteq(4567, val32);
+       ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
+                                                  &val32));
+
+       ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
+       ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
+       ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
+       ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
+
+       return 0;
+}
+DM_TEST(dm_test_read_int_index, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
 /* Test iteration through devices by drvdata */
 static int dm_test_uclass_drvdata(struct unit_test_state *uts)
 {