blob: a08678dde4e1935255c73ca3859cc274f3f620f5 [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
Tom Rinidec7ea02024-05-20 13:35:03 -06008#include <config.h>
Simon Glass247f5962014-09-04 16:27:26 -06009#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>
Rasmus Villemoesa865db72024-10-03 16:10:28 +020021#include <linux/build_bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Simon Glass247f5962014-09-04 16:27:26 -060023
24DECLARE_GLOBAL_DATA_PTR;
25
Simon Glass593f50b2014-10-29 13:09:03 -060026/*
27 * Table with supported baudrates (defined in config_xyz.h)
28 */
Tom Rini6a5dccc2022-11-16 13:10:41 -050029static const unsigned long baudrate_table[] = CFG_SYS_BAUDRATE_TABLE;
Simon Glass593f50b2014-10-29 13:09:03 -060030
Simon Glass60573922018-10-01 12:22:20 -060031#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass89d9cf22017-06-12 06:21:57 -060032static int serial_check_stdout(const void *blob, struct udevice **devp)
33{
Bin Mengd86eba42019-11-25 05:52:36 -080034 int node = -1;
Rasmus Villemoesf439a1e2023-03-29 09:57:51 +020035 const char *str, *p;
Bin Mengd86eba42019-11-25 05:52:36 -080036 int namelen;
Simon Glass89d9cf22017-06-12 06:21:57 -060037
38 /* Check for a chosen console */
Bin Mengd86eba42019-11-25 05:52:36 -080039 str = fdtdec_get_chosen_prop(blob, "stdout-path");
40 if (str) {
41 p = strchr(str, ':');
42 namelen = p ? p - str : strlen(str);
Rasmus Villemoesf439a1e2023-03-29 09:57:51 +020043 /*
44 * This also deals with things like
45 *
46 * stdout-path = "serial0:115200n8";
47 *
48 * since fdt_path_offset_namelen() treats a str not
49 * beginning with '/' as an alias and thus applies
50 * fdt_get_alias_namelen() to it.
51 */
Bin Mengd86eba42019-11-25 05:52:36 -080052 node = fdt_path_offset_namelen(blob, str, namelen);
Simon Glass89d9cf22017-06-12 06:21:57 -060053 }
Bin Mengd86eba42019-11-25 05:52:36 -080054
Simon Glass89d9cf22017-06-12 06:21:57 -060055 if (node < 0)
56 node = fdt_path_offset(blob, "console");
57 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
58 return 0;
59
60 /*
61 * If the console is not marked to be bound before relocation, bind it
62 * anyway.
63 */
64 if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
Patrice Chotardcc523cf2021-09-10 16:16:20 +020065 devp, NULL, false)) {
Mark Kettenisf5309ef2022-02-21 22:17:37 +010066 if (device_get_uclass_id(*devp) == UCLASS_SERIAL &&
67 !device_probe(*devp))
Simon Glass89d9cf22017-06-12 06:21:57 -060068 return 0;
69 }
70
71 return -ENODEV;
72}
73
Simon Glass247f5962014-09-04 16:27:26 -060074static void serial_find_console_or_panic(void)
75{
Simon Glassc860e3e2015-10-17 19:41:17 -060076 const void *blob = gd->fdt_blob;
Simon Glassa4c2e132014-11-10 18:00:20 -070077 struct udevice *dev;
Alexander Grafc8bda542018-01-29 13:57:20 +010078#ifdef CONFIG_SERIAL_SEARCH_ALL
Alexander Graf132ca2d2018-01-25 12:05:42 +010079 int ret;
Alexander Grafc8bda542018-01-29 13:57:20 +010080#endif
Simon Glass247f5962014-09-04 16:27:26 -060081
Simon Glassafd81a52016-07-04 11:58:04 -060082 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
83 uclass_first_device(UCLASS_SERIAL, &dev);
84 if (dev) {
85 gd->cur_serial_dev = dev;
86 return;
87 }
88 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
Simon Glassa21ec4c2017-06-12 06:21:58 -060089 /* Live tree has support for stdout */
90 if (of_live_active()) {
91 struct device_node *np = of_get_stdout();
92
93 if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
94 np_to_ofnode(np), &dev)) {
95 gd->cur_serial_dev = dev;
96 return;
97 }
98 } else {
99 if (!serial_check_stdout(blob, &dev)) {
100 gd->cur_serial_dev = dev;
101 return;
102 }
Simon Glassa4c2e132014-11-10 18:00:20 -0700103 }
Simon Glass8579a512015-03-06 13:19:03 -0700104 }
Simon Glass7ec24132024-09-29 19:49:48 -0600105 if (!IS_ENABLED(CONFIG_XPL_BUILD) || !CONFIG_IS_ENABLED(OF_CONTROL) ||
Simon Glass65f83292022-01-12 19:26:15 -0700106 !blob) {
Simon Glassc62fb992015-05-12 14:55:05 -0600107 /*
Stefan Roese88c3a882015-11-26 13:38:43 +0100108 * Try to use CONFIG_CONS_INDEX if available (it is numbered
109 * from 1!).
110 *
111 * Failing that, get the device with sequence number 0, or in
Alexander Graf132ca2d2018-01-25 12:05:42 +0100112 * extremis just the first working serial device we can find.
113 * But we insist on having a console (even if it is silent).
Stefan Roese88c3a882015-11-26 13:38:43 +0100114 */
Simon Glassbc5aa992014-10-22 21:37:06 -0600115#ifdef CONFIG_CONS_INDEX
116#define INDEX (CONFIG_CONS_INDEX - 1)
117#else
118#define INDEX 0
119#endif
Alexander Grafc8bda542018-01-29 13:57:20 +0100120
121#ifdef CONFIG_SERIAL_SEARCH_ALL
Simon Glassc62fb992015-05-12 14:55:05 -0600122 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
Alexander Graf132ca2d2018-01-25 12:05:42 +0100123 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
Simon Glass6211d762020-12-19 10:40:10 -0700124 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) {
Alexander Graf132ca2d2018-01-25 12:05:42 +0100125 gd->cur_serial_dev = dev;
126 return;
127 }
128 }
129
130 /* Search for any working device */
131 for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
132 dev;
133 ret = uclass_next_device_check(&dev)) {
134 if (!ret) {
135 /* Device did succeed probing */
136 gd->cur_serial_dev = dev;
137 return;
138 }
Simon Glassc62fb992015-05-12 14:55:05 -0600139 }
Alexander Grafc8bda542018-01-29 13:57:20 +0100140#else
141 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
142 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200143 !uclass_first_device_err(UCLASS_SERIAL, &dev)) {
Alexander Grafc8bda542018-01-29 13:57:20 +0100144 gd->cur_serial_dev = dev;
145 return;
146 }
147#endif
148
Simon Glassbc5aa992014-10-22 21:37:06 -0600149#undef INDEX
Simon Glassc62fb992015-05-12 14:55:05 -0600150 }
151
Hans de Goede04f80942015-08-08 17:45:18 +0200152#ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
Simon Glassc62fb992015-05-12 14:55:05 -0600153 panic_str("No serial driver found");
Hans de Goede04f80942015-08-08 17:45:18 +0200154#endif
Maksim Kiselev5f6d9142023-08-18 12:34:30 +0300155 gd->cur_serial_dev = NULL;
Simon Glass247f5962014-09-04 16:27:26 -0600156}
Simon Glass60573922018-10-01 12:22:20 -0600157#endif /* CONFIG_SERIAL_PRESENT */
Simon Glass247f5962014-09-04 16:27:26 -0600158
Algapally Santosh Sagardf178992023-09-21 16:50:43 +0530159/**
160 * check_valid_baudrate() - Check whether baudrate is valid or not
161 *
162 * @baud: baud rate to check
163 * Return: 0 if OK, -ve on error
164 */
165static int check_valid_baudrate(int baud)
166{
167 int i;
168
169 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
170 if (baud == baudrate_table[i])
171 return 0;
172 }
173
174 return -EINVAL;
175}
176
177int fetch_baud_from_dtb(void)
178{
179 int baud_value, ret;
180
181 baud_value = ofnode_read_baud();
182 ret = check_valid_baudrate(baud_value);
183 if (ret)
184 return ret;
185
186 return baud_value;
187}
188
Simon Glass247f5962014-09-04 16:27:26 -0600189/* Called prior to relocation */
190int serial_init(void)
191{
Simon Glass60573922018-10-01 12:22:20 -0600192#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass247f5962014-09-04 16:27:26 -0600193 serial_find_console_or_panic();
194 gd->flags |= GD_FLG_SERIAL_READY;
Algapally Santosh Sagardf178992023-09-21 16:50:43 +0530195
196 if (IS_ENABLED(CONFIG_OF_SERIAL_BAUD)) {
197 int ret = 0;
198 char *ptr = (char*)&default_environment[0];
199
200 /*
201 * Fetch the baudrate from the dtb and update the value in the
202 * default environment.
203 */
204 ret = fetch_baud_from_dtb();
205 if (ret != -EINVAL && ret != -EFAULT) {
206 gd->baudrate = ret;
207
208 while (*ptr != '\0' && *(ptr + 1) != '\0')
209 ptr++;
210 ptr += 2;
211 sprintf(ptr, "baudrate=%d", gd->baudrate);
212 }
213 }
Sean Anderson01c18af2020-02-02 13:15:17 -0500214 serial_setbrg();
Simon Glass60573922018-10-01 12:22:20 -0600215#endif
Simon Glass247f5962014-09-04 16:27:26 -0600216
217 return 0;
218}
219
220/* Called after relocation */
Ovidiu Panait96008c92020-07-24 14:12:22 +0300221int serial_initialize(void)
Simon Glass247f5962014-09-04 16:27:26 -0600222{
Vabhav Sharma42149c92020-12-09 10:42:04 +0530223 /* Scanning uclass to probe devices */
224 if (IS_ENABLED(CONFIG_SERIAL_PROBE_ALL)) {
225 int ret;
226
227 ret = uclass_probe_all(UCLASS_SERIAL);
228 if (ret)
229 return ret;
230 }
231
Ovidiu Panait96008c92020-07-24 14:12:22 +0300232 return serial_init();
Simon Glass247f5962014-09-04 16:27:26 -0600233}
234
Rasmus Villemoes83a5d2b2023-10-16 10:35:21 +0200235static void _serial_flush(struct udevice *dev)
236{
237 struct dm_serial_ops *ops = serial_get_ops(dev);
238
239 if (!ops->pending)
240 return;
241 while (ops->pending(dev, false) > 0)
242 ;
243}
Rasmus Villemoes83a5d2b2023-10-16 10:35:21 +0200244
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900245static void _serial_putc(struct udevice *dev, char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600246{
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900247 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600248 int err;
249
Alison Wangc0a9ce62016-03-02 11:00:36 +0800250 if (ch == '\n')
251 _serial_putc(dev, '\r');
252
Simon Glass247f5962014-09-04 16:27:26 -0600253 do {
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900254 err = ops->putc(dev, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600255 } while (err == -EAGAIN);
Rasmus Villemoes0b491f52023-10-16 10:35:22 +0200256
257 if (IS_ENABLED(CONFIG_CONSOLE_FLUSH_ON_NEWLINE) && ch == '\n')
258 _serial_flush(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600259}
260
Sean Andersond56ce3c2022-04-04 14:17:57 -0400261static int __serial_puts(struct udevice *dev, const char *str, size_t len)
262{
263 struct dm_serial_ops *ops = serial_get_ops(dev);
264
265 do {
266 ssize_t written = ops->puts(dev, str, len);
267
268 if (written < 0)
269 return written;
270 str += written;
271 len -= written;
272 } while (len);
273
274 return 0;
275}
276
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900277static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass73285a22014-10-01 19:57:23 -0600278{
Sean Andersonaa22f972022-03-22 16:59:34 -0400279 struct dm_serial_ops *ops = serial_get_ops(dev);
280
281 if (!CONFIG_IS_ENABLED(SERIAL_PUTS) || !ops->puts) {
282 while (*str)
283 _serial_putc(dev, *str++);
284 return;
285 }
286
287 do {
288 const char *newline = strchrnul(str, '\n');
Sean Andersond56ce3c2022-04-04 14:17:57 -0400289 size_t len = newline - str;
Sean Andersonaa22f972022-03-22 16:59:34 -0400290
Sean Andersond56ce3c2022-04-04 14:17:57 -0400291 if (__serial_puts(dev, str, len))
292 return;
Sean Andersonaa22f972022-03-22 16:59:34 -0400293
Sean Andersond56ce3c2022-04-04 14:17:57 -0400294 if (*newline && __serial_puts(dev, "\r\n", 2))
295 return;
Sean Andersonaa22f972022-03-22 16:59:34 -0400296
Rasmus Villemoes0b491f52023-10-16 10:35:22 +0200297 if (IS_ENABLED(CONFIG_CONSOLE_FLUSH_ON_NEWLINE) && *newline)
298 _serial_flush(dev);
299
Sean Andersond56ce3c2022-04-04 14:17:57 -0400300 str += len + !!*newline;
Sean Andersonaa22f972022-03-22 16:59:34 -0400301 } while (*str);
Simon Glass73285a22014-10-01 19:57:23 -0600302}
303
Stefan Roese02072832017-08-16 17:37:16 +0200304static int __serial_getc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600305{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900306 struct dm_serial_ops *ops = serial_get_ops(dev);
307 int err;
Simon Glass247f5962014-09-04 16:27:26 -0600308
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900309 do {
310 err = ops->getc(dev);
311 if (err == -EAGAIN)
Stefan Roese80877fa2022-09-02 14:10:46 +0200312 schedule();
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900313 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600314
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900315 return err >= 0 ? err : 0;
Simon Glass247f5962014-09-04 16:27:26 -0600316}
317
Stefan Roese02072832017-08-16 17:37:16 +0200318static int __serial_tstc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600319{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900320 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600321
322 if (ops->pending)
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900323 return ops->pending(dev, true);
Simon Glass247f5962014-09-04 16:27:26 -0600324
325 return 1;
326}
327
Stefan Roese02072832017-08-16 17:37:16 +0200328#if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
329static int _serial_tstc(struct udevice *dev)
330{
331 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Rasmus Villemoese38be2d2024-10-03 16:10:27 +0200332 uint wr, avail;
Stefan Roese02072832017-08-16 17:37:16 +0200333
Rasmus Villemoesa865db72024-10-03 16:10:28 +0200334 BUILD_BUG_ON_NOT_POWER_OF_2(CONFIG_SERIAL_RX_BUFFER_SIZE);
335
Rasmus Villemoese38be2d2024-10-03 16:10:27 +0200336 /* Read all available chars into the RX buffer while there's room */
337 avail = CONFIG_SERIAL_RX_BUFFER_SIZE - (upriv->wr_ptr - upriv->rd_ptr);
338 while (avail-- && __serial_tstc(dev)) {
Rasmus Villemoes50fc1882024-10-03 16:10:26 +0200339 wr = upriv->wr_ptr++ % CONFIG_SERIAL_RX_BUFFER_SIZE;
340 upriv->buf[wr] = __serial_getc(dev);
Stefan Roese02072832017-08-16 17:37:16 +0200341 }
342
343 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
344}
345
346static int _serial_getc(struct udevice *dev)
347{
348 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
349 char val;
Rasmus Villemoes50fc1882024-10-03 16:10:26 +0200350 uint rd;
Stefan Roese02072832017-08-16 17:37:16 +0200351
Patrick Delaunay9c442822018-08-03 13:38:43 +0200352 if (upriv->rd_ptr == upriv->wr_ptr)
353 return __serial_getc(dev);
354
Rasmus Villemoes50fc1882024-10-03 16:10:26 +0200355 rd = upriv->rd_ptr++ % CONFIG_SERIAL_RX_BUFFER_SIZE;
356 val = upriv->buf[rd];
Stefan Roese02072832017-08-16 17:37:16 +0200357
358 return val;
359}
360
361#else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
362
363static int _serial_getc(struct udevice *dev)
364{
365 return __serial_getc(dev);
366}
367
368static int _serial_tstc(struct udevice *dev)
369{
370 return __serial_tstc(dev);
371}
372#endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
373
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900374void serial_putc(char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600375{
Hans de Goede04f80942015-08-08 17:45:18 +0200376 if (gd->cur_serial_dev)
377 _serial_putc(gd->cur_serial_dev, ch);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900378}
Simon Glass247f5962014-09-04 16:27:26 -0600379
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900380void serial_puts(const char *str)
381{
Hans de Goede04f80942015-08-08 17:45:18 +0200382 if (gd->cur_serial_dev)
383 _serial_puts(gd->cur_serial_dev, str);
Simon Glass247f5962014-09-04 16:27:26 -0600384}
385
Pali Rohára48a24f2022-09-05 11:31:19 +0200386#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
387void serial_flush(void)
388{
389 if (!gd->cur_serial_dev)
390 return;
391
392 _serial_flush(gd->cur_serial_dev);
393}
394#endif
395
Simon Glass73285a22014-10-01 19:57:23 -0600396int serial_getc(void)
397{
Hans de Goede04f80942015-08-08 17:45:18 +0200398 if (!gd->cur_serial_dev)
399 return 0;
400
Simon Glassa4c2e132014-11-10 18:00:20 -0700401 return _serial_getc(gd->cur_serial_dev);
Simon Glass73285a22014-10-01 19:57:23 -0600402}
403
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900404int serial_tstc(void)
405{
Hans de Goede04f80942015-08-08 17:45:18 +0200406 if (!gd->cur_serial_dev)
407 return 0;
408
Simon Glassa4c2e132014-11-10 18:00:20 -0700409 return _serial_tstc(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900410}
411
412void serial_setbrg(void)
413{
Hans de Goede04f80942015-08-08 17:45:18 +0200414 struct dm_serial_ops *ops;
415
416 if (!gd->cur_serial_dev)
417 return;
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900418
Hans de Goede04f80942015-08-08 17:45:18 +0200419 ops = serial_get_ops(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900420 if (ops->setbrg)
Simon Glassa4c2e132014-11-10 18:00:20 -0700421 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900422}
423
Simon Glassdaaff932018-12-28 14:23:08 -0700424int serial_getconfig(struct udevice *dev, uint *config)
Andy Shevchenko08e98792018-11-20 23:52:32 +0200425{
426 struct dm_serial_ops *ops;
427
Simon Glassdaaff932018-12-28 14:23:08 -0700428 ops = serial_get_ops(dev);
Andy Shevchenko08e98792018-11-20 23:52:32 +0200429 if (ops->getconfig)
Simon Glassdaaff932018-12-28 14:23:08 -0700430 return ops->getconfig(dev, config);
Andy Shevchenko08e98792018-11-20 23:52:32 +0200431
432 return 0;
433}
434
Simon Glassde62d482018-12-28 14:23:09 -0700435int serial_setconfig(struct udevice *dev, uint config)
Patrice Chotard46c1d9f2018-08-03 15:07:38 +0200436{
437 struct dm_serial_ops *ops;
438
Simon Glassde62d482018-12-28 14:23:09 -0700439 ops = serial_get_ops(dev);
Patrice Chotard46c1d9f2018-08-03 15:07:38 +0200440 if (ops->setconfig)
Simon Glassde62d482018-12-28 14:23:09 -0700441 return ops->setconfig(dev, config);
Patrice Chotard46c1d9f2018-08-03 15:07:38 +0200442
443 return 0;
444}
445
Simon Glass896c1642018-12-28 14:23:10 -0700446int serial_getinfo(struct udevice *dev, struct serial_device_info *info)
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200447{
448 struct dm_serial_ops *ops;
449
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200450 if (!info)
451 return -EINVAL;
452
453 info->baudrate = gd->baudrate;
454
Simon Glass896c1642018-12-28 14:23:10 -0700455 ops = serial_get_ops(dev);
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200456 if (ops->getinfo)
Simon Glass896c1642018-12-28 14:23:10 -0700457 return ops->getinfo(dev, info);
Andy Shevchenko44f21da2018-11-20 23:52:33 +0200458
459 return -EINVAL;
460}
461
Simon Glass247f5962014-09-04 16:27:26 -0600462void serial_stdio_init(void)
463{
464}
465
Tom Rini952cc382022-12-04 10:14:13 -0500466#if CONFIG_IS_ENABLED(DM_STDIO)
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900467
468#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass73285a22014-10-01 19:57:23 -0600469static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600470{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900471 _serial_putc(sdev->priv, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600472}
473
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900474static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
Simon Glass247f5962014-09-04 16:27:26 -0600475{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900476 _serial_puts(sdev->priv, str);
Simon Glass247f5962014-09-04 16:27:26 -0600477}
478
Pali Rohár9c9ceca2022-09-05 11:31:18 +0200479#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
480static void serial_stub_flush(struct stdio_dev *sdev)
481{
482 _serial_flush(sdev->priv);
483}
484#endif
485
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900486static int serial_stub_getc(struct stdio_dev *sdev)
Simon Glass247f5962014-09-04 16:27:26 -0600487{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900488 return _serial_getc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600489}
490
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900491static int serial_stub_tstc(struct stdio_dev *sdev)
Simon Glass247f5962014-09-04 16:27:26 -0600492{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900493 return _serial_tstc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600494}
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900495#endif
Heiko Schocher6fe2dd52022-02-02 13:55:19 +0100496#endif
Simon Glass247f5962014-09-04 16:27:26 -0600497
Simon Glass593f50b2014-10-29 13:09:03 -0600498/**
499 * on_baudrate() - Update the actual baudrate when the env var changes
500 *
501 * This will check for a valid baudrate and only apply it if valid.
502 */
503static int on_baudrate(const char *name, const char *value, enum env_op op,
504 int flags)
505{
506 int i;
507 int baudrate;
508
509 switch (op) {
510 case env_op_create:
511 case env_op_overwrite:
512 /*
513 * Switch to new baudrate if new baudrate is supported
514 */
Simon Glassff9b9032021-07-24 09:03:30 -0600515 baudrate = dectoul(value, NULL);
Simon Glass593f50b2014-10-29 13:09:03 -0600516
517 /* Not actually changing */
518 if (gd->baudrate == baudrate)
519 return 0;
520
521 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
522 if (baudrate == baudrate_table[i])
523 break;
524 }
525 if (i == ARRAY_SIZE(baudrate_table)) {
526 if ((flags & H_FORCE) == 0)
527 printf("## Baudrate %d bps not supported\n",
528 baudrate);
529 return 1;
530 }
531 if ((flags & H_INTERACTIVE) != 0) {
532 printf("## Switch baudrate to %d bps and press ENTER ...\n",
533 baudrate);
534 udelay(50000);
Pali Rohár8e67a8a2022-09-05 11:31:20 +0200535 flush();
Simon Glass593f50b2014-10-29 13:09:03 -0600536 }
537
538 gd->baudrate = baudrate;
539
540 serial_setbrg();
541
542 udelay(50000);
543
544 if ((flags & H_INTERACTIVE) != 0)
545 while (1) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200546 if (getchar() == '\r')
Simon Glass593f50b2014-10-29 13:09:03 -0600547 break;
548 }
549
550 return 0;
551 case env_op_delete:
552 printf("## Baudrate may not be deleted\n");
553 return 1;
554 default:
555 return 0;
556 }
557}
558U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Simon Glass247f5962014-09-04 16:27:26 -0600559
Simon Glass30a13492015-12-13 21:36:58 -0700560#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Simon Glass247f5962014-09-04 16:27:26 -0600561static int serial_post_probe(struct udevice *dev)
562{
Simon Glass247f5962014-09-04 16:27:26 -0600563 struct dm_serial_ops *ops = serial_get_ops(dev);
Tom Rini952cc382022-12-04 10:14:13 -0500564#if CONFIG_IS_ENABLED(DM_STDIO)
Simon Glassde0977b2015-03-05 12:25:20 -0700565 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700566 struct stdio_dev sdev;
567#endif
Simon Glass247f5962014-09-04 16:27:26 -0600568 int ret;
569
570 /* Set the baud rate */
571 if (ops->setbrg) {
572 ret = ops->setbrg(dev, gd->baudrate);
573 if (ret)
574 return ret;
575 }
576
Tom Rini952cc382022-12-04 10:14:13 -0500577#if CONFIG_IS_ENABLED(DM_STDIO)
Simon Glass247f5962014-09-04 16:27:26 -0600578 if (!(gd->flags & GD_FLG_RELOC))
579 return 0;
Simon Glass247f5962014-09-04 16:27:26 -0600580 memset(&sdev, '\0', sizeof(sdev));
581
Heinrich Schuchardt711ad632023-09-29 02:47:16 +0200582 strlcpy(sdev.name, dev->name, sizeof(sdev.name));
Simon Glass10a7fe92017-07-27 09:31:04 -0600583 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
Simon Glass247f5962014-09-04 16:27:26 -0600584 sdev.priv = dev;
585 sdev.putc = serial_stub_putc;
586 sdev.puts = serial_stub_puts;
Pali Rohár9c9ceca2022-09-05 11:31:18 +0200587 STDIO_DEV_ASSIGN_FLUSH(&sdev, serial_stub_flush);
Simon Glass247f5962014-09-04 16:27:26 -0600588 sdev.getc = serial_stub_getc;
589 sdev.tstc = serial_stub_tstc;
Stefan Roese02072832017-08-16 17:37:16 +0200590
Simon Glass247f5962014-09-04 16:27:26 -0600591 stdio_register_dev(&sdev, &upriv->sdev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700592#endif
Simon Glass247f5962014-09-04 16:27:26 -0600593 return 0;
594}
595
596static int serial_pre_remove(struct udevice *dev)
597{
Simon Glass3110ece2016-10-17 20:13:02 -0600598#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
Simon Glassde0977b2015-03-05 12:25:20 -0700599 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600600
Stefan Roesedcc4b072017-04-24 09:48:01 +0200601 if (stdio_deregister_dev(upriv->sdev, true))
Simon Glass247f5962014-09-04 16:27:26 -0600602 return -EPERM;
603#endif
604
605 return 0;
606}
607
608UCLASS_DRIVER(serial) = {
609 .id = UCLASS_SERIAL,
610 .name = "serial",
Simon Glass0ccb0972015-01-25 08:27:05 -0700611 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass247f5962014-09-04 16:27:26 -0600612 .post_probe = serial_post_probe,
613 .pre_remove = serial_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700614 .per_device_auto = sizeof(struct serial_dev_priv),
Simon Glass247f5962014-09-04 16:27:26 -0600615};
Simon Glass30a13492015-12-13 21:36:58 -0700616#endif