Move bootretry code into bootretry.c and clean up

This code is only used by one board, so it seems a shame to clutter up
the readline code with it. Move it into its own file.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/board/hymod/hymod.c b/board/hymod/hymod.c
index 55ffd67..f6990e9 100644
--- a/board/hymod/hymod.c
+++ b/board/hymod/hymod.c
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <mpc8260.h>
 #include <mpc8260_irq.h>
diff --git a/board/hymod/input.c b/board/hymod/input.c
index 59ad6aa..2f857c0 100644
--- a/board/hymod/input.c
+++ b/board/hymod/input.c
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 
 int
diff --git a/common/Makefile b/common/Makefile
index 81721f9..391a8d6 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -29,6 +29,11 @@
 obj-y += autoboot.o
 endif
 
+# This option is not just y/n - it can have a numeric value
+ifdef CONFIG_BOOT_RETRY_TIME
+obj-y += bootretry.o
+endif
+
 # boards
 obj-$(CONFIG_SYS_GENERIC_BOARD) += board_f.o
 obj-$(CONFIG_SYS_GENERIC_BOARD) += board_r.o
diff --git a/common/autoboot.c b/common/autoboot.c
index 6933e3f..5f8d9c3 100644
--- a/common/autoboot.c
+++ b/common/autoboot.c
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <fdtdec.h>
 #include <menu.h>
diff --git a/common/bootretry.c b/common/bootretry.c
new file mode 100644
index 0000000..12653c0
--- /dev/null
+++ b/common/bootretry.c
@@ -0,0 +1,59 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <bootretry.h>
+#include <cli.h>
+#include <errno.h>
+#include <watchdog.h>
+
+#ifndef CONFIG_BOOT_RETRY_MIN
+#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
+#endif
+
+static uint64_t endtime;  /* must be set, default is instant timeout */
+static int      retry_time = -1; /* -1 so can call readline before main_loop */
+
+/***************************************************************************
+ * initialize command line timeout
+ */
+void init_cmd_timeout(void)
+{
+	char *s = getenv("bootretry");
+
+	if (s != NULL)
+		retry_time = (int)simple_strtol(s, NULL, 10);
+	else
+		retry_time = CONFIG_BOOT_RETRY_TIME;
+
+	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
+		retry_time = CONFIG_BOOT_RETRY_MIN;
+}
+
+/***************************************************************************
+ * reset command line timeout to retry_time seconds
+ */
+void reset_cmd_timeout(void)
+{
+	endtime = endtick(retry_time);
+}
+
+int bootretry_tstc_timeout(void)
+{
+	while (!tstc()) {	/* while no incoming data */
+		if (retry_time >= 0 && get_ticks() > endtime)
+			return -ETIMEDOUT;
+		WATCHDOG_RESET();
+	}
+
+	return 0;
+}
+
+void bootretry_dont_retry(void)
+{
+	retry_time = -1;
+}
diff --git a/common/cli_hush.c b/common/cli_hush.c
index a612bfb..d6544ae 100644
--- a/common/cli_hush.c
+++ b/common/cli_hush.c
@@ -79,6 +79,7 @@
 #include <malloc.h>         /* malloc, free, realloc*/
 #include <linux/ctype.h>    /* isalpha, isdigit */
 #include <common.h>        /* readline */
+#include <bootretry.h>
 #include <cli.h>
 #include <cli_hush.h>
 #include <command.h>        /* find_cmd */
diff --git a/common/cli_readline.c b/common/cli_readline.c
index df446b8..9a9fb35 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <watchdog.h>
 
@@ -18,17 +19,8 @@
 static const char erase_seq[] = "\b \b";	/* erase sequence */
 static const char   tab_seq[] = "        ";	/* used to expand TABs */
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-static uint64_t endtime;      /* must be set, default is instant timeout */
-static int      retry_time = -1; /* -1 so can call readline before main_loop */
-#endif
-
 char console_buffer[CONFIG_SYS_CBSIZE + 1];	/* console I/O buffer	*/
 
