blob: da017dc5b3e3d8bed0263d1c8b753c47e2520196 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7ac16102004-08-01 22:48:16 +00002/*
3 * (C) Copyright 2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk7ac16102004-08-01 22:48:16 +00005 */
6
7#include <common.h>
Simon Glass9d1f6192019-08-02 09:44:25 -06008#include <env_internal.h>
Simon Glassf11478f2019-12-28 10:45:07 -07009#include <hang.h>
wdenk7ac16102004-08-01 22:48:16 +000010#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020011#include <stdio_dev.h>
Mike Frysinger078f2f12011-05-14 06:56:15 +000012#include <post.h>
13#include <linux/compiler.h>
Marek Vasut0e1b5772012-10-06 14:07:03 +000014#include <errno.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
wdenk7ac16102004-08-01 22:48:16 +000016
Wolfgang Denk6405a152006-03-31 18:32:53 +020017DECLARE_GLOBAL_DATA_PTR;
18
Gerlando Falauto34148d62011-11-18 06:49:11 +000019static struct serial_device *serial_devices;
20static struct serial_device *serial_current;
Joe Hershberger3928bb62012-12-11 22:16:27 -060021/*
22 * Table with supported baudrates (defined in config_xyz.h)
23 */
24static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
wdenk7ac16102004-08-01 22:48:16 +000025
Marek Vasut95656ea2012-10-08 11:36:39 +000026/**
27 * serial_null() - Void registration routine of a serial driver
28 *
29 * This routine implements a void registration routine of a serial
30 * driver. The registration routine of a particular driver is aliased
31 * to this empty function in case the driver is not compiled into
32 * U-Boot.
33 */
Marek Vasutf6af0482012-09-12 17:49:58 +020034static void serial_null(void)
35{
Joe Hershberger3928bb62012-12-11 22:16:27 -060036}
37
38/**
39 * on_baudrate() - Update the actual baudrate when the env var changes
40 *
Heinrich Schuchardt995ea732018-07-29 10:41:02 +020041 * @name: changed environment variable
42 * @value: new value of the environment variable
43 * @op: operation (create, overwrite, or delete)
44 * @flags: attributes of environment variable change,
45 * see flags H_* in include/search.h
46 *
Joe Hershberger3928bb62012-12-11 22:16:27 -060047 * This will check for a valid baudrate and only apply it if valid.
Heinrich Schuchardt995ea732018-07-29 10:41:02 +020048 *
49 * Return: 0 on success, 1 on error
Joe Hershberger3928bb62012-12-11 22:16:27 -060050 */
51static int on_baudrate(const char *name, const char *value, enum env_op op,
52 int flags)
53{
54 int i;
55 int baudrate;
56
57 switch (op) {
58 case env_op_create:
59 case env_op_overwrite:
60 /*
61 * Switch to new baudrate if new baudrate is supported
62 */
63 baudrate = simple_strtoul(value, NULL, 10);
64
65 /* Not actually changing */
66 if (gd->baudrate == baudrate)
67 return 0;
68
Axel Linebca8732013-06-23 00:46:41 +080069 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
Joe Hershberger3928bb62012-12-11 22:16:27 -060070 if (baudrate == baudrate_table[i])
71 break;
72 }
Axel Linebca8732013-06-23 00:46:41 +080073 if (i == ARRAY_SIZE(baudrate_table)) {
Joe Hershberger3928bb62012-12-11 22:16:27 -060074 if ((flags & H_FORCE) == 0)
75 printf("## Baudrate %d bps not supported\n",
76 baudrate);
77 return 1;
78 }
79 if ((flags & H_INTERACTIVE) != 0) {
80 printf("## Switch baudrate to %d"
81 " bps and press ENTER ...\n", baudrate);
82 udelay(50000);
83 }
84
85 gd->baudrate = baudrate;
Joe Hershberger3928bb62012-12-11 22:16:27 -060086
87 serial_setbrg();
88
89 udelay(50000);
90
91 if ((flags & H_INTERACTIVE) != 0)
92 while (1) {
93 if (getc() == '\r')
94 break;
95 }
96
97 return 0;
98 case env_op_delete:
99 printf("## Baudrate may not be deleted\n");
100 return 1;
101 default:
102 return 0;
103 }
Marek Vasutf6af0482012-09-12 17:49:58 +0200104}
Joe Hershberger3928bb62012-12-11 22:16:27 -0600105U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Marek Vasutf6af0482012-09-12 17:49:58 +0200106
Marek Vasut95656ea2012-10-08 11:36:39 +0000107/**
108 * serial_initfunc() - Forward declare of driver registration routine
109 * @name: Name of the real driver registration routine.
110 *
111 * This macro expands onto forward declaration of a driver registration
112 * routine, which is then used below in serial_initialize() function.
113 * The declaration is made weak and aliases to serial_null() so in case
114 * the driver is not compiled in, the function is still declared and can
115 * be used, but aliases to serial_null() and thus is optimized away.
116 */
Marek Vasutf6af0482012-09-12 17:49:58 +0200117#define serial_initfunc(name) \
118 void name(void) \
119 __attribute__((weak, alias("serial_null")));
120
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100121serial_initfunc(atmel_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100122serial_initfunc(mcf_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100123serial_initfunc(mpc85xx_serial_initialize);
Marek Vasut64c60552012-09-14 22:37:43 +0200124serial_initfunc(mxc_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100125serial_initfunc(ns16550_serial_initialize);
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200126serial_initfunc(pl01x_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100127serial_initfunc(pxa_serial_initialize);
Marek Vasut904d3d72012-09-14 22:40:08 +0200128serial_initfunc(sh_serial_initialize);
developer77c7c732019-09-25 17:45:18 +0800129serial_initfunc(mtk_serial_initialize);
Marek Vasut419d8942012-09-12 13:50:56 +0200130
Marek Vasut95656ea2012-10-08 11:36:39 +0000131/**
132 * serial_register() - Register serial driver with serial driver core
133 * @dev: Pointer to the serial driver structure
134 *
135 * This function registers the serial driver supplied via @dev with
136 * serial driver core, thus making U-Boot aware of it and making it
137 * available for U-Boot to use. On platforms that still require manual
138 * relocation of constant variables, relocation of the supplied structure
139 * is performed.
140 */
Mike Frysingerffadc822011-04-29 18:03:30 +0000141void serial_register(struct serial_device *dev)
wdenk7ac16102004-08-01 22:48:16 +0000142{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200143#ifdef CONFIG_NEEDS_MANUAL_RELOC
Marek Vasut2206b342012-09-16 18:54:22 +0200144 if (dev->start)
145 dev->start += gd->reloc_off;
146 if (dev->stop)
147 dev->stop += gd->reloc_off;
148 if (dev->setbrg)
149 dev->setbrg += gd->reloc_off;
150 if (dev->getc)
151 dev->getc += gd->reloc_off;
152 if (dev->tstc)
153 dev->tstc += gd->reloc_off;
154 if (dev->putc)
155 dev->putc += gd->reloc_off;
156 if (dev->puts)
157 dev->puts += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500158#endif
wdenk7ac16102004-08-01 22:48:16 +0000159
160 dev->next = serial_devices;
161 serial_devices = dev;
wdenk7ac16102004-08-01 22:48:16 +0000162}
163
Marek Vasut95656ea2012-10-08 11:36:39 +0000164/**
165 * serial_initialize() - Register all compiled-in serial port drivers
166 *
167 * This function registers all serial port drivers that are compiled
168 * into the U-Boot binary with the serial core, thus making them
169 * available to U-Boot to use. Lastly, this function assigns a default
170 * serial port to the serial core. That serial port is then used as a
171 * default output.
172 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000173void serial_initialize(void)
wdenk7ac16102004-08-01 22:48:16 +0000174{
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100175 atmel_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100176 mcf_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100177 mpc85xx_serial_initialize();
Marek Vasut64c60552012-09-14 22:37:43 +0200178 mxc_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100179 ns16550_serial_initialize();
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200180 pl01x_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100181 pxa_serial_initialize();
Marek Vasut904d3d72012-09-14 22:40:08 +0200182 sh_serial_initialize();
developer77c7c732019-09-25 17:45:18 +0800183 mtk_serial_initialize();
Marek Vasut01f9ea82012-09-13 01:16:50 +0200184
Gerlando Falauto34148d62011-11-18 06:49:11 +0000185 serial_assign(default_serial_console()->name);
wdenk7ac16102004-08-01 22:48:16 +0000186}
187
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200188static int serial_stub_start(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600189{
190 struct serial_device *dev = sdev->priv;
191
192 return dev->start();
193}
194
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200195static int serial_stub_stop(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600196{
197 struct serial_device *dev = sdev->priv;
198
199 return dev->stop();
200}
201
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200202static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600203{
204 struct serial_device *dev = sdev->priv;
205
206 dev->putc(ch);
207}
208
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200209static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600210{
211 struct serial_device *dev = sdev->priv;
212
213 dev->puts(str);
214}
215
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900216static int serial_stub_getc(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600217{
218 struct serial_device *dev = sdev->priv;
219
220 return dev->getc();
221}
222
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900223static int serial_stub_tstc(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600224{
225 struct serial_device *dev = sdev->priv;
226
227 return dev->tstc();
228}
229
Marek Vasut95656ea2012-10-08 11:36:39 +0000230/**
231 * serial_stdio_init() - Register serial ports with STDIO core
232 *
233 * This function generates a proxy driver for each serial port driver.
234 * These proxy drivers then register with the STDIO core, making the
235 * serial drivers available as STDIO devices.
236 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000237void serial_stdio_init(void)
wdenk7ac16102004-08-01 22:48:16 +0000238{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200239 struct stdio_dev dev;
wdenk7ac16102004-08-01 22:48:16 +0000240 struct serial_device *s = serial_devices;
241
wdenk8f08c5b2004-10-11 22:43:02 +0000242 while (s) {
Gerlando Falauto34148d62011-11-18 06:49:11 +0000243 memset(&dev, 0, sizeof(dev));
wdenk7ac16102004-08-01 22:48:16 +0000244
Gerlando Falauto34148d62011-11-18 06:49:11 +0000245 strcpy(dev.name, s->name);
wdenk7ac16102004-08-01 22:48:16 +0000246 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
247
Simon Glass0d1e1f72014-07-23 06:54:59 -0600248 dev.start = serial_stub_start;
249 dev.stop = serial_stub_stop;
250 dev.putc = serial_stub_putc;
251 dev.puts = serial_stub_puts;
252 dev.getc = serial_stub_getc;
253 dev.tstc = serial_stub_tstc;
Simon Glassac2a14f2014-09-04 16:27:23 -0600254 dev.priv = s;
wdenk7ac16102004-08-01 22:48:16 +0000255
Gerlando Falauto34148d62011-11-18 06:49:11 +0000256 stdio_register(&dev);
wdenk7ac16102004-08-01 22:48:16 +0000257
258 s = s->next;
259 }
260}
261
Marek Vasut95656ea2012-10-08 11:36:39 +0000262/**
263 * serial_assign() - Select the serial output device by name
264 * @name: Name of the serial driver to be used as default output
265 *
266 * This function configures the serial output multiplexing by
267 * selecting which serial device will be used as default. In case
268 * the STDIO "serial" device is selected as stdin/stdout/stderr,
269 * the serial device previously configured by this function will be
270 * used for the particular operation.
271 *
272 * Returns 0 on success, negative on error.
273 */
Gerlando Falauto92999582011-11-18 06:49:12 +0000274int serial_assign(const char *name)
wdenk7ac16102004-08-01 22:48:16 +0000275{
276 struct serial_device *s;
277
wdenk8f08c5b2004-10-11 22:43:02 +0000278 for (s = serial_devices; s; s = s->next) {
Marek Vasut0e1b5772012-10-06 14:07:03 +0000279 if (strcmp(s->name, name))
280 continue;
281 serial_current = s;
282 return 0;
wdenk7ac16102004-08-01 22:48:16 +0000283 }
284
Marek Vasut0e1b5772012-10-06 14:07:03 +0000285 return -EINVAL;
wdenk7ac16102004-08-01 22:48:16 +0000286}
287
Marek Vasut95656ea2012-10-08 11:36:39 +0000288/**
289 * serial_reinit_all() - Reinitialize all compiled-in serial ports
290 *
291 * This function reinitializes all serial ports that are compiled
292 * into U-Boot by calling their serial_start() functions.
293 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000294void serial_reinit_all(void)
wdenk7ac16102004-08-01 22:48:16 +0000295{
296 struct serial_device *s;
297
Gerlando Falauto34148d62011-11-18 06:49:11 +0000298 for (s = serial_devices; s; s = s->next)
Marek Vasutb46931d2012-09-07 14:35:31 +0200299 s->start();
wdenk7ac16102004-08-01 22:48:16 +0000300}
301
Marek Vasut95656ea2012-10-08 11:36:39 +0000302/**
303 * get_current() - Return pointer to currently selected serial port
304 *
305 * This function returns a pointer to currently selected serial port.
306 * The currently selected serial port is altered by serial_assign()
307 * function.
308 *
309 * In case this function is called before relocation or before any serial
310 * port is configured, this function calls default_serial_console() to
311 * determine the serial port. Otherwise, the configured serial port is
312 * returned.
313 *
314 * Returns pointer to the currently selected serial port on success,
315 * NULL on error.
316 */
Simon Glass8f4e6f12011-08-19 11:09:43 +0000317static struct serial_device *get_current(void)
wdenk7ac16102004-08-01 22:48:16 +0000318{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000319 struct serial_device *dev;
320
Marek Vasutbdad8072012-10-06 14:07:04 +0000321 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass8f4e6f12011-08-19 11:09:43 +0000322 dev = default_serial_console();
Marek Vasutbdad8072012-10-06 14:07:04 +0000323 else if (!serial_current)
324 dev = default_serial_console();
325 else
326 dev = serial_current;
wdenk8f08c5b2004-10-11 22:43:02 +0000327
Marek Vasutbdad8072012-10-06 14:07:04 +0000328 /* We must have a console device */
329 if (!dev) {
Marek Vasuta6f63342012-09-15 10:33:51 +0200330#ifdef CONFIG_SPL_BUILD
Marek Vasutbdad8072012-10-06 14:07:04 +0000331 puts("Cannot find console\n");
332 hang();
Marek Vasuta6f63342012-09-15 10:33:51 +0200333#else
Marek Vasutbdad8072012-10-06 14:07:04 +0000334 panic("Cannot find console\n");
Marek Vasuta6f63342012-09-15 10:33:51 +0200335#endif
Marek Vasutbdad8072012-10-06 14:07:04 +0000336 }
337
Simon Glass8f4e6f12011-08-19 11:09:43 +0000338 return dev;
339}
wdenk7ac16102004-08-01 22:48:16 +0000340
Marek Vasut95656ea2012-10-08 11:36:39 +0000341/**
342 * serial_init() - Initialize currently selected serial port
343 *
344 * This function initializes the currently selected serial port. This
345 * usually involves setting up the registers of that particular port,
346 * enabling clock and such. This function uses the get_current() call
347 * to determine which port is selected.
348 *
349 * Returns 0 on success, negative on error.
350 */
Simon Glass8f4e6f12011-08-19 11:09:43 +0000351int serial_init(void)
352{
Simon Glassb9d5f6b2014-07-23 06:55:07 -0600353 gd->flags |= GD_FLG_SERIAL_READY;
Marek Vasutb46931d2012-09-07 14:35:31 +0200354 return get_current()->start();
wdenk7ac16102004-08-01 22:48:16 +0000355}
356
Marek Vasut95656ea2012-10-08 11:36:39 +0000357/**
358 * serial_setbrg() - Configure baud-rate of currently selected serial port
359 *
360 * This function configures the baud-rate of the currently selected
361 * serial port. The baud-rate is retrieved from global data within
362 * the serial port driver. This function uses the get_current() call
363 * to determine which port is selected.
364 *
365 * Returns 0 on success, negative on error.
366 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000367void serial_setbrg(void)
wdenk7ac16102004-08-01 22:48:16 +0000368{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000369 get_current()->setbrg();
wdenk7ac16102004-08-01 22:48:16 +0000370}
371
Marek Vasut95656ea2012-10-08 11:36:39 +0000372/**
373 * serial_getc() - Read character from currently selected serial port
374 *
375 * This function retrieves a character from currently selected serial
376 * port. In case there is no character waiting on the serial port,
377 * this function will block and wait for the character to appear. This
378 * function uses the get_current() call to determine which port is
379 * selected.
380 *
381 * Returns the character on success, negative on error.
382 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000383int serial_getc(void)
wdenk7ac16102004-08-01 22:48:16 +0000384{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000385 return get_current()->getc();
wdenk7ac16102004-08-01 22:48:16 +0000386}
387
Marek Vasut95656ea2012-10-08 11:36:39 +0000388/**
389 * serial_tstc() - Test if data is available on currently selected serial port
390 *
391 * This function tests if one or more characters are available on
392 * currently selected serial port. This function never blocks. This
393 * function uses the get_current() call to determine which port is
394 * selected.
395 *
396 * Returns positive if character is available, zero otherwise.
397 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000398int serial_tstc(void)
wdenk7ac16102004-08-01 22:48:16 +0000399{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000400 return get_current()->tstc();
wdenk7ac16102004-08-01 22:48:16 +0000401}
402
Marek Vasut95656ea2012-10-08 11:36:39 +0000403/**
404 * serial_putc() - Output character via currently selected serial port
405 * @c: Single character to be output from the serial port.
406 *
407 * This function outputs a character via currently selected serial
408 * port. This character is passed to the serial port driver responsible
409 * for controlling the hardware. The hardware may still be in process
410 * of transmitting another character, therefore this function may block
411 * for a short amount of time. This function uses the get_current()
412 * call to determine which port is selected.
413 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000414void serial_putc(const char c)
wdenk7ac16102004-08-01 22:48:16 +0000415{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000416 get_current()->putc(c);
wdenk7ac16102004-08-01 22:48:16 +0000417}
418
Marek Vasut95656ea2012-10-08 11:36:39 +0000419/**
420 * serial_puts() - Output string via currently selected serial port
421 * @s: Zero-terminated string to be output from the serial port.
422 *
423 * This function outputs a zero-terminated string via currently
424 * selected serial port. This function behaves as an accelerator
425 * in case the hardware can queue multiple characters for transfer.
426 * The whole string that is to be output is available to the function
427 * implementing the hardware manipulation. Transmitting the whole
428 * string may take some time, thus this function may block for some
429 * amount of time. This function uses the get_current() call to
430 * determine which port is selected.
431 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000432void serial_puts(const char *s)
wdenk7ac16102004-08-01 22:48:16 +0000433{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000434 get_current()->puts(s);
wdenk7ac16102004-08-01 22:48:16 +0000435}
Mike Frysinger078f2f12011-05-14 06:56:15 +0000436
Marek Vasut95656ea2012-10-08 11:36:39 +0000437/**
438 * default_serial_puts() - Output string by calling serial_putc() in loop
439 * @s: Zero-terminated string to be output from the serial port.
440 *
441 * This function outputs a zero-terminated string by calling serial_putc()
442 * in a loop. Most drivers do not support queueing more than one byte for
443 * transfer, thus this function precisely implements their serial_puts().
444 *
445 * To optimize the number of get_current() calls, this function only
446 * calls get_current() once and then directly accesses the putc() call
447 * of the &struct serial_device .
448 */
Marek Vasut6b9124e2012-10-06 14:07:01 +0000449void default_serial_puts(const char *s)
450{
451 struct serial_device *dev = get_current();
452 while (*s)
453 dev->putc(*s++);
454}
455
Mike Frysinger078f2f12011-05-14 06:56:15 +0000456#if CONFIG_POST & CONFIG_SYS_POST_UART
457static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
458
Marek Vasut95656ea2012-10-08 11:36:39 +0000459/**
460 * uart_post_test() - Test the currently selected serial port using POST
461 * @flags: POST framework flags
462 *
463 * Do a loopback test of the currently selected serial port. This
464 * function is only useful in the context of the POST testing framwork.
Vagrant Cascadianbeb288b2015-11-24 14:46:24 -0800465 * The serial port is first configured into loopback mode and then
Marek Vasut95656ea2012-10-08 11:36:39 +0000466 * characters are sent through it.
467 *
468 * Returns 0 on success, value otherwise.
469 */
Mike Frysinger078f2f12011-05-14 06:56:15 +0000470/* Mark weak until post/cpu/.../uart.c migrate over */
471__weak
472int uart_post_test(int flags)
473{
474 unsigned char c;
475 int ret, saved_baud, b;
476 struct serial_device *saved_dev, *s;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000477
478 /* Save current serial state */
479 ret = 0;
480 saved_dev = serial_current;
Masahiro Yamada197c7202014-04-04 20:09:58 +0900481 saved_baud = gd->baudrate;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000482
483 for (s = serial_devices; s; s = s->next) {
484 /* If this driver doesn't support loop back, skip it */
485 if (!s->loop)
486 continue;
487
488 /* Test the next device */
489 serial_current = s;
490
491 ret = serial_init();
492 if (ret)
493 goto done;
494
495 /* Consume anything that happens to be queued */
496 while (serial_tstc())
497 serial_getc();
498
499 /* Enable loop back */
500 s->loop(1);
501
502 /* Test every available baud rate */
503 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
Masahiro Yamada197c7202014-04-04 20:09:58 +0900504 gd->baudrate = bauds[b];
Mike Frysinger078f2f12011-05-14 06:56:15 +0000505 serial_setbrg();
506
507 /*
508 * Stick to printable chars to avoid issues:
509 * - terminal corruption
510 * - serial program reacting to sequences and sending
511 * back random extra data
512 * - most serial drivers add in extra chars (like \r\n)
513 */
514 for (c = 0x20; c < 0x7f; ++c) {
515 /* Send it out */
516 serial_putc(c);
517
518 /* Make sure it's the same one */
519 ret = (c != serial_getc());
520 if (ret) {
521 s->loop(0);
522 goto done;
523 }
524
525 /* Clean up the output in case it was sent */
526 serial_putc('\b');
527 ret = ('\b' != serial_getc());
528 if (ret) {
529 s->loop(0);
530 goto done;
531 }
532 }
533 }
534
535 /* Disable loop back */
536 s->loop(0);
537
Marek Vasutb46931d2012-09-07 14:35:31 +0200538 /* XXX: There is no serial_stop() !? */
539 if (s->stop)
540 s->stop();
Mike Frysinger078f2f12011-05-14 06:56:15 +0000541 }
542
543 done:
544 /* Restore previous serial state */
545 serial_current = saved_dev;
Masahiro Yamada197c7202014-04-04 20:09:58 +0900546 gd->baudrate = saved_baud;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000547 serial_reinit_all();
548 serial_setbrg();
549
550 return ret;
551}
552#endif