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 void serial_putc(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_setbrg(void)
88 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
91 ops->setbrg(cur_dev, gd->baudrate);
94 void serial_puts(const char *str)
100 int serial_tstc(void)
102 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
105 return ops->pending(cur_dev, true);
110 int serial_getc(void)
112 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
116 err = ops->getc(cur_dev);
117 } while (err == -EAGAIN);
119 return err >= 0 ? err : 0;
122 void serial_stdio_init(void)
126 void serial_stub_putc(struct stdio_dev *sdev, const char ch)
128 struct udevice *dev = sdev->priv;
129 struct dm_serial_ops *ops = serial_get_ops(dev);
134 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
137 serial_stub_putc(sdev, *str++);
140 int serial_stub_getc(struct stdio_dev *sdev)
142 struct udevice *dev = sdev->priv;
143 struct dm_serial_ops *ops = serial_get_ops(dev);
148 err = ops->getc(dev);
149 } while (err == -EAGAIN);
151 return err >= 0 ? err : 0;
154 int serial_stub_tstc(struct stdio_dev *sdev)
156 struct udevice *dev = sdev->priv;
157 struct dm_serial_ops *ops = serial_get_ops(dev);
160 return ops->pending(dev, true);
165 static int serial_post_probe(struct udevice *dev)
167 struct stdio_dev sdev;
168 struct dm_serial_ops *ops = serial_get_ops(dev);
169 struct serial_dev_priv *upriv = dev->uclass_priv;
172 /* Set the baud rate */
174 ret = ops->setbrg(dev, gd->baudrate);
179 if (!(gd->flags & GD_FLG_RELOC))
182 memset(&sdev, '\0', sizeof(sdev));
184 strncpy(sdev.name, dev->name, sizeof(sdev.name));
185 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
187 sdev.putc = serial_stub_putc;
188 sdev.puts = serial_stub_puts;
189 sdev.getc = serial_stub_getc;
190 sdev.tstc = serial_stub_tstc;
191 stdio_register_dev(&sdev, &upriv->sdev);
196 static int serial_pre_remove(struct udevice *dev)
198 #ifdef CONFIG_SYS_STDIO_DEREGISTER
199 struct serial_dev_priv *upriv = dev->uclass_priv;
201 if (stdio_deregister_dev(upriv->sdev))
208 UCLASS_DRIVER(serial) = {
211 .post_probe = serial_post_probe,
212 .pre_remove = serial_pre_remove,
213 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),