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