2 * Copyright (c) 2014 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <stdio_dev.h>
15 #include <dm/device-internal.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* The currently-selected console serial device */
20 struct udevice *cur_dev __attribute__ ((section(".data")));
22 #ifndef CONFIG_SYS_MALLOC_F_LEN
23 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
26 static void serial_find_console_or_panic(void)
28 #ifdef CONFIG_OF_CONTROL
31 /* Check for a chosen console */
32 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
34 node = fdtdec_get_alias_node(gd->fdt_blob, "console");
35 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
39 * If the console is not marked to be bound before relocation, bind
43 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
44 if (!device_probe(cur_dev))
50 * Failing that, get the device with sequence number 0, or in extremis
51 * just the first serial device we can find. But we insist on having
52 * a console (even if it is silent).
54 if (uclass_get_device_by_seq(UCLASS_SERIAL, 0, &cur_dev) &&
55 (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
56 panic("No serial driver found");
59 /* Called prior to relocation */
62 serial_find_console_or_panic();
63 gd->flags |= GD_FLG_SERIAL_READY;
68 /* Called after relocation */
69 void serial_initialize(void)
71 serial_find_console_or_panic();
74 static void serial_putc_dev(struct udevice *dev, char ch)
76 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
80 err = ops->putc(cur_dev, ch);
81 } while (err == -EAGAIN);
86 void serial_putc(char ch)
88 serial_putc_dev(cur_dev, ch);
91 void serial_setbrg(void)
93 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
96 ops->setbrg(cur_dev, gd->baudrate);
99 void serial_puts(const char *str)
105 int serial_tstc(void)
107 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
110 return ops->pending(cur_dev, true);
115 static int serial_getc_dev(struct udevice *dev)
117 struct dm_serial_ops *ops = serial_get_ops(dev);
121 err = ops->getc(dev);
122 } while (err == -EAGAIN);
124 return err >= 0 ? err : 0;
127 int serial_getc(void)
129 return serial_getc_dev(cur_dev);
132 void serial_stdio_init(void)
136 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
138 struct udevice *dev = sdev->priv;
140 serial_putc_dev(dev, ch);
143 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
146 serial_stub_putc(sdev, *str++);
149 int serial_stub_getc(struct stdio_dev *sdev)
151 struct udevice *dev = sdev->priv;
153 return serial_getc_dev(dev);
156 int serial_stub_tstc(struct stdio_dev *sdev)
158 struct udevice *dev = sdev->priv;
159 struct dm_serial_ops *ops = serial_get_ops(dev);
162 return ops->pending(dev, true);
167 static int serial_post_probe(struct udevice *dev)
169 struct stdio_dev sdev;
170 struct dm_serial_ops *ops = serial_get_ops(dev);
171 struct serial_dev_priv *upriv = dev->uclass_priv;
174 /* Set the baud rate */
176 ret = ops->setbrg(dev, gd->baudrate);
181 if (!(gd->flags & GD_FLG_RELOC))
184 memset(&sdev, '\0', sizeof(sdev));
186 strncpy(sdev.name, dev->name, sizeof(sdev.name));
187 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
189 sdev.putc = serial_stub_putc;
190 sdev.puts = serial_stub_puts;
191 sdev.getc = serial_stub_getc;
192 sdev.tstc = serial_stub_tstc;
193 stdio_register_dev(&sdev, &upriv->sdev);
198 static int serial_pre_remove(struct udevice *dev)
200 #ifdef CONFIG_SYS_STDIO_DEREGISTER
201 struct serial_dev_priv *upriv = dev->uclass_priv;
203 if (stdio_deregister_dev(upriv->sdev, 0))
210 UCLASS_DRIVER(serial) = {
213 .post_probe = serial_post_probe,
214 .pre_remove = serial_pre_remove,
215 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),