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