blob: 1177f7d396b889effbb9973b77966a013f15ee96 [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>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020027#include <stdio_dev.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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020032#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +000033/*
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
37 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +010039extern int overwrite_console(void);
40#define OVERWRITE_CONSOLE overwrite_console()
wdenk47d1a6e2002-11-03 00:01:44 +000041#else
wdenk83e40ba2005-03-31 18:42:15 +000042#define OVERWRITE_CONSOLE 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020043#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk47d1a6e2002-11-03 00:01:44 +000044
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020045#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +000046
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020047static int console_setfile(int file, struct stdio_dev * dev)
wdenk47d1a6e2002-11-03 00:01:44 +000048{
49 int error = 0;
50
51 if (dev == NULL)
52 return -1;
53
54 switch (file) {
55 case stdin:
56 case stdout:
57 case stderr:
58 /* Start new device */
59 if (dev->start) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +010060 error = dev->start();
wdenk47d1a6e2002-11-03 00:01:44 +000061 /* If it's not started dont use it */
62 if (error < 0)
63 break;
64 }
65
66 /* Assign the new device (leaving the existing one started) */
67 stdio_devices[file] = dev;
68
69 /*
70 * Update monitor functions
71 * (to use the console stuff by other applications)
72 */
73 switch (file) {
74 case stdin:
wdenk27b207f2003-07-24 23:38:38 +000075 gd->jt[XF_getc] = dev->getc;
76 gd->jt[XF_tstc] = dev->tstc;
wdenk47d1a6e2002-11-03 00:01:44 +000077 break;
78 case stdout:
wdenk27b207f2003-07-24 23:38:38 +000079 gd->jt[XF_putc] = dev->putc;
80 gd->jt[XF_puts] = dev->puts;
81 gd->jt[XF_printf] = printf;
wdenk47d1a6e2002-11-03 00:01:44 +000082 break;
83 }
84 break;
85
86 default: /* Invalid file ID */
87 error = -1;
88 }
89 return error;
90}
91
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010092#if defined(CONFIG_CONSOLE_MUX)
93/** Console I/O multiplexing *******************************************/
94
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020095static struct stdio_dev *tstcdev;
96struct stdio_dev **console_devices[MAX_FILES];
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010097int cd_count[MAX_FILES];
98
99/*
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.
104 */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100105static int console_getc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100106{
107 unsigned char ret;
108
109 /* This is never called with testcdev == NULL */
110 ret = tstcdev->getc();
111 tstcdev = NULL;
112 return ret;
113}
114
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100115static int console_tstc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100116{
117 int i, ret;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200118 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100119
120 disable_ctrlc(1);
121 for (i = 0; i < cd_count[file]; i++) {
122 dev = console_devices[file][i];
123 if (dev->tstc != NULL) {
124 ret = dev->tstc();
125 if (ret > 0) {
126 tstcdev = dev;
127 disable_ctrlc(0);
128 return ret;
129 }
130 }
131 }
132 disable_ctrlc(0);
133
134 return 0;
135}
136
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100137static void console_putc(int file, const char c)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100138{
139 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200140 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100141
142 for (i = 0; i < cd_count[file]; i++) {
143 dev = console_devices[file][i];
144 if (dev->putc != NULL)
145 dev->putc(c);
146 }
147}
148
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100149static void console_puts(int file, const char *s)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100150{
151 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200152 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100153
154 for (i = 0; i < cd_count[file]; i++) {
155 dev = console_devices[file][i];
156 if (dev->puts != NULL)
157 dev->puts(s);
158 }
159}
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100160
161static inline void console_printdevs(int file)
162{
163 iomux_printdevs(file);
164}
165
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200166static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100167{
168 iomux_doenv(file, dev->name);
169}
170#else
171static inline int console_getc(int file)
172{
173 return stdio_devices[file]->getc();
174}
175
176static inline int console_tstc(int file)
177{
178 return stdio_devices[file]->tstc();
179}
180
181static inline void console_putc(int file, const char c)
182{
183 stdio_devices[file]->putc(c);
184}
185
186static inline void console_puts(int file, const char *s)
187{
188 stdio_devices[file]->puts(s);
189}
190
191static inline void console_printdevs(int file)
192{
193 printf("%s\n", stdio_devices[file]->name);
194}
195
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200196static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100197{
198 console_setfile(file, dev);
199}
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100200#endif /* defined(CONFIG_CONSOLE_MUX) */
201
wdenk47d1a6e2002-11-03 00:01:44 +0000202/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
203
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200204int serial_printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000205{
206 va_list args;
207 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200208 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000209
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100210 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000211
212 /* For this to work, printbuffer must be larger than
213 * anything we ever want to print.
214 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000215 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100216 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000217
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100218 serial_puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200219 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000220}
221
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100222int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000223{
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100224 if (file < MAX_FILES) {
225#if defined(CONFIG_CONSOLE_MUX)
226 /*
227 * Effectively poll for input wherever it may be available.
228 */
229 for (;;) {
230 /*
231 * Upper layer may have already called tstc() so
232 * check for that first.
233 */
234 if (tstcdev != NULL)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100235 return console_getc(file);
236 console_tstc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100237#ifdef CONFIG_WATCHDOG
238 /*
239 * If the watchdog must be rate-limited then it should
240 * already be handled in board-specific code.
241 */
242 udelay(1);
243#endif
244 }
245#else
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100246 return console_getc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100247#endif
248 }
wdenk47d1a6e2002-11-03 00:01:44 +0000249
250 return -1;
251}
252
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100253int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000254{
255 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100256 return console_tstc(file);
wdenk47d1a6e2002-11-03 00:01:44 +0000257
258 return -1;
259}
260
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100261void fputc(int file, const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000262{
263 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100264 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000265}
266
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100267void fputs(int file, const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000268{
269 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100270 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000271}
272
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200273int fprintf(int file, const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000274{
275 va_list args;
276 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200277 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000278
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100279 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000280
281 /* For this to work, printbuffer must be larger than
282 * anything we ever want to print.
283 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000284 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100285 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000286
287 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100288 fputs(file, printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200289 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000290}
291
292/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
293
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100294int getc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000295{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100296#ifdef CONFIG_DISABLE_CONSOLE
297 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
298 return 0;
299#endif
300
Graeme Russe3e454c2011-08-29 02:14:05 +0000301 if (!gd->have_console)
302 return 0;
303
wdenk47d1a6e2002-11-03 00:01:44 +0000304 if (gd->flags & GD_FLG_DEVINIT) {
305 /* Get from the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100306 return fgetc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000307 }
308
309 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100310 return serial_getc();
wdenk47d1a6e2002-11-03 00:01:44 +0000311}
312
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100313int tstc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000314{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100315#ifdef CONFIG_DISABLE_CONSOLE
316 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
317 return 0;
318#endif
319
Graeme Russe3e454c2011-08-29 02:14:05 +0000320 if (!gd->have_console)
321 return 0;
322
wdenk47d1a6e2002-11-03 00:01:44 +0000323 if (gd->flags & GD_FLG_DEVINIT) {
324 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100325 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000326 }
327
328 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100329 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000330}
331
Simon Glass3fa49772012-03-19 21:59:14 -0700332#ifdef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russ9558b482011-09-01 00:48:27 +0000333#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
334
335static void pre_console_putc(const char c)
336{
337 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
338
339 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
340}
341
342static void pre_console_puts(const char *s)
343{
344 while (*s)
345 pre_console_putc(*s++);
346}
347
348static void print_pre_console_buffer(void)
349{
350 unsigned long i = 0;
351 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
352
353 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
354 i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
355
356 while (i < gd->precon_buf_idx)
357 putc(buffer[CIRC_BUF_IDX(i++)]);
358}
359#else
360static inline void pre_console_putc(const char c) {}
361static inline void pre_console_puts(const char *s) {}
362static inline void print_pre_console_buffer(void) {}
363#endif
364
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100365void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000366{
wdenka6cccae2004-02-06 21:48:22 +0000367#ifdef CONFIG_SILENT_CONSOLE
368 if (gd->flags & GD_FLG_SILENT)
wdenkf6e20fc2004-02-08 19:38:38 +0000369 return;
wdenka6cccae2004-02-06 21:48:22 +0000370#endif
371
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100372#ifdef CONFIG_DISABLE_CONSOLE
373 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
374 return;
375#endif
376
Graeme Russe3e454c2011-08-29 02:14:05 +0000377 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000378 return pre_console_putc(c);
Graeme Russe3e454c2011-08-29 02:14:05 +0000379
wdenk47d1a6e2002-11-03 00:01:44 +0000380 if (gd->flags & GD_FLG_DEVINIT) {
381 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100382 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000383 } else {
384 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100385 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000386 }
387}
388
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100389void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000390{
wdenka6cccae2004-02-06 21:48:22 +0000391#ifdef CONFIG_SILENT_CONSOLE
392 if (gd->flags & GD_FLG_SILENT)
393 return;
394#endif
395
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100396#ifdef CONFIG_DISABLE_CONSOLE
397 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
398 return;
399#endif
400
Graeme Russe3e454c2011-08-29 02:14:05 +0000401 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000402 return pre_console_puts(s);
Graeme Russe3e454c2011-08-29 02:14:05 +0000403
wdenk47d1a6e2002-11-03 00:01:44 +0000404 if (gd->flags & GD_FLG_DEVINIT) {
405 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100406 fputs(stdout, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000407 } else {
408 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100409 serial_puts(s);
wdenk47d1a6e2002-11-03 00:01:44 +0000410 }
411}
412
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200413int printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000414{
415 va_list args;
416 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200417 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000418
Graeme Russ9558b482011-09-01 00:48:27 +0000419#ifndef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russe3e454c2011-08-29 02:14:05 +0000420 if (!gd->have_console)
421 return 0;
Graeme Russ9558b482011-09-01 00:48:27 +0000422#endif
Graeme Russe3e454c2011-08-29 02:14:05 +0000423
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100424 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000425
426 /* For this to work, printbuffer must be larger than
427 * anything we ever want to print.
428 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000429 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100430 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000431
432 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100433 puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200434 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000435}
436
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200437int vprintf(const char *fmt, va_list args)
wdenk6dd652f2003-06-19 23:40:20 +0000438{
439 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200440 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk6dd652f2003-06-19 23:40:20 +0000441
Graeme Russ9558b482011-09-01 00:48:27 +0000442#ifndef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russe3e454c2011-08-29 02:14:05 +0000443 if (!gd->have_console)
444 return 0;
Graeme Russ9558b482011-09-01 00:48:27 +0000445#endif
Graeme Russe3e454c2011-08-29 02:14:05 +0000446
wdenk6dd652f2003-06-19 23:40:20 +0000447 /* For this to work, printbuffer must be larger than
448 * anything we ever want to print.
449 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000450 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk6dd652f2003-06-19 23:40:20 +0000451
452 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100453 puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200454 return i;
wdenk6dd652f2003-06-19 23:40:20 +0000455}
456
wdenk47d1a6e2002-11-03 00:01:44 +0000457/* test if ctrl-c was pressed */
458static int ctrlc_disabled = 0; /* see disable_ctrl() */
459static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100460int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000461{
wdenk47d1a6e2002-11-03 00:01:44 +0000462 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100463 if (tstc()) {
464 switch (getc()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000465 case 0x03: /* ^C - Control C */
466 ctrlc_was_pressed = 1;
467 return 1;
468 default:
469 break;
470 }
471 }
472 }
473 return 0;
474}
475
476/* pass 1 to disable ctrlc() checking, 0 to enable.
477 * returns previous state
478 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100479int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000480{
481 int prev = ctrlc_disabled; /* save previous state */
482
483 ctrlc_disabled = disable;
484 return prev;
485}
486
487int had_ctrlc (void)
488{
489 return ctrlc_was_pressed;
490}
491
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100492void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000493{
494 ctrlc_was_pressed = 0;
495}
496
497#ifdef CONFIG_MODEM_SUPPORT_DEBUG
498char screen[1024];
499char *cursor = screen;
500int once = 0;
501inline void dbg(const char *fmt, ...)
502{
503 va_list args;
504 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200505 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000506
507 if (!once) {
508 memset(screen, 0, sizeof(screen));
509 once++;
510 }
511
512 va_start(args, fmt);
513
514 /* For this to work, printbuffer must be larger than
515 * anything we ever want to print.
516 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000517 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk47d1a6e2002-11-03 00:01:44 +0000518 va_end(args);
519
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100520 if ((screen + sizeof(screen) - 1 - cursor)
521 < strlen(printbuffer) + 1) {
wdenk47d1a6e2002-11-03 00:01:44 +0000522 memset(screen, 0, sizeof(screen));
523 cursor = screen;
524 }
525 sprintf(cursor, printbuffer);
526 cursor += strlen(printbuffer);
527
528}
529#else
530inline void dbg(const char *fmt, ...)
531{
532}
533#endif
534
535/** U-Boot INIT FUNCTIONS *************************************************/
536
Mike Frysingerd7be3052010-10-20 07:18:03 -0400537struct stdio_dev *search_device(int flags, const char *name)
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200538{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200539 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200540
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200541 dev = stdio_get_by_name(name);
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200542
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100543 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200544 return dev;
545
546 return NULL;
547}
548
Mike Frysingerd7be3052010-10-20 07:18:03 -0400549int console_assign(int file, const char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000550{
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200551 int flag;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200552 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000553
554 /* Check for valid file */
555 switch (file) {
556 case stdin:
557 flag = DEV_FLAGS_INPUT;
558 break;
559 case stdout:
560 case stderr:
561 flag = DEV_FLAGS_OUTPUT;
562 break;
563 default:
564 return -1;
565 }
566
567 /* Check for valid device name */
568
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200569 dev = search_device(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000570
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100571 if (dev)
572 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000573
574 return -1;
575}
576
577/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100578int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000579{
wdenk47d1a6e2002-11-03 00:01:44 +0000580 gd->have_console = 1;
wdenkf72da342003-10-10 10:05:42 +0000581
582#ifdef CONFIG_SILENT_CONSOLE
583 if (getenv("silent") != NULL)
584 gd->flags |= GD_FLG_SILENT;
585#endif
586
Graeme Russ9558b482011-09-01 00:48:27 +0000587 print_pre_console_buffer();
588
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100589 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000590}
591
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200592void stdio_print_current_devices(void)
593{
David Brownell52f6c342009-08-30 11:05:29 -0700594#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200595 /* Print information */
596 puts("In: ");
597 if (stdio_devices[stdin] == NULL) {
598 puts("No input devices available!\n");
599 } else {
600 printf ("%s\n", stdio_devices[stdin]->name);
601 }
602
603 puts("Out: ");
604 if (stdio_devices[stdout] == NULL) {
605 puts("No output devices available!\n");
606 } else {
607 printf ("%s\n", stdio_devices[stdout]->name);
608 }
609
610 puts("Err: ");
611 if (stdio_devices[stderr] == NULL) {
612 puts("No error devices available!\n");
613 } else {
614 printf ("%s\n", stdio_devices[stderr]->name);
615 }
616#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
617}
618
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200619#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +0000620/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100621int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000622{
623 char *stdinname, *stdoutname, *stderrname;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200624 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200625#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk6e592382004-04-18 17:39:38 +0000626 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200627#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100628#ifdef CONFIG_CONSOLE_MUX
629 int iomux_err = 0;
630#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000631
632 /* set default handlers at first */
wdenk27b207f2003-07-24 23:38:38 +0000633 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;
wdenk47d1a6e2002-11-03 00:01:44 +0000638
639 /* stdin stdout and stderr are in environment */
640 /* scan for it */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100641 stdinname = getenv("stdin");
642 stdoutname = getenv("stdout");
643 stderrname = getenv("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +0000644
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200645 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100646 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
647 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
648 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100649#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);
653 if (!iomux_err)
654 /* Successful, so skip all the code below. */
655 goto done;
656#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000657 }
658 /* if the devices are overwritten or not found, use default device */
659 if (inputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100660 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000661 }
662 if (outputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100663 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000664 }
665 if (errdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100666 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000667 }
668 /* Initializes output console first */
669 if (outputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100670 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100671 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000672 }
673 if (errdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100674 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100675 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000676 }
677 if (inputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100678 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100679 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000680 }
681
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100682#ifdef CONFIG_CONSOLE_MUX
683done:
684#endif
685
wdenk5f535fe2003-09-18 09:21:33 +0000686 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
687
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200688 stdio_print_current_devices();
wdenk47d1a6e2002-11-03 00:01:44 +0000689
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200690#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk47d1a6e2002-11-03 00:01:44 +0000691 /* set the environment variables (will overwrite previous env settings) */
692 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100693 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000694 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200695#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk47d1a6e2002-11-03 00:01:44 +0000696
697#if 0
698 /* If nothing usable installed, use only the initial console */
699 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100700 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000701#endif
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100702 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000703}
704
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200705#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000706
707/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100708int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000709{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200710 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200711 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200712 struct list_head *list = stdio_get_list();
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200713 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200714 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000715
wdenkd791b1d2003-04-20 14:04:18 +0000716#ifdef CONFIG_SPLASH_SCREEN
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100717 /*
718 * suppress all output if splash screen is enabled and we have
Anatolij Gustschina7490812010-03-16 15:29:33 +0100719 * 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.
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100722 */
Anatolij Gustschina7490812010-03-16 15:29:33 +0100723 if (getenv("splashimage") != NULL) {
724 if (!(gd->flags & GD_FLG_SILENT))
725 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
726 }
wdenkf72da342003-10-10 10:05:42 +0000727#endif
728
wdenk47d1a6e2002-11-03 00:01:44 +0000729 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200730 list_for_each(pos, list) {
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200731 dev = list_entry(pos, struct stdio_dev, list);
wdenk47d1a6e2002-11-03 00:01:44 +0000732
733 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
734 inputdev = dev;
735 }
736 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
737 outputdev = dev;
738 }
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200739 if(inputdev && outputdev)
740 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000741 }
742
743 /* Initializes output console first */
744 if (outputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100745 console_setfile(stdout, outputdev);
746 console_setfile(stderr, outputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100747#ifdef CONFIG_CONSOLE_MUX
748 console_devices[stdout][0] = outputdev;
749 console_devices[stderr][0] = outputdev;
750#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000751 }
752
753 /* Initializes input console */
754 if (inputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100755 console_setfile(stdin, inputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100756#ifdef CONFIG_CONSOLE_MUX
757 console_devices[stdin][0] = inputdev;
758#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000759 }
760
wdenk5f535fe2003-09-18 09:21:33 +0000761 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
762
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200763 stdio_print_current_devices();
wdenk47d1a6e2002-11-03 00:01:44 +0000764
765 /* Setting environment variables */
766 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100767 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000768 }
769
770#if 0
771 /* If nothing usable installed, use only the initial console */
772 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100773 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000774#endif
775
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100776 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000777}
778
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200779#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */