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 #ifdef CONFIG_RAM_CONSOLE
203 static uint used_size = 0;
205 void ram_puts(const char* s)
208 uint wlen = strlen(s);
210 p = (uint *)CONFIG_RAM_CONSOLE_START + used_size;
212 if (used_size >= CONFIG_RAM_CONSOLE_SIZE) {
213 serial_puts("ram console: space overflow!\n");
216 else if ((used_size + wlen) > CONFIG_RAM_CONSOLE_SIZE) {
217 serial_puts("ram console: no enough ram space !\n");
219 /* fill available space */
220 wlen = CONFIG_RAM_CONSOLE_SIZE - used_size;
221 memcpy((void*)p, (void*)s, wlen);
223 used_size = CONFIG_RAM_CONSOLE_SIZE;
227 memcpy((void*)p, (void*)s, wlen);
234 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
236 int serial_printf(const char *fmt, ...)
240 char printbuffer[CONFIG_SYS_PBSIZE];
244 /* For this to work, printbuffer must be larger than
245 * anything we ever want to print.
247 i = vsprintf(printbuffer, fmt, args);
250 serial_puts(printbuffer);
256 if (file < MAX_FILES) {
257 #if defined(CONFIG_CONSOLE_MUX)
259 * Effectively poll for input wherever it may be available.
263 * Upper layer may have already called tstc() so
264 * check for that first.
267 return console_getc(file);
269 #ifdef CONFIG_WATCHDOG
271 * If the watchdog must be rate-limited then it should
272 * already be handled in board-specific code.
278 return console_getc(file);
287 if (file < MAX_FILES)
288 return console_tstc(file);
293 void fputc(int file, const char c)
295 if (file < MAX_FILES)
296 console_putc(file, c);
299 void fputs(int file, const char *s)
301 if (file < MAX_FILES)
302 console_puts(file, s);
305 int fprintf(int file, const char *fmt, ...)
309 char printbuffer[CONFIG_SYS_PBSIZE];
313 /* For this to work, printbuffer must be larger than
314 * anything we ever want to print.
316 i = vsprintf(printbuffer, fmt, args);
319 /* Send to desired file */
320 fputs(file, printbuffer);
324 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
328 #ifdef CONFIG_DISABLE_CONSOLE
329 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
333 if (gd->flags & GD_FLG_DEVINIT) {
334 /* Get from the standard input */
338 /* Send directly to the handler */
339 return serial_getc();
344 #ifdef CONFIG_DISABLE_CONSOLE
345 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
349 if (gd->flags & GD_FLG_DEVINIT) {
350 /* Test the standard input */
354 /* Send directly to the handler */
355 return serial_tstc();
358 void putc(const char c)
360 #ifdef CONFIG_SILENT_CONSOLE
361 if (gd->flags & GD_FLG_SILENT)
365 #ifdef CONFIG_DISABLE_CONSOLE
366 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
370 if (gd->flags & GD_FLG_DEVINIT) {
371 /* Send to the standard output */
374 /* Send directly to the handler */
379 void puts(const char *s)
381 #ifdef CONFIG_SILENT_CONSOLE
382 if (gd->flags & GD_FLG_SILENT)
386 #ifdef CONFIG_DISABLE_CONSOLE
387 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
391 if (gd->flags & GD_FLG_DEVINIT) {
392 /* Send to the standard output */
395 /* Send directly to the handler */
399 #ifdef CONFIG_RAM_CONSOLE
404 int printf(const char *fmt, ...)
408 char printbuffer[CONFIG_SYS_PBSIZE];
412 /* For this to work, printbuffer must be larger than
413 * anything we ever want to print.
415 i = vsprintf(printbuffer, fmt, args);
418 /* Print the string */
423 int vprintf(const char *fmt, va_list args)
426 char printbuffer[CONFIG_SYS_PBSIZE];
428 /* For this to work, printbuffer must be larger than
429 * anything we ever want to print.
431 i = vsprintf(printbuffer, fmt, args);
433 /* Print the string */
438 /* test if ctrl-c was pressed */
439 static int ctrlc_disabled = 0; /* see disable_ctrl() */
440 static int ctrlc_was_pressed = 0;
443 if (!ctrlc_disabled && gd->have_console) {
446 case 0x03: /* ^C - Control C */
447 ctrlc_was_pressed = 1;
457 /* pass 1 to disable ctrlc() checking, 0 to enable.
458 * returns previous state
460 int disable_ctrlc(int disable)
462 int prev = ctrlc_disabled; /* save previous state */
464 ctrlc_disabled = disable;
470 return ctrlc_was_pressed;
473 void clear_ctrlc(void)
475 ctrlc_was_pressed = 0;
478 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
480 char *cursor = screen;
482 inline void dbg(const char *fmt, ...)
486 char printbuffer[CONFIG_SYS_PBSIZE];
489 memset(screen, 0, sizeof(screen));
495 /* For this to work, printbuffer must be larger than
496 * anything we ever want to print.
498 i = vsprintf(printbuffer, fmt, args);
501 if ((screen + sizeof(screen) - 1 - cursor)
502 < strlen(printbuffer) + 1) {
503 memset(screen, 0, sizeof(screen));
506 sprintf(cursor, printbuffer);
507 cursor += strlen(printbuffer);
511 inline void dbg(const char *fmt, ...)
516 /** U-Boot INIT FUNCTIONS *************************************************/
518 struct stdio_dev *search_device(int flags, char *name)
520 struct stdio_dev *dev;
522 dev = stdio_get_by_name(name);
524 if (dev && (dev->flags & flags))
530 int console_assign(int file, char *devname)
533 struct stdio_dev *dev;
535 /* Check for valid file */
538 flag = DEV_FLAGS_INPUT;
542 flag = DEV_FLAGS_OUTPUT;
548 /* Check for valid device name */
550 dev = search_device(flag, devname);
553 return console_setfile(file, dev);
558 /* Called before relocation - use serial functions */
559 int console_init_f(void)
561 gd->have_console = 1;
563 #ifdef CONFIG_SILENT_CONSOLE
564 if (getenv("silent") != NULL)
565 gd->flags |= GD_FLG_SILENT;
571 void stdio_print_current_devices(void)
573 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
574 /* Print information */
576 if (stdio_devices[stdin] == NULL) {
577 puts("No input devices available!\n");
579 printf ("%s\n", stdio_devices[stdin]->name);
583 if (stdio_devices[stdout] == NULL) {
584 puts("No output devices available!\n");
586 printf ("%s\n", stdio_devices[stdout]->name);
590 if (stdio_devices[stderr] == NULL) {
591 puts("No error devices available!\n");
593 printf ("%s\n", stdio_devices[stderr]->name);
595 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
598 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
599 /* Called after the relocation - use desired console functions */
600 int console_init_r(void)
602 char *stdinname, *stdoutname, *stderrname;
603 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
604 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
606 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
607 #ifdef CONFIG_CONSOLE_MUX
611 /* set default handlers at first */
612 gd->jt[XF_getc] = serial_getc;
613 gd->jt[XF_tstc] = serial_tstc;
614 gd->jt[XF_putc] = serial_putc;
615 gd->jt[XF_puts] = serial_puts;
616 gd->jt[XF_printf] = serial_printf;
618 /* stdin stdout and stderr are in environment */
620 stdinname = getenv("stdin");
621 stdoutname = getenv("stdout");
622 stderrname = getenv("stderr");
624 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
625 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
626 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
627 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
628 #ifdef CONFIG_CONSOLE_MUX
629 iomux_err = iomux_doenv(stdin, stdinname);
630 iomux_err += iomux_doenv(stdout, stdoutname);
631 iomux_err += iomux_doenv(stderr, stderrname);
633 /* Successful, so skip all the code below. */
637 /* if the devices are overwritten or not found, use default device */
638 if (inputdev == NULL) {
639 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
641 if (outputdev == NULL) {
642 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
644 if (errdev == NULL) {
645 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
647 /* Initializes output console first */
648 if (outputdev != NULL) {
649 /* need to set a console if not done above. */
650 console_doenv(stdout, outputdev);
652 if (errdev != NULL) {
653 /* need to set a console if not done above. */
654 console_doenv(stderr, errdev);
656 if (inputdev != NULL) {
657 /* need to set a console if not done above. */
658 console_doenv(stdin, inputdev);
661 #ifdef CONFIG_CONSOLE_MUX
665 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
667 stdio_print_current_devices();
669 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
670 /* set the environment variables (will overwrite previous env settings) */
671 for (i = 0; i < 3; i++) {
672 setenv(stdio_names[i], stdio_devices[i]->name);
674 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
677 /* If nothing usable installed, use only the initial console */
678 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
684 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
686 /* Called after the relocation - use desired console functions */
687 int console_init_r(void)
689 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
691 struct list_head *list = stdio_get_list();
692 struct list_head *pos;
693 struct stdio_dev *dev;
695 #ifdef CONFIG_SPLASH_SCREEN
697 * suppress all output if splash screen is enabled and we have
698 * a bmp to display. We redirect the output from frame buffer
699 * console to serial console in this case or suppress it if
700 * "silent" mode was requested.
702 if (getenv("splashimage") != NULL) {
703 if (!(gd->flags & GD_FLG_SILENT))
704 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
708 /* Scan devices looking for input and output devices */
709 list_for_each(pos, list) {
710 dev = list_entry(pos, struct stdio_dev, list);
712 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
715 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
718 if(inputdev && outputdev)
722 /* Initializes output console first */
723 if (outputdev != NULL) {
724 console_setfile(stdout, outputdev);
725 console_setfile(stderr, outputdev);
726 #ifdef CONFIG_CONSOLE_MUX
727 console_devices[stdout][0] = outputdev;
728 console_devices[stderr][0] = outputdev;
732 /* Initializes input console */
733 if (inputdev != NULL) {
734 console_setfile(stdin, inputdev);
735 #ifdef CONFIG_CONSOLE_MUX
736 console_devices[stdin][0] = inputdev;
740 #if !(defined(CONFIG_SC8830) || defined(CONFIG_SC9630) )
742 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
745 stdio_print_current_devices();
747 /* Setting environment variables */
748 for (i = 0; i < 3; i++) {
749 setenv(stdio_names[i], stdio_devices[i]->name);
753 /* If nothing usable installed, use only the initial console */
754 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
761 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */