3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
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,
28 #include <stdio_dev.h>
30 #include <environment.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 static int on_console(const char *name, const char *value, enum env_op op,
39 /* Check for console redirection */
40 if (strcmp(name, "stdin") == 0)
42 else if (strcmp(name, "stdout") == 0)
44 else if (strcmp(name, "stderr") == 0)
47 /* if not actually setting a console variable, we don't care */
48 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
53 case env_op_overwrite:
55 #ifdef CONFIG_CONSOLE_MUX
56 if (iomux_doenv(console, value))
59 /* Try assigning specified device */
60 if (console_assign(console, value) < 0)
62 #endif /* CONFIG_CONSOLE_MUX */
66 if ((flags & H_FORCE) == 0)
67 printf("Can't delete \"%s\"\n", name);
74 U_BOOT_ENV_CALLBACK(console, on_console);
76 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
78 * if overwrite_console returns 1, the stdin, stderr and stdout
79 * are switched to the serial port, else the settings in the
80 * environment are used
82 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
83 extern int overwrite_console(void);
84 #define OVERWRITE_CONSOLE overwrite_console()
86 #define OVERWRITE_CONSOLE 0
87 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
89 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
91 static int console_setfile(int file, struct stdio_dev * dev)
102 /* Start new device */
104 error = dev->start();
105 /* If it's not started dont use it */
110 /* Assign the new device (leaving the existing one started) */
111 stdio_devices[file] = dev;
114 * Update monitor functions
115 * (to use the console stuff by other applications)
119 gd->jt[XF_getc] = dev->getc;
120 gd->jt[XF_tstc] = dev->tstc;
123 gd->jt[XF_putc] = dev->putc;
124 gd->jt[XF_puts] = dev->puts;
125 gd->jt[XF_printf] = printf;
130 default: /* Invalid file ID */
136 #if defined(CONFIG_CONSOLE_MUX)
137 /** Console I/O multiplexing *******************************************/
139 static struct stdio_dev *tstcdev;
140 struct stdio_dev **console_devices[MAX_FILES];
141 int cd_count[MAX_FILES];
144 * This depends on tstc() always being called before getc().
145 * This is guaranteed to be true because this routine is called
146 * only from fgetc() which assures it.
147 * No attempt is made to demultiplex multiple input sources.
149 static int console_getc(int file)
153 /* This is never called with testcdev == NULL */
154 ret = tstcdev->getc();
159 static int console_tstc(int file)
162 struct stdio_dev *dev;
165 for (i = 0; i < cd_count[file]; i++) {
166 dev = console_devices[file][i];
167 if (dev->tstc != NULL) {
181 static void console_putc(int file, const char c)
184 struct stdio_dev *dev;
186 for (i = 0; i < cd_count[file]; i++) {
187 dev = console_devices[file][i];
188 if (dev->putc != NULL)
193 static void console_puts(int file, const char *s)
196 struct stdio_dev *dev;
198 for (i = 0; i < cd_count[file]; i++) {
199 dev = console_devices[file][i];
200 if (dev->puts != NULL)
205 static inline void console_printdevs(int file)
207 iomux_printdevs(file);
210 static inline void console_doenv(int file, struct stdio_dev *dev)
212 iomux_doenv(file, dev->name);
215 static inline int console_getc(int file)
217 return stdio_devices[file]->getc();
220 static inline int console_tstc(int file)
222 return stdio_devices[file]->tstc();
225 static inline void console_putc(int file, const char c)
227 stdio_devices[file]->putc(c);
230 static inline void console_puts(int file, const char *s)
232 stdio_devices[file]->puts(s);
235 static inline void console_printdevs(int file)
237 printf("%s\n", stdio_devices[file]->name);
240 static inline void console_doenv(int file, struct stdio_dev *dev)
242 console_setfile(file, dev);
244 #endif /* defined(CONFIG_CONSOLE_MUX) */
246 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
248 int serial_printf(const char *fmt, ...)
252 char printbuffer[CONFIG_SYS_PBSIZE];
256 /* For this to work, printbuffer must be larger than
257 * anything we ever want to print.
259 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
262 serial_puts(printbuffer);
268 if (file < MAX_FILES) {
269 #if defined(CONFIG_CONSOLE_MUX)
271 * Effectively poll for input wherever it may be available.
275 * Upper layer may have already called tstc() so
276 * check for that first.
279 return console_getc(file);
281 #ifdef CONFIG_WATCHDOG
283 * If the watchdog must be rate-limited then it should
284 * already be handled in board-specific code.
290 return console_getc(file);
299 if (file < MAX_FILES)
300 return console_tstc(file);
305 void fputc(int file, const char c)
307 if (file < MAX_FILES)
308 console_putc(file, c);
311 void fputs(int file, const char *s)
313 if (file < MAX_FILES)
314 console_puts(file, s);
317 int fprintf(int file, const char *fmt, ...)
321 char printbuffer[CONFIG_SYS_PBSIZE];
325 /* For this to work, printbuffer must be larger than
326 * anything we ever want to print.
328 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
331 /* Send to desired file */
332 fputs(file, printbuffer);
336 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
340 #ifdef CONFIG_DISABLE_CONSOLE
341 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
345 if (!gd->have_console)
348 if (gd->flags & GD_FLG_DEVINIT) {
349 /* Get from the standard input */
353 /* Send directly to the handler */
354 return serial_getc();
359 #ifdef CONFIG_DISABLE_CONSOLE
360 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
364 if (!gd->have_console)
367 if (gd->flags & GD_FLG_DEVINIT) {
368 /* Test the standard input */
372 /* Send directly to the handler */
373 return serial_tstc();
376 #ifdef CONFIG_PRE_CONSOLE_BUFFER
377 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
379 static void pre_console_putc(const char c)
381 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
383 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
386 static void pre_console_puts(const char *s)
389 pre_console_putc(*s++);
392 static void print_pre_console_buffer(void)
395 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
397 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
398 i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
400 while (i < gd->precon_buf_idx)
401 putc(buffer[CIRC_BUF_IDX(i++)]);
404 static inline void pre_console_putc(const char c) {}
405 static inline void pre_console_puts(const char *s) {}
406 static inline void print_pre_console_buffer(void) {}
409 void putc(const char c)
411 #ifdef CONFIG_SILENT_CONSOLE
412 if (gd->flags & GD_FLG_SILENT)
416 #ifdef CONFIG_DISABLE_CONSOLE
417 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
421 if (!gd->have_console)
422 return pre_console_putc(c);
424 if (gd->flags & GD_FLG_DEVINIT) {
425 /* Send to the standard output */
428 /* Send directly to the handler */
433 void puts(const char *s)
435 #ifdef CONFIG_SILENT_CONSOLE
436 if (gd->flags & GD_FLG_SILENT)
440 #ifdef CONFIG_DISABLE_CONSOLE
441 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
445 if (!gd->have_console)
446 return pre_console_puts(s);
448 if (gd->flags & GD_FLG_DEVINIT) {
449 /* Send to the standard output */
452 /* Send directly to the handler */
457 int printf(const char *fmt, ...)
461 char printbuffer[CONFIG_SYS_PBSIZE];
463 #ifndef CONFIG_PRE_CONSOLE_BUFFER
464 if (!gd->have_console)
470 /* For this to work, printbuffer must be larger than
471 * anything we ever want to print.
473 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
476 /* Print the string */
481 int vprintf(const char *fmt, va_list args)
484 char printbuffer[CONFIG_SYS_PBSIZE];
486 #ifndef CONFIG_PRE_CONSOLE_BUFFER
487 if (!gd->have_console)
491 /* For this to work, printbuffer must be larger than
492 * anything we ever want to print.
494 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
496 /* Print the string */
501 /* test if ctrl-c was pressed */
502 static int ctrlc_disabled = 0; /* see disable_ctrl() */
503 static int ctrlc_was_pressed = 0;
506 if (!ctrlc_disabled && gd->have_console) {
509 case 0x03: /* ^C - Control C */
510 ctrlc_was_pressed = 1;
520 /* pass 1 to disable ctrlc() checking, 0 to enable.
521 * returns previous state
523 int disable_ctrlc(int disable)
525 int prev = ctrlc_disabled; /* save previous state */
527 ctrlc_disabled = disable;
533 return ctrlc_was_pressed;
536 void clear_ctrlc(void)
538 ctrlc_was_pressed = 0;
541 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
543 char *cursor = screen;
545 inline void dbg(const char *fmt, ...)
549 char printbuffer[CONFIG_SYS_PBSIZE];
552 memset(screen, 0, sizeof(screen));
558 /* For this to work, printbuffer must be larger than
559 * anything we ever want to print.
561 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
564 if ((screen + sizeof(screen) - 1 - cursor)
565 < strlen(printbuffer) + 1) {
566 memset(screen, 0, sizeof(screen));
569 sprintf(cursor, printbuffer);
570 cursor += strlen(printbuffer);
574 inline void dbg(const char *fmt, ...)
579 /** U-Boot INIT FUNCTIONS *************************************************/
581 struct stdio_dev *search_device(int flags, const char *name)
583 struct stdio_dev *dev;
585 dev = stdio_get_by_name(name);
587 if (dev && (dev->flags & flags))
593 int console_assign(int file, const char *devname)
596 struct stdio_dev *dev;
598 /* Check for valid file */
601 flag = DEV_FLAGS_INPUT;
605 flag = DEV_FLAGS_OUTPUT;
611 /* Check for valid device name */
613 dev = search_device(flag, devname);
616 return console_setfile(file, dev);
621 /* Called before relocation - use serial functions */
622 int console_init_f(void)
624 gd->have_console = 1;
626 #ifdef CONFIG_SILENT_CONSOLE
627 if (getenv("silent") != NULL)
628 gd->flags |= GD_FLG_SILENT;
631 print_pre_console_buffer();
636 void stdio_print_current_devices(void)
638 /* Print information */
640 if (stdio_devices[stdin] == NULL) {
641 puts("No input devices available!\n");
643 printf ("%s\n", stdio_devices[stdin]->name);
647 if (stdio_devices[stdout] == NULL) {
648 puts("No output devices available!\n");
650 printf ("%s\n", stdio_devices[stdout]->name);
654 if (stdio_devices[stderr] == NULL) {
655 puts("No error devices available!\n");
657 printf ("%s\n", stdio_devices[stderr]->name);
661 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
662 /* Called after the relocation - use desired console functions */
663 int console_init_r(void)
665 char *stdinname, *stdoutname, *stderrname;
666 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
667 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
669 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
670 #ifdef CONFIG_CONSOLE_MUX
674 /* set default handlers at first */
675 gd->jt[XF_getc] = serial_getc;
676 gd->jt[XF_tstc] = serial_tstc;
677 gd->jt[XF_putc] = serial_putc;
678 gd->jt[XF_puts] = serial_puts;
679 gd->jt[XF_printf] = serial_printf;
681 /* stdin stdout and stderr are in environment */
683 stdinname = getenv("stdin");
684 stdoutname = getenv("stdout");
685 stderrname = getenv("stderr");
687 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
688 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
689 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
690 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
691 #ifdef CONFIG_CONSOLE_MUX
692 iomux_err = iomux_doenv(stdin, stdinname);
693 iomux_err += iomux_doenv(stdout, stdoutname);
694 iomux_err += iomux_doenv(stderr, stderrname);
696 /* Successful, so skip all the code below. */
700 /* if the devices are overwritten or not found, use default device */
701 if (inputdev == NULL) {
702 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
704 if (outputdev == NULL) {
705 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
707 if (errdev == NULL) {
708 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
710 /* Initializes output console first */
711 if (outputdev != NULL) {
712 /* need to set a console if not done above. */
713 console_doenv(stdout, outputdev);
715 if (errdev != NULL) {
716 /* need to set a console if not done above. */
717 console_doenv(stderr, errdev);
719 if (inputdev != NULL) {
720 /* need to set a console if not done above. */
721 console_doenv(stdin, inputdev);
724 #ifdef CONFIG_CONSOLE_MUX
728 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
729 stdio_print_current_devices();
730 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
732 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
733 /* set the environment variables (will overwrite previous env settings) */
734 for (i = 0; i < 3; i++) {
735 setenv(stdio_names[i], stdio_devices[i]->name);
737 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
739 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
742 /* If nothing usable installed, use only the initial console */
743 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
749 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
751 /* Called after the relocation - use desired console functions */
752 int console_init_r(void)
754 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
756 struct list_head *list = stdio_get_list();
757 struct list_head *pos;
758 struct stdio_dev *dev;
760 #ifdef CONFIG_SPLASH_SCREEN
762 * suppress all output if splash screen is enabled and we have
763 * a bmp to display. We redirect the output from frame buffer
764 * console to serial console in this case or suppress it if
765 * "silent" mode was requested.
767 if (getenv("splashimage") != NULL) {
768 if (!(gd->flags & GD_FLG_SILENT))
769 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
773 /* Scan devices looking for input and output devices */
774 list_for_each(pos, list) {
775 dev = list_entry(pos, struct stdio_dev, list);
777 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
780 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
783 if(inputdev && outputdev)
787 /* Initializes output console first */
788 if (outputdev != NULL) {
789 console_setfile(stdout, outputdev);
790 console_setfile(stderr, outputdev);
791 #ifdef CONFIG_CONSOLE_MUX
792 console_devices[stdout][0] = outputdev;
793 console_devices[stderr][0] = outputdev;
797 /* Initializes input console */
798 if (inputdev != NULL) {
799 console_setfile(stdin, inputdev);
800 #ifdef CONFIG_CONSOLE_MUX
801 console_devices[stdin][0] = inputdev;
805 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
806 stdio_print_current_devices();
807 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
809 /* Setting environment variables */
810 for (i = 0; i < 3; i++) {
811 setenv(stdio_names[i], stdio_devices[i]->name);
814 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
817 /* If nothing usable installed, use only the initial console */
818 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
825 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */