xref: /rk3399_rockchip-uboot/common/console.c (revision fb8094783a48d6c50f8d326441ccf82e38d49f43)
1 /*
2  * (C) Copyright 2000
3  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <console.h>
10 #include <debug_uart.h>
11 #include <dm.h>
12 #include <stdarg.h>
13 #include <iomux.h>
14 #include <malloc.h>
15 #include <mapmem.h>
16 #include <os.h>
17 #include <serial.h>
18 #include <stdio_dev.h>
19 #include <exports.h>
20 #include <environment.h>
21 #include <watchdog.h>
22 #include <vsprintf.h>
23 
24 #ifdef CONFIG_PSTORE
25 #include <asm/arch/pstore.h>
26 #endif
27 
28 DECLARE_GLOBAL_DATA_PTR;
29 
30 static int on_console(const char *name, const char *value, enum env_op op,
31 	int flags)
32 {
33 	int console = -1;
34 
35 	/* Check for console redirection */
36 	if (strcmp(name, "stdin") == 0)
37 		console = stdin;
38 	else if (strcmp(name, "stdout") == 0)
39 		console = stdout;
40 	else if (strcmp(name, "stderr") == 0)
41 		console = stderr;
42 
43 	/* if not actually setting a console variable, we don't care */
44 	if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
45 		return 0;
46 
47 	switch (op) {
48 	case env_op_create:
49 	case env_op_overwrite:
50 
51 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
52 		if (iomux_doenv(console, value))
53 			return 1;
54 #else
55 		/* Try assigning specified device */
56 		if (console_assign(console, value) < 0)
57 			return 1;
58 #endif
59 		return 0;
60 
61 	case env_op_delete:
62 		if ((flags & H_FORCE) == 0)
63 			printf("Can't delete \"%s\"\n", name);
64 		return 1;
65 
66 	default:
67 		return 0;
68 	}
69 }
70 U_BOOT_ENV_CALLBACK(console, on_console);
71 
72 #ifdef CONFIG_SILENT_CONSOLE
73 static int on_silent(const char *name, const char *value, enum env_op op,
74 	int flags)
75 {
76 #if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET)
77 	if (flags & H_INTERACTIVE)
78 		return 0;
79 #endif
80 #if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC)
81 	if ((flags & H_INTERACTIVE) == 0)
82 		return 0;
83 #endif
84 
85 	if (value != NULL)
86 		gd->flags |= GD_FLG_SILENT;
87 	else
88 		gd->flags &= ~GD_FLG_SILENT;
89 
90 	return 0;
91 }
92 U_BOOT_ENV_CALLBACK(silent, on_silent);
93 #endif
94 
95 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
96 /*
97  * if overwrite_console returns 1, the stdin, stderr and stdout
98  * are switched to the serial port, else the settings in the
99  * environment are used
100  */
101 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
102 extern int overwrite_console(void);
103 #define OVERWRITE_CONSOLE overwrite_console()
104 #else
105 #define OVERWRITE_CONSOLE 0
106 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
107 
108 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
109 
110 static int console_setfile(int file, struct stdio_dev * dev)
111 {
112 	int error = 0;
113 
114 	if (dev == NULL)
115 		return -1;
116 
117 	switch (file) {
118 	case stdin:
119 	case stdout:
120 	case stderr:
121 		/* Start new device */
122 		if (dev->start) {
123 			error = dev->start(dev);
124 			/* If it's not started dont use it */
125 			if (error < 0)
126 				break;
127 		}
128 
129 		/* Assign the new device (leaving the existing one started) */
130 		stdio_devices[file] = dev;
131 
132 		/*
133 		 * Update monitor functions
134 		 * (to use the console stuff by other applications)
135 		 */
136 		switch (file) {
137 		case stdin:
138 			gd->jt->getc = getc;
139 			gd->jt->tstc = tstc;
140 			break;
141 		case stdout:
142 			gd->jt->putc  = putc;
143 			gd->jt->puts  = puts;
144 			gd->jt->printf = printf;
145 			break;
146 		}
147 		break;
148 
149 	default:		/* Invalid file ID */
150 		error = -1;
151 	}
152 	return error;
153 }
154 
155 /**
156  * console_dev_is_serial() - Check if a stdio device is a serial device
157  *
158  * @sdev: Device to check
159  * @return true if this device is in the serial uclass (or for pre-driver-model,
160  * whether it is called "serial".
161  */
162 static bool console_dev_is_serial(struct stdio_dev *sdev)
163 {
164 	bool is_serial;
165 
166 #ifdef CONFIG_DM_SERIAL
167 	if (sdev->flags & DEV_FLAGS_DM) {
168 		struct udevice *dev = sdev->priv;
169 
170 		is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
171 	} else
172 #endif
173 	is_serial = !strcmp(sdev->name, "serial");
174 
175 	return is_serial;
176 }
177 
178 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
179 /** Console I/O multiplexing *******************************************/
180 
181 static struct stdio_dev *tstcdev;
182 struct stdio_dev **console_devices[MAX_FILES];
183 int cd_count[MAX_FILES];
184 
185 /*
186  * This depends on tstc() always being called before getc().
187  * This is guaranteed to be true because this routine is called
188  * only from fgetc() which assures it.
189  * No attempt is made to demultiplex multiple input sources.
190  */
191 static int console_getc(int file)
192 {
193 	unsigned char ret;
194 
195 	/* This is never called with testcdev == NULL */
196 	ret = tstcdev->getc(tstcdev);
197 	tstcdev = NULL;
198 	return ret;
199 }
200 
201 static int console_tstc(int file)
202 {
203 	int i, ret;
204 	struct stdio_dev *dev;
205 
206 	disable_ctrlc(1);
207 	for (i = 0; i < cd_count[file]; i++) {
208 		dev = console_devices[file][i];
209 		if (dev->tstc != NULL) {
210 			ret = dev->tstc(dev);
211 			if (ret > 0) {
212 				tstcdev = dev;
213 				disable_ctrlc(0);
214 				return ret;
215 			}
216 		}
217 	}
218 	disable_ctrlc(0);
219 
220 	return 0;
221 }
222 
223 static void console_putc(int file, const char c)
224 {
225 	int i;
226 	struct stdio_dev *dev;
227 
228 	for (i = 0; i < cd_count[file]; i++) {
229 		dev = console_devices[file][i];
230 		if (dev->putc != NULL)
231 			dev->putc(dev, c);
232 	}
233 }
234 
235 static void console_puts_noserial(int file, const char *s)
236 {
237 	int i;
238 	struct stdio_dev *dev;
239 
240 	for (i = 0; i < cd_count[file]; i++) {
241 		dev = console_devices[file][i];
242 		if (dev->puts != NULL && !console_dev_is_serial(dev))
243 			dev->puts(dev, s);
244 	}
245 }
246 
247 static void console_puts(int file, const char *s)
248 {
249 	int i;
250 	struct stdio_dev *dev;
251 
252 	for (i = 0; i < cd_count[file]; i++) {
253 		dev = console_devices[file][i];
254 		if (dev->puts != NULL)
255 			dev->puts(dev, s);
256 	}
257 }
258 
259 static inline void console_doenv(int file, struct stdio_dev *dev)
260 {
261 	iomux_doenv(file, dev->name);
262 }
263 
264 static void console_clear(int file)
265 {
266 	int i;
267 	struct stdio_dev *dev;
268 
269 	for (i = 0; i < cd_count[file]; i++) {
270 		dev = console_devices[file][i];
271 		if (dev->clear != NULL)
272 			dev->clear(dev);
273 	}
274 }
275 #else
276 static inline int console_getc(int file)
277 {
278 	return stdio_devices[file]->getc(stdio_devices[file]);
279 }
280 
281 static inline int console_tstc(int file)
282 {
283 	return stdio_devices[file]->tstc(stdio_devices[file]);
284 }
285 
286 static inline void console_putc(int file, const char c)
287 {
288 	stdio_devices[file]->putc(stdio_devices[file], c);
289 }
290 
291 static inline void console_puts_noserial(int file, const char *s)
292 {
293 	if (!console_dev_is_serial(stdio_devices[file]))
294 		stdio_devices[file]->puts(stdio_devices[file], s);
295 }
296 
297 static inline void console_puts(int file, const char *s)
298 {
299 	stdio_devices[file]->puts(stdio_devices[file], s);
300 }
301 
302 static inline void console_clear(int file)
303 {
304 	if (stdio_devices[file]->clear)
305 		stdio_devices[file]->clear(stdio_devices[file]);
306 }
307 
308 static inline void console_doenv(int file, struct stdio_dev *dev)
309 {
310 	console_setfile(file, dev);
311 }
312 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
313 
314 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
315 
316 int serial_printf(const char *fmt, ...)
317 {
318 	va_list args;
319 	uint i;
320 	char printbuffer[CONFIG_SYS_PBSIZE];
321 
322 	va_start(args, fmt);
323 
324 	/* For this to work, printbuffer must be larger than
325 	 * anything we ever want to print.
326 	 */
327 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
328 	va_end(args);
329 
330 	serial_puts(printbuffer);
331 	return i;
332 }
333 
334 int fgetc(int file)
335 {
336 	if (file < MAX_FILES) {
337 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
338 		/*
339 		 * Effectively poll for input wherever it may be available.
340 		 */
341 		for (;;) {
342 			WATCHDOG_RESET();
343 			/*
344 			 * Upper layer may have already called tstc() so
345 			 * check for that first.
346 			 */
347 			if (tstcdev != NULL)
348 				return console_getc(file);
349 			console_tstc(file);
350 #ifdef CONFIG_WATCHDOG
351 			/*
352 			 * If the watchdog must be rate-limited then it should
353 			 * already be handled in board-specific code.
354 			 */
355 			 udelay(1);
356 #endif
357 		}
358 #else
359 		return console_getc(file);
360 #endif
361 	}
362 
363 	return -1;
364 }
365 
366 int ftstc(int file)
367 {
368 	if (file < MAX_FILES)
369 		return console_tstc(file);
370 
371 	return -1;
372 }
373 
374 void fputc(int file, const char c)
375 {
376 	if (file < MAX_FILES)
377 		console_putc(file, c);
378 }
379 
380 void fputs(int file, const char *s)
381 {
382 	if (file < MAX_FILES)
383 		console_puts(file, s);
384 }
385 
386 void fclear(int file)
387 {
388 	if (file < MAX_FILES)
389 		console_clear(file);
390 }
391 
392 int fprintf(int file, const char *fmt, ...)
393 {
394 	va_list args;
395 	uint i;
396 	char printbuffer[CONFIG_SYS_PBSIZE];
397 
398 	va_start(args, fmt);
399 
400 	/* For this to work, printbuffer must be larger than
401 	 * anything we ever want to print.
402 	 */
403 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
404 	va_end(args);
405 
406 	/* Send to desired file */
407 	fputs(file, printbuffer);
408 	return i;
409 }
410 
411 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
412 
413 int getc(void)
414 {
415 	if (!gd || gd->flags & GD_FLG_DISABLE_CONSOLE)
416 		return 0;
417 
418 	if (!gd->have_console)
419 		return 0;
420 
421 #ifdef CONFIG_CONSOLE_RECORD
422 	if (gd->console_in.start) {
423 		int ch;
424 
425 		ch = membuff_getbyte((struct membuff *)&gd->console_in);
426 		if (ch != -1)
427 			return 1;
428 	}
429 #endif
430 	if (gd->flags & GD_FLG_DEVINIT) {
431 		/* Get from the standard input */
432 		return fgetc(stdin);
433 	}
434 
435 	/* Send directly to the handler */
436 	return serial_getc();
437 }
438 
439 int tstc(void)
440 {
441 	if (!gd || gd->flags & GD_FLG_DISABLE_CONSOLE)
442 		return 0;
443 
444 	if (!gd->have_console)
445 		return 0;
446 #ifdef CONFIG_CONSOLE_RECORD
447 	if (gd->console_in.start) {
448 		if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
449 			return 1;
450 	}
451 #endif
452 	if (gd->flags & GD_FLG_DEVINIT) {
453 		/* Test the standard input */
454 		return ftstc(stdin);
455 	}
456 
457 	/* Send directly to the handler */
458 	return serial_tstc();
459 }
460 
461 void flushc(void)
462 {
463 	if (!gd || gd->flags & GD_FLG_DISABLE_CONSOLE)
464 		return;
465 
466 #ifdef CONFIG_DEBUG_UART_NS16550
467 	debug_uart_flushc();
468 #endif
469 }
470 
471 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL			0
472 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL	1
473 
474 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
475 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
476 
477 static void pre_console_putc(const char c)
478 {
479 	char *buffer;
480 
481 	buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
482 
483 	buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
484 
485 	unmap_sysmem(buffer);
486 }
487 
488 static void print_pre_console_buffer(int flushpoint)
489 {
490 	unsigned long in = 0, out = 0;
491 	char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
492 	char *buf_in;
493 
494 	buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
495 	if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
496 		in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
497 
498 	while (in < gd->precon_buf_idx)
499 		buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
500 	unmap_sysmem(buf_in);
501 
502 	buf_out[out] = 0;
503 
504 	switch (flushpoint) {
505 	case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
506 		puts(buf_out);
507 		break;
508 	case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
509 		console_puts_noserial(stdout, buf_out);
510 		break;
511 	}
512 }
513 #else
514 static inline void pre_console_putc(const char c) {}
515 static inline void print_pre_console_buffer(int flushpoint) {}
516 #endif
517 
518 void putc(const char c)
519 {
520 	if (!gd || gd->flags & GD_FLG_DISABLE_CONSOLE)
521 		return;
522 
523 #ifdef CONFIG_PSTORE
524 	putc_to_ram(c);
525 #endif
526 
527 #ifdef CONFIG_CONSOLE_RECORD
528 	if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
529 		membuff_putbyte((struct membuff *)&gd->console_out, c);
530 #endif
531 #ifdef CONFIG_SILENT_CONSOLE
532 	if (gd->flags & GD_FLG_SILENT)
533 		return;
534 #endif
535 
536 #ifdef CONFIG_DEBUG_UART
537 	/* if we don't have a console yet, use the debug UART */
538 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
539 		printch(c);
540 		return;
541 	}
542 #endif
543 	if (!gd->have_console)
544 		return pre_console_putc(c);
545 
546 	if (gd->flags & GD_FLG_DEVINIT) {
547 		/* Send to the standard output */
548 		fputc(stdout, c);
549 	} else {
550 		/* Send directly to the handler */
551 		pre_console_putc(c);
552 		serial_putc(c);
553 	}
554 }
555 
556 #if ((!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)) && \
557 	defined(CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP))
558 static void vspfunc(char *buf, size_t size, char *format, ...)
559 {
560 	va_list ap;
561 
562 	va_start(ap, format);
563 	vsnprintf(buf, size, format, ap);
564 	va_end(ap);
565 }
566 
567 void puts(const char *s)
568 {
569 	unsigned long ts_sec, ts_msec, us, delta_ms;
570 	char pr_timestamp[32], *p;
571 	int cpu;
572 
573 	while (*s) {
574 		if (*s == '\n') {
575 			gd->new_line = 1;
576 			putc(*s++);
577 			continue;
578 		}
579 
580 		if (gd->new_line) {
581 			us = (get_ticks() / (gd->arch.timer_rate_hz / 1000000));
582 			if (gd->last_us)
583 				delta_ms = DIV_ROUND_UP(us - gd->last_us, 1000);
584 			else
585 				delta_ms = 0;
586 			gd->last_us = us;
587 			gd->new_line = 0;
588 
589 			ts_sec = us / 1000000;
590 			ts_msec = us % 1000000;
591 			cpu = read_mpidr() & 0xfff;
592 			vspfunc(pr_timestamp, sizeof(pr_timestamp),
593 				"[%5lu.%06lu:%x:%03d] ", ts_sec, ts_msec, cpu, delta_ms);
594 			p = pr_timestamp;
595 			while (*p)
596 				putc(*p++);
597 		}
598 
599 		putc(*s++);
600 	}
601 }
602 #else
603 void puts(const char *s)
604 {
605 	while (*s)
606 		putc(*s++);
607 }
608 #endif
609 
610 
611 #ifdef CONFIG_CONSOLE_RECORD
612 int console_record_init(void)
613 {
614 	int ret;
615 
616 	ret = membuff_new((struct membuff *)&gd->console_out,
617 			  CONFIG_CONSOLE_RECORD_OUT_SIZE);
618 	if (ret)
619 		return ret;
620 	ret = membuff_new((struct membuff *)&gd->console_in,
621 			  CONFIG_CONSOLE_RECORD_IN_SIZE);
622 
623 	return ret;
624 }
625 
626 void console_record_reset(void)
627 {
628 	membuff_purge((struct membuff *)&gd->console_out);
629 	membuff_purge((struct membuff *)&gd->console_in);
630 }
631 
632 void console_record_reset_enable(void)
633 {
634 	console_record_reset();
635 	gd->flags |= GD_FLG_RECORD;
636 }
637 
638 /* Print and remove data from buffer */
639 void console_record_print_purge(void)
640 {
641 	unsigned long flags;
642 	char c;
643 
644 	if (!gd || !(gd->flags & GD_FLG_RECORD))
645 		return;
646 
647 	/* Remove some bits to avoid running unexpected branch in putc() */
648 	flags = gd->flags;
649 	gd->flags &= ~(GD_FLG_RECORD | GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE);
650 
651 	printf("\n\n## Console Record: \n");
652 	while (!membuff_isempty((struct membuff *)&gd->console_out)) {
653 		c = membuff_getbyte((struct membuff *)&gd->console_out);
654 		putc(c);
655 	}
656 
657 	gd->flags = flags;
658 }
659 #endif
660 
661 /* test if ctrl-c was pressed */
662 static int ctrlc_disabled = 0;	/* see disable_ctrl() */
663 static int ctrlc_was_pressed = 0;
664 int ctrlc(void)
665 {
666 #ifndef CONFIG_SANDBOX
667 	if (!ctrlc_disabled && gd->have_console) {
668 		if (tstc()) {
669 			switch (getc()) {
670 			case 0x03:		/* ^C - Control C */
671 				ctrlc_was_pressed = 1;
672 				return 1;
673 			default:
674 				break;
675 			}
676 		}
677 	}
678 #endif
679 
680 	return 0;
681 }
682 /* Reads user's confirmation.
683    Returns 1 if user's input is "y", "Y", "yes" or "YES"
684 */
685 int confirm_yesno(void)
686 {
687 	int i;
688 	char str_input[5];
689 
690 	/* Flush input */
691 	while (tstc())
692 		getc();
693 	i = 0;
694 	while (i < sizeof(str_input)) {
695 		str_input[i] = getc();
696 		putc(str_input[i]);
697 		if (str_input[i] == '\r')
698 			break;
699 		i++;
700 	}
701 	putc('\n');
702 	if (strncmp(str_input, "y\r", 2) == 0 ||
703 	    strncmp(str_input, "Y\r", 2) == 0 ||
704 	    strncmp(str_input, "yes\r", 4) == 0 ||
705 	    strncmp(str_input, "YES\r", 4) == 0)
706 		return 1;
707 	return 0;
708 }
709 /* pass 1 to disable ctrlc() checking, 0 to enable.
710  * returns previous state
711  */
712 int disable_ctrlc(int disable)
713 {
714 	int prev = ctrlc_disabled;	/* save previous state */
715 
716 	ctrlc_disabled = disable;
717 	return prev;
718 }
719 
720 int had_ctrlc (void)
721 {
722 	return ctrlc_was_pressed;
723 }
724 
725 void clear_ctrlc(void)
726 {
727 	ctrlc_was_pressed = 0;
728 }
729 
730 /** U-Boot INIT FUNCTIONS *************************************************/
731 
732 struct stdio_dev *search_device(int flags, const char *name)
733 {
734 	struct stdio_dev *dev;
735 
736 	dev = stdio_get_by_name(name);
737 #ifdef CONFIG_VIDCONSOLE_AS_LCD
738 	if (!dev && !strcmp(name, "lcd"))
739 		dev = stdio_get_by_name("vidconsole");
740 #endif
741 
742 	if (dev && (dev->flags & flags))
743 		return dev;
744 
745 	return NULL;
746 }
747 
748 int console_assign(int file, const char *devname)
749 {
750 	int flag;
751 	struct stdio_dev *dev;
752 
753 	/* Check for valid file */
754 	switch (file) {
755 	case stdin:
756 		flag = DEV_FLAGS_INPUT;
757 		break;
758 	case stdout:
759 	case stderr:
760 		flag = DEV_FLAGS_OUTPUT;
761 		break;
762 	default:
763 		return -1;
764 	}
765 
766 	/* Check for valid device name */
767 
768 	dev = search_device(flag, devname);
769 
770 	if (dev)
771 		return console_setfile(file, dev);
772 
773 	return -1;
774 }
775 
776 static void console_update_silent(void)
777 {
778 #ifdef CONFIG_SILENT_CONSOLE
779 	if (env_get("silent") != NULL) {
780 		printf("U-Boot: enable silent console\n");
781 		gd->flags |= GD_FLG_SILENT;
782 	} else {
783 		gd->flags &= ~GD_FLG_SILENT;
784 	}
785 #endif
786 }
787 
788 int console_announce_r(void)
789 {
790 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
791 	char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
792 
793 	display_options_get_banner(false, buf, sizeof(buf));
794 
795 	console_puts_noserial(stdout, buf);
796 #endif
797 
798 	return 0;
799 }
800 
801 /* Called before relocation - use serial functions */
802 int console_init_f(void)
803 {
804 	gd->have_console = 1;
805 
806 	console_update_silent();
807 
808 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
809 
810 	return 0;
811 }
812 
813 void stdio_print_current_devices(void)
814 {
815 	/* Print information */
816 	puts("In:    ");
817 	if (stdio_devices[stdin] == NULL) {
818 		puts("No input devices available!\n");
819 	} else {
820 		printf ("%s\n", stdio_devices[stdin]->name);
821 	}
822 
823 	puts("Out:   ");
824 	if (stdio_devices[stdout] == NULL) {
825 		puts("No output devices available!\n");
826 	} else {
827 		printf ("%s\n", stdio_devices[stdout]->name);
828 	}
829 
830 	puts("Err:   ");
831 	if (stdio_devices[stderr] == NULL) {
832 		puts("No error devices available!\n");
833 	} else {
834 		printf ("%s\n", stdio_devices[stderr]->name);
835 	}
836 }
837 
838 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
839 /* Called after the relocation - use desired console functions */
840 int console_init_r(void)
841 {
842 	char *stdinname, *stdoutname, *stderrname;
843 	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
844 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
845 	int i;
846 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
847 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
848 	int iomux_err = 0;
849 #endif
850 
851 	/* set default handlers at first */
852 	gd->jt->getc  = serial_getc;
853 	gd->jt->tstc  = serial_tstc;
854 	gd->jt->putc  = serial_putc;
855 	gd->jt->puts  = serial_puts;
856 	gd->jt->printf = serial_printf;
857 
858 	/* stdin stdout and stderr are in environment */
859 	/* scan for it */
860 	stdinname  = env_get("stdin");
861 	stdoutname = env_get("stdout");
862 	stderrname = env_get("stderr");
863 
864 	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
865 		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
866 		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
867 		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
868 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
869 		iomux_err = iomux_doenv(stdin, stdinname);
870 		iomux_err += iomux_doenv(stdout, stdoutname);
871 		iomux_err += iomux_doenv(stderr, stderrname);
872 		if (!iomux_err)
873 			/* Successful, so skip all the code below. */
874 			goto done;
875 #endif
876 	}
877 	/* if the devices are overwritten or not found, use default device */
878 	if (inputdev == NULL) {
879 		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
880 	}
881 	if (outputdev == NULL) {
882 		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
883 	}
884 	if (errdev == NULL) {
885 		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
886 	}
887 	/* Initializes output console first */
888 	if (outputdev != NULL) {
889 		/* need to set a console if not done above. */
890 		console_doenv(stdout, outputdev);
891 	}
892 	if (errdev != NULL) {
893 		/* need to set a console if not done above. */
894 		console_doenv(stderr, errdev);
895 	}
896 	if (inputdev != NULL) {
897 		/* need to set a console if not done above. */
898 		console_doenv(stdin, inputdev);
899 	}
900 
901 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
902 done:
903 #endif
904 
905 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
906 	stdio_print_current_devices();
907 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
908 #ifdef CONFIG_VIDCONSOLE_AS_LCD
909 	if (strstr(stdoutname, "lcd"))
910 		printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
911 #endif
912 
913 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
914 	/* set the environment variables (will overwrite previous env settings) */
915 	for (i = 0; i < 3; i++) {
916 		env_set(stdio_names[i], stdio_devices[i]->name);
917 	}
918 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
919 
920 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
921 
922 #if 0
923 	/* If nothing usable installed, use only the initial console */
924 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
925 		return 0;
926 #endif
927 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
928 	return 0;
929 }
930 
931 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
932 
933 /* Called after the relocation - use desired console functions */
934 int console_init_r(void)
935 {
936 	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
937 	int i;
938 	struct list_head *list = stdio_get_list();
939 	struct list_head *pos;
940 	struct stdio_dev *dev;
941 
942 	console_update_silent();
943 
944 #ifdef CONFIG_SPLASH_SCREEN
945 	/*
946 	 * suppress all output if splash screen is enabled and we have
947 	 * a bmp to display. We redirect the output from frame buffer
948 	 * console to serial console in this case or suppress it if
949 	 * "silent" mode was requested.
950 	 */
951 	if (env_get("splashimage") != NULL) {
952 		if (!(gd->flags & GD_FLG_SILENT))
953 			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
954 	}
955 #endif
956 
957 	/* Scan devices looking for input and output devices */
958 	list_for_each(pos, list) {
959 		dev = list_entry(pos, struct stdio_dev, list);
960 
961 		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
962 			inputdev = dev;
963 		}
964 		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
965 			outputdev = dev;
966 		}
967 		if(inputdev && outputdev)
968 			break;
969 	}
970 
971 	/* Initializes output console first */
972 	if (outputdev != NULL) {
973 		console_setfile(stdout, outputdev);
974 		console_setfile(stderr, outputdev);
975 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
976 		console_devices[stdout][0] = outputdev;
977 		console_devices[stderr][0] = outputdev;
978 #endif
979 	}
980 
981 	/* Initializes input console */
982 	if (inputdev != NULL) {
983 		console_setfile(stdin, inputdev);
984 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
985 		console_devices[stdin][0] = inputdev;
986 #endif
987 	}
988 
989 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
990 	stdio_print_current_devices();
991 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
992 
993 	/* Setting environment variables */
994 	for (i = 0; i < 3; i++) {
995 		env_set(stdio_names[i], stdio_devices[i]->name);
996 	}
997 
998 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
999 
1000 #if 0
1001 	/* If nothing usable installed, use only the initial console */
1002 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
1003 		return 0;
1004 #endif
1005 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
1006 	return 0;
1007 }
1008 
1009 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
1010