blob: 71f1a5cb91024e9fc473e2a5e61f0deb82efd3c1 [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>
Simon Glassc487fd42014-10-22 21:37:02 -060014#include <watchdog.h>
Simon Glass57d92752014-09-04 16:27:26 -060015#include <dm/lists.h>
16#include <dm/device-internal.h>
17
Simon Glassc487fd42014-10-22 21:37:02 -060018#include <ns16550.h>
19
Simon Glass57d92752014-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 Glass59990bf2014-09-17 09:02:40 -060031#ifdef CONFIG_OF_CONTROL
Simon Glass57d92752014-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 Glass59990bf2014-09-17 09:02:40 -060051#endif
Simon Glass57d92752014-09-04 16:27:26 -060052 /*
Simon Glass91155c62014-10-22 21:37:06 -060053 * Try to use CONFIG_CONS_INDEX if available (it is numbered from 1!).
54 *
Simon Glass57d92752014-09-04 16:27:26 -060055 * Failing that, get the device with sequence number 0, or in extremis
56 * just the first serial device we can find. But we insist on having
57 * a console (even if it is silent).
58 */
Simon Glass91155c62014-10-22 21:37:06 -060059#ifdef CONFIG_CONS_INDEX
60#define INDEX (CONFIG_CONS_INDEX - 1)
61#else
62#define INDEX 0
63#endif
64 if (uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &cur_dev) &&
65 uclass_get_device(UCLASS_SERIAL, INDEX, &cur_dev) &&
Simon Glass57d92752014-09-04 16:27:26 -060066 (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
67 panic("No serial driver found");
Simon Glass91155c62014-10-22 21:37:06 -060068#undef INDEX
Simon Glass57d92752014-09-04 16:27:26 -060069}
70
71/* Called prior to relocation */
72int serial_init(void)
73{
74 serial_find_console_or_panic();
75 gd->flags |= GD_FLG_SERIAL_READY;
76
77 return 0;
78}
79
80/* Called after relocation */
81void serial_initialize(void)
82{
83 serial_find_console_or_panic();
84}
85
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +090086static void _serial_putc(struct udevice *dev, char ch)
Simon Glass57d92752014-09-04 16:27:26 -060087{
Masahiro Yamadabac64462014-10-23 22:26:06 +090088 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass57d92752014-09-04 16:27:26 -060089 int err;
90
91 do {
Masahiro Yamadabac64462014-10-23 22:26:06 +090092 err = ops->putc(dev, ch);
Simon Glass57d92752014-09-04 16:27:26 -060093 } while (err == -EAGAIN);
94 if (ch == '\n')
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +090095 _serial_putc(dev, '\r');
Simon Glass57d92752014-09-04 16:27:26 -060096}
97
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +090098static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass57d92752014-09-04 16:27:26 -060099{
100 while (*str)
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900101 _serial_putc(dev, *str++);
Simon Glass57d92752014-09-04 16:27:26 -0600102}
103
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900104static int _serial_getc(struct udevice *dev)
Simon Glass57d92752014-09-04 16:27:26 -0600105{
Simon Glassb8893322014-10-01 19:57:23 -0600106 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600107 int err;
108
109 do {
Simon Glassb8893322014-10-01 19:57:23 -0600110 err = ops->getc(dev);
Simon Glassc487fd42014-10-22 21:37:02 -0600111 if (err == -EAGAIN)
112 WATCHDOG_RESET();
Simon Glass57d92752014-09-04 16:27:26 -0600113 } while (err == -EAGAIN);
114
115 return err >= 0 ? err : 0;
116}
117
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900118static int _serial_tstc(struct udevice *dev)
119{
120 struct dm_serial_ops *ops = serial_get_ops(dev);
121
122 if (ops->pending)
123 return ops->pending(dev, true);
124
125 return 1;
126}
127
128void serial_putc(char ch)
129{
130 _serial_putc(cur_dev, ch);
131}
132
133void serial_puts(const char *str)
134{
135 _serial_puts(cur_dev, str);
136}
137
Simon Glassb8893322014-10-01 19:57:23 -0600138int serial_getc(void)
139{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900140 return _serial_getc(cur_dev);
141}
142
143int serial_tstc(void)
144{
145 return _serial_tstc(cur_dev);
146}
147
148void serial_setbrg(void)
149{
150 struct dm_serial_ops *ops = serial_get_ops(cur_dev);
151
152 if (ops->setbrg)
153 ops->setbrg(cur_dev, gd->baudrate);
Simon Glassb8893322014-10-01 19:57:23 -0600154}
155
Simon Glass57d92752014-09-04 16:27:26 -0600156void serial_stdio_init(void)
157{
158}
159
Simon Glassb8893322014-10-01 19:57:23 -0600160static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass57d92752014-09-04 16:27:26 -0600161{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900162 _serial_putc(sdev->priv, ch);
Simon Glass57d92752014-09-04 16:27:26 -0600163}
164
165void serial_stub_puts(struct stdio_dev *sdev, const char *str)
166{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900167 _serial_puts(sdev->priv, str);
Simon Glass57d92752014-09-04 16:27:26 -0600168}
169
170int serial_stub_getc(struct stdio_dev *sdev)
171{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900172 return _serial_getc(sdev->priv);
Simon Glass57d92752014-09-04 16:27:26 -0600173}
174
175int serial_stub_tstc(struct stdio_dev *sdev)
176{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900177 return _serial_tstc(sdev->priv);
Simon Glass57d92752014-09-04 16:27:26 -0600178}
179
180static int serial_post_probe(struct udevice *dev)
181{
182 struct stdio_dev sdev;
183 struct dm_serial_ops *ops = serial_get_ops(dev);
184 struct serial_dev_priv *upriv = dev->uclass_priv;
185 int ret;
186
187 /* Set the baud rate */
188 if (ops->setbrg) {
189 ret = ops->setbrg(dev, gd->baudrate);
190 if (ret)
191 return ret;
192 }
193
194 if (!(gd->flags & GD_FLG_RELOC))
195 return 0;
196
197 memset(&sdev, '\0', sizeof(sdev));
198
199 strncpy(sdev.name, dev->name, sizeof(sdev.name));
200 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
201 sdev.priv = dev;
202 sdev.putc = serial_stub_putc;
203 sdev.puts = serial_stub_puts;
204 sdev.getc = serial_stub_getc;
205 sdev.tstc = serial_stub_tstc;
206 stdio_register_dev(&sdev, &upriv->sdev);
207
208 return 0;
209}
210
211static int serial_pre_remove(struct udevice *dev)
212{
213#ifdef CONFIG_SYS_STDIO_DEREGISTER
214 struct serial_dev_priv *upriv = dev->uclass_priv;
215
Hans de Goede4a742982014-10-10 18:30:17 +0200216 if (stdio_deregister_dev(upriv->sdev, 0))
Simon Glass57d92752014-09-04 16:27:26 -0600217 return -EPERM;
218#endif
219
220 return 0;
221}
222
223UCLASS_DRIVER(serial) = {
224 .id = UCLASS_SERIAL,
225 .name = "serial",
226 .post_probe = serial_post_probe,
227 .pre_remove = serial_pre_remove,
228 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
229};