blob: 7e4290684c5e41384f35601006b9764dfe930df5 [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>
Simon Glass247f5962014-09-04 16:27:26 -060011#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
18DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass593f50b2014-10-29 13:09:03 -060020/*
21 * Table with supported baudrates (defined in config_xyz.h)
22 */
23static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
24
Simon Glass247f5962014-09-04 16:27:26 -060025#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
Simon Glass89d9cf22017-06-12 06:21:57 -060029static int serial_check_stdout(const void *blob, struct udevice **devp)
30{
31 int node;
32
33 /* Check for a chosen console */
34 node = fdtdec_get_chosen_node(blob, "stdout-path");
35 if (node < 0) {
36 const char *str, *p, *name;
37
38 /*
39 * Deal with things like
40 * stdout-path = "serial0:115200n8";
41 *
42 * We need to look up the alias and then follow it to the
43 * correct node.
44 */
45 str = fdtdec_get_chosen_prop(blob, "stdout-path");
46 if (str) {
47 p = strchr(str, ':');
48 name = fdt_get_alias_namelen(blob, str,
49 p ? p - str : strlen(str));
50 if (name)
51 node = fdt_path_offset(blob, name);
52 }
53 }
54 if (node < 0)
55 node = fdt_path_offset(blob, "console");
56 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
57 return 0;
58
59 /*
60 * If the console is not marked to be bound before relocation, bind it
61 * anyway.
62 */
63 if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
64 devp)) {
65 if (!device_probe(*devp))
66 return 0;
67 }
68
69 return -ENODEV;
70}
71
Simon Glass247f5962014-09-04 16:27:26 -060072static void serial_find_console_or_panic(void)
73{
Simon Glassc860e3e2015-10-17 19:41:17 -060074 const void *blob = gd->fdt_blob;
Simon Glassa4c2e132014-11-10 18:00:20 -070075 struct udevice *dev;
Simon Glass247f5962014-09-04 16:27:26 -060076
Simon Glassafd81a52016-07-04 11:58:04 -060077 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
78 uclass_first_device(UCLASS_SERIAL, &dev);
79 if (dev) {
80 gd->cur_serial_dev = dev;
81 return;
82 }
83 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
Simon Glass89d9cf22017-06-12 06:21:57 -060084 if (!serial_check_stdout(blob, &dev)) {
Simon Glassa4c2e132014-11-10 18:00:20 -070085 gd->cur_serial_dev = dev;
Simon Glass247f5962014-09-04 16:27:26 -060086 return;
Simon Glassa4c2e132014-11-10 18:00:20 -070087 }
Simon Glass8579a512015-03-06 13:19:03 -070088 }
Simon Glassc860e3e2015-10-17 19:41:17 -060089 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
Simon Glassc62fb992015-05-12 14:55:05 -060090 /*
Stefan Roese88c3a882015-11-26 13:38:43 +010091 * Try to use CONFIG_CONS_INDEX if available (it is numbered
92 * from 1!).
93 *
94 * Failing that, get the device with sequence number 0, or in
95 * extremis just the first serial device we can find. But we
96 * insist on having a console (even if it is silent).
97 */
Simon Glassbc5aa992014-10-22 21:37:06 -060098#ifdef CONFIG_CONS_INDEX
99#define INDEX (CONFIG_CONS_INDEX - 1)
100#else
101#define INDEX 0
102#endif
Simon Glassc62fb992015-05-12 14:55:05 -0600103 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
104 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
Simon Glass66640772015-07-31 09:31:20 -0600105 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
Simon Glassc62fb992015-05-12 14:55:05 -0600106 gd->cur_serial_dev = dev;
107 return;
108 }
Simon Glassbc5aa992014-10-22 21:37:06 -0600109#undef INDEX
Simon Glassc62fb992015-05-12 14:55:05 -0600110 }
111
Hans de Goede04f80942015-08-08 17:45:18 +0200112#ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
Simon Glassc62fb992015-05-12 14:55:05 -0600113 panic_str("No serial driver found");
Hans de Goede04f80942015-08-08 17:45:18 +0200114#endif
Simon Glass247f5962014-09-04 16:27:26 -0600115}
116
117/* Called prior to relocation */
118int serial_init(void)
119{
120 serial_find_console_or_panic();
121 gd->flags |= GD_FLG_SERIAL_READY;
122
123 return 0;
124}
125
126/* Called after relocation */
127void serial_initialize(void)
128{
Simon Glass51f73f12016-03-11 22:06:51 -0700129 serial_init();
Simon Glass247f5962014-09-04 16:27:26 -0600130}
131
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900132static void _serial_putc(struct udevice *dev, char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600133{
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900134 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600135 int err;
136
Alison Wangc0a9ce62016-03-02 11:00:36 +0800137 if (ch == '\n')
138 _serial_putc(dev, '\r');
139
Simon Glass247f5962014-09-04 16:27:26 -0600140 do {
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900141 err = ops->putc(dev, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600142 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600143}
144
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900145static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass73285a22014-10-01 19:57:23 -0600146{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900147 while (*str)
148 _serial_putc(dev, *str++);
Simon Glass73285a22014-10-01 19:57:23 -0600149}
150
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900151static int _serial_getc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600152{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900153 struct dm_serial_ops *ops = serial_get_ops(dev);
154 int err;
Simon Glass247f5962014-09-04 16:27:26 -0600155
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900156 do {
157 err = ops->getc(dev);
158 if (err == -EAGAIN)
159 WATCHDOG_RESET();
160 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600161
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900162 return err >= 0 ? err : 0;
Simon Glass247f5962014-09-04 16:27:26 -0600163}
164
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900165static int _serial_tstc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600166{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900167 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600168
169 if (ops->pending)
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900170 return ops->pending(dev, true);
Simon Glass247f5962014-09-04 16:27:26 -0600171
172 return 1;
173}
174
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900175void serial_putc(char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600176{
Hans de Goede04f80942015-08-08 17:45:18 +0200177 if (gd->cur_serial_dev)
178 _serial_putc(gd->cur_serial_dev, ch);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900179}
Simon Glass247f5962014-09-04 16:27:26 -0600180
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900181void serial_puts(const char *str)
182{
Hans de Goede04f80942015-08-08 17:45:18 +0200183 if (gd->cur_serial_dev)
184 _serial_puts(gd->cur_serial_dev, str);
Simon Glass247f5962014-09-04 16:27:26 -0600185}
186
Simon Glass73285a22014-10-01 19:57:23 -0600187int serial_getc(void)
188{
Hans de Goede04f80942015-08-08 17:45:18 +0200189 if (!gd->cur_serial_dev)
190 return 0;
191
Simon Glassa4c2e132014-11-10 18:00:20 -0700192 return _serial_getc(gd->cur_serial_dev);
Simon Glass73285a22014-10-01 19:57:23 -0600193}
194
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900195int serial_tstc(void)
196{
Hans de Goede04f80942015-08-08 17:45:18 +0200197 if (!gd->cur_serial_dev)
198 return 0;
199
Simon Glassa4c2e132014-11-10 18:00:20 -0700200 return _serial_tstc(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900201}
202
203void serial_setbrg(void)
204{
Hans de Goede04f80942015-08-08 17:45:18 +0200205 struct dm_serial_ops *ops;
206
207 if (!gd->cur_serial_dev)
208 return;
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900209
Hans de Goede04f80942015-08-08 17:45:18 +0200210 ops = serial_get_ops(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900211 if (ops->setbrg)
Simon Glassa4c2e132014-11-10 18:00:20 -0700212 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900213}
214
Simon Glass247f5962014-09-04 16:27:26 -0600215void serial_stdio_init(void)
216{
217}
218
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900219#if defined(CONFIG_DM_STDIO)
220
221#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass73285a22014-10-01 19:57:23 -0600222static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600223{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900224 _serial_putc(sdev->priv, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600225}
Simon Glassc058d8b2014-11-10 17:16:48 -0700226#endif
Simon Glass247f5962014-09-04 16:27:26 -0600227
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900228static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
Simon Glass247f5962014-09-04 16:27:26 -0600229{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900230 _serial_puts(sdev->priv, str);
Simon Glass247f5962014-09-04 16:27:26 -0600231}
232
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900233static int serial_stub_getc(struct stdio_dev *sdev)
Simon Glass247f5962014-09-04 16:27:26 -0600234{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900235 return _serial_getc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600236}
237
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900238static int serial_stub_tstc(struct stdio_dev *sdev)
Simon Glass247f5962014-09-04 16:27:26 -0600239{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900240 return _serial_tstc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600241}
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900242#endif
Simon Glass247f5962014-09-04 16:27:26 -0600243
Simon Glass593f50b2014-10-29 13:09:03 -0600244/**
245 * on_baudrate() - Update the actual baudrate when the env var changes
246 *
247 * This will check for a valid baudrate and only apply it if valid.
248 */
249static int on_baudrate(const char *name, const char *value, enum env_op op,
250 int flags)
251{
252 int i;
253 int baudrate;
254
255 switch (op) {
256 case env_op_create:
257 case env_op_overwrite:
258 /*
259 * Switch to new baudrate if new baudrate is supported
260 */
261 baudrate = simple_strtoul(value, NULL, 10);
262
263 /* Not actually changing */
264 if (gd->baudrate == baudrate)
265 return 0;
266
267 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
268 if (baudrate == baudrate_table[i])
269 break;
270 }
271 if (i == ARRAY_SIZE(baudrate_table)) {
272 if ((flags & H_FORCE) == 0)
273 printf("## Baudrate %d bps not supported\n",
274 baudrate);
275 return 1;
276 }
277 if ((flags & H_INTERACTIVE) != 0) {
278 printf("## Switch baudrate to %d bps and press ENTER ...\n",
279 baudrate);
280 udelay(50000);
281 }
282
283 gd->baudrate = baudrate;
284
285 serial_setbrg();
286
287 udelay(50000);
288
289 if ((flags & H_INTERACTIVE) != 0)
290 while (1) {
291 if (getc() == '\r')
292 break;
293 }
294
295 return 0;
296 case env_op_delete:
297 printf("## Baudrate may not be deleted\n");
298 return 1;
299 default:
300 return 0;
301 }
302}
303U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Simon Glass247f5962014-09-04 16:27:26 -0600304
Simon Glass30a13492015-12-13 21:36:58 -0700305#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass247f5962014-09-04 16:27:26 -0600306static int serial_post_probe(struct udevice *dev)
307{
Simon Glass247f5962014-09-04 16:27:26 -0600308 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700309#ifdef CONFIG_DM_STDIO
Simon Glassde0977b2015-03-05 12:25:20 -0700310 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700311 struct stdio_dev sdev;
312#endif
Simon Glass247f5962014-09-04 16:27:26 -0600313 int ret;
314
Michal Simek0ec473b2015-02-02 16:31:59 +0100315#if defined(CONFIG_NEEDS_MANUAL_RELOC)
316 if (ops->setbrg)
317 ops->setbrg += gd->reloc_off;
318 if (ops->getc)
319 ops->getc += gd->reloc_off;
320 if (ops->putc)
321 ops->putc += gd->reloc_off;
322 if (ops->pending)
323 ops->pending += gd->reloc_off;
324 if (ops->clear)
325 ops->clear += gd->reloc_off;
326#if CONFIG_POST & CONFIG_SYS_POST_UART
327 if (ops->loop)
328 ops->loop += gd->reloc_off
329#endif
330#endif
Simon Glass247f5962014-09-04 16:27:26 -0600331 /* Set the baud rate */
332 if (ops->setbrg) {
333 ret = ops->setbrg(dev, gd->baudrate);
334 if (ret)
335 return ret;
336 }
337
Simon Glassc058d8b2014-11-10 17:16:48 -0700338#ifdef CONFIG_DM_STDIO
Simon Glass247f5962014-09-04 16:27:26 -0600339 if (!(gd->flags & GD_FLG_RELOC))
340 return 0;
Simon Glass247f5962014-09-04 16:27:26 -0600341 memset(&sdev, '\0', sizeof(sdev));
342
343 strncpy(sdev.name, dev->name, sizeof(sdev.name));
344 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
345 sdev.priv = dev;
346 sdev.putc = serial_stub_putc;
347 sdev.puts = serial_stub_puts;
348 sdev.getc = serial_stub_getc;
349 sdev.tstc = serial_stub_tstc;
350 stdio_register_dev(&sdev, &upriv->sdev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700351#endif
Simon Glass247f5962014-09-04 16:27:26 -0600352 return 0;
353}
354
355static int serial_pre_remove(struct udevice *dev)
356{
Simon Glass3110ece2016-10-17 20:13:02 -0600357#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
Simon Glassde0977b2015-03-05 12:25:20 -0700358 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600359
Stefan Roesedcc4b072017-04-24 09:48:01 +0200360 if (stdio_deregister_dev(upriv->sdev, true))
Simon Glass247f5962014-09-04 16:27:26 -0600361 return -EPERM;
362#endif
363
364 return 0;
365}
366
367UCLASS_DRIVER(serial) = {
368 .id = UCLASS_SERIAL,
369 .name = "serial",
Simon Glass0ccb0972015-01-25 08:27:05 -0700370 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass247f5962014-09-04 16:27:26 -0600371 .post_probe = serial_post_probe,
372 .pre_remove = serial_pre_remove,
373 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
374};
Simon Glass30a13492015-12-13 21:36:58 -0700375#endif