blob: 1a75950d19fa4c738f7f5c881da08561d416faf2 [file] [log] [blame]
Simon Glass57d92752014-09-04 16:27:26 -06001/*
2 * Copyright (c) 2014 The Chromium OS Authors.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <fdtdec.h>
11#include <os.h>
12#include <serial.h>
13#include <stdio_dev.h>
14#include <dm/lists.h>
15#include <dm/device-internal.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19/* The currently-selected console serial device */
20struct udevice *cur_dev __attribute__ ((section(".data")));
21
22#ifndef CONFIG_SYS_MALLOC_F_LEN
23#error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
24#endif
25
26static void serial_find_console_or_panic(void)
27{
Simon Glass59990bf2014-09-17 09:02:40 -060028#ifdef CONFIG_OF_CONTROL
Simon Glass57d92752014-09-04 16:27:26 -060029 int node;
30
31 /* Check for a chosen console */
32 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
33 if (node < 0)
34 node = fdtdec_get_alias_node(gd->fdt_blob, "console");
35 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
36 return;
37
38 /*
39 * If the console is not marked to be bound before relocation, bind
40 * it anyway.
41 */
42 if (node > 0 &&
43 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
44 if (!device_probe(cur_dev))
45 return;
46 cur_dev = NULL;
47 }
Simon Glass59990bf2014-09-17 09:02:40 -060048#endif
Simon Glass57d92752014-09-04 16:27:26 -060049 /*
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).
53 */
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");
57}
58
59/* Called prior to relocation */
60int serial_init(void)
61{
62 serial_find_console_or_panic();
63 gd->flags |= GD_FLG_SERIAL_READY;
64
65 return 0;
66}
67
68/* Called after relocation */
69void serial_initialize(void)
70{
71 serial_find_console_or_panic();
72}
73
Simon Glassb8893322014-10-01 19:57:23 -060074static void serial_putc_dev(struct udevice *dev, char ch)
Simon Glass57d92752014-09-04 16:27:26 -060075{
76 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
77 int err;
78
79 do {
80 err = ops->putc(cur_dev, ch);
81 } while (err == -EAGAIN);
82 if (ch == '\n')
83 serial_putc('\r');
84}
85
Simon Glassb8893322014-10-01 19:57:23 -060086void serial_putc(char ch)
87{
88 serial_putc_dev(cur_dev, ch);
89}
90
Simon Glass57d92752014-09-04 16:27:26 -060091void serial_setbrg(void)
92{
93 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
94
95 if (ops->setbrg)
96 ops->setbrg(cur_dev, gd->baudrate);
97}
98
99void serial_puts(const char *str)
100{
101 while (*str)
102 serial_putc(*str++);
103}
104
105int serial_tstc(void)
106{
107 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
108
109 if (ops->pending)
110 return ops->pending(cur_dev, true);
111
112 return 1;
113}
114
Simon Glassb8893322014-10-01 19:57:23 -0600115static int serial_getc_dev(struct udevice *dev)
Simon Glass57d92752014-09-04 16:27:26 -0600116{
Simon Glassb8893322014-10-01 19:57:23 -0600117 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600118 int err;
119
120 do {
Simon Glassb8893322014-10-01 19:57:23 -0600121 err = ops->getc(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600122 } while (err == -EAGAIN);
123
124 return err >= 0 ? err : 0;
125}
126
Simon Glassb8893322014-10-01 19:57:23 -0600127int serial_getc(void)
128{
129 return serial_getc_dev(cur_dev);
130}
131
Simon Glass57d92752014-09-04 16:27:26 -0600132void serial_stdio_init(void)
133{
134}
135
Simon Glassb8893322014-10-01 19:57:23 -0600136static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass57d92752014-09-04 16:27:26 -0600137{
138 struct udevice *dev = sdev->priv;
Simon Glass57d92752014-09-04 16:27:26 -0600139
Simon Glassb8893322014-10-01 19:57:23 -0600140 serial_putc_dev(dev, ch);
Simon Glass57d92752014-09-04 16:27:26 -0600141}
142
143void serial_stub_puts(struct stdio_dev *sdev, const char *str)
144{
145 while (*str)
146 serial_stub_putc(sdev, *str++);
147}
148
149int serial_stub_getc(struct stdio_dev *sdev)
150{
151 struct udevice *dev = sdev->priv;
Simon Glass57d92752014-09-04 16:27:26 -0600152
Simon Glassb8893322014-10-01 19:57:23 -0600153 return serial_getc_dev(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600154}
155
156int serial_stub_tstc(struct stdio_dev *sdev)
157{
158 struct udevice *dev = sdev->priv;
159 struct dm_serial_ops *ops = serial_get_ops(dev);
160
161 if (ops->pending)
162 return ops->pending(dev, true);
163
164 return 1;
165}
166
167static int serial_post_probe(struct udevice *dev)
168{
169 struct stdio_dev sdev;
170 struct dm_serial_ops *ops = serial_get_ops(dev);
171 struct serial_dev_priv *upriv = dev->uclass_priv;
172 int ret;
173
174 /* Set the baud rate */
175 if (ops->setbrg) {
176 ret = ops->setbrg(dev, gd->baudrate);
177 if (ret)
178 return ret;
179 }
180
181 if (!(gd->flags & GD_FLG_RELOC))
182 return 0;
183
184 memset(&sdev, '\0', sizeof(sdev));
185
186 strncpy(sdev.name, dev->name, sizeof(sdev.name));
187 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
188 sdev.priv = dev;
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);
194
195 return 0;
196}
197
198static int serial_pre_remove(struct udevice *dev)
199{
200#ifdef CONFIG_SYS_STDIO_DEREGISTER
201 struct serial_dev_priv *upriv = dev->uclass_priv;
202
Hans de Goede4a742982014-10-10 18:30:17 +0200203 if (stdio_deregister_dev(upriv->sdev, 0))
Simon Glass57d92752014-09-04 16:27:26 -0600204 return -EPERM;
205#endif
206
207 return 0;
208}
209
210UCLASS_DRIVER(serial) = {
211 .id = UCLASS_SERIAL,
212 .name = "serial",
213 .post_probe = serial_post_probe,
214 .pre_remove = serial_pre_remove,
215 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
216};