blob: b907508dbe5d75c0d29f79871b911ee5402221a0 [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>
wdenk7ac16102004-08-01 22:48:16 +00009#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020010#include <stdio_dev.h>
Mike Frysinger078f2f12011-05-14 06:56:15 +000011#include <post.h>
12#include <linux/compiler.h>
Marek Vasut0e1b5772012-10-06 14:07:03 +000013#include <errno.h>
wdenk7ac16102004-08-01 22:48:16 +000014
Wolfgang Denk6405a152006-03-31 18:32:53 +020015DECLARE_GLOBAL_DATA_PTR;
16
Gerlando Falauto34148d62011-11-18 06:49:11 +000017static struct serial_device *serial_devices;
18static struct serial_device *serial_current;
Joe Hershberger3928bb62012-12-11 22:16:27 -060019/*
20 * Table with supported baudrates (defined in config_xyz.h)
21 */
22static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
wdenk7ac16102004-08-01 22:48:16 +000023
Marek Vasut95656ea2012-10-08 11:36:39 +000024/**
25 * serial_null() - Void registration routine of a serial driver
26 *
27 * This routine implements a void registration routine of a serial
28 * driver. The registration routine of a particular driver is aliased
29 * to this empty function in case the driver is not compiled into
30 * U-Boot.
31 */
Marek Vasutf6af0482012-09-12 17:49:58 +020032static void serial_null(void)
33{
Joe Hershberger3928bb62012-12-11 22:16:27 -060034}
35
36/**
37 * on_baudrate() - Update the actual baudrate when the env var changes
38 *
Heinrich Schuchardt995ea732018-07-29 10:41:02 +020039 * @name: changed environment variable
40 * @value: new value of the environment variable
41 * @op: operation (create, overwrite, or delete)
42 * @flags: attributes of environment variable change,
43 * see flags H_* in include/search.h
44 *
Joe Hershberger3928bb62012-12-11 22:16:27 -060045 * This will check for a valid baudrate and only apply it if valid.
Heinrich Schuchardt995ea732018-07-29 10:41:02 +020046 *
47 * Return: 0 on success, 1 on error
Joe Hershberger3928bb62012-12-11 22:16:27 -060048 */
49static int on_baudrate(const char *name, const char *value, enum env_op op,
50 int flags)
51{
52 int i;
53 int baudrate;
54
55 switch (op) {
56 case env_op_create:
57 case env_op_overwrite:
58 /*
59 * Switch to new baudrate if new baudrate is supported
60 */
61 baudrate = simple_strtoul(value, NULL, 10);
62
63 /* Not actually changing */
64 if (gd->baudrate == baudrate)
65 return 0;
66
Axel Linebca8732013-06-23 00:46:41 +080067 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
Joe Hershberger3928bb62012-12-11 22:16:27 -060068 if (baudrate == baudrate_table[i])
69 break;
70 }
Axel Linebca8732013-06-23 00:46:41 +080071 if (i == ARRAY_SIZE(baudrate_table)) {
Joe Hershberger3928bb62012-12-11 22:16:27 -060072 if ((flags & H_FORCE) == 0)
73 printf("## Baudrate %d bps not supported\n",
74 baudrate);
75 return 1;
76 }
77 if ((flags & H_INTERACTIVE) != 0) {
78 printf("## Switch baudrate to %d"
79 " bps and press ENTER ...\n", baudrate);
80 udelay(50000);
81 }
82
83 gd->baudrate = baudrate;
Joe Hershberger3928bb62012-12-11 22:16:27 -060084
85 serial_setbrg();
86
87 udelay(50000);
88
89 if ((flags & H_INTERACTIVE) != 0)
90 while (1) {
91 if (getc() == '\r')
92 break;
93 }
94
95 return 0;
96 case env_op_delete:
97 printf("## Baudrate may not be deleted\n");
98 return 1;
99 default:
100 return 0;
101 }
Marek Vasutf6af0482012-09-12 17:49:58 +0200102}
Joe Hershberger3928bb62012-12-11 22:16:27 -0600103U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Marek Vasutf6af0482012-09-12 17:49:58 +0200104
Marek Vasut95656ea2012-10-08 11:36:39 +0000105/**
106 * serial_initfunc() - Forward declare of driver registration routine
107 * @name: Name of the real driver registration routine.
108 *
109 * This macro expands onto forward declaration of a driver registration
110 * routine, which is then used below in serial_initialize() function.
111 * The declaration is made weak and aliases to serial_null() so in case
112 * the driver is not compiled in, the function is still declared and can
113 * be used, but aliases to serial_null() and thus is optimized away.
114 */
Marek Vasutf6af0482012-09-12 17:49:58 +0200115#define serial_initfunc(name) \
116 void name(void) \
117 __attribute__((weak, alias("serial_null")));
118
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100119serial_initfunc(atmel_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100120serial_initfunc(mcf_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100121serial_initfunc(mpc85xx_serial_initialize);
Marek Vasut64c60552012-09-14 22:37:43 +0200122serial_initfunc(mxc_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100123serial_initfunc(ns16550_serial_initialize);
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200124serial_initfunc(pl01x_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100125serial_initfunc(pxa_serial_initialize);
Marek Vasut904d3d72012-09-14 22:40:08 +0200126serial_initfunc(sh_serial_initialize);
Marek Vasut419d8942012-09-12 13:50:56 +0200127
Marek Vasut95656ea2012-10-08 11:36:39 +0000128/**
129 * serial_register() - Register serial driver with serial driver core
130 * @dev: Pointer to the serial driver structure
131 *
132 * This function registers the serial driver supplied via @dev with
133 * serial driver core, thus making U-Boot aware of it and making it
134 * available for U-Boot to use. On platforms that still require manual
135 * relocation of constant variables, relocation of the supplied structure
136 * is performed.
137 */
Mike Frysingerffadc822011-04-29 18:03:30 +0000138void serial_register(struct serial_device *dev)
wdenk7ac16102004-08-01 22:48:16 +0000139{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200140#ifdef CONFIG_NEEDS_MANUAL_RELOC
Marek Vasut2206b342012-09-16 18:54:22 +0200141 if (dev->start)
142 dev->start += gd->reloc_off;
143 if (dev->stop)
144 dev->stop += gd->reloc_off;
145 if (dev->setbrg)
146 dev->setbrg += gd->reloc_off;
147 if (dev->getc)
148 dev->getc += gd->reloc_off;
149 if (dev->tstc)
150 dev->tstc += gd->reloc_off;
151 if (dev->putc)
152 dev->putc += gd->reloc_off;
153 if (dev->puts)
154 dev->puts += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500155#endif
wdenk7ac16102004-08-01 22:48:16 +0000156
157 dev->next = serial_devices;
158 serial_devices = dev;
wdenk7ac16102004-08-01 22:48:16 +0000159}
160
Marek Vasut95656ea2012-10-08 11:36:39 +0000161/**
162 * serial_initialize() - Register all compiled-in serial port drivers
163 *
164 * This function registers all serial port drivers that are compiled
165 * into the U-Boot binary with the serial core, thus making them
166 * available to U-Boot to use. Lastly, this function assigns a default
167 * serial port to the serial core. That serial port is then used as a
168 * default output.
169 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000170void serial_initialize(void)
wdenk7ac16102004-08-01 22:48:16 +0000171{
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100172 atmel_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100173 mcf_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100174 mpc85xx_serial_initialize();
Marek Vasut64c60552012-09-14 22:37:43 +0200175 mxc_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100176 ns16550_serial_initialize();
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200177 pl01x_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100178 pxa_serial_initialize();
Marek Vasut904d3d72012-09-14 22:40:08 +0200179 sh_serial_initialize();
Marek Vasut01f9ea82012-09-13 01:16:50 +0200180
Gerlando Falauto34148d62011-11-18 06:49:11 +0000181 serial_assign(default_serial_console()->name);
wdenk7ac16102004-08-01 22:48:16 +0000182}
183
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200184static int serial_stub_start(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600185{
186 struct serial_device *dev = sdev->priv;
187
188 return dev->start();
189}
190
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200191static int serial_stub_stop(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600192{
193 struct serial_device *dev = sdev->priv;
194
195 return dev->stop();
196}
197
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200198static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600199{
200 struct serial_device *dev = sdev->priv;
201
202 dev->putc(ch);
203}
204
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200205static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600206{
207 struct serial_device *dev = sdev->priv;
208
209 dev->puts(str);
210}
211
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900212static int serial_stub_getc(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600213{
214 struct serial_device *dev = sdev->priv;
215
216 return dev->getc();
217}
218
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900219static int serial_stub_tstc(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600220{
221 struct serial_device *dev = sdev->priv;
222
223 return dev->tstc();
224}
225
Marek Vasut95656ea2012-10-08 11:36:39 +0000226/**
227 * serial_stdio_init() - Register serial ports with STDIO core
228 *
229 * This function generates a proxy driver for each serial port driver.
230 * These proxy drivers then register with the STDIO core, making the
231 * serial drivers available as STDIO devices.
232 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000233void serial_stdio_init(void)
wdenk7ac16102004-08-01 22:48:16 +0000234{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200235 struct stdio_dev dev;
wdenk7ac16102004-08-01 22:48:16 +0000236 struct serial_device *s = serial_devices;
237
wdenk8f08c5b2004-10-11 22:43:02 +0000238 while (s) {
Gerlando Falauto34148d62011-11-18 06:49:11 +0000239 memset(&dev, 0, sizeof(dev));
wdenk7ac16102004-08-01 22:48:16 +0000240
Gerlando Falauto34148d62011-11-18 06:49:11 +0000241 strcpy(dev.name, s->name);
wdenk7ac16102004-08-01 22:48:16 +0000242 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
243
Simon Glass0d1e1f72014-07-23 06:54:59 -0600244 dev.start = serial_stub_start;
245 dev.stop = serial_stub_stop;
246 dev.putc = serial_stub_putc;
247 dev.puts = serial_stub_puts;
248 dev.getc = serial_stub_getc;
249 dev.tstc = serial_stub_tstc;
Simon Glassac2a14f2014-09-04 16:27:23 -0600250 dev.priv = s;
wdenk7ac16102004-08-01 22:48:16 +0000251
Gerlando Falauto34148d62011-11-18 06:49:11 +0000252 stdio_register(&dev);
wdenk7ac16102004-08-01 22:48:16 +0000253
254 s = s->next;
255 }
256}
257
Marek Vasut95656ea2012-10-08 11:36:39 +0000258/**
259 * serial_assign() - Select the serial output device by name
260 * @name: Name of the serial driver to be used as default output
261 *
262 * This function configures the serial output multiplexing by
263 * selecting which serial device will be used as default. In case
264 * the STDIO "serial" device is selected as stdin/stdout/stderr,
265 * the serial device previously configured by this function will be
266 * used for the particular operation.
267 *
268 * Returns 0 on success, negative on error.
269 */
Gerlando Falauto92999582011-11-18 06:49:12 +0000270int serial_assign(const char *name)
wdenk7ac16102004-08-01 22:48:16 +0000271{
272 struct serial_device *s;
273
wdenk8f08c5b2004-10-11 22:43:02 +0000274 for (s = serial_devices; s; s = s->next) {
Marek Vasut0e1b5772012-10-06 14:07:03 +0000275 if (strcmp(s->name, name))
276 continue;
277 serial_current = s;
278 return 0;
wdenk7ac16102004-08-01 22:48:16 +0000279 }
280
Marek Vasut0e1b5772012-10-06 14:07:03 +0000281 return -EINVAL;
wdenk7ac16102004-08-01 22:48:16 +0000282}
283
Marek Vasut95656ea2012-10-08 11:36:39 +0000284/**
285 * serial_reinit_all() - Reinitialize all compiled-in serial ports
286 *
287 * This function reinitializes all serial ports that are compiled
288 * into U-Boot by calling their serial_start() functions.
289 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000290void serial_reinit_all(void)
wdenk7ac16102004-08-01 22:48:16 +0000291{
292 struct serial_device *s;
293
Gerlando Falauto34148d62011-11-18 06:49:11 +0000294 for (s = serial_devices; s; s = s->next)
Marek Vasutb46931d2012-09-07 14:35:31 +0200295 s->start();
wdenk7ac16102004-08-01 22:48:16 +0000296}
297
Marek Vasut95656ea2012-10-08 11:36:39 +0000298/**
299 * get_current() - Return pointer to currently selected serial port
300 *
301 * This function returns a pointer to currently selected serial port.
302 * The currently selected serial port is altered by serial_assign()
303 * function.
304 *
305 * In case this function is called before relocation or before any serial
306 * port is configured, this function calls default_serial_console() to
307 * determine the serial port. Otherwise, the configured serial port is
308 * returned.
309 *
310 * Returns pointer to the currently selected serial port on success,
311 * NULL on error.
312 */
Simon Glass8f4e6f12011-08-19 11:09:43 +0000313static struct serial_device *get_current(void)
wdenk7ac16102004-08-01 22:48:16 +0000314{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000315 struct serial_device *dev;
316
Marek Vasutbdad8072012-10-06 14:07:04 +0000317 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass8f4e6f12011-08-19 11:09:43 +0000318 dev = default_serial_console();
Marek Vasutbdad8072012-10-06 14:07:04 +0000319 else if (!serial_current)
320 dev = default_serial_console();
321 else
322 dev = serial_current;
wdenk8f08c5b2004-10-11 22:43:02 +0000323
Marek Vasutbdad8072012-10-06 14:07:04 +0000324 /* We must have a console device */
325 if (!dev) {
Marek Vasuta6f63342012-09-15 10:33:51 +0200326#ifdef CONFIG_SPL_BUILD
Marek Vasutbdad8072012-10-06 14:07:04 +0000327 puts("Cannot find console\n");
328 hang();
Marek Vasuta6f63342012-09-15 10:33:51 +0200329#else
Marek Vasutbdad8072012-10-06 14:07:04 +0000330 panic("Cannot find console\n");
Marek Vasuta6f63342012-09-15 10:33:51 +0200331#endif
Marek Vasutbdad8072012-10-06 14:07:04 +0000332 }
333
Simon Glass8f4e6f12011-08-19 11:09:43 +0000334 return dev;
335}
wdenk7ac16102004-08-01 22:48:16 +0000336
Marek Vasut95656ea2012-10-08 11:36:39 +0000337/**
338 * serial_init() - Initialize currently selected serial port
339 *
340 * This function initializes the currently selected serial port. This
341 * usually involves setting up the registers of that particular port,
342 * enabling clock and such. This function uses the get_current() call
343 * to determine which port is selected.
344 *
345 * Returns 0 on success, negative on error.
346 */
Simon Glass8f4e6f12011-08-19 11:09:43 +0000347int serial_init(void)
348{
Simon Glassb9d5f6b2014-07-23 06:55:07 -0600349 gd->flags |= GD_FLG_SERIAL_READY;
Marek Vasutb46931d2012-09-07 14:35:31 +0200350 return get_current()->start();
wdenk7ac16102004-08-01 22:48:16 +0000351}
352
Marek Vasut95656ea2012-10-08 11:36:39 +0000353/**
354 * serial_setbrg() - Configure baud-rate of currently selected serial port
355 *
356 * This function configures the baud-rate of the currently selected
357 * serial port. The baud-rate is retrieved from global data within
358 * the serial port driver. This function uses the get_current() call
359 * to determine which port is selected.
360 *
361 * Returns 0 on success, negative on error.
362 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000363void serial_setbrg(void)
wdenk7ac16102004-08-01 22:48:16 +0000364{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000365 get_current()->setbrg();
wdenk7ac16102004-08-01 22:48:16 +0000366}
367
Marek Vasut95656ea2012-10-08 11:36:39 +0000368/**
369 * serial_getc() - Read character from currently selected serial port
370 *
371 * This function retrieves a character from currently selected serial
372 * port. In case there is no character waiting on the serial port,
373 * this function will block and wait for the character to appear. This
374 * function uses the get_current() call to determine which port is
375 * selected.
376 *
377 * Returns the character on success, negative on error.
378 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000379int serial_getc(void)
wdenk7ac16102004-08-01 22:48:16 +0000380{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000381 return get_current()->getc();
wdenk7ac16102004-08-01 22:48:16 +0000382}
383
Marek Vasut95656ea2012-10-08 11:36:39 +0000384/**
385 * serial_tstc() - Test if data is available on currently selected serial port
386 *
387 * This function tests if one or more characters are available on
388 * currently selected serial port. This function never blocks. This
389 * function uses the get_current() call to determine which port is
390 * selected.
391 *
392 * Returns positive if character is available, zero otherwise.
393 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000394int serial_tstc(void)
wdenk7ac16102004-08-01 22:48:16 +0000395{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000396 return get_current()->tstc();
wdenk7ac16102004-08-01 22:48:16 +0000397}
398
Marek Vasut95656ea2012-10-08 11:36:39 +0000399/**
400 * serial_putc() - Output character via currently selected serial port
401 * @c: Single character to be output from the serial port.
402 *
403 * This function outputs a character via currently selected serial
404 * port. This character is passed to the serial port driver responsible
405 * for controlling the hardware. The hardware may still be in process
406 * of transmitting another character, therefore this function may block
407 * for a short amount of time. This function uses the get_current()
408 * call to determine which port is selected.
409 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000410void serial_putc(const char c)
wdenk7ac16102004-08-01 22:48:16 +0000411{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000412 get_current()->putc(c);
wdenk7ac16102004-08-01 22:48:16 +0000413}
414
Marek Vasut95656ea2012-10-08 11:36:39 +0000415/**
416 * serial_puts() - Output string via currently selected serial port
417 * @s: Zero-terminated string to be output from the serial port.
418 *
419 * This function outputs a zero-terminated string via currently
420 * selected serial port. This function behaves as an accelerator
421 * in case the hardware can queue multiple characters for transfer.
422 * The whole string that is to be output is available to the function
423 * implementing the hardware manipulation. Transmitting the whole
424 * string may take some time, thus this function may block for some
425 * amount of time. This function uses the get_current() call to
426 * determine which port is selected.
427 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000428void serial_puts(const char *s)
wdenk7ac16102004-08-01 22:48:16 +0000429{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000430 get_current()->puts(s);
wdenk7ac16102004-08-01 22:48:16 +0000431}
Mike Frysinger078f2f12011-05-14 06:56:15 +0000432
Marek Vasut95656ea2012-10-08 11:36:39 +0000433/**
434 * default_serial_puts() - Output string by calling serial_putc() in loop
435 * @s: Zero-terminated string to be output from the serial port.
436 *
437 * This function outputs a zero-terminated string by calling serial_putc()
438 * in a loop. Most drivers do not support queueing more than one byte for
439 * transfer, thus this function precisely implements their serial_puts().
440 *
441 * To optimize the number of get_current() calls, this function only
442 * calls get_current() once and then directly accesses the putc() call
443 * of the &struct serial_device .
444 */
Marek Vasut6b9124e2012-10-06 14:07:01 +0000445void default_serial_puts(const char *s)
446{
447 struct serial_device *dev = get_current();
448 while (*s)
449 dev->putc(*s++);
450}
451
Mike Frysinger078f2f12011-05-14 06:56:15 +0000452#if CONFIG_POST & CONFIG_SYS_POST_UART
453static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
454
Marek Vasut95656ea2012-10-08 11:36:39 +0000455/**
456 * uart_post_test() - Test the currently selected serial port using POST
457 * @flags: POST framework flags
458 *
459 * Do a loopback test of the currently selected serial port. This
460 * function is only useful in the context of the POST testing framwork.
Vagrant Cascadianbeb288b2015-11-24 14:46:24 -0800461 * The serial port is first configured into loopback mode and then
Marek Vasut95656ea2012-10-08 11:36:39 +0000462 * characters are sent through it.
463 *
464 * Returns 0 on success, value otherwise.
465 */
Mike Frysinger078f2f12011-05-14 06:56:15 +0000466/* Mark weak until post/cpu/.../uart.c migrate over */
467__weak
468int uart_post_test(int flags)
469{
470 unsigned char c;
471 int ret, saved_baud, b;
472 struct serial_device *saved_dev, *s;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000473
474 /* Save current serial state */
475 ret = 0;
476 saved_dev = serial_current;
Masahiro Yamada197c7202014-04-04 20:09:58 +0900477 saved_baud = gd->baudrate;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000478
479 for (s = serial_devices; s; s = s->next) {
480 /* If this driver doesn't support loop back, skip it */
481 if (!s->loop)
482 continue;
483
484 /* Test the next device */
485 serial_current = s;
486
487 ret = serial_init();
488 if (ret)
489 goto done;
490
491 /* Consume anything that happens to be queued */
492 while (serial_tstc())
493 serial_getc();
494
495 /* Enable loop back */
496 s->loop(1);
497
498 /* Test every available baud rate */
499 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
Masahiro Yamada197c7202014-04-04 20:09:58 +0900500 gd->baudrate = bauds[b];
Mike Frysinger078f2f12011-05-14 06:56:15 +0000501 serial_setbrg();
502
503 /*
504 * Stick to printable chars to avoid issues:
505 * - terminal corruption
506 * - serial program reacting to sequences and sending
507 * back random extra data
508 * - most serial drivers add in extra chars (like \r\n)
509 */
510 for (c = 0x20; c < 0x7f; ++c) {
511 /* Send it out */
512 serial_putc(c);
513
514 /* Make sure it's the same one */
515 ret = (c != serial_getc());
516 if (ret) {
517 s->loop(0);
518 goto done;
519 }
520
521 /* Clean up the output in case it was sent */
522 serial_putc('\b');
523 ret = ('\b' != serial_getc());
524 if (ret) {
525 s->loop(0);
526 goto done;
527 }
528 }
529 }
530
531 /* Disable loop back */
532 s->loop(0);
533
Marek Vasutb46931d2012-09-07 14:35:31 +0200534 /* XXX: There is no serial_stop() !? */
535 if (s->stop)
536 s->stop();
Mike Frysinger078f2f12011-05-14 06:56:15 +0000537 }
538
539 done:
540 /* Restore previous serial state */
541 serial_current = saved_dev;
Masahiro Yamada197c7202014-04-04 20:09:58 +0900542 gd->baudrate = saved_baud;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000543 serial_reinit_all();
544 serial_setbrg();
545
546 return ret;
547}
548#endif