Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2004 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 8 | #include <env_internal.h> |
Simon Glass | db41d65 | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 9 | #include <hang.h> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 10 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 11 | #include <stdio_dev.h> |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 12 | #include <post.h> |
| 13 | #include <linux/compiler.h> |
Marek Vasut | 6d93e25 | 2012-10-06 14:07:03 +0000 | [diff] [blame] | 14 | #include <errno.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 16 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 19 | static struct serial_device *serial_devices; |
| 20 | static struct serial_device *serial_current; |
Joe Hershberger | 3205771 | 2012-12-11 22:16:27 -0600 | [diff] [blame] | 21 | /* |
| 22 | * Table with supported baudrates (defined in config_xyz.h) |
| 23 | */ |
| 24 | static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 25 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 26 | /** |
| 27 | * serial_null() - Void registration routine of a serial driver |
| 28 | * |
| 29 | * This routine implements a void registration routine of a serial |
| 30 | * driver. The registration routine of a particular driver is aliased |
| 31 | * to this empty function in case the driver is not compiled into |
| 32 | * U-Boot. |
| 33 | */ |
Marek Vasut | 2a333ae | 2012-09-12 17:49:58 +0200 | [diff] [blame] | 34 | static void serial_null(void) |
| 35 | { |
| 36 | } |
| 37 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 38 | /** |
Joe Hershberger | 3205771 | 2012-12-11 22:16:27 -0600 | [diff] [blame] | 39 | * on_baudrate() - Update the actual baudrate when the env var changes |
| 40 | * |
Heinrich Schuchardt | 938b05a | 2018-07-29 10:41:02 +0200 | [diff] [blame] | 41 | * @name: changed environment variable |
| 42 | * @value: new value of the environment variable |
| 43 | * @op: operation (create, overwrite, or delete) |
| 44 | * @flags: attributes of environment variable change, |
| 45 | * see flags H_* in include/search.h |
| 46 | * |
Joe Hershberger | 3205771 | 2012-12-11 22:16:27 -0600 | [diff] [blame] | 47 | * This will check for a valid baudrate and only apply it if valid. |
Heinrich Schuchardt | 938b05a | 2018-07-29 10:41:02 +0200 | [diff] [blame] | 48 | * |
| 49 | * Return: 0 on success, 1 on error |
Joe Hershberger | 3205771 | 2012-12-11 22:16:27 -0600 | [diff] [blame] | 50 | */ |
| 51 | static int on_baudrate(const char *name, const char *value, enum env_op op, |
| 52 | int flags) |
| 53 | { |
| 54 | int i; |
| 55 | int baudrate; |
| 56 | |
| 57 | switch (op) { |
| 58 | case env_op_create: |
| 59 | case env_op_overwrite: |
| 60 | /* |
| 61 | * Switch to new baudrate if new baudrate is supported |
| 62 | */ |
| 63 | baudrate = simple_strtoul(value, NULL, 10); |
| 64 | |
| 65 | /* Not actually changing */ |
| 66 | if (gd->baudrate == baudrate) |
| 67 | return 0; |
| 68 | |
Axel Lin | 9935175 | 2013-06-23 00:46:41 +0800 | [diff] [blame] | 69 | for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { |
Joe Hershberger | 3205771 | 2012-12-11 22:16:27 -0600 | [diff] [blame] | 70 | if (baudrate == baudrate_table[i]) |
| 71 | break; |
| 72 | } |
Axel Lin | 9935175 | 2013-06-23 00:46:41 +0800 | [diff] [blame] | 73 | if (i == ARRAY_SIZE(baudrate_table)) { |
Joe Hershberger | 3205771 | 2012-12-11 22:16:27 -0600 | [diff] [blame] | 74 | if ((flags & H_FORCE) == 0) |
| 75 | printf("## Baudrate %d bps not supported\n", |
| 76 | baudrate); |
| 77 | return 1; |
| 78 | } |
| 79 | if ((flags & H_INTERACTIVE) != 0) { |
| 80 | printf("## Switch baudrate to %d" |
| 81 | " bps and press ENTER ...\n", baudrate); |
| 82 | udelay(50000); |
| 83 | } |
| 84 | |
| 85 | gd->baudrate = baudrate; |
Joe Hershberger | 3205771 | 2012-12-11 22:16:27 -0600 | [diff] [blame] | 86 | |
| 87 | serial_setbrg(); |
| 88 | |
| 89 | udelay(50000); |
| 90 | |
| 91 | if ((flags & H_INTERACTIVE) != 0) |
| 92 | while (1) { |
| 93 | if (getc() == '\r') |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | case env_op_delete: |
| 99 | printf("## Baudrate may not be deleted\n"); |
| 100 | return 1; |
| 101 | default: |
| 102 | return 0; |
| 103 | } |
| 104 | } |
| 105 | U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); |
| 106 | |
| 107 | /** |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 108 | * serial_initfunc() - Forward declare of driver registration routine |
| 109 | * @name: Name of the real driver registration routine. |
| 110 | * |
| 111 | * This macro expands onto forward declaration of a driver registration |
| 112 | * routine, which is then used below in serial_initialize() function. |
| 113 | * The declaration is made weak and aliases to serial_null() so in case |
| 114 | * the driver is not compiled in, the function is still declared and can |
| 115 | * be used, but aliases to serial_null() and thus is optimized away. |
| 116 | */ |
Marek Vasut | 2a333ae | 2012-09-12 17:49:58 +0200 | [diff] [blame] | 117 | #define serial_initfunc(name) \ |
| 118 | void name(void) \ |
| 119 | __attribute__((weak, alias("serial_null"))); |
| 120 | |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 121 | serial_initfunc(atmel_serial_initialize); |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 122 | serial_initfunc(mcf_serial_initialize); |
Marek Vasut | 7a31154 | 2012-09-13 01:38:52 +0200 | [diff] [blame] | 123 | serial_initfunc(mpc85xx_serial_initialize); |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 124 | serial_initfunc(mxc_serial_initialize); |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 125 | serial_initfunc(ns16550_serial_initialize); |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 126 | serial_initfunc(pl01x_serial_initialize); |
| 127 | serial_initfunc(pxa_serial_initialize); |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 128 | serial_initfunc(sh_serial_initialize); |
Weijie Gao | 44fa676 | 2019-09-25 17:45:18 +0800 | [diff] [blame] | 129 | serial_initfunc(mtk_serial_initialize); |
Marek Vasut | f0eb1f6 | 2012-09-12 13:50:56 +0200 | [diff] [blame] | 130 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 131 | /** |
| 132 | * serial_register() - Register serial driver with serial driver core |
| 133 | * @dev: Pointer to the serial driver structure |
| 134 | * |
| 135 | * This function registers the serial driver supplied via @dev with |
| 136 | * serial driver core, thus making U-Boot aware of it and making it |
| 137 | * available for U-Boot to use. On platforms that still require manual |
| 138 | * relocation of constant variables, relocation of the supplied structure |
| 139 | * is performed. |
| 140 | */ |
Mike Frysinger | c52b4f7 | 2011-04-29 18:03:30 +0000 | [diff] [blame] | 141 | void serial_register(struct serial_device *dev) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 142 | { |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 143 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Marek Vasut | f2760c4 | 2012-09-16 18:54:22 +0200 | [diff] [blame] | 144 | if (dev->start) |
| 145 | dev->start += gd->reloc_off; |
| 146 | if (dev->stop) |
| 147 | dev->stop += gd->reloc_off; |
| 148 | if (dev->setbrg) |
| 149 | dev->setbrg += gd->reloc_off; |
| 150 | if (dev->getc) |
| 151 | dev->getc += gd->reloc_off; |
| 152 | if (dev->tstc) |
| 153 | dev->tstc += gd->reloc_off; |
| 154 | if (dev->putc) |
| 155 | dev->putc += gd->reloc_off; |
| 156 | if (dev->puts) |
| 157 | dev->puts += gd->reloc_off; |
Peter Tyser | 521af04 | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 158 | #endif |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 159 | |
| 160 | dev->next = serial_devices; |
| 161 | serial_devices = dev; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 164 | /** |
| 165 | * serial_initialize() - Register all compiled-in serial port drivers |
| 166 | * |
| 167 | * This function registers all serial port drivers that are compiled |
| 168 | * into the U-Boot binary with the serial core, thus making them |
| 169 | * available to U-Boot to use. Lastly, this function assigns a default |
| 170 | * serial port to the serial core. That serial port is then used as a |
| 171 | * default output. |
| 172 | */ |
Ovidiu Panait | 39a1922 | 2020-07-24 14:12:22 +0300 | [diff] [blame] | 173 | int serial_initialize(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 174 | { |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 175 | atmel_serial_initialize(); |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 176 | mcf_serial_initialize(); |
Marek Vasut | 7a31154 | 2012-09-13 01:38:52 +0200 | [diff] [blame] | 177 | mpc85xx_serial_initialize(); |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 178 | mxc_serial_initialize(); |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 179 | ns16550_serial_initialize(); |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 180 | pl01x_serial_initialize(); |
| 181 | pxa_serial_initialize(); |
Jeroen Hofstee | 94a255d | 2014-10-27 20:10:07 +0100 | [diff] [blame] | 182 | sh_serial_initialize(); |
Weijie Gao | 44fa676 | 2019-09-25 17:45:18 +0800 | [diff] [blame] | 183 | mtk_serial_initialize(); |
Marek Vasut | 7b953c5 | 2012-09-13 01:16:50 +0200 | [diff] [blame] | 184 | |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 185 | serial_assign(default_serial_console()->name); |
Ovidiu Panait | 39a1922 | 2020-07-24 14:12:22 +0300 | [diff] [blame] | 186 | |
| 187 | return 0; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Jeroen Hofstee | 654f8d0 | 2014-10-08 22:57:44 +0200 | [diff] [blame] | 190 | static int serial_stub_start(struct stdio_dev *sdev) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 191 | { |
| 192 | struct serial_device *dev = sdev->priv; |
| 193 | |
| 194 | return dev->start(); |
| 195 | } |
| 196 | |
Jeroen Hofstee | 654f8d0 | 2014-10-08 22:57:44 +0200 | [diff] [blame] | 197 | static int serial_stub_stop(struct stdio_dev *sdev) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 198 | { |
| 199 | struct serial_device *dev = sdev->priv; |
| 200 | |
| 201 | return dev->stop(); |
| 202 | } |
| 203 | |
Jeroen Hofstee | 654f8d0 | 2014-10-08 22:57:44 +0200 | [diff] [blame] | 204 | static void serial_stub_putc(struct stdio_dev *sdev, const char ch) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 205 | { |
| 206 | struct serial_device *dev = sdev->priv; |
| 207 | |
| 208 | dev->putc(ch); |
| 209 | } |
| 210 | |
Jeroen Hofstee | 654f8d0 | 2014-10-08 22:57:44 +0200 | [diff] [blame] | 211 | static void serial_stub_puts(struct stdio_dev *sdev, const char *str) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 212 | { |
| 213 | struct serial_device *dev = sdev->priv; |
| 214 | |
| 215 | dev->puts(str); |
| 216 | } |
| 217 | |
Masahiro Yamada | 49ddcf3 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 218 | static int serial_stub_getc(struct stdio_dev *sdev) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 219 | { |
| 220 | struct serial_device *dev = sdev->priv; |
| 221 | |
| 222 | return dev->getc(); |
| 223 | } |
| 224 | |
Masahiro Yamada | 49ddcf3 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 225 | static int serial_stub_tstc(struct stdio_dev *sdev) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 226 | { |
| 227 | struct serial_device *dev = sdev->priv; |
| 228 | |
| 229 | return dev->tstc(); |
| 230 | } |
| 231 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 232 | /** |
| 233 | * serial_stdio_init() - Register serial ports with STDIO core |
| 234 | * |
| 235 | * This function generates a proxy driver for each serial port driver. |
| 236 | * These proxy drivers then register with the STDIO core, making the |
| 237 | * serial drivers available as STDIO devices. |
| 238 | */ |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 239 | void serial_stdio_init(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 240 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 241 | struct stdio_dev dev; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 242 | struct serial_device *s = serial_devices; |
| 243 | |
wdenk | 2ee6653 | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 244 | while (s) { |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 245 | memset(&dev, 0, sizeof(dev)); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 246 | |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 247 | strcpy(dev.name, s->name); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 248 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
| 249 | |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 250 | dev.start = serial_stub_start; |
| 251 | dev.stop = serial_stub_stop; |
| 252 | dev.putc = serial_stub_putc; |
| 253 | dev.puts = serial_stub_puts; |
| 254 | dev.getc = serial_stub_getc; |
| 255 | dev.tstc = serial_stub_tstc; |
Simon Glass | addf951 | 2014-09-04 16:27:23 -0600 | [diff] [blame] | 256 | dev.priv = s; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 257 | |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 258 | stdio_register(&dev); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 259 | |
| 260 | s = s->next; |
| 261 | } |
| 262 | } |
| 263 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 264 | /** |
| 265 | * serial_assign() - Select the serial output device by name |
| 266 | * @name: Name of the serial driver to be used as default output |
| 267 | * |
| 268 | * This function configures the serial output multiplexing by |
| 269 | * selecting which serial device will be used as default. In case |
| 270 | * the STDIO "serial" device is selected as stdin/stdout/stderr, |
| 271 | * the serial device previously configured by this function will be |
| 272 | * used for the particular operation. |
| 273 | * |
| 274 | * Returns 0 on success, negative on error. |
| 275 | */ |
Gerlando Falauto | 7813ca9 | 2011-11-18 06:49:12 +0000 | [diff] [blame] | 276 | int serial_assign(const char *name) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 277 | { |
| 278 | struct serial_device *s; |
| 279 | |
wdenk | 2ee6653 | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 280 | for (s = serial_devices; s; s = s->next) { |
Marek Vasut | 6d93e25 | 2012-10-06 14:07:03 +0000 | [diff] [blame] | 281 | if (strcmp(s->name, name)) |
| 282 | continue; |
| 283 | serial_current = s; |
| 284 | return 0; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Marek Vasut | 6d93e25 | 2012-10-06 14:07:03 +0000 | [diff] [blame] | 287 | return -EINVAL; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 290 | /** |
| 291 | * serial_reinit_all() - Reinitialize all compiled-in serial ports |
| 292 | * |
| 293 | * This function reinitializes all serial ports that are compiled |
| 294 | * into U-Boot by calling their serial_start() functions. |
| 295 | */ |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 296 | void serial_reinit_all(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 297 | { |
| 298 | struct serial_device *s; |
| 299 | |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 300 | for (s = serial_devices; s; s = s->next) |
Marek Vasut | 89143fb | 2012-09-07 14:35:31 +0200 | [diff] [blame] | 301 | s->start(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 304 | /** |
| 305 | * get_current() - Return pointer to currently selected serial port |
| 306 | * |
| 307 | * This function returns a pointer to currently selected serial port. |
| 308 | * The currently selected serial port is altered by serial_assign() |
| 309 | * function. |
| 310 | * |
| 311 | * In case this function is called before relocation or before any serial |
| 312 | * port is configured, this function calls default_serial_console() to |
| 313 | * determine the serial port. Otherwise, the configured serial port is |
| 314 | * returned. |
| 315 | * |
| 316 | * Returns pointer to the currently selected serial port on success, |
| 317 | * NULL on error. |
| 318 | */ |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 319 | static struct serial_device *get_current(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 320 | { |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 321 | struct serial_device *dev; |
| 322 | |
Marek Vasut | dee1941 | 2012-10-06 14:07:04 +0000 | [diff] [blame] | 323 | if (!(gd->flags & GD_FLG_RELOC)) |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 324 | dev = default_serial_console(); |
Marek Vasut | dee1941 | 2012-10-06 14:07:04 +0000 | [diff] [blame] | 325 | else if (!serial_current) |
| 326 | dev = default_serial_console(); |
| 327 | else |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 328 | dev = serial_current; |
Marek Vasut | dee1941 | 2012-10-06 14:07:04 +0000 | [diff] [blame] | 329 | |
| 330 | /* We must have a console device */ |
| 331 | if (!dev) { |
| 332 | #ifdef CONFIG_SPL_BUILD |
| 333 | puts("Cannot find console\n"); |
| 334 | hang(); |
| 335 | #else |
| 336 | panic("Cannot find console\n"); |
| 337 | #endif |
| 338 | } |
| 339 | |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 340 | return dev; |
| 341 | } |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 342 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 343 | /** |
| 344 | * serial_init() - Initialize currently selected serial port |
| 345 | * |
| 346 | * This function initializes the currently selected serial port. This |
| 347 | * usually involves setting up the registers of that particular port, |
| 348 | * enabling clock and such. This function uses the get_current() call |
| 349 | * to determine which port is selected. |
| 350 | * |
| 351 | * Returns 0 on success, negative on error. |
| 352 | */ |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 353 | int serial_init(void) |
| 354 | { |
Simon Glass | 093f79a | 2014-07-23 06:55:07 -0600 | [diff] [blame] | 355 | gd->flags |= GD_FLG_SERIAL_READY; |
Marek Vasut | 89143fb | 2012-09-07 14:35:31 +0200 | [diff] [blame] | 356 | return get_current()->start(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 359 | /** |
| 360 | * serial_setbrg() - Configure baud-rate of currently selected serial port |
| 361 | * |
| 362 | * This function configures the baud-rate of the currently selected |
| 363 | * serial port. The baud-rate is retrieved from global data within |
| 364 | * the serial port driver. This function uses the get_current() call |
| 365 | * to determine which port is selected. |
| 366 | * |
| 367 | * Returns 0 on success, negative on error. |
| 368 | */ |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 369 | void serial_setbrg(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 370 | { |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 371 | get_current()->setbrg(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 374 | /** |
| 375 | * serial_getc() - Read character from currently selected serial port |
| 376 | * |
| 377 | * This function retrieves a character from currently selected serial |
| 378 | * port. In case there is no character waiting on the serial port, |
| 379 | * this function will block and wait for the character to appear. This |
| 380 | * function uses the get_current() call to determine which port is |
| 381 | * selected. |
| 382 | * |
| 383 | * Returns the character on success, negative on error. |
| 384 | */ |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 385 | int serial_getc(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 386 | { |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 387 | return get_current()->getc(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 390 | /** |
| 391 | * serial_tstc() - Test if data is available on currently selected serial port |
| 392 | * |
| 393 | * This function tests if one or more characters are available on |
| 394 | * currently selected serial port. This function never blocks. This |
| 395 | * function uses the get_current() call to determine which port is |
| 396 | * selected. |
| 397 | * |
| 398 | * Returns positive if character is available, zero otherwise. |
| 399 | */ |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 400 | int serial_tstc(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 401 | { |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 402 | return get_current()->tstc(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 405 | /** |
| 406 | * serial_putc() - Output character via currently selected serial port |
| 407 | * @c: Single character to be output from the serial port. |
| 408 | * |
| 409 | * This function outputs a character via currently selected serial |
| 410 | * port. This character is passed to the serial port driver responsible |
| 411 | * for controlling the hardware. The hardware may still be in process |
| 412 | * of transmitting another character, therefore this function may block |
| 413 | * for a short amount of time. This function uses the get_current() |
| 414 | * call to determine which port is selected. |
| 415 | */ |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 416 | void serial_putc(const char c) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 417 | { |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 418 | get_current()->putc(c); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 421 | /** |
| 422 | * serial_puts() - Output string via currently selected serial port |
| 423 | * @s: Zero-terminated string to be output from the serial port. |
| 424 | * |
| 425 | * This function outputs a zero-terminated string via currently |
| 426 | * selected serial port. This function behaves as an accelerator |
| 427 | * in case the hardware can queue multiple characters for transfer. |
| 428 | * The whole string that is to be output is available to the function |
| 429 | * implementing the hardware manipulation. Transmitting the whole |
| 430 | * string may take some time, thus this function may block for some |
| 431 | * amount of time. This function uses the get_current() call to |
| 432 | * determine which port is selected. |
| 433 | */ |
Gerlando Falauto | a6e6f7f | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 434 | void serial_puts(const char *s) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 435 | { |
Simon Glass | 857c283 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 436 | get_current()->puts(s); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 437 | } |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 438 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 439 | /** |
| 440 | * default_serial_puts() - Output string by calling serial_putc() in loop |
| 441 | * @s: Zero-terminated string to be output from the serial port. |
| 442 | * |
| 443 | * This function outputs a zero-terminated string by calling serial_putc() |
| 444 | * in a loop. Most drivers do not support queueing more than one byte for |
| 445 | * transfer, thus this function precisely implements their serial_puts(). |
| 446 | * |
| 447 | * To optimize the number of get_current() calls, this function only |
| 448 | * calls get_current() once and then directly accesses the putc() call |
| 449 | * of the &struct serial_device . |
| 450 | */ |
Marek Vasut | bfb7d7a | 2012-10-06 14:07:01 +0000 | [diff] [blame] | 451 | void default_serial_puts(const char *s) |
| 452 | { |
| 453 | struct serial_device *dev = get_current(); |
| 454 | while (*s) |
| 455 | dev->putc(*s++); |
| 456 | } |
| 457 | |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 458 | #if CONFIG_POST & CONFIG_SYS_POST_UART |
| 459 | static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE; |
| 460 | |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 461 | /** |
| 462 | * uart_post_test() - Test the currently selected serial port using POST |
| 463 | * @flags: POST framework flags |
| 464 | * |
| 465 | * Do a loopback test of the currently selected serial port. This |
| 466 | * function is only useful in the context of the POST testing framwork. |
Vagrant Cascadian | 1b25e58 | 2015-11-24 14:46:24 -0800 | [diff] [blame] | 467 | * The serial port is first configured into loopback mode and then |
Marek Vasut | 9cd2b9e | 2012-10-08 11:36:39 +0000 | [diff] [blame] | 468 | * characters are sent through it. |
| 469 | * |
| 470 | * Returns 0 on success, value otherwise. |
| 471 | */ |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 472 | /* Mark weak until post/cpu/.../uart.c migrate over */ |
| 473 | __weak |
| 474 | int uart_post_test(int flags) |
| 475 | { |
| 476 | unsigned char c; |
| 477 | int ret, saved_baud, b; |
| 478 | struct serial_device *saved_dev, *s; |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 479 | |
| 480 | /* Save current serial state */ |
| 481 | ret = 0; |
| 482 | saved_dev = serial_current; |
Masahiro Yamada | 8e26157 | 2014-04-04 20:09:58 +0900 | [diff] [blame] | 483 | saved_baud = gd->baudrate; |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 484 | |
| 485 | for (s = serial_devices; s; s = s->next) { |
| 486 | /* If this driver doesn't support loop back, skip it */ |
| 487 | if (!s->loop) |
| 488 | continue; |
| 489 | |
| 490 | /* Test the next device */ |
| 491 | serial_current = s; |
| 492 | |
| 493 | ret = serial_init(); |
| 494 | if (ret) |
| 495 | goto done; |
| 496 | |
| 497 | /* Consume anything that happens to be queued */ |
| 498 | while (serial_tstc()) |
| 499 | serial_getc(); |
| 500 | |
| 501 | /* Enable loop back */ |
| 502 | s->loop(1); |
| 503 | |
| 504 | /* Test every available baud rate */ |
| 505 | for (b = 0; b < ARRAY_SIZE(bauds); ++b) { |
Masahiro Yamada | 8e26157 | 2014-04-04 20:09:58 +0900 | [diff] [blame] | 506 | gd->baudrate = bauds[b]; |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 507 | serial_setbrg(); |
| 508 | |
| 509 | /* |
| 510 | * Stick to printable chars to avoid issues: |
| 511 | * - terminal corruption |
| 512 | * - serial program reacting to sequences and sending |
| 513 | * back random extra data |
| 514 | * - most serial drivers add in extra chars (like \r\n) |
| 515 | */ |
| 516 | for (c = 0x20; c < 0x7f; ++c) { |
| 517 | /* Send it out */ |
| 518 | serial_putc(c); |
| 519 | |
| 520 | /* Make sure it's the same one */ |
| 521 | ret = (c != serial_getc()); |
| 522 | if (ret) { |
| 523 | s->loop(0); |
| 524 | goto done; |
| 525 | } |
| 526 | |
| 527 | /* Clean up the output in case it was sent */ |
| 528 | serial_putc('\b'); |
| 529 | ret = ('\b' != serial_getc()); |
| 530 | if (ret) { |
| 531 | s->loop(0); |
| 532 | goto done; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /* Disable loop back */ |
| 538 | s->loop(0); |
| 539 | |
Marek Vasut | 89143fb | 2012-09-07 14:35:31 +0200 | [diff] [blame] | 540 | /* XXX: There is no serial_stop() !? */ |
| 541 | if (s->stop) |
| 542 | s->stop(); |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | done: |
| 546 | /* Restore previous serial state */ |
| 547 | serial_current = saved_dev; |
Masahiro Yamada | 8e26157 | 2014-04-04 20:09:58 +0900 | [diff] [blame] | 548 | gd->baudrate = saved_baud; |
Mike Frysinger | 7b826c2 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 549 | serial_reinit_all(); |
| 550 | serial_setbrg(); |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | #endif |