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 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 8 | #include <common.h> |
| 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> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 25 | /* |
| 26 | * Table with supported baudrates (defined in config_xyz.h) |
| 27 | */ |
| 28 | static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; |
| 29 | |
Andy Yan | 1fa20e4d | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 30 | #if !CONFIG_VAL(SYS_MALLOC_F_LEN) |
| 31 | #error "Serial is required before relocation - define CONFIG_$(SPL_)SYS_MALLOC_F_LEN to make this work" |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 32 | #endif |
| 33 | |
Simon Glass | 6057392 | 2018-10-01 12:22:20 -0600 | [diff] [blame] | 34 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 35 | static int serial_check_stdout(const void *blob, struct udevice **devp) |
| 36 | { |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 37 | int node = -1; |
| 38 | const char *str, *p, *name; |
| 39 | int namelen; |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 40 | |
| 41 | /* Check for a chosen console */ |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 42 | str = fdtdec_get_chosen_prop(blob, "stdout-path"); |
| 43 | if (str) { |
| 44 | p = strchr(str, ':'); |
| 45 | namelen = p ? p - str : strlen(str); |
| 46 | node = fdt_path_offset_namelen(blob, str, namelen); |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 47 | |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 48 | if (node < 0) { |
| 49 | /* |
| 50 | * Deal with things like |
| 51 | * stdout-path = "serial0:115200n8"; |
| 52 | * |
| 53 | * We need to look up the alias and then follow it to |
| 54 | * the correct node. |
| 55 | */ |
| 56 | name = fdt_get_alias_namelen(blob, str, namelen); |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 57 | if (name) |
| 58 | node = fdt_path_offset(blob, name); |
| 59 | } |
| 60 | } |
Bin Meng | d86eba4 | 2019-11-25 05:52:36 -0800 | [diff] [blame] | 61 | |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 62 | if (node < 0) |
| 63 | node = fdt_path_offset(blob, "console"); |
| 64 | if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp)) |
| 65 | return 0; |
| 66 | |
| 67 | /* |
| 68 | * If the console is not marked to be bound before relocation, bind it |
| 69 | * anyway. |
| 70 | */ |
| 71 | if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node), |
Bin Meng | 9a9b074 | 2018-10-10 22:06:58 -0700 | [diff] [blame] | 72 | devp, false)) { |
Simon Glass | 89d9cf2 | 2017-06-12 06:21:57 -0600 | [diff] [blame] | 73 | if (!device_probe(*devp)) |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | return -ENODEV; |
| 78 | } |
| 79 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 80 | static void serial_find_console_or_panic(void) |
| 81 | { |
Simon Glass | c860e3e | 2015-10-17 19:41:17 -0600 | [diff] [blame] | 82 | const void *blob = gd->fdt_blob; |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 83 | struct udevice *dev; |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 84 | #ifdef CONFIG_SERIAL_SEARCH_ALL |
Alexander Graf | 132ca2d | 2018-01-25 12:05:42 +0100 | [diff] [blame] | 85 | int ret; |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 86 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 87 | |
Simon Glass | afd81a5 | 2016-07-04 11:58:04 -0600 | [diff] [blame] | 88 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 89 | uclass_first_device(UCLASS_SERIAL, &dev); |
| 90 | if (dev) { |
| 91 | gd->cur_serial_dev = dev; |
| 92 | return; |
| 93 | } |
| 94 | } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) { |
Simon Glass | a21ec4c | 2017-06-12 06:21:58 -0600 | [diff] [blame] | 95 | /* Live tree has support for stdout */ |
| 96 | if (of_live_active()) { |
| 97 | struct device_node *np = of_get_stdout(); |
| 98 | |
| 99 | if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL, |
| 100 | np_to_ofnode(np), &dev)) { |
| 101 | gd->cur_serial_dev = dev; |
| 102 | return; |
| 103 | } |
| 104 | } else { |
| 105 | if (!serial_check_stdout(blob, &dev)) { |
| 106 | gd->cur_serial_dev = dev; |
| 107 | return; |
| 108 | } |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 109 | } |
Simon Glass | 8579a51 | 2015-03-06 13:19:03 -0700 | [diff] [blame] | 110 | } |
Simon Glass | c860e3e | 2015-10-17 19:41:17 -0600 | [diff] [blame] | 111 | if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) { |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 112 | /* |
Stefan Roese | 88c3a88 | 2015-11-26 13:38:43 +0100 | [diff] [blame] | 113 | * Try to use CONFIG_CONS_INDEX if available (it is numbered |
| 114 | * from 1!). |
| 115 | * |
| 116 | * Failing that, get the device with sequence number 0, or in |
Alexander Graf | 132ca2d | 2018-01-25 12:05:42 +0100 | [diff] [blame] | 117 | * extremis just the first working serial device we can find. |
| 118 | * But we insist on having a console (even if it is silent). |
Stefan Roese | 88c3a88 | 2015-11-26 13:38:43 +0100 | [diff] [blame] | 119 | */ |
Simon Glass | bc5aa99 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 120 | #ifdef CONFIG_CONS_INDEX |
| 121 | #define INDEX (CONFIG_CONS_INDEX - 1) |
| 122 | #else |
| 123 | #define INDEX 0 |
| 124 | #endif |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 125 | |
| 126 | #ifdef CONFIG_SERIAL_SEARCH_ALL |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 127 | if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || |
Alexander Graf | 132ca2d | 2018-01-25 12:05:42 +0100 | [diff] [blame] | 128 | !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 129 | if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) { |
Alexander Graf | 132ca2d | 2018-01-25 12:05:42 +0100 | [diff] [blame] | 130 | gd->cur_serial_dev = dev; |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* Search for any working device */ |
| 136 | for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev); |
| 137 | dev; |
| 138 | ret = uclass_next_device_check(&dev)) { |
| 139 | if (!ret) { |
| 140 | /* Device did succeed probing */ |
| 141 | gd->cur_serial_dev = dev; |
| 142 | return; |
| 143 | } |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 144 | } |
Alexander Graf | c8bda54 | 2018-01-29 13:57:20 +0100 | [diff] [blame] | 145 | #else |
| 146 | if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || |
| 147 | !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) || |
| 148 | (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) { |
| 149 | gd->cur_serial_dev = dev; |
| 150 | return; |
| 151 | } |
| 152 | #endif |
| 153 | |
Simon Glass | bc5aa99 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 154 | #undef INDEX |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 157 | #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 158 | panic_str("No serial driver found"); |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 159 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 160 | } |
Simon Glass | 6057392 | 2018-10-01 12:22:20 -0600 | [diff] [blame] | 161 | #endif /* CONFIG_SERIAL_PRESENT */ |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 162 | |
| 163 | /* Called prior to relocation */ |
| 164 | int serial_init(void) |
| 165 | { |
Simon Glass | 6057392 | 2018-10-01 12:22:20 -0600 | [diff] [blame] | 166 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 167 | serial_find_console_or_panic(); |
| 168 | gd->flags |= GD_FLG_SERIAL_READY; |
Sean Anderson | 01c18af | 2020-02-02 13:15:17 -0500 | [diff] [blame] | 169 | serial_setbrg(); |
Simon Glass | 6057392 | 2018-10-01 12:22:20 -0600 | [diff] [blame] | 170 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* Called after relocation */ |
Ovidiu Panait | 96008c9 | 2020-07-24 14:12:22 +0300 | [diff] [blame] | 176 | int serial_initialize(void) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 177 | { |
Vabhav Sharma | 42149c9 | 2020-12-09 10:42:04 +0530 | [diff] [blame] | 178 | /* Scanning uclass to probe devices */ |
| 179 | if (IS_ENABLED(CONFIG_SERIAL_PROBE_ALL)) { |
| 180 | int ret; |
| 181 | |
| 182 | ret = uclass_probe_all(UCLASS_SERIAL); |
| 183 | if (ret) |
| 184 | return ret; |
| 185 | } |
| 186 | |
Ovidiu Panait | 96008c9 | 2020-07-24 14:12:22 +0300 | [diff] [blame] | 187 | return serial_init(); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 188 | } |
| 189 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 190 | static void _serial_putc(struct udevice *dev, char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 191 | { |
Masahiro Yamada | 84df41a | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 192 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 193 | int err; |
| 194 | |
Alison Wang | c0a9ce6 | 2016-03-02 11:00:36 +0800 | [diff] [blame] | 195 | if (ch == '\n') |
| 196 | _serial_putc(dev, '\r'); |
| 197 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 198 | do { |
Masahiro Yamada | 84df41a | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 199 | err = ops->putc(dev, ch); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 200 | } while (err == -EAGAIN); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 203 | static void _serial_puts(struct udevice *dev, const char *str) |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 204 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 205 | while (*str) |
| 206 | _serial_putc(dev, *str++); |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 207 | } |
| 208 | |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 209 | static int __serial_getc(struct udevice *dev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 210 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 211 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 212 | int err; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 213 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 214 | do { |
| 215 | err = ops->getc(dev); |
| 216 | if (err == -EAGAIN) |
| 217 | WATCHDOG_RESET(); |
| 218 | } while (err == -EAGAIN); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 219 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 220 | return err >= 0 ? err : 0; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 221 | } |
| 222 | |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 223 | static int __serial_tstc(struct udevice *dev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 224 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 225 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 226 | |
| 227 | if (ops->pending) |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 228 | return ops->pending(dev, true); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 229 | |
| 230 | return 1; |
| 231 | } |
| 232 | |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 233 | #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) |
| 234 | static int _serial_tstc(struct udevice *dev) |
| 235 | { |
| 236 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
| 237 | |
| 238 | /* Read all available chars into the RX buffer */ |
| 239 | while (__serial_tstc(dev)) { |
| 240 | upriv->buf[upriv->wr_ptr++] = __serial_getc(dev); |
| 241 | upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE; |
| 242 | } |
| 243 | |
| 244 | return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0; |
| 245 | } |
| 246 | |
| 247 | static int _serial_getc(struct udevice *dev) |
| 248 | { |
| 249 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
| 250 | char val; |
| 251 | |
Patrick Delaunay | 9c44282 | 2018-08-03 13:38:43 +0200 | [diff] [blame] | 252 | if (upriv->rd_ptr == upriv->wr_ptr) |
| 253 | return __serial_getc(dev); |
| 254 | |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 255 | val = upriv->buf[upriv->rd_ptr++]; |
| 256 | upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE; |
| 257 | |
| 258 | return val; |
| 259 | } |
| 260 | |
| 261 | #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ |
| 262 | |
| 263 | static int _serial_getc(struct udevice *dev) |
| 264 | { |
| 265 | return __serial_getc(dev); |
| 266 | } |
| 267 | |
| 268 | static int _serial_tstc(struct udevice *dev) |
| 269 | { |
| 270 | return __serial_tstc(dev); |
| 271 | } |
| 272 | #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ |
| 273 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 274 | void serial_putc(char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 275 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 276 | if (gd->cur_serial_dev) |
| 277 | _serial_putc(gd->cur_serial_dev, ch); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 278 | } |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 279 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 280 | void serial_puts(const char *str) |
| 281 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 282 | if (gd->cur_serial_dev) |
| 283 | _serial_puts(gd->cur_serial_dev, str); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 284 | } |
| 285 | |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 286 | int serial_getc(void) |
| 287 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 288 | if (!gd->cur_serial_dev) |
| 289 | return 0; |
| 290 | |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 291 | return _serial_getc(gd->cur_serial_dev); |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 292 | } |
| 293 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 294 | int serial_tstc(void) |
| 295 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 296 | if (!gd->cur_serial_dev) |
| 297 | return 0; |
| 298 | |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 299 | return _serial_tstc(gd->cur_serial_dev); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void serial_setbrg(void) |
| 303 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 304 | struct dm_serial_ops *ops; |
| 305 | |
| 306 | if (!gd->cur_serial_dev) |
| 307 | return; |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 308 | |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 309 | ops = serial_get_ops(gd->cur_serial_dev); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 310 | if (ops->setbrg) |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 311 | ops->setbrg(gd->cur_serial_dev, gd->baudrate); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 312 | } |
| 313 | |
Simon Glass | daaff93 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 314 | int serial_getconfig(struct udevice *dev, uint *config) |
Andy Shevchenko | 08e9879 | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 315 | { |
| 316 | struct dm_serial_ops *ops; |
| 317 | |
Simon Glass | daaff93 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 318 | ops = serial_get_ops(dev); |
Andy Shevchenko | 08e9879 | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 319 | if (ops->getconfig) |
Simon Glass | daaff93 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 320 | return ops->getconfig(dev, config); |
Andy Shevchenko | 08e9879 | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Simon Glass | de62d48 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 325 | int serial_setconfig(struct udevice *dev, uint config) |
Patrice Chotard | 46c1d9f | 2018-08-03 15:07:38 +0200 | [diff] [blame] | 326 | { |
| 327 | struct dm_serial_ops *ops; |
| 328 | |
Simon Glass | de62d48 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 329 | ops = serial_get_ops(dev); |
Patrice Chotard | 46c1d9f | 2018-08-03 15:07:38 +0200 | [diff] [blame] | 330 | if (ops->setconfig) |
Simon Glass | de62d48 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 331 | return ops->setconfig(dev, config); |
Patrice Chotard | 46c1d9f | 2018-08-03 15:07:38 +0200 | [diff] [blame] | 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 336 | int serial_getinfo(struct udevice *dev, struct serial_device_info *info) |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 337 | { |
| 338 | struct dm_serial_ops *ops; |
| 339 | |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 340 | if (!info) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | info->baudrate = gd->baudrate; |
| 344 | |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 345 | ops = serial_get_ops(dev); |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 346 | if (ops->getinfo) |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 347 | return ops->getinfo(dev, info); |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 348 | |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 352 | void serial_stdio_init(void) |
| 353 | { |
| 354 | } |
| 355 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 356 | #if defined(CONFIG_DM_STDIO) |
| 357 | |
| 358 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 359 | static void serial_stub_putc(struct stdio_dev *sdev, const char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 360 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 361 | _serial_putc(sdev->priv, ch); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 362 | } |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 363 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 364 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 365 | static void serial_stub_puts(struct stdio_dev *sdev, const char *str) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 366 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 367 | _serial_puts(sdev->priv, str); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 368 | } |
| 369 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 370 | static int serial_stub_getc(struct stdio_dev *sdev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 371 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 372 | return _serial_getc(sdev->priv); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 373 | } |
| 374 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 375 | static int serial_stub_tstc(struct stdio_dev *sdev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 376 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 377 | return _serial_tstc(sdev->priv); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 378 | } |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame] | 379 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 380 | |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 381 | /** |
| 382 | * on_baudrate() - Update the actual baudrate when the env var changes |
| 383 | * |
| 384 | * This will check for a valid baudrate and only apply it if valid. |
| 385 | */ |
| 386 | static int on_baudrate(const char *name, const char *value, enum env_op op, |
| 387 | int flags) |
| 388 | { |
| 389 | int i; |
| 390 | int baudrate; |
| 391 | |
| 392 | switch (op) { |
| 393 | case env_op_create: |
| 394 | case env_op_overwrite: |
| 395 | /* |
| 396 | * Switch to new baudrate if new baudrate is supported |
| 397 | */ |
| 398 | baudrate = simple_strtoul(value, NULL, 10); |
| 399 | |
| 400 | /* Not actually changing */ |
| 401 | if (gd->baudrate == baudrate) |
| 402 | return 0; |
| 403 | |
| 404 | for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { |
| 405 | if (baudrate == baudrate_table[i]) |
| 406 | break; |
| 407 | } |
| 408 | if (i == ARRAY_SIZE(baudrate_table)) { |
| 409 | if ((flags & H_FORCE) == 0) |
| 410 | printf("## Baudrate %d bps not supported\n", |
| 411 | baudrate); |
| 412 | return 1; |
| 413 | } |
| 414 | if ((flags & H_INTERACTIVE) != 0) { |
| 415 | printf("## Switch baudrate to %d bps and press ENTER ...\n", |
| 416 | baudrate); |
| 417 | udelay(50000); |
| 418 | } |
| 419 | |
| 420 | gd->baudrate = baudrate; |
| 421 | |
| 422 | serial_setbrg(); |
| 423 | |
| 424 | udelay(50000); |
| 425 | |
| 426 | if ((flags & H_INTERACTIVE) != 0) |
| 427 | while (1) { |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 428 | if (getchar() == '\r') |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | case env_op_delete: |
| 434 | printf("## Baudrate may not be deleted\n"); |
| 435 | return 1; |
| 436 | default: |
| 437 | return 0; |
| 438 | } |
| 439 | } |
| 440 | U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 441 | |
Simon Glass | 30a1349 | 2015-12-13 21:36:58 -0700 | [diff] [blame] | 442 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 443 | static int serial_post_probe(struct udevice *dev) |
| 444 | { |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 445 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 446 | #ifdef CONFIG_DM_STDIO |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 447 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 448 | struct stdio_dev sdev; |
| 449 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 450 | int ret; |
| 451 | |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 452 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 453 | if (ops->setbrg) |
| 454 | ops->setbrg += gd->reloc_off; |
| 455 | if (ops->getc) |
| 456 | ops->getc += gd->reloc_off; |
| 457 | if (ops->putc) |
| 458 | ops->putc += gd->reloc_off; |
| 459 | if (ops->pending) |
| 460 | ops->pending += gd->reloc_off; |
| 461 | if (ops->clear) |
| 462 | ops->clear += gd->reloc_off; |
Andy Shevchenko | 08e9879 | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 463 | if (ops->getconfig) |
| 464 | ops->getconfig += gd->reloc_off; |
Patrice Chotard | 46c1d9f | 2018-08-03 15:07:38 +0200 | [diff] [blame] | 465 | if (ops->setconfig) |
| 466 | ops->setconfig += gd->reloc_off; |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 467 | #if CONFIG_POST & CONFIG_SYS_POST_UART |
| 468 | if (ops->loop) |
Andy Shevchenko | 84139bc | 2018-11-05 18:24:03 +0200 | [diff] [blame] | 469 | ops->loop += gd->reloc_off; |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 470 | #endif |
Andy Shevchenko | 44f21da | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 471 | if (ops->getinfo) |
| 472 | ops->getinfo += gd->reloc_off; |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 473 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 474 | /* Set the baud rate */ |
| 475 | if (ops->setbrg) { |
| 476 | ret = ops->setbrg(dev, gd->baudrate); |
| 477 | if (ret) |
| 478 | return ret; |
| 479 | } |
| 480 | |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 481 | #ifdef CONFIG_DM_STDIO |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 482 | if (!(gd->flags & GD_FLG_RELOC)) |
| 483 | return 0; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 484 | memset(&sdev, '\0', sizeof(sdev)); |
| 485 | |
| 486 | strncpy(sdev.name, dev->name, sizeof(sdev.name)); |
Simon Glass | 10a7fe9 | 2017-07-27 09:31:04 -0600 | [diff] [blame] | 487 | sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 488 | sdev.priv = dev; |
| 489 | sdev.putc = serial_stub_putc; |
| 490 | sdev.puts = serial_stub_puts; |
| 491 | sdev.getc = serial_stub_getc; |
| 492 | sdev.tstc = serial_stub_tstc; |
Stefan Roese | 0207283 | 2017-08-16 17:37:16 +0200 | [diff] [blame] | 493 | |
| 494 | #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) |
| 495 | /* Allocate the RX buffer */ |
| 496 | upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE); |
| 497 | #endif |
| 498 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 499 | stdio_register_dev(&sdev, &upriv->sdev); |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 500 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static int serial_pre_remove(struct udevice *dev) |
| 505 | { |
Simon Glass | 3110ece | 2016-10-17 20:13:02 -0600 | [diff] [blame] | 506 | #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 507 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 508 | |
Stefan Roese | dcc4b07 | 2017-04-24 09:48:01 +0200 | [diff] [blame] | 509 | if (stdio_deregister_dev(upriv->sdev, true)) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 510 | return -EPERM; |
| 511 | #endif |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | UCLASS_DRIVER(serial) = { |
| 517 | .id = UCLASS_SERIAL, |
| 518 | .name = "serial", |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 519 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 520 | .post_probe = serial_post_probe, |
| 521 | .pre_remove = serial_pre_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 522 | .per_device_auto = sizeof(struct serial_dev_priv), |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 523 | }; |
Simon Glass | 30a1349 | 2015-12-13 21:36:58 -0700 | [diff] [blame] | 524 | #endif |