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