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 = vscnprintf(printbuffer, sizeof(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 = vscnprintf(printbuffer, sizeof(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 #ifdef CONFIG_PRE_CONSOLE_BUFFER
333 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
335 static void pre_console_putc(const char c)
337 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
339 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
342 static void pre_console_puts(const char *s)
345 pre_console_putc(*s++);
348 static void print_pre_console_buffer(void)
351 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
353 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
354 i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
356 while (i < gd->precon_buf_idx)
357 putc(buffer[CIRC_BUF_IDX(i++)]);
360 static inline void pre_console_putc(const char c) {}
361 static inline void pre_console_puts(const char *s) {}
362 static inline void print_pre_console_buffer(void) {}
365 void putc(const char c)
367 #ifdef CONFIG_SILENT_CONSOLE
368 if (gd->flags & GD_FLG_SILENT)
372 #ifdef CONFIG_DISABLE_CONSOLE
373 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
377 if (!gd->have_console)
378 return pre_console_putc(c);
380 if (gd->flags & GD_FLG_DEVINIT) {
381 /* Send to the standard output */
384 /* Send directly to the handler */
389 void puts(const char *s)
391 #ifdef CONFIG_SILENT_CONSOLE
392 if (gd->flags & GD_FLG_SILENT)
396 #ifdef CONFIG_DISABLE_CONSOLE
397 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
401 if (!gd->have_console)
402 return pre_console_puts(s);
404 if (gd->flags & GD_FLG_DEVINIT) {
405 /* Send to the standard output */
408 /* Send directly to the handler */
413 int printf(const char *fmt, ...)
417 char printbuffer[CONFIG_SYS_PBSIZE];
419 #ifndef CONFIG_PRE_CONSOLE_BUFFER
420 if (!gd->have_console)
426 /* For this to work, printbuffer must be larger than
427 * anything we ever want to print.
429 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
432 /* Print the string */
437 int vprintf(const char *fmt, va_list args)
440 char printbuffer[CONFIG_SYS_PBSIZE];
442 #ifndef CONFIG_PRE_CONSOLE_BUFFER
443 if (!gd->have_console)
447 /* For this to work, printbuffer must be larger than
448 * anything we ever want to print.
450 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
452 /* Print the string */
457 /* test if ctrl-c was pressed */
458 static int ctrlc_disabled = 0; /* see disable_ctrl() */
459 static int ctrlc_was_pressed = 0;
462 if (!ctrlc_disabled && gd->have_console) {
465 case 0x03: /* ^C - Control C */
466 ctrlc_was_pressed = 1;
476 /* pass 1 to disable ctrlc() checking, 0 to enable.
477 * returns previous state
479 int disable_ctrlc(int disable)
481 int prev = ctrlc_disabled; /* save previous state */
483 ctrlc_disabled = disable;
489 return ctrlc_was_pressed;
492 void clear_ctrlc(void)
494 ctrlc_was_pressed = 0;
497 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
499 char *cursor = screen;
501 inline void dbg(const char *fmt, ...)
505 char printbuffer[CONFIG_SYS_PBSIZE];
508 memset(screen, 0, sizeof(screen));
514 /* For this to work, printbuffer must be larger than
515 * anything we ever want to print.
517 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
520 if ((screen + sizeof(screen) - 1 - cursor)
521 < strlen(printbuffer) + 1) {
522 memset(screen, 0, sizeof(screen));
525 sprintf(cursor, printbuffer);
526 cursor += strlen(printbuffer);
530 inline void dbg(const char *fmt, ...)
535 /** U-Boot INIT FUNCTIONS *************************************************/
537 struct stdio_dev *search_device(int flags, const char *name)
539 struct stdio_dev *dev;
541 dev = stdio_get_by_name(name);
543 if (dev && (dev->flags & flags))
549 int console_assign(int file, const char *devname)
552 struct stdio_dev *dev;
554 /* Check for valid file */
557 flag = DEV_FLAGS_INPUT;
561 flag = DEV_FLAGS_OUTPUT;
567 /* Check for valid device name */
569 dev = search_device(flag, devname);
572 return console_setfile(file, dev);
577 /* Called before relocation - use serial functions */
578 int console_init_f(void)
580 gd->have_console = 1;
582 #ifdef CONFIG_SILENT_CONSOLE
583 if (getenv("silent") != NULL)
584 gd->flags |= GD_FLG_SILENT;
587 print_pre_console_buffer();
592 void stdio_print_current_devices(void)
594 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
595 /* Print information */
597 if (stdio_devices[stdin] == NULL) {
598 puts("No input devices available!\n");
600 printf ("%s\n", stdio_devices[stdin]->name);
604 if (stdio_devices[stdout] == NULL) {
605 puts("No output devices available!\n");
607 printf ("%s\n", stdio_devices[stdout]->name);
611 if (stdio_devices[stderr] == NULL) {
612 puts("No error devices available!\n");
614 printf ("%s\n", stdio_devices[stderr]->name);
616 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
619 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
620 /* Called after the relocation - use desired console functions */
621 int console_init_r(void)
623 char *stdinname, *stdoutname, *stderrname;
624 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
625 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
627 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
628 #ifdef CONFIG_CONSOLE_MUX
632 /* set default handlers at first */
633 gd->jt[XF_getc] = serial_getc;
634 gd->jt[XF_tstc] = serial_tstc;
635 gd->jt[XF_putc] = serial_putc;
636 gd->jt[XF_puts] = serial_puts;
637 gd->jt[XF_printf] = serial_printf;
639 /* stdin stdout and stderr are in environment */
641 stdinname = getenv("stdin");
642 stdoutname = getenv("stdout");
643 stderrname = getenv("stderr");
645 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
646 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
647 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
648 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
649 #ifdef CONFIG_CONSOLE_MUX
650 iomux_err = iomux_doenv(stdin, stdinname);
651 iomux_err += iomux_doenv(stdout, stdoutname);
652 iomux_err += iomux_doenv(stderr, stderrname);
654 /* Successful, so skip all the code below. */
658 /* if the devices are overwritten or not found, use default device */
659 if (inputdev == NULL) {
660 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
662 if (outputdev == NULL) {
663 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
665 if (errdev == NULL) {
666 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
668 /* Initializes output console first */
669 if (outputdev != NULL) {
670 /* need to set a console if not done above. */
671 console_doenv(stdout, outputdev);
673 if (errdev != NULL) {
674 /* need to set a console if not done above. */
675 console_doenv(stderr, errdev);
677 if (inputdev != NULL) {
678 /* need to set a console if not done above. */
679 console_doenv(stdin, inputdev);
682 #ifdef CONFIG_CONSOLE_MUX
686 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
688 stdio_print_current_devices();
690 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
691 /* set the environment variables (will overwrite previous env settings) */
692 for (i = 0; i < 3; i++) {
693 setenv(stdio_names[i], stdio_devices[i]->name);
695 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
698 /* If nothing usable installed, use only the initial console */
699 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
705 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
707 /* Called after the relocation - use desired console functions */
708 int console_init_r(void)
710 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
712 struct list_head *list = stdio_get_list();
713 struct list_head *pos;
714 struct stdio_dev *dev;
716 #ifdef CONFIG_SPLASH_SCREEN
718 * suppress all output if splash screen is enabled and we have
719 * a bmp to display. We redirect the output from frame buffer
720 * console to serial console in this case or suppress it if
721 * "silent" mode was requested.
723 if (getenv("splashimage") != NULL) {
724 if (!(gd->flags & GD_FLG_SILENT))
725 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
729 /* Scan devices looking for input and output devices */
730 list_for_each(pos, list) {
731 dev = list_entry(pos, struct stdio_dev, list);
733 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
736 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
739 if(inputdev && outputdev)
743 /* Initializes output console first */
744 if (outputdev != NULL) {
745 console_setfile(stdout, outputdev);
746 console_setfile(stderr, outputdev);
747 #ifdef CONFIG_CONSOLE_MUX
748 console_devices[stdout][0] = outputdev;
749 console_devices[stderr][0] = outputdev;
753 /* Initializes input console */
754 if (inputdev != NULL) {
755 console_setfile(stdin, inputdev);
756 #ifdef CONFIG_CONSOLE_MUX
757 console_devices[stdin][0] = inputdev;
761 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
763 stdio_print_current_devices();
765 /* Setting environment variables */
766 for (i = 0; i < 3; i++) {
767 setenv(stdio_names[i], stdio_devices[i]->name);
771 /* If nothing usable installed, use only the initial console */
772 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
779 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */