wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <stdarg.h> |
| 10 | #include <malloc.h> |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 11 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 12 | #include <stdio_dev.h> |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 13 | #include <exports.h> |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 14 | #include <environment.h> |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 15 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 18 | static int on_console(const char *name, const char *value, enum env_op op, |
| 19 | int flags) |
| 20 | { |
| 21 | int console = -1; |
| 22 | |
| 23 | /* Check for console redirection */ |
| 24 | if (strcmp(name, "stdin") == 0) |
| 25 | console = stdin; |
| 26 | else if (strcmp(name, "stdout") == 0) |
| 27 | console = stdout; |
| 28 | else if (strcmp(name, "stderr") == 0) |
| 29 | console = stderr; |
| 30 | |
| 31 | /* if not actually setting a console variable, we don't care */ |
| 32 | if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0) |
| 33 | return 0; |
| 34 | |
| 35 | switch (op) { |
| 36 | case env_op_create: |
| 37 | case env_op_overwrite: |
| 38 | |
| 39 | #ifdef CONFIG_CONSOLE_MUX |
| 40 | if (iomux_doenv(console, value)) |
| 41 | return 1; |
| 42 | #else |
| 43 | /* Try assigning specified device */ |
| 44 | if (console_assign(console, value) < 0) |
| 45 | return 1; |
| 46 | #endif /* CONFIG_CONSOLE_MUX */ |
| 47 | return 0; |
| 48 | |
| 49 | case env_op_delete: |
| 50 | if ((flags & H_FORCE) == 0) |
| 51 | printf("Can't delete \"%s\"\n", name); |
| 52 | return 1; |
| 53 | |
| 54 | default: |
| 55 | return 0; |
| 56 | } |
| 57 | } |
| 58 | U_BOOT_ENV_CALLBACK(console, on_console); |
| 59 | |
Joe Hershberger | e080d54 | 2012-12-11 22:16:30 -0600 | [diff] [blame] | 60 | #ifdef CONFIG_SILENT_CONSOLE |
| 61 | static int on_silent(const char *name, const char *value, enum env_op op, |
| 62 | int flags) |
| 63 | { |
| 64 | #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET |
| 65 | if (flags & H_INTERACTIVE) |
| 66 | return 0; |
| 67 | #endif |
| 68 | #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC |
| 69 | if ((flags & H_INTERACTIVE) == 0) |
| 70 | return 0; |
| 71 | #endif |
| 72 | |
| 73 | if (value != NULL) |
| 74 | gd->flags |= GD_FLG_SILENT; |
| 75 | else |
| 76 | gd->flags &= ~GD_FLG_SILENT; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | U_BOOT_ENV_CALLBACK(silent, on_silent); |
| 81 | #endif |
| 82 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 83 | #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 84 | /* |
| 85 | * if overwrite_console returns 1, the stdin, stderr and stdout |
| 86 | * are switched to the serial port, else the settings in the |
| 87 | * environment are used |
| 88 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 89 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 90 | extern int overwrite_console(void); |
| 91 | #define OVERWRITE_CONSOLE overwrite_console() |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 92 | #else |
wdenk | 83e40ba | 2005-03-31 18:42:15 +0000 | [diff] [blame] | 93 | #define OVERWRITE_CONSOLE 0 |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 94 | #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 95 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 97 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 98 | static int console_setfile(int file, struct stdio_dev * dev) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 99 | { |
| 100 | int error = 0; |
| 101 | |
| 102 | if (dev == NULL) |
| 103 | return -1; |
| 104 | |
| 105 | switch (file) { |
| 106 | case stdin: |
| 107 | case stdout: |
| 108 | case stderr: |
| 109 | /* Start new device */ |
| 110 | if (dev->start) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 111 | error = dev->start(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 112 | /* If it's not started dont use it */ |
| 113 | if (error < 0) |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | /* Assign the new device (leaving the existing one started) */ |
| 118 | stdio_devices[file] = dev; |
| 119 | |
| 120 | /* |
| 121 | * Update monitor functions |
| 122 | * (to use the console stuff by other applications) |
| 123 | */ |
| 124 | switch (file) { |
| 125 | case stdin: |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 126 | gd->jt[XF_getc] = dev->getc; |
| 127 | gd->jt[XF_tstc] = dev->tstc; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 128 | break; |
| 129 | case stdout: |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 130 | gd->jt[XF_putc] = dev->putc; |
| 131 | gd->jt[XF_puts] = dev->puts; |
| 132 | gd->jt[XF_printf] = printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 133 | break; |
| 134 | } |
| 135 | break; |
| 136 | |
| 137 | default: /* Invalid file ID */ |
| 138 | error = -1; |
| 139 | } |
| 140 | return error; |
| 141 | } |
| 142 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 143 | #if defined(CONFIG_CONSOLE_MUX) |
| 144 | /** Console I/O multiplexing *******************************************/ |
| 145 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 146 | static struct stdio_dev *tstcdev; |
| 147 | struct stdio_dev **console_devices[MAX_FILES]; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 148 | int cd_count[MAX_FILES]; |
| 149 | |
| 150 | /* |
| 151 | * This depends on tstc() always being called before getc(). |
| 152 | * This is guaranteed to be true because this routine is called |
| 153 | * only from fgetc() which assures it. |
| 154 | * No attempt is made to demultiplex multiple input sources. |
| 155 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 156 | static int console_getc(int file) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 157 | { |
| 158 | unsigned char ret; |
| 159 | |
| 160 | /* This is never called with testcdev == NULL */ |
| 161 | ret = tstcdev->getc(); |
| 162 | tstcdev = NULL; |
| 163 | return ret; |
| 164 | } |
| 165 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 166 | static int console_tstc(int file) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 167 | { |
| 168 | int i, ret; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 169 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 170 | |
| 171 | disable_ctrlc(1); |
| 172 | for (i = 0; i < cd_count[file]; i++) { |
| 173 | dev = console_devices[file][i]; |
| 174 | if (dev->tstc != NULL) { |
| 175 | ret = dev->tstc(); |
| 176 | if (ret > 0) { |
| 177 | tstcdev = dev; |
| 178 | disable_ctrlc(0); |
| 179 | return ret; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | disable_ctrlc(0); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 188 | static void console_putc(int file, const char c) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 189 | { |
| 190 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 191 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 192 | |
| 193 | for (i = 0; i < cd_count[file]; i++) { |
| 194 | dev = console_devices[file][i]; |
| 195 | if (dev->putc != NULL) |
| 196 | dev->putc(c); |
| 197 | } |
| 198 | } |
| 199 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 200 | static void console_puts(int file, const char *s) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 201 | { |
| 202 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 203 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 204 | |
| 205 | for (i = 0; i < cd_count[file]; i++) { |
| 206 | dev = console_devices[file][i]; |
| 207 | if (dev->puts != NULL) |
| 208 | dev->puts(s); |
| 209 | } |
| 210 | } |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 211 | |
| 212 | static inline void console_printdevs(int file) |
| 213 | { |
| 214 | iomux_printdevs(file); |
| 215 | } |
| 216 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 217 | static inline void console_doenv(int file, struct stdio_dev *dev) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 218 | { |
| 219 | iomux_doenv(file, dev->name); |
| 220 | } |
| 221 | #else |
| 222 | static inline int console_getc(int file) |
| 223 | { |
| 224 | return stdio_devices[file]->getc(); |
| 225 | } |
| 226 | |
| 227 | static inline int console_tstc(int file) |
| 228 | { |
| 229 | return stdio_devices[file]->tstc(); |
| 230 | } |
| 231 | |
| 232 | static inline void console_putc(int file, const char c) |
| 233 | { |
| 234 | stdio_devices[file]->putc(c); |
| 235 | } |
| 236 | |
| 237 | static inline void console_puts(int file, const char *s) |
| 238 | { |
| 239 | stdio_devices[file]->puts(s); |
| 240 | } |
| 241 | |
| 242 | static inline void console_printdevs(int file) |
| 243 | { |
| 244 | printf("%s\n", stdio_devices[file]->name); |
| 245 | } |
| 246 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 247 | static inline void console_doenv(int file, struct stdio_dev *dev) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 248 | { |
| 249 | console_setfile(file, dev); |
| 250 | } |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 251 | #endif /* defined(CONFIG_CONSOLE_MUX) */ |
| 252 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 253 | /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/ |
| 254 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 255 | int serial_printf(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 256 | { |
| 257 | va_list args; |
| 258 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 259 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 260 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 261 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 262 | |
| 263 | /* For this to work, printbuffer must be larger than |
| 264 | * anything we ever want to print. |
| 265 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 266 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 267 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 268 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 269 | serial_puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 270 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 273 | int fgetc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 274 | { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 275 | if (file < MAX_FILES) { |
| 276 | #if defined(CONFIG_CONSOLE_MUX) |
| 277 | /* |
| 278 | * Effectively poll for input wherever it may be available. |
| 279 | */ |
| 280 | for (;;) { |
| 281 | /* |
| 282 | * Upper layer may have already called tstc() so |
| 283 | * check for that first. |
| 284 | */ |
| 285 | if (tstcdev != NULL) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 286 | return console_getc(file); |
| 287 | console_tstc(file); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 288 | #ifdef CONFIG_WATCHDOG |
| 289 | /* |
| 290 | * If the watchdog must be rate-limited then it should |
| 291 | * already be handled in board-specific code. |
| 292 | */ |
| 293 | udelay(1); |
| 294 | #endif |
| 295 | } |
| 296 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 297 | return console_getc(file); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 298 | #endif |
| 299 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 300 | |
| 301 | return -1; |
| 302 | } |
| 303 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 304 | int ftstc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 305 | { |
| 306 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 307 | return console_tstc(file); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 308 | |
| 309 | return -1; |
| 310 | } |
| 311 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 312 | void fputc(int file, const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 313 | { |
| 314 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 315 | console_putc(file, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 318 | void fputs(int file, const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 319 | { |
| 320 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 321 | console_puts(file, s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 324 | int fprintf(int file, const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 325 | { |
| 326 | va_list args; |
| 327 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 328 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 329 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 330 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 331 | |
| 332 | /* For this to work, printbuffer must be larger than |
| 333 | * anything we ever want to print. |
| 334 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 335 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 336 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 337 | |
| 338 | /* Send to desired file */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 339 | fputs(file, printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 340 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/ |
| 344 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 345 | int getc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 346 | { |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 347 | #ifdef CONFIG_DISABLE_CONSOLE |
| 348 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 349 | return 0; |
| 350 | #endif |
| 351 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 352 | if (!gd->have_console) |
| 353 | return 0; |
| 354 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 355 | if (gd->flags & GD_FLG_DEVINIT) { |
| 356 | /* Get from the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 357 | return fgetc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 361 | return serial_getc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 364 | int tstc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 365 | { |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 366 | #ifdef CONFIG_DISABLE_CONSOLE |
| 367 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 368 | return 0; |
| 369 | #endif |
| 370 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 371 | if (!gd->have_console) |
| 372 | return 0; |
| 373 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 374 | if (gd->flags & GD_FLG_DEVINIT) { |
| 375 | /* Test the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 376 | return ftstc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 380 | return serial_tstc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Simon Glass | 3fa4977 | 2012-03-19 21:59:14 -0700 | [diff] [blame] | 383 | #ifdef CONFIG_PRE_CONSOLE_BUFFER |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 384 | #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ) |
| 385 | |
| 386 | static void pre_console_putc(const char c) |
| 387 | { |
| 388 | char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR; |
| 389 | |
| 390 | buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c; |
| 391 | } |
| 392 | |
| 393 | static void pre_console_puts(const char *s) |
| 394 | { |
| 395 | while (*s) |
| 396 | pre_console_putc(*s++); |
| 397 | } |
| 398 | |
| 399 | static void print_pre_console_buffer(void) |
| 400 | { |
| 401 | unsigned long i = 0; |
| 402 | char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR; |
| 403 | |
| 404 | if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ) |
| 405 | i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ; |
| 406 | |
| 407 | while (i < gd->precon_buf_idx) |
| 408 | putc(buffer[CIRC_BUF_IDX(i++)]); |
| 409 | } |
| 410 | #else |
| 411 | static inline void pre_console_putc(const char c) {} |
| 412 | static inline void pre_console_puts(const char *s) {} |
| 413 | static inline void print_pre_console_buffer(void) {} |
| 414 | #endif |
| 415 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 416 | void putc(const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 417 | { |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 418 | #ifdef CONFIG_SILENT_CONSOLE |
| 419 | if (gd->flags & GD_FLG_SILENT) |
wdenk | f6e20fc | 2004-02-08 19:38:38 +0000 | [diff] [blame] | 420 | return; |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 421 | #endif |
| 422 | |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 423 | #ifdef CONFIG_DISABLE_CONSOLE |
| 424 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 425 | return; |
| 426 | #endif |
| 427 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 428 | if (!gd->have_console) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 429 | return pre_console_putc(c); |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 430 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 431 | if (gd->flags & GD_FLG_DEVINIT) { |
| 432 | /* Send to the standard output */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 433 | fputc(stdout, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 434 | } else { |
| 435 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 436 | serial_putc(c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 440 | void puts(const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 441 | { |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 442 | #ifdef CONFIG_SILENT_CONSOLE |
| 443 | if (gd->flags & GD_FLG_SILENT) |
| 444 | return; |
| 445 | #endif |
| 446 | |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 447 | #ifdef CONFIG_DISABLE_CONSOLE |
| 448 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 449 | return; |
| 450 | #endif |
| 451 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 452 | if (!gd->have_console) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 453 | return pre_console_puts(s); |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 454 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 455 | if (gd->flags & GD_FLG_DEVINIT) { |
| 456 | /* Send to the standard output */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 457 | fputs(stdout, s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 458 | } else { |
| 459 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 460 | serial_puts(s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 464 | int printf(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 465 | { |
| 466 | va_list args; |
| 467 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 468 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 469 | |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 470 | #ifndef CONFIG_PRE_CONSOLE_BUFFER |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 471 | if (!gd->have_console) |
| 472 | return 0; |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 473 | #endif |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 474 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 475 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 476 | |
| 477 | /* For this to work, printbuffer must be larger than |
| 478 | * anything we ever want to print. |
| 479 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 480 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 481 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 482 | |
| 483 | /* Print the string */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 484 | puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 485 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 488 | int vprintf(const char *fmt, va_list args) |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 489 | { |
| 490 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 491 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 492 | |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 493 | #ifndef CONFIG_PRE_CONSOLE_BUFFER |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 494 | if (!gd->have_console) |
| 495 | return 0; |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 496 | #endif |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 497 | |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 498 | /* For this to work, printbuffer must be larger than |
| 499 | * anything we ever want to print. |
| 500 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 501 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 502 | |
| 503 | /* Print the string */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 504 | puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 505 | return i; |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 506 | } |
| 507 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 508 | /* test if ctrl-c was pressed */ |
| 509 | static int ctrlc_disabled = 0; /* see disable_ctrl() */ |
| 510 | static int ctrlc_was_pressed = 0; |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 511 | int ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 512 | { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 513 | if (!ctrlc_disabled && gd->have_console) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 514 | if (tstc()) { |
| 515 | switch (getc()) { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 516 | case 0x03: /* ^C - Control C */ |
| 517 | ctrlc_was_pressed = 1; |
| 518 | return 1; |
| 519 | default: |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* pass 1 to disable ctrlc() checking, 0 to enable. |
| 528 | * returns previous state |
| 529 | */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 530 | int disable_ctrlc(int disable) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 531 | { |
| 532 | int prev = ctrlc_disabled; /* save previous state */ |
| 533 | |
| 534 | ctrlc_disabled = disable; |
| 535 | return prev; |
| 536 | } |
| 537 | |
| 538 | int had_ctrlc (void) |
| 539 | { |
| 540 | return ctrlc_was_pressed; |
| 541 | } |
| 542 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 543 | void clear_ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 544 | { |
| 545 | ctrlc_was_pressed = 0; |
| 546 | } |
| 547 | |
| 548 | #ifdef CONFIG_MODEM_SUPPORT_DEBUG |
| 549 | char screen[1024]; |
| 550 | char *cursor = screen; |
| 551 | int once = 0; |
| 552 | inline void dbg(const char *fmt, ...) |
| 553 | { |
| 554 | va_list args; |
| 555 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 556 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 557 | |
| 558 | if (!once) { |
| 559 | memset(screen, 0, sizeof(screen)); |
| 560 | once++; |
| 561 | } |
| 562 | |
| 563 | va_start(args, fmt); |
| 564 | |
| 565 | /* For this to work, printbuffer must be larger than |
| 566 | * anything we ever want to print. |
| 567 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 568 | i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 569 | va_end(args); |
| 570 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 571 | if ((screen + sizeof(screen) - 1 - cursor) |
| 572 | < strlen(printbuffer) + 1) { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 573 | memset(screen, 0, sizeof(screen)); |
| 574 | cursor = screen; |
| 575 | } |
| 576 | sprintf(cursor, printbuffer); |
| 577 | cursor += strlen(printbuffer); |
| 578 | |
| 579 | } |
| 580 | #else |
| 581 | inline void dbg(const char *fmt, ...) |
| 582 | { |
| 583 | } |
| 584 | #endif |
| 585 | |
| 586 | /** U-Boot INIT FUNCTIONS *************************************************/ |
| 587 | |
Mike Frysinger | d7be305 | 2010-10-20 07:18:03 -0400 | [diff] [blame] | 588 | struct stdio_dev *search_device(int flags, const char *name) |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 589 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 590 | struct stdio_dev *dev; |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 591 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 592 | dev = stdio_get_by_name(name); |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 593 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 594 | if (dev && (dev->flags & flags)) |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 595 | return dev; |
| 596 | |
| 597 | return NULL; |
| 598 | } |
| 599 | |
Mike Frysinger | d7be305 | 2010-10-20 07:18:03 -0400 | [diff] [blame] | 600 | int console_assign(int file, const char *devname) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 601 | { |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 602 | int flag; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 603 | struct stdio_dev *dev; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 604 | |
| 605 | /* Check for valid file */ |
| 606 | switch (file) { |
| 607 | case stdin: |
| 608 | flag = DEV_FLAGS_INPUT; |
| 609 | break; |
| 610 | case stdout: |
| 611 | case stderr: |
| 612 | flag = DEV_FLAGS_OUTPUT; |
| 613 | break; |
| 614 | default: |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | /* Check for valid device name */ |
| 619 | |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 620 | dev = search_device(flag, devname); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 621 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 622 | if (dev) |
| 623 | return console_setfile(file, dev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 624 | |
| 625 | return -1; |
| 626 | } |
| 627 | |
| 628 | /* Called before relocation - use serial functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 629 | int console_init_f(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 630 | { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 631 | gd->have_console = 1; |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 632 | |
| 633 | #ifdef CONFIG_SILENT_CONSOLE |
| 634 | if (getenv("silent") != NULL) |
| 635 | gd->flags |= GD_FLG_SILENT; |
| 636 | #endif |
| 637 | |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 638 | print_pre_console_buffer(); |
| 639 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 640 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 643 | void stdio_print_current_devices(void) |
| 644 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 645 | /* Print information */ |
| 646 | puts("In: "); |
| 647 | if (stdio_devices[stdin] == NULL) { |
| 648 | puts("No input devices available!\n"); |
| 649 | } else { |
| 650 | printf ("%s\n", stdio_devices[stdin]->name); |
| 651 | } |
| 652 | |
| 653 | puts("Out: "); |
| 654 | if (stdio_devices[stdout] == NULL) { |
| 655 | puts("No output devices available!\n"); |
| 656 | } else { |
| 657 | printf ("%s\n", stdio_devices[stdout]->name); |
| 658 | } |
| 659 | |
| 660 | puts("Err: "); |
| 661 | if (stdio_devices[stderr] == NULL) { |
| 662 | puts("No error devices available!\n"); |
| 663 | } else { |
| 664 | printf ("%s\n", stdio_devices[stderr]->name); |
| 665 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 666 | } |
| 667 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 668 | #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 669 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 670 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 671 | { |
| 672 | char *stdinname, *stdoutname, *stderrname; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 673 | struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 674 | #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 675 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 676 | #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */ |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 677 | #ifdef CONFIG_CONSOLE_MUX |
| 678 | int iomux_err = 0; |
| 679 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 680 | |
| 681 | /* set default handlers at first */ |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 682 | gd->jt[XF_getc] = serial_getc; |
| 683 | gd->jt[XF_tstc] = serial_tstc; |
| 684 | gd->jt[XF_putc] = serial_putc; |
| 685 | gd->jt[XF_puts] = serial_puts; |
| 686 | gd->jt[XF_printf] = serial_printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 687 | |
| 688 | /* stdin stdout and stderr are in environment */ |
| 689 | /* scan for it */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 690 | stdinname = getenv("stdin"); |
| 691 | stdoutname = getenv("stdout"); |
| 692 | stderrname = getenv("stderr"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 693 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 694 | if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 695 | inputdev = search_device(DEV_FLAGS_INPUT, stdinname); |
| 696 | outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname); |
| 697 | errdev = search_device(DEV_FLAGS_OUTPUT, stderrname); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 698 | #ifdef CONFIG_CONSOLE_MUX |
| 699 | iomux_err = iomux_doenv(stdin, stdinname); |
| 700 | iomux_err += iomux_doenv(stdout, stdoutname); |
| 701 | iomux_err += iomux_doenv(stderr, stderrname); |
| 702 | if (!iomux_err) |
| 703 | /* Successful, so skip all the code below. */ |
| 704 | goto done; |
| 705 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 706 | } |
| 707 | /* if the devices are overwritten or not found, use default device */ |
| 708 | if (inputdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 709 | inputdev = search_device(DEV_FLAGS_INPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 710 | } |
| 711 | if (outputdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 712 | outputdev = search_device(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 713 | } |
| 714 | if (errdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 715 | errdev = search_device(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 716 | } |
| 717 | /* Initializes output console first */ |
| 718 | if (outputdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 719 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 720 | console_doenv(stdout, outputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 721 | } |
| 722 | if (errdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 723 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 724 | console_doenv(stderr, errdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 725 | } |
| 726 | if (inputdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 727 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 728 | console_doenv(stdin, inputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 731 | #ifdef CONFIG_CONSOLE_MUX |
| 732 | done: |
| 733 | #endif |
| 734 | |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 735 | #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 736 | stdio_print_current_devices(); |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 737 | #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 738 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 739 | #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 740 | /* set the environment variables (will overwrite previous env settings) */ |
| 741 | for (i = 0; i < 3; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 742 | setenv(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 743 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 744 | #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 745 | |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 746 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 747 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 748 | #if 0 |
| 749 | /* If nothing usable installed, use only the initial console */ |
| 750 | if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL)) |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 751 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 752 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 753 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 756 | #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 757 | |
| 758 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 759 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 760 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 761 | struct stdio_dev *inputdev = NULL, *outputdev = NULL; |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 762 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 763 | struct list_head *list = stdio_get_list(); |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 764 | struct list_head *pos; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 765 | struct stdio_dev *dev; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 766 | |
wdenk | d791b1d | 2003-04-20 14:04:18 +0000 | [diff] [blame] | 767 | #ifdef CONFIG_SPLASH_SCREEN |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 768 | /* |
| 769 | * suppress all output if splash screen is enabled and we have |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 770 | * a bmp to display. We redirect the output from frame buffer |
| 771 | * console to serial console in this case or suppress it if |
| 772 | * "silent" mode was requested. |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 773 | */ |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 774 | if (getenv("splashimage") != NULL) { |
| 775 | if (!(gd->flags & GD_FLG_SILENT)) |
| 776 | outputdev = search_device (DEV_FLAGS_OUTPUT, "serial"); |
| 777 | } |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 778 | #endif |
| 779 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 780 | /* Scan devices looking for input and output devices */ |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 781 | list_for_each(pos, list) { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 782 | dev = list_entry(pos, struct stdio_dev, list); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 783 | |
| 784 | if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) { |
| 785 | inputdev = dev; |
| 786 | } |
| 787 | if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) { |
| 788 | outputdev = dev; |
| 789 | } |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 790 | if(inputdev && outputdev) |
| 791 | break; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* Initializes output console first */ |
| 795 | if (outputdev != NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 796 | console_setfile(stdout, outputdev); |
| 797 | console_setfile(stderr, outputdev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 798 | #ifdef CONFIG_CONSOLE_MUX |
| 799 | console_devices[stdout][0] = outputdev; |
| 800 | console_devices[stderr][0] = outputdev; |
| 801 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* Initializes input console */ |
| 805 | if (inputdev != NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 806 | console_setfile(stdin, inputdev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 807 | #ifdef CONFIG_CONSOLE_MUX |
| 808 | console_devices[stdin][0] = inputdev; |
| 809 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 812 | #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 813 | stdio_print_current_devices(); |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 814 | #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 815 | |
| 816 | /* Setting environment variables */ |
| 817 | for (i = 0; i < 3; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 818 | setenv(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 821 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 822 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 823 | #if 0 |
| 824 | /* If nothing usable installed, use only the initial console */ |
| 825 | if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL)) |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 826 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 827 | #endif |
| 828 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 829 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 832 | #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |