There is no need to call ->start() for already started device. All the same,
there is no need to call ->stop() for devices still in use.

For now enforce this only for IOMUX case.

Cc: Simon Glass <s...@chromium.org>
Signed-off-by: Andy Shevchenko <andriy.shevche...@linux.intel.com>
---
v3: renamed new function and added a documentation for it
 common/console.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/common/console.c b/common/console.c
index 9973f96e7629..e5fd214ae86f 100644
--- a/common/console.c
+++ b/common/console.c
@@ -174,6 +174,32 @@ static struct stdio_dev *tstcdev;
 struct stdio_dev **console_devices[MAX_FILES];
 int cd_count[MAX_FILES];
 
+/**
+ * console_needs_start_stop() - check if we need to start or stop the STDIO 
device
+ * @file: STDIO file
+ * @sdev: STDIO device in question
+ *
+ * This function checks if we need to start or stop the stdio device used for
+ * a console. For IOMUX case it simply enforces one time start and one time
+ * stop of the device independently of how many STDIO files are using it. In
+ * other words, we start console once before first STDIO device wants it and
+ * stop after the last is gone.
+ */
+static bool console_needs_start_stop(int file, struct stdio_dev *sdev)
+{
+       int i, j;
+
+       for (i = 0; i < ARRAY_SIZE(cd_count); i++) {
+               if (i == file)
+                       continue;
+
+               for (j = 0; j < cd_count[i]; j++)
+                       if (console_devices[i][j] == sdev)
+                               return false;
+       }
+       return true;
+}
+
 /*
  * This depends on tstc() always being called before getchar().
  * This is guaranteed to be true because this routine is called
@@ -272,6 +298,11 @@ static inline void console_doenv(int file, struct 
stdio_dev *dev)
 }
 #endif
 #else
+static inline bool console_needs_start_stop(int file, struct stdio_dev *sdev)
+{
+       return true;
+}
+
 static inline int console_getc(int file)
 {
        return stdio_devices[file]->getc(stdio_devices[file]);
@@ -310,6 +341,9 @@ int console_start(int file, struct stdio_dev *sdev)
 {
        int error;
 
+       if (!console_needs_start_stop(file, sdev))
+               return 0;
+
        /* Start new device */
        if (sdev->start) {
                error = sdev->start(sdev);
@@ -322,6 +356,9 @@ int console_start(int file, struct stdio_dev *sdev)
 
 void console_stop(int file, struct stdio_dev *sdev)
 {
+       if (!console_needs_start_stop(file, sdev))
+               return;
+
        if (sdev->stop)
                sdev->stop(sdev);
 }
-- 
2.29.2

Reply via email to