-#ifndef CONFIG_BOOT_RETRY_MIN
-#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
-#endif
-
 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
 {
 	char *s;
@@ -267,13 +259,8 @@
 		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
 
 	while (1) {
-#ifdef CONFIG_BOOT_RETRY_TIME
-		while (!tstc()) {	/* while no incoming data */
-			if (retry_time >= 0 && get_ticks() > endtime)
-				return -2;	/* timed out */
-			WATCHDOG_RESET();
-		}
-#endif
+		if (bootretry_tstc_timeout())
+			return -2;	/* timed out */
 		if (first && timeout) {
 			uint64_t etime = endtick(timeout);
 
@@ -539,13 +526,8 @@
 	col = plen;
 
 	for (;;) {
-#ifdef CONFIG_BOOT_RETRY_TIME
-		while (!tstc()) {	/* while no incoming data */
-			if (retry_time >= 0 && get_ticks() > endtime)
-				return -2;	/* timed out */
-			WATCHDOG_RESET();
-		}
-#endif
+		if (bootretry_tstc_timeout())
+			return -2;	/* timed out */
 		WATCHDOG_RESET();	/* Trigger watchdog, if needed */
 
 #ifdef CONFIG_SHOW_ACTIVITY
@@ -637,35 +619,3 @@
 	}
 #endif
 }
-
-#ifdef CONFIG_BOOT_RETRY_TIME
-/***************************************************************************
- * initialize command line timeout
- */
-void init_cmd_timeout(void)
-{
-	char *s = getenv("bootretry");
-
-	if (s != NULL)
-		retry_time = (int)simple_strtol(s, NULL, 10);
-	else
-		retry_time =  CONFIG_BOOT_RETRY_TIME;
-
-	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
-		retry_time = CONFIG_BOOT_RETRY_MIN;
-}
-
-/***************************************************************************
- * reset command line timeout to retry_time seconds
- */
-void reset_cmd_timeout(void)
-{
-	endtime = endtick(retry_time);
-}
-
-void bootretry_dont_retry(void)
-{
-	retry_time = -1;
-}
-
-#endif
diff --git a/common/cli_simple.c b/common/cli_simple.c
index 3039cfc..5b7e2ce 100644
--- a/common/cli_simple.c
+++ b/common/cli_simple.c
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <linux/ctype.h>
 
diff --git a/common/cmd_i2c.c b/common/cmd_i2c.c
index 7158b5a..4fa1213 100644
--- a/common/cmd_i2c.c
+++ b/common/cmd_i2c.c
@@ -66,6 +66,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #include <edid.h>
diff --git a/common/cmd_mem.c b/common/cmd_mem.c
index 15a4b84..9bd7c0e 100644
--- a/common/cmd_mem.c
+++ b/common/cmd_mem.c
@@ -12,6 +12,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #ifdef CONFIG_HAS_DATAFLASH
diff --git a/common/cmd_pci.c b/common/cmd_pci.c
index 1ed55ce..180b35a 100644
--- a/common/cmd_pci.c
+++ b/common/cmd_pci.c
@@ -14,6 +14,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #include <asm/processor.h>
diff --git a/include/bootretry.h b/include/bootretry.h
new file mode 100644
index 0000000..025c29d
--- /dev/null
+++ b/include/bootretry.h
@@ -0,0 +1,31 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef __bootretry_h
+#define __bootretry_h
+
+#ifdef CONFIG_BOOT_RETRY_TIME
+/**
+ * bootretry_tstc_timeout() - ensure we get a keypress before timeout
+ *
+ * Check for a keypress repeatedly, resetting the watchdog each time. If a
+ * keypress is not received within the command timeout, return an error.
+ *
+ * @return 0 if a key is received in time, -ETIMEDOUT if not
+ */
+int bootretry_tstc_timeout(void);
+#else
+static inline int bootretry_tstc_timeout(void)
+{
+	return 0;
+}
+#endif
+
+void init_cmd_timeout(void);
+void reset_cmd_timeout(void);
+
+#endif
diff --git a/include/common.h b/include/common.h
index 75cb525..91745cf 100644
--- a/include/common.h
+++ b/include/common.h
@@ -286,8 +286,6 @@
  * @return 0 on success, or != 0 on error.
  */
 int run_command_list(const char *cmd, int len, int flag);
-void	init_cmd_timeout(void);
-void	reset_cmd_timeout(void);
 extern char console_buffer[];
 
 /* arch/$(ARCH)/lib/board.c */