5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <stdio_dev.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
34 * if overwrite_console returns 1, the stdin, stderr and stdout
35 * are switched to the serial port, else the settings in the
36 * environment are used
38 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
39 extern int overwrite_console(void);
40 #define OVERWRITE_CONSOLE overwrite_console()
42 #define OVERWRITE_CONSOLE 0
43 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
45 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
47 static int console_setfile(int file, struct stdio_dev * dev)
58 /* Start new device */
61 /* If it's not started dont use it */
66 /* Assign the new device (leaving the existing one started) */
67 stdio_devices[file] = dev;
70 * Update monitor functions
71 * (to use the console stuff by other applications)
75 gd->jt[XF_getc] = dev->getc;
76 gd->jt[XF_tstc] = dev->tstc;
79 gd->jt[XF_putc] = dev->putc;
80 gd->jt[XF_puts] = dev->puts;
81 gd->jt[XF_printf] = printf;
86 default: /* Invalid file ID */
92 #if defined(CONFIG_CONSOLE_MUX)
93 /** Console I/O multiplexing *******************************************/
95 static struct stdio_dev *tstcdev;
96 struct stdio_dev **console_devices[MAX_FILES];
97 int cd_count[MAX_FILES];
100 * This depends on tstc() always being called before getc().
101 * This is guaranteed to be true because this routine is called
102 * only from fgetc() which assures it.
103 * No attempt is made to demultiplex multiple input sources.
105 static int console_getc(int file)
109 /* This is never called with testcdev == NULL */
110 ret = tstcdev->getc();
115 static int console_tstc(int file)
118 struct stdio_dev *dev;
121 for (i = 0; i < cd_count[file]; i++) {
122 dev = console_devices[file][i];
123 if (dev->tstc != NULL) {
137 static void console_putc(int file, const char c)
140 struct stdio_dev *dev;
142 for (i = 0; i < cd_count[file]; i++) {
143 dev = console_devices[file][i];
144 if (dev->putc != NULL)
149 static void console_puts(int file, const char *s)
152 struct stdio_dev *dev;
154 for (i = 0; i < cd_count[file]; i++) {
155 dev = console_devices[file][i];
156 if (dev->puts != NULL)
161 static inline void console_printdevs(int file)
163 iomux_printdevs(file);
166 static inline void console_doenv(int file, struct stdio_dev *dev)
168 iomux_doenv(file, dev->name);
171 static inline int console_getc(int file)
173 return stdio_devices[file]->getc();
176 static inline int console_tstc(int file)
178 return stdio_devices[file]->tstc();
181 static inline void console_putc(int file, const char c)
183 stdio_devices[file]->putc(c);
186 static inline void console_puts(int file, const char *s)
188 stdio_devices[file]->puts(s);
191 static inline void console_printdevs(int file)
193 printf("%s\n", stdio_devices[file]->name);
196 static inline void console_doenv(int file, struct stdio_dev *dev)
198 console_setfile(file, dev);
200 #endif /* defined(CONFIG_CONSOLE_MUX) */
202 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
204 void serial_printf(const char *fmt, ...)
208 char printbuffer[CONFIG_SYS_PBSIZE];
212 /* For this to work, printbuffer must be larger than
213 * anything we ever want to print.
215 i = vsprintf(printbuffer, fmt, args);
218 serial_puts(printbuffer);
223 if (file < MAX_FILES) {
224 #if defined(CONFIG_CONSOLE_MUX)
226 * Effectively poll for input wherever it may be available.
230 * Upper layer may have already called tstc() so
231 * check for that first.
234 return console_getc(file);
236 #ifdef CONFIG_WATCHDOG
238 * If the watchdog must be rate-limited then it should
239 * already be handled in board-specific code.
245 return console_getc(file);
254 if (file < MAX_FILES)
255 return console_tstc(file);
260 void fputc(int file, const char c)
262 if (file < MAX_FILES)
263 console_putc(file, c);
266 void fputs(int file, const char *s)
268 if (file < MAX_FILES)
269 console_puts(file, s);
272 void fprintf(int file, const char *fmt, ...)
276 char printbuffer[CONFIG_SYS_PBSIZE];
280 /* For this to work, printbuffer must be larger than
281 * anything we ever want to print.
283 i = vsprintf(printbuffer, fmt, args);
286 /* Send to desired file */
287 fputs(file, printbuffer);
290 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
294 #ifdef CONFIG_DISABLE_CONSOLE
295 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
299 if (gd->flags & GD_FLG_DEVINIT) {
300 /* Get from the standard input */
304 /* Send directly to the handler */
305 return serial_getc();
310 #ifdef CONFIG_DISABLE_CONSOLE
311 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
315 if (gd->flags & GD_FLG_DEVINIT) {
316 /* Test the standard input */
320 /* Send directly to the handler */
321 return serial_tstc();
324 void putc(const char c)
326 #ifdef CONFIG_SILENT_CONSOLE
327 if (gd->flags & GD_FLG_SILENT)
331 #ifdef CONFIG_DISABLE_CONSOLE
332 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
336 if (gd->flags & GD_FLG_DEVINIT) {
337 /* Send to the standard output */
340 /* Send directly to the handler */
345 void puts(const char *s)
347 #ifdef CONFIG_SILENT_CONSOLE
348 if (gd->flags & GD_FLG_SILENT)
352 #ifdef CONFIG_DISABLE_CONSOLE
353 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
357 if (gd->flags & GD_FLG_DEVINIT) {
358 /* Send to the standard output */
361 /* Send directly to the handler */
366 void printf(const char *fmt, ...)
370 char printbuffer[CONFIG_SYS_PBSIZE];
374 /* For this to work, printbuffer must be larger than
375 * anything we ever want to print.
377 i = vsprintf(printbuffer, fmt, args);
380 /* Print the string */
384 void vprintf(const char *fmt, va_list args)
387 char printbuffer[CONFIG_SYS_PBSIZE];
389 /* For this to work, printbuffer must be larger than
390 * anything we ever want to print.
392 i = vsprintf(printbuffer, fmt, args);
394 /* Print the string */
398 /* test if ctrl-c was pressed */
399 static int ctrlc_disabled = 0; /* see disable_ctrl() */
400 static int ctrlc_was_pressed = 0;
403 if (!ctrlc_disabled && gd->have_console) {
406 case 0x03: /* ^C - Control C */
407 ctrlc_was_pressed = 1;
417 /* pass 1 to disable ctrlc() checking, 0 to enable.
418 * returns previous state
420 int disable_ctrlc(int disable)
422 int prev = ctrlc_disabled; /* save previous state */
424 ctrlc_disabled = disable;
430 return ctrlc_was_pressed;
433 void clear_ctrlc(void)
435 ctrlc_was_pressed = 0;
438 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
440 char *cursor = screen;
442 inline void dbg(const char *fmt, ...)
446 char printbuffer[CONFIG_SYS_PBSIZE];
449 memset(screen, 0, sizeof(screen));
455 /* For this to work, printbuffer must be larger than
456 * anything we ever want to print.
458 i = vsprintf(printbuffer, fmt, args);
461 if ((screen + sizeof(screen) - 1 - cursor)
462 < strlen(printbuffer) + 1) {
463 memset(screen, 0, sizeof(screen));
466 sprintf(cursor, printbuffer);
467 cursor += strlen(printbuffer);
471 inline void dbg(const char *fmt, ...)
476 /** U-Boot INIT FUNCTIONS *************************************************/
478 struct stdio_dev *search_device(int flags, char *name)
480 struct stdio_dev *dev;
482 dev = stdio_get_by_name(name);
484 if (dev && (dev->flags & flags))
490 int console_assign(int file, char *devname)
493 struct stdio_dev *dev;
495 /* Check for valid file */
498 flag = DEV_FLAGS_INPUT;
502 flag = DEV_FLAGS_OUTPUT;
508 /* Check for valid device name */
510 dev = search_device(flag, devname);
513 return console_setfile(file, dev);
518 /* Called before relocation - use serial functions */
519 int console_init_f(void)
521 gd->have_console = 1;
523 #ifdef CONFIG_SILENT_CONSOLE
524 if (getenv("silent") != NULL)
525 gd->flags |= GD_FLG_SILENT;
531 void stdio_print_current_devices(void)
533 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
534 /* Print information */
536 if (stdio_devices[stdin] == NULL) {
537 puts("No input devices available!\n");
539 printf ("%s\n", stdio_devices[stdin]->name);
543 if (stdio_devices[stdout] == NULL) {
544 puts("No output devices available!\n");
546 printf ("%s\n", stdio_devices[stdout]->name);
550 if (stdio_devices[stderr] == NULL) {
551 puts("No error devices available!\n");
553 printf ("%s\n", stdio_devices[stderr]->name);
555 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
558 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
559 /* Called after the relocation - use desired console functions */
560 int console_init_r(void)
562 char *stdinname, *stdoutname, *stderrname;
563 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
564 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
566 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
567 #ifdef CONFIG_CONSOLE_MUX
571 /* set default handlers at first */
572 gd->jt[XF_getc] = serial_getc;
573 gd->jt[XF_tstc] = serial_tstc;
574 gd->jt[XF_putc] = serial_putc;
575 gd->jt[XF_puts] = serial_puts;
576 gd->jt[XF_printf] = serial_printf;
578 /* stdin stdout and stderr are in environment */
580 stdinname = getenv("stdin");
581 stdoutname = getenv("stdout");
582 stderrname = getenv("stderr");
584 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
585 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
586 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
587 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
588 #ifdef CONFIG_CONSOLE_MUX
589 iomux_err = iomux_doenv(stdin, stdinname);
590 iomux_err += iomux_doenv(stdout, stdoutname);
591 iomux_err += iomux_doenv(stderr, stderrname);
593 /* Successful, so skip all the code below. */
597 /* if the devices are overwritten or not found, use default device */
598 if (inputdev == NULL) {
599 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
601 if (outputdev == NULL) {
602 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
604 if (errdev == NULL) {
605 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
607 /* Initializes output console first */
608 if (outputdev != NULL) {
609 /* need to set a console if not done above. */
610 console_doenv(stdout, outputdev);
612 if (errdev != NULL) {
613 /* need to set a console if not done above. */
614 console_doenv(stderr, errdev);
616 if (inputdev != NULL) {
617 /* need to set a console if not done above. */
618 console_doenv(stdin, inputdev);
621 #ifdef CONFIG_CONSOLE_MUX
625 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
627 stdio_print_current_devices();
629 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
630 /* set the environment variables (will overwrite previous env settings) */
631 for (i = 0; i < 3; i++) {
632 setenv(stdio_names[i], stdio_devices[i]->name);
634 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
637 /* If nothing usable installed, use only the initial console */
638 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
644 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
646 /* Called after the relocation - use desired console functions */
647 int console_init_r(void)
649 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
651 struct list_head *list = stdio_get_list();
652 struct list_head *pos;
653 struct stdio_dev *dev;
655 #ifdef CONFIG_SPLASH_SCREEN
657 * suppress all output if splash screen is enabled and we have
658 * a bmp to display. We redirect the output from frame buffer
659 * console to serial console in this case or suppress it if
660 * "silent" mode was requested.
662 if (getenv("splashimage") != NULL) {
663 if (!(gd->flags & GD_FLG_SILENT))
664 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
668 /* Scan devices looking for input and output devices */
669 list_for_each(pos, list) {
670 dev = list_entry(pos, struct stdio_dev, list);
672 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
675 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
678 if(inputdev && outputdev)
682 /* Initializes output console first */
683 if (outputdev != NULL) {
684 console_setfile(stdout, outputdev);
685 console_setfile(stderr, outputdev);
686 #ifdef CONFIG_CONSOLE_MUX
687 console_devices[stdout][0] = outputdev;
688 console_devices[stderr][0] = outputdev;
692 /* Initializes input console */
693 if (inputdev != NULL) {
694 console_setfile(stdin, inputdev);
695 #ifdef CONFIG_CONSOLE_MUX
696 console_devices[stdin][0] = inputdev;
700 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
702 stdio_print_current_devices();
704 /* Setting environment variables */
705 for (i = 0; i < 3; i++) {
706 setenv(stdio_names[i], stdio_devices[i]->name);
710 /* If nothing usable installed, use only the initial console */
711 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
718 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */