blob: a9c4f89e1a0901f31dfb4a85bba36fc6d6993b58 [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 Glassafd81a52016-07-04 11:58:04 -060036 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
37 uclass_first_device(UCLASS_SERIAL, &dev);
38 if (dev) {
39 gd->cur_serial_dev = dev;
40 return;
41 }
42 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
Simon Glassc62fb992015-05-12 14:55:05 -060043 /* Check for a chosen console */
Simon Glassc860e3e2015-10-17 19:41:17 -060044 node = fdtdec_get_chosen_node(blob, "stdout-path");
45 if (node < 0) {
46 const char *str, *p, *name;
47
48 /*
49 * Deal with things like
50 * stdout-path = "serial0:115200n8";
51 *
52 * We need to look up the alias and then follow it to
53 * the correct node.
54 */
55 str = fdtdec_get_chosen_prop(blob, "stdout-path");
56 if (str) {
57 p = strchr(str, ':');
58 name = fdt_get_alias_namelen(blob, str,
59 p ? p - str : strlen(str));
60 if (name)
61 node = fdt_path_offset(blob, name);
62 }
63 }
Simon Glassc62fb992015-05-12 14:55:05 -060064 if (node < 0)
Simon Glassc860e3e2015-10-17 19:41:17 -060065 node = fdt_path_offset(blob, "console");
Simon Glassc62fb992015-05-12 14:55:05 -060066 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
67 &dev)) {
Simon Glassa4c2e132014-11-10 18:00:20 -070068 gd->cur_serial_dev = dev;
Simon Glass247f5962014-09-04 16:27:26 -060069 return;
Simon Glassa4c2e132014-11-10 18:00:20 -070070 }
Simon Glassc62fb992015-05-12 14:55:05 -060071
72 /*
Stefan Roese88c3a882015-11-26 13:38:43 +010073 * If the console is not marked to be bound before relocation,
74 * bind it anyway.
75 */
Simon Glassc62fb992015-05-12 14:55:05 -060076 if (node > 0 &&
Simon Glass802b2c62017-05-18 20:09:06 -060077 !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
78 &dev)) {
Simon Glassc62fb992015-05-12 14:55:05 -060079 if (!device_probe(dev)) {
80 gd->cur_serial_dev = dev;
81 return;
82 }
83 }
Simon Glass8579a512015-03-06 13:19:03 -070084 }
Simon Glassc860e3e2015-10-17 19:41:17 -060085 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
Simon Glassc62fb992015-05-12 14:55:05 -060086 /*
Stefan Roese88c3a882015-11-26 13:38:43 +010087 * Try to use CONFIG_CONS_INDEX if available (it is numbered
88 * from 1!).
89 *
90 * Failing that, get the device with sequence number 0, or in
91 * extremis just the first serial device we can find. But we
92 * insist on having a console (even if it is silent).
93 */
Simon Glassbc5aa992014-10-22 21:37:06 -060094#ifdef CONFIG_CONS_INDEX
95#define INDEX (CONFIG_CONS_INDEX - 1)
96#else
97#define INDEX 0
98#endif
Simon Glassc62fb992015-05-12 14:55:05 -060099 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
100 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
Simon Glass66640772015-07-31 09:31:20 -0600101 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
Simon Glassc62fb992015-05-12 14:55:05 -0600102 gd->cur_serial_dev = dev;
103 return;
104 }
Simon Glassbc5aa992014-10-22 21:37:06 -0600105#undef INDEX
Simon Glassc62fb992015-05-12 14:55:05 -0600106 }
107
Hans de Goede04f80942015-08-08 17:45:18 +0200108#ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
Simon Glassc62fb992015-05-12 14:55:05 -0600109 panic_str("No serial driver found");
Hans de Goede04f80942015-08-08 17:45:18 +0200110#endif
Simon Glass247f5962014-09-04 16:27:26 -0600111}
112
113/* Called prior to relocation */
114int serial_init(void)
115{
116 serial_find_console_or_panic();
117 gd->flags |= GD_FLG_SERIAL_READY;
118
119 return 0;
120}
121
122/* Called after relocation */
123void serial_initialize(void)
124{
Simon Glass51f73f12016-03-11 22:06:51 -0700125 serial_init();
Simon Glass247f5962014-09-04 16:27:26 -0600126}
127
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900128static void _serial_putc(struct udevice *dev, char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600129{
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900130 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600131 int err;
132
Alison Wangc0a9ce62016-03-02 11:00:36 +0800133 if (ch == '\n')
134 _serial_putc(dev, '\r');
135
Simon Glass247f5962014-09-04 16:27:26 -0600136 do {
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900137 err = ops->putc(dev, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600138 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600139}
140
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900141static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass73285a22014-10-01 19:57:23 -0600142{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900143 while (*str)
144 _serial_putc(dev, *str++);
Simon Glass73285a22014-10-01 19:57:23 -0600145}
146
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900147static int _serial_getc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600148{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900149 struct dm_serial_ops *ops = serial_get_ops(dev);
150 int err;
Simon Glass247f5962014-09-04 16:27:26 -0600151
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900152 do {
153 err = ops->getc(dev);
154 if (err == -EAGAIN)
155 WATCHDOG_RESET();
156 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600157
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900158 return err >= 0 ? err : 0;
Simon Glass247f5962014-09-04 16:27:26 -0600159}
160
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900161static int _serial_tstc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600162{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900163 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600164
165 if (ops->pending)
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900166 return ops->pending(dev, true);
Simon Glass247f5962014-09-04 16:27:26 -0600167
168 return 1;
169}
170
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900171void serial_putc(char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600172{
Hans de Goede04f80942015-08-08 17:45:18 +0200173 if (gd->cur_serial_dev)
174 _serial_putc(gd->cur_serial_dev, ch);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900175}
Simon Glass247f5962014-09-04 16:27:26 -0600176
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900177void serial_puts(const char *str)
178{
Hans de Goede04f80942015-08-08 17:45:18 +0200179 if (gd->cur_serial_dev)
180 _serial_puts(gd->cur_serial_dev, str);
Simon Glass247f5962014-09-04 16:27:26 -0600181}
182
Simon Glass73285a22014-10-01 19:57:23 -0600183int serial_getc(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_getc(gd->cur_serial_dev);
Simon Glass73285a22014-10-01 19:57:23 -0600189}
190
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900191int serial_tstc(void)
192{
Hans de Goede04f80942015-08-08 17:45:18 +0200193 if (!gd->cur_serial_dev)
194 return 0;
195
Simon Glassa4c2e132014-11-10 18:00:20 -0700196 return _serial_tstc(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900197}
198
199void serial_setbrg(void)
200{
Hans de Goede04f80942015-08-08 17:45:18 +0200201 struct dm_serial_ops *ops;
202
203 if (!gd->cur_serial_dev)
204 return;
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900205
Hans de Goede04f80942015-08-08 17:45:18 +0200206 ops = serial_get_ops(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900207 if (ops->setbrg)
Simon Glassa4c2e132014-11-10 18:00:20 -0700208 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900209}
210
Simon Glass247f5962014-09-04 16:27:26 -0600211void serial_stdio_init(void)
212{
213}
214
Simon Glass30a13492015-12-13 21:36:58 -0700215#if defined(CONFIG_DM_STDIO) && CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass73285a22014-10-01 19:57:23 -0600216static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600217{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900218 _serial_putc(sdev->priv, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600219}
Simon Glassc058d8b2014-11-10 17:16:48 -0700220#endif
Simon Glass247f5962014-09-04 16:27:26 -0600221
222void serial_stub_puts(struct stdio_dev *sdev, const char *str)
223{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900224 _serial_puts(sdev->priv, str);
Simon Glass247f5962014-09-04 16:27:26 -0600225}
226
227int serial_stub_getc(struct stdio_dev *sdev)
228{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900229 return _serial_getc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600230}
231
232int serial_stub_tstc(struct stdio_dev *sdev)
233{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900234 return _serial_tstc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600235}
236
Simon Glass593f50b2014-10-29 13:09:03 -0600237/**
238 * on_baudrate() - Update the actual baudrate when the env var changes
239 *
240 * This will check for a valid baudrate and only apply it if valid.
241 */
242static int on_baudrate(const char *name, const char *value, enum env_op op,
243 int flags)
244{
245 int i;
246 int baudrate;
247
248 switch (op) {
249 case env_op_create:
250 case env_op_overwrite:
251 /*
252 * Switch to new baudrate if new baudrate is supported
253 */
254 baudrate = simple_strtoul(value, NULL, 10);
255
256 /* Not actually changing */
257 if (gd->baudrate == baudrate)
258 return 0;
259
260 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
261 if (baudrate == baudrate_table[i])
262 break;
263 }
264 if (i == ARRAY_SIZE(baudrate_table)) {
265 if ((flags & H_FORCE) == 0)
266 printf("## Baudrate %d bps not supported\n",
267 baudrate);
268 return 1;
269 }
270 if ((flags & H_INTERACTIVE) != 0) {
271 printf("## Switch baudrate to %d bps and press ENTER ...\n",
272 baudrate);
273 udelay(50000);
274 }
275
276 gd->baudrate = baudrate;
277
278 serial_setbrg();
279
280 udelay(50000);
281
282 if ((flags & H_INTERACTIVE) != 0)
283 while (1) {
284 if (getc() == '\r')
285 break;
286 }
287
288 return 0;
289 case env_op_delete:
290 printf("## Baudrate may not be deleted\n");
291 return 1;
292 default:
293 return 0;
294 }
295}
296U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Simon Glass247f5962014-09-04 16:27:26 -0600297
Simon Glass30a13492015-12-13 21:36:58 -0700298#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass247f5962014-09-04 16:27:26 -0600299static int serial_post_probe(struct udevice *dev)
300{
Simon Glass247f5962014-09-04 16:27:26 -0600301 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700302#ifdef CONFIG_DM_STDIO
Simon Glassde0977b2015-03-05 12:25:20 -0700303 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700304 struct stdio_dev sdev;
305#endif
Simon Glass247f5962014-09-04 16:27:26 -0600306 int ret;
307
Michal Simek0ec473b2015-02-02 16:31:59 +0100308#if defined(CONFIG_NEEDS_MANUAL_RELOC)
309 if (ops->setbrg)
310 ops->setbrg += gd->reloc_off;
311 if (ops->getc)
312 ops->getc += gd->reloc_off;
313 if (ops->putc)
314 ops->putc += gd->reloc_off;
315 if (ops->pending)
316 ops->pending += gd->reloc_off;
317 if (ops->clear)
318 ops->clear += gd->reloc_off;
319#if CONFIG_POST & CONFIG_SYS_POST_UART
320 if (ops->loop)
321 ops->loop += gd->reloc_off
322#endif
323#endif
Simon Glass247f5962014-09-04 16:27:26 -0600324 /* Set the baud rate */
325 if (ops->setbrg) {
326 ret = ops->setbrg(dev, gd->baudrate);
327 if (ret)
328 return ret;
329 }
330
Simon Glassc058d8b2014-11-10 17:16:48 -0700331#ifdef CONFIG_DM_STDIO
Simon Glass247f5962014-09-04 16:27:26 -0600332 if (!(gd->flags & GD_FLG_RELOC))
333 return 0;
Simon Glass247f5962014-09-04 16:27:26 -0600334 memset(&sdev, '\0', sizeof(sdev));
335
336 strncpy(sdev.name, dev->name, sizeof(sdev.name));
337 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
338 sdev.priv = dev;
339 sdev.putc = serial_stub_putc;
340 sdev.puts = serial_stub_puts;
341 sdev.getc = serial_stub_getc;
342 sdev.tstc = serial_stub_tstc;
343 stdio_register_dev(&sdev, &upriv->sdev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700344#endif
Simon Glass247f5962014-09-04 16:27:26 -0600345 return 0;
346}
347
348static int serial_pre_remove(struct udevice *dev)
349{
Simon Glass3110ece2016-10-17 20:13:02 -0600350#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
Simon Glassde0977b2015-03-05 12:25:20 -0700351 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600352
Stefan Roesedcc4b072017-04-24 09:48:01 +0200353 if (stdio_deregister_dev(upriv->sdev, true))
Simon Glass247f5962014-09-04 16:27:26 -0600354 return -EPERM;
355#endif
356
357 return 0;
358}
359
360UCLASS_DRIVER(serial) = {
361 .id = UCLASS_SERIAL,
362 .name = "serial",
Simon Glass0ccb0972015-01-25 08:27:05 -0700363 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass247f5962014-09-04 16:27:26 -0600364 .post_probe = serial_post_probe,
365 .pre_remove = serial_pre_remove,
366 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
367};
Simon Glass30a13492015-12-13 21:36:58 -0700368#endif