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