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,
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 int 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);
224 if (file < MAX_FILES) {
225 #if defined(CONFIG_CONSOLE_MUX)
227 * Effectively poll for input wherever it may be available.
231 * Upper layer may have already called tstc() so
232 * check for that first.
235 return console_getc(file);
237 #ifdef CONFIG_WATCHDOG
239 * If the watchdog must be rate-limited then it should
240 * already be handled in board-specific code.
246 return console_getc(file);
255 if (file < MAX_FILES)
256 return console_tstc(file);
261 void fputc(int file, const char c)
263 if (file < MAX_FILES)
264 console_putc(file, c);
267 void fputs(int file, const char *s)
269 if (file < MAX_FILES)
270 console_puts(file, s);
273 int fprintf(int file, const char *fmt, ...)
277 char printbuffer[CONFIG_SYS_PBSIZE];
281 /* For this to work, printbuffer must be larger than
282 * anything we ever want to print.
284 i = vsprintf(printbuffer, fmt, args);
287 /* Send to desired file */
288 fputs(file, printbuffer);
292 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
296 #ifdef CONFIG_DISABLE_CONSOLE
297 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
301 if (!gd->have_console)
304 if (gd->flags & GD_FLG_DEVINIT) {
305 /* Get from the standard input */
309 /* Send directly to the handler */
310 return serial_getc();
315 #ifdef CONFIG_DISABLE_CONSOLE
316 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
320 if (!gd->have_console)
323 if (gd->flags & GD_FLG_DEVINIT) {
324 /* Test the standard input */
328 /* Send directly to the handler */
329 return serial_tstc();
332 #if defined(CONFIG_PRE_CONSOLE_BUFFER) || defined(CONFIG_PRE_CONSOLE_PUTC)
333 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
335 static void pre_console_putc(const char c)
337 #ifdef CONFIG_PRE_CONSOLE_BUFFER
338 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
340 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
342 #ifdef CONFIG_PRE_CONSOLE_PUTC
343 board_pre_console_putc(c);
347 static void pre_console_puts(const char *s)
350 pre_console_putc(*s++);
353 static void print_pre_console_buffer(void)
355 #ifdef CONFIG_PRE_CONSOLE_BUFFER
357 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
359 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
360 i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
362 while (i < gd->precon_buf_idx)
363 putc(buffer[CIRC_BUF_IDX(i++)]);
368 static inline void pre_console_putc(const char c) {}
369 static inline void pre_console_puts(const char *s) {}
370 static inline void print_pre_console_buffer(void) {}
373 void putc(const char c)
375 #ifdef CONFIG_SILENT_CONSOLE
376 if (gd->flags & GD_FLG_SILENT)
380 #ifdef CONFIG_DISABLE_CONSOLE
381 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
385 if (!gd->have_console)
386 return pre_console_putc(c);
388 if (gd->flags & GD_FLG_DEVINIT) {
389 /* Send to the standard output */
392 /* Send directly to the handler */
397 void puts(const char *s)
399 #ifdef CONFIG_SILENT_CONSOLE
400 if (gd->flags & GD_FLG_SILENT)
404 #ifdef CONFIG_DISABLE_CONSOLE
405 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
409 if (!gd->have_console)
410 return pre_console_puts(s);
412 if (gd->flags & GD_FLG_DEVINIT) {
413 /* Send to the standard output */
416 /* Send directly to the handler */
421 int printf(const char *fmt, ...)
425 char printbuffer[CONFIG_SYS_PBSIZE];
427 #ifndef CONFIG_PRE_CONSOLE_BUFFER
428 if (!gd->have_console)
434 /* For this to work, printbuffer must be larger than
435 * anything we ever want to print.
437 i = vsprintf(printbuffer, fmt, args);
440 /* Print the string */
445 int vprintf(const char *fmt, va_list args)
448 char printbuffer[CONFIG_SYS_PBSIZE];
450 #ifndef CONFIG_PRE_CONSOLE_BUFFER
451 if (!gd->have_console)
455 /* For this to work, printbuffer must be larger than
456 * anything we ever want to print.
458 i = vsprintf(printbuffer, fmt, args);
460 /* Print the string */
465 /* test if ctrl-c was pressed */
466 static int ctrlc_disabled = 0; /* see disable_ctrl() */
467 static int ctrlc_was_pressed = 0;
470 if (!ctrlc_disabled && gd->have_console) {
473 case 0x03: /* ^C - Control C */
474 ctrlc_was_pressed = 1;
484 /* pass 1 to disable ctrlc() checking, 0 to enable.
485 * returns previous state
487 int disable_ctrlc(int disable)
489 int prev = ctrlc_disabled; /* save previous state */
491 ctrlc_disabled = disable;
497 return ctrlc_was_pressed;
500 void clear_ctrlc(void)
502 ctrlc_was_pressed = 0;
505 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
507 char *cursor = screen;
509 inline void dbg(const char *fmt, ...)
513 char printbuffer[CONFIG_SYS_PBSIZE];
516 memset(screen, 0, sizeof(screen));
522 /* For this to work, printbuffer must be larger than
523 * anything we ever want to print.
525 i = vsprintf(printbuffer, fmt, args);
528 if ((screen + sizeof(screen) - 1 - cursor)
529 < strlen(printbuffer) + 1) {
530 memset(screen, 0, sizeof(screen));
533 sprintf(cursor, printbuffer);
534 cursor += strlen(printbuffer);
538 inline void dbg(const char *fmt, ...)
543 /** U-Boot INIT FUNCTIONS *************************************************/
545 struct stdio_dev *search_device(int flags, const char *name)
547 struct stdio_dev *dev;
549 dev = stdio_get_by_name(name);
551 if (dev && (dev->flags & flags))
557 int console_assign(int file, const char *devname)
560 struct stdio_dev *dev;
562 /* Check for valid file */
565 flag = DEV_FLAGS_INPUT;
569 flag = DEV_FLAGS_OUTPUT;
575 /* Check for valid device name */
577 dev = search_device(flag, devname);
580 return console_setfile(file, dev);
585 /* Called before relocation - use serial functions */
586 int console_init_f(void)
588 gd->have_console = 1;
590 #ifdef CONFIG_SILENT_CONSOLE
591 if (getenv("silent") != NULL)
592 gd->flags |= GD_FLG_SILENT;
595 print_pre_console_buffer();
600 void stdio_print_current_devices(void)
602 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
603 /* Print information */
605 if (stdio_devices[stdin] == NULL) {
606 puts("No input devices available!\n");
608 printf ("%s\n", stdio_devices[stdin]->name);
612 if (stdio_devices[stdout] == NULL) {
613 puts("No output devices available!\n");
615 printf ("%s\n", stdio_devices[stdout]->name);
619 if (stdio_devices[stderr] == NULL) {
620 puts("No error devices available!\n");
622 printf ("%s\n", stdio_devices[stderr]->name);
624 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
627 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
628 /* Called after the relocation - use desired console functions */
629 int console_init_r(void)
631 char *stdinname, *stdoutname, *stderrname;
632 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
633 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
635 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
636 #ifdef CONFIG_CONSOLE_MUX
640 /* set default handlers at first */
641 gd->jt[XF_getc] = serial_getc;
642 gd->jt[XF_tstc] = serial_tstc;
643 gd->jt[XF_putc] = serial_putc;
644 gd->jt[XF_puts] = serial_puts;
645 gd->jt[XF_printf] = serial_printf;
647 /* stdin stdout and stderr are in environment */
649 stdinname = getenv("stdin");
650 stdoutname = getenv("stdout");
651 stderrname = getenv("stderr");
653 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
654 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
655 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
656 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
657 #ifdef CONFIG_CONSOLE_MUX
658 iomux_err = iomux_doenv(stdin, stdinname);
659 iomux_err += iomux_doenv(stdout, stdoutname);
660 iomux_err += iomux_doenv(stderr, stderrname);
662 /* Successful, so skip all the code below. */
666 /* if the devices are overwritten or not found, use default device */
667 if (inputdev == NULL) {
668 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
670 if (outputdev == NULL) {
671 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
673 if (errdev == NULL) {
674 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
676 /* Initializes output console first */
677 if (outputdev != NULL) {
678 /* need to set a console if not done above. */
679 console_doenv(stdout, outputdev);
681 if (errdev != NULL) {
682 /* need to set a console if not done above. */
683 console_doenv(stderr, errdev);
685 if (inputdev != NULL) {
686 /* need to set a console if not done above. */
687 console_doenv(stdin, inputdev);
690 #ifdef CONFIG_CONSOLE_MUX
694 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
696 stdio_print_current_devices();
698 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
699 /* set the environment variables (will overwrite previous env settings) */
700 for (i = 0; i < 3; i++) {
701 setenv(stdio_names[i], stdio_devices[i]->name);
703 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
706 /* If nothing usable installed, use only the initial console */
707 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
713 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
715 /* Called after the relocation - use desired console functions */
716 int console_init_r(void)
718 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
720 struct list_head *list = stdio_get_list();
721 struct list_head *pos;
722 struct stdio_dev *dev;
724 #ifdef CONFIG_SPLASH_SCREEN
726 * suppress all output if splash screen is enabled and we have
727 * a bmp to display. We redirect the output from frame buffer
728 * console to serial console in this case or suppress it if
729 * "silent" mode was requested.
731 if (getenv("splashimage") != NULL) {
732 if (!(gd->flags & GD_FLG_SILENT))
733 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
737 /* Scan devices looking for input and output devices */
738 list_for_each(pos, list) {
739 dev = list_entry(pos, struct stdio_dev, list);
741 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
744 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
747 if(inputdev && outputdev)
751 /* Initializes output console first */
752 if (outputdev != NULL) {
753 console_setfile(stdout, outputdev);
754 console_setfile(stderr, outputdev);
755 #ifdef CONFIG_CONSOLE_MUX
756 console_devices[stdout][0] = outputdev;
757 console_devices[stderr][0] = outputdev;
761 /* Initializes input console */
762 if (inputdev != NULL) {
763 console_setfile(stdin, inputdev);
764 #ifdef CONFIG_CONSOLE_MUX
765 console_devices[stdin][0] = inputdev;
769 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
771 stdio_print_current_devices();
773 /* Setting environment variables */
774 for (i = 0; i < 3; i++) {
775 setenv(stdio_names[i], stdio_devices[i]->name);
779 /* If nothing usable installed, use only the initial console */
780 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
787 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */