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