public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Sean Anderson <seanga2@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH v11 10/21] reset: Add generic reset driver
Date: Wed, 20 May 2020 02:41:00 -0400	[thread overview]
Message-ID: <20200520064111.317605-11-seanga2@gmail.com> (raw)
In-Reply-To: <20200520064111.317605-1-seanga2@gmail.com>

This patch adds a generic reset driver. It is designed to be useful when
one has a register in a regmap which contains bits that reset other
devices. I thought this seemed like a very generic use, so here is a
generic driver. The overall structure has been modeled on the syscon-reboot
driver.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
---

Changes in v5:
- Reorder includes
- Include linux/err.h explicitly

Changes in v4:
- Added basic test
- Fix incorrect usage of regmap_update_bits

Changes in v3:
- New

 arch/sandbox/dts/test.dts                     | 15 ++++
 configs/sandbox_defconfig                     |  2 +
 .../reset/syscon-reset.txt                    | 36 +++++++++
 drivers/reset/Kconfig                         |  5 ++
 drivers/reset/Makefile                        |  1 +
 drivers/reset/reset-syscon.c                  | 80 +++++++++++++++++++
 test/dm/Makefile                              |  1 +
 test/dm/syscon-reset.c                        | 58 ++++++++++++++
 8 files changed, 198 insertions(+)
 create mode 100644 doc/device-tree-bindings/reset/syscon-reset.txt
 create mode 100644 drivers/reset/reset-syscon.c
 create mode 100644 test/dm/syscon-reset.c

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index d08396aff6..f5b685f7fe 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -1035,6 +1035,21 @@
 		clocks = <&clk_sandbox 4>;
 		power-domains = <&pwrdom 1>;
 	};
+
+	resetc2: syscon-reset {
+		compatible = "syscon-reset";
+		#reset-cells = <1>;
+		regmap = <&syscon0>;
+		offset = <1>;
+		mask = <0x27FFFFFF>;
+		assert-high = <0>;
+	};
+
+	syscon-reset-test {
+		compatible = "sandbox,misc_sandbox";
+		resets = <&resetc2 15>, <&resetc2 30>, <&resetc2 60>;
+		reset-names = "valid", "no_mask", "out_of_range";
+	};
 };
 
 #include "sandbox_pmic.dtsi"
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index 722459a2a5..30401834ad 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -196,6 +196,8 @@ CONFIG_REMOTEPROC_SANDBOX=y
 CONFIG_DM_RESET=y
 CONFIG_SANDBOX_RESET=y
 CONFIG_DM_RNG=y
+CONFIG_RNG_SANDBOX=y
+CONFIG_RESET_SYSCON=y
 CONFIG_DM_RTC=y
 CONFIG_RTC_RV8803=y
 CONFIG_DEBUG_UART_SANDBOX=y
diff --git a/doc/device-tree-bindings/reset/syscon-reset.txt b/doc/device-tree-bindings/reset/syscon-reset.txt
new file mode 100644
index 0000000000..f136b3d225
--- /dev/null
+++ b/doc/device-tree-bindings/reset/syscon-reset.txt
@@ -0,0 +1,36 @@
+Generic SYSCON mapped register reset driver
+
+This is a generic reset driver using syscon to map the reset register.
+The reset is generally performed with a write to the reset register
+defined by the register map pointed by syscon reference plus the offset and
+shifted by the reset specifier/
+
+To assert a reset on some device, the equivalent of the following operation is
+performed, where reset_id is the reset specifier from the device's resets
+property.
+
+	if (BIT(reset_id) & mask)
+		regmap[offset][reset_id] = assert-high;
+
+Required properties:
+- compatible: should contain "syscon-reset"
+- #reset-cells: must be 1
+- regmap: this is phandle to the register map node
+- offset: offset in the register map for the reboot register (in bytes)
+
+Optional properties:
+- mask: accept only the reset specifiers defined by the mask (32 bit)
+- assert-high: Bit to write when asserting a reset. Defaults to 1.
+
+Default will be little endian mode, 32 bit access only.
+
+Example:
+
+	reset-controller {
+		compatible = "syscon-reset";
+		#reset-cells = <1>;
+		regmap = <&sysctl>;
+		offset = <0x20>;
+		mask = <0x27FFFFFF>;
+		assert-high = <0>;
+	};
diff --git a/drivers/reset/Kconfig b/drivers/reset/Kconfig
index 88d3be1593..58ba0c686e 100644
--- a/drivers/reset/Kconfig
+++ b/drivers/reset/Kconfig
@@ -148,4 +148,9 @@ config RESET_IMX7
 	help
 	  Support for reset controller on i.MX7/8 SoCs.
 
+config RESET_SYSCON
+	bool "Enable generic syscon reset driver support"
+	depends on DM_RESET
+	help
+	  Support generic syscon mapped register reset devices.
 endmenu
