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,
30 DECLARE_GLOBAL_DATA_PTR;
32 #ifdef CONFIG_AMIGAONEG3SE
33 int console_changed = 0;
36 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
38 * if overwrite_console returns 1, the stdin, stderr and stdout
39 * are switched to the serial port, else the settings in the
40 * environment are used
42 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
43 extern int overwrite_console(void);
44 #define OVERWRITE_CONSOLE overwrite_console()
46 #define OVERWRITE_CONSOLE 0
47 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
49 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
51 static int console_setfile(int file, device_t * dev)
62 /* Start new device */
65 /* If it's not started dont use it */
70 /* Assign the new device (leaving the existing one started) */
71 stdio_devices[file] = dev;
74 * Update monitor functions
75 * (to use the console stuff by other applications)
79 gd->jt[XF_getc] = dev->getc;
80 gd->jt[XF_tstc] = dev->tstc;
83 gd->jt[XF_putc] = dev->putc;
84 gd->jt[XF_puts] = dev->puts;
85 gd->jt[XF_printf] = printf;
90 default: /* Invalid file ID */
96 #if defined(CONFIG_CONSOLE_MUX)
97 /** Console I/O multiplexing *******************************************/
99 static device_t *tstcdev;
100 device_t **console_devices[MAX_FILES];
101 int cd_count[MAX_FILES];
104 * This depends on tstc() always being called before getc().
105 * This is guaranteed to be true because this routine is called
106 * only from fgetc() which assures it.
107 * No attempt is made to demultiplex multiple input sources.
109 static int console_getc(int file)
113 /* This is never called with testcdev == NULL */
114 ret = tstcdev->getc();
119 static int console_tstc(int file)
125 for (i = 0; i < cd_count[file]; i++) {
126 dev = console_devices[file][i];
127 if (dev->tstc != NULL) {
141 static void console_putc(int file, const char c)
146 for (i = 0; i < cd_count[file]; i++) {
147 dev = console_devices[file][i];
148 if (dev->putc != NULL)
153 static void console_puts(int file, const char *s)
158 for (i = 0; i < cd_count[file]; i++) {
159 dev = console_devices[file][i];
160 if (dev->puts != NULL)
165 static inline void console_printdevs(int file)
167 iomux_printdevs(file);
170 static inline void console_doenv(int file, device_t *dev)
172 iomux_doenv(file, dev->name);
175 static inline int console_getc(int file)
177 return stdio_devices[file]->getc();
180 static inline int console_tstc(int file)
182 return stdio_devices[file]->tstc();
185 static inline void console_putc(int file, const char c)
187 stdio_devices[file]->putc(c);
190 static inline void console_puts(int file, const char *s)
192 stdio_devices[file]->puts(s);
195 static inline void console_printdevs(int file)
197 printf("%s\n", stdio_devices[file]->name);
200 static inline void console_doenv(int file, device_t *dev)
202 console_setfile(file, dev);
204 #endif /* defined(CONFIG_CONSOLE_MUX) */
206 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
208 void serial_printf(const char *fmt, ...)
212 char printbuffer[CONFIG_SYS_PBSIZE];
216 /* For this to work, printbuffer must be larger than
217 * anything we ever want to print.
219 i = vsprintf(printbuffer, fmt, args);
222 serial_puts(printbuffer);
227 if (file < MAX_FILES) {
228 #if defined(CONFIG_CONSOLE_MUX)
230 * Effectively poll for input wherever it may be available.
234 * Upper layer may have already called tstc() so
235 * check for that first.
238 return console_getc(file);
240 #ifdef CONFIG_WATCHDOG
242 * If the watchdog must be rate-limited then it should
243 * already be handled in board-specific code.
249 return console_getc(file);
258 if (file < MAX_FILES)
259 return console_tstc(file);
264 void fputc(int file, const char c)
266 if (file < MAX_FILES)
267 console_putc(file, c);
270 void fputs(int file, const char *s)
272 if (file < MAX_FILES)
273 console_puts(file, s);
276 void fprintf(int file, const char *fmt, ...)
280 char printbuffer[CONFIG_SYS_PBSIZE];
284 /* For this to work, printbuffer must be larger than
285 * anything we ever want to print.
287 i = vsprintf(printbuffer, fmt, args);
290 /* Send to desired file */
291 fputs(file, printbuffer);
294 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
298 #ifdef CONFIG_DISABLE_CONSOLE
299 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
303 if (gd->flags & GD_FLG_DEVINIT) {
304 /* Get from the standard input */
308 /* Send directly to the handler */
309 return serial_getc();
314 #ifdef CONFIG_DISABLE_CONSOLE
315 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
319 if (gd->flags & GD_FLG_DEVINIT) {
320 /* Test the standard input */
324 /* Send directly to the handler */
325 return serial_tstc();
328 void putc(const char c)
330 #ifdef CONFIG_SILENT_CONSOLE
331 if (gd->flags & GD_FLG_SILENT)
335 #ifdef CONFIG_DISABLE_CONSOLE
336 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
340 if (gd->flags & GD_FLG_DEVINIT) {
341 /* Send to the standard output */
344 /* Send directly to the handler */
349 void puts(const char *s)
351 #ifdef CONFIG_SILENT_CONSOLE
352 if (gd->flags & GD_FLG_SILENT)
356 #ifdef CONFIG_DISABLE_CONSOLE
357 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
361 if (gd->flags & GD_FLG_DEVINIT) {
362 /* Send to the standard output */
365 /* Send directly to the handler */
370 void printf(const char *fmt, ...)
374 char printbuffer[CONFIG_SYS_PBSIZE];
378 /* For this to work, printbuffer must be larger than
379 * anything we ever want to print.
381 i = vsprintf(printbuffer, fmt, args);
384 /* Print the string */
388 void vprintf(const char *fmt, va_list args)
391 char printbuffer[CONFIG_SYS_PBSIZE];
393 /* For this to work, printbuffer must be larger than
394 * anything we ever want to print.
396 i = vsprintf(printbuffer, fmt, args);
398 /* 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 device_t *search_device(int flags, char *name)
486 dev = device_get_by_name(name);
488 if (dev && (dev->flags & flags))
494 int console_assign(int file, char *devname)
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 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
536 /* Called after the relocation - use desired console functions */
537 int console_init_r(void)
539 char *stdinname, *stdoutname, *stderrname;
540 device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
541 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
543 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
544 #ifdef CONFIG_CONSOLE_MUX
548 /* set default handlers at first */
549 gd->jt[XF_getc] = serial_getc;
550 gd->jt[XF_tstc] = serial_tstc;
551 gd->jt[XF_putc] = serial_putc;
552 gd->jt[XF_puts] = serial_puts;
553 gd->jt[XF_printf] = serial_printf;
555 /* stdin stdout and stderr are in environment */
557 stdinname = getenv("stdin");
558 stdoutname = getenv("stdout");
559 stderrname = getenv("stderr");
561 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
562 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
563 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
564 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
565 #ifdef CONFIG_CONSOLE_MUX
566 iomux_err = iomux_doenv(stdin, stdinname);
567 iomux_err += iomux_doenv(stdout, stdoutname);
568 iomux_err += iomux_doenv(stderr, stderrname);
570 /* Successful, so skip all the code below. */
574 /* if the devices are overwritten or not found, use default device */
575 if (inputdev == NULL) {
576 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
578 if (outputdev == NULL) {
579 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
581 if (errdev == NULL) {
582 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
584 /* Initializes output console first */
585 if (outputdev != NULL) {
586 /* need to set a console if not done above. */
587 console_doenv(stdout, outputdev);
589 if (errdev != NULL) {
590 /* need to set a console if not done above. */
591 console_doenv(stderr, errdev);
593 if (inputdev != NULL) {
594 /* need to set a console if not done above. */
595 console_doenv(stdin, inputdev);
598 #ifdef CONFIG_CONSOLE_MUX
602 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
604 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
605 /* Print information */
607 if (stdio_devices[stdin] == NULL) {
608 puts("No input devices available!\n");
610 console_printdevs(stdin);
614 if (stdio_devices[stdout] == NULL) {
615 puts("No output devices available!\n");
617 console_printdevs(stdout);
621 if (stdio_devices[stderr] == NULL) {
622 puts("No error devices available!\n");
624 console_printdevs(stderr);
626 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
628 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
629 /* set the environment variables (will overwrite previous env settings) */
630 for (i = 0; i < 3; i++) {
631 setenv(stdio_names[i], stdio_devices[i]->name);
633 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
636 /* If nothing usable installed, use only the initial console */
637 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
643 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
645 /* Called after the relocation - use desired console functions */
646 int console_init_r(void)
648 device_t *inputdev = NULL, *outputdev = NULL;
650 struct list_head *list = device_get_list();
651 struct list_head *pos;
654 #ifdef CONFIG_SPLASH_SCREEN
656 * suppress all output if splash screen is enabled and we have
659 if (getenv("splashimage") != NULL)
660 gd->flags |= GD_FLG_SILENT;
663 /* Scan devices looking for input and output devices */
664 list_for_each(pos, list) {
665 dev = list_entry(pos, device_t, list);
667 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
670 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
673 if(inputdev && outputdev)
677 /* Initializes output console first */
678 if (outputdev != NULL) {
679 console_setfile(stdout, outputdev);
680 console_setfile(stderr, outputdev);
681 #ifdef CONFIG_CONSOLE_MUX
682 console_devices[stdout][0] = outputdev;
683 console_devices[stderr][0] = outputdev;
687 /* Initializes input console */
688 if (inputdev != NULL) {
689 console_setfile(stdin, inputdev);
690 #ifdef CONFIG_CONSOLE_MUX
691 console_devices[stdin][0] = inputdev;
695 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
697 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
698 /* Print information */
700 if (stdio_devices[stdin] == NULL) {
701 puts("No input devices available!\n");
703 printf("%s\n", stdio_devices[stdin]->name);
707 if (stdio_devices[stdout] == NULL) {
708 puts("No output devices available!\n");
710 printf("%s\n", stdio_devices[stdout]->name);
714 if (stdio_devices[stderr] == NULL) {
715 puts("No error devices available!\n");
717 printf("%s\n", stdio_devices[stderr]->name);
719 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
721 /* Setting environment variables */
722 for (i = 0; i < 3; i++) {
723 setenv(stdio_names[i], stdio_devices[i]->name);
727 /* If nothing usable installed, use only the initial console */
728 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
735 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */