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