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->flags & GD_FLG_DEVINIT) {
302 /* Get from the standard input */
306 /* Send directly to the handler */
307 return serial_getc();
312 #ifdef CONFIG_DISABLE_CONSOLE
313 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
317 if (gd->flags & GD_FLG_DEVINIT) {
318 /* Test the standard input */
322 /* Send directly to the handler */
323 return serial_tstc();
326 void putc(const char c)
328 #ifdef CONFIG_SILENT_CONSOLE
329 if (gd->flags & GD_FLG_SILENT)
333 #ifdef CONFIG_DISABLE_CONSOLE
334 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
338 if (gd->flags & GD_FLG_DEVINIT) {
339 /* Send to the standard output */
342 /* Send directly to the handler */
347 void puts(const char *s)
349 #ifdef CONFIG_SILENT_CONSOLE
350 if (gd->flags & GD_FLG_SILENT)
354 #ifdef CONFIG_DISABLE_CONSOLE
355 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
359 if (gd->flags & GD_FLG_DEVINIT) {
360 /* Send to the standard output */
363 /* Send directly to the handler */
368 int printf(const char *fmt, ...)
372 char printbuffer[CONFIG_SYS_PBSIZE];
376 /* For this to work, printbuffer must be larger than
377 * anything we ever want to print.
379 i = vsprintf(printbuffer, fmt, args);
382 /* Print the string */
387 int vprintf(const char *fmt, va_list args)
390 char printbuffer[CONFIG_SYS_PBSIZE];
392 /* For this to work, printbuffer must be larger than
393 * anything we ever want to print.
395 i = vsprintf(printbuffer, fmt, args);
397 /* Print the string */
402 /* test if ctrl-c was pressed */
403 static int ctrlc_disabled = 0; /* see disable_ctrl() */
404 static int ctrlc_was_pressed = 0;
407 if (!ctrlc_disabled && gd->have_console) {
410 case 0x03: /* ^C - Control C */
411 ctrlc_was_pressed = 1;
421 /* pass 1 to disable ctrlc() checking, 0 to enable.
422 * returns previous state
424 int disable_ctrlc(int disable)
426 int prev = ctrlc_disabled; /* save previous state */
428 ctrlc_disabled = disable;
434 return ctrlc_was_pressed;
437 void clear_ctrlc(void)
439 ctrlc_was_pressed = 0;
442 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
444 char *cursor = screen;
446 inline void dbg(const char *fmt, ...)
450 char printbuffer[CONFIG_SYS_PBSIZE];
453 memset(screen, 0, sizeof(screen));
459 /* For this to work, printbuffer must be larger than
460 * anything we ever want to print.
462 i = vsprintf(printbuffer, fmt, args);
465 if ((screen + sizeof(screen) - 1 - cursor)
466 < strlen(printbuffer) + 1) {
467 memset(screen, 0, sizeof(screen));
470 sprintf(cursor, printbuffer);
471 cursor += strlen(printbuffer);
475 inline void dbg(const char *fmt, ...)
480 /** U-Boot INIT FUNCTIONS *************************************************/
482 struct stdio_dev *search_device(int flags, const char *name)
484 struct stdio_dev *dev;
486 dev = stdio_get_by_name(name);
488 if (dev && (dev->flags & flags))
494 int console_assign(int file, const char *devname)
497 struct stdio_dev *dev;
499 /* Check for valid file */
502 flag = DEV_FLAGS_INPUT;
506 flag = DEV_FLAGS_OUTPUT;
512 /* Check for valid device name */
514 dev = search_device(flag, devname);
517 return console_setfile(file, dev);
522 /* Called before relocation - use serial functions */
523 int console_init_f(void)
525 gd->have_console = 1;
527 #ifdef CONFIG_SILENT_CONSOLE
528 if (getenv("silent") != NULL)
529 gd->flags |= GD_FLG_SILENT;
535 void stdio_print_current_devices(void)
537 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
538 /* Print information */
540 if (stdio_devices[stdin] == NULL) {
541 puts("No input devices available!\n");
543 printf ("%s\n", stdio_devices[stdin]->name);
547 if (stdio_devices[stdout] == NULL) {
548 puts("No output devices available!\n");
550 printf ("%s\n", stdio_devices[stdout]->name);
554 if (stdio_devices[stderr] == NULL) {
555 puts("No error devices available!\n");
557 printf ("%s\n", stdio_devices[stderr]->name);
559 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
562 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
563 /* Called after the relocation - use desired console functions */
564 int console_init_r(void)
566 char *stdinname, *stdoutname, *stderrname;
567 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
568 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
570 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
571 #ifdef CONFIG_CONSOLE_MUX
575 /* set default handlers at first */
576 gd->jt[XF_getc] = serial_getc;
577 gd->jt[XF_tstc] = serial_tstc;
578 gd->jt[XF_putc] = serial_putc;
579 gd->jt[XF_puts] = serial_puts;
580 gd->jt[XF_printf] = serial_printf;
582 /* stdin stdout and stderr are in environment */
584 stdinname = getenv("stdin");
585 stdoutname = getenv("stdout");
586 stderrname = getenv("stderr");
588 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
589 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
590 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
591 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
592 #ifdef CONFIG_CONSOLE_MUX
593 iomux_err = iomux_doenv(stdin, stdinname);
594 iomux_err += iomux_doenv(stdout, stdoutname);
595 iomux_err += iomux_doenv(stderr, stderrname);
597 /* Successful, so skip all the code below. */
601 /* if the devices are overwritten or not found, use default device */
602 if (inputdev == NULL) {
603 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
605 if (outputdev == NULL) {
606 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
608 if (errdev == NULL) {
609 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
611 /* Initializes output console first */
612 if (outputdev != NULL) {
613 /* need to set a console if not done above. */
614 console_doenv(stdout, outputdev);
616 if (errdev != NULL) {
617 /* need to set a console if not done above. */
618 console_doenv(stderr, errdev);
620 if (inputdev != NULL) {
621 /* need to set a console if not done above. */
622 console_doenv(stdin, inputdev);
625 #ifdef CONFIG_CONSOLE_MUX
629 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
631 stdio_print_current_devices();
633 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
634 /* set the environment variables (will overwrite previous env settings) */
635 for (i = 0; i < 3; i++) {
636 setenv(stdio_names[i], stdio_devices[i]->name);
638 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
641 /* If nothing usable installed, use only the initial console */
642 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
648 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
650 /* Called after the relocation - use desired console functions */
651 int console_init_r(void)
653 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
655 struct list_head *list = stdio_get_list();
656 struct list_head *pos;
657 struct stdio_dev *dev;
659 #ifdef CONFIG_SPLASH_SCREEN
661 * suppress all output if splash screen is enabled and we have
662 * a bmp to display. We redirect the output from frame buffer
663 * console to serial console in this case or suppress it if
664 * "silent" mode was requested.
666 if (getenv("splashimage") != NULL) {
667 if (!(gd->flags & GD_FLG_SILENT))
668 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
672 /* Scan devices looking for input and output devices */
673 list_for_each(pos, list) {
674 dev = list_entry(pos, struct stdio_dev, list);
676 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
679 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
682 if(inputdev && outputdev)
686 /* Initializes output console first */
687 if (outputdev != NULL) {
688 console_setfile(stdout, outputdev);
689 console_setfile(stderr, outputdev);
690 #ifdef CONFIG_CONSOLE_MUX
691 console_devices[stdout][0] = outputdev;
692 console_devices[stderr][0] = outputdev;
696 /* Initializes input console */
697 if (inputdev != NULL) {
698 console_setfile(stdin, inputdev);
699 #ifdef CONFIG_CONSOLE_MUX
700 console_devices[stdin][0] = inputdev;
704 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
706 stdio_print_current_devices();
708 /* Setting environment variables */
709 for (i = 0; i < 3; i++) {
710 setenv(stdio_names[i], stdio_devices[i]->name);
714 /* If nothing usable installed, use only the initial console */
715 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
722 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */