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)
36 /* Check for a chosen console */
37 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
39 node = fdt_path_offset(gd->fdt_blob, "console");
40 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
42 gd->cur_serial_dev = dev;
47 * If the console is not marked to be bound before relocation,
51 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &dev)) {
52 if (!device_probe(dev)) {
53 gd->cur_serial_dev = dev;
59 * Try to use CONFIG_CONS_INDEX if available (it is numbered
62 * Failing that, get the device with sequence number 0, or in
63 * extremis just the first serial device we can find. But we
64 * insist on having a console (even if it is silent).
66 #ifdef CONFIG_CONS_INDEX
67 #define INDEX (CONFIG_CONS_INDEX - 1)
71 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
72 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
73 (!uclass_first_device(UCLASS_SERIAL, &dev) || dev)) {
74 gd->cur_serial_dev = dev;
80 panic_str("No serial driver found");
83 /* Called prior to relocation */
86 serial_find_console_or_panic();
87 gd->flags |= GD_FLG_SERIAL_READY;
92 /* Called after relocation */
93 void serial_initialize(void)
95 serial_find_console_or_panic();
98 static void _serial_putc(struct udevice *dev, char ch)
100 struct dm_serial_ops *ops = serial_get_ops(dev);
104 err = ops->putc(dev, ch);
105 } while (err == -EAGAIN);
107 _serial_putc(dev, '\r');
110 static void _serial_puts(struct udevice *dev, const char *str)
113 _serial_putc(dev, *str++);
116 static int _serial_getc(struct udevice *dev)
118 struct dm_serial_ops *ops = serial_get_ops(dev);
122 err = ops->getc(dev);
125 } while (err == -EAGAIN);
127 return err >= 0 ? err : 0;
130 static int _serial_tstc(struct udevice *dev)
132 struct dm_serial_ops *ops = serial_get_ops(dev);
135 return ops->pending(dev, true);
140 void serial_putc(char ch)
142 _serial_putc(gd->cur_serial_dev, ch);
145 void serial_puts(const char *str)
147 _serial_puts(gd->cur_serial_dev, str);
150 int serial_getc(void)
152 return _serial_getc(gd->cur_serial_dev);
155 int serial_tstc(void)
157 return _serial_tstc(gd->cur_serial_dev);
160 void serial_setbrg(void)
162 struct dm_serial_ops *ops = serial_get_ops(gd->cur_serial_dev);
165 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
168 void serial_stdio_init(void)
172 #ifdef CONFIG_DM_STDIO
173 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
175 _serial_putc(sdev->priv, ch);
179 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
181 _serial_puts(sdev->priv, str);
184 int serial_stub_getc(struct stdio_dev *sdev)
186 return _serial_getc(sdev->priv);
189 int serial_stub_tstc(struct stdio_dev *sdev)
191 return _serial_tstc(sdev->priv);
195 * on_baudrate() - Update the actual baudrate when the env var changes
197 * This will check for a valid baudrate and only apply it if valid.
199 static int on_baudrate(const char *name, const char *value, enum env_op op,
207 case env_op_overwrite:
209 * Switch to new baudrate if new baudrate is supported
211 baudrate = simple_strtoul(value, NULL, 10);
213 /* Not actually changing */
214 if (gd->baudrate == baudrate)
217 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
218 if (baudrate == baudrate_table[i])
221 if (i == ARRAY_SIZE(baudrate_table)) {
222 if ((flags & H_FORCE) == 0)
223 printf("## Baudrate %d bps not supported\n",
227 if ((flags & H_INTERACTIVE) != 0) {
228 printf("## Switch baudrate to %d bps and press ENTER ...\n",
233 gd->baudrate = baudrate;
239 if ((flags & H_INTERACTIVE) != 0)
247 printf("## Baudrate may not be deleted\n");
253 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
255 static int serial_post_probe(struct udevice *dev)
257 struct dm_serial_ops *ops = serial_get_ops(dev);
258 #ifdef CONFIG_DM_STDIO
259 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
260 struct stdio_dev sdev;
264 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
266 ops->setbrg += gd->reloc_off;
268 ops->getc += gd->reloc_off;
270 ops->putc += gd->reloc_off;
272 ops->pending += gd->reloc_off;
274 ops->clear += gd->reloc_off;
275 #if CONFIG_POST & CONFIG_SYS_POST_UART
277 ops->loop += gd->reloc_off
280 /* Set the baud rate */
282 ret = ops->setbrg(dev, gd->baudrate);
287 #ifdef CONFIG_DM_STDIO
288 if (!(gd->flags & GD_FLG_RELOC))
290 memset(&sdev, '\0', sizeof(sdev));
292 strncpy(sdev.name, dev->name, sizeof(sdev.name));
293 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
295 sdev.putc = serial_stub_putc;
296 sdev.puts = serial_stub_puts;
297 sdev.getc = serial_stub_getc;
298 sdev.tstc = serial_stub_tstc;
299 stdio_register_dev(&sdev, &upriv->sdev);
304 static int serial_pre_remove(struct udevice *dev)
306 #ifdef CONFIG_SYS_STDIO_DEREGISTER
307 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
309 if (stdio_deregister_dev(upriv->sdev, 0))
316 UCLASS_DRIVER(serial) = {
319 .flags = DM_UC_FLAG_SEQ_ALIAS,
320 .post_probe = serial_post_probe,
321 .pre_remove = serial_pre_remove,
322 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),