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)
30 /* Check for a chosen console */
31 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
33 node = fdtdec_get_alias_node(gd->fdt_blob, "console");
34 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
38 * If the console is not marked to be bound before relocation, bind
42 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
43 if (!device_probe(cur_dev))
49 * Failing that, get the device with sequence number 0, or in extremis
50 * just the first serial device we can find. But we insist on having
51 * a console (even if it is silent).
53 if (uclass_get_device_by_seq(UCLASS_SERIAL, 0, &cur_dev) &&
54 (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
55 panic("No serial driver found");
58 /* Called prior to relocation */
61 serial_find_console_or_panic();
62 gd->flags |= GD_FLG_SERIAL_READY;
67 /* Called after relocation */
68 void serial_initialize(void)
70 serial_find_console_or_panic();
73 void serial_putc(char ch)
75 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
79 err = ops->putc(cur_dev, ch);
80 } while (err == -EAGAIN);
85 void serial_setbrg(void)
87 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
90 ops->setbrg(cur_dev, gd->baudrate);
93 void serial_puts(const char *str)
101 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
104 return ops->pending(cur_dev, true);
109 int serial_getc(void)
111 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
115 err = ops->getc(cur_dev);
116 } while (err == -EAGAIN);
118 return err >= 0 ? err : 0;
121 void serial_stdio_init(void)
125 void serial_stub_putc(struct stdio_dev *sdev, const char ch)
127 struct udevice *dev = sdev->priv;
128 struct dm_serial_ops *ops = serial_get_ops(dev);
133 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
136 serial_stub_putc(sdev, *str++);
139 int serial_stub_getc(struct stdio_dev *sdev)
141 struct udevice *dev = sdev->priv;
142 struct dm_serial_ops *ops = serial_get_ops(dev);
147 err = ops->getc(dev);
148 } while (err == -EAGAIN);
150 return err >= 0 ? err : 0;
153 int serial_stub_tstc(struct stdio_dev *sdev)
155 struct udevice *dev = sdev->priv;
156 struct dm_serial_ops *ops = serial_get_ops(dev);
159 return ops->pending(dev, true);
164 static int serial_post_probe(struct udevice *dev)
166 struct stdio_dev sdev;
167 struct dm_serial_ops *ops = serial_get_ops(dev);
168 struct serial_dev_priv *upriv = dev->uclass_priv;
171 /* Set the baud rate */
173 ret = ops->setbrg(dev, gd->baudrate);
178 if (!(gd->flags & GD_FLG_RELOC))
181 memset(&sdev, '\0', sizeof(sdev));
183 strncpy(sdev.name, dev->name, sizeof(sdev.name));
184 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
186 sdev.putc = serial_stub_putc;
187 sdev.puts = serial_stub_puts;
188 sdev.getc = serial_stub_getc;
189 sdev.tstc = serial_stub_tstc;
190 stdio_register_dev(&sdev, &upriv->sdev);
195 static int serial_pre_remove(struct udevice *dev)
197 #ifdef CONFIG_SYS_STDIO_DEREGISTER
198 struct serial_dev_priv *upriv = dev->uclass_priv;
200 if (stdio_deregister_dev(upriv->sdev))
207 UCLASS_DRIVER(serial) = {
210 .post_probe = serial_post_probe,
211 .pre_remove = serial_pre_remove,
212 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),