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_CONTROL) && blob) {
37 /* Check for a chosen console */
38 node = fdtdec_get_chosen_node(blob, "stdout-path");
40 const char *str, *p, *name;
43 * Deal with things like
44 * stdout-path = "serial0:115200n8";
46 * We need to look up the alias and then follow it to
49 str = fdtdec_get_chosen_prop(blob, "stdout-path");
52 name = fdt_get_alias_namelen(blob, str,
53 p ? p - str : strlen(str));
55 node = fdt_path_offset(blob, name);
59 node = fdt_path_offset(blob, "console");
60 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
62 gd->cur_serial_dev = dev;
67 * If the console is not marked to be bound before relocation,
71 !lists_bind_fdt(gd->dm_root, blob, node, &dev)) {
72 if (!device_probe(dev)) {
73 gd->cur_serial_dev = dev;
78 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
80 * Try to use CONFIG_CONS_INDEX if available (it is numbered
83 * Failing that, get the device with sequence number 0, or in
84 * extremis just the first serial device we can find. But we
85 * insist on having a console (even if it is silent).
87 #ifdef CONFIG_CONS_INDEX
88 #define INDEX (CONFIG_CONS_INDEX - 1)
92 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
93 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
94 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
95 gd->cur_serial_dev = dev;
101 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
102 panic_str("No serial driver found");
106 /* Called prior to relocation */
107 int serial_init(void)
109 serial_find_console_or_panic();
110 gd->flags |= GD_FLG_SERIAL_READY;
115 /* Called after relocation */
116 void serial_initialize(void)
118 serial_find_console_or_panic();
121 static void _serial_putc(struct udevice *dev, char ch)
123 struct dm_serial_ops *ops = serial_get_ops(dev);
127 err = ops->putc(dev, ch);
128 } while (err == -EAGAIN);
130 _serial_putc(dev, '\r');
133 static void _serial_puts(struct udevice *dev, const char *str)
136 _serial_putc(dev, *str++);
139 static int _serial_getc(struct udevice *dev)
141 struct dm_serial_ops *ops = serial_get_ops(dev);
145 err = ops->getc(dev);
148 } while (err == -EAGAIN);
150 return err >= 0 ? err : 0;
153 static int _serial_tstc(struct udevice *dev)
155 struct dm_serial_ops *ops = serial_get_ops(dev);
158 return ops->pending(dev, true);
163 void serial_putc(char ch)
165 if (gd->cur_serial_dev)
166 _serial_putc(gd->cur_serial_dev, ch);
169 void serial_puts(const char *str)
171 if (gd->cur_serial_dev)
172 _serial_puts(gd->cur_serial_dev, str);
175 int serial_getc(void)
177 if (!gd->cur_serial_dev)
180 return _serial_getc(gd->cur_serial_dev);
183 int serial_tstc(void)
185 if (!gd->cur_serial_dev)
188 return _serial_tstc(gd->cur_serial_dev);
191 void serial_setbrg(void)
193 struct dm_serial_ops *ops;
195 if (!gd->cur_serial_dev)
198 ops = serial_get_ops(gd->cur_serial_dev);
200 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
203 void serial_stdio_init(void)
207 #if defined(CONFIG_DM_STDIO) && CONFIG_IS_ENABLED(SERIAL_PRESENT)
208 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
210 _serial_putc(sdev->priv, ch);
214 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
216 _serial_puts(sdev->priv, str);
219 int serial_stub_getc(struct stdio_dev *sdev)
221 return _serial_getc(sdev->priv);
224 int serial_stub_tstc(struct stdio_dev *sdev)
226 return _serial_tstc(sdev->priv);
230 * on_baudrate() - Update the actual baudrate when the env var changes
232 * This will check for a valid baudrate and only apply it if valid.
234 static int on_baudrate(const char *name, const char *value, enum env_op op,
242 case env_op_overwrite:
244 * Switch to new baudrate if new baudrate is supported
246 baudrate = simple_strtoul(value, NULL, 10);
248 /* Not actually changing */
249 if (gd->baudrate == baudrate)
252 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
253 if (baudrate == baudrate_table[i])
256 if (i == ARRAY_SIZE(baudrate_table)) {
257 if ((flags & H_FORCE) == 0)
258 printf("## Baudrate %d bps not supported\n",
262 if ((flags & H_INTERACTIVE) != 0) {
263 printf("## Switch baudrate to %d bps and press ENTER ...\n",
268 gd->baudrate = baudrate;
274 if ((flags & H_INTERACTIVE) != 0)
282 printf("## Baudrate may not be deleted\n");
288 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
290 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
291 static int serial_post_probe(struct udevice *dev)
293 struct dm_serial_ops *ops = serial_get_ops(dev);
294 #ifdef CONFIG_DM_STDIO
295 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
296 struct stdio_dev sdev;
300 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
302 ops->setbrg += gd->reloc_off;
304 ops->getc += gd->reloc_off;
306 ops->putc += gd->reloc_off;
308 ops->pending += gd->reloc_off;
310 ops->clear += gd->reloc_off;
311 #if CONFIG_POST & CONFIG_SYS_POST_UART
313 ops->loop += gd->reloc_off
316 /* Set the baud rate */
318 ret = ops->setbrg(dev, gd->baudrate);
323 #ifdef CONFIG_DM_STDIO
324 if (!(gd->flags & GD_FLG_RELOC))
326 memset(&sdev, '\0', sizeof(sdev));
328 strncpy(sdev.name, dev->name, sizeof(sdev.name));
329 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
331 sdev.putc = serial_stub_putc;
332 sdev.puts = serial_stub_puts;
333 sdev.getc = serial_stub_getc;
334 sdev.tstc = serial_stub_tstc;
335 stdio_register_dev(&sdev, &upriv->sdev);
340 static int serial_pre_remove(struct udevice *dev)
342 #ifdef CONFIG_SYS_STDIO_DEREGISTER
343 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
345 if (stdio_deregister_dev(upriv->sdev, 0))
352 UCLASS_DRIVER(serial) = {
355 .flags = DM_UC_FLAG_SEQ_ALIAS,
356 .post_probe = serial_post_probe,
357 .pre_remove = serial_pre_remove,
358 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),