blob: 842f78bff3c3863c20834cda5c49f08cef4a2c25 [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>
Simon Glass593f50b2014-10-29 13:09:03 -06009#include <environment.h>
Simon Glass247f5962014-09-04 16:27:26 -060010#include <errno.h>
11#include <fdtdec.h>
12#include <os.h>
13#include <serial.h>
14#include <stdio_dev.h>
Simon Glass5c60b2a2014-10-22 21:37:02 -060015#include <watchdog.h>
Simon Glass247f5962014-09-04 16:27:26 -060016#include <dm/lists.h>
17#include <dm/device-internal.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass593f50b2014-10-29 13:09:03 -060021/*
22 * Table with supported baudrates (defined in config_xyz.h)
23 */
24static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
25
Simon Glass247f5962014-09-04 16:27:26 -060026#ifndef CONFIG_SYS_MALLOC_F_LEN
27#error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
28#endif
29
30static void serial_find_console_or_panic(void)
31{
Simon Glassc860e3e2015-10-17 19:41:17 -060032 const void *blob = gd->fdt_blob;
Simon Glassa4c2e132014-11-10 18:00:20 -070033 struct udevice *dev;
Simon Glass247f5962014-09-04 16:27:26 -060034 int node;
35
Simon Glassc860e3e2015-10-17 19:41:17 -060036 if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
Simon Glassc62fb992015-05-12 14:55:05 -060037 /* Check for a chosen console */
Simon Glassc860e3e2015-10-17 19:41:17 -060038 node = fdtdec_get_chosen_node(blob, "stdout-path");
39 if (node < 0) {
40 const char *str, *p, *name;
41
42 /*
43 * Deal with things like
44 * stdout-path = "serial0:115200n8";
45 *
46 * We need to look up the alias and then follow it to
47 * the correct node.
48 */
49 str = fdtdec_get_chosen_prop(blob, "stdout-path");
50 if (str) {
51 p = strchr(str, ':');
52 name = fdt_get_alias_namelen(blob, str,
53 p ? p - str : strlen(str));
54 if (name)
55 node = fdt_path_offset(blob, name);
56 }
57 }
Simon Glassc62fb992015-05-12 14:55:05 -060058 if (node < 0)
Simon Glassc860e3e2015-10-17 19:41:17 -060059 node = fdt_path_offset(blob, "console");
Simon Glassc62fb992015-05-12 14:55:05 -060060 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
61 &dev)) {
Simon Glassa4c2e132014-11-10 18:00:20 -070062 gd->cur_serial_dev = dev;
Simon Glass247f5962014-09-04 16:27:26 -060063 return;
Simon Glassa4c2e132014-11-10 18:00:20 -070064 }
Simon Glassc62fb992015-05-12 14:55:05 -060065
66 /*
67 * If the console is not marked to be bound before relocation,
68 * bind it anyway.
69 */
70 if (node > 0 &&
Simon Glassc860e3e2015-10-17 19:41:17 -060071 !lists_bind_fdt(gd->dm_root, blob, node, &dev)) {
Simon Glassc62fb992015-05-12 14:55:05 -060072 if (!device_probe(dev)) {
73 gd->cur_serial_dev = dev;
74 return;
75 }
76 }
Simon Glass8579a512015-03-06 13:19:03 -070077 }
Simon Glassc860e3e2015-10-17 19:41:17 -060078 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
Simon Glassc62fb992015-05-12 14:55:05 -060079 /*
80 * Try to use CONFIG_CONS_INDEX if available (it is numbered
81 * from 1!).
82 *
83 * Failing that, get the device with sequence number 0, or in
84 * extremis just the first serial device we can find. But we
85 * insist on having a console (even if it is silent).
86 */
Simon Glassbc5aa992014-10-22 21:37:06 -060087#ifdef CONFIG_CONS_INDEX
88#define INDEX (CONFIG_CONS_INDEX - 1)
89#else
90#define INDEX 0
91#endif
Simon Glassc62fb992015-05-12 14:55:05 -060092 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
93 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
Simon Glass66640772015-07-31 09:31:20 -060094 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
Simon Glassc62fb992015-05-12 14:55:05 -060095 gd->cur_serial_dev = dev;
96 return;
97 }
Simon Glassbc5aa992014-10-22 21:37:06 -060098#undef INDEX
Simon Glassc62fb992015-05-12 14:55:05 -060099 }
100
Hans de Goede04f80942015-08-08 17:45:18 +0200101#ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
Simon Glassc62fb992015-05-12 14:55:05 -0600102 panic_str("No serial driver found");
Hans de Goede04f80942015-08-08 17:45:18 +0200103#endif
Simon Glass247f5962014-09-04 16:27:26 -0600104}
105
106/* Called prior to relocation */
107int serial_init(void)
108{
109 serial_find_console_or_panic();
110 gd->flags |= GD_FLG_SERIAL_READY;
111
112 return 0;
113}
114
115/* Called after relocation */
116void serial_initialize(void)
117{
118 serial_find_console_or_panic();
119}
120
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900121static void _serial_putc(struct udevice *dev, char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600122{
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900123 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600124 int err;
125
126 do {
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900127 err = ops->putc(dev, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600128 } while (err == -EAGAIN);
129 if (ch == '\n')
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900130 _serial_putc(dev, '\r');
Simon Glass247f5962014-09-04 16:27:26 -0600131}
132
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900133static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass73285a22014-10-01 19:57:23 -0600134{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900135 while (*str)
136 _serial_putc(dev, *str++);
Simon Glass73285a22014-10-01 19:57:23 -0600137}
138
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900139static int _serial_getc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600140{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900141 struct dm_serial_ops *ops = serial_get_ops(dev);
142 int err;
Simon Glass247f5962014-09-04 16:27:26 -0600143
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900144 do {
145 err = ops->getc(dev);
146 if (err == -EAGAIN)
147 WATCHDOG_RESET();
148 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600149
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900150 return err >= 0 ? err : 0;
Simon Glass247f5962014-09-04 16:27:26 -0600151}
152
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900153static int _serial_tstc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600154{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900155 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600156
157 if (ops->pending)
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900158 return ops->pending(dev, true);
Simon Glass247f5962014-09-04 16:27:26 -0600159
160 return 1;
161}
162
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900163void serial_putc(char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600164{
Hans de Goede04f80942015-08-08 17:45:18 +0200165 if (gd->cur_serial_dev)
166 _serial_putc(gd->cur_serial_dev, ch);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900167}
Simon Glass247f5962014-09-04 16:27:26 -0600168
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900169void serial_puts(const char *str)
170{
Hans de Goede04f80942015-08-08 17:45:18 +0200171 if (gd->cur_serial_dev)
172 _serial_puts(gd->cur_serial_dev, str);
Simon Glass247f5962014-09-04 16:27:26 -0600173}
174
Simon Glass73285a22014-10-01 19:57:23 -0600175int serial_getc(void)
176{
Hans de Goede04f80942015-08-08 17:45:18 +0200177 if (!gd->cur_serial_dev)
178 return 0;
179
Simon Glassa4c2e132014-11-10 18:00:20 -0700180 return _serial_getc(gd->cur_serial_dev);
Simon Glass73285a22014-10-01 19:57:23 -0600181}
182
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900183int serial_tstc(void)
184{
Hans de Goede04f80942015-08-08 17:45:18 +0200185 if (!gd->cur_serial_dev)
186 return 0;
187
Simon Glassa4c2e132014-11-10 18:00:20 -0700188 return _serial_tstc(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900189}
190
191void serial_setbrg(void)
192{
Hans de Goede04f80942015-08-08 17:45:18 +0200193 struct dm_serial_ops *ops;
194
195 if (!gd->cur_serial_dev)
196 return;
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900197
Hans de Goede04f80942015-08-08 17:45:18 +0200198 ops = serial_get_ops(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900199 if (ops->setbrg)
Simon Glassa4c2e132014-11-10 18:00:20 -0700200 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900201}
202
Simon Glass247f5962014-09-04 16:27:26 -0600203void serial_stdio_init(void)
204{
205}
206
Simon Glassc058d8b2014-11-10 17:16:48 -0700207#ifdef CONFIG_DM_STDIO
Simon Glass73285a22014-10-01 19:57:23 -0600208static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600209{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900210 _serial_putc(sdev->priv, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600211}
Simon Glassc058d8b2014-11-10 17:16:48 -0700212#endif
Simon Glass247f5962014-09-04 16:27:26 -0600213
214void serial_stub_puts(struct stdio_dev *sdev, const char *str)
215{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900216 _serial_puts(sdev->priv, str);
Simon Glass247f5962014-09-04 16:27:26 -0600217}
218
219int serial_stub_getc(struct stdio_dev *sdev)
220{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900221 return _serial_getc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600222}
223
224int serial_stub_tstc(struct stdio_dev *sdev)
225{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900226 return _serial_tstc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600227}
228
Simon Glass593f50b2014-10-29 13:09:03 -0600229/**
230 * on_baudrate() - Update the actual baudrate when the env var changes
231 *
232 * This will check for a valid baudrate and only apply it if valid.
233 */
234static int on_baudrate(const char *name, const char *value, enum env_op op,
235 int flags)
236{
237 int i;
238 int baudrate;
239
240 switch (op) {
241 case env_op_create:
242 case env_op_overwrite:
243 /*
244 * Switch to new baudrate if new baudrate is supported
245 */
246 baudrate = simple_strtoul(value, NULL, 10);
247
248 /* Not actually changing */
249 if (gd->baudrate == baudrate)
250 return 0;
251
252 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
253 if (baudrate == baudrate_table[i])
254 break;
255 }
256 if (i == ARRAY_SIZE(baudrate_table)) {
257 if ((flags & H_FORCE) == 0)
258 printf("## Baudrate %d bps not supported\n",
259 baudrate);
260 return 1;
261 }
262 if ((flags & H_INTERACTIVE) != 0) {
263 printf("## Switch baudrate to %d bps and press ENTER ...\n",
264 baudrate);
265 udelay(50000);
266 }
267
268 gd->baudrate = baudrate;
269
270 serial_setbrg();
271
272 udelay(50000);
273
274 if ((flags & H_INTERACTIVE) != 0)
275 while (1) {
276 if (getc() == '\r')
277 break;
278 }
279
280 return 0;
281 case env_op_delete:
282 printf("## Baudrate may not be deleted\n");
283 return 1;
284 default:
285 return 0;
286 }
287}
288U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Simon Glass247f5962014-09-04 16:27:26 -0600289
290static int serial_post_probe(struct udevice *dev)
291{
Simon Glass247f5962014-09-04 16:27:26 -0600292 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700293#ifdef CONFIG_DM_STDIO
Simon Glassde0977b2015-03-05 12:25:20 -0700294 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700295 struct stdio_dev sdev;
296#endif
Simon Glass247f5962014-09-04 16:27:26 -0600297 int ret;
298
Michal Simek0ec473b2015-02-02 16:31:59 +0100299#if defined(CONFIG_NEEDS_MANUAL_RELOC)
300 if (ops->setbrg)
301 ops->setbrg += gd->reloc_off;
302 if (ops->getc)
303 ops->getc += gd->reloc_off;
304 if (ops->putc)
305 ops->putc += gd->reloc_off;
306 if (ops->pending)
307 ops->pending += gd->reloc_off;
308 if (ops->clear)
309 ops->clear += gd->reloc_off;
310#if CONFIG_POST & CONFIG_SYS_POST_UART
311 if (ops->loop)
312 ops->loop += gd->reloc_off
313#endif
314#endif
Simon Glass247f5962014-09-04 16:27:26 -0600315 /* Set the baud rate */
316 if (ops->setbrg) {
317 ret = ops->setbrg(dev, gd->baudrate);
318 if (ret)
319 return ret;
320 }
321
Simon Glassc058d8b2014-11-10 17:16:48 -0700322#ifdef CONFIG_DM_STDIO
Simon Glass247f5962014-09-04 16:27:26 -0600323 if (!(gd->flags & GD_FLG_RELOC))
324 return 0;
Simon Glass247f5962014-09-04 16:27:26 -0600325 memset(&sdev, '\0', sizeof(sdev));
326
327 strncpy(sdev.name, dev->name, sizeof(sdev.name));
328 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
329 sdev.priv = dev;
330 sdev.putc = serial_stub_putc;
331 sdev.puts = serial_stub_puts;
332 sdev.getc = serial_stub_getc;
333 sdev.tstc = serial_stub_tstc;
334 stdio_register_dev(&sdev, &upriv->sdev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700335#endif
Simon Glass247f5962014-09-04 16:27:26 -0600336 return 0;
337}
338
339static int serial_pre_remove(struct udevice *dev)
340{
341#ifdef CONFIG_SYS_STDIO_DEREGISTER
Simon Glassde0977b2015-03-05 12:25:20 -0700342 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600343
Hans de Goede4c4150c2014-10-10 18:30:17 +0200344 if (stdio_deregister_dev(upriv->sdev, 0))
Simon Glass247f5962014-09-04 16:27:26 -0600345 return -EPERM;
346#endif
347
348 return 0;
349}
350
351UCLASS_DRIVER(serial) = {
352 .id = UCLASS_SERIAL,
353 .name = "serial",
Simon Glass0ccb0972015-01-25 08:27:05 -0700354 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass247f5962014-09-04 16:27:26 -0600355 .post_probe = serial_post_probe,
356 .pre_remove = serial_pre_remove,
357 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
358};