2 * Copyright (c) 2014 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <stdio_dev.h>
16 #include <dm/device-internal.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 /* The currently-selected console serial device */
23 struct udevice *cur_dev __attribute__ ((section(".data")));
25 #ifndef CONFIG_SYS_MALLOC_F_LEN
26 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
29 static void serial_find_console_or_panic(void)
31 #ifdef CONFIG_OF_CONTROL
34 /* Check for a chosen console */
35 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
37 node = fdtdec_get_alias_node(gd->fdt_blob, "console");
38 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
42 * If the console is not marked to be bound before relocation, bind
46 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
47 if (!device_probe(cur_dev))
53 * Try to use CONFIG_CONS_INDEX if available (it is numbered from 1!).
55 * Failing that, get the device with sequence number 0, or in extremis
56 * just the first serial device we can find. But we insist on having
57 * a console (even if it is silent).
59 #ifdef CONFIG_CONS_INDEX
60 #define INDEX (CONFIG_CONS_INDEX - 1)
64 if (uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &cur_dev) &&
65 uclass_get_device(UCLASS_SERIAL, INDEX, &cur_dev) &&
66 (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
67 panic("No serial driver found");
71 /* Called prior to relocation */
74 serial_find_console_or_panic();
75 gd->flags |= GD_FLG_SERIAL_READY;
80 /* Called after relocation */
81 void serial_initialize(void)
83 serial_find_console_or_panic();
86 static void _serial_putc(struct udevice *dev, char ch)
88 struct dm_serial_ops *ops = serial_get_ops(dev);
92 err = ops->putc(dev, ch);
93 } while (err == -EAGAIN);
95 _serial_putc(dev, '\r');
98 static void _serial_puts(struct udevice *dev, const char *str)
101 _serial_putc(dev, *str++);
104 static int _serial_getc(struct udevice *dev)
106 struct dm_serial_ops *ops = serial_get_ops(dev);
110 err = ops->getc(dev);
113 } while (err == -EAGAIN);
115 return err >= 0 ? err : 0;
118 static int _serial_tstc(struct udevice *dev)
120 struct dm_serial_ops *ops = serial_get_ops(dev);
123 return ops->pending(dev, true);
128 void serial_putc(char ch)
130 _serial_putc(cur_dev, ch);
133 void serial_puts(const char *str)
135 _serial_puts(cur_dev, str);
138 int serial_getc(void)
140 return _serial_getc(cur_dev);
143 int serial_tstc(void)
145 return _serial_tstc(cur_dev);
148 void serial_setbrg(void)
150 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
153 ops->setbrg(cur_dev, gd->baudrate);
156 void serial_stdio_init(void)
160 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
162 _serial_putc(sdev->priv, ch);
165 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
167 _serial_puts(sdev->priv, str);
170 int serial_stub_getc(struct stdio_dev *sdev)
172 return _serial_getc(sdev->priv);
175 int serial_stub_tstc(struct stdio_dev *sdev)
177 return _serial_tstc(sdev->priv);
180 static int serial_post_probe(struct udevice *dev)
182 struct stdio_dev sdev;
183 struct dm_serial_ops *ops = serial_get_ops(dev);
184 struct serial_dev_priv *upriv = dev->uclass_priv;
187 /* Set the baud rate */
189 ret = ops->setbrg(dev, gd->baudrate);
194 if (!(gd->flags & GD_FLG_RELOC))
197 memset(&sdev, '\0', sizeof(sdev));
199 strncpy(sdev.name, dev->name, sizeof(sdev.name));
200 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
202 sdev.putc = serial_stub_putc;
203 sdev.puts = serial_stub_puts;
204 sdev.getc = serial_stub_getc;
205 sdev.tstc = serial_stub_tstc;
206 stdio_register_dev(&sdev, &upriv->sdev);
211 static int serial_pre_remove(struct udevice *dev)
213 #ifdef CONFIG_SYS_STDIO_DEREGISTER
214 struct serial_dev_priv *upriv = dev->uclass_priv;
216 if (stdio_deregister_dev(upriv->sdev, 0))
223 UCLASS_DRIVER(serial) = {
226 .post_probe = serial_post_probe,
227 .pre_remove = serial_pre_remove,
228 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),