blob: bf7317869097a8bf84bf9929bacdf90ff8f693a3 [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>
Joe Hershberger849d5d92012-12-11 22:16:29 -060027#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020028#include <stdio_dev.h>
wdenk27b207f2003-07-24 23:38:38 +000029#include <exports.h>
Joe Hershberger849d5d92012-12-11 22:16:29 -060030#include <environment.h>
wdenk47d1a6e2002-11-03 00:01:44 +000031
Wolfgang Denkd87080b2006-03-31 18:32:53 +020032DECLARE_GLOBAL_DATA_PTR;
33
Joe Hershberger849d5d92012-12-11 22:16:29 -060034static int on_console(const char *name, const char *value, enum env_op op,
35 int flags)
36{
37 int console = -1;
38
39 /* Check for console redirection */
40 if (strcmp(name, "stdin") == 0)
41 console = stdin;
42 else if (strcmp(name, "stdout") == 0)
43 console = stdout;
44 else if (strcmp(name, "stderr") == 0)
45 console = stderr;
46
47 /* if not actually setting a console variable, we don't care */
48 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
49 return 0;
50
51 switch (op) {
52 case env_op_create:
53 case env_op_overwrite:
54
55#ifdef CONFIG_CONSOLE_MUX
56 if (iomux_doenv(console, value))
57 return 1;
58#else
59 /* Try assigning specified device */
60 if (console_assign(console, value) < 0)
61 return 1;
62#endif /* CONFIG_CONSOLE_MUX */
63 return 0;
64
65 case env_op_delete:
66 if ((flags & H_FORCE) == 0)
67 printf("Can't delete \"%s\"\n", name);
68 return 1;
69
70 default:
71 return 0;
72 }
73}
74U_BOOT_ENV_CALLBACK(console, on_console);
75
Joe Hershbergere080d542012-12-11 22:16:30 -060076#ifdef CONFIG_SILENT_CONSOLE
77static int on_silent(const char *name, const char *value, enum env_op op,
78 int flags)
79{
80#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
81 if (flags & H_INTERACTIVE)
82 return 0;
83#endif
84#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
85 if ((flags & H_INTERACTIVE) == 0)
86 return 0;
87#endif
88
89 if (value != NULL)
90 gd->flags |= GD_FLG_SILENT;
91 else
92 gd->flags &= ~GD_FLG_SILENT;
93
94 return 0;
95}
96U_BOOT_ENV_CALLBACK(silent, on_silent);
97#endif
98
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +0000100/*
101 * if overwrite_console returns 1, the stdin, stderr and stdout
102 * are switched to the serial port, else the settings in the
103 * environment are used
104 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200105#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100106extern int overwrite_console(void);
107#define OVERWRITE_CONSOLE overwrite_console()
wdenk47d1a6e2002-11-03 00:01:44 +0000108#else
wdenk83e40ba2005-03-31 18:42:15 +0000109#define OVERWRITE_CONSOLE 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200110#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk47d1a6e2002-11-03 00:01:44 +0000111
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200112#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000113
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200114static int console_setfile(int file, struct stdio_dev * dev)
wdenk47d1a6e2002-11-03 00:01:44 +0000115{
116 int error = 0;
117
118 if (dev == NULL)
119 return -1;
120
121 switch (file) {
122 case stdin:
123 case stdout:
124 case stderr:
125 /* Start new device */
126 if (dev->start) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100127 error = dev->start();
wdenk47d1a6e2002-11-03 00:01:44 +0000128 /* If it's not started dont use it */
129 if (error < 0)
130 break;
131 }
132
133 /* Assign the new device (leaving the existing one started) */
134 stdio_devices[file] = dev;
135
136 /*
137 * Update monitor functions
138 * (to use the console stuff by other applications)
139 */
140 switch (file) {
141 case stdin:
wdenk27b207f2003-07-24 23:38:38 +0000142 gd->jt[XF_getc] = dev->getc;
143 gd->jt[XF_tstc] = dev->tstc;
wdenk47d1a6e2002-11-03 00:01:44 +0000144 break;
145 case stdout:
wdenk27b207f2003-07-24 23:38:38 +0000146 gd->jt[XF_putc] = dev->putc;
147 gd->jt[XF_puts] = dev->puts;
148 gd->jt[XF_printf] = printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000149 break;
150 }
151 break;
152
153 default: /* Invalid file ID */
154 error = -1;
155 }
156 return error;
157}
158
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100159#if defined(CONFIG_CONSOLE_MUX)
160/** Console I/O multiplexing *******************************************/
161
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200162static struct stdio_dev *tstcdev;
163struct stdio_dev **console_devices[MAX_FILES];
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100164int cd_count[MAX_FILES];
165
166/*
167 * This depends on tstc() always being called before getc().
168 * This is guaranteed to be true because this routine is called
169 * only from fgetc() which assures it.
170 * No attempt is made to demultiplex multiple input sources.
171 */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100172static int console_getc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100173{
174 unsigned char ret;
175
176 /* This is never called with testcdev == NULL */
177 ret = tstcdev->getc();
178 tstcdev = NULL;
179 return ret;
180}
181
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100182static int console_tstc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100183{
184 int i, ret;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200185 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100186
187 disable_ctrlc(1);
188 for (i = 0; i < cd_count[file]; i++) {
189 dev = console_devices[file][i];
190 if (dev->tstc != NULL) {
191 ret = dev->tstc();
192 if (ret > 0) {
193 tstcdev = dev;
194 disable_ctrlc(0);
195 return ret;
196 }
197 }
198 }
199 disable_ctrlc(0);
200
201 return 0;
202}
203
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100204static void console_putc(int file, const char c)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100205{
206 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200207 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100208
209 for (i = 0; i < cd_count[file]; i++) {
210 dev = console_devices[file][i];
211 if (dev->putc != NULL)
212 dev->putc(c);
213 }
214}
215
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100216static void console_puts(int file, const char *s)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100217{
218 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200219 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100220
221 for (i = 0; i < cd_count[file]; i++) {
222 dev = console_devices[file][i];
223 if (dev->puts != NULL)
224 dev->puts(s);
225 }
226}
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100227
228static inline void console_printdevs(int file)
229{
230 iomux_printdevs(file);
231}
232
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200233static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100234{
235 iomux_doenv(file, dev->name);
236}
237#else
238static inline int console_getc(int file)
239{
240 return stdio_devices[file]->getc();
241}
242
243static inline int console_tstc(int file)
244{
245 return stdio_devices[file]->tstc();
246}
247
248static inline void console_putc(int file, const char c)
249{
250 stdio_devices[file]->putc(c);
251}
252
253static inline void console_puts(int file, const char *s)
254{
255 stdio_devices[file]->puts(s);
256}
257
258static inline void console_printdevs(int file)
259{
260 printf("%s\n", stdio_devices[file]->name);
261}
262
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200263static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100264{
265 console_setfile(file, dev);
266}
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100267#endif /* defined(CONFIG_CONSOLE_MUX) */
268
wdenk47d1a6e2002-11-03 00:01:44 +0000269/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
270
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200271int serial_printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000272{
273 va_list args;
274 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200275 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000276
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100277 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000278
279 /* For this to work, printbuffer must be larger than
280 * anything we ever want to print.
281 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000282 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100283 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000284
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100285 serial_puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200286 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000287}
288
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100289int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000290{
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100291 if (file < MAX_FILES) {
292#if defined(CONFIG_CONSOLE_MUX)
293 /*
294 * Effectively poll for input wherever it may be available.
295 */
296 for (;;) {
297 /*
298 * Upper layer may have already called tstc() so
299 * check for that first.
300 */
301 if (tstcdev != NULL)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100302 return console_getc(file);
303 console_tstc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100304#ifdef CONFIG_WATCHDOG
305 /*
306 * If the watchdog must be rate-limited then it should
307 * already be handled in board-specific code.
308 */
309 udelay(1);
310#endif
311 }
312#else
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100313 return console_getc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100314#endif
315 }
wdenk47d1a6e2002-11-03 00:01:44 +0000316
317 return -1;
318}
319
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100320int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000321{
322 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100323 return console_tstc(file);
wdenk47d1a6e2002-11-03 00:01:44 +0000324
325 return -1;
326}
327
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100328void fputc(int file, const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000329{
330 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100331 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000332}
333
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100334void fputs(int file, const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000335{
336 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100337 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000338}
339
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200340int fprintf(int file, const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000341{
342 va_list args;
343 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200344 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000345
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100346 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000347
348 /* For this to work, printbuffer must be larger than
349 * anything we ever want to print.
350 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000351 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100352 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000353
354 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100355 fputs(file, printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200356 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000357}
358
359/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
360
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100361int getc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000362{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100363#ifdef CONFIG_DISABLE_CONSOLE
364 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
365 return 0;
366#endif
367
Graeme Russe3e454c2011-08-29 02:14:05 +0000368 if (!gd->have_console)
369 return 0;
370
wdenk47d1a6e2002-11-03 00:01:44 +0000371 if (gd->flags & GD_FLG_DEVINIT) {
372 /* Get from the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100373 return fgetc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000374 }
375
376 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100377 return serial_getc();
wdenk47d1a6e2002-11-03 00:01:44 +0000378}
379
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100380int tstc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000381{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100382#ifdef CONFIG_DISABLE_CONSOLE
383 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
384 return 0;
385#endif
386
Graeme Russe3e454c2011-08-29 02:14:05 +0000387 if (!gd->have_console)
388 return 0;
389
wdenk47d1a6e2002-11-03 00:01:44 +0000390 if (gd->flags & GD_FLG_DEVINIT) {
391 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100392 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000393 }
394
395 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100396 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000397}
398
Simon Glass3fa49772012-03-19 21:59:14 -0700399#ifdef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russ9558b482011-09-01 00:48:27 +0000400#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
401
402static void pre_console_putc(const char c)
403{
404 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
405
406 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
407}
408
409static void pre_console_puts(const char *s)
410{
411 while (*s)
412 pre_console_putc(*s++);
413}
414
415static void print_pre_console_buffer(void)
416{
417 unsigned long i = 0;
418 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
419
420 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
421 i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
422
423 while (i < gd->precon_buf_idx)
424 putc(buffer[CIRC_BUF_IDX(i++)]);
425}
426#else
427static inline void pre_console_putc(const char c) {}
428static inline void pre_console_puts(const char *s) {}
429static inline void print_pre_console_buffer(void) {}
430#endif
431
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100432void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000433{
wdenka6cccae2004-02-06 21:48:22 +0000434#ifdef CONFIG_SILENT_CONSOLE
435 if (gd->flags & GD_FLG_SILENT)
wdenkf6e20fc2004-02-08 19:38:38 +0000436 return;
wdenka6cccae2004-02-06 21:48:22 +0000437#endif
438
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100439#ifdef CONFIG_DISABLE_CONSOLE
440 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
441 return;
442#endif
443
Graeme Russe3e454c2011-08-29 02:14:05 +0000444 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000445 return pre_console_putc(c);
Graeme Russe3e454c2011-08-29 02:14:05 +0000446
wdenk47d1a6e2002-11-03 00:01:44 +0000447 if (gd->flags & GD_FLG_DEVINIT) {
448 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100449 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000450 } else {
451 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100452 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000453 }
454}
455
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100456void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000457{
wdenka6cccae2004-02-06 21:48:22 +0000458#ifdef CONFIG_SILENT_CONSOLE
459 if (gd->flags & GD_FLG_SILENT)
460 return;
461#endif
462
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100463#ifdef CONFIG_DISABLE_CONSOLE
464 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
465 return;
466#endif
467
Graeme Russe3e454c2011-08-29 02:14:05 +0000468 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000469 return pre_console_puts(s);
Graeme Russe3e454c2011-08-29 02:14:05 +0000470
wdenk47d1a6e2002-11-03 00:01:44 +0000471 if (gd->flags & GD_FLG_DEVINIT) {
472 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100473 fputs(stdout, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000474 } else {
475 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100476 serial_puts(s);
wdenk47d1a6e2002-11-03 00:01:44 +0000477 }
478}
479
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200480int printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000481{
482 va_list args;
483 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200484 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000485
Graeme Russ9558b482011-09-01 00:48:27 +0000486#ifndef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russe3e454c2011-08-29 02:14:05 +0000487 if (!gd->have_console)
488 return 0;
Graeme Russ9558b482011-09-01 00:48:27 +0000489#endif
Graeme Russe3e454c2011-08-29 02:14:05 +0000490
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100491 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000492
493 /* For this to work, printbuffer must be larger than
494 * anything we ever want to print.
495 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000496 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100497 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000498
499 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100500 puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200501 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000502}
503
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200504int vprintf(const char *fmt, va_list args)
wdenk6dd652f2003-06-19 23:40:20 +0000505{
506 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200507 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk6dd652f2003-06-19 23:40:20 +0000508
Graeme Russ9558b482011-09-01 00:48:27 +0000509#ifndef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russe3e454c2011-08-29 02:14:05 +0000510 if (!gd->have_console)
511 return 0;
Graeme Russ9558b482011-09-01 00:48:27 +0000512#endif
Graeme Russe3e454c2011-08-29 02:14:05 +0000513
wdenk6dd652f2003-06-19 23:40:20 +0000514 /* 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 = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk6dd652f2003-06-19 23:40:20 +0000518
519 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100520 puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200521 return i;
wdenk6dd652f2003-06-19 23:40:20 +0000522}
523
wdenk47d1a6e2002-11-03 00:01:44 +0000524/* test if ctrl-c was pressed */
525static int ctrlc_disabled = 0; /* see disable_ctrl() */
526static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100527int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000528{
wdenk47d1a6e2002-11-03 00:01:44 +0000529 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100530 if (tstc()) {
531 switch (getc()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000532 case 0x03: /* ^C - Control C */
533 ctrlc_was_pressed = 1;
534 return 1;
535 default:
536 break;
537 }
538 }
539 }
540 return 0;
541}
542
543/* pass 1 to disable ctrlc() checking, 0 to enable.
544 * returns previous state
545 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100546int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000547{
548 int prev = ctrlc_disabled; /* save previous state */
549
550 ctrlc_disabled = disable;
551 return prev;
552}
553
554int had_ctrlc (void)
555{
556 return ctrlc_was_pressed;
557}
558
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100559void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000560{
561 ctrlc_was_pressed = 0;
562}
563
564#ifdef CONFIG_MODEM_SUPPORT_DEBUG
565char screen[1024];
566char *cursor = screen;
567int once = 0;
568inline void dbg(const char *fmt, ...)
569{
570 va_list args;
571 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200572 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000573
574 if (!once) {
575 memset(screen, 0, sizeof(screen));
576 once++;
577 }
578
579 va_start(args, fmt);
580
581 /* For this to work, printbuffer must be larger than
582 * anything we ever want to print.
583 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000584 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk47d1a6e2002-11-03 00:01:44 +0000585 va_end(args);
586
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100587 if ((screen + sizeof(screen) - 1 - cursor)
588 < strlen(printbuffer) + 1) {
wdenk47d1a6e2002-11-03 00:01:44 +0000589 memset(screen, 0, sizeof(screen));
590 cursor = screen;
591 }
592 sprintf(cursor, printbuffer);
593 cursor += strlen(printbuffer);
594
595}
596#else
597inline void dbg(const char *fmt, ...)
598{
599}
600#endif
601
602/** U-Boot INIT FUNCTIONS *************************************************/
603
Mike Frysingerd7be3052010-10-20 07:18:03 -0400604struct stdio_dev *search_device(int flags, const char *name)
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200605{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200606 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200607
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200608 dev = stdio_get_by_name(name);
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200609
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100610 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200611 return dev;
612
613 return NULL;
614}
615
Mike Frysingerd7be3052010-10-20 07:18:03 -0400616int console_assign(int file, const char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000617{
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200618 int flag;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200619 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000620
621 /* Check for valid file */
622 switch (file) {
623 case stdin:
624 flag = DEV_FLAGS_INPUT;
625 break;
626 case stdout:
627 case stderr:
628 flag = DEV_FLAGS_OUTPUT;
629 break;
630 default:
631 return -1;
632 }
633
634 /* Check for valid device name */
635
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200636 dev = search_device(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000637
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100638 if (dev)
639 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000640
641 return -1;
642}
643
644/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100645int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000646{
wdenk47d1a6e2002-11-03 00:01:44 +0000647 gd->have_console = 1;
wdenkf72da342003-10-10 10:05:42 +0000648
649#ifdef CONFIG_SILENT_CONSOLE
650 if (getenv("silent") != NULL)
651 gd->flags |= GD_FLG_SILENT;
652#endif
653
Graeme Russ9558b482011-09-01 00:48:27 +0000654 print_pre_console_buffer();
655
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100656 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000657}
658
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200659void stdio_print_current_devices(void)
660{
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200661 /* Print information */
662 puts("In: ");
663 if (stdio_devices[stdin] == NULL) {
664 puts("No input devices available!\n");
665 } else {
666 printf ("%s\n", stdio_devices[stdin]->name);
667 }
668
669 puts("Out: ");
670 if (stdio_devices[stdout] == NULL) {
671 puts("No output devices available!\n");
672 } else {
673 printf ("%s\n", stdio_devices[stdout]->name);
674 }
675
676 puts("Err: ");
677 if (stdio_devices[stderr] == NULL) {
678 puts("No error devices available!\n");
679 } else {
680 printf ("%s\n", stdio_devices[stderr]->name);
681 }
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200682}
683
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200684#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +0000685/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100686int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000687{
688 char *stdinname, *stdoutname, *stderrname;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200689 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200690#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk6e592382004-04-18 17:39:38 +0000691 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200692#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100693#ifdef CONFIG_CONSOLE_MUX
694 int iomux_err = 0;
695#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000696
697 /* set default handlers at first */
wdenk27b207f2003-07-24 23:38:38 +0000698 gd->jt[XF_getc] = serial_getc;
699 gd->jt[XF_tstc] = serial_tstc;
700 gd->jt[XF_putc] = serial_putc;
701 gd->jt[XF_puts] = serial_puts;
702 gd->jt[XF_printf] = serial_printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000703
704 /* stdin stdout and stderr are in environment */
705 /* scan for it */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100706 stdinname = getenv("stdin");
707 stdoutname = getenv("stdout");
708 stderrname = getenv("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +0000709
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200710 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100711 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
712 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
713 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100714#ifdef CONFIG_CONSOLE_MUX
715 iomux_err = iomux_doenv(stdin, stdinname);
716 iomux_err += iomux_doenv(stdout, stdoutname);
717 iomux_err += iomux_doenv(stderr, stderrname);
718 if (!iomux_err)
719 /* Successful, so skip all the code below. */
720 goto done;
721#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000722 }
723 /* if the devices are overwritten or not found, use default device */
724 if (inputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100725 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000726 }
727 if (outputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100728 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000729 }
730 if (errdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100731 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000732 }
733 /* Initializes output console first */
734 if (outputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100735 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100736 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000737 }
738 if (errdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100739 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100740 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000741 }
742 if (inputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100743 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100744 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000745 }
746
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100747#ifdef CONFIG_CONSOLE_MUX
748done:
749#endif
750
Simon Glass78c112c2012-12-05 14:46:43 +0000751#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200752 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000753#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000754
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200755#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk47d1a6e2002-11-03 00:01:44 +0000756 /* set the environment variables (will overwrite previous env settings) */
757 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100758 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000759 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200760#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk47d1a6e2002-11-03 00:01:44 +0000761
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600762 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
763
wdenk47d1a6e2002-11-03 00:01:44 +0000764#if 0
765 /* If nothing usable installed, use only the initial console */
766 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100767 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000768#endif
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100769 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000770}
771
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200772#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000773
774/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100775int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000776{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200777 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200778 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200779 struct list_head *list = stdio_get_list();
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200780 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200781 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000782
wdenkd791b1d2003-04-20 14:04:18 +0000783#ifdef CONFIG_SPLASH_SCREEN
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100784 /*
785 * suppress all output if splash screen is enabled and we have
Anatolij Gustschina7490812010-03-16 15:29:33 +0100786 * a bmp to display. We redirect the output from frame buffer
787 * console to serial console in this case or suppress it if
788 * "silent" mode was requested.
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100789 */
Anatolij Gustschina7490812010-03-16 15:29:33 +0100790 if (getenv("splashimage") != NULL) {
791 if (!(gd->flags & GD_FLG_SILENT))
792 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
793 }
wdenkf72da342003-10-10 10:05:42 +0000794#endif
795
wdenk47d1a6e2002-11-03 00:01:44 +0000796 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200797 list_for_each(pos, list) {
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200798 dev = list_entry(pos, struct stdio_dev, list);
wdenk47d1a6e2002-11-03 00:01:44 +0000799
800 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
801 inputdev = dev;
802 }
803 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
804 outputdev = dev;
805 }
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200806 if(inputdev && outputdev)
807 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000808 }
809
810 /* Initializes output console first */
811 if (outputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100812 console_setfile(stdout, outputdev);
813 console_setfile(stderr, outputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100814#ifdef CONFIG_CONSOLE_MUX
815 console_devices[stdout][0] = outputdev;
816 console_devices[stderr][0] = outputdev;
817#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000818 }
819
820 /* Initializes input console */
821 if (inputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100822 console_setfile(stdin, inputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100823#ifdef CONFIG_CONSOLE_MUX
824 console_devices[stdin][0] = inputdev;
825#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000826 }
827
Simon Glass78c112c2012-12-05 14:46:43 +0000828#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200829 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000830#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000831
832 /* Setting environment variables */
833 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100834 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000835 }
836
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600837 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
838
wdenk47d1a6e2002-11-03 00:01:44 +0000839#if 0
840 /* If nothing usable installed, use only the initial console */
841 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100842 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000843#endif
844
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100845 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000846}
847
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200848#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */