1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 The Chromium OS Authors.
8 #include <environment.h>
12 #include <stdio_dev.h>
15 #include <dm/device-internal.h>
16 #include <dm/of_access.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 * Table with supported baudrates (defined in config_xyz.h)
23 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
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"
29 static int serial_check_stdout(const void *blob, struct udevice **devp)
33 /* Check for a chosen console */
34 node = fdtdec_get_chosen_node(blob, "stdout-path");
36 const char *str, *p, *name;
39 * Deal with things like
40 * stdout-path = "serial0:115200n8";
42 * We need to look up the alias and then follow it to the
45 str = fdtdec_get_chosen_prop(blob, "stdout-path");
48 name = fdt_get_alias_namelen(blob, str,
49 p ? p - str : strlen(str));
51 node = fdt_path_offset(blob, name);
55 node = fdt_path_offset(blob, "console");
56 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
60 * If the console is not marked to be bound before relocation, bind it
63 if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
65 if (!device_probe(*devp))
72 static void serial_find_console_or_panic(void)
74 const void *blob = gd->fdt_blob;
76 #ifdef CONFIG_SERIAL_SEARCH_ALL
80 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
81 uclass_first_device(UCLASS_SERIAL, &dev);
83 gd->cur_serial_dev = dev;
86 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
87 /* Live tree has support for stdout */
88 if (of_live_active()) {
89 struct device_node *np = of_get_stdout();
91 if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
92 np_to_ofnode(np), &dev)) {
93 gd->cur_serial_dev = dev;
97 if (!serial_check_stdout(blob, &dev)) {
98 gd->cur_serial_dev = dev;
103 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
105 * Try to use CONFIG_CONS_INDEX if available (it is numbered
108 * Failing that, get the device with sequence number 0, or in
109 * extremis just the first working serial device we can find.
110 * But we insist on having a console (even if it is silent).
112 #ifdef CONFIG_CONS_INDEX
113 #define INDEX (CONFIG_CONS_INDEX - 1)
118 #ifdef CONFIG_SERIAL_SEARCH_ALL
119 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
120 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
121 if (dev->flags & DM_FLAG_ACTIVATED) {
122 gd->cur_serial_dev = dev;
127 /* Search for any working device */
128 for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
130 ret = uclass_next_device_check(&dev)) {
132 /* Device did succeed probing */
133 gd->cur_serial_dev = dev;
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;
149 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
150 panic_str("No serial driver found");
154 /* Called prior to relocation */
155 int serial_init(void)
157 serial_find_console_or_panic();
158 gd->flags |= GD_FLG_SERIAL_READY;
163 /* Called after relocation */
164 void serial_initialize(void)
169 static void _serial_putc(struct udevice *dev, char ch)
171 struct dm_serial_ops *ops = serial_get_ops(dev);
175 _serial_putc(dev, '\r');
178 err = ops->putc(dev, ch);
179 } while (err == -EAGAIN);
182 static void _serial_puts(struct udevice *dev, const char *str)
185 _serial_putc(dev, *str++);
188 static int __serial_getc(struct udevice *dev)
190 struct dm_serial_ops *ops = serial_get_ops(dev);
194 err = ops->getc(dev);
197 } while (err == -EAGAIN);
199 return err >= 0 ? err : 0;
202 static int __serial_tstc(struct udevice *dev)
204 struct dm_serial_ops *ops = serial_get_ops(dev);
207 return ops->pending(dev, true);
212 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
213 static int _serial_tstc(struct udevice *dev)
215 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
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;
223 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
226 static int _serial_getc(struct udevice *dev)
228 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
231 if (upriv->rd_ptr == upriv->wr_ptr)
232 return __serial_getc(dev);
234 val = upriv->buf[upriv->rd_ptr++];
235 upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
240 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
242 static int _serial_getc(struct udevice *dev)
244 return __serial_getc(dev);
247 static int _serial_tstc(struct udevice *dev)
249 return __serial_tstc(dev);
251 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
253 void serial_putc(char ch)
255 if (gd->cur_serial_dev)
256 _serial_putc(gd->cur_serial_dev, ch);
259 void serial_puts(const char *str)
261 if (gd->cur_serial_dev)
262 _serial_puts(gd->cur_serial_dev, str);
265 int serial_getc(void)
267 if (!gd->cur_serial_dev)
270 return _serial_getc(gd->cur_serial_dev);
273 int serial_tstc(void)
275 if (!gd->cur_serial_dev)
278 return _serial_tstc(gd->cur_serial_dev);
281 void serial_setbrg(void)
283 struct dm_serial_ops *ops;
285 if (!gd->cur_serial_dev)
288 ops = serial_get_ops(gd->cur_serial_dev);
290 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
293 void serial_stdio_init(void)
297 #if defined(CONFIG_DM_STDIO)
299 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
300 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
302 _serial_putc(sdev->priv, ch);
306 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
308 _serial_puts(sdev->priv, str);
311 static int serial_stub_getc(struct stdio_dev *sdev)
313 return _serial_getc(sdev->priv);
316 static int serial_stub_tstc(struct stdio_dev *sdev)
318 return _serial_tstc(sdev->priv);
323 * on_baudrate() - Update the actual baudrate when the env var changes
325 * This will check for a valid baudrate and only apply it if valid.
327 static int on_baudrate(const char *name, const char *value, enum env_op op,
335 case env_op_overwrite:
337 * Switch to new baudrate if new baudrate is supported
339 baudrate = simple_strtoul(value, NULL, 10);
341 /* Not actually changing */
342 if (gd->baudrate == baudrate)
345 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
346 if (baudrate == baudrate_table[i])
349 if (i == ARRAY_SIZE(baudrate_table)) {
350 if ((flags & H_FORCE) == 0)
351 printf("## Baudrate %d bps not supported\n",
355 if ((flags & H_INTERACTIVE) != 0) {
356 printf("## Switch baudrate to %d bps and press ENTER ...\n",
361 gd->baudrate = baudrate;
367 if ((flags & H_INTERACTIVE) != 0)
375 printf("## Baudrate may not be deleted\n");
381 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
383 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
384 static int serial_post_probe(struct udevice *dev)
386 struct dm_serial_ops *ops = serial_get_ops(dev);
387 #ifdef CONFIG_DM_STDIO
388 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
389 struct stdio_dev sdev;
393 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
395 ops->setbrg += gd->reloc_off;
397 ops->getc += gd->reloc_off;
399 ops->putc += gd->reloc_off;
401 ops->pending += gd->reloc_off;
403 ops->clear += gd->reloc_off;
404 #if CONFIG_POST & CONFIG_SYS_POST_UART
406 ops->loop += gd->reloc_off
409 /* Set the baud rate */
411 ret = ops->setbrg(dev, gd->baudrate);
416 #ifdef CONFIG_DM_STDIO
417 if (!(gd->flags & GD_FLG_RELOC))
419 memset(&sdev, '\0', sizeof(sdev));
421 strncpy(sdev.name, dev->name, sizeof(sdev.name));
422 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
424 sdev.putc = serial_stub_putc;
425 sdev.puts = serial_stub_puts;
426 sdev.getc = serial_stub_getc;
427 sdev.tstc = serial_stub_tstc;
429 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
430 /* Allocate the RX buffer */
431 upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
434 stdio_register_dev(&sdev, &upriv->sdev);
439 static int serial_pre_remove(struct udevice *dev)
441 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
442 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
444 if (stdio_deregister_dev(upriv->sdev, true))
451 UCLASS_DRIVER(serial) = {
454 .flags = DM_UC_FLAG_SEQ_ALIAS,
455 .post_probe = serial_post_probe,
456 .pre_remove = serial_pre_remove,
457 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),