blob: 2add047880ea63a5fec679f8e18d0c3e1f5abc2d [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
2 * (C) Copyright 2000
3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <stdarg.h>
26#include <malloc.h>
27#include <console.h>
wdenk27b207f2003-07-24 23:38:38 +000028#include <exports.h>
wdenk47d1a6e2002-11-03 00:01:44 +000029
Wolfgang Denkd87080b2006-03-31 18:32:53 +020030DECLARE_GLOBAL_DATA_PTR;
31
wdenkc7de8292002-11-19 11:04:11 +000032#ifdef CONFIG_AMIGAONEG3SE
33int console_changed = 0;
34#endif
35
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020036#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +000037/*
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
41 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020042#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +010043extern int overwrite_console(void);
44#define OVERWRITE_CONSOLE overwrite_console()
wdenk47d1a6e2002-11-03 00:01:44 +000045#else
wdenk83e40ba2005-03-31 18:42:15 +000046#define OVERWRITE_CONSOLE 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk47d1a6e2002-11-03 00:01:44 +000048
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020049#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +000050
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +010051static int console_setfile(int file, device_t * dev)
wdenk47d1a6e2002-11-03 00:01:44 +000052{
53 int error = 0;
54
55 if (dev == NULL)
56 return -1;
57
58 switch (file) {
59 case stdin:
60 case stdout:
61 case stderr:
62 /* Start new device */
63 if (dev->start) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +010064 error = dev->start();
wdenk47d1a6e2002-11-03 00:01:44 +000065 /* If it's not started dont use it */
66 if (error < 0)
67 break;
68 }
69
70 /* Assign the new device (leaving the existing one started) */
71 stdio_devices[file] = dev;
72
73 /*
74 * Update monitor functions
75 * (to use the console stuff by other applications)
76 */
77 switch (file) {
78 case stdin:
wdenk27b207f2003-07-24 23:38:38 +000079 gd->jt[XF_getc] = dev->getc;
80 gd->jt[XF_tstc] = dev->tstc;
wdenk47d1a6e2002-11-03 00:01:44 +000081 break;
82 case stdout:
wdenk27b207f2003-07-24 23:38:38 +000083 gd->jt[XF_putc] = dev->putc;
84 gd->jt[XF_puts] = dev->puts;
85 gd->jt[XF_printf] = printf;
wdenk47d1a6e2002-11-03 00:01:44 +000086 break;
87 }
88 break;
89
90 default: /* Invalid file ID */
91 error = -1;
92 }
93 return error;
94}
95
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010096#if defined(CONFIG_CONSOLE_MUX)
97/** Console I/O multiplexing *******************************************/
98
99static device_t *tstcdev;
100device_t **console_devices[MAX_FILES];
101int cd_count[MAX_FILES];
102
103/*
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.
108 */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100109static int console_getc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100110{
111 unsigned char ret;
112
113 /* This is never called with testcdev == NULL */
114 ret = tstcdev->getc();
115 tstcdev = NULL;
116 return ret;
117}
118
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100119static int console_tstc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100120{
121 int i, ret;
122 device_t *dev;
123
124 disable_ctrlc(1);
125 for (i = 0; i < cd_count[file]; i++) {
126 dev = console_devices[file][i];
127 if (dev->tstc != NULL) {
128 ret = dev->tstc();
129 if (ret > 0) {
130 tstcdev = dev;
131 disable_ctrlc(0);
132 return ret;
133 }
134 }
135 }
136 disable_ctrlc(0);
137
138 return 0;
139}
140
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100141static void console_putc(int file, const char c)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100142{
143 int i;
144 device_t *dev;
145
146 for (i = 0; i < cd_count[file]; i++) {
147 dev = console_devices[file][i];
148 if (dev->putc != NULL)
149 dev->putc(c);
150 }
151}
152
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100153static void console_puts(int file, const char *s)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100154{
155 int i;
156 device_t *dev;
157
158 for (i = 0; i < cd_count[file]; i++) {
159 dev = console_devices[file][i];
160 if (dev->puts != NULL)
161 dev->puts(s);
162 }
163}
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100164
165static inline void console_printdevs(int file)
166{
167 iomux_printdevs(file);
168}
169
170static inline void console_doenv(int file, device_t *dev)
171{
172 iomux_doenv(file, dev->name);
173}
174#else
175static inline int console_getc(int file)
176{
177 return stdio_devices[file]->getc();
178}
179
180static inline int console_tstc(int file)
181{
182 return stdio_devices[file]->tstc();
183}
184
185static inline void console_putc(int file, const char c)
186{
187 stdio_devices[file]->putc(c);
188}
189
190static inline void console_puts(int file, const char *s)
191{
192 stdio_devices[file]->puts(s);
193}
194
195static inline void console_printdevs(int file)
196{
197 printf("%s\n", stdio_devices[file]->name);
198}
199
200static inline void console_doenv(int file, device_t *dev)
201{
202 console_setfile(file, dev);
203}
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100204#endif /* defined(CONFIG_CONSOLE_MUX) */
205
wdenk47d1a6e2002-11-03 00:01:44 +0000206/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
207
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100208void serial_printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000209{
210 va_list args;
211 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200212 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000213
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100214 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000215
216 /* For this to work, printbuffer must be larger than
217 * anything we ever want to print.
218 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100219 i = vsprintf(printbuffer, fmt, args);
220 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000221
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100222 serial_puts(printbuffer);
wdenk47d1a6e2002-11-03 00:01:44 +0000223}
224
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100225int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000226{
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100227 if (file < MAX_FILES) {
228#if defined(CONFIG_CONSOLE_MUX)
229 /*
230 * Effectively poll for input wherever it may be available.
231 */
232 for (;;) {
233 /*
234 * Upper layer may have already called tstc() so
235 * check for that first.
236 */
237 if (tstcdev != NULL)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100238 return console_getc(file);
239 console_tstc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100240#ifdef CONFIG_WATCHDOG
241 /*
242 * If the watchdog must be rate-limited then it should
243 * already be handled in board-specific code.
244 */
245 udelay(1);
246#endif
247 }
248#else
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100249 return console_getc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100250#endif
251 }
wdenk47d1a6e2002-11-03 00:01:44 +0000252
253 return -1;
254}
255
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100256int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000257{
258 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100259 return console_tstc(file);
wdenk47d1a6e2002-11-03 00:01:44 +0000260
261 return -1;
262}
263
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100264void fputc(int file, const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000265{
266 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100267 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000268}
269
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100270void fputs(int file, const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000271{
272 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100273 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000274}
275
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100276void fprintf(int file, const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000277{
278 va_list args;
279 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200280 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000281
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100282 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000283
284 /* For this to work, printbuffer must be larger than
285 * anything we ever want to print.
286 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100287 i = vsprintf(printbuffer, fmt, args);
288 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000289
290 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100291 fputs(file, printbuffer);
wdenk47d1a6e2002-11-03 00:01:44 +0000292}
293
294/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
295
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100296int getc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000297{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100298#ifdef CONFIG_DISABLE_CONSOLE
299 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
300 return 0;
301#endif
302
wdenk47d1a6e2002-11-03 00:01:44 +0000303 if (gd->flags & GD_FLG_DEVINIT) {
304 /* Get from the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100305 return fgetc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000306 }
307
308 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100309 return serial_getc();
wdenk47d1a6e2002-11-03 00:01:44 +0000310}
311
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100312int tstc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000313{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100314#ifdef CONFIG_DISABLE_CONSOLE
315 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
316 return 0;
317#endif
318
wdenk47d1a6e2002-11-03 00:01:44 +0000319 if (gd->flags & GD_FLG_DEVINIT) {
320 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100321 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000322 }
323
324 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100325 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000326}
327
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100328void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000329{
wdenka6cccae2004-02-06 21:48:22 +0000330#ifdef CONFIG_SILENT_CONSOLE
331 if (gd->flags & GD_FLG_SILENT)
wdenkf6e20fc2004-02-08 19:38:38 +0000332 return;
wdenka6cccae2004-02-06 21:48:22 +0000333#endif
334
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100335#ifdef CONFIG_DISABLE_CONSOLE
336 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
337 return;
338#endif
339
wdenk47d1a6e2002-11-03 00:01:44 +0000340 if (gd->flags & GD_FLG_DEVINIT) {
341 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100342 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000343 } else {
344 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100345 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000346 }
347}
348
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100349void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000350{
wdenka6cccae2004-02-06 21:48:22 +0000351#ifdef CONFIG_SILENT_CONSOLE
352 if (gd->flags & GD_FLG_SILENT)
353 return;
354#endif
355
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100356#ifdef CONFIG_DISABLE_CONSOLE
357 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
358 return;
359#endif
360
wdenk47d1a6e2002-11-03 00:01:44 +0000361 if (gd->flags & GD_FLG_DEVINIT) {
362 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100363 fputs(stdout, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000364 } else {
365 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100366 serial_puts(s);
wdenk47d1a6e2002-11-03 00:01:44 +0000367 }
368}
369
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100370void printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000371{
372 va_list args;
373 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200374 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000375
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100376 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000377
378 /* For this to work, printbuffer must be larger than
379 * anything we ever want to print.
380 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100381 i = vsprintf(printbuffer, fmt, args);
382 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000383
384 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100385 puts(printbuffer);
wdenk47d1a6e2002-11-03 00:01:44 +0000386}
387
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100388void vprintf(const char *fmt, va_list args)
wdenk6dd652f2003-06-19 23:40:20 +0000389{
390 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200391 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk6dd652f2003-06-19 23:40:20 +0000392
393 /* For this to work, printbuffer must be larger than
394 * anything we ever want to print.
395 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100396 i = vsprintf(printbuffer, fmt, args);
wdenk6dd652f2003-06-19 23:40:20 +0000397
398 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100399 puts(printbuffer);
wdenk6dd652f2003-06-19 23:40:20 +0000400}
401
wdenk47d1a6e2002-11-03 00:01:44 +0000402/* test if ctrl-c was pressed */
403static int ctrlc_disabled = 0; /* see disable_ctrl() */
404static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100405int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000406{
wdenk47d1a6e2002-11-03 00:01:44 +0000407 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100408 if (tstc()) {
409 switch (getc()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000410 case 0x03: /* ^C - Control C */
411 ctrlc_was_pressed = 1;
412 return 1;
413 default:
414 break;
415 }
416 }
417 }
418 return 0;
419}
420
421/* pass 1 to disable ctrlc() checking, 0 to enable.
422 * returns previous state
423 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100424int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000425{
426 int prev = ctrlc_disabled; /* save previous state */
427
428 ctrlc_disabled = disable;
429 return prev;
430}
431
432int had_ctrlc (void)
433{
434 return ctrlc_was_pressed;
435}
436
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100437void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000438{
439 ctrlc_was_pressed = 0;
440}
441
442#ifdef CONFIG_MODEM_SUPPORT_DEBUG
443char screen[1024];
444char *cursor = screen;
445int once = 0;
446inline void dbg(const char *fmt, ...)
447{
448 va_list args;
449 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200450 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000451
452 if (!once) {
453 memset(screen, 0, sizeof(screen));
454 once++;
455 }
456
457 va_start(args, fmt);
458
459 /* For this to work, printbuffer must be larger than
460 * anything we ever want to print.
461 */
462 i = vsprintf(printbuffer, fmt, args);
463 va_end(args);
464
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100465 if ((screen + sizeof(screen) - 1 - cursor)
466 < strlen(printbuffer) + 1) {
wdenk47d1a6e2002-11-03 00:01:44 +0000467 memset(screen, 0, sizeof(screen));
468 cursor = screen;
469 }
470 sprintf(cursor, printbuffer);
471 cursor += strlen(printbuffer);
472
473}
474#else
475inline void dbg(const char *fmt, ...)
476{
477}
478#endif
479
480/** U-Boot INIT FUNCTIONS *************************************************/
481
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100482device_t *search_device(int flags, char *name)
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200483{
484 device_t *dev;
485
486 dev = device_get_by_name(name);
487
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100488 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200489 return dev;
490
491 return NULL;
492}
493
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100494int console_assign(int file, char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000495{
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200496 int flag;
497 device_t *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000498
499 /* Check for valid file */
500 switch (file) {
501 case stdin:
502 flag = DEV_FLAGS_INPUT;
503 break;
504 case stdout:
505 case stderr:
506 flag = DEV_FLAGS_OUTPUT;
507 break;
508 default:
509 return -1;
510 }
511
512 /* Check for valid device name */
513
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200514 dev = search_device(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000515
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100516 if (dev)
517 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000518
519 return -1;
520}
521
522/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100523int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000524{
wdenk47d1a6e2002-11-03 00:01:44 +0000525 gd->have_console = 1;
wdenkf72da342003-10-10 10:05:42 +0000526
527#ifdef CONFIG_SILENT_CONSOLE
528 if (getenv("silent") != NULL)
529 gd->flags |= GD_FLG_SILENT;
530#endif
531
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100532 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000533}
534
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200535#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +0000536/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100537int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000538{
539 char *stdinname, *stdoutname, *stderrname;
540 device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200541#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk6e592382004-04-18 17:39:38 +0000542 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200543#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100544#ifdef CONFIG_CONSOLE_MUX
545 int iomux_err = 0;
546#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000547
548 /* set default handlers at first */
wdenk27b207f2003-07-24 23:38:38 +0000549 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;
wdenk47d1a6e2002-11-03 00:01:44 +0000554
555 /* stdin stdout and stderr are in environment */
556 /* scan for it */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100557 stdinname = getenv("stdin");
558 stdoutname = getenv("stdout");
559 stderrname = getenv("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +0000560
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200561 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100562 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
563 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
564 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100565#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);
569 if (!iomux_err)
570 /* Successful, so skip all the code below. */
571 goto done;
572#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000573 }
574 /* if the devices are overwritten or not found, use default device */
575 if (inputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100576 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000577 }
578 if (outputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100579 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000580 }
581 if (errdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100582 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000583 }
584 /* Initializes output console first */
585 if (outputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100586 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100587 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000588 }
589 if (errdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100590 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100591 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000592 }
593 if (inputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100594 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100595 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000596 }
597
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100598#ifdef CONFIG_CONSOLE_MUX
599done:
600#endif
601
wdenk5f535fe2003-09-18 09:21:33 +0000602 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
603
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200604#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
wdenk47d1a6e2002-11-03 00:01:44 +0000605 /* Print information */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100606 puts("In: ");
wdenk47d1a6e2002-11-03 00:01:44 +0000607 if (stdio_devices[stdin] == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100608 puts("No input devices available!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000609 } else {
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100610 console_printdevs(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000611 }
612
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100613 puts("Out: ");
wdenk47d1a6e2002-11-03 00:01:44 +0000614 if (stdio_devices[stdout] == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100615 puts("No output devices available!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000616 } else {
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100617 console_printdevs(stdout);
wdenk47d1a6e2002-11-03 00:01:44 +0000618 }
619
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100620 puts("Err: ");
wdenk47d1a6e2002-11-03 00:01:44 +0000621 if (stdio_devices[stderr] == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100622 puts("No error devices available!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000623 } else {
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100624 console_printdevs(stderr);
wdenk47d1a6e2002-11-03 00:01:44 +0000625 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200626#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000627
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200628#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk47d1a6e2002-11-03 00:01:44 +0000629 /* set the environment variables (will overwrite previous env settings) */
630 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100631 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000632 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200633#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk47d1a6e2002-11-03 00:01:44 +0000634
635#if 0
636 /* If nothing usable installed, use only the initial console */
637 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100638 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000639#endif
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100640 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000641}
642
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200643#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000644
645/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100646int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000647{
648 device_t *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200649 int i;
650 struct list_head *list = device_get_list();
651 struct list_head *pos;
652 device_t *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000653
wdenkd791b1d2003-04-20 14:04:18 +0000654#ifdef CONFIG_SPLASH_SCREEN
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100655 /*
656 * suppress all output if splash screen is enabled and we have
657 * a bmp to display
658 */
dzu87970eb2003-09-29 21:55:54 +0000659 if (getenv("splashimage") != NULL)
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200660 gd->flags |= GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000661#endif
662
wdenk47d1a6e2002-11-03 00:01:44 +0000663 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200664 list_for_each(pos, list) {
665 dev = list_entry(pos, device_t, list);
wdenk47d1a6e2002-11-03 00:01:44 +0000666
667 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
668 inputdev = dev;
669 }
670 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
671 outputdev = dev;
672 }
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200673 if(inputdev && outputdev)
674 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000675 }
676
677 /* Initializes output console first */
678 if (outputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100679 console_setfile(stdout, outputdev);
680 console_setfile(stderr, outputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100681#ifdef CONFIG_CONSOLE_MUX
682 console_devices[stdout][0] = outputdev;
683 console_devices[stderr][0] = outputdev;
684#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000685 }
686
687 /* Initializes input console */
688 if (inputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100689 console_setfile(stdin, inputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100690#ifdef CONFIG_CONSOLE_MUX
691 console_devices[stdin][0] = inputdev;
692#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000693 }
694
wdenk5f535fe2003-09-18 09:21:33 +0000695 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
696
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200697#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
wdenk47cd00f2003-03-06 13:39:27 +0000698 /* Print information */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100699 puts("In: ");
wdenk47d1a6e2002-11-03 00:01:44 +0000700 if (stdio_devices[stdin] == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100701 puts("No input devices available!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000702 } else {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100703 printf("%s\n", stdio_devices[stdin]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000704 }
705
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100706 puts("Out: ");
wdenk47d1a6e2002-11-03 00:01:44 +0000707 if (stdio_devices[stdout] == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100708 puts("No output devices available!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000709 } else {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100710 printf("%s\n", stdio_devices[stdout]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000711 }
712
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100713 puts("Err: ");
wdenk47d1a6e2002-11-03 00:01:44 +0000714 if (stdio_devices[stderr] == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100715 puts("No error devices available!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000716 } else {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100717 printf("%s\n", stdio_devices[stderr]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000718 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200719#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000720
721 /* Setting environment variables */
722 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100723 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000724 }
725
726#if 0
727 /* If nothing usable installed, use only the initial console */
728 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100729 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000730#endif
731
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100732 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000733}
734
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200735#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */