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>
21 DECLARE_GLOBAL_DATA_PTR;
23 /* The currently-selected console serial device */
24 struct udevice *cur_dev __attribute__ ((section(".data")));
27 * Table with supported baudrates (defined in config_xyz.h)
29 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
31 #ifndef CONFIG_SYS_MALLOC_F_LEN
32 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
35 static void serial_find_console_or_panic(void)
37 #ifdef CONFIG_OF_CONTROL
40 /* Check for a chosen console */
41 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
43 node = fdtdec_get_alias_node(gd->fdt_blob, "console");
44 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
48 * If the console is not marked to be bound before relocation, bind
52 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
53 if (!device_probe(cur_dev))
59 * Try to use CONFIG_CONS_INDEX if available (it is numbered from 1!).
61 * Failing that, get the device with sequence number 0, or in extremis
62 * just the first serial device we can find. But we insist on having
63 * a console (even if it is silent).
65 #ifdef CONFIG_CONS_INDEX
66 #define INDEX (CONFIG_CONS_INDEX - 1)
70 if (uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &cur_dev) &&
71 uclass_get_device(UCLASS_SERIAL, INDEX, &cur_dev) &&
72 (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
73 panic("No serial driver found");
77 /* Called prior to relocation */
80 serial_find_console_or_panic();
81 gd->flags |= GD_FLG_SERIAL_READY;
86 /* Called after relocation */
87 void serial_initialize(void)
89 serial_find_console_or_panic();
92 static void _serial_putc(struct udevice *dev, char ch)
94 struct dm_serial_ops *ops = serial_get_ops(dev);
98 err = ops->putc(dev, ch);
99 } while (err == -EAGAIN);
101 _serial_putc(dev, '\r');
104 static void _serial_puts(struct udevice *dev, const char *str)
107 _serial_putc(dev, *str++);
110 static int _serial_getc(struct udevice *dev)
112 struct dm_serial_ops *ops = serial_get_ops(dev);
116 err = ops->getc(dev);
119 } while (err == -EAGAIN);
121 return err >= 0 ? err : 0;
124 static int _serial_tstc(struct udevice *dev)
126 struct dm_serial_ops *ops = serial_get_ops(dev);
129 return ops->pending(dev, true);
134 void serial_putc(char ch)
136 _serial_putc(cur_dev, ch);
139 void serial_puts(const char *str)
141 _serial_puts(cur_dev, str);
144 int serial_getc(void)
146 return _serial_getc(cur_dev);
149 int serial_tstc(void)
151 return _serial_tstc(cur_dev);
154 void serial_setbrg(void)
156 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
159 ops->setbrg(cur_dev, gd->baudrate);
162 void serial_stdio_init(void)
166 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
168 _serial_putc(sdev->priv, ch);
171 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
173 _serial_puts(sdev->priv, str);
176 int serial_stub_getc(struct stdio_dev *sdev)
178 return _serial_getc(sdev->priv);
181 int serial_stub_tstc(struct stdio_dev *sdev)
183 return _serial_tstc(sdev->priv);
187 * on_baudrate() - Update the actual baudrate when the env var changes
189 * This will check for a valid baudrate and only apply it if valid.
191 static int on_baudrate(const char *name, const char *value, enum env_op op,
199 case env_op_overwrite:
201 * Switch to new baudrate if new baudrate is supported
203 baudrate = simple_strtoul(value, NULL, 10);
205 /* Not actually changing */
206 if (gd->baudrate == baudrate)
209 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
210 if (baudrate == baudrate_table[i])
213 if (i == ARRAY_SIZE(baudrate_table)) {
214 if ((flags & H_FORCE) == 0)
215 printf("## Baudrate %d bps not supported\n",
219 if ((flags & H_INTERACTIVE) != 0) {
220 printf("## Switch baudrate to %d bps and press ENTER ...\n",
225 gd->baudrate = baudrate;
231 if ((flags & H_INTERACTIVE) != 0)
239 printf("## Baudrate may not be deleted\n");
245 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
247 static int serial_post_probe(struct udevice *dev)
249 struct stdio_dev sdev;
250 struct dm_serial_ops *ops = serial_get_ops(dev);
251 struct serial_dev_priv *upriv = dev->uclass_priv;
254 /* Set the baud rate */
256 ret = ops->setbrg(dev, gd->baudrate);
261 if (!(gd->flags & GD_FLG_RELOC))
264 memset(&sdev, '\0', sizeof(sdev));
266 strncpy(sdev.name, dev->name, sizeof(sdev.name));
267 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
269 sdev.putc = serial_stub_putc;
270 sdev.puts = serial_stub_puts;
271 sdev.getc = serial_stub_getc;
272 sdev.tstc = serial_stub_tstc;
273 stdio_register_dev(&sdev, &upriv->sdev);
278 static int serial_pre_remove(struct udevice *dev)
280 #ifdef CONFIG_SYS_STDIO_DEREGISTER
281 struct serial_dev_priv *upriv = dev->uclass_priv;
283 if (stdio_deregister_dev(upriv->sdev, 0))
290 UCLASS_DRIVER(serial) = {
293 .post_probe = serial_post_probe,
294 .pre_remove = serial_pre_remove,
295 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),