blob: 1983a3d55bde3850bcd7369e8a5a807260fcf4f5 [file] [log] [blame]
Simon Glass247f5962014-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>
Simon Glass5c60b2a2014-10-22 21:37:02 -060014#include <watchdog.h>
Simon Glass247f5962014-09-04 16:27:26 -060015#include <dm/lists.h>
16#include <dm/device-internal.h>
17
Simon Glass5c60b2a2014-10-22 21:37:02 -060018#include <ns16550.h>
19
Simon Glass247f5962014-09-04 16:27:26 -060020DECLARE_GLOBAL_DATA_PTR;
21
22/* The currently-selected console serial device */
23struct udevice *cur_dev __attribute__ ((section(".data")));
24
25#ifndef CONFIG_SYS_MALLOC_F_LEN
26#error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
27#endif
28
29static void serial_find_console_or_panic(void)
30{
Simon Glass28141d52014-09-17 09:02:40 -060031#ifdef CONFIG_OF_CONTROL
Simon Glass247f5962014-09-04 16:27:26 -060032 int node;
33
34 /* Check for a chosen console */
35 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
36 if (node < 0)
37 node = fdtdec_get_alias_node(gd->fdt_blob, "console");
38 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
39 return;
40
41 /*
42 * If the console is not marked to be bound before relocation, bind
43 * it anyway.
44 */
45 if (node > 0 &&
46 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
47 if (!device_probe(cur_dev))
48 return;
49 cur_dev = NULL;
50 }
Simon Glass28141d52014-09-17 09:02:40 -060051#endif
Simon Glass247f5962014-09-04 16:27:26 -060052 /*
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).
56 */
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");
60}
61
62/* Called prior to relocation */
63int serial_init(void)
64{
65 serial_find_console_or_panic();
66 gd->flags |= GD_FLG_SERIAL_READY;
67
68 return 0;
69}
70
71/* Called after relocation */
72void serial_initialize(void)
73{
74 serial_find_console_or_panic();
75}
76
Simon Glass73285a22014-10-01 19:57:23 -060077static void serial_putc_dev(struct udevice *dev, char ch)
Simon Glass247f5962014-09-04 16:27:26 -060078{
79 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
80 int err;
81
82 do {
83 err = ops->putc(cur_dev, ch);
84 } while (err == -EAGAIN);
85 if (ch == '\n')
86 serial_putc('\r');
87}
88
Simon Glass73285a22014-10-01 19:57:23 -060089void serial_putc(char ch)
90{
91 serial_putc_dev(cur_dev, ch);
92}
93
Simon Glass247f5962014-09-04 16:27:26 -060094void serial_setbrg(void)
95{
96 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
97
98 if (ops->setbrg)
99 ops->setbrg(cur_dev, gd->baudrate);
100}
101
102void serial_puts(const char *str)
103{
104 while (*str)
105 serial_putc(*str++);
106}
107
108int serial_tstc(void)
109{
110 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
111
112 if (ops->pending)
113 return ops->pending(cur_dev, true);
114
115 return 1;
116}
117
Simon Glass73285a22014-10-01 19:57:23 -0600118static int serial_getc_dev(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600119{
Simon Glass73285a22014-10-01 19:57:23 -0600120 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600121 int err;
122
123 do {
Simon Glass73285a22014-10-01 19:57:23 -0600124 err = ops->getc(dev);
Simon Glass5c60b2a2014-10-22 21:37:02 -0600125 if (err == -EAGAIN)
126 WATCHDOG_RESET();
Simon Glass247f5962014-09-04 16:27:26 -0600127 } while (err == -EAGAIN);
128
129 return err >= 0 ? err : 0;
130}
131
Simon Glass73285a22014-10-01 19:57:23 -0600132int serial_getc(void)
133{
134 return serial_getc_dev(cur_dev);
135}
136
Simon Glass247f5962014-09-04 16:27:26 -0600137void serial_stdio_init(void)
138{
139}
140
Simon Glass73285a22014-10-01 19:57:23 -0600141static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600142{
143 struct udevice *dev = sdev->priv;
Simon Glass247f5962014-09-04 16:27:26 -0600144
Simon Glass73285a22014-10-01 19:57:23 -0600145 serial_putc_dev(dev, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600146}
147
148void serial_stub_puts(struct stdio_dev *sdev, const char *str)
149{
150 while (*str)
151 serial_stub_putc(sdev, *str++);
152}
153
154int serial_stub_getc(struct stdio_dev *sdev)
155{
156 struct udevice *dev = sdev->priv;
Simon Glass247f5962014-09-04 16:27:26 -0600157
Simon Glass73285a22014-10-01 19:57:23 -0600158 return serial_getc_dev(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600159}
160
161int serial_stub_tstc(struct stdio_dev *sdev)
162{
163 struct udevice *dev = sdev->priv;
164 struct dm_serial_ops *ops = serial_get_ops(dev);
165
166 if (ops->pending)
167 return ops->pending(dev, true);
168
169 return 1;
170}
171
172static int serial_post_probe(struct udevice *dev)
173{
174 struct stdio_dev sdev;
175 struct dm_serial_ops *ops = serial_get_ops(dev);
176 struct serial_dev_priv *upriv = dev->uclass_priv;
177 int ret;
178
179 /* Set the baud rate */
180 if (ops->setbrg) {
181 ret = ops->setbrg(dev, gd->baudrate);
182 if (ret)
183 return ret;
184 }
185
186 if (!(gd->flags & GD_FLG_RELOC))
187 return 0;
188
189 memset(&sdev, '\0', sizeof(sdev));
190
191 strncpy(sdev.name, dev->name, sizeof(sdev.name));
192 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
193 sdev.priv = dev;
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);
199
200 return 0;
201}
202
203static int serial_pre_remove(struct udevice *dev)
204{
205#ifdef CONFIG_SYS_STDIO_DEREGISTER
206 struct serial_dev_priv *upriv = dev->uclass_priv;
207
Hans de Goede4c4150c2014-10-10 18:30:17 +0200208 if (stdio_deregister_dev(upriv->sdev, 0))
Simon Glass247f5962014-09-04 16:27:26 -0600209 return -EPERM;
210#endif
211
212 return 0;
213}
214
215UCLASS_DRIVER(serial) = {
216 .id = UCLASS_SERIAL,
217 .name = "serial",
218 .post_probe = serial_post_probe,
219 .pre_remove = serial_pre_remove,
220 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
221};