diff --git a/drivers/reset/Makefile b/drivers/reset/Makefile
index 0a044d5d8c..433f1eca54 100644
--- a/drivers/reset/Makefile
+++ b/drivers/reset/Makefile
@@ -23,3 +23,4 @@ obj-$(CONFIG_RESET_MTMIPS) += reset-mtmips.o
 obj-$(CONFIG_RESET_SUNXI) += reset-sunxi.o
 obj-$(CONFIG_RESET_HISILICON) += reset-hisilicon.o
 obj-$(CONFIG_RESET_IMX7) += reset-imx7.o
+obj-$(CONFIG_RESET_SYSCON) += reset-syscon.o
diff --git a/drivers/reset/reset-syscon.c b/drivers/reset/reset-syscon.c
new file mode 100644
index 0000000000..34dfe0bab6
--- /dev/null
+++ b/drivers/reset/reset-syscon.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 Sean Anderson
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <regmap.h>
+#include <reset.h>
+#include <reset-uclass.h>
+#include <syscon.h>
+#include <linux/err.h>
+
+struct syscon_reset_priv {
+	struct regmap *regmap;
+	uint offset;
+	uint mask;
+	bool assert_high;
+};
+
+static int syscon_reset_request(struct reset_ctl *rst)
+{
+	struct syscon_reset_priv *priv = dev_get_priv(rst->dev);
+
+	if (BIT(rst->id) & priv->mask)
+		return 0;
+	else
+		return -EINVAL;
+}
+
+static int syscon_reset_assert(struct reset_ctl *rst)
+{
+	struct syscon_reset_priv *priv = dev_get_priv(rst->dev);
+
+	return regmap_update_bits(priv->regmap, priv->offset, BIT(rst->id),
+				  priv->assert_high ? BIT(rst->id) : 0);
+}
+
+static int syscon_reset_deassert(struct reset_ctl *rst)
+{
+	struct syscon_reset_priv *priv = dev_get_priv(rst->dev);
+
+	return regmap_update_bits(priv->regmap, priv->offset, BIT(rst->id),
+				  priv->assert_high ? 0 : BIT(rst->id));
+}
+
+static const struct reset_ops syscon_reset_ops = {
+	.request = syscon_reset_request,
+	.rst_assert = syscon_reset_assert,
+	.rst_deassert = syscon_reset_deassert,
+};
+
+int syscon_reset_probe(struct udevice *dev)
+{
+	struct syscon_reset_priv *priv = dev_get_priv(dev);
+
+	priv->regmap = syscon_regmap_lookup_by_phandle(dev, "regmap");
+	if (IS_ERR(priv->regmap))
+		return -ENODEV;
+
+	priv->offset = dev_read_u32_default(dev, "offset", 0);
+	priv->mask = dev_read_u32_default(dev, "mask", 0);
+	priv->assert_high = dev_read_u32_default(dev, "assert-high", true);
+
+	return 0;
+}
+
+static const struct udevice_id syscon_reset_ids[] = {
+	{ .compatible = "syscon-reset" },
+	{ },
+};
+
+U_BOOT_DRIVER(syscon_reset) = {
+	.name = "syscon_reset",
+	.id = UCLASS_RESET,
+	.of_match = syscon_reset_ids,
+	.probe = syscon_reset_probe,
+	.priv_auto_alloc_size = sizeof(struct syscon_reset_priv),
+	.ops = &syscon_reset_ops,
+};
diff --git a/test/dm/Makefile b/test/dm/Makefile
index 518ee8e377..0d1c66fa1e 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -75,4 +75,5 @@ obj-$(CONFIG_DM_MDIO_MUX) += mdio_mux.o
 obj-$(CONFIG_DM_RNG) += rng.o
 obj-$(CONFIG_CLK_K210_SET_RATE) += k210_pll.o
 obj-$(CONFIG_SIMPLE_PM_BUS) += simple-pm-bus.o
+obj-$(CONFIG_RESET_SYSCON) += syscon-reset.o
 endif
