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