blob: 8171b17faf88ccba60deb759e16973ac8f089d45 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass247f5962014-09-04 16:27:26 -06002/*
3 * Copyright (c) 2014 The Chromium OS Authors.
Simon Glass247f5962014-09-04 16:27:26 -06004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_SERIAL
7
Simon Glass247f5962014-09-04 16:27:26 -06008#include <common.h>
9#include <dm.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060010#include <env_internal.h>
Simon Glass247f5962014-09-04 16:27:26 -060011#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Simon Glass247f5962014-09-04 16:27:26 -060013#include <os.h>
14#include <serial.h>
15#include <stdio_dev.h>
Simon Glass5c60b2a2014-10-22 21:37:02 -060016#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass247f5962014-09-04 16:27:26 -060018#include <dm/lists.h>
19#include <dm/device-internal.h>
Simon Glassa21ec4c2017-06-12 06:21:58 -060020#include <dm/of_access.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Simon Glass247f5962014-09-04 16:27:26 -060022
23DECLARE_GLOBAL_DATA_PTR;
24
Simon Glass593f50b2014-10-29 13:09:03 -060025/*
26 * Table with supported baudrates (defined in config_xyz.h)
27 */
28static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
29
Andy Yan1fa20e4d2017-07-24 17:43:34 +080030#if !CONFIG_VAL(SYS_MALLOC_F_LEN)
31#error "Serial is required before relocation - define CONFIG_$(SPL_)SYS_MALLOC_F_LEN to make this work"
Simon Glass247f5962014-09-04 16:27:26 -060032#endif
33
Simon Glass60573922018-10-01 12:22:20 -060034#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass89d9cf22017-06-12 06:21:57 -060035static int serial_check_stdout(const void *blob, struct udevice **devp)
36{
Bin Mengd86eba42019-11-25 05:52:36 -080037 int node = -1;
38 const char *str, *p, *name;
39 int namelen;
Simon Glass89d9cf22017-06-12 06:21:57 -060040
41 /* Check for a chosen console */
Bin Mengd86eba42019-11-25 05:52:36 -080042 str = fdtdec_get_chosen_prop(blob, "stdout-path");
43 if (str) {
44 p = strchr(str, ':');
45 namelen = p ? p - str : strlen(str);
46 node = fdt_path_offset_namelen(blob, str, namelen);
Simon Glass89d9cf22017-06-12 06:21:57 -060047
Bin Mengd86eba42019-11-25 05:52:36 -080048 if (node < 0) {
49 /*
50 * Deal with things like
51 * stdout-path = "serial0:115200n8";
52 *
53 * We need to look up the alias and then follow it to
54 * the correct node.
55 */
56 name = fdt_get_alias_namelen(blob, str, namelen);
Simon Glass89d9cf22017-06-12 06:21:57 -060057 if (name)
58 node = fdt_path_offset(blob, name);
59 }
60 }
Bin Mengd86eba42019-11-25 05:52:36 -080061
Simon Glass89d9cf22017-06-12 06:21:57 -060062 if (node < 0)
63 node = fdt_path_offset(blob, "console");
64 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
65 return 0;
66
67 /*
68 * If the console is not marked to be bound before relocation, bind it
69 * anyway.
70 */
71 if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
Bin Meng9a9b0742018-10-10 22:06:58 -070072 devp, false)) {
Simon Glass89d9cf22017-06-12 06:21:57 -060073 if (!device_probe(*devp))
74 return 0;
75 }
76
77 return -ENODEV;
78}
79
Simon Glass247f5962014-09-04 16:27:26 -060080static void serial_find_console_or_panic(void)
81{
Simon Glassc860e3e2015-10-17 19:41:17 -060082 const void *blob = gd->fdt_blob;
Simon Glassa4c2e132014-11-10 18:00:20 -070083 struct udevice *dev;
Alexander Grafc8bda542018-01-29 13:57:20 +010084#ifdef CONFIG_SERIAL_SEARCH_ALL
Alexander Graf132ca2d2018-01-25 12:05:42 +010085 int ret;
Alexander Grafc8bda542018-01-29 13:57:20 +010086#endif
Simon Glass247f5962014-09-04 16:27:26 -060087
Simon Glassafd81a52016-07-04 11:58:04 -060088 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
89 uclass_first_device(UCLASS_SERIAL, &dev);
90 if (dev) {
91 gd->cur_serial_dev = dev;
92 return;
93 }
94 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
Simon Glassa21ec4c2017-06-12 06:21:58 -060095 /* Live tree has support for stdout */
96 if (of_live_active()) {
97 struct device_node *np = of_get_stdout();
98
99 if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
100 np_to_ofnode(np), &dev)) {
101 gd->cur_serial_dev = dev;
102 return;
103 }
104 } else {
105 if (!serial_check_stdout(blob, &dev)) {
106 gd->cur_serial_dev = dev;
107 return;
108 }
Simon Glassa4c2e132014-11-10 18:00:20 -0700109 }
Simon Glass8579a512015-03-06 13:19:03 -0700110 }
Simon Glassc860e3e2015-10-17 19:41:17 -0600111 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
Simon Glassc62fb992015-05-12 14:55:05 -0600112 /*
Stefan Roese88c3a882015-11-26 13:38:43 +0100113 * Try to use CONFIG_CONS_INDEX if available (it is numbered
114 * from 1!).
115 *
116 * Failing that, get the device with sequence number 0, or in
Alexander Graf132ca2d2018-01-25 12:05:42 +0100117 * extremis just the first working serial device we can find.
118 * But we insist on having a console (even if it is silent).
Stefan Roese88c3a882015-11-26 13:38:43 +0100119 */
Simon Glassbc5aa992014-10-22 21:37:06 -0600120#ifdef CONFIG_CONS_INDEX
121#define INDEX (CONFIG_CONS_INDEX - 1)
122#else
123#define INDEX 0
124#endif
Alexander Grafc8bda542018-01-29 13:57:20 +0100125
126#ifdef CONFIG_SERIAL_SEARCH_ALL
Simon Glassc62fb992015-05-12 14:55:05 -0600127 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
Alexander Graf132ca2d2018-01-25 12:05:42 +0100128 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
Simon Glass6211d762020-12-19 10:40:10 -0700129 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) {
Alexander Graf132ca2d2018-01-25 12:05:42 +0100130 gd->cur_serial_dev = dev;
131 return;
132 }
133 }
134
135 /* Search for any working device */
136 for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
137 dev;
138 ret = uclass_next_device_check(&dev)) {
139 if (!ret) {
140 /* Device did succeed probing */
141 gd->cur_serial_dev = dev;
142 return;
143 }
Simon Glassc62fb992015-05-12 14:55:05 -0600144 }
Alexander Grafc8bda542018-01-29 13:57:20 +0100145#else
146 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
147 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
148 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
149 gd->cur_serial_dev = dev;
150 return;
151 }
152#endif
153
Simon Glassbc5aa992014-10-22 21:37:06 -0600154#undef INDEX
Simon Glassc62fb992015-05-12 14:55:05 -0600155 }
156
Hans de Goede04f80942015-08-08 17:45:18 +0200157#ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
Simon Glassc62fb992015-05-12 14:55:05 -0600158 panic_str("No serial driver found");
Hans de Goede04f80942015-08-08 17:45:18 +0200159#endif
Simon Glass247f5962014-09-04 16:27:26 -0600160}
Simon Glass60573922018-10-01 12:22:20 -0600161#endif /* CONFIG_SERIAL_PRESENT */
Simon Glass247f5962014-09-04 16:27:26 -0600162
163/* Called prior to relocation */
164int serial_init(void)
165{
Simon Glass60573922018-10-01 12:22:20 -0600166#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass247f5962014-09-04 16:27:26 -0600167 serial_find_console_or_panic();
168 gd->flags |= GD_FLG_SERIAL_READY;
Sean Anderson01c18af2020-02-02 13:15:17 -0500169 serial_setbrg();
Simon Glass60573922018-10-01 12:22:20 -0600170#endif
Simon Glass247f5962014-09-04 16:27:26 -0600171
172 return 0;
173}
174
175/* Called after relocation */
Ovidiu Panait96008c92020-07-24 14:12:22 +0300176int serial_initialize(void)
Simon Glass247f5962014-09-04 16:27:26 -0600177{
Vabhav Sharma42149c92020-12-09 10:42:04 +0530178 /* Scanning uclass to probe devices */
179 if (IS_ENABLED(CONFIG_SERIAL_PROBE_ALL)) {
180 int ret;
181
182 ret = uclass_probe_all(UCLASS_SERIAL);
183 if (ret)
184 return ret;
185 }
186
Ovidiu Panait96008c92020-07-24 14:12:22 +0300187 return serial_init();
Simon Glass247f5962014-09-04 16:27:26 -0600188}
189
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900190static void _serial_putc(struct udevice *dev, char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600191{
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900192 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600193 int err;
194
Alison Wangc0a9ce62016-03-02 11:00:36 +0800195 if (ch == '\n')
196 _serial_putc(dev, '\r');
197
Simon Glass247f5962014-09-04 16:27:26 -0600198 do {
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900199 err = ops->putc(dev, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600200 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600201}
202
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900203static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass73285a22014-10-01 19:57:23 -0600204{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900205 while (*str)
206 _serial_putc(dev, *str++);
Simon Glass73285a22014-10-01 19:57:23 -0600207}
208
Stefan Roese02072832017-08-16 17:37:16 +0200209static int __serial_getc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600210{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900211 struct dm_serial_ops *ops = serial_get_ops(dev);
212 int err;
Simon Glass247f5962014-09-04 16:27:26 -0600213
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900214 do {
215 err = ops->getc(dev);
216 if (err == -EAGAIN)
217 WATCHDOG_RESET();
218 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600219
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900220 return err >= 0 ? err : 0;
Simon Glass247f5962014-09-04 16:27:26 -0600221}
222
Stefan Roese02072832017-08-16 17:37:16 +0200223static int __serial_tstc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600224{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900225 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600226
227 if (ops->pending)
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900228 return ops->pending(dev, true);
Simon Glass247f5962014-09-04 16:27:26 -0600229
230 return 1;
231}
232
Stefan Roese02072832017-08-16 17:37:16 +0200233#if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
234static int _serial_tstc(struct udevice *dev)
235{
236 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
237
238 /* Read all available chars into the RX buffer */
239 while (__serial_tstc(dev)) {
240 upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
241 upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
242 }
243
244 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
245}
246
247static int _serial_getc(struct udevice *dev)
248{
249 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
250 char val;
251
Patrick Delaunay9c442822018-08-03 13:38:43 +0200252 if (upriv->rd_ptr == upriv->wr_ptr)
253 return __serial_getc(dev);
254
Stefan Roese02072832017-08-16 17:37:16 +0200255 val = upriv->buf[upriv->rd_ptr++];
256 upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
257
258 return val;
259}
260
261#else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
262
263static int _serial_getc(struct udevice *dev)
264{
265 return __serial_getc(dev);
266}
267
268static int _serial_tstc(struct udevice *dev)
269{
270 return __serial_tstc(dev);
271}
272#endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
273
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900274void serial_putc(char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600275{
Hans de Goede04f80942015-08-08 17:45:18 +0200276 if (gd->cur_serial_dev)
277 _serial_putc(gd->cur_serial_dev, ch);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900278}
Simon Glass247f5962014-09-04 16:27:26 -0600279
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900280void serial_puts(const char *str)
281{
Hans de Goede04f80942015-08-08 17:45:18 +0200282 if (gd->cur_serial_dev)
283 _serial_puts(gd->cur_serial_dev, str);
Simon Glass247f5962014-09-04 16:27:26 -0600284}
285
Simon Glass73285a22014-10-01 19:57:23 -0600286int serial_getc(void)
287{
Hans de Goede04f80942015-08-08 17:45:18 +0200288 if (!gd->cur_serial_dev)
289 return 0;
290
Simon Glassa4c2e132014-11-10 18:00:20 -0700291 return _serial_getc(gd->cur_serial_dev);
Simon Glass73285a22014-10-01 19:57:23 -0600292}
293
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900294int serial_tstc(void)
295{
Hans de Goede04f80942015-08-08 17:45:18 +0200296 if (!gd->cur_serial_dev)
297 return 0;
298
Simon Glassa4c2e132014-11-10 18:00:20 -0700299 return _serial_tstc(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900300}
301
302void serial_setbrg(void)
303{
Hans de Goede04f80942015-08-08 17:45:18 +0200304 struct dm_serial_ops *ops;
305
306 if (!gd->cur_serial_dev)
307 return;
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900308
Hans de Goede04f80942015-08-08 17:45:18 +0200309 ops = serial_get_ops(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900310 if (ops->setbrg)
Simon Glassa4c2e132014-11-10 18:00:20 -0700311 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900312}
313
Simon Glassdaaff932018-12-28 14:23:08 -0700314int serial_getconfig(struct udevice *dev, uint *config)
Andy Shevchenko08e98792018-11-20 23:52:32 +0200315{
316 struct dm_serial_ops *ops;
317
Simon Glassdaaff932018-12-28 14:23:08 -0700318 ops = serial_get_ops(dev);
Andy Shevchenko08e98792018-11-20 23:52:32 +0200319 if (ops->getconfig)
Simon Glassdaaff932018-12-28 14:23:08 -0700320 return ops->getconfig(dev, config);
Andy Shevchenko08e98792018-11-20 23:52:32 +0200321
322 return 0;
323}
324
Simon Glassde62d482018-12-28 14:23:09 -0700325int serial_setconfig(struct udevice *dev, uint config)
Patrice Chotard46c1d9f2018-08-03 15:07:38 +0200326{
327 struct dm_serial_ops *ops;
328
Simon Glassde62d482018-12-28 14:23:09 -0700329 ops = serial_get_ops(dev);
Patrice Chotard46c1d9f2018-08-03 15:07:38 +0200330 if (ops->setconfig)
Simon Glassde62d482018-12-28 14:23:09 -0700331 return ops->setconfig(dev, config);
Patrice Chotard46c1d9f2018-08-03 15:07:38 +0200332
333 return 0;
334}
335
Simon Glass896c1642018-12-28 14:23:10 -0700336int serial_getinfo(struct udevice *dev, struct serial_device_info *info)
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200337{
338 struct dm_serial_ops *ops;
339
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200340 if (!info)
341 return -EINVAL;
342
343 info->baudrate = gd->baudrate;
344
Simon Glass896c1642018-12-28 14:23:10 -0700345 ops = serial_get_ops(dev);
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200346 if (ops->getinfo)
Simon Glass896c1642018-12-28 14:23:10 -0700347 return ops->getinfo(dev, info);
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200348
349 return -EINVAL;
350}
351
Simon Glass247f5962014-09-04 16:27:26 -0600352void serial_stdio_init(void)
353{
354}
355
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900356#if defined(CONFIG_DM_STDIO)
357
358#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass73285a22014-10-01 19:57:23 -0600359static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600360{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900361 _serial_putc(sdev->priv, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600362}
Simon Glassc058d8b2014-11-10 17:16:48 -0700363#endif
Simon Glass247f5962014-09-04 16:27:26 -0600364
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900365static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
Simon Glass247f5962014-09-04 16:27:26 -0600366{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900367 _serial_puts(sdev->priv, str);
Simon Glass247f5962014-09-04 16:27:26 -0600368}
369
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900370static int serial_stub_getc(struct stdio_dev *sdev)
Simon Glass247f5962014-09-04 16:27:26 -0600371{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900372 return _serial_getc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600373}
374
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900375static int serial_stub_tstc(struct stdio_dev *sdev)
Simon Glass247f5962014-09-04 16:27:26 -0600376{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900377 return _serial_tstc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600378}
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900379#endif
Simon Glass247f5962014-09-04 16:27:26 -0600380
Simon Glass593f50b2014-10-29 13:09:03 -0600381/**
382 * on_baudrate() - Update the actual baudrate when the env var changes
383 *
384 * This will check for a valid baudrate and only apply it if valid.
385 */
386static int on_baudrate(const char *name, const char *value, enum env_op op,
387 int flags)
388{
389 int i;
390 int baudrate;
391
392 switch (op) {
393 case env_op_create:
394 case env_op_overwrite:
395 /*
396 * Switch to new baudrate if new baudrate is supported
397 */
Simon Glassff9b9032021-07-24 09:03:30 -0600398 baudrate = dectoul(value, NULL);
Simon Glass593f50b2014-10-29 13:09:03 -0600399
400 /* Not actually changing */
401 if (gd->baudrate == baudrate)
402 return 0;
403
404 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
405 if (baudrate == baudrate_table[i])
406 break;
407 }
408 if (i == ARRAY_SIZE(baudrate_table)) {
409 if ((flags & H_FORCE) == 0)
410 printf("## Baudrate %d bps not supported\n",
411 baudrate);
412 return 1;
413 }
414 if ((flags & H_INTERACTIVE) != 0) {
415 printf("## Switch baudrate to %d bps and press ENTER ...\n",
416 baudrate);
417 udelay(50000);
418 }
419
420 gd->baudrate = baudrate;
421
422 serial_setbrg();
423
424 udelay(50000);
425
426 if ((flags & H_INTERACTIVE) != 0)
427 while (1) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200428 if (getchar() == '\r')
Simon Glass593f50b2014-10-29 13:09:03 -0600429 break;
430 }
431
432 return 0;
433 case env_op_delete:
434 printf("## Baudrate may not be deleted\n");
435 return 1;
436 default:
437 return 0;
438 }
439}
440U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Simon Glass247f5962014-09-04 16:27:26 -0600441
Simon Glass30a13492015-12-13 21:36:58 -0700442#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass247f5962014-09-04 16:27:26 -0600443static int serial_post_probe(struct udevice *dev)
444{
Simon Glass247f5962014-09-04 16:27:26 -0600445 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700446#ifdef CONFIG_DM_STDIO
Simon Glassde0977b2015-03-05 12:25:20 -0700447 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700448 struct stdio_dev sdev;
449#endif
Simon Glass247f5962014-09-04 16:27:26 -0600450 int ret;
451
Michal Simek0ec473b2015-02-02 16:31:59 +0100452#if defined(CONFIG_NEEDS_MANUAL_RELOC)
453 if (ops->setbrg)
454 ops->setbrg += gd->reloc_off;
455 if (ops->getc)
456 ops->getc += gd->reloc_off;
457 if (ops->putc)
458 ops->putc += gd->reloc_off;
459 if (ops->pending)
460 ops->pending += gd->reloc_off;
461 if (ops->clear)
462 ops->clear += gd->reloc_off;
Andy Shevchenko08e98792018-11-20 23:52:32 +0200463 if (ops->getconfig)
464 ops->getconfig += gd->reloc_off;
Patrice Chotard46c1d9f2018-08-03 15:07:38 +0200465 if (ops->setconfig)
466 ops->setconfig += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100467#if CONFIG_POST & CONFIG_SYS_POST_UART
468 if (ops->loop)
Andy Shevchenko84139bc2018-11-05 18:24:03 +0200469 ops->loop += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100470#endif
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200471 if (ops->getinfo)
472 ops->getinfo += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100473#endif
Simon Glass247f5962014-09-04 16:27:26 -0600474 /* Set the baud rate */
475 if (ops->setbrg) {
476 ret = ops->setbrg(dev, gd->baudrate);
477 if (ret)
478 return ret;
479 }
480
Simon Glassc058d8b2014-11-10 17:16:48 -0700481#ifdef CONFIG_DM_STDIO
Simon Glass247f5962014-09-04 16:27:26 -0600482 if (!(gd->flags & GD_FLG_RELOC))
483 return 0;
Simon Glass247f5962014-09-04 16:27:26 -0600484 memset(&sdev, '\0', sizeof(sdev));
485
486 strncpy(sdev.name, dev->name, sizeof(sdev.name));
Simon Glass10a7fe92017-07-27 09:31:04 -0600487 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
Simon Glass247f5962014-09-04 16:27:26 -0600488 sdev.priv = dev;
489 sdev.putc = serial_stub_putc;
490 sdev.puts = serial_stub_puts;
491 sdev.getc = serial_stub_getc;
492 sdev.tstc = serial_stub_tstc;
Stefan Roese02072832017-08-16 17:37:16 +0200493
494#if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
495 /* Allocate the RX buffer */
496 upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
497#endif
498
Simon Glass247f5962014-09-04 16:27:26 -0600499 stdio_register_dev(&sdev, &upriv->sdev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700500#endif
Simon Glass247f5962014-09-04 16:27:26 -0600501 return 0;
502}
503
504static int serial_pre_remove(struct udevice *dev)
505{
Simon Glass3110ece2016-10-17 20:13:02 -0600506#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
Simon Glassde0977b2015-03-05 12:25:20 -0700507 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600508
Stefan Roesedcc4b072017-04-24 09:48:01 +0200509 if (stdio_deregister_dev(upriv->sdev, true))
Simon Glass247f5962014-09-04 16:27:26 -0600510 return -EPERM;
511#endif
512
513 return 0;
514}
515
516UCLASS_DRIVER(serial) = {
517 .id = UCLASS_SERIAL,
518 .name = "serial",
Simon Glass0ccb0972015-01-25 08:27:05 -0700519 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass247f5962014-09-04 16:27:26 -0600520 .post_probe = serial_post_probe,
521 .pre_remove = serial_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700522 .per_device_auto = sizeof(struct serial_dev_priv),
Simon Glass247f5962014-09-04 16:27:26 -0600523};
Simon Glass30a13492015-12-13 21:36:58 -0700524#endif