blob: baeaeaac8ea2fa236503358f2162776c6c96b55a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk281e00a2004-08-01 22:48:16 +00002/*
3 * (C) Copyright 2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk281e00a2004-08-01 22:48:16 +00005 */
6
7#include <common.h>
Simon Glassf3998fd2019-08-02 09:44:25 -06008#include <env_internal.h>
Simon Glassdb41d652019-12-28 10:45:07 -07009#include <hang.h>
wdenk281e00a2004-08-01 22:48:16 +000010#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020011#include <stdio_dev.h>
Mike Frysinger7b826c22011-05-14 06:56:15 +000012#include <post.h>
13#include <linux/compiler.h>
Marek Vasut6d93e252012-10-06 14:07:03 +000014#include <errno.h>
wdenk281e00a2004-08-01 22:48:16 +000015
Wolfgang Denkd87080b2006-03-31 18:32:53 +020016DECLARE_GLOBAL_DATA_PTR;
17
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +000018static struct serial_device *serial_devices;
19static struct serial_device *serial_current;
Joe Hershberger32057712012-12-11 22:16:27 -060020/*
21 * Table with supported baudrates (defined in config_xyz.h)
22 */
23static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
wdenk281e00a2004-08-01 22:48:16 +000024
Marek Vasut9cd2b9e2012-10-08 11:36:39 +000025/**
26 * serial_null() - Void registration routine of a serial driver
27 *
28 * This routine implements a void registration routine of a serial
29 * driver. The registration routine of a particular driver is aliased
30 * to this empty function in case the driver is not compiled into
31 * U-Boot.
32 */
Marek Vasut2a333ae2012-09-12 17:49:58 +020033static void serial_null(void)
34{
35}
36
Marek Vasut9cd2b9e2012-10-08 11:36:39 +000037/**
Joe Hershberger32057712012-12-11 22:16:27 -060038 * on_baudrate() - Update the actual baudrate when the env var changes
39 *
Heinrich Schuchardt938b05a2018-07-29 10:41:02 +020040 * @name: changed environment variable
41 * @value: new value of the environment variable
42 * @op: operation (create, overwrite, or delete)
43 * @flags: attributes of environment variable change,
44 * see flags H_* in include/search.h
45 *
Joe Hershberger32057712012-12-11 22:16:27 -060046 * This will check for a valid baudrate and only apply it if valid.
Heinrich Schuchardt938b05a2018-07-29 10:41:02 +020047 *
48 * Return: 0 on success, 1 on error
Joe Hershberger32057712012-12-11 22:16:27 -060049 */
50static int on_baudrate(const char *name, const char *value, enum env_op op,
51 int flags)
52{
53 int i;
54 int baudrate;
55
56 switch (op) {
57 case env_op_create:
58 case env_op_overwrite:
59 /*
60 * Switch to new baudrate if new baudrate is supported
61 */
62 baudrate = simple_strtoul(value, NULL, 10);
63
64 /* Not actually changing */
65 if (gd->baudrate == baudrate)
66 return 0;
67
Axel Lin99351752013-06-23 00:46:41 +080068 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
Joe Hershberger32057712012-12-11 22:16:27 -060069 if (baudrate == baudrate_table[i])
70 break;
71 }
Axel Lin99351752013-06-23 00:46:41 +080072 if (i == ARRAY_SIZE(baudrate_table)) {
Joe Hershberger32057712012-12-11 22:16:27 -060073 if ((flags & H_FORCE) == 0)
74 printf("## Baudrate %d bps not supported\n",
75 baudrate);
76 return 1;
77 }
78 if ((flags & H_INTERACTIVE) != 0) {
79 printf("## Switch baudrate to %d"
80 " bps and press ENTER ...\n", baudrate);
81 udelay(50000);
82 }
83
84 gd->baudrate = baudrate;
Joe Hershberger32057712012-12-11 22:16:27 -060085
86 serial_setbrg();
87
88 udelay(50000);
89
90 if ((flags & H_INTERACTIVE) != 0)
91 while (1) {
92 if (getc() == '\r')
93 break;
94 }
95
96 return 0;
97 case env_op_delete:
98 printf("## Baudrate may not be deleted\n");
99 return 1;
100 default:
101 return 0;
102 }
103}
104U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
105
106/**
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000107 * serial_initfunc() - Forward declare of driver registration routine
108 * @name: Name of the real driver registration routine.
109 *
110 * This macro expands onto forward declaration of a driver registration
111 * routine, which is then used below in serial_initialize() function.
112 * The declaration is made weak and aliases to serial_null() so in case
113 * the driver is not compiled in, the function is still declared and can
114 * be used, but aliases to serial_null() and thus is optimized away.
115 */
Marek Vasut2a333ae2012-09-12 17:49:58 +0200116#define serial_initfunc(name) \
117 void name(void) \
118 __attribute__((weak, alias("serial_null")));
119
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100120serial_initfunc(atmel_serial_initialize);
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100121serial_initfunc(mcf_serial_initialize);
Marek Vasut7a311542012-09-13 01:38:52 +0200122serial_initfunc(mpc85xx_serial_initialize);
Marek Vasuta9434722012-09-14 22:37:43 +0200123serial_initfunc(mxc_serial_initialize);
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100124serial_initfunc(ns16550_serial_initialize);
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100125serial_initfunc(pl01x_serial_initialize);
126serial_initfunc(pxa_serial_initialize);
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100127serial_initfunc(sh_serial_initialize);
Weijie Gao44fa6762019-09-25 17:45:18 +0800128serial_initfunc(mtk_serial_initialize);
Marek Vasutf0eb1f62012-09-12 13:50:56 +0200129
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000130/**
131 * serial_register() - Register serial driver with serial driver core
132 * @dev: Pointer to the serial driver structure
133 *
134 * This function registers the serial driver supplied via @dev with
135 * serial driver core, thus making U-Boot aware of it and making it
136 * available for U-Boot to use. On platforms that still require manual
137 * relocation of constant variables, relocation of the supplied structure
138 * is performed.
139 */
Mike Frysingerc52b4f72011-04-29 18:03:30 +0000140void serial_register(struct serial_device *dev)
wdenk281e00a2004-08-01 22:48:16 +0000141{
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200142#ifdef CONFIG_NEEDS_MANUAL_RELOC
Marek Vasutf2760c42012-09-16 18:54:22 +0200143 if (dev->start)
144 dev->start += gd->reloc_off;
145 if (dev->stop)
146 dev->stop += gd->reloc_off;
147 if (dev->setbrg)
148 dev->setbrg += gd->reloc_off;
149 if (dev->getc)
150 dev->getc += gd->reloc_off;
151 if (dev->tstc)
152 dev->tstc += gd->reloc_off;
153 if (dev->putc)
154 dev->putc += gd->reloc_off;
155 if (dev->puts)
156 dev->puts += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -0500157#endif
wdenk281e00a2004-08-01 22:48:16 +0000158
159 dev->next = serial_devices;
160 serial_devices = dev;
wdenk281e00a2004-08-01 22:48:16 +0000161}
162
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000163/**
164 * serial_initialize() - Register all compiled-in serial port drivers
165 *
166 * This function registers all serial port drivers that are compiled
167 * into the U-Boot binary with the serial core, thus making them
168 * available to U-Boot to use. Lastly, this function assigns a default
169 * serial port to the serial core. That serial port is then used as a
170 * default output.
171 */
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000172void serial_initialize(void)
wdenk281e00a2004-08-01 22:48:16 +0000173{
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100174 atmel_serial_initialize();
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100175 mcf_serial_initialize();
Marek Vasut7a311542012-09-13 01:38:52 +0200176 mpc85xx_serial_initialize();
Marek Vasuta9434722012-09-14 22:37:43 +0200177 mxc_serial_initialize();
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100178 ns16550_serial_initialize();
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100179 pl01x_serial_initialize();
180 pxa_serial_initialize();
Jeroen Hofstee94a255d2014-10-27 20:10:07 +0100181 sh_serial_initialize();
Weijie Gao44fa6762019-09-25 17:45:18 +0800182 mtk_serial_initialize();
Marek Vasut7b953c52012-09-13 01:16:50 +0200183
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000184 serial_assign(default_serial_console()->name);
wdenk281e00a2004-08-01 22:48:16 +0000185}
186
Jeroen Hofstee654f8d02014-10-08 22:57:44 +0200187static int serial_stub_start(struct stdio_dev *sdev)
Simon Glass709ea542014-07-23 06:54:59 -0600188{
189 struct serial_device *dev = sdev->priv;
190
191 return dev->start();
192}
193
Jeroen Hofstee654f8d02014-10-08 22:57:44 +0200194static int serial_stub_stop(struct stdio_dev *sdev)
Simon Glass709ea542014-07-23 06:54:59 -0600195{
196 struct serial_device *dev = sdev->priv;
197
198 return dev->stop();
199}
200
Jeroen Hofstee654f8d02014-10-08 22:57:44 +0200201static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass709ea542014-07-23 06:54:59 -0600202{
203 struct serial_device *dev = sdev->priv;
204
205 dev->putc(ch);
206}
207
Jeroen Hofstee654f8d02014-10-08 22:57:44 +0200208static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
Simon Glass709ea542014-07-23 06:54:59 -0600209{
210 struct serial_device *dev = sdev->priv;
211
212 dev->puts(str);
213}
214
Masahiro Yamada49ddcf32017-06-22 16:48:49 +0900215static int serial_stub_getc(struct stdio_dev *sdev)
Simon Glass709ea542014-07-23 06:54:59 -0600216{
217 struct serial_device *dev = sdev->priv;
218
219 return dev->getc();
220}
221
Masahiro Yamada49ddcf32017-06-22 16:48:49 +0900222static int serial_stub_tstc(struct stdio_dev *sdev)
Simon Glass709ea542014-07-23 06:54:59 -0600223{
224 struct serial_device *dev = sdev->priv;
225
226 return dev->tstc();
227}
228
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000229/**
230 * serial_stdio_init() - Register serial ports with STDIO core
231 *
232 * This function generates a proxy driver for each serial port driver.
233 * These proxy drivers then register with the STDIO core, making the
234 * serial drivers available as STDIO devices.
235 */
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000236void serial_stdio_init(void)
wdenk281e00a2004-08-01 22:48:16 +0000237{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200238 struct stdio_dev dev;
wdenk281e00a2004-08-01 22:48:16 +0000239 struct serial_device *s = serial_devices;
240
wdenk2ee66532004-10-11 22:43:02 +0000241 while (s) {
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000242 memset(&dev, 0, sizeof(dev));
wdenk281e00a2004-08-01 22:48:16 +0000243
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000244 strcpy(dev.name, s->name);
wdenk281e00a2004-08-01 22:48:16 +0000245 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
246
Simon Glass709ea542014-07-23 06:54:59 -0600247 dev.start = serial_stub_start;
248 dev.stop = serial_stub_stop;
249 dev.putc = serial_stub_putc;
250 dev.puts = serial_stub_puts;
251 dev.getc = serial_stub_getc;
252 dev.tstc = serial_stub_tstc;
Simon Glassaddf9512014-09-04 16:27:23 -0600253 dev.priv = s;
wdenk281e00a2004-08-01 22:48:16 +0000254
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000255 stdio_register(&dev);
wdenk281e00a2004-08-01 22:48:16 +0000256
257 s = s->next;
258 }
259}
260
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000261/**
262 * serial_assign() - Select the serial output device by name
263 * @name: Name of the serial driver to be used as default output
264 *
265 * This function configures the serial output multiplexing by
266 * selecting which serial device will be used as default. In case
267 * the STDIO "serial" device is selected as stdin/stdout/stderr,
268 * the serial device previously configured by this function will be
269 * used for the particular operation.
270 *
271 * Returns 0 on success, negative on error.
272 */
Gerlando Falauto7813ca92011-11-18 06:49:12 +0000273int serial_assign(const char *name)
wdenk281e00a2004-08-01 22:48:16 +0000274{
275 struct serial_device *s;
276
wdenk2ee66532004-10-11 22:43:02 +0000277 for (s = serial_devices; s; s = s->next) {
Marek Vasut6d93e252012-10-06 14:07:03 +0000278 if (strcmp(s->name, name))
279 continue;
280 serial_current = s;
281 return 0;
wdenk281e00a2004-08-01 22:48:16 +0000282 }
283
Marek Vasut6d93e252012-10-06 14:07:03 +0000284 return -EINVAL;
wdenk281e00a2004-08-01 22:48:16 +0000285}
286
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000287/**
288 * serial_reinit_all() - Reinitialize all compiled-in serial ports
289 *
290 * This function reinitializes all serial ports that are compiled
291 * into U-Boot by calling their serial_start() functions.
292 */
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000293void serial_reinit_all(void)
wdenk281e00a2004-08-01 22:48:16 +0000294{
295 struct serial_device *s;
296
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000297 for (s = serial_devices; s; s = s->next)
Marek Vasut89143fb2012-09-07 14:35:31 +0200298 s->start();
wdenk281e00a2004-08-01 22:48:16 +0000299}
300
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000301/**
302 * get_current() - Return pointer to currently selected serial port
303 *
304 * This function returns a pointer to currently selected serial port.
305 * The currently selected serial port is altered by serial_assign()
306 * function.
307 *
308 * In case this function is called before relocation or before any serial
309 * port is configured, this function calls default_serial_console() to
310 * determine the serial port. Otherwise, the configured serial port is
311 * returned.
312 *
313 * Returns pointer to the currently selected serial port on success,
314 * NULL on error.
315 */
Simon Glass857c2832011-08-19 11:09:43 +0000316static struct serial_device *get_current(void)
wdenk281e00a2004-08-01 22:48:16 +0000317{
Simon Glass857c2832011-08-19 11:09:43 +0000318 struct serial_device *dev;
319
Marek Vasutdee19412012-10-06 14:07:04 +0000320 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass857c2832011-08-19 11:09:43 +0000321 dev = default_serial_console();
Marek Vasutdee19412012-10-06 14:07:04 +0000322 else if (!serial_current)
323 dev = default_serial_console();
324 else
Simon Glass857c2832011-08-19 11:09:43 +0000325 dev = serial_current;
Marek Vasutdee19412012-10-06 14:07:04 +0000326
327 /* We must have a console device */
328 if (!dev) {
329#ifdef CONFIG_SPL_BUILD
330 puts("Cannot find console\n");
331 hang();
332#else
333 panic("Cannot find console\n");
334#endif
335 }
336
Simon Glass857c2832011-08-19 11:09:43 +0000337 return dev;
338}
wdenk281e00a2004-08-01 22:48:16 +0000339
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000340/**
341 * serial_init() - Initialize currently selected serial port
342 *
343 * This function initializes the currently selected serial port. This
344 * usually involves setting up the registers of that particular port,
345 * enabling clock and such. This function uses the get_current() call
346 * to determine which port is selected.
347 *
348 * Returns 0 on success, negative on error.
349 */
Simon Glass857c2832011-08-19 11:09:43 +0000350int serial_init(void)
351{
Simon Glass093f79a2014-07-23 06:55:07 -0600352 gd->flags |= GD_FLG_SERIAL_READY;
Marek Vasut89143fb2012-09-07 14:35:31 +0200353 return get_current()->start();
wdenk281e00a2004-08-01 22:48:16 +0000354}
355
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000356/**
357 * serial_setbrg() - Configure baud-rate of currently selected serial port
358 *
359 * This function configures the baud-rate of the currently selected
360 * serial port. The baud-rate is retrieved from global data within
361 * the serial port driver. This function uses the get_current() call
362 * to determine which port is selected.
363 *
364 * Returns 0 on success, negative on error.
365 */
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000366void serial_setbrg(void)
wdenk281e00a2004-08-01 22:48:16 +0000367{
Simon Glass857c2832011-08-19 11:09:43 +0000368 get_current()->setbrg();
wdenk281e00a2004-08-01 22:48:16 +0000369}
370
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000371/**
372 * serial_getc() - Read character from currently selected serial port
373 *
374 * This function retrieves a character from currently selected serial
375 * port. In case there is no character waiting on the serial port,
376 * this function will block and wait for the character to appear. This
377 * function uses the get_current() call to determine which port is
378 * selected.
379 *
380 * Returns the character on success, negative on error.
381 */
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000382int serial_getc(void)
wdenk281e00a2004-08-01 22:48:16 +0000383{
Simon Glass857c2832011-08-19 11:09:43 +0000384 return get_current()->getc();
wdenk281e00a2004-08-01 22:48:16 +0000385}
386
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000387/**
388 * serial_tstc() - Test if data is available on currently selected serial port
389 *
390 * This function tests if one or more characters are available on
391 * currently selected serial port. This function never blocks. This
392 * function uses the get_current() call to determine which port is
393 * selected.
394 *
395 * Returns positive if character is available, zero otherwise.
396 */
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000397int serial_tstc(void)
wdenk281e00a2004-08-01 22:48:16 +0000398{
Simon Glass857c2832011-08-19 11:09:43 +0000399 return get_current()->tstc();
wdenk281e00a2004-08-01 22:48:16 +0000400}
401
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000402/**
403 * serial_putc() - Output character via currently selected serial port
404 * @c: Single character to be output from the serial port.
405 *
406 * This function outputs a character via currently selected serial
407 * port. This character is passed to the serial port driver responsible
408 * for controlling the hardware. The hardware may still be in process
409 * of transmitting another character, therefore this function may block
410 * for a short amount of time. This function uses the get_current()
411 * call to determine which port is selected.
412 */
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000413void serial_putc(const char c)
wdenk281e00a2004-08-01 22:48:16 +0000414{
Simon Glass857c2832011-08-19 11:09:43 +0000415 get_current()->putc(c);
wdenk281e00a2004-08-01 22:48:16 +0000416}
417
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000418/**
419 * serial_puts() - Output string via currently selected serial port
420 * @s: Zero-terminated string to be output from the serial port.
421 *
422 * This function outputs a zero-terminated string via currently
423 * selected serial port. This function behaves as an accelerator
424 * in case the hardware can queue multiple characters for transfer.
425 * The whole string that is to be output is available to the function
426 * implementing the hardware manipulation. Transmitting the whole
427 * string may take some time, thus this function may block for some
428 * amount of time. This function uses the get_current() call to
429 * determine which port is selected.
430 */
Gerlando Falautoa6e6f7f2011-11-18 06:49:11 +0000431void serial_puts(const char *s)
wdenk281e00a2004-08-01 22:48:16 +0000432{
Simon Glass857c2832011-08-19 11:09:43 +0000433 get_current()->puts(s);
wdenk281e00a2004-08-01 22:48:16 +0000434}
Mike Frysinger7b826c22011-05-14 06:56:15 +0000435
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000436/**
437 * default_serial_puts() - Output string by calling serial_putc() in loop
438 * @s: Zero-terminated string to be output from the serial port.
439 *
440 * This function outputs a zero-terminated string by calling serial_putc()
441 * in a loop. Most drivers do not support queueing more than one byte for
442 * transfer, thus this function precisely implements their serial_puts().
443 *
444 * To optimize the number of get_current() calls, this function only
445 * calls get_current() once and then directly accesses the putc() call
446 * of the &struct serial_device .
447 */
Marek Vasutbfb7d7a2012-10-06 14:07:01 +0000448void default_serial_puts(const char *s)
449{
450 struct serial_device *dev = get_current();
451 while (*s)
452 dev->putc(*s++);
453}
454
Mike Frysinger7b826c22011-05-14 06:56:15 +0000455#if CONFIG_POST & CONFIG_SYS_POST_UART
456static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
457
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000458/**
459 * uart_post_test() - Test the currently selected serial port using POST
460 * @flags: POST framework flags
461 *
462 * Do a loopback test of the currently selected serial port. This
463 * function is only useful in the context of the POST testing framwork.
Vagrant Cascadian1b25e582015-11-24 14:46:24 -0800464 * The serial port is first configured into loopback mode and then
Marek Vasut9cd2b9e2012-10-08 11:36:39 +0000465 * characters are sent through it.
466 *
467 * Returns 0 on success, value otherwise.
468 */
Mike Frysinger7b826c22011-05-14 06:56:15 +0000469/* Mark weak until post/cpu/.../uart.c migrate over */
470__weak
471int uart_post_test(int flags)
472{
473 unsigned char c;
474 int ret, saved_baud, b;
475 struct serial_device *saved_dev, *s;
Mike Frysinger7b826c22011-05-14 06:56:15 +0000476
477 /* Save current serial state */
478 ret = 0;
479 saved_dev = serial_current;
Masahiro Yamada8e261572014-04-04 20:09:58 +0900480 saved_baud = gd->baudrate;
Mike Frysinger7b826c22011-05-14 06:56:15 +0000481
482 for (s = serial_devices; s; s = s->next) {
483 /* If this driver doesn't support loop back, skip it */
484 if (!s->loop)
485 continue;
486
487 /* Test the next device */
488 serial_current = s;
489
490 ret = serial_init();
491 if (ret)
492 goto done;
493
494 /* Consume anything that happens to be queued */
495 while (serial_tstc())
496 serial_getc();
497
498 /* Enable loop back */
499 s->loop(1);
500
501 /* Test every available baud rate */
502 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
Masahiro Yamada8e261572014-04-04 20:09:58 +0900503 gd->baudrate = bauds[b];
Mike Frysinger7b826c22011-05-14 06:56:15 +0000504 serial_setbrg();
505
506 /*
507 * Stick to printable chars to avoid issues:
508 * - terminal corruption
509 * - serial program reacting to sequences and sending
510 * back random extra data
511 * - most serial drivers add in extra chars (like \r\n)
512 */
513 for (c = 0x20; c < 0x7f; ++c) {
514 /* Send it out */
515 serial_putc(c);
516
517 /* Make sure it's the same one */
518 ret = (c != serial_getc());
519 if (ret) {
520 s->loop(0);
521 goto done;
522 }
523
524 /* Clean up the output in case it was sent */
525 serial_putc('\b');
526 ret = ('\b' != serial_getc());
527 if (ret) {
528 s->loop(0);
529 goto done;
530 }
531 }
532 }
533
534 /* Disable loop back */
535 s->loop(0);
536
Marek Vasut89143fb2012-09-07 14:35:31 +0200537 /* XXX: There is no serial_stop() !? */
538 if (s->stop)
539 s->stop();
Mike Frysinger7b826c22011-05-14 06:56:15 +0000540 }
541
542 done:
543 /* Restore previous serial state */
544 serial_current = saved_dev;
Masahiro Yamada8e261572014-04-04 20:09:58 +0900545 gd->baudrate = saved_baud;
Mike Frysinger7b826c22011-05-14 06:56:15 +0000546 serial_reinit_all();
547 serial_setbrg();
548
549 return ret;
550}
551#endif