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 | * |
| 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 Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 27 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 28 | #include <stdio_dev.h> |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 29 | #include <exports.h> |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 30 | #include <environment.h> |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 31 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 34 | static 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 | } |
| 74 | U_BOOT_ENV_CALLBACK(console, on_console); |
| 75 | |
Joe Hershberger | e080d54 | 2012-12-11 22:16:30 -0600 | [diff] [blame] | 76 | #ifdef CONFIG_SILENT_CONSOLE |
| 77 | static 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 | } |
| 96 | U_BOOT_ENV_CALLBACK(silent, on_silent); |
| 97 | #endif |
| 98 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 99 | #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 100 | /* |
| 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-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 105 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 106 | extern int overwrite_console(void); |
| 107 | #define OVERWRITE_CONSOLE overwrite_console() |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 108 | #else |
wdenk | 83e40ba | 2005-03-31 18:42:15 +0000 | [diff] [blame] | 109 | #define OVERWRITE_CONSOLE 0 |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 110 | #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 111 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 112 | #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 113 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 114 | static int console_setfile(int file, struct stdio_dev * dev) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 115 | { |
| 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-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 127 | error = dev->start(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 128 | /* 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: |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 142 | gd->jt[XF_getc] = dev->getc; |
| 143 | gd->jt[XF_tstc] = dev->tstc; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 144 | break; |
| 145 | case stdout: |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 146 | gd->jt[XF_putc] = dev->putc; |
| 147 | gd->jt[XF_puts] = dev->puts; |
| 148 | gd->jt[XF_printf] = printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | break; |
| 152 | |
| 153 | default: /* Invalid file ID */ |
| 154 | error = -1; |
| 155 | } |
| 156 | return error; |
| 157 | } |
| 158 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 159 | #if defined(CONFIG_CONSOLE_MUX) |
| 160 | /** Console I/O multiplexing *******************************************/ |
| 161 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 162 | static struct stdio_dev *tstcdev; |
| 163 | struct stdio_dev **console_devices[MAX_FILES]; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 164 | int 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-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 172 | static int console_getc(int file) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 173 | { |
| 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-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 182 | static int console_tstc(int file) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 183 | { |
| 184 | int i, ret; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 185 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 186 | |
| 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-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 204 | static void console_putc(int file, const char c) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 205 | { |
| 206 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 207 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 208 | |
| 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-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 216 | static void console_puts(int file, const char *s) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 217 | { |
| 218 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 219 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 220 | |
| 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-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 227 | |
| 228 | static inline void console_printdevs(int file) |
| 229 | { |
| 230 | iomux_printdevs(file); |
| 231 | } |
| 232 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 233 | 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] | 234 | { |
| 235 | iomux_doenv(file, dev->name); |
| 236 | } |
| 237 | #else |
| 238 | static inline int console_getc(int file) |
| 239 | { |
| 240 | return stdio_devices[file]->getc(); |
| 241 | } |
| 242 | |
| 243 | static inline int console_tstc(int file) |
| 244 | { |
| 245 | return stdio_devices[file]->tstc(); |
| 246 | } |
| 247 | |
| 248 | static inline void console_putc(int file, const char c) |
| 249 | { |
| 250 | stdio_devices[file]->putc(c); |
| 251 | } |
| 252 | |
| 253 | static inline void console_puts(int file, const char *s) |
| 254 | { |
| 255 | stdio_devices[file]->puts(s); |
| 256 | } |
| 257 | |
| 258 | static inline void console_printdevs(int file) |
| 259 | { |
| 260 | printf("%s\n", stdio_devices[file]->name); |
| 261 | } |
| 262 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 263 | 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] | 264 | { |
| 265 | console_setfile(file, dev); |
| 266 | } |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 267 | #endif /* defined(CONFIG_CONSOLE_MUX) */ |
| 268 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 269 | /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/ |
| 270 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 271 | int serial_printf(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 272 | { |
| 273 | va_list args; |
| 274 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 275 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 276 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 277 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 278 | |
| 279 | /* For this to work, printbuffer must be larger than |
| 280 | * anything we ever want to print. |
| 281 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 282 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 283 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 284 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 285 | serial_puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 286 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 289 | int fgetc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 290 | { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 291 | 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-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 302 | return console_getc(file); |
| 303 | console_tstc(file); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 304 | #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-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 313 | return console_getc(file); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 314 | #endif |
| 315 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 316 | |
| 317 | return -1; |
| 318 | } |
| 319 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 320 | int ftstc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 321 | { |
| 322 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 323 | return console_tstc(file); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 324 | |
| 325 | return -1; |
| 326 | } |
| 327 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 328 | void fputc(int file, const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 329 | { |
| 330 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 331 | console_putc(file, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 334 | void fputs(int file, const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 335 | { |
| 336 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 337 | console_puts(file, s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 340 | int fprintf(int file, const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 341 | { |
| 342 | va_list args; |
| 343 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 344 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 345 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 346 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 347 | |
| 348 | /* For this to work, printbuffer must be larger than |
| 349 | * anything we ever want to print. |
| 350 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 351 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 352 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 353 | |
| 354 | /* Send to desired file */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 355 | fputs(file, printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 356 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/ |
| 360 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 361 | int getc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 362 | { |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 363 | #ifdef CONFIG_DISABLE_CONSOLE |
| 364 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 365 | return 0; |
| 366 | #endif |
| 367 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 368 | if (!gd->have_console) |
| 369 | return 0; |
| 370 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 371 | if (gd->flags & GD_FLG_DEVINIT) { |
| 372 | /* Get from the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 373 | return fgetc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 377 | return serial_getc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 380 | int tstc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 381 | { |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 382 | #ifdef CONFIG_DISABLE_CONSOLE |
| 383 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 384 | return 0; |
| 385 | #endif |
| 386 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 387 | if (!gd->have_console) |
| 388 | return 0; |
| 389 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 390 | if (gd->flags & GD_FLG_DEVINIT) { |
| 391 | /* Test the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 392 | return ftstc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 396 | return serial_tstc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Simon Glass | 3fa4977 | 2012-03-19 21:59:14 -0700 | [diff] [blame] | 399 | #ifdef CONFIG_PRE_CONSOLE_BUFFER |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 400 | #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ) |
| 401 | |
| 402 | static 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 | |
| 409 | static void pre_console_puts(const char *s) |
| 410 | { |
| 411 | while (*s) |
| 412 | pre_console_putc(*s++); |
| 413 | } |
| 414 | |
| 415 | static 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 |
| 427 | static inline void pre_console_putc(const char c) {} |
| 428 | static inline void pre_console_puts(const char *s) {} |
| 429 | static inline void print_pre_console_buffer(void) {} |
| 430 | #endif |
| 431 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 432 | void putc(const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 433 | { |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 434 | #ifdef CONFIG_SILENT_CONSOLE |
| 435 | if (gd->flags & GD_FLG_SILENT) |
wdenk | f6e20fc | 2004-02-08 19:38:38 +0000 | [diff] [blame] | 436 | return; |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 437 | #endif |
| 438 | |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 439 | #ifdef CONFIG_DISABLE_CONSOLE |
| 440 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 441 | return; |
| 442 | #endif |
| 443 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 444 | if (!gd->have_console) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 445 | return pre_console_putc(c); |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 446 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 447 | if (gd->flags & GD_FLG_DEVINIT) { |
| 448 | /* Send to the standard output */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 449 | fputc(stdout, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 450 | } else { |
| 451 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 452 | serial_putc(c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 456 | void puts(const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 457 | { |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 458 | #ifdef CONFIG_SILENT_CONSOLE |
| 459 | if (gd->flags & GD_FLG_SILENT) |
| 460 | return; |
| 461 | #endif |
| 462 | |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 463 | #ifdef CONFIG_DISABLE_CONSOLE |
| 464 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 465 | return; |
| 466 | #endif |
| 467 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 468 | if (!gd->have_console) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 469 | return pre_console_puts(s); |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 470 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 471 | if (gd->flags & GD_FLG_DEVINIT) { |
| 472 | /* Send to the standard output */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 473 | fputs(stdout, s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 474 | } else { |
| 475 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 476 | serial_puts(s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 480 | int printf(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 481 | { |
| 482 | va_list args; |
| 483 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 484 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 485 | |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 486 | #ifndef CONFIG_PRE_CONSOLE_BUFFER |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 487 | if (!gd->have_console) |
| 488 | return 0; |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 489 | #endif |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 490 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 491 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 492 | |
| 493 | /* For this to work, printbuffer must be larger than |
| 494 | * anything we ever want to print. |
| 495 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 496 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 497 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 498 | |
| 499 | /* Print the string */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 500 | puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 501 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 504 | int vprintf(const char *fmt, va_list args) |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 505 | { |
| 506 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 507 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 508 | |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 509 | #ifndef CONFIG_PRE_CONSOLE_BUFFER |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 510 | if (!gd->have_console) |
| 511 | return 0; |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 512 | #endif |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 513 | |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 514 | /* For this to work, printbuffer must be larger than |
| 515 | * anything we ever want to print. |
| 516 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 517 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 518 | |
| 519 | /* Print the string */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 520 | puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 521 | return i; |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 522 | } |
| 523 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 524 | /* test if ctrl-c was pressed */ |
| 525 | static int ctrlc_disabled = 0; /* see disable_ctrl() */ |
| 526 | static int ctrlc_was_pressed = 0; |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 527 | int ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 528 | { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 529 | if (!ctrlc_disabled && gd->have_console) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 530 | if (tstc()) { |
| 531 | switch (getc()) { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 532 | 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-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 546 | int disable_ctrlc(int disable) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 547 | { |
| 548 | int prev = ctrlc_disabled; /* save previous state */ |
| 549 | |
| 550 | ctrlc_disabled = disable; |
| 551 | return prev; |
| 552 | } |
| 553 | |
| 554 | int had_ctrlc (void) |
| 555 | { |
| 556 | return ctrlc_was_pressed; |
| 557 | } |
| 558 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 559 | void clear_ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 560 | { |
| 561 | ctrlc_was_pressed = 0; |
| 562 | } |
| 563 | |
| 564 | #ifdef CONFIG_MODEM_SUPPORT_DEBUG |
| 565 | char screen[1024]; |
| 566 | char *cursor = screen; |
| 567 | int once = 0; |
| 568 | inline void dbg(const char *fmt, ...) |
| 569 | { |
| 570 | va_list args; |
| 571 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 572 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 573 | |
| 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 Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 584 | i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 585 | va_end(args); |
| 586 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 587 | if ((screen + sizeof(screen) - 1 - cursor) |
| 588 | < strlen(printbuffer) + 1) { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 589 | memset(screen, 0, sizeof(screen)); |
| 590 | cursor = screen; |
| 591 | } |
| 592 | sprintf(cursor, printbuffer); |
| 593 | cursor += strlen(printbuffer); |
| 594 | |
| 595 | } |
| 596 | #else |
| 597 | inline void dbg(const char *fmt, ...) |
| 598 | { |
| 599 | } |
| 600 | #endif |
| 601 | |
| 602 | /** U-Boot INIT FUNCTIONS *************************************************/ |
| 603 | |
Mike Frysinger | d7be305 | 2010-10-20 07:18:03 -0400 | [diff] [blame] | 604 | struct stdio_dev *search_device(int flags, const char *name) |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 605 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 606 | struct stdio_dev *dev; |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 607 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 608 | dev = stdio_get_by_name(name); |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 609 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 610 | if (dev && (dev->flags & flags)) |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 611 | return dev; |
| 612 | |
| 613 | return NULL; |
| 614 | } |
| 615 | |
Mike Frysinger | d7be305 | 2010-10-20 07:18:03 -0400 | [diff] [blame] | 616 | int console_assign(int file, const char *devname) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 617 | { |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 618 | int flag; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 619 | struct stdio_dev *dev; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 620 | |
| 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-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 636 | dev = search_device(flag, devname); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 637 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 638 | if (dev) |
| 639 | return console_setfile(file, dev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 640 | |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | /* Called before relocation - use serial functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 645 | int console_init_f(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 646 | { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 647 | gd->have_console = 1; |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 648 | |
| 649 | #ifdef CONFIG_SILENT_CONSOLE |
| 650 | if (getenv("silent") != NULL) |
| 651 | gd->flags |= GD_FLG_SILENT; |
| 652 | #endif |
| 653 | |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 654 | print_pre_console_buffer(); |
| 655 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 656 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 659 | void stdio_print_current_devices(void) |
| 660 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 661 | /* 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-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 684 | #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 685 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 686 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 687 | { |
| 688 | char *stdinname, *stdoutname, *stderrname; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 689 | struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 690 | #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 691 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 692 | #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */ |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 693 | #ifdef CONFIG_CONSOLE_MUX |
| 694 | int iomux_err = 0; |
| 695 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 696 | |
| 697 | /* set default handlers at first */ |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 698 | 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; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 703 | |
| 704 | /* stdin stdout and stderr are in environment */ |
| 705 | /* scan for it */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 706 | stdinname = getenv("stdin"); |
| 707 | stdoutname = getenv("stdout"); |
| 708 | stderrname = getenv("stderr"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 709 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 710 | if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 711 | inputdev = search_device(DEV_FLAGS_INPUT, stdinname); |
| 712 | outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname); |
| 713 | errdev = search_device(DEV_FLAGS_OUTPUT, stderrname); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 714 | #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 |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 722 | } |
| 723 | /* if the devices are overwritten or not found, use default device */ |
| 724 | if (inputdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 725 | inputdev = search_device(DEV_FLAGS_INPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 726 | } |
| 727 | if (outputdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 728 | outputdev = search_device(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 729 | } |
| 730 | if (errdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 731 | errdev = search_device(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 732 | } |
| 733 | /* Initializes output console first */ |
| 734 | if (outputdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 735 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 736 | console_doenv(stdout, outputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 737 | } |
| 738 | if (errdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 739 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 740 | console_doenv(stderr, errdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 741 | } |
| 742 | if (inputdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 743 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 744 | console_doenv(stdin, inputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 747 | #ifdef CONFIG_CONSOLE_MUX |
| 748 | done: |
| 749 | #endif |
| 750 | |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 751 | #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 752 | stdio_print_current_devices(); |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 753 | #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 754 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 755 | #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 756 | /* set the environment variables (will overwrite previous env settings) */ |
| 757 | for (i = 0; i < 3; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 758 | setenv(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 759 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 760 | #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 761 | |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 762 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 763 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 764 | #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-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 767 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 768 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 769 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 772 | #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 773 | |
| 774 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 775 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 776 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 777 | struct stdio_dev *inputdev = NULL, *outputdev = NULL; |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 778 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 779 | struct list_head *list = stdio_get_list(); |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 780 | struct list_head *pos; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 781 | struct stdio_dev *dev; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 782 | |
wdenk | d791b1d | 2003-04-20 14:04:18 +0000 | [diff] [blame] | 783 | #ifdef CONFIG_SPLASH_SCREEN |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 784 | /* |
| 785 | * suppress all output if splash screen is enabled and we have |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 786 | * 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-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 789 | */ |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 790 | if (getenv("splashimage") != NULL) { |
| 791 | if (!(gd->flags & GD_FLG_SILENT)) |
| 792 | outputdev = search_device (DEV_FLAGS_OUTPUT, "serial"); |
| 793 | } |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 794 | #endif |
| 795 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 796 | /* Scan devices looking for input and output devices */ |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 797 | list_for_each(pos, list) { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 798 | dev = list_entry(pos, struct stdio_dev, list); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 799 | |
| 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-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 806 | if(inputdev && outputdev) |
| 807 | break; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* Initializes output console first */ |
| 811 | if (outputdev != NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 812 | console_setfile(stdout, outputdev); |
| 813 | console_setfile(stderr, outputdev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 814 | #ifdef CONFIG_CONSOLE_MUX |
| 815 | console_devices[stdout][0] = outputdev; |
| 816 | console_devices[stderr][0] = outputdev; |
| 817 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* Initializes input console */ |
| 821 | if (inputdev != NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 822 | console_setfile(stdin, inputdev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 823 | #ifdef CONFIG_CONSOLE_MUX |
| 824 | console_devices[stdin][0] = inputdev; |
| 825 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 828 | #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 829 | stdio_print_current_devices(); |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 830 | #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 831 | |
| 832 | /* Setting environment variables */ |
| 833 | for (i = 0; i < 3; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 834 | setenv(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 837 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 838 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 839 | #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-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 842 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 843 | #endif |
| 844 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 845 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 848 | #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |