test: Fix filesystem tests always being skipped
[pandora-u-boot.git] / common / console.c
index e82b5d2..561cdf3 100644 (file)
@@ -19,6 +19,7 @@
 #include <exports.h>
 #include <env_internal.h>
 #include <watchdog.h>
+#include <asm/global_data.h>
 #include <linux/delay.h>
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -172,13 +173,9 @@ static int console_setfile(int file, struct stdio_dev * dev)
        case stdin:
        case stdout:
        case stderr:
-               /* Start new device */
-               if (dev->start) {
-                       error = dev->start(dev);
-                       /* If it's not started dont use it */
-                       if (error < 0)
-                               break;
-               }
+               error = console_start(file, dev);
+               if (error)
+                       break;
 
                /* Assign the new device (leaving the existing one started) */
                stdio_devices[file] = dev;
@@ -236,9 +233,35 @@ static struct stdio_dev *tstcdev;
 struct stdio_dev **console_devices[MAX_FILES];
 int cd_count[MAX_FILES];
 
-static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
+static void console_devices_set(int file, struct stdio_dev *dev)
 {
        console_devices[file][0] = dev;
+       cd_count[file] = 1;
+}
+
+/**
+ * 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;
+
+       for (i = 0; i < ARRAY_SIZE(cd_count); i++) {
+               if (i == file)
+                       continue;
+
+               if (iomux_match_device(console_devices[i], cd_count[i], sdev) >= 0)
+                       return false;
+       }
+       return true;
 }
 
 /*
@@ -270,8 +293,7 @@ static int console_tstc(int file)
        int prev;
 
        prev = disable_ctrlc(1);
-       for (i = 0; i < cd_count[file]; i++) {
-               dev = console_devices[file][i];
+       for_each_console_dev(i, file, dev) {
                if (dev->tstc != NULL) {
                        ret = dev->tstc(dev);
                        if (ret > 0) {
@@ -291,8 +313,7 @@ static void console_putc(int file, const char c)
        int i;
        struct stdio_dev *dev;
 
-       for (i = 0; i < cd_count[file]; i++) {
-               dev = console_devices[file][i];
+       for_each_console_dev(i, file, dev) {
                if (dev->putc != NULL)
                        dev->putc(dev, c);
        }
@@ -311,11 +332,9 @@ static void console_puts_select(int file, bool serial_only, const char *s)
        int i;
        struct stdio_dev *dev;
 
-       for (i = 0; i < cd_count[file]; i++) {
-               bool is_serial;
+       for_each_console_dev(i, file, dev) {
+               bool is_serial = console_dev_is_serial(dev);
 
-               dev = console_devices[file][i];
-               is_serial = console_dev_is_serial(dev);
                if (dev->puts && serial_only == is_serial)
                        dev->puts(dev, s);
        }
@@ -331,8 +350,7 @@ static void console_puts(int file, const char *s)
        int i;
        struct stdio_dev *dev;
 
-       for (i = 0; i < cd_count[file]; i++) {
-               dev = console_devices[file][i];
+       for_each_console_dev(i, file, dev) {
                if (dev->puts != NULL)
                        dev->puts(dev, s);
        }
@@ -346,8 +364,13 @@ static inline void console_doenv(int file, struct stdio_dev *dev)
 #endif
 #else
 
-static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
+static void console_devices_set(int file, struct stdio_dev *dev)
+{
+}
+
+static inline bool console_needs_start_stop(int file, struct stdio_dev *sdev)
 {
+       return true;
 }
 
 static inline int console_getc(int file)
@@ -389,6 +412,38 @@ static inline void console_doenv(int file, struct stdio_dev *dev)
 #endif
 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
 
+static void __maybe_unused console_setfile_and_devices(int file, struct stdio_dev *dev)
+{
+       console_setfile(file, dev);
+       console_devices_set(file, dev);
+}
+
+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);
+               /* If it's not started don't use it */
+               if (error < 0)
+                       return error;
+       }
+       return 0;
+}
+
+void console_stop(int file, struct stdio_dev *sdev)
+{
+       if (!console_needs_start_stop(file, sdev))
+               return;
+
+       if (sdev->stop)
+               sdev->stop(sdev);
+}
+
 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
 
 int serial_printf(const char *fmt, ...)
@@ -779,7 +834,7 @@ void clear_ctrlc(void)
 
 /** U-Boot INIT FUNCTIONS *************************************************/
 
-struct stdio_dev *search_device(int flags, const char *name)
+struct stdio_dev *console_search_dev(int flags, const char *name)
 {
        struct stdio_dev *dev;
 
@@ -801,21 +856,13 @@ int console_assign(int file, const char *devname)
        struct stdio_dev *dev;
 
        /* Check for valid file */
-       switch (file) {
-       case stdin:
-               flag = DEV_FLAGS_INPUT;
-               break;
-       case stdout:
-       case stderr:
-               flag = DEV_FLAGS_OUTPUT;
-               break;
-       default:
-               return -1;
-       }
+       flag = stdio_file_to_flags(file);
+       if (flag < 0)
+               return flag;
 
        /* Check for valid device name */
 
-       dev = search_device(flag, devname);
+       dev = console_search_dev(flag, devname);
 
        if (dev)
                return console_setfile(file, dev);
@@ -921,9 +968,9 @@ int console_init_r(void)
        stderrname = env_get("stderr");
 
        if (OVERWRITE_CONSOLE == 0) {   /* if not overwritten by config switch */
-               inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
-               outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
-               errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
+               inputdev  = console_search_dev(DEV_FLAGS_INPUT,  stdinname);
+               outputdev = console_search_dev(DEV_FLAGS_OUTPUT, stdoutname);
+               errdev    = console_search_dev(DEV_FLAGS_OUTPUT, stderrname);
                if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
                        iomux_err = iomux_doenv(stdin, stdinname);
                        iomux_err += iomux_doenv(stdout, stdoutname);
@@ -935,13 +982,13 @@ int console_init_r(void)
        }
        /* if the devices are overwritten or not found, use default device */
        if (inputdev == NULL) {
-               inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
+               inputdev  = console_search_dev(DEV_FLAGS_INPUT,  "serial");
        }
        if (outputdev == NULL) {
-               outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
+               outputdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial");
        }
        if (errdev == NULL) {
-               errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
+               errdev    = console_search_dev(DEV_FLAGS_OUTPUT, "serial");
        }
        /* Initializes output console first */
        if (outputdev != NULL) {
@@ -976,11 +1023,6 @@ done:
 
        gd->flags |= GD_FLG_DEVINIT;    /* device initialization completed */
 
-#if 0
-       /* If nothing usable installed, use only the initial console */
-       if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
-               return 0;
-#endif
        print_pre_console_buffer(flushpoint);
        return 0;
 }
@@ -1011,7 +1053,7 @@ int console_init_r(void)
         */
        if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) {
                if (!(gd->flags & GD_FLG_SILENT))
-                       outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
+                       outputdev = console_search_dev (DEV_FLAGS_OUTPUT, "serial");
        }
 
        /* Scan devices looking for input and output devices */
@@ -1030,17 +1072,13 @@ int console_init_r(void)
 
        /* Initializes output console first */
        if (outputdev != NULL) {
-               console_setfile(stdout, outputdev);
-               console_setfile(stderr, outputdev);
-               console_devices_set(stdout, outputdev);
-               console_devices_set(stderr, outputdev);
+               console_setfile_and_devices(stdout, outputdev);
+               console_setfile_and_devices(stderr, outputdev);
        }
 
        /* Initializes input console */
-       if (inputdev != NULL) {
-               console_setfile(stdin, inputdev);
-               console_devices_set(stdin, inputdev);
-       }
+       if (inputdev != NULL)
+               console_setfile_and_devices(stdin, inputdev);
 
        if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
                stdio_print_current_devices();
@@ -1052,11 +1090,6 @@ int console_init_r(void)
 
        gd->flags |= GD_FLG_DEVINIT;    /* device initialization completed */
 
-#if 0
-       /* If nothing usable installed, use only the initial console */
-       if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
-               return 0;
-#endif
        print_pre_console_buffer(flushpoint);
        return 0;
 }