diff --git a/test/dm/syscon-reset.c b/test/dm/syscon-reset.c
new file mode 100644
index 0000000000..1db0206e00
--- /dev/null
+++ b/test/dm/syscon-reset.c
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/test.h>
+#include <regmap.h>
+#include <reset.h>
+#include <syscon.h>
+#include <test/ut.h>
+#include <asm/test.h>
+
+/* The following values must match the device tree */
+#define TEST_RESET_REG 1
+#define TEST_RESET_ASSERT_HIGH 0
+#define TEST_RESET_ASSERT (TEST_RESET_ASSERT_HIGH ? (u32)-1 : (u32)0)
+#define TEST_RESET_DEASSERT (~TEST_RESET_ASSERT)
+
+#define TEST_RESET_VALID 15
+#define TEST_RESET_NOMASK 30
+#define TEST_RESET_OUTOFRANGE 60
+
+static int dm_test_syscon_reset(struct unit_test_state *uts)
+{
+	struct regmap *map;
+	struct reset_ctl rst;
+	struct udevice *reset;
+	struct udevice *syscon;
+	struct udevice *syscon_reset;
+	uint reg;
+
+	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "syscon-reset-test",
+					      &reset));
+	ut_assertok(uclass_get_device_by_name(UCLASS_SYSCON, "syscon at 0",
+					      &syscon));
+	ut_assertok(uclass_get_device_by_name(UCLASS_RESET, "syscon-reset",
+					      &syscon_reset));
+	ut_assertok_ptr((map = syscon_get_regmap(syscon)));
+
+	ut_asserteq(-EINVAL, reset_get_by_name(reset, "no_mask", &rst));
+	ut_asserteq(-EINVAL, reset_get_by_name(reset, "out_of_range", &rst));
+	ut_assertok(reset_get_by_name(reset, "valid", &rst));
+
+	sandbox_set_enable_memio(true);
+	ut_assertok(regmap_write(map, TEST_RESET_REG, TEST_RESET_DEASSERT));
+	ut_assertok(reset_assert(&rst));
+	ut_assertok(regmap_read(map, TEST_RESET_REG, &reg));
+	ut_asserteq(TEST_RESET_DEASSERT ^ BIT(TEST_RESET_VALID), reg);
+
+	ut_assertok(reset_deassert(&rst));
+	ut_assertok(regmap_read(map, TEST_RESET_REG, &reg));
+	ut_asserteq(TEST_RESET_DEASSERT, reg);
+
+	return 0;
+}
+DM_TEST(dm_test_syscon_reset, DM_TESTF_SCAN_FDT);
-- 
2.26.2

  parent reply	other threads:[~2020-05-20  6:41 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-20  6:40 [PATCH v11 00/21] riscv: Add Sipeed Maix support Sean Anderson
2020-05-20  6:40 ` [PATCH v11 01/21] clk: Always use the supplied struct clk Sean Anderson
2020-05-20  6:40 ` [PATCH v11 02/21] clk: Check that ops of composite clock components exist before calling Sean Anderson
2020-05-20  6:40 ` [PATCH v11 03/21] clk: Unconditionally recursively en-/dis-able clocks Sean Anderson
2020-05-20  6:40 ` [PATCH v11 04/21] clk: Fix clk_get_by_* handling of index Sean Anderson
2020-05-20  6:40 ` [PATCH v11 05/21] clk: Add K210 pll support Sean Anderson
2020-05-20  6:40 ` [PATCH v11 06/21] clk: Add a bypass clock for K210 Sean Anderson
2020-05-20  6:40 ` [PATCH v11 07/21] clk: Add K210 clock support Sean Anderson
2020-05-20  6:40 ` [PATCH v11 08/21] dm: Add support for simple-pm-bus Sean Anderson
2020-05-20  6:40 ` [PATCH v11 09/21] dm: Fix error handling for dev_read_addr_ptr Sean Anderson
2020-05-20  6:41 ` Sean Anderson [this message]
2020-05-20  6:41 ` [PATCH v11 11/21] lib: Always set errno in hcreate_r Sean Anderson
2020-05-20  6:41 ` [PATCH v11 12/21] riscv: Add headers for asm/global_data.h Sean Anderson
2020-05-20  6:41 ` [PATCH v11 13/21] riscv: Clear pending interrupts before enabling IPIs Sean Anderson
2020-05-20  6:41 ` [PATCH v11 14/21] riscv: Clean up IPI initialization code Sean Anderson
2020-05-20  7:52   ` Rick Chen
2020-05-20  6:41 ` [PATCH v11 15/21] riscv: Add option to support RISC-V privileged spec 1.9 Sean Anderson
2020-05-20  6:41 ` [PATCH v11 16/21] riscv: Allow use of reset drivers Sean Anderson
2020-05-20  6:41 ` [PATCH v11 17/21] riscv: Try to get cpu frequency from a "clocks" node if it exists Sean Anderson
2020-05-20  6:41 ` [PATCH v11 18/21] riscv: Enable cpu clock if it is present Sean Anderson
2020-05-20  6:41 ` [PATCH v11 19/21] riscv: Add device tree for K210 and Sipeed Maix BitM Sean Anderson
2020-05-20  6:41 ` [PATCH v11 20/21] doc: riscv: Add documentation for Sipeed Maix Bit Sean Anderson
2020-05-20  6:41 ` [PATCH v11 21/21] riscv: Add Sipeed Maix support Sean Anderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200520064111.317605-11-seanga2@gmail.com \
    --to=seanga2@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox