diff mbox

[U-Boot,1/5] lib: Add wait_for_bit

Message ID 1450138168-25102-2-git-send-email-mateusz.kulikowski@gmail.com
State Changes Requested
Delegated to: Marek Vasut
Headers show

Commit Message

Mateusz Kulikowski Dec. 15, 2015, 12:09 a.m. UTC
Add function to poll register waiting for specific bit(s).
Similar functions are implemented in few drivers - they are almost
identical and can be generalized.
Signed-off-by: Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
---

 include/wait_bit.h | 34 ++++++++++++++++++++++++++++++++++
 lib/Kconfig        |  4 ++++
 lib/Makefile       |  1 +
 lib/wait_bit.c     | 44 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 include/wait_bit.h
 create mode 100644 lib/wait_bit.c

Comments

Marek Vasut Dec. 15, 2015, 12:20 a.m. UTC | #1
On Tuesday, December 15, 2015 at 01:09:23 AM, Mateusz Kulikowski wrote:
> Add function to poll register waiting for specific bit(s).
> Similar functions are implemented in few drivers - they are almost
> identical and can be generalized.
> Signed-off-by: Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
> ---

Excellent !

>  include/wait_bit.h | 34 ++++++++++++++++++++++++++++++++++
>  lib/Kconfig        |  4 ++++
>  lib/Makefile       |  1 +
>  lib/wait_bit.c     | 44 ++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 83 insertions(+)
>  create mode 100644 include/wait_bit.h
>  create mode 100644 lib/wait_bit.c

[...]

> +int wait_for_bit(const char *prefix,

const u32 *

> u32 *reg, const u32 mask,

const bool, const unsigned int , const bool ;-)

> bool set,
> +		 unsigned int timeout, bool breakable)

> +{
> +	u32 val;
> +	unsigned long start = get_timer(0);
> +
> +	while (1) {
> +		val = readl(reg);
> +
> +		if (!set)
> +			val = ~val;
> +
> +		if ((val & mask) == mask)
> +			return 0;
> +
> +		if (get_timer(start) > timeout)
> +			break;
> +
> +		if (breakable && ctrlc()) {
> +			puts("Abort\n");
> +			return -EINTR;
> +		}
> +
> +		udelay(1);
> +	}
> +
> +	debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n", prefix, reg, mask,
> +	      set);
> +
> +	return -ETIMEDOUT;
> +}

Best regards,
Marek Vasut
diff mbox

Patch

diff --git a/include/wait_bit.h b/include/wait_bit.h
new file mode 100644
index 0000000..7dbfa1a
--- /dev/null
+++ b/include/wait_bit.h
@@ -0,0 +1,34 @@ 
+/*
+ * Wait for bit with timeout and ctrlc
+ *
+ * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef __WAIT_BIT_H
+#define __WAIT_BIT_H
+
+/**
+ * wait_for_bit()	waits for bit set/cleared in register
+ *
+ * Function polls register waiting for specific bit(s) change
+ * (either 0->1 or 1->0). It can fail under two conditions:
+ * - Timeout
+ * - User interaction (CTRL-C)
+ * Function succeeds only if all bits of masked register are set/cleared
+ * (depending on set option).
+ *
+ * @param prefix	Prefix added to timeout messagge (message visible only
+ *			with debug enabled)
+ * @param reg		Register that will be read (using readl())
+ * @param mask		Bit(s) of register that must be active
+ * @param set		Selects wait condition (bit set or clear)
+ * @param timeout	Timeout (in miliseconds)
+ * @param breakable	Enables CTRL-C interruption
+ * @return		0 on success, -ETIMEDOUT or -EINTR on failure
+ */
+int wait_for_bit(const char *prefix, u32 *reg, const u32 mask, bool set,
+		 unsigned int timeout, bool breakable);
+
+#endif
diff --git a/lib/Kconfig b/lib/Kconfig
index 9d580e4..5977fc6 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -137,6 +137,10 @@  config ERRNO_STR
 	  - if errno is null or positive number - a pointer to "Success" message
 	  - if errno is negative - a pointer to errno related message
 
+config LIB_WAIT_BIT
+	bool
+	default n
+
 source lib/efi/Kconfig
 
 endmenu
diff --git a/lib/Makefile b/lib/Makefile
index 1f1ff6f..437f937 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -81,6 +81,7 @@  obj-y += time.o
 obj-$(CONFIG_TRACE) += trace.o
 obj-$(CONFIG_LIB_UUID) += uuid.o
 obj-$(CONFIG_LIB_RAND) += rand.o
+obj-$(CONFIG_LIB_WAIT_BIT) += wait_bit.o
 
 ifdef CONFIG_SPL_BUILD
 # SPL U-Boot may use full-printf, tiny-printf or none at all
diff --git a/lib/wait_bit.c b/lib/wait_bit.c
new file mode 100644
index 0000000..3c78eac
--- /dev/null
+++ b/lib/wait_bit.c
@@ -0,0 +1,44 @@ 
+/*
+ * Wait for bit interruptible by timeout or ctrlc
+ *
+ * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <console.h>
+#include <asm/io.h>
+#include <asm/errno.h>
+
+int wait_for_bit(const char *prefix, u32 *reg, const u32 mask, bool set,
+		 unsigned int timeout, bool breakable)
+{
+	u32 val;
+	unsigned long start = get_timer(0);
+
+	while (1) {
+		val = readl(reg);
+
+		if (!set)
+			val = ~val;
+
+		if ((val & mask) == mask)
+			return 0;
+
+		if (get_timer(start) > timeout)
+			break;
+
+		if (breakable && ctrlc()) {
+			puts("Abort\n");
+			return -EINTR;
+		}
+
+		udelay(1);
+	}
+
+	debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n", prefix, reg, mask,
+	      set);
+
+	return -ETIMEDOUT;
+}