1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 The Chromium OS Authors.
6 #define LOG_CATEGORY UCLASS_SERIAL
10 #include <env_internal.h>
15 #include <stdio_dev.h>
17 #include <asm/global_data.h>
19 #include <dm/device-internal.h>
20 #include <dm/of_access.h>
21 #include <linux/delay.h>
23 DECLARE_GLOBAL_DATA_PTR;
26 * Table with supported baudrates (defined in config_xyz.h)
28 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
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"
34 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
35 static int serial_check_stdout(const void *blob, struct udevice **devp)
38 const char *str, *p, *name;
41 /* Check for a chosen console */
42 str = fdtdec_get_chosen_prop(blob, "stdout-path");
45 namelen = p ? p - str : strlen(str);
46 node = fdt_path_offset_namelen(blob, str, namelen);
50 * Deal with things like
51 * stdout-path = "serial0:115200n8";
53 * We need to look up the alias and then follow it to
56 name = fdt_get_alias_namelen(blob, str, namelen);
58 node = fdt_path_offset(blob, name);
63 node = fdt_path_offset(blob, "console");
64 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
68 * If the console is not marked to be bound before relocation, bind it
71 if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
73 if (!device_probe(*devp))
80 static void serial_find_console_or_panic(void)
82 const void *blob = gd->fdt_blob;
84 #ifdef CONFIG_SERIAL_SEARCH_ALL
88 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
89 uclass_first_device(UCLASS_SERIAL, &dev);
91 gd->cur_serial_dev = dev;
94 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
95 /* Live tree has support for stdout */
96 if (of_live_active()) {
97 struct device_node *np = of_get_stdout();
99 if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
100 np_to_ofnode(np), &dev)) {
101 gd->cur_serial_dev = dev;
105 if (!serial_check_stdout(blob, &dev)) {
106 gd->cur_serial_dev = dev;
111 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
113 * Try to use CONFIG_CONS_INDEX if available (it is numbered
116 * Failing that, get the device with sequence number 0, or in
117 * extremis just the first working serial device we can find.
118 * But we insist on having a console (even if it is silent).
120 #ifdef CONFIG_CONS_INDEX
121 #define INDEX (CONFIG_CONS_INDEX - 1)
126 #ifdef CONFIG_SERIAL_SEARCH_ALL
127 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
128 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
129 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) {
130 gd->cur_serial_dev = dev;
135 /* Search for any working device */
136 for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
138 ret = uclass_next_device_check(&dev)) {
140 /* Device did succeed probing */
141 gd->cur_serial_dev = dev;
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;
157 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
158 panic_str("No serial driver found");
161 #endif /* CONFIG_SERIAL_PRESENT */
163 /* Called prior to relocation */
164 int serial_init(void)
166 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
167 serial_find_console_or_panic();
168 gd->flags |= GD_FLG_SERIAL_READY;
175 /* Called after relocation */
176 int serial_initialize(void)
178 /* Scanning uclass to probe devices */
179 if (IS_ENABLED(CONFIG_SERIAL_PROBE_ALL)) {
182 ret = uclass_probe_all(UCLASS_SERIAL);
187 return serial_init();
190 static void _serial_putc(struct udevice *dev, char ch)
192 struct dm_serial_ops *ops = serial_get_ops(dev);
196 _serial_putc(dev, '\r');
199 err = ops->putc(dev, ch);
200 } while (err == -EAGAIN);
203 static void _serial_puts(struct udevice *dev, const char *str)
206 _serial_putc(dev, *str++);
209 static int __serial_getc(struct udevice *dev)
211 struct dm_serial_ops *ops = serial_get_ops(dev);
215 err = ops->getc(dev);
218 } while (err == -EAGAIN);
220 return err >= 0 ? err : 0;
223 static int __serial_tstc(struct udevice *dev)
225 struct dm_serial_ops *ops = serial_get_ops(dev);
228 return ops->pending(dev, true);
233 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
234 static int _serial_tstc(struct udevice *dev)
236 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
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;
244 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
247 static int _serial_getc(struct udevice *dev)
249 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
252 if (upriv->rd_ptr == upriv->wr_ptr)
253 return __serial_getc(dev);
255 val = upriv->buf[upriv->rd_ptr++];
256 upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
261 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
263 static int _serial_getc(struct udevice *dev)
265 return __serial_getc(dev);
268 static int _serial_tstc(struct udevice *dev)
270 return __serial_tstc(dev);
272 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
274 void serial_putc(char ch)
276 if (gd->cur_serial_dev)
277 _serial_putc(gd->cur_serial_dev, ch);
280 void serial_puts(const char *str)
282 if (gd->cur_serial_dev)
283 _serial_puts(gd->cur_serial_dev, str);
286 int serial_getc(void)
288 if (!gd->cur_serial_dev)
291 return _serial_getc(gd->cur_serial_dev);
294 int serial_tstc(void)
296 if (!gd->cur_serial_dev)
299 return _serial_tstc(gd->cur_serial_dev);
302 void serial_setbrg(void)
304 struct dm_serial_ops *ops;
306 if (!gd->cur_serial_dev)
309 ops = serial_get_ops(gd->cur_serial_dev);
311 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
314 int serial_getconfig(struct udevice *dev, uint *config)
316 struct dm_serial_ops *ops;
318 ops = serial_get_ops(dev);
320 return ops->getconfig(dev, config);
325 int serial_setconfig(struct udevice *dev, uint config)
327 struct dm_serial_ops *ops;
329 ops = serial_get_ops(dev);
331 return ops->setconfig(dev, config);
336 int serial_getinfo(struct udevice *dev, struct serial_device_info *info)
338 struct dm_serial_ops *ops;
343 info->baudrate = gd->baudrate;
345 ops = serial_get_ops(dev);
347 return ops->getinfo(dev, info);
352 void serial_stdio_init(void)
356 #if defined(CONFIG_DM_STDIO)
358 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
359 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
361 _serial_putc(sdev->priv, ch);
365 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
367 _serial_puts(sdev->priv, str);
370 static int serial_stub_getc(struct stdio_dev *sdev)
372 return _serial_getc(sdev->priv);
375 static int serial_stub_tstc(struct stdio_dev *sdev)
377 return _serial_tstc(sdev->priv);
382 * on_baudrate() - Update the actual baudrate when the env var changes
384 * This will check for a valid baudrate and only apply it if valid.
386 static int on_baudrate(const char *name, const char *value, enum env_op op,
394 case env_op_overwrite:
396 * Switch to new baudrate if new baudrate is supported
398 baudrate = dectoul(value, NULL);
400 /* Not actually changing */
401 if (gd->baudrate == baudrate)
404 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
405 if (baudrate == baudrate_table[i])
408 if (i == ARRAY_SIZE(baudrate_table)) {
409 if ((flags & H_FORCE) == 0)
410 printf("## Baudrate %d bps not supported\n",
414 if ((flags & H_INTERACTIVE) != 0) {
415 printf("## Switch baudrate to %d bps and press ENTER ...\n",
420 gd->baudrate = baudrate;
426 if ((flags & H_INTERACTIVE) != 0)
428 if (getchar() == '\r')
434 printf("## Baudrate may not be deleted\n");
440 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
442 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
443 static int serial_post_probe(struct udevice *dev)
445 struct dm_serial_ops *ops = serial_get_ops(dev);
446 #ifdef CONFIG_DM_STDIO
447 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
448 struct stdio_dev sdev;
452 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
454 ops->setbrg += gd->reloc_off;
456 ops->getc += gd->reloc_off;
458 ops->putc += gd->reloc_off;
460 ops->pending += gd->reloc_off;
462 ops->clear += gd->reloc_off;
464 ops->getconfig += gd->reloc_off;
466 ops->setconfig += gd->reloc_off;
467 #if CONFIG_POST & CONFIG_SYS_POST_UART
469 ops->loop += gd->reloc_off;
472 ops->getinfo += gd->reloc_off;
474 /* Set the baud rate */
476 ret = ops->setbrg(dev, gd->baudrate);
481 #ifdef CONFIG_DM_STDIO
482 if (!(gd->flags & GD_FLG_RELOC))
484 memset(&sdev, '\0', sizeof(sdev));
486 strncpy(sdev.name, dev->name, sizeof(sdev.name));
487 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
489 sdev.putc = serial_stub_putc;
490 sdev.puts = serial_stub_puts;
491 sdev.getc = serial_stub_getc;
492 sdev.tstc = serial_stub_tstc;
494 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
495 /* Allocate the RX buffer */
496 upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
499 stdio_register_dev(&sdev, &upriv->sdev);
504 static int serial_pre_remove(struct udevice *dev)
506 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
507 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
509 if (stdio_deregister_dev(upriv->sdev, true))
516 UCLASS_DRIVER(serial) = {
519 .flags = DM_UC_FLAG_SEQ_ALIAS,
520 .post_probe = serial_post_probe,
521 .pre_remove = serial_pre_remove,
522 .per_device_auto = sizeof(struct serial_dev_priv),