From patchwork Sun Mar 29 16:04:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 1263471 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=libero.it header.i=@libero.it header.a=rsa-sha256 header.s=s2014 header.b=VmjvQ2bg; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48r0nL07Gmz9sPk for ; Mon, 30 Mar 2020 03:05:57 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D8779818B6; Sun, 29 Mar 2020 18:05:42 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=libero.it header.i=@libero.it header.b="VmjvQ2bg"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3993E818BD; Sun, 29 Mar 2020 18:05:37 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FORGED_SPF_HELO, FREEMAIL_FROM, SPF_HELO_PASS, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from libero.it (smtp-34.italiaonline.it [213.209.10.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C7EA0804CD for ; Sun, 29 Mar 2020 18:05:33 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=dariobin@libero.it Received: from localhost.localdomain ([79.45.94.224]) by smtp-34.iol.local with ESMTPA id IaR4jI7q4cy2wIaRJjboBw; Sun, 29 Mar 2020 18:05:33 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2014; t=1585497933; bh=FvQ7EViB+XlKJwhfwFf5Qahmuy7vmaWWb9GYxalbjhk=; h=From; b=VmjvQ2bgFXtSCfvLEwg/HFBiuDrWlmOXmferzSkNdZCeA5fdodgMQHg4IIwhZ2gXA YKChcn7mSfIZabZ60AZfPU5phQAvmn7HhCHK4gfQmohNNXpPRHaOQSsiBkkv8JnUFR xfdbGXbJ8lrJA3FX8k8F/e414QDLSv39tqiz5b8TD4+5dD7C1wM0qGb2kwnsMx5Tjz hRkg9XB/8vaG+f91Gp+efedd8aTSQJbyohmB2FmOg6b2w8in75EQAjURNdzb897Fir z44M9xpyiEtge/PKpCjb8B/MIjYXLX8m+dAH0bmfEQMQYH8vukplOKL1y2rKiQEryI q+O3D4m2jfsnw== X-CNFS-Analysis: v=2.3 cv=XINOtjpE c=1 sm=1 tr=0 a=31YWZ2jLnToYGIwP5Rnx9w==:117 a=31YWZ2jLnToYGIwP5Rnx9w==:17 a=jpOVt7BSZ2e4Z31A5e1TngXxSK0=:19 a=ibg1_B5GdhFutVyTdDoA:9 From: Dario Binacchi To: u-boot@lists.denx.de Cc: Dario Binacchi , AKASHI Takahiro , Alex Marginean , Bin Meng , Jean-Jacques Hiblot , Lukasz Majewski , Patrick Delaunay , Simon Glass Subject: [PATCH 1/3] dm: test: add test case for dev_read_u64 function Date: Sun, 29 Mar 2020 18:04:40 +0200 Message-Id: <20200329160443.11518-2-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200329160443.11518-1-dariobin@libero.it> References: <20200329160443.11518-1-dariobin@libero.it> X-CMAE-Envelope: MS4wfGpxHwWvaq7hCq2Qnex9oqJMMT97+0N0ds/sW7aJUoqBR0l4YxHRMMQ12s8WeFU9oZKmDrXPOOfl0av+MWgordyRUVy4RX2RH5P0GlDAD+XORa/2om8C 40zvCuPhTP5eS49W9zUq+QHAW5IuZjUInSlDa4NWMVkwsgum0+dtvAhZ/hXGrBlYGlEhMfyJ3zlSfDuJlUj7T1fNNn47PNyOPUGDUdyWiFzR7KZgdAZuQ88L AHkqo84GNYdRllYV4zYZnCdCNs2c3kzZ3DncfIa9A6T0TCa6HAQ1Pcw71vjg/49C3s2cbLYP2xB+3GseAtpKuFWV8yp05c/u4IjpcTMkwb1mSXeT5UjT4CsA XopcaSUpSm12u2CswgRbBZ0eUdg1o21ukjmYXDyB+p4/4O+bdEyxe/ZA+n+Cr+z+PyB+9B74 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.2 at phobos.denx.de X-Virus-Status: Clean Add test case to cover dev_read_u64 and dev_read_u64_default functions. Signed-off-by: Dario Binacchi Reviewed-by: Simon Glass Reviewed-by: Simon Glass --- arch/sandbox/dts/test.dts | 1 + include/test/ut.h | 16 ++++++++++++++++ test/dm/test-fdt.c | 10 ++++++++++ 3 files changed, 27 insertions(+) diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts index 4a277934a7..6664adb385 100644 --- a/arch/sandbox/dts/test.dts +++ b/arch/sandbox/dts/test.dts @@ -93,6 +93,7 @@ <&gpio_b 9 0xc 3 2 1>; int-value = <1234>; uint-value = <(-1234)>; + int64-value = /bits/ 64 <0x1111222233334444>; interrupts-extended = <&irq 3 0>; }; diff --git a/include/test/ut.h b/include/test/ut.h index 04df8ba3af..ab861588a8 100644 --- a/include/test/ut.h +++ b/include/test/ut.h @@ -104,6 +104,22 @@ int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); } \ } +/* Assert that two 64 int expressions are equal */ +#define ut_asserteq_64(expr1, expr2) { \ + u64 _val1 = (expr1), _val2 = (expr2); \ + \ + if (_val1 != _val2) { \ + ut_failf(uts, __FILE__, __LINE__, __func__, \ + #expr1 " == " #expr2, \ + "Expected %#llx (%lld), got %#llx (%lld)", \ + (unsigned long long)_val1, \ + (unsigned long long)_val1, \ + (unsigned long long)_val2, \ + (unsigned long long)_val2); \ + return CMD_RET_FAILURE; \ + } \ +} + /* Assert that two string expressions are equal */ #define ut_asserteq_str(expr1, expr2) { \ const char *_val1 = (expr1), *_val2 = (expr2); \ diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c index 75ae08081c..50bff4fdfb 100644 --- a/test/dm/test-fdt.c +++ b/test/dm/test-fdt.c @@ -867,6 +867,7 @@ static int dm_test_read_int(struct unit_test_state *uts) u32 val32; s32 sval; uint val; + u64 val64; ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); ut_asserteq_str("a-test", dev->name); @@ -891,6 +892,15 @@ static int dm_test_read_int(struct unit_test_state *uts) ut_assertok(dev_read_u32u(dev, "uint-value", &val)); ut_asserteq(-1234, val); + ut_assertok(dev_read_u64(dev, "int64-value", &val64)); + ut_asserteq_64(0x1111222233334444, val64); + + ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64)); + ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6)); + + ut_asserteq_64(0x1111222233334444, + dev_read_u64_default(dev, "int64-value", 6)); + return 0; } DM_TEST(dm_test_read_int, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); From patchwork Sun Mar 29 16:04:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 1263473 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=libero.it header.i=@libero.it header.a=rsa-sha256 header.s=s2014 header.b=eEgUxhhv; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48r0np22cNz9sPk for ; Mon, 30 Mar 2020 03:06:22 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3A49A818E8; Sun, 29 Mar 2020 18:06:14 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=libero.it header.i=@libero.it header.b="eEgUxhhv"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B9315818C6; Sun, 29 Mar 2020 18:05:57 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FORGED_SPF_HELO, FREEMAIL_FROM, SPF_HELO_PASS, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from libero.it (smtp-34.italiaonline.it [213.209.10.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1F8CD818D0 for ; Sun, 29 Mar 2020 18:05:46 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=dariobin@libero.it Received: from localhost.localdomain ([79.45.94.224]) by smtp-34.iol.local with ESMTPA id IaR4jI7q4cy2wIaRVjboDx; Sun, 29 Mar 2020 18:05:46 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2014; t=1585497946; bh=GPWGMqdW+lPdJY2afu0gc3Lbu3pU6JKgLcAA2XLs6EM=; h=From; b=eEgUxhhvVhGCojjyCGZNRFVDlLzPelIFm/qk/1vvhwtW6WTCdUPyQ9BdfCDzVTxhh ldy3Q/ASW4+DM+vI6dm0XGWH5YnPdiByXCHjJ/QjQWcJ5iFoRiXgCNppmDeCBVU7LH ZtLOYYRkdjpF0waduVWtEK6VH0M8kCB24gE79T4Knj2b6hYnjqCtn3R5gS36hy4NIe bDe5BrSZcJYb0YskXFWWAyK99WmTTtwvB/xJRENcz9BJ5kPtb3l3KQoz/FhtOy5z+g bKyGtGdaoEjdyuUJlw0ChVBt/Hq8loOnnl50pqgS4UraOCvg5pS6CxyKvmgscDESk7 s582BUVfDsQtA== X-CNFS-Analysis: v=2.3 cv=XINOtjpE c=1 sm=1 tr=0 a=31YWZ2jLnToYGIwP5Rnx9w==:117 a=31YWZ2jLnToYGIwP5Rnx9w==:17 a=jpOVt7BSZ2e4Z31A5e1TngXxSK0=:19 a=fVFbeAJXF_LtzwHawbcA:9 From: Dario Binacchi To: u-boot@lists.denx.de Cc: Dario Binacchi , AKASHI Takahiro , Alex Marginean , Bin Meng , Jean-Jacques Hiblot , Lukasz Majewski , Simon Glass Subject: [PATCH 2/3] dm: core: support reading a single indexed u32 value Date: Sun, 29 Mar 2020 18:04:41 +0200 Message-Id: <20200329160443.11518-3-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200329160443.11518-1-dariobin@libero.it> References: <20200329160443.11518-1-dariobin@libero.it> X-CMAE-Envelope: MS4wfNSyhrpe+QSVm1rVQrbYFT5QfD+4N8rLORW0Acwi6NQj5KyhUeYJw/MD5+WScOYgqSaUBfcMnz0BhCAj13myW1IVMLMqs7NTE0dsEp24SNXojDmG4n0l SiDmuNCP3DOeENG7SNQG84S5CQ4wmiS/6RpVUFrpgTLnvx5nwROahZmpvM4PjhNeJqSehdYNXs5Qjc0xSmnGqjmZfB13zuGRQvad69d/aWz90slyM9zR+JWh kU71ENI3j8tE3Y09G09HMjStsNonlHSZyEvxqntpG6Zbtwknlfgijr05GLM/pQkvSkg27+NBM8h58m917PTCvyWgfqBwuWylfZ8Fsd9cVfmrBhUkEjrMrOQf j43Cj7cHfwxBOhI0eF4ju3eGOqxdJkPhorIOyZQWfkDgMX/hUTA= X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.2 at phobos.denx.de X-Virus-Status: Clean 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 Reviewed-by: Simon Glass --- arch/sandbox/dts/test.dts | 1 + drivers/core/of_access.c | 22 +++++++++++++++++++++ drivers/core/ofnode.c | 40 +++++++++++++++++++++++++++++++++++++++ drivers/core/read.c | 13 +++++++++++++ include/dm/of_access.h | 19 +++++++++++++++++++ include/dm/ofnode.h | 25 ++++++++++++++++++++++++ include/dm/read.h | 40 +++++++++++++++++++++++++++++++++++++++ test/dm/test-fdt.c | 29 ++++++++++++++++++++++++++++ 8 files changed, 189 insertions(+) diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts index 6664adb385..270eb3d87d 100644 --- a/arch/sandbox/dts/test.dts +++ b/arch/sandbox/dts/test.dts @@ -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>; }; diff --git a/drivers/core/of_access.c b/drivers/core/of_access.c index acd745c121..55e4a38fc5 100644 --- a/drivers/core/of_access.c +++ b/drivers/core/of_access.c @@ -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; diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index 96a5dd20bd..5bc3b02996 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -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)); diff --git a/drivers/core/read.c b/drivers/core/read.c index 1f999b1b31..ce78f09d28 100644 --- a/drivers/core/read.c +++ b/drivers/core/read.c @@ -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); diff --git a/include/dm/of_access.h b/include/dm/of_access.h index 13fedb7cf5..92876b3ecb 100644 --- a/include/dm/of_access.h +++ b/include/dm/of_access.h @@ -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 * diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h index b5a50e8849..ce5e366c06 100644 --- a/include/dm/ofnode.h +++ b/include/dm/ofnode.h @@ -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 * diff --git a/include/dm/read.h b/include/dm/read.h index da8c7f25e7..77d3bc8db5 100644 --- a/include/dm/read.h +++ b/include/dm/read.h @@ -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) { diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c index 50bff4fdfb..5fa1238d94 100644 --- a/test/dm/test-fdt.c +++ b/test/dm/test-fdt.c @@ -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) { From patchwork Sun Mar 29 16:04:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 1263472 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=libero.it header.i=@libero.it header.a=rsa-sha256 header.s=s2014 header.b=K2B1Kjjg; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48r0nb4By7z9sRR for ; Mon, 30 Mar 2020 03:06:11 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 71C44818D0; Sun, 29 Mar 2020 18:06:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=libero.it header.i=@libero.it header.b="K2B1Kjjg"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 550AC818D9; Sun, 29 Mar 2020 18:05:57 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FORGED_SPF_HELO, FREEMAIL_FROM, SPF_HELO_PASS, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from libero.it (smtp-34.italiaonline.it [213.209.10.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4FE48818BD for ; Sun, 29 Mar 2020 18:05:51 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=dariobin@libero.it Received: from localhost.localdomain ([79.45.94.224]) by smtp-34.iol.local with ESMTPA id IaR4jI7q4cy2wIaRajboEu; Sun, 29 Mar 2020 18:05:51 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2014; t=1585497951; bh=MbNuAUPJXSY0V2evT7TKuUSou44xFxnifwyq9cGK+Wc=; h=From; b=K2B1KjjgW9eg9OLbt4MksUTPrliB/b53empZMP/+Pyf83pDMZNKW7YMx441TsikMG ZP9m8v+If/DyVym8wd/cYTKBHlr6s3bMSaJE79Z8AiW75xfFta+pAUUL4twW58Dbvs /q/+MbOhwQ375EeLzupAbXQoHclPhiKr4ifSpHxc25FYE+TGh1eaH/pIHnHIa88otq KIZVzSPqMpW3z6dYd2hh81jeMLaNMY+TcS2mXCOzjIarjEPYrOieDuRr3EUyUWJCZ9 hpQO3LYwOLj5McL/zFMZv7dQEpVzVvOGcFFFCmXWaPpkcVJBxy+TuGmNEe3NMUJPoZ 6Xculhwbhv/mA== X-CNFS-Analysis: v=2.3 cv=XINOtjpE c=1 sm=1 tr=0 a=31YWZ2jLnToYGIwP5Rnx9w==:117 a=31YWZ2jLnToYGIwP5Rnx9w==:17 a=jpOVt7BSZ2e4Z31A5e1TngXxSK0=:19 a=VyzCIZW8NGLn2DbIR6AA:9 From: Dario Binacchi To: u-boot@lists.denx.de Cc: Dario Binacchi , Simon Glass Subject: [PATCH 3/3] dm: core: refactor functions reading an u32 from dt Date: Sun, 29 Mar 2020 18:04:42 +0200 Message-Id: <20200329160443.11518-4-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200329160443.11518-1-dariobin@libero.it> References: <20200329160443.11518-1-dariobin@libero.it> X-CMAE-Envelope: MS4wfJb2EmGjmMe3DGR/CDjjfQN7mJhhzwT4ZCqhZrUEyVwOCfIkGSyLQoUtLspCH5V7hgnMMdmWFbCT7yzeCA6uwVLicqdn1dICzXssgtRhL9UMA79QgXmV 7ZS2HaOIXIuPem0K9w5uhWuKhxkriszkyIkQIJLn5HLm6jGsr3ed5VXfcyJ3yratzWIH+RAcVGmdrmF01MrGbvzlHaPLeQ4YyZT9KgNdWVHz3B6wtmZjQDt/ X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.2 at phobos.denx.de X-Virus-Status: Clean Now reading a 32 bit value from a device-tree property can be expressed as reading the first element of an array with a single value. Signed-off-by: Dario Binacchi Reviewed-by: Simon Glass --- drivers/core/of_access.c | 16 +--------------- drivers/core/ofnode.c | 23 ++--------------------- 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/drivers/core/of_access.c b/drivers/core/of_access.c index 55e4a38fc5..d116d106d9 100644 --- a/drivers/core/of_access.c +++ b/drivers/core/of_access.c @@ -449,21 +449,7 @@ static void *of_find_property_value_of_size(const struct device_node *np, int of_read_u32(const struct device_node *np, const char *propname, 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)); - if (IS_ERR(val)) { - debug("(not found)\n"); - return PTR_ERR(val); - } - - *outp = be32_to_cpup(val); - debug("%#x (%d)\n", *outp, *outp); - - return 0; + return of_read_u32_index(np, propname, 0, outp); } int of_read_u32_array(const struct device_node *np, const char *propname, diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index 5bc3b02996..b0be7cbe19 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -18,32 +18,13 @@ int ofnode_read_u32(ofnode node, const char *propname, u32 *outp) { - assert(ofnode_valid(node)); - debug("%s: %s: ", __func__, propname); - - if (ofnode_is_np(node)) { - return of_read_u32(ofnode_to_np(node), propname, outp); - } else { - const fdt32_t *cell; - int len; - - cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), - propname, &len); - if (!cell || len < sizeof(int)) { - debug("(not found)\n"); - return -EINVAL; - } - *outp = fdt32_to_cpu(cell[0]); - } - debug("%#x (%d)\n", *outp, *outp); - - return 0; + return ofnode_read_u32_index(node, propname, 0, outp); } u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def) { assert(ofnode_valid(node)); - ofnode_read_u32(node, propname, &def); + ofnode_read_u32_index(node, propname, 0, &def); return def; }