Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 The Chromium OS Authors. |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SERIAL |
| 7 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 8 | #include <config.h> |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 10 | #include <env_internal.h> |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 13 | #include <os.h> |
| 14 | #include <serial.h> |
| 15 | #include <stdio_dev.h> |
Simon Glass | 5c60b2a | 2014-10-22 21:37:02 -0600 | [diff] [blame] | 16 | #include <watchdog.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 18 | #include <dm/lists.h> |
| 19 | #include <dm/device-internal.h> |
Simon Glass | a21ec4c | 2017-06-12 06:21:58 -0600 | [diff] [blame] | 20 | #include <dm/of_access.h> |
Rasmus Villemoes | a865db7 | 2024-10-03 16:10:28 +0200 | [diff] [blame] | 21 | #include <linux/build_bug.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 26 | /* |
| 27 | * Table with supported baudrates (defined in config_xyz.h) |
| 28 | */ |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 29 | static const unsigned long baudrate_table[] = CFG_SYS_BAUDRATE_TABLE; |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 30 | |
Simon Glass | 6057392 | 2018-10-01 12:22:20 -0600 | [diff] [blame] | 31 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 32 | static int serial_check_stdout(const void *blob, struct udevice **devp) |
| 33 | { |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 34 | int node = -1; |
Rasmus Villemoes | f439a1e | 2023-03-29 09:57:51 +0200 | [diff] [blame] | 35 | const char *str, *p; |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 36 | int namelen; |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 37 | |
| 38 | /* Check for a chosen console */ |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 39 | str = fdtdec_get_chosen_prop(blob, "stdout-path"); |
| 40 | if (str) { |
| 41 | p = strchr(str, ':'); |
| 42 | namelen = p ? p - str : strlen(str); |
Rasmus Villemoes | f439a1e | 2023-03-29 09:57:51 +0200 | [diff] [blame] | 43 | /* |
| 44 | * This also deals with things like |
| 45 | * |
| 46 | * stdout-path = "serial0:115200n8"; |
| 47 | * |
| 48 | * since fdt_path_offset_namelen() treats a str not |
| 49 | * beginning with '/' as an alias and thus applies |
| 50 | * fdt_get_alias_namelen() to it. |
| 51 | */ |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 52 | node = fdt_path_offset_namelen(blob, str, namelen); |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 53 | } |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 54 | |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 55 | if (node < 0) |
| 56 | node = fdt_path_offset(blob, "console"); |
| 57 | if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp)) |
| 58 | return 0; |
| 59 | |
| 60 | /* |
| 61 | * If the console is not marked to be bound before relocation, bind it |
| 62 | * anyway. |
| 63 | */ |
| 64 | if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node), |
Patrice Chotard | cc523cf | 2021-09-10 16:16:20 +0200 | [diff] [blame] | 65 | devp, NULL, false)) { |
Mark Kettenis | f5309ef | 2022-02-21 22:17:37 +0100 | [diff] [blame] | 66 | if (device_get_uclass_id(*devp) == UCLASS_SERIAL && |
| 67 | !device_probe(*devp)) |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | return -ENODEV; |
| 72 | } |
| 73 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 74 | static void serial_find_console_or_panic(void) |
| 75 | { |
Simon Glass | c860e3e | 2015-10-17 19:41:17 -0600 | [diff] [blame] | 76 | const void *blob = gd->fdt_blob; |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 77 | struct udevice *dev; |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 78 | #ifdef CONFIG_SERIAL_SEARCH_ALL |
Alexander Graf | 132ca2d | 2018-01-25 12:05:42 +0100 | [diff] [blame] | 79 | int ret; |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 80 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 81 | |
Simon Glass | afd81a5 | 2016-07-04 11:58:04 -0600 | [diff] [blame] | 82 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 83 | uclass_first_device(UCLASS_SERIAL, &dev); |
| 84 | if (dev) { |
| 85 | gd->cur_serial_dev = dev; |
| 86 | return; |
| 87 | } |
| 88 | } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) { |
Simon Glass | a21ec4c | 2017-06-12 06:21:58 -0600 | [diff] [blame] | 89 | /* Live tree has support for stdout */ |
| 90 | if (of_live_active()) { |
| 91 | struct device_node *np = of_get_stdout(); |
| 92 | |
| 93 | if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL, |
| 94 | np_to_ofnode(np), &dev)) { |
| 95 | gd->cur_serial_dev = dev; |
| 96 | return; |
| 97 | } |
| 98 | } else { |
| 99 | if (!serial_check_stdout(blob, &dev)) { |
| 100 | gd->cur_serial_dev = dev; |
| 101 | return; |
| 102 | } |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 103 | } |
Simon Glass | 8579a51 | 2015-03-06 13:19:03 -0700 | [diff] [blame] | 104 | } |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 105 | if (!IS_ENABLED(CONFIG_XPL_BUILD) || !CONFIG_IS_ENABLED(OF_CONTROL) || |
Simon Glass | 65f8329 | 2022-01-12 19:26:15 -0700 | [diff] [blame] | 106 | !blob) { |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 107 | /* |
Stefan Roese | 88c3a88 | 2015-11-26 13:38:43 +0100 | [diff] [blame] | 108 | * Try to use CONFIG_CONS_INDEX if available (it is numbered |
| 109 | * from 1!). |
| 110 | * |
| 111 | * Failing that, get the device with sequence number 0, or in |
Alexander Graf | 132ca2d | 2018-01-25 12:05:42 +0100 | [diff] [blame] | 112 | * extremis just the first working serial device we can find. |
| 113 | * But we insist on having a console (even if it is silent). |
Stefan Roese | 88c3a88 | 2015-11-26 13:38:43 +0100 | [diff] [blame] | 114 | */ |
Simon Glass | bc5aa99 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 115 | #ifdef CONFIG_CONS_INDEX |
| 116 | #define INDEX (CONFIG_CONS_INDEX - 1) |
| 117 | #else |
| 118 | #define INDEX 0 |
| 119 | #endif |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 120 | |
| 121 | #ifdef CONFIG_SERIAL_SEARCH_ALL |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 122 | if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || |
Alexander Graf | 132ca2d | 2018-01-25 12:05:42 +0100 | [diff] [blame] | 123 | !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 124 | if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) { |
Alexander Graf | 132ca2d | 2018-01-25 12:05:42 +0100 | [diff] [blame] | 125 | gd->cur_serial_dev = dev; |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* Search for any working device */ |
| 131 | for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev); |
| 132 | dev; |
| 133 | ret = uclass_next_device_check(&dev)) { |
| 134 | if (!ret) { |
| 135 | /* Device did succeed probing */ |
| 136 | gd->cur_serial_dev = dev; |
| 137 | return; |
| 138 | } |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 139 | } |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 140 | #else |
| 141 | if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || |
| 142 | !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) || |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 143 | !uclass_first_device_err(UCLASS_SERIAL, &dev)) { |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 144 | gd->cur_serial_dev = dev; |
| 145 | return; |
| 146 | } |
| 147 | #endif |
| 148 | |
Simon Glass | bc5aa99 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 149 | #undef INDEX |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 150 | } |
| 151 | |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 152 | #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 153 | panic_str("No serial driver found"); |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 154 | #endif |
Maksim Kiselev | 5f6d914 | 2023-08-18 12:34:30 +0300 | [diff] [blame] | 155 | gd->cur_serial_dev = NULL; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 156 | } |
Simon Glass | 6057392 | 2018-10-01 12:22:20 -0600 | [diff] [blame] | 157 | #endif /* CONFIG_SERIAL_PRESENT */ |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 158 | |
Algapally Santosh Sagar | df17899 | 2023-09-21 16:50:43 +0530 | [diff] [blame] | 159 | /** |
| 160 | * check_valid_baudrate() - Check whether baudrate is valid or not |
| 161 | * |
| 162 | * @baud: baud rate to check |
| 163 | * Return: 0 if OK, -ve on error |
| 164 | */ |
| 165 | static int check_valid_baudrate(int baud) |
| 166 | { |
| 167 | int i; |
| 168 | |
| 169 | for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { |
| 170 | if (baud == baudrate_table[i]) |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | int fetch_baud_from_dtb(void) |
| 178 | { |
| 179 | int baud_value, ret; |
| 180 | |
| 181 | baud_value = ofnode_read_baud(); |
| 182 | ret = check_valid_baudrate(baud_value); |
| 183 | if (ret) |
| 184 | return ret; |
| 185 | |
| 186 | return baud_value; |
| 187 | } |
| 188 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 189 | /* Called prior to relocation */ |
| 190 | int serial_init(void) |
| 191 | { |
Simon Glass | 6057392 | 2018-10-01 12:22:20 -0600 | [diff] [blame] | 192 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 193 | serial_find_console_or_panic(); |
| 194 | gd->flags |= GD_FLG_SERIAL_READY; |
Algapally Santosh Sagar | df17899 | 2023-09-21 16:50:43 +0530 | [diff] [blame] | 195 | |
| 196 | if (IS_ENABLED(CONFIG_OF_SERIAL_BAUD)) { |
| 197 | int ret = 0; |
| 198 | char *ptr = (char*)&default_environment[0]; |
| 199 | |
| 200 | /* |
| 201 | * Fetch the baudrate from the dtb and update the value in the |
| 202 | * default environment. |
| 203 | */ |
| 204 | ret = fetch_baud_from_dtb(); |
| 205 | if (ret != -EINVAL && ret != -EFAULT) { |
| 206 | gd->baudrate = ret; |
| 207 | |
| 208 | while (*ptr != '\0' && *(ptr + 1) != '\0') |
| 209 | ptr++; |
| 210 | ptr += 2; |
| 211 | sprintf(ptr, "baudrate=%d", gd->baudrate); |
| 212 | } |
| 213 | } |
Sean Anderson | 01c18af | 2020-02-02 13:15:17 -0500 | [diff] [blame] | 214 | serial_setbrg(); |
Simon Glass | 6057392 | 2018-10-01 12:22:20 -0600 | [diff] [blame] | 215 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* Called after relocation */ |
Ovidiu Panait | 96008c9 | 2020-07-24 14:12:22 +0300 | [diff] [blame] | 221 | int serial_initialize(void) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 222 | { |
Vabhav Sharma | 42149c9 | 2020-12-09 10:42:04 +0530 | [diff] [blame] | 223 | /* Scanning uclass to probe devices */ |
| 224 | if (IS_ENABLED(CONFIG_SERIAL_PROBE_ALL)) { |
| 225 | int ret; |
| 226 | |
| 227 | ret = uclass_probe_all(UCLASS_SERIAL); |
| 228 | if (ret) |
| 229 | return ret; |
| 230 | } |
| 231 | |
Ovidiu Panait | 96008c9 | 2020-07-24 14:12:22 +0300 | [diff] [blame] | 232 | return serial_init(); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 233 | } |
| 234 | |
Rasmus Villemoes | 83a5d2b | 2023-10-16 10:35:21 +0200 | [diff] [blame] | 235 | static void _serial_flush(struct udevice *dev) |
| 236 | { |
| 237 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 238 | |
| 239 | if (!ops->pending) |
| 240 | return; |
| 241 | while (ops->pending(dev, false) > 0) |
| 242 | ; |
| 243 | } |
Rasmus Villemoes | 83a5d2b | 2023-10-16 10:35:21 +0200 | [diff] [blame] | 244 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 245 | static void _serial_putc(struct udevice *dev, char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 246 | { |
Masahiro Yamada | 84df41a | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 247 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 248 | int err; |
| 249 | |
Alison Wang | c0a9ce6 | 2016-03-02 11:00:36 +0800 | [diff] [blame] | 250 | if (ch == '\n') |
| 251 | _serial_putc(dev, '\r'); |
| 252 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 253 | do { |
Masahiro Yamada | 84df41a | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 254 | err = ops->putc(dev, ch); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 255 | } while (err == -EAGAIN); |
Rasmus Villemoes | 0b491f5 | 2023-10-16 10:35:22 +0200 | [diff] [blame] | 256 | |
| 257 | if (IS_ENABLED(CONFIG_CONSOLE_FLUSH_ON_NEWLINE) && ch == '\n') |
| 258 | _serial_flush(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 259 | } |
| 260 | |
Sean Anderson | d56ce3c | 2022-04-04 14:17:57 -0400 | [diff] [blame] | 261 | static int __serial_puts(struct udevice *dev, const char *str, size_t len) |
| 262 | { |
| 263 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 264 | |
| 265 | do { |
| 266 | ssize_t written = ops->puts(dev, str, len); |
| 267 | |
| 268 | if (written < 0) |
| 269 | return written; |
| 270 | str += written; |
| 271 | len -= written; |
| 272 | } while (len); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 277 | static void _serial_puts(struct udevice *dev, const char *str) |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 278 | { |
Sean Anderson | aa22f97 | 2022-03-22 16:59:34 -0400 | [diff] [blame] | 279 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 280 | |
| 281 | if (!CONFIG_IS_ENABLED(SERIAL_PUTS) || !ops->puts) { |
| 282 | while (*str) |
| 283 | _serial_putc(dev, *str++); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | do { |
| 288 | const char *newline = strchrnul(str, '\n'); |
Sean Anderson | d56ce3c | 2022-04-04 14:17:57 -0400 | [diff] [blame] | 289 | size_t len = newline - str; |
Sean Anderson | aa22f97 | 2022-03-22 16:59:34 -0400 | [diff] [blame] | 290 | |
Sean Anderson | d56ce3c | 2022-04-04 14:17:57 -0400 | [diff] [blame] | 291 | if (__serial_puts(dev, str, len)) |
| 292 | return; |
Sean Anderson | aa22f97 | 2022-03-22 16:59:34 -0400 | [diff] [blame] | 293 | |
Sean Anderson | d56ce3c | 2022-04-04 14:17:57 -0400 | [diff] [blame] | 294 | if (*newline && __serial_puts(dev, "\r\n", 2)) |
| 295 | return; |
Sean Anderson | aa22f97 | 2022-03-22 16:59:34 -0400 | [diff] [blame] | 296 | |
Rasmus Villemoes | 0b491f5 | 2023-10-16 10:35:22 +0200 | [diff] [blame] | 297 | if (IS_ENABLED(CONFIG_CONSOLE_FLUSH_ON_NEWLINE) && *newline) |
| 298 | _serial_flush(dev); |
| 299 | |
Sean Anderson | d56ce3c | 2022-04-04 14:17:57 -0400 | [diff] [blame] | 300 | str += len + !!*newline; |
Sean Anderson | aa22f97 | 2022-03-22 16:59:34 -0400 | [diff] [blame] | 301 | } while (*str); |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 302 | } |
| 303 | |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 304 | static int __serial_getc(struct udevice *dev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 305 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 306 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 307 | int err; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 308 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 309 | do { |
| 310 | err = ops->getc(dev); |
| 311 | if (err == -EAGAIN) |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 312 | schedule(); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 313 | } while (err == -EAGAIN); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 314 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 315 | return err >= 0 ? err : 0; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 316 | } |
| 317 | |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 318 | static int __serial_tstc(struct udevice *dev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 319 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 320 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 321 | |
| 322 | if (ops->pending) |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 323 | return ops->pending(dev, true); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 324 | |
| 325 | return 1; |
| 326 | } |
| 327 | |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 328 | #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) |
| 329 | static int _serial_tstc(struct udevice *dev) |
| 330 | { |
| 331 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Rasmus Villemoes | e38be2d | 2024-10-03 16:10:27 +0200 | [diff] [blame] | 332 | uint wr, avail; |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 333 | |
Rasmus Villemoes | a865db7 | 2024-10-03 16:10:28 +0200 | [diff] [blame] | 334 | BUILD_BUG_ON_NOT_POWER_OF_2(CONFIG_SERIAL_RX_BUFFER_SIZE); |
| 335 | |
Rasmus Villemoes | e38be2d | 2024-10-03 16:10:27 +0200 | [diff] [blame] | 336 | /* Read all available chars into the RX buffer while there's room */ |
| 337 | avail = CONFIG_SERIAL_RX_BUFFER_SIZE - (upriv->wr_ptr - upriv->rd_ptr); |
| 338 | while (avail-- && __serial_tstc(dev)) { |
Rasmus Villemoes | 50fc188 | 2024-10-03 16:10:26 +0200 | [diff] [blame] | 339 | wr = upriv->wr_ptr++ % CONFIG_SERIAL_RX_BUFFER_SIZE; |
| 340 | upriv->buf[wr] = __serial_getc(dev); |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0; |
| 344 | } |
| 345 | |
| 346 | static int _serial_getc(struct udevice *dev) |
| 347 | { |
| 348 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
| 349 | char val; |
Rasmus Villemoes | 50fc188 | 2024-10-03 16:10:26 +0200 | [diff] [blame] | 350 | uint rd; |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 351 | |
Patrick Delaunay | 9c44282 | 2018-08-03 13:38:43 +0200 | [diff] [blame] | 352 | if (upriv->rd_ptr == upriv->wr_ptr) |
| 353 | return __serial_getc(dev); |
| 354 | |
Rasmus Villemoes | 50fc188 | 2024-10-03 16:10:26 +0200 | [diff] [blame] | 355 | rd = upriv->rd_ptr++ % CONFIG_SERIAL_RX_BUFFER_SIZE; |
| 356 | val = upriv->buf[rd]; |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 357 | |
| 358 | return val; |
| 359 | } |
| 360 | |
| 361 | #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ |
| 362 | |
| 363 | static int _serial_getc(struct udevice *dev) |
| 364 | { |
| 365 | return __serial_getc(dev); |
| 366 | } |
| 367 | |
| 368 | static int _serial_tstc(struct udevice *dev) |
| 369 | { |
| 370 | return __serial_tstc(dev); |
| 371 | } |
| 372 | #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ |
| 373 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 374 | void serial_putc(char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 375 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 376 | if (gd->cur_serial_dev) |
| 377 | _serial_putc(gd->cur_serial_dev, ch); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 378 | } |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 379 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 380 | void serial_puts(const char *str) |
| 381 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 382 | if (gd->cur_serial_dev) |
| 383 | _serial_puts(gd->cur_serial_dev, str); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 384 | } |
| 385 | |
Pali Rohár | a48a24f | 2022-09-05 11:31:19 +0200 | [diff] [blame] | 386 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 387 | void serial_flush(void) |
| 388 | { |
| 389 | if (!gd->cur_serial_dev) |
| 390 | return; |
| 391 | |
| 392 | _serial_flush(gd->cur_serial_dev); |
| 393 | } |
| 394 | #endif |
| 395 | |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 396 | int serial_getc(void) |
| 397 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 398 | if (!gd->cur_serial_dev) |
| 399 | return 0; |
| 400 | |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 401 | return _serial_getc(gd->cur_serial_dev); |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 402 | } |
| 403 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 404 | int serial_tstc(void) |
| 405 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 406 | if (!gd->cur_serial_dev) |
| 407 | return 0; |
| 408 | |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 409 | return _serial_tstc(gd->cur_serial_dev); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void serial_setbrg(void) |
| 413 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 414 | struct dm_serial_ops *ops; |
| 415 | |
| 416 | if (!gd->cur_serial_dev) |
| 417 | return; |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 418 | |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 419 | ops = serial_get_ops(gd->cur_serial_dev); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 420 | if (ops->setbrg) |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 421 | ops->setbrg(gd->cur_serial_dev, gd->baudrate); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 422 | } |
| 423 | |
Simon Glass | daaff93 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 424 | int serial_getconfig(struct udevice *dev, uint *config) |
Andy Shevchenko | 08e9879 | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 425 | { |
| 426 | struct dm_serial_ops *ops; |
| 427 | |
Simon Glass | daaff93 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 428 | ops = serial_get_ops(dev); |
Andy Shevchenko | 08e9879 | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 429 | if (ops->getconfig) |
Simon Glass | daaff93 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 430 | return ops->getconfig(dev, config); |
Andy Shevchenko | 08e9879 | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Simon Glass | de62d48 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 435 | int serial_setconfig(struct udevice *dev, uint config) |
Patrice Chotard | 46c1d9f | 2018-08-03 15:07:38 +0200 | [diff] [blame] | 436 | { |
| 437 | struct dm_serial_ops *ops; |
| 438 | |
Simon Glass | de62d48 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 439 | ops = serial_get_ops(dev); |
Patrice Chotard | 46c1d9f | 2018-08-03 15:07:38 +0200 | [diff] [blame] | 440 | if (ops->setconfig) |
Simon Glass | de62d48 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 441 | return ops->setconfig(dev, config); |
Patrice Chotard | 46c1d9f | 2018-08-03 15:07:38 +0200 | [diff] [blame] | 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 446 | int serial_getinfo(struct udevice *dev, struct serial_device_info *info) |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 447 | { |
| 448 | struct dm_serial_ops *ops; |
| 449 | |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 450 | if (!info) |
| 451 | return -EINVAL; |
| 452 | |
| 453 | info->baudrate = gd->baudrate; |
| 454 | |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 455 | ops = serial_get_ops(dev); |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 456 | if (ops->getinfo) |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 457 | return ops->getinfo(dev, info); |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 458 | |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 462 | void serial_stdio_init(void) |
| 463 | { |
| 464 | } |
| 465 | |
Tom Rini | 952cc38 | 2022-12-04 10:14:13 -0500 | [diff] [blame] | 466 | #if CONFIG_IS_ENABLED(DM_STDIO) |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 467 | |
| 468 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 469 | static void serial_stub_putc(struct stdio_dev *sdev, const char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 470 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 471 | _serial_putc(sdev->priv, ch); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 472 | } |
| 473 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 474 | static void serial_stub_puts(struct stdio_dev *sdev, const char *str) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 475 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 476 | _serial_puts(sdev->priv, str); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 477 | } |
| 478 | |
Pali Rohár | 9c9ceca | 2022-09-05 11:31:18 +0200 | [diff] [blame] | 479 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 480 | static void serial_stub_flush(struct stdio_dev *sdev) |
| 481 | { |
| 482 | _serial_flush(sdev->priv); |
| 483 | } |
| 484 | #endif |
| 485 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 486 | static int serial_stub_getc(struct stdio_dev *sdev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 487 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 488 | return _serial_getc(sdev->priv); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 489 | } |
| 490 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 491 | static int serial_stub_tstc(struct stdio_dev *sdev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 492 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 493 | return _serial_tstc(sdev->priv); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 494 | } |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 495 | #endif |
Heiko Schocher | 6fe2dd5 | 2022-02-02 13:55:19 +0100 | [diff] [blame] | 496 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 497 | |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 498 | /** |
| 499 | * on_baudrate() - Update the actual baudrate when the env var changes |
| 500 | * |
| 501 | * This will check for a valid baudrate and only apply it if valid. |
| 502 | */ |
| 503 | static int on_baudrate(const char *name, const char *value, enum env_op op, |
| 504 | int flags) |
| 505 | { |
| 506 | int i; |
| 507 | int baudrate; |
| 508 | |
| 509 | switch (op) { |
| 510 | case env_op_create: |
| 511 | case env_op_overwrite: |
| 512 | /* |
| 513 | * Switch to new baudrate if new baudrate is supported |
| 514 | */ |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 515 | baudrate = dectoul(value, NULL); |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 516 | |
| 517 | /* Not actually changing */ |
| 518 | if (gd->baudrate == baudrate) |
| 519 | return 0; |
| 520 | |
| 521 | for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { |
| 522 | if (baudrate == baudrate_table[i]) |
| 523 | break; |
| 524 | } |
| 525 | if (i == ARRAY_SIZE(baudrate_table)) { |
| 526 | if ((flags & H_FORCE) == 0) |
| 527 | printf("## Baudrate %d bps not supported\n", |
| 528 | baudrate); |
| 529 | return 1; |
| 530 | } |
| 531 | if ((flags & H_INTERACTIVE) != 0) { |
| 532 | printf("## Switch baudrate to %d bps and press ENTER ...\n", |
| 533 | baudrate); |
| 534 | udelay(50000); |
Pali Rohár | 8e67a8a | 2022-09-05 11:31:20 +0200 | [diff] [blame] | 535 | flush(); |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | gd->baudrate = baudrate; |
| 539 | |
| 540 | serial_setbrg(); |
| 541 | |
| 542 | udelay(50000); |
| 543 | |
| 544 | if ((flags & H_INTERACTIVE) != 0) |
| 545 | while (1) { |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 546 | if (getchar() == '\r') |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | case env_op_delete: |
| 552 | printf("## Baudrate may not be deleted\n"); |
| 553 | return 1; |
| 554 | default: |
| 555 | return 0; |
| 556 | } |
| 557 | } |
| 558 | U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 559 | |
Simon Glass | 30a1349 | 2015-12-13 21:36:58 -0700 | [diff] [blame] | 560 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 561 | static int serial_post_probe(struct udevice *dev) |
| 562 | { |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 563 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Tom Rini | 952cc38 | 2022-12-04 10:14:13 -0500 | [diff] [blame] | 564 | #if CONFIG_IS_ENABLED(DM_STDIO) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 565 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 566 | struct stdio_dev sdev; |
| 567 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 568 | int ret; |
| 569 | |
| 570 | /* Set the baud rate */ |
| 571 | if (ops->setbrg) { |
| 572 | ret = ops->setbrg(dev, gd->baudrate); |
| 573 | if (ret) |
| 574 | return ret; |
| 575 | } |
| 576 | |
Tom Rini | 952cc38 | 2022-12-04 10:14:13 -0500 | [diff] [blame] | 577 | #if CONFIG_IS_ENABLED(DM_STDIO) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 578 | if (!(gd->flags & GD_FLG_RELOC)) |
| 579 | return 0; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 580 | memset(&sdev, '\0', sizeof(sdev)); |
| 581 | |
Heinrich Schuchardt | 711ad63 | 2023-09-29 02:47:16 +0200 | [diff] [blame] | 582 | strlcpy(sdev.name, dev->name, sizeof(sdev.name)); |
Simon Glass | 10a7fe9 | 2017-07-27 09:31:04 -0600 | [diff] [blame] | 583 | sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 584 | sdev.priv = dev; |
| 585 | sdev.putc = serial_stub_putc; |
| 586 | sdev.puts = serial_stub_puts; |
Pali Rohár | 9c9ceca | 2022-09-05 11:31:18 +0200 | [diff] [blame] | 587 | STDIO_DEV_ASSIGN_FLUSH(&sdev, serial_stub_flush); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 588 | sdev.getc = serial_stub_getc; |
| 589 | sdev.tstc = serial_stub_tstc; |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 590 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 591 | stdio_register_dev(&sdev, &upriv->sdev); |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 592 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static int serial_pre_remove(struct udevice *dev) |
| 597 | { |
Simon Glass | 3110ece | 2016-10-17 20:13:02 -0600 | [diff] [blame] | 598 | #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 599 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 600 | |
Stefan Roese | dcc4b07 | 2017-04-24 09:48:01 +0200 | [diff] [blame] | 601 | if (stdio_deregister_dev(upriv->sdev, true)) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 602 | return -EPERM; |
| 603 | #endif |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | UCLASS_DRIVER(serial) = { |
| 609 | .id = UCLASS_SERIAL, |
| 610 | .name = "serial", |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 611 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 612 | .post_probe = serial_post_probe, |
| 613 | .pre_remove = serial_pre_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 614 | .per_device_auto = sizeof(struct serial_dev_priv), |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 615 | }; |
Simon Glass | 30a1349 | 2015-12-13 21:36:58 -0700 | [diff] [blame] | 616 | #endif |