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 * Failing that, get the device with sequence number 0, or in extremis
54 * just the first serial device we can find. But we insist on having
55 * a console (even if it is silent).
57 if (uclass_get_device_by_seq(UCLASS_SERIAL, 0, &cur_dev) &&
58 (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
59 panic("No serial driver found");
62 /* Called prior to relocation */
65 serial_find_console_or_panic();
66 gd->flags |= GD_FLG_SERIAL_READY;
71 /* Called after relocation */
72 void serial_initialize(void)
74 serial_find_console_or_panic();
77 static void serial_putc_dev(struct udevice *dev, char ch)
79 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
83 err = ops->putc(cur_dev, ch);
84 } while (err == -EAGAIN);
89 void serial_putc(char ch)
91 serial_putc_dev(cur_dev, ch);
94 void serial_setbrg(void)
96 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
99 ops->setbrg(cur_dev, gd->baudrate);
102 void serial_puts(const char *str)
108 int serial_tstc(void)
110 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
113 return ops->pending(cur_dev, true);
118 static int serial_getc_dev(struct udevice *dev)
120 struct dm_serial_ops *ops = serial_get_ops(dev);
124 err = ops->getc(dev);
127 } while (err == -EAGAIN);
129 return err >= 0 ? err : 0;
132 int serial_getc(void)
134 return serial_getc_dev(cur_dev);
137 void serial_stdio_init(void)
141 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
143 struct udevice *dev = sdev->priv;
145 serial_putc_dev(dev, ch);
148 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
151 serial_stub_putc(sdev, *str++);
154 int serial_stub_getc(struct stdio_dev *sdev)
156 struct udevice *dev = sdev->priv;
158 return serial_getc_dev(dev);
161 int serial_stub_tstc(struct stdio_dev *sdev)
163 struct udevice *dev = sdev->priv;
164 struct dm_serial_ops *ops = serial_get_ops(dev);
167 return ops->pending(dev, true);
172 static int serial_post_probe(struct udevice *dev)
174 struct stdio_dev sdev;
175 struct dm_serial_ops *ops = serial_get_ops(dev);
176 struct serial_dev_priv *upriv = dev->uclass_priv;
179 /* Set the baud rate */
181 ret = ops->setbrg(dev, gd->baudrate);
186 if (!(gd->flags & GD_FLG_RELOC))
189 memset(&sdev, '\0', sizeof(sdev));
191 strncpy(sdev.name, dev->name, sizeof(sdev.name));
192 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
194 sdev.putc = serial_stub_putc;
195 sdev.puts = serial_stub_puts;
196 sdev.getc = serial_stub_getc;
197 sdev.tstc = serial_stub_tstc;
198 stdio_register_dev(&sdev, &upriv->sdev);
203 static int serial_pre_remove(struct udevice *dev)
205 #ifdef CONFIG_SYS_STDIO_DEREGISTER
206 struct serial_dev_priv *upriv = dev->uclass_priv;
208 if (stdio_deregister_dev(upriv->sdev, 0))
215 UCLASS_DRIVER(serial) = {
218 .post_probe = serial_post_probe,
219 .pre_remove = serial_pre_remove,
220 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),