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