blob: 321d23ee93bf5c8d909e2309742eac28eb776671 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass57d92752014-09-04 16:27:26 -06002/*
3 * Copyright (c) 2014 The Chromium OS Authors.
Simon Glass57d92752014-09-04 16:27:26 -06004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glassad1b81c2014-10-29 13:09:03 -06008#include <environment.h>
Simon Glass57d92752014-09-04 16:27:26 -06009#include <errno.h>
Simon Glass57d92752014-09-04 16:27:26 -060010#include <os.h>
11#include <serial.h>
12#include <stdio_dev.h>
Simon Glassc487fd42014-10-22 21:37:02 -060013#include <watchdog.h>
Simon Glass57d92752014-09-04 16:27:26 -060014#include <dm/lists.h>
15#include <dm/device-internal.h>
Simon Glassf93472a2017-06-12 06:21:58 -060016#include <dm/of_access.h>
Simon Glass57d92752014-09-04 16:27:26 -060017
18DECLARE_GLOBAL_DATA_PTR;
19
Simon Glassad1b81c2014-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
Andy Yanf1896c42017-07-24 17:43:34 +080025#if !CONFIG_VAL(SYS_MALLOC_F_LEN)
26#error "Serial is required before relocation - define CONFIG_$(SPL_)SYS_MALLOC_F_LEN to make this work"
Simon Glass57d92752014-09-04 16:27:26 -060027#endif
28
Simon Glassd0960852017-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 Glass57d92752014-09-04 16:27:26 -060072static void serial_find_console_or_panic(void)
73{
Simon Glass6bdc5932015-10-17 19:41:17 -060074 const void *blob = gd->fdt_blob;
Simon Glass469a5792014-11-10 18:00:20 -070075 struct udevice *dev;
Alexander Grafae5326a2018-01-29 13:57:20 +010076#ifdef CONFIG_SERIAL_SEARCH_ALL
Alexander Graf608b0c42018-01-25 12:05:42 +010077 int ret;
Alexander Grafae5326a2018-01-29 13:57:20 +010078#endif
Simon Glass57d92752014-09-04 16:27:26 -060079
Simon Glassb484b0d2016-07-04 11:58:04 -060080 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
81 uclass_first_device(UCLASS_SERIAL, &dev);
82 if (dev) {
83 gd->cur_serial_dev = dev;
84 return;
85 }
86 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
Simon Glassf93472a2017-06-12 06:21:58 -060087 /* Live tree has support for stdout */
88 if (of_live_active()) {
89 struct device_node *np = of_get_stdout();
90
91 if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
92 np_to_ofnode(np), &dev)) {
93 gd->cur_serial_dev = dev;
94 return;
95 }
96 } else {
97 if (!serial_check_stdout(blob, &dev)) {
98 gd->cur_serial_dev = dev;
99 return;
100 }
Simon Glass469a5792014-11-10 18:00:20 -0700101 }
Simon Glassaf282242015-03-06 13:19:03 -0700102 }
Simon Glass6bdc5932015-10-17 19:41:17 -0600103 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
Simon Glasse9fc0582015-05-12 14:55:05 -0600104 /*
Stefan Roeseeb623b92015-11-26 13:38:43 +0100105 * Try to use CONFIG_CONS_INDEX if available (it is numbered
106 * from 1!).
107 *
108 * Failing that, get the device with sequence number 0, or in
Alexander Graf608b0c42018-01-25 12:05:42 +0100109 * extremis just the first working serial device we can find.
110 * But we insist on having a console (even if it is silent).
Stefan Roeseeb623b92015-11-26 13:38:43 +0100111 */
Simon Glass91155c62014-10-22 21:37:06 -0600112#ifdef CONFIG_CONS_INDEX
113#define INDEX (CONFIG_CONS_INDEX - 1)
114#else
115#define INDEX 0
116#endif
Alexander Grafae5326a2018-01-29 13:57:20 +0100117
118#ifdef CONFIG_SERIAL_SEARCH_ALL
Simon Glasse9fc0582015-05-12 14:55:05 -0600119 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
Alexander Graf608b0c42018-01-25 12:05:42 +0100120 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
121 if (dev->flags & DM_FLAG_ACTIVATED) {
122 gd->cur_serial_dev = dev;
123 return;
124 }
125 }
126
127 /* Search for any working device */
128 for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
129 dev;
130 ret = uclass_next_device_check(&dev)) {
131 if (!ret) {
132 /* Device did succeed probing */
133 gd->cur_serial_dev = dev;
134 return;
135 }
Simon Glasse9fc0582015-05-12 14:55:05 -0600136 }
Alexander Grafae5326a2018-01-29 13:57:20 +0100137#else
138 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
139 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
140 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
141 gd->cur_serial_dev = dev;
142 return;
143 }
144#endif
145
Simon Glass91155c62014-10-22 21:37:06 -0600146#undef INDEX
Simon Glasse9fc0582015-05-12 14:55:05 -0600147 }
148
Hans de Goede8c458582015-08-08 17:45:18 +0200149#ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
Simon Glasse9fc0582015-05-12 14:55:05 -0600150 panic_str("No serial driver found");
Hans de Goede8c458582015-08-08 17:45:18 +0200151#endif
Simon Glass57d92752014-09-04 16:27:26 -0600152}
153
154/* Called prior to relocation */
155int serial_init(void)
156{
157 serial_find_console_or_panic();
158 gd->flags |= GD_FLG_SERIAL_READY;
159
160 return 0;
161}
162
163/* Called after relocation */
164void serial_initialize(void)
165{
Simon Glasse4d6ab02016-03-11 22:06:51 -0700166 serial_init();
Simon Glass57d92752014-09-04 16:27:26 -0600167}
168
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900169static void _serial_putc(struct udevice *dev, char ch)
Simon Glass57d92752014-09-04 16:27:26 -0600170{
Masahiro Yamadabac64462014-10-23 22:26:06 +0900171 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600172 int err;
173
Alison Wangc5917b42016-03-02 11:00:36 +0800174 if (ch == '\n')
175 _serial_putc(dev, '\r');
176
Simon Glass57d92752014-09-04 16:27:26 -0600177 do {
Masahiro Yamadabac64462014-10-23 22:26:06 +0900178 err = ops->putc(dev, ch);
Simon Glass57d92752014-09-04 16:27:26 -0600179 } while (err == -EAGAIN);
Simon Glass57d92752014-09-04 16:27:26 -0600180}
181
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900182static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass57d92752014-09-04 16:27:26 -0600183{
184 while (*str)
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900185 _serial_putc(dev, *str++);
Simon Glass57d92752014-09-04 16:27:26 -0600186}
187
Stefan Roese3ca7a062017-08-16 17:37:16 +0200188static int __serial_getc(struct udevice *dev)
Simon Glass57d92752014-09-04 16:27:26 -0600189{
Simon Glassb8893322014-10-01 19:57:23 -0600190 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600191 int err;
192
193 do {
Simon Glassb8893322014-10-01 19:57:23 -0600194 err = ops->getc(dev);
Simon Glassc487fd42014-10-22 21:37:02 -0600195 if (err == -EAGAIN)
196 WATCHDOG_RESET();
Simon Glass57d92752014-09-04 16:27:26 -0600197 } while (err == -EAGAIN);
198
199 return err >= 0 ? err : 0;
200}
201
Stefan Roese3ca7a062017-08-16 17:37:16 +0200202static int __serial_tstc(struct udevice *dev)
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900203{
204 struct dm_serial_ops *ops = serial_get_ops(dev);
205
206 if (ops->pending)
207 return ops->pending(dev, true);
208
209 return 1;
210}
211
Stefan Roese3ca7a062017-08-16 17:37:16 +0200212#if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
213static int _serial_tstc(struct udevice *dev)
214{
215 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
216
217 /* Read all available chars into the RX buffer */
218 while (__serial_tstc(dev)) {
219 upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
220 upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
221 }
222
223 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
224}
225
226static int _serial_getc(struct udevice *dev)
227{
228 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
229 char val;
230
231 val = upriv->buf[upriv->rd_ptr++];
232 upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
233
234 return val;
235}
236
237#else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
238
239static int _serial_getc(struct udevice *dev)
240{
241 return __serial_getc(dev);
242}
243
244static int _serial_tstc(struct udevice *dev)
245{
246 return __serial_tstc(dev);
247}
248#endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
249
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900250void serial_putc(char ch)
251{
Hans de Goede8c458582015-08-08 17:45:18 +0200252 if (gd->cur_serial_dev)
253 _serial_putc(gd->cur_serial_dev, ch);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900254}
255
256void serial_puts(const char *str)
257{
Hans de Goede8c458582015-08-08 17:45:18 +0200258 if (gd->cur_serial_dev)
259 _serial_puts(gd->cur_serial_dev, str);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900260}
261
Simon Glassb8893322014-10-01 19:57:23 -0600262int serial_getc(void)
263{
Hans de Goede8c458582015-08-08 17:45:18 +0200264 if (!gd->cur_serial_dev)
265 return 0;
266
Simon Glass469a5792014-11-10 18:00:20 -0700267 return _serial_getc(gd->cur_serial_dev);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900268}
269
270int serial_tstc(void)
271{
Hans de Goede8c458582015-08-08 17:45:18 +0200272 if (!gd->cur_serial_dev)
273 return 0;
274
Simon Glass469a5792014-11-10 18:00:20 -0700275 return _serial_tstc(gd->cur_serial_dev);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900276}
277
278void serial_setbrg(void)
279{
Hans de Goede8c458582015-08-08 17:45:18 +0200280 struct dm_serial_ops *ops;
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900281
Hans de Goede8c458582015-08-08 17:45:18 +0200282 if (!gd->cur_serial_dev)
283 return;
284
285 ops = serial_get_ops(gd->cur_serial_dev);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900286 if (ops->setbrg)
Simon Glass469a5792014-11-10 18:00:20 -0700287 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
Simon Glassb8893322014-10-01 19:57:23 -0600288}
289
Simon Glass57d92752014-09-04 16:27:26 -0600290void serial_stdio_init(void)
291{
292}
293
Masahiro Yamada49ddcf32017-06-22 16:48:49 +0900294#if defined(CONFIG_DM_STDIO)
295
296#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glassb8893322014-10-01 19:57:23 -0600297static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass57d92752014-09-04 16:27:26 -0600298{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900299 _serial_putc(sdev->priv, ch);
Simon Glass57d92752014-09-04 16:27:26 -0600300}
Simon Glass236f2bd2014-11-10 17:16:48 -0700301#endif
Simon Glass57d92752014-09-04 16:27:26 -0600302
Masahiro Yamada49ddcf32017-06-22 16:48:49 +0900303static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
Simon Glass57d92752014-09-04 16:27:26 -0600304{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900305 _serial_puts(sdev->priv, str);
Simon Glass57d92752014-09-04 16:27:26 -0600306}
307
Masahiro Yamada49ddcf32017-06-22 16:48:49 +0900308static int serial_stub_getc(struct stdio_dev *sdev)
Simon Glass57d92752014-09-04 16:27:26 -0600309{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900310 return _serial_getc(sdev->priv);
Simon Glass57d92752014-09-04 16:27:26 -0600311}
312
Masahiro Yamada49ddcf32017-06-22 16:48:49 +0900313static int serial_stub_tstc(struct stdio_dev *sdev)
Simon Glass57d92752014-09-04 16:27:26 -0600314{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900315 return _serial_tstc(sdev->priv);
Simon Glass57d92752014-09-04 16:27:26 -0600316}
Masahiro Yamada49ddcf32017-06-22 16:48:49 +0900317#endif
Simon Glass57d92752014-09-04 16:27:26 -0600318
Simon Glassad1b81c2014-10-29 13:09:03 -0600319/**
320 * on_baudrate() - Update the actual baudrate when the env var changes
321 *
322 * This will check for a valid baudrate and only apply it if valid.
323 */
324static int on_baudrate(const char *name, const char *value, enum env_op op,
325 int flags)
326{
327 int i;
328 int baudrate;
329
330 switch (op) {
331 case env_op_create:
332 case env_op_overwrite:
333 /*
334 * Switch to new baudrate if new baudrate is supported
335 */
336 baudrate = simple_strtoul(value, NULL, 10);
337
338 /* Not actually changing */
339 if (gd->baudrate == baudrate)
340 return 0;
341
342 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
343 if (baudrate == baudrate_table[i])
344 break;
345 }
346 if (i == ARRAY_SIZE(baudrate_table)) {
347 if ((flags & H_FORCE) == 0)
348 printf("## Baudrate %d bps not supported\n",
349 baudrate);
350 return 1;
351 }
352 if ((flags & H_INTERACTIVE) != 0) {
353 printf("## Switch baudrate to %d bps and press ENTER ...\n",
354 baudrate);
355 udelay(50000);
356 }
357
358 gd->baudrate = baudrate;
359
360 serial_setbrg();
361
362 udelay(50000);
363
364 if ((flags & H_INTERACTIVE) != 0)
365 while (1) {
366 if (getc() == '\r')
367 break;
368 }
369
370 return 0;
371 case env_op_delete:
372 printf("## Baudrate may not be deleted\n");
373 return 1;
374 default:
375 return 0;
376 }
377}
378U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
379
Simon Glass92c55b62015-12-13 21:36:58 -0700380#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass57d92752014-09-04 16:27:26 -0600381static int serial_post_probe(struct udevice *dev)
382{
Simon Glass57d92752014-09-04 16:27:26 -0600383 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass236f2bd2014-11-10 17:16:48 -0700384#ifdef CONFIG_DM_STDIO
Simon Glasse564f052015-03-05 12:25:20 -0700385 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass236f2bd2014-11-10 17:16:48 -0700386 struct stdio_dev sdev;
387#endif
Simon Glass57d92752014-09-04 16:27:26 -0600388 int ret;
389
Michal Simek484fdf52015-02-02 16:31:59 +0100390#if defined(CONFIG_NEEDS_MANUAL_RELOC)
391 if (ops->setbrg)
392 ops->setbrg += gd->reloc_off;
393 if (ops->getc)
394 ops->getc += gd->reloc_off;
395 if (ops->putc)
396 ops->putc += gd->reloc_off;
397 if (ops->pending)
398 ops->pending += gd->reloc_off;
399 if (ops->clear)
400 ops->clear += gd->reloc_off;
401#if CONFIG_POST & CONFIG_SYS_POST_UART
402 if (ops->loop)
403 ops->loop += gd->reloc_off
404#endif
405#endif
Simon Glass57d92752014-09-04 16:27:26 -0600406 /* Set the baud rate */
407 if (ops->setbrg) {
408 ret = ops->setbrg(dev, gd->baudrate);
409 if (ret)
410 return ret;
411 }
412
Simon Glass236f2bd2014-11-10 17:16:48 -0700413#ifdef CONFIG_DM_STDIO
Simon Glass57d92752014-09-04 16:27:26 -0600414 if (!(gd->flags & GD_FLG_RELOC))
415 return 0;
Simon Glass57d92752014-09-04 16:27:26 -0600416 memset(&sdev, '\0', sizeof(sdev));
417
418 strncpy(sdev.name, dev->name, sizeof(sdev.name));
Simon Glass7b3c4c32017-07-27 09:31:04 -0600419 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
Simon Glass57d92752014-09-04 16:27:26 -0600420 sdev.priv = dev;
421 sdev.putc = serial_stub_putc;
422 sdev.puts = serial_stub_puts;
423 sdev.getc = serial_stub_getc;
424 sdev.tstc = serial_stub_tstc;
Stefan Roese3ca7a062017-08-16 17:37:16 +0200425
426#if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
427 /* Allocate the RX buffer */
428 upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
429#endif
430
Simon Glass57d92752014-09-04 16:27:26 -0600431 stdio_register_dev(&sdev, &upriv->sdev);
Simon Glass236f2bd2014-11-10 17:16:48 -0700432#endif
Simon Glass57d92752014-09-04 16:27:26 -0600433 return 0;
434}
435
436static int serial_pre_remove(struct udevice *dev)
437{
Simon Glass869588d2016-10-17 20:13:02 -0600438#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
Simon Glasse564f052015-03-05 12:25:20 -0700439 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600440
Stefan Roesee98856f2017-04-24 09:48:01 +0200441 if (stdio_deregister_dev(upriv->sdev, true))
Simon Glass57d92752014-09-04 16:27:26 -0600442 return -EPERM;
443#endif
444
445 return 0;
446}
447
448UCLASS_DRIVER(serial) = {
449 .id = UCLASS_SERIAL,
450 .name = "serial",
Simon Glass9cc36a22015-01-25 08:27:05 -0700451 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass57d92752014-09-04 16:27:26 -0600452 .post_probe = serial_post_probe,
453 .pre_remove = serial_pre_remove,
454 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
455};
Simon Glass92c55b62015-12-13 21:36:58 -0700456#endif