2 * Copyright (c) 2014 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <environment.h>
14 #include <stdio_dev.h>
17 #include <dm/device-internal.h>
19 DECLARE_GLOBAL_DATA_PTR;
22 * Table with supported baudrates (defined in config_xyz.h)
24 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
26 #ifndef CONFIG_SYS_MALLOC_F_LEN
27 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
30 static void serial_find_console_or_panic(void)
32 const void *blob = gd->fdt_blob;
36 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
37 uclass_first_device(UCLASS_SERIAL, &dev);
39 gd->cur_serial_dev = dev;
42 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
43 /* Check for a chosen console */
44 node = fdtdec_get_chosen_node(blob, "stdout-path");
46 const char *str, *p, *name;
49 * Deal with things like
50 * stdout-path = "serial0:115200n8";
52 * We need to look up the alias and then follow it to
55 str = fdtdec_get_chosen_prop(blob, "stdout-path");
58 name = fdt_get_alias_namelen(blob, str,
59 p ? p - str : strlen(str));
61 node = fdt_path_offset(blob, name);
65 node = fdt_path_offset(blob, "console");
66 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
68 gd->cur_serial_dev = dev;
73 * If the console is not marked to be bound before relocation,
77 !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
79 if (!device_probe(dev)) {
80 gd->cur_serial_dev = dev;
85 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
87 * Try to use CONFIG_CONS_INDEX if available (it is numbered
90 * Failing that, get the device with sequence number 0, or in
91 * extremis just the first serial device we can find. But we
92 * insist on having a console (even if it is silent).
94 #ifdef CONFIG_CONS_INDEX
95 #define INDEX (CONFIG_CONS_INDEX - 1)
99 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
100 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
101 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
102 gd->cur_serial_dev = dev;
108 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
109 panic_str("No serial driver found");
113 /* Called prior to relocation */
114 int serial_init(void)
116 serial_find_console_or_panic();
117 gd->flags |= GD_FLG_SERIAL_READY;
122 /* Called after relocation */
123 void serial_initialize(void)
128 static void _serial_putc(struct udevice *dev, char ch)
130 struct dm_serial_ops *ops = serial_get_ops(dev);
134 _serial_putc(dev, '\r');
137 err = ops->putc(dev, ch);
138 } while (err == -EAGAIN);
141 static void _serial_puts(struct udevice *dev, const char *str)
144 _serial_putc(dev, *str++);
147 static int _serial_getc(struct udevice *dev)
149 struct dm_serial_ops *ops = serial_get_ops(dev);
153 err = ops->getc(dev);
156 } while (err == -EAGAIN);
158 return err >= 0 ? err : 0;
161 static int _serial_tstc(struct udevice *dev)
163 struct dm_serial_ops *ops = serial_get_ops(dev);
166 return ops->pending(dev, true);
171 void serial_putc(char ch)
173 if (gd->cur_serial_dev)
174 _serial_putc(gd->cur_serial_dev, ch);
177 void serial_puts(const char *str)
179 if (gd->cur_serial_dev)
180 _serial_puts(gd->cur_serial_dev, str);
183 int serial_getc(void)
185 if (!gd->cur_serial_dev)
188 return _serial_getc(gd->cur_serial_dev);
191 int serial_tstc(void)
193 if (!gd->cur_serial_dev)
196 return _serial_tstc(gd->cur_serial_dev);
199 void serial_setbrg(void)
201 struct dm_serial_ops *ops;
203 if (!gd->cur_serial_dev)
206 ops = serial_get_ops(gd->cur_serial_dev);
208 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
211 void serial_stdio_init(void)
215 #if defined(CONFIG_DM_STDIO)
217 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
218 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
220 _serial_putc(sdev->priv, ch);
224 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
226 _serial_puts(sdev->priv, str);
229 static int serial_stub_getc(struct stdio_dev *sdev)
231 return _serial_getc(sdev->priv);
234 static int serial_stub_tstc(struct stdio_dev *sdev)
236 return _serial_tstc(sdev->priv);
241 * on_baudrate() - Update the actual baudrate when the env var changes
243 * This will check for a valid baudrate and only apply it if valid.
245 static int on_baudrate(const char *name, const char *value, enum env_op op,
253 case env_op_overwrite:
255 * Switch to new baudrate if new baudrate is supported
257 baudrate = simple_strtoul(value, NULL, 10);
259 /* Not actually changing */
260 if (gd->baudrate == baudrate)
263 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
264 if (baudrate == baudrate_table[i])
267 if (i == ARRAY_SIZE(baudrate_table)) {
268 if ((flags & H_FORCE) == 0)
269 printf("## Baudrate %d bps not supported\n",
273 if ((flags & H_INTERACTIVE) != 0) {
274 printf("## Switch baudrate to %d bps and press ENTER ...\n",
279 gd->baudrate = baudrate;
285 if ((flags & H_INTERACTIVE) != 0)
293 printf("## Baudrate may not be deleted\n");
299 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
301 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
302 static int serial_post_probe(struct udevice *dev)
304 struct dm_serial_ops *ops = serial_get_ops(dev);
305 #ifdef CONFIG_DM_STDIO
306 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
307 struct stdio_dev sdev;
311 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
313 ops->setbrg += gd->reloc_off;
315 ops->getc += gd->reloc_off;
317 ops->putc += gd->reloc_off;
319 ops->pending += gd->reloc_off;
321 ops->clear += gd->reloc_off;
322 #if CONFIG_POST & CONFIG_SYS_POST_UART
324 ops->loop += gd->reloc_off
327 /* Set the baud rate */
329 ret = ops->setbrg(dev, gd->baudrate);
334 #ifdef CONFIG_DM_STDIO
335 if (!(gd->flags & GD_FLG_RELOC))
337 memset(&sdev, '\0', sizeof(sdev));
339 strncpy(sdev.name, dev->name, sizeof(sdev.name));
340 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
342 sdev.putc = serial_stub_putc;
343 sdev.puts = serial_stub_puts;
344 sdev.getc = serial_stub_getc;
345 sdev.tstc = serial_stub_tstc;
346 stdio_register_dev(&sdev, &upriv->sdev);
351 static int serial_pre_remove(struct udevice *dev)
353 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
354 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
356 if (stdio_deregister_dev(upriv->sdev, true))
363 UCLASS_DRIVER(serial) = {
366 .flags = DM_UC_FLAG_SEQ_ALIAS,
367 .post_probe = serial_post_probe,
368 .pre_remove = serial_pre_remove,
369